blob: fbd68383ca60d40c3fa577f308ea089442d71a1b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Francois Romieu07d3f512007-02-21 22:40:46 +01002 * r8169.c: RealTek 8169/8168/8101 ethernet driver.
3 *
4 * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
5 * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
6 * Copyright (c) a lot of people too. Please respect their work.
7 *
8 * See MAINTAINERS file for support contact information.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11#include <linux/module.h>
12#include <linux/moduleparam.h>
13#include <linux/pci.h>
14#include <linux/netdevice.h>
15#include <linux/etherdevice.h>
16#include <linux/delay.h>
17#include <linux/ethtool.h>
18#include <linux/mii.h>
19#include <linux/if_vlan.h>
20#include <linux/crc32.h>
21#include <linux/in.h>
22#include <linux/ip.h>
23#include <linux/tcp.h>
24#include <linux/init.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000025#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/dma-mapping.h>
Rafael J. Wysockie1759442010-03-14 14:33:51 +000027#include <linux/pm_runtime.h>
françois romieubca03d52011-01-03 15:07:31 +000028#include <linux/firmware.h>
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +000029#include <linux/pci-aspm.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040030#include <linux/prefetch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Francois Romieu99f252b2007-04-02 22:59:59 +020032#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/io.h>
34#include <asm/irq.h>
35
Francois Romieu865c6522008-05-11 14:51:00 +020036#define RTL8169_VERSION "2.3LK-NAPI"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define MODULENAME "r8169"
38#define PFX MODULENAME ": "
39
françois romieubca03d52011-01-03 15:07:31 +000040#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
41#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
hayeswang01dc7fe2011-03-21 01:50:28 +000042#define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
43#define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080044#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
françois romieubca03d52011-01-03 15:07:31 +000045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#ifdef RTL8169_DEBUG
47#define assert(expr) \
Francois Romieu5b0384f2006-08-16 16:00:01 +020048 if (!(expr)) { \
49 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
Harvey Harrisonb39d66a2008-08-20 16:52:04 -070050 #expr,__FILE__,__func__,__LINE__); \
Francois Romieu5b0384f2006-08-16 16:00:01 +020051 }
Joe Perches06fa7352007-10-18 21:15:00 +020052#define dprintk(fmt, args...) \
53 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#else
55#define assert(expr) do {} while (0)
56#define dprintk(fmt, args...) do {} while (0)
57#endif /* RTL8169_DEBUG */
58
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020059#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d2005-09-30 16:54:02 -070060 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#define TX_BUFFS_AVAIL(tp) \
63 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
66 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050067static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69/* MAC address length */
70#define MAC_ADDR_LEN 6
71
Francois Romieu9c14cea2008-07-05 00:21:15 +020072#define MAX_READ_REQUEST_SHIFT 12
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#define RX_FIFO_THRESH 7 /* 7 means NO threshold, Rx buffer level before first PCI xfer. */
74#define RX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
75#define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
77#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
78
79#define R8169_REGS_SIZE 256
80#define R8169_NAPI_WEIGHT 64
81#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
82#define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
83#define RX_BUF_SIZE 1536 /* Rx Buffer size */
84#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
85#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
86
87#define RTL8169_TX_TIMEOUT (6*HZ)
88#define RTL8169_PHY_TIMEOUT (10*HZ)
89
françois romieuea8dbdd2009-03-15 01:10:50 +000090#define RTL_EEPROM_SIG cpu_to_le32(0x8129)
91#define RTL_EEPROM_SIG_MASK cpu_to_le32(0xffff)
Francois Romieue1564ec2008-10-16 22:46:13 +020092#define RTL_EEPROM_SIG_ADDR 0x0000
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094/* write/read MMIO register */
95#define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
96#define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
97#define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
98#define RTL_R8(reg) readb (ioaddr + (reg))
99#define RTL_R16(reg) readw (ioaddr + (reg))
Junchang Wang06f555f2010-05-30 02:26:07 +0000100#define RTL_R32(reg) readl (ioaddr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +0200103 RTL_GIGA_MAC_VER_01 = 0,
104 RTL_GIGA_MAC_VER_02,
105 RTL_GIGA_MAC_VER_03,
106 RTL_GIGA_MAC_VER_04,
107 RTL_GIGA_MAC_VER_05,
108 RTL_GIGA_MAC_VER_06,
109 RTL_GIGA_MAC_VER_07,
110 RTL_GIGA_MAC_VER_08,
111 RTL_GIGA_MAC_VER_09,
112 RTL_GIGA_MAC_VER_10,
113 RTL_GIGA_MAC_VER_11,
114 RTL_GIGA_MAC_VER_12,
115 RTL_GIGA_MAC_VER_13,
116 RTL_GIGA_MAC_VER_14,
117 RTL_GIGA_MAC_VER_15,
118 RTL_GIGA_MAC_VER_16,
119 RTL_GIGA_MAC_VER_17,
120 RTL_GIGA_MAC_VER_18,
121 RTL_GIGA_MAC_VER_19,
122 RTL_GIGA_MAC_VER_20,
123 RTL_GIGA_MAC_VER_21,
124 RTL_GIGA_MAC_VER_22,
125 RTL_GIGA_MAC_VER_23,
126 RTL_GIGA_MAC_VER_24,
127 RTL_GIGA_MAC_VER_25,
128 RTL_GIGA_MAC_VER_26,
129 RTL_GIGA_MAC_VER_27,
130 RTL_GIGA_MAC_VER_28,
131 RTL_GIGA_MAC_VER_29,
132 RTL_GIGA_MAC_VER_30,
133 RTL_GIGA_MAC_VER_31,
134 RTL_GIGA_MAC_VER_32,
135 RTL_GIGA_MAC_VER_33,
136 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137};
138
Francois Romieu2b7b4312011-04-18 22:53:24 -0700139enum rtl_tx_desc_version {
140 RTL_TD_0 = 0,
141 RTL_TD_1 = 1,
142};
143
Francois Romieu85bffe62011-04-27 08:22:39 +0200144#define _R(NAME,TD,FW) \
145 { .name = NAME, .txd_version = TD, .fw_name = FW }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800147static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 const char *name;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700149 enum rtl_tx_desc_version txd_version;
Francois Romieu85bffe62011-04-27 08:22:39 +0200150 const char *fw_name;
151} rtl_chip_infos[] = {
152 /* PCI devices. */
153 [RTL_GIGA_MAC_VER_01] =
154 _R("RTL8169", RTL_TD_0, NULL),
155 [RTL_GIGA_MAC_VER_02] =
156 _R("RTL8169s", RTL_TD_0, NULL),
157 [RTL_GIGA_MAC_VER_03] =
158 _R("RTL8110s", RTL_TD_0, NULL),
159 [RTL_GIGA_MAC_VER_04] =
160 _R("RTL8169sb/8110sb", RTL_TD_0, NULL),
161 [RTL_GIGA_MAC_VER_05] =
162 _R("RTL8169sc/8110sc", RTL_TD_0, NULL),
163 [RTL_GIGA_MAC_VER_06] =
164 _R("RTL8169sc/8110sc", RTL_TD_0, NULL),
165 /* PCI-E devices. */
166 [RTL_GIGA_MAC_VER_07] =
167 _R("RTL8102e", RTL_TD_1, NULL),
168 [RTL_GIGA_MAC_VER_08] =
169 _R("RTL8102e", RTL_TD_1, NULL),
170 [RTL_GIGA_MAC_VER_09] =
171 _R("RTL8102e", RTL_TD_1, NULL),
172 [RTL_GIGA_MAC_VER_10] =
173 _R("RTL8101e", RTL_TD_0, NULL),
174 [RTL_GIGA_MAC_VER_11] =
175 _R("RTL8168b/8111b", RTL_TD_0, NULL),
176 [RTL_GIGA_MAC_VER_12] =
177 _R("RTL8168b/8111b", RTL_TD_0, NULL),
178 [RTL_GIGA_MAC_VER_13] =
179 _R("RTL8101e", RTL_TD_0, NULL),
180 [RTL_GIGA_MAC_VER_14] =
181 _R("RTL8100e", RTL_TD_0, NULL),
182 [RTL_GIGA_MAC_VER_15] =
183 _R("RTL8100e", RTL_TD_0, NULL),
184 [RTL_GIGA_MAC_VER_16] =
185 _R("RTL8101e", RTL_TD_0, NULL),
186 [RTL_GIGA_MAC_VER_17] =
187 _R("RTL8168b/8111b", RTL_TD_0, NULL),
188 [RTL_GIGA_MAC_VER_18] =
189 _R("RTL8168cp/8111cp", RTL_TD_1, NULL),
190 [RTL_GIGA_MAC_VER_19] =
191 _R("RTL8168c/8111c", RTL_TD_1, NULL),
192 [RTL_GIGA_MAC_VER_20] =
193 _R("RTL8168c/8111c", RTL_TD_1, NULL),
194 [RTL_GIGA_MAC_VER_21] =
195 _R("RTL8168c/8111c", RTL_TD_1, NULL),
196 [RTL_GIGA_MAC_VER_22] =
197 _R("RTL8168c/8111c", RTL_TD_1, NULL),
198 [RTL_GIGA_MAC_VER_23] =
199 _R("RTL8168cp/8111cp", RTL_TD_1, NULL),
200 [RTL_GIGA_MAC_VER_24] =
201 _R("RTL8168cp/8111cp", RTL_TD_1, NULL),
202 [RTL_GIGA_MAC_VER_25] =
203 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1),
204 [RTL_GIGA_MAC_VER_26] =
205 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2),
206 [RTL_GIGA_MAC_VER_27] =
207 _R("RTL8168dp/8111dp", RTL_TD_1, NULL),
208 [RTL_GIGA_MAC_VER_28] =
209 _R("RTL8168dp/8111dp", RTL_TD_1, NULL),
210 [RTL_GIGA_MAC_VER_29] =
211 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1),
212 [RTL_GIGA_MAC_VER_30] =
213 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1),
214 [RTL_GIGA_MAC_VER_31] =
215 _R("RTL8168dp/8111dp", RTL_TD_1, NULL),
216 [RTL_GIGA_MAC_VER_32] =
217 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1),
218 [RTL_GIGA_MAC_VER_33] =
219 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220};
221#undef _R
222
Francois Romieubcf0bf92006-07-26 23:14:13 +0200223enum cfg_version {
224 RTL_CFG_0 = 0x00,
225 RTL_CFG_1,
226 RTL_CFG_2
227};
228
Francois Romieu07ce4062007-02-23 23:36:39 +0100229static void rtl_hw_start_8169(struct net_device *);
230static void rtl_hw_start_8168(struct net_device *);
231static void rtl_hw_start_8101(struct net_device *);
232
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000233static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200234 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200235 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Francois Romieud81bf552006-09-20 21:31:20 +0200236 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100237 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200238 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
239 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200240 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200241 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
242 { PCI_VENDOR_ID_LINKSYS, 0x1032,
243 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100244 { 0x0001, 0x8168,
245 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 {0,},
247};
248
249MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
250
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000251static int rx_buf_sz = 16383;
David S. Miller4300e8c2010-03-26 10:23:30 -0700252static int use_dac;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200253static struct {
254 u32 msg_enable;
255} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Francois Romieu07d3f512007-02-21 22:40:46 +0100257enum rtl_registers {
258 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100259 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100260 MAR0 = 8, /* Multicast filter. */
261 CounterAddrLow = 0x10,
262 CounterAddrHigh = 0x14,
263 TxDescStartAddrLow = 0x20,
264 TxDescStartAddrHigh = 0x24,
265 TxHDescStartAddrLow = 0x28,
266 TxHDescStartAddrHigh = 0x2c,
267 FLASH = 0x30,
268 ERSR = 0x36,
269 ChipCmd = 0x37,
270 TxPoll = 0x38,
271 IntrMask = 0x3c,
272 IntrStatus = 0x3e,
273 TxConfig = 0x40,
274 RxConfig = 0x44,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700275
276#define RTL_RX_CONFIG_MASK 0xff7e1880u
277
Francois Romieu07d3f512007-02-21 22:40:46 +0100278 RxMissed = 0x4c,
279 Cfg9346 = 0x50,
280 Config0 = 0x51,
281 Config1 = 0x52,
282 Config2 = 0x53,
283 Config3 = 0x54,
284 Config4 = 0x55,
285 Config5 = 0x56,
286 MultiIntr = 0x5c,
287 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100288 PHYstatus = 0x6c,
289 RxMaxSize = 0xda,
290 CPlusCmd = 0xe0,
291 IntrMitigate = 0xe2,
292 RxDescAddrLow = 0xe4,
293 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000294 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
295
296#define NoEarlyTx 0x3f /* Max value : no early transmit. */
297
298 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
299
300#define TxPacketMax (8064 >> 7)
301
Francois Romieu07d3f512007-02-21 22:40:46 +0100302 FuncEvent = 0xf0,
303 FuncEventMask = 0xf4,
304 FuncPresetState = 0xf8,
305 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306};
307
Francois Romieuf162a5d2008-06-01 22:37:49 +0200308enum rtl8110_registers {
309 TBICSR = 0x64,
310 TBI_ANAR = 0x68,
311 TBI_LPAR = 0x6a,
312};
313
314enum rtl8168_8101_registers {
315 CSIDR = 0x64,
316 CSIAR = 0x68,
317#define CSIAR_FLAG 0x80000000
318#define CSIAR_WRITE_CMD 0x80000000
319#define CSIAR_BYTE_ENABLE 0x0f
320#define CSIAR_BYTE_ENABLE_SHIFT 12
321#define CSIAR_ADDR_MASK 0x0fff
françois romieu065c27c2011-01-03 15:08:12 +0000322 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200323 EPHYAR = 0x80,
324#define EPHYAR_FLAG 0x80000000
325#define EPHYAR_WRITE_CMD 0x80000000
326#define EPHYAR_REG_MASK 0x1f
327#define EPHYAR_REG_SHIFT 16
328#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800329 DLLPR = 0xd0,
330#define PM_SWITCH (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200331 DBG_REG = 0xd1,
332#define FIX_NAK_1 (1 << 4)
333#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800334 TWSI = 0xd2,
335 MCU = 0xd3,
336#define EN_NDP (1 << 3)
337#define EN_OOB_RESET (1 << 2)
françois romieudaf9df62009-10-07 12:44:20 +0000338 EFUSEAR = 0xdc,
339#define EFUSEAR_FLAG 0x80000000
340#define EFUSEAR_WRITE_CMD 0x80000000
341#define EFUSEAR_READ_CMD 0x00000000
342#define EFUSEAR_REG_MASK 0x03ff
343#define EFUSEAR_REG_SHIFT 8
344#define EFUSEAR_DATA_MASK 0xff
Francois Romieuf162a5d2008-06-01 22:37:49 +0200345};
346
françois romieuc0e45c12011-01-03 15:08:04 +0000347enum rtl8168_registers {
françois romieub646d902011-01-03 15:08:21 +0000348 ERIDR = 0x70,
349 ERIAR = 0x74,
350#define ERIAR_FLAG 0x80000000
351#define ERIAR_WRITE_CMD 0x80000000
352#define ERIAR_READ_CMD 0x00000000
353#define ERIAR_ADDR_BYTE_ALIGN 4
354#define ERIAR_EXGMAC 0
355#define ERIAR_MSIX 1
356#define ERIAR_ASF 2
357#define ERIAR_TYPE_SHIFT 16
358#define ERIAR_BYTEEN 0x0f
359#define ERIAR_BYTEEN_SHIFT 12
françois romieuc0e45c12011-01-03 15:08:04 +0000360 EPHY_RXER_NUM = 0x7c,
361 OCPDR = 0xb0, /* OCP GPHY access */
362#define OCPDR_WRITE_CMD 0x80000000
363#define OCPDR_READ_CMD 0x00000000
364#define OCPDR_REG_MASK 0x7f
365#define OCPDR_GPHY_REG_SHIFT 16
366#define OCPDR_DATA_MASK 0xffff
367 OCPAR = 0xb4,
368#define OCPAR_FLAG 0x80000000
369#define OCPAR_GPHY_WRITE_CMD 0x8000f060
370#define OCPAR_GPHY_READ_CMD 0x0000f060
hayeswang01dc7fe2011-03-21 01:50:28 +0000371 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
372 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200373#define TXPLA_RST (1 << 29)
françois romieuc0e45c12011-01-03 15:08:04 +0000374};
375
Francois Romieu07d3f512007-02-21 22:40:46 +0100376enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100378 SYSErr = 0x8000,
379 PCSTimeout = 0x4000,
380 SWInt = 0x0100,
381 TxDescUnavail = 0x0080,
382 RxFIFOOver = 0x0040,
383 LinkChg = 0x0020,
384 RxOverflow = 0x0010,
385 TxErr = 0x0008,
386 TxOK = 0x0004,
387 RxErr = 0x0002,
388 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 /* RxStatusDesc */
Francois Romieu9dccf612006-05-14 12:31:17 +0200391 RxFOVF = (1 << 23),
392 RxRWT = (1 << 22),
393 RxRES = (1 << 21),
394 RxRUNT = (1 << 20),
395 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 /* ChipCmdBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100398 CmdReset = 0x10,
399 CmdRxEnb = 0x08,
400 CmdTxEnb = 0x04,
401 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Francois Romieu275391a2007-02-23 23:50:28 +0100403 /* TXPoll register p.5 */
404 HPQ = 0x80, /* Poll cmd on the high prio queue */
405 NPQ = 0x40, /* Poll cmd on the low prio queue */
406 FSWInt = 0x01, /* Forced software interrupt */
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100409 Cfg9346_Lock = 0x00,
410 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100413 AcceptErr = 0x20,
414 AcceptRunt = 0x10,
415 AcceptBroadcast = 0x08,
416 AcceptMulticast = 0x04,
417 AcceptMyPhys = 0x02,
418 AcceptAllPhys = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 /* RxConfigBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100421 RxCfgFIFOShift = 13,
422 RxCfgDMAShift = 8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 /* TxConfigBits */
425 TxInterFrameGapShift = 24,
426 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
427
Francois Romieu5d06a992006-02-23 00:47:58 +0100428 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200429 LEDS1 = (1 << 7),
430 LEDS0 = (1 << 6),
Francois Romieufbac58f2007-10-04 22:51:38 +0200431 MSIEnable = (1 << 5), /* Enable Message Signaled Interrupt */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200432 Speed_down = (1 << 4),
433 MEMMAP = (1 << 3),
434 IOMAP = (1 << 2),
435 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100436 PMEnable = (1 << 0), /* Power Management Enable */
437
Francois Romieu6dccd162007-02-13 23:38:05 +0100438 /* Config2 register p. 25 */
439 PCI_Clock_66MHz = 0x01,
440 PCI_Clock_33MHz = 0x00,
441
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100442 /* Config3 register p.25 */
443 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
444 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200445 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100446
Francois Romieu5d06a992006-02-23 00:47:58 +0100447 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100448 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
449 MWF = (1 << 5), /* Accept Multicast wakeup frame */
450 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200451 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100452 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100453 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 /* TBICSR p.28 */
456 TBIReset = 0x80000000,
457 TBILoopback = 0x40000000,
458 TBINwEnable = 0x20000000,
459 TBINwRestart = 0x10000000,
460 TBILinkOk = 0x02000000,
461 TBINwComplete = 0x01000000,
462
463 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200464 EnableBist = (1 << 15), // 8168 8101
465 Mac_dbgo_oe = (1 << 14), // 8168 8101
466 Normal_mode = (1 << 13), // unused
467 Force_half_dup = (1 << 12), // 8168 8101
468 Force_rxflow_en = (1 << 11), // 8168 8101
469 Force_txflow_en = (1 << 10), // 8168 8101
470 Cxpl_dbg_sel = (1 << 9), // 8168 8101
471 ASF = (1 << 8), // 8168 8101
472 PktCntrDisable = (1 << 7), // 8168 8101
473 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 RxVlan = (1 << 6),
475 RxChkSum = (1 << 5),
476 PCIDAC = (1 << 4),
477 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100478 INTT_0 = 0x0000, // 8168
479 INTT_1 = 0x0001, // 8168
480 INTT_2 = 0x0002, // 8168
481 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100484 TBI_Enable = 0x80,
485 TxFlowCtrl = 0x40,
486 RxFlowCtrl = 0x20,
487 _1000bpsF = 0x10,
488 _100bps = 0x08,
489 _10bps = 0x04,
490 LinkStatus = 0x02,
491 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100494 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200495
496 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100497 CounterDump = 0x8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498};
499
Francois Romieu2b7b4312011-04-18 22:53:24 -0700500enum rtl_desc_bit {
501 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
503 RingEnd = (1 << 30), /* End of descriptor ring */
504 FirstFrag = (1 << 29), /* First segment of a packet */
505 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700506};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Francois Romieu2b7b4312011-04-18 22:53:24 -0700508/* Generic case. */
509enum rtl_tx_desc_bit {
510 /* First doubleword. */
511 TD_LSO = (1 << 27), /* Large Send Offload */
512#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Francois Romieu2b7b4312011-04-18 22:53:24 -0700514 /* Second doubleword. */
515 TxVlanTag = (1 << 17), /* Add VLAN tag */
516};
517
518/* 8169, 8168b and 810x except 8102e. */
519enum rtl_tx_desc_bit_0 {
520 /* First doubleword. */
521#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
522 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
523 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
524 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
525};
526
527/* 8102e, 8168c and beyond. */
528enum rtl_tx_desc_bit_1 {
529 /* Second doubleword. */
530#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
531 TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
532 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
533 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
534};
535
536static const struct rtl_tx_desc_info {
537 struct {
538 u32 udp;
539 u32 tcp;
540 } checksum;
541 u16 mss_shift;
542 u16 opts_offset;
543} tx_desc_info [] = {
544 [RTL_TD_0] = {
545 .checksum = {
546 .udp = TD0_IP_CS | TD0_UDP_CS,
547 .tcp = TD0_IP_CS | TD0_TCP_CS
548 },
549 .mss_shift = TD0_MSS_SHIFT,
550 .opts_offset = 0
551 },
552 [RTL_TD_1] = {
553 .checksum = {
554 .udp = TD1_IP_CS | TD1_UDP_CS,
555 .tcp = TD1_IP_CS | TD1_TCP_CS
556 },
557 .mss_shift = TD1_MSS_SHIFT,
558 .opts_offset = 1
559 }
560};
561
562enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 /* Rx private */
564 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
565 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
566
567#define RxProtoUDP (PID1)
568#define RxProtoTCP (PID0)
569#define RxProtoIP (PID1 | PID0)
570#define RxProtoMask RxProtoIP
571
572 IPFail = (1 << 16), /* IP checksum failed */
573 UDPFail = (1 << 15), /* UDP/IP checksum failed */
574 TCPFail = (1 << 14), /* TCP/IP checksum failed */
575 RxVlanTag = (1 << 16), /* VLAN tag available */
576};
577
578#define RsvdMask 0x3fffc000
579
580struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200581 __le32 opts1;
582 __le32 opts2;
583 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584};
585
586struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200587 __le32 opts1;
588 __le32 opts2;
589 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590};
591
592struct ring_info {
593 struct sk_buff *skb;
594 u32 len;
595 u8 __pad[sizeof(void *) - sizeof(u32)];
596};
597
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200598enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200599 RTL_FEATURE_WOL = (1 << 0),
600 RTL_FEATURE_MSI = (1 << 1),
601 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200602};
603
Ivan Vecera355423d2009-02-06 21:49:57 -0800604struct rtl8169_counters {
605 __le64 tx_packets;
606 __le64 rx_packets;
607 __le64 tx_errors;
608 __le32 rx_errors;
609 __le16 rx_missed;
610 __le16 align_errors;
611 __le32 tx_one_collision;
612 __le32 tx_multi_collision;
613 __le64 rx_unicast;
614 __le64 rx_broadcast;
615 __le32 rx_multicast;
616 __le16 tx_aborted;
617 __le16 tx_underun;
618};
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620struct rtl8169_private {
621 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200622 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000623 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700624 struct napi_struct napi;
Francois Romieucecb5fd2011-04-01 10:21:07 +0200625 spinlock_t lock;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200626 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700627 u16 txd_version;
628 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
630 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
631 u32 dirty_rx;
632 u32 dirty_tx;
633 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
634 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
635 dma_addr_t TxPhyAddr;
636 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000637 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 struct timer_list timer;
640 u16 cp_cmd;
Francois Romieu0e485152007-02-20 00:00:26 +0100641 u16 intr_event;
642 u16 napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 u16 intr_mask;
françois romieuc0e45c12011-01-03 15:08:04 +0000644
645 struct mdio_ops {
646 void (*write)(void __iomem *, int, int);
647 int (*read)(void __iomem *, int);
648 } mdio_ops;
649
françois romieu065c27c2011-01-03 15:08:12 +0000650 struct pll_power_ops {
651 void (*down)(struct rtl8169_private *);
652 void (*up)(struct rtl8169_private *);
653 } pll_power_ops;
654
Oliver Neukum54405cd2011-01-06 21:55:13 +0100655 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Francois Romieuccdffb92008-07-26 14:26:06 +0200656 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000657 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100658 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000659 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800661 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
David Howellsc4028952006-11-22 14:57:56 +0000662 struct delayed_work task;
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200663 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200664
665 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800666 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000667 u32 saved_wolopts;
françois romieuf1e02ed2011-01-13 13:07:53 +0000668
Francois Romieub6ffd972011-06-17 17:00:05 +0200669 struct rtl_fw {
670 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200671
672#define RTL_VER_SIZE 32
673
674 char version[RTL_VER_SIZE];
675
676 struct rtl_fw_phy_action {
677 __le32 *code;
678 size_t size;
679 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200680 } *rtl_fw;
François Romieu953a12c2011-04-24 17:38:48 +0200681#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682};
683
Ralf Baechle979b6c12005-06-13 14:30:40 -0700684MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700687MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200688module_param_named(debug, debug.msg_enable, int, 0);
689MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690MODULE_LICENSE("GPL");
691MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000692MODULE_FIRMWARE(FIRMWARE_8168D_1);
693MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000694MODULE_FIRMWARE(FIRMWARE_8168E_1);
695MODULE_FIRMWARE(FIRMWARE_8168E_2);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800696MODULE_FIRMWARE(FIRMWARE_8105E_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698static int rtl8169_open(struct net_device *dev);
Stephen Hemminger613573252009-08-31 19:50:58 +0000699static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
700 struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100701static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702static int rtl8169_init_ring(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100703static void rtl_hw_start(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704static int rtl8169_close(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100705static void rtl_set_rx_mode(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706static void rtl8169_tx_timeout(struct net_device *dev);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200707static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708static int rtl8169_rx_interrupt(struct net_device *, struct rtl8169_private *,
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700709 void __iomem *, u32 budget);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200710static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711static void rtl8169_down(struct net_device *dev);
Francois Romieu99f252b2007-04-02 22:59:59 +0200712static void rtl8169_rx_clear(struct rtl8169_private *tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700713static int rtl8169_poll(struct napi_struct *napi, int budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715static const unsigned int rtl8169_rx_config =
Francois Romieu5b0384f2006-08-16 16:00:01 +0200716 (RX_FIFO_THRESH << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
françois romieub646d902011-01-03 15:08:21 +0000718static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
719{
720 void __iomem *ioaddr = tp->mmio_addr;
721 int i;
722
723 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
724 for (i = 0; i < 20; i++) {
725 udelay(100);
726 if (RTL_R32(OCPAR) & OCPAR_FLAG)
727 break;
728 }
729 return RTL_R32(OCPDR);
730}
731
732static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
733{
734 void __iomem *ioaddr = tp->mmio_addr;
735 int i;
736
737 RTL_W32(OCPDR, data);
738 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
739 for (i = 0; i < 20; i++) {
740 udelay(100);
741 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
742 break;
743 }
744}
745
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800746static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000747{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800748 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000749 int i;
750
751 RTL_W8(ERIDR, cmd);
752 RTL_W32(ERIAR, 0x800010e8);
753 msleep(2);
754 for (i = 0; i < 5; i++) {
755 udelay(100);
756 if (!(RTL_R32(ERIDR) & ERIAR_FLAG))
757 break;
758 }
759
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800760 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000761}
762
763#define OOB_CMD_RESET 0x00
764#define OOB_CMD_DRIVER_START 0x05
765#define OOB_CMD_DRIVER_STOP 0x06
766
Francois Romieucecb5fd2011-04-01 10:21:07 +0200767static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
768{
769 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
770}
771
françois romieub646d902011-01-03 15:08:21 +0000772static void rtl8168_driver_start(struct rtl8169_private *tp)
773{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200774 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000775 int i;
776
777 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
778
Francois Romieucecb5fd2011-04-01 10:21:07 +0200779 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000780
françois romieub646d902011-01-03 15:08:21 +0000781 for (i = 0; i < 10; i++) {
782 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000783 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
françois romieub646d902011-01-03 15:08:21 +0000784 break;
785 }
786}
787
788static void rtl8168_driver_stop(struct rtl8169_private *tp)
789{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200790 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000791 int i;
792
793 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
794
Francois Romieucecb5fd2011-04-01 10:21:07 +0200795 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000796
françois romieub646d902011-01-03 15:08:21 +0000797 for (i = 0; i < 10; i++) {
798 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000799 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
françois romieub646d902011-01-03 15:08:21 +0000800 break;
801 }
802}
803
hayeswang4804b3b2011-03-21 01:50:29 +0000804static int r8168dp_check_dash(struct rtl8169_private *tp)
805{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200806 u16 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000807
Francois Romieucecb5fd2011-04-01 10:21:07 +0200808 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
hayeswang4804b3b2011-03-21 01:50:29 +0000809}
françois romieub646d902011-01-03 15:08:21 +0000810
françois romieu4da19632011-01-03 15:07:55 +0000811static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
813 int i;
814
Francois Romieua6baf3a2007-11-08 23:23:21 +0100815 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Francois Romieu23714082006-01-29 00:49:09 +0100817 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100818 /*
819 * Check if the RTL8169 has completed writing to the specified
820 * MII register.
821 */
Francois Romieu5b0384f2006-08-16 16:00:01 +0200822 if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 break;
Francois Romieu23714082006-01-29 00:49:09 +0100824 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
Timo Teräs024a07b2010-06-06 15:38:47 -0700826 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700827 * According to hardware specs a 20us delay is required after write
828 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700829 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700830 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832
françois romieu4da19632011-01-03 15:07:55 +0000833static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
835 int i, value = -1;
836
Francois Romieua6baf3a2007-11-08 23:23:21 +0100837 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Francois Romieu23714082006-01-29 00:49:09 +0100839 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100840 /*
841 * Check if the RTL8169 has completed retrieving data from
842 * the specified MII register.
843 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 if (RTL_R32(PHYAR) & 0x80000000) {
Francois Romieua6baf3a2007-11-08 23:23:21 +0100845 value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 break;
847 }
Francois Romieu23714082006-01-29 00:49:09 +0100848 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
Timo Teräs81a95f02010-06-09 17:31:48 -0700850 /*
851 * According to hardware specs a 20us delay is required after read
852 * complete indication, but before sending next command.
853 */
854 udelay(20);
855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 return value;
857}
858
françois romieuc0e45c12011-01-03 15:08:04 +0000859static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
860{
861 int i;
862
863 RTL_W32(OCPDR, data |
864 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
865 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
866 RTL_W32(EPHY_RXER_NUM, 0);
867
868 for (i = 0; i < 100; i++) {
869 mdelay(1);
870 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
871 break;
872 }
873}
874
875static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
876{
877 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
878 (value & OCPDR_DATA_MASK));
879}
880
881static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
882{
883 int i;
884
885 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
886
887 mdelay(1);
888 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
889 RTL_W32(EPHY_RXER_NUM, 0);
890
891 for (i = 0; i < 100; i++) {
892 mdelay(1);
893 if (RTL_R32(OCPAR) & OCPAR_FLAG)
894 break;
895 }
896
897 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
898}
899
françois romieue6de30d2011-01-03 15:08:37 +0000900#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
901
902static void r8168dp_2_mdio_start(void __iomem *ioaddr)
903{
904 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
905}
906
907static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
908{
909 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
910}
911
912static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
913{
914 r8168dp_2_mdio_start(ioaddr);
915
916 r8169_mdio_write(ioaddr, reg_addr, value);
917
918 r8168dp_2_mdio_stop(ioaddr);
919}
920
921static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
922{
923 int value;
924
925 r8168dp_2_mdio_start(ioaddr);
926
927 value = r8169_mdio_read(ioaddr, reg_addr);
928
929 r8168dp_2_mdio_stop(ioaddr);
930
931 return value;
932}
933
françois romieu4da19632011-01-03 15:07:55 +0000934static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +0200935{
françois romieuc0e45c12011-01-03 15:08:04 +0000936 tp->mdio_ops.write(tp->mmio_addr, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +0200937}
938
françois romieu4da19632011-01-03 15:07:55 +0000939static int rtl_readphy(struct rtl8169_private *tp, int location)
940{
françois romieuc0e45c12011-01-03 15:08:04 +0000941 return tp->mdio_ops.read(tp->mmio_addr, location);
françois romieu4da19632011-01-03 15:07:55 +0000942}
943
944static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
945{
946 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
947}
948
949static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +0000950{
951 int val;
952
françois romieu4da19632011-01-03 15:07:55 +0000953 val = rtl_readphy(tp, reg_addr);
954 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +0000955}
956
Francois Romieuccdffb92008-07-26 14:26:06 +0200957static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
958 int val)
959{
960 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +0200961
françois romieu4da19632011-01-03 15:07:55 +0000962 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +0200963}
964
965static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
966{
967 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +0200968
françois romieu4da19632011-01-03 15:07:55 +0000969 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +0200970}
971
Francois Romieudacf8152008-08-02 20:44:13 +0200972static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
973{
974 unsigned int i;
975
976 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
977 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
978
979 for (i = 0; i < 100; i++) {
980 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
981 break;
982 udelay(10);
983 }
984}
985
986static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
987{
988 u16 value = 0xffff;
989 unsigned int i;
990
991 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
992
993 for (i = 0; i < 100; i++) {
994 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
995 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
996 break;
997 }
998 udelay(10);
999 }
1000
1001 return value;
1002}
1003
1004static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
1005{
1006 unsigned int i;
1007
1008 RTL_W32(CSIDR, value);
1009 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
1010 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1011
1012 for (i = 0; i < 100; i++) {
1013 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
1014 break;
1015 udelay(10);
1016 }
1017}
1018
1019static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
1020{
1021 u32 value = ~0x00;
1022 unsigned int i;
1023
1024 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
1025 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1026
1027 for (i = 0; i < 100; i++) {
1028 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
1029 value = RTL_R32(CSIDR);
1030 break;
1031 }
1032 udelay(10);
1033 }
1034
1035 return value;
1036}
1037
françois romieudaf9df62009-10-07 12:44:20 +00001038static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1039{
1040 u8 value = 0xff;
1041 unsigned int i;
1042
1043 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1044
1045 for (i = 0; i < 300; i++) {
1046 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1047 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1048 break;
1049 }
1050 udelay(100);
1051 }
1052
1053 return value;
1054}
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056static void rtl8169_irq_mask_and_ack(void __iomem *ioaddr)
1057{
1058 RTL_W16(IntrMask, 0x0000);
1059
1060 RTL_W16(IntrStatus, 0xffff);
1061}
1062
1063static void rtl8169_asic_down(void __iomem *ioaddr)
1064{
1065 RTL_W8(ChipCmd, 0x00);
1066 rtl8169_irq_mask_and_ack(ioaddr);
1067 RTL_R16(CPlusCmd);
1068}
1069
françois romieu4da19632011-01-03 15:07:55 +00001070static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
françois romieu4da19632011-01-03 15:07:55 +00001072 void __iomem *ioaddr = tp->mmio_addr;
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 return RTL_R32(TBICSR) & TBIReset;
1075}
1076
françois romieu4da19632011-01-03 15:07:55 +00001077static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
françois romieu4da19632011-01-03 15:07:55 +00001079 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}
1081
1082static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1083{
1084 return RTL_R32(TBICSR) & TBILinkOk;
1085}
1086
1087static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1088{
1089 return RTL_R8(PHYstatus) & LinkStatus;
1090}
1091
françois romieu4da19632011-01-03 15:07:55 +00001092static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
françois romieu4da19632011-01-03 15:07:55 +00001094 void __iomem *ioaddr = tp->mmio_addr;
1095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1097}
1098
françois romieu4da19632011-01-03 15:07:55 +00001099static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100{
1101 unsigned int val;
1102
françois romieu4da19632011-01-03 15:07:55 +00001103 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1104 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
1106
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001107static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001108 struct rtl8169_private *tp,
1109 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
1111 unsigned long flags;
1112
1113 spin_lock_irqsave(&tp->lock, flags);
1114 if (tp->link_ok(ioaddr)) {
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001115 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001116 if (pm)
1117 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001119 if (net_ratelimit())
1120 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001121 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001123 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001124 if (pm)
1125 pm_schedule_suspend(&tp->pci_dev->dev, 100);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 spin_unlock_irqrestore(&tp->lock, flags);
1128}
1129
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001130static void rtl8169_check_link_status(struct net_device *dev,
1131 struct rtl8169_private *tp,
1132 void __iomem *ioaddr)
1133{
1134 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1135}
1136
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001137#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1138
1139static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1140{
1141 void __iomem *ioaddr = tp->mmio_addr;
1142 u8 options;
1143 u32 wolopts = 0;
1144
1145 options = RTL_R8(Config1);
1146 if (!(options & PMEnable))
1147 return 0;
1148
1149 options = RTL_R8(Config3);
1150 if (options & LinkUp)
1151 wolopts |= WAKE_PHY;
1152 if (options & MagicPacket)
1153 wolopts |= WAKE_MAGIC;
1154
1155 options = RTL_R8(Config5);
1156 if (options & UWF)
1157 wolopts |= WAKE_UCAST;
1158 if (options & BWF)
1159 wolopts |= WAKE_BCAST;
1160 if (options & MWF)
1161 wolopts |= WAKE_MCAST;
1162
1163 return wolopts;
1164}
1165
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001166static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1167{
1168 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001169
1170 spin_lock_irq(&tp->lock);
1171
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001172 wol->supported = WAKE_ANY;
1173 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001174
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001175 spin_unlock_irq(&tp->lock);
1176}
1177
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001178static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001179{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001180 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001181 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001182 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001183 u32 opt;
1184 u16 reg;
1185 u8 mask;
1186 } cfg[] = {
1187 { WAKE_ANY, Config1, PMEnable },
1188 { WAKE_PHY, Config3, LinkUp },
1189 { WAKE_MAGIC, Config3, MagicPacket },
1190 { WAKE_UCAST, Config5, UWF },
1191 { WAKE_BCAST, Config5, BWF },
1192 { WAKE_MCAST, Config5, MWF },
1193 { WAKE_ANY, Config5, LanWake }
1194 };
1195
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001196 RTL_W8(Cfg9346, Cfg9346_Unlock);
1197
1198 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1199 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001200 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001201 options |= cfg[i].mask;
1202 RTL_W8(cfg[i].reg, options);
1203 }
1204
1205 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001206}
1207
1208static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1209{
1210 struct rtl8169_private *tp = netdev_priv(dev);
1211
1212 spin_lock_irq(&tp->lock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001213
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001214 if (wol->wolopts)
1215 tp->features |= RTL_FEATURE_WOL;
1216 else
1217 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001218 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001219 spin_unlock_irq(&tp->lock);
1220
françois romieuea809072010-11-08 13:23:58 +00001221 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1222
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001223 return 0;
1224}
1225
Francois Romieu31bd2042011-04-26 18:58:59 +02001226static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1227{
Francois Romieu85bffe62011-04-27 08:22:39 +02001228 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001229}
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231static void rtl8169_get_drvinfo(struct net_device *dev,
1232 struct ethtool_drvinfo *info)
1233{
1234 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001235 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237 strcpy(info->driver, MODULENAME);
1238 strcpy(info->version, RTL8169_VERSION);
1239 strcpy(info->bus_info, pci_name(tp->pci_dev));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001240 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
1241 strcpy(info->fw_version, IS_ERR_OR_NULL(rtl_fw) ? "N/A" :
1242 rtl_fw->version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
1244
1245static int rtl8169_get_regs_len(struct net_device *dev)
1246{
1247 return R8169_REGS_SIZE;
1248}
1249
1250static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001251 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
1253 struct rtl8169_private *tp = netdev_priv(dev);
1254 void __iomem *ioaddr = tp->mmio_addr;
1255 int ret = 0;
1256 u32 reg;
1257
1258 reg = RTL_R32(TBICSR);
1259 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1260 (duplex == DUPLEX_FULL)) {
1261 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1262 } else if (autoneg == AUTONEG_ENABLE)
1263 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1264 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001265 netif_warn(tp, link, dev,
1266 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 ret = -EOPNOTSUPP;
1268 }
1269
1270 return ret;
1271}
1272
1273static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001274 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
1276 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001277 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001278 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Hayes Wang716b50a2011-02-22 17:26:18 +08001280 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001283 int auto_nego;
1284
françois romieu4da19632011-01-03 15:07:55 +00001285 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001286 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1287 ADVERTISE_100HALF | ADVERTISE_100FULL);
1288
1289 if (adv & ADVERTISED_10baseT_Half)
1290 auto_nego |= ADVERTISE_10HALF;
1291 if (adv & ADVERTISED_10baseT_Full)
1292 auto_nego |= ADVERTISE_10FULL;
1293 if (adv & ADVERTISED_100baseT_Half)
1294 auto_nego |= ADVERTISE_100HALF;
1295 if (adv & ADVERTISED_100baseT_Full)
1296 auto_nego |= ADVERTISE_100FULL;
1297
françois romieu3577aa12009-05-19 10:46:48 +00001298 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1299
françois romieu4da19632011-01-03 15:07:55 +00001300 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001301 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1302
1303 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001304 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001305 if (adv & ADVERTISED_1000baseT_Half)
1306 giga_ctrl |= ADVERTISE_1000HALF;
1307 if (adv & ADVERTISED_1000baseT_Full)
1308 giga_ctrl |= ADVERTISE_1000FULL;
1309 } else if (adv & (ADVERTISED_1000baseT_Half |
1310 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001311 netif_info(tp, link, dev,
1312 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001313 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
françois romieu3577aa12009-05-19 10:46:48 +00001316 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001317
françois romieu4da19632011-01-03 15:07:55 +00001318 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1319 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001320 } else {
1321 giga_ctrl = 0;
1322
1323 if (speed == SPEED_10)
1324 bmcr = 0;
1325 else if (speed == SPEED_100)
1326 bmcr = BMCR_SPEED100;
1327 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001328 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001329
1330 if (duplex == DUPLEX_FULL)
1331 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001332 }
1333
françois romieu4da19632011-01-03 15:07:55 +00001334 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001335
Francois Romieucecb5fd2011-04-01 10:21:07 +02001336 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1337 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001338 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001339 rtl_writephy(tp, 0x17, 0x2138);
1340 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001341 } else {
françois romieu4da19632011-01-03 15:07:55 +00001342 rtl_writephy(tp, 0x17, 0x2108);
1343 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001344 }
1345 }
1346
Oliver Neukum54405cd2011-01-06 21:55:13 +01001347 rc = 0;
1348out:
1349 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350}
1351
1352static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001353 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
1355 struct rtl8169_private *tp = netdev_priv(dev);
1356 int ret;
1357
Oliver Neukum54405cd2011-01-06 21:55:13 +01001358 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001359 if (ret < 0)
1360 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Francois Romieu4876cc12011-03-11 21:07:11 +01001362 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1363 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001365 }
1366out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 return ret;
1368}
1369
1370static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1371{
1372 struct rtl8169_private *tp = netdev_priv(dev);
1373 unsigned long flags;
1374 int ret;
1375
Francois Romieu4876cc12011-03-11 21:07:11 +01001376 del_timer_sync(&tp->timer);
1377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 spin_lock_irqsave(&tp->lock, flags);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001379 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001380 cmd->duplex, cmd->advertising);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 return ret;
1384}
1385
Michał Mirosław350fb322011-04-08 06:35:56 +00001386static u32 rtl8169_fix_features(struct net_device *dev, u32 features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
Francois Romieu2b7b4312011-04-18 22:53:24 -07001388 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001389 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Michał Mirosław350fb322011-04-08 06:35:56 +00001391 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392}
1393
Michał Mirosław350fb322011-04-08 06:35:56 +00001394static int rtl8169_set_features(struct net_device *dev, u32 features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
1396 struct rtl8169_private *tp = netdev_priv(dev);
1397 void __iomem *ioaddr = tp->mmio_addr;
1398 unsigned long flags;
1399
1400 spin_lock_irqsave(&tp->lock, flags);
1401
Michał Mirosław350fb322011-04-08 06:35:56 +00001402 if (features & NETIF_F_RXCSUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 tp->cp_cmd |= RxChkSum;
1404 else
1405 tp->cp_cmd &= ~RxChkSum;
1406
Michał Mirosław350fb322011-04-08 06:35:56 +00001407 if (dev->features & NETIF_F_HW_VLAN_RX)
1408 tp->cp_cmd |= RxVlan;
1409 else
1410 tp->cp_cmd &= ~RxVlan;
1411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 RTL_W16(CPlusCmd, tp->cp_cmd);
1413 RTL_R16(CPlusCmd);
1414
1415 spin_unlock_irqrestore(&tp->lock, flags);
1416
1417 return 0;
1418}
1419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1421 struct sk_buff *skb)
1422{
Jesse Grosseab6d182010-10-20 13:56:03 +00001423 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1425}
1426
Francois Romieu7a8fc772011-03-01 17:18:33 +01001427static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
1429 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Francois Romieu7a8fc772011-03-01 17:18:33 +01001431 if (opts2 & RxVlanTag)
1432 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435}
1436
Francois Romieuccdffb92008-07-26 14:26:06 +02001437static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
1439 struct rtl8169_private *tp = netdev_priv(dev);
1440 void __iomem *ioaddr = tp->mmio_addr;
1441 u32 status;
1442
1443 cmd->supported =
1444 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1445 cmd->port = PORT_FIBRE;
1446 cmd->transceiver = XCVR_INTERNAL;
1447
1448 status = RTL_R32(TBICSR);
1449 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1450 cmd->autoneg = !!(status & TBINwEnable);
1451
David Decotigny70739492011-04-27 18:32:40 +00001452 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001454
1455 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456}
1457
Francois Romieuccdffb92008-07-26 14:26:06 +02001458static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
1460 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Francois Romieuccdffb92008-07-26 14:26:06 +02001462 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463}
1464
1465static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1466{
1467 struct rtl8169_private *tp = netdev_priv(dev);
1468 unsigned long flags;
Francois Romieuccdffb92008-07-26 14:26:06 +02001469 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
1471 spin_lock_irqsave(&tp->lock, flags);
1472
Francois Romieuccdffb92008-07-26 14:26:06 +02001473 rc = tp->get_settings(dev, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
1475 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieuccdffb92008-07-26 14:26:06 +02001476 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477}
1478
1479static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1480 void *p)
1481{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001482 struct rtl8169_private *tp = netdev_priv(dev);
1483 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Francois Romieu5b0384f2006-08-16 16:00:01 +02001485 if (regs->len > R8169_REGS_SIZE)
1486 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Francois Romieu5b0384f2006-08-16 16:00:01 +02001488 spin_lock_irqsave(&tp->lock, flags);
1489 memcpy_fromio(p, tp->mmio_addr, regs->len);
1490 spin_unlock_irqrestore(&tp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491}
1492
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001493static u32 rtl8169_get_msglevel(struct net_device *dev)
1494{
1495 struct rtl8169_private *tp = netdev_priv(dev);
1496
1497 return tp->msg_enable;
1498}
1499
1500static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1501{
1502 struct rtl8169_private *tp = netdev_priv(dev);
1503
1504 tp->msg_enable = value;
1505}
1506
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001507static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1508 "tx_packets",
1509 "rx_packets",
1510 "tx_errors",
1511 "rx_errors",
1512 "rx_missed",
1513 "align_errors",
1514 "tx_single_collisions",
1515 "tx_multi_collisions",
1516 "unicast",
1517 "broadcast",
1518 "multicast",
1519 "tx_aborted",
1520 "tx_underrun",
1521};
1522
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001523static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001524{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001525 switch (sset) {
1526 case ETH_SS_STATS:
1527 return ARRAY_SIZE(rtl8169_gstrings);
1528 default:
1529 return -EOPNOTSUPP;
1530 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001531}
1532
Ivan Vecera355423d2009-02-06 21:49:57 -08001533static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001534{
1535 struct rtl8169_private *tp = netdev_priv(dev);
1536 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001537 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001538 struct rtl8169_counters *counters;
1539 dma_addr_t paddr;
1540 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001541 int wait = 1000;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001542
Ivan Vecera355423d2009-02-06 21:49:57 -08001543 /*
1544 * Some chips are unable to dump tally counters when the receiver
1545 * is disabled.
1546 */
1547 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1548 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001549
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001550 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001551 if (!counters)
1552 return;
1553
1554 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001555 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001556 RTL_W32(CounterAddrLow, cmd);
1557 RTL_W32(CounterAddrLow, cmd | CounterDump);
1558
Ivan Vecera355423d2009-02-06 21:49:57 -08001559 while (wait--) {
1560 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
Ivan Vecera355423d2009-02-06 21:49:57 -08001561 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001562 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001563 }
1564 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001565 }
1566
1567 RTL_W32(CounterAddrLow, 0);
1568 RTL_W32(CounterAddrHigh, 0);
1569
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001570 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001571}
1572
Ivan Vecera355423d2009-02-06 21:49:57 -08001573static void rtl8169_get_ethtool_stats(struct net_device *dev,
1574 struct ethtool_stats *stats, u64 *data)
1575{
1576 struct rtl8169_private *tp = netdev_priv(dev);
1577
1578 ASSERT_RTNL();
1579
1580 rtl8169_update_counters(dev);
1581
1582 data[0] = le64_to_cpu(tp->counters.tx_packets);
1583 data[1] = le64_to_cpu(tp->counters.rx_packets);
1584 data[2] = le64_to_cpu(tp->counters.tx_errors);
1585 data[3] = le32_to_cpu(tp->counters.rx_errors);
1586 data[4] = le16_to_cpu(tp->counters.rx_missed);
1587 data[5] = le16_to_cpu(tp->counters.align_errors);
1588 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1589 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1590 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1591 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1592 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1593 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1594 data[12] = le16_to_cpu(tp->counters.tx_underun);
1595}
1596
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001597static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1598{
1599 switch(stringset) {
1600 case ETH_SS_STATS:
1601 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1602 break;
1603 }
1604}
1605
Jeff Garzik7282d492006-09-13 14:30:00 -04001606static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 .get_drvinfo = rtl8169_get_drvinfo,
1608 .get_regs_len = rtl8169_get_regs_len,
1609 .get_link = ethtool_op_get_link,
1610 .get_settings = rtl8169_get_settings,
1611 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001612 .get_msglevel = rtl8169_get_msglevel,
1613 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001615 .get_wol = rtl8169_get_wol,
1616 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001617 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001618 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001619 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620};
1621
Francois Romieu07d3f512007-02-21 22:40:46 +01001622static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02001623 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
Francois Romieu5d320a22011-05-08 17:47:36 +02001625 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01001626 /*
1627 * The driver currently handles the 8168Bf and the 8168Be identically
1628 * but they can be identified more specifically through the test below
1629 * if needed:
1630 *
1631 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001632 *
1633 * Same thing for the 8101Eb and the 8101Ec:
1634 *
1635 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001636 */
Francois Romieu37441002011-06-17 22:58:54 +02001637 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001639 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 int mac_version;
1641 } mac_info[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00001642 /* 8168E family. */
1643 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1644 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1645 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1646
Francois Romieu5b538df2008-07-20 16:22:45 +02001647 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001648 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1649 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001650 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001651
françois romieue6de30d2011-01-03 15:08:37 +00001652 /* 8168DP family. */
1653 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1654 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00001655 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00001656
Francois Romieuef808d52008-06-29 13:10:54 +02001657 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001658 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001659 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001660 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001661 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001662 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1663 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001664 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001665 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001666 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001667
1668 /* 8168B family. */
1669 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1670 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1671 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1672 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1673
1674 /* 8101 family. */
hayeswang36a0e6c2011-03-21 01:50:30 +00001675 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08001676 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1677 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1678 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001679 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1680 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1681 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1682 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1683 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1684 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001685 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001686 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001687 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001688 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1689 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001690 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1691 /* FIXME: where did these entries come from ? -- FR */
1692 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1693 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1694
1695 /* 8110 family. */
1696 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1697 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1698 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1699 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1700 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1701 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1702
Jean Delvaref21b75e2009-05-26 20:54:48 -07001703 /* Catch-all */
1704 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02001705 };
1706 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 u32 reg;
1708
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001709 reg = RTL_R32(TxConfig);
1710 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 p++;
1712 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02001713
1714 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1715 netif_notice(tp, probe, dev,
1716 "unknown MAC, using family default\n");
1717 tp->mac_version = default_version;
1718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719}
1720
1721static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1722{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001723 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724}
1725
Francois Romieu867763c2007-08-17 18:21:58 +02001726struct phy_reg {
1727 u16 reg;
1728 u16 val;
1729};
1730
françois romieu4da19632011-01-03 15:07:55 +00001731static void rtl_writephy_batch(struct rtl8169_private *tp,
1732 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02001733{
1734 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00001735 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02001736 regs++;
1737 }
1738}
1739
françois romieubca03d52011-01-03 15:07:31 +00001740#define PHY_READ 0x00000000
1741#define PHY_DATA_OR 0x10000000
1742#define PHY_DATA_AND 0x20000000
1743#define PHY_BJMPN 0x30000000
1744#define PHY_READ_EFUSE 0x40000000
1745#define PHY_READ_MAC_BYTE 0x50000000
1746#define PHY_WRITE_MAC_BYTE 0x60000000
1747#define PHY_CLEAR_READCOUNT 0x70000000
1748#define PHY_WRITE 0x80000000
1749#define PHY_READCOUNT_EQ_SKIP 0x90000000
1750#define PHY_COMP_EQ_SKIPN 0xa0000000
1751#define PHY_COMP_NEQ_SKIPN 0xb0000000
1752#define PHY_WRITE_PREVIOUS 0xc0000000
1753#define PHY_SKIPN 0xd0000000
1754#define PHY_DELAY_MS 0xe0000000
1755#define PHY_WRITE_ERI_WORD 0xf0000000
1756
Hayes Wang960aee62011-06-18 11:37:48 +02001757struct fw_info {
1758 u32 magic;
1759 char version[RTL_VER_SIZE];
1760 __le32 fw_start;
1761 __le32 fw_len;
1762 u8 chksum;
1763} __packed;
1764
Francois Romieu1c361ef2011-06-17 17:16:24 +02001765#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
1766
1767static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00001768{
Francois Romieub6ffd972011-06-17 17:00:05 +02001769 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02001770 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02001771 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
1772 char *version = rtl_fw->version;
1773 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00001774
Francois Romieu1c361ef2011-06-17 17:16:24 +02001775 if (fw->size < FW_OPCODE_SIZE)
1776 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02001777
1778 if (!fw_info->magic) {
1779 size_t i, size, start;
1780 u8 checksum = 0;
1781
1782 if (fw->size < sizeof(*fw_info))
1783 goto out;
1784
1785 for (i = 0; i < fw->size; i++)
1786 checksum += fw->data[i];
1787 if (checksum != 0)
1788 goto out;
1789
1790 start = le32_to_cpu(fw_info->fw_start);
1791 if (start > fw->size)
1792 goto out;
1793
1794 size = le32_to_cpu(fw_info->fw_len);
1795 if (size > (fw->size - start) / FW_OPCODE_SIZE)
1796 goto out;
1797
1798 memcpy(version, fw_info->version, RTL_VER_SIZE);
1799
1800 pa->code = (__le32 *)(fw->data + start);
1801 pa->size = size;
1802 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02001803 if (fw->size % FW_OPCODE_SIZE)
1804 goto out;
1805
1806 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
1807
1808 pa->code = (__le32 *)fw->data;
1809 pa->size = fw->size / FW_OPCODE_SIZE;
1810 }
1811 version[RTL_VER_SIZE - 1] = 0;
1812
1813 rc = true;
1814out:
1815 return rc;
1816}
1817
Francois Romieufd112f22011-06-18 00:10:29 +02001818static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
1819 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02001820{
Francois Romieufd112f22011-06-18 00:10:29 +02001821 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02001822 size_t index;
1823
Francois Romieu1c361ef2011-06-17 17:16:24 +02001824 for (index = 0; index < pa->size; index++) {
1825 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00001826 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00001827
hayeswang42b82dc2011-01-10 02:07:25 +00001828 switch(action & 0xf0000000) {
1829 case PHY_READ:
1830 case PHY_DATA_OR:
1831 case PHY_DATA_AND:
1832 case PHY_READ_EFUSE:
1833 case PHY_CLEAR_READCOUNT:
1834 case PHY_WRITE:
1835 case PHY_WRITE_PREVIOUS:
1836 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00001837 break;
1838
hayeswang42b82dc2011-01-10 02:07:25 +00001839 case PHY_BJMPN:
1840 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02001841 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001842 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02001843 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00001844 }
1845 break;
1846 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02001847 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02001848 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001849 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02001850 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00001851 }
1852 break;
1853 case PHY_COMP_EQ_SKIPN:
1854 case PHY_COMP_NEQ_SKIPN:
1855 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02001856 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02001857 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001858 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02001859 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00001860 }
1861 break;
1862
1863 case PHY_READ_MAC_BYTE:
1864 case PHY_WRITE_MAC_BYTE:
1865 case PHY_WRITE_ERI_WORD:
1866 default:
Francois Romieufd112f22011-06-18 00:10:29 +02001867 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00001868 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02001869 goto out;
françois romieubca03d52011-01-03 15:07:31 +00001870 }
1871 }
Francois Romieufd112f22011-06-18 00:10:29 +02001872 rc = true;
1873out:
1874 return rc;
1875}
françois romieubca03d52011-01-03 15:07:31 +00001876
Francois Romieufd112f22011-06-18 00:10:29 +02001877static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
1878{
1879 struct net_device *dev = tp->dev;
1880 int rc = -EINVAL;
1881
1882 if (!rtl_fw_format_ok(tp, rtl_fw)) {
1883 netif_err(tp, ifup, dev, "invalid firwmare\n");
1884 goto out;
1885 }
1886
1887 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
1888 rc = 0;
1889out:
1890 return rc;
1891}
1892
1893static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
1894{
1895 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
1896 u32 predata, count;
1897 size_t index;
1898
1899 predata = count = 0;
hayeswang42b82dc2011-01-10 02:07:25 +00001900
Francois Romieu1c361ef2011-06-17 17:16:24 +02001901 for (index = 0; index < pa->size; ) {
1902 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00001903 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00001904 u32 regno = (action & 0x0fff0000) >> 16;
1905
1906 if (!action)
1907 break;
françois romieubca03d52011-01-03 15:07:31 +00001908
1909 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00001910 case PHY_READ:
1911 predata = rtl_readphy(tp, regno);
1912 count++;
1913 index++;
françois romieubca03d52011-01-03 15:07:31 +00001914 break;
hayeswang42b82dc2011-01-10 02:07:25 +00001915 case PHY_DATA_OR:
1916 predata |= data;
1917 index++;
1918 break;
1919 case PHY_DATA_AND:
1920 predata &= data;
1921 index++;
1922 break;
1923 case PHY_BJMPN:
1924 index -= regno;
1925 break;
1926 case PHY_READ_EFUSE:
1927 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
1928 index++;
1929 break;
1930 case PHY_CLEAR_READCOUNT:
1931 count = 0;
1932 index++;
1933 break;
1934 case PHY_WRITE:
1935 rtl_writephy(tp, regno, data);
1936 index++;
1937 break;
1938 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02001939 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00001940 break;
1941 case PHY_COMP_EQ_SKIPN:
1942 if (predata == data)
1943 index += regno;
1944 index++;
1945 break;
1946 case PHY_COMP_NEQ_SKIPN:
1947 if (predata != data)
1948 index += regno;
1949 index++;
1950 break;
1951 case PHY_WRITE_PREVIOUS:
1952 rtl_writephy(tp, regno, predata);
1953 index++;
1954 break;
1955 case PHY_SKIPN:
1956 index += regno + 1;
1957 break;
1958 case PHY_DELAY_MS:
1959 mdelay(data);
1960 index++;
1961 break;
1962
1963 case PHY_READ_MAC_BYTE:
1964 case PHY_WRITE_MAC_BYTE:
1965 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00001966 default:
1967 BUG();
1968 }
1969 }
1970}
1971
françois romieuf1e02ed2011-01-13 13:07:53 +00001972static void rtl_release_firmware(struct rtl8169_private *tp)
1973{
Francois Romieub6ffd972011-06-17 17:00:05 +02001974 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
1975 release_firmware(tp->rtl_fw->fw);
1976 kfree(tp->rtl_fw);
1977 }
1978 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00001979}
1980
François Romieu953a12c2011-04-24 17:38:48 +02001981static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00001982{
Francois Romieub6ffd972011-06-17 17:00:05 +02001983 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00001984
1985 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieub6ffd972011-06-17 17:00:05 +02001986 if (!IS_ERR_OR_NULL(rtl_fw))
1987 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02001988}
1989
1990static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
1991{
1992 if (rtl_readphy(tp, reg) != val)
1993 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
1994 else
1995 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00001996}
1997
françois romieu4da19632011-01-03 15:07:55 +00001998static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002000 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002001 { 0x1f, 0x0001 },
2002 { 0x06, 0x006e },
2003 { 0x08, 0x0708 },
2004 { 0x15, 0x4000 },
2005 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
françois romieu0b9b5712009-08-10 19:44:56 +00002007 { 0x1f, 0x0001 },
2008 { 0x03, 0x00a1 },
2009 { 0x02, 0x0008 },
2010 { 0x01, 0x0120 },
2011 { 0x00, 0x1000 },
2012 { 0x04, 0x0800 },
2013 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
françois romieu0b9b5712009-08-10 19:44:56 +00002015 { 0x03, 0xff41 },
2016 { 0x02, 0xdf60 },
2017 { 0x01, 0x0140 },
2018 { 0x00, 0x0077 },
2019 { 0x04, 0x7800 },
2020 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
françois romieu0b9b5712009-08-10 19:44:56 +00002022 { 0x03, 0x802f },
2023 { 0x02, 0x4f02 },
2024 { 0x01, 0x0409 },
2025 { 0x00, 0xf0f9 },
2026 { 0x04, 0x9800 },
2027 { 0x04, 0x9000 },
2028
2029 { 0x03, 0xdf01 },
2030 { 0x02, 0xdf20 },
2031 { 0x01, 0xff95 },
2032 { 0x00, 0xba00 },
2033 { 0x04, 0xa800 },
2034 { 0x04, 0xa000 },
2035
2036 { 0x03, 0xff41 },
2037 { 0x02, 0xdf20 },
2038 { 0x01, 0x0140 },
2039 { 0x00, 0x00bb },
2040 { 0x04, 0xb800 },
2041 { 0x04, 0xb000 },
2042
2043 { 0x03, 0xdf41 },
2044 { 0x02, 0xdc60 },
2045 { 0x01, 0x6340 },
2046 { 0x00, 0x007d },
2047 { 0x04, 0xd800 },
2048 { 0x04, 0xd000 },
2049
2050 { 0x03, 0xdf01 },
2051 { 0x02, 0xdf20 },
2052 { 0x01, 0x100a },
2053 { 0x00, 0xa0ff },
2054 { 0x04, 0xf800 },
2055 { 0x04, 0xf000 },
2056
2057 { 0x1f, 0x0000 },
2058 { 0x0b, 0x0000 },
2059 { 0x00, 0x9200 }
2060 };
2061
françois romieu4da19632011-01-03 15:07:55 +00002062 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063}
2064
françois romieu4da19632011-01-03 15:07:55 +00002065static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002066{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002067 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002068 { 0x1f, 0x0002 },
2069 { 0x01, 0x90d0 },
2070 { 0x1f, 0x0000 }
2071 };
2072
françois romieu4da19632011-01-03 15:07:55 +00002073 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002074}
2075
françois romieu4da19632011-01-03 15:07:55 +00002076static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002077{
2078 struct pci_dev *pdev = tp->pci_dev;
2079 u16 vendor_id, device_id;
2080
2081 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &vendor_id);
2082 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &device_id);
2083
2084 if ((vendor_id != PCI_VENDOR_ID_GIGABYTE) || (device_id != 0xe000))
2085 return;
2086
françois romieu4da19632011-01-03 15:07:55 +00002087 rtl_writephy(tp, 0x1f, 0x0001);
2088 rtl_writephy(tp, 0x10, 0xf01b);
2089 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002090}
2091
françois romieu4da19632011-01-03 15:07:55 +00002092static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002093{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002094 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002095 { 0x1f, 0x0001 },
2096 { 0x04, 0x0000 },
2097 { 0x03, 0x00a1 },
2098 { 0x02, 0x0008 },
2099 { 0x01, 0x0120 },
2100 { 0x00, 0x1000 },
2101 { 0x04, 0x0800 },
2102 { 0x04, 0x9000 },
2103 { 0x03, 0x802f },
2104 { 0x02, 0x4f02 },
2105 { 0x01, 0x0409 },
2106 { 0x00, 0xf099 },
2107 { 0x04, 0x9800 },
2108 { 0x04, 0xa000 },
2109 { 0x03, 0xdf01 },
2110 { 0x02, 0xdf20 },
2111 { 0x01, 0xff95 },
2112 { 0x00, 0xba00 },
2113 { 0x04, 0xa800 },
2114 { 0x04, 0xf000 },
2115 { 0x03, 0xdf01 },
2116 { 0x02, 0xdf20 },
2117 { 0x01, 0x101a },
2118 { 0x00, 0xa0ff },
2119 { 0x04, 0xf800 },
2120 { 0x04, 0x0000 },
2121 { 0x1f, 0x0000 },
2122
2123 { 0x1f, 0x0001 },
2124 { 0x10, 0xf41b },
2125 { 0x14, 0xfb54 },
2126 { 0x18, 0xf5c7 },
2127 { 0x1f, 0x0000 },
2128
2129 { 0x1f, 0x0001 },
2130 { 0x17, 0x0cc0 },
2131 { 0x1f, 0x0000 }
2132 };
2133
françois romieu4da19632011-01-03 15:07:55 +00002134 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002135
françois romieu4da19632011-01-03 15:07:55 +00002136 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002137}
2138
françois romieu4da19632011-01-03 15:07:55 +00002139static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002140{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002141 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002142 { 0x1f, 0x0001 },
2143 { 0x04, 0x0000 },
2144 { 0x03, 0x00a1 },
2145 { 0x02, 0x0008 },
2146 { 0x01, 0x0120 },
2147 { 0x00, 0x1000 },
2148 { 0x04, 0x0800 },
2149 { 0x04, 0x9000 },
2150 { 0x03, 0x802f },
2151 { 0x02, 0x4f02 },
2152 { 0x01, 0x0409 },
2153 { 0x00, 0xf099 },
2154 { 0x04, 0x9800 },
2155 { 0x04, 0xa000 },
2156 { 0x03, 0xdf01 },
2157 { 0x02, 0xdf20 },
2158 { 0x01, 0xff95 },
2159 { 0x00, 0xba00 },
2160 { 0x04, 0xa800 },
2161 { 0x04, 0xf000 },
2162 { 0x03, 0xdf01 },
2163 { 0x02, 0xdf20 },
2164 { 0x01, 0x101a },
2165 { 0x00, 0xa0ff },
2166 { 0x04, 0xf800 },
2167 { 0x04, 0x0000 },
2168 { 0x1f, 0x0000 },
2169
2170 { 0x1f, 0x0001 },
2171 { 0x0b, 0x8480 },
2172 { 0x1f, 0x0000 },
2173
2174 { 0x1f, 0x0001 },
2175 { 0x18, 0x67c7 },
2176 { 0x04, 0x2000 },
2177 { 0x03, 0x002f },
2178 { 0x02, 0x4360 },
2179 { 0x01, 0x0109 },
2180 { 0x00, 0x3022 },
2181 { 0x04, 0x2800 },
2182 { 0x1f, 0x0000 },
2183
2184 { 0x1f, 0x0001 },
2185 { 0x17, 0x0cc0 },
2186 { 0x1f, 0x0000 }
2187 };
2188
françois romieu4da19632011-01-03 15:07:55 +00002189 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002190}
2191
françois romieu4da19632011-01-03 15:07:55 +00002192static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002193{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002194 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002195 { 0x10, 0xf41b },
2196 { 0x1f, 0x0000 }
2197 };
2198
françois romieu4da19632011-01-03 15:07:55 +00002199 rtl_writephy(tp, 0x1f, 0x0001);
2200 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002201
françois romieu4da19632011-01-03 15:07:55 +00002202 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002203}
2204
françois romieu4da19632011-01-03 15:07:55 +00002205static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002206{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002207 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002208 { 0x1f, 0x0001 },
2209 { 0x10, 0xf41b },
2210 { 0x1f, 0x0000 }
2211 };
2212
françois romieu4da19632011-01-03 15:07:55 +00002213 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002214}
2215
françois romieu4da19632011-01-03 15:07:55 +00002216static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002217{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002218 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002219 { 0x1f, 0x0000 },
2220 { 0x1d, 0x0f00 },
2221 { 0x1f, 0x0002 },
2222 { 0x0c, 0x1ec8 },
2223 { 0x1f, 0x0000 }
2224 };
2225
françois romieu4da19632011-01-03 15:07:55 +00002226 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002227}
2228
françois romieu4da19632011-01-03 15:07:55 +00002229static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002230{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002231 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002232 { 0x1f, 0x0001 },
2233 { 0x1d, 0x3d98 },
2234 { 0x1f, 0x0000 }
2235 };
2236
françois romieu4da19632011-01-03 15:07:55 +00002237 rtl_writephy(tp, 0x1f, 0x0000);
2238 rtl_patchphy(tp, 0x14, 1 << 5);
2239 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002240
françois romieu4da19632011-01-03 15:07:55 +00002241 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002242}
2243
françois romieu4da19632011-01-03 15:07:55 +00002244static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002245{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002246 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002247 { 0x1f, 0x0001 },
2248 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002249 { 0x1f, 0x0002 },
2250 { 0x00, 0x88d4 },
2251 { 0x01, 0x82b1 },
2252 { 0x03, 0x7002 },
2253 { 0x08, 0x9e30 },
2254 { 0x09, 0x01f0 },
2255 { 0x0a, 0x5500 },
2256 { 0x0c, 0x00c8 },
2257 { 0x1f, 0x0003 },
2258 { 0x12, 0xc096 },
2259 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002260 { 0x1f, 0x0000 },
2261 { 0x1f, 0x0000 },
2262 { 0x09, 0x2000 },
2263 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002264 };
2265
françois romieu4da19632011-01-03 15:07:55 +00002266 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002267
françois romieu4da19632011-01-03 15:07:55 +00002268 rtl_patchphy(tp, 0x14, 1 << 5);
2269 rtl_patchphy(tp, 0x0d, 1 << 5);
2270 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002271}
2272
françois romieu4da19632011-01-03 15:07:55 +00002273static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002274{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002275 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002276 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002277 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002278 { 0x03, 0x802f },
2279 { 0x02, 0x4f02 },
2280 { 0x01, 0x0409 },
2281 { 0x00, 0xf099 },
2282 { 0x04, 0x9800 },
2283 { 0x04, 0x9000 },
2284 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002285 { 0x1f, 0x0002 },
2286 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002287 { 0x06, 0x0761 },
2288 { 0x1f, 0x0003 },
2289 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002290 { 0x1f, 0x0000 }
2291 };
2292
françois romieu4da19632011-01-03 15:07:55 +00002293 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002294
françois romieu4da19632011-01-03 15:07:55 +00002295 rtl_patchphy(tp, 0x16, 1 << 0);
2296 rtl_patchphy(tp, 0x14, 1 << 5);
2297 rtl_patchphy(tp, 0x0d, 1 << 5);
2298 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002299}
2300
françois romieu4da19632011-01-03 15:07:55 +00002301static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002302{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002303 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002304 { 0x1f, 0x0001 },
2305 { 0x12, 0x2300 },
2306 { 0x1d, 0x3d98 },
2307 { 0x1f, 0x0002 },
2308 { 0x0c, 0x7eb8 },
2309 { 0x06, 0x5461 },
2310 { 0x1f, 0x0003 },
2311 { 0x16, 0x0f0a },
2312 { 0x1f, 0x0000 }
2313 };
2314
françois romieu4da19632011-01-03 15:07:55 +00002315 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002316
françois romieu4da19632011-01-03 15:07:55 +00002317 rtl_patchphy(tp, 0x16, 1 << 0);
2318 rtl_patchphy(tp, 0x14, 1 << 5);
2319 rtl_patchphy(tp, 0x0d, 1 << 5);
2320 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002321}
2322
françois romieu4da19632011-01-03 15:07:55 +00002323static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002324{
françois romieu4da19632011-01-03 15:07:55 +00002325 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002326}
2327
françois romieubca03d52011-01-03 15:07:31 +00002328static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002329{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002330 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002331 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002332 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002333 { 0x06, 0x4064 },
2334 { 0x07, 0x2863 },
2335 { 0x08, 0x059c },
2336 { 0x09, 0x26b4 },
2337 { 0x0a, 0x6a19 },
2338 { 0x0b, 0xdcc8 },
2339 { 0x10, 0xf06d },
2340 { 0x14, 0x7f68 },
2341 { 0x18, 0x7fd9 },
2342 { 0x1c, 0xf0ff },
2343 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002344 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002345 { 0x12, 0xf49f },
2346 { 0x13, 0x070b },
2347 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002348 { 0x14, 0x94c0 },
2349
2350 /*
2351 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002352 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002353 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002354 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002355 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002356 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002357 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002358 { 0x06, 0x5561 },
2359
2360 /*
2361 * Can not link to 1Gbps with bad cable
2362 * Decrease SNR threshold form 21.07dB to 19.04dB
2363 */
2364 { 0x1f, 0x0001 },
2365 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002366
2367 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002368 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002369 };
françois romieubca03d52011-01-03 15:07:31 +00002370 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002371
françois romieu4da19632011-01-03 15:07:55 +00002372 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002373
françois romieubca03d52011-01-03 15:07:31 +00002374 /*
2375 * Rx Error Issue
2376 * Fine Tune Switching regulator parameter
2377 */
françois romieu4da19632011-01-03 15:07:55 +00002378 rtl_writephy(tp, 0x1f, 0x0002);
2379 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2380 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002381
françois romieudaf9df62009-10-07 12:44:20 +00002382 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002383 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002384 { 0x1f, 0x0002 },
2385 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002386 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002387 { 0x05, 0x8330 },
2388 { 0x06, 0x669a },
2389 { 0x1f, 0x0002 }
2390 };
2391 int val;
2392
françois romieu4da19632011-01-03 15:07:55 +00002393 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002394
françois romieu4da19632011-01-03 15:07:55 +00002395 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002396
2397 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002398 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002399 0x0065, 0x0066, 0x0067, 0x0068,
2400 0x0069, 0x006a, 0x006b, 0x006c
2401 };
2402 int i;
2403
françois romieu4da19632011-01-03 15:07:55 +00002404 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002405
2406 val &= 0xff00;
2407 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002408 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002409 }
2410 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002411 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002412 { 0x1f, 0x0002 },
2413 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002414 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002415 { 0x05, 0x8330 },
2416 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002417 };
2418
françois romieu4da19632011-01-03 15:07:55 +00002419 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002420 }
2421
françois romieubca03d52011-01-03 15:07:31 +00002422 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002423 rtl_writephy(tp, 0x1f, 0x0002);
2424 rtl_patchphy(tp, 0x0d, 0x0300);
2425 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002426
françois romieubca03d52011-01-03 15:07:31 +00002427 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002428 rtl_writephy(tp, 0x1f, 0x0002);
2429 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2430 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002431
françois romieu4da19632011-01-03 15:07:55 +00002432 rtl_writephy(tp, 0x1f, 0x0005);
2433 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002434
2435 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002436
françois romieu4da19632011-01-03 15:07:55 +00002437 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002438}
2439
françois romieubca03d52011-01-03 15:07:31 +00002440static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002441{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002442 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002443 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002444 { 0x1f, 0x0001 },
2445 { 0x06, 0x4064 },
2446 { 0x07, 0x2863 },
2447 { 0x08, 0x059c },
2448 { 0x09, 0x26b4 },
2449 { 0x0a, 0x6a19 },
2450 { 0x0b, 0xdcc8 },
2451 { 0x10, 0xf06d },
2452 { 0x14, 0x7f68 },
2453 { 0x18, 0x7fd9 },
2454 { 0x1c, 0xf0ff },
2455 { 0x1d, 0x3d9c },
2456 { 0x1f, 0x0003 },
2457 { 0x12, 0xf49f },
2458 { 0x13, 0x070b },
2459 { 0x1a, 0x05ad },
2460 { 0x14, 0x94c0 },
2461
françois romieubca03d52011-01-03 15:07:31 +00002462 /*
2463 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002464 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002465 */
françois romieudaf9df62009-10-07 12:44:20 +00002466 { 0x1f, 0x0002 },
2467 { 0x06, 0x5561 },
2468 { 0x1f, 0x0005 },
2469 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002470 { 0x06, 0x5561 },
2471
2472 /*
2473 * Can not link to 1Gbps with bad cable
2474 * Decrease SNR threshold form 21.07dB to 19.04dB
2475 */
2476 { 0x1f, 0x0001 },
2477 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002478
2479 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002480 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002481 };
françois romieubca03d52011-01-03 15:07:31 +00002482 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002483
françois romieu4da19632011-01-03 15:07:55 +00002484 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002485
2486 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002487 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002488 { 0x1f, 0x0002 },
2489 { 0x05, 0x669a },
2490 { 0x1f, 0x0005 },
2491 { 0x05, 0x8330 },
2492 { 0x06, 0x669a },
2493
2494 { 0x1f, 0x0002 }
2495 };
2496 int val;
2497
françois romieu4da19632011-01-03 15:07:55 +00002498 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002499
françois romieu4da19632011-01-03 15:07:55 +00002500 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002501 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002502 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002503 0x0065, 0x0066, 0x0067, 0x0068,
2504 0x0069, 0x006a, 0x006b, 0x006c
2505 };
2506 int i;
2507
françois romieu4da19632011-01-03 15:07:55 +00002508 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002509
2510 val &= 0xff00;
2511 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002512 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002513 }
2514 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002515 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002516 { 0x1f, 0x0002 },
2517 { 0x05, 0x2642 },
2518 { 0x1f, 0x0005 },
2519 { 0x05, 0x8330 },
2520 { 0x06, 0x2642 }
2521 };
2522
françois romieu4da19632011-01-03 15:07:55 +00002523 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002524 }
2525
françois romieubca03d52011-01-03 15:07:31 +00002526 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002527 rtl_writephy(tp, 0x1f, 0x0002);
2528 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2529 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002530
françois romieubca03d52011-01-03 15:07:31 +00002531 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002532 rtl_writephy(tp, 0x1f, 0x0002);
2533 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002534
françois romieu4da19632011-01-03 15:07:55 +00002535 rtl_writephy(tp, 0x1f, 0x0005);
2536 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002537
2538 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002539
françois romieu4da19632011-01-03 15:07:55 +00002540 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002541}
2542
françois romieu4da19632011-01-03 15:07:55 +00002543static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002544{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002545 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002546 { 0x1f, 0x0002 },
2547 { 0x10, 0x0008 },
2548 { 0x0d, 0x006c },
2549
2550 { 0x1f, 0x0000 },
2551 { 0x0d, 0xf880 },
2552
2553 { 0x1f, 0x0001 },
2554 { 0x17, 0x0cc0 },
2555
2556 { 0x1f, 0x0001 },
2557 { 0x0b, 0xa4d8 },
2558 { 0x09, 0x281c },
2559 { 0x07, 0x2883 },
2560 { 0x0a, 0x6b35 },
2561 { 0x1d, 0x3da4 },
2562 { 0x1c, 0xeffd },
2563 { 0x14, 0x7f52 },
2564 { 0x18, 0x7fc6 },
2565 { 0x08, 0x0601 },
2566 { 0x06, 0x4063 },
2567 { 0x10, 0xf074 },
2568 { 0x1f, 0x0003 },
2569 { 0x13, 0x0789 },
2570 { 0x12, 0xf4bd },
2571 { 0x1a, 0x04fd },
2572 { 0x14, 0x84b0 },
2573 { 0x1f, 0x0000 },
2574 { 0x00, 0x9200 },
2575
2576 { 0x1f, 0x0005 },
2577 { 0x01, 0x0340 },
2578 { 0x1f, 0x0001 },
2579 { 0x04, 0x4000 },
2580 { 0x03, 0x1d21 },
2581 { 0x02, 0x0c32 },
2582 { 0x01, 0x0200 },
2583 { 0x00, 0x5554 },
2584 { 0x04, 0x4800 },
2585 { 0x04, 0x4000 },
2586 { 0x04, 0xf000 },
2587 { 0x03, 0xdf01 },
2588 { 0x02, 0xdf20 },
2589 { 0x01, 0x101a },
2590 { 0x00, 0xa0ff },
2591 { 0x04, 0xf800 },
2592 { 0x04, 0xf000 },
2593 { 0x1f, 0x0000 },
2594
2595 { 0x1f, 0x0007 },
2596 { 0x1e, 0x0023 },
2597 { 0x16, 0x0000 },
2598 { 0x1f, 0x0000 }
2599 };
2600
françois romieu4da19632011-01-03 15:07:55 +00002601 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002602}
2603
françois romieue6de30d2011-01-03 15:08:37 +00002604static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2605{
2606 static const struct phy_reg phy_reg_init[] = {
2607 { 0x1f, 0x0001 },
2608 { 0x17, 0x0cc0 },
2609
2610 { 0x1f, 0x0007 },
2611 { 0x1e, 0x002d },
2612 { 0x18, 0x0040 },
2613 { 0x1f, 0x0000 }
2614 };
2615
2616 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2617 rtl_patchphy(tp, 0x0d, 1 << 5);
2618}
2619
hayeswang01dc7fe2011-03-21 01:50:28 +00002620static void rtl8168e_hw_phy_config(struct rtl8169_private *tp)
2621{
2622 static const struct phy_reg phy_reg_init[] = {
2623 /* Enable Delay cap */
2624 { 0x1f, 0x0005 },
2625 { 0x05, 0x8b80 },
2626 { 0x06, 0xc896 },
2627 { 0x1f, 0x0000 },
2628
2629 /* Channel estimation fine tune */
2630 { 0x1f, 0x0001 },
2631 { 0x0b, 0x6c20 },
2632 { 0x07, 0x2872 },
2633 { 0x1c, 0xefff },
2634 { 0x1f, 0x0003 },
2635 { 0x14, 0x6420 },
2636 { 0x1f, 0x0000 },
2637
2638 /* Update PFM & 10M TX idle timer */
2639 { 0x1f, 0x0007 },
2640 { 0x1e, 0x002f },
2641 { 0x15, 0x1919 },
2642 { 0x1f, 0x0000 },
2643
2644 { 0x1f, 0x0007 },
2645 { 0x1e, 0x00ac },
2646 { 0x18, 0x0006 },
2647 { 0x1f, 0x0000 }
2648 };
2649
Francois Romieu15ecd032011-04-27 13:52:22 -07002650 rtl_apply_firmware(tp);
2651
hayeswang01dc7fe2011-03-21 01:50:28 +00002652 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2653
2654 /* DCO enable for 10M IDLE Power */
2655 rtl_writephy(tp, 0x1f, 0x0007);
2656 rtl_writephy(tp, 0x1e, 0x0023);
2657 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2658 rtl_writephy(tp, 0x1f, 0x0000);
2659
2660 /* For impedance matching */
2661 rtl_writephy(tp, 0x1f, 0x0002);
2662 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002663 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002664
2665 /* PHY auto speed down */
2666 rtl_writephy(tp, 0x1f, 0x0007);
2667 rtl_writephy(tp, 0x1e, 0x002d);
2668 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2669 rtl_writephy(tp, 0x1f, 0x0000);
2670 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2671
2672 rtl_writephy(tp, 0x1f, 0x0005);
2673 rtl_writephy(tp, 0x05, 0x8b86);
2674 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2675 rtl_writephy(tp, 0x1f, 0x0000);
2676
2677 rtl_writephy(tp, 0x1f, 0x0005);
2678 rtl_writephy(tp, 0x05, 0x8b85);
2679 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2680 rtl_writephy(tp, 0x1f, 0x0007);
2681 rtl_writephy(tp, 0x1e, 0x0020);
2682 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2683 rtl_writephy(tp, 0x1f, 0x0006);
2684 rtl_writephy(tp, 0x00, 0x5a00);
2685 rtl_writephy(tp, 0x1f, 0x0000);
2686 rtl_writephy(tp, 0x0d, 0x0007);
2687 rtl_writephy(tp, 0x0e, 0x003c);
2688 rtl_writephy(tp, 0x0d, 0x4007);
2689 rtl_writephy(tp, 0x0e, 0x0000);
2690 rtl_writephy(tp, 0x0d, 0x0000);
2691}
2692
françois romieu4da19632011-01-03 15:07:55 +00002693static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02002694{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002695 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02002696 { 0x1f, 0x0003 },
2697 { 0x08, 0x441d },
2698 { 0x01, 0x9100 },
2699 { 0x1f, 0x0000 }
2700 };
2701
françois romieu4da19632011-01-03 15:07:55 +00002702 rtl_writephy(tp, 0x1f, 0x0000);
2703 rtl_patchphy(tp, 0x11, 1 << 12);
2704 rtl_patchphy(tp, 0x19, 1 << 13);
2705 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002706
françois romieu4da19632011-01-03 15:07:55 +00002707 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02002708}
2709
Hayes Wang5a5e4442011-02-22 17:26:21 +08002710static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
2711{
2712 static const struct phy_reg phy_reg_init[] = {
2713 { 0x1f, 0x0005 },
2714 { 0x1a, 0x0000 },
2715 { 0x1f, 0x0000 },
2716
2717 { 0x1f, 0x0004 },
2718 { 0x1c, 0x0000 },
2719 { 0x1f, 0x0000 },
2720
2721 { 0x1f, 0x0001 },
2722 { 0x15, 0x7701 },
2723 { 0x1f, 0x0000 }
2724 };
2725
2726 /* Disable ALDPS before ram code */
2727 rtl_writephy(tp, 0x1f, 0x0000);
2728 rtl_writephy(tp, 0x18, 0x0310);
2729 msleep(100);
2730
François Romieu953a12c2011-04-24 17:38:48 +02002731 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08002732
2733 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2734}
2735
Francois Romieu5615d9f2007-08-17 17:50:46 +02002736static void rtl_hw_phy_config(struct net_device *dev)
2737{
2738 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002739
2740 rtl8169_print_mac_version(tp);
2741
2742 switch (tp->mac_version) {
2743 case RTL_GIGA_MAC_VER_01:
2744 break;
2745 case RTL_GIGA_MAC_VER_02:
2746 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00002747 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002748 break;
2749 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00002750 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002751 break;
françois romieu2e9558562009-08-10 19:44:19 +00002752 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00002753 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002754 break;
françois romieu8c7006a2009-08-10 19:43:29 +00002755 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00002756 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00002757 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02002758 case RTL_GIGA_MAC_VER_07:
2759 case RTL_GIGA_MAC_VER_08:
2760 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00002761 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002762 break;
Francois Romieu236b8082008-05-30 16:11:48 +02002763 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00002764 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002765 break;
2766 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00002767 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002768 break;
2769 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00002770 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002771 break;
Francois Romieu867763c2007-08-17 18:21:58 +02002772 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00002773 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002774 break;
2775 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00002776 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002777 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02002778 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00002779 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002780 break;
Francois Romieu197ff762008-06-28 13:16:02 +02002781 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00002782 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02002783 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02002784 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00002785 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002786 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02002787 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002788 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00002789 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02002790 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02002791 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00002792 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002793 break;
2794 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00002795 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002796 break;
2797 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00002798 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02002799 break;
françois romieue6de30d2011-01-03 15:08:37 +00002800 case RTL_GIGA_MAC_VER_28:
2801 rtl8168d_4_hw_phy_config(tp);
2802 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08002803 case RTL_GIGA_MAC_VER_29:
2804 case RTL_GIGA_MAC_VER_30:
2805 rtl8105e_hw_phy_config(tp);
2806 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02002807 case RTL_GIGA_MAC_VER_31:
2808 /* None. */
2809 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00002810 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00002811 case RTL_GIGA_MAC_VER_33:
Francois Romieu15ecd032011-04-27 13:52:22 -07002812 rtl8168e_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00002813 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02002814
Francois Romieu5615d9f2007-08-17 17:50:46 +02002815 default:
2816 break;
2817 }
2818}
2819
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820static void rtl8169_phy_timer(unsigned long __opaque)
2821{
2822 struct net_device *dev = (struct net_device *)__opaque;
2823 struct rtl8169_private *tp = netdev_priv(dev);
2824 struct timer_list *timer = &tp->timer;
2825 void __iomem *ioaddr = tp->mmio_addr;
2826 unsigned long timeout = RTL8169_PHY_TIMEOUT;
2827
Francois Romieubcf0bf92006-07-26 23:14:13 +02002828 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 spin_lock_irq(&tp->lock);
2831
françois romieu4da19632011-01-03 15:07:55 +00002832 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02002833 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 * A busy loop could burn quite a few cycles on nowadays CPU.
2835 * Let's delay the execution of the timer for a few ticks.
2836 */
2837 timeout = HZ/10;
2838 goto out_mod_timer;
2839 }
2840
2841 if (tp->link_ok(ioaddr))
2842 goto out_unlock;
2843
Joe Perchesbf82c182010-02-09 11:49:50 +00002844 netif_warn(tp, link, dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
françois romieu4da19632011-01-03 15:07:55 +00002846 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
2848out_mod_timer:
2849 mod_timer(timer, jiffies + timeout);
2850out_unlock:
2851 spin_unlock_irq(&tp->lock);
2852}
2853
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854#ifdef CONFIG_NET_POLL_CONTROLLER
2855/*
2856 * Polling 'interrupt' - used by things like netconsole to send skbs
2857 * without having to re-enable interrupts. It's not called while
2858 * the interrupt routine is executing.
2859 */
2860static void rtl8169_netpoll(struct net_device *dev)
2861{
2862 struct rtl8169_private *tp = netdev_priv(dev);
2863 struct pci_dev *pdev = tp->pci_dev;
2864
2865 disable_irq(pdev->irq);
David Howells7d12e782006-10-05 14:55:46 +01002866 rtl8169_interrupt(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 enable_irq(pdev->irq);
2868}
2869#endif
2870
2871static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
2872 void __iomem *ioaddr)
2873{
2874 iounmap(ioaddr);
2875 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00002876 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 pci_disable_device(pdev);
2878 free_netdev(dev);
2879}
2880
Francois Romieubf793292006-11-01 00:53:05 +01002881static void rtl8169_phy_reset(struct net_device *dev,
2882 struct rtl8169_private *tp)
2883{
Francois Romieu07d3f512007-02-21 22:40:46 +01002884 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01002885
françois romieu4da19632011-01-03 15:07:55 +00002886 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01002887 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00002888 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01002889 return;
2890 msleep(1);
2891 }
Joe Perchesbf82c182010-02-09 11:49:50 +00002892 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01002893}
2894
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002895static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002897 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002898
Francois Romieu5615d9f2007-08-17 17:50:46 +02002899 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002900
Marcus Sundberg773328942008-07-10 21:28:08 +02002901 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
2902 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2903 RTL_W8(0x82, 0x01);
2904 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002905
Francois Romieu6dccd162007-02-13 23:38:05 +01002906 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
2907
2908 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
2909 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002910
Francois Romieubcf0bf92006-07-26 23:14:13 +02002911 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002912 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2913 RTL_W8(0x82, 0x01);
2914 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00002915 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002916 }
2917
Francois Romieubf793292006-11-01 00:53:05 +01002918 rtl8169_phy_reset(dev, tp);
2919
Oliver Neukum54405cd2011-01-06 21:55:13 +01002920 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002921 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
2922 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
2923 (tp->mii.supports_gmii ?
2924 ADVERTISED_1000baseT_Half |
2925 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002926
Joe Perchesbf82c182010-02-09 11:49:50 +00002927 if (RTL_R8(PHYstatus) & TBI_Enable)
2928 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002929}
2930
Francois Romieu773d2022007-01-31 23:47:43 +01002931static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
2932{
2933 void __iomem *ioaddr = tp->mmio_addr;
2934 u32 high;
2935 u32 low;
2936
2937 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
2938 high = addr[4] | (addr[5] << 8);
2939
2940 spin_lock_irq(&tp->lock);
2941
2942 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00002943
Francois Romieu773d2022007-01-31 23:47:43 +01002944 RTL_W32(MAC4, high);
françois romieu908ba2b2010-04-26 11:42:58 +00002945 RTL_R32(MAC4);
2946
Francois Romieu78f1cd02010-03-27 19:35:46 -07002947 RTL_W32(MAC0, low);
françois romieu908ba2b2010-04-26 11:42:58 +00002948 RTL_R32(MAC0);
2949
Francois Romieu773d2022007-01-31 23:47:43 +01002950 RTL_W8(Cfg9346, Cfg9346_Lock);
2951
2952 spin_unlock_irq(&tp->lock);
2953}
2954
2955static int rtl_set_mac_address(struct net_device *dev, void *p)
2956{
2957 struct rtl8169_private *tp = netdev_priv(dev);
2958 struct sockaddr *addr = p;
2959
2960 if (!is_valid_ether_addr(addr->sa_data))
2961 return -EADDRNOTAVAIL;
2962
2963 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
2964
2965 rtl_rar_set(tp, dev->dev_addr);
2966
2967 return 0;
2968}
2969
Francois Romieu5f787a12006-08-17 13:02:36 +02002970static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2971{
2972 struct rtl8169_private *tp = netdev_priv(dev);
2973 struct mii_ioctl_data *data = if_mii(ifr);
2974
Francois Romieu8b4ab282008-11-19 22:05:25 -08002975 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
2976}
Francois Romieu5f787a12006-08-17 13:02:36 +02002977
Francois Romieucecb5fd2011-04-01 10:21:07 +02002978static int rtl_xmii_ioctl(struct rtl8169_private *tp,
2979 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08002980{
Francois Romieu5f787a12006-08-17 13:02:36 +02002981 switch (cmd) {
2982 case SIOCGMIIPHY:
2983 data->phy_id = 32; /* Internal PHY */
2984 return 0;
2985
2986 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00002987 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02002988 return 0;
2989
2990 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00002991 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02002992 return 0;
2993 }
2994 return -EOPNOTSUPP;
2995}
2996
Francois Romieu8b4ab282008-11-19 22:05:25 -08002997static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
2998{
2999 return -EOPNOTSUPP;
3000}
3001
Francois Romieu0e485152007-02-20 00:00:26 +01003002static const struct rtl_cfg_info {
3003 void (*hw_start)(struct net_device *);
3004 unsigned int region;
3005 unsigned int align;
3006 u16 intr_event;
3007 u16 napi_event;
Francois Romieuccdffb92008-07-26 14:26:06 +02003008 unsigned features;
Jean Delvaref21b75e2009-05-26 20:54:48 -07003009 u8 default_ver;
Francois Romieu0e485152007-02-20 00:00:26 +01003010} rtl_cfg_infos [] = {
3011 [RTL_CFG_0] = {
3012 .hw_start = rtl_hw_start_8169,
3013 .region = 1,
Francois Romieue9f63f32007-02-28 23:16:57 +01003014 .align = 0,
Francois Romieu0e485152007-02-20 00:00:26 +01003015 .intr_event = SYSErr | LinkChg | RxOverflow |
3016 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003017 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003018 .features = RTL_FEATURE_GMII,
3019 .default_ver = RTL_GIGA_MAC_VER_01,
Francois Romieu0e485152007-02-20 00:00:26 +01003020 },
3021 [RTL_CFG_1] = {
3022 .hw_start = rtl_hw_start_8168,
3023 .region = 2,
3024 .align = 8,
françois romieu53f57352010-11-08 13:23:05 +00003025 .intr_event = SYSErr | LinkChg | RxOverflow |
Francois Romieu0e485152007-02-20 00:00:26 +01003026 TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003027 .napi_event = TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003028 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
3029 .default_ver = RTL_GIGA_MAC_VER_11,
Francois Romieu0e485152007-02-20 00:00:26 +01003030 },
3031 [RTL_CFG_2] = {
3032 .hw_start = rtl_hw_start_8101,
3033 .region = 2,
3034 .align = 8,
3035 .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
3036 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003037 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003038 .features = RTL_FEATURE_MSI,
3039 .default_ver = RTL_GIGA_MAC_VER_13,
Francois Romieu0e485152007-02-20 00:00:26 +01003040 }
3041};
3042
Francois Romieufbac58f2007-10-04 22:51:38 +02003043/* Cfg9346_Unlock assumed. */
3044static unsigned rtl_try_msi(struct pci_dev *pdev, void __iomem *ioaddr,
3045 const struct rtl_cfg_info *cfg)
3046{
3047 unsigned msi = 0;
3048 u8 cfg2;
3049
3050 cfg2 = RTL_R8(Config2) & ~MSIEnable;
Francois Romieuccdffb92008-07-26 14:26:06 +02003051 if (cfg->features & RTL_FEATURE_MSI) {
Francois Romieufbac58f2007-10-04 22:51:38 +02003052 if (pci_enable_msi(pdev)) {
3053 dev_info(&pdev->dev, "no MSI. Back to INTx.\n");
3054 } else {
3055 cfg2 |= MSIEnable;
3056 msi = RTL_FEATURE_MSI;
3057 }
3058 }
3059 RTL_W8(Config2, cfg2);
3060 return msi;
3061}
3062
3063static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3064{
3065 if (tp->features & RTL_FEATURE_MSI) {
3066 pci_disable_msi(pdev);
3067 tp->features &= ~RTL_FEATURE_MSI;
3068 }
3069}
3070
Francois Romieu8b4ab282008-11-19 22:05:25 -08003071static const struct net_device_ops rtl8169_netdev_ops = {
3072 .ndo_open = rtl8169_open,
3073 .ndo_stop = rtl8169_close,
3074 .ndo_get_stats = rtl8169_get_stats,
Stephen Hemminger00829822008-11-20 20:14:53 -08003075 .ndo_start_xmit = rtl8169_start_xmit,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003076 .ndo_tx_timeout = rtl8169_tx_timeout,
3077 .ndo_validate_addr = eth_validate_addr,
3078 .ndo_change_mtu = rtl8169_change_mtu,
Michał Mirosław350fb322011-04-08 06:35:56 +00003079 .ndo_fix_features = rtl8169_fix_features,
3080 .ndo_set_features = rtl8169_set_features,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003081 .ndo_set_mac_address = rtl_set_mac_address,
3082 .ndo_do_ioctl = rtl8169_ioctl,
3083 .ndo_set_multicast_list = rtl_set_rx_mode,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003084#ifdef CONFIG_NET_POLL_CONTROLLER
3085 .ndo_poll_controller = rtl8169_netpoll,
3086#endif
3087
3088};
3089
françois romieuc0e45c12011-01-03 15:08:04 +00003090static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3091{
3092 struct mdio_ops *ops = &tp->mdio_ops;
3093
3094 switch (tp->mac_version) {
3095 case RTL_GIGA_MAC_VER_27:
3096 ops->write = r8168dp_1_mdio_write;
3097 ops->read = r8168dp_1_mdio_read;
3098 break;
françois romieue6de30d2011-01-03 15:08:37 +00003099 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003100 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003101 ops->write = r8168dp_2_mdio_write;
3102 ops->read = r8168dp_2_mdio_read;
3103 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003104 default:
3105 ops->write = r8169_mdio_write;
3106 ops->read = r8169_mdio_read;
3107 break;
3108 }
3109}
3110
françois romieu065c27c2011-01-03 15:08:12 +00003111static void r810x_phy_power_down(struct rtl8169_private *tp)
3112{
3113 rtl_writephy(tp, 0x1f, 0x0000);
3114 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3115}
3116
3117static void r810x_phy_power_up(struct rtl8169_private *tp)
3118{
3119 rtl_writephy(tp, 0x1f, 0x0000);
3120 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3121}
3122
3123static void r810x_pll_power_down(struct rtl8169_private *tp)
3124{
3125 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3126 rtl_writephy(tp, 0x1f, 0x0000);
3127 rtl_writephy(tp, MII_BMCR, 0x0000);
3128 return;
3129 }
3130
3131 r810x_phy_power_down(tp);
3132}
3133
3134static void r810x_pll_power_up(struct rtl8169_private *tp)
3135{
3136 r810x_phy_power_up(tp);
3137}
3138
3139static void r8168_phy_power_up(struct rtl8169_private *tp)
3140{
3141 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003142 switch (tp->mac_version) {
3143 case RTL_GIGA_MAC_VER_11:
3144 case RTL_GIGA_MAC_VER_12:
3145 case RTL_GIGA_MAC_VER_17:
3146 case RTL_GIGA_MAC_VER_18:
3147 case RTL_GIGA_MAC_VER_19:
3148 case RTL_GIGA_MAC_VER_20:
3149 case RTL_GIGA_MAC_VER_21:
3150 case RTL_GIGA_MAC_VER_22:
3151 case RTL_GIGA_MAC_VER_23:
3152 case RTL_GIGA_MAC_VER_24:
3153 case RTL_GIGA_MAC_VER_25:
3154 case RTL_GIGA_MAC_VER_26:
3155 case RTL_GIGA_MAC_VER_27:
3156 case RTL_GIGA_MAC_VER_28:
3157 case RTL_GIGA_MAC_VER_31:
3158 rtl_writephy(tp, 0x0e, 0x0000);
3159 break;
3160 default:
3161 break;
3162 }
françois romieu065c27c2011-01-03 15:08:12 +00003163 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3164}
3165
3166static void r8168_phy_power_down(struct rtl8169_private *tp)
3167{
3168 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003169 switch (tp->mac_version) {
3170 case RTL_GIGA_MAC_VER_32:
3171 case RTL_GIGA_MAC_VER_33:
3172 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3173 break;
3174
3175 case RTL_GIGA_MAC_VER_11:
3176 case RTL_GIGA_MAC_VER_12:
3177 case RTL_GIGA_MAC_VER_17:
3178 case RTL_GIGA_MAC_VER_18:
3179 case RTL_GIGA_MAC_VER_19:
3180 case RTL_GIGA_MAC_VER_20:
3181 case RTL_GIGA_MAC_VER_21:
3182 case RTL_GIGA_MAC_VER_22:
3183 case RTL_GIGA_MAC_VER_23:
3184 case RTL_GIGA_MAC_VER_24:
3185 case RTL_GIGA_MAC_VER_25:
3186 case RTL_GIGA_MAC_VER_26:
3187 case RTL_GIGA_MAC_VER_27:
3188 case RTL_GIGA_MAC_VER_28:
3189 case RTL_GIGA_MAC_VER_31:
3190 rtl_writephy(tp, 0x0e, 0x0200);
3191 default:
3192 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3193 break;
3194 }
françois romieu065c27c2011-01-03 15:08:12 +00003195}
3196
3197static void r8168_pll_power_down(struct rtl8169_private *tp)
3198{
3199 void __iomem *ioaddr = tp->mmio_addr;
3200
Francois Romieucecb5fd2011-04-01 10:21:07 +02003201 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3202 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3203 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003204 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003205 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003206 }
françois romieu065c27c2011-01-03 15:08:12 +00003207
Francois Romieucecb5fd2011-04-01 10:21:07 +02003208 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3209 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00003210 (RTL_R16(CPlusCmd) & ASF)) {
3211 return;
3212 }
3213
hayeswang01dc7fe2011-03-21 01:50:28 +00003214 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3215 tp->mac_version == RTL_GIGA_MAC_VER_33)
3216 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3217
françois romieu065c27c2011-01-03 15:08:12 +00003218 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3219 rtl_writephy(tp, 0x1f, 0x0000);
3220 rtl_writephy(tp, MII_BMCR, 0x0000);
3221
3222 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3223 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3224 return;
3225 }
3226
3227 r8168_phy_power_down(tp);
3228
3229 switch (tp->mac_version) {
3230 case RTL_GIGA_MAC_VER_25:
3231 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003232 case RTL_GIGA_MAC_VER_27:
3233 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003234 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003235 case RTL_GIGA_MAC_VER_32:
3236 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003237 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3238 break;
3239 }
3240}
3241
3242static void r8168_pll_power_up(struct rtl8169_private *tp)
3243{
3244 void __iomem *ioaddr = tp->mmio_addr;
3245
Francois Romieucecb5fd2011-04-01 10:21:07 +02003246 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3247 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3248 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003249 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003250 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003251 }
françois romieu065c27c2011-01-03 15:08:12 +00003252
3253 switch (tp->mac_version) {
3254 case RTL_GIGA_MAC_VER_25:
3255 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003256 case RTL_GIGA_MAC_VER_27:
3257 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003258 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003259 case RTL_GIGA_MAC_VER_32:
3260 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003261 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3262 break;
3263 }
3264
3265 r8168_phy_power_up(tp);
3266}
3267
3268static void rtl_pll_power_op(struct rtl8169_private *tp,
3269 void (*op)(struct rtl8169_private *))
3270{
3271 if (op)
3272 op(tp);
3273}
3274
3275static void rtl_pll_power_down(struct rtl8169_private *tp)
3276{
3277 rtl_pll_power_op(tp, tp->pll_power_ops.down);
3278}
3279
3280static void rtl_pll_power_up(struct rtl8169_private *tp)
3281{
3282 rtl_pll_power_op(tp, tp->pll_power_ops.up);
3283}
3284
3285static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3286{
3287 struct pll_power_ops *ops = &tp->pll_power_ops;
3288
3289 switch (tp->mac_version) {
3290 case RTL_GIGA_MAC_VER_07:
3291 case RTL_GIGA_MAC_VER_08:
3292 case RTL_GIGA_MAC_VER_09:
3293 case RTL_GIGA_MAC_VER_10:
3294 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003295 case RTL_GIGA_MAC_VER_29:
3296 case RTL_GIGA_MAC_VER_30:
françois romieu065c27c2011-01-03 15:08:12 +00003297 ops->down = r810x_pll_power_down;
3298 ops->up = r810x_pll_power_up;
3299 break;
3300
3301 case RTL_GIGA_MAC_VER_11:
3302 case RTL_GIGA_MAC_VER_12:
3303 case RTL_GIGA_MAC_VER_17:
3304 case RTL_GIGA_MAC_VER_18:
3305 case RTL_GIGA_MAC_VER_19:
3306 case RTL_GIGA_MAC_VER_20:
3307 case RTL_GIGA_MAC_VER_21:
3308 case RTL_GIGA_MAC_VER_22:
3309 case RTL_GIGA_MAC_VER_23:
3310 case RTL_GIGA_MAC_VER_24:
3311 case RTL_GIGA_MAC_VER_25:
3312 case RTL_GIGA_MAC_VER_26:
3313 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003314 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003315 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003316 case RTL_GIGA_MAC_VER_32:
3317 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003318 ops->down = r8168_pll_power_down;
3319 ops->up = r8168_pll_power_up;
3320 break;
3321
3322 default:
3323 ops->down = NULL;
3324 ops->up = NULL;
3325 break;
3326 }
3327}
3328
Francois Romieu6f43adc2011-04-29 15:05:51 +02003329static void rtl_hw_reset(struct rtl8169_private *tp)
3330{
3331 void __iomem *ioaddr = tp->mmio_addr;
3332 int i;
3333
3334 /* Soft reset the chip. */
3335 RTL_W8(ChipCmd, CmdReset);
3336
3337 /* Check that the chip has finished the reset. */
3338 for (i = 0; i < 100; i++) {
3339 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3340 break;
3341 msleep_interruptible(1);
3342 }
3343}
3344
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003345static int __devinit
3346rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
3347{
Francois Romieu0e485152007-02-20 00:00:26 +01003348 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
3349 const unsigned int region = cfg->region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 struct rtl8169_private *tp;
Francois Romieuccdffb92008-07-26 14:26:06 +02003351 struct mii_if_info *mii;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003352 struct net_device *dev;
3353 void __iomem *ioaddr;
Francois Romieu2b7b4312011-04-18 22:53:24 -07003354 int chipset, i;
Francois Romieu07d3f512007-02-21 22:40:46 +01003355 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003357 if (netif_msg_drv(&debug)) {
3358 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
3359 MODULENAME, RTL8169_VERSION);
3360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 dev = alloc_etherdev(sizeof (*tp));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003363 if (!dev) {
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003364 if (netif_msg_drv(&debug))
Jeff Garzik9b91cf92006-06-27 11:39:50 -04003365 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003366 rc = -ENOMEM;
3367 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 }
3369
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieu8b4ab282008-11-19 22:05:25 -08003371 dev->netdev_ops = &rtl8169_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 tp = netdev_priv(dev);
David Howellsc4028952006-11-22 14:57:56 +00003373 tp->dev = dev;
Ivan Vecera21e197f2008-04-17 22:48:41 +02003374 tp->pci_dev = pdev;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003375 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376
Francois Romieuccdffb92008-07-26 14:26:06 +02003377 mii = &tp->mii;
3378 mii->dev = dev;
3379 mii->mdio_read = rtl_mdio_read;
3380 mii->mdio_write = rtl_mdio_write;
3381 mii->phy_id_mask = 0x1f;
3382 mii->reg_num_mask = 0x1f;
3383 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
3384
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +00003385 /* disable ASPM completely as that cause random device stop working
3386 * problems as well as full system hangs for some PCIe devices users */
3387 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
3388 PCIE_LINK_STATE_CLKPM);
3389
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 /* enable device (incl. PCI PM wakeup and hotplug setup) */
3391 rc = pci_enable_device(pdev);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003392 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003393 netif_err(tp, probe, dev, "enable failure\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003394 goto err_out_free_dev_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 }
3396
françois romieu87aeec72010-04-26 11:42:06 +00003397 if (pci_set_mwi(pdev) < 0)
3398 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 /* make sure PCI base addr 1 is MMIO */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003401 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003402 netif_err(tp, probe, dev,
3403 "region #%d not an MMIO resource, aborting\n",
3404 region);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003406 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003408
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 /* check for weird/broken PCI region reporting */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003410 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003411 netif_err(tp, probe, dev,
3412 "Invalid PCI region size(s), aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003414 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 }
3416
3417 rc = pci_request_regions(pdev, MODULENAME);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003418 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003419 netif_err(tp, probe, dev, "could not request regions\n");
françois romieu87aeec72010-04-26 11:42:06 +00003420 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 }
3422
Hayes Wangd24e9aa2011-02-22 17:26:19 +08003423 tp->cp_cmd = RxChkSum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424
3425 if ((sizeof(dma_addr_t) > 4) &&
David S. Miller4300e8c2010-03-26 10:23:30 -07003426 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 tp->cp_cmd |= PCIDAC;
3428 dev->features |= NETIF_F_HIGHDMA;
3429 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07003430 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003432 netif_err(tp, probe, dev, "DMA configuration failed\n");
françois romieu87aeec72010-04-26 11:42:06 +00003433 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 }
3435 }
3436
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 /* ioremap MMIO region */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003438 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003439 if (!ioaddr) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003440 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 rc = -EIO;
françois romieu87aeec72010-04-26 11:42:06 +00003442 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 }
Francois Romieu6f43adc2011-04-29 15:05:51 +02003444 tp->mmio_addr = ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445
Jon Masone44daad2011-06-27 07:46:31 +00003446 if (!pci_is_pcie(pdev))
3447 netif_info(tp, probe, dev, "not PCI Express\n");
David S. Miller4300e8c2010-03-26 10:23:30 -07003448
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003449 RTL_W16(IntrMask, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450
Francois Romieu6f43adc2011-04-29 15:05:51 +02003451 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003453 RTL_W16(IntrStatus, 0xffff);
3454
françois romieuca52efd2009-07-24 12:34:19 +00003455 pci_set_master(pdev);
3456
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 /* Identify chip attached to board */
Francois Romieu5d320a22011-05-08 17:47:36 +02003458 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459
Francois Romieu7a8fc772011-03-01 17:18:33 +01003460 /*
3461 * Pretend we are using VLANs; This bypasses a nasty bug where
3462 * Interrupts stop flowing on high load on 8110SCd controllers.
3463 */
3464 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3465 tp->cp_cmd |= RxVlan;
3466
françois romieuc0e45c12011-01-03 15:08:04 +00003467 rtl_init_mdio_ops(tp);
françois romieu065c27c2011-01-03 15:08:12 +00003468 rtl_init_pll_power_ops(tp);
françois romieuc0e45c12011-01-03 15:08:04 +00003469
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 rtl8169_print_mac_version(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471
Francois Romieu85bffe62011-04-27 08:22:39 +02003472 chipset = tp->mac_version;
3473 tp->txd_version = rtl_chip_infos[chipset].txd_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474
Francois Romieu5d06a992006-02-23 00:47:58 +01003475 RTL_W8(Cfg9346, Cfg9346_Unlock);
3476 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
3477 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
Bruno Prémont20037fa2008-10-08 17:05:03 -07003478 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
3479 tp->features |= RTL_FEATURE_WOL;
3480 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
3481 tp->features |= RTL_FEATURE_WOL;
Francois Romieufbac58f2007-10-04 22:51:38 +02003482 tp->features |= rtl_try_msi(pdev, ioaddr, cfg);
Francois Romieu5d06a992006-02-23 00:47:58 +01003483 RTL_W8(Cfg9346, Cfg9346_Lock);
3484
Francois Romieu66ec5d42007-11-06 22:56:10 +01003485 if ((tp->mac_version <= RTL_GIGA_MAC_VER_06) &&
3486 (RTL_R8(PHYstatus) & TBI_Enable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 tp->set_speed = rtl8169_set_speed_tbi;
3488 tp->get_settings = rtl8169_gset_tbi;
3489 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
3490 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
3491 tp->link_ok = rtl8169_tbi_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003492 tp->do_ioctl = rtl_tbi_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 } else {
3494 tp->set_speed = rtl8169_set_speed_xmii;
3495 tp->get_settings = rtl8169_gset_xmii;
3496 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
3497 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
3498 tp->link_ok = rtl8169_xmii_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003499 tp->do_ioctl = rtl_xmii_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 }
3501
Francois Romieudf58ef52008-10-09 14:35:58 -07003502 spin_lock_init(&tp->lock);
3503
Ivan Vecera7bf6bf42008-09-23 22:46:29 +00003504 /* Get MAC address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 for (i = 0; i < MAC_ADDR_LEN; i++)
3506 dev->dev_addr[i] = RTL_R8(MAC0 + i);
John W. Linville6d6525b2005-09-12 10:48:57 -04003507 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
3511 dev->irq = pdev->irq;
3512 dev->base_addr = (unsigned long) ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003514 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515
Michał Mirosław350fb322011-04-08 06:35:56 +00003516 /* don't enable SG, IP_CSUM and TSO by default - it might not work
3517 * properly for all devices */
3518 dev->features |= NETIF_F_RXCSUM |
3519 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3520
3521 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3522 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3523 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3524 NETIF_F_HIGHDMA;
3525
3526 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3527 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
3528 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529
3530 tp->intr_mask = 0xffff;
Francois Romieu0e485152007-02-20 00:00:26 +01003531 tp->hw_start = cfg->hw_start;
3532 tp->intr_event = cfg->intr_event;
3533 tp->napi_event = cfg->napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534
Francois Romieu2efa53f2007-03-09 00:00:05 +01003535 init_timer(&tp->timer);
3536 tp->timer.data = (unsigned long) dev;
3537 tp->timer.function = rtl8169_phy_timer;
3538
Francois Romieub6ffd972011-06-17 17:00:05 +02003539 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
François Romieu953a12c2011-04-24 17:38:48 +02003540
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 rc = register_netdev(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003542 if (rc < 0)
françois romieu87aeec72010-04-26 11:42:06 +00003543 goto err_out_msi_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544
3545 pci_set_drvdata(pdev, dev);
3546
Joe Perchesbf82c182010-02-09 11:49:50 +00003547 netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
Francois Romieu85bffe62011-04-27 08:22:39 +02003548 rtl_chip_infos[chipset].name, dev->base_addr, dev->dev_addr,
Joe Perchesbf82c182010-02-09 11:49:50 +00003549 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550
Francois Romieucecb5fd2011-04-01 10:21:07 +02003551 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3552 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3553 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00003554 rtl8168_driver_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003555 }
françois romieub646d902011-01-03 15:08:21 +00003556
Bruno Prémont8b76ab32008-10-08 17:06:25 -07003557 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558
Alan Sternf3ec4f82010-06-08 15:23:51 -04003559 if (pci_dev_run_wake(pdev))
3560 pm_runtime_put_noidle(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003561
Ivan Vecera0d672e92011-02-15 02:08:39 +00003562 netif_carrier_off(dev);
3563
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003564out:
3565 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566
françois romieu87aeec72010-04-26 11:42:06 +00003567err_out_msi_4:
Francois Romieufbac58f2007-10-04 22:51:38 +02003568 rtl_disable_msi(pdev, tp);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003569 iounmap(ioaddr);
françois romieu87aeec72010-04-26 11:42:06 +00003570err_out_free_res_3:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003571 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003572err_out_mwi_2:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003573 pci_clear_mwi(pdev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003574 pci_disable_device(pdev);
3575err_out_free_dev_1:
3576 free_netdev(dev);
3577 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578}
3579
Francois Romieu07d3f512007-02-21 22:40:46 +01003580static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581{
3582 struct net_device *dev = pci_get_drvdata(pdev);
3583 struct rtl8169_private *tp = netdev_priv(dev);
3584
Francois Romieucecb5fd2011-04-01 10:21:07 +02003585 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3586 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3587 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00003588 rtl8168_driver_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003589 }
françois romieub646d902011-01-03 15:08:21 +00003590
Tejun Heo23f333a2010-12-12 16:45:14 +01003591 cancel_delayed_work_sync(&tp->task);
Francois Romieueb2a0212007-02-15 23:37:21 +01003592
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 unregister_netdev(dev);
Ivan Veceracc098dc2009-11-29 23:12:52 -08003594
François Romieu953a12c2011-04-24 17:38:48 +02003595 rtl_release_firmware(tp);
3596
Alan Sternf3ec4f82010-06-08 15:23:51 -04003597 if (pci_dev_run_wake(pdev))
3598 pm_runtime_get_noresume(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003599
Ivan Veceracc098dc2009-11-29 23:12:52 -08003600 /* restore original MAC address */
3601 rtl_rar_set(tp, dev->perm_addr);
3602
Francois Romieufbac58f2007-10-04 22:51:38 +02003603 rtl_disable_msi(pdev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 rtl8169_release_board(pdev, dev, tp->mmio_addr);
3605 pci_set_drvdata(pdev, NULL);
3606}
3607
Francois Romieub6ffd972011-06-17 17:00:05 +02003608static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
3609{
3610 struct rtl_fw *rtl_fw;
3611 const char *name;
3612 int rc = -ENOMEM;
3613
3614 name = rtl_lookup_firmware_name(tp);
3615 if (!name)
3616 goto out_no_firmware;
3617
3618 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
3619 if (!rtl_fw)
3620 goto err_warn;
3621
3622 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
3623 if (rc < 0)
3624 goto err_free;
3625
Francois Romieufd112f22011-06-18 00:10:29 +02003626 rc = rtl_check_firmware(tp, rtl_fw);
3627 if (rc < 0)
3628 goto err_release_firmware;
3629
Francois Romieub6ffd972011-06-17 17:00:05 +02003630 tp->rtl_fw = rtl_fw;
3631out:
3632 return;
3633
Francois Romieufd112f22011-06-18 00:10:29 +02003634err_release_firmware:
3635 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02003636err_free:
3637 kfree(rtl_fw);
3638err_warn:
3639 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
3640 name, rc);
3641out_no_firmware:
3642 tp->rtl_fw = NULL;
3643 goto out;
3644}
3645
François Romieu953a12c2011-04-24 17:38:48 +02003646static void rtl_request_firmware(struct rtl8169_private *tp)
3647{
Francois Romieub6ffd972011-06-17 17:00:05 +02003648 if (IS_ERR(tp->rtl_fw))
3649 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02003650}
3651
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652static int rtl8169_open(struct net_device *dev)
3653{
3654 struct rtl8169_private *tp = netdev_priv(dev);
françois romieueee3a962011-01-08 02:17:26 +00003655 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu99f252b2007-04-02 22:59:59 +02003657 int retval = -ENOMEM;
3658
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003659 pm_runtime_get_sync(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660
Neil Hormanc0cd8842010-03-29 13:16:02 -07003661 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 * Rx and Tx desscriptors needs 256 bytes alignment.
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003663 * dma_alloc_coherent provides more.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 */
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003665 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
3666 &tp->TxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 if (!tp->TxDescArray)
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003668 goto err_pm_runtime_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003670 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
3671 &tp->RxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 if (!tp->RxDescArray)
Francois Romieu99f252b2007-04-02 22:59:59 +02003673 goto err_free_tx_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674
3675 retval = rtl8169_init_ring(dev);
3676 if (retval < 0)
Francois Romieu99f252b2007-04-02 22:59:59 +02003677 goto err_free_rx_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678
David Howellsc4028952006-11-22 14:57:56 +00003679 INIT_DELAYED_WORK(&tp->task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680
Francois Romieu99f252b2007-04-02 22:59:59 +02003681 smp_mb();
3682
François Romieu953a12c2011-04-24 17:38:48 +02003683 rtl_request_firmware(tp);
3684
Francois Romieufbac58f2007-10-04 22:51:38 +02003685 retval = request_irq(dev->irq, rtl8169_interrupt,
3686 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
Francois Romieu99f252b2007-04-02 22:59:59 +02003687 dev->name, dev);
3688 if (retval < 0)
François Romieu953a12c2011-04-24 17:38:48 +02003689 goto err_release_fw_2;
Francois Romieu99f252b2007-04-02 22:59:59 +02003690
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003691 napi_enable(&tp->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003692
françois romieueee3a962011-01-08 02:17:26 +00003693 rtl8169_init_phy(dev, tp);
3694
Michał Mirosław350fb322011-04-08 06:35:56 +00003695 rtl8169_set_features(dev, dev->features);
françois romieueee3a962011-01-08 02:17:26 +00003696
françois romieu065c27c2011-01-03 15:08:12 +00003697 rtl_pll_power_up(tp);
3698
Francois Romieu07ce4062007-02-23 23:36:39 +01003699 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003701 tp->saved_wolopts = 0;
3702 pm_runtime_put_noidle(&pdev->dev);
3703
françois romieueee3a962011-01-08 02:17:26 +00003704 rtl8169_check_link_status(dev, tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705out:
3706 return retval;
3707
François Romieu953a12c2011-04-24 17:38:48 +02003708err_release_fw_2:
3709 rtl_release_firmware(tp);
Francois Romieu99f252b2007-04-02 22:59:59 +02003710 rtl8169_rx_clear(tp);
3711err_free_rx_1:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003712 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
3713 tp->RxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003714 tp->RxDescArray = NULL;
Francois Romieu99f252b2007-04-02 22:59:59 +02003715err_free_tx_0:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003716 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
3717 tp->TxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003718 tp->TxDescArray = NULL;
3719err_pm_runtime_put:
3720 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 goto out;
3722}
3723
françois romieue6de30d2011-01-03 15:08:37 +00003724static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725{
françois romieue6de30d2011-01-03 15:08:37 +00003726 void __iomem *ioaddr = tp->mmio_addr;
3727
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 /* Disable interrupts */
3729 rtl8169_irq_mask_and_ack(ioaddr);
3730
Hayes Wang5d2e1952011-02-22 17:26:22 +08003731 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00003732 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3733 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieue6de30d2011-01-03 15:08:37 +00003734 while (RTL_R8(TxPoll) & NPQ)
3735 udelay(20);
3736
3737 }
3738
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 /* Reset the chipset */
3740 RTL_W8(ChipCmd, CmdReset);
3741
3742 /* PCI commit */
3743 RTL_R8(ChipCmd);
3744}
3745
Francois Romieu7f796d82007-06-11 23:04:41 +02003746static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01003747{
3748 void __iomem *ioaddr = tp->mmio_addr;
3749 u32 cfg = rtl8169_rx_config;
3750
Francois Romieu2b7b4312011-04-18 22:53:24 -07003751 cfg |= (RTL_R32(RxConfig) & RTL_RX_CONFIG_MASK);
Francois Romieu9cb427b2006-11-02 00:10:16 +01003752 RTL_W32(RxConfig, cfg);
3753
3754 /* Set DMA burst size and Interframe Gap Time */
3755 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
3756 (InterFrameGap << TxInterFrameGapShift));
3757}
3758
Francois Romieu07ce4062007-02-23 23:36:39 +01003759static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760{
3761 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762
Francois Romieu6f43adc2011-04-29 15:05:51 +02003763 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764
Francois Romieu07ce4062007-02-23 23:36:39 +01003765 tp->hw_start(dev);
3766
Francois Romieu07ce4062007-02-23 23:36:39 +01003767 netif_start_queue(dev);
3768}
3769
Francois Romieu7f796d82007-06-11 23:04:41 +02003770static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
3771 void __iomem *ioaddr)
3772{
3773 /*
3774 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
3775 * register to be written before TxDescAddrLow to work.
3776 * Switching from MMIO to I/O access fixes the issue as well.
3777 */
3778 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07003779 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02003780 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07003781 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d82007-06-11 23:04:41 +02003782}
3783
3784static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
3785{
3786 u16 cmd;
3787
3788 cmd = RTL_R16(CPlusCmd);
3789 RTL_W16(CPlusCmd, cmd);
3790 return cmd;
3791}
3792
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07003793static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d82007-06-11 23:04:41 +02003794{
3795 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e82009-10-26 10:52:37 +00003796 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d82007-06-11 23:04:41 +02003797}
3798
Francois Romieu6dccd162007-02-13 23:38:05 +01003799static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
3800{
Francois Romieu37441002011-06-17 22:58:54 +02003801 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01003802 u32 mac_version;
3803 u32 clk;
3804 u32 val;
3805 } cfg2_info [] = {
3806 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
3807 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
3808 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
3809 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02003810 };
3811 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01003812 unsigned int i;
3813 u32 clk;
3814
3815 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01003816 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01003817 if ((p->mac_version == mac_version) && (p->clk == clk)) {
3818 RTL_W32(0x7c, p->val);
3819 break;
3820 }
3821 }
3822}
3823
Francois Romieu07ce4062007-02-23 23:36:39 +01003824static void rtl_hw_start_8169(struct net_device *dev)
3825{
3826 struct rtl8169_private *tp = netdev_priv(dev);
3827 void __iomem *ioaddr = tp->mmio_addr;
3828 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01003829
Francois Romieu9cb427b2006-11-02 00:10:16 +01003830 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
3831 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
3832 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
3833 }
3834
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02003836 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
3837 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
3838 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
3839 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01003840 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3841
françois romieuf0298f82011-01-03 15:07:42 +00003842 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843
Eric Dumazet6f0333b2010-10-11 11:17:47 +00003844 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845
Francois Romieucecb5fd2011-04-01 10:21:07 +02003846 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
3847 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
3848 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
3849 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02003850 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851
Francois Romieu7f796d82007-06-11 23:04:41 +02003852 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02003853
Francois Romieucecb5fd2011-04-01 10:21:07 +02003854 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
3855 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02003856 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02003858 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 }
3860
Francois Romieubcf0bf92006-07-26 23:14:13 +02003861 RTL_W16(CPlusCmd, tp->cp_cmd);
3862
Francois Romieu6dccd162007-02-13 23:38:05 +01003863 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
3864
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 /*
3866 * Undocumented corner. Supposedly:
3867 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
3868 */
3869 RTL_W16(IntrMitigate, 0x0000);
3870
Francois Romieu7f796d82007-06-11 23:04:41 +02003871 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01003872
Francois Romieucecb5fd2011-04-01 10:21:07 +02003873 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
3874 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
3875 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
3876 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02003877 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3878 rtl_set_rx_tx_config_registers(tp);
3879 }
3880
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02003882
3883 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
3884 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885
3886 RTL_W32(RxMissed, 0);
3887
Francois Romieu07ce4062007-02-23 23:36:39 +01003888 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889
3890 /* no early-rx interrupts */
3891 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01003892
3893 /* Enable all known interrupts by setting the interrupt mask. */
Francois Romieu0e485152007-02-20 00:00:26 +01003894 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01003895}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896
Francois Romieu9c14cea2008-07-05 00:21:15 +02003897static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
Francois Romieu458a9f62008-08-02 15:50:02 +02003898{
Jon Masone44daad2011-06-27 07:46:31 +00003899 int cap = pci_pcie_cap(pdev);
Francois Romieu458a9f62008-08-02 15:50:02 +02003900
Francois Romieu9c14cea2008-07-05 00:21:15 +02003901 if (cap) {
3902 u16 ctl;
3903
3904 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
3905 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
3906 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
3907 }
Francois Romieu458a9f62008-08-02 15:50:02 +02003908}
3909
françois romieu650e8d52011-01-03 15:08:29 +00003910static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02003911{
3912 u32 csi;
3913
3914 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
françois romieu650e8d52011-01-03 15:08:29 +00003915 rtl_csi_write(ioaddr, 0x070c, csi | bits);
3916}
3917
françois romieue6de30d2011-01-03 15:08:37 +00003918static void rtl_csi_access_enable_1(void __iomem *ioaddr)
3919{
3920 rtl_csi_access_enable(ioaddr, 0x17000000);
3921}
3922
françois romieu650e8d52011-01-03 15:08:29 +00003923static void rtl_csi_access_enable_2(void __iomem *ioaddr)
3924{
3925 rtl_csi_access_enable(ioaddr, 0x27000000);
Francois Romieudacf8152008-08-02 20:44:13 +02003926}
3927
3928struct ephy_info {
3929 unsigned int offset;
3930 u16 mask;
3931 u16 bits;
3932};
3933
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003934static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02003935{
3936 u16 w;
3937
3938 while (len-- > 0) {
3939 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
3940 rtl_ephy_write(ioaddr, e->offset, w);
3941 e++;
3942 }
3943}
3944
Francois Romieub726e492008-06-28 12:22:59 +02003945static void rtl_disable_clock_request(struct pci_dev *pdev)
3946{
Jon Masone44daad2011-06-27 07:46:31 +00003947 int cap = pci_pcie_cap(pdev);
Francois Romieub726e492008-06-28 12:22:59 +02003948
3949 if (cap) {
3950 u16 ctl;
3951
3952 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
3953 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
3954 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
3955 }
3956}
3957
françois romieue6de30d2011-01-03 15:08:37 +00003958static void rtl_enable_clock_request(struct pci_dev *pdev)
3959{
Jon Masone44daad2011-06-27 07:46:31 +00003960 int cap = pci_pcie_cap(pdev);
françois romieue6de30d2011-01-03 15:08:37 +00003961
3962 if (cap) {
3963 u16 ctl;
3964
3965 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
3966 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
3967 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
3968 }
3969}
3970
Francois Romieub726e492008-06-28 12:22:59 +02003971#define R8168_CPCMD_QUIRK_MASK (\
3972 EnableBist | \
3973 Mac_dbgo_oe | \
3974 Force_half_dup | \
3975 Force_rxflow_en | \
3976 Force_txflow_en | \
3977 Cxpl_dbg_sel | \
3978 ASF | \
3979 PktCntrDisable | \
3980 Mac_dbgo_sel)
3981
Francois Romieu219a1e92008-06-28 11:58:39 +02003982static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
3983{
Francois Romieub726e492008-06-28 12:22:59 +02003984 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3985
3986 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3987
Francois Romieu2e68ae42008-06-28 12:00:55 +02003988 rtl_tx_performance_tweak(pdev,
3989 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02003990}
3991
3992static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
3993{
3994 rtl_hw_start_8168bb(ioaddr, pdev);
Francois Romieub726e492008-06-28 12:22:59 +02003995
françois romieuf0298f82011-01-03 15:07:42 +00003996 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02003997
3998 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02003999}
4000
4001static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
4002{
Francois Romieub726e492008-06-28 12:22:59 +02004003 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4004
4005 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4006
Francois Romieu219a1e92008-06-28 11:58:39 +02004007 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02004008
4009 rtl_disable_clock_request(pdev);
4010
4011 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02004012}
4013
Francois Romieuef3386f2008-06-29 12:24:30 +02004014static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
Francois Romieu219a1e92008-06-28 11:58:39 +02004015{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004016 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004017 { 0x01, 0, 0x0001 },
4018 { 0x02, 0x0800, 0x1000 },
4019 { 0x03, 0, 0x0042 },
4020 { 0x06, 0x0080, 0x0000 },
4021 { 0x07, 0, 0x2000 }
4022 };
4023
françois romieu650e8d52011-01-03 15:08:29 +00004024 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004025
4026 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4027
Francois Romieu219a1e92008-06-28 11:58:39 +02004028 __rtl_hw_start_8168cp(ioaddr, pdev);
4029}
4030
Francois Romieuef3386f2008-06-29 12:24:30 +02004031static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
4032{
françois romieu650e8d52011-01-03 15:08:29 +00004033 rtl_csi_access_enable_2(ioaddr);
Francois Romieuef3386f2008-06-29 12:24:30 +02004034
4035 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4036
4037 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4038
4039 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4040}
4041
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004042static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
4043{
françois romieu650e8d52011-01-03 15:08:29 +00004044 rtl_csi_access_enable_2(ioaddr);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004045
4046 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4047
4048 /* Magic. */
4049 RTL_W8(DBG_REG, 0x20);
4050
françois romieuf0298f82011-01-03 15:07:42 +00004051 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004052
4053 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4054
4055 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4056}
4057
Francois Romieu219a1e92008-06-28 11:58:39 +02004058static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
4059{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004060 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004061 { 0x02, 0x0800, 0x1000 },
4062 { 0x03, 0, 0x0002 },
4063 { 0x06, 0x0080, 0x0000 }
4064 };
4065
françois romieu650e8d52011-01-03 15:08:29 +00004066 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004067
4068 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4069
4070 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4071
Francois Romieu219a1e92008-06-28 11:58:39 +02004072 __rtl_hw_start_8168cp(ioaddr, pdev);
4073}
4074
4075static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
4076{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004077 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004078 { 0x01, 0, 0x0001 },
4079 { 0x03, 0x0400, 0x0220 }
4080 };
4081
françois romieu650e8d52011-01-03 15:08:29 +00004082 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004083
4084 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4085
Francois Romieu219a1e92008-06-28 11:58:39 +02004086 __rtl_hw_start_8168cp(ioaddr, pdev);
4087}
4088
Francois Romieu197ff762008-06-28 13:16:02 +02004089static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
4090{
4091 rtl_hw_start_8168c_2(ioaddr, pdev);
4092}
4093
Francois Romieu6fb07052008-06-29 11:54:28 +02004094static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
4095{
françois romieu650e8d52011-01-03 15:08:29 +00004096 rtl_csi_access_enable_2(ioaddr);
Francois Romieu6fb07052008-06-29 11:54:28 +02004097
4098 __rtl_hw_start_8168cp(ioaddr, pdev);
4099}
4100
Francois Romieu5b538df2008-07-20 16:22:45 +02004101static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
4102{
françois romieu650e8d52011-01-03 15:08:29 +00004103 rtl_csi_access_enable_2(ioaddr);
Francois Romieu5b538df2008-07-20 16:22:45 +02004104
4105 rtl_disable_clock_request(pdev);
4106
françois romieuf0298f82011-01-03 15:07:42 +00004107 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004108
4109 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4110
4111 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4112}
4113
hayeswang4804b3b2011-03-21 01:50:29 +00004114static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
4115{
4116 rtl_csi_access_enable_1(ioaddr);
4117
4118 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4119
4120 RTL_W8(MaxTxPacketSize, TxPacketMax);
4121
4122 rtl_disable_clock_request(pdev);
4123}
4124
françois romieue6de30d2011-01-03 15:08:37 +00004125static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4126{
4127 static const struct ephy_info e_info_8168d_4[] = {
4128 { 0x0b, ~0, 0x48 },
4129 { 0x19, 0x20, 0x50 },
4130 { 0x0c, ~0, 0x20 }
4131 };
4132 int i;
4133
4134 rtl_csi_access_enable_1(ioaddr);
4135
4136 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4137
4138 RTL_W8(MaxTxPacketSize, TxPacketMax);
4139
4140 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4141 const struct ephy_info *e = e_info_8168d_4 + i;
4142 u16 w;
4143
4144 w = rtl_ephy_read(ioaddr, e->offset);
4145 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4146 }
4147
4148 rtl_enable_clock_request(pdev);
4149}
4150
hayeswang01dc7fe2011-03-21 01:50:28 +00004151static void rtl_hw_start_8168e(void __iomem *ioaddr, struct pci_dev *pdev)
4152{
4153 static const struct ephy_info e_info_8168e[] = {
4154 { 0x00, 0x0200, 0x0100 },
4155 { 0x00, 0x0000, 0x0004 },
4156 { 0x06, 0x0002, 0x0001 },
4157 { 0x06, 0x0000, 0x0030 },
4158 { 0x07, 0x0000, 0x2000 },
4159 { 0x00, 0x0000, 0x0020 },
4160 { 0x03, 0x5800, 0x2000 },
4161 { 0x03, 0x0000, 0x0001 },
4162 { 0x01, 0x0800, 0x1000 },
4163 { 0x07, 0x0000, 0x4000 },
4164 { 0x1e, 0x0000, 0x2000 },
4165 { 0x19, 0xffff, 0xfe6c },
4166 { 0x0a, 0x0000, 0x0040 }
4167 };
4168
4169 rtl_csi_access_enable_2(ioaddr);
4170
4171 rtl_ephy_init(ioaddr, e_info_8168e, ARRAY_SIZE(e_info_8168e));
4172
4173 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4174
4175 RTL_W8(MaxTxPacketSize, TxPacketMax);
4176
4177 rtl_disable_clock_request(pdev);
4178
4179 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02004180 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4181 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004182
Francois Romieucecb5fd2011-04-01 10:21:07 +02004183 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004184}
4185
Francois Romieu07ce4062007-02-23 23:36:39 +01004186static void rtl_hw_start_8168(struct net_device *dev)
4187{
Francois Romieu2dd99532007-06-11 23:22:52 +02004188 struct rtl8169_private *tp = netdev_priv(dev);
4189 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01004190 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu2dd99532007-06-11 23:22:52 +02004191
4192 RTL_W8(Cfg9346, Cfg9346_Unlock);
4193
françois romieuf0298f82011-01-03 15:07:42 +00004194 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004195
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004196 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004197
Francois Romieu0e485152007-02-20 00:00:26 +01004198 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004199
4200 RTL_W16(CPlusCmd, tp->cp_cmd);
4201
Francois Romieu0e485152007-02-20 00:00:26 +01004202 RTL_W16(IntrMitigate, 0x5151);
4203
4204 /* Work around for RxFIFO overflow. */
Ivan Vecerab5ba6d12011-01-27 12:24:11 +01004205 if (tp->mac_version == RTL_GIGA_MAC_VER_11 ||
4206 tp->mac_version == RTL_GIGA_MAC_VER_22) {
Francois Romieu0e485152007-02-20 00:00:26 +01004207 tp->intr_event |= RxFIFOOver | PCSTimeout;
4208 tp->intr_event &= ~RxOverflow;
4209 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004210
4211 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4212
Francois Romieub8363902008-06-01 12:31:57 +02004213 rtl_set_rx_mode(dev);
4214
4215 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4216 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004217
4218 RTL_R8(IntrMask);
4219
Francois Romieu219a1e92008-06-28 11:58:39 +02004220 switch (tp->mac_version) {
4221 case RTL_GIGA_MAC_VER_11:
4222 rtl_hw_start_8168bb(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004223 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004224
4225 case RTL_GIGA_MAC_VER_12:
4226 case RTL_GIGA_MAC_VER_17:
4227 rtl_hw_start_8168bef(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004228 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004229
4230 case RTL_GIGA_MAC_VER_18:
Francois Romieuef3386f2008-06-29 12:24:30 +02004231 rtl_hw_start_8168cp_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004232 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004233
4234 case RTL_GIGA_MAC_VER_19:
4235 rtl_hw_start_8168c_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004236 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004237
4238 case RTL_GIGA_MAC_VER_20:
4239 rtl_hw_start_8168c_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004240 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004241
Francois Romieu197ff762008-06-28 13:16:02 +02004242 case RTL_GIGA_MAC_VER_21:
4243 rtl_hw_start_8168c_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004244 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004245
Francois Romieu6fb07052008-06-29 11:54:28 +02004246 case RTL_GIGA_MAC_VER_22:
4247 rtl_hw_start_8168c_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004248 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004249
Francois Romieuef3386f2008-06-29 12:24:30 +02004250 case RTL_GIGA_MAC_VER_23:
4251 rtl_hw_start_8168cp_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004252 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004253
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004254 case RTL_GIGA_MAC_VER_24:
4255 rtl_hw_start_8168cp_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004256 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004257
Francois Romieu5b538df2008-07-20 16:22:45 +02004258 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00004259 case RTL_GIGA_MAC_VER_26:
4260 case RTL_GIGA_MAC_VER_27:
Francois Romieu5b538df2008-07-20 16:22:45 +02004261 rtl_hw_start_8168d(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004262 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004263
françois romieue6de30d2011-01-03 15:08:37 +00004264 case RTL_GIGA_MAC_VER_28:
4265 rtl_hw_start_8168d_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004266 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004267
hayeswang4804b3b2011-03-21 01:50:29 +00004268 case RTL_GIGA_MAC_VER_31:
4269 rtl_hw_start_8168dp(ioaddr, pdev);
4270 break;
4271
hayeswang01dc7fe2011-03-21 01:50:28 +00004272 case RTL_GIGA_MAC_VER_32:
4273 case RTL_GIGA_MAC_VER_33:
4274 rtl_hw_start_8168e(ioaddr, pdev);
4275 break;
françois romieue6de30d2011-01-03 15:08:37 +00004276
Francois Romieu219a1e92008-06-28 11:58:39 +02004277 default:
4278 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
4279 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00004280 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004281 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004282
Francois Romieu0e485152007-02-20 00:00:26 +01004283 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4284
Francois Romieub8363902008-06-01 12:31:57 +02004285 RTL_W8(Cfg9346, Cfg9346_Lock);
4286
Francois Romieu2dd99532007-06-11 23:22:52 +02004287 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004288
Francois Romieu0e485152007-02-20 00:00:26 +01004289 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01004290}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291
Francois Romieu2857ffb2008-08-02 21:08:49 +02004292#define R810X_CPCMD_QUIRK_MASK (\
4293 EnableBist | \
4294 Mac_dbgo_oe | \
4295 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00004296 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02004297 Force_txflow_en | \
4298 Cxpl_dbg_sel | \
4299 ASF | \
4300 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004301 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004302
4303static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4304{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004305 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004306 { 0x01, 0, 0x6e65 },
4307 { 0x02, 0, 0x091f },
4308 { 0x03, 0, 0xc2f9 },
4309 { 0x06, 0, 0xafb5 },
4310 { 0x07, 0, 0x0e00 },
4311 { 0x19, 0, 0xec80 },
4312 { 0x01, 0, 0x2e65 },
4313 { 0x01, 0, 0x6e65 }
4314 };
4315 u8 cfg1;
4316
françois romieu650e8d52011-01-03 15:08:29 +00004317 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004318
4319 RTL_W8(DBG_REG, FIX_NAK_1);
4320
4321 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4322
4323 RTL_W8(Config1,
4324 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
4325 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4326
4327 cfg1 = RTL_R8(Config1);
4328 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
4329 RTL_W8(Config1, cfg1 & ~LEDS0);
4330
Francois Romieu2857ffb2008-08-02 21:08:49 +02004331 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
4332}
4333
4334static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4335{
françois romieu650e8d52011-01-03 15:08:29 +00004336 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004337
4338 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4339
4340 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
4341 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004342}
4343
4344static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
4345{
4346 rtl_hw_start_8102e_2(ioaddr, pdev);
4347
4348 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
4349}
4350
Hayes Wang5a5e4442011-02-22 17:26:21 +08004351static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4352{
4353 static const struct ephy_info e_info_8105e_1[] = {
4354 { 0x07, 0, 0x4000 },
4355 { 0x19, 0, 0x0200 },
4356 { 0x19, 0, 0x0020 },
4357 { 0x1e, 0, 0x2000 },
4358 { 0x03, 0, 0x0001 },
4359 { 0x19, 0, 0x0100 },
4360 { 0x19, 0, 0x0004 },
4361 { 0x0a, 0, 0x0020 }
4362 };
4363
Francois Romieucecb5fd2011-04-01 10:21:07 +02004364 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004365 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
4366
Francois Romieucecb5fd2011-04-01 10:21:07 +02004367 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004368 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
4369
4370 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
4371 RTL_W8(DLLPR, RTL_R8(DLLPR) | PM_SWITCH);
4372
4373 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
4374}
4375
4376static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4377{
4378 rtl_hw_start_8105e_1(ioaddr, pdev);
4379 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
4380}
4381
Francois Romieu07ce4062007-02-23 23:36:39 +01004382static void rtl_hw_start_8101(struct net_device *dev)
4383{
Francois Romieucdf1a602007-06-11 23:29:50 +02004384 struct rtl8169_private *tp = netdev_priv(dev);
4385 void __iomem *ioaddr = tp->mmio_addr;
4386 struct pci_dev *pdev = tp->pci_dev;
4387
Francois Romieucecb5fd2011-04-01 10:21:07 +02004388 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
4389 tp->mac_version == RTL_GIGA_MAC_VER_16) {
Jon Masone44daad2011-06-27 07:46:31 +00004390 int cap = pci_pcie_cap(pdev);
Francois Romieu9c14cea2008-07-05 00:21:15 +02004391
4392 if (cap) {
4393 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
4394 PCI_EXP_DEVCTL_NOSNOOP_EN);
4395 }
Francois Romieucdf1a602007-06-11 23:29:50 +02004396 }
4397
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004398 RTL_W8(Cfg9346, Cfg9346_Unlock);
4399
Francois Romieu2857ffb2008-08-02 21:08:49 +02004400 switch (tp->mac_version) {
4401 case RTL_GIGA_MAC_VER_07:
4402 rtl_hw_start_8102e_1(ioaddr, pdev);
4403 break;
4404
4405 case RTL_GIGA_MAC_VER_08:
4406 rtl_hw_start_8102e_3(ioaddr, pdev);
4407 break;
4408
4409 case RTL_GIGA_MAC_VER_09:
4410 rtl_hw_start_8102e_2(ioaddr, pdev);
4411 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004412
4413 case RTL_GIGA_MAC_VER_29:
4414 rtl_hw_start_8105e_1(ioaddr, pdev);
4415 break;
4416 case RTL_GIGA_MAC_VER_30:
4417 rtl_hw_start_8105e_2(ioaddr, pdev);
4418 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02004419 }
4420
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004421 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02004422
françois romieuf0298f82011-01-03 15:07:42 +00004423 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02004424
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004425 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02004426
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004427 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02004428 RTL_W16(CPlusCmd, tp->cp_cmd);
4429
4430 RTL_W16(IntrMitigate, 0x0000);
4431
4432 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4433
4434 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4435 rtl_set_rx_tx_config_registers(tp);
4436
Francois Romieucdf1a602007-06-11 23:29:50 +02004437 RTL_R8(IntrMask);
4438
Francois Romieucdf1a602007-06-11 23:29:50 +02004439 rtl_set_rx_mode(dev);
4440
4441 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004442
Francois Romieu0e485152007-02-20 00:00:26 +01004443 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444}
4445
4446static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
4447{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 if (new_mtu < ETH_ZLEN || new_mtu > SafeMtu)
4449 return -EINVAL;
4450
4451 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00004452 netdev_update_features(dev);
4453
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00004454 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455}
4456
4457static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
4458{
Al Viro95e09182007-12-22 18:55:39 +00004459 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
4461}
4462
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004463static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
4464 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004466 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004467 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004468
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004469 kfree(*data_buff);
4470 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 rtl8169_make_unusable_by_asic(desc);
4472}
4473
4474static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
4475{
4476 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
4477
4478 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
4479}
4480
4481static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
4482 u32 rx_buf_sz)
4483{
4484 desc->addr = cpu_to_le64(mapping);
4485 wmb();
4486 rtl8169_mark_to_asic(desc, rx_buf_sz);
4487}
4488
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004489static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004491 return (void *)ALIGN((long)data, 16);
4492}
4493
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004494static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
4495 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004496{
4497 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004499 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004500 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004501 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004503 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
4504 if (!data)
4505 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01004506
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004507 if (rtl8169_align(data) != data) {
4508 kfree(data);
4509 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
4510 if (!data)
4511 return NULL;
4512 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004513
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004514 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004515 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004516 if (unlikely(dma_mapping_error(d, mapping))) {
4517 if (net_ratelimit())
4518 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004519 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521
4522 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004523 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004524
4525err_out:
4526 kfree(data);
4527 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528}
4529
4530static void rtl8169_rx_clear(struct rtl8169_private *tp)
4531{
Francois Romieu07d3f512007-02-21 22:40:46 +01004532 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533
4534 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004535 if (tp->Rx_databuff[i]) {
4536 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 tp->RxDescArray + i);
4538 }
4539 }
4540}
4541
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004542static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004544 desc->opts1 |= cpu_to_le32(RingEnd);
4545}
Francois Romieu5b0384f2006-08-16 16:00:01 +02004546
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004547static int rtl8169_rx_fill(struct rtl8169_private *tp)
4548{
4549 unsigned int i;
4550
4551 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004552 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02004553
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004554 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004556
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004557 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004558 if (!data) {
4559 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004560 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004561 }
4562 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004565 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
4566 return 0;
4567
4568err_out:
4569 rtl8169_rx_clear(tp);
4570 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571}
4572
4573static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4574{
4575 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
4576}
4577
4578static int rtl8169_init_ring(struct net_device *dev)
4579{
4580 struct rtl8169_private *tp = netdev_priv(dev);
4581
4582 rtl8169_init_ring_indexes(tp);
4583
4584 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004585 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004587 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588}
4589
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004590static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 struct TxDesc *desc)
4592{
4593 unsigned int len = tx_skb->len;
4594
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004595 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
4596
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 desc->opts1 = 0x00;
4598 desc->opts2 = 0x00;
4599 desc->addr = 0x00;
4600 tx_skb->len = 0;
4601}
4602
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004603static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
4604 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605{
4606 unsigned int i;
4607
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004608 for (i = 0; i < n; i++) {
4609 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 struct ring_info *tx_skb = tp->tx_skb + entry;
4611 unsigned int len = tx_skb->len;
4612
4613 if (len) {
4614 struct sk_buff *skb = tx_skb->skb;
4615
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004616 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 tp->TxDescArray + entry);
4618 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00004619 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 dev_kfree_skb(skb);
4621 tx_skb->skb = NULL;
4622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 }
4624 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004625}
4626
4627static void rtl8169_tx_clear(struct rtl8169_private *tp)
4628{
4629 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 tp->cur_tx = tp->dirty_tx = 0;
4631}
4632
David Howellsc4028952006-11-22 14:57:56 +00004633static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634{
4635 struct rtl8169_private *tp = netdev_priv(dev);
4636
David Howellsc4028952006-11-22 14:57:56 +00004637 PREPARE_DELAYED_WORK(&tp->task, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 schedule_delayed_work(&tp->task, 4);
4639}
4640
4641static void rtl8169_wait_for_quiescence(struct net_device *dev)
4642{
4643 struct rtl8169_private *tp = netdev_priv(dev);
4644 void __iomem *ioaddr = tp->mmio_addr;
4645
4646 synchronize_irq(dev->irq);
4647
4648 /* Wait for any pending NAPI task to complete */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004649 napi_disable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650
4651 rtl8169_irq_mask_and_ack(ioaddr);
4652
David S. Millerd1d08d12008-01-07 20:53:33 -08004653 tp->intr_mask = 0xffff;
4654 RTL_W16(IntrMask, tp->intr_event);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004655 napi_enable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656}
4657
David Howellsc4028952006-11-22 14:57:56 +00004658static void rtl8169_reinit_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659{
David Howellsc4028952006-11-22 14:57:56 +00004660 struct rtl8169_private *tp =
4661 container_of(work, struct rtl8169_private, task.work);
4662 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 int ret;
4664
Francois Romieueb2a0212007-02-15 23:37:21 +01004665 rtnl_lock();
4666
4667 if (!netif_running(dev))
4668 goto out_unlock;
4669
4670 rtl8169_wait_for_quiescence(dev);
4671 rtl8169_close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672
4673 ret = rtl8169_open(dev);
4674 if (unlikely(ret < 0)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004675 if (net_ratelimit())
4676 netif_err(tp, drv, dev,
4677 "reinit failure (status = %d). Rescheduling\n",
4678 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 rtl8169_schedule_work(dev, rtl8169_reinit_task);
4680 }
Francois Romieueb2a0212007-02-15 23:37:21 +01004681
4682out_unlock:
4683 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684}
4685
David Howellsc4028952006-11-22 14:57:56 +00004686static void rtl8169_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687{
David Howellsc4028952006-11-22 14:57:56 +00004688 struct rtl8169_private *tp =
4689 container_of(work, struct rtl8169_private, task.work);
4690 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01004691 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692
Francois Romieueb2a0212007-02-15 23:37:21 +01004693 rtnl_lock();
4694
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 if (!netif_running(dev))
Francois Romieueb2a0212007-02-15 23:37:21 +01004696 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697
4698 rtl8169_wait_for_quiescence(dev);
4699
Francois Romieu56de4142011-03-15 17:29:31 +01004700 for (i = 0; i < NUM_RX_DESC; i++)
4701 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
4702
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 rtl8169_tx_clear(tp);
4704
Francois Romieu56de4142011-03-15 17:29:31 +01004705 rtl8169_init_ring_indexes(tp);
4706 rtl_hw_start(dev);
4707 netif_wake_queue(dev);
4708 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Francois Romieueb2a0212007-02-15 23:37:21 +01004709
4710out_unlock:
4711 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712}
4713
4714static void rtl8169_tx_timeout(struct net_device *dev)
4715{
4716 struct rtl8169_private *tp = netdev_priv(dev);
4717
françois romieue6de30d2011-01-03 15:08:37 +00004718 rtl8169_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719
4720 /* Let's wait a bit while any (async) irq lands on */
4721 rtl8169_schedule_work(dev, rtl8169_reset_task);
4722}
4723
4724static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07004725 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726{
4727 struct skb_shared_info *info = skb_shinfo(skb);
4728 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04004729 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004730 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731
4732 entry = tp->cur_tx;
4733 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
4734 skb_frag_t *frag = info->frags + cur_frag;
4735 dma_addr_t mapping;
4736 u32 status, len;
4737 void *addr;
4738
4739 entry = (entry + 1) % NUM_TX_DESC;
4740
4741 txd = tp->TxDescArray + entry;
4742 len = frag->size;
4743 addr = ((void *) page_address(frag->page)) + frag->page_offset;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004744 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004745 if (unlikely(dma_mapping_error(d, mapping))) {
4746 if (net_ratelimit())
4747 netif_err(tp, drv, tp->dev,
4748 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004749 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751
Francois Romieucecb5fd2011-04-01 10:21:07 +02004752 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07004753 status = opts[0] | len |
4754 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755
4756 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07004757 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 txd->addr = cpu_to_le64(mapping);
4759
4760 tp->tx_skb[entry].len = len;
4761 }
4762
4763 if (cur_frag) {
4764 tp->tx_skb[entry].skb = skb;
4765 txd->opts1 |= cpu_to_le32(LastFrag);
4766 }
4767
4768 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004769
4770err_out:
4771 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
4772 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773}
4774
Francois Romieu2b7b4312011-04-18 22:53:24 -07004775static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
4776 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777{
Francois Romieu2b7b4312011-04-18 22:53:24 -07004778 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00004779 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07004780 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781
Francois Romieu2b7b4312011-04-18 22:53:24 -07004782 if (mss) {
4783 opts[0] |= TD_LSO;
4784 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
4785 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07004786 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787
4788 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07004789 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07004791 opts[offset] |= info->checksum.udp;
4792 else
4793 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795}
4796
Stephen Hemminger613573252009-08-31 19:50:58 +00004797static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
4798 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799{
4800 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004801 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 struct TxDesc *txd = tp->TxDescArray + entry;
4803 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004804 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805 dma_addr_t mapping;
4806 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07004807 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004808 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02004809
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004811 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004812 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 }
4814
4815 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004816 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004818 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004819 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004820 if (unlikely(dma_mapping_error(d, mapping))) {
4821 if (net_ratelimit())
4822 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004823 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825
4826 tp->tx_skb[entry].len = len;
4827 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828
Francois Romieu2b7b4312011-04-18 22:53:24 -07004829 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
4830 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004831
Francois Romieu2b7b4312011-04-18 22:53:24 -07004832 rtl8169_tso_csum(tp, skb, opts);
4833
4834 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004835 if (frags < 0)
4836 goto err_dma_1;
4837 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07004838 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004839 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07004840 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004841 tp->tx_skb[entry].skb = skb;
4842 }
4843
Francois Romieu2b7b4312011-04-18 22:53:24 -07004844 txd->opts2 = cpu_to_le32(opts[1]);
4845
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 wmb();
4847
Francois Romieucecb5fd2011-04-01 10:21:07 +02004848 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07004849 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 txd->opts1 = cpu_to_le32(status);
4851
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 tp->cur_tx += frags + 1;
4853
David Dillow4c020a92010-03-03 16:33:10 +00004854 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855
Francois Romieucecb5fd2011-04-01 10:21:07 +02004856 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857
4858 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
4859 netif_stop_queue(dev);
4860 smp_rmb();
4861 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
4862 netif_wake_queue(dev);
4863 }
4864
Stephen Hemminger613573252009-08-31 19:50:58 +00004865 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004867err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004868 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004869err_dma_0:
4870 dev_kfree_skb(skb);
4871 dev->stats.tx_dropped++;
4872 return NETDEV_TX_OK;
4873
4874err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004876 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00004877 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878}
4879
4880static void rtl8169_pcierr_interrupt(struct net_device *dev)
4881{
4882 struct rtl8169_private *tp = netdev_priv(dev);
4883 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 u16 pci_status, pci_cmd;
4885
4886 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
4887 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
4888
Joe Perchesbf82c182010-02-09 11:49:50 +00004889 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
4890 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891
4892 /*
4893 * The recovery sequence below admits a very elaborated explanation:
4894 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01004895 * - I did not see what else could be done;
4896 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 *
4898 * Feel free to adjust to your needs.
4899 */
Francois Romieua27993f2006-12-18 00:04:19 +01004900 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01004901 pci_cmd &= ~PCI_COMMAND_PARITY;
4902 else
4903 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
4904
4905 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906
4907 pci_write_config_word(pdev, PCI_STATUS,
4908 pci_status & (PCI_STATUS_DETECTED_PARITY |
4909 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
4910 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
4911
4912 /* The infamous DAC f*ckup only happens at boot time */
4913 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00004914 void __iomem *ioaddr = tp->mmio_addr;
4915
Joe Perchesbf82c182010-02-09 11:49:50 +00004916 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 tp->cp_cmd &= ~PCIDAC;
4918 RTL_W16(CPlusCmd, tp->cp_cmd);
4919 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 }
4921
françois romieue6de30d2011-01-03 15:08:37 +00004922 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01004923
4924 rtl8169_schedule_work(dev, rtl8169_reinit_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925}
4926
Francois Romieu07d3f512007-02-21 22:40:46 +01004927static void rtl8169_tx_interrupt(struct net_device *dev,
4928 struct rtl8169_private *tp,
4929 void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930{
4931 unsigned int dirty_tx, tx_left;
4932
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 dirty_tx = tp->dirty_tx;
4934 smp_rmb();
4935 tx_left = tp->cur_tx - dirty_tx;
4936
4937 while (tx_left > 0) {
4938 unsigned int entry = dirty_tx % NUM_TX_DESC;
4939 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 u32 status;
4941
4942 rmb();
4943 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
4944 if (status & DescOwn)
4945 break;
4946
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004947 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
4948 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 if (status & LastFrag) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00004950 dev->stats.tx_packets++;
4951 dev->stats.tx_bytes += tx_skb->skb->len;
Eric Dumazet87433bf2009-06-09 22:55:53 +00004952 dev_kfree_skb(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 tx_skb->skb = NULL;
4954 }
4955 dirty_tx++;
4956 tx_left--;
4957 }
4958
4959 if (tp->dirty_tx != dirty_tx) {
4960 tp->dirty_tx = dirty_tx;
4961 smp_wmb();
4962 if (netif_queue_stopped(dev) &&
4963 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
4964 netif_wake_queue(dev);
4965 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02004966 /*
4967 * 8168 hack: TxPoll requests are lost when the Tx packets are
4968 * too close. Let's kick an extra TxPoll request when a burst
4969 * of start_xmit activity is detected (if it is not detected,
4970 * it is slow enough). -- FR
4971 */
4972 smp_rmb();
4973 if (tp->cur_tx != dirty_tx)
4974 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 }
4976}
4977
Francois Romieu126fa4b2005-05-12 20:09:17 -04004978static inline int rtl8169_fragmented_frame(u32 status)
4979{
4980 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
4981}
4982
Eric Dumazetadea1ac72010-09-05 20:04:05 -07004983static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 u32 status = opts1 & RxProtoMask;
4986
4987 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00004988 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 skb->ip_summed = CHECKSUM_UNNECESSARY;
4990 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07004991 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992}
4993
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004994static struct sk_buff *rtl8169_try_rx_copy(void *data,
4995 struct rtl8169_private *tp,
4996 int pkt_size,
4997 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02004999 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005000 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005002 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005003 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005004 prefetch(data);
5005 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5006 if (skb)
5007 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005008 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5009
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005010 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011}
5012
Francois Romieu07d3f512007-02-21 22:40:46 +01005013static int rtl8169_rx_interrupt(struct net_device *dev,
5014 struct rtl8169_private *tp,
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005015 void __iomem *ioaddr, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016{
5017 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005018 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 cur_rx = tp->cur_rx;
5021 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02005022 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005024 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005026 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 u32 status;
5028
5029 rmb();
Francois Romieu126fa4b2005-05-12 20:09:17 -04005030 status = le32_to_cpu(desc->opts1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031
5032 if (status & DescOwn)
5033 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005034 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005035 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5036 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005037 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02005039 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02005041 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005042 if (status & RxFOVF) {
5043 rtl8169_schedule_work(dev, rtl8169_reset_task);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005044 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005045 }
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005046 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005048 struct sk_buff *skb;
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005049 dma_addr_t addr = le64_to_cpu(desc->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 int pkt_size = (status & 0x00001FFF) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051
Francois Romieu126fa4b2005-05-12 20:09:17 -04005052 /*
5053 * The driver does not support incoming fragmented
5054 * frames. They are seen as a symptom of over-mtu
5055 * sized frames.
5056 */
5057 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02005058 dev->stats.rx_dropped++;
5059 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005060 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005061 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005062 }
5063
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005064 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5065 tp, pkt_size, addr);
5066 rtl8169_mark_to_asic(desc, rx_buf_sz);
5067 if (!skb) {
5068 dev->stats.rx_dropped++;
5069 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 }
5071
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005072 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 skb_put(skb, pkt_size);
5074 skb->protocol = eth_type_trans(skb, dev);
5075
Francois Romieu7a8fc772011-03-01 17:18:33 +01005076 rtl8169_rx_vlan_tag(desc, skb);
5077
Francois Romieu56de4142011-03-15 17:29:31 +01005078 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079
Francois Romieucebf8cc2007-10-18 12:06:54 +02005080 dev->stats.rx_bytes += pkt_size;
5081 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 }
Francois Romieu6dccd162007-02-13 23:38:05 +01005083
5084 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00005085 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01005086 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5087 desc->opts2 = 0;
5088 cur_rx++;
5089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 }
5091
5092 count = cur_rx - tp->cur_rx;
5093 tp->cur_rx = cur_rx;
5094
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005095 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096
5097 return count;
5098}
5099
Francois Romieu07d3f512007-02-21 22:40:46 +01005100static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101{
Francois Romieu07d3f512007-02-21 22:40:46 +01005102 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 int handled = 0;
Francois Romieu865c6522008-05-11 14:51:00 +02005106 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107
David Dillowf11a3772009-05-22 15:29:34 +00005108 /* loop handling interrupts until we have no new ones or
5109 * we hit a invalid/hotplug case.
5110 */
Francois Romieu865c6522008-05-11 14:51:00 +02005111 status = RTL_R16(IntrStatus);
David Dillowf11a3772009-05-22 15:29:34 +00005112 while (status && status != 0xffff) {
5113 handled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114
David Dillowf11a3772009-05-22 15:29:34 +00005115 /* Handle all of the error cases first. These will reset
5116 * the chip, so just exit the loop.
5117 */
5118 if (unlikely(!netif_running(dev))) {
5119 rtl8169_asic_down(ioaddr);
5120 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 }
David Dillowf11a3772009-05-22 15:29:34 +00005122
Francois Romieu1519e572011-02-03 12:02:36 +01005123 if (unlikely(status & RxFIFOOver)) {
5124 switch (tp->mac_version) {
5125 /* Work around for rx fifo overflow */
5126 case RTL_GIGA_MAC_VER_11:
5127 case RTL_GIGA_MAC_VER_22:
5128 case RTL_GIGA_MAC_VER_26:
5129 netif_stop_queue(dev);
5130 rtl8169_tx_timeout(dev);
5131 goto done;
Francois Romieuf60ac8e2011-02-03 17:27:52 +01005132 /* Testers needed. */
5133 case RTL_GIGA_MAC_VER_17:
5134 case RTL_GIGA_MAC_VER_19:
5135 case RTL_GIGA_MAC_VER_20:
5136 case RTL_GIGA_MAC_VER_21:
5137 case RTL_GIGA_MAC_VER_23:
5138 case RTL_GIGA_MAC_VER_24:
5139 case RTL_GIGA_MAC_VER_27:
5140 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00005141 case RTL_GIGA_MAC_VER_31:
Francois Romieu1519e572011-02-03 12:02:36 +01005142 /* Experimental science. Pktgen proof. */
5143 case RTL_GIGA_MAC_VER_12:
5144 case RTL_GIGA_MAC_VER_25:
5145 if (status == RxFIFOOver)
5146 goto done;
5147 break;
5148 default:
5149 break;
5150 }
David Dillowf11a3772009-05-22 15:29:34 +00005151 }
5152
5153 if (unlikely(status & SYSErr)) {
5154 rtl8169_pcierr_interrupt(dev);
5155 break;
5156 }
5157
5158 if (status & LinkChg)
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005159 __rtl8169_check_link_status(dev, tp, ioaddr, true);
David Dillowf11a3772009-05-22 15:29:34 +00005160
5161 /* We need to see the lastest version of tp->intr_mask to
5162 * avoid ignoring an MSI interrupt and having to wait for
5163 * another event which may never come.
5164 */
5165 smp_rmb();
5166 if (status & tp->intr_mask & tp->napi_event) {
5167 RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
5168 tp->intr_mask = ~tp->napi_event;
5169
5170 if (likely(napi_schedule_prep(&tp->napi)))
5171 __napi_schedule(&tp->napi);
Joe Perchesbf82c182010-02-09 11:49:50 +00005172 else
5173 netif_info(tp, intr, dev,
5174 "interrupt %04x in poll\n", status);
David Dillowf11a3772009-05-22 15:29:34 +00005175 }
5176
5177 /* We only get a new MSI interrupt when all active irq
5178 * sources on the chip have been acknowledged. So, ack
5179 * everything we've seen and check if new sources have become
5180 * active to avoid blocking all interrupts from the chip.
5181 */
5182 RTL_W16(IntrStatus,
5183 (status & RxFIFOOver) ? (status | RxOverflow) : status);
5184 status = RTL_R16(IntrStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 }
Francois Romieu1519e572011-02-03 12:02:36 +01005186done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 return IRQ_RETVAL(handled);
5188}
5189
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005190static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005192 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5193 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 void __iomem *ioaddr = tp->mmio_addr;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005195 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005197 work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 rtl8169_tx_interrupt(dev, tp, ioaddr);
5199
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005200 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005201 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00005202
5203 /* We need for force the visibility of tp->intr_mask
5204 * for other CPUs, as we can loose an MSI interrupt
5205 * and potentially wait for a retransmit timeout if we don't.
5206 * The posted write to IntrMask is safe, as it will
5207 * eventually make it to the chip and we won't loose anything
5208 * until it does.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 */
David Dillowf11a3772009-05-22 15:29:34 +00005210 tp->intr_mask = 0xffff;
David Dillow4c020a92010-03-03 16:33:10 +00005211 wmb();
Francois Romieu0e485152007-02-20 00:00:26 +01005212 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 }
5214
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005215 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217
Francois Romieu523a6092008-09-10 22:28:56 +02005218static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5219{
5220 struct rtl8169_private *tp = netdev_priv(dev);
5221
5222 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5223 return;
5224
5225 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5226 RTL_W32(RxMissed, 0);
5227}
5228
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229static void rtl8169_down(struct net_device *dev)
5230{
5231 struct rtl8169_private *tp = netdev_priv(dev);
5232 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233
Francois Romieu4876cc12011-03-11 21:07:11 +01005234 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235
5236 netif_stop_queue(dev);
5237
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005238 napi_disable(&tp->napi);
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005239
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240 spin_lock_irq(&tp->lock);
5241
5242 rtl8169_asic_down(ioaddr);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005243 /*
5244 * At this point device interrupts can not be enabled in any function,
5245 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task,
5246 * rtl8169_reinit_task) and napi is disabled (rtl8169_poll).
5247 */
Francois Romieu523a6092008-09-10 22:28:56 +02005248 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249
5250 spin_unlock_irq(&tp->lock);
5251
5252 synchronize_irq(dev->irq);
5253
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 /* Give a racing hard_start_xmit a few cycles to complete. */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07005255 synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257 rtl8169_tx_clear(tp);
5258
5259 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00005260
5261 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262}
5263
5264static int rtl8169_close(struct net_device *dev)
5265{
5266 struct rtl8169_private *tp = netdev_priv(dev);
5267 struct pci_dev *pdev = tp->pci_dev;
5268
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005269 pm_runtime_get_sync(&pdev->dev);
5270
Francois Romieucecb5fd2011-04-01 10:21:07 +02005271 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08005272 rtl8169_update_counters(dev);
5273
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 rtl8169_down(dev);
5275
5276 free_irq(dev->irq, dev);
5277
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00005278 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5279 tp->RxPhyAddr);
5280 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5281 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 tp->TxDescArray = NULL;
5283 tp->RxDescArray = NULL;
5284
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005285 pm_runtime_put_sync(&pdev->dev);
5286
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 return 0;
5288}
5289
Francois Romieu07ce4062007-02-23 23:36:39 +01005290static void rtl_set_rx_mode(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291{
5292 struct rtl8169_private *tp = netdev_priv(dev);
5293 void __iomem *ioaddr = tp->mmio_addr;
5294 unsigned long flags;
5295 u32 mc_filter[2]; /* Multicast hash filter */
Francois Romieu07d3f512007-02-21 22:40:46 +01005296 int rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 u32 tmp = 0;
5298
5299 if (dev->flags & IFF_PROMISC) {
5300 /* Unconditionally log net taps. */
Joe Perchesbf82c182010-02-09 11:49:50 +00005301 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302 rx_mode =
5303 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5304 AcceptAllPhys;
5305 mc_filter[1] = mc_filter[0] = 0xffffffff;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00005306 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
Joe Perches8e95a202009-12-03 07:58:21 +00005307 (dev->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308 /* Too many to filter perfectly -- accept all multicasts. */
5309 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5310 mc_filter[1] = mc_filter[0] = 0xffffffff;
5311 } else {
Jiri Pirko22bedad2010-04-01 21:22:57 +00005312 struct netdev_hw_addr *ha;
Francois Romieu07d3f512007-02-21 22:40:46 +01005313
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314 rx_mode = AcceptBroadcast | AcceptMyPhys;
5315 mc_filter[1] = mc_filter[0] = 0;
Jiri Pirko22bedad2010-04-01 21:22:57 +00005316 netdev_for_each_mc_addr(ha, dev) {
5317 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5319 rx_mode |= AcceptMulticast;
5320 }
5321 }
5322
5323 spin_lock_irqsave(&tp->lock, flags);
5324
5325 tmp = rtl8169_rx_config | rx_mode |
Francois Romieu2b7b4312011-04-18 22:53:24 -07005326 (RTL_R32(RxConfig) & RTL_RX_CONFIG_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327
Francois Romieuf887cce2008-07-17 22:24:18 +02005328 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
Francois Romieu1087f4f2007-12-26 22:46:05 +01005329 u32 data = mc_filter[0];
5330
5331 mc_filter[0] = swab32(mc_filter[1]);
5332 mc_filter[1] = swab32(data);
Francois Romieubcf0bf92006-07-26 23:14:13 +02005333 }
5334
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 RTL_W32(MAR0 + 4, mc_filter[1]);
Francois Romieu78f1cd02010-03-27 19:35:46 -07005336 RTL_W32(MAR0 + 0, mc_filter[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337
Francois Romieu57a9f232007-06-04 22:10:15 +02005338 RTL_W32(RxConfig, tmp);
5339
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 spin_unlock_irqrestore(&tp->lock, flags);
5341}
5342
5343/**
5344 * rtl8169_get_stats - Get rtl8169 read/write statistics
5345 * @dev: The Ethernet Device to get statistics for
5346 *
5347 * Get TX/RX statistics for rtl8169
5348 */
5349static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
5350{
5351 struct rtl8169_private *tp = netdev_priv(dev);
5352 void __iomem *ioaddr = tp->mmio_addr;
5353 unsigned long flags;
5354
5355 if (netif_running(dev)) {
5356 spin_lock_irqsave(&tp->lock, flags);
Francois Romieu523a6092008-09-10 22:28:56 +02005357 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358 spin_unlock_irqrestore(&tp->lock, flags);
5359 }
Francois Romieu5b0384f2006-08-16 16:00:01 +02005360
Francois Romieucebf8cc2007-10-18 12:06:54 +02005361 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362}
5363
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005364static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01005365{
françois romieu065c27c2011-01-03 15:08:12 +00005366 struct rtl8169_private *tp = netdev_priv(dev);
5367
Francois Romieu5d06a992006-02-23 00:47:58 +01005368 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005369 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01005370
françois romieu065c27c2011-01-03 15:08:12 +00005371 rtl_pll_power_down(tp);
5372
Francois Romieu5d06a992006-02-23 00:47:58 +01005373 netif_device_detach(dev);
5374 netif_stop_queue(dev);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005375}
Francois Romieu5d06a992006-02-23 00:47:58 +01005376
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005377#ifdef CONFIG_PM
5378
5379static int rtl8169_suspend(struct device *device)
5380{
5381 struct pci_dev *pdev = to_pci_dev(device);
5382 struct net_device *dev = pci_get_drvdata(pdev);
5383
5384 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02005385
Francois Romieu5d06a992006-02-23 00:47:58 +01005386 return 0;
5387}
5388
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005389static void __rtl8169_resume(struct net_device *dev)
5390{
françois romieu065c27c2011-01-03 15:08:12 +00005391 struct rtl8169_private *tp = netdev_priv(dev);
5392
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005393 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00005394
5395 rtl_pll_power_up(tp);
5396
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005397 rtl8169_schedule_work(dev, rtl8169_reset_task);
5398}
5399
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005400static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01005401{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005402 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01005403 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005404 struct rtl8169_private *tp = netdev_priv(dev);
5405
5406 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01005407
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005408 if (netif_running(dev))
5409 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01005410
Francois Romieu5d06a992006-02-23 00:47:58 +01005411 return 0;
5412}
5413
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005414static int rtl8169_runtime_suspend(struct device *device)
5415{
5416 struct pci_dev *pdev = to_pci_dev(device);
5417 struct net_device *dev = pci_get_drvdata(pdev);
5418 struct rtl8169_private *tp = netdev_priv(dev);
5419
5420 if (!tp->TxDescArray)
5421 return 0;
5422
5423 spin_lock_irq(&tp->lock);
5424 tp->saved_wolopts = __rtl8169_get_wol(tp);
5425 __rtl8169_set_wol(tp, WAKE_ANY);
5426 spin_unlock_irq(&tp->lock);
5427
5428 rtl8169_net_suspend(dev);
5429
5430 return 0;
5431}
5432
5433static int rtl8169_runtime_resume(struct device *device)
5434{
5435 struct pci_dev *pdev = to_pci_dev(device);
5436 struct net_device *dev = pci_get_drvdata(pdev);
5437 struct rtl8169_private *tp = netdev_priv(dev);
5438
5439 if (!tp->TxDescArray)
5440 return 0;
5441
5442 spin_lock_irq(&tp->lock);
5443 __rtl8169_set_wol(tp, tp->saved_wolopts);
5444 tp->saved_wolopts = 0;
5445 spin_unlock_irq(&tp->lock);
5446
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005447 rtl8169_init_phy(dev, tp);
5448
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005449 __rtl8169_resume(dev);
5450
5451 return 0;
5452}
5453
5454static int rtl8169_runtime_idle(struct device *device)
5455{
5456 struct pci_dev *pdev = to_pci_dev(device);
5457 struct net_device *dev = pci_get_drvdata(pdev);
5458 struct rtl8169_private *tp = netdev_priv(dev);
5459
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005460 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005461}
5462
Alexey Dobriyan47145212009-12-14 18:00:08 -08005463static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02005464 .suspend = rtl8169_suspend,
5465 .resume = rtl8169_resume,
5466 .freeze = rtl8169_suspend,
5467 .thaw = rtl8169_resume,
5468 .poweroff = rtl8169_suspend,
5469 .restore = rtl8169_resume,
5470 .runtime_suspend = rtl8169_runtime_suspend,
5471 .runtime_resume = rtl8169_runtime_resume,
5472 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005473};
5474
5475#define RTL8169_PM_OPS (&rtl8169_pm_ops)
5476
5477#else /* !CONFIG_PM */
5478
5479#define RTL8169_PM_OPS NULL
5480
5481#endif /* !CONFIG_PM */
5482
Francois Romieu1765f952008-09-13 17:21:40 +02005483static void rtl_shutdown(struct pci_dev *pdev)
5484{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005485 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00005486 struct rtl8169_private *tp = netdev_priv(dev);
5487 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu1765f952008-09-13 17:21:40 +02005488
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005489 rtl8169_net_suspend(dev);
5490
Francois Romieucecb5fd2011-04-01 10:21:07 +02005491 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08005492 rtl_rar_set(tp, dev->perm_addr);
5493
françois romieu4bb3f522009-06-17 11:41:45 +00005494 spin_lock_irq(&tp->lock);
5495
5496 rtl8169_asic_down(ioaddr);
5497
5498 spin_unlock_irq(&tp->lock);
5499
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005500 if (system_state == SYSTEM_POWER_OFF) {
françois romieuca52efd2009-07-24 12:34:19 +00005501 /* WoL fails with some 8168 when the receiver is disabled. */
5502 if (tp->features & RTL_FEATURE_WOL) {
5503 pci_clear_master(pdev);
5504
5505 RTL_W8(ChipCmd, CmdRxEnb);
5506 /* PCI commit */
5507 RTL_R8(ChipCmd);
5508 }
5509
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005510 pci_wake_from_d3(pdev, true);
5511 pci_set_power_state(pdev, PCI_D3hot);
5512 }
5513}
Francois Romieu5d06a992006-02-23 00:47:58 +01005514
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515static struct pci_driver rtl8169_pci_driver = {
5516 .name = MODULENAME,
5517 .id_table = rtl8169_pci_tbl,
5518 .probe = rtl8169_init_one,
5519 .remove = __devexit_p(rtl8169_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02005520 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005521 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522};
5523
Francois Romieu07d3f512007-02-21 22:40:46 +01005524static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525{
Jeff Garzik29917622006-08-19 17:48:59 -04005526 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527}
5528
Francois Romieu07d3f512007-02-21 22:40:46 +01005529static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530{
5531 pci_unregister_driver(&rtl8169_pci_driver);
5532}
5533
5534module_init(rtl8169_init_module);
5535module_exit(rtl8169_cleanup_module);