r8169.c 131 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
/*
Francois Romieu's avatar
Francois Romieu committed
2
3
4
5
6
7
8
 * r8169.c: RealTek 8169/8168/8101 ethernet driver.
 *
 * Copyright (c) 2002 ShuChen <shuchen@realtek.com.tw>
 * Copyright (c) 2003 - 2007 Francois Romieu <romieu@fr.zoreil.com>
 * Copyright (c) a lot of people too. Please respect their work.
 *
 * See MAINTAINERS file for support contact information.
Linus Torvalds's avatar
Linus Torvalds committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 */

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/pci.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/delay.h>
#include <linux/ethtool.h>
#include <linux/mii.h>
#include <linux/if_vlan.h>
#include <linux/crc32.h>
#include <linux/in.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/init.h>
#include <linux/dma-mapping.h>
26
#include <linux/pm_runtime.h>
27
#include <linux/firmware.h>
Stanislaw Gruszka's avatar
Stanislaw Gruszka committed
28
#include <linux/pci-aspm.h>
Linus Torvalds's avatar
Linus Torvalds committed
29

30
#include <asm/system.h>
Linus Torvalds's avatar
Linus Torvalds committed
31
32
33
#include <asm/io.h>
#include <asm/irq.h>

Francois Romieu's avatar
Francois Romieu committed
34
#define RTL8169_VERSION "2.3LK-NAPI"
Linus Torvalds's avatar
Linus Torvalds committed
35
36
37
#define MODULENAME "r8169"
#define PFX MODULENAME ": "

38
39
#define FIRMWARE_8168D_1	"rtl_nic/rtl8168d-1.fw"
#define FIRMWARE_8168D_2	"rtl_nic/rtl8168d-2.fw"
hayeswang's avatar
hayeswang committed
40
41
#define FIRMWARE_8168E_1	"rtl_nic/rtl8168e-1.fw"
#define FIRMWARE_8168E_2	"rtl_nic/rtl8168e-2.fw"
42
#define FIRMWARE_8105E_1	"rtl_nic/rtl8105e-1.fw"
43

Linus Torvalds's avatar
Linus Torvalds committed
44
45
#ifdef RTL8169_DEBUG
#define assert(expr) \
46
47
	if (!(expr)) {					\
		printk( "Assertion failed! %s,%s,%s,line=%d\n",	\
48
		#expr,__FILE__,__func__,__LINE__);		\
49
	}
50
51
#define dprintk(fmt, args...) \
	do { printk(KERN_DEBUG PFX fmt, ## args); } while (0)
Linus Torvalds's avatar
Linus Torvalds committed
52
53
54
55
56
#else
#define assert(expr) do {} while (0)
#define dprintk(fmt, args...)	do {} while (0)
#endif /* RTL8169_DEBUG */

57
#define R8169_MSG_DEFAULT \
58
	(NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
59

Linus Torvalds's avatar
Linus Torvalds committed
60
61
62
63
64
#define TX_BUFFS_AVAIL(tp) \
	(tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)

/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
   The RTL chips use a 64 element hash table based on the Ethernet CRC. */
65
static const int multicast_filter_limit = 32;
Linus Torvalds's avatar
Linus Torvalds committed
66
67
68
69

/* MAC address length */
#define MAC_ADDR_LEN	6

70
#define MAX_READ_REQUEST_SHIFT	12
Linus Torvalds's avatar
Linus Torvalds committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#define RX_FIFO_THRESH	7	/* 7 means NO threshold, Rx buffer level before first PCI xfer. */
#define RX_DMA_BURST	6	/* Maximum PCI burst, '6' is 1024 */
#define TX_DMA_BURST	6	/* Maximum PCI burst, '6' is 1024 */
#define SafeMtu		0x1c20	/* ... actually life sucks beyond ~7k */
#define InterFrameGap	0x03	/* 3 means InterFrameGap = the shortest one */

#define R8169_REGS_SIZE		256
#define R8169_NAPI_WEIGHT	64
#define NUM_TX_DESC	64	/* Number of Tx descriptor registers */
#define NUM_RX_DESC	256	/* Number of Rx descriptor registers */
#define RX_BUF_SIZE	1536	/* Rx Buffer size */
#define R8169_TX_RING_BYTES	(NUM_TX_DESC * sizeof(struct TxDesc))
#define R8169_RX_RING_BYTES	(NUM_RX_DESC * sizeof(struct RxDesc))

#define RTL8169_TX_TIMEOUT	(6*HZ)
#define RTL8169_PHY_TIMEOUT	(10*HZ)

88
89
#define RTL_EEPROM_SIG		cpu_to_le32(0x8129)
#define RTL_EEPROM_SIG_MASK	cpu_to_le32(0xffff)
90
91
#define RTL_EEPROM_SIG_ADDR	0x0000

Linus Torvalds's avatar
Linus Torvalds committed
92
93
94
95
96
97
/* write/read MMIO register */
#define RTL_W8(reg, val8)	writeb ((val8), ioaddr + (reg))
#define RTL_W16(reg, val16)	writew ((val16), ioaddr + (reg))
#define RTL_W32(reg, val32)	writel ((val32), ioaddr + (reg))
#define RTL_R8(reg)		readb (ioaddr + (reg))
#define RTL_R16(reg)		readw (ioaddr + (reg))
98
#define RTL_R32(reg)		readl (ioaddr + (reg))
Linus Torvalds's avatar
Linus Torvalds committed
99
100

enum mac_version {
101
	RTL_GIGA_MAC_NONE   = 0x00,
Francois Romieu's avatar
Francois Romieu committed
102
103
104
105
106
	RTL_GIGA_MAC_VER_01 = 0x01, // 8169
	RTL_GIGA_MAC_VER_02 = 0x02, // 8169S
	RTL_GIGA_MAC_VER_03 = 0x03, // 8110S
	RTL_GIGA_MAC_VER_04 = 0x04, // 8169SB
	RTL_GIGA_MAC_VER_05 = 0x05, // 8110SCd
107
	RTL_GIGA_MAC_VER_06 = 0x06, // 8110SCe
108
109
110
111
	RTL_GIGA_MAC_VER_07 = 0x07, // 8102e
	RTL_GIGA_MAC_VER_08 = 0x08, // 8102e
	RTL_GIGA_MAC_VER_09 = 0x09, // 8102e
	RTL_GIGA_MAC_VER_10 = 0x0a, // 8101e
112
	RTL_GIGA_MAC_VER_11 = 0x0b, // 8168Bb
Francois Romieu's avatar
Francois Romieu committed
113
114
115
116
117
118
119
120
	RTL_GIGA_MAC_VER_12 = 0x0c, // 8168Be
	RTL_GIGA_MAC_VER_13 = 0x0d, // 8101Eb
	RTL_GIGA_MAC_VER_14 = 0x0e, // 8101 ?
	RTL_GIGA_MAC_VER_15 = 0x0f, // 8101 ?
	RTL_GIGA_MAC_VER_16 = 0x11, // 8101Ec
	RTL_GIGA_MAC_VER_17 = 0x10, // 8168Bf
	RTL_GIGA_MAC_VER_18 = 0x12, // 8168CP
	RTL_GIGA_MAC_VER_19 = 0x13, // 8168C
121
	RTL_GIGA_MAC_VER_20 = 0x14, // 8168C
122
	RTL_GIGA_MAC_VER_21 = 0x15, // 8168C
123
	RTL_GIGA_MAC_VER_22 = 0x16, // 8168C
124
	RTL_GIGA_MAC_VER_23 = 0x17, // 8168CP
125
	RTL_GIGA_MAC_VER_24 = 0x18, // 8168CP
126
127
	RTL_GIGA_MAC_VER_25 = 0x19, // 8168D
	RTL_GIGA_MAC_VER_26 = 0x1a, // 8168D
françois romieu's avatar
françois romieu committed
128
129
	RTL_GIGA_MAC_VER_27 = 0x1b, // 8168DP
	RTL_GIGA_MAC_VER_28 = 0x1c, // 8168DP
130
131
	RTL_GIGA_MAC_VER_29 = 0x1d, // 8105E
	RTL_GIGA_MAC_VER_30 = 0x1e, // 8105E
132
	RTL_GIGA_MAC_VER_31 = 0x1f, // 8168DP
hayeswang's avatar
hayeswang committed
133
134
	RTL_GIGA_MAC_VER_32 = 0x20, // 8168E
	RTL_GIGA_MAC_VER_33 = 0x21, // 8168E
Linus Torvalds's avatar
Linus Torvalds committed
135
136
};

Francois Romieu's avatar
Francois Romieu committed
137
138
139
140
141
142
143
enum rtl_tx_desc_version {
	RTL_TD_0	= 0,
	RTL_TD_1	= 1,
};

#define _R(NAME,MAC,TD) \
	{ .name = NAME, .mac_version = MAC, .txd_version = TD }
Linus Torvalds's avatar
Linus Torvalds committed
144

145
static const struct {
Linus Torvalds's avatar
Linus Torvalds committed
146
147
	const char *name;
	u8 mac_version;
Francois Romieu's avatar
Francois Romieu committed
148
	enum rtl_tx_desc_version txd_version;
Linus Torvalds's avatar
Linus Torvalds committed
149
} rtl_chip_info[] = {
Francois Romieu's avatar
Francois Romieu committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
	_R("RTL8169",		RTL_GIGA_MAC_VER_01, RTL_TD_0), // 8169
	_R("RTL8169s",		RTL_GIGA_MAC_VER_02, RTL_TD_0), // 8169S
	_R("RTL8110s",		RTL_GIGA_MAC_VER_03, RTL_TD_0), // 8110S
	_R("RTL8169sb/8110sb",	RTL_GIGA_MAC_VER_04, RTL_TD_0), // 8169SB
	_R("RTL8169sc/8110sc",	RTL_GIGA_MAC_VER_05, RTL_TD_0), // 8110SCd
	_R("RTL8169sc/8110sc",	RTL_GIGA_MAC_VER_06, RTL_TD_0), // 8110SCe
	_R("RTL8102e",		RTL_GIGA_MAC_VER_07, RTL_TD_1), // PCI-E
	_R("RTL8102e",		RTL_GIGA_MAC_VER_08, RTL_TD_1), // PCI-E
	_R("RTL8102e",		RTL_GIGA_MAC_VER_09, RTL_TD_1), // PCI-E
	_R("RTL8101e",		RTL_GIGA_MAC_VER_10, RTL_TD_0), // PCI-E
	_R("RTL8168b/8111b",	RTL_GIGA_MAC_VER_11, RTL_TD_0), // PCI-E
	_R("RTL8168b/8111b",	RTL_GIGA_MAC_VER_12, RTL_TD_0), // PCI-E
	_R("RTL8101e",		RTL_GIGA_MAC_VER_13, RTL_TD_0), // PCI-E 8139
	_R("RTL8100e",		RTL_GIGA_MAC_VER_14, RTL_TD_0), // PCI-E 8139
	_R("RTL8100e",		RTL_GIGA_MAC_VER_15, RTL_TD_0), // PCI-E 8139
	_R("RTL8168b/8111b",	RTL_GIGA_MAC_VER_17, RTL_TD_0), // PCI-E
	_R("RTL8101e",		RTL_GIGA_MAC_VER_16, RTL_TD_0), // PCI-E
	_R("RTL8168cp/8111cp",	RTL_GIGA_MAC_VER_18, RTL_TD_1), // PCI-E
	_R("RTL8168c/8111c",	RTL_GIGA_MAC_VER_19, RTL_TD_1), // PCI-E
	_R("RTL8168c/8111c",	RTL_GIGA_MAC_VER_20, RTL_TD_1), // PCI-E
	_R("RTL8168c/8111c",	RTL_GIGA_MAC_VER_21, RTL_TD_1), // PCI-E
	_R("RTL8168c/8111c",	RTL_GIGA_MAC_VER_22, RTL_TD_1), // PCI-E
	_R("RTL8168cp/8111cp",	RTL_GIGA_MAC_VER_23, RTL_TD_1), // PCI-E
	_R("RTL8168cp/8111cp",	RTL_GIGA_MAC_VER_24, RTL_TD_1), // PCI-E
	_R("RTL8168d/8111d",	RTL_GIGA_MAC_VER_25, RTL_TD_1), // PCI-E
	_R("RTL8168d/8111d",	RTL_GIGA_MAC_VER_26, RTL_TD_1), // PCI-E
	_R("RTL8168dp/8111dp",	RTL_GIGA_MAC_VER_27, RTL_TD_1), // PCI-E
	_R("RTL8168dp/8111dp",	RTL_GIGA_MAC_VER_28, RTL_TD_1), // PCI-E
	_R("RTL8105e",		RTL_GIGA_MAC_VER_29, RTL_TD_1), // PCI-E
	_R("RTL8105e",		RTL_GIGA_MAC_VER_30, RTL_TD_1), // PCI-E
	_R("RTL8168dp/8111dp",	RTL_GIGA_MAC_VER_31, RTL_TD_1), // PCI-E
	_R("RTL8168e/8111e",	RTL_GIGA_MAC_VER_32, RTL_TD_1), // PCI-E
	_R("RTL8168e/8111e",	RTL_GIGA_MAC_VER_33, RTL_TD_1)  // PCI-E
Linus Torvalds's avatar
Linus Torvalds committed
183
184
185
};
#undef _R

186
187
188
189
190
191
192
static const struct rtl_firmware_info {
	int mac_version;
	const char *fw_name;
} rtl_firmware_infos[] = {
	{ .mac_version = RTL_GIGA_MAC_VER_25, .fw_name = FIRMWARE_8168D_1 },
	{ .mac_version = RTL_GIGA_MAC_VER_26, .fw_name = FIRMWARE_8168D_2 },
	{ .mac_version = RTL_GIGA_MAC_VER_29, .fw_name = FIRMWARE_8105E_1 },
193
	{ .mac_version = RTL_GIGA_MAC_VER_30, .fw_name = FIRMWARE_8105E_1 },
194
195
	{ .mac_version = RTL_GIGA_MAC_VER_32, .fw_name = FIRMWARE_8168E_1 },
	{ .mac_version = RTL_GIGA_MAC_VER_33, .fw_name = FIRMWARE_8168E_2 }
196
197
};

198
199
200
201
202
203
enum cfg_version {
	RTL_CFG_0 = 0x00,
	RTL_CFG_1,
	RTL_CFG_2
};

204
205
206
207
static void rtl_hw_start_8169(struct net_device *);
static void rtl_hw_start_8168(struct net_device *);
static void rtl_hw_start_8101(struct net_device *);

208
static DEFINE_PCI_DEVICE_TABLE(rtl8169_pci_tbl) = {
209
	{ PCI_DEVICE(PCI_VENDOR_ID_REALTEK,	0x8129), 0, 0, RTL_CFG_0 },
210
	{ PCI_DEVICE(PCI_VENDOR_ID_REALTEK,	0x8136), 0, 0, RTL_CFG_2 },
211
	{ PCI_DEVICE(PCI_VENDOR_ID_REALTEK,	0x8167), 0, 0, RTL_CFG_0 },
212
	{ PCI_DEVICE(PCI_VENDOR_ID_REALTEK,	0x8168), 0, 0, RTL_CFG_1 },
213
214
	{ PCI_DEVICE(PCI_VENDOR_ID_REALTEK,	0x8169), 0, 0, RTL_CFG_0 },
	{ PCI_DEVICE(PCI_VENDOR_ID_DLINK,	0x4300), 0, 0, RTL_CFG_0 },
215
	{ PCI_DEVICE(PCI_VENDOR_ID_AT,		0xc107), 0, 0, RTL_CFG_0 },
216
217
218
	{ PCI_DEVICE(0x16ec,			0x0116), 0, 0, RTL_CFG_0 },
	{ PCI_VENDOR_ID_LINKSYS,		0x1032,
		PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
219
220
	{ 0x0001,				0x8168,
		PCI_ANY_ID, 0x2410, 0, 0, RTL_CFG_2 },
Linus Torvalds's avatar
Linus Torvalds committed
221
222
223
224
225
	{0,},
};

MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);

226
static int rx_buf_sz = 16383;
227
static int use_dac;
228
229
230
static struct {
	u32 msg_enable;
} debug = { -1 };
Linus Torvalds's avatar
Linus Torvalds committed
231

Francois Romieu's avatar
Francois Romieu committed
232
233
enum rtl_registers {
	MAC0		= 0,	/* Ethernet hardware address. */
234
	MAC4		= 4,
Francois Romieu's avatar
Francois Romieu committed
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
	MAR0		= 8,	/* Multicast filter. */
	CounterAddrLow		= 0x10,
	CounterAddrHigh		= 0x14,
	TxDescStartAddrLow	= 0x20,
	TxDescStartAddrHigh	= 0x24,
	TxHDescStartAddrLow	= 0x28,
	TxHDescStartAddrHigh	= 0x2c,
	FLASH		= 0x30,
	ERSR		= 0x36,
	ChipCmd		= 0x37,
	TxPoll		= 0x38,
	IntrMask	= 0x3c,
	IntrStatus	= 0x3e,
	TxConfig	= 0x40,
	RxConfig	= 0x44,
Francois Romieu's avatar
Francois Romieu committed
250
251
252

#define RTL_RX_CONFIG_MASK		0xff7e1880u

Francois Romieu's avatar
Francois Romieu committed
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
	RxMissed	= 0x4c,
	Cfg9346		= 0x50,
	Config0		= 0x51,
	Config1		= 0x52,
	Config2		= 0x53,
	Config3		= 0x54,
	Config4		= 0x55,
	Config5		= 0x56,
	MultiIntr	= 0x5c,
	PHYAR		= 0x60,
	PHYstatus	= 0x6c,
	RxMaxSize	= 0xda,
	CPlusCmd	= 0xe0,
	IntrMitigate	= 0xe2,
	RxDescAddrLow	= 0xe4,
	RxDescAddrHigh	= 0xe8,
269
270
271
272
273
274
275
276
	EarlyTxThres	= 0xec,	/* 8169. Unit of 32 bytes. */

#define NoEarlyTx	0x3f	/* Max value : no early transmit. */

	MaxTxPacketSize	= 0xec,	/* 8101/8168. Unit of 128 bytes. */

#define TxPacketMax	(8064 >> 7)

Francois Romieu's avatar
Francois Romieu committed
277
278
279
280
	FuncEvent	= 0xf0,
	FuncEventMask	= 0xf4,
	FuncPresetState	= 0xf8,
	FuncForceEvent	= 0xfc,
Linus Torvalds's avatar
Linus Torvalds committed
281
282
};

283
284
285
286
287
288
289
290
291
292
293
294
295
296
enum rtl8110_registers {
	TBICSR			= 0x64,
	TBI_ANAR		= 0x68,
	TBI_LPAR		= 0x6a,
};

enum rtl8168_8101_registers {
	CSIDR			= 0x64,
	CSIAR			= 0x68,
#define	CSIAR_FLAG			0x80000000
#define	CSIAR_WRITE_CMD			0x80000000
#define	CSIAR_BYTE_ENABLE		0x0f
#define	CSIAR_BYTE_ENABLE_SHIFT		12
#define	CSIAR_ADDR_MASK			0x0fff
françois romieu's avatar
françois romieu committed
297
	PMCH			= 0x6f,
298
299
300
301
302
303
	EPHYAR			= 0x80,
#define	EPHYAR_FLAG			0x80000000
#define	EPHYAR_WRITE_CMD		0x80000000
#define	EPHYAR_REG_MASK			0x1f
#define	EPHYAR_REG_SHIFT		16
#define	EPHYAR_DATA_MASK		0xffff
304
305
	DLLPR			= 0xd0,
#define	PM_SWITCH			(1 << 6)
306
307
308
	DBG_REG			= 0xd1,
#define	FIX_NAK_1			(1 << 4)
#define	FIX_NAK_2			(1 << 3)
309
310
311
312
	TWSI			= 0xd2,
	MCU			= 0xd3,
#define	EN_NDP				(1 << 3)
#define	EN_OOB_RESET			(1 << 2)
313
314
315
316
317
318
319
	EFUSEAR			= 0xdc,
#define	EFUSEAR_FLAG			0x80000000
#define	EFUSEAR_WRITE_CMD		0x80000000
#define	EFUSEAR_READ_CMD		0x00000000
#define	EFUSEAR_REG_MASK		0x03ff
#define	EFUSEAR_REG_SHIFT		8
#define	EFUSEAR_DATA_MASK		0xff
320
321
};

322
enum rtl8168_registers {
françois romieu's avatar
françois romieu committed
323
324
325
326
327
328
329
330
331
332
333
334
	ERIDR			= 0x70,
	ERIAR			= 0x74,
#define ERIAR_FLAG			0x80000000
#define ERIAR_WRITE_CMD			0x80000000
#define ERIAR_READ_CMD			0x00000000
#define ERIAR_ADDR_BYTE_ALIGN		4
#define ERIAR_EXGMAC			0
#define ERIAR_MSIX			1
#define ERIAR_ASF			2
#define ERIAR_TYPE_SHIFT		16
#define ERIAR_BYTEEN			0x0f
#define ERIAR_BYTEEN_SHIFT		12
335
336
337
338
339
340
341
342
343
344
345
	EPHY_RXER_NUM		= 0x7c,
	OCPDR			= 0xb0,	/* OCP GPHY access */
#define OCPDR_WRITE_CMD			0x80000000
#define OCPDR_READ_CMD			0x00000000
#define OCPDR_REG_MASK			0x7f
#define OCPDR_GPHY_REG_SHIFT		16
#define OCPDR_DATA_MASK			0xffff
	OCPAR			= 0xb4,
#define OCPAR_FLAG			0x80000000
#define OCPAR_GPHY_WRITE_CMD		0x8000f060
#define OCPAR_GPHY_READ_CMD		0x0000f060
hayeswang's avatar
hayeswang committed
346
347
348
	RDSAR1			= 0xd0,	/* 8168c only. Undocumented on 8168dp */
	MISC			= 0xf0,	/* 8168e only. */
	txpla_rst			= (1 << 29)
349
350
};

Francois Romieu's avatar
Francois Romieu committed
351
enum rtl_register_content {
Linus Torvalds's avatar
Linus Torvalds committed
352
	/* InterruptStatusBits */
Francois Romieu's avatar
Francois Romieu committed
353
354
355
356
357
358
359
360
361
362
363
	SYSErr		= 0x8000,
	PCSTimeout	= 0x4000,
	SWInt		= 0x0100,
	TxDescUnavail	= 0x0080,
	RxFIFOOver	= 0x0040,
	LinkChg		= 0x0020,
	RxOverflow	= 0x0010,
	TxErr		= 0x0008,
	TxOK		= 0x0004,
	RxErr		= 0x0002,
	RxOK		= 0x0001,
Linus Torvalds's avatar
Linus Torvalds committed
364
365

	/* RxStatusDesc */
366
367
368
369
370
	RxFOVF	= (1 << 23),
	RxRWT	= (1 << 22),
	RxRES	= (1 << 21),
	RxRUNT	= (1 << 20),
	RxCRC	= (1 << 19),
Linus Torvalds's avatar
Linus Torvalds committed
371
372

	/* ChipCmdBits */
Francois Romieu's avatar
Francois Romieu committed
373
374
375
376
	CmdReset	= 0x10,
	CmdRxEnb	= 0x08,
	CmdTxEnb	= 0x04,
	RxBufEmpty	= 0x01,
Linus Torvalds's avatar
Linus Torvalds committed
377

378
379
380
381
382
	/* TXPoll register p.5 */
	HPQ		= 0x80,		/* Poll cmd on the high prio queue */
	NPQ		= 0x40,		/* Poll cmd on the low prio queue */
	FSWInt		= 0x01,		/* Forced software interrupt */

Linus Torvalds's avatar
Linus Torvalds committed
383
	/* Cfg9346Bits */
Francois Romieu's avatar
Francois Romieu committed
384
385
	Cfg9346_Lock	= 0x00,
	Cfg9346_Unlock	= 0xc0,
Linus Torvalds's avatar
Linus Torvalds committed
386
387

	/* rx_mode_bits */
Francois Romieu's avatar
Francois Romieu committed
388
389
390
391
392
393
	AcceptErr	= 0x20,
	AcceptRunt	= 0x10,
	AcceptBroadcast	= 0x08,
	AcceptMulticast	= 0x04,
	AcceptMyPhys	= 0x02,
	AcceptAllPhys	= 0x01,
Linus Torvalds's avatar
Linus Torvalds committed
394
395

	/* RxConfigBits */
Francois Romieu's avatar
Francois Romieu committed
396
397
	RxCfgFIFOShift	= 13,
	RxCfgDMAShift	=  8,
Linus Torvalds's avatar
Linus Torvalds committed
398
399
400
401
402

	/* TxConfigBits */
	TxInterFrameGapShift = 24,
	TxDMAShift = 8,	/* DMA burst value (0-7) is shift this many bits */

403
	/* Config1 register p.24 */
404
405
	LEDS1		= (1 << 7),
	LEDS0		= (1 << 6),
Francois Romieu's avatar
Francois Romieu committed
406
	MSIEnable	= (1 << 5),	/* Enable Message Signaled Interrupt */
407
408
409
410
	Speed_down	= (1 << 4),
	MEMMAP		= (1 << 3),
	IOMAP		= (1 << 2),
	VPD		= (1 << 1),
411
412
	PMEnable	= (1 << 0),	/* Power Management Enable */

413
414
415
416
	/* Config2 register p. 25 */
	PCI_Clock_66MHz = 0x01,
	PCI_Clock_33MHz = 0x00,

Francois Romieu's avatar
Francois Romieu committed
417
418
419
	/* Config3 register p.25 */
	MagicPacket	= (1 << 5),	/* Wake up when receives a Magic Packet */
	LinkUp		= (1 << 4),	/* Wake up when the cable connection is re-established */
420
	Beacon_en	= (1 << 0),	/* 8168 only. Reserved in the 8168b */
Francois Romieu's avatar
Francois Romieu committed
421

422
	/* Config5 register p.27 */
Francois Romieu's avatar
Francois Romieu committed
423
424
425
	BWF		= (1 << 6),	/* Accept Broadcast wakeup frame */
	MWF		= (1 << 5),	/* Accept Multicast wakeup frame */
	UWF		= (1 << 4),	/* Accept Unicast wakeup frame */
hayeswang's avatar
hayeswang committed
426
	spi_en		= (1 << 3),
Francois Romieu's avatar
Francois Romieu committed
427
	LanWake		= (1 << 1),	/* LanWake enable/disable */
428
429
	PMEStatus	= (1 << 0),	/* PME status can be reset by PCI RST# */

Linus Torvalds's avatar
Linus Torvalds committed
430
431
432
433
434
435
436
437
438
	/* TBICSR p.28 */
	TBIReset	= 0x80000000,
	TBILoopback	= 0x40000000,
	TBINwEnable	= 0x20000000,
	TBINwRestart	= 0x10000000,
	TBILinkOk	= 0x02000000,
	TBINwComplete	= 0x01000000,

	/* CPlusCmd p.31 */
439
440
441
442
443
444
445
446
447
448
	EnableBist	= (1 << 15),	// 8168 8101
	Mac_dbgo_oe	= (1 << 14),	// 8168 8101
	Normal_mode	= (1 << 13),	// unused
	Force_half_dup	= (1 << 12),	// 8168 8101
	Force_rxflow_en	= (1 << 11),	// 8168 8101
	Force_txflow_en	= (1 << 10),	// 8168 8101
	Cxpl_dbg_sel	= (1 << 9),	// 8168 8101
	ASF		= (1 << 8),	// 8168 8101
	PktCntrDisable	= (1 << 7),	// 8168 8101
	Mac_dbgo_sel	= 0x001c,	// 8168
Linus Torvalds's avatar
Linus Torvalds committed
449
450
451
452
	RxVlan		= (1 << 6),
	RxChkSum	= (1 << 5),
	PCIDAC		= (1 << 4),
	PCIMulRW	= (1 << 3),
453
454
455
456
	INTT_0		= 0x0000,	// 8168
	INTT_1		= 0x0001,	// 8168
	INTT_2		= 0x0002,	// 8168
	INTT_3		= 0x0003,	// 8168
Linus Torvalds's avatar
Linus Torvalds committed
457
458

	/* rtl8169_PHYstatus */
Francois Romieu's avatar
Francois Romieu committed
459
460
461
462
463
464
465
466
	TBI_Enable	= 0x80,
	TxFlowCtrl	= 0x40,
	RxFlowCtrl	= 0x20,
	_1000bpsF	= 0x10,
	_100bps		= 0x08,
	_10bps		= 0x04,
	LinkStatus	= 0x02,
	FullDup		= 0x01,
Linus Torvalds's avatar
Linus Torvalds committed
467
468

	/* _TBICSRBit */
Francois Romieu's avatar
Francois Romieu committed
469
	TBILinkOK	= 0x02000000,
470
471

	/* DumpCounterCommand */
Francois Romieu's avatar
Francois Romieu committed
472
	CounterDump	= 0x8,
Linus Torvalds's avatar
Linus Torvalds committed
473
474
};

Francois Romieu's avatar
Francois Romieu committed
475
476
enum rtl_desc_bit {
	/* First doubleword. */
Linus Torvalds's avatar
Linus Torvalds committed
477
478
479
480
	DescOwn		= (1 << 31), /* Descriptor is owned by NIC */
	RingEnd		= (1 << 30), /* End of descriptor ring */
	FirstFrag	= (1 << 29), /* First segment of a packet */
	LastFrag	= (1 << 28), /* Final segment of a packet */
Francois Romieu's avatar
Francois Romieu committed
481
482
483
484
485
486
487
};

/* Generic case. */
enum rtl_tx_desc_bit {
	/* First doubleword. */
	TD_LSO		= (1 << 27),		/* Large Send Offload */
#define TD_MSS_MAX			0x07ffu	/* MSS value */
Linus Torvalds's avatar
Linus Torvalds committed
488

Francois Romieu's avatar
Francois Romieu committed
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
	/* Second doubleword. */
	TxVlanTag	= (1 << 17),		/* Add VLAN tag */
};

/* 8169, 8168b and 810x except 8102e. */
enum rtl_tx_desc_bit_0 {
	/* First doubleword. */
#define TD0_MSS_SHIFT			16	/* MSS position (11 bits) */
	TD0_TCP_CS	= (1 << 16),		/* Calculate TCP/IP checksum */
	TD0_UDP_CS	= (1 << 17),		/* Calculate UDP/IP checksum */
	TD0_IP_CS	= (1 << 18),		/* Calculate IP checksum */
};

/* 8102e, 8168c and beyond. */
enum rtl_tx_desc_bit_1 {
	/* Second doubleword. */
#define TD1_MSS_SHIFT			18	/* MSS position (11 bits) */
	TD1_IP_CS	= (1 << 29),		/* Calculate IP checksum */
	TD1_TCP_CS	= (1 << 30),		/* Calculate TCP/IP checksum */
	TD1_UDP_CS	= (1 << 31),		/* Calculate UDP/IP checksum */
};
Linus Torvalds's avatar
Linus Torvalds committed
510

Francois Romieu's avatar
Francois Romieu committed
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
static const struct rtl_tx_desc_info {
	struct {
		u32 udp;
		u32 tcp;
	} checksum;
	u16 mss_shift;
	u16 opts_offset;
} tx_desc_info [] = {
	[RTL_TD_0] = {
		.checksum = {
			.udp	= TD0_IP_CS | TD0_UDP_CS,
			.tcp	= TD0_IP_CS | TD0_TCP_CS
		},
		.mss_shift	= TD0_MSS_SHIFT,
		.opts_offset	= 0
	},
	[RTL_TD_1] = {
		.checksum = {
			.udp	= TD1_IP_CS | TD1_UDP_CS,
			.tcp	= TD1_IP_CS | TD1_TCP_CS
		},
		.mss_shift	= TD1_MSS_SHIFT,
		.opts_offset	= 1
	}
};

enum rtl_rx_desc_bit {
Linus Torvalds's avatar
Linus Torvalds committed
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
	/* Rx private */
	PID1		= (1 << 18), /* Protocol ID bit 1/2 */
	PID0		= (1 << 17), /* Protocol ID bit 2/2 */

#define RxProtoUDP	(PID1)
#define RxProtoTCP	(PID0)
#define RxProtoIP	(PID1 | PID0)
#define RxProtoMask	RxProtoIP

	IPFail		= (1 << 16), /* IP checksum failed */
	UDPFail		= (1 << 15), /* UDP/IP checksum failed */
	TCPFail		= (1 << 14), /* TCP/IP checksum failed */
	RxVlanTag	= (1 << 16), /* VLAN tag available */
};

#define RsvdMask	0x3fffc000

struct TxDesc {
556
557
558
	__le32 opts1;
	__le32 opts2;
	__le64 addr;
Linus Torvalds's avatar
Linus Torvalds committed
559
560
561
};

struct RxDesc {
562
563
564
	__le32 opts1;
	__le32 opts2;
	__le64 addr;
Linus Torvalds's avatar
Linus Torvalds committed
565
566
567
568
569
570
571
572
};

struct ring_info {
	struct sk_buff	*skb;
	u32		len;
	u8		__pad[sizeof(void *) - sizeof(u32)];
};

573
enum features {
574
575
576
	RTL_FEATURE_WOL		= (1 << 0),
	RTL_FEATURE_MSI		= (1 << 1),
	RTL_FEATURE_GMII	= (1 << 2),
577
578
};

579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
struct rtl8169_counters {
	__le64	tx_packets;
	__le64	rx_packets;
	__le64	tx_errors;
	__le32	rx_errors;
	__le16	rx_missed;
	__le16	align_errors;
	__le32	tx_one_collision;
	__le32	tx_multi_collision;
	__le64	rx_unicast;
	__le64	rx_broadcast;
	__le32	rx_multicast;
	__le16	tx_aborted;
	__le16	tx_underun;
};

Linus Torvalds's avatar
Linus Torvalds committed
595
596
597
struct rtl8169_private {
	void __iomem *mmio_addr;	/* memory map physical address */
	struct pci_dev *pci_dev;	/* Index of PCI device */
David Howells's avatar
David Howells committed
598
	struct net_device *dev;
599
	struct napi_struct napi;
Linus Torvalds's avatar
Linus Torvalds committed
600
	spinlock_t lock;		/* spin lock flag */
601
	u32 msg_enable;
Francois Romieu's avatar
Francois Romieu committed
602
603
	u16 txd_version;
	u16 mac_version;
Linus Torvalds's avatar
Linus Torvalds committed
604
605
606
607
608
609
610
611
	u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
	u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
	u32 dirty_rx;
	u32 dirty_tx;
	struct TxDesc *TxDescArray;	/* 256-aligned Tx descriptor ring */
	struct RxDesc *RxDescArray;	/* 256-aligned Rx descriptor ring */
	dma_addr_t TxPhyAddr;
	dma_addr_t RxPhyAddr;
612
	void *Rx_databuff[NUM_RX_DESC];	/* Rx data buffers */
Linus Torvalds's avatar
Linus Torvalds committed
613
614
615
	struct ring_info tx_skb[NUM_TX_DESC];	/* Tx data buffers */
	struct timer_list timer;
	u16 cp_cmd;
616
617
	u16 intr_event;
	u16 napi_event;
Linus Torvalds's avatar
Linus Torvalds committed
618
619
	u16 intr_mask;
	int phy_1000_ctrl_reg;
620
621
622
623
624
625

	struct mdio_ops {
		void (*write)(void __iomem *, int, int);
		int (*read)(void __iomem *, int);
	} mdio_ops;

françois romieu's avatar
françois romieu committed
626
627
628
629
630
	struct pll_power_ops {
		void (*down)(struct rtl8169_private *);
		void (*up)(struct rtl8169_private *);
	} pll_power_ops;

631
	int (*set_speed)(struct net_device *, u8 aneg, u16 sp, u8 dpx, u32 adv);
632
	int (*get_settings)(struct net_device *, struct ethtool_cmd *);
633
	void (*phy_reset_enable)(struct rtl8169_private *tp);
634
	void (*hw_start)(struct net_device *);
635
	unsigned int (*phy_reset_pending)(struct rtl8169_private *tp);
Linus Torvalds's avatar
Linus Torvalds committed
636
	unsigned int (*link_ok)(void __iomem *);
637
	int (*do_ioctl)(struct rtl8169_private *tp, struct mii_ioctl_data *data, int cmd);
638
	int pcie_cap;
David Howells's avatar
David Howells committed
639
	struct delayed_work task;
640
	unsigned features;
641
642

	struct mii_if_info mii;
643
	struct rtl8169_counters counters;
644
	u32 saved_wolopts;
645
646

	const struct firmware *fw;
647
#define RTL_FIRMWARE_UNKNOWN	ERR_PTR(-EAGAIN);
Linus Torvalds's avatar
Linus Torvalds committed
648
649
};

650
MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
Linus Torvalds's avatar
Linus Torvalds committed
651
652
MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
module_param(use_dac, int, 0);
653
MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
654
655
module_param_named(debug, debug.msg_enable, int, 0);
MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
Linus Torvalds's avatar
Linus Torvalds committed
656
657
MODULE_LICENSE("GPL");
MODULE_VERSION(RTL8169_VERSION);
658
659
MODULE_FIRMWARE(FIRMWARE_8168D_1);
MODULE_FIRMWARE(FIRMWARE_8168D_2);
hayeswang's avatar
hayeswang committed
660
661
MODULE_FIRMWARE(FIRMWARE_8168E_1);
MODULE_FIRMWARE(FIRMWARE_8168E_2);
662
MODULE_FIRMWARE(FIRMWARE_8105E_1);
Linus Torvalds's avatar
Linus Torvalds committed
663
664

static int rtl8169_open(struct net_device *dev);
665
666
static netdev_tx_t rtl8169_start_xmit(struct sk_buff *skb,
				      struct net_device *dev);
667
static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
Linus Torvalds's avatar
Linus Torvalds committed
668
static int rtl8169_init_ring(struct net_device *dev);
669
static void rtl_hw_start(struct net_device *dev);
Linus Torvalds's avatar
Linus Torvalds committed
670
static int rtl8169_close(struct net_device *dev);
671
static void rtl_set_rx_mode(struct net_device *dev);
Linus Torvalds's avatar
Linus Torvalds committed
672
static void rtl8169_tx_timeout(struct net_device *dev);
Richard Dawe's avatar
Richard Dawe committed
673
static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
Linus Torvalds's avatar
Linus Torvalds committed
674
static int rtl8169_rx_interrupt(struct net_device *, struct rtl8169_private *,
675
				void __iomem *, u32 budget);
Richard Dawe's avatar
Richard Dawe committed
676
static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
Linus Torvalds's avatar
Linus Torvalds committed
677
static void rtl8169_down(struct net_device *dev);
678
static void rtl8169_rx_clear(struct rtl8169_private *tp);
679
static int rtl8169_poll(struct napi_struct *napi, int budget);
Linus Torvalds's avatar
Linus Torvalds committed
680
681

static const unsigned int rtl8169_rx_config =
682
	(RX_FIFO_THRESH << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift);
Linus Torvalds's avatar
Linus Torvalds committed
683

françois romieu's avatar
françois romieu committed
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
static u32 ocp_read(struct rtl8169_private *tp, u8 mask, u16 reg)
{
	void __iomem *ioaddr = tp->mmio_addr;
	int i;

	RTL_W32(OCPAR, ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
	for (i = 0; i < 20; i++) {
		udelay(100);
		if (RTL_R32(OCPAR) & OCPAR_FLAG)
			break;
	}
	return RTL_R32(OCPDR);
}

static void ocp_write(struct rtl8169_private *tp, u8 mask, u16 reg, u32 data)
{
	void __iomem *ioaddr = tp->mmio_addr;
	int i;

	RTL_W32(OCPDR, data);
	RTL_W32(OCPAR, OCPAR_FLAG | ((u32)mask & 0x0f) << 12 | (reg & 0x0fff));
	for (i = 0; i < 20; i++) {
		udelay(100);
		if ((RTL_R32(OCPAR) & OCPAR_FLAG) == 0)
			break;
	}
}

712
static void rtl8168_oob_notify(struct rtl8169_private *tp, u8 cmd)
françois romieu's avatar
françois romieu committed
713
{
714
	void __iomem *ioaddr = tp->mmio_addr;
françois romieu's avatar
françois romieu committed
715
716
717
718
719
720
721
722
723
724
725
	int i;

	RTL_W8(ERIDR, cmd);
	RTL_W32(ERIAR, 0x800010e8);
	msleep(2);
	for (i = 0; i < 5; i++) {
		udelay(100);
		if (!(RTL_R32(ERIDR) & ERIAR_FLAG))
			break;
	}

726
	ocp_write(tp, 0x1, 0x30, 0x00000001);
françois romieu's avatar
françois romieu committed
727
728
729
730
731
732
733
734
735
}

#define OOB_CMD_RESET		0x00
#define OOB_CMD_DRIVER_START	0x05
#define OOB_CMD_DRIVER_STOP	0x06

static void rtl8168_driver_start(struct rtl8169_private *tp)
{
	int i;
736
	u32 reg;
françois romieu's avatar
françois romieu committed
737
738
739

	rtl8168_oob_notify(tp, OOB_CMD_DRIVER_START);

740
741
742
743
744
	if (tp->mac_version == RTL_GIGA_MAC_VER_31)
		reg = 0xb8;
	else
		reg = 0x10;

françois romieu's avatar
françois romieu committed
745
746
	for (i = 0; i < 10; i++) {
		msleep(10);
747
		if (ocp_read(tp, 0x0f, reg) & 0x00000800)
françois romieu's avatar
françois romieu committed
748
749
750
751
752
753
754
			break;
	}
}

static void rtl8168_driver_stop(struct rtl8169_private *tp)
{
	int i;
755
	u32 reg;
françois romieu's avatar
françois romieu committed
756
757
758

	rtl8168_oob_notify(tp, OOB_CMD_DRIVER_STOP);

759
760
761
762
763
	if (tp->mac_version == RTL_GIGA_MAC_VER_31)
		reg = 0xb8;
	else
		reg = 0x10;

françois romieu's avatar
françois romieu committed
764
765
	for (i = 0; i < 10; i++) {
		msleep(10);
766
		if ((ocp_read(tp, 0x0f, reg) & 0x00000800) == 0)
françois romieu's avatar
françois romieu committed
767
768
769
770
			break;
	}
}

771
772
773
774
775
776
777
778
779
780
781
782
783
784
static int r8168dp_check_dash(struct rtl8169_private *tp)
{
	u32 reg;

	if (tp->mac_version == RTL_GIGA_MAC_VER_31)
		reg = 0xb8;
	else
		reg = 0x10;

	if (ocp_read(tp, 0xF, reg) & 0x00008000)
		return 1;
	else
		return 0;
}
françois romieu's avatar
françois romieu committed
785

786
static void r8169_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
Linus Torvalds's avatar
Linus Torvalds committed
787
788
789
{
	int i;

790
	RTL_W32(PHYAR, 0x80000000 | (reg_addr & 0x1f) << 16 | (value & 0xffff));
Linus Torvalds's avatar
Linus Torvalds committed
791

792
	for (i = 20; i > 0; i--) {
Francois Romieu's avatar
Francois Romieu committed
793
794
795
796
		/*
		 * Check if the RTL8169 has completed writing to the specified
		 * MII register.
		 */
797
		if (!(RTL_R32(PHYAR) & 0x80000000))
Linus Torvalds's avatar
Linus Torvalds committed
798
			break;
799
		udelay(25);
Linus Torvalds's avatar
Linus Torvalds committed
800
	}
801
	/*
802
803
	 * According to hardware specs a 20us delay is required after write
	 * complete indication, but before sending next command.
804
	 */
805
	udelay(20);
Linus Torvalds's avatar
Linus Torvalds committed
806
807
}

808
static int r8169_mdio_read(void __iomem *ioaddr, int reg_addr)
Linus Torvalds's avatar
Linus Torvalds committed
809
810
811
{
	int i, value = -1;

812
	RTL_W32(PHYAR, 0x0 | (reg_addr & 0x1f) << 16);
Linus Torvalds's avatar
Linus Torvalds committed
813

814
	for (i = 20; i > 0; i--) {
Francois Romieu's avatar
Francois Romieu committed
815
816
817
818
		/*
		 * Check if the RTL8169 has completed retrieving data from
		 * the specified MII register.
		 */
Linus Torvalds's avatar
Linus Torvalds committed
819
		if (RTL_R32(PHYAR) & 0x80000000) {
820
			value = RTL_R32(PHYAR) & 0xffff;
Linus Torvalds's avatar
Linus Torvalds committed
821
822
			break;
		}
823
		udelay(25);
Linus Torvalds's avatar
Linus Torvalds committed
824
	}
825
826
827
828
829
830
	/*
	 * According to hardware specs a 20us delay is required after read
	 * complete indication, but before sending next command.
	 */
	udelay(20);

Linus Torvalds's avatar
Linus Torvalds committed
831
832
833
	return value;
}

834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
static void r8168dp_1_mdio_access(void __iomem *ioaddr, int reg_addr, u32 data)
{
	int i;

	RTL_W32(OCPDR, data |
		((reg_addr & OCPDR_REG_MASK) << OCPDR_GPHY_REG_SHIFT));
	RTL_W32(OCPAR, OCPAR_GPHY_WRITE_CMD);
	RTL_W32(EPHY_RXER_NUM, 0);

	for (i = 0; i < 100; i++) {
		mdelay(1);
		if (!(RTL_R32(OCPAR) & OCPAR_FLAG))
			break;
	}
}

static void r8168dp_1_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
{
	r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_WRITE_CMD |
		(value & OCPDR_DATA_MASK));
}

static int r8168dp_1_mdio_read(void __iomem *ioaddr, int reg_addr)
{
	int i;

	r8168dp_1_mdio_access(ioaddr, reg_addr, OCPDR_READ_CMD);

	mdelay(1);
	RTL_W32(OCPAR, OCPAR_GPHY_READ_CMD);
	RTL_W32(EPHY_RXER_NUM, 0);

	for (i = 0; i < 100; i++) {
		mdelay(1);
		if (RTL_R32(OCPAR) & OCPAR_FLAG)
			break;
	}

	return RTL_R32(OCPDR) & OCPDR_DATA_MASK;
}

françois romieu's avatar
françois romieu committed
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
#define R8168DP_1_MDIO_ACCESS_BIT	0x00020000

static void r8168dp_2_mdio_start(void __iomem *ioaddr)
{
	RTL_W32(0xd0, RTL_R32(0xd0) & ~R8168DP_1_MDIO_ACCESS_BIT);
}

static void r8168dp_2_mdio_stop(void __iomem *ioaddr)
{
	RTL_W32(0xd0, RTL_R32(0xd0) | R8168DP_1_MDIO_ACCESS_BIT);
}

static void r8168dp_2_mdio_write(void __iomem *ioaddr, int reg_addr, int value)
{
	r8168dp_2_mdio_start(ioaddr);

	r8169_mdio_write(ioaddr, reg_addr, value);

	r8168dp_2_mdio_stop(ioaddr);
}

static int r8168dp_2_mdio_read(void __iomem *ioaddr, int reg_addr)
{
	int value;

	r8168dp_2_mdio_start(ioaddr);

	value = r8169_mdio_read(ioaddr, reg_addr);

	r8168dp_2_mdio_stop(ioaddr);

	return value;
}

909
static void rtl_writephy(struct rtl8169_private *tp, int location, u32 val)
910
{
911
	tp->mdio_ops.write(tp->mmio_addr, location, val);
912
913
}

914
915
static int rtl_readphy(struct rtl8169_private *tp, int location)
{
916
	return tp->mdio_ops.read(tp->mmio_addr, location);
917
918
919
920
921
922
923
924
}

static void rtl_patchphy(struct rtl8169_private *tp, int reg_addr, int value)
{
	rtl_writephy(tp, reg_addr, rtl_readphy(tp, reg_addr) | value);
}

static void rtl_w1w0_phy(struct rtl8169_private *tp, int reg_addr, int p, int m)
925
926
927
{
	int val;

928
929
	val = rtl_readphy(tp, reg_addr);
	rtl_writephy(tp, reg_addr, (val | p) & ~m);
930
931
}

932
933
934
935
936
static void rtl_mdio_write(struct net_device *dev, int phy_id, int location,
			   int val)
{
	struct rtl8169_private *tp = netdev_priv(dev);

937
	rtl_writephy(tp, location, val);
938
939
940
941
942
943
}

static int rtl_mdio_read(struct net_device *dev, int phy_id, int location)
{
	struct rtl8169_private *tp = netdev_priv(dev);

944
	return rtl_readphy(tp, location);
945
946
}

947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
static void rtl_ephy_write(void __iomem *ioaddr, int reg_addr, int value)
{
	unsigned int i;

	RTL_W32(EPHYAR, EPHYAR_WRITE_CMD | (value & EPHYAR_DATA_MASK) |
		(reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);

	for (i = 0; i < 100; i++) {
		if (!(RTL_R32(EPHYAR) & EPHYAR_FLAG))
			break;
		udelay(10);
	}
}

static u16 rtl_ephy_read(void __iomem *ioaddr, int reg_addr)
{
	u16 value = 0xffff;
	unsigned int i;

	RTL_W32(EPHYAR, (reg_addr & EPHYAR_REG_MASK) << EPHYAR_REG_SHIFT);

	for (i = 0; i < 100; i++) {
		if (RTL_R32(EPHYAR) & EPHYAR_FLAG) {
			value = RTL_R32(EPHYAR) & EPHYAR_DATA_MASK;
			break;
		}
		udelay(10);
	}

	return value;
}

static void rtl_csi_write(void __iomem *ioaddr, int addr, int value)
{
	unsigned int i;

	RTL_W32(CSIDR, value);
	RTL_W32(CSIAR, CSIAR_WRITE_CMD | (addr & CSIAR_ADDR_MASK) |
		CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);

	for (i = 0; i < 100; i++) {
		if (!(RTL_R32(CSIAR) & CSIAR_FLAG))
			break;
		udelay(10);
	}
}

static u32 rtl_csi_read(void __iomem *ioaddr, int addr)
{
	u32 value = ~0x00;
	unsigned int i;

	RTL_W32(CSIAR, (addr & CSIAR_ADDR_MASK) |
		CSIAR_BYTE_ENABLE << CSIAR_BYTE_ENABLE_SHIFT);

	for (i = 0; i < 100; i++) {
		if (RTL_R32(CSIAR) & CSIAR_FLAG) {
			value = RTL_R32(CSIDR);
			break;
		}
		udelay(10);
	}

	return value;
}

1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
static u8 rtl8168d_efuse_read(void __iomem *ioaddr, int reg_addr)
{
	u8 value = 0xff;
	unsigned int i;

	RTL_W32(EFUSEAR, (reg_addr & EFUSEAR_REG_MASK) << EFUSEAR_REG_SHIFT);

	for (i = 0; i < 300; i++) {
		if (RTL_R32(EFUSEAR) & EFUSEAR_FLAG) {
			value = RTL_R32(EFUSEAR) & EFUSEAR_DATA_MASK;
			break;
		}
		udelay(100);
	}

	return value;
}

Linus Torvalds's avatar
Linus Torvalds committed
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
static void rtl8169_irq_mask_and_ack(void __iomem *ioaddr)
{
	RTL_W16(IntrMask, 0x0000);

	RTL_W16(IntrStatus, 0xffff);
}

static void rtl8169_asic_down(void __iomem *ioaddr)
{
	RTL_W8(ChipCmd, 0x00);
	rtl8169_irq_mask_and_ack(ioaddr);
	RTL_R16(CPlusCmd);
}

1045
static unsigned int rtl8169_tbi_reset_pending(struct rtl8169_private *tp)
Linus Torvalds's avatar
Linus Torvalds committed
1046
{
1047
1048
	void __iomem *ioaddr = tp->mmio_addr;

Linus Torvalds's avatar
Linus Torvalds committed
1049
1050
1051
	return RTL_R32(TBICSR) & TBIReset;
}

1052
static unsigned int rtl8169_xmii_reset_pending(struct rtl8169_private *tp)
Linus Torvalds's avatar
Linus Torvalds committed
1053
{
1054
	return rtl_readphy(tp, MII_BMCR) & BMCR_RESET;
Linus Torvalds's avatar
Linus Torvalds committed
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
}

static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
{
	return RTL_R32(TBICSR) & TBILinkOk;
}

static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
{
	return RTL_R8(PHYstatus) & LinkStatus;
}

1067
static void rtl8169_tbi_reset_enable(struct rtl8169_private *tp)
Linus Torvalds's avatar
Linus Torvalds committed
1068
{
1069
1070
	void __iomem *ioaddr = tp->mmio_addr;

Linus Torvalds's avatar
Linus Torvalds committed
1071
1072
1073
	RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
}

1074
static void rtl8169_xmii_reset_enable(struct rtl8169_private *tp)
Linus Torvalds's avatar
Linus Torvalds committed
1075
1076
1077
{
	unsigned int val;

1078
1079
	val = rtl_readphy(tp, MII_BMCR) | BMCR_RESET;
	rtl_writephy(tp, MII_BMCR, val & 0xffff);
Linus Torvalds's avatar
Linus Torvalds committed
1080
1081
}

1082
static void __rtl8169_check_link_status(struct net_device *dev,
Francois Romieu's avatar
Francois Romieu committed
1083
				      struct rtl8169_private *tp,
1084
1085
				      void __iomem *ioaddr,
				      bool pm)
Linus Torvalds's avatar
Linus Torvalds committed
1086
1087
1088
1089
1090
{
	unsigned long flags;

	spin_lock_irqsave(&tp->lock, flags);
	if (tp->link_ok(ioaddr)) {
1091
		/* This is to cancel a scheduled suspend if there's one. */
1092
1093
		if (pm)
			pm_request_resume(&tp->pci_dev->dev);
Linus Torvalds's avatar
Linus Torvalds committed
1094
		netif_carrier_on(dev);
1095
1096
		if (net_ratelimit())
			netif_info(tp, ifup, dev, "link up\n");
1097
	} else {
Linus Torvalds's avatar
Linus Torvalds committed
1098
		netif_carrier_off(dev);
1099
		netif_info(tp, ifdown, dev, "link down\n");
1100
1101
		if (pm)
			pm_schedule_suspend(&tp->pci_dev->dev, 100);
1102
	}
Linus Torvalds's avatar
Linus Torvalds committed
1103
1104
1105
	spin_unlock_irqrestore(&tp->lock, flags);
}

1106
1107
1108
1109
1110
1111
1112
static void rtl8169_check_link_status(struct net_device *dev,
				      struct rtl8169_private *tp,
				      void __iomem *ioaddr)
{
	__rtl8169_check_link_status(dev, tp, ioaddr, false);
}

1113
1114
1115
#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)

static u32 __rtl8169_get_wol(struct rtl8169_private *tp)
Francois Romieu's avatar
Francois Romieu committed
1116
1117
1118
{
	void __iomem *ioaddr = tp->mmio_addr;
	u8 options;
1119
	u32 wolopts = 0;
Francois Romieu's avatar
Francois Romieu committed
1120
1121
1122

	options = RTL_R8(Config1);
	if (!(options & PMEnable))
1123
		return 0;
Francois Romieu's avatar
Francois Romieu committed
1124
1125
1126

	options = RTL_R8(Config3);
	if (options & LinkUp)
1127
		wolopts |= WAKE_PHY;
Francois Romieu's avatar
Francois Romieu committed
1128
	if (options & MagicPacket)
1129
		wolopts |= WAKE_MAGIC;
Francois Romieu's avatar
Francois Romieu committed
1130
1131
1132

	options = RTL_R8(Config5);
	if (options & UWF)
1133
		wolopts |= WAKE_UCAST;
Francois Romieu's avatar
Francois Romieu committed
1134
	if (options & BWF)
1135
		wolopts |= WAKE_BCAST;
Francois Romieu's avatar
Francois Romieu committed
1136
	if (options & MWF)
1137
		wolopts |= WAKE_MCAST;
Francois Romieu's avatar
Francois Romieu committed
1138

1139
	return wolopts;
Francois Romieu's avatar
Francois Romieu committed
1140
1141
}

1142
static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
Francois Romieu's avatar
Francois Romieu committed
1143
1144
{
	struct rtl8169_private *tp = netdev_priv(dev);
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155

	spin_lock_irq(&tp->lock);

	wol->supported = WAKE_ANY;
	wol->wolopts = __rtl8169_get_wol(tp);

	spin_unlock_irq(&tp->lock);
}

static void __rtl8169_set_wol(struct rtl8169_private *tp, u32 wolopts)
{
Francois Romieu's avatar
Francois Romieu committed
1156
	void __iomem *ioaddr = tp->mmio_addr;
Francois Romieu's avatar
Francois Romieu committed
1157
	unsigned int i;
1158
	static const struct {
Francois Romieu's avatar
Francois Romieu committed
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
		u32 opt;
		u16 reg;
		u8  mask;
	} cfg[] = {
		{ WAKE_ANY,   Config1, PMEnable },
		{ WAKE_PHY,   Config3, LinkUp },
		{ WAKE_MAGIC, Config3, MagicPacket },
		{ WAKE_UCAST, Config5, UWF },
		{ WAKE_BCAST, Config5, BWF },
		{ WAKE_MCAST, Config5, MWF },
		{ WAKE_ANY,   Config5, LanWake }
	};

	RTL_W8(Cfg9346, Cfg9346_Unlock);

	for (i = 0; i < ARRAY_SIZE(cfg); i++) {
		u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
1176
		if (wolopts & cfg[i].opt)
Francois Romieu's avatar
Francois Romieu committed
1177
1178
1179
1180
1181
			options |= cfg[i].mask;
		RTL_W8(cfg[i].reg, options);
	}

	RTL_W8(Cfg9346, Cfg9346_Lock);
1182
1183
1184
1185
1186
1187
1188
}

static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
{
	struct rtl8169_private *tp = netdev_priv(dev);

	spin_lock_irq(&tp->lock);
Francois Romieu's avatar
Francois Romieu committed
1189

1190
1191
1192
1193
	if (wol->wolopts)
		tp->features |= RTL_FEATURE_WOL;
	else
		tp->features &= ~RTL_FEATURE_WOL;
1194
	__rtl8169_set_wol(tp, wol->wolopts);
Francois Romieu's avatar
Francois Romieu committed
1195
1196
	spin_unlock_irq(&tp->lock);

1197
1198
	device_set_wakeup_enable(&tp->pci_dev->dev, wol->wolopts);

Francois Romieu's avatar
Francois Romieu committed
1199
1200
1201
	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
static void rtl8169_get_drvinfo(struct net_device *dev,
				struct ethtool_drvinfo *info)
{
	struct rtl8169_private *tp = netdev_priv(dev);

	strcpy(info->driver, MODULENAME);
	strcpy(info->version, RTL8169_VERSION);
	strcpy(info->bus_info, pci_name(tp->pci_dev));
}

static int rtl8169_get_regs_len(struct net_device *dev)
{
	return R8169_REGS_SIZE;
}

static int rtl8169_set_speed_tbi(struct net_device *dev,
1218
				 u8 autoneg, u16 speed, u8 duplex, u32 ignored)
Linus Torvalds's avatar
Linus Torvalds committed
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
{
	struct rtl8169_private *tp = netdev_priv(dev);
	void __iomem *ioaddr = tp->mmio_addr;
	int ret = 0;
	u32 reg;

	reg = RTL_R32(TBICSR);
	if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
	    (duplex == DUPLEX_FULL)) {
		RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
	} else if (autoneg == AUTONEG_ENABLE)
		RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
	else {
1232
1233
		netif_warn(tp, link, dev,
			   "incorrect speed setting refused in TBI mode\n");
Linus Torvalds's avatar
Linus Torvalds committed
1234
1235
1236
1237
1238
1239
1240
		ret = -EOPNOTSUPP;
	}

	return ret;
}

static int rtl8169_set_speed_xmii(struct net_device *dev,
1241
				  u8 autoneg, u16 speed, u8 duplex, u32 adv)
Linus Torvalds's avatar
Linus Torvalds committed
1242
1243
{
	struct rtl8169_private *tp = netdev_priv(dev);
1244
	int giga_ctrl, bmcr;
1245
	int rc = -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
1246

1247
	rtl_writephy(tp, 0x1f, 0x0000);
Linus Torvalds's avatar
Linus Torvalds committed
1248
1249

	if (autoneg == AUTONEG_ENABLE) {
1250
1251
		int auto_nego;

1252
		auto_nego = rtl_readphy(tp, MII_ADVERTISE);
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
		auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
				ADVERTISE_100HALF | ADVERTISE_100FULL);

		if (adv & ADVERTISED_10baseT_Half)
			auto_nego |= ADVERTISE_10HALF;
		if (adv & ADVERTISED_10baseT_Full)
			auto_nego |= ADVERTISE_10FULL;
		if (adv & ADVERTISED_100baseT_Half)
			auto_nego |= ADVERTISE_100HALF;
		if (adv & ADVERTISED_100baseT_Full)
			auto_nego |= ADVERTISE_100FULL;

1265
		auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
Linus Torvalds's avatar
Linus Torvalds committed
1266

1267
		giga_ctrl = rtl_readphy(tp, MII_CTRL1000);
1268
		giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
1269

1270
1271
1272
1273
1274
1275
1276
1277
		/* The 8100e/8101e/8102e do Fast Ethernet only. */
		if ((tp->mac_version != RTL_GIGA_MAC_VER_07) &&
		    (tp->mac_version != RTL_GIGA_MAC_VER_08) &&
		    (tp->mac_version != RTL_GIGA_MAC_VER_09) &&
		    (tp->mac_version != RTL_GIGA_MAC_VER_10) &&
		    (tp->mac_version != RTL_GIGA_MAC_VER_13) &&
		    (tp->mac_version != RTL_GIGA_MAC_VER_14) &&
		    (tp->mac_version != RTL_GIGA_MAC_VER_15) &&
1278
1279
1280
		    (tp->mac_version != RTL_GIGA_MAC_VER_16) &&
		    (tp->mac_version != RTL_GIGA_MAC_VER_29) &&
		    (tp->mac_version != RTL_GIGA_MAC_VER_30)) {
1281
1282
1283
1284
1285
1286
			if (adv & ADVERTISED_1000baseT_Half)
				giga_ctrl |= ADVERTISE_1000HALF;
			if (adv & ADVERTISED_1000baseT_Full)
				giga_ctrl |= ADVERTISE_1000FULL;
		} else if (adv & (ADVERTISED_1000baseT_Half |
				  ADVERTISED_1000baseT_Full)) {
1287
1288
			netif_info(tp, link, dev,
				   "PHY does not support 1000Mbps\n");
1289
			goto out;
1290
		}
Linus Torvalds's avatar
Linus Torvalds committed
1291

1292
1293
		bmcr = BMCR_ANENABLE | BMCR_ANRESTART;

1294
1295
		rtl_writephy(tp, MII_ADVERTISE, auto_nego);
		rtl_writephy(tp, MII_CTRL1000, giga_ctrl);
1296
1297
1298
1299
1300
1301
1302
1303
	} else {
		giga_ctrl = 0;

		if (speed == SPEED_10)
			bmcr = 0;
		else if (speed == SPEED_100)
			bmcr = BMCR_SPEED100;
		else
1304
			goto out;
1305
1306
1307

		if (duplex == DUPLEX_FULL)
			bmcr |= BMCR_FULLDPLX;
Roger So's avatar
Roger So committed
1308
1309
	}

Linus Torvalds's avatar
Linus Torvalds committed
1310
1311
	tp->phy_1000_ctrl_reg = giga_ctrl;

1312
	rtl_writephy(tp, MII_BMCR, bmcr);
1313
1314
1315
1316

	if ((tp->mac_version == RTL_GIGA_MAC_VER_02) ||
	    (tp->mac_version == RTL_GIGA_MAC_VER_03)) {
		if ((speed == SPEED_100) && (autoneg != AUTONEG_ENABLE)) {
1317
1318
			rtl_writephy(tp, 0x17, 0x2138);
			rtl_writephy(tp, 0x0e, 0x0260);
1319
		} else {
1320
1321
			rtl_writephy(tp, 0x17, 0x2108);
			rtl_writephy(tp, 0x0e, 0x0000);
1322
1323
1324
		}
	}

1325
1326
1327
	rc = 0;
out:
	return rc;
Linus Torvalds's avatar
Linus Torvalds committed
1328
1329
1330
}

static int rtl8169_set_speed(struct net_device *dev,
1331
			     u8 autoneg, u16 speed, u8 duplex, u32 advertising)
Linus Torvalds's avatar
Linus Torvalds committed
1332
1333
1334
1335
{
	struct rtl8169_private *tp = netdev_priv(dev);
	int ret;

1336
	ret = tp->set_speed(dev, autoneg, speed, duplex, advertising);
Linus Torvalds's avatar
Linus Torvalds committed
1337

1338
	if (netif_running(dev) && (tp->phy_1000_ctrl_reg & ADVERTISE_1000FULL))
Linus Torvalds's avatar
Linus Torvalds committed
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
		mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);

	return ret;
}

static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
{
	struct rtl8169_private *tp = netdev_priv(dev);
	unsigned long flags;
	int ret;

	spin_lock_irqsave(&tp->lock, flags);
1351
1352
	ret = rtl8169_set_speed(dev,
		cmd->autoneg, cmd->speed, cmd->duplex, cmd->advertising);
Linus Torvalds's avatar
Linus Torvalds committed
1353
	spin_unlock_irqrestore(&tp->lock, flags);
1354

Linus Torvalds's avatar
Linus Torvalds committed
1355
1356
1357
	return ret;