blob: c51515f53df1fb5510e33d2c6ee154ad6e90622c [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. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200348#define 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 */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200426 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 */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200597 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000598 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700599 struct napi_struct napi;
Francois Romieucecb5fd2011-04-01 10:21:07 +0200600 spinlock_t lock;
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
Francois Romieucecb5fd2011-04-01 10:21:07 +0200733static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
734{
735 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
736}
737
françois romieub646d902011-01-03 15:08:21 +0000738static void rtl8168_driver_start(struct rtl8169_private *tp)
739{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200740 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000741 int i;
742
743 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
744
Francois Romieucecb5fd2011-04-01 10:21:07 +0200745 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000746
françois romieub646d902011-01-03 15:08:21 +0000747 for (i = 0; i < 10; i++) {
748 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000749 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
françois romieub646d902011-01-03 15:08:21 +0000750 break;
751 }
752}
753
754static void rtl8168_driver_stop(struct rtl8169_private *tp)
755{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200756 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000757 int i;
758
759 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
760
Francois Romieucecb5fd2011-04-01 10:21:07 +0200761 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000762
françois romieub646d902011-01-03 15:08:21 +0000763 for (i = 0; i < 10; i++) {
764 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000765 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
françois romieub646d902011-01-03 15:08:21 +0000766 break;
767 }
768}
769
hayeswang4804b3b2011-03-21 01:50:29 +0000770static int r8168dp_check_dash(struct rtl8169_private *tp)
771{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200772 u16 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000773
Francois Romieucecb5fd2011-04-01 10:21:07 +0200774 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
hayeswang4804b3b2011-03-21 01:50:29 +0000775}
françois romieub646d902011-01-03 15:08:21 +0000776
françois romieu4da19632011-01-03 15:07:55 +0000777static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
779 int i;
780
Francois Romieua6baf3a2007-11-08 23:23:21 +0100781 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Francois Romieu23714082006-01-29 00:49:09 +0100783 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100784 /*
785 * Check if the RTL8169 has completed writing to the specified
786 * MII register.
787 */
Francois Romieu5b0384f2006-08-16 16:00:01 +0200788 if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 break;
Francois Romieu23714082006-01-29 00:49:09 +0100790 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
Timo Teräs024a07b2010-06-06 15:38:47 -0700792 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700793 * According to hardware specs a 20us delay is required after write
794 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700795 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700796 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798
françois romieu4da19632011-01-03 15:07:55 +0000799static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
801 int i, value = -1;
802
Francois Romieua6baf3a2007-11-08 23:23:21 +0100803 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Francois Romieu23714082006-01-29 00:49:09 +0100805 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100806 /*
807 * Check if the RTL8169 has completed retrieving data from
808 * the specified MII register.
809 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 if (RTL_R32(PHYAR) & 0x80000000) {
Francois Romieua6baf3a2007-11-08 23:23:21 +0100811 value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 break;
813 }
Francois Romieu23714082006-01-29 00:49:09 +0100814 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
Timo Teräs81a95f02010-06-09 17:31:48 -0700816 /*
817 * According to hardware specs a 20us delay is required after read
818 * complete indication, but before sending next command.
819 */
820 udelay(20);
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return value;
823}
824
françois romieuc0e45c12011-01-03 15:08:04 +0000825static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
826{
827 int i;
828
829 RTL_W32(OCPDR, data |
830 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
831 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
832 RTL_W32(EPHY_RXER_NUM, 0);
833
834 for (i = 0; i < 100; i++) {
835 mdelay(1);
836 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
837 break;
838 }
839}
840
841static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
842{
843 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
844 (value & OCPDR_DATA_MASK));
845}
846
847static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
848{
849 int i;
850
851 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
852
853 mdelay(1);
854 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
855 RTL_W32(EPHY_RXER_NUM, 0);
856
857 for (i = 0; i < 100; i++) {
858 mdelay(1);
859 if (RTL_R32(OCPAR) & OCPAR_FLAG)
860 break;
861 }
862
863 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
864}
865
françois romieue6de30d2011-01-03 15:08:37 +0000866#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
867
868static void r8168dp_2_mdio_start(void __iomem *ioaddr)
869{
870 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
871}
872
873static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
874{
875 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
876}
877
878static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
879{
880 r8168dp_2_mdio_start(ioaddr);
881
882 r8169_mdio_write(ioaddr, reg_addr, value);
883
884 r8168dp_2_mdio_stop(ioaddr);
885}
886
887static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
888{
889 int value;
890
891 r8168dp_2_mdio_start(ioaddr);
892
893 value = r8169_mdio_read(ioaddr, reg_addr);
894
895 r8168dp_2_mdio_stop(ioaddr);
896
897 return value;
898}
899
françois romieu4da19632011-01-03 15:07:55 +0000900static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +0200901{
françois romieuc0e45c12011-01-03 15:08:04 +0000902 tp->mdio_ops.write(tp->mmio_addr, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +0200903}
904
françois romieu4da19632011-01-03 15:07:55 +0000905static int rtl_readphy(struct rtl8169_private *tp, int location)
906{
françois romieuc0e45c12011-01-03 15:08:04 +0000907 return tp->mdio_ops.read(tp->mmio_addr, location);
françois romieu4da19632011-01-03 15:07:55 +0000908}
909
910static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
911{
912 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
913}
914
915static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +0000916{
917 int val;
918
françois romieu4da19632011-01-03 15:07:55 +0000919 val = rtl_readphy(tp, reg_addr);
920 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +0000921}
922
Francois Romieuccdffb92008-07-26 14:26:06 +0200923static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
924 int val)
925{
926 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +0200927
françois romieu4da19632011-01-03 15:07:55 +0000928 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +0200929}
930
931static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
932{
933 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +0200934
françois romieu4da19632011-01-03 15:07:55 +0000935 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +0200936}
937
Francois Romieudacf8152008-08-02 20:44:13 +0200938static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
939{
940 unsigned int i;
941
942 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
943 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
944
945 for (i = 0; i < 100; i++) {
946 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
947 break;
948 udelay(10);
949 }
950}
951
952static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
953{
954 u16 value = 0xffff;
955 unsigned int i;
956
957 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
958
959 for (i = 0; i < 100; i++) {
960 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
961 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
962 break;
963 }
964 udelay(10);
965 }
966
967 return value;
968}
969
970static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
971{
972 unsigned int i;
973
974 RTL_W32(CSIDR, value);
975 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
976 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
977
978 for (i = 0; i < 100; i++) {
979 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
980 break;
981 udelay(10);
982 }
983}
984
985static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
986{
987 u32 value = ~0x00;
988 unsigned int i;
989
990 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
991 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
992
993 for (i = 0; i < 100; i++) {
994 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
995 value = RTL_R32(CSIDR);
996 break;
997 }
998 udelay(10);
999 }
1000
1001 return value;
1002}
1003
françois romieudaf9df62009-10-07 12:44:20 +00001004static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1005{
1006 u8 value = 0xff;
1007 unsigned int i;
1008
1009 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1010
1011 for (i = 0; i < 300; i++) {
1012 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1013 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1014 break;
1015 }
1016 udelay(100);
1017 }
1018
1019 return value;
1020}
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022static void rtl8169_irq_mask_and_ack(void __iomem *ioaddr)
1023{
1024 RTL_W16(IntrMask, 0x0000);
1025
1026 RTL_W16(IntrStatus, 0xffff);
1027}
1028
1029static void rtl8169_asic_down(void __iomem *ioaddr)
1030{
1031 RTL_W8(ChipCmd, 0x00);
1032 rtl8169_irq_mask_and_ack(ioaddr);
1033 RTL_R16(CPlusCmd);
1034}
1035
françois romieu4da19632011-01-03 15:07:55 +00001036static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
françois romieu4da19632011-01-03 15:07:55 +00001038 void __iomem *ioaddr = tp->mmio_addr;
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 return RTL_R32(TBICSR) & TBIReset;
1041}
1042
françois romieu4da19632011-01-03 15:07:55 +00001043static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
françois romieu4da19632011-01-03 15:07:55 +00001045 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046}
1047
1048static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1049{
1050 return RTL_R32(TBICSR) & TBILinkOk;
1051}
1052
1053static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1054{
1055 return RTL_R8(PHYstatus) & LinkStatus;
1056}
1057
françois romieu4da19632011-01-03 15:07:55 +00001058static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
françois romieu4da19632011-01-03 15:07:55 +00001060 void __iomem *ioaddr = tp->mmio_addr;
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1063}
1064
françois romieu4da19632011-01-03 15:07:55 +00001065static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
1067 unsigned int val;
1068
françois romieu4da19632011-01-03 15:07:55 +00001069 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1070 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071}
1072
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001073static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001074 struct rtl8169_private *tp,
1075 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
1077 unsigned long flags;
1078
1079 spin_lock_irqsave(&tp->lock, flags);
1080 if (tp->link_ok(ioaddr)) {
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001081 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001082 if (pm)
1083 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001085 if (net_ratelimit())
1086 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001087 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001089 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001090 if (pm)
1091 pm_schedule_suspend(&tp->pci_dev->dev, 100);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 spin_unlock_irqrestore(&tp->lock, flags);
1094}
1095
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001096static void rtl8169_check_link_status(struct net_device *dev,
1097 struct rtl8169_private *tp,
1098 void __iomem *ioaddr)
1099{
1100 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1101}
1102
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001103#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1104
1105static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1106{
1107 void __iomem *ioaddr = tp->mmio_addr;
1108 u8 options;
1109 u32 wolopts = 0;
1110
1111 options = RTL_R8(Config1);
1112 if (!(options & PMEnable))
1113 return 0;
1114
1115 options = RTL_R8(Config3);
1116 if (options & LinkUp)
1117 wolopts |= WAKE_PHY;
1118 if (options & MagicPacket)
1119 wolopts |= WAKE_MAGIC;
1120
1121 options = RTL_R8(Config5);
1122 if (options & UWF)
1123 wolopts |= WAKE_UCAST;
1124 if (options & BWF)
1125 wolopts |= WAKE_BCAST;
1126 if (options & MWF)
1127 wolopts |= WAKE_MCAST;
1128
1129 return wolopts;
1130}
1131
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001132static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1133{
1134 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001135
1136 spin_lock_irq(&tp->lock);
1137
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001138 wol->supported = WAKE_ANY;
1139 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001140
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001141 spin_unlock_irq(&tp->lock);
1142}
1143
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001144static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001145{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001146 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001147 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001148 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001149 u32 opt;
1150 u16 reg;
1151 u8 mask;
1152 } cfg[] = {
1153 { WAKE_ANY, Config1, PMEnable },
1154 { WAKE_PHY, Config3, LinkUp },
1155 { WAKE_MAGIC, Config3, MagicPacket },
1156 { WAKE_UCAST, Config5, UWF },
1157 { WAKE_BCAST, Config5, BWF },
1158 { WAKE_MCAST, Config5, MWF },
1159 { WAKE_ANY, Config5, LanWake }
1160 };
1161
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001162 RTL_W8(Cfg9346, Cfg9346_Unlock);
1163
1164 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1165 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001166 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001167 options |= cfg[i].mask;
1168 RTL_W8(cfg[i].reg, options);
1169 }
1170
1171 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001172}
1173
1174static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1175{
1176 struct rtl8169_private *tp = netdev_priv(dev);
1177
1178 spin_lock_irq(&tp->lock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001179
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001180 if (wol->wolopts)
1181 tp->features |= RTL_FEATURE_WOL;
1182 else
1183 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001184 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001185 spin_unlock_irq(&tp->lock);
1186
françois romieuea809072010-11-08 13:23:58 +00001187 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1188
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001189 return 0;
1190}
1191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192static void rtl8169_get_drvinfo(struct net_device *dev,
1193 struct ethtool_drvinfo *info)
1194{
1195 struct rtl8169_private *tp = netdev_priv(dev);
1196
1197 strcpy(info->driver, MODULENAME);
1198 strcpy(info->version, RTL8169_VERSION);
1199 strcpy(info->bus_info, pci_name(tp->pci_dev));
1200}
1201
1202static int rtl8169_get_regs_len(struct net_device *dev)
1203{
1204 return R8169_REGS_SIZE;
1205}
1206
1207static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001208 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
1210 struct rtl8169_private *tp = netdev_priv(dev);
1211 void __iomem *ioaddr = tp->mmio_addr;
1212 int ret = 0;
1213 u32 reg;
1214
1215 reg = RTL_R32(TBICSR);
1216 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1217 (duplex == DUPLEX_FULL)) {
1218 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1219 } else if (autoneg == AUTONEG_ENABLE)
1220 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1221 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001222 netif_warn(tp, link, dev,
1223 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 ret = -EOPNOTSUPP;
1225 }
1226
1227 return ret;
1228}
1229
1230static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001231 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
1233 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001234 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001235 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Hayes Wang716b50a2011-02-22 17:26:18 +08001237 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
1239 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001240 int auto_nego;
1241
françois romieu4da19632011-01-03 15:07:55 +00001242 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001243 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1244 ADVERTISE_100HALF | ADVERTISE_100FULL);
1245
1246 if (adv & ADVERTISED_10baseT_Half)
1247 auto_nego |= ADVERTISE_10HALF;
1248 if (adv & ADVERTISED_10baseT_Full)
1249 auto_nego |= ADVERTISE_10FULL;
1250 if (adv & ADVERTISED_100baseT_Half)
1251 auto_nego |= ADVERTISE_100HALF;
1252 if (adv & ADVERTISED_100baseT_Full)
1253 auto_nego |= ADVERTISE_100FULL;
1254
françois romieu3577aa12009-05-19 10:46:48 +00001255 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1256
françois romieu4da19632011-01-03 15:07:55 +00001257 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001258 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1259
1260 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +02001261 if (tp->mac_version != RTL_GIGA_MAC_VER_07 &&
1262 tp->mac_version != RTL_GIGA_MAC_VER_08 &&
1263 tp->mac_version != RTL_GIGA_MAC_VER_09 &&
1264 tp->mac_version != RTL_GIGA_MAC_VER_10 &&
1265 tp->mac_version != RTL_GIGA_MAC_VER_13 &&
1266 tp->mac_version != RTL_GIGA_MAC_VER_14 &&
1267 tp->mac_version != RTL_GIGA_MAC_VER_15 &&
1268 tp->mac_version != RTL_GIGA_MAC_VER_16 &&
1269 tp->mac_version != RTL_GIGA_MAC_VER_29 &&
1270 tp->mac_version != RTL_GIGA_MAC_VER_30) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001271 if (adv & ADVERTISED_1000baseT_Half)
1272 giga_ctrl |= ADVERTISE_1000HALF;
1273 if (adv & ADVERTISED_1000baseT_Full)
1274 giga_ctrl |= ADVERTISE_1000FULL;
1275 } else if (adv & (ADVERTISED_1000baseT_Half |
1276 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001277 netif_info(tp, link, dev,
1278 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001279 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
françois romieu3577aa12009-05-19 10:46:48 +00001282 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001283
françois romieu4da19632011-01-03 15:07:55 +00001284 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1285 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001286 } else {
1287 giga_ctrl = 0;
1288
1289 if (speed == SPEED_10)
1290 bmcr = 0;
1291 else if (speed == SPEED_100)
1292 bmcr = BMCR_SPEED100;
1293 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001294 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001295
1296 if (duplex == DUPLEX_FULL)
1297 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001298 }
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 tp->phy_1000_ctrl_reg = giga_ctrl;
1301
françois romieu4da19632011-01-03 15:07:55 +00001302 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001303
Francois Romieucecb5fd2011-04-01 10:21:07 +02001304 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1305 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001306 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001307 rtl_writephy(tp, 0x17, 0x2138);
1308 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001309 } else {
françois romieu4da19632011-01-03 15:07:55 +00001310 rtl_writephy(tp, 0x17, 0x2108);
1311 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001312 }
1313 }
1314
Oliver Neukum54405cd2011-01-06 21:55:13 +01001315 rc = 0;
1316out:
1317 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318}
1319
1320static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001321 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
1323 struct rtl8169_private *tp = netdev_priv(dev);
1324 int ret;
1325
Oliver Neukum54405cd2011-01-06 21:55:13 +01001326 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Francois Romieu64e4bfb2006-08-17 12:43:06 +02001328 if (netif_running(dev) && (tp->phy_1000_ctrl_reg & ADVERTISE_1000FULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
1330
1331 return ret;
1332}
1333
1334static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1335{
1336 struct rtl8169_private *tp = netdev_priv(dev);
1337 unsigned long flags;
1338 int ret;
1339
1340 spin_lock_irqsave(&tp->lock, flags);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001341 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001342 cmd->duplex, cmd->advertising);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001344
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 return ret;
1346}
1347
Michał Mirosław350fb322011-04-08 06:35:56 +00001348static u32 rtl8169_fix_features(struct net_device *dev, u32 features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
Francois Romieu2b7b4312011-04-18 22:53:24 -07001350 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001351 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Michał Mirosław350fb322011-04-08 06:35:56 +00001353 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354}
1355
Michał Mirosław350fb322011-04-08 06:35:56 +00001356static int rtl8169_set_features(struct net_device *dev, u32 features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
1358 struct rtl8169_private *tp = netdev_priv(dev);
1359 void __iomem *ioaddr = tp->mmio_addr;
1360 unsigned long flags;
1361
1362 spin_lock_irqsave(&tp->lock, flags);
1363
Michał Mirosław350fb322011-04-08 06:35:56 +00001364 if (features & NETIF_F_RXCSUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 tp->cp_cmd |= RxChkSum;
1366 else
1367 tp->cp_cmd &= ~RxChkSum;
1368
Michał Mirosław350fb322011-04-08 06:35:56 +00001369 if (dev->features & NETIF_F_HW_VLAN_RX)
1370 tp->cp_cmd |= RxVlan;
1371 else
1372 tp->cp_cmd &= ~RxVlan;
1373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 RTL_W16(CPlusCmd, tp->cp_cmd);
1375 RTL_R16(CPlusCmd);
1376
1377 spin_unlock_irqrestore(&tp->lock, flags);
1378
1379 return 0;
1380}
1381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1383 struct sk_buff *skb)
1384{
Jesse Grosseab6d182010-10-20 13:56:03 +00001385 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1387}
1388
Francois Romieu7a8fc772011-03-01 17:18:33 +01001389static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
1391 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Francois Romieu7a8fc772011-03-01 17:18:33 +01001393 if (opts2 & RxVlanTag)
1394 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397}
1398
Francois Romieuccdffb92008-07-26 14:26:06 +02001399static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
1401 struct rtl8169_private *tp = netdev_priv(dev);
1402 void __iomem *ioaddr = tp->mmio_addr;
1403 u32 status;
1404
1405 cmd->supported =
1406 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1407 cmd->port = PORT_FIBRE;
1408 cmd->transceiver = XCVR_INTERNAL;
1409
1410 status = RTL_R32(TBICSR);
1411 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1412 cmd->autoneg = !!(status & TBINwEnable);
1413
David Decotigny70739492011-04-27 18:32:40 +00001414 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001416
1417 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418}
1419
Francois Romieuccdffb92008-07-26 14:26:06 +02001420static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421{
1422 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Francois Romieuccdffb92008-07-26 14:26:06 +02001424 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425}
1426
1427static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1428{
1429 struct rtl8169_private *tp = netdev_priv(dev);
1430 unsigned long flags;
Francois Romieuccdffb92008-07-26 14:26:06 +02001431 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 spin_lock_irqsave(&tp->lock, flags);
1434
Francois Romieuccdffb92008-07-26 14:26:06 +02001435 rc = tp->get_settings(dev, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
1437 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieuccdffb92008-07-26 14:26:06 +02001438 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439}
1440
1441static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1442 void *p)
1443{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001444 struct rtl8169_private *tp = netdev_priv(dev);
1445 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Francois Romieu5b0384f2006-08-16 16:00:01 +02001447 if (regs->len > R8169_REGS_SIZE)
1448 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Francois Romieu5b0384f2006-08-16 16:00:01 +02001450 spin_lock_irqsave(&tp->lock, flags);
1451 memcpy_fromio(p, tp->mmio_addr, regs->len);
1452 spin_unlock_irqrestore(&tp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453}
1454
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001455static u32 rtl8169_get_msglevel(struct net_device *dev)
1456{
1457 struct rtl8169_private *tp = netdev_priv(dev);
1458
1459 return tp->msg_enable;
1460}
1461
1462static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1463{
1464 struct rtl8169_private *tp = netdev_priv(dev);
1465
1466 tp->msg_enable = value;
1467}
1468
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001469static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1470 "tx_packets",
1471 "rx_packets",
1472 "tx_errors",
1473 "rx_errors",
1474 "rx_missed",
1475 "align_errors",
1476 "tx_single_collisions",
1477 "tx_multi_collisions",
1478 "unicast",
1479 "broadcast",
1480 "multicast",
1481 "tx_aborted",
1482 "tx_underrun",
1483};
1484
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001485static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001486{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001487 switch (sset) {
1488 case ETH_SS_STATS:
1489 return ARRAY_SIZE(rtl8169_gstrings);
1490 default:
1491 return -EOPNOTSUPP;
1492 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001493}
1494
Ivan Vecera355423d2009-02-06 21:49:57 -08001495static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001496{
1497 struct rtl8169_private *tp = netdev_priv(dev);
1498 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001499 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001500 struct rtl8169_counters *counters;
1501 dma_addr_t paddr;
1502 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001503 int wait = 1000;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001504
Ivan Vecera355423d2009-02-06 21:49:57 -08001505 /*
1506 * Some chips are unable to dump tally counters when the receiver
1507 * is disabled.
1508 */
1509 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1510 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001511
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001512 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001513 if (!counters)
1514 return;
1515
1516 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001517 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001518 RTL_W32(CounterAddrLow, cmd);
1519 RTL_W32(CounterAddrLow, cmd | CounterDump);
1520
Ivan Vecera355423d2009-02-06 21:49:57 -08001521 while (wait--) {
1522 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
Ivan Vecera355423d2009-02-06 21:49:57 -08001523 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001524 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001525 }
1526 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001527 }
1528
1529 RTL_W32(CounterAddrLow, 0);
1530 RTL_W32(CounterAddrHigh, 0);
1531
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001532 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001533}
1534
Ivan Vecera355423d2009-02-06 21:49:57 -08001535static void rtl8169_get_ethtool_stats(struct net_device *dev,
1536 struct ethtool_stats *stats, u64 *data)
1537{
1538 struct rtl8169_private *tp = netdev_priv(dev);
1539
1540 ASSERT_RTNL();
1541
1542 rtl8169_update_counters(dev);
1543
1544 data[0] = le64_to_cpu(tp->counters.tx_packets);
1545 data[1] = le64_to_cpu(tp->counters.rx_packets);
1546 data[2] = le64_to_cpu(tp->counters.tx_errors);
1547 data[3] = le32_to_cpu(tp->counters.rx_errors);
1548 data[4] = le16_to_cpu(tp->counters.rx_missed);
1549 data[5] = le16_to_cpu(tp->counters.align_errors);
1550 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1551 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1552 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1553 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1554 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1555 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1556 data[12] = le16_to_cpu(tp->counters.tx_underun);
1557}
1558
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001559static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1560{
1561 switch(stringset) {
1562 case ETH_SS_STATS:
1563 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1564 break;
1565 }
1566}
1567
Jeff Garzik7282d492006-09-13 14:30:00 -04001568static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 .get_drvinfo = rtl8169_get_drvinfo,
1570 .get_regs_len = rtl8169_get_regs_len,
1571 .get_link = ethtool_op_get_link,
1572 .get_settings = rtl8169_get_settings,
1573 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001574 .get_msglevel = rtl8169_get_msglevel,
1575 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001577 .get_wol = rtl8169_get_wol,
1578 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001579 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001580 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001581 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582};
1583
Francois Romieu07d3f512007-02-21 22:40:46 +01001584static void rtl8169_get_mac_version(struct rtl8169_private *tp,
1585 void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586{
Francois Romieu0e485152007-02-20 00:00:26 +01001587 /*
1588 * The driver currently handles the 8168Bf and the 8168Be identically
1589 * but they can be identified more specifically through the test below
1590 * if needed:
1591 *
1592 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001593 *
1594 * Same thing for the 8101Eb and the 8101Ec:
1595 *
1596 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001597 */
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001598 static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001600 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 int mac_version;
1602 } mac_info[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00001603 /* 8168E family. */
1604 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1605 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1606 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1607
Francois Romieu5b538df2008-07-20 16:22:45 +02001608 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001609 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1610 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001611 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001612
françois romieue6de30d2011-01-03 15:08:37 +00001613 /* 8168DP family. */
1614 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1615 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00001616 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00001617
Francois Romieuef808d52008-06-29 13:10:54 +02001618 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001619 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001620 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001621 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001622 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001623 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1624 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001625 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001626 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001627 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001628
1629 /* 8168B family. */
1630 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1631 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1632 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1633 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1634
1635 /* 8101 family. */
hayeswang36a0e6c2011-03-21 01:50:30 +00001636 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08001637 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1638 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1639 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001640 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1641 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1642 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1643 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1644 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1645 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001646 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001647 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001648 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001649 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1650 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001651 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1652 /* FIXME: where did these entries come from ? -- FR */
1653 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1654 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1655
1656 /* 8110 family. */
1657 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1658 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1659 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1660 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1661 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1662 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1663
Jean Delvaref21b75e2009-05-26 20:54:48 -07001664 /* Catch-all */
1665 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 }, *p = mac_info;
1667 u32 reg;
1668
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001669 reg = RTL_R32(TxConfig);
1670 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 p++;
1672 tp->mac_version = p->mac_version;
1673}
1674
1675static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1676{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001677 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678}
1679
Francois Romieu867763c2007-08-17 18:21:58 +02001680struct phy_reg {
1681 u16 reg;
1682 u16 val;
1683};
1684
françois romieu4da19632011-01-03 15:07:55 +00001685static void rtl_writephy_batch(struct rtl8169_private *tp,
1686 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02001687{
1688 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00001689 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02001690 regs++;
1691 }
1692}
1693
françois romieubca03d52011-01-03 15:07:31 +00001694#define PHY_READ 0x00000000
1695#define PHY_DATA_OR 0x10000000
1696#define PHY_DATA_AND 0x20000000
1697#define PHY_BJMPN 0x30000000
1698#define PHY_READ_EFUSE 0x40000000
1699#define PHY_READ_MAC_BYTE 0x50000000
1700#define PHY_WRITE_MAC_BYTE 0x60000000
1701#define PHY_CLEAR_READCOUNT 0x70000000
1702#define PHY_WRITE 0x80000000
1703#define PHY_READCOUNT_EQ_SKIP 0x90000000
1704#define PHY_COMP_EQ_SKIPN 0xa0000000
1705#define PHY_COMP_NEQ_SKIPN 0xb0000000
1706#define PHY_WRITE_PREVIOUS 0xc0000000
1707#define PHY_SKIPN 0xd0000000
1708#define PHY_DELAY_MS 0xe0000000
1709#define PHY_WRITE_ERI_WORD 0xf0000000
1710
1711static void
1712rtl_phy_write_fw(struct rtl8169_private *tp, const struct firmware *fw)
1713{
françois romieubca03d52011-01-03 15:07:31 +00001714 __le32 *phytable = (__le32 *)fw->data;
1715 struct net_device *dev = tp->dev;
hayeswang42b82dc2011-01-10 02:07:25 +00001716 size_t index, fw_size = fw->size / sizeof(*phytable);
1717 u32 predata, count;
françois romieubca03d52011-01-03 15:07:31 +00001718
1719 if (fw->size % sizeof(*phytable)) {
1720 netif_err(tp, probe, dev, "odd sized firmware %zd\n", fw->size);
1721 return;
1722 }
1723
hayeswang42b82dc2011-01-10 02:07:25 +00001724 for (index = 0; index < fw_size; index++) {
1725 u32 action = le32_to_cpu(phytable[index]);
1726 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00001727
hayeswang42b82dc2011-01-10 02:07:25 +00001728 switch(action & 0xf0000000) {
1729 case PHY_READ:
1730 case PHY_DATA_OR:
1731 case PHY_DATA_AND:
1732 case PHY_READ_EFUSE:
1733 case PHY_CLEAR_READCOUNT:
1734 case PHY_WRITE:
1735 case PHY_WRITE_PREVIOUS:
1736 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00001737 break;
1738
hayeswang42b82dc2011-01-10 02:07:25 +00001739 case PHY_BJMPN:
1740 if (regno > index) {
1741 netif_err(tp, probe, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001742 "Out of range of firmware\n");
hayeswang42b82dc2011-01-10 02:07:25 +00001743 return;
1744 }
1745 break;
1746 case PHY_READCOUNT_EQ_SKIP:
1747 if (index + 2 >= fw_size) {
1748 netif_err(tp, probe, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001749 "Out of range of firmware\n");
hayeswang42b82dc2011-01-10 02:07:25 +00001750 return;
1751 }
1752 break;
1753 case PHY_COMP_EQ_SKIPN:
1754 case PHY_COMP_NEQ_SKIPN:
1755 case PHY_SKIPN:
1756 if (index + 1 + regno >= fw_size) {
1757 netif_err(tp, probe, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001758 "Out of range of firmware\n");
hayeswang42b82dc2011-01-10 02:07:25 +00001759 return;
1760 }
1761 break;
1762
1763 case PHY_READ_MAC_BYTE:
1764 case PHY_WRITE_MAC_BYTE:
1765 case PHY_WRITE_ERI_WORD:
1766 default:
1767 netif_err(tp, probe, tp->dev,
1768 "Invalid action 0x%08x\n", action);
françois romieubca03d52011-01-03 15:07:31 +00001769 return;
1770 }
1771 }
1772
hayeswang42b82dc2011-01-10 02:07:25 +00001773 predata = 0;
1774 count = 0;
1775
1776 for (index = 0; index < fw_size; ) {
1777 u32 action = le32_to_cpu(phytable[index]);
françois romieubca03d52011-01-03 15:07:31 +00001778 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00001779 u32 regno = (action & 0x0fff0000) >> 16;
1780
1781 if (!action)
1782 break;
françois romieubca03d52011-01-03 15:07:31 +00001783
1784 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00001785 case PHY_READ:
1786 predata = rtl_readphy(tp, regno);
1787 count++;
1788 index++;
françois romieubca03d52011-01-03 15:07:31 +00001789 break;
hayeswang42b82dc2011-01-10 02:07:25 +00001790 case PHY_DATA_OR:
1791 predata |= data;
1792 index++;
1793 break;
1794 case PHY_DATA_AND:
1795 predata &= data;
1796 index++;
1797 break;
1798 case PHY_BJMPN:
1799 index -= regno;
1800 break;
1801 case PHY_READ_EFUSE:
1802 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
1803 index++;
1804 break;
1805 case PHY_CLEAR_READCOUNT:
1806 count = 0;
1807 index++;
1808 break;
1809 case PHY_WRITE:
1810 rtl_writephy(tp, regno, data);
1811 index++;
1812 break;
1813 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02001814 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00001815 break;
1816 case PHY_COMP_EQ_SKIPN:
1817 if (predata == data)
1818 index += regno;
1819 index++;
1820 break;
1821 case PHY_COMP_NEQ_SKIPN:
1822 if (predata != data)
1823 index += regno;
1824 index++;
1825 break;
1826 case PHY_WRITE_PREVIOUS:
1827 rtl_writephy(tp, regno, predata);
1828 index++;
1829 break;
1830 case PHY_SKIPN:
1831 index += regno + 1;
1832 break;
1833 case PHY_DELAY_MS:
1834 mdelay(data);
1835 index++;
1836 break;
1837
1838 case PHY_READ_MAC_BYTE:
1839 case PHY_WRITE_MAC_BYTE:
1840 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00001841 default:
1842 BUG();
1843 }
1844 }
1845}
1846
françois romieuf1e02ed2011-01-13 13:07:53 +00001847static void rtl_release_firmware(struct rtl8169_private *tp)
1848{
François Romieu953a12c2011-04-24 17:38:48 +02001849 if (!IS_ERR_OR_NULL(tp->fw))
1850 release_firmware(tp->fw);
1851 tp->fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00001852}
1853
François Romieu953a12c2011-04-24 17:38:48 +02001854static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00001855{
François Romieu953a12c2011-04-24 17:38:48 +02001856 const struct firmware *fw = tp->fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00001857
1858 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
François Romieu953a12c2011-04-24 17:38:48 +02001859 if (!IS_ERR_OR_NULL(fw))
1860 rtl_phy_write_fw(tp, fw);
1861}
1862
1863static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
1864{
1865 if (rtl_readphy(tp, reg) != val)
1866 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
1867 else
1868 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00001869}
1870
françois romieu4da19632011-01-03 15:07:55 +00001871static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001873 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00001874 { 0x1f, 0x0001 },
1875 { 0x06, 0x006e },
1876 { 0x08, 0x0708 },
1877 { 0x15, 0x4000 },
1878 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
françois romieu0b9b5712009-08-10 19:44:56 +00001880 { 0x1f, 0x0001 },
1881 { 0x03, 0x00a1 },
1882 { 0x02, 0x0008 },
1883 { 0x01, 0x0120 },
1884 { 0x00, 0x1000 },
1885 { 0x04, 0x0800 },
1886 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
françois romieu0b9b5712009-08-10 19:44:56 +00001888 { 0x03, 0xff41 },
1889 { 0x02, 0xdf60 },
1890 { 0x01, 0x0140 },
1891 { 0x00, 0x0077 },
1892 { 0x04, 0x7800 },
1893 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
françois romieu0b9b5712009-08-10 19:44:56 +00001895 { 0x03, 0x802f },
1896 { 0x02, 0x4f02 },
1897 { 0x01, 0x0409 },
1898 { 0x00, 0xf0f9 },
1899 { 0x04, 0x9800 },
1900 { 0x04, 0x9000 },
1901
1902 { 0x03, 0xdf01 },
1903 { 0x02, 0xdf20 },
1904 { 0x01, 0xff95 },
1905 { 0x00, 0xba00 },
1906 { 0x04, 0xa800 },
1907 { 0x04, 0xa000 },
1908
1909 { 0x03, 0xff41 },
1910 { 0x02, 0xdf20 },
1911 { 0x01, 0x0140 },
1912 { 0x00, 0x00bb },
1913 { 0x04, 0xb800 },
1914 { 0x04, 0xb000 },
1915
1916 { 0x03, 0xdf41 },
1917 { 0x02, 0xdc60 },
1918 { 0x01, 0x6340 },
1919 { 0x00, 0x007d },
1920 { 0x04, 0xd800 },
1921 { 0x04, 0xd000 },
1922
1923 { 0x03, 0xdf01 },
1924 { 0x02, 0xdf20 },
1925 { 0x01, 0x100a },
1926 { 0x00, 0xa0ff },
1927 { 0x04, 0xf800 },
1928 { 0x04, 0xf000 },
1929
1930 { 0x1f, 0x0000 },
1931 { 0x0b, 0x0000 },
1932 { 0x00, 0x9200 }
1933 };
1934
françois romieu4da19632011-01-03 15:07:55 +00001935 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936}
1937
françois romieu4da19632011-01-03 15:07:55 +00001938static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02001939{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001940 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02001941 { 0x1f, 0x0002 },
1942 { 0x01, 0x90d0 },
1943 { 0x1f, 0x0000 }
1944 };
1945
françois romieu4da19632011-01-03 15:07:55 +00001946 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02001947}
1948
françois romieu4da19632011-01-03 15:07:55 +00001949static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00001950{
1951 struct pci_dev *pdev = tp->pci_dev;
1952 u16 vendor_id, device_id;
1953
1954 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &vendor_id);
1955 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &device_id);
1956
1957 if ((vendor_id != PCI_VENDOR_ID_GIGABYTE) || (device_id != 0xe000))
1958 return;
1959
françois romieu4da19632011-01-03 15:07:55 +00001960 rtl_writephy(tp, 0x1f, 0x0001);
1961 rtl_writephy(tp, 0x10, 0xf01b);
1962 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00001963}
1964
françois romieu4da19632011-01-03 15:07:55 +00001965static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00001966{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001967 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00001968 { 0x1f, 0x0001 },
1969 { 0x04, 0x0000 },
1970 { 0x03, 0x00a1 },
1971 { 0x02, 0x0008 },
1972 { 0x01, 0x0120 },
1973 { 0x00, 0x1000 },
1974 { 0x04, 0x0800 },
1975 { 0x04, 0x9000 },
1976 { 0x03, 0x802f },
1977 { 0x02, 0x4f02 },
1978 { 0x01, 0x0409 },
1979 { 0x00, 0xf099 },
1980 { 0x04, 0x9800 },
1981 { 0x04, 0xa000 },
1982 { 0x03, 0xdf01 },
1983 { 0x02, 0xdf20 },
1984 { 0x01, 0xff95 },
1985 { 0x00, 0xba00 },
1986 { 0x04, 0xa800 },
1987 { 0x04, 0xf000 },
1988 { 0x03, 0xdf01 },
1989 { 0x02, 0xdf20 },
1990 { 0x01, 0x101a },
1991 { 0x00, 0xa0ff },
1992 { 0x04, 0xf800 },
1993 { 0x04, 0x0000 },
1994 { 0x1f, 0x0000 },
1995
1996 { 0x1f, 0x0001 },
1997 { 0x10, 0xf41b },
1998 { 0x14, 0xfb54 },
1999 { 0x18, 0xf5c7 },
2000 { 0x1f, 0x0000 },
2001
2002 { 0x1f, 0x0001 },
2003 { 0x17, 0x0cc0 },
2004 { 0x1f, 0x0000 }
2005 };
2006
françois romieu4da19632011-01-03 15:07:55 +00002007 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002008
françois romieu4da19632011-01-03 15:07:55 +00002009 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002010}
2011
françois romieu4da19632011-01-03 15:07:55 +00002012static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002013{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002014 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002015 { 0x1f, 0x0001 },
2016 { 0x04, 0x0000 },
2017 { 0x03, 0x00a1 },
2018 { 0x02, 0x0008 },
2019 { 0x01, 0x0120 },
2020 { 0x00, 0x1000 },
2021 { 0x04, 0x0800 },
2022 { 0x04, 0x9000 },
2023 { 0x03, 0x802f },
2024 { 0x02, 0x4f02 },
2025 { 0x01, 0x0409 },
2026 { 0x00, 0xf099 },
2027 { 0x04, 0x9800 },
2028 { 0x04, 0xa000 },
2029 { 0x03, 0xdf01 },
2030 { 0x02, 0xdf20 },
2031 { 0x01, 0xff95 },
2032 { 0x00, 0xba00 },
2033 { 0x04, 0xa800 },
2034 { 0x04, 0xf000 },
2035 { 0x03, 0xdf01 },
2036 { 0x02, 0xdf20 },
2037 { 0x01, 0x101a },
2038 { 0x00, 0xa0ff },
2039 { 0x04, 0xf800 },
2040 { 0x04, 0x0000 },
2041 { 0x1f, 0x0000 },
2042
2043 { 0x1f, 0x0001 },
2044 { 0x0b, 0x8480 },
2045 { 0x1f, 0x0000 },
2046
2047 { 0x1f, 0x0001 },
2048 { 0x18, 0x67c7 },
2049 { 0x04, 0x2000 },
2050 { 0x03, 0x002f },
2051 { 0x02, 0x4360 },
2052 { 0x01, 0x0109 },
2053 { 0x00, 0x3022 },
2054 { 0x04, 0x2800 },
2055 { 0x1f, 0x0000 },
2056
2057 { 0x1f, 0x0001 },
2058 { 0x17, 0x0cc0 },
2059 { 0x1f, 0x0000 }
2060 };
2061
françois romieu4da19632011-01-03 15:07:55 +00002062 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002063}
2064
françois romieu4da19632011-01-03 15:07:55 +00002065static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002066{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002067 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002068 { 0x10, 0xf41b },
2069 { 0x1f, 0x0000 }
2070 };
2071
françois romieu4da19632011-01-03 15:07:55 +00002072 rtl_writephy(tp, 0x1f, 0x0001);
2073 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002074
françois romieu4da19632011-01-03 15:07:55 +00002075 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002076}
2077
françois romieu4da19632011-01-03 15:07:55 +00002078static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002079{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002080 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002081 { 0x1f, 0x0001 },
2082 { 0x10, 0xf41b },
2083 { 0x1f, 0x0000 }
2084 };
2085
françois romieu4da19632011-01-03 15:07:55 +00002086 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002087}
2088
françois romieu4da19632011-01-03 15:07:55 +00002089static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002090{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002091 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002092 { 0x1f, 0x0000 },
2093 { 0x1d, 0x0f00 },
2094 { 0x1f, 0x0002 },
2095 { 0x0c, 0x1ec8 },
2096 { 0x1f, 0x0000 }
2097 };
2098
françois romieu4da19632011-01-03 15:07:55 +00002099 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002100}
2101
françois romieu4da19632011-01-03 15:07:55 +00002102static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002103{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002104 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002105 { 0x1f, 0x0001 },
2106 { 0x1d, 0x3d98 },
2107 { 0x1f, 0x0000 }
2108 };
2109
françois romieu4da19632011-01-03 15:07:55 +00002110 rtl_writephy(tp, 0x1f, 0x0000);
2111 rtl_patchphy(tp, 0x14, 1 << 5);
2112 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002113
françois romieu4da19632011-01-03 15:07:55 +00002114 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002115}
2116
françois romieu4da19632011-01-03 15:07:55 +00002117static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002118{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002119 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002120 { 0x1f, 0x0001 },
2121 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002122 { 0x1f, 0x0002 },
2123 { 0x00, 0x88d4 },
2124 { 0x01, 0x82b1 },
2125 { 0x03, 0x7002 },
2126 { 0x08, 0x9e30 },
2127 { 0x09, 0x01f0 },
2128 { 0x0a, 0x5500 },
2129 { 0x0c, 0x00c8 },
2130 { 0x1f, 0x0003 },
2131 { 0x12, 0xc096 },
2132 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002133 { 0x1f, 0x0000 },
2134 { 0x1f, 0x0000 },
2135 { 0x09, 0x2000 },
2136 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002137 };
2138
françois romieu4da19632011-01-03 15:07:55 +00002139 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002140
françois romieu4da19632011-01-03 15:07:55 +00002141 rtl_patchphy(tp, 0x14, 1 << 5);
2142 rtl_patchphy(tp, 0x0d, 1 << 5);
2143 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002144}
2145
françois romieu4da19632011-01-03 15:07:55 +00002146static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002147{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002148 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002149 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002150 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002151 { 0x03, 0x802f },
2152 { 0x02, 0x4f02 },
2153 { 0x01, 0x0409 },
2154 { 0x00, 0xf099 },
2155 { 0x04, 0x9800 },
2156 { 0x04, 0x9000 },
2157 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002158 { 0x1f, 0x0002 },
2159 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002160 { 0x06, 0x0761 },
2161 { 0x1f, 0x0003 },
2162 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002163 { 0x1f, 0x0000 }
2164 };
2165
françois romieu4da19632011-01-03 15:07:55 +00002166 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002167
françois romieu4da19632011-01-03 15:07:55 +00002168 rtl_patchphy(tp, 0x16, 1 << 0);
2169 rtl_patchphy(tp, 0x14, 1 << 5);
2170 rtl_patchphy(tp, 0x0d, 1 << 5);
2171 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002172}
2173
françois romieu4da19632011-01-03 15:07:55 +00002174static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002175{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002176 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002177 { 0x1f, 0x0001 },
2178 { 0x12, 0x2300 },
2179 { 0x1d, 0x3d98 },
2180 { 0x1f, 0x0002 },
2181 { 0x0c, 0x7eb8 },
2182 { 0x06, 0x5461 },
2183 { 0x1f, 0x0003 },
2184 { 0x16, 0x0f0a },
2185 { 0x1f, 0x0000 }
2186 };
2187
françois romieu4da19632011-01-03 15:07:55 +00002188 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002189
françois romieu4da19632011-01-03 15:07:55 +00002190 rtl_patchphy(tp, 0x16, 1 << 0);
2191 rtl_patchphy(tp, 0x14, 1 << 5);
2192 rtl_patchphy(tp, 0x0d, 1 << 5);
2193 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002194}
2195
françois romieu4da19632011-01-03 15:07:55 +00002196static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002197{
françois romieu4da19632011-01-03 15:07:55 +00002198 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002199}
2200
françois romieubca03d52011-01-03 15:07:31 +00002201static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002202{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002203 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002204 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002205 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002206 { 0x06, 0x4064 },
2207 { 0x07, 0x2863 },
2208 { 0x08, 0x059c },
2209 { 0x09, 0x26b4 },
2210 { 0x0a, 0x6a19 },
2211 { 0x0b, 0xdcc8 },
2212 { 0x10, 0xf06d },
2213 { 0x14, 0x7f68 },
2214 { 0x18, 0x7fd9 },
2215 { 0x1c, 0xf0ff },
2216 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002217 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002218 { 0x12, 0xf49f },
2219 { 0x13, 0x070b },
2220 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002221 { 0x14, 0x94c0 },
2222
2223 /*
2224 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002225 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002226 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002227 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002228 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002229 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002230 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002231 { 0x06, 0x5561 },
2232
2233 /*
2234 * Can not link to 1Gbps with bad cable
2235 * Decrease SNR threshold form 21.07dB to 19.04dB
2236 */
2237 { 0x1f, 0x0001 },
2238 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002239
2240 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002241 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002242 };
françois romieubca03d52011-01-03 15:07:31 +00002243 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002244
françois romieu4da19632011-01-03 15:07:55 +00002245 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002246
françois romieubca03d52011-01-03 15:07:31 +00002247 /*
2248 * Rx Error Issue
2249 * Fine Tune Switching regulator parameter
2250 */
françois romieu4da19632011-01-03 15:07:55 +00002251 rtl_writephy(tp, 0x1f, 0x0002);
2252 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2253 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002254
françois romieudaf9df62009-10-07 12:44:20 +00002255 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002256 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002257 { 0x1f, 0x0002 },
2258 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002259 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002260 { 0x05, 0x8330 },
2261 { 0x06, 0x669a },
2262 { 0x1f, 0x0002 }
2263 };
2264 int val;
2265
françois romieu4da19632011-01-03 15:07:55 +00002266 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002267
françois romieu4da19632011-01-03 15:07:55 +00002268 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002269
2270 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002271 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002272 0x0065, 0x0066, 0x0067, 0x0068,
2273 0x0069, 0x006a, 0x006b, 0x006c
2274 };
2275 int i;
2276
françois romieu4da19632011-01-03 15:07:55 +00002277 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002278
2279 val &= 0xff00;
2280 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002281 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002282 }
2283 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002284 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002285 { 0x1f, 0x0002 },
2286 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002287 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002288 { 0x05, 0x8330 },
2289 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002290 };
2291
françois romieu4da19632011-01-03 15:07:55 +00002292 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002293 }
2294
françois romieubca03d52011-01-03 15:07:31 +00002295 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002296 rtl_writephy(tp, 0x1f, 0x0002);
2297 rtl_patchphy(tp, 0x0d, 0x0300);
2298 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002299
françois romieubca03d52011-01-03 15:07:31 +00002300 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002301 rtl_writephy(tp, 0x1f, 0x0002);
2302 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2303 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002304
françois romieu4da19632011-01-03 15:07:55 +00002305 rtl_writephy(tp, 0x1f, 0x0005);
2306 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002307
2308 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002309
françois romieu4da19632011-01-03 15:07:55 +00002310 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002311}
2312
françois romieubca03d52011-01-03 15:07:31 +00002313static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002314{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002315 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002316 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002317 { 0x1f, 0x0001 },
2318 { 0x06, 0x4064 },
2319 { 0x07, 0x2863 },
2320 { 0x08, 0x059c },
2321 { 0x09, 0x26b4 },
2322 { 0x0a, 0x6a19 },
2323 { 0x0b, 0xdcc8 },
2324 { 0x10, 0xf06d },
2325 { 0x14, 0x7f68 },
2326 { 0x18, 0x7fd9 },
2327 { 0x1c, 0xf0ff },
2328 { 0x1d, 0x3d9c },
2329 { 0x1f, 0x0003 },
2330 { 0x12, 0xf49f },
2331 { 0x13, 0x070b },
2332 { 0x1a, 0x05ad },
2333 { 0x14, 0x94c0 },
2334
françois romieubca03d52011-01-03 15:07:31 +00002335 /*
2336 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002337 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002338 */
françois romieudaf9df62009-10-07 12:44:20 +00002339 { 0x1f, 0x0002 },
2340 { 0x06, 0x5561 },
2341 { 0x1f, 0x0005 },
2342 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002343 { 0x06, 0x5561 },
2344
2345 /*
2346 * Can not link to 1Gbps with bad cable
2347 * Decrease SNR threshold form 21.07dB to 19.04dB
2348 */
2349 { 0x1f, 0x0001 },
2350 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002351
2352 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002353 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002354 };
françois romieubca03d52011-01-03 15:07:31 +00002355 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002356
françois romieu4da19632011-01-03 15:07:55 +00002357 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002358
2359 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002360 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002361 { 0x1f, 0x0002 },
2362 { 0x05, 0x669a },
2363 { 0x1f, 0x0005 },
2364 { 0x05, 0x8330 },
2365 { 0x06, 0x669a },
2366
2367 { 0x1f, 0x0002 }
2368 };
2369 int val;
2370
françois romieu4da19632011-01-03 15:07:55 +00002371 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002372
françois romieu4da19632011-01-03 15:07:55 +00002373 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002374 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002375 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002376 0x0065, 0x0066, 0x0067, 0x0068,
2377 0x0069, 0x006a, 0x006b, 0x006c
2378 };
2379 int i;
2380
françois romieu4da19632011-01-03 15:07:55 +00002381 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002382
2383 val &= 0xff00;
2384 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002385 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002386 }
2387 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002388 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002389 { 0x1f, 0x0002 },
2390 { 0x05, 0x2642 },
2391 { 0x1f, 0x0005 },
2392 { 0x05, 0x8330 },
2393 { 0x06, 0x2642 }
2394 };
2395
françois romieu4da19632011-01-03 15:07:55 +00002396 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002397 }
2398
françois romieubca03d52011-01-03 15:07:31 +00002399 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002400 rtl_writephy(tp, 0x1f, 0x0002);
2401 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2402 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002403
françois romieubca03d52011-01-03 15:07:31 +00002404 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002405 rtl_writephy(tp, 0x1f, 0x0002);
2406 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002407
françois romieu4da19632011-01-03 15:07:55 +00002408 rtl_writephy(tp, 0x1f, 0x0005);
2409 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002410
2411 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002412
françois romieu4da19632011-01-03 15:07:55 +00002413 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002414}
2415
françois romieu4da19632011-01-03 15:07:55 +00002416static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002417{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002418 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002419 { 0x1f, 0x0002 },
2420 { 0x10, 0x0008 },
2421 { 0x0d, 0x006c },
2422
2423 { 0x1f, 0x0000 },
2424 { 0x0d, 0xf880 },
2425
2426 { 0x1f, 0x0001 },
2427 { 0x17, 0x0cc0 },
2428
2429 { 0x1f, 0x0001 },
2430 { 0x0b, 0xa4d8 },
2431 { 0x09, 0x281c },
2432 { 0x07, 0x2883 },
2433 { 0x0a, 0x6b35 },
2434 { 0x1d, 0x3da4 },
2435 { 0x1c, 0xeffd },
2436 { 0x14, 0x7f52 },
2437 { 0x18, 0x7fc6 },
2438 { 0x08, 0x0601 },
2439 { 0x06, 0x4063 },
2440 { 0x10, 0xf074 },
2441 { 0x1f, 0x0003 },
2442 { 0x13, 0x0789 },
2443 { 0x12, 0xf4bd },
2444 { 0x1a, 0x04fd },
2445 { 0x14, 0x84b0 },
2446 { 0x1f, 0x0000 },
2447 { 0x00, 0x9200 },
2448
2449 { 0x1f, 0x0005 },
2450 { 0x01, 0x0340 },
2451 { 0x1f, 0x0001 },
2452 { 0x04, 0x4000 },
2453 { 0x03, 0x1d21 },
2454 { 0x02, 0x0c32 },
2455 { 0x01, 0x0200 },
2456 { 0x00, 0x5554 },
2457 { 0x04, 0x4800 },
2458 { 0x04, 0x4000 },
2459 { 0x04, 0xf000 },
2460 { 0x03, 0xdf01 },
2461 { 0x02, 0xdf20 },
2462 { 0x01, 0x101a },
2463 { 0x00, 0xa0ff },
2464 { 0x04, 0xf800 },
2465 { 0x04, 0xf000 },
2466 { 0x1f, 0x0000 },
2467
2468 { 0x1f, 0x0007 },
2469 { 0x1e, 0x0023 },
2470 { 0x16, 0x0000 },
2471 { 0x1f, 0x0000 }
2472 };
2473
françois romieu4da19632011-01-03 15:07:55 +00002474 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002475}
2476
françois romieue6de30d2011-01-03 15:08:37 +00002477static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2478{
2479 static const struct phy_reg phy_reg_init[] = {
2480 { 0x1f, 0x0001 },
2481 { 0x17, 0x0cc0 },
2482
2483 { 0x1f, 0x0007 },
2484 { 0x1e, 0x002d },
2485 { 0x18, 0x0040 },
2486 { 0x1f, 0x0000 }
2487 };
2488
2489 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2490 rtl_patchphy(tp, 0x0d, 1 << 5);
2491}
2492
hayeswang01dc7fe2011-03-21 01:50:28 +00002493static void rtl8168e_hw_phy_config(struct rtl8169_private *tp)
2494{
2495 static const struct phy_reg phy_reg_init[] = {
2496 /* Enable Delay cap */
2497 { 0x1f, 0x0005 },
2498 { 0x05, 0x8b80 },
2499 { 0x06, 0xc896 },
2500 { 0x1f, 0x0000 },
2501
2502 /* Channel estimation fine tune */
2503 { 0x1f, 0x0001 },
2504 { 0x0b, 0x6c20 },
2505 { 0x07, 0x2872 },
2506 { 0x1c, 0xefff },
2507 { 0x1f, 0x0003 },
2508 { 0x14, 0x6420 },
2509 { 0x1f, 0x0000 },
2510
2511 /* Update PFM & 10M TX idle timer */
2512 { 0x1f, 0x0007 },
2513 { 0x1e, 0x002f },
2514 { 0x15, 0x1919 },
2515 { 0x1f, 0x0000 },
2516
2517 { 0x1f, 0x0007 },
2518 { 0x1e, 0x00ac },
2519 { 0x18, 0x0006 },
2520 { 0x1f, 0x0000 }
2521 };
2522
Francois Romieu15ecd032011-04-27 13:52:22 -07002523 rtl_apply_firmware(tp);
2524
hayeswang01dc7fe2011-03-21 01:50:28 +00002525 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2526
2527 /* DCO enable for 10M IDLE Power */
2528 rtl_writephy(tp, 0x1f, 0x0007);
2529 rtl_writephy(tp, 0x1e, 0x0023);
2530 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2531 rtl_writephy(tp, 0x1f, 0x0000);
2532
2533 /* For impedance matching */
2534 rtl_writephy(tp, 0x1f, 0x0002);
2535 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002536 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002537
2538 /* PHY auto speed down */
2539 rtl_writephy(tp, 0x1f, 0x0007);
2540 rtl_writephy(tp, 0x1e, 0x002d);
2541 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2542 rtl_writephy(tp, 0x1f, 0x0000);
2543 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2544
2545 rtl_writephy(tp, 0x1f, 0x0005);
2546 rtl_writephy(tp, 0x05, 0x8b86);
2547 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2548 rtl_writephy(tp, 0x1f, 0x0000);
2549
2550 rtl_writephy(tp, 0x1f, 0x0005);
2551 rtl_writephy(tp, 0x05, 0x8b85);
2552 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2553 rtl_writephy(tp, 0x1f, 0x0007);
2554 rtl_writephy(tp, 0x1e, 0x0020);
2555 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2556 rtl_writephy(tp, 0x1f, 0x0006);
2557 rtl_writephy(tp, 0x00, 0x5a00);
2558 rtl_writephy(tp, 0x1f, 0x0000);
2559 rtl_writephy(tp, 0x0d, 0x0007);
2560 rtl_writephy(tp, 0x0e, 0x003c);
2561 rtl_writephy(tp, 0x0d, 0x4007);
2562 rtl_writephy(tp, 0x0e, 0x0000);
2563 rtl_writephy(tp, 0x0d, 0x0000);
2564}
2565
françois romieu4da19632011-01-03 15:07:55 +00002566static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02002567{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002568 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02002569 { 0x1f, 0x0003 },
2570 { 0x08, 0x441d },
2571 { 0x01, 0x9100 },
2572 { 0x1f, 0x0000 }
2573 };
2574
françois romieu4da19632011-01-03 15:07:55 +00002575 rtl_writephy(tp, 0x1f, 0x0000);
2576 rtl_patchphy(tp, 0x11, 1 << 12);
2577 rtl_patchphy(tp, 0x19, 1 << 13);
2578 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002579
françois romieu4da19632011-01-03 15:07:55 +00002580 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02002581}
2582
Hayes Wang5a5e4442011-02-22 17:26:21 +08002583static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
2584{
2585 static const struct phy_reg phy_reg_init[] = {
2586 { 0x1f, 0x0005 },
2587 { 0x1a, 0x0000 },
2588 { 0x1f, 0x0000 },
2589
2590 { 0x1f, 0x0004 },
2591 { 0x1c, 0x0000 },
2592 { 0x1f, 0x0000 },
2593
2594 { 0x1f, 0x0001 },
2595 { 0x15, 0x7701 },
2596 { 0x1f, 0x0000 }
2597 };
2598
2599 /* Disable ALDPS before ram code */
2600 rtl_writephy(tp, 0x1f, 0x0000);
2601 rtl_writephy(tp, 0x18, 0x0310);
2602 msleep(100);
2603
François Romieu953a12c2011-04-24 17:38:48 +02002604 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08002605
2606 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2607}
2608
Francois Romieu5615d9f2007-08-17 17:50:46 +02002609static void rtl_hw_phy_config(struct net_device *dev)
2610{
2611 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002612
2613 rtl8169_print_mac_version(tp);
2614
2615 switch (tp->mac_version) {
2616 case RTL_GIGA_MAC_VER_01:
2617 break;
2618 case RTL_GIGA_MAC_VER_02:
2619 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00002620 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002621 break;
2622 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00002623 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002624 break;
françois romieu2e9558562009-08-10 19:44:19 +00002625 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00002626 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002627 break;
françois romieu8c7006a2009-08-10 19:43:29 +00002628 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00002629 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00002630 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02002631 case RTL_GIGA_MAC_VER_07:
2632 case RTL_GIGA_MAC_VER_08:
2633 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00002634 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002635 break;
Francois Romieu236b8082008-05-30 16:11:48 +02002636 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00002637 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002638 break;
2639 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00002640 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002641 break;
2642 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00002643 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002644 break;
Francois Romieu867763c2007-08-17 18:21:58 +02002645 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00002646 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002647 break;
2648 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00002649 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002650 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02002651 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00002652 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002653 break;
Francois Romieu197ff762008-06-28 13:16:02 +02002654 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00002655 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02002656 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02002657 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00002658 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002659 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02002660 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002661 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00002662 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02002663 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02002664 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00002665 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002666 break;
2667 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00002668 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002669 break;
2670 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00002671 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02002672 break;
françois romieue6de30d2011-01-03 15:08:37 +00002673 case RTL_GIGA_MAC_VER_28:
2674 rtl8168d_4_hw_phy_config(tp);
2675 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08002676 case RTL_GIGA_MAC_VER_29:
2677 case RTL_GIGA_MAC_VER_30:
2678 rtl8105e_hw_phy_config(tp);
2679 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02002680 case RTL_GIGA_MAC_VER_31:
2681 /* None. */
2682 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00002683 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00002684 case RTL_GIGA_MAC_VER_33:
Francois Romieu15ecd032011-04-27 13:52:22 -07002685 rtl8168e_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00002686 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02002687
Francois Romieu5615d9f2007-08-17 17:50:46 +02002688 default:
2689 break;
2690 }
2691}
2692
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693static void rtl8169_phy_timer(unsigned long __opaque)
2694{
2695 struct net_device *dev = (struct net_device *)__opaque;
2696 struct rtl8169_private *tp = netdev_priv(dev);
2697 struct timer_list *timer = &tp->timer;
2698 void __iomem *ioaddr = tp->mmio_addr;
2699 unsigned long timeout = RTL8169_PHY_TIMEOUT;
2700
Francois Romieubcf0bf92006-07-26 23:14:13 +02002701 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
Francois Romieu64e4bfb2006-08-17 12:43:06 +02002703 if (!(tp->phy_1000_ctrl_reg & ADVERTISE_1000FULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 return;
2705
2706 spin_lock_irq(&tp->lock);
2707
françois romieu4da19632011-01-03 15:07:55 +00002708 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02002709 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 * A busy loop could burn quite a few cycles on nowadays CPU.
2711 * Let's delay the execution of the timer for a few ticks.
2712 */
2713 timeout = HZ/10;
2714 goto out_mod_timer;
2715 }
2716
2717 if (tp->link_ok(ioaddr))
2718 goto out_unlock;
2719
Joe Perchesbf82c182010-02-09 11:49:50 +00002720 netif_warn(tp, link, dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
françois romieu4da19632011-01-03 15:07:55 +00002722 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723
2724out_mod_timer:
2725 mod_timer(timer, jiffies + timeout);
2726out_unlock:
2727 spin_unlock_irq(&tp->lock);
2728}
2729
2730static inline void rtl8169_delete_timer(struct net_device *dev)
2731{
2732 struct rtl8169_private *tp = netdev_priv(dev);
2733 struct timer_list *timer = &tp->timer;
2734
Francois Romieue179bb72007-08-17 15:05:21 +02002735 if (tp->mac_version <= RTL_GIGA_MAC_VER_01)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 return;
2737
2738 del_timer_sync(timer);
2739}
2740
2741static inline void rtl8169_request_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
Francois Romieu2efa53f2007-03-09 00:00:05 +01002749 mod_timer(timer, jiffies + RTL8169_PHY_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750}
2751
2752#ifdef CONFIG_NET_POLL_CONTROLLER
2753/*
2754 * Polling 'interrupt' - used by things like netconsole to send skbs
2755 * without having to re-enable interrupts. It's not called while
2756 * the interrupt routine is executing.
2757 */
2758static void rtl8169_netpoll(struct net_device *dev)
2759{
2760 struct rtl8169_private *tp = netdev_priv(dev);
2761 struct pci_dev *pdev = tp->pci_dev;
2762
2763 disable_irq(pdev->irq);
David Howells7d12e782006-10-05 14:55:46 +01002764 rtl8169_interrupt(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 enable_irq(pdev->irq);
2766}
2767#endif
2768
2769static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
2770 void __iomem *ioaddr)
2771{
2772 iounmap(ioaddr);
2773 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00002774 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 pci_disable_device(pdev);
2776 free_netdev(dev);
2777}
2778
Francois Romieubf793292006-11-01 00:53:05 +01002779static void rtl8169_phy_reset(struct net_device *dev,
2780 struct rtl8169_private *tp)
2781{
Francois Romieu07d3f512007-02-21 22:40:46 +01002782 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01002783
françois romieu4da19632011-01-03 15:07:55 +00002784 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01002785 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00002786 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01002787 return;
2788 msleep(1);
2789 }
Joe Perchesbf82c182010-02-09 11:49:50 +00002790 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01002791}
2792
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002793static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002795 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002796
Francois Romieu5615d9f2007-08-17 17:50:46 +02002797 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002798
Marcus Sundberg773328942008-07-10 21:28:08 +02002799 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
2800 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2801 RTL_W8(0x82, 0x01);
2802 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002803
Francois Romieu6dccd162007-02-13 23:38:05 +01002804 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
2805
2806 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
2807 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002808
Francois Romieubcf0bf92006-07-26 23:14:13 +02002809 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002810 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2811 RTL_W8(0x82, 0x01);
2812 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00002813 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002814 }
2815
Francois Romieubf793292006-11-01 00:53:05 +01002816 rtl8169_phy_reset(dev, tp);
2817
Oliver Neukum54405cd2011-01-06 21:55:13 +01002818 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002819 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
2820 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
2821 (tp->mii.supports_gmii ?
2822 ADVERTISED_1000baseT_Half |
2823 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002824
Joe Perchesbf82c182010-02-09 11:49:50 +00002825 if (RTL_R8(PHYstatus) & TBI_Enable)
2826 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002827}
2828
Francois Romieu773d2022007-01-31 23:47:43 +01002829static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
2830{
2831 void __iomem *ioaddr = tp->mmio_addr;
2832 u32 high;
2833 u32 low;
2834
2835 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
2836 high = addr[4] | (addr[5] << 8);
2837
2838 spin_lock_irq(&tp->lock);
2839
2840 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00002841
Francois Romieu773d2022007-01-31 23:47:43 +01002842 RTL_W32(MAC4, high);
françois romieu908ba2b2010-04-26 11:42:58 +00002843 RTL_R32(MAC4);
2844
Francois Romieu78f1cd02010-03-27 19:35:46 -07002845 RTL_W32(MAC0, low);
françois romieu908ba2b2010-04-26 11:42:58 +00002846 RTL_R32(MAC0);
2847
Francois Romieu773d2022007-01-31 23:47:43 +01002848 RTL_W8(Cfg9346, Cfg9346_Lock);
2849
2850 spin_unlock_irq(&tp->lock);
2851}
2852
2853static int rtl_set_mac_address(struct net_device *dev, void *p)
2854{
2855 struct rtl8169_private *tp = netdev_priv(dev);
2856 struct sockaddr *addr = p;
2857
2858 if (!is_valid_ether_addr(addr->sa_data))
2859 return -EADDRNOTAVAIL;
2860
2861 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
2862
2863 rtl_rar_set(tp, dev->dev_addr);
2864
2865 return 0;
2866}
2867
Francois Romieu5f787a12006-08-17 13:02:36 +02002868static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2869{
2870 struct rtl8169_private *tp = netdev_priv(dev);
2871 struct mii_ioctl_data *data = if_mii(ifr);
2872
Francois Romieu8b4ab282008-11-19 22:05:25 -08002873 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
2874}
Francois Romieu5f787a12006-08-17 13:02:36 +02002875
Francois Romieucecb5fd2011-04-01 10:21:07 +02002876static int rtl_xmii_ioctl(struct rtl8169_private *tp,
2877 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08002878{
Francois Romieu5f787a12006-08-17 13:02:36 +02002879 switch (cmd) {
2880 case SIOCGMIIPHY:
2881 data->phy_id = 32; /* Internal PHY */
2882 return 0;
2883
2884 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00002885 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02002886 return 0;
2887
2888 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00002889 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02002890 return 0;
2891 }
2892 return -EOPNOTSUPP;
2893}
2894
Francois Romieu8b4ab282008-11-19 22:05:25 -08002895static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
2896{
2897 return -EOPNOTSUPP;
2898}
2899
Francois Romieu0e485152007-02-20 00:00:26 +01002900static const struct rtl_cfg_info {
2901 void (*hw_start)(struct net_device *);
2902 unsigned int region;
2903 unsigned int align;
2904 u16 intr_event;
2905 u16 napi_event;
Francois Romieuccdffb92008-07-26 14:26:06 +02002906 unsigned features;
Jean Delvaref21b75e2009-05-26 20:54:48 -07002907 u8 default_ver;
Francois Romieu0e485152007-02-20 00:00:26 +01002908} rtl_cfg_infos [] = {
2909 [RTL_CFG_0] = {
2910 .hw_start = rtl_hw_start_8169,
2911 .region = 1,
Francois Romieue9f63f32007-02-28 23:16:57 +01002912 .align = 0,
Francois Romieu0e485152007-02-20 00:00:26 +01002913 .intr_event = SYSErr | LinkChg | RxOverflow |
2914 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02002915 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07002916 .features = RTL_FEATURE_GMII,
2917 .default_ver = RTL_GIGA_MAC_VER_01,
Francois Romieu0e485152007-02-20 00:00:26 +01002918 },
2919 [RTL_CFG_1] = {
2920 .hw_start = rtl_hw_start_8168,
2921 .region = 2,
2922 .align = 8,
françois romieu53f57352010-11-08 13:23:05 +00002923 .intr_event = SYSErr | LinkChg | RxOverflow |
Francois Romieu0e485152007-02-20 00:00:26 +01002924 TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02002925 .napi_event = TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07002926 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
2927 .default_ver = RTL_GIGA_MAC_VER_11,
Francois Romieu0e485152007-02-20 00:00:26 +01002928 },
2929 [RTL_CFG_2] = {
2930 .hw_start = rtl_hw_start_8101,
2931 .region = 2,
2932 .align = 8,
2933 .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
2934 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02002935 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07002936 .features = RTL_FEATURE_MSI,
2937 .default_ver = RTL_GIGA_MAC_VER_13,
Francois Romieu0e485152007-02-20 00:00:26 +01002938 }
2939};
2940
Francois Romieufbac58f2007-10-04 22:51:38 +02002941/* Cfg9346_Unlock assumed. */
2942static unsigned rtl_try_msi(struct pci_dev *pdev, void __iomem *ioaddr,
2943 const struct rtl_cfg_info *cfg)
2944{
2945 unsigned msi = 0;
2946 u8 cfg2;
2947
2948 cfg2 = RTL_R8(Config2) & ~MSIEnable;
Francois Romieuccdffb92008-07-26 14:26:06 +02002949 if (cfg->features & RTL_FEATURE_MSI) {
Francois Romieufbac58f2007-10-04 22:51:38 +02002950 if (pci_enable_msi(pdev)) {
2951 dev_info(&pdev->dev, "no MSI. Back to INTx.\n");
2952 } else {
2953 cfg2 |= MSIEnable;
2954 msi = RTL_FEATURE_MSI;
2955 }
2956 }
2957 RTL_W8(Config2, cfg2);
2958 return msi;
2959}
2960
2961static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
2962{
2963 if (tp->features & RTL_FEATURE_MSI) {
2964 pci_disable_msi(pdev);
2965 tp->features &= ~RTL_FEATURE_MSI;
2966 }
2967}
2968
Francois Romieu8b4ab282008-11-19 22:05:25 -08002969static const struct net_device_ops rtl8169_netdev_ops = {
2970 .ndo_open = rtl8169_open,
2971 .ndo_stop = rtl8169_close,
2972 .ndo_get_stats = rtl8169_get_stats,
Stephen Hemminger00829822008-11-20 20:14:53 -08002973 .ndo_start_xmit = rtl8169_start_xmit,
Francois Romieu8b4ab282008-11-19 22:05:25 -08002974 .ndo_tx_timeout = rtl8169_tx_timeout,
2975 .ndo_validate_addr = eth_validate_addr,
2976 .ndo_change_mtu = rtl8169_change_mtu,
Michał Mirosław350fb322011-04-08 06:35:56 +00002977 .ndo_fix_features = rtl8169_fix_features,
2978 .ndo_set_features = rtl8169_set_features,
Francois Romieu8b4ab282008-11-19 22:05:25 -08002979 .ndo_set_mac_address = rtl_set_mac_address,
2980 .ndo_do_ioctl = rtl8169_ioctl,
2981 .ndo_set_multicast_list = rtl_set_rx_mode,
Francois Romieu8b4ab282008-11-19 22:05:25 -08002982#ifdef CONFIG_NET_POLL_CONTROLLER
2983 .ndo_poll_controller = rtl8169_netpoll,
2984#endif
2985
2986};
2987
françois romieuc0e45c12011-01-03 15:08:04 +00002988static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
2989{
2990 struct mdio_ops *ops = &tp->mdio_ops;
2991
2992 switch (tp->mac_version) {
2993 case RTL_GIGA_MAC_VER_27:
2994 ops->write = r8168dp_1_mdio_write;
2995 ops->read = r8168dp_1_mdio_read;
2996 break;
françois romieue6de30d2011-01-03 15:08:37 +00002997 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00002998 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00002999 ops->write = r8168dp_2_mdio_write;
3000 ops->read = r8168dp_2_mdio_read;
3001 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003002 default:
3003 ops->write = r8169_mdio_write;
3004 ops->read = r8169_mdio_read;
3005 break;
3006 }
3007}
3008
françois romieu065c27c2011-01-03 15:08:12 +00003009static void r810x_phy_power_down(struct rtl8169_private *tp)
3010{
3011 rtl_writephy(tp, 0x1f, 0x0000);
3012 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3013}
3014
3015static void r810x_phy_power_up(struct rtl8169_private *tp)
3016{
3017 rtl_writephy(tp, 0x1f, 0x0000);
3018 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3019}
3020
3021static void r810x_pll_power_down(struct rtl8169_private *tp)
3022{
3023 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3024 rtl_writephy(tp, 0x1f, 0x0000);
3025 rtl_writephy(tp, MII_BMCR, 0x0000);
3026 return;
3027 }
3028
3029 r810x_phy_power_down(tp);
3030}
3031
3032static void r810x_pll_power_up(struct rtl8169_private *tp)
3033{
3034 r810x_phy_power_up(tp);
3035}
3036
3037static void r8168_phy_power_up(struct rtl8169_private *tp)
3038{
3039 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003040 switch (tp->mac_version) {
3041 case RTL_GIGA_MAC_VER_11:
3042 case RTL_GIGA_MAC_VER_12:
3043 case RTL_GIGA_MAC_VER_17:
3044 case RTL_GIGA_MAC_VER_18:
3045 case RTL_GIGA_MAC_VER_19:
3046 case RTL_GIGA_MAC_VER_20:
3047 case RTL_GIGA_MAC_VER_21:
3048 case RTL_GIGA_MAC_VER_22:
3049 case RTL_GIGA_MAC_VER_23:
3050 case RTL_GIGA_MAC_VER_24:
3051 case RTL_GIGA_MAC_VER_25:
3052 case RTL_GIGA_MAC_VER_26:
3053 case RTL_GIGA_MAC_VER_27:
3054 case RTL_GIGA_MAC_VER_28:
3055 case RTL_GIGA_MAC_VER_31:
3056 rtl_writephy(tp, 0x0e, 0x0000);
3057 break;
3058 default:
3059 break;
3060 }
françois romieu065c27c2011-01-03 15:08:12 +00003061 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3062}
3063
3064static void r8168_phy_power_down(struct rtl8169_private *tp)
3065{
3066 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003067 switch (tp->mac_version) {
3068 case RTL_GIGA_MAC_VER_32:
3069 case RTL_GIGA_MAC_VER_33:
3070 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3071 break;
3072
3073 case RTL_GIGA_MAC_VER_11:
3074 case RTL_GIGA_MAC_VER_12:
3075 case RTL_GIGA_MAC_VER_17:
3076 case RTL_GIGA_MAC_VER_18:
3077 case RTL_GIGA_MAC_VER_19:
3078 case RTL_GIGA_MAC_VER_20:
3079 case RTL_GIGA_MAC_VER_21:
3080 case RTL_GIGA_MAC_VER_22:
3081 case RTL_GIGA_MAC_VER_23:
3082 case RTL_GIGA_MAC_VER_24:
3083 case RTL_GIGA_MAC_VER_25:
3084 case RTL_GIGA_MAC_VER_26:
3085 case RTL_GIGA_MAC_VER_27:
3086 case RTL_GIGA_MAC_VER_28:
3087 case RTL_GIGA_MAC_VER_31:
3088 rtl_writephy(tp, 0x0e, 0x0200);
3089 default:
3090 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3091 break;
3092 }
françois romieu065c27c2011-01-03 15:08:12 +00003093}
3094
3095static void r8168_pll_power_down(struct rtl8169_private *tp)
3096{
3097 void __iomem *ioaddr = tp->mmio_addr;
3098
Francois Romieucecb5fd2011-04-01 10:21:07 +02003099 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3100 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3101 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003102 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003103 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003104 }
françois romieu065c27c2011-01-03 15:08:12 +00003105
Francois Romieucecb5fd2011-04-01 10:21:07 +02003106 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3107 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00003108 (RTL_R16(CPlusCmd) & ASF)) {
3109 return;
3110 }
3111
hayeswang01dc7fe2011-03-21 01:50:28 +00003112 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3113 tp->mac_version == RTL_GIGA_MAC_VER_33)
3114 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3115
françois romieu065c27c2011-01-03 15:08:12 +00003116 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3117 rtl_writephy(tp, 0x1f, 0x0000);
3118 rtl_writephy(tp, MII_BMCR, 0x0000);
3119
3120 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3121 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3122 return;
3123 }
3124
3125 r8168_phy_power_down(tp);
3126
3127 switch (tp->mac_version) {
3128 case RTL_GIGA_MAC_VER_25:
3129 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003130 case RTL_GIGA_MAC_VER_27:
3131 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003132 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003133 case RTL_GIGA_MAC_VER_32:
3134 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003135 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3136 break;
3137 }
3138}
3139
3140static void r8168_pll_power_up(struct rtl8169_private *tp)
3141{
3142 void __iomem *ioaddr = tp->mmio_addr;
3143
Francois Romieucecb5fd2011-04-01 10:21:07 +02003144 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3145 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3146 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003147 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003148 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003149 }
françois romieu065c27c2011-01-03 15:08:12 +00003150
3151 switch (tp->mac_version) {
3152 case RTL_GIGA_MAC_VER_25:
3153 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003154 case RTL_GIGA_MAC_VER_27:
3155 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003156 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003157 case RTL_GIGA_MAC_VER_32:
3158 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003159 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3160 break;
3161 }
3162
3163 r8168_phy_power_up(tp);
3164}
3165
3166static void rtl_pll_power_op(struct rtl8169_private *tp,
3167 void (*op)(struct rtl8169_private *))
3168{
3169 if (op)
3170 op(tp);
3171}
3172
3173static void rtl_pll_power_down(struct rtl8169_private *tp)
3174{
3175 rtl_pll_power_op(tp, tp->pll_power_ops.down);
3176}
3177
3178static void rtl_pll_power_up(struct rtl8169_private *tp)
3179{
3180 rtl_pll_power_op(tp, tp->pll_power_ops.up);
3181}
3182
3183static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3184{
3185 struct pll_power_ops *ops = &tp->pll_power_ops;
3186
3187 switch (tp->mac_version) {
3188 case RTL_GIGA_MAC_VER_07:
3189 case RTL_GIGA_MAC_VER_08:
3190 case RTL_GIGA_MAC_VER_09:
3191 case RTL_GIGA_MAC_VER_10:
3192 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003193 case RTL_GIGA_MAC_VER_29:
3194 case RTL_GIGA_MAC_VER_30:
françois romieu065c27c2011-01-03 15:08:12 +00003195 ops->down = r810x_pll_power_down;
3196 ops->up = r810x_pll_power_up;
3197 break;
3198
3199 case RTL_GIGA_MAC_VER_11:
3200 case RTL_GIGA_MAC_VER_12:
3201 case RTL_GIGA_MAC_VER_17:
3202 case RTL_GIGA_MAC_VER_18:
3203 case RTL_GIGA_MAC_VER_19:
3204 case RTL_GIGA_MAC_VER_20:
3205 case RTL_GIGA_MAC_VER_21:
3206 case RTL_GIGA_MAC_VER_22:
3207 case RTL_GIGA_MAC_VER_23:
3208 case RTL_GIGA_MAC_VER_24:
3209 case RTL_GIGA_MAC_VER_25:
3210 case RTL_GIGA_MAC_VER_26:
3211 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003212 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003213 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003214 case RTL_GIGA_MAC_VER_32:
3215 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003216 ops->down = r8168_pll_power_down;
3217 ops->up = r8168_pll_power_up;
3218 break;
3219
3220 default:
3221 ops->down = NULL;
3222 ops->up = NULL;
3223 break;
3224 }
3225}
3226
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003227static int __devinit
3228rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
3229{
Francois Romieu0e485152007-02-20 00:00:26 +01003230 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
3231 const unsigned int region = cfg->region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 struct rtl8169_private *tp;
Francois Romieuccdffb92008-07-26 14:26:06 +02003233 struct mii_if_info *mii;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003234 struct net_device *dev;
3235 void __iomem *ioaddr;
Francois Romieu2b7b4312011-04-18 22:53:24 -07003236 int chipset, i;
Francois Romieu07d3f512007-02-21 22:40:46 +01003237 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003239 if (netif_msg_drv(&debug)) {
3240 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
3241 MODULENAME, RTL8169_VERSION);
3242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 dev = alloc_etherdev(sizeof (*tp));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003245 if (!dev) {
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003246 if (netif_msg_drv(&debug))
Jeff Garzik9b91cf92006-06-27 11:39:50 -04003247 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003248 rc = -ENOMEM;
3249 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 }
3251
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieu8b4ab282008-11-19 22:05:25 -08003253 dev->netdev_ops = &rtl8169_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 tp = netdev_priv(dev);
David Howellsc4028952006-11-22 14:57:56 +00003255 tp->dev = dev;
Ivan Vecera21e197f2008-04-17 22:48:41 +02003256 tp->pci_dev = pdev;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003257 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258
Francois Romieuccdffb92008-07-26 14:26:06 +02003259 mii = &tp->mii;
3260 mii->dev = dev;
3261 mii->mdio_read = rtl_mdio_read;
3262 mii->mdio_write = rtl_mdio_write;
3263 mii->phy_id_mask = 0x1f;
3264 mii->reg_num_mask = 0x1f;
3265 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
3266
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +00003267 /* disable ASPM completely as that cause random device stop working
3268 * problems as well as full system hangs for some PCIe devices users */
3269 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
3270 PCIE_LINK_STATE_CLKPM);
3271
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 /* enable device (incl. PCI PM wakeup and hotplug setup) */
3273 rc = pci_enable_device(pdev);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003274 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003275 netif_err(tp, probe, dev, "enable failure\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003276 goto err_out_free_dev_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 }
3278
françois romieu87aeec72010-04-26 11:42:06 +00003279 if (pci_set_mwi(pdev) < 0)
3280 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 /* make sure PCI base addr 1 is MMIO */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003283 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003284 netif_err(tp, probe, dev,
3285 "region #%d not an MMIO resource, aborting\n",
3286 region);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003288 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003290
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 /* check for weird/broken PCI region reporting */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003292 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003293 netif_err(tp, probe, dev,
3294 "Invalid PCI region size(s), aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003296 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 }
3298
3299 rc = pci_request_regions(pdev, MODULENAME);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003300 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003301 netif_err(tp, probe, dev, "could not request regions\n");
françois romieu87aeec72010-04-26 11:42:06 +00003302 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 }
3304
Hayes Wangd24e9aa2011-02-22 17:26:19 +08003305 tp->cp_cmd = RxChkSum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306
3307 if ((sizeof(dma_addr_t) > 4) &&
David S. Miller4300e8c2010-03-26 10:23:30 -07003308 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 tp->cp_cmd |= PCIDAC;
3310 dev->features |= NETIF_F_HIGHDMA;
3311 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07003312 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003314 netif_err(tp, probe, dev, "DMA configuration failed\n");
françois romieu87aeec72010-04-26 11:42:06 +00003315 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 }
3317 }
3318
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 /* ioremap MMIO region */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003320 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003321 if (!ioaddr) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003322 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 rc = -EIO;
françois romieu87aeec72010-04-26 11:42:06 +00003324 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 }
3326
David S. Miller4300e8c2010-03-26 10:23:30 -07003327 tp->pcie_cap = pci_find_capability(pdev, PCI_CAP_ID_EXP);
3328 if (!tp->pcie_cap)
3329 netif_info(tp, probe, dev, "no PCI Express capability\n");
3330
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003331 RTL_W16(IntrMask, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332
3333 /* Soft reset the chip. */
3334 RTL_W8(ChipCmd, CmdReset);
3335
3336 /* Check that the chip has finished the reset. */
Francois Romieu07d3f512007-02-21 22:40:46 +01003337 for (i = 0; i < 100; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3339 break;
Francois Romieub518fa82006-08-16 15:23:13 +02003340 msleep_interruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 }
3342
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003343 RTL_W16(IntrStatus, 0xffff);
3344
françois romieuca52efd2009-07-24 12:34:19 +00003345 pci_set_master(pdev);
3346
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 /* Identify chip attached to board */
3348 rtl8169_get_mac_version(tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349
Francois Romieu7a8fc772011-03-01 17:18:33 +01003350 /*
3351 * Pretend we are using VLANs; This bypasses a nasty bug where
3352 * Interrupts stop flowing on high load on 8110SCd controllers.
3353 */
3354 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3355 tp->cp_cmd |= RxVlan;
3356
françois romieuc0e45c12011-01-03 15:08:04 +00003357 rtl_init_mdio_ops(tp);
françois romieu065c27c2011-01-03 15:08:12 +00003358 rtl_init_pll_power_ops(tp);
françois romieuc0e45c12011-01-03 15:08:04 +00003359
Jean Delvaref21b75e2009-05-26 20:54:48 -07003360 /* Use appropriate default if unknown */
3361 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003362 netif_notice(tp, probe, dev,
3363 "unknown MAC, using family default\n");
Jean Delvaref21b75e2009-05-26 20:54:48 -07003364 tp->mac_version = cfg->default_ver;
3365 }
3366
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 rtl8169_print_mac_version(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368
Roel Kluincee60c32008-04-17 22:35:54 +02003369 for (i = 0; i < ARRAY_SIZE(rtl_chip_info); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 if (tp->mac_version == rtl_chip_info[i].mac_version)
3371 break;
3372 }
Roel Kluincee60c32008-04-17 22:35:54 +02003373 if (i == ARRAY_SIZE(rtl_chip_info)) {
Jean Delvaref21b75e2009-05-26 20:54:48 -07003374 dev_err(&pdev->dev,
3375 "driver bug, MAC version not found in rtl_chip_info\n");
françois romieu87aeec72010-04-26 11:42:06 +00003376 goto err_out_msi_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 }
Francois Romieu2b7b4312011-04-18 22:53:24 -07003378 chipset = i;
3379 tp->txd_version = rtl_chip_info[chipset].txd_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380
Francois Romieu5d06a992006-02-23 00:47:58 +01003381 RTL_W8(Cfg9346, Cfg9346_Unlock);
3382 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
3383 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
Bruno Prémont20037fa2008-10-08 17:05:03 -07003384 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
3385 tp->features |= RTL_FEATURE_WOL;
3386 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
3387 tp->features |= RTL_FEATURE_WOL;
Francois Romieufbac58f2007-10-04 22:51:38 +02003388 tp->features |= rtl_try_msi(pdev, ioaddr, cfg);
Francois Romieu5d06a992006-02-23 00:47:58 +01003389 RTL_W8(Cfg9346, Cfg9346_Lock);
3390
Francois Romieu66ec5d42007-11-06 22:56:10 +01003391 if ((tp->mac_version <= RTL_GIGA_MAC_VER_06) &&
3392 (RTL_R8(PHYstatus) & TBI_Enable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 tp->set_speed = rtl8169_set_speed_tbi;
3394 tp->get_settings = rtl8169_gset_tbi;
3395 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
3396 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
3397 tp->link_ok = rtl8169_tbi_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003398 tp->do_ioctl = rtl_tbi_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399
Francois Romieu64e4bfb2006-08-17 12:43:06 +02003400 tp->phy_1000_ctrl_reg = ADVERTISE_1000FULL; /* Implied by TBI */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 } else {
3402 tp->set_speed = rtl8169_set_speed_xmii;
3403 tp->get_settings = rtl8169_gset_xmii;
3404 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
3405 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
3406 tp->link_ok = rtl8169_xmii_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003407 tp->do_ioctl = rtl_xmii_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 }
3409
Francois Romieudf58ef52008-10-09 14:35:58 -07003410 spin_lock_init(&tp->lock);
3411
Petr Vandrovec738e1e62008-10-12 20:58:29 -07003412 tp->mmio_addr = ioaddr;
3413
Ivan Vecera7bf6bf42008-09-23 22:46:29 +00003414 /* Get MAC address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 for (i = 0; i < MAC_ADDR_LEN; i++)
3416 dev->dev_addr[i] = RTL_R8(MAC0 + i);
John W. Linville6d6525b2005-09-12 10:48:57 -04003417 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
3421 dev->irq = pdev->irq;
3422 dev->base_addr = (unsigned long) ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003424 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425
Michał Mirosław350fb322011-04-08 06:35:56 +00003426 /* don't enable SG, IP_CSUM and TSO by default - it might not work
3427 * properly for all devices */
3428 dev->features |= NETIF_F_RXCSUM |
3429 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3430
3431 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3432 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3433 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3434 NETIF_F_HIGHDMA;
3435
3436 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3437 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
3438 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439
3440 tp->intr_mask = 0xffff;
Francois Romieu0e485152007-02-20 00:00:26 +01003441 tp->hw_start = cfg->hw_start;
3442 tp->intr_event = cfg->intr_event;
3443 tp->napi_event = cfg->napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444
Francois Romieu2efa53f2007-03-09 00:00:05 +01003445 init_timer(&tp->timer);
3446 tp->timer.data = (unsigned long) dev;
3447 tp->timer.function = rtl8169_phy_timer;
3448
François Romieu953a12c2011-04-24 17:38:48 +02003449 tp->fw = RTL_FIRMWARE_UNKNOWN;
3450
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 rc = register_netdev(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003452 if (rc < 0)
françois romieu87aeec72010-04-26 11:42:06 +00003453 goto err_out_msi_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454
3455 pci_set_drvdata(pdev, dev);
3456
Joe Perchesbf82c182010-02-09 11:49:50 +00003457 netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
Francois Romieu2b7b4312011-04-18 22:53:24 -07003458 rtl_chip_info[chipset].name, dev->base_addr, dev->dev_addr,
Joe Perchesbf82c182010-02-09 11:49:50 +00003459 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460
Francois Romieucecb5fd2011-04-01 10:21:07 +02003461 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3462 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3463 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00003464 rtl8168_driver_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003465 }
françois romieub646d902011-01-03 15:08:21 +00003466
Bruno Prémont8b76ab32008-10-08 17:06:25 -07003467 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468
Alan Sternf3ec4f82010-06-08 15:23:51 -04003469 if (pci_dev_run_wake(pdev))
3470 pm_runtime_put_noidle(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003471
Ivan Vecera0d672e92011-02-15 02:08:39 +00003472 netif_carrier_off(dev);
3473
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003474out:
3475 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476
françois romieu87aeec72010-04-26 11:42:06 +00003477err_out_msi_4:
Francois Romieufbac58f2007-10-04 22:51:38 +02003478 rtl_disable_msi(pdev, tp);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003479 iounmap(ioaddr);
françois romieu87aeec72010-04-26 11:42:06 +00003480err_out_free_res_3:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003481 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003482err_out_mwi_2:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003483 pci_clear_mwi(pdev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003484 pci_disable_device(pdev);
3485err_out_free_dev_1:
3486 free_netdev(dev);
3487 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488}
3489
Francois Romieu07d3f512007-02-21 22:40:46 +01003490static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491{
3492 struct net_device *dev = pci_get_drvdata(pdev);
3493 struct rtl8169_private *tp = netdev_priv(dev);
3494
Francois Romieucecb5fd2011-04-01 10:21:07 +02003495 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3496 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3497 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00003498 rtl8168_driver_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003499 }
françois romieub646d902011-01-03 15:08:21 +00003500
Tejun Heo23f333a2010-12-12 16:45:14 +01003501 cancel_delayed_work_sync(&tp->task);
Francois Romieueb2a0212007-02-15 23:37:21 +01003502
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 unregister_netdev(dev);
Ivan Veceracc098dc2009-11-29 23:12:52 -08003504
François Romieu953a12c2011-04-24 17:38:48 +02003505 rtl_release_firmware(tp);
3506
Alan Sternf3ec4f82010-06-08 15:23:51 -04003507 if (pci_dev_run_wake(pdev))
3508 pm_runtime_get_noresume(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003509
Ivan Veceracc098dc2009-11-29 23:12:52 -08003510 /* restore original MAC address */
3511 rtl_rar_set(tp, dev->perm_addr);
3512
Francois Romieufbac58f2007-10-04 22:51:38 +02003513 rtl_disable_msi(pdev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 rtl8169_release_board(pdev, dev, tp->mmio_addr);
3515 pci_set_drvdata(pdev, NULL);
3516}
3517
François Romieu953a12c2011-04-24 17:38:48 +02003518static void rtl_request_firmware(struct rtl8169_private *tp)
3519{
3520 int i;
3521
3522 /* Return early if the firmware is already loaded / cached. */
3523 if (!IS_ERR(tp->fw))
3524 goto out;
3525
3526 for (i = 0; i < ARRAY_SIZE(rtl_firmware_infos); i++) {
3527 const struct rtl_firmware_info *info = rtl_firmware_infos + i;
3528
3529 if (info->mac_version == tp->mac_version) {
3530 const char *name = info->fw_name;
3531 int rc;
3532
3533 rc = request_firmware(&tp->fw, name, &tp->pci_dev->dev);
3534 if (rc < 0) {
3535 netif_warn(tp, ifup, tp->dev, "unable to load "
3536 "firmware patch %s (%d)\n", name, rc);
3537 goto out_disable_request_firmware;
3538 }
3539 goto out;
3540 }
3541 }
3542
3543out_disable_request_firmware:
3544 tp->fw = NULL;
3545out:
3546 return;
3547}
3548
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549static int rtl8169_open(struct net_device *dev)
3550{
3551 struct rtl8169_private *tp = netdev_priv(dev);
françois romieueee3a962011-01-08 02:17:26 +00003552 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu99f252b2007-04-02 22:59:59 +02003554 int retval = -ENOMEM;
3555
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003556 pm_runtime_get_sync(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557
Neil Hormanc0cd8842010-03-29 13:16:02 -07003558 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 * Rx and Tx desscriptors needs 256 bytes alignment.
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003560 * dma_alloc_coherent provides more.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 */
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003562 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
3563 &tp->TxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 if (!tp->TxDescArray)
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003565 goto err_pm_runtime_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003567 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
3568 &tp->RxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 if (!tp->RxDescArray)
Francois Romieu99f252b2007-04-02 22:59:59 +02003570 goto err_free_tx_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571
3572 retval = rtl8169_init_ring(dev);
3573 if (retval < 0)
Francois Romieu99f252b2007-04-02 22:59:59 +02003574 goto err_free_rx_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575
David Howellsc4028952006-11-22 14:57:56 +00003576 INIT_DELAYED_WORK(&tp->task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577
Francois Romieu99f252b2007-04-02 22:59:59 +02003578 smp_mb();
3579
François Romieu953a12c2011-04-24 17:38:48 +02003580 rtl_request_firmware(tp);
3581
Francois Romieufbac58f2007-10-04 22:51:38 +02003582 retval = request_irq(dev->irq, rtl8169_interrupt,
3583 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
Francois Romieu99f252b2007-04-02 22:59:59 +02003584 dev->name, dev);
3585 if (retval < 0)
François Romieu953a12c2011-04-24 17:38:48 +02003586 goto err_release_fw_2;
Francois Romieu99f252b2007-04-02 22:59:59 +02003587
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003588 napi_enable(&tp->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003589
françois romieueee3a962011-01-08 02:17:26 +00003590 rtl8169_init_phy(dev, tp);
3591
Michał Mirosław350fb322011-04-08 06:35:56 +00003592 rtl8169_set_features(dev, dev->features);
françois romieueee3a962011-01-08 02:17:26 +00003593
françois romieu065c27c2011-01-03 15:08:12 +00003594 rtl_pll_power_up(tp);
3595
Francois Romieu07ce4062007-02-23 23:36:39 +01003596 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597
3598 rtl8169_request_timer(dev);
3599
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003600 tp->saved_wolopts = 0;
3601 pm_runtime_put_noidle(&pdev->dev);
3602
françois romieueee3a962011-01-08 02:17:26 +00003603 rtl8169_check_link_status(dev, tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604out:
3605 return retval;
3606
François Romieu953a12c2011-04-24 17:38:48 +02003607err_release_fw_2:
3608 rtl_release_firmware(tp);
Francois Romieu99f252b2007-04-02 22:59:59 +02003609 rtl8169_rx_clear(tp);
3610err_free_rx_1:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003611 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
3612 tp->RxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003613 tp->RxDescArray = NULL;
Francois Romieu99f252b2007-04-02 22:59:59 +02003614err_free_tx_0:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003615 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
3616 tp->TxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003617 tp->TxDescArray = NULL;
3618err_pm_runtime_put:
3619 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 goto out;
3621}
3622
françois romieue6de30d2011-01-03 15:08:37 +00003623static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624{
françois romieue6de30d2011-01-03 15:08:37 +00003625 void __iomem *ioaddr = tp->mmio_addr;
3626
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 /* Disable interrupts */
3628 rtl8169_irq_mask_and_ack(ioaddr);
3629
Hayes Wang5d2e1952011-02-22 17:26:22 +08003630 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00003631 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3632 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieue6de30d2011-01-03 15:08:37 +00003633 while (RTL_R8(TxPoll) & NPQ)
3634 udelay(20);
3635
3636 }
3637
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 /* Reset the chipset */
3639 RTL_W8(ChipCmd, CmdReset);
3640
3641 /* PCI commit */
3642 RTL_R8(ChipCmd);
3643}
3644
Francois Romieu7f796d82007-06-11 23:04:41 +02003645static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01003646{
3647 void __iomem *ioaddr = tp->mmio_addr;
3648 u32 cfg = rtl8169_rx_config;
3649
Francois Romieu2b7b4312011-04-18 22:53:24 -07003650 cfg |= (RTL_R32(RxConfig) & RTL_RX_CONFIG_MASK);
Francois Romieu9cb427b2006-11-02 00:10:16 +01003651 RTL_W32(RxConfig, cfg);
3652
3653 /* Set DMA burst size and Interframe Gap Time */
3654 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
3655 (InterFrameGap << TxInterFrameGapShift));
3656}
3657
Francois Romieu07ce4062007-02-23 23:36:39 +01003658static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659{
3660 struct rtl8169_private *tp = netdev_priv(dev);
3661 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01003662 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663
3664 /* Soft reset the chip. */
3665 RTL_W8(ChipCmd, CmdReset);
3666
3667 /* Check that the chip has finished the reset. */
Francois Romieu07d3f512007-02-21 22:40:46 +01003668 for (i = 0; i < 100; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3670 break;
Francois Romieub518fa82006-08-16 15:23:13 +02003671 msleep_interruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 }
3673
Francois Romieu07ce4062007-02-23 23:36:39 +01003674 tp->hw_start(dev);
3675
Francois Romieu07ce4062007-02-23 23:36:39 +01003676 netif_start_queue(dev);
3677}
3678
3679
Francois Romieu7f796d82007-06-11 23:04:41 +02003680static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
3681 void __iomem *ioaddr)
3682{
3683 /*
3684 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
3685 * register to be written before TxDescAddrLow to work.
3686 * Switching from MMIO to I/O access fixes the issue as well.
3687 */
3688 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07003689 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02003690 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07003691 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02003692}
3693
3694static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
3695{
3696 u16 cmd;
3697
3698 cmd = RTL_R16(CPlusCmd);
3699 RTL_W16(CPlusCmd, cmd);
3700 return cmd;
3701}
3702
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07003703static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d82007-06-11 23:04:41 +02003704{
3705 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e82009-10-26 10:52:37 +00003706 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d82007-06-11 23:04:41 +02003707}
3708
Francois Romieu6dccd162007-02-13 23:38:05 +01003709static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
3710{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003711 static const struct {
Francois Romieu6dccd162007-02-13 23:38:05 +01003712 u32 mac_version;
3713 u32 clk;
3714 u32 val;
3715 } cfg2_info [] = {
3716 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
3717 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
3718 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
3719 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
3720 }, *p = cfg2_info;
3721 unsigned int i;
3722 u32 clk;
3723
3724 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01003725 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01003726 if ((p->mac_version == mac_version) && (p->clk == clk)) {
3727 RTL_W32(0x7c, p->val);
3728 break;
3729 }
3730 }
3731}
3732
Francois Romieu07ce4062007-02-23 23:36:39 +01003733static void rtl_hw_start_8169(struct net_device *dev)
3734{
3735 struct rtl8169_private *tp = netdev_priv(dev);
3736 void __iomem *ioaddr = tp->mmio_addr;
3737 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01003738
Francois Romieu9cb427b2006-11-02 00:10:16 +01003739 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
3740 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
3741 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
3742 }
3743
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02003745 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
3746 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
3747 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
3748 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01003749 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3750
françois romieuf0298f82011-01-03 15:07:42 +00003751 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752
Eric Dumazet6f0333b2010-10-11 11:17:47 +00003753 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754
Francois Romieucecb5fd2011-04-01 10:21:07 +02003755 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)
Francois Romieuc946b302007-10-04 00:42:50 +02003759 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760
Francois Romieu7f796d82007-06-11 23:04:41 +02003761 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02003762
Francois Romieucecb5fd2011-04-01 10:21:07 +02003763 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
3764 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02003765 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02003767 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 }
3769
Francois Romieubcf0bf92006-07-26 23:14:13 +02003770 RTL_W16(CPlusCmd, tp->cp_cmd);
3771
Francois Romieu6dccd162007-02-13 23:38:05 +01003772 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
3773
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 /*
3775 * Undocumented corner. Supposedly:
3776 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
3777 */
3778 RTL_W16(IntrMitigate, 0x0000);
3779
Francois Romieu7f796d82007-06-11 23:04:41 +02003780 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01003781
Francois Romieucecb5fd2011-04-01 10:21:07 +02003782 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
3783 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
3784 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
3785 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02003786 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3787 rtl_set_rx_tx_config_registers(tp);
3788 }
3789
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02003791
3792 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
3793 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794
3795 RTL_W32(RxMissed, 0);
3796
Francois Romieu07ce4062007-02-23 23:36:39 +01003797 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798
3799 /* no early-rx interrupts */
3800 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01003801
3802 /* Enable all known interrupts by setting the interrupt mask. */
Francois Romieu0e485152007-02-20 00:00:26 +01003803 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01003804}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805
Francois Romieu9c14cea2008-07-05 00:21:15 +02003806static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
Francois Romieu458a9f62008-08-02 15:50:02 +02003807{
Francois Romieu9c14cea2008-07-05 00:21:15 +02003808 struct net_device *dev = pci_get_drvdata(pdev);
3809 struct rtl8169_private *tp = netdev_priv(dev);
3810 int cap = tp->pcie_cap;
Francois Romieu458a9f62008-08-02 15:50:02 +02003811
Francois Romieu9c14cea2008-07-05 00:21:15 +02003812 if (cap) {
3813 u16 ctl;
3814
3815 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
3816 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
3817 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
3818 }
Francois Romieu458a9f62008-08-02 15:50:02 +02003819}
3820
françois romieu650e8d52011-01-03 15:08:29 +00003821static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02003822{
3823 u32 csi;
3824
3825 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
françois romieu650e8d52011-01-03 15:08:29 +00003826 rtl_csi_write(ioaddr, 0x070c, csi | bits);
3827}
3828
françois romieue6de30d2011-01-03 15:08:37 +00003829static void rtl_csi_access_enable_1(void __iomem *ioaddr)
3830{
3831 rtl_csi_access_enable(ioaddr, 0x17000000);
3832}
3833
françois romieu650e8d52011-01-03 15:08:29 +00003834static void rtl_csi_access_enable_2(void __iomem *ioaddr)
3835{
3836 rtl_csi_access_enable(ioaddr, 0x27000000);
Francois Romieudacf8152008-08-02 20:44:13 +02003837}
3838
3839struct ephy_info {
3840 unsigned int offset;
3841 u16 mask;
3842 u16 bits;
3843};
3844
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003845static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02003846{
3847 u16 w;
3848
3849 while (len-- > 0) {
3850 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
3851 rtl_ephy_write(ioaddr, e->offset, w);
3852 e++;
3853 }
3854}
3855
Francois Romieub726e492008-06-28 12:22:59 +02003856static void rtl_disable_clock_request(struct pci_dev *pdev)
3857{
3858 struct net_device *dev = pci_get_drvdata(pdev);
3859 struct rtl8169_private *tp = netdev_priv(dev);
3860 int cap = tp->pcie_cap;
3861
3862 if (cap) {
3863 u16 ctl;
3864
3865 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
3866 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
3867 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
3868 }
3869}
3870
françois romieue6de30d2011-01-03 15:08:37 +00003871static void rtl_enable_clock_request(struct pci_dev *pdev)
3872{
3873 struct net_device *dev = pci_get_drvdata(pdev);
3874 struct rtl8169_private *tp = netdev_priv(dev);
3875 int cap = tp->pcie_cap;
3876
3877 if (cap) {
3878 u16 ctl;
3879
3880 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
3881 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
3882 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
3883 }
3884}
3885
Francois Romieub726e492008-06-28 12:22:59 +02003886#define R8168_CPCMD_QUIRK_MASK (\
3887 EnableBist | \
3888 Mac_dbgo_oe | \
3889 Force_half_dup | \
3890 Force_rxflow_en | \
3891 Force_txflow_en | \
3892 Cxpl_dbg_sel | \
3893 ASF | \
3894 PktCntrDisable | \
3895 Mac_dbgo_sel)
3896
Francois Romieu219a1e92008-06-28 11:58:39 +02003897static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
3898{
Francois Romieub726e492008-06-28 12:22:59 +02003899 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3900
3901 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3902
Francois Romieu2e68ae42008-06-28 12:00:55 +02003903 rtl_tx_performance_tweak(pdev,
3904 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02003905}
3906
3907static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
3908{
3909 rtl_hw_start_8168bb(ioaddr, pdev);
Francois Romieub726e492008-06-28 12:22:59 +02003910
françois romieuf0298f82011-01-03 15:07:42 +00003911 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02003912
3913 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02003914}
3915
3916static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
3917{
Francois Romieub726e492008-06-28 12:22:59 +02003918 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
3919
3920 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3921
Francois Romieu219a1e92008-06-28 11:58:39 +02003922 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02003923
3924 rtl_disable_clock_request(pdev);
3925
3926 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02003927}
3928
Francois Romieuef3386f2008-06-29 12:24:30 +02003929static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
Francois Romieu219a1e92008-06-28 11:58:39 +02003930{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003931 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02003932 { 0x01, 0, 0x0001 },
3933 { 0x02, 0x0800, 0x1000 },
3934 { 0x03, 0, 0x0042 },
3935 { 0x06, 0x0080, 0x0000 },
3936 { 0x07, 0, 0x2000 }
3937 };
3938
françois romieu650e8d52011-01-03 15:08:29 +00003939 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02003940
3941 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
3942
Francois Romieu219a1e92008-06-28 11:58:39 +02003943 __rtl_hw_start_8168cp(ioaddr, pdev);
3944}
3945
Francois Romieuef3386f2008-06-29 12:24:30 +02003946static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
3947{
françois romieu650e8d52011-01-03 15:08:29 +00003948 rtl_csi_access_enable_2(ioaddr);
Francois Romieuef3386f2008-06-29 12:24:30 +02003949
3950 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3951
3952 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3953
3954 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3955}
3956
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003957static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
3958{
françois romieu650e8d52011-01-03 15:08:29 +00003959 rtl_csi_access_enable_2(ioaddr);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003960
3961 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3962
3963 /* Magic. */
3964 RTL_W8(DBG_REG, 0x20);
3965
françois romieuf0298f82011-01-03 15:07:42 +00003966 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003967
3968 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3969
3970 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3971}
3972
Francois Romieu219a1e92008-06-28 11:58:39 +02003973static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
3974{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003975 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02003976 { 0x02, 0x0800, 0x1000 },
3977 { 0x03, 0, 0x0002 },
3978 { 0x06, 0x0080, 0x0000 }
3979 };
3980
françois romieu650e8d52011-01-03 15:08:29 +00003981 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02003982
3983 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
3984
3985 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
3986
Francois Romieu219a1e92008-06-28 11:58:39 +02003987 __rtl_hw_start_8168cp(ioaddr, pdev);
3988}
3989
3990static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
3991{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003992 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02003993 { 0x01, 0, 0x0001 },
3994 { 0x03, 0x0400, 0x0220 }
3995 };
3996
françois romieu650e8d52011-01-03 15:08:29 +00003997 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02003998
3999 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4000
Francois Romieu219a1e92008-06-28 11:58:39 +02004001 __rtl_hw_start_8168cp(ioaddr, pdev);
4002}
4003
Francois Romieu197ff762008-06-28 13:16:02 +02004004static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
4005{
4006 rtl_hw_start_8168c_2(ioaddr, pdev);
4007}
4008
Francois Romieu6fb07052008-06-29 11:54:28 +02004009static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
4010{
françois romieu650e8d52011-01-03 15:08:29 +00004011 rtl_csi_access_enable_2(ioaddr);
Francois Romieu6fb07052008-06-29 11:54:28 +02004012
4013 __rtl_hw_start_8168cp(ioaddr, pdev);
4014}
4015
Francois Romieu5b538df2008-07-20 16:22:45 +02004016static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
4017{
françois romieu650e8d52011-01-03 15:08:29 +00004018 rtl_csi_access_enable_2(ioaddr);
Francois Romieu5b538df2008-07-20 16:22:45 +02004019
4020 rtl_disable_clock_request(pdev);
4021
françois romieuf0298f82011-01-03 15:07:42 +00004022 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004023
4024 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4025
4026 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4027}
4028
hayeswang4804b3b2011-03-21 01:50:29 +00004029static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
4030{
4031 rtl_csi_access_enable_1(ioaddr);
4032
4033 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4034
4035 RTL_W8(MaxTxPacketSize, TxPacketMax);
4036
4037 rtl_disable_clock_request(pdev);
4038}
4039
françois romieue6de30d2011-01-03 15:08:37 +00004040static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4041{
4042 static const struct ephy_info e_info_8168d_4[] = {
4043 { 0x0b, ~0, 0x48 },
4044 { 0x19, 0x20, 0x50 },
4045 { 0x0c, ~0, 0x20 }
4046 };
4047 int i;
4048
4049 rtl_csi_access_enable_1(ioaddr);
4050
4051 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4052
4053 RTL_W8(MaxTxPacketSize, TxPacketMax);
4054
4055 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4056 const struct ephy_info *e = e_info_8168d_4 + i;
4057 u16 w;
4058
4059 w = rtl_ephy_read(ioaddr, e->offset);
4060 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4061 }
4062
4063 rtl_enable_clock_request(pdev);
4064}
4065
hayeswang01dc7fe2011-03-21 01:50:28 +00004066static void rtl_hw_start_8168e(void __iomem *ioaddr, struct pci_dev *pdev)
4067{
4068 static const struct ephy_info e_info_8168e[] = {
4069 { 0x00, 0x0200, 0x0100 },
4070 { 0x00, 0x0000, 0x0004 },
4071 { 0x06, 0x0002, 0x0001 },
4072 { 0x06, 0x0000, 0x0030 },
4073 { 0x07, 0x0000, 0x2000 },
4074 { 0x00, 0x0000, 0x0020 },
4075 { 0x03, 0x5800, 0x2000 },
4076 { 0x03, 0x0000, 0x0001 },
4077 { 0x01, 0x0800, 0x1000 },
4078 { 0x07, 0x0000, 0x4000 },
4079 { 0x1e, 0x0000, 0x2000 },
4080 { 0x19, 0xffff, 0xfe6c },
4081 { 0x0a, 0x0000, 0x0040 }
4082 };
4083
4084 rtl_csi_access_enable_2(ioaddr);
4085
4086 rtl_ephy_init(ioaddr, e_info_8168e, ARRAY_SIZE(e_info_8168e));
4087
4088 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4089
4090 RTL_W8(MaxTxPacketSize, TxPacketMax);
4091
4092 rtl_disable_clock_request(pdev);
4093
4094 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02004095 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4096 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004097
Francois Romieucecb5fd2011-04-01 10:21:07 +02004098 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004099}
4100
Francois Romieu07ce4062007-02-23 23:36:39 +01004101static void rtl_hw_start_8168(struct net_device *dev)
4102{
Francois Romieu2dd99532007-06-11 23:22:52 +02004103 struct rtl8169_private *tp = netdev_priv(dev);
4104 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01004105 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu2dd99532007-06-11 23:22:52 +02004106
4107 RTL_W8(Cfg9346, Cfg9346_Unlock);
4108
françois romieuf0298f82011-01-03 15:07:42 +00004109 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004110
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004111 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004112
Francois Romieu0e485152007-02-20 00:00:26 +01004113 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004114
4115 RTL_W16(CPlusCmd, tp->cp_cmd);
4116
Francois Romieu0e485152007-02-20 00:00:26 +01004117 RTL_W16(IntrMitigate, 0x5151);
4118
4119 /* Work around for RxFIFO overflow. */
Ivan Vecerab5ba6d12011-01-27 12:24:11 +01004120 if (tp->mac_version == RTL_GIGA_MAC_VER_11 ||
4121 tp->mac_version == RTL_GIGA_MAC_VER_22) {
Francois Romieu0e485152007-02-20 00:00:26 +01004122 tp->intr_event |= RxFIFOOver | PCSTimeout;
4123 tp->intr_event &= ~RxOverflow;
4124 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004125
4126 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4127
Francois Romieub8363902008-06-01 12:31:57 +02004128 rtl_set_rx_mode(dev);
4129
4130 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4131 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004132
4133 RTL_R8(IntrMask);
4134
Francois Romieu219a1e92008-06-28 11:58:39 +02004135 switch (tp->mac_version) {
4136 case RTL_GIGA_MAC_VER_11:
4137 rtl_hw_start_8168bb(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004138 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004139
4140 case RTL_GIGA_MAC_VER_12:
4141 case RTL_GIGA_MAC_VER_17:
4142 rtl_hw_start_8168bef(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004143 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004144
4145 case RTL_GIGA_MAC_VER_18:
Francois Romieuef3386f2008-06-29 12:24:30 +02004146 rtl_hw_start_8168cp_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004147 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004148
4149 case RTL_GIGA_MAC_VER_19:
4150 rtl_hw_start_8168c_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004151 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004152
4153 case RTL_GIGA_MAC_VER_20:
4154 rtl_hw_start_8168c_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004155 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004156
Francois Romieu197ff762008-06-28 13:16:02 +02004157 case RTL_GIGA_MAC_VER_21:
4158 rtl_hw_start_8168c_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004159 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004160
Francois Romieu6fb07052008-06-29 11:54:28 +02004161 case RTL_GIGA_MAC_VER_22:
4162 rtl_hw_start_8168c_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004163 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004164
Francois Romieuef3386f2008-06-29 12:24:30 +02004165 case RTL_GIGA_MAC_VER_23:
4166 rtl_hw_start_8168cp_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004167 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004168
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004169 case RTL_GIGA_MAC_VER_24:
4170 rtl_hw_start_8168cp_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004171 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004172
Francois Romieu5b538df2008-07-20 16:22:45 +02004173 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00004174 case RTL_GIGA_MAC_VER_26:
4175 case RTL_GIGA_MAC_VER_27:
Francois Romieu5b538df2008-07-20 16:22:45 +02004176 rtl_hw_start_8168d(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004177 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004178
françois romieue6de30d2011-01-03 15:08:37 +00004179 case RTL_GIGA_MAC_VER_28:
4180 rtl_hw_start_8168d_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004181 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004182
hayeswang4804b3b2011-03-21 01:50:29 +00004183 case RTL_GIGA_MAC_VER_31:
4184 rtl_hw_start_8168dp(ioaddr, pdev);
4185 break;
4186
hayeswang01dc7fe2011-03-21 01:50:28 +00004187 case RTL_GIGA_MAC_VER_32:
4188 case RTL_GIGA_MAC_VER_33:
4189 rtl_hw_start_8168e(ioaddr, pdev);
4190 break;
françois romieue6de30d2011-01-03 15:08:37 +00004191
Francois Romieu219a1e92008-06-28 11:58:39 +02004192 default:
4193 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
4194 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00004195 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004196 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004197
Francois Romieu0e485152007-02-20 00:00:26 +01004198 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4199
Francois Romieub8363902008-06-01 12:31:57 +02004200 RTL_W8(Cfg9346, Cfg9346_Lock);
4201
Francois Romieu2dd99532007-06-11 23:22:52 +02004202 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004203
Francois Romieu0e485152007-02-20 00:00:26 +01004204 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01004205}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206
Francois Romieu2857ffb2008-08-02 21:08:49 +02004207#define R810X_CPCMD_QUIRK_MASK (\
4208 EnableBist | \
4209 Mac_dbgo_oe | \
4210 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00004211 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02004212 Force_txflow_en | \
4213 Cxpl_dbg_sel | \
4214 ASF | \
4215 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004216 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004217
4218static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4219{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004220 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004221 { 0x01, 0, 0x6e65 },
4222 { 0x02, 0, 0x091f },
4223 { 0x03, 0, 0xc2f9 },
4224 { 0x06, 0, 0xafb5 },
4225 { 0x07, 0, 0x0e00 },
4226 { 0x19, 0, 0xec80 },
4227 { 0x01, 0, 0x2e65 },
4228 { 0x01, 0, 0x6e65 }
4229 };
4230 u8 cfg1;
4231
françois romieu650e8d52011-01-03 15:08:29 +00004232 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004233
4234 RTL_W8(DBG_REG, FIX_NAK_1);
4235
4236 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4237
4238 RTL_W8(Config1,
4239 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
4240 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4241
4242 cfg1 = RTL_R8(Config1);
4243 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
4244 RTL_W8(Config1, cfg1 & ~LEDS0);
4245
Francois Romieu2857ffb2008-08-02 21:08:49 +02004246 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
4247}
4248
4249static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4250{
françois romieu650e8d52011-01-03 15:08:29 +00004251 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004252
4253 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4254
4255 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
4256 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004257}
4258
4259static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
4260{
4261 rtl_hw_start_8102e_2(ioaddr, pdev);
4262
4263 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
4264}
4265
Hayes Wang5a5e4442011-02-22 17:26:21 +08004266static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4267{
4268 static const struct ephy_info e_info_8105e_1[] = {
4269 { 0x07, 0, 0x4000 },
4270 { 0x19, 0, 0x0200 },
4271 { 0x19, 0, 0x0020 },
4272 { 0x1e, 0, 0x2000 },
4273 { 0x03, 0, 0x0001 },
4274 { 0x19, 0, 0x0100 },
4275 { 0x19, 0, 0x0004 },
4276 { 0x0a, 0, 0x0020 }
4277 };
4278
Francois Romieucecb5fd2011-04-01 10:21:07 +02004279 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004280 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
4281
Francois Romieucecb5fd2011-04-01 10:21:07 +02004282 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004283 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
4284
4285 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
4286 RTL_W8(DLLPR, RTL_R8(DLLPR) | PM_SWITCH);
4287
4288 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
4289}
4290
4291static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4292{
4293 rtl_hw_start_8105e_1(ioaddr, pdev);
4294 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
4295}
4296
Francois Romieu07ce4062007-02-23 23:36:39 +01004297static void rtl_hw_start_8101(struct net_device *dev)
4298{
Francois Romieucdf1a602007-06-11 23:29:50 +02004299 struct rtl8169_private *tp = netdev_priv(dev);
4300 void __iomem *ioaddr = tp->mmio_addr;
4301 struct pci_dev *pdev = tp->pci_dev;
4302
Francois Romieucecb5fd2011-04-01 10:21:07 +02004303 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
4304 tp->mac_version == RTL_GIGA_MAC_VER_16) {
Francois Romieu9c14cea2008-07-05 00:21:15 +02004305 int cap = tp->pcie_cap;
4306
4307 if (cap) {
4308 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
4309 PCI_EXP_DEVCTL_NOSNOOP_EN);
4310 }
Francois Romieucdf1a602007-06-11 23:29:50 +02004311 }
4312
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004313 RTL_W8(Cfg9346, Cfg9346_Unlock);
4314
Francois Romieu2857ffb2008-08-02 21:08:49 +02004315 switch (tp->mac_version) {
4316 case RTL_GIGA_MAC_VER_07:
4317 rtl_hw_start_8102e_1(ioaddr, pdev);
4318 break;
4319
4320 case RTL_GIGA_MAC_VER_08:
4321 rtl_hw_start_8102e_3(ioaddr, pdev);
4322 break;
4323
4324 case RTL_GIGA_MAC_VER_09:
4325 rtl_hw_start_8102e_2(ioaddr, pdev);
4326 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004327
4328 case RTL_GIGA_MAC_VER_29:
4329 rtl_hw_start_8105e_1(ioaddr, pdev);
4330 break;
4331 case RTL_GIGA_MAC_VER_30:
4332 rtl_hw_start_8105e_2(ioaddr, pdev);
4333 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02004334 }
4335
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004336 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02004337
françois romieuf0298f82011-01-03 15:07:42 +00004338 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02004339
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004340 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02004341
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004342 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02004343 RTL_W16(CPlusCmd, tp->cp_cmd);
4344
4345 RTL_W16(IntrMitigate, 0x0000);
4346
4347 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4348
4349 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4350 rtl_set_rx_tx_config_registers(tp);
4351
Francois Romieucdf1a602007-06-11 23:29:50 +02004352 RTL_R8(IntrMask);
4353
Francois Romieucdf1a602007-06-11 23:29:50 +02004354 rtl_set_rx_mode(dev);
4355
4356 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004357
Francois Romieu0e485152007-02-20 00:00:26 +01004358 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359}
4360
4361static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
4362{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 if (new_mtu < ETH_ZLEN || new_mtu > SafeMtu)
4364 return -EINVAL;
4365
4366 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00004367 netdev_update_features(dev);
4368
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00004369 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370}
4371
4372static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
4373{
Al Viro95e09182007-12-22 18:55:39 +00004374 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
4376}
4377
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004378static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
4379 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004381 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004382 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004383
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004384 kfree(*data_buff);
4385 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 rtl8169_make_unusable_by_asic(desc);
4387}
4388
4389static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
4390{
4391 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
4392
4393 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
4394}
4395
4396static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
4397 u32 rx_buf_sz)
4398{
4399 desc->addr = cpu_to_le64(mapping);
4400 wmb();
4401 rtl8169_mark_to_asic(desc, rx_buf_sz);
4402}
4403
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004404static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004406 return (void *)ALIGN((long)data, 16);
4407}
4408
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004409static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
4410 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004411{
4412 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004414 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004415 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004416 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004418 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
4419 if (!data)
4420 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01004421
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004422 if (rtl8169_align(data) != data) {
4423 kfree(data);
4424 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
4425 if (!data)
4426 return NULL;
4427 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004428
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004429 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004430 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004431 if (unlikely(dma_mapping_error(d, mapping))) {
4432 if (net_ratelimit())
4433 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004434 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436
4437 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004438 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004439
4440err_out:
4441 kfree(data);
4442 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443}
4444
4445static void rtl8169_rx_clear(struct rtl8169_private *tp)
4446{
Francois Romieu07d3f512007-02-21 22:40:46 +01004447 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448
4449 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004450 if (tp->Rx_databuff[i]) {
4451 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 tp->RxDescArray + i);
4453 }
4454 }
4455}
4456
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004457static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004459 desc->opts1 |= cpu_to_le32(RingEnd);
4460}
Francois Romieu5b0384f2006-08-16 16:00:01 +02004461
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004462static int rtl8169_rx_fill(struct rtl8169_private *tp)
4463{
4464 unsigned int i;
4465
4466 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004467 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02004468
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004469 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004471
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004472 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004473 if (!data) {
4474 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004475 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004476 }
4477 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004480 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
4481 return 0;
4482
4483err_out:
4484 rtl8169_rx_clear(tp);
4485 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486}
4487
4488static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4489{
4490 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
4491}
4492
4493static int rtl8169_init_ring(struct net_device *dev)
4494{
4495 struct rtl8169_private *tp = netdev_priv(dev);
4496
4497 rtl8169_init_ring_indexes(tp);
4498
4499 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004500 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004502 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503}
4504
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004505static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 struct TxDesc *desc)
4507{
4508 unsigned int len = tx_skb->len;
4509
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004510 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
4511
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 desc->opts1 = 0x00;
4513 desc->opts2 = 0x00;
4514 desc->addr = 0x00;
4515 tx_skb->len = 0;
4516}
4517
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004518static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
4519 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520{
4521 unsigned int i;
4522
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004523 for (i = 0; i < n; i++) {
4524 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 struct ring_info *tx_skb = tp->tx_skb + entry;
4526 unsigned int len = tx_skb->len;
4527
4528 if (len) {
4529 struct sk_buff *skb = tx_skb->skb;
4530
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004531 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 tp->TxDescArray + entry);
4533 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00004534 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 dev_kfree_skb(skb);
4536 tx_skb->skb = NULL;
4537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 }
4539 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004540}
4541
4542static void rtl8169_tx_clear(struct rtl8169_private *tp)
4543{
4544 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 tp->cur_tx = tp->dirty_tx = 0;
4546}
4547
David Howellsc4028952006-11-22 14:57:56 +00004548static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549{
4550 struct rtl8169_private *tp = netdev_priv(dev);
4551
David Howellsc4028952006-11-22 14:57:56 +00004552 PREPARE_DELAYED_WORK(&tp->task, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 schedule_delayed_work(&tp->task, 4);
4554}
4555
4556static void rtl8169_wait_for_quiescence(struct net_device *dev)
4557{
4558 struct rtl8169_private *tp = netdev_priv(dev);
4559 void __iomem *ioaddr = tp->mmio_addr;
4560
4561 synchronize_irq(dev->irq);
4562
4563 /* Wait for any pending NAPI task to complete */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004564 napi_disable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565
4566 rtl8169_irq_mask_and_ack(ioaddr);
4567
David S. Millerd1d08d12008-01-07 20:53:33 -08004568 tp->intr_mask = 0xffff;
4569 RTL_W16(IntrMask, tp->intr_event);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004570 napi_enable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571}
4572
David Howellsc4028952006-11-22 14:57:56 +00004573static void rtl8169_reinit_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574{
David Howellsc4028952006-11-22 14:57:56 +00004575 struct rtl8169_private *tp =
4576 container_of(work, struct rtl8169_private, task.work);
4577 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 int ret;
4579
Francois Romieueb2a0212007-02-15 23:37:21 +01004580 rtnl_lock();
4581
4582 if (!netif_running(dev))
4583 goto out_unlock;
4584
4585 rtl8169_wait_for_quiescence(dev);
4586 rtl8169_close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587
4588 ret = rtl8169_open(dev);
4589 if (unlikely(ret < 0)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004590 if (net_ratelimit())
4591 netif_err(tp, drv, dev,
4592 "reinit failure (status = %d). Rescheduling\n",
4593 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 rtl8169_schedule_work(dev, rtl8169_reinit_task);
4595 }
Francois Romieueb2a0212007-02-15 23:37:21 +01004596
4597out_unlock:
4598 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599}
4600
David Howellsc4028952006-11-22 14:57:56 +00004601static void rtl8169_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602{
David Howellsc4028952006-11-22 14:57:56 +00004603 struct rtl8169_private *tp =
4604 container_of(work, struct rtl8169_private, task.work);
4605 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606
Francois Romieueb2a0212007-02-15 23:37:21 +01004607 rtnl_lock();
4608
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 if (!netif_running(dev))
Francois Romieueb2a0212007-02-15 23:37:21 +01004610 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611
4612 rtl8169_wait_for_quiescence(dev);
4613
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004614 rtl8169_rx_interrupt(dev, tp, tp->mmio_addr, ~(u32)0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 rtl8169_tx_clear(tp);
4616
4617 if (tp->dirty_rx == tp->cur_rx) {
4618 rtl8169_init_ring_indexes(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01004619 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 netif_wake_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004621 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 } else {
Joe Perchesbf82c182010-02-09 11:49:50 +00004623 if (net_ratelimit())
4624 netif_emerg(tp, intr, dev, "Rx buffers shortage\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 rtl8169_schedule_work(dev, rtl8169_reset_task);
4626 }
Francois Romieueb2a0212007-02-15 23:37:21 +01004627
4628out_unlock:
4629 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630}
4631
4632static void rtl8169_tx_timeout(struct net_device *dev)
4633{
4634 struct rtl8169_private *tp = netdev_priv(dev);
4635
françois romieue6de30d2011-01-03 15:08:37 +00004636 rtl8169_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637
4638 /* Let's wait a bit while any (async) irq lands on */
4639 rtl8169_schedule_work(dev, rtl8169_reset_task);
4640}
4641
4642static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07004643 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644{
4645 struct skb_shared_info *info = skb_shinfo(skb);
4646 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04004647 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004648 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649
4650 entry = tp->cur_tx;
4651 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
4652 skb_frag_t *frag = info->frags + cur_frag;
4653 dma_addr_t mapping;
4654 u32 status, len;
4655 void *addr;
4656
4657 entry = (entry + 1) % NUM_TX_DESC;
4658
4659 txd = tp->TxDescArray + entry;
4660 len = frag->size;
4661 addr = ((void *) page_address(frag->page)) + frag->page_offset;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004662 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004663 if (unlikely(dma_mapping_error(d, mapping))) {
4664 if (net_ratelimit())
4665 netif_err(tp, drv, tp->dev,
4666 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004667 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669
Francois Romieucecb5fd2011-04-01 10:21:07 +02004670 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07004671 status = opts[0] | len |
4672 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673
4674 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07004675 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 txd->addr = cpu_to_le64(mapping);
4677
4678 tp->tx_skb[entry].len = len;
4679 }
4680
4681 if (cur_frag) {
4682 tp->tx_skb[entry].skb = skb;
4683 txd->opts1 |= cpu_to_le32(LastFrag);
4684 }
4685
4686 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004687
4688err_out:
4689 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
4690 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691}
4692
Francois Romieu2b7b4312011-04-18 22:53:24 -07004693static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
4694 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695{
Francois Romieu2b7b4312011-04-18 22:53:24 -07004696 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00004697 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07004698 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699
Francois Romieu2b7b4312011-04-18 22:53:24 -07004700 if (mss) {
4701 opts[0] |= TD_LSO;
4702 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
4703 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07004704 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705
4706 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07004707 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07004709 opts[offset] |= info->checksum.udp;
4710 else
4711 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713}
4714
Stephen Hemminger613573252009-08-31 19:50:58 +00004715static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
4716 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717{
4718 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004719 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 struct TxDesc *txd = tp->TxDescArray + entry;
4721 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004722 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 dma_addr_t mapping;
4724 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07004725 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004726 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02004727
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004729 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004730 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 }
4732
4733 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004734 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004736 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004737 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004738 if (unlikely(dma_mapping_error(d, mapping))) {
4739 if (net_ratelimit())
4740 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004741 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743
4744 tp->tx_skb[entry].len = len;
4745 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746
Francois Romieu2b7b4312011-04-18 22:53:24 -07004747 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
4748 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004749
Francois Romieu2b7b4312011-04-18 22:53:24 -07004750 rtl8169_tso_csum(tp, skb, opts);
4751
4752 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004753 if (frags < 0)
4754 goto err_dma_1;
4755 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07004756 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004757 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07004758 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004759 tp->tx_skb[entry].skb = skb;
4760 }
4761
Francois Romieu2b7b4312011-04-18 22:53:24 -07004762 txd->opts2 = cpu_to_le32(opts[1]);
4763
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 wmb();
4765
Francois Romieucecb5fd2011-04-01 10:21:07 +02004766 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07004767 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 txd->opts1 = cpu_to_le32(status);
4769
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 tp->cur_tx += frags + 1;
4771
David Dillow4c020a92010-03-03 16:33:10 +00004772 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773
Francois Romieucecb5fd2011-04-01 10:21:07 +02004774 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775
4776 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
4777 netif_stop_queue(dev);
4778 smp_rmb();
4779 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
4780 netif_wake_queue(dev);
4781 }
4782
Stephen Hemminger613573252009-08-31 19:50:58 +00004783 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004785err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004786 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004787err_dma_0:
4788 dev_kfree_skb(skb);
4789 dev->stats.tx_dropped++;
4790 return NETDEV_TX_OK;
4791
4792err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004794 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00004795 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796}
4797
4798static void rtl8169_pcierr_interrupt(struct net_device *dev)
4799{
4800 struct rtl8169_private *tp = netdev_priv(dev);
4801 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 u16 pci_status, pci_cmd;
4803
4804 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
4805 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
4806
Joe Perchesbf82c182010-02-09 11:49:50 +00004807 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
4808 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809
4810 /*
4811 * The recovery sequence below admits a very elaborated explanation:
4812 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01004813 * - I did not see what else could be done;
4814 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 *
4816 * Feel free to adjust to your needs.
4817 */
Francois Romieua27993f2006-12-18 00:04:19 +01004818 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01004819 pci_cmd &= ~PCI_COMMAND_PARITY;
4820 else
4821 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
4822
4823 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824
4825 pci_write_config_word(pdev, PCI_STATUS,
4826 pci_status & (PCI_STATUS_DETECTED_PARITY |
4827 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
4828 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
4829
4830 /* The infamous DAC f*ckup only happens at boot time */
4831 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00004832 void __iomem *ioaddr = tp->mmio_addr;
4833
Joe Perchesbf82c182010-02-09 11:49:50 +00004834 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 tp->cp_cmd &= ~PCIDAC;
4836 RTL_W16(CPlusCmd, tp->cp_cmd);
4837 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 }
4839
françois romieue6de30d2011-01-03 15:08:37 +00004840 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01004841
4842 rtl8169_schedule_work(dev, rtl8169_reinit_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843}
4844
Francois Romieu07d3f512007-02-21 22:40:46 +01004845static void rtl8169_tx_interrupt(struct net_device *dev,
4846 struct rtl8169_private *tp,
4847 void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848{
4849 unsigned int dirty_tx, tx_left;
4850
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 dirty_tx = tp->dirty_tx;
4852 smp_rmb();
4853 tx_left = tp->cur_tx - dirty_tx;
4854
4855 while (tx_left > 0) {
4856 unsigned int entry = dirty_tx % NUM_TX_DESC;
4857 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 u32 status;
4859
4860 rmb();
4861 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
4862 if (status & DescOwn)
4863 break;
4864
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004865 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
4866 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 if (status & LastFrag) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00004868 dev->stats.tx_packets++;
4869 dev->stats.tx_bytes += tx_skb->skb->len;
Eric Dumazet87433bf2009-06-09 22:55:53 +00004870 dev_kfree_skb(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 tx_skb->skb = NULL;
4872 }
4873 dirty_tx++;
4874 tx_left--;
4875 }
4876
4877 if (tp->dirty_tx != dirty_tx) {
4878 tp->dirty_tx = dirty_tx;
4879 smp_wmb();
4880 if (netif_queue_stopped(dev) &&
4881 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
4882 netif_wake_queue(dev);
4883 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02004884 /*
4885 * 8168 hack: TxPoll requests are lost when the Tx packets are
4886 * too close. Let's kick an extra TxPoll request when a burst
4887 * of start_xmit activity is detected (if it is not detected,
4888 * it is slow enough). -- FR
4889 */
4890 smp_rmb();
4891 if (tp->cur_tx != dirty_tx)
4892 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 }
4894}
4895
Francois Romieu126fa4b2005-05-12 20:09:17 -04004896static inline int rtl8169_fragmented_frame(u32 status)
4897{
4898 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
4899}
4900
Eric Dumazetadea1ac72010-09-05 20:04:05 -07004901static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 u32 status = opts1 & RxProtoMask;
4904
4905 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00004906 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 skb->ip_summed = CHECKSUM_UNNECESSARY;
4908 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07004909 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910}
4911
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004912static struct sk_buff *rtl8169_try_rx_copy(void *data,
4913 struct rtl8169_private *tp,
4914 int pkt_size,
4915 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02004917 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004918 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004920 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004921 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004922 prefetch(data);
4923 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
4924 if (skb)
4925 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004926 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
4927
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004928 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929}
4930
Eric Dumazet630b9432010-03-31 02:08:31 +00004931/*
4932 * Warning : rtl8169_rx_interrupt() might be called :
4933 * 1) from NAPI (softirq) context
4934 * (polling = 1 : we should call netif_receive_skb())
4935 * 2) from process context (rtl8169_reset_task())
4936 * (polling = 0 : we must call netif_rx() instead)
4937 */
Francois Romieu07d3f512007-02-21 22:40:46 +01004938static int rtl8169_rx_interrupt(struct net_device *dev,
4939 struct rtl8169_private *tp,
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004940 void __iomem *ioaddr, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941{
4942 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004943 unsigned int count;
Eric Dumazet630b9432010-03-31 02:08:31 +00004944 int polling = (budget != ~(u32)0) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 cur_rx = tp->cur_rx;
4947 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02004948 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949
Richard Dawe4dcb7d32005-05-27 21:12:00 +02004950 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04004952 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 u32 status;
4954
4955 rmb();
Francois Romieu126fa4b2005-05-12 20:09:17 -04004956 status = le32_to_cpu(desc->opts1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957
4958 if (status & DescOwn)
4959 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02004960 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004961 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
4962 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004963 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02004965 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02004967 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02004968 if (status & RxFOVF) {
4969 rtl8169_schedule_work(dev, rtl8169_reset_task);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004970 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02004971 }
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004972 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004974 struct sk_buff *skb;
Stephen Hemmingerb4496552007-06-17 01:06:49 +02004975 dma_addr_t addr = le64_to_cpu(desc->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 int pkt_size = (status & 0x00001FFF) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977
Francois Romieu126fa4b2005-05-12 20:09:17 -04004978 /*
4979 * The driver does not support incoming fragmented
4980 * frames. They are seen as a symptom of over-mtu
4981 * sized frames.
4982 */
4983 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02004984 dev->stats.rx_dropped++;
4985 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004986 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02004987 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04004988 }
4989
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004990 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
4991 tp, pkt_size, addr);
4992 rtl8169_mark_to_asic(desc, rx_buf_sz);
4993 if (!skb) {
4994 dev->stats.rx_dropped++;
4995 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996 }
4997
Eric Dumazetadea1ac72010-09-05 20:04:05 -07004998 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 skb_put(skb, pkt_size);
5000 skb->protocol = eth_type_trans(skb, dev);
5001
Francois Romieu7a8fc772011-03-01 17:18:33 +01005002 rtl8169_rx_vlan_tag(desc, skb);
5003
5004 if (likely(polling))
5005 napi_gro_receive(&tp->napi, skb);
5006 else
5007 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008
Francois Romieucebf8cc2007-10-18 12:06:54 +02005009 dev->stats.rx_bytes += pkt_size;
5010 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 }
Francois Romieu6dccd162007-02-13 23:38:05 +01005012
5013 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00005014 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01005015 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5016 desc->opts2 = 0;
5017 cur_rx++;
5018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 }
5020
5021 count = cur_rx - tp->cur_rx;
5022 tp->cur_rx = cur_rx;
5023
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005024 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025
5026 return count;
5027}
5028
Francois Romieu07d3f512007-02-21 22:40:46 +01005029static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030{
Francois Romieu07d3f512007-02-21 22:40:46 +01005031 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 int handled = 0;
Francois Romieu865c6522008-05-11 14:51:00 +02005035 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036
David Dillowf11a3772009-05-22 15:29:34 +00005037 /* loop handling interrupts until we have no new ones or
5038 * we hit a invalid/hotplug case.
5039 */
Francois Romieu865c6522008-05-11 14:51:00 +02005040 status = RTL_R16(IntrStatus);
David Dillowf11a3772009-05-22 15:29:34 +00005041 while (status && status != 0xffff) {
5042 handled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043
David Dillowf11a3772009-05-22 15:29:34 +00005044 /* Handle all of the error cases first. These will reset
5045 * the chip, so just exit the loop.
5046 */
5047 if (unlikely(!netif_running(dev))) {
5048 rtl8169_asic_down(ioaddr);
5049 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 }
David Dillowf11a3772009-05-22 15:29:34 +00005051
Francois Romieu1519e572011-02-03 12:02:36 +01005052 if (unlikely(status & RxFIFOOver)) {
5053 switch (tp->mac_version) {
5054 /* Work around for rx fifo overflow */
5055 case RTL_GIGA_MAC_VER_11:
5056 case RTL_GIGA_MAC_VER_22:
5057 case RTL_GIGA_MAC_VER_26:
5058 netif_stop_queue(dev);
5059 rtl8169_tx_timeout(dev);
5060 goto done;
Francois Romieuf60ac8e2011-02-03 17:27:52 +01005061 /* Testers needed. */
5062 case RTL_GIGA_MAC_VER_17:
5063 case RTL_GIGA_MAC_VER_19:
5064 case RTL_GIGA_MAC_VER_20:
5065 case RTL_GIGA_MAC_VER_21:
5066 case RTL_GIGA_MAC_VER_23:
5067 case RTL_GIGA_MAC_VER_24:
5068 case RTL_GIGA_MAC_VER_27:
5069 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00005070 case RTL_GIGA_MAC_VER_31:
Francois Romieu1519e572011-02-03 12:02:36 +01005071 /* Experimental science. Pktgen proof. */
5072 case RTL_GIGA_MAC_VER_12:
5073 case RTL_GIGA_MAC_VER_25:
5074 if (status == RxFIFOOver)
5075 goto done;
5076 break;
5077 default:
5078 break;
5079 }
David Dillowf11a3772009-05-22 15:29:34 +00005080 }
5081
5082 if (unlikely(status & SYSErr)) {
5083 rtl8169_pcierr_interrupt(dev);
5084 break;
5085 }
5086
5087 if (status & LinkChg)
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005088 __rtl8169_check_link_status(dev, tp, ioaddr, true);
David Dillowf11a3772009-05-22 15:29:34 +00005089
5090 /* We need to see the lastest version of tp->intr_mask to
5091 * avoid ignoring an MSI interrupt and having to wait for
5092 * another event which may never come.
5093 */
5094 smp_rmb();
5095 if (status & tp->intr_mask & tp->napi_event) {
5096 RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
5097 tp->intr_mask = ~tp->napi_event;
5098
5099 if (likely(napi_schedule_prep(&tp->napi)))
5100 __napi_schedule(&tp->napi);
Joe Perchesbf82c182010-02-09 11:49:50 +00005101 else
5102 netif_info(tp, intr, dev,
5103 "interrupt %04x in poll\n", status);
David Dillowf11a3772009-05-22 15:29:34 +00005104 }
5105
5106 /* We only get a new MSI interrupt when all active irq
5107 * sources on the chip have been acknowledged. So, ack
5108 * everything we've seen and check if new sources have become
5109 * active to avoid blocking all interrupts from the chip.
5110 */
5111 RTL_W16(IntrStatus,
5112 (status & RxFIFOOver) ? (status | RxOverflow) : status);
5113 status = RTL_R16(IntrStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 }
Francois Romieu1519e572011-02-03 12:02:36 +01005115done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 return IRQ_RETVAL(handled);
5117}
5118
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005119static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005121 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5122 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 void __iomem *ioaddr = tp->mmio_addr;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005124 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005126 work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 rtl8169_tx_interrupt(dev, tp, ioaddr);
5128
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005129 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005130 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00005131
5132 /* We need for force the visibility of tp->intr_mask
5133 * for other CPUs, as we can loose an MSI interrupt
5134 * and potentially wait for a retransmit timeout if we don't.
5135 * The posted write to IntrMask is safe, as it will
5136 * eventually make it to the chip and we won't loose anything
5137 * until it does.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 */
David Dillowf11a3772009-05-22 15:29:34 +00005139 tp->intr_mask = 0xffff;
David Dillow4c020a92010-03-03 16:33:10 +00005140 wmb();
Francois Romieu0e485152007-02-20 00:00:26 +01005141 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 }
5143
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005144 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146
Francois Romieu523a6092008-09-10 22:28:56 +02005147static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5148{
5149 struct rtl8169_private *tp = netdev_priv(dev);
5150
5151 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5152 return;
5153
5154 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5155 RTL_W32(RxMissed, 0);
5156}
5157
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158static void rtl8169_down(struct net_device *dev)
5159{
5160 struct rtl8169_private *tp = netdev_priv(dev);
5161 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162
5163 rtl8169_delete_timer(dev);
5164
5165 netif_stop_queue(dev);
5166
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005167 napi_disable(&tp->napi);
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005168
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169 spin_lock_irq(&tp->lock);
5170
5171 rtl8169_asic_down(ioaddr);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005172 /*
5173 * At this point device interrupts can not be enabled in any function,
5174 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task,
5175 * rtl8169_reinit_task) and napi is disabled (rtl8169_poll).
5176 */
Francois Romieu523a6092008-09-10 22:28:56 +02005177 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178
5179 spin_unlock_irq(&tp->lock);
5180
5181 synchronize_irq(dev->irq);
5182
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 /* Give a racing hard_start_xmit a few cycles to complete. */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07005184 synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 rtl8169_tx_clear(tp);
5187
5188 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00005189
5190 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191}
5192
5193static int rtl8169_close(struct net_device *dev)
5194{
5195 struct rtl8169_private *tp = netdev_priv(dev);
5196 struct pci_dev *pdev = tp->pci_dev;
5197
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005198 pm_runtime_get_sync(&pdev->dev);
5199
Francois Romieucecb5fd2011-04-01 10:21:07 +02005200 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08005201 rtl8169_update_counters(dev);
5202
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 rtl8169_down(dev);
5204
5205 free_irq(dev->irq, dev);
5206
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00005207 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5208 tp->RxPhyAddr);
5209 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5210 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 tp->TxDescArray = NULL;
5212 tp->RxDescArray = NULL;
5213
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005214 pm_runtime_put_sync(&pdev->dev);
5215
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 return 0;
5217}
5218
Francois Romieu07ce4062007-02-23 23:36:39 +01005219static void rtl_set_rx_mode(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220{
5221 struct rtl8169_private *tp = netdev_priv(dev);
5222 void __iomem *ioaddr = tp->mmio_addr;
5223 unsigned long flags;
5224 u32 mc_filter[2]; /* Multicast hash filter */
Francois Romieu07d3f512007-02-21 22:40:46 +01005225 int rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 u32 tmp = 0;
5227
5228 if (dev->flags & IFF_PROMISC) {
5229 /* Unconditionally log net taps. */
Joe Perchesbf82c182010-02-09 11:49:50 +00005230 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 rx_mode =
5232 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5233 AcceptAllPhys;
5234 mc_filter[1] = mc_filter[0] = 0xffffffff;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00005235 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
Joe Perches8e95a202009-12-03 07:58:21 +00005236 (dev->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 /* Too many to filter perfectly -- accept all multicasts. */
5238 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5239 mc_filter[1] = mc_filter[0] = 0xffffffff;
5240 } else {
Jiri Pirko22bedad2010-04-01 21:22:57 +00005241 struct netdev_hw_addr *ha;
Francois Romieu07d3f512007-02-21 22:40:46 +01005242
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243 rx_mode = AcceptBroadcast | AcceptMyPhys;
5244 mc_filter[1] = mc_filter[0] = 0;
Jiri Pirko22bedad2010-04-01 21:22:57 +00005245 netdev_for_each_mc_addr(ha, dev) {
5246 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5248 rx_mode |= AcceptMulticast;
5249 }
5250 }
5251
5252 spin_lock_irqsave(&tp->lock, flags);
5253
5254 tmp = rtl8169_rx_config | rx_mode |
Francois Romieu2b7b4312011-04-18 22:53:24 -07005255 (RTL_R32(RxConfig) & RTL_RX_CONFIG_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256
Francois Romieuf887cce2008-07-17 22:24:18 +02005257 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
Francois Romieu1087f4f2007-12-26 22:46:05 +01005258 u32 data = mc_filter[0];
5259
5260 mc_filter[0] = swab32(mc_filter[1]);
5261 mc_filter[1] = swab32(data);
Francois Romieubcf0bf92006-07-26 23:14:13 +02005262 }
5263
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264 RTL_W32(MAR0 + 4, mc_filter[1]);
Francois Romieu78f1cd02010-03-27 19:35:46 -07005265 RTL_W32(MAR0 + 0, mc_filter[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266
Francois Romieu57a9f232007-06-04 22:10:15 +02005267 RTL_W32(RxConfig, tmp);
5268
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269 spin_unlock_irqrestore(&tp->lock, flags);
5270}
5271
5272/**
5273 * rtl8169_get_stats - Get rtl8169 read/write statistics
5274 * @dev: The Ethernet Device to get statistics for
5275 *
5276 * Get TX/RX statistics for rtl8169
5277 */
5278static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
5279{
5280 struct rtl8169_private *tp = netdev_priv(dev);
5281 void __iomem *ioaddr = tp->mmio_addr;
5282 unsigned long flags;
5283
5284 if (netif_running(dev)) {
5285 spin_lock_irqsave(&tp->lock, flags);
Francois Romieu523a6092008-09-10 22:28:56 +02005286 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 spin_unlock_irqrestore(&tp->lock, flags);
5288 }
Francois Romieu5b0384f2006-08-16 16:00:01 +02005289
Francois Romieucebf8cc2007-10-18 12:06:54 +02005290 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291}
5292
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005293static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01005294{
françois romieu065c27c2011-01-03 15:08:12 +00005295 struct rtl8169_private *tp = netdev_priv(dev);
5296
Francois Romieu5d06a992006-02-23 00:47:58 +01005297 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005298 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01005299
françois romieu065c27c2011-01-03 15:08:12 +00005300 rtl_pll_power_down(tp);
5301
Francois Romieu5d06a992006-02-23 00:47:58 +01005302 netif_device_detach(dev);
5303 netif_stop_queue(dev);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005304}
Francois Romieu5d06a992006-02-23 00:47:58 +01005305
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005306#ifdef CONFIG_PM
5307
5308static int rtl8169_suspend(struct device *device)
5309{
5310 struct pci_dev *pdev = to_pci_dev(device);
5311 struct net_device *dev = pci_get_drvdata(pdev);
5312
5313 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02005314
Francois Romieu5d06a992006-02-23 00:47:58 +01005315 return 0;
5316}
5317
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005318static void __rtl8169_resume(struct net_device *dev)
5319{
françois romieu065c27c2011-01-03 15:08:12 +00005320 struct rtl8169_private *tp = netdev_priv(dev);
5321
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005322 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00005323
5324 rtl_pll_power_up(tp);
5325
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005326 rtl8169_schedule_work(dev, rtl8169_reset_task);
5327}
5328
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005329static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01005330{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005331 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01005332 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005333 struct rtl8169_private *tp = netdev_priv(dev);
5334
5335 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01005336
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005337 if (netif_running(dev))
5338 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01005339
Francois Romieu5d06a992006-02-23 00:47:58 +01005340 return 0;
5341}
5342
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005343static int rtl8169_runtime_suspend(struct device *device)
5344{
5345 struct pci_dev *pdev = to_pci_dev(device);
5346 struct net_device *dev = pci_get_drvdata(pdev);
5347 struct rtl8169_private *tp = netdev_priv(dev);
5348
5349 if (!tp->TxDescArray)
5350 return 0;
5351
5352 spin_lock_irq(&tp->lock);
5353 tp->saved_wolopts = __rtl8169_get_wol(tp);
5354 __rtl8169_set_wol(tp, WAKE_ANY);
5355 spin_unlock_irq(&tp->lock);
5356
5357 rtl8169_net_suspend(dev);
5358
5359 return 0;
5360}
5361
5362static int rtl8169_runtime_resume(struct device *device)
5363{
5364 struct pci_dev *pdev = to_pci_dev(device);
5365 struct net_device *dev = pci_get_drvdata(pdev);
5366 struct rtl8169_private *tp = netdev_priv(dev);
5367
5368 if (!tp->TxDescArray)
5369 return 0;
5370
5371 spin_lock_irq(&tp->lock);
5372 __rtl8169_set_wol(tp, tp->saved_wolopts);
5373 tp->saved_wolopts = 0;
5374 spin_unlock_irq(&tp->lock);
5375
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005376 rtl8169_init_phy(dev, tp);
5377
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005378 __rtl8169_resume(dev);
5379
5380 return 0;
5381}
5382
5383static int rtl8169_runtime_idle(struct device *device)
5384{
5385 struct pci_dev *pdev = to_pci_dev(device);
5386 struct net_device *dev = pci_get_drvdata(pdev);
5387 struct rtl8169_private *tp = netdev_priv(dev);
5388
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005389 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005390}
5391
Alexey Dobriyan47145212009-12-14 18:00:08 -08005392static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02005393 .suspend = rtl8169_suspend,
5394 .resume = rtl8169_resume,
5395 .freeze = rtl8169_suspend,
5396 .thaw = rtl8169_resume,
5397 .poweroff = rtl8169_suspend,
5398 .restore = rtl8169_resume,
5399 .runtime_suspend = rtl8169_runtime_suspend,
5400 .runtime_resume = rtl8169_runtime_resume,
5401 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005402};
5403
5404#define RTL8169_PM_OPS (&rtl8169_pm_ops)
5405
5406#else /* !CONFIG_PM */
5407
5408#define RTL8169_PM_OPS NULL
5409
5410#endif /* !CONFIG_PM */
5411
Francois Romieu1765f952008-09-13 17:21:40 +02005412static void rtl_shutdown(struct pci_dev *pdev)
5413{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005414 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00005415 struct rtl8169_private *tp = netdev_priv(dev);
5416 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu1765f952008-09-13 17:21:40 +02005417
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005418 rtl8169_net_suspend(dev);
5419
Francois Romieucecb5fd2011-04-01 10:21:07 +02005420 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08005421 rtl_rar_set(tp, dev->perm_addr);
5422
françois romieu4bb3f522009-06-17 11:41:45 +00005423 spin_lock_irq(&tp->lock);
5424
5425 rtl8169_asic_down(ioaddr);
5426
5427 spin_unlock_irq(&tp->lock);
5428
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005429 if (system_state == SYSTEM_POWER_OFF) {
françois romieuca52efd2009-07-24 12:34:19 +00005430 /* WoL fails with some 8168 when the receiver is disabled. */
5431 if (tp->features & RTL_FEATURE_WOL) {
5432 pci_clear_master(pdev);
5433
5434 RTL_W8(ChipCmd, CmdRxEnb);
5435 /* PCI commit */
5436 RTL_R8(ChipCmd);
5437 }
5438
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005439 pci_wake_from_d3(pdev, true);
5440 pci_set_power_state(pdev, PCI_D3hot);
5441 }
5442}
Francois Romieu5d06a992006-02-23 00:47:58 +01005443
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444static struct pci_driver rtl8169_pci_driver = {
5445 .name = MODULENAME,
5446 .id_table = rtl8169_pci_tbl,
5447 .probe = rtl8169_init_one,
5448 .remove = __devexit_p(rtl8169_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02005449 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005450 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451};
5452
Francois Romieu07d3f512007-02-21 22:40:46 +01005453static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454{
Jeff Garzik29917622006-08-19 17:48:59 -04005455 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456}
5457
Francois Romieu07d3f512007-02-21 22:40:46 +01005458static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459{
5460 pci_unregister_driver(&rtl8169_pci_driver);
5461}
5462
5463module_init(rtl8169_init_module);
5464module_exit(rtl8169_cleanup_module);