blob: 3eeefe4b49e0706560e71ff92c06651c551dfd0a [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
Francois Romieub6ffd972011-06-17 17:00:05 +0200670 struct rtl_fw {
671 const struct firmware *fw;
672 } *rtl_fw;
François Romieu953a12c2011-04-24 17:38:48 +0200673#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674};
675
Ralf Baechle979b6c12005-06-13 14:30:40 -0700676MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700679MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200680module_param_named(debug, debug.msg_enable, int, 0);
681MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682MODULE_LICENSE("GPL");
683MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000684MODULE_FIRMWARE(FIRMWARE_8168D_1);
685MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000686MODULE_FIRMWARE(FIRMWARE_8168E_1);
687MODULE_FIRMWARE(FIRMWARE_8168E_2);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800688MODULE_FIRMWARE(FIRMWARE_8105E_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690static int rtl8169_open(struct net_device *dev);
Stephen Hemminger613573252009-08-31 19:50:58 +0000691static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
692 struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100693static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694static int rtl8169_init_ring(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100695static void rtl_hw_start(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696static int rtl8169_close(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100697static void rtl_set_rx_mode(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698static void rtl8169_tx_timeout(struct net_device *dev);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200699static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700static int rtl8169_rx_interrupt(struct net_device *, struct rtl8169_private *,
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700701 void __iomem *, u32 budget);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200702static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703static void rtl8169_down(struct net_device *dev);
Francois Romieu99f252b2007-04-02 22:59:59 +0200704static void rtl8169_rx_clear(struct rtl8169_private *tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700705static int rtl8169_poll(struct napi_struct *napi, int budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707static const unsigned int rtl8169_rx_config =
Francois Romieu5b0384f2006-08-16 16:00:01 +0200708 (RX_FIFO_THRESH << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
françois romieub646d902011-01-03 15:08:21 +0000710static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
711{
712 void __iomem *ioaddr = tp->mmio_addr;
713 int i;
714
715 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
716 for (i = 0; i < 20; i++) {
717 udelay(100);
718 if (RTL_R32(OCPAR) & OCPAR_FLAG)
719 break;
720 }
721 return RTL_R32(OCPDR);
722}
723
724static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
725{
726 void __iomem *ioaddr = tp->mmio_addr;
727 int i;
728
729 RTL_W32(OCPDR, data);
730 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
731 for (i = 0; i < 20; i++) {
732 udelay(100);
733 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
734 break;
735 }
736}
737
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800738static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000739{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800740 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000741 int i;
742
743 RTL_W8(ERIDR, cmd);
744 RTL_W32(ERIAR, 0x800010e8);
745 msleep(2);
746 for (i = 0; i < 5; i++) {
747 udelay(100);
748 if (!(RTL_R32(ERIDR) & ERIAR_FLAG))
749 break;
750 }
751
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800752 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000753}
754
755#define OOB_CMD_RESET 0x00
756#define OOB_CMD_DRIVER_START 0x05
757#define OOB_CMD_DRIVER_STOP 0x06
758
Francois Romieucecb5fd2011-04-01 10:21:07 +0200759static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
760{
761 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
762}
763
françois romieub646d902011-01-03 15:08:21 +0000764static void rtl8168_driver_start(struct rtl8169_private *tp)
765{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200766 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000767 int i;
768
769 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
770
Francois Romieucecb5fd2011-04-01 10:21:07 +0200771 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000772
françois romieub646d902011-01-03 15:08:21 +0000773 for (i = 0; i < 10; i++) {
774 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000775 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
françois romieub646d902011-01-03 15:08:21 +0000776 break;
777 }
778}
779
780static void rtl8168_driver_stop(struct rtl8169_private *tp)
781{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200782 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000783 int i;
784
785 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
786
Francois Romieucecb5fd2011-04-01 10:21:07 +0200787 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000788
françois romieub646d902011-01-03 15:08:21 +0000789 for (i = 0; i < 10; i++) {
790 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000791 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
françois romieub646d902011-01-03 15:08:21 +0000792 break;
793 }
794}
795
hayeswang4804b3b2011-03-21 01:50:29 +0000796static int r8168dp_check_dash(struct rtl8169_private *tp)
797{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200798 u16 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000799
Francois Romieucecb5fd2011-04-01 10:21:07 +0200800 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
hayeswang4804b3b2011-03-21 01:50:29 +0000801}
françois romieub646d902011-01-03 15:08:21 +0000802
françois romieu4da19632011-01-03 15:07:55 +0000803static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
805 int i;
806
Francois Romieua6baf3a2007-11-08 23:23:21 +0100807 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Francois Romieu23714082006-01-29 00:49:09 +0100809 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100810 /*
811 * Check if the RTL8169 has completed writing to the specified
812 * MII register.
813 */
Francois Romieu5b0384f2006-08-16 16:00:01 +0200814 if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 break;
Francois Romieu23714082006-01-29 00:49:09 +0100816 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
Timo Teräs024a07b2010-06-06 15:38:47 -0700818 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700819 * According to hardware specs a 20us delay is required after write
820 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700821 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700822 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
824
françois romieu4da19632011-01-03 15:07:55 +0000825static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
827 int i, value = -1;
828
Francois Romieua6baf3a2007-11-08 23:23:21 +0100829 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Francois Romieu23714082006-01-29 00:49:09 +0100831 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100832 /*
833 * Check if the RTL8169 has completed retrieving data from
834 * the specified MII register.
835 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 if (RTL_R32(PHYAR) & 0x80000000) {
Francois Romieua6baf3a2007-11-08 23:23:21 +0100837 value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 break;
839 }
Francois Romieu23714082006-01-29 00:49:09 +0100840 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 }
Timo Teräs81a95f02010-06-09 17:31:48 -0700842 /*
843 * According to hardware specs a 20us delay is required after read
844 * complete indication, but before sending next command.
845 */
846 udelay(20);
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return value;
849}
850
françois romieuc0e45c12011-01-03 15:08:04 +0000851static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
852{
853 int i;
854
855 RTL_W32(OCPDR, data |
856 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
857 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
858 RTL_W32(EPHY_RXER_NUM, 0);
859
860 for (i = 0; i < 100; i++) {
861 mdelay(1);
862 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
863 break;
864 }
865}
866
867static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
868{
869 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
870 (value & OCPDR_DATA_MASK));
871}
872
873static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
874{
875 int i;
876
877 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
878
879 mdelay(1);
880 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
881 RTL_W32(EPHY_RXER_NUM, 0);
882
883 for (i = 0; i < 100; i++) {
884 mdelay(1);
885 if (RTL_R32(OCPAR) & OCPAR_FLAG)
886 break;
887 }
888
889 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
890}
891
françois romieue6de30d2011-01-03 15:08:37 +0000892#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
893
894static void r8168dp_2_mdio_start(void __iomem *ioaddr)
895{
896 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
897}
898
899static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
900{
901 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
902}
903
904static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
905{
906 r8168dp_2_mdio_start(ioaddr);
907
908 r8169_mdio_write(ioaddr, reg_addr, value);
909
910 r8168dp_2_mdio_stop(ioaddr);
911}
912
913static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
914{
915 int value;
916
917 r8168dp_2_mdio_start(ioaddr);
918
919 value = r8169_mdio_read(ioaddr, reg_addr);
920
921 r8168dp_2_mdio_stop(ioaddr);
922
923 return value;
924}
925
françois romieu4da19632011-01-03 15:07:55 +0000926static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +0200927{
françois romieuc0e45c12011-01-03 15:08:04 +0000928 tp->mdio_ops.write(tp->mmio_addr, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +0200929}
930
françois romieu4da19632011-01-03 15:07:55 +0000931static int rtl_readphy(struct rtl8169_private *tp, int location)
932{
françois romieuc0e45c12011-01-03 15:08:04 +0000933 return tp->mdio_ops.read(tp->mmio_addr, location);
françois romieu4da19632011-01-03 15:07:55 +0000934}
935
936static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
937{
938 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
939}
940
941static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +0000942{
943 int val;
944
françois romieu4da19632011-01-03 15:07:55 +0000945 val = rtl_readphy(tp, reg_addr);
946 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +0000947}
948
Francois Romieuccdffb92008-07-26 14:26:06 +0200949static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
950 int val)
951{
952 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +0200953
françois romieu4da19632011-01-03 15:07:55 +0000954 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +0200955}
956
957static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
958{
959 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +0200960
françois romieu4da19632011-01-03 15:07:55 +0000961 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +0200962}
963
Francois Romieudacf8152008-08-02 20:44:13 +0200964static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
965{
966 unsigned int i;
967
968 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
969 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
970
971 for (i = 0; i < 100; i++) {
972 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
973 break;
974 udelay(10);
975 }
976}
977
978static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
979{
980 u16 value = 0xffff;
981 unsigned int i;
982
983 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
984
985 for (i = 0; i < 100; i++) {
986 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
987 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
988 break;
989 }
990 udelay(10);
991 }
992
993 return value;
994}
995
996static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
997{
998 unsigned int i;
999
1000 RTL_W32(CSIDR, value);
1001 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
1002 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1003
1004 for (i = 0; i < 100; i++) {
1005 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
1006 break;
1007 udelay(10);
1008 }
1009}
1010
1011static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
1012{
1013 u32 value = ~0x00;
1014 unsigned int i;
1015
1016 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
1017 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1018
1019 for (i = 0; i < 100; i++) {
1020 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
1021 value = RTL_R32(CSIDR);
1022 break;
1023 }
1024 udelay(10);
1025 }
1026
1027 return value;
1028}
1029
françois romieudaf9df62009-10-07 12:44:20 +00001030static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1031{
1032 u8 value = 0xff;
1033 unsigned int i;
1034
1035 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1036
1037 for (i = 0; i < 300; i++) {
1038 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1039 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1040 break;
1041 }
1042 udelay(100);
1043 }
1044
1045 return value;
1046}
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048static void rtl8169_irq_mask_and_ack(void __iomem *ioaddr)
1049{
1050 RTL_W16(IntrMask, 0x0000);
1051
1052 RTL_W16(IntrStatus, 0xffff);
1053}
1054
1055static void rtl8169_asic_down(void __iomem *ioaddr)
1056{
1057 RTL_W8(ChipCmd, 0x00);
1058 rtl8169_irq_mask_and_ack(ioaddr);
1059 RTL_R16(CPlusCmd);
1060}
1061
françois romieu4da19632011-01-03 15:07:55 +00001062static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
françois romieu4da19632011-01-03 15:07:55 +00001064 void __iomem *ioaddr = tp->mmio_addr;
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 return RTL_R32(TBICSR) & TBIReset;
1067}
1068
françois romieu4da19632011-01-03 15:07:55 +00001069static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
françois romieu4da19632011-01-03 15:07:55 +00001071 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
1073
1074static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1075{
1076 return RTL_R32(TBICSR) & TBILinkOk;
1077}
1078
1079static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1080{
1081 return RTL_R8(PHYstatus) & LinkStatus;
1082}
1083
françois romieu4da19632011-01-03 15:07:55 +00001084static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
françois romieu4da19632011-01-03 15:07:55 +00001086 void __iomem *ioaddr = tp->mmio_addr;
1087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1089}
1090
françois romieu4da19632011-01-03 15:07:55 +00001091static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
1093 unsigned int val;
1094
françois romieu4da19632011-01-03 15:07:55 +00001095 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1096 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097}
1098
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001099static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001100 struct rtl8169_private *tp,
1101 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
1103 unsigned long flags;
1104
1105 spin_lock_irqsave(&tp->lock, flags);
1106 if (tp->link_ok(ioaddr)) {
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001107 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001108 if (pm)
1109 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001111 if (net_ratelimit())
1112 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001113 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001115 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001116 if (pm)
1117 pm_schedule_suspend(&tp->pci_dev->dev, 100);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 spin_unlock_irqrestore(&tp->lock, flags);
1120}
1121
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001122static void rtl8169_check_link_status(struct net_device *dev,
1123 struct rtl8169_private *tp,
1124 void __iomem *ioaddr)
1125{
1126 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1127}
1128
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001129#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1130
1131static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1132{
1133 void __iomem *ioaddr = tp->mmio_addr;
1134 u8 options;
1135 u32 wolopts = 0;
1136
1137 options = RTL_R8(Config1);
1138 if (!(options & PMEnable))
1139 return 0;
1140
1141 options = RTL_R8(Config3);
1142 if (options & LinkUp)
1143 wolopts |= WAKE_PHY;
1144 if (options & MagicPacket)
1145 wolopts |= WAKE_MAGIC;
1146
1147 options = RTL_R8(Config5);
1148 if (options & UWF)
1149 wolopts |= WAKE_UCAST;
1150 if (options & BWF)
1151 wolopts |= WAKE_BCAST;
1152 if (options & MWF)
1153 wolopts |= WAKE_MCAST;
1154
1155 return wolopts;
1156}
1157
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001158static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1159{
1160 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001161
1162 spin_lock_irq(&tp->lock);
1163
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001164 wol->supported = WAKE_ANY;
1165 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001166
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001167 spin_unlock_irq(&tp->lock);
1168}
1169
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001170static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001171{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001172 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001173 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001174 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001175 u32 opt;
1176 u16 reg;
1177 u8 mask;
1178 } cfg[] = {
1179 { WAKE_ANY, Config1, PMEnable },
1180 { WAKE_PHY, Config3, LinkUp },
1181 { WAKE_MAGIC, Config3, MagicPacket },
1182 { WAKE_UCAST, Config5, UWF },
1183 { WAKE_BCAST, Config5, BWF },
1184 { WAKE_MCAST, Config5, MWF },
1185 { WAKE_ANY, Config5, LanWake }
1186 };
1187
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001188 RTL_W8(Cfg9346, Cfg9346_Unlock);
1189
1190 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1191 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001192 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001193 options |= cfg[i].mask;
1194 RTL_W8(cfg[i].reg, options);
1195 }
1196
1197 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001198}
1199
1200static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1201{
1202 struct rtl8169_private *tp = netdev_priv(dev);
1203
1204 spin_lock_irq(&tp->lock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001205
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001206 if (wol->wolopts)
1207 tp->features |= RTL_FEATURE_WOL;
1208 else
1209 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001210 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001211 spin_unlock_irq(&tp->lock);
1212
françois romieuea809072010-11-08 13:23:58 +00001213 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1214
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001215 return 0;
1216}
1217
Francois Romieu31bd2042011-04-26 18:58:59 +02001218static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1219{
Francois Romieu85bffe62011-04-27 08:22:39 +02001220 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001221}
1222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223static void rtl8169_get_drvinfo(struct net_device *dev,
1224 struct ethtool_drvinfo *info)
1225{
1226 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001227 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 strcpy(info->driver, MODULENAME);
1230 strcpy(info->version, RTL8169_VERSION);
1231 strcpy(info->bus_info, pci_name(tp->pci_dev));
Francois Romieub6ffd972011-06-17 17:00:05 +02001232 strncpy(info->fw_version, IS_ERR_OR_NULL(rtl_fw) ? "N/A" :
Francois Romieu31bd2042011-04-26 18:58:59 +02001233 rtl_lookup_firmware_name(tp), sizeof(info->fw_version) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234}
1235
1236static int rtl8169_get_regs_len(struct net_device *dev)
1237{
1238 return R8169_REGS_SIZE;
1239}
1240
1241static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001242 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
1244 struct rtl8169_private *tp = netdev_priv(dev);
1245 void __iomem *ioaddr = tp->mmio_addr;
1246 int ret = 0;
1247 u32 reg;
1248
1249 reg = RTL_R32(TBICSR);
1250 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1251 (duplex == DUPLEX_FULL)) {
1252 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1253 } else if (autoneg == AUTONEG_ENABLE)
1254 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1255 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001256 netif_warn(tp, link, dev,
1257 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 ret = -EOPNOTSUPP;
1259 }
1260
1261 return ret;
1262}
1263
1264static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001265 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
1267 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001268 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001269 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Hayes Wang716b50a2011-02-22 17:26:18 +08001271 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001274 int auto_nego;
1275
françois romieu4da19632011-01-03 15:07:55 +00001276 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001277 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1278 ADVERTISE_100HALF | ADVERTISE_100FULL);
1279
1280 if (adv & ADVERTISED_10baseT_Half)
1281 auto_nego |= ADVERTISE_10HALF;
1282 if (adv & ADVERTISED_10baseT_Full)
1283 auto_nego |= ADVERTISE_10FULL;
1284 if (adv & ADVERTISED_100baseT_Half)
1285 auto_nego |= ADVERTISE_100HALF;
1286 if (adv & ADVERTISED_100baseT_Full)
1287 auto_nego |= ADVERTISE_100FULL;
1288
françois romieu3577aa12009-05-19 10:46:48 +00001289 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1290
françois romieu4da19632011-01-03 15:07:55 +00001291 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001292 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1293
1294 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001295 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001296 if (adv & ADVERTISED_1000baseT_Half)
1297 giga_ctrl |= ADVERTISE_1000HALF;
1298 if (adv & ADVERTISED_1000baseT_Full)
1299 giga_ctrl |= ADVERTISE_1000FULL;
1300 } else if (adv & (ADVERTISED_1000baseT_Half |
1301 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001302 netif_info(tp, link, dev,
1303 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001304 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
françois romieu3577aa12009-05-19 10:46:48 +00001307 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001308
françois romieu4da19632011-01-03 15:07:55 +00001309 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1310 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001311 } else {
1312 giga_ctrl = 0;
1313
1314 if (speed == SPEED_10)
1315 bmcr = 0;
1316 else if (speed == SPEED_100)
1317 bmcr = BMCR_SPEED100;
1318 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001319 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001320
1321 if (duplex == DUPLEX_FULL)
1322 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001323 }
1324
françois romieu4da19632011-01-03 15:07:55 +00001325 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001326
Francois Romieucecb5fd2011-04-01 10:21:07 +02001327 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1328 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001329 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001330 rtl_writephy(tp, 0x17, 0x2138);
1331 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001332 } else {
françois romieu4da19632011-01-03 15:07:55 +00001333 rtl_writephy(tp, 0x17, 0x2108);
1334 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001335 }
1336 }
1337
Oliver Neukum54405cd2011-01-06 21:55:13 +01001338 rc = 0;
1339out:
1340 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341}
1342
1343static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001344 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
1346 struct rtl8169_private *tp = netdev_priv(dev);
1347 int ret;
1348
Oliver Neukum54405cd2011-01-06 21:55:13 +01001349 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001350 if (ret < 0)
1351 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Francois Romieu4876cc12011-03-11 21:07:11 +01001353 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1354 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001356 }
1357out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 return ret;
1359}
1360
1361static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1362{
1363 struct rtl8169_private *tp = netdev_priv(dev);
1364 unsigned long flags;
1365 int ret;
1366
Francois Romieu4876cc12011-03-11 21:07:11 +01001367 del_timer_sync(&tp->timer);
1368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 spin_lock_irqsave(&tp->lock, flags);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001370 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001371 cmd->duplex, cmd->advertising);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 return ret;
1375}
1376
Michał Mirosław350fb322011-04-08 06:35:56 +00001377static u32 rtl8169_fix_features(struct net_device *dev, u32 features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
Francois Romieu2b7b4312011-04-18 22:53:24 -07001379 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001380 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Michał Mirosław350fb322011-04-08 06:35:56 +00001382 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383}
1384
Michał Mirosław350fb322011-04-08 06:35:56 +00001385static int rtl8169_set_features(struct net_device *dev, u32 features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386{
1387 struct rtl8169_private *tp = netdev_priv(dev);
1388 void __iomem *ioaddr = tp->mmio_addr;
1389 unsigned long flags;
1390
1391 spin_lock_irqsave(&tp->lock, flags);
1392
Michał Mirosław350fb322011-04-08 06:35:56 +00001393 if (features & NETIF_F_RXCSUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 tp->cp_cmd |= RxChkSum;
1395 else
1396 tp->cp_cmd &= ~RxChkSum;
1397
Michał Mirosław350fb322011-04-08 06:35:56 +00001398 if (dev->features & NETIF_F_HW_VLAN_RX)
1399 tp->cp_cmd |= RxVlan;
1400 else
1401 tp->cp_cmd &= ~RxVlan;
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 RTL_W16(CPlusCmd, tp->cp_cmd);
1404 RTL_R16(CPlusCmd);
1405
1406 spin_unlock_irqrestore(&tp->lock, flags);
1407
1408 return 0;
1409}
1410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1412 struct sk_buff *skb)
1413{
Jesse Grosseab6d182010-10-20 13:56:03 +00001414 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1416}
1417
Francois Romieu7a8fc772011-03-01 17:18:33 +01001418static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
1420 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Francois Romieu7a8fc772011-03-01 17:18:33 +01001422 if (opts2 & RxVlanTag)
1423 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426}
1427
Francois Romieuccdffb92008-07-26 14:26:06 +02001428static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
1430 struct rtl8169_private *tp = netdev_priv(dev);
1431 void __iomem *ioaddr = tp->mmio_addr;
1432 u32 status;
1433
1434 cmd->supported =
1435 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1436 cmd->port = PORT_FIBRE;
1437 cmd->transceiver = XCVR_INTERNAL;
1438
1439 status = RTL_R32(TBICSR);
1440 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1441 cmd->autoneg = !!(status & TBINwEnable);
1442
David Decotigny70739492011-04-27 18:32:40 +00001443 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001445
1446 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447}
1448
Francois Romieuccdffb92008-07-26 14:26:06 +02001449static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
1451 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Francois Romieuccdffb92008-07-26 14:26:06 +02001453 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454}
1455
1456static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1457{
1458 struct rtl8169_private *tp = netdev_priv(dev);
1459 unsigned long flags;
Francois Romieuccdffb92008-07-26 14:26:06 +02001460 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
1462 spin_lock_irqsave(&tp->lock, flags);
1463
Francois Romieuccdffb92008-07-26 14:26:06 +02001464 rc = tp->get_settings(dev, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
1466 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieuccdffb92008-07-26 14:26:06 +02001467 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
1469
1470static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1471 void *p)
1472{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001473 struct rtl8169_private *tp = netdev_priv(dev);
1474 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Francois Romieu5b0384f2006-08-16 16:00:01 +02001476 if (regs->len > R8169_REGS_SIZE)
1477 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Francois Romieu5b0384f2006-08-16 16:00:01 +02001479 spin_lock_irqsave(&tp->lock, flags);
1480 memcpy_fromio(p, tp->mmio_addr, regs->len);
1481 spin_unlock_irqrestore(&tp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482}
1483
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001484static u32 rtl8169_get_msglevel(struct net_device *dev)
1485{
1486 struct rtl8169_private *tp = netdev_priv(dev);
1487
1488 return tp->msg_enable;
1489}
1490
1491static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1492{
1493 struct rtl8169_private *tp = netdev_priv(dev);
1494
1495 tp->msg_enable = value;
1496}
1497
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001498static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1499 "tx_packets",
1500 "rx_packets",
1501 "tx_errors",
1502 "rx_errors",
1503 "rx_missed",
1504 "align_errors",
1505 "tx_single_collisions",
1506 "tx_multi_collisions",
1507 "unicast",
1508 "broadcast",
1509 "multicast",
1510 "tx_aborted",
1511 "tx_underrun",
1512};
1513
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001514static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001515{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001516 switch (sset) {
1517 case ETH_SS_STATS:
1518 return ARRAY_SIZE(rtl8169_gstrings);
1519 default:
1520 return -EOPNOTSUPP;
1521 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001522}
1523
Ivan Vecera355423d2009-02-06 21:49:57 -08001524static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001525{
1526 struct rtl8169_private *tp = netdev_priv(dev);
1527 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001528 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001529 struct rtl8169_counters *counters;
1530 dma_addr_t paddr;
1531 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001532 int wait = 1000;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001533
Ivan Vecera355423d2009-02-06 21:49:57 -08001534 /*
1535 * Some chips are unable to dump tally counters when the receiver
1536 * is disabled.
1537 */
1538 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1539 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001540
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001541 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001542 if (!counters)
1543 return;
1544
1545 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001546 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001547 RTL_W32(CounterAddrLow, cmd);
1548 RTL_W32(CounterAddrLow, cmd | CounterDump);
1549
Ivan Vecera355423d2009-02-06 21:49:57 -08001550 while (wait--) {
1551 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
Ivan Vecera355423d2009-02-06 21:49:57 -08001552 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001553 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001554 }
1555 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001556 }
1557
1558 RTL_W32(CounterAddrLow, 0);
1559 RTL_W32(CounterAddrHigh, 0);
1560
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001561 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001562}
1563
Ivan Vecera355423d2009-02-06 21:49:57 -08001564static void rtl8169_get_ethtool_stats(struct net_device *dev,
1565 struct ethtool_stats *stats, u64 *data)
1566{
1567 struct rtl8169_private *tp = netdev_priv(dev);
1568
1569 ASSERT_RTNL();
1570
1571 rtl8169_update_counters(dev);
1572
1573 data[0] = le64_to_cpu(tp->counters.tx_packets);
1574 data[1] = le64_to_cpu(tp->counters.rx_packets);
1575 data[2] = le64_to_cpu(tp->counters.tx_errors);
1576 data[3] = le32_to_cpu(tp->counters.rx_errors);
1577 data[4] = le16_to_cpu(tp->counters.rx_missed);
1578 data[5] = le16_to_cpu(tp->counters.align_errors);
1579 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1580 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1581 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1582 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1583 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1584 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1585 data[12] = le16_to_cpu(tp->counters.tx_underun);
1586}
1587
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001588static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1589{
1590 switch(stringset) {
1591 case ETH_SS_STATS:
1592 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1593 break;
1594 }
1595}
1596
Jeff Garzik7282d492006-09-13 14:30:00 -04001597static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 .get_drvinfo = rtl8169_get_drvinfo,
1599 .get_regs_len = rtl8169_get_regs_len,
1600 .get_link = ethtool_op_get_link,
1601 .get_settings = rtl8169_get_settings,
1602 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001603 .get_msglevel = rtl8169_get_msglevel,
1604 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001606 .get_wol = rtl8169_get_wol,
1607 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001608 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001609 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001610 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611};
1612
Francois Romieu07d3f512007-02-21 22:40:46 +01001613static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02001614 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615{
Francois Romieu5d320a22011-05-08 17:47:36 +02001616 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01001617 /*
1618 * The driver currently handles the 8168Bf and the 8168Be identically
1619 * but they can be identified more specifically through the test below
1620 * if needed:
1621 *
1622 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001623 *
1624 * Same thing for the 8101Eb and the 8101Ec:
1625 *
1626 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001627 */
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001628 static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001630 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 int mac_version;
1632 } mac_info[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00001633 /* 8168E family. */
1634 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1635 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1636 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1637
Francois Romieu5b538df2008-07-20 16:22:45 +02001638 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001639 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1640 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001641 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001642
françois romieue6de30d2011-01-03 15:08:37 +00001643 /* 8168DP family. */
1644 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1645 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00001646 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00001647
Francois Romieuef808d52008-06-29 13:10:54 +02001648 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001649 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001650 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001651 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001652 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001653 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1654 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001655 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001656 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001657 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001658
1659 /* 8168B family. */
1660 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1661 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1662 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1663 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1664
1665 /* 8101 family. */
hayeswang36a0e6c2011-03-21 01:50:30 +00001666 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08001667 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1668 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1669 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001670 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1671 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1672 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1673 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1674 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1675 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001676 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001677 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001678 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001679 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1680 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001681 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1682 /* FIXME: where did these entries come from ? -- FR */
1683 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1684 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1685
1686 /* 8110 family. */
1687 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1688 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1689 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1690 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1691 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1692 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1693
Jean Delvaref21b75e2009-05-26 20:54:48 -07001694 /* Catch-all */
1695 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 }, *p = mac_info;
1697 u32 reg;
1698
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001699 reg = RTL_R32(TxConfig);
1700 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 p++;
1702 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02001703
1704 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1705 netif_notice(tp, probe, dev,
1706 "unknown MAC, using family default\n");
1707 tp->mac_version = default_version;
1708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709}
1710
1711static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1712{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001713 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714}
1715
Francois Romieu867763c2007-08-17 18:21:58 +02001716struct phy_reg {
1717 u16 reg;
1718 u16 val;
1719};
1720
françois romieu4da19632011-01-03 15:07:55 +00001721static void rtl_writephy_batch(struct rtl8169_private *tp,
1722 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02001723{
1724 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00001725 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02001726 regs++;
1727 }
1728}
1729
françois romieubca03d52011-01-03 15:07:31 +00001730#define PHY_READ 0x00000000
1731#define PHY_DATA_OR 0x10000000
1732#define PHY_DATA_AND 0x20000000
1733#define PHY_BJMPN 0x30000000
1734#define PHY_READ_EFUSE 0x40000000
1735#define PHY_READ_MAC_BYTE 0x50000000
1736#define PHY_WRITE_MAC_BYTE 0x60000000
1737#define PHY_CLEAR_READCOUNT 0x70000000
1738#define PHY_WRITE 0x80000000
1739#define PHY_READCOUNT_EQ_SKIP 0x90000000
1740#define PHY_COMP_EQ_SKIPN 0xa0000000
1741#define PHY_COMP_NEQ_SKIPN 0xb0000000
1742#define PHY_WRITE_PREVIOUS 0xc0000000
1743#define PHY_SKIPN 0xd0000000
1744#define PHY_DELAY_MS 0xe0000000
1745#define PHY_WRITE_ERI_WORD 0xf0000000
1746
Francois Romieub6ffd972011-06-17 17:00:05 +02001747static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00001748{
Francois Romieub6ffd972011-06-17 17:00:05 +02001749 const struct firmware *fw = rtl_fw->fw;
françois romieubca03d52011-01-03 15:07:31 +00001750 __le32 *phytable = (__le32 *)fw->data;
1751 struct net_device *dev = tp->dev;
hayeswang42b82dc2011-01-10 02:07:25 +00001752 size_t index, fw_size = fw->size / sizeof(*phytable);
1753 u32 predata, count;
françois romieubca03d52011-01-03 15:07:31 +00001754
1755 if (fw->size % sizeof(*phytable)) {
1756 netif_err(tp, probe, dev, "odd sized firmware %zd\n", fw->size);
1757 return;
1758 }
1759
hayeswang42b82dc2011-01-10 02:07:25 +00001760 for (index = 0; index < fw_size; index++) {
1761 u32 action = le32_to_cpu(phytable[index]);
1762 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00001763
hayeswang42b82dc2011-01-10 02:07:25 +00001764 switch(action & 0xf0000000) {
1765 case PHY_READ:
1766 case PHY_DATA_OR:
1767 case PHY_DATA_AND:
1768 case PHY_READ_EFUSE:
1769 case PHY_CLEAR_READCOUNT:
1770 case PHY_WRITE:
1771 case PHY_WRITE_PREVIOUS:
1772 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00001773 break;
1774
hayeswang42b82dc2011-01-10 02:07:25 +00001775 case PHY_BJMPN:
1776 if (regno > index) {
1777 netif_err(tp, probe, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001778 "Out of range of firmware\n");
hayeswang42b82dc2011-01-10 02:07:25 +00001779 return;
1780 }
1781 break;
1782 case PHY_READCOUNT_EQ_SKIP:
1783 if (index + 2 >= fw_size) {
1784 netif_err(tp, probe, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001785 "Out of range of firmware\n");
hayeswang42b82dc2011-01-10 02:07:25 +00001786 return;
1787 }
1788 break;
1789 case PHY_COMP_EQ_SKIPN:
1790 case PHY_COMP_NEQ_SKIPN:
1791 case PHY_SKIPN:
1792 if (index + 1 + regno >= fw_size) {
1793 netif_err(tp, probe, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001794 "Out of range of firmware\n");
hayeswang42b82dc2011-01-10 02:07:25 +00001795 return;
1796 }
1797 break;
1798
1799 case PHY_READ_MAC_BYTE:
1800 case PHY_WRITE_MAC_BYTE:
1801 case PHY_WRITE_ERI_WORD:
1802 default:
1803 netif_err(tp, probe, tp->dev,
1804 "Invalid action 0x%08x\n", action);
françois romieubca03d52011-01-03 15:07:31 +00001805 return;
1806 }
1807 }
1808
hayeswang42b82dc2011-01-10 02:07:25 +00001809 predata = 0;
1810 count = 0;
1811
1812 for (index = 0; index < fw_size; ) {
1813 u32 action = le32_to_cpu(phytable[index]);
françois romieubca03d52011-01-03 15:07:31 +00001814 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00001815 u32 regno = (action & 0x0fff0000) >> 16;
1816
1817 if (!action)
1818 break;
françois romieubca03d52011-01-03 15:07:31 +00001819
1820 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00001821 case PHY_READ:
1822 predata = rtl_readphy(tp, regno);
1823 count++;
1824 index++;
françois romieubca03d52011-01-03 15:07:31 +00001825 break;
hayeswang42b82dc2011-01-10 02:07:25 +00001826 case PHY_DATA_OR:
1827 predata |= data;
1828 index++;
1829 break;
1830 case PHY_DATA_AND:
1831 predata &= data;
1832 index++;
1833 break;
1834 case PHY_BJMPN:
1835 index -= regno;
1836 break;
1837 case PHY_READ_EFUSE:
1838 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
1839 index++;
1840 break;
1841 case PHY_CLEAR_READCOUNT:
1842 count = 0;
1843 index++;
1844 break;
1845 case PHY_WRITE:
1846 rtl_writephy(tp, regno, data);
1847 index++;
1848 break;
1849 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02001850 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00001851 break;
1852 case PHY_COMP_EQ_SKIPN:
1853 if (predata == data)
1854 index += regno;
1855 index++;
1856 break;
1857 case PHY_COMP_NEQ_SKIPN:
1858 if (predata != data)
1859 index += regno;
1860 index++;
1861 break;
1862 case PHY_WRITE_PREVIOUS:
1863 rtl_writephy(tp, regno, predata);
1864 index++;
1865 break;
1866 case PHY_SKIPN:
1867 index += regno + 1;
1868 break;
1869 case PHY_DELAY_MS:
1870 mdelay(data);
1871 index++;
1872 break;
1873
1874 case PHY_READ_MAC_BYTE:
1875 case PHY_WRITE_MAC_BYTE:
1876 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00001877 default:
1878 BUG();
1879 }
1880 }
1881}
1882
françois romieuf1e02ed2011-01-13 13:07:53 +00001883static void rtl_release_firmware(struct rtl8169_private *tp)
1884{
Francois Romieub6ffd972011-06-17 17:00:05 +02001885 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
1886 release_firmware(tp->rtl_fw->fw);
1887 kfree(tp->rtl_fw);
1888 }
1889 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00001890}
1891
François Romieu953a12c2011-04-24 17:38:48 +02001892static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00001893{
Francois Romieub6ffd972011-06-17 17:00:05 +02001894 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00001895
1896 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieub6ffd972011-06-17 17:00:05 +02001897 if (!IS_ERR_OR_NULL(rtl_fw))
1898 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02001899}
1900
1901static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
1902{
1903 if (rtl_readphy(tp, reg) != val)
1904 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
1905 else
1906 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00001907}
1908
françois romieu4da19632011-01-03 15:07:55 +00001909static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001911 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00001912 { 0x1f, 0x0001 },
1913 { 0x06, 0x006e },
1914 { 0x08, 0x0708 },
1915 { 0x15, 0x4000 },
1916 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
françois romieu0b9b5712009-08-10 19:44:56 +00001918 { 0x1f, 0x0001 },
1919 { 0x03, 0x00a1 },
1920 { 0x02, 0x0008 },
1921 { 0x01, 0x0120 },
1922 { 0x00, 0x1000 },
1923 { 0x04, 0x0800 },
1924 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
françois romieu0b9b5712009-08-10 19:44:56 +00001926 { 0x03, 0xff41 },
1927 { 0x02, 0xdf60 },
1928 { 0x01, 0x0140 },
1929 { 0x00, 0x0077 },
1930 { 0x04, 0x7800 },
1931 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
françois romieu0b9b5712009-08-10 19:44:56 +00001933 { 0x03, 0x802f },
1934 { 0x02, 0x4f02 },
1935 { 0x01, 0x0409 },
1936 { 0x00, 0xf0f9 },
1937 { 0x04, 0x9800 },
1938 { 0x04, 0x9000 },
1939
1940 { 0x03, 0xdf01 },
1941 { 0x02, 0xdf20 },
1942 { 0x01, 0xff95 },
1943 { 0x00, 0xba00 },
1944 { 0x04, 0xa800 },
1945 { 0x04, 0xa000 },
1946
1947 { 0x03, 0xff41 },
1948 { 0x02, 0xdf20 },
1949 { 0x01, 0x0140 },
1950 { 0x00, 0x00bb },
1951 { 0x04, 0xb800 },
1952 { 0x04, 0xb000 },
1953
1954 { 0x03, 0xdf41 },
1955 { 0x02, 0xdc60 },
1956 { 0x01, 0x6340 },
1957 { 0x00, 0x007d },
1958 { 0x04, 0xd800 },
1959 { 0x04, 0xd000 },
1960
1961 { 0x03, 0xdf01 },
1962 { 0x02, 0xdf20 },
1963 { 0x01, 0x100a },
1964 { 0x00, 0xa0ff },
1965 { 0x04, 0xf800 },
1966 { 0x04, 0xf000 },
1967
1968 { 0x1f, 0x0000 },
1969 { 0x0b, 0x0000 },
1970 { 0x00, 0x9200 }
1971 };
1972
françois romieu4da19632011-01-03 15:07:55 +00001973 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974}
1975
françois romieu4da19632011-01-03 15:07:55 +00001976static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02001977{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001978 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02001979 { 0x1f, 0x0002 },
1980 { 0x01, 0x90d0 },
1981 { 0x1f, 0x0000 }
1982 };
1983
françois romieu4da19632011-01-03 15:07:55 +00001984 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02001985}
1986
françois romieu4da19632011-01-03 15:07:55 +00001987static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00001988{
1989 struct pci_dev *pdev = tp->pci_dev;
1990 u16 vendor_id, device_id;
1991
1992 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &vendor_id);
1993 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &device_id);
1994
1995 if ((vendor_id != PCI_VENDOR_ID_GIGABYTE) || (device_id != 0xe000))
1996 return;
1997
françois romieu4da19632011-01-03 15:07:55 +00001998 rtl_writephy(tp, 0x1f, 0x0001);
1999 rtl_writephy(tp, 0x10, 0xf01b);
2000 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002001}
2002
françois romieu4da19632011-01-03 15:07:55 +00002003static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002004{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002005 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002006 { 0x1f, 0x0001 },
2007 { 0x04, 0x0000 },
2008 { 0x03, 0x00a1 },
2009 { 0x02, 0x0008 },
2010 { 0x01, 0x0120 },
2011 { 0x00, 0x1000 },
2012 { 0x04, 0x0800 },
2013 { 0x04, 0x9000 },
2014 { 0x03, 0x802f },
2015 { 0x02, 0x4f02 },
2016 { 0x01, 0x0409 },
2017 { 0x00, 0xf099 },
2018 { 0x04, 0x9800 },
2019 { 0x04, 0xa000 },
2020 { 0x03, 0xdf01 },
2021 { 0x02, 0xdf20 },
2022 { 0x01, 0xff95 },
2023 { 0x00, 0xba00 },
2024 { 0x04, 0xa800 },
2025 { 0x04, 0xf000 },
2026 { 0x03, 0xdf01 },
2027 { 0x02, 0xdf20 },
2028 { 0x01, 0x101a },
2029 { 0x00, 0xa0ff },
2030 { 0x04, 0xf800 },
2031 { 0x04, 0x0000 },
2032 { 0x1f, 0x0000 },
2033
2034 { 0x1f, 0x0001 },
2035 { 0x10, 0xf41b },
2036 { 0x14, 0xfb54 },
2037 { 0x18, 0xf5c7 },
2038 { 0x1f, 0x0000 },
2039
2040 { 0x1f, 0x0001 },
2041 { 0x17, 0x0cc0 },
2042 { 0x1f, 0x0000 }
2043 };
2044
françois romieu4da19632011-01-03 15:07:55 +00002045 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002046
françois romieu4da19632011-01-03 15:07:55 +00002047 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002048}
2049
françois romieu4da19632011-01-03 15:07:55 +00002050static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002051{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002052 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002053 { 0x1f, 0x0001 },
2054 { 0x04, 0x0000 },
2055 { 0x03, 0x00a1 },
2056 { 0x02, 0x0008 },
2057 { 0x01, 0x0120 },
2058 { 0x00, 0x1000 },
2059 { 0x04, 0x0800 },
2060 { 0x04, 0x9000 },
2061 { 0x03, 0x802f },
2062 { 0x02, 0x4f02 },
2063 { 0x01, 0x0409 },
2064 { 0x00, 0xf099 },
2065 { 0x04, 0x9800 },
2066 { 0x04, 0xa000 },
2067 { 0x03, 0xdf01 },
2068 { 0x02, 0xdf20 },
2069 { 0x01, 0xff95 },
2070 { 0x00, 0xba00 },
2071 { 0x04, 0xa800 },
2072 { 0x04, 0xf000 },
2073 { 0x03, 0xdf01 },
2074 { 0x02, 0xdf20 },
2075 { 0x01, 0x101a },
2076 { 0x00, 0xa0ff },
2077 { 0x04, 0xf800 },
2078 { 0x04, 0x0000 },
2079 { 0x1f, 0x0000 },
2080
2081 { 0x1f, 0x0001 },
2082 { 0x0b, 0x8480 },
2083 { 0x1f, 0x0000 },
2084
2085 { 0x1f, 0x0001 },
2086 { 0x18, 0x67c7 },
2087 { 0x04, 0x2000 },
2088 { 0x03, 0x002f },
2089 { 0x02, 0x4360 },
2090 { 0x01, 0x0109 },
2091 { 0x00, 0x3022 },
2092 { 0x04, 0x2800 },
2093 { 0x1f, 0x0000 },
2094
2095 { 0x1f, 0x0001 },
2096 { 0x17, 0x0cc0 },
2097 { 0x1f, 0x0000 }
2098 };
2099
françois romieu4da19632011-01-03 15:07:55 +00002100 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002101}
2102
françois romieu4da19632011-01-03 15:07:55 +00002103static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002104{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002105 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002106 { 0x10, 0xf41b },
2107 { 0x1f, 0x0000 }
2108 };
2109
françois romieu4da19632011-01-03 15:07:55 +00002110 rtl_writephy(tp, 0x1f, 0x0001);
2111 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002112
françois romieu4da19632011-01-03 15:07:55 +00002113 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002114}
2115
françois romieu4da19632011-01-03 15:07:55 +00002116static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002117{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002118 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002119 { 0x1f, 0x0001 },
2120 { 0x10, 0xf41b },
2121 { 0x1f, 0x0000 }
2122 };
2123
françois romieu4da19632011-01-03 15:07:55 +00002124 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002125}
2126
françois romieu4da19632011-01-03 15:07:55 +00002127static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002128{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002129 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002130 { 0x1f, 0x0000 },
2131 { 0x1d, 0x0f00 },
2132 { 0x1f, 0x0002 },
2133 { 0x0c, 0x1ec8 },
2134 { 0x1f, 0x0000 }
2135 };
2136
françois romieu4da19632011-01-03 15:07:55 +00002137 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002138}
2139
françois romieu4da19632011-01-03 15:07:55 +00002140static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002141{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002142 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002143 { 0x1f, 0x0001 },
2144 { 0x1d, 0x3d98 },
2145 { 0x1f, 0x0000 }
2146 };
2147
françois romieu4da19632011-01-03 15:07:55 +00002148 rtl_writephy(tp, 0x1f, 0x0000);
2149 rtl_patchphy(tp, 0x14, 1 << 5);
2150 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002151
françois romieu4da19632011-01-03 15:07:55 +00002152 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002153}
2154
françois romieu4da19632011-01-03 15:07:55 +00002155static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002156{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002157 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002158 { 0x1f, 0x0001 },
2159 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002160 { 0x1f, 0x0002 },
2161 { 0x00, 0x88d4 },
2162 { 0x01, 0x82b1 },
2163 { 0x03, 0x7002 },
2164 { 0x08, 0x9e30 },
2165 { 0x09, 0x01f0 },
2166 { 0x0a, 0x5500 },
2167 { 0x0c, 0x00c8 },
2168 { 0x1f, 0x0003 },
2169 { 0x12, 0xc096 },
2170 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002171 { 0x1f, 0x0000 },
2172 { 0x1f, 0x0000 },
2173 { 0x09, 0x2000 },
2174 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002175 };
2176
françois romieu4da19632011-01-03 15:07:55 +00002177 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002178
françois romieu4da19632011-01-03 15:07:55 +00002179 rtl_patchphy(tp, 0x14, 1 << 5);
2180 rtl_patchphy(tp, 0x0d, 1 << 5);
2181 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002182}
2183
françois romieu4da19632011-01-03 15:07:55 +00002184static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002185{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002186 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002187 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002188 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002189 { 0x03, 0x802f },
2190 { 0x02, 0x4f02 },
2191 { 0x01, 0x0409 },
2192 { 0x00, 0xf099 },
2193 { 0x04, 0x9800 },
2194 { 0x04, 0x9000 },
2195 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002196 { 0x1f, 0x0002 },
2197 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002198 { 0x06, 0x0761 },
2199 { 0x1f, 0x0003 },
2200 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002201 { 0x1f, 0x0000 }
2202 };
2203
françois romieu4da19632011-01-03 15:07:55 +00002204 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002205
françois romieu4da19632011-01-03 15:07:55 +00002206 rtl_patchphy(tp, 0x16, 1 << 0);
2207 rtl_patchphy(tp, 0x14, 1 << 5);
2208 rtl_patchphy(tp, 0x0d, 1 << 5);
2209 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002210}
2211
françois romieu4da19632011-01-03 15:07:55 +00002212static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002213{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002214 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002215 { 0x1f, 0x0001 },
2216 { 0x12, 0x2300 },
2217 { 0x1d, 0x3d98 },
2218 { 0x1f, 0x0002 },
2219 { 0x0c, 0x7eb8 },
2220 { 0x06, 0x5461 },
2221 { 0x1f, 0x0003 },
2222 { 0x16, 0x0f0a },
2223 { 0x1f, 0x0000 }
2224 };
2225
françois romieu4da19632011-01-03 15:07:55 +00002226 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002227
françois romieu4da19632011-01-03 15:07:55 +00002228 rtl_patchphy(tp, 0x16, 1 << 0);
2229 rtl_patchphy(tp, 0x14, 1 << 5);
2230 rtl_patchphy(tp, 0x0d, 1 << 5);
2231 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002232}
2233
françois romieu4da19632011-01-03 15:07:55 +00002234static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002235{
françois romieu4da19632011-01-03 15:07:55 +00002236 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002237}
2238
françois romieubca03d52011-01-03 15:07:31 +00002239static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002240{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002241 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002242 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002243 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002244 { 0x06, 0x4064 },
2245 { 0x07, 0x2863 },
2246 { 0x08, 0x059c },
2247 { 0x09, 0x26b4 },
2248 { 0x0a, 0x6a19 },
2249 { 0x0b, 0xdcc8 },
2250 { 0x10, 0xf06d },
2251 { 0x14, 0x7f68 },
2252 { 0x18, 0x7fd9 },
2253 { 0x1c, 0xf0ff },
2254 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002255 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002256 { 0x12, 0xf49f },
2257 { 0x13, 0x070b },
2258 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002259 { 0x14, 0x94c0 },
2260
2261 /*
2262 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002263 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002264 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002265 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002266 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002267 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002268 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002269 { 0x06, 0x5561 },
2270
2271 /*
2272 * Can not link to 1Gbps with bad cable
2273 * Decrease SNR threshold form 21.07dB to 19.04dB
2274 */
2275 { 0x1f, 0x0001 },
2276 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002277
2278 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002279 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002280 };
françois romieubca03d52011-01-03 15:07:31 +00002281 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002282
françois romieu4da19632011-01-03 15:07:55 +00002283 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002284
françois romieubca03d52011-01-03 15:07:31 +00002285 /*
2286 * Rx Error Issue
2287 * Fine Tune Switching regulator parameter
2288 */
françois romieu4da19632011-01-03 15:07:55 +00002289 rtl_writephy(tp, 0x1f, 0x0002);
2290 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2291 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002292
françois romieudaf9df62009-10-07 12:44:20 +00002293 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002294 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002295 { 0x1f, 0x0002 },
2296 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002297 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002298 { 0x05, 0x8330 },
2299 { 0x06, 0x669a },
2300 { 0x1f, 0x0002 }
2301 };
2302 int val;
2303
françois romieu4da19632011-01-03 15:07:55 +00002304 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002305
françois romieu4da19632011-01-03 15:07:55 +00002306 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002307
2308 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002309 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002310 0x0065, 0x0066, 0x0067, 0x0068,
2311 0x0069, 0x006a, 0x006b, 0x006c
2312 };
2313 int i;
2314
françois romieu4da19632011-01-03 15:07:55 +00002315 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002316
2317 val &= 0xff00;
2318 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002319 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002320 }
2321 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002322 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002323 { 0x1f, 0x0002 },
2324 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002325 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002326 { 0x05, 0x8330 },
2327 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002328 };
2329
françois romieu4da19632011-01-03 15:07:55 +00002330 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002331 }
2332
françois romieubca03d52011-01-03 15:07:31 +00002333 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002334 rtl_writephy(tp, 0x1f, 0x0002);
2335 rtl_patchphy(tp, 0x0d, 0x0300);
2336 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002337
françois romieubca03d52011-01-03 15:07:31 +00002338 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002339 rtl_writephy(tp, 0x1f, 0x0002);
2340 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2341 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002342
françois romieu4da19632011-01-03 15:07:55 +00002343 rtl_writephy(tp, 0x1f, 0x0005);
2344 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002345
2346 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002347
françois romieu4da19632011-01-03 15:07:55 +00002348 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002349}
2350
françois romieubca03d52011-01-03 15:07:31 +00002351static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002352{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002353 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002354 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002355 { 0x1f, 0x0001 },
2356 { 0x06, 0x4064 },
2357 { 0x07, 0x2863 },
2358 { 0x08, 0x059c },
2359 { 0x09, 0x26b4 },
2360 { 0x0a, 0x6a19 },
2361 { 0x0b, 0xdcc8 },
2362 { 0x10, 0xf06d },
2363 { 0x14, 0x7f68 },
2364 { 0x18, 0x7fd9 },
2365 { 0x1c, 0xf0ff },
2366 { 0x1d, 0x3d9c },
2367 { 0x1f, 0x0003 },
2368 { 0x12, 0xf49f },
2369 { 0x13, 0x070b },
2370 { 0x1a, 0x05ad },
2371 { 0x14, 0x94c0 },
2372
françois romieubca03d52011-01-03 15:07:31 +00002373 /*
2374 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002375 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002376 */
françois romieudaf9df62009-10-07 12:44:20 +00002377 { 0x1f, 0x0002 },
2378 { 0x06, 0x5561 },
2379 { 0x1f, 0x0005 },
2380 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002381 { 0x06, 0x5561 },
2382
2383 /*
2384 * Can not link to 1Gbps with bad cable
2385 * Decrease SNR threshold form 21.07dB to 19.04dB
2386 */
2387 { 0x1f, 0x0001 },
2388 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002389
2390 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002391 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002392 };
françois romieubca03d52011-01-03 15:07:31 +00002393 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002394
françois romieu4da19632011-01-03 15:07:55 +00002395 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002396
2397 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002398 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002399 { 0x1f, 0x0002 },
2400 { 0x05, 0x669a },
2401 { 0x1f, 0x0005 },
2402 { 0x05, 0x8330 },
2403 { 0x06, 0x669a },
2404
2405 { 0x1f, 0x0002 }
2406 };
2407 int val;
2408
françois romieu4da19632011-01-03 15:07:55 +00002409 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002410
françois romieu4da19632011-01-03 15:07:55 +00002411 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002412 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002413 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002414 0x0065, 0x0066, 0x0067, 0x0068,
2415 0x0069, 0x006a, 0x006b, 0x006c
2416 };
2417 int i;
2418
françois romieu4da19632011-01-03 15:07:55 +00002419 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002420
2421 val &= 0xff00;
2422 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002423 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002424 }
2425 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002426 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002427 { 0x1f, 0x0002 },
2428 { 0x05, 0x2642 },
2429 { 0x1f, 0x0005 },
2430 { 0x05, 0x8330 },
2431 { 0x06, 0x2642 }
2432 };
2433
françois romieu4da19632011-01-03 15:07:55 +00002434 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002435 }
2436
françois romieubca03d52011-01-03 15:07:31 +00002437 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002438 rtl_writephy(tp, 0x1f, 0x0002);
2439 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2440 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002441
françois romieubca03d52011-01-03 15:07:31 +00002442 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002443 rtl_writephy(tp, 0x1f, 0x0002);
2444 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002445
françois romieu4da19632011-01-03 15:07:55 +00002446 rtl_writephy(tp, 0x1f, 0x0005);
2447 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002448
2449 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002450
françois romieu4da19632011-01-03 15:07:55 +00002451 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002452}
2453
françois romieu4da19632011-01-03 15:07:55 +00002454static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002455{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002456 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002457 { 0x1f, 0x0002 },
2458 { 0x10, 0x0008 },
2459 { 0x0d, 0x006c },
2460
2461 { 0x1f, 0x0000 },
2462 { 0x0d, 0xf880 },
2463
2464 { 0x1f, 0x0001 },
2465 { 0x17, 0x0cc0 },
2466
2467 { 0x1f, 0x0001 },
2468 { 0x0b, 0xa4d8 },
2469 { 0x09, 0x281c },
2470 { 0x07, 0x2883 },
2471 { 0x0a, 0x6b35 },
2472 { 0x1d, 0x3da4 },
2473 { 0x1c, 0xeffd },
2474 { 0x14, 0x7f52 },
2475 { 0x18, 0x7fc6 },
2476 { 0x08, 0x0601 },
2477 { 0x06, 0x4063 },
2478 { 0x10, 0xf074 },
2479 { 0x1f, 0x0003 },
2480 { 0x13, 0x0789 },
2481 { 0x12, 0xf4bd },
2482 { 0x1a, 0x04fd },
2483 { 0x14, 0x84b0 },
2484 { 0x1f, 0x0000 },
2485 { 0x00, 0x9200 },
2486
2487 { 0x1f, 0x0005 },
2488 { 0x01, 0x0340 },
2489 { 0x1f, 0x0001 },
2490 { 0x04, 0x4000 },
2491 { 0x03, 0x1d21 },
2492 { 0x02, 0x0c32 },
2493 { 0x01, 0x0200 },
2494 { 0x00, 0x5554 },
2495 { 0x04, 0x4800 },
2496 { 0x04, 0x4000 },
2497 { 0x04, 0xf000 },
2498 { 0x03, 0xdf01 },
2499 { 0x02, 0xdf20 },
2500 { 0x01, 0x101a },
2501 { 0x00, 0xa0ff },
2502 { 0x04, 0xf800 },
2503 { 0x04, 0xf000 },
2504 { 0x1f, 0x0000 },
2505
2506 { 0x1f, 0x0007 },
2507 { 0x1e, 0x0023 },
2508 { 0x16, 0x0000 },
2509 { 0x1f, 0x0000 }
2510 };
2511
françois romieu4da19632011-01-03 15:07:55 +00002512 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002513}
2514
françois romieue6de30d2011-01-03 15:08:37 +00002515static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2516{
2517 static const struct phy_reg phy_reg_init[] = {
2518 { 0x1f, 0x0001 },
2519 { 0x17, 0x0cc0 },
2520
2521 { 0x1f, 0x0007 },
2522 { 0x1e, 0x002d },
2523 { 0x18, 0x0040 },
2524 { 0x1f, 0x0000 }
2525 };
2526
2527 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2528 rtl_patchphy(tp, 0x0d, 1 << 5);
2529}
2530
hayeswang01dc7fe2011-03-21 01:50:28 +00002531static void rtl8168e_hw_phy_config(struct rtl8169_private *tp)
2532{
2533 static const struct phy_reg phy_reg_init[] = {
2534 /* Enable Delay cap */
2535 { 0x1f, 0x0005 },
2536 { 0x05, 0x8b80 },
2537 { 0x06, 0xc896 },
2538 { 0x1f, 0x0000 },
2539
2540 /* Channel estimation fine tune */
2541 { 0x1f, 0x0001 },
2542 { 0x0b, 0x6c20 },
2543 { 0x07, 0x2872 },
2544 { 0x1c, 0xefff },
2545 { 0x1f, 0x0003 },
2546 { 0x14, 0x6420 },
2547 { 0x1f, 0x0000 },
2548
2549 /* Update PFM & 10M TX idle timer */
2550 { 0x1f, 0x0007 },
2551 { 0x1e, 0x002f },
2552 { 0x15, 0x1919 },
2553 { 0x1f, 0x0000 },
2554
2555 { 0x1f, 0x0007 },
2556 { 0x1e, 0x00ac },
2557 { 0x18, 0x0006 },
2558 { 0x1f, 0x0000 }
2559 };
2560
Francois Romieu15ecd032011-04-27 13:52:22 -07002561 rtl_apply_firmware(tp);
2562
hayeswang01dc7fe2011-03-21 01:50:28 +00002563 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2564
2565 /* DCO enable for 10M IDLE Power */
2566 rtl_writephy(tp, 0x1f, 0x0007);
2567 rtl_writephy(tp, 0x1e, 0x0023);
2568 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2569 rtl_writephy(tp, 0x1f, 0x0000);
2570
2571 /* For impedance matching */
2572 rtl_writephy(tp, 0x1f, 0x0002);
2573 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002574 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002575
2576 /* PHY auto speed down */
2577 rtl_writephy(tp, 0x1f, 0x0007);
2578 rtl_writephy(tp, 0x1e, 0x002d);
2579 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2580 rtl_writephy(tp, 0x1f, 0x0000);
2581 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2582
2583 rtl_writephy(tp, 0x1f, 0x0005);
2584 rtl_writephy(tp, 0x05, 0x8b86);
2585 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2586 rtl_writephy(tp, 0x1f, 0x0000);
2587
2588 rtl_writephy(tp, 0x1f, 0x0005);
2589 rtl_writephy(tp, 0x05, 0x8b85);
2590 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2591 rtl_writephy(tp, 0x1f, 0x0007);
2592 rtl_writephy(tp, 0x1e, 0x0020);
2593 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2594 rtl_writephy(tp, 0x1f, 0x0006);
2595 rtl_writephy(tp, 0x00, 0x5a00);
2596 rtl_writephy(tp, 0x1f, 0x0000);
2597 rtl_writephy(tp, 0x0d, 0x0007);
2598 rtl_writephy(tp, 0x0e, 0x003c);
2599 rtl_writephy(tp, 0x0d, 0x4007);
2600 rtl_writephy(tp, 0x0e, 0x0000);
2601 rtl_writephy(tp, 0x0d, 0x0000);
2602}
2603
françois romieu4da19632011-01-03 15:07:55 +00002604static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02002605{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002606 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02002607 { 0x1f, 0x0003 },
2608 { 0x08, 0x441d },
2609 { 0x01, 0x9100 },
2610 { 0x1f, 0x0000 }
2611 };
2612
françois romieu4da19632011-01-03 15:07:55 +00002613 rtl_writephy(tp, 0x1f, 0x0000);
2614 rtl_patchphy(tp, 0x11, 1 << 12);
2615 rtl_patchphy(tp, 0x19, 1 << 13);
2616 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002617
françois romieu4da19632011-01-03 15:07:55 +00002618 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02002619}
2620
Hayes Wang5a5e4442011-02-22 17:26:21 +08002621static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
2622{
2623 static const struct phy_reg phy_reg_init[] = {
2624 { 0x1f, 0x0005 },
2625 { 0x1a, 0x0000 },
2626 { 0x1f, 0x0000 },
2627
2628 { 0x1f, 0x0004 },
2629 { 0x1c, 0x0000 },
2630 { 0x1f, 0x0000 },
2631
2632 { 0x1f, 0x0001 },
2633 { 0x15, 0x7701 },
2634 { 0x1f, 0x0000 }
2635 };
2636
2637 /* Disable ALDPS before ram code */
2638 rtl_writephy(tp, 0x1f, 0x0000);
2639 rtl_writephy(tp, 0x18, 0x0310);
2640 msleep(100);
2641
François Romieu953a12c2011-04-24 17:38:48 +02002642 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08002643
2644 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2645}
2646
Francois Romieu5615d9f2007-08-17 17:50:46 +02002647static void rtl_hw_phy_config(struct net_device *dev)
2648{
2649 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002650
2651 rtl8169_print_mac_version(tp);
2652
2653 switch (tp->mac_version) {
2654 case RTL_GIGA_MAC_VER_01:
2655 break;
2656 case RTL_GIGA_MAC_VER_02:
2657 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00002658 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002659 break;
2660 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00002661 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002662 break;
françois romieu2e9558562009-08-10 19:44:19 +00002663 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00002664 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002665 break;
françois romieu8c7006a2009-08-10 19:43:29 +00002666 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00002667 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00002668 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02002669 case RTL_GIGA_MAC_VER_07:
2670 case RTL_GIGA_MAC_VER_08:
2671 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00002672 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002673 break;
Francois Romieu236b8082008-05-30 16:11:48 +02002674 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00002675 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002676 break;
2677 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00002678 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002679 break;
2680 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00002681 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002682 break;
Francois Romieu867763c2007-08-17 18:21:58 +02002683 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00002684 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002685 break;
2686 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00002687 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002688 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02002689 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00002690 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002691 break;
Francois Romieu197ff762008-06-28 13:16:02 +02002692 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00002693 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02002694 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02002695 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00002696 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002697 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02002698 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002699 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00002700 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02002701 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02002702 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00002703 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002704 break;
2705 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00002706 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002707 break;
2708 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00002709 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02002710 break;
françois romieue6de30d2011-01-03 15:08:37 +00002711 case RTL_GIGA_MAC_VER_28:
2712 rtl8168d_4_hw_phy_config(tp);
2713 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08002714 case RTL_GIGA_MAC_VER_29:
2715 case RTL_GIGA_MAC_VER_30:
2716 rtl8105e_hw_phy_config(tp);
2717 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02002718 case RTL_GIGA_MAC_VER_31:
2719 /* None. */
2720 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00002721 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00002722 case RTL_GIGA_MAC_VER_33:
Francois Romieu15ecd032011-04-27 13:52:22 -07002723 rtl8168e_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00002724 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02002725
Francois Romieu5615d9f2007-08-17 17:50:46 +02002726 default:
2727 break;
2728 }
2729}
2730
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731static void rtl8169_phy_timer(unsigned long __opaque)
2732{
2733 struct net_device *dev = (struct net_device *)__opaque;
2734 struct rtl8169_private *tp = netdev_priv(dev);
2735 struct timer_list *timer = &tp->timer;
2736 void __iomem *ioaddr = tp->mmio_addr;
2737 unsigned long timeout = RTL8169_PHY_TIMEOUT;
2738
Francois Romieubcf0bf92006-07-26 23:14:13 +02002739 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 spin_lock_irq(&tp->lock);
2742
françois romieu4da19632011-01-03 15:07:55 +00002743 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02002744 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 * A busy loop could burn quite a few cycles on nowadays CPU.
2746 * Let's delay the execution of the timer for a few ticks.
2747 */
2748 timeout = HZ/10;
2749 goto out_mod_timer;
2750 }
2751
2752 if (tp->link_ok(ioaddr))
2753 goto out_unlock;
2754
Joe Perchesbf82c182010-02-09 11:49:50 +00002755 netif_warn(tp, link, dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
françois romieu4da19632011-01-03 15:07:55 +00002757 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758
2759out_mod_timer:
2760 mod_timer(timer, jiffies + timeout);
2761out_unlock:
2762 spin_unlock_irq(&tp->lock);
2763}
2764
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765#ifdef CONFIG_NET_POLL_CONTROLLER
2766/*
2767 * Polling 'interrupt' - used by things like netconsole to send skbs
2768 * without having to re-enable interrupts. It's not called while
2769 * the interrupt routine is executing.
2770 */
2771static void rtl8169_netpoll(struct net_device *dev)
2772{
2773 struct rtl8169_private *tp = netdev_priv(dev);
2774 struct pci_dev *pdev = tp->pci_dev;
2775
2776 disable_irq(pdev->irq);
David Howells7d12e782006-10-05 14:55:46 +01002777 rtl8169_interrupt(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 enable_irq(pdev->irq);
2779}
2780#endif
2781
2782static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
2783 void __iomem *ioaddr)
2784{
2785 iounmap(ioaddr);
2786 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00002787 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 pci_disable_device(pdev);
2789 free_netdev(dev);
2790}
2791
Francois Romieubf793292006-11-01 00:53:05 +01002792static void rtl8169_phy_reset(struct net_device *dev,
2793 struct rtl8169_private *tp)
2794{
Francois Romieu07d3f512007-02-21 22:40:46 +01002795 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01002796
françois romieu4da19632011-01-03 15:07:55 +00002797 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01002798 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00002799 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01002800 return;
2801 msleep(1);
2802 }
Joe Perchesbf82c182010-02-09 11:49:50 +00002803 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01002804}
2805
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002806static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002808 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002809
Francois Romieu5615d9f2007-08-17 17:50:46 +02002810 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002811
Marcus Sundberg773328942008-07-10 21:28:08 +02002812 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
2813 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2814 RTL_W8(0x82, 0x01);
2815 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002816
Francois Romieu6dccd162007-02-13 23:38:05 +01002817 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
2818
2819 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
2820 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002821
Francois Romieubcf0bf92006-07-26 23:14:13 +02002822 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002823 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2824 RTL_W8(0x82, 0x01);
2825 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00002826 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002827 }
2828
Francois Romieubf793292006-11-01 00:53:05 +01002829 rtl8169_phy_reset(dev, tp);
2830
Oliver Neukum54405cd2011-01-06 21:55:13 +01002831 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002832 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
2833 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
2834 (tp->mii.supports_gmii ?
2835 ADVERTISED_1000baseT_Half |
2836 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002837
Joe Perchesbf82c182010-02-09 11:49:50 +00002838 if (RTL_R8(PHYstatus) & TBI_Enable)
2839 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002840}
2841
Francois Romieu773d2022007-01-31 23:47:43 +01002842static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
2843{
2844 void __iomem *ioaddr = tp->mmio_addr;
2845 u32 high;
2846 u32 low;
2847
2848 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
2849 high = addr[4] | (addr[5] << 8);
2850
2851 spin_lock_irq(&tp->lock);
2852
2853 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00002854
Francois Romieu773d2022007-01-31 23:47:43 +01002855 RTL_W32(MAC4, high);
françois romieu908ba2b2010-04-26 11:42:58 +00002856 RTL_R32(MAC4);
2857
Francois Romieu78f1cd02010-03-27 19:35:46 -07002858 RTL_W32(MAC0, low);
françois romieu908ba2b2010-04-26 11:42:58 +00002859 RTL_R32(MAC0);
2860
Francois Romieu773d2022007-01-31 23:47:43 +01002861 RTL_W8(Cfg9346, Cfg9346_Lock);
2862
2863 spin_unlock_irq(&tp->lock);
2864}
2865
2866static int rtl_set_mac_address(struct net_device *dev, void *p)
2867{
2868 struct rtl8169_private *tp = netdev_priv(dev);
2869 struct sockaddr *addr = p;
2870
2871 if (!is_valid_ether_addr(addr->sa_data))
2872 return -EADDRNOTAVAIL;
2873
2874 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
2875
2876 rtl_rar_set(tp, dev->dev_addr);
2877
2878 return 0;
2879}
2880
Francois Romieu5f787a12006-08-17 13:02:36 +02002881static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2882{
2883 struct rtl8169_private *tp = netdev_priv(dev);
2884 struct mii_ioctl_data *data = if_mii(ifr);
2885
Francois Romieu8b4ab282008-11-19 22:05:25 -08002886 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
2887}
Francois Romieu5f787a12006-08-17 13:02:36 +02002888
Francois Romieucecb5fd2011-04-01 10:21:07 +02002889static int rtl_xmii_ioctl(struct rtl8169_private *tp,
2890 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08002891{
Francois Romieu5f787a12006-08-17 13:02:36 +02002892 switch (cmd) {
2893 case SIOCGMIIPHY:
2894 data->phy_id = 32; /* Internal PHY */
2895 return 0;
2896
2897 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00002898 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02002899 return 0;
2900
2901 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00002902 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02002903 return 0;
2904 }
2905 return -EOPNOTSUPP;
2906}
2907
Francois Romieu8b4ab282008-11-19 22:05:25 -08002908static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
2909{
2910 return -EOPNOTSUPP;
2911}
2912
Francois Romieu0e485152007-02-20 00:00:26 +01002913static const struct rtl_cfg_info {
2914 void (*hw_start)(struct net_device *);
2915 unsigned int region;
2916 unsigned int align;
2917 u16 intr_event;
2918 u16 napi_event;
Francois Romieuccdffb92008-07-26 14:26:06 +02002919 unsigned features;
Jean Delvaref21b75e2009-05-26 20:54:48 -07002920 u8 default_ver;
Francois Romieu0e485152007-02-20 00:00:26 +01002921} rtl_cfg_infos [] = {
2922 [RTL_CFG_0] = {
2923 .hw_start = rtl_hw_start_8169,
2924 .region = 1,
Francois Romieue9f63f32007-02-28 23:16:57 +01002925 .align = 0,
Francois Romieu0e485152007-02-20 00:00:26 +01002926 .intr_event = SYSErr | LinkChg | RxOverflow |
2927 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02002928 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07002929 .features = RTL_FEATURE_GMII,
2930 .default_ver = RTL_GIGA_MAC_VER_01,
Francois Romieu0e485152007-02-20 00:00:26 +01002931 },
2932 [RTL_CFG_1] = {
2933 .hw_start = rtl_hw_start_8168,
2934 .region = 2,
2935 .align = 8,
françois romieu53f57352010-11-08 13:23:05 +00002936 .intr_event = SYSErr | LinkChg | RxOverflow |
Francois Romieu0e485152007-02-20 00:00:26 +01002937 TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02002938 .napi_event = TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07002939 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
2940 .default_ver = RTL_GIGA_MAC_VER_11,
Francois Romieu0e485152007-02-20 00:00:26 +01002941 },
2942 [RTL_CFG_2] = {
2943 .hw_start = rtl_hw_start_8101,
2944 .region = 2,
2945 .align = 8,
2946 .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
2947 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02002948 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07002949 .features = RTL_FEATURE_MSI,
2950 .default_ver = RTL_GIGA_MAC_VER_13,
Francois Romieu0e485152007-02-20 00:00:26 +01002951 }
2952};
2953
Francois Romieufbac58f2007-10-04 22:51:38 +02002954/* Cfg9346_Unlock assumed. */
2955static unsigned rtl_try_msi(struct pci_dev *pdev, void __iomem *ioaddr,
2956 const struct rtl_cfg_info *cfg)
2957{
2958 unsigned msi = 0;
2959 u8 cfg2;
2960
2961 cfg2 = RTL_R8(Config2) & ~MSIEnable;
Francois Romieuccdffb92008-07-26 14:26:06 +02002962 if (cfg->features & RTL_FEATURE_MSI) {
Francois Romieufbac58f2007-10-04 22:51:38 +02002963 if (pci_enable_msi(pdev)) {
2964 dev_info(&pdev->dev, "no MSI. Back to INTx.\n");
2965 } else {
2966 cfg2 |= MSIEnable;
2967 msi = RTL_FEATURE_MSI;
2968 }
2969 }
2970 RTL_W8(Config2, cfg2);
2971 return msi;
2972}
2973
2974static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
2975{
2976 if (tp->features & RTL_FEATURE_MSI) {
2977 pci_disable_msi(pdev);
2978 tp->features &= ~RTL_FEATURE_MSI;
2979 }
2980}
2981
Francois Romieu8b4ab282008-11-19 22:05:25 -08002982static const struct net_device_ops rtl8169_netdev_ops = {
2983 .ndo_open = rtl8169_open,
2984 .ndo_stop = rtl8169_close,
2985 .ndo_get_stats = rtl8169_get_stats,
Stephen Hemminger00829822008-11-20 20:14:53 -08002986 .ndo_start_xmit = rtl8169_start_xmit,
Francois Romieu8b4ab282008-11-19 22:05:25 -08002987 .ndo_tx_timeout = rtl8169_tx_timeout,
2988 .ndo_validate_addr = eth_validate_addr,
2989 .ndo_change_mtu = rtl8169_change_mtu,
Michał Mirosław350fb322011-04-08 06:35:56 +00002990 .ndo_fix_features = rtl8169_fix_features,
2991 .ndo_set_features = rtl8169_set_features,
Francois Romieu8b4ab282008-11-19 22:05:25 -08002992 .ndo_set_mac_address = rtl_set_mac_address,
2993 .ndo_do_ioctl = rtl8169_ioctl,
2994 .ndo_set_multicast_list = rtl_set_rx_mode,
Francois Romieu8b4ab282008-11-19 22:05:25 -08002995#ifdef CONFIG_NET_POLL_CONTROLLER
2996 .ndo_poll_controller = rtl8169_netpoll,
2997#endif
2998
2999};
3000
françois romieuc0e45c12011-01-03 15:08:04 +00003001static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3002{
3003 struct mdio_ops *ops = &tp->mdio_ops;
3004
3005 switch (tp->mac_version) {
3006 case RTL_GIGA_MAC_VER_27:
3007 ops->write = r8168dp_1_mdio_write;
3008 ops->read = r8168dp_1_mdio_read;
3009 break;
françois romieue6de30d2011-01-03 15:08:37 +00003010 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003011 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003012 ops->write = r8168dp_2_mdio_write;
3013 ops->read = r8168dp_2_mdio_read;
3014 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003015 default:
3016 ops->write = r8169_mdio_write;
3017 ops->read = r8169_mdio_read;
3018 break;
3019 }
3020}
3021
françois romieu065c27c2011-01-03 15:08:12 +00003022static void r810x_phy_power_down(struct rtl8169_private *tp)
3023{
3024 rtl_writephy(tp, 0x1f, 0x0000);
3025 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3026}
3027
3028static void r810x_phy_power_up(struct rtl8169_private *tp)
3029{
3030 rtl_writephy(tp, 0x1f, 0x0000);
3031 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3032}
3033
3034static void r810x_pll_power_down(struct rtl8169_private *tp)
3035{
3036 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3037 rtl_writephy(tp, 0x1f, 0x0000);
3038 rtl_writephy(tp, MII_BMCR, 0x0000);
3039 return;
3040 }
3041
3042 r810x_phy_power_down(tp);
3043}
3044
3045static void r810x_pll_power_up(struct rtl8169_private *tp)
3046{
3047 r810x_phy_power_up(tp);
3048}
3049
3050static void r8168_phy_power_up(struct rtl8169_private *tp)
3051{
3052 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003053 switch (tp->mac_version) {
3054 case RTL_GIGA_MAC_VER_11:
3055 case RTL_GIGA_MAC_VER_12:
3056 case RTL_GIGA_MAC_VER_17:
3057 case RTL_GIGA_MAC_VER_18:
3058 case RTL_GIGA_MAC_VER_19:
3059 case RTL_GIGA_MAC_VER_20:
3060 case RTL_GIGA_MAC_VER_21:
3061 case RTL_GIGA_MAC_VER_22:
3062 case RTL_GIGA_MAC_VER_23:
3063 case RTL_GIGA_MAC_VER_24:
3064 case RTL_GIGA_MAC_VER_25:
3065 case RTL_GIGA_MAC_VER_26:
3066 case RTL_GIGA_MAC_VER_27:
3067 case RTL_GIGA_MAC_VER_28:
3068 case RTL_GIGA_MAC_VER_31:
3069 rtl_writephy(tp, 0x0e, 0x0000);
3070 break;
3071 default:
3072 break;
3073 }
françois romieu065c27c2011-01-03 15:08:12 +00003074 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3075}
3076
3077static void r8168_phy_power_down(struct rtl8169_private *tp)
3078{
3079 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003080 switch (tp->mac_version) {
3081 case RTL_GIGA_MAC_VER_32:
3082 case RTL_GIGA_MAC_VER_33:
3083 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3084 break;
3085
3086 case RTL_GIGA_MAC_VER_11:
3087 case RTL_GIGA_MAC_VER_12:
3088 case RTL_GIGA_MAC_VER_17:
3089 case RTL_GIGA_MAC_VER_18:
3090 case RTL_GIGA_MAC_VER_19:
3091 case RTL_GIGA_MAC_VER_20:
3092 case RTL_GIGA_MAC_VER_21:
3093 case RTL_GIGA_MAC_VER_22:
3094 case RTL_GIGA_MAC_VER_23:
3095 case RTL_GIGA_MAC_VER_24:
3096 case RTL_GIGA_MAC_VER_25:
3097 case RTL_GIGA_MAC_VER_26:
3098 case RTL_GIGA_MAC_VER_27:
3099 case RTL_GIGA_MAC_VER_28:
3100 case RTL_GIGA_MAC_VER_31:
3101 rtl_writephy(tp, 0x0e, 0x0200);
3102 default:
3103 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3104 break;
3105 }
françois romieu065c27c2011-01-03 15:08:12 +00003106}
3107
3108static void r8168_pll_power_down(struct rtl8169_private *tp)
3109{
3110 void __iomem *ioaddr = tp->mmio_addr;
3111
Francois Romieucecb5fd2011-04-01 10:21:07 +02003112 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3113 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3114 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003115 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003116 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003117 }
françois romieu065c27c2011-01-03 15:08:12 +00003118
Francois Romieucecb5fd2011-04-01 10:21:07 +02003119 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3120 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00003121 (RTL_R16(CPlusCmd) & ASF)) {
3122 return;
3123 }
3124
hayeswang01dc7fe2011-03-21 01:50:28 +00003125 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3126 tp->mac_version == RTL_GIGA_MAC_VER_33)
3127 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3128
françois romieu065c27c2011-01-03 15:08:12 +00003129 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3130 rtl_writephy(tp, 0x1f, 0x0000);
3131 rtl_writephy(tp, MII_BMCR, 0x0000);
3132
3133 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3134 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3135 return;
3136 }
3137
3138 r8168_phy_power_down(tp);
3139
3140 switch (tp->mac_version) {
3141 case RTL_GIGA_MAC_VER_25:
3142 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003143 case RTL_GIGA_MAC_VER_27:
3144 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003145 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003146 case RTL_GIGA_MAC_VER_32:
3147 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003148 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3149 break;
3150 }
3151}
3152
3153static void r8168_pll_power_up(struct rtl8169_private *tp)
3154{
3155 void __iomem *ioaddr = tp->mmio_addr;
3156
Francois Romieucecb5fd2011-04-01 10:21:07 +02003157 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3158 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3159 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003160 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003161 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003162 }
françois romieu065c27c2011-01-03 15:08:12 +00003163
3164 switch (tp->mac_version) {
3165 case RTL_GIGA_MAC_VER_25:
3166 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003167 case RTL_GIGA_MAC_VER_27:
3168 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003169 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003170 case RTL_GIGA_MAC_VER_32:
3171 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003172 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3173 break;
3174 }
3175
3176 r8168_phy_power_up(tp);
3177}
3178
3179static void rtl_pll_power_op(struct rtl8169_private *tp,
3180 void (*op)(struct rtl8169_private *))
3181{
3182 if (op)
3183 op(tp);
3184}
3185
3186static void rtl_pll_power_down(struct rtl8169_private *tp)
3187{
3188 rtl_pll_power_op(tp, tp->pll_power_ops.down);
3189}
3190
3191static void rtl_pll_power_up(struct rtl8169_private *tp)
3192{
3193 rtl_pll_power_op(tp, tp->pll_power_ops.up);
3194}
3195
3196static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3197{
3198 struct pll_power_ops *ops = &tp->pll_power_ops;
3199
3200 switch (tp->mac_version) {
3201 case RTL_GIGA_MAC_VER_07:
3202 case RTL_GIGA_MAC_VER_08:
3203 case RTL_GIGA_MAC_VER_09:
3204 case RTL_GIGA_MAC_VER_10:
3205 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003206 case RTL_GIGA_MAC_VER_29:
3207 case RTL_GIGA_MAC_VER_30:
françois romieu065c27c2011-01-03 15:08:12 +00003208 ops->down = r810x_pll_power_down;
3209 ops->up = r810x_pll_power_up;
3210 break;
3211
3212 case RTL_GIGA_MAC_VER_11:
3213 case RTL_GIGA_MAC_VER_12:
3214 case RTL_GIGA_MAC_VER_17:
3215 case RTL_GIGA_MAC_VER_18:
3216 case RTL_GIGA_MAC_VER_19:
3217 case RTL_GIGA_MAC_VER_20:
3218 case RTL_GIGA_MAC_VER_21:
3219 case RTL_GIGA_MAC_VER_22:
3220 case RTL_GIGA_MAC_VER_23:
3221 case RTL_GIGA_MAC_VER_24:
3222 case RTL_GIGA_MAC_VER_25:
3223 case RTL_GIGA_MAC_VER_26:
3224 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003225 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003226 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003227 case RTL_GIGA_MAC_VER_32:
3228 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003229 ops->down = r8168_pll_power_down;
3230 ops->up = r8168_pll_power_up;
3231 break;
3232
3233 default:
3234 ops->down = NULL;
3235 ops->up = NULL;
3236 break;
3237 }
3238}
3239
Francois Romieu6f43adc2011-04-29 15:05:51 +02003240static void rtl_hw_reset(struct rtl8169_private *tp)
3241{
3242 void __iomem *ioaddr = tp->mmio_addr;
3243 int i;
3244
3245 /* Soft reset the chip. */
3246 RTL_W8(ChipCmd, CmdReset);
3247
3248 /* Check that the chip has finished the reset. */
3249 for (i = 0; i < 100; i++) {
3250 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3251 break;
3252 msleep_interruptible(1);
3253 }
3254}
3255
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003256static int __devinit
3257rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
3258{
Francois Romieu0e485152007-02-20 00:00:26 +01003259 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
3260 const unsigned int region = cfg->region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 struct rtl8169_private *tp;
Francois Romieuccdffb92008-07-26 14:26:06 +02003262 struct mii_if_info *mii;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003263 struct net_device *dev;
3264 void __iomem *ioaddr;
Francois Romieu2b7b4312011-04-18 22:53:24 -07003265 int chipset, i;
Francois Romieu07d3f512007-02-21 22:40:46 +01003266 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003268 if (netif_msg_drv(&debug)) {
3269 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
3270 MODULENAME, RTL8169_VERSION);
3271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 dev = alloc_etherdev(sizeof (*tp));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003274 if (!dev) {
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003275 if (netif_msg_drv(&debug))
Jeff Garzik9b91cf92006-06-27 11:39:50 -04003276 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003277 rc = -ENOMEM;
3278 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 }
3280
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieu8b4ab282008-11-19 22:05:25 -08003282 dev->netdev_ops = &rtl8169_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 tp = netdev_priv(dev);
David Howellsc4028952006-11-22 14:57:56 +00003284 tp->dev = dev;
Ivan Vecera21e197f2008-04-17 22:48:41 +02003285 tp->pci_dev = pdev;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003286 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287
Francois Romieuccdffb92008-07-26 14:26:06 +02003288 mii = &tp->mii;
3289 mii->dev = dev;
3290 mii->mdio_read = rtl_mdio_read;
3291 mii->mdio_write = rtl_mdio_write;
3292 mii->phy_id_mask = 0x1f;
3293 mii->reg_num_mask = 0x1f;
3294 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
3295
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +00003296 /* disable ASPM completely as that cause random device stop working
3297 * problems as well as full system hangs for some PCIe devices users */
3298 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
3299 PCIE_LINK_STATE_CLKPM);
3300
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 /* enable device (incl. PCI PM wakeup and hotplug setup) */
3302 rc = pci_enable_device(pdev);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003303 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003304 netif_err(tp, probe, dev, "enable failure\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003305 goto err_out_free_dev_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 }
3307
françois romieu87aeec72010-04-26 11:42:06 +00003308 if (pci_set_mwi(pdev) < 0)
3309 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 /* make sure PCI base addr 1 is MMIO */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003312 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003313 netif_err(tp, probe, dev,
3314 "region #%d not an MMIO resource, aborting\n",
3315 region);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003317 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003319
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 /* check for weird/broken PCI region reporting */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003321 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003322 netif_err(tp, probe, dev,
3323 "Invalid PCI region size(s), aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003325 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 }
3327
3328 rc = pci_request_regions(pdev, MODULENAME);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003329 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003330 netif_err(tp, probe, dev, "could not request regions\n");
françois romieu87aeec72010-04-26 11:42:06 +00003331 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 }
3333
Hayes Wangd24e9aa2011-02-22 17:26:19 +08003334 tp->cp_cmd = RxChkSum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335
3336 if ((sizeof(dma_addr_t) > 4) &&
David S. Miller4300e8c2010-03-26 10:23:30 -07003337 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 tp->cp_cmd |= PCIDAC;
3339 dev->features |= NETIF_F_HIGHDMA;
3340 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07003341 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003343 netif_err(tp, probe, dev, "DMA configuration failed\n");
françois romieu87aeec72010-04-26 11:42:06 +00003344 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 }
3346 }
3347
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 /* ioremap MMIO region */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003349 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003350 if (!ioaddr) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003351 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 rc = -EIO;
françois romieu87aeec72010-04-26 11:42:06 +00003353 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 }
Francois Romieu6f43adc2011-04-29 15:05:51 +02003355 tp->mmio_addr = ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356
David S. Miller4300e8c2010-03-26 10:23:30 -07003357 tp->pcie_cap = pci_find_capability(pdev, PCI_CAP_ID_EXP);
3358 if (!tp->pcie_cap)
3359 netif_info(tp, probe, dev, "no PCI Express capability\n");
3360
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003361 RTL_W16(IntrMask, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362
Francois Romieu6f43adc2011-04-29 15:05:51 +02003363 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003365 RTL_W16(IntrStatus, 0xffff);
3366
françois romieuca52efd2009-07-24 12:34:19 +00003367 pci_set_master(pdev);
3368
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 /* Identify chip attached to board */
Francois Romieu5d320a22011-05-08 17:47:36 +02003370 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371
Francois Romieu7a8fc772011-03-01 17:18:33 +01003372 /*
3373 * Pretend we are using VLANs; This bypasses a nasty bug where
3374 * Interrupts stop flowing on high load on 8110SCd controllers.
3375 */
3376 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3377 tp->cp_cmd |= RxVlan;
3378
françois romieuc0e45c12011-01-03 15:08:04 +00003379 rtl_init_mdio_ops(tp);
françois romieu065c27c2011-01-03 15:08:12 +00003380 rtl_init_pll_power_ops(tp);
françois romieuc0e45c12011-01-03 15:08:04 +00003381
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 rtl8169_print_mac_version(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383
Francois Romieu85bffe62011-04-27 08:22:39 +02003384 chipset = tp->mac_version;
3385 tp->txd_version = rtl_chip_infos[chipset].txd_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386
Francois Romieu5d06a992006-02-23 00:47:58 +01003387 RTL_W8(Cfg9346, Cfg9346_Unlock);
3388 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
3389 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
Bruno Prémont20037fa2008-10-08 17:05:03 -07003390 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
3391 tp->features |= RTL_FEATURE_WOL;
3392 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
3393 tp->features |= RTL_FEATURE_WOL;
Francois Romieufbac58f2007-10-04 22:51:38 +02003394 tp->features |= rtl_try_msi(pdev, ioaddr, cfg);
Francois Romieu5d06a992006-02-23 00:47:58 +01003395 RTL_W8(Cfg9346, Cfg9346_Lock);
3396
Francois Romieu66ec5d42007-11-06 22:56:10 +01003397 if ((tp->mac_version <= RTL_GIGA_MAC_VER_06) &&
3398 (RTL_R8(PHYstatus) & TBI_Enable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 tp->set_speed = rtl8169_set_speed_tbi;
3400 tp->get_settings = rtl8169_gset_tbi;
3401 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
3402 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
3403 tp->link_ok = rtl8169_tbi_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003404 tp->do_ioctl = rtl_tbi_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 } else {
3406 tp->set_speed = rtl8169_set_speed_xmii;
3407 tp->get_settings = rtl8169_gset_xmii;
3408 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
3409 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
3410 tp->link_ok = rtl8169_xmii_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003411 tp->do_ioctl = rtl_xmii_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 }
3413
Francois Romieudf58ef52008-10-09 14:35:58 -07003414 spin_lock_init(&tp->lock);
3415
Ivan Vecera7bf6bf42008-09-23 22:46:29 +00003416 /* Get MAC address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 for (i = 0; i < MAC_ADDR_LEN; i++)
3418 dev->dev_addr[i] = RTL_R8(MAC0 + i);
John W. Linville6d6525b2005-09-12 10:48:57 -04003419 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
3423 dev->irq = pdev->irq;
3424 dev->base_addr = (unsigned long) ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003426 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427
Michał Mirosław350fb322011-04-08 06:35:56 +00003428 /* don't enable SG, IP_CSUM and TSO by default - it might not work
3429 * properly for all devices */
3430 dev->features |= NETIF_F_RXCSUM |
3431 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3432
3433 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3434 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3435 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3436 NETIF_F_HIGHDMA;
3437
3438 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3439 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
3440 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441
3442 tp->intr_mask = 0xffff;
Francois Romieu0e485152007-02-20 00:00:26 +01003443 tp->hw_start = cfg->hw_start;
3444 tp->intr_event = cfg->intr_event;
3445 tp->napi_event = cfg->napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446
Francois Romieu2efa53f2007-03-09 00:00:05 +01003447 init_timer(&tp->timer);
3448 tp->timer.data = (unsigned long) dev;
3449 tp->timer.function = rtl8169_phy_timer;
3450
Francois Romieub6ffd972011-06-17 17:00:05 +02003451 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
François Romieu953a12c2011-04-24 17:38:48 +02003452
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 rc = register_netdev(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003454 if (rc < 0)
françois romieu87aeec72010-04-26 11:42:06 +00003455 goto err_out_msi_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456
3457 pci_set_drvdata(pdev, dev);
3458
Joe Perchesbf82c182010-02-09 11:49:50 +00003459 netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
Francois Romieu85bffe62011-04-27 08:22:39 +02003460 rtl_chip_infos[chipset].name, dev->base_addr, dev->dev_addr,
Joe Perchesbf82c182010-02-09 11:49:50 +00003461 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462
Francois Romieucecb5fd2011-04-01 10:21:07 +02003463 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3464 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3465 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00003466 rtl8168_driver_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003467 }
françois romieub646d902011-01-03 15:08:21 +00003468
Bruno Prémont8b76ab32008-10-08 17:06:25 -07003469 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470
Alan Sternf3ec4f82010-06-08 15:23:51 -04003471 if (pci_dev_run_wake(pdev))
3472 pm_runtime_put_noidle(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003473
Ivan Vecera0d672e92011-02-15 02:08:39 +00003474 netif_carrier_off(dev);
3475
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003476out:
3477 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478
françois romieu87aeec72010-04-26 11:42:06 +00003479err_out_msi_4:
Francois Romieufbac58f2007-10-04 22:51:38 +02003480 rtl_disable_msi(pdev, tp);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003481 iounmap(ioaddr);
françois romieu87aeec72010-04-26 11:42:06 +00003482err_out_free_res_3:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003483 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003484err_out_mwi_2:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003485 pci_clear_mwi(pdev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003486 pci_disable_device(pdev);
3487err_out_free_dev_1:
3488 free_netdev(dev);
3489 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490}
3491
Francois Romieu07d3f512007-02-21 22:40:46 +01003492static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493{
3494 struct net_device *dev = pci_get_drvdata(pdev);
3495 struct rtl8169_private *tp = netdev_priv(dev);
3496
Francois Romieucecb5fd2011-04-01 10:21:07 +02003497 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3498 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3499 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00003500 rtl8168_driver_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003501 }
françois romieub646d902011-01-03 15:08:21 +00003502
Tejun Heo23f333a2010-12-12 16:45:14 +01003503 cancel_delayed_work_sync(&tp->task);
Francois Romieueb2a0212007-02-15 23:37:21 +01003504
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 unregister_netdev(dev);
Ivan Veceracc098dc2009-11-29 23:12:52 -08003506
François Romieu953a12c2011-04-24 17:38:48 +02003507 rtl_release_firmware(tp);
3508
Alan Sternf3ec4f82010-06-08 15:23:51 -04003509 if (pci_dev_run_wake(pdev))
3510 pm_runtime_get_noresume(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003511
Ivan Veceracc098dc2009-11-29 23:12:52 -08003512 /* restore original MAC address */
3513 rtl_rar_set(tp, dev->perm_addr);
3514
Francois Romieufbac58f2007-10-04 22:51:38 +02003515 rtl_disable_msi(pdev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 rtl8169_release_board(pdev, dev, tp->mmio_addr);
3517 pci_set_drvdata(pdev, NULL);
3518}
3519
Francois Romieub6ffd972011-06-17 17:00:05 +02003520static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
3521{
3522 struct rtl_fw *rtl_fw;
3523 const char *name;
3524 int rc = -ENOMEM;
3525
3526 name = rtl_lookup_firmware_name(tp);
3527 if (!name)
3528 goto out_no_firmware;
3529
3530 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
3531 if (!rtl_fw)
3532 goto err_warn;
3533
3534 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
3535 if (rc < 0)
3536 goto err_free;
3537
3538 tp->rtl_fw = rtl_fw;
3539out:
3540 return;
3541
3542err_free:
3543 kfree(rtl_fw);
3544err_warn:
3545 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
3546 name, rc);
3547out_no_firmware:
3548 tp->rtl_fw = NULL;
3549 goto out;
3550}
3551
François Romieu953a12c2011-04-24 17:38:48 +02003552static void rtl_request_firmware(struct rtl8169_private *tp)
3553{
Francois Romieub6ffd972011-06-17 17:00:05 +02003554 if (IS_ERR(tp->rtl_fw))
3555 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02003556}
3557
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558static int rtl8169_open(struct net_device *dev)
3559{
3560 struct rtl8169_private *tp = netdev_priv(dev);
françois romieueee3a962011-01-08 02:17:26 +00003561 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu99f252b2007-04-02 22:59:59 +02003563 int retval = -ENOMEM;
3564
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003565 pm_runtime_get_sync(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566
Neil Hormanc0cd8842010-03-29 13:16:02 -07003567 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 * Rx and Tx desscriptors needs 256 bytes alignment.
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003569 * dma_alloc_coherent provides more.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 */
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003571 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
3572 &tp->TxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 if (!tp->TxDescArray)
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003574 goto err_pm_runtime_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003576 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
3577 &tp->RxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 if (!tp->RxDescArray)
Francois Romieu99f252b2007-04-02 22:59:59 +02003579 goto err_free_tx_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580
3581 retval = rtl8169_init_ring(dev);
3582 if (retval < 0)
Francois Romieu99f252b2007-04-02 22:59:59 +02003583 goto err_free_rx_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584
David Howellsc4028952006-11-22 14:57:56 +00003585 INIT_DELAYED_WORK(&tp->task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586
Francois Romieu99f252b2007-04-02 22:59:59 +02003587 smp_mb();
3588
François Romieu953a12c2011-04-24 17:38:48 +02003589 rtl_request_firmware(tp);
3590
Francois Romieufbac58f2007-10-04 22:51:38 +02003591 retval = request_irq(dev->irq, rtl8169_interrupt,
3592 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
Francois Romieu99f252b2007-04-02 22:59:59 +02003593 dev->name, dev);
3594 if (retval < 0)
François Romieu953a12c2011-04-24 17:38:48 +02003595 goto err_release_fw_2;
Francois Romieu99f252b2007-04-02 22:59:59 +02003596
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003597 napi_enable(&tp->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003598
françois romieueee3a962011-01-08 02:17:26 +00003599 rtl8169_init_phy(dev, tp);
3600
Michał Mirosław350fb322011-04-08 06:35:56 +00003601 rtl8169_set_features(dev, dev->features);
françois romieueee3a962011-01-08 02:17:26 +00003602
françois romieu065c27c2011-01-03 15:08:12 +00003603 rtl_pll_power_up(tp);
3604
Francois Romieu07ce4062007-02-23 23:36:39 +01003605 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003607 tp->saved_wolopts = 0;
3608 pm_runtime_put_noidle(&pdev->dev);
3609
françois romieueee3a962011-01-08 02:17:26 +00003610 rtl8169_check_link_status(dev, tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611out:
3612 return retval;
3613
François Romieu953a12c2011-04-24 17:38:48 +02003614err_release_fw_2:
3615 rtl_release_firmware(tp);
Francois Romieu99f252b2007-04-02 22:59:59 +02003616 rtl8169_rx_clear(tp);
3617err_free_rx_1:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003618 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
3619 tp->RxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003620 tp->RxDescArray = NULL;
Francois Romieu99f252b2007-04-02 22:59:59 +02003621err_free_tx_0:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003622 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
3623 tp->TxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003624 tp->TxDescArray = NULL;
3625err_pm_runtime_put:
3626 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 goto out;
3628}
3629
françois romieue6de30d2011-01-03 15:08:37 +00003630static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631{
françois romieue6de30d2011-01-03 15:08:37 +00003632 void __iomem *ioaddr = tp->mmio_addr;
3633
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 /* Disable interrupts */
3635 rtl8169_irq_mask_and_ack(ioaddr);
3636
Hayes Wang5d2e1952011-02-22 17:26:22 +08003637 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00003638 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3639 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieue6de30d2011-01-03 15:08:37 +00003640 while (RTL_R8(TxPoll) & NPQ)
3641 udelay(20);
3642
3643 }
3644
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 /* Reset the chipset */
3646 RTL_W8(ChipCmd, CmdReset);
3647
3648 /* PCI commit */
3649 RTL_R8(ChipCmd);
3650}
3651
Francois Romieu7f796d82007-06-11 23:04:41 +02003652static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01003653{
3654 void __iomem *ioaddr = tp->mmio_addr;
3655 u32 cfg = rtl8169_rx_config;
3656
Francois Romieu2b7b4312011-04-18 22:53:24 -07003657 cfg |= (RTL_R32(RxConfig) & RTL_RX_CONFIG_MASK);
Francois Romieu9cb427b2006-11-02 00:10:16 +01003658 RTL_W32(RxConfig, cfg);
3659
3660 /* Set DMA burst size and Interframe Gap Time */
3661 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
3662 (InterFrameGap << TxInterFrameGapShift));
3663}
3664
Francois Romieu07ce4062007-02-23 23:36:39 +01003665static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666{
3667 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668
Francois Romieu6f43adc2011-04-29 15:05:51 +02003669 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670
Francois Romieu07ce4062007-02-23 23:36:39 +01003671 tp->hw_start(dev);
3672
Francois Romieu07ce4062007-02-23 23:36:39 +01003673 netif_start_queue(dev);
3674}
3675
Francois Romieu7f796d82007-06-11 23:04:41 +02003676static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
3677 void __iomem *ioaddr)
3678{
3679 /*
3680 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
3681 * register to be written before TxDescAddrLow to work.
3682 * Switching from MMIO to I/O access fixes the issue as well.
3683 */
3684 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07003685 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02003686 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07003687 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02003688}
3689
3690static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
3691{
3692 u16 cmd;
3693
3694 cmd = RTL_R16(CPlusCmd);
3695 RTL_W16(CPlusCmd, cmd);
3696 return cmd;
3697}
3698
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07003699static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d82007-06-11 23:04:41 +02003700{
3701 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e82009-10-26 10:52:37 +00003702 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d82007-06-11 23:04:41 +02003703}
3704
Francois Romieu6dccd162007-02-13 23:38:05 +01003705static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
3706{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003707 static const struct {
Francois Romieu6dccd162007-02-13 23:38:05 +01003708 u32 mac_version;
3709 u32 clk;
3710 u32 val;
3711 } cfg2_info [] = {
3712 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
3713 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
3714 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
3715 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
3716 }, *p = cfg2_info;
3717 unsigned int i;
3718 u32 clk;
3719
3720 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01003721 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01003722 if ((p->mac_version == mac_version) && (p->clk == clk)) {
3723 RTL_W32(0x7c, p->val);
3724 break;
3725 }
3726 }
3727}
3728
Francois Romieu07ce4062007-02-23 23:36:39 +01003729static void rtl_hw_start_8169(struct net_device *dev)
3730{
3731 struct rtl8169_private *tp = netdev_priv(dev);
3732 void __iomem *ioaddr = tp->mmio_addr;
3733 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01003734
Francois Romieu9cb427b2006-11-02 00:10:16 +01003735 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
3736 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
3737 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
3738 }
3739
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02003741 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
3742 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
3743 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
3744 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01003745 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3746
françois romieuf0298f82011-01-03 15:07:42 +00003747 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748
Eric Dumazet6f0333b2010-10-11 11:17:47 +00003749 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750
Francois Romieucecb5fd2011-04-01 10:21:07 +02003751 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
3752 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
3753 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
3754 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02003755 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756
Francois Romieu7f796d82007-06-11 23:04:41 +02003757 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02003758
Francois Romieucecb5fd2011-04-01 10:21:07 +02003759 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
3760 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02003761 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02003763 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 }
3765
Francois Romieubcf0bf92006-07-26 23:14:13 +02003766 RTL_W16(CPlusCmd, tp->cp_cmd);
3767
Francois Romieu6dccd162007-02-13 23:38:05 +01003768 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
3769
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 /*
3771 * Undocumented corner. Supposedly:
3772 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
3773 */
3774 RTL_W16(IntrMitigate, 0x0000);
3775
Francois Romieu7f796d82007-06-11 23:04:41 +02003776 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01003777
Francois Romieucecb5fd2011-04-01 10:21:07 +02003778 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
3779 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
3780 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
3781 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02003782 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3783 rtl_set_rx_tx_config_registers(tp);
3784 }
3785
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02003787
3788 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
3789 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790
3791 RTL_W32(RxMissed, 0);
3792
Francois Romieu07ce4062007-02-23 23:36:39 +01003793 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794
3795 /* no early-rx interrupts */
3796 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01003797
3798 /* Enable all known interrupts by setting the interrupt mask. */
Francois Romieu0e485152007-02-20 00:00:26 +01003799 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01003800}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801
Francois Romieu9c14cea2008-07-05 00:21:15 +02003802static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
Francois Romieu458a9f62008-08-02 15:50:02 +02003803{
Francois Romieu9c14cea2008-07-05 00:21:15 +02003804 struct net_device *dev = pci_get_drvdata(pdev);
3805 struct rtl8169_private *tp = netdev_priv(dev);
3806 int cap = tp->pcie_cap;
Francois Romieu458a9f62008-08-02 15:50:02 +02003807
Francois Romieu9c14cea2008-07-05 00:21:15 +02003808 if (cap) {
3809 u16 ctl;
3810
3811 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
3812 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
3813 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
3814 }
Francois Romieu458a9f62008-08-02 15:50:02 +02003815}
3816
françois romieu650e8d52011-01-03 15:08:29 +00003817static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02003818{
3819 u32 csi;
3820
3821 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
françois romieu650e8d52011-01-03 15:08:29 +00003822 rtl_csi_write(ioaddr, 0x070c, csi | bits);
3823}
3824
françois romieue6de30d2011-01-03 15:08:37 +00003825static void rtl_csi_access_enable_1(void __iomem *ioaddr)
3826{
3827 rtl_csi_access_enable(ioaddr, 0x17000000);
3828}
3829
françois romieu650e8d52011-01-03 15:08:29 +00003830static void rtl_csi_access_enable_2(void __iomem *ioaddr)
3831{
3832 rtl_csi_access_enable(ioaddr, 0x27000000);
Francois Romieudacf8152008-08-02 20:44:13 +02003833}
3834
3835struct ephy_info {
3836 unsigned int offset;
3837 u16 mask;
3838 u16 bits;
3839};
3840
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003841static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02003842{
3843 u16 w;
3844
3845 while (len-- > 0) {
3846 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
3847 rtl_ephy_write(ioaddr, e->offset, w);
3848 e++;
3849 }
3850}
3851
Francois Romieub726e492008-06-28 12:22:59 +02003852static void rtl_disable_clock_request(struct pci_dev *pdev)
3853{
3854 struct net_device *dev = pci_get_drvdata(pdev);
3855 struct rtl8169_private *tp = netdev_priv(dev);
3856 int cap = tp->pcie_cap;
3857
3858 if (cap) {
3859 u16 ctl;
3860
3861 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
3862 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
3863 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
3864 }
3865}
3866
françois romieue6de30d2011-01-03 15:08:37 +00003867static void rtl_enable_clock_request(struct pci_dev *pdev)
3868{
3869 struct net_device *dev = pci_get_drvdata(pdev);
3870 struct rtl8169_private *tp = netdev_priv(dev);
3871 int cap = tp->pcie_cap;
3872
3873 if (cap) {
3874 u16 ctl;
3875
3876 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
3877 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
3878 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
3879 }
3880}
3881
Francois Romieub726e492008-06-28 12:22:59 +02003882#define R8168_CPCMD_QUIRK_MASK (\
3883 EnableBist | \
3884 Mac_dbgo_oe | \
3885 Force_half_dup | \
3886 Force_rxflow_en | \
3887 Force_txflow_en | \
3888 Cxpl_dbg_sel | \
3889 ASF | \
3890 PktCntrDisable | \
3891 Mac_dbgo_sel)
3892
Francois Romieu219a1e92008-06-28 11:58:39 +02003893static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
3894{
Francois Romieub726e492008-06-28 12:22:59 +02003895 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3896
3897 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3898
Francois Romieu2e68ae42008-06-28 12:00:55 +02003899 rtl_tx_performance_tweak(pdev,
3900 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02003901}
3902
3903static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
3904{
3905 rtl_hw_start_8168bb(ioaddr, pdev);
Francois Romieub726e492008-06-28 12:22:59 +02003906
françois romieuf0298f82011-01-03 15:07:42 +00003907 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02003908
3909 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02003910}
3911
3912static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
3913{
Francois Romieub726e492008-06-28 12:22:59 +02003914 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
3915
3916 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3917
Francois Romieu219a1e92008-06-28 11:58:39 +02003918 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02003919
3920 rtl_disable_clock_request(pdev);
3921
3922 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02003923}
3924
Francois Romieuef3386f2008-06-29 12:24:30 +02003925static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
Francois Romieu219a1e92008-06-28 11:58:39 +02003926{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003927 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02003928 { 0x01, 0, 0x0001 },
3929 { 0x02, 0x0800, 0x1000 },
3930 { 0x03, 0, 0x0042 },
3931 { 0x06, 0x0080, 0x0000 },
3932 { 0x07, 0, 0x2000 }
3933 };
3934
françois romieu650e8d52011-01-03 15:08:29 +00003935 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02003936
3937 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
3938
Francois Romieu219a1e92008-06-28 11:58:39 +02003939 __rtl_hw_start_8168cp(ioaddr, pdev);
3940}
3941
Francois Romieuef3386f2008-06-29 12:24:30 +02003942static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
3943{
françois romieu650e8d52011-01-03 15:08:29 +00003944 rtl_csi_access_enable_2(ioaddr);
Francois Romieuef3386f2008-06-29 12:24:30 +02003945
3946 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3947
3948 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3949
3950 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3951}
3952
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003953static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
3954{
françois romieu650e8d52011-01-03 15:08:29 +00003955 rtl_csi_access_enable_2(ioaddr);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003956
3957 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3958
3959 /* Magic. */
3960 RTL_W8(DBG_REG, 0x20);
3961
françois romieuf0298f82011-01-03 15:07:42 +00003962 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003963
3964 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3965
3966 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3967}
3968
Francois Romieu219a1e92008-06-28 11:58:39 +02003969static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
3970{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003971 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02003972 { 0x02, 0x0800, 0x1000 },
3973 { 0x03, 0, 0x0002 },
3974 { 0x06, 0x0080, 0x0000 }
3975 };
3976
françois romieu650e8d52011-01-03 15:08:29 +00003977 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02003978
3979 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
3980
3981 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
3982
Francois Romieu219a1e92008-06-28 11:58:39 +02003983 __rtl_hw_start_8168cp(ioaddr, pdev);
3984}
3985
3986static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
3987{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003988 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02003989 { 0x01, 0, 0x0001 },
3990 { 0x03, 0x0400, 0x0220 }
3991 };
3992
françois romieu650e8d52011-01-03 15:08:29 +00003993 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02003994
3995 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
3996
Francois Romieu219a1e92008-06-28 11:58:39 +02003997 __rtl_hw_start_8168cp(ioaddr, pdev);
3998}
3999
Francois Romieu197ff762008-06-28 13:16:02 +02004000static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
4001{
4002 rtl_hw_start_8168c_2(ioaddr, pdev);
4003}
4004
Francois Romieu6fb07052008-06-29 11:54:28 +02004005static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
4006{
françois romieu650e8d52011-01-03 15:08:29 +00004007 rtl_csi_access_enable_2(ioaddr);
Francois Romieu6fb07052008-06-29 11:54:28 +02004008
4009 __rtl_hw_start_8168cp(ioaddr, pdev);
4010}
4011
Francois Romieu5b538df2008-07-20 16:22:45 +02004012static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
4013{
françois romieu650e8d52011-01-03 15:08:29 +00004014 rtl_csi_access_enable_2(ioaddr);
Francois Romieu5b538df2008-07-20 16:22:45 +02004015
4016 rtl_disable_clock_request(pdev);
4017
françois romieuf0298f82011-01-03 15:07:42 +00004018 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004019
4020 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4021
4022 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4023}
4024
hayeswang4804b3b2011-03-21 01:50:29 +00004025static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
4026{
4027 rtl_csi_access_enable_1(ioaddr);
4028
4029 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4030
4031 RTL_W8(MaxTxPacketSize, TxPacketMax);
4032
4033 rtl_disable_clock_request(pdev);
4034}
4035
françois romieue6de30d2011-01-03 15:08:37 +00004036static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4037{
4038 static const struct ephy_info e_info_8168d_4[] = {
4039 { 0x0b, ~0, 0x48 },
4040 { 0x19, 0x20, 0x50 },
4041 { 0x0c, ~0, 0x20 }
4042 };
4043 int i;
4044
4045 rtl_csi_access_enable_1(ioaddr);
4046
4047 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4048
4049 RTL_W8(MaxTxPacketSize, TxPacketMax);
4050
4051 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4052 const struct ephy_info *e = e_info_8168d_4 + i;
4053 u16 w;
4054
4055 w = rtl_ephy_read(ioaddr, e->offset);
4056 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4057 }
4058
4059 rtl_enable_clock_request(pdev);
4060}
4061
hayeswang01dc7fe2011-03-21 01:50:28 +00004062static void rtl_hw_start_8168e(void __iomem *ioaddr, struct pci_dev *pdev)
4063{
4064 static const struct ephy_info e_info_8168e[] = {
4065 { 0x00, 0x0200, 0x0100 },
4066 { 0x00, 0x0000, 0x0004 },
4067 { 0x06, 0x0002, 0x0001 },
4068 { 0x06, 0x0000, 0x0030 },
4069 { 0x07, 0x0000, 0x2000 },
4070 { 0x00, 0x0000, 0x0020 },
4071 { 0x03, 0x5800, 0x2000 },
4072 { 0x03, 0x0000, 0x0001 },
4073 { 0x01, 0x0800, 0x1000 },
4074 { 0x07, 0x0000, 0x4000 },
4075 { 0x1e, 0x0000, 0x2000 },
4076 { 0x19, 0xffff, 0xfe6c },
4077 { 0x0a, 0x0000, 0x0040 }
4078 };
4079
4080 rtl_csi_access_enable_2(ioaddr);
4081
4082 rtl_ephy_init(ioaddr, e_info_8168e, ARRAY_SIZE(e_info_8168e));
4083
4084 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4085
4086 RTL_W8(MaxTxPacketSize, TxPacketMax);
4087
4088 rtl_disable_clock_request(pdev);
4089
4090 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02004091 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4092 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004093
Francois Romieucecb5fd2011-04-01 10:21:07 +02004094 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004095}
4096
Francois Romieu07ce4062007-02-23 23:36:39 +01004097static void rtl_hw_start_8168(struct net_device *dev)
4098{
Francois Romieu2dd99532007-06-11 23:22:52 +02004099 struct rtl8169_private *tp = netdev_priv(dev);
4100 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01004101 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu2dd99532007-06-11 23:22:52 +02004102
4103 RTL_W8(Cfg9346, Cfg9346_Unlock);
4104
françois romieuf0298f82011-01-03 15:07:42 +00004105 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004106
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004107 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004108
Francois Romieu0e485152007-02-20 00:00:26 +01004109 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004110
4111 RTL_W16(CPlusCmd, tp->cp_cmd);
4112
Francois Romieu0e485152007-02-20 00:00:26 +01004113 RTL_W16(IntrMitigate, 0x5151);
4114
4115 /* Work around for RxFIFO overflow. */
Ivan Vecerab5ba6d12011-01-27 12:24:11 +01004116 if (tp->mac_version == RTL_GIGA_MAC_VER_11 ||
4117 tp->mac_version == RTL_GIGA_MAC_VER_22) {
Francois Romieu0e485152007-02-20 00:00:26 +01004118 tp->intr_event |= RxFIFOOver | PCSTimeout;
4119 tp->intr_event &= ~RxOverflow;
4120 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004121
4122 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4123
Francois Romieub8363902008-06-01 12:31:57 +02004124 rtl_set_rx_mode(dev);
4125
4126 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4127 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004128
4129 RTL_R8(IntrMask);
4130
Francois Romieu219a1e92008-06-28 11:58:39 +02004131 switch (tp->mac_version) {
4132 case RTL_GIGA_MAC_VER_11:
4133 rtl_hw_start_8168bb(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004134 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004135
4136 case RTL_GIGA_MAC_VER_12:
4137 case RTL_GIGA_MAC_VER_17:
4138 rtl_hw_start_8168bef(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004139 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004140
4141 case RTL_GIGA_MAC_VER_18:
Francois Romieuef3386f2008-06-29 12:24:30 +02004142 rtl_hw_start_8168cp_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004143 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004144
4145 case RTL_GIGA_MAC_VER_19:
4146 rtl_hw_start_8168c_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004147 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004148
4149 case RTL_GIGA_MAC_VER_20:
4150 rtl_hw_start_8168c_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004151 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004152
Francois Romieu197ff762008-06-28 13:16:02 +02004153 case RTL_GIGA_MAC_VER_21:
4154 rtl_hw_start_8168c_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004155 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004156
Francois Romieu6fb07052008-06-29 11:54:28 +02004157 case RTL_GIGA_MAC_VER_22:
4158 rtl_hw_start_8168c_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004159 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004160
Francois Romieuef3386f2008-06-29 12:24:30 +02004161 case RTL_GIGA_MAC_VER_23:
4162 rtl_hw_start_8168cp_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004163 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004164
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004165 case RTL_GIGA_MAC_VER_24:
4166 rtl_hw_start_8168cp_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004167 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004168
Francois Romieu5b538df2008-07-20 16:22:45 +02004169 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00004170 case RTL_GIGA_MAC_VER_26:
4171 case RTL_GIGA_MAC_VER_27:
Francois Romieu5b538df2008-07-20 16:22:45 +02004172 rtl_hw_start_8168d(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004173 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004174
françois romieue6de30d2011-01-03 15:08:37 +00004175 case RTL_GIGA_MAC_VER_28:
4176 rtl_hw_start_8168d_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004177 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004178
hayeswang4804b3b2011-03-21 01:50:29 +00004179 case RTL_GIGA_MAC_VER_31:
4180 rtl_hw_start_8168dp(ioaddr, pdev);
4181 break;
4182
hayeswang01dc7fe2011-03-21 01:50:28 +00004183 case RTL_GIGA_MAC_VER_32:
4184 case RTL_GIGA_MAC_VER_33:
4185 rtl_hw_start_8168e(ioaddr, pdev);
4186 break;
françois romieue6de30d2011-01-03 15:08:37 +00004187
Francois Romieu219a1e92008-06-28 11:58:39 +02004188 default:
4189 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
4190 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00004191 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004192 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004193
Francois Romieu0e485152007-02-20 00:00:26 +01004194 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4195
Francois Romieub8363902008-06-01 12:31:57 +02004196 RTL_W8(Cfg9346, Cfg9346_Lock);
4197
Francois Romieu2dd99532007-06-11 23:22:52 +02004198 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004199
Francois Romieu0e485152007-02-20 00:00:26 +01004200 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01004201}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202
Francois Romieu2857ffb2008-08-02 21:08:49 +02004203#define R810X_CPCMD_QUIRK_MASK (\
4204 EnableBist | \
4205 Mac_dbgo_oe | \
4206 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00004207 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02004208 Force_txflow_en | \
4209 Cxpl_dbg_sel | \
4210 ASF | \
4211 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004212 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004213
4214static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4215{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004216 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004217 { 0x01, 0, 0x6e65 },
4218 { 0x02, 0, 0x091f },
4219 { 0x03, 0, 0xc2f9 },
4220 { 0x06, 0, 0xafb5 },
4221 { 0x07, 0, 0x0e00 },
4222 { 0x19, 0, 0xec80 },
4223 { 0x01, 0, 0x2e65 },
4224 { 0x01, 0, 0x6e65 }
4225 };
4226 u8 cfg1;
4227
françois romieu650e8d52011-01-03 15:08:29 +00004228 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004229
4230 RTL_W8(DBG_REG, FIX_NAK_1);
4231
4232 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4233
4234 RTL_W8(Config1,
4235 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
4236 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4237
4238 cfg1 = RTL_R8(Config1);
4239 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
4240 RTL_W8(Config1, cfg1 & ~LEDS0);
4241
Francois Romieu2857ffb2008-08-02 21:08:49 +02004242 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
4243}
4244
4245static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4246{
françois romieu650e8d52011-01-03 15:08:29 +00004247 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004248
4249 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4250
4251 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
4252 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004253}
4254
4255static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
4256{
4257 rtl_hw_start_8102e_2(ioaddr, pdev);
4258
4259 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
4260}
4261
Hayes Wang5a5e4442011-02-22 17:26:21 +08004262static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4263{
4264 static const struct ephy_info e_info_8105e_1[] = {
4265 { 0x07, 0, 0x4000 },
4266 { 0x19, 0, 0x0200 },
4267 { 0x19, 0, 0x0020 },
4268 { 0x1e, 0, 0x2000 },
4269 { 0x03, 0, 0x0001 },
4270 { 0x19, 0, 0x0100 },
4271 { 0x19, 0, 0x0004 },
4272 { 0x0a, 0, 0x0020 }
4273 };
4274
Francois Romieucecb5fd2011-04-01 10:21:07 +02004275 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004276 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
4277
Francois Romieucecb5fd2011-04-01 10:21:07 +02004278 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004279 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
4280
4281 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
4282 RTL_W8(DLLPR, RTL_R8(DLLPR) | PM_SWITCH);
4283
4284 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
4285}
4286
4287static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4288{
4289 rtl_hw_start_8105e_1(ioaddr, pdev);
4290 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
4291}
4292
Francois Romieu07ce4062007-02-23 23:36:39 +01004293static void rtl_hw_start_8101(struct net_device *dev)
4294{
Francois Romieucdf1a602007-06-11 23:29:50 +02004295 struct rtl8169_private *tp = netdev_priv(dev);
4296 void __iomem *ioaddr = tp->mmio_addr;
4297 struct pci_dev *pdev = tp->pci_dev;
4298
Francois Romieucecb5fd2011-04-01 10:21:07 +02004299 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
4300 tp->mac_version == RTL_GIGA_MAC_VER_16) {
Francois Romieu9c14cea2008-07-05 00:21:15 +02004301 int cap = tp->pcie_cap;
4302
4303 if (cap) {
4304 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
4305 PCI_EXP_DEVCTL_NOSNOOP_EN);
4306 }
Francois Romieucdf1a602007-06-11 23:29:50 +02004307 }
4308
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004309 RTL_W8(Cfg9346, Cfg9346_Unlock);
4310
Francois Romieu2857ffb2008-08-02 21:08:49 +02004311 switch (tp->mac_version) {
4312 case RTL_GIGA_MAC_VER_07:
4313 rtl_hw_start_8102e_1(ioaddr, pdev);
4314 break;
4315
4316 case RTL_GIGA_MAC_VER_08:
4317 rtl_hw_start_8102e_3(ioaddr, pdev);
4318 break;
4319
4320 case RTL_GIGA_MAC_VER_09:
4321 rtl_hw_start_8102e_2(ioaddr, pdev);
4322 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004323
4324 case RTL_GIGA_MAC_VER_29:
4325 rtl_hw_start_8105e_1(ioaddr, pdev);
4326 break;
4327 case RTL_GIGA_MAC_VER_30:
4328 rtl_hw_start_8105e_2(ioaddr, pdev);
4329 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02004330 }
4331
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004332 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02004333
françois romieuf0298f82011-01-03 15:07:42 +00004334 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02004335
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004336 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02004337
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004338 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02004339 RTL_W16(CPlusCmd, tp->cp_cmd);
4340
4341 RTL_W16(IntrMitigate, 0x0000);
4342
4343 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4344
4345 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4346 rtl_set_rx_tx_config_registers(tp);
4347
Francois Romieucdf1a602007-06-11 23:29:50 +02004348 RTL_R8(IntrMask);
4349
Francois Romieucdf1a602007-06-11 23:29:50 +02004350 rtl_set_rx_mode(dev);
4351
4352 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004353
Francois Romieu0e485152007-02-20 00:00:26 +01004354 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355}
4356
4357static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
4358{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 if (new_mtu < ETH_ZLEN || new_mtu > SafeMtu)
4360 return -EINVAL;
4361
4362 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00004363 netdev_update_features(dev);
4364
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00004365 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366}
4367
4368static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
4369{
Al Viro95e09182007-12-22 18:55:39 +00004370 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
4372}
4373
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004374static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
4375 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004377 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004378 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004379
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004380 kfree(*data_buff);
4381 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 rtl8169_make_unusable_by_asic(desc);
4383}
4384
4385static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
4386{
4387 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
4388
4389 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
4390}
4391
4392static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
4393 u32 rx_buf_sz)
4394{
4395 desc->addr = cpu_to_le64(mapping);
4396 wmb();
4397 rtl8169_mark_to_asic(desc, rx_buf_sz);
4398}
4399
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004400static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004402 return (void *)ALIGN((long)data, 16);
4403}
4404
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004405static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
4406 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004407{
4408 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004410 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004411 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004412 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004414 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
4415 if (!data)
4416 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01004417
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004418 if (rtl8169_align(data) != data) {
4419 kfree(data);
4420 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
4421 if (!data)
4422 return NULL;
4423 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004424
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004425 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004426 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004427 if (unlikely(dma_mapping_error(d, mapping))) {
4428 if (net_ratelimit())
4429 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004430 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432
4433 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004434 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004435
4436err_out:
4437 kfree(data);
4438 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439}
4440
4441static void rtl8169_rx_clear(struct rtl8169_private *tp)
4442{
Francois Romieu07d3f512007-02-21 22:40:46 +01004443 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444
4445 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004446 if (tp->Rx_databuff[i]) {
4447 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 tp->RxDescArray + i);
4449 }
4450 }
4451}
4452
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004453static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004455 desc->opts1 |= cpu_to_le32(RingEnd);
4456}
Francois Romieu5b0384f2006-08-16 16:00:01 +02004457
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004458static int rtl8169_rx_fill(struct rtl8169_private *tp)
4459{
4460 unsigned int i;
4461
4462 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004463 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02004464
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004465 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004467
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004468 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004469 if (!data) {
4470 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004471 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004472 }
4473 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004476 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
4477 return 0;
4478
4479err_out:
4480 rtl8169_rx_clear(tp);
4481 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482}
4483
4484static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4485{
4486 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
4487}
4488
4489static int rtl8169_init_ring(struct net_device *dev)
4490{
4491 struct rtl8169_private *tp = netdev_priv(dev);
4492
4493 rtl8169_init_ring_indexes(tp);
4494
4495 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004496 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004498 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499}
4500
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004501static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 struct TxDesc *desc)
4503{
4504 unsigned int len = tx_skb->len;
4505
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004506 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
4507
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 desc->opts1 = 0x00;
4509 desc->opts2 = 0x00;
4510 desc->addr = 0x00;
4511 tx_skb->len = 0;
4512}
4513
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004514static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
4515 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516{
4517 unsigned int i;
4518
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004519 for (i = 0; i < n; i++) {
4520 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 struct ring_info *tx_skb = tp->tx_skb + entry;
4522 unsigned int len = tx_skb->len;
4523
4524 if (len) {
4525 struct sk_buff *skb = tx_skb->skb;
4526
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004527 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 tp->TxDescArray + entry);
4529 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00004530 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 dev_kfree_skb(skb);
4532 tx_skb->skb = NULL;
4533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 }
4535 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004536}
4537
4538static void rtl8169_tx_clear(struct rtl8169_private *tp)
4539{
4540 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 tp->cur_tx = tp->dirty_tx = 0;
4542}
4543
David Howellsc4028952006-11-22 14:57:56 +00004544static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545{
4546 struct rtl8169_private *tp = netdev_priv(dev);
4547
David Howellsc4028952006-11-22 14:57:56 +00004548 PREPARE_DELAYED_WORK(&tp->task, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 schedule_delayed_work(&tp->task, 4);
4550}
4551
4552static void rtl8169_wait_for_quiescence(struct net_device *dev)
4553{
4554 struct rtl8169_private *tp = netdev_priv(dev);
4555 void __iomem *ioaddr = tp->mmio_addr;
4556
4557 synchronize_irq(dev->irq);
4558
4559 /* Wait for any pending NAPI task to complete */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004560 napi_disable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561
4562 rtl8169_irq_mask_and_ack(ioaddr);
4563
David S. Millerd1d08d12008-01-07 20:53:33 -08004564 tp->intr_mask = 0xffff;
4565 RTL_W16(IntrMask, tp->intr_event);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004566 napi_enable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567}
4568
David Howellsc4028952006-11-22 14:57:56 +00004569static void rtl8169_reinit_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570{
David Howellsc4028952006-11-22 14:57:56 +00004571 struct rtl8169_private *tp =
4572 container_of(work, struct rtl8169_private, task.work);
4573 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 int ret;
4575
Francois Romieueb2a0212007-02-15 23:37:21 +01004576 rtnl_lock();
4577
4578 if (!netif_running(dev))
4579 goto out_unlock;
4580
4581 rtl8169_wait_for_quiescence(dev);
4582 rtl8169_close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583
4584 ret = rtl8169_open(dev);
4585 if (unlikely(ret < 0)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004586 if (net_ratelimit())
4587 netif_err(tp, drv, dev,
4588 "reinit failure (status = %d). Rescheduling\n",
4589 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 rtl8169_schedule_work(dev, rtl8169_reinit_task);
4591 }
Francois Romieueb2a0212007-02-15 23:37:21 +01004592
4593out_unlock:
4594 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595}
4596
David Howellsc4028952006-11-22 14:57:56 +00004597static void rtl8169_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598{
David Howellsc4028952006-11-22 14:57:56 +00004599 struct rtl8169_private *tp =
4600 container_of(work, struct rtl8169_private, task.work);
4601 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01004602 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603
Francois Romieueb2a0212007-02-15 23:37:21 +01004604 rtnl_lock();
4605
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 if (!netif_running(dev))
Francois Romieueb2a0212007-02-15 23:37:21 +01004607 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608
4609 rtl8169_wait_for_quiescence(dev);
4610
Francois Romieu56de4142011-03-15 17:29:31 +01004611 for (i = 0; i < NUM_RX_DESC; i++)
4612 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
4613
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 rtl8169_tx_clear(tp);
4615
Francois Romieu56de4142011-03-15 17:29:31 +01004616 rtl8169_init_ring_indexes(tp);
4617 rtl_hw_start(dev);
4618 netif_wake_queue(dev);
4619 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Francois Romieueb2a0212007-02-15 23:37:21 +01004620
4621out_unlock:
4622 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623}
4624
4625static void rtl8169_tx_timeout(struct net_device *dev)
4626{
4627 struct rtl8169_private *tp = netdev_priv(dev);
4628
françois romieue6de30d2011-01-03 15:08:37 +00004629 rtl8169_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630
4631 /* Let's wait a bit while any (async) irq lands on */
4632 rtl8169_schedule_work(dev, rtl8169_reset_task);
4633}
4634
4635static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07004636 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637{
4638 struct skb_shared_info *info = skb_shinfo(skb);
4639 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04004640 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004641 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642
4643 entry = tp->cur_tx;
4644 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
4645 skb_frag_t *frag = info->frags + cur_frag;
4646 dma_addr_t mapping;
4647 u32 status, len;
4648 void *addr;
4649
4650 entry = (entry + 1) % NUM_TX_DESC;
4651
4652 txd = tp->TxDescArray + entry;
4653 len = frag->size;
4654 addr = ((void *) page_address(frag->page)) + frag->page_offset;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004655 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004656 if (unlikely(dma_mapping_error(d, mapping))) {
4657 if (net_ratelimit())
4658 netif_err(tp, drv, tp->dev,
4659 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004660 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662
Francois Romieucecb5fd2011-04-01 10:21:07 +02004663 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07004664 status = opts[0] | len |
4665 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666
4667 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07004668 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 txd->addr = cpu_to_le64(mapping);
4670
4671 tp->tx_skb[entry].len = len;
4672 }
4673
4674 if (cur_frag) {
4675 tp->tx_skb[entry].skb = skb;
4676 txd->opts1 |= cpu_to_le32(LastFrag);
4677 }
4678
4679 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004680
4681err_out:
4682 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
4683 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684}
4685
Francois Romieu2b7b4312011-04-18 22:53:24 -07004686static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
4687 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688{
Francois Romieu2b7b4312011-04-18 22:53:24 -07004689 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00004690 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07004691 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692
Francois Romieu2b7b4312011-04-18 22:53:24 -07004693 if (mss) {
4694 opts[0] |= TD_LSO;
4695 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
4696 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07004697 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698
4699 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07004700 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07004702 opts[offset] |= info->checksum.udp;
4703 else
4704 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706}
4707
Stephen Hemminger613573252009-08-31 19:50:58 +00004708static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
4709 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710{
4711 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004712 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 struct TxDesc *txd = tp->TxDescArray + entry;
4714 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004715 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 dma_addr_t mapping;
4717 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07004718 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004719 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02004720
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004722 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004723 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 }
4725
4726 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004727 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004729 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004730 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004731 if (unlikely(dma_mapping_error(d, mapping))) {
4732 if (net_ratelimit())
4733 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004734 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736
4737 tp->tx_skb[entry].len = len;
4738 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739
Francois Romieu2b7b4312011-04-18 22:53:24 -07004740 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
4741 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004742
Francois Romieu2b7b4312011-04-18 22:53:24 -07004743 rtl8169_tso_csum(tp, skb, opts);
4744
4745 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004746 if (frags < 0)
4747 goto err_dma_1;
4748 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07004749 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004750 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07004751 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004752 tp->tx_skb[entry].skb = skb;
4753 }
4754
Francois Romieu2b7b4312011-04-18 22:53:24 -07004755 txd->opts2 = cpu_to_le32(opts[1]);
4756
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 wmb();
4758
Francois Romieucecb5fd2011-04-01 10:21:07 +02004759 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07004760 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 txd->opts1 = cpu_to_le32(status);
4762
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 tp->cur_tx += frags + 1;
4764
David Dillow4c020a92010-03-03 16:33:10 +00004765 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766
Francois Romieucecb5fd2011-04-01 10:21:07 +02004767 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768
4769 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
4770 netif_stop_queue(dev);
4771 smp_rmb();
4772 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
4773 netif_wake_queue(dev);
4774 }
4775
Stephen Hemminger613573252009-08-31 19:50:58 +00004776 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004778err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004779 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004780err_dma_0:
4781 dev_kfree_skb(skb);
4782 dev->stats.tx_dropped++;
4783 return NETDEV_TX_OK;
4784
4785err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004787 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00004788 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789}
4790
4791static void rtl8169_pcierr_interrupt(struct net_device *dev)
4792{
4793 struct rtl8169_private *tp = netdev_priv(dev);
4794 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 u16 pci_status, pci_cmd;
4796
4797 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
4798 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
4799
Joe Perchesbf82c182010-02-09 11:49:50 +00004800 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
4801 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802
4803 /*
4804 * The recovery sequence below admits a very elaborated explanation:
4805 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01004806 * - I did not see what else could be done;
4807 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 *
4809 * Feel free to adjust to your needs.
4810 */
Francois Romieua27993f2006-12-18 00:04:19 +01004811 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01004812 pci_cmd &= ~PCI_COMMAND_PARITY;
4813 else
4814 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
4815
4816 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817
4818 pci_write_config_word(pdev, PCI_STATUS,
4819 pci_status & (PCI_STATUS_DETECTED_PARITY |
4820 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
4821 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
4822
4823 /* The infamous DAC f*ckup only happens at boot time */
4824 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00004825 void __iomem *ioaddr = tp->mmio_addr;
4826
Joe Perchesbf82c182010-02-09 11:49:50 +00004827 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 tp->cp_cmd &= ~PCIDAC;
4829 RTL_W16(CPlusCmd, tp->cp_cmd);
4830 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 }
4832
françois romieue6de30d2011-01-03 15:08:37 +00004833 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01004834
4835 rtl8169_schedule_work(dev, rtl8169_reinit_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836}
4837
Francois Romieu07d3f512007-02-21 22:40:46 +01004838static void rtl8169_tx_interrupt(struct net_device *dev,
4839 struct rtl8169_private *tp,
4840 void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841{
4842 unsigned int dirty_tx, tx_left;
4843
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 dirty_tx = tp->dirty_tx;
4845 smp_rmb();
4846 tx_left = tp->cur_tx - dirty_tx;
4847
4848 while (tx_left > 0) {
4849 unsigned int entry = dirty_tx % NUM_TX_DESC;
4850 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 u32 status;
4852
4853 rmb();
4854 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
4855 if (status & DescOwn)
4856 break;
4857
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004858 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
4859 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 if (status & LastFrag) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00004861 dev->stats.tx_packets++;
4862 dev->stats.tx_bytes += tx_skb->skb->len;
Eric Dumazet87433bf2009-06-09 22:55:53 +00004863 dev_kfree_skb(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 tx_skb->skb = NULL;
4865 }
4866 dirty_tx++;
4867 tx_left--;
4868 }
4869
4870 if (tp->dirty_tx != dirty_tx) {
4871 tp->dirty_tx = dirty_tx;
4872 smp_wmb();
4873 if (netif_queue_stopped(dev) &&
4874 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
4875 netif_wake_queue(dev);
4876 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02004877 /*
4878 * 8168 hack: TxPoll requests are lost when the Tx packets are
4879 * too close. Let's kick an extra TxPoll request when a burst
4880 * of start_xmit activity is detected (if it is not detected,
4881 * it is slow enough). -- FR
4882 */
4883 smp_rmb();
4884 if (tp->cur_tx != dirty_tx)
4885 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 }
4887}
4888
Francois Romieu126fa4b2005-05-12 20:09:17 -04004889static inline int rtl8169_fragmented_frame(u32 status)
4890{
4891 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
4892}
4893
Eric Dumazetadea1ac72010-09-05 20:04:05 -07004894static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 u32 status = opts1 & RxProtoMask;
4897
4898 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00004899 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900 skb->ip_summed = CHECKSUM_UNNECESSARY;
4901 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07004902 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903}
4904
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004905static struct sk_buff *rtl8169_try_rx_copy(void *data,
4906 struct rtl8169_private *tp,
4907 int pkt_size,
4908 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02004910 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004911 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004913 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004914 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004915 prefetch(data);
4916 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
4917 if (skb)
4918 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004919 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
4920
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004921 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922}
4923
Francois Romieu07d3f512007-02-21 22:40:46 +01004924static int rtl8169_rx_interrupt(struct net_device *dev,
4925 struct rtl8169_private *tp,
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004926 void __iomem *ioaddr, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927{
4928 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004929 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 cur_rx = tp->cur_rx;
4932 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02004933 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934
Richard Dawe4dcb7d32005-05-27 21:12:00 +02004935 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04004937 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 u32 status;
4939
4940 rmb();
Francois Romieu126fa4b2005-05-12 20:09:17 -04004941 status = le32_to_cpu(desc->opts1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942
4943 if (status & DescOwn)
4944 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02004945 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004946 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
4947 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004948 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02004950 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02004952 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02004953 if (status & RxFOVF) {
4954 rtl8169_schedule_work(dev, rtl8169_reset_task);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004955 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02004956 }
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004957 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004959 struct sk_buff *skb;
Stephen Hemmingerb4496552007-06-17 01:06:49 +02004960 dma_addr_t addr = le64_to_cpu(desc->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 int pkt_size = (status & 0x00001FFF) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962
Francois Romieu126fa4b2005-05-12 20:09:17 -04004963 /*
4964 * The driver does not support incoming fragmented
4965 * frames. They are seen as a symptom of over-mtu
4966 * sized frames.
4967 */
4968 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02004969 dev->stats.rx_dropped++;
4970 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004971 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02004972 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04004973 }
4974
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004975 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
4976 tp, pkt_size, addr);
4977 rtl8169_mark_to_asic(desc, rx_buf_sz);
4978 if (!skb) {
4979 dev->stats.rx_dropped++;
4980 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 }
4982
Eric Dumazetadea1ac72010-09-05 20:04:05 -07004983 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 skb_put(skb, pkt_size);
4985 skb->protocol = eth_type_trans(skb, dev);
4986
Francois Romieu7a8fc772011-03-01 17:18:33 +01004987 rtl8169_rx_vlan_tag(desc, skb);
4988
Francois Romieu56de4142011-03-15 17:29:31 +01004989 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990
Francois Romieucebf8cc2007-10-18 12:06:54 +02004991 dev->stats.rx_bytes += pkt_size;
4992 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 }
Francois Romieu6dccd162007-02-13 23:38:05 +01004994
4995 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00004996 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01004997 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
4998 desc->opts2 = 0;
4999 cur_rx++;
5000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 }
5002
5003 count = cur_rx - tp->cur_rx;
5004 tp->cur_rx = cur_rx;
5005
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005006 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007
5008 return count;
5009}
5010
Francois Romieu07d3f512007-02-21 22:40:46 +01005011static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012{
Francois Romieu07d3f512007-02-21 22:40:46 +01005013 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 int handled = 0;
Francois Romieu865c6522008-05-11 14:51:00 +02005017 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018
David Dillowf11a3772009-05-22 15:29:34 +00005019 /* loop handling interrupts until we have no new ones or
5020 * we hit a invalid/hotplug case.
5021 */
Francois Romieu865c6522008-05-11 14:51:00 +02005022 status = RTL_R16(IntrStatus);
David Dillowf11a3772009-05-22 15:29:34 +00005023 while (status && status != 0xffff) {
5024 handled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025
David Dillowf11a3772009-05-22 15:29:34 +00005026 /* Handle all of the error cases first. These will reset
5027 * the chip, so just exit the loop.
5028 */
5029 if (unlikely(!netif_running(dev))) {
5030 rtl8169_asic_down(ioaddr);
5031 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 }
David Dillowf11a3772009-05-22 15:29:34 +00005033
Francois Romieu1519e572011-02-03 12:02:36 +01005034 if (unlikely(status & RxFIFOOver)) {
5035 switch (tp->mac_version) {
5036 /* Work around for rx fifo overflow */
5037 case RTL_GIGA_MAC_VER_11:
5038 case RTL_GIGA_MAC_VER_22:
5039 case RTL_GIGA_MAC_VER_26:
5040 netif_stop_queue(dev);
5041 rtl8169_tx_timeout(dev);
5042 goto done;
Francois Romieuf60ac8e2011-02-03 17:27:52 +01005043 /* Testers needed. */
5044 case RTL_GIGA_MAC_VER_17:
5045 case RTL_GIGA_MAC_VER_19:
5046 case RTL_GIGA_MAC_VER_20:
5047 case RTL_GIGA_MAC_VER_21:
5048 case RTL_GIGA_MAC_VER_23:
5049 case RTL_GIGA_MAC_VER_24:
5050 case RTL_GIGA_MAC_VER_27:
5051 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00005052 case RTL_GIGA_MAC_VER_31:
Francois Romieu1519e572011-02-03 12:02:36 +01005053 /* Experimental science. Pktgen proof. */
5054 case RTL_GIGA_MAC_VER_12:
5055 case RTL_GIGA_MAC_VER_25:
5056 if (status == RxFIFOOver)
5057 goto done;
5058 break;
5059 default:
5060 break;
5061 }
David Dillowf11a3772009-05-22 15:29:34 +00005062 }
5063
5064 if (unlikely(status & SYSErr)) {
5065 rtl8169_pcierr_interrupt(dev);
5066 break;
5067 }
5068
5069 if (status & LinkChg)
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005070 __rtl8169_check_link_status(dev, tp, ioaddr, true);
David Dillowf11a3772009-05-22 15:29:34 +00005071
5072 /* We need to see the lastest version of tp->intr_mask to
5073 * avoid ignoring an MSI interrupt and having to wait for
5074 * another event which may never come.
5075 */
5076 smp_rmb();
5077 if (status & tp->intr_mask & tp->napi_event) {
5078 RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
5079 tp->intr_mask = ~tp->napi_event;
5080
5081 if (likely(napi_schedule_prep(&tp->napi)))
5082 __napi_schedule(&tp->napi);
Joe Perchesbf82c182010-02-09 11:49:50 +00005083 else
5084 netif_info(tp, intr, dev,
5085 "interrupt %04x in poll\n", status);
David Dillowf11a3772009-05-22 15:29:34 +00005086 }
5087
5088 /* We only get a new MSI interrupt when all active irq
5089 * sources on the chip have been acknowledged. So, ack
5090 * everything we've seen and check if new sources have become
5091 * active to avoid blocking all interrupts from the chip.
5092 */
5093 RTL_W16(IntrStatus,
5094 (status & RxFIFOOver) ? (status | RxOverflow) : status);
5095 status = RTL_R16(IntrStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 }
Francois Romieu1519e572011-02-03 12:02:36 +01005097done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 return IRQ_RETVAL(handled);
5099}
5100
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005101static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005103 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5104 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 void __iomem *ioaddr = tp->mmio_addr;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005106 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005108 work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 rtl8169_tx_interrupt(dev, tp, ioaddr);
5110
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005111 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005112 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00005113
5114 /* We need for force the visibility of tp->intr_mask
5115 * for other CPUs, as we can loose an MSI interrupt
5116 * and potentially wait for a retransmit timeout if we don't.
5117 * The posted write to IntrMask is safe, as it will
5118 * eventually make it to the chip and we won't loose anything
5119 * until it does.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 */
David Dillowf11a3772009-05-22 15:29:34 +00005121 tp->intr_mask = 0xffff;
David Dillow4c020a92010-03-03 16:33:10 +00005122 wmb();
Francois Romieu0e485152007-02-20 00:00:26 +01005123 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124 }
5125
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005126 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128
Francois Romieu523a6092008-09-10 22:28:56 +02005129static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5130{
5131 struct rtl8169_private *tp = netdev_priv(dev);
5132
5133 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5134 return;
5135
5136 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5137 RTL_W32(RxMissed, 0);
5138}
5139
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140static void rtl8169_down(struct net_device *dev)
5141{
5142 struct rtl8169_private *tp = netdev_priv(dev);
5143 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144
Francois Romieu4876cc12011-03-11 21:07:11 +01005145 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146
5147 netif_stop_queue(dev);
5148
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005149 napi_disable(&tp->napi);
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005150
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151 spin_lock_irq(&tp->lock);
5152
5153 rtl8169_asic_down(ioaddr);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005154 /*
5155 * At this point device interrupts can not be enabled in any function,
5156 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task,
5157 * rtl8169_reinit_task) and napi is disabled (rtl8169_poll).
5158 */
Francois Romieu523a6092008-09-10 22:28:56 +02005159 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160
5161 spin_unlock_irq(&tp->lock);
5162
5163 synchronize_irq(dev->irq);
5164
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 /* Give a racing hard_start_xmit a few cycles to complete. */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07005166 synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 rtl8169_tx_clear(tp);
5169
5170 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00005171
5172 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173}
5174
5175static int rtl8169_close(struct net_device *dev)
5176{
5177 struct rtl8169_private *tp = netdev_priv(dev);
5178 struct pci_dev *pdev = tp->pci_dev;
5179
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005180 pm_runtime_get_sync(&pdev->dev);
5181
Francois Romieucecb5fd2011-04-01 10:21:07 +02005182 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08005183 rtl8169_update_counters(dev);
5184
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 rtl8169_down(dev);
5186
5187 free_irq(dev->irq, dev);
5188
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00005189 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5190 tp->RxPhyAddr);
5191 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5192 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 tp->TxDescArray = NULL;
5194 tp->RxDescArray = NULL;
5195
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005196 pm_runtime_put_sync(&pdev->dev);
5197
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 return 0;
5199}
5200
Francois Romieu07ce4062007-02-23 23:36:39 +01005201static void rtl_set_rx_mode(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202{
5203 struct rtl8169_private *tp = netdev_priv(dev);
5204 void __iomem *ioaddr = tp->mmio_addr;
5205 unsigned long flags;
5206 u32 mc_filter[2]; /* Multicast hash filter */
Francois Romieu07d3f512007-02-21 22:40:46 +01005207 int rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 u32 tmp = 0;
5209
5210 if (dev->flags & IFF_PROMISC) {
5211 /* Unconditionally log net taps. */
Joe Perchesbf82c182010-02-09 11:49:50 +00005212 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 rx_mode =
5214 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5215 AcceptAllPhys;
5216 mc_filter[1] = mc_filter[0] = 0xffffffff;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00005217 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
Joe Perches8e95a202009-12-03 07:58:21 +00005218 (dev->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 /* Too many to filter perfectly -- accept all multicasts. */
5220 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5221 mc_filter[1] = mc_filter[0] = 0xffffffff;
5222 } else {
Jiri Pirko22bedad2010-04-01 21:22:57 +00005223 struct netdev_hw_addr *ha;
Francois Romieu07d3f512007-02-21 22:40:46 +01005224
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225 rx_mode = AcceptBroadcast | AcceptMyPhys;
5226 mc_filter[1] = mc_filter[0] = 0;
Jiri Pirko22bedad2010-04-01 21:22:57 +00005227 netdev_for_each_mc_addr(ha, dev) {
5228 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5230 rx_mode |= AcceptMulticast;
5231 }
5232 }
5233
5234 spin_lock_irqsave(&tp->lock, flags);
5235
5236 tmp = rtl8169_rx_config | rx_mode |
Francois Romieu2b7b4312011-04-18 22:53:24 -07005237 (RTL_R32(RxConfig) & RTL_RX_CONFIG_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238
Francois Romieuf887cce2008-07-17 22:24:18 +02005239 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
Francois Romieu1087f4f2007-12-26 22:46:05 +01005240 u32 data = mc_filter[0];
5241
5242 mc_filter[0] = swab32(mc_filter[1]);
5243 mc_filter[1] = swab32(data);
Francois Romieubcf0bf92006-07-26 23:14:13 +02005244 }
5245
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 RTL_W32(MAR0 + 4, mc_filter[1]);
Francois Romieu78f1cd02010-03-27 19:35:46 -07005247 RTL_W32(MAR0 + 0, mc_filter[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248
Francois Romieu57a9f232007-06-04 22:10:15 +02005249 RTL_W32(RxConfig, tmp);
5250
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 spin_unlock_irqrestore(&tp->lock, flags);
5252}
5253
5254/**
5255 * rtl8169_get_stats - Get rtl8169 read/write statistics
5256 * @dev: The Ethernet Device to get statistics for
5257 *
5258 * Get TX/RX statistics for rtl8169
5259 */
5260static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
5261{
5262 struct rtl8169_private *tp = netdev_priv(dev);
5263 void __iomem *ioaddr = tp->mmio_addr;
5264 unsigned long flags;
5265
5266 if (netif_running(dev)) {
5267 spin_lock_irqsave(&tp->lock, flags);
Francois Romieu523a6092008-09-10 22:28:56 +02005268 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269 spin_unlock_irqrestore(&tp->lock, flags);
5270 }
Francois Romieu5b0384f2006-08-16 16:00:01 +02005271
Francois Romieucebf8cc2007-10-18 12:06:54 +02005272 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273}
5274
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005275static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01005276{
françois romieu065c27c2011-01-03 15:08:12 +00005277 struct rtl8169_private *tp = netdev_priv(dev);
5278
Francois Romieu5d06a992006-02-23 00:47:58 +01005279 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005280 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01005281
françois romieu065c27c2011-01-03 15:08:12 +00005282 rtl_pll_power_down(tp);
5283
Francois Romieu5d06a992006-02-23 00:47:58 +01005284 netif_device_detach(dev);
5285 netif_stop_queue(dev);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005286}
Francois Romieu5d06a992006-02-23 00:47:58 +01005287
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005288#ifdef CONFIG_PM
5289
5290static int rtl8169_suspend(struct device *device)
5291{
5292 struct pci_dev *pdev = to_pci_dev(device);
5293 struct net_device *dev = pci_get_drvdata(pdev);
5294
5295 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02005296
Francois Romieu5d06a992006-02-23 00:47:58 +01005297 return 0;
5298}
5299
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005300static void __rtl8169_resume(struct net_device *dev)
5301{
françois romieu065c27c2011-01-03 15:08:12 +00005302 struct rtl8169_private *tp = netdev_priv(dev);
5303
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005304 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00005305
5306 rtl_pll_power_up(tp);
5307
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005308 rtl8169_schedule_work(dev, rtl8169_reset_task);
5309}
5310
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005311static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01005312{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005313 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01005314 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005315 struct rtl8169_private *tp = netdev_priv(dev);
5316
5317 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01005318
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005319 if (netif_running(dev))
5320 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01005321
Francois Romieu5d06a992006-02-23 00:47:58 +01005322 return 0;
5323}
5324
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005325static int rtl8169_runtime_suspend(struct device *device)
5326{
5327 struct pci_dev *pdev = to_pci_dev(device);
5328 struct net_device *dev = pci_get_drvdata(pdev);
5329 struct rtl8169_private *tp = netdev_priv(dev);
5330
5331 if (!tp->TxDescArray)
5332 return 0;
5333
5334 spin_lock_irq(&tp->lock);
5335 tp->saved_wolopts = __rtl8169_get_wol(tp);
5336 __rtl8169_set_wol(tp, WAKE_ANY);
5337 spin_unlock_irq(&tp->lock);
5338
5339 rtl8169_net_suspend(dev);
5340
5341 return 0;
5342}
5343
5344static int rtl8169_runtime_resume(struct device *device)
5345{
5346 struct pci_dev *pdev = to_pci_dev(device);
5347 struct net_device *dev = pci_get_drvdata(pdev);
5348 struct rtl8169_private *tp = netdev_priv(dev);
5349
5350 if (!tp->TxDescArray)
5351 return 0;
5352
5353 spin_lock_irq(&tp->lock);
5354 __rtl8169_set_wol(tp, tp->saved_wolopts);
5355 tp->saved_wolopts = 0;
5356 spin_unlock_irq(&tp->lock);
5357
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005358 rtl8169_init_phy(dev, tp);
5359
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005360 __rtl8169_resume(dev);
5361
5362 return 0;
5363}
5364
5365static int rtl8169_runtime_idle(struct device *device)
5366{
5367 struct pci_dev *pdev = to_pci_dev(device);
5368 struct net_device *dev = pci_get_drvdata(pdev);
5369 struct rtl8169_private *tp = netdev_priv(dev);
5370
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005371 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005372}
5373
Alexey Dobriyan47145212009-12-14 18:00:08 -08005374static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02005375 .suspend = rtl8169_suspend,
5376 .resume = rtl8169_resume,
5377 .freeze = rtl8169_suspend,
5378 .thaw = rtl8169_resume,
5379 .poweroff = rtl8169_suspend,
5380 .restore = rtl8169_resume,
5381 .runtime_suspend = rtl8169_runtime_suspend,
5382 .runtime_resume = rtl8169_runtime_resume,
5383 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005384};
5385
5386#define RTL8169_PM_OPS (&rtl8169_pm_ops)
5387
5388#else /* !CONFIG_PM */
5389
5390#define RTL8169_PM_OPS NULL
5391
5392#endif /* !CONFIG_PM */
5393
Francois Romieu1765f952008-09-13 17:21:40 +02005394static void rtl_shutdown(struct pci_dev *pdev)
5395{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005396 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00005397 struct rtl8169_private *tp = netdev_priv(dev);
5398 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu1765f952008-09-13 17:21:40 +02005399
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005400 rtl8169_net_suspend(dev);
5401
Francois Romieucecb5fd2011-04-01 10:21:07 +02005402 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08005403 rtl_rar_set(tp, dev->perm_addr);
5404
françois romieu4bb3f522009-06-17 11:41:45 +00005405 spin_lock_irq(&tp->lock);
5406
5407 rtl8169_asic_down(ioaddr);
5408
5409 spin_unlock_irq(&tp->lock);
5410
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005411 if (system_state == SYSTEM_POWER_OFF) {
françois romieuca52efd2009-07-24 12:34:19 +00005412 /* WoL fails with some 8168 when the receiver is disabled. */
5413 if (tp->features & RTL_FEATURE_WOL) {
5414 pci_clear_master(pdev);
5415
5416 RTL_W8(ChipCmd, CmdRxEnb);
5417 /* PCI commit */
5418 RTL_R8(ChipCmd);
5419 }
5420
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005421 pci_wake_from_d3(pdev, true);
5422 pci_set_power_state(pdev, PCI_D3hot);
5423 }
5424}
Francois Romieu5d06a992006-02-23 00:47:58 +01005425
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426static struct pci_driver rtl8169_pci_driver = {
5427 .name = MODULENAME,
5428 .id_table = rtl8169_pci_tbl,
5429 .probe = rtl8169_init_one,
5430 .remove = __devexit_p(rtl8169_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02005431 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005432 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433};
5434
Francois Romieu07d3f512007-02-21 22:40:46 +01005435static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436{
Jeff Garzik29917622006-08-19 17:48:59 -04005437 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438}
5439
Francois Romieu07d3f512007-02-21 22:40:46 +01005440static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441{
5442 pci_unregister_driver(&rtl8169_pci_driver);
5443}
5444
5445module_init(rtl8169_init_module);
5446module_exit(rtl8169_cleanup_module);