r8152.c 72.7 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
25

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

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

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

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

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

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

/* PLA_FMC */
#define FMC_FCR_MCU_EN		0x0001

/* PLA_EEEP_CR */
#define EEEP_CR_EEEP_TX		0x0002

hayeswang's avatar
hayeswang committed
172
173
174
/* PLA_WDT6_CTRL */
#define WDT6_SET_MODE		0x0010

hayeswang's avatar
hayeswang committed
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* PLA_TCR0 */
#define TCR0_TX_EMPTY		0x0800
#define TCR0_AUTO_FIFO		0x0080

/* PLA_TCR1 */
#define VERSION_MASK		0x7cf0

/* 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
212
213
214
215
216
217
/* 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
218
219
220
/* PAL_BDC_CR */
#define ALDPS_PROXY_MODE	0x0001

hayeswang's avatar
hayeswang committed
221
222
223
224
/* PLA_CONFIG34 */
#define LINK_ON_WAKE_EN		0x0010
#define LINK_OFF_WAKE_EN	0x0008

hayeswang's avatar
hayeswang committed
225
/* PLA_CONFIG5 */
hayeswang's avatar
hayeswang committed
226
227
228
#define BWF_EN			0x0040
#define MWF_EN			0x0020
#define UWF_EN			0x0010
hayeswang's avatar
hayeswang committed
229
230
231
232
233
234
235
236
237
238
239
240
241
#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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#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
261
262
263
264
265
266
267
268
269
270
271
272
273
274

/* 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
275
276
277
/* PLA_BOOT_CTRL */
#define AUTOLOAD_DONE		0x0002

hayeswang's avatar
hayeswang committed
278
279
280
281
282
283
284
285
286
/* USB_DEV_STAT */
#define STAT_SPEED_MASK		0x0006
#define STAT_SPEED_HIGH		0x0000
#define STAT_SPEED_FULL		0x0001

/* USB_TX_AGG */
#define TX_AGG_MAX_THRESHOLD	0x03

/* USB_RX_BUF_TH */
hayeswang's avatar
hayeswang committed
287
#define RX_THR_SUPPER		0x0c350180
hayeswang's avatar
hayeswang committed
288
#define RX_THR_HIGH		0x7a120180
hayeswang's avatar
hayeswang committed
289
#define RX_THR_SLOW		0xffff0180
hayeswang's avatar
hayeswang committed
290
291
292
293
294
295
296
297
298

/* 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
299
#define RESUME_INDICATE		0x0001
hayeswang's avatar
hayeswang committed
300
301
302
303

/* USB_USB_CTRL */
#define RX_AGG_DISABLE		0x0010

hayeswang's avatar
hayeswang committed
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/* 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
332
333
334
335
336
337
/* OCP_ALDPS_CONFIG */
#define ENPWRSAVE		0x8000
#define ENPDNPS			0x0200
#define LINKENA			0x0100
#define DIS_SDSAVE		0x0010

hayeswang's avatar
hayeswang committed
338
339
340
341
342
343
344
345
346
347
/* 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
348
349
350
351
352
353
354
355
356
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
/* 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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/* 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
410
enum rtl_register_content {
hayeswang's avatar
hayeswang committed
411
	_1000bps	= 0x10,
hayeswang's avatar
hayeswang committed
412
413
414
415
416
417
	_100bps		= 0x08,
	_10bps		= 0x04,
	LINK_STATUS	= 0x02,
	FULL_DUP	= 0x01,
};

hayeswang's avatar
hayeswang committed
418
419
#define RTL8152_MAX_TX		10
#define RTL8152_MAX_RX		10
420
#define INTBUFSIZE		2
hayeswang's avatar
hayeswang committed
421
422
423
#define CRC_SIZE		4
#define TX_ALIGN		4
#define RX_ALIGN		8
424
425

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

hayeswang's avatar
hayeswang committed
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#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

#define RTL8152_RMS		(VLAN_ETH_FRAME_LEN + VLAN_HLEN)
#define RTL8152_TX_TIMEOUT	(HZ)

/* rtl8152 flags */
enum rtl8152_flags {
	RTL8152_UNPLUG = 0,
	RTL8152_SET_RX_MODE,
446
447
	WORK_ENABLE,
	RTL8152_LINK_CHG,
hayeswang's avatar
hayeswang committed
448
	SELECTIVE_SUSPEND,
449
	PHY_RESET,
hayeswang's avatar
hayeswang committed
450
451
452
453
454
};

/* Define these values to match your device */
#define VENDOR_ID_REALTEK		0x0bda
#define PRODUCT_ID_RTL8152		0x8152
hayeswang's avatar
hayeswang committed
455
456
457
458
#define PRODUCT_ID_RTL8153		0x8153

#define VENDOR_ID_SAMSUNG		0x04e8
#define PRODUCT_ID_SAMSUNG		0xa101
hayeswang's avatar
hayeswang committed
459
460
461
462

#define MCU_TYPE_PLA			0x0100
#define MCU_TYPE_USB			0x0000

463
464
465
#define REALTEK_USB_DEVICE(vend, prod)	\
	USB_DEVICE_INTERFACE_CLASS(vend, prod, USB_CLASS_VENDOR_SPEC)

hayeswang's avatar
hayeswang committed
466
struct rx_desc {
467
	__le32 opts1;
hayeswang's avatar
hayeswang committed
468
#define RX_LEN_MASK			0x7fff
469
470
471
472
473
	__le32 opts2;
	__le32 opts3;
	__le32 opts4;
	__le32 opts5;
	__le32 opts6;
hayeswang's avatar
hayeswang committed
474
475
476
};

struct tx_desc {
477
	__le32 opts1;
hayeswang's avatar
hayeswang committed
478
479
#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
480
481
#define TX_LEN_MASK		0x3ffff

482
	__le32 opts2;
hayeswang's avatar
hayeswang committed
483
484
485
486
#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
487
488
};

489
490
struct r8152;

hayeswang's avatar
hayeswang committed
491
492
493
struct rx_agg {
	struct list_head list;
	struct urb *urb;
494
	struct r8152 *context;
hayeswang's avatar
hayeswang committed
495
496
497
498
499
500
501
	void *buffer;
	void *head;
};

struct tx_agg {
	struct list_head list;
	struct urb *urb;
502
	struct r8152 *context;
hayeswang's avatar
hayeswang committed
503
504
505
506
507
508
	void *buffer;
	void *head;
	u32 skb_num;
	u32 skb_len;
};

hayeswang's avatar
hayeswang committed
509
510
511
512
struct r8152 {
	unsigned long flags;
	struct usb_device *udev;
	struct tasklet_struct tl;
513
	struct usb_interface *intf;
hayeswang's avatar
hayeswang committed
514
	struct net_device *netdev;
515
	struct urb *intr_urb;
hayeswang's avatar
hayeswang committed
516
517
518
519
520
	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
521
522
	struct delayed_work schedule;
	struct mii_if_info mii;
hayeswang's avatar
hayeswang committed
523
524
525
526
527

	struct rtl_ops {
		void (*init)(struct r8152 *);
		int (*enable)(struct r8152 *);
		void (*disable)(struct r8152 *);
528
		void (*up)(struct r8152 *);
hayeswang's avatar
hayeswang committed
529
530
531
532
		void (*down)(struct r8152 *);
		void (*unload)(struct r8152 *);
	} rtl_ops;

533
	int intr_interval;
hayeswang's avatar
hayeswang committed
534
	u32 saved_wolopts;
hayeswang's avatar
hayeswang committed
535
	u32 msg_enable;
536
	u32 tx_qlen;
hayeswang's avatar
hayeswang committed
537
	u16 ocp_base;
538
	u8 *intr_buff;
hayeswang's avatar
hayeswang committed
539
540
541
542
543
544
545
	u8 version;
	u8 speed;
};

enum rtl_version {
	RTL_VER_UNKNOWN = 0,
	RTL_VER_01,
hayeswang's avatar
hayeswang committed
546
547
548
549
550
	RTL_VER_02,
	RTL_VER_03,
	RTL_VER_04,
	RTL_VER_05,
	RTL_VER_MAX
hayeswang's avatar
hayeswang committed
551
552
553
554
555
556
};

/* 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
557
static unsigned int rx_buf_sz = 16384;
hayeswang's avatar
hayeswang committed
558
559
560
561

static
int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
{
562
563
564
565
566
567
568
569
	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
570
			       RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
571
572
573
574
575
576
			       value, index, tmp, size, 500);

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

	return ret;
hayeswang's avatar
hayeswang committed
577
578
579
580
581
}

static
int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
{
582
583
584
585
586
587
588
589
590
591
	int ret;
	void *tmp;

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

	memcpy(tmp, data, size);

	ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0),
hayeswang's avatar
hayeswang committed
592
			       RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
593
594
595
			       value, index, tmp, size, 500);

	kfree(tmp);
596

597
	return ret;
hayeswang's avatar
hayeswang committed
598
599
600
601
602
}

static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
				void *data, u16 type)
{
603
604
	u16 limit = 64;
	int ret = 0;
hayeswang's avatar
hayeswang committed
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642

	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)
{
643
644
645
	int ret;
	u16 byteen_start, byteen_end, byen;
	u16 limit = 512;
hayeswang's avatar
hayeswang committed
646
647
648
649
650
651
652
653
654
655
656
657
658
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
697
698
699
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

	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)
{
733
	__le32 data;
hayeswang's avatar
hayeswang committed
734

735
	generic_ocp_read(tp, index, sizeof(data), &data, type);
hayeswang's avatar
hayeswang committed
736
737
738
739
740
741

	return __le32_to_cpu(data);
}

static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
{
742
743
744
	__le32 tmp = __cpu_to_le32(data);

	generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
hayeswang's avatar
hayeswang committed
745
746
747
748
749
}

static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
{
	u32 data;
750
	__le32 tmp;
hayeswang's avatar
hayeswang committed
751
752
753
754
	u8 shift = index & 2;

	index &= ~3;

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

757
	data = __le32_to_cpu(tmp);
hayeswang's avatar
hayeswang committed
758
759
760
761
762
763
764
765
	data >>= (shift * 8);
	data &= 0xffff;

	return (u16)data;
}

static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
{
766
767
	u32 mask = 0xffff;
	__le32 tmp;
hayeswang's avatar
hayeswang committed
768
769
770
771
772
773
774
775
776
777
778
779
	u16 byen = BYTE_EN_WORD;
	u8 shift = index & 2;

	data &= mask;

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

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

782
783
	data |= __le32_to_cpu(tmp) & ~mask;
	tmp = __cpu_to_le32(data);
hayeswang's avatar
hayeswang committed
784

785
	generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswang's avatar
hayeswang committed
786
787
788
789
790
}

static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
{
	u32 data;
791
	__le32 tmp;
hayeswang's avatar
hayeswang committed
792
793
794
795
	u8 shift = index & 3;

	index &= ~3;

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

798
	data = __le32_to_cpu(tmp);
hayeswang's avatar
hayeswang committed
799
800
801
802
803
804
805
806
	data >>= (shift * 8);
	data &= 0xff;

	return (u8)data;
}

static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
{
807
808
	u32 mask = 0xff;
	__le32 tmp;
hayeswang's avatar
hayeswang committed
809
810
811
812
813
814
815
816
817
818
819
820
	u16 byen = BYTE_EN_BYTE;
	u8 shift = index & 3;

	data &= mask;

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

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

823
824
	data |= __le32_to_cpu(tmp) & ~mask;
	tmp = __cpu_to_le32(data);
hayeswang's avatar
hayeswang committed
825

826
	generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswang's avatar
hayeswang committed
827
828
}

829
static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
830
831
832
833
834
835
836
837
838
839
{
	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;
840
	return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
841
842
}

843
static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
hayeswang's avatar
hayeswang committed
844
{
845
	u16 ocp_base, ocp_index;
hayeswang's avatar
hayeswang committed
846

847
848
849
850
	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
851
	}
852
853
854

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

857
static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
hayeswang's avatar
hayeswang committed
858
{
859
860
	ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
}
hayeswang's avatar
hayeswang committed
861

862
863
864
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
865
866
}

hayeswang's avatar
hayeswang committed
867
868
869
870
871
872
873
874
875
876
877
878
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
879
880
881
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
882
	int ret;
hayeswang's avatar
hayeswang committed
883
884
885
886

	if (phy_id != R8152_PHY_ID)
		return -EINVAL;

hayeswang's avatar
hayeswang committed
887
888
889
890
891
892
893
894
895
896
	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
897
898
899
900
901
902
903
904
905
906
}

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

	if (phy_id != R8152_PHY_ID)
		return;

hayeswang's avatar
hayeswang committed
907
908
909
	if (usb_autopm_get_interface(tp->intf) < 0)
		return;

hayeswang's avatar
hayeswang committed
910
	r8152_mdio_write(tp, reg, val);
hayeswang's avatar
hayeswang committed
911
912

	usb_autopm_put_interface(tp->intf);
hayeswang's avatar
hayeswang committed
913
914
}

hayeswang's avatar
hayeswang committed
915
916
917
static
int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);

hayeswang's avatar
hayeswang committed
918
919
920
static inline void set_ethernet_addr(struct r8152 *tp)
{
	struct net_device *dev = tp->netdev;
921
	int ret;
922
	u8 node_id[8] = {0};
hayeswang's avatar
hayeswang committed
923

924
925
926
927
928
929
	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
930
		netif_notice(tp, probe, dev, "inet addr fail\n");
931
932
933
934
935
936
937
938
939
940
	} 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);
		}

hayeswang's avatar
hayeswang committed
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
		memcpy(dev->dev_addr, node_id, dev->addr_len);
		memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
	}
}

static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
{
	struct r8152 *tp = netdev_priv(netdev);
	struct sockaddr *addr = p;

	if (!is_valid_ether_addr(addr->sa_data))
		return -EADDRNOTAVAIL;

	memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);

	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
	pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);

	return 0;
}

static void read_bulk_callback(struct urb *urb)
{
	struct net_device *netdev;
	int status = urb->status;
hayeswang's avatar
hayeswang committed
967
968
	struct rx_agg *agg;
	struct r8152 *tp;
hayeswang's avatar
hayeswang committed
969
970
	int result;

hayeswang's avatar
hayeswang committed
971
972
973
974
975
	agg = urb->context;
	if (!agg)
		return;

	tp = agg->context;
hayeswang's avatar
hayeswang committed
976
977
	if (!tp)
		return;
hayeswang's avatar
hayeswang committed
978

hayeswang's avatar
hayeswang committed
979
980
	if (test_bit(RTL8152_UNPLUG, &tp->flags))
		return;
hayeswang's avatar
hayeswang committed
981
982
983
984

	if (!test_bit(WORK_ENABLE, &tp->flags))
		return;

hayeswang's avatar
hayeswang committed
985
	netdev = tp->netdev;
hayeswang's avatar
hayeswang committed
986
987
988

	/* When link down, the driver would cancel all bulks. */
	/* This avoid the re-submitting bulk */
hayeswang's avatar
hayeswang committed
989
	if (!netif_carrier_ok(netdev))
hayeswang's avatar
hayeswang committed
990
991
		return;

hayeswang's avatar
hayeswang committed
992
993
	usb_mark_last_busy(tp->udev);

hayeswang's avatar
hayeswang committed
994
995
	switch (status) {
	case 0:
hayeswang's avatar
hayeswang committed
996
997
998
		if (urb->actual_length < ETH_ZLEN)
			break;

999
		spin_lock(&tp->rx_lock);
hayeswang's avatar
hayeswang committed
1000
		list_add_tail(&agg->list, &tp->rx_done);