blob: 5f838ef924945c79360fc4b51f3ab49568694a3f [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>
Paul Gortmaker70c71602011-05-22 16:47:17 -040029#include <linux/prefetch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Francois Romieu99f252b2007-04-02 22:59:59 +020031#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/io.h>
33#include <asm/irq.h>
34
Francois Romieu865c6522008-05-11 14:51:00 +020035#define RTL8169_VERSION "2.3LK-NAPI"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#define MODULENAME "r8169"
37#define PFX MODULENAME ": "
38
françois romieubca03d52011-01-03 15:07:31 +000039#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
40#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
hayeswang01dc7fe2011-03-21 01:50:28 +000041#define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
42#define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080043#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
françois romieubca03d52011-01-03 15:07:31 +000044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#ifdef RTL8169_DEBUG
46#define assert(expr) \
Francois Romieu5b0384f2006-08-16 16:00:01 +020047 if (!(expr)) { \
48 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
Harvey Harrisonb39d66a2008-08-20 16:52:04 -070049 #expr,__FILE__,__func__,__LINE__); \
Francois Romieu5b0384f2006-08-16 16:00:01 +020050 }
Joe Perches06fa7352007-10-18 21:15:00 +020051#define dprintk(fmt, args...) \
52 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#else
54#define assert(expr) do {} while (0)
55#define dprintk(fmt, args...) do {} while (0)
56#endif /* RTL8169_DEBUG */
57
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020058#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d2005-09-30 16:54:02 -070059 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#define TX_BUFFS_AVAIL(tp) \
62 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
65 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050066static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68/* MAC address length */
69#define MAC_ADDR_LEN 6
70
Francois Romieu9c14cea2008-07-05 00:21:15 +020071#define MAX_READ_REQUEST_SHIFT 12
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define RX_FIFO_THRESH 7 /* 7 means NO threshold, Rx buffer level before first PCI xfer. */
73#define RX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
74#define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
76#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
77
78#define R8169_REGS_SIZE 256
79#define R8169_NAPI_WEIGHT 64
80#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
81#define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
82#define RX_BUF_SIZE 1536 /* Rx Buffer size */
83#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
84#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
85
86#define RTL8169_TX_TIMEOUT (6*HZ)
87#define RTL8169_PHY_TIMEOUT (10*HZ)
88
françois romieuea8dbdd2009-03-15 01:10:50 +000089#define RTL_EEPROM_SIG cpu_to_le32(0x8129)
90#define RTL_EEPROM_SIG_MASK cpu_to_le32(0xffff)
Francois Romieue1564ec2008-10-16 22:46:13 +020091#define RTL_EEPROM_SIG_ADDR 0x0000
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/* write/read MMIO register */
94#define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
95#define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
96#define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
97#define RTL_R8(reg) readb (ioaddr + (reg))
98#define RTL_R16(reg) readw (ioaddr + (reg))
Junchang Wang06f555f2010-05-30 02:26:07 +000099#define RTL_R32(reg) readl (ioaddr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +0200102 RTL_GIGA_MAC_VER_01 = 0,
103 RTL_GIGA_MAC_VER_02,
104 RTL_GIGA_MAC_VER_03,
105 RTL_GIGA_MAC_VER_04,
106 RTL_GIGA_MAC_VER_05,
107 RTL_GIGA_MAC_VER_06,
108 RTL_GIGA_MAC_VER_07,
109 RTL_GIGA_MAC_VER_08,
110 RTL_GIGA_MAC_VER_09,
111 RTL_GIGA_MAC_VER_10,
112 RTL_GIGA_MAC_VER_11,
113 RTL_GIGA_MAC_VER_12,
114 RTL_GIGA_MAC_VER_13,
115 RTL_GIGA_MAC_VER_14,
116 RTL_GIGA_MAC_VER_15,
117 RTL_GIGA_MAC_VER_16,
118 RTL_GIGA_MAC_VER_17,
119 RTL_GIGA_MAC_VER_18,
120 RTL_GIGA_MAC_VER_19,
121 RTL_GIGA_MAC_VER_20,
122 RTL_GIGA_MAC_VER_21,
123 RTL_GIGA_MAC_VER_22,
124 RTL_GIGA_MAC_VER_23,
125 RTL_GIGA_MAC_VER_24,
126 RTL_GIGA_MAC_VER_25,
127 RTL_GIGA_MAC_VER_26,
128 RTL_GIGA_MAC_VER_27,
129 RTL_GIGA_MAC_VER_28,
130 RTL_GIGA_MAC_VER_29,
131 RTL_GIGA_MAC_VER_30,
132 RTL_GIGA_MAC_VER_31,
133 RTL_GIGA_MAC_VER_32,
134 RTL_GIGA_MAC_VER_33,
135 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136};
137
Francois Romieu2b7b4312011-04-18 22:53:24 -0700138enum rtl_tx_desc_version {
139 RTL_TD_0 = 0,
140 RTL_TD_1 = 1,
141};
142
Francois Romieu85bffe62011-04-27 08:22:39 +0200143#define _R(NAME,TD,FW) \
144 { .name = NAME, .txd_version = TD, .fw_name = FW }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800146static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 const char *name;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700148 enum rtl_tx_desc_version txd_version;
Francois Romieu85bffe62011-04-27 08:22:39 +0200149 const char *fw_name;
150} rtl_chip_infos[] = {
151 /* PCI devices. */
152 [RTL_GIGA_MAC_VER_01] =
153 _R("RTL8169", RTL_TD_0, NULL),
154 [RTL_GIGA_MAC_VER_02] =
155 _R("RTL8169s", RTL_TD_0, NULL),
156 [RTL_GIGA_MAC_VER_03] =
157 _R("RTL8110s", RTL_TD_0, NULL),
158 [RTL_GIGA_MAC_VER_04] =
159 _R("RTL8169sb/8110sb", RTL_TD_0, NULL),
160 [RTL_GIGA_MAC_VER_05] =
161 _R("RTL8169sc/8110sc", RTL_TD_0, NULL),
162 [RTL_GIGA_MAC_VER_06] =
163 _R("RTL8169sc/8110sc", RTL_TD_0, NULL),
164 /* PCI-E devices. */
165 [RTL_GIGA_MAC_VER_07] =
166 _R("RTL8102e", RTL_TD_1, NULL),
167 [RTL_GIGA_MAC_VER_08] =
168 _R("RTL8102e", RTL_TD_1, NULL),
169 [RTL_GIGA_MAC_VER_09] =
170 _R("RTL8102e", RTL_TD_1, NULL),
171 [RTL_GIGA_MAC_VER_10] =
172 _R("RTL8101e", RTL_TD_0, NULL),
173 [RTL_GIGA_MAC_VER_11] =
174 _R("RTL8168b/8111b", RTL_TD_0, NULL),
175 [RTL_GIGA_MAC_VER_12] =
176 _R("RTL8168b/8111b", RTL_TD_0, NULL),
177 [RTL_GIGA_MAC_VER_13] =
178 _R("RTL8101e", RTL_TD_0, NULL),
179 [RTL_GIGA_MAC_VER_14] =
180 _R("RTL8100e", RTL_TD_0, NULL),
181 [RTL_GIGA_MAC_VER_15] =
182 _R("RTL8100e", RTL_TD_0, NULL),
183 [RTL_GIGA_MAC_VER_16] =
184 _R("RTL8101e", RTL_TD_0, NULL),
185 [RTL_GIGA_MAC_VER_17] =
186 _R("RTL8168b/8111b", RTL_TD_0, NULL),
187 [RTL_GIGA_MAC_VER_18] =
188 _R("RTL8168cp/8111cp", RTL_TD_1, NULL),
189 [RTL_GIGA_MAC_VER_19] =
190 _R("RTL8168c/8111c", RTL_TD_1, NULL),
191 [RTL_GIGA_MAC_VER_20] =
192 _R("RTL8168c/8111c", RTL_TD_1, NULL),
193 [RTL_GIGA_MAC_VER_21] =
194 _R("RTL8168c/8111c", RTL_TD_1, NULL),
195 [RTL_GIGA_MAC_VER_22] =
196 _R("RTL8168c/8111c", RTL_TD_1, NULL),
197 [RTL_GIGA_MAC_VER_23] =
198 _R("RTL8168cp/8111cp", RTL_TD_1, NULL),
199 [RTL_GIGA_MAC_VER_24] =
200 _R("RTL8168cp/8111cp", RTL_TD_1, NULL),
201 [RTL_GIGA_MAC_VER_25] =
202 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1),
203 [RTL_GIGA_MAC_VER_26] =
204 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2),
205 [RTL_GIGA_MAC_VER_27] =
206 _R("RTL8168dp/8111dp", RTL_TD_1, NULL),
207 [RTL_GIGA_MAC_VER_28] =
208 _R("RTL8168dp/8111dp", RTL_TD_1, NULL),
209 [RTL_GIGA_MAC_VER_29] =
210 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1),
211 [RTL_GIGA_MAC_VER_30] =
212 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1),
213 [RTL_GIGA_MAC_VER_31] =
214 _R("RTL8168dp/8111dp", RTL_TD_1, NULL),
215 [RTL_GIGA_MAC_VER_32] =
216 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1),
217 [RTL_GIGA_MAC_VER_33] =
218 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219};
220#undef _R
221
Francois Romieubcf0bf92006-07-26 23:14:13 +0200222enum cfg_version {
223 RTL_CFG_0 = 0x00,
224 RTL_CFG_1,
225 RTL_CFG_2
226};
227
Francois Romieu07ce4062007-02-23 23:36:39 +0100228static void rtl_hw_start_8169(struct net_device *);
229static void rtl_hw_start_8168(struct net_device *);
230static void rtl_hw_start_8101(struct net_device *);
231
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000232static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200233 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200234 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Francois Romieud81bf552006-09-20 21:31:20 +0200235 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100236 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200237 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
238 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Lennart Sorensen71061592011-07-28 13:18:11 +0000239 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4302), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200240 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200241 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
242 { PCI_VENDOR_ID_LINKSYS, 0x1032,
243 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100244 { 0x0001, 0x8168,
245 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 {0,},
247};
248
249MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
250
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000251static int rx_buf_sz = 16383;
David S. Miller4300e8c2010-03-26 10:23:30 -0700252static int use_dac;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200253static struct {
254 u32 msg_enable;
255} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Francois Romieu07d3f512007-02-21 22:40:46 +0100257enum rtl_registers {
258 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100259 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100260 MAR0 = 8, /* Multicast filter. */
261 CounterAddrLow = 0x10,
262 CounterAddrHigh = 0x14,
263 TxDescStartAddrLow = 0x20,
264 TxDescStartAddrHigh = 0x24,
265 TxHDescStartAddrLow = 0x28,
266 TxHDescStartAddrHigh = 0x2c,
267 FLASH = 0x30,
268 ERSR = 0x36,
269 ChipCmd = 0x37,
270 TxPoll = 0x38,
271 IntrMask = 0x3c,
272 IntrStatus = 0x3e,
273 TxConfig = 0x40,
274 RxConfig = 0x44,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700275
276#define RTL_RX_CONFIG_MASK 0xff7e1880u
277
Francois Romieu07d3f512007-02-21 22:40:46 +0100278 RxMissed = 0x4c,
279 Cfg9346 = 0x50,
280 Config0 = 0x51,
281 Config1 = 0x52,
282 Config2 = 0x53,
283 Config3 = 0x54,
284 Config4 = 0x55,
285 Config5 = 0x56,
286 MultiIntr = 0x5c,
287 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100288 PHYstatus = 0x6c,
289 RxMaxSize = 0xda,
290 CPlusCmd = 0xe0,
291 IntrMitigate = 0xe2,
292 RxDescAddrLow = 0xe4,
293 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000294 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
295
296#define NoEarlyTx 0x3f /* Max value : no early transmit. */
297
298 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
299
300#define TxPacketMax (8064 >> 7)
301
Francois Romieu07d3f512007-02-21 22:40:46 +0100302 FuncEvent = 0xf0,
303 FuncEventMask = 0xf4,
304 FuncPresetState = 0xf8,
305 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306};
307
Francois Romieuf162a5d2008-06-01 22:37:49 +0200308enum rtl8110_registers {
309 TBICSR = 0x64,
310 TBI_ANAR = 0x68,
311 TBI_LPAR = 0x6a,
312};
313
314enum rtl8168_8101_registers {
315 CSIDR = 0x64,
316 CSIAR = 0x68,
317#define CSIAR_FLAG 0x80000000
318#define CSIAR_WRITE_CMD 0x80000000
319#define CSIAR_BYTE_ENABLE 0x0f
320#define CSIAR_BYTE_ENABLE_SHIFT 12
321#define CSIAR_ADDR_MASK 0x0fff
françois romieu065c27c2011-01-03 15:08:12 +0000322 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200323 EPHYAR = 0x80,
324#define EPHYAR_FLAG 0x80000000
325#define EPHYAR_WRITE_CMD 0x80000000
326#define EPHYAR_REG_MASK 0x1f
327#define EPHYAR_REG_SHIFT 16
328#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800329 DLLPR = 0xd0,
330#define PM_SWITCH (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200331 DBG_REG = 0xd1,
332#define FIX_NAK_1 (1 << 4)
333#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800334 TWSI = 0xd2,
335 MCU = 0xd3,
336#define EN_NDP (1 << 3)
337#define EN_OOB_RESET (1 << 2)
françois romieudaf9df62009-10-07 12:44:20 +0000338 EFUSEAR = 0xdc,
339#define EFUSEAR_FLAG 0x80000000
340#define EFUSEAR_WRITE_CMD 0x80000000
341#define EFUSEAR_READ_CMD 0x00000000
342#define EFUSEAR_REG_MASK 0x03ff
343#define EFUSEAR_REG_SHIFT 8
344#define EFUSEAR_DATA_MASK 0xff
Francois Romieuf162a5d2008-06-01 22:37:49 +0200345};
346
françois romieuc0e45c12011-01-03 15:08:04 +0000347enum rtl8168_registers {
françois romieub646d902011-01-03 15:08:21 +0000348 ERIDR = 0x70,
349 ERIAR = 0x74,
350#define ERIAR_FLAG 0x80000000
351#define ERIAR_WRITE_CMD 0x80000000
352#define ERIAR_READ_CMD 0x00000000
353#define ERIAR_ADDR_BYTE_ALIGN 4
354#define ERIAR_EXGMAC 0
355#define ERIAR_MSIX 1
356#define ERIAR_ASF 2
357#define ERIAR_TYPE_SHIFT 16
358#define ERIAR_BYTEEN 0x0f
359#define ERIAR_BYTEEN_SHIFT 12
françois romieuc0e45c12011-01-03 15:08:04 +0000360 EPHY_RXER_NUM = 0x7c,
361 OCPDR = 0xb0, /* OCP GPHY access */
362#define OCPDR_WRITE_CMD 0x80000000
363#define OCPDR_READ_CMD 0x00000000
364#define OCPDR_REG_MASK 0x7f
365#define OCPDR_GPHY_REG_SHIFT 16
366#define OCPDR_DATA_MASK 0xffff
367 OCPAR = 0xb4,
368#define OCPAR_FLAG 0x80000000
369#define OCPAR_GPHY_WRITE_CMD 0x8000f060
370#define OCPAR_GPHY_READ_CMD 0x0000f060
hayeswang01dc7fe2011-03-21 01:50:28 +0000371 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
372 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200373#define TXPLA_RST (1 << 29)
françois romieuc0e45c12011-01-03 15:08:04 +0000374};
375
Francois Romieu07d3f512007-02-21 22:40:46 +0100376enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100378 SYSErr = 0x8000,
379 PCSTimeout = 0x4000,
380 SWInt = 0x0100,
381 TxDescUnavail = 0x0080,
382 RxFIFOOver = 0x0040,
383 LinkChg = 0x0020,
384 RxOverflow = 0x0010,
385 TxErr = 0x0008,
386 TxOK = 0x0004,
387 RxErr = 0x0002,
388 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 /* RxStatusDesc */
Francois Romieu9dccf612006-05-14 12:31:17 +0200391 RxFOVF = (1 << 23),
392 RxRWT = (1 << 22),
393 RxRES = (1 << 21),
394 RxRUNT = (1 << 20),
395 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 /* ChipCmdBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100398 CmdReset = 0x10,
399 CmdRxEnb = 0x08,
400 CmdTxEnb = 0x04,
401 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Francois Romieu275391a2007-02-23 23:50:28 +0100403 /* TXPoll register p.5 */
404 HPQ = 0x80, /* Poll cmd on the high prio queue */
405 NPQ = 0x40, /* Poll cmd on the low prio queue */
406 FSWInt = 0x01, /* Forced software interrupt */
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100409 Cfg9346_Lock = 0x00,
410 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100413 AcceptErr = 0x20,
414 AcceptRunt = 0x10,
415 AcceptBroadcast = 0x08,
416 AcceptMulticast = 0x04,
417 AcceptMyPhys = 0x02,
418 AcceptAllPhys = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 /* RxConfigBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100421 RxCfgFIFOShift = 13,
422 RxCfgDMAShift = 8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 /* TxConfigBits */
425 TxInterFrameGapShift = 24,
426 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
427
Francois Romieu5d06a992006-02-23 00:47:58 +0100428 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200429 LEDS1 = (1 << 7),
430 LEDS0 = (1 << 6),
Francois Romieufbac58f2007-10-04 22:51:38 +0200431 MSIEnable = (1 << 5), /* Enable Message Signaled Interrupt */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200432 Speed_down = (1 << 4),
433 MEMMAP = (1 << 3),
434 IOMAP = (1 << 2),
435 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100436 PMEnable = (1 << 0), /* Power Management Enable */
437
Francois Romieu6dccd162007-02-13 23:38:05 +0100438 /* Config2 register p. 25 */
439 PCI_Clock_66MHz = 0x01,
440 PCI_Clock_33MHz = 0x00,
441
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100442 /* Config3 register p.25 */
443 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
444 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200445 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100446
Francois Romieu5d06a992006-02-23 00:47:58 +0100447 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100448 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
449 MWF = (1 << 5), /* Accept Multicast wakeup frame */
450 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200451 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100452 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100453 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 /* TBICSR p.28 */
456 TBIReset = 0x80000000,
457 TBILoopback = 0x40000000,
458 TBINwEnable = 0x20000000,
459 TBINwRestart = 0x10000000,
460 TBILinkOk = 0x02000000,
461 TBINwComplete = 0x01000000,
462
463 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200464 EnableBist = (1 << 15), // 8168 8101
465 Mac_dbgo_oe = (1 << 14), // 8168 8101
466 Normal_mode = (1 << 13), // unused
467 Force_half_dup = (1 << 12), // 8168 8101
468 Force_rxflow_en = (1 << 11), // 8168 8101
469 Force_txflow_en = (1 << 10), // 8168 8101
470 Cxpl_dbg_sel = (1 << 9), // 8168 8101
471 ASF = (1 << 8), // 8168 8101
472 PktCntrDisable = (1 << 7), // 8168 8101
473 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 RxVlan = (1 << 6),
475 RxChkSum = (1 << 5),
476 PCIDAC = (1 << 4),
477 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100478 INTT_0 = 0x0000, // 8168
479 INTT_1 = 0x0001, // 8168
480 INTT_2 = 0x0002, // 8168
481 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100484 TBI_Enable = 0x80,
485 TxFlowCtrl = 0x40,
486 RxFlowCtrl = 0x20,
487 _1000bpsF = 0x10,
488 _100bps = 0x08,
489 _10bps = 0x04,
490 LinkStatus = 0x02,
491 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100494 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200495
496 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100497 CounterDump = 0x8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498};
499
Francois Romieu2b7b4312011-04-18 22:53:24 -0700500enum rtl_desc_bit {
501 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
503 RingEnd = (1 << 30), /* End of descriptor ring */
504 FirstFrag = (1 << 29), /* First segment of a packet */
505 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700506};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Francois Romieu2b7b4312011-04-18 22:53:24 -0700508/* Generic case. */
509enum rtl_tx_desc_bit {
510 /* First doubleword. */
511 TD_LSO = (1 << 27), /* Large Send Offload */
512#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Francois Romieu2b7b4312011-04-18 22:53:24 -0700514 /* Second doubleword. */
515 TxVlanTag = (1 << 17), /* Add VLAN tag */
516};
517
518/* 8169, 8168b and 810x except 8102e. */
519enum rtl_tx_desc_bit_0 {
520 /* First doubleword. */
521#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
522 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
523 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
524 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
525};
526
527/* 8102e, 8168c and beyond. */
528enum rtl_tx_desc_bit_1 {
529 /* Second doubleword. */
530#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
531 TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
532 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
533 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
534};
535
536static const struct rtl_tx_desc_info {
537 struct {
538 u32 udp;
539 u32 tcp;
540 } checksum;
541 u16 mss_shift;
542 u16 opts_offset;
543} tx_desc_info [] = {
544 [RTL_TD_0] = {
545 .checksum = {
546 .udp = TD0_IP_CS | TD0_UDP_CS,
547 .tcp = TD0_IP_CS | TD0_TCP_CS
548 },
549 .mss_shift = TD0_MSS_SHIFT,
550 .opts_offset = 0
551 },
552 [RTL_TD_1] = {
553 .checksum = {
554 .udp = TD1_IP_CS | TD1_UDP_CS,
555 .tcp = TD1_IP_CS | TD1_TCP_CS
556 },
557 .mss_shift = TD1_MSS_SHIFT,
558 .opts_offset = 1
559 }
560};
561
562enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 /* Rx private */
564 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
565 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
566
567#define RxProtoUDP (PID1)
568#define RxProtoTCP (PID0)
569#define RxProtoIP (PID1 | PID0)
570#define RxProtoMask RxProtoIP
571
572 IPFail = (1 << 16), /* IP checksum failed */
573 UDPFail = (1 << 15), /* UDP/IP checksum failed */
574 TCPFail = (1 << 14), /* TCP/IP checksum failed */
575 RxVlanTag = (1 << 16), /* VLAN tag available */
576};
577
578#define RsvdMask 0x3fffc000
579
580struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200581 __le32 opts1;
582 __le32 opts2;
583 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584};
585
586struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200587 __le32 opts1;
588 __le32 opts2;
589 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590};
591
592struct ring_info {
593 struct sk_buff *skb;
594 u32 len;
595 u8 __pad[sizeof(void *) - sizeof(u32)];
596};
597
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200598enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200599 RTL_FEATURE_WOL = (1 << 0),
600 RTL_FEATURE_MSI = (1 << 1),
601 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200602};
603
Ivan Vecera355423d2009-02-06 21:49:57 -0800604struct rtl8169_counters {
605 __le64 tx_packets;
606 __le64 rx_packets;
607 __le64 tx_errors;
608 __le32 rx_errors;
609 __le16 rx_missed;
610 __le16 align_errors;
611 __le32 tx_one_collision;
612 __le32 tx_multi_collision;
613 __le64 rx_unicast;
614 __le64 rx_broadcast;
615 __le32 rx_multicast;
616 __le16 tx_aborted;
617 __le16 tx_underun;
618};
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620struct rtl8169_private {
621 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200622 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000623 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700624 struct napi_struct napi;
Francois Romieucecb5fd2011-04-01 10:21:07 +0200625 spinlock_t lock;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200626 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700627 u16 txd_version;
628 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
630 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
631 u32 dirty_rx;
632 u32 dirty_tx;
633 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
634 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
635 dma_addr_t TxPhyAddr;
636 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000637 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 struct timer_list timer;
640 u16 cp_cmd;
Francois Romieu0e485152007-02-20 00:00:26 +0100641 u16 intr_event;
642 u16 napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 u16 intr_mask;
françois romieuc0e45c12011-01-03 15:08:04 +0000644
645 struct mdio_ops {
646 void (*write)(void __iomem *, int, int);
647 int (*read)(void __iomem *, int);
648 } mdio_ops;
649
françois romieu065c27c2011-01-03 15:08:12 +0000650 struct pll_power_ops {
651 void (*down)(struct rtl8169_private *);
652 void (*up)(struct rtl8169_private *);
653 } pll_power_ops;
654
Oliver Neukum54405cd2011-01-06 21:55:13 +0100655 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Francois Romieuccdffb92008-07-26 14:26:06 +0200656 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000657 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100658 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000659 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800661 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
Francois Romieu9c14cea2008-07-05 00:21:15 +0200662 int pcie_cap;
David Howellsc4028952006-11-22 14:57:56 +0000663 struct delayed_work task;
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200664 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200665
666 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800667 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000668 u32 saved_wolopts;
françois romieuf1e02ed2011-01-13 13:07:53 +0000669
670 const struct firmware *fw;
François Romieu953a12c2011-04-24 17:38:48 +0200671#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672};
673
Ralf Baechle979b6c12005-06-13 14:30:40 -0700674MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700677MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200678module_param_named(debug, debug.msg_enable, int, 0);
679MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680MODULE_LICENSE("GPL");
681MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000682MODULE_FIRMWARE(FIRMWARE_8168D_1);
683MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000684MODULE_FIRMWARE(FIRMWARE_8168E_1);
685MODULE_FIRMWARE(FIRMWARE_8168E_2);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800686MODULE_FIRMWARE(FIRMWARE_8105E_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688static int rtl8169_open(struct net_device *dev);
Stephen Hemminger613573252009-08-31 19:50:58 +0000689static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
690 struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100691static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692static int rtl8169_init_ring(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100693static void rtl_hw_start(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694static int rtl8169_close(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100695static void rtl_set_rx_mode(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696static void rtl8169_tx_timeout(struct net_device *dev);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200697static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698static int rtl8169_rx_interrupt(struct net_device *, struct rtl8169_private *,
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700699 void __iomem *, u32 budget);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200700static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701static void rtl8169_down(struct net_device *dev);
Francois Romieu99f252b2007-04-02 22:59:59 +0200702static void rtl8169_rx_clear(struct rtl8169_private *tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700703static int rtl8169_poll(struct napi_struct *napi, int budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705static const unsigned int rtl8169_rx_config =
Francois Romieu5b0384f2006-08-16 16:00:01 +0200706 (RX_FIFO_THRESH << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
françois romieub646d902011-01-03 15:08:21 +0000708static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
709{
710 void __iomem *ioaddr = tp->mmio_addr;
711 int i;
712
713 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
714 for (i = 0; i < 20; i++) {
715 udelay(100);
716 if (RTL_R32(OCPAR) & OCPAR_FLAG)
717 break;
718 }
719 return RTL_R32(OCPDR);
720}
721
722static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
723{
724 void __iomem *ioaddr = tp->mmio_addr;
725 int i;
726
727 RTL_W32(OCPDR, data);
728 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
729 for (i = 0; i < 20; i++) {
730 udelay(100);
731 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
732 break;
733 }
734}
735
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800736static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000737{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800738 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000739 int i;
740
741 RTL_W8(ERIDR, cmd);
742 RTL_W32(ERIAR, 0x800010e8);
743 msleep(2);
744 for (i = 0; i < 5; i++) {
745 udelay(100);
Francois Romieu1e4e82b2011-06-24 19:52:13 +0200746 if (!(RTL_R32(ERIAR) & ERIAR_FLAG))
françois romieub646d902011-01-03 15:08:21 +0000747 break;
748 }
749
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800750 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000751}
752
753#define OOB_CMD_RESET 0x00
754#define OOB_CMD_DRIVER_START 0x05
755#define OOB_CMD_DRIVER_STOP 0x06
756
Francois Romieucecb5fd2011-04-01 10:21:07 +0200757static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
758{
759 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
760}
761
françois romieub646d902011-01-03 15:08:21 +0000762static void rtl8168_driver_start(struct rtl8169_private *tp)
763{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200764 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000765 int i;
766
767 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
768
Francois Romieucecb5fd2011-04-01 10:21:07 +0200769 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000770
françois romieub646d902011-01-03 15:08:21 +0000771 for (i = 0; i < 10; i++) {
772 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000773 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
françois romieub646d902011-01-03 15:08:21 +0000774 break;
775 }
776}
777
778static void rtl8168_driver_stop(struct rtl8169_private *tp)
779{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200780 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000781 int i;
782
783 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
784
Francois Romieucecb5fd2011-04-01 10:21:07 +0200785 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000786
françois romieub646d902011-01-03 15:08:21 +0000787 for (i = 0; i < 10; i++) {
788 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000789 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
françois romieub646d902011-01-03 15:08:21 +0000790 break;
791 }
792}
793
hayeswang4804b3b2011-03-21 01:50:29 +0000794static int r8168dp_check_dash(struct rtl8169_private *tp)
795{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200796 u16 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000797
Francois Romieucecb5fd2011-04-01 10:21:07 +0200798 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
hayeswang4804b3b2011-03-21 01:50:29 +0000799}
françois romieub646d902011-01-03 15:08:21 +0000800
françois romieu4da19632011-01-03 15:07:55 +0000801static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
803 int i;
804
Francois Romieua6baf3a2007-11-08 23:23:21 +0100805 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Francois Romieu23714082006-01-29 00:49:09 +0100807 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100808 /*
809 * Check if the RTL8169 has completed writing to the specified
810 * MII register.
811 */
Francois Romieu5b0384f2006-08-16 16:00:01 +0200812 if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 break;
Francois Romieu23714082006-01-29 00:49:09 +0100814 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
Timo Teräs024a07b2010-06-06 15:38:47 -0700816 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700817 * According to hardware specs a 20us delay is required after write
818 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700819 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700820 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
822
françois romieu4da19632011-01-03 15:07:55 +0000823static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
825 int i, value = -1;
826
Francois Romieua6baf3a2007-11-08 23:23:21 +0100827 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Francois Romieu23714082006-01-29 00:49:09 +0100829 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100830 /*
831 * Check if the RTL8169 has completed retrieving data from
832 * the specified MII register.
833 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if (RTL_R32(PHYAR) & 0x80000000) {
Francois Romieua6baf3a2007-11-08 23:23:21 +0100835 value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 break;
837 }
Francois Romieu23714082006-01-29 00:49:09 +0100838 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
Timo Teräs81a95f02010-06-09 17:31:48 -0700840 /*
841 * According to hardware specs a 20us delay is required after read
842 * complete indication, but before sending next command.
843 */
844 udelay(20);
845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return value;
847}
848
françois romieuc0e45c12011-01-03 15:08:04 +0000849static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
850{
851 int i;
852
853 RTL_W32(OCPDR, data |
854 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
855 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
856 RTL_W32(EPHY_RXER_NUM, 0);
857
858 for (i = 0; i < 100; i++) {
859 mdelay(1);
860 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
861 break;
862 }
863}
864
865static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
866{
867 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
868 (value & OCPDR_DATA_MASK));
869}
870
871static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
872{
873 int i;
874
875 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
876
877 mdelay(1);
878 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
879 RTL_W32(EPHY_RXER_NUM, 0);
880
881 for (i = 0; i < 100; i++) {
882 mdelay(1);
883 if (RTL_R32(OCPAR) & OCPAR_FLAG)
884 break;
885 }
886
887 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
888}
889
françois romieue6de30d2011-01-03 15:08:37 +0000890#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
891
892static void r8168dp_2_mdio_start(void __iomem *ioaddr)
893{
894 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
895}
896
897static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
898{
899 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
900}
901
902static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
903{
904 r8168dp_2_mdio_start(ioaddr);
905
906 r8169_mdio_write(ioaddr, reg_addr, value);
907
908 r8168dp_2_mdio_stop(ioaddr);
909}
910
911static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
912{
913 int value;
914
915 r8168dp_2_mdio_start(ioaddr);
916
917 value = r8169_mdio_read(ioaddr, reg_addr);
918
919 r8168dp_2_mdio_stop(ioaddr);
920
921 return value;
922}
923
françois romieu4da19632011-01-03 15:07:55 +0000924static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +0200925{
françois romieuc0e45c12011-01-03 15:08:04 +0000926 tp->mdio_ops.write(tp->mmio_addr, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +0200927}
928
françois romieu4da19632011-01-03 15:07:55 +0000929static int rtl_readphy(struct rtl8169_private *tp, int location)
930{
françois romieuc0e45c12011-01-03 15:08:04 +0000931 return tp->mdio_ops.read(tp->mmio_addr, location);
françois romieu4da19632011-01-03 15:07:55 +0000932}
933
934static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
935{
936 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
937}
938
939static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +0000940{
941 int val;
942
françois romieu4da19632011-01-03 15:07:55 +0000943 val = rtl_readphy(tp, reg_addr);
944 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +0000945}
946
Francois Romieuccdffb92008-07-26 14:26:06 +0200947static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
948 int val)
949{
950 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +0200951
françois romieu4da19632011-01-03 15:07:55 +0000952 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +0200953}
954
955static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
956{
957 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +0200958
françois romieu4da19632011-01-03 15:07:55 +0000959 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +0200960}
961
Francois Romieudacf8152008-08-02 20:44:13 +0200962static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
963{
964 unsigned int i;
965
966 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
967 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
968
969 for (i = 0; i < 100; i++) {
970 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
971 break;
972 udelay(10);
973 }
974}
975
976static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
977{
978 u16 value = 0xffff;
979 unsigned int i;
980
981 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
982
983 for (i = 0; i < 100; i++) {
984 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
985 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
986 break;
987 }
988 udelay(10);
989 }
990
991 return value;
992}
993
994static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
995{
996 unsigned int i;
997
998 RTL_W32(CSIDR, value);
999 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
1000 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1001
1002 for (i = 0; i < 100; i++) {
1003 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
1004 break;
1005 udelay(10);
1006 }
1007}
1008
1009static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
1010{
1011 u32 value = ~0x00;
1012 unsigned int i;
1013
1014 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
1015 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1016
1017 for (i = 0; i < 100; i++) {
1018 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
1019 value = RTL_R32(CSIDR);
1020 break;
1021 }
1022 udelay(10);
1023 }
1024
1025 return value;
1026}
1027
françois romieudaf9df62009-10-07 12:44:20 +00001028static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1029{
1030 u8 value = 0xff;
1031 unsigned int i;
1032
1033 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1034
1035 for (i = 0; i < 300; i++) {
1036 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1037 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1038 break;
1039 }
1040 udelay(100);
1041 }
1042
1043 return value;
1044}
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046static void rtl8169_irq_mask_and_ack(void __iomem *ioaddr)
1047{
1048 RTL_W16(IntrMask, 0x0000);
1049
1050 RTL_W16(IntrStatus, 0xffff);
1051}
1052
1053static void rtl8169_asic_down(void __iomem *ioaddr)
1054{
1055 RTL_W8(ChipCmd, 0x00);
1056 rtl8169_irq_mask_and_ack(ioaddr);
1057 RTL_R16(CPlusCmd);
1058}
1059
françois romieu4da19632011-01-03 15:07:55 +00001060static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
françois romieu4da19632011-01-03 15:07:55 +00001062 void __iomem *ioaddr = tp->mmio_addr;
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return RTL_R32(TBICSR) & TBIReset;
1065}
1066
françois romieu4da19632011-01-03 15:07:55 +00001067static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
françois romieu4da19632011-01-03 15:07:55 +00001069 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070}
1071
1072static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1073{
1074 return RTL_R32(TBICSR) & TBILinkOk;
1075}
1076
1077static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1078{
1079 return RTL_R8(PHYstatus) & LinkStatus;
1080}
1081
françois romieu4da19632011-01-03 15:07:55 +00001082static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
françois romieu4da19632011-01-03 15:07:55 +00001084 void __iomem *ioaddr = tp->mmio_addr;
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1087}
1088
françois romieu4da19632011-01-03 15:07:55 +00001089static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
1091 unsigned int val;
1092
françois romieu4da19632011-01-03 15:07:55 +00001093 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1094 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095}
1096
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001097static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001098 struct rtl8169_private *tp,
1099 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100{
1101 unsigned long flags;
1102
1103 spin_lock_irqsave(&tp->lock, flags);
1104 if (tp->link_ok(ioaddr)) {
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001105 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001106 if (pm)
1107 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001109 if (net_ratelimit())
1110 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001111 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001113 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001114 if (pm)
1115 pm_schedule_suspend(&tp->pci_dev->dev, 100);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 spin_unlock_irqrestore(&tp->lock, flags);
1118}
1119
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001120static void rtl8169_check_link_status(struct net_device *dev,
1121 struct rtl8169_private *tp,
1122 void __iomem *ioaddr)
1123{
1124 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1125}
1126
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001127#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1128
1129static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1130{
1131 void __iomem *ioaddr = tp->mmio_addr;
1132 u8 options;
1133 u32 wolopts = 0;
1134
1135 options = RTL_R8(Config1);
1136 if (!(options & PMEnable))
1137 return 0;
1138
1139 options = RTL_R8(Config3);
1140 if (options & LinkUp)
1141 wolopts |= WAKE_PHY;
1142 if (options & MagicPacket)
1143 wolopts |= WAKE_MAGIC;
1144
1145 options = RTL_R8(Config5);
1146 if (options & UWF)
1147 wolopts |= WAKE_UCAST;
1148 if (options & BWF)
1149 wolopts |= WAKE_BCAST;
1150 if (options & MWF)
1151 wolopts |= WAKE_MCAST;
1152
1153 return wolopts;
1154}
1155
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001156static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1157{
1158 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001159
1160 spin_lock_irq(&tp->lock);
1161
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001162 wol->supported = WAKE_ANY;
1163 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001164
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001165 spin_unlock_irq(&tp->lock);
1166}
1167
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001168static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001169{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001170 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001171 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001172 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001173 u32 opt;
1174 u16 reg;
1175 u8 mask;
1176 } cfg[] = {
1177 { WAKE_ANY, Config1, PMEnable },
1178 { WAKE_PHY, Config3, LinkUp },
1179 { WAKE_MAGIC, Config3, MagicPacket },
1180 { WAKE_UCAST, Config5, UWF },
1181 { WAKE_BCAST, Config5, BWF },
1182 { WAKE_MCAST, Config5, MWF },
1183 { WAKE_ANY, Config5, LanWake }
1184 };
1185
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001186 RTL_W8(Cfg9346, Cfg9346_Unlock);
1187
1188 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1189 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001190 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001191 options |= cfg[i].mask;
1192 RTL_W8(cfg[i].reg, options);
1193 }
1194
1195 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001196}
1197
1198static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1199{
1200 struct rtl8169_private *tp = netdev_priv(dev);
1201
1202 spin_lock_irq(&tp->lock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001203
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001204 if (wol->wolopts)
1205 tp->features |= RTL_FEATURE_WOL;
1206 else
1207 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001208 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001209 spin_unlock_irq(&tp->lock);
1210
françois romieuea809072010-11-08 13:23:58 +00001211 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1212
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001213 return 0;
1214}
1215
Francois Romieu31bd2042011-04-26 18:58:59 +02001216static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1217{
Francois Romieu85bffe62011-04-27 08:22:39 +02001218 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001219}
1220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221static void rtl8169_get_drvinfo(struct net_device *dev,
1222 struct ethtool_drvinfo *info)
1223{
1224 struct rtl8169_private *tp = netdev_priv(dev);
1225
1226 strcpy(info->driver, MODULENAME);
1227 strcpy(info->version, RTL8169_VERSION);
1228 strcpy(info->bus_info, pci_name(tp->pci_dev));
Francois Romieu31bd2042011-04-26 18:58:59 +02001229 strncpy(info->fw_version, IS_ERR_OR_NULL(tp->fw) ? "N/A" :
1230 rtl_lookup_firmware_name(tp), sizeof(info->fw_version) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231}
1232
1233static int rtl8169_get_regs_len(struct net_device *dev)
1234{
1235 return R8169_REGS_SIZE;
1236}
1237
1238static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001239 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
1241 struct rtl8169_private *tp = netdev_priv(dev);
1242 void __iomem *ioaddr = tp->mmio_addr;
1243 int ret = 0;
1244 u32 reg;
1245
1246 reg = RTL_R32(TBICSR);
1247 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1248 (duplex == DUPLEX_FULL)) {
1249 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1250 } else if (autoneg == AUTONEG_ENABLE)
1251 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1252 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001253 netif_warn(tp, link, dev,
1254 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 ret = -EOPNOTSUPP;
1256 }
1257
1258 return ret;
1259}
1260
1261static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001262 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
1264 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001265 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001266 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Hayes Wang716b50a2011-02-22 17:26:18 +08001268 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001271 int auto_nego;
1272
françois romieu4da19632011-01-03 15:07:55 +00001273 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001274 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1275 ADVERTISE_100HALF | ADVERTISE_100FULL);
1276
1277 if (adv & ADVERTISED_10baseT_Half)
1278 auto_nego |= ADVERTISE_10HALF;
1279 if (adv & ADVERTISED_10baseT_Full)
1280 auto_nego |= ADVERTISE_10FULL;
1281 if (adv & ADVERTISED_100baseT_Half)
1282 auto_nego |= ADVERTISE_100HALF;
1283 if (adv & ADVERTISED_100baseT_Full)
1284 auto_nego |= ADVERTISE_100FULL;
1285
françois romieu3577aa12009-05-19 10:46:48 +00001286 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1287
françois romieu4da19632011-01-03 15:07:55 +00001288 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001289 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1290
1291 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001292 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001293 if (adv & ADVERTISED_1000baseT_Half)
1294 giga_ctrl |= ADVERTISE_1000HALF;
1295 if (adv & ADVERTISED_1000baseT_Full)
1296 giga_ctrl |= ADVERTISE_1000FULL;
1297 } else if (adv & (ADVERTISED_1000baseT_Half |
1298 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001299 netif_info(tp, link, dev,
1300 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001301 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
françois romieu3577aa12009-05-19 10:46:48 +00001304 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001305
françois romieu4da19632011-01-03 15:07:55 +00001306 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1307 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001308 } else {
1309 giga_ctrl = 0;
1310
1311 if (speed == SPEED_10)
1312 bmcr = 0;
1313 else if (speed == SPEED_100)
1314 bmcr = BMCR_SPEED100;
1315 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001316 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001317
1318 if (duplex == DUPLEX_FULL)
1319 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001320 }
1321
françois romieu4da19632011-01-03 15:07:55 +00001322 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001323
Francois Romieucecb5fd2011-04-01 10:21:07 +02001324 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1325 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001326 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001327 rtl_writephy(tp, 0x17, 0x2138);
1328 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001329 } else {
françois romieu4da19632011-01-03 15:07:55 +00001330 rtl_writephy(tp, 0x17, 0x2108);
1331 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001332 }
1333 }
1334
Oliver Neukum54405cd2011-01-06 21:55:13 +01001335 rc = 0;
1336out:
1337 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338}
1339
1340static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001341 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
1343 struct rtl8169_private *tp = netdev_priv(dev);
1344 int ret;
1345
Oliver Neukum54405cd2011-01-06 21:55:13 +01001346 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001347 if (ret < 0)
1348 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Francois Romieu4876cc12011-03-11 21:07:11 +01001350 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1351 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001353 }
1354out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 return ret;
1356}
1357
1358static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1359{
1360 struct rtl8169_private *tp = netdev_priv(dev);
1361 unsigned long flags;
1362 int ret;
1363
Francois Romieu4876cc12011-03-11 21:07:11 +01001364 del_timer_sync(&tp->timer);
1365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 spin_lock_irqsave(&tp->lock, flags);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001367 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001368 cmd->duplex, cmd->advertising);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 return ret;
1372}
1373
Michał Mirosław350fb322011-04-08 06:35:56 +00001374static u32 rtl8169_fix_features(struct net_device *dev, u32 features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
Francois Romieu2b7b4312011-04-18 22:53:24 -07001376 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001377 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Michał Mirosław350fb322011-04-08 06:35:56 +00001379 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380}
1381
Michał Mirosław350fb322011-04-08 06:35:56 +00001382static int rtl8169_set_features(struct net_device *dev, u32 features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383{
1384 struct rtl8169_private *tp = netdev_priv(dev);
1385 void __iomem *ioaddr = tp->mmio_addr;
1386 unsigned long flags;
1387
1388 spin_lock_irqsave(&tp->lock, flags);
1389
Michał Mirosław350fb322011-04-08 06:35:56 +00001390 if (features & NETIF_F_RXCSUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 tp->cp_cmd |= RxChkSum;
1392 else
1393 tp->cp_cmd &= ~RxChkSum;
1394
Michał Mirosław350fb322011-04-08 06:35:56 +00001395 if (dev->features & NETIF_F_HW_VLAN_RX)
1396 tp->cp_cmd |= RxVlan;
1397 else
1398 tp->cp_cmd &= ~RxVlan;
1399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 RTL_W16(CPlusCmd, tp->cp_cmd);
1401 RTL_R16(CPlusCmd);
1402
1403 spin_unlock_irqrestore(&tp->lock, flags);
1404
1405 return 0;
1406}
1407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1409 struct sk_buff *skb)
1410{
Jesse Grosseab6d182010-10-20 13:56:03 +00001411 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1413}
1414
Francois Romieu7a8fc772011-03-01 17:18:33 +01001415static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
1417 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Francois Romieu7a8fc772011-03-01 17:18:33 +01001419 if (opts2 & RxVlanTag)
1420 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423}
1424
Francois Romieuccdffb92008-07-26 14:26:06 +02001425static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
1427 struct rtl8169_private *tp = netdev_priv(dev);
1428 void __iomem *ioaddr = tp->mmio_addr;
1429 u32 status;
1430
1431 cmd->supported =
1432 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1433 cmd->port = PORT_FIBRE;
1434 cmd->transceiver = XCVR_INTERNAL;
1435
1436 status = RTL_R32(TBICSR);
1437 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1438 cmd->autoneg = !!(status & TBINwEnable);
1439
David Decotigny70739492011-04-27 18:32:40 +00001440 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001442
1443 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444}
1445
Francois Romieuccdffb92008-07-26 14:26:06 +02001446static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
1448 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Francois Romieuccdffb92008-07-26 14:26:06 +02001450 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451}
1452
1453static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1454{
1455 struct rtl8169_private *tp = netdev_priv(dev);
1456 unsigned long flags;
Francois Romieuccdffb92008-07-26 14:26:06 +02001457 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
1459 spin_lock_irqsave(&tp->lock, flags);
1460
Francois Romieuccdffb92008-07-26 14:26:06 +02001461 rc = tp->get_settings(dev, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieuccdffb92008-07-26 14:26:06 +02001464 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465}
1466
1467static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1468 void *p)
1469{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001470 struct rtl8169_private *tp = netdev_priv(dev);
1471 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Francois Romieu5b0384f2006-08-16 16:00:01 +02001473 if (regs->len > R8169_REGS_SIZE)
1474 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Francois Romieu5b0384f2006-08-16 16:00:01 +02001476 spin_lock_irqsave(&tp->lock, flags);
1477 memcpy_fromio(p, tp->mmio_addr, regs->len);
1478 spin_unlock_irqrestore(&tp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479}
1480
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001481static u32 rtl8169_get_msglevel(struct net_device *dev)
1482{
1483 struct rtl8169_private *tp = netdev_priv(dev);
1484
1485 return tp->msg_enable;
1486}
1487
1488static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1489{
1490 struct rtl8169_private *tp = netdev_priv(dev);
1491
1492 tp->msg_enable = value;
1493}
1494
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001495static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1496 "tx_packets",
1497 "rx_packets",
1498 "tx_errors",
1499 "rx_errors",
1500 "rx_missed",
1501 "align_errors",
1502 "tx_single_collisions",
1503 "tx_multi_collisions",
1504 "unicast",
1505 "broadcast",
1506 "multicast",
1507 "tx_aborted",
1508 "tx_underrun",
1509};
1510
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001511static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001512{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001513 switch (sset) {
1514 case ETH_SS_STATS:
1515 return ARRAY_SIZE(rtl8169_gstrings);
1516 default:
1517 return -EOPNOTSUPP;
1518 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001519}
1520
Ivan Vecera355423d2009-02-06 21:49:57 -08001521static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001522{
1523 struct rtl8169_private *tp = netdev_priv(dev);
1524 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001525 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001526 struct rtl8169_counters *counters;
1527 dma_addr_t paddr;
1528 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001529 int wait = 1000;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001530
Ivan Vecera355423d2009-02-06 21:49:57 -08001531 /*
1532 * Some chips are unable to dump tally counters when the receiver
1533 * is disabled.
1534 */
1535 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1536 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001537
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001538 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001539 if (!counters)
1540 return;
1541
1542 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001543 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001544 RTL_W32(CounterAddrLow, cmd);
1545 RTL_W32(CounterAddrLow, cmd | CounterDump);
1546
Ivan Vecera355423d2009-02-06 21:49:57 -08001547 while (wait--) {
1548 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
Ivan Vecera355423d2009-02-06 21:49:57 -08001549 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001550 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001551 }
1552 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001553 }
1554
1555 RTL_W32(CounterAddrLow, 0);
1556 RTL_W32(CounterAddrHigh, 0);
1557
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001558 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001559}
1560
Ivan Vecera355423d2009-02-06 21:49:57 -08001561static void rtl8169_get_ethtool_stats(struct net_device *dev,
1562 struct ethtool_stats *stats, u64 *data)
1563{
1564 struct rtl8169_private *tp = netdev_priv(dev);
1565
1566 ASSERT_RTNL();
1567
1568 rtl8169_update_counters(dev);
1569
1570 data[0] = le64_to_cpu(tp->counters.tx_packets);
1571 data[1] = le64_to_cpu(tp->counters.rx_packets);
1572 data[2] = le64_to_cpu(tp->counters.tx_errors);
1573 data[3] = le32_to_cpu(tp->counters.rx_errors);
1574 data[4] = le16_to_cpu(tp->counters.rx_missed);
1575 data[5] = le16_to_cpu(tp->counters.align_errors);
1576 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1577 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1578 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1579 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1580 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1581 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1582 data[12] = le16_to_cpu(tp->counters.tx_underun);
1583}
1584
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001585static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1586{
1587 switch(stringset) {
1588 case ETH_SS_STATS:
1589 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1590 break;
1591 }
1592}
1593
Jeff Garzik7282d492006-09-13 14:30:00 -04001594static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 .get_drvinfo = rtl8169_get_drvinfo,
1596 .get_regs_len = rtl8169_get_regs_len,
1597 .get_link = ethtool_op_get_link,
1598 .get_settings = rtl8169_get_settings,
1599 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001600 .get_msglevel = rtl8169_get_msglevel,
1601 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001603 .get_wol = rtl8169_get_wol,
1604 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001605 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001606 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001607 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608};
1609
Francois Romieu07d3f512007-02-21 22:40:46 +01001610static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02001611 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612{
Francois Romieu5d320a22011-05-08 17:47:36 +02001613 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01001614 /*
1615 * The driver currently handles the 8168Bf and the 8168Be identically
1616 * but they can be identified more specifically through the test below
1617 * if needed:
1618 *
1619 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001620 *
1621 * Same thing for the 8101Eb and the 8101Ec:
1622 *
1623 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001624 */
Francois Romieu37441002011-06-17 22:58:54 +02001625 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001627 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 int mac_version;
1629 } mac_info[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00001630 /* 8168E family. */
1631 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1632 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1633 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1634
Francois Romieu5b538df2008-07-20 16:22:45 +02001635 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001636 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1637 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001638 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001639
françois romieue6de30d2011-01-03 15:08:37 +00001640 /* 8168DP family. */
1641 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1642 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00001643 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00001644
Francois Romieuef808d52008-06-29 13:10:54 +02001645 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001646 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001647 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001648 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001649 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001650 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1651 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001652 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001653 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001654 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001655
1656 /* 8168B family. */
1657 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1658 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1659 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1660 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1661
1662 /* 8101 family. */
hayeswang36a0e6c2011-03-21 01:50:30 +00001663 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08001664 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1665 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1666 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001667 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1668 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1669 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1670 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1671 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1672 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001673 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001674 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001675 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001676 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1677 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001678 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1679 /* FIXME: where did these entries come from ? -- FR */
1680 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1681 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1682
1683 /* 8110 family. */
1684 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1685 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1686 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1687 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1688 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1689 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1690
Jean Delvaref21b75e2009-05-26 20:54:48 -07001691 /* Catch-all */
1692 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02001693 };
1694 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 u32 reg;
1696
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001697 reg = RTL_R32(TxConfig);
1698 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 p++;
1700 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02001701
1702 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1703 netif_notice(tp, probe, dev,
1704 "unknown MAC, using family default\n");
1705 tp->mac_version = default_version;
1706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707}
1708
1709static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1710{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001711 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712}
1713
Francois Romieu867763c2007-08-17 18:21:58 +02001714struct phy_reg {
1715 u16 reg;
1716 u16 val;
1717};
1718
françois romieu4da19632011-01-03 15:07:55 +00001719static void rtl_writephy_batch(struct rtl8169_private *tp,
1720 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02001721{
1722 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00001723 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02001724 regs++;
1725 }
1726}
1727
françois romieubca03d52011-01-03 15:07:31 +00001728#define PHY_READ 0x00000000
1729#define PHY_DATA_OR 0x10000000
1730#define PHY_DATA_AND 0x20000000
1731#define PHY_BJMPN 0x30000000
1732#define PHY_READ_EFUSE 0x40000000
1733#define PHY_READ_MAC_BYTE 0x50000000
1734#define PHY_WRITE_MAC_BYTE 0x60000000
1735#define PHY_CLEAR_READCOUNT 0x70000000
1736#define PHY_WRITE 0x80000000
1737#define PHY_READCOUNT_EQ_SKIP 0x90000000
1738#define PHY_COMP_EQ_SKIPN 0xa0000000
1739#define PHY_COMP_NEQ_SKIPN 0xb0000000
1740#define PHY_WRITE_PREVIOUS 0xc0000000
1741#define PHY_SKIPN 0xd0000000
1742#define PHY_DELAY_MS 0xe0000000
1743#define PHY_WRITE_ERI_WORD 0xf0000000
1744
1745static void
1746rtl_phy_write_fw(struct rtl8169_private *tp, const struct firmware *fw)
1747{
françois romieubca03d52011-01-03 15:07:31 +00001748 __le32 *phytable = (__le32 *)fw->data;
1749 struct net_device *dev = tp->dev;
hayeswang42b82dc2011-01-10 02:07:25 +00001750 size_t index, fw_size = fw->size / sizeof(*phytable);
1751 u32 predata, count;
françois romieubca03d52011-01-03 15:07:31 +00001752
1753 if (fw->size % sizeof(*phytable)) {
1754 netif_err(tp, probe, dev, "odd sized firmware %zd\n", fw->size);
1755 return;
1756 }
1757
hayeswang42b82dc2011-01-10 02:07:25 +00001758 for (index = 0; index < fw_size; index++) {
1759 u32 action = le32_to_cpu(phytable[index]);
1760 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00001761
hayeswang42b82dc2011-01-10 02:07:25 +00001762 switch(action & 0xf0000000) {
1763 case PHY_READ:
1764 case PHY_DATA_OR:
1765 case PHY_DATA_AND:
1766 case PHY_READ_EFUSE:
1767 case PHY_CLEAR_READCOUNT:
1768 case PHY_WRITE:
1769 case PHY_WRITE_PREVIOUS:
1770 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00001771 break;
1772
hayeswang42b82dc2011-01-10 02:07:25 +00001773 case PHY_BJMPN:
1774 if (regno > index) {
1775 netif_err(tp, probe, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001776 "Out of range of firmware\n");
hayeswang42b82dc2011-01-10 02:07:25 +00001777 return;
1778 }
1779 break;
1780 case PHY_READCOUNT_EQ_SKIP:
1781 if (index + 2 >= fw_size) {
1782 netif_err(tp, probe, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001783 "Out of range of firmware\n");
hayeswang42b82dc2011-01-10 02:07:25 +00001784 return;
1785 }
1786 break;
1787 case PHY_COMP_EQ_SKIPN:
1788 case PHY_COMP_NEQ_SKIPN:
1789 case PHY_SKIPN:
1790 if (index + 1 + regno >= fw_size) {
1791 netif_err(tp, probe, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001792 "Out of range of firmware\n");
hayeswang42b82dc2011-01-10 02:07:25 +00001793 return;
1794 }
1795 break;
1796
1797 case PHY_READ_MAC_BYTE:
1798 case PHY_WRITE_MAC_BYTE:
1799 case PHY_WRITE_ERI_WORD:
1800 default:
1801 netif_err(tp, probe, tp->dev,
1802 "Invalid action 0x%08x\n", action);
françois romieubca03d52011-01-03 15:07:31 +00001803 return;
1804 }
1805 }
1806
hayeswang42b82dc2011-01-10 02:07:25 +00001807 predata = 0;
1808 count = 0;
1809
1810 for (index = 0; index < fw_size; ) {
1811 u32 action = le32_to_cpu(phytable[index]);
françois romieubca03d52011-01-03 15:07:31 +00001812 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00001813 u32 regno = (action & 0x0fff0000) >> 16;
1814
1815 if (!action)
1816 break;
françois romieubca03d52011-01-03 15:07:31 +00001817
1818 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00001819 case PHY_READ:
1820 predata = rtl_readphy(tp, regno);
1821 count++;
1822 index++;
françois romieubca03d52011-01-03 15:07:31 +00001823 break;
hayeswang42b82dc2011-01-10 02:07:25 +00001824 case PHY_DATA_OR:
1825 predata |= data;
1826 index++;
1827 break;
1828 case PHY_DATA_AND:
1829 predata &= data;
1830 index++;
1831 break;
1832 case PHY_BJMPN:
1833 index -= regno;
1834 break;
1835 case PHY_READ_EFUSE:
1836 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
1837 index++;
1838 break;
1839 case PHY_CLEAR_READCOUNT:
1840 count = 0;
1841 index++;
1842 break;
1843 case PHY_WRITE:
1844 rtl_writephy(tp, regno, data);
1845 index++;
1846 break;
1847 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02001848 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00001849 break;
1850 case PHY_COMP_EQ_SKIPN:
1851 if (predata == data)
1852 index += regno;
1853 index++;
1854 break;
1855 case PHY_COMP_NEQ_SKIPN:
1856 if (predata != data)
1857 index += regno;
1858 index++;
1859 break;
1860 case PHY_WRITE_PREVIOUS:
1861 rtl_writephy(tp, regno, predata);
1862 index++;
1863 break;
1864 case PHY_SKIPN:
1865 index += regno + 1;
1866 break;
1867 case PHY_DELAY_MS:
1868 mdelay(data);
1869 index++;
1870 break;
1871
1872 case PHY_READ_MAC_BYTE:
1873 case PHY_WRITE_MAC_BYTE:
1874 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00001875 default:
1876 BUG();
1877 }
1878 }
1879}
1880
françois romieuf1e02ed2011-01-13 13:07:53 +00001881static void rtl_release_firmware(struct rtl8169_private *tp)
1882{
François Romieu953a12c2011-04-24 17:38:48 +02001883 if (!IS_ERR_OR_NULL(tp->fw))
1884 release_firmware(tp->fw);
1885 tp->fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00001886}
1887
François Romieu953a12c2011-04-24 17:38:48 +02001888static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00001889{
François Romieu953a12c2011-04-24 17:38:48 +02001890 const struct firmware *fw = tp->fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00001891
1892 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
François Romieu953a12c2011-04-24 17:38:48 +02001893 if (!IS_ERR_OR_NULL(fw))
1894 rtl_phy_write_fw(tp, fw);
1895}
1896
1897static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
1898{
1899 if (rtl_readphy(tp, reg) != val)
1900 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
1901 else
1902 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00001903}
1904
françois romieu4da19632011-01-03 15:07:55 +00001905static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001907 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00001908 { 0x1f, 0x0001 },
1909 { 0x06, 0x006e },
1910 { 0x08, 0x0708 },
1911 { 0x15, 0x4000 },
1912 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
françois romieu0b9b5712009-08-10 19:44:56 +00001914 { 0x1f, 0x0001 },
1915 { 0x03, 0x00a1 },
1916 { 0x02, 0x0008 },
1917 { 0x01, 0x0120 },
1918 { 0x00, 0x1000 },
1919 { 0x04, 0x0800 },
1920 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
françois romieu0b9b5712009-08-10 19:44:56 +00001922 { 0x03, 0xff41 },
1923 { 0x02, 0xdf60 },
1924 { 0x01, 0x0140 },
1925 { 0x00, 0x0077 },
1926 { 0x04, 0x7800 },
1927 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
françois romieu0b9b5712009-08-10 19:44:56 +00001929 { 0x03, 0x802f },
1930 { 0x02, 0x4f02 },
1931 { 0x01, 0x0409 },
1932 { 0x00, 0xf0f9 },
1933 { 0x04, 0x9800 },
1934 { 0x04, 0x9000 },
1935
1936 { 0x03, 0xdf01 },
1937 { 0x02, 0xdf20 },
1938 { 0x01, 0xff95 },
1939 { 0x00, 0xba00 },
1940 { 0x04, 0xa800 },
1941 { 0x04, 0xa000 },
1942
1943 { 0x03, 0xff41 },
1944 { 0x02, 0xdf20 },
1945 { 0x01, 0x0140 },
1946 { 0x00, 0x00bb },
1947 { 0x04, 0xb800 },
1948 { 0x04, 0xb000 },
1949
1950 { 0x03, 0xdf41 },
1951 { 0x02, 0xdc60 },
1952 { 0x01, 0x6340 },
1953 { 0x00, 0x007d },
1954 { 0x04, 0xd800 },
1955 { 0x04, 0xd000 },
1956
1957 { 0x03, 0xdf01 },
1958 { 0x02, 0xdf20 },
1959 { 0x01, 0x100a },
1960 { 0x00, 0xa0ff },
1961 { 0x04, 0xf800 },
1962 { 0x04, 0xf000 },
1963
1964 { 0x1f, 0x0000 },
1965 { 0x0b, 0x0000 },
1966 { 0x00, 0x9200 }
1967 };
1968
françois romieu4da19632011-01-03 15:07:55 +00001969 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970}
1971
françois romieu4da19632011-01-03 15:07:55 +00001972static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02001973{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001974 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02001975 { 0x1f, 0x0002 },
1976 { 0x01, 0x90d0 },
1977 { 0x1f, 0x0000 }
1978 };
1979
françois romieu4da19632011-01-03 15:07:55 +00001980 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02001981}
1982
françois romieu4da19632011-01-03 15:07:55 +00001983static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00001984{
1985 struct pci_dev *pdev = tp->pci_dev;
1986 u16 vendor_id, device_id;
1987
1988 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &vendor_id);
1989 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &device_id);
1990
1991 if ((vendor_id != PCI_VENDOR_ID_GIGABYTE) || (device_id != 0xe000))
1992 return;
1993
françois romieu4da19632011-01-03 15:07:55 +00001994 rtl_writephy(tp, 0x1f, 0x0001);
1995 rtl_writephy(tp, 0x10, 0xf01b);
1996 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00001997}
1998
françois romieu4da19632011-01-03 15:07:55 +00001999static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002000{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002001 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002002 { 0x1f, 0x0001 },
2003 { 0x04, 0x0000 },
2004 { 0x03, 0x00a1 },
2005 { 0x02, 0x0008 },
2006 { 0x01, 0x0120 },
2007 { 0x00, 0x1000 },
2008 { 0x04, 0x0800 },
2009 { 0x04, 0x9000 },
2010 { 0x03, 0x802f },
2011 { 0x02, 0x4f02 },
2012 { 0x01, 0x0409 },
2013 { 0x00, 0xf099 },
2014 { 0x04, 0x9800 },
2015 { 0x04, 0xa000 },
2016 { 0x03, 0xdf01 },
2017 { 0x02, 0xdf20 },
2018 { 0x01, 0xff95 },
2019 { 0x00, 0xba00 },
2020 { 0x04, 0xa800 },
2021 { 0x04, 0xf000 },
2022 { 0x03, 0xdf01 },
2023 { 0x02, 0xdf20 },
2024 { 0x01, 0x101a },
2025 { 0x00, 0xa0ff },
2026 { 0x04, 0xf800 },
2027 { 0x04, 0x0000 },
2028 { 0x1f, 0x0000 },
2029
2030 { 0x1f, 0x0001 },
2031 { 0x10, 0xf41b },
2032 { 0x14, 0xfb54 },
2033 { 0x18, 0xf5c7 },
2034 { 0x1f, 0x0000 },
2035
2036 { 0x1f, 0x0001 },
2037 { 0x17, 0x0cc0 },
2038 { 0x1f, 0x0000 }
2039 };
2040
françois romieu4da19632011-01-03 15:07:55 +00002041 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002042
françois romieu4da19632011-01-03 15:07:55 +00002043 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002044}
2045
françois romieu4da19632011-01-03 15:07:55 +00002046static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002047{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002048 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002049 { 0x1f, 0x0001 },
2050 { 0x04, 0x0000 },
2051 { 0x03, 0x00a1 },
2052 { 0x02, 0x0008 },
2053 { 0x01, 0x0120 },
2054 { 0x00, 0x1000 },
2055 { 0x04, 0x0800 },
2056 { 0x04, 0x9000 },
2057 { 0x03, 0x802f },
2058 { 0x02, 0x4f02 },
2059 { 0x01, 0x0409 },
2060 { 0x00, 0xf099 },
2061 { 0x04, 0x9800 },
2062 { 0x04, 0xa000 },
2063 { 0x03, 0xdf01 },
2064 { 0x02, 0xdf20 },
2065 { 0x01, 0xff95 },
2066 { 0x00, 0xba00 },
2067 { 0x04, 0xa800 },
2068 { 0x04, 0xf000 },
2069 { 0x03, 0xdf01 },
2070 { 0x02, 0xdf20 },
2071 { 0x01, 0x101a },
2072 { 0x00, 0xa0ff },
2073 { 0x04, 0xf800 },
2074 { 0x04, 0x0000 },
2075 { 0x1f, 0x0000 },
2076
2077 { 0x1f, 0x0001 },
2078 { 0x0b, 0x8480 },
2079 { 0x1f, 0x0000 },
2080
2081 { 0x1f, 0x0001 },
2082 { 0x18, 0x67c7 },
2083 { 0x04, 0x2000 },
2084 { 0x03, 0x002f },
2085 { 0x02, 0x4360 },
2086 { 0x01, 0x0109 },
2087 { 0x00, 0x3022 },
2088 { 0x04, 0x2800 },
2089 { 0x1f, 0x0000 },
2090
2091 { 0x1f, 0x0001 },
2092 { 0x17, 0x0cc0 },
2093 { 0x1f, 0x0000 }
2094 };
2095
françois romieu4da19632011-01-03 15:07:55 +00002096 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002097}
2098
françois romieu4da19632011-01-03 15:07:55 +00002099static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002100{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002101 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002102 { 0x10, 0xf41b },
2103 { 0x1f, 0x0000 }
2104 };
2105
françois romieu4da19632011-01-03 15:07:55 +00002106 rtl_writephy(tp, 0x1f, 0x0001);
2107 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002108
françois romieu4da19632011-01-03 15:07:55 +00002109 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002110}
2111
françois romieu4da19632011-01-03 15:07:55 +00002112static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002113{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002114 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002115 { 0x1f, 0x0001 },
2116 { 0x10, 0xf41b },
2117 { 0x1f, 0x0000 }
2118 };
2119
françois romieu4da19632011-01-03 15:07:55 +00002120 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002121}
2122
françois romieu4da19632011-01-03 15:07:55 +00002123static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002124{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002125 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002126 { 0x1f, 0x0000 },
2127 { 0x1d, 0x0f00 },
2128 { 0x1f, 0x0002 },
2129 { 0x0c, 0x1ec8 },
2130 { 0x1f, 0x0000 }
2131 };
2132
françois romieu4da19632011-01-03 15:07:55 +00002133 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002134}
2135
françois romieu4da19632011-01-03 15:07:55 +00002136static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002137{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002138 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002139 { 0x1f, 0x0001 },
2140 { 0x1d, 0x3d98 },
2141 { 0x1f, 0x0000 }
2142 };
2143
françois romieu4da19632011-01-03 15:07:55 +00002144 rtl_writephy(tp, 0x1f, 0x0000);
2145 rtl_patchphy(tp, 0x14, 1 << 5);
2146 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002147
françois romieu4da19632011-01-03 15:07:55 +00002148 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002149}
2150
françois romieu4da19632011-01-03 15:07:55 +00002151static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002152{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002153 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002154 { 0x1f, 0x0001 },
2155 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002156 { 0x1f, 0x0002 },
2157 { 0x00, 0x88d4 },
2158 { 0x01, 0x82b1 },
2159 { 0x03, 0x7002 },
2160 { 0x08, 0x9e30 },
2161 { 0x09, 0x01f0 },
2162 { 0x0a, 0x5500 },
2163 { 0x0c, 0x00c8 },
2164 { 0x1f, 0x0003 },
2165 { 0x12, 0xc096 },
2166 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002167 { 0x1f, 0x0000 },
2168 { 0x1f, 0x0000 },
2169 { 0x09, 0x2000 },
2170 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002171 };
2172
françois romieu4da19632011-01-03 15:07:55 +00002173 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002174
françois romieu4da19632011-01-03 15:07:55 +00002175 rtl_patchphy(tp, 0x14, 1 << 5);
2176 rtl_patchphy(tp, 0x0d, 1 << 5);
2177 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002178}
2179
françois romieu4da19632011-01-03 15:07:55 +00002180static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002181{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002182 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002183 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002184 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002185 { 0x03, 0x802f },
2186 { 0x02, 0x4f02 },
2187 { 0x01, 0x0409 },
2188 { 0x00, 0xf099 },
2189 { 0x04, 0x9800 },
2190 { 0x04, 0x9000 },
2191 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002192 { 0x1f, 0x0002 },
2193 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002194 { 0x06, 0x0761 },
2195 { 0x1f, 0x0003 },
2196 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002197 { 0x1f, 0x0000 }
2198 };
2199
françois romieu4da19632011-01-03 15:07:55 +00002200 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002201
françois romieu4da19632011-01-03 15:07:55 +00002202 rtl_patchphy(tp, 0x16, 1 << 0);
2203 rtl_patchphy(tp, 0x14, 1 << 5);
2204 rtl_patchphy(tp, 0x0d, 1 << 5);
2205 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002206}
2207
françois romieu4da19632011-01-03 15:07:55 +00002208static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002209{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002210 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002211 { 0x1f, 0x0001 },
2212 { 0x12, 0x2300 },
2213 { 0x1d, 0x3d98 },
2214 { 0x1f, 0x0002 },
2215 { 0x0c, 0x7eb8 },
2216 { 0x06, 0x5461 },
2217 { 0x1f, 0x0003 },
2218 { 0x16, 0x0f0a },
2219 { 0x1f, 0x0000 }
2220 };
2221
françois romieu4da19632011-01-03 15:07:55 +00002222 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002223
françois romieu4da19632011-01-03 15:07:55 +00002224 rtl_patchphy(tp, 0x16, 1 << 0);
2225 rtl_patchphy(tp, 0x14, 1 << 5);
2226 rtl_patchphy(tp, 0x0d, 1 << 5);
2227 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002228}
2229
françois romieu4da19632011-01-03 15:07:55 +00002230static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002231{
françois romieu4da19632011-01-03 15:07:55 +00002232 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002233}
2234
françois romieubca03d52011-01-03 15:07:31 +00002235static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002236{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002237 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002238 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002239 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002240 { 0x06, 0x4064 },
2241 { 0x07, 0x2863 },
2242 { 0x08, 0x059c },
2243 { 0x09, 0x26b4 },
2244 { 0x0a, 0x6a19 },
2245 { 0x0b, 0xdcc8 },
2246 { 0x10, 0xf06d },
2247 { 0x14, 0x7f68 },
2248 { 0x18, 0x7fd9 },
2249 { 0x1c, 0xf0ff },
2250 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002251 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002252 { 0x12, 0xf49f },
2253 { 0x13, 0x070b },
2254 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002255 { 0x14, 0x94c0 },
2256
2257 /*
2258 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002259 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002260 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002261 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002262 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002263 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002264 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002265 { 0x06, 0x5561 },
2266
2267 /*
2268 * Can not link to 1Gbps with bad cable
2269 * Decrease SNR threshold form 21.07dB to 19.04dB
2270 */
2271 { 0x1f, 0x0001 },
2272 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002273
2274 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002275 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002276 };
françois romieubca03d52011-01-03 15:07:31 +00002277 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002278
françois romieu4da19632011-01-03 15:07:55 +00002279 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002280
françois romieubca03d52011-01-03 15:07:31 +00002281 /*
2282 * Rx Error Issue
2283 * Fine Tune Switching regulator parameter
2284 */
françois romieu4da19632011-01-03 15:07:55 +00002285 rtl_writephy(tp, 0x1f, 0x0002);
2286 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2287 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002288
françois romieudaf9df62009-10-07 12:44:20 +00002289 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002290 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002291 { 0x1f, 0x0002 },
2292 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002293 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002294 { 0x05, 0x8330 },
2295 { 0x06, 0x669a },
2296 { 0x1f, 0x0002 }
2297 };
2298 int val;
2299
françois romieu4da19632011-01-03 15:07:55 +00002300 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002301
françois romieu4da19632011-01-03 15:07:55 +00002302 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002303
2304 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002305 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002306 0x0065, 0x0066, 0x0067, 0x0068,
2307 0x0069, 0x006a, 0x006b, 0x006c
2308 };
2309 int i;
2310
françois romieu4da19632011-01-03 15:07:55 +00002311 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002312
2313 val &= 0xff00;
2314 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002315 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002316 }
2317 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002318 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002319 { 0x1f, 0x0002 },
2320 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002321 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002322 { 0x05, 0x8330 },
2323 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002324 };
2325
françois romieu4da19632011-01-03 15:07:55 +00002326 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002327 }
2328
françois romieubca03d52011-01-03 15:07:31 +00002329 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002330 rtl_writephy(tp, 0x1f, 0x0002);
2331 rtl_patchphy(tp, 0x0d, 0x0300);
2332 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002333
françois romieubca03d52011-01-03 15:07:31 +00002334 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002335 rtl_writephy(tp, 0x1f, 0x0002);
2336 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2337 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002338
françois romieu4da19632011-01-03 15:07:55 +00002339 rtl_writephy(tp, 0x1f, 0x0005);
2340 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002341
2342 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002343
françois romieu4da19632011-01-03 15:07:55 +00002344 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002345}
2346
françois romieubca03d52011-01-03 15:07:31 +00002347static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002348{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002349 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002350 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002351 { 0x1f, 0x0001 },
2352 { 0x06, 0x4064 },
2353 { 0x07, 0x2863 },
2354 { 0x08, 0x059c },
2355 { 0x09, 0x26b4 },
2356 { 0x0a, 0x6a19 },
2357 { 0x0b, 0xdcc8 },
2358 { 0x10, 0xf06d },
2359 { 0x14, 0x7f68 },
2360 { 0x18, 0x7fd9 },
2361 { 0x1c, 0xf0ff },
2362 { 0x1d, 0x3d9c },
2363 { 0x1f, 0x0003 },
2364 { 0x12, 0xf49f },
2365 { 0x13, 0x070b },
2366 { 0x1a, 0x05ad },
2367 { 0x14, 0x94c0 },
2368
françois romieubca03d52011-01-03 15:07:31 +00002369 /*
2370 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002371 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002372 */
françois romieudaf9df62009-10-07 12:44:20 +00002373 { 0x1f, 0x0002 },
2374 { 0x06, 0x5561 },
2375 { 0x1f, 0x0005 },
2376 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002377 { 0x06, 0x5561 },
2378
2379 /*
2380 * Can not link to 1Gbps with bad cable
2381 * Decrease SNR threshold form 21.07dB to 19.04dB
2382 */
2383 { 0x1f, 0x0001 },
2384 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002385
2386 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002387 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002388 };
françois romieubca03d52011-01-03 15:07:31 +00002389 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002390
françois romieu4da19632011-01-03 15:07:55 +00002391 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002392
2393 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002394 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002395 { 0x1f, 0x0002 },
2396 { 0x05, 0x669a },
2397 { 0x1f, 0x0005 },
2398 { 0x05, 0x8330 },
2399 { 0x06, 0x669a },
2400
2401 { 0x1f, 0x0002 }
2402 };
2403 int val;
2404
françois romieu4da19632011-01-03 15:07:55 +00002405 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002406
françois romieu4da19632011-01-03 15:07:55 +00002407 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002408 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002409 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002410 0x0065, 0x0066, 0x0067, 0x0068,
2411 0x0069, 0x006a, 0x006b, 0x006c
2412 };
2413 int i;
2414
françois romieu4da19632011-01-03 15:07:55 +00002415 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002416
2417 val &= 0xff00;
2418 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002419 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002420 }
2421 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002422 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002423 { 0x1f, 0x0002 },
2424 { 0x05, 0x2642 },
2425 { 0x1f, 0x0005 },
2426 { 0x05, 0x8330 },
2427 { 0x06, 0x2642 }
2428 };
2429
françois romieu4da19632011-01-03 15:07:55 +00002430 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002431 }
2432
françois romieubca03d52011-01-03 15:07:31 +00002433 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002434 rtl_writephy(tp, 0x1f, 0x0002);
2435 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2436 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002437
françois romieubca03d52011-01-03 15:07:31 +00002438 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002439 rtl_writephy(tp, 0x1f, 0x0002);
2440 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002441
françois romieu4da19632011-01-03 15:07:55 +00002442 rtl_writephy(tp, 0x1f, 0x0005);
2443 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002444
2445 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002446
françois romieu4da19632011-01-03 15:07:55 +00002447 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002448}
2449
françois romieu4da19632011-01-03 15:07:55 +00002450static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002451{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002452 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002453 { 0x1f, 0x0002 },
2454 { 0x10, 0x0008 },
2455 { 0x0d, 0x006c },
2456
2457 { 0x1f, 0x0000 },
2458 { 0x0d, 0xf880 },
2459
2460 { 0x1f, 0x0001 },
2461 { 0x17, 0x0cc0 },
2462
2463 { 0x1f, 0x0001 },
2464 { 0x0b, 0xa4d8 },
2465 { 0x09, 0x281c },
2466 { 0x07, 0x2883 },
2467 { 0x0a, 0x6b35 },
2468 { 0x1d, 0x3da4 },
2469 { 0x1c, 0xeffd },
2470 { 0x14, 0x7f52 },
2471 { 0x18, 0x7fc6 },
2472 { 0x08, 0x0601 },
2473 { 0x06, 0x4063 },
2474 { 0x10, 0xf074 },
2475 { 0x1f, 0x0003 },
2476 { 0x13, 0x0789 },
2477 { 0x12, 0xf4bd },
2478 { 0x1a, 0x04fd },
2479 { 0x14, 0x84b0 },
2480 { 0x1f, 0x0000 },
2481 { 0x00, 0x9200 },
2482
2483 { 0x1f, 0x0005 },
2484 { 0x01, 0x0340 },
2485 { 0x1f, 0x0001 },
2486 { 0x04, 0x4000 },
2487 { 0x03, 0x1d21 },
2488 { 0x02, 0x0c32 },
2489 { 0x01, 0x0200 },
2490 { 0x00, 0x5554 },
2491 { 0x04, 0x4800 },
2492 { 0x04, 0x4000 },
2493 { 0x04, 0xf000 },
2494 { 0x03, 0xdf01 },
2495 { 0x02, 0xdf20 },
2496 { 0x01, 0x101a },
2497 { 0x00, 0xa0ff },
2498 { 0x04, 0xf800 },
2499 { 0x04, 0xf000 },
2500 { 0x1f, 0x0000 },
2501
2502 { 0x1f, 0x0007 },
2503 { 0x1e, 0x0023 },
2504 { 0x16, 0x0000 },
2505 { 0x1f, 0x0000 }
2506 };
2507
françois romieu4da19632011-01-03 15:07:55 +00002508 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002509}
2510
françois romieue6de30d2011-01-03 15:08:37 +00002511static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2512{
2513 static const struct phy_reg phy_reg_init[] = {
2514 { 0x1f, 0x0001 },
2515 { 0x17, 0x0cc0 },
2516
2517 { 0x1f, 0x0007 },
2518 { 0x1e, 0x002d },
2519 { 0x18, 0x0040 },
2520 { 0x1f, 0x0000 }
2521 };
2522
2523 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2524 rtl_patchphy(tp, 0x0d, 1 << 5);
2525}
2526
hayeswang01dc7fe2011-03-21 01:50:28 +00002527static void rtl8168e_hw_phy_config(struct rtl8169_private *tp)
2528{
2529 static const struct phy_reg phy_reg_init[] = {
2530 /* Enable Delay cap */
2531 { 0x1f, 0x0005 },
2532 { 0x05, 0x8b80 },
2533 { 0x06, 0xc896 },
2534 { 0x1f, 0x0000 },
2535
2536 /* Channel estimation fine tune */
2537 { 0x1f, 0x0001 },
2538 { 0x0b, 0x6c20 },
2539 { 0x07, 0x2872 },
2540 { 0x1c, 0xefff },
2541 { 0x1f, 0x0003 },
2542 { 0x14, 0x6420 },
2543 { 0x1f, 0x0000 },
2544
2545 /* Update PFM & 10M TX idle timer */
2546 { 0x1f, 0x0007 },
2547 { 0x1e, 0x002f },
2548 { 0x15, 0x1919 },
2549 { 0x1f, 0x0000 },
2550
2551 { 0x1f, 0x0007 },
2552 { 0x1e, 0x00ac },
2553 { 0x18, 0x0006 },
2554 { 0x1f, 0x0000 }
2555 };
2556
Francois Romieu15ecd032011-04-27 13:52:22 -07002557 rtl_apply_firmware(tp);
2558
hayeswang01dc7fe2011-03-21 01:50:28 +00002559 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2560
2561 /* DCO enable for 10M IDLE Power */
2562 rtl_writephy(tp, 0x1f, 0x0007);
2563 rtl_writephy(tp, 0x1e, 0x0023);
2564 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2565 rtl_writephy(tp, 0x1f, 0x0000);
2566
2567 /* For impedance matching */
2568 rtl_writephy(tp, 0x1f, 0x0002);
2569 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002570 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002571
2572 /* PHY auto speed down */
2573 rtl_writephy(tp, 0x1f, 0x0007);
2574 rtl_writephy(tp, 0x1e, 0x002d);
2575 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2576 rtl_writephy(tp, 0x1f, 0x0000);
2577 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2578
2579 rtl_writephy(tp, 0x1f, 0x0005);
2580 rtl_writephy(tp, 0x05, 0x8b86);
2581 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2582 rtl_writephy(tp, 0x1f, 0x0000);
2583
2584 rtl_writephy(tp, 0x1f, 0x0005);
2585 rtl_writephy(tp, 0x05, 0x8b85);
2586 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2587 rtl_writephy(tp, 0x1f, 0x0007);
2588 rtl_writephy(tp, 0x1e, 0x0020);
2589 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2590 rtl_writephy(tp, 0x1f, 0x0006);
2591 rtl_writephy(tp, 0x00, 0x5a00);
2592 rtl_writephy(tp, 0x1f, 0x0000);
2593 rtl_writephy(tp, 0x0d, 0x0007);
2594 rtl_writephy(tp, 0x0e, 0x003c);
2595 rtl_writephy(tp, 0x0d, 0x4007);
2596 rtl_writephy(tp, 0x0e, 0x0000);
2597 rtl_writephy(tp, 0x0d, 0x0000);
2598}
2599
françois romieu4da19632011-01-03 15:07:55 +00002600static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02002601{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002602 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02002603 { 0x1f, 0x0003 },
2604 { 0x08, 0x441d },
2605 { 0x01, 0x9100 },
2606 { 0x1f, 0x0000 }
2607 };
2608
françois romieu4da19632011-01-03 15:07:55 +00002609 rtl_writephy(tp, 0x1f, 0x0000);
2610 rtl_patchphy(tp, 0x11, 1 << 12);
2611 rtl_patchphy(tp, 0x19, 1 << 13);
2612 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002613
françois romieu4da19632011-01-03 15:07:55 +00002614 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02002615}
2616
Hayes Wang5a5e4442011-02-22 17:26:21 +08002617static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
2618{
2619 static const struct phy_reg phy_reg_init[] = {
2620 { 0x1f, 0x0005 },
2621 { 0x1a, 0x0000 },
2622 { 0x1f, 0x0000 },
2623
2624 { 0x1f, 0x0004 },
2625 { 0x1c, 0x0000 },
2626 { 0x1f, 0x0000 },
2627
2628 { 0x1f, 0x0001 },
2629 { 0x15, 0x7701 },
2630 { 0x1f, 0x0000 }
2631 };
2632
2633 /* Disable ALDPS before ram code */
2634 rtl_writephy(tp, 0x1f, 0x0000);
2635 rtl_writephy(tp, 0x18, 0x0310);
2636 msleep(100);
2637
François Romieu953a12c2011-04-24 17:38:48 +02002638 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08002639
2640 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2641}
2642
Francois Romieu5615d9f2007-08-17 17:50:46 +02002643static void rtl_hw_phy_config(struct net_device *dev)
2644{
2645 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002646
2647 rtl8169_print_mac_version(tp);
2648
2649 switch (tp->mac_version) {
2650 case RTL_GIGA_MAC_VER_01:
2651 break;
2652 case RTL_GIGA_MAC_VER_02:
2653 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00002654 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002655 break;
2656 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00002657 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002658 break;
françois romieu2e9558562009-08-10 19:44:19 +00002659 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00002660 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002661 break;
françois romieu8c7006a2009-08-10 19:43:29 +00002662 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00002663 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00002664 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02002665 case RTL_GIGA_MAC_VER_07:
2666 case RTL_GIGA_MAC_VER_08:
2667 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00002668 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002669 break;
Francois Romieu236b8082008-05-30 16:11:48 +02002670 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00002671 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002672 break;
2673 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00002674 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002675 break;
2676 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00002677 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002678 break;
Francois Romieu867763c2007-08-17 18:21:58 +02002679 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00002680 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002681 break;
2682 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00002683 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002684 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02002685 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00002686 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002687 break;
Francois Romieu197ff762008-06-28 13:16:02 +02002688 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00002689 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02002690 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02002691 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00002692 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002693 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02002694 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002695 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00002696 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02002697 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02002698 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00002699 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002700 break;
2701 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00002702 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002703 break;
2704 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00002705 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02002706 break;
françois romieue6de30d2011-01-03 15:08:37 +00002707 case RTL_GIGA_MAC_VER_28:
2708 rtl8168d_4_hw_phy_config(tp);
2709 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08002710 case RTL_GIGA_MAC_VER_29:
2711 case RTL_GIGA_MAC_VER_30:
2712 rtl8105e_hw_phy_config(tp);
2713 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02002714 case RTL_GIGA_MAC_VER_31:
2715 /* None. */
2716 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00002717 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00002718 case RTL_GIGA_MAC_VER_33:
Francois Romieu15ecd032011-04-27 13:52:22 -07002719 rtl8168e_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00002720 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02002721
Francois Romieu5615d9f2007-08-17 17:50:46 +02002722 default:
2723 break;
2724 }
2725}
2726
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727static void rtl8169_phy_timer(unsigned long __opaque)
2728{
2729 struct net_device *dev = (struct net_device *)__opaque;
2730 struct rtl8169_private *tp = netdev_priv(dev);
2731 struct timer_list *timer = &tp->timer;
2732 void __iomem *ioaddr = tp->mmio_addr;
2733 unsigned long timeout = RTL8169_PHY_TIMEOUT;
2734
Francois Romieubcf0bf92006-07-26 23:14:13 +02002735 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 spin_lock_irq(&tp->lock);
2738
françois romieu4da19632011-01-03 15:07:55 +00002739 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02002740 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 * A busy loop could burn quite a few cycles on nowadays CPU.
2742 * Let's delay the execution of the timer for a few ticks.
2743 */
2744 timeout = HZ/10;
2745 goto out_mod_timer;
2746 }
2747
2748 if (tp->link_ok(ioaddr))
2749 goto out_unlock;
2750
Joe Perchesbf82c182010-02-09 11:49:50 +00002751 netif_warn(tp, link, dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
françois romieu4da19632011-01-03 15:07:55 +00002753 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754
2755out_mod_timer:
2756 mod_timer(timer, jiffies + timeout);
2757out_unlock:
2758 spin_unlock_irq(&tp->lock);
2759}
2760
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761#ifdef CONFIG_NET_POLL_CONTROLLER
2762/*
2763 * Polling 'interrupt' - used by things like netconsole to send skbs
2764 * without having to re-enable interrupts. It's not called while
2765 * the interrupt routine is executing.
2766 */
2767static void rtl8169_netpoll(struct net_device *dev)
2768{
2769 struct rtl8169_private *tp = netdev_priv(dev);
2770 struct pci_dev *pdev = tp->pci_dev;
2771
2772 disable_irq(pdev->irq);
David Howells7d12e782006-10-05 14:55:46 +01002773 rtl8169_interrupt(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 enable_irq(pdev->irq);
2775}
2776#endif
2777
2778static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
2779 void __iomem *ioaddr)
2780{
2781 iounmap(ioaddr);
2782 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00002783 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 pci_disable_device(pdev);
2785 free_netdev(dev);
2786}
2787
Francois Romieubf793292006-11-01 00:53:05 +01002788static void rtl8169_phy_reset(struct net_device *dev,
2789 struct rtl8169_private *tp)
2790{
Francois Romieu07d3f512007-02-21 22:40:46 +01002791 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01002792
françois romieu4da19632011-01-03 15:07:55 +00002793 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01002794 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00002795 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01002796 return;
2797 msleep(1);
2798 }
Joe Perchesbf82c182010-02-09 11:49:50 +00002799 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01002800}
2801
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002802static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002804 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002805
Francois Romieu5615d9f2007-08-17 17:50:46 +02002806 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002807
Marcus Sundberg773328942008-07-10 21:28:08 +02002808 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
2809 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2810 RTL_W8(0x82, 0x01);
2811 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002812
Francois Romieu6dccd162007-02-13 23:38:05 +01002813 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
2814
2815 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
2816 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002817
Francois Romieubcf0bf92006-07-26 23:14:13 +02002818 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002819 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2820 RTL_W8(0x82, 0x01);
2821 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00002822 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002823 }
2824
Francois Romieubf793292006-11-01 00:53:05 +01002825 rtl8169_phy_reset(dev, tp);
2826
Oliver Neukum54405cd2011-01-06 21:55:13 +01002827 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002828 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
2829 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
2830 (tp->mii.supports_gmii ?
2831 ADVERTISED_1000baseT_Half |
2832 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002833
Joe Perchesbf82c182010-02-09 11:49:50 +00002834 if (RTL_R8(PHYstatus) & TBI_Enable)
2835 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002836}
2837
Francois Romieu773d2022007-01-31 23:47:43 +01002838static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
2839{
2840 void __iomem *ioaddr = tp->mmio_addr;
2841 u32 high;
2842 u32 low;
2843
2844 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
2845 high = addr[4] | (addr[5] << 8);
2846
2847 spin_lock_irq(&tp->lock);
2848
2849 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00002850
Francois Romieu773d2022007-01-31 23:47:43 +01002851 RTL_W32(MAC4, high);
françois romieu908ba2b2010-04-26 11:42:58 +00002852 RTL_R32(MAC4);
2853
Francois Romieu78f1cd02010-03-27 19:35:46 -07002854 RTL_W32(MAC0, low);
françois romieu908ba2b2010-04-26 11:42:58 +00002855 RTL_R32(MAC0);
2856
Francois Romieu773d2022007-01-31 23:47:43 +01002857 RTL_W8(Cfg9346, Cfg9346_Lock);
2858
2859 spin_unlock_irq(&tp->lock);
2860}
2861
2862static int rtl_set_mac_address(struct net_device *dev, void *p)
2863{
2864 struct rtl8169_private *tp = netdev_priv(dev);
2865 struct sockaddr *addr = p;
2866
2867 if (!is_valid_ether_addr(addr->sa_data))
2868 return -EADDRNOTAVAIL;
2869
2870 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
2871
2872 rtl_rar_set(tp, dev->dev_addr);
2873
2874 return 0;
2875}
2876
Francois Romieu5f787a12006-08-17 13:02:36 +02002877static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2878{
2879 struct rtl8169_private *tp = netdev_priv(dev);
2880 struct mii_ioctl_data *data = if_mii(ifr);
2881
Francois Romieu8b4ab282008-11-19 22:05:25 -08002882 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
2883}
Francois Romieu5f787a12006-08-17 13:02:36 +02002884
Francois Romieucecb5fd2011-04-01 10:21:07 +02002885static int rtl_xmii_ioctl(struct rtl8169_private *tp,
2886 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08002887{
Francois Romieu5f787a12006-08-17 13:02:36 +02002888 switch (cmd) {
2889 case SIOCGMIIPHY:
2890 data->phy_id = 32; /* Internal PHY */
2891 return 0;
2892
2893 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00002894 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02002895 return 0;
2896
2897 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00002898 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02002899 return 0;
2900 }
2901 return -EOPNOTSUPP;
2902}
2903
Francois Romieu8b4ab282008-11-19 22:05:25 -08002904static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
2905{
2906 return -EOPNOTSUPP;
2907}
2908
Francois Romieu0e485152007-02-20 00:00:26 +01002909static const struct rtl_cfg_info {
2910 void (*hw_start)(struct net_device *);
2911 unsigned int region;
2912 unsigned int align;
2913 u16 intr_event;
2914 u16 napi_event;
Francois Romieuccdffb92008-07-26 14:26:06 +02002915 unsigned features;
Jean Delvaref21b75e2009-05-26 20:54:48 -07002916 u8 default_ver;
Francois Romieu0e485152007-02-20 00:00:26 +01002917} rtl_cfg_infos [] = {
2918 [RTL_CFG_0] = {
2919 .hw_start = rtl_hw_start_8169,
2920 .region = 1,
Francois Romieue9f63f32007-02-28 23:16:57 +01002921 .align = 0,
Francois Romieu0e485152007-02-20 00:00:26 +01002922 .intr_event = SYSErr | LinkChg | RxOverflow |
2923 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02002924 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07002925 .features = RTL_FEATURE_GMII,
2926 .default_ver = RTL_GIGA_MAC_VER_01,
Francois Romieu0e485152007-02-20 00:00:26 +01002927 },
2928 [RTL_CFG_1] = {
2929 .hw_start = rtl_hw_start_8168,
2930 .region = 2,
2931 .align = 8,
françois romieu53f57352010-11-08 13:23:05 +00002932 .intr_event = SYSErr | LinkChg | RxOverflow |
Francois Romieu0e485152007-02-20 00:00:26 +01002933 TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02002934 .napi_event = TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07002935 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
2936 .default_ver = RTL_GIGA_MAC_VER_11,
Francois Romieu0e485152007-02-20 00:00:26 +01002937 },
2938 [RTL_CFG_2] = {
2939 .hw_start = rtl_hw_start_8101,
2940 .region = 2,
2941 .align = 8,
2942 .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
2943 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02002944 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07002945 .features = RTL_FEATURE_MSI,
2946 .default_ver = RTL_GIGA_MAC_VER_13,
Francois Romieu0e485152007-02-20 00:00:26 +01002947 }
2948};
2949
Francois Romieufbac58f2007-10-04 22:51:38 +02002950/* Cfg9346_Unlock assumed. */
2951static unsigned rtl_try_msi(struct pci_dev *pdev, void __iomem *ioaddr,
2952 const struct rtl_cfg_info *cfg)
2953{
2954 unsigned msi = 0;
2955 u8 cfg2;
2956
2957 cfg2 = RTL_R8(Config2) & ~MSIEnable;
Francois Romieuccdffb92008-07-26 14:26:06 +02002958 if (cfg->features & RTL_FEATURE_MSI) {
Francois Romieufbac58f2007-10-04 22:51:38 +02002959 if (pci_enable_msi(pdev)) {
2960 dev_info(&pdev->dev, "no MSI. Back to INTx.\n");
2961 } else {
2962 cfg2 |= MSIEnable;
2963 msi = RTL_FEATURE_MSI;
2964 }
2965 }
2966 RTL_W8(Config2, cfg2);
2967 return msi;
2968}
2969
2970static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
2971{
2972 if (tp->features & RTL_FEATURE_MSI) {
2973 pci_disable_msi(pdev);
2974 tp->features &= ~RTL_FEATURE_MSI;
2975 }
2976}
2977
Francois Romieu8b4ab282008-11-19 22:05:25 -08002978static const struct net_device_ops rtl8169_netdev_ops = {
2979 .ndo_open = rtl8169_open,
2980 .ndo_stop = rtl8169_close,
2981 .ndo_get_stats = rtl8169_get_stats,
Stephen Hemminger00829822008-11-20 20:14:53 -08002982 .ndo_start_xmit = rtl8169_start_xmit,
Francois Romieu8b4ab282008-11-19 22:05:25 -08002983 .ndo_tx_timeout = rtl8169_tx_timeout,
2984 .ndo_validate_addr = eth_validate_addr,
2985 .ndo_change_mtu = rtl8169_change_mtu,
Michał Mirosław350fb322011-04-08 06:35:56 +00002986 .ndo_fix_features = rtl8169_fix_features,
2987 .ndo_set_features = rtl8169_set_features,
Francois Romieu8b4ab282008-11-19 22:05:25 -08002988 .ndo_set_mac_address = rtl_set_mac_address,
2989 .ndo_do_ioctl = rtl8169_ioctl,
2990 .ndo_set_multicast_list = rtl_set_rx_mode,
Francois Romieu8b4ab282008-11-19 22:05:25 -08002991#ifdef CONFIG_NET_POLL_CONTROLLER
2992 .ndo_poll_controller = rtl8169_netpoll,
2993#endif
2994
2995};
2996
françois romieuc0e45c12011-01-03 15:08:04 +00002997static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
2998{
2999 struct mdio_ops *ops = &tp->mdio_ops;
3000
3001 switch (tp->mac_version) {
3002 case RTL_GIGA_MAC_VER_27:
3003 ops->write = r8168dp_1_mdio_write;
3004 ops->read = r8168dp_1_mdio_read;
3005 break;
françois romieue6de30d2011-01-03 15:08:37 +00003006 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003007 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003008 ops->write = r8168dp_2_mdio_write;
3009 ops->read = r8168dp_2_mdio_read;
3010 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003011 default:
3012 ops->write = r8169_mdio_write;
3013 ops->read = r8169_mdio_read;
3014 break;
3015 }
3016}
3017
françois romieu065c27c2011-01-03 15:08:12 +00003018static void r810x_phy_power_down(struct rtl8169_private *tp)
3019{
3020 rtl_writephy(tp, 0x1f, 0x0000);
3021 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3022}
3023
3024static void r810x_phy_power_up(struct rtl8169_private *tp)
3025{
3026 rtl_writephy(tp, 0x1f, 0x0000);
3027 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3028}
3029
3030static void r810x_pll_power_down(struct rtl8169_private *tp)
3031{
3032 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3033 rtl_writephy(tp, 0x1f, 0x0000);
3034 rtl_writephy(tp, MII_BMCR, 0x0000);
3035 return;
3036 }
3037
3038 r810x_phy_power_down(tp);
3039}
3040
3041static void r810x_pll_power_up(struct rtl8169_private *tp)
3042{
3043 r810x_phy_power_up(tp);
3044}
3045
3046static void r8168_phy_power_up(struct rtl8169_private *tp)
3047{
3048 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003049 switch (tp->mac_version) {
3050 case RTL_GIGA_MAC_VER_11:
3051 case RTL_GIGA_MAC_VER_12:
3052 case RTL_GIGA_MAC_VER_17:
3053 case RTL_GIGA_MAC_VER_18:
3054 case RTL_GIGA_MAC_VER_19:
3055 case RTL_GIGA_MAC_VER_20:
3056 case RTL_GIGA_MAC_VER_21:
3057 case RTL_GIGA_MAC_VER_22:
3058 case RTL_GIGA_MAC_VER_23:
3059 case RTL_GIGA_MAC_VER_24:
3060 case RTL_GIGA_MAC_VER_25:
3061 case RTL_GIGA_MAC_VER_26:
3062 case RTL_GIGA_MAC_VER_27:
3063 case RTL_GIGA_MAC_VER_28:
3064 case RTL_GIGA_MAC_VER_31:
3065 rtl_writephy(tp, 0x0e, 0x0000);
3066 break;
3067 default:
3068 break;
3069 }
françois romieu065c27c2011-01-03 15:08:12 +00003070 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3071}
3072
3073static void r8168_phy_power_down(struct rtl8169_private *tp)
3074{
3075 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003076 switch (tp->mac_version) {
3077 case RTL_GIGA_MAC_VER_32:
3078 case RTL_GIGA_MAC_VER_33:
3079 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3080 break;
3081
3082 case RTL_GIGA_MAC_VER_11:
3083 case RTL_GIGA_MAC_VER_12:
3084 case RTL_GIGA_MAC_VER_17:
3085 case RTL_GIGA_MAC_VER_18:
3086 case RTL_GIGA_MAC_VER_19:
3087 case RTL_GIGA_MAC_VER_20:
3088 case RTL_GIGA_MAC_VER_21:
3089 case RTL_GIGA_MAC_VER_22:
3090 case RTL_GIGA_MAC_VER_23:
3091 case RTL_GIGA_MAC_VER_24:
3092 case RTL_GIGA_MAC_VER_25:
3093 case RTL_GIGA_MAC_VER_26:
3094 case RTL_GIGA_MAC_VER_27:
3095 case RTL_GIGA_MAC_VER_28:
3096 case RTL_GIGA_MAC_VER_31:
3097 rtl_writephy(tp, 0x0e, 0x0200);
3098 default:
3099 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3100 break;
3101 }
françois romieu065c27c2011-01-03 15:08:12 +00003102}
3103
3104static void r8168_pll_power_down(struct rtl8169_private *tp)
3105{
3106 void __iomem *ioaddr = tp->mmio_addr;
3107
Francois Romieucecb5fd2011-04-01 10:21:07 +02003108 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3109 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3110 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003111 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003112 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003113 }
françois romieu065c27c2011-01-03 15:08:12 +00003114
Francois Romieucecb5fd2011-04-01 10:21:07 +02003115 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3116 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00003117 (RTL_R16(CPlusCmd) & ASF)) {
3118 return;
3119 }
3120
hayeswang01dc7fe2011-03-21 01:50:28 +00003121 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3122 tp->mac_version == RTL_GIGA_MAC_VER_33)
3123 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3124
françois romieu065c27c2011-01-03 15:08:12 +00003125 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3126 rtl_writephy(tp, 0x1f, 0x0000);
3127 rtl_writephy(tp, MII_BMCR, 0x0000);
3128
3129 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3130 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3131 return;
3132 }
3133
3134 r8168_phy_power_down(tp);
3135
3136 switch (tp->mac_version) {
3137 case RTL_GIGA_MAC_VER_25:
3138 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003139 case RTL_GIGA_MAC_VER_27:
3140 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003141 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003142 case RTL_GIGA_MAC_VER_32:
3143 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003144 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3145 break;
3146 }
3147}
3148
3149static void r8168_pll_power_up(struct rtl8169_private *tp)
3150{
3151 void __iomem *ioaddr = tp->mmio_addr;
3152
Francois Romieucecb5fd2011-04-01 10:21:07 +02003153 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3154 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3155 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003156 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003157 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003158 }
françois romieu065c27c2011-01-03 15:08:12 +00003159
3160 switch (tp->mac_version) {
3161 case RTL_GIGA_MAC_VER_25:
3162 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003163 case RTL_GIGA_MAC_VER_27:
3164 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003165 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003166 case RTL_GIGA_MAC_VER_32:
3167 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003168 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3169 break;
3170 }
3171
3172 r8168_phy_power_up(tp);
3173}
3174
3175static void rtl_pll_power_op(struct rtl8169_private *tp,
3176 void (*op)(struct rtl8169_private *))
3177{
3178 if (op)
3179 op(tp);
3180}
3181
3182static void rtl_pll_power_down(struct rtl8169_private *tp)
3183{
3184 rtl_pll_power_op(tp, tp->pll_power_ops.down);
3185}
3186
3187static void rtl_pll_power_up(struct rtl8169_private *tp)
3188{
3189 rtl_pll_power_op(tp, tp->pll_power_ops.up);
3190}
3191
3192static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3193{
3194 struct pll_power_ops *ops = &tp->pll_power_ops;
3195
3196 switch (tp->mac_version) {
3197 case RTL_GIGA_MAC_VER_07:
3198 case RTL_GIGA_MAC_VER_08:
3199 case RTL_GIGA_MAC_VER_09:
3200 case RTL_GIGA_MAC_VER_10:
3201 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003202 case RTL_GIGA_MAC_VER_29:
3203 case RTL_GIGA_MAC_VER_30:
françois romieu065c27c2011-01-03 15:08:12 +00003204 ops->down = r810x_pll_power_down;
3205 ops->up = r810x_pll_power_up;
3206 break;
3207
3208 case RTL_GIGA_MAC_VER_11:
3209 case RTL_GIGA_MAC_VER_12:
3210 case RTL_GIGA_MAC_VER_17:
3211 case RTL_GIGA_MAC_VER_18:
3212 case RTL_GIGA_MAC_VER_19:
3213 case RTL_GIGA_MAC_VER_20:
3214 case RTL_GIGA_MAC_VER_21:
3215 case RTL_GIGA_MAC_VER_22:
3216 case RTL_GIGA_MAC_VER_23:
3217 case RTL_GIGA_MAC_VER_24:
3218 case RTL_GIGA_MAC_VER_25:
3219 case RTL_GIGA_MAC_VER_26:
3220 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003221 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003222 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003223 case RTL_GIGA_MAC_VER_32:
3224 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003225 ops->down = r8168_pll_power_down;
3226 ops->up = r8168_pll_power_up;
3227 break;
3228
3229 default:
3230 ops->down = NULL;
3231 ops->up = NULL;
3232 break;
3233 }
3234}
3235
Francois Romieu6f43adc2011-04-29 15:05:51 +02003236static void rtl_hw_reset(struct rtl8169_private *tp)
3237{
3238 void __iomem *ioaddr = tp->mmio_addr;
3239 int i;
3240
3241 /* Soft reset the chip. */
3242 RTL_W8(ChipCmd, CmdReset);
3243
3244 /* Check that the chip has finished the reset. */
3245 for (i = 0; i < 100; i++) {
3246 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3247 break;
3248 msleep_interruptible(1);
3249 }
3250}
3251
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003252static int __devinit
3253rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
3254{
Francois Romieu0e485152007-02-20 00:00:26 +01003255 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
3256 const unsigned int region = cfg->region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 struct rtl8169_private *tp;
Francois Romieuccdffb92008-07-26 14:26:06 +02003258 struct mii_if_info *mii;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003259 struct net_device *dev;
3260 void __iomem *ioaddr;
Francois Romieu2b7b4312011-04-18 22:53:24 -07003261 int chipset, i;
Francois Romieu07d3f512007-02-21 22:40:46 +01003262 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003264 if (netif_msg_drv(&debug)) {
3265 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
3266 MODULENAME, RTL8169_VERSION);
3267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 dev = alloc_etherdev(sizeof (*tp));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003270 if (!dev) {
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003271 if (netif_msg_drv(&debug))
Jeff Garzik9b91cf92006-06-27 11:39:50 -04003272 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003273 rc = -ENOMEM;
3274 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 }
3276
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieu8b4ab282008-11-19 22:05:25 -08003278 dev->netdev_ops = &rtl8169_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 tp = netdev_priv(dev);
David Howellsc4028952006-11-22 14:57:56 +00003280 tp->dev = dev;
Ivan Vecera21e197f2008-04-17 22:48:41 +02003281 tp->pci_dev = pdev;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003282 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
Francois Romieuccdffb92008-07-26 14:26:06 +02003284 mii = &tp->mii;
3285 mii->dev = dev;
3286 mii->mdio_read = rtl_mdio_read;
3287 mii->mdio_write = rtl_mdio_write;
3288 mii->phy_id_mask = 0x1f;
3289 mii->reg_num_mask = 0x1f;
3290 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
3291
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +00003292 /* disable ASPM completely as that cause random device stop working
3293 * problems as well as full system hangs for some PCIe devices users */
3294 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
3295 PCIE_LINK_STATE_CLKPM);
3296
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 /* enable device (incl. PCI PM wakeup and hotplug setup) */
3298 rc = pci_enable_device(pdev);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003299 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003300 netif_err(tp, probe, dev, "enable failure\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003301 goto err_out_free_dev_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 }
3303
françois romieu87aeec72010-04-26 11:42:06 +00003304 if (pci_set_mwi(pdev) < 0)
3305 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 /* make sure PCI base addr 1 is MMIO */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003308 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003309 netif_err(tp, probe, dev,
3310 "region #%d not an MMIO resource, aborting\n",
3311 region);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003313 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003315
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 /* check for weird/broken PCI region reporting */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003317 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003318 netif_err(tp, probe, dev,
3319 "Invalid PCI region size(s), aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003321 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 }
3323
3324 rc = pci_request_regions(pdev, MODULENAME);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003325 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003326 netif_err(tp, probe, dev, "could not request regions\n");
françois romieu87aeec72010-04-26 11:42:06 +00003327 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 }
3329
Hayes Wangd24e9aa2011-02-22 17:26:19 +08003330 tp->cp_cmd = RxChkSum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331
3332 if ((sizeof(dma_addr_t) > 4) &&
David S. Miller4300e8c2010-03-26 10:23:30 -07003333 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 tp->cp_cmd |= PCIDAC;
3335 dev->features |= NETIF_F_HIGHDMA;
3336 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07003337 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003339 netif_err(tp, probe, dev, "DMA configuration failed\n");
françois romieu87aeec72010-04-26 11:42:06 +00003340 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 }
3342 }
3343
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 /* ioremap MMIO region */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003345 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003346 if (!ioaddr) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003347 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 rc = -EIO;
françois romieu87aeec72010-04-26 11:42:06 +00003349 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 }
Francois Romieu6f43adc2011-04-29 15:05:51 +02003351 tp->mmio_addr = ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352
David S. Miller4300e8c2010-03-26 10:23:30 -07003353 tp->pcie_cap = pci_find_capability(pdev, PCI_CAP_ID_EXP);
3354 if (!tp->pcie_cap)
3355 netif_info(tp, probe, dev, "no PCI Express capability\n");
3356
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003357 RTL_W16(IntrMask, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358
Francois Romieu6f43adc2011-04-29 15:05:51 +02003359 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003361 RTL_W16(IntrStatus, 0xffff);
3362
françois romieuca52efd2009-07-24 12:34:19 +00003363 pci_set_master(pdev);
3364
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 /* Identify chip attached to board */
Francois Romieu5d320a22011-05-08 17:47:36 +02003366 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367
Francois Romieu7a8fc772011-03-01 17:18:33 +01003368 /*
3369 * Pretend we are using VLANs; This bypasses a nasty bug where
3370 * Interrupts stop flowing on high load on 8110SCd controllers.
3371 */
3372 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3373 tp->cp_cmd |= RxVlan;
3374
françois romieuc0e45c12011-01-03 15:08:04 +00003375 rtl_init_mdio_ops(tp);
françois romieu065c27c2011-01-03 15:08:12 +00003376 rtl_init_pll_power_ops(tp);
françois romieuc0e45c12011-01-03 15:08:04 +00003377
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 rtl8169_print_mac_version(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379
Francois Romieu85bffe62011-04-27 08:22:39 +02003380 chipset = tp->mac_version;
3381 tp->txd_version = rtl_chip_infos[chipset].txd_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382
Francois Romieu5d06a992006-02-23 00:47:58 +01003383 RTL_W8(Cfg9346, Cfg9346_Unlock);
3384 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
3385 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
Bruno Prémont20037fa2008-10-08 17:05:03 -07003386 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
3387 tp->features |= RTL_FEATURE_WOL;
3388 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
3389 tp->features |= RTL_FEATURE_WOL;
Francois Romieufbac58f2007-10-04 22:51:38 +02003390 tp->features |= rtl_try_msi(pdev, ioaddr, cfg);
Francois Romieu5d06a992006-02-23 00:47:58 +01003391 RTL_W8(Cfg9346, Cfg9346_Lock);
3392
Francois Romieu66ec5d42007-11-06 22:56:10 +01003393 if ((tp->mac_version <= RTL_GIGA_MAC_VER_06) &&
3394 (RTL_R8(PHYstatus) & TBI_Enable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 tp->set_speed = rtl8169_set_speed_tbi;
3396 tp->get_settings = rtl8169_gset_tbi;
3397 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
3398 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
3399 tp->link_ok = rtl8169_tbi_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003400 tp->do_ioctl = rtl_tbi_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 } else {
3402 tp->set_speed = rtl8169_set_speed_xmii;
3403 tp->get_settings = rtl8169_gset_xmii;
3404 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
3405 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
3406 tp->link_ok = rtl8169_xmii_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003407 tp->do_ioctl = rtl_xmii_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 }
3409
Francois Romieudf58ef52008-10-09 14:35:58 -07003410 spin_lock_init(&tp->lock);
3411
Ivan Vecera7bf6bf42008-09-23 22:46:29 +00003412 /* Get MAC address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 for (i = 0; i < MAC_ADDR_LEN; i++)
3414 dev->dev_addr[i] = RTL_R8(MAC0 + i);
John W. Linville6d6525b2005-09-12 10:48:57 -04003415 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
3419 dev->irq = pdev->irq;
3420 dev->base_addr = (unsigned long) ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003422 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423
Michał Mirosław350fb322011-04-08 06:35:56 +00003424 /* don't enable SG, IP_CSUM and TSO by default - it might not work
3425 * properly for all devices */
3426 dev->features |= NETIF_F_RXCSUM |
3427 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3428
3429 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3430 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3431 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3432 NETIF_F_HIGHDMA;
3433
3434 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3435 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
3436 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437
3438 tp->intr_mask = 0xffff;
Francois Romieu0e485152007-02-20 00:00:26 +01003439 tp->hw_start = cfg->hw_start;
3440 tp->intr_event = cfg->intr_event;
3441 tp->napi_event = cfg->napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442
Francois Romieu2efa53f2007-03-09 00:00:05 +01003443 init_timer(&tp->timer);
3444 tp->timer.data = (unsigned long) dev;
3445 tp->timer.function = rtl8169_phy_timer;
3446
François Romieu953a12c2011-04-24 17:38:48 +02003447 tp->fw = RTL_FIRMWARE_UNKNOWN;
3448
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 rc = register_netdev(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003450 if (rc < 0)
françois romieu87aeec72010-04-26 11:42:06 +00003451 goto err_out_msi_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452
3453 pci_set_drvdata(pdev, dev);
3454
Joe Perchesbf82c182010-02-09 11:49:50 +00003455 netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
Francois Romieu85bffe62011-04-27 08:22:39 +02003456 rtl_chip_infos[chipset].name, dev->base_addr, dev->dev_addr,
Joe Perchesbf82c182010-02-09 11:49:50 +00003457 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458
Francois Romieucecb5fd2011-04-01 10:21:07 +02003459 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3460 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3461 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00003462 rtl8168_driver_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003463 }
françois romieub646d902011-01-03 15:08:21 +00003464
Bruno Prémont8b76ab32008-10-08 17:06:25 -07003465 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466
Alan Sternf3ec4f82010-06-08 15:23:51 -04003467 if (pci_dev_run_wake(pdev))
3468 pm_runtime_put_noidle(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003469
Ivan Vecera0d672e92011-02-15 02:08:39 +00003470 netif_carrier_off(dev);
3471
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003472out:
3473 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474
françois romieu87aeec72010-04-26 11:42:06 +00003475err_out_msi_4:
Francois Romieufbac58f2007-10-04 22:51:38 +02003476 rtl_disable_msi(pdev, tp);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003477 iounmap(ioaddr);
françois romieu87aeec72010-04-26 11:42:06 +00003478err_out_free_res_3:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003479 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003480err_out_mwi_2:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003481 pci_clear_mwi(pdev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003482 pci_disable_device(pdev);
3483err_out_free_dev_1:
3484 free_netdev(dev);
3485 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486}
3487
Francois Romieu07d3f512007-02-21 22:40:46 +01003488static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489{
3490 struct net_device *dev = pci_get_drvdata(pdev);
3491 struct rtl8169_private *tp = netdev_priv(dev);
3492
Francois Romieucecb5fd2011-04-01 10:21:07 +02003493 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3494 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3495 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00003496 rtl8168_driver_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003497 }
françois romieub646d902011-01-03 15:08:21 +00003498
Tejun Heo23f333a2010-12-12 16:45:14 +01003499 cancel_delayed_work_sync(&tp->task);
Francois Romieueb2a0212007-02-15 23:37:21 +01003500
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 unregister_netdev(dev);
Ivan Veceracc098dc2009-11-29 23:12:52 -08003502
François Romieu953a12c2011-04-24 17:38:48 +02003503 rtl_release_firmware(tp);
3504
Alan Sternf3ec4f82010-06-08 15:23:51 -04003505 if (pci_dev_run_wake(pdev))
3506 pm_runtime_get_noresume(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003507
Ivan Veceracc098dc2009-11-29 23:12:52 -08003508 /* restore original MAC address */
3509 rtl_rar_set(tp, dev->perm_addr);
3510
Francois Romieufbac58f2007-10-04 22:51:38 +02003511 rtl_disable_msi(pdev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 rtl8169_release_board(pdev, dev, tp->mmio_addr);
3513 pci_set_drvdata(pdev, NULL);
3514}
3515
François Romieu953a12c2011-04-24 17:38:48 +02003516static void rtl_request_firmware(struct rtl8169_private *tp)
3517{
François Romieu953a12c2011-04-24 17:38:48 +02003518 /* Return early if the firmware is already loaded / cached. */
Francois Romieu31bd2042011-04-26 18:58:59 +02003519 if (IS_ERR(tp->fw)) {
3520 const char *name;
François Romieu953a12c2011-04-24 17:38:48 +02003521
Francois Romieu31bd2042011-04-26 18:58:59 +02003522 name = rtl_lookup_firmware_name(tp);
3523 if (name) {
François Romieu953a12c2011-04-24 17:38:48 +02003524 int rc;
3525
3526 rc = request_firmware(&tp->fw, name, &tp->pci_dev->dev);
Francois Romieu31bd2042011-04-26 18:58:59 +02003527 if (rc >= 0)
3528 return;
François Romieu953a12c2011-04-24 17:38:48 +02003529
Francois Romieu31bd2042011-04-26 18:58:59 +02003530 netif_warn(tp, ifup, tp->dev, "unable to load "
3531 "firmware patch %s (%d)\n", name, rc);
3532 }
3533 tp->fw = NULL;
3534 }
François Romieu953a12c2011-04-24 17:38:48 +02003535}
3536
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537static int rtl8169_open(struct net_device *dev)
3538{
3539 struct rtl8169_private *tp = netdev_priv(dev);
françois romieueee3a962011-01-08 02:17:26 +00003540 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu99f252b2007-04-02 22:59:59 +02003542 int retval = -ENOMEM;
3543
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003544 pm_runtime_get_sync(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545
Neil Hormanc0cd8842010-03-29 13:16:02 -07003546 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 * Rx and Tx desscriptors needs 256 bytes alignment.
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003548 * dma_alloc_coherent provides more.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 */
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003550 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
3551 &tp->TxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 if (!tp->TxDescArray)
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003553 goto err_pm_runtime_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003555 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
3556 &tp->RxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 if (!tp->RxDescArray)
Francois Romieu99f252b2007-04-02 22:59:59 +02003558 goto err_free_tx_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559
3560 retval = rtl8169_init_ring(dev);
3561 if (retval < 0)
Francois Romieu99f252b2007-04-02 22:59:59 +02003562 goto err_free_rx_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563
David Howellsc4028952006-11-22 14:57:56 +00003564 INIT_DELAYED_WORK(&tp->task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565
Francois Romieu99f252b2007-04-02 22:59:59 +02003566 smp_mb();
3567
François Romieu953a12c2011-04-24 17:38:48 +02003568 rtl_request_firmware(tp);
3569
Francois Romieufbac58f2007-10-04 22:51:38 +02003570 retval = request_irq(dev->irq, rtl8169_interrupt,
3571 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
Francois Romieu99f252b2007-04-02 22:59:59 +02003572 dev->name, dev);
3573 if (retval < 0)
François Romieu953a12c2011-04-24 17:38:48 +02003574 goto err_release_fw_2;
Francois Romieu99f252b2007-04-02 22:59:59 +02003575
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003576 napi_enable(&tp->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003577
françois romieueee3a962011-01-08 02:17:26 +00003578 rtl8169_init_phy(dev, tp);
3579
Michał Mirosław350fb322011-04-08 06:35:56 +00003580 rtl8169_set_features(dev, dev->features);
françois romieueee3a962011-01-08 02:17:26 +00003581
françois romieu065c27c2011-01-03 15:08:12 +00003582 rtl_pll_power_up(tp);
3583
Francois Romieu07ce4062007-02-23 23:36:39 +01003584 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003586 tp->saved_wolopts = 0;
3587 pm_runtime_put_noidle(&pdev->dev);
3588
françois romieueee3a962011-01-08 02:17:26 +00003589 rtl8169_check_link_status(dev, tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590out:
3591 return retval;
3592
François Romieu953a12c2011-04-24 17:38:48 +02003593err_release_fw_2:
3594 rtl_release_firmware(tp);
Francois Romieu99f252b2007-04-02 22:59:59 +02003595 rtl8169_rx_clear(tp);
3596err_free_rx_1:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003597 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
3598 tp->RxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003599 tp->RxDescArray = NULL;
Francois Romieu99f252b2007-04-02 22:59:59 +02003600err_free_tx_0:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003601 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
3602 tp->TxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003603 tp->TxDescArray = NULL;
3604err_pm_runtime_put:
3605 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 goto out;
3607}
3608
françois romieue6de30d2011-01-03 15:08:37 +00003609static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610{
françois romieue6de30d2011-01-03 15:08:37 +00003611 void __iomem *ioaddr = tp->mmio_addr;
3612
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 /* Disable interrupts */
3614 rtl8169_irq_mask_and_ack(ioaddr);
3615
Hayes Wang5d2e1952011-02-22 17:26:22 +08003616 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00003617 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3618 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieue6de30d2011-01-03 15:08:37 +00003619 while (RTL_R8(TxPoll) & NPQ)
3620 udelay(20);
3621
3622 }
3623
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 /* Reset the chipset */
3625 RTL_W8(ChipCmd, CmdReset);
3626
3627 /* PCI commit */
3628 RTL_R8(ChipCmd);
3629}
3630
Francois Romieu7f796d82007-06-11 23:04:41 +02003631static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01003632{
3633 void __iomem *ioaddr = tp->mmio_addr;
3634 u32 cfg = rtl8169_rx_config;
3635
Francois Romieu2b7b4312011-04-18 22:53:24 -07003636 cfg |= (RTL_R32(RxConfig) & RTL_RX_CONFIG_MASK);
Francois Romieu9cb427b2006-11-02 00:10:16 +01003637 RTL_W32(RxConfig, cfg);
3638
3639 /* Set DMA burst size and Interframe Gap Time */
3640 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
3641 (InterFrameGap << TxInterFrameGapShift));
3642}
3643
Francois Romieu07ce4062007-02-23 23:36:39 +01003644static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645{
3646 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647
Francois Romieu6f43adc2011-04-29 15:05:51 +02003648 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649
Francois Romieu07ce4062007-02-23 23:36:39 +01003650 tp->hw_start(dev);
3651
Francois Romieu07ce4062007-02-23 23:36:39 +01003652 netif_start_queue(dev);
3653}
3654
Francois Romieu7f796d82007-06-11 23:04:41 +02003655static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
3656 void __iomem *ioaddr)
3657{
3658 /*
3659 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
3660 * register to be written before TxDescAddrLow to work.
3661 * Switching from MMIO to I/O access fixes the issue as well.
3662 */
3663 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07003664 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02003665 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07003666 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02003667}
3668
3669static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
3670{
3671 u16 cmd;
3672
3673 cmd = RTL_R16(CPlusCmd);
3674 RTL_W16(CPlusCmd, cmd);
3675 return cmd;
3676}
3677
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07003678static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d82007-06-11 23:04:41 +02003679{
3680 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e82009-10-26 10:52:37 +00003681 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d82007-06-11 23:04:41 +02003682}
3683
Francois Romieu6dccd162007-02-13 23:38:05 +01003684static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
3685{
Francois Romieu37441002011-06-17 22:58:54 +02003686 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01003687 u32 mac_version;
3688 u32 clk;
3689 u32 val;
3690 } cfg2_info [] = {
3691 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
3692 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
3693 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
3694 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02003695 };
3696 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01003697 unsigned int i;
3698 u32 clk;
3699
3700 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01003701 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01003702 if ((p->mac_version == mac_version) && (p->clk == clk)) {
3703 RTL_W32(0x7c, p->val);
3704 break;
3705 }
3706 }
3707}
3708
Francois Romieu07ce4062007-02-23 23:36:39 +01003709static void rtl_hw_start_8169(struct net_device *dev)
3710{
3711 struct rtl8169_private *tp = netdev_priv(dev);
3712 void __iomem *ioaddr = tp->mmio_addr;
3713 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01003714
Francois Romieu9cb427b2006-11-02 00:10:16 +01003715 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
3716 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
3717 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
3718 }
3719
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02003721 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
3722 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
3723 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
3724 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01003725 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3726
françois romieuf0298f82011-01-03 15:07:42 +00003727 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728
Eric Dumazet6f0333b2010-10-11 11:17:47 +00003729 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730
Francois Romieucecb5fd2011-04-01 10:21:07 +02003731 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
3732 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
3733 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
3734 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02003735 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736
Francois Romieu7f796d82007-06-11 23:04:41 +02003737 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02003738
Francois Romieucecb5fd2011-04-01 10:21:07 +02003739 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
3740 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02003741 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02003743 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 }
3745
Francois Romieubcf0bf92006-07-26 23:14:13 +02003746 RTL_W16(CPlusCmd, tp->cp_cmd);
3747
Francois Romieu6dccd162007-02-13 23:38:05 +01003748 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
3749
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 /*
3751 * Undocumented corner. Supposedly:
3752 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
3753 */
3754 RTL_W16(IntrMitigate, 0x0000);
3755
Francois Romieu7f796d82007-06-11 23:04:41 +02003756 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01003757
Francois Romieucecb5fd2011-04-01 10:21:07 +02003758 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
3759 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
3760 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
3761 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02003762 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3763 rtl_set_rx_tx_config_registers(tp);
3764 }
3765
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02003767
3768 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
3769 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770
3771 RTL_W32(RxMissed, 0);
3772
Francois Romieu07ce4062007-02-23 23:36:39 +01003773 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774
3775 /* no early-rx interrupts */
3776 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01003777
3778 /* Enable all known interrupts by setting the interrupt mask. */
Francois Romieu0e485152007-02-20 00:00:26 +01003779 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01003780}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781
Francois Romieu9c14cea2008-07-05 00:21:15 +02003782static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
Francois Romieu458a9f62008-08-02 15:50:02 +02003783{
Francois Romieu9c14cea2008-07-05 00:21:15 +02003784 struct net_device *dev = pci_get_drvdata(pdev);
3785 struct rtl8169_private *tp = netdev_priv(dev);
3786 int cap = tp->pcie_cap;
Francois Romieu458a9f62008-08-02 15:50:02 +02003787
Francois Romieu9c14cea2008-07-05 00:21:15 +02003788 if (cap) {
3789 u16 ctl;
3790
3791 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
3792 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
3793 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
3794 }
Francois Romieu458a9f62008-08-02 15:50:02 +02003795}
3796
françois romieu650e8d52011-01-03 15:08:29 +00003797static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02003798{
3799 u32 csi;
3800
3801 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
françois romieu650e8d52011-01-03 15:08:29 +00003802 rtl_csi_write(ioaddr, 0x070c, csi | bits);
3803}
3804
françois romieue6de30d2011-01-03 15:08:37 +00003805static void rtl_csi_access_enable_1(void __iomem *ioaddr)
3806{
3807 rtl_csi_access_enable(ioaddr, 0x17000000);
3808}
3809
françois romieu650e8d52011-01-03 15:08:29 +00003810static void rtl_csi_access_enable_2(void __iomem *ioaddr)
3811{
3812 rtl_csi_access_enable(ioaddr, 0x27000000);
Francois Romieudacf8152008-08-02 20:44:13 +02003813}
3814
3815struct ephy_info {
3816 unsigned int offset;
3817 u16 mask;
3818 u16 bits;
3819};
3820
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003821static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02003822{
3823 u16 w;
3824
3825 while (len-- > 0) {
3826 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
3827 rtl_ephy_write(ioaddr, e->offset, w);
3828 e++;
3829 }
3830}
3831
Francois Romieub726e492008-06-28 12:22:59 +02003832static void rtl_disable_clock_request(struct pci_dev *pdev)
3833{
3834 struct net_device *dev = pci_get_drvdata(pdev);
3835 struct rtl8169_private *tp = netdev_priv(dev);
3836 int cap = tp->pcie_cap;
3837
3838 if (cap) {
3839 u16 ctl;
3840
3841 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
3842 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
3843 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
3844 }
3845}
3846
françois romieue6de30d2011-01-03 15:08:37 +00003847static void rtl_enable_clock_request(struct pci_dev *pdev)
3848{
3849 struct net_device *dev = pci_get_drvdata(pdev);
3850 struct rtl8169_private *tp = netdev_priv(dev);
3851 int cap = tp->pcie_cap;
3852
3853 if (cap) {
3854 u16 ctl;
3855
3856 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
3857 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
3858 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
3859 }
3860}
3861
Francois Romieub726e492008-06-28 12:22:59 +02003862#define R8168_CPCMD_QUIRK_MASK (\
3863 EnableBist | \
3864 Mac_dbgo_oe | \
3865 Force_half_dup | \
3866 Force_rxflow_en | \
3867 Force_txflow_en | \
3868 Cxpl_dbg_sel | \
3869 ASF | \
3870 PktCntrDisable | \
3871 Mac_dbgo_sel)
3872
Francois Romieu219a1e92008-06-28 11:58:39 +02003873static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
3874{
Francois Romieub726e492008-06-28 12:22:59 +02003875 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3876
3877 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3878
Francois Romieu2e68ae42008-06-28 12:00:55 +02003879 rtl_tx_performance_tweak(pdev,
3880 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02003881}
3882
3883static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
3884{
3885 rtl_hw_start_8168bb(ioaddr, pdev);
Francois Romieub726e492008-06-28 12:22:59 +02003886
françois romieuf0298f82011-01-03 15:07:42 +00003887 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02003888
3889 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02003890}
3891
3892static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
3893{
Francois Romieub726e492008-06-28 12:22:59 +02003894 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
3895
3896 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3897
Francois Romieu219a1e92008-06-28 11:58:39 +02003898 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02003899
3900 rtl_disable_clock_request(pdev);
3901
3902 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02003903}
3904
Francois Romieuef3386f2008-06-29 12:24:30 +02003905static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
Francois Romieu219a1e92008-06-28 11:58:39 +02003906{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003907 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02003908 { 0x01, 0, 0x0001 },
3909 { 0x02, 0x0800, 0x1000 },
3910 { 0x03, 0, 0x0042 },
3911 { 0x06, 0x0080, 0x0000 },
3912 { 0x07, 0, 0x2000 }
3913 };
3914
françois romieu650e8d52011-01-03 15:08:29 +00003915 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02003916
3917 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
3918
Francois Romieu219a1e92008-06-28 11:58:39 +02003919 __rtl_hw_start_8168cp(ioaddr, pdev);
3920}
3921
Francois Romieuef3386f2008-06-29 12:24:30 +02003922static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
3923{
françois romieu650e8d52011-01-03 15:08:29 +00003924 rtl_csi_access_enable_2(ioaddr);
Francois Romieuef3386f2008-06-29 12:24:30 +02003925
3926 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3927
3928 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3929
3930 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3931}
3932
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003933static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
3934{
françois romieu650e8d52011-01-03 15:08:29 +00003935 rtl_csi_access_enable_2(ioaddr);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003936
3937 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3938
3939 /* Magic. */
3940 RTL_W8(DBG_REG, 0x20);
3941
françois romieuf0298f82011-01-03 15:07:42 +00003942 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003943
3944 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3945
3946 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3947}
3948
Francois Romieu219a1e92008-06-28 11:58:39 +02003949static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
3950{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003951 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02003952 { 0x02, 0x0800, 0x1000 },
3953 { 0x03, 0, 0x0002 },
3954 { 0x06, 0x0080, 0x0000 }
3955 };
3956
françois romieu650e8d52011-01-03 15:08:29 +00003957 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02003958
3959 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
3960
3961 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
3962
Francois Romieu219a1e92008-06-28 11:58:39 +02003963 __rtl_hw_start_8168cp(ioaddr, pdev);
3964}
3965
3966static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
3967{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003968 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02003969 { 0x01, 0, 0x0001 },
3970 { 0x03, 0x0400, 0x0220 }
3971 };
3972
françois romieu650e8d52011-01-03 15:08:29 +00003973 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02003974
3975 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
3976
Francois Romieu219a1e92008-06-28 11:58:39 +02003977 __rtl_hw_start_8168cp(ioaddr, pdev);
3978}
3979
Francois Romieu197ff762008-06-28 13:16:02 +02003980static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
3981{
3982 rtl_hw_start_8168c_2(ioaddr, pdev);
3983}
3984
Francois Romieu6fb07052008-06-29 11:54:28 +02003985static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
3986{
françois romieu650e8d52011-01-03 15:08:29 +00003987 rtl_csi_access_enable_2(ioaddr);
Francois Romieu6fb07052008-06-29 11:54:28 +02003988
3989 __rtl_hw_start_8168cp(ioaddr, pdev);
3990}
3991
Francois Romieu5b538df2008-07-20 16:22:45 +02003992static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
3993{
françois romieu650e8d52011-01-03 15:08:29 +00003994 rtl_csi_access_enable_2(ioaddr);
Francois Romieu5b538df2008-07-20 16:22:45 +02003995
3996 rtl_disable_clock_request(pdev);
3997
françois romieuf0298f82011-01-03 15:07:42 +00003998 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02003999
4000 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4001
4002 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4003}
4004
hayeswang4804b3b2011-03-21 01:50:29 +00004005static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
4006{
4007 rtl_csi_access_enable_1(ioaddr);
4008
4009 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4010
4011 RTL_W8(MaxTxPacketSize, TxPacketMax);
4012
4013 rtl_disable_clock_request(pdev);
4014}
4015
françois romieue6de30d2011-01-03 15:08:37 +00004016static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4017{
4018 static const struct ephy_info e_info_8168d_4[] = {
4019 { 0x0b, ~0, 0x48 },
4020 { 0x19, 0x20, 0x50 },
4021 { 0x0c, ~0, 0x20 }
4022 };
4023 int i;
4024
4025 rtl_csi_access_enable_1(ioaddr);
4026
4027 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4028
4029 RTL_W8(MaxTxPacketSize, TxPacketMax);
4030
4031 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4032 const struct ephy_info *e = e_info_8168d_4 + i;
4033 u16 w;
4034
4035 w = rtl_ephy_read(ioaddr, e->offset);
4036 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4037 }
4038
4039 rtl_enable_clock_request(pdev);
4040}
4041
hayeswang01dc7fe2011-03-21 01:50:28 +00004042static void rtl_hw_start_8168e(void __iomem *ioaddr, struct pci_dev *pdev)
4043{
4044 static const struct ephy_info e_info_8168e[] = {
4045 { 0x00, 0x0200, 0x0100 },
4046 { 0x00, 0x0000, 0x0004 },
4047 { 0x06, 0x0002, 0x0001 },
4048 { 0x06, 0x0000, 0x0030 },
4049 { 0x07, 0x0000, 0x2000 },
4050 { 0x00, 0x0000, 0x0020 },
4051 { 0x03, 0x5800, 0x2000 },
4052 { 0x03, 0x0000, 0x0001 },
4053 { 0x01, 0x0800, 0x1000 },
4054 { 0x07, 0x0000, 0x4000 },
4055 { 0x1e, 0x0000, 0x2000 },
4056 { 0x19, 0xffff, 0xfe6c },
4057 { 0x0a, 0x0000, 0x0040 }
4058 };
4059
4060 rtl_csi_access_enable_2(ioaddr);
4061
4062 rtl_ephy_init(ioaddr, e_info_8168e, ARRAY_SIZE(e_info_8168e));
4063
4064 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4065
4066 RTL_W8(MaxTxPacketSize, TxPacketMax);
4067
4068 rtl_disable_clock_request(pdev);
4069
4070 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02004071 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4072 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004073
Francois Romieucecb5fd2011-04-01 10:21:07 +02004074 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004075}
4076
Francois Romieu07ce4062007-02-23 23:36:39 +01004077static void rtl_hw_start_8168(struct net_device *dev)
4078{
Francois Romieu2dd99532007-06-11 23:22:52 +02004079 struct rtl8169_private *tp = netdev_priv(dev);
4080 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01004081 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu2dd99532007-06-11 23:22:52 +02004082
4083 RTL_W8(Cfg9346, Cfg9346_Unlock);
4084
françois romieuf0298f82011-01-03 15:07:42 +00004085 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004086
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004087 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004088
Francois Romieu0e485152007-02-20 00:00:26 +01004089 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004090
4091 RTL_W16(CPlusCmd, tp->cp_cmd);
4092
Francois Romieu0e485152007-02-20 00:00:26 +01004093 RTL_W16(IntrMitigate, 0x5151);
4094
4095 /* Work around for RxFIFO overflow. */
Ivan Vecerab5ba6d12011-01-27 12:24:11 +01004096 if (tp->mac_version == RTL_GIGA_MAC_VER_11 ||
4097 tp->mac_version == RTL_GIGA_MAC_VER_22) {
Francois Romieu0e485152007-02-20 00:00:26 +01004098 tp->intr_event |= RxFIFOOver | PCSTimeout;
4099 tp->intr_event &= ~RxOverflow;
4100 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004101
4102 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4103
Francois Romieub8363902008-06-01 12:31:57 +02004104 rtl_set_rx_mode(dev);
4105
4106 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4107 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004108
4109 RTL_R8(IntrMask);
4110
Francois Romieu219a1e92008-06-28 11:58:39 +02004111 switch (tp->mac_version) {
4112 case RTL_GIGA_MAC_VER_11:
4113 rtl_hw_start_8168bb(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004114 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004115
4116 case RTL_GIGA_MAC_VER_12:
4117 case RTL_GIGA_MAC_VER_17:
4118 rtl_hw_start_8168bef(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004119 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004120
4121 case RTL_GIGA_MAC_VER_18:
Francois Romieuef3386f2008-06-29 12:24:30 +02004122 rtl_hw_start_8168cp_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_19:
4126 rtl_hw_start_8168c_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004127 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004128
4129 case RTL_GIGA_MAC_VER_20:
4130 rtl_hw_start_8168c_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004131 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004132
Francois Romieu197ff762008-06-28 13:16:02 +02004133 case RTL_GIGA_MAC_VER_21:
4134 rtl_hw_start_8168c_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004135 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004136
Francois Romieu6fb07052008-06-29 11:54:28 +02004137 case RTL_GIGA_MAC_VER_22:
4138 rtl_hw_start_8168c_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004139 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004140
Francois Romieuef3386f2008-06-29 12:24:30 +02004141 case RTL_GIGA_MAC_VER_23:
4142 rtl_hw_start_8168cp_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004143 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004144
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004145 case RTL_GIGA_MAC_VER_24:
4146 rtl_hw_start_8168cp_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004147 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004148
Francois Romieu5b538df2008-07-20 16:22:45 +02004149 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00004150 case RTL_GIGA_MAC_VER_26:
4151 case RTL_GIGA_MAC_VER_27:
Francois Romieu5b538df2008-07-20 16:22:45 +02004152 rtl_hw_start_8168d(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004153 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004154
françois romieue6de30d2011-01-03 15:08:37 +00004155 case RTL_GIGA_MAC_VER_28:
4156 rtl_hw_start_8168d_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004157 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004158
hayeswang4804b3b2011-03-21 01:50:29 +00004159 case RTL_GIGA_MAC_VER_31:
4160 rtl_hw_start_8168dp(ioaddr, pdev);
4161 break;
4162
hayeswang01dc7fe2011-03-21 01:50:28 +00004163 case RTL_GIGA_MAC_VER_32:
4164 case RTL_GIGA_MAC_VER_33:
4165 rtl_hw_start_8168e(ioaddr, pdev);
4166 break;
françois romieue6de30d2011-01-03 15:08:37 +00004167
Francois Romieu219a1e92008-06-28 11:58:39 +02004168 default:
4169 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
4170 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00004171 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004172 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004173
Francois Romieu0e485152007-02-20 00:00:26 +01004174 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4175
Francois Romieub8363902008-06-01 12:31:57 +02004176 RTL_W8(Cfg9346, Cfg9346_Lock);
4177
Francois Romieu2dd99532007-06-11 23:22:52 +02004178 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004179
Francois Romieu0e485152007-02-20 00:00:26 +01004180 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01004181}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182
Francois Romieu2857ffb2008-08-02 21:08:49 +02004183#define R810X_CPCMD_QUIRK_MASK (\
4184 EnableBist | \
4185 Mac_dbgo_oe | \
4186 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00004187 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02004188 Force_txflow_en | \
4189 Cxpl_dbg_sel | \
4190 ASF | \
4191 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004192 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004193
4194static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4195{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004196 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004197 { 0x01, 0, 0x6e65 },
4198 { 0x02, 0, 0x091f },
4199 { 0x03, 0, 0xc2f9 },
4200 { 0x06, 0, 0xafb5 },
4201 { 0x07, 0, 0x0e00 },
4202 { 0x19, 0, 0xec80 },
4203 { 0x01, 0, 0x2e65 },
4204 { 0x01, 0, 0x6e65 }
4205 };
4206 u8 cfg1;
4207
françois romieu650e8d52011-01-03 15:08:29 +00004208 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004209
4210 RTL_W8(DBG_REG, FIX_NAK_1);
4211
4212 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4213
4214 RTL_W8(Config1,
4215 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
4216 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4217
4218 cfg1 = RTL_R8(Config1);
4219 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
4220 RTL_W8(Config1, cfg1 & ~LEDS0);
4221
Francois Romieu2857ffb2008-08-02 21:08:49 +02004222 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
4223}
4224
4225static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4226{
françois romieu650e8d52011-01-03 15:08:29 +00004227 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004228
4229 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4230
4231 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
4232 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004233}
4234
4235static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
4236{
4237 rtl_hw_start_8102e_2(ioaddr, pdev);
4238
4239 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
4240}
4241
Hayes Wang5a5e4442011-02-22 17:26:21 +08004242static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4243{
4244 static const struct ephy_info e_info_8105e_1[] = {
4245 { 0x07, 0, 0x4000 },
4246 { 0x19, 0, 0x0200 },
4247 { 0x19, 0, 0x0020 },
4248 { 0x1e, 0, 0x2000 },
4249 { 0x03, 0, 0x0001 },
4250 { 0x19, 0, 0x0100 },
4251 { 0x19, 0, 0x0004 },
4252 { 0x0a, 0, 0x0020 }
4253 };
4254
Francois Romieucecb5fd2011-04-01 10:21:07 +02004255 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004256 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
4257
Francois Romieucecb5fd2011-04-01 10:21:07 +02004258 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004259 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
4260
4261 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
4262 RTL_W8(DLLPR, RTL_R8(DLLPR) | PM_SWITCH);
4263
4264 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
4265}
4266
4267static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4268{
4269 rtl_hw_start_8105e_1(ioaddr, pdev);
4270 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
4271}
4272
Francois Romieu07ce4062007-02-23 23:36:39 +01004273static void rtl_hw_start_8101(struct net_device *dev)
4274{
Francois Romieucdf1a602007-06-11 23:29:50 +02004275 struct rtl8169_private *tp = netdev_priv(dev);
4276 void __iomem *ioaddr = tp->mmio_addr;
4277 struct pci_dev *pdev = tp->pci_dev;
4278
Francois Romieucecb5fd2011-04-01 10:21:07 +02004279 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
4280 tp->mac_version == RTL_GIGA_MAC_VER_16) {
Francois Romieu9c14cea2008-07-05 00:21:15 +02004281 int cap = tp->pcie_cap;
4282
4283 if (cap) {
4284 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
4285 PCI_EXP_DEVCTL_NOSNOOP_EN);
4286 }
Francois Romieucdf1a602007-06-11 23:29:50 +02004287 }
4288
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004289 RTL_W8(Cfg9346, Cfg9346_Unlock);
4290
Francois Romieu2857ffb2008-08-02 21:08:49 +02004291 switch (tp->mac_version) {
4292 case RTL_GIGA_MAC_VER_07:
4293 rtl_hw_start_8102e_1(ioaddr, pdev);
4294 break;
4295
4296 case RTL_GIGA_MAC_VER_08:
4297 rtl_hw_start_8102e_3(ioaddr, pdev);
4298 break;
4299
4300 case RTL_GIGA_MAC_VER_09:
4301 rtl_hw_start_8102e_2(ioaddr, pdev);
4302 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004303
4304 case RTL_GIGA_MAC_VER_29:
4305 rtl_hw_start_8105e_1(ioaddr, pdev);
4306 break;
4307 case RTL_GIGA_MAC_VER_30:
4308 rtl_hw_start_8105e_2(ioaddr, pdev);
4309 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02004310 }
4311
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004312 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02004313
françois romieuf0298f82011-01-03 15:07:42 +00004314 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02004315
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004316 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02004317
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004318 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02004319 RTL_W16(CPlusCmd, tp->cp_cmd);
4320
4321 RTL_W16(IntrMitigate, 0x0000);
4322
4323 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4324
4325 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4326 rtl_set_rx_tx_config_registers(tp);
4327
Francois Romieucdf1a602007-06-11 23:29:50 +02004328 RTL_R8(IntrMask);
4329
Francois Romieucdf1a602007-06-11 23:29:50 +02004330 rtl_set_rx_mode(dev);
4331
4332 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004333
Francois Romieu0e485152007-02-20 00:00:26 +01004334 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335}
4336
4337static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
4338{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 if (new_mtu < ETH_ZLEN || new_mtu > SafeMtu)
4340 return -EINVAL;
4341
4342 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00004343 netdev_update_features(dev);
4344
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00004345 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346}
4347
4348static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
4349{
Al Viro95e09182007-12-22 18:55:39 +00004350 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
4352}
4353
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004354static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
4355 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004357 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004358 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004359
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004360 kfree(*data_buff);
4361 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 rtl8169_make_unusable_by_asic(desc);
4363}
4364
4365static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
4366{
4367 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
4368
4369 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
4370}
4371
4372static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
4373 u32 rx_buf_sz)
4374{
4375 desc->addr = cpu_to_le64(mapping);
4376 wmb();
4377 rtl8169_mark_to_asic(desc, rx_buf_sz);
4378}
4379
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004380static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004382 return (void *)ALIGN((long)data, 16);
4383}
4384
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004385static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
4386 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004387{
4388 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004390 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004391 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004392 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004394 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
4395 if (!data)
4396 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01004397
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004398 if (rtl8169_align(data) != data) {
4399 kfree(data);
4400 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
4401 if (!data)
4402 return NULL;
4403 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004404
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004405 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004406 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004407 if (unlikely(dma_mapping_error(d, mapping))) {
4408 if (net_ratelimit())
4409 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004410 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412
4413 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004414 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004415
4416err_out:
4417 kfree(data);
4418 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419}
4420
4421static void rtl8169_rx_clear(struct rtl8169_private *tp)
4422{
Francois Romieu07d3f512007-02-21 22:40:46 +01004423 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424
4425 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004426 if (tp->Rx_databuff[i]) {
4427 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 tp->RxDescArray + i);
4429 }
4430 }
4431}
4432
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004433static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004435 desc->opts1 |= cpu_to_le32(RingEnd);
4436}
Francois Romieu5b0384f2006-08-16 16:00:01 +02004437
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004438static int rtl8169_rx_fill(struct rtl8169_private *tp)
4439{
4440 unsigned int i;
4441
4442 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004443 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02004444
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004445 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004447
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004448 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004449 if (!data) {
4450 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004451 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004452 }
4453 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004456 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
4457 return 0;
4458
4459err_out:
4460 rtl8169_rx_clear(tp);
4461 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462}
4463
4464static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4465{
4466 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
4467}
4468
4469static int rtl8169_init_ring(struct net_device *dev)
4470{
4471 struct rtl8169_private *tp = netdev_priv(dev);
4472
4473 rtl8169_init_ring_indexes(tp);
4474
4475 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004476 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004478 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479}
4480
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004481static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 struct TxDesc *desc)
4483{
4484 unsigned int len = tx_skb->len;
4485
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004486 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
4487
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 desc->opts1 = 0x00;
4489 desc->opts2 = 0x00;
4490 desc->addr = 0x00;
4491 tx_skb->len = 0;
4492}
4493
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004494static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
4495 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496{
4497 unsigned int i;
4498
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004499 for (i = 0; i < n; i++) {
4500 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 struct ring_info *tx_skb = tp->tx_skb + entry;
4502 unsigned int len = tx_skb->len;
4503
4504 if (len) {
4505 struct sk_buff *skb = tx_skb->skb;
4506
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004507 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 tp->TxDescArray + entry);
4509 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00004510 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 dev_kfree_skb(skb);
4512 tx_skb->skb = NULL;
4513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 }
4515 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004516}
4517
4518static void rtl8169_tx_clear(struct rtl8169_private *tp)
4519{
4520 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 tp->cur_tx = tp->dirty_tx = 0;
4522}
4523
David Howellsc4028952006-11-22 14:57:56 +00004524static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525{
4526 struct rtl8169_private *tp = netdev_priv(dev);
4527
David Howellsc4028952006-11-22 14:57:56 +00004528 PREPARE_DELAYED_WORK(&tp->task, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 schedule_delayed_work(&tp->task, 4);
4530}
4531
4532static void rtl8169_wait_for_quiescence(struct net_device *dev)
4533{
4534 struct rtl8169_private *tp = netdev_priv(dev);
4535 void __iomem *ioaddr = tp->mmio_addr;
4536
4537 synchronize_irq(dev->irq);
4538
4539 /* Wait for any pending NAPI task to complete */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004540 napi_disable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541
4542 rtl8169_irq_mask_and_ack(ioaddr);
4543
David S. Millerd1d08d12008-01-07 20:53:33 -08004544 tp->intr_mask = 0xffff;
4545 RTL_W16(IntrMask, tp->intr_event);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004546 napi_enable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547}
4548
David Howellsc4028952006-11-22 14:57:56 +00004549static void rtl8169_reinit_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550{
David Howellsc4028952006-11-22 14:57:56 +00004551 struct rtl8169_private *tp =
4552 container_of(work, struct rtl8169_private, task.work);
4553 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554 int ret;
4555
Francois Romieueb2a0212007-02-15 23:37:21 +01004556 rtnl_lock();
4557
4558 if (!netif_running(dev))
4559 goto out_unlock;
4560
4561 rtl8169_wait_for_quiescence(dev);
4562 rtl8169_close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563
4564 ret = rtl8169_open(dev);
4565 if (unlikely(ret < 0)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004566 if (net_ratelimit())
4567 netif_err(tp, drv, dev,
4568 "reinit failure (status = %d). Rescheduling\n",
4569 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 rtl8169_schedule_work(dev, rtl8169_reinit_task);
4571 }
Francois Romieueb2a0212007-02-15 23:37:21 +01004572
4573out_unlock:
4574 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575}
4576
David Howellsc4028952006-11-22 14:57:56 +00004577static void rtl8169_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578{
David Howellsc4028952006-11-22 14:57:56 +00004579 struct rtl8169_private *tp =
4580 container_of(work, struct rtl8169_private, task.work);
4581 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01004582 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583
Francois Romieueb2a0212007-02-15 23:37:21 +01004584 rtnl_lock();
4585
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 if (!netif_running(dev))
Francois Romieueb2a0212007-02-15 23:37:21 +01004587 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588
4589 rtl8169_wait_for_quiescence(dev);
4590
Francois Romieu56de4142011-03-15 17:29:31 +01004591 for (i = 0; i < NUM_RX_DESC; i++)
4592 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
4593
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 rtl8169_tx_clear(tp);
4595
Francois Romieu56de4142011-03-15 17:29:31 +01004596 rtl8169_init_ring_indexes(tp);
4597 rtl_hw_start(dev);
4598 netif_wake_queue(dev);
4599 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Francois Romieueb2a0212007-02-15 23:37:21 +01004600
4601out_unlock:
4602 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603}
4604
4605static void rtl8169_tx_timeout(struct net_device *dev)
4606{
4607 struct rtl8169_private *tp = netdev_priv(dev);
4608
françois romieue6de30d2011-01-03 15:08:37 +00004609 rtl8169_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610
4611 /* Let's wait a bit while any (async) irq lands on */
4612 rtl8169_schedule_work(dev, rtl8169_reset_task);
4613}
4614
4615static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07004616 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617{
4618 struct skb_shared_info *info = skb_shinfo(skb);
4619 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04004620 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004621 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622
4623 entry = tp->cur_tx;
4624 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
4625 skb_frag_t *frag = info->frags + cur_frag;
4626 dma_addr_t mapping;
4627 u32 status, len;
4628 void *addr;
4629
4630 entry = (entry + 1) % NUM_TX_DESC;
4631
4632 txd = tp->TxDescArray + entry;
4633 len = frag->size;
4634 addr = ((void *) page_address(frag->page)) + frag->page_offset;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004635 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004636 if (unlikely(dma_mapping_error(d, mapping))) {
4637 if (net_ratelimit())
4638 netif_err(tp, drv, tp->dev,
4639 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004640 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642
Francois Romieucecb5fd2011-04-01 10:21:07 +02004643 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07004644 status = opts[0] | len |
4645 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646
4647 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07004648 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 txd->addr = cpu_to_le64(mapping);
4650
4651 tp->tx_skb[entry].len = len;
4652 }
4653
4654 if (cur_frag) {
4655 tp->tx_skb[entry].skb = skb;
4656 txd->opts1 |= cpu_to_le32(LastFrag);
4657 }
4658
4659 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004660
4661err_out:
4662 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
4663 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664}
4665
Francois Romieu2b7b4312011-04-18 22:53:24 -07004666static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
4667 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668{
Francois Romieu2b7b4312011-04-18 22:53:24 -07004669 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00004670 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07004671 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672
Francois Romieu2b7b4312011-04-18 22:53:24 -07004673 if (mss) {
4674 opts[0] |= TD_LSO;
4675 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
4676 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07004677 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678
4679 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07004680 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07004682 opts[offset] |= info->checksum.udp;
4683 else
4684 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686}
4687
Stephen Hemminger613573252009-08-31 19:50:58 +00004688static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
4689 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690{
4691 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004692 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 struct TxDesc *txd = tp->TxDescArray + entry;
4694 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004695 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 dma_addr_t mapping;
4697 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07004698 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004699 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02004700
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004702 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004703 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 }
4705
4706 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004707 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004709 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004710 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004711 if (unlikely(dma_mapping_error(d, mapping))) {
4712 if (net_ratelimit())
4713 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004714 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716
4717 tp->tx_skb[entry].len = len;
4718 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719
Francois Romieu2b7b4312011-04-18 22:53:24 -07004720 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
4721 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004722
Francois Romieu2b7b4312011-04-18 22:53:24 -07004723 rtl8169_tso_csum(tp, skb, opts);
4724
4725 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004726 if (frags < 0)
4727 goto err_dma_1;
4728 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07004729 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004730 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07004731 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004732 tp->tx_skb[entry].skb = skb;
4733 }
4734
Francois Romieu2b7b4312011-04-18 22:53:24 -07004735 txd->opts2 = cpu_to_le32(opts[1]);
4736
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 wmb();
4738
Francois Romieucecb5fd2011-04-01 10:21:07 +02004739 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07004740 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 txd->opts1 = cpu_to_le32(status);
4742
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 tp->cur_tx += frags + 1;
4744
David Dillow4c020a92010-03-03 16:33:10 +00004745 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746
Francois Romieucecb5fd2011-04-01 10:21:07 +02004747 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748
4749 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
4750 netif_stop_queue(dev);
4751 smp_rmb();
4752 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
4753 netif_wake_queue(dev);
4754 }
4755
Stephen Hemminger613573252009-08-31 19:50:58 +00004756 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004758err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004759 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004760err_dma_0:
4761 dev_kfree_skb(skb);
4762 dev->stats.tx_dropped++;
4763 return NETDEV_TX_OK;
4764
4765err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004767 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00004768 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769}
4770
4771static void rtl8169_pcierr_interrupt(struct net_device *dev)
4772{
4773 struct rtl8169_private *tp = netdev_priv(dev);
4774 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 u16 pci_status, pci_cmd;
4776
4777 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
4778 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
4779
Joe Perchesbf82c182010-02-09 11:49:50 +00004780 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
4781 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782
4783 /*
4784 * The recovery sequence below admits a very elaborated explanation:
4785 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01004786 * - I did not see what else could be done;
4787 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 *
4789 * Feel free to adjust to your needs.
4790 */
Francois Romieua27993f2006-12-18 00:04:19 +01004791 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01004792 pci_cmd &= ~PCI_COMMAND_PARITY;
4793 else
4794 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
4795
4796 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797
4798 pci_write_config_word(pdev, PCI_STATUS,
4799 pci_status & (PCI_STATUS_DETECTED_PARITY |
4800 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
4801 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
4802
4803 /* The infamous DAC f*ckup only happens at boot time */
4804 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00004805 void __iomem *ioaddr = tp->mmio_addr;
4806
Joe Perchesbf82c182010-02-09 11:49:50 +00004807 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 tp->cp_cmd &= ~PCIDAC;
4809 RTL_W16(CPlusCmd, tp->cp_cmd);
4810 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 }
4812
françois romieue6de30d2011-01-03 15:08:37 +00004813 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01004814
4815 rtl8169_schedule_work(dev, rtl8169_reinit_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816}
4817
Francois Romieu07d3f512007-02-21 22:40:46 +01004818static void rtl8169_tx_interrupt(struct net_device *dev,
4819 struct rtl8169_private *tp,
4820 void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821{
4822 unsigned int dirty_tx, tx_left;
4823
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 dirty_tx = tp->dirty_tx;
4825 smp_rmb();
4826 tx_left = tp->cur_tx - dirty_tx;
4827
4828 while (tx_left > 0) {
4829 unsigned int entry = dirty_tx % NUM_TX_DESC;
4830 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 u32 status;
4832
4833 rmb();
4834 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
4835 if (status & DescOwn)
4836 break;
4837
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004838 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
4839 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 if (status & LastFrag) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00004841 dev->stats.tx_packets++;
4842 dev->stats.tx_bytes += tx_skb->skb->len;
Eric Dumazet87433bf2009-06-09 22:55:53 +00004843 dev_kfree_skb(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 tx_skb->skb = NULL;
4845 }
4846 dirty_tx++;
4847 tx_left--;
4848 }
4849
4850 if (tp->dirty_tx != dirty_tx) {
4851 tp->dirty_tx = dirty_tx;
4852 smp_wmb();
4853 if (netif_queue_stopped(dev) &&
4854 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
4855 netif_wake_queue(dev);
4856 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02004857 /*
4858 * 8168 hack: TxPoll requests are lost when the Tx packets are
4859 * too close. Let's kick an extra TxPoll request when a burst
4860 * of start_xmit activity is detected (if it is not detected,
4861 * it is slow enough). -- FR
4862 */
4863 smp_rmb();
4864 if (tp->cur_tx != dirty_tx)
4865 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866 }
4867}
4868
Francois Romieu126fa4b2005-05-12 20:09:17 -04004869static inline int rtl8169_fragmented_frame(u32 status)
4870{
4871 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
4872}
4873
Eric Dumazetadea1ac72010-09-05 20:04:05 -07004874static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 u32 status = opts1 & RxProtoMask;
4877
4878 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00004879 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 skb->ip_summed = CHECKSUM_UNNECESSARY;
4881 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07004882 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883}
4884
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004885static struct sk_buff *rtl8169_try_rx_copy(void *data,
4886 struct rtl8169_private *tp,
4887 int pkt_size,
4888 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02004890 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004891 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004893 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004894 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004895 prefetch(data);
4896 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
4897 if (skb)
4898 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004899 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
4900
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004901 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902}
4903
Francois Romieu07d3f512007-02-21 22:40:46 +01004904static int rtl8169_rx_interrupt(struct net_device *dev,
4905 struct rtl8169_private *tp,
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004906 void __iomem *ioaddr, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907{
4908 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004909 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 cur_rx = tp->cur_rx;
4912 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02004913 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914
Richard Dawe4dcb7d32005-05-27 21:12:00 +02004915 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04004917 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 u32 status;
4919
4920 rmb();
Francois Romieu126fa4b2005-05-12 20:09:17 -04004921 status = le32_to_cpu(desc->opts1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922
4923 if (status & DescOwn)
4924 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02004925 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004926 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
4927 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004928 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02004930 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02004932 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02004933 if (status & RxFOVF) {
4934 rtl8169_schedule_work(dev, rtl8169_reset_task);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004935 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02004936 }
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004937 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004939 struct sk_buff *skb;
Stephen Hemmingerb4496552007-06-17 01:06:49 +02004940 dma_addr_t addr = le64_to_cpu(desc->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 int pkt_size = (status & 0x00001FFF) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942
Francois Romieu126fa4b2005-05-12 20:09:17 -04004943 /*
4944 * The driver does not support incoming fragmented
4945 * frames. They are seen as a symptom of over-mtu
4946 * sized frames.
4947 */
4948 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02004949 dev->stats.rx_dropped++;
4950 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004951 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02004952 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04004953 }
4954
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004955 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
4956 tp, pkt_size, addr);
4957 rtl8169_mark_to_asic(desc, rx_buf_sz);
4958 if (!skb) {
4959 dev->stats.rx_dropped++;
4960 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 }
4962
Eric Dumazetadea1ac72010-09-05 20:04:05 -07004963 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 skb_put(skb, pkt_size);
4965 skb->protocol = eth_type_trans(skb, dev);
4966
Francois Romieu7a8fc772011-03-01 17:18:33 +01004967 rtl8169_rx_vlan_tag(desc, skb);
4968
Francois Romieu56de4142011-03-15 17:29:31 +01004969 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970
Francois Romieucebf8cc2007-10-18 12:06:54 +02004971 dev->stats.rx_bytes += pkt_size;
4972 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 }
Francois Romieu6dccd162007-02-13 23:38:05 +01004974
4975 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00004976 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01004977 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
4978 desc->opts2 = 0;
4979 cur_rx++;
4980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 }
4982
4983 count = cur_rx - tp->cur_rx;
4984 tp->cur_rx = cur_rx;
4985
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004986 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987
4988 return count;
4989}
4990
Francois Romieu07d3f512007-02-21 22:40:46 +01004991static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992{
Francois Romieu07d3f512007-02-21 22:40:46 +01004993 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996 int handled = 0;
Francois Romieu865c6522008-05-11 14:51:00 +02004997 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998
David Dillowf11a3772009-05-22 15:29:34 +00004999 /* loop handling interrupts until we have no new ones or
5000 * we hit a invalid/hotplug case.
5001 */
Francois Romieu865c6522008-05-11 14:51:00 +02005002 status = RTL_R16(IntrStatus);
David Dillowf11a3772009-05-22 15:29:34 +00005003 while (status && status != 0xffff) {
5004 handled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005
David Dillowf11a3772009-05-22 15:29:34 +00005006 /* Handle all of the error cases first. These will reset
5007 * the chip, so just exit the loop.
5008 */
5009 if (unlikely(!netif_running(dev))) {
5010 rtl8169_asic_down(ioaddr);
5011 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 }
David Dillowf11a3772009-05-22 15:29:34 +00005013
Francois Romieu1519e572011-02-03 12:02:36 +01005014 if (unlikely(status & RxFIFOOver)) {
5015 switch (tp->mac_version) {
5016 /* Work around for rx fifo overflow */
5017 case RTL_GIGA_MAC_VER_11:
5018 case RTL_GIGA_MAC_VER_22:
5019 case RTL_GIGA_MAC_VER_26:
5020 netif_stop_queue(dev);
5021 rtl8169_tx_timeout(dev);
5022 goto done;
Francois Romieuf60ac8e2011-02-03 17:27:52 +01005023 /* Testers needed. */
5024 case RTL_GIGA_MAC_VER_17:
5025 case RTL_GIGA_MAC_VER_19:
5026 case RTL_GIGA_MAC_VER_20:
5027 case RTL_GIGA_MAC_VER_21:
5028 case RTL_GIGA_MAC_VER_23:
5029 case RTL_GIGA_MAC_VER_24:
5030 case RTL_GIGA_MAC_VER_27:
5031 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00005032 case RTL_GIGA_MAC_VER_31:
Francois Romieu1519e572011-02-03 12:02:36 +01005033 /* Experimental science. Pktgen proof. */
5034 case RTL_GIGA_MAC_VER_12:
5035 case RTL_GIGA_MAC_VER_25:
5036 if (status == RxFIFOOver)
5037 goto done;
5038 break;
5039 default:
5040 break;
5041 }
David Dillowf11a3772009-05-22 15:29:34 +00005042 }
5043
5044 if (unlikely(status & SYSErr)) {
5045 rtl8169_pcierr_interrupt(dev);
5046 break;
5047 }
5048
5049 if (status & LinkChg)
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005050 __rtl8169_check_link_status(dev, tp, ioaddr, true);
David Dillowf11a3772009-05-22 15:29:34 +00005051
5052 /* We need to see the lastest version of tp->intr_mask to
5053 * avoid ignoring an MSI interrupt and having to wait for
5054 * another event which may never come.
5055 */
5056 smp_rmb();
5057 if (status & tp->intr_mask & tp->napi_event) {
5058 RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
5059 tp->intr_mask = ~tp->napi_event;
5060
5061 if (likely(napi_schedule_prep(&tp->napi)))
5062 __napi_schedule(&tp->napi);
Joe Perchesbf82c182010-02-09 11:49:50 +00005063 else
5064 netif_info(tp, intr, dev,
5065 "interrupt %04x in poll\n", status);
David Dillowf11a3772009-05-22 15:29:34 +00005066 }
5067
5068 /* We only get a new MSI interrupt when all active irq
5069 * sources on the chip have been acknowledged. So, ack
5070 * everything we've seen and check if new sources have become
5071 * active to avoid blocking all interrupts from the chip.
5072 */
5073 RTL_W16(IntrStatus,
5074 (status & RxFIFOOver) ? (status | RxOverflow) : status);
5075 status = RTL_R16(IntrStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 }
Francois Romieu1519e572011-02-03 12:02:36 +01005077done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 return IRQ_RETVAL(handled);
5079}
5080
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005081static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005083 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5084 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 void __iomem *ioaddr = tp->mmio_addr;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005086 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005088 work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 rtl8169_tx_interrupt(dev, tp, ioaddr);
5090
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005091 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005092 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00005093
5094 /* We need for force the visibility of tp->intr_mask
5095 * for other CPUs, as we can loose an MSI interrupt
5096 * and potentially wait for a retransmit timeout if we don't.
5097 * The posted write to IntrMask is safe, as it will
5098 * eventually make it to the chip and we won't loose anything
5099 * until it does.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 */
David Dillowf11a3772009-05-22 15:29:34 +00005101 tp->intr_mask = 0xffff;
David Dillow4c020a92010-03-03 16:33:10 +00005102 wmb();
Francois Romieu0e485152007-02-20 00:00:26 +01005103 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 }
5105
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005106 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108
Francois Romieu523a6092008-09-10 22:28:56 +02005109static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5110{
5111 struct rtl8169_private *tp = netdev_priv(dev);
5112
5113 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5114 return;
5115
5116 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5117 RTL_W32(RxMissed, 0);
5118}
5119
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120static void rtl8169_down(struct net_device *dev)
5121{
5122 struct rtl8169_private *tp = netdev_priv(dev);
5123 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124
Francois Romieu4876cc12011-03-11 21:07:11 +01005125 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126
5127 netif_stop_queue(dev);
5128
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005129 napi_disable(&tp->napi);
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005130
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 spin_lock_irq(&tp->lock);
5132
5133 rtl8169_asic_down(ioaddr);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005134 /*
5135 * At this point device interrupts can not be enabled in any function,
5136 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task,
5137 * rtl8169_reinit_task) and napi is disabled (rtl8169_poll).
5138 */
Francois Romieu523a6092008-09-10 22:28:56 +02005139 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140
5141 spin_unlock_irq(&tp->lock);
5142
5143 synchronize_irq(dev->irq);
5144
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 /* Give a racing hard_start_xmit a few cycles to complete. */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07005146 synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148 rtl8169_tx_clear(tp);
5149
5150 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00005151
5152 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153}
5154
5155static int rtl8169_close(struct net_device *dev)
5156{
5157 struct rtl8169_private *tp = netdev_priv(dev);
5158 struct pci_dev *pdev = tp->pci_dev;
5159
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005160 pm_runtime_get_sync(&pdev->dev);
5161
Francois Romieucecb5fd2011-04-01 10:21:07 +02005162 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08005163 rtl8169_update_counters(dev);
5164
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 rtl8169_down(dev);
5166
5167 free_irq(dev->irq, dev);
5168
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00005169 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5170 tp->RxPhyAddr);
5171 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5172 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 tp->TxDescArray = NULL;
5174 tp->RxDescArray = NULL;
5175
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005176 pm_runtime_put_sync(&pdev->dev);
5177
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 return 0;
5179}
5180
Francois Romieu07ce4062007-02-23 23:36:39 +01005181static void rtl_set_rx_mode(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182{
5183 struct rtl8169_private *tp = netdev_priv(dev);
5184 void __iomem *ioaddr = tp->mmio_addr;
5185 unsigned long flags;
5186 u32 mc_filter[2]; /* Multicast hash filter */
Francois Romieu07d3f512007-02-21 22:40:46 +01005187 int rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 u32 tmp = 0;
5189
5190 if (dev->flags & IFF_PROMISC) {
5191 /* Unconditionally log net taps. */
Joe Perchesbf82c182010-02-09 11:49:50 +00005192 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 rx_mode =
5194 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5195 AcceptAllPhys;
5196 mc_filter[1] = mc_filter[0] = 0xffffffff;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00005197 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
Joe Perches8e95a202009-12-03 07:58:21 +00005198 (dev->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 /* Too many to filter perfectly -- accept all multicasts. */
5200 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5201 mc_filter[1] = mc_filter[0] = 0xffffffff;
5202 } else {
Jiri Pirko22bedad2010-04-01 21:22:57 +00005203 struct netdev_hw_addr *ha;
Francois Romieu07d3f512007-02-21 22:40:46 +01005204
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 rx_mode = AcceptBroadcast | AcceptMyPhys;
5206 mc_filter[1] = mc_filter[0] = 0;
Jiri Pirko22bedad2010-04-01 21:22:57 +00005207 netdev_for_each_mc_addr(ha, dev) {
5208 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5210 rx_mode |= AcceptMulticast;
5211 }
5212 }
5213
5214 spin_lock_irqsave(&tp->lock, flags);
5215
5216 tmp = rtl8169_rx_config | rx_mode |
Francois Romieu2b7b4312011-04-18 22:53:24 -07005217 (RTL_R32(RxConfig) & RTL_RX_CONFIG_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218
Francois Romieuf887cce2008-07-17 22:24:18 +02005219 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
Francois Romieu1087f4f2007-12-26 22:46:05 +01005220 u32 data = mc_filter[0];
5221
5222 mc_filter[0] = swab32(mc_filter[1]);
5223 mc_filter[1] = swab32(data);
Francois Romieubcf0bf92006-07-26 23:14:13 +02005224 }
5225
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 RTL_W32(MAR0 + 4, mc_filter[1]);
Francois Romieu78f1cd02010-03-27 19:35:46 -07005227 RTL_W32(MAR0 + 0, mc_filter[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228
Francois Romieu57a9f232007-06-04 22:10:15 +02005229 RTL_W32(RxConfig, tmp);
5230
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 spin_unlock_irqrestore(&tp->lock, flags);
5232}
5233
5234/**
5235 * rtl8169_get_stats - Get rtl8169 read/write statistics
5236 * @dev: The Ethernet Device to get statistics for
5237 *
5238 * Get TX/RX statistics for rtl8169
5239 */
5240static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
5241{
5242 struct rtl8169_private *tp = netdev_priv(dev);
5243 void __iomem *ioaddr = tp->mmio_addr;
5244 unsigned long flags;
5245
5246 if (netif_running(dev)) {
5247 spin_lock_irqsave(&tp->lock, flags);
Francois Romieu523a6092008-09-10 22:28:56 +02005248 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 spin_unlock_irqrestore(&tp->lock, flags);
5250 }
Francois Romieu5b0384f2006-08-16 16:00:01 +02005251
Francois Romieucebf8cc2007-10-18 12:06:54 +02005252 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253}
5254
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005255static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01005256{
françois romieu065c27c2011-01-03 15:08:12 +00005257 struct rtl8169_private *tp = netdev_priv(dev);
5258
Francois Romieu5d06a992006-02-23 00:47:58 +01005259 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005260 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01005261
françois romieu065c27c2011-01-03 15:08:12 +00005262 rtl_pll_power_down(tp);
5263
Francois Romieu5d06a992006-02-23 00:47:58 +01005264 netif_device_detach(dev);
5265 netif_stop_queue(dev);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005266}
Francois Romieu5d06a992006-02-23 00:47:58 +01005267
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005268#ifdef CONFIG_PM
5269
5270static int rtl8169_suspend(struct device *device)
5271{
5272 struct pci_dev *pdev = to_pci_dev(device);
5273 struct net_device *dev = pci_get_drvdata(pdev);
5274
5275 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02005276
Francois Romieu5d06a992006-02-23 00:47:58 +01005277 return 0;
5278}
5279
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005280static void __rtl8169_resume(struct net_device *dev)
5281{
françois romieu065c27c2011-01-03 15:08:12 +00005282 struct rtl8169_private *tp = netdev_priv(dev);
5283
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005284 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00005285
5286 rtl_pll_power_up(tp);
5287
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005288 rtl8169_schedule_work(dev, rtl8169_reset_task);
5289}
5290
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005291static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01005292{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005293 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01005294 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005295 struct rtl8169_private *tp = netdev_priv(dev);
5296
5297 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01005298
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005299 if (netif_running(dev))
5300 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01005301
Francois Romieu5d06a992006-02-23 00:47:58 +01005302 return 0;
5303}
5304
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005305static int rtl8169_runtime_suspend(struct device *device)
5306{
5307 struct pci_dev *pdev = to_pci_dev(device);
5308 struct net_device *dev = pci_get_drvdata(pdev);
5309 struct rtl8169_private *tp = netdev_priv(dev);
5310
5311 if (!tp->TxDescArray)
5312 return 0;
5313
5314 spin_lock_irq(&tp->lock);
5315 tp->saved_wolopts = __rtl8169_get_wol(tp);
5316 __rtl8169_set_wol(tp, WAKE_ANY);
5317 spin_unlock_irq(&tp->lock);
5318
5319 rtl8169_net_suspend(dev);
5320
5321 return 0;
5322}
5323
5324static int rtl8169_runtime_resume(struct device *device)
5325{
5326 struct pci_dev *pdev = to_pci_dev(device);
5327 struct net_device *dev = pci_get_drvdata(pdev);
5328 struct rtl8169_private *tp = netdev_priv(dev);
5329
5330 if (!tp->TxDescArray)
5331 return 0;
5332
5333 spin_lock_irq(&tp->lock);
5334 __rtl8169_set_wol(tp, tp->saved_wolopts);
5335 tp->saved_wolopts = 0;
5336 spin_unlock_irq(&tp->lock);
5337
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005338 rtl8169_init_phy(dev, tp);
5339
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005340 __rtl8169_resume(dev);
5341
5342 return 0;
5343}
5344
5345static int rtl8169_runtime_idle(struct device *device)
5346{
5347 struct pci_dev *pdev = to_pci_dev(device);
5348 struct net_device *dev = pci_get_drvdata(pdev);
5349 struct rtl8169_private *tp = netdev_priv(dev);
5350
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005351 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005352}
5353
Alexey Dobriyan47145212009-12-14 18:00:08 -08005354static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02005355 .suspend = rtl8169_suspend,
5356 .resume = rtl8169_resume,
5357 .freeze = rtl8169_suspend,
5358 .thaw = rtl8169_resume,
5359 .poweroff = rtl8169_suspend,
5360 .restore = rtl8169_resume,
5361 .runtime_suspend = rtl8169_runtime_suspend,
5362 .runtime_resume = rtl8169_runtime_resume,
5363 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005364};
5365
5366#define RTL8169_PM_OPS (&rtl8169_pm_ops)
5367
5368#else /* !CONFIG_PM */
5369
5370#define RTL8169_PM_OPS NULL
5371
5372#endif /* !CONFIG_PM */
5373
Francois Romieu1765f952008-09-13 17:21:40 +02005374static void rtl_shutdown(struct pci_dev *pdev)
5375{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005376 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00005377 struct rtl8169_private *tp = netdev_priv(dev);
5378 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu1765f952008-09-13 17:21:40 +02005379
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005380 rtl8169_net_suspend(dev);
5381
Francois Romieucecb5fd2011-04-01 10:21:07 +02005382 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08005383 rtl_rar_set(tp, dev->perm_addr);
5384
françois romieu4bb3f522009-06-17 11:41:45 +00005385 spin_lock_irq(&tp->lock);
5386
5387 rtl8169_asic_down(ioaddr);
5388
5389 spin_unlock_irq(&tp->lock);
5390
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005391 if (system_state == SYSTEM_POWER_OFF) {
françois romieuca52efd2009-07-24 12:34:19 +00005392 /* WoL fails with some 8168 when the receiver is disabled. */
5393 if (tp->features & RTL_FEATURE_WOL) {
5394 pci_clear_master(pdev);
5395
5396 RTL_W8(ChipCmd, CmdRxEnb);
5397 /* PCI commit */
5398 RTL_R8(ChipCmd);
5399 }
5400
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005401 pci_wake_from_d3(pdev, true);
5402 pci_set_power_state(pdev, PCI_D3hot);
5403 }
5404}
Francois Romieu5d06a992006-02-23 00:47:58 +01005405
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406static struct pci_driver rtl8169_pci_driver = {
5407 .name = MODULENAME,
5408 .id_table = rtl8169_pci_tbl,
5409 .probe = rtl8169_init_one,
5410 .remove = __devexit_p(rtl8169_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02005411 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005412 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413};
5414
Francois Romieu07d3f512007-02-21 22:40:46 +01005415static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416{
Jeff Garzik29917622006-08-19 17:48:59 -04005417 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418}
5419
Francois Romieu07d3f512007-02-21 22:40:46 +01005420static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421{
5422 pci_unregister_driver(&rtl8169_pci_driver);
5423}
5424
5425module_init(rtl8169_init_module);
5426module_exit(rtl8169_cleanup_module);