blob: 73108249462e635d6e358b5215097b9c48859324 [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>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000025#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/dma-mapping.h>
Rafael J. Wysockie1759442010-03-14 14:33:51 +000027#include <linux/pm_runtime.h>
françois romieubca03d52011-01-03 15:07:31 +000028#include <linux/firmware.h>
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +000029#include <linux/pci-aspm.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040030#include <linux/prefetch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Francois Romieu99f252b2007-04-02 22:59:59 +020032#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/io.h>
34#include <asm/irq.h>
35
Francois Romieu865c6522008-05-11 14:51:00 +020036#define RTL8169_VERSION "2.3LK-NAPI"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define MODULENAME "r8169"
38#define PFX MODULENAME ": "
39
françois romieubca03d52011-01-03 15:07:31 +000040#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
41#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
hayeswang01dc7fe2011-03-21 01:50:28 +000042#define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
43#define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080044#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
françois romieubca03d52011-01-03 15:07:31 +000045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#ifdef RTL8169_DEBUG
47#define assert(expr) \
Francois Romieu5b0384f2006-08-16 16:00:01 +020048 if (!(expr)) { \
49 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
Harvey Harrisonb39d66a2008-08-20 16:52:04 -070050 #expr,__FILE__,__func__,__LINE__); \
Francois Romieu5b0384f2006-08-16 16:00:01 +020051 }
Joe Perches06fa7352007-10-18 21:15:00 +020052#define dprintk(fmt, args...) \
53 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#else
55#define assert(expr) do {} while (0)
56#define dprintk(fmt, args...) do {} while (0)
57#endif /* RTL8169_DEBUG */
58
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020059#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d2005-09-30 16:54:02 -070060 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#define TX_BUFFS_AVAIL(tp) \
63 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
66 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050067static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69/* MAC address length */
70#define MAC_ADDR_LEN 6
71
Francois Romieu9c14cea2008-07-05 00:21:15 +020072#define MAX_READ_REQUEST_SHIFT 12
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#define RX_FIFO_THRESH 7 /* 7 means NO threshold, Rx buffer level before first PCI xfer. */
74#define RX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
75#define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
77#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
78
79#define R8169_REGS_SIZE 256
80#define R8169_NAPI_WEIGHT 64
81#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
82#define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
83#define RX_BUF_SIZE 1536 /* Rx Buffer size */
84#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
85#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
86
87#define RTL8169_TX_TIMEOUT (6*HZ)
88#define RTL8169_PHY_TIMEOUT (10*HZ)
89
françois romieuea8dbdd2009-03-15 01:10:50 +000090#define RTL_EEPROM_SIG cpu_to_le32(0x8129)
91#define RTL_EEPROM_SIG_MASK cpu_to_le32(0xffff)
Francois Romieue1564ec2008-10-16 22:46:13 +020092#define RTL_EEPROM_SIG_ADDR 0x0000
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094/* write/read MMIO register */
95#define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
96#define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
97#define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
98#define RTL_R8(reg) readb (ioaddr + (reg))
99#define RTL_R16(reg) readw (ioaddr + (reg))
Junchang Wang06f555f2010-05-30 02:26:07 +0000100#define RTL_R32(reg) readl (ioaddr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +0200103 RTL_GIGA_MAC_VER_01 = 0,
104 RTL_GIGA_MAC_VER_02,
105 RTL_GIGA_MAC_VER_03,
106 RTL_GIGA_MAC_VER_04,
107 RTL_GIGA_MAC_VER_05,
108 RTL_GIGA_MAC_VER_06,
109 RTL_GIGA_MAC_VER_07,
110 RTL_GIGA_MAC_VER_08,
111 RTL_GIGA_MAC_VER_09,
112 RTL_GIGA_MAC_VER_10,
113 RTL_GIGA_MAC_VER_11,
114 RTL_GIGA_MAC_VER_12,
115 RTL_GIGA_MAC_VER_13,
116 RTL_GIGA_MAC_VER_14,
117 RTL_GIGA_MAC_VER_15,
118 RTL_GIGA_MAC_VER_16,
119 RTL_GIGA_MAC_VER_17,
120 RTL_GIGA_MAC_VER_18,
121 RTL_GIGA_MAC_VER_19,
122 RTL_GIGA_MAC_VER_20,
123 RTL_GIGA_MAC_VER_21,
124 RTL_GIGA_MAC_VER_22,
125 RTL_GIGA_MAC_VER_23,
126 RTL_GIGA_MAC_VER_24,
127 RTL_GIGA_MAC_VER_25,
128 RTL_GIGA_MAC_VER_26,
129 RTL_GIGA_MAC_VER_27,
130 RTL_GIGA_MAC_VER_28,
131 RTL_GIGA_MAC_VER_29,
132 RTL_GIGA_MAC_VER_30,
133 RTL_GIGA_MAC_VER_31,
134 RTL_GIGA_MAC_VER_32,
135 RTL_GIGA_MAC_VER_33,
136 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137};
138
Francois Romieu2b7b4312011-04-18 22:53:24 -0700139enum rtl_tx_desc_version {
140 RTL_TD_0 = 0,
141 RTL_TD_1 = 1,
142};
143
Francois Romieu85bffe62011-04-27 08:22:39 +0200144#define _R(NAME,TD,FW) \
145 { .name = NAME, .txd_version = TD, .fw_name = FW }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800147static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 const char *name;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700149 enum rtl_tx_desc_version txd_version;
Francois Romieu85bffe62011-04-27 08:22:39 +0200150 const char *fw_name;
151} rtl_chip_infos[] = {
152 /* PCI devices. */
153 [RTL_GIGA_MAC_VER_01] =
154 _R("RTL8169", RTL_TD_0, NULL),
155 [RTL_GIGA_MAC_VER_02] =
156 _R("RTL8169s", RTL_TD_0, NULL),
157 [RTL_GIGA_MAC_VER_03] =
158 _R("RTL8110s", RTL_TD_0, NULL),
159 [RTL_GIGA_MAC_VER_04] =
160 _R("RTL8169sb/8110sb", RTL_TD_0, NULL),
161 [RTL_GIGA_MAC_VER_05] =
162 _R("RTL8169sc/8110sc", RTL_TD_0, NULL),
163 [RTL_GIGA_MAC_VER_06] =
164 _R("RTL8169sc/8110sc", RTL_TD_0, NULL),
165 /* PCI-E devices. */
166 [RTL_GIGA_MAC_VER_07] =
167 _R("RTL8102e", RTL_TD_1, NULL),
168 [RTL_GIGA_MAC_VER_08] =
169 _R("RTL8102e", RTL_TD_1, NULL),
170 [RTL_GIGA_MAC_VER_09] =
171 _R("RTL8102e", RTL_TD_1, NULL),
172 [RTL_GIGA_MAC_VER_10] =
173 _R("RTL8101e", RTL_TD_0, NULL),
174 [RTL_GIGA_MAC_VER_11] =
175 _R("RTL8168b/8111b", RTL_TD_0, NULL),
176 [RTL_GIGA_MAC_VER_12] =
177 _R("RTL8168b/8111b", RTL_TD_0, NULL),
178 [RTL_GIGA_MAC_VER_13] =
179 _R("RTL8101e", RTL_TD_0, NULL),
180 [RTL_GIGA_MAC_VER_14] =
181 _R("RTL8100e", RTL_TD_0, NULL),
182 [RTL_GIGA_MAC_VER_15] =
183 _R("RTL8100e", RTL_TD_0, NULL),
184 [RTL_GIGA_MAC_VER_16] =
185 _R("RTL8101e", RTL_TD_0, NULL),
186 [RTL_GIGA_MAC_VER_17] =
187 _R("RTL8168b/8111b", RTL_TD_0, NULL),
188 [RTL_GIGA_MAC_VER_18] =
189 _R("RTL8168cp/8111cp", RTL_TD_1, NULL),
190 [RTL_GIGA_MAC_VER_19] =
191 _R("RTL8168c/8111c", RTL_TD_1, NULL),
192 [RTL_GIGA_MAC_VER_20] =
193 _R("RTL8168c/8111c", RTL_TD_1, NULL),
194 [RTL_GIGA_MAC_VER_21] =
195 _R("RTL8168c/8111c", RTL_TD_1, NULL),
196 [RTL_GIGA_MAC_VER_22] =
197 _R("RTL8168c/8111c", RTL_TD_1, NULL),
198 [RTL_GIGA_MAC_VER_23] =
199 _R("RTL8168cp/8111cp", RTL_TD_1, NULL),
200 [RTL_GIGA_MAC_VER_24] =
201 _R("RTL8168cp/8111cp", RTL_TD_1, NULL),
202 [RTL_GIGA_MAC_VER_25] =
203 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1),
204 [RTL_GIGA_MAC_VER_26] =
205 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2),
206 [RTL_GIGA_MAC_VER_27] =
207 _R("RTL8168dp/8111dp", RTL_TD_1, NULL),
208 [RTL_GIGA_MAC_VER_28] =
209 _R("RTL8168dp/8111dp", RTL_TD_1, NULL),
210 [RTL_GIGA_MAC_VER_29] =
211 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1),
212 [RTL_GIGA_MAC_VER_30] =
213 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1),
214 [RTL_GIGA_MAC_VER_31] =
215 _R("RTL8168dp/8111dp", RTL_TD_1, NULL),
216 [RTL_GIGA_MAC_VER_32] =
217 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1),
218 [RTL_GIGA_MAC_VER_33] =
219 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220};
221#undef _R
222
Francois Romieubcf0bf92006-07-26 23:14:13 +0200223enum cfg_version {
224 RTL_CFG_0 = 0x00,
225 RTL_CFG_1,
226 RTL_CFG_2
227};
228
Francois Romieu07ce4062007-02-23 23:36:39 +0100229static void rtl_hw_start_8169(struct net_device *);
230static void rtl_hw_start_8168(struct net_device *);
231static void rtl_hw_start_8101(struct net_device *);
232
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000233static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200234 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200235 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Francois Romieud81bf552006-09-20 21:31:20 +0200236 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100237 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200238 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
239 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 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);
746 if (!(RTL_R32(ERIDR) & ERIAR_FLAG))
747 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 */
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001625 static const struct {
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 }, *p = mac_info;
1694 u32 reg;
1695
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001696 reg = RTL_R32(TxConfig);
1697 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 p++;
1699 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02001700
1701 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1702 netif_notice(tp, probe, dev,
1703 "unknown MAC, using family default\n");
1704 tp->mac_version = default_version;
1705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706}
1707
1708static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1709{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001710 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711}
1712
Francois Romieu867763c2007-08-17 18:21:58 +02001713struct phy_reg {
1714 u16 reg;
1715 u16 val;
1716};
1717
françois romieu4da19632011-01-03 15:07:55 +00001718static void rtl_writephy_batch(struct rtl8169_private *tp,
1719 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02001720{
1721 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00001722 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02001723 regs++;
1724 }
1725}
1726
françois romieubca03d52011-01-03 15:07:31 +00001727#define PHY_READ 0x00000000
1728#define PHY_DATA_OR 0x10000000
1729#define PHY_DATA_AND 0x20000000
1730#define PHY_BJMPN 0x30000000
1731#define PHY_READ_EFUSE 0x40000000
1732#define PHY_READ_MAC_BYTE 0x50000000
1733#define PHY_WRITE_MAC_BYTE 0x60000000
1734#define PHY_CLEAR_READCOUNT 0x70000000
1735#define PHY_WRITE 0x80000000
1736#define PHY_READCOUNT_EQ_SKIP 0x90000000
1737#define PHY_COMP_EQ_SKIPN 0xa0000000
1738#define PHY_COMP_NEQ_SKIPN 0xb0000000
1739#define PHY_WRITE_PREVIOUS 0xc0000000
1740#define PHY_SKIPN 0xd0000000
1741#define PHY_DELAY_MS 0xe0000000
1742#define PHY_WRITE_ERI_WORD 0xf0000000
1743
1744static void
1745rtl_phy_write_fw(struct rtl8169_private *tp, const struct firmware *fw)
1746{
françois romieubca03d52011-01-03 15:07:31 +00001747 __le32 *phytable = (__le32 *)fw->data;
1748 struct net_device *dev = tp->dev;
hayeswang42b82dc2011-01-10 02:07:25 +00001749 size_t index, fw_size = fw->size / sizeof(*phytable);
1750 u32 predata, count;
françois romieubca03d52011-01-03 15:07:31 +00001751
1752 if (fw->size % sizeof(*phytable)) {
1753 netif_err(tp, probe, dev, "odd sized firmware %zd\n", fw->size);
1754 return;
1755 }
1756
hayeswang42b82dc2011-01-10 02:07:25 +00001757 for (index = 0; index < fw_size; index++) {
1758 u32 action = le32_to_cpu(phytable[index]);
1759 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00001760
hayeswang42b82dc2011-01-10 02:07:25 +00001761 switch(action & 0xf0000000) {
1762 case PHY_READ:
1763 case PHY_DATA_OR:
1764 case PHY_DATA_AND:
1765 case PHY_READ_EFUSE:
1766 case PHY_CLEAR_READCOUNT:
1767 case PHY_WRITE:
1768 case PHY_WRITE_PREVIOUS:
1769 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00001770 break;
1771
hayeswang42b82dc2011-01-10 02:07:25 +00001772 case PHY_BJMPN:
1773 if (regno > index) {
1774 netif_err(tp, probe, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001775 "Out of range of firmware\n");
hayeswang42b82dc2011-01-10 02:07:25 +00001776 return;
1777 }
1778 break;
1779 case PHY_READCOUNT_EQ_SKIP:
1780 if (index + 2 >= fw_size) {
1781 netif_err(tp, probe, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001782 "Out of range of firmware\n");
hayeswang42b82dc2011-01-10 02:07:25 +00001783 return;
1784 }
1785 break;
1786 case PHY_COMP_EQ_SKIPN:
1787 case PHY_COMP_NEQ_SKIPN:
1788 case PHY_SKIPN:
1789 if (index + 1 + regno >= fw_size) {
1790 netif_err(tp, probe, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001791 "Out of range of firmware\n");
hayeswang42b82dc2011-01-10 02:07:25 +00001792 return;
1793 }
1794 break;
1795
1796 case PHY_READ_MAC_BYTE:
1797 case PHY_WRITE_MAC_BYTE:
1798 case PHY_WRITE_ERI_WORD:
1799 default:
1800 netif_err(tp, probe, tp->dev,
1801 "Invalid action 0x%08x\n", action);
françois romieubca03d52011-01-03 15:07:31 +00001802 return;
1803 }
1804 }
1805
hayeswang42b82dc2011-01-10 02:07:25 +00001806 predata = 0;
1807 count = 0;
1808
1809 for (index = 0; index < fw_size; ) {
1810 u32 action = le32_to_cpu(phytable[index]);
françois romieubca03d52011-01-03 15:07:31 +00001811 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00001812 u32 regno = (action & 0x0fff0000) >> 16;
1813
1814 if (!action)
1815 break;
françois romieubca03d52011-01-03 15:07:31 +00001816
1817 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00001818 case PHY_READ:
1819 predata = rtl_readphy(tp, regno);
1820 count++;
1821 index++;
françois romieubca03d52011-01-03 15:07:31 +00001822 break;
hayeswang42b82dc2011-01-10 02:07:25 +00001823 case PHY_DATA_OR:
1824 predata |= data;
1825 index++;
1826 break;
1827 case PHY_DATA_AND:
1828 predata &= data;
1829 index++;
1830 break;
1831 case PHY_BJMPN:
1832 index -= regno;
1833 break;
1834 case PHY_READ_EFUSE:
1835 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
1836 index++;
1837 break;
1838 case PHY_CLEAR_READCOUNT:
1839 count = 0;
1840 index++;
1841 break;
1842 case PHY_WRITE:
1843 rtl_writephy(tp, regno, data);
1844 index++;
1845 break;
1846 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02001847 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00001848 break;
1849 case PHY_COMP_EQ_SKIPN:
1850 if (predata == data)
1851 index += regno;
1852 index++;
1853 break;
1854 case PHY_COMP_NEQ_SKIPN:
1855 if (predata != data)
1856 index += regno;
1857 index++;
1858 break;
1859 case PHY_WRITE_PREVIOUS:
1860 rtl_writephy(tp, regno, predata);
1861 index++;
1862 break;
1863 case PHY_SKIPN:
1864 index += regno + 1;
1865 break;
1866 case PHY_DELAY_MS:
1867 mdelay(data);
1868 index++;
1869 break;
1870
1871 case PHY_READ_MAC_BYTE:
1872 case PHY_WRITE_MAC_BYTE:
1873 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00001874 default:
1875 BUG();
1876 }
1877 }
1878}
1879
françois romieuf1e02ed2011-01-13 13:07:53 +00001880static void rtl_release_firmware(struct rtl8169_private *tp)
1881{
François Romieu953a12c2011-04-24 17:38:48 +02001882 if (!IS_ERR_OR_NULL(tp->fw))
1883 release_firmware(tp->fw);
1884 tp->fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00001885}
1886
François Romieu953a12c2011-04-24 17:38:48 +02001887static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00001888{
François Romieu953a12c2011-04-24 17:38:48 +02001889 const struct firmware *fw = tp->fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00001890
1891 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
François Romieu953a12c2011-04-24 17:38:48 +02001892 if (!IS_ERR_OR_NULL(fw))
1893 rtl_phy_write_fw(tp, fw);
1894}
1895
1896static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
1897{
1898 if (rtl_readphy(tp, reg) != val)
1899 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
1900 else
1901 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00001902}
1903
françois romieu4da19632011-01-03 15:07:55 +00001904static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001906 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00001907 { 0x1f, 0x0001 },
1908 { 0x06, 0x006e },
1909 { 0x08, 0x0708 },
1910 { 0x15, 0x4000 },
1911 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
françois romieu0b9b5712009-08-10 19:44:56 +00001913 { 0x1f, 0x0001 },
1914 { 0x03, 0x00a1 },
1915 { 0x02, 0x0008 },
1916 { 0x01, 0x0120 },
1917 { 0x00, 0x1000 },
1918 { 0x04, 0x0800 },
1919 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
françois romieu0b9b5712009-08-10 19:44:56 +00001921 { 0x03, 0xff41 },
1922 { 0x02, 0xdf60 },
1923 { 0x01, 0x0140 },
1924 { 0x00, 0x0077 },
1925 { 0x04, 0x7800 },
1926 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
françois romieu0b9b5712009-08-10 19:44:56 +00001928 { 0x03, 0x802f },
1929 { 0x02, 0x4f02 },
1930 { 0x01, 0x0409 },
1931 { 0x00, 0xf0f9 },
1932 { 0x04, 0x9800 },
1933 { 0x04, 0x9000 },
1934
1935 { 0x03, 0xdf01 },
1936 { 0x02, 0xdf20 },
1937 { 0x01, 0xff95 },
1938 { 0x00, 0xba00 },
1939 { 0x04, 0xa800 },
1940 { 0x04, 0xa000 },
1941
1942 { 0x03, 0xff41 },
1943 { 0x02, 0xdf20 },
1944 { 0x01, 0x0140 },
1945 { 0x00, 0x00bb },
1946 { 0x04, 0xb800 },
1947 { 0x04, 0xb000 },
1948
1949 { 0x03, 0xdf41 },
1950 { 0x02, 0xdc60 },
1951 { 0x01, 0x6340 },
1952 { 0x00, 0x007d },
1953 { 0x04, 0xd800 },
1954 { 0x04, 0xd000 },
1955
1956 { 0x03, 0xdf01 },
1957 { 0x02, 0xdf20 },
1958 { 0x01, 0x100a },
1959 { 0x00, 0xa0ff },
1960 { 0x04, 0xf800 },
1961 { 0x04, 0xf000 },
1962
1963 { 0x1f, 0x0000 },
1964 { 0x0b, 0x0000 },
1965 { 0x00, 0x9200 }
1966 };
1967
françois romieu4da19632011-01-03 15:07:55 +00001968 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969}
1970
françois romieu4da19632011-01-03 15:07:55 +00001971static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02001972{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001973 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02001974 { 0x1f, 0x0002 },
1975 { 0x01, 0x90d0 },
1976 { 0x1f, 0x0000 }
1977 };
1978
françois romieu4da19632011-01-03 15:07:55 +00001979 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02001980}
1981
françois romieu4da19632011-01-03 15:07:55 +00001982static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00001983{
1984 struct pci_dev *pdev = tp->pci_dev;
1985 u16 vendor_id, device_id;
1986
1987 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &vendor_id);
1988 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &device_id);
1989
1990 if ((vendor_id != PCI_VENDOR_ID_GIGABYTE) || (device_id != 0xe000))
1991 return;
1992
françois romieu4da19632011-01-03 15:07:55 +00001993 rtl_writephy(tp, 0x1f, 0x0001);
1994 rtl_writephy(tp, 0x10, 0xf01b);
1995 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00001996}
1997
françois romieu4da19632011-01-03 15:07:55 +00001998static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00001999{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002000 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002001 { 0x1f, 0x0001 },
2002 { 0x04, 0x0000 },
2003 { 0x03, 0x00a1 },
2004 { 0x02, 0x0008 },
2005 { 0x01, 0x0120 },
2006 { 0x00, 0x1000 },
2007 { 0x04, 0x0800 },
2008 { 0x04, 0x9000 },
2009 { 0x03, 0x802f },
2010 { 0x02, 0x4f02 },
2011 { 0x01, 0x0409 },
2012 { 0x00, 0xf099 },
2013 { 0x04, 0x9800 },
2014 { 0x04, 0xa000 },
2015 { 0x03, 0xdf01 },
2016 { 0x02, 0xdf20 },
2017 { 0x01, 0xff95 },
2018 { 0x00, 0xba00 },
2019 { 0x04, 0xa800 },
2020 { 0x04, 0xf000 },
2021 { 0x03, 0xdf01 },
2022 { 0x02, 0xdf20 },
2023 { 0x01, 0x101a },
2024 { 0x00, 0xa0ff },
2025 { 0x04, 0xf800 },
2026 { 0x04, 0x0000 },
2027 { 0x1f, 0x0000 },
2028
2029 { 0x1f, 0x0001 },
2030 { 0x10, 0xf41b },
2031 { 0x14, 0xfb54 },
2032 { 0x18, 0xf5c7 },
2033 { 0x1f, 0x0000 },
2034
2035 { 0x1f, 0x0001 },
2036 { 0x17, 0x0cc0 },
2037 { 0x1f, 0x0000 }
2038 };
2039
françois romieu4da19632011-01-03 15:07:55 +00002040 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002041
françois romieu4da19632011-01-03 15:07:55 +00002042 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002043}
2044
françois romieu4da19632011-01-03 15:07:55 +00002045static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002046{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002047 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002048 { 0x1f, 0x0001 },
2049 { 0x04, 0x0000 },
2050 { 0x03, 0x00a1 },
2051 { 0x02, 0x0008 },
2052 { 0x01, 0x0120 },
2053 { 0x00, 0x1000 },
2054 { 0x04, 0x0800 },
2055 { 0x04, 0x9000 },
2056 { 0x03, 0x802f },
2057 { 0x02, 0x4f02 },
2058 { 0x01, 0x0409 },
2059 { 0x00, 0xf099 },
2060 { 0x04, 0x9800 },
2061 { 0x04, 0xa000 },
2062 { 0x03, 0xdf01 },
2063 { 0x02, 0xdf20 },
2064 { 0x01, 0xff95 },
2065 { 0x00, 0xba00 },
2066 { 0x04, 0xa800 },
2067 { 0x04, 0xf000 },
2068 { 0x03, 0xdf01 },
2069 { 0x02, 0xdf20 },
2070 { 0x01, 0x101a },
2071 { 0x00, 0xa0ff },
2072 { 0x04, 0xf800 },
2073 { 0x04, 0x0000 },
2074 { 0x1f, 0x0000 },
2075
2076 { 0x1f, 0x0001 },
2077 { 0x0b, 0x8480 },
2078 { 0x1f, 0x0000 },
2079
2080 { 0x1f, 0x0001 },
2081 { 0x18, 0x67c7 },
2082 { 0x04, 0x2000 },
2083 { 0x03, 0x002f },
2084 { 0x02, 0x4360 },
2085 { 0x01, 0x0109 },
2086 { 0x00, 0x3022 },
2087 { 0x04, 0x2800 },
2088 { 0x1f, 0x0000 },
2089
2090 { 0x1f, 0x0001 },
2091 { 0x17, 0x0cc0 },
2092 { 0x1f, 0x0000 }
2093 };
2094
françois romieu4da19632011-01-03 15:07:55 +00002095 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002096}
2097
françois romieu4da19632011-01-03 15:07:55 +00002098static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002099{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002100 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002101 { 0x10, 0xf41b },
2102 { 0x1f, 0x0000 }
2103 };
2104
françois romieu4da19632011-01-03 15:07:55 +00002105 rtl_writephy(tp, 0x1f, 0x0001);
2106 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002107
françois romieu4da19632011-01-03 15:07:55 +00002108 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002109}
2110
françois romieu4da19632011-01-03 15:07:55 +00002111static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002112{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002113 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002114 { 0x1f, 0x0001 },
2115 { 0x10, 0xf41b },
2116 { 0x1f, 0x0000 }
2117 };
2118
françois romieu4da19632011-01-03 15:07:55 +00002119 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002120}
2121
françois romieu4da19632011-01-03 15:07:55 +00002122static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002123{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002124 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002125 { 0x1f, 0x0000 },
2126 { 0x1d, 0x0f00 },
2127 { 0x1f, 0x0002 },
2128 { 0x0c, 0x1ec8 },
2129 { 0x1f, 0x0000 }
2130 };
2131
françois romieu4da19632011-01-03 15:07:55 +00002132 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002133}
2134
françois romieu4da19632011-01-03 15:07:55 +00002135static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002136{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002137 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002138 { 0x1f, 0x0001 },
2139 { 0x1d, 0x3d98 },
2140 { 0x1f, 0x0000 }
2141 };
2142
françois romieu4da19632011-01-03 15:07:55 +00002143 rtl_writephy(tp, 0x1f, 0x0000);
2144 rtl_patchphy(tp, 0x14, 1 << 5);
2145 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002146
françois romieu4da19632011-01-03 15:07:55 +00002147 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002148}
2149
françois romieu4da19632011-01-03 15:07:55 +00002150static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002151{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002152 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002153 { 0x1f, 0x0001 },
2154 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002155 { 0x1f, 0x0002 },
2156 { 0x00, 0x88d4 },
2157 { 0x01, 0x82b1 },
2158 { 0x03, 0x7002 },
2159 { 0x08, 0x9e30 },
2160 { 0x09, 0x01f0 },
2161 { 0x0a, 0x5500 },
2162 { 0x0c, 0x00c8 },
2163 { 0x1f, 0x0003 },
2164 { 0x12, 0xc096 },
2165 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002166 { 0x1f, 0x0000 },
2167 { 0x1f, 0x0000 },
2168 { 0x09, 0x2000 },
2169 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002170 };
2171
françois romieu4da19632011-01-03 15:07:55 +00002172 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002173
françois romieu4da19632011-01-03 15:07:55 +00002174 rtl_patchphy(tp, 0x14, 1 << 5);
2175 rtl_patchphy(tp, 0x0d, 1 << 5);
2176 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002177}
2178
françois romieu4da19632011-01-03 15:07:55 +00002179static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002180{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002181 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002182 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002183 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002184 { 0x03, 0x802f },
2185 { 0x02, 0x4f02 },
2186 { 0x01, 0x0409 },
2187 { 0x00, 0xf099 },
2188 { 0x04, 0x9800 },
2189 { 0x04, 0x9000 },
2190 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002191 { 0x1f, 0x0002 },
2192 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002193 { 0x06, 0x0761 },
2194 { 0x1f, 0x0003 },
2195 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002196 { 0x1f, 0x0000 }
2197 };
2198
françois romieu4da19632011-01-03 15:07:55 +00002199 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002200
françois romieu4da19632011-01-03 15:07:55 +00002201 rtl_patchphy(tp, 0x16, 1 << 0);
2202 rtl_patchphy(tp, 0x14, 1 << 5);
2203 rtl_patchphy(tp, 0x0d, 1 << 5);
2204 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002205}
2206
françois romieu4da19632011-01-03 15:07:55 +00002207static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002208{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002209 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002210 { 0x1f, 0x0001 },
2211 { 0x12, 0x2300 },
2212 { 0x1d, 0x3d98 },
2213 { 0x1f, 0x0002 },
2214 { 0x0c, 0x7eb8 },
2215 { 0x06, 0x5461 },
2216 { 0x1f, 0x0003 },
2217 { 0x16, 0x0f0a },
2218 { 0x1f, 0x0000 }
2219 };
2220
françois romieu4da19632011-01-03 15:07:55 +00002221 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002222
françois romieu4da19632011-01-03 15:07:55 +00002223 rtl_patchphy(tp, 0x16, 1 << 0);
2224 rtl_patchphy(tp, 0x14, 1 << 5);
2225 rtl_patchphy(tp, 0x0d, 1 << 5);
2226 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002227}
2228
françois romieu4da19632011-01-03 15:07:55 +00002229static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002230{
françois romieu4da19632011-01-03 15:07:55 +00002231 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002232}
2233
françois romieubca03d52011-01-03 15:07:31 +00002234static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002235{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002236 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002237 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002238 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002239 { 0x06, 0x4064 },
2240 { 0x07, 0x2863 },
2241 { 0x08, 0x059c },
2242 { 0x09, 0x26b4 },
2243 { 0x0a, 0x6a19 },
2244 { 0x0b, 0xdcc8 },
2245 { 0x10, 0xf06d },
2246 { 0x14, 0x7f68 },
2247 { 0x18, 0x7fd9 },
2248 { 0x1c, 0xf0ff },
2249 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002250 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002251 { 0x12, 0xf49f },
2252 { 0x13, 0x070b },
2253 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002254 { 0x14, 0x94c0 },
2255
2256 /*
2257 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002258 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002259 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002260 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002261 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002262 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002263 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002264 { 0x06, 0x5561 },
2265
2266 /*
2267 * Can not link to 1Gbps with bad cable
2268 * Decrease SNR threshold form 21.07dB to 19.04dB
2269 */
2270 { 0x1f, 0x0001 },
2271 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002272
2273 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002274 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002275 };
françois romieubca03d52011-01-03 15:07:31 +00002276 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002277
françois romieu4da19632011-01-03 15:07:55 +00002278 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002279
françois romieubca03d52011-01-03 15:07:31 +00002280 /*
2281 * Rx Error Issue
2282 * Fine Tune Switching regulator parameter
2283 */
françois romieu4da19632011-01-03 15:07:55 +00002284 rtl_writephy(tp, 0x1f, 0x0002);
2285 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2286 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002287
françois romieudaf9df62009-10-07 12:44:20 +00002288 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002289 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002290 { 0x1f, 0x0002 },
2291 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002292 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002293 { 0x05, 0x8330 },
2294 { 0x06, 0x669a },
2295 { 0x1f, 0x0002 }
2296 };
2297 int val;
2298
françois romieu4da19632011-01-03 15:07:55 +00002299 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002300
françois romieu4da19632011-01-03 15:07:55 +00002301 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002302
2303 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002304 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002305 0x0065, 0x0066, 0x0067, 0x0068,
2306 0x0069, 0x006a, 0x006b, 0x006c
2307 };
2308 int i;
2309
françois romieu4da19632011-01-03 15:07:55 +00002310 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002311
2312 val &= 0xff00;
2313 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002314 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002315 }
2316 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002317 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002318 { 0x1f, 0x0002 },
2319 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002320 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002321 { 0x05, 0x8330 },
2322 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002323 };
2324
françois romieu4da19632011-01-03 15:07:55 +00002325 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002326 }
2327
françois romieubca03d52011-01-03 15:07:31 +00002328 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002329 rtl_writephy(tp, 0x1f, 0x0002);
2330 rtl_patchphy(tp, 0x0d, 0x0300);
2331 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002332
françois romieubca03d52011-01-03 15:07:31 +00002333 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002334 rtl_writephy(tp, 0x1f, 0x0002);
2335 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2336 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002337
françois romieu4da19632011-01-03 15:07:55 +00002338 rtl_writephy(tp, 0x1f, 0x0005);
2339 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002340
2341 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002342
françois romieu4da19632011-01-03 15:07:55 +00002343 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002344}
2345
françois romieubca03d52011-01-03 15:07:31 +00002346static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002347{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002348 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002349 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002350 { 0x1f, 0x0001 },
2351 { 0x06, 0x4064 },
2352 { 0x07, 0x2863 },
2353 { 0x08, 0x059c },
2354 { 0x09, 0x26b4 },
2355 { 0x0a, 0x6a19 },
2356 { 0x0b, 0xdcc8 },
2357 { 0x10, 0xf06d },
2358 { 0x14, 0x7f68 },
2359 { 0x18, 0x7fd9 },
2360 { 0x1c, 0xf0ff },
2361 { 0x1d, 0x3d9c },
2362 { 0x1f, 0x0003 },
2363 { 0x12, 0xf49f },
2364 { 0x13, 0x070b },
2365 { 0x1a, 0x05ad },
2366 { 0x14, 0x94c0 },
2367
françois romieubca03d52011-01-03 15:07:31 +00002368 /*
2369 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002370 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002371 */
françois romieudaf9df62009-10-07 12:44:20 +00002372 { 0x1f, 0x0002 },
2373 { 0x06, 0x5561 },
2374 { 0x1f, 0x0005 },
2375 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002376 { 0x06, 0x5561 },
2377
2378 /*
2379 * Can not link to 1Gbps with bad cable
2380 * Decrease SNR threshold form 21.07dB to 19.04dB
2381 */
2382 { 0x1f, 0x0001 },
2383 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002384
2385 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002386 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002387 };
françois romieubca03d52011-01-03 15:07:31 +00002388 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002389
françois romieu4da19632011-01-03 15:07:55 +00002390 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002391
2392 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002393 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002394 { 0x1f, 0x0002 },
2395 { 0x05, 0x669a },
2396 { 0x1f, 0x0005 },
2397 { 0x05, 0x8330 },
2398 { 0x06, 0x669a },
2399
2400 { 0x1f, 0x0002 }
2401 };
2402 int val;
2403
françois romieu4da19632011-01-03 15:07:55 +00002404 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002405
françois romieu4da19632011-01-03 15:07:55 +00002406 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002407 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002408 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002409 0x0065, 0x0066, 0x0067, 0x0068,
2410 0x0069, 0x006a, 0x006b, 0x006c
2411 };
2412 int i;
2413
françois romieu4da19632011-01-03 15:07:55 +00002414 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002415
2416 val &= 0xff00;
2417 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002418 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002419 }
2420 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002421 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002422 { 0x1f, 0x0002 },
2423 { 0x05, 0x2642 },
2424 { 0x1f, 0x0005 },
2425 { 0x05, 0x8330 },
2426 { 0x06, 0x2642 }
2427 };
2428
françois romieu4da19632011-01-03 15:07:55 +00002429 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002430 }
2431
françois romieubca03d52011-01-03 15:07:31 +00002432 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002433 rtl_writephy(tp, 0x1f, 0x0002);
2434 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2435 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002436
françois romieubca03d52011-01-03 15:07:31 +00002437 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002438 rtl_writephy(tp, 0x1f, 0x0002);
2439 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002440
françois romieu4da19632011-01-03 15:07:55 +00002441 rtl_writephy(tp, 0x1f, 0x0005);
2442 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002443
2444 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002445
françois romieu4da19632011-01-03 15:07:55 +00002446 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002447}
2448
françois romieu4da19632011-01-03 15:07:55 +00002449static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002450{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002451 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002452 { 0x1f, 0x0002 },
2453 { 0x10, 0x0008 },
2454 { 0x0d, 0x006c },
2455
2456 { 0x1f, 0x0000 },
2457 { 0x0d, 0xf880 },
2458
2459 { 0x1f, 0x0001 },
2460 { 0x17, 0x0cc0 },
2461
2462 { 0x1f, 0x0001 },
2463 { 0x0b, 0xa4d8 },
2464 { 0x09, 0x281c },
2465 { 0x07, 0x2883 },
2466 { 0x0a, 0x6b35 },
2467 { 0x1d, 0x3da4 },
2468 { 0x1c, 0xeffd },
2469 { 0x14, 0x7f52 },
2470 { 0x18, 0x7fc6 },
2471 { 0x08, 0x0601 },
2472 { 0x06, 0x4063 },
2473 { 0x10, 0xf074 },
2474 { 0x1f, 0x0003 },
2475 { 0x13, 0x0789 },
2476 { 0x12, 0xf4bd },
2477 { 0x1a, 0x04fd },
2478 { 0x14, 0x84b0 },
2479 { 0x1f, 0x0000 },
2480 { 0x00, 0x9200 },
2481
2482 { 0x1f, 0x0005 },
2483 { 0x01, 0x0340 },
2484 { 0x1f, 0x0001 },
2485 { 0x04, 0x4000 },
2486 { 0x03, 0x1d21 },
2487 { 0x02, 0x0c32 },
2488 { 0x01, 0x0200 },
2489 { 0x00, 0x5554 },
2490 { 0x04, 0x4800 },
2491 { 0x04, 0x4000 },
2492 { 0x04, 0xf000 },
2493 { 0x03, 0xdf01 },
2494 { 0x02, 0xdf20 },
2495 { 0x01, 0x101a },
2496 { 0x00, 0xa0ff },
2497 { 0x04, 0xf800 },
2498 { 0x04, 0xf000 },
2499 { 0x1f, 0x0000 },
2500
2501 { 0x1f, 0x0007 },
2502 { 0x1e, 0x0023 },
2503 { 0x16, 0x0000 },
2504 { 0x1f, 0x0000 }
2505 };
2506
françois romieu4da19632011-01-03 15:07:55 +00002507 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002508}
2509
françois romieue6de30d2011-01-03 15:08:37 +00002510static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2511{
2512 static const struct phy_reg phy_reg_init[] = {
2513 { 0x1f, 0x0001 },
2514 { 0x17, 0x0cc0 },
2515
2516 { 0x1f, 0x0007 },
2517 { 0x1e, 0x002d },
2518 { 0x18, 0x0040 },
2519 { 0x1f, 0x0000 }
2520 };
2521
2522 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2523 rtl_patchphy(tp, 0x0d, 1 << 5);
2524}
2525
hayeswang01dc7fe2011-03-21 01:50:28 +00002526static void rtl8168e_hw_phy_config(struct rtl8169_private *tp)
2527{
2528 static const struct phy_reg phy_reg_init[] = {
2529 /* Enable Delay cap */
2530 { 0x1f, 0x0005 },
2531 { 0x05, 0x8b80 },
2532 { 0x06, 0xc896 },
2533 { 0x1f, 0x0000 },
2534
2535 /* Channel estimation fine tune */
2536 { 0x1f, 0x0001 },
2537 { 0x0b, 0x6c20 },
2538 { 0x07, 0x2872 },
2539 { 0x1c, 0xefff },
2540 { 0x1f, 0x0003 },
2541 { 0x14, 0x6420 },
2542 { 0x1f, 0x0000 },
2543
2544 /* Update PFM & 10M TX idle timer */
2545 { 0x1f, 0x0007 },
2546 { 0x1e, 0x002f },
2547 { 0x15, 0x1919 },
2548 { 0x1f, 0x0000 },
2549
2550 { 0x1f, 0x0007 },
2551 { 0x1e, 0x00ac },
2552 { 0x18, 0x0006 },
2553 { 0x1f, 0x0000 }
2554 };
2555
Francois Romieu15ecd032011-04-27 13:52:22 -07002556 rtl_apply_firmware(tp);
2557
hayeswang01dc7fe2011-03-21 01:50:28 +00002558 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2559
2560 /* DCO enable for 10M IDLE Power */
2561 rtl_writephy(tp, 0x1f, 0x0007);
2562 rtl_writephy(tp, 0x1e, 0x0023);
2563 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2564 rtl_writephy(tp, 0x1f, 0x0000);
2565
2566 /* For impedance matching */
2567 rtl_writephy(tp, 0x1f, 0x0002);
2568 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002569 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002570
2571 /* PHY auto speed down */
2572 rtl_writephy(tp, 0x1f, 0x0007);
2573 rtl_writephy(tp, 0x1e, 0x002d);
2574 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2575 rtl_writephy(tp, 0x1f, 0x0000);
2576 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2577
2578 rtl_writephy(tp, 0x1f, 0x0005);
2579 rtl_writephy(tp, 0x05, 0x8b86);
2580 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2581 rtl_writephy(tp, 0x1f, 0x0000);
2582
2583 rtl_writephy(tp, 0x1f, 0x0005);
2584 rtl_writephy(tp, 0x05, 0x8b85);
2585 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2586 rtl_writephy(tp, 0x1f, 0x0007);
2587 rtl_writephy(tp, 0x1e, 0x0020);
2588 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2589 rtl_writephy(tp, 0x1f, 0x0006);
2590 rtl_writephy(tp, 0x00, 0x5a00);
2591 rtl_writephy(tp, 0x1f, 0x0000);
2592 rtl_writephy(tp, 0x0d, 0x0007);
2593 rtl_writephy(tp, 0x0e, 0x003c);
2594 rtl_writephy(tp, 0x0d, 0x4007);
2595 rtl_writephy(tp, 0x0e, 0x0000);
2596 rtl_writephy(tp, 0x0d, 0x0000);
2597}
2598
françois romieu4da19632011-01-03 15:07:55 +00002599static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02002600{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002601 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02002602 { 0x1f, 0x0003 },
2603 { 0x08, 0x441d },
2604 { 0x01, 0x9100 },
2605 { 0x1f, 0x0000 }
2606 };
2607
françois romieu4da19632011-01-03 15:07:55 +00002608 rtl_writephy(tp, 0x1f, 0x0000);
2609 rtl_patchphy(tp, 0x11, 1 << 12);
2610 rtl_patchphy(tp, 0x19, 1 << 13);
2611 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002612
françois romieu4da19632011-01-03 15:07:55 +00002613 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02002614}
2615
Hayes Wang5a5e4442011-02-22 17:26:21 +08002616static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
2617{
2618 static const struct phy_reg phy_reg_init[] = {
2619 { 0x1f, 0x0005 },
2620 { 0x1a, 0x0000 },
2621 { 0x1f, 0x0000 },
2622
2623 { 0x1f, 0x0004 },
2624 { 0x1c, 0x0000 },
2625 { 0x1f, 0x0000 },
2626
2627 { 0x1f, 0x0001 },
2628 { 0x15, 0x7701 },
2629 { 0x1f, 0x0000 }
2630 };
2631
2632 /* Disable ALDPS before ram code */
2633 rtl_writephy(tp, 0x1f, 0x0000);
2634 rtl_writephy(tp, 0x18, 0x0310);
2635 msleep(100);
2636
François Romieu953a12c2011-04-24 17:38:48 +02002637 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08002638
2639 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2640}
2641
Francois Romieu5615d9f2007-08-17 17:50:46 +02002642static void rtl_hw_phy_config(struct net_device *dev)
2643{
2644 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002645
2646 rtl8169_print_mac_version(tp);
2647
2648 switch (tp->mac_version) {
2649 case RTL_GIGA_MAC_VER_01:
2650 break;
2651 case RTL_GIGA_MAC_VER_02:
2652 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00002653 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002654 break;
2655 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00002656 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002657 break;
françois romieu2e9558562009-08-10 19:44:19 +00002658 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00002659 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002660 break;
françois romieu8c7006a2009-08-10 19:43:29 +00002661 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00002662 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00002663 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02002664 case RTL_GIGA_MAC_VER_07:
2665 case RTL_GIGA_MAC_VER_08:
2666 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00002667 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002668 break;
Francois Romieu236b8082008-05-30 16:11:48 +02002669 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00002670 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002671 break;
2672 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00002673 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002674 break;
2675 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00002676 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002677 break;
Francois Romieu867763c2007-08-17 18:21:58 +02002678 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00002679 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002680 break;
2681 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00002682 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002683 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02002684 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00002685 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002686 break;
Francois Romieu197ff762008-06-28 13:16:02 +02002687 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00002688 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02002689 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02002690 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00002691 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002692 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02002693 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002694 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00002695 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02002696 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02002697 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00002698 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002699 break;
2700 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00002701 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002702 break;
2703 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00002704 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02002705 break;
françois romieue6de30d2011-01-03 15:08:37 +00002706 case RTL_GIGA_MAC_VER_28:
2707 rtl8168d_4_hw_phy_config(tp);
2708 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08002709 case RTL_GIGA_MAC_VER_29:
2710 case RTL_GIGA_MAC_VER_30:
2711 rtl8105e_hw_phy_config(tp);
2712 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02002713 case RTL_GIGA_MAC_VER_31:
2714 /* None. */
2715 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00002716 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00002717 case RTL_GIGA_MAC_VER_33:
Francois Romieu15ecd032011-04-27 13:52:22 -07002718 rtl8168e_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00002719 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02002720
Francois Romieu5615d9f2007-08-17 17:50:46 +02002721 default:
2722 break;
2723 }
2724}
2725
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726static void rtl8169_phy_timer(unsigned long __opaque)
2727{
2728 struct net_device *dev = (struct net_device *)__opaque;
2729 struct rtl8169_private *tp = netdev_priv(dev);
2730 struct timer_list *timer = &tp->timer;
2731 void __iomem *ioaddr = tp->mmio_addr;
2732 unsigned long timeout = RTL8169_PHY_TIMEOUT;
2733
Francois Romieubcf0bf92006-07-26 23:14:13 +02002734 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 spin_lock_irq(&tp->lock);
2737
françois romieu4da19632011-01-03 15:07:55 +00002738 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02002739 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 * A busy loop could burn quite a few cycles on nowadays CPU.
2741 * Let's delay the execution of the timer for a few ticks.
2742 */
2743 timeout = HZ/10;
2744 goto out_mod_timer;
2745 }
2746
2747 if (tp->link_ok(ioaddr))
2748 goto out_unlock;
2749
Joe Perchesbf82c182010-02-09 11:49:50 +00002750 netif_warn(tp, link, dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751
françois romieu4da19632011-01-03 15:07:55 +00002752 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
2754out_mod_timer:
2755 mod_timer(timer, jiffies + timeout);
2756out_unlock:
2757 spin_unlock_irq(&tp->lock);
2758}
2759
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760#ifdef CONFIG_NET_POLL_CONTROLLER
2761/*
2762 * Polling 'interrupt' - used by things like netconsole to send skbs
2763 * without having to re-enable interrupts. It's not called while
2764 * the interrupt routine is executing.
2765 */
2766static void rtl8169_netpoll(struct net_device *dev)
2767{
2768 struct rtl8169_private *tp = netdev_priv(dev);
2769 struct pci_dev *pdev = tp->pci_dev;
2770
2771 disable_irq(pdev->irq);
David Howells7d12e782006-10-05 14:55:46 +01002772 rtl8169_interrupt(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 enable_irq(pdev->irq);
2774}
2775#endif
2776
2777static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
2778 void __iomem *ioaddr)
2779{
2780 iounmap(ioaddr);
2781 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00002782 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 pci_disable_device(pdev);
2784 free_netdev(dev);
2785}
2786
Francois Romieubf793292006-11-01 00:53:05 +01002787static void rtl8169_phy_reset(struct net_device *dev,
2788 struct rtl8169_private *tp)
2789{
Francois Romieu07d3f512007-02-21 22:40:46 +01002790 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01002791
françois romieu4da19632011-01-03 15:07:55 +00002792 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01002793 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00002794 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01002795 return;
2796 msleep(1);
2797 }
Joe Perchesbf82c182010-02-09 11:49:50 +00002798 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01002799}
2800
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002801static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002803 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002804
Francois Romieu5615d9f2007-08-17 17:50:46 +02002805 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002806
Marcus Sundberg773328942008-07-10 21:28:08 +02002807 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
2808 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2809 RTL_W8(0x82, 0x01);
2810 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002811
Francois Romieu6dccd162007-02-13 23:38:05 +01002812 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
2813
2814 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
2815 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002816
Francois Romieubcf0bf92006-07-26 23:14:13 +02002817 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002818 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2819 RTL_W8(0x82, 0x01);
2820 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00002821 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002822 }
2823
Francois Romieubf793292006-11-01 00:53:05 +01002824 rtl8169_phy_reset(dev, tp);
2825
Oliver Neukum54405cd2011-01-06 21:55:13 +01002826 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002827 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
2828 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
2829 (tp->mii.supports_gmii ?
2830 ADVERTISED_1000baseT_Half |
2831 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002832
Joe Perchesbf82c182010-02-09 11:49:50 +00002833 if (RTL_R8(PHYstatus) & TBI_Enable)
2834 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002835}
2836
Francois Romieu773d2022007-01-31 23:47:43 +01002837static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
2838{
2839 void __iomem *ioaddr = tp->mmio_addr;
2840 u32 high;
2841 u32 low;
2842
2843 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
2844 high = addr[4] | (addr[5] << 8);
2845
2846 spin_lock_irq(&tp->lock);
2847
2848 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00002849
Francois Romieu773d2022007-01-31 23:47:43 +01002850 RTL_W32(MAC4, high);
françois romieu908ba2b2010-04-26 11:42:58 +00002851 RTL_R32(MAC4);
2852
Francois Romieu78f1cd02010-03-27 19:35:46 -07002853 RTL_W32(MAC0, low);
françois romieu908ba2b2010-04-26 11:42:58 +00002854 RTL_R32(MAC0);
2855
Francois Romieu773d2022007-01-31 23:47:43 +01002856 RTL_W8(Cfg9346, Cfg9346_Lock);
2857
2858 spin_unlock_irq(&tp->lock);
2859}
2860
2861static int rtl_set_mac_address(struct net_device *dev, void *p)
2862{
2863 struct rtl8169_private *tp = netdev_priv(dev);
2864 struct sockaddr *addr = p;
2865
2866 if (!is_valid_ether_addr(addr->sa_data))
2867 return -EADDRNOTAVAIL;
2868
2869 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
2870
2871 rtl_rar_set(tp, dev->dev_addr);
2872
2873 return 0;
2874}
2875
Francois Romieu5f787a12006-08-17 13:02:36 +02002876static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2877{
2878 struct rtl8169_private *tp = netdev_priv(dev);
2879 struct mii_ioctl_data *data = if_mii(ifr);
2880
Francois Romieu8b4ab282008-11-19 22:05:25 -08002881 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
2882}
Francois Romieu5f787a12006-08-17 13:02:36 +02002883
Francois Romieucecb5fd2011-04-01 10:21:07 +02002884static int rtl_xmii_ioctl(struct rtl8169_private *tp,
2885 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08002886{
Francois Romieu5f787a12006-08-17 13:02:36 +02002887 switch (cmd) {
2888 case SIOCGMIIPHY:
2889 data->phy_id = 32; /* Internal PHY */
2890 return 0;
2891
2892 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00002893 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02002894 return 0;
2895
2896 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00002897 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02002898 return 0;
2899 }
2900 return -EOPNOTSUPP;
2901}
2902
Francois Romieu8b4ab282008-11-19 22:05:25 -08002903static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
2904{
2905 return -EOPNOTSUPP;
2906}
2907
Francois Romieu0e485152007-02-20 00:00:26 +01002908static const struct rtl_cfg_info {
2909 void (*hw_start)(struct net_device *);
2910 unsigned int region;
2911 unsigned int align;
2912 u16 intr_event;
2913 u16 napi_event;
Francois Romieuccdffb92008-07-26 14:26:06 +02002914 unsigned features;
Jean Delvaref21b75e2009-05-26 20:54:48 -07002915 u8 default_ver;
Francois Romieu0e485152007-02-20 00:00:26 +01002916} rtl_cfg_infos [] = {
2917 [RTL_CFG_0] = {
2918 .hw_start = rtl_hw_start_8169,
2919 .region = 1,
Francois Romieue9f63f32007-02-28 23:16:57 +01002920 .align = 0,
Francois Romieu0e485152007-02-20 00:00:26 +01002921 .intr_event = SYSErr | LinkChg | RxOverflow |
2922 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02002923 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07002924 .features = RTL_FEATURE_GMII,
2925 .default_ver = RTL_GIGA_MAC_VER_01,
Francois Romieu0e485152007-02-20 00:00:26 +01002926 },
2927 [RTL_CFG_1] = {
2928 .hw_start = rtl_hw_start_8168,
2929 .region = 2,
2930 .align = 8,
françois romieu53f57352010-11-08 13:23:05 +00002931 .intr_event = SYSErr | LinkChg | RxOverflow |
Francois Romieu0e485152007-02-20 00:00:26 +01002932 TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02002933 .napi_event = TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07002934 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
2935 .default_ver = RTL_GIGA_MAC_VER_11,
Francois Romieu0e485152007-02-20 00:00:26 +01002936 },
2937 [RTL_CFG_2] = {
2938 .hw_start = rtl_hw_start_8101,
2939 .region = 2,
2940 .align = 8,
2941 .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
2942 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02002943 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07002944 .features = RTL_FEATURE_MSI,
2945 .default_ver = RTL_GIGA_MAC_VER_13,
Francois Romieu0e485152007-02-20 00:00:26 +01002946 }
2947};
2948
Francois Romieufbac58f2007-10-04 22:51:38 +02002949/* Cfg9346_Unlock assumed. */
2950static unsigned rtl_try_msi(struct pci_dev *pdev, void __iomem *ioaddr,
2951 const struct rtl_cfg_info *cfg)
2952{
2953 unsigned msi = 0;
2954 u8 cfg2;
2955
2956 cfg2 = RTL_R8(Config2) & ~MSIEnable;
Francois Romieuccdffb92008-07-26 14:26:06 +02002957 if (cfg->features & RTL_FEATURE_MSI) {
Francois Romieufbac58f2007-10-04 22:51:38 +02002958 if (pci_enable_msi(pdev)) {
2959 dev_info(&pdev->dev, "no MSI. Back to INTx.\n");
2960 } else {
2961 cfg2 |= MSIEnable;
2962 msi = RTL_FEATURE_MSI;
2963 }
2964 }
2965 RTL_W8(Config2, cfg2);
2966 return msi;
2967}
2968
2969static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
2970{
2971 if (tp->features & RTL_FEATURE_MSI) {
2972 pci_disable_msi(pdev);
2973 tp->features &= ~RTL_FEATURE_MSI;
2974 }
2975}
2976
Francois Romieu8b4ab282008-11-19 22:05:25 -08002977static const struct net_device_ops rtl8169_netdev_ops = {
2978 .ndo_open = rtl8169_open,
2979 .ndo_stop = rtl8169_close,
2980 .ndo_get_stats = rtl8169_get_stats,
Stephen Hemminger00829822008-11-20 20:14:53 -08002981 .ndo_start_xmit = rtl8169_start_xmit,
Francois Romieu8b4ab282008-11-19 22:05:25 -08002982 .ndo_tx_timeout = rtl8169_tx_timeout,
2983 .ndo_validate_addr = eth_validate_addr,
2984 .ndo_change_mtu = rtl8169_change_mtu,
Michał Mirosław350fb322011-04-08 06:35:56 +00002985 .ndo_fix_features = rtl8169_fix_features,
2986 .ndo_set_features = rtl8169_set_features,
Francois Romieu8b4ab282008-11-19 22:05:25 -08002987 .ndo_set_mac_address = rtl_set_mac_address,
2988 .ndo_do_ioctl = rtl8169_ioctl,
2989 .ndo_set_multicast_list = rtl_set_rx_mode,
Francois Romieu8b4ab282008-11-19 22:05:25 -08002990#ifdef CONFIG_NET_POLL_CONTROLLER
2991 .ndo_poll_controller = rtl8169_netpoll,
2992#endif
2993
2994};
2995
françois romieuc0e45c12011-01-03 15:08:04 +00002996static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
2997{
2998 struct mdio_ops *ops = &tp->mdio_ops;
2999
3000 switch (tp->mac_version) {
3001 case RTL_GIGA_MAC_VER_27:
3002 ops->write = r8168dp_1_mdio_write;
3003 ops->read = r8168dp_1_mdio_read;
3004 break;
françois romieue6de30d2011-01-03 15:08:37 +00003005 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003006 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003007 ops->write = r8168dp_2_mdio_write;
3008 ops->read = r8168dp_2_mdio_read;
3009 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003010 default:
3011 ops->write = r8169_mdio_write;
3012 ops->read = r8169_mdio_read;
3013 break;
3014 }
3015}
3016
françois romieu065c27c2011-01-03 15:08:12 +00003017static void r810x_phy_power_down(struct rtl8169_private *tp)
3018{
3019 rtl_writephy(tp, 0x1f, 0x0000);
3020 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3021}
3022
3023static void r810x_phy_power_up(struct rtl8169_private *tp)
3024{
3025 rtl_writephy(tp, 0x1f, 0x0000);
3026 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3027}
3028
3029static void r810x_pll_power_down(struct rtl8169_private *tp)
3030{
3031 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3032 rtl_writephy(tp, 0x1f, 0x0000);
3033 rtl_writephy(tp, MII_BMCR, 0x0000);
3034 return;
3035 }
3036
3037 r810x_phy_power_down(tp);
3038}
3039
3040static void r810x_pll_power_up(struct rtl8169_private *tp)
3041{
3042 r810x_phy_power_up(tp);
3043}
3044
3045static void r8168_phy_power_up(struct rtl8169_private *tp)
3046{
3047 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003048 switch (tp->mac_version) {
3049 case RTL_GIGA_MAC_VER_11:
3050 case RTL_GIGA_MAC_VER_12:
3051 case RTL_GIGA_MAC_VER_17:
3052 case RTL_GIGA_MAC_VER_18:
3053 case RTL_GIGA_MAC_VER_19:
3054 case RTL_GIGA_MAC_VER_20:
3055 case RTL_GIGA_MAC_VER_21:
3056 case RTL_GIGA_MAC_VER_22:
3057 case RTL_GIGA_MAC_VER_23:
3058 case RTL_GIGA_MAC_VER_24:
3059 case RTL_GIGA_MAC_VER_25:
3060 case RTL_GIGA_MAC_VER_26:
3061 case RTL_GIGA_MAC_VER_27:
3062 case RTL_GIGA_MAC_VER_28:
3063 case RTL_GIGA_MAC_VER_31:
3064 rtl_writephy(tp, 0x0e, 0x0000);
3065 break;
3066 default:
3067 break;
3068 }
françois romieu065c27c2011-01-03 15:08:12 +00003069 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3070}
3071
3072static void r8168_phy_power_down(struct rtl8169_private *tp)
3073{
3074 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003075 switch (tp->mac_version) {
3076 case RTL_GIGA_MAC_VER_32:
3077 case RTL_GIGA_MAC_VER_33:
3078 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3079 break;
3080
3081 case RTL_GIGA_MAC_VER_11:
3082 case RTL_GIGA_MAC_VER_12:
3083 case RTL_GIGA_MAC_VER_17:
3084 case RTL_GIGA_MAC_VER_18:
3085 case RTL_GIGA_MAC_VER_19:
3086 case RTL_GIGA_MAC_VER_20:
3087 case RTL_GIGA_MAC_VER_21:
3088 case RTL_GIGA_MAC_VER_22:
3089 case RTL_GIGA_MAC_VER_23:
3090 case RTL_GIGA_MAC_VER_24:
3091 case RTL_GIGA_MAC_VER_25:
3092 case RTL_GIGA_MAC_VER_26:
3093 case RTL_GIGA_MAC_VER_27:
3094 case RTL_GIGA_MAC_VER_28:
3095 case RTL_GIGA_MAC_VER_31:
3096 rtl_writephy(tp, 0x0e, 0x0200);
3097 default:
3098 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3099 break;
3100 }
françois romieu065c27c2011-01-03 15:08:12 +00003101}
3102
3103static void r8168_pll_power_down(struct rtl8169_private *tp)
3104{
3105 void __iomem *ioaddr = tp->mmio_addr;
3106
Francois Romieucecb5fd2011-04-01 10:21:07 +02003107 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3108 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3109 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003110 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003111 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003112 }
françois romieu065c27c2011-01-03 15:08:12 +00003113
Francois Romieucecb5fd2011-04-01 10:21:07 +02003114 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3115 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00003116 (RTL_R16(CPlusCmd) & ASF)) {
3117 return;
3118 }
3119
hayeswang01dc7fe2011-03-21 01:50:28 +00003120 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3121 tp->mac_version == RTL_GIGA_MAC_VER_33)
3122 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3123
françois romieu065c27c2011-01-03 15:08:12 +00003124 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3125 rtl_writephy(tp, 0x1f, 0x0000);
3126 rtl_writephy(tp, MII_BMCR, 0x0000);
3127
3128 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3129 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3130 return;
3131 }
3132
3133 r8168_phy_power_down(tp);
3134
3135 switch (tp->mac_version) {
3136 case RTL_GIGA_MAC_VER_25:
3137 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003138 case RTL_GIGA_MAC_VER_27:
3139 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003140 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003141 case RTL_GIGA_MAC_VER_32:
3142 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003143 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3144 break;
3145 }
3146}
3147
3148static void r8168_pll_power_up(struct rtl8169_private *tp)
3149{
3150 void __iomem *ioaddr = tp->mmio_addr;
3151
Francois Romieucecb5fd2011-04-01 10:21:07 +02003152 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3153 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3154 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003155 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003156 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003157 }
françois romieu065c27c2011-01-03 15:08:12 +00003158
3159 switch (tp->mac_version) {
3160 case RTL_GIGA_MAC_VER_25:
3161 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003162 case RTL_GIGA_MAC_VER_27:
3163 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003164 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003165 case RTL_GIGA_MAC_VER_32:
3166 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003167 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3168 break;
3169 }
3170
3171 r8168_phy_power_up(tp);
3172}
3173
3174static void rtl_pll_power_op(struct rtl8169_private *tp,
3175 void (*op)(struct rtl8169_private *))
3176{
3177 if (op)
3178 op(tp);
3179}
3180
3181static void rtl_pll_power_down(struct rtl8169_private *tp)
3182{
3183 rtl_pll_power_op(tp, tp->pll_power_ops.down);
3184}
3185
3186static void rtl_pll_power_up(struct rtl8169_private *tp)
3187{
3188 rtl_pll_power_op(tp, tp->pll_power_ops.up);
3189}
3190
3191static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3192{
3193 struct pll_power_ops *ops = &tp->pll_power_ops;
3194
3195 switch (tp->mac_version) {
3196 case RTL_GIGA_MAC_VER_07:
3197 case RTL_GIGA_MAC_VER_08:
3198 case RTL_GIGA_MAC_VER_09:
3199 case RTL_GIGA_MAC_VER_10:
3200 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003201 case RTL_GIGA_MAC_VER_29:
3202 case RTL_GIGA_MAC_VER_30:
françois romieu065c27c2011-01-03 15:08:12 +00003203 ops->down = r810x_pll_power_down;
3204 ops->up = r810x_pll_power_up;
3205 break;
3206
3207 case RTL_GIGA_MAC_VER_11:
3208 case RTL_GIGA_MAC_VER_12:
3209 case RTL_GIGA_MAC_VER_17:
3210 case RTL_GIGA_MAC_VER_18:
3211 case RTL_GIGA_MAC_VER_19:
3212 case RTL_GIGA_MAC_VER_20:
3213 case RTL_GIGA_MAC_VER_21:
3214 case RTL_GIGA_MAC_VER_22:
3215 case RTL_GIGA_MAC_VER_23:
3216 case RTL_GIGA_MAC_VER_24:
3217 case RTL_GIGA_MAC_VER_25:
3218 case RTL_GIGA_MAC_VER_26:
3219 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003220 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003221 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003222 case RTL_GIGA_MAC_VER_32:
3223 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003224 ops->down = r8168_pll_power_down;
3225 ops->up = r8168_pll_power_up;
3226 break;
3227
3228 default:
3229 ops->down = NULL;
3230 ops->up = NULL;
3231 break;
3232 }
3233}
3234
Francois Romieu6f43adc2011-04-29 15:05:51 +02003235static void rtl_hw_reset(struct rtl8169_private *tp)
3236{
3237 void __iomem *ioaddr = tp->mmio_addr;
3238 int i;
3239
3240 /* Soft reset the chip. */
3241 RTL_W8(ChipCmd, CmdReset);
3242
3243 /* Check that the chip has finished the reset. */
3244 for (i = 0; i < 100; i++) {
3245 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3246 break;
3247 msleep_interruptible(1);
3248 }
3249}
3250
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003251static int __devinit
3252rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
3253{
Francois Romieu0e485152007-02-20 00:00:26 +01003254 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
3255 const unsigned int region = cfg->region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 struct rtl8169_private *tp;
Francois Romieuccdffb92008-07-26 14:26:06 +02003257 struct mii_if_info *mii;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003258 struct net_device *dev;
3259 void __iomem *ioaddr;
Francois Romieu2b7b4312011-04-18 22:53:24 -07003260 int chipset, i;
Francois Romieu07d3f512007-02-21 22:40:46 +01003261 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003263 if (netif_msg_drv(&debug)) {
3264 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
3265 MODULENAME, RTL8169_VERSION);
3266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 dev = alloc_etherdev(sizeof (*tp));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003269 if (!dev) {
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003270 if (netif_msg_drv(&debug))
Jeff Garzik9b91cf92006-06-27 11:39:50 -04003271 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003272 rc = -ENOMEM;
3273 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 }
3275
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieu8b4ab282008-11-19 22:05:25 -08003277 dev->netdev_ops = &rtl8169_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 tp = netdev_priv(dev);
David Howellsc4028952006-11-22 14:57:56 +00003279 tp->dev = dev;
Ivan Vecera21e197f2008-04-17 22:48:41 +02003280 tp->pci_dev = pdev;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003281 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
Francois Romieuccdffb92008-07-26 14:26:06 +02003283 mii = &tp->mii;
3284 mii->dev = dev;
3285 mii->mdio_read = rtl_mdio_read;
3286 mii->mdio_write = rtl_mdio_write;
3287 mii->phy_id_mask = 0x1f;
3288 mii->reg_num_mask = 0x1f;
3289 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
3290
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +00003291 /* disable ASPM completely as that cause random device stop working
3292 * problems as well as full system hangs for some PCIe devices users */
3293 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
3294 PCIE_LINK_STATE_CLKPM);
3295
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 /* enable device (incl. PCI PM wakeup and hotplug setup) */
3297 rc = pci_enable_device(pdev);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003298 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003299 netif_err(tp, probe, dev, "enable failure\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003300 goto err_out_free_dev_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 }
3302
françois romieu87aeec72010-04-26 11:42:06 +00003303 if (pci_set_mwi(pdev) < 0)
3304 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 /* make sure PCI base addr 1 is MMIO */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003307 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003308 netif_err(tp, probe, dev,
3309 "region #%d not an MMIO resource, aborting\n",
3310 region);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003312 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003314
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 /* check for weird/broken PCI region reporting */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003316 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003317 netif_err(tp, probe, dev,
3318 "Invalid PCI region size(s), aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003320 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 }
3322
3323 rc = pci_request_regions(pdev, MODULENAME);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003324 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003325 netif_err(tp, probe, dev, "could not request regions\n");
françois romieu87aeec72010-04-26 11:42:06 +00003326 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 }
3328
Hayes Wangd24e9aa2011-02-22 17:26:19 +08003329 tp->cp_cmd = RxChkSum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330
3331 if ((sizeof(dma_addr_t) > 4) &&
David S. Miller4300e8c2010-03-26 10:23:30 -07003332 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 tp->cp_cmd |= PCIDAC;
3334 dev->features |= NETIF_F_HIGHDMA;
3335 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07003336 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003338 netif_err(tp, probe, dev, "DMA configuration failed\n");
françois romieu87aeec72010-04-26 11:42:06 +00003339 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 }
3341 }
3342
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 /* ioremap MMIO region */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003344 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003345 if (!ioaddr) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003346 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 rc = -EIO;
françois romieu87aeec72010-04-26 11:42:06 +00003348 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 }
Francois Romieu6f43adc2011-04-29 15:05:51 +02003350 tp->mmio_addr = ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351
David S. Miller4300e8c2010-03-26 10:23:30 -07003352 tp->pcie_cap = pci_find_capability(pdev, PCI_CAP_ID_EXP);
3353 if (!tp->pcie_cap)
3354 netif_info(tp, probe, dev, "no PCI Express capability\n");
3355
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003356 RTL_W16(IntrMask, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357
Francois Romieu6f43adc2011-04-29 15:05:51 +02003358 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003360 RTL_W16(IntrStatus, 0xffff);
3361
françois romieuca52efd2009-07-24 12:34:19 +00003362 pci_set_master(pdev);
3363
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 /* Identify chip attached to board */
Francois Romieu5d320a22011-05-08 17:47:36 +02003365 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366
Francois Romieu7a8fc772011-03-01 17:18:33 +01003367 /*
3368 * Pretend we are using VLANs; This bypasses a nasty bug where
3369 * Interrupts stop flowing on high load on 8110SCd controllers.
3370 */
3371 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3372 tp->cp_cmd |= RxVlan;
3373
françois romieuc0e45c12011-01-03 15:08:04 +00003374 rtl_init_mdio_ops(tp);
françois romieu065c27c2011-01-03 15:08:12 +00003375 rtl_init_pll_power_ops(tp);
françois romieuc0e45c12011-01-03 15:08:04 +00003376
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 rtl8169_print_mac_version(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378
Francois Romieu85bffe62011-04-27 08:22:39 +02003379 chipset = tp->mac_version;
3380 tp->txd_version = rtl_chip_infos[chipset].txd_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381
Francois Romieu5d06a992006-02-23 00:47:58 +01003382 RTL_W8(Cfg9346, Cfg9346_Unlock);
3383 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
3384 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
Bruno Prémont20037fa2008-10-08 17:05:03 -07003385 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
3386 tp->features |= RTL_FEATURE_WOL;
3387 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
3388 tp->features |= RTL_FEATURE_WOL;
Francois Romieufbac58f2007-10-04 22:51:38 +02003389 tp->features |= rtl_try_msi(pdev, ioaddr, cfg);
Francois Romieu5d06a992006-02-23 00:47:58 +01003390 RTL_W8(Cfg9346, Cfg9346_Lock);
3391
Francois Romieu66ec5d42007-11-06 22:56:10 +01003392 if ((tp->mac_version <= RTL_GIGA_MAC_VER_06) &&
3393 (RTL_R8(PHYstatus) & TBI_Enable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 tp->set_speed = rtl8169_set_speed_tbi;
3395 tp->get_settings = rtl8169_gset_tbi;
3396 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
3397 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
3398 tp->link_ok = rtl8169_tbi_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003399 tp->do_ioctl = rtl_tbi_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 } else {
3401 tp->set_speed = rtl8169_set_speed_xmii;
3402 tp->get_settings = rtl8169_gset_xmii;
3403 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
3404 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
3405 tp->link_ok = rtl8169_xmii_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003406 tp->do_ioctl = rtl_xmii_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 }
3408
Francois Romieudf58ef52008-10-09 14:35:58 -07003409 spin_lock_init(&tp->lock);
3410
Ivan Vecera7bf6bf42008-09-23 22:46:29 +00003411 /* Get MAC address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 for (i = 0; i < MAC_ADDR_LEN; i++)
3413 dev->dev_addr[i] = RTL_R8(MAC0 + i);
John W. Linville6d6525b2005-09-12 10:48:57 -04003414 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
3418 dev->irq = pdev->irq;
3419 dev->base_addr = (unsigned long) ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003421 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422
Michał Mirosław350fb322011-04-08 06:35:56 +00003423 /* don't enable SG, IP_CSUM and TSO by default - it might not work
3424 * properly for all devices */
3425 dev->features |= NETIF_F_RXCSUM |
3426 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3427
3428 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3429 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3430 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3431 NETIF_F_HIGHDMA;
3432
3433 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3434 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
3435 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436
3437 tp->intr_mask = 0xffff;
Francois Romieu0e485152007-02-20 00:00:26 +01003438 tp->hw_start = cfg->hw_start;
3439 tp->intr_event = cfg->intr_event;
3440 tp->napi_event = cfg->napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441
Francois Romieu2efa53f2007-03-09 00:00:05 +01003442 init_timer(&tp->timer);
3443 tp->timer.data = (unsigned long) dev;
3444 tp->timer.function = rtl8169_phy_timer;
3445
François Romieu953a12c2011-04-24 17:38:48 +02003446 tp->fw = RTL_FIRMWARE_UNKNOWN;
3447
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 rc = register_netdev(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003449 if (rc < 0)
françois romieu87aeec72010-04-26 11:42:06 +00003450 goto err_out_msi_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451
3452 pci_set_drvdata(pdev, dev);
3453
Joe Perchesbf82c182010-02-09 11:49:50 +00003454 netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
Francois Romieu85bffe62011-04-27 08:22:39 +02003455 rtl_chip_infos[chipset].name, dev->base_addr, dev->dev_addr,
Joe Perchesbf82c182010-02-09 11:49:50 +00003456 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457
Francois Romieucecb5fd2011-04-01 10:21:07 +02003458 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3459 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3460 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00003461 rtl8168_driver_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003462 }
françois romieub646d902011-01-03 15:08:21 +00003463
Bruno Prémont8b76ab32008-10-08 17:06:25 -07003464 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465
Alan Sternf3ec4f82010-06-08 15:23:51 -04003466 if (pci_dev_run_wake(pdev))
3467 pm_runtime_put_noidle(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003468
Ivan Vecera0d672e92011-02-15 02:08:39 +00003469 netif_carrier_off(dev);
3470
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003471out:
3472 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473
françois romieu87aeec72010-04-26 11:42:06 +00003474err_out_msi_4:
Francois Romieufbac58f2007-10-04 22:51:38 +02003475 rtl_disable_msi(pdev, tp);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003476 iounmap(ioaddr);
françois romieu87aeec72010-04-26 11:42:06 +00003477err_out_free_res_3:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003478 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003479err_out_mwi_2:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003480 pci_clear_mwi(pdev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003481 pci_disable_device(pdev);
3482err_out_free_dev_1:
3483 free_netdev(dev);
3484 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485}
3486
Francois Romieu07d3f512007-02-21 22:40:46 +01003487static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488{
3489 struct net_device *dev = pci_get_drvdata(pdev);
3490 struct rtl8169_private *tp = netdev_priv(dev);
3491
Francois Romieucecb5fd2011-04-01 10:21:07 +02003492 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3493 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3494 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00003495 rtl8168_driver_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003496 }
françois romieub646d902011-01-03 15:08:21 +00003497
Tejun Heo23f333a2010-12-12 16:45:14 +01003498 cancel_delayed_work_sync(&tp->task);
Francois Romieueb2a0212007-02-15 23:37:21 +01003499
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 unregister_netdev(dev);
Ivan Veceracc098dc2009-11-29 23:12:52 -08003501
François Romieu953a12c2011-04-24 17:38:48 +02003502 rtl_release_firmware(tp);
3503
Alan Sternf3ec4f82010-06-08 15:23:51 -04003504 if (pci_dev_run_wake(pdev))
3505 pm_runtime_get_noresume(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003506
Ivan Veceracc098dc2009-11-29 23:12:52 -08003507 /* restore original MAC address */
3508 rtl_rar_set(tp, dev->perm_addr);
3509
Francois Romieufbac58f2007-10-04 22:51:38 +02003510 rtl_disable_msi(pdev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 rtl8169_release_board(pdev, dev, tp->mmio_addr);
3512 pci_set_drvdata(pdev, NULL);
3513}
3514
François Romieu953a12c2011-04-24 17:38:48 +02003515static void rtl_request_firmware(struct rtl8169_private *tp)
3516{
François Romieu953a12c2011-04-24 17:38:48 +02003517 /* Return early if the firmware is already loaded / cached. */
Francois Romieu31bd2042011-04-26 18:58:59 +02003518 if (IS_ERR(tp->fw)) {
3519 const char *name;
François Romieu953a12c2011-04-24 17:38:48 +02003520
Francois Romieu31bd2042011-04-26 18:58:59 +02003521 name = rtl_lookup_firmware_name(tp);
3522 if (name) {
François Romieu953a12c2011-04-24 17:38:48 +02003523 int rc;
3524
3525 rc = request_firmware(&tp->fw, name, &tp->pci_dev->dev);
Francois Romieu31bd2042011-04-26 18:58:59 +02003526 if (rc >= 0)
3527 return;
François Romieu953a12c2011-04-24 17:38:48 +02003528
Francois Romieu31bd2042011-04-26 18:58:59 +02003529 netif_warn(tp, ifup, tp->dev, "unable to load "
3530 "firmware patch %s (%d)\n", name, rc);
3531 }
3532 tp->fw = NULL;
3533 }
François Romieu953a12c2011-04-24 17:38:48 +02003534}
3535
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536static int rtl8169_open(struct net_device *dev)
3537{
3538 struct rtl8169_private *tp = netdev_priv(dev);
françois romieueee3a962011-01-08 02:17:26 +00003539 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu99f252b2007-04-02 22:59:59 +02003541 int retval = -ENOMEM;
3542
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003543 pm_runtime_get_sync(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544
Neil Hormanc0cd8842010-03-29 13:16:02 -07003545 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 * Rx and Tx desscriptors needs 256 bytes alignment.
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003547 * dma_alloc_coherent provides more.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 */
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003549 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
3550 &tp->TxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 if (!tp->TxDescArray)
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003552 goto err_pm_runtime_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003554 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
3555 &tp->RxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 if (!tp->RxDescArray)
Francois Romieu99f252b2007-04-02 22:59:59 +02003557 goto err_free_tx_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558
3559 retval = rtl8169_init_ring(dev);
3560 if (retval < 0)
Francois Romieu99f252b2007-04-02 22:59:59 +02003561 goto err_free_rx_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562
David Howellsc4028952006-11-22 14:57:56 +00003563 INIT_DELAYED_WORK(&tp->task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564
Francois Romieu99f252b2007-04-02 22:59:59 +02003565 smp_mb();
3566
François Romieu953a12c2011-04-24 17:38:48 +02003567 rtl_request_firmware(tp);
3568
Francois Romieufbac58f2007-10-04 22:51:38 +02003569 retval = request_irq(dev->irq, rtl8169_interrupt,
3570 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
Francois Romieu99f252b2007-04-02 22:59:59 +02003571 dev->name, dev);
3572 if (retval < 0)
François Romieu953a12c2011-04-24 17:38:48 +02003573 goto err_release_fw_2;
Francois Romieu99f252b2007-04-02 22:59:59 +02003574
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003575 napi_enable(&tp->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003576
françois romieueee3a962011-01-08 02:17:26 +00003577 rtl8169_init_phy(dev, tp);
3578
Michał Mirosław350fb322011-04-08 06:35:56 +00003579 rtl8169_set_features(dev, dev->features);
françois romieueee3a962011-01-08 02:17:26 +00003580
françois romieu065c27c2011-01-03 15:08:12 +00003581 rtl_pll_power_up(tp);
3582
Francois Romieu07ce4062007-02-23 23:36:39 +01003583 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003585 tp->saved_wolopts = 0;
3586 pm_runtime_put_noidle(&pdev->dev);
3587
françois romieueee3a962011-01-08 02:17:26 +00003588 rtl8169_check_link_status(dev, tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589out:
3590 return retval;
3591
François Romieu953a12c2011-04-24 17:38:48 +02003592err_release_fw_2:
3593 rtl_release_firmware(tp);
Francois Romieu99f252b2007-04-02 22:59:59 +02003594 rtl8169_rx_clear(tp);
3595err_free_rx_1:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003596 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
3597 tp->RxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003598 tp->RxDescArray = NULL;
Francois Romieu99f252b2007-04-02 22:59:59 +02003599err_free_tx_0:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003600 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
3601 tp->TxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003602 tp->TxDescArray = NULL;
3603err_pm_runtime_put:
3604 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 goto out;
3606}
3607
françois romieue6de30d2011-01-03 15:08:37 +00003608static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609{
françois romieue6de30d2011-01-03 15:08:37 +00003610 void __iomem *ioaddr = tp->mmio_addr;
3611
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 /* Disable interrupts */
3613 rtl8169_irq_mask_and_ack(ioaddr);
3614
Hayes Wang5d2e1952011-02-22 17:26:22 +08003615 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00003616 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3617 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieue6de30d2011-01-03 15:08:37 +00003618 while (RTL_R8(TxPoll) & NPQ)
3619 udelay(20);
3620
3621 }
3622
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 /* Reset the chipset */
3624 RTL_W8(ChipCmd, CmdReset);
3625
3626 /* PCI commit */
3627 RTL_R8(ChipCmd);
3628}
3629
Francois Romieu7f796d82007-06-11 23:04:41 +02003630static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01003631{
3632 void __iomem *ioaddr = tp->mmio_addr;
3633 u32 cfg = rtl8169_rx_config;
3634
Francois Romieu2b7b4312011-04-18 22:53:24 -07003635 cfg |= (RTL_R32(RxConfig) & RTL_RX_CONFIG_MASK);
Francois Romieu9cb427b2006-11-02 00:10:16 +01003636 RTL_W32(RxConfig, cfg);
3637
3638 /* Set DMA burst size and Interframe Gap Time */
3639 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
3640 (InterFrameGap << TxInterFrameGapShift));
3641}
3642
Francois Romieu07ce4062007-02-23 23:36:39 +01003643static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644{
3645 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646
Francois Romieu6f43adc2011-04-29 15:05:51 +02003647 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648
Francois Romieu07ce4062007-02-23 23:36:39 +01003649 tp->hw_start(dev);
3650
Francois Romieu07ce4062007-02-23 23:36:39 +01003651 netif_start_queue(dev);
3652}
3653
Francois Romieu7f796d82007-06-11 23:04:41 +02003654static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
3655 void __iomem *ioaddr)
3656{
3657 /*
3658 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
3659 * register to be written before TxDescAddrLow to work.
3660 * Switching from MMIO to I/O access fixes the issue as well.
3661 */
3662 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07003663 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02003664 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07003665 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02003666}
3667
3668static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
3669{
3670 u16 cmd;
3671
3672 cmd = RTL_R16(CPlusCmd);
3673 RTL_W16(CPlusCmd, cmd);
3674 return cmd;
3675}
3676
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07003677static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d82007-06-11 23:04:41 +02003678{
3679 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e82009-10-26 10:52:37 +00003680 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d82007-06-11 23:04:41 +02003681}
3682
Francois Romieu6dccd162007-02-13 23:38:05 +01003683static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
3684{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003685 static const struct {
Francois Romieu6dccd162007-02-13 23:38:05 +01003686 u32 mac_version;
3687 u32 clk;
3688 u32 val;
3689 } cfg2_info [] = {
3690 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
3691 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
3692 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
3693 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
3694 }, *p = cfg2_info;
3695 unsigned int i;
3696 u32 clk;
3697
3698 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01003699 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01003700 if ((p->mac_version == mac_version) && (p->clk == clk)) {
3701 RTL_W32(0x7c, p->val);
3702 break;
3703 }
3704 }
3705}
3706
Francois Romieu07ce4062007-02-23 23:36:39 +01003707static void rtl_hw_start_8169(struct net_device *dev)
3708{
3709 struct rtl8169_private *tp = netdev_priv(dev);
3710 void __iomem *ioaddr = tp->mmio_addr;
3711 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01003712
Francois Romieu9cb427b2006-11-02 00:10:16 +01003713 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
3714 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
3715 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
3716 }
3717
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02003719 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
3720 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
3721 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
3722 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01003723 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3724
françois romieuf0298f82011-01-03 15:07:42 +00003725 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726
Eric Dumazet6f0333b2010-10-11 11:17:47 +00003727 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728
Francois Romieucecb5fd2011-04-01 10:21:07 +02003729 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
3730 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
3731 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
3732 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02003733 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734
Francois Romieu7f796d82007-06-11 23:04:41 +02003735 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02003736
Francois Romieucecb5fd2011-04-01 10:21:07 +02003737 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
3738 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02003739 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02003741 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 }
3743
Francois Romieubcf0bf92006-07-26 23:14:13 +02003744 RTL_W16(CPlusCmd, tp->cp_cmd);
3745
Francois Romieu6dccd162007-02-13 23:38:05 +01003746 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
3747
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 /*
3749 * Undocumented corner. Supposedly:
3750 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
3751 */
3752 RTL_W16(IntrMitigate, 0x0000);
3753
Francois Romieu7f796d82007-06-11 23:04:41 +02003754 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01003755
Francois Romieucecb5fd2011-04-01 10:21:07 +02003756 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
3757 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
3758 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
3759 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02003760 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3761 rtl_set_rx_tx_config_registers(tp);
3762 }
3763
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02003765
3766 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
3767 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768
3769 RTL_W32(RxMissed, 0);
3770
Francois Romieu07ce4062007-02-23 23:36:39 +01003771 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772
3773 /* no early-rx interrupts */
3774 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01003775
3776 /* Enable all known interrupts by setting the interrupt mask. */
Francois Romieu0e485152007-02-20 00:00:26 +01003777 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01003778}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779
Francois Romieu9c14cea2008-07-05 00:21:15 +02003780static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
Francois Romieu458a9f62008-08-02 15:50:02 +02003781{
Francois Romieu9c14cea2008-07-05 00:21:15 +02003782 struct net_device *dev = pci_get_drvdata(pdev);
3783 struct rtl8169_private *tp = netdev_priv(dev);
3784 int cap = tp->pcie_cap;
Francois Romieu458a9f62008-08-02 15:50:02 +02003785
Francois Romieu9c14cea2008-07-05 00:21:15 +02003786 if (cap) {
3787 u16 ctl;
3788
3789 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
3790 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
3791 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
3792 }
Francois Romieu458a9f62008-08-02 15:50:02 +02003793}
3794
françois romieu650e8d52011-01-03 15:08:29 +00003795static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02003796{
3797 u32 csi;
3798
3799 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
françois romieu650e8d52011-01-03 15:08:29 +00003800 rtl_csi_write(ioaddr, 0x070c, csi | bits);
3801}
3802
françois romieue6de30d2011-01-03 15:08:37 +00003803static void rtl_csi_access_enable_1(void __iomem *ioaddr)
3804{
3805 rtl_csi_access_enable(ioaddr, 0x17000000);
3806}
3807
françois romieu650e8d52011-01-03 15:08:29 +00003808static void rtl_csi_access_enable_2(void __iomem *ioaddr)
3809{
3810 rtl_csi_access_enable(ioaddr, 0x27000000);
Francois Romieudacf8152008-08-02 20:44:13 +02003811}
3812
3813struct ephy_info {
3814 unsigned int offset;
3815 u16 mask;
3816 u16 bits;
3817};
3818
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003819static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02003820{
3821 u16 w;
3822
3823 while (len-- > 0) {
3824 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
3825 rtl_ephy_write(ioaddr, e->offset, w);
3826 e++;
3827 }
3828}
3829
Francois Romieub726e492008-06-28 12:22:59 +02003830static void rtl_disable_clock_request(struct pci_dev *pdev)
3831{
3832 struct net_device *dev = pci_get_drvdata(pdev);
3833 struct rtl8169_private *tp = netdev_priv(dev);
3834 int cap = tp->pcie_cap;
3835
3836 if (cap) {
3837 u16 ctl;
3838
3839 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
3840 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
3841 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
3842 }
3843}
3844
françois romieue6de30d2011-01-03 15:08:37 +00003845static void rtl_enable_clock_request(struct pci_dev *pdev)
3846{
3847 struct net_device *dev = pci_get_drvdata(pdev);
3848 struct rtl8169_private *tp = netdev_priv(dev);
3849 int cap = tp->pcie_cap;
3850
3851 if (cap) {
3852 u16 ctl;
3853
3854 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
3855 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
3856 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
3857 }
3858}
3859
Francois Romieub726e492008-06-28 12:22:59 +02003860#define R8168_CPCMD_QUIRK_MASK (\
3861 EnableBist | \
3862 Mac_dbgo_oe | \
3863 Force_half_dup | \
3864 Force_rxflow_en | \
3865 Force_txflow_en | \
3866 Cxpl_dbg_sel | \
3867 ASF | \
3868 PktCntrDisable | \
3869 Mac_dbgo_sel)
3870
Francois Romieu219a1e92008-06-28 11:58:39 +02003871static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
3872{
Francois Romieub726e492008-06-28 12:22:59 +02003873 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3874
3875 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3876
Francois Romieu2e68ae42008-06-28 12:00:55 +02003877 rtl_tx_performance_tweak(pdev,
3878 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02003879}
3880
3881static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
3882{
3883 rtl_hw_start_8168bb(ioaddr, pdev);
Francois Romieub726e492008-06-28 12:22:59 +02003884
françois romieuf0298f82011-01-03 15:07:42 +00003885 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02003886
3887 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02003888}
3889
3890static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
3891{
Francois Romieub726e492008-06-28 12:22:59 +02003892 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
3893
3894 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3895
Francois Romieu219a1e92008-06-28 11:58:39 +02003896 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02003897
3898 rtl_disable_clock_request(pdev);
3899
3900 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02003901}
3902
Francois Romieuef3386f2008-06-29 12:24:30 +02003903static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
Francois Romieu219a1e92008-06-28 11:58:39 +02003904{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003905 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02003906 { 0x01, 0, 0x0001 },
3907 { 0x02, 0x0800, 0x1000 },
3908 { 0x03, 0, 0x0042 },
3909 { 0x06, 0x0080, 0x0000 },
3910 { 0x07, 0, 0x2000 }
3911 };
3912
françois romieu650e8d52011-01-03 15:08:29 +00003913 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02003914
3915 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
3916
Francois Romieu219a1e92008-06-28 11:58:39 +02003917 __rtl_hw_start_8168cp(ioaddr, pdev);
3918}
3919
Francois Romieuef3386f2008-06-29 12:24:30 +02003920static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
3921{
françois romieu650e8d52011-01-03 15:08:29 +00003922 rtl_csi_access_enable_2(ioaddr);
Francois Romieuef3386f2008-06-29 12:24:30 +02003923
3924 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3925
3926 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3927
3928 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3929}
3930
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003931static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
3932{
françois romieu650e8d52011-01-03 15:08:29 +00003933 rtl_csi_access_enable_2(ioaddr);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003934
3935 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3936
3937 /* Magic. */
3938 RTL_W8(DBG_REG, 0x20);
3939
françois romieuf0298f82011-01-03 15:07:42 +00003940 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003941
3942 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3943
3944 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3945}
3946
Francois Romieu219a1e92008-06-28 11:58:39 +02003947static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
3948{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003949 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02003950 { 0x02, 0x0800, 0x1000 },
3951 { 0x03, 0, 0x0002 },
3952 { 0x06, 0x0080, 0x0000 }
3953 };
3954
françois romieu650e8d52011-01-03 15:08:29 +00003955 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02003956
3957 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
3958
3959 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
3960
Francois Romieu219a1e92008-06-28 11:58:39 +02003961 __rtl_hw_start_8168cp(ioaddr, pdev);
3962}
3963
3964static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
3965{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003966 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02003967 { 0x01, 0, 0x0001 },
3968 { 0x03, 0x0400, 0x0220 }
3969 };
3970
françois romieu650e8d52011-01-03 15:08:29 +00003971 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02003972
3973 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
3974
Francois Romieu219a1e92008-06-28 11:58:39 +02003975 __rtl_hw_start_8168cp(ioaddr, pdev);
3976}
3977
Francois Romieu197ff762008-06-28 13:16:02 +02003978static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
3979{
3980 rtl_hw_start_8168c_2(ioaddr, pdev);
3981}
3982
Francois Romieu6fb07052008-06-29 11:54:28 +02003983static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
3984{
françois romieu650e8d52011-01-03 15:08:29 +00003985 rtl_csi_access_enable_2(ioaddr);
Francois Romieu6fb07052008-06-29 11:54:28 +02003986
3987 __rtl_hw_start_8168cp(ioaddr, pdev);
3988}
3989
Francois Romieu5b538df2008-07-20 16:22:45 +02003990static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
3991{
françois romieu650e8d52011-01-03 15:08:29 +00003992 rtl_csi_access_enable_2(ioaddr);
Francois Romieu5b538df2008-07-20 16:22:45 +02003993
3994 rtl_disable_clock_request(pdev);
3995
françois romieuf0298f82011-01-03 15:07:42 +00003996 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02003997
3998 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3999
4000 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4001}
4002
hayeswang4804b3b2011-03-21 01:50:29 +00004003static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
4004{
4005 rtl_csi_access_enable_1(ioaddr);
4006
4007 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4008
4009 RTL_W8(MaxTxPacketSize, TxPacketMax);
4010
4011 rtl_disable_clock_request(pdev);
4012}
4013
françois romieue6de30d2011-01-03 15:08:37 +00004014static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4015{
4016 static const struct ephy_info e_info_8168d_4[] = {
4017 { 0x0b, ~0, 0x48 },
4018 { 0x19, 0x20, 0x50 },
4019 { 0x0c, ~0, 0x20 }
4020 };
4021 int i;
4022
4023 rtl_csi_access_enable_1(ioaddr);
4024
4025 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4026
4027 RTL_W8(MaxTxPacketSize, TxPacketMax);
4028
4029 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4030 const struct ephy_info *e = e_info_8168d_4 + i;
4031 u16 w;
4032
4033 w = rtl_ephy_read(ioaddr, e->offset);
4034 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4035 }
4036
4037 rtl_enable_clock_request(pdev);
4038}
4039
hayeswang01dc7fe2011-03-21 01:50:28 +00004040static void rtl_hw_start_8168e(void __iomem *ioaddr, struct pci_dev *pdev)
4041{
4042 static const struct ephy_info e_info_8168e[] = {
4043 { 0x00, 0x0200, 0x0100 },
4044 { 0x00, 0x0000, 0x0004 },
4045 { 0x06, 0x0002, 0x0001 },
4046 { 0x06, 0x0000, 0x0030 },
4047 { 0x07, 0x0000, 0x2000 },
4048 { 0x00, 0x0000, 0x0020 },
4049 { 0x03, 0x5800, 0x2000 },
4050 { 0x03, 0x0000, 0x0001 },
4051 { 0x01, 0x0800, 0x1000 },
4052 { 0x07, 0x0000, 0x4000 },
4053 { 0x1e, 0x0000, 0x2000 },
4054 { 0x19, 0xffff, 0xfe6c },
4055 { 0x0a, 0x0000, 0x0040 }
4056 };
4057
4058 rtl_csi_access_enable_2(ioaddr);
4059
4060 rtl_ephy_init(ioaddr, e_info_8168e, ARRAY_SIZE(e_info_8168e));
4061
4062 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4063
4064 RTL_W8(MaxTxPacketSize, TxPacketMax);
4065
4066 rtl_disable_clock_request(pdev);
4067
4068 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02004069 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4070 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004071
Francois Romieucecb5fd2011-04-01 10:21:07 +02004072 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004073}
4074
Francois Romieu07ce4062007-02-23 23:36:39 +01004075static void rtl_hw_start_8168(struct net_device *dev)
4076{
Francois Romieu2dd99532007-06-11 23:22:52 +02004077 struct rtl8169_private *tp = netdev_priv(dev);
4078 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01004079 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu2dd99532007-06-11 23:22:52 +02004080
4081 RTL_W8(Cfg9346, Cfg9346_Unlock);
4082
françois romieuf0298f82011-01-03 15:07:42 +00004083 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004084
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004085 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004086
Francois Romieu0e485152007-02-20 00:00:26 +01004087 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004088
4089 RTL_W16(CPlusCmd, tp->cp_cmd);
4090
Francois Romieu0e485152007-02-20 00:00:26 +01004091 RTL_W16(IntrMitigate, 0x5151);
4092
4093 /* Work around for RxFIFO overflow. */
Ivan Vecerab5ba6d12011-01-27 12:24:11 +01004094 if (tp->mac_version == RTL_GIGA_MAC_VER_11 ||
4095 tp->mac_version == RTL_GIGA_MAC_VER_22) {
Francois Romieu0e485152007-02-20 00:00:26 +01004096 tp->intr_event |= RxFIFOOver | PCSTimeout;
4097 tp->intr_event &= ~RxOverflow;
4098 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004099
4100 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4101
Francois Romieub8363902008-06-01 12:31:57 +02004102 rtl_set_rx_mode(dev);
4103
4104 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4105 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004106
4107 RTL_R8(IntrMask);
4108
Francois Romieu219a1e92008-06-28 11:58:39 +02004109 switch (tp->mac_version) {
4110 case RTL_GIGA_MAC_VER_11:
4111 rtl_hw_start_8168bb(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004112 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004113
4114 case RTL_GIGA_MAC_VER_12:
4115 case RTL_GIGA_MAC_VER_17:
4116 rtl_hw_start_8168bef(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004117 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004118
4119 case RTL_GIGA_MAC_VER_18:
Francois Romieuef3386f2008-06-29 12:24:30 +02004120 rtl_hw_start_8168cp_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004121 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004122
4123 case RTL_GIGA_MAC_VER_19:
4124 rtl_hw_start_8168c_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004125 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004126
4127 case RTL_GIGA_MAC_VER_20:
4128 rtl_hw_start_8168c_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004129 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004130
Francois Romieu197ff762008-06-28 13:16:02 +02004131 case RTL_GIGA_MAC_VER_21:
4132 rtl_hw_start_8168c_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004133 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004134
Francois Romieu6fb07052008-06-29 11:54:28 +02004135 case RTL_GIGA_MAC_VER_22:
4136 rtl_hw_start_8168c_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004137 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004138
Francois Romieuef3386f2008-06-29 12:24:30 +02004139 case RTL_GIGA_MAC_VER_23:
4140 rtl_hw_start_8168cp_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004141 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004142
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004143 case RTL_GIGA_MAC_VER_24:
4144 rtl_hw_start_8168cp_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004145 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004146
Francois Romieu5b538df2008-07-20 16:22:45 +02004147 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00004148 case RTL_GIGA_MAC_VER_26:
4149 case RTL_GIGA_MAC_VER_27:
Francois Romieu5b538df2008-07-20 16:22:45 +02004150 rtl_hw_start_8168d(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004151 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004152
françois romieue6de30d2011-01-03 15:08:37 +00004153 case RTL_GIGA_MAC_VER_28:
4154 rtl_hw_start_8168d_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004155 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004156
hayeswang4804b3b2011-03-21 01:50:29 +00004157 case RTL_GIGA_MAC_VER_31:
4158 rtl_hw_start_8168dp(ioaddr, pdev);
4159 break;
4160
hayeswang01dc7fe2011-03-21 01:50:28 +00004161 case RTL_GIGA_MAC_VER_32:
4162 case RTL_GIGA_MAC_VER_33:
4163 rtl_hw_start_8168e(ioaddr, pdev);
4164 break;
françois romieue6de30d2011-01-03 15:08:37 +00004165
Francois Romieu219a1e92008-06-28 11:58:39 +02004166 default:
4167 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
4168 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00004169 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004170 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004171
Francois Romieu0e485152007-02-20 00:00:26 +01004172 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4173
Francois Romieub8363902008-06-01 12:31:57 +02004174 RTL_W8(Cfg9346, Cfg9346_Lock);
4175
Francois Romieu2dd99532007-06-11 23:22:52 +02004176 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004177
Francois Romieu0e485152007-02-20 00:00:26 +01004178 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01004179}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180
Francois Romieu2857ffb2008-08-02 21:08:49 +02004181#define R810X_CPCMD_QUIRK_MASK (\
4182 EnableBist | \
4183 Mac_dbgo_oe | \
4184 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00004185 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02004186 Force_txflow_en | \
4187 Cxpl_dbg_sel | \
4188 ASF | \
4189 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004190 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004191
4192static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4193{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004194 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004195 { 0x01, 0, 0x6e65 },
4196 { 0x02, 0, 0x091f },
4197 { 0x03, 0, 0xc2f9 },
4198 { 0x06, 0, 0xafb5 },
4199 { 0x07, 0, 0x0e00 },
4200 { 0x19, 0, 0xec80 },
4201 { 0x01, 0, 0x2e65 },
4202 { 0x01, 0, 0x6e65 }
4203 };
4204 u8 cfg1;
4205
françois romieu650e8d52011-01-03 15:08:29 +00004206 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004207
4208 RTL_W8(DBG_REG, FIX_NAK_1);
4209
4210 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4211
4212 RTL_W8(Config1,
4213 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
4214 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4215
4216 cfg1 = RTL_R8(Config1);
4217 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
4218 RTL_W8(Config1, cfg1 & ~LEDS0);
4219
Francois Romieu2857ffb2008-08-02 21:08:49 +02004220 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
4221}
4222
4223static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4224{
françois romieu650e8d52011-01-03 15:08:29 +00004225 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004226
4227 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4228
4229 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
4230 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004231}
4232
4233static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
4234{
4235 rtl_hw_start_8102e_2(ioaddr, pdev);
4236
4237 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
4238}
4239
Hayes Wang5a5e4442011-02-22 17:26:21 +08004240static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4241{
4242 static const struct ephy_info e_info_8105e_1[] = {
4243 { 0x07, 0, 0x4000 },
4244 { 0x19, 0, 0x0200 },
4245 { 0x19, 0, 0x0020 },
4246 { 0x1e, 0, 0x2000 },
4247 { 0x03, 0, 0x0001 },
4248 { 0x19, 0, 0x0100 },
4249 { 0x19, 0, 0x0004 },
4250 { 0x0a, 0, 0x0020 }
4251 };
4252
Francois Romieucecb5fd2011-04-01 10:21:07 +02004253 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004254 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
4255
Francois Romieucecb5fd2011-04-01 10:21:07 +02004256 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004257 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
4258
4259 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
4260 RTL_W8(DLLPR, RTL_R8(DLLPR) | PM_SWITCH);
4261
4262 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
4263}
4264
4265static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4266{
4267 rtl_hw_start_8105e_1(ioaddr, pdev);
4268 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
4269}
4270
Francois Romieu07ce4062007-02-23 23:36:39 +01004271static void rtl_hw_start_8101(struct net_device *dev)
4272{
Francois Romieucdf1a602007-06-11 23:29:50 +02004273 struct rtl8169_private *tp = netdev_priv(dev);
4274 void __iomem *ioaddr = tp->mmio_addr;
4275 struct pci_dev *pdev = tp->pci_dev;
4276
Francois Romieucecb5fd2011-04-01 10:21:07 +02004277 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
4278 tp->mac_version == RTL_GIGA_MAC_VER_16) {
Francois Romieu9c14cea2008-07-05 00:21:15 +02004279 int cap = tp->pcie_cap;
4280
4281 if (cap) {
4282 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
4283 PCI_EXP_DEVCTL_NOSNOOP_EN);
4284 }
Francois Romieucdf1a602007-06-11 23:29:50 +02004285 }
4286
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004287 RTL_W8(Cfg9346, Cfg9346_Unlock);
4288
Francois Romieu2857ffb2008-08-02 21:08:49 +02004289 switch (tp->mac_version) {
4290 case RTL_GIGA_MAC_VER_07:
4291 rtl_hw_start_8102e_1(ioaddr, pdev);
4292 break;
4293
4294 case RTL_GIGA_MAC_VER_08:
4295 rtl_hw_start_8102e_3(ioaddr, pdev);
4296 break;
4297
4298 case RTL_GIGA_MAC_VER_09:
4299 rtl_hw_start_8102e_2(ioaddr, pdev);
4300 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004301
4302 case RTL_GIGA_MAC_VER_29:
4303 rtl_hw_start_8105e_1(ioaddr, pdev);
4304 break;
4305 case RTL_GIGA_MAC_VER_30:
4306 rtl_hw_start_8105e_2(ioaddr, pdev);
4307 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02004308 }
4309
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004310 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02004311
françois romieuf0298f82011-01-03 15:07:42 +00004312 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02004313
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004314 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02004315
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004316 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02004317 RTL_W16(CPlusCmd, tp->cp_cmd);
4318
4319 RTL_W16(IntrMitigate, 0x0000);
4320
4321 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4322
4323 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4324 rtl_set_rx_tx_config_registers(tp);
4325
Francois Romieucdf1a602007-06-11 23:29:50 +02004326 RTL_R8(IntrMask);
4327
Francois Romieucdf1a602007-06-11 23:29:50 +02004328 rtl_set_rx_mode(dev);
4329
4330 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004331
Francois Romieu0e485152007-02-20 00:00:26 +01004332 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333}
4334
4335static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
4336{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 if (new_mtu < ETH_ZLEN || new_mtu > SafeMtu)
4338 return -EINVAL;
4339
4340 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00004341 netdev_update_features(dev);
4342
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00004343 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344}
4345
4346static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
4347{
Al Viro95e09182007-12-22 18:55:39 +00004348 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
4350}
4351
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004352static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
4353 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004355 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004356 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004357
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004358 kfree(*data_buff);
4359 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 rtl8169_make_unusable_by_asic(desc);
4361}
4362
4363static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
4364{
4365 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
4366
4367 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
4368}
4369
4370static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
4371 u32 rx_buf_sz)
4372{
4373 desc->addr = cpu_to_le64(mapping);
4374 wmb();
4375 rtl8169_mark_to_asic(desc, rx_buf_sz);
4376}
4377
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004378static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004380 return (void *)ALIGN((long)data, 16);
4381}
4382
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004383static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
4384 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004385{
4386 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004388 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004389 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004390 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004392 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
4393 if (!data)
4394 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01004395
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004396 if (rtl8169_align(data) != data) {
4397 kfree(data);
4398 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
4399 if (!data)
4400 return NULL;
4401 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004402
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004403 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004404 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004405 if (unlikely(dma_mapping_error(d, mapping))) {
4406 if (net_ratelimit())
4407 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004408 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410
4411 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004412 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004413
4414err_out:
4415 kfree(data);
4416 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417}
4418
4419static void rtl8169_rx_clear(struct rtl8169_private *tp)
4420{
Francois Romieu07d3f512007-02-21 22:40:46 +01004421 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422
4423 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004424 if (tp->Rx_databuff[i]) {
4425 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 tp->RxDescArray + i);
4427 }
4428 }
4429}
4430
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004431static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004433 desc->opts1 |= cpu_to_le32(RingEnd);
4434}
Francois Romieu5b0384f2006-08-16 16:00:01 +02004435
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004436static int rtl8169_rx_fill(struct rtl8169_private *tp)
4437{
4438 unsigned int i;
4439
4440 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004441 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02004442
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004443 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004445
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004446 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004447 if (!data) {
4448 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004449 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004450 }
4451 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004454 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
4455 return 0;
4456
4457err_out:
4458 rtl8169_rx_clear(tp);
4459 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460}
4461
4462static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4463{
4464 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
4465}
4466
4467static int rtl8169_init_ring(struct net_device *dev)
4468{
4469 struct rtl8169_private *tp = netdev_priv(dev);
4470
4471 rtl8169_init_ring_indexes(tp);
4472
4473 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004474 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004476 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477}
4478
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004479static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 struct TxDesc *desc)
4481{
4482 unsigned int len = tx_skb->len;
4483
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004484 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
4485
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 desc->opts1 = 0x00;
4487 desc->opts2 = 0x00;
4488 desc->addr = 0x00;
4489 tx_skb->len = 0;
4490}
4491
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004492static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
4493 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494{
4495 unsigned int i;
4496
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004497 for (i = 0; i < n; i++) {
4498 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 struct ring_info *tx_skb = tp->tx_skb + entry;
4500 unsigned int len = tx_skb->len;
4501
4502 if (len) {
4503 struct sk_buff *skb = tx_skb->skb;
4504
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004505 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 tp->TxDescArray + entry);
4507 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00004508 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 dev_kfree_skb(skb);
4510 tx_skb->skb = NULL;
4511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 }
4513 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004514}
4515
4516static void rtl8169_tx_clear(struct rtl8169_private *tp)
4517{
4518 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 tp->cur_tx = tp->dirty_tx = 0;
4520}
4521
David Howellsc4028952006-11-22 14:57:56 +00004522static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523{
4524 struct rtl8169_private *tp = netdev_priv(dev);
4525
David Howellsc4028952006-11-22 14:57:56 +00004526 PREPARE_DELAYED_WORK(&tp->task, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 schedule_delayed_work(&tp->task, 4);
4528}
4529
4530static void rtl8169_wait_for_quiescence(struct net_device *dev)
4531{
4532 struct rtl8169_private *tp = netdev_priv(dev);
4533 void __iomem *ioaddr = tp->mmio_addr;
4534
4535 synchronize_irq(dev->irq);
4536
4537 /* Wait for any pending NAPI task to complete */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004538 napi_disable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539
4540 rtl8169_irq_mask_and_ack(ioaddr);
4541
David S. Millerd1d08d12008-01-07 20:53:33 -08004542 tp->intr_mask = 0xffff;
4543 RTL_W16(IntrMask, tp->intr_event);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004544 napi_enable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545}
4546
David Howellsc4028952006-11-22 14:57:56 +00004547static void rtl8169_reinit_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548{
David Howellsc4028952006-11-22 14:57:56 +00004549 struct rtl8169_private *tp =
4550 container_of(work, struct rtl8169_private, task.work);
4551 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 int ret;
4553
Francois Romieueb2a0212007-02-15 23:37:21 +01004554 rtnl_lock();
4555
4556 if (!netif_running(dev))
4557 goto out_unlock;
4558
4559 rtl8169_wait_for_quiescence(dev);
4560 rtl8169_close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561
4562 ret = rtl8169_open(dev);
4563 if (unlikely(ret < 0)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004564 if (net_ratelimit())
4565 netif_err(tp, drv, dev,
4566 "reinit failure (status = %d). Rescheduling\n",
4567 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 rtl8169_schedule_work(dev, rtl8169_reinit_task);
4569 }
Francois Romieueb2a0212007-02-15 23:37:21 +01004570
4571out_unlock:
4572 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573}
4574
David Howellsc4028952006-11-22 14:57:56 +00004575static void rtl8169_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576{
David Howellsc4028952006-11-22 14:57:56 +00004577 struct rtl8169_private *tp =
4578 container_of(work, struct rtl8169_private, task.work);
4579 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01004580 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581
Francois Romieueb2a0212007-02-15 23:37:21 +01004582 rtnl_lock();
4583
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 if (!netif_running(dev))
Francois Romieueb2a0212007-02-15 23:37:21 +01004585 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586
4587 rtl8169_wait_for_quiescence(dev);
4588
Francois Romieu56de4142011-03-15 17:29:31 +01004589 for (i = 0; i < NUM_RX_DESC; i++)
4590 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
4591
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 rtl8169_tx_clear(tp);
4593
Francois Romieu56de4142011-03-15 17:29:31 +01004594 rtl8169_init_ring_indexes(tp);
4595 rtl_hw_start(dev);
4596 netif_wake_queue(dev);
4597 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Francois Romieueb2a0212007-02-15 23:37:21 +01004598
4599out_unlock:
4600 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601}
4602
4603static void rtl8169_tx_timeout(struct net_device *dev)
4604{
4605 struct rtl8169_private *tp = netdev_priv(dev);
4606
françois romieue6de30d2011-01-03 15:08:37 +00004607 rtl8169_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608
4609 /* Let's wait a bit while any (async) irq lands on */
4610 rtl8169_schedule_work(dev, rtl8169_reset_task);
4611}
4612
4613static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07004614 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615{
4616 struct skb_shared_info *info = skb_shinfo(skb);
4617 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04004618 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004619 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620
4621 entry = tp->cur_tx;
4622 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
4623 skb_frag_t *frag = info->frags + cur_frag;
4624 dma_addr_t mapping;
4625 u32 status, len;
4626 void *addr;
4627
4628 entry = (entry + 1) % NUM_TX_DESC;
4629
4630 txd = tp->TxDescArray + entry;
4631 len = frag->size;
4632 addr = ((void *) page_address(frag->page)) + frag->page_offset;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004633 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004634 if (unlikely(dma_mapping_error(d, mapping))) {
4635 if (net_ratelimit())
4636 netif_err(tp, drv, tp->dev,
4637 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004638 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640
Francois Romieucecb5fd2011-04-01 10:21:07 +02004641 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07004642 status = opts[0] | len |
4643 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644
4645 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07004646 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 txd->addr = cpu_to_le64(mapping);
4648
4649 tp->tx_skb[entry].len = len;
4650 }
4651
4652 if (cur_frag) {
4653 tp->tx_skb[entry].skb = skb;
4654 txd->opts1 |= cpu_to_le32(LastFrag);
4655 }
4656
4657 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004658
4659err_out:
4660 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
4661 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662}
4663
Francois Romieu2b7b4312011-04-18 22:53:24 -07004664static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
4665 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666{
Francois Romieu2b7b4312011-04-18 22:53:24 -07004667 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00004668 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07004669 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670
Francois Romieu2b7b4312011-04-18 22:53:24 -07004671 if (mss) {
4672 opts[0] |= TD_LSO;
4673 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
4674 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07004675 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676
4677 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07004678 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07004680 opts[offset] |= info->checksum.udp;
4681 else
4682 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684}
4685
Stephen Hemminger613573252009-08-31 19:50:58 +00004686static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
4687 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688{
4689 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004690 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 struct TxDesc *txd = tp->TxDescArray + entry;
4692 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004693 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 dma_addr_t mapping;
4695 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07004696 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004697 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02004698
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004700 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004701 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 }
4703
4704 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004705 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004707 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004708 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004709 if (unlikely(dma_mapping_error(d, mapping))) {
4710 if (net_ratelimit())
4711 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004712 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714
4715 tp->tx_skb[entry].len = len;
4716 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717
Francois Romieu2b7b4312011-04-18 22:53:24 -07004718 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
4719 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004720
Francois Romieu2b7b4312011-04-18 22:53:24 -07004721 rtl8169_tso_csum(tp, skb, opts);
4722
4723 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004724 if (frags < 0)
4725 goto err_dma_1;
4726 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07004727 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004728 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07004729 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004730 tp->tx_skb[entry].skb = skb;
4731 }
4732
Francois Romieu2b7b4312011-04-18 22:53:24 -07004733 txd->opts2 = cpu_to_le32(opts[1]);
4734
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 wmb();
4736
Francois Romieucecb5fd2011-04-01 10:21:07 +02004737 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07004738 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 txd->opts1 = cpu_to_le32(status);
4740
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 tp->cur_tx += frags + 1;
4742
David Dillow4c020a92010-03-03 16:33:10 +00004743 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744
Francois Romieucecb5fd2011-04-01 10:21:07 +02004745 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746
4747 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
4748 netif_stop_queue(dev);
4749 smp_rmb();
4750 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
4751 netif_wake_queue(dev);
4752 }
4753
Stephen Hemminger613573252009-08-31 19:50:58 +00004754 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004756err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004757 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004758err_dma_0:
4759 dev_kfree_skb(skb);
4760 dev->stats.tx_dropped++;
4761 return NETDEV_TX_OK;
4762
4763err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004765 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00004766 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767}
4768
4769static void rtl8169_pcierr_interrupt(struct net_device *dev)
4770{
4771 struct rtl8169_private *tp = netdev_priv(dev);
4772 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 u16 pci_status, pci_cmd;
4774
4775 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
4776 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
4777
Joe Perchesbf82c182010-02-09 11:49:50 +00004778 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
4779 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780
4781 /*
4782 * The recovery sequence below admits a very elaborated explanation:
4783 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01004784 * - I did not see what else could be done;
4785 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 *
4787 * Feel free to adjust to your needs.
4788 */
Francois Romieua27993f2006-12-18 00:04:19 +01004789 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01004790 pci_cmd &= ~PCI_COMMAND_PARITY;
4791 else
4792 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
4793
4794 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795
4796 pci_write_config_word(pdev, PCI_STATUS,
4797 pci_status & (PCI_STATUS_DETECTED_PARITY |
4798 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
4799 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
4800
4801 /* The infamous DAC f*ckup only happens at boot time */
4802 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00004803 void __iomem *ioaddr = tp->mmio_addr;
4804
Joe Perchesbf82c182010-02-09 11:49:50 +00004805 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 tp->cp_cmd &= ~PCIDAC;
4807 RTL_W16(CPlusCmd, tp->cp_cmd);
4808 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 }
4810
françois romieue6de30d2011-01-03 15:08:37 +00004811 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01004812
4813 rtl8169_schedule_work(dev, rtl8169_reinit_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814}
4815
Francois Romieu07d3f512007-02-21 22:40:46 +01004816static void rtl8169_tx_interrupt(struct net_device *dev,
4817 struct rtl8169_private *tp,
4818 void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819{
4820 unsigned int dirty_tx, tx_left;
4821
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 dirty_tx = tp->dirty_tx;
4823 smp_rmb();
4824 tx_left = tp->cur_tx - dirty_tx;
4825
4826 while (tx_left > 0) {
4827 unsigned int entry = dirty_tx % NUM_TX_DESC;
4828 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 u32 status;
4830
4831 rmb();
4832 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
4833 if (status & DescOwn)
4834 break;
4835
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004836 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
4837 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 if (status & LastFrag) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00004839 dev->stats.tx_packets++;
4840 dev->stats.tx_bytes += tx_skb->skb->len;
Eric Dumazet87433bf2009-06-09 22:55:53 +00004841 dev_kfree_skb(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 tx_skb->skb = NULL;
4843 }
4844 dirty_tx++;
4845 tx_left--;
4846 }
4847
4848 if (tp->dirty_tx != dirty_tx) {
4849 tp->dirty_tx = dirty_tx;
4850 smp_wmb();
4851 if (netif_queue_stopped(dev) &&
4852 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
4853 netif_wake_queue(dev);
4854 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02004855 /*
4856 * 8168 hack: TxPoll requests are lost when the Tx packets are
4857 * too close. Let's kick an extra TxPoll request when a burst
4858 * of start_xmit activity is detected (if it is not detected,
4859 * it is slow enough). -- FR
4860 */
4861 smp_rmb();
4862 if (tp->cur_tx != dirty_tx)
4863 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 }
4865}
4866
Francois Romieu126fa4b2005-05-12 20:09:17 -04004867static inline int rtl8169_fragmented_frame(u32 status)
4868{
4869 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
4870}
4871
Eric Dumazetadea1ac72010-09-05 20:04:05 -07004872static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 u32 status = opts1 & RxProtoMask;
4875
4876 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00004877 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 skb->ip_summed = CHECKSUM_UNNECESSARY;
4879 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07004880 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881}
4882
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004883static struct sk_buff *rtl8169_try_rx_copy(void *data,
4884 struct rtl8169_private *tp,
4885 int pkt_size,
4886 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02004888 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004889 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004891 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004892 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004893 prefetch(data);
4894 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
4895 if (skb)
4896 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004897 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
4898
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004899 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900}
4901
Francois Romieu07d3f512007-02-21 22:40:46 +01004902static int rtl8169_rx_interrupt(struct net_device *dev,
4903 struct rtl8169_private *tp,
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004904 void __iomem *ioaddr, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905{
4906 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004907 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 cur_rx = tp->cur_rx;
4910 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02004911 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912
Richard Dawe4dcb7d32005-05-27 21:12:00 +02004913 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04004915 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 u32 status;
4917
4918 rmb();
Francois Romieu126fa4b2005-05-12 20:09:17 -04004919 status = le32_to_cpu(desc->opts1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920
4921 if (status & DescOwn)
4922 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02004923 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004924 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
4925 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004926 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02004928 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02004930 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02004931 if (status & RxFOVF) {
4932 rtl8169_schedule_work(dev, rtl8169_reset_task);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004933 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02004934 }
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004935 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004937 struct sk_buff *skb;
Stephen Hemmingerb4496552007-06-17 01:06:49 +02004938 dma_addr_t addr = le64_to_cpu(desc->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 int pkt_size = (status & 0x00001FFF) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940
Francois Romieu126fa4b2005-05-12 20:09:17 -04004941 /*
4942 * The driver does not support incoming fragmented
4943 * frames. They are seen as a symptom of over-mtu
4944 * sized frames.
4945 */
4946 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02004947 dev->stats.rx_dropped++;
4948 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004949 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02004950 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04004951 }
4952
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004953 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
4954 tp, pkt_size, addr);
4955 rtl8169_mark_to_asic(desc, rx_buf_sz);
4956 if (!skb) {
4957 dev->stats.rx_dropped++;
4958 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 }
4960
Eric Dumazetadea1ac72010-09-05 20:04:05 -07004961 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 skb_put(skb, pkt_size);
4963 skb->protocol = eth_type_trans(skb, dev);
4964
Francois Romieu7a8fc772011-03-01 17:18:33 +01004965 rtl8169_rx_vlan_tag(desc, skb);
4966
Francois Romieu56de4142011-03-15 17:29:31 +01004967 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968
Francois Romieucebf8cc2007-10-18 12:06:54 +02004969 dev->stats.rx_bytes += pkt_size;
4970 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 }
Francois Romieu6dccd162007-02-13 23:38:05 +01004972
4973 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00004974 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01004975 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
4976 desc->opts2 = 0;
4977 cur_rx++;
4978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 }
4980
4981 count = cur_rx - tp->cur_rx;
4982 tp->cur_rx = cur_rx;
4983
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004984 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985
4986 return count;
4987}
4988
Francois Romieu07d3f512007-02-21 22:40:46 +01004989static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990{
Francois Romieu07d3f512007-02-21 22:40:46 +01004991 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 int handled = 0;
Francois Romieu865c6522008-05-11 14:51:00 +02004995 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996
David Dillowf11a3772009-05-22 15:29:34 +00004997 /* loop handling interrupts until we have no new ones or
4998 * we hit a invalid/hotplug case.
4999 */
Francois Romieu865c6522008-05-11 14:51:00 +02005000 status = RTL_R16(IntrStatus);
David Dillowf11a3772009-05-22 15:29:34 +00005001 while (status && status != 0xffff) {
5002 handled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003
David Dillowf11a3772009-05-22 15:29:34 +00005004 /* Handle all of the error cases first. These will reset
5005 * the chip, so just exit the loop.
5006 */
5007 if (unlikely(!netif_running(dev))) {
5008 rtl8169_asic_down(ioaddr);
5009 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 }
David Dillowf11a3772009-05-22 15:29:34 +00005011
Francois Romieu1519e572011-02-03 12:02:36 +01005012 if (unlikely(status & RxFIFOOver)) {
5013 switch (tp->mac_version) {
5014 /* Work around for rx fifo overflow */
5015 case RTL_GIGA_MAC_VER_11:
5016 case RTL_GIGA_MAC_VER_22:
5017 case RTL_GIGA_MAC_VER_26:
5018 netif_stop_queue(dev);
5019 rtl8169_tx_timeout(dev);
5020 goto done;
Francois Romieuf60ac8e2011-02-03 17:27:52 +01005021 /* Testers needed. */
5022 case RTL_GIGA_MAC_VER_17:
5023 case RTL_GIGA_MAC_VER_19:
5024 case RTL_GIGA_MAC_VER_20:
5025 case RTL_GIGA_MAC_VER_21:
5026 case RTL_GIGA_MAC_VER_23:
5027 case RTL_GIGA_MAC_VER_24:
5028 case RTL_GIGA_MAC_VER_27:
5029 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00005030 case RTL_GIGA_MAC_VER_31:
Francois Romieu1519e572011-02-03 12:02:36 +01005031 /* Experimental science. Pktgen proof. */
5032 case RTL_GIGA_MAC_VER_12:
5033 case RTL_GIGA_MAC_VER_25:
5034 if (status == RxFIFOOver)
5035 goto done;
5036 break;
5037 default:
5038 break;
5039 }
David Dillowf11a3772009-05-22 15:29:34 +00005040 }
5041
5042 if (unlikely(status & SYSErr)) {
5043 rtl8169_pcierr_interrupt(dev);
5044 break;
5045 }
5046
5047 if (status & LinkChg)
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005048 __rtl8169_check_link_status(dev, tp, ioaddr, true);
David Dillowf11a3772009-05-22 15:29:34 +00005049
5050 /* We need to see the lastest version of tp->intr_mask to
5051 * avoid ignoring an MSI interrupt and having to wait for
5052 * another event which may never come.
5053 */
5054 smp_rmb();
5055 if (status & tp->intr_mask & tp->napi_event) {
5056 RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
5057 tp->intr_mask = ~tp->napi_event;
5058
5059 if (likely(napi_schedule_prep(&tp->napi)))
5060 __napi_schedule(&tp->napi);
Joe Perchesbf82c182010-02-09 11:49:50 +00005061 else
5062 netif_info(tp, intr, dev,
5063 "interrupt %04x in poll\n", status);
David Dillowf11a3772009-05-22 15:29:34 +00005064 }
5065
5066 /* We only get a new MSI interrupt when all active irq
5067 * sources on the chip have been acknowledged. So, ack
5068 * everything we've seen and check if new sources have become
5069 * active to avoid blocking all interrupts from the chip.
5070 */
5071 RTL_W16(IntrStatus,
5072 (status & RxFIFOOver) ? (status | RxOverflow) : status);
5073 status = RTL_R16(IntrStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 }
Francois Romieu1519e572011-02-03 12:02:36 +01005075done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 return IRQ_RETVAL(handled);
5077}
5078
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005079static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005081 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5082 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 void __iomem *ioaddr = tp->mmio_addr;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005084 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005086 work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 rtl8169_tx_interrupt(dev, tp, ioaddr);
5088
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005089 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005090 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00005091
5092 /* We need for force the visibility of tp->intr_mask
5093 * for other CPUs, as we can loose an MSI interrupt
5094 * and potentially wait for a retransmit timeout if we don't.
5095 * The posted write to IntrMask is safe, as it will
5096 * eventually make it to the chip and we won't loose anything
5097 * until it does.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 */
David Dillowf11a3772009-05-22 15:29:34 +00005099 tp->intr_mask = 0xffff;
David Dillow4c020a92010-03-03 16:33:10 +00005100 wmb();
Francois Romieu0e485152007-02-20 00:00:26 +01005101 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 }
5103
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005104 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106
Francois Romieu523a6092008-09-10 22:28:56 +02005107static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5108{
5109 struct rtl8169_private *tp = netdev_priv(dev);
5110
5111 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5112 return;
5113
5114 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5115 RTL_W32(RxMissed, 0);
5116}
5117
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118static void rtl8169_down(struct net_device *dev)
5119{
5120 struct rtl8169_private *tp = netdev_priv(dev);
5121 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122
Francois Romieu4876cc12011-03-11 21:07:11 +01005123 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124
5125 netif_stop_queue(dev);
5126
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005127 napi_disable(&tp->napi);
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005128
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 spin_lock_irq(&tp->lock);
5130
5131 rtl8169_asic_down(ioaddr);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005132 /*
5133 * At this point device interrupts can not be enabled in any function,
5134 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task,
5135 * rtl8169_reinit_task) and napi is disabled (rtl8169_poll).
5136 */
Francois Romieu523a6092008-09-10 22:28:56 +02005137 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138
5139 spin_unlock_irq(&tp->lock);
5140
5141 synchronize_irq(dev->irq);
5142
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 /* Give a racing hard_start_xmit a few cycles to complete. */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07005144 synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 rtl8169_tx_clear(tp);
5147
5148 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00005149
5150 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151}
5152
5153static int rtl8169_close(struct net_device *dev)
5154{
5155 struct rtl8169_private *tp = netdev_priv(dev);
5156 struct pci_dev *pdev = tp->pci_dev;
5157
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005158 pm_runtime_get_sync(&pdev->dev);
5159
Francois Romieucecb5fd2011-04-01 10:21:07 +02005160 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08005161 rtl8169_update_counters(dev);
5162
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 rtl8169_down(dev);
5164
5165 free_irq(dev->irq, dev);
5166
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00005167 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5168 tp->RxPhyAddr);
5169 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5170 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 tp->TxDescArray = NULL;
5172 tp->RxDescArray = NULL;
5173
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005174 pm_runtime_put_sync(&pdev->dev);
5175
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 return 0;
5177}
5178
Francois Romieu07ce4062007-02-23 23:36:39 +01005179static void rtl_set_rx_mode(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180{
5181 struct rtl8169_private *tp = netdev_priv(dev);
5182 void __iomem *ioaddr = tp->mmio_addr;
5183 unsigned long flags;
5184 u32 mc_filter[2]; /* Multicast hash filter */
Francois Romieu07d3f512007-02-21 22:40:46 +01005185 int rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 u32 tmp = 0;
5187
5188 if (dev->flags & IFF_PROMISC) {
5189 /* Unconditionally log net taps. */
Joe Perchesbf82c182010-02-09 11:49:50 +00005190 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 rx_mode =
5192 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5193 AcceptAllPhys;
5194 mc_filter[1] = mc_filter[0] = 0xffffffff;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00005195 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
Joe Perches8e95a202009-12-03 07:58:21 +00005196 (dev->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197 /* Too many to filter perfectly -- accept all multicasts. */
5198 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5199 mc_filter[1] = mc_filter[0] = 0xffffffff;
5200 } else {
Jiri Pirko22bedad2010-04-01 21:22:57 +00005201 struct netdev_hw_addr *ha;
Francois Romieu07d3f512007-02-21 22:40:46 +01005202
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 rx_mode = AcceptBroadcast | AcceptMyPhys;
5204 mc_filter[1] = mc_filter[0] = 0;
Jiri Pirko22bedad2010-04-01 21:22:57 +00005205 netdev_for_each_mc_addr(ha, dev) {
5206 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5208 rx_mode |= AcceptMulticast;
5209 }
5210 }
5211
5212 spin_lock_irqsave(&tp->lock, flags);
5213
5214 tmp = rtl8169_rx_config | rx_mode |
Francois Romieu2b7b4312011-04-18 22:53:24 -07005215 (RTL_R32(RxConfig) & RTL_RX_CONFIG_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216
Francois Romieuf887cce2008-07-17 22:24:18 +02005217 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
Francois Romieu1087f4f2007-12-26 22:46:05 +01005218 u32 data = mc_filter[0];
5219
5220 mc_filter[0] = swab32(mc_filter[1]);
5221 mc_filter[1] = swab32(data);
Francois Romieubcf0bf92006-07-26 23:14:13 +02005222 }
5223
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224 RTL_W32(MAR0 + 4, mc_filter[1]);
Francois Romieu78f1cd02010-03-27 19:35:46 -07005225 RTL_W32(MAR0 + 0, mc_filter[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226
Francois Romieu57a9f232007-06-04 22:10:15 +02005227 RTL_W32(RxConfig, tmp);
5228
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 spin_unlock_irqrestore(&tp->lock, flags);
5230}
5231
5232/**
5233 * rtl8169_get_stats - Get rtl8169 read/write statistics
5234 * @dev: The Ethernet Device to get statistics for
5235 *
5236 * Get TX/RX statistics for rtl8169
5237 */
5238static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
5239{
5240 struct rtl8169_private *tp = netdev_priv(dev);
5241 void __iomem *ioaddr = tp->mmio_addr;
5242 unsigned long flags;
5243
5244 if (netif_running(dev)) {
5245 spin_lock_irqsave(&tp->lock, flags);
Francois Romieu523a6092008-09-10 22:28:56 +02005246 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247 spin_unlock_irqrestore(&tp->lock, flags);
5248 }
Francois Romieu5b0384f2006-08-16 16:00:01 +02005249
Francois Romieucebf8cc2007-10-18 12:06:54 +02005250 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251}
5252
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005253static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01005254{
françois romieu065c27c2011-01-03 15:08:12 +00005255 struct rtl8169_private *tp = netdev_priv(dev);
5256
Francois Romieu5d06a992006-02-23 00:47:58 +01005257 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005258 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01005259
françois romieu065c27c2011-01-03 15:08:12 +00005260 rtl_pll_power_down(tp);
5261
Francois Romieu5d06a992006-02-23 00:47:58 +01005262 netif_device_detach(dev);
5263 netif_stop_queue(dev);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005264}
Francois Romieu5d06a992006-02-23 00:47:58 +01005265
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005266#ifdef CONFIG_PM
5267
5268static int rtl8169_suspend(struct device *device)
5269{
5270 struct pci_dev *pdev = to_pci_dev(device);
5271 struct net_device *dev = pci_get_drvdata(pdev);
5272
5273 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02005274
Francois Romieu5d06a992006-02-23 00:47:58 +01005275 return 0;
5276}
5277
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005278static void __rtl8169_resume(struct net_device *dev)
5279{
françois romieu065c27c2011-01-03 15:08:12 +00005280 struct rtl8169_private *tp = netdev_priv(dev);
5281
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005282 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00005283
5284 rtl_pll_power_up(tp);
5285
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005286 rtl8169_schedule_work(dev, rtl8169_reset_task);
5287}
5288
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005289static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01005290{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005291 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01005292 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005293 struct rtl8169_private *tp = netdev_priv(dev);
5294
5295 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01005296
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005297 if (netif_running(dev))
5298 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01005299
Francois Romieu5d06a992006-02-23 00:47:58 +01005300 return 0;
5301}
5302
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005303static int rtl8169_runtime_suspend(struct device *device)
5304{
5305 struct pci_dev *pdev = to_pci_dev(device);
5306 struct net_device *dev = pci_get_drvdata(pdev);
5307 struct rtl8169_private *tp = netdev_priv(dev);
5308
5309 if (!tp->TxDescArray)
5310 return 0;
5311
5312 spin_lock_irq(&tp->lock);
5313 tp->saved_wolopts = __rtl8169_get_wol(tp);
5314 __rtl8169_set_wol(tp, WAKE_ANY);
5315 spin_unlock_irq(&tp->lock);
5316
5317 rtl8169_net_suspend(dev);
5318
5319 return 0;
5320}
5321
5322static int rtl8169_runtime_resume(struct device *device)
5323{
5324 struct pci_dev *pdev = to_pci_dev(device);
5325 struct net_device *dev = pci_get_drvdata(pdev);
5326 struct rtl8169_private *tp = netdev_priv(dev);
5327
5328 if (!tp->TxDescArray)
5329 return 0;
5330
5331 spin_lock_irq(&tp->lock);
5332 __rtl8169_set_wol(tp, tp->saved_wolopts);
5333 tp->saved_wolopts = 0;
5334 spin_unlock_irq(&tp->lock);
5335
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005336 rtl8169_init_phy(dev, tp);
5337
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005338 __rtl8169_resume(dev);
5339
5340 return 0;
5341}
5342
5343static int rtl8169_runtime_idle(struct device *device)
5344{
5345 struct pci_dev *pdev = to_pci_dev(device);
5346 struct net_device *dev = pci_get_drvdata(pdev);
5347 struct rtl8169_private *tp = netdev_priv(dev);
5348
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005349 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005350}
5351
Alexey Dobriyan47145212009-12-14 18:00:08 -08005352static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02005353 .suspend = rtl8169_suspend,
5354 .resume = rtl8169_resume,
5355 .freeze = rtl8169_suspend,
5356 .thaw = rtl8169_resume,
5357 .poweroff = rtl8169_suspend,
5358 .restore = rtl8169_resume,
5359 .runtime_suspend = rtl8169_runtime_suspend,
5360 .runtime_resume = rtl8169_runtime_resume,
5361 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005362};
5363
5364#define RTL8169_PM_OPS (&rtl8169_pm_ops)
5365
5366#else /* !CONFIG_PM */
5367
5368#define RTL8169_PM_OPS NULL
5369
5370#endif /* !CONFIG_PM */
5371
Francois Romieu1765f952008-09-13 17:21:40 +02005372static void rtl_shutdown(struct pci_dev *pdev)
5373{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005374 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00005375 struct rtl8169_private *tp = netdev_priv(dev);
5376 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu1765f952008-09-13 17:21:40 +02005377
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005378 rtl8169_net_suspend(dev);
5379
Francois Romieucecb5fd2011-04-01 10:21:07 +02005380 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08005381 rtl_rar_set(tp, dev->perm_addr);
5382
françois romieu4bb3f522009-06-17 11:41:45 +00005383 spin_lock_irq(&tp->lock);
5384
5385 rtl8169_asic_down(ioaddr);
5386
5387 spin_unlock_irq(&tp->lock);
5388
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005389 if (system_state == SYSTEM_POWER_OFF) {
françois romieuca52efd2009-07-24 12:34:19 +00005390 /* WoL fails with some 8168 when the receiver is disabled. */
5391 if (tp->features & RTL_FEATURE_WOL) {
5392 pci_clear_master(pdev);
5393
5394 RTL_W8(ChipCmd, CmdRxEnb);
5395 /* PCI commit */
5396 RTL_R8(ChipCmd);
5397 }
5398
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005399 pci_wake_from_d3(pdev, true);
5400 pci_set_power_state(pdev, PCI_D3hot);
5401 }
5402}
Francois Romieu5d06a992006-02-23 00:47:58 +01005403
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404static struct pci_driver rtl8169_pci_driver = {
5405 .name = MODULENAME,
5406 .id_table = rtl8169_pci_tbl,
5407 .probe = rtl8169_init_one,
5408 .remove = __devexit_p(rtl8169_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02005409 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005410 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411};
5412
Francois Romieu07d3f512007-02-21 22:40:46 +01005413static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414{
Jeff Garzik29917622006-08-19 17:48:59 -04005415 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416}
5417
Francois Romieu07d3f512007-02-21 22:40:46 +01005418static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419{
5420 pci_unregister_driver(&rtl8169_pci_driver);
5421}
5422
5423module_init(rtl8169_init_module);
5424module_exit(rtl8169_cleanup_module);