blob: f8c4435afe515942fd5dc38074d6f8895483b97b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Francois Romieu07d3f512007-02-21 22:40:46 +01002 * r8169.c: RealTek 8169/8168/8101 ethernet driver.
3 *
4 * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
5 * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
6 * Copyright (c) a lot of people too. Please respect their work.
7 *
8 * See MAINTAINERS file for support contact information.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11#include <linux/module.h>
12#include <linux/moduleparam.h>
13#include <linux/pci.h>
14#include <linux/netdevice.h>
15#include <linux/etherdevice.h>
16#include <linux/delay.h>
17#include <linux/ethtool.h>
18#include <linux/mii.h>
19#include <linux/if_vlan.h>
20#include <linux/crc32.h>
21#include <linux/in.h>
22#include <linux/ip.h>
23#include <linux/tcp.h>
24#include <linux/init.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000025#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/dma-mapping.h>
Rafael J. Wysockie1759442010-03-14 14:33:51 +000027#include <linux/pm_runtime.h>
françois romieubca03d52011-01-03 15:07:31 +000028#include <linux/firmware.h>
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +000029#include <linux/pci-aspm.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040030#include <linux/prefetch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Francois Romieu99f252b2007-04-02 22:59:59 +020032#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/io.h>
34#include <asm/irq.h>
35
Francois Romieu865c6522008-05-11 14:51:00 +020036#define RTL8169_VERSION "2.3LK-NAPI"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define MODULENAME "r8169"
38#define PFX MODULENAME ": "
39
françois romieubca03d52011-01-03 15:07:31 +000040#define FIRMWARE_8168D_1 "rtl_nic/rtl8168d-1.fw"
41#define FIRMWARE_8168D_2 "rtl_nic/rtl8168d-2.fw"
hayeswang01dc7fe2011-03-21 01:50:28 +000042#define FIRMWARE_8168E_1 "rtl_nic/rtl8168e-1.fw"
43#define FIRMWARE_8168E_2 "rtl_nic/rtl8168e-2.fw"
Hayes Wang5a5e4442011-02-22 17:26:21 +080044#define FIRMWARE_8105E_1 "rtl_nic/rtl8105e-1.fw"
françois romieubca03d52011-01-03 15:07:31 +000045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#ifdef RTL8169_DEBUG
47#define assert(expr) \
Francois Romieu5b0384f2006-08-16 16:00:01 +020048 if (!(expr)) { \
49 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
Harvey Harrisonb39d66a2008-08-20 16:52:04 -070050 #expr,__FILE__,__func__,__LINE__); \
Francois Romieu5b0384f2006-08-16 16:00:01 +020051 }
Joe Perches06fa7352007-10-18 21:15:00 +020052#define dprintk(fmt, args...) \
53 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#else
55#define assert(expr) do {} while (0)
56#define dprintk(fmt, args...) do {} while (0)
57#endif /* RTL8169_DEBUG */
58
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020059#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d2005-09-30 16:54:02 -070060 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#define TX_BUFFS_AVAIL(tp) \
63 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
66 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050067static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69/* MAC address length */
70#define MAC_ADDR_LEN 6
71
Francois Romieu9c14cea2008-07-05 00:21:15 +020072#define MAX_READ_REQUEST_SHIFT 12
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#define RX_FIFO_THRESH 7 /* 7 means NO threshold, Rx buffer level before first PCI xfer. */
74#define RX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
75#define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
77#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
78
79#define R8169_REGS_SIZE 256
80#define R8169_NAPI_WEIGHT 64
81#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
82#define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
83#define RX_BUF_SIZE 1536 /* Rx Buffer size */
84#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
85#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
86
87#define RTL8169_TX_TIMEOUT (6*HZ)
88#define RTL8169_PHY_TIMEOUT (10*HZ)
89
françois romieuea8dbdd2009-03-15 01:10:50 +000090#define RTL_EEPROM_SIG cpu_to_le32(0x8129)
91#define RTL_EEPROM_SIG_MASK cpu_to_le32(0xffff)
Francois Romieue1564ec2008-10-16 22:46:13 +020092#define RTL_EEPROM_SIG_ADDR 0x0000
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094/* write/read MMIO register */
95#define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
96#define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
97#define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
98#define RTL_R8(reg) readb (ioaddr + (reg))
99#define RTL_R16(reg) readw (ioaddr + (reg))
Junchang Wang06f555f2010-05-30 02:26:07 +0000100#define RTL_R32(reg) readl (ioaddr + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102enum mac_version {
Francois Romieu85bffe62011-04-27 08:22:39 +0200103 RTL_GIGA_MAC_VER_01 = 0,
104 RTL_GIGA_MAC_VER_02,
105 RTL_GIGA_MAC_VER_03,
106 RTL_GIGA_MAC_VER_04,
107 RTL_GIGA_MAC_VER_05,
108 RTL_GIGA_MAC_VER_06,
109 RTL_GIGA_MAC_VER_07,
110 RTL_GIGA_MAC_VER_08,
111 RTL_GIGA_MAC_VER_09,
112 RTL_GIGA_MAC_VER_10,
113 RTL_GIGA_MAC_VER_11,
114 RTL_GIGA_MAC_VER_12,
115 RTL_GIGA_MAC_VER_13,
116 RTL_GIGA_MAC_VER_14,
117 RTL_GIGA_MAC_VER_15,
118 RTL_GIGA_MAC_VER_16,
119 RTL_GIGA_MAC_VER_17,
120 RTL_GIGA_MAC_VER_18,
121 RTL_GIGA_MAC_VER_19,
122 RTL_GIGA_MAC_VER_20,
123 RTL_GIGA_MAC_VER_21,
124 RTL_GIGA_MAC_VER_22,
125 RTL_GIGA_MAC_VER_23,
126 RTL_GIGA_MAC_VER_24,
127 RTL_GIGA_MAC_VER_25,
128 RTL_GIGA_MAC_VER_26,
129 RTL_GIGA_MAC_VER_27,
130 RTL_GIGA_MAC_VER_28,
131 RTL_GIGA_MAC_VER_29,
132 RTL_GIGA_MAC_VER_30,
133 RTL_GIGA_MAC_VER_31,
134 RTL_GIGA_MAC_VER_32,
135 RTL_GIGA_MAC_VER_33,
136 RTL_GIGA_MAC_NONE = 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137};
138
Francois Romieu2b7b4312011-04-18 22:53:24 -0700139enum rtl_tx_desc_version {
140 RTL_TD_0 = 0,
141 RTL_TD_1 = 1,
142};
143
Francois Romieu85bffe62011-04-27 08:22:39 +0200144#define _R(NAME,TD,FW) \
145 { .name = NAME, .txd_version = TD, .fw_name = FW }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800147static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 const char *name;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700149 enum rtl_tx_desc_version txd_version;
Francois Romieu85bffe62011-04-27 08:22:39 +0200150 const char *fw_name;
151} rtl_chip_infos[] = {
152 /* PCI devices. */
153 [RTL_GIGA_MAC_VER_01] =
154 _R("RTL8169", RTL_TD_0, NULL),
155 [RTL_GIGA_MAC_VER_02] =
156 _R("RTL8169s", RTL_TD_0, NULL),
157 [RTL_GIGA_MAC_VER_03] =
158 _R("RTL8110s", RTL_TD_0, NULL),
159 [RTL_GIGA_MAC_VER_04] =
160 _R("RTL8169sb/8110sb", RTL_TD_0, NULL),
161 [RTL_GIGA_MAC_VER_05] =
162 _R("RTL8169sc/8110sc", RTL_TD_0, NULL),
163 [RTL_GIGA_MAC_VER_06] =
164 _R("RTL8169sc/8110sc", RTL_TD_0, NULL),
165 /* PCI-E devices. */
166 [RTL_GIGA_MAC_VER_07] =
167 _R("RTL8102e", RTL_TD_1, NULL),
168 [RTL_GIGA_MAC_VER_08] =
169 _R("RTL8102e", RTL_TD_1, NULL),
170 [RTL_GIGA_MAC_VER_09] =
171 _R("RTL8102e", RTL_TD_1, NULL),
172 [RTL_GIGA_MAC_VER_10] =
173 _R("RTL8101e", RTL_TD_0, NULL),
174 [RTL_GIGA_MAC_VER_11] =
175 _R("RTL8168b/8111b", RTL_TD_0, NULL),
176 [RTL_GIGA_MAC_VER_12] =
177 _R("RTL8168b/8111b", RTL_TD_0, NULL),
178 [RTL_GIGA_MAC_VER_13] =
179 _R("RTL8101e", RTL_TD_0, NULL),
180 [RTL_GIGA_MAC_VER_14] =
181 _R("RTL8100e", RTL_TD_0, NULL),
182 [RTL_GIGA_MAC_VER_15] =
183 _R("RTL8100e", RTL_TD_0, NULL),
184 [RTL_GIGA_MAC_VER_16] =
185 _R("RTL8101e", RTL_TD_0, NULL),
186 [RTL_GIGA_MAC_VER_17] =
187 _R("RTL8168b/8111b", RTL_TD_0, NULL),
188 [RTL_GIGA_MAC_VER_18] =
189 _R("RTL8168cp/8111cp", RTL_TD_1, NULL),
190 [RTL_GIGA_MAC_VER_19] =
191 _R("RTL8168c/8111c", RTL_TD_1, NULL),
192 [RTL_GIGA_MAC_VER_20] =
193 _R("RTL8168c/8111c", RTL_TD_1, NULL),
194 [RTL_GIGA_MAC_VER_21] =
195 _R("RTL8168c/8111c", RTL_TD_1, NULL),
196 [RTL_GIGA_MAC_VER_22] =
197 _R("RTL8168c/8111c", RTL_TD_1, NULL),
198 [RTL_GIGA_MAC_VER_23] =
199 _R("RTL8168cp/8111cp", RTL_TD_1, NULL),
200 [RTL_GIGA_MAC_VER_24] =
201 _R("RTL8168cp/8111cp", RTL_TD_1, NULL),
202 [RTL_GIGA_MAC_VER_25] =
203 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_1),
204 [RTL_GIGA_MAC_VER_26] =
205 _R("RTL8168d/8111d", RTL_TD_1, FIRMWARE_8168D_2),
206 [RTL_GIGA_MAC_VER_27] =
207 _R("RTL8168dp/8111dp", RTL_TD_1, NULL),
208 [RTL_GIGA_MAC_VER_28] =
209 _R("RTL8168dp/8111dp", RTL_TD_1, NULL),
210 [RTL_GIGA_MAC_VER_29] =
211 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1),
212 [RTL_GIGA_MAC_VER_30] =
213 _R("RTL8105e", RTL_TD_1, FIRMWARE_8105E_1),
214 [RTL_GIGA_MAC_VER_31] =
215 _R("RTL8168dp/8111dp", RTL_TD_1, NULL),
216 [RTL_GIGA_MAC_VER_32] =
217 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_1),
218 [RTL_GIGA_MAC_VER_33] =
219 _R("RTL8168e/8111e", RTL_TD_1, FIRMWARE_8168E_2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220};
221#undef _R
222
Francois Romieubcf0bf92006-07-26 23:14:13 +0200223enum cfg_version {
224 RTL_CFG_0 = 0x00,
225 RTL_CFG_1,
226 RTL_CFG_2
227};
228
Francois Romieu07ce4062007-02-23 23:36:39 +0100229static void rtl_hw_start_8169(struct net_device *);
230static void rtl_hw_start_8168(struct net_device *);
231static void rtl_hw_start_8101(struct net_device *);
232
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000233static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200234 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200235 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Francois Romieud81bf552006-09-20 21:31:20 +0200236 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100237 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200238 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
239 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200240 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200241 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
242 { PCI_VENDOR_ID_LINKSYS, 0x1032,
243 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100244 { 0x0001, 0x8168,
245 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 {0,},
247};
248
249MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
250
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000251static int rx_buf_sz = 16383;
David S. Miller4300e8c2010-03-26 10:23:30 -0700252static int use_dac;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200253static struct {
254 u32 msg_enable;
255} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Francois Romieu07d3f512007-02-21 22:40:46 +0100257enum rtl_registers {
258 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100259 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100260 MAR0 = 8, /* Multicast filter. */
261 CounterAddrLow = 0x10,
262 CounterAddrHigh = 0x14,
263 TxDescStartAddrLow = 0x20,
264 TxDescStartAddrHigh = 0x24,
265 TxHDescStartAddrLow = 0x28,
266 TxHDescStartAddrHigh = 0x2c,
267 FLASH = 0x30,
268 ERSR = 0x36,
269 ChipCmd = 0x37,
270 TxPoll = 0x38,
271 IntrMask = 0x3c,
272 IntrStatus = 0x3e,
273 TxConfig = 0x40,
274 RxConfig = 0x44,
Francois Romieu2b7b4312011-04-18 22:53:24 -0700275
276#define RTL_RX_CONFIG_MASK 0xff7e1880u
277
Francois Romieu07d3f512007-02-21 22:40:46 +0100278 RxMissed = 0x4c,
279 Cfg9346 = 0x50,
280 Config0 = 0x51,
281 Config1 = 0x52,
282 Config2 = 0x53,
283 Config3 = 0x54,
284 Config4 = 0x55,
285 Config5 = 0x56,
286 MultiIntr = 0x5c,
287 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100288 PHYstatus = 0x6c,
289 RxMaxSize = 0xda,
290 CPlusCmd = 0xe0,
291 IntrMitigate = 0xe2,
292 RxDescAddrLow = 0xe4,
293 RxDescAddrHigh = 0xe8,
françois romieuf0298f82011-01-03 15:07:42 +0000294 EarlyTxThres = 0xec, /* 8169. Unit of 32 bytes. */
295
296#define NoEarlyTx 0x3f /* Max value : no early transmit. */
297
298 MaxTxPacketSize = 0xec, /* 8101/8168. Unit of 128 bytes. */
299
300#define TxPacketMax (8064 >> 7)
301
Francois Romieu07d3f512007-02-21 22:40:46 +0100302 FuncEvent = 0xf0,
303 FuncEventMask = 0xf4,
304 FuncPresetState = 0xf8,
305 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306};
307
Francois Romieuf162a5d2008-06-01 22:37:49 +0200308enum rtl8110_registers {
309 TBICSR = 0x64,
310 TBI_ANAR = 0x68,
311 TBI_LPAR = 0x6a,
312};
313
314enum rtl8168_8101_registers {
315 CSIDR = 0x64,
316 CSIAR = 0x68,
317#define CSIAR_FLAG 0x80000000
318#define CSIAR_WRITE_CMD 0x80000000
319#define CSIAR_BYTE_ENABLE 0x0f
320#define CSIAR_BYTE_ENABLE_SHIFT 12
321#define CSIAR_ADDR_MASK 0x0fff
françois romieu065c27c2011-01-03 15:08:12 +0000322 PMCH = 0x6f,
Francois Romieuf162a5d2008-06-01 22:37:49 +0200323 EPHYAR = 0x80,
324#define EPHYAR_FLAG 0x80000000
325#define EPHYAR_WRITE_CMD 0x80000000
326#define EPHYAR_REG_MASK 0x1f
327#define EPHYAR_REG_SHIFT 16
328#define EPHYAR_DATA_MASK 0xffff
Hayes Wang5a5e4442011-02-22 17:26:21 +0800329 DLLPR = 0xd0,
330#define PM_SWITCH (1 << 6)
Francois Romieuf162a5d2008-06-01 22:37:49 +0200331 DBG_REG = 0xd1,
332#define FIX_NAK_1 (1 << 4)
333#define FIX_NAK_2 (1 << 3)
Hayes Wang5a5e4442011-02-22 17:26:21 +0800334 TWSI = 0xd2,
335 MCU = 0xd3,
336#define EN_NDP (1 << 3)
337#define EN_OOB_RESET (1 << 2)
françois romieudaf9df62009-10-07 12:44:20 +0000338 EFUSEAR = 0xdc,
339#define EFUSEAR_FLAG 0x80000000
340#define EFUSEAR_WRITE_CMD 0x80000000
341#define EFUSEAR_READ_CMD 0x00000000
342#define EFUSEAR_REG_MASK 0x03ff
343#define EFUSEAR_REG_SHIFT 8
344#define EFUSEAR_DATA_MASK 0xff
Francois Romieuf162a5d2008-06-01 22:37:49 +0200345};
346
françois romieuc0e45c12011-01-03 15:08:04 +0000347enum rtl8168_registers {
françois romieub646d902011-01-03 15:08:21 +0000348 ERIDR = 0x70,
349 ERIAR = 0x74,
350#define ERIAR_FLAG 0x80000000
351#define ERIAR_WRITE_CMD 0x80000000
352#define ERIAR_READ_CMD 0x00000000
353#define ERIAR_ADDR_BYTE_ALIGN 4
354#define ERIAR_EXGMAC 0
355#define ERIAR_MSIX 1
356#define ERIAR_ASF 2
357#define ERIAR_TYPE_SHIFT 16
358#define ERIAR_BYTEEN 0x0f
359#define ERIAR_BYTEEN_SHIFT 12
françois romieuc0e45c12011-01-03 15:08:04 +0000360 EPHY_RXER_NUM = 0x7c,
361 OCPDR = 0xb0, /* OCP GPHY access */
362#define OCPDR_WRITE_CMD 0x80000000
363#define OCPDR_READ_CMD 0x00000000
364#define OCPDR_REG_MASK 0x7f
365#define OCPDR_GPHY_REG_SHIFT 16
366#define OCPDR_DATA_MASK 0xffff
367 OCPAR = 0xb4,
368#define OCPAR_FLAG 0x80000000
369#define OCPAR_GPHY_WRITE_CMD 0x8000f060
370#define OCPAR_GPHY_READ_CMD 0x0000f060
hayeswang01dc7fe2011-03-21 01:50:28 +0000371 RDSAR1 = 0xd0, /* 8168c only. Undocumented on 8168dp */
372 MISC = 0xf0, /* 8168e only. */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200373#define TXPLA_RST (1 << 29)
françois romieuc0e45c12011-01-03 15:08:04 +0000374};
375
Francois Romieu07d3f512007-02-21 22:40:46 +0100376enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100378 SYSErr = 0x8000,
379 PCSTimeout = 0x4000,
380 SWInt = 0x0100,
381 TxDescUnavail = 0x0080,
382 RxFIFOOver = 0x0040,
383 LinkChg = 0x0020,
384 RxOverflow = 0x0010,
385 TxErr = 0x0008,
386 TxOK = 0x0004,
387 RxErr = 0x0002,
388 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 /* RxStatusDesc */
Francois Romieu9dccf612006-05-14 12:31:17 +0200391 RxFOVF = (1 << 23),
392 RxRWT = (1 << 22),
393 RxRES = (1 << 21),
394 RxRUNT = (1 << 20),
395 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 /* ChipCmdBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100398 CmdReset = 0x10,
399 CmdRxEnb = 0x08,
400 CmdTxEnb = 0x04,
401 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Francois Romieu275391a2007-02-23 23:50:28 +0100403 /* TXPoll register p.5 */
404 HPQ = 0x80, /* Poll cmd on the high prio queue */
405 NPQ = 0x40, /* Poll cmd on the low prio queue */
406 FSWInt = 0x01, /* Forced software interrupt */
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100409 Cfg9346_Lock = 0x00,
410 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100413 AcceptErr = 0x20,
414 AcceptRunt = 0x10,
415 AcceptBroadcast = 0x08,
416 AcceptMulticast = 0x04,
417 AcceptMyPhys = 0x02,
418 AcceptAllPhys = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 /* RxConfigBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100421 RxCfgFIFOShift = 13,
422 RxCfgDMAShift = 8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 /* TxConfigBits */
425 TxInterFrameGapShift = 24,
426 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
427
Francois Romieu5d06a992006-02-23 00:47:58 +0100428 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200429 LEDS1 = (1 << 7),
430 LEDS0 = (1 << 6),
Francois Romieufbac58f2007-10-04 22:51:38 +0200431 MSIEnable = (1 << 5), /* Enable Message Signaled Interrupt */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200432 Speed_down = (1 << 4),
433 MEMMAP = (1 << 3),
434 IOMAP = (1 << 2),
435 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100436 PMEnable = (1 << 0), /* Power Management Enable */
437
Francois Romieu6dccd162007-02-13 23:38:05 +0100438 /* Config2 register p. 25 */
439 PCI_Clock_66MHz = 0x01,
440 PCI_Clock_33MHz = 0x00,
441
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100442 /* Config3 register p.25 */
443 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
444 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200445 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100446
Francois Romieu5d06a992006-02-23 00:47:58 +0100447 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100448 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
449 MWF = (1 << 5), /* Accept Multicast wakeup frame */
450 UWF = (1 << 4), /* Accept Unicast wakeup frame */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200451 Spi_en = (1 << 3),
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100452 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100453 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 /* TBICSR p.28 */
456 TBIReset = 0x80000000,
457 TBILoopback = 0x40000000,
458 TBINwEnable = 0x20000000,
459 TBINwRestart = 0x10000000,
460 TBILinkOk = 0x02000000,
461 TBINwComplete = 0x01000000,
462
463 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200464 EnableBist = (1 << 15), // 8168 8101
465 Mac_dbgo_oe = (1 << 14), // 8168 8101
466 Normal_mode = (1 << 13), // unused
467 Force_half_dup = (1 << 12), // 8168 8101
468 Force_rxflow_en = (1 << 11), // 8168 8101
469 Force_txflow_en = (1 << 10), // 8168 8101
470 Cxpl_dbg_sel = (1 << 9), // 8168 8101
471 ASF = (1 << 8), // 8168 8101
472 PktCntrDisable = (1 << 7), // 8168 8101
473 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 RxVlan = (1 << 6),
475 RxChkSum = (1 << 5),
476 PCIDAC = (1 << 4),
477 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100478 INTT_0 = 0x0000, // 8168
479 INTT_1 = 0x0001, // 8168
480 INTT_2 = 0x0002, // 8168
481 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100484 TBI_Enable = 0x80,
485 TxFlowCtrl = 0x40,
486 RxFlowCtrl = 0x20,
487 _1000bpsF = 0x10,
488 _100bps = 0x08,
489 _10bps = 0x04,
490 LinkStatus = 0x02,
491 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100494 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200495
496 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100497 CounterDump = 0x8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498};
499
Francois Romieu2b7b4312011-04-18 22:53:24 -0700500enum rtl_desc_bit {
501 /* First doubleword. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
503 RingEnd = (1 << 30), /* End of descriptor ring */
504 FirstFrag = (1 << 29), /* First segment of a packet */
505 LastFrag = (1 << 28), /* Final segment of a packet */
Francois Romieu2b7b4312011-04-18 22:53:24 -0700506};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Francois Romieu2b7b4312011-04-18 22:53:24 -0700508/* Generic case. */
509enum rtl_tx_desc_bit {
510 /* First doubleword. */
511 TD_LSO = (1 << 27), /* Large Send Offload */
512#define TD_MSS_MAX 0x07ffu /* MSS value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Francois Romieu2b7b4312011-04-18 22:53:24 -0700514 /* Second doubleword. */
515 TxVlanTag = (1 << 17), /* Add VLAN tag */
516};
517
518/* 8169, 8168b and 810x except 8102e. */
519enum rtl_tx_desc_bit_0 {
520 /* First doubleword. */
521#define TD0_MSS_SHIFT 16 /* MSS position (11 bits) */
522 TD0_TCP_CS = (1 << 16), /* Calculate TCP/IP checksum */
523 TD0_UDP_CS = (1 << 17), /* Calculate UDP/IP checksum */
524 TD0_IP_CS = (1 << 18), /* Calculate IP checksum */
525};
526
527/* 8102e, 8168c and beyond. */
528enum rtl_tx_desc_bit_1 {
529 /* Second doubleword. */
530#define TD1_MSS_SHIFT 18 /* MSS position (11 bits) */
531 TD1_IP_CS = (1 << 29), /* Calculate IP checksum */
532 TD1_TCP_CS = (1 << 30), /* Calculate TCP/IP checksum */
533 TD1_UDP_CS = (1 << 31), /* Calculate UDP/IP checksum */
534};
535
536static const struct rtl_tx_desc_info {
537 struct {
538 u32 udp;
539 u32 tcp;
540 } checksum;
541 u16 mss_shift;
542 u16 opts_offset;
543} tx_desc_info [] = {
544 [RTL_TD_0] = {
545 .checksum = {
546 .udp = TD0_IP_CS | TD0_UDP_CS,
547 .tcp = TD0_IP_CS | TD0_TCP_CS
548 },
549 .mss_shift = TD0_MSS_SHIFT,
550 .opts_offset = 0
551 },
552 [RTL_TD_1] = {
553 .checksum = {
554 .udp = TD1_IP_CS | TD1_UDP_CS,
555 .tcp = TD1_IP_CS | TD1_TCP_CS
556 },
557 .mss_shift = TD1_MSS_SHIFT,
558 .opts_offset = 1
559 }
560};
561
562enum rtl_rx_desc_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 /* Rx private */
564 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
565 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
566
567#define RxProtoUDP (PID1)
568#define RxProtoTCP (PID0)
569#define RxProtoIP (PID1 | PID0)
570#define RxProtoMask RxProtoIP
571
572 IPFail = (1 << 16), /* IP checksum failed */
573 UDPFail = (1 << 15), /* UDP/IP checksum failed */
574 TCPFail = (1 << 14), /* TCP/IP checksum failed */
575 RxVlanTag = (1 << 16), /* VLAN tag available */
576};
577
578#define RsvdMask 0x3fffc000
579
580struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200581 __le32 opts1;
582 __le32 opts2;
583 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584};
585
586struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200587 __le32 opts1;
588 __le32 opts2;
589 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590};
591
592struct ring_info {
593 struct sk_buff *skb;
594 u32 len;
595 u8 __pad[sizeof(void *) - sizeof(u32)];
596};
597
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200598enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200599 RTL_FEATURE_WOL = (1 << 0),
600 RTL_FEATURE_MSI = (1 << 1),
601 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200602};
603
Ivan Vecera355423d2009-02-06 21:49:57 -0800604struct rtl8169_counters {
605 __le64 tx_packets;
606 __le64 rx_packets;
607 __le64 tx_errors;
608 __le32 rx_errors;
609 __le16 rx_missed;
610 __le16 align_errors;
611 __le32 tx_one_collision;
612 __le32 tx_multi_collision;
613 __le64 rx_unicast;
614 __le64 rx_broadcast;
615 __le32 rx_multicast;
616 __le16 tx_aborted;
617 __le16 tx_underun;
618};
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620struct rtl8169_private {
621 void __iomem *mmio_addr; /* memory map physical address */
Francois Romieucecb5fd2011-04-01 10:21:07 +0200622 struct pci_dev *pci_dev;
David Howellsc4028952006-11-22 14:57:56 +0000623 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700624 struct napi_struct napi;
Francois Romieucecb5fd2011-04-01 10:21:07 +0200625 spinlock_t lock;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200626 u32 msg_enable;
Francois Romieu2b7b4312011-04-18 22:53:24 -0700627 u16 txd_version;
628 u16 mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
630 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
631 u32 dirty_rx;
632 u32 dirty_tx;
633 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
634 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
635 dma_addr_t TxPhyAddr;
636 dma_addr_t RxPhyAddr;
Eric Dumazet6f0333b2010-10-11 11:17:47 +0000637 void *Rx_databuff[NUM_RX_DESC]; /* Rx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 struct timer_list timer;
640 u16 cp_cmd;
Francois Romieu0e485152007-02-20 00:00:26 +0100641 u16 intr_event;
642 u16 napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 u16 intr_mask;
françois romieuc0e45c12011-01-03 15:08:04 +0000644
645 struct mdio_ops {
646 void (*write)(void __iomem *, int, int);
647 int (*read)(void __iomem *, int);
648 } mdio_ops;
649
françois romieu065c27c2011-01-03 15:08:12 +0000650 struct pll_power_ops {
651 void (*down)(struct rtl8169_private *);
652 void (*up)(struct rtl8169_private *);
653 } pll_power_ops;
654
Oliver Neukum54405cd2011-01-06 21:55:13 +0100655 int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
Francois Romieuccdffb92008-07-26 14:26:06 +0200656 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
françois romieu4da19632011-01-03 15:07:55 +0000657 void (*phy_reset_enable)(struct rtl8169_private *tp);
Francois Romieu07ce4062007-02-23 23:36:39 +0100658 void (*hw_start)(struct net_device *);
françois romieu4da19632011-01-03 15:07:55 +0000659 unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 unsigned int (*link_ok)(void __iomem *);
Francois Romieu8b4ab282008-11-19 22:05:25 -0800661 int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
Francois Romieu9c14cea2008-07-05 00:21:15 +0200662 int pcie_cap;
David Howellsc4028952006-11-22 14:57:56 +0000663 struct delayed_work task;
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200664 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200665
666 struct mii_if_info mii;
Ivan Vecera355423d2009-02-06 21:49:57 -0800667 struct rtl8169_counters counters;
Rafael J. Wysockie1759442010-03-14 14:33:51 +0000668 u32 saved_wolopts;
françois romieuf1e02ed2011-01-13 13:07:53 +0000669
Francois Romieub6ffd972011-06-17 17:00:05 +0200670 struct rtl_fw {
671 const struct firmware *fw;
Francois Romieu1c361ef2011-06-17 17:16:24 +0200672
673#define RTL_VER_SIZE 32
674
675 char version[RTL_VER_SIZE];
676
677 struct rtl_fw_phy_action {
678 __le32 *code;
679 size_t size;
680 } phy_action;
Francois Romieub6ffd972011-06-17 17:00:05 +0200681 } *rtl_fw;
François Romieu953a12c2011-04-24 17:38:48 +0200682#define RTL_FIRMWARE_UNKNOWN ERR_PTR(-EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683};
684
Ralf Baechle979b6c12005-06-13 14:30:40 -0700685MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687module_param(use_dac, int, 0);
David S. Miller4300e8c2010-03-26 10:23:30 -0700688MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200689module_param_named(debug, debug.msg_enable, int, 0);
690MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691MODULE_LICENSE("GPL");
692MODULE_VERSION(RTL8169_VERSION);
françois romieubca03d52011-01-03 15:07:31 +0000693MODULE_FIRMWARE(FIRMWARE_8168D_1);
694MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang01dc7fe2011-03-21 01:50:28 +0000695MODULE_FIRMWARE(FIRMWARE_8168E_1);
696MODULE_FIRMWARE(FIRMWARE_8168E_2);
Hayes Wang5a5e4442011-02-22 17:26:21 +0800697MODULE_FIRMWARE(FIRMWARE_8105E_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699static int rtl8169_open(struct net_device *dev);
Stephen Hemminger613573252009-08-31 19:50:58 +0000700static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
701 struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100702static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703static int rtl8169_init_ring(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100704static void rtl_hw_start(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705static int rtl8169_close(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100706static void rtl_set_rx_mode(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707static void rtl8169_tx_timeout(struct net_device *dev);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200708static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709static int rtl8169_rx_interrupt(struct net_device *, struct rtl8169_private *,
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700710 void __iomem *, u32 budget);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200711static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712static void rtl8169_down(struct net_device *dev);
Francois Romieu99f252b2007-04-02 22:59:59 +0200713static void rtl8169_rx_clear(struct rtl8169_private *tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700714static int rtl8169_poll(struct napi_struct *napi, int budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716static const unsigned int rtl8169_rx_config =
Francois Romieu5b0384f2006-08-16 16:00:01 +0200717 (RX_FIFO_THRESH << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
françois romieub646d902011-01-03 15:08:21 +0000719static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
720{
721 void __iomem *ioaddr = tp->mmio_addr;
722 int i;
723
724 RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
725 for (i = 0; i < 20; i++) {
726 udelay(100);
727 if (RTL_R32(OCPAR) & OCPAR_FLAG)
728 break;
729 }
730 return RTL_R32(OCPDR);
731}
732
733static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
734{
735 void __iomem *ioaddr = tp->mmio_addr;
736 int i;
737
738 RTL_W32(OCPDR, data);
739 RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
740 for (i = 0; i < 20; i++) {
741 udelay(100);
742 if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
743 break;
744 }
745}
746
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800747static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieub646d902011-01-03 15:08:21 +0000748{
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800749 void __iomem *ioaddr = tp->mmio_addr;
françois romieub646d902011-01-03 15:08:21 +0000750 int i;
751
752 RTL_W8(ERIDR, cmd);
753 RTL_W32(ERIAR, 0x800010e8);
754 msleep(2);
755 for (i = 0; i < 5; i++) {
756 udelay(100);
757 if (!(RTL_R32(ERIDR) & ERIAR_FLAG))
758 break;
759 }
760
Hayes Wangfac5b3c2011-02-22 17:26:20 +0800761 ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieub646d902011-01-03 15:08:21 +0000762}
763
764#define OOB_CMD_RESET 0x00
765#define OOB_CMD_DRIVER_START 0x05
766#define OOB_CMD_DRIVER_STOP 0x06
767
Francois Romieucecb5fd2011-04-01 10:21:07 +0200768static u16 rtl8168_get_ocp_reg(struct rtl8169_private *tp)
769{
770 return (tp->mac_version == RTL_GIGA_MAC_VER_31) ? 0xb8 : 0x10;
771}
772
françois romieub646d902011-01-03 15:08:21 +0000773static void rtl8168_driver_start(struct rtl8169_private *tp)
774{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200775 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000776 int i;
777
778 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);
779
Francois Romieucecb5fd2011-04-01 10:21:07 +0200780 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000781
françois romieub646d902011-01-03 15:08:21 +0000782 for (i = 0; i < 10; i++) {
783 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000784 if (ocp_read(tp, 0x0f, reg) & 0x00000800)
françois romieub646d902011-01-03 15:08:21 +0000785 break;
786 }
787}
788
789static void rtl8168_driver_stop(struct rtl8169_private *tp)
790{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200791 u16 reg;
françois romieub646d902011-01-03 15:08:21 +0000792 int i;
793
794 rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);
795
Francois Romieucecb5fd2011-04-01 10:21:07 +0200796 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000797
françois romieub646d902011-01-03 15:08:21 +0000798 for (i = 0; i < 10; i++) {
799 msleep(10);
hayeswang4804b3b2011-03-21 01:50:29 +0000800 if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
françois romieub646d902011-01-03 15:08:21 +0000801 break;
802 }
803}
804
hayeswang4804b3b2011-03-21 01:50:29 +0000805static int r8168dp_check_dash(struct rtl8169_private *tp)
806{
Francois Romieucecb5fd2011-04-01 10:21:07 +0200807 u16 reg = rtl8168_get_ocp_reg(tp);
hayeswang4804b3b2011-03-21 01:50:29 +0000808
Francois Romieucecb5fd2011-04-01 10:21:07 +0200809 return (ocp_read(tp, 0x0f, reg) & 0x00008000) ? 1 : 0;
hayeswang4804b3b2011-03-21 01:50:29 +0000810}
françois romieub646d902011-01-03 15:08:21 +0000811
françois romieu4da19632011-01-03 15:07:55 +0000812static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
814 int i;
815
Francois Romieua6baf3a2007-11-08 23:23:21 +0100816 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Francois Romieu23714082006-01-29 00:49:09 +0100818 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100819 /*
820 * Check if the RTL8169 has completed writing to the specified
821 * MII register.
822 */
Francois Romieu5b0384f2006-08-16 16:00:01 +0200823 if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 break;
Francois Romieu23714082006-01-29 00:49:09 +0100825 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 }
Timo Teräs024a07b2010-06-06 15:38:47 -0700827 /*
Timo Teräs81a95f02010-06-09 17:31:48 -0700828 * According to hardware specs a 20us delay is required after write
829 * complete indication, but before sending next command.
Timo Teräs024a07b2010-06-06 15:38:47 -0700830 */
Timo Teräs81a95f02010-06-09 17:31:48 -0700831 udelay(20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
françois romieu4da19632011-01-03 15:07:55 +0000834static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
836 int i, value = -1;
837
Francois Romieua6baf3a2007-11-08 23:23:21 +0100838 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Francois Romieu23714082006-01-29 00:49:09 +0100840 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100841 /*
842 * Check if the RTL8169 has completed retrieving data from
843 * the specified MII register.
844 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (RTL_R32(PHYAR) & 0x80000000) {
Francois Romieua6baf3a2007-11-08 23:23:21 +0100846 value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 break;
848 }
Francois Romieu23714082006-01-29 00:49:09 +0100849 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 }
Timo Teräs81a95f02010-06-09 17:31:48 -0700851 /*
852 * According to hardware specs a 20us delay is required after read
853 * complete indication, but before sending next command.
854 */
855 udelay(20);
856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 return value;
858}
859
françois romieuc0e45c12011-01-03 15:08:04 +0000860static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
861{
862 int i;
863
864 RTL_W32(OCPDR, data |
865 ((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
866 RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
867 RTL_W32(EPHY_RXER_NUM, 0);
868
869 for (i = 0; i < 100; i++) {
870 mdelay(1);
871 if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
872 break;
873 }
874}
875
876static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
877{
878 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
879 (value & OCPDR_DATA_MASK));
880}
881
882static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
883{
884 int i;
885
886 r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);
887
888 mdelay(1);
889 RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
890 RTL_W32(EPHY_RXER_NUM, 0);
891
892 for (i = 0; i < 100; i++) {
893 mdelay(1);
894 if (RTL_R32(OCPAR) & OCPAR_FLAG)
895 break;
896 }
897
898 return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
899}
900
françois romieue6de30d2011-01-03 15:08:37 +0000901#define R8168DP_1_MDIO_ACCESS_BIT 0x00020000
902
903static void r8168dp_2_mdio_start(void __iomem *ioaddr)
904{
905 RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
906}
907
908static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
909{
910 RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
911}
912
913static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
914{
915 r8168dp_2_mdio_start(ioaddr);
916
917 r8169_mdio_write(ioaddr, reg_addr, value);
918
919 r8168dp_2_mdio_stop(ioaddr);
920}
921
922static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
923{
924 int value;
925
926 r8168dp_2_mdio_start(ioaddr);
927
928 value = r8169_mdio_read(ioaddr, reg_addr);
929
930 r8168dp_2_mdio_stop(ioaddr);
931
932 return value;
933}
934
françois romieu4da19632011-01-03 15:07:55 +0000935static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
Francois Romieudacf8152008-08-02 20:44:13 +0200936{
françois romieuc0e45c12011-01-03 15:08:04 +0000937 tp->mdio_ops.write(tp->mmio_addr, location, val);
Francois Romieudacf8152008-08-02 20:44:13 +0200938}
939
françois romieu4da19632011-01-03 15:07:55 +0000940static int rtl_readphy(struct rtl8169_private *tp, int location)
941{
françois romieuc0e45c12011-01-03 15:08:04 +0000942 return tp->mdio_ops.read(tp->mmio_addr, location);
françois romieu4da19632011-01-03 15:07:55 +0000943}
944
945static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
946{
947 rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
948}
949
950static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
françois romieudaf9df62009-10-07 12:44:20 +0000951{
952 int val;
953
françois romieu4da19632011-01-03 15:07:55 +0000954 val = rtl_readphy(tp, reg_addr);
955 rtl_writephy(tp, reg_addr, (val | p) & ~m);
françois romieudaf9df62009-10-07 12:44:20 +0000956}
957
Francois Romieuccdffb92008-07-26 14:26:06 +0200958static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
959 int val)
960{
961 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +0200962
françois romieu4da19632011-01-03 15:07:55 +0000963 rtl_writephy(tp, location, val);
Francois Romieuccdffb92008-07-26 14:26:06 +0200964}
965
966static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
967{
968 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieuccdffb92008-07-26 14:26:06 +0200969
françois romieu4da19632011-01-03 15:07:55 +0000970 return rtl_readphy(tp, location);
Francois Romieuccdffb92008-07-26 14:26:06 +0200971}
972
Francois Romieudacf8152008-08-02 20:44:13 +0200973static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
974{
975 unsigned int i;
976
977 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
978 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
979
980 for (i = 0; i < 100; i++) {
981 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
982 break;
983 udelay(10);
984 }
985}
986
987static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
988{
989 u16 value = 0xffff;
990 unsigned int i;
991
992 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
993
994 for (i = 0; i < 100; i++) {
995 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
996 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
997 break;
998 }
999 udelay(10);
1000 }
1001
1002 return value;
1003}
1004
1005static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
1006{
1007 unsigned int i;
1008
1009 RTL_W32(CSIDR, value);
1010 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
1011 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1012
1013 for (i = 0; i < 100; i++) {
1014 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
1015 break;
1016 udelay(10);
1017 }
1018}
1019
1020static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
1021{
1022 u32 value = ~0x00;
1023 unsigned int i;
1024
1025 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
1026 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
1027
1028 for (i = 0; i < 100; i++) {
1029 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
1030 value = RTL_R32(CSIDR);
1031 break;
1032 }
1033 udelay(10);
1034 }
1035
1036 return value;
1037}
1038
françois romieudaf9df62009-10-07 12:44:20 +00001039static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
1040{
1041 u8 value = 0xff;
1042 unsigned int i;
1043
1044 RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);
1045
1046 for (i = 0; i < 300; i++) {
1047 if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
1048 value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
1049 break;
1050 }
1051 udelay(100);
1052 }
1053
1054 return value;
1055}
1056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057static void rtl8169_irq_mask_and_ack(void __iomem *ioaddr)
1058{
1059 RTL_W16(IntrMask, 0x0000);
1060
1061 RTL_W16(IntrStatus, 0xffff);
1062}
1063
1064static void rtl8169_asic_down(void __iomem *ioaddr)
1065{
1066 RTL_W8(ChipCmd, 0x00);
1067 rtl8169_irq_mask_and_ack(ioaddr);
1068 RTL_R16(CPlusCmd);
1069}
1070
françois romieu4da19632011-01-03 15:07:55 +00001071static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
françois romieu4da19632011-01-03 15:07:55 +00001073 void __iomem *ioaddr = tp->mmio_addr;
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 return RTL_R32(TBICSR) & TBIReset;
1076}
1077
françois romieu4da19632011-01-03 15:07:55 +00001078static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
françois romieu4da19632011-01-03 15:07:55 +00001080 return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081}
1082
1083static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
1084{
1085 return RTL_R32(TBICSR) & TBILinkOk;
1086}
1087
1088static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
1089{
1090 return RTL_R8(PHYstatus) & LinkStatus;
1091}
1092
françois romieu4da19632011-01-03 15:07:55 +00001093static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
françois romieu4da19632011-01-03 15:07:55 +00001095 void __iomem *ioaddr = tp->mmio_addr;
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
1098}
1099
françois romieu4da19632011-01-03 15:07:55 +00001100static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
1102 unsigned int val;
1103
françois romieu4da19632011-01-03 15:07:55 +00001104 val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
1105 rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106}
1107
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001108static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001109 struct rtl8169_private *tp,
1110 void __iomem *ioaddr, bool pm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111{
1112 unsigned long flags;
1113
1114 spin_lock_irqsave(&tp->lock, flags);
1115 if (tp->link_ok(ioaddr)) {
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001116 /* This is to cancel a scheduled suspend if there's one. */
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001117 if (pm)
1118 pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 netif_carrier_on(dev);
Francois Romieu1519e572011-02-03 12:02:36 +01001120 if (net_ratelimit())
1121 netif_info(tp, ifup, dev, "link up\n");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001122 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 netif_carrier_off(dev);
Joe Perchesbf82c182010-02-09 11:49:50 +00001124 netif_info(tp, ifdown, dev, "link down\n");
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001125 if (pm)
1126 pm_schedule_suspend(&tp->pci_dev->dev, 100);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 spin_unlock_irqrestore(&tp->lock, flags);
1129}
1130
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00001131static void rtl8169_check_link_status(struct net_device *dev,
1132 struct rtl8169_private *tp,
1133 void __iomem *ioaddr)
1134{
1135 __rtl8169_check_link_status(dev, tp, ioaddr, false);
1136}
1137
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001138#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
1139
1140static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
1141{
1142 void __iomem *ioaddr = tp->mmio_addr;
1143 u8 options;
1144 u32 wolopts = 0;
1145
1146 options = RTL_R8(Config1);
1147 if (!(options & PMEnable))
1148 return 0;
1149
1150 options = RTL_R8(Config3);
1151 if (options & LinkUp)
1152 wolopts |= WAKE_PHY;
1153 if (options & MagicPacket)
1154 wolopts |= WAKE_MAGIC;
1155
1156 options = RTL_R8(Config5);
1157 if (options & UWF)
1158 wolopts |= WAKE_UCAST;
1159 if (options & BWF)
1160 wolopts |= WAKE_BCAST;
1161 if (options & MWF)
1162 wolopts |= WAKE_MCAST;
1163
1164 return wolopts;
1165}
1166
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001167static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1168{
1169 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001170
1171 spin_lock_irq(&tp->lock);
1172
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001173 wol->supported = WAKE_ANY;
1174 wol->wolopts = __rtl8169_get_wol(tp);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001175
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001176 spin_unlock_irq(&tp->lock);
1177}
1178
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001179static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001180{
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001181 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001182 unsigned int i;
Alexey Dobriyan350f7592009-11-25 15:54:21 -08001183 static const struct {
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001184 u32 opt;
1185 u16 reg;
1186 u8 mask;
1187 } cfg[] = {
1188 { WAKE_ANY, Config1, PMEnable },
1189 { WAKE_PHY, Config3, LinkUp },
1190 { WAKE_MAGIC, Config3, MagicPacket },
1191 { WAKE_UCAST, Config5, UWF },
1192 { WAKE_BCAST, Config5, BWF },
1193 { WAKE_MCAST, Config5, MWF },
1194 { WAKE_ANY, Config5, LanWake }
1195 };
1196
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001197 RTL_W8(Cfg9346, Cfg9346_Unlock);
1198
1199 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
1200 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001201 if (wolopts & cfg[i].opt)
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001202 options |= cfg[i].mask;
1203 RTL_W8(cfg[i].reg, options);
1204 }
1205
1206 RTL_W8(Cfg9346, Cfg9346_Lock);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001207}
1208
1209static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1210{
1211 struct rtl8169_private *tp = netdev_priv(dev);
1212
1213 spin_lock_irq(&tp->lock);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001214
Francois Romieuf23e7fd2007-10-04 22:36:14 +02001215 if (wol->wolopts)
1216 tp->features |= RTL_FEATURE_WOL;
1217 else
1218 tp->features &= ~RTL_FEATURE_WOL;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00001219 __rtl8169_set_wol(tp, wol->wolopts);
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001220 spin_unlock_irq(&tp->lock);
1221
françois romieuea809072010-11-08 13:23:58 +00001222 device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);
1223
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001224 return 0;
1225}
1226
Francois Romieu31bd2042011-04-26 18:58:59 +02001227static const char *rtl_lookup_firmware_name(struct rtl8169_private *tp)
1228{
Francois Romieu85bffe62011-04-27 08:22:39 +02001229 return rtl_chip_infos[tp->mac_version].fw_name;
Francois Romieu31bd2042011-04-26 18:58:59 +02001230}
1231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232static void rtl8169_get_drvinfo(struct net_device *dev,
1233 struct ethtool_drvinfo *info)
1234{
1235 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieub6ffd972011-06-17 17:00:05 +02001236 struct rtl_fw *rtl_fw = tp->rtl_fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 strcpy(info->driver, MODULENAME);
1239 strcpy(info->version, RTL8169_VERSION);
1240 strcpy(info->bus_info, pci_name(tp->pci_dev));
Francois Romieu1c361ef2011-06-17 17:16:24 +02001241 BUILD_BUG_ON(sizeof(info->fw_version) < sizeof(rtl_fw->version));
1242 strcpy(info->fw_version, IS_ERR_OR_NULL(rtl_fw) ? "N/A" :
1243 rtl_fw->version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245
1246static int rtl8169_get_regs_len(struct net_device *dev)
1247{
1248 return R8169_REGS_SIZE;
1249}
1250
1251static int rtl8169_set_speed_tbi(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001252 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
1254 struct rtl8169_private *tp = netdev_priv(dev);
1255 void __iomem *ioaddr = tp->mmio_addr;
1256 int ret = 0;
1257 u32 reg;
1258
1259 reg = RTL_R32(TBICSR);
1260 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
1261 (duplex == DUPLEX_FULL)) {
1262 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
1263 } else if (autoneg == AUTONEG_ENABLE)
1264 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
1265 else {
Joe Perchesbf82c182010-02-09 11:49:50 +00001266 netif_warn(tp, link, dev,
1267 "incorrect speed setting refused in TBI mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 ret = -EOPNOTSUPP;
1269 }
1270
1271 return ret;
1272}
1273
1274static int rtl8169_set_speed_xmii(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001275 u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
1277 struct rtl8169_private *tp = netdev_priv(dev);
françois romieu3577aa12009-05-19 10:46:48 +00001278 int giga_ctrl, bmcr;
Oliver Neukum54405cd2011-01-06 21:55:13 +01001279 int rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Hayes Wang716b50a2011-02-22 17:26:18 +08001281 rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283 if (autoneg == AUTONEG_ENABLE) {
françois romieu3577aa12009-05-19 10:46:48 +00001284 int auto_nego;
1285
françois romieu4da19632011-01-03 15:07:55 +00001286 auto_nego = rtl_readphy(tp, MII_ADVERTISE);
Oliver Neukum54405cd2011-01-06 21:55:13 +01001287 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1288 ADVERTISE_100HALF | ADVERTISE_100FULL);
1289
1290 if (adv & ADVERTISED_10baseT_Half)
1291 auto_nego |= ADVERTISE_10HALF;
1292 if (adv & ADVERTISED_10baseT_Full)
1293 auto_nego |= ADVERTISE_10FULL;
1294 if (adv & ADVERTISED_100baseT_Half)
1295 auto_nego |= ADVERTISE_100HALF;
1296 if (adv & ADVERTISED_100baseT_Full)
1297 auto_nego |= ADVERTISE_100FULL;
1298
françois romieu3577aa12009-05-19 10:46:48 +00001299 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1300
françois romieu4da19632011-01-03 15:07:55 +00001301 giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
françois romieu3577aa12009-05-19 10:46:48 +00001302 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1303
1304 /* The 8100e/8101e/8102e do Fast Ethernet only. */
Francois Romieu826e6cb2011-03-11 20:30:24 +01001305 if (tp->mii.supports_gmii) {
Oliver Neukum54405cd2011-01-06 21:55:13 +01001306 if (adv & ADVERTISED_1000baseT_Half)
1307 giga_ctrl |= ADVERTISE_1000HALF;
1308 if (adv & ADVERTISED_1000baseT_Full)
1309 giga_ctrl |= ADVERTISE_1000FULL;
1310 } else if (adv & (ADVERTISED_1000baseT_Half |
1311 ADVERTISED_1000baseT_Full)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00001312 netif_info(tp, link, dev,
1313 "PHY does not support 1000Mbps\n");
Oliver Neukum54405cd2011-01-06 21:55:13 +01001314 goto out;
Francois Romieubcf0bf92006-07-26 23:14:13 +02001315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
françois romieu3577aa12009-05-19 10:46:48 +00001317 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
Francois Romieu623a1592006-05-14 12:42:14 +02001318
françois romieu4da19632011-01-03 15:07:55 +00001319 rtl_writephy(tp, MII_ADVERTISE, auto_nego);
1320 rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
françois romieu3577aa12009-05-19 10:46:48 +00001321 } else {
1322 giga_ctrl = 0;
1323
1324 if (speed == SPEED_10)
1325 bmcr = 0;
1326 else if (speed == SPEED_100)
1327 bmcr = BMCR_SPEED100;
1328 else
Oliver Neukum54405cd2011-01-06 21:55:13 +01001329 goto out;
françois romieu3577aa12009-05-19 10:46:48 +00001330
1331 if (duplex == DUPLEX_FULL)
1332 bmcr |= BMCR_FULLDPLX;
Roger So2584fbc2007-07-31 23:52:42 +02001333 }
1334
françois romieu4da19632011-01-03 15:07:55 +00001335 rtl_writephy(tp, MII_BMCR, bmcr);
françois romieu3577aa12009-05-19 10:46:48 +00001336
Francois Romieucecb5fd2011-04-01 10:21:07 +02001337 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
1338 tp->mac_version == RTL_GIGA_MAC_VER_03) {
françois romieu3577aa12009-05-19 10:46:48 +00001339 if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
françois romieu4da19632011-01-03 15:07:55 +00001340 rtl_writephy(tp, 0x17, 0x2138);
1341 rtl_writephy(tp, 0x0e, 0x0260);
françois romieu3577aa12009-05-19 10:46:48 +00001342 } else {
françois romieu4da19632011-01-03 15:07:55 +00001343 rtl_writephy(tp, 0x17, 0x2108);
1344 rtl_writephy(tp, 0x0e, 0x0000);
françois romieu3577aa12009-05-19 10:46:48 +00001345 }
1346 }
1347
Oliver Neukum54405cd2011-01-06 21:55:13 +01001348 rc = 0;
1349out:
1350 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351}
1352
1353static int rtl8169_set_speed(struct net_device *dev,
Oliver Neukum54405cd2011-01-06 21:55:13 +01001354 u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
1356 struct rtl8169_private *tp = netdev_priv(dev);
1357 int ret;
1358
Oliver Neukum54405cd2011-01-06 21:55:13 +01001359 ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Francois Romieu4876cc12011-03-11 21:07:11 +01001360 if (ret < 0)
1361 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Francois Romieu4876cc12011-03-11 21:07:11 +01001363 if (netif_running(dev) && (autoneg == AUTONEG_ENABLE) &&
1364 (advertising & ADVERTISED_1000baseT_Full)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
Francois Romieu4876cc12011-03-11 21:07:11 +01001366 }
1367out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 return ret;
1369}
1370
1371static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1372{
1373 struct rtl8169_private *tp = netdev_priv(dev);
1374 unsigned long flags;
1375 int ret;
1376
Francois Romieu4876cc12011-03-11 21:07:11 +01001377 del_timer_sync(&tp->timer);
1378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 spin_lock_irqsave(&tp->lock, flags);
Francois Romieucecb5fd2011-04-01 10:21:07 +02001380 ret = rtl8169_set_speed(dev, cmd->autoneg, ethtool_cmd_speed(cmd),
David Decotigny25db0332011-04-27 18:32:39 +00001381 cmd->duplex, cmd->advertising);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 return ret;
1385}
1386
Michał Mirosław350fb322011-04-08 06:35:56 +00001387static u32 rtl8169_fix_features(struct net_device *dev, u32 features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388{
Francois Romieu2b7b4312011-04-18 22:53:24 -07001389 if (dev->mtu > TD_MSS_MAX)
Michał Mirosław350fb322011-04-08 06:35:56 +00001390 features &= ~NETIF_F_ALL_TSO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Michał Mirosław350fb322011-04-08 06:35:56 +00001392 return features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393}
1394
Michał Mirosław350fb322011-04-08 06:35:56 +00001395static int rtl8169_set_features(struct net_device *dev, u32 features)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396{
1397 struct rtl8169_private *tp = netdev_priv(dev);
1398 void __iomem *ioaddr = tp->mmio_addr;
1399 unsigned long flags;
1400
1401 spin_lock_irqsave(&tp->lock, flags);
1402
Michał Mirosław350fb322011-04-08 06:35:56 +00001403 if (features & NETIF_F_RXCSUM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 tp->cp_cmd |= RxChkSum;
1405 else
1406 tp->cp_cmd &= ~RxChkSum;
1407
Michał Mirosław350fb322011-04-08 06:35:56 +00001408 if (dev->features & NETIF_F_HW_VLAN_RX)
1409 tp->cp_cmd |= RxVlan;
1410 else
1411 tp->cp_cmd &= ~RxVlan;
1412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 RTL_W16(CPlusCmd, tp->cp_cmd);
1414 RTL_R16(CPlusCmd);
1415
1416 spin_unlock_irqrestore(&tp->lock, flags);
1417
1418 return 0;
1419}
1420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
1422 struct sk_buff *skb)
1423{
Jesse Grosseab6d182010-10-20 13:56:03 +00001424 return (vlan_tx_tag_present(skb)) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
1426}
1427
Francois Romieu7a8fc772011-03-01 17:18:33 +01001428static void rtl8169_rx_vlan_tag(struct RxDesc *desc, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
1430 u32 opts2 = le32_to_cpu(desc->opts2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Francois Romieu7a8fc772011-03-01 17:18:33 +01001432 if (opts2 & RxVlanTag)
1433 __vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));
Eric Dumazet2edae082010-09-06 18:46:39 +00001434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 desc->opts2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436}
1437
Francois Romieuccdffb92008-07-26 14:26:06 +02001438static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
1440 struct rtl8169_private *tp = netdev_priv(dev);
1441 void __iomem *ioaddr = tp->mmio_addr;
1442 u32 status;
1443
1444 cmd->supported =
1445 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
1446 cmd->port = PORT_FIBRE;
1447 cmd->transceiver = XCVR_INTERNAL;
1448
1449 status = RTL_R32(TBICSR);
1450 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
1451 cmd->autoneg = !!(status & TBINwEnable);
1452
David Decotigny70739492011-04-27 18:32:40 +00001453 ethtool_cmd_speed_set(cmd, SPEED_1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001455
1456 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457}
1458
Francois Romieuccdffb92008-07-26 14:26:06 +02001459static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
1461 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
Francois Romieuccdffb92008-07-26 14:26:06 +02001463 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
1466static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1467{
1468 struct rtl8169_private *tp = netdev_priv(dev);
1469 unsigned long flags;
Francois Romieuccdffb92008-07-26 14:26:06 +02001470 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
1472 spin_lock_irqsave(&tp->lock, flags);
1473
Francois Romieuccdffb92008-07-26 14:26:06 +02001474 rc = tp->get_settings(dev, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
1476 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieuccdffb92008-07-26 14:26:06 +02001477 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478}
1479
1480static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1481 void *p)
1482{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001483 struct rtl8169_private *tp = netdev_priv(dev);
1484 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Francois Romieu5b0384f2006-08-16 16:00:01 +02001486 if (regs->len > R8169_REGS_SIZE)
1487 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Francois Romieu5b0384f2006-08-16 16:00:01 +02001489 spin_lock_irqsave(&tp->lock, flags);
1490 memcpy_fromio(p, tp->mmio_addr, regs->len);
1491 spin_unlock_irqrestore(&tp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492}
1493
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001494static u32 rtl8169_get_msglevel(struct net_device *dev)
1495{
1496 struct rtl8169_private *tp = netdev_priv(dev);
1497
1498 return tp->msg_enable;
1499}
1500
1501static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1502{
1503 struct rtl8169_private *tp = netdev_priv(dev);
1504
1505 tp->msg_enable = value;
1506}
1507
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001508static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1509 "tx_packets",
1510 "rx_packets",
1511 "tx_errors",
1512 "rx_errors",
1513 "rx_missed",
1514 "align_errors",
1515 "tx_single_collisions",
1516 "tx_multi_collisions",
1517 "unicast",
1518 "broadcast",
1519 "multicast",
1520 "tx_aborted",
1521 "tx_underrun",
1522};
1523
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001524static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001525{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001526 switch (sset) {
1527 case ETH_SS_STATS:
1528 return ARRAY_SIZE(rtl8169_gstrings);
1529 default:
1530 return -EOPNOTSUPP;
1531 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001532}
1533
Ivan Vecera355423d2009-02-06 21:49:57 -08001534static void rtl8169_update_counters(struct net_device *dev)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001535{
1536 struct rtl8169_private *tp = netdev_priv(dev);
1537 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieucecb5fd2011-04-01 10:21:07 +02001538 struct device *d = &tp->pci_dev->dev;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001539 struct rtl8169_counters *counters;
1540 dma_addr_t paddr;
1541 u32 cmd;
Ivan Vecera355423d2009-02-06 21:49:57 -08001542 int wait = 1000;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001543
Ivan Vecera355423d2009-02-06 21:49:57 -08001544 /*
1545 * Some chips are unable to dump tally counters when the receiver
1546 * is disabled.
1547 */
1548 if ((RTL_R8(ChipCmd) & CmdRxEnb) == 0)
1549 return;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001550
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001551 counters = dma_alloc_coherent(d, sizeof(*counters), &paddr, GFP_KERNEL);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001552 if (!counters)
1553 return;
1554
1555 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07001556 cmd = (u64)paddr & DMA_BIT_MASK(32);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001557 RTL_W32(CounterAddrLow, cmd);
1558 RTL_W32(CounterAddrLow, cmd | CounterDump);
1559
Ivan Vecera355423d2009-02-06 21:49:57 -08001560 while (wait--) {
1561 if ((RTL_R32(CounterAddrLow) & CounterDump) == 0) {
Ivan Vecera355423d2009-02-06 21:49:57 -08001562 memcpy(&tp->counters, counters, sizeof(*counters));
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001563 break;
Ivan Vecera355423d2009-02-06 21:49:57 -08001564 }
1565 udelay(10);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001566 }
1567
1568 RTL_W32(CounterAddrLow, 0);
1569 RTL_W32(CounterAddrHigh, 0);
1570
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00001571 dma_free_coherent(d, sizeof(*counters), counters, paddr);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001572}
1573
Ivan Vecera355423d2009-02-06 21:49:57 -08001574static void rtl8169_get_ethtool_stats(struct net_device *dev,
1575 struct ethtool_stats *stats, u64 *data)
1576{
1577 struct rtl8169_private *tp = netdev_priv(dev);
1578
1579 ASSERT_RTNL();
1580
1581 rtl8169_update_counters(dev);
1582
1583 data[0] = le64_to_cpu(tp->counters.tx_packets);
1584 data[1] = le64_to_cpu(tp->counters.rx_packets);
1585 data[2] = le64_to_cpu(tp->counters.tx_errors);
1586 data[3] = le32_to_cpu(tp->counters.rx_errors);
1587 data[4] = le16_to_cpu(tp->counters.rx_missed);
1588 data[5] = le16_to_cpu(tp->counters.align_errors);
1589 data[6] = le32_to_cpu(tp->counters.tx_one_collision);
1590 data[7] = le32_to_cpu(tp->counters.tx_multi_collision);
1591 data[8] = le64_to_cpu(tp->counters.rx_unicast);
1592 data[9] = le64_to_cpu(tp->counters.rx_broadcast);
1593 data[10] = le32_to_cpu(tp->counters.rx_multicast);
1594 data[11] = le16_to_cpu(tp->counters.tx_aborted);
1595 data[12] = le16_to_cpu(tp->counters.tx_underun);
1596}
1597
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001598static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1599{
1600 switch(stringset) {
1601 case ETH_SS_STATS:
1602 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1603 break;
1604 }
1605}
1606
Jeff Garzik7282d492006-09-13 14:30:00 -04001607static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 .get_drvinfo = rtl8169_get_drvinfo,
1609 .get_regs_len = rtl8169_get_regs_len,
1610 .get_link = ethtool_op_get_link,
1611 .get_settings = rtl8169_get_settings,
1612 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001613 .get_msglevel = rtl8169_get_msglevel,
1614 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001616 .get_wol = rtl8169_get_wol,
1617 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001618 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001619 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001620 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621};
1622
Francois Romieu07d3f512007-02-21 22:40:46 +01001623static void rtl8169_get_mac_version(struct rtl8169_private *tp,
Francois Romieu5d320a22011-05-08 17:47:36 +02001624 struct net_device *dev, u8 default_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625{
Francois Romieu5d320a22011-05-08 17:47:36 +02001626 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01001627 /*
1628 * The driver currently handles the 8168Bf and the 8168Be identically
1629 * but they can be identified more specifically through the test below
1630 * if needed:
1631 *
1632 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001633 *
1634 * Same thing for the 8101Eb and the 8101Ec:
1635 *
1636 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001637 */
Francois Romieu37441002011-06-17 22:58:54 +02001638 static const struct rtl_mac_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001640 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 int mac_version;
1642 } mac_info[] = {
hayeswang01dc7fe2011-03-21 01:50:28 +00001643 /* 8168E family. */
1644 { 0x7cf00000, 0x2c200000, RTL_GIGA_MAC_VER_33 },
1645 { 0x7cf00000, 0x2c100000, RTL_GIGA_MAC_VER_32 },
1646 { 0x7c800000, 0x2c000000, RTL_GIGA_MAC_VER_33 },
1647
Francois Romieu5b538df2008-07-20 16:22:45 +02001648 /* 8168D family. */
françois romieudaf9df62009-10-07 12:44:20 +00001649 { 0x7cf00000, 0x28300000, RTL_GIGA_MAC_VER_26 },
1650 { 0x7cf00000, 0x28100000, RTL_GIGA_MAC_VER_25 },
françois romieudaf9df62009-10-07 12:44:20 +00001651 { 0x7c800000, 0x28000000, RTL_GIGA_MAC_VER_26 },
Francois Romieu5b538df2008-07-20 16:22:45 +02001652
françois romieue6de30d2011-01-03 15:08:37 +00001653 /* 8168DP family. */
1654 { 0x7cf00000, 0x28800000, RTL_GIGA_MAC_VER_27 },
1655 { 0x7cf00000, 0x28a00000, RTL_GIGA_MAC_VER_28 },
hayeswang4804b3b2011-03-21 01:50:29 +00001656 { 0x7cf00000, 0x28b00000, RTL_GIGA_MAC_VER_31 },
françois romieue6de30d2011-01-03 15:08:37 +00001657
Francois Romieuef808d52008-06-29 13:10:54 +02001658 /* 8168C family. */
Francois Romieu17c99292010-07-11 17:10:09 -07001659 { 0x7cf00000, 0x3cb00000, RTL_GIGA_MAC_VER_24 },
Francois Romieuef3386f2008-06-29 12:24:30 +02001660 { 0x7cf00000, 0x3c900000, RTL_GIGA_MAC_VER_23 },
Francois Romieuef808d52008-06-29 13:10:54 +02001661 { 0x7cf00000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
Francois Romieu7f3e3d32008-07-20 18:53:20 +02001662 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_24 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001663 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1664 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
Francois Romieu197ff762008-06-28 13:16:02 +02001665 { 0x7cf00000, 0x3c300000, RTL_GIGA_MAC_VER_21 },
Francois Romieu6fb07052008-06-29 11:54:28 +02001666 { 0x7cf00000, 0x3c400000, RTL_GIGA_MAC_VER_22 },
Francois Romieuef808d52008-06-29 13:10:54 +02001667 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_22 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001668
1669 /* 8168B family. */
1670 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1671 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1672 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1673 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1674
1675 /* 8101 family. */
hayeswang36a0e6c2011-03-21 01:50:30 +00001676 { 0x7cf00000, 0x40b00000, RTL_GIGA_MAC_VER_30 },
Hayes Wang5a5e4442011-02-22 17:26:21 +08001677 { 0x7cf00000, 0x40a00000, RTL_GIGA_MAC_VER_30 },
1678 { 0x7cf00000, 0x40900000, RTL_GIGA_MAC_VER_29 },
1679 { 0x7c800000, 0x40800000, RTL_GIGA_MAC_VER_30 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001680 { 0x7cf00000, 0x34a00000, RTL_GIGA_MAC_VER_09 },
1681 { 0x7cf00000, 0x24a00000, RTL_GIGA_MAC_VER_09 },
1682 { 0x7cf00000, 0x34900000, RTL_GIGA_MAC_VER_08 },
1683 { 0x7cf00000, 0x24900000, RTL_GIGA_MAC_VER_08 },
1684 { 0x7cf00000, 0x34800000, RTL_GIGA_MAC_VER_07 },
1685 { 0x7cf00000, 0x24800000, RTL_GIGA_MAC_VER_07 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001686 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001687 { 0x7cf00000, 0x34300000, RTL_GIGA_MAC_VER_10 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001688 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
Francois Romieu2857ffb2008-08-02 21:08:49 +02001689 { 0x7c800000, 0x34800000, RTL_GIGA_MAC_VER_09 },
1690 { 0x7c800000, 0x24800000, RTL_GIGA_MAC_VER_09 },
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001691 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1692 /* FIXME: where did these entries come from ? -- FR */
1693 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1694 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1695
1696 /* 8110 family. */
1697 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1698 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1699 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1700 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1701 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1702 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1703
Jean Delvaref21b75e2009-05-26 20:54:48 -07001704 /* Catch-all */
1705 { 0x00000000, 0x00000000, RTL_GIGA_MAC_NONE }
Francois Romieu37441002011-06-17 22:58:54 +02001706 };
1707 const struct rtl_mac_info *p = mac_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 u32 reg;
1709
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001710 reg = RTL_R32(TxConfig);
1711 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 p++;
1713 tp->mac_version = p->mac_version;
Francois Romieu5d320a22011-05-08 17:47:36 +02001714
1715 if (tp->mac_version == RTL_GIGA_MAC_NONE) {
1716 netif_notice(tp, probe, dev,
1717 "unknown MAC, using family default\n");
1718 tp->mac_version = default_version;
1719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720}
1721
1722static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1723{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001724 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725}
1726
Francois Romieu867763c2007-08-17 18:21:58 +02001727struct phy_reg {
1728 u16 reg;
1729 u16 val;
1730};
1731
françois romieu4da19632011-01-03 15:07:55 +00001732static void rtl_writephy_batch(struct rtl8169_private *tp,
1733 const struct phy_reg *regs, int len)
Francois Romieu867763c2007-08-17 18:21:58 +02001734{
1735 while (len-- > 0) {
françois romieu4da19632011-01-03 15:07:55 +00001736 rtl_writephy(tp, regs->reg, regs->val);
Francois Romieu867763c2007-08-17 18:21:58 +02001737 regs++;
1738 }
1739}
1740
françois romieubca03d52011-01-03 15:07:31 +00001741#define PHY_READ 0x00000000
1742#define PHY_DATA_OR 0x10000000
1743#define PHY_DATA_AND 0x20000000
1744#define PHY_BJMPN 0x30000000
1745#define PHY_READ_EFUSE 0x40000000
1746#define PHY_READ_MAC_BYTE 0x50000000
1747#define PHY_WRITE_MAC_BYTE 0x60000000
1748#define PHY_CLEAR_READCOUNT 0x70000000
1749#define PHY_WRITE 0x80000000
1750#define PHY_READCOUNT_EQ_SKIP 0x90000000
1751#define PHY_COMP_EQ_SKIPN 0xa0000000
1752#define PHY_COMP_NEQ_SKIPN 0xb0000000
1753#define PHY_WRITE_PREVIOUS 0xc0000000
1754#define PHY_SKIPN 0xd0000000
1755#define PHY_DELAY_MS 0xe0000000
1756#define PHY_WRITE_ERI_WORD 0xf0000000
1757
Hayes Wang960aee62011-06-18 11:37:48 +02001758struct fw_info {
1759 u32 magic;
1760 char version[RTL_VER_SIZE];
1761 __le32 fw_start;
1762 __le32 fw_len;
1763 u8 chksum;
1764} __packed;
1765
Francois Romieu1c361ef2011-06-17 17:16:24 +02001766#define FW_OPCODE_SIZE sizeof(typeof(*((struct rtl_fw_phy_action *)0)->code))
1767
1768static bool rtl_fw_format_ok(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
françois romieubca03d52011-01-03 15:07:31 +00001769{
Francois Romieub6ffd972011-06-17 17:00:05 +02001770 const struct firmware *fw = rtl_fw->fw;
Hayes Wang960aee62011-06-18 11:37:48 +02001771 struct fw_info *fw_info = (struct fw_info *)fw->data;
Francois Romieu1c361ef2011-06-17 17:16:24 +02001772 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
1773 char *version = rtl_fw->version;
1774 bool rc = false;
françois romieubca03d52011-01-03 15:07:31 +00001775
Francois Romieu1c361ef2011-06-17 17:16:24 +02001776 if (fw->size < FW_OPCODE_SIZE)
1777 goto out;
Hayes Wang960aee62011-06-18 11:37:48 +02001778
1779 if (!fw_info->magic) {
1780 size_t i, size, start;
1781 u8 checksum = 0;
1782
1783 if (fw->size < sizeof(*fw_info))
1784 goto out;
1785
1786 for (i = 0; i < fw->size; i++)
1787 checksum += fw->data[i];
1788 if (checksum != 0)
1789 goto out;
1790
1791 start = le32_to_cpu(fw_info->fw_start);
1792 if (start > fw->size)
1793 goto out;
1794
1795 size = le32_to_cpu(fw_info->fw_len);
1796 if (size > (fw->size - start) / FW_OPCODE_SIZE)
1797 goto out;
1798
1799 memcpy(version, fw_info->version, RTL_VER_SIZE);
1800
1801 pa->code = (__le32 *)(fw->data + start);
1802 pa->size = size;
1803 } else {
Francois Romieu1c361ef2011-06-17 17:16:24 +02001804 if (fw->size % FW_OPCODE_SIZE)
1805 goto out;
1806
1807 strlcpy(version, rtl_lookup_firmware_name(tp), RTL_VER_SIZE);
1808
1809 pa->code = (__le32 *)fw->data;
1810 pa->size = fw->size / FW_OPCODE_SIZE;
1811 }
1812 version[RTL_VER_SIZE - 1] = 0;
1813
1814 rc = true;
1815out:
1816 return rc;
1817}
1818
Francois Romieufd112f22011-06-18 00:10:29 +02001819static bool rtl_fw_data_ok(struct rtl8169_private *tp, struct net_device *dev,
1820 struct rtl_fw_phy_action *pa)
Francois Romieu1c361ef2011-06-17 17:16:24 +02001821{
Francois Romieufd112f22011-06-18 00:10:29 +02001822 bool rc = false;
Francois Romieu1c361ef2011-06-17 17:16:24 +02001823 size_t index;
1824
Francois Romieu1c361ef2011-06-17 17:16:24 +02001825 for (index = 0; index < pa->size; index++) {
1826 u32 action = le32_to_cpu(pa->code[index]);
hayeswang42b82dc2011-01-10 02:07:25 +00001827 u32 regno = (action & 0x0fff0000) >> 16;
françois romieubca03d52011-01-03 15:07:31 +00001828
hayeswang42b82dc2011-01-10 02:07:25 +00001829 switch(action & 0xf0000000) {
1830 case PHY_READ:
1831 case PHY_DATA_OR:
1832 case PHY_DATA_AND:
1833 case PHY_READ_EFUSE:
1834 case PHY_CLEAR_READCOUNT:
1835 case PHY_WRITE:
1836 case PHY_WRITE_PREVIOUS:
1837 case PHY_DELAY_MS:
françois romieubca03d52011-01-03 15:07:31 +00001838 break;
1839
hayeswang42b82dc2011-01-10 02:07:25 +00001840 case PHY_BJMPN:
1841 if (regno > index) {
Francois Romieufd112f22011-06-18 00:10:29 +02001842 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001843 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02001844 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00001845 }
1846 break;
1847 case PHY_READCOUNT_EQ_SKIP:
Francois Romieu1c361ef2011-06-17 17:16:24 +02001848 if (index + 2 >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02001849 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001850 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02001851 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00001852 }
1853 break;
1854 case PHY_COMP_EQ_SKIPN:
1855 case PHY_COMP_NEQ_SKIPN:
1856 case PHY_SKIPN:
Francois Romieu1c361ef2011-06-17 17:16:24 +02001857 if (index + 1 + regno >= pa->size) {
Francois Romieufd112f22011-06-18 00:10:29 +02001858 netif_err(tp, ifup, tp->dev,
Francois Romieucecb5fd2011-04-01 10:21:07 +02001859 "Out of range of firmware\n");
Francois Romieufd112f22011-06-18 00:10:29 +02001860 goto out;
hayeswang42b82dc2011-01-10 02:07:25 +00001861 }
1862 break;
1863
1864 case PHY_READ_MAC_BYTE:
1865 case PHY_WRITE_MAC_BYTE:
1866 case PHY_WRITE_ERI_WORD:
1867 default:
Francois Romieufd112f22011-06-18 00:10:29 +02001868 netif_err(tp, ifup, tp->dev,
hayeswang42b82dc2011-01-10 02:07:25 +00001869 "Invalid action 0x%08x\n", action);
Francois Romieufd112f22011-06-18 00:10:29 +02001870 goto out;
françois romieubca03d52011-01-03 15:07:31 +00001871 }
1872 }
Francois Romieufd112f22011-06-18 00:10:29 +02001873 rc = true;
1874out:
1875 return rc;
1876}
françois romieubca03d52011-01-03 15:07:31 +00001877
Francois Romieufd112f22011-06-18 00:10:29 +02001878static int rtl_check_firmware(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
1879{
1880 struct net_device *dev = tp->dev;
1881 int rc = -EINVAL;
1882
1883 if (!rtl_fw_format_ok(tp, rtl_fw)) {
1884 netif_err(tp, ifup, dev, "invalid firwmare\n");
1885 goto out;
1886 }
1887
1888 if (rtl_fw_data_ok(tp, dev, &rtl_fw->phy_action))
1889 rc = 0;
1890out:
1891 return rc;
1892}
1893
1894static void rtl_phy_write_fw(struct rtl8169_private *tp, struct rtl_fw *rtl_fw)
1895{
1896 struct rtl_fw_phy_action *pa = &rtl_fw->phy_action;
1897 u32 predata, count;
1898 size_t index;
1899
1900 predata = count = 0;
hayeswang42b82dc2011-01-10 02:07:25 +00001901
Francois Romieu1c361ef2011-06-17 17:16:24 +02001902 for (index = 0; index < pa->size; ) {
1903 u32 action = le32_to_cpu(pa->code[index]);
françois romieubca03d52011-01-03 15:07:31 +00001904 u32 data = action & 0x0000ffff;
hayeswang42b82dc2011-01-10 02:07:25 +00001905 u32 regno = (action & 0x0fff0000) >> 16;
1906
1907 if (!action)
1908 break;
françois romieubca03d52011-01-03 15:07:31 +00001909
1910 switch(action & 0xf0000000) {
hayeswang42b82dc2011-01-10 02:07:25 +00001911 case PHY_READ:
1912 predata = rtl_readphy(tp, regno);
1913 count++;
1914 index++;
françois romieubca03d52011-01-03 15:07:31 +00001915 break;
hayeswang42b82dc2011-01-10 02:07:25 +00001916 case PHY_DATA_OR:
1917 predata |= data;
1918 index++;
1919 break;
1920 case PHY_DATA_AND:
1921 predata &= data;
1922 index++;
1923 break;
1924 case PHY_BJMPN:
1925 index -= regno;
1926 break;
1927 case PHY_READ_EFUSE:
1928 predata = rtl8168d_efuse_read(tp->mmio_addr, regno);
1929 index++;
1930 break;
1931 case PHY_CLEAR_READCOUNT:
1932 count = 0;
1933 index++;
1934 break;
1935 case PHY_WRITE:
1936 rtl_writephy(tp, regno, data);
1937 index++;
1938 break;
1939 case PHY_READCOUNT_EQ_SKIP:
Francois Romieucecb5fd2011-04-01 10:21:07 +02001940 index += (count == data) ? 2 : 1;
hayeswang42b82dc2011-01-10 02:07:25 +00001941 break;
1942 case PHY_COMP_EQ_SKIPN:
1943 if (predata == data)
1944 index += regno;
1945 index++;
1946 break;
1947 case PHY_COMP_NEQ_SKIPN:
1948 if (predata != data)
1949 index += regno;
1950 index++;
1951 break;
1952 case PHY_WRITE_PREVIOUS:
1953 rtl_writephy(tp, regno, predata);
1954 index++;
1955 break;
1956 case PHY_SKIPN:
1957 index += regno + 1;
1958 break;
1959 case PHY_DELAY_MS:
1960 mdelay(data);
1961 index++;
1962 break;
1963
1964 case PHY_READ_MAC_BYTE:
1965 case PHY_WRITE_MAC_BYTE:
1966 case PHY_WRITE_ERI_WORD:
françois romieubca03d52011-01-03 15:07:31 +00001967 default:
1968 BUG();
1969 }
1970 }
1971}
1972
françois romieuf1e02ed2011-01-13 13:07:53 +00001973static void rtl_release_firmware(struct rtl8169_private *tp)
1974{
Francois Romieub6ffd972011-06-17 17:00:05 +02001975 if (!IS_ERR_OR_NULL(tp->rtl_fw)) {
1976 release_firmware(tp->rtl_fw->fw);
1977 kfree(tp->rtl_fw);
1978 }
1979 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
françois romieuf1e02ed2011-01-13 13:07:53 +00001980}
1981
François Romieu953a12c2011-04-24 17:38:48 +02001982static void rtl_apply_firmware(struct rtl8169_private *tp)
françois romieuf1e02ed2011-01-13 13:07:53 +00001983{
Francois Romieub6ffd972011-06-17 17:00:05 +02001984 struct rtl_fw *rtl_fw = tp->rtl_fw;
françois romieuf1e02ed2011-01-13 13:07:53 +00001985
1986 /* TODO: release firmware once rtl_phy_write_fw signals failures. */
Francois Romieub6ffd972011-06-17 17:00:05 +02001987 if (!IS_ERR_OR_NULL(rtl_fw))
1988 rtl_phy_write_fw(tp, rtl_fw);
François Romieu953a12c2011-04-24 17:38:48 +02001989}
1990
1991static void rtl_apply_firmware_cond(struct rtl8169_private *tp, u8 reg, u16 val)
1992{
1993 if (rtl_readphy(tp, reg) != val)
1994 netif_warn(tp, hw, tp->dev, "chipset not ready for firmware\n");
1995 else
1996 rtl_apply_firmware(tp);
françois romieuf1e02ed2011-01-13 13:07:53 +00001997}
1998
françois romieu4da19632011-01-03 15:07:55 +00001999static void rtl8169s_hw_phy_config(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002001 static const struct phy_reg phy_reg_init[] = {
françois romieu0b9b5712009-08-10 19:44:56 +00002002 { 0x1f, 0x0001 },
2003 { 0x06, 0x006e },
2004 { 0x08, 0x0708 },
2005 { 0x15, 0x4000 },
2006 { 0x18, 0x65c7 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
françois romieu0b9b5712009-08-10 19:44:56 +00002008 { 0x1f, 0x0001 },
2009 { 0x03, 0x00a1 },
2010 { 0x02, 0x0008 },
2011 { 0x01, 0x0120 },
2012 { 0x00, 0x1000 },
2013 { 0x04, 0x0800 },
2014 { 0x04, 0x0000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
françois romieu0b9b5712009-08-10 19:44:56 +00002016 { 0x03, 0xff41 },
2017 { 0x02, 0xdf60 },
2018 { 0x01, 0x0140 },
2019 { 0x00, 0x0077 },
2020 { 0x04, 0x7800 },
2021 { 0x04, 0x7000 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
françois romieu0b9b5712009-08-10 19:44:56 +00002023 { 0x03, 0x802f },
2024 { 0x02, 0x4f02 },
2025 { 0x01, 0x0409 },
2026 { 0x00, 0xf0f9 },
2027 { 0x04, 0x9800 },
2028 { 0x04, 0x9000 },
2029
2030 { 0x03, 0xdf01 },
2031 { 0x02, 0xdf20 },
2032 { 0x01, 0xff95 },
2033 { 0x00, 0xba00 },
2034 { 0x04, 0xa800 },
2035 { 0x04, 0xa000 },
2036
2037 { 0x03, 0xff41 },
2038 { 0x02, 0xdf20 },
2039 { 0x01, 0x0140 },
2040 { 0x00, 0x00bb },
2041 { 0x04, 0xb800 },
2042 { 0x04, 0xb000 },
2043
2044 { 0x03, 0xdf41 },
2045 { 0x02, 0xdc60 },
2046 { 0x01, 0x6340 },
2047 { 0x00, 0x007d },
2048 { 0x04, 0xd800 },
2049 { 0x04, 0xd000 },
2050
2051 { 0x03, 0xdf01 },
2052 { 0x02, 0xdf20 },
2053 { 0x01, 0x100a },
2054 { 0x00, 0xa0ff },
2055 { 0x04, 0xf800 },
2056 { 0x04, 0xf000 },
2057
2058 { 0x1f, 0x0000 },
2059 { 0x0b, 0x0000 },
2060 { 0x00, 0x9200 }
2061 };
2062
françois romieu4da19632011-01-03 15:07:55 +00002063 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064}
2065
françois romieu4da19632011-01-03 15:07:55 +00002066static void rtl8169sb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5615d9f2007-08-17 17:50:46 +02002067{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002068 static const struct phy_reg phy_reg_init[] = {
Francois Romieua441d7b2007-08-17 18:26:35 +02002069 { 0x1f, 0x0002 },
2070 { 0x01, 0x90d0 },
2071 { 0x1f, 0x0000 }
2072 };
2073
françois romieu4da19632011-01-03 15:07:55 +00002074 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02002075}
2076
françois romieu4da19632011-01-03 15:07:55 +00002077static void rtl8169scd_hw_phy_config_quirk(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002078{
2079 struct pci_dev *pdev = tp->pci_dev;
2080 u16 vendor_id, device_id;
2081
2082 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &vendor_id);
2083 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &device_id);
2084
2085 if ((vendor_id != PCI_VENDOR_ID_GIGABYTE) || (device_id != 0xe000))
2086 return;
2087
françois romieu4da19632011-01-03 15:07:55 +00002088 rtl_writephy(tp, 0x1f, 0x0001);
2089 rtl_writephy(tp, 0x10, 0xf01b);
2090 rtl_writephy(tp, 0x1f, 0x0000);
françois romieu2e9558562009-08-10 19:44:19 +00002091}
2092
françois romieu4da19632011-01-03 15:07:55 +00002093static void rtl8169scd_hw_phy_config(struct rtl8169_private *tp)
françois romieu2e9558562009-08-10 19:44:19 +00002094{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002095 static const struct phy_reg phy_reg_init[] = {
françois romieu2e9558562009-08-10 19:44:19 +00002096 { 0x1f, 0x0001 },
2097 { 0x04, 0x0000 },
2098 { 0x03, 0x00a1 },
2099 { 0x02, 0x0008 },
2100 { 0x01, 0x0120 },
2101 { 0x00, 0x1000 },
2102 { 0x04, 0x0800 },
2103 { 0x04, 0x9000 },
2104 { 0x03, 0x802f },
2105 { 0x02, 0x4f02 },
2106 { 0x01, 0x0409 },
2107 { 0x00, 0xf099 },
2108 { 0x04, 0x9800 },
2109 { 0x04, 0xa000 },
2110 { 0x03, 0xdf01 },
2111 { 0x02, 0xdf20 },
2112 { 0x01, 0xff95 },
2113 { 0x00, 0xba00 },
2114 { 0x04, 0xa800 },
2115 { 0x04, 0xf000 },
2116 { 0x03, 0xdf01 },
2117 { 0x02, 0xdf20 },
2118 { 0x01, 0x101a },
2119 { 0x00, 0xa0ff },
2120 { 0x04, 0xf800 },
2121 { 0x04, 0x0000 },
2122 { 0x1f, 0x0000 },
2123
2124 { 0x1f, 0x0001 },
2125 { 0x10, 0xf41b },
2126 { 0x14, 0xfb54 },
2127 { 0x18, 0xf5c7 },
2128 { 0x1f, 0x0000 },
2129
2130 { 0x1f, 0x0001 },
2131 { 0x17, 0x0cc0 },
2132 { 0x1f, 0x0000 }
2133 };
2134
françois romieu4da19632011-01-03 15:07:55 +00002135 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu2e9558562009-08-10 19:44:19 +00002136
françois romieu4da19632011-01-03 15:07:55 +00002137 rtl8169scd_hw_phy_config_quirk(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002138}
2139
françois romieu4da19632011-01-03 15:07:55 +00002140static void rtl8169sce_hw_phy_config(struct rtl8169_private *tp)
françois romieu8c7006a2009-08-10 19:43:29 +00002141{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002142 static const struct phy_reg phy_reg_init[] = {
françois romieu8c7006a2009-08-10 19:43:29 +00002143 { 0x1f, 0x0001 },
2144 { 0x04, 0x0000 },
2145 { 0x03, 0x00a1 },
2146 { 0x02, 0x0008 },
2147 { 0x01, 0x0120 },
2148 { 0x00, 0x1000 },
2149 { 0x04, 0x0800 },
2150 { 0x04, 0x9000 },
2151 { 0x03, 0x802f },
2152 { 0x02, 0x4f02 },
2153 { 0x01, 0x0409 },
2154 { 0x00, 0xf099 },
2155 { 0x04, 0x9800 },
2156 { 0x04, 0xa000 },
2157 { 0x03, 0xdf01 },
2158 { 0x02, 0xdf20 },
2159 { 0x01, 0xff95 },
2160 { 0x00, 0xba00 },
2161 { 0x04, 0xa800 },
2162 { 0x04, 0xf000 },
2163 { 0x03, 0xdf01 },
2164 { 0x02, 0xdf20 },
2165 { 0x01, 0x101a },
2166 { 0x00, 0xa0ff },
2167 { 0x04, 0xf800 },
2168 { 0x04, 0x0000 },
2169 { 0x1f, 0x0000 },
2170
2171 { 0x1f, 0x0001 },
2172 { 0x0b, 0x8480 },
2173 { 0x1f, 0x0000 },
2174
2175 { 0x1f, 0x0001 },
2176 { 0x18, 0x67c7 },
2177 { 0x04, 0x2000 },
2178 { 0x03, 0x002f },
2179 { 0x02, 0x4360 },
2180 { 0x01, 0x0109 },
2181 { 0x00, 0x3022 },
2182 { 0x04, 0x2800 },
2183 { 0x1f, 0x0000 },
2184
2185 { 0x1f, 0x0001 },
2186 { 0x17, 0x0cc0 },
2187 { 0x1f, 0x0000 }
2188 };
2189
françois romieu4da19632011-01-03 15:07:55 +00002190 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieu8c7006a2009-08-10 19:43:29 +00002191}
2192
françois romieu4da19632011-01-03 15:07:55 +00002193static void rtl8168bb_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002194{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002195 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002196 { 0x10, 0xf41b },
2197 { 0x1f, 0x0000 }
2198 };
2199
françois romieu4da19632011-01-03 15:07:55 +00002200 rtl_writephy(tp, 0x1f, 0x0001);
2201 rtl_patchphy(tp, 0x16, 1 << 0);
Francois Romieu236b8082008-05-30 16:11:48 +02002202
françois romieu4da19632011-01-03 15:07:55 +00002203 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002204}
2205
françois romieu4da19632011-01-03 15:07:55 +00002206static void rtl8168bef_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu236b8082008-05-30 16:11:48 +02002207{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002208 static const struct phy_reg phy_reg_init[] = {
Francois Romieu236b8082008-05-30 16:11:48 +02002209 { 0x1f, 0x0001 },
2210 { 0x10, 0xf41b },
2211 { 0x1f, 0x0000 }
2212 };
2213
françois romieu4da19632011-01-03 15:07:55 +00002214 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu236b8082008-05-30 16:11:48 +02002215}
2216
françois romieu4da19632011-01-03 15:07:55 +00002217static void rtl8168cp_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002218{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002219 static const struct phy_reg phy_reg_init[] = {
Francois Romieu867763c2007-08-17 18:21:58 +02002220 { 0x1f, 0x0000 },
2221 { 0x1d, 0x0f00 },
2222 { 0x1f, 0x0002 },
2223 { 0x0c, 0x1ec8 },
2224 { 0x1f, 0x0000 }
2225 };
2226
françois romieu4da19632011-01-03 15:07:55 +00002227 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu867763c2007-08-17 18:21:58 +02002228}
2229
françois romieu4da19632011-01-03 15:07:55 +00002230static void rtl8168cp_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieuef3386f2008-06-29 12:24:30 +02002231{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002232 static const struct phy_reg phy_reg_init[] = {
Francois Romieuef3386f2008-06-29 12:24:30 +02002233 { 0x1f, 0x0001 },
2234 { 0x1d, 0x3d98 },
2235 { 0x1f, 0x0000 }
2236 };
2237
françois romieu4da19632011-01-03 15:07:55 +00002238 rtl_writephy(tp, 0x1f, 0x0000);
2239 rtl_patchphy(tp, 0x14, 1 << 5);
2240 rtl_patchphy(tp, 0x0d, 1 << 5);
Francois Romieuef3386f2008-06-29 12:24:30 +02002241
françois romieu4da19632011-01-03 15:07:55 +00002242 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuef3386f2008-06-29 12:24:30 +02002243}
2244
françois romieu4da19632011-01-03 15:07:55 +00002245static void rtl8168c_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu867763c2007-08-17 18:21:58 +02002246{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002247 static const struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02002248 { 0x1f, 0x0001 },
2249 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02002250 { 0x1f, 0x0002 },
2251 { 0x00, 0x88d4 },
2252 { 0x01, 0x82b1 },
2253 { 0x03, 0x7002 },
2254 { 0x08, 0x9e30 },
2255 { 0x09, 0x01f0 },
2256 { 0x0a, 0x5500 },
2257 { 0x0c, 0x00c8 },
2258 { 0x1f, 0x0003 },
2259 { 0x12, 0xc096 },
2260 { 0x16, 0x000a },
Francois Romieuf50d4272008-05-30 16:07:07 +02002261 { 0x1f, 0x0000 },
2262 { 0x1f, 0x0000 },
2263 { 0x09, 0x2000 },
2264 { 0x09, 0x0000 }
Francois Romieu867763c2007-08-17 18:21:58 +02002265 };
2266
françois romieu4da19632011-01-03 15:07:55 +00002267 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002268
françois romieu4da19632011-01-03 15:07:55 +00002269 rtl_patchphy(tp, 0x14, 1 << 5);
2270 rtl_patchphy(tp, 0x0d, 1 << 5);
2271 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu867763c2007-08-17 18:21:58 +02002272}
2273
françois romieu4da19632011-01-03 15:07:55 +00002274static void rtl8168c_2_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu7da97ec2007-10-18 15:20:43 +02002275{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002276 static const struct phy_reg phy_reg_init[] = {
Francois Romieuf50d4272008-05-30 16:07:07 +02002277 { 0x1f, 0x0001 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002278 { 0x12, 0x2300 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002279 { 0x03, 0x802f },
2280 { 0x02, 0x4f02 },
2281 { 0x01, 0x0409 },
2282 { 0x00, 0xf099 },
2283 { 0x04, 0x9800 },
2284 { 0x04, 0x9000 },
2285 { 0x1d, 0x3d98 },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002286 { 0x1f, 0x0002 },
2287 { 0x0c, 0x7eb8 },
Francois Romieuf50d4272008-05-30 16:07:07 +02002288 { 0x06, 0x0761 },
2289 { 0x1f, 0x0003 },
2290 { 0x16, 0x0f0a },
Francois Romieu7da97ec2007-10-18 15:20:43 +02002291 { 0x1f, 0x0000 }
2292 };
2293
françois romieu4da19632011-01-03 15:07:55 +00002294 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieuf50d4272008-05-30 16:07:07 +02002295
françois romieu4da19632011-01-03 15:07:55 +00002296 rtl_patchphy(tp, 0x16, 1 << 0);
2297 rtl_patchphy(tp, 0x14, 1 << 5);
2298 rtl_patchphy(tp, 0x0d, 1 << 5);
2299 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002300}
2301
françois romieu4da19632011-01-03 15:07:55 +00002302static void rtl8168c_3_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu197ff762008-06-28 13:16:02 +02002303{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002304 static const struct phy_reg phy_reg_init[] = {
Francois Romieu197ff762008-06-28 13:16:02 +02002305 { 0x1f, 0x0001 },
2306 { 0x12, 0x2300 },
2307 { 0x1d, 0x3d98 },
2308 { 0x1f, 0x0002 },
2309 { 0x0c, 0x7eb8 },
2310 { 0x06, 0x5461 },
2311 { 0x1f, 0x0003 },
2312 { 0x16, 0x0f0a },
2313 { 0x1f, 0x0000 }
2314 };
2315
françois romieu4da19632011-01-03 15:07:55 +00002316 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu197ff762008-06-28 13:16:02 +02002317
françois romieu4da19632011-01-03 15:07:55 +00002318 rtl_patchphy(tp, 0x16, 1 << 0);
2319 rtl_patchphy(tp, 0x14, 1 << 5);
2320 rtl_patchphy(tp, 0x0d, 1 << 5);
2321 rtl_writephy(tp, 0x1f, 0x0000);
Francois Romieu197ff762008-06-28 13:16:02 +02002322}
2323
françois romieu4da19632011-01-03 15:07:55 +00002324static void rtl8168c_4_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu6fb07052008-06-29 11:54:28 +02002325{
françois romieu4da19632011-01-03 15:07:55 +00002326 rtl8168c_3_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002327}
2328
françois romieubca03d52011-01-03 15:07:31 +00002329static void rtl8168d_1_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu5b538df2008-07-20 16:22:45 +02002330{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002331 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002332 /* Channel Estimation */
Francois Romieu5b538df2008-07-20 16:22:45 +02002333 { 0x1f, 0x0001 },
françois romieudaf9df62009-10-07 12:44:20 +00002334 { 0x06, 0x4064 },
2335 { 0x07, 0x2863 },
2336 { 0x08, 0x059c },
2337 { 0x09, 0x26b4 },
2338 { 0x0a, 0x6a19 },
2339 { 0x0b, 0xdcc8 },
2340 { 0x10, 0xf06d },
2341 { 0x14, 0x7f68 },
2342 { 0x18, 0x7fd9 },
2343 { 0x1c, 0xf0ff },
2344 { 0x1d, 0x3d9c },
Francois Romieu5b538df2008-07-20 16:22:45 +02002345 { 0x1f, 0x0003 },
françois romieudaf9df62009-10-07 12:44:20 +00002346 { 0x12, 0xf49f },
2347 { 0x13, 0x070b },
2348 { 0x1a, 0x05ad },
françois romieubca03d52011-01-03 15:07:31 +00002349 { 0x14, 0x94c0 },
2350
2351 /*
2352 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002353 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002354 */
Francois Romieu5b538df2008-07-20 16:22:45 +02002355 { 0x1f, 0x0002 },
françois romieudaf9df62009-10-07 12:44:20 +00002356 { 0x06, 0x5561 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002357 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002358 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002359 { 0x06, 0x5561 },
2360
2361 /*
2362 * Can not link to 1Gbps with bad cable
2363 * Decrease SNR threshold form 21.07dB to 19.04dB
2364 */
2365 { 0x1f, 0x0001 },
2366 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002367
2368 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002369 { 0x0d, 0xf880 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002370 };
françois romieubca03d52011-01-03 15:07:31 +00002371 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu5b538df2008-07-20 16:22:45 +02002372
françois romieu4da19632011-01-03 15:07:55 +00002373 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
Francois Romieu5b538df2008-07-20 16:22:45 +02002374
françois romieubca03d52011-01-03 15:07:31 +00002375 /*
2376 * Rx Error Issue
2377 * Fine Tune Switching regulator parameter
2378 */
françois romieu4da19632011-01-03 15:07:55 +00002379 rtl_writephy(tp, 0x1f, 0x0002);
2380 rtl_w1w0_phy(tp, 0x0b, 0x0010, 0x00ef);
2381 rtl_w1w0_phy(tp, 0x0c, 0xa200, 0x5d00);
françois romieudaf9df62009-10-07 12:44:20 +00002382
françois romieudaf9df62009-10-07 12:44:20 +00002383 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002384 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002385 { 0x1f, 0x0002 },
2386 { 0x05, 0x669a },
Francois Romieu5b538df2008-07-20 16:22:45 +02002387 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002388 { 0x05, 0x8330 },
2389 { 0x06, 0x669a },
2390 { 0x1f, 0x0002 }
2391 };
2392 int val;
2393
françois romieu4da19632011-01-03 15:07:55 +00002394 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002395
françois romieu4da19632011-01-03 15:07:55 +00002396 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002397
2398 if ((val & 0x00ff) != 0x006c) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002399 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002400 0x0065, 0x0066, 0x0067, 0x0068,
2401 0x0069, 0x006a, 0x006b, 0x006c
2402 };
2403 int i;
2404
françois romieu4da19632011-01-03 15:07:55 +00002405 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002406
2407 val &= 0xff00;
2408 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002409 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002410 }
2411 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002412 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002413 { 0x1f, 0x0002 },
2414 { 0x05, 0x6662 },
Francois Romieu5b538df2008-07-20 16:22:45 +02002415 { 0x1f, 0x0005 },
françois romieudaf9df62009-10-07 12:44:20 +00002416 { 0x05, 0x8330 },
2417 { 0x06, 0x6662 }
Francois Romieu5b538df2008-07-20 16:22:45 +02002418 };
2419
françois romieu4da19632011-01-03 15:07:55 +00002420 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002421 }
2422
françois romieubca03d52011-01-03 15:07:31 +00002423 /* RSET couple improve */
françois romieu4da19632011-01-03 15:07:55 +00002424 rtl_writephy(tp, 0x1f, 0x0002);
2425 rtl_patchphy(tp, 0x0d, 0x0300);
2426 rtl_patchphy(tp, 0x0f, 0x0010);
françois romieudaf9df62009-10-07 12:44:20 +00002427
françois romieubca03d52011-01-03 15:07:31 +00002428 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002429 rtl_writephy(tp, 0x1f, 0x0002);
2430 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2431 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002432
françois romieu4da19632011-01-03 15:07:55 +00002433 rtl_writephy(tp, 0x1f, 0x0005);
2434 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002435
2436 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xbf00);
françois romieubca03d52011-01-03 15:07:31 +00002437
françois romieu4da19632011-01-03 15:07:55 +00002438 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002439}
2440
françois romieubca03d52011-01-03 15:07:31 +00002441static void rtl8168d_2_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002442{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002443 static const struct phy_reg phy_reg_init_0[] = {
françois romieubca03d52011-01-03 15:07:31 +00002444 /* Channel Estimation */
françois romieudaf9df62009-10-07 12:44:20 +00002445 { 0x1f, 0x0001 },
2446 { 0x06, 0x4064 },
2447 { 0x07, 0x2863 },
2448 { 0x08, 0x059c },
2449 { 0x09, 0x26b4 },
2450 { 0x0a, 0x6a19 },
2451 { 0x0b, 0xdcc8 },
2452 { 0x10, 0xf06d },
2453 { 0x14, 0x7f68 },
2454 { 0x18, 0x7fd9 },
2455 { 0x1c, 0xf0ff },
2456 { 0x1d, 0x3d9c },
2457 { 0x1f, 0x0003 },
2458 { 0x12, 0xf49f },
2459 { 0x13, 0x070b },
2460 { 0x1a, 0x05ad },
2461 { 0x14, 0x94c0 },
2462
françois romieubca03d52011-01-03 15:07:31 +00002463 /*
2464 * Tx Error Issue
Francois Romieucecb5fd2011-04-01 10:21:07 +02002465 * Enhance line driver power
françois romieubca03d52011-01-03 15:07:31 +00002466 */
françois romieudaf9df62009-10-07 12:44:20 +00002467 { 0x1f, 0x0002 },
2468 { 0x06, 0x5561 },
2469 { 0x1f, 0x0005 },
2470 { 0x05, 0x8332 },
françois romieubca03d52011-01-03 15:07:31 +00002471 { 0x06, 0x5561 },
2472
2473 /*
2474 * Can not link to 1Gbps with bad cable
2475 * Decrease SNR threshold form 21.07dB to 19.04dB
2476 */
2477 { 0x1f, 0x0001 },
2478 { 0x17, 0x0cc0 },
françois romieudaf9df62009-10-07 12:44:20 +00002479
2480 { 0x1f, 0x0000 },
françois romieubca03d52011-01-03 15:07:31 +00002481 { 0x0d, 0xf880 }
françois romieudaf9df62009-10-07 12:44:20 +00002482 };
françois romieubca03d52011-01-03 15:07:31 +00002483 void __iomem *ioaddr = tp->mmio_addr;
françois romieudaf9df62009-10-07 12:44:20 +00002484
françois romieu4da19632011-01-03 15:07:55 +00002485 rtl_writephy_batch(tp, phy_reg_init_0, ARRAY_SIZE(phy_reg_init_0));
françois romieudaf9df62009-10-07 12:44:20 +00002486
2487 if (rtl8168d_efuse_read(ioaddr, 0x01) == 0xb1) {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002488 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002489 { 0x1f, 0x0002 },
2490 { 0x05, 0x669a },
2491 { 0x1f, 0x0005 },
2492 { 0x05, 0x8330 },
2493 { 0x06, 0x669a },
2494
2495 { 0x1f, 0x0002 }
2496 };
2497 int val;
2498
françois romieu4da19632011-01-03 15:07:55 +00002499 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002500
françois romieu4da19632011-01-03 15:07:55 +00002501 val = rtl_readphy(tp, 0x0d);
françois romieudaf9df62009-10-07 12:44:20 +00002502 if ((val & 0x00ff) != 0x006c) {
Joe Perchesb6bc7652010-12-21 02:16:08 -08002503 static const u32 set[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002504 0x0065, 0x0066, 0x0067, 0x0068,
2505 0x0069, 0x006a, 0x006b, 0x006c
2506 };
2507 int i;
2508
françois romieu4da19632011-01-03 15:07:55 +00002509 rtl_writephy(tp, 0x1f, 0x0002);
françois romieudaf9df62009-10-07 12:44:20 +00002510
2511 val &= 0xff00;
2512 for (i = 0; i < ARRAY_SIZE(set); i++)
françois romieu4da19632011-01-03 15:07:55 +00002513 rtl_writephy(tp, 0x0d, val | set[i]);
françois romieudaf9df62009-10-07 12:44:20 +00002514 }
2515 } else {
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002516 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002517 { 0x1f, 0x0002 },
2518 { 0x05, 0x2642 },
2519 { 0x1f, 0x0005 },
2520 { 0x05, 0x8330 },
2521 { 0x06, 0x2642 }
2522 };
2523
françois romieu4da19632011-01-03 15:07:55 +00002524 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
françois romieudaf9df62009-10-07 12:44:20 +00002525 }
2526
françois romieubca03d52011-01-03 15:07:31 +00002527 /* Fine tune PLL performance */
françois romieu4da19632011-01-03 15:07:55 +00002528 rtl_writephy(tp, 0x1f, 0x0002);
2529 rtl_w1w0_phy(tp, 0x02, 0x0100, 0x0600);
2530 rtl_w1w0_phy(tp, 0x03, 0x0000, 0xe000);
françois romieudaf9df62009-10-07 12:44:20 +00002531
françois romieubca03d52011-01-03 15:07:31 +00002532 /* Switching regulator Slew rate */
françois romieu4da19632011-01-03 15:07:55 +00002533 rtl_writephy(tp, 0x1f, 0x0002);
2534 rtl_patchphy(tp, 0x0f, 0x0017);
françois romieudaf9df62009-10-07 12:44:20 +00002535
françois romieu4da19632011-01-03 15:07:55 +00002536 rtl_writephy(tp, 0x1f, 0x0005);
2537 rtl_writephy(tp, 0x05, 0x001b);
François Romieu953a12c2011-04-24 17:38:48 +02002538
2539 rtl_apply_firmware_cond(tp, MII_EXPANSION, 0xb300);
françois romieubca03d52011-01-03 15:07:31 +00002540
françois romieu4da19632011-01-03 15:07:55 +00002541 rtl_writephy(tp, 0x1f, 0x0000);
françois romieudaf9df62009-10-07 12:44:20 +00002542}
2543
françois romieu4da19632011-01-03 15:07:55 +00002544static void rtl8168d_3_hw_phy_config(struct rtl8169_private *tp)
françois romieudaf9df62009-10-07 12:44:20 +00002545{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002546 static const struct phy_reg phy_reg_init[] = {
françois romieudaf9df62009-10-07 12:44:20 +00002547 { 0x1f, 0x0002 },
2548 { 0x10, 0x0008 },
2549 { 0x0d, 0x006c },
2550
2551 { 0x1f, 0x0000 },
2552 { 0x0d, 0xf880 },
2553
2554 { 0x1f, 0x0001 },
2555 { 0x17, 0x0cc0 },
2556
2557 { 0x1f, 0x0001 },
2558 { 0x0b, 0xa4d8 },
2559 { 0x09, 0x281c },
2560 { 0x07, 0x2883 },
2561 { 0x0a, 0x6b35 },
2562 { 0x1d, 0x3da4 },
2563 { 0x1c, 0xeffd },
2564 { 0x14, 0x7f52 },
2565 { 0x18, 0x7fc6 },
2566 { 0x08, 0x0601 },
2567 { 0x06, 0x4063 },
2568 { 0x10, 0xf074 },
2569 { 0x1f, 0x0003 },
2570 { 0x13, 0x0789 },
2571 { 0x12, 0xf4bd },
2572 { 0x1a, 0x04fd },
2573 { 0x14, 0x84b0 },
2574 { 0x1f, 0x0000 },
2575 { 0x00, 0x9200 },
2576
2577 { 0x1f, 0x0005 },
2578 { 0x01, 0x0340 },
2579 { 0x1f, 0x0001 },
2580 { 0x04, 0x4000 },
2581 { 0x03, 0x1d21 },
2582 { 0x02, 0x0c32 },
2583 { 0x01, 0x0200 },
2584 { 0x00, 0x5554 },
2585 { 0x04, 0x4800 },
2586 { 0x04, 0x4000 },
2587 { 0x04, 0xf000 },
2588 { 0x03, 0xdf01 },
2589 { 0x02, 0xdf20 },
2590 { 0x01, 0x101a },
2591 { 0x00, 0xa0ff },
2592 { 0x04, 0xf800 },
2593 { 0x04, 0xf000 },
2594 { 0x1f, 0x0000 },
2595
2596 { 0x1f, 0x0007 },
2597 { 0x1e, 0x0023 },
2598 { 0x16, 0x0000 },
2599 { 0x1f, 0x0000 }
2600 };
2601
françois romieu4da19632011-01-03 15:07:55 +00002602 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5b538df2008-07-20 16:22:45 +02002603}
2604
françois romieue6de30d2011-01-03 15:08:37 +00002605static void rtl8168d_4_hw_phy_config(struct rtl8169_private *tp)
2606{
2607 static const struct phy_reg phy_reg_init[] = {
2608 { 0x1f, 0x0001 },
2609 { 0x17, 0x0cc0 },
2610
2611 { 0x1f, 0x0007 },
2612 { 0x1e, 0x002d },
2613 { 0x18, 0x0040 },
2614 { 0x1f, 0x0000 }
2615 };
2616
2617 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2618 rtl_patchphy(tp, 0x0d, 1 << 5);
2619}
2620
hayeswang01dc7fe2011-03-21 01:50:28 +00002621static void rtl8168e_hw_phy_config(struct rtl8169_private *tp)
2622{
2623 static const struct phy_reg phy_reg_init[] = {
2624 /* Enable Delay cap */
2625 { 0x1f, 0x0005 },
2626 { 0x05, 0x8b80 },
2627 { 0x06, 0xc896 },
2628 { 0x1f, 0x0000 },
2629
2630 /* Channel estimation fine tune */
2631 { 0x1f, 0x0001 },
2632 { 0x0b, 0x6c20 },
2633 { 0x07, 0x2872 },
2634 { 0x1c, 0xefff },
2635 { 0x1f, 0x0003 },
2636 { 0x14, 0x6420 },
2637 { 0x1f, 0x0000 },
2638
2639 /* Update PFM & 10M TX idle timer */
2640 { 0x1f, 0x0007 },
2641 { 0x1e, 0x002f },
2642 { 0x15, 0x1919 },
2643 { 0x1f, 0x0000 },
2644
2645 { 0x1f, 0x0007 },
2646 { 0x1e, 0x00ac },
2647 { 0x18, 0x0006 },
2648 { 0x1f, 0x0000 }
2649 };
2650
Francois Romieu15ecd032011-04-27 13:52:22 -07002651 rtl_apply_firmware(tp);
2652
hayeswang01dc7fe2011-03-21 01:50:28 +00002653 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2654
2655 /* DCO enable for 10M IDLE Power */
2656 rtl_writephy(tp, 0x1f, 0x0007);
2657 rtl_writephy(tp, 0x1e, 0x0023);
2658 rtl_w1w0_phy(tp, 0x17, 0x0006, 0x0000);
2659 rtl_writephy(tp, 0x1f, 0x0000);
2660
2661 /* For impedance matching */
2662 rtl_writephy(tp, 0x1f, 0x0002);
2663 rtl_w1w0_phy(tp, 0x08, 0x8000, 0x7f00);
Francois Romieucecb5fd2011-04-01 10:21:07 +02002664 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00002665
2666 /* PHY auto speed down */
2667 rtl_writephy(tp, 0x1f, 0x0007);
2668 rtl_writephy(tp, 0x1e, 0x002d);
2669 rtl_w1w0_phy(tp, 0x18, 0x0050, 0x0000);
2670 rtl_writephy(tp, 0x1f, 0x0000);
2671 rtl_w1w0_phy(tp, 0x14, 0x8000, 0x0000);
2672
2673 rtl_writephy(tp, 0x1f, 0x0005);
2674 rtl_writephy(tp, 0x05, 0x8b86);
2675 rtl_w1w0_phy(tp, 0x06, 0x0001, 0x0000);
2676 rtl_writephy(tp, 0x1f, 0x0000);
2677
2678 rtl_writephy(tp, 0x1f, 0x0005);
2679 rtl_writephy(tp, 0x05, 0x8b85);
2680 rtl_w1w0_phy(tp, 0x06, 0x0000, 0x2000);
2681 rtl_writephy(tp, 0x1f, 0x0007);
2682 rtl_writephy(tp, 0x1e, 0x0020);
2683 rtl_w1w0_phy(tp, 0x15, 0x0000, 0x1100);
2684 rtl_writephy(tp, 0x1f, 0x0006);
2685 rtl_writephy(tp, 0x00, 0x5a00);
2686 rtl_writephy(tp, 0x1f, 0x0000);
2687 rtl_writephy(tp, 0x0d, 0x0007);
2688 rtl_writephy(tp, 0x0e, 0x003c);
2689 rtl_writephy(tp, 0x0d, 0x4007);
2690 rtl_writephy(tp, 0x0e, 0x0000);
2691 rtl_writephy(tp, 0x0d, 0x0000);
2692}
2693
françois romieu4da19632011-01-03 15:07:55 +00002694static void rtl8102e_hw_phy_config(struct rtl8169_private *tp)
Francois Romieu2857ffb2008-08-02 21:08:49 +02002695{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08002696 static const struct phy_reg phy_reg_init[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02002697 { 0x1f, 0x0003 },
2698 { 0x08, 0x441d },
2699 { 0x01, 0x9100 },
2700 { 0x1f, 0x0000 }
2701 };
2702
françois romieu4da19632011-01-03 15:07:55 +00002703 rtl_writephy(tp, 0x1f, 0x0000);
2704 rtl_patchphy(tp, 0x11, 1 << 12);
2705 rtl_patchphy(tp, 0x19, 1 << 13);
2706 rtl_patchphy(tp, 0x10, 1 << 15);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002707
françois romieu4da19632011-01-03 15:07:55 +00002708 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu2857ffb2008-08-02 21:08:49 +02002709}
2710
Hayes Wang5a5e4442011-02-22 17:26:21 +08002711static void rtl8105e_hw_phy_config(struct rtl8169_private *tp)
2712{
2713 static const struct phy_reg phy_reg_init[] = {
2714 { 0x1f, 0x0005 },
2715 { 0x1a, 0x0000 },
2716 { 0x1f, 0x0000 },
2717
2718 { 0x1f, 0x0004 },
2719 { 0x1c, 0x0000 },
2720 { 0x1f, 0x0000 },
2721
2722 { 0x1f, 0x0001 },
2723 { 0x15, 0x7701 },
2724 { 0x1f, 0x0000 }
2725 };
2726
2727 /* Disable ALDPS before ram code */
2728 rtl_writephy(tp, 0x1f, 0x0000);
2729 rtl_writephy(tp, 0x18, 0x0310);
2730 msleep(100);
2731
François Romieu953a12c2011-04-24 17:38:48 +02002732 rtl_apply_firmware(tp);
Hayes Wang5a5e4442011-02-22 17:26:21 +08002733
2734 rtl_writephy_batch(tp, phy_reg_init, ARRAY_SIZE(phy_reg_init));
2735}
2736
Francois Romieu5615d9f2007-08-17 17:50:46 +02002737static void rtl_hw_phy_config(struct net_device *dev)
2738{
2739 struct rtl8169_private *tp = netdev_priv(dev);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002740
2741 rtl8169_print_mac_version(tp);
2742
2743 switch (tp->mac_version) {
2744 case RTL_GIGA_MAC_VER_01:
2745 break;
2746 case RTL_GIGA_MAC_VER_02:
2747 case RTL_GIGA_MAC_VER_03:
françois romieu4da19632011-01-03 15:07:55 +00002748 rtl8169s_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002749 break;
2750 case RTL_GIGA_MAC_VER_04:
françois romieu4da19632011-01-03 15:07:55 +00002751 rtl8169sb_hw_phy_config(tp);
Francois Romieu5615d9f2007-08-17 17:50:46 +02002752 break;
françois romieu2e9558562009-08-10 19:44:19 +00002753 case RTL_GIGA_MAC_VER_05:
françois romieu4da19632011-01-03 15:07:55 +00002754 rtl8169scd_hw_phy_config(tp);
françois romieu2e9558562009-08-10 19:44:19 +00002755 break;
françois romieu8c7006a2009-08-10 19:43:29 +00002756 case RTL_GIGA_MAC_VER_06:
françois romieu4da19632011-01-03 15:07:55 +00002757 rtl8169sce_hw_phy_config(tp);
françois romieu8c7006a2009-08-10 19:43:29 +00002758 break;
Francois Romieu2857ffb2008-08-02 21:08:49 +02002759 case RTL_GIGA_MAC_VER_07:
2760 case RTL_GIGA_MAC_VER_08:
2761 case RTL_GIGA_MAC_VER_09:
françois romieu4da19632011-01-03 15:07:55 +00002762 rtl8102e_hw_phy_config(tp);
Francois Romieu2857ffb2008-08-02 21:08:49 +02002763 break;
Francois Romieu236b8082008-05-30 16:11:48 +02002764 case RTL_GIGA_MAC_VER_11:
françois romieu4da19632011-01-03 15:07:55 +00002765 rtl8168bb_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002766 break;
2767 case RTL_GIGA_MAC_VER_12:
françois romieu4da19632011-01-03 15:07:55 +00002768 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002769 break;
2770 case RTL_GIGA_MAC_VER_17:
françois romieu4da19632011-01-03 15:07:55 +00002771 rtl8168bef_hw_phy_config(tp);
Francois Romieu236b8082008-05-30 16:11:48 +02002772 break;
Francois Romieu867763c2007-08-17 18:21:58 +02002773 case RTL_GIGA_MAC_VER_18:
françois romieu4da19632011-01-03 15:07:55 +00002774 rtl8168cp_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002775 break;
2776 case RTL_GIGA_MAC_VER_19:
françois romieu4da19632011-01-03 15:07:55 +00002777 rtl8168c_1_hw_phy_config(tp);
Francois Romieu867763c2007-08-17 18:21:58 +02002778 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02002779 case RTL_GIGA_MAC_VER_20:
françois romieu4da19632011-01-03 15:07:55 +00002780 rtl8168c_2_hw_phy_config(tp);
Francois Romieu7da97ec2007-10-18 15:20:43 +02002781 break;
Francois Romieu197ff762008-06-28 13:16:02 +02002782 case RTL_GIGA_MAC_VER_21:
françois romieu4da19632011-01-03 15:07:55 +00002783 rtl8168c_3_hw_phy_config(tp);
Francois Romieu197ff762008-06-28 13:16:02 +02002784 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02002785 case RTL_GIGA_MAC_VER_22:
françois romieu4da19632011-01-03 15:07:55 +00002786 rtl8168c_4_hw_phy_config(tp);
Francois Romieu6fb07052008-06-29 11:54:28 +02002787 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02002788 case RTL_GIGA_MAC_VER_23:
Francois Romieu7f3e3d32008-07-20 18:53:20 +02002789 case RTL_GIGA_MAC_VER_24:
françois romieu4da19632011-01-03 15:07:55 +00002790 rtl8168cp_2_hw_phy_config(tp);
Francois Romieuef3386f2008-06-29 12:24:30 +02002791 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02002792 case RTL_GIGA_MAC_VER_25:
françois romieubca03d52011-01-03 15:07:31 +00002793 rtl8168d_1_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002794 break;
2795 case RTL_GIGA_MAC_VER_26:
françois romieubca03d52011-01-03 15:07:31 +00002796 rtl8168d_2_hw_phy_config(tp);
françois romieudaf9df62009-10-07 12:44:20 +00002797 break;
2798 case RTL_GIGA_MAC_VER_27:
françois romieu4da19632011-01-03 15:07:55 +00002799 rtl8168d_3_hw_phy_config(tp);
Francois Romieu5b538df2008-07-20 16:22:45 +02002800 break;
françois romieue6de30d2011-01-03 15:08:37 +00002801 case RTL_GIGA_MAC_VER_28:
2802 rtl8168d_4_hw_phy_config(tp);
2803 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08002804 case RTL_GIGA_MAC_VER_29:
2805 case RTL_GIGA_MAC_VER_30:
2806 rtl8105e_hw_phy_config(tp);
2807 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02002808 case RTL_GIGA_MAC_VER_31:
2809 /* None. */
2810 break;
hayeswang01dc7fe2011-03-21 01:50:28 +00002811 case RTL_GIGA_MAC_VER_32:
hayeswang01dc7fe2011-03-21 01:50:28 +00002812 case RTL_GIGA_MAC_VER_33:
Francois Romieu15ecd032011-04-27 13:52:22 -07002813 rtl8168e_hw_phy_config(tp);
hayeswang01dc7fe2011-03-21 01:50:28 +00002814 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02002815
Francois Romieu5615d9f2007-08-17 17:50:46 +02002816 default:
2817 break;
2818 }
2819}
2820
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821static void rtl8169_phy_timer(unsigned long __opaque)
2822{
2823 struct net_device *dev = (struct net_device *)__opaque;
2824 struct rtl8169_private *tp = netdev_priv(dev);
2825 struct timer_list *timer = &tp->timer;
2826 void __iomem *ioaddr = tp->mmio_addr;
2827 unsigned long timeout = RTL8169_PHY_TIMEOUT;
2828
Francois Romieubcf0bf92006-07-26 23:14:13 +02002829 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 spin_lock_irq(&tp->lock);
2832
françois romieu4da19632011-01-03 15:07:55 +00002833 if (tp->phy_reset_pending(tp)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02002834 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 * A busy loop could burn quite a few cycles on nowadays CPU.
2836 * Let's delay the execution of the timer for a few ticks.
2837 */
2838 timeout = HZ/10;
2839 goto out_mod_timer;
2840 }
2841
2842 if (tp->link_ok(ioaddr))
2843 goto out_unlock;
2844
Joe Perchesbf82c182010-02-09 11:49:50 +00002845 netif_warn(tp, link, dev, "PHY reset until link up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846
françois romieu4da19632011-01-03 15:07:55 +00002847 tp->phy_reset_enable(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
2849out_mod_timer:
2850 mod_timer(timer, jiffies + timeout);
2851out_unlock:
2852 spin_unlock_irq(&tp->lock);
2853}
2854
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855#ifdef CONFIG_NET_POLL_CONTROLLER
2856/*
2857 * Polling 'interrupt' - used by things like netconsole to send skbs
2858 * without having to re-enable interrupts. It's not called while
2859 * the interrupt routine is executing.
2860 */
2861static void rtl8169_netpoll(struct net_device *dev)
2862{
2863 struct rtl8169_private *tp = netdev_priv(dev);
2864 struct pci_dev *pdev = tp->pci_dev;
2865
2866 disable_irq(pdev->irq);
David Howells7d12e782006-10-05 14:55:46 +01002867 rtl8169_interrupt(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 enable_irq(pdev->irq);
2869}
2870#endif
2871
2872static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
2873 void __iomem *ioaddr)
2874{
2875 iounmap(ioaddr);
2876 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00002877 pci_clear_mwi(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 pci_disable_device(pdev);
2879 free_netdev(dev);
2880}
2881
Francois Romieubf793292006-11-01 00:53:05 +01002882static void rtl8169_phy_reset(struct net_device *dev,
2883 struct rtl8169_private *tp)
2884{
Francois Romieu07d3f512007-02-21 22:40:46 +01002885 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01002886
françois romieu4da19632011-01-03 15:07:55 +00002887 tp->phy_reset_enable(tp);
Francois Romieubf793292006-11-01 00:53:05 +01002888 for (i = 0; i < 100; i++) {
françois romieu4da19632011-01-03 15:07:55 +00002889 if (!tp->phy_reset_pending(tp))
Francois Romieubf793292006-11-01 00:53:05 +01002890 return;
2891 msleep(1);
2892 }
Joe Perchesbf82c182010-02-09 11:49:50 +00002893 netif_err(tp, link, dev, "PHY reset failed\n");
Francois Romieubf793292006-11-01 00:53:05 +01002894}
2895
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002896static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002898 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002899
Francois Romieu5615d9f2007-08-17 17:50:46 +02002900 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002901
Marcus Sundberg773328942008-07-10 21:28:08 +02002902 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
2903 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2904 RTL_W8(0x82, 0x01);
2905 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002906
Francois Romieu6dccd162007-02-13 23:38:05 +01002907 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
2908
2909 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
2910 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002911
Francois Romieubcf0bf92006-07-26 23:14:13 +02002912 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002913 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
2914 RTL_W8(0x82, 0x01);
2915 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
françois romieu4da19632011-01-03 15:07:55 +00002916 rtl_writephy(tp, 0x0b, 0x0000); //w 0x0b 15 0 0
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002917 }
2918
Francois Romieubf793292006-11-01 00:53:05 +01002919 rtl8169_phy_reset(dev, tp);
2920
Oliver Neukum54405cd2011-01-06 21:55:13 +01002921 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL,
Francois Romieucecb5fd2011-04-01 10:21:07 +02002922 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
2923 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
2924 (tp->mii.supports_gmii ?
2925 ADVERTISED_1000baseT_Half |
2926 ADVERTISED_1000baseT_Full : 0));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002927
Joe Perchesbf82c182010-02-09 11:49:50 +00002928 if (RTL_R8(PHYstatus) & TBI_Enable)
2929 netif_info(tp, link, dev, "TBI auto-negotiating\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02002930}
2931
Francois Romieu773d2022007-01-31 23:47:43 +01002932static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
2933{
2934 void __iomem *ioaddr = tp->mmio_addr;
2935 u32 high;
2936 u32 low;
2937
2938 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
2939 high = addr[4] | (addr[5] << 8);
2940
2941 spin_lock_irq(&tp->lock);
2942
2943 RTL_W8(Cfg9346, Cfg9346_Unlock);
françois romieu908ba2b2010-04-26 11:42:58 +00002944
Francois Romieu773d2022007-01-31 23:47:43 +01002945 RTL_W32(MAC4, high);
françois romieu908ba2b2010-04-26 11:42:58 +00002946 RTL_R32(MAC4);
2947
Francois Romieu78f1cd02010-03-27 19:35:46 -07002948 RTL_W32(MAC0, low);
françois romieu908ba2b2010-04-26 11:42:58 +00002949 RTL_R32(MAC0);
2950
Francois Romieu773d2022007-01-31 23:47:43 +01002951 RTL_W8(Cfg9346, Cfg9346_Lock);
2952
2953 spin_unlock_irq(&tp->lock);
2954}
2955
2956static int rtl_set_mac_address(struct net_device *dev, void *p)
2957{
2958 struct rtl8169_private *tp = netdev_priv(dev);
2959 struct sockaddr *addr = p;
2960
2961 if (!is_valid_ether_addr(addr->sa_data))
2962 return -EADDRNOTAVAIL;
2963
2964 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
2965
2966 rtl_rar_set(tp, dev->dev_addr);
2967
2968 return 0;
2969}
2970
Francois Romieu5f787a12006-08-17 13:02:36 +02002971static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2972{
2973 struct rtl8169_private *tp = netdev_priv(dev);
2974 struct mii_ioctl_data *data = if_mii(ifr);
2975
Francois Romieu8b4ab282008-11-19 22:05:25 -08002976 return netif_running(dev) ? tp->do_ioctl(tp, data, cmd) : -ENODEV;
2977}
Francois Romieu5f787a12006-08-17 13:02:36 +02002978
Francois Romieucecb5fd2011-04-01 10:21:07 +02002979static int rtl_xmii_ioctl(struct rtl8169_private *tp,
2980 struct mii_ioctl_data *data, int cmd)
Francois Romieu8b4ab282008-11-19 22:05:25 -08002981{
Francois Romieu5f787a12006-08-17 13:02:36 +02002982 switch (cmd) {
2983 case SIOCGMIIPHY:
2984 data->phy_id = 32; /* Internal PHY */
2985 return 0;
2986
2987 case SIOCGMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00002988 data->val_out = rtl_readphy(tp, data->reg_num & 0x1f);
Francois Romieu5f787a12006-08-17 13:02:36 +02002989 return 0;
2990
2991 case SIOCSMIIREG:
françois romieu4da19632011-01-03 15:07:55 +00002992 rtl_writephy(tp, data->reg_num & 0x1f, data->val_in);
Francois Romieu5f787a12006-08-17 13:02:36 +02002993 return 0;
2994 }
2995 return -EOPNOTSUPP;
2996}
2997
Francois Romieu8b4ab282008-11-19 22:05:25 -08002998static int rtl_tbi_ioctl(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd)
2999{
3000 return -EOPNOTSUPP;
3001}
3002
Francois Romieu0e485152007-02-20 00:00:26 +01003003static const struct rtl_cfg_info {
3004 void (*hw_start)(struct net_device *);
3005 unsigned int region;
3006 unsigned int align;
3007 u16 intr_event;
3008 u16 napi_event;
Francois Romieuccdffb92008-07-26 14:26:06 +02003009 unsigned features;
Jean Delvaref21b75e2009-05-26 20:54:48 -07003010 u8 default_ver;
Francois Romieu0e485152007-02-20 00:00:26 +01003011} rtl_cfg_infos [] = {
3012 [RTL_CFG_0] = {
3013 .hw_start = rtl_hw_start_8169,
3014 .region = 1,
Francois Romieue9f63f32007-02-28 23:16:57 +01003015 .align = 0,
Francois Romieu0e485152007-02-20 00:00:26 +01003016 .intr_event = SYSErr | LinkChg | RxOverflow |
3017 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003018 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003019 .features = RTL_FEATURE_GMII,
3020 .default_ver = RTL_GIGA_MAC_VER_01,
Francois Romieu0e485152007-02-20 00:00:26 +01003021 },
3022 [RTL_CFG_1] = {
3023 .hw_start = rtl_hw_start_8168,
3024 .region = 2,
3025 .align = 8,
françois romieu53f57352010-11-08 13:23:05 +00003026 .intr_event = SYSErr | LinkChg | RxOverflow |
Francois Romieu0e485152007-02-20 00:00:26 +01003027 TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003028 .napi_event = TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003029 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI,
3030 .default_ver = RTL_GIGA_MAC_VER_11,
Francois Romieu0e485152007-02-20 00:00:26 +01003031 },
3032 [RTL_CFG_2] = {
3033 .hw_start = rtl_hw_start_8101,
3034 .region = 2,
3035 .align = 8,
3036 .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
3037 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02003038 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Jean Delvaref21b75e2009-05-26 20:54:48 -07003039 .features = RTL_FEATURE_MSI,
3040 .default_ver = RTL_GIGA_MAC_VER_13,
Francois Romieu0e485152007-02-20 00:00:26 +01003041 }
3042};
3043
Francois Romieufbac58f2007-10-04 22:51:38 +02003044/* Cfg9346_Unlock assumed. */
3045static unsigned rtl_try_msi(struct pci_dev *pdev, void __iomem *ioaddr,
3046 const struct rtl_cfg_info *cfg)
3047{
3048 unsigned msi = 0;
3049 u8 cfg2;
3050
3051 cfg2 = RTL_R8(Config2) & ~MSIEnable;
Francois Romieuccdffb92008-07-26 14:26:06 +02003052 if (cfg->features & RTL_FEATURE_MSI) {
Francois Romieufbac58f2007-10-04 22:51:38 +02003053 if (pci_enable_msi(pdev)) {
3054 dev_info(&pdev->dev, "no MSI. Back to INTx.\n");
3055 } else {
3056 cfg2 |= MSIEnable;
3057 msi = RTL_FEATURE_MSI;
3058 }
3059 }
3060 RTL_W8(Config2, cfg2);
3061 return msi;
3062}
3063
3064static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
3065{
3066 if (tp->features & RTL_FEATURE_MSI) {
3067 pci_disable_msi(pdev);
3068 tp->features &= ~RTL_FEATURE_MSI;
3069 }
3070}
3071
Francois Romieu8b4ab282008-11-19 22:05:25 -08003072static const struct net_device_ops rtl8169_netdev_ops = {
3073 .ndo_open = rtl8169_open,
3074 .ndo_stop = rtl8169_close,
3075 .ndo_get_stats = rtl8169_get_stats,
Stephen Hemminger00829822008-11-20 20:14:53 -08003076 .ndo_start_xmit = rtl8169_start_xmit,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003077 .ndo_tx_timeout = rtl8169_tx_timeout,
3078 .ndo_validate_addr = eth_validate_addr,
3079 .ndo_change_mtu = rtl8169_change_mtu,
Michał Mirosław350fb322011-04-08 06:35:56 +00003080 .ndo_fix_features = rtl8169_fix_features,
3081 .ndo_set_features = rtl8169_set_features,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003082 .ndo_set_mac_address = rtl_set_mac_address,
3083 .ndo_do_ioctl = rtl8169_ioctl,
3084 .ndo_set_multicast_list = rtl_set_rx_mode,
Francois Romieu8b4ab282008-11-19 22:05:25 -08003085#ifdef CONFIG_NET_POLL_CONTROLLER
3086 .ndo_poll_controller = rtl8169_netpoll,
3087#endif
3088
3089};
3090
françois romieuc0e45c12011-01-03 15:08:04 +00003091static void __devinit rtl_init_mdio_ops(struct rtl8169_private *tp)
3092{
3093 struct mdio_ops *ops = &tp->mdio_ops;
3094
3095 switch (tp->mac_version) {
3096 case RTL_GIGA_MAC_VER_27:
3097 ops->write = r8168dp_1_mdio_write;
3098 ops->read = r8168dp_1_mdio_read;
3099 break;
françois romieue6de30d2011-01-03 15:08:37 +00003100 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003101 case RTL_GIGA_MAC_VER_31:
françois romieue6de30d2011-01-03 15:08:37 +00003102 ops->write = r8168dp_2_mdio_write;
3103 ops->read = r8168dp_2_mdio_read;
3104 break;
françois romieuc0e45c12011-01-03 15:08:04 +00003105 default:
3106 ops->write = r8169_mdio_write;
3107 ops->read = r8169_mdio_read;
3108 break;
3109 }
3110}
3111
françois romieu065c27c2011-01-03 15:08:12 +00003112static void r810x_phy_power_down(struct rtl8169_private *tp)
3113{
3114 rtl_writephy(tp, 0x1f, 0x0000);
3115 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3116}
3117
3118static void r810x_phy_power_up(struct rtl8169_private *tp)
3119{
3120 rtl_writephy(tp, 0x1f, 0x0000);
3121 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3122}
3123
3124static void r810x_pll_power_down(struct rtl8169_private *tp)
3125{
3126 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3127 rtl_writephy(tp, 0x1f, 0x0000);
3128 rtl_writephy(tp, MII_BMCR, 0x0000);
3129 return;
3130 }
3131
3132 r810x_phy_power_down(tp);
3133}
3134
3135static void r810x_pll_power_up(struct rtl8169_private *tp)
3136{
3137 r810x_phy_power_up(tp);
3138}
3139
3140static void r8168_phy_power_up(struct rtl8169_private *tp)
3141{
3142 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003143 switch (tp->mac_version) {
3144 case RTL_GIGA_MAC_VER_11:
3145 case RTL_GIGA_MAC_VER_12:
3146 case RTL_GIGA_MAC_VER_17:
3147 case RTL_GIGA_MAC_VER_18:
3148 case RTL_GIGA_MAC_VER_19:
3149 case RTL_GIGA_MAC_VER_20:
3150 case RTL_GIGA_MAC_VER_21:
3151 case RTL_GIGA_MAC_VER_22:
3152 case RTL_GIGA_MAC_VER_23:
3153 case RTL_GIGA_MAC_VER_24:
3154 case RTL_GIGA_MAC_VER_25:
3155 case RTL_GIGA_MAC_VER_26:
3156 case RTL_GIGA_MAC_VER_27:
3157 case RTL_GIGA_MAC_VER_28:
3158 case RTL_GIGA_MAC_VER_31:
3159 rtl_writephy(tp, 0x0e, 0x0000);
3160 break;
3161 default:
3162 break;
3163 }
françois romieu065c27c2011-01-03 15:08:12 +00003164 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE);
3165}
3166
3167static void r8168_phy_power_down(struct rtl8169_private *tp)
3168{
3169 rtl_writephy(tp, 0x1f, 0x0000);
hayeswang01dc7fe2011-03-21 01:50:28 +00003170 switch (tp->mac_version) {
3171 case RTL_GIGA_MAC_VER_32:
3172 case RTL_GIGA_MAC_VER_33:
3173 rtl_writephy(tp, MII_BMCR, BMCR_ANENABLE | BMCR_PDOWN);
3174 break;
3175
3176 case RTL_GIGA_MAC_VER_11:
3177 case RTL_GIGA_MAC_VER_12:
3178 case RTL_GIGA_MAC_VER_17:
3179 case RTL_GIGA_MAC_VER_18:
3180 case RTL_GIGA_MAC_VER_19:
3181 case RTL_GIGA_MAC_VER_20:
3182 case RTL_GIGA_MAC_VER_21:
3183 case RTL_GIGA_MAC_VER_22:
3184 case RTL_GIGA_MAC_VER_23:
3185 case RTL_GIGA_MAC_VER_24:
3186 case RTL_GIGA_MAC_VER_25:
3187 case RTL_GIGA_MAC_VER_26:
3188 case RTL_GIGA_MAC_VER_27:
3189 case RTL_GIGA_MAC_VER_28:
3190 case RTL_GIGA_MAC_VER_31:
3191 rtl_writephy(tp, 0x0e, 0x0200);
3192 default:
3193 rtl_writephy(tp, MII_BMCR, BMCR_PDOWN);
3194 break;
3195 }
françois romieu065c27c2011-01-03 15:08:12 +00003196}
3197
3198static void r8168_pll_power_down(struct rtl8169_private *tp)
3199{
3200 void __iomem *ioaddr = tp->mmio_addr;
3201
Francois Romieucecb5fd2011-04-01 10:21:07 +02003202 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3203 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3204 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003205 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003206 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003207 }
françois romieu065c27c2011-01-03 15:08:12 +00003208
Francois Romieucecb5fd2011-04-01 10:21:07 +02003209 if ((tp->mac_version == RTL_GIGA_MAC_VER_23 ||
3210 tp->mac_version == RTL_GIGA_MAC_VER_24) &&
françois romieu065c27c2011-01-03 15:08:12 +00003211 (RTL_R16(CPlusCmd) & ASF)) {
3212 return;
3213 }
3214
hayeswang01dc7fe2011-03-21 01:50:28 +00003215 if (tp->mac_version == RTL_GIGA_MAC_VER_32 ||
3216 tp->mac_version == RTL_GIGA_MAC_VER_33)
3217 rtl_ephy_write(ioaddr, 0x19, 0xff64);
3218
françois romieu065c27c2011-01-03 15:08:12 +00003219 if (__rtl8169_get_wol(tp) & WAKE_ANY) {
3220 rtl_writephy(tp, 0x1f, 0x0000);
3221 rtl_writephy(tp, MII_BMCR, 0x0000);
3222
3223 RTL_W32(RxConfig, RTL_R32(RxConfig) |
3224 AcceptBroadcast | AcceptMulticast | AcceptMyPhys);
3225 return;
3226 }
3227
3228 r8168_phy_power_down(tp);
3229
3230 switch (tp->mac_version) {
3231 case RTL_GIGA_MAC_VER_25:
3232 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003233 case RTL_GIGA_MAC_VER_27:
3234 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003235 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003236 case RTL_GIGA_MAC_VER_32:
3237 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003238 RTL_W8(PMCH, RTL_R8(PMCH) & ~0x80);
3239 break;
3240 }
3241}
3242
3243static void r8168_pll_power_up(struct rtl8169_private *tp)
3244{
3245 void __iomem *ioaddr = tp->mmio_addr;
3246
Francois Romieucecb5fd2011-04-01 10:21:07 +02003247 if ((tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3248 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3249 tp->mac_version == RTL_GIGA_MAC_VER_31) &&
hayeswang4804b3b2011-03-21 01:50:29 +00003250 r8168dp_check_dash(tp)) {
françois romieu065c27c2011-01-03 15:08:12 +00003251 return;
Hayes Wang5d2e1952011-02-22 17:26:22 +08003252 }
françois romieu065c27c2011-01-03 15:08:12 +00003253
3254 switch (tp->mac_version) {
3255 case RTL_GIGA_MAC_VER_25:
3256 case RTL_GIGA_MAC_VER_26:
Hayes Wang5d2e1952011-02-22 17:26:22 +08003257 case RTL_GIGA_MAC_VER_27:
3258 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003259 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003260 case RTL_GIGA_MAC_VER_32:
3261 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003262 RTL_W8(PMCH, RTL_R8(PMCH) | 0x80);
3263 break;
3264 }
3265
3266 r8168_phy_power_up(tp);
3267}
3268
3269static void rtl_pll_power_op(struct rtl8169_private *tp,
3270 void (*op)(struct rtl8169_private *))
3271{
3272 if (op)
3273 op(tp);
3274}
3275
3276static void rtl_pll_power_down(struct rtl8169_private *tp)
3277{
3278 rtl_pll_power_op(tp, tp->pll_power_ops.down);
3279}
3280
3281static void rtl_pll_power_up(struct rtl8169_private *tp)
3282{
3283 rtl_pll_power_op(tp, tp->pll_power_ops.up);
3284}
3285
3286static void __devinit rtl_init_pll_power_ops(struct rtl8169_private *tp)
3287{
3288 struct pll_power_ops *ops = &tp->pll_power_ops;
3289
3290 switch (tp->mac_version) {
3291 case RTL_GIGA_MAC_VER_07:
3292 case RTL_GIGA_MAC_VER_08:
3293 case RTL_GIGA_MAC_VER_09:
3294 case RTL_GIGA_MAC_VER_10:
3295 case RTL_GIGA_MAC_VER_16:
Hayes Wang5a5e4442011-02-22 17:26:21 +08003296 case RTL_GIGA_MAC_VER_29:
3297 case RTL_GIGA_MAC_VER_30:
françois romieu065c27c2011-01-03 15:08:12 +00003298 ops->down = r810x_pll_power_down;
3299 ops->up = r810x_pll_power_up;
3300 break;
3301
3302 case RTL_GIGA_MAC_VER_11:
3303 case RTL_GIGA_MAC_VER_12:
3304 case RTL_GIGA_MAC_VER_17:
3305 case RTL_GIGA_MAC_VER_18:
3306 case RTL_GIGA_MAC_VER_19:
3307 case RTL_GIGA_MAC_VER_20:
3308 case RTL_GIGA_MAC_VER_21:
3309 case RTL_GIGA_MAC_VER_22:
3310 case RTL_GIGA_MAC_VER_23:
3311 case RTL_GIGA_MAC_VER_24:
3312 case RTL_GIGA_MAC_VER_25:
3313 case RTL_GIGA_MAC_VER_26:
3314 case RTL_GIGA_MAC_VER_27:
françois romieue6de30d2011-01-03 15:08:37 +00003315 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00003316 case RTL_GIGA_MAC_VER_31:
hayeswang01dc7fe2011-03-21 01:50:28 +00003317 case RTL_GIGA_MAC_VER_32:
3318 case RTL_GIGA_MAC_VER_33:
françois romieu065c27c2011-01-03 15:08:12 +00003319 ops->down = r8168_pll_power_down;
3320 ops->up = r8168_pll_power_up;
3321 break;
3322
3323 default:
3324 ops->down = NULL;
3325 ops->up = NULL;
3326 break;
3327 }
3328}
3329
Francois Romieu6f43adc2011-04-29 15:05:51 +02003330static void rtl_hw_reset(struct rtl8169_private *tp)
3331{
3332 void __iomem *ioaddr = tp->mmio_addr;
3333 int i;
3334
3335 /* Soft reset the chip. */
3336 RTL_W8(ChipCmd, CmdReset);
3337
3338 /* Check that the chip has finished the reset. */
3339 for (i = 0; i < 100; i++) {
3340 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
3341 break;
3342 msleep_interruptible(1);
3343 }
3344}
3345
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003346static int __devinit
3347rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
3348{
Francois Romieu0e485152007-02-20 00:00:26 +01003349 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
3350 const unsigned int region = cfg->region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 struct rtl8169_private *tp;
Francois Romieuccdffb92008-07-26 14:26:06 +02003352 struct mii_if_info *mii;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003353 struct net_device *dev;
3354 void __iomem *ioaddr;
Francois Romieu2b7b4312011-04-18 22:53:24 -07003355 int chipset, i;
Francois Romieu07d3f512007-02-21 22:40:46 +01003356 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003358 if (netif_msg_drv(&debug)) {
3359 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
3360 MODULENAME, RTL8169_VERSION);
3361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 dev = alloc_etherdev(sizeof (*tp));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003364 if (!dev) {
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003365 if (netif_msg_drv(&debug))
Jeff Garzik9b91cf92006-06-27 11:39:50 -04003366 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003367 rc = -ENOMEM;
3368 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 }
3370
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 SET_NETDEV_DEV(dev, &pdev->dev);
Francois Romieu8b4ab282008-11-19 22:05:25 -08003372 dev->netdev_ops = &rtl8169_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 tp = netdev_priv(dev);
David Howellsc4028952006-11-22 14:57:56 +00003374 tp->dev = dev;
Ivan Vecera21e197f2008-04-17 22:48:41 +02003375 tp->pci_dev = pdev;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003376 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377
Francois Romieuccdffb92008-07-26 14:26:06 +02003378 mii = &tp->mii;
3379 mii->dev = dev;
3380 mii->mdio_read = rtl_mdio_read;
3381 mii->mdio_write = rtl_mdio_write;
3382 mii->phy_id_mask = 0x1f;
3383 mii->reg_num_mask = 0x1f;
3384 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
3385
Stanislaw Gruszkaba04c7c2011-02-22 02:00:11 +00003386 /* disable ASPM completely as that cause random device stop working
3387 * problems as well as full system hangs for some PCIe devices users */
3388 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
3389 PCIE_LINK_STATE_CLKPM);
3390
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 /* enable device (incl. PCI PM wakeup and hotplug setup) */
3392 rc = pci_enable_device(pdev);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003393 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003394 netif_err(tp, probe, dev, "enable failure\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003395 goto err_out_free_dev_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 }
3397
françois romieu87aeec72010-04-26 11:42:06 +00003398 if (pci_set_mwi(pdev) < 0)
3399 netif_info(tp, probe, dev, "Mem-Wr-Inval unavailable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 /* make sure PCI base addr 1 is MMIO */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003402 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003403 netif_err(tp, probe, dev,
3404 "region #%d not an MMIO resource, aborting\n",
3405 region);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003407 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003409
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 /* check for weird/broken PCI region reporting */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003411 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003412 netif_err(tp, probe, dev,
3413 "Invalid PCI region size(s), aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 rc = -ENODEV;
françois romieu87aeec72010-04-26 11:42:06 +00003415 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 }
3417
3418 rc = pci_request_regions(pdev, MODULENAME);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003419 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003420 netif_err(tp, probe, dev, "could not request regions\n");
françois romieu87aeec72010-04-26 11:42:06 +00003421 goto err_out_mwi_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 }
3423
Hayes Wangd24e9aa2011-02-22 17:26:19 +08003424 tp->cp_cmd = RxChkSum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425
3426 if ((sizeof(dma_addr_t) > 4) &&
David S. Miller4300e8c2010-03-26 10:23:30 -07003427 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) && use_dac) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 tp->cp_cmd |= PCIDAC;
3429 dev->features |= NETIF_F_HIGHDMA;
3430 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07003431 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 if (rc < 0) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003433 netif_err(tp, probe, dev, "DMA configuration failed\n");
françois romieu87aeec72010-04-26 11:42:06 +00003434 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 }
3436 }
3437
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 /* ioremap MMIO region */
Francois Romieubcf0bf92006-07-26 23:14:13 +02003439 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003440 if (!ioaddr) {
Joe Perchesbf82c182010-02-09 11:49:50 +00003441 netif_err(tp, probe, dev, "cannot remap MMIO, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 rc = -EIO;
françois romieu87aeec72010-04-26 11:42:06 +00003443 goto err_out_free_res_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 }
Francois Romieu6f43adc2011-04-29 15:05:51 +02003445 tp->mmio_addr = ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446
David S. Miller4300e8c2010-03-26 10:23:30 -07003447 tp->pcie_cap = pci_find_capability(pdev, PCI_CAP_ID_EXP);
3448 if (!tp->pcie_cap)
3449 netif_info(tp, probe, dev, "no PCI Express capability\n");
3450
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003451 RTL_W16(IntrMask, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452
Francois Romieu6f43adc2011-04-29 15:05:51 +02003453 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454
Karsten Wiesed78ad8c2009-04-02 01:06:01 -07003455 RTL_W16(IntrStatus, 0xffff);
3456
françois romieuca52efd2009-07-24 12:34:19 +00003457 pci_set_master(pdev);
3458
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 /* Identify chip attached to board */
Francois Romieu5d320a22011-05-08 17:47:36 +02003460 rtl8169_get_mac_version(tp, dev, cfg->default_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461
Francois Romieu7a8fc772011-03-01 17:18:33 +01003462 /*
3463 * Pretend we are using VLANs; This bypasses a nasty bug where
3464 * Interrupts stop flowing on high load on 8110SCd controllers.
3465 */
3466 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3467 tp->cp_cmd |= RxVlan;
3468
françois romieuc0e45c12011-01-03 15:08:04 +00003469 rtl_init_mdio_ops(tp);
françois romieu065c27c2011-01-03 15:08:12 +00003470 rtl_init_pll_power_ops(tp);
françois romieuc0e45c12011-01-03 15:08:04 +00003471
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 rtl8169_print_mac_version(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473
Francois Romieu85bffe62011-04-27 08:22:39 +02003474 chipset = tp->mac_version;
3475 tp->txd_version = rtl_chip_infos[chipset].txd_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476
Francois Romieu5d06a992006-02-23 00:47:58 +01003477 RTL_W8(Cfg9346, Cfg9346_Unlock);
3478 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
3479 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
Bruno Prémont20037fa2008-10-08 17:05:03 -07003480 if ((RTL_R8(Config3) & (LinkUp | MagicPacket)) != 0)
3481 tp->features |= RTL_FEATURE_WOL;
3482 if ((RTL_R8(Config5) & (UWF | BWF | MWF)) != 0)
3483 tp->features |= RTL_FEATURE_WOL;
Francois Romieufbac58f2007-10-04 22:51:38 +02003484 tp->features |= rtl_try_msi(pdev, ioaddr, cfg);
Francois Romieu5d06a992006-02-23 00:47:58 +01003485 RTL_W8(Cfg9346, Cfg9346_Lock);
3486
Francois Romieu66ec5d42007-11-06 22:56:10 +01003487 if ((tp->mac_version <= RTL_GIGA_MAC_VER_06) &&
3488 (RTL_R8(PHYstatus) & TBI_Enable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 tp->set_speed = rtl8169_set_speed_tbi;
3490 tp->get_settings = rtl8169_gset_tbi;
3491 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
3492 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
3493 tp->link_ok = rtl8169_tbi_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003494 tp->do_ioctl = rtl_tbi_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 } else {
3496 tp->set_speed = rtl8169_set_speed_xmii;
3497 tp->get_settings = rtl8169_gset_xmii;
3498 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
3499 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
3500 tp->link_ok = rtl8169_xmii_link_ok;
Francois Romieu8b4ab282008-11-19 22:05:25 -08003501 tp->do_ioctl = rtl_xmii_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 }
3503
Francois Romieudf58ef512008-10-09 14:35:58 -07003504 spin_lock_init(&tp->lock);
3505
Ivan Vecera7bf6bf42008-09-23 22:46:29 +00003506 /* Get MAC address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 for (i = 0; i < MAC_ADDR_LEN; i++)
3508 dev->dev_addr[i] = RTL_R8(MAC0 + i);
John W. Linville6d6525b2005-09-12 10:48:57 -04003509 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
3513 dev->irq = pdev->irq;
3514 dev->base_addr = (unsigned long) ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003516 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517
Michał Mirosław350fb322011-04-08 06:35:56 +00003518 /* don't enable SG, IP_CSUM and TSO by default - it might not work
3519 * properly for all devices */
3520 dev->features |= NETIF_F_RXCSUM |
3521 NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3522
3523 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3524 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
3525 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3526 NETIF_F_HIGHDMA;
3527
3528 if (tp->mac_version == RTL_GIGA_MAC_VER_05)
3529 /* 8110SCd requires hardware Rx VLAN - disallow toggling */
3530 dev->hw_features &= ~NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531
3532 tp->intr_mask = 0xffff;
Francois Romieu0e485152007-02-20 00:00:26 +01003533 tp->hw_start = cfg->hw_start;
3534 tp->intr_event = cfg->intr_event;
3535 tp->napi_event = cfg->napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536
Francois Romieu2efa53f2007-03-09 00:00:05 +01003537 init_timer(&tp->timer);
3538 tp->timer.data = (unsigned long) dev;
3539 tp->timer.function = rtl8169_phy_timer;
3540
Francois Romieub6ffd972011-06-17 17:00:05 +02003541 tp->rtl_fw = RTL_FIRMWARE_UNKNOWN;
François Romieu953a12c2011-04-24 17:38:48 +02003542
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 rc = register_netdev(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003544 if (rc < 0)
françois romieu87aeec72010-04-26 11:42:06 +00003545 goto err_out_msi_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546
3547 pci_set_drvdata(pdev, dev);
3548
Joe Perchesbf82c182010-02-09 11:49:50 +00003549 netif_info(tp, probe, dev, "%s at 0x%lx, %pM, XID %08x IRQ %d\n",
Francois Romieu85bffe62011-04-27 08:22:39 +02003550 rtl_chip_infos[chipset].name, dev->base_addr, dev->dev_addr,
Joe Perchesbf82c182010-02-09 11:49:50 +00003551 (u32)(RTL_R32(TxConfig) & 0x9cf0f8ff), dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552
Francois Romieucecb5fd2011-04-01 10:21:07 +02003553 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3554 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3555 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00003556 rtl8168_driver_start(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003557 }
françois romieub646d902011-01-03 15:08:21 +00003558
Bruno Prémont8b76ab32008-10-08 17:06:25 -07003559 device_set_wakeup_enable(&pdev->dev, tp->features & RTL_FEATURE_WOL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560
Alan Sternf3ec4f82010-06-08 15:23:51 -04003561 if (pci_dev_run_wake(pdev))
3562 pm_runtime_put_noidle(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003563
Ivan Vecera0d672e92011-02-15 02:08:39 +00003564 netif_carrier_off(dev);
3565
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003566out:
3567 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568
françois romieu87aeec72010-04-26 11:42:06 +00003569err_out_msi_4:
Francois Romieufbac58f2007-10-04 22:51:38 +02003570 rtl_disable_msi(pdev, tp);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003571 iounmap(ioaddr);
françois romieu87aeec72010-04-26 11:42:06 +00003572err_out_free_res_3:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003573 pci_release_regions(pdev);
françois romieu87aeec72010-04-26 11:42:06 +00003574err_out_mwi_2:
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003575 pci_clear_mwi(pdev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02003576 pci_disable_device(pdev);
3577err_out_free_dev_1:
3578 free_netdev(dev);
3579 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580}
3581
Francois Romieu07d3f512007-02-21 22:40:46 +01003582static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583{
3584 struct net_device *dev = pci_get_drvdata(pdev);
3585 struct rtl8169_private *tp = netdev_priv(dev);
3586
Francois Romieucecb5fd2011-04-01 10:21:07 +02003587 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
3588 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3589 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieub646d902011-01-03 15:08:21 +00003590 rtl8168_driver_stop(tp);
françois romieue6de30d2011-01-03 15:08:37 +00003591 }
françois romieub646d902011-01-03 15:08:21 +00003592
Tejun Heo23f333a2010-12-12 16:45:14 +01003593 cancel_delayed_work_sync(&tp->task);
Francois Romieueb2a0212007-02-15 23:37:21 +01003594
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 unregister_netdev(dev);
Ivan Veceracc098dc2009-11-29 23:12:52 -08003596
François Romieu953a12c2011-04-24 17:38:48 +02003597 rtl_release_firmware(tp);
3598
Alan Sternf3ec4f82010-06-08 15:23:51 -04003599 if (pci_dev_run_wake(pdev))
3600 pm_runtime_get_noresume(&pdev->dev);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003601
Ivan Veceracc098dc2009-11-29 23:12:52 -08003602 /* restore original MAC address */
3603 rtl_rar_set(tp, dev->perm_addr);
3604
Francois Romieufbac58f2007-10-04 22:51:38 +02003605 rtl_disable_msi(pdev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 rtl8169_release_board(pdev, dev, tp->mmio_addr);
3607 pci_set_drvdata(pdev, NULL);
3608}
3609
Francois Romieub6ffd972011-06-17 17:00:05 +02003610static void rtl_request_uncached_firmware(struct rtl8169_private *tp)
3611{
3612 struct rtl_fw *rtl_fw;
3613 const char *name;
3614 int rc = -ENOMEM;
3615
3616 name = rtl_lookup_firmware_name(tp);
3617 if (!name)
3618 goto out_no_firmware;
3619
3620 rtl_fw = kzalloc(sizeof(*rtl_fw), GFP_KERNEL);
3621 if (!rtl_fw)
3622 goto err_warn;
3623
3624 rc = request_firmware(&rtl_fw->fw, name, &tp->pci_dev->dev);
3625 if (rc < 0)
3626 goto err_free;
3627
Francois Romieufd112f22011-06-18 00:10:29 +02003628 rc = rtl_check_firmware(tp, rtl_fw);
3629 if (rc < 0)
3630 goto err_release_firmware;
3631
Francois Romieub6ffd972011-06-17 17:00:05 +02003632 tp->rtl_fw = rtl_fw;
3633out:
3634 return;
3635
Francois Romieufd112f22011-06-18 00:10:29 +02003636err_release_firmware:
3637 release_firmware(rtl_fw->fw);
Francois Romieub6ffd972011-06-17 17:00:05 +02003638err_free:
3639 kfree(rtl_fw);
3640err_warn:
3641 netif_warn(tp, ifup, tp->dev, "unable to load firmware patch %s (%d)\n",
3642 name, rc);
3643out_no_firmware:
3644 tp->rtl_fw = NULL;
3645 goto out;
3646}
3647
François Romieu953a12c2011-04-24 17:38:48 +02003648static void rtl_request_firmware(struct rtl8169_private *tp)
3649{
Francois Romieub6ffd972011-06-17 17:00:05 +02003650 if (IS_ERR(tp->rtl_fw))
3651 rtl_request_uncached_firmware(tp);
François Romieu953a12c2011-04-24 17:38:48 +02003652}
3653
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654static int rtl8169_open(struct net_device *dev)
3655{
3656 struct rtl8169_private *tp = netdev_priv(dev);
françois romieueee3a962011-01-08 02:17:26 +00003657 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu99f252b2007-04-02 22:59:59 +02003659 int retval = -ENOMEM;
3660
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003661 pm_runtime_get_sync(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662
Neil Hormanc0cd8842010-03-29 13:16:02 -07003663 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 * Rx and Tx desscriptors needs 256 bytes alignment.
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003665 * dma_alloc_coherent provides more.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 */
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003667 tp->TxDescArray = dma_alloc_coherent(&pdev->dev, R8169_TX_RING_BYTES,
3668 &tp->TxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 if (!tp->TxDescArray)
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003670 goto err_pm_runtime_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003672 tp->RxDescArray = dma_alloc_coherent(&pdev->dev, R8169_RX_RING_BYTES,
3673 &tp->RxPhyAddr, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 if (!tp->RxDescArray)
Francois Romieu99f252b2007-04-02 22:59:59 +02003675 goto err_free_tx_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676
3677 retval = rtl8169_init_ring(dev);
3678 if (retval < 0)
Francois Romieu99f252b2007-04-02 22:59:59 +02003679 goto err_free_rx_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680
David Howellsc4028952006-11-22 14:57:56 +00003681 INIT_DELAYED_WORK(&tp->task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682
Francois Romieu99f252b2007-04-02 22:59:59 +02003683 smp_mb();
3684
François Romieu953a12c2011-04-24 17:38:48 +02003685 rtl_request_firmware(tp);
3686
Francois Romieufbac58f2007-10-04 22:51:38 +02003687 retval = request_irq(dev->irq, rtl8169_interrupt,
3688 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
Francois Romieu99f252b2007-04-02 22:59:59 +02003689 dev->name, dev);
3690 if (retval < 0)
François Romieu953a12c2011-04-24 17:38:48 +02003691 goto err_release_fw_2;
Francois Romieu99f252b2007-04-02 22:59:59 +02003692
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003693 napi_enable(&tp->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003694
françois romieueee3a962011-01-08 02:17:26 +00003695 rtl8169_init_phy(dev, tp);
3696
Michał Mirosław350fb322011-04-08 06:35:56 +00003697 rtl8169_set_features(dev, dev->features);
françois romieueee3a962011-01-08 02:17:26 +00003698
françois romieu065c27c2011-01-03 15:08:12 +00003699 rtl_pll_power_up(tp);
3700
Francois Romieu07ce4062007-02-23 23:36:39 +01003701 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003703 tp->saved_wolopts = 0;
3704 pm_runtime_put_noidle(&pdev->dev);
3705
françois romieueee3a962011-01-08 02:17:26 +00003706 rtl8169_check_link_status(dev, tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707out:
3708 return retval;
3709
François Romieu953a12c2011-04-24 17:38:48 +02003710err_release_fw_2:
3711 rtl_release_firmware(tp);
Francois Romieu99f252b2007-04-02 22:59:59 +02003712 rtl8169_rx_clear(tp);
3713err_free_rx_1:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003714 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
3715 tp->RxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003716 tp->RxDescArray = NULL;
Francois Romieu99f252b2007-04-02 22:59:59 +02003717err_free_tx_0:
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00003718 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
3719 tp->TxPhyAddr);
Rafael J. Wysockie1759442010-03-14 14:33:51 +00003720 tp->TxDescArray = NULL;
3721err_pm_runtime_put:
3722 pm_runtime_put_noidle(&pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 goto out;
3724}
3725
françois romieue6de30d2011-01-03 15:08:37 +00003726static void rtl8169_hw_reset(struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727{
françois romieue6de30d2011-01-03 15:08:37 +00003728 void __iomem *ioaddr = tp->mmio_addr;
3729
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 /* Disable interrupts */
3731 rtl8169_irq_mask_and_ack(ioaddr);
3732
Hayes Wang5d2e1952011-02-22 17:26:22 +08003733 if (tp->mac_version == RTL_GIGA_MAC_VER_27 ||
hayeswang4804b3b2011-03-21 01:50:29 +00003734 tp->mac_version == RTL_GIGA_MAC_VER_28 ||
3735 tp->mac_version == RTL_GIGA_MAC_VER_31) {
françois romieue6de30d2011-01-03 15:08:37 +00003736 while (RTL_R8(TxPoll) & NPQ)
3737 udelay(20);
3738
3739 }
3740
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 /* Reset the chipset */
3742 RTL_W8(ChipCmd, CmdReset);
3743
3744 /* PCI commit */
3745 RTL_R8(ChipCmd);
3746}
3747
Francois Romieu7f796d832007-06-11 23:04:41 +02003748static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01003749{
3750 void __iomem *ioaddr = tp->mmio_addr;
3751 u32 cfg = rtl8169_rx_config;
3752
Francois Romieu2b7b4312011-04-18 22:53:24 -07003753 cfg |= (RTL_R32(RxConfig) & RTL_RX_CONFIG_MASK);
Francois Romieu9cb427b2006-11-02 00:10:16 +01003754 RTL_W32(RxConfig, cfg);
3755
3756 /* Set DMA burst size and Interframe Gap Time */
3757 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
3758 (InterFrameGap << TxInterFrameGapShift));
3759}
3760
Francois Romieu07ce4062007-02-23 23:36:39 +01003761static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762{
3763 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764
Francois Romieu6f43adc2011-04-29 15:05:51 +02003765 rtl_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766
Francois Romieu07ce4062007-02-23 23:36:39 +01003767 tp->hw_start(dev);
3768
Francois Romieu07ce4062007-02-23 23:36:39 +01003769 netif_start_queue(dev);
3770}
3771
Francois Romieu7f796d832007-06-11 23:04:41 +02003772static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
3773 void __iomem *ioaddr)
3774{
3775 /*
3776 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
3777 * register to be written before TxDescAddrLow to work.
3778 * Switching from MMIO to I/O access fixes the issue as well.
3779 */
3780 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07003781 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02003782 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
Yang Hongyang284901a2009-04-06 19:01:15 -07003783 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_BIT_MASK(32));
Francois Romieu7f796d832007-06-11 23:04:41 +02003784}
3785
3786static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
3787{
3788 u16 cmd;
3789
3790 cmd = RTL_R16(CPlusCmd);
3791 RTL_W16(CPlusCmd, cmd);
3792 return cmd;
3793}
3794
Eric Dumazetfdd7b4c2009-06-09 04:01:02 -07003795static void rtl_set_rx_max_size(void __iomem *ioaddr, unsigned int rx_buf_sz)
Francois Romieu7f796d832007-06-11 23:04:41 +02003796{
3797 /* Low hurts. Let's disable the filtering. */
Raimonds Cicans207d6e872009-10-26 10:52:37 +00003798 RTL_W16(RxMaxSize, rx_buf_sz + 1);
Francois Romieu7f796d832007-06-11 23:04:41 +02003799}
3800
Francois Romieu6dccd162007-02-13 23:38:05 +01003801static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
3802{
Francois Romieu37441002011-06-17 22:58:54 +02003803 static const struct rtl_cfg2_info {
Francois Romieu6dccd162007-02-13 23:38:05 +01003804 u32 mac_version;
3805 u32 clk;
3806 u32 val;
3807 } cfg2_info [] = {
3808 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
3809 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
3810 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
3811 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
Francois Romieu37441002011-06-17 22:58:54 +02003812 };
3813 const struct rtl_cfg2_info *p = cfg2_info;
Francois Romieu6dccd162007-02-13 23:38:05 +01003814 unsigned int i;
3815 u32 clk;
3816
3817 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01003818 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01003819 if ((p->mac_version == mac_version) && (p->clk == clk)) {
3820 RTL_W32(0x7c, p->val);
3821 break;
3822 }
3823 }
3824}
3825
Francois Romieu07ce4062007-02-23 23:36:39 +01003826static void rtl_hw_start_8169(struct net_device *dev)
3827{
3828 struct rtl8169_private *tp = netdev_priv(dev);
3829 void __iomem *ioaddr = tp->mmio_addr;
3830 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01003831
Francois Romieu9cb427b2006-11-02 00:10:16 +01003832 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
3833 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
3834 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
3835 }
3836
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieucecb5fd2011-04-01 10:21:07 +02003838 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
3839 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
3840 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
3841 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieu9cb427b2006-11-02 00:10:16 +01003842 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3843
françois romieuf0298f82011-01-03 15:07:42 +00003844 RTL_W8(EarlyTxThres, NoEarlyTx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845
Eric Dumazet6f0333b2010-10-11 11:17:47 +00003846 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847
Francois Romieucecb5fd2011-04-01 10:21:07 +02003848 if (tp->mac_version == RTL_GIGA_MAC_VER_01 ||
3849 tp->mac_version == RTL_GIGA_MAC_VER_02 ||
3850 tp->mac_version == RTL_GIGA_MAC_VER_03 ||
3851 tp->mac_version == RTL_GIGA_MAC_VER_04)
Francois Romieuc946b302007-10-04 00:42:50 +02003852 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853
Francois Romieu7f796d832007-06-11 23:04:41 +02003854 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02003855
Francois Romieucecb5fd2011-04-01 10:21:07 +02003856 if (tp->mac_version == RTL_GIGA_MAC_VER_02 ||
3857 tp->mac_version == RTL_GIGA_MAC_VER_03) {
Joe Perches06fa7352007-10-18 21:15:00 +02003858 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02003860 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 }
3862
Francois Romieubcf0bf92006-07-26 23:14:13 +02003863 RTL_W16(CPlusCmd, tp->cp_cmd);
3864
Francois Romieu6dccd162007-02-13 23:38:05 +01003865 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
3866
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 /*
3868 * Undocumented corner. Supposedly:
3869 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
3870 */
3871 RTL_W16(IntrMitigate, 0x0000);
3872
Francois Romieu7f796d832007-06-11 23:04:41 +02003873 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01003874
Francois Romieucecb5fd2011-04-01 10:21:07 +02003875 if (tp->mac_version != RTL_GIGA_MAC_VER_01 &&
3876 tp->mac_version != RTL_GIGA_MAC_VER_02 &&
3877 tp->mac_version != RTL_GIGA_MAC_VER_03 &&
3878 tp->mac_version != RTL_GIGA_MAC_VER_04) {
Francois Romieuc946b302007-10-04 00:42:50 +02003879 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
3880 rtl_set_rx_tx_config_registers(tp);
3881 }
3882
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02003884
3885 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
3886 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887
3888 RTL_W32(RxMissed, 0);
3889
Francois Romieu07ce4062007-02-23 23:36:39 +01003890 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891
3892 /* no early-rx interrupts */
3893 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01003894
3895 /* Enable all known interrupts by setting the interrupt mask. */
Francois Romieu0e485152007-02-20 00:00:26 +01003896 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01003897}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898
Francois Romieu9c14cea2008-07-05 00:21:15 +02003899static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
Francois Romieu458a9f62008-08-02 15:50:02 +02003900{
Francois Romieu9c14cea2008-07-05 00:21:15 +02003901 struct net_device *dev = pci_get_drvdata(pdev);
3902 struct rtl8169_private *tp = netdev_priv(dev);
3903 int cap = tp->pcie_cap;
Francois Romieu458a9f62008-08-02 15:50:02 +02003904
Francois Romieu9c14cea2008-07-05 00:21:15 +02003905 if (cap) {
3906 u16 ctl;
3907
3908 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
3909 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
3910 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
3911 }
Francois Romieu458a9f62008-08-02 15:50:02 +02003912}
3913
françois romieu650e8d52011-01-03 15:08:29 +00003914static void rtl_csi_access_enable(void __iomem *ioaddr, u32 bits)
Francois Romieudacf8152008-08-02 20:44:13 +02003915{
3916 u32 csi;
3917
3918 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
françois romieu650e8d52011-01-03 15:08:29 +00003919 rtl_csi_write(ioaddr, 0x070c, csi | bits);
3920}
3921
françois romieue6de30d2011-01-03 15:08:37 +00003922static void rtl_csi_access_enable_1(void __iomem *ioaddr)
3923{
3924 rtl_csi_access_enable(ioaddr, 0x17000000);
3925}
3926
françois romieu650e8d52011-01-03 15:08:29 +00003927static void rtl_csi_access_enable_2(void __iomem *ioaddr)
3928{
3929 rtl_csi_access_enable(ioaddr, 0x27000000);
Francois Romieudacf8152008-08-02 20:44:13 +02003930}
3931
3932struct ephy_info {
3933 unsigned int offset;
3934 u16 mask;
3935 u16 bits;
3936};
3937
Alexey Dobriyan350f7592009-11-25 15:54:21 -08003938static void rtl_ephy_init(void __iomem *ioaddr, const struct ephy_info *e, int len)
Francois Romieudacf8152008-08-02 20:44:13 +02003939{
3940 u16 w;
3941
3942 while (len-- > 0) {
3943 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
3944 rtl_ephy_write(ioaddr, e->offset, w);
3945 e++;
3946 }
3947}
3948
Francois Romieub726e492008-06-28 12:22:59 +02003949static void rtl_disable_clock_request(struct pci_dev *pdev)
3950{
3951 struct net_device *dev = pci_get_drvdata(pdev);
3952 struct rtl8169_private *tp = netdev_priv(dev);
3953 int cap = tp->pcie_cap;
3954
3955 if (cap) {
3956 u16 ctl;
3957
3958 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
3959 ctl &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
3960 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
3961 }
3962}
3963
françois romieue6de30d2011-01-03 15:08:37 +00003964static void rtl_enable_clock_request(struct pci_dev *pdev)
3965{
3966 struct net_device *dev = pci_get_drvdata(pdev);
3967 struct rtl8169_private *tp = netdev_priv(dev);
3968 int cap = tp->pcie_cap;
3969
3970 if (cap) {
3971 u16 ctl;
3972
3973 pci_read_config_word(pdev, cap + PCI_EXP_LNKCTL, &ctl);
3974 ctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
3975 pci_write_config_word(pdev, cap + PCI_EXP_LNKCTL, ctl);
3976 }
3977}
3978
Francois Romieub726e492008-06-28 12:22:59 +02003979#define R8168_CPCMD_QUIRK_MASK (\
3980 EnableBist | \
3981 Mac_dbgo_oe | \
3982 Force_half_dup | \
3983 Force_rxflow_en | \
3984 Force_txflow_en | \
3985 Cxpl_dbg_sel | \
3986 ASF | \
3987 PktCntrDisable | \
3988 Mac_dbgo_sel)
3989
Francois Romieu219a1e92008-06-28 11:58:39 +02003990static void rtl_hw_start_8168bb(void __iomem *ioaddr, struct pci_dev *pdev)
3991{
Francois Romieub726e492008-06-28 12:22:59 +02003992 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
3993
3994 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
3995
Francois Romieu2e68ae42008-06-28 12:00:55 +02003996 rtl_tx_performance_tweak(pdev,
3997 (0x5 << MAX_READ_REQUEST_SHIFT) | PCI_EXP_DEVCTL_NOSNOOP_EN);
Francois Romieu219a1e92008-06-28 11:58:39 +02003998}
3999
4000static void rtl_hw_start_8168bef(void __iomem *ioaddr, struct pci_dev *pdev)
4001{
4002 rtl_hw_start_8168bb(ioaddr, pdev);
Francois Romieub726e492008-06-28 12:22:59 +02004003
françois romieuf0298f82011-01-03 15:07:42 +00004004 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieub726e492008-06-28 12:22:59 +02004005
4006 RTL_W8(Config4, RTL_R8(Config4) & ~(1 << 0));
Francois Romieu219a1e92008-06-28 11:58:39 +02004007}
4008
4009static void __rtl_hw_start_8168cp(void __iomem *ioaddr, struct pci_dev *pdev)
4010{
Francois Romieub726e492008-06-28 12:22:59 +02004011 RTL_W8(Config1, RTL_R8(Config1) | Speed_down);
4012
4013 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4014
Francois Romieu219a1e92008-06-28 11:58:39 +02004015 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieub726e492008-06-28 12:22:59 +02004016
4017 rtl_disable_clock_request(pdev);
4018
4019 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
Francois Romieu219a1e92008-06-28 11:58:39 +02004020}
4021
Francois Romieuef3386f2008-06-29 12:24:30 +02004022static void rtl_hw_start_8168cp_1(void __iomem *ioaddr, struct pci_dev *pdev)
Francois Romieu219a1e92008-06-28 11:58:39 +02004023{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004024 static const struct ephy_info e_info_8168cp[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004025 { 0x01, 0, 0x0001 },
4026 { 0x02, 0x0800, 0x1000 },
4027 { 0x03, 0, 0x0042 },
4028 { 0x06, 0x0080, 0x0000 },
4029 { 0x07, 0, 0x2000 }
4030 };
4031
françois romieu650e8d52011-01-03 15:08:29 +00004032 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004033
4034 rtl_ephy_init(ioaddr, e_info_8168cp, ARRAY_SIZE(e_info_8168cp));
4035
Francois Romieu219a1e92008-06-28 11:58:39 +02004036 __rtl_hw_start_8168cp(ioaddr, pdev);
4037}
4038
Francois Romieuef3386f2008-06-29 12:24:30 +02004039static void rtl_hw_start_8168cp_2(void __iomem *ioaddr, struct pci_dev *pdev)
4040{
françois romieu650e8d52011-01-03 15:08:29 +00004041 rtl_csi_access_enable_2(ioaddr);
Francois Romieuef3386f2008-06-29 12:24:30 +02004042
4043 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4044
4045 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4046
4047 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4048}
4049
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004050static void rtl_hw_start_8168cp_3(void __iomem *ioaddr, struct pci_dev *pdev)
4051{
françois romieu650e8d52011-01-03 15:08:29 +00004052 rtl_csi_access_enable_2(ioaddr);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004053
4054 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4055
4056 /* Magic. */
4057 RTL_W8(DBG_REG, 0x20);
4058
françois romieuf0298f82011-01-03 15:07:42 +00004059 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004060
4061 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4062
4063 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4064}
4065
Francois Romieu219a1e92008-06-28 11:58:39 +02004066static void rtl_hw_start_8168c_1(void __iomem *ioaddr, struct pci_dev *pdev)
4067{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004068 static const struct ephy_info e_info_8168c_1[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004069 { 0x02, 0x0800, 0x1000 },
4070 { 0x03, 0, 0x0002 },
4071 { 0x06, 0x0080, 0x0000 }
4072 };
4073
françois romieu650e8d52011-01-03 15:08:29 +00004074 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004075
4076 RTL_W8(DBG_REG, 0x06 | FIX_NAK_1 | FIX_NAK_2);
4077
4078 rtl_ephy_init(ioaddr, e_info_8168c_1, ARRAY_SIZE(e_info_8168c_1));
4079
Francois Romieu219a1e92008-06-28 11:58:39 +02004080 __rtl_hw_start_8168cp(ioaddr, pdev);
4081}
4082
4083static void rtl_hw_start_8168c_2(void __iomem *ioaddr, struct pci_dev *pdev)
4084{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004085 static const struct ephy_info e_info_8168c_2[] = {
Francois Romieub726e492008-06-28 12:22:59 +02004086 { 0x01, 0, 0x0001 },
4087 { 0x03, 0x0400, 0x0220 }
4088 };
4089
françois romieu650e8d52011-01-03 15:08:29 +00004090 rtl_csi_access_enable_2(ioaddr);
Francois Romieub726e492008-06-28 12:22:59 +02004091
4092 rtl_ephy_init(ioaddr, e_info_8168c_2, ARRAY_SIZE(e_info_8168c_2));
4093
Francois Romieu219a1e92008-06-28 11:58:39 +02004094 __rtl_hw_start_8168cp(ioaddr, pdev);
4095}
4096
Francois Romieu197ff762008-06-28 13:16:02 +02004097static void rtl_hw_start_8168c_3(void __iomem *ioaddr, struct pci_dev *pdev)
4098{
4099 rtl_hw_start_8168c_2(ioaddr, pdev);
4100}
4101
Francois Romieu6fb07052008-06-29 11:54:28 +02004102static void rtl_hw_start_8168c_4(void __iomem *ioaddr, struct pci_dev *pdev)
4103{
françois romieu650e8d52011-01-03 15:08:29 +00004104 rtl_csi_access_enable_2(ioaddr);
Francois Romieu6fb07052008-06-29 11:54:28 +02004105
4106 __rtl_hw_start_8168cp(ioaddr, pdev);
4107}
4108
Francois Romieu5b538df2008-07-20 16:22:45 +02004109static void rtl_hw_start_8168d(void __iomem *ioaddr, struct pci_dev *pdev)
4110{
françois romieu650e8d52011-01-03 15:08:29 +00004111 rtl_csi_access_enable_2(ioaddr);
Francois Romieu5b538df2008-07-20 16:22:45 +02004112
4113 rtl_disable_clock_request(pdev);
4114
françois romieuf0298f82011-01-03 15:07:42 +00004115 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu5b538df2008-07-20 16:22:45 +02004116
4117 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4118
4119 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) & ~R8168_CPCMD_QUIRK_MASK);
4120}
4121
hayeswang4804b3b2011-03-21 01:50:29 +00004122static void rtl_hw_start_8168dp(void __iomem *ioaddr, struct pci_dev *pdev)
4123{
4124 rtl_csi_access_enable_1(ioaddr);
4125
4126 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4127
4128 RTL_W8(MaxTxPacketSize, TxPacketMax);
4129
4130 rtl_disable_clock_request(pdev);
4131}
4132
françois romieue6de30d2011-01-03 15:08:37 +00004133static void rtl_hw_start_8168d_4(void __iomem *ioaddr, struct pci_dev *pdev)
4134{
4135 static const struct ephy_info e_info_8168d_4[] = {
4136 { 0x0b, ~0, 0x48 },
4137 { 0x19, 0x20, 0x50 },
4138 { 0x0c, ~0, 0x20 }
4139 };
4140 int i;
4141
4142 rtl_csi_access_enable_1(ioaddr);
4143
4144 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4145
4146 RTL_W8(MaxTxPacketSize, TxPacketMax);
4147
4148 for (i = 0; i < ARRAY_SIZE(e_info_8168d_4); i++) {
4149 const struct ephy_info *e = e_info_8168d_4 + i;
4150 u16 w;
4151
4152 w = rtl_ephy_read(ioaddr, e->offset);
4153 rtl_ephy_write(ioaddr, 0x03, (w & e->mask) | e->bits);
4154 }
4155
4156 rtl_enable_clock_request(pdev);
4157}
4158
hayeswang01dc7fe2011-03-21 01:50:28 +00004159static void rtl_hw_start_8168e(void __iomem *ioaddr, struct pci_dev *pdev)
4160{
4161 static const struct ephy_info e_info_8168e[] = {
4162 { 0x00, 0x0200, 0x0100 },
4163 { 0x00, 0x0000, 0x0004 },
4164 { 0x06, 0x0002, 0x0001 },
4165 { 0x06, 0x0000, 0x0030 },
4166 { 0x07, 0x0000, 0x2000 },
4167 { 0x00, 0x0000, 0x0020 },
4168 { 0x03, 0x5800, 0x2000 },
4169 { 0x03, 0x0000, 0x0001 },
4170 { 0x01, 0x0800, 0x1000 },
4171 { 0x07, 0x0000, 0x4000 },
4172 { 0x1e, 0x0000, 0x2000 },
4173 { 0x19, 0xffff, 0xfe6c },
4174 { 0x0a, 0x0000, 0x0040 }
4175 };
4176
4177 rtl_csi_access_enable_2(ioaddr);
4178
4179 rtl_ephy_init(ioaddr, e_info_8168e, ARRAY_SIZE(e_info_8168e));
4180
4181 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4182
4183 RTL_W8(MaxTxPacketSize, TxPacketMax);
4184
4185 rtl_disable_clock_request(pdev);
4186
4187 /* Reset tx FIFO pointer */
Francois Romieucecb5fd2011-04-01 10:21:07 +02004188 RTL_W32(MISC, RTL_R32(MISC) | TXPLA_RST);
4189 RTL_W32(MISC, RTL_R32(MISC) & ~TXPLA_RST);
hayeswang01dc7fe2011-03-21 01:50:28 +00004190
Francois Romieucecb5fd2011-04-01 10:21:07 +02004191 RTL_W8(Config5, RTL_R8(Config5) & ~Spi_en);
hayeswang01dc7fe2011-03-21 01:50:28 +00004192}
4193
Francois Romieu07ce4062007-02-23 23:36:39 +01004194static void rtl_hw_start_8168(struct net_device *dev)
4195{
Francois Romieu2dd99532007-06-11 23:22:52 +02004196 struct rtl8169_private *tp = netdev_priv(dev);
4197 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01004198 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu2dd99532007-06-11 23:22:52 +02004199
4200 RTL_W8(Cfg9346, Cfg9346_Unlock);
4201
françois romieuf0298f82011-01-03 15:07:42 +00004202 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieu2dd99532007-06-11 23:22:52 +02004203
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004204 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieu2dd99532007-06-11 23:22:52 +02004205
Francois Romieu0e485152007-02-20 00:00:26 +01004206 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02004207
4208 RTL_W16(CPlusCmd, tp->cp_cmd);
4209
Francois Romieu0e485152007-02-20 00:00:26 +01004210 RTL_W16(IntrMitigate, 0x5151);
4211
4212 /* Work around for RxFIFO overflow. */
Ivan Vecerab5ba6d12011-01-27 12:24:11 +01004213 if (tp->mac_version == RTL_GIGA_MAC_VER_11 ||
4214 tp->mac_version == RTL_GIGA_MAC_VER_22) {
Francois Romieu0e485152007-02-20 00:00:26 +01004215 tp->intr_event |= RxFIFOOver | PCSTimeout;
4216 tp->intr_event &= ~RxOverflow;
4217 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004218
4219 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4220
Francois Romieub8363902008-06-01 12:31:57 +02004221 rtl_set_rx_mode(dev);
4222
4223 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
4224 (InterFrameGap << TxInterFrameGapShift));
Francois Romieu2dd99532007-06-11 23:22:52 +02004225
4226 RTL_R8(IntrMask);
4227
Francois Romieu219a1e92008-06-28 11:58:39 +02004228 switch (tp->mac_version) {
4229 case RTL_GIGA_MAC_VER_11:
4230 rtl_hw_start_8168bb(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004231 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004232
4233 case RTL_GIGA_MAC_VER_12:
4234 case RTL_GIGA_MAC_VER_17:
4235 rtl_hw_start_8168bef(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004236 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004237
4238 case RTL_GIGA_MAC_VER_18:
Francois Romieuef3386f2008-06-29 12:24:30 +02004239 rtl_hw_start_8168cp_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004240 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004241
4242 case RTL_GIGA_MAC_VER_19:
4243 rtl_hw_start_8168c_1(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004244 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004245
4246 case RTL_GIGA_MAC_VER_20:
4247 rtl_hw_start_8168c_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004248 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004249
Francois Romieu197ff762008-06-28 13:16:02 +02004250 case RTL_GIGA_MAC_VER_21:
4251 rtl_hw_start_8168c_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004252 break;
Francois Romieu197ff762008-06-28 13:16:02 +02004253
Francois Romieu6fb07052008-06-29 11:54:28 +02004254 case RTL_GIGA_MAC_VER_22:
4255 rtl_hw_start_8168c_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004256 break;
Francois Romieu6fb07052008-06-29 11:54:28 +02004257
Francois Romieuef3386f2008-06-29 12:24:30 +02004258 case RTL_GIGA_MAC_VER_23:
4259 rtl_hw_start_8168cp_2(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004260 break;
Francois Romieuef3386f2008-06-29 12:24:30 +02004261
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004262 case RTL_GIGA_MAC_VER_24:
4263 rtl_hw_start_8168cp_3(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004264 break;
Francois Romieu7f3e3d32008-07-20 18:53:20 +02004265
Francois Romieu5b538df2008-07-20 16:22:45 +02004266 case RTL_GIGA_MAC_VER_25:
françois romieudaf9df62009-10-07 12:44:20 +00004267 case RTL_GIGA_MAC_VER_26:
4268 case RTL_GIGA_MAC_VER_27:
Francois Romieu5b538df2008-07-20 16:22:45 +02004269 rtl_hw_start_8168d(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004270 break;
Francois Romieu5b538df2008-07-20 16:22:45 +02004271
françois romieue6de30d2011-01-03 15:08:37 +00004272 case RTL_GIGA_MAC_VER_28:
4273 rtl_hw_start_8168d_4(ioaddr, pdev);
hayeswang4804b3b2011-03-21 01:50:29 +00004274 break;
Francois Romieucecb5fd2011-04-01 10:21:07 +02004275
hayeswang4804b3b2011-03-21 01:50:29 +00004276 case RTL_GIGA_MAC_VER_31:
4277 rtl_hw_start_8168dp(ioaddr, pdev);
4278 break;
4279
hayeswang01dc7fe2011-03-21 01:50:28 +00004280 case RTL_GIGA_MAC_VER_32:
4281 case RTL_GIGA_MAC_VER_33:
4282 rtl_hw_start_8168e(ioaddr, pdev);
4283 break;
françois romieue6de30d2011-01-03 15:08:37 +00004284
Francois Romieu219a1e92008-06-28 11:58:39 +02004285 default:
4286 printk(KERN_ERR PFX "%s: unknown chipset (mac_version = %d).\n",
4287 dev->name, tp->mac_version);
hayeswang4804b3b2011-03-21 01:50:29 +00004288 break;
Francois Romieu219a1e92008-06-28 11:58:39 +02004289 }
Francois Romieu2dd99532007-06-11 23:22:52 +02004290
Francois Romieu0e485152007-02-20 00:00:26 +01004291 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4292
Francois Romieub8363902008-06-01 12:31:57 +02004293 RTL_W8(Cfg9346, Cfg9346_Lock);
4294
Francois Romieu2dd99532007-06-11 23:22:52 +02004295 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004296
Francois Romieu0e485152007-02-20 00:00:26 +01004297 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01004298}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299
Francois Romieu2857ffb2008-08-02 21:08:49 +02004300#define R810X_CPCMD_QUIRK_MASK (\
4301 EnableBist | \
4302 Mac_dbgo_oe | \
4303 Force_half_dup | \
françois romieu5edcc532009-08-10 19:41:52 +00004304 Force_rxflow_en | \
Francois Romieu2857ffb2008-08-02 21:08:49 +02004305 Force_txflow_en | \
4306 Cxpl_dbg_sel | \
4307 ASF | \
4308 PktCntrDisable | \
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004309 Mac_dbgo_sel)
Francois Romieu2857ffb2008-08-02 21:08:49 +02004310
4311static void rtl_hw_start_8102e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4312{
Alexey Dobriyan350f7592009-11-25 15:54:21 -08004313 static const struct ephy_info e_info_8102e_1[] = {
Francois Romieu2857ffb2008-08-02 21:08:49 +02004314 { 0x01, 0, 0x6e65 },
4315 { 0x02, 0, 0x091f },
4316 { 0x03, 0, 0xc2f9 },
4317 { 0x06, 0, 0xafb5 },
4318 { 0x07, 0, 0x0e00 },
4319 { 0x19, 0, 0xec80 },
4320 { 0x01, 0, 0x2e65 },
4321 { 0x01, 0, 0x6e65 }
4322 };
4323 u8 cfg1;
4324
françois romieu650e8d52011-01-03 15:08:29 +00004325 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004326
4327 RTL_W8(DBG_REG, FIX_NAK_1);
4328
4329 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4330
4331 RTL_W8(Config1,
4332 LEDS1 | LEDS0 | Speed_down | MEMMAP | IOMAP | VPD | PMEnable);
4333 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
4334
4335 cfg1 = RTL_R8(Config1);
4336 if ((cfg1 & LEDS0) && (cfg1 & LEDS1))
4337 RTL_W8(Config1, cfg1 & ~LEDS0);
4338
Francois Romieu2857ffb2008-08-02 21:08:49 +02004339 rtl_ephy_init(ioaddr, e_info_8102e_1, ARRAY_SIZE(e_info_8102e_1));
4340}
4341
4342static void rtl_hw_start_8102e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4343{
françois romieu650e8d52011-01-03 15:08:29 +00004344 rtl_csi_access_enable_2(ioaddr);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004345
4346 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
4347
4348 RTL_W8(Config1, MEMMAP | IOMAP | VPD | PMEnable);
4349 RTL_W8(Config3, RTL_R8(Config3) & ~Beacon_en);
Francois Romieu2857ffb2008-08-02 21:08:49 +02004350}
4351
4352static void rtl_hw_start_8102e_3(void __iomem *ioaddr, struct pci_dev *pdev)
4353{
4354 rtl_hw_start_8102e_2(ioaddr, pdev);
4355
4356 rtl_ephy_write(ioaddr, 0x03, 0xc2f9);
4357}
4358
Hayes Wang5a5e4442011-02-22 17:26:21 +08004359static void rtl_hw_start_8105e_1(void __iomem *ioaddr, struct pci_dev *pdev)
4360{
4361 static const struct ephy_info e_info_8105e_1[] = {
4362 { 0x07, 0, 0x4000 },
4363 { 0x19, 0, 0x0200 },
4364 { 0x19, 0, 0x0020 },
4365 { 0x1e, 0, 0x2000 },
4366 { 0x03, 0, 0x0001 },
4367 { 0x19, 0, 0x0100 },
4368 { 0x19, 0, 0x0004 },
4369 { 0x0a, 0, 0x0020 }
4370 };
4371
Francois Romieucecb5fd2011-04-01 10:21:07 +02004372 /* Force LAN exit from ASPM if Rx/Tx are not idle */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004373 RTL_W32(FuncEvent, RTL_R32(FuncEvent) | 0x002800);
4374
Francois Romieucecb5fd2011-04-01 10:21:07 +02004375 /* Disable Early Tally Counter */
Hayes Wang5a5e4442011-02-22 17:26:21 +08004376 RTL_W32(FuncEvent, RTL_R32(FuncEvent) & ~0x010000);
4377
4378 RTL_W8(MCU, RTL_R8(MCU) | EN_NDP | EN_OOB_RESET);
4379 RTL_W8(DLLPR, RTL_R8(DLLPR) | PM_SWITCH);
4380
4381 rtl_ephy_init(ioaddr, e_info_8105e_1, ARRAY_SIZE(e_info_8105e_1));
4382}
4383
4384static void rtl_hw_start_8105e_2(void __iomem *ioaddr, struct pci_dev *pdev)
4385{
4386 rtl_hw_start_8105e_1(ioaddr, pdev);
4387 rtl_ephy_write(ioaddr, 0x1e, rtl_ephy_read(ioaddr, 0x1e) | 0x8000);
4388}
4389
Francois Romieu07ce4062007-02-23 23:36:39 +01004390static void rtl_hw_start_8101(struct net_device *dev)
4391{
Francois Romieucdf1a602007-06-11 23:29:50 +02004392 struct rtl8169_private *tp = netdev_priv(dev);
4393 void __iomem *ioaddr = tp->mmio_addr;
4394 struct pci_dev *pdev = tp->pci_dev;
4395
Francois Romieucecb5fd2011-04-01 10:21:07 +02004396 if (tp->mac_version == RTL_GIGA_MAC_VER_13 ||
4397 tp->mac_version == RTL_GIGA_MAC_VER_16) {
Francois Romieu9c14cea2008-07-05 00:21:15 +02004398 int cap = tp->pcie_cap;
4399
4400 if (cap) {
4401 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
4402 PCI_EXP_DEVCTL_NOSNOOP_EN);
4403 }
Francois Romieucdf1a602007-06-11 23:29:50 +02004404 }
4405
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004406 RTL_W8(Cfg9346, Cfg9346_Unlock);
4407
Francois Romieu2857ffb2008-08-02 21:08:49 +02004408 switch (tp->mac_version) {
4409 case RTL_GIGA_MAC_VER_07:
4410 rtl_hw_start_8102e_1(ioaddr, pdev);
4411 break;
4412
4413 case RTL_GIGA_MAC_VER_08:
4414 rtl_hw_start_8102e_3(ioaddr, pdev);
4415 break;
4416
4417 case RTL_GIGA_MAC_VER_09:
4418 rtl_hw_start_8102e_2(ioaddr, pdev);
4419 break;
Hayes Wang5a5e4442011-02-22 17:26:21 +08004420
4421 case RTL_GIGA_MAC_VER_29:
4422 rtl_hw_start_8105e_1(ioaddr, pdev);
4423 break;
4424 case RTL_GIGA_MAC_VER_30:
4425 rtl_hw_start_8105e_2(ioaddr, pdev);
4426 break;
Francois Romieucdf1a602007-06-11 23:29:50 +02004427 }
4428
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004429 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieucdf1a602007-06-11 23:29:50 +02004430
françois romieuf0298f82011-01-03 15:07:42 +00004431 RTL_W8(MaxTxPacketSize, TxPacketMax);
Francois Romieucdf1a602007-06-11 23:29:50 +02004432
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004433 rtl_set_rx_max_size(ioaddr, rx_buf_sz);
Francois Romieucdf1a602007-06-11 23:29:50 +02004434
Hayes Wangd24e9aa2011-02-22 17:26:19 +08004435 tp->cp_cmd &= ~R810X_CPCMD_QUIRK_MASK;
Francois Romieucdf1a602007-06-11 23:29:50 +02004436 RTL_W16(CPlusCmd, tp->cp_cmd);
4437
4438 RTL_W16(IntrMitigate, 0x0000);
4439
4440 rtl_set_rx_tx_desc_registers(tp, ioaddr);
4441
4442 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
4443 rtl_set_rx_tx_config_registers(tp);
4444
Francois Romieucdf1a602007-06-11 23:29:50 +02004445 RTL_R8(IntrMask);
4446
Francois Romieucdf1a602007-06-11 23:29:50 +02004447 rtl_set_rx_mode(dev);
4448
4449 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu6dccd162007-02-13 23:38:05 +01004450
Francois Romieu0e485152007-02-20 00:00:26 +01004451 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452}
4453
4454static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
4455{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 if (new_mtu < ETH_ZLEN || new_mtu > SafeMtu)
4457 return -EINVAL;
4458
4459 dev->mtu = new_mtu;
Michał Mirosław350fb322011-04-08 06:35:56 +00004460 netdev_update_features(dev);
4461
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00004462 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463}
4464
4465static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
4466{
Al Viro95e09182007-12-22 18:55:39 +00004467 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
4469}
4470
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004471static void rtl8169_free_rx_databuff(struct rtl8169_private *tp,
4472 void **data_buff, struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473{
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004474 dma_unmap_single(&tp->pci_dev->dev, le64_to_cpu(desc->addr), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004475 DMA_FROM_DEVICE);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004476
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004477 kfree(*data_buff);
4478 *data_buff = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 rtl8169_make_unusable_by_asic(desc);
4480}
4481
4482static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
4483{
4484 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
4485
4486 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
4487}
4488
4489static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
4490 u32 rx_buf_sz)
4491{
4492 desc->addr = cpu_to_le64(mapping);
4493 wmb();
4494 rtl8169_mark_to_asic(desc, rx_buf_sz);
4495}
4496
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004497static inline void *rtl8169_align(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498{
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004499 return (void *)ALIGN((long)data, 16);
4500}
4501
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004502static struct sk_buff *rtl8169_alloc_rx_data(struct rtl8169_private *tp,
4503 struct RxDesc *desc)
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004504{
4505 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 dma_addr_t mapping;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004507 struct device *d = &tp->pci_dev->dev;
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004508 struct net_device *dev = tp->dev;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004509 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004511 data = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
4512 if (!data)
4513 return NULL;
Francois Romieue9f63f32007-02-28 23:16:57 +01004514
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004515 if (rtl8169_align(data) != data) {
4516 kfree(data);
4517 data = kmalloc_node(rx_buf_sz + 15, GFP_KERNEL, node);
4518 if (!data)
4519 return NULL;
4520 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004521
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004522 mapping = dma_map_single(d, rtl8169_align(data), rx_buf_sz,
Stanislaw Gruszka231aee62010-10-20 22:25:38 +00004523 DMA_FROM_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004524 if (unlikely(dma_mapping_error(d, mapping))) {
4525 if (net_ratelimit())
4526 netif_err(tp, drv, tp->dev, "Failed to map RX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004527 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529
4530 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004531 return data;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004532
4533err_out:
4534 kfree(data);
4535 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536}
4537
4538static void rtl8169_rx_clear(struct rtl8169_private *tp)
4539{
Francois Romieu07d3f512007-02-21 22:40:46 +01004540 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541
4542 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004543 if (tp->Rx_databuff[i]) {
4544 rtl8169_free_rx_databuff(tp, tp->Rx_databuff + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 tp->RxDescArray + i);
4546 }
4547 }
4548}
4549
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004550static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551{
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004552 desc->opts1 |= cpu_to_le32(RingEnd);
4553}
Francois Romieu5b0384f2006-08-16 16:00:01 +02004554
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004555static int rtl8169_rx_fill(struct rtl8169_private *tp)
4556{
4557 unsigned int i;
4558
4559 for (i = 0; i < NUM_RX_DESC; i++) {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004560 void *data;
Francois Romieu4ae47c22007-06-16 23:28:45 +02004561
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004562 if (tp->Rx_databuff[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02004564
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004565 data = rtl8169_alloc_rx_data(tp, tp->RxDescArray + i);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004566 if (!data) {
4567 rtl8169_make_unusable_by_asic(tp->RxDescArray + i);
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004568 goto err_out;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004569 }
4570 tp->Rx_databuff[i] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004573 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
4574 return 0;
4575
4576err_out:
4577 rtl8169_rx_clear(tp);
4578 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579}
4580
4581static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
4582{
4583 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
4584}
4585
4586static int rtl8169_init_ring(struct net_device *dev)
4587{
4588 struct rtl8169_private *tp = netdev_priv(dev);
4589
4590 rtl8169_init_ring_indexes(tp);
4591
4592 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
Eric Dumazet6f0333b2010-10-11 11:17:47 +00004593 memset(tp->Rx_databuff, 0x0, NUM_RX_DESC * sizeof(void *));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594
Stanislaw Gruszka0ecbe1c2010-10-20 22:25:37 +00004595 return rtl8169_rx_fill(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596}
4597
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004598static void rtl8169_unmap_tx_skb(struct device *d, struct ring_info *tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 struct TxDesc *desc)
4600{
4601 unsigned int len = tx_skb->len;
4602
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004603 dma_unmap_single(d, le64_to_cpu(desc->addr), len, DMA_TO_DEVICE);
4604
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 desc->opts1 = 0x00;
4606 desc->opts2 = 0x00;
4607 desc->addr = 0x00;
4608 tx_skb->len = 0;
4609}
4610
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004611static void rtl8169_tx_clear_range(struct rtl8169_private *tp, u32 start,
4612 unsigned int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613{
4614 unsigned int i;
4615
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004616 for (i = 0; i < n; i++) {
4617 unsigned int entry = (start + i) % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 struct ring_info *tx_skb = tp->tx_skb + entry;
4619 unsigned int len = tx_skb->len;
4620
4621 if (len) {
4622 struct sk_buff *skb = tx_skb->skb;
4623
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004624 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 tp->TxDescArray + entry);
4626 if (skb) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00004627 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 dev_kfree_skb(skb);
4629 tx_skb->skb = NULL;
4630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 }
4632 }
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004633}
4634
4635static void rtl8169_tx_clear(struct rtl8169_private *tp)
4636{
4637 rtl8169_tx_clear_range(tp, tp->dirty_tx, NUM_TX_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 tp->cur_tx = tp->dirty_tx = 0;
4639}
4640
David Howellsc4028952006-11-22 14:57:56 +00004641static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642{
4643 struct rtl8169_private *tp = netdev_priv(dev);
4644
David Howellsc4028952006-11-22 14:57:56 +00004645 PREPARE_DELAYED_WORK(&tp->task, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 schedule_delayed_work(&tp->task, 4);
4647}
4648
4649static void rtl8169_wait_for_quiescence(struct net_device *dev)
4650{
4651 struct rtl8169_private *tp = netdev_priv(dev);
4652 void __iomem *ioaddr = tp->mmio_addr;
4653
4654 synchronize_irq(dev->irq);
4655
4656 /* Wait for any pending NAPI task to complete */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004657 napi_disable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658
4659 rtl8169_irq_mask_and_ack(ioaddr);
4660
David S. Millerd1d08d12008-01-07 20:53:33 -08004661 tp->intr_mask = 0xffff;
4662 RTL_W16(IntrMask, tp->intr_event);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07004663 napi_enable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664}
4665
David Howellsc4028952006-11-22 14:57:56 +00004666static void rtl8169_reinit_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667{
David Howellsc4028952006-11-22 14:57:56 +00004668 struct rtl8169_private *tp =
4669 container_of(work, struct rtl8169_private, task.work);
4670 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 int ret;
4672
Francois Romieueb2a0212007-02-15 23:37:21 +01004673 rtnl_lock();
4674
4675 if (!netif_running(dev))
4676 goto out_unlock;
4677
4678 rtl8169_wait_for_quiescence(dev);
4679 rtl8169_close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680
4681 ret = rtl8169_open(dev);
4682 if (unlikely(ret < 0)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004683 if (net_ratelimit())
4684 netif_err(tp, drv, dev,
4685 "reinit failure (status = %d). Rescheduling\n",
4686 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 rtl8169_schedule_work(dev, rtl8169_reinit_task);
4688 }
Francois Romieueb2a0212007-02-15 23:37:21 +01004689
4690out_unlock:
4691 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692}
4693
David Howellsc4028952006-11-22 14:57:56 +00004694static void rtl8169_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695{
David Howellsc4028952006-11-22 14:57:56 +00004696 struct rtl8169_private *tp =
4697 container_of(work, struct rtl8169_private, task.work);
4698 struct net_device *dev = tp->dev;
Francois Romieu56de4142011-03-15 17:29:31 +01004699 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700
Francois Romieueb2a0212007-02-15 23:37:21 +01004701 rtnl_lock();
4702
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 if (!netif_running(dev))
Francois Romieueb2a0212007-02-15 23:37:21 +01004704 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705
4706 rtl8169_wait_for_quiescence(dev);
4707
Francois Romieu56de4142011-03-15 17:29:31 +01004708 for (i = 0; i < NUM_RX_DESC; i++)
4709 rtl8169_mark_to_asic(tp->RxDescArray + i, rx_buf_sz);
4710
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 rtl8169_tx_clear(tp);
4712
Francois Romieu56de4142011-03-15 17:29:31 +01004713 rtl8169_init_ring_indexes(tp);
4714 rtl_hw_start(dev);
4715 netif_wake_queue(dev);
4716 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Francois Romieueb2a0212007-02-15 23:37:21 +01004717
4718out_unlock:
4719 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720}
4721
4722static void rtl8169_tx_timeout(struct net_device *dev)
4723{
4724 struct rtl8169_private *tp = netdev_priv(dev);
4725
françois romieue6de30d2011-01-03 15:08:37 +00004726 rtl8169_hw_reset(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727
4728 /* Let's wait a bit while any (async) irq lands on */
4729 rtl8169_schedule_work(dev, rtl8169_reset_task);
4730}
4731
4732static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
Francois Romieu2b7b4312011-04-18 22:53:24 -07004733 u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734{
4735 struct skb_shared_info *info = skb_shinfo(skb);
4736 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04004737 struct TxDesc * uninitialized_var(txd);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004738 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739
4740 entry = tp->cur_tx;
4741 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
4742 skb_frag_t *frag = info->frags + cur_frag;
4743 dma_addr_t mapping;
4744 u32 status, len;
4745 void *addr;
4746
4747 entry = (entry + 1) % NUM_TX_DESC;
4748
4749 txd = tp->TxDescArray + entry;
4750 len = frag->size;
4751 addr = ((void *) page_address(frag->page)) + frag->page_offset;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004752 mapping = dma_map_single(d, addr, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004753 if (unlikely(dma_mapping_error(d, mapping))) {
4754 if (net_ratelimit())
4755 netif_err(tp, drv, tp->dev,
4756 "Failed to map TX fragments DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004757 goto err_out;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759
Francois Romieucecb5fd2011-04-01 10:21:07 +02004760 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07004761 status = opts[0] | len |
4762 (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763
4764 txd->opts1 = cpu_to_le32(status);
Francois Romieu2b7b4312011-04-18 22:53:24 -07004765 txd->opts2 = cpu_to_le32(opts[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 txd->addr = cpu_to_le64(mapping);
4767
4768 tp->tx_skb[entry].len = len;
4769 }
4770
4771 if (cur_frag) {
4772 tp->tx_skb[entry].skb = skb;
4773 txd->opts1 |= cpu_to_le32(LastFrag);
4774 }
4775
4776 return cur_frag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004777
4778err_out:
4779 rtl8169_tx_clear_range(tp, tp->cur_tx + 1, cur_frag);
4780 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781}
4782
Francois Romieu2b7b4312011-04-18 22:53:24 -07004783static inline void rtl8169_tso_csum(struct rtl8169_private *tp,
4784 struct sk_buff *skb, u32 *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785{
Francois Romieu2b7b4312011-04-18 22:53:24 -07004786 const struct rtl_tx_desc_info *info = tx_desc_info + tp->txd_version;
Michał Mirosław350fb322011-04-08 06:35:56 +00004787 u32 mss = skb_shinfo(skb)->gso_size;
Francois Romieu2b7b4312011-04-18 22:53:24 -07004788 int offset = info->opts_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789
Francois Romieu2b7b4312011-04-18 22:53:24 -07004790 if (mss) {
4791 opts[0] |= TD_LSO;
4792 opts[offset] |= min(mss, TD_MSS_MAX) << info->mss_shift;
4793 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07004794 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795
4796 if (ip->protocol == IPPROTO_TCP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07004797 opts[offset] |= info->checksum.tcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 else if (ip->protocol == IPPROTO_UDP)
Francois Romieu2b7b4312011-04-18 22:53:24 -07004799 opts[offset] |= info->checksum.udp;
4800 else
4801 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803}
4804
Stephen Hemminger613573252009-08-31 19:50:58 +00004805static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
4806 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807{
4808 struct rtl8169_private *tp = netdev_priv(dev);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004809 unsigned int entry = tp->cur_tx % NUM_TX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 struct TxDesc *txd = tp->TxDescArray + entry;
4811 void __iomem *ioaddr = tp->mmio_addr;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004812 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 dma_addr_t mapping;
4814 u32 status, len;
Francois Romieu2b7b4312011-04-18 22:53:24 -07004815 u32 opts[2];
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004816 int frags;
Francois Romieu5b0384f2006-08-16 16:00:01 +02004817
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00004819 netif_err(tp, drv, dev, "BUG! Tx Ring full when queue awake!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004820 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 }
4822
4823 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004824 goto err_stop_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004826 len = skb_headlen(skb);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004827 mapping = dma_map_single(d, skb->data, len, DMA_TO_DEVICE);
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004828 if (unlikely(dma_mapping_error(d, mapping))) {
4829 if (net_ratelimit())
4830 netif_err(tp, drv, dev, "Failed to map TX DMA!\n");
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004831 goto err_dma_0;
Stanislaw Gruszkad827d862010-10-20 22:25:43 +00004832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833
4834 tp->tx_skb[entry].len = len;
4835 txd->addr = cpu_to_le64(mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836
Francois Romieu2b7b4312011-04-18 22:53:24 -07004837 opts[1] = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
4838 opts[0] = DescOwn;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004839
Francois Romieu2b7b4312011-04-18 22:53:24 -07004840 rtl8169_tso_csum(tp, skb, opts);
4841
4842 frags = rtl8169_xmit_frags(tp, skb, opts);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004843 if (frags < 0)
4844 goto err_dma_1;
4845 else if (frags)
Francois Romieu2b7b4312011-04-18 22:53:24 -07004846 opts[0] |= FirstFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004847 else {
Francois Romieu2b7b4312011-04-18 22:53:24 -07004848 opts[0] |= FirstFrag | LastFrag;
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004849 tp->tx_skb[entry].skb = skb;
4850 }
4851
Francois Romieu2b7b4312011-04-18 22:53:24 -07004852 txd->opts2 = cpu_to_le32(opts[1]);
4853
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 wmb();
4855
Francois Romieucecb5fd2011-04-01 10:21:07 +02004856 /* Anti gcc 2.95.3 bugware (sic) */
Francois Romieu2b7b4312011-04-18 22:53:24 -07004857 status = opts[0] | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 txd->opts1 = cpu_to_le32(status);
4859
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 tp->cur_tx += frags + 1;
4861
David Dillow4c020a92010-03-03 16:33:10 +00004862 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863
Francois Romieucecb5fd2011-04-01 10:21:07 +02004864 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865
4866 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
4867 netif_stop_queue(dev);
4868 smp_rmb();
4869 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
4870 netif_wake_queue(dev);
4871 }
4872
Stephen Hemminger613573252009-08-31 19:50:58 +00004873 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004875err_dma_1:
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004876 rtl8169_unmap_tx_skb(d, tp->tx_skb + entry, txd);
Stanislaw Gruszka3eafe502010-10-20 22:25:36 +00004877err_dma_0:
4878 dev_kfree_skb(skb);
4879 dev->stats.tx_dropped++;
4880 return NETDEV_TX_OK;
4881
4882err_stop_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883 netif_stop_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02004884 dev->stats.tx_dropped++;
Stephen Hemminger613573252009-08-31 19:50:58 +00004885 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886}
4887
4888static void rtl8169_pcierr_interrupt(struct net_device *dev)
4889{
4890 struct rtl8169_private *tp = netdev_priv(dev);
4891 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892 u16 pci_status, pci_cmd;
4893
4894 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
4895 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
4896
Joe Perchesbf82c182010-02-09 11:49:50 +00004897 netif_err(tp, intr, dev, "PCI error (cmd = 0x%04x, status = 0x%04x)\n",
4898 pci_cmd, pci_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899
4900 /*
4901 * The recovery sequence below admits a very elaborated explanation:
4902 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01004903 * - I did not see what else could be done;
4904 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 *
4906 * Feel free to adjust to your needs.
4907 */
Francois Romieua27993f2006-12-18 00:04:19 +01004908 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01004909 pci_cmd &= ~PCI_COMMAND_PARITY;
4910 else
4911 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
4912
4913 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914
4915 pci_write_config_word(pdev, PCI_STATUS,
4916 pci_status & (PCI_STATUS_DETECTED_PARITY |
4917 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
4918 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
4919
4920 /* The infamous DAC f*ckup only happens at boot time */
4921 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
françois romieue6de30d2011-01-03 15:08:37 +00004922 void __iomem *ioaddr = tp->mmio_addr;
4923
Joe Perchesbf82c182010-02-09 11:49:50 +00004924 netif_info(tp, intr, dev, "disabling PCI DAC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 tp->cp_cmd &= ~PCIDAC;
4926 RTL_W16(CPlusCmd, tp->cp_cmd);
4927 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 }
4929
françois romieue6de30d2011-01-03 15:08:37 +00004930 rtl8169_hw_reset(tp);
Francois Romieud03902b2006-11-23 00:00:42 +01004931
4932 rtl8169_schedule_work(dev, rtl8169_reinit_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933}
4934
Francois Romieu07d3f512007-02-21 22:40:46 +01004935static void rtl8169_tx_interrupt(struct net_device *dev,
4936 struct rtl8169_private *tp,
4937 void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938{
4939 unsigned int dirty_tx, tx_left;
4940
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 dirty_tx = tp->dirty_tx;
4942 smp_rmb();
4943 tx_left = tp->cur_tx - dirty_tx;
4944
4945 while (tx_left > 0) {
4946 unsigned int entry = dirty_tx % NUM_TX_DESC;
4947 struct ring_info *tx_skb = tp->tx_skb + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 u32 status;
4949
4950 rmb();
4951 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
4952 if (status & DescOwn)
4953 break;
4954
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00004955 rtl8169_unmap_tx_skb(&tp->pci_dev->dev, tx_skb,
4956 tp->TxDescArray + entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 if (status & LastFrag) {
Stanislaw Gruszkacac4b222010-10-20 22:25:40 +00004958 dev->stats.tx_packets++;
4959 dev->stats.tx_bytes += tx_skb->skb->len;
Eric Dumazet87433bf2009-06-09 22:55:53 +00004960 dev_kfree_skb(tx_skb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 tx_skb->skb = NULL;
4962 }
4963 dirty_tx++;
4964 tx_left--;
4965 }
4966
4967 if (tp->dirty_tx != dirty_tx) {
4968 tp->dirty_tx = dirty_tx;
4969 smp_wmb();
4970 if (netif_queue_stopped(dev) &&
4971 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
4972 netif_wake_queue(dev);
4973 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02004974 /*
4975 * 8168 hack: TxPoll requests are lost when the Tx packets are
4976 * too close. Let's kick an extra TxPoll request when a burst
4977 * of start_xmit activity is detected (if it is not detected,
4978 * it is slow enough). -- FR
4979 */
4980 smp_rmb();
4981 if (tp->cur_tx != dirty_tx)
4982 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 }
4984}
4985
Francois Romieu126fa4b2005-05-12 20:09:17 -04004986static inline int rtl8169_fragmented_frame(u32 status)
4987{
4988 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
4989}
4990
Eric Dumazetadea1ac72010-09-05 20:04:05 -07004991static inline void rtl8169_rx_csum(struct sk_buff *skb, u32 opts1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 u32 status = opts1 & RxProtoMask;
4994
4995 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
Shan Weid5d3ebe2010-11-12 00:15:25 +00004996 ((status == RxProtoUDP) && !(opts1 & UDPFail)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 skb->ip_summed = CHECKSUM_UNNECESSARY;
4998 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07004999 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000}
5001
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005002static struct sk_buff *rtl8169_try_rx_copy(void *data,
5003 struct rtl8169_private *tp,
5004 int pkt_size,
5005 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005007 struct sk_buff *skb;
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005008 struct device *d = &tp->pci_dev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005010 data = rtl8169_align(data);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005011 dma_sync_single_for_cpu(d, addr, pkt_size, DMA_FROM_DEVICE);
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005012 prefetch(data);
5013 skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
5014 if (skb)
5015 memcpy(skb->data, data, pkt_size);
Stanislaw Gruszka48addcc2010-10-20 22:25:39 +00005016 dma_sync_single_for_device(d, addr, pkt_size, DMA_FROM_DEVICE);
5017
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005018 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019}
5020
Francois Romieu07d3f512007-02-21 22:40:46 +01005021static int rtl8169_rx_interrupt(struct net_device *dev,
5022 struct rtl8169_private *tp,
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005023 void __iomem *ioaddr, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024{
5025 unsigned int cur_rx, rx_left;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005026 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 cur_rx = tp->cur_rx;
5029 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02005030 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005032 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005034 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 u32 status;
5036
5037 rmb();
Francois Romieu126fa4b2005-05-12 20:09:17 -04005038 status = le32_to_cpu(desc->opts1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039
5040 if (status & DescOwn)
5041 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005042 if (unlikely(status & RxRES)) {
Joe Perchesbf82c182010-02-09 11:49:50 +00005043 netif_info(tp, rx_err, dev, "Rx ERROR. status = %08x\n",
5044 status);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005045 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02005047 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02005049 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005050 if (status & RxFOVF) {
5051 rtl8169_schedule_work(dev, rtl8169_reset_task);
Francois Romieucebf8cc2007-10-18 12:06:54 +02005052 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02005053 }
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005054 rtl8169_mark_to_asic(desc, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055 } else {
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005056 struct sk_buff *skb;
Stephen Hemmingerb4496552007-06-17 01:06:49 +02005057 dma_addr_t addr = le64_to_cpu(desc->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 int pkt_size = (status & 0x00001FFF) - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059
Francois Romieu126fa4b2005-05-12 20:09:17 -04005060 /*
5061 * The driver does not support incoming fragmented
5062 * frames. They are seen as a symptom of over-mtu
5063 * sized frames.
5064 */
5065 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02005066 dev->stats.rx_dropped++;
5067 dev->stats.rx_length_errors++;
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005068 rtl8169_mark_to_asic(desc, rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02005069 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04005070 }
5071
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005072 skb = rtl8169_try_rx_copy(tp->Rx_databuff[entry],
5073 tp, pkt_size, addr);
5074 rtl8169_mark_to_asic(desc, rx_buf_sz);
5075 if (!skb) {
5076 dev->stats.rx_dropped++;
5077 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 }
5079
Eric Dumazetadea1ac72010-09-05 20:04:05 -07005080 rtl8169_rx_csum(skb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 skb_put(skb, pkt_size);
5082 skb->protocol = eth_type_trans(skb, dev);
5083
Francois Romieu7a8fc772011-03-01 17:18:33 +01005084 rtl8169_rx_vlan_tag(desc, skb);
5085
Francois Romieu56de4142011-03-15 17:29:31 +01005086 napi_gro_receive(&tp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087
Francois Romieucebf8cc2007-10-18 12:06:54 +02005088 dev->stats.rx_bytes += pkt_size;
5089 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 }
Francois Romieu6dccd162007-02-13 23:38:05 +01005091
5092 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00005093 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01005094 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
5095 desc->opts2 = 0;
5096 cur_rx++;
5097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 }
5099
5100 count = cur_rx - tp->cur_rx;
5101 tp->cur_rx = cur_rx;
5102
Eric Dumazet6f0333b2010-10-11 11:17:47 +00005103 tp->dirty_rx += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104
5105 return count;
5106}
5107
Francois Romieu07d3f512007-02-21 22:40:46 +01005108static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109{
Francois Romieu07d3f512007-02-21 22:40:46 +01005110 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 int handled = 0;
Francois Romieu865c6522008-05-11 14:51:00 +02005114 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115
David Dillowf11a3772009-05-22 15:29:34 +00005116 /* loop handling interrupts until we have no new ones or
5117 * we hit a invalid/hotplug case.
5118 */
Francois Romieu865c6522008-05-11 14:51:00 +02005119 status = RTL_R16(IntrStatus);
David Dillowf11a3772009-05-22 15:29:34 +00005120 while (status && status != 0xffff) {
5121 handled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122
David Dillowf11a3772009-05-22 15:29:34 +00005123 /* Handle all of the error cases first. These will reset
5124 * the chip, so just exit the loop.
5125 */
5126 if (unlikely(!netif_running(dev))) {
5127 rtl8169_asic_down(ioaddr);
5128 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 }
David Dillowf11a3772009-05-22 15:29:34 +00005130
Francois Romieu1519e572011-02-03 12:02:36 +01005131 if (unlikely(status & RxFIFOOver)) {
5132 switch (tp->mac_version) {
5133 /* Work around for rx fifo overflow */
5134 case RTL_GIGA_MAC_VER_11:
5135 case RTL_GIGA_MAC_VER_22:
5136 case RTL_GIGA_MAC_VER_26:
5137 netif_stop_queue(dev);
5138 rtl8169_tx_timeout(dev);
5139 goto done;
Francois Romieuf60ac8e2011-02-03 17:27:52 +01005140 /* Testers needed. */
5141 case RTL_GIGA_MAC_VER_17:
5142 case RTL_GIGA_MAC_VER_19:
5143 case RTL_GIGA_MAC_VER_20:
5144 case RTL_GIGA_MAC_VER_21:
5145 case RTL_GIGA_MAC_VER_23:
5146 case RTL_GIGA_MAC_VER_24:
5147 case RTL_GIGA_MAC_VER_27:
5148 case RTL_GIGA_MAC_VER_28:
hayeswang4804b3b2011-03-21 01:50:29 +00005149 case RTL_GIGA_MAC_VER_31:
Francois Romieu1519e572011-02-03 12:02:36 +01005150 /* Experimental science. Pktgen proof. */
5151 case RTL_GIGA_MAC_VER_12:
5152 case RTL_GIGA_MAC_VER_25:
5153 if (status == RxFIFOOver)
5154 goto done;
5155 break;
5156 default:
5157 break;
5158 }
David Dillowf11a3772009-05-22 15:29:34 +00005159 }
5160
5161 if (unlikely(status & SYSErr)) {
5162 rtl8169_pcierr_interrupt(dev);
5163 break;
5164 }
5165
5166 if (status & LinkChg)
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005167 __rtl8169_check_link_status(dev, tp, ioaddr, true);
David Dillowf11a3772009-05-22 15:29:34 +00005168
5169 /* We need to see the lastest version of tp->intr_mask to
5170 * avoid ignoring an MSI interrupt and having to wait for
5171 * another event which may never come.
5172 */
5173 smp_rmb();
5174 if (status & tp->intr_mask & tp->napi_event) {
5175 RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
5176 tp->intr_mask = ~tp->napi_event;
5177
5178 if (likely(napi_schedule_prep(&tp->napi)))
5179 __napi_schedule(&tp->napi);
Joe Perchesbf82c182010-02-09 11:49:50 +00005180 else
5181 netif_info(tp, intr, dev,
5182 "interrupt %04x in poll\n", status);
David Dillowf11a3772009-05-22 15:29:34 +00005183 }
5184
5185 /* We only get a new MSI interrupt when all active irq
5186 * sources on the chip have been acknowledged. So, ack
5187 * everything we've seen and check if new sources have become
5188 * active to avoid blocking all interrupts from the chip.
5189 */
5190 RTL_W16(IntrStatus,
5191 (status & RxFIFOOver) ? (status | RxOverflow) : status);
5192 status = RTL_R16(IntrStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 }
Francois Romieu1519e572011-02-03 12:02:36 +01005194done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 return IRQ_RETVAL(handled);
5196}
5197
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005198static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005200 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
5201 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202 void __iomem *ioaddr = tp->mmio_addr;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005203 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005205 work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206 rtl8169_tx_interrupt(dev, tp, ioaddr);
5207
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005208 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005209 napi_complete(napi);
David Dillowf11a3772009-05-22 15:29:34 +00005210
5211 /* We need for force the visibility of tp->intr_mask
5212 * for other CPUs, as we can loose an MSI interrupt
5213 * and potentially wait for a retransmit timeout if we don't.
5214 * The posted write to IntrMask is safe, as it will
5215 * eventually make it to the chip and we won't loose anything
5216 * until it does.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 */
David Dillowf11a3772009-05-22 15:29:34 +00005218 tp->intr_mask = 0xffff;
David Dillow4c020a92010-03-03 16:33:10 +00005219 wmb();
Francois Romieu0e485152007-02-20 00:00:26 +01005220 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221 }
5222
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005223 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225
Francois Romieu523a6092008-09-10 22:28:56 +02005226static void rtl8169_rx_missed(struct net_device *dev, void __iomem *ioaddr)
5227{
5228 struct rtl8169_private *tp = netdev_priv(dev);
5229
5230 if (tp->mac_version > RTL_GIGA_MAC_VER_06)
5231 return;
5232
5233 dev->stats.rx_missed_errors += (RTL_R32(RxMissed) & 0xffffff);
5234 RTL_W32(RxMissed, 0);
5235}
5236
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237static void rtl8169_down(struct net_device *dev)
5238{
5239 struct rtl8169_private *tp = netdev_priv(dev);
5240 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241
Francois Romieu4876cc12011-03-11 21:07:11 +01005242 del_timer_sync(&tp->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243
5244 netif_stop_queue(dev);
5245
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005246 napi_disable(&tp->napi);
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01005247
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248 spin_lock_irq(&tp->lock);
5249
5250 rtl8169_asic_down(ioaddr);
Stanislaw Gruszka323bb682010-10-20 22:25:41 +00005251 /*
5252 * At this point device interrupts can not be enabled in any function,
5253 * as netif_running is not true (rtl8169_interrupt, rtl8169_reset_task,
5254 * rtl8169_reinit_task) and napi is disabled (rtl8169_poll).
5255 */
Francois Romieu523a6092008-09-10 22:28:56 +02005256 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257
5258 spin_unlock_irq(&tp->lock);
5259
5260 synchronize_irq(dev->irq);
5261
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262 /* Give a racing hard_start_xmit a few cycles to complete. */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07005263 synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265 rtl8169_tx_clear(tp);
5266
5267 rtl8169_rx_clear(tp);
françois romieu065c27c2011-01-03 15:08:12 +00005268
5269 rtl_pll_power_down(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270}
5271
5272static int rtl8169_close(struct net_device *dev)
5273{
5274 struct rtl8169_private *tp = netdev_priv(dev);
5275 struct pci_dev *pdev = tp->pci_dev;
5276
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005277 pm_runtime_get_sync(&pdev->dev);
5278
Francois Romieucecb5fd2011-04-01 10:21:07 +02005279 /* Update counters before going down */
Ivan Vecera355423d2009-02-06 21:49:57 -08005280 rtl8169_update_counters(dev);
5281
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 rtl8169_down(dev);
5283
5284 free_irq(dev->irq, dev);
5285
Stanislaw Gruszka82553bb2010-10-08 04:25:01 +00005286 dma_free_coherent(&pdev->dev, R8169_RX_RING_BYTES, tp->RxDescArray,
5287 tp->RxPhyAddr);
5288 dma_free_coherent(&pdev->dev, R8169_TX_RING_BYTES, tp->TxDescArray,
5289 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290 tp->TxDescArray = NULL;
5291 tp->RxDescArray = NULL;
5292
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005293 pm_runtime_put_sync(&pdev->dev);
5294
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 return 0;
5296}
5297
Francois Romieu07ce4062007-02-23 23:36:39 +01005298static void rtl_set_rx_mode(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299{
5300 struct rtl8169_private *tp = netdev_priv(dev);
5301 void __iomem *ioaddr = tp->mmio_addr;
5302 unsigned long flags;
5303 u32 mc_filter[2]; /* Multicast hash filter */
Francois Romieu07d3f512007-02-21 22:40:46 +01005304 int rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305 u32 tmp = 0;
5306
5307 if (dev->flags & IFF_PROMISC) {
5308 /* Unconditionally log net taps. */
Joe Perchesbf82c182010-02-09 11:49:50 +00005309 netif_notice(tp, link, dev, "Promiscuous mode enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310 rx_mode =
5311 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
5312 AcceptAllPhys;
5313 mc_filter[1] = mc_filter[0] = 0xffffffff;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00005314 } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
Joe Perches8e95a202009-12-03 07:58:21 +00005315 (dev->flags & IFF_ALLMULTI)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 /* Too many to filter perfectly -- accept all multicasts. */
5317 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
5318 mc_filter[1] = mc_filter[0] = 0xffffffff;
5319 } else {
Jiri Pirko22bedad32010-04-01 21:22:57 +00005320 struct netdev_hw_addr *ha;
Francois Romieu07d3f512007-02-21 22:40:46 +01005321
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 rx_mode = AcceptBroadcast | AcceptMyPhys;
5323 mc_filter[1] = mc_filter[0] = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +00005324 netdev_for_each_mc_addr(ha, dev) {
5325 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
5327 rx_mode |= AcceptMulticast;
5328 }
5329 }
5330
5331 spin_lock_irqsave(&tp->lock, flags);
5332
5333 tmp = rtl8169_rx_config | rx_mode |
Francois Romieu2b7b4312011-04-18 22:53:24 -07005334 (RTL_R32(RxConfig) & RTL_RX_CONFIG_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335
Francois Romieuf887cce2008-07-17 22:24:18 +02005336 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
Francois Romieu1087f4f2007-12-26 22:46:05 +01005337 u32 data = mc_filter[0];
5338
5339 mc_filter[0] = swab32(mc_filter[1]);
5340 mc_filter[1] = swab32(data);
Francois Romieubcf0bf92006-07-26 23:14:13 +02005341 }
5342
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343 RTL_W32(MAR0 + 4, mc_filter[1]);
Francois Romieu78f1cd02010-03-27 19:35:46 -07005344 RTL_W32(MAR0 + 0, mc_filter[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345
Francois Romieu57a9f232007-06-04 22:10:15 +02005346 RTL_W32(RxConfig, tmp);
5347
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 spin_unlock_irqrestore(&tp->lock, flags);
5349}
5350
5351/**
5352 * rtl8169_get_stats - Get rtl8169 read/write statistics
5353 * @dev: The Ethernet Device to get statistics for
5354 *
5355 * Get TX/RX statistics for rtl8169
5356 */
5357static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
5358{
5359 struct rtl8169_private *tp = netdev_priv(dev);
5360 void __iomem *ioaddr = tp->mmio_addr;
5361 unsigned long flags;
5362
5363 if (netif_running(dev)) {
5364 spin_lock_irqsave(&tp->lock, flags);
Francois Romieu523a6092008-09-10 22:28:56 +02005365 rtl8169_rx_missed(dev, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 spin_unlock_irqrestore(&tp->lock, flags);
5367 }
Francois Romieu5b0384f2006-08-16 16:00:01 +02005368
Francois Romieucebf8cc2007-10-18 12:06:54 +02005369 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370}
5371
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005372static void rtl8169_net_suspend(struct net_device *dev)
Francois Romieu5d06a992006-02-23 00:47:58 +01005373{
françois romieu065c27c2011-01-03 15:08:12 +00005374 struct rtl8169_private *tp = netdev_priv(dev);
5375
Francois Romieu5d06a992006-02-23 00:47:58 +01005376 if (!netif_running(dev))
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005377 return;
Francois Romieu5d06a992006-02-23 00:47:58 +01005378
françois romieu065c27c2011-01-03 15:08:12 +00005379 rtl_pll_power_down(tp);
5380
Francois Romieu5d06a992006-02-23 00:47:58 +01005381 netif_device_detach(dev);
5382 netif_stop_queue(dev);
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005383}
Francois Romieu5d06a992006-02-23 00:47:58 +01005384
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005385#ifdef CONFIG_PM
5386
5387static int rtl8169_suspend(struct device *device)
5388{
5389 struct pci_dev *pdev = to_pci_dev(device);
5390 struct net_device *dev = pci_get_drvdata(pdev);
5391
5392 rtl8169_net_suspend(dev);
Francois Romieu1371fa62007-04-02 23:01:11 +02005393
Francois Romieu5d06a992006-02-23 00:47:58 +01005394 return 0;
5395}
5396
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005397static void __rtl8169_resume(struct net_device *dev)
5398{
françois romieu065c27c2011-01-03 15:08:12 +00005399 struct rtl8169_private *tp = netdev_priv(dev);
5400
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005401 netif_device_attach(dev);
françois romieu065c27c2011-01-03 15:08:12 +00005402
5403 rtl_pll_power_up(tp);
5404
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005405 rtl8169_schedule_work(dev, rtl8169_reset_task);
5406}
5407
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005408static int rtl8169_resume(struct device *device)
Francois Romieu5d06a992006-02-23 00:47:58 +01005409{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005410 struct pci_dev *pdev = to_pci_dev(device);
Francois Romieu5d06a992006-02-23 00:47:58 +01005411 struct net_device *dev = pci_get_drvdata(pdev);
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005412 struct rtl8169_private *tp = netdev_priv(dev);
5413
5414 rtl8169_init_phy(dev, tp);
Francois Romieu5d06a992006-02-23 00:47:58 +01005415
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005416 if (netif_running(dev))
5417 __rtl8169_resume(dev);
Francois Romieu5d06a992006-02-23 00:47:58 +01005418
Francois Romieu5d06a992006-02-23 00:47:58 +01005419 return 0;
5420}
5421
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005422static int rtl8169_runtime_suspend(struct device *device)
5423{
5424 struct pci_dev *pdev = to_pci_dev(device);
5425 struct net_device *dev = pci_get_drvdata(pdev);
5426 struct rtl8169_private *tp = netdev_priv(dev);
5427
5428 if (!tp->TxDescArray)
5429 return 0;
5430
5431 spin_lock_irq(&tp->lock);
5432 tp->saved_wolopts = __rtl8169_get_wol(tp);
5433 __rtl8169_set_wol(tp, WAKE_ANY);
5434 spin_unlock_irq(&tp->lock);
5435
5436 rtl8169_net_suspend(dev);
5437
5438 return 0;
5439}
5440
5441static int rtl8169_runtime_resume(struct device *device)
5442{
5443 struct pci_dev *pdev = to_pci_dev(device);
5444 struct net_device *dev = pci_get_drvdata(pdev);
5445 struct rtl8169_private *tp = netdev_priv(dev);
5446
5447 if (!tp->TxDescArray)
5448 return 0;
5449
5450 spin_lock_irq(&tp->lock);
5451 __rtl8169_set_wol(tp, tp->saved_wolopts);
5452 tp->saved_wolopts = 0;
5453 spin_unlock_irq(&tp->lock);
5454
Stanislaw Gruszkafccec102010-10-20 22:25:42 +00005455 rtl8169_init_phy(dev, tp);
5456
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005457 __rtl8169_resume(dev);
5458
5459 return 0;
5460}
5461
5462static int rtl8169_runtime_idle(struct device *device)
5463{
5464 struct pci_dev *pdev = to_pci_dev(device);
5465 struct net_device *dev = pci_get_drvdata(pdev);
5466 struct rtl8169_private *tp = netdev_priv(dev);
5467
Rafael J. Wysockie4fbce72010-12-08 15:32:14 +00005468 return tp->TxDescArray ? -EBUSY : 0;
Rafael J. Wysockie1759442010-03-14 14:33:51 +00005469}
5470
Alexey Dobriyan47145212009-12-14 18:00:08 -08005471static const struct dev_pm_ops rtl8169_pm_ops = {
Francois Romieucecb5fd2011-04-01 10:21:07 +02005472 .suspend = rtl8169_suspend,
5473 .resume = rtl8169_resume,
5474 .freeze = rtl8169_suspend,
5475 .thaw = rtl8169_resume,
5476 .poweroff = rtl8169_suspend,
5477 .restore = rtl8169_resume,
5478 .runtime_suspend = rtl8169_runtime_suspend,
5479 .runtime_resume = rtl8169_runtime_resume,
5480 .runtime_idle = rtl8169_runtime_idle,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005481};
5482
5483#define RTL8169_PM_OPS (&rtl8169_pm_ops)
5484
5485#else /* !CONFIG_PM */
5486
5487#define RTL8169_PM_OPS NULL
5488
5489#endif /* !CONFIG_PM */
5490
Francois Romieu1765f952008-09-13 17:21:40 +02005491static void rtl_shutdown(struct pci_dev *pdev)
5492{
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005493 struct net_device *dev = pci_get_drvdata(pdev);
françois romieu4bb3f522009-06-17 11:41:45 +00005494 struct rtl8169_private *tp = netdev_priv(dev);
5495 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu1765f952008-09-13 17:21:40 +02005496
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005497 rtl8169_net_suspend(dev);
5498
Francois Romieucecb5fd2011-04-01 10:21:07 +02005499 /* Restore original MAC address */
Ivan Veceracc098dc2009-11-29 23:12:52 -08005500 rtl_rar_set(tp, dev->perm_addr);
5501
françois romieu4bb3f522009-06-17 11:41:45 +00005502 spin_lock_irq(&tp->lock);
5503
5504 rtl8169_asic_down(ioaddr);
5505
5506 spin_unlock_irq(&tp->lock);
5507
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005508 if (system_state == SYSTEM_POWER_OFF) {
françois romieuca52efd2009-07-24 12:34:19 +00005509 /* WoL fails with some 8168 when the receiver is disabled. */
5510 if (tp->features & RTL_FEATURE_WOL) {
5511 pci_clear_master(pdev);
5512
5513 RTL_W8(ChipCmd, CmdRxEnb);
5514 /* PCI commit */
5515 RTL_R8(ChipCmd);
5516 }
5517
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005518 pci_wake_from_d3(pdev, true);
5519 pci_set_power_state(pdev, PCI_D3hot);
5520 }
5521}
Francois Romieu5d06a992006-02-23 00:47:58 +01005522
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523static struct pci_driver rtl8169_pci_driver = {
5524 .name = MODULENAME,
5525 .id_table = rtl8169_pci_tbl,
5526 .probe = rtl8169_init_one,
5527 .remove = __devexit_p(rtl8169_remove_one),
Francois Romieu1765f952008-09-13 17:21:40 +02005528 .shutdown = rtl_shutdown,
Rafael J. Wysocki861ab442009-04-05 08:40:04 +00005529 .driver.pm = RTL8169_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530};
5531
Francois Romieu07d3f512007-02-21 22:40:46 +01005532static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533{
Jeff Garzik29917622006-08-19 17:48:59 -04005534 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535}
5536
Francois Romieu07d3f512007-02-21 22:40:46 +01005537static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538{
5539 pci_unregister_driver(&rtl8169_pci_driver);
5540}
5541
5542module_init(rtl8169_init_module);
5543module_exit(rtl8169_cleanup_module);