blob: 81906bc919a4a83bff3c94707335bdcaa169929e [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;
françois romieuc0e45c12011-01-03 15:08:04 +0000619
620 struct mdio_ops {
621 void (*write)(void __iomem *, int, int);
622 int (*read)(void __iomem *, int);
623 } mdio_ops;
624
françois romieu065c27c2011-01-03 15:08:12 +0000625 struct pll_power_ops {
626 void (*down)(struct rtl8169_private *);
627 void (*up)(struct rtl8169_private *);
628 } pll_power_ops;
629
Oliver Neukum54405cd2011-01-06 21:55:13 +0100630 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Francois Romieuccdffb92008-07-26 14:26:06 +0200631 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000632 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100633 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000634 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800636 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
Francois Romieu9c14cea2008-07-05 00:21:15 +0200637 int pcie_cap;
David Howellsc4028952006-11-22 14:57:56 +0000638 struct delayed_work task;
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200639 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200640
641 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800642 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000643 u32 saved_wolopts;
françois romieuf1e02ed2011-01-13 13:07:53 +0000644
645 const struct firmware *fw;
François Romieu953a12c2011-04-24 17:38:48 +0200646#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647};
648
Ralf Baechle979b6c12005-06-13 14:30:40 -0700649MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700652MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200653module_param_named(debug, debug.msg_enable, int, 0);
654MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655MODULE_LICENSE("GPL");
656MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000657MODULE_FIRMWARE(FIRMWARE_8168D_1);
658MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000659MODULE_FIRMWARE(FIRMWARE_8168E_1);
660MODULE_FIRMWARE(FIRMWARE_8168E_2);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800661MODULE_FIRMWARE(FIRMWARE_8105E_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663static int rtl8169_open(struct net_device *dev);
Stephen Hemminger613573252009-08-31 19:50:58 +0000664static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
665 struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100666static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667static int rtl8169_init_ring(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100668static void rtl_hw_start(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669static int rtl8169_close(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100670static void rtl_set_rx_mode(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671static void rtl8169_tx_timeout(struct net_device *dev);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200672static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673static int rtl8169_rx_interrupt(struct net_device *, struct rtl8169_private *,
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700674 void __iomem *, u32 budget);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200675static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676static void rtl8169_down(struct net_device *dev);
Francois Romieu99f252b2007-04-02 22:59:59 +0200677static void rtl8169_rx_clear(struct rtl8169_private *tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700678static int rtl8169_poll(struct napi_struct *napi, int budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680static const unsigned int rtl8169_rx_config =
Francois Romieu5b0384f2006-08-16 16:00:01 +0200681 (RX_FIFO_THRESH << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
françois romieub646d902011-01-03 15:08:21 +0000683static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
684{
685 void __iomem *ioaddr = tp->mmio_addr;
686 int i;
687
688 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
689 for (i = 0; i < 20; i++) {
690 udelay(100);
691 if (RTL_R32(OCPAR) & OCPAR_FLAG)
692 break;
693 }
694 return RTL_R32(OCPDR);
695}
696
697static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
698{
699 void __iomem *ioaddr = tp->mmio_addr;
700 int i;
701
702 RTL_W32(OCPDR, data);
703 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
704 for (i = 0; i < 20; i++) {
705 udelay(100);
706 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
707 break;
708 }
709}
710
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800711static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000712{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800713 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000714 int i;
715
716 RTL_W8(ERIDR, cmd);
717 RTL_W32(ERIAR, 0x800010e8);
718 msleep(2);
719 for (i = 0; i < 5; i++) {
720 udelay(100);
721 if (!(RTL_R32(ERIDR) & ERIAR_FLAG))
722 break;
723 }
724
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800725 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000726}
727
728#define OOB_CMD_RESET 0x00
729#define OOB_CMD_DRIVER_START 0x05
730#define OOB_CMD_DRIVER_STOP 0x06
731
Francois Romieucecb5fd2011-04-01 10:21:07 +0200732static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
733{
734 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
735}
736
françois romieub646d902011-01-03 15:08:21 +0000737static void rtl8168_driver_start(struct rtl8169_private *tp)
738{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200739 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000740 int i;
741
742 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
743
Francois Romieucecb5fd2011-04-01 10:21:07 +0200744 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000745
françois romieub646d902011-01-03 15:08:21 +0000746 for (i = 0; i < 10; i++) {
747 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000748 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
françois romieub646d902011-01-03 15:08:21 +0000749 break;
750 }
751}
752
753static void rtl8168_driver_stop(struct rtl8169_private *tp)
754{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200755 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000756 int i;
757
758 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
759
Francois Romieucecb5fd2011-04-01 10:21:07 +0200760 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000761
françois romieub646d902011-01-03 15:08:21 +0000762 for (i = 0; i < 10; i++) {
763 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000764 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
françois romieub646d902011-01-03 15:08:21 +0000765 break;
766 }
767}
768
hayeswang4804b3b2011-03-21 01:50:29 +0000769static int r8168dp_check_dash(struct rtl8169_private *tp)
770{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200771 u16 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000772
Francois Romieucecb5fd2011-04-01 10:21:07 +0200773 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
hayeswang4804b3b2011-03-21 01:50:29 +0000774}
françois romieub646d902011-01-03 15:08:21 +0000775
françois romieu4da19632011-01-03 15:07:55 +0000776static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
778 int i;
779
Francois Romieua6baf3a2007-11-08 23:23:21 +0100780 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Francois Romieu23714082006-01-29 00:49:09 +0100782 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100783 /*
784 * Check if the RTL8169 has completed writing to the specified
785 * MII register.
786 */
Francois Romieu5b0384f2006-08-16 16:00:01 +0200787 if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 break;
Francois Romieu23714082006-01-29 00:49:09 +0100789 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
Timo Teräs024a07b2010-06-06 15:38:47 -0700791 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700792 * According to hardware specs a 20us delay is required after write
793 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700794 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700795 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
797
françois romieu4da19632011-01-03 15:07:55 +0000798static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
800 int i, value = -1;
801
Francois Romieua6baf3a2007-11-08 23:23:21 +0100802 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Francois Romieu23714082006-01-29 00:49:09 +0100804 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100805 /*
806 * Check if the RTL8169 has completed retrieving data from
807 * the specified MII register.
808 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (RTL_R32(PHYAR) & 0x80000000) {
Francois Romieua6baf3a2007-11-08 23:23:21 +0100810 value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 break;
812 }
Francois Romieu23714082006-01-29 00:49:09 +0100813 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
Timo Teräs81a95f02010-06-09 17:31:48 -0700815 /*
816 * According to hardware specs a 20us delay is required after read
817 * complete indication, but before sending next command.
818 */
819 udelay(20);
820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return value;
822}
823
françois romieuc0e45c12011-01-03 15:08:04 +0000824static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
825{
826 int i;
827
828 RTL_W32(OCPDR, data |
829 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
830 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
831 RTL_W32(EPHY_RXER_NUM, 0);
832
833 for (i = 0; i < 100; i++) {
834 mdelay(1);
835 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
836 break;
837 }
838}
839
840static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
841{
842 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
843 (value & OCPDR_DATA_MASK));
844}
845
846static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
847{
848 int i;
849
850 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
851
852 mdelay(1);
853 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
854 RTL_W32(EPHY_RXER_NUM, 0);
855
856 for (i = 0; i < 100; i++) {
857 mdelay(1);
858 if (RTL_R32(OCPAR) & OCPAR_FLAG)
859 break;
860 }
861
862 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
863}
864
françois romieue6de30d2011-01-03 15:08:37 +0000865#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
866
867static void r8168dp_2_mdio_start(void __iomem *ioaddr)
868{
869 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
870}
871
872static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
873{
874 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
875}
876
877static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
878{
879 r8168dp_2_mdio_start(ioaddr);
880
881 r8169_mdio_write(ioaddr, reg_addr, value);
882
883 r8168dp_2_mdio_stop(ioaddr);
884}
885
886static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
887{
888 int value;
889
890 r8168dp_2_mdio_start(ioaddr);
891
892 value = r8169_mdio_read(ioaddr, reg_addr);
893
894 r8168dp_2_mdio_stop(ioaddr);
895
896 return value;
897}
898
françois romieu4da19632011-01-03 15:07:55 +0000899static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +0200900{
françois romieuc0e45c12011-01-03 15:08:04 +0000901 tp->mdio_ops.write(tp->mmio_addr, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +0200902}
903
françois romieu4da19632011-01-03 15:07:55 +0000904static int rtl_readphy(struct rtl8169_private *tp, int location)
905{
françois romieuc0e45c12011-01-03 15:08:04 +0000906 return tp->mdio_ops.read(tp->mmio_addr, location);
françois romieu4da19632011-01-03 15:07:55 +0000907}
908
909static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
910{
911 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
912}
913
914static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +0000915{
916 int val;
917
françois romieu4da19632011-01-03 15:07:55 +0000918 val = rtl_readphy(tp, reg_addr);
919 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +0000920}
921
Francois Romieuccdffb92008-07-26 14:26:06 +0200922static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
923 int val)
924{
925 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +0200926
françois romieu4da19632011-01-03 15:07:55 +0000927 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +0200928}
929
930static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
931{
932 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +0200933
françois romieu4da19632011-01-03 15:07:55 +0000934 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +0200935}
936
Francois Romieudacf8152008-08-02 20:44:13 +0200937static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
938{
939 unsigned int i;
940
941 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
942 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
943
944 for (i = 0; i < 100; i++) {
945 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
946 break;
947 udelay(10);
948 }
949}
950
951static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
952{
953 u16 value = 0xffff;
954 unsigned int i;
955
956 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
957
958 for (i = 0; i < 100; i++) {
959 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
960 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
961 break;
962 }
963 udelay(10);
964 }
965
966 return value;
967}
968
969static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
970{
971 unsigned int i;
972
973 RTL_W32(CSIDR, value);
974 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
975 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
976
977 for (i = 0; i < 100; i++) {
978 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
979 break;
980 udelay(10);
981 }
982}
983
984static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
985{
986 u32 value = ~0x00;
987 unsigned int i;
988
989 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
990 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
991
992 for (i = 0; i < 100; i++) {
993 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
994 value = RTL_R32(CSIDR);
995 break;
996 }
997 udelay(10);
998 }
999
1000 return value;
1001}
1002
françois romieudaf9df62009-10-07 12:44:20 +00001003static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1004{
1005 u8 value = 0xff;
1006 unsigned int i;
1007
1008 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1009
1010 for (i = 0; i < 300; i++) {
1011 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1012 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1013 break;
1014 }
1015 udelay(100);
1016 }
1017
1018 return value;
1019}
1020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021static void rtl8169_irq_mask_and_ack(void __iomem *ioaddr)
1022{
1023 RTL_W16(IntrMask, 0x0000);
1024
1025 RTL_W16(IntrStatus, 0xffff);
1026}
1027
1028static void rtl8169_asic_down(void __iomem *ioaddr)
1029{
1030 RTL_W8(ChipCmd, 0x00);
1031 rtl8169_irq_mask_and_ack(ioaddr);
1032 RTL_R16(CPlusCmd);
1033}
1034
françois romieu4da19632011-01-03 15:07:55 +00001035static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
françois romieu4da19632011-01-03 15:07:55 +00001037 void __iomem *ioaddr = tp->mmio_addr;
1038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 return RTL_R32(TBICSR) & TBIReset;
1040}
1041
françois romieu4da19632011-01-03 15:07:55 +00001042static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
françois romieu4da19632011-01-03 15:07:55 +00001044 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}
1046
1047static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1048{
1049 return RTL_R32(TBICSR) & TBILinkOk;
1050}
1051
1052static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1053{
1054 return RTL_R8(PHYstatus) & LinkStatus;
1055}
1056
françois romieu4da19632011-01-03 15:07:55 +00001057static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
françois romieu4da19632011-01-03 15:07:55 +00001059 void __iomem *ioaddr = tp->mmio_addr;
1060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1062}
1063
françois romieu4da19632011-01-03 15:07:55 +00001064static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
1066 unsigned int val;
1067
françois romieu4da19632011-01-03 15:07:55 +00001068 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1069 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070}
1071
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001072static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001073 struct rtl8169_private *tp,
1074 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
1076 unsigned long flags;
1077
1078 spin_lock_irqsave(&tp->lock, flags);
1079 if (tp->link_ok(ioaddr)) {
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001080 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001081 if (pm)
1082 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001084 if (net_ratelimit())
1085 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001086 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001088 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001089 if (pm)
1090 pm_schedule_suspend(&tp->pci_dev->dev, 100);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 spin_unlock_irqrestore(&tp->lock, flags);
1093}
1094
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001095static void rtl8169_check_link_status(struct net_device *dev,
1096 struct rtl8169_private *tp,
1097 void __iomem *ioaddr)
1098{
1099 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1100}
1101
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001102#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1103
1104static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1105{
1106 void __iomem *ioaddr = tp->mmio_addr;
1107 u8 options;
1108 u32 wolopts = 0;
1109
1110 options = RTL_R8(Config1);
1111 if (!(options & PMEnable))
1112 return 0;
1113
1114 options = RTL_R8(Config3);
1115 if (options & LinkUp)
1116 wolopts |= WAKE_PHY;
1117 if (options & MagicPacket)
1118 wolopts |= WAKE_MAGIC;
1119
1120 options = RTL_R8(Config5);
1121 if (options & UWF)
1122 wolopts |= WAKE_UCAST;
1123 if (options & BWF)
1124 wolopts |= WAKE_BCAST;
1125 if (options & MWF)
1126 wolopts |= WAKE_MCAST;
1127
1128 return wolopts;
1129}
1130
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001131static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1132{
1133 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001134
1135 spin_lock_irq(&tp->lock);
1136
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001137 wol->supported = WAKE_ANY;
1138 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001139
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001140 spin_unlock_irq(&tp->lock);
1141}
1142
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001143static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001144{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001145 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001146 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001147 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001148 u32 opt;
1149 u16 reg;
1150 u8 mask;
1151 } cfg[] = {
1152 { WAKE_ANY, Config1, PMEnable },
1153 { WAKE_PHY, Config3, LinkUp },
1154 { WAKE_MAGIC, Config3, MagicPacket },
1155 { WAKE_UCAST, Config5, UWF },
1156 { WAKE_BCAST, Config5, BWF },
1157 { WAKE_MCAST, Config5, MWF },
1158 { WAKE_ANY, Config5, LanWake }
1159 };
1160
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001161 RTL_W8(Cfg9346, Cfg9346_Unlock);
1162
1163 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1164 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001165 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001166 options |= cfg[i].mask;
1167 RTL_W8(cfg[i].reg, options);
1168 }
1169
1170 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001171}
1172
1173static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1174{
1175 struct rtl8169_private *tp = netdev_priv(dev);
1176
1177 spin_lock_irq(&tp->lock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001178
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001179 if (wol->wolopts)
1180 tp->features |= RTL_FEATURE_WOL;
1181 else
1182 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001183 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001184 spin_unlock_irq(&tp->lock);
1185
françois romieuea809072010-11-08 13:23:58 +00001186 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1187
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001188 return 0;
1189}
1190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191static void rtl8169_get_drvinfo(struct net_device *dev,
1192 struct ethtool_drvinfo *info)
1193{
1194 struct rtl8169_private *tp = netdev_priv(dev);
1195
1196 strcpy(info->driver, MODULENAME);
1197 strcpy(info->version, RTL8169_VERSION);
1198 strcpy(info->bus_info, pci_name(tp->pci_dev));
1199}
1200
1201static int rtl8169_get_regs_len(struct net_device *dev)
1202{
1203 return R8169_REGS_SIZE;
1204}
1205
1206static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001207 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
1209 struct rtl8169_private *tp = netdev_priv(dev);
1210 void __iomem *ioaddr = tp->mmio_addr;
1211 int ret = 0;
1212 u32 reg;
1213
1214 reg = RTL_R32(TBICSR);
1215 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1216 (duplex == DUPLEX_FULL)) {
1217 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1218 } else if (autoneg == AUTONEG_ENABLE)
1219 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1220 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001221 netif_warn(tp, link, dev,
1222 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 ret = -EOPNOTSUPP;
1224 }
1225
1226 return ret;
1227}
1228
1229static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001230 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
1232 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001233 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001234 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Hayes Wang716b50a2011-02-22 17:26:18 +08001236 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001239 int auto_nego;
1240
françois romieu4da19632011-01-03 15:07:55 +00001241 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001242 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1243 ADVERTISE_100HALF | ADVERTISE_100FULL);
1244
1245 if (adv & ADVERTISED_10baseT_Half)
1246 auto_nego |= ADVERTISE_10HALF;
1247 if (adv & ADVERTISED_10baseT_Full)
1248 auto_nego |= ADVERTISE_10FULL;
1249 if (adv & ADVERTISED_100baseT_Half)
1250 auto_nego |= ADVERTISE_100HALF;
1251 if (adv & ADVERTISED_100baseT_Full)
1252 auto_nego |= ADVERTISE_100FULL;
1253
françois romieu3577aa12009-05-19 10:46:48 +00001254 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1255
françois romieu4da19632011-01-03 15:07:55 +00001256 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001257 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1258
1259 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001260 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001261 if (adv & ADVERTISED_1000baseT_Half)
1262 giga_ctrl |= ADVERTISE_1000HALF;
1263 if (adv & ADVERTISED_1000baseT_Full)
1264 giga_ctrl |= ADVERTISE_1000FULL;
1265 } else if (adv & (ADVERTISED_1000baseT_Half |
1266 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001267 netif_info(tp, link, dev,
1268 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001269 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
françois romieu3577aa12009-05-19 10:46:48 +00001272 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001273
françois romieu4da19632011-01-03 15:07:55 +00001274 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1275 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001276 } else {
1277 giga_ctrl = 0;
1278
1279 if (speed == SPEED_10)
1280 bmcr = 0;
1281 else if (speed == SPEED_100)
1282 bmcr = BMCR_SPEED100;
1283 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001284 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001285
1286 if (duplex == DUPLEX_FULL)
1287 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001288 }
1289
françois romieu4da19632011-01-03 15:07:55 +00001290 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001291
Francois Romieucecb5fd2011-04-01 10:21:07 +02001292 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1293 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001294 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001295 rtl_writephy(tp, 0x17, 0x2138);
1296 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001297 } else {
françois romieu4da19632011-01-03 15:07:55 +00001298 rtl_writephy(tp, 0x17, 0x2108);
1299 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001300 }
1301 }
1302
Oliver Neukum54405cd2011-01-06 21:55:13 +01001303 rc = 0;
1304out:
1305 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306}
1307
1308static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001309 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
1311 struct rtl8169_private *tp = netdev_priv(dev);
1312 int ret;
1313
Oliver Neukum54405cd2011-01-06 21:55:13 +01001314 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001315 if (ret < 0)
1316 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Francois Romieu4876cc12011-03-11 21:07:11 +01001318 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1319 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001321 }
1322out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 return ret;
1324}
1325
1326static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1327{
1328 struct rtl8169_private *tp = netdev_priv(dev);
1329 unsigned long flags;
1330 int ret;
1331
Francois Romieu4876cc12011-03-11 21:07:11 +01001332 del_timer_sync(&tp->timer);
1333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 spin_lock_irqsave(&tp->lock, flags);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001335 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001336 cmd->duplex, cmd->advertising);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 return ret;
1340}
1341
Michał Mirosław350fb322011-04-08 06:35:56 +00001342static u32 rtl8169_fix_features(struct net_device *dev, u32 features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
Francois Romieu2b7b4312011-04-18 22:53:24 -07001344 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001345 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Michał Mirosław350fb322011-04-08 06:35:56 +00001347 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348}
1349
Michał Mirosław350fb322011-04-08 06:35:56 +00001350static int rtl8169_set_features(struct net_device *dev, u32 features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
1352 struct rtl8169_private *tp = netdev_priv(dev);
1353 void __iomem *ioaddr = tp->mmio_addr;
1354 unsigned long flags;
1355
1356 spin_lock_irqsave(&tp->lock, flags);
1357
Michał Mirosław350fb322011-04-08 06:35:56 +00001358 if (features & NETIF_F_RXCSUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 tp->cp_cmd |= RxChkSum;
1360 else
1361 tp->cp_cmd &= ~RxChkSum;
1362
Michał Mirosław350fb322011-04-08 06:35:56 +00001363 if (dev->features & NETIF_F_HW_VLAN_RX)
1364 tp->cp_cmd |= RxVlan;
1365 else
1366 tp->cp_cmd &= ~RxVlan;
1367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 RTL_W16(CPlusCmd, tp->cp_cmd);
1369 RTL_R16(CPlusCmd);
1370
1371 spin_unlock_irqrestore(&tp->lock, flags);
1372
1373 return 0;
1374}
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1377 struct sk_buff *skb)
1378{
Jesse Grosseab6d182010-10-20 13:56:03 +00001379 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1381}
1382
Francois Romieu7a8fc772011-03-01 17:18:33 +01001383static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
1385 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Francois Romieu7a8fc772011-03-01 17:18:33 +01001387 if (opts2 & RxVlanTag)
1388 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391}
1392
Francois Romieuccdffb92008-07-26 14:26:06 +02001393static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
1395 struct rtl8169_private *tp = netdev_priv(dev);
1396 void __iomem *ioaddr = tp->mmio_addr;
1397 u32 status;
1398
1399 cmd->supported =
1400 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1401 cmd->port = PORT_FIBRE;
1402 cmd->transceiver = XCVR_INTERNAL;
1403
1404 status = RTL_R32(TBICSR);
1405 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1406 cmd->autoneg = !!(status & TBINwEnable);
1407
David Decotigny70739492011-04-27 18:32:40 +00001408 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001410
1411 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412}
1413
Francois Romieuccdffb92008-07-26 14:26:06 +02001414static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
1416 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Francois Romieuccdffb92008-07-26 14:26:06 +02001418 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419}
1420
1421static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1422{
1423 struct rtl8169_private *tp = netdev_priv(dev);
1424 unsigned long flags;
Francois Romieuccdffb92008-07-26 14:26:06 +02001425 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427 spin_lock_irqsave(&tp->lock, flags);
1428
Francois Romieuccdffb92008-07-26 14:26:06 +02001429 rc = tp->get_settings(dev, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
1431 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieuccdffb92008-07-26 14:26:06 +02001432 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433}
1434
1435static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1436 void *p)
1437{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001438 struct rtl8169_private *tp = netdev_priv(dev);
1439 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Francois Romieu5b0384f2006-08-16 16:00:01 +02001441 if (regs->len > R8169_REGS_SIZE)
1442 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Francois Romieu5b0384f2006-08-16 16:00:01 +02001444 spin_lock_irqsave(&tp->lock, flags);
1445 memcpy_fromio(p, tp->mmio_addr, regs->len);
1446 spin_unlock_irqrestore(&tp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447}
1448
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001449static u32 rtl8169_get_msglevel(struct net_device *dev)
1450{
1451 struct rtl8169_private *tp = netdev_priv(dev);
1452
1453 return tp->msg_enable;
1454}
1455
1456static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1457{
1458 struct rtl8169_private *tp = netdev_priv(dev);
1459
1460 tp->msg_enable = value;
1461}
1462
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001463static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1464 "tx_packets",
1465 "rx_packets",
1466 "tx_errors",
1467 "rx_errors",
1468 "rx_missed",
1469 "align_errors",
1470 "tx_single_collisions",
1471 "tx_multi_collisions",
1472 "unicast",
1473 "broadcast",
1474 "multicast",
1475 "tx_aborted",
1476 "tx_underrun",
1477};
1478
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001479static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001480{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001481 switch (sset) {
1482 case ETH_SS_STATS:
1483 return ARRAY_SIZE(rtl8169_gstrings);
1484 default:
1485 return -EOPNOTSUPP;
1486 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001487}
1488
Ivan Vecera355423d2009-02-06 21:49:57 -08001489static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001490{
1491 struct rtl8169_private *tp = netdev_priv(dev);
1492 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001493 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001494 struct rtl8169_counters *counters;
1495 dma_addr_t paddr;
1496 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001497 int wait = 1000;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001498
Ivan Vecera355423d2009-02-06 21:49:57 -08001499 /*
1500 * Some chips are unable to dump tally counters when the receiver
1501 * is disabled.
1502 */
1503 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1504 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001505
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001506 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001507 if (!counters)
1508 return;
1509
1510 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001511 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001512 RTL_W32(CounterAddrLow, cmd);
1513 RTL_W32(CounterAddrLow, cmd | CounterDump);
1514
Ivan Vecera355423d2009-02-06 21:49:57 -08001515 while (wait--) {
1516 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
Ivan Vecera355423d2009-02-06 21:49:57 -08001517 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001518 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001519 }
1520 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001521 }
1522
1523 RTL_W32(CounterAddrLow, 0);
1524 RTL_W32(CounterAddrHigh, 0);
1525
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001526 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001527}
1528
Ivan Vecera355423d2009-02-06 21:49:57 -08001529static void rtl8169_get_ethtool_stats(struct net_device *dev,
1530 struct ethtool_stats *stats, u64 *data)
1531{
1532 struct rtl8169_private *tp = netdev_priv(dev);
1533
1534 ASSERT_RTNL();
1535
1536 rtl8169_update_counters(dev);
1537
1538 data[0] = le64_to_cpu(tp->counters.tx_packets);
1539 data[1] = le64_to_cpu(tp->counters.rx_packets);
1540 data[2] = le64_to_cpu(tp->counters.tx_errors);
1541 data[3] = le32_to_cpu(tp->counters.rx_errors);
1542 data[4] = le16_to_cpu(tp->counters.rx_missed);
1543 data[5] = le16_to_cpu(tp->counters.align_errors);
1544 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1545 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1546 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1547 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1548 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1549 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1550 data[12] = le16_to_cpu(tp->counters.tx_underun);
1551}
1552
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001553static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1554{
1555 switch(stringset) {
1556 case ETH_SS_STATS:
1557 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1558 break;
1559 }
1560}
1561
Jeff Garzik7282d492006-09-13 14:30:00 -04001562static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 .get_drvinfo = rtl8169_get_drvinfo,
1564 .get_regs_len = rtl8169_get_regs_len,
1565 .get_link = ethtool_op_get_link,
1566 .get_settings = rtl8169_get_settings,
1567 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001568 .get_msglevel = rtl8169_get_msglevel,
1569 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001571 .get_wol = rtl8169_get_wol,
1572 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001573 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001574 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001575 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576};
1577
Francois Romieu07d3f512007-02-21 22:40:46 +01001578static void rtl8169_get_mac_version(struct rtl8169_private *tp,
1579 void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
Francois Romieu0e485152007-02-20 00:00:26 +01001581 /*
1582 * The driver currently handles the 8168Bf and the 8168Be identically
1583 * but they can be identified more specifically through the test below
1584 * if needed:
1585 *
1586 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001587 *
1588 * Same thing for the 8101Eb and the 8101Ec:
1589 *
1590 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001591 */
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001592 static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001594 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 int mac_version;
1596 } mac_info[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00001597 /* 8168E family. */
1598 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1599 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1600 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1601
Francois Romieu5b538df2008-07-20 16:22:45 +02001602 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001603 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1604 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001605 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001606
françois romieue6de30d2011-01-03 15:08:37 +00001607 /* 8168DP family. */
1608 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1609 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00001610 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00001611
Francois Romieuef808d52008-06-29 13:10:54 +02001612 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001613 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001614 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001615 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001616 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001617 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1618 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001619 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001620 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001621 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001622
1623 /* 8168B family. */
1624 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1625 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1626 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1627 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1628
1629 /* 8101 family. */
hayeswang36a0e6c2011-03-21 01:50:30 +00001630 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08001631 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1632 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1633 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001634 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1635 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1636 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1637 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1638 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1639 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001640 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001641 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001642 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001643 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1644 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001645 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1646 /* FIXME: where did these entries come from ? -- FR */
1647 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1648 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1649
1650 /* 8110 family. */
1651 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1652 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1653 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1654 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1655 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1656 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1657
Jean Delvaref21b75e2009-05-26 20:54:48 -07001658 /* Catch-all */
1659 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 }, *p = mac_info;
1661 u32 reg;
1662
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001663 reg = RTL_R32(TxConfig);
1664 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 p++;
1666 tp->mac_version = p->mac_version;
1667}
1668
1669static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1670{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001671 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672}
1673
Francois Romieu867763c2007-08-17 18:21:58 +02001674struct phy_reg {
1675 u16 reg;
1676 u16 val;
1677};
1678
françois romieu4da19632011-01-03 15:07:55 +00001679static void rtl_writephy_batch(struct rtl8169_private *tp,
1680 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02001681{
1682 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00001683 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02001684 regs++;
1685 }
1686}
1687
françois romieubca03d52011-01-03 15:07:31 +00001688#define PHY_READ 0x00000000
1689#define PHY_DATA_OR 0x10000000
1690#define PHY_DATA_AND 0x20000000
1691#define PHY_BJMPN 0x30000000
1692#define PHY_READ_EFUSE 0x40000000
1693#define PHY_READ_MAC_BYTE 0x50000000
1694#define PHY_WRITE_MAC_BYTE 0x60000000
1695#define PHY_CLEAR_READCOUNT 0x70000000
1696#define PHY_WRITE 0x80000000
1697#define PHY_READCOUNT_EQ_SKIP 0x90000000
1698#define PHY_COMP_EQ_SKIPN 0xa0000000
1699#define PHY_COMP_NEQ_SKIPN 0xb0000000
1700#define PHY_WRITE_PREVIOUS 0xc0000000
1701#define PHY_SKIPN 0xd0000000
1702#define PHY_DELAY_MS 0xe0000000
1703#define PHY_WRITE_ERI_WORD 0xf0000000
1704
1705static void
1706rtl_phy_write_fw(struct rtl8169_private *tp, const struct firmware *fw)
1707{
françois romieubca03d52011-01-03 15:07:31 +00001708 __le32 *phytable = (__le32 *)fw->data;
1709 struct net_device *dev = tp->dev;
hayeswang42b82dc2011-01-10 02:07:25 +00001710 size_t index, fw_size = fw->size / sizeof(*phytable);
1711 u32 predata, count;
françois romieubca03d52011-01-03 15:07:31 +00001712
1713 if (fw->size % sizeof(*phytable)) {
1714 netif_err(tp, probe, dev, "odd sized firmware %zd\n", fw->size);
1715 return;
1716 }
1717
hayeswang42b82dc2011-01-10 02:07:25 +00001718 for (index = 0; index < fw_size; index++) {
1719 u32 action = le32_to_cpu(phytable[index]);
1720 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00001721
hayeswang42b82dc2011-01-10 02:07:25 +00001722 switch(action & 0xf0000000) {
1723 case PHY_READ:
1724 case PHY_DATA_OR:
1725 case PHY_DATA_AND:
1726 case PHY_READ_EFUSE:
1727 case PHY_CLEAR_READCOUNT:
1728 case PHY_WRITE:
1729 case PHY_WRITE_PREVIOUS:
1730 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00001731 break;
1732
hayeswang42b82dc2011-01-10 02:07:25 +00001733 case PHY_BJMPN:
1734 if (regno > index) {
1735 netif_err(tp, probe, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001736 "Out of range of firmware\n");
hayeswang42b82dc2011-01-10 02:07:25 +00001737 return;
1738 }
1739 break;
1740 case PHY_READCOUNT_EQ_SKIP:
1741 if (index + 2 >= fw_size) {
1742 netif_err(tp, probe, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001743 "Out of range of firmware\n");
hayeswang42b82dc2011-01-10 02:07:25 +00001744 return;
1745 }
1746 break;
1747 case PHY_COMP_EQ_SKIPN:
1748 case PHY_COMP_NEQ_SKIPN:
1749 case PHY_SKIPN:
1750 if (index + 1 + regno >= fw_size) {
1751 netif_err(tp, probe, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001752 "Out of range of firmware\n");
hayeswang42b82dc2011-01-10 02:07:25 +00001753 return;
1754 }
1755 break;
1756
1757 case PHY_READ_MAC_BYTE:
1758 case PHY_WRITE_MAC_BYTE:
1759 case PHY_WRITE_ERI_WORD:
1760 default:
1761 netif_err(tp, probe, tp->dev,
1762 "Invalid action 0x%08x\n", action);
françois romieubca03d52011-01-03 15:07:31 +00001763 return;
1764 }
1765 }
1766
hayeswang42b82dc2011-01-10 02:07:25 +00001767 predata = 0;
1768 count = 0;
1769
1770 for (index = 0; index < fw_size; ) {
1771 u32 action = le32_to_cpu(phytable[index]);
françois romieubca03d52011-01-03 15:07:31 +00001772 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00001773 u32 regno = (action & 0x0fff0000) >> 16;
1774
1775 if (!action)
1776 break;
françois romieubca03d52011-01-03 15:07:31 +00001777
1778 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00001779 case PHY_READ:
1780 predata = rtl_readphy(tp, regno);
1781 count++;
1782 index++;
françois romieubca03d52011-01-03 15:07:31 +00001783 break;
hayeswang42b82dc2011-01-10 02:07:25 +00001784 case PHY_DATA_OR:
1785 predata |= data;
1786 index++;
1787 break;
1788 case PHY_DATA_AND:
1789 predata &= data;
1790 index++;
1791 break;
1792 case PHY_BJMPN:
1793 index -= regno;
1794 break;
1795 case PHY_READ_EFUSE:
1796 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
1797 index++;
1798 break;
1799 case PHY_CLEAR_READCOUNT:
1800 count = 0;
1801 index++;
1802 break;
1803 case PHY_WRITE:
1804 rtl_writephy(tp, regno, data);
1805 index++;
1806 break;
1807 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02001808 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00001809 break;
1810 case PHY_COMP_EQ_SKIPN:
1811 if (predata == data)
1812 index += regno;
1813 index++;
1814 break;
1815 case PHY_COMP_NEQ_SKIPN:
1816 if (predata != data)
1817 index += regno;
1818 index++;
1819 break;
1820 case PHY_WRITE_PREVIOUS:
1821 rtl_writephy(tp, regno, predata);
1822 index++;
1823 break;
1824 case PHY_SKIPN:
1825 index += regno + 1;
1826 break;
1827 case PHY_DELAY_MS:
1828 mdelay(data);
1829 index++;
1830 break;
1831
1832 case PHY_READ_MAC_BYTE:
1833 case PHY_WRITE_MAC_BYTE:
1834 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00001835 default:
1836 BUG();
1837 }
1838 }
1839}
1840
françois romieuf1e02ed2011-01-13 13:07:53 +00001841static void rtl_release_firmware(struct rtl8169_private *tp)
1842{
François Romieu953a12c2011-04-24 17:38:48 +02001843 if (!IS_ERR_OR_NULL(tp->fw))
1844 release_firmware(tp->fw);
1845 tp->fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00001846}
1847
François Romieu953a12c2011-04-24 17:38:48 +02001848static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00001849{
François Romieu953a12c2011-04-24 17:38:48 +02001850 const struct firmware *fw = tp->fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00001851
1852 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
François Romieu953a12c2011-04-24 17:38:48 +02001853 if (!IS_ERR_OR_NULL(fw))
1854 rtl_phy_write_fw(tp, fw);
1855}
1856
1857static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
1858{
1859 if (rtl_readphy(tp, reg) != val)
1860 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
1861 else
1862 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00001863}
1864
françois romieu4da19632011-01-03 15:07:55 +00001865static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001867 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00001868 { 0x1f, 0x0001 },
1869 { 0x06, 0x006e },
1870 { 0x08, 0x0708 },
1871 { 0x15, 0x4000 },
1872 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
françois romieu0b9b5712009-08-10 19:44:56 +00001874 { 0x1f, 0x0001 },
1875 { 0x03, 0x00a1 },
1876 { 0x02, 0x0008 },
1877 { 0x01, 0x0120 },
1878 { 0x00, 0x1000 },
1879 { 0x04, 0x0800 },
1880 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
françois romieu0b9b5712009-08-10 19:44:56 +00001882 { 0x03, 0xff41 },
1883 { 0x02, 0xdf60 },
1884 { 0x01, 0x0140 },
1885 { 0x00, 0x0077 },
1886 { 0x04, 0x7800 },
1887 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
françois romieu0b9b5712009-08-10 19:44:56 +00001889 { 0x03, 0x802f },
1890 { 0x02, 0x4f02 },
1891 { 0x01, 0x0409 },
1892 { 0x00, 0xf0f9 },
1893 { 0x04, 0x9800 },
1894 { 0x04, 0x9000 },
1895
1896 { 0x03, 0xdf01 },
1897 { 0x02, 0xdf20 },
1898 { 0x01, 0xff95 },
1899 { 0x00, 0xba00 },
1900 { 0x04, 0xa800 },
1901 { 0x04, 0xa000 },
1902
1903 { 0x03, 0xff41 },
1904 { 0x02, 0xdf20 },
1905 { 0x01, 0x0140 },
1906 { 0x00, 0x00bb },
1907 { 0x04, 0xb800 },
1908 { 0x04, 0xb000 },
1909
1910 { 0x03, 0xdf41 },
1911 { 0x02, 0xdc60 },
1912 { 0x01, 0x6340 },
1913 { 0x00, 0x007d },
1914 { 0x04, 0xd800 },
1915 { 0x04, 0xd000 },
1916
1917 { 0x03, 0xdf01 },
1918 { 0x02, 0xdf20 },
1919 { 0x01, 0x100a },
1920 { 0x00, 0xa0ff },
1921 { 0x04, 0xf800 },
1922 { 0x04, 0xf000 },
1923
1924 { 0x1f, 0x0000 },
1925 { 0x0b, 0x0000 },
1926 { 0x00, 0x9200 }
1927 };
1928
françois romieu4da19632011-01-03 15:07:55 +00001929 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930}
1931
françois romieu4da19632011-01-03 15:07:55 +00001932static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02001933{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001934 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02001935 { 0x1f, 0x0002 },
1936 { 0x01, 0x90d0 },
1937 { 0x1f, 0x0000 }
1938 };
1939
françois romieu4da19632011-01-03 15:07:55 +00001940 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02001941}
1942
françois romieu4da19632011-01-03 15:07:55 +00001943static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00001944{
1945 struct pci_dev *pdev = tp->pci_dev;
1946 u16 vendor_id, device_id;
1947
1948 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &vendor_id);
1949 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &device_id);
1950
1951 if ((vendor_id != PCI_VENDOR_ID_GIGABYTE) || (device_id != 0xe000))
1952 return;
1953
françois romieu4da19632011-01-03 15:07:55 +00001954 rtl_writephy(tp, 0x1f, 0x0001);
1955 rtl_writephy(tp, 0x10, 0xf01b);
1956 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00001957}
1958
françois romieu4da19632011-01-03 15:07:55 +00001959static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00001960{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001961 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00001962 { 0x1f, 0x0001 },
1963 { 0x04, 0x0000 },
1964 { 0x03, 0x00a1 },
1965 { 0x02, 0x0008 },
1966 { 0x01, 0x0120 },
1967 { 0x00, 0x1000 },
1968 { 0x04, 0x0800 },
1969 { 0x04, 0x9000 },
1970 { 0x03, 0x802f },
1971 { 0x02, 0x4f02 },
1972 { 0x01, 0x0409 },
1973 { 0x00, 0xf099 },
1974 { 0x04, 0x9800 },
1975 { 0x04, 0xa000 },
1976 { 0x03, 0xdf01 },
1977 { 0x02, 0xdf20 },
1978 { 0x01, 0xff95 },
1979 { 0x00, 0xba00 },
1980 { 0x04, 0xa800 },
1981 { 0x04, 0xf000 },
1982 { 0x03, 0xdf01 },
1983 { 0x02, 0xdf20 },
1984 { 0x01, 0x101a },
1985 { 0x00, 0xa0ff },
1986 { 0x04, 0xf800 },
1987 { 0x04, 0x0000 },
1988 { 0x1f, 0x0000 },
1989
1990 { 0x1f, 0x0001 },
1991 { 0x10, 0xf41b },
1992 { 0x14, 0xfb54 },
1993 { 0x18, 0xf5c7 },
1994 { 0x1f, 0x0000 },
1995
1996 { 0x1f, 0x0001 },
1997 { 0x17, 0x0cc0 },
1998 { 0x1f, 0x0000 }
1999 };
2000
françois romieu4da19632011-01-03 15:07:55 +00002001 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002002
françois romieu4da19632011-01-03 15:07:55 +00002003 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002004}
2005
françois romieu4da19632011-01-03 15:07:55 +00002006static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002007{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002008 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002009 { 0x1f, 0x0001 },
2010 { 0x04, 0x0000 },
2011 { 0x03, 0x00a1 },
2012 { 0x02, 0x0008 },
2013 { 0x01, 0x0120 },
2014 { 0x00, 0x1000 },
2015 { 0x04, 0x0800 },
2016 { 0x04, 0x9000 },
2017 { 0x03, 0x802f },
2018 { 0x02, 0x4f02 },
2019 { 0x01, 0x0409 },
2020 { 0x00, 0xf099 },
2021 { 0x04, 0x9800 },
2022 { 0x04, 0xa000 },
2023 { 0x03, 0xdf01 },
2024 { 0x02, 0xdf20 },
2025 { 0x01, 0xff95 },
2026 { 0x00, 0xba00 },
2027 { 0x04, 0xa800 },
2028 { 0x04, 0xf000 },
2029 { 0x03, 0xdf01 },
2030 { 0x02, 0xdf20 },
2031 { 0x01, 0x101a },
2032 { 0x00, 0xa0ff },
2033 { 0x04, 0xf800 },
2034 { 0x04, 0x0000 },
2035 { 0x1f, 0x0000 },
2036
2037 { 0x1f, 0x0001 },
2038 { 0x0b, 0x8480 },
2039 { 0x1f, 0x0000 },
2040
2041 { 0x1f, 0x0001 },
2042 { 0x18, 0x67c7 },
2043 { 0x04, 0x2000 },
2044 { 0x03, 0x002f },
2045 { 0x02, 0x4360 },
2046 { 0x01, 0x0109 },
2047 { 0x00, 0x3022 },
2048 { 0x04, 0x2800 },
2049 { 0x1f, 0x0000 },
2050
2051 { 0x1f, 0x0001 },
2052 { 0x17, 0x0cc0 },
2053 { 0x1f, 0x0000 }
2054 };
2055
françois romieu4da19632011-01-03 15:07:55 +00002056 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002057}
2058
françois romieu4da19632011-01-03 15:07:55 +00002059static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002060{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002061 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002062 { 0x10, 0xf41b },
2063 { 0x1f, 0x0000 }
2064 };
2065
françois romieu4da19632011-01-03 15:07:55 +00002066 rtl_writephy(tp, 0x1f, 0x0001);
2067 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002068
françois romieu4da19632011-01-03 15:07:55 +00002069 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002070}
2071
françois romieu4da19632011-01-03 15:07:55 +00002072static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002073{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002074 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002075 { 0x1f, 0x0001 },
2076 { 0x10, 0xf41b },
2077 { 0x1f, 0x0000 }
2078 };
2079
françois romieu4da19632011-01-03 15:07:55 +00002080 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002081}
2082
françois romieu4da19632011-01-03 15:07:55 +00002083static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002084{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002085 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002086 { 0x1f, 0x0000 },
2087 { 0x1d, 0x0f00 },
2088 { 0x1f, 0x0002 },
2089 { 0x0c, 0x1ec8 },
2090 { 0x1f, 0x0000 }
2091 };
2092
françois romieu4da19632011-01-03 15:07:55 +00002093 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002094}
2095
françois romieu4da19632011-01-03 15:07:55 +00002096static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002097{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002098 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002099 { 0x1f, 0x0001 },
2100 { 0x1d, 0x3d98 },
2101 { 0x1f, 0x0000 }
2102 };
2103
françois romieu4da19632011-01-03 15:07:55 +00002104 rtl_writephy(tp, 0x1f, 0x0000);
2105 rtl_patchphy(tp, 0x14, 1 << 5);
2106 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002107
françois romieu4da19632011-01-03 15:07:55 +00002108 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002109}
2110
françois romieu4da19632011-01-03 15:07:55 +00002111static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002112{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002113 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002114 { 0x1f, 0x0001 },
2115 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002116 { 0x1f, 0x0002 },
2117 { 0x00, 0x88d4 },
2118 { 0x01, 0x82b1 },
2119 { 0x03, 0x7002 },
2120 { 0x08, 0x9e30 },
2121 { 0x09, 0x01f0 },
2122 { 0x0a, 0x5500 },
2123 { 0x0c, 0x00c8 },
2124 { 0x1f, 0x0003 },
2125 { 0x12, 0xc096 },
2126 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002127 { 0x1f, 0x0000 },
2128 { 0x1f, 0x0000 },
2129 { 0x09, 0x2000 },
2130 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002131 };
2132
françois romieu4da19632011-01-03 15:07:55 +00002133 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002134
françois romieu4da19632011-01-03 15:07:55 +00002135 rtl_patchphy(tp, 0x14, 1 << 5);
2136 rtl_patchphy(tp, 0x0d, 1 << 5);
2137 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002138}
2139
françois romieu4da19632011-01-03 15:07:55 +00002140static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002141{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002142 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002143 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002144 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002145 { 0x03, 0x802f },
2146 { 0x02, 0x4f02 },
2147 { 0x01, 0x0409 },
2148 { 0x00, 0xf099 },
2149 { 0x04, 0x9800 },
2150 { 0x04, 0x9000 },
2151 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002152 { 0x1f, 0x0002 },
2153 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002154 { 0x06, 0x0761 },
2155 { 0x1f, 0x0003 },
2156 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002157 { 0x1f, 0x0000 }
2158 };
2159
françois romieu4da19632011-01-03 15:07:55 +00002160 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002161
françois romieu4da19632011-01-03 15:07:55 +00002162 rtl_patchphy(tp, 0x16, 1 << 0);
2163 rtl_patchphy(tp, 0x14, 1 << 5);
2164 rtl_patchphy(tp, 0x0d, 1 << 5);
2165 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002166}
2167
françois romieu4da19632011-01-03 15:07:55 +00002168static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002169{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002170 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002171 { 0x1f, 0x0001 },
2172 { 0x12, 0x2300 },
2173 { 0x1d, 0x3d98 },
2174 { 0x1f, 0x0002 },
2175 { 0x0c, 0x7eb8 },
2176 { 0x06, 0x5461 },
2177 { 0x1f, 0x0003 },
2178 { 0x16, 0x0f0a },
2179 { 0x1f, 0x0000 }
2180 };
2181
françois romieu4da19632011-01-03 15:07:55 +00002182 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002183
françois romieu4da19632011-01-03 15:07:55 +00002184 rtl_patchphy(tp, 0x16, 1 << 0);
2185 rtl_patchphy(tp, 0x14, 1 << 5);
2186 rtl_patchphy(tp, 0x0d, 1 << 5);
2187 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002188}
2189
françois romieu4da19632011-01-03 15:07:55 +00002190static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002191{
françois romieu4da19632011-01-03 15:07:55 +00002192 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002193}
2194
françois romieubca03d52011-01-03 15:07:31 +00002195static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002196{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002197 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002198 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002199 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002200 { 0x06, 0x4064 },
2201 { 0x07, 0x2863 },
2202 { 0x08, 0x059c },
2203 { 0x09, 0x26b4 },
2204 { 0x0a, 0x6a19 },
2205 { 0x0b, 0xdcc8 },
2206 { 0x10, 0xf06d },
2207 { 0x14, 0x7f68 },
2208 { 0x18, 0x7fd9 },
2209 { 0x1c, 0xf0ff },
2210 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002211 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002212 { 0x12, 0xf49f },
2213 { 0x13, 0x070b },
2214 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002215 { 0x14, 0x94c0 },
2216
2217 /*
2218 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002219 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002220 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002221 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002222 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002223 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002224 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002225 { 0x06, 0x5561 },
2226
2227 /*
2228 * Can not link to 1Gbps with bad cable
2229 * Decrease SNR threshold form 21.07dB to 19.04dB
2230 */
2231 { 0x1f, 0x0001 },
2232 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002233
2234 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002235 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002236 };
françois romieubca03d52011-01-03 15:07:31 +00002237 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002238
françois romieu4da19632011-01-03 15:07:55 +00002239 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002240
françois romieubca03d52011-01-03 15:07:31 +00002241 /*
2242 * Rx Error Issue
2243 * Fine Tune Switching regulator parameter
2244 */
françois romieu4da19632011-01-03 15:07:55 +00002245 rtl_writephy(tp, 0x1f, 0x0002);
2246 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2247 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002248
françois romieudaf9df62009-10-07 12:44:20 +00002249 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002250 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002251 { 0x1f, 0x0002 },
2252 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002253 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002254 { 0x05, 0x8330 },
2255 { 0x06, 0x669a },
2256 { 0x1f, 0x0002 }
2257 };
2258 int val;
2259
françois romieu4da19632011-01-03 15:07:55 +00002260 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002261
françois romieu4da19632011-01-03 15:07:55 +00002262 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002263
2264 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002265 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002266 0x0065, 0x0066, 0x0067, 0x0068,
2267 0x0069, 0x006a, 0x006b, 0x006c
2268 };
2269 int i;
2270
françois romieu4da19632011-01-03 15:07:55 +00002271 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002272
2273 val &= 0xff00;
2274 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002275 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002276 }
2277 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002278 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002279 { 0x1f, 0x0002 },
2280 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002281 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002282 { 0x05, 0x8330 },
2283 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002284 };
2285
françois romieu4da19632011-01-03 15:07:55 +00002286 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002287 }
2288
françois romieubca03d52011-01-03 15:07:31 +00002289 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002290 rtl_writephy(tp, 0x1f, 0x0002);
2291 rtl_patchphy(tp, 0x0d, 0x0300);
2292 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002293
françois romieubca03d52011-01-03 15:07:31 +00002294 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002295 rtl_writephy(tp, 0x1f, 0x0002);
2296 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2297 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002298
françois romieu4da19632011-01-03 15:07:55 +00002299 rtl_writephy(tp, 0x1f, 0x0005);
2300 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002301
2302 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002303
françois romieu4da19632011-01-03 15:07:55 +00002304 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002305}
2306
françois romieubca03d52011-01-03 15:07:31 +00002307static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002308{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002309 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002310 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002311 { 0x1f, 0x0001 },
2312 { 0x06, 0x4064 },
2313 { 0x07, 0x2863 },
2314 { 0x08, 0x059c },
2315 { 0x09, 0x26b4 },
2316 { 0x0a, 0x6a19 },
2317 { 0x0b, 0xdcc8 },
2318 { 0x10, 0xf06d },
2319 { 0x14, 0x7f68 },
2320 { 0x18, 0x7fd9 },
2321 { 0x1c, 0xf0ff },
2322 { 0x1d, 0x3d9c },
2323 { 0x1f, 0x0003 },
2324 { 0x12, 0xf49f },
2325 { 0x13, 0x070b },
2326 { 0x1a, 0x05ad },
2327 { 0x14, 0x94c0 },
2328
françois romieubca03d52011-01-03 15:07:31 +00002329 /*
2330 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002331 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002332 */
françois romieudaf9df62009-10-07 12:44:20 +00002333 { 0x1f, 0x0002 },
2334 { 0x06, 0x5561 },
2335 { 0x1f, 0x0005 },
2336 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002337 { 0x06, 0x5561 },
2338
2339 /*
2340 * Can not link to 1Gbps with bad cable
2341 * Decrease SNR threshold form 21.07dB to 19.04dB
2342 */
2343 { 0x1f, 0x0001 },
2344 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002345
2346 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002347 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002348 };
françois romieubca03d52011-01-03 15:07:31 +00002349 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002350
françois romieu4da19632011-01-03 15:07:55 +00002351 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002352
2353 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002354 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002355 { 0x1f, 0x0002 },
2356 { 0x05, 0x669a },
2357 { 0x1f, 0x0005 },
2358 { 0x05, 0x8330 },
2359 { 0x06, 0x669a },
2360
2361 { 0x1f, 0x0002 }
2362 };
2363 int val;
2364
françois romieu4da19632011-01-03 15:07:55 +00002365 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002366
françois romieu4da19632011-01-03 15:07:55 +00002367 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002368 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002369 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002370 0x0065, 0x0066, 0x0067, 0x0068,
2371 0x0069, 0x006a, 0x006b, 0x006c
2372 };
2373 int i;
2374
françois romieu4da19632011-01-03 15:07:55 +00002375 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002376
2377 val &= 0xff00;
2378 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002379 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002380 }
2381 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002382 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002383 { 0x1f, 0x0002 },
2384 { 0x05, 0x2642 },
2385 { 0x1f, 0x0005 },
2386 { 0x05, 0x8330 },
2387 { 0x06, 0x2642 }
2388 };
2389
françois romieu4da19632011-01-03 15:07:55 +00002390 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002391 }
2392
françois romieubca03d52011-01-03 15:07:31 +00002393 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002394 rtl_writephy(tp, 0x1f, 0x0002);
2395 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2396 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002397
françois romieubca03d52011-01-03 15:07:31 +00002398 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002399 rtl_writephy(tp, 0x1f, 0x0002);
2400 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002401
françois romieu4da19632011-01-03 15:07:55 +00002402 rtl_writephy(tp, 0x1f, 0x0005);
2403 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002404
2405 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002406
françois romieu4da19632011-01-03 15:07:55 +00002407 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002408}
2409
françois romieu4da19632011-01-03 15:07:55 +00002410static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002411{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002412 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002413 { 0x1f, 0x0002 },
2414 { 0x10, 0x0008 },
2415 { 0x0d, 0x006c },
2416
2417 { 0x1f, 0x0000 },
2418 { 0x0d, 0xf880 },
2419
2420 { 0x1f, 0x0001 },
2421 { 0x17, 0x0cc0 },
2422
2423 { 0x1f, 0x0001 },
2424 { 0x0b, 0xa4d8 },
2425 { 0x09, 0x281c },
2426 { 0x07, 0x2883 },
2427 { 0x0a, 0x6b35 },
2428 { 0x1d, 0x3da4 },
2429 { 0x1c, 0xeffd },
2430 { 0x14, 0x7f52 },
2431 { 0x18, 0x7fc6 },
2432 { 0x08, 0x0601 },
2433 { 0x06, 0x4063 },
2434 { 0x10, 0xf074 },
2435 { 0x1f, 0x0003 },
2436 { 0x13, 0x0789 },
2437 { 0x12, 0xf4bd },
2438 { 0x1a, 0x04fd },
2439 { 0x14, 0x84b0 },
2440 { 0x1f, 0x0000 },
2441 { 0x00, 0x9200 },
2442
2443 { 0x1f, 0x0005 },
2444 { 0x01, 0x0340 },
2445 { 0x1f, 0x0001 },
2446 { 0x04, 0x4000 },
2447 { 0x03, 0x1d21 },
2448 { 0x02, 0x0c32 },
2449 { 0x01, 0x0200 },
2450 { 0x00, 0x5554 },
2451 { 0x04, 0x4800 },
2452 { 0x04, 0x4000 },
2453 { 0x04, 0xf000 },
2454 { 0x03, 0xdf01 },
2455 { 0x02, 0xdf20 },
2456 { 0x01, 0x101a },
2457 { 0x00, 0xa0ff },
2458 { 0x04, 0xf800 },
2459 { 0x04, 0xf000 },
2460 { 0x1f, 0x0000 },
2461
2462 { 0x1f, 0x0007 },
2463 { 0x1e, 0x0023 },
2464 { 0x16, 0x0000 },
2465 { 0x1f, 0x0000 }
2466 };
2467
françois romieu4da19632011-01-03 15:07:55 +00002468 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002469}
2470
françois romieue6de30d2011-01-03 15:08:37 +00002471static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2472{
2473 static const struct phy_reg phy_reg_init[] = {
2474 { 0x1f, 0x0001 },
2475 { 0x17, 0x0cc0 },
2476
2477 { 0x1f, 0x0007 },
2478 { 0x1e, 0x002d },
2479 { 0x18, 0x0040 },
2480 { 0x1f, 0x0000 }
2481 };
2482
2483 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2484 rtl_patchphy(tp, 0x0d, 1 << 5);
2485}
2486
hayeswang01dc7fe2011-03-21 01:50:28 +00002487static void rtl8168e_hw_phy_config(struct rtl8169_private *tp)
2488{
2489 static const struct phy_reg phy_reg_init[] = {
2490 /* Enable Delay cap */
2491 { 0x1f, 0x0005 },
2492 { 0x05, 0x8b80 },
2493 { 0x06, 0xc896 },
2494 { 0x1f, 0x0000 },
2495
2496 /* Channel estimation fine tune */
2497 { 0x1f, 0x0001 },
2498 { 0x0b, 0x6c20 },
2499 { 0x07, 0x2872 },
2500 { 0x1c, 0xefff },
2501 { 0x1f, 0x0003 },
2502 { 0x14, 0x6420 },
2503 { 0x1f, 0x0000 },
2504
2505 /* Update PFM & 10M TX idle timer */
2506 { 0x1f, 0x0007 },
2507 { 0x1e, 0x002f },
2508 { 0x15, 0x1919 },
2509 { 0x1f, 0x0000 },
2510
2511 { 0x1f, 0x0007 },
2512 { 0x1e, 0x00ac },
2513 { 0x18, 0x0006 },
2514 { 0x1f, 0x0000 }
2515 };
2516
Francois Romieu15ecd032011-04-27 13:52:22 -07002517 rtl_apply_firmware(tp);
2518
hayeswang01dc7fe2011-03-21 01:50:28 +00002519 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2520
2521 /* DCO enable for 10M IDLE Power */
2522 rtl_writephy(tp, 0x1f, 0x0007);
2523 rtl_writephy(tp, 0x1e, 0x0023);
2524 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2525 rtl_writephy(tp, 0x1f, 0x0000);
2526
2527 /* For impedance matching */
2528 rtl_writephy(tp, 0x1f, 0x0002);
2529 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002530 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002531
2532 /* PHY auto speed down */
2533 rtl_writephy(tp, 0x1f, 0x0007);
2534 rtl_writephy(tp, 0x1e, 0x002d);
2535 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2536 rtl_writephy(tp, 0x1f, 0x0000);
2537 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2538
2539 rtl_writephy(tp, 0x1f, 0x0005);
2540 rtl_writephy(tp, 0x05, 0x8b86);
2541 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2542 rtl_writephy(tp, 0x1f, 0x0000);
2543
2544 rtl_writephy(tp, 0x1f, 0x0005);
2545 rtl_writephy(tp, 0x05, 0x8b85);
2546 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2547 rtl_writephy(tp, 0x1f, 0x0007);
2548 rtl_writephy(tp, 0x1e, 0x0020);
2549 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2550 rtl_writephy(tp, 0x1f, 0x0006);
2551 rtl_writephy(tp, 0x00, 0x5a00);
2552 rtl_writephy(tp, 0x1f, 0x0000);
2553 rtl_writephy(tp, 0x0d, 0x0007);
2554 rtl_writephy(tp, 0x0e, 0x003c);
2555 rtl_writephy(tp, 0x0d, 0x4007);
2556 rtl_writephy(tp, 0x0e, 0x0000);
2557 rtl_writephy(tp, 0x0d, 0x0000);
2558}
2559
françois romieu4da19632011-01-03 15:07:55 +00002560static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02002561{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002562 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02002563 { 0x1f, 0x0003 },
2564 { 0x08, 0x441d },
2565 { 0x01, 0x9100 },
2566 { 0x1f, 0x0000 }
2567 };
2568
françois romieu4da19632011-01-03 15:07:55 +00002569 rtl_writephy(tp, 0x1f, 0x0000);
2570 rtl_patchphy(tp, 0x11, 1 << 12);
2571 rtl_patchphy(tp, 0x19, 1 << 13);
2572 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002573
françois romieu4da19632011-01-03 15:07:55 +00002574 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02002575}
2576
Hayes Wang5a5e4442011-02-22 17:26:21 +08002577static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
2578{
2579 static const struct phy_reg phy_reg_init[] = {
2580 { 0x1f, 0x0005 },
2581 { 0x1a, 0x0000 },
2582 { 0x1f, 0x0000 },
2583
2584 { 0x1f, 0x0004 },
2585 { 0x1c, 0x0000 },
2586 { 0x1f, 0x0000 },
2587
2588 { 0x1f, 0x0001 },
2589 { 0x15, 0x7701 },
2590 { 0x1f, 0x0000 }
2591 };
2592
2593 /* Disable ALDPS before ram code */
2594 rtl_writephy(tp, 0x1f, 0x0000);
2595 rtl_writephy(tp, 0x18, 0x0310);
2596 msleep(100);
2597
François Romieu953a12c2011-04-24 17:38:48 +02002598 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08002599
2600 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2601}
2602
Francois Romieu5615d9f2007-08-17 17:50:46 +02002603static void rtl_hw_phy_config(struct net_device *dev)
2604{
2605 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002606
2607 rtl8169_print_mac_version(tp);
2608
2609 switch (tp->mac_version) {
2610 case RTL_GIGA_MAC_VER_01:
2611 break;
2612 case RTL_GIGA_MAC_VER_02:
2613 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00002614 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002615 break;
2616 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00002617 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002618 break;
françois romieu2e9558562009-08-10 19:44:19 +00002619 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00002620 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002621 break;
françois romieu8c7006a2009-08-10 19:43:29 +00002622 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00002623 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00002624 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02002625 case RTL_GIGA_MAC_VER_07:
2626 case RTL_GIGA_MAC_VER_08:
2627 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00002628 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002629 break;
Francois Romieu236b8082008-05-30 16:11:48 +02002630 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00002631 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002632 break;
2633 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00002634 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002635 break;
2636 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00002637 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002638 break;
Francois Romieu867763c2007-08-17 18:21:58 +02002639 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00002640 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002641 break;
2642 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00002643 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002644 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02002645 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00002646 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002647 break;
Francois Romieu197ff762008-06-28 13:16:02 +02002648 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00002649 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02002650 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02002651 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00002652 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002653 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02002654 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002655 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00002656 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02002657 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02002658 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00002659 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002660 break;
2661 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00002662 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002663 break;
2664 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00002665 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02002666 break;
françois romieue6de30d2011-01-03 15:08:37 +00002667 case RTL_GIGA_MAC_VER_28:
2668 rtl8168d_4_hw_phy_config(tp);
2669 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08002670 case RTL_GIGA_MAC_VER_29:
2671 case RTL_GIGA_MAC_VER_30:
2672 rtl8105e_hw_phy_config(tp);
2673 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02002674 case RTL_GIGA_MAC_VER_31:
2675 /* None. */
2676 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00002677 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00002678 case RTL_GIGA_MAC_VER_33:
Francois Romieu15ecd032011-04-27 13:52:22 -07002679 rtl8168e_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00002680 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02002681
Francois Romieu5615d9f2007-08-17 17:50:46 +02002682 default:
2683 break;
2684 }
2685}
2686
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687static void rtl8169_phy_timer(unsigned long __opaque)
2688{
2689 struct net_device *dev = (struct net_device *)__opaque;
2690 struct rtl8169_private *tp = netdev_priv(dev);
2691 struct timer_list *timer = &tp->timer;
2692 void __iomem *ioaddr = tp->mmio_addr;
2693 unsigned long timeout = RTL8169_PHY_TIMEOUT;
2694
Francois Romieubcf0bf92006-07-26 23:14:13 +02002695 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 spin_lock_irq(&tp->lock);
2698
françois romieu4da19632011-01-03 15:07:55 +00002699 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02002700 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 * A busy loop could burn quite a few cycles on nowadays CPU.
2702 * Let's delay the execution of the timer for a few ticks.
2703 */
2704 timeout = HZ/10;
2705 goto out_mod_timer;
2706 }
2707
2708 if (tp->link_ok(ioaddr))
2709 goto out_unlock;
2710
Joe Perchesbf82c182010-02-09 11:49:50 +00002711 netif_warn(tp, link, dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
françois romieu4da19632011-01-03 15:07:55 +00002713 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
2715out_mod_timer:
2716 mod_timer(timer, jiffies + timeout);
2717out_unlock:
2718 spin_unlock_irq(&tp->lock);
2719}
2720
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721#ifdef CONFIG_NET_POLL_CONTROLLER
2722/*
2723 * Polling 'interrupt' - used by things like netconsole to send skbs
2724 * without having to re-enable interrupts. It's not called while
2725 * the interrupt routine is executing.
2726 */
2727static void rtl8169_netpoll(struct net_device *dev)
2728{
2729 struct rtl8169_private *tp = netdev_priv(dev);
2730 struct pci_dev *pdev = tp->pci_dev;
2731
2732 disable_irq(pdev->irq);
David Howells7d12e782006-10-05 14:55:46 +01002733 rtl8169_interrupt(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 enable_irq(pdev->irq);
2735}
2736#endif
2737
2738static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
2739 void __iomem *ioaddr)
2740{
2741 iounmap(ioaddr);
2742 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00002743 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 pci_disable_device(pdev);
2745 free_netdev(dev);
2746}
2747
Francois Romieubf793292006-11-01 00:53:05 +01002748static void rtl8169_phy_reset(struct net_device *dev,
2749 struct rtl8169_private *tp)
2750{
Francois Romieu07d3f512007-02-21 22:40:46 +01002751 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01002752
françois romieu4da19632011-01-03 15:07:55 +00002753 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01002754 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00002755 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01002756 return;
2757 msleep(1);
2758 }
Joe Perchesbf82c182010-02-09 11:49:50 +00002759 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01002760}
2761
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002762static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002764 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002765
Francois Romieu5615d9f2007-08-17 17:50:46 +02002766 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002767
Marcus Sundberg773328942008-07-10 21:28:08 +02002768 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
2769 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2770 RTL_W8(0x82, 0x01);
2771 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002772
Francois Romieu6dccd162007-02-13 23:38:05 +01002773 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
2774
2775 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
2776 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002777
Francois Romieubcf0bf92006-07-26 23:14:13 +02002778 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002779 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2780 RTL_W8(0x82, 0x01);
2781 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00002782 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002783 }
2784
Francois Romieubf793292006-11-01 00:53:05 +01002785 rtl8169_phy_reset(dev, tp);
2786
Oliver Neukum54405cd2011-01-06 21:55:13 +01002787 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002788 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
2789 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
2790 (tp->mii.supports_gmii ?
2791 ADVERTISED_1000baseT_Half |
2792 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002793
Joe Perchesbf82c182010-02-09 11:49:50 +00002794 if (RTL_R8(PHYstatus) & TBI_Enable)
2795 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002796}
2797
Francois Romieu773d2022007-01-31 23:47:43 +01002798static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
2799{
2800 void __iomem *ioaddr = tp->mmio_addr;
2801 u32 high;
2802 u32 low;
2803
2804 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
2805 high = addr[4] | (addr[5] << 8);
2806
2807 spin_lock_irq(&tp->lock);
2808
2809 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00002810
Francois Romieu773d2022007-01-31 23:47:43 +01002811 RTL_W32(MAC4, high);
françois romieu908ba2b2010-04-26 11:42:58 +00002812 RTL_R32(MAC4);
2813
Francois Romieu78f1cd02010-03-27 19:35:46 -07002814 RTL_W32(MAC0, low);
françois romieu908ba2b2010-04-26 11:42:58 +00002815 RTL_R32(MAC0);
2816
Francois Romieu773d2022007-01-31 23:47:43 +01002817 RTL_W8(Cfg9346, Cfg9346_Lock);
2818
2819 spin_unlock_irq(&tp->lock);
2820}
2821
2822static int rtl_set_mac_address(struct net_device *dev, void *p)
2823{
2824 struct rtl8169_private *tp = netdev_priv(dev);
2825 struct sockaddr *addr = p;
2826
2827 if (!is_valid_ether_addr(addr->sa_data))
2828 return -EADDRNOTAVAIL;
2829
2830 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
2831
2832 rtl_rar_set(tp, dev->dev_addr);
2833
2834 return 0;
2835}
2836
Francois Romieu5f787a12006-08-17 13:02:36 +02002837static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2838{
2839 struct rtl8169_private *tp = netdev_priv(dev);
2840 struct mii_ioctl_data *data = if_mii(ifr);
2841
Francois Romieu8b4ab282008-11-19 22:05:25 -08002842 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
2843}
Francois Romieu5f787a12006-08-17 13:02:36 +02002844
Francois Romieucecb5fd2011-04-01 10:21:07 +02002845static int rtl_xmii_ioctl(struct rtl8169_private *tp,
2846 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08002847{
Francois Romieu5f787a12006-08-17 13:02:36 +02002848 switch (cmd) {
2849 case SIOCGMIIPHY:
2850 data->phy_id = 32; /* Internal PHY */
2851 return 0;
2852
2853 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00002854 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02002855 return 0;
2856
2857 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00002858 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02002859 return 0;
2860 }
2861 return -EOPNOTSUPP;
2862}
2863
Francois Romieu8b4ab282008-11-19 22:05:25 -08002864static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
2865{
2866 return -EOPNOTSUPP;
2867}
2868
Francois Romieu0e485152007-02-20 00:00:26 +01002869static const struct rtl_cfg_info {
2870 void (*hw_start)(struct net_device *);
2871 unsigned int region;
2872 unsigned int align;
2873 u16 intr_event;
2874 u16 napi_event;
Francois Romieuccdffb92008-07-26 14:26:06 +02002875 unsigned features;
Jean Delvaref21b75e2009-05-26 20:54:48 -07002876 u8 default_ver;
Francois Romieu0e485152007-02-20 00:00:26 +01002877} rtl_cfg_infos [] = {
2878 [RTL_CFG_0] = {
2879 .hw_start = rtl_hw_start_8169,
2880 .region = 1,
Francois Romieue9f63f32007-02-28 23:16:57 +01002881 .align = 0,
Francois Romieu0e485152007-02-20 00:00:26 +01002882 .intr_event = SYSErr | LinkChg | RxOverflow |
2883 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02002884 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07002885 .features = RTL_FEATURE_GMII,
2886 .default_ver = RTL_GIGA_MAC_VER_01,
Francois Romieu0e485152007-02-20 00:00:26 +01002887 },
2888 [RTL_CFG_1] = {
2889 .hw_start = rtl_hw_start_8168,
2890 .region = 2,
2891 .align = 8,
françois romieu53f57352010-11-08 13:23:05 +00002892 .intr_event = SYSErr | LinkChg | RxOverflow |
Francois Romieu0e485152007-02-20 00:00:26 +01002893 TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02002894 .napi_event = TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07002895 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
2896 .default_ver = RTL_GIGA_MAC_VER_11,
Francois Romieu0e485152007-02-20 00:00:26 +01002897 },
2898 [RTL_CFG_2] = {
2899 .hw_start = rtl_hw_start_8101,
2900 .region = 2,
2901 .align = 8,
2902 .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
2903 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02002904 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07002905 .features = RTL_FEATURE_MSI,
2906 .default_ver = RTL_GIGA_MAC_VER_13,
Francois Romieu0e485152007-02-20 00:00:26 +01002907 }
2908};
2909
Francois Romieufbac58f2007-10-04 22:51:38 +02002910/* Cfg9346_Unlock assumed. */
2911static unsigned rtl_try_msi(struct pci_dev *pdev, void __iomem *ioaddr,
2912 const struct rtl_cfg_info *cfg)
2913{
2914 unsigned msi = 0;
2915 u8 cfg2;
2916
2917 cfg2 = RTL_R8(Config2) & ~MSIEnable;
Francois Romieuccdffb92008-07-26 14:26:06 +02002918 if (cfg->features & RTL_FEATURE_MSI) {
Francois Romieufbac58f2007-10-04 22:51:38 +02002919 if (pci_enable_msi(pdev)) {
2920 dev_info(&pdev->dev, "no MSI. Back to INTx.\n");
2921 } else {
2922 cfg2 |= MSIEnable;
2923 msi = RTL_FEATURE_MSI;
2924 }
2925 }
2926 RTL_W8(Config2, cfg2);
2927 return msi;
2928}
2929
2930static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
2931{
2932 if (tp->features & RTL_FEATURE_MSI) {
2933 pci_disable_msi(pdev);
2934 tp->features &= ~RTL_FEATURE_MSI;
2935 }
2936}
2937
Francois Romieu8b4ab282008-11-19 22:05:25 -08002938static const struct net_device_ops rtl8169_netdev_ops = {
2939 .ndo_open = rtl8169_open,
2940 .ndo_stop = rtl8169_close,
2941 .ndo_get_stats = rtl8169_get_stats,
Stephen Hemminger00829822008-11-20 20:14:53 -08002942 .ndo_start_xmit = rtl8169_start_xmit,
Francois Romieu8b4ab282008-11-19 22:05:25 -08002943 .ndo_tx_timeout = rtl8169_tx_timeout,
2944 .ndo_validate_addr = eth_validate_addr,
2945 .ndo_change_mtu = rtl8169_change_mtu,
Michał Mirosław350fb322011-04-08 06:35:56 +00002946 .ndo_fix_features = rtl8169_fix_features,
2947 .ndo_set_features = rtl8169_set_features,
Francois Romieu8b4ab282008-11-19 22:05:25 -08002948 .ndo_set_mac_address = rtl_set_mac_address,
2949 .ndo_do_ioctl = rtl8169_ioctl,
2950 .ndo_set_multicast_list = rtl_set_rx_mode,
Francois Romieu8b4ab282008-11-19 22:05:25 -08002951#ifdef CONFIG_NET_POLL_CONTROLLER
2952 .ndo_poll_controller = rtl8169_netpoll,
2953#endif
2954
2955};
2956
françois romieuc0e45c12011-01-03 15:08:04 +00002957static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
2958{
2959 struct mdio_ops *ops = &tp->mdio_ops;
2960
2961 switch (tp->mac_version) {
2962 case RTL_GIGA_MAC_VER_27:
2963 ops->write = r8168dp_1_mdio_write;
2964 ops->read = r8168dp_1_mdio_read;
2965 break;
françois romieue6de30d2011-01-03 15:08:37 +00002966 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00002967 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00002968 ops->write = r8168dp_2_mdio_write;
2969 ops->read = r8168dp_2_mdio_read;
2970 break;
françois romieuc0e45c12011-01-03 15:08:04 +00002971 default:
2972 ops->write = r8169_mdio_write;
2973 ops->read = r8169_mdio_read;
2974 break;
2975 }
2976}
2977
françois romieu065c27c2011-01-03 15:08:12 +00002978static void r810x_phy_power_down(struct rtl8169_private *tp)
2979{
2980 rtl_writephy(tp, 0x1f, 0x0000);
2981 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
2982}
2983
2984static void r810x_phy_power_up(struct rtl8169_private *tp)
2985{
2986 rtl_writephy(tp, 0x1f, 0x0000);
2987 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
2988}
2989
2990static void r810x_pll_power_down(struct rtl8169_private *tp)
2991{
2992 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
2993 rtl_writephy(tp, 0x1f, 0x0000);
2994 rtl_writephy(tp, MII_BMCR, 0x0000);
2995 return;
2996 }
2997
2998 r810x_phy_power_down(tp);
2999}
3000
3001static void r810x_pll_power_up(struct rtl8169_private *tp)
3002{
3003 r810x_phy_power_up(tp);
3004}
3005
3006static void r8168_phy_power_up(struct rtl8169_private *tp)
3007{
3008 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003009 switch (tp->mac_version) {
3010 case RTL_GIGA_MAC_VER_11:
3011 case RTL_GIGA_MAC_VER_12:
3012 case RTL_GIGA_MAC_VER_17:
3013 case RTL_GIGA_MAC_VER_18:
3014 case RTL_GIGA_MAC_VER_19:
3015 case RTL_GIGA_MAC_VER_20:
3016 case RTL_GIGA_MAC_VER_21:
3017 case RTL_GIGA_MAC_VER_22:
3018 case RTL_GIGA_MAC_VER_23:
3019 case RTL_GIGA_MAC_VER_24:
3020 case RTL_GIGA_MAC_VER_25:
3021 case RTL_GIGA_MAC_VER_26:
3022 case RTL_GIGA_MAC_VER_27:
3023 case RTL_GIGA_MAC_VER_28:
3024 case RTL_GIGA_MAC_VER_31:
3025 rtl_writephy(tp, 0x0e, 0x0000);
3026 break;
3027 default:
3028 break;
3029 }
françois romieu065c27c2011-01-03 15:08:12 +00003030 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3031}
3032
3033static void r8168_phy_power_down(struct rtl8169_private *tp)
3034{
3035 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003036 switch (tp->mac_version) {
3037 case RTL_GIGA_MAC_VER_32:
3038 case RTL_GIGA_MAC_VER_33:
3039 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3040 break;
3041
3042 case RTL_GIGA_MAC_VER_11:
3043 case RTL_GIGA_MAC_VER_12:
3044 case RTL_GIGA_MAC_VER_17:
3045 case RTL_GIGA_MAC_VER_18:
3046 case RTL_GIGA_MAC_VER_19:
3047 case RTL_GIGA_MAC_VER_20:
3048 case RTL_GIGA_MAC_VER_21:
3049 case RTL_GIGA_MAC_VER_22:
3050 case RTL_GIGA_MAC_VER_23:
3051 case RTL_GIGA_MAC_VER_24:
3052 case RTL_GIGA_MAC_VER_25:
3053 case RTL_GIGA_MAC_VER_26:
3054 case RTL_GIGA_MAC_VER_27:
3055 case RTL_GIGA_MAC_VER_28:
3056 case RTL_GIGA_MAC_VER_31:
3057 rtl_writephy(tp, 0x0e, 0x0200);
3058 default:
3059 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3060 break;
3061 }
françois romieu065c27c2011-01-03 15:08:12 +00003062}
3063
3064static void r8168_pll_power_down(struct rtl8169_private *tp)
3065{
3066 void __iomem *ioaddr = tp->mmio_addr;
3067
Francois Romieucecb5fd2011-04-01 10:21:07 +02003068 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3069 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3070 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003071 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003072 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003073 }
françois romieu065c27c2011-01-03 15:08:12 +00003074
Francois Romieucecb5fd2011-04-01 10:21:07 +02003075 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3076 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00003077 (RTL_R16(CPlusCmd) & ASF)) {
3078 return;
3079 }
3080
hayeswang01dc7fe2011-03-21 01:50:28 +00003081 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3082 tp->mac_version == RTL_GIGA_MAC_VER_33)
3083 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3084
françois romieu065c27c2011-01-03 15:08:12 +00003085 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3086 rtl_writephy(tp, 0x1f, 0x0000);
3087 rtl_writephy(tp, MII_BMCR, 0x0000);
3088
3089 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3090 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3091 return;
3092 }
3093
3094 r8168_phy_power_down(tp);
3095
3096 switch (tp->mac_version) {
3097 case RTL_GIGA_MAC_VER_25:
3098 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003099 case RTL_GIGA_MAC_VER_27:
3100 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003101 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003102 case RTL_GIGA_MAC_VER_32:
3103 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003104 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3105 break;
3106 }
3107}
3108
3109static void r8168_pll_power_up(struct rtl8169_private *tp)
3110{
3111 void __iomem *ioaddr = tp->mmio_addr;
3112
Francois Romieucecb5fd2011-04-01 10:21:07 +02003113 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3114 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3115 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003116 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003117 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003118 }
françois romieu065c27c2011-01-03 15:08:12 +00003119
3120 switch (tp->mac_version) {
3121 case RTL_GIGA_MAC_VER_25:
3122 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003123 case RTL_GIGA_MAC_VER_27:
3124 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003125 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003126 case RTL_GIGA_MAC_VER_32:
3127 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003128 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3129 break;
3130 }
3131
3132 r8168_phy_power_up(tp);
3133}
3134
3135static void rtl_pll_power_op(struct rtl8169_private *tp,
3136 void (*op)(struct rtl8169_private *))
3137{
3138 if (op)
3139 op(tp);
3140}
3141
3142static void rtl_pll_power_down(struct rtl8169_private *tp)
3143{
3144 rtl_pll_power_op(tp, tp->pll_power_ops.down);
3145}
3146
3147static void rtl_pll_power_up(struct rtl8169_private *tp)
3148{
3149 rtl_pll_power_op(tp, tp->pll_power_ops.up);
3150}
3151
3152static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3153{
3154 struct pll_power_ops *ops = &tp->pll_power_ops;
3155
3156 switch (tp->mac_version) {
3157 case RTL_GIGA_MAC_VER_07:
3158 case RTL_GIGA_MAC_VER_08:
3159 case RTL_GIGA_MAC_VER_09:
3160 case RTL_GIGA_MAC_VER_10:
3161 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003162 case RTL_GIGA_MAC_VER_29:
3163 case RTL_GIGA_MAC_VER_30:
françois romieu065c27c2011-01-03 15:08:12 +00003164 ops->down = r810x_pll_power_down;
3165 ops->up = r810x_pll_power_up;
3166 break;
3167
3168 case RTL_GIGA_MAC_VER_11:
3169 case RTL_GIGA_MAC_VER_12:
3170 case RTL_GIGA_MAC_VER_17:
3171 case RTL_GIGA_MAC_VER_18:
3172 case RTL_GIGA_MAC_VER_19:
3173 case RTL_GIGA_MAC_VER_20:
3174 case RTL_GIGA_MAC_VER_21:
3175 case RTL_GIGA_MAC_VER_22:
3176 case RTL_GIGA_MAC_VER_23:
3177 case RTL_GIGA_MAC_VER_24:
3178 case RTL_GIGA_MAC_VER_25:
3179 case RTL_GIGA_MAC_VER_26:
3180 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003181 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003182 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003183 case RTL_GIGA_MAC_VER_32:
3184 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003185 ops->down = r8168_pll_power_down;
3186 ops->up = r8168_pll_power_up;
3187 break;
3188
3189 default:
3190 ops->down = NULL;
3191 ops->up = NULL;
3192 break;
3193 }
3194}
3195
Francois Romieu6f43adc2011-04-29 15:05:51 +02003196static void rtl_hw_reset(struct rtl8169_private *tp)
3197{
3198 void __iomem *ioaddr = tp->mmio_addr;
3199 int i;
3200
3201 /* Soft reset the chip. */
3202 RTL_W8(ChipCmd, CmdReset);
3203
3204 /* Check that the chip has finished the reset. */
3205 for (i = 0; i < 100; i++) {
3206 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3207 break;
3208 msleep_interruptible(1);
3209 }
3210}
3211
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003212static int __devinit
3213rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
3214{
Francois Romieu0e485152007-02-20 00:00:26 +01003215 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
3216 const unsigned int region = cfg->region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 struct rtl8169_private *tp;
Francois Romieuccdffb92008-07-26 14:26:06 +02003218 struct mii_if_info *mii;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003219 struct net_device *dev;
3220 void __iomem *ioaddr;
Francois Romieu2b7b4312011-04-18 22:53:24 -07003221 int chipset, i;
Francois Romieu07d3f512007-02-21 22:40:46 +01003222 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003224 if (netif_msg_drv(&debug)) {
3225 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
3226 MODULENAME, RTL8169_VERSION);
3227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 dev = alloc_etherdev(sizeof (*tp));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003230 if (!dev) {
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003231 if (netif_msg_drv(&debug))
Jeff Garzik9b91cf92006-06-27 11:39:50 -04003232 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003233 rc = -ENOMEM;
3234 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 }
3236
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieu8b4ab282008-11-19 22:05:25 -08003238 dev->netdev_ops = &rtl8169_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 tp = netdev_priv(dev);
David Howellsc4028952006-11-22 14:57:56 +00003240 tp->dev = dev;
Ivan Vecera21e197f2008-04-17 22:48:41 +02003241 tp->pci_dev = pdev;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003242 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243
Francois Romieuccdffb92008-07-26 14:26:06 +02003244 mii = &tp->mii;
3245 mii->dev = dev;
3246 mii->mdio_read = rtl_mdio_read;
3247 mii->mdio_write = rtl_mdio_write;
3248 mii->phy_id_mask = 0x1f;
3249 mii->reg_num_mask = 0x1f;
3250 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
3251
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +00003252 /* disable ASPM completely as that cause random device stop working
3253 * problems as well as full system hangs for some PCIe devices users */
3254 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
3255 PCIE_LINK_STATE_CLKPM);
3256
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 /* enable device (incl. PCI PM wakeup and hotplug setup) */
3258 rc = pci_enable_device(pdev);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003259 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003260 netif_err(tp, probe, dev, "enable failure\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003261 goto err_out_free_dev_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 }
3263
françois romieu87aeec72010-04-26 11:42:06 +00003264 if (pci_set_mwi(pdev) < 0)
3265 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 /* make sure PCI base addr 1 is MMIO */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003268 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003269 netif_err(tp, probe, dev,
3270 "region #%d not an MMIO resource, aborting\n",
3271 region);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003273 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003275
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 /* check for weird/broken PCI region reporting */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003277 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003278 netif_err(tp, probe, dev,
3279 "Invalid PCI region size(s), aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003281 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 }
3283
3284 rc = pci_request_regions(pdev, MODULENAME);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003285 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003286 netif_err(tp, probe, dev, "could not request regions\n");
françois romieu87aeec72010-04-26 11:42:06 +00003287 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 }
3289
Hayes Wangd24e9aa2011-02-22 17:26:19 +08003290 tp->cp_cmd = RxChkSum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291
3292 if ((sizeof(dma_addr_t) > 4) &&
David S. Miller4300e8c2010-03-26 10:23:30 -07003293 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 tp->cp_cmd |= PCIDAC;
3295 dev->features |= NETIF_F_HIGHDMA;
3296 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07003297 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003299 netif_err(tp, probe, dev, "DMA configuration failed\n");
françois romieu87aeec72010-04-26 11:42:06 +00003300 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 }
3302 }
3303
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 /* ioremap MMIO region */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003305 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003306 if (!ioaddr) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003307 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 rc = -EIO;
françois romieu87aeec72010-04-26 11:42:06 +00003309 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 }
Francois Romieu6f43adc2011-04-29 15:05:51 +02003311 tp->mmio_addr = ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312
David S. Miller4300e8c2010-03-26 10:23:30 -07003313 tp->pcie_cap = pci_find_capability(pdev, PCI_CAP_ID_EXP);
3314 if (!tp->pcie_cap)
3315 netif_info(tp, probe, dev, "no PCI Express capability\n");
3316
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003317 RTL_W16(IntrMask, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318
Francois Romieu6f43adc2011-04-29 15:05:51 +02003319 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003321 RTL_W16(IntrStatus, 0xffff);
3322
françois romieuca52efd2009-07-24 12:34:19 +00003323 pci_set_master(pdev);
3324
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 /* Identify chip attached to board */
3326 rtl8169_get_mac_version(tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327
Francois Romieu7a8fc772011-03-01 17:18:33 +01003328 /*
3329 * Pretend we are using VLANs; This bypasses a nasty bug where
3330 * Interrupts stop flowing on high load on 8110SCd controllers.
3331 */
3332 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3333 tp->cp_cmd |= RxVlan;
3334
françois romieuc0e45c12011-01-03 15:08:04 +00003335 rtl_init_mdio_ops(tp);
françois romieu065c27c2011-01-03 15:08:12 +00003336 rtl_init_pll_power_ops(tp);
françois romieuc0e45c12011-01-03 15:08:04 +00003337
Jean Delvaref21b75e2009-05-26 20:54:48 -07003338 /* Use appropriate default if unknown */
3339 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003340 netif_notice(tp, probe, dev,
3341 "unknown MAC, using family default\n");
Jean Delvaref21b75e2009-05-26 20:54:48 -07003342 tp->mac_version = cfg->default_ver;
3343 }
3344
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 rtl8169_print_mac_version(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346
Roel Kluincee60c32008-04-17 22:35:54 +02003347 for (i = 0; i < ARRAY_SIZE(rtl_chip_info); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 if (tp->mac_version == rtl_chip_info[i].mac_version)
3349 break;
3350 }
Roel Kluincee60c32008-04-17 22:35:54 +02003351 if (i == ARRAY_SIZE(rtl_chip_info)) {
Jean Delvaref21b75e2009-05-26 20:54:48 -07003352 dev_err(&pdev->dev,
3353 "driver bug, MAC version not found in rtl_chip_info\n");
françois romieu87aeec72010-04-26 11:42:06 +00003354 goto err_out_msi_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 }
Francois Romieu2b7b4312011-04-18 22:53:24 -07003356 chipset = i;
3357 tp->txd_version = rtl_chip_info[chipset].txd_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358
Francois Romieu5d06a992006-02-23 00:47:58 +01003359 RTL_W8(Cfg9346, Cfg9346_Unlock);
3360 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
3361 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
Bruno Prémont20037fa2008-10-08 17:05:03 -07003362 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
3363 tp->features |= RTL_FEATURE_WOL;
3364 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
3365 tp->features |= RTL_FEATURE_WOL;
Francois Romieufbac58f2007-10-04 22:51:38 +02003366 tp->features |= rtl_try_msi(pdev, ioaddr, cfg);
Francois Romieu5d06a992006-02-23 00:47:58 +01003367 RTL_W8(Cfg9346, Cfg9346_Lock);
3368
Francois Romieu66ec5d42007-11-06 22:56:10 +01003369 if ((tp->mac_version <= RTL_GIGA_MAC_VER_06) &&
3370 (RTL_R8(PHYstatus) & TBI_Enable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 tp->set_speed = rtl8169_set_speed_tbi;
3372 tp->get_settings = rtl8169_gset_tbi;
3373 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
3374 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
3375 tp->link_ok = rtl8169_tbi_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003376 tp->do_ioctl = rtl_tbi_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 } else {
3378 tp->set_speed = rtl8169_set_speed_xmii;
3379 tp->get_settings = rtl8169_gset_xmii;
3380 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
3381 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
3382 tp->link_ok = rtl8169_xmii_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003383 tp->do_ioctl = rtl_xmii_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 }
3385
Francois Romieudf58ef52008-10-09 14:35:58 -07003386 spin_lock_init(&tp->lock);
3387
Ivan Vecera7bf6bf42008-09-23 22:46:29 +00003388 /* Get MAC address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 for (i = 0; i < MAC_ADDR_LEN; i++)
3390 dev->dev_addr[i] = RTL_R8(MAC0 + i);
John W. Linville6d6525b2005-09-12 10:48:57 -04003391 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
3395 dev->irq = pdev->irq;
3396 dev->base_addr = (unsigned long) ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003398 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399
Michał Mirosław350fb322011-04-08 06:35:56 +00003400 /* don't enable SG, IP_CSUM and TSO by default - it might not work
3401 * properly for all devices */
3402 dev->features |= NETIF_F_RXCSUM |
3403 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3404
3405 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3406 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3407 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3408 NETIF_F_HIGHDMA;
3409
3410 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3411 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
3412 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413
3414 tp->intr_mask = 0xffff;
Francois Romieu0e485152007-02-20 00:00:26 +01003415 tp->hw_start = cfg->hw_start;
3416 tp->intr_event = cfg->intr_event;
3417 tp->napi_event = cfg->napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418
Francois Romieu2efa53f2007-03-09 00:00:05 +01003419 init_timer(&tp->timer);
3420 tp->timer.data = (unsigned long) dev;
3421 tp->timer.function = rtl8169_phy_timer;
3422
François Romieu953a12c2011-04-24 17:38:48 +02003423 tp->fw = RTL_FIRMWARE_UNKNOWN;
3424
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 rc = register_netdev(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003426 if (rc < 0)
françois romieu87aeec72010-04-26 11:42:06 +00003427 goto err_out_msi_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428
3429 pci_set_drvdata(pdev, dev);
3430
Joe Perchesbf82c182010-02-09 11:49:50 +00003431 netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
Francois Romieu2b7b4312011-04-18 22:53:24 -07003432 rtl_chip_info[chipset].name, dev->base_addr, dev->dev_addr,
Joe Perchesbf82c182010-02-09 11:49:50 +00003433 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434
Francois Romieucecb5fd2011-04-01 10:21:07 +02003435 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3436 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3437 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00003438 rtl8168_driver_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003439 }
françois romieub646d902011-01-03 15:08:21 +00003440
Bruno Prémont8b76ab32008-10-08 17:06:25 -07003441 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442
Alan Sternf3ec4f82010-06-08 15:23:51 -04003443 if (pci_dev_run_wake(pdev))
3444 pm_runtime_put_noidle(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003445
Ivan Vecera0d672e92011-02-15 02:08:39 +00003446 netif_carrier_off(dev);
3447
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003448out:
3449 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450
françois romieu87aeec72010-04-26 11:42:06 +00003451err_out_msi_4:
Francois Romieufbac58f2007-10-04 22:51:38 +02003452 rtl_disable_msi(pdev, tp);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003453 iounmap(ioaddr);
françois romieu87aeec72010-04-26 11:42:06 +00003454err_out_free_res_3:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003455 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003456err_out_mwi_2:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003457 pci_clear_mwi(pdev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003458 pci_disable_device(pdev);
3459err_out_free_dev_1:
3460 free_netdev(dev);
3461 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462}
3463
Francois Romieu07d3f512007-02-21 22:40:46 +01003464static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465{
3466 struct net_device *dev = pci_get_drvdata(pdev);
3467 struct rtl8169_private *tp = netdev_priv(dev);
3468
Francois Romieucecb5fd2011-04-01 10:21:07 +02003469 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3470 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3471 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00003472 rtl8168_driver_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003473 }
françois romieub646d902011-01-03 15:08:21 +00003474
Tejun Heo23f333a2010-12-12 16:45:14 +01003475 cancel_delayed_work_sync(&tp->task);
Francois Romieueb2a0212007-02-15 23:37:21 +01003476
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 unregister_netdev(dev);
Ivan Veceracc098dc2009-11-29 23:12:52 -08003478
François Romieu953a12c2011-04-24 17:38:48 +02003479 rtl_release_firmware(tp);
3480
Alan Sternf3ec4f82010-06-08 15:23:51 -04003481 if (pci_dev_run_wake(pdev))
3482 pm_runtime_get_noresume(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003483
Ivan Veceracc098dc2009-11-29 23:12:52 -08003484 /* restore original MAC address */
3485 rtl_rar_set(tp, dev->perm_addr);
3486
Francois Romieufbac58f2007-10-04 22:51:38 +02003487 rtl_disable_msi(pdev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 rtl8169_release_board(pdev, dev, tp->mmio_addr);
3489 pci_set_drvdata(pdev, NULL);
3490}
3491
François Romieu953a12c2011-04-24 17:38:48 +02003492static void rtl_request_firmware(struct rtl8169_private *tp)
3493{
3494 int i;
3495
3496 /* Return early if the firmware is already loaded / cached. */
3497 if (!IS_ERR(tp->fw))
3498 goto out;
3499
3500 for (i = 0; i < ARRAY_SIZE(rtl_firmware_infos); i++) {
3501 const struct rtl_firmware_info *info = rtl_firmware_infos + i;
3502
3503 if (info->mac_version == tp->mac_version) {
3504 const char *name = info->fw_name;
3505 int rc;
3506
3507 rc = request_firmware(&tp->fw, name, &tp->pci_dev->dev);
3508 if (rc < 0) {
3509 netif_warn(tp, ifup, tp->dev, "unable to load "
3510 "firmware patch %s (%d)\n", name, rc);
3511 goto out_disable_request_firmware;
3512 }
3513 goto out;
3514 }
3515 }
3516
3517out_disable_request_firmware:
3518 tp->fw = NULL;
3519out:
3520 return;
3521}
3522
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523static int rtl8169_open(struct net_device *dev)
3524{
3525 struct rtl8169_private *tp = netdev_priv(dev);
françois romieueee3a962011-01-08 02:17:26 +00003526 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu99f252b2007-04-02 22:59:59 +02003528 int retval = -ENOMEM;
3529
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003530 pm_runtime_get_sync(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531
Neil Hormanc0cd8842010-03-29 13:16:02 -07003532 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 * Rx and Tx desscriptors needs 256 bytes alignment.
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003534 * dma_alloc_coherent provides more.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 */
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003536 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
3537 &tp->TxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 if (!tp->TxDescArray)
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003539 goto err_pm_runtime_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003541 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
3542 &tp->RxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 if (!tp->RxDescArray)
Francois Romieu99f252b2007-04-02 22:59:59 +02003544 goto err_free_tx_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545
3546 retval = rtl8169_init_ring(dev);
3547 if (retval < 0)
Francois Romieu99f252b2007-04-02 22:59:59 +02003548 goto err_free_rx_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549
David Howellsc4028952006-11-22 14:57:56 +00003550 INIT_DELAYED_WORK(&tp->task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551
Francois Romieu99f252b2007-04-02 22:59:59 +02003552 smp_mb();
3553
François Romieu953a12c2011-04-24 17:38:48 +02003554 rtl_request_firmware(tp);
3555
Francois Romieufbac58f2007-10-04 22:51:38 +02003556 retval = request_irq(dev->irq, rtl8169_interrupt,
3557 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
Francois Romieu99f252b2007-04-02 22:59:59 +02003558 dev->name, dev);
3559 if (retval < 0)
François Romieu953a12c2011-04-24 17:38:48 +02003560 goto err_release_fw_2;
Francois Romieu99f252b2007-04-02 22:59:59 +02003561
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003562 napi_enable(&tp->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003563
françois romieueee3a962011-01-08 02:17:26 +00003564 rtl8169_init_phy(dev, tp);
3565
Michał Mirosław350fb322011-04-08 06:35:56 +00003566 rtl8169_set_features(dev, dev->features);
françois romieueee3a962011-01-08 02:17:26 +00003567
françois romieu065c27c2011-01-03 15:08:12 +00003568 rtl_pll_power_up(tp);
3569
Francois Romieu07ce4062007-02-23 23:36:39 +01003570 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003572 tp->saved_wolopts = 0;
3573 pm_runtime_put_noidle(&pdev->dev);
3574
françois romieueee3a962011-01-08 02:17:26 +00003575 rtl8169_check_link_status(dev, tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576out:
3577 return retval;
3578
François Romieu953a12c2011-04-24 17:38:48 +02003579err_release_fw_2:
3580 rtl_release_firmware(tp);
Francois Romieu99f252b2007-04-02 22:59:59 +02003581 rtl8169_rx_clear(tp);
3582err_free_rx_1:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003583 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
3584 tp->RxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003585 tp->RxDescArray = NULL;
Francois Romieu99f252b2007-04-02 22:59:59 +02003586err_free_tx_0:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003587 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
3588 tp->TxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003589 tp->TxDescArray = NULL;
3590err_pm_runtime_put:
3591 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 goto out;
3593}
3594
françois romieue6de30d2011-01-03 15:08:37 +00003595static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596{
françois romieue6de30d2011-01-03 15:08:37 +00003597 void __iomem *ioaddr = tp->mmio_addr;
3598
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 /* Disable interrupts */
3600 rtl8169_irq_mask_and_ack(ioaddr);
3601
Hayes Wang5d2e1952011-02-22 17:26:22 +08003602 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00003603 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3604 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieue6de30d2011-01-03 15:08:37 +00003605 while (RTL_R8(TxPoll) & NPQ)
3606 udelay(20);
3607
3608 }
3609
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 /* Reset the chipset */
3611 RTL_W8(ChipCmd, CmdReset);
3612
3613 /* PCI commit */
3614 RTL_R8(ChipCmd);
3615}
3616
Francois Romieu7f796d82007-06-11 23:04:41 +02003617static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01003618{
3619 void __iomem *ioaddr = tp->mmio_addr;
3620 u32 cfg = rtl8169_rx_config;
3621
Francois Romieu2b7b4312011-04-18 22:53:24 -07003622 cfg |= (RTL_R32(RxConfig) & RTL_RX_CONFIG_MASK);
Francois Romieu9cb427b2006-11-02 00:10:16 +01003623 RTL_W32(RxConfig, cfg);
3624
3625 /* Set DMA burst size and Interframe Gap Time */
3626 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
3627 (InterFrameGap << TxInterFrameGapShift));
3628}
3629
Francois Romieu07ce4062007-02-23 23:36:39 +01003630static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631{
3632 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633
Francois Romieu6f43adc2011-04-29 15:05:51 +02003634 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635
Francois Romieu07ce4062007-02-23 23:36:39 +01003636 tp->hw_start(dev);
3637
Francois Romieu07ce4062007-02-23 23:36:39 +01003638 netif_start_queue(dev);
3639}
3640
Francois Romieu7f796d82007-06-11 23:04:41 +02003641static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
3642 void __iomem *ioaddr)
3643{
3644 /*
3645 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
3646 * register to be written before TxDescAddrLow to work.
3647 * Switching from MMIO to I/O access fixes the issue as well.
3648 */
3649 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07003650 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02003651 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07003652 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02003653}
3654
3655static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
3656{
3657 u16 cmd;
3658
3659 cmd = RTL_R16(CPlusCmd);
3660 RTL_W16(CPlusCmd, cmd);
3661 return cmd;
3662}
3663
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07003664static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d82007-06-11 23:04:41 +02003665{
3666 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e82009-10-26 10:52:37 +00003667 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d82007-06-11 23:04:41 +02003668}
3669
Francois Romieu6dccd162007-02-13 23:38:05 +01003670static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
3671{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003672 static const struct {
Francois Romieu6dccd162007-02-13 23:38:05 +01003673 u32 mac_version;
3674 u32 clk;
3675 u32 val;
3676 } cfg2_info [] = {
3677 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
3678 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
3679 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
3680 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
3681 }, *p = cfg2_info;
3682 unsigned int i;
3683 u32 clk;
3684
3685 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01003686 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01003687 if ((p->mac_version == mac_version) && (p->clk == clk)) {
3688 RTL_W32(0x7c, p->val);
3689 break;
3690 }
3691 }
3692}
3693
Francois Romieu07ce4062007-02-23 23:36:39 +01003694static void rtl_hw_start_8169(struct net_device *dev)
3695{
3696 struct rtl8169_private *tp = netdev_priv(dev);
3697 void __iomem *ioaddr = tp->mmio_addr;
3698 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01003699
Francois Romieu9cb427b2006-11-02 00:10:16 +01003700 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
3701 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
3702 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
3703 }
3704
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02003706 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
3707 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
3708 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
3709 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01003710 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3711
françois romieuf0298f82011-01-03 15:07:42 +00003712 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713
Eric Dumazet6f0333b2010-10-11 11:17:47 +00003714 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715
Francois Romieucecb5fd2011-04-01 10:21:07 +02003716 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
3717 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
3718 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
3719 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02003720 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721
Francois Romieu7f796d82007-06-11 23:04:41 +02003722 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02003723
Francois Romieucecb5fd2011-04-01 10:21:07 +02003724 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
3725 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02003726 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02003728 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 }
3730
Francois Romieubcf0bf92006-07-26 23:14:13 +02003731 RTL_W16(CPlusCmd, tp->cp_cmd);
3732
Francois Romieu6dccd162007-02-13 23:38:05 +01003733 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
3734
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 /*
3736 * Undocumented corner. Supposedly:
3737 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
3738 */
3739 RTL_W16(IntrMitigate, 0x0000);
3740
Francois Romieu7f796d82007-06-11 23:04:41 +02003741 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01003742
Francois Romieucecb5fd2011-04-01 10:21:07 +02003743 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
3744 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
3745 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
3746 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02003747 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3748 rtl_set_rx_tx_config_registers(tp);
3749 }
3750
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02003752
3753 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
3754 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755
3756 RTL_W32(RxMissed, 0);
3757
Francois Romieu07ce4062007-02-23 23:36:39 +01003758 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759
3760 /* no early-rx interrupts */
3761 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01003762
3763 /* Enable all known interrupts by setting the interrupt mask. */
Francois Romieu0e485152007-02-20 00:00:26 +01003764 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01003765}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766
Francois Romieu9c14cea2008-07-05 00:21:15 +02003767static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
Francois Romieu458a9f62008-08-02 15:50:02 +02003768{
Francois Romieu9c14cea2008-07-05 00:21:15 +02003769 struct net_device *dev = pci_get_drvdata(pdev);
3770 struct rtl8169_private *tp = netdev_priv(dev);
3771 int cap = tp->pcie_cap;
Francois Romieu458a9f62008-08-02 15:50:02 +02003772
Francois Romieu9c14cea2008-07-05 00:21:15 +02003773 if (cap) {
3774 u16 ctl;
3775
3776 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
3777 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
3778 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
3779 }
Francois Romieu458a9f62008-08-02 15:50:02 +02003780}
3781
françois romieu650e8d52011-01-03 15:08:29 +00003782static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02003783{
3784 u32 csi;
3785
3786 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
françois romieu650e8d52011-01-03 15:08:29 +00003787 rtl_csi_write(ioaddr, 0x070c, csi | bits);
3788}
3789
françois romieue6de30d2011-01-03 15:08:37 +00003790static void rtl_csi_access_enable_1(void __iomem *ioaddr)
3791{
3792 rtl_csi_access_enable(ioaddr, 0x17000000);
3793}
3794
françois romieu650e8d52011-01-03 15:08:29 +00003795static void rtl_csi_access_enable_2(void __iomem *ioaddr)
3796{
3797 rtl_csi_access_enable(ioaddr, 0x27000000);
Francois Romieudacf8152008-08-02 20:44:13 +02003798}
3799
3800struct ephy_info {
3801 unsigned int offset;
3802 u16 mask;
3803 u16 bits;
3804};
3805
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003806static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02003807{
3808 u16 w;
3809
3810 while (len-- > 0) {
3811 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
3812 rtl_ephy_write(ioaddr, e->offset, w);
3813 e++;
3814 }
3815}
3816
Francois Romieub726e492008-06-28 12:22:59 +02003817static void rtl_disable_clock_request(struct pci_dev *pdev)
3818{
3819 struct net_device *dev = pci_get_drvdata(pdev);
3820 struct rtl8169_private *tp = netdev_priv(dev);
3821 int cap = tp->pcie_cap;
3822
3823 if (cap) {
3824 u16 ctl;
3825
3826 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
3827 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
3828 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
3829 }
3830}
3831
françois romieue6de30d2011-01-03 15:08:37 +00003832static void rtl_enable_clock_request(struct pci_dev *pdev)
3833{
3834 struct net_device *dev = pci_get_drvdata(pdev);
3835 struct rtl8169_private *tp = netdev_priv(dev);
3836 int cap = tp->pcie_cap;
3837
3838 if (cap) {
3839 u16 ctl;
3840
3841 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
3842 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
3843 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
3844 }
3845}
3846
Francois Romieub726e492008-06-28 12:22:59 +02003847#define R8168_CPCMD_QUIRK_MASK (\
3848 EnableBist | \
3849 Mac_dbgo_oe | \
3850 Force_half_dup | \
3851 Force_rxflow_en | \
3852 Force_txflow_en | \
3853 Cxpl_dbg_sel | \
3854 ASF | \
3855 PktCntrDisable | \
3856 Mac_dbgo_sel)
3857
Francois Romieu219a1e92008-06-28 11:58:39 +02003858static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
3859{
Francois Romieub726e492008-06-28 12:22:59 +02003860 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3861
3862 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3863
Francois Romieu2e68ae42008-06-28 12:00:55 +02003864 rtl_tx_performance_tweak(pdev,
3865 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02003866}
3867
3868static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
3869{
3870 rtl_hw_start_8168bb(ioaddr, pdev);
Francois Romieub726e492008-06-28 12:22:59 +02003871
françois romieuf0298f82011-01-03 15:07:42 +00003872 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02003873
3874 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02003875}
3876
3877static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
3878{
Francois Romieub726e492008-06-28 12:22:59 +02003879 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
3880
3881 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3882
Francois Romieu219a1e92008-06-28 11:58:39 +02003883 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02003884
3885 rtl_disable_clock_request(pdev);
3886
3887 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02003888}
3889
Francois Romieuef3386f2008-06-29 12:24:30 +02003890static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
Francois Romieu219a1e92008-06-28 11:58:39 +02003891{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003892 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02003893 { 0x01, 0, 0x0001 },
3894 { 0x02, 0x0800, 0x1000 },
3895 { 0x03, 0, 0x0042 },
3896 { 0x06, 0x0080, 0x0000 },
3897 { 0x07, 0, 0x2000 }
3898 };
3899
françois romieu650e8d52011-01-03 15:08:29 +00003900 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02003901
3902 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
3903
Francois Romieu219a1e92008-06-28 11:58:39 +02003904 __rtl_hw_start_8168cp(ioaddr, pdev);
3905}
3906
Francois Romieuef3386f2008-06-29 12:24:30 +02003907static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
3908{
françois romieu650e8d52011-01-03 15:08:29 +00003909 rtl_csi_access_enable_2(ioaddr);
Francois Romieuef3386f2008-06-29 12:24:30 +02003910
3911 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3912
3913 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3914
3915 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3916}
3917
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003918static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
3919{
françois romieu650e8d52011-01-03 15:08:29 +00003920 rtl_csi_access_enable_2(ioaddr);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003921
3922 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3923
3924 /* Magic. */
3925 RTL_W8(DBG_REG, 0x20);
3926
françois romieuf0298f82011-01-03 15:07:42 +00003927 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02003928
3929 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3930
3931 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3932}
3933
Francois Romieu219a1e92008-06-28 11:58:39 +02003934static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
3935{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003936 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02003937 { 0x02, 0x0800, 0x1000 },
3938 { 0x03, 0, 0x0002 },
3939 { 0x06, 0x0080, 0x0000 }
3940 };
3941
françois romieu650e8d52011-01-03 15:08:29 +00003942 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02003943
3944 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
3945
3946 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
3947
Francois Romieu219a1e92008-06-28 11:58:39 +02003948 __rtl_hw_start_8168cp(ioaddr, pdev);
3949}
3950
3951static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
3952{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003953 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02003954 { 0x01, 0, 0x0001 },
3955 { 0x03, 0x0400, 0x0220 }
3956 };
3957
françois romieu650e8d52011-01-03 15:08:29 +00003958 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02003959
3960 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
3961
Francois Romieu219a1e92008-06-28 11:58:39 +02003962 __rtl_hw_start_8168cp(ioaddr, pdev);
3963}
3964
Francois Romieu197ff762008-06-28 13:16:02 +02003965static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
3966{
3967 rtl_hw_start_8168c_2(ioaddr, pdev);
3968}
3969
Francois Romieu6fb07052008-06-29 11:54:28 +02003970static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
3971{
françois romieu650e8d52011-01-03 15:08:29 +00003972 rtl_csi_access_enable_2(ioaddr);
Francois Romieu6fb07052008-06-29 11:54:28 +02003973
3974 __rtl_hw_start_8168cp(ioaddr, pdev);
3975}
3976
Francois Romieu5b538df2008-07-20 16:22:45 +02003977static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
3978{
françois romieu650e8d52011-01-03 15:08:29 +00003979 rtl_csi_access_enable_2(ioaddr);
Francois Romieu5b538df2008-07-20 16:22:45 +02003980
3981 rtl_disable_clock_request(pdev);
3982
françois romieuf0298f82011-01-03 15:07:42 +00003983 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02003984
3985 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3986
3987 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3988}
3989
hayeswang4804b3b2011-03-21 01:50:29 +00003990static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
3991{
3992 rtl_csi_access_enable_1(ioaddr);
3993
3994 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
3995
3996 RTL_W8(MaxTxPacketSize, TxPacketMax);
3997
3998 rtl_disable_clock_request(pdev);
3999}
4000
françois romieue6de30d2011-01-03 15:08:37 +00004001static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4002{
4003 static const struct ephy_info e_info_8168d_4[] = {
4004 { 0x0b, ~0, 0x48 },
4005 { 0x19, 0x20, 0x50 },
4006 { 0x0c, ~0, 0x20 }
4007 };
4008 int i;
4009
4010 rtl_csi_access_enable_1(ioaddr);
4011
4012 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4013
4014 RTL_W8(MaxTxPacketSize, TxPacketMax);
4015
4016 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4017 const struct ephy_info *e = e_info_8168d_4 + i;
4018 u16 w;
4019
4020 w = rtl_ephy_read(ioaddr, e->offset);
4021 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4022 }
4023
4024 rtl_enable_clock_request(pdev);
4025}
4026
hayeswang01dc7fe2011-03-21 01:50:28 +00004027static void rtl_hw_start_8168e(void __iomem *ioaddr, struct pci_dev *pdev)
4028{
4029 static const struct ephy_info e_info_8168e[] = {
4030 { 0x00, 0x0200, 0x0100 },
4031 { 0x00, 0x0000, 0x0004 },
4032 { 0x06, 0x0002, 0x0001 },
4033 { 0x06, 0x0000, 0x0030 },
4034 { 0x07, 0x0000, 0x2000 },
4035 { 0x00, 0x0000, 0x0020 },
4036 { 0x03, 0x5800, 0x2000 },
4037 { 0x03, 0x0000, 0x0001 },
4038 { 0x01, 0x0800, 0x1000 },
4039 { 0x07, 0x0000, 0x4000 },
4040 { 0x1e, 0x0000, 0x2000 },
4041 { 0x19, 0xffff, 0xfe6c },
4042 { 0x0a, 0x0000, 0x0040 }
4043 };
4044
4045 rtl_csi_access_enable_2(ioaddr);
4046
4047 rtl_ephy_init(ioaddr, e_info_8168e, ARRAY_SIZE(e_info_8168e));
4048
4049 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4050
4051 RTL_W8(MaxTxPacketSize, TxPacketMax);
4052
4053 rtl_disable_clock_request(pdev);
4054
4055 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02004056 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4057 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004058
Francois Romieucecb5fd2011-04-01 10:21:07 +02004059 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004060}
4061
Francois Romieu07ce4062007-02-23 23:36:39 +01004062static void rtl_hw_start_8168(struct net_device *dev)
4063{
Francois Romieu2dd99532007-06-11 23:22:52 +02004064 struct rtl8169_private *tp = netdev_priv(dev);
4065 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01004066 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu2dd99532007-06-11 23:22:52 +02004067
4068 RTL_W8(Cfg9346, Cfg9346_Unlock);
4069
françois romieuf0298f82011-01-03 15:07:42 +00004070 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004071
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004072 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004073
Francois Romieu0e485152007-02-20 00:00:26 +01004074 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004075
4076 RTL_W16(CPlusCmd, tp->cp_cmd);
4077
Francois Romieu0e485152007-02-20 00:00:26 +01004078 RTL_W16(IntrMitigate, 0x5151);
4079
4080 /* Work around for RxFIFO overflow. */
Ivan Vecerab5ba6d12011-01-27 12:24:11 +01004081 if (tp->mac_version == RTL_GIGA_MAC_VER_11 ||
4082 tp->mac_version == RTL_GIGA_MAC_VER_22) {
Francois Romieu0e485152007-02-20 00:00:26 +01004083 tp->intr_event |= RxFIFOOver | PCSTimeout;
4084 tp->intr_event &= ~RxOverflow;
4085 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004086
4087 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4088
Francois Romieub8363902008-06-01 12:31:57 +02004089 rtl_set_rx_mode(dev);
4090
4091 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4092 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004093
4094 RTL_R8(IntrMask);
4095
Francois Romieu219a1e92008-06-28 11:58:39 +02004096 switch (tp->mac_version) {
4097 case RTL_GIGA_MAC_VER_11:
4098 rtl_hw_start_8168bb(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004099 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004100
4101 case RTL_GIGA_MAC_VER_12:
4102 case RTL_GIGA_MAC_VER_17:
4103 rtl_hw_start_8168bef(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004104 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004105
4106 case RTL_GIGA_MAC_VER_18:
Francois Romieuef3386f2008-06-29 12:24:30 +02004107 rtl_hw_start_8168cp_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004108 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004109
4110 case RTL_GIGA_MAC_VER_19:
4111 rtl_hw_start_8168c_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004112 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004113
4114 case RTL_GIGA_MAC_VER_20:
4115 rtl_hw_start_8168c_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004116 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004117
Francois Romieu197ff762008-06-28 13:16:02 +02004118 case RTL_GIGA_MAC_VER_21:
4119 rtl_hw_start_8168c_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004120 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004121
Francois Romieu6fb07052008-06-29 11:54:28 +02004122 case RTL_GIGA_MAC_VER_22:
4123 rtl_hw_start_8168c_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004124 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004125
Francois Romieuef3386f2008-06-29 12:24:30 +02004126 case RTL_GIGA_MAC_VER_23:
4127 rtl_hw_start_8168cp_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004128 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004129
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004130 case RTL_GIGA_MAC_VER_24:
4131 rtl_hw_start_8168cp_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004132 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004133
Francois Romieu5b538df2008-07-20 16:22:45 +02004134 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00004135 case RTL_GIGA_MAC_VER_26:
4136 case RTL_GIGA_MAC_VER_27:
Francois Romieu5b538df2008-07-20 16:22:45 +02004137 rtl_hw_start_8168d(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004138 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004139
françois romieue6de30d2011-01-03 15:08:37 +00004140 case RTL_GIGA_MAC_VER_28:
4141 rtl_hw_start_8168d_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004142 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004143
hayeswang4804b3b2011-03-21 01:50:29 +00004144 case RTL_GIGA_MAC_VER_31:
4145 rtl_hw_start_8168dp(ioaddr, pdev);
4146 break;
4147
hayeswang01dc7fe2011-03-21 01:50:28 +00004148 case RTL_GIGA_MAC_VER_32:
4149 case RTL_GIGA_MAC_VER_33:
4150 rtl_hw_start_8168e(ioaddr, pdev);
4151 break;
françois romieue6de30d2011-01-03 15:08:37 +00004152
Francois Romieu219a1e92008-06-28 11:58:39 +02004153 default:
4154 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
4155 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00004156 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004157 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004158
Francois Romieu0e485152007-02-20 00:00:26 +01004159 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4160
Francois Romieub8363902008-06-01 12:31:57 +02004161 RTL_W8(Cfg9346, Cfg9346_Lock);
4162
Francois Romieu2dd99532007-06-11 23:22:52 +02004163 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004164
Francois Romieu0e485152007-02-20 00:00:26 +01004165 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01004166}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167
Francois Romieu2857ffb2008-08-02 21:08:49 +02004168#define R810X_CPCMD_QUIRK_MASK (\
4169 EnableBist | \
4170 Mac_dbgo_oe | \
4171 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00004172 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02004173 Force_txflow_en | \
4174 Cxpl_dbg_sel | \
4175 ASF | \
4176 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004177 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004178
4179static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4180{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004181 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004182 { 0x01, 0, 0x6e65 },
4183 { 0x02, 0, 0x091f },
4184 { 0x03, 0, 0xc2f9 },
4185 { 0x06, 0, 0xafb5 },
4186 { 0x07, 0, 0x0e00 },
4187 { 0x19, 0, 0xec80 },
4188 { 0x01, 0, 0x2e65 },
4189 { 0x01, 0, 0x6e65 }
4190 };
4191 u8 cfg1;
4192
françois romieu650e8d52011-01-03 15:08:29 +00004193 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004194
4195 RTL_W8(DBG_REG, FIX_NAK_1);
4196
4197 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4198
4199 RTL_W8(Config1,
4200 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
4201 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4202
4203 cfg1 = RTL_R8(Config1);
4204 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
4205 RTL_W8(Config1, cfg1 & ~LEDS0);
4206
Francois Romieu2857ffb2008-08-02 21:08:49 +02004207 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
4208}
4209
4210static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4211{
françois romieu650e8d52011-01-03 15:08:29 +00004212 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004213
4214 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4215
4216 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
4217 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004218}
4219
4220static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
4221{
4222 rtl_hw_start_8102e_2(ioaddr, pdev);
4223
4224 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
4225}
4226
Hayes Wang5a5e4442011-02-22 17:26:21 +08004227static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4228{
4229 static const struct ephy_info e_info_8105e_1[] = {
4230 { 0x07, 0, 0x4000 },
4231 { 0x19, 0, 0x0200 },
4232 { 0x19, 0, 0x0020 },
4233 { 0x1e, 0, 0x2000 },
4234 { 0x03, 0, 0x0001 },
4235 { 0x19, 0, 0x0100 },
4236 { 0x19, 0, 0x0004 },
4237 { 0x0a, 0, 0x0020 }
4238 };
4239
Francois Romieucecb5fd2011-04-01 10:21:07 +02004240 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004241 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
4242
Francois Romieucecb5fd2011-04-01 10:21:07 +02004243 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004244 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
4245
4246 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
4247 RTL_W8(DLLPR, RTL_R8(DLLPR) | PM_SWITCH);
4248
4249 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
4250}
4251
4252static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4253{
4254 rtl_hw_start_8105e_1(ioaddr, pdev);
4255 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
4256}
4257
Francois Romieu07ce4062007-02-23 23:36:39 +01004258static void rtl_hw_start_8101(struct net_device *dev)
4259{
Francois Romieucdf1a602007-06-11 23:29:50 +02004260 struct rtl8169_private *tp = netdev_priv(dev);
4261 void __iomem *ioaddr = tp->mmio_addr;
4262 struct pci_dev *pdev = tp->pci_dev;
4263
Francois Romieucecb5fd2011-04-01 10:21:07 +02004264 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
4265 tp->mac_version == RTL_GIGA_MAC_VER_16) {
Francois Romieu9c14cea2008-07-05 00:21:15 +02004266 int cap = tp->pcie_cap;
4267
4268 if (cap) {
4269 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
4270 PCI_EXP_DEVCTL_NOSNOOP_EN);
4271 }
Francois Romieucdf1a602007-06-11 23:29:50 +02004272 }
4273
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004274 RTL_W8(Cfg9346, Cfg9346_Unlock);
4275
Francois Romieu2857ffb2008-08-02 21:08:49 +02004276 switch (tp->mac_version) {
4277 case RTL_GIGA_MAC_VER_07:
4278 rtl_hw_start_8102e_1(ioaddr, pdev);
4279 break;
4280
4281 case RTL_GIGA_MAC_VER_08:
4282 rtl_hw_start_8102e_3(ioaddr, pdev);
4283 break;
4284
4285 case RTL_GIGA_MAC_VER_09:
4286 rtl_hw_start_8102e_2(ioaddr, pdev);
4287 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004288
4289 case RTL_GIGA_MAC_VER_29:
4290 rtl_hw_start_8105e_1(ioaddr, pdev);
4291 break;
4292 case RTL_GIGA_MAC_VER_30:
4293 rtl_hw_start_8105e_2(ioaddr, pdev);
4294 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02004295 }
4296
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004297 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02004298
françois romieuf0298f82011-01-03 15:07:42 +00004299 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02004300
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004301 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02004302
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004303 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02004304 RTL_W16(CPlusCmd, tp->cp_cmd);
4305
4306 RTL_W16(IntrMitigate, 0x0000);
4307
4308 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4309
4310 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4311 rtl_set_rx_tx_config_registers(tp);
4312
Francois Romieucdf1a602007-06-11 23:29:50 +02004313 RTL_R8(IntrMask);
4314
Francois Romieucdf1a602007-06-11 23:29:50 +02004315 rtl_set_rx_mode(dev);
4316
4317 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004318
Francois Romieu0e485152007-02-20 00:00:26 +01004319 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320}
4321
4322static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
4323{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 if (new_mtu < ETH_ZLEN || new_mtu > SafeMtu)
4325 return -EINVAL;
4326
4327 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00004328 netdev_update_features(dev);
4329
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00004330 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331}
4332
4333static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
4334{
Al Viro95e09182007-12-22 18:55:39 +00004335 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
4337}
4338
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004339static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
4340 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004342 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004343 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004344
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004345 kfree(*data_buff);
4346 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 rtl8169_make_unusable_by_asic(desc);
4348}
4349
4350static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
4351{
4352 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
4353
4354 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
4355}
4356
4357static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
4358 u32 rx_buf_sz)
4359{
4360 desc->addr = cpu_to_le64(mapping);
4361 wmb();
4362 rtl8169_mark_to_asic(desc, rx_buf_sz);
4363}
4364
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004365static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004367 return (void *)ALIGN((long)data, 16);
4368}
4369
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004370static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
4371 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004372{
4373 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004375 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004376 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004377 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004379 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
4380 if (!data)
4381 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01004382
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004383 if (rtl8169_align(data) != data) {
4384 kfree(data);
4385 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
4386 if (!data)
4387 return NULL;
4388 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004389
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004390 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004391 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004392 if (unlikely(dma_mapping_error(d, mapping))) {
4393 if (net_ratelimit())
4394 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004395 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397
4398 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004399 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004400
4401err_out:
4402 kfree(data);
4403 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404}
4405
4406static void rtl8169_rx_clear(struct rtl8169_private *tp)
4407{
Francois Romieu07d3f512007-02-21 22:40:46 +01004408 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409
4410 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004411 if (tp->Rx_databuff[i]) {
4412 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 tp->RxDescArray + i);
4414 }
4415 }
4416}
4417
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004418static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004420 desc->opts1 |= cpu_to_le32(RingEnd);
4421}
Francois Romieu5b0384f2006-08-16 16:00:01 +02004422
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004423static int rtl8169_rx_fill(struct rtl8169_private *tp)
4424{
4425 unsigned int i;
4426
4427 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004428 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02004429
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004430 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004432
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004433 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004434 if (!data) {
4435 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004436 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004437 }
4438 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004441 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
4442 return 0;
4443
4444err_out:
4445 rtl8169_rx_clear(tp);
4446 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447}
4448
4449static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4450{
4451 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
4452}
4453
4454static int rtl8169_init_ring(struct net_device *dev)
4455{
4456 struct rtl8169_private *tp = netdev_priv(dev);
4457
4458 rtl8169_init_ring_indexes(tp);
4459
4460 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004461 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004463 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464}
4465
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004466static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 struct TxDesc *desc)
4468{
4469 unsigned int len = tx_skb->len;
4470
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004471 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
4472
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 desc->opts1 = 0x00;
4474 desc->opts2 = 0x00;
4475 desc->addr = 0x00;
4476 tx_skb->len = 0;
4477}
4478
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004479static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
4480 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481{
4482 unsigned int i;
4483
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004484 for (i = 0; i < n; i++) {
4485 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 struct ring_info *tx_skb = tp->tx_skb + entry;
4487 unsigned int len = tx_skb->len;
4488
4489 if (len) {
4490 struct sk_buff *skb = tx_skb->skb;
4491
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004492 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 tp->TxDescArray + entry);
4494 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00004495 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 dev_kfree_skb(skb);
4497 tx_skb->skb = NULL;
4498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 }
4500 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004501}
4502
4503static void rtl8169_tx_clear(struct rtl8169_private *tp)
4504{
4505 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 tp->cur_tx = tp->dirty_tx = 0;
4507}
4508
David Howellsc4028952006-11-22 14:57:56 +00004509static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510{
4511 struct rtl8169_private *tp = netdev_priv(dev);
4512
David Howellsc4028952006-11-22 14:57:56 +00004513 PREPARE_DELAYED_WORK(&tp->task, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 schedule_delayed_work(&tp->task, 4);
4515}
4516
4517static void rtl8169_wait_for_quiescence(struct net_device *dev)
4518{
4519 struct rtl8169_private *tp = netdev_priv(dev);
4520 void __iomem *ioaddr = tp->mmio_addr;
4521
4522 synchronize_irq(dev->irq);
4523
4524 /* Wait for any pending NAPI task to complete */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004525 napi_disable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526
4527 rtl8169_irq_mask_and_ack(ioaddr);
4528
David S. Millerd1d08d12008-01-07 20:53:33 -08004529 tp->intr_mask = 0xffff;
4530 RTL_W16(IntrMask, tp->intr_event);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004531 napi_enable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532}
4533
David Howellsc4028952006-11-22 14:57:56 +00004534static void rtl8169_reinit_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535{
David Howellsc4028952006-11-22 14:57:56 +00004536 struct rtl8169_private *tp =
4537 container_of(work, struct rtl8169_private, task.work);
4538 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 int ret;
4540
Francois Romieueb2a0212007-02-15 23:37:21 +01004541 rtnl_lock();
4542
4543 if (!netif_running(dev))
4544 goto out_unlock;
4545
4546 rtl8169_wait_for_quiescence(dev);
4547 rtl8169_close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548
4549 ret = rtl8169_open(dev);
4550 if (unlikely(ret < 0)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004551 if (net_ratelimit())
4552 netif_err(tp, drv, dev,
4553 "reinit failure (status = %d). Rescheduling\n",
4554 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 rtl8169_schedule_work(dev, rtl8169_reinit_task);
4556 }
Francois Romieueb2a0212007-02-15 23:37:21 +01004557
4558out_unlock:
4559 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560}
4561
David Howellsc4028952006-11-22 14:57:56 +00004562static void rtl8169_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563{
David Howellsc4028952006-11-22 14:57:56 +00004564 struct rtl8169_private *tp =
4565 container_of(work, struct rtl8169_private, task.work);
4566 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01004567 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568
Francois Romieueb2a0212007-02-15 23:37:21 +01004569 rtnl_lock();
4570
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 if (!netif_running(dev))
Francois Romieueb2a0212007-02-15 23:37:21 +01004572 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573
4574 rtl8169_wait_for_quiescence(dev);
4575
Francois Romieu56de4142011-03-15 17:29:31 +01004576 for (i = 0; i < NUM_RX_DESC; i++)
4577 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
4578
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 rtl8169_tx_clear(tp);
4580
Francois Romieu56de4142011-03-15 17:29:31 +01004581 rtl8169_init_ring_indexes(tp);
4582 rtl_hw_start(dev);
4583 netif_wake_queue(dev);
4584 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Francois Romieueb2a0212007-02-15 23:37:21 +01004585
4586out_unlock:
4587 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588}
4589
4590static void rtl8169_tx_timeout(struct net_device *dev)
4591{
4592 struct rtl8169_private *tp = netdev_priv(dev);
4593
françois romieue6de30d2011-01-03 15:08:37 +00004594 rtl8169_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595
4596 /* Let's wait a bit while any (async) irq lands on */
4597 rtl8169_schedule_work(dev, rtl8169_reset_task);
4598}
4599
4600static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07004601 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602{
4603 struct skb_shared_info *info = skb_shinfo(skb);
4604 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04004605 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004606 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607
4608 entry = tp->cur_tx;
4609 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
4610 skb_frag_t *frag = info->frags + cur_frag;
4611 dma_addr_t mapping;
4612 u32 status, len;
4613 void *addr;
4614
4615 entry = (entry + 1) % NUM_TX_DESC;
4616
4617 txd = tp->TxDescArray + entry;
4618 len = frag->size;
4619 addr = ((void *) page_address(frag->page)) + frag->page_offset;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004620 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004621 if (unlikely(dma_mapping_error(d, mapping))) {
4622 if (net_ratelimit())
4623 netif_err(tp, drv, tp->dev,
4624 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004625 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627
Francois Romieucecb5fd2011-04-01 10:21:07 +02004628 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07004629 status = opts[0] | len |
4630 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631
4632 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07004633 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 txd->addr = cpu_to_le64(mapping);
4635
4636 tp->tx_skb[entry].len = len;
4637 }
4638
4639 if (cur_frag) {
4640 tp->tx_skb[entry].skb = skb;
4641 txd->opts1 |= cpu_to_le32(LastFrag);
4642 }
4643
4644 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004645
4646err_out:
4647 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
4648 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649}
4650
Francois Romieu2b7b4312011-04-18 22:53:24 -07004651static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
4652 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653{
Francois Romieu2b7b4312011-04-18 22:53:24 -07004654 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00004655 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07004656 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657
Francois Romieu2b7b4312011-04-18 22:53:24 -07004658 if (mss) {
4659 opts[0] |= TD_LSO;
4660 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
4661 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07004662 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663
4664 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07004665 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07004667 opts[offset] |= info->checksum.udp;
4668 else
4669 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671}
4672
Stephen Hemminger613573252009-08-31 19:50:58 +00004673static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
4674 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675{
4676 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004677 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 struct TxDesc *txd = tp->TxDescArray + entry;
4679 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004680 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 dma_addr_t mapping;
4682 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07004683 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004684 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02004685
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004687 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004688 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 }
4690
4691 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004692 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004694 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004695 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004696 if (unlikely(dma_mapping_error(d, mapping))) {
4697 if (net_ratelimit())
4698 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004699 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701
4702 tp->tx_skb[entry].len = len;
4703 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704
Francois Romieu2b7b4312011-04-18 22:53:24 -07004705 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
4706 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004707
Francois Romieu2b7b4312011-04-18 22:53:24 -07004708 rtl8169_tso_csum(tp, skb, opts);
4709
4710 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004711 if (frags < 0)
4712 goto err_dma_1;
4713 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07004714 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004715 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07004716 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004717 tp->tx_skb[entry].skb = skb;
4718 }
4719
Francois Romieu2b7b4312011-04-18 22:53:24 -07004720 txd->opts2 = cpu_to_le32(opts[1]);
4721
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 wmb();
4723
Francois Romieucecb5fd2011-04-01 10:21:07 +02004724 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07004725 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 txd->opts1 = cpu_to_le32(status);
4727
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 tp->cur_tx += frags + 1;
4729
David Dillow4c020a92010-03-03 16:33:10 +00004730 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731
Francois Romieucecb5fd2011-04-01 10:21:07 +02004732 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733
4734 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
4735 netif_stop_queue(dev);
4736 smp_rmb();
4737 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
4738 netif_wake_queue(dev);
4739 }
4740
Stephen Hemminger613573252009-08-31 19:50:58 +00004741 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004743err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004744 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004745err_dma_0:
4746 dev_kfree_skb(skb);
4747 dev->stats.tx_dropped++;
4748 return NETDEV_TX_OK;
4749
4750err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004752 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00004753 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754}
4755
4756static void rtl8169_pcierr_interrupt(struct net_device *dev)
4757{
4758 struct rtl8169_private *tp = netdev_priv(dev);
4759 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 u16 pci_status, pci_cmd;
4761
4762 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
4763 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
4764
Joe Perchesbf82c182010-02-09 11:49:50 +00004765 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
4766 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767
4768 /*
4769 * The recovery sequence below admits a very elaborated explanation:
4770 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01004771 * - I did not see what else could be done;
4772 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 *
4774 * Feel free to adjust to your needs.
4775 */
Francois Romieua27993f2006-12-18 00:04:19 +01004776 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01004777 pci_cmd &= ~PCI_COMMAND_PARITY;
4778 else
4779 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
4780
4781 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782
4783 pci_write_config_word(pdev, PCI_STATUS,
4784 pci_status & (PCI_STATUS_DETECTED_PARITY |
4785 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
4786 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
4787
4788 /* The infamous DAC f*ckup only happens at boot time */
4789 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00004790 void __iomem *ioaddr = tp->mmio_addr;
4791
Joe Perchesbf82c182010-02-09 11:49:50 +00004792 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 tp->cp_cmd &= ~PCIDAC;
4794 RTL_W16(CPlusCmd, tp->cp_cmd);
4795 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 }
4797
françois romieue6de30d2011-01-03 15:08:37 +00004798 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01004799
4800 rtl8169_schedule_work(dev, rtl8169_reinit_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801}
4802
Francois Romieu07d3f512007-02-21 22:40:46 +01004803static void rtl8169_tx_interrupt(struct net_device *dev,
4804 struct rtl8169_private *tp,
4805 void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806{
4807 unsigned int dirty_tx, tx_left;
4808
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 dirty_tx = tp->dirty_tx;
4810 smp_rmb();
4811 tx_left = tp->cur_tx - dirty_tx;
4812
4813 while (tx_left > 0) {
4814 unsigned int entry = dirty_tx % NUM_TX_DESC;
4815 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816 u32 status;
4817
4818 rmb();
4819 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
4820 if (status & DescOwn)
4821 break;
4822
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004823 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
4824 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 if (status & LastFrag) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00004826 dev->stats.tx_packets++;
4827 dev->stats.tx_bytes += tx_skb->skb->len;
Eric Dumazet87433bf2009-06-09 22:55:53 +00004828 dev_kfree_skb(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 tx_skb->skb = NULL;
4830 }
4831 dirty_tx++;
4832 tx_left--;
4833 }
4834
4835 if (tp->dirty_tx != dirty_tx) {
4836 tp->dirty_tx = dirty_tx;
4837 smp_wmb();
4838 if (netif_queue_stopped(dev) &&
4839 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
4840 netif_wake_queue(dev);
4841 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02004842 /*
4843 * 8168 hack: TxPoll requests are lost when the Tx packets are
4844 * too close. Let's kick an extra TxPoll request when a burst
4845 * of start_xmit activity is detected (if it is not detected,
4846 * it is slow enough). -- FR
4847 */
4848 smp_rmb();
4849 if (tp->cur_tx != dirty_tx)
4850 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 }
4852}
4853
Francois Romieu126fa4b2005-05-12 20:09:17 -04004854static inline int rtl8169_fragmented_frame(u32 status)
4855{
4856 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
4857}
4858
Eric Dumazetadea1ac72010-09-05 20:04:05 -07004859static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 u32 status = opts1 & RxProtoMask;
4862
4863 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00004864 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 skb->ip_summed = CHECKSUM_UNNECESSARY;
4866 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07004867 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868}
4869
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004870static struct sk_buff *rtl8169_try_rx_copy(void *data,
4871 struct rtl8169_private *tp,
4872 int pkt_size,
4873 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02004875 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004876 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004878 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004879 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004880 prefetch(data);
4881 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
4882 if (skb)
4883 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004884 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
4885
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004886 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887}
4888
Francois Romieu07d3f512007-02-21 22:40:46 +01004889static int rtl8169_rx_interrupt(struct net_device *dev,
4890 struct rtl8169_private *tp,
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004891 void __iomem *ioaddr, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892{
4893 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004894 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 cur_rx = tp->cur_rx;
4897 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02004898 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899
Richard Dawe4dcb7d32005-05-27 21:12:00 +02004900 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04004902 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 u32 status;
4904
4905 rmb();
Francois Romieu126fa4b2005-05-12 20:09:17 -04004906 status = le32_to_cpu(desc->opts1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907
4908 if (status & DescOwn)
4909 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02004910 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004911 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
4912 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004913 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02004915 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02004917 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02004918 if (status & RxFOVF) {
4919 rtl8169_schedule_work(dev, rtl8169_reset_task);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004920 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02004921 }
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004922 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004924 struct sk_buff *skb;
Stephen Hemmingerb4496552007-06-17 01:06:49 +02004925 dma_addr_t addr = le64_to_cpu(desc->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 int pkt_size = (status & 0x00001FFF) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927
Francois Romieu126fa4b2005-05-12 20:09:17 -04004928 /*
4929 * The driver does not support incoming fragmented
4930 * frames. They are seen as a symptom of over-mtu
4931 * sized frames.
4932 */
4933 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02004934 dev->stats.rx_dropped++;
4935 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004936 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02004937 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04004938 }
4939
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004940 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
4941 tp, pkt_size, addr);
4942 rtl8169_mark_to_asic(desc, rx_buf_sz);
4943 if (!skb) {
4944 dev->stats.rx_dropped++;
4945 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 }
4947
Eric Dumazetadea1ac72010-09-05 20:04:05 -07004948 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 skb_put(skb, pkt_size);
4950 skb->protocol = eth_type_trans(skb, dev);
4951
Francois Romieu7a8fc772011-03-01 17:18:33 +01004952 rtl8169_rx_vlan_tag(desc, skb);
4953
Francois Romieu56de4142011-03-15 17:29:31 +01004954 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955
Francois Romieucebf8cc2007-10-18 12:06:54 +02004956 dev->stats.rx_bytes += pkt_size;
4957 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 }
Francois Romieu6dccd162007-02-13 23:38:05 +01004959
4960 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00004961 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01004962 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
4963 desc->opts2 = 0;
4964 cur_rx++;
4965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 }
4967
4968 count = cur_rx - tp->cur_rx;
4969 tp->cur_rx = cur_rx;
4970
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004971 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972
4973 return count;
4974}
4975
Francois Romieu07d3f512007-02-21 22:40:46 +01004976static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977{
Francois Romieu07d3f512007-02-21 22:40:46 +01004978 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 int handled = 0;
Francois Romieu865c6522008-05-11 14:51:00 +02004982 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983
David Dillowf11a3772009-05-22 15:29:34 +00004984 /* loop handling interrupts until we have no new ones or
4985 * we hit a invalid/hotplug case.
4986 */
Francois Romieu865c6522008-05-11 14:51:00 +02004987 status = RTL_R16(IntrStatus);
David Dillowf11a3772009-05-22 15:29:34 +00004988 while (status && status != 0xffff) {
4989 handled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990
David Dillowf11a3772009-05-22 15:29:34 +00004991 /* Handle all of the error cases first. These will reset
4992 * the chip, so just exit the loop.
4993 */
4994 if (unlikely(!netif_running(dev))) {
4995 rtl8169_asic_down(ioaddr);
4996 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 }
David Dillowf11a3772009-05-22 15:29:34 +00004998
Francois Romieu1519e572011-02-03 12:02:36 +01004999 if (unlikely(status & RxFIFOOver)) {
5000 switch (tp->mac_version) {
5001 /* Work around for rx fifo overflow */
5002 case RTL_GIGA_MAC_VER_11:
5003 case RTL_GIGA_MAC_VER_22:
5004 case RTL_GIGA_MAC_VER_26:
5005 netif_stop_queue(dev);
5006 rtl8169_tx_timeout(dev);
5007 goto done;
Francois Romieuf60ac8e2011-02-03 17:27:52 +01005008 /* Testers needed. */
5009 case RTL_GIGA_MAC_VER_17:
5010 case RTL_GIGA_MAC_VER_19:
5011 case RTL_GIGA_MAC_VER_20:
5012 case RTL_GIGA_MAC_VER_21:
5013 case RTL_GIGA_MAC_VER_23:
5014 case RTL_GIGA_MAC_VER_24:
5015 case RTL_GIGA_MAC_VER_27:
5016 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00005017 case RTL_GIGA_MAC_VER_31:
Francois Romieu1519e572011-02-03 12:02:36 +01005018 /* Experimental science. Pktgen proof. */
5019 case RTL_GIGA_MAC_VER_12:
5020 case RTL_GIGA_MAC_VER_25:
5021 if (status == RxFIFOOver)
5022 goto done;
5023 break;
5024 default:
5025 break;
5026 }
David Dillowf11a3772009-05-22 15:29:34 +00005027 }
5028
5029 if (unlikely(status & SYSErr)) {
5030 rtl8169_pcierr_interrupt(dev);
5031 break;
5032 }
5033
5034 if (status & LinkChg)
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005035 __rtl8169_check_link_status(dev, tp, ioaddr, true);
David Dillowf11a3772009-05-22 15:29:34 +00005036
5037 /* We need to see the lastest version of tp->intr_mask to
5038 * avoid ignoring an MSI interrupt and having to wait for
5039 * another event which may never come.
5040 */
5041 smp_rmb();
5042 if (status & tp->intr_mask & tp->napi_event) {
5043 RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
5044 tp->intr_mask = ~tp->napi_event;
5045
5046 if (likely(napi_schedule_prep(&tp->napi)))
5047 __napi_schedule(&tp->napi);
Joe Perchesbf82c182010-02-09 11:49:50 +00005048 else
5049 netif_info(tp, intr, dev,
5050 "interrupt %04x in poll\n", status);
David Dillowf11a3772009-05-22 15:29:34 +00005051 }
5052
5053 /* We only get a new MSI interrupt when all active irq
5054 * sources on the chip have been acknowledged. So, ack
5055 * everything we've seen and check if new sources have become
5056 * active to avoid blocking all interrupts from the chip.
5057 */
5058 RTL_W16(IntrStatus,
5059 (status & RxFIFOOver) ? (status | RxOverflow) : status);
5060 status = RTL_R16(IntrStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 }
Francois Romieu1519e572011-02-03 12:02:36 +01005062done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 return IRQ_RETVAL(handled);
5064}
5065
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005066static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005068 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5069 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 void __iomem *ioaddr = tp->mmio_addr;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005071 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005073 work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 rtl8169_tx_interrupt(dev, tp, ioaddr);
5075
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005076 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005077 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00005078
5079 /* We need for force the visibility of tp->intr_mask
5080 * for other CPUs, as we can loose an MSI interrupt
5081 * and potentially wait for a retransmit timeout if we don't.
5082 * The posted write to IntrMask is safe, as it will
5083 * eventually make it to the chip and we won't loose anything
5084 * until it does.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 */
David Dillowf11a3772009-05-22 15:29:34 +00005086 tp->intr_mask = 0xffff;
David Dillow4c020a92010-03-03 16:33:10 +00005087 wmb();
Francois Romieu0e485152007-02-20 00:00:26 +01005088 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 }
5090
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005091 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093
Francois Romieu523a6092008-09-10 22:28:56 +02005094static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5095{
5096 struct rtl8169_private *tp = netdev_priv(dev);
5097
5098 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5099 return;
5100
5101 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5102 RTL_W32(RxMissed, 0);
5103}
5104
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105static void rtl8169_down(struct net_device *dev)
5106{
5107 struct rtl8169_private *tp = netdev_priv(dev);
5108 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109
Francois Romieu4876cc12011-03-11 21:07:11 +01005110 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111
5112 netif_stop_queue(dev);
5113
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005114 napi_disable(&tp->napi);
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005115
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 spin_lock_irq(&tp->lock);
5117
5118 rtl8169_asic_down(ioaddr);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005119 /*
5120 * At this point device interrupts can not be enabled in any function,
5121 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task,
5122 * rtl8169_reinit_task) and napi is disabled (rtl8169_poll).
5123 */
Francois Romieu523a6092008-09-10 22:28:56 +02005124 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125
5126 spin_unlock_irq(&tp->lock);
5127
5128 synchronize_irq(dev->irq);
5129
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 /* Give a racing hard_start_xmit a few cycles to complete. */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07005131 synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 rtl8169_tx_clear(tp);
5134
5135 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00005136
5137 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138}
5139
5140static int rtl8169_close(struct net_device *dev)
5141{
5142 struct rtl8169_private *tp = netdev_priv(dev);
5143 struct pci_dev *pdev = tp->pci_dev;
5144
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005145 pm_runtime_get_sync(&pdev->dev);
5146
Francois Romieucecb5fd2011-04-01 10:21:07 +02005147 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08005148 rtl8169_update_counters(dev);
5149
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 rtl8169_down(dev);
5151
5152 free_irq(dev->irq, dev);
5153
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00005154 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5155 tp->RxPhyAddr);
5156 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5157 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 tp->TxDescArray = NULL;
5159 tp->RxDescArray = NULL;
5160
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005161 pm_runtime_put_sync(&pdev->dev);
5162
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 return 0;
5164}
5165
Francois Romieu07ce4062007-02-23 23:36:39 +01005166static void rtl_set_rx_mode(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167{
5168 struct rtl8169_private *tp = netdev_priv(dev);
5169 void __iomem *ioaddr = tp->mmio_addr;
5170 unsigned long flags;
5171 u32 mc_filter[2]; /* Multicast hash filter */
Francois Romieu07d3f512007-02-21 22:40:46 +01005172 int rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 u32 tmp = 0;
5174
5175 if (dev->flags & IFF_PROMISC) {
5176 /* Unconditionally log net taps. */
Joe Perchesbf82c182010-02-09 11:49:50 +00005177 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 rx_mode =
5179 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5180 AcceptAllPhys;
5181 mc_filter[1] = mc_filter[0] = 0xffffffff;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00005182 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
Joe Perches8e95a202009-12-03 07:58:21 +00005183 (dev->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 /* Too many to filter perfectly -- accept all multicasts. */
5185 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5186 mc_filter[1] = mc_filter[0] = 0xffffffff;
5187 } else {
Jiri Pirko22bedad2010-04-01 21:22:57 +00005188 struct netdev_hw_addr *ha;
Francois Romieu07d3f512007-02-21 22:40:46 +01005189
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 rx_mode = AcceptBroadcast | AcceptMyPhys;
5191 mc_filter[1] = mc_filter[0] = 0;
Jiri Pirko22bedad2010-04-01 21:22:57 +00005192 netdev_for_each_mc_addr(ha, dev) {
5193 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5195 rx_mode |= AcceptMulticast;
5196 }
5197 }
5198
5199 spin_lock_irqsave(&tp->lock, flags);
5200
5201 tmp = rtl8169_rx_config | rx_mode |
Francois Romieu2b7b4312011-04-18 22:53:24 -07005202 (RTL_R32(RxConfig) & RTL_RX_CONFIG_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203
Francois Romieuf887cce2008-07-17 22:24:18 +02005204 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
Francois Romieu1087f4f2007-12-26 22:46:05 +01005205 u32 data = mc_filter[0];
5206
5207 mc_filter[0] = swab32(mc_filter[1]);
5208 mc_filter[1] = swab32(data);
Francois Romieubcf0bf92006-07-26 23:14:13 +02005209 }
5210
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 RTL_W32(MAR0 + 4, mc_filter[1]);
Francois Romieu78f1cd02010-03-27 19:35:46 -07005212 RTL_W32(MAR0 + 0, mc_filter[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213
Francois Romieu57a9f232007-06-04 22:10:15 +02005214 RTL_W32(RxConfig, tmp);
5215
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 spin_unlock_irqrestore(&tp->lock, flags);
5217}
5218
5219/**
5220 * rtl8169_get_stats - Get rtl8169 read/write statistics
5221 * @dev: The Ethernet Device to get statistics for
5222 *
5223 * Get TX/RX statistics for rtl8169
5224 */
5225static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
5226{
5227 struct rtl8169_private *tp = netdev_priv(dev);
5228 void __iomem *ioaddr = tp->mmio_addr;
5229 unsigned long flags;
5230
5231 if (netif_running(dev)) {
5232 spin_lock_irqsave(&tp->lock, flags);
Francois Romieu523a6092008-09-10 22:28:56 +02005233 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 spin_unlock_irqrestore(&tp->lock, flags);
5235 }
Francois Romieu5b0384f2006-08-16 16:00:01 +02005236
Francois Romieucebf8cc2007-10-18 12:06:54 +02005237 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238}
5239
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005240static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01005241{
françois romieu065c27c2011-01-03 15:08:12 +00005242 struct rtl8169_private *tp = netdev_priv(dev);
5243
Francois Romieu5d06a992006-02-23 00:47:58 +01005244 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005245 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01005246
françois romieu065c27c2011-01-03 15:08:12 +00005247 rtl_pll_power_down(tp);
5248
Francois Romieu5d06a992006-02-23 00:47:58 +01005249 netif_device_detach(dev);
5250 netif_stop_queue(dev);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005251}
Francois Romieu5d06a992006-02-23 00:47:58 +01005252
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005253#ifdef CONFIG_PM
5254
5255static int rtl8169_suspend(struct device *device)
5256{
5257 struct pci_dev *pdev = to_pci_dev(device);
5258 struct net_device *dev = pci_get_drvdata(pdev);
5259
5260 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02005261
Francois Romieu5d06a992006-02-23 00:47:58 +01005262 return 0;
5263}
5264
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005265static void __rtl8169_resume(struct net_device *dev)
5266{
françois romieu065c27c2011-01-03 15:08:12 +00005267 struct rtl8169_private *tp = netdev_priv(dev);
5268
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005269 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00005270
5271 rtl_pll_power_up(tp);
5272
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005273 rtl8169_schedule_work(dev, rtl8169_reset_task);
5274}
5275
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005276static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01005277{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005278 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01005279 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005280 struct rtl8169_private *tp = netdev_priv(dev);
5281
5282 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01005283
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005284 if (netif_running(dev))
5285 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01005286
Francois Romieu5d06a992006-02-23 00:47:58 +01005287 return 0;
5288}
5289
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005290static int rtl8169_runtime_suspend(struct device *device)
5291{
5292 struct pci_dev *pdev = to_pci_dev(device);
5293 struct net_device *dev = pci_get_drvdata(pdev);
5294 struct rtl8169_private *tp = netdev_priv(dev);
5295
5296 if (!tp->TxDescArray)
5297 return 0;
5298
5299 spin_lock_irq(&tp->lock);
5300 tp->saved_wolopts = __rtl8169_get_wol(tp);
5301 __rtl8169_set_wol(tp, WAKE_ANY);
5302 spin_unlock_irq(&tp->lock);
5303
5304 rtl8169_net_suspend(dev);
5305
5306 return 0;
5307}
5308
5309static int rtl8169_runtime_resume(struct device *device)
5310{
5311 struct pci_dev *pdev = to_pci_dev(device);
5312 struct net_device *dev = pci_get_drvdata(pdev);
5313 struct rtl8169_private *tp = netdev_priv(dev);
5314
5315 if (!tp->TxDescArray)
5316 return 0;
5317
5318 spin_lock_irq(&tp->lock);
5319 __rtl8169_set_wol(tp, tp->saved_wolopts);
5320 tp->saved_wolopts = 0;
5321 spin_unlock_irq(&tp->lock);
5322
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005323 rtl8169_init_phy(dev, tp);
5324
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005325 __rtl8169_resume(dev);
5326
5327 return 0;
5328}
5329
5330static int rtl8169_runtime_idle(struct device *device)
5331{
5332 struct pci_dev *pdev = to_pci_dev(device);
5333 struct net_device *dev = pci_get_drvdata(pdev);
5334 struct rtl8169_private *tp = netdev_priv(dev);
5335
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005336 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005337}
5338
Alexey Dobriyan47145212009-12-14 18:00:08 -08005339static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02005340 .suspend = rtl8169_suspend,
5341 .resume = rtl8169_resume,
5342 .freeze = rtl8169_suspend,
5343 .thaw = rtl8169_resume,
5344 .poweroff = rtl8169_suspend,
5345 .restore = rtl8169_resume,
5346 .runtime_suspend = rtl8169_runtime_suspend,
5347 .runtime_resume = rtl8169_runtime_resume,
5348 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005349};
5350
5351#define RTL8169_PM_OPS (&rtl8169_pm_ops)
5352
5353#else /* !CONFIG_PM */
5354
5355#define RTL8169_PM_OPS NULL
5356
5357#endif /* !CONFIG_PM */
5358
Francois Romieu1765f952008-09-13 17:21:40 +02005359static void rtl_shutdown(struct pci_dev *pdev)
5360{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005361 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00005362 struct rtl8169_private *tp = netdev_priv(dev);
5363 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu1765f952008-09-13 17:21:40 +02005364
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005365 rtl8169_net_suspend(dev);
5366
Francois Romieucecb5fd2011-04-01 10:21:07 +02005367 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08005368 rtl_rar_set(tp, dev->perm_addr);
5369
françois romieu4bb3f522009-06-17 11:41:45 +00005370 spin_lock_irq(&tp->lock);
5371
5372 rtl8169_asic_down(ioaddr);
5373
5374 spin_unlock_irq(&tp->lock);
5375
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005376 if (system_state == SYSTEM_POWER_OFF) {
françois romieuca52efd2009-07-24 12:34:19 +00005377 /* WoL fails with some 8168 when the receiver is disabled. */
5378 if (tp->features & RTL_FEATURE_WOL) {
5379 pci_clear_master(pdev);
5380
5381 RTL_W8(ChipCmd, CmdRxEnb);
5382 /* PCI commit */
5383 RTL_R8(ChipCmd);
5384 }
5385
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005386 pci_wake_from_d3(pdev, true);
5387 pci_set_power_state(pdev, PCI_D3hot);
5388 }
5389}
Francois Romieu5d06a992006-02-23 00:47:58 +01005390
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391static struct pci_driver rtl8169_pci_driver = {
5392 .name = MODULENAME,
5393 .id_table = rtl8169_pci_tbl,
5394 .probe = rtl8169_init_one,
5395 .remove = __devexit_p(rtl8169_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02005396 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005397 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398};
5399
Francois Romieu07d3f512007-02-21 22:40:46 +01005400static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401{
Jeff Garzik29917622006-08-19 17:48:59 -04005402 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403}
5404
Francois Romieu07d3f512007-02-21 22:40:46 +01005405static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406{
5407 pci_unregister_driver(&rtl8169_pci_driver);
5408}
5409
5410module_init(rtl8169_init_module);
5411module_exit(rtl8169_cleanup_module);