r8152.c 81.3 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

/* Version Information */
hayeswang's avatar
hayeswang committed
27
#define DRIVER_VERSION "v1.06.1 (2014/10/01)"
hayeswang's avatar
hayeswang committed
28
#define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>"
hayeswang's avatar
hayeswang committed
29
#define DRIVER_DESC "Realtek RTL8152/RTL8153 Based USB Ethernet Adapters"
hayeswang's avatar
hayeswang committed
30
31
32
33
34
35
36
37
38
39
40
41
#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
#define PLA_FMC			0xc0b4
#define PLA_CFG_WOL		0xc0b6
hayeswang's avatar
hayeswang committed
42
#define PLA_TEREDO_CFG		0xc0bc
hayeswang's avatar
hayeswang committed
43
#define PLA_MAR			0xcd00
hayeswang's avatar
hayeswang committed
44
#define PLA_BACKUP		0xd000
hayeswang's avatar
hayeswang committed
45
#define PAL_BDC_CR		0xd1a0
hayeswang's avatar
hayeswang committed
46
47
#define PLA_TEREDO_TIMER	0xd2cc
#define PLA_REALWOW_TIMER	0xd2e8
hayeswang's avatar
hayeswang committed
48
49
50
#define PLA_LEDSEL		0xdd90
#define PLA_LED_FEATURE		0xdd92
#define PLA_PHYAR		0xde00
hayeswang's avatar
hayeswang committed
51
#define PLA_BOOT_CTRL		0xe004
hayeswang's avatar
hayeswang committed
52
53
54
55
#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
56
57
58
59
#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
60
61
#define PLA_TCR0		0xe610
#define PLA_TCR1		0xe612
62
#define PLA_MTPS		0xe615
hayeswang's avatar
hayeswang committed
63
#define PLA_TXFIFO_CTRL		0xe618
64
#define PLA_RSTTALLY		0xe800
hayeswang's avatar
hayeswang committed
65
66
#define PLA_CR			0xe813
#define PLA_CRWECR		0xe81c
hayeswang's avatar
hayeswang committed
67
68
#define PLA_CONFIG12		0xe81e	/* CONFIG1, CONFIG2 */
#define PLA_CONFIG34		0xe820	/* CONFIG3, CONFIG4 */
hayeswang's avatar
hayeswang committed
69
70
71
72
73
74
75
#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
76
#define PLA_TALLYCNT		0xe890
hayeswang's avatar
hayeswang committed
77
78
79
80
81
82
83
84
85
86
87
#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
88
#define PLA_BP_EN		0xfc38
hayeswang's avatar
hayeswang committed
89

hayeswang's avatar
hayeswang committed
90
#define USB_U2P3_CTRL		0xb460
hayeswang's avatar
hayeswang committed
91
92
93
94
95
96
#define USB_DEV_STAT		0xb808
#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
97
#define USB_RX_EARLY_AGG	0xd42c
hayeswang's avatar
hayeswang committed
98
99
#define USB_PM_CTRL_STATUS	0xd432
#define USB_TX_DMA		0xd434
hayeswang's avatar
hayeswang committed
100
101
#define USB_TOLERANCE		0xd490
#define USB_LPM_CTRL		0xd41a
hayeswang's avatar
hayeswang committed
102
#define USB_UPS_CTRL		0xd800
hayeswang's avatar
hayeswang committed
103
104
105
106
#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
107
108
109
110
111
112
113
114
115
#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
116
#define USB_BP_EN		0xfc38
hayeswang's avatar
hayeswang committed
117
118
119
120
121
122

/* OCP Registers */
#define OCP_ALDPS_CONFIG	0x2010
#define OCP_EEE_CONFIG1		0x2080
#define OCP_EEE_CONFIG2		0x2092
#define OCP_EEE_CONFIG3		0x2094
123
#define OCP_BASE_MII		0xa400
hayeswang's avatar
hayeswang committed
124
125
#define OCP_EEE_AR		0xa41a
#define OCP_EEE_DATA		0xa41c
hayeswang's avatar
hayeswang committed
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#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
#define OCP_EEE_CFG2		0xa5d0
#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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155

/* 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
156
#define RXFIFO_THR2_NORMAL	0x00a0
hayeswang's avatar
hayeswang committed
157
158
159
160
161

/* PLA_RXFIFO_CTRL2 */
#define RXFIFO_THR3_FULL	0x00000078
#define RXFIFO_THR3_HIGH	0x00000048
#define RXFIFO_THR3_OOB		0x0000005a
hayeswang's avatar
hayeswang committed
162
#define RXFIFO_THR3_NORMAL	0x0110
hayeswang's avatar
hayeswang committed
163
164
165

/* PLA_TXFIFO_CTRL */
#define TXFIFO_THR_NORMAL	0x00400008
hayeswang's avatar
hayeswang committed
166
#define TXFIFO_THR_NORMAL2	0x01000008
hayeswang's avatar
hayeswang committed
167
168
169
170
171
172
173

/* PLA_FMC */
#define FMC_FCR_MCU_EN		0x0001

/* PLA_EEEP_CR */
#define EEEP_CR_EEEP_TX		0x0002

hayeswang's avatar
hayeswang committed
174
175
176
/* PLA_WDT6_CTRL */
#define WDT6_SET_MODE		0x0010

hayeswang's avatar
hayeswang committed
177
178
179
180
181
182
183
/* PLA_TCR0 */
#define TCR0_TX_EMPTY		0x0800
#define TCR0_AUTO_FIFO		0x0080

/* PLA_TCR1 */
#define VERSION_MASK		0x7cf0

184
185
186
187
/* PLA_MTPS */
#define MTPS_JUMBO		(12 * 1024 / 64)
#define MTPS_DEFAULT		(6 * 1024 / 64)

188
189
190
/* PLA_RSTTALLY */
#define TALLY_RESET		0x0001

hayeswang's avatar
hayeswang committed
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* 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
221
222
223
224
225
226
/* 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
227
228
229
/* PAL_BDC_CR */
#define ALDPS_PROXY_MODE	0x0001

hayeswang's avatar
hayeswang committed
230
231
232
233
/* PLA_CONFIG34 */
#define LINK_ON_WAKE_EN		0x0010
#define LINK_OFF_WAKE_EN	0x0008

hayeswang's avatar
hayeswang committed
234
/* PLA_CONFIG5 */
hayeswang's avatar
hayeswang committed
235
236
237
#define BWF_EN			0x0040
#define MWF_EN			0x0020
#define UWF_EN			0x0010
hayeswang's avatar
hayeswang committed
238
239
240
241
242
243
244
245
246
247
248
249
250
#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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#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
270
271
272
273
274
275
276
277
278
279
280
281
282
283

/* 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
284
285
286
/* PLA_BOOT_CTRL */
#define AUTOLOAD_DONE		0x0002

hayeswang's avatar
hayeswang committed
287
288
289
/* USB_DEV_STAT */
#define STAT_SPEED_MASK		0x0006
#define STAT_SPEED_HIGH		0x0000
290
#define STAT_SPEED_FULL		0x0002
hayeswang's avatar
hayeswang committed
291
292
293
294
295

/* USB_TX_AGG */
#define TX_AGG_MAX_THRESHOLD	0x03

/* USB_RX_BUF_TH */
hayeswang's avatar
hayeswang committed
296
#define RX_THR_SUPPER		0x0c350180
hayeswang's avatar
hayeswang committed
297
#define RX_THR_HIGH		0x7a120180
hayeswang's avatar
hayeswang committed
298
#define RX_THR_SLOW		0xffff0180
hayeswang's avatar
hayeswang committed
299
300
301
302
303
304
305
306
307

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

/* USB_UPS_CTRL */
#define POWER_CUT		0x0100

/* USB_PM_CTRL_STATUS */
hayeswang's avatar
hayeswang committed
308
#define RESUME_INDICATE		0x0001
hayeswang's avatar
hayeswang committed
309
310
311
312

/* USB_USB_CTRL */
#define RX_AGG_DISABLE		0x0010

hayeswang's avatar
hayeswang committed
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/* 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

/* USB_RX_EARLY_AGG */
#define EARLY_AGG_SUPPER	0x0e832981
#define EARLY_AGG_HIGH		0x0e837a12
#define EARLY_AGG_SLOW		0x0e83ffff

/* USB_WDT11_CTRL */
#define TIMER11_EN		0x0001

/* USB_LPM_CTRL */
#define LPM_TIMER_MASK		0x0c
#define LPM_TIMER_500MS		0x04	/* 500 ms */
#define LPM_TIMER_500US		0x0c	/* 500 us */

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

hayeswang's avatar
hayeswang committed
341
342
343
344
345
346
/* OCP_ALDPS_CONFIG */
#define ENPWRSAVE		0x8000
#define ENPDNPS			0x0200
#define LINKENA			0x0100
#define DIS_SDSAVE		0x0010

hayeswang's avatar
hayeswang committed
347
348
349
350
351
352
353
354
355
356
/* 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/* 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
#define SDRISETIME		0x0010	/* bit 4 ~ 6 */
#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 */
#define FST_SNR_EYE_R		0x1500	/* bit 7 ~ 15 */
#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 */
#define DEVICE_ADDR		0x0007

/* OCP_EEE_DATA */
#define EEE_ADDR		0x003C
#define EEE_DATA		0x0002

hayeswang's avatar
hayeswang committed
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/* OCP_EEE_CFG */
#define CTAP_SHORT_EN		0x0040
#define EEE10_EN		0x0010

/* OCP_DOWN_SPEED */
#define EN_10M_BGOFF		0x0080

/* OCP_EEE_CFG2 */
#define MY1000_EEE		0x0004
#define MY100_EEE		0x0002

/* 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

hayeswang's avatar
hayeswang committed
419
enum rtl_register_content {
hayeswang's avatar
hayeswang committed
420
	_1000bps	= 0x10,
hayeswang's avatar
hayeswang committed
421
422
423
424
425
426
	_100bps		= 0x08,
	_10bps		= 0x04,
	LINK_STATUS	= 0x02,
	FULL_DUP	= 0x01,
};

hayeswang's avatar
hayeswang committed
427
428
#define RTL8152_MAX_TX		10
#define RTL8152_MAX_RX		10
429
#define INTBUFSIZE		2
hayeswang's avatar
hayeswang committed
430
431
432
#define CRC_SIZE		4
#define TX_ALIGN		4
#define RX_ALIGN		8
433
434

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

hayeswang's avatar
hayeswang committed
436
437
438
439
440
441
442
443
444
445
446
447
#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

448
449
#define RTL8153_MAX_PACKET	9216 /* 9K */
#define RTL8153_MAX_MTU		(RTL8153_MAX_PACKET - VLAN_ETH_HLEN - VLAN_HLEN)
hayeswang's avatar
hayeswang committed
450
#define RTL8152_RMS		(VLAN_ETH_FRAME_LEN + VLAN_HLEN)
451
#define RTL8153_RMS		RTL8153_MAX_PACKET
hayeswang's avatar
hayeswang committed
452
#define RTL8152_TX_TIMEOUT	(5 * HZ)
hayeswang's avatar
hayeswang committed
453
454
455
456
457

/* rtl8152 flags */
enum rtl8152_flags {
	RTL8152_UNPLUG = 0,
	RTL8152_SET_RX_MODE,
458
459
	WORK_ENABLE,
	RTL8152_LINK_CHG,
hayeswang's avatar
hayeswang committed
460
	SELECTIVE_SUSPEND,
461
	PHY_RESET,
462
	SCHEDULE_TASKLET,
hayeswang's avatar
hayeswang committed
463
464
465
466
467
};

/* Define these values to match your device */
#define VENDOR_ID_REALTEK		0x0bda
#define PRODUCT_ID_RTL8152		0x8152
hayeswang's avatar
hayeswang committed
468
469
470
471
#define PRODUCT_ID_RTL8153		0x8153

#define VENDOR_ID_SAMSUNG		0x04e8
#define PRODUCT_ID_SAMSUNG		0xa101
hayeswang's avatar
hayeswang committed
472
473
474
475

#define MCU_TYPE_PLA			0x0100
#define MCU_TYPE_USB			0x0000

476
477
478
#define REALTEK_USB_DEVICE(vend, prod)	\
	USB_DEVICE_INTERFACE_CLASS(vend, prod, USB_CLASS_VENDOR_SPEC)

479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
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;
	__le16	tx_underun;
};

hayeswang's avatar
hayeswang committed
495
struct rx_desc {
496
	__le32 opts1;
hayeswang's avatar
hayeswang committed
497
#define RX_LEN_MASK			0x7fff
hayeswang's avatar
hayeswang committed
498

499
	__le32 opts2;
hayeswang's avatar
hayeswang committed
500
501
#define RD_UDP_CS			(1 << 23)
#define RD_TCP_CS			(1 << 22)
hayeswang's avatar
hayeswang committed
502
#define RD_IPV6_CS			(1 << 20)
hayeswang's avatar
hayeswang committed
503
504
#define RD_IPV4_CS			(1 << 19)

505
	__le32 opts3;
hayeswang's avatar
hayeswang committed
506
507
508
509
#define IPF				(1 << 23) /* IP checksum fail */
#define UDPF				(1 << 22) /* UDP checksum fail */
#define TCPF				(1 << 21) /* TCP checksum fail */

510
511
512
	__le32 opts4;
	__le32 opts5;
	__le32 opts6;
hayeswang's avatar
hayeswang committed
513
514
515
};

struct tx_desc {
516
	__le32 opts1;
hayeswang's avatar
hayeswang committed
517
518
#define TX_FS			(1 << 31) /* First segment of a packet */
#define TX_LS			(1 << 30) /* Final segment of a packet */
hayeswang's avatar
hayeswang committed
519
#define GTSENDV4		(1 << 28)
hayeswang's avatar
hayeswang committed
520
#define GTSENDV6		(1 << 27)
hayeswang's avatar
hayeswang committed
521
#define GTTCPHO_SHIFT		18
hayeswang's avatar
hayeswang committed
522
#define GTTCPHO_MAX		0x7fU
hayeswang's avatar
hayeswang committed
523
#define TX_LEN_MAX		0x3ffffU
hayeswang's avatar
hayeswang committed
524

525
	__le32 opts2;
hayeswang's avatar
hayeswang committed
526
527
528
529
#define UDP_CS			(1 << 31) /* Calculate UDP/IP checksum */
#define TCP_CS			(1 << 30) /* Calculate TCP/IP checksum */
#define IPV4_CS			(1 << 29) /* Calculate IPv4 checksum */
#define IPV6_CS			(1 << 28) /* Calculate IPv6 checksum */
hayeswang's avatar
hayeswang committed
530
531
532
#define MSS_SHIFT		17
#define MSS_MAX			0x7ffU
#define TCPHO_SHIFT		17
hayeswang's avatar
hayeswang committed
533
#define TCPHO_MAX		0x7ffU
hayeswang's avatar
hayeswang committed
534
535
};

536
537
struct r8152;

hayeswang's avatar
hayeswang committed
538
539
540
struct rx_agg {
	struct list_head list;
	struct urb *urb;
541
	struct r8152 *context;
hayeswang's avatar
hayeswang committed
542
543
544
545
546
547
548
	void *buffer;
	void *head;
};

struct tx_agg {
	struct list_head list;
	struct urb *urb;
549
	struct r8152 *context;
hayeswang's avatar
hayeswang committed
550
551
552
553
554
555
	void *buffer;
	void *head;
	u32 skb_num;
	u32 skb_len;
};

hayeswang's avatar
hayeswang committed
556
557
558
559
struct r8152 {
	unsigned long flags;
	struct usb_device *udev;
	struct tasklet_struct tl;
560
	struct usb_interface *intf;
hayeswang's avatar
hayeswang committed
561
	struct net_device *netdev;
562
	struct urb *intr_urb;
hayeswang's avatar
hayeswang committed
563
564
565
566
567
	struct tx_agg tx_info[RTL8152_MAX_TX];
	struct rx_agg rx_info[RTL8152_MAX_RX];
	struct list_head rx_done, tx_free;
	struct sk_buff_head tx_queue;
	spinlock_t rx_lock, tx_lock;
hayeswang's avatar
hayeswang committed
568
569
	struct delayed_work schedule;
	struct mii_if_info mii;
hayeswang's avatar
hayeswang committed
570
571
572
573
574

	struct rtl_ops {
		void (*init)(struct r8152 *);
		int (*enable)(struct r8152 *);
		void (*disable)(struct r8152 *);
575
		void (*up)(struct r8152 *);
hayeswang's avatar
hayeswang committed
576
577
578
579
		void (*down)(struct r8152 *);
		void (*unload)(struct r8152 *);
	} rtl_ops;

580
	int intr_interval;
hayeswang's avatar
hayeswang committed
581
	u32 saved_wolopts;
hayeswang's avatar
hayeswang committed
582
	u32 msg_enable;
583
	u32 tx_qlen;
hayeswang's avatar
hayeswang committed
584
	u16 ocp_base;
585
	u8 *intr_buff;
hayeswang's avatar
hayeswang committed
586
587
588
589
590
591
592
	u8 version;
	u8 speed;
};

enum rtl_version {
	RTL_VER_UNKNOWN = 0,
	RTL_VER_01,
hayeswang's avatar
hayeswang committed
593
594
595
596
597
	RTL_VER_02,
	RTL_VER_03,
	RTL_VER_04,
	RTL_VER_05,
	RTL_VER_MAX
hayeswang's avatar
hayeswang committed
598
599
};

hayeswang's avatar
hayeswang committed
600
601
602
603
604
605
enum tx_csum_stat {
	TX_CSUM_SUCCESS = 0,
	TX_CSUM_TSO,
	TX_CSUM_NONE
};

hayeswang's avatar
hayeswang committed
606
607
608
609
/* 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
610
static unsigned int rx_buf_sz = 16384;
hayeswang's avatar
hayeswang committed
611

hayeswang's avatar
hayeswang committed
612
613
614
#define RTL_LIMITED_TSO_SIZE	(rx_buf_sz - sizeof(struct tx_desc) - \
				 VLAN_ETH_HLEN - VLAN_HLEN)

hayeswang's avatar
hayeswang committed
615
616
617
static
int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
{
618
619
620
621
622
623
624
625
	int ret;
	void *tmp;

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

	ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0),
hayeswang's avatar
hayeswang committed
626
			       RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
627
628
629
630
631
632
			       value, index, tmp, size, 500);

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

	return ret;
hayeswang's avatar
hayeswang committed
633
634
635
636
637
}

static
int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
{
638
639
640
	int ret;
	void *tmp;

641
	tmp = kmemdup(data, size, GFP_KERNEL);
642
643
644
645
	if (!tmp)
		return -ENOMEM;

	ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0),
hayeswang's avatar
hayeswang committed
646
			       RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
647
648
649
			       value, index, tmp, size, 500);

	kfree(tmp);
650

651
	return ret;
hayeswang's avatar
hayeswang committed
652
653
654
655
656
}

static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
				void *data, u16 type)
{
657
658
	u16 limit = 64;
	int ret = 0;
hayeswang's avatar
hayeswang committed
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696

	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;
		}
	}

	return ret;
}

static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
				u16 size, void *data, u16 type)
{
697
698
699
	int ret;
	u16 byteen_start, byteen_end, byen;
	u16 limit = 512;
hayeswang's avatar
hayeswang committed
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
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
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786

	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,
					type | BYTE_EN_DWORD,
					limit, data);
				if (ret < 0)
					goto error1;

				index += limit;
				data += limit;
				size -= limit;
			} else {
				ret = set_registers(tp, index,
					type | BYTE_EN_DWORD,
					size, data);
				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:
	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)
{
787
	__le32 data;
hayeswang's avatar
hayeswang committed
788

789
	generic_ocp_read(tp, index, sizeof(data), &data, type);
hayeswang's avatar
hayeswang committed
790
791
792
793
794
795

	return __le32_to_cpu(data);
}

static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
{
796
797
798
	__le32 tmp = __cpu_to_le32(data);

	generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
hayeswang's avatar
hayeswang committed
799
800
801
802
803
}

static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
{
	u32 data;
804
	__le32 tmp;
hayeswang's avatar
hayeswang committed
805
806
807
808
	u8 shift = index & 2;

	index &= ~3;

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

811
	data = __le32_to_cpu(tmp);
hayeswang's avatar
hayeswang committed
812
813
814
815
816
817
818
819
	data >>= (shift * 8);
	data &= 0xffff;

	return (u16)data;
}

static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
{
820
821
	u32 mask = 0xffff;
	__le32 tmp;
hayeswang's avatar
hayeswang committed
822
823
824
825
826
827
828
829
830
831
832
833
	u16 byen = BYTE_EN_WORD;
	u8 shift = index & 2;

	data &= mask;

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

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

836
837
	data |= __le32_to_cpu(tmp) & ~mask;
	tmp = __cpu_to_le32(data);
hayeswang's avatar
hayeswang committed
838

839
	generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswang's avatar
hayeswang committed
840
841
842
843
844
}

static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
{
	u32 data;
845
	__le32 tmp;
hayeswang's avatar
hayeswang committed
846
847
848
849
	u8 shift = index & 3;

	index &= ~3;

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

852
	data = __le32_to_cpu(tmp);
hayeswang's avatar
hayeswang committed
853
854
855
856
857
858
859
860
	data >>= (shift * 8);
	data &= 0xff;

	return (u8)data;
}

static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
{
861
862
	u32 mask = 0xff;
	__le32 tmp;
hayeswang's avatar
hayeswang committed
863
864
865
866
867
868
869
870
871
872
873
874
	u16 byen = BYTE_EN_BYTE;
	u8 shift = index & 3;

	data &= mask;

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

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

877
878
	data |= __le32_to_cpu(tmp) & ~mask;
	tmp = __cpu_to_le32(data);
hayeswang's avatar
hayeswang committed
879

880
	generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswang's avatar
hayeswang committed
881
882
}

883
static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
884
885
886
887
888
889
890
891
892
893
{
	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;
894
	return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
895
896
}

897
static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
hayeswang's avatar
hayeswang committed
898
{
899
	u16 ocp_base, ocp_index;
hayeswang's avatar
hayeswang committed
900

901
902
903
904
	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
905
	}
906
907
908

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

911
static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
hayeswang's avatar
hayeswang committed
912
{
913
914
	ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
}
hayeswang's avatar
hayeswang committed
915

916
917
918
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
919
920
}

hayeswang's avatar
hayeswang committed
921
922
923
924
925
926
927
928
929
930
931
932
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);
}

static u16 sram_read(struct r8152 *tp, u16 addr)
{
	ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
	return ocp_reg_read(tp, OCP_SRAM_DATA);
}

hayeswang's avatar
hayeswang committed
933
934
935
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
936
	int ret;
hayeswang's avatar
hayeswang committed
937

hayeswang's avatar
hayeswang committed
938
939
940
	if (test_bit(RTL8152_UNPLUG, &tp->flags))
		return -ENODEV;

hayeswang's avatar
hayeswang committed
941
942
943
	if (phy_id != R8152_PHY_ID)
		return -EINVAL;

hayeswang's avatar
hayeswang committed
944
945
946
947
948
949
950
951
952
953
	ret = usb_autopm_get_interface(tp->intf);
	if (ret < 0)
		goto out;

	ret = r8152_mdio_read(tp, reg);

	usb_autopm_put_interface(tp->intf);

out:
	return ret;
hayeswang's avatar
hayeswang committed
954
955
956
957
958
959
960
}

static
void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
{
	struct r8152 *tp = netdev_priv(netdev);

hayeswang's avatar
hayeswang committed
961
962
963
	if (test_bit(RTL8152_UNPLUG, &tp->flags))
		return;

hayeswang's avatar
hayeswang committed
964
965
966
	if (phy_id != R8152_PHY_ID)
		return;

hayeswang's avatar
hayeswang committed
967
968
969
	if (usb_autopm_get_interface(tp->intf) < 0)
		return;

hayeswang's avatar
hayeswang committed
970
	r8152_mdio_write(tp, reg, val);
hayeswang's avatar
hayeswang committed
971
972

	usb_autopm_put_interface(tp->intf);
hayeswang's avatar
hayeswang committed
973
974
}

hayeswang's avatar
hayeswang committed
975
976
977
static
int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);

hayeswang's avatar
hayeswang committed
978
979
980
static inline void set_ethernet_addr(struct r8152 *tp)
{
	struct net_device *dev = tp->netdev;
981
	int ret;
982
	u8 node_id[8] = {0};
hayeswang's avatar
hayeswang committed
983

984
985
986
987
988
989
	if (tp->version == RTL_VER_01)
		ret = pla_ocp_read(tp, PLA_IDR, sizeof(node_id), node_id);
	else
		ret = pla_ocp_read(tp, PLA_BACKUP, sizeof(node_id), node_id);

	if (ret < 0) {
hayeswang's avatar
hayeswang committed
990
		netif_notice(tp, probe, dev, "inet addr fail\n");
991
992
993
994
995
996
997
998
999
1000
	} else {
		if (tp->version != RTL_VER_01) {
			ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR,
				       CRWECR_CONFIG);
			pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES,
				      sizeof(node_id), node_id);
			ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR,
				       CRWECR_NORAML);
		}

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