blob: 04f4e6086cd05bda8ce0e9c927b1b54af3d0dd3a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Francois Romieu07d3f512007-02-21 22:40:46 +01002 * r8169.c: RealTek 8169/8168/8101 ethernet driver.
3 *
4 * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
5 * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
6 * Copyright (c) a lot of people too. Please respect their work.
7 *
8 * See MAINTAINERS file for support contact information.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11#include <linux/module.h>
12#include <linux/moduleparam.h>
13#include <linux/pci.h>
14#include <linux/netdevice.h>
15#include <linux/etherdevice.h>
16#include <linux/delay.h>
17#include <linux/ethtool.h>
18#include <linux/mii.h>
19#include <linux/if_vlan.h>
20#include <linux/crc32.h>
21#include <linux/in.h>
22#include <linux/ip.h>
23#include <linux/tcp.h>
24#include <linux/init.h>
25#include <linux/dma-mapping.h>
Rafael J. Wysockie1759442010-03-14 14:33:51 +000026#include <linux/pm_runtime.h>
françois romieubca03d52011-01-03 15:07:31 +000027#include <linux/firmware.h>
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +000028#include <linux/pci-aspm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Francois Romieu99f252b2007-04-02 22:59:59 +020030#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/io.h>
32#include <asm/irq.h>
33
Francois Romieu865c6522008-05-11 14:51:00 +020034#define RTL8169_VERSION "2.3LK-NAPI"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#define MODULENAME "r8169"
36#define PFX MODULENAME ": "
37
françois romieubca03d52011-01-03 15:07:31 +000038#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
39#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
hayeswang01dc7fe2011-03-21 01:50:28 +000040#define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
41#define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080042#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
françois romieubca03d52011-01-03 15:07:31 +000043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#ifdef RTL8169_DEBUG
45#define assert(expr) \
Francois Romieu5b0384f2006-08-16 16:00:01 +020046 if (!(expr)) { \
47 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
Harvey Harrisonb39d66a2008-08-20 16:52:04 -070048 #expr,__FILE__,__func__,__LINE__); \
Francois Romieu5b0384f2006-08-16 16:00:01 +020049 }
Joe Perches06fa7352007-10-18 21:15:00 +020050#define dprintk(fmt, args...) \
51 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#else
53#define assert(expr) do {} while (0)
54#define dprintk(fmt, args...) do {} while (0)
55#endif /* RTL8169_DEBUG */
56
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020057#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d2005-09-30 16:54:02 -070058 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#define TX_BUFFS_AVAIL(tp) \
61 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
64 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050065static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67/* MAC address length */
68#define MAC_ADDR_LEN 6
69
Francois Romieu9c14cea2008-07-05 00:21:15 +020070#define MAX_READ_REQUEST_SHIFT 12
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define RX_FIFO_THRESH 7 /* 7 means NO threshold, Rx buffer level before first PCI xfer. */
72#define RX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
73#define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
75#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
76
77#define R8169_REGS_SIZE 256
78#define R8169_NAPI_WEIGHT 64
79#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
80#define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
81#define RX_BUF_SIZE 1536 /* Rx Buffer size */
82#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
83#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
84
85#define RTL8169_TX_TIMEOUT (6*HZ)
86#define RTL8169_PHY_TIMEOUT (10*HZ)
87
françois romieuea8dbdd2009-03-15 01:10:50 +000088#define RTL_EEPROM_SIG cpu_to_le32(0x8129)
89#define RTL_EEPROM_SIG_MASK cpu_to_le32(0xffff)
Francois Romieue1564ec2008-10-16 22:46:13 +020090#define RTL_EEPROM_SIG_ADDR 0x0000
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/* write/read MMIO register */
93#define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
94#define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
95#define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
96#define RTL_R8(reg) readb (ioaddr + (reg))
97#define RTL_R16(reg) readw (ioaddr + (reg))
Junchang Wang06f555f2010-05-30 02:26:07 +000098#define RTL_R32(reg) readl (ioaddr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +0200101 RTL_GIGA_MAC_VER_01 = 0,
102 RTL_GIGA_MAC_VER_02,
103 RTL_GIGA_MAC_VER_03,
104 RTL_GIGA_MAC_VER_04,
105 RTL_GIGA_MAC_VER_05,
106 RTL_GIGA_MAC_VER_06,
107 RTL_GIGA_MAC_VER_07,
108 RTL_GIGA_MAC_VER_08,
109 RTL_GIGA_MAC_VER_09,
110 RTL_GIGA_MAC_VER_10,
111 RTL_GIGA_MAC_VER_11,
112 RTL_GIGA_MAC_VER_12,
113 RTL_GIGA_MAC_VER_13,
114 RTL_GIGA_MAC_VER_14,
115 RTL_GIGA_MAC_VER_15,
116 RTL_GIGA_MAC_VER_16,
117 RTL_GIGA_MAC_VER_17,
118 RTL_GIGA_MAC_VER_18,
119 RTL_GIGA_MAC_VER_19,
120 RTL_GIGA_MAC_VER_20,
121 RTL_GIGA_MAC_VER_21,
122 RTL_GIGA_MAC_VER_22,
123 RTL_GIGA_MAC_VER_23,
124 RTL_GIGA_MAC_VER_24,
125 RTL_GIGA_MAC_VER_25,
126 RTL_GIGA_MAC_VER_26,
127 RTL_GIGA_MAC_VER_27,
128 RTL_GIGA_MAC_VER_28,
129 RTL_GIGA_MAC_VER_29,
130 RTL_GIGA_MAC_VER_30,
131 RTL_GIGA_MAC_VER_31,
132 RTL_GIGA_MAC_VER_32,
133 RTL_GIGA_MAC_VER_33,
134 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135};
136
Francois Romieu2b7b4312011-04-18 22:53:24 -0700137enum rtl_tx_desc_version {
138 RTL_TD_0 = 0,
139 RTL_TD_1 = 1,
140};
141
Francois Romieu85bffe62011-04-27 08:22:39 +0200142#define _R(NAME,TD,FW) \
143 { .name = NAME, .txd_version = TD, .fw_name = FW }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800145static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 const char *name;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700147 enum rtl_tx_desc_version txd_version;
Francois Romieu85bffe62011-04-27 08:22:39 +0200148 const char *fw_name;
149} rtl_chip_infos[] = {
150 /* PCI devices. */
151 [RTL_GIGA_MAC_VER_01] =
152 _R("RTL8169", RTL_TD_0, NULL),
153 [RTL_GIGA_MAC_VER_02] =
154 _R("RTL8169s", RTL_TD_0, NULL),
155 [RTL_GIGA_MAC_VER_03] =
156 _R("RTL8110s", RTL_TD_0, NULL),
157 [RTL_GIGA_MAC_VER_04] =
158 _R("RTL8169sb/8110sb", RTL_TD_0, NULL),
159 [RTL_GIGA_MAC_VER_05] =
160 _R("RTL8169sc/8110sc", RTL_TD_0, NULL),
161 [RTL_GIGA_MAC_VER_06] =
162 _R("RTL8169sc/8110sc", RTL_TD_0, NULL),
163 /* PCI-E devices. */
164 [RTL_GIGA_MAC_VER_07] =
165 _R("RTL8102e", RTL_TD_1, NULL),
166 [RTL_GIGA_MAC_VER_08] =
167 _R("RTL8102e", RTL_TD_1, NULL),
168 [RTL_GIGA_MAC_VER_09] =
169 _R("RTL8102e", RTL_TD_1, NULL),
170 [RTL_GIGA_MAC_VER_10] =
171 _R("RTL8101e", RTL_TD_0, NULL),
172 [RTL_GIGA_MAC_VER_11] =
173 _R("RTL8168b/8111b", RTL_TD_0, NULL),
174 [RTL_GIGA_MAC_VER_12] =
175 _R("RTL8168b/8111b", RTL_TD_0, NULL),
176 [RTL_GIGA_MAC_VER_13] =
177 _R("RTL8101e", RTL_TD_0, NULL),
178 [RTL_GIGA_MAC_VER_14] =
179 _R("RTL8100e", RTL_TD_0, NULL),
180 [RTL_GIGA_MAC_VER_15] =
181 _R("RTL8100e", RTL_TD_0, NULL),
182 [RTL_GIGA_MAC_VER_16] =
183 _R("RTL8101e", RTL_TD_0, NULL),
184 [RTL_GIGA_MAC_VER_17] =
185 _R("RTL8168b/8111b", RTL_TD_0, NULL),
186 [RTL_GIGA_MAC_VER_18] =
187 _R("RTL8168cp/8111cp", RTL_TD_1, NULL),
188 [RTL_GIGA_MAC_VER_19] =
189 _R("RTL8168c/8111c", RTL_TD_1, NULL),
190 [RTL_GIGA_MAC_VER_20] =
191 _R("RTL8168c/8111c", RTL_TD_1, NULL),
192 [RTL_GIGA_MAC_VER_21] =
193 _R("RTL8168c/8111c", RTL_TD_1, NULL),
194 [RTL_GIGA_MAC_VER_22] =
195 _R("RTL8168c/8111c", RTL_TD_1, NULL),
196 [RTL_GIGA_MAC_VER_23] =
197 _R("RTL8168cp/8111cp", RTL_TD_1, NULL),
198 [RTL_GIGA_MAC_VER_24] =
199 _R("RTL8168cp/8111cp", RTL_TD_1, NULL),
200 [RTL_GIGA_MAC_VER_25] =
201 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1),
202 [RTL_GIGA_MAC_VER_26] =
203 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2),
204 [RTL_GIGA_MAC_VER_27] =
205 _R("RTL8168dp/8111dp", RTL_TD_1, NULL),
206 [RTL_GIGA_MAC_VER_28] =
207 _R("RTL8168dp/8111dp", RTL_TD_1, NULL),
208 [RTL_GIGA_MAC_VER_29] =
209 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1),
210 [RTL_GIGA_MAC_VER_30] =
211 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1),
212 [RTL_GIGA_MAC_VER_31] =
213 _R("RTL8168dp/8111dp", RTL_TD_1, NULL),
214 [RTL_GIGA_MAC_VER_32] =
215 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1),
216 [RTL_GIGA_MAC_VER_33] =
217 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218};
219#undef _R
220
Francois Romieubcf0bf92006-07-26 23:14:13 +0200221enum cfg_version {
222 RTL_CFG_0 = 0x00,
223 RTL_CFG_1,
224 RTL_CFG_2
225};
226
Francois Romieu07ce4062007-02-23 23:36:39 +0100227static void rtl_hw_start_8169(struct net_device *);
228static void rtl_hw_start_8168(struct net_device *);
229static void rtl_hw_start_8101(struct net_device *);
230
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000231static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200232 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200233 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Francois Romieud81bf552006-09-20 21:31:20 +0200234 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100235 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200236 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
237 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200238 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200239 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
240 { PCI_VENDOR_ID_LINKSYS, 0x1032,
241 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100242 { 0x0001, 0x8168,
243 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 {0,},
245};
246
247MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
248
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000249static int rx_buf_sz = 16383;
David S. Miller4300e8c2010-03-26 10:23:30 -0700250static int use_dac;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200251static struct {
252 u32 msg_enable;
253} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Francois Romieu07d3f512007-02-21 22:40:46 +0100255enum rtl_registers {
256 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100257 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100258 MAR0 = 8, /* Multicast filter. */
259 CounterAddrLow = 0x10,
260 CounterAddrHigh = 0x14,
261 TxDescStartAddrLow = 0x20,
262 TxDescStartAddrHigh = 0x24,
263 TxHDescStartAddrLow = 0x28,
264 TxHDescStartAddrHigh = 0x2c,
265 FLASH = 0x30,
266 ERSR = 0x36,
267 ChipCmd = 0x37,
268 TxPoll = 0x38,
269 IntrMask = 0x3c,
270 IntrStatus = 0x3e,
271 TxConfig = 0x40,
272 RxConfig = 0x44,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700273
274#define RTL_RX_CONFIG_MASK 0xff7e1880u
275
Francois Romieu07d3f512007-02-21 22:40:46 +0100276 RxMissed = 0x4c,
277 Cfg9346 = 0x50,
278 Config0 = 0x51,
279 Config1 = 0x52,
280 Config2 = 0x53,
281 Config3 = 0x54,
282 Config4 = 0x55,
283 Config5 = 0x56,
284 MultiIntr = 0x5c,
285 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100286 PHYstatus = 0x6c,
287 RxMaxSize = 0xda,
288 CPlusCmd = 0xe0,
289 IntrMitigate = 0xe2,
290 RxDescAddrLow = 0xe4,
291 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000292 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
293
294#define NoEarlyTx 0x3f /* Max value : no early transmit. */
295
296 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
297
298#define TxPacketMax (8064 >> 7)
299
Francois Romieu07d3f512007-02-21 22:40:46 +0100300 FuncEvent = 0xf0,
301 FuncEventMask = 0xf4,
302 FuncPresetState = 0xf8,
303 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304};
305
Francois Romieuf162a5d2008-06-01 22:37:49 +0200306enum rtl8110_registers {
307 TBICSR = 0x64,
308 TBI_ANAR = 0x68,
309 TBI_LPAR = 0x6a,
310};
311
312enum rtl8168_8101_registers {
313 CSIDR = 0x64,
314 CSIAR = 0x68,
315#define CSIAR_FLAG 0x80000000
316#define CSIAR_WRITE_CMD 0x80000000
317#define CSIAR_BYTE_ENABLE 0x0f
318#define CSIAR_BYTE_ENABLE_SHIFT 12
319#define CSIAR_ADDR_MASK 0x0fff
françois romieu065c27c2011-01-03 15:08:12 +0000320 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200321 EPHYAR = 0x80,
322#define EPHYAR_FLAG 0x80000000
323#define EPHYAR_WRITE_CMD 0x80000000
324#define EPHYAR_REG_MASK 0x1f
325#define EPHYAR_REG_SHIFT 16
326#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800327 DLLPR = 0xd0,
328#define PM_SWITCH (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200329 DBG_REG = 0xd1,
330#define FIX_NAK_1 (1 << 4)
331#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800332 TWSI = 0xd2,
333 MCU = 0xd3,
334#define EN_NDP (1 << 3)
335#define EN_OOB_RESET (1 << 2)
françois romieudaf9df62009-10-07 12:44:20 +0000336 EFUSEAR = 0xdc,
337#define EFUSEAR_FLAG 0x80000000
338#define EFUSEAR_WRITE_CMD 0x80000000
339#define EFUSEAR_READ_CMD 0x00000000
340#define EFUSEAR_REG_MASK 0x03ff
341#define EFUSEAR_REG_SHIFT 8
342#define EFUSEAR_DATA_MASK 0xff
Francois Romieuf162a5d2008-06-01 22:37:49 +0200343};
344
françois romieuc0e45c12011-01-03 15:08:04 +0000345enum rtl8168_registers {
françois romieub646d902011-01-03 15:08:21 +0000346 ERIDR = 0x70,
347 ERIAR = 0x74,
348#define ERIAR_FLAG 0x80000000
349#define ERIAR_WRITE_CMD 0x80000000
350#define ERIAR_READ_CMD 0x00000000
351#define ERIAR_ADDR_BYTE_ALIGN 4
352#define ERIAR_EXGMAC 0
353#define ERIAR_MSIX 1
354#define ERIAR_ASF 2
355#define ERIAR_TYPE_SHIFT 16
356#define ERIAR_BYTEEN 0x0f
357#define ERIAR_BYTEEN_SHIFT 12
françois romieuc0e45c12011-01-03 15:08:04 +0000358 EPHY_RXER_NUM = 0x7c,
359 OCPDR = 0xb0, /* OCP GPHY access */
360#define OCPDR_WRITE_CMD 0x80000000
361#define OCPDR_READ_CMD 0x00000000
362#define OCPDR_REG_MASK 0x7f
363#define OCPDR_GPHY_REG_SHIFT 16
364#define OCPDR_DATA_MASK 0xffff
365 OCPAR = 0xb4,
366#define OCPAR_FLAG 0x80000000
367#define OCPAR_GPHY_WRITE_CMD 0x8000f060
368#define OCPAR_GPHY_READ_CMD 0x0000f060
hayeswang01dc7fe2011-03-21 01:50:28 +0000369 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
370 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200371#define TXPLA_RST (1 << 29)
françois romieuc0e45c12011-01-03 15:08:04 +0000372};
373
Francois Romieu07d3f512007-02-21 22:40:46 +0100374enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100376 SYSErr = 0x8000,
377 PCSTimeout = 0x4000,
378 SWInt = 0x0100,
379 TxDescUnavail = 0x0080,
380 RxFIFOOver = 0x0040,
381 LinkChg = 0x0020,
382 RxOverflow = 0x0010,
383 TxErr = 0x0008,
384 TxOK = 0x0004,
385 RxErr = 0x0002,
386 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 /* RxStatusDesc */
Francois Romieu9dccf612006-05-14 12:31:17 +0200389 RxFOVF = (1 << 23),
390 RxRWT = (1 << 22),
391 RxRES = (1 << 21),
392 RxRUNT = (1 << 20),
393 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 /* ChipCmdBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100396 CmdReset = 0x10,
397 CmdRxEnb = 0x08,
398 CmdTxEnb = 0x04,
399 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Francois Romieu275391a2007-02-23 23:50:28 +0100401 /* TXPoll register p.5 */
402 HPQ = 0x80, /* Poll cmd on the high prio queue */
403 NPQ = 0x40, /* Poll cmd on the low prio queue */
404 FSWInt = 0x01, /* Forced software interrupt */
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100407 Cfg9346_Lock = 0x00,
408 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100411 AcceptErr = 0x20,
412 AcceptRunt = 0x10,
413 AcceptBroadcast = 0x08,
414 AcceptMulticast = 0x04,
415 AcceptMyPhys = 0x02,
416 AcceptAllPhys = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 /* RxConfigBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100419 RxCfgFIFOShift = 13,
420 RxCfgDMAShift = 8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 /* TxConfigBits */
423 TxInterFrameGapShift = 24,
424 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
425
Francois Romieu5d06a992006-02-23 00:47:58 +0100426 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200427 LEDS1 = (1 << 7),
428 LEDS0 = (1 << 6),
Francois Romieufbac58f2007-10-04 22:51:38 +0200429 MSIEnable = (1 << 5), /* Enable Message Signaled Interrupt */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200430 Speed_down = (1 << 4),
431 MEMMAP = (1 << 3),
432 IOMAP = (1 << 2),
433 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100434 PMEnable = (1 << 0), /* Power Management Enable */
435
Francois Romieu6dccd162007-02-13 23:38:05 +0100436 /* Config2 register p. 25 */
437 PCI_Clock_66MHz = 0x01,
438 PCI_Clock_33MHz = 0x00,
439
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100440 /* Config3 register p.25 */
441 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
442 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200443 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100444
Francois Romieu5d06a992006-02-23 00:47:58 +0100445 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100446 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
447 MWF = (1 << 5), /* Accept Multicast wakeup frame */
448 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200449 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100450 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100451 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 /* TBICSR p.28 */
454 TBIReset = 0x80000000,
455 TBILoopback = 0x40000000,
456 TBINwEnable = 0x20000000,
457 TBINwRestart = 0x10000000,
458 TBILinkOk = 0x02000000,
459 TBINwComplete = 0x01000000,
460
461 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200462 EnableBist = (1 << 15), // 8168 8101
463 Mac_dbgo_oe = (1 << 14), // 8168 8101
464 Normal_mode = (1 << 13), // unused
465 Force_half_dup = (1 << 12), // 8168 8101
466 Force_rxflow_en = (1 << 11), // 8168 8101
467 Force_txflow_en = (1 << 10), // 8168 8101
468 Cxpl_dbg_sel = (1 << 9), // 8168 8101
469 ASF = (1 << 8), // 8168 8101
470 PktCntrDisable = (1 << 7), // 8168 8101
471 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 RxVlan = (1 << 6),
473 RxChkSum = (1 << 5),
474 PCIDAC = (1 << 4),
475 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100476 INTT_0 = 0x0000, // 8168
477 INTT_1 = 0x0001, // 8168
478 INTT_2 = 0x0002, // 8168
479 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100482 TBI_Enable = 0x80,
483 TxFlowCtrl = 0x40,
484 RxFlowCtrl = 0x20,
485 _1000bpsF = 0x10,
486 _100bps = 0x08,
487 _10bps = 0x04,
488 LinkStatus = 0x02,
489 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100492 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200493
494 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100495 CounterDump = 0x8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496};
497
Francois Romieu2b7b4312011-04-18 22:53:24 -0700498enum rtl_desc_bit {
499 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
501 RingEnd = (1 << 30), /* End of descriptor ring */
502 FirstFrag = (1 << 29), /* First segment of a packet */
503 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700504};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Francois Romieu2b7b4312011-04-18 22:53:24 -0700506/* Generic case. */
507enum rtl_tx_desc_bit {
508 /* First doubleword. */
509 TD_LSO = (1 << 27), /* Large Send Offload */
510#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Francois Romieu2b7b4312011-04-18 22:53:24 -0700512 /* Second doubleword. */
513 TxVlanTag = (1 << 17), /* Add VLAN tag */
514};
515
516/* 8169, 8168b and 810x except 8102e. */
517enum rtl_tx_desc_bit_0 {
518 /* First doubleword. */
519#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
520 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
521 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
522 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
523};
524
525/* 8102e, 8168c and beyond. */
526enum rtl_tx_desc_bit_1 {
527 /* Second doubleword. */
528#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
529 TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
530 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
531 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
532};
533
534static const struct rtl_tx_desc_info {
535 struct {
536 u32 udp;
537 u32 tcp;
538 } checksum;
539 u16 mss_shift;
540 u16 opts_offset;
541} tx_desc_info [] = {
542 [RTL_TD_0] = {
543 .checksum = {
544 .udp = TD0_IP_CS | TD0_UDP_CS,
545 .tcp = TD0_IP_CS | TD0_TCP_CS
546 },
547 .mss_shift = TD0_MSS_SHIFT,
548 .opts_offset = 0
549 },
550 [RTL_TD_1] = {
551 .checksum = {
552 .udp = TD1_IP_CS | TD1_UDP_CS,
553 .tcp = TD1_IP_CS | TD1_TCP_CS
554 },
555 .mss_shift = TD1_MSS_SHIFT,
556 .opts_offset = 1
557 }
558};
559
560enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 /* Rx private */
562 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
563 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
564
565#define RxProtoUDP (PID1)
566#define RxProtoTCP (PID0)
567#define RxProtoIP (PID1 | PID0)
568#define RxProtoMask RxProtoIP
569
570 IPFail = (1 << 16), /* IP checksum failed */
571 UDPFail = (1 << 15), /* UDP/IP checksum failed */
572 TCPFail = (1 << 14), /* TCP/IP checksum failed */
573 RxVlanTag = (1 << 16), /* VLAN tag available */
574};
575
576#define RsvdMask 0x3fffc000
577
578struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200579 __le32 opts1;
580 __le32 opts2;
581 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582};
583
584struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200585 __le32 opts1;
586 __le32 opts2;
587 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588};
589
590struct ring_info {
591 struct sk_buff *skb;
592 u32 len;
593 u8 __pad[sizeof(void *) - sizeof(u32)];
594};
595
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200596enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200597 RTL_FEATURE_WOL = (1 << 0),
598 RTL_FEATURE_MSI = (1 << 1),
599 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200600};
601
Ivan Vecera355423d2009-02-06 21:49:57 -0800602struct rtl8169_counters {
603 __le64 tx_packets;
604 __le64 rx_packets;
605 __le64 tx_errors;
606 __le32 rx_errors;
607 __le16 rx_missed;
608 __le16 align_errors;
609 __le32 tx_one_collision;
610 __le32 tx_multi_collision;
611 __le64 rx_unicast;
612 __le64 rx_broadcast;
613 __le32 rx_multicast;
614 __le16 tx_aborted;
615 __le16 tx_underun;
616};
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618struct rtl8169_private {
619 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200620 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000621 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700622 struct napi_struct napi;
Francois Romieucecb5fd2011-04-01 10:21:07 +0200623 spinlock_t lock;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200624 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700625 u16 txd_version;
626 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
628 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
629 u32 dirty_rx;
630 u32 dirty_tx;
631 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
632 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
633 dma_addr_t TxPhyAddr;
634 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000635 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 struct timer_list timer;
638 u16 cp_cmd;
Francois Romieu0e485152007-02-20 00:00:26 +0100639 u16 intr_event;
640 u16 napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 u16 intr_mask;
françois romieuc0e45c12011-01-03 15:08:04 +0000642
643 struct mdio_ops {
644 void (*write)(void __iomem *, int, int);
645 int (*read)(void __iomem *, int);
646 } mdio_ops;
647
françois romieu065c27c2011-01-03 15:08:12 +0000648 struct pll_power_ops {
649 void (*down)(struct rtl8169_private *);
650 void (*up)(struct rtl8169_private *);
651 } pll_power_ops;
652
Oliver Neukum54405cd2011-01-06 21:55:13 +0100653 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Francois Romieuccdffb92008-07-26 14:26:06 +0200654 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000655 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100656 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000657 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800659 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
Francois Romieu9c14cea2008-07-05 00:21:15 +0200660 int pcie_cap;
David Howellsc4028952006-11-22 14:57:56 +0000661 struct delayed_work task;
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200662 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200663
664 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800665 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000666 u32 saved_wolopts;
françois romieuf1e02ed2011-01-13 13:07:53 +0000667
668 const struct firmware *fw;
François Romieu953a12c2011-04-24 17:38:48 +0200669#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670};
671
Ralf Baechle979b6c12005-06-13 14:30:40 -0700672MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700675MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200676module_param_named(debug, debug.msg_enable, int, 0);
677MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678MODULE_LICENSE("GPL");
679MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000680MODULE_FIRMWARE(FIRMWARE_8168D_1);
681MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000682MODULE_FIRMWARE(FIRMWARE_8168E_1);
683MODULE_FIRMWARE(FIRMWARE_8168E_2);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800684MODULE_FIRMWARE(FIRMWARE_8105E_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686static int rtl8169_open(struct net_device *dev);
Stephen Hemminger613573252009-08-31 19:50:58 +0000687static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
688 struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100689static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690static int rtl8169_init_ring(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100691static void rtl_hw_start(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692static int rtl8169_close(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100693static void rtl_set_rx_mode(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694static void rtl8169_tx_timeout(struct net_device *dev);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200695static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696static int rtl8169_rx_interrupt(struct net_device *, struct rtl8169_private *,
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700697 void __iomem *, u32 budget);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200698static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699static void rtl8169_down(struct net_device *dev);
Francois Romieu99f252b2007-04-02 22:59:59 +0200700static void rtl8169_rx_clear(struct rtl8169_private *tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700701static int rtl8169_poll(struct napi_struct *napi, int budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703static const unsigned int rtl8169_rx_config =
Francois Romieu5b0384f2006-08-16 16:00:01 +0200704 (RX_FIFO_THRESH << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
françois romieub646d902011-01-03 15:08:21 +0000706static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
707{
708 void __iomem *ioaddr = tp->mmio_addr;
709 int i;
710
711 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
712 for (i = 0; i < 20; i++) {
713 udelay(100);
714 if (RTL_R32(OCPAR) & OCPAR_FLAG)
715 break;
716 }
717 return RTL_R32(OCPDR);
718}
719
720static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
721{
722 void __iomem *ioaddr = tp->mmio_addr;
723 int i;
724
725 RTL_W32(OCPDR, data);
726 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
727 for (i = 0; i < 20; i++) {
728 udelay(100);
729 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
730 break;
731 }
732}
733
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800734static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000735{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800736 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000737 int i;
738
739 RTL_W8(ERIDR, cmd);
740 RTL_W32(ERIAR, 0x800010e8);
741 msleep(2);
742 for (i = 0; i < 5; i++) {
743 udelay(100);
744 if (!(RTL_R32(ERIDR) & ERIAR_FLAG))
745 break;
746 }
747
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800748 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000749}
750
751#define OOB_CMD_RESET 0x00
752#define OOB_CMD_DRIVER_START 0x05
753#define OOB_CMD_DRIVER_STOP 0x06
754
Francois Romieucecb5fd2011-04-01 10:21:07 +0200755static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
756{
757 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
758}
759
françois romieub646d902011-01-03 15:08:21 +0000760static void rtl8168_driver_start(struct rtl8169_private *tp)
761{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200762 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000763 int i;
764
765 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
766
Francois Romieucecb5fd2011-04-01 10:21:07 +0200767 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000768
françois romieub646d902011-01-03 15:08:21 +0000769 for (i = 0; i < 10; i++) {
770 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000771 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
françois romieub646d902011-01-03 15:08:21 +0000772 break;
773 }
774}
775
776static void rtl8168_driver_stop(struct rtl8169_private *tp)
777{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200778 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000779 int i;
780
781 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
782
Francois Romieucecb5fd2011-04-01 10:21:07 +0200783 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000784
françois romieub646d902011-01-03 15:08:21 +0000785 for (i = 0; i < 10; i++) {
786 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000787 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
françois romieub646d902011-01-03 15:08:21 +0000788 break;
789 }
790}
791
hayeswang4804b3b2011-03-21 01:50:29 +0000792static int r8168dp_check_dash(struct rtl8169_private *tp)
793{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200794 u16 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000795
Francois Romieucecb5fd2011-04-01 10:21:07 +0200796 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
hayeswang4804b3b2011-03-21 01:50:29 +0000797}
françois romieub646d902011-01-03 15:08:21 +0000798
françois romieu4da19632011-01-03 15:07:55 +0000799static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
801 int i;
802
Francois Romieua6baf3a2007-11-08 23:23:21 +0100803 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Francois Romieu23714082006-01-29 00:49:09 +0100805 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100806 /*
807 * Check if the RTL8169 has completed writing to the specified
808 * MII register.
809 */
Francois Romieu5b0384f2006-08-16 16:00:01 +0200810 if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 break;
Francois Romieu23714082006-01-29 00:49:09 +0100812 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
Timo Teräs024a07b2010-06-06 15:38:47 -0700814 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700815 * According to hardware specs a 20us delay is required after write
816 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700817 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700818 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
françois romieu4da19632011-01-03 15:07:55 +0000821static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
823 int i, value = -1;
824
Francois Romieua6baf3a2007-11-08 23:23:21 +0100825 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Francois Romieu23714082006-01-29 00:49:09 +0100827 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100828 /*
829 * Check if the RTL8169 has completed retrieving data from
830 * the specified MII register.
831 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (RTL_R32(PHYAR) & 0x80000000) {
Francois Romieua6baf3a2007-11-08 23:23:21 +0100833 value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 break;
835 }
Francois Romieu23714082006-01-29 00:49:09 +0100836 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
Timo Teräs81a95f02010-06-09 17:31:48 -0700838 /*
839 * According to hardware specs a 20us delay is required after read
840 * complete indication, but before sending next command.
841 */
842 udelay(20);
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return value;
845}
846
françois romieuc0e45c12011-01-03 15:08:04 +0000847static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
848{
849 int i;
850
851 RTL_W32(OCPDR, data |
852 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
853 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
854 RTL_W32(EPHY_RXER_NUM, 0);
855
856 for (i = 0; i < 100; i++) {
857 mdelay(1);
858 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
859 break;
860 }
861}
862
863static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
864{
865 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
866 (value & OCPDR_DATA_MASK));
867}
868
869static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
870{
871 int i;
872
873 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
874
875 mdelay(1);
876 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
877 RTL_W32(EPHY_RXER_NUM, 0);
878
879 for (i = 0; i < 100; i++) {
880 mdelay(1);
881 if (RTL_R32(OCPAR) & OCPAR_FLAG)
882 break;
883 }
884
885 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
886}
887
françois romieue6de30d2011-01-03 15:08:37 +0000888#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
889
890static void r8168dp_2_mdio_start(void __iomem *ioaddr)
891{
892 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
893}
894
895static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
896{
897 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
898}
899
900static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
901{
902 r8168dp_2_mdio_start(ioaddr);
903
904 r8169_mdio_write(ioaddr, reg_addr, value);
905
906 r8168dp_2_mdio_stop(ioaddr);
907}
908
909static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
910{
911 int value;
912
913 r8168dp_2_mdio_start(ioaddr);
914
915 value = r8169_mdio_read(ioaddr, reg_addr);
916
917 r8168dp_2_mdio_stop(ioaddr);
918
919 return value;
920}
921
françois romieu4da19632011-01-03 15:07:55 +0000922static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +0200923{
françois romieuc0e45c12011-01-03 15:08:04 +0000924 tp->mdio_ops.write(tp->mmio_addr, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +0200925}
926
françois romieu4da19632011-01-03 15:07:55 +0000927static int rtl_readphy(struct rtl8169_private *tp, int location)
928{
françois romieuc0e45c12011-01-03 15:08:04 +0000929 return tp->mdio_ops.read(tp->mmio_addr, location);
françois romieu4da19632011-01-03 15:07:55 +0000930}
931
932static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
933{
934 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
935}
936
937static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +0000938{
939 int val;
940
françois romieu4da19632011-01-03 15:07:55 +0000941 val = rtl_readphy(tp, reg_addr);
942 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +0000943}
944
Francois Romieuccdffb92008-07-26 14:26:06 +0200945static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
946 int val)
947{
948 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +0200949
françois romieu4da19632011-01-03 15:07:55 +0000950 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +0200951}
952
953static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
954{
955 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +0200956
françois romieu4da19632011-01-03 15:07:55 +0000957 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +0200958}
959
Francois Romieudacf8152008-08-02 20:44:13 +0200960static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
961{
962 unsigned int i;
963
964 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
965 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
966
967 for (i = 0; i < 100; i++) {
968 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
969 break;
970 udelay(10);
971 }
972}
973
974static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
975{
976 u16 value = 0xffff;
977 unsigned int i;
978
979 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
980
981 for (i = 0; i < 100; i++) {
982 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
983 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
984 break;
985 }
986 udelay(10);
987 }
988
989 return value;
990}
991
992static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
993{
994 unsigned int i;
995
996 RTL_W32(CSIDR, value);
997 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
998 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
999
1000 for (i = 0; i < 100; i++) {
1001 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
1002 break;
1003 udelay(10);
1004 }
1005}
1006
1007static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
1008{
1009 u32 value = ~0x00;
1010 unsigned int i;
1011
1012 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
1013 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1014
1015 for (i = 0; i < 100; i++) {
1016 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
1017 value = RTL_R32(CSIDR);
1018 break;
1019 }
1020 udelay(10);
1021 }
1022
1023 return value;
1024}
1025
françois romieudaf9df62009-10-07 12:44:20 +00001026static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1027{
1028 u8 value = 0xff;
1029 unsigned int i;
1030
1031 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1032
1033 for (i = 0; i < 300; i++) {
1034 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1035 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1036 break;
1037 }
1038 udelay(100);
1039 }
1040
1041 return value;
1042}
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044static void rtl8169_irq_mask_and_ack(void __iomem *ioaddr)
1045{
1046 RTL_W16(IntrMask, 0x0000);
1047
1048 RTL_W16(IntrStatus, 0xffff);
1049}
1050
1051static void rtl8169_asic_down(void __iomem *ioaddr)
1052{
1053 RTL_W8(ChipCmd, 0x00);
1054 rtl8169_irq_mask_and_ack(ioaddr);
1055 RTL_R16(CPlusCmd);
1056}
1057
françois romieu4da19632011-01-03 15:07:55 +00001058static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
françois romieu4da19632011-01-03 15:07:55 +00001060 void __iomem *ioaddr = tp->mmio_addr;
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return RTL_R32(TBICSR) & TBIReset;
1063}
1064
françois romieu4da19632011-01-03 15:07:55 +00001065static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
françois romieu4da19632011-01-03 15:07:55 +00001067 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068}
1069
1070static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1071{
1072 return RTL_R32(TBICSR) & TBILinkOk;
1073}
1074
1075static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1076{
1077 return RTL_R8(PHYstatus) & LinkStatus;
1078}
1079
françois romieu4da19632011-01-03 15:07:55 +00001080static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
françois romieu4da19632011-01-03 15:07:55 +00001082 void __iomem *ioaddr = tp->mmio_addr;
1083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1085}
1086
françois romieu4da19632011-01-03 15:07:55 +00001087static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
1089 unsigned int val;
1090
françois romieu4da19632011-01-03 15:07:55 +00001091 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1092 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093}
1094
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001095static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001096 struct rtl8169_private *tp,
1097 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
1099 unsigned long flags;
1100
1101 spin_lock_irqsave(&tp->lock, flags);
1102 if (tp->link_ok(ioaddr)) {
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001103 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001104 if (pm)
1105 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001107 if (net_ratelimit())
1108 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001109 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001111 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001112 if (pm)
1113 pm_schedule_suspend(&tp->pci_dev->dev, 100);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 spin_unlock_irqrestore(&tp->lock, flags);
1116}
1117
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001118static void rtl8169_check_link_status(struct net_device *dev,
1119 struct rtl8169_private *tp,
1120 void __iomem *ioaddr)
1121{
1122 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1123}
1124
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001125#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1126
1127static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1128{
1129 void __iomem *ioaddr = tp->mmio_addr;
1130 u8 options;
1131 u32 wolopts = 0;
1132
1133 options = RTL_R8(Config1);
1134 if (!(options & PMEnable))
1135 return 0;
1136
1137 options = RTL_R8(Config3);
1138 if (options & LinkUp)
1139 wolopts |= WAKE_PHY;
1140 if (options & MagicPacket)
1141 wolopts |= WAKE_MAGIC;
1142
1143 options = RTL_R8(Config5);
1144 if (options & UWF)
1145 wolopts |= WAKE_UCAST;
1146 if (options & BWF)
1147 wolopts |= WAKE_BCAST;
1148 if (options & MWF)
1149 wolopts |= WAKE_MCAST;
1150
1151 return wolopts;
1152}
1153
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001154static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1155{
1156 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001157
1158 spin_lock_irq(&tp->lock);
1159
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001160 wol->supported = WAKE_ANY;
1161 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001162
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001163 spin_unlock_irq(&tp->lock);
1164}
1165
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001166static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001167{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001168 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001169 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001170 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001171 u32 opt;
1172 u16 reg;
1173 u8 mask;
1174 } cfg[] = {
1175 { WAKE_ANY, Config1, PMEnable },
1176 { WAKE_PHY, Config3, LinkUp },
1177 { WAKE_MAGIC, Config3, MagicPacket },
1178 { WAKE_UCAST, Config5, UWF },
1179 { WAKE_BCAST, Config5, BWF },
1180 { WAKE_MCAST, Config5, MWF },
1181 { WAKE_ANY, Config5, LanWake }
1182 };
1183
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001184 RTL_W8(Cfg9346, Cfg9346_Unlock);
1185
1186 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1187 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001188 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001189 options |= cfg[i].mask;
1190 RTL_W8(cfg[i].reg, options);
1191 }
1192
1193 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001194}
1195
1196static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1197{
1198 struct rtl8169_private *tp = netdev_priv(dev);
1199
1200 spin_lock_irq(&tp->lock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001201
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001202 if (wol->wolopts)
1203 tp->features |= RTL_FEATURE_WOL;
1204 else
1205 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001206 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001207 spin_unlock_irq(&tp->lock);
1208
françois romieuea809072010-11-08 13:23:58 +00001209 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1210
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001211 return 0;
1212}
1213
Francois Romieu31bd2042011-04-26 18:58:59 +02001214static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1215{
Francois Romieu85bffe62011-04-27 08:22:39 +02001216 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001217}
1218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219static void rtl8169_get_drvinfo(struct net_device *dev,
1220 struct ethtool_drvinfo *info)
1221{
1222 struct rtl8169_private *tp = netdev_priv(dev);
1223
1224 strcpy(info->driver, MODULENAME);
1225 strcpy(info->version, RTL8169_VERSION);
1226 strcpy(info->bus_info, pci_name(tp->pci_dev));
Francois Romieu31bd2042011-04-26 18:58:59 +02001227 strncpy(info->fw_version, IS_ERR_OR_NULL(tp->fw) ? "N/A" :
1228 rtl_lookup_firmware_name(tp), sizeof(info->fw_version) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229}
1230
1231static int rtl8169_get_regs_len(struct net_device *dev)
1232{
1233 return R8169_REGS_SIZE;
1234}
1235
1236static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001237 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
1239 struct rtl8169_private *tp = netdev_priv(dev);
1240 void __iomem *ioaddr = tp->mmio_addr;
1241 int ret = 0;
1242 u32 reg;
1243
1244 reg = RTL_R32(TBICSR);
1245 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1246 (duplex == DUPLEX_FULL)) {
1247 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1248 } else if (autoneg == AUTONEG_ENABLE)
1249 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1250 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001251 netif_warn(tp, link, dev,
1252 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 ret = -EOPNOTSUPP;
1254 }
1255
1256 return ret;
1257}
1258
1259static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001260 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
1262 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001263 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001264 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Hayes Wang716b50a2011-02-22 17:26:18 +08001266 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001269 int auto_nego;
1270
françois romieu4da19632011-01-03 15:07:55 +00001271 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001272 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1273 ADVERTISE_100HALF | ADVERTISE_100FULL);
1274
1275 if (adv & ADVERTISED_10baseT_Half)
1276 auto_nego |= ADVERTISE_10HALF;
1277 if (adv & ADVERTISED_10baseT_Full)
1278 auto_nego |= ADVERTISE_10FULL;
1279 if (adv & ADVERTISED_100baseT_Half)
1280 auto_nego |= ADVERTISE_100HALF;
1281 if (adv & ADVERTISED_100baseT_Full)
1282 auto_nego |= ADVERTISE_100FULL;
1283
françois romieu3577aa12009-05-19 10:46:48 +00001284 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1285
françois romieu4da19632011-01-03 15:07:55 +00001286 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001287 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1288
1289 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001290 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001291 if (adv & ADVERTISED_1000baseT_Half)
1292 giga_ctrl |= ADVERTISE_1000HALF;
1293 if (adv & ADVERTISED_1000baseT_Full)
1294 giga_ctrl |= ADVERTISE_1000FULL;
1295 } else if (adv & (ADVERTISED_1000baseT_Half |
1296 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001297 netif_info(tp, link, dev,
1298 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001299 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
françois romieu3577aa12009-05-19 10:46:48 +00001302 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001303
françois romieu4da19632011-01-03 15:07:55 +00001304 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1305 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001306 } else {
1307 giga_ctrl = 0;
1308
1309 if (speed == SPEED_10)
1310 bmcr = 0;
1311 else if (speed == SPEED_100)
1312 bmcr = BMCR_SPEED100;
1313 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001314 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001315
1316 if (duplex == DUPLEX_FULL)
1317 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001318 }
1319
françois romieu4da19632011-01-03 15:07:55 +00001320 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001321
Francois Romieucecb5fd2011-04-01 10:21:07 +02001322 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1323 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001324 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001325 rtl_writephy(tp, 0x17, 0x2138);
1326 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001327 } else {
françois romieu4da19632011-01-03 15:07:55 +00001328 rtl_writephy(tp, 0x17, 0x2108);
1329 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001330 }
1331 }
1332
Oliver Neukum54405cd2011-01-06 21:55:13 +01001333 rc = 0;
1334out:
1335 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336}
1337
1338static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001339 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
1341 struct rtl8169_private *tp = netdev_priv(dev);
1342 int ret;
1343
Oliver Neukum54405cd2011-01-06 21:55:13 +01001344 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001345 if (ret < 0)
1346 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Francois Romieu4876cc12011-03-11 21:07:11 +01001348 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1349 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001351 }
1352out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 return ret;
1354}
1355
1356static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1357{
1358 struct rtl8169_private *tp = netdev_priv(dev);
1359 unsigned long flags;
1360 int ret;
1361
Francois Romieu4876cc12011-03-11 21:07:11 +01001362 del_timer_sync(&tp->timer);
1363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 spin_lock_irqsave(&tp->lock, flags);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001365 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001366 cmd->duplex, cmd->advertising);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return ret;
1370}
1371
Michał Mirosław350fb322011-04-08 06:35:56 +00001372static u32 rtl8169_fix_features(struct net_device *dev, u32 features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
Francois Romieu2b7b4312011-04-18 22:53:24 -07001374 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001375 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Michał Mirosław350fb322011-04-08 06:35:56 +00001377 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378}
1379
Michał Mirosław350fb322011-04-08 06:35:56 +00001380static int rtl8169_set_features(struct net_device *dev, u32 features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
1382 struct rtl8169_private *tp = netdev_priv(dev);
1383 void __iomem *ioaddr = tp->mmio_addr;
1384 unsigned long flags;
1385
1386 spin_lock_irqsave(&tp->lock, flags);
1387
Michał Mirosław350fb322011-04-08 06:35:56 +00001388 if (features & NETIF_F_RXCSUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 tp->cp_cmd |= RxChkSum;
1390 else
1391 tp->cp_cmd &= ~RxChkSum;
1392
Michał Mirosław350fb322011-04-08 06:35:56 +00001393 if (dev->features & NETIF_F_HW_VLAN_RX)
1394 tp->cp_cmd |= RxVlan;
1395 else
1396 tp->cp_cmd &= ~RxVlan;
1397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 RTL_W16(CPlusCmd, tp->cp_cmd);
1399 RTL_R16(CPlusCmd);
1400
1401 spin_unlock_irqrestore(&tp->lock, flags);
1402
1403 return 0;
1404}
1405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1407 struct sk_buff *skb)
1408{
Jesse Grosseab6d182010-10-20 13:56:03 +00001409 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1411}
1412
Francois Romieu7a8fc772011-03-01 17:18:33 +01001413static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
1415 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Francois Romieu7a8fc772011-03-01 17:18:33 +01001417 if (opts2 & RxVlanTag)
1418 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421}
1422
Francois Romieuccdffb92008-07-26 14:26:06 +02001423static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
1425 struct rtl8169_private *tp = netdev_priv(dev);
1426 void __iomem *ioaddr = tp->mmio_addr;
1427 u32 status;
1428
1429 cmd->supported =
1430 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1431 cmd->port = PORT_FIBRE;
1432 cmd->transceiver = XCVR_INTERNAL;
1433
1434 status = RTL_R32(TBICSR);
1435 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1436 cmd->autoneg = !!(status & TBINwEnable);
1437
David Decotigny70739492011-04-27 18:32:40 +00001438 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001440
1441 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442}
1443
Francois Romieuccdffb92008-07-26 14:26:06 +02001444static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
1446 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Francois Romieuccdffb92008-07-26 14:26:06 +02001448 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449}
1450
1451static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1452{
1453 struct rtl8169_private *tp = netdev_priv(dev);
1454 unsigned long flags;
Francois Romieuccdffb92008-07-26 14:26:06 +02001455 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457 spin_lock_irqsave(&tp->lock, flags);
1458
Francois Romieuccdffb92008-07-26 14:26:06 +02001459 rc = tp->get_settings(dev, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
1461 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieuccdffb92008-07-26 14:26:06 +02001462 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463}
1464
1465static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1466 void *p)
1467{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001468 struct rtl8169_private *tp = netdev_priv(dev);
1469 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Francois Romieu5b0384f2006-08-16 16:00:01 +02001471 if (regs->len > R8169_REGS_SIZE)
1472 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Francois Romieu5b0384f2006-08-16 16:00:01 +02001474 spin_lock_irqsave(&tp->lock, flags);
1475 memcpy_fromio(p, tp->mmio_addr, regs->len);
1476 spin_unlock_irqrestore(&tp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477}
1478
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001479static u32 rtl8169_get_msglevel(struct net_device *dev)
1480{
1481 struct rtl8169_private *tp = netdev_priv(dev);
1482
1483 return tp->msg_enable;
1484}
1485
1486static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1487{
1488 struct rtl8169_private *tp = netdev_priv(dev);
1489
1490 tp->msg_enable = value;
1491}
1492
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001493static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1494 "tx_packets",
1495 "rx_packets",
1496 "tx_errors",
1497 "rx_errors",
1498 "rx_missed",
1499 "align_errors",
1500 "tx_single_collisions",
1501 "tx_multi_collisions",
1502 "unicast",
1503 "broadcast",
1504 "multicast",
1505 "tx_aborted",
1506 "tx_underrun",
1507};
1508
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001509static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001510{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001511 switch (sset) {
1512 case ETH_SS_STATS:
1513 return ARRAY_SIZE(rtl8169_gstrings);
1514 default:
1515 return -EOPNOTSUPP;
1516 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001517}
1518
Ivan Vecera355423d2009-02-06 21:49:57 -08001519static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001520{
1521 struct rtl8169_private *tp = netdev_priv(dev);
1522 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001523 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001524 struct rtl8169_counters *counters;
1525 dma_addr_t paddr;
1526 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001527 int wait = 1000;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001528
Ivan Vecera355423d2009-02-06 21:49:57 -08001529 /*
1530 * Some chips are unable to dump tally counters when the receiver
1531 * is disabled.
1532 */
1533 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1534 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001535
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001536 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001537 if (!counters)
1538 return;
1539
1540 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001541 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001542 RTL_W32(CounterAddrLow, cmd);
1543 RTL_W32(CounterAddrLow, cmd | CounterDump);
1544
Ivan Vecera355423d2009-02-06 21:49:57 -08001545 while (wait--) {
1546 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
Ivan Vecera355423d2009-02-06 21:49:57 -08001547 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001548 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001549 }
1550 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001551 }
1552
1553 RTL_W32(CounterAddrLow, 0);
1554 RTL_W32(CounterAddrHigh, 0);
1555
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001556 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001557}
1558
Ivan Vecera355423d2009-02-06 21:49:57 -08001559static void rtl8169_get_ethtool_stats(struct net_device *dev,
1560 struct ethtool_stats *stats, u64 *data)
1561{
1562 struct rtl8169_private *tp = netdev_priv(dev);
1563
1564 ASSERT_RTNL();
1565
1566 rtl8169_update_counters(dev);
1567
1568 data[0] = le64_to_cpu(tp->counters.tx_packets);
1569 data[1] = le64_to_cpu(tp->counters.rx_packets);
1570 data[2] = le64_to_cpu(tp->counters.tx_errors);
1571 data[3] = le32_to_cpu(tp->counters.rx_errors);
1572 data[4] = le16_to_cpu(tp->counters.rx_missed);
1573 data[5] = le16_to_cpu(tp->counters.align_errors);
1574 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1575 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1576 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1577 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1578 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1579 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1580 data[12] = le16_to_cpu(tp->counters.tx_underun);
1581}
1582
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001583static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1584{
1585 switch(stringset) {
1586 case ETH_SS_STATS:
1587 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1588 break;
1589 }
1590}
1591
Jeff Garzik7282d492006-09-13 14:30:00 -04001592static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 .get_drvinfo = rtl8169_get_drvinfo,
1594 .get_regs_len = rtl8169_get_regs_len,
1595 .get_link = ethtool_op_get_link,
1596 .get_settings = rtl8169_get_settings,
1597 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001598 .get_msglevel = rtl8169_get_msglevel,
1599 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001601 .get_wol = rtl8169_get_wol,
1602 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001603 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001604 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001605 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606};
1607
Francois Romieu07d3f512007-02-21 22:40:46 +01001608static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02001609 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
Francois Romieu5d320a22011-05-08 17:47:36 +02001611 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01001612 /*
1613 * The driver currently handles the 8168Bf and the 8168Be identically
1614 * but they can be identified more specifically through the test below
1615 * if needed:
1616 *
1617 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001618 *
1619 * Same thing for the 8101Eb and the 8101Ec:
1620 *
1621 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001622 */
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001623 static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001625 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 int mac_version;
1627 } mac_info[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00001628 /* 8168E family. */
1629 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1630 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1631 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1632
Francois Romieu5b538df2008-07-20 16:22:45 +02001633 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001634 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1635 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001636 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001637
françois romieue6de30d2011-01-03 15:08:37 +00001638 /* 8168DP family. */
1639 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1640 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00001641 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00001642
Francois Romieuef808d52008-06-29 13:10:54 +02001643 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001644 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001645 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001646 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001647 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001648 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1649 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001650 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001651 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001652 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001653
1654 /* 8168B family. */
1655 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1656 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1657 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1658 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1659
1660 /* 8101 family. */
hayeswang36a0e6c2011-03-21 01:50:30 +00001661 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08001662 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1663 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1664 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001665 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1666 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1667 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1668 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1669 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1670 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001671 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001672 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001673 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001674 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1675 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001676 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1677 /* FIXME: where did these entries come from ? -- FR */
1678 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1679 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1680
1681 /* 8110 family. */
1682 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1683 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1684 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1685 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1686 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1687 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1688
Jean Delvaref21b75e2009-05-26 20:54:48 -07001689 /* Catch-all */
1690 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 }, *p = mac_info;
1692 u32 reg;
1693
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001694 reg = RTL_R32(TxConfig);
1695 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 p++;
1697 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02001698
1699 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1700 netif_notice(tp, probe, dev,
1701 "unknown MAC, using family default\n");
1702 tp->mac_version = default_version;
1703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704}
1705
1706static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1707{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001708 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709}
1710
Francois Romieu867763c2007-08-17 18:21:58 +02001711struct phy_reg {
1712 u16 reg;
1713 u16 val;
1714};
1715
françois romieu4da19632011-01-03 15:07:55 +00001716static void rtl_writephy_batch(struct rtl8169_private *tp,
1717 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02001718{
1719 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00001720 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02001721 regs++;
1722 }
1723}
1724
françois romieubca03d52011-01-03 15:07:31 +00001725#define PHY_READ 0x00000000
1726#define PHY_DATA_OR 0x10000000
1727#define PHY_DATA_AND 0x20000000
1728#define PHY_BJMPN 0x30000000
1729#define PHY_READ_EFUSE 0x40000000
1730#define PHY_READ_MAC_BYTE 0x50000000
1731#define PHY_WRITE_MAC_BYTE 0x60000000
1732#define PHY_CLEAR_READCOUNT 0x70000000
1733#define PHY_WRITE 0x80000000
1734#define PHY_READCOUNT_EQ_SKIP 0x90000000
1735#define PHY_COMP_EQ_SKIPN 0xa0000000
1736#define PHY_COMP_NEQ_SKIPN 0xb0000000
1737#define PHY_WRITE_PREVIOUS 0xc0000000
1738#define PHY_SKIPN 0xd0000000
1739#define PHY_DELAY_MS 0xe0000000
1740#define PHY_WRITE_ERI_WORD 0xf0000000
1741
1742static void
1743rtl_phy_write_fw(struct rtl8169_private *tp, const struct firmware *fw)
1744{
françois romieubca03d52011-01-03 15:07:31 +00001745 __le32 *phytable = (__le32 *)fw->data;
1746 struct net_device *dev = tp->dev;
hayeswang42b82dc2011-01-10 02:07:25 +00001747 size_t index, fw_size = fw->size / sizeof(*phytable);
1748 u32 predata, count;
françois romieubca03d52011-01-03 15:07:31 +00001749
1750 if (fw->size % sizeof(*phytable)) {
1751 netif_err(tp, probe, dev, "odd sized firmware %zd\n", fw->size);
1752 return;
1753 }
1754
hayeswang42b82dc2011-01-10 02:07:25 +00001755 for (index = 0; index < fw_size; index++) {
1756 u32 action = le32_to_cpu(phytable[index]);
1757 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00001758
hayeswang42b82dc2011-01-10 02:07:25 +00001759 switch(action & 0xf0000000) {
1760 case PHY_READ:
1761 case PHY_DATA_OR:
1762 case PHY_DATA_AND:
1763 case PHY_READ_EFUSE:
1764 case PHY_CLEAR_READCOUNT:
1765 case PHY_WRITE:
1766 case PHY_WRITE_PREVIOUS:
1767 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00001768 break;
1769
hayeswang42b82dc2011-01-10 02:07:25 +00001770 case PHY_BJMPN:
1771 if (regno > index) {
1772 netif_err(tp, probe, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001773 "Out of range of firmware\n");
hayeswang42b82dc2011-01-10 02:07:25 +00001774 return;
1775 }
1776 break;
1777 case PHY_READCOUNT_EQ_SKIP:
1778 if (index + 2 >= fw_size) {
1779 netif_err(tp, probe, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001780 "Out of range of firmware\n");
hayeswang42b82dc2011-01-10 02:07:25 +00001781 return;
1782 }
1783 break;
1784 case PHY_COMP_EQ_SKIPN:
1785 case PHY_COMP_NEQ_SKIPN:
1786 case PHY_SKIPN:
1787 if (index + 1 + regno >= fw_size) {
1788 netif_err(tp, probe, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001789 "Out of range of firmware\n");
hayeswang42b82dc2011-01-10 02:07:25 +00001790 return;
1791 }
1792 break;
1793
1794 case PHY_READ_MAC_BYTE:
1795 case PHY_WRITE_MAC_BYTE:
1796 case PHY_WRITE_ERI_WORD:
1797 default:
1798 netif_err(tp, probe, tp->dev,
1799 "Invalid action 0x%08x\n", action);
françois romieubca03d52011-01-03 15:07:31 +00001800 return;
1801 }
1802 }
1803
hayeswang42b82dc2011-01-10 02:07:25 +00001804 predata = 0;
1805 count = 0;
1806
1807 for (index = 0; index < fw_size; ) {
1808 u32 action = le32_to_cpu(phytable[index]);
françois romieubca03d52011-01-03 15:07:31 +00001809 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00001810 u32 regno = (action & 0x0fff0000) >> 16;
1811
1812 if (!action)
1813 break;
françois romieubca03d52011-01-03 15:07:31 +00001814
1815 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00001816 case PHY_READ:
1817 predata = rtl_readphy(tp, regno);
1818 count++;
1819 index++;
françois romieubca03d52011-01-03 15:07:31 +00001820 break;
hayeswang42b82dc2011-01-10 02:07:25 +00001821 case PHY_DATA_OR:
1822 predata |= data;
1823 index++;
1824 break;
1825 case PHY_DATA_AND:
1826 predata &= data;
1827 index++;
1828 break;
1829 case PHY_BJMPN:
1830 index -= regno;
1831 break;
1832 case PHY_READ_EFUSE:
1833 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
1834 index++;
1835 break;
1836 case PHY_CLEAR_READCOUNT:
1837 count = 0;
1838 index++;
1839 break;
1840 case PHY_WRITE:
1841 rtl_writephy(tp, regno, data);
1842 index++;
1843 break;
1844 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02001845 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00001846 break;
1847 case PHY_COMP_EQ_SKIPN:
1848 if (predata == data)
1849 index += regno;
1850 index++;
1851 break;
1852 case PHY_COMP_NEQ_SKIPN:
1853 if (predata != data)
1854 index += regno;
1855 index++;
1856 break;
1857 case PHY_WRITE_PREVIOUS:
1858 rtl_writephy(tp, regno, predata);
1859 index++;
1860 break;
1861 case PHY_SKIPN:
1862 index += regno + 1;
1863 break;
1864 case PHY_DELAY_MS:
1865 mdelay(data);
1866 index++;
1867 break;
1868
1869 case PHY_READ_MAC_BYTE:
1870 case PHY_WRITE_MAC_BYTE:
1871 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00001872 default:
1873 BUG();
1874 }
1875 }
1876}
1877
françois romieuf1e02ed2011-01-13 13:07:53 +00001878static void rtl_release_firmware(struct rtl8169_private *tp)
1879{
François Romieu953a12c2011-04-24 17:38:48 +02001880 if (!IS_ERR_OR_NULL(tp->fw))
1881 release_firmware(tp->fw);
1882 tp->fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00001883}
1884
François Romieu953a12c2011-04-24 17:38:48 +02001885static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00001886{
François Romieu953a12c2011-04-24 17:38:48 +02001887 const struct firmware *fw = tp->fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00001888
1889 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
François Romieu953a12c2011-04-24 17:38:48 +02001890 if (!IS_ERR_OR_NULL(fw))
1891 rtl_phy_write_fw(tp, fw);
1892}
1893
1894static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
1895{
1896 if (rtl_readphy(tp, reg) != val)
1897 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
1898 else
1899 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00001900}
1901
françois romieu4da19632011-01-03 15:07:55 +00001902static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001904 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00001905 { 0x1f, 0x0001 },
1906 { 0x06, 0x006e },
1907 { 0x08, 0x0708 },
1908 { 0x15, 0x4000 },
1909 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
françois romieu0b9b5712009-08-10 19:44:56 +00001911 { 0x1f, 0x0001 },
1912 { 0x03, 0x00a1 },
1913 { 0x02, 0x0008 },
1914 { 0x01, 0x0120 },
1915 { 0x00, 0x1000 },
1916 { 0x04, 0x0800 },
1917 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
françois romieu0b9b5712009-08-10 19:44:56 +00001919 { 0x03, 0xff41 },
1920 { 0x02, 0xdf60 },
1921 { 0x01, 0x0140 },
1922 { 0x00, 0x0077 },
1923 { 0x04, 0x7800 },
1924 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
françois romieu0b9b5712009-08-10 19:44:56 +00001926 { 0x03, 0x802f },
1927 { 0x02, 0x4f02 },
1928 { 0x01, 0x0409 },
1929 { 0x00, 0xf0f9 },
1930 { 0x04, 0x9800 },
1931 { 0x04, 0x9000 },
1932
1933 { 0x03, 0xdf01 },
1934 { 0x02, 0xdf20 },
1935 { 0x01, 0xff95 },
1936 { 0x00, 0xba00 },
1937 { 0x04, 0xa800 },
1938 { 0x04, 0xa000 },
1939
1940 { 0x03, 0xff41 },
1941 { 0x02, 0xdf20 },
1942 { 0x01, 0x0140 },
1943 { 0x00, 0x00bb },
1944 { 0x04, 0xb800 },
1945 { 0x04, 0xb000 },
1946
1947 { 0x03, 0xdf41 },
1948 { 0x02, 0xdc60 },
1949 { 0x01, 0x6340 },
1950 { 0x00, 0x007d },
1951 { 0x04, 0xd800 },
1952 { 0x04, 0xd000 },
1953
1954 { 0x03, 0xdf01 },
1955 { 0x02, 0xdf20 },
1956 { 0x01, 0x100a },
1957 { 0x00, 0xa0ff },
1958 { 0x04, 0xf800 },
1959 { 0x04, 0xf000 },
1960
1961 { 0x1f, 0x0000 },
1962 { 0x0b, 0x0000 },
1963 { 0x00, 0x9200 }
1964 };
1965
françois romieu4da19632011-01-03 15:07:55 +00001966 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967}
1968
françois romieu4da19632011-01-03 15:07:55 +00001969static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02001970{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001971 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02001972 { 0x1f, 0x0002 },
1973 { 0x01, 0x90d0 },
1974 { 0x1f, 0x0000 }
1975 };
1976
françois romieu4da19632011-01-03 15:07:55 +00001977 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02001978}
1979
françois romieu4da19632011-01-03 15:07:55 +00001980static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00001981{
1982 struct pci_dev *pdev = tp->pci_dev;
1983 u16 vendor_id, device_id;
1984
1985 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &vendor_id);
1986 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &device_id);
1987
1988 if ((vendor_id != PCI_VENDOR_ID_GIGABYTE) || (device_id != 0xe000))
1989 return;
1990
françois romieu4da19632011-01-03 15:07:55 +00001991 rtl_writephy(tp, 0x1f, 0x0001);
1992 rtl_writephy(tp, 0x10, 0xf01b);
1993 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00001994}
1995
françois romieu4da19632011-01-03 15:07:55 +00001996static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00001997{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001998 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00001999 { 0x1f, 0x0001 },
2000 { 0x04, 0x0000 },
2001 { 0x03, 0x00a1 },
2002 { 0x02, 0x0008 },
2003 { 0x01, 0x0120 },
2004 { 0x00, 0x1000 },
2005 { 0x04, 0x0800 },
2006 { 0x04, 0x9000 },
2007 { 0x03, 0x802f },
2008 { 0x02, 0x4f02 },
2009 { 0x01, 0x0409 },
2010 { 0x00, 0xf099 },
2011 { 0x04, 0x9800 },
2012 { 0x04, 0xa000 },
2013 { 0x03, 0xdf01 },
2014 { 0x02, 0xdf20 },
2015 { 0x01, 0xff95 },
2016 { 0x00, 0xba00 },
2017 { 0x04, 0xa800 },
2018 { 0x04, 0xf000 },
2019 { 0x03, 0xdf01 },
2020 { 0x02, 0xdf20 },
2021 { 0x01, 0x101a },
2022 { 0x00, 0xa0ff },
2023 { 0x04, 0xf800 },
2024 { 0x04, 0x0000 },
2025 { 0x1f, 0x0000 },
2026
2027 { 0x1f, 0x0001 },
2028 { 0x10, 0xf41b },
2029 { 0x14, 0xfb54 },
2030 { 0x18, 0xf5c7 },
2031 { 0x1f, 0x0000 },
2032
2033 { 0x1f, 0x0001 },
2034 { 0x17, 0x0cc0 },
2035 { 0x1f, 0x0000 }
2036 };
2037
françois romieu4da19632011-01-03 15:07:55 +00002038 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002039
françois romieu4da19632011-01-03 15:07:55 +00002040 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002041}
2042
françois romieu4da19632011-01-03 15:07:55 +00002043static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002044{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002045 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002046 { 0x1f, 0x0001 },
2047 { 0x04, 0x0000 },
2048 { 0x03, 0x00a1 },
2049 { 0x02, 0x0008 },
2050 { 0x01, 0x0120 },
2051 { 0x00, 0x1000 },
2052 { 0x04, 0x0800 },
2053 { 0x04, 0x9000 },
2054 { 0x03, 0x802f },
2055 { 0x02, 0x4f02 },
2056 { 0x01, 0x0409 },
2057 { 0x00, 0xf099 },
2058 { 0x04, 0x9800 },
2059 { 0x04, 0xa000 },
2060 { 0x03, 0xdf01 },
2061 { 0x02, 0xdf20 },
2062 { 0x01, 0xff95 },
2063 { 0x00, 0xba00 },
2064 { 0x04, 0xa800 },
2065 { 0x04, 0xf000 },
2066 { 0x03, 0xdf01 },
2067 { 0x02, 0xdf20 },
2068 { 0x01, 0x101a },
2069 { 0x00, 0xa0ff },
2070 { 0x04, 0xf800 },
2071 { 0x04, 0x0000 },
2072 { 0x1f, 0x0000 },
2073
2074 { 0x1f, 0x0001 },
2075 { 0x0b, 0x8480 },
2076 { 0x1f, 0x0000 },
2077
2078 { 0x1f, 0x0001 },
2079 { 0x18, 0x67c7 },
2080 { 0x04, 0x2000 },
2081 { 0x03, 0x002f },
2082 { 0x02, 0x4360 },
2083 { 0x01, 0x0109 },
2084 { 0x00, 0x3022 },
2085 { 0x04, 0x2800 },
2086 { 0x1f, 0x0000 },
2087
2088 { 0x1f, 0x0001 },
2089 { 0x17, 0x0cc0 },
2090 { 0x1f, 0x0000 }
2091 };
2092
françois romieu4da19632011-01-03 15:07:55 +00002093 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002094}
2095
françois romieu4da19632011-01-03 15:07:55 +00002096static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002097{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002098 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002099 { 0x10, 0xf41b },
2100 { 0x1f, 0x0000 }
2101 };
2102
françois romieu4da19632011-01-03 15:07:55 +00002103 rtl_writephy(tp, 0x1f, 0x0001);
2104 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002105
françois romieu4da19632011-01-03 15:07:55 +00002106 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002107}
2108
françois romieu4da19632011-01-03 15:07:55 +00002109static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002110{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002111 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002112 { 0x1f, 0x0001 },
2113 { 0x10, 0xf41b },
2114 { 0x1f, 0x0000 }
2115 };
2116
françois romieu4da19632011-01-03 15:07:55 +00002117 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002118}
2119
françois romieu4da19632011-01-03 15:07:55 +00002120static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002121{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002122 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002123 { 0x1f, 0x0000 },
2124 { 0x1d, 0x0f00 },
2125 { 0x1f, 0x0002 },
2126 { 0x0c, 0x1ec8 },
2127 { 0x1f, 0x0000 }
2128 };
2129
françois romieu4da19632011-01-03 15:07:55 +00002130 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002131}
2132
françois romieu4da19632011-01-03 15:07:55 +00002133static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002134{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002135 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002136 { 0x1f, 0x0001 },
2137 { 0x1d, 0x3d98 },
2138 { 0x1f, 0x0000 }
2139 };
2140
françois romieu4da19632011-01-03 15:07:55 +00002141 rtl_writephy(tp, 0x1f, 0x0000);
2142 rtl_patchphy(tp, 0x14, 1 << 5);
2143 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002144
françois romieu4da19632011-01-03 15:07:55 +00002145 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002146}
2147
françois romieu4da19632011-01-03 15:07:55 +00002148static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002149{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002150 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002151 { 0x1f, 0x0001 },
2152 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002153 { 0x1f, 0x0002 },
2154 { 0x00, 0x88d4 },
2155 { 0x01, 0x82b1 },
2156 { 0x03, 0x7002 },
2157 { 0x08, 0x9e30 },
2158 { 0x09, 0x01f0 },
2159 { 0x0a, 0x5500 },
2160 { 0x0c, 0x00c8 },
2161 { 0x1f, 0x0003 },
2162 { 0x12, 0xc096 },
2163 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002164 { 0x1f, 0x0000 },
2165 { 0x1f, 0x0000 },
2166 { 0x09, 0x2000 },
2167 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002168 };
2169
françois romieu4da19632011-01-03 15:07:55 +00002170 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002171
françois romieu4da19632011-01-03 15:07:55 +00002172 rtl_patchphy(tp, 0x14, 1 << 5);
2173 rtl_patchphy(tp, 0x0d, 1 << 5);
2174 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002175}
2176
françois romieu4da19632011-01-03 15:07:55 +00002177static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002178{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002179 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002180 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002181 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002182 { 0x03, 0x802f },
2183 { 0x02, 0x4f02 },
2184 { 0x01, 0x0409 },
2185 { 0x00, 0xf099 },
2186 { 0x04, 0x9800 },
2187 { 0x04, 0x9000 },
2188 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002189 { 0x1f, 0x0002 },
2190 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002191 { 0x06, 0x0761 },
2192 { 0x1f, 0x0003 },
2193 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002194 { 0x1f, 0x0000 }
2195 };
2196
françois romieu4da19632011-01-03 15:07:55 +00002197 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002198
françois romieu4da19632011-01-03 15:07:55 +00002199 rtl_patchphy(tp, 0x16, 1 << 0);
2200 rtl_patchphy(tp, 0x14, 1 << 5);
2201 rtl_patchphy(tp, 0x0d, 1 << 5);
2202 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002203}
2204
françois romieu4da19632011-01-03 15:07:55 +00002205static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002206{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002207 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002208 { 0x1f, 0x0001 },
2209 { 0x12, 0x2300 },
2210 { 0x1d, 0x3d98 },
2211 { 0x1f, 0x0002 },
2212 { 0x0c, 0x7eb8 },
2213 { 0x06, 0x5461 },
2214 { 0x1f, 0x0003 },
2215 { 0x16, 0x0f0a },
2216 { 0x1f, 0x0000 }
2217 };
2218
françois romieu4da19632011-01-03 15:07:55 +00002219 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002220
françois romieu4da19632011-01-03 15:07:55 +00002221 rtl_patchphy(tp, 0x16, 1 << 0);
2222 rtl_patchphy(tp, 0x14, 1 << 5);
2223 rtl_patchphy(tp, 0x0d, 1 << 5);
2224 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002225}
2226
françois romieu4da19632011-01-03 15:07:55 +00002227static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002228{
françois romieu4da19632011-01-03 15:07:55 +00002229 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002230}
2231
françois romieubca03d52011-01-03 15:07:31 +00002232static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002233{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002234 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002235 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002236 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002237 { 0x06, 0x4064 },
2238 { 0x07, 0x2863 },
2239 { 0x08, 0x059c },
2240 { 0x09, 0x26b4 },
2241 { 0x0a, 0x6a19 },
2242 { 0x0b, 0xdcc8 },
2243 { 0x10, 0xf06d },
2244 { 0x14, 0x7f68 },
2245 { 0x18, 0x7fd9 },
2246 { 0x1c, 0xf0ff },
2247 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002248 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002249 { 0x12, 0xf49f },
2250 { 0x13, 0x070b },
2251 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002252 { 0x14, 0x94c0 },
2253
2254 /*
2255 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002256 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002257 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002258 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002259 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002260 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002261 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002262 { 0x06, 0x5561 },
2263
2264 /*
2265 * Can not link to 1Gbps with bad cable
2266 * Decrease SNR threshold form 21.07dB to 19.04dB
2267 */
2268 { 0x1f, 0x0001 },
2269 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002270
2271 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002272 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002273 };
françois romieubca03d52011-01-03 15:07:31 +00002274 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002275
françois romieu4da19632011-01-03 15:07:55 +00002276 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002277
françois romieubca03d52011-01-03 15:07:31 +00002278 /*
2279 * Rx Error Issue
2280 * Fine Tune Switching regulator parameter
2281 */
françois romieu4da19632011-01-03 15:07:55 +00002282 rtl_writephy(tp, 0x1f, 0x0002);
2283 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2284 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002285
françois romieudaf9df62009-10-07 12:44:20 +00002286 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002287 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002288 { 0x1f, 0x0002 },
2289 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002290 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002291 { 0x05, 0x8330 },
2292 { 0x06, 0x669a },
2293 { 0x1f, 0x0002 }
2294 };
2295 int val;
2296
françois romieu4da19632011-01-03 15:07:55 +00002297 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002298
françois romieu4da19632011-01-03 15:07:55 +00002299 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002300
2301 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002302 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002303 0x0065, 0x0066, 0x0067, 0x0068,
2304 0x0069, 0x006a, 0x006b, 0x006c
2305 };
2306 int i;
2307
françois romieu4da19632011-01-03 15:07:55 +00002308 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002309
2310 val &= 0xff00;
2311 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002312 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002313 }
2314 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002315 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002316 { 0x1f, 0x0002 },
2317 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002318 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002319 { 0x05, 0x8330 },
2320 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002321 };
2322
françois romieu4da19632011-01-03 15:07:55 +00002323 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002324 }
2325
françois romieubca03d52011-01-03 15:07:31 +00002326 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002327 rtl_writephy(tp, 0x1f, 0x0002);
2328 rtl_patchphy(tp, 0x0d, 0x0300);
2329 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002330
françois romieubca03d52011-01-03 15:07:31 +00002331 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002332 rtl_writephy(tp, 0x1f, 0x0002);
2333 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2334 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002335
françois romieu4da19632011-01-03 15:07:55 +00002336 rtl_writephy(tp, 0x1f, 0x0005);
2337 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002338
2339 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002340
françois romieu4da19632011-01-03 15:07:55 +00002341 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002342}
2343
françois romieubca03d52011-01-03 15:07:31 +00002344static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002345{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002346 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002347 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002348 { 0x1f, 0x0001 },
2349 { 0x06, 0x4064 },
2350 { 0x07, 0x2863 },
2351 { 0x08, 0x059c },
2352 { 0x09, 0x26b4 },
2353 { 0x0a, 0x6a19 },
2354 { 0x0b, 0xdcc8 },
2355 { 0x10, 0xf06d },
2356 { 0x14, 0x7f68 },
2357 { 0x18, 0x7fd9 },
2358 { 0x1c, 0xf0ff },
2359 { 0x1d, 0x3d9c },
2360 { 0x1f, 0x0003 },
2361 { 0x12, 0xf49f },
2362 { 0x13, 0x070b },
2363 { 0x1a, 0x05ad },
2364 { 0x14, 0x94c0 },
2365
françois romieubca03d52011-01-03 15:07:31 +00002366 /*
2367 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002368 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002369 */
françois romieudaf9df62009-10-07 12:44:20 +00002370 { 0x1f, 0x0002 },
2371 { 0x06, 0x5561 },
2372 { 0x1f, 0x0005 },
2373 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002374 { 0x06, 0x5561 },
2375
2376 /*
2377 * Can not link to 1Gbps with bad cable
2378 * Decrease SNR threshold form 21.07dB to 19.04dB
2379 */
2380 { 0x1f, 0x0001 },
2381 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002382
2383 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002384 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002385 };
françois romieubca03d52011-01-03 15:07:31 +00002386 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002387
françois romieu4da19632011-01-03 15:07:55 +00002388 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002389
2390 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002391 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002392 { 0x1f, 0x0002 },
2393 { 0x05, 0x669a },
2394 { 0x1f, 0x0005 },
2395 { 0x05, 0x8330 },
2396 { 0x06, 0x669a },
2397
2398 { 0x1f, 0x0002 }
2399 };
2400 int val;
2401
françois romieu4da19632011-01-03 15:07:55 +00002402 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002403
françois romieu4da19632011-01-03 15:07:55 +00002404 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002405 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002406 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002407 0x0065, 0x0066, 0x0067, 0x0068,
2408 0x0069, 0x006a, 0x006b, 0x006c
2409 };
2410 int i;
2411
françois romieu4da19632011-01-03 15:07:55 +00002412 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002413
2414 val &= 0xff00;
2415 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002416 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002417 }
2418 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002419 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002420 { 0x1f, 0x0002 },
2421 { 0x05, 0x2642 },
2422 { 0x1f, 0x0005 },
2423 { 0x05, 0x8330 },
2424 { 0x06, 0x2642 }
2425 };
2426
françois romieu4da19632011-01-03 15:07:55 +00002427 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002428 }
2429
françois romieubca03d52011-01-03 15:07:31 +00002430 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002431 rtl_writephy(tp, 0x1f, 0x0002);
2432 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2433 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002434
françois romieubca03d52011-01-03 15:07:31 +00002435 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002436 rtl_writephy(tp, 0x1f, 0x0002);
2437 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002438
françois romieu4da19632011-01-03 15:07:55 +00002439 rtl_writephy(tp, 0x1f, 0x0005);
2440 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002441
2442 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002443
françois romieu4da19632011-01-03 15:07:55 +00002444 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002445}
2446
françois romieu4da19632011-01-03 15:07:55 +00002447static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002448{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002449 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002450 { 0x1f, 0x0002 },
2451 { 0x10, 0x0008 },
2452 { 0x0d, 0x006c },
2453
2454 { 0x1f, 0x0000 },
2455 { 0x0d, 0xf880 },
2456
2457 { 0x1f, 0x0001 },
2458 { 0x17, 0x0cc0 },
2459
2460 { 0x1f, 0x0001 },
2461 { 0x0b, 0xa4d8 },
2462 { 0x09, 0x281c },
2463 { 0x07, 0x2883 },
2464 { 0x0a, 0x6b35 },
2465 { 0x1d, 0x3da4 },
2466 { 0x1c, 0xeffd },
2467 { 0x14, 0x7f52 },
2468 { 0x18, 0x7fc6 },
2469 { 0x08, 0x0601 },
2470 { 0x06, 0x4063 },
2471 { 0x10, 0xf074 },
2472 { 0x1f, 0x0003 },
2473 { 0x13, 0x0789 },
2474 { 0x12, 0xf4bd },
2475 { 0x1a, 0x04fd },
2476 { 0x14, 0x84b0 },
2477 { 0x1f, 0x0000 },
2478 { 0x00, 0x9200 },
2479
2480 { 0x1f, 0x0005 },
2481 { 0x01, 0x0340 },
2482 { 0x1f, 0x0001 },
2483 { 0x04, 0x4000 },
2484 { 0x03, 0x1d21 },
2485 { 0x02, 0x0c32 },
2486 { 0x01, 0x0200 },
2487 { 0x00, 0x5554 },
2488 { 0x04, 0x4800 },
2489 { 0x04, 0x4000 },
2490 { 0x04, 0xf000 },
2491 { 0x03, 0xdf01 },
2492 { 0x02, 0xdf20 },
2493 { 0x01, 0x101a },
2494 { 0x00, 0xa0ff },
2495 { 0x04, 0xf800 },
2496 { 0x04, 0xf000 },
2497 { 0x1f, 0x0000 },
2498
2499 { 0x1f, 0x0007 },
2500 { 0x1e, 0x0023 },
2501 { 0x16, 0x0000 },
2502 { 0x1f, 0x0000 }
2503 };
2504
françois romieu4da19632011-01-03 15:07:55 +00002505 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002506}
2507
françois romieue6de30d2011-01-03 15:08:37 +00002508static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2509{
2510 static const struct phy_reg phy_reg_init[] = {
2511 { 0x1f, 0x0001 },
2512 { 0x17, 0x0cc0 },
2513
2514 { 0x1f, 0x0007 },
2515 { 0x1e, 0x002d },
2516 { 0x18, 0x0040 },
2517 { 0x1f, 0x0000 }
2518 };
2519
2520 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2521 rtl_patchphy(tp, 0x0d, 1 << 5);
2522}
2523
hayeswang01dc7fe2011-03-21 01:50:28 +00002524static void rtl8168e_hw_phy_config(struct rtl8169_private *tp)
2525{
2526 static const struct phy_reg phy_reg_init[] = {
2527 /* Enable Delay cap */
2528 { 0x1f, 0x0005 },
2529 { 0x05, 0x8b80 },
2530 { 0x06, 0xc896 },
2531 { 0x1f, 0x0000 },
2532
2533 /* Channel estimation fine tune */
2534 { 0x1f, 0x0001 },
2535 { 0x0b, 0x6c20 },
2536 { 0x07, 0x2872 },
2537 { 0x1c, 0xefff },
2538 { 0x1f, 0x0003 },
2539 { 0x14, 0x6420 },
2540 { 0x1f, 0x0000 },
2541
2542 /* Update PFM & 10M TX idle timer */
2543 { 0x1f, 0x0007 },
2544 { 0x1e, 0x002f },
2545 { 0x15, 0x1919 },
2546 { 0x1f, 0x0000 },
2547
2548 { 0x1f, 0x0007 },
2549 { 0x1e, 0x00ac },
2550 { 0x18, 0x0006 },
2551 { 0x1f, 0x0000 }
2552 };
2553
Francois Romieu15ecd032011-04-27 13:52:22 -07002554 rtl_apply_firmware(tp);
2555
hayeswang01dc7fe2011-03-21 01:50:28 +00002556 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2557
2558 /* DCO enable for 10M IDLE Power */
2559 rtl_writephy(tp, 0x1f, 0x0007);
2560 rtl_writephy(tp, 0x1e, 0x0023);
2561 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2562 rtl_writephy(tp, 0x1f, 0x0000);
2563
2564 /* For impedance matching */
2565 rtl_writephy(tp, 0x1f, 0x0002);
2566 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002567 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002568
2569 /* PHY auto speed down */
2570 rtl_writephy(tp, 0x1f, 0x0007);
2571 rtl_writephy(tp, 0x1e, 0x002d);
2572 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2573 rtl_writephy(tp, 0x1f, 0x0000);
2574 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2575
2576 rtl_writephy(tp, 0x1f, 0x0005);
2577 rtl_writephy(tp, 0x05, 0x8b86);
2578 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2579 rtl_writephy(tp, 0x1f, 0x0000);
2580
2581 rtl_writephy(tp, 0x1f, 0x0005);
2582 rtl_writephy(tp, 0x05, 0x8b85);
2583 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2584 rtl_writephy(tp, 0x1f, 0x0007);
2585 rtl_writephy(tp, 0x1e, 0x0020);
2586 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2587 rtl_writephy(tp, 0x1f, 0x0006);
2588 rtl_writephy(tp, 0x00, 0x5a00);
2589 rtl_writephy(tp, 0x1f, 0x0000);
2590 rtl_writephy(tp, 0x0d, 0x0007);
2591 rtl_writephy(tp, 0x0e, 0x003c);
2592 rtl_writephy(tp, 0x0d, 0x4007);
2593 rtl_writephy(tp, 0x0e, 0x0000);
2594 rtl_writephy(tp, 0x0d, 0x0000);
2595}
2596
françois romieu4da19632011-01-03 15:07:55 +00002597static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02002598{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002599 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02002600 { 0x1f, 0x0003 },
2601 { 0x08, 0x441d },
2602 { 0x01, 0x9100 },
2603 { 0x1f, 0x0000 }
2604 };
2605
françois romieu4da19632011-01-03 15:07:55 +00002606 rtl_writephy(tp, 0x1f, 0x0000);
2607 rtl_patchphy(tp, 0x11, 1 << 12);
2608 rtl_patchphy(tp, 0x19, 1 << 13);
2609 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002610
françois romieu4da19632011-01-03 15:07:55 +00002611 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02002612}
2613
Hayes Wang5a5e4442011-02-22 17:26:21 +08002614static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
2615{
2616 static const struct phy_reg phy_reg_init[] = {
2617 { 0x1f, 0x0005 },
2618 { 0x1a, 0x0000 },
2619 { 0x1f, 0x0000 },
2620
2621 { 0x1f, 0x0004 },
2622 { 0x1c, 0x0000 },
2623 { 0x1f, 0x0000 },
2624
2625 { 0x1f, 0x0001 },
2626 { 0x15, 0x7701 },
2627 { 0x1f, 0x0000 }
2628 };
2629
2630 /* Disable ALDPS before ram code */
2631 rtl_writephy(tp, 0x1f, 0x0000);
2632 rtl_writephy(tp, 0x18, 0x0310);
2633 msleep(100);
2634
François Romieu953a12c2011-04-24 17:38:48 +02002635 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08002636
2637 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2638}
2639
Francois Romieu5615d9f2007-08-17 17:50:46 +02002640static void rtl_hw_phy_config(struct net_device *dev)
2641{
2642 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002643
2644 rtl8169_print_mac_version(tp);
2645
2646 switch (tp->mac_version) {
2647 case RTL_GIGA_MAC_VER_01:
2648 break;
2649 case RTL_GIGA_MAC_VER_02:
2650 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00002651 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002652 break;
2653 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00002654 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002655 break;
françois romieu2e9558562009-08-10 19:44:19 +00002656 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00002657 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002658 break;
françois romieu8c7006a2009-08-10 19:43:29 +00002659 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00002660 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00002661 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02002662 case RTL_GIGA_MAC_VER_07:
2663 case RTL_GIGA_MAC_VER_08:
2664 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00002665 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002666 break;
Francois Romieu236b8082008-05-30 16:11:48 +02002667 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00002668 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002669 break;
2670 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00002671 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002672 break;
2673 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00002674 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002675 break;
Francois Romieu867763c2007-08-17 18:21:58 +02002676 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00002677 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002678 break;
2679 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00002680 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002681 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02002682 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00002683 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002684 break;
Francois Romieu197ff762008-06-28 13:16:02 +02002685 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00002686 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02002687 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02002688 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00002689 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002690 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02002691 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002692 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00002693 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02002694 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02002695 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00002696 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002697 break;
2698 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00002699 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002700 break;
2701 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00002702 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02002703 break;
françois romieue6de30d2011-01-03 15:08:37 +00002704 case RTL_GIGA_MAC_VER_28:
2705 rtl8168d_4_hw_phy_config(tp);
2706 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08002707 case RTL_GIGA_MAC_VER_29:
2708 case RTL_GIGA_MAC_VER_30:
2709 rtl8105e_hw_phy_config(tp);
2710 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02002711 case RTL_GIGA_MAC_VER_31:
2712 /* None. */
2713 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00002714 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00002715 case RTL_GIGA_MAC_VER_33:
Francois Romieu15ecd032011-04-27 13:52:22 -07002716 rtl8168e_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00002717 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02002718
Francois Romieu5615d9f2007-08-17 17:50:46 +02002719 default:
2720 break;
2721 }
2722}
2723
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724static void rtl8169_phy_timer(unsigned long __opaque)
2725{
2726 struct net_device *dev = (struct net_device *)__opaque;
2727 struct rtl8169_private *tp = netdev_priv(dev);
2728 struct timer_list *timer = &tp->timer;
2729 void __iomem *ioaddr = tp->mmio_addr;
2730 unsigned long timeout = RTL8169_PHY_TIMEOUT;
2731
Francois Romieubcf0bf92006-07-26 23:14:13 +02002732 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 spin_lock_irq(&tp->lock);
2735
françois romieu4da19632011-01-03 15:07:55 +00002736 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02002737 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 * A busy loop could burn quite a few cycles on nowadays CPU.
2739 * Let's delay the execution of the timer for a few ticks.
2740 */
2741 timeout = HZ/10;
2742 goto out_mod_timer;
2743 }
2744
2745 if (tp->link_ok(ioaddr))
2746 goto out_unlock;
2747
Joe Perchesbf82c182010-02-09 11:49:50 +00002748 netif_warn(tp, link, dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
françois romieu4da19632011-01-03 15:07:55 +00002750 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751
2752out_mod_timer:
2753 mod_timer(timer, jiffies + timeout);
2754out_unlock:
2755 spin_unlock_irq(&tp->lock);
2756}
2757
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758#ifdef CONFIG_NET_POLL_CONTROLLER
2759/*
2760 * Polling 'interrupt' - used by things like netconsole to send skbs
2761 * without having to re-enable interrupts. It's not called while
2762 * the interrupt routine is executing.
2763 */
2764static void rtl8169_netpoll(struct net_device *dev)
2765{
2766 struct rtl8169_private *tp = netdev_priv(dev);
2767 struct pci_dev *pdev = tp->pci_dev;
2768
2769 disable_irq(pdev->irq);
David Howells7d12e782006-10-05 14:55:46 +01002770 rtl8169_interrupt(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 enable_irq(pdev->irq);
2772}
2773#endif
2774
2775static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
2776 void __iomem *ioaddr)
2777{
2778 iounmap(ioaddr);
2779 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00002780 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 pci_disable_device(pdev);
2782 free_netdev(dev);
2783}
2784
Francois Romieubf793292006-11-01 00:53:05 +01002785static void rtl8169_phy_reset(struct net_device *dev,
2786 struct rtl8169_private *tp)
2787{
Francois Romieu07d3f512007-02-21 22:40:46 +01002788 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01002789
françois romieu4da19632011-01-03 15:07:55 +00002790 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01002791 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00002792 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01002793 return;
2794 msleep(1);
2795 }
Joe Perchesbf82c182010-02-09 11:49:50 +00002796 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01002797}
2798
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002799static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002801 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002802
Francois Romieu5615d9f2007-08-17 17:50:46 +02002803 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002804
Marcus Sundberg773328942008-07-10 21:28:08 +02002805 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
2806 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2807 RTL_W8(0x82, 0x01);
2808 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002809
Francois Romieu6dccd162007-02-13 23:38:05 +01002810 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
2811
2812 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
2813 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002814
Francois Romieubcf0bf92006-07-26 23:14:13 +02002815 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002816 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2817 RTL_W8(0x82, 0x01);
2818 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00002819 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002820 }
2821
Francois Romieubf793292006-11-01 00:53:05 +01002822 rtl8169_phy_reset(dev, tp);
2823
Oliver Neukum54405cd2011-01-06 21:55:13 +01002824 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002825 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
2826 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
2827 (tp->mii.supports_gmii ?
2828 ADVERTISED_1000baseT_Half |
2829 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002830
Joe Perchesbf82c182010-02-09 11:49:50 +00002831 if (RTL_R8(PHYstatus) & TBI_Enable)
2832 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002833}
2834
Francois Romieu773d2022007-01-31 23:47:43 +01002835static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
2836{
2837 void __iomem *ioaddr = tp->mmio_addr;
2838 u32 high;
2839 u32 low;
2840
2841 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
2842 high = addr[4] | (addr[5] << 8);
2843
2844 spin_lock_irq(&tp->lock);
2845
2846 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00002847
Francois Romieu773d2022007-01-31 23:47:43 +01002848 RTL_W32(MAC4, high);
françois romieu908ba2b2010-04-26 11:42:58 +00002849 RTL_R32(MAC4);
2850
Francois Romieu78f1cd02010-03-27 19:35:46 -07002851 RTL_W32(MAC0, low);
françois romieu908ba2b2010-04-26 11:42:58 +00002852 RTL_R32(MAC0);
2853
Francois Romieu773d2022007-01-31 23:47:43 +01002854 RTL_W8(Cfg9346, Cfg9346_Lock);
2855
2856 spin_unlock_irq(&tp->lock);
2857}
2858
2859static int rtl_set_mac_address(struct net_device *dev, void *p)
2860{
2861 struct rtl8169_private *tp = netdev_priv(dev);
2862 struct sockaddr *addr = p;
2863
2864 if (!is_valid_ether_addr(addr->sa_data))
2865 return -EADDRNOTAVAIL;
2866
2867 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
2868
2869 rtl_rar_set(tp, dev->dev_addr);
2870
2871 return 0;
2872}
2873
Francois Romieu5f787a12006-08-17 13:02:36 +02002874static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2875{
2876 struct rtl8169_private *tp = netdev_priv(dev);
2877 struct mii_ioctl_data *data = if_mii(ifr);
2878
Francois Romieu8b4ab282008-11-19 22:05:25 -08002879 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
2880}
Francois Romieu5f787a12006-08-17 13:02:36 +02002881
Francois Romieucecb5fd2011-04-01 10:21:07 +02002882static int rtl_xmii_ioctl(struct rtl8169_private *tp,
2883 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08002884{
Francois Romieu5f787a12006-08-17 13:02:36 +02002885 switch (cmd) {
2886 case SIOCGMIIPHY:
2887 data->phy_id = 32; /* Internal PHY */
2888 return 0;
2889
2890 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00002891 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02002892 return 0;
2893
2894 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00002895 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02002896 return 0;
2897 }
2898 return -EOPNOTSUPP;
2899}
2900
Francois Romieu8b4ab282008-11-19 22:05:25 -08002901static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
2902{
2903 return -EOPNOTSUPP;
2904}
2905
Francois Romieu0e485152007-02-20 00:00:26 +01002906static const struct rtl_cfg_info {
2907 void (*hw_start)(struct net_device *);
2908 unsigned int region;
2909 unsigned int align;
2910 u16 intr_event;
2911 u16 napi_event;
Francois Romieuccdffb92008-07-26 14:26:06 +02002912 unsigned features;
Jean Delvaref21b75e2009-05-26 20:54:48 -07002913 u8 default_ver;
Francois Romieu0e485152007-02-20 00:00:26 +01002914} rtl_cfg_infos [] = {
2915 [RTL_CFG_0] = {
2916 .hw_start = rtl_hw_start_8169,
2917 .region = 1,
Francois Romieue9f63f32007-02-28 23:16:57 +01002918 .align = 0,
Francois Romieu0e485152007-02-20 00:00:26 +01002919 .intr_event = SYSErr | LinkChg | RxOverflow |
2920 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02002921 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07002922 .features = RTL_FEATURE_GMII,
2923 .default_ver = RTL_GIGA_MAC_VER_01,
Francois Romieu0e485152007-02-20 00:00:26 +01002924 },
2925 [RTL_CFG_1] = {
2926 .hw_start = rtl_hw_start_8168,
2927 .region = 2,
2928 .align = 8,
françois romieu53f57352010-11-08 13:23:05 +00002929 .intr_event = SYSErr | LinkChg | RxOverflow |
Francois Romieu0e485152007-02-20 00:00:26 +01002930 TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02002931 .napi_event = TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07002932 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
2933 .default_ver = RTL_GIGA_MAC_VER_11,
Francois Romieu0e485152007-02-20 00:00:26 +01002934 },
2935 [RTL_CFG_2] = {
2936 .hw_start = rtl_hw_start_8101,
2937 .region = 2,
2938 .align = 8,
2939 .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
2940 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02002941 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07002942 .features = RTL_FEATURE_MSI,
2943 .default_ver = RTL_GIGA_MAC_VER_13,
Francois Romieu0e485152007-02-20 00:00:26 +01002944 }
2945};
2946
Francois Romieufbac58f2007-10-04 22:51:38 +02002947/* Cfg9346_Unlock assumed. */
2948static unsigned rtl_try_msi(struct pci_dev *pdev, void __iomem *ioaddr,
2949 const struct rtl_cfg_info *cfg)
2950{
2951 unsigned msi = 0;
2952 u8 cfg2;
2953
2954 cfg2 = RTL_R8(Config2) & ~MSIEnable;
Francois Romieuccdffb92008-07-26 14:26:06 +02002955 if (cfg->features & RTL_FEATURE_MSI) {
Francois Romieufbac58f2007-10-04 22:51:38 +02002956 if (pci_enable_msi(pdev)) {
2957 dev_info(&pdev->dev, "no MSI. Back to INTx.\n");
2958 } else {
2959 cfg2 |= MSIEnable;
2960 msi = RTL_FEATURE_MSI;
2961 }
2962 }
2963 RTL_W8(Config2, cfg2);
2964 return msi;
2965}
2966
2967static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
2968{
2969 if (tp->features & RTL_FEATURE_MSI) {
2970 pci_disable_msi(pdev);
2971 tp->features &= ~RTL_FEATURE_MSI;
2972 }
2973}
2974
Francois Romieu8b4ab282008-11-19 22:05:25 -08002975static const struct net_device_ops rtl8169_netdev_ops = {
2976 .ndo_open = rtl8169_open,
2977 .ndo_stop = rtl8169_close,
2978 .ndo_get_stats = rtl8169_get_stats,
Stephen Hemminger00829822008-11-20 20:14:53 -08002979 .ndo_start_xmit = rtl8169_start_xmit,
Francois Romieu8b4ab282008-11-19 22:05:25 -08002980 .ndo_tx_timeout = rtl8169_tx_timeout,
2981 .ndo_validate_addr = eth_validate_addr,
2982 .ndo_change_mtu = rtl8169_change_mtu,
Michał Mirosław350fb322011-04-08 06:35:56 +00002983 .ndo_fix_features = rtl8169_fix_features,
2984 .ndo_set_features = rtl8169_set_features,
Francois Romieu8b4ab282008-11-19 22:05:25 -08002985 .ndo_set_mac_address = rtl_set_mac_address,
2986 .ndo_do_ioctl = rtl8169_ioctl,
2987 .ndo_set_multicast_list = rtl_set_rx_mode,
Francois Romieu8b4ab282008-11-19 22:05:25 -08002988#ifdef CONFIG_NET_POLL_CONTROLLER
2989 .ndo_poll_controller = rtl8169_netpoll,
2990#endif
2991
2992};
2993
françois romieuc0e45c12011-01-03 15:08:04 +00002994static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
2995{
2996 struct mdio_ops *ops = &tp->mdio_ops;
2997
2998 switch (tp->mac_version) {
2999 case RTL_GIGA_MAC_VER_27:
3000 ops->write = r8168dp_1_mdio_write;
3001 ops->read = r8168dp_1_mdio_read;
3002 break;
françois romieue6de30d2011-01-03 15:08:37 +00003003 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003004 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003005 ops->write = r8168dp_2_mdio_write;
3006 ops->read = r8168dp_2_mdio_read;
3007 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003008 default:
3009 ops->write = r8169_mdio_write;
3010 ops->read = r8169_mdio_read;
3011 break;
3012 }
3013}
3014
françois romieu065c27c2011-01-03 15:08:12 +00003015static void r810x_phy_power_down(struct rtl8169_private *tp)
3016{
3017 rtl_writephy(tp, 0x1f, 0x0000);
3018 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3019}
3020
3021static void r810x_phy_power_up(struct rtl8169_private *tp)
3022{
3023 rtl_writephy(tp, 0x1f, 0x0000);
3024 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3025}
3026
3027static void r810x_pll_power_down(struct rtl8169_private *tp)
3028{
3029 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3030 rtl_writephy(tp, 0x1f, 0x0000);
3031 rtl_writephy(tp, MII_BMCR, 0x0000);
3032 return;
3033 }
3034
3035 r810x_phy_power_down(tp);
3036}
3037
3038static void r810x_pll_power_up(struct rtl8169_private *tp)
3039{
3040 r810x_phy_power_up(tp);
3041}
3042
3043static void r8168_phy_power_up(struct rtl8169_private *tp)
3044{
3045 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003046 switch (tp->mac_version) {
3047 case RTL_GIGA_MAC_VER_11:
3048 case RTL_GIGA_MAC_VER_12:
3049 case RTL_GIGA_MAC_VER_17:
3050 case RTL_GIGA_MAC_VER_18:
3051 case RTL_GIGA_MAC_VER_19:
3052 case RTL_GIGA_MAC_VER_20:
3053 case RTL_GIGA_MAC_VER_21:
3054 case RTL_GIGA_MAC_VER_22:
3055 case RTL_GIGA_MAC_VER_23:
3056 case RTL_GIGA_MAC_VER_24:
3057 case RTL_GIGA_MAC_VER_25:
3058 case RTL_GIGA_MAC_VER_26:
3059 case RTL_GIGA_MAC_VER_27:
3060 case RTL_GIGA_MAC_VER_28:
3061 case RTL_GIGA_MAC_VER_31:
3062 rtl_writephy(tp, 0x0e, 0x0000);
3063 break;
3064 default:
3065 break;
3066 }
françois romieu065c27c2011-01-03 15:08:12 +00003067 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3068}
3069
3070static void r8168_phy_power_down(struct rtl8169_private *tp)
3071{
3072 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003073 switch (tp->mac_version) {
3074 case RTL_GIGA_MAC_VER_32:
3075 case RTL_GIGA_MAC_VER_33:
3076 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3077 break;
3078
3079 case RTL_GIGA_MAC_VER_11:
3080 case RTL_GIGA_MAC_VER_12:
3081 case RTL_GIGA_MAC_VER_17:
3082 case RTL_GIGA_MAC_VER_18:
3083 case RTL_GIGA_MAC_VER_19:
3084 case RTL_GIGA_MAC_VER_20:
3085 case RTL_GIGA_MAC_VER_21:
3086 case RTL_GIGA_MAC_VER_22:
3087 case RTL_GIGA_MAC_VER_23:
3088 case RTL_GIGA_MAC_VER_24:
3089 case RTL_GIGA_MAC_VER_25:
3090 case RTL_GIGA_MAC_VER_26:
3091 case RTL_GIGA_MAC_VER_27:
3092 case RTL_GIGA_MAC_VER_28:
3093 case RTL_GIGA_MAC_VER_31:
3094 rtl_writephy(tp, 0x0e, 0x0200);
3095 default:
3096 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3097 break;
3098 }
françois romieu065c27c2011-01-03 15:08:12 +00003099}
3100
3101static void r8168_pll_power_down(struct rtl8169_private *tp)
3102{
3103 void __iomem *ioaddr = tp->mmio_addr;
3104
Francois Romieucecb5fd2011-04-01 10:21:07 +02003105 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3106 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3107 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003108 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003109 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003110 }
françois romieu065c27c2011-01-03 15:08:12 +00003111
Francois Romieucecb5fd2011-04-01 10:21:07 +02003112 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3113 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00003114 (RTL_R16(CPlusCmd) & ASF)) {
3115 return;
3116 }
3117
hayeswang01dc7fe2011-03-21 01:50:28 +00003118 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3119 tp->mac_version == RTL_GIGA_MAC_VER_33)
3120 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3121
françois romieu065c27c2011-01-03 15:08:12 +00003122 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3123 rtl_writephy(tp, 0x1f, 0x0000);
3124 rtl_writephy(tp, MII_BMCR, 0x0000);
3125
3126 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3127 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3128 return;
3129 }
3130
3131 r8168_phy_power_down(tp);
3132
3133 switch (tp->mac_version) {
3134 case RTL_GIGA_MAC_VER_25:
3135 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003136 case RTL_GIGA_MAC_VER_27:
3137 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003138 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003139 case RTL_GIGA_MAC_VER_32:
3140 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003141 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3142 break;
3143 }
3144}
3145
3146static void r8168_pll_power_up(struct rtl8169_private *tp)
3147{
3148 void __iomem *ioaddr = tp->mmio_addr;
3149
Francois Romieucecb5fd2011-04-01 10:21:07 +02003150 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3151 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3152 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003153 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003154 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003155 }
françois romieu065c27c2011-01-03 15:08:12 +00003156
3157 switch (tp->mac_version) {
3158 case RTL_GIGA_MAC_VER_25:
3159 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003160 case RTL_GIGA_MAC_VER_27:
3161 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003162 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003163 case RTL_GIGA_MAC_VER_32:
3164 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003165 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3166 break;
3167 }
3168
3169 r8168_phy_power_up(tp);
3170}
3171
3172static void rtl_pll_power_op(struct rtl8169_private *tp,
3173 void (*op)(struct rtl8169_private *))
3174{
3175 if (op)
3176 op(tp);
3177}
3178
3179static void rtl_pll_power_down(struct rtl8169_private *tp)
3180{
3181 rtl_pll_power_op(tp, tp->pll_power_ops.down);
3182}
3183
3184static void rtl_pll_power_up(struct rtl8169_private *tp)
3185{
3186 rtl_pll_power_op(tp, tp->pll_power_ops.up);
3187}
3188
3189static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3190{
3191 struct pll_power_ops *ops = &tp->pll_power_ops;
3192
3193 switch (tp->mac_version) {
3194 case RTL_GIGA_MAC_VER_07:
3195 case RTL_GIGA_MAC_VER_08:
3196 case RTL_GIGA_MAC_VER_09:
3197 case RTL_GIGA_MAC_VER_10:
3198 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003199 case RTL_GIGA_MAC_VER_29:
3200 case RTL_GIGA_MAC_VER_30:
françois romieu065c27c2011-01-03 15:08:12 +00003201 ops->down = r810x_pll_power_down;
3202 ops->up = r810x_pll_power_up;
3203 break;
3204
3205 case RTL_GIGA_MAC_VER_11:
3206 case RTL_GIGA_MAC_VER_12:
3207 case RTL_GIGA_MAC_VER_17:
3208 case RTL_GIGA_MAC_VER_18:
3209 case RTL_GIGA_MAC_VER_19:
3210 case RTL_GIGA_MAC_VER_20:
3211 case RTL_GIGA_MAC_VER_21:
3212 case RTL_GIGA_MAC_VER_22:
3213 case RTL_GIGA_MAC_VER_23:
3214 case RTL_GIGA_MAC_VER_24:
3215 case RTL_GIGA_MAC_VER_25:
3216 case RTL_GIGA_MAC_VER_26:
3217 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003218 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003219 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003220 case RTL_GIGA_MAC_VER_32:
3221 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003222 ops->down = r8168_pll_power_down;
3223 ops->up = r8168_pll_power_up;
3224 break;
3225
3226 default:
3227 ops->down = NULL;
3228 ops->up = NULL;
3229 break;
3230 }
3231}
3232
Francois Romieu6f43adc2011-04-29 15:05:51 +02003233static void rtl_hw_reset(struct rtl8169_private *tp)
3234{
3235 void __iomem *ioaddr = tp->mmio_addr;
3236 int i;
3237
3238 /* Soft reset the chip. */
3239 RTL_W8(ChipCmd, CmdReset);
3240
3241 /* Check that the chip has finished the reset. */
3242 for (i = 0; i < 100; i++) {
3243 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3244 break;
3245 msleep_interruptible(1);
3246 }
3247}
3248
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003249static int __devinit
3250rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
3251{
Francois Romieu0e485152007-02-20 00:00:26 +01003252 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
3253 const unsigned int region = cfg->region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 struct rtl8169_private *tp;
Francois Romieuccdffb92008-07-26 14:26:06 +02003255 struct mii_if_info *mii;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003256 struct net_device *dev;
3257 void __iomem *ioaddr;
Francois Romieu2b7b4312011-04-18 22:53:24 -07003258 int chipset, i;
Francois Romieu07d3f512007-02-21 22:40:46 +01003259 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003261 if (netif_msg_drv(&debug)) {
3262 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
3263 MODULENAME, RTL8169_VERSION);
3264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 dev = alloc_etherdev(sizeof (*tp));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003267 if (!dev) {
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003268 if (netif_msg_drv(&debug))
Jeff Garzik9b91cf92006-06-27 11:39:50 -04003269 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003270 rc = -ENOMEM;
3271 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 }
3273
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieu8b4ab282008-11-19 22:05:25 -08003275 dev->netdev_ops = &rtl8169_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 tp = netdev_priv(dev);
David Howellsc4028952006-11-22 14:57:56 +00003277 tp->dev = dev;
Ivan Vecera21e197f2008-04-17 22:48:41 +02003278 tp->pci_dev = pdev;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003279 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280
Francois Romieuccdffb92008-07-26 14:26:06 +02003281 mii = &tp->mii;
3282 mii->dev = dev;
3283 mii->mdio_read = rtl_mdio_read;
3284 mii->mdio_write = rtl_mdio_write;
3285 mii->phy_id_mask = 0x1f;
3286 mii->reg_num_mask = 0x1f;
3287 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
3288
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +00003289 /* disable ASPM completely as that cause random device stop working
3290 * problems as well as full system hangs for some PCIe devices users */
3291 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
3292 PCIE_LINK_STATE_CLKPM);
3293
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 /* enable device (incl. PCI PM wakeup and hotplug setup) */
3295 rc = pci_enable_device(pdev);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003296 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003297 netif_err(tp, probe, dev, "enable failure\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003298 goto err_out_free_dev_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 }
3300
françois romieu87aeec72010-04-26 11:42:06 +00003301 if (pci_set_mwi(pdev) < 0)
3302 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 /* make sure PCI base addr 1 is MMIO */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003305 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003306 netif_err(tp, probe, dev,
3307 "region #%d not an MMIO resource, aborting\n",
3308 region);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003310 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003312
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 /* check for weird/broken PCI region reporting */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003314 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003315 netif_err(tp, probe, dev,
3316 "Invalid PCI region size(s), aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003318 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 }
3320
3321 rc = pci_request_regions(pdev, MODULENAME);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003322 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003323 netif_err(tp, probe, dev, "could not request regions\n");
françois romieu87aeec72010-04-26 11:42:06 +00003324 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 }
3326
Hayes Wangd24e9aa2011-02-22 17:26:19 +08003327 tp->cp_cmd = RxChkSum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328
3329 if ((sizeof(dma_addr_t) > 4) &&
David S. Miller4300e8c2010-03-26 10:23:30 -07003330 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 tp->cp_cmd |= PCIDAC;
3332 dev->features |= NETIF_F_HIGHDMA;
3333 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07003334 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003336 netif_err(tp, probe, dev, "DMA configuration failed\n");
françois romieu87aeec72010-04-26 11:42:06 +00003337 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 }
3339 }
3340
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 /* ioremap MMIO region */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003342 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003343 if (!ioaddr) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003344 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 rc = -EIO;
françois romieu87aeec72010-04-26 11:42:06 +00003346 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 }
Francois Romieu6f43adc2011-04-29 15:05:51 +02003348 tp->mmio_addr = ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349
David S. Miller4300e8c2010-03-26 10:23:30 -07003350 tp->pcie_cap = pci_find_capability(pdev, PCI_CAP_ID_EXP);
3351 if (!tp->pcie_cap)
3352 netif_info(tp, probe, dev, "no PCI Express capability\n");
3353
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003354 RTL_W16(IntrMask, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355
Francois Romieu6f43adc2011-04-29 15:05:51 +02003356 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003358 RTL_W16(IntrStatus, 0xffff);
3359
françois romieuca52efd2009-07-24 12:34:19 +00003360 pci_set_master(pdev);
3361
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 /* Identify chip attached to board */
Francois Romieu5d320a22011-05-08 17:47:36 +02003363 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364
Francois Romieu7a8fc772011-03-01 17:18:33 +01003365 /*
3366 * Pretend we are using VLANs; This bypasses a nasty bug where
3367 * Interrupts stop flowing on high load on 8110SCd controllers.
3368 */
3369 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3370 tp->cp_cmd |= RxVlan;
3371
françois romieuc0e45c12011-01-03 15:08:04 +00003372 rtl_init_mdio_ops(tp);
françois romieu065c27c2011-01-03 15:08:12 +00003373 rtl_init_pll_power_ops(tp);
françois romieuc0e45c12011-01-03 15:08:04 +00003374
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 rtl8169_print_mac_version(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376
Francois Romieu85bffe62011-04-27 08:22:39 +02003377 chipset = tp->mac_version;
3378 tp->txd_version = rtl_chip_infos[chipset].txd_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379
Francois Romieu5d06a992006-02-23 00:47:58 +01003380 RTL_W8(Cfg9346, Cfg9346_Unlock);
3381 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
3382 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
Bruno Prémont20037fa2008-10-08 17:05:03 -07003383 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
3384 tp->features |= RTL_FEATURE_WOL;
3385 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
3386 tp->features |= RTL_FEATURE_WOL;
Francois Romieufbac58f2007-10-04 22:51:38 +02003387 tp->features |= rtl_try_msi(pdev, ioaddr, cfg);
Francois Romieu5d06a992006-02-23 00:47:58 +01003388 RTL_W8(Cfg9346, Cfg9346_Lock);
3389
Francois Romieu66ec5d42007-11-06 22:56:10 +01003390 if ((tp->mac_version <= RTL_GIGA_MAC_VER_06) &&
3391 (RTL_R8(PHYstatus) & TBI_Enable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 tp->set_speed = rtl8169_set_speed_tbi;
3393 tp->get_settings = rtl8169_gset_tbi;
3394 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
3395 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
3396 tp->link_ok = rtl8169_tbi_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003397 tp->do_ioctl = rtl_tbi_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 } else {
3399 tp->set_speed = rtl8169_set_speed_xmii;
3400 tp->get_settings = rtl8169_gset_xmii;
3401 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
3402 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
3403 tp->link_ok = rtl8169_xmii_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003404 tp->do_ioctl = rtl_xmii_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 }
3406
Francois Romieudf58ef52008-10-09 14:35:58 -07003407 spin_lock_init(&tp->lock);
3408
Ivan Vecera7bf6bf42008-09-23 22:46:29 +00003409 /* Get MAC address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 for (i = 0; i < MAC_ADDR_LEN; i++)
3411 dev->dev_addr[i] = RTL_R8(MAC0 + i);
John W. Linville6d6525b2005-09-12 10:48:57 -04003412 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
3416 dev->irq = pdev->irq;
3417 dev->base_addr = (unsigned long) ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003419 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420
Michał Mirosław350fb322011-04-08 06:35:56 +00003421 /* don't enable SG, IP_CSUM and TSO by default - it might not work
3422 * properly for all devices */
3423 dev->features |= NETIF_F_RXCSUM |
3424 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3425
3426 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3427 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3428 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3429 NETIF_F_HIGHDMA;
3430
3431 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3432 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
3433 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434
3435 tp->intr_mask = 0xffff;
Francois Romieu0e485152007-02-20 00:00:26 +01003436 tp->hw_start = cfg->hw_start;
3437 tp->intr_event = cfg->intr_event;
3438 tp->napi_event = cfg->napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439
Francois Romieu2efa53f2007-03-09 00:00:05 +01003440 init_timer(&tp->timer);
3441 tp->timer.data = (unsigned long) dev;
3442 tp->timer.function = rtl8169_phy_timer;
3443
François Romieu953a12c2011-04-24 17:38:48 +02003444 tp->fw = RTL_FIRMWARE_UNKNOWN;
3445
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 rc = register_netdev(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003447 if (rc < 0)
françois romieu87aeec72010-04-26 11:42:06 +00003448 goto err_out_msi_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449
3450 pci_set_drvdata(pdev, dev);
3451
Joe Perchesbf82c182010-02-09 11:49:50 +00003452 netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
Francois Romieu85bffe62011-04-27 08:22:39 +02003453 rtl_chip_infos[chipset].name, dev->base_addr, dev->dev_addr,
Joe Perchesbf82c182010-02-09 11:49:50 +00003454 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455
Francois Romieucecb5fd2011-04-01 10:21:07 +02003456 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3457 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3458 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00003459 rtl8168_driver_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003460 }
françois romieub646d902011-01-03 15:08:21 +00003461
Bruno Prémont8b76ab32008-10-08 17:06:25 -07003462 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463
Alan Sternf3ec4f82010-06-08 15:23:51 -04003464 if (pci_dev_run_wake(pdev))
3465 pm_runtime_put_noidle(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003466
Ivan Vecera0d672e92011-02-15 02:08:39 +00003467 netif_carrier_off(dev);
3468
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003469out:
3470 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471
françois romieu87aeec72010-04-26 11:42:06 +00003472err_out_msi_4:
Francois Romieufbac58f2007-10-04 22:51:38 +02003473 rtl_disable_msi(pdev, tp);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003474 iounmap(ioaddr);
françois romieu87aeec72010-04-26 11:42:06 +00003475err_out_free_res_3:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003476 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003477err_out_mwi_2:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003478 pci_clear_mwi(pdev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003479 pci_disable_device(pdev);
3480err_out_free_dev_1:
3481 free_netdev(dev);
3482 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483}
3484
Francois Romieu07d3f512007-02-21 22:40:46 +01003485static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486{
3487 struct net_device *dev = pci_get_drvdata(pdev);
3488 struct rtl8169_private *tp = netdev_priv(dev);
3489
Francois Romieucecb5fd2011-04-01 10:21:07 +02003490 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3491 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3492 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00003493 rtl8168_driver_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003494 }
françois romieub646d902011-01-03 15:08:21 +00003495
Tejun Heo23f333a2010-12-12 16:45:14 +01003496 cancel_delayed_work_sync(&tp->task);
Francois Romieueb2a0212007-02-15 23:37:21 +01003497
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 unregister_netdev(dev);
Ivan Veceracc098dc2009-11-29 23:12:52 -08003499
François Romieu953a12c2011-04-24 17:38:48 +02003500 rtl_release_firmware(tp);
3501
Alan Sternf3ec4f82010-06-08 15:23:51 -04003502 if (pci_dev_run_wake(pdev))
3503 pm_runtime_get_noresume(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003504
Ivan Veceracc098dc2009-11-29 23:12:52 -08003505 /* restore original MAC address */
3506 rtl_rar_set(tp, dev->perm_addr);
3507
Francois Romieufbac58f2007-10-04 22:51:38 +02003508 rtl_disable_msi(pdev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 rtl8169_release_board(pdev, dev, tp->mmio_addr);
3510 pci_set_drvdata(pdev, NULL);
3511}
3512
François Romieu953a12c2011-04-24 17:38:48 +02003513static void rtl_request_firmware(struct rtl8169_private *tp)
3514{
François Romieu953a12c2011-04-24 17:38:48 +02003515 /* Return early if the firmware is already loaded / cached. */
Francois Romieu31bd2042011-04-26 18:58:59 +02003516 if (IS_ERR(tp->fw)) {
3517 const char *name;
François Romieu953a12c2011-04-24 17:38:48 +02003518
Francois Romieu31bd2042011-04-26 18:58:59 +02003519 name = rtl_lookup_firmware_name(tp);
3520 if (name) {
François Romieu953a12c2011-04-24 17:38:48 +02003521 int rc;
3522
3523 rc = request_firmware(&tp->fw, name, &tp->pci_dev->dev);
Francois Romieu31bd2042011-04-26 18:58:59 +02003524 if (rc >= 0)
3525 return;
François Romieu953a12c2011-04-24 17:38:48 +02003526
Francois Romieu31bd2042011-04-26 18:58:59 +02003527 netif_warn(tp, ifup, tp->dev, "unable to load "
3528 "firmware patch %s (%d)\n", name, rc);
3529 }
3530 tp->fw = NULL;
3531 }
François Romieu953a12c2011-04-24 17:38:48 +02003532}
3533
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534static int rtl8169_open(struct net_device *dev)
3535{
3536 struct rtl8169_private *tp = netdev_priv(dev);
françois romieueee3a962011-01-08 02:17:26 +00003537 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu99f252b2007-04-02 22:59:59 +02003539 int retval = -ENOMEM;
3540
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003541 pm_runtime_get_sync(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542
Neil Hormanc0cd8842010-03-29 13:16:02 -07003543 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 * Rx and Tx desscriptors needs 256 bytes alignment.
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003545 * dma_alloc_coherent provides more.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 */
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003547 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
3548 &tp->TxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 if (!tp->TxDescArray)
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003550 goto err_pm_runtime_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003552 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
3553 &tp->RxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 if (!tp->RxDescArray)
Francois Romieu99f252b2007-04-02 22:59:59 +02003555 goto err_free_tx_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556
3557 retval = rtl8169_init_ring(dev);
3558 if (retval < 0)
Francois Romieu99f252b2007-04-02 22:59:59 +02003559 goto err_free_rx_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560
David Howellsc4028952006-11-22 14:57:56 +00003561 INIT_DELAYED_WORK(&tp->task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562
Francois Romieu99f252b2007-04-02 22:59:59 +02003563 smp_mb();
3564
François Romieu953a12c2011-04-24 17:38:48 +02003565 rtl_request_firmware(tp);
3566
Francois Romieufbac58f2007-10-04 22:51:38 +02003567 retval = request_irq(dev->irq, rtl8169_interrupt,
3568 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
Francois Romieu99f252b2007-04-02 22:59:59 +02003569 dev->name, dev);
3570 if (retval < 0)
François Romieu953a12c2011-04-24 17:38:48 +02003571 goto err_release_fw_2;
Francois Romieu99f252b2007-04-02 22:59:59 +02003572
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003573 napi_enable(&tp->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003574
françois romieueee3a962011-01-08 02:17:26 +00003575 rtl8169_init_phy(dev, tp);
3576
Michał Mirosław350fb322011-04-08 06:35:56 +00003577 rtl8169_set_features(dev, dev->features);
françois romieueee3a962011-01-08 02:17:26 +00003578
françois romieu065c27c2011-01-03 15:08:12 +00003579 rtl_pll_power_up(tp);
3580
Francois Romieu07ce4062007-02-23 23:36:39 +01003581 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003583 tp->saved_wolopts = 0;
3584 pm_runtime_put_noidle(&pdev->dev);
3585
françois romieueee3a962011-01-08 02:17:26 +00003586 rtl8169_check_link_status(dev, tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587out:
3588 return retval;
3589
François Romieu953a12c2011-04-24 17:38:48 +02003590err_release_fw_2:
3591 rtl_release_firmware(tp);
Francois Romieu99f252b2007-04-02 22:59:59 +02003592 rtl8169_rx_clear(tp);
3593err_free_rx_1:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003594 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
3595 tp->RxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003596 tp->RxDescArray = NULL;
Francois Romieu99f252b2007-04-02 22:59:59 +02003597err_free_tx_0:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003598 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
3599 tp->TxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003600 tp->TxDescArray = NULL;
3601err_pm_runtime_put:
3602 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 goto out;
3604}
3605
françois romieue6de30d2011-01-03 15:08:37 +00003606static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607{
françois romieue6de30d2011-01-03 15:08:37 +00003608 void __iomem *ioaddr = tp->mmio_addr;
3609
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 /* Disable interrupts */
3611 rtl8169_irq_mask_and_ack(ioaddr);
3612
Hayes Wang5d2e1952011-02-22 17:26:22 +08003613 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00003614 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3615 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieue6de30d2011-01-03 15:08:37 +00003616 while (RTL_R8(TxPoll) & NPQ)
3617 udelay(20);
3618
3619 }
3620
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 /* Reset the chipset */
3622 RTL_W8(ChipCmd, CmdReset);
3623
3624 /* PCI commit */
3625 RTL_R8(ChipCmd);
3626}
3627
Francois Romieu7f796d82007-06-11 23:04:41 +02003628static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01003629{
3630 void __iomem *ioaddr = tp->mmio_addr;
3631 u32 cfg = rtl8169_rx_config;
3632
Francois Romieu2b7b4312011-04-18 22:53:24 -07003633 cfg |= (RTL_R32(RxConfig) & RTL_RX_CONFIG_MASK);
Francois Romieu9cb427b2006-11-02 00:10:16 +01003634 RTL_W32(RxConfig, cfg);
3635
3636 /* Set DMA burst size and Interframe Gap Time */
3637 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
3638 (InterFrameGap << TxInterFrameGapShift));
3639}
3640
Francois Romieu07ce4062007-02-23 23:36:39 +01003641static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642{
3643 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644
Francois Romieu6f43adc2011-04-29 15:05:51 +02003645 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646
Francois Romieu07ce4062007-02-23 23:36:39 +01003647 tp->hw_start(dev);
3648
Francois Romieu07ce4062007-02-23 23:36:39 +01003649 netif_start_queue(dev);
3650}
3651
Francois Romieu7f796d82007-06-11 23:04:41 +02003652static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
3653 void __iomem *ioaddr)
3654{
3655 /*
3656 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
3657 * register to be written before TxDescAddrLow to work.
3658 * Switching from MMIO to I/O access fixes the issue as well.
3659 */
3660 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07003661 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02003662 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07003663 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02003664}
3665
3666static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
3667{
3668 u16 cmd;
3669
3670 cmd = RTL_R16(CPlusCmd);
3671 RTL_W16(CPlusCmd, cmd);
3672 return cmd;
3673}
3674
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07003675static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d82007-06-11 23:04:41 +02003676{
3677 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e82009-10-26 10:52:37 +00003678 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d82007-06-11 23:04:41 +02003679}
3680
Francois Romieu6dccd162007-02-13 23:38:05 +01003681static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
3682{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003683 static const struct {
Francois Romieu6dccd162007-02-13 23:38:05 +01003684 u32 mac_version;
3685 u32 clk;
3686 u32 val;
3687 } cfg2_info [] = {
3688 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
3689 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
3690 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
3691 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
3692 }, *p = cfg2_info;
3693 unsigned int i;
3694 u32 clk;
3695
3696 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01003697 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01003698 if ((p->mac_version == mac_version) && (p->clk == clk)) {
3699 RTL_W32(0x7c, p->val);
3700 break;
3701 }
3702 }
3703}
3704
Francois Romieu07ce4062007-02-23 23:36:39 +01003705static void rtl_hw_start_8169(struct net_device *dev)
3706{
3707 struct rtl8169_private *tp = netdev_priv(dev);
3708 void __iomem *ioaddr = tp->mmio_addr;
3709 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01003710
Francois Romieu9cb427b2006-11-02 00:10:16 +01003711 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
3712 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
3713 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
3714 }
3715
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02003717 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
3718 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
3719 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
3720 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01003721 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3722
françois romieuf0298f82011-01-03 15:07:42 +00003723 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724
Eric Dumazet6f0333b2010-10-11 11:17:47 +00003725 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726
Francois Romieucecb5fd2011-04-01 10:21:07 +02003727 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
3728 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
3729 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
3730 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02003731 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732
Francois Romieu7f796d82007-06-11 23:04:41 +02003733 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02003734
Francois Romieucecb5fd2011-04-01 10:21:07 +02003735 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
3736 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02003737 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02003739 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 }
3741
Francois Romieubcf0bf92006-07-26 23:14:13 +02003742 RTL_W16(CPlusCmd, tp->cp_cmd);
3743
Francois Romieu6dccd162007-02-13 23:38:05 +01003744 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
3745
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 /*
3747 * Undocumented corner. Supposedly:
3748 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
3749 */
3750 RTL_W16(IntrMitigate, 0x0000);
3751
Francois Romieu7f796d82007-06-11 23:04:41 +02003752 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01003753
Francois Romieucecb5fd2011-04-01 10:21:07 +02003754 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
3755 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
3756 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
3757 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02003758 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3759 rtl_set_rx_tx_config_registers(tp);
3760 }
3761
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02003763
3764 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
3765 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766
3767 RTL_W32(RxMissed, 0);
3768
Francois Romieu07ce4062007-02-23 23:36:39 +01003769 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770
3771 /* no early-rx interrupts */
3772 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01003773
3774 /* Enable all known interrupts by setting the interrupt mask. */
Francois Romieu0e485152007-02-20 00:00:26 +01003775 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01003776}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777
Francois Romieu9c14cea2008-07-05 00:21:15 +02003778static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
Francois Romieu458a9f62008-08-02 15:50:02 +02003779{
Francois Romieu9c14cea2008-07-05 00:21:15 +02003780 struct net_device *dev = pci_get_drvdata(pdev);
3781 struct rtl8169_private *tp = netdev_priv(dev);
3782 int cap = tp->pcie_cap;
Francois Romieu458a9f62008-08-02 15:50:02 +02003783
Francois Romieu9c14cea2008-07-05 00:21:15 +02003784 if (cap) {
3785 u16 ctl;
3786
3787 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
3788 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
3789 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
3790 }
Francois Romieu458a9f62008-08-02 15:50:02 +02003791}
3792
françois romieu650e8d52011-01-03 15:08:29 +00003793static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02003794{
3795 u32 csi;
3796
3797 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
françois romieu650e8d52011-01-03 15:08:29 +00003798 rtl_csi_write(ioaddr, 0x070c, csi | bits);
3799}
3800
françois romieue6de30d2011-01-03 15:08:37 +00003801static void rtl_csi_access_enable_1(void __iomem *ioaddr)
3802{
3803 rtl_csi_access_enable(ioaddr, 0x17000000);
3804}
3805
françois romieu650e8d52011-01-03 15:08:29 +00003806static void rtl_csi_access_enable_2(void __iomem *ioaddr)
3807{
3808 rtl_csi_access_enable(ioaddr, 0x27000000);
Francois Romieudacf8152008-08-02 20:44:13 +02003809}
3810
3811struct ephy_info {
3812 unsigned int offset;
3813 u16 mask;
3814 u16 bits;
3815};
3816
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003817static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02003818{
3819 u16 w;
3820
3821 while (len-- > 0) {
3822 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
3823 rtl_ephy_write(ioaddr, e->offset, w);
3824 e++;
3825 }
3826}
3827
Francois Romieub726e492008-06-28 12:22:59 +02003828static void rtl_disable_clock_request(struct pci_dev *pdev)
3829{
3830 struct net_device *dev = pci_get_drvdata(pdev);
3831 struct rtl8169_private *tp = netdev_priv(dev);
3832 int cap = tp->pcie_cap;
3833
3834 if (cap) {
3835 u16 ctl;
3836
3837 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
3838 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
3839 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
3840 }
3841}
3842
françois romieue6de30d2011-01-03 15:08:37 +00003843static void rtl_enable_clock_request(struct pci_dev *pdev)
3844{
3845 struct net_device *dev = pci_get_drvdata(pdev);
3846 struct rtl8169_private *tp = netdev_priv(dev);
3847 int cap = tp->pcie_cap;
3848
3849 if (cap) {
3850 u16 ctl;
3851
3852 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
3853 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
3854 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
3855 }
3856}
3857
Francois Romieub726e492008-06-28 12:22:59 +02003858#define R8168_CPCMD_QUIRK_MASK (\
3859 EnableBist | \
3860 Mac_dbgo_oe | \
3861 Force_half_dup | \
3862 Force_rxflow_en | \
3863 Force_txflow_en | \
3864 Cxpl_dbg_sel | \
3865 ASF | \
3866 PktCntrDisable | \
3867 Mac_dbgo_sel)
3868
Francois Romieu219a1e92008-06-28 11:58:39 +02003869static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
3870{
Francois Romieub726e492008-06-28 12:22:59 +02003871 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3872
3873 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3874
Francois Romieu2e68ae42008-06-28 12:00:55 +02003875 rtl_tx_performance_tweak(pdev,
3876 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02003877}
3878
3879static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
3880{
3881 rtl_hw_start_8168bb(ioaddr, pdev);
Francois Romieub726e492008-06-28 12:22:59 +02003882
françois romieuf0298f82011-01-03 15:07:42 +00003883 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02003884
3885 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02003886}
3887
3888static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
3889{
Francois Romieub726e492008-06-28 12:22:59 +02003890 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
3891
3892 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3893
Francois Romieu219a1e92008-06-28 11:58:39 +02003894 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02003895
3896 rtl_disable_clock_request(pdev);
3897
3898 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02003899}
3900
Francois Romieuef3386f2008-06-29 12:24:30 +02003901static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
Francois Romieu219a1e92008-06-28 11:58:39 +02003902{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003903 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02003904 { 0x01, 0, 0x0001 },
3905 { 0x02, 0x0800, 0x1000 },
3906 { 0x03, 0, 0x0042 },
3907 { 0x06, 0x0080, 0x0000 },
3908 { 0x07, 0, 0x2000 }
3909 };
3910
françois romieu650e8d52011-01-03 15:08:29 +00003911 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02003912
3913 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
3914
Francois Romieu219a1e92008-06-28 11:58:39 +02003915 __rtl_hw_start_8168cp(ioaddr, pdev);
3916}
3917
Francois Romieuef3386f2008-06-29 12:24:30 +02003918static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
3919{
françois romieu650e8d52011-01-03 15:08:29 +00003920 rtl_csi_access_enable_2(ioaddr);
Francois Romieuef3386f2008-06-29 12:24:30 +02003921
3922 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3923
3924 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3925
3926 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3927}
3928
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003929static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
3930{
françois romieu650e8d52011-01-03 15:08:29 +00003931 rtl_csi_access_enable_2(ioaddr);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003932
3933 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3934
3935 /* Magic. */
3936 RTL_W8(DBG_REG, 0x20);
3937
françois romieuf0298f82011-01-03 15:07:42 +00003938 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003939
3940 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3941
3942 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3943}
3944
Francois Romieu219a1e92008-06-28 11:58:39 +02003945static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
3946{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003947 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02003948 { 0x02, 0x0800, 0x1000 },
3949 { 0x03, 0, 0x0002 },
3950 { 0x06, 0x0080, 0x0000 }
3951 };
3952
françois romieu650e8d52011-01-03 15:08:29 +00003953 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02003954
3955 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
3956
3957 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
3958
Francois Romieu219a1e92008-06-28 11:58:39 +02003959 __rtl_hw_start_8168cp(ioaddr, pdev);
3960}
3961
3962static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
3963{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003964 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02003965 { 0x01, 0, 0x0001 },
3966 { 0x03, 0x0400, 0x0220 }
3967 };
3968
françois romieu650e8d52011-01-03 15:08:29 +00003969 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02003970
3971 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
3972
Francois Romieu219a1e92008-06-28 11:58:39 +02003973 __rtl_hw_start_8168cp(ioaddr, pdev);
3974}
3975
Francois Romieu197ff762008-06-28 13:16:02 +02003976static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
3977{
3978 rtl_hw_start_8168c_2(ioaddr, pdev);
3979}
3980
Francois Romieu6fb07052008-06-29 11:54:28 +02003981static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
3982{
françois romieu650e8d52011-01-03 15:08:29 +00003983 rtl_csi_access_enable_2(ioaddr);
Francois Romieu6fb07052008-06-29 11:54:28 +02003984
3985 __rtl_hw_start_8168cp(ioaddr, pdev);
3986}
3987
Francois Romieu5b538df2008-07-20 16:22:45 +02003988static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
3989{
françois romieu650e8d52011-01-03 15:08:29 +00003990 rtl_csi_access_enable_2(ioaddr);
Francois Romieu5b538df2008-07-20 16:22:45 +02003991
3992 rtl_disable_clock_request(pdev);
3993
françois romieuf0298f82011-01-03 15:07:42 +00003994 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02003995
3996 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3997
3998 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3999}
4000
hayeswang4804b3b2011-03-21 01:50:29 +00004001static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
4002{
4003 rtl_csi_access_enable_1(ioaddr);
4004
4005 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4006
4007 RTL_W8(MaxTxPacketSize, TxPacketMax);
4008
4009 rtl_disable_clock_request(pdev);
4010}
4011
françois romieue6de30d2011-01-03 15:08:37 +00004012static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4013{
4014 static const struct ephy_info e_info_8168d_4[] = {
4015 { 0x0b, ~0, 0x48 },
4016 { 0x19, 0x20, 0x50 },
4017 { 0x0c, ~0, 0x20 }
4018 };
4019 int i;
4020
4021 rtl_csi_access_enable_1(ioaddr);
4022
4023 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4024
4025 RTL_W8(MaxTxPacketSize, TxPacketMax);
4026
4027 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4028 const struct ephy_info *e = e_info_8168d_4 + i;
4029 u16 w;
4030
4031 w = rtl_ephy_read(ioaddr, e->offset);
4032 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4033 }
4034
4035 rtl_enable_clock_request(pdev);
4036}
4037
hayeswang01dc7fe2011-03-21 01:50:28 +00004038static void rtl_hw_start_8168e(void __iomem *ioaddr, struct pci_dev *pdev)
4039{
4040 static const struct ephy_info e_info_8168e[] = {
4041 { 0x00, 0x0200, 0x0100 },
4042 { 0x00, 0x0000, 0x0004 },
4043 { 0x06, 0x0002, 0x0001 },
4044 { 0x06, 0x0000, 0x0030 },
4045 { 0x07, 0x0000, 0x2000 },
4046 { 0x00, 0x0000, 0x0020 },
4047 { 0x03, 0x5800, 0x2000 },
4048 { 0x03, 0x0000, 0x0001 },
4049 { 0x01, 0x0800, 0x1000 },
4050 { 0x07, 0x0000, 0x4000 },
4051 { 0x1e, 0x0000, 0x2000 },
4052 { 0x19, 0xffff, 0xfe6c },
4053 { 0x0a, 0x0000, 0x0040 }
4054 };
4055
4056 rtl_csi_access_enable_2(ioaddr);
4057
4058 rtl_ephy_init(ioaddr, e_info_8168e, ARRAY_SIZE(e_info_8168e));
4059
4060 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4061
4062 RTL_W8(MaxTxPacketSize, TxPacketMax);
4063
4064 rtl_disable_clock_request(pdev);
4065
4066 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02004067 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4068 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004069
Francois Romieucecb5fd2011-04-01 10:21:07 +02004070 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004071}
4072
Francois Romieu07ce4062007-02-23 23:36:39 +01004073static void rtl_hw_start_8168(struct net_device *dev)
4074{
Francois Romieu2dd99532007-06-11 23:22:52 +02004075 struct rtl8169_private *tp = netdev_priv(dev);
4076 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01004077 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu2dd99532007-06-11 23:22:52 +02004078
4079 RTL_W8(Cfg9346, Cfg9346_Unlock);
4080
françois romieuf0298f82011-01-03 15:07:42 +00004081 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004082
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004083 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004084
Francois Romieu0e485152007-02-20 00:00:26 +01004085 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004086
4087 RTL_W16(CPlusCmd, tp->cp_cmd);
4088
Francois Romieu0e485152007-02-20 00:00:26 +01004089 RTL_W16(IntrMitigate, 0x5151);
4090
4091 /* Work around for RxFIFO overflow. */
Ivan Vecerab5ba6d12011-01-27 12:24:11 +01004092 if (tp->mac_version == RTL_GIGA_MAC_VER_11 ||
4093 tp->mac_version == RTL_GIGA_MAC_VER_22) {
Francois Romieu0e485152007-02-20 00:00:26 +01004094 tp->intr_event |= RxFIFOOver | PCSTimeout;
4095 tp->intr_event &= ~RxOverflow;
4096 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004097
4098 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4099
Francois Romieub8363902008-06-01 12:31:57 +02004100 rtl_set_rx_mode(dev);
4101
4102 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4103 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004104
4105 RTL_R8(IntrMask);
4106
Francois Romieu219a1e92008-06-28 11:58:39 +02004107 switch (tp->mac_version) {
4108 case RTL_GIGA_MAC_VER_11:
4109 rtl_hw_start_8168bb(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004110 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004111
4112 case RTL_GIGA_MAC_VER_12:
4113 case RTL_GIGA_MAC_VER_17:
4114 rtl_hw_start_8168bef(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004115 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004116
4117 case RTL_GIGA_MAC_VER_18:
Francois Romieuef3386f2008-06-29 12:24:30 +02004118 rtl_hw_start_8168cp_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004119 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004120
4121 case RTL_GIGA_MAC_VER_19:
4122 rtl_hw_start_8168c_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004123 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004124
4125 case RTL_GIGA_MAC_VER_20:
4126 rtl_hw_start_8168c_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004127 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004128
Francois Romieu197ff762008-06-28 13:16:02 +02004129 case RTL_GIGA_MAC_VER_21:
4130 rtl_hw_start_8168c_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004131 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004132
Francois Romieu6fb07052008-06-29 11:54:28 +02004133 case RTL_GIGA_MAC_VER_22:
4134 rtl_hw_start_8168c_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004135 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004136
Francois Romieuef3386f2008-06-29 12:24:30 +02004137 case RTL_GIGA_MAC_VER_23:
4138 rtl_hw_start_8168cp_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004139 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004140
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004141 case RTL_GIGA_MAC_VER_24:
4142 rtl_hw_start_8168cp_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004143 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004144
Francois Romieu5b538df2008-07-20 16:22:45 +02004145 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00004146 case RTL_GIGA_MAC_VER_26:
4147 case RTL_GIGA_MAC_VER_27:
Francois Romieu5b538df2008-07-20 16:22:45 +02004148 rtl_hw_start_8168d(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004149 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004150
françois romieue6de30d2011-01-03 15:08:37 +00004151 case RTL_GIGA_MAC_VER_28:
4152 rtl_hw_start_8168d_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004153 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004154
hayeswang4804b3b2011-03-21 01:50:29 +00004155 case RTL_GIGA_MAC_VER_31:
4156 rtl_hw_start_8168dp(ioaddr, pdev);
4157 break;
4158
hayeswang01dc7fe2011-03-21 01:50:28 +00004159 case RTL_GIGA_MAC_VER_32:
4160 case RTL_GIGA_MAC_VER_33:
4161 rtl_hw_start_8168e(ioaddr, pdev);
4162 break;
françois romieue6de30d2011-01-03 15:08:37 +00004163
Francois Romieu219a1e92008-06-28 11:58:39 +02004164 default:
4165 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
4166 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00004167 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004168 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004169
Francois Romieu0e485152007-02-20 00:00:26 +01004170 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4171
Francois Romieub8363902008-06-01 12:31:57 +02004172 RTL_W8(Cfg9346, Cfg9346_Lock);
4173
Francois Romieu2dd99532007-06-11 23:22:52 +02004174 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004175
Francois Romieu0e485152007-02-20 00:00:26 +01004176 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01004177}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178
Francois Romieu2857ffb2008-08-02 21:08:49 +02004179#define R810X_CPCMD_QUIRK_MASK (\
4180 EnableBist | \
4181 Mac_dbgo_oe | \
4182 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00004183 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02004184 Force_txflow_en | \
4185 Cxpl_dbg_sel | \
4186 ASF | \
4187 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004188 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004189
4190static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4191{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004192 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004193 { 0x01, 0, 0x6e65 },
4194 { 0x02, 0, 0x091f },
4195 { 0x03, 0, 0xc2f9 },
4196 { 0x06, 0, 0xafb5 },
4197 { 0x07, 0, 0x0e00 },
4198 { 0x19, 0, 0xec80 },
4199 { 0x01, 0, 0x2e65 },
4200 { 0x01, 0, 0x6e65 }
4201 };
4202 u8 cfg1;
4203
françois romieu650e8d52011-01-03 15:08:29 +00004204 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004205
4206 RTL_W8(DBG_REG, FIX_NAK_1);
4207
4208 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4209
4210 RTL_W8(Config1,
4211 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
4212 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4213
4214 cfg1 = RTL_R8(Config1);
4215 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
4216 RTL_W8(Config1, cfg1 & ~LEDS0);
4217
Francois Romieu2857ffb2008-08-02 21:08:49 +02004218 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
4219}
4220
4221static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4222{
françois romieu650e8d52011-01-03 15:08:29 +00004223 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004224
4225 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4226
4227 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
4228 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004229}
4230
4231static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
4232{
4233 rtl_hw_start_8102e_2(ioaddr, pdev);
4234
4235 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
4236}
4237
Hayes Wang5a5e4442011-02-22 17:26:21 +08004238static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4239{
4240 static const struct ephy_info e_info_8105e_1[] = {
4241 { 0x07, 0, 0x4000 },
4242 { 0x19, 0, 0x0200 },
4243 { 0x19, 0, 0x0020 },
4244 { 0x1e, 0, 0x2000 },
4245 { 0x03, 0, 0x0001 },
4246 { 0x19, 0, 0x0100 },
4247 { 0x19, 0, 0x0004 },
4248 { 0x0a, 0, 0x0020 }
4249 };
4250
Francois Romieucecb5fd2011-04-01 10:21:07 +02004251 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004252 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
4253
Francois Romieucecb5fd2011-04-01 10:21:07 +02004254 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004255 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
4256
4257 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
4258 RTL_W8(DLLPR, RTL_R8(DLLPR) | PM_SWITCH);
4259
4260 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
4261}
4262
4263static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4264{
4265 rtl_hw_start_8105e_1(ioaddr, pdev);
4266 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
4267}
4268
Francois Romieu07ce4062007-02-23 23:36:39 +01004269static void rtl_hw_start_8101(struct net_device *dev)
4270{
Francois Romieucdf1a602007-06-11 23:29:50 +02004271 struct rtl8169_private *tp = netdev_priv(dev);
4272 void __iomem *ioaddr = tp->mmio_addr;
4273 struct pci_dev *pdev = tp->pci_dev;
4274
Francois Romieucecb5fd2011-04-01 10:21:07 +02004275 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
4276 tp->mac_version == RTL_GIGA_MAC_VER_16) {
Francois Romieu9c14cea2008-07-05 00:21:15 +02004277 int cap = tp->pcie_cap;
4278
4279 if (cap) {
4280 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
4281 PCI_EXP_DEVCTL_NOSNOOP_EN);
4282 }
Francois Romieucdf1a602007-06-11 23:29:50 +02004283 }
4284
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004285 RTL_W8(Cfg9346, Cfg9346_Unlock);
4286
Francois Romieu2857ffb2008-08-02 21:08:49 +02004287 switch (tp->mac_version) {
4288 case RTL_GIGA_MAC_VER_07:
4289 rtl_hw_start_8102e_1(ioaddr, pdev);
4290 break;
4291
4292 case RTL_GIGA_MAC_VER_08:
4293 rtl_hw_start_8102e_3(ioaddr, pdev);
4294 break;
4295
4296 case RTL_GIGA_MAC_VER_09:
4297 rtl_hw_start_8102e_2(ioaddr, pdev);
4298 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004299
4300 case RTL_GIGA_MAC_VER_29:
4301 rtl_hw_start_8105e_1(ioaddr, pdev);
4302 break;
4303 case RTL_GIGA_MAC_VER_30:
4304 rtl_hw_start_8105e_2(ioaddr, pdev);
4305 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02004306 }
4307
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004308 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02004309
françois romieuf0298f82011-01-03 15:07:42 +00004310 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02004311
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004312 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02004313
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004314 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02004315 RTL_W16(CPlusCmd, tp->cp_cmd);
4316
4317 RTL_W16(IntrMitigate, 0x0000);
4318
4319 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4320
4321 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4322 rtl_set_rx_tx_config_registers(tp);
4323
Francois Romieucdf1a602007-06-11 23:29:50 +02004324 RTL_R8(IntrMask);
4325
Francois Romieucdf1a602007-06-11 23:29:50 +02004326 rtl_set_rx_mode(dev);
4327
4328 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004329
Francois Romieu0e485152007-02-20 00:00:26 +01004330 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331}
4332
4333static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
4334{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 if (new_mtu < ETH_ZLEN || new_mtu > SafeMtu)
4336 return -EINVAL;
4337
4338 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00004339 netdev_update_features(dev);
4340
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00004341 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342}
4343
4344static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
4345{
Al Viro95e09182007-12-22 18:55:39 +00004346 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
4348}
4349
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004350static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
4351 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004353 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004354 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004355
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004356 kfree(*data_buff);
4357 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 rtl8169_make_unusable_by_asic(desc);
4359}
4360
4361static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
4362{
4363 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
4364
4365 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
4366}
4367
4368static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
4369 u32 rx_buf_sz)
4370{
4371 desc->addr = cpu_to_le64(mapping);
4372 wmb();
4373 rtl8169_mark_to_asic(desc, rx_buf_sz);
4374}
4375
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004376static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004378 return (void *)ALIGN((long)data, 16);
4379}
4380
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004381static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
4382 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004383{
4384 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004386 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004387 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004388 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004390 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
4391 if (!data)
4392 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01004393
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004394 if (rtl8169_align(data) != data) {
4395 kfree(data);
4396 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
4397 if (!data)
4398 return NULL;
4399 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004400
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004401 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004402 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004403 if (unlikely(dma_mapping_error(d, mapping))) {
4404 if (net_ratelimit())
4405 netif_err(tp, drv, tp->dev, "Failed to map RX 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 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004410 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004411
4412err_out:
4413 kfree(data);
4414 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415}
4416
4417static void rtl8169_rx_clear(struct rtl8169_private *tp)
4418{
Francois Romieu07d3f512007-02-21 22:40:46 +01004419 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420
4421 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004422 if (tp->Rx_databuff[i]) {
4423 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 tp->RxDescArray + i);
4425 }
4426 }
4427}
4428
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004429static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004431 desc->opts1 |= cpu_to_le32(RingEnd);
4432}
Francois Romieu5b0384f2006-08-16 16:00:01 +02004433
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004434static int rtl8169_rx_fill(struct rtl8169_private *tp)
4435{
4436 unsigned int i;
4437
4438 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004439 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02004440
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004441 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004443
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004444 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004445 if (!data) {
4446 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004447 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004448 }
4449 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004452 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
4453 return 0;
4454
4455err_out:
4456 rtl8169_rx_clear(tp);
4457 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458}
4459
4460static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4461{
4462 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
4463}
4464
4465static int rtl8169_init_ring(struct net_device *dev)
4466{
4467 struct rtl8169_private *tp = netdev_priv(dev);
4468
4469 rtl8169_init_ring_indexes(tp);
4470
4471 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004472 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004474 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475}
4476
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004477static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 struct TxDesc *desc)
4479{
4480 unsigned int len = tx_skb->len;
4481
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004482 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
4483
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 desc->opts1 = 0x00;
4485 desc->opts2 = 0x00;
4486 desc->addr = 0x00;
4487 tx_skb->len = 0;
4488}
4489
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004490static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
4491 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492{
4493 unsigned int i;
4494
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004495 for (i = 0; i < n; i++) {
4496 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 struct ring_info *tx_skb = tp->tx_skb + entry;
4498 unsigned int len = tx_skb->len;
4499
4500 if (len) {
4501 struct sk_buff *skb = tx_skb->skb;
4502
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004503 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 tp->TxDescArray + entry);
4505 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00004506 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 dev_kfree_skb(skb);
4508 tx_skb->skb = NULL;
4509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 }
4511 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004512}
4513
4514static void rtl8169_tx_clear(struct rtl8169_private *tp)
4515{
4516 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 tp->cur_tx = tp->dirty_tx = 0;
4518}
4519
David Howellsc4028952006-11-22 14:57:56 +00004520static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521{
4522 struct rtl8169_private *tp = netdev_priv(dev);
4523
David Howellsc4028952006-11-22 14:57:56 +00004524 PREPARE_DELAYED_WORK(&tp->task, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 schedule_delayed_work(&tp->task, 4);
4526}
4527
4528static void rtl8169_wait_for_quiescence(struct net_device *dev)
4529{
4530 struct rtl8169_private *tp = netdev_priv(dev);
4531 void __iomem *ioaddr = tp->mmio_addr;
4532
4533 synchronize_irq(dev->irq);
4534
4535 /* Wait for any pending NAPI task to complete */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004536 napi_disable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537
4538 rtl8169_irq_mask_and_ack(ioaddr);
4539
David S. Millerd1d08d12008-01-07 20:53:33 -08004540 tp->intr_mask = 0xffff;
4541 RTL_W16(IntrMask, tp->intr_event);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004542 napi_enable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543}
4544
David Howellsc4028952006-11-22 14:57:56 +00004545static void rtl8169_reinit_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546{
David Howellsc4028952006-11-22 14:57:56 +00004547 struct rtl8169_private *tp =
4548 container_of(work, struct rtl8169_private, task.work);
4549 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550 int ret;
4551
Francois Romieueb2a0212007-02-15 23:37:21 +01004552 rtnl_lock();
4553
4554 if (!netif_running(dev))
4555 goto out_unlock;
4556
4557 rtl8169_wait_for_quiescence(dev);
4558 rtl8169_close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559
4560 ret = rtl8169_open(dev);
4561 if (unlikely(ret < 0)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004562 if (net_ratelimit())
4563 netif_err(tp, drv, dev,
4564 "reinit failure (status = %d). Rescheduling\n",
4565 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 rtl8169_schedule_work(dev, rtl8169_reinit_task);
4567 }
Francois Romieueb2a0212007-02-15 23:37:21 +01004568
4569out_unlock:
4570 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571}
4572
David Howellsc4028952006-11-22 14:57:56 +00004573static void rtl8169_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574{
David Howellsc4028952006-11-22 14:57:56 +00004575 struct rtl8169_private *tp =
4576 container_of(work, struct rtl8169_private, task.work);
4577 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01004578 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579
Francois Romieueb2a0212007-02-15 23:37:21 +01004580 rtnl_lock();
4581
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 if (!netif_running(dev))
Francois Romieueb2a0212007-02-15 23:37:21 +01004583 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584
4585 rtl8169_wait_for_quiescence(dev);
4586
Francois Romieu56de4142011-03-15 17:29:31 +01004587 for (i = 0; i < NUM_RX_DESC; i++)
4588 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
4589
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 rtl8169_tx_clear(tp);
4591
Francois Romieu56de4142011-03-15 17:29:31 +01004592 rtl8169_init_ring_indexes(tp);
4593 rtl_hw_start(dev);
4594 netif_wake_queue(dev);
4595 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Francois Romieueb2a0212007-02-15 23:37:21 +01004596
4597out_unlock:
4598 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599}
4600
4601static void rtl8169_tx_timeout(struct net_device *dev)
4602{
4603 struct rtl8169_private *tp = netdev_priv(dev);
4604
françois romieue6de30d2011-01-03 15:08:37 +00004605 rtl8169_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606
4607 /* Let's wait a bit while any (async) irq lands on */
4608 rtl8169_schedule_work(dev, rtl8169_reset_task);
4609}
4610
4611static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07004612 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613{
4614 struct skb_shared_info *info = skb_shinfo(skb);
4615 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04004616 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004617 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618
4619 entry = tp->cur_tx;
4620 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
4621 skb_frag_t *frag = info->frags + cur_frag;
4622 dma_addr_t mapping;
4623 u32 status, len;
4624 void *addr;
4625
4626 entry = (entry + 1) % NUM_TX_DESC;
4627
4628 txd = tp->TxDescArray + entry;
4629 len = frag->size;
4630 addr = ((void *) page_address(frag->page)) + frag->page_offset;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004631 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004632 if (unlikely(dma_mapping_error(d, mapping))) {
4633 if (net_ratelimit())
4634 netif_err(tp, drv, tp->dev,
4635 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004636 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638
Francois Romieucecb5fd2011-04-01 10:21:07 +02004639 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07004640 status = opts[0] | len |
4641 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642
4643 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07004644 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 txd->addr = cpu_to_le64(mapping);
4646
4647 tp->tx_skb[entry].len = len;
4648 }
4649
4650 if (cur_frag) {
4651 tp->tx_skb[entry].skb = skb;
4652 txd->opts1 |= cpu_to_le32(LastFrag);
4653 }
4654
4655 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004656
4657err_out:
4658 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
4659 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660}
4661
Francois Romieu2b7b4312011-04-18 22:53:24 -07004662static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
4663 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664{
Francois Romieu2b7b4312011-04-18 22:53:24 -07004665 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00004666 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07004667 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668
Francois Romieu2b7b4312011-04-18 22:53:24 -07004669 if (mss) {
4670 opts[0] |= TD_LSO;
4671 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
4672 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07004673 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674
4675 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07004676 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07004678 opts[offset] |= info->checksum.udp;
4679 else
4680 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682}
4683
Stephen Hemminger613573252009-08-31 19:50:58 +00004684static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
4685 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686{
4687 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004688 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 struct TxDesc *txd = tp->TxDescArray + entry;
4690 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004691 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 dma_addr_t mapping;
4693 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07004694 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004695 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02004696
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004698 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004699 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 }
4701
4702 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004703 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004705 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004706 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004707 if (unlikely(dma_mapping_error(d, mapping))) {
4708 if (net_ratelimit())
4709 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004710 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712
4713 tp->tx_skb[entry].len = len;
4714 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715
Francois Romieu2b7b4312011-04-18 22:53:24 -07004716 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
4717 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004718
Francois Romieu2b7b4312011-04-18 22:53:24 -07004719 rtl8169_tso_csum(tp, skb, opts);
4720
4721 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004722 if (frags < 0)
4723 goto err_dma_1;
4724 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07004725 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004726 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07004727 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004728 tp->tx_skb[entry].skb = skb;
4729 }
4730
Francois Romieu2b7b4312011-04-18 22:53:24 -07004731 txd->opts2 = cpu_to_le32(opts[1]);
4732
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 wmb();
4734
Francois Romieucecb5fd2011-04-01 10:21:07 +02004735 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07004736 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 txd->opts1 = cpu_to_le32(status);
4738
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 tp->cur_tx += frags + 1;
4740
David Dillow4c020a92010-03-03 16:33:10 +00004741 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742
Francois Romieucecb5fd2011-04-01 10:21:07 +02004743 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744
4745 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
4746 netif_stop_queue(dev);
4747 smp_rmb();
4748 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
4749 netif_wake_queue(dev);
4750 }
4751
Stephen Hemminger613573252009-08-31 19:50:58 +00004752 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004754err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004755 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004756err_dma_0:
4757 dev_kfree_skb(skb);
4758 dev->stats.tx_dropped++;
4759 return NETDEV_TX_OK;
4760
4761err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004763 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00004764 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765}
4766
4767static void rtl8169_pcierr_interrupt(struct net_device *dev)
4768{
4769 struct rtl8169_private *tp = netdev_priv(dev);
4770 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 u16 pci_status, pci_cmd;
4772
4773 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
4774 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
4775
Joe Perchesbf82c182010-02-09 11:49:50 +00004776 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
4777 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778
4779 /*
4780 * The recovery sequence below admits a very elaborated explanation:
4781 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01004782 * - I did not see what else could be done;
4783 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 *
4785 * Feel free to adjust to your needs.
4786 */
Francois Romieua27993f2006-12-18 00:04:19 +01004787 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01004788 pci_cmd &= ~PCI_COMMAND_PARITY;
4789 else
4790 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
4791
4792 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793
4794 pci_write_config_word(pdev, PCI_STATUS,
4795 pci_status & (PCI_STATUS_DETECTED_PARITY |
4796 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
4797 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
4798
4799 /* The infamous DAC f*ckup only happens at boot time */
4800 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00004801 void __iomem *ioaddr = tp->mmio_addr;
4802
Joe Perchesbf82c182010-02-09 11:49:50 +00004803 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 tp->cp_cmd &= ~PCIDAC;
4805 RTL_W16(CPlusCmd, tp->cp_cmd);
4806 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 }
4808
françois romieue6de30d2011-01-03 15:08:37 +00004809 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01004810
4811 rtl8169_schedule_work(dev, rtl8169_reinit_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812}
4813
Francois Romieu07d3f512007-02-21 22:40:46 +01004814static void rtl8169_tx_interrupt(struct net_device *dev,
4815 struct rtl8169_private *tp,
4816 void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817{
4818 unsigned int dirty_tx, tx_left;
4819
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 dirty_tx = tp->dirty_tx;
4821 smp_rmb();
4822 tx_left = tp->cur_tx - dirty_tx;
4823
4824 while (tx_left > 0) {
4825 unsigned int entry = dirty_tx % NUM_TX_DESC;
4826 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 u32 status;
4828
4829 rmb();
4830 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
4831 if (status & DescOwn)
4832 break;
4833
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004834 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
4835 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 if (status & LastFrag) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00004837 dev->stats.tx_packets++;
4838 dev->stats.tx_bytes += tx_skb->skb->len;
Eric Dumazet87433bf2009-06-09 22:55:53 +00004839 dev_kfree_skb(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 tx_skb->skb = NULL;
4841 }
4842 dirty_tx++;
4843 tx_left--;
4844 }
4845
4846 if (tp->dirty_tx != dirty_tx) {
4847 tp->dirty_tx = dirty_tx;
4848 smp_wmb();
4849 if (netif_queue_stopped(dev) &&
4850 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
4851 netif_wake_queue(dev);
4852 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02004853 /*
4854 * 8168 hack: TxPoll requests are lost when the Tx packets are
4855 * too close. Let's kick an extra TxPoll request when a burst
4856 * of start_xmit activity is detected (if it is not detected,
4857 * it is slow enough). -- FR
4858 */
4859 smp_rmb();
4860 if (tp->cur_tx != dirty_tx)
4861 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 }
4863}
4864
Francois Romieu126fa4b2005-05-12 20:09:17 -04004865static inline int rtl8169_fragmented_frame(u32 status)
4866{
4867 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
4868}
4869
Eric Dumazetadea1ac72010-09-05 20:04:05 -07004870static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 u32 status = opts1 & RxProtoMask;
4873
4874 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00004875 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 skb->ip_summed = CHECKSUM_UNNECESSARY;
4877 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07004878 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879}
4880
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004881static struct sk_buff *rtl8169_try_rx_copy(void *data,
4882 struct rtl8169_private *tp,
4883 int pkt_size,
4884 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02004886 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004887 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004889 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004890 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004891 prefetch(data);
4892 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
4893 if (skb)
4894 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004895 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
4896
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004897 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898}
4899
Francois Romieu07d3f512007-02-21 22:40:46 +01004900static int rtl8169_rx_interrupt(struct net_device *dev,
4901 struct rtl8169_private *tp,
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004902 void __iomem *ioaddr, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903{
4904 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004905 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 cur_rx = tp->cur_rx;
4908 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02004909 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910
Richard Dawe4dcb7d32005-05-27 21:12:00 +02004911 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04004913 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 u32 status;
4915
4916 rmb();
Francois Romieu126fa4b2005-05-12 20:09:17 -04004917 status = le32_to_cpu(desc->opts1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918
4919 if (status & DescOwn)
4920 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02004921 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004922 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
4923 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004924 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02004926 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02004928 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02004929 if (status & RxFOVF) {
4930 rtl8169_schedule_work(dev, rtl8169_reset_task);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004931 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02004932 }
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004933 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004935 struct sk_buff *skb;
Stephen Hemmingerb4496552007-06-17 01:06:49 +02004936 dma_addr_t addr = le64_to_cpu(desc->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 int pkt_size = (status & 0x00001FFF) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938
Francois Romieu126fa4b2005-05-12 20:09:17 -04004939 /*
4940 * The driver does not support incoming fragmented
4941 * frames. They are seen as a symptom of over-mtu
4942 * sized frames.
4943 */
4944 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02004945 dev->stats.rx_dropped++;
4946 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004947 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02004948 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04004949 }
4950
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004951 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
4952 tp, pkt_size, addr);
4953 rtl8169_mark_to_asic(desc, rx_buf_sz);
4954 if (!skb) {
4955 dev->stats.rx_dropped++;
4956 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 }
4958
Eric Dumazetadea1ac72010-09-05 20:04:05 -07004959 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 skb_put(skb, pkt_size);
4961 skb->protocol = eth_type_trans(skb, dev);
4962
Francois Romieu7a8fc772011-03-01 17:18:33 +01004963 rtl8169_rx_vlan_tag(desc, skb);
4964
Francois Romieu56de4142011-03-15 17:29:31 +01004965 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966
Francois Romieucebf8cc2007-10-18 12:06:54 +02004967 dev->stats.rx_bytes += pkt_size;
4968 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 }
Francois Romieu6dccd162007-02-13 23:38:05 +01004970
4971 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00004972 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01004973 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
4974 desc->opts2 = 0;
4975 cur_rx++;
4976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 }
4978
4979 count = cur_rx - tp->cur_rx;
4980 tp->cur_rx = cur_rx;
4981
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004982 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983
4984 return count;
4985}
4986
Francois Romieu07d3f512007-02-21 22:40:46 +01004987static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988{
Francois Romieu07d3f512007-02-21 22:40:46 +01004989 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 int handled = 0;
Francois Romieu865c6522008-05-11 14:51:00 +02004993 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994
David Dillowf11a3772009-05-22 15:29:34 +00004995 /* loop handling interrupts until we have no new ones or
4996 * we hit a invalid/hotplug case.
4997 */
Francois Romieu865c6522008-05-11 14:51:00 +02004998 status = RTL_R16(IntrStatus);
David Dillowf11a3772009-05-22 15:29:34 +00004999 while (status && status != 0xffff) {
5000 handled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001
David Dillowf11a3772009-05-22 15:29:34 +00005002 /* Handle all of the error cases first. These will reset
5003 * the chip, so just exit the loop.
5004 */
5005 if (unlikely(!netif_running(dev))) {
5006 rtl8169_asic_down(ioaddr);
5007 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008 }
David Dillowf11a3772009-05-22 15:29:34 +00005009
Francois Romieu1519e572011-02-03 12:02:36 +01005010 if (unlikely(status & RxFIFOOver)) {
5011 switch (tp->mac_version) {
5012 /* Work around for rx fifo overflow */
5013 case RTL_GIGA_MAC_VER_11:
5014 case RTL_GIGA_MAC_VER_22:
5015 case RTL_GIGA_MAC_VER_26:
5016 netif_stop_queue(dev);
5017 rtl8169_tx_timeout(dev);
5018 goto done;
Francois Romieuf60ac8e2011-02-03 17:27:52 +01005019 /* Testers needed. */
5020 case RTL_GIGA_MAC_VER_17:
5021 case RTL_GIGA_MAC_VER_19:
5022 case RTL_GIGA_MAC_VER_20:
5023 case RTL_GIGA_MAC_VER_21:
5024 case RTL_GIGA_MAC_VER_23:
5025 case RTL_GIGA_MAC_VER_24:
5026 case RTL_GIGA_MAC_VER_27:
5027 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00005028 case RTL_GIGA_MAC_VER_31:
Francois Romieu1519e572011-02-03 12:02:36 +01005029 /* Experimental science. Pktgen proof. */
5030 case RTL_GIGA_MAC_VER_12:
5031 case RTL_GIGA_MAC_VER_25:
5032 if (status == RxFIFOOver)
5033 goto done;
5034 break;
5035 default:
5036 break;
5037 }
David Dillowf11a3772009-05-22 15:29:34 +00005038 }
5039
5040 if (unlikely(status & SYSErr)) {
5041 rtl8169_pcierr_interrupt(dev);
5042 break;
5043 }
5044
5045 if (status & LinkChg)
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005046 __rtl8169_check_link_status(dev, tp, ioaddr, true);
David Dillowf11a3772009-05-22 15:29:34 +00005047
5048 /* We need to see the lastest version of tp->intr_mask to
5049 * avoid ignoring an MSI interrupt and having to wait for
5050 * another event which may never come.
5051 */
5052 smp_rmb();
5053 if (status & tp->intr_mask & tp->napi_event) {
5054 RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
5055 tp->intr_mask = ~tp->napi_event;
5056
5057 if (likely(napi_schedule_prep(&tp->napi)))
5058 __napi_schedule(&tp->napi);
Joe Perchesbf82c182010-02-09 11:49:50 +00005059 else
5060 netif_info(tp, intr, dev,
5061 "interrupt %04x in poll\n", status);
David Dillowf11a3772009-05-22 15:29:34 +00005062 }
5063
5064 /* We only get a new MSI interrupt when all active irq
5065 * sources on the chip have been acknowledged. So, ack
5066 * everything we've seen and check if new sources have become
5067 * active to avoid blocking all interrupts from the chip.
5068 */
5069 RTL_W16(IntrStatus,
5070 (status & RxFIFOOver) ? (status | RxOverflow) : status);
5071 status = RTL_R16(IntrStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 }
Francois Romieu1519e572011-02-03 12:02:36 +01005073done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 return IRQ_RETVAL(handled);
5075}
5076
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005077static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005079 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5080 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 void __iomem *ioaddr = tp->mmio_addr;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005082 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005084 work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 rtl8169_tx_interrupt(dev, tp, ioaddr);
5086
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005087 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005088 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00005089
5090 /* We need for force the visibility of tp->intr_mask
5091 * for other CPUs, as we can loose an MSI interrupt
5092 * and potentially wait for a retransmit timeout if we don't.
5093 * The posted write to IntrMask is safe, as it will
5094 * eventually make it to the chip and we won't loose anything
5095 * until it does.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 */
David Dillowf11a3772009-05-22 15:29:34 +00005097 tp->intr_mask = 0xffff;
David Dillow4c020a92010-03-03 16:33:10 +00005098 wmb();
Francois Romieu0e485152007-02-20 00:00:26 +01005099 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 }
5101
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005102 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104
Francois Romieu523a6092008-09-10 22:28:56 +02005105static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5106{
5107 struct rtl8169_private *tp = netdev_priv(dev);
5108
5109 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5110 return;
5111
5112 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5113 RTL_W32(RxMissed, 0);
5114}
5115
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116static void rtl8169_down(struct net_device *dev)
5117{
5118 struct rtl8169_private *tp = netdev_priv(dev);
5119 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120
Francois Romieu4876cc12011-03-11 21:07:11 +01005121 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122
5123 netif_stop_queue(dev);
5124
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005125 napi_disable(&tp->napi);
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005126
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 spin_lock_irq(&tp->lock);
5128
5129 rtl8169_asic_down(ioaddr);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005130 /*
5131 * At this point device interrupts can not be enabled in any function,
5132 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task,
5133 * rtl8169_reinit_task) and napi is disabled (rtl8169_poll).
5134 */
Francois Romieu523a6092008-09-10 22:28:56 +02005135 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136
5137 spin_unlock_irq(&tp->lock);
5138
5139 synchronize_irq(dev->irq);
5140
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 /* Give a racing hard_start_xmit a few cycles to complete. */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07005142 synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 rtl8169_tx_clear(tp);
5145
5146 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00005147
5148 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149}
5150
5151static int rtl8169_close(struct net_device *dev)
5152{
5153 struct rtl8169_private *tp = netdev_priv(dev);
5154 struct pci_dev *pdev = tp->pci_dev;
5155
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005156 pm_runtime_get_sync(&pdev->dev);
5157
Francois Romieucecb5fd2011-04-01 10:21:07 +02005158 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08005159 rtl8169_update_counters(dev);
5160
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 rtl8169_down(dev);
5162
5163 free_irq(dev->irq, dev);
5164
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00005165 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5166 tp->RxPhyAddr);
5167 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5168 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169 tp->TxDescArray = NULL;
5170 tp->RxDescArray = NULL;
5171
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005172 pm_runtime_put_sync(&pdev->dev);
5173
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 return 0;
5175}
5176
Francois Romieu07ce4062007-02-23 23:36:39 +01005177static void rtl_set_rx_mode(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178{
5179 struct rtl8169_private *tp = netdev_priv(dev);
5180 void __iomem *ioaddr = tp->mmio_addr;
5181 unsigned long flags;
5182 u32 mc_filter[2]; /* Multicast hash filter */
Francois Romieu07d3f512007-02-21 22:40:46 +01005183 int rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 u32 tmp = 0;
5185
5186 if (dev->flags & IFF_PROMISC) {
5187 /* Unconditionally log net taps. */
Joe Perchesbf82c182010-02-09 11:49:50 +00005188 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189 rx_mode =
5190 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5191 AcceptAllPhys;
5192 mc_filter[1] = mc_filter[0] = 0xffffffff;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00005193 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
Joe Perches8e95a202009-12-03 07:58:21 +00005194 (dev->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 /* Too many to filter perfectly -- accept all multicasts. */
5196 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5197 mc_filter[1] = mc_filter[0] = 0xffffffff;
5198 } else {
Jiri Pirko22bedad2010-04-01 21:22:57 +00005199 struct netdev_hw_addr *ha;
Francois Romieu07d3f512007-02-21 22:40:46 +01005200
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201 rx_mode = AcceptBroadcast | AcceptMyPhys;
5202 mc_filter[1] = mc_filter[0] = 0;
Jiri Pirko22bedad2010-04-01 21:22:57 +00005203 netdev_for_each_mc_addr(ha, dev) {
5204 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5206 rx_mode |= AcceptMulticast;
5207 }
5208 }
5209
5210 spin_lock_irqsave(&tp->lock, flags);
5211
5212 tmp = rtl8169_rx_config | rx_mode |
Francois Romieu2b7b4312011-04-18 22:53:24 -07005213 (RTL_R32(RxConfig) & RTL_RX_CONFIG_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214
Francois Romieuf887cce2008-07-17 22:24:18 +02005215 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
Francois Romieu1087f4f2007-12-26 22:46:05 +01005216 u32 data = mc_filter[0];
5217
5218 mc_filter[0] = swab32(mc_filter[1]);
5219 mc_filter[1] = swab32(data);
Francois Romieubcf0bf92006-07-26 23:14:13 +02005220 }
5221
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 RTL_W32(MAR0 + 4, mc_filter[1]);
Francois Romieu78f1cd02010-03-27 19:35:46 -07005223 RTL_W32(MAR0 + 0, mc_filter[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224
Francois Romieu57a9f232007-06-04 22:10:15 +02005225 RTL_W32(RxConfig, tmp);
5226
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 spin_unlock_irqrestore(&tp->lock, flags);
5228}
5229
5230/**
5231 * rtl8169_get_stats - Get rtl8169 read/write statistics
5232 * @dev: The Ethernet Device to get statistics for
5233 *
5234 * Get TX/RX statistics for rtl8169
5235 */
5236static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
5237{
5238 struct rtl8169_private *tp = netdev_priv(dev);
5239 void __iomem *ioaddr = tp->mmio_addr;
5240 unsigned long flags;
5241
5242 if (netif_running(dev)) {
5243 spin_lock_irqsave(&tp->lock, flags);
Francois Romieu523a6092008-09-10 22:28:56 +02005244 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 spin_unlock_irqrestore(&tp->lock, flags);
5246 }
Francois Romieu5b0384f2006-08-16 16:00:01 +02005247
Francois Romieucebf8cc2007-10-18 12:06:54 +02005248 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249}
5250
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005251static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01005252{
françois romieu065c27c2011-01-03 15:08:12 +00005253 struct rtl8169_private *tp = netdev_priv(dev);
5254
Francois Romieu5d06a992006-02-23 00:47:58 +01005255 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005256 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01005257
françois romieu065c27c2011-01-03 15:08:12 +00005258 rtl_pll_power_down(tp);
5259
Francois Romieu5d06a992006-02-23 00:47:58 +01005260 netif_device_detach(dev);
5261 netif_stop_queue(dev);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005262}
Francois Romieu5d06a992006-02-23 00:47:58 +01005263
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005264#ifdef CONFIG_PM
5265
5266static int rtl8169_suspend(struct device *device)
5267{
5268 struct pci_dev *pdev = to_pci_dev(device);
5269 struct net_device *dev = pci_get_drvdata(pdev);
5270
5271 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02005272
Francois Romieu5d06a992006-02-23 00:47:58 +01005273 return 0;
5274}
5275
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005276static void __rtl8169_resume(struct net_device *dev)
5277{
françois romieu065c27c2011-01-03 15:08:12 +00005278 struct rtl8169_private *tp = netdev_priv(dev);
5279
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005280 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00005281
5282 rtl_pll_power_up(tp);
5283
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005284 rtl8169_schedule_work(dev, rtl8169_reset_task);
5285}
5286
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005287static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01005288{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005289 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01005290 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005291 struct rtl8169_private *tp = netdev_priv(dev);
5292
5293 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01005294
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005295 if (netif_running(dev))
5296 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01005297
Francois Romieu5d06a992006-02-23 00:47:58 +01005298 return 0;
5299}
5300
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005301static int rtl8169_runtime_suspend(struct device *device)
5302{
5303 struct pci_dev *pdev = to_pci_dev(device);
5304 struct net_device *dev = pci_get_drvdata(pdev);
5305 struct rtl8169_private *tp = netdev_priv(dev);
5306
5307 if (!tp->TxDescArray)
5308 return 0;
5309
5310 spin_lock_irq(&tp->lock);
5311 tp->saved_wolopts = __rtl8169_get_wol(tp);
5312 __rtl8169_set_wol(tp, WAKE_ANY);
5313 spin_unlock_irq(&tp->lock);
5314
5315 rtl8169_net_suspend(dev);
5316
5317 return 0;
5318}
5319
5320static int rtl8169_runtime_resume(struct device *device)
5321{
5322 struct pci_dev *pdev = to_pci_dev(device);
5323 struct net_device *dev = pci_get_drvdata(pdev);
5324 struct rtl8169_private *tp = netdev_priv(dev);
5325
5326 if (!tp->TxDescArray)
5327 return 0;
5328
5329 spin_lock_irq(&tp->lock);
5330 __rtl8169_set_wol(tp, tp->saved_wolopts);
5331 tp->saved_wolopts = 0;
5332 spin_unlock_irq(&tp->lock);
5333
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005334 rtl8169_init_phy(dev, tp);
5335
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005336 __rtl8169_resume(dev);
5337
5338 return 0;
5339}
5340
5341static int rtl8169_runtime_idle(struct device *device)
5342{
5343 struct pci_dev *pdev = to_pci_dev(device);
5344 struct net_device *dev = pci_get_drvdata(pdev);
5345 struct rtl8169_private *tp = netdev_priv(dev);
5346
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005347 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005348}
5349
Alexey Dobriyan47145212009-12-14 18:00:08 -08005350static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02005351 .suspend = rtl8169_suspend,
5352 .resume = rtl8169_resume,
5353 .freeze = rtl8169_suspend,
5354 .thaw = rtl8169_resume,
5355 .poweroff = rtl8169_suspend,
5356 .restore = rtl8169_resume,
5357 .runtime_suspend = rtl8169_runtime_suspend,
5358 .runtime_resume = rtl8169_runtime_resume,
5359 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005360};
5361
5362#define RTL8169_PM_OPS (&rtl8169_pm_ops)
5363
5364#else /* !CONFIG_PM */
5365
5366#define RTL8169_PM_OPS NULL
5367
5368#endif /* !CONFIG_PM */
5369
Francois Romieu1765f952008-09-13 17:21:40 +02005370static void rtl_shutdown(struct pci_dev *pdev)
5371{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005372 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00005373 struct rtl8169_private *tp = netdev_priv(dev);
5374 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu1765f952008-09-13 17:21:40 +02005375
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005376 rtl8169_net_suspend(dev);
5377
Francois Romieucecb5fd2011-04-01 10:21:07 +02005378 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08005379 rtl_rar_set(tp, dev->perm_addr);
5380
françois romieu4bb3f522009-06-17 11:41:45 +00005381 spin_lock_irq(&tp->lock);
5382
5383 rtl8169_asic_down(ioaddr);
5384
5385 spin_unlock_irq(&tp->lock);
5386
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005387 if (system_state == SYSTEM_POWER_OFF) {
françois romieuca52efd2009-07-24 12:34:19 +00005388 /* WoL fails with some 8168 when the receiver is disabled. */
5389 if (tp->features & RTL_FEATURE_WOL) {
5390 pci_clear_master(pdev);
5391
5392 RTL_W8(ChipCmd, CmdRxEnb);
5393 /* PCI commit */
5394 RTL_R8(ChipCmd);
5395 }
5396
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005397 pci_wake_from_d3(pdev, true);
5398 pci_set_power_state(pdev, PCI_D3hot);
5399 }
5400}
Francois Romieu5d06a992006-02-23 00:47:58 +01005401
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402static struct pci_driver rtl8169_pci_driver = {
5403 .name = MODULENAME,
5404 .id_table = rtl8169_pci_tbl,
5405 .probe = rtl8169_init_one,
5406 .remove = __devexit_p(rtl8169_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02005407 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005408 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409};
5410
Francois Romieu07d3f512007-02-21 22:40:46 +01005411static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412{
Jeff Garzik29917622006-08-19 17:48:59 -04005413 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414}
5415
Francois Romieu07d3f512007-02-21 22:40:46 +01005416static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417{
5418 pci_unregister_driver(&rtl8169_pci_driver);
5419}
5420
5421module_init(rtl8169_init_module);
5422module_exit(rtl8169_cleanup_module);