blob: 6364e0b03fddd8b8a23504e94c3a4e24d83f1cc1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Francois Romieu07d3f512007-02-21 22:40:46 +01002 * r8169.c: RealTek 8169/8168/8101 ethernet driver.
3 *
4 * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
5 * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
6 * Copyright (c) a lot of people too. Please respect their work.
7 *
8 * See MAINTAINERS file for support contact information.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11#include <linux/module.h>
12#include <linux/moduleparam.h>
13#include <linux/pci.h>
14#include <linux/netdevice.h>
15#include <linux/etherdevice.h>
16#include <linux/delay.h>
17#include <linux/ethtool.h>
18#include <linux/mii.h>
19#include <linux/if_vlan.h>
20#include <linux/crc32.h>
21#include <linux/in.h>
22#include <linux/ip.h>
23#include <linux/tcp.h>
24#include <linux/init.h>
25#include <linux/dma-mapping.h>
Rafael J. Wysockie1759442010-03-14 14:33:51 +000026#include <linux/pm_runtime.h>
françois romieubca03d52011-01-03 15:07:31 +000027#include <linux/firmware.h>
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +000028#include <linux/pci-aspm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Francois Romieu99f252b2007-04-02 22:59:59 +020030#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/io.h>
32#include <asm/irq.h>
33
Francois Romieu865c6522008-05-11 14:51:00 +020034#define RTL8169_VERSION "2.3LK-NAPI"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#define MODULENAME "r8169"
36#define PFX MODULENAME ": "
37
françois romieubca03d52011-01-03 15:07:31 +000038#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
39#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
hayeswang01dc7fe2011-03-21 01:50:28 +000040#define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
41#define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080042#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
françois romieubca03d52011-01-03 15:07:31 +000043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#ifdef RTL8169_DEBUG
45#define assert(expr) \
Francois Romieu5b0384f2006-08-16 16:00:01 +020046 if (!(expr)) { \
47 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
Harvey Harrisonb39d66a2008-08-20 16:52:04 -070048 #expr,__FILE__,__func__,__LINE__); \
Francois Romieu5b0384f2006-08-16 16:00:01 +020049 }
Joe Perches06fa7352007-10-18 21:15:00 +020050#define dprintk(fmt, args...) \
51 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#else
53#define assert(expr) do {} while (0)
54#define dprintk(fmt, args...) do {} while (0)
55#endif /* RTL8169_DEBUG */
56
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020057#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d2005-09-30 16:54:02 -070058 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#define TX_BUFFS_AVAIL(tp) \
61 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
64 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050065static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67/* MAC address length */
68#define MAC_ADDR_LEN 6
69
Francois Romieu9c14cea2008-07-05 00:21:15 +020070#define MAX_READ_REQUEST_SHIFT 12
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define RX_FIFO_THRESH 7 /* 7 means NO threshold, Rx buffer level before first PCI xfer. */
72#define RX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
73#define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
75#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
76
77#define R8169_REGS_SIZE 256
78#define R8169_NAPI_WEIGHT 64
79#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
80#define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
81#define RX_BUF_SIZE 1536 /* Rx Buffer size */
82#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
83#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
84
85#define RTL8169_TX_TIMEOUT (6*HZ)
86#define RTL8169_PHY_TIMEOUT (10*HZ)
87
françois romieuea8dbdd2009-03-15 01:10:50 +000088#define RTL_EEPROM_SIG cpu_to_le32(0x8129)
89#define RTL_EEPROM_SIG_MASK cpu_to_le32(0xffff)
Francois Romieue1564ec2008-10-16 22:46:13 +020090#define RTL_EEPROM_SIG_ADDR 0x0000
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/* write/read MMIO register */
93#define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
94#define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
95#define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
96#define RTL_R8(reg) readb (ioaddr + (reg))
97#define RTL_R16(reg) readw (ioaddr + (reg))
Junchang Wang06f555f2010-05-30 02:26:07 +000098#define RTL_R32(reg) readl (ioaddr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100enum mac_version {
Jean Delvaref21b75e2009-05-26 20:54:48 -0700101 RTL_GIGA_MAC_NONE = 0x00,
Francois Romieuba6eb6e2007-06-11 23:35:18 +0200102 RTL_GIGA_MAC_VER_01 = 0x01, // 8169
103 RTL_GIGA_MAC_VER_02 = 0x02, // 8169S
104 RTL_GIGA_MAC_VER_03 = 0x03, // 8110S
105 RTL_GIGA_MAC_VER_04 = 0x04, // 8169SB
106 RTL_GIGA_MAC_VER_05 = 0x05, // 8110SCd
Francois Romieu6dccd162007-02-13 23:38:05 +0100107 RTL_GIGA_MAC_VER_06 = 0x06, // 8110SCe
Francois Romieu2857ffb2008-08-02 21:08:49 +0200108 RTL_GIGA_MAC_VER_07 = 0x07, // 8102e
109 RTL_GIGA_MAC_VER_08 = 0x08, // 8102e
110 RTL_GIGA_MAC_VER_09 = 0x09, // 8102e
111 RTL_GIGA_MAC_VER_10 = 0x0a, // 8101e
Francois Romieu2dd99532007-06-11 23:22:52 +0200112 RTL_GIGA_MAC_VER_11 = 0x0b, // 8168Bb
Francois Romieue3cf0cc2007-08-17 14:55:46 +0200113 RTL_GIGA_MAC_VER_12 = 0x0c, // 8168Be
114 RTL_GIGA_MAC_VER_13 = 0x0d, // 8101Eb
115 RTL_GIGA_MAC_VER_14 = 0x0e, // 8101 ?
116 RTL_GIGA_MAC_VER_15 = 0x0f, // 8101 ?
117 RTL_GIGA_MAC_VER_16 = 0x11, // 8101Ec
118 RTL_GIGA_MAC_VER_17 = 0x10, // 8168Bf
119 RTL_GIGA_MAC_VER_18 = 0x12, // 8168CP
120 RTL_GIGA_MAC_VER_19 = 0x13, // 8168C
Francois Romieu197ff762008-06-28 13:16:02 +0200121 RTL_GIGA_MAC_VER_20 = 0x14, // 8168C
Francois Romieu6fb07052008-06-29 11:54:28 +0200122 RTL_GIGA_MAC_VER_21 = 0x15, // 8168C
Francois Romieuef3386f2008-06-29 12:24:30 +0200123 RTL_GIGA_MAC_VER_22 = 0x16, // 8168C
Francois Romieu7f3e3d32008-07-20 18:53:20 +0200124 RTL_GIGA_MAC_VER_23 = 0x17, // 8168CP
Francois Romieu5b538df2008-07-20 16:22:45 +0200125 RTL_GIGA_MAC_VER_24 = 0x18, // 8168CP
françois romieudaf9df62009-10-07 12:44:20 +0000126 RTL_GIGA_MAC_VER_25 = 0x19, // 8168D
127 RTL_GIGA_MAC_VER_26 = 0x1a, // 8168D
françois romieue6de30d2011-01-03 15:08:37 +0000128 RTL_GIGA_MAC_VER_27 = 0x1b, // 8168DP
129 RTL_GIGA_MAC_VER_28 = 0x1c, // 8168DP
Hayes Wang5a5e4442011-02-22 17:26:21 +0800130 RTL_GIGA_MAC_VER_29 = 0x1d, // 8105E
131 RTL_GIGA_MAC_VER_30 = 0x1e, // 8105E
hayeswang4804b3b2011-03-21 01:50:29 +0000132 RTL_GIGA_MAC_VER_31 = 0x1f, // 8168DP
hayeswang01dc7fe2011-03-21 01:50:28 +0000133 RTL_GIGA_MAC_VER_32 = 0x20, // 8168E
134 RTL_GIGA_MAC_VER_33 = 0x21, // 8168E
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135};
136
Francois Romieu2b7b4312011-04-18 22:53:24 -0700137enum rtl_tx_desc_version {
138 RTL_TD_0 = 0,
139 RTL_TD_1 = 1,
140};
141
142#define _R(NAME,MAC,TD) \
143 { .name = NAME, .mac_version = MAC, .txd_version = TD }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800145static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 const char *name;
147 u8 mac_version;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700148 enum rtl_tx_desc_version txd_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149} rtl_chip_info[] = {
Francois Romieu2b7b4312011-04-18 22:53:24 -0700150 _R("RTL8169", RTL_GIGA_MAC_VER_01, RTL_TD_0), // 8169
151 _R("RTL8169s", RTL_GIGA_MAC_VER_02, RTL_TD_0), // 8169S
152 _R("RTL8110s", RTL_GIGA_MAC_VER_03, RTL_TD_0), // 8110S
153 _R("RTL8169sb/8110sb", RTL_GIGA_MAC_VER_04, RTL_TD_0), // 8169SB
154 _R("RTL8169sc/8110sc", RTL_GIGA_MAC_VER_05, RTL_TD_0), // 8110SCd
155 _R("RTL8169sc/8110sc", RTL_GIGA_MAC_VER_06, RTL_TD_0), // 8110SCe
156 _R("RTL8102e", RTL_GIGA_MAC_VER_07, RTL_TD_1), // PCI-E
157 _R("RTL8102e", RTL_GIGA_MAC_VER_08, RTL_TD_1), // PCI-E
158 _R("RTL8102e", RTL_GIGA_MAC_VER_09, RTL_TD_1), // PCI-E
159 _R("RTL8101e", RTL_GIGA_MAC_VER_10, RTL_TD_0), // PCI-E
160 _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_11, RTL_TD_0), // PCI-E
161 _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_12, RTL_TD_0), // PCI-E
162 _R("RTL8101e", RTL_GIGA_MAC_VER_13, RTL_TD_0), // PCI-E 8139
163 _R("RTL8100e", RTL_GIGA_MAC_VER_14, RTL_TD_0), // PCI-E 8139
164 _R("RTL8100e", RTL_GIGA_MAC_VER_15, RTL_TD_0), // PCI-E 8139
165 _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_17, RTL_TD_0), // PCI-E
166 _R("RTL8101e", RTL_GIGA_MAC_VER_16, RTL_TD_0), // PCI-E
167 _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_18, RTL_TD_1), // PCI-E
168 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_19, RTL_TD_1), // PCI-E
169 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_20, RTL_TD_1), // PCI-E
170 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_21, RTL_TD_1), // PCI-E
171 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_22, RTL_TD_1), // PCI-E
172 _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_23, RTL_TD_1), // PCI-E
173 _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_24, RTL_TD_1), // PCI-E
174 _R("RTL8168d/8111d", RTL_GIGA_MAC_VER_25, RTL_TD_1), // PCI-E
175 _R("RTL8168d/8111d", RTL_GIGA_MAC_VER_26, RTL_TD_1), // PCI-E
176 _R("RTL8168dp/8111dp", RTL_GIGA_MAC_VER_27, RTL_TD_1), // PCI-E
177 _R("RTL8168dp/8111dp", RTL_GIGA_MAC_VER_28, RTL_TD_1), // PCI-E
178 _R("RTL8105e", RTL_GIGA_MAC_VER_29, RTL_TD_1), // PCI-E
179 _R("RTL8105e", RTL_GIGA_MAC_VER_30, RTL_TD_1), // PCI-E
180 _R("RTL8168dp/8111dp", RTL_GIGA_MAC_VER_31, RTL_TD_1), // PCI-E
181 _R("RTL8168e/8111e", RTL_GIGA_MAC_VER_32, RTL_TD_1), // PCI-E
182 _R("RTL8168e/8111e", RTL_GIGA_MAC_VER_33, RTL_TD_1) // PCI-E
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183};
184#undef _R
185
François Romieu953a12c2011-04-24 17:38:48 +0200186static const struct rtl_firmware_info {
187 int mac_version;
188 const char *fw_name;
189} rtl_firmware_infos[] = {
190 { .mac_version = RTL_GIGA_MAC_VER_25, .fw_name = FIRMWARE_8168D_1 },
191 { .mac_version = RTL_GIGA_MAC_VER_26, .fw_name = FIRMWARE_8168D_2 },
192 { .mac_version = RTL_GIGA_MAC_VER_29, .fw_name = FIRMWARE_8105E_1 },
David S. Miller2bd93d72011-04-26 12:16:46 -0700193 { .mac_version = RTL_GIGA_MAC_VER_30, .fw_name = FIRMWARE_8105E_1 },
Francois Romieu15ecd032011-04-27 13:52:22 -0700194 { .mac_version = RTL_GIGA_MAC_VER_32, .fw_name = FIRMWARE_8168E_1 },
195 { .mac_version = RTL_GIGA_MAC_VER_33, .fw_name = FIRMWARE_8168E_2 }
François Romieu953a12c2011-04-24 17:38:48 +0200196};
197
Francois Romieubcf0bf92006-07-26 23:14:13 +0200198enum cfg_version {
199 RTL_CFG_0 = 0x00,
200 RTL_CFG_1,
201 RTL_CFG_2
202};
203
Francois Romieu07ce4062007-02-23 23:36:39 +0100204static void rtl_hw_start_8169(struct net_device *);
205static void rtl_hw_start_8168(struct net_device *);
206static void rtl_hw_start_8101(struct net_device *);
207
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000208static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200209 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200210 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Francois Romieud81bf552006-09-20 21:31:20 +0200211 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100212 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200213 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
214 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200215 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200216 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
217 { PCI_VENDOR_ID_LINKSYS, 0x1032,
218 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100219 { 0x0001, 0x8168,
220 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 {0,},
222};
223
224MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
225
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000226static int rx_buf_sz = 16383;
David S. Miller4300e8c2010-03-26 10:23:30 -0700227static int use_dac;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200228static struct {
229 u32 msg_enable;
230} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Francois Romieu07d3f512007-02-21 22:40:46 +0100232enum rtl_registers {
233 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100234 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100235 MAR0 = 8, /* Multicast filter. */
236 CounterAddrLow = 0x10,
237 CounterAddrHigh = 0x14,
238 TxDescStartAddrLow = 0x20,
239 TxDescStartAddrHigh = 0x24,
240 TxHDescStartAddrLow = 0x28,
241 TxHDescStartAddrHigh = 0x2c,
242 FLASH = 0x30,
243 ERSR = 0x36,
244 ChipCmd = 0x37,
245 TxPoll = 0x38,
246 IntrMask = 0x3c,
247 IntrStatus = 0x3e,
248 TxConfig = 0x40,
249 RxConfig = 0x44,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700250
251#define RTL_RX_CONFIG_MASK 0xff7e1880u
252
Francois Romieu07d3f512007-02-21 22:40:46 +0100253 RxMissed = 0x4c,
254 Cfg9346 = 0x50,
255 Config0 = 0x51,
256 Config1 = 0x52,
257 Config2 = 0x53,
258 Config3 = 0x54,
259 Config4 = 0x55,
260 Config5 = 0x56,
261 MultiIntr = 0x5c,
262 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100263 PHYstatus = 0x6c,
264 RxMaxSize = 0xda,
265 CPlusCmd = 0xe0,
266 IntrMitigate = 0xe2,
267 RxDescAddrLow = 0xe4,
268 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000269 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
270
271#define NoEarlyTx 0x3f /* Max value : no early transmit. */
272
273 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
274
275#define TxPacketMax (8064 >> 7)
276
Francois Romieu07d3f512007-02-21 22:40:46 +0100277 FuncEvent = 0xf0,
278 FuncEventMask = 0xf4,
279 FuncPresetState = 0xf8,
280 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281};
282
Francois Romieuf162a5d2008-06-01 22:37:49 +0200283enum rtl8110_registers {
284 TBICSR = 0x64,
285 TBI_ANAR = 0x68,
286 TBI_LPAR = 0x6a,
287};
288
289enum rtl8168_8101_registers {
290 CSIDR = 0x64,
291 CSIAR = 0x68,
292#define CSIAR_FLAG 0x80000000
293#define CSIAR_WRITE_CMD 0x80000000
294#define CSIAR_BYTE_ENABLE 0x0f
295#define CSIAR_BYTE_ENABLE_SHIFT 12
296#define CSIAR_ADDR_MASK 0x0fff
françois romieu065c27c2011-01-03 15:08:12 +0000297 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200298 EPHYAR = 0x80,
299#define EPHYAR_FLAG 0x80000000
300#define EPHYAR_WRITE_CMD 0x80000000
301#define EPHYAR_REG_MASK 0x1f
302#define EPHYAR_REG_SHIFT 16
303#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800304 DLLPR = 0xd0,
305#define PM_SWITCH (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200306 DBG_REG = 0xd1,
307#define FIX_NAK_1 (1 << 4)
308#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800309 TWSI = 0xd2,
310 MCU = 0xd3,
311#define EN_NDP (1 << 3)
312#define EN_OOB_RESET (1 << 2)
françois romieudaf9df62009-10-07 12:44:20 +0000313 EFUSEAR = 0xdc,
314#define EFUSEAR_FLAG 0x80000000
315#define EFUSEAR_WRITE_CMD 0x80000000
316#define EFUSEAR_READ_CMD 0x00000000
317#define EFUSEAR_REG_MASK 0x03ff
318#define EFUSEAR_REG_SHIFT 8
319#define EFUSEAR_DATA_MASK 0xff
Francois Romieuf162a5d2008-06-01 22:37:49 +0200320};
321
françois romieuc0e45c12011-01-03 15:08:04 +0000322enum rtl8168_registers {
françois romieub646d902011-01-03 15:08:21 +0000323 ERIDR = 0x70,
324 ERIAR = 0x74,
325#define ERIAR_FLAG 0x80000000
326#define ERIAR_WRITE_CMD 0x80000000
327#define ERIAR_READ_CMD 0x00000000
328#define ERIAR_ADDR_BYTE_ALIGN 4
329#define ERIAR_EXGMAC 0
330#define ERIAR_MSIX 1
331#define ERIAR_ASF 2
332#define ERIAR_TYPE_SHIFT 16
333#define ERIAR_BYTEEN 0x0f
334#define ERIAR_BYTEEN_SHIFT 12
françois romieuc0e45c12011-01-03 15:08:04 +0000335 EPHY_RXER_NUM = 0x7c,
336 OCPDR = 0xb0, /* OCP GPHY access */
337#define OCPDR_WRITE_CMD 0x80000000
338#define OCPDR_READ_CMD 0x00000000
339#define OCPDR_REG_MASK 0x7f
340#define OCPDR_GPHY_REG_SHIFT 16
341#define OCPDR_DATA_MASK 0xffff
342 OCPAR = 0xb4,
343#define OCPAR_FLAG 0x80000000
344#define OCPAR_GPHY_WRITE_CMD 0x8000f060
345#define OCPAR_GPHY_READ_CMD 0x0000f060
hayeswang01dc7fe2011-03-21 01:50:28 +0000346 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
347 MISC = 0xf0, /* 8168e only. */
348 txpla_rst = (1 << 29)
françois romieuc0e45c12011-01-03 15:08:04 +0000349};
350
Francois Romieu07d3f512007-02-21 22:40:46 +0100351enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100353 SYSErr = 0x8000,
354 PCSTimeout = 0x4000,
355 SWInt = 0x0100,
356 TxDescUnavail = 0x0080,
357 RxFIFOOver = 0x0040,
358 LinkChg = 0x0020,
359 RxOverflow = 0x0010,
360 TxErr = 0x0008,
361 TxOK = 0x0004,
362 RxErr = 0x0002,
363 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 /* RxStatusDesc */
Francois Romieu9dccf612006-05-14 12:31:17 +0200366 RxFOVF = (1 << 23),
367 RxRWT = (1 << 22),
368 RxRES = (1 << 21),
369 RxRUNT = (1 << 20),
370 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 /* ChipCmdBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100373 CmdReset = 0x10,
374 CmdRxEnb = 0x08,
375 CmdTxEnb = 0x04,
376 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Francois Romieu275391a2007-02-23 23:50:28 +0100378 /* TXPoll register p.5 */
379 HPQ = 0x80, /* Poll cmd on the high prio queue */
380 NPQ = 0x40, /* Poll cmd on the low prio queue */
381 FSWInt = 0x01, /* Forced software interrupt */
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100384 Cfg9346_Lock = 0x00,
385 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100388 AcceptErr = 0x20,
389 AcceptRunt = 0x10,
390 AcceptBroadcast = 0x08,
391 AcceptMulticast = 0x04,
392 AcceptMyPhys = 0x02,
393 AcceptAllPhys = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 /* RxConfigBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100396 RxCfgFIFOShift = 13,
397 RxCfgDMAShift = 8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 /* TxConfigBits */
400 TxInterFrameGapShift = 24,
401 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
402
Francois Romieu5d06a992006-02-23 00:47:58 +0100403 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200404 LEDS1 = (1 << 7),
405 LEDS0 = (1 << 6),
Francois Romieufbac58f2007-10-04 22:51:38 +0200406 MSIEnable = (1 << 5), /* Enable Message Signaled Interrupt */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200407 Speed_down = (1 << 4),
408 MEMMAP = (1 << 3),
409 IOMAP = (1 << 2),
410 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100411 PMEnable = (1 << 0), /* Power Management Enable */
412
Francois Romieu6dccd162007-02-13 23:38:05 +0100413 /* Config2 register p. 25 */
414 PCI_Clock_66MHz = 0x01,
415 PCI_Clock_33MHz = 0x00,
416
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100417 /* Config3 register p.25 */
418 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
419 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200420 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100421
Francois Romieu5d06a992006-02-23 00:47:58 +0100422 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100423 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
424 MWF = (1 << 5), /* Accept Multicast wakeup frame */
425 UWF = (1 << 4), /* Accept Unicast wakeup frame */
hayeswang01dc7fe2011-03-21 01:50:28 +0000426 spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100427 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100428 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 /* TBICSR p.28 */
431 TBIReset = 0x80000000,
432 TBILoopback = 0x40000000,
433 TBINwEnable = 0x20000000,
434 TBINwRestart = 0x10000000,
435 TBILinkOk = 0x02000000,
436 TBINwComplete = 0x01000000,
437
438 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200439 EnableBist = (1 << 15), // 8168 8101
440 Mac_dbgo_oe = (1 << 14), // 8168 8101
441 Normal_mode = (1 << 13), // unused
442 Force_half_dup = (1 << 12), // 8168 8101
443 Force_rxflow_en = (1 << 11), // 8168 8101
444 Force_txflow_en = (1 << 10), // 8168 8101
445 Cxpl_dbg_sel = (1 << 9), // 8168 8101
446 ASF = (1 << 8), // 8168 8101
447 PktCntrDisable = (1 << 7), // 8168 8101
448 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 RxVlan = (1 << 6),
450 RxChkSum = (1 << 5),
451 PCIDAC = (1 << 4),
452 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100453 INTT_0 = 0x0000, // 8168
454 INTT_1 = 0x0001, // 8168
455 INTT_2 = 0x0002, // 8168
456 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100459 TBI_Enable = 0x80,
460 TxFlowCtrl = 0x40,
461 RxFlowCtrl = 0x20,
462 _1000bpsF = 0x10,
463 _100bps = 0x08,
464 _10bps = 0x04,
465 LinkStatus = 0x02,
466 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100469 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200470
471 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100472 CounterDump = 0x8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473};
474
Francois Romieu2b7b4312011-04-18 22:53:24 -0700475enum rtl_desc_bit {
476 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
478 RingEnd = (1 << 30), /* End of descriptor ring */
479 FirstFrag = (1 << 29), /* First segment of a packet */
480 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700481};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Francois Romieu2b7b4312011-04-18 22:53:24 -0700483/* Generic case. */
484enum rtl_tx_desc_bit {
485 /* First doubleword. */
486 TD_LSO = (1 << 27), /* Large Send Offload */
487#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Francois Romieu2b7b4312011-04-18 22:53:24 -0700489 /* Second doubleword. */
490 TxVlanTag = (1 << 17), /* Add VLAN tag */
491};
492
493/* 8169, 8168b and 810x except 8102e. */
494enum rtl_tx_desc_bit_0 {
495 /* First doubleword. */
496#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
497 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
498 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
499 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
500};
501
502/* 8102e, 8168c and beyond. */
503enum rtl_tx_desc_bit_1 {
504 /* Second doubleword. */
505#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
506 TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
507 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
508 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
509};
510
511static const struct rtl_tx_desc_info {
512 struct {
513 u32 udp;
514 u32 tcp;
515 } checksum;
516 u16 mss_shift;
517 u16 opts_offset;
518} tx_desc_info [] = {
519 [RTL_TD_0] = {
520 .checksum = {
521 .udp = TD0_IP_CS | TD0_UDP_CS,
522 .tcp = TD0_IP_CS | TD0_TCP_CS
523 },
524 .mss_shift = TD0_MSS_SHIFT,
525 .opts_offset = 0
526 },
527 [RTL_TD_1] = {
528 .checksum = {
529 .udp = TD1_IP_CS | TD1_UDP_CS,
530 .tcp = TD1_IP_CS | TD1_TCP_CS
531 },
532 .mss_shift = TD1_MSS_SHIFT,
533 .opts_offset = 1
534 }
535};
536
537enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 /* Rx private */
539 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
540 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
541
542#define RxProtoUDP (PID1)
543#define RxProtoTCP (PID0)
544#define RxProtoIP (PID1 | PID0)
545#define RxProtoMask RxProtoIP
546
547 IPFail = (1 << 16), /* IP checksum failed */
548 UDPFail = (1 << 15), /* UDP/IP checksum failed */
549 TCPFail = (1 << 14), /* TCP/IP checksum failed */
550 RxVlanTag = (1 << 16), /* VLAN tag available */
551};
552
553#define RsvdMask 0x3fffc000
554
555struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200556 __le32 opts1;
557 __le32 opts2;
558 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559};
560
561struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200562 __le32 opts1;
563 __le32 opts2;
564 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565};
566
567struct ring_info {
568 struct sk_buff *skb;
569 u32 len;
570 u8 __pad[sizeof(void *) - sizeof(u32)];
571};
572
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200573enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200574 RTL_FEATURE_WOL = (1 << 0),
575 RTL_FEATURE_MSI = (1 << 1),
576 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200577};
578
Ivan Vecera355423d2009-02-06 21:49:57 -0800579struct rtl8169_counters {
580 __le64 tx_packets;
581 __le64 rx_packets;
582 __le64 tx_errors;
583 __le32 rx_errors;
584 __le16 rx_missed;
585 __le16 align_errors;
586 __le32 tx_one_collision;
587 __le32 tx_multi_collision;
588 __le64 rx_unicast;
589 __le64 rx_broadcast;
590 __le32 rx_multicast;
591 __le16 tx_aborted;
592 __le16 tx_underun;
593};
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595struct rtl8169_private {
596 void __iomem *mmio_addr; /* memory map physical address */
597 struct pci_dev *pci_dev; /* Index of PCI device */
David Howellsc4028952006-11-22 14:57:56 +0000598 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700599 struct napi_struct napi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 spinlock_t lock; /* spin lock flag */
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200601 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700602 u16 txd_version;
603 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
605 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
606 u32 dirty_rx;
607 u32 dirty_tx;
608 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
609 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
610 dma_addr_t TxPhyAddr;
611 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000612 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 struct timer_list timer;
615 u16 cp_cmd;
Francois Romieu0e485152007-02-20 00:00:26 +0100616 u16 intr_event;
617 u16 napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 u16 intr_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 int phy_1000_ctrl_reg;
françois romieuc0e45c12011-01-03 15:08:04 +0000620
621 struct mdio_ops {
622 void (*write)(void __iomem *, int, int);
623 int (*read)(void __iomem *, int);
624 } mdio_ops;
625
françois romieu065c27c2011-01-03 15:08:12 +0000626 struct pll_power_ops {
627 void (*down)(struct rtl8169_private *);
628 void (*up)(struct rtl8169_private *);
629 } pll_power_ops;
630
Oliver Neukum54405cd2011-01-06 21:55:13 +0100631 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Francois Romieuccdffb92008-07-26 14:26:06 +0200632 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000633 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100634 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000635 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800637 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
Francois Romieu9c14cea2008-07-05 00:21:15 +0200638 int pcie_cap;
David Howellsc4028952006-11-22 14:57:56 +0000639 struct delayed_work task;
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200640 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200641
642 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800643 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000644 u32 saved_wolopts;
françois romieuf1e02ed2011-01-13 13:07:53 +0000645
646 const struct firmware *fw;
François Romieu953a12c2011-04-24 17:38:48 +0200647#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648};
649
Ralf Baechle979b6c12005-06-13 14:30:40 -0700650MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700653MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200654module_param_named(debug, debug.msg_enable, int, 0);
655MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656MODULE_LICENSE("GPL");
657MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000658MODULE_FIRMWARE(FIRMWARE_8168D_1);
659MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000660MODULE_FIRMWARE(FIRMWARE_8168E_1);
661MODULE_FIRMWARE(FIRMWARE_8168E_2);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800662MODULE_FIRMWARE(FIRMWARE_8105E_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664static int rtl8169_open(struct net_device *dev);
Stephen Hemminger613573252009-08-31 19:50:58 +0000665static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
666 struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100667static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668static int rtl8169_init_ring(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100669static void rtl_hw_start(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670static int rtl8169_close(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100671static void rtl_set_rx_mode(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672static void rtl8169_tx_timeout(struct net_device *dev);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200673static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674static int rtl8169_rx_interrupt(struct net_device *, struct rtl8169_private *,
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700675 void __iomem *, u32 budget);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200676static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677static void rtl8169_down(struct net_device *dev);
Francois Romieu99f252b2007-04-02 22:59:59 +0200678static void rtl8169_rx_clear(struct rtl8169_private *tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700679static int rtl8169_poll(struct napi_struct *napi, int budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681static const unsigned int rtl8169_rx_config =
Francois Romieu5b0384f2006-08-16 16:00:01 +0200682 (RX_FIFO_THRESH << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
françois romieub646d902011-01-03 15:08:21 +0000684static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
685{
686 void __iomem *ioaddr = tp->mmio_addr;
687 int i;
688
689 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
690 for (i = 0; i < 20; i++) {
691 udelay(100);
692 if (RTL_R32(OCPAR) & OCPAR_FLAG)
693 break;
694 }
695 return RTL_R32(OCPDR);
696}
697
698static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
699{
700 void __iomem *ioaddr = tp->mmio_addr;
701 int i;
702
703 RTL_W32(OCPDR, data);
704 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
705 for (i = 0; i < 20; i++) {
706 udelay(100);
707 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
708 break;
709 }
710}
711
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800712static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000713{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800714 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000715 int i;
716
717 RTL_W8(ERIDR, cmd);
718 RTL_W32(ERIAR, 0x800010e8);
719 msleep(2);
720 for (i = 0; i < 5; i++) {
721 udelay(100);
722 if (!(RTL_R32(ERIDR) & ERIAR_FLAG))
723 break;
724 }
725
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800726 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000727}
728
729#define OOB_CMD_RESET 0x00
730#define OOB_CMD_DRIVER_START 0x05
731#define OOB_CMD_DRIVER_STOP 0x06
732
733static void rtl8168_driver_start(struct rtl8169_private *tp)
734{
735 int i;
hayeswang4804b3b2011-03-21 01:50:29 +0000736 u32 reg;
françois romieub646d902011-01-03 15:08:21 +0000737
738 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
739
hayeswang4804b3b2011-03-21 01:50:29 +0000740 if (tp->mac_version == RTL_GIGA_MAC_VER_31)
741 reg = 0xb8;
742 else
743 reg = 0x10;
744
françois romieub646d902011-01-03 15:08:21 +0000745 for (i = 0; i < 10; i++) {
746 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000747 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
françois romieub646d902011-01-03 15:08:21 +0000748 break;
749 }
750}
751
752static void rtl8168_driver_stop(struct rtl8169_private *tp)
753{
754 int i;
hayeswang4804b3b2011-03-21 01:50:29 +0000755 u32 reg;
françois romieub646d902011-01-03 15:08:21 +0000756
757 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
758
hayeswang4804b3b2011-03-21 01:50:29 +0000759 if (tp->mac_version == RTL_GIGA_MAC_VER_31)
760 reg = 0xb8;
761 else
762 reg = 0x10;
763
françois romieub646d902011-01-03 15:08:21 +0000764 for (i = 0; i < 10; i++) {
765 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000766 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
françois romieub646d902011-01-03 15:08:21 +0000767 break;
768 }
769}
770
hayeswang4804b3b2011-03-21 01:50:29 +0000771static int r8168dp_check_dash(struct rtl8169_private *tp)
772{
773 u32 reg;
774
775 if (tp->mac_version == RTL_GIGA_MAC_VER_31)
776 reg = 0xb8;
777 else
778 reg = 0x10;
779
780 if (ocp_read(tp, 0xF, reg) & 0x00008000)
781 return 1;
782 else
783 return 0;
784}
françois romieub646d902011-01-03 15:08:21 +0000785
françois romieu4da19632011-01-03 15:07:55 +0000786static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
788 int i;
789
Francois Romieua6baf3a2007-11-08 23:23:21 +0100790 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Francois Romieu23714082006-01-29 00:49:09 +0100792 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100793 /*
794 * Check if the RTL8169 has completed writing to the specified
795 * MII register.
796 */
Francois Romieu5b0384f2006-08-16 16:00:01 +0200797 if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 break;
Francois Romieu23714082006-01-29 00:49:09 +0100799 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
Timo Teräs024a07b2010-06-06 15:38:47 -0700801 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700802 * According to hardware specs a 20us delay is required after write
803 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700804 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700805 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807
françois romieu4da19632011-01-03 15:07:55 +0000808static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
810 int i, value = -1;
811
Francois Romieua6baf3a2007-11-08 23:23:21 +0100812 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Francois Romieu23714082006-01-29 00:49:09 +0100814 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100815 /*
816 * Check if the RTL8169 has completed retrieving data from
817 * the specified MII register.
818 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (RTL_R32(PHYAR) & 0x80000000) {
Francois Romieua6baf3a2007-11-08 23:23:21 +0100820 value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 break;
822 }
Francois Romieu23714082006-01-29 00:49:09 +0100823 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 }
Timo Teräs81a95f02010-06-09 17:31:48 -0700825 /*
826 * According to hardware specs a 20us delay is required after read
827 * complete indication, but before sending next command.
828 */
829 udelay(20);
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return value;
832}
833
françois romieuc0e45c12011-01-03 15:08:04 +0000834static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
835{
836 int i;
837
838 RTL_W32(OCPDR, data |
839 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
840 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
841 RTL_W32(EPHY_RXER_NUM, 0);
842
843 for (i = 0; i < 100; i++) {
844 mdelay(1);
845 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
846 break;
847 }
848}
849
850static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
851{
852 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
853 (value & OCPDR_DATA_MASK));
854}
855
856static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
857{
858 int i;
859
860 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
861
862 mdelay(1);
863 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
864 RTL_W32(EPHY_RXER_NUM, 0);
865
866 for (i = 0; i < 100; i++) {
867 mdelay(1);
868 if (RTL_R32(OCPAR) & OCPAR_FLAG)
869 break;
870 }
871
872 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
873}
874
françois romieue6de30d2011-01-03 15:08:37 +0000875#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
876
877static void r8168dp_2_mdio_start(void __iomem *ioaddr)
878{
879 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
880}
881
882static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
883{
884 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
885}
886
887static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
888{
889 r8168dp_2_mdio_start(ioaddr);
890
891 r8169_mdio_write(ioaddr, reg_addr, value);
892
893 r8168dp_2_mdio_stop(ioaddr);
894}
895
896static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
897{
898 int value;
899
900 r8168dp_2_mdio_start(ioaddr);
901
902 value = r8169_mdio_read(ioaddr, reg_addr);
903
904 r8168dp_2_mdio_stop(ioaddr);
905
906 return value;
907}
908
françois romieu4da19632011-01-03 15:07:55 +0000909static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +0200910{
françois romieuc0e45c12011-01-03 15:08:04 +0000911 tp->mdio_ops.write(tp->mmio_addr, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +0200912}
913
françois romieu4da19632011-01-03 15:07:55 +0000914static int rtl_readphy(struct rtl8169_private *tp, int location)
915{
françois romieuc0e45c12011-01-03 15:08:04 +0000916 return tp->mdio_ops.read(tp->mmio_addr, location);
françois romieu4da19632011-01-03 15:07:55 +0000917}
918
919static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
920{
921 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
922}
923
924static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +0000925{
926 int val;
927
françois romieu4da19632011-01-03 15:07:55 +0000928 val = rtl_readphy(tp, reg_addr);
929 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +0000930}
931
Francois Romieuccdffb92008-07-26 14:26:06 +0200932static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
933 int val)
934{
935 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +0200936
françois romieu4da19632011-01-03 15:07:55 +0000937 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +0200938}
939
940static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
941{
942 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +0200943
françois romieu4da19632011-01-03 15:07:55 +0000944 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +0200945}
946
Francois Romieudacf8152008-08-02 20:44:13 +0200947static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
948{
949 unsigned int i;
950
951 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
952 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
953
954 for (i = 0; i < 100; i++) {
955 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
956 break;
957 udelay(10);
958 }
959}
960
961static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
962{
963 u16 value = 0xffff;
964 unsigned int i;
965
966 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
967
968 for (i = 0; i < 100; i++) {
969 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
970 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
971 break;
972 }
973 udelay(10);
974 }
975
976 return value;
977}
978
979static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
980{
981 unsigned int i;
982
983 RTL_W32(CSIDR, value);
984 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
985 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
986
987 for (i = 0; i < 100; i++) {
988 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
989 break;
990 udelay(10);
991 }
992}
993
994static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
995{
996 u32 value = ~0x00;
997 unsigned int i;
998
999 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
1000 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1001
1002 for (i = 0; i < 100; i++) {
1003 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
1004 value = RTL_R32(CSIDR);
1005 break;
1006 }
1007 udelay(10);
1008 }
1009
1010 return value;
1011}
1012
françois romieudaf9df62009-10-07 12:44:20 +00001013static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1014{
1015 u8 value = 0xff;
1016 unsigned int i;
1017
1018 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1019
1020 for (i = 0; i < 300; i++) {
1021 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1022 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1023 break;
1024 }
1025 udelay(100);
1026 }
1027
1028 return value;
1029}
1030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031static void rtl8169_irq_mask_and_ack(void __iomem *ioaddr)
1032{
1033 RTL_W16(IntrMask, 0x0000);
1034
1035 RTL_W16(IntrStatus, 0xffff);
1036}
1037
1038static void rtl8169_asic_down(void __iomem *ioaddr)
1039{
1040 RTL_W8(ChipCmd, 0x00);
1041 rtl8169_irq_mask_and_ack(ioaddr);
1042 RTL_R16(CPlusCmd);
1043}
1044
françois romieu4da19632011-01-03 15:07:55 +00001045static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
françois romieu4da19632011-01-03 15:07:55 +00001047 void __iomem *ioaddr = tp->mmio_addr;
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return RTL_R32(TBICSR) & TBIReset;
1050}
1051
françois romieu4da19632011-01-03 15:07:55 +00001052static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
françois romieu4da19632011-01-03 15:07:55 +00001054 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055}
1056
1057static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1058{
1059 return RTL_R32(TBICSR) & TBILinkOk;
1060}
1061
1062static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1063{
1064 return RTL_R8(PHYstatus) & LinkStatus;
1065}
1066
françois romieu4da19632011-01-03 15:07:55 +00001067static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
françois romieu4da19632011-01-03 15:07:55 +00001069 void __iomem *ioaddr = tp->mmio_addr;
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1072}
1073
françois romieu4da19632011-01-03 15:07:55 +00001074static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
1076 unsigned int val;
1077
françois romieu4da19632011-01-03 15:07:55 +00001078 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1079 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}
1081
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001082static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieu07d3f512007-02-21 22:40:46 +01001083 struct rtl8169_private *tp,
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001084 void __iomem *ioaddr,
1085 bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
1087 unsigned long flags;
1088
1089 spin_lock_irqsave(&tp->lock, flags);
1090 if (tp->link_ok(ioaddr)) {
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001091 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001092 if (pm)
1093 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001095 if (net_ratelimit())
1096 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001097 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001099 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001100 if (pm)
1101 pm_schedule_suspend(&tp->pci_dev->dev, 100);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 spin_unlock_irqrestore(&tp->lock, flags);
1104}
1105
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001106static void rtl8169_check_link_status(struct net_device *dev,
1107 struct rtl8169_private *tp,
1108 void __iomem *ioaddr)
1109{
1110 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1111}
1112
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001113#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1114
1115static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1116{
1117 void __iomem *ioaddr = tp->mmio_addr;
1118 u8 options;
1119 u32 wolopts = 0;
1120
1121 options = RTL_R8(Config1);
1122 if (!(options & PMEnable))
1123 return 0;
1124
1125 options = RTL_R8(Config3);
1126 if (options & LinkUp)
1127 wolopts |= WAKE_PHY;
1128 if (options & MagicPacket)
1129 wolopts |= WAKE_MAGIC;
1130
1131 options = RTL_R8(Config5);
1132 if (options & UWF)
1133 wolopts |= WAKE_UCAST;
1134 if (options & BWF)
1135 wolopts |= WAKE_BCAST;
1136 if (options & MWF)
1137 wolopts |= WAKE_MCAST;
1138
1139 return wolopts;
1140}
1141
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001142static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1143{
1144 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001145
1146 spin_lock_irq(&tp->lock);
1147
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001148 wol->supported = WAKE_ANY;
1149 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001150
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001151 spin_unlock_irq(&tp->lock);
1152}
1153
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001154static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001155{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001156 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001157 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001158 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001159 u32 opt;
1160 u16 reg;
1161 u8 mask;
1162 } cfg[] = {
1163 { WAKE_ANY, Config1, PMEnable },
1164 { WAKE_PHY, Config3, LinkUp },
1165 { WAKE_MAGIC, Config3, MagicPacket },
1166 { WAKE_UCAST, Config5, UWF },
1167 { WAKE_BCAST, Config5, BWF },
1168 { WAKE_MCAST, Config5, MWF },
1169 { WAKE_ANY, Config5, LanWake }
1170 };
1171
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001172 RTL_W8(Cfg9346, Cfg9346_Unlock);
1173
1174 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1175 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001176 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001177 options |= cfg[i].mask;
1178 RTL_W8(cfg[i].reg, options);
1179 }
1180
1181 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001182}
1183
1184static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1185{
1186 struct rtl8169_private *tp = netdev_priv(dev);
1187
1188 spin_lock_irq(&tp->lock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001189
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001190 if (wol->wolopts)
1191 tp->features |= RTL_FEATURE_WOL;
1192 else
1193 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001194 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001195 spin_unlock_irq(&tp->lock);
1196
françois romieuea809072010-11-08 13:23:58 +00001197 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1198
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001199 return 0;
1200}
1201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202static void rtl8169_get_drvinfo(struct net_device *dev,
1203 struct ethtool_drvinfo *info)
1204{
1205 struct rtl8169_private *tp = netdev_priv(dev);
1206
1207 strcpy(info->driver, MODULENAME);
1208 strcpy(info->version, RTL8169_VERSION);
1209 strcpy(info->bus_info, pci_name(tp->pci_dev));
1210}
1211
1212static int rtl8169_get_regs_len(struct net_device *dev)
1213{
1214 return R8169_REGS_SIZE;
1215}
1216
1217static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001218 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
1220 struct rtl8169_private *tp = netdev_priv(dev);
1221 void __iomem *ioaddr = tp->mmio_addr;
1222 int ret = 0;
1223 u32 reg;
1224
1225 reg = RTL_R32(TBICSR);
1226 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1227 (duplex == DUPLEX_FULL)) {
1228 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1229 } else if (autoneg == AUTONEG_ENABLE)
1230 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1231 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001232 netif_warn(tp, link, dev,
1233 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 ret = -EOPNOTSUPP;
1235 }
1236
1237 return ret;
1238}
1239
1240static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001241 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
1243 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001244 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001245 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Hayes Wang716b50a2011-02-22 17:26:18 +08001247 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001250 int auto_nego;
1251
françois romieu4da19632011-01-03 15:07:55 +00001252 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001253 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1254 ADVERTISE_100HALF | ADVERTISE_100FULL);
1255
1256 if (adv & ADVERTISED_10baseT_Half)
1257 auto_nego |= ADVERTISE_10HALF;
1258 if (adv & ADVERTISED_10baseT_Full)
1259 auto_nego |= ADVERTISE_10FULL;
1260 if (adv & ADVERTISED_100baseT_Half)
1261 auto_nego |= ADVERTISE_100HALF;
1262 if (adv & ADVERTISED_100baseT_Full)
1263 auto_nego |= ADVERTISE_100FULL;
1264
françois romieu3577aa12009-05-19 10:46:48 +00001265 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1266
françois romieu4da19632011-01-03 15:07:55 +00001267 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001268 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1269
1270 /* The 8100e/8101e/8102e do Fast Ethernet only. */
1271 if ((tp->mac_version != RTL_GIGA_MAC_VER_07) &&
1272 (tp->mac_version != RTL_GIGA_MAC_VER_08) &&
1273 (tp->mac_version != RTL_GIGA_MAC_VER_09) &&
1274 (tp->mac_version != RTL_GIGA_MAC_VER_10) &&
1275 (tp->mac_version != RTL_GIGA_MAC_VER_13) &&
1276 (tp->mac_version != RTL_GIGA_MAC_VER_14) &&
1277 (tp->mac_version != RTL_GIGA_MAC_VER_15) &&
Hayes Wang5a5e4442011-02-22 17:26:21 +08001278 (tp->mac_version != RTL_GIGA_MAC_VER_16) &&
1279 (tp->mac_version != RTL_GIGA_MAC_VER_29) &&
1280 (tp->mac_version != RTL_GIGA_MAC_VER_30)) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001281 if (adv & ADVERTISED_1000baseT_Half)
1282 giga_ctrl |= ADVERTISE_1000HALF;
1283 if (adv & ADVERTISED_1000baseT_Full)
1284 giga_ctrl |= ADVERTISE_1000FULL;
1285 } else if (adv & (ADVERTISED_1000baseT_Half |
1286 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001287 netif_info(tp, link, dev,
1288 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001289 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
françois romieu3577aa12009-05-19 10:46:48 +00001292 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001293
françois romieu4da19632011-01-03 15:07:55 +00001294 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1295 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001296 } else {
1297 giga_ctrl = 0;
1298
1299 if (speed == SPEED_10)
1300 bmcr = 0;
1301 else if (speed == SPEED_100)
1302 bmcr = BMCR_SPEED100;
1303 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001304 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001305
1306 if (duplex == DUPLEX_FULL)
1307 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001308 }
1309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 tp->phy_1000_ctrl_reg = giga_ctrl;
1311
françois romieu4da19632011-01-03 15:07:55 +00001312 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001313
1314 if ((tp->mac_version == RTL_GIGA_MAC_VER_02) ||
1315 (tp->mac_version == RTL_GIGA_MAC_VER_03)) {
1316 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001317 rtl_writephy(tp, 0x17, 0x2138);
1318 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001319 } else {
françois romieu4da19632011-01-03 15:07:55 +00001320 rtl_writephy(tp, 0x17, 0x2108);
1321 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001322 }
1323 }
1324
Oliver Neukum54405cd2011-01-06 21:55:13 +01001325 rc = 0;
1326out:
1327 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328}
1329
1330static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001331 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
1333 struct rtl8169_private *tp = netdev_priv(dev);
1334 int ret;
1335
Oliver Neukum54405cd2011-01-06 21:55:13 +01001336 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Francois Romieu64e4bfb2006-08-17 12:43:06 +02001338 if (netif_running(dev) && (tp->phy_1000_ctrl_reg & ADVERTISE_1000FULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
1340
1341 return ret;
1342}
1343
1344static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1345{
1346 struct rtl8169_private *tp = netdev_priv(dev);
1347 unsigned long flags;
1348 int ret;
1349
1350 spin_lock_irqsave(&tp->lock, flags);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001351 ret = rtl8169_set_speed(dev,
1352 cmd->autoneg, cmd->speed, cmd->duplex, cmd->advertising);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 return ret;
1356}
1357
Michał Mirosław350fb322011-04-08 06:35:56 +00001358static u32 rtl8169_fix_features(struct net_device *dev, u32 features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
Francois Romieu2b7b4312011-04-18 22:53:24 -07001360 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001361 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Michał Mirosław350fb322011-04-08 06:35:56 +00001363 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364}
1365
Michał Mirosław350fb322011-04-08 06:35:56 +00001366static int rtl8169_set_features(struct net_device *dev, u32 features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
1368 struct rtl8169_private *tp = netdev_priv(dev);
1369 void __iomem *ioaddr = tp->mmio_addr;
1370 unsigned long flags;
1371
1372 spin_lock_irqsave(&tp->lock, flags);
1373
Michał Mirosław350fb322011-04-08 06:35:56 +00001374 if (features & NETIF_F_RXCSUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 tp->cp_cmd |= RxChkSum;
1376 else
1377 tp->cp_cmd &= ~RxChkSum;
1378
Michał Mirosław350fb322011-04-08 06:35:56 +00001379 if (dev->features & NETIF_F_HW_VLAN_RX)
1380 tp->cp_cmd |= RxVlan;
1381 else
1382 tp->cp_cmd &= ~RxVlan;
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 RTL_W16(CPlusCmd, tp->cp_cmd);
1385 RTL_R16(CPlusCmd);
1386
1387 spin_unlock_irqrestore(&tp->lock, flags);
1388
1389 return 0;
1390}
1391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1393 struct sk_buff *skb)
1394{
Jesse Grosseab6d182010-10-20 13:56:03 +00001395 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1397}
1398
Francois Romieu7a8fc772011-03-01 17:18:33 +01001399static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
1401 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Francois Romieu7a8fc772011-03-01 17:18:33 +01001403 if (opts2 & RxVlanTag)
1404 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407}
1408
Francois Romieuccdffb92008-07-26 14:26:06 +02001409static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
1411 struct rtl8169_private *tp = netdev_priv(dev);
1412 void __iomem *ioaddr = tp->mmio_addr;
1413 u32 status;
1414
1415 cmd->supported =
1416 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1417 cmd->port = PORT_FIBRE;
1418 cmd->transceiver = XCVR_INTERNAL;
1419
1420 status = RTL_R32(TBICSR);
1421 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1422 cmd->autoneg = !!(status & TBINwEnable);
1423
1424 cmd->speed = SPEED_1000;
1425 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001426
1427 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428}
1429
Francois Romieuccdffb92008-07-26 14:26:06 +02001430static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
1432 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Francois Romieuccdffb92008-07-26 14:26:06 +02001434 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435}
1436
1437static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1438{
1439 struct rtl8169_private *tp = netdev_priv(dev);
1440 unsigned long flags;
Francois Romieuccdffb92008-07-26 14:26:06 +02001441 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 spin_lock_irqsave(&tp->lock, flags);
1444
Francois Romieuccdffb92008-07-26 14:26:06 +02001445 rc = tp->get_settings(dev, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
1447 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieuccdffb92008-07-26 14:26:06 +02001448 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449}
1450
1451static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1452 void *p)
1453{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001454 struct rtl8169_private *tp = netdev_priv(dev);
1455 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Francois Romieu5b0384f2006-08-16 16:00:01 +02001457 if (regs->len > R8169_REGS_SIZE)
1458 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Francois Romieu5b0384f2006-08-16 16:00:01 +02001460 spin_lock_irqsave(&tp->lock, flags);
1461 memcpy_fromio(p, tp->mmio_addr, regs->len);
1462 spin_unlock_irqrestore(&tp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463}
1464
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001465static u32 rtl8169_get_msglevel(struct net_device *dev)
1466{
1467 struct rtl8169_private *tp = netdev_priv(dev);
1468
1469 return tp->msg_enable;
1470}
1471
1472static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1473{
1474 struct rtl8169_private *tp = netdev_priv(dev);
1475
1476 tp->msg_enable = value;
1477}
1478
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001479static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1480 "tx_packets",
1481 "rx_packets",
1482 "tx_errors",
1483 "rx_errors",
1484 "rx_missed",
1485 "align_errors",
1486 "tx_single_collisions",
1487 "tx_multi_collisions",
1488 "unicast",
1489 "broadcast",
1490 "multicast",
1491 "tx_aborted",
1492 "tx_underrun",
1493};
1494
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001495static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001496{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001497 switch (sset) {
1498 case ETH_SS_STATS:
1499 return ARRAY_SIZE(rtl8169_gstrings);
1500 default:
1501 return -EOPNOTSUPP;
1502 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001503}
1504
Ivan Vecera355423d2009-02-06 21:49:57 -08001505static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001506{
1507 struct rtl8169_private *tp = netdev_priv(dev);
1508 void __iomem *ioaddr = tp->mmio_addr;
1509 struct rtl8169_counters *counters;
1510 dma_addr_t paddr;
1511 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001512 int wait = 1000;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001513 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001514
Ivan Vecera355423d2009-02-06 21:49:57 -08001515 /*
1516 * Some chips are unable to dump tally counters when the receiver
1517 * is disabled.
1518 */
1519 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1520 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001521
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001522 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001523 if (!counters)
1524 return;
1525
1526 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001527 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001528 RTL_W32(CounterAddrLow, cmd);
1529 RTL_W32(CounterAddrLow, cmd | CounterDump);
1530
Ivan Vecera355423d2009-02-06 21:49:57 -08001531 while (wait--) {
1532 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
1533 /* copy updated counters */
1534 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001535 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001536 }
1537 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001538 }
1539
1540 RTL_W32(CounterAddrLow, 0);
1541 RTL_W32(CounterAddrHigh, 0);
1542
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001543 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001544}
1545
Ivan Vecera355423d2009-02-06 21:49:57 -08001546static void rtl8169_get_ethtool_stats(struct net_device *dev,
1547 struct ethtool_stats *stats, u64 *data)
1548{
1549 struct rtl8169_private *tp = netdev_priv(dev);
1550
1551 ASSERT_RTNL();
1552
1553 rtl8169_update_counters(dev);
1554
1555 data[0] = le64_to_cpu(tp->counters.tx_packets);
1556 data[1] = le64_to_cpu(tp->counters.rx_packets);
1557 data[2] = le64_to_cpu(tp->counters.tx_errors);
1558 data[3] = le32_to_cpu(tp->counters.rx_errors);
1559 data[4] = le16_to_cpu(tp->counters.rx_missed);
1560 data[5] = le16_to_cpu(tp->counters.align_errors);
1561 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1562 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1563 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1564 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1565 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1566 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1567 data[12] = le16_to_cpu(tp->counters.tx_underun);
1568}
1569
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001570static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1571{
1572 switch(stringset) {
1573 case ETH_SS_STATS:
1574 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1575 break;
1576 }
1577}
1578
Jeff Garzik7282d492006-09-13 14:30:00 -04001579static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 .get_drvinfo = rtl8169_get_drvinfo,
1581 .get_regs_len = rtl8169_get_regs_len,
1582 .get_link = ethtool_op_get_link,
1583 .get_settings = rtl8169_get_settings,
1584 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001585 .get_msglevel = rtl8169_get_msglevel,
1586 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001588 .get_wol = rtl8169_get_wol,
1589 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001590 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001591 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001592 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593};
1594
Francois Romieu07d3f512007-02-21 22:40:46 +01001595static void rtl8169_get_mac_version(struct rtl8169_private *tp,
1596 void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
Francois Romieu0e485152007-02-20 00:00:26 +01001598 /*
1599 * The driver currently handles the 8168Bf and the 8168Be identically
1600 * but they can be identified more specifically through the test below
1601 * if needed:
1602 *
1603 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001604 *
1605 * Same thing for the 8101Eb and the 8101Ec:
1606 *
1607 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001608 */
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001609 static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001611 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 int mac_version;
1613 } mac_info[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00001614 /* 8168E family. */
1615 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1616 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1617 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1618
Francois Romieu5b538df2008-07-20 16:22:45 +02001619 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001620 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1621 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001622 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001623
françois romieue6de30d2011-01-03 15:08:37 +00001624 /* 8168DP family. */
1625 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1626 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00001627 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00001628
Francois Romieuef808d52008-06-29 13:10:54 +02001629 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001630 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001631 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001632 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001633 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001634 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1635 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001636 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001637 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001638 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001639
1640 /* 8168B family. */
1641 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1642 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1643 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1644 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1645
1646 /* 8101 family. */
hayeswang36a0e6c2011-03-21 01:50:30 +00001647 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08001648 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1649 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1650 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001651 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1652 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1653 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1654 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1655 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1656 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001657 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001658 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001659 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001660 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1661 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001662 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1663 /* FIXME: where did these entries come from ? -- FR */
1664 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1665 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1666
1667 /* 8110 family. */
1668 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1669 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1670 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1671 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1672 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1673 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1674
Jean Delvaref21b75e2009-05-26 20:54:48 -07001675 /* Catch-all */
1676 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 }, *p = mac_info;
1678 u32 reg;
1679
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001680 reg = RTL_R32(TxConfig);
1681 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 p++;
1683 tp->mac_version = p->mac_version;
1684}
1685
1686static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1687{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001688 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689}
1690
Francois Romieu867763c2007-08-17 18:21:58 +02001691struct phy_reg {
1692 u16 reg;
1693 u16 val;
1694};
1695
françois romieu4da19632011-01-03 15:07:55 +00001696static void rtl_writephy_batch(struct rtl8169_private *tp,
1697 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02001698{
1699 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00001700 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02001701 regs++;
1702 }
1703}
1704
françois romieubca03d52011-01-03 15:07:31 +00001705#define PHY_READ 0x00000000
1706#define PHY_DATA_OR 0x10000000
1707#define PHY_DATA_AND 0x20000000
1708#define PHY_BJMPN 0x30000000
1709#define PHY_READ_EFUSE 0x40000000
1710#define PHY_READ_MAC_BYTE 0x50000000
1711#define PHY_WRITE_MAC_BYTE 0x60000000
1712#define PHY_CLEAR_READCOUNT 0x70000000
1713#define PHY_WRITE 0x80000000
1714#define PHY_READCOUNT_EQ_SKIP 0x90000000
1715#define PHY_COMP_EQ_SKIPN 0xa0000000
1716#define PHY_COMP_NEQ_SKIPN 0xb0000000
1717#define PHY_WRITE_PREVIOUS 0xc0000000
1718#define PHY_SKIPN 0xd0000000
1719#define PHY_DELAY_MS 0xe0000000
1720#define PHY_WRITE_ERI_WORD 0xf0000000
1721
1722static void
1723rtl_phy_write_fw(struct rtl8169_private *tp, const struct firmware *fw)
1724{
françois romieubca03d52011-01-03 15:07:31 +00001725 __le32 *phytable = (__le32 *)fw->data;
1726 struct net_device *dev = tp->dev;
hayeswang42b82dc2011-01-10 02:07:25 +00001727 size_t index, fw_size = fw->size / sizeof(*phytable);
1728 u32 predata, count;
françois romieubca03d52011-01-03 15:07:31 +00001729
1730 if (fw->size % sizeof(*phytable)) {
1731 netif_err(tp, probe, dev, "odd sized firmware %zd\n", fw->size);
1732 return;
1733 }
1734
hayeswang42b82dc2011-01-10 02:07:25 +00001735 for (index = 0; index < fw_size; index++) {
1736 u32 action = le32_to_cpu(phytable[index]);
1737 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00001738
hayeswang42b82dc2011-01-10 02:07:25 +00001739 switch(action & 0xf0000000) {
1740 case PHY_READ:
1741 case PHY_DATA_OR:
1742 case PHY_DATA_AND:
1743 case PHY_READ_EFUSE:
1744 case PHY_CLEAR_READCOUNT:
1745 case PHY_WRITE:
1746 case PHY_WRITE_PREVIOUS:
1747 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00001748 break;
1749
hayeswang42b82dc2011-01-10 02:07:25 +00001750 case PHY_BJMPN:
1751 if (regno > index) {
1752 netif_err(tp, probe, tp->dev,
1753 "Out of range of firmware\n");
1754 return;
1755 }
1756 break;
1757 case PHY_READCOUNT_EQ_SKIP:
1758 if (index + 2 >= fw_size) {
1759 netif_err(tp, probe, tp->dev,
1760 "Out of range of firmware\n");
1761 return;
1762 }
1763 break;
1764 case PHY_COMP_EQ_SKIPN:
1765 case PHY_COMP_NEQ_SKIPN:
1766 case PHY_SKIPN:
1767 if (index + 1 + regno >= fw_size) {
1768 netif_err(tp, probe, tp->dev,
1769 "Out of range of firmware\n");
1770 return;
1771 }
1772 break;
1773
1774 case PHY_READ_MAC_BYTE:
1775 case PHY_WRITE_MAC_BYTE:
1776 case PHY_WRITE_ERI_WORD:
1777 default:
1778 netif_err(tp, probe, tp->dev,
1779 "Invalid action 0x%08x\n", action);
françois romieubca03d52011-01-03 15:07:31 +00001780 return;
1781 }
1782 }
1783
hayeswang42b82dc2011-01-10 02:07:25 +00001784 predata = 0;
1785 count = 0;
1786
1787 for (index = 0; index < fw_size; ) {
1788 u32 action = le32_to_cpu(phytable[index]);
françois romieubca03d52011-01-03 15:07:31 +00001789 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00001790 u32 regno = (action & 0x0fff0000) >> 16;
1791
1792 if (!action)
1793 break;
françois romieubca03d52011-01-03 15:07:31 +00001794
1795 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00001796 case PHY_READ:
1797 predata = rtl_readphy(tp, regno);
1798 count++;
1799 index++;
françois romieubca03d52011-01-03 15:07:31 +00001800 break;
hayeswang42b82dc2011-01-10 02:07:25 +00001801 case PHY_DATA_OR:
1802 predata |= data;
1803 index++;
1804 break;
1805 case PHY_DATA_AND:
1806 predata &= data;
1807 index++;
1808 break;
1809 case PHY_BJMPN:
1810 index -= regno;
1811 break;
1812 case PHY_READ_EFUSE:
1813 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
1814 index++;
1815 break;
1816 case PHY_CLEAR_READCOUNT:
1817 count = 0;
1818 index++;
1819 break;
1820 case PHY_WRITE:
1821 rtl_writephy(tp, regno, data);
1822 index++;
1823 break;
1824 case PHY_READCOUNT_EQ_SKIP:
1825 if (count == data)
1826 index += 2;
1827 else
1828 index += 1;
1829 break;
1830 case PHY_COMP_EQ_SKIPN:
1831 if (predata == data)
1832 index += regno;
1833 index++;
1834 break;
1835 case PHY_COMP_NEQ_SKIPN:
1836 if (predata != data)
1837 index += regno;
1838 index++;
1839 break;
1840 case PHY_WRITE_PREVIOUS:
1841 rtl_writephy(tp, regno, predata);
1842 index++;
1843 break;
1844 case PHY_SKIPN:
1845 index += regno + 1;
1846 break;
1847 case PHY_DELAY_MS:
1848 mdelay(data);
1849 index++;
1850 break;
1851
1852 case PHY_READ_MAC_BYTE:
1853 case PHY_WRITE_MAC_BYTE:
1854 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00001855 default:
1856 BUG();
1857 }
1858 }
1859}
1860
françois romieuf1e02ed2011-01-13 13:07:53 +00001861static void rtl_release_firmware(struct rtl8169_private *tp)
1862{
François Romieu953a12c2011-04-24 17:38:48 +02001863 if (!IS_ERR_OR_NULL(tp->fw))
1864 release_firmware(tp->fw);
1865 tp->fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00001866}
1867
François Romieu953a12c2011-04-24 17:38:48 +02001868static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00001869{
François Romieu953a12c2011-04-24 17:38:48 +02001870 const struct firmware *fw = tp->fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00001871
1872 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
François Romieu953a12c2011-04-24 17:38:48 +02001873 if (!IS_ERR_OR_NULL(fw))
1874 rtl_phy_write_fw(tp, fw);
1875}
1876
1877static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
1878{
1879 if (rtl_readphy(tp, reg) != val)
1880 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
1881 else
1882 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00001883}
1884
françois romieu4da19632011-01-03 15:07:55 +00001885static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001887 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00001888 { 0x1f, 0x0001 },
1889 { 0x06, 0x006e },
1890 { 0x08, 0x0708 },
1891 { 0x15, 0x4000 },
1892 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
françois romieu0b9b5712009-08-10 19:44:56 +00001894 { 0x1f, 0x0001 },
1895 { 0x03, 0x00a1 },
1896 { 0x02, 0x0008 },
1897 { 0x01, 0x0120 },
1898 { 0x00, 0x1000 },
1899 { 0x04, 0x0800 },
1900 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
françois romieu0b9b5712009-08-10 19:44:56 +00001902 { 0x03, 0xff41 },
1903 { 0x02, 0xdf60 },
1904 { 0x01, 0x0140 },
1905 { 0x00, 0x0077 },
1906 { 0x04, 0x7800 },
1907 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
françois romieu0b9b5712009-08-10 19:44:56 +00001909 { 0x03, 0x802f },
1910 { 0x02, 0x4f02 },
1911 { 0x01, 0x0409 },
1912 { 0x00, 0xf0f9 },
1913 { 0x04, 0x9800 },
1914 { 0x04, 0x9000 },
1915
1916 { 0x03, 0xdf01 },
1917 { 0x02, 0xdf20 },
1918 { 0x01, 0xff95 },
1919 { 0x00, 0xba00 },
1920 { 0x04, 0xa800 },
1921 { 0x04, 0xa000 },
1922
1923 { 0x03, 0xff41 },
1924 { 0x02, 0xdf20 },
1925 { 0x01, 0x0140 },
1926 { 0x00, 0x00bb },
1927 { 0x04, 0xb800 },
1928 { 0x04, 0xb000 },
1929
1930 { 0x03, 0xdf41 },
1931 { 0x02, 0xdc60 },
1932 { 0x01, 0x6340 },
1933 { 0x00, 0x007d },
1934 { 0x04, 0xd800 },
1935 { 0x04, 0xd000 },
1936
1937 { 0x03, 0xdf01 },
1938 { 0x02, 0xdf20 },
1939 { 0x01, 0x100a },
1940 { 0x00, 0xa0ff },
1941 { 0x04, 0xf800 },
1942 { 0x04, 0xf000 },
1943
1944 { 0x1f, 0x0000 },
1945 { 0x0b, 0x0000 },
1946 { 0x00, 0x9200 }
1947 };
1948
françois romieu4da19632011-01-03 15:07:55 +00001949 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950}
1951
françois romieu4da19632011-01-03 15:07:55 +00001952static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02001953{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001954 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02001955 { 0x1f, 0x0002 },
1956 { 0x01, 0x90d0 },
1957 { 0x1f, 0x0000 }
1958 };
1959
françois romieu4da19632011-01-03 15:07:55 +00001960 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02001961}
1962
françois romieu4da19632011-01-03 15:07:55 +00001963static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00001964{
1965 struct pci_dev *pdev = tp->pci_dev;
1966 u16 vendor_id, device_id;
1967
1968 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &vendor_id);
1969 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &device_id);
1970
1971 if ((vendor_id != PCI_VENDOR_ID_GIGABYTE) || (device_id != 0xe000))
1972 return;
1973
françois romieu4da19632011-01-03 15:07:55 +00001974 rtl_writephy(tp, 0x1f, 0x0001);
1975 rtl_writephy(tp, 0x10, 0xf01b);
1976 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00001977}
1978
françois romieu4da19632011-01-03 15:07:55 +00001979static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00001980{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001981 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00001982 { 0x1f, 0x0001 },
1983 { 0x04, 0x0000 },
1984 { 0x03, 0x00a1 },
1985 { 0x02, 0x0008 },
1986 { 0x01, 0x0120 },
1987 { 0x00, 0x1000 },
1988 { 0x04, 0x0800 },
1989 { 0x04, 0x9000 },
1990 { 0x03, 0x802f },
1991 { 0x02, 0x4f02 },
1992 { 0x01, 0x0409 },
1993 { 0x00, 0xf099 },
1994 { 0x04, 0x9800 },
1995 { 0x04, 0xa000 },
1996 { 0x03, 0xdf01 },
1997 { 0x02, 0xdf20 },
1998 { 0x01, 0xff95 },
1999 { 0x00, 0xba00 },
2000 { 0x04, 0xa800 },
2001 { 0x04, 0xf000 },
2002 { 0x03, 0xdf01 },
2003 { 0x02, 0xdf20 },
2004 { 0x01, 0x101a },
2005 { 0x00, 0xa0ff },
2006 { 0x04, 0xf800 },
2007 { 0x04, 0x0000 },
2008 { 0x1f, 0x0000 },
2009
2010 { 0x1f, 0x0001 },
2011 { 0x10, 0xf41b },
2012 { 0x14, 0xfb54 },
2013 { 0x18, 0xf5c7 },
2014 { 0x1f, 0x0000 },
2015
2016 { 0x1f, 0x0001 },
2017 { 0x17, 0x0cc0 },
2018 { 0x1f, 0x0000 }
2019 };
2020
françois romieu4da19632011-01-03 15:07:55 +00002021 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002022
françois romieu4da19632011-01-03 15:07:55 +00002023 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002024}
2025
françois romieu4da19632011-01-03 15:07:55 +00002026static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002027{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002028 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002029 { 0x1f, 0x0001 },
2030 { 0x04, 0x0000 },
2031 { 0x03, 0x00a1 },
2032 { 0x02, 0x0008 },
2033 { 0x01, 0x0120 },
2034 { 0x00, 0x1000 },
2035 { 0x04, 0x0800 },
2036 { 0x04, 0x9000 },
2037 { 0x03, 0x802f },
2038 { 0x02, 0x4f02 },
2039 { 0x01, 0x0409 },
2040 { 0x00, 0xf099 },
2041 { 0x04, 0x9800 },
2042 { 0x04, 0xa000 },
2043 { 0x03, 0xdf01 },
2044 { 0x02, 0xdf20 },
2045 { 0x01, 0xff95 },
2046 { 0x00, 0xba00 },
2047 { 0x04, 0xa800 },
2048 { 0x04, 0xf000 },
2049 { 0x03, 0xdf01 },
2050 { 0x02, 0xdf20 },
2051 { 0x01, 0x101a },
2052 { 0x00, 0xa0ff },
2053 { 0x04, 0xf800 },
2054 { 0x04, 0x0000 },
2055 { 0x1f, 0x0000 },
2056
2057 { 0x1f, 0x0001 },
2058 { 0x0b, 0x8480 },
2059 { 0x1f, 0x0000 },
2060
2061 { 0x1f, 0x0001 },
2062 { 0x18, 0x67c7 },
2063 { 0x04, 0x2000 },
2064 { 0x03, 0x002f },
2065 { 0x02, 0x4360 },
2066 { 0x01, 0x0109 },
2067 { 0x00, 0x3022 },
2068 { 0x04, 0x2800 },
2069 { 0x1f, 0x0000 },
2070
2071 { 0x1f, 0x0001 },
2072 { 0x17, 0x0cc0 },
2073 { 0x1f, 0x0000 }
2074 };
2075
françois romieu4da19632011-01-03 15:07:55 +00002076 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002077}
2078
françois romieu4da19632011-01-03 15:07:55 +00002079static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002080{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002081 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002082 { 0x10, 0xf41b },
2083 { 0x1f, 0x0000 }
2084 };
2085
françois romieu4da19632011-01-03 15:07:55 +00002086 rtl_writephy(tp, 0x1f, 0x0001);
2087 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002088
françois romieu4da19632011-01-03 15:07:55 +00002089 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002090}
2091
françois romieu4da19632011-01-03 15:07:55 +00002092static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002093{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002094 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002095 { 0x1f, 0x0001 },
2096 { 0x10, 0xf41b },
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));
Francois Romieu236b8082008-05-30 16:11:48 +02002101}
2102
françois romieu4da19632011-01-03 15:07:55 +00002103static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002104{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002105 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002106 { 0x1f, 0x0000 },
2107 { 0x1d, 0x0f00 },
2108 { 0x1f, 0x0002 },
2109 { 0x0c, 0x1ec8 },
2110 { 0x1f, 0x0000 }
2111 };
2112
françois romieu4da19632011-01-03 15:07:55 +00002113 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002114}
2115
françois romieu4da19632011-01-03 15:07:55 +00002116static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002117{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002118 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002119 { 0x1f, 0x0001 },
2120 { 0x1d, 0x3d98 },
2121 { 0x1f, 0x0000 }
2122 };
2123
françois romieu4da19632011-01-03 15:07:55 +00002124 rtl_writephy(tp, 0x1f, 0x0000);
2125 rtl_patchphy(tp, 0x14, 1 << 5);
2126 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002127
françois romieu4da19632011-01-03 15:07:55 +00002128 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002129}
2130
françois romieu4da19632011-01-03 15:07:55 +00002131static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002132{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002133 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002134 { 0x1f, 0x0001 },
2135 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002136 { 0x1f, 0x0002 },
2137 { 0x00, 0x88d4 },
2138 { 0x01, 0x82b1 },
2139 { 0x03, 0x7002 },
2140 { 0x08, 0x9e30 },
2141 { 0x09, 0x01f0 },
2142 { 0x0a, 0x5500 },
2143 { 0x0c, 0x00c8 },
2144 { 0x1f, 0x0003 },
2145 { 0x12, 0xc096 },
2146 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002147 { 0x1f, 0x0000 },
2148 { 0x1f, 0x0000 },
2149 { 0x09, 0x2000 },
2150 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002151 };
2152
françois romieu4da19632011-01-03 15:07:55 +00002153 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002154
françois romieu4da19632011-01-03 15:07:55 +00002155 rtl_patchphy(tp, 0x14, 1 << 5);
2156 rtl_patchphy(tp, 0x0d, 1 << 5);
2157 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002158}
2159
françois romieu4da19632011-01-03 15:07:55 +00002160static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002161{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002162 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002163 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002164 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002165 { 0x03, 0x802f },
2166 { 0x02, 0x4f02 },
2167 { 0x01, 0x0409 },
2168 { 0x00, 0xf099 },
2169 { 0x04, 0x9800 },
2170 { 0x04, 0x9000 },
2171 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002172 { 0x1f, 0x0002 },
2173 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002174 { 0x06, 0x0761 },
2175 { 0x1f, 0x0003 },
2176 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002177 { 0x1f, 0x0000 }
2178 };
2179
françois romieu4da19632011-01-03 15:07:55 +00002180 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002181
françois romieu4da19632011-01-03 15:07:55 +00002182 rtl_patchphy(tp, 0x16, 1 << 0);
2183 rtl_patchphy(tp, 0x14, 1 << 5);
2184 rtl_patchphy(tp, 0x0d, 1 << 5);
2185 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002186}
2187
françois romieu4da19632011-01-03 15:07:55 +00002188static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002189{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002190 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002191 { 0x1f, 0x0001 },
2192 { 0x12, 0x2300 },
2193 { 0x1d, 0x3d98 },
2194 { 0x1f, 0x0002 },
2195 { 0x0c, 0x7eb8 },
2196 { 0x06, 0x5461 },
2197 { 0x1f, 0x0003 },
2198 { 0x16, 0x0f0a },
2199 { 0x1f, 0x0000 }
2200 };
2201
françois romieu4da19632011-01-03 15:07:55 +00002202 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002203
françois romieu4da19632011-01-03 15:07:55 +00002204 rtl_patchphy(tp, 0x16, 1 << 0);
2205 rtl_patchphy(tp, 0x14, 1 << 5);
2206 rtl_patchphy(tp, 0x0d, 1 << 5);
2207 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002208}
2209
françois romieu4da19632011-01-03 15:07:55 +00002210static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002211{
françois romieu4da19632011-01-03 15:07:55 +00002212 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002213}
2214
françois romieubca03d52011-01-03 15:07:31 +00002215static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002216{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002217 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002218 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002219 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002220 { 0x06, 0x4064 },
2221 { 0x07, 0x2863 },
2222 { 0x08, 0x059c },
2223 { 0x09, 0x26b4 },
2224 { 0x0a, 0x6a19 },
2225 { 0x0b, 0xdcc8 },
2226 { 0x10, 0xf06d },
2227 { 0x14, 0x7f68 },
2228 { 0x18, 0x7fd9 },
2229 { 0x1c, 0xf0ff },
2230 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002231 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002232 { 0x12, 0xf49f },
2233 { 0x13, 0x070b },
2234 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002235 { 0x14, 0x94c0 },
2236
2237 /*
2238 * Tx Error Issue
2239 * enhance line driver power
2240 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002241 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002242 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002243 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002244 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002245 { 0x06, 0x5561 },
2246
2247 /*
2248 * Can not link to 1Gbps with bad cable
2249 * Decrease SNR threshold form 21.07dB to 19.04dB
2250 */
2251 { 0x1f, 0x0001 },
2252 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002253
2254 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002255 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002256 };
françois romieubca03d52011-01-03 15:07:31 +00002257 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002258
françois romieu4da19632011-01-03 15:07:55 +00002259 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002260
françois romieubca03d52011-01-03 15:07:31 +00002261 /*
2262 * Rx Error Issue
2263 * Fine Tune Switching regulator parameter
2264 */
françois romieu4da19632011-01-03 15:07:55 +00002265 rtl_writephy(tp, 0x1f, 0x0002);
2266 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2267 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002268
françois romieudaf9df62009-10-07 12:44:20 +00002269 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002270 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002271 { 0x1f, 0x0002 },
2272 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002273 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002274 { 0x05, 0x8330 },
2275 { 0x06, 0x669a },
2276 { 0x1f, 0x0002 }
2277 };
2278 int val;
2279
françois romieu4da19632011-01-03 15:07:55 +00002280 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002281
françois romieu4da19632011-01-03 15:07:55 +00002282 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002283
2284 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002285 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002286 0x0065, 0x0066, 0x0067, 0x0068,
2287 0x0069, 0x006a, 0x006b, 0x006c
2288 };
2289 int i;
2290
françois romieu4da19632011-01-03 15:07:55 +00002291 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002292
2293 val &= 0xff00;
2294 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002295 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002296 }
2297 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002298 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002299 { 0x1f, 0x0002 },
2300 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002301 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002302 { 0x05, 0x8330 },
2303 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002304 };
2305
françois romieu4da19632011-01-03 15:07:55 +00002306 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002307 }
2308
françois romieubca03d52011-01-03 15:07:31 +00002309 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002310 rtl_writephy(tp, 0x1f, 0x0002);
2311 rtl_patchphy(tp, 0x0d, 0x0300);
2312 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002313
françois romieubca03d52011-01-03 15:07:31 +00002314 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002315 rtl_writephy(tp, 0x1f, 0x0002);
2316 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2317 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002318
françois romieu4da19632011-01-03 15:07:55 +00002319 rtl_writephy(tp, 0x1f, 0x0005);
2320 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002321
2322 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002323
françois romieu4da19632011-01-03 15:07:55 +00002324 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002325}
2326
françois romieubca03d52011-01-03 15:07:31 +00002327static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002328{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002329 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002330 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002331 { 0x1f, 0x0001 },
2332 { 0x06, 0x4064 },
2333 { 0x07, 0x2863 },
2334 { 0x08, 0x059c },
2335 { 0x09, 0x26b4 },
2336 { 0x0a, 0x6a19 },
2337 { 0x0b, 0xdcc8 },
2338 { 0x10, 0xf06d },
2339 { 0x14, 0x7f68 },
2340 { 0x18, 0x7fd9 },
2341 { 0x1c, 0xf0ff },
2342 { 0x1d, 0x3d9c },
2343 { 0x1f, 0x0003 },
2344 { 0x12, 0xf49f },
2345 { 0x13, 0x070b },
2346 { 0x1a, 0x05ad },
2347 { 0x14, 0x94c0 },
2348
françois romieubca03d52011-01-03 15:07:31 +00002349 /*
2350 * Tx Error Issue
2351 * enhance line driver power
2352 */
françois romieudaf9df62009-10-07 12:44:20 +00002353 { 0x1f, 0x0002 },
2354 { 0x06, 0x5561 },
2355 { 0x1f, 0x0005 },
2356 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002357 { 0x06, 0x5561 },
2358
2359 /*
2360 * Can not link to 1Gbps with bad cable
2361 * Decrease SNR threshold form 21.07dB to 19.04dB
2362 */
2363 { 0x1f, 0x0001 },
2364 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002365
2366 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002367 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002368 };
françois romieubca03d52011-01-03 15:07:31 +00002369 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002370
françois romieu4da19632011-01-03 15:07:55 +00002371 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002372
2373 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002374 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002375 { 0x1f, 0x0002 },
2376 { 0x05, 0x669a },
2377 { 0x1f, 0x0005 },
2378 { 0x05, 0x8330 },
2379 { 0x06, 0x669a },
2380
2381 { 0x1f, 0x0002 }
2382 };
2383 int val;
2384
françois romieu4da19632011-01-03 15:07:55 +00002385 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002386
françois romieu4da19632011-01-03 15:07:55 +00002387 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002388 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002389 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002390 0x0065, 0x0066, 0x0067, 0x0068,
2391 0x0069, 0x006a, 0x006b, 0x006c
2392 };
2393 int i;
2394
françois romieu4da19632011-01-03 15:07:55 +00002395 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002396
2397 val &= 0xff00;
2398 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002399 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002400 }
2401 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002402 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002403 { 0x1f, 0x0002 },
2404 { 0x05, 0x2642 },
2405 { 0x1f, 0x0005 },
2406 { 0x05, 0x8330 },
2407 { 0x06, 0x2642 }
2408 };
2409
françois romieu4da19632011-01-03 15:07:55 +00002410 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002411 }
2412
françois romieubca03d52011-01-03 15:07:31 +00002413 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002414 rtl_writephy(tp, 0x1f, 0x0002);
2415 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2416 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002417
françois romieubca03d52011-01-03 15:07:31 +00002418 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002419 rtl_writephy(tp, 0x1f, 0x0002);
2420 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002421
françois romieu4da19632011-01-03 15:07:55 +00002422 rtl_writephy(tp, 0x1f, 0x0005);
2423 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002424
2425 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002426
françois romieu4da19632011-01-03 15:07:55 +00002427 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002428}
2429
françois romieu4da19632011-01-03 15:07:55 +00002430static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002431{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002432 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002433 { 0x1f, 0x0002 },
2434 { 0x10, 0x0008 },
2435 { 0x0d, 0x006c },
2436
2437 { 0x1f, 0x0000 },
2438 { 0x0d, 0xf880 },
2439
2440 { 0x1f, 0x0001 },
2441 { 0x17, 0x0cc0 },
2442
2443 { 0x1f, 0x0001 },
2444 { 0x0b, 0xa4d8 },
2445 { 0x09, 0x281c },
2446 { 0x07, 0x2883 },
2447 { 0x0a, 0x6b35 },
2448 { 0x1d, 0x3da4 },
2449 { 0x1c, 0xeffd },
2450 { 0x14, 0x7f52 },
2451 { 0x18, 0x7fc6 },
2452 { 0x08, 0x0601 },
2453 { 0x06, 0x4063 },
2454 { 0x10, 0xf074 },
2455 { 0x1f, 0x0003 },
2456 { 0x13, 0x0789 },
2457 { 0x12, 0xf4bd },
2458 { 0x1a, 0x04fd },
2459 { 0x14, 0x84b0 },
2460 { 0x1f, 0x0000 },
2461 { 0x00, 0x9200 },
2462
2463 { 0x1f, 0x0005 },
2464 { 0x01, 0x0340 },
2465 { 0x1f, 0x0001 },
2466 { 0x04, 0x4000 },
2467 { 0x03, 0x1d21 },
2468 { 0x02, 0x0c32 },
2469 { 0x01, 0x0200 },
2470 { 0x00, 0x5554 },
2471 { 0x04, 0x4800 },
2472 { 0x04, 0x4000 },
2473 { 0x04, 0xf000 },
2474 { 0x03, 0xdf01 },
2475 { 0x02, 0xdf20 },
2476 { 0x01, 0x101a },
2477 { 0x00, 0xa0ff },
2478 { 0x04, 0xf800 },
2479 { 0x04, 0xf000 },
2480 { 0x1f, 0x0000 },
2481
2482 { 0x1f, 0x0007 },
2483 { 0x1e, 0x0023 },
2484 { 0x16, 0x0000 },
2485 { 0x1f, 0x0000 }
2486 };
2487
françois romieu4da19632011-01-03 15:07:55 +00002488 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002489}
2490
françois romieue6de30d2011-01-03 15:08:37 +00002491static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2492{
2493 static const struct phy_reg phy_reg_init[] = {
2494 { 0x1f, 0x0001 },
2495 { 0x17, 0x0cc0 },
2496
2497 { 0x1f, 0x0007 },
2498 { 0x1e, 0x002d },
2499 { 0x18, 0x0040 },
2500 { 0x1f, 0x0000 }
2501 };
2502
2503 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2504 rtl_patchphy(tp, 0x0d, 1 << 5);
2505}
2506
hayeswang01dc7fe2011-03-21 01:50:28 +00002507static void rtl8168e_hw_phy_config(struct rtl8169_private *tp)
2508{
2509 static const struct phy_reg phy_reg_init[] = {
2510 /* Enable Delay cap */
2511 { 0x1f, 0x0005 },
2512 { 0x05, 0x8b80 },
2513 { 0x06, 0xc896 },
2514 { 0x1f, 0x0000 },
2515
2516 /* Channel estimation fine tune */
2517 { 0x1f, 0x0001 },
2518 { 0x0b, 0x6c20 },
2519 { 0x07, 0x2872 },
2520 { 0x1c, 0xefff },
2521 { 0x1f, 0x0003 },
2522 { 0x14, 0x6420 },
2523 { 0x1f, 0x0000 },
2524
2525 /* Update PFM & 10M TX idle timer */
2526 { 0x1f, 0x0007 },
2527 { 0x1e, 0x002f },
2528 { 0x15, 0x1919 },
2529 { 0x1f, 0x0000 },
2530
2531 { 0x1f, 0x0007 },
2532 { 0x1e, 0x00ac },
2533 { 0x18, 0x0006 },
2534 { 0x1f, 0x0000 }
2535 };
2536
Francois Romieu15ecd032011-04-27 13:52:22 -07002537 rtl_apply_firmware(tp);
2538
hayeswang01dc7fe2011-03-21 01:50:28 +00002539 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2540
2541 /* DCO enable for 10M IDLE Power */
2542 rtl_writephy(tp, 0x1f, 0x0007);
2543 rtl_writephy(tp, 0x1e, 0x0023);
2544 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2545 rtl_writephy(tp, 0x1f, 0x0000);
2546
2547 /* For impedance matching */
2548 rtl_writephy(tp, 0x1f, 0x0002);
2549 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
2550 rtl_writephy(tp, 0x1F, 0x0000);
2551
2552 /* PHY auto speed down */
2553 rtl_writephy(tp, 0x1f, 0x0007);
2554 rtl_writephy(tp, 0x1e, 0x002d);
2555 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2556 rtl_writephy(tp, 0x1f, 0x0000);
2557 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2558
2559 rtl_writephy(tp, 0x1f, 0x0005);
2560 rtl_writephy(tp, 0x05, 0x8b86);
2561 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2562 rtl_writephy(tp, 0x1f, 0x0000);
2563
2564 rtl_writephy(tp, 0x1f, 0x0005);
2565 rtl_writephy(tp, 0x05, 0x8b85);
2566 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2567 rtl_writephy(tp, 0x1f, 0x0007);
2568 rtl_writephy(tp, 0x1e, 0x0020);
2569 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2570 rtl_writephy(tp, 0x1f, 0x0006);
2571 rtl_writephy(tp, 0x00, 0x5a00);
2572 rtl_writephy(tp, 0x1f, 0x0000);
2573 rtl_writephy(tp, 0x0d, 0x0007);
2574 rtl_writephy(tp, 0x0e, 0x003c);
2575 rtl_writephy(tp, 0x0d, 0x4007);
2576 rtl_writephy(tp, 0x0e, 0x0000);
2577 rtl_writephy(tp, 0x0d, 0x0000);
2578}
2579
françois romieu4da19632011-01-03 15:07:55 +00002580static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02002581{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002582 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02002583 { 0x1f, 0x0003 },
2584 { 0x08, 0x441d },
2585 { 0x01, 0x9100 },
2586 { 0x1f, 0x0000 }
2587 };
2588
françois romieu4da19632011-01-03 15:07:55 +00002589 rtl_writephy(tp, 0x1f, 0x0000);
2590 rtl_patchphy(tp, 0x11, 1 << 12);
2591 rtl_patchphy(tp, 0x19, 1 << 13);
2592 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002593
françois romieu4da19632011-01-03 15:07:55 +00002594 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02002595}
2596
Hayes Wang5a5e4442011-02-22 17:26:21 +08002597static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
2598{
2599 static const struct phy_reg phy_reg_init[] = {
2600 { 0x1f, 0x0005 },
2601 { 0x1a, 0x0000 },
2602 { 0x1f, 0x0000 },
2603
2604 { 0x1f, 0x0004 },
2605 { 0x1c, 0x0000 },
2606 { 0x1f, 0x0000 },
2607
2608 { 0x1f, 0x0001 },
2609 { 0x15, 0x7701 },
2610 { 0x1f, 0x0000 }
2611 };
2612
2613 /* Disable ALDPS before ram code */
2614 rtl_writephy(tp, 0x1f, 0x0000);
2615 rtl_writephy(tp, 0x18, 0x0310);
2616 msleep(100);
2617
François Romieu953a12c2011-04-24 17:38:48 +02002618 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08002619
2620 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2621}
2622
Francois Romieu5615d9f2007-08-17 17:50:46 +02002623static void rtl_hw_phy_config(struct net_device *dev)
2624{
2625 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002626
2627 rtl8169_print_mac_version(tp);
2628
2629 switch (tp->mac_version) {
2630 case RTL_GIGA_MAC_VER_01:
2631 break;
2632 case RTL_GIGA_MAC_VER_02:
2633 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00002634 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002635 break;
2636 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00002637 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002638 break;
françois romieu2e9558562009-08-10 19:44:19 +00002639 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00002640 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002641 break;
françois romieu8c7006a2009-08-10 19:43:29 +00002642 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00002643 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00002644 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02002645 case RTL_GIGA_MAC_VER_07:
2646 case RTL_GIGA_MAC_VER_08:
2647 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00002648 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002649 break;
Francois Romieu236b8082008-05-30 16:11:48 +02002650 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00002651 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002652 break;
2653 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00002654 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002655 break;
2656 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00002657 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002658 break;
Francois Romieu867763c2007-08-17 18:21:58 +02002659 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00002660 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002661 break;
2662 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00002663 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002664 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02002665 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00002666 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002667 break;
Francois Romieu197ff762008-06-28 13:16:02 +02002668 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00002669 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02002670 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02002671 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00002672 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002673 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02002674 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002675 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00002676 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02002677 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02002678 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00002679 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002680 break;
2681 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00002682 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002683 break;
2684 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00002685 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02002686 break;
françois romieue6de30d2011-01-03 15:08:37 +00002687 case RTL_GIGA_MAC_VER_28:
2688 rtl8168d_4_hw_phy_config(tp);
2689 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08002690 case RTL_GIGA_MAC_VER_29:
2691 case RTL_GIGA_MAC_VER_30:
2692 rtl8105e_hw_phy_config(tp);
2693 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00002694 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00002695 case RTL_GIGA_MAC_VER_33:
Francois Romieu15ecd032011-04-27 13:52:22 -07002696 rtl8168e_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00002697 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02002698
Francois Romieu5615d9f2007-08-17 17:50:46 +02002699 default:
2700 break;
2701 }
2702}
2703
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704static void rtl8169_phy_timer(unsigned long __opaque)
2705{
2706 struct net_device *dev = (struct net_device *)__opaque;
2707 struct rtl8169_private *tp = netdev_priv(dev);
2708 struct timer_list *timer = &tp->timer;
2709 void __iomem *ioaddr = tp->mmio_addr;
2710 unsigned long timeout = RTL8169_PHY_TIMEOUT;
2711
Francois Romieubcf0bf92006-07-26 23:14:13 +02002712 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
Francois Romieu64e4bfb2006-08-17 12:43:06 +02002714 if (!(tp->phy_1000_ctrl_reg & ADVERTISE_1000FULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 return;
2716
2717 spin_lock_irq(&tp->lock);
2718
françois romieu4da19632011-01-03 15:07:55 +00002719 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02002720 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 * A busy loop could burn quite a few cycles on nowadays CPU.
2722 * Let's delay the execution of the timer for a few ticks.
2723 */
2724 timeout = HZ/10;
2725 goto out_mod_timer;
2726 }
2727
2728 if (tp->link_ok(ioaddr))
2729 goto out_unlock;
2730
Joe Perchesbf82c182010-02-09 11:49:50 +00002731 netif_warn(tp, link, dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732
françois romieu4da19632011-01-03 15:07:55 +00002733 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
2735out_mod_timer:
2736 mod_timer(timer, jiffies + timeout);
2737out_unlock:
2738 spin_unlock_irq(&tp->lock);
2739}
2740
2741static inline void rtl8169_delete_timer(struct net_device *dev)
2742{
2743 struct rtl8169_private *tp = netdev_priv(dev);
2744 struct timer_list *timer = &tp->timer;
2745
Francois Romieue179bb72007-08-17 15:05:21 +02002746 if (tp->mac_version <= RTL_GIGA_MAC_VER_01)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 return;
2748
2749 del_timer_sync(timer);
2750}
2751
2752static inline void rtl8169_request_timer(struct net_device *dev)
2753{
2754 struct rtl8169_private *tp = netdev_priv(dev);
2755 struct timer_list *timer = &tp->timer;
2756
Francois Romieue179bb72007-08-17 15:05:21 +02002757 if (tp->mac_version <= RTL_GIGA_MAC_VER_01)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 return;
2759
Francois Romieu2efa53f2007-03-09 00:00:05 +01002760 mod_timer(timer, jiffies + RTL8169_PHY_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761}
2762
2763#ifdef CONFIG_NET_POLL_CONTROLLER
2764/*
2765 * Polling 'interrupt' - used by things like netconsole to send skbs
2766 * without having to re-enable interrupts. It's not called while
2767 * the interrupt routine is executing.
2768 */
2769static void rtl8169_netpoll(struct net_device *dev)
2770{
2771 struct rtl8169_private *tp = netdev_priv(dev);
2772 struct pci_dev *pdev = tp->pci_dev;
2773
2774 disable_irq(pdev->irq);
David Howells7d12e782006-10-05 14:55:46 +01002775 rtl8169_interrupt(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 enable_irq(pdev->irq);
2777}
2778#endif
2779
2780static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
2781 void __iomem *ioaddr)
2782{
2783 iounmap(ioaddr);
2784 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00002785 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 pci_disable_device(pdev);
2787 free_netdev(dev);
2788}
2789
Francois Romieubf793292006-11-01 00:53:05 +01002790static void rtl8169_phy_reset(struct net_device *dev,
2791 struct rtl8169_private *tp)
2792{
Francois Romieu07d3f512007-02-21 22:40:46 +01002793 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01002794
françois romieu4da19632011-01-03 15:07:55 +00002795 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01002796 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00002797 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01002798 return;
2799 msleep(1);
2800 }
Joe Perchesbf82c182010-02-09 11:49:50 +00002801 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01002802}
2803
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002804static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002806 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002807
Francois Romieu5615d9f2007-08-17 17:50:46 +02002808 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002809
Marcus Sundberg773328942008-07-10 21:28:08 +02002810 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
2811 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2812 RTL_W8(0x82, 0x01);
2813 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002814
Francois Romieu6dccd162007-02-13 23:38:05 +01002815 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
2816
2817 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
2818 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002819
Francois Romieubcf0bf92006-07-26 23:14:13 +02002820 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002821 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2822 RTL_W8(0x82, 0x01);
2823 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00002824 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002825 }
2826
Francois Romieubf793292006-11-01 00:53:05 +01002827 rtl8169_phy_reset(dev, tp);
2828
Oliver Neukum54405cd2011-01-06 21:55:13 +01002829 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
2830 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
2831 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
Eric Dumazetdb552b32011-03-19 16:39:11 +01002832 (tp->mii.supports_gmii ?
Oliver Neukum54405cd2011-01-06 21:55:13 +01002833 ADVERTISED_1000baseT_Half |
Eric Dumazetdb552b32011-03-19 16:39:11 +01002834 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002835
Joe Perchesbf82c182010-02-09 11:49:50 +00002836 if (RTL_R8(PHYstatus) & TBI_Enable)
2837 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002838}
2839
Francois Romieu773d2022007-01-31 23:47:43 +01002840static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
2841{
2842 void __iomem *ioaddr = tp->mmio_addr;
2843 u32 high;
2844 u32 low;
2845
2846 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
2847 high = addr[4] | (addr[5] << 8);
2848
2849 spin_lock_irq(&tp->lock);
2850
2851 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00002852
Francois Romieu773d2022007-01-31 23:47:43 +01002853 RTL_W32(MAC4, high);
françois romieu908ba2b2010-04-26 11:42:58 +00002854 RTL_R32(MAC4);
2855
Francois Romieu78f1cd02010-03-27 19:35:46 -07002856 RTL_W32(MAC0, low);
françois romieu908ba2b2010-04-26 11:42:58 +00002857 RTL_R32(MAC0);
2858
Francois Romieu773d2022007-01-31 23:47:43 +01002859 RTL_W8(Cfg9346, Cfg9346_Lock);
2860
2861 spin_unlock_irq(&tp->lock);
2862}
2863
2864static int rtl_set_mac_address(struct net_device *dev, void *p)
2865{
2866 struct rtl8169_private *tp = netdev_priv(dev);
2867 struct sockaddr *addr = p;
2868
2869 if (!is_valid_ether_addr(addr->sa_data))
2870 return -EADDRNOTAVAIL;
2871
2872 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
2873
2874 rtl_rar_set(tp, dev->dev_addr);
2875
2876 return 0;
2877}
2878
Francois Romieu5f787a12006-08-17 13:02:36 +02002879static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2880{
2881 struct rtl8169_private *tp = netdev_priv(dev);
2882 struct mii_ioctl_data *data = if_mii(ifr);
2883
Francois Romieu8b4ab282008-11-19 22:05:25 -08002884 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
2885}
Francois Romieu5f787a12006-08-17 13:02:36 +02002886
Francois Romieu8b4ab282008-11-19 22:05:25 -08002887static int rtl_xmii_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
2888{
Francois Romieu5f787a12006-08-17 13:02:36 +02002889 switch (cmd) {
2890 case SIOCGMIIPHY:
2891 data->phy_id = 32; /* Internal PHY */
2892 return 0;
2893
2894 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00002895 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02002896 return 0;
2897
2898 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00002899 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02002900 return 0;
2901 }
2902 return -EOPNOTSUPP;
2903}
2904
Francois Romieu8b4ab282008-11-19 22:05:25 -08002905static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
2906{
2907 return -EOPNOTSUPP;
2908}
2909
Francois Romieu0e485152007-02-20 00:00:26 +01002910static const struct rtl_cfg_info {
2911 void (*hw_start)(struct net_device *);
2912 unsigned int region;
2913 unsigned int align;
2914 u16 intr_event;
2915 u16 napi_event;
Francois Romieuccdffb92008-07-26 14:26:06 +02002916 unsigned features;
Jean Delvaref21b75e2009-05-26 20:54:48 -07002917 u8 default_ver;
Francois Romieu0e485152007-02-20 00:00:26 +01002918} rtl_cfg_infos [] = {
2919 [RTL_CFG_0] = {
2920 .hw_start = rtl_hw_start_8169,
2921 .region = 1,
Francois Romieue9f63f32007-02-28 23:16:57 +01002922 .align = 0,
Francois Romieu0e485152007-02-20 00:00:26 +01002923 .intr_event = SYSErr | LinkChg | RxOverflow |
2924 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02002925 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07002926 .features = RTL_FEATURE_GMII,
2927 .default_ver = RTL_GIGA_MAC_VER_01,
Francois Romieu0e485152007-02-20 00:00:26 +01002928 },
2929 [RTL_CFG_1] = {
2930 .hw_start = rtl_hw_start_8168,
2931 .region = 2,
2932 .align = 8,
françois romieu53f57352010-11-08 13:23:05 +00002933 .intr_event = SYSErr | LinkChg | RxOverflow |
Francois Romieu0e485152007-02-20 00:00:26 +01002934 TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02002935 .napi_event = TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07002936 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
2937 .default_ver = RTL_GIGA_MAC_VER_11,
Francois Romieu0e485152007-02-20 00:00:26 +01002938 },
2939 [RTL_CFG_2] = {
2940 .hw_start = rtl_hw_start_8101,
2941 .region = 2,
2942 .align = 8,
2943 .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
2944 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02002945 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07002946 .features = RTL_FEATURE_MSI,
2947 .default_ver = RTL_GIGA_MAC_VER_13,
Francois Romieu0e485152007-02-20 00:00:26 +01002948 }
2949};
2950
Francois Romieufbac58f2007-10-04 22:51:38 +02002951/* Cfg9346_Unlock assumed. */
2952static unsigned rtl_try_msi(struct pci_dev *pdev, void __iomem *ioaddr,
2953 const struct rtl_cfg_info *cfg)
2954{
2955 unsigned msi = 0;
2956 u8 cfg2;
2957
2958 cfg2 = RTL_R8(Config2) & ~MSIEnable;
Francois Romieuccdffb92008-07-26 14:26:06 +02002959 if (cfg->features & RTL_FEATURE_MSI) {
Francois Romieufbac58f2007-10-04 22:51:38 +02002960 if (pci_enable_msi(pdev)) {
2961 dev_info(&pdev->dev, "no MSI. Back to INTx.\n");
2962 } else {
2963 cfg2 |= MSIEnable;
2964 msi = RTL_FEATURE_MSI;
2965 }
2966 }
2967 RTL_W8(Config2, cfg2);
2968 return msi;
2969}
2970
2971static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
2972{
2973 if (tp->features & RTL_FEATURE_MSI) {
2974 pci_disable_msi(pdev);
2975 tp->features &= ~RTL_FEATURE_MSI;
2976 }
2977}
2978
Francois Romieu8b4ab282008-11-19 22:05:25 -08002979static const struct net_device_ops rtl8169_netdev_ops = {
2980 .ndo_open = rtl8169_open,
2981 .ndo_stop = rtl8169_close,
2982 .ndo_get_stats = rtl8169_get_stats,
Stephen Hemminger00829822008-11-20 20:14:53 -08002983 .ndo_start_xmit = rtl8169_start_xmit,
Francois Romieu8b4ab282008-11-19 22:05:25 -08002984 .ndo_tx_timeout = rtl8169_tx_timeout,
2985 .ndo_validate_addr = eth_validate_addr,
2986 .ndo_change_mtu = rtl8169_change_mtu,
Michał Mirosław350fb322011-04-08 06:35:56 +00002987 .ndo_fix_features = rtl8169_fix_features,
2988 .ndo_set_features = rtl8169_set_features,
Francois Romieu8b4ab282008-11-19 22:05:25 -08002989 .ndo_set_mac_address = rtl_set_mac_address,
2990 .ndo_do_ioctl = rtl8169_ioctl,
2991 .ndo_set_multicast_list = rtl_set_rx_mode,
Francois Romieu8b4ab282008-11-19 22:05:25 -08002992#ifdef CONFIG_NET_POLL_CONTROLLER
2993 .ndo_poll_controller = rtl8169_netpoll,
2994#endif
2995
2996};
2997
françois romieuc0e45c12011-01-03 15:08:04 +00002998static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
2999{
3000 struct mdio_ops *ops = &tp->mdio_ops;
3001
3002 switch (tp->mac_version) {
3003 case RTL_GIGA_MAC_VER_27:
3004 ops->write = r8168dp_1_mdio_write;
3005 ops->read = r8168dp_1_mdio_read;
3006 break;
françois romieue6de30d2011-01-03 15:08:37 +00003007 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003008 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003009 ops->write = r8168dp_2_mdio_write;
3010 ops->read = r8168dp_2_mdio_read;
3011 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003012 default:
3013 ops->write = r8169_mdio_write;
3014 ops->read = r8169_mdio_read;
3015 break;
3016 }
3017}
3018
françois romieu065c27c2011-01-03 15:08:12 +00003019static void r810x_phy_power_down(struct rtl8169_private *tp)
3020{
3021 rtl_writephy(tp, 0x1f, 0x0000);
3022 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3023}
3024
3025static void r810x_phy_power_up(struct rtl8169_private *tp)
3026{
3027 rtl_writephy(tp, 0x1f, 0x0000);
3028 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3029}
3030
3031static void r810x_pll_power_down(struct rtl8169_private *tp)
3032{
3033 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3034 rtl_writephy(tp, 0x1f, 0x0000);
3035 rtl_writephy(tp, MII_BMCR, 0x0000);
3036 return;
3037 }
3038
3039 r810x_phy_power_down(tp);
3040}
3041
3042static void r810x_pll_power_up(struct rtl8169_private *tp)
3043{
3044 r810x_phy_power_up(tp);
3045}
3046
3047static void r8168_phy_power_up(struct rtl8169_private *tp)
3048{
3049 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003050 switch (tp->mac_version) {
3051 case RTL_GIGA_MAC_VER_11:
3052 case RTL_GIGA_MAC_VER_12:
3053 case RTL_GIGA_MAC_VER_17:
3054 case RTL_GIGA_MAC_VER_18:
3055 case RTL_GIGA_MAC_VER_19:
3056 case RTL_GIGA_MAC_VER_20:
3057 case RTL_GIGA_MAC_VER_21:
3058 case RTL_GIGA_MAC_VER_22:
3059 case RTL_GIGA_MAC_VER_23:
3060 case RTL_GIGA_MAC_VER_24:
3061 case RTL_GIGA_MAC_VER_25:
3062 case RTL_GIGA_MAC_VER_26:
3063 case RTL_GIGA_MAC_VER_27:
3064 case RTL_GIGA_MAC_VER_28:
3065 case RTL_GIGA_MAC_VER_31:
3066 rtl_writephy(tp, 0x0e, 0x0000);
3067 break;
3068 default:
3069 break;
3070 }
françois romieu065c27c2011-01-03 15:08:12 +00003071 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3072}
3073
3074static void r8168_phy_power_down(struct rtl8169_private *tp)
3075{
3076 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003077 switch (tp->mac_version) {
3078 case RTL_GIGA_MAC_VER_32:
3079 case RTL_GIGA_MAC_VER_33:
3080 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3081 break;
3082
3083 case RTL_GIGA_MAC_VER_11:
3084 case RTL_GIGA_MAC_VER_12:
3085 case RTL_GIGA_MAC_VER_17:
3086 case RTL_GIGA_MAC_VER_18:
3087 case RTL_GIGA_MAC_VER_19:
3088 case RTL_GIGA_MAC_VER_20:
3089 case RTL_GIGA_MAC_VER_21:
3090 case RTL_GIGA_MAC_VER_22:
3091 case RTL_GIGA_MAC_VER_23:
3092 case RTL_GIGA_MAC_VER_24:
3093 case RTL_GIGA_MAC_VER_25:
3094 case RTL_GIGA_MAC_VER_26:
3095 case RTL_GIGA_MAC_VER_27:
3096 case RTL_GIGA_MAC_VER_28:
3097 case RTL_GIGA_MAC_VER_31:
3098 rtl_writephy(tp, 0x0e, 0x0200);
3099 default:
3100 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3101 break;
3102 }
françois romieu065c27c2011-01-03 15:08:12 +00003103}
3104
3105static void r8168_pll_power_down(struct rtl8169_private *tp)
3106{
3107 void __iomem *ioaddr = tp->mmio_addr;
3108
Hayes Wang5d2e1952011-02-22 17:26:22 +08003109 if (((tp->mac_version == RTL_GIGA_MAC_VER_27) ||
hayeswang4804b3b2011-03-21 01:50:29 +00003110 (tp->mac_version == RTL_GIGA_MAC_VER_28) ||
3111 (tp->mac_version == RTL_GIGA_MAC_VER_31)) &&
3112 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003113 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003114 }
françois romieu065c27c2011-01-03 15:08:12 +00003115
3116 if (((tp->mac_version == RTL_GIGA_MAC_VER_23) ||
3117 (tp->mac_version == RTL_GIGA_MAC_VER_24)) &&
3118 (RTL_R16(CPlusCmd) & ASF)) {
3119 return;
3120 }
3121
hayeswang01dc7fe2011-03-21 01:50:28 +00003122 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3123 tp->mac_version == RTL_GIGA_MAC_VER_33)
3124 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3125
françois romieu065c27c2011-01-03 15:08:12 +00003126 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3127 rtl_writephy(tp, 0x1f, 0x0000);
3128 rtl_writephy(tp, MII_BMCR, 0x0000);
3129
3130 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3131 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3132 return;
3133 }
3134
3135 r8168_phy_power_down(tp);
3136
3137 switch (tp->mac_version) {
3138 case RTL_GIGA_MAC_VER_25:
3139 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003140 case RTL_GIGA_MAC_VER_27:
3141 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003142 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003143 case RTL_GIGA_MAC_VER_32:
3144 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003145 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3146 break;
3147 }
3148}
3149
3150static void r8168_pll_power_up(struct rtl8169_private *tp)
3151{
3152 void __iomem *ioaddr = tp->mmio_addr;
3153
Hayes Wang5d2e1952011-02-22 17:26:22 +08003154 if (((tp->mac_version == RTL_GIGA_MAC_VER_27) ||
hayeswang4804b3b2011-03-21 01:50:29 +00003155 (tp->mac_version == RTL_GIGA_MAC_VER_28) ||
3156 (tp->mac_version == RTL_GIGA_MAC_VER_31)) &&
3157 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003158 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003159 }
françois romieu065c27c2011-01-03 15:08:12 +00003160
3161 switch (tp->mac_version) {
3162 case RTL_GIGA_MAC_VER_25:
3163 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003164 case RTL_GIGA_MAC_VER_27:
3165 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003166 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003167 case RTL_GIGA_MAC_VER_32:
3168 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003169 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3170 break;
3171 }
3172
3173 r8168_phy_power_up(tp);
3174}
3175
3176static void rtl_pll_power_op(struct rtl8169_private *tp,
3177 void (*op)(struct rtl8169_private *))
3178{
3179 if (op)
3180 op(tp);
3181}
3182
3183static void rtl_pll_power_down(struct rtl8169_private *tp)
3184{
3185 rtl_pll_power_op(tp, tp->pll_power_ops.down);
3186}
3187
3188static void rtl_pll_power_up(struct rtl8169_private *tp)
3189{
3190 rtl_pll_power_op(tp, tp->pll_power_ops.up);
3191}
3192
3193static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3194{
3195 struct pll_power_ops *ops = &tp->pll_power_ops;
3196
3197 switch (tp->mac_version) {
3198 case RTL_GIGA_MAC_VER_07:
3199 case RTL_GIGA_MAC_VER_08:
3200 case RTL_GIGA_MAC_VER_09:
3201 case RTL_GIGA_MAC_VER_10:
3202 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003203 case RTL_GIGA_MAC_VER_29:
3204 case RTL_GIGA_MAC_VER_30:
françois romieu065c27c2011-01-03 15:08:12 +00003205 ops->down = r810x_pll_power_down;
3206 ops->up = r810x_pll_power_up;
3207 break;
3208
3209 case RTL_GIGA_MAC_VER_11:
3210 case RTL_GIGA_MAC_VER_12:
3211 case RTL_GIGA_MAC_VER_17:
3212 case RTL_GIGA_MAC_VER_18:
3213 case RTL_GIGA_MAC_VER_19:
3214 case RTL_GIGA_MAC_VER_20:
3215 case RTL_GIGA_MAC_VER_21:
3216 case RTL_GIGA_MAC_VER_22:
3217 case RTL_GIGA_MAC_VER_23:
3218 case RTL_GIGA_MAC_VER_24:
3219 case RTL_GIGA_MAC_VER_25:
3220 case RTL_GIGA_MAC_VER_26:
3221 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003222 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003223 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003224 case RTL_GIGA_MAC_VER_32:
3225 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003226 ops->down = r8168_pll_power_down;
3227 ops->up = r8168_pll_power_up;
3228 break;
3229
3230 default:
3231 ops->down = NULL;
3232 ops->up = NULL;
3233 break;
3234 }
3235}
3236
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003237static int __devinit
3238rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
3239{
Francois Romieu0e485152007-02-20 00:00:26 +01003240 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
3241 const unsigned int region = cfg->region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 struct rtl8169_private *tp;
Francois Romieuccdffb92008-07-26 14:26:06 +02003243 struct mii_if_info *mii;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003244 struct net_device *dev;
3245 void __iomem *ioaddr;
Francois Romieu2b7b4312011-04-18 22:53:24 -07003246 int chipset, i;
Francois Romieu07d3f512007-02-21 22:40:46 +01003247 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003249 if (netif_msg_drv(&debug)) {
3250 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
3251 MODULENAME, RTL8169_VERSION);
3252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 dev = alloc_etherdev(sizeof (*tp));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003255 if (!dev) {
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003256 if (netif_msg_drv(&debug))
Jeff Garzik9b91cf92006-06-27 11:39:50 -04003257 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003258 rc = -ENOMEM;
3259 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 }
3261
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieu8b4ab282008-11-19 22:05:25 -08003263 dev->netdev_ops = &rtl8169_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 tp = netdev_priv(dev);
David Howellsc4028952006-11-22 14:57:56 +00003265 tp->dev = dev;
Ivan Vecera21e197f2008-04-17 22:48:41 +02003266 tp->pci_dev = pdev;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003267 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268
Francois Romieuccdffb92008-07-26 14:26:06 +02003269 mii = &tp->mii;
3270 mii->dev = dev;
3271 mii->mdio_read = rtl_mdio_read;
3272 mii->mdio_write = rtl_mdio_write;
3273 mii->phy_id_mask = 0x1f;
3274 mii->reg_num_mask = 0x1f;
3275 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
3276
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +00003277 /* disable ASPM completely as that cause random device stop working
3278 * problems as well as full system hangs for some PCIe devices users */
3279 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
3280 PCIE_LINK_STATE_CLKPM);
3281
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 /* enable device (incl. PCI PM wakeup and hotplug setup) */
3283 rc = pci_enable_device(pdev);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003284 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003285 netif_err(tp, probe, dev, "enable failure\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003286 goto err_out_free_dev_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 }
3288
françois romieu87aeec72010-04-26 11:42:06 +00003289 if (pci_set_mwi(pdev) < 0)
3290 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 /* make sure PCI base addr 1 is MMIO */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003293 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003294 netif_err(tp, probe, dev,
3295 "region #%d not an MMIO resource, aborting\n",
3296 region);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003298 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003300
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 /* check for weird/broken PCI region reporting */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003302 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003303 netif_err(tp, probe, dev,
3304 "Invalid PCI region size(s), aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003306 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 }
3308
3309 rc = pci_request_regions(pdev, MODULENAME);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003310 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003311 netif_err(tp, probe, dev, "could not request regions\n");
françois romieu87aeec72010-04-26 11:42:06 +00003312 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 }
3314
Hayes Wangd24e9aa2011-02-22 17:26:19 +08003315 tp->cp_cmd = RxChkSum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316
3317 if ((sizeof(dma_addr_t) > 4) &&
David S. Miller4300e8c2010-03-26 10:23:30 -07003318 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 tp->cp_cmd |= PCIDAC;
3320 dev->features |= NETIF_F_HIGHDMA;
3321 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07003322 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003324 netif_err(tp, probe, dev, "DMA configuration failed\n");
françois romieu87aeec72010-04-26 11:42:06 +00003325 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 }
3327 }
3328
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 /* ioremap MMIO region */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003330 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003331 if (!ioaddr) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003332 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 rc = -EIO;
françois romieu87aeec72010-04-26 11:42:06 +00003334 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 }
3336
David S. Miller4300e8c2010-03-26 10:23:30 -07003337 tp->pcie_cap = pci_find_capability(pdev, PCI_CAP_ID_EXP);
3338 if (!tp->pcie_cap)
3339 netif_info(tp, probe, dev, "no PCI Express capability\n");
3340
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003341 RTL_W16(IntrMask, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342
3343 /* Soft reset the chip. */
3344 RTL_W8(ChipCmd, CmdReset);
3345
3346 /* Check that the chip has finished the reset. */
Francois Romieu07d3f512007-02-21 22:40:46 +01003347 for (i = 0; i < 100; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3349 break;
Francois Romieub518fa82006-08-16 15:23:13 +02003350 msleep_interruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 }
3352
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003353 RTL_W16(IntrStatus, 0xffff);
3354
françois romieuca52efd2009-07-24 12:34:19 +00003355 pci_set_master(pdev);
3356
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 /* Identify chip attached to board */
3358 rtl8169_get_mac_version(tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359
Francois Romieu7a8fc772011-03-01 17:18:33 +01003360 /*
3361 * Pretend we are using VLANs; This bypasses a nasty bug where
3362 * Interrupts stop flowing on high load on 8110SCd controllers.
3363 */
3364 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3365 tp->cp_cmd |= RxVlan;
3366
françois romieuc0e45c12011-01-03 15:08:04 +00003367 rtl_init_mdio_ops(tp);
françois romieu065c27c2011-01-03 15:08:12 +00003368 rtl_init_pll_power_ops(tp);
françois romieuc0e45c12011-01-03 15:08:04 +00003369
Jean Delvaref21b75e2009-05-26 20:54:48 -07003370 /* Use appropriate default if unknown */
3371 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003372 netif_notice(tp, probe, dev,
3373 "unknown MAC, using family default\n");
Jean Delvaref21b75e2009-05-26 20:54:48 -07003374 tp->mac_version = cfg->default_ver;
3375 }
3376
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 rtl8169_print_mac_version(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378
Roel Kluincee60c32008-04-17 22:35:54 +02003379 for (i = 0; i < ARRAY_SIZE(rtl_chip_info); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 if (tp->mac_version == rtl_chip_info[i].mac_version)
3381 break;
3382 }
Roel Kluincee60c32008-04-17 22:35:54 +02003383 if (i == ARRAY_SIZE(rtl_chip_info)) {
Jean Delvaref21b75e2009-05-26 20:54:48 -07003384 dev_err(&pdev->dev,
3385 "driver bug, MAC version not found in rtl_chip_info\n");
françois romieu87aeec72010-04-26 11:42:06 +00003386 goto err_out_msi_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 }
Francois Romieu2b7b4312011-04-18 22:53:24 -07003388 chipset = i;
3389 tp->txd_version = rtl_chip_info[chipset].txd_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390
Francois Romieu5d06a992006-02-23 00:47:58 +01003391 RTL_W8(Cfg9346, Cfg9346_Unlock);
3392 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
3393 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
Bruno Prémont20037fa2008-10-08 17:05:03 -07003394 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
3395 tp->features |= RTL_FEATURE_WOL;
3396 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
3397 tp->features |= RTL_FEATURE_WOL;
Francois Romieufbac58f2007-10-04 22:51:38 +02003398 tp->features |= rtl_try_msi(pdev, ioaddr, cfg);
Francois Romieu5d06a992006-02-23 00:47:58 +01003399 RTL_W8(Cfg9346, Cfg9346_Lock);
3400
Francois Romieu66ec5d42007-11-06 22:56:10 +01003401 if ((tp->mac_version <= RTL_GIGA_MAC_VER_06) &&
3402 (RTL_R8(PHYstatus) & TBI_Enable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 tp->set_speed = rtl8169_set_speed_tbi;
3404 tp->get_settings = rtl8169_gset_tbi;
3405 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
3406 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
3407 tp->link_ok = rtl8169_tbi_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003408 tp->do_ioctl = rtl_tbi_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409
Francois Romieu64e4bfb2006-08-17 12:43:06 +02003410 tp->phy_1000_ctrl_reg = ADVERTISE_1000FULL; /* Implied by TBI */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 } else {
3412 tp->set_speed = rtl8169_set_speed_xmii;
3413 tp->get_settings = rtl8169_gset_xmii;
3414 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
3415 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
3416 tp->link_ok = rtl8169_xmii_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003417 tp->do_ioctl = rtl_xmii_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 }
3419
Francois Romieudf58ef52008-10-09 14:35:58 -07003420 spin_lock_init(&tp->lock);
3421
Petr Vandrovec738e1e62008-10-12 20:58:29 -07003422 tp->mmio_addr = ioaddr;
3423
Ivan Vecera7bf6bf42008-09-23 22:46:29 +00003424 /* Get MAC address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 for (i = 0; i < MAC_ADDR_LEN; i++)
3426 dev->dev_addr[i] = RTL_R8(MAC0 + i);
John W. Linville6d6525b2005-09-12 10:48:57 -04003427 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
3431 dev->irq = pdev->irq;
3432 dev->base_addr = (unsigned long) ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003434 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435
Michał Mirosław350fb322011-04-08 06:35:56 +00003436 /* don't enable SG, IP_CSUM and TSO by default - it might not work
3437 * properly for all devices */
3438 dev->features |= NETIF_F_RXCSUM |
3439 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3440
3441 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3442 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3443 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3444 NETIF_F_HIGHDMA;
3445
3446 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3447 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
3448 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449
3450 tp->intr_mask = 0xffff;
Francois Romieu0e485152007-02-20 00:00:26 +01003451 tp->hw_start = cfg->hw_start;
3452 tp->intr_event = cfg->intr_event;
3453 tp->napi_event = cfg->napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454
Francois Romieu2efa53f2007-03-09 00:00:05 +01003455 init_timer(&tp->timer);
3456 tp->timer.data = (unsigned long) dev;
3457 tp->timer.function = rtl8169_phy_timer;
3458
François Romieu953a12c2011-04-24 17:38:48 +02003459 tp->fw = RTL_FIRMWARE_UNKNOWN;
3460
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 rc = register_netdev(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003462 if (rc < 0)
françois romieu87aeec72010-04-26 11:42:06 +00003463 goto err_out_msi_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464
3465 pci_set_drvdata(pdev, dev);
3466
Joe Perchesbf82c182010-02-09 11:49:50 +00003467 netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
Francois Romieu2b7b4312011-04-18 22:53:24 -07003468 rtl_chip_info[chipset].name, dev->base_addr, dev->dev_addr,
Joe Perchesbf82c182010-02-09 11:49:50 +00003469 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470
françois romieue6de30d2011-01-03 15:08:37 +00003471 if ((tp->mac_version == RTL_GIGA_MAC_VER_27) ||
hayeswang4804b3b2011-03-21 01:50:29 +00003472 (tp->mac_version == RTL_GIGA_MAC_VER_28) ||
3473 (tp->mac_version == RTL_GIGA_MAC_VER_31)) {
françois romieub646d902011-01-03 15:08:21 +00003474 rtl8168_driver_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003475 }
françois romieub646d902011-01-03 15:08:21 +00003476
Bruno Prémont8b76ab32008-10-08 17:06:25 -07003477 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478
Alan Sternf3ec4f82010-06-08 15:23:51 -04003479 if (pci_dev_run_wake(pdev))
3480 pm_runtime_put_noidle(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003481
Ivan Vecera0d672e92011-02-15 02:08:39 +00003482 netif_carrier_off(dev);
3483
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003484out:
3485 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486
françois romieu87aeec72010-04-26 11:42:06 +00003487err_out_msi_4:
Francois Romieufbac58f2007-10-04 22:51:38 +02003488 rtl_disable_msi(pdev, tp);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003489 iounmap(ioaddr);
françois romieu87aeec72010-04-26 11:42:06 +00003490err_out_free_res_3:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003491 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003492err_out_mwi_2:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003493 pci_clear_mwi(pdev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003494 pci_disable_device(pdev);
3495err_out_free_dev_1:
3496 free_netdev(dev);
3497 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498}
3499
Francois Romieu07d3f512007-02-21 22:40:46 +01003500static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501{
3502 struct net_device *dev = pci_get_drvdata(pdev);
3503 struct rtl8169_private *tp = netdev_priv(dev);
3504
françois romieue6de30d2011-01-03 15:08:37 +00003505 if ((tp->mac_version == RTL_GIGA_MAC_VER_27) ||
hayeswang4804b3b2011-03-21 01:50:29 +00003506 (tp->mac_version == RTL_GIGA_MAC_VER_28) ||
3507 (tp->mac_version == RTL_GIGA_MAC_VER_31)) {
françois romieub646d902011-01-03 15:08:21 +00003508 rtl8168_driver_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003509 }
françois romieub646d902011-01-03 15:08:21 +00003510
Tejun Heo23f333a2010-12-12 16:45:14 +01003511 cancel_delayed_work_sync(&tp->task);
Francois Romieueb2a0212007-02-15 23:37:21 +01003512
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 unregister_netdev(dev);
Ivan Veceracc098dc2009-11-29 23:12:52 -08003514
François Romieu953a12c2011-04-24 17:38:48 +02003515 rtl_release_firmware(tp);
3516
Alan Sternf3ec4f82010-06-08 15:23:51 -04003517 if (pci_dev_run_wake(pdev))
3518 pm_runtime_get_noresume(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003519
Ivan Veceracc098dc2009-11-29 23:12:52 -08003520 /* restore original MAC address */
3521 rtl_rar_set(tp, dev->perm_addr);
3522
Francois Romieufbac58f2007-10-04 22:51:38 +02003523 rtl_disable_msi(pdev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 rtl8169_release_board(pdev, dev, tp->mmio_addr);
3525 pci_set_drvdata(pdev, NULL);
3526}
3527
François Romieu953a12c2011-04-24 17:38:48 +02003528static void rtl_request_firmware(struct rtl8169_private *tp)
3529{
3530 int i;
3531
3532 /* Return early if the firmware is already loaded / cached. */
3533 if (!IS_ERR(tp->fw))
3534 goto out;
3535
3536 for (i = 0; i < ARRAY_SIZE(rtl_firmware_infos); i++) {
3537 const struct rtl_firmware_info *info = rtl_firmware_infos + i;
3538
3539 if (info->mac_version == tp->mac_version) {
3540 const char *name = info->fw_name;
3541 int rc;
3542
3543 rc = request_firmware(&tp->fw, name, &tp->pci_dev->dev);
3544 if (rc < 0) {
3545 netif_warn(tp, ifup, tp->dev, "unable to load "
3546 "firmware patch %s (%d)\n", name, rc);
3547 goto out_disable_request_firmware;
3548 }
3549 goto out;
3550 }
3551 }
3552
3553out_disable_request_firmware:
3554 tp->fw = NULL;
3555out:
3556 return;
3557}
3558
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559static int rtl8169_open(struct net_device *dev)
3560{
3561 struct rtl8169_private *tp = netdev_priv(dev);
françois romieueee3a962011-01-08 02:17:26 +00003562 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu99f252b2007-04-02 22:59:59 +02003564 int retval = -ENOMEM;
3565
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003566 pm_runtime_get_sync(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567
Neil Hormanc0cd8842010-03-29 13:16:02 -07003568 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 * Rx and Tx desscriptors needs 256 bytes alignment.
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003570 * dma_alloc_coherent provides more.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 */
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003572 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
3573 &tp->TxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 if (!tp->TxDescArray)
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003575 goto err_pm_runtime_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003577 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
3578 &tp->RxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 if (!tp->RxDescArray)
Francois Romieu99f252b2007-04-02 22:59:59 +02003580 goto err_free_tx_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581
3582 retval = rtl8169_init_ring(dev);
3583 if (retval < 0)
Francois Romieu99f252b2007-04-02 22:59:59 +02003584 goto err_free_rx_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585
David Howellsc4028952006-11-22 14:57:56 +00003586 INIT_DELAYED_WORK(&tp->task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587
Francois Romieu99f252b2007-04-02 22:59:59 +02003588 smp_mb();
3589
François Romieu953a12c2011-04-24 17:38:48 +02003590 rtl_request_firmware(tp);
3591
Francois Romieufbac58f2007-10-04 22:51:38 +02003592 retval = request_irq(dev->irq, rtl8169_interrupt,
3593 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
Francois Romieu99f252b2007-04-02 22:59:59 +02003594 dev->name, dev);
3595 if (retval < 0)
François Romieu953a12c2011-04-24 17:38:48 +02003596 goto err_release_fw_2;
Francois Romieu99f252b2007-04-02 22:59:59 +02003597
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003598 napi_enable(&tp->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003599
françois romieueee3a962011-01-08 02:17:26 +00003600 rtl8169_init_phy(dev, tp);
3601
Michał Mirosław350fb322011-04-08 06:35:56 +00003602 rtl8169_set_features(dev, dev->features);
françois romieueee3a962011-01-08 02:17:26 +00003603
françois romieu065c27c2011-01-03 15:08:12 +00003604 rtl_pll_power_up(tp);
3605
Francois Romieu07ce4062007-02-23 23:36:39 +01003606 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607
3608 rtl8169_request_timer(dev);
3609
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003610 tp->saved_wolopts = 0;
3611 pm_runtime_put_noidle(&pdev->dev);
3612
françois romieueee3a962011-01-08 02:17:26 +00003613 rtl8169_check_link_status(dev, tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614out:
3615 return retval;
3616
François Romieu953a12c2011-04-24 17:38:48 +02003617err_release_fw_2:
3618 rtl_release_firmware(tp);
Francois Romieu99f252b2007-04-02 22:59:59 +02003619 rtl8169_rx_clear(tp);
3620err_free_rx_1:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003621 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
3622 tp->RxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003623 tp->RxDescArray = NULL;
Francois Romieu99f252b2007-04-02 22:59:59 +02003624err_free_tx_0:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003625 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
3626 tp->TxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003627 tp->TxDescArray = NULL;
3628err_pm_runtime_put:
3629 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 goto out;
3631}
3632
françois romieue6de30d2011-01-03 15:08:37 +00003633static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634{
françois romieue6de30d2011-01-03 15:08:37 +00003635 void __iomem *ioaddr = tp->mmio_addr;
3636
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 /* Disable interrupts */
3638 rtl8169_irq_mask_and_ack(ioaddr);
3639
Hayes Wang5d2e1952011-02-22 17:26:22 +08003640 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00003641 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3642 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieue6de30d2011-01-03 15:08:37 +00003643 while (RTL_R8(TxPoll) & NPQ)
3644 udelay(20);
3645
3646 }
3647
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 /* Reset the chipset */
3649 RTL_W8(ChipCmd, CmdReset);
3650
3651 /* PCI commit */
3652 RTL_R8(ChipCmd);
3653}
3654
Francois Romieu7f796d82007-06-11 23:04:41 +02003655static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01003656{
3657 void __iomem *ioaddr = tp->mmio_addr;
3658 u32 cfg = rtl8169_rx_config;
3659
Francois Romieu2b7b4312011-04-18 22:53:24 -07003660 cfg |= (RTL_R32(RxConfig) & RTL_RX_CONFIG_MASK);
Francois Romieu9cb427b2006-11-02 00:10:16 +01003661 RTL_W32(RxConfig, cfg);
3662
3663 /* Set DMA burst size and Interframe Gap Time */
3664 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
3665 (InterFrameGap << TxInterFrameGapShift));
3666}
3667
Francois Romieu07ce4062007-02-23 23:36:39 +01003668static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669{
3670 struct rtl8169_private *tp = netdev_priv(dev);
3671 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01003672 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673
3674 /* Soft reset the chip. */
3675 RTL_W8(ChipCmd, CmdReset);
3676
3677 /* Check that the chip has finished the reset. */
Francois Romieu07d3f512007-02-21 22:40:46 +01003678 for (i = 0; i < 100; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3680 break;
Francois Romieub518fa82006-08-16 15:23:13 +02003681 msleep_interruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 }
3683
Francois Romieu07ce4062007-02-23 23:36:39 +01003684 tp->hw_start(dev);
3685
Francois Romieu07ce4062007-02-23 23:36:39 +01003686 netif_start_queue(dev);
3687}
3688
3689
Francois Romieu7f796d82007-06-11 23:04:41 +02003690static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
3691 void __iomem *ioaddr)
3692{
3693 /*
3694 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
3695 * register to be written before TxDescAddrLow to work.
3696 * Switching from MMIO to I/O access fixes the issue as well.
3697 */
3698 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07003699 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02003700 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07003701 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02003702}
3703
3704static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
3705{
3706 u16 cmd;
3707
3708 cmd = RTL_R16(CPlusCmd);
3709 RTL_W16(CPlusCmd, cmd);
3710 return cmd;
3711}
3712
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07003713static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d82007-06-11 23:04:41 +02003714{
3715 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e82009-10-26 10:52:37 +00003716 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d82007-06-11 23:04:41 +02003717}
3718
Francois Romieu6dccd162007-02-13 23:38:05 +01003719static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
3720{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003721 static const struct {
Francois Romieu6dccd162007-02-13 23:38:05 +01003722 u32 mac_version;
3723 u32 clk;
3724 u32 val;
3725 } cfg2_info [] = {
3726 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
3727 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
3728 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
3729 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
3730 }, *p = cfg2_info;
3731 unsigned int i;
3732 u32 clk;
3733
3734 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01003735 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01003736 if ((p->mac_version == mac_version) && (p->clk == clk)) {
3737 RTL_W32(0x7c, p->val);
3738 break;
3739 }
3740 }
3741}
3742
Francois Romieu07ce4062007-02-23 23:36:39 +01003743static void rtl_hw_start_8169(struct net_device *dev)
3744{
3745 struct rtl8169_private *tp = netdev_priv(dev);
3746 void __iomem *ioaddr = tp->mmio_addr;
3747 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01003748
Francois Romieu9cb427b2006-11-02 00:10:16 +01003749 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
3750 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
3751 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
3752 }
3753
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieu9cb427b2006-11-02 00:10:16 +01003755 if ((tp->mac_version == RTL_GIGA_MAC_VER_01) ||
3756 (tp->mac_version == RTL_GIGA_MAC_VER_02) ||
3757 (tp->mac_version == RTL_GIGA_MAC_VER_03) ||
3758 (tp->mac_version == RTL_GIGA_MAC_VER_04))
3759 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3760
françois romieuf0298f82011-01-03 15:07:42 +00003761 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762
Eric Dumazet6f0333b2010-10-11 11:17:47 +00003763 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764
Francois Romieuc946b302007-10-04 00:42:50 +02003765 if ((tp->mac_version == RTL_GIGA_MAC_VER_01) ||
3766 (tp->mac_version == RTL_GIGA_MAC_VER_02) ||
3767 (tp->mac_version == RTL_GIGA_MAC_VER_03) ||
3768 (tp->mac_version == RTL_GIGA_MAC_VER_04))
3769 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770
Francois Romieu7f796d82007-06-11 23:04:41 +02003771 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02003772
3773 if ((tp->mac_version == RTL_GIGA_MAC_VER_02) ||
3774 (tp->mac_version == RTL_GIGA_MAC_VER_03)) {
Joe Perches06fa7352007-10-18 21:15:00 +02003775 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02003777 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 }
3779
Francois Romieubcf0bf92006-07-26 23:14:13 +02003780 RTL_W16(CPlusCmd, tp->cp_cmd);
3781
Francois Romieu6dccd162007-02-13 23:38:05 +01003782 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
3783
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 /*
3785 * Undocumented corner. Supposedly:
3786 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
3787 */
3788 RTL_W16(IntrMitigate, 0x0000);
3789
Francois Romieu7f796d82007-06-11 23:04:41 +02003790 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01003791
Francois Romieuc946b302007-10-04 00:42:50 +02003792 if ((tp->mac_version != RTL_GIGA_MAC_VER_01) &&
3793 (tp->mac_version != RTL_GIGA_MAC_VER_02) &&
3794 (tp->mac_version != RTL_GIGA_MAC_VER_03) &&
3795 (tp->mac_version != RTL_GIGA_MAC_VER_04)) {
3796 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3797 rtl_set_rx_tx_config_registers(tp);
3798 }
3799
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02003801
3802 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
3803 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804
3805 RTL_W32(RxMissed, 0);
3806
Francois Romieu07ce4062007-02-23 23:36:39 +01003807 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808
3809 /* no early-rx interrupts */
3810 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01003811
3812 /* Enable all known interrupts by setting the interrupt mask. */
Francois Romieu0e485152007-02-20 00:00:26 +01003813 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01003814}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815
Francois Romieu9c14cea2008-07-05 00:21:15 +02003816static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
Francois Romieu458a9f62008-08-02 15:50:02 +02003817{
Francois Romieu9c14cea2008-07-05 00:21:15 +02003818 struct net_device *dev = pci_get_drvdata(pdev);
3819 struct rtl8169_private *tp = netdev_priv(dev);
3820 int cap = tp->pcie_cap;
Francois Romieu458a9f62008-08-02 15:50:02 +02003821
Francois Romieu9c14cea2008-07-05 00:21:15 +02003822 if (cap) {
3823 u16 ctl;
3824
3825 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
3826 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
3827 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
3828 }
Francois Romieu458a9f62008-08-02 15:50:02 +02003829}
3830
françois romieu650e8d52011-01-03 15:08:29 +00003831static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02003832{
3833 u32 csi;
3834
3835 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
françois romieu650e8d52011-01-03 15:08:29 +00003836 rtl_csi_write(ioaddr, 0x070c, csi | bits);
3837}
3838
françois romieue6de30d2011-01-03 15:08:37 +00003839static void rtl_csi_access_enable_1(void __iomem *ioaddr)
3840{
3841 rtl_csi_access_enable(ioaddr, 0x17000000);
3842}
3843
françois romieu650e8d52011-01-03 15:08:29 +00003844static void rtl_csi_access_enable_2(void __iomem *ioaddr)
3845{
3846 rtl_csi_access_enable(ioaddr, 0x27000000);
Francois Romieudacf8152008-08-02 20:44:13 +02003847}
3848
3849struct ephy_info {
3850 unsigned int offset;
3851 u16 mask;
3852 u16 bits;
3853};
3854
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003855static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02003856{
3857 u16 w;
3858
3859 while (len-- > 0) {
3860 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
3861 rtl_ephy_write(ioaddr, e->offset, w);
3862 e++;
3863 }
3864}
3865
Francois Romieub726e492008-06-28 12:22:59 +02003866static void rtl_disable_clock_request(struct pci_dev *pdev)
3867{
3868 struct net_device *dev = pci_get_drvdata(pdev);
3869 struct rtl8169_private *tp = netdev_priv(dev);
3870 int cap = tp->pcie_cap;
3871
3872 if (cap) {
3873 u16 ctl;
3874
3875 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
3876 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
3877 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
3878 }
3879}
3880
françois romieue6de30d2011-01-03 15:08:37 +00003881static void rtl_enable_clock_request(struct pci_dev *pdev)
3882{
3883 struct net_device *dev = pci_get_drvdata(pdev);
3884 struct rtl8169_private *tp = netdev_priv(dev);
3885 int cap = tp->pcie_cap;
3886
3887 if (cap) {
3888 u16 ctl;
3889
3890 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
3891 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
3892 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
3893 }
3894}
3895
Francois Romieub726e492008-06-28 12:22:59 +02003896#define R8168_CPCMD_QUIRK_MASK (\
3897 EnableBist | \
3898 Mac_dbgo_oe | \
3899 Force_half_dup | \
3900 Force_rxflow_en | \
3901 Force_txflow_en | \
3902 Cxpl_dbg_sel | \
3903 ASF | \
3904 PktCntrDisable | \
3905 Mac_dbgo_sel)
3906
Francois Romieu219a1e92008-06-28 11:58:39 +02003907static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
3908{
Francois Romieub726e492008-06-28 12:22:59 +02003909 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3910
3911 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3912
Francois Romieu2e68ae42008-06-28 12:00:55 +02003913 rtl_tx_performance_tweak(pdev,
3914 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02003915}
3916
3917static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
3918{
3919 rtl_hw_start_8168bb(ioaddr, pdev);
Francois Romieub726e492008-06-28 12:22:59 +02003920
françois romieuf0298f82011-01-03 15:07:42 +00003921 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02003922
3923 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02003924}
3925
3926static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
3927{
Francois Romieub726e492008-06-28 12:22:59 +02003928 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
3929
3930 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3931
Francois Romieu219a1e92008-06-28 11:58:39 +02003932 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02003933
3934 rtl_disable_clock_request(pdev);
3935
3936 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02003937}
3938
Francois Romieuef3386f2008-06-29 12:24:30 +02003939static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
Francois Romieu219a1e92008-06-28 11:58:39 +02003940{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003941 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02003942 { 0x01, 0, 0x0001 },
3943 { 0x02, 0x0800, 0x1000 },
3944 { 0x03, 0, 0x0042 },
3945 { 0x06, 0x0080, 0x0000 },
3946 { 0x07, 0, 0x2000 }
3947 };
3948
françois romieu650e8d52011-01-03 15:08:29 +00003949 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02003950
3951 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
3952
Francois Romieu219a1e92008-06-28 11:58:39 +02003953 __rtl_hw_start_8168cp(ioaddr, pdev);
3954}
3955
Francois Romieuef3386f2008-06-29 12:24:30 +02003956static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
3957{
françois romieu650e8d52011-01-03 15:08:29 +00003958 rtl_csi_access_enable_2(ioaddr);
Francois Romieuef3386f2008-06-29 12:24:30 +02003959
3960 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3961
3962 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3963
3964 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3965}
3966
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003967static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
3968{
françois romieu650e8d52011-01-03 15:08:29 +00003969 rtl_csi_access_enable_2(ioaddr);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003970
3971 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3972
3973 /* Magic. */
3974 RTL_W8(DBG_REG, 0x20);
3975
françois romieuf0298f82011-01-03 15:07:42 +00003976 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003977
3978 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3979
3980 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3981}
3982
Francois Romieu219a1e92008-06-28 11:58:39 +02003983static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
3984{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003985 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02003986 { 0x02, 0x0800, 0x1000 },
3987 { 0x03, 0, 0x0002 },
3988 { 0x06, 0x0080, 0x0000 }
3989 };
3990
françois romieu650e8d52011-01-03 15:08:29 +00003991 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02003992
3993 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
3994
3995 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
3996
Francois Romieu219a1e92008-06-28 11:58:39 +02003997 __rtl_hw_start_8168cp(ioaddr, pdev);
3998}
3999
4000static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
4001{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004002 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004003 { 0x01, 0, 0x0001 },
4004 { 0x03, 0x0400, 0x0220 }
4005 };
4006
françois romieu650e8d52011-01-03 15:08:29 +00004007 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004008
4009 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4010
Francois Romieu219a1e92008-06-28 11:58:39 +02004011 __rtl_hw_start_8168cp(ioaddr, pdev);
4012}
4013
Francois Romieu197ff762008-06-28 13:16:02 +02004014static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
4015{
4016 rtl_hw_start_8168c_2(ioaddr, pdev);
4017}
4018
Francois Romieu6fb07052008-06-29 11:54:28 +02004019static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
4020{
françois romieu650e8d52011-01-03 15:08:29 +00004021 rtl_csi_access_enable_2(ioaddr);
Francois Romieu6fb07052008-06-29 11:54:28 +02004022
4023 __rtl_hw_start_8168cp(ioaddr, pdev);
4024}
4025
Francois Romieu5b538df2008-07-20 16:22:45 +02004026static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
4027{
françois romieu650e8d52011-01-03 15:08:29 +00004028 rtl_csi_access_enable_2(ioaddr);
Francois Romieu5b538df2008-07-20 16:22:45 +02004029
4030 rtl_disable_clock_request(pdev);
4031
françois romieuf0298f82011-01-03 15:07:42 +00004032 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004033
4034 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4035
4036 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4037}
4038
hayeswang4804b3b2011-03-21 01:50:29 +00004039static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
4040{
4041 rtl_csi_access_enable_1(ioaddr);
4042
4043 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4044
4045 RTL_W8(MaxTxPacketSize, TxPacketMax);
4046
4047 rtl_disable_clock_request(pdev);
4048}
4049
françois romieue6de30d2011-01-03 15:08:37 +00004050static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4051{
4052 static const struct ephy_info e_info_8168d_4[] = {
4053 { 0x0b, ~0, 0x48 },
4054 { 0x19, 0x20, 0x50 },
4055 { 0x0c, ~0, 0x20 }
4056 };
4057 int i;
4058
4059 rtl_csi_access_enable_1(ioaddr);
4060
4061 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4062
4063 RTL_W8(MaxTxPacketSize, TxPacketMax);
4064
4065 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4066 const struct ephy_info *e = e_info_8168d_4 + i;
4067 u16 w;
4068
4069 w = rtl_ephy_read(ioaddr, e->offset);
4070 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4071 }
4072
4073 rtl_enable_clock_request(pdev);
4074}
4075
hayeswang01dc7fe2011-03-21 01:50:28 +00004076static void rtl_hw_start_8168e(void __iomem *ioaddr, struct pci_dev *pdev)
4077{
4078 static const struct ephy_info e_info_8168e[] = {
4079 { 0x00, 0x0200, 0x0100 },
4080 { 0x00, 0x0000, 0x0004 },
4081 { 0x06, 0x0002, 0x0001 },
4082 { 0x06, 0x0000, 0x0030 },
4083 { 0x07, 0x0000, 0x2000 },
4084 { 0x00, 0x0000, 0x0020 },
4085 { 0x03, 0x5800, 0x2000 },
4086 { 0x03, 0x0000, 0x0001 },
4087 { 0x01, 0x0800, 0x1000 },
4088 { 0x07, 0x0000, 0x4000 },
4089 { 0x1e, 0x0000, 0x2000 },
4090 { 0x19, 0xffff, 0xfe6c },
4091 { 0x0a, 0x0000, 0x0040 }
4092 };
4093
4094 rtl_csi_access_enable_2(ioaddr);
4095
4096 rtl_ephy_init(ioaddr, e_info_8168e, ARRAY_SIZE(e_info_8168e));
4097
4098 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4099
4100 RTL_W8(MaxTxPacketSize, TxPacketMax);
4101
4102 rtl_disable_clock_request(pdev);
4103
4104 /* Reset tx FIFO pointer */
4105 RTL_W32(MISC, RTL_R32(MISC) | txpla_rst);
4106 RTL_W32(MISC, RTL_R32(MISC) & ~txpla_rst);
4107
4108 RTL_W8(Config5, RTL_R8(Config5) & ~spi_en);
4109}
4110
Francois Romieu07ce4062007-02-23 23:36:39 +01004111static void rtl_hw_start_8168(struct net_device *dev)
4112{
Francois Romieu2dd99532007-06-11 23:22:52 +02004113 struct rtl8169_private *tp = netdev_priv(dev);
4114 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01004115 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu2dd99532007-06-11 23:22:52 +02004116
4117 RTL_W8(Cfg9346, Cfg9346_Unlock);
4118
françois romieuf0298f82011-01-03 15:07:42 +00004119 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004120
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004121 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004122
Francois Romieu0e485152007-02-20 00:00:26 +01004123 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004124
4125 RTL_W16(CPlusCmd, tp->cp_cmd);
4126
Francois Romieu0e485152007-02-20 00:00:26 +01004127 RTL_W16(IntrMitigate, 0x5151);
4128
4129 /* Work around for RxFIFO overflow. */
Ivan Vecerab5ba6d12011-01-27 12:24:11 +01004130 if (tp->mac_version == RTL_GIGA_MAC_VER_11 ||
4131 tp->mac_version == RTL_GIGA_MAC_VER_22) {
Francois Romieu0e485152007-02-20 00:00:26 +01004132 tp->intr_event |= RxFIFOOver | PCSTimeout;
4133 tp->intr_event &= ~RxOverflow;
4134 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004135
4136 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4137
Francois Romieub8363902008-06-01 12:31:57 +02004138 rtl_set_rx_mode(dev);
4139
4140 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4141 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004142
4143 RTL_R8(IntrMask);
4144
Francois Romieu219a1e92008-06-28 11:58:39 +02004145 switch (tp->mac_version) {
4146 case RTL_GIGA_MAC_VER_11:
4147 rtl_hw_start_8168bb(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004148 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004149
4150 case RTL_GIGA_MAC_VER_12:
4151 case RTL_GIGA_MAC_VER_17:
4152 rtl_hw_start_8168bef(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004153 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004154
4155 case RTL_GIGA_MAC_VER_18:
Francois Romieuef3386f2008-06-29 12:24:30 +02004156 rtl_hw_start_8168cp_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004157 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004158
4159 case RTL_GIGA_MAC_VER_19:
4160 rtl_hw_start_8168c_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004161 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004162
4163 case RTL_GIGA_MAC_VER_20:
4164 rtl_hw_start_8168c_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004165 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004166
Francois Romieu197ff762008-06-28 13:16:02 +02004167 case RTL_GIGA_MAC_VER_21:
4168 rtl_hw_start_8168c_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004169 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004170
Francois Romieu6fb07052008-06-29 11:54:28 +02004171 case RTL_GIGA_MAC_VER_22:
4172 rtl_hw_start_8168c_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004173 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004174
Francois Romieuef3386f2008-06-29 12:24:30 +02004175 case RTL_GIGA_MAC_VER_23:
4176 rtl_hw_start_8168cp_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004177 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004178
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004179 case RTL_GIGA_MAC_VER_24:
4180 rtl_hw_start_8168cp_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004181 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004182
Francois Romieu5b538df2008-07-20 16:22:45 +02004183 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00004184 case RTL_GIGA_MAC_VER_26:
4185 case RTL_GIGA_MAC_VER_27:
Francois Romieu5b538df2008-07-20 16:22:45 +02004186 rtl_hw_start_8168d(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004187 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004188
françois romieue6de30d2011-01-03 15:08:37 +00004189 case RTL_GIGA_MAC_VER_28:
4190 rtl_hw_start_8168d_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004191 break;
4192 case RTL_GIGA_MAC_VER_31:
4193 rtl_hw_start_8168dp(ioaddr, pdev);
4194 break;
4195
hayeswang01dc7fe2011-03-21 01:50:28 +00004196 case RTL_GIGA_MAC_VER_32:
4197 case RTL_GIGA_MAC_VER_33:
4198 rtl_hw_start_8168e(ioaddr, pdev);
4199 break;
françois romieue6de30d2011-01-03 15:08:37 +00004200
Francois Romieu219a1e92008-06-28 11:58:39 +02004201 default:
4202 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
4203 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00004204 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004205 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004206
Francois Romieu0e485152007-02-20 00:00:26 +01004207 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4208
Francois Romieub8363902008-06-01 12:31:57 +02004209 RTL_W8(Cfg9346, Cfg9346_Lock);
4210
Francois Romieu2dd99532007-06-11 23:22:52 +02004211 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004212
Francois Romieu0e485152007-02-20 00:00:26 +01004213 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01004214}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215
Francois Romieu2857ffb2008-08-02 21:08:49 +02004216#define R810X_CPCMD_QUIRK_MASK (\
4217 EnableBist | \
4218 Mac_dbgo_oe | \
4219 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00004220 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02004221 Force_txflow_en | \
4222 Cxpl_dbg_sel | \
4223 ASF | \
4224 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004225 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004226
4227static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4228{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004229 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004230 { 0x01, 0, 0x6e65 },
4231 { 0x02, 0, 0x091f },
4232 { 0x03, 0, 0xc2f9 },
4233 { 0x06, 0, 0xafb5 },
4234 { 0x07, 0, 0x0e00 },
4235 { 0x19, 0, 0xec80 },
4236 { 0x01, 0, 0x2e65 },
4237 { 0x01, 0, 0x6e65 }
4238 };
4239 u8 cfg1;
4240
françois romieu650e8d52011-01-03 15:08:29 +00004241 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004242
4243 RTL_W8(DBG_REG, FIX_NAK_1);
4244
4245 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4246
4247 RTL_W8(Config1,
4248 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
4249 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4250
4251 cfg1 = RTL_R8(Config1);
4252 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
4253 RTL_W8(Config1, cfg1 & ~LEDS0);
4254
Francois Romieu2857ffb2008-08-02 21:08:49 +02004255 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
4256}
4257
4258static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4259{
françois romieu650e8d52011-01-03 15:08:29 +00004260 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004261
4262 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4263
4264 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
4265 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004266}
4267
4268static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
4269{
4270 rtl_hw_start_8102e_2(ioaddr, pdev);
4271
4272 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
4273}
4274
Hayes Wang5a5e4442011-02-22 17:26:21 +08004275static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4276{
4277 static const struct ephy_info e_info_8105e_1[] = {
4278 { 0x07, 0, 0x4000 },
4279 { 0x19, 0, 0x0200 },
4280 { 0x19, 0, 0x0020 },
4281 { 0x1e, 0, 0x2000 },
4282 { 0x03, 0, 0x0001 },
4283 { 0x19, 0, 0x0100 },
4284 { 0x19, 0, 0x0004 },
4285 { 0x0a, 0, 0x0020 }
4286 };
4287
4288 /* Force LAN exit from ASPM if Rx/Tx are not idel */
4289 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
4290
4291 /* disable Early Tally Counter */
4292 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
4293
4294 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
4295 RTL_W8(DLLPR, RTL_R8(DLLPR) | PM_SWITCH);
4296
4297 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
4298}
4299
4300static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4301{
4302 rtl_hw_start_8105e_1(ioaddr, pdev);
4303 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
4304}
4305
Francois Romieu07ce4062007-02-23 23:36:39 +01004306static void rtl_hw_start_8101(struct net_device *dev)
4307{
Francois Romieucdf1a602007-06-11 23:29:50 +02004308 struct rtl8169_private *tp = netdev_priv(dev);
4309 void __iomem *ioaddr = tp->mmio_addr;
4310 struct pci_dev *pdev = tp->pci_dev;
4311
Francois Romieue3cf0cc2007-08-17 14:55:46 +02004312 if ((tp->mac_version == RTL_GIGA_MAC_VER_13) ||
4313 (tp->mac_version == RTL_GIGA_MAC_VER_16)) {
Francois Romieu9c14cea2008-07-05 00:21:15 +02004314 int cap = tp->pcie_cap;
4315
4316 if (cap) {
4317 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
4318 PCI_EXP_DEVCTL_NOSNOOP_EN);
4319 }
Francois Romieucdf1a602007-06-11 23:29:50 +02004320 }
4321
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004322 RTL_W8(Cfg9346, Cfg9346_Unlock);
4323
Francois Romieu2857ffb2008-08-02 21:08:49 +02004324 switch (tp->mac_version) {
4325 case RTL_GIGA_MAC_VER_07:
4326 rtl_hw_start_8102e_1(ioaddr, pdev);
4327 break;
4328
4329 case RTL_GIGA_MAC_VER_08:
4330 rtl_hw_start_8102e_3(ioaddr, pdev);
4331 break;
4332
4333 case RTL_GIGA_MAC_VER_09:
4334 rtl_hw_start_8102e_2(ioaddr, pdev);
4335 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004336
4337 case RTL_GIGA_MAC_VER_29:
4338 rtl_hw_start_8105e_1(ioaddr, pdev);
4339 break;
4340 case RTL_GIGA_MAC_VER_30:
4341 rtl_hw_start_8105e_2(ioaddr, pdev);
4342 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02004343 }
4344
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004345 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02004346
françois romieuf0298f82011-01-03 15:07:42 +00004347 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02004348
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004349 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02004350
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004351 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02004352 RTL_W16(CPlusCmd, tp->cp_cmd);
4353
4354 RTL_W16(IntrMitigate, 0x0000);
4355
4356 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4357
4358 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4359 rtl_set_rx_tx_config_registers(tp);
4360
Francois Romieucdf1a602007-06-11 23:29:50 +02004361 RTL_R8(IntrMask);
4362
Francois Romieucdf1a602007-06-11 23:29:50 +02004363 rtl_set_rx_mode(dev);
4364
4365 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004366
Francois Romieu0e485152007-02-20 00:00:26 +01004367 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368}
4369
4370static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
4371{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 if (new_mtu < ETH_ZLEN || new_mtu > SafeMtu)
4373 return -EINVAL;
4374
4375 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00004376 netdev_update_features(dev);
4377
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00004378 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379}
4380
4381static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
4382{
Al Viro95e09182007-12-22 18:55:39 +00004383 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
4385}
4386
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004387static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
4388 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004390 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004391 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004392
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004393 kfree(*data_buff);
4394 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 rtl8169_make_unusable_by_asic(desc);
4396}
4397
4398static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
4399{
4400 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
4401
4402 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
4403}
4404
4405static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
4406 u32 rx_buf_sz)
4407{
4408 desc->addr = cpu_to_le64(mapping);
4409 wmb();
4410 rtl8169_mark_to_asic(desc, rx_buf_sz);
4411}
4412
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004413static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004415 return (void *)ALIGN((long)data, 16);
4416}
4417
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004418static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
4419 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004420{
4421 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004423 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004424 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004425 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004427 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
4428 if (!data)
4429 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01004430
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004431 if (rtl8169_align(data) != data) {
4432 kfree(data);
4433 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
4434 if (!data)
4435 return NULL;
4436 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004437
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004438 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004439 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004440 if (unlikely(dma_mapping_error(d, mapping))) {
4441 if (net_ratelimit())
4442 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004443 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445
4446 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004447 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004448
4449err_out:
4450 kfree(data);
4451 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452}
4453
4454static void rtl8169_rx_clear(struct rtl8169_private *tp)
4455{
Francois Romieu07d3f512007-02-21 22:40:46 +01004456 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457
4458 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004459 if (tp->Rx_databuff[i]) {
4460 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 tp->RxDescArray + i);
4462 }
4463 }
4464}
4465
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004466static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004468 desc->opts1 |= cpu_to_le32(RingEnd);
4469}
Francois Romieu5b0384f2006-08-16 16:00:01 +02004470
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004471static int rtl8169_rx_fill(struct rtl8169_private *tp)
4472{
4473 unsigned int i;
4474
4475 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004476 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02004477
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004478 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004480
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004481 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004482 if (!data) {
4483 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004484 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004485 }
4486 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004489 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
4490 return 0;
4491
4492err_out:
4493 rtl8169_rx_clear(tp);
4494 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495}
4496
4497static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4498{
4499 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
4500}
4501
4502static int rtl8169_init_ring(struct net_device *dev)
4503{
4504 struct rtl8169_private *tp = netdev_priv(dev);
4505
4506 rtl8169_init_ring_indexes(tp);
4507
4508 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004509 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004511 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512}
4513
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004514static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 struct TxDesc *desc)
4516{
4517 unsigned int len = tx_skb->len;
4518
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004519 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
4520
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 desc->opts1 = 0x00;
4522 desc->opts2 = 0x00;
4523 desc->addr = 0x00;
4524 tx_skb->len = 0;
4525}
4526
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004527static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
4528 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529{
4530 unsigned int i;
4531
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004532 for (i = 0; i < n; i++) {
4533 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 struct ring_info *tx_skb = tp->tx_skb + entry;
4535 unsigned int len = tx_skb->len;
4536
4537 if (len) {
4538 struct sk_buff *skb = tx_skb->skb;
4539
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004540 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 tp->TxDescArray + entry);
4542 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00004543 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 dev_kfree_skb(skb);
4545 tx_skb->skb = NULL;
4546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 }
4548 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004549}
4550
4551static void rtl8169_tx_clear(struct rtl8169_private *tp)
4552{
4553 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554 tp->cur_tx = tp->dirty_tx = 0;
4555}
4556
David Howellsc4028952006-11-22 14:57:56 +00004557static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558{
4559 struct rtl8169_private *tp = netdev_priv(dev);
4560
David Howellsc4028952006-11-22 14:57:56 +00004561 PREPARE_DELAYED_WORK(&tp->task, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 schedule_delayed_work(&tp->task, 4);
4563}
4564
4565static void rtl8169_wait_for_quiescence(struct net_device *dev)
4566{
4567 struct rtl8169_private *tp = netdev_priv(dev);
4568 void __iomem *ioaddr = tp->mmio_addr;
4569
4570 synchronize_irq(dev->irq);
4571
4572 /* Wait for any pending NAPI task to complete */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004573 napi_disable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574
4575 rtl8169_irq_mask_and_ack(ioaddr);
4576
David S. Millerd1d08d12008-01-07 20:53:33 -08004577 tp->intr_mask = 0xffff;
4578 RTL_W16(IntrMask, tp->intr_event);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004579 napi_enable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580}
4581
David Howellsc4028952006-11-22 14:57:56 +00004582static void rtl8169_reinit_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583{
David Howellsc4028952006-11-22 14:57:56 +00004584 struct rtl8169_private *tp =
4585 container_of(work, struct rtl8169_private, task.work);
4586 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 int ret;
4588
Francois Romieueb2a0212007-02-15 23:37:21 +01004589 rtnl_lock();
4590
4591 if (!netif_running(dev))
4592 goto out_unlock;
4593
4594 rtl8169_wait_for_quiescence(dev);
4595 rtl8169_close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596
4597 ret = rtl8169_open(dev);
4598 if (unlikely(ret < 0)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004599 if (net_ratelimit())
4600 netif_err(tp, drv, dev,
4601 "reinit failure (status = %d). Rescheduling\n",
4602 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 rtl8169_schedule_work(dev, rtl8169_reinit_task);
4604 }
Francois Romieueb2a0212007-02-15 23:37:21 +01004605
4606out_unlock:
4607 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608}
4609
David Howellsc4028952006-11-22 14:57:56 +00004610static void rtl8169_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611{
David Howellsc4028952006-11-22 14:57:56 +00004612 struct rtl8169_private *tp =
4613 container_of(work, struct rtl8169_private, task.work);
4614 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615
Francois Romieueb2a0212007-02-15 23:37:21 +01004616 rtnl_lock();
4617
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 if (!netif_running(dev))
Francois Romieueb2a0212007-02-15 23:37:21 +01004619 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620
4621 rtl8169_wait_for_quiescence(dev);
4622
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004623 rtl8169_rx_interrupt(dev, tp, tp->mmio_addr, ~(u32)0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 rtl8169_tx_clear(tp);
4625
4626 if (tp->dirty_rx == tp->cur_rx) {
4627 rtl8169_init_ring_indexes(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01004628 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 netif_wake_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004630 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 } else {
Joe Perchesbf82c182010-02-09 11:49:50 +00004632 if (net_ratelimit())
4633 netif_emerg(tp, intr, dev, "Rx buffers shortage\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 rtl8169_schedule_work(dev, rtl8169_reset_task);
4635 }
Francois Romieueb2a0212007-02-15 23:37:21 +01004636
4637out_unlock:
4638 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639}
4640
4641static void rtl8169_tx_timeout(struct net_device *dev)
4642{
4643 struct rtl8169_private *tp = netdev_priv(dev);
4644
françois romieue6de30d2011-01-03 15:08:37 +00004645 rtl8169_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646
4647 /* Let's wait a bit while any (async) irq lands on */
4648 rtl8169_schedule_work(dev, rtl8169_reset_task);
4649}
4650
4651static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07004652 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653{
4654 struct skb_shared_info *info = skb_shinfo(skb);
4655 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04004656 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004657 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658
4659 entry = tp->cur_tx;
4660 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
4661 skb_frag_t *frag = info->frags + cur_frag;
4662 dma_addr_t mapping;
4663 u32 status, len;
4664 void *addr;
4665
4666 entry = (entry + 1) % NUM_TX_DESC;
4667
4668 txd = tp->TxDescArray + entry;
4669 len = frag->size;
4670 addr = ((void *) page_address(frag->page)) + frag->page_offset;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004671 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004672 if (unlikely(dma_mapping_error(d, mapping))) {
4673 if (net_ratelimit())
4674 netif_err(tp, drv, tp->dev,
4675 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004676 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678
4679 /* anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07004680 status = opts[0] | len |
4681 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682
4683 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07004684 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 txd->addr = cpu_to_le64(mapping);
4686
4687 tp->tx_skb[entry].len = len;
4688 }
4689
4690 if (cur_frag) {
4691 tp->tx_skb[entry].skb = skb;
4692 txd->opts1 |= cpu_to_le32(LastFrag);
4693 }
4694
4695 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004696
4697err_out:
4698 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
4699 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700}
4701
Francois Romieu2b7b4312011-04-18 22:53:24 -07004702static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
4703 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704{
Francois Romieu2b7b4312011-04-18 22:53:24 -07004705 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00004706 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07004707 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708
Francois Romieu2b7b4312011-04-18 22:53:24 -07004709 if (mss) {
4710 opts[0] |= TD_LSO;
4711 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
4712 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07004713 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714
4715 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07004716 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07004718 opts[offset] |= info->checksum.udp;
4719 else
4720 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722}
4723
Stephen Hemminger613573252009-08-31 19:50:58 +00004724static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
4725 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726{
4727 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004728 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 struct TxDesc *txd = tp->TxDescArray + entry;
4730 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004731 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 dma_addr_t mapping;
4733 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07004734 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004735 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02004736
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004738 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004739 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 }
4741
4742 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004743 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004745 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004746 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004747 if (unlikely(dma_mapping_error(d, mapping))) {
4748 if (net_ratelimit())
4749 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004750 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752
4753 tp->tx_skb[entry].len = len;
4754 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755
Francois Romieu2b7b4312011-04-18 22:53:24 -07004756 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
4757 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004758
Francois Romieu2b7b4312011-04-18 22:53:24 -07004759 rtl8169_tso_csum(tp, skb, opts);
4760
4761 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004762 if (frags < 0)
4763 goto err_dma_1;
4764 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07004765 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004766 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07004767 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004768 tp->tx_skb[entry].skb = skb;
4769 }
4770
Francois Romieu2b7b4312011-04-18 22:53:24 -07004771 txd->opts2 = cpu_to_le32(opts[1]);
4772
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 wmb();
4774
4775 /* anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07004776 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 txd->opts1 = cpu_to_le32(status);
4778
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 tp->cur_tx += frags + 1;
4780
David Dillow4c020a92010-03-03 16:33:10 +00004781 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782
Francois Romieu275391a2007-02-23 23:50:28 +01004783 RTL_W8(TxPoll, NPQ); /* set polling bit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784
4785 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
4786 netif_stop_queue(dev);
4787 smp_rmb();
4788 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
4789 netif_wake_queue(dev);
4790 }
4791
Stephen Hemminger613573252009-08-31 19:50:58 +00004792 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004794err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004795 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004796err_dma_0:
4797 dev_kfree_skb(skb);
4798 dev->stats.tx_dropped++;
4799 return NETDEV_TX_OK;
4800
4801err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004803 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00004804 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805}
4806
4807static void rtl8169_pcierr_interrupt(struct net_device *dev)
4808{
4809 struct rtl8169_private *tp = netdev_priv(dev);
4810 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 u16 pci_status, pci_cmd;
4812
4813 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
4814 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
4815
Joe Perchesbf82c182010-02-09 11:49:50 +00004816 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
4817 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818
4819 /*
4820 * The recovery sequence below admits a very elaborated explanation:
4821 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01004822 * - I did not see what else could be done;
4823 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 *
4825 * Feel free to adjust to your needs.
4826 */
Francois Romieua27993f2006-12-18 00:04:19 +01004827 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01004828 pci_cmd &= ~PCI_COMMAND_PARITY;
4829 else
4830 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
4831
4832 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833
4834 pci_write_config_word(pdev, PCI_STATUS,
4835 pci_status & (PCI_STATUS_DETECTED_PARITY |
4836 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
4837 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
4838
4839 /* The infamous DAC f*ckup only happens at boot time */
4840 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00004841 void __iomem *ioaddr = tp->mmio_addr;
4842
Joe Perchesbf82c182010-02-09 11:49:50 +00004843 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 tp->cp_cmd &= ~PCIDAC;
4845 RTL_W16(CPlusCmd, tp->cp_cmd);
4846 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 }
4848
françois romieue6de30d2011-01-03 15:08:37 +00004849 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01004850
4851 rtl8169_schedule_work(dev, rtl8169_reinit_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852}
4853
Francois Romieu07d3f512007-02-21 22:40:46 +01004854static void rtl8169_tx_interrupt(struct net_device *dev,
4855 struct rtl8169_private *tp,
4856 void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857{
4858 unsigned int dirty_tx, tx_left;
4859
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 dirty_tx = tp->dirty_tx;
4861 smp_rmb();
4862 tx_left = tp->cur_tx - dirty_tx;
4863
4864 while (tx_left > 0) {
4865 unsigned int entry = dirty_tx % NUM_TX_DESC;
4866 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 u32 status;
4868
4869 rmb();
4870 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
4871 if (status & DescOwn)
4872 break;
4873
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004874 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
4875 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 if (status & LastFrag) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00004877 dev->stats.tx_packets++;
4878 dev->stats.tx_bytes += tx_skb->skb->len;
Eric Dumazet87433bf2009-06-09 22:55:53 +00004879 dev_kfree_skb(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 tx_skb->skb = NULL;
4881 }
4882 dirty_tx++;
4883 tx_left--;
4884 }
4885
4886 if (tp->dirty_tx != dirty_tx) {
4887 tp->dirty_tx = dirty_tx;
4888 smp_wmb();
4889 if (netif_queue_stopped(dev) &&
4890 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
4891 netif_wake_queue(dev);
4892 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02004893 /*
4894 * 8168 hack: TxPoll requests are lost when the Tx packets are
4895 * too close. Let's kick an extra TxPoll request when a burst
4896 * of start_xmit activity is detected (if it is not detected,
4897 * it is slow enough). -- FR
4898 */
4899 smp_rmb();
4900 if (tp->cur_tx != dirty_tx)
4901 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 }
4903}
4904
Francois Romieu126fa4b2005-05-12 20:09:17 -04004905static inline int rtl8169_fragmented_frame(u32 status)
4906{
4907 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
4908}
4909
Eric Dumazetadea1ac72010-09-05 20:04:05 -07004910static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 u32 status = opts1 & RxProtoMask;
4913
4914 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00004915 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 skb->ip_summed = CHECKSUM_UNNECESSARY;
4917 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07004918 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919}
4920
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004921static struct sk_buff *rtl8169_try_rx_copy(void *data,
4922 struct rtl8169_private *tp,
4923 int pkt_size,
4924 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02004926 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004927 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004929 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004930 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004931 prefetch(data);
4932 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
4933 if (skb)
4934 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004935 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
4936
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004937 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938}
4939
Eric Dumazet630b9432010-03-31 02:08:31 +00004940/*
4941 * Warning : rtl8169_rx_interrupt() might be called :
4942 * 1) from NAPI (softirq) context
4943 * (polling = 1 : we should call netif_receive_skb())
4944 * 2) from process context (rtl8169_reset_task())
4945 * (polling = 0 : we must call netif_rx() instead)
4946 */
Francois Romieu07d3f512007-02-21 22:40:46 +01004947static int rtl8169_rx_interrupt(struct net_device *dev,
4948 struct rtl8169_private *tp,
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004949 void __iomem *ioaddr, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950{
4951 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004952 unsigned int count;
Eric Dumazet630b9432010-03-31 02:08:31 +00004953 int polling = (budget != ~(u32)0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 cur_rx = tp->cur_rx;
4956 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02004957 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958
Richard Dawe4dcb7d32005-05-27 21:12:00 +02004959 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04004961 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 u32 status;
4963
4964 rmb();
Francois Romieu126fa4b2005-05-12 20:09:17 -04004965 status = le32_to_cpu(desc->opts1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966
4967 if (status & DescOwn)
4968 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02004969 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004970 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
4971 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004972 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02004974 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02004976 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02004977 if (status & RxFOVF) {
4978 rtl8169_schedule_work(dev, rtl8169_reset_task);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004979 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02004980 }
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004981 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004983 struct sk_buff *skb;
Stephen Hemmingerb4496552007-06-17 01:06:49 +02004984 dma_addr_t addr = le64_to_cpu(desc->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 int pkt_size = (status & 0x00001FFF) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986
Francois Romieu126fa4b2005-05-12 20:09:17 -04004987 /*
4988 * The driver does not support incoming fragmented
4989 * frames. They are seen as a symptom of over-mtu
4990 * sized frames.
4991 */
4992 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02004993 dev->stats.rx_dropped++;
4994 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004995 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02004996 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04004997 }
4998
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004999 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5000 tp, pkt_size, addr);
5001 rtl8169_mark_to_asic(desc, rx_buf_sz);
5002 if (!skb) {
5003 dev->stats.rx_dropped++;
5004 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 }
5006
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005007 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008 skb_put(skb, pkt_size);
5009 skb->protocol = eth_type_trans(skb, dev);
5010
Francois Romieu7a8fc772011-03-01 17:18:33 +01005011 rtl8169_rx_vlan_tag(desc, skb);
5012
5013 if (likely(polling))
5014 napi_gro_receive(&tp->napi, skb);
5015 else
5016 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017
Francois Romieucebf8cc2007-10-18 12:06:54 +02005018 dev->stats.rx_bytes += pkt_size;
5019 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 }
Francois Romieu6dccd162007-02-13 23:38:05 +01005021
5022 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00005023 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01005024 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5025 desc->opts2 = 0;
5026 cur_rx++;
5027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 }
5029
5030 count = cur_rx - tp->cur_rx;
5031 tp->cur_rx = cur_rx;
5032
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005033 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034
5035 return count;
5036}
5037
Francois Romieu07d3f512007-02-21 22:40:46 +01005038static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039{
Francois Romieu07d3f512007-02-21 22:40:46 +01005040 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 int handled = 0;
Francois Romieu865c6522008-05-11 14:51:00 +02005044 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045
David Dillowf11a3772009-05-22 15:29:34 +00005046 /* loop handling interrupts until we have no new ones or
5047 * we hit a invalid/hotplug case.
5048 */
Francois Romieu865c6522008-05-11 14:51:00 +02005049 status = RTL_R16(IntrStatus);
David Dillowf11a3772009-05-22 15:29:34 +00005050 while (status && status != 0xffff) {
5051 handled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052
David Dillowf11a3772009-05-22 15:29:34 +00005053 /* Handle all of the error cases first. These will reset
5054 * the chip, so just exit the loop.
5055 */
5056 if (unlikely(!netif_running(dev))) {
5057 rtl8169_asic_down(ioaddr);
5058 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 }
David Dillowf11a3772009-05-22 15:29:34 +00005060
Francois Romieu1519e572011-02-03 12:02:36 +01005061 if (unlikely(status & RxFIFOOver)) {
5062 switch (tp->mac_version) {
5063 /* Work around for rx fifo overflow */
5064 case RTL_GIGA_MAC_VER_11:
5065 case RTL_GIGA_MAC_VER_22:
5066 case RTL_GIGA_MAC_VER_26:
5067 netif_stop_queue(dev);
5068 rtl8169_tx_timeout(dev);
5069 goto done;
Francois Romieuf60ac8e2011-02-03 17:27:52 +01005070 /* Testers needed. */
5071 case RTL_GIGA_MAC_VER_17:
5072 case RTL_GIGA_MAC_VER_19:
5073 case RTL_GIGA_MAC_VER_20:
5074 case RTL_GIGA_MAC_VER_21:
5075 case RTL_GIGA_MAC_VER_23:
5076 case RTL_GIGA_MAC_VER_24:
5077 case RTL_GIGA_MAC_VER_27:
5078 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00005079 case RTL_GIGA_MAC_VER_31:
Francois Romieu1519e572011-02-03 12:02:36 +01005080 /* Experimental science. Pktgen proof. */
5081 case RTL_GIGA_MAC_VER_12:
5082 case RTL_GIGA_MAC_VER_25:
5083 if (status == RxFIFOOver)
5084 goto done;
5085 break;
5086 default:
5087 break;
5088 }
David Dillowf11a3772009-05-22 15:29:34 +00005089 }
5090
5091 if (unlikely(status & SYSErr)) {
5092 rtl8169_pcierr_interrupt(dev);
5093 break;
5094 }
5095
5096 if (status & LinkChg)
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005097 __rtl8169_check_link_status(dev, tp, ioaddr, true);
David Dillowf11a3772009-05-22 15:29:34 +00005098
5099 /* We need to see the lastest version of tp->intr_mask to
5100 * avoid ignoring an MSI interrupt and having to wait for
5101 * another event which may never come.
5102 */
5103 smp_rmb();
5104 if (status & tp->intr_mask & tp->napi_event) {
5105 RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
5106 tp->intr_mask = ~tp->napi_event;
5107
5108 if (likely(napi_schedule_prep(&tp->napi)))
5109 __napi_schedule(&tp->napi);
Joe Perchesbf82c182010-02-09 11:49:50 +00005110 else
5111 netif_info(tp, intr, dev,
5112 "interrupt %04x in poll\n", status);
David Dillowf11a3772009-05-22 15:29:34 +00005113 }
5114
5115 /* We only get a new MSI interrupt when all active irq
5116 * sources on the chip have been acknowledged. So, ack
5117 * everything we've seen and check if new sources have become
5118 * active to avoid blocking all interrupts from the chip.
5119 */
5120 RTL_W16(IntrStatus,
5121 (status & RxFIFOOver) ? (status | RxOverflow) : status);
5122 status = RTL_R16(IntrStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 }
Francois Romieu1519e572011-02-03 12:02:36 +01005124done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 return IRQ_RETVAL(handled);
5126}
5127
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005128static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005130 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5131 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132 void __iomem *ioaddr = tp->mmio_addr;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005133 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005135 work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 rtl8169_tx_interrupt(dev, tp, ioaddr);
5137
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005138 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005139 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00005140
5141 /* We need for force the visibility of tp->intr_mask
5142 * for other CPUs, as we can loose an MSI interrupt
5143 * and potentially wait for a retransmit timeout if we don't.
5144 * The posted write to IntrMask is safe, as it will
5145 * eventually make it to the chip and we won't loose anything
5146 * until it does.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 */
David Dillowf11a3772009-05-22 15:29:34 +00005148 tp->intr_mask = 0xffff;
David Dillow4c020a92010-03-03 16:33:10 +00005149 wmb();
Francois Romieu0e485152007-02-20 00:00:26 +01005150 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151 }
5152
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005153 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155
Francois Romieu523a6092008-09-10 22:28:56 +02005156static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5157{
5158 struct rtl8169_private *tp = netdev_priv(dev);
5159
5160 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5161 return;
5162
5163 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5164 RTL_W32(RxMissed, 0);
5165}
5166
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167static void rtl8169_down(struct net_device *dev)
5168{
5169 struct rtl8169_private *tp = netdev_priv(dev);
5170 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171
5172 rtl8169_delete_timer(dev);
5173
5174 netif_stop_queue(dev);
5175
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005176 napi_disable(&tp->napi);
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005177
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 spin_lock_irq(&tp->lock);
5179
5180 rtl8169_asic_down(ioaddr);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005181 /*
5182 * At this point device interrupts can not be enabled in any function,
5183 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task,
5184 * rtl8169_reinit_task) and napi is disabled (rtl8169_poll).
5185 */
Francois Romieu523a6092008-09-10 22:28:56 +02005186 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187
5188 spin_unlock_irq(&tp->lock);
5189
5190 synchronize_irq(dev->irq);
5191
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192 /* Give a racing hard_start_xmit a few cycles to complete. */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07005193 synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 rtl8169_tx_clear(tp);
5196
5197 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00005198
5199 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200}
5201
5202static int rtl8169_close(struct net_device *dev)
5203{
5204 struct rtl8169_private *tp = netdev_priv(dev);
5205 struct pci_dev *pdev = tp->pci_dev;
5206
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005207 pm_runtime_get_sync(&pdev->dev);
5208
Ivan Vecera355423d2009-02-06 21:49:57 -08005209 /* update counters before going down */
5210 rtl8169_update_counters(dev);
5211
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212 rtl8169_down(dev);
5213
5214 free_irq(dev->irq, dev);
5215
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00005216 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5217 tp->RxPhyAddr);
5218 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5219 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 tp->TxDescArray = NULL;
5221 tp->RxDescArray = NULL;
5222
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005223 pm_runtime_put_sync(&pdev->dev);
5224
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225 return 0;
5226}
5227
Francois Romieu07ce4062007-02-23 23:36:39 +01005228static void rtl_set_rx_mode(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229{
5230 struct rtl8169_private *tp = netdev_priv(dev);
5231 void __iomem *ioaddr = tp->mmio_addr;
5232 unsigned long flags;
5233 u32 mc_filter[2]; /* Multicast hash filter */
Francois Romieu07d3f512007-02-21 22:40:46 +01005234 int rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 u32 tmp = 0;
5236
5237 if (dev->flags & IFF_PROMISC) {
5238 /* Unconditionally log net taps. */
Joe Perchesbf82c182010-02-09 11:49:50 +00005239 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240 rx_mode =
5241 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5242 AcceptAllPhys;
5243 mc_filter[1] = mc_filter[0] = 0xffffffff;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00005244 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
Joe Perches8e95a202009-12-03 07:58:21 +00005245 (dev->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 /* Too many to filter perfectly -- accept all multicasts. */
5247 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5248 mc_filter[1] = mc_filter[0] = 0xffffffff;
5249 } else {
Jiri Pirko22bedad2010-04-01 21:22:57 +00005250 struct netdev_hw_addr *ha;
Francois Romieu07d3f512007-02-21 22:40:46 +01005251
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252 rx_mode = AcceptBroadcast | AcceptMyPhys;
5253 mc_filter[1] = mc_filter[0] = 0;
Jiri Pirko22bedad2010-04-01 21:22:57 +00005254 netdev_for_each_mc_addr(ha, dev) {
5255 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5257 rx_mode |= AcceptMulticast;
5258 }
5259 }
5260
5261 spin_lock_irqsave(&tp->lock, flags);
5262
5263 tmp = rtl8169_rx_config | rx_mode |
Francois Romieu2b7b4312011-04-18 22:53:24 -07005264 (RTL_R32(RxConfig) & RTL_RX_CONFIG_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265
Francois Romieuf887cce2008-07-17 22:24:18 +02005266 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
Francois Romieu1087f4f2007-12-26 22:46:05 +01005267 u32 data = mc_filter[0];
5268
5269 mc_filter[0] = swab32(mc_filter[1]);
5270 mc_filter[1] = swab32(data);
Francois Romieubcf0bf92006-07-26 23:14:13 +02005271 }
5272
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273 RTL_W32(MAR0 + 4, mc_filter[1]);
Francois Romieu78f1cd02010-03-27 19:35:46 -07005274 RTL_W32(MAR0 + 0, mc_filter[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275
Francois Romieu57a9f232007-06-04 22:10:15 +02005276 RTL_W32(RxConfig, tmp);
5277
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278 spin_unlock_irqrestore(&tp->lock, flags);
5279}
5280
5281/**
5282 * rtl8169_get_stats - Get rtl8169 read/write statistics
5283 * @dev: The Ethernet Device to get statistics for
5284 *
5285 * Get TX/RX statistics for rtl8169
5286 */
5287static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
5288{
5289 struct rtl8169_private *tp = netdev_priv(dev);
5290 void __iomem *ioaddr = tp->mmio_addr;
5291 unsigned long flags;
5292
5293 if (netif_running(dev)) {
5294 spin_lock_irqsave(&tp->lock, flags);
Francois Romieu523a6092008-09-10 22:28:56 +02005295 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 spin_unlock_irqrestore(&tp->lock, flags);
5297 }
Francois Romieu5b0384f2006-08-16 16:00:01 +02005298
Francois Romieucebf8cc2007-10-18 12:06:54 +02005299 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300}
5301
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005302static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01005303{
françois romieu065c27c2011-01-03 15:08:12 +00005304 struct rtl8169_private *tp = netdev_priv(dev);
5305
Francois Romieu5d06a992006-02-23 00:47:58 +01005306 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005307 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01005308
françois romieu065c27c2011-01-03 15:08:12 +00005309 rtl_pll_power_down(tp);
5310
Francois Romieu5d06a992006-02-23 00:47:58 +01005311 netif_device_detach(dev);
5312 netif_stop_queue(dev);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005313}
Francois Romieu5d06a992006-02-23 00:47:58 +01005314
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005315#ifdef CONFIG_PM
5316
5317static int rtl8169_suspend(struct device *device)
5318{
5319 struct pci_dev *pdev = to_pci_dev(device);
5320 struct net_device *dev = pci_get_drvdata(pdev);
5321
5322 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02005323
Francois Romieu5d06a992006-02-23 00:47:58 +01005324 return 0;
5325}
5326
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005327static void __rtl8169_resume(struct net_device *dev)
5328{
françois romieu065c27c2011-01-03 15:08:12 +00005329 struct rtl8169_private *tp = netdev_priv(dev);
5330
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005331 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00005332
5333 rtl_pll_power_up(tp);
5334
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005335 rtl8169_schedule_work(dev, rtl8169_reset_task);
5336}
5337
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005338static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01005339{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005340 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01005341 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005342 struct rtl8169_private *tp = netdev_priv(dev);
5343
5344 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01005345
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005346 if (netif_running(dev))
5347 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01005348
Francois Romieu5d06a992006-02-23 00:47:58 +01005349 return 0;
5350}
5351
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005352static int rtl8169_runtime_suspend(struct device *device)
5353{
5354 struct pci_dev *pdev = to_pci_dev(device);
5355 struct net_device *dev = pci_get_drvdata(pdev);
5356 struct rtl8169_private *tp = netdev_priv(dev);
5357
5358 if (!tp->TxDescArray)
5359 return 0;
5360
5361 spin_lock_irq(&tp->lock);
5362 tp->saved_wolopts = __rtl8169_get_wol(tp);
5363 __rtl8169_set_wol(tp, WAKE_ANY);
5364 spin_unlock_irq(&tp->lock);
5365
5366 rtl8169_net_suspend(dev);
5367
5368 return 0;
5369}
5370
5371static int rtl8169_runtime_resume(struct device *device)
5372{
5373 struct pci_dev *pdev = to_pci_dev(device);
5374 struct net_device *dev = pci_get_drvdata(pdev);
5375 struct rtl8169_private *tp = netdev_priv(dev);
5376
5377 if (!tp->TxDescArray)
5378 return 0;
5379
5380 spin_lock_irq(&tp->lock);
5381 __rtl8169_set_wol(tp, tp->saved_wolopts);
5382 tp->saved_wolopts = 0;
5383 spin_unlock_irq(&tp->lock);
5384
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005385 rtl8169_init_phy(dev, tp);
5386
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005387 __rtl8169_resume(dev);
5388
5389 return 0;
5390}
5391
5392static int rtl8169_runtime_idle(struct device *device)
5393{
5394 struct pci_dev *pdev = to_pci_dev(device);
5395 struct net_device *dev = pci_get_drvdata(pdev);
5396 struct rtl8169_private *tp = netdev_priv(dev);
5397
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005398 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005399}
5400
Alexey Dobriyan47145212009-12-14 18:00:08 -08005401static const struct dev_pm_ops rtl8169_pm_ops = {
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005402 .suspend = rtl8169_suspend,
5403 .resume = rtl8169_resume,
5404 .freeze = rtl8169_suspend,
5405 .thaw = rtl8169_resume,
5406 .poweroff = rtl8169_suspend,
5407 .restore = rtl8169_resume,
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005408 .runtime_suspend = rtl8169_runtime_suspend,
5409 .runtime_resume = rtl8169_runtime_resume,
5410 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005411};
5412
5413#define RTL8169_PM_OPS (&rtl8169_pm_ops)
5414
5415#else /* !CONFIG_PM */
5416
5417#define RTL8169_PM_OPS NULL
5418
5419#endif /* !CONFIG_PM */
5420
Francois Romieu1765f952008-09-13 17:21:40 +02005421static void rtl_shutdown(struct pci_dev *pdev)
5422{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005423 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00005424 struct rtl8169_private *tp = netdev_priv(dev);
5425 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu1765f952008-09-13 17:21:40 +02005426
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005427 rtl8169_net_suspend(dev);
5428
Ivan Veceracc098dc2009-11-29 23:12:52 -08005429 /* restore original MAC address */
5430 rtl_rar_set(tp, dev->perm_addr);
5431
françois romieu4bb3f522009-06-17 11:41:45 +00005432 spin_lock_irq(&tp->lock);
5433
5434 rtl8169_asic_down(ioaddr);
5435
5436 spin_unlock_irq(&tp->lock);
5437
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005438 if (system_state == SYSTEM_POWER_OFF) {
françois romieuca52efd2009-07-24 12:34:19 +00005439 /* WoL fails with some 8168 when the receiver is disabled. */
5440 if (tp->features & RTL_FEATURE_WOL) {
5441 pci_clear_master(pdev);
5442
5443 RTL_W8(ChipCmd, CmdRxEnb);
5444 /* PCI commit */
5445 RTL_R8(ChipCmd);
5446 }
5447
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005448 pci_wake_from_d3(pdev, true);
5449 pci_set_power_state(pdev, PCI_D3hot);
5450 }
5451}
Francois Romieu5d06a992006-02-23 00:47:58 +01005452
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453static struct pci_driver rtl8169_pci_driver = {
5454 .name = MODULENAME,
5455 .id_table = rtl8169_pci_tbl,
5456 .probe = rtl8169_init_one,
5457 .remove = __devexit_p(rtl8169_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02005458 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005459 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460};
5461
Francois Romieu07d3f512007-02-21 22:40:46 +01005462static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463{
Jeff Garzik29917622006-08-19 17:48:59 -04005464 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465}
5466
Francois Romieu07d3f512007-02-21 22:40:46 +01005467static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468{
5469 pci_unregister_driver(&rtl8169_pci_driver);
5470}
5471
5472module_init(rtl8169_init_module);
5473module_exit(rtl8169_cleanup_module);