blob: f4ad9ff268690ba4c46cea6ee97ef17d8c15944a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Francois Romieu07d3f512007-02-21 22:40:46 +01002 * r8169.c: RealTek 8169/8168/8101 ethernet driver.
3 *
4 * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
5 * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
6 * Copyright (c) a lot of people too. Please respect their work.
7 *
8 * See MAINTAINERS file for support contact information.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11#include <linux/module.h>
12#include <linux/moduleparam.h>
13#include <linux/pci.h>
14#include <linux/netdevice.h>
15#include <linux/etherdevice.h>
16#include <linux/delay.h>
17#include <linux/ethtool.h>
18#include <linux/mii.h>
19#include <linux/if_vlan.h>
20#include <linux/crc32.h>
21#include <linux/in.h>
22#include <linux/ip.h>
23#include <linux/tcp.h>
24#include <linux/init.h>
25#include <linux/dma-mapping.h>
26
Francois Romieu99f252b2007-04-02 22:59:59 +020027#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/io.h>
29#include <asm/irq.h>
30
Francois Romieu865c6522008-05-11 14:51:00 +020031#define RTL8169_VERSION "2.3LK-NAPI"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#define MODULENAME "r8169"
33#define PFX MODULENAME ": "
34
35#ifdef RTL8169_DEBUG
36#define assert(expr) \
Francois Romieu5b0384f2006-08-16 16:00:01 +020037 if (!(expr)) { \
38 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
39 #expr,__FILE__,__FUNCTION__,__LINE__); \
40 }
Joe Perches06fa7352007-10-18 21:15:00 +020041#define dprintk(fmt, args...) \
42 do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#else
44#define assert(expr) do {} while (0)
45#define dprintk(fmt, args...) do {} while (0)
46#endif /* RTL8169_DEBUG */
47
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020048#define R8169_MSG_DEFAULT \
Francois Romieuf0e837d2005-09-30 16:54:02 -070049 (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +020050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define TX_BUFFS_AVAIL(tp) \
52 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054/* Maximum events (Rx packets, etc.) to handle at each interrupt. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050055static const int max_interrupt_work = 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
58 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
Arjan van de Venf71e1302006-03-03 21:33:57 -050059static const int multicast_filter_limit = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61/* MAC address length */
62#define MAC_ADDR_LEN 6
63
Francois Romieu9c14cea2008-07-05 00:21:15 +020064#define MAX_READ_REQUEST_SHIFT 12
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#define RX_FIFO_THRESH 7 /* 7 means NO threshold, Rx buffer level before first PCI xfer. */
66#define RX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
67#define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
Francois Romieu07d3f512007-02-21 22:40:46 +010068#define EarlyTxThld 0x3F /* 0x3F means NO early transmit */
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#define RxPacketMaxSize 0x3FE8 /* 16K - 1 - ETH_HLEN - VLAN - CRC... */
70#define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
71#define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
72
73#define R8169_REGS_SIZE 256
74#define R8169_NAPI_WEIGHT 64
75#define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
76#define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
77#define RX_BUF_SIZE 1536 /* Rx Buffer size */
78#define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
79#define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
80
81#define RTL8169_TX_TIMEOUT (6*HZ)
82#define RTL8169_PHY_TIMEOUT (10*HZ)
83
84/* write/read MMIO register */
85#define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
86#define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
87#define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
88#define RTL_R8(reg) readb (ioaddr + (reg))
89#define RTL_R16(reg) readw (ioaddr + (reg))
90#define RTL_R32(reg) ((unsigned long) readl (ioaddr + (reg)))
91
92enum mac_version {
Francois Romieuba6eb6e2007-06-11 23:35:18 +020093 RTL_GIGA_MAC_VER_01 = 0x01, // 8169
94 RTL_GIGA_MAC_VER_02 = 0x02, // 8169S
95 RTL_GIGA_MAC_VER_03 = 0x03, // 8110S
96 RTL_GIGA_MAC_VER_04 = 0x04, // 8169SB
97 RTL_GIGA_MAC_VER_05 = 0x05, // 8110SCd
Francois Romieu6dccd162007-02-13 23:38:05 +010098 RTL_GIGA_MAC_VER_06 = 0x06, // 8110SCe
Francois Romieu2dd99532007-06-11 23:22:52 +020099 RTL_GIGA_MAC_VER_11 = 0x0b, // 8168Bb
Francois Romieue3cf0cc2007-08-17 14:55:46 +0200100 RTL_GIGA_MAC_VER_12 = 0x0c, // 8168Be
101 RTL_GIGA_MAC_VER_13 = 0x0d, // 8101Eb
102 RTL_GIGA_MAC_VER_14 = 0x0e, // 8101 ?
103 RTL_GIGA_MAC_VER_15 = 0x0f, // 8101 ?
104 RTL_GIGA_MAC_VER_16 = 0x11, // 8101Ec
105 RTL_GIGA_MAC_VER_17 = 0x10, // 8168Bf
106 RTL_GIGA_MAC_VER_18 = 0x12, // 8168CP
107 RTL_GIGA_MAC_VER_19 = 0x13, // 8168C
108 RTL_GIGA_MAC_VER_20 = 0x14 // 8168C
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109};
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#define _R(NAME,MAC,MASK) \
112 { .name = NAME, .mac_version = MAC, .RxConfigMask = MASK }
113
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800114static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 const char *name;
116 u8 mac_version;
117 u32 RxConfigMask; /* Clears the bits supported by this chip */
118} rtl_chip_info[] = {
Francois Romieuba6eb6e2007-06-11 23:35:18 +0200119 _R("RTL8169", RTL_GIGA_MAC_VER_01, 0xff7e1880), // 8169
120 _R("RTL8169s", RTL_GIGA_MAC_VER_02, 0xff7e1880), // 8169S
121 _R("RTL8110s", RTL_GIGA_MAC_VER_03, 0xff7e1880), // 8110S
122 _R("RTL8169sb/8110sb", RTL_GIGA_MAC_VER_04, 0xff7e1880), // 8169SB
123 _R("RTL8169sc/8110sc", RTL_GIGA_MAC_VER_05, 0xff7e1880), // 8110SCd
Francois Romieu6dccd162007-02-13 23:38:05 +0100124 _R("RTL8169sc/8110sc", RTL_GIGA_MAC_VER_06, 0xff7e1880), // 8110SCe
Francois Romieubcf0bf92006-07-26 23:14:13 +0200125 _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_11, 0xff7e1880), // PCI-E
126 _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_12, 0xff7e1880), // PCI-E
127 _R("RTL8101e", RTL_GIGA_MAC_VER_13, 0xff7e1880), // PCI-E 8139
128 _R("RTL8100e", RTL_GIGA_MAC_VER_14, 0xff7e1880), // PCI-E 8139
Francois Romieue3cf0cc2007-08-17 14:55:46 +0200129 _R("RTL8100e", RTL_GIGA_MAC_VER_15, 0xff7e1880), // PCI-E 8139
130 _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_17, 0xff7e1880), // PCI-E
131 _R("RTL8101e", RTL_GIGA_MAC_VER_16, 0xff7e1880), // PCI-E
132 _R("RTL8168cp/8111cp", RTL_GIGA_MAC_VER_18, 0xff7e1880), // PCI-E
133 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_19, 0xff7e1880), // PCI-E
134 _R("RTL8168c/8111c", RTL_GIGA_MAC_VER_20, 0xff7e1880) // PCI-E
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135};
136#undef _R
137
Francois Romieubcf0bf92006-07-26 23:14:13 +0200138enum cfg_version {
139 RTL_CFG_0 = 0x00,
140 RTL_CFG_1,
141 RTL_CFG_2
142};
143
Francois Romieu07ce4062007-02-23 23:36:39 +0100144static void rtl_hw_start_8169(struct net_device *);
145static void rtl_hw_start_8168(struct net_device *);
146static void rtl_hw_start_8101(struct net_device *);
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148static struct pci_device_id rtl8169_pci_tbl[] = {
Francois Romieubcf0bf92006-07-26 23:14:13 +0200149 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
Francois Romieud2eed8c2006-08-31 22:01:07 +0200150 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
Francois Romieud81bf552006-09-20 21:31:20 +0200151 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
Francois Romieu07ce4062007-02-23 23:36:39 +0100152 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200153 { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
154 { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
Francois Romieubc1660b2007-10-12 23:58:09 +0200155 { PCI_DEVICE(PCI_VENDOR_ID_AT, 0xc107), 0, 0, RTL_CFG_0 },
Francois Romieubcf0bf92006-07-26 23:14:13 +0200156 { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
157 { PCI_VENDOR_ID_LINKSYS, 0x1032,
158 PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
Ciaran McCreesh11d2e282007-11-01 22:48:15 +0100159 { 0x0001, 0x8168,
160 PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 {0,},
162};
163
164MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
165
166static int rx_copybreak = 200;
167static int use_dac;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200168static struct {
169 u32 msg_enable;
170} debug = { -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Francois Romieu07d3f512007-02-21 22:40:46 +0100172enum rtl_registers {
173 MAC0 = 0, /* Ethernet hardware address. */
Francois Romieu773d2022007-01-31 23:47:43 +0100174 MAC4 = 4,
Francois Romieu07d3f512007-02-21 22:40:46 +0100175 MAR0 = 8, /* Multicast filter. */
176 CounterAddrLow = 0x10,
177 CounterAddrHigh = 0x14,
178 TxDescStartAddrLow = 0x20,
179 TxDescStartAddrHigh = 0x24,
180 TxHDescStartAddrLow = 0x28,
181 TxHDescStartAddrHigh = 0x2c,
182 FLASH = 0x30,
183 ERSR = 0x36,
184 ChipCmd = 0x37,
185 TxPoll = 0x38,
186 IntrMask = 0x3c,
187 IntrStatus = 0x3e,
188 TxConfig = 0x40,
189 RxConfig = 0x44,
190 RxMissed = 0x4c,
191 Cfg9346 = 0x50,
192 Config0 = 0x51,
193 Config1 = 0x52,
194 Config2 = 0x53,
195 Config3 = 0x54,
196 Config4 = 0x55,
197 Config5 = 0x56,
198 MultiIntr = 0x5c,
199 PHYAR = 0x60,
Francois Romieu07d3f512007-02-21 22:40:46 +0100200 PHYstatus = 0x6c,
201 RxMaxSize = 0xda,
202 CPlusCmd = 0xe0,
203 IntrMitigate = 0xe2,
204 RxDescAddrLow = 0xe4,
205 RxDescAddrHigh = 0xe8,
206 EarlyTxThres = 0xec,
207 FuncEvent = 0xf0,
208 FuncEventMask = 0xf4,
209 FuncPresetState = 0xf8,
210 FuncForceEvent = 0xfc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211};
212
Francois Romieuf162a5d2008-06-01 22:37:49 +0200213enum rtl8110_registers {
214 TBICSR = 0x64,
215 TBI_ANAR = 0x68,
216 TBI_LPAR = 0x6a,
217};
218
219enum rtl8168_8101_registers {
220 CSIDR = 0x64,
221 CSIAR = 0x68,
222#define CSIAR_FLAG 0x80000000
223#define CSIAR_WRITE_CMD 0x80000000
224#define CSIAR_BYTE_ENABLE 0x0f
225#define CSIAR_BYTE_ENABLE_SHIFT 12
226#define CSIAR_ADDR_MASK 0x0fff
227
228 EPHYAR = 0x80,
229#define EPHYAR_FLAG 0x80000000
230#define EPHYAR_WRITE_CMD 0x80000000
231#define EPHYAR_REG_MASK 0x1f
232#define EPHYAR_REG_SHIFT 16
233#define EPHYAR_DATA_MASK 0xffff
234 DBG_REG = 0xd1,
235#define FIX_NAK_1 (1 << 4)
236#define FIX_NAK_2 (1 << 3)
237};
238
Francois Romieu07d3f512007-02-21 22:40:46 +0100239enum rtl_register_content {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 /* InterruptStatusBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100241 SYSErr = 0x8000,
242 PCSTimeout = 0x4000,
243 SWInt = 0x0100,
244 TxDescUnavail = 0x0080,
245 RxFIFOOver = 0x0040,
246 LinkChg = 0x0020,
247 RxOverflow = 0x0010,
248 TxErr = 0x0008,
249 TxOK = 0x0004,
250 RxErr = 0x0002,
251 RxOK = 0x0001,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 /* RxStatusDesc */
Francois Romieu9dccf612006-05-14 12:31:17 +0200254 RxFOVF = (1 << 23),
255 RxRWT = (1 << 22),
256 RxRES = (1 << 21),
257 RxRUNT = (1 << 20),
258 RxCRC = (1 << 19),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 /* ChipCmdBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100261 CmdReset = 0x10,
262 CmdRxEnb = 0x08,
263 CmdTxEnb = 0x04,
264 RxBufEmpty = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Francois Romieu275391a2007-02-23 23:50:28 +0100266 /* TXPoll register p.5 */
267 HPQ = 0x80, /* Poll cmd on the high prio queue */
268 NPQ = 0x40, /* Poll cmd on the low prio queue */
269 FSWInt = 0x01, /* Forced software interrupt */
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 /* Cfg9346Bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100272 Cfg9346_Lock = 0x00,
273 Cfg9346_Unlock = 0xc0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 /* rx_mode_bits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100276 AcceptErr = 0x20,
277 AcceptRunt = 0x10,
278 AcceptBroadcast = 0x08,
279 AcceptMulticast = 0x04,
280 AcceptMyPhys = 0x02,
281 AcceptAllPhys = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 /* RxConfigBits */
Francois Romieu07d3f512007-02-21 22:40:46 +0100284 RxCfgFIFOShift = 13,
285 RxCfgDMAShift = 8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 /* TxConfigBits */
288 TxInterFrameGapShift = 24,
289 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
290
Francois Romieu5d06a992006-02-23 00:47:58 +0100291 /* Config1 register p.24 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200292 LEDS1 = (1 << 7),
293 LEDS0 = (1 << 6),
Francois Romieufbac58f2007-10-04 22:51:38 +0200294 MSIEnable = (1 << 5), /* Enable Message Signaled Interrupt */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200295 Speed_down = (1 << 4),
296 MEMMAP = (1 << 3),
297 IOMAP = (1 << 2),
298 VPD = (1 << 1),
Francois Romieu5d06a992006-02-23 00:47:58 +0100299 PMEnable = (1 << 0), /* Power Management Enable */
300
Francois Romieu6dccd162007-02-13 23:38:05 +0100301 /* Config2 register p. 25 */
302 PCI_Clock_66MHz = 0x01,
303 PCI_Clock_33MHz = 0x00,
304
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100305 /* Config3 register p.25 */
306 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
307 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200308 Beacon_en = (1 << 0), /* 8168 only. Reserved in the 8168b */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100309
Francois Romieu5d06a992006-02-23 00:47:58 +0100310 /* Config5 register p.27 */
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100311 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
312 MWF = (1 << 5), /* Accept Multicast wakeup frame */
313 UWF = (1 << 4), /* Accept Unicast wakeup frame */
314 LanWake = (1 << 1), /* LanWake enable/disable */
Francois Romieu5d06a992006-02-23 00:47:58 +0100315 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 /* TBICSR p.28 */
318 TBIReset = 0x80000000,
319 TBILoopback = 0x40000000,
320 TBINwEnable = 0x20000000,
321 TBINwRestart = 0x10000000,
322 TBILinkOk = 0x02000000,
323 TBINwComplete = 0x01000000,
324
325 /* CPlusCmd p.31 */
Francois Romieuf162a5d2008-06-01 22:37:49 +0200326 EnableBist = (1 << 15), // 8168 8101
327 Mac_dbgo_oe = (1 << 14), // 8168 8101
328 Normal_mode = (1 << 13), // unused
329 Force_half_dup = (1 << 12), // 8168 8101
330 Force_rxflow_en = (1 << 11), // 8168 8101
331 Force_txflow_en = (1 << 10), // 8168 8101
332 Cxpl_dbg_sel = (1 << 9), // 8168 8101
333 ASF = (1 << 8), // 8168 8101
334 PktCntrDisable = (1 << 7), // 8168 8101
335 Mac_dbgo_sel = 0x001c, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 RxVlan = (1 << 6),
337 RxChkSum = (1 << 5),
338 PCIDAC = (1 << 4),
339 PCIMulRW = (1 << 3),
Francois Romieu0e485152007-02-20 00:00:26 +0100340 INTT_0 = 0x0000, // 8168
341 INTT_1 = 0x0001, // 8168
342 INTT_2 = 0x0002, // 8168
343 INTT_3 = 0x0003, // 8168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 /* rtl8169_PHYstatus */
Francois Romieu07d3f512007-02-21 22:40:46 +0100346 TBI_Enable = 0x80,
347 TxFlowCtrl = 0x40,
348 RxFlowCtrl = 0x20,
349 _1000bpsF = 0x10,
350 _100bps = 0x08,
351 _10bps = 0x04,
352 LinkStatus = 0x02,
353 FullDup = 0x01,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 /* _TBICSRBit */
Francois Romieu07d3f512007-02-21 22:40:46 +0100356 TBILinkOK = 0x02000000,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +0200357
358 /* DumpCounterCommand */
Francois Romieu07d3f512007-02-21 22:40:46 +0100359 CounterDump = 0x8,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360};
361
Francois Romieu07d3f512007-02-21 22:40:46 +0100362enum desc_status_bit {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
364 RingEnd = (1 << 30), /* End of descriptor ring */
365 FirstFrag = (1 << 29), /* First segment of a packet */
366 LastFrag = (1 << 28), /* Final segment of a packet */
367
368 /* Tx private */
369 LargeSend = (1 << 27), /* TCP Large Send Offload (TSO) */
370 MSSShift = 16, /* MSS value position */
371 MSSMask = 0xfff, /* MSS value + LargeSend bit: 12 bits */
372 IPCS = (1 << 18), /* Calculate IP checksum */
373 UDPCS = (1 << 17), /* Calculate UDP/IP checksum */
374 TCPCS = (1 << 16), /* Calculate TCP/IP checksum */
375 TxVlanTag = (1 << 17), /* Add VLAN tag */
376
377 /* Rx private */
378 PID1 = (1 << 18), /* Protocol ID bit 1/2 */
379 PID0 = (1 << 17), /* Protocol ID bit 2/2 */
380
381#define RxProtoUDP (PID1)
382#define RxProtoTCP (PID0)
383#define RxProtoIP (PID1 | PID0)
384#define RxProtoMask RxProtoIP
385
386 IPFail = (1 << 16), /* IP checksum failed */
387 UDPFail = (1 << 15), /* UDP/IP checksum failed */
388 TCPFail = (1 << 14), /* TCP/IP checksum failed */
389 RxVlanTag = (1 << 16), /* VLAN tag available */
390};
391
392#define RsvdMask 0x3fffc000
393
394struct TxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200395 __le32 opts1;
396 __le32 opts2;
397 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398};
399
400struct RxDesc {
Rolf Eike Beer6cccd6e2007-05-21 22:11:04 +0200401 __le32 opts1;
402 __le32 opts2;
403 __le64 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404};
405
406struct ring_info {
407 struct sk_buff *skb;
408 u32 len;
409 u8 __pad[sizeof(void *) - sizeof(u32)];
410};
411
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200412enum features {
Francois Romieuccdffb92008-07-26 14:26:06 +0200413 RTL_FEATURE_WOL = (1 << 0),
414 RTL_FEATURE_MSI = (1 << 1),
415 RTL_FEATURE_GMII = (1 << 2),
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200416};
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418struct rtl8169_private {
419 void __iomem *mmio_addr; /* memory map physical address */
420 struct pci_dev *pci_dev; /* Index of PCI device */
David Howellsc4028952006-11-22 14:57:56 +0000421 struct net_device *dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700422 struct napi_struct napi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 spinlock_t lock; /* spin lock flag */
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200424 u32 msg_enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 int chipset;
426 int mac_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
428 u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
429 u32 dirty_rx;
430 u32 dirty_tx;
431 struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
432 struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
433 dma_addr_t TxPhyAddr;
434 dma_addr_t RxPhyAddr;
435 struct sk_buff *Rx_skbuff[NUM_RX_DESC]; /* Rx data buffers */
436 struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
Francois Romieubcf0bf92006-07-26 23:14:13 +0200437 unsigned align;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 unsigned rx_buf_sz;
439 struct timer_list timer;
440 u16 cp_cmd;
Francois Romieu0e485152007-02-20 00:00:26 +0100441 u16 intr_event;
442 u16 napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 u16 intr_mask;
444 int phy_auto_nego_reg;
445 int phy_1000_ctrl_reg;
446#ifdef CONFIG_R8169_VLAN
447 struct vlan_group *vlgrp;
448#endif
449 int (*set_speed)(struct net_device *, u8 autoneg, u16 speed, u8 duplex);
Francois Romieuccdffb92008-07-26 14:26:06 +0200450 int (*get_settings)(struct net_device *, struct ethtool_cmd *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 void (*phy_reset_enable)(void __iomem *);
Francois Romieu07ce4062007-02-23 23:36:39 +0100452 void (*hw_start)(struct net_device *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 unsigned int (*phy_reset_pending)(void __iomem *);
454 unsigned int (*link_ok)(void __iomem *);
Francois Romieu9c14cea2008-07-05 00:21:15 +0200455 int pcie_cap;
David Howellsc4028952006-11-22 14:57:56 +0000456 struct delayed_work task;
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200457 unsigned features;
Francois Romieuccdffb92008-07-26 14:26:06 +0200458
459 struct mii_if_info mii;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460};
461
Ralf Baechle979b6c12005-06-13 14:30:40 -0700462MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464module_param(rx_copybreak, int, 0);
Stephen Hemminger1b7efd52005-05-27 21:11:45 +0200465MODULE_PARM_DESC(rx_copybreak, "Copy breakpoint for copy-only-tiny-frames");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466module_param(use_dac, int, 0);
467MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200468module_param_named(debug, debug.msg_enable, int, 0);
469MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470MODULE_LICENSE("GPL");
471MODULE_VERSION(RTL8169_VERSION);
472
473static int rtl8169_open(struct net_device *dev);
474static int rtl8169_start_xmit(struct sk_buff *skb, struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100475static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476static int rtl8169_init_ring(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100477static void rtl_hw_start(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478static int rtl8169_close(struct net_device *dev);
Francois Romieu07ce4062007-02-23 23:36:39 +0100479static void rtl_set_rx_mode(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480static void rtl8169_tx_timeout(struct net_device *dev);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200481static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482static int rtl8169_rx_interrupt(struct net_device *, struct rtl8169_private *,
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700483 void __iomem *, u32 budget);
Richard Dawe4dcb7d32005-05-27 21:12:00 +0200484static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485static void rtl8169_down(struct net_device *dev);
Francois Romieu99f252b2007-04-02 22:59:59 +0200486static void rtl8169_rx_clear(struct rtl8169_private *tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700487static int rtl8169_poll(struct napi_struct *napi, int budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489static const unsigned int rtl8169_rx_config =
Francois Romieu5b0384f2006-08-16 16:00:01 +0200490 (RX_FIFO_THRESH << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Francois Romieu07d3f512007-02-21 22:40:46 +0100492static void mdio_write(void __iomem *ioaddr, int reg_addr, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
494 int i;
495
Francois Romieua6baf3a2007-11-08 23:23:21 +0100496 RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Francois Romieu23714082006-01-29 00:49:09 +0100498 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100499 /*
500 * Check if the RTL8169 has completed writing to the specified
501 * MII register.
502 */
Francois Romieu5b0384f2006-08-16 16:00:01 +0200503 if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 break;
Francois Romieu23714082006-01-29 00:49:09 +0100505 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
507}
508
Francois Romieu07d3f512007-02-21 22:40:46 +0100509static int mdio_read(void __iomem *ioaddr, int reg_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
511 int i, value = -1;
512
Francois Romieua6baf3a2007-11-08 23:23:21 +0100513 RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Francois Romieu23714082006-01-29 00:49:09 +0100515 for (i = 20; i > 0; i--) {
Francois Romieu07d3f512007-02-21 22:40:46 +0100516 /*
517 * Check if the RTL8169 has completed retrieving data from
518 * the specified MII register.
519 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (RTL_R32(PHYAR) & 0x80000000) {
Francois Romieua6baf3a2007-11-08 23:23:21 +0100521 value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 break;
523 }
Francois Romieu23714082006-01-29 00:49:09 +0100524 udelay(25);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
526 return value;
527}
528
Francois Romieudacf8152008-08-02 20:44:13 +0200529static void mdio_patch(void __iomem *ioaddr, int reg_addr, int value)
530{
531 mdio_write(ioaddr, reg_addr, mdio_read(ioaddr, reg_addr) | value);
532}
533
Francois Romieuccdffb92008-07-26 14:26:06 +0200534static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
535 int val)
536{
537 struct rtl8169_private *tp = netdev_priv(dev);
538 void __iomem *ioaddr = tp->mmio_addr;
539
540 mdio_write(ioaddr, location, val);
541}
542
543static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
544{
545 struct rtl8169_private *tp = netdev_priv(dev);
546 void __iomem *ioaddr = tp->mmio_addr;
547
548 return mdio_read(ioaddr, location);
549}
550
Francois Romieudacf8152008-08-02 20:44:13 +0200551static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
552{
553 unsigned int i;
554
555 RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
556 (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
557
558 for (i = 0; i < 100; i++) {
559 if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
560 break;
561 udelay(10);
562 }
563}
564
565static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
566{
567 u16 value = 0xffff;
568 unsigned int i;
569
570 RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);
571
572 for (i = 0; i < 100; i++) {
573 if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
574 value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
575 break;
576 }
577 udelay(10);
578 }
579
580 return value;
581}
582
583static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
584{
585 unsigned int i;
586
587 RTL_W32(CSIDR, value);
588 RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
589 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
590
591 for (i = 0; i < 100; i++) {
592 if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
593 break;
594 udelay(10);
595 }
596}
597
598static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
599{
600 u32 value = ~0x00;
601 unsigned int i;
602
603 RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
604 CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);
605
606 for (i = 0; i < 100; i++) {
607 if (RTL_R32(CSIAR) & CSIAR_FLAG) {
608 value = RTL_R32(CSIDR);
609 break;
610 }
611 udelay(10);
612 }
613
614 return value;
615}
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617static void rtl8169_irq_mask_and_ack(void __iomem *ioaddr)
618{
619 RTL_W16(IntrMask, 0x0000);
620
621 RTL_W16(IntrStatus, 0xffff);
622}
623
624static void rtl8169_asic_down(void __iomem *ioaddr)
625{
626 RTL_W8(ChipCmd, 0x00);
627 rtl8169_irq_mask_and_ack(ioaddr);
628 RTL_R16(CPlusCmd);
629}
630
631static unsigned int rtl8169_tbi_reset_pending(void __iomem *ioaddr)
632{
633 return RTL_R32(TBICSR) & TBIReset;
634}
635
636static unsigned int rtl8169_xmii_reset_pending(void __iomem *ioaddr)
637{
Francois Romieu64e4bfb2006-08-17 12:43:06 +0200638 return mdio_read(ioaddr, MII_BMCR) & BMCR_RESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
641static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
642{
643 return RTL_R32(TBICSR) & TBILinkOk;
644}
645
646static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
647{
648 return RTL_R8(PHYstatus) & LinkStatus;
649}
650
651static void rtl8169_tbi_reset_enable(void __iomem *ioaddr)
652{
653 RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
654}
655
656static void rtl8169_xmii_reset_enable(void __iomem *ioaddr)
657{
658 unsigned int val;
659
Francois Romieu9e0db8e2007-03-08 23:59:54 +0100660 val = mdio_read(ioaddr, MII_BMCR) | BMCR_RESET;
661 mdio_write(ioaddr, MII_BMCR, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
664static void rtl8169_check_link_status(struct net_device *dev,
Francois Romieu07d3f512007-02-21 22:40:46 +0100665 struct rtl8169_private *tp,
666 void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 unsigned long flags;
669
670 spin_lock_irqsave(&tp->lock, flags);
671 if (tp->link_ok(ioaddr)) {
672 netif_carrier_on(dev);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200673 if (netif_msg_ifup(tp))
674 printk(KERN_INFO PFX "%s: link up\n", dev->name);
675 } else {
676 if (netif_msg_ifdown(tp))
677 printk(KERN_INFO PFX "%s: link down\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 netif_carrier_off(dev);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 spin_unlock_irqrestore(&tp->lock, flags);
681}
682
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100683static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
684{
685 struct rtl8169_private *tp = netdev_priv(dev);
686 void __iomem *ioaddr = tp->mmio_addr;
687 u8 options;
688
689 wol->wolopts = 0;
690
691#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
692 wol->supported = WAKE_ANY;
693
694 spin_lock_irq(&tp->lock);
695
696 options = RTL_R8(Config1);
697 if (!(options & PMEnable))
698 goto out_unlock;
699
700 options = RTL_R8(Config3);
701 if (options & LinkUp)
702 wol->wolopts |= WAKE_PHY;
703 if (options & MagicPacket)
704 wol->wolopts |= WAKE_MAGIC;
705
706 options = RTL_R8(Config5);
707 if (options & UWF)
708 wol->wolopts |= WAKE_UCAST;
709 if (options & BWF)
Francois Romieu5b0384f2006-08-16 16:00:01 +0200710 wol->wolopts |= WAKE_BCAST;
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100711 if (options & MWF)
Francois Romieu5b0384f2006-08-16 16:00:01 +0200712 wol->wolopts |= WAKE_MCAST;
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100713
714out_unlock:
715 spin_unlock_irq(&tp->lock);
716}
717
718static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
719{
720 struct rtl8169_private *tp = netdev_priv(dev);
721 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +0100722 unsigned int i;
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100723 static struct {
724 u32 opt;
725 u16 reg;
726 u8 mask;
727 } cfg[] = {
728 { WAKE_ANY, Config1, PMEnable },
729 { WAKE_PHY, Config3, LinkUp },
730 { WAKE_MAGIC, Config3, MagicPacket },
731 { WAKE_UCAST, Config5, UWF },
732 { WAKE_BCAST, Config5, BWF },
733 { WAKE_MCAST, Config5, MWF },
734 { WAKE_ANY, Config5, LanWake }
735 };
736
737 spin_lock_irq(&tp->lock);
738
739 RTL_W8(Cfg9346, Cfg9346_Unlock);
740
741 for (i = 0; i < ARRAY_SIZE(cfg); i++) {
742 u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
743 if (wol->wolopts & cfg[i].opt)
744 options |= cfg[i].mask;
745 RTL_W8(cfg[i].reg, options);
746 }
747
748 RTL_W8(Cfg9346, Cfg9346_Lock);
749
Francois Romieuf23e7fd2007-10-04 22:36:14 +0200750 if (wol->wolopts)
751 tp->features |= RTL_FEATURE_WOL;
752 else
753 tp->features &= ~RTL_FEATURE_WOL;
Francois Romieu61a4dcc2006-02-23 00:55:25 +0100754
755 spin_unlock_irq(&tp->lock);
756
757 return 0;
758}
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760static void rtl8169_get_drvinfo(struct net_device *dev,
761 struct ethtool_drvinfo *info)
762{
763 struct rtl8169_private *tp = netdev_priv(dev);
764
765 strcpy(info->driver, MODULENAME);
766 strcpy(info->version, RTL8169_VERSION);
767 strcpy(info->bus_info, pci_name(tp->pci_dev));
768}
769
770static int rtl8169_get_regs_len(struct net_device *dev)
771{
772 return R8169_REGS_SIZE;
773}
774
775static int rtl8169_set_speed_tbi(struct net_device *dev,
776 u8 autoneg, u16 speed, u8 duplex)
777{
778 struct rtl8169_private *tp = netdev_priv(dev);
779 void __iomem *ioaddr = tp->mmio_addr;
780 int ret = 0;
781 u32 reg;
782
783 reg = RTL_R32(TBICSR);
784 if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
785 (duplex == DUPLEX_FULL)) {
786 RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
787 } else if (autoneg == AUTONEG_ENABLE)
788 RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
789 else {
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +0200790 if (netif_msg_link(tp)) {
791 printk(KERN_WARNING "%s: "
792 "incorrect speed setting refused in TBI mode\n",
793 dev->name);
794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 ret = -EOPNOTSUPP;
796 }
797
798 return ret;
799}
800
801static int rtl8169_set_speed_xmii(struct net_device *dev,
802 u8 autoneg, u16 speed, u8 duplex)
803{
804 struct rtl8169_private *tp = netdev_priv(dev);
805 void __iomem *ioaddr = tp->mmio_addr;
806 int auto_nego, giga_ctrl;
807
Francois Romieu64e4bfb2006-08-17 12:43:06 +0200808 auto_nego = mdio_read(ioaddr, MII_ADVERTISE);
809 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
810 ADVERTISE_100HALF | ADVERTISE_100FULL);
811 giga_ctrl = mdio_read(ioaddr, MII_CTRL1000);
812 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 if (autoneg == AUTONEG_ENABLE) {
Francois Romieu64e4bfb2006-08-17 12:43:06 +0200815 auto_nego |= (ADVERTISE_10HALF | ADVERTISE_10FULL |
816 ADVERTISE_100HALF | ADVERTISE_100FULL);
817 giga_ctrl |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 } else {
819 if (speed == SPEED_10)
Francois Romieu64e4bfb2006-08-17 12:43:06 +0200820 auto_nego |= ADVERTISE_10HALF | ADVERTISE_10FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 else if (speed == SPEED_100)
Francois Romieu64e4bfb2006-08-17 12:43:06 +0200822 auto_nego |= ADVERTISE_100HALF | ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 else if (speed == SPEED_1000)
Francois Romieu64e4bfb2006-08-17 12:43:06 +0200824 giga_ctrl |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 if (duplex == DUPLEX_HALF)
Francois Romieu64e4bfb2006-08-17 12:43:06 +0200827 auto_nego &= ~(ADVERTISE_10FULL | ADVERTISE_100FULL);
Andy Gospodarek726ecdc2006-01-31 19:16:52 +0100828
829 if (duplex == DUPLEX_FULL)
Francois Romieu64e4bfb2006-08-17 12:43:06 +0200830 auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_100HALF);
Francois Romieubcf0bf92006-07-26 23:14:13 +0200831
832 /* This tweak comes straight from Realtek's driver. */
833 if ((speed == SPEED_100) && (duplex == DUPLEX_HALF) &&
Francois Romieue3cf0cc2007-08-17 14:55:46 +0200834 ((tp->mac_version == RTL_GIGA_MAC_VER_13) ||
835 (tp->mac_version == RTL_GIGA_MAC_VER_16))) {
Francois Romieu64e4bfb2006-08-17 12:43:06 +0200836 auto_nego = ADVERTISE_100HALF | ADVERTISE_CSMA;
Francois Romieubcf0bf92006-07-26 23:14:13 +0200837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
839
Francois Romieubcf0bf92006-07-26 23:14:13 +0200840 /* The 8100e/8101e do Fast Ethernet only. */
841 if ((tp->mac_version == RTL_GIGA_MAC_VER_13) ||
842 (tp->mac_version == RTL_GIGA_MAC_VER_14) ||
Francois Romieue3cf0cc2007-08-17 14:55:46 +0200843 (tp->mac_version == RTL_GIGA_MAC_VER_15) ||
844 (tp->mac_version == RTL_GIGA_MAC_VER_16)) {
Francois Romieu64e4bfb2006-08-17 12:43:06 +0200845 if ((giga_ctrl & (ADVERTISE_1000FULL | ADVERTISE_1000HALF)) &&
Francois Romieubcf0bf92006-07-26 23:14:13 +0200846 netif_msg_link(tp)) {
847 printk(KERN_INFO "%s: PHY does not support 1000Mbps.\n",
848 dev->name);
849 }
Francois Romieu64e4bfb2006-08-17 12:43:06 +0200850 giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 }
852
Francois Romieu623a1592006-05-14 12:42:14 +0200853 auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
854
Francois Romieue3cf0cc2007-08-17 14:55:46 +0200855 if ((tp->mac_version == RTL_GIGA_MAC_VER_12) ||
856 (tp->mac_version == RTL_GIGA_MAC_VER_17)) {
Roger So2584fbc2007-07-31 23:52:42 +0200857 /* Vendor specific (0x1f) and reserved (0x0e) MII registers. */
858 mdio_write(ioaddr, 0x1f, 0x0000);
859 mdio_write(ioaddr, 0x0e, 0x0000);
860 }
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 tp->phy_auto_nego_reg = auto_nego;
863 tp->phy_1000_ctrl_reg = giga_ctrl;
864
Francois Romieu64e4bfb2006-08-17 12:43:06 +0200865 mdio_write(ioaddr, MII_ADVERTISE, auto_nego);
866 mdio_write(ioaddr, MII_CTRL1000, giga_ctrl);
867 mdio_write(ioaddr, MII_BMCR, BMCR_ANENABLE | BMCR_ANRESTART);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 return 0;
869}
870
871static int rtl8169_set_speed(struct net_device *dev,
872 u8 autoneg, u16 speed, u8 duplex)
873{
874 struct rtl8169_private *tp = netdev_priv(dev);
875 int ret;
876
877 ret = tp->set_speed(dev, autoneg, speed, duplex);
878
Francois Romieu64e4bfb2006-08-17 12:43:06 +0200879 if (netif_running(dev) && (tp->phy_1000_ctrl_reg & ADVERTISE_1000FULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
881
882 return ret;
883}
884
885static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
886{
887 struct rtl8169_private *tp = netdev_priv(dev);
888 unsigned long flags;
889 int ret;
890
891 spin_lock_irqsave(&tp->lock, flags);
892 ret = rtl8169_set_speed(dev, cmd->autoneg, cmd->speed, cmd->duplex);
893 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieu5b0384f2006-08-16 16:00:01 +0200894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return ret;
896}
897
898static u32 rtl8169_get_rx_csum(struct net_device *dev)
899{
900 struct rtl8169_private *tp = netdev_priv(dev);
901
902 return tp->cp_cmd & RxChkSum;
903}
904
905static int rtl8169_set_rx_csum(struct net_device *dev, u32 data)
906{
907 struct rtl8169_private *tp = netdev_priv(dev);
908 void __iomem *ioaddr = tp->mmio_addr;
909 unsigned long flags;
910
911 spin_lock_irqsave(&tp->lock, flags);
912
913 if (data)
914 tp->cp_cmd |= RxChkSum;
915 else
916 tp->cp_cmd &= ~RxChkSum;
917
918 RTL_W16(CPlusCmd, tp->cp_cmd);
919 RTL_R16(CPlusCmd);
920
921 spin_unlock_irqrestore(&tp->lock, flags);
922
923 return 0;
924}
925
926#ifdef CONFIG_R8169_VLAN
927
928static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
929 struct sk_buff *skb)
930{
931 return (tp->vlgrp && vlan_tx_tag_present(skb)) ?
932 TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
933}
934
935static void rtl8169_vlan_rx_register(struct net_device *dev,
936 struct vlan_group *grp)
937{
938 struct rtl8169_private *tp = netdev_priv(dev);
939 void __iomem *ioaddr = tp->mmio_addr;
940 unsigned long flags;
941
942 spin_lock_irqsave(&tp->lock, flags);
943 tp->vlgrp = grp;
944 if (tp->vlgrp)
945 tp->cp_cmd |= RxVlan;
946 else
947 tp->cp_cmd &= ~RxVlan;
948 RTL_W16(CPlusCmd, tp->cp_cmd);
949 RTL_R16(CPlusCmd);
950 spin_unlock_irqrestore(&tp->lock, flags);
951}
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953static int rtl8169_rx_vlan_skb(struct rtl8169_private *tp, struct RxDesc *desc,
954 struct sk_buff *skb)
955{
956 u32 opts2 = le32_to_cpu(desc->opts2);
Francois Romieu865c6522008-05-11 14:51:00 +0200957 struct vlan_group *vlgrp = tp->vlgrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 int ret;
959
Francois Romieu865c6522008-05-11 14:51:00 +0200960 if (vlgrp && (opts2 & RxVlanTag)) {
961 vlan_hwaccel_receive_skb(skb, vlgrp, swab16(opts2 & 0xffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 ret = 0;
963 } else
964 ret = -1;
965 desc->opts2 = 0;
966 return ret;
967}
968
969#else /* !CONFIG_R8169_VLAN */
970
971static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
972 struct sk_buff *skb)
973{
974 return 0;
975}
976
977static int rtl8169_rx_vlan_skb(struct rtl8169_private *tp, struct RxDesc *desc,
978 struct sk_buff *skb)
979{
980 return -1;
981}
982
983#endif
984
Francois Romieuccdffb92008-07-26 14:26:06 +0200985static int rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
987 struct rtl8169_private *tp = netdev_priv(dev);
988 void __iomem *ioaddr = tp->mmio_addr;
989 u32 status;
990
991 cmd->supported =
992 SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
993 cmd->port = PORT_FIBRE;
994 cmd->transceiver = XCVR_INTERNAL;
995
996 status = RTL_R32(TBICSR);
997 cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
998 cmd->autoneg = !!(status & TBINwEnable);
999
1000 cmd->speed = SPEED_1000;
1001 cmd->duplex = DUPLEX_FULL; /* Always set */
Francois Romieuccdffb92008-07-26 14:26:06 +02001002
1003 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004}
1005
Francois Romieuccdffb92008-07-26 14:26:06 +02001006static int rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
1008 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Francois Romieuccdffb92008-07-26 14:26:06 +02001010 return mii_ethtool_gset(&tp->mii, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011}
1012
1013static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1014{
1015 struct rtl8169_private *tp = netdev_priv(dev);
1016 unsigned long flags;
Francois Romieuccdffb92008-07-26 14:26:06 +02001017 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 spin_lock_irqsave(&tp->lock, flags);
1020
Francois Romieuccdffb92008-07-26 14:26:06 +02001021 rc = tp->get_settings(dev, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 spin_unlock_irqrestore(&tp->lock, flags);
Francois Romieuccdffb92008-07-26 14:26:06 +02001024 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025}
1026
1027static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1028 void *p)
1029{
Francois Romieu5b0384f2006-08-16 16:00:01 +02001030 struct rtl8169_private *tp = netdev_priv(dev);
1031 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Francois Romieu5b0384f2006-08-16 16:00:01 +02001033 if (regs->len > R8169_REGS_SIZE)
1034 regs->len = R8169_REGS_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Francois Romieu5b0384f2006-08-16 16:00:01 +02001036 spin_lock_irqsave(&tp->lock, flags);
1037 memcpy_fromio(p, tp->mmio_addr, regs->len);
1038 spin_unlock_irqrestore(&tp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039}
1040
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001041static u32 rtl8169_get_msglevel(struct net_device *dev)
1042{
1043 struct rtl8169_private *tp = netdev_priv(dev);
1044
1045 return tp->msg_enable;
1046}
1047
1048static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
1049{
1050 struct rtl8169_private *tp = netdev_priv(dev);
1051
1052 tp->msg_enable = value;
1053}
1054
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001055static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
1056 "tx_packets",
1057 "rx_packets",
1058 "tx_errors",
1059 "rx_errors",
1060 "rx_missed",
1061 "align_errors",
1062 "tx_single_collisions",
1063 "tx_multi_collisions",
1064 "unicast",
1065 "broadcast",
1066 "multicast",
1067 "tx_aborted",
1068 "tx_underrun",
1069};
1070
1071struct rtl8169_counters {
Al Virob1eab702007-08-23 02:30:16 -04001072 __le64 tx_packets;
1073 __le64 rx_packets;
1074 __le64 tx_errors;
1075 __le32 rx_errors;
1076 __le16 rx_missed;
1077 __le16 align_errors;
1078 __le32 tx_one_collision;
1079 __le32 tx_multi_collision;
1080 __le64 rx_unicast;
1081 __le64 rx_broadcast;
1082 __le32 rx_multicast;
1083 __le16 tx_aborted;
1084 __le16 tx_underun;
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001085};
1086
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001087static int rtl8169_get_sset_count(struct net_device *dev, int sset)
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001088{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001089 switch (sset) {
1090 case ETH_SS_STATS:
1091 return ARRAY_SIZE(rtl8169_gstrings);
1092 default:
1093 return -EOPNOTSUPP;
1094 }
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001095}
1096
1097static void rtl8169_get_ethtool_stats(struct net_device *dev,
1098 struct ethtool_stats *stats, u64 *data)
1099{
1100 struct rtl8169_private *tp = netdev_priv(dev);
1101 void __iomem *ioaddr = tp->mmio_addr;
1102 struct rtl8169_counters *counters;
1103 dma_addr_t paddr;
1104 u32 cmd;
1105
1106 ASSERT_RTNL();
1107
1108 counters = pci_alloc_consistent(tp->pci_dev, sizeof(*counters), &paddr);
1109 if (!counters)
1110 return;
1111
1112 RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
1113 cmd = (u64)paddr & DMA_32BIT_MASK;
1114 RTL_W32(CounterAddrLow, cmd);
1115 RTL_W32(CounterAddrLow, cmd | CounterDump);
1116
1117 while (RTL_R32(CounterAddrLow) & CounterDump) {
1118 if (msleep_interruptible(1))
1119 break;
1120 }
1121
1122 RTL_W32(CounterAddrLow, 0);
1123 RTL_W32(CounterAddrHigh, 0);
1124
Francois Romieu5b0384f2006-08-16 16:00:01 +02001125 data[0] = le64_to_cpu(counters->tx_packets);
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001126 data[1] = le64_to_cpu(counters->rx_packets);
1127 data[2] = le64_to_cpu(counters->tx_errors);
1128 data[3] = le32_to_cpu(counters->rx_errors);
1129 data[4] = le16_to_cpu(counters->rx_missed);
1130 data[5] = le16_to_cpu(counters->align_errors);
1131 data[6] = le32_to_cpu(counters->tx_one_collision);
1132 data[7] = le32_to_cpu(counters->tx_multi_collision);
1133 data[8] = le64_to_cpu(counters->rx_unicast);
1134 data[9] = le64_to_cpu(counters->rx_broadcast);
1135 data[10] = le32_to_cpu(counters->rx_multicast);
1136 data[11] = le16_to_cpu(counters->tx_aborted);
1137 data[12] = le16_to_cpu(counters->tx_underun);
1138
1139 pci_free_consistent(tp->pci_dev, sizeof(*counters), counters, paddr);
1140}
1141
1142static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1143{
1144 switch(stringset) {
1145 case ETH_SS_STATS:
1146 memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
1147 break;
1148 }
1149}
1150
Jeff Garzik7282d492006-09-13 14:30:00 -04001151static const struct ethtool_ops rtl8169_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 .get_drvinfo = rtl8169_get_drvinfo,
1153 .get_regs_len = rtl8169_get_regs_len,
1154 .get_link = ethtool_op_get_link,
1155 .get_settings = rtl8169_get_settings,
1156 .set_settings = rtl8169_set_settings,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001157 .get_msglevel = rtl8169_get_msglevel,
1158 .set_msglevel = rtl8169_set_msglevel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 .get_rx_csum = rtl8169_get_rx_csum,
1160 .set_rx_csum = rtl8169_set_rx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 .set_tx_csum = ethtool_op_set_tx_csum,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 .set_sg = ethtool_op_set_sg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 .set_tso = ethtool_op_set_tso,
1164 .get_regs = rtl8169_get_regs,
Francois Romieu61a4dcc2006-02-23 00:55:25 +01001165 .get_wol = rtl8169_get_wol,
1166 .set_wol = rtl8169_set_wol,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001167 .get_strings = rtl8169_get_strings,
Jeff Garzikb9f2c042007-10-03 18:07:32 -07001168 .get_sset_count = rtl8169_get_sset_count,
Stephen Hemmingerd4a3a0f2005-05-27 21:11:56 +02001169 .get_ethtool_stats = rtl8169_get_ethtool_stats,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170};
1171
Francois Romieu07d3f512007-02-21 22:40:46 +01001172static void rtl8169_write_gmii_reg_bit(void __iomem *ioaddr, int reg,
1173 int bitnum, int bitval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
1175 int val;
1176
1177 val = mdio_read(ioaddr, reg);
1178 val = (bitval == 1) ?
1179 val | (bitval << bitnum) : val & ~(0x0001 << bitnum);
Francois Romieu5b0384f2006-08-16 16:00:01 +02001180 mdio_write(ioaddr, reg, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181}
1182
Francois Romieu07d3f512007-02-21 22:40:46 +01001183static void rtl8169_get_mac_version(struct rtl8169_private *tp,
1184 void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
Francois Romieu0e485152007-02-20 00:00:26 +01001186 /*
1187 * The driver currently handles the 8168Bf and the 8168Be identically
1188 * but they can be identified more specifically through the test below
1189 * if needed:
1190 *
1191 * (RTL_R32(TxConfig) & 0x700000) == 0x500000 ? 8168Bf : 8168Be
Francois Romieu01272152007-02-20 22:58:51 +01001192 *
1193 * Same thing for the 8101Eb and the 8101Ec:
1194 *
1195 * (RTL_R32(TxConfig) & 0x700000) == 0x200000 ? 8101Eb : 8101Ec
Francois Romieu0e485152007-02-20 00:00:26 +01001196 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 const struct {
1198 u32 mask;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001199 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 int mac_version;
1201 } mac_info[] = {
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001202 /* 8168B family. */
1203 { 0x7c800000, 0x3c800000, RTL_GIGA_MAC_VER_18 },
1204 { 0x7cf00000, 0x3c000000, RTL_GIGA_MAC_VER_19 },
1205 { 0x7cf00000, 0x3c200000, RTL_GIGA_MAC_VER_20 },
1206 { 0x7c800000, 0x3c000000, RTL_GIGA_MAC_VER_20 },
1207
1208 /* 8168B family. */
1209 { 0x7cf00000, 0x38000000, RTL_GIGA_MAC_VER_12 },
1210 { 0x7cf00000, 0x38500000, RTL_GIGA_MAC_VER_17 },
1211 { 0x7c800000, 0x38000000, RTL_GIGA_MAC_VER_17 },
1212 { 0x7c800000, 0x30000000, RTL_GIGA_MAC_VER_11 },
1213
1214 /* 8101 family. */
1215 { 0x7cf00000, 0x34000000, RTL_GIGA_MAC_VER_13 },
1216 { 0x7cf00000, 0x34200000, RTL_GIGA_MAC_VER_16 },
1217 { 0x7c800000, 0x34000000, RTL_GIGA_MAC_VER_16 },
1218 /* FIXME: where did these entries come from ? -- FR */
1219 { 0xfc800000, 0x38800000, RTL_GIGA_MAC_VER_15 },
1220 { 0xfc800000, 0x30800000, RTL_GIGA_MAC_VER_14 },
1221
1222 /* 8110 family. */
1223 { 0xfc800000, 0x98000000, RTL_GIGA_MAC_VER_06 },
1224 { 0xfc800000, 0x18000000, RTL_GIGA_MAC_VER_05 },
1225 { 0xfc800000, 0x10000000, RTL_GIGA_MAC_VER_04 },
1226 { 0xfc800000, 0x04000000, RTL_GIGA_MAC_VER_03 },
1227 { 0xfc800000, 0x00800000, RTL_GIGA_MAC_VER_02 },
1228 { 0xfc800000, 0x00000000, RTL_GIGA_MAC_VER_01 },
1229
1230 { 0x00000000, 0x00000000, RTL_GIGA_MAC_VER_01 } /* Catch-all */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }, *p = mac_info;
1232 u32 reg;
1233
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001234 reg = RTL_R32(TxConfig);
1235 while ((reg & p->mask) != p->val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 p++;
1237 tp->mac_version = p->mac_version;
Francois Romieue3cf0cc2007-08-17 14:55:46 +02001238
1239 if (p->mask == 0x00000000) {
1240 struct pci_dev *pdev = tp->pci_dev;
1241
1242 dev_info(&pdev->dev, "unknown MAC (%08x)\n", reg);
1243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245
1246static void rtl8169_print_mac_version(struct rtl8169_private *tp)
1247{
Francois Romieubcf0bf92006-07-26 23:14:13 +02001248 dprintk("mac_version = 0x%02x\n", tp->mac_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
Francois Romieu867763c2007-08-17 18:21:58 +02001251struct phy_reg {
1252 u16 reg;
1253 u16 val;
1254};
1255
1256static void rtl_phy_write(void __iomem *ioaddr, struct phy_reg *regs, int len)
1257{
1258 while (len-- > 0) {
1259 mdio_write(ioaddr, regs->reg, regs->val);
1260 regs++;
1261 }
1262}
1263
Francois Romieu5615d9f2007-08-17 17:50:46 +02001264static void rtl8169s_hw_phy_config(void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 struct {
1267 u16 regs[5]; /* Beware of bit-sign propagation */
1268 } phy_magic[5] = { {
1269 { 0x0000, //w 4 15 12 0
1270 0x00a1, //w 3 15 0 00a1
1271 0x0008, //w 2 15 0 0008
1272 0x1020, //w 1 15 0 1020
1273 0x1000 } },{ //w 0 15 0 1000
1274 { 0x7000, //w 4 15 12 7
1275 0xff41, //w 3 15 0 ff41
1276 0xde60, //w 2 15 0 de60
1277 0x0140, //w 1 15 0 0140
1278 0x0077 } },{ //w 0 15 0 0077
1279 { 0xa000, //w 4 15 12 a
1280 0xdf01, //w 3 15 0 df01
1281 0xdf20, //w 2 15 0 df20
1282 0xff95, //w 1 15 0 ff95
1283 0xfa00 } },{ //w 0 15 0 fa00
1284 { 0xb000, //w 4 15 12 b
1285 0xff41, //w 3 15 0 ff41
1286 0xde20, //w 2 15 0 de20
1287 0x0140, //w 1 15 0 0140
1288 0x00bb } },{ //w 0 15 0 00bb
1289 { 0xf000, //w 4 15 12 f
1290 0xdf01, //w 3 15 0 df01
1291 0xdf20, //w 2 15 0 df20
1292 0xff95, //w 1 15 0 ff95
1293 0xbf00 } //w 0 15 0 bf00
1294 }
1295 }, *p = phy_magic;
Francois Romieu07d3f512007-02-21 22:40:46 +01001296 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Francois Romieua441d7b2007-08-17 18:26:35 +02001298 mdio_write(ioaddr, 0x1f, 0x0001); //w 31 2 0 1
1299 mdio_write(ioaddr, 0x15, 0x1000); //w 21 15 0 1000
1300 mdio_write(ioaddr, 0x18, 0x65c7); //w 24 15 0 65c7
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 rtl8169_write_gmii_reg_bit(ioaddr, 4, 11, 0); //w 4 11 11 0
1302
1303 for (i = 0; i < ARRAY_SIZE(phy_magic); i++, p++) {
1304 int val, pos = 4;
1305
1306 val = (mdio_read(ioaddr, pos) & 0x0fff) | (p->regs[0] & 0xffff);
1307 mdio_write(ioaddr, pos, val);
1308 while (--pos >= 0)
1309 mdio_write(ioaddr, pos, p->regs[4 - pos] & 0xffff);
1310 rtl8169_write_gmii_reg_bit(ioaddr, 4, 11, 1); //w 4 11 11 1
1311 rtl8169_write_gmii_reg_bit(ioaddr, 4, 11, 0); //w 4 11 11 0
1312 }
Francois Romieua441d7b2007-08-17 18:26:35 +02001313 mdio_write(ioaddr, 0x1f, 0x0000); //w 31 2 0 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314}
1315
Francois Romieu5615d9f2007-08-17 17:50:46 +02001316static void rtl8169sb_hw_phy_config(void __iomem *ioaddr)
1317{
Francois Romieua441d7b2007-08-17 18:26:35 +02001318 struct phy_reg phy_reg_init[] = {
1319 { 0x1f, 0x0002 },
1320 { 0x01, 0x90d0 },
1321 { 0x1f, 0x0000 }
1322 };
1323
1324 rtl_phy_write(ioaddr, phy_reg_init, ARRAY_SIZE(phy_reg_init));
Francois Romieu5615d9f2007-08-17 17:50:46 +02001325}
1326
Francois Romieu867763c2007-08-17 18:21:58 +02001327static void rtl8168cp_hw_phy_config(void __iomem *ioaddr)
1328{
1329 struct phy_reg phy_reg_init[] = {
1330 { 0x1f, 0x0000 },
1331 { 0x1d, 0x0f00 },
1332 { 0x1f, 0x0002 },
1333 { 0x0c, 0x1ec8 },
1334 { 0x1f, 0x0000 }
1335 };
1336
1337 rtl_phy_write(ioaddr, phy_reg_init, ARRAY_SIZE(phy_reg_init));
1338}
1339
1340static void rtl8168c_hw_phy_config(void __iomem *ioaddr)
1341{
1342 struct phy_reg phy_reg_init[] = {
Francois Romieua3f80672007-10-18 14:35:11 +02001343 { 0x1f, 0x0001 },
1344 { 0x12, 0x2300 },
Francois Romieu867763c2007-08-17 18:21:58 +02001345 { 0x1f, 0x0002 },
1346 { 0x00, 0x88d4 },
1347 { 0x01, 0x82b1 },
1348 { 0x03, 0x7002 },
1349 { 0x08, 0x9e30 },
1350 { 0x09, 0x01f0 },
1351 { 0x0a, 0x5500 },
1352 { 0x0c, 0x00c8 },
1353 { 0x1f, 0x0003 },
1354 { 0x12, 0xc096 },
1355 { 0x16, 0x000a },
1356 { 0x1f, 0x0000 }
1357 };
1358
1359 rtl_phy_write(ioaddr, phy_reg_init, ARRAY_SIZE(phy_reg_init));
1360}
1361
Francois Romieu7da97ec2007-10-18 15:20:43 +02001362static void rtl8168cx_hw_phy_config(void __iomem *ioaddr)
1363{
1364 struct phy_reg phy_reg_init[] = {
1365 { 0x1f, 0x0000 },
1366 { 0x12, 0x2300 },
1367 { 0x1f, 0x0003 },
1368 { 0x16, 0x0f0a },
1369 { 0x1f, 0x0000 },
1370 { 0x1f, 0x0002 },
1371 { 0x0c, 0x7eb8 },
1372 { 0x1f, 0x0000 }
1373 };
1374
1375 rtl_phy_write(ioaddr, phy_reg_init, ARRAY_SIZE(phy_reg_init));
1376}
1377
Francois Romieu5615d9f2007-08-17 17:50:46 +02001378static void rtl_hw_phy_config(struct net_device *dev)
1379{
1380 struct rtl8169_private *tp = netdev_priv(dev);
1381 void __iomem *ioaddr = tp->mmio_addr;
1382
1383 rtl8169_print_mac_version(tp);
1384
1385 switch (tp->mac_version) {
1386 case RTL_GIGA_MAC_VER_01:
1387 break;
1388 case RTL_GIGA_MAC_VER_02:
1389 case RTL_GIGA_MAC_VER_03:
1390 rtl8169s_hw_phy_config(ioaddr);
1391 break;
1392 case RTL_GIGA_MAC_VER_04:
1393 rtl8169sb_hw_phy_config(ioaddr);
1394 break;
Francois Romieu867763c2007-08-17 18:21:58 +02001395 case RTL_GIGA_MAC_VER_18:
1396 rtl8168cp_hw_phy_config(ioaddr);
1397 break;
1398 case RTL_GIGA_MAC_VER_19:
1399 rtl8168c_hw_phy_config(ioaddr);
1400 break;
Francois Romieu7da97ec2007-10-18 15:20:43 +02001401 case RTL_GIGA_MAC_VER_20:
1402 rtl8168cx_hw_phy_config(ioaddr);
1403 break;
Francois Romieu5615d9f2007-08-17 17:50:46 +02001404 default:
1405 break;
1406 }
1407}
1408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409static void rtl8169_phy_timer(unsigned long __opaque)
1410{
1411 struct net_device *dev = (struct net_device *)__opaque;
1412 struct rtl8169_private *tp = netdev_priv(dev);
1413 struct timer_list *timer = &tp->timer;
1414 void __iomem *ioaddr = tp->mmio_addr;
1415 unsigned long timeout = RTL8169_PHY_TIMEOUT;
1416
Francois Romieubcf0bf92006-07-26 23:14:13 +02001417 assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Francois Romieu64e4bfb2006-08-17 12:43:06 +02001419 if (!(tp->phy_1000_ctrl_reg & ADVERTISE_1000FULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return;
1421
1422 spin_lock_irq(&tp->lock);
1423
1424 if (tp->phy_reset_pending(ioaddr)) {
Francois Romieu5b0384f2006-08-16 16:00:01 +02001425 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 * A busy loop could burn quite a few cycles on nowadays CPU.
1427 * Let's delay the execution of the timer for a few ticks.
1428 */
1429 timeout = HZ/10;
1430 goto out_mod_timer;
1431 }
1432
1433 if (tp->link_ok(ioaddr))
1434 goto out_unlock;
1435
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001436 if (netif_msg_link(tp))
1437 printk(KERN_WARNING "%s: PHY reset until link up\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
1439 tp->phy_reset_enable(ioaddr);
1440
1441out_mod_timer:
1442 mod_timer(timer, jiffies + timeout);
1443out_unlock:
1444 spin_unlock_irq(&tp->lock);
1445}
1446
1447static inline void rtl8169_delete_timer(struct net_device *dev)
1448{
1449 struct rtl8169_private *tp = netdev_priv(dev);
1450 struct timer_list *timer = &tp->timer;
1451
Francois Romieue179bb72007-08-17 15:05:21 +02001452 if (tp->mac_version <= RTL_GIGA_MAC_VER_01)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 return;
1454
1455 del_timer_sync(timer);
1456}
1457
1458static inline void rtl8169_request_timer(struct net_device *dev)
1459{
1460 struct rtl8169_private *tp = netdev_priv(dev);
1461 struct timer_list *timer = &tp->timer;
1462
Francois Romieue179bb72007-08-17 15:05:21 +02001463 if (tp->mac_version <= RTL_GIGA_MAC_VER_01)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 return;
1465
Francois Romieu2efa53f2007-03-09 00:00:05 +01001466 mod_timer(timer, jiffies + RTL8169_PHY_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467}
1468
1469#ifdef CONFIG_NET_POLL_CONTROLLER
1470/*
1471 * Polling 'interrupt' - used by things like netconsole to send skbs
1472 * without having to re-enable interrupts. It's not called while
1473 * the interrupt routine is executing.
1474 */
1475static void rtl8169_netpoll(struct net_device *dev)
1476{
1477 struct rtl8169_private *tp = netdev_priv(dev);
1478 struct pci_dev *pdev = tp->pci_dev;
1479
1480 disable_irq(pdev->irq);
David Howells7d12e782006-10-05 14:55:46 +01001481 rtl8169_interrupt(pdev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 enable_irq(pdev->irq);
1483}
1484#endif
1485
1486static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
1487 void __iomem *ioaddr)
1488{
1489 iounmap(ioaddr);
1490 pci_release_regions(pdev);
1491 pci_disable_device(pdev);
1492 free_netdev(dev);
1493}
1494
Francois Romieubf793292006-11-01 00:53:05 +01001495static void rtl8169_phy_reset(struct net_device *dev,
1496 struct rtl8169_private *tp)
1497{
1498 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001499 unsigned int i;
Francois Romieubf793292006-11-01 00:53:05 +01001500
1501 tp->phy_reset_enable(ioaddr);
1502 for (i = 0; i < 100; i++) {
1503 if (!tp->phy_reset_pending(ioaddr))
1504 return;
1505 msleep(1);
1506 }
1507 if (netif_msg_link(tp))
1508 printk(KERN_ERR "%s: PHY reset failed.\n", dev->name);
1509}
1510
Francois Romieu4ff96fa2006-07-26 22:05:06 +02001511static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512{
Francois Romieu4ff96fa2006-07-26 22:05:06 +02001513 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02001514
Francois Romieu5615d9f2007-08-17 17:50:46 +02001515 rtl_hw_phy_config(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02001516
Marcus Sundberg773328942008-07-10 21:28:08 +02001517 if (tp->mac_version <= RTL_GIGA_MAC_VER_06) {
1518 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
1519 RTL_W8(0x82, 0x01);
1520 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02001521
Francois Romieu6dccd162007-02-13 23:38:05 +01001522 pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
1523
1524 if (tp->mac_version <= RTL_GIGA_MAC_VER_06)
1525 pci_write_config_byte(tp->pci_dev, PCI_CACHE_LINE_SIZE, 0x08);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02001526
Francois Romieubcf0bf92006-07-26 23:14:13 +02001527 if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02001528 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
1529 RTL_W8(0x82, 0x01);
1530 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
1531 mdio_write(ioaddr, 0x0b, 0x0000); //w 0x0b 15 0 0
1532 }
1533
Francois Romieubf793292006-11-01 00:53:05 +01001534 rtl8169_phy_reset(dev, tp);
1535
Francois Romieu901dda22007-02-21 00:10:20 +01001536 /*
1537 * rtl8169_set_speed_xmii takes good care of the Fast Ethernet
1538 * only 8101. Don't panic.
1539 */
1540 rtl8169_set_speed(dev, AUTONEG_ENABLE, SPEED_1000, DUPLEX_FULL);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02001541
1542 if ((RTL_R8(PHYstatus) & TBI_Enable) && netif_msg_link(tp))
1543 printk(KERN_INFO PFX "%s: TBI auto-negotiating\n", dev->name);
1544}
1545
Francois Romieu773d2022007-01-31 23:47:43 +01001546static void rtl_rar_set(struct rtl8169_private *tp, u8 *addr)
1547{
1548 void __iomem *ioaddr = tp->mmio_addr;
1549 u32 high;
1550 u32 low;
1551
1552 low = addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24);
1553 high = addr[4] | (addr[5] << 8);
1554
1555 spin_lock_irq(&tp->lock);
1556
1557 RTL_W8(Cfg9346, Cfg9346_Unlock);
1558 RTL_W32(MAC0, low);
1559 RTL_W32(MAC4, high);
1560 RTL_W8(Cfg9346, Cfg9346_Lock);
1561
1562 spin_unlock_irq(&tp->lock);
1563}
1564
1565static int rtl_set_mac_address(struct net_device *dev, void *p)
1566{
1567 struct rtl8169_private *tp = netdev_priv(dev);
1568 struct sockaddr *addr = p;
1569
1570 if (!is_valid_ether_addr(addr->sa_data))
1571 return -EADDRNOTAVAIL;
1572
1573 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1574
1575 rtl_rar_set(tp, dev->dev_addr);
1576
1577 return 0;
1578}
1579
Francois Romieu5f787a12006-08-17 13:02:36 +02001580static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1581{
1582 struct rtl8169_private *tp = netdev_priv(dev);
1583 struct mii_ioctl_data *data = if_mii(ifr);
1584
1585 if (!netif_running(dev))
1586 return -ENODEV;
1587
1588 switch (cmd) {
1589 case SIOCGMIIPHY:
1590 data->phy_id = 32; /* Internal PHY */
1591 return 0;
1592
1593 case SIOCGMIIREG:
1594 data->val_out = mdio_read(tp->mmio_addr, data->reg_num & 0x1f);
1595 return 0;
1596
1597 case SIOCSMIIREG:
1598 if (!capable(CAP_NET_ADMIN))
1599 return -EPERM;
1600 mdio_write(tp->mmio_addr, data->reg_num & 0x1f, data->val_in);
1601 return 0;
1602 }
1603 return -EOPNOTSUPP;
1604}
1605
Francois Romieu0e485152007-02-20 00:00:26 +01001606static const struct rtl_cfg_info {
1607 void (*hw_start)(struct net_device *);
1608 unsigned int region;
1609 unsigned int align;
1610 u16 intr_event;
1611 u16 napi_event;
Francois Romieuccdffb92008-07-26 14:26:06 +02001612 unsigned features;
Francois Romieu0e485152007-02-20 00:00:26 +01001613} rtl_cfg_infos [] = {
1614 [RTL_CFG_0] = {
1615 .hw_start = rtl_hw_start_8169,
1616 .region = 1,
Francois Romieue9f63f32007-02-28 23:16:57 +01001617 .align = 0,
Francois Romieu0e485152007-02-20 00:00:26 +01001618 .intr_event = SYSErr | LinkChg | RxOverflow |
1619 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02001620 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Francois Romieuccdffb92008-07-26 14:26:06 +02001621 .features = RTL_FEATURE_GMII
Francois Romieu0e485152007-02-20 00:00:26 +01001622 },
1623 [RTL_CFG_1] = {
1624 .hw_start = rtl_hw_start_8168,
1625 .region = 2,
1626 .align = 8,
1627 .intr_event = SYSErr | LinkChg | RxOverflow |
1628 TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02001629 .napi_event = TxErr | TxOK | RxOK | RxOverflow,
Francois Romieuccdffb92008-07-26 14:26:06 +02001630 .features = RTL_FEATURE_GMII | RTL_FEATURE_MSI
Francois Romieu0e485152007-02-20 00:00:26 +01001631 },
1632 [RTL_CFG_2] = {
1633 .hw_start = rtl_hw_start_8101,
1634 .region = 2,
1635 .align = 8,
1636 .intr_event = SYSErr | LinkChg | RxOverflow | PCSTimeout |
1637 RxFIFOOver | TxErr | TxOK | RxOK | RxErr,
Francois Romieufbac58f2007-10-04 22:51:38 +02001638 .napi_event = RxFIFOOver | TxErr | TxOK | RxOK | RxOverflow,
Francois Romieuccdffb92008-07-26 14:26:06 +02001639 .features = RTL_FEATURE_MSI
Francois Romieu0e485152007-02-20 00:00:26 +01001640 }
1641};
1642
Francois Romieufbac58f2007-10-04 22:51:38 +02001643/* Cfg9346_Unlock assumed. */
1644static unsigned rtl_try_msi(struct pci_dev *pdev, void __iomem *ioaddr,
1645 const struct rtl_cfg_info *cfg)
1646{
1647 unsigned msi = 0;
1648 u8 cfg2;
1649
1650 cfg2 = RTL_R8(Config2) & ~MSIEnable;
Francois Romieuccdffb92008-07-26 14:26:06 +02001651 if (cfg->features & RTL_FEATURE_MSI) {
Francois Romieufbac58f2007-10-04 22:51:38 +02001652 if (pci_enable_msi(pdev)) {
1653 dev_info(&pdev->dev, "no MSI. Back to INTx.\n");
1654 } else {
1655 cfg2 |= MSIEnable;
1656 msi = RTL_FEATURE_MSI;
1657 }
1658 }
1659 RTL_W8(Config2, cfg2);
1660 return msi;
1661}
1662
1663static void rtl_disable_msi(struct pci_dev *pdev, struct rtl8169_private *tp)
1664{
1665 if (tp->features & RTL_FEATURE_MSI) {
1666 pci_disable_msi(pdev);
1667 tp->features &= ~RTL_FEATURE_MSI;
1668 }
1669}
1670
Francois Romieu4ff96fa2006-07-26 22:05:06 +02001671static int __devinit
1672rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
1673{
Francois Romieu0e485152007-02-20 00:00:26 +01001674 const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
1675 const unsigned int region = cfg->region;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 struct rtl8169_private *tp;
Francois Romieuccdffb92008-07-26 14:26:06 +02001677 struct mii_if_info *mii;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02001678 struct net_device *dev;
1679 void __iomem *ioaddr;
Francois Romieu07d3f512007-02-21 22:40:46 +01001680 unsigned int i;
1681 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Francois Romieu4ff96fa2006-07-26 22:05:06 +02001683 if (netif_msg_drv(&debug)) {
1684 printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
1685 MODULENAME, RTL8169_VERSION);
1686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 dev = alloc_etherdev(sizeof (*tp));
Francois Romieu4ff96fa2006-07-26 22:05:06 +02001689 if (!dev) {
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001690 if (netif_msg_drv(&debug))
Jeff Garzik9b91cf92006-06-27 11:39:50 -04001691 dev_err(&pdev->dev, "unable to alloc new ethernet\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02001692 rc = -ENOMEM;
1693 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 }
1695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 SET_NETDEV_DEV(dev, &pdev->dev);
1697 tp = netdev_priv(dev);
David Howellsc4028952006-11-22 14:57:56 +00001698 tp->dev = dev;
Ivan Vecera21e197f2008-04-17 22:48:41 +02001699 tp->pci_dev = pdev;
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001700 tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Francois Romieuccdffb92008-07-26 14:26:06 +02001702 mii = &tp->mii;
1703 mii->dev = dev;
1704 mii->mdio_read = rtl_mdio_read;
1705 mii->mdio_write = rtl_mdio_write;
1706 mii->phy_id_mask = 0x1f;
1707 mii->reg_num_mask = 0x1f;
1708 mii->supports_gmii = !!(cfg->features & RTL_FEATURE_GMII);
1709
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 /* enable device (incl. PCI PM wakeup and hotplug setup) */
1711 rc = pci_enable_device(pdev);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001712 if (rc < 0) {
Jeff Garzik2e8a5382006-06-27 10:47:51 -04001713 if (netif_msg_probe(tp))
Jeff Garzik9b91cf92006-06-27 11:39:50 -04001714 dev_err(&pdev->dev, "enable failure\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02001715 goto err_out_free_dev_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 }
1717
1718 rc = pci_set_mwi(pdev);
1719 if (rc < 0)
Francois Romieu4ff96fa2006-07-26 22:05:06 +02001720 goto err_out_disable_2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 /* make sure PCI base addr 1 is MMIO */
Francois Romieubcf0bf92006-07-26 23:14:13 +02001723 if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02001724 if (netif_msg_probe(tp)) {
Jeff Garzik9b91cf92006-06-27 11:39:50 -04001725 dev_err(&pdev->dev,
Francois Romieubcf0bf92006-07-26 23:14:13 +02001726 "region #%d not an MMIO resource, aborting\n",
1727 region);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02001728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 rc = -ENODEV;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02001730 goto err_out_mwi_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 }
Francois Romieu4ff96fa2006-07-26 22:05:06 +02001732
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 /* check for weird/broken PCI region reporting */
Francois Romieubcf0bf92006-07-26 23:14:13 +02001734 if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02001735 if (netif_msg_probe(tp)) {
Jeff Garzik9b91cf92006-06-27 11:39:50 -04001736 dev_err(&pdev->dev,
Francois Romieu4ff96fa2006-07-26 22:05:06 +02001737 "Invalid PCI region size(s), aborting\n");
1738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 rc = -ENODEV;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02001740 goto err_out_mwi_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 }
1742
1743 rc = pci_request_regions(pdev, MODULENAME);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001744 if (rc < 0) {
Jeff Garzik2e8a5382006-06-27 10:47:51 -04001745 if (netif_msg_probe(tp))
Jeff Garzik9b91cf92006-06-27 11:39:50 -04001746 dev_err(&pdev->dev, "could not request regions.\n");
Francois Romieu4ff96fa2006-07-26 22:05:06 +02001747 goto err_out_mwi_3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 }
1749
1750 tp->cp_cmd = PCIMulRW | RxChkSum;
1751
1752 if ((sizeof(dma_addr_t) > 4) &&
1753 !pci_set_dma_mask(pdev, DMA_64BIT_MASK) && use_dac) {
1754 tp->cp_cmd |= PCIDAC;
1755 dev->features |= NETIF_F_HIGHDMA;
1756 } else {
1757 rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
1758 if (rc < 0) {
Francois Romieu4ff96fa2006-07-26 22:05:06 +02001759 if (netif_msg_probe(tp)) {
Jeff Garzik9b91cf92006-06-27 11:39:50 -04001760 dev_err(&pdev->dev,
Francois Romieu4ff96fa2006-07-26 22:05:06 +02001761 "DMA configuration failed.\n");
1762 }
1763 goto err_out_free_res_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 }
1765 }
1766
1767 pci_set_master(pdev);
1768
1769 /* ioremap MMIO region */
Francois Romieubcf0bf92006-07-26 23:14:13 +02001770 ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02001771 if (!ioaddr) {
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001772 if (netif_msg_probe(tp))
Jeff Garzik9b91cf92006-06-27 11:39:50 -04001773 dev_err(&pdev->dev, "cannot remap MMIO, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 rc = -EIO;
Francois Romieu4ff96fa2006-07-26 22:05:06 +02001775 goto err_out_free_res_4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 }
1777
Francois Romieu9c14cea2008-07-05 00:21:15 +02001778 tp->pcie_cap = pci_find_capability(pdev, PCI_CAP_ID_EXP);
1779 if (!tp->pcie_cap && netif_msg_probe(tp))
1780 dev_info(&pdev->dev, "no PCI Express capability\n");
1781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 /* Unneeded ? Don't mess with Mrs. Murphy. */
1783 rtl8169_irq_mask_and_ack(ioaddr);
1784
1785 /* Soft reset the chip. */
1786 RTL_W8(ChipCmd, CmdReset);
1787
1788 /* Check that the chip has finished the reset. */
Francois Romieu07d3f512007-02-21 22:40:46 +01001789 for (i = 0; i < 100; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
1791 break;
Francois Romieub518fa82006-08-16 15:23:13 +02001792 msleep_interruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 }
1794
1795 /* Identify chip attached to board */
1796 rtl8169_get_mac_version(tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
1798 rtl8169_print_mac_version(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
Roel Kluincee60c32008-04-17 22:35:54 +02001800 for (i = 0; i < ARRAY_SIZE(rtl_chip_info); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 if (tp->mac_version == rtl_chip_info[i].mac_version)
1802 break;
1803 }
Roel Kluincee60c32008-04-17 22:35:54 +02001804 if (i == ARRAY_SIZE(rtl_chip_info)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 /* Unknown chip: assume array element #0, original RTL-8169 */
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001806 if (netif_msg_probe(tp)) {
Jeff Garzik2e8a5382006-06-27 10:47:51 -04001807 dev_printk(KERN_DEBUG, &pdev->dev,
Francois Romieu4ff96fa2006-07-26 22:05:06 +02001808 "unknown chip version, assuming %s\n",
1809 rtl_chip_info[0].name);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001810 }
Roel Kluincee60c32008-04-17 22:35:54 +02001811 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 }
1813 tp->chipset = i;
1814
Francois Romieu5d06a992006-02-23 00:47:58 +01001815 RTL_W8(Cfg9346, Cfg9346_Unlock);
1816 RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
1817 RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
Francois Romieufbac58f2007-10-04 22:51:38 +02001818 tp->features |= rtl_try_msi(pdev, ioaddr, cfg);
Francois Romieu5d06a992006-02-23 00:47:58 +01001819 RTL_W8(Cfg9346, Cfg9346_Lock);
1820
Francois Romieu66ec5d42007-11-06 22:56:10 +01001821 if ((tp->mac_version <= RTL_GIGA_MAC_VER_06) &&
1822 (RTL_R8(PHYstatus) & TBI_Enable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 tp->set_speed = rtl8169_set_speed_tbi;
1824 tp->get_settings = rtl8169_gset_tbi;
1825 tp->phy_reset_enable = rtl8169_tbi_reset_enable;
1826 tp->phy_reset_pending = rtl8169_tbi_reset_pending;
1827 tp->link_ok = rtl8169_tbi_link_ok;
1828
Francois Romieu64e4bfb2006-08-17 12:43:06 +02001829 tp->phy_1000_ctrl_reg = ADVERTISE_1000FULL; /* Implied by TBI */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 } else {
1831 tp->set_speed = rtl8169_set_speed_xmii;
1832 tp->get_settings = rtl8169_gset_xmii;
1833 tp->phy_reset_enable = rtl8169_xmii_reset_enable;
1834 tp->phy_reset_pending = rtl8169_xmii_reset_pending;
1835 tp->link_ok = rtl8169_xmii_link_ok;
Francois Romieu5f787a12006-08-17 13:02:36 +02001836
1837 dev->do_ioctl = rtl8169_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 }
1839
1840 /* Get MAC address. FIXME: read EEPROM */
1841 for (i = 0; i < MAC_ADDR_LEN; i++)
1842 dev->dev_addr[i] = RTL_R8(MAC0 + i);
John W. Linville6d6525b2005-09-12 10:48:57 -04001843 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
1845 dev->open = rtl8169_open;
1846 dev->hard_start_xmit = rtl8169_start_xmit;
1847 dev->get_stats = rtl8169_get_stats;
1848 SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
1849 dev->stop = rtl8169_close;
1850 dev->tx_timeout = rtl8169_tx_timeout;
Francois Romieu07ce4062007-02-23 23:36:39 +01001851 dev->set_multicast_list = rtl_set_rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
1853 dev->irq = pdev->irq;
1854 dev->base_addr = (unsigned long) ioaddr;
1855 dev->change_mtu = rtl8169_change_mtu;
Francois Romieu773d2022007-01-31 23:47:43 +01001856 dev->set_mac_address = rtl_set_mac_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001858 netif_napi_add(dev, &tp->napi, rtl8169_poll, R8169_NAPI_WEIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
1860#ifdef CONFIG_R8169_VLAN
1861 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
1862 dev->vlan_rx_register = rtl8169_vlan_rx_register;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863#endif
1864
1865#ifdef CONFIG_NET_POLL_CONTROLLER
1866 dev->poll_controller = rtl8169_netpoll;
1867#endif
1868
1869 tp->intr_mask = 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 tp->mmio_addr = ioaddr;
Francois Romieu0e485152007-02-20 00:00:26 +01001871 tp->align = cfg->align;
1872 tp->hw_start = cfg->hw_start;
1873 tp->intr_event = cfg->intr_event;
1874 tp->napi_event = cfg->napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Francois Romieu2efa53f2007-03-09 00:00:05 +01001876 init_timer(&tp->timer);
1877 tp->timer.data = (unsigned long) dev;
1878 tp->timer.function = rtl8169_phy_timer;
1879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 spin_lock_init(&tp->lock);
1881
1882 rc = register_netdev(dev);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02001883 if (rc < 0)
Francois Romieufbac58f2007-10-04 22:51:38 +02001884 goto err_out_msi_5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
1886 pci_set_drvdata(pdev, dev);
1887
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001888 if (netif_msg_probe(tp)) {
Francois Romieu96b97092007-05-30 00:32:05 +02001889 u32 xid = RTL_R32(TxConfig) & 0x7cf0f8ff;
1890
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001891 printk(KERN_INFO "%s: %s at 0x%lx, "
1892 "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x, "
Francois Romieu96b97092007-05-30 00:32:05 +02001893 "XID %08x IRQ %d\n",
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001894 dev->name,
Francois Romieubcf0bf92006-07-26 23:14:13 +02001895 rtl_chip_info[tp->chipset].name,
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001896 dev->base_addr,
1897 dev->dev_addr[0], dev->dev_addr[1],
1898 dev->dev_addr[2], dev->dev_addr[3],
Francois Romieu96b97092007-05-30 00:32:05 +02001899 dev->dev_addr[4], dev->dev_addr[5], xid, dev->irq);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02001900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Francois Romieu4ff96fa2006-07-26 22:05:06 +02001902 rtl8169_init_phy(dev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Francois Romieu4ff96fa2006-07-26 22:05:06 +02001904out:
1905 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
Francois Romieufbac58f2007-10-04 22:51:38 +02001907err_out_msi_5:
1908 rtl_disable_msi(pdev, tp);
Francois Romieu4ff96fa2006-07-26 22:05:06 +02001909 iounmap(ioaddr);
1910err_out_free_res_4:
1911 pci_release_regions(pdev);
1912err_out_mwi_3:
1913 pci_clear_mwi(pdev);
1914err_out_disable_2:
1915 pci_disable_device(pdev);
1916err_out_free_dev_1:
1917 free_netdev(dev);
1918 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919}
1920
Francois Romieu07d3f512007-02-21 22:40:46 +01001921static void __devexit rtl8169_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922{
1923 struct net_device *dev = pci_get_drvdata(pdev);
1924 struct rtl8169_private *tp = netdev_priv(dev);
1925
Francois Romieueb2a0212007-02-15 23:37:21 +01001926 flush_scheduled_work();
1927
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 unregister_netdev(dev);
Francois Romieufbac58f2007-10-04 22:51:38 +02001929 rtl_disable_msi(pdev, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 rtl8169_release_board(pdev, dev, tp->mmio_addr);
1931 pci_set_drvdata(pdev, NULL);
1932}
1933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934static void rtl8169_set_rxbufsize(struct rtl8169_private *tp,
1935 struct net_device *dev)
1936{
1937 unsigned int mtu = dev->mtu;
1938
1939 tp->rx_buf_sz = (mtu > RX_BUF_SIZE) ? mtu + ETH_HLEN + 8 : RX_BUF_SIZE;
1940}
1941
1942static int rtl8169_open(struct net_device *dev)
1943{
1944 struct rtl8169_private *tp = netdev_priv(dev);
1945 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu99f252b2007-04-02 22:59:59 +02001946 int retval = -ENOMEM;
1947
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
1949 rtl8169_set_rxbufsize(tp, dev);
1950
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 /*
1952 * Rx and Tx desscriptors needs 256 bytes alignment.
1953 * pci_alloc_consistent provides more.
1954 */
1955 tp->TxDescArray = pci_alloc_consistent(pdev, R8169_TX_RING_BYTES,
1956 &tp->TxPhyAddr);
1957 if (!tp->TxDescArray)
Francois Romieu99f252b2007-04-02 22:59:59 +02001958 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
1960 tp->RxDescArray = pci_alloc_consistent(pdev, R8169_RX_RING_BYTES,
1961 &tp->RxPhyAddr);
1962 if (!tp->RxDescArray)
Francois Romieu99f252b2007-04-02 22:59:59 +02001963 goto err_free_tx_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
1965 retval = rtl8169_init_ring(dev);
1966 if (retval < 0)
Francois Romieu99f252b2007-04-02 22:59:59 +02001967 goto err_free_rx_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
David Howellsc4028952006-11-22 14:57:56 +00001969 INIT_DELAYED_WORK(&tp->task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Francois Romieu99f252b2007-04-02 22:59:59 +02001971 smp_mb();
1972
Francois Romieufbac58f2007-10-04 22:51:38 +02001973 retval = request_irq(dev->irq, rtl8169_interrupt,
1974 (tp->features & RTL_FEATURE_MSI) ? 0 : IRQF_SHARED,
Francois Romieu99f252b2007-04-02 22:59:59 +02001975 dev->name, dev);
1976 if (retval < 0)
1977 goto err_release_ring_2;
1978
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001979 napi_enable(&tp->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001980
Francois Romieu07ce4062007-02-23 23:36:39 +01001981 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
1983 rtl8169_request_timer(dev);
1984
1985 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
1986out:
1987 return retval;
1988
Francois Romieu99f252b2007-04-02 22:59:59 +02001989err_release_ring_2:
1990 rtl8169_rx_clear(tp);
1991err_free_rx_1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 pci_free_consistent(pdev, R8169_RX_RING_BYTES, tp->RxDescArray,
1993 tp->RxPhyAddr);
Francois Romieu99f252b2007-04-02 22:59:59 +02001994err_free_tx_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 pci_free_consistent(pdev, R8169_TX_RING_BYTES, tp->TxDescArray,
1996 tp->TxPhyAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 goto out;
1998}
1999
2000static void rtl8169_hw_reset(void __iomem *ioaddr)
2001{
2002 /* Disable interrupts */
2003 rtl8169_irq_mask_and_ack(ioaddr);
2004
2005 /* Reset the chipset */
2006 RTL_W8(ChipCmd, CmdReset);
2007
2008 /* PCI commit */
2009 RTL_R8(ChipCmd);
2010}
2011
Francois Romieu7f796d82007-06-11 23:04:41 +02002012static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
Francois Romieu9cb427b2006-11-02 00:10:16 +01002013{
2014 void __iomem *ioaddr = tp->mmio_addr;
2015 u32 cfg = rtl8169_rx_config;
2016
2017 cfg |= (RTL_R32(RxConfig) & rtl_chip_info[tp->chipset].RxConfigMask);
2018 RTL_W32(RxConfig, cfg);
2019
2020 /* Set DMA burst size and Interframe Gap Time */
2021 RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
2022 (InterFrameGap << TxInterFrameGapShift));
2023}
2024
Francois Romieu07ce4062007-02-23 23:36:39 +01002025static void rtl_hw_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026{
2027 struct rtl8169_private *tp = netdev_priv(dev);
2028 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu07d3f512007-02-21 22:40:46 +01002029 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
2031 /* Soft reset the chip. */
2032 RTL_W8(ChipCmd, CmdReset);
2033
2034 /* Check that the chip has finished the reset. */
Francois Romieu07d3f512007-02-21 22:40:46 +01002035 for (i = 0; i < 100; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 if ((RTL_R8(ChipCmd) & CmdReset) == 0)
2037 break;
Francois Romieub518fa82006-08-16 15:23:13 +02002038 msleep_interruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 }
2040
Francois Romieu07ce4062007-02-23 23:36:39 +01002041 tp->hw_start(dev);
2042
Francois Romieu07ce4062007-02-23 23:36:39 +01002043 netif_start_queue(dev);
2044}
2045
2046
Francois Romieu7f796d82007-06-11 23:04:41 +02002047static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
2048 void __iomem *ioaddr)
2049{
2050 /*
2051 * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
2052 * register to be written before TxDescAddrLow to work.
2053 * Switching from MMIO to I/O access fixes the issue as well.
2054 */
2055 RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
2056 RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_32BIT_MASK);
2057 RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
2058 RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_32BIT_MASK);
2059}
2060
2061static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
2062{
2063 u16 cmd;
2064
2065 cmd = RTL_R16(CPlusCmd);
2066 RTL_W16(CPlusCmd, cmd);
2067 return cmd;
2068}
2069
2070static void rtl_set_rx_max_size(void __iomem *ioaddr)
2071{
2072 /* Low hurts. Let's disable the filtering. */
2073 RTL_W16(RxMaxSize, 16383);
2074}
2075
Francois Romieu6dccd162007-02-13 23:38:05 +01002076static void rtl8169_set_magic_reg(void __iomem *ioaddr, unsigned mac_version)
2077{
2078 struct {
2079 u32 mac_version;
2080 u32 clk;
2081 u32 val;
2082 } cfg2_info [] = {
2083 { RTL_GIGA_MAC_VER_05, PCI_Clock_33MHz, 0x000fff00 }, // 8110SCd
2084 { RTL_GIGA_MAC_VER_05, PCI_Clock_66MHz, 0x000fffff },
2085 { RTL_GIGA_MAC_VER_06, PCI_Clock_33MHz, 0x00ffff00 }, // 8110SCe
2086 { RTL_GIGA_MAC_VER_06, PCI_Clock_66MHz, 0x00ffffff }
2087 }, *p = cfg2_info;
2088 unsigned int i;
2089 u32 clk;
2090
2091 clk = RTL_R8(Config2) & PCI_Clock_66MHz;
Francois Romieucadf1852008-01-03 23:38:38 +01002092 for (i = 0; i < ARRAY_SIZE(cfg2_info); i++, p++) {
Francois Romieu6dccd162007-02-13 23:38:05 +01002093 if ((p->mac_version == mac_version) && (p->clk == clk)) {
2094 RTL_W32(0x7c, p->val);
2095 break;
2096 }
2097 }
2098}
2099
Francois Romieu07ce4062007-02-23 23:36:39 +01002100static void rtl_hw_start_8169(struct net_device *dev)
2101{
2102 struct rtl8169_private *tp = netdev_priv(dev);
2103 void __iomem *ioaddr = tp->mmio_addr;
2104 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu07ce4062007-02-23 23:36:39 +01002105
Francois Romieu9cb427b2006-11-02 00:10:16 +01002106 if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
2107 RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
2108 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
2109 }
2110
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 RTL_W8(Cfg9346, Cfg9346_Unlock);
Francois Romieu9cb427b2006-11-02 00:10:16 +01002112 if ((tp->mac_version == RTL_GIGA_MAC_VER_01) ||
2113 (tp->mac_version == RTL_GIGA_MAC_VER_02) ||
2114 (tp->mac_version == RTL_GIGA_MAC_VER_03) ||
2115 (tp->mac_version == RTL_GIGA_MAC_VER_04))
2116 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
2117
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 RTL_W8(EarlyTxThres, EarlyTxThld);
2119
Francois Romieu7f796d82007-06-11 23:04:41 +02002120 rtl_set_rx_max_size(ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
Francois Romieuc946b302007-10-04 00:42:50 +02002122 if ((tp->mac_version == RTL_GIGA_MAC_VER_01) ||
2123 (tp->mac_version == RTL_GIGA_MAC_VER_02) ||
2124 (tp->mac_version == RTL_GIGA_MAC_VER_03) ||
2125 (tp->mac_version == RTL_GIGA_MAC_VER_04))
2126 rtl_set_rx_tx_config_registers(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
Francois Romieu7f796d82007-06-11 23:04:41 +02002128 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
Francois Romieubcf0bf92006-07-26 23:14:13 +02002129
2130 if ((tp->mac_version == RTL_GIGA_MAC_VER_02) ||
2131 (tp->mac_version == RTL_GIGA_MAC_VER_03)) {
Joe Perches06fa7352007-10-18 21:15:00 +02002132 dprintk("Set MAC Reg C+CR Offset 0xE0. "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 "Bit-3 and bit-14 MUST be 1\n");
Francois Romieubcf0bf92006-07-26 23:14:13 +02002134 tp->cp_cmd |= (1 << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 }
2136
Francois Romieubcf0bf92006-07-26 23:14:13 +02002137 RTL_W16(CPlusCmd, tp->cp_cmd);
2138
Francois Romieu6dccd162007-02-13 23:38:05 +01002139 rtl8169_set_magic_reg(ioaddr, tp->mac_version);
2140
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 /*
2142 * Undocumented corner. Supposedly:
2143 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
2144 */
2145 RTL_W16(IntrMitigate, 0x0000);
2146
Francois Romieu7f796d82007-06-11 23:04:41 +02002147 rtl_set_rx_tx_desc_registers(tp, ioaddr);
Francois Romieu9cb427b2006-11-02 00:10:16 +01002148
Francois Romieuc946b302007-10-04 00:42:50 +02002149 if ((tp->mac_version != RTL_GIGA_MAC_VER_01) &&
2150 (tp->mac_version != RTL_GIGA_MAC_VER_02) &&
2151 (tp->mac_version != RTL_GIGA_MAC_VER_03) &&
2152 (tp->mac_version != RTL_GIGA_MAC_VER_04)) {
2153 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
2154 rtl_set_rx_tx_config_registers(tp);
2155 }
2156
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 RTL_W8(Cfg9346, Cfg9346_Lock);
Francois Romieub518fa82006-08-16 15:23:13 +02002158
2159 /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
2160 RTL_R8(IntrMask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
2162 RTL_W32(RxMissed, 0);
2163
Francois Romieu07ce4062007-02-23 23:36:39 +01002164 rtl_set_rx_mode(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
2166 /* no early-rx interrupts */
2167 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01002168
2169 /* Enable all known interrupts by setting the interrupt mask. */
Francois Romieu0e485152007-02-20 00:00:26 +01002170 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01002171}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
Francois Romieu9c14cea2008-07-05 00:21:15 +02002173static void rtl_tx_performance_tweak(struct pci_dev *pdev, u16 force)
Francois Romieu458a9f62008-08-02 15:50:02 +02002174{
Francois Romieu9c14cea2008-07-05 00:21:15 +02002175 struct net_device *dev = pci_get_drvdata(pdev);
2176 struct rtl8169_private *tp = netdev_priv(dev);
2177 int cap = tp->pcie_cap;
Francois Romieu458a9f62008-08-02 15:50:02 +02002178
Francois Romieu9c14cea2008-07-05 00:21:15 +02002179 if (cap) {
2180 u16 ctl;
2181
2182 pci_read_config_word(pdev, cap + PCI_EXP_DEVCTL, &ctl);
2183 ctl = (ctl & ~PCI_EXP_DEVCTL_READRQ) | force;
2184 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL, ctl);
2185 }
Francois Romieu458a9f62008-08-02 15:50:02 +02002186}
2187
Francois Romieudacf8152008-08-02 20:44:13 +02002188static void rtl_csi_access_enable(void __iomem *ioaddr)
2189{
2190 u32 csi;
2191
2192 csi = rtl_csi_read(ioaddr, 0x070c) & 0x00ffffff;
2193 rtl_csi_write(ioaddr, 0x070c, csi | 0x27000000);
2194}
2195
2196struct ephy_info {
2197 unsigned int offset;
2198 u16 mask;
2199 u16 bits;
2200};
2201
2202static void rtl_ephy_init(void __iomem *ioaddr, struct ephy_info *e, int len)
2203{
2204 u16 w;
2205
2206 while (len-- > 0) {
2207 w = (rtl_ephy_read(ioaddr, e->offset) & ~e->mask) | e->bits;
2208 rtl_ephy_write(ioaddr, e->offset, w);
2209 e++;
2210 }
2211}
2212
Francois Romieu07ce4062007-02-23 23:36:39 +01002213static void rtl_hw_start_8168(struct net_device *dev)
2214{
Francois Romieu2dd99532007-06-11 23:22:52 +02002215 struct rtl8169_private *tp = netdev_priv(dev);
2216 void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu0e485152007-02-20 00:00:26 +01002217 struct pci_dev *pdev = tp->pci_dev;
Francois Romieu2dd99532007-06-11 23:22:52 +02002218
2219 RTL_W8(Cfg9346, Cfg9346_Unlock);
2220
2221 RTL_W8(EarlyTxThres, EarlyTxThld);
2222
2223 rtl_set_rx_max_size(ioaddr);
2224
Francois Romieu0e485152007-02-20 00:00:26 +01002225 rtl_set_rx_tx_config_registers(tp);
2226
2227 tp->cp_cmd |= RTL_R16(CPlusCmd) | PktCntrDisable | INTT_1;
Francois Romieu2dd99532007-06-11 23:22:52 +02002228
2229 RTL_W16(CPlusCmd, tp->cp_cmd);
2230
Francois Romieu9c14cea2008-07-05 00:21:15 +02002231 rtl_tx_performance_tweak(pdev, 0x5 << MAX_READ_REQUEST_SHIFT);
Francois Romieu0e485152007-02-20 00:00:26 +01002232
2233 RTL_W16(IntrMitigate, 0x5151);
2234
2235 /* Work around for RxFIFO overflow. */
2236 if (tp->mac_version == RTL_GIGA_MAC_VER_11) {
2237 tp->intr_event |= RxFIFOOver | PCSTimeout;
2238 tp->intr_event &= ~RxOverflow;
2239 }
Francois Romieu2dd99532007-06-11 23:22:52 +02002240
2241 rtl_set_rx_tx_desc_registers(tp, ioaddr);
2242
Francois Romieu2dd99532007-06-11 23:22:52 +02002243 RTL_W8(Cfg9346, Cfg9346_Lock);
2244
2245 RTL_R8(IntrMask);
2246
2247 RTL_W32(RxMissed, 0);
2248
2249 rtl_set_rx_mode(dev);
2250
Francois Romieu0e485152007-02-20 00:00:26 +01002251 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
2252
Francois Romieu2dd99532007-06-11 23:22:52 +02002253 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
Francois Romieu6dccd162007-02-13 23:38:05 +01002254
Francois Romieu0e485152007-02-20 00:00:26 +01002255 RTL_W16(IntrMask, tp->intr_event);
Francois Romieu07ce4062007-02-23 23:36:39 +01002256}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
Francois Romieu07ce4062007-02-23 23:36:39 +01002258static void rtl_hw_start_8101(struct net_device *dev)
2259{
Francois Romieucdf1a602007-06-11 23:29:50 +02002260 struct rtl8169_private *tp = netdev_priv(dev);
2261 void __iomem *ioaddr = tp->mmio_addr;
2262 struct pci_dev *pdev = tp->pci_dev;
2263
Francois Romieue3cf0cc2007-08-17 14:55:46 +02002264 if ((tp->mac_version == RTL_GIGA_MAC_VER_13) ||
2265 (tp->mac_version == RTL_GIGA_MAC_VER_16)) {
Francois Romieu9c14cea2008-07-05 00:21:15 +02002266 int cap = tp->pcie_cap;
2267
2268 if (cap) {
2269 pci_write_config_word(pdev, cap + PCI_EXP_DEVCTL,
2270 PCI_EXP_DEVCTL_NOSNOOP_EN);
2271 }
Francois Romieucdf1a602007-06-11 23:29:50 +02002272 }
2273
2274 RTL_W8(Cfg9346, Cfg9346_Unlock);
2275
2276 RTL_W8(EarlyTxThres, EarlyTxThld);
2277
2278 rtl_set_rx_max_size(ioaddr);
2279
2280 tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
2281
2282 RTL_W16(CPlusCmd, tp->cp_cmd);
2283
2284 RTL_W16(IntrMitigate, 0x0000);
2285
2286 rtl_set_rx_tx_desc_registers(tp, ioaddr);
2287
2288 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
2289 rtl_set_rx_tx_config_registers(tp);
2290
2291 RTL_W8(Cfg9346, Cfg9346_Lock);
2292
2293 RTL_R8(IntrMask);
2294
2295 RTL_W32(RxMissed, 0);
2296
2297 rtl_set_rx_mode(dev);
2298
Francois Romieu0e485152007-02-20 00:00:26 +01002299 RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
2300
Francois Romieucdf1a602007-06-11 23:29:50 +02002301 RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xf000);
Francois Romieu6dccd162007-02-13 23:38:05 +01002302
Francois Romieu0e485152007-02-20 00:00:26 +01002303 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304}
2305
2306static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
2307{
2308 struct rtl8169_private *tp = netdev_priv(dev);
2309 int ret = 0;
2310
2311 if (new_mtu < ETH_ZLEN || new_mtu > SafeMtu)
2312 return -EINVAL;
2313
2314 dev->mtu = new_mtu;
2315
2316 if (!netif_running(dev))
2317 goto out;
2318
2319 rtl8169_down(dev);
2320
2321 rtl8169_set_rxbufsize(tp, dev);
2322
2323 ret = rtl8169_init_ring(dev);
2324 if (ret < 0)
2325 goto out;
2326
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002327 napi_enable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
Francois Romieu07ce4062007-02-23 23:36:39 +01002329 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
2331 rtl8169_request_timer(dev);
2332
2333out:
2334 return ret;
2335}
2336
2337static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
2338{
Al Viro95e09182007-12-22 18:55:39 +00002339 desc->addr = cpu_to_le64(0x0badbadbadbadbadull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
2341}
2342
2343static void rtl8169_free_rx_skb(struct rtl8169_private *tp,
2344 struct sk_buff **sk_buff, struct RxDesc *desc)
2345{
2346 struct pci_dev *pdev = tp->pci_dev;
2347
2348 pci_unmap_single(pdev, le64_to_cpu(desc->addr), tp->rx_buf_sz,
2349 PCI_DMA_FROMDEVICE);
2350 dev_kfree_skb(*sk_buff);
2351 *sk_buff = NULL;
2352 rtl8169_make_unusable_by_asic(desc);
2353}
2354
2355static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
2356{
2357 u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
2358
2359 desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
2360}
2361
2362static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
2363 u32 rx_buf_sz)
2364{
2365 desc->addr = cpu_to_le64(mapping);
2366 wmb();
2367 rtl8169_mark_to_asic(desc, rx_buf_sz);
2368}
2369
Stephen Hemminger15d31752007-06-16 22:36:41 +02002370static struct sk_buff *rtl8169_alloc_rx_skb(struct pci_dev *pdev,
2371 struct net_device *dev,
2372 struct RxDesc *desc, int rx_buf_sz,
2373 unsigned int align)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374{
2375 struct sk_buff *skb;
2376 dma_addr_t mapping;
Francois Romieue9f63f32007-02-28 23:16:57 +01002377 unsigned int pad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
Francois Romieue9f63f32007-02-28 23:16:57 +01002379 pad = align ? align : NET_IP_ALIGN;
2380
2381 skb = netdev_alloc_skb(dev, rx_buf_sz + pad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 if (!skb)
2383 goto err_out;
2384
Francois Romieue9f63f32007-02-28 23:16:57 +01002385 skb_reserve(skb, align ? ((pad - 1) & (unsigned long)skb->data) : pad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
David S. Miller689be432005-06-28 15:25:31 -07002387 mapping = pci_map_single(pdev, skb->data, rx_buf_sz,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 PCI_DMA_FROMDEVICE);
2389
2390 rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391out:
Stephen Hemminger15d31752007-06-16 22:36:41 +02002392 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
2394err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 rtl8169_make_unusable_by_asic(desc);
2396 goto out;
2397}
2398
2399static void rtl8169_rx_clear(struct rtl8169_private *tp)
2400{
Francois Romieu07d3f512007-02-21 22:40:46 +01002401 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
2403 for (i = 0; i < NUM_RX_DESC; i++) {
2404 if (tp->Rx_skbuff[i]) {
2405 rtl8169_free_rx_skb(tp, tp->Rx_skbuff + i,
2406 tp->RxDescArray + i);
2407 }
2408 }
2409}
2410
2411static u32 rtl8169_rx_fill(struct rtl8169_private *tp, struct net_device *dev,
2412 u32 start, u32 end)
2413{
2414 u32 cur;
Francois Romieu5b0384f2006-08-16 16:00:01 +02002415
Francois Romieu4ae47c22007-06-16 23:28:45 +02002416 for (cur = start; end - cur != 0; cur++) {
Stephen Hemminger15d31752007-06-16 22:36:41 +02002417 struct sk_buff *skb;
2418 unsigned int i = cur % NUM_RX_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
Francois Romieu4ae47c22007-06-16 23:28:45 +02002420 WARN_ON((s32)(end - cur) < 0);
2421
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 if (tp->Rx_skbuff[i])
2423 continue;
Francois Romieubcf0bf92006-07-26 23:14:13 +02002424
Stephen Hemminger15d31752007-06-16 22:36:41 +02002425 skb = rtl8169_alloc_rx_skb(tp->pci_dev, dev,
2426 tp->RxDescArray + i,
2427 tp->rx_buf_sz, tp->align);
2428 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 break;
Stephen Hemminger15d31752007-06-16 22:36:41 +02002430
2431 tp->Rx_skbuff[i] = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 }
2433 return cur - start;
2434}
2435
2436static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
2437{
2438 desc->opts1 |= cpu_to_le32(RingEnd);
2439}
2440
2441static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
2442{
2443 tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
2444}
2445
2446static int rtl8169_init_ring(struct net_device *dev)
2447{
2448 struct rtl8169_private *tp = netdev_priv(dev);
2449
2450 rtl8169_init_ring_indexes(tp);
2451
2452 memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
2453 memset(tp->Rx_skbuff, 0x0, NUM_RX_DESC * sizeof(struct sk_buff *));
2454
2455 if (rtl8169_rx_fill(tp, dev, 0, NUM_RX_DESC) != NUM_RX_DESC)
2456 goto err_out;
2457
2458 rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
2459
2460 return 0;
2461
2462err_out:
2463 rtl8169_rx_clear(tp);
2464 return -ENOMEM;
2465}
2466
2467static void rtl8169_unmap_tx_skb(struct pci_dev *pdev, struct ring_info *tx_skb,
2468 struct TxDesc *desc)
2469{
2470 unsigned int len = tx_skb->len;
2471
2472 pci_unmap_single(pdev, le64_to_cpu(desc->addr), len, PCI_DMA_TODEVICE);
2473 desc->opts1 = 0x00;
2474 desc->opts2 = 0x00;
2475 desc->addr = 0x00;
2476 tx_skb->len = 0;
2477}
2478
2479static void rtl8169_tx_clear(struct rtl8169_private *tp)
2480{
2481 unsigned int i;
2482
2483 for (i = tp->dirty_tx; i < tp->dirty_tx + NUM_TX_DESC; i++) {
2484 unsigned int entry = i % NUM_TX_DESC;
2485 struct ring_info *tx_skb = tp->tx_skb + entry;
2486 unsigned int len = tx_skb->len;
2487
2488 if (len) {
2489 struct sk_buff *skb = tx_skb->skb;
2490
2491 rtl8169_unmap_tx_skb(tp->pci_dev, tx_skb,
2492 tp->TxDescArray + entry);
2493 if (skb) {
2494 dev_kfree_skb(skb);
2495 tx_skb->skb = NULL;
2496 }
Francois Romieucebf8cc2007-10-18 12:06:54 +02002497 tp->dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 }
2499 }
2500 tp->cur_tx = tp->dirty_tx = 0;
2501}
2502
David Howellsc4028952006-11-22 14:57:56 +00002503static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504{
2505 struct rtl8169_private *tp = netdev_priv(dev);
2506
David Howellsc4028952006-11-22 14:57:56 +00002507 PREPARE_DELAYED_WORK(&tp->task, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 schedule_delayed_work(&tp->task, 4);
2509}
2510
2511static void rtl8169_wait_for_quiescence(struct net_device *dev)
2512{
2513 struct rtl8169_private *tp = netdev_priv(dev);
2514 void __iomem *ioaddr = tp->mmio_addr;
2515
2516 synchronize_irq(dev->irq);
2517
2518 /* Wait for any pending NAPI task to complete */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002519 napi_disable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
2521 rtl8169_irq_mask_and_ack(ioaddr);
2522
David S. Millerd1d08d12008-01-07 20:53:33 -08002523 tp->intr_mask = 0xffff;
2524 RTL_W16(IntrMask, tp->intr_event);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002525 napi_enable(&tp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526}
2527
David Howellsc4028952006-11-22 14:57:56 +00002528static void rtl8169_reinit_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529{
David Howellsc4028952006-11-22 14:57:56 +00002530 struct rtl8169_private *tp =
2531 container_of(work, struct rtl8169_private, task.work);
2532 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 int ret;
2534
Francois Romieueb2a0212007-02-15 23:37:21 +01002535 rtnl_lock();
2536
2537 if (!netif_running(dev))
2538 goto out_unlock;
2539
2540 rtl8169_wait_for_quiescence(dev);
2541 rtl8169_close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542
2543 ret = rtl8169_open(dev);
2544 if (unlikely(ret < 0)) {
Francois Romieu07d3f512007-02-21 22:40:46 +01002545 if (net_ratelimit() && netif_msg_drv(tp)) {
Joe Perches53edbec2007-10-18 21:15:01 +02002546 printk(KERN_ERR PFX "%s: reinit failure (status = %d)."
Francois Romieu07d3f512007-02-21 22:40:46 +01002547 " Rescheduling.\n", dev->name, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 }
2549 rtl8169_schedule_work(dev, rtl8169_reinit_task);
2550 }
Francois Romieueb2a0212007-02-15 23:37:21 +01002551
2552out_unlock:
2553 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554}
2555
David Howellsc4028952006-11-22 14:57:56 +00002556static void rtl8169_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557{
David Howellsc4028952006-11-22 14:57:56 +00002558 struct rtl8169_private *tp =
2559 container_of(work, struct rtl8169_private, task.work);
2560 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
Francois Romieueb2a0212007-02-15 23:37:21 +01002562 rtnl_lock();
2563
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 if (!netif_running(dev))
Francois Romieueb2a0212007-02-15 23:37:21 +01002565 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
2567 rtl8169_wait_for_quiescence(dev);
2568
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002569 rtl8169_rx_interrupt(dev, tp, tp->mmio_addr, ~(u32)0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 rtl8169_tx_clear(tp);
2571
2572 if (tp->dirty_rx == tp->cur_rx) {
2573 rtl8169_init_ring_indexes(tp);
Francois Romieu07ce4062007-02-23 23:36:39 +01002574 rtl_hw_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 netif_wake_queue(dev);
Francois Romieucebf8cc2007-10-18 12:06:54 +02002576 rtl8169_check_link_status(dev, tp, tp->mmio_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 } else {
Francois Romieu07d3f512007-02-21 22:40:46 +01002578 if (net_ratelimit() && netif_msg_intr(tp)) {
Joe Perches53edbec2007-10-18 21:15:01 +02002579 printk(KERN_EMERG PFX "%s: Rx buffers shortage\n",
Francois Romieu07d3f512007-02-21 22:40:46 +01002580 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 }
2582 rtl8169_schedule_work(dev, rtl8169_reset_task);
2583 }
Francois Romieueb2a0212007-02-15 23:37:21 +01002584
2585out_unlock:
2586 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587}
2588
2589static void rtl8169_tx_timeout(struct net_device *dev)
2590{
2591 struct rtl8169_private *tp = netdev_priv(dev);
2592
2593 rtl8169_hw_reset(tp->mmio_addr);
2594
2595 /* Let's wait a bit while any (async) irq lands on */
2596 rtl8169_schedule_work(dev, rtl8169_reset_task);
2597}
2598
2599static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
2600 u32 opts1)
2601{
2602 struct skb_shared_info *info = skb_shinfo(skb);
2603 unsigned int cur_frag, entry;
Jeff Garzika6343af2007-07-17 05:39:58 -04002604 struct TxDesc * uninitialized_var(txd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
2606 entry = tp->cur_tx;
2607 for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
2608 skb_frag_t *frag = info->frags + cur_frag;
2609 dma_addr_t mapping;
2610 u32 status, len;
2611 void *addr;
2612
2613 entry = (entry + 1) % NUM_TX_DESC;
2614
2615 txd = tp->TxDescArray + entry;
2616 len = frag->size;
2617 addr = ((void *) page_address(frag->page)) + frag->page_offset;
2618 mapping = pci_map_single(tp->pci_dev, addr, len, PCI_DMA_TODEVICE);
2619
2620 /* anti gcc 2.95.3 bugware (sic) */
2621 status = opts1 | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
2622
2623 txd->opts1 = cpu_to_le32(status);
2624 txd->addr = cpu_to_le64(mapping);
2625
2626 tp->tx_skb[entry].len = len;
2627 }
2628
2629 if (cur_frag) {
2630 tp->tx_skb[entry].skb = skb;
2631 txd->opts1 |= cpu_to_le32(LastFrag);
2632 }
2633
2634 return cur_frag;
2635}
2636
2637static inline u32 rtl8169_tso_csum(struct sk_buff *skb, struct net_device *dev)
2638{
2639 if (dev->features & NETIF_F_TSO) {
Herbert Xu79671682006-06-22 02:40:14 -07002640 u32 mss = skb_shinfo(skb)->gso_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
2642 if (mss)
2643 return LargeSend | ((mss & MSSMask) << MSSShift);
2644 }
Patrick McHardy84fa7932006-08-29 16:44:56 -07002645 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002646 const struct iphdr *ip = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647
2648 if (ip->protocol == IPPROTO_TCP)
2649 return IPCS | TCPCS;
2650 else if (ip->protocol == IPPROTO_UDP)
2651 return IPCS | UDPCS;
2652 WARN_ON(1); /* we need a WARN() */
2653 }
2654 return 0;
2655}
2656
2657static int rtl8169_start_xmit(struct sk_buff *skb, struct net_device *dev)
2658{
2659 struct rtl8169_private *tp = netdev_priv(dev);
2660 unsigned int frags, entry = tp->cur_tx % NUM_TX_DESC;
2661 struct TxDesc *txd = tp->TxDescArray + entry;
2662 void __iomem *ioaddr = tp->mmio_addr;
2663 dma_addr_t mapping;
2664 u32 status, len;
2665 u32 opts1;
Francois Romieu188f4af2006-08-16 14:51:52 +02002666 int ret = NETDEV_TX_OK;
Francois Romieu5b0384f2006-08-16 16:00:01 +02002667
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002669 if (netif_msg_drv(tp)) {
2670 printk(KERN_ERR
2671 "%s: BUG! Tx Ring full when queue awake!\n",
2672 dev->name);
2673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 goto err_stop;
2675 }
2676
2677 if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
2678 goto err_stop;
2679
2680 opts1 = DescOwn | rtl8169_tso_csum(skb, dev);
2681
2682 frags = rtl8169_xmit_frags(tp, skb, opts1);
2683 if (frags) {
2684 len = skb_headlen(skb);
2685 opts1 |= FirstFrag;
2686 } else {
2687 len = skb->len;
2688
2689 if (unlikely(len < ETH_ZLEN)) {
Herbert Xu5b057c62006-06-23 02:06:41 -07002690 if (skb_padto(skb, ETH_ZLEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 goto err_update_stats;
2692 len = ETH_ZLEN;
2693 }
2694
2695 opts1 |= FirstFrag | LastFrag;
2696 tp->tx_skb[entry].skb = skb;
2697 }
2698
2699 mapping = pci_map_single(tp->pci_dev, skb->data, len, PCI_DMA_TODEVICE);
2700
2701 tp->tx_skb[entry].len = len;
2702 txd->addr = cpu_to_le64(mapping);
2703 txd->opts2 = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
2704
2705 wmb();
2706
2707 /* anti gcc 2.95.3 bugware (sic) */
2708 status = opts1 | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
2709 txd->opts1 = cpu_to_le32(status);
2710
2711 dev->trans_start = jiffies;
2712
2713 tp->cur_tx += frags + 1;
2714
2715 smp_wmb();
2716
Francois Romieu275391a2007-02-23 23:50:28 +01002717 RTL_W8(TxPoll, NPQ); /* set polling bit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718
2719 if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
2720 netif_stop_queue(dev);
2721 smp_rmb();
2722 if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
2723 netif_wake_queue(dev);
2724 }
2725
2726out:
2727 return ret;
2728
2729err_stop:
2730 netif_stop_queue(dev);
Francois Romieu188f4af2006-08-16 14:51:52 +02002731 ret = NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732err_update_stats:
Francois Romieucebf8cc2007-10-18 12:06:54 +02002733 dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 goto out;
2735}
2736
2737static void rtl8169_pcierr_interrupt(struct net_device *dev)
2738{
2739 struct rtl8169_private *tp = netdev_priv(dev);
2740 struct pci_dev *pdev = tp->pci_dev;
2741 void __iomem *ioaddr = tp->mmio_addr;
2742 u16 pci_status, pci_cmd;
2743
2744 pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
2745 pci_read_config_word(pdev, PCI_STATUS, &pci_status);
2746
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002747 if (netif_msg_intr(tp)) {
2748 printk(KERN_ERR
2749 "%s: PCI error (cmd = 0x%04x, status = 0x%04x).\n",
2750 dev->name, pci_cmd, pci_status);
2751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
2753 /*
2754 * The recovery sequence below admits a very elaborated explanation:
2755 * - it seems to work;
Francois Romieud03902b2006-11-23 00:00:42 +01002756 * - I did not see what else could be done;
2757 * - it makes iop3xx happy.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 *
2759 * Feel free to adjust to your needs.
2760 */
Francois Romieua27993f2006-12-18 00:04:19 +01002761 if (pdev->broken_parity_status)
Francois Romieud03902b2006-11-23 00:00:42 +01002762 pci_cmd &= ~PCI_COMMAND_PARITY;
2763 else
2764 pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
2765
2766 pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
2768 pci_write_config_word(pdev, PCI_STATUS,
2769 pci_status & (PCI_STATUS_DETECTED_PARITY |
2770 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
2771 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
2772
2773 /* The infamous DAC f*ckup only happens at boot time */
2774 if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002775 if (netif_msg_intr(tp))
2776 printk(KERN_INFO "%s: disabling PCI DAC.\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 tp->cp_cmd &= ~PCIDAC;
2778 RTL_W16(CPlusCmd, tp->cp_cmd);
2779 dev->features &= ~NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 }
2781
2782 rtl8169_hw_reset(ioaddr);
Francois Romieud03902b2006-11-23 00:00:42 +01002783
2784 rtl8169_schedule_work(dev, rtl8169_reinit_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785}
2786
Francois Romieu07d3f512007-02-21 22:40:46 +01002787static void rtl8169_tx_interrupt(struct net_device *dev,
2788 struct rtl8169_private *tp,
2789 void __iomem *ioaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790{
2791 unsigned int dirty_tx, tx_left;
2792
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 dirty_tx = tp->dirty_tx;
2794 smp_rmb();
2795 tx_left = tp->cur_tx - dirty_tx;
2796
2797 while (tx_left > 0) {
2798 unsigned int entry = dirty_tx % NUM_TX_DESC;
2799 struct ring_info *tx_skb = tp->tx_skb + entry;
2800 u32 len = tx_skb->len;
2801 u32 status;
2802
2803 rmb();
2804 status = le32_to_cpu(tp->TxDescArray[entry].opts1);
2805 if (status & DescOwn)
2806 break;
2807
Francois Romieucebf8cc2007-10-18 12:06:54 +02002808 dev->stats.tx_bytes += len;
2809 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
2811 rtl8169_unmap_tx_skb(tp->pci_dev, tx_skb, tp->TxDescArray + entry);
2812
2813 if (status & LastFrag) {
2814 dev_kfree_skb_irq(tx_skb->skb);
2815 tx_skb->skb = NULL;
2816 }
2817 dirty_tx++;
2818 tx_left--;
2819 }
2820
2821 if (tp->dirty_tx != dirty_tx) {
2822 tp->dirty_tx = dirty_tx;
2823 smp_wmb();
2824 if (netif_queue_stopped(dev) &&
2825 (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
2826 netif_wake_queue(dev);
2827 }
Francois Romieud78ae2d2007-08-26 20:08:19 +02002828 /*
2829 * 8168 hack: TxPoll requests are lost when the Tx packets are
2830 * too close. Let's kick an extra TxPoll request when a burst
2831 * of start_xmit activity is detected (if it is not detected,
2832 * it is slow enough). -- FR
2833 */
2834 smp_rmb();
2835 if (tp->cur_tx != dirty_tx)
2836 RTL_W8(TxPoll, NPQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 }
2838}
2839
Francois Romieu126fa4b2005-05-12 20:09:17 -04002840static inline int rtl8169_fragmented_frame(u32 status)
2841{
2842 return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
2843}
2844
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845static inline void rtl8169_rx_csum(struct sk_buff *skb, struct RxDesc *desc)
2846{
2847 u32 opts1 = le32_to_cpu(desc->opts1);
2848 u32 status = opts1 & RxProtoMask;
2849
2850 if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
2851 ((status == RxProtoUDP) && !(opts1 & UDPFail)) ||
2852 ((status == RxProtoIP) && !(opts1 & IPFail)))
2853 skb->ip_summed = CHECKSUM_UNNECESSARY;
2854 else
2855 skb->ip_summed = CHECKSUM_NONE;
2856}
2857
Francois Romieu07d3f512007-02-21 22:40:46 +01002858static inline bool rtl8169_try_rx_copy(struct sk_buff **sk_buff,
2859 struct rtl8169_private *tp, int pkt_size,
2860 dma_addr_t addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861{
Stephen Hemmingerb4496552007-06-17 01:06:49 +02002862 struct sk_buff *skb;
2863 bool done = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
Stephen Hemmingerb4496552007-06-17 01:06:49 +02002865 if (pkt_size >= rx_copybreak)
2866 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
Francois Romieu07d3f512007-02-21 22:40:46 +01002868 skb = netdev_alloc_skb(tp->dev, pkt_size + NET_IP_ALIGN);
Stephen Hemmingerb4496552007-06-17 01:06:49 +02002869 if (!skb)
2870 goto out;
2871
Francois Romieu07d3f512007-02-21 22:40:46 +01002872 pci_dma_sync_single_for_cpu(tp->pci_dev, addr, pkt_size,
2873 PCI_DMA_FROMDEVICE);
Francois Romieu86402232007-02-20 22:20:51 +01002874 skb_reserve(skb, NET_IP_ALIGN);
Stephen Hemmingerb4496552007-06-17 01:06:49 +02002875 skb_copy_from_linear_data(*sk_buff, skb->data, pkt_size);
2876 *sk_buff = skb;
2877 done = true;
2878out:
2879 return done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880}
2881
Francois Romieu07d3f512007-02-21 22:40:46 +01002882static int rtl8169_rx_interrupt(struct net_device *dev,
2883 struct rtl8169_private *tp,
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002884 void __iomem *ioaddr, u32 budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885{
2886 unsigned int cur_rx, rx_left;
2887 unsigned int delta, count;
2888
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 cur_rx = tp->cur_rx;
2890 rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
Francois Romieu865c6522008-05-11 14:51:00 +02002891 rx_left = min(rx_left, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892
Richard Dawe4dcb7d32005-05-27 21:12:00 +02002893 for (; rx_left > 0; rx_left--, cur_rx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 unsigned int entry = cur_rx % NUM_RX_DESC;
Francois Romieu126fa4b2005-05-12 20:09:17 -04002895 struct RxDesc *desc = tp->RxDescArray + entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 u32 status;
2897
2898 rmb();
Francois Romieu126fa4b2005-05-12 20:09:17 -04002899 status = le32_to_cpu(desc->opts1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900
2901 if (status & DescOwn)
2902 break;
Richard Dawe4dcb7d32005-05-27 21:12:00 +02002903 if (unlikely(status & RxRES)) {
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002904 if (netif_msg_rx_err(tp)) {
2905 printk(KERN_INFO
2906 "%s: Rx ERROR. status = %08x\n",
2907 dev->name, status);
2908 }
Francois Romieucebf8cc2007-10-18 12:06:54 +02002909 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 if (status & (RxRWT | RxRUNT))
Francois Romieucebf8cc2007-10-18 12:06:54 +02002911 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 if (status & RxCRC)
Francois Romieucebf8cc2007-10-18 12:06:54 +02002913 dev->stats.rx_crc_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02002914 if (status & RxFOVF) {
2915 rtl8169_schedule_work(dev, rtl8169_reset_task);
Francois Romieucebf8cc2007-10-18 12:06:54 +02002916 dev->stats.rx_fifo_errors++;
Francois Romieu9dccf612006-05-14 12:31:17 +02002917 }
Francois Romieu126fa4b2005-05-12 20:09:17 -04002918 rtl8169_mark_to_asic(desc, tp->rx_buf_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 struct sk_buff *skb = tp->Rx_skbuff[entry];
Stephen Hemmingerb4496552007-06-17 01:06:49 +02002921 dma_addr_t addr = le64_to_cpu(desc->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 int pkt_size = (status & 0x00001FFF) - 4;
Stephen Hemmingerb4496552007-06-17 01:06:49 +02002923 struct pci_dev *pdev = tp->pci_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
Francois Romieu126fa4b2005-05-12 20:09:17 -04002925 /*
2926 * The driver does not support incoming fragmented
2927 * frames. They are seen as a symptom of over-mtu
2928 * sized frames.
2929 */
2930 if (unlikely(rtl8169_fragmented_frame(status))) {
Francois Romieucebf8cc2007-10-18 12:06:54 +02002931 dev->stats.rx_dropped++;
2932 dev->stats.rx_length_errors++;
Francois Romieu126fa4b2005-05-12 20:09:17 -04002933 rtl8169_mark_to_asic(desc, tp->rx_buf_sz);
Richard Dawe4dcb7d32005-05-27 21:12:00 +02002934 continue;
Francois Romieu126fa4b2005-05-12 20:09:17 -04002935 }
2936
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 rtl8169_rx_csum(skb, desc);
Francois Romieubcf0bf92006-07-26 23:14:13 +02002938
Francois Romieu07d3f512007-02-21 22:40:46 +01002939 if (rtl8169_try_rx_copy(&skb, tp, pkt_size, addr)) {
Stephen Hemmingerb4496552007-06-17 01:06:49 +02002940 pci_dma_sync_single_for_device(pdev, addr,
2941 pkt_size, PCI_DMA_FROMDEVICE);
2942 rtl8169_mark_to_asic(desc, tp->rx_buf_sz);
2943 } else {
2944 pci_unmap_single(pdev, addr, pkt_size,
2945 PCI_DMA_FROMDEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 tp->Rx_skbuff[entry] = NULL;
2947 }
2948
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 skb_put(skb, pkt_size);
2950 skb->protocol = eth_type_trans(skb, dev);
2951
2952 if (rtl8169_rx_vlan_skb(tp, desc, skb) < 0)
Francois Romieu865c6522008-05-11 14:51:00 +02002953 netif_receive_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954
2955 dev->last_rx = jiffies;
Francois Romieucebf8cc2007-10-18 12:06:54 +02002956 dev->stats.rx_bytes += pkt_size;
2957 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 }
Francois Romieu6dccd162007-02-13 23:38:05 +01002959
2960 /* Work around for AMD plateform. */
Al Viro95e09182007-12-22 18:55:39 +00002961 if ((desc->opts2 & cpu_to_le32(0xfffe000)) &&
Francois Romieu6dccd162007-02-13 23:38:05 +01002962 (tp->mac_version == RTL_GIGA_MAC_VER_05)) {
2963 desc->opts2 = 0;
2964 cur_rx++;
2965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 }
2967
2968 count = cur_rx - tp->cur_rx;
2969 tp->cur_rx = cur_rx;
2970
2971 delta = rtl8169_rx_fill(tp, dev, tp->dirty_rx, tp->cur_rx);
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002972 if (!delta && count && netif_msg_intr(tp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 printk(KERN_INFO "%s: no Rx buffer allocated\n", dev->name);
2974 tp->dirty_rx += delta;
2975
2976 /*
2977 * FIXME: until there is periodic timer to try and refill the ring,
2978 * a temporary shortage may definitely kill the Rx process.
2979 * - disable the asic to try and avoid an overflow and kick it again
2980 * after refill ?
2981 * - how do others driver handle this condition (Uh oh...).
2982 */
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02002983 if ((tp->dirty_rx + NUM_RX_DESC == tp->cur_rx) && netif_msg_intr(tp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 printk(KERN_EMERG "%s: Rx buffers exhausted\n", dev->name);
2985
2986 return count;
2987}
2988
Francois Romieu07d3f512007-02-21 22:40:46 +01002989static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990{
Francois Romieu07d3f512007-02-21 22:40:46 +01002991 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 struct rtl8169_private *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 void __iomem *ioaddr = tp->mmio_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 int handled = 0;
Francois Romieu865c6522008-05-11 14:51:00 +02002995 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
Francois Romieu865c6522008-05-11 14:51:00 +02002997 status = RTL_R16(IntrStatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
Francois Romieu865c6522008-05-11 14:51:00 +02002999 /* hotplug/major error/no more work/shared irq */
3000 if ((status == 0xffff) || !status)
3001 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
Francois Romieu865c6522008-05-11 14:51:00 +02003003 handled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
Francois Romieu865c6522008-05-11 14:51:00 +02003005 if (unlikely(!netif_running(dev))) {
3006 rtl8169_asic_down(ioaddr);
3007 goto out;
3008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
Francois Romieu865c6522008-05-11 14:51:00 +02003010 status &= tp->intr_mask;
3011 RTL_W16(IntrStatus,
3012 (status & RxFIFOOver) ? (status | RxOverflow) : status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013
Francois Romieu865c6522008-05-11 14:51:00 +02003014 if (!(status & tp->intr_event))
3015 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
Francois Romieu865c6522008-05-11 14:51:00 +02003017 /* Work around for rx fifo overflow */
3018 if (unlikely(status & RxFIFOOver) &&
3019 (tp->mac_version == RTL_GIGA_MAC_VER_11)) {
3020 netif_stop_queue(dev);
3021 rtl8169_tx_timeout(dev);
3022 goto out;
3023 }
Francois Romieu0e485152007-02-20 00:00:26 +01003024
Francois Romieu865c6522008-05-11 14:51:00 +02003025 if (unlikely(status & SYSErr)) {
3026 rtl8169_pcierr_interrupt(dev);
3027 goto out;
3028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029
Francois Romieu865c6522008-05-11 14:51:00 +02003030 if (status & LinkChg)
3031 rtl8169_check_link_status(dev, tp, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032
Francois Romieu865c6522008-05-11 14:51:00 +02003033 if (status & tp->napi_event) {
3034 RTL_W16(IntrMask, tp->intr_event & ~tp->napi_event);
3035 tp->intr_mask = ~tp->napi_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003037 if (likely(netif_rx_schedule_prep(dev, &tp->napi)))
3038 __netif_rx_schedule(dev, &tp->napi);
Francois Romieu865c6522008-05-11 14:51:00 +02003039 else if (netif_msg_intr(tp)) {
3040 printk(KERN_INFO "%s: interrupt %04x in poll\n",
3041 dev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 }
3044out:
3045 return IRQ_RETVAL(handled);
3046}
3047
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003048static int rtl8169_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003050 struct rtl8169_private *tp = container_of(napi, struct rtl8169_private, napi);
3051 struct net_device *dev = tp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 void __iomem *ioaddr = tp->mmio_addr;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003053 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003055 work_done = rtl8169_rx_interrupt(dev, tp, ioaddr, (u32) budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 rtl8169_tx_interrupt(dev, tp, ioaddr);
3057
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003058 if (work_done < budget) {
3059 netif_rx_complete(dev, napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 tp->intr_mask = 0xffff;
3061 /*
3062 * 20040426: the barrier is not strictly required but the
3063 * behavior of the irq handler could be less predictable
3064 * without it. Btw, the lack of flush for the posted pci
3065 * write is safe - FR
3066 */
3067 smp_wmb();
Francois Romieu0e485152007-02-20 00:00:26 +01003068 RTL_W16(IntrMask, tp->intr_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 }
3070
Stephen Hemmingerbea33482007-10-03 16:41:36 -07003071 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073
3074static void rtl8169_down(struct net_device *dev)
3075{
3076 struct rtl8169_private *tp = netdev_priv(dev);
3077 void __iomem *ioaddr = tp->mmio_addr;
Arnaud Patard733b7362006-10-12 22:33:31 +02003078 unsigned int intrmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079
3080 rtl8169_delete_timer(dev);
3081
3082 netif_stop_queue(dev);
3083
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01003084 napi_disable(&tp->napi);
Stephen Hemminger93dd79e2007-10-28 17:14:06 +01003085
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086core_down:
3087 spin_lock_irq(&tp->lock);
3088
3089 rtl8169_asic_down(ioaddr);
3090
3091 /* Update the error counts. */
Francois Romieucebf8cc2007-10-18 12:06:54 +02003092 dev->stats.rx_missed_errors += RTL_R32(RxMissed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 RTL_W32(RxMissed, 0);
3094
3095 spin_unlock_irq(&tp->lock);
3096
3097 synchronize_irq(dev->irq);
3098
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 /* Give a racing hard_start_xmit a few cycles to complete. */
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07003100 synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
3102 /*
3103 * And now for the 50k$ question: are IRQ disabled or not ?
3104 *
3105 * Two paths lead here:
3106 * 1) dev->close
3107 * -> netif_running() is available to sync the current code and the
3108 * IRQ handler. See rtl8169_interrupt for details.
3109 * 2) dev->change_mtu
3110 * -> rtl8169_poll can not be issued again and re-enable the
3111 * interruptions. Let's simply issue the IRQ down sequence again.
Arnaud Patard733b7362006-10-12 22:33:31 +02003112 *
3113 * No loop if hotpluged or major error (0xffff).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 */
Arnaud Patard733b7362006-10-12 22:33:31 +02003115 intrmask = RTL_R16(IntrMask);
3116 if (intrmask && (intrmask != 0xffff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 goto core_down;
3118
3119 rtl8169_tx_clear(tp);
3120
3121 rtl8169_rx_clear(tp);
3122}
3123
3124static int rtl8169_close(struct net_device *dev)
3125{
3126 struct rtl8169_private *tp = netdev_priv(dev);
3127 struct pci_dev *pdev = tp->pci_dev;
3128
3129 rtl8169_down(dev);
3130
3131 free_irq(dev->irq, dev);
3132
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 pci_free_consistent(pdev, R8169_RX_RING_BYTES, tp->RxDescArray,
3134 tp->RxPhyAddr);
3135 pci_free_consistent(pdev, R8169_TX_RING_BYTES, tp->TxDescArray,
3136 tp->TxPhyAddr);
3137 tp->TxDescArray = NULL;
3138 tp->RxDescArray = NULL;
3139
3140 return 0;
3141}
3142
Francois Romieu07ce4062007-02-23 23:36:39 +01003143static void rtl_set_rx_mode(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144{
3145 struct rtl8169_private *tp = netdev_priv(dev);
3146 void __iomem *ioaddr = tp->mmio_addr;
3147 unsigned long flags;
3148 u32 mc_filter[2]; /* Multicast hash filter */
Francois Romieu07d3f512007-02-21 22:40:46 +01003149 int rx_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 u32 tmp = 0;
3151
3152 if (dev->flags & IFF_PROMISC) {
3153 /* Unconditionally log net taps. */
Stephen Hemmingerb57b7e52005-05-27 21:11:52 +02003154 if (netif_msg_link(tp)) {
3155 printk(KERN_NOTICE "%s: Promiscuous mode enabled.\n",
3156 dev->name);
3157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 rx_mode =
3159 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
3160 AcceptAllPhys;
3161 mc_filter[1] = mc_filter[0] = 0xffffffff;
3162 } else if ((dev->mc_count > multicast_filter_limit)
3163 || (dev->flags & IFF_ALLMULTI)) {
3164 /* Too many to filter perfectly -- accept all multicasts. */
3165 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
3166 mc_filter[1] = mc_filter[0] = 0xffffffff;
3167 } else {
3168 struct dev_mc_list *mclist;
Francois Romieu07d3f512007-02-21 22:40:46 +01003169 unsigned int i;
3170
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 rx_mode = AcceptBroadcast | AcceptMyPhys;
3172 mc_filter[1] = mc_filter[0] = 0;
3173 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
3174 i++, mclist = mclist->next) {
3175 int bit_nr = ether_crc(ETH_ALEN, mclist->dmi_addr) >> 26;
3176 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
3177 rx_mode |= AcceptMulticast;
3178 }
3179 }
3180
3181 spin_lock_irqsave(&tp->lock, flags);
3182
3183 tmp = rtl8169_rx_config | rx_mode |
3184 (RTL_R32(RxConfig) & rtl_chip_info[tp->chipset].RxConfigMask);
3185
Francois Romieuf887cce2008-07-17 22:24:18 +02003186 if (tp->mac_version > RTL_GIGA_MAC_VER_06) {
Francois Romieu1087f4f2007-12-26 22:46:05 +01003187 u32 data = mc_filter[0];
3188
3189 mc_filter[0] = swab32(mc_filter[1]);
3190 mc_filter[1] = swab32(data);
Francois Romieubcf0bf92006-07-26 23:14:13 +02003191 }
3192
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 RTL_W32(MAR0 + 0, mc_filter[0]);
3194 RTL_W32(MAR0 + 4, mc_filter[1]);
3195
Francois Romieu57a9f232007-06-04 22:10:15 +02003196 RTL_W32(RxConfig, tmp);
3197
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 spin_unlock_irqrestore(&tp->lock, flags);
3199}
3200
3201/**
3202 * rtl8169_get_stats - Get rtl8169 read/write statistics
3203 * @dev: The Ethernet Device to get statistics for
3204 *
3205 * Get TX/RX statistics for rtl8169
3206 */
3207static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
3208{
3209 struct rtl8169_private *tp = netdev_priv(dev);
3210 void __iomem *ioaddr = tp->mmio_addr;
3211 unsigned long flags;
3212
3213 if (netif_running(dev)) {
3214 spin_lock_irqsave(&tp->lock, flags);
Francois Romieucebf8cc2007-10-18 12:06:54 +02003215 dev->stats.rx_missed_errors += RTL_R32(RxMissed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 RTL_W32(RxMissed, 0);
3217 spin_unlock_irqrestore(&tp->lock, flags);
3218 }
Francois Romieu5b0384f2006-08-16 16:00:01 +02003219
Francois Romieucebf8cc2007-10-18 12:06:54 +02003220 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221}
3222
Francois Romieu5d06a992006-02-23 00:47:58 +01003223#ifdef CONFIG_PM
3224
3225static int rtl8169_suspend(struct pci_dev *pdev, pm_message_t state)
3226{
3227 struct net_device *dev = pci_get_drvdata(pdev);
3228 struct rtl8169_private *tp = netdev_priv(dev);
3229 void __iomem *ioaddr = tp->mmio_addr;
3230
3231 if (!netif_running(dev))
Francois Romieu1371fa62007-04-02 23:01:11 +02003232 goto out_pci_suspend;
Francois Romieu5d06a992006-02-23 00:47:58 +01003233
3234 netif_device_detach(dev);
3235 netif_stop_queue(dev);
3236
3237 spin_lock_irq(&tp->lock);
3238
3239 rtl8169_asic_down(ioaddr);
3240
Francois Romieucebf8cc2007-10-18 12:06:54 +02003241 dev->stats.rx_missed_errors += RTL_R32(RxMissed);
Francois Romieu5d06a992006-02-23 00:47:58 +01003242 RTL_W32(RxMissed, 0);
3243
3244 spin_unlock_irq(&tp->lock);
3245
Francois Romieu1371fa62007-04-02 23:01:11 +02003246out_pci_suspend:
Francois Romieu5d06a992006-02-23 00:47:58 +01003247 pci_save_state(pdev);
Francois Romieuf23e7fd2007-10-04 22:36:14 +02003248 pci_enable_wake(pdev, pci_choose_state(pdev, state),
3249 (tp->features & RTL_FEATURE_WOL) ? 1 : 0);
Francois Romieu5d06a992006-02-23 00:47:58 +01003250 pci_set_power_state(pdev, pci_choose_state(pdev, state));
Francois Romieu1371fa62007-04-02 23:01:11 +02003251
Francois Romieu5d06a992006-02-23 00:47:58 +01003252 return 0;
3253}
3254
3255static int rtl8169_resume(struct pci_dev *pdev)
3256{
3257 struct net_device *dev = pci_get_drvdata(pdev);
3258
Francois Romieu1371fa62007-04-02 23:01:11 +02003259 pci_set_power_state(pdev, PCI_D0);
3260 pci_restore_state(pdev);
3261 pci_enable_wake(pdev, PCI_D0, 0);
3262
Francois Romieu5d06a992006-02-23 00:47:58 +01003263 if (!netif_running(dev))
3264 goto out;
3265
3266 netif_device_attach(dev);
3267
Francois Romieu5d06a992006-02-23 00:47:58 +01003268 rtl8169_schedule_work(dev, rtl8169_reset_task);
3269out:
3270 return 0;
3271}
3272
3273#endif /* CONFIG_PM */
3274
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275static struct pci_driver rtl8169_pci_driver = {
3276 .name = MODULENAME,
3277 .id_table = rtl8169_pci_tbl,
3278 .probe = rtl8169_init_one,
3279 .remove = __devexit_p(rtl8169_remove_one),
3280#ifdef CONFIG_PM
3281 .suspend = rtl8169_suspend,
3282 .resume = rtl8169_resume,
3283#endif
3284};
3285
Francois Romieu07d3f512007-02-21 22:40:46 +01003286static int __init rtl8169_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287{
Jeff Garzik29917622006-08-19 17:48:59 -04003288 return pci_register_driver(&rtl8169_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289}
3290
Francois Romieu07d3f512007-02-21 22:40:46 +01003291static void __exit rtl8169_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292{
3293 pci_unregister_driver(&rtl8169_pci_driver);
3294}
3295
3296module_init(rtl8169_init_module);
3297module_exit(rtl8169_cleanup_module);