r8152.c 103 KB
Newer Older
hayeswang's avatar
hayeswang committed
1
/*
2
 *  Copyright (c) 2014 Realtek Semiconductor Corp. All rights reserved.
hayeswang's avatar
hayeswang committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 */

#include <linux/signal.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/mii.h>
#include <linux/ethtool.h>
#include <linux/usb.h>
#include <linux/crc32.h>
#include <linux/if_vlan.h>
#include <linux/uaccess.h>
hayeswang's avatar
hayeswang committed
21
#include <linux/list.h>
hayeswang's avatar
hayeswang committed
22
23
#include <linux/ip.h>
#include <linux/ipv6.h>
hayeswang's avatar
hayeswang committed
24
#include <net/ip6_checksum.h>
hayeswang's avatar
hayeswang committed
25
26
#include <uapi/linux/mdio.h>
#include <linux/mdio.h>
hayeswang's avatar
hayeswang committed
27
#include <linux/usb/cdc.h>
hayeswang's avatar
hayeswang committed
28
#include <linux/suspend.h>
29
#include <linux/acpi.h>
hayeswang's avatar
hayeswang committed
30

hayeswang's avatar
hayeswang committed
31
32
33
34
/* Information for net-next */
#define NETNEXT_VERSION		"08"

/* Information for net */
35
#define NET_VERSION		"6"
hayeswang's avatar
hayeswang committed
36
37

#define DRIVER_VERSION		"v1." NETNEXT_VERSION "." NET_VERSION
hayeswang's avatar
hayeswang committed
38
#define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>"
hayeswang's avatar
hayeswang committed
39
#define DRIVER_DESC "Realtek RTL8152/RTL8153 Based USB Ethernet Adapters"
hayeswang's avatar
hayeswang committed
40
41
42
43
44
45
46
47
48
49
#define MODULENAME "r8152"

#define R8152_PHY_ID		32

#define PLA_IDR			0xc000
#define PLA_RCR			0xc010
#define PLA_RMS			0xc016
#define PLA_RXFIFO_CTRL0	0xc0a0
#define PLA_RXFIFO_CTRL1	0xc0a4
#define PLA_RXFIFO_CTRL2	0xc0a8
hayeswang's avatar
hayeswang committed
50
#define PLA_DMY_REG0		0xc0b0
hayeswang's avatar
hayeswang committed
51
52
#define PLA_FMC			0xc0b4
#define PLA_CFG_WOL		0xc0b6
hayeswang's avatar
hayeswang committed
53
#define PLA_TEREDO_CFG		0xc0bc
hayeswang's avatar
hayeswang committed
54
#define PLA_MAR			0xcd00
hayeswang's avatar
hayeswang committed
55
#define PLA_BACKUP		0xd000
hayeswang's avatar
hayeswang committed
56
#define PAL_BDC_CR		0xd1a0
hayeswang's avatar
hayeswang committed
57
58
#define PLA_TEREDO_TIMER	0xd2cc
#define PLA_REALWOW_TIMER	0xd2e8
hayeswang's avatar
hayeswang committed
59
60
61
#define PLA_LEDSEL		0xdd90
#define PLA_LED_FEATURE		0xdd92
#define PLA_PHYAR		0xde00
hayeswang's avatar
hayeswang committed
62
#define PLA_BOOT_CTRL		0xe004
hayeswang's avatar
hayeswang committed
63
64
65
66
#define PLA_GPHY_INTR_IMR	0xe022
#define PLA_EEE_CR		0xe040
#define PLA_EEEP_CR		0xe080
#define PLA_MAC_PWR_CTRL	0xe0c0
hayeswang's avatar
hayeswang committed
67
68
69
70
#define PLA_MAC_PWR_CTRL2	0xe0ca
#define PLA_MAC_PWR_CTRL3	0xe0cc
#define PLA_MAC_PWR_CTRL4	0xe0ce
#define PLA_WDT6_CTRL		0xe428
hayeswang's avatar
hayeswang committed
71
72
#define PLA_TCR0		0xe610
#define PLA_TCR1		0xe612
73
#define PLA_MTPS		0xe615
hayeswang's avatar
hayeswang committed
74
#define PLA_TXFIFO_CTRL		0xe618
75
#define PLA_RSTTALLY		0xe800
hayeswang's avatar
hayeswang committed
76
77
#define PLA_CR			0xe813
#define PLA_CRWECR		0xe81c
hayeswang's avatar
hayeswang committed
78
79
#define PLA_CONFIG12		0xe81e	/* CONFIG1, CONFIG2 */
#define PLA_CONFIG34		0xe820	/* CONFIG3, CONFIG4 */
hayeswang's avatar
hayeswang committed
80
81
82
83
84
85
86
#define PLA_CONFIG5		0xe822
#define PLA_PHY_PWR		0xe84c
#define PLA_OOB_CTRL		0xe84f
#define PLA_CPCR		0xe854
#define PLA_MISC_0		0xe858
#define PLA_MISC_1		0xe85a
#define PLA_OCP_GPHY_BASE	0xe86c
87
#define PLA_TALLYCNT		0xe890
hayeswang's avatar
hayeswang committed
88
89
90
91
92
93
94
95
96
97
98
#define PLA_SFF_STS_7		0xe8de
#define PLA_PHYSTATUS		0xe908
#define PLA_BP_BA		0xfc26
#define PLA_BP_0		0xfc28
#define PLA_BP_1		0xfc2a
#define PLA_BP_2		0xfc2c
#define PLA_BP_3		0xfc2e
#define PLA_BP_4		0xfc30
#define PLA_BP_5		0xfc32
#define PLA_BP_6		0xfc34
#define PLA_BP_7		0xfc36
hayeswang's avatar
hayeswang committed
99
#define PLA_BP_EN		0xfc38
hayeswang's avatar
hayeswang committed
100

hayeswang's avatar
hayeswang committed
101
102
#define USB_USB2PHY		0xb41e
#define USB_SSPHYLINK2		0xb428
hayeswang's avatar
hayeswang committed
103
#define USB_U2P3_CTRL		0xb460
hayeswang's avatar
hayeswang committed
104
105
#define USB_CSR_DUMMY1		0xb464
#define USB_CSR_DUMMY2		0xb466
hayeswang's avatar
hayeswang committed
106
#define USB_DEV_STAT		0xb808
hayeswang's avatar
hayeswang committed
107
108
#define USB_CONNECT_TIMER	0xcbf8
#define USB_BURST_SIZE		0xcfc0
hayeswang's avatar
hayeswang committed
109
110
111
112
113
#define USB_USB_CTRL		0xd406
#define USB_PHY_CTRL		0xd408
#define USB_TX_AGG		0xd40a
#define USB_RX_BUF_TH		0xd40c
#define USB_USB_TIMER		0xd428
hayeswang's avatar
hayeswang committed
114
115
#define USB_RX_EARLY_TIMEOUT	0xd42c
#define USB_RX_EARLY_SIZE	0xd42e
hayeswang's avatar
hayeswang committed
116
117
#define USB_PM_CTRL_STATUS	0xd432
#define USB_TX_DMA		0xd434
hayeswang's avatar
hayeswang committed
118
119
#define USB_TOLERANCE		0xd490
#define USB_LPM_CTRL		0xd41a
hayeswang's avatar
hayeswang committed
120
#define USB_BMU_RESET		0xd4b0
hayeswang's avatar
hayeswang committed
121
#define USB_UPS_CTRL		0xd800
hayeswang's avatar
hayeswang committed
122
123
124
125
#define USB_MISC_0		0xd81a
#define USB_POWER_CUT		0xd80a
#define USB_AFE_CTRL2		0xd824
#define USB_WDT11_CTRL		0xe43c
hayeswang's avatar
hayeswang committed
126
127
128
129
130
131
132
133
134
#define USB_BP_BA		0xfc26
#define USB_BP_0		0xfc28
#define USB_BP_1		0xfc2a
#define USB_BP_2		0xfc2c
#define USB_BP_3		0xfc2e
#define USB_BP_4		0xfc30
#define USB_BP_5		0xfc32
#define USB_BP_6		0xfc34
#define USB_BP_7		0xfc36
hayeswang's avatar
hayeswang committed
135
#define USB_BP_EN		0xfc38
hayeswang's avatar
hayeswang committed
136
137
138
139
140
141

/* OCP Registers */
#define OCP_ALDPS_CONFIG	0x2010
#define OCP_EEE_CONFIG1		0x2080
#define OCP_EEE_CONFIG2		0x2092
#define OCP_EEE_CONFIG3		0x2094
142
#define OCP_BASE_MII		0xa400
hayeswang's avatar
hayeswang committed
143
144
#define OCP_EEE_AR		0xa41a
#define OCP_EEE_DATA		0xa41c
hayeswang's avatar
hayeswang committed
145
146
147
148
149
150
#define OCP_PHY_STATUS		0xa420
#define OCP_POWER_CFG		0xa430
#define OCP_EEE_CFG		0xa432
#define OCP_SRAM_ADDR		0xa436
#define OCP_SRAM_DATA		0xa438
#define OCP_DOWN_SPEED		0xa442
hayeswang's avatar
hayeswang committed
151
#define OCP_EEE_ABLE		0xa5c4
hayeswang's avatar
hayeswang committed
152
#define OCP_EEE_ADV		0xa5d0
hayeswang's avatar
hayeswang committed
153
#define OCP_EEE_LPABLE		0xa5d2
154
#define OCP_PHY_STATE		0xa708		/* nway state for 8153 */
hayeswang's avatar
hayeswang committed
155
156
157
158
159
160
161
#define OCP_ADC_CFG		0xbc06

/* SRAM Register */
#define SRAM_LPF_CFG		0x8012
#define SRAM_10M_AMP1		0x8080
#define SRAM_10M_AMP2		0x8082
#define SRAM_IMPEDANCE		0x8084
hayeswang's avatar
hayeswang committed
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177

/* PLA_RCR */
#define RCR_AAP			0x00000001
#define RCR_APM			0x00000002
#define RCR_AM			0x00000004
#define RCR_AB			0x00000008
#define RCR_ACPT_ALL		(RCR_AAP | RCR_APM | RCR_AM | RCR_AB)

/* PLA_RXFIFO_CTRL0 */
#define RXFIFO_THR1_NORMAL	0x00080002
#define RXFIFO_THR1_OOB		0x01800003

/* PLA_RXFIFO_CTRL1 */
#define RXFIFO_THR2_FULL	0x00000060
#define RXFIFO_THR2_HIGH	0x00000038
#define RXFIFO_THR2_OOB		0x0000004a
hayeswang's avatar
hayeswang committed
178
#define RXFIFO_THR2_NORMAL	0x00a0
hayeswang's avatar
hayeswang committed
179
180
181
182
183

/* PLA_RXFIFO_CTRL2 */
#define RXFIFO_THR3_FULL	0x00000078
#define RXFIFO_THR3_HIGH	0x00000048
#define RXFIFO_THR3_OOB		0x0000005a
hayeswang's avatar
hayeswang committed
184
#define RXFIFO_THR3_NORMAL	0x0110
hayeswang's avatar
hayeswang committed
185
186
187

/* PLA_TXFIFO_CTRL */
#define TXFIFO_THR_NORMAL	0x00400008
hayeswang's avatar
hayeswang committed
188
#define TXFIFO_THR_NORMAL2	0x01000008
hayeswang's avatar
hayeswang committed
189

hayeswang's avatar
hayeswang committed
190
191
192
/* PLA_DMY_REG0 */
#define ECM_ALDPS		0x0002

hayeswang's avatar
hayeswang committed
193
194
195
196
197
198
/* PLA_FMC */
#define FMC_FCR_MCU_EN		0x0001

/* PLA_EEEP_CR */
#define EEEP_CR_EEEP_TX		0x0002

hayeswang's avatar
hayeswang committed
199
200
201
/* PLA_WDT6_CTRL */
#define WDT6_SET_MODE		0x0010

hayeswang's avatar
hayeswang committed
202
203
204
205
206
207
208
/* PLA_TCR0 */
#define TCR0_TX_EMPTY		0x0800
#define TCR0_AUTO_FIFO		0x0080

/* PLA_TCR1 */
#define VERSION_MASK		0x7cf0

209
210
211
212
/* PLA_MTPS */
#define MTPS_JUMBO		(12 * 1024 / 64)
#define MTPS_DEFAULT		(6 * 1024 / 64)

213
214
215
/* PLA_RSTTALLY */
#define TALLY_RESET		0x0001

hayeswang's avatar
hayeswang committed
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* PLA_CR */
#define CR_RST			0x10
#define CR_RE			0x08
#define CR_TE			0x04

/* PLA_CRWECR */
#define CRWECR_NORAML		0x00
#define CRWECR_CONFIG		0xc0

/* PLA_OOB_CTRL */
#define NOW_IS_OOB		0x80
#define TXFIFO_EMPTY		0x20
#define RXFIFO_EMPTY		0x10
#define LINK_LIST_READY		0x02
#define DIS_MCU_CLROOB		0x01
#define FIFO_EMPTY		(TXFIFO_EMPTY | RXFIFO_EMPTY)

/* PLA_MISC_1 */
#define RXDY_GATED_EN		0x0008

/* PLA_SFF_STS_7 */
#define RE_INIT_LL		0x8000
#define MCU_BORW_EN		0x4000

/* PLA_CPCR */
#define CPCR_RX_VLAN		0x0040

/* PLA_CFG_WOL */
#define MAGIC_EN		0x0001

hayeswang's avatar
hayeswang committed
246
247
248
249
250
251
/* PLA_TEREDO_CFG */
#define TEREDO_SEL		0x8000
#define TEREDO_WAKE_MASK	0x7f00
#define TEREDO_RS_EVENT_MASK	0x00fe
#define OOB_TEREDO_EN		0x0001

hayeswang's avatar
hayeswang committed
252
253
254
/* PAL_BDC_CR */
#define ALDPS_PROXY_MODE	0x0001

hayeswang's avatar
hayeswang committed
255
256
257
258
/* PLA_CONFIG34 */
#define LINK_ON_WAKE_EN		0x0010
#define LINK_OFF_WAKE_EN	0x0008

hayeswang's avatar
hayeswang committed
259
/* PLA_CONFIG5 */
hayeswang's avatar
hayeswang committed
260
261
262
#define BWF_EN			0x0040
#define MWF_EN			0x0020
#define UWF_EN			0x0010
hayeswang's avatar
hayeswang committed
263
264
265
266
267
268
269
270
271
272
273
274
275
#define LAN_WAKE_EN		0x0002

/* PLA_LED_FEATURE */
#define LED_MODE_MASK		0x0700

/* PLA_PHY_PWR */
#define TX_10M_IDLE_EN		0x0080
#define PFM_PWM_SWITCH		0x0040

/* PLA_MAC_PWR_CTRL */
#define D3_CLK_GATED_EN		0x00004000
#define MCU_CLK_RATIO		0x07010f07
#define MCU_CLK_RATIO_MASK	0x0f0f0f0f
hayeswang's avatar
hayeswang committed
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#define ALDPS_SPDWN_RATIO	0x0f87

/* PLA_MAC_PWR_CTRL2 */
#define EEE_SPDWN_RATIO		0x8007

/* PLA_MAC_PWR_CTRL3 */
#define PKT_AVAIL_SPDWN_EN	0x0100
#define SUSPEND_SPDWN_EN	0x0004
#define U1U2_SPDWN_EN		0x0002
#define L1_SPDWN_EN		0x0001

/* PLA_MAC_PWR_CTRL4 */
#define PWRSAVE_SPDWN_EN	0x1000
#define RXDV_SPDWN_EN		0x0800
#define TX10MIDLE_EN		0x0100
#define TP100_SPDWN_EN		0x0020
#define TP500_SPDWN_EN		0x0010
#define TP1000_SPDWN_EN		0x0008
#define EEE_SPDWN_EN		0x0001
hayeswang's avatar
hayeswang committed
295
296
297
298
299
300
301
302
303
304
305
306
307
308

/* PLA_GPHY_INTR_IMR */
#define GPHY_STS_MSK		0x0001
#define SPEED_DOWN_MSK		0x0002
#define SPDWN_RXDV_MSK		0x0004
#define SPDWN_LINKCHG_MSK	0x0008

/* PLA_PHYAR */
#define PHYAR_FLAG		0x80000000

/* PLA_EEE_CR */
#define EEE_RX_EN		0x0001
#define EEE_TX_EN		0x0002

hayeswang's avatar
hayeswang committed
309
310
311
/* PLA_BOOT_CTRL */
#define AUTOLOAD_DONE		0x0002

hayeswang's avatar
hayeswang committed
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/* USB_USB2PHY */
#define USB2PHY_SUSPEND		0x0001
#define USB2PHY_L1		0x0002

/* USB_SSPHYLINK2 */
#define pwd_dn_scale_mask	0x3ffe
#define pwd_dn_scale(x)		((x) << 1)

/* USB_CSR_DUMMY1 */
#define DYNAMIC_BURST		0x0001

/* USB_CSR_DUMMY2 */
#define EP4_FULL_FC		0x0001

hayeswang's avatar
hayeswang committed
326
327
328
/* USB_DEV_STAT */
#define STAT_SPEED_MASK		0x0006
#define STAT_SPEED_HIGH		0x0000
329
#define STAT_SPEED_FULL		0x0002
hayeswang's avatar
hayeswang committed
330
331
332
333
334

/* USB_TX_AGG */
#define TX_AGG_MAX_THRESHOLD	0x03

/* USB_RX_BUF_TH */
hayeswang's avatar
hayeswang committed
335
#define RX_THR_SUPPER		0x0c350180
hayeswang's avatar
hayeswang committed
336
#define RX_THR_HIGH		0x7a120180
hayeswang's avatar
hayeswang committed
337
#define RX_THR_SLOW		0xffff0180
hayeswang's avatar
hayeswang committed
338
339
340
341
342

/* USB_TX_DMA */
#define TEST_MODE_DISABLE	0x00000001
#define TX_SIZE_ADJUST1		0x00000100

hayeswang's avatar
hayeswang committed
343
344
345
346
/* USB_BMU_RESET */
#define BMU_RESET_EP_IN		0x01
#define BMU_RESET_EP_OUT	0x02

hayeswang's avatar
hayeswang committed
347
348
349
350
/* USB_UPS_CTRL */
#define POWER_CUT		0x0100

/* USB_PM_CTRL_STATUS */
hayeswang's avatar
hayeswang committed
351
#define RESUME_INDICATE		0x0001
hayeswang's avatar
hayeswang committed
352
353
354

/* USB_USB_CTRL */
#define RX_AGG_DISABLE		0x0010
355
#define RX_ZERO_EN		0x0080
hayeswang's avatar
hayeswang committed
356

hayeswang's avatar
hayeswang committed
357
358
359
360
361
362
363
364
365
366
/* USB_U2P3_CTRL */
#define U2P3_ENABLE		0x0001

/* USB_POWER_CUT */
#define PWR_EN			0x0001
#define PHASE2_EN		0x0008

/* USB_MISC_0 */
#define PCUT_STATUS		0x0001

hayeswang's avatar
hayeswang committed
367
368
369
370
/* USB_RX_EARLY_TIMEOUT */
#define COALESCE_SUPER		 85000U
#define COALESCE_HIGH		250000U
#define COALESCE_SLOW		524280U
hayeswang's avatar
hayeswang committed
371
372
373
374
375

/* USB_WDT11_CTRL */
#define TIMER11_EN		0x0001

/* USB_LPM_CTRL */
hayeswang's avatar
hayeswang committed
376
377
378
/* bit 4 ~ 5: fifo empty boundary */
#define FIFO_EMPTY_1FB		0x30	/* 0x1fb * 64 = 32448 bytes */
/* bit 2 ~ 3: LMP timer */
hayeswang's avatar
hayeswang committed
379
380
381
#define LPM_TIMER_MASK		0x0c
#define LPM_TIMER_500MS		0x04	/* 500 ms */
#define LPM_TIMER_500US		0x0c	/* 500 us */
hayeswang's avatar
hayeswang committed
382
#define ROK_EXIT_LPM		0x02
hayeswang's avatar
hayeswang committed
383
384
385
386
387
388

/* USB_AFE_CTRL2 */
#define SEN_VAL_MASK		0xf800
#define SEN_VAL_NORMAL		0xa000
#define SEL_RXIDLE		0x0100

hayeswang's avatar
hayeswang committed
389
390
391
392
393
394
/* OCP_ALDPS_CONFIG */
#define ENPWRSAVE		0x8000
#define ENPDNPS			0x0200
#define LINKENA			0x0100
#define DIS_SDSAVE		0x0010

hayeswang's avatar
hayeswang committed
395
396
397
398
399
400
401
402
403
404
/* OCP_PHY_STATUS */
#define PHY_STAT_MASK		0x0007
#define PHY_STAT_LAN_ON		3
#define PHY_STAT_PWRDN		5

/* OCP_POWER_CFG */
#define EEE_CLKDIV_EN		0x8000
#define EN_ALDPS		0x0004
#define EN_10M_PLLOFF		0x0001

hayeswang's avatar
hayeswang committed
405
406
407
408
409
410
411
/* OCP_EEE_CONFIG1 */
#define RG_TXLPI_MSK_HFDUP	0x8000
#define RG_MATCLR_EN		0x4000
#define EEE_10_CAP		0x2000
#define EEE_NWAY_EN		0x1000
#define TX_QUIET_EN		0x0200
#define RX_QUIET_EN		0x0100
hayeswang's avatar
hayeswang committed
412
#define sd_rise_time_mask	0x0070
hayeswang's avatar
hayeswang committed
413
#define sd_rise_time(x)		(min(x, 7) << 4)	/* bit 4 ~ 6 */
hayeswang's avatar
hayeswang committed
414
415
416
417
418
419
420
421
422
423
424
#define RG_RXLPI_MSK_HFDUP	0x0008
#define SDFALLTIME		0x0007	/* bit 0 ~ 2 */

/* OCP_EEE_CONFIG2 */
#define RG_LPIHYS_NUM		0x7000	/* bit 12 ~ 15 */
#define RG_DACQUIET_EN		0x0400
#define RG_LDVQUIET_EN		0x0200
#define RG_CKRSEL		0x0020
#define RG_EEEPRG_EN		0x0010

/* OCP_EEE_CONFIG3 */
hayeswang's avatar
hayeswang committed
425
#define fast_snr_mask		0xff80
hayeswang's avatar
hayeswang committed
426
#define fast_snr(x)		(min(x, 0x1ff) << 7)	/* bit 7 ~ 15 */
hayeswang's avatar
hayeswang committed
427
428
429
430
431
432
433
434
435
#define RG_LFS_SEL		0x0060	/* bit 6 ~ 5 */
#define MSK_PH			0x0006	/* bit 0 ~ 3 */

/* OCP_EEE_AR */
/* bit[15:14] function */
#define FUN_ADDR		0x0000
#define FUN_DATA		0x4000
/* bit[4:0] device addr */

hayeswang's avatar
hayeswang committed
436
437
438
439
440
441
442
/* OCP_EEE_CFG */
#define CTAP_SHORT_EN		0x0040
#define EEE10_EN		0x0010

/* OCP_DOWN_SPEED */
#define EN_10M_BGOFF		0x0080

443
444
445
446
/* OCP_PHY_STATE */
#define TXDIS_STATE		0x01
#define ABD_STATE		0x02

hayeswang's avatar
hayeswang committed
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/* OCP_ADC_CFG */
#define CKADSEL_L		0x0100
#define ADC_EN			0x0080
#define EN_EMI_L		0x0040

/* SRAM_LPF_CFG */
#define LPF_AUTO_TUNE		0x8000

/* SRAM_10M_AMP1 */
#define GDAC_IB_UPALL		0x0008

/* SRAM_10M_AMP2 */
#define AMP_DN			0x0200

/* SRAM_IMPEDANCE */
#define RX_DRIVING_MASK		0x6000

464
465
466
467
468
/* MAC PASSTHRU */
#define AD_MASK			0xfee0
#define EFUSE			0xcfdb
#define PASS_THRU_MASK		0x1

hayeswang's avatar
hayeswang committed
469
enum rtl_register_content {
hayeswang's avatar
hayeswang committed
470
	_1000bps	= 0x10,
hayeswang's avatar
hayeswang committed
471
472
473
474
475
476
	_100bps		= 0x08,
	_10bps		= 0x04,
	LINK_STATUS	= 0x02,
	FULL_DUP	= 0x01,
};

hayeswang's avatar
hayeswang committed
477
#define RTL8152_MAX_TX		4
hayeswang's avatar
hayeswang committed
478
#define RTL8152_MAX_RX		10
479
#define INTBUFSIZE		2
hayeswang's avatar
hayeswang committed
480
481
482
#define CRC_SIZE		4
#define TX_ALIGN		4
#define RX_ALIGN		8
483
484

#define INTR_LINK		0x0004
hayeswang's avatar
hayeswang committed
485

hayeswang's avatar
hayeswang committed
486
487
488
489
490
491
492
493
494
495
496
497
#define RTL8152_REQT_READ	0xc0
#define RTL8152_REQT_WRITE	0x40
#define RTL8152_REQ_GET_REGS	0x05
#define RTL8152_REQ_SET_REGS	0x05

#define BYTE_EN_DWORD		0xff
#define BYTE_EN_WORD		0x33
#define BYTE_EN_BYTE		0x11
#define BYTE_EN_SIX_BYTES	0x3f
#define BYTE_EN_START_MASK	0x0f
#define BYTE_EN_END_MASK	0xf0

498
499
#define RTL8153_MAX_PACKET	9216 /* 9K */
#define RTL8153_MAX_MTU		(RTL8153_MAX_PACKET - VLAN_ETH_HLEN - VLAN_HLEN)
hayeswang's avatar
hayeswang committed
500
#define RTL8152_RMS		(VLAN_ETH_FRAME_LEN + VLAN_HLEN)
501
#define RTL8153_RMS		RTL8153_MAX_PACKET
hayeswang's avatar
hayeswang committed
502
#define RTL8152_TX_TIMEOUT	(5 * HZ)
hayeswang's avatar
hayeswang committed
503
#define RTL8152_NAPI_WEIGHT	64
hayeswang's avatar
hayeswang committed
504
505
506
507
508

/* rtl8152 flags */
enum rtl8152_flags {
	RTL8152_UNPLUG = 0,
	RTL8152_SET_RX_MODE,
509
510
	WORK_ENABLE,
	RTL8152_LINK_CHG,
hayeswang's avatar
hayeswang committed
511
	SELECTIVE_SUSPEND,
512
	PHY_RESET,
hayeswang's avatar
hayeswang committed
513
	SCHEDULE_NAPI,
hayeswang's avatar
hayeswang committed
514
515
516
517
};

/* Define these values to match your device */
#define VENDOR_ID_REALTEK		0x0bda
hayeswang's avatar
hayeswang committed
518
#define VENDOR_ID_SAMSUNG		0x04e8
519
#define VENDOR_ID_LENOVO		0x17ef
520
#define VENDOR_ID_NVIDIA		0x0955
hayeswang's avatar
hayeswang committed
521
522
523
524

#define MCU_TYPE_PLA			0x0100
#define MCU_TYPE_USB			0x0000

525
526
527
528
529
530
531
532
533
534
535
536
537
struct tally_counter {
	__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;
hayeswang's avatar
hayeswang committed
538
	__le16	tx_underrun;
539
540
};

hayeswang's avatar
hayeswang committed
541
struct rx_desc {
542
	__le32 opts1;
hayeswang's avatar
hayeswang committed
543
#define RX_LEN_MASK			0x7fff
hayeswang's avatar
hayeswang committed
544

545
	__le32 opts2;
hayeswang's avatar
hayeswang committed
546
547
548
549
#define RD_UDP_CS			BIT(23)
#define RD_TCP_CS			BIT(22)
#define RD_IPV6_CS			BIT(20)
#define RD_IPV4_CS			BIT(19)
hayeswang's avatar
hayeswang committed
550

551
	__le32 opts3;
hayeswang's avatar
hayeswang committed
552
553
554
555
#define IPF				BIT(23) /* IP checksum fail */
#define UDPF				BIT(22) /* UDP checksum fail */
#define TCPF				BIT(21) /* TCP checksum fail */
#define RX_VLAN_TAG			BIT(16)
hayeswang's avatar
hayeswang committed
556

557
558
559
	__le32 opts4;
	__le32 opts5;
	__le32 opts6;
hayeswang's avatar
hayeswang committed
560
561
562
};

struct tx_desc {
563
	__le32 opts1;
hayeswang's avatar
hayeswang committed
564
565
566
567
#define TX_FS			BIT(31) /* First segment of a packet */
#define TX_LS			BIT(30) /* Final segment of a packet */
#define GTSENDV4		BIT(28)
#define GTSENDV6		BIT(27)
hayeswang's avatar
hayeswang committed
568
#define GTTCPHO_SHIFT		18
hayeswang's avatar
hayeswang committed
569
#define GTTCPHO_MAX		0x7fU
hayeswang's avatar
hayeswang committed
570
#define TX_LEN_MAX		0x3ffffU
hayeswang's avatar
hayeswang committed
571

572
	__le32 opts2;
hayeswang's avatar
hayeswang committed
573
574
575
576
#define UDP_CS			BIT(31) /* Calculate UDP/IP checksum */
#define TCP_CS			BIT(30) /* Calculate TCP/IP checksum */
#define IPV4_CS			BIT(29) /* Calculate IPv4 checksum */
#define IPV6_CS			BIT(28) /* Calculate IPv6 checksum */
hayeswang's avatar
hayeswang committed
577
578
579
#define MSS_SHIFT		17
#define MSS_MAX			0x7ffU
#define TCPHO_SHIFT		17
hayeswang's avatar
hayeswang committed
580
#define TCPHO_MAX		0x7ffU
hayeswang's avatar
hayeswang committed
581
#define TX_VLAN_TAG		BIT(16)
hayeswang's avatar
hayeswang committed
582
583
};

584
585
struct r8152;

hayeswang's avatar
hayeswang committed
586
587
588
struct rx_agg {
	struct list_head list;
	struct urb *urb;
589
	struct r8152 *context;
hayeswang's avatar
hayeswang committed
590
591
592
593
594
595
596
	void *buffer;
	void *head;
};

struct tx_agg {
	struct list_head list;
	struct urb *urb;
597
	struct r8152 *context;
hayeswang's avatar
hayeswang committed
598
599
600
601
602
603
	void *buffer;
	void *head;
	u32 skb_num;
	u32 skb_len;
};

hayeswang's avatar
hayeswang committed
604
605
606
struct r8152 {
	unsigned long flags;
	struct usb_device *udev;
hayeswang's avatar
hayeswang committed
607
	struct napi_struct napi;
608
	struct usb_interface *intf;
hayeswang's avatar
hayeswang committed
609
	struct net_device *netdev;
610
	struct urb *intr_urb;
hayeswang's avatar
hayeswang committed
611
612
613
	struct tx_agg tx_info[RTL8152_MAX_TX];
	struct rx_agg rx_info[RTL8152_MAX_RX];
	struct list_head rx_done, tx_free;
hayeswang's avatar
hayeswang committed
614
	struct sk_buff_head tx_queue, rx_queue;
hayeswang's avatar
hayeswang committed
615
	spinlock_t rx_lock, tx_lock;
616
	struct delayed_work schedule, hw_phy_work;
hayeswang's avatar
hayeswang committed
617
	struct mii_if_info mii;
hayeswang's avatar
hayeswang committed
618
	struct mutex control;	/* use for hw setting */
hayeswang's avatar
hayeswang committed
619
620
621
#ifdef CONFIG_PM_SLEEP
	struct notifier_block pm_notifier;
#endif
hayeswang's avatar
hayeswang committed
622
623
624
625
626

	struct rtl_ops {
		void (*init)(struct r8152 *);
		int (*enable)(struct r8152 *);
		void (*disable)(struct r8152 *);
627
		void (*up)(struct r8152 *);
hayeswang's avatar
hayeswang committed
628
629
		void (*down)(struct r8152 *);
		void (*unload)(struct r8152 *);
hayeswang's avatar
hayeswang committed
630
631
		int (*eee_get)(struct r8152 *, struct ethtool_eee *);
		int (*eee_set)(struct r8152 *, struct ethtool_eee *);
632
		bool (*in_nway)(struct r8152 *);
633
		void (*hw_phy_cfg)(struct r8152 *);
634
		void (*autosuspend_en)(struct r8152 *tp, bool enable);
hayeswang's avatar
hayeswang committed
635
636
	} rtl_ops;

637
	int intr_interval;
hayeswang's avatar
hayeswang committed
638
	u32 saved_wolopts;
hayeswang's avatar
hayeswang committed
639
	u32 msg_enable;
640
	u32 tx_qlen;
hayeswang's avatar
hayeswang committed
641
	u32 coalesce;
hayeswang's avatar
hayeswang committed
642
	u16 ocp_base;
hayeswang's avatar
hayeswang committed
643
	u16 speed;
644
	u8 *intr_buff;
hayeswang's avatar
hayeswang committed
645
	u8 version;
hayeswang's avatar
hayeswang committed
646
647
	u8 duplex;
	u8 autoneg;
hayeswang's avatar
hayeswang committed
648
649
650
651
652
};

enum rtl_version {
	RTL_VER_UNKNOWN = 0,
	RTL_VER_01,
hayeswang's avatar
hayeswang committed
653
654
655
656
	RTL_VER_02,
	RTL_VER_03,
	RTL_VER_04,
	RTL_VER_05,
657
	RTL_VER_06,
hayeswang's avatar
hayeswang committed
658
	RTL_VER_MAX
hayeswang's avatar
hayeswang committed
659
660
};

hayeswang's avatar
hayeswang committed
661
662
663
664
665
666
enum tx_csum_stat {
	TX_CSUM_SUCCESS = 0,
	TX_CSUM_TSO,
	TX_CSUM_NONE
};

hayeswang's avatar
hayeswang committed
667
668
669
670
/* 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.
 */
static const int multicast_filter_limit = 32;
hayeswang's avatar
hayeswang committed
671
static unsigned int agg_buf_sz = 16384;
hayeswang's avatar
hayeswang committed
672

hayeswang's avatar
hayeswang committed
673
#define RTL_LIMITED_TSO_SIZE	(agg_buf_sz - sizeof(struct tx_desc) - \
hayeswang's avatar
hayeswang committed
674
675
				 VLAN_ETH_HLEN - VLAN_HLEN)

hayeswang's avatar
hayeswang committed
676
677
678
static
int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
{
679
680
681
682
683
684
685
686
	int ret;
	void *tmp;

	tmp = kmalloc(size, GFP_KERNEL);
	if (!tmp)
		return -ENOMEM;

	ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0),
687
688
			      RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
			      value, index, tmp, size, 500);
689
690
691
692
693

	memcpy(data, tmp, size);
	kfree(tmp);

	return ret;
hayeswang's avatar
hayeswang committed
694
695
696
697
698
}

static
int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
{
699
700
701
	int ret;
	void *tmp;

702
	tmp = kmemdup(data, size, GFP_KERNEL);
703
704
705
706
	if (!tmp)
		return -ENOMEM;

	ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0),
707
708
			      RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
			      value, index, tmp, size, 500);
709
710

	kfree(tmp);
711

712
	return ret;
hayeswang's avatar
hayeswang committed
713
714
715
}

static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
716
			    void *data, u16 type)
hayeswang's avatar
hayeswang committed
717
{
718
719
	u16 limit = 64;
	int ret = 0;
hayeswang's avatar
hayeswang committed
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751

	if (test_bit(RTL8152_UNPLUG, &tp->flags))
		return -ENODEV;

	/* both size and indix must be 4 bytes align */
	if ((size & 3) || !size || (index & 3) || !data)
		return -EPERM;

	if ((u32)index + (u32)size > 0xffff)
		return -EPERM;

	while (size) {
		if (size > limit) {
			ret = get_registers(tp, index, type, limit, data);
			if (ret < 0)
				break;

			index += limit;
			data += limit;
			size -= limit;
		} else {
			ret = get_registers(tp, index, type, size, data);
			if (ret < 0)
				break;

			index += size;
			data += size;
			size = 0;
			break;
		}
	}

752
753
754
	if (ret == -ENODEV)
		set_bit(RTL8152_UNPLUG, &tp->flags);

hayeswang's avatar
hayeswang committed
755
756
757
758
	return ret;
}

static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
759
			     u16 size, void *data, u16 type)
hayeswang's avatar
hayeswang committed
760
{
761
762
763
	int ret;
	u16 byteen_start, byteen_end, byen;
	u16 limit = 512;
hayeswang's avatar
hayeswang committed
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792

	if (test_bit(RTL8152_UNPLUG, &tp->flags))
		return -ENODEV;

	/* both size and indix must be 4 bytes align */
	if ((size & 3) || !size || (index & 3) || !data)
		return -EPERM;

	if ((u32)index + (u32)size > 0xffff)
		return -EPERM;

	byteen_start = byteen & BYTE_EN_START_MASK;
	byteen_end = byteen & BYTE_EN_END_MASK;

	byen = byteen_start | (byteen_start << 4);
	ret = set_registers(tp, index, type | byen, 4, data);
	if (ret < 0)
		goto error1;

	index += 4;
	data += 4;
	size -= 4;

	if (size) {
		size -= 4;

		while (size) {
			if (size > limit) {
				ret = set_registers(tp, index,
793
794
						    type | BYTE_EN_DWORD,
						    limit, data);
hayeswang's avatar
hayeswang committed
795
796
797
798
799
800
801
802
				if (ret < 0)
					goto error1;

				index += limit;
				data += limit;
				size -= limit;
			} else {
				ret = set_registers(tp, index,
803
804
						    type | BYTE_EN_DWORD,
						    size, data);
hayeswang's avatar
hayeswang committed
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
				if (ret < 0)
					goto error1;

				index += size;
				data += size;
				size = 0;
				break;
			}
		}

		byen = byteen_end | (byteen_end >> 4);
		ret = set_registers(tp, index, type | byen, 4, data);
		if (ret < 0)
			goto error1;
	}

error1:
822
823
824
	if (ret == -ENODEV)
		set_bit(RTL8152_UNPLUG, &tp->flags);

hayeswang's avatar
hayeswang committed
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
	return ret;
}

static inline
int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
{
	return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
}

static inline
int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
{
	return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
}

static inline
int usb_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
{
	return generic_ocp_read(tp, index, size, data, MCU_TYPE_USB);
}

static inline
int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
{
	return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
}

static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
{
854
	__le32 data;
hayeswang's avatar
hayeswang committed
855

856
	generic_ocp_read(tp, index, sizeof(data), &data, type);
hayeswang's avatar
hayeswang committed
857
858
859
860
861
862

	return __le32_to_cpu(data);
}

static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
{
863
864
865
	__le32 tmp = __cpu_to_le32(data);

	generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
hayeswang's avatar
hayeswang committed
866
867
868
869
870
}

static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
{
	u32 data;
871
	__le32 tmp;
hayeswang's avatar
hayeswang committed
872
873
874
875
	u8 shift = index & 2;

	index &= ~3;

876
	generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswang's avatar
hayeswang committed
877

878
	data = __le32_to_cpu(tmp);
hayeswang's avatar
hayeswang committed
879
880
881
882
883
884
885
886
	data >>= (shift * 8);
	data &= 0xffff;

	return (u16)data;
}

static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
{
887
888
	u32 mask = 0xffff;
	__le32 tmp;
hayeswang's avatar
hayeswang committed
889
890
891
892
893
894
895
896
897
898
899
900
	u16 byen = BYTE_EN_WORD;
	u8 shift = index & 2;

	data &= mask;

	if (index & 2) {
		byen <<= shift;
		mask <<= (shift * 8);
		data <<= (shift * 8);
		index &= ~3;
	}

901
	tmp = __cpu_to_le32(data);
hayeswang's avatar
hayeswang committed
902

903
	generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswang's avatar
hayeswang committed
904
905
906
907
908
}

static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
{
	u32 data;
909
	__le32 tmp;
hayeswang's avatar
hayeswang committed
910
911
912
913
	u8 shift = index & 3;

	index &= ~3;

914
	generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswang's avatar
hayeswang committed
915

916
	data = __le32_to_cpu(tmp);
hayeswang's avatar
hayeswang committed
917
918
919
920
921
922
923
924
	data >>= (shift * 8);
	data &= 0xff;

	return (u8)data;
}

static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
{
925
926
	u32 mask = 0xff;
	__le32 tmp;
hayeswang's avatar
hayeswang committed
927
928
929
930
931
932
933
934
935
936
937
938
	u16 byen = BYTE_EN_BYTE;
	u8 shift = index & 3;

	data &= mask;

	if (index & 3) {
		byen <<= shift;
		mask <<= (shift * 8);
		data <<= (shift * 8);
		index &= ~3;
	}

939
	tmp = __cpu_to_le32(data);
hayeswang's avatar
hayeswang committed
940

941
	generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswang's avatar
hayeswang committed
942
943
}

944
static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
945
946
947
948
949
950
951
952
953
954
{
	u16 ocp_base, ocp_index;

	ocp_base = addr & 0xf000;
	if (ocp_base != tp->ocp_base) {
		ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
		tp->ocp_base = ocp_base;
	}

	ocp_index = (addr & 0x0fff) | 0xb000;
955
	return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
956
957
}

958
static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
hayeswang's avatar
hayeswang committed
959
{
960
	u16 ocp_base, ocp_index;
hayeswang's avatar
hayeswang committed
961

962
963
964
965
	ocp_base = addr & 0xf000;
	if (ocp_base != tp->ocp_base) {
		ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
		tp->ocp_base = ocp_base;
hayeswang's avatar
hayeswang committed
966
	}
967
968
969

	ocp_index = (addr & 0x0fff) | 0xb000;
	ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
hayeswang's avatar
hayeswang committed
970
971
}

972
static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
hayeswang's avatar
hayeswang committed
973
{
974
975
	ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
}
hayeswang's avatar
hayeswang committed
976

977
978
979
static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
{
	return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2);
hayeswang's avatar
hayeswang committed
980
981
}

hayeswang's avatar
hayeswang committed
982
983
984
985
986
987
static void sram_write(struct r8152 *tp, u16 addr, u16 data)
{
	ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
	ocp_reg_write(tp, OCP_SRAM_DATA, data);
}

hayeswang's avatar
hayeswang committed
988
989
990
static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
{
	struct r8152 *tp = netdev_priv(netdev);
hayeswang's avatar
hayeswang committed
991
	int ret;
hayeswang's avatar
hayeswang committed
992

hayeswang's avatar
hayeswang committed
993
994
995
	if (test_bit(RTL8152_UNPLUG, &tp->flags))
		return -ENODEV;

hayeswang's avatar
hayeswang committed
996
997
998
	if (phy_id != R8152_PHY_ID)
		return -EINVAL;

hayeswang's avatar
hayeswang committed
999
1000
	ret = r8152_mdio_read(tp, reg);

For faster browsing, not all history is shown. View entire blame