xref: /openbmc/linux/drivers/net/usb/r8152.c (revision 5f2fb52fac15a8a8e10ce020dd532504a8abfc4e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  Copyright (c) 2014 Realtek Semiconductor Corp. All rights reserved.
4  */
5 
6 #include <linux/signal.h>
7 #include <linux/slab.h>
8 #include <linux/module.h>
9 #include <linux/netdevice.h>
10 #include <linux/etherdevice.h>
11 #include <linux/mii.h>
12 #include <linux/ethtool.h>
13 #include <linux/usb.h>
14 #include <linux/crc32.h>
15 #include <linux/if_vlan.h>
16 #include <linux/uaccess.h>
17 #include <linux/list.h>
18 #include <linux/ip.h>
19 #include <linux/ipv6.h>
20 #include <net/ip6_checksum.h>
21 #include <uapi/linux/mdio.h>
22 #include <linux/mdio.h>
23 #include <linux/usb/cdc.h>
24 #include <linux/suspend.h>
25 #include <linux/atomic.h>
26 #include <linux/acpi.h>
27 #include <linux/firmware.h>
28 #include <crypto/hash.h>
29 
30 /* Information for net-next */
31 #define NETNEXT_VERSION		"11"
32 
33 /* Information for net */
34 #define NET_VERSION		"11"
35 
36 #define DRIVER_VERSION		"v1." NETNEXT_VERSION "." NET_VERSION
37 #define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>"
38 #define DRIVER_DESC "Realtek RTL8152/RTL8153 Based USB Ethernet Adapters"
39 #define MODULENAME "r8152"
40 
41 #define R8152_PHY_ID		32
42 
43 #define PLA_IDR			0xc000
44 #define PLA_RCR			0xc010
45 #define PLA_RMS			0xc016
46 #define PLA_RXFIFO_CTRL0	0xc0a0
47 #define PLA_RXFIFO_CTRL1	0xc0a4
48 #define PLA_RXFIFO_CTRL2	0xc0a8
49 #define PLA_DMY_REG0		0xc0b0
50 #define PLA_FMC			0xc0b4
51 #define PLA_CFG_WOL		0xc0b6
52 #define PLA_TEREDO_CFG		0xc0bc
53 #define PLA_TEREDO_WAKE_BASE	0xc0c4
54 #define PLA_MAR			0xcd00
55 #define PLA_BACKUP		0xd000
56 #define PLA_BDC_CR		0xd1a0
57 #define PLA_TEREDO_TIMER	0xd2cc
58 #define PLA_REALWOW_TIMER	0xd2e8
59 #define PLA_UPHY_TIMER		0xd388
60 #define PLA_SUSPEND_FLAG	0xd38a
61 #define PLA_INDICATE_FALG	0xd38c
62 #define PLA_MACDBG_PRE		0xd38c	/* RTL_VER_04 only */
63 #define PLA_MACDBG_POST		0xd38e	/* RTL_VER_04 only */
64 #define PLA_EXTRA_STATUS	0xd398
65 #define PLA_EFUSE_DATA		0xdd00
66 #define PLA_EFUSE_CMD		0xdd02
67 #define PLA_LEDSEL		0xdd90
68 #define PLA_LED_FEATURE		0xdd92
69 #define PLA_PHYAR		0xde00
70 #define PLA_BOOT_CTRL		0xe004
71 #define PLA_LWAKE_CTRL_REG	0xe007
72 #define PLA_GPHY_INTR_IMR	0xe022
73 #define PLA_EEE_CR		0xe040
74 #define PLA_EEEP_CR		0xe080
75 #define PLA_MAC_PWR_CTRL	0xe0c0
76 #define PLA_MAC_PWR_CTRL2	0xe0ca
77 #define PLA_MAC_PWR_CTRL3	0xe0cc
78 #define PLA_MAC_PWR_CTRL4	0xe0ce
79 #define PLA_WDT6_CTRL		0xe428
80 #define PLA_TCR0		0xe610
81 #define PLA_TCR1		0xe612
82 #define PLA_MTPS		0xe615
83 #define PLA_TXFIFO_CTRL		0xe618
84 #define PLA_RSTTALLY		0xe800
85 #define PLA_CR			0xe813
86 #define PLA_CRWECR		0xe81c
87 #define PLA_CONFIG12		0xe81e	/* CONFIG1, CONFIG2 */
88 #define PLA_CONFIG34		0xe820	/* CONFIG3, CONFIG4 */
89 #define PLA_CONFIG5		0xe822
90 #define PLA_PHY_PWR		0xe84c
91 #define PLA_OOB_CTRL		0xe84f
92 #define PLA_CPCR		0xe854
93 #define PLA_MISC_0		0xe858
94 #define PLA_MISC_1		0xe85a
95 #define PLA_OCP_GPHY_BASE	0xe86c
96 #define PLA_TALLYCNT		0xe890
97 #define PLA_SFF_STS_7		0xe8de
98 #define PLA_PHYSTATUS		0xe908
99 #define PLA_CONFIG6		0xe90a /* CONFIG6 */
100 #define PLA_BP_BA		0xfc26
101 #define PLA_BP_0		0xfc28
102 #define PLA_BP_1		0xfc2a
103 #define PLA_BP_2		0xfc2c
104 #define PLA_BP_3		0xfc2e
105 #define PLA_BP_4		0xfc30
106 #define PLA_BP_5		0xfc32
107 #define PLA_BP_6		0xfc34
108 #define PLA_BP_7		0xfc36
109 #define PLA_BP_EN		0xfc38
110 
111 #define USB_USB2PHY		0xb41e
112 #define USB_SSPHYLINK1		0xb426
113 #define USB_SSPHYLINK2		0xb428
114 #define USB_U2P3_CTRL		0xb460
115 #define USB_CSR_DUMMY1		0xb464
116 #define USB_CSR_DUMMY2		0xb466
117 #define USB_DEV_STAT		0xb808
118 #define USB_CONNECT_TIMER	0xcbf8
119 #define USB_MSC_TIMER		0xcbfc
120 #define USB_BURST_SIZE		0xcfc0
121 #define USB_FW_FIX_EN0		0xcfca
122 #define USB_FW_FIX_EN1		0xcfcc
123 #define USB_LPM_CONFIG		0xcfd8
124 #define USB_CSTMR		0xcfef	/* RTL8153A */
125 #define USB_FW_CTRL		0xd334	/* RTL8153B */
126 #define USB_FC_TIMER		0xd340
127 #define USB_USB_CTRL		0xd406
128 #define USB_PHY_CTRL		0xd408
129 #define USB_TX_AGG		0xd40a
130 #define USB_RX_BUF_TH		0xd40c
131 #define USB_USB_TIMER		0xd428
132 #define USB_RX_EARLY_TIMEOUT	0xd42c
133 #define USB_RX_EARLY_SIZE	0xd42e
134 #define USB_PM_CTRL_STATUS	0xd432	/* RTL8153A */
135 #define USB_RX_EXTRA_AGGR_TMR	0xd432	/* RTL8153B */
136 #define USB_TX_DMA		0xd434
137 #define USB_UPT_RXDMA_OWN	0xd437
138 #define USB_TOLERANCE		0xd490
139 #define USB_LPM_CTRL		0xd41a
140 #define USB_BMU_RESET		0xd4b0
141 #define USB_U1U2_TIMER		0xd4da
142 #define USB_FW_TASK		0xd4e8	/* RTL8153B */
143 #define USB_UPS_CTRL		0xd800
144 #define USB_POWER_CUT		0xd80a
145 #define USB_MISC_0		0xd81a
146 #define USB_MISC_1		0xd81f
147 #define USB_AFE_CTRL2		0xd824
148 #define USB_UPS_CFG		0xd842
149 #define USB_UPS_FLAGS		0xd848
150 #define USB_WDT1_CTRL		0xe404
151 #define USB_WDT11_CTRL		0xe43c
152 #define USB_BP_BA		PLA_BP_BA
153 #define USB_BP_0		PLA_BP_0
154 #define USB_BP_1		PLA_BP_1
155 #define USB_BP_2		PLA_BP_2
156 #define USB_BP_3		PLA_BP_3
157 #define USB_BP_4		PLA_BP_4
158 #define USB_BP_5		PLA_BP_5
159 #define USB_BP_6		PLA_BP_6
160 #define USB_BP_7		PLA_BP_7
161 #define USB_BP_EN		PLA_BP_EN	/* RTL8153A */
162 #define USB_BP_8		0xfc38		/* RTL8153B */
163 #define USB_BP_9		0xfc3a
164 #define USB_BP_10		0xfc3c
165 #define USB_BP_11		0xfc3e
166 #define USB_BP_12		0xfc40
167 #define USB_BP_13		0xfc42
168 #define USB_BP_14		0xfc44
169 #define USB_BP_15		0xfc46
170 #define USB_BP2_EN		0xfc48
171 
172 /* OCP Registers */
173 #define OCP_ALDPS_CONFIG	0x2010
174 #define OCP_EEE_CONFIG1		0x2080
175 #define OCP_EEE_CONFIG2		0x2092
176 #define OCP_EEE_CONFIG3		0x2094
177 #define OCP_BASE_MII		0xa400
178 #define OCP_EEE_AR		0xa41a
179 #define OCP_EEE_DATA		0xa41c
180 #define OCP_PHY_STATUS		0xa420
181 #define OCP_NCTL_CFG		0xa42c
182 #define OCP_POWER_CFG		0xa430
183 #define OCP_EEE_CFG		0xa432
184 #define OCP_SRAM_ADDR		0xa436
185 #define OCP_SRAM_DATA		0xa438
186 #define OCP_DOWN_SPEED		0xa442
187 #define OCP_EEE_ABLE		0xa5c4
188 #define OCP_EEE_ADV		0xa5d0
189 #define OCP_EEE_LPABLE		0xa5d2
190 #define OCP_PHY_STATE		0xa708		/* nway state for 8153 */
191 #define OCP_PHY_PATCH_STAT	0xb800
192 #define OCP_PHY_PATCH_CMD	0xb820
193 #define OCP_PHY_LOCK		0xb82e
194 #define OCP_ADC_IOFFSET		0xbcfc
195 #define OCP_ADC_CFG		0xbc06
196 #define OCP_SYSCLK_CFG		0xc416
197 
198 /* SRAM Register */
199 #define SRAM_GREEN_CFG		0x8011
200 #define SRAM_LPF_CFG		0x8012
201 #define SRAM_10M_AMP1		0x8080
202 #define SRAM_10M_AMP2		0x8082
203 #define SRAM_IMPEDANCE		0x8084
204 #define SRAM_PHY_LOCK		0xb82e
205 
206 /* PLA_RCR */
207 #define RCR_AAP			0x00000001
208 #define RCR_APM			0x00000002
209 #define RCR_AM			0x00000004
210 #define RCR_AB			0x00000008
211 #define RCR_ACPT_ALL		(RCR_AAP | RCR_APM | RCR_AM | RCR_AB)
212 
213 /* PLA_RXFIFO_CTRL0 */
214 #define RXFIFO_THR1_NORMAL	0x00080002
215 #define RXFIFO_THR1_OOB		0x01800003
216 
217 /* PLA_RXFIFO_CTRL1 */
218 #define RXFIFO_THR2_FULL	0x00000060
219 #define RXFIFO_THR2_HIGH	0x00000038
220 #define RXFIFO_THR2_OOB		0x0000004a
221 #define RXFIFO_THR2_NORMAL	0x00a0
222 
223 /* PLA_RXFIFO_CTRL2 */
224 #define RXFIFO_THR3_FULL	0x00000078
225 #define RXFIFO_THR3_HIGH	0x00000048
226 #define RXFIFO_THR3_OOB		0x0000005a
227 #define RXFIFO_THR3_NORMAL	0x0110
228 
229 /* PLA_TXFIFO_CTRL */
230 #define TXFIFO_THR_NORMAL	0x00400008
231 #define TXFIFO_THR_NORMAL2	0x01000008
232 
233 /* PLA_DMY_REG0 */
234 #define ECM_ALDPS		0x0002
235 
236 /* PLA_FMC */
237 #define FMC_FCR_MCU_EN		0x0001
238 
239 /* PLA_EEEP_CR */
240 #define EEEP_CR_EEEP_TX		0x0002
241 
242 /* PLA_WDT6_CTRL */
243 #define WDT6_SET_MODE		0x0010
244 
245 /* PLA_TCR0 */
246 #define TCR0_TX_EMPTY		0x0800
247 #define TCR0_AUTO_FIFO		0x0080
248 
249 /* PLA_TCR1 */
250 #define VERSION_MASK		0x7cf0
251 
252 /* PLA_MTPS */
253 #define MTPS_JUMBO		(12 * 1024 / 64)
254 #define MTPS_DEFAULT		(6 * 1024 / 64)
255 
256 /* PLA_RSTTALLY */
257 #define TALLY_RESET		0x0001
258 
259 /* PLA_CR */
260 #define CR_RST			0x10
261 #define CR_RE			0x08
262 #define CR_TE			0x04
263 
264 /* PLA_CRWECR */
265 #define CRWECR_NORAML		0x00
266 #define CRWECR_CONFIG		0xc0
267 
268 /* PLA_OOB_CTRL */
269 #define NOW_IS_OOB		0x80
270 #define TXFIFO_EMPTY		0x20
271 #define RXFIFO_EMPTY		0x10
272 #define LINK_LIST_READY		0x02
273 #define DIS_MCU_CLROOB		0x01
274 #define FIFO_EMPTY		(TXFIFO_EMPTY | RXFIFO_EMPTY)
275 
276 /* PLA_MISC_1 */
277 #define RXDY_GATED_EN		0x0008
278 
279 /* PLA_SFF_STS_7 */
280 #define RE_INIT_LL		0x8000
281 #define MCU_BORW_EN		0x4000
282 
283 /* PLA_CPCR */
284 #define CPCR_RX_VLAN		0x0040
285 
286 /* PLA_CFG_WOL */
287 #define MAGIC_EN		0x0001
288 
289 /* PLA_TEREDO_CFG */
290 #define TEREDO_SEL		0x8000
291 #define TEREDO_WAKE_MASK	0x7f00
292 #define TEREDO_RS_EVENT_MASK	0x00fe
293 #define OOB_TEREDO_EN		0x0001
294 
295 /* PLA_BDC_CR */
296 #define ALDPS_PROXY_MODE	0x0001
297 
298 /* PLA_EFUSE_CMD */
299 #define EFUSE_READ_CMD		BIT(15)
300 #define EFUSE_DATA_BIT16	BIT(7)
301 
302 /* PLA_CONFIG34 */
303 #define LINK_ON_WAKE_EN		0x0010
304 #define LINK_OFF_WAKE_EN	0x0008
305 
306 /* PLA_CONFIG6 */
307 #define LANWAKE_CLR_EN		BIT(0)
308 
309 /* PLA_CONFIG5 */
310 #define BWF_EN			0x0040
311 #define MWF_EN			0x0020
312 #define UWF_EN			0x0010
313 #define LAN_WAKE_EN		0x0002
314 
315 /* PLA_LED_FEATURE */
316 #define LED_MODE_MASK		0x0700
317 
318 /* PLA_PHY_PWR */
319 #define TX_10M_IDLE_EN		0x0080
320 #define PFM_PWM_SWITCH		0x0040
321 #define TEST_IO_OFF		BIT(4)
322 
323 /* PLA_MAC_PWR_CTRL */
324 #define D3_CLK_GATED_EN		0x00004000
325 #define MCU_CLK_RATIO		0x07010f07
326 #define MCU_CLK_RATIO_MASK	0x0f0f0f0f
327 #define ALDPS_SPDWN_RATIO	0x0f87
328 
329 /* PLA_MAC_PWR_CTRL2 */
330 #define EEE_SPDWN_RATIO		0x8007
331 #define MAC_CLK_SPDWN_EN	BIT(15)
332 
333 /* PLA_MAC_PWR_CTRL3 */
334 #define PLA_MCU_SPDWN_EN	BIT(14)
335 #define PKT_AVAIL_SPDWN_EN	0x0100
336 #define SUSPEND_SPDWN_EN	0x0004
337 #define U1U2_SPDWN_EN		0x0002
338 #define L1_SPDWN_EN		0x0001
339 
340 /* PLA_MAC_PWR_CTRL4 */
341 #define PWRSAVE_SPDWN_EN	0x1000
342 #define RXDV_SPDWN_EN		0x0800
343 #define TX10MIDLE_EN		0x0100
344 #define TP100_SPDWN_EN		0x0020
345 #define TP500_SPDWN_EN		0x0010
346 #define TP1000_SPDWN_EN		0x0008
347 #define EEE_SPDWN_EN		0x0001
348 
349 /* PLA_GPHY_INTR_IMR */
350 #define GPHY_STS_MSK		0x0001
351 #define SPEED_DOWN_MSK		0x0002
352 #define SPDWN_RXDV_MSK		0x0004
353 #define SPDWN_LINKCHG_MSK	0x0008
354 
355 /* PLA_PHYAR */
356 #define PHYAR_FLAG		0x80000000
357 
358 /* PLA_EEE_CR */
359 #define EEE_RX_EN		0x0001
360 #define EEE_TX_EN		0x0002
361 
362 /* PLA_BOOT_CTRL */
363 #define AUTOLOAD_DONE		0x0002
364 
365 /* PLA_LWAKE_CTRL_REG */
366 #define LANWAKE_PIN		BIT(7)
367 
368 /* PLA_SUSPEND_FLAG */
369 #define LINK_CHG_EVENT		BIT(0)
370 
371 /* PLA_INDICATE_FALG */
372 #define UPCOMING_RUNTIME_D3	BIT(0)
373 
374 /* PLA_MACDBG_PRE and PLA_MACDBG_POST */
375 #define DEBUG_OE		BIT(0)
376 #define DEBUG_LTSSM		0x0082
377 
378 /* PLA_EXTRA_STATUS */
379 #define CUR_LINK_OK		BIT(15)
380 #define U3P3_CHECK_EN		BIT(7)	/* RTL_VER_05 only */
381 #define LINK_CHANGE_FLAG	BIT(8)
382 #define POLL_LINK_CHG		BIT(0)
383 
384 /* USB_USB2PHY */
385 #define USB2PHY_SUSPEND		0x0001
386 #define USB2PHY_L1		0x0002
387 
388 /* USB_SSPHYLINK1 */
389 #define DELAY_PHY_PWR_CHG	BIT(1)
390 
391 /* USB_SSPHYLINK2 */
392 #define pwd_dn_scale_mask	0x3ffe
393 #define pwd_dn_scale(x)		((x) << 1)
394 
395 /* USB_CSR_DUMMY1 */
396 #define DYNAMIC_BURST		0x0001
397 
398 /* USB_CSR_DUMMY2 */
399 #define EP4_FULL_FC		0x0001
400 
401 /* USB_DEV_STAT */
402 #define STAT_SPEED_MASK		0x0006
403 #define STAT_SPEED_HIGH		0x0000
404 #define STAT_SPEED_FULL		0x0002
405 
406 /* USB_FW_FIX_EN0 */
407 #define FW_FIX_SUSPEND		BIT(14)
408 
409 /* USB_FW_FIX_EN1 */
410 #define FW_IP_RESET_EN		BIT(9)
411 
412 /* USB_LPM_CONFIG */
413 #define LPM_U1U2_EN		BIT(0)
414 
415 /* USB_TX_AGG */
416 #define TX_AGG_MAX_THRESHOLD	0x03
417 
418 /* USB_RX_BUF_TH */
419 #define RX_THR_SUPPER		0x0c350180
420 #define RX_THR_HIGH		0x7a120180
421 #define RX_THR_SLOW		0xffff0180
422 #define RX_THR_B		0x00010001
423 
424 /* USB_TX_DMA */
425 #define TEST_MODE_DISABLE	0x00000001
426 #define TX_SIZE_ADJUST1		0x00000100
427 
428 /* USB_BMU_RESET */
429 #define BMU_RESET_EP_IN		0x01
430 #define BMU_RESET_EP_OUT	0x02
431 
432 /* USB_UPT_RXDMA_OWN */
433 #define OWN_UPDATE		BIT(0)
434 #define OWN_CLEAR		BIT(1)
435 
436 /* USB_FW_TASK */
437 #define FC_PATCH_TASK		BIT(1)
438 
439 /* USB_UPS_CTRL */
440 #define POWER_CUT		0x0100
441 
442 /* USB_PM_CTRL_STATUS */
443 #define RESUME_INDICATE		0x0001
444 
445 /* USB_CSTMR */
446 #define FORCE_SUPER		BIT(0)
447 
448 /* USB_FW_CTRL */
449 #define FLOW_CTRL_PATCH_OPT	BIT(1)
450 
451 /* USB_FC_TIMER */
452 #define CTRL_TIMER_EN		BIT(15)
453 
454 /* USB_USB_CTRL */
455 #define RX_AGG_DISABLE		0x0010
456 #define RX_ZERO_EN		0x0080
457 
458 /* USB_U2P3_CTRL */
459 #define U2P3_ENABLE		0x0001
460 
461 /* USB_POWER_CUT */
462 #define PWR_EN			0x0001
463 #define PHASE2_EN		0x0008
464 #define UPS_EN			BIT(4)
465 #define USP_PREWAKE		BIT(5)
466 
467 /* USB_MISC_0 */
468 #define PCUT_STATUS		0x0001
469 
470 /* USB_RX_EARLY_TIMEOUT */
471 #define COALESCE_SUPER		 85000U
472 #define COALESCE_HIGH		250000U
473 #define COALESCE_SLOW		524280U
474 
475 /* USB_WDT1_CTRL */
476 #define WTD1_EN			BIT(0)
477 
478 /* USB_WDT11_CTRL */
479 #define TIMER11_EN		0x0001
480 
481 /* USB_LPM_CTRL */
482 /* bit 4 ~ 5: fifo empty boundary */
483 #define FIFO_EMPTY_1FB		0x30	/* 0x1fb * 64 = 32448 bytes */
484 /* bit 2 ~ 3: LMP timer */
485 #define LPM_TIMER_MASK		0x0c
486 #define LPM_TIMER_500MS		0x04	/* 500 ms */
487 #define LPM_TIMER_500US		0x0c	/* 500 us */
488 #define ROK_EXIT_LPM		0x02
489 
490 /* USB_AFE_CTRL2 */
491 #define SEN_VAL_MASK		0xf800
492 #define SEN_VAL_NORMAL		0xa000
493 #define SEL_RXIDLE		0x0100
494 
495 /* USB_UPS_CFG */
496 #define SAW_CNT_1MS_MASK	0x0fff
497 
498 /* USB_UPS_FLAGS */
499 #define UPS_FLAGS_R_TUNE		BIT(0)
500 #define UPS_FLAGS_EN_10M_CKDIV		BIT(1)
501 #define UPS_FLAGS_250M_CKDIV		BIT(2)
502 #define UPS_FLAGS_EN_ALDPS		BIT(3)
503 #define UPS_FLAGS_CTAP_SHORT_DIS	BIT(4)
504 #define ups_flags_speed(x)		((x) << 16)
505 #define UPS_FLAGS_EN_EEE		BIT(20)
506 #define UPS_FLAGS_EN_500M_EEE		BIT(21)
507 #define UPS_FLAGS_EN_EEE_CKDIV		BIT(22)
508 #define UPS_FLAGS_EEE_PLLOFF_100	BIT(23)
509 #define UPS_FLAGS_EEE_PLLOFF_GIGA	BIT(24)
510 #define UPS_FLAGS_EEE_CMOD_LV_EN	BIT(25)
511 #define UPS_FLAGS_EN_GREEN		BIT(26)
512 #define UPS_FLAGS_EN_FLOW_CTR		BIT(27)
513 
514 enum spd_duplex {
515 	NWAY_10M_HALF,
516 	NWAY_10M_FULL,
517 	NWAY_100M_HALF,
518 	NWAY_100M_FULL,
519 	NWAY_1000M_FULL,
520 	FORCE_10M_HALF,
521 	FORCE_10M_FULL,
522 	FORCE_100M_HALF,
523 	FORCE_100M_FULL,
524 };
525 
526 /* OCP_ALDPS_CONFIG */
527 #define ENPWRSAVE		0x8000
528 #define ENPDNPS			0x0200
529 #define LINKENA			0x0100
530 #define DIS_SDSAVE		0x0010
531 
532 /* OCP_PHY_STATUS */
533 #define PHY_STAT_MASK		0x0007
534 #define PHY_STAT_EXT_INIT	2
535 #define PHY_STAT_LAN_ON		3
536 #define PHY_STAT_PWRDN		5
537 
538 /* OCP_NCTL_CFG */
539 #define PGA_RETURN_EN		BIT(1)
540 
541 /* OCP_POWER_CFG */
542 #define EEE_CLKDIV_EN		0x8000
543 #define EN_ALDPS		0x0004
544 #define EN_10M_PLLOFF		0x0001
545 
546 /* OCP_EEE_CONFIG1 */
547 #define RG_TXLPI_MSK_HFDUP	0x8000
548 #define RG_MATCLR_EN		0x4000
549 #define EEE_10_CAP		0x2000
550 #define EEE_NWAY_EN		0x1000
551 #define TX_QUIET_EN		0x0200
552 #define RX_QUIET_EN		0x0100
553 #define sd_rise_time_mask	0x0070
554 #define sd_rise_time(x)		(min(x, 7) << 4)	/* bit 4 ~ 6 */
555 #define RG_RXLPI_MSK_HFDUP	0x0008
556 #define SDFALLTIME		0x0007	/* bit 0 ~ 2 */
557 
558 /* OCP_EEE_CONFIG2 */
559 #define RG_LPIHYS_NUM		0x7000	/* bit 12 ~ 15 */
560 #define RG_DACQUIET_EN		0x0400
561 #define RG_LDVQUIET_EN		0x0200
562 #define RG_CKRSEL		0x0020
563 #define RG_EEEPRG_EN		0x0010
564 
565 /* OCP_EEE_CONFIG3 */
566 #define fast_snr_mask		0xff80
567 #define fast_snr(x)		(min(x, 0x1ff) << 7)	/* bit 7 ~ 15 */
568 #define RG_LFS_SEL		0x0060	/* bit 6 ~ 5 */
569 #define MSK_PH			0x0006	/* bit 0 ~ 3 */
570 
571 /* OCP_EEE_AR */
572 /* bit[15:14] function */
573 #define FUN_ADDR		0x0000
574 #define FUN_DATA		0x4000
575 /* bit[4:0] device addr */
576 
577 /* OCP_EEE_CFG */
578 #define CTAP_SHORT_EN		0x0040
579 #define EEE10_EN		0x0010
580 
581 /* OCP_DOWN_SPEED */
582 #define EN_EEE_CMODE		BIT(14)
583 #define EN_EEE_1000		BIT(13)
584 #define EN_EEE_100		BIT(12)
585 #define EN_10M_CLKDIV		BIT(11)
586 #define EN_10M_BGOFF		0x0080
587 
588 /* OCP_PHY_STATE */
589 #define TXDIS_STATE		0x01
590 #define ABD_STATE		0x02
591 
592 /* OCP_PHY_PATCH_STAT */
593 #define PATCH_READY		BIT(6)
594 
595 /* OCP_PHY_PATCH_CMD */
596 #define PATCH_REQUEST		BIT(4)
597 
598 /* OCP_PHY_LOCK */
599 #define PATCH_LOCK		BIT(0)
600 
601 /* OCP_ADC_CFG */
602 #define CKADSEL_L		0x0100
603 #define ADC_EN			0x0080
604 #define EN_EMI_L		0x0040
605 
606 /* OCP_SYSCLK_CFG */
607 #define clk_div_expo(x)		(min(x, 5) << 8)
608 
609 /* SRAM_GREEN_CFG */
610 #define GREEN_ETH_EN		BIT(15)
611 #define R_TUNE_EN		BIT(11)
612 
613 /* SRAM_LPF_CFG */
614 #define LPF_AUTO_TUNE		0x8000
615 
616 /* SRAM_10M_AMP1 */
617 #define GDAC_IB_UPALL		0x0008
618 
619 /* SRAM_10M_AMP2 */
620 #define AMP_DN			0x0200
621 
622 /* SRAM_IMPEDANCE */
623 #define RX_DRIVING_MASK		0x6000
624 
625 /* SRAM_PHY_LOCK */
626 #define PHY_PATCH_LOCK		0x0001
627 
628 /* MAC PASSTHRU */
629 #define AD_MASK			0xfee0
630 #define BND_MASK		0x0004
631 #define BD_MASK			0x0001
632 #define EFUSE			0xcfdb
633 #define PASS_THRU_MASK		0x1
634 
635 #define BP4_SUPER_ONLY		0x1578	/* RTL_VER_04 only */
636 
637 enum rtl_register_content {
638 	_1000bps	= 0x10,
639 	_100bps		= 0x08,
640 	_10bps		= 0x04,
641 	LINK_STATUS	= 0x02,
642 	FULL_DUP	= 0x01,
643 };
644 
645 #define RTL8152_MAX_TX		4
646 #define RTL8152_MAX_RX		10
647 #define INTBUFSIZE		2
648 #define TX_ALIGN		4
649 #define RX_ALIGN		8
650 
651 #define RTL8152_RX_MAX_PENDING	4096
652 #define RTL8152_RXFG_HEADSZ	256
653 
654 #define INTR_LINK		0x0004
655 
656 #define RTL8152_REQT_READ	0xc0
657 #define RTL8152_REQT_WRITE	0x40
658 #define RTL8152_REQ_GET_REGS	0x05
659 #define RTL8152_REQ_SET_REGS	0x05
660 
661 #define BYTE_EN_DWORD		0xff
662 #define BYTE_EN_WORD		0x33
663 #define BYTE_EN_BYTE		0x11
664 #define BYTE_EN_SIX_BYTES	0x3f
665 #define BYTE_EN_START_MASK	0x0f
666 #define BYTE_EN_END_MASK	0xf0
667 
668 #define RTL8153_MAX_PACKET	9216 /* 9K */
669 #define RTL8153_MAX_MTU		(RTL8153_MAX_PACKET - VLAN_ETH_HLEN - \
670 				 ETH_FCS_LEN)
671 #define RTL8152_RMS		(VLAN_ETH_FRAME_LEN + ETH_FCS_LEN)
672 #define RTL8153_RMS		RTL8153_MAX_PACKET
673 #define RTL8152_TX_TIMEOUT	(5 * HZ)
674 #define RTL8152_NAPI_WEIGHT	64
675 #define rx_reserved_size(x)	((x) + VLAN_ETH_HLEN + ETH_FCS_LEN + \
676 				 sizeof(struct rx_desc) + RX_ALIGN)
677 
678 /* rtl8152 flags */
679 enum rtl8152_flags {
680 	RTL8152_UNPLUG = 0,
681 	RTL8152_SET_RX_MODE,
682 	WORK_ENABLE,
683 	RTL8152_LINK_CHG,
684 	SELECTIVE_SUSPEND,
685 	PHY_RESET,
686 	SCHEDULE_TASKLET,
687 	GREEN_ETHERNET,
688 	DELL_TB_RX_AGG_BUG,
689 	LENOVO_MACPASSTHRU,
690 };
691 
692 /* Define these values to match your device */
693 #define VENDOR_ID_REALTEK		0x0bda
694 #define VENDOR_ID_MICROSOFT		0x045e
695 #define VENDOR_ID_SAMSUNG		0x04e8
696 #define VENDOR_ID_LENOVO		0x17ef
697 #define VENDOR_ID_LINKSYS		0x13b1
698 #define VENDOR_ID_NVIDIA		0x0955
699 #define VENDOR_ID_TPLINK		0x2357
700 
701 #define MCU_TYPE_PLA			0x0100
702 #define MCU_TYPE_USB			0x0000
703 
704 struct tally_counter {
705 	__le64	tx_packets;
706 	__le64	rx_packets;
707 	__le64	tx_errors;
708 	__le32	rx_errors;
709 	__le16	rx_missed;
710 	__le16	align_errors;
711 	__le32	tx_one_collision;
712 	__le32	tx_multi_collision;
713 	__le64	rx_unicast;
714 	__le64	rx_broadcast;
715 	__le32	rx_multicast;
716 	__le16	tx_aborted;
717 	__le16	tx_underrun;
718 };
719 
720 struct rx_desc {
721 	__le32 opts1;
722 #define RX_LEN_MASK			0x7fff
723 
724 	__le32 opts2;
725 #define RD_UDP_CS			BIT(23)
726 #define RD_TCP_CS			BIT(22)
727 #define RD_IPV6_CS			BIT(20)
728 #define RD_IPV4_CS			BIT(19)
729 
730 	__le32 opts3;
731 #define IPF				BIT(23) /* IP checksum fail */
732 #define UDPF				BIT(22) /* UDP checksum fail */
733 #define TCPF				BIT(21) /* TCP checksum fail */
734 #define RX_VLAN_TAG			BIT(16)
735 
736 	__le32 opts4;
737 	__le32 opts5;
738 	__le32 opts6;
739 };
740 
741 struct tx_desc {
742 	__le32 opts1;
743 #define TX_FS			BIT(31) /* First segment of a packet */
744 #define TX_LS			BIT(30) /* Final segment of a packet */
745 #define GTSENDV4		BIT(28)
746 #define GTSENDV6		BIT(27)
747 #define GTTCPHO_SHIFT		18
748 #define GTTCPHO_MAX		0x7fU
749 #define TX_LEN_MAX		0x3ffffU
750 
751 	__le32 opts2;
752 #define UDP_CS			BIT(31) /* Calculate UDP/IP checksum */
753 #define TCP_CS			BIT(30) /* Calculate TCP/IP checksum */
754 #define IPV4_CS			BIT(29) /* Calculate IPv4 checksum */
755 #define IPV6_CS			BIT(28) /* Calculate IPv6 checksum */
756 #define MSS_SHIFT		17
757 #define MSS_MAX			0x7ffU
758 #define TCPHO_SHIFT		17
759 #define TCPHO_MAX		0x7ffU
760 #define TX_VLAN_TAG		BIT(16)
761 };
762 
763 struct r8152;
764 
765 struct rx_agg {
766 	struct list_head list, info_list;
767 	struct urb *urb;
768 	struct r8152 *context;
769 	struct page *page;
770 	void *buffer;
771 };
772 
773 struct tx_agg {
774 	struct list_head list;
775 	struct urb *urb;
776 	struct r8152 *context;
777 	void *buffer;
778 	void *head;
779 	u32 skb_num;
780 	u32 skb_len;
781 };
782 
783 struct r8152 {
784 	unsigned long flags;
785 	struct usb_device *udev;
786 	struct napi_struct napi;
787 	struct usb_interface *intf;
788 	struct net_device *netdev;
789 	struct urb *intr_urb;
790 	struct tx_agg tx_info[RTL8152_MAX_TX];
791 	struct list_head rx_info, rx_used;
792 	struct list_head rx_done, tx_free;
793 	struct sk_buff_head tx_queue, rx_queue;
794 	spinlock_t rx_lock, tx_lock;
795 	struct delayed_work schedule, hw_phy_work;
796 	struct mii_if_info mii;
797 	struct mutex control;	/* use for hw setting */
798 #ifdef CONFIG_PM_SLEEP
799 	struct notifier_block pm_notifier;
800 #endif
801 	struct tasklet_struct tx_tl;
802 
803 	struct rtl_ops {
804 		void (*init)(struct r8152 *tp);
805 		int (*enable)(struct r8152 *tp);
806 		void (*disable)(struct r8152 *tp);
807 		void (*up)(struct r8152 *tp);
808 		void (*down)(struct r8152 *tp);
809 		void (*unload)(struct r8152 *tp);
810 		int (*eee_get)(struct r8152 *tp, struct ethtool_eee *eee);
811 		int (*eee_set)(struct r8152 *tp, struct ethtool_eee *eee);
812 		bool (*in_nway)(struct r8152 *tp);
813 		void (*hw_phy_cfg)(struct r8152 *tp);
814 		void (*autosuspend_en)(struct r8152 *tp, bool enable);
815 	} rtl_ops;
816 
817 	struct ups_info {
818 		u32 _10m_ckdiv:1;
819 		u32 _250m_ckdiv:1;
820 		u32 aldps:1;
821 		u32 lite_mode:2;
822 		u32 speed_duplex:4;
823 		u32 eee:1;
824 		u32 eee_lite:1;
825 		u32 eee_ckdiv:1;
826 		u32 eee_plloff_100:1;
827 		u32 eee_plloff_giga:1;
828 		u32 eee_cmod_lv:1;
829 		u32 green:1;
830 		u32 flow_control:1;
831 		u32 ctap_short_off:1;
832 	} ups_info;
833 
834 #define RTL_VER_SIZE		32
835 
836 	struct rtl_fw {
837 		const char *fw_name;
838 		const struct firmware *fw;
839 
840 		char version[RTL_VER_SIZE];
841 		int (*pre_fw)(struct r8152 *tp);
842 		int (*post_fw)(struct r8152 *tp);
843 
844 		bool retry;
845 	} rtl_fw;
846 
847 	atomic_t rx_count;
848 
849 	bool eee_en;
850 	int intr_interval;
851 	u32 saved_wolopts;
852 	u32 msg_enable;
853 	u32 tx_qlen;
854 	u32 coalesce;
855 	u32 advertising;
856 	u32 rx_buf_sz;
857 	u32 rx_copybreak;
858 	u32 rx_pending;
859 
860 	u16 ocp_base;
861 	u16 speed;
862 	u16 eee_adv;
863 	u8 *intr_buff;
864 	u8 version;
865 	u8 duplex;
866 	u8 autoneg;
867 };
868 
869 /**
870  * struct fw_block - block type and total length
871  * @type: type of the current block, such as RTL_FW_END, RTL_FW_PLA,
872  *	RTL_FW_USB and so on.
873  * @length: total length of the current block.
874  */
875 struct fw_block {
876 	__le32 type;
877 	__le32 length;
878 } __packed;
879 
880 /**
881  * struct fw_header - header of the firmware file
882  * @checksum: checksum of sha256 which is calculated from the whole file
883  *	except the checksum field of the file. That is, calculate sha256
884  *	from the version field to the end of the file.
885  * @version: version of this firmware.
886  * @blocks: the first firmware block of the file
887  */
888 struct fw_header {
889 	u8 checksum[32];
890 	char version[RTL_VER_SIZE];
891 	struct fw_block blocks[0];
892 } __packed;
893 
894 /**
895  * struct fw_mac - a firmware block used by RTL_FW_PLA and RTL_FW_USB.
896  *	The layout of the firmware block is:
897  *	<struct fw_mac> + <info> + <firmware data>.
898  * @fw_offset: offset of the firmware binary data. The start address of
899  *	the data would be the address of struct fw_mac + @fw_offset.
900  * @fw_reg: the register to load the firmware. Depends on chip.
901  * @bp_ba_addr: the register to write break point base address. Depends on
902  *	chip.
903  * @bp_ba_value: break point base address. Depends on chip.
904  * @bp_en_addr: the register to write break point enabled mask. Depends
905  *	on chip.
906  * @bp_en_value: break point enabled mask. Depends on the firmware.
907  * @bp_start: the start register of break points. Depends on chip.
908  * @bp_num: the break point number which needs to be set for this firmware.
909  *	Depends on the firmware.
910  * @bp: break points. Depends on firmware.
911  * @fw_ver_reg: the register to store the fw version.
912  * @fw_ver_data: the firmware version of the current type.
913  * @info: additional information for debugging, and is followed by the
914  *	binary data of firmware.
915  */
916 struct fw_mac {
917 	struct fw_block blk_hdr;
918 	__le16 fw_offset;
919 	__le16 fw_reg;
920 	__le16 bp_ba_addr;
921 	__le16 bp_ba_value;
922 	__le16 bp_en_addr;
923 	__le16 bp_en_value;
924 	__le16 bp_start;
925 	__le16 bp_num;
926 	__le16 bp[16]; /* any value determined by firmware */
927 	__le32 reserved;
928 	__le16 fw_ver_reg;
929 	u8 fw_ver_data;
930 	char info[0];
931 } __packed;
932 
933 /**
934  * struct fw_phy_patch_key - a firmware block used by RTL_FW_PHY_START.
935  *	This is used to set patch key when loading the firmware of PHY.
936  * @key_reg: the register to write the patch key.
937  * @key_data: patch key.
938  */
939 struct fw_phy_patch_key {
940 	struct fw_block blk_hdr;
941 	__le16 key_reg;
942 	__le16 key_data;
943 	__le32 reserved;
944 } __packed;
945 
946 /**
947  * struct fw_phy_nc - a firmware block used by RTL_FW_PHY_NC.
948  *	The layout of the firmware block is:
949  *	<struct fw_phy_nc> + <info> + <firmware data>.
950  * @fw_offset: offset of the firmware binary data. The start address of
951  *	the data would be the address of struct fw_phy_nc + @fw_offset.
952  * @fw_reg: the register to load the firmware. Depends on chip.
953  * @ba_reg: the register to write the base address. Depends on chip.
954  * @ba_data: base address. Depends on chip.
955  * @patch_en_addr: the register of enabling patch mode. Depends on chip.
956  * @patch_en_value: patch mode enabled mask. Depends on the firmware.
957  * @mode_reg: the regitster of switching the mode.
958  * @mod_pre: the mode needing to be set before loading the firmware.
959  * @mod_post: the mode to be set when finishing to load the firmware.
960  * @bp_start: the start register of break points. Depends on chip.
961  * @bp_num: the break point number which needs to be set for this firmware.
962  *	Depends on the firmware.
963  * @bp: break points. Depends on firmware.
964  * @info: additional information for debugging, and is followed by the
965  *	binary data of firmware.
966  */
967 struct fw_phy_nc {
968 	struct fw_block blk_hdr;
969 	__le16 fw_offset;
970 	__le16 fw_reg;
971 	__le16 ba_reg;
972 	__le16 ba_data;
973 	__le16 patch_en_addr;
974 	__le16 patch_en_value;
975 	__le16 mode_reg;
976 	__le16 mode_pre;
977 	__le16 mode_post;
978 	__le16 reserved;
979 	__le16 bp_start;
980 	__le16 bp_num;
981 	__le16 bp[4];
982 	char info[0];
983 } __packed;
984 
985 enum rtl_fw_type {
986 	RTL_FW_END = 0,
987 	RTL_FW_PLA,
988 	RTL_FW_USB,
989 	RTL_FW_PHY_START,
990 	RTL_FW_PHY_STOP,
991 	RTL_FW_PHY_NC,
992 };
993 
994 enum rtl_version {
995 	RTL_VER_UNKNOWN = 0,
996 	RTL_VER_01,
997 	RTL_VER_02,
998 	RTL_VER_03,
999 	RTL_VER_04,
1000 	RTL_VER_05,
1001 	RTL_VER_06,
1002 	RTL_VER_07,
1003 	RTL_VER_08,
1004 	RTL_VER_09,
1005 	RTL_VER_MAX
1006 };
1007 
1008 enum tx_csum_stat {
1009 	TX_CSUM_SUCCESS = 0,
1010 	TX_CSUM_TSO,
1011 	TX_CSUM_NONE
1012 };
1013 
1014 #define RTL_ADVERTISED_10_HALF			BIT(0)
1015 #define RTL_ADVERTISED_10_FULL			BIT(1)
1016 #define RTL_ADVERTISED_100_HALF			BIT(2)
1017 #define RTL_ADVERTISED_100_FULL			BIT(3)
1018 #define RTL_ADVERTISED_1000_HALF		BIT(4)
1019 #define RTL_ADVERTISED_1000_FULL		BIT(5)
1020 
1021 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
1022  * The RTL chips use a 64 element hash table based on the Ethernet CRC.
1023  */
1024 static const int multicast_filter_limit = 32;
1025 static unsigned int agg_buf_sz = 16384;
1026 
1027 #define RTL_LIMITED_TSO_SIZE	(agg_buf_sz - sizeof(struct tx_desc) - \
1028 				 VLAN_ETH_HLEN - ETH_FCS_LEN)
1029 
1030 static
1031 int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
1032 {
1033 	int ret;
1034 	void *tmp;
1035 
1036 	tmp = kmalloc(size, GFP_KERNEL);
1037 	if (!tmp)
1038 		return -ENOMEM;
1039 
1040 	ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0),
1041 			      RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
1042 			      value, index, tmp, size, 500);
1043 	if (ret < 0)
1044 		memset(data, 0xff, size);
1045 	else
1046 		memcpy(data, tmp, size);
1047 
1048 	kfree(tmp);
1049 
1050 	return ret;
1051 }
1052 
1053 static
1054 int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
1055 {
1056 	int ret;
1057 	void *tmp;
1058 
1059 	tmp = kmemdup(data, size, GFP_KERNEL);
1060 	if (!tmp)
1061 		return -ENOMEM;
1062 
1063 	ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0),
1064 			      RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
1065 			      value, index, tmp, size, 500);
1066 
1067 	kfree(tmp);
1068 
1069 	return ret;
1070 }
1071 
1072 static void rtl_set_unplug(struct r8152 *tp)
1073 {
1074 	if (tp->udev->state == USB_STATE_NOTATTACHED) {
1075 		set_bit(RTL8152_UNPLUG, &tp->flags);
1076 		smp_mb__after_atomic();
1077 	}
1078 }
1079 
1080 static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
1081 			    void *data, u16 type)
1082 {
1083 	u16 limit = 64;
1084 	int ret = 0;
1085 
1086 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
1087 		return -ENODEV;
1088 
1089 	/* both size and indix must be 4 bytes align */
1090 	if ((size & 3) || !size || (index & 3) || !data)
1091 		return -EPERM;
1092 
1093 	if ((u32)index + (u32)size > 0xffff)
1094 		return -EPERM;
1095 
1096 	while (size) {
1097 		if (size > limit) {
1098 			ret = get_registers(tp, index, type, limit, data);
1099 			if (ret < 0)
1100 				break;
1101 
1102 			index += limit;
1103 			data += limit;
1104 			size -= limit;
1105 		} else {
1106 			ret = get_registers(tp, index, type, size, data);
1107 			if (ret < 0)
1108 				break;
1109 
1110 			index += size;
1111 			data += size;
1112 			size = 0;
1113 			break;
1114 		}
1115 	}
1116 
1117 	if (ret == -ENODEV)
1118 		rtl_set_unplug(tp);
1119 
1120 	return ret;
1121 }
1122 
1123 static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
1124 			     u16 size, void *data, u16 type)
1125 {
1126 	int ret;
1127 	u16 byteen_start, byteen_end, byen;
1128 	u16 limit = 512;
1129 
1130 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
1131 		return -ENODEV;
1132 
1133 	/* both size and indix must be 4 bytes align */
1134 	if ((size & 3) || !size || (index & 3) || !data)
1135 		return -EPERM;
1136 
1137 	if ((u32)index + (u32)size > 0xffff)
1138 		return -EPERM;
1139 
1140 	byteen_start = byteen & BYTE_EN_START_MASK;
1141 	byteen_end = byteen & BYTE_EN_END_MASK;
1142 
1143 	byen = byteen_start | (byteen_start << 4);
1144 	ret = set_registers(tp, index, type | byen, 4, data);
1145 	if (ret < 0)
1146 		goto error1;
1147 
1148 	index += 4;
1149 	data += 4;
1150 	size -= 4;
1151 
1152 	if (size) {
1153 		size -= 4;
1154 
1155 		while (size) {
1156 			if (size > limit) {
1157 				ret = set_registers(tp, index,
1158 						    type | BYTE_EN_DWORD,
1159 						    limit, data);
1160 				if (ret < 0)
1161 					goto error1;
1162 
1163 				index += limit;
1164 				data += limit;
1165 				size -= limit;
1166 			} else {
1167 				ret = set_registers(tp, index,
1168 						    type | BYTE_EN_DWORD,
1169 						    size, data);
1170 				if (ret < 0)
1171 					goto error1;
1172 
1173 				index += size;
1174 				data += size;
1175 				size = 0;
1176 				break;
1177 			}
1178 		}
1179 
1180 		byen = byteen_end | (byteen_end >> 4);
1181 		ret = set_registers(tp, index, type | byen, 4, data);
1182 		if (ret < 0)
1183 			goto error1;
1184 	}
1185 
1186 error1:
1187 	if (ret == -ENODEV)
1188 		rtl_set_unplug(tp);
1189 
1190 	return ret;
1191 }
1192 
1193 static inline
1194 int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
1195 {
1196 	return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
1197 }
1198 
1199 static inline
1200 int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
1201 {
1202 	return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
1203 }
1204 
1205 static inline
1206 int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
1207 {
1208 	return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
1209 }
1210 
1211 static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
1212 {
1213 	__le32 data;
1214 
1215 	generic_ocp_read(tp, index, sizeof(data), &data, type);
1216 
1217 	return __le32_to_cpu(data);
1218 }
1219 
1220 static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
1221 {
1222 	__le32 tmp = __cpu_to_le32(data);
1223 
1224 	generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
1225 }
1226 
1227 static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
1228 {
1229 	u32 data;
1230 	__le32 tmp;
1231 	u16 byen = BYTE_EN_WORD;
1232 	u8 shift = index & 2;
1233 
1234 	index &= ~3;
1235 	byen <<= shift;
1236 
1237 	generic_ocp_read(tp, index, sizeof(tmp), &tmp, type | byen);
1238 
1239 	data = __le32_to_cpu(tmp);
1240 	data >>= (shift * 8);
1241 	data &= 0xffff;
1242 
1243 	return (u16)data;
1244 }
1245 
1246 static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
1247 {
1248 	u32 mask = 0xffff;
1249 	__le32 tmp;
1250 	u16 byen = BYTE_EN_WORD;
1251 	u8 shift = index & 2;
1252 
1253 	data &= mask;
1254 
1255 	if (index & 2) {
1256 		byen <<= shift;
1257 		mask <<= (shift * 8);
1258 		data <<= (shift * 8);
1259 		index &= ~3;
1260 	}
1261 
1262 	tmp = __cpu_to_le32(data);
1263 
1264 	generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
1265 }
1266 
1267 static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
1268 {
1269 	u32 data;
1270 	__le32 tmp;
1271 	u8 shift = index & 3;
1272 
1273 	index &= ~3;
1274 
1275 	generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
1276 
1277 	data = __le32_to_cpu(tmp);
1278 	data >>= (shift * 8);
1279 	data &= 0xff;
1280 
1281 	return (u8)data;
1282 }
1283 
1284 static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
1285 {
1286 	u32 mask = 0xff;
1287 	__le32 tmp;
1288 	u16 byen = BYTE_EN_BYTE;
1289 	u8 shift = index & 3;
1290 
1291 	data &= mask;
1292 
1293 	if (index & 3) {
1294 		byen <<= shift;
1295 		mask <<= (shift * 8);
1296 		data <<= (shift * 8);
1297 		index &= ~3;
1298 	}
1299 
1300 	tmp = __cpu_to_le32(data);
1301 
1302 	generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
1303 }
1304 
1305 static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
1306 {
1307 	u16 ocp_base, ocp_index;
1308 
1309 	ocp_base = addr & 0xf000;
1310 	if (ocp_base != tp->ocp_base) {
1311 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
1312 		tp->ocp_base = ocp_base;
1313 	}
1314 
1315 	ocp_index = (addr & 0x0fff) | 0xb000;
1316 	return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
1317 }
1318 
1319 static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
1320 {
1321 	u16 ocp_base, ocp_index;
1322 
1323 	ocp_base = addr & 0xf000;
1324 	if (ocp_base != tp->ocp_base) {
1325 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
1326 		tp->ocp_base = ocp_base;
1327 	}
1328 
1329 	ocp_index = (addr & 0x0fff) | 0xb000;
1330 	ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
1331 }
1332 
1333 static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
1334 {
1335 	ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
1336 }
1337 
1338 static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
1339 {
1340 	return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2);
1341 }
1342 
1343 static void sram_write(struct r8152 *tp, u16 addr, u16 data)
1344 {
1345 	ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
1346 	ocp_reg_write(tp, OCP_SRAM_DATA, data);
1347 }
1348 
1349 static u16 sram_read(struct r8152 *tp, u16 addr)
1350 {
1351 	ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
1352 	return ocp_reg_read(tp, OCP_SRAM_DATA);
1353 }
1354 
1355 static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
1356 {
1357 	struct r8152 *tp = netdev_priv(netdev);
1358 	int ret;
1359 
1360 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
1361 		return -ENODEV;
1362 
1363 	if (phy_id != R8152_PHY_ID)
1364 		return -EINVAL;
1365 
1366 	ret = r8152_mdio_read(tp, reg);
1367 
1368 	return ret;
1369 }
1370 
1371 static
1372 void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
1373 {
1374 	struct r8152 *tp = netdev_priv(netdev);
1375 
1376 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
1377 		return;
1378 
1379 	if (phy_id != R8152_PHY_ID)
1380 		return;
1381 
1382 	r8152_mdio_write(tp, reg, val);
1383 }
1384 
1385 static int
1386 r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);
1387 
1388 static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
1389 {
1390 	struct r8152 *tp = netdev_priv(netdev);
1391 	struct sockaddr *addr = p;
1392 	int ret = -EADDRNOTAVAIL;
1393 
1394 	if (!is_valid_ether_addr(addr->sa_data))
1395 		goto out1;
1396 
1397 	ret = usb_autopm_get_interface(tp->intf);
1398 	if (ret < 0)
1399 		goto out1;
1400 
1401 	mutex_lock(&tp->control);
1402 
1403 	memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1404 
1405 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
1406 	pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
1407 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
1408 
1409 	mutex_unlock(&tp->control);
1410 
1411 	usb_autopm_put_interface(tp->intf);
1412 out1:
1413 	return ret;
1414 }
1415 
1416 /* Devices containing proper chips can support a persistent
1417  * host system provided MAC address.
1418  * Examples of this are Dell TB15 and Dell WD15 docks
1419  */
1420 static int vendor_mac_passthru_addr_read(struct r8152 *tp, struct sockaddr *sa)
1421 {
1422 	acpi_status status;
1423 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1424 	union acpi_object *obj;
1425 	int ret = -EINVAL;
1426 	u32 ocp_data;
1427 	unsigned char buf[6];
1428 	char *mac_obj_name;
1429 	acpi_object_type mac_obj_type;
1430 	int mac_strlen;
1431 
1432 	if (test_bit(LENOVO_MACPASSTHRU, &tp->flags)) {
1433 		mac_obj_name = "\\MACA";
1434 		mac_obj_type = ACPI_TYPE_STRING;
1435 		mac_strlen = 0x16;
1436 	} else {
1437 		/* test for -AD variant of RTL8153 */
1438 		ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
1439 		if ((ocp_data & AD_MASK) == 0x1000) {
1440 			/* test for MAC address pass-through bit */
1441 			ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, EFUSE);
1442 			if ((ocp_data & PASS_THRU_MASK) != 1) {
1443 				netif_dbg(tp, probe, tp->netdev,
1444 						"No efuse for RTL8153-AD MAC pass through\n");
1445 				return -ENODEV;
1446 			}
1447 		} else {
1448 			/* test for RTL8153-BND and RTL8153-BD */
1449 			ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_1);
1450 			if ((ocp_data & BND_MASK) == 0 && (ocp_data & BD_MASK) == 0) {
1451 				netif_dbg(tp, probe, tp->netdev,
1452 						"Invalid variant for MAC pass through\n");
1453 				return -ENODEV;
1454 			}
1455 		}
1456 
1457 		mac_obj_name = "\\_SB.AMAC";
1458 		mac_obj_type = ACPI_TYPE_BUFFER;
1459 		mac_strlen = 0x17;
1460 	}
1461 
1462 	/* returns _AUXMAC_#AABBCCDDEEFF# */
1463 	status = acpi_evaluate_object(NULL, mac_obj_name, NULL, &buffer);
1464 	obj = (union acpi_object *)buffer.pointer;
1465 	if (!ACPI_SUCCESS(status))
1466 		return -ENODEV;
1467 	if (obj->type != mac_obj_type || obj->string.length != mac_strlen) {
1468 		netif_warn(tp, probe, tp->netdev,
1469 			   "Invalid buffer for pass-thru MAC addr: (%d, %d)\n",
1470 			   obj->type, obj->string.length);
1471 		goto amacout;
1472 	}
1473 
1474 	if (strncmp(obj->string.pointer, "_AUXMAC_#", 9) != 0 ||
1475 	    strncmp(obj->string.pointer + 0x15, "#", 1) != 0) {
1476 		netif_warn(tp, probe, tp->netdev,
1477 			   "Invalid header when reading pass-thru MAC addr\n");
1478 		goto amacout;
1479 	}
1480 	ret = hex2bin(buf, obj->string.pointer + 9, 6);
1481 	if (!(ret == 0 && is_valid_ether_addr(buf))) {
1482 		netif_warn(tp, probe, tp->netdev,
1483 			   "Invalid MAC for pass-thru MAC addr: %d, %pM\n",
1484 			   ret, buf);
1485 		ret = -EINVAL;
1486 		goto amacout;
1487 	}
1488 	memcpy(sa->sa_data, buf, 6);
1489 	netif_info(tp, probe, tp->netdev,
1490 		   "Using pass-thru MAC addr %pM\n", sa->sa_data);
1491 
1492 amacout:
1493 	kfree(obj);
1494 	return ret;
1495 }
1496 
1497 static int determine_ethernet_addr(struct r8152 *tp, struct sockaddr *sa)
1498 {
1499 	struct net_device *dev = tp->netdev;
1500 	int ret;
1501 
1502 	sa->sa_family = dev->type;
1503 
1504 	if (tp->version == RTL_VER_01) {
1505 		ret = pla_ocp_read(tp, PLA_IDR, 8, sa->sa_data);
1506 	} else {
1507 		/* if device doesn't support MAC pass through this will
1508 		 * be expected to be non-zero
1509 		 */
1510 		ret = vendor_mac_passthru_addr_read(tp, sa);
1511 		if (ret < 0)
1512 			ret = pla_ocp_read(tp, PLA_BACKUP, 8, sa->sa_data);
1513 	}
1514 
1515 	if (ret < 0) {
1516 		netif_err(tp, probe, dev, "Get ether addr fail\n");
1517 	} else if (!is_valid_ether_addr(sa->sa_data)) {
1518 		netif_err(tp, probe, dev, "Invalid ether addr %pM\n",
1519 			  sa->sa_data);
1520 		eth_hw_addr_random(dev);
1521 		ether_addr_copy(sa->sa_data, dev->dev_addr);
1522 		netif_info(tp, probe, dev, "Random ether addr %pM\n",
1523 			   sa->sa_data);
1524 		return 0;
1525 	}
1526 
1527 	return ret;
1528 }
1529 
1530 static int set_ethernet_addr(struct r8152 *tp)
1531 {
1532 	struct net_device *dev = tp->netdev;
1533 	struct sockaddr sa;
1534 	int ret;
1535 
1536 	ret = determine_ethernet_addr(tp, &sa);
1537 	if (ret < 0)
1538 		return ret;
1539 
1540 	if (tp->version == RTL_VER_01)
1541 		ether_addr_copy(dev->dev_addr, sa.sa_data);
1542 	else
1543 		ret = rtl8152_set_mac_address(dev, &sa);
1544 
1545 	return ret;
1546 }
1547 
1548 static void read_bulk_callback(struct urb *urb)
1549 {
1550 	struct net_device *netdev;
1551 	int status = urb->status;
1552 	struct rx_agg *agg;
1553 	struct r8152 *tp;
1554 	unsigned long flags;
1555 
1556 	agg = urb->context;
1557 	if (!agg)
1558 		return;
1559 
1560 	tp = agg->context;
1561 	if (!tp)
1562 		return;
1563 
1564 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
1565 		return;
1566 
1567 	if (!test_bit(WORK_ENABLE, &tp->flags))
1568 		return;
1569 
1570 	netdev = tp->netdev;
1571 
1572 	/* When link down, the driver would cancel all bulks. */
1573 	/* This avoid the re-submitting bulk */
1574 	if (!netif_carrier_ok(netdev))
1575 		return;
1576 
1577 	usb_mark_last_busy(tp->udev);
1578 
1579 	switch (status) {
1580 	case 0:
1581 		if (urb->actual_length < ETH_ZLEN)
1582 			break;
1583 
1584 		spin_lock_irqsave(&tp->rx_lock, flags);
1585 		list_add_tail(&agg->list, &tp->rx_done);
1586 		spin_unlock_irqrestore(&tp->rx_lock, flags);
1587 		napi_schedule(&tp->napi);
1588 		return;
1589 	case -ESHUTDOWN:
1590 		rtl_set_unplug(tp);
1591 		netif_device_detach(tp->netdev);
1592 		return;
1593 	case -ENOENT:
1594 		return;	/* the urb is in unlink state */
1595 	case -ETIME:
1596 		if (net_ratelimit())
1597 			netdev_warn(netdev, "maybe reset is needed?\n");
1598 		break;
1599 	default:
1600 		if (net_ratelimit())
1601 			netdev_warn(netdev, "Rx status %d\n", status);
1602 		break;
1603 	}
1604 
1605 	r8152_submit_rx(tp, agg, GFP_ATOMIC);
1606 }
1607 
1608 static void write_bulk_callback(struct urb *urb)
1609 {
1610 	struct net_device_stats *stats;
1611 	struct net_device *netdev;
1612 	struct tx_agg *agg;
1613 	struct r8152 *tp;
1614 	unsigned long flags;
1615 	int status = urb->status;
1616 
1617 	agg = urb->context;
1618 	if (!agg)
1619 		return;
1620 
1621 	tp = agg->context;
1622 	if (!tp)
1623 		return;
1624 
1625 	netdev = tp->netdev;
1626 	stats = &netdev->stats;
1627 	if (status) {
1628 		if (net_ratelimit())
1629 			netdev_warn(netdev, "Tx status %d\n", status);
1630 		stats->tx_errors += agg->skb_num;
1631 	} else {
1632 		stats->tx_packets += agg->skb_num;
1633 		stats->tx_bytes += agg->skb_len;
1634 	}
1635 
1636 	spin_lock_irqsave(&tp->tx_lock, flags);
1637 	list_add_tail(&agg->list, &tp->tx_free);
1638 	spin_unlock_irqrestore(&tp->tx_lock, flags);
1639 
1640 	usb_autopm_put_interface_async(tp->intf);
1641 
1642 	if (!netif_carrier_ok(netdev))
1643 		return;
1644 
1645 	if (!test_bit(WORK_ENABLE, &tp->flags))
1646 		return;
1647 
1648 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
1649 		return;
1650 
1651 	if (!skb_queue_empty(&tp->tx_queue))
1652 		tasklet_schedule(&tp->tx_tl);
1653 }
1654 
1655 static void intr_callback(struct urb *urb)
1656 {
1657 	struct r8152 *tp;
1658 	__le16 *d;
1659 	int status = urb->status;
1660 	int res;
1661 
1662 	tp = urb->context;
1663 	if (!tp)
1664 		return;
1665 
1666 	if (!test_bit(WORK_ENABLE, &tp->flags))
1667 		return;
1668 
1669 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
1670 		return;
1671 
1672 	switch (status) {
1673 	case 0:			/* success */
1674 		break;
1675 	case -ECONNRESET:	/* unlink */
1676 	case -ESHUTDOWN:
1677 		netif_device_detach(tp->netdev);
1678 		/* fall through */
1679 	case -ENOENT:
1680 	case -EPROTO:
1681 		netif_info(tp, intr, tp->netdev,
1682 			   "Stop submitting intr, status %d\n", status);
1683 		return;
1684 	case -EOVERFLOW:
1685 		netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n");
1686 		goto resubmit;
1687 	/* -EPIPE:  should clear the halt */
1688 	default:
1689 		netif_info(tp, intr, tp->netdev, "intr status %d\n", status);
1690 		goto resubmit;
1691 	}
1692 
1693 	d = urb->transfer_buffer;
1694 	if (INTR_LINK & __le16_to_cpu(d[0])) {
1695 		if (!netif_carrier_ok(tp->netdev)) {
1696 			set_bit(RTL8152_LINK_CHG, &tp->flags);
1697 			schedule_delayed_work(&tp->schedule, 0);
1698 		}
1699 	} else {
1700 		if (netif_carrier_ok(tp->netdev)) {
1701 			netif_stop_queue(tp->netdev);
1702 			set_bit(RTL8152_LINK_CHG, &tp->flags);
1703 			schedule_delayed_work(&tp->schedule, 0);
1704 		}
1705 	}
1706 
1707 resubmit:
1708 	res = usb_submit_urb(urb, GFP_ATOMIC);
1709 	if (res == -ENODEV) {
1710 		rtl_set_unplug(tp);
1711 		netif_device_detach(tp->netdev);
1712 	} else if (res) {
1713 		netif_err(tp, intr, tp->netdev,
1714 			  "can't resubmit intr, status %d\n", res);
1715 	}
1716 }
1717 
1718 static inline void *rx_agg_align(void *data)
1719 {
1720 	return (void *)ALIGN((uintptr_t)data, RX_ALIGN);
1721 }
1722 
1723 static inline void *tx_agg_align(void *data)
1724 {
1725 	return (void *)ALIGN((uintptr_t)data, TX_ALIGN);
1726 }
1727 
1728 static void free_rx_agg(struct r8152 *tp, struct rx_agg *agg)
1729 {
1730 	list_del(&agg->info_list);
1731 
1732 	usb_free_urb(agg->urb);
1733 	put_page(agg->page);
1734 	kfree(agg);
1735 
1736 	atomic_dec(&tp->rx_count);
1737 }
1738 
1739 static struct rx_agg *alloc_rx_agg(struct r8152 *tp, gfp_t mflags)
1740 {
1741 	struct net_device *netdev = tp->netdev;
1742 	int node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
1743 	unsigned int order = get_order(tp->rx_buf_sz);
1744 	struct rx_agg *rx_agg;
1745 	unsigned long flags;
1746 
1747 	rx_agg = kmalloc_node(sizeof(*rx_agg), mflags, node);
1748 	if (!rx_agg)
1749 		return NULL;
1750 
1751 	rx_agg->page = alloc_pages(mflags | __GFP_COMP, order);
1752 	if (!rx_agg->page)
1753 		goto free_rx;
1754 
1755 	rx_agg->buffer = page_address(rx_agg->page);
1756 
1757 	rx_agg->urb = usb_alloc_urb(0, mflags);
1758 	if (!rx_agg->urb)
1759 		goto free_buf;
1760 
1761 	rx_agg->context = tp;
1762 
1763 	INIT_LIST_HEAD(&rx_agg->list);
1764 	INIT_LIST_HEAD(&rx_agg->info_list);
1765 	spin_lock_irqsave(&tp->rx_lock, flags);
1766 	list_add_tail(&rx_agg->info_list, &tp->rx_info);
1767 	spin_unlock_irqrestore(&tp->rx_lock, flags);
1768 
1769 	atomic_inc(&tp->rx_count);
1770 
1771 	return rx_agg;
1772 
1773 free_buf:
1774 	__free_pages(rx_agg->page, order);
1775 free_rx:
1776 	kfree(rx_agg);
1777 	return NULL;
1778 }
1779 
1780 static void free_all_mem(struct r8152 *tp)
1781 {
1782 	struct rx_agg *agg, *agg_next;
1783 	unsigned long flags;
1784 	int i;
1785 
1786 	spin_lock_irqsave(&tp->rx_lock, flags);
1787 
1788 	list_for_each_entry_safe(agg, agg_next, &tp->rx_info, info_list)
1789 		free_rx_agg(tp, agg);
1790 
1791 	spin_unlock_irqrestore(&tp->rx_lock, flags);
1792 
1793 	WARN_ON(atomic_read(&tp->rx_count));
1794 
1795 	for (i = 0; i < RTL8152_MAX_TX; i++) {
1796 		usb_free_urb(tp->tx_info[i].urb);
1797 		tp->tx_info[i].urb = NULL;
1798 
1799 		kfree(tp->tx_info[i].buffer);
1800 		tp->tx_info[i].buffer = NULL;
1801 		tp->tx_info[i].head = NULL;
1802 	}
1803 
1804 	usb_free_urb(tp->intr_urb);
1805 	tp->intr_urb = NULL;
1806 
1807 	kfree(tp->intr_buff);
1808 	tp->intr_buff = NULL;
1809 }
1810 
1811 static int alloc_all_mem(struct r8152 *tp)
1812 {
1813 	struct net_device *netdev = tp->netdev;
1814 	struct usb_interface *intf = tp->intf;
1815 	struct usb_host_interface *alt = intf->cur_altsetting;
1816 	struct usb_host_endpoint *ep_intr = alt->endpoint + 2;
1817 	int node, i;
1818 
1819 	node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
1820 
1821 	spin_lock_init(&tp->rx_lock);
1822 	spin_lock_init(&tp->tx_lock);
1823 	INIT_LIST_HEAD(&tp->rx_info);
1824 	INIT_LIST_HEAD(&tp->tx_free);
1825 	INIT_LIST_HEAD(&tp->rx_done);
1826 	skb_queue_head_init(&tp->tx_queue);
1827 	skb_queue_head_init(&tp->rx_queue);
1828 	atomic_set(&tp->rx_count, 0);
1829 
1830 	for (i = 0; i < RTL8152_MAX_RX; i++) {
1831 		if (!alloc_rx_agg(tp, GFP_KERNEL))
1832 			goto err1;
1833 	}
1834 
1835 	for (i = 0; i < RTL8152_MAX_TX; i++) {
1836 		struct urb *urb;
1837 		u8 *buf;
1838 
1839 		buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node);
1840 		if (!buf)
1841 			goto err1;
1842 
1843 		if (buf != tx_agg_align(buf)) {
1844 			kfree(buf);
1845 			buf = kmalloc_node(agg_buf_sz + TX_ALIGN, GFP_KERNEL,
1846 					   node);
1847 			if (!buf)
1848 				goto err1;
1849 		}
1850 
1851 		urb = usb_alloc_urb(0, GFP_KERNEL);
1852 		if (!urb) {
1853 			kfree(buf);
1854 			goto err1;
1855 		}
1856 
1857 		INIT_LIST_HEAD(&tp->tx_info[i].list);
1858 		tp->tx_info[i].context = tp;
1859 		tp->tx_info[i].urb = urb;
1860 		tp->tx_info[i].buffer = buf;
1861 		tp->tx_info[i].head = tx_agg_align(buf);
1862 
1863 		list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
1864 	}
1865 
1866 	tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1867 	if (!tp->intr_urb)
1868 		goto err1;
1869 
1870 	tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
1871 	if (!tp->intr_buff)
1872 		goto err1;
1873 
1874 	tp->intr_interval = (int)ep_intr->desc.bInterval;
1875 	usb_fill_int_urb(tp->intr_urb, tp->udev, usb_rcvintpipe(tp->udev, 3),
1876 			 tp->intr_buff, INTBUFSIZE, intr_callback,
1877 			 tp, tp->intr_interval);
1878 
1879 	return 0;
1880 
1881 err1:
1882 	free_all_mem(tp);
1883 	return -ENOMEM;
1884 }
1885 
1886 static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp)
1887 {
1888 	struct tx_agg *agg = NULL;
1889 	unsigned long flags;
1890 
1891 	if (list_empty(&tp->tx_free))
1892 		return NULL;
1893 
1894 	spin_lock_irqsave(&tp->tx_lock, flags);
1895 	if (!list_empty(&tp->tx_free)) {
1896 		struct list_head *cursor;
1897 
1898 		cursor = tp->tx_free.next;
1899 		list_del_init(cursor);
1900 		agg = list_entry(cursor, struct tx_agg, list);
1901 	}
1902 	spin_unlock_irqrestore(&tp->tx_lock, flags);
1903 
1904 	return agg;
1905 }
1906 
1907 /* r8152_csum_workaround()
1908  * The hw limits the value of the transport offset. When the offset is out of
1909  * range, calculate the checksum by sw.
1910  */
1911 static void r8152_csum_workaround(struct r8152 *tp, struct sk_buff *skb,
1912 				  struct sk_buff_head *list)
1913 {
1914 	if (skb_shinfo(skb)->gso_size) {
1915 		netdev_features_t features = tp->netdev->features;
1916 		struct sk_buff *segs, *seg, *next;
1917 		struct sk_buff_head seg_list;
1918 
1919 		features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
1920 		segs = skb_gso_segment(skb, features);
1921 		if (IS_ERR(segs) || !segs)
1922 			goto drop;
1923 
1924 		__skb_queue_head_init(&seg_list);
1925 
1926 		skb_list_walk_safe(segs, seg, next) {
1927 			skb_mark_not_on_list(seg);
1928 			__skb_queue_tail(&seg_list, seg);
1929 		}
1930 
1931 		skb_queue_splice(&seg_list, list);
1932 		dev_kfree_skb(skb);
1933 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1934 		if (skb_checksum_help(skb) < 0)
1935 			goto drop;
1936 
1937 		__skb_queue_head(list, skb);
1938 	} else {
1939 		struct net_device_stats *stats;
1940 
1941 drop:
1942 		stats = &tp->netdev->stats;
1943 		stats->tx_dropped++;
1944 		dev_kfree_skb(skb);
1945 	}
1946 }
1947 
1948 /* msdn_giant_send_check()
1949  * According to the document of microsoft, the TCP Pseudo Header excludes the
1950  * packet length for IPv6 TCP large packets.
1951  */
1952 static int msdn_giant_send_check(struct sk_buff *skb)
1953 {
1954 	const struct ipv6hdr *ipv6h;
1955 	struct tcphdr *th;
1956 	int ret;
1957 
1958 	ret = skb_cow_head(skb, 0);
1959 	if (ret)
1960 		return ret;
1961 
1962 	ipv6h = ipv6_hdr(skb);
1963 	th = tcp_hdr(skb);
1964 
1965 	th->check = 0;
1966 	th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
1967 
1968 	return ret;
1969 }
1970 
1971 static inline void rtl_tx_vlan_tag(struct tx_desc *desc, struct sk_buff *skb)
1972 {
1973 	if (skb_vlan_tag_present(skb)) {
1974 		u32 opts2;
1975 
1976 		opts2 = TX_VLAN_TAG | swab16(skb_vlan_tag_get(skb));
1977 		desc->opts2 |= cpu_to_le32(opts2);
1978 	}
1979 }
1980 
1981 static inline void rtl_rx_vlan_tag(struct rx_desc *desc, struct sk_buff *skb)
1982 {
1983 	u32 opts2 = le32_to_cpu(desc->opts2);
1984 
1985 	if (opts2 & RX_VLAN_TAG)
1986 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1987 				       swab16(opts2 & 0xffff));
1988 }
1989 
1990 static int r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc,
1991 			 struct sk_buff *skb, u32 len, u32 transport_offset)
1992 {
1993 	u32 mss = skb_shinfo(skb)->gso_size;
1994 	u32 opts1, opts2 = 0;
1995 	int ret = TX_CSUM_SUCCESS;
1996 
1997 	WARN_ON_ONCE(len > TX_LEN_MAX);
1998 
1999 	opts1 = len | TX_FS | TX_LS;
2000 
2001 	if (mss) {
2002 		if (transport_offset > GTTCPHO_MAX) {
2003 			netif_warn(tp, tx_err, tp->netdev,
2004 				   "Invalid transport offset 0x%x for TSO\n",
2005 				   transport_offset);
2006 			ret = TX_CSUM_TSO;
2007 			goto unavailable;
2008 		}
2009 
2010 		switch (vlan_get_protocol(skb)) {
2011 		case htons(ETH_P_IP):
2012 			opts1 |= GTSENDV4;
2013 			break;
2014 
2015 		case htons(ETH_P_IPV6):
2016 			if (msdn_giant_send_check(skb)) {
2017 				ret = TX_CSUM_TSO;
2018 				goto unavailable;
2019 			}
2020 			opts1 |= GTSENDV6;
2021 			break;
2022 
2023 		default:
2024 			WARN_ON_ONCE(1);
2025 			break;
2026 		}
2027 
2028 		opts1 |= transport_offset << GTTCPHO_SHIFT;
2029 		opts2 |= min(mss, MSS_MAX) << MSS_SHIFT;
2030 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
2031 		u8 ip_protocol;
2032 
2033 		if (transport_offset > TCPHO_MAX) {
2034 			netif_warn(tp, tx_err, tp->netdev,
2035 				   "Invalid transport offset 0x%x\n",
2036 				   transport_offset);
2037 			ret = TX_CSUM_NONE;
2038 			goto unavailable;
2039 		}
2040 
2041 		switch (vlan_get_protocol(skb)) {
2042 		case htons(ETH_P_IP):
2043 			opts2 |= IPV4_CS;
2044 			ip_protocol = ip_hdr(skb)->protocol;
2045 			break;
2046 
2047 		case htons(ETH_P_IPV6):
2048 			opts2 |= IPV6_CS;
2049 			ip_protocol = ipv6_hdr(skb)->nexthdr;
2050 			break;
2051 
2052 		default:
2053 			ip_protocol = IPPROTO_RAW;
2054 			break;
2055 		}
2056 
2057 		if (ip_protocol == IPPROTO_TCP)
2058 			opts2 |= TCP_CS;
2059 		else if (ip_protocol == IPPROTO_UDP)
2060 			opts2 |= UDP_CS;
2061 		else
2062 			WARN_ON_ONCE(1);
2063 
2064 		opts2 |= transport_offset << TCPHO_SHIFT;
2065 	}
2066 
2067 	desc->opts2 = cpu_to_le32(opts2);
2068 	desc->opts1 = cpu_to_le32(opts1);
2069 
2070 unavailable:
2071 	return ret;
2072 }
2073 
2074 static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg)
2075 {
2076 	struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
2077 	int remain, ret;
2078 	u8 *tx_data;
2079 
2080 	__skb_queue_head_init(&skb_head);
2081 	spin_lock(&tx_queue->lock);
2082 	skb_queue_splice_init(tx_queue, &skb_head);
2083 	spin_unlock(&tx_queue->lock);
2084 
2085 	tx_data = agg->head;
2086 	agg->skb_num = 0;
2087 	agg->skb_len = 0;
2088 	remain = agg_buf_sz;
2089 
2090 	while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) {
2091 		struct tx_desc *tx_desc;
2092 		struct sk_buff *skb;
2093 		unsigned int len;
2094 		u32 offset;
2095 
2096 		skb = __skb_dequeue(&skb_head);
2097 		if (!skb)
2098 			break;
2099 
2100 		len = skb->len + sizeof(*tx_desc);
2101 
2102 		if (len > remain) {
2103 			__skb_queue_head(&skb_head, skb);
2104 			break;
2105 		}
2106 
2107 		tx_data = tx_agg_align(tx_data);
2108 		tx_desc = (struct tx_desc *)tx_data;
2109 
2110 		offset = (u32)skb_transport_offset(skb);
2111 
2112 		if (r8152_tx_csum(tp, tx_desc, skb, skb->len, offset)) {
2113 			r8152_csum_workaround(tp, skb, &skb_head);
2114 			continue;
2115 		}
2116 
2117 		rtl_tx_vlan_tag(tx_desc, skb);
2118 
2119 		tx_data += sizeof(*tx_desc);
2120 
2121 		len = skb->len;
2122 		if (skb_copy_bits(skb, 0, tx_data, len) < 0) {
2123 			struct net_device_stats *stats = &tp->netdev->stats;
2124 
2125 			stats->tx_dropped++;
2126 			dev_kfree_skb_any(skb);
2127 			tx_data -= sizeof(*tx_desc);
2128 			continue;
2129 		}
2130 
2131 		tx_data += len;
2132 		agg->skb_len += len;
2133 		agg->skb_num += skb_shinfo(skb)->gso_segs ?: 1;
2134 
2135 		dev_kfree_skb_any(skb);
2136 
2137 		remain = agg_buf_sz - (int)(tx_agg_align(tx_data) - agg->head);
2138 
2139 		if (test_bit(DELL_TB_RX_AGG_BUG, &tp->flags))
2140 			break;
2141 	}
2142 
2143 	if (!skb_queue_empty(&skb_head)) {
2144 		spin_lock(&tx_queue->lock);
2145 		skb_queue_splice(&skb_head, tx_queue);
2146 		spin_unlock(&tx_queue->lock);
2147 	}
2148 
2149 	netif_tx_lock(tp->netdev);
2150 
2151 	if (netif_queue_stopped(tp->netdev) &&
2152 	    skb_queue_len(&tp->tx_queue) < tp->tx_qlen)
2153 		netif_wake_queue(tp->netdev);
2154 
2155 	netif_tx_unlock(tp->netdev);
2156 
2157 	ret = usb_autopm_get_interface_async(tp->intf);
2158 	if (ret < 0)
2159 		goto out_tx_fill;
2160 
2161 	usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2),
2162 			  agg->head, (int)(tx_data - (u8 *)agg->head),
2163 			  (usb_complete_t)write_bulk_callback, agg);
2164 
2165 	ret = usb_submit_urb(agg->urb, GFP_ATOMIC);
2166 	if (ret < 0)
2167 		usb_autopm_put_interface_async(tp->intf);
2168 
2169 out_tx_fill:
2170 	return ret;
2171 }
2172 
2173 static u8 r8152_rx_csum(struct r8152 *tp, struct rx_desc *rx_desc)
2174 {
2175 	u8 checksum = CHECKSUM_NONE;
2176 	u32 opts2, opts3;
2177 
2178 	if (!(tp->netdev->features & NETIF_F_RXCSUM))
2179 		goto return_result;
2180 
2181 	opts2 = le32_to_cpu(rx_desc->opts2);
2182 	opts3 = le32_to_cpu(rx_desc->opts3);
2183 
2184 	if (opts2 & RD_IPV4_CS) {
2185 		if (opts3 & IPF)
2186 			checksum = CHECKSUM_NONE;
2187 		else if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
2188 			checksum = CHECKSUM_UNNECESSARY;
2189 		else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
2190 			checksum = CHECKSUM_UNNECESSARY;
2191 	} else if (opts2 & RD_IPV6_CS) {
2192 		if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
2193 			checksum = CHECKSUM_UNNECESSARY;
2194 		else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
2195 			checksum = CHECKSUM_UNNECESSARY;
2196 	}
2197 
2198 return_result:
2199 	return checksum;
2200 }
2201 
2202 static inline bool rx_count_exceed(struct r8152 *tp)
2203 {
2204 	return atomic_read(&tp->rx_count) > RTL8152_MAX_RX;
2205 }
2206 
2207 static inline int agg_offset(struct rx_agg *agg, void *addr)
2208 {
2209 	return (int)(addr - agg->buffer);
2210 }
2211 
2212 static struct rx_agg *rtl_get_free_rx(struct r8152 *tp, gfp_t mflags)
2213 {
2214 	struct rx_agg *agg, *agg_next, *agg_free = NULL;
2215 	unsigned long flags;
2216 
2217 	spin_lock_irqsave(&tp->rx_lock, flags);
2218 
2219 	list_for_each_entry_safe(agg, agg_next, &tp->rx_used, list) {
2220 		if (page_count(agg->page) == 1) {
2221 			if (!agg_free) {
2222 				list_del_init(&agg->list);
2223 				agg_free = agg;
2224 				continue;
2225 			}
2226 			if (rx_count_exceed(tp)) {
2227 				list_del_init(&agg->list);
2228 				free_rx_agg(tp, agg);
2229 			}
2230 			break;
2231 		}
2232 	}
2233 
2234 	spin_unlock_irqrestore(&tp->rx_lock, flags);
2235 
2236 	if (!agg_free && atomic_read(&tp->rx_count) < tp->rx_pending)
2237 		agg_free = alloc_rx_agg(tp, mflags);
2238 
2239 	return agg_free;
2240 }
2241 
2242 static int rx_bottom(struct r8152 *tp, int budget)
2243 {
2244 	unsigned long flags;
2245 	struct list_head *cursor, *next, rx_queue;
2246 	int ret = 0, work_done = 0;
2247 	struct napi_struct *napi = &tp->napi;
2248 
2249 	if (!skb_queue_empty(&tp->rx_queue)) {
2250 		while (work_done < budget) {
2251 			struct sk_buff *skb = __skb_dequeue(&tp->rx_queue);
2252 			struct net_device *netdev = tp->netdev;
2253 			struct net_device_stats *stats = &netdev->stats;
2254 			unsigned int pkt_len;
2255 
2256 			if (!skb)
2257 				break;
2258 
2259 			pkt_len = skb->len;
2260 			napi_gro_receive(napi, skb);
2261 			work_done++;
2262 			stats->rx_packets++;
2263 			stats->rx_bytes += pkt_len;
2264 		}
2265 	}
2266 
2267 	if (list_empty(&tp->rx_done))
2268 		goto out1;
2269 
2270 	INIT_LIST_HEAD(&rx_queue);
2271 	spin_lock_irqsave(&tp->rx_lock, flags);
2272 	list_splice_init(&tp->rx_done, &rx_queue);
2273 	spin_unlock_irqrestore(&tp->rx_lock, flags);
2274 
2275 	list_for_each_safe(cursor, next, &rx_queue) {
2276 		struct rx_desc *rx_desc;
2277 		struct rx_agg *agg, *agg_free;
2278 		int len_used = 0;
2279 		struct urb *urb;
2280 		u8 *rx_data;
2281 
2282 		list_del_init(cursor);
2283 
2284 		agg = list_entry(cursor, struct rx_agg, list);
2285 		urb = agg->urb;
2286 		if (urb->actual_length < ETH_ZLEN)
2287 			goto submit;
2288 
2289 		agg_free = rtl_get_free_rx(tp, GFP_ATOMIC);
2290 
2291 		rx_desc = agg->buffer;
2292 		rx_data = agg->buffer;
2293 		len_used += sizeof(struct rx_desc);
2294 
2295 		while (urb->actual_length > len_used) {
2296 			struct net_device *netdev = tp->netdev;
2297 			struct net_device_stats *stats = &netdev->stats;
2298 			unsigned int pkt_len, rx_frag_head_sz;
2299 			struct sk_buff *skb;
2300 
2301 			/* limite the skb numbers for rx_queue */
2302 			if (unlikely(skb_queue_len(&tp->rx_queue) >= 1000))
2303 				break;
2304 
2305 			pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
2306 			if (pkt_len < ETH_ZLEN)
2307 				break;
2308 
2309 			len_used += pkt_len;
2310 			if (urb->actual_length < len_used)
2311 				break;
2312 
2313 			pkt_len -= ETH_FCS_LEN;
2314 			rx_data += sizeof(struct rx_desc);
2315 
2316 			if (!agg_free || tp->rx_copybreak > pkt_len)
2317 				rx_frag_head_sz = pkt_len;
2318 			else
2319 				rx_frag_head_sz = tp->rx_copybreak;
2320 
2321 			skb = napi_alloc_skb(napi, rx_frag_head_sz);
2322 			if (!skb) {
2323 				stats->rx_dropped++;
2324 				goto find_next_rx;
2325 			}
2326 
2327 			skb->ip_summed = r8152_rx_csum(tp, rx_desc);
2328 			memcpy(skb->data, rx_data, rx_frag_head_sz);
2329 			skb_put(skb, rx_frag_head_sz);
2330 			pkt_len -= rx_frag_head_sz;
2331 			rx_data += rx_frag_head_sz;
2332 			if (pkt_len) {
2333 				skb_add_rx_frag(skb, 0, agg->page,
2334 						agg_offset(agg, rx_data),
2335 						pkt_len,
2336 						SKB_DATA_ALIGN(pkt_len));
2337 				get_page(agg->page);
2338 			}
2339 
2340 			skb->protocol = eth_type_trans(skb, netdev);
2341 			rtl_rx_vlan_tag(rx_desc, skb);
2342 			if (work_done < budget) {
2343 				work_done++;
2344 				stats->rx_packets++;
2345 				stats->rx_bytes += skb->len;
2346 				napi_gro_receive(napi, skb);
2347 			} else {
2348 				__skb_queue_tail(&tp->rx_queue, skb);
2349 			}
2350 
2351 find_next_rx:
2352 			rx_data = rx_agg_align(rx_data + pkt_len + ETH_FCS_LEN);
2353 			rx_desc = (struct rx_desc *)rx_data;
2354 			len_used = agg_offset(agg, rx_data);
2355 			len_used += sizeof(struct rx_desc);
2356 		}
2357 
2358 		WARN_ON(!agg_free && page_count(agg->page) > 1);
2359 
2360 		if (agg_free) {
2361 			spin_lock_irqsave(&tp->rx_lock, flags);
2362 			if (page_count(agg->page) == 1) {
2363 				list_add(&agg_free->list, &tp->rx_used);
2364 			} else {
2365 				list_add_tail(&agg->list, &tp->rx_used);
2366 				agg = agg_free;
2367 				urb = agg->urb;
2368 			}
2369 			spin_unlock_irqrestore(&tp->rx_lock, flags);
2370 		}
2371 
2372 submit:
2373 		if (!ret) {
2374 			ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
2375 		} else {
2376 			urb->actual_length = 0;
2377 			list_add_tail(&agg->list, next);
2378 		}
2379 	}
2380 
2381 	if (!list_empty(&rx_queue)) {
2382 		spin_lock_irqsave(&tp->rx_lock, flags);
2383 		list_splice_tail(&rx_queue, &tp->rx_done);
2384 		spin_unlock_irqrestore(&tp->rx_lock, flags);
2385 	}
2386 
2387 out1:
2388 	return work_done;
2389 }
2390 
2391 static void tx_bottom(struct r8152 *tp)
2392 {
2393 	int res;
2394 
2395 	do {
2396 		struct net_device *netdev = tp->netdev;
2397 		struct tx_agg *agg;
2398 
2399 		if (skb_queue_empty(&tp->tx_queue))
2400 			break;
2401 
2402 		agg = r8152_get_tx_agg(tp);
2403 		if (!agg)
2404 			break;
2405 
2406 		res = r8152_tx_agg_fill(tp, agg);
2407 		if (!res)
2408 			continue;
2409 
2410 		if (res == -ENODEV) {
2411 			rtl_set_unplug(tp);
2412 			netif_device_detach(netdev);
2413 		} else {
2414 			struct net_device_stats *stats = &netdev->stats;
2415 			unsigned long flags;
2416 
2417 			netif_warn(tp, tx_err, netdev,
2418 				   "failed tx_urb %d\n", res);
2419 			stats->tx_dropped += agg->skb_num;
2420 
2421 			spin_lock_irqsave(&tp->tx_lock, flags);
2422 			list_add_tail(&agg->list, &tp->tx_free);
2423 			spin_unlock_irqrestore(&tp->tx_lock, flags);
2424 		}
2425 	} while (res == 0);
2426 }
2427 
2428 static void bottom_half(unsigned long data)
2429 {
2430 	struct r8152 *tp;
2431 
2432 	tp = (struct r8152 *)data;
2433 
2434 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
2435 		return;
2436 
2437 	if (!test_bit(WORK_ENABLE, &tp->flags))
2438 		return;
2439 
2440 	/* When link down, the driver would cancel all bulks. */
2441 	/* This avoid the re-submitting bulk */
2442 	if (!netif_carrier_ok(tp->netdev))
2443 		return;
2444 
2445 	clear_bit(SCHEDULE_TASKLET, &tp->flags);
2446 
2447 	tx_bottom(tp);
2448 }
2449 
2450 static int r8152_poll(struct napi_struct *napi, int budget)
2451 {
2452 	struct r8152 *tp = container_of(napi, struct r8152, napi);
2453 	int work_done;
2454 
2455 	work_done = rx_bottom(tp, budget);
2456 
2457 	if (work_done < budget) {
2458 		if (!napi_complete_done(napi, work_done))
2459 			goto out;
2460 		if (!list_empty(&tp->rx_done))
2461 			napi_schedule(napi);
2462 	}
2463 
2464 out:
2465 	return work_done;
2466 }
2467 
2468 static
2469 int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
2470 {
2471 	int ret;
2472 
2473 	/* The rx would be stopped, so skip submitting */
2474 	if (test_bit(RTL8152_UNPLUG, &tp->flags) ||
2475 	    !test_bit(WORK_ENABLE, &tp->flags) || !netif_carrier_ok(tp->netdev))
2476 		return 0;
2477 
2478 	usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
2479 			  agg->buffer, tp->rx_buf_sz,
2480 			  (usb_complete_t)read_bulk_callback, agg);
2481 
2482 	ret = usb_submit_urb(agg->urb, mem_flags);
2483 	if (ret == -ENODEV) {
2484 		rtl_set_unplug(tp);
2485 		netif_device_detach(tp->netdev);
2486 	} else if (ret) {
2487 		struct urb *urb = agg->urb;
2488 		unsigned long flags;
2489 
2490 		urb->actual_length = 0;
2491 		spin_lock_irqsave(&tp->rx_lock, flags);
2492 		list_add_tail(&agg->list, &tp->rx_done);
2493 		spin_unlock_irqrestore(&tp->rx_lock, flags);
2494 
2495 		netif_err(tp, rx_err, tp->netdev,
2496 			  "Couldn't submit rx[%p], ret = %d\n", agg, ret);
2497 
2498 		napi_schedule(&tp->napi);
2499 	}
2500 
2501 	return ret;
2502 }
2503 
2504 static void rtl_drop_queued_tx(struct r8152 *tp)
2505 {
2506 	struct net_device_stats *stats = &tp->netdev->stats;
2507 	struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
2508 	struct sk_buff *skb;
2509 
2510 	if (skb_queue_empty(tx_queue))
2511 		return;
2512 
2513 	__skb_queue_head_init(&skb_head);
2514 	spin_lock_bh(&tx_queue->lock);
2515 	skb_queue_splice_init(tx_queue, &skb_head);
2516 	spin_unlock_bh(&tx_queue->lock);
2517 
2518 	while ((skb = __skb_dequeue(&skb_head))) {
2519 		dev_kfree_skb(skb);
2520 		stats->tx_dropped++;
2521 	}
2522 }
2523 
2524 static void rtl8152_tx_timeout(struct net_device *netdev, unsigned int txqueue)
2525 {
2526 	struct r8152 *tp = netdev_priv(netdev);
2527 
2528 	netif_warn(tp, tx_err, netdev, "Tx timeout\n");
2529 
2530 	usb_queue_reset_device(tp->intf);
2531 }
2532 
2533 static void rtl8152_set_rx_mode(struct net_device *netdev)
2534 {
2535 	struct r8152 *tp = netdev_priv(netdev);
2536 
2537 	if (netif_carrier_ok(netdev)) {
2538 		set_bit(RTL8152_SET_RX_MODE, &tp->flags);
2539 		schedule_delayed_work(&tp->schedule, 0);
2540 	}
2541 }
2542 
2543 static void _rtl8152_set_rx_mode(struct net_device *netdev)
2544 {
2545 	struct r8152 *tp = netdev_priv(netdev);
2546 	u32 mc_filter[2];	/* Multicast hash filter */
2547 	__le32 tmp[2];
2548 	u32 ocp_data;
2549 
2550 	netif_stop_queue(netdev);
2551 	ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2552 	ocp_data &= ~RCR_ACPT_ALL;
2553 	ocp_data |= RCR_AB | RCR_APM;
2554 
2555 	if (netdev->flags & IFF_PROMISC) {
2556 		/* Unconditionally log net taps. */
2557 		netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
2558 		ocp_data |= RCR_AM | RCR_AAP;
2559 		mc_filter[1] = 0xffffffff;
2560 		mc_filter[0] = 0xffffffff;
2561 	} else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
2562 		   (netdev->flags & IFF_ALLMULTI)) {
2563 		/* Too many to filter perfectly -- accept all multicasts. */
2564 		ocp_data |= RCR_AM;
2565 		mc_filter[1] = 0xffffffff;
2566 		mc_filter[0] = 0xffffffff;
2567 	} else {
2568 		struct netdev_hw_addr *ha;
2569 
2570 		mc_filter[1] = 0;
2571 		mc_filter[0] = 0;
2572 		netdev_for_each_mc_addr(ha, netdev) {
2573 			int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
2574 
2575 			mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
2576 			ocp_data |= RCR_AM;
2577 		}
2578 	}
2579 
2580 	tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
2581 	tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
2582 
2583 	pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
2584 	ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2585 	netif_wake_queue(netdev);
2586 }
2587 
2588 static netdev_features_t
2589 rtl8152_features_check(struct sk_buff *skb, struct net_device *dev,
2590 		       netdev_features_t features)
2591 {
2592 	u32 mss = skb_shinfo(skb)->gso_size;
2593 	int max_offset = mss ? GTTCPHO_MAX : TCPHO_MAX;
2594 	int offset = skb_transport_offset(skb);
2595 
2596 	if ((mss || skb->ip_summed == CHECKSUM_PARTIAL) && offset > max_offset)
2597 		features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2598 	else if ((skb->len + sizeof(struct tx_desc)) > agg_buf_sz)
2599 		features &= ~NETIF_F_GSO_MASK;
2600 
2601 	return features;
2602 }
2603 
2604 static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
2605 				      struct net_device *netdev)
2606 {
2607 	struct r8152 *tp = netdev_priv(netdev);
2608 
2609 	skb_tx_timestamp(skb);
2610 
2611 	skb_queue_tail(&tp->tx_queue, skb);
2612 
2613 	if (!list_empty(&tp->tx_free)) {
2614 		if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
2615 			set_bit(SCHEDULE_TASKLET, &tp->flags);
2616 			schedule_delayed_work(&tp->schedule, 0);
2617 		} else {
2618 			usb_mark_last_busy(tp->udev);
2619 			tasklet_schedule(&tp->tx_tl);
2620 		}
2621 	} else if (skb_queue_len(&tp->tx_queue) > tp->tx_qlen) {
2622 		netif_stop_queue(netdev);
2623 	}
2624 
2625 	return NETDEV_TX_OK;
2626 }
2627 
2628 static void r8152b_reset_packet_filter(struct r8152 *tp)
2629 {
2630 	u32	ocp_data;
2631 
2632 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
2633 	ocp_data &= ~FMC_FCR_MCU_EN;
2634 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
2635 	ocp_data |= FMC_FCR_MCU_EN;
2636 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
2637 }
2638 
2639 static void rtl8152_nic_reset(struct r8152 *tp)
2640 {
2641 	int	i;
2642 
2643 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
2644 
2645 	for (i = 0; i < 1000; i++) {
2646 		if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
2647 			break;
2648 		usleep_range(100, 400);
2649 	}
2650 }
2651 
2652 static void set_tx_qlen(struct r8152 *tp)
2653 {
2654 	struct net_device *netdev = tp->netdev;
2655 
2656 	tp->tx_qlen = agg_buf_sz / (netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN +
2657 				    sizeof(struct tx_desc));
2658 }
2659 
2660 static inline u8 rtl8152_get_speed(struct r8152 *tp)
2661 {
2662 	return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
2663 }
2664 
2665 static void rtl_set_eee_plus(struct r8152 *tp)
2666 {
2667 	u32 ocp_data;
2668 	u8 speed;
2669 
2670 	speed = rtl8152_get_speed(tp);
2671 	if (speed & _10bps) {
2672 		ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
2673 		ocp_data |= EEEP_CR_EEEP_TX;
2674 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
2675 	} else {
2676 		ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
2677 		ocp_data &= ~EEEP_CR_EEEP_TX;
2678 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
2679 	}
2680 }
2681 
2682 static void rxdy_gated_en(struct r8152 *tp, bool enable)
2683 {
2684 	u32 ocp_data;
2685 
2686 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
2687 	if (enable)
2688 		ocp_data |= RXDY_GATED_EN;
2689 	else
2690 		ocp_data &= ~RXDY_GATED_EN;
2691 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
2692 }
2693 
2694 static int rtl_start_rx(struct r8152 *tp)
2695 {
2696 	struct rx_agg *agg, *agg_next;
2697 	struct list_head tmp_list;
2698 	unsigned long flags;
2699 	int ret = 0, i = 0;
2700 
2701 	INIT_LIST_HEAD(&tmp_list);
2702 
2703 	spin_lock_irqsave(&tp->rx_lock, flags);
2704 
2705 	INIT_LIST_HEAD(&tp->rx_done);
2706 	INIT_LIST_HEAD(&tp->rx_used);
2707 
2708 	list_splice_init(&tp->rx_info, &tmp_list);
2709 
2710 	spin_unlock_irqrestore(&tp->rx_lock, flags);
2711 
2712 	list_for_each_entry_safe(agg, agg_next, &tmp_list, info_list) {
2713 		INIT_LIST_HEAD(&agg->list);
2714 
2715 		/* Only RTL8152_MAX_RX rx_agg need to be submitted. */
2716 		if (++i > RTL8152_MAX_RX) {
2717 			spin_lock_irqsave(&tp->rx_lock, flags);
2718 			list_add_tail(&agg->list, &tp->rx_used);
2719 			spin_unlock_irqrestore(&tp->rx_lock, flags);
2720 		} else if (unlikely(ret < 0)) {
2721 			spin_lock_irqsave(&tp->rx_lock, flags);
2722 			list_add_tail(&agg->list, &tp->rx_done);
2723 			spin_unlock_irqrestore(&tp->rx_lock, flags);
2724 		} else {
2725 			ret = r8152_submit_rx(tp, agg, GFP_KERNEL);
2726 		}
2727 	}
2728 
2729 	spin_lock_irqsave(&tp->rx_lock, flags);
2730 	WARN_ON(!list_empty(&tp->rx_info));
2731 	list_splice(&tmp_list, &tp->rx_info);
2732 	spin_unlock_irqrestore(&tp->rx_lock, flags);
2733 
2734 	return ret;
2735 }
2736 
2737 static int rtl_stop_rx(struct r8152 *tp)
2738 {
2739 	struct rx_agg *agg, *agg_next;
2740 	struct list_head tmp_list;
2741 	unsigned long flags;
2742 
2743 	INIT_LIST_HEAD(&tmp_list);
2744 
2745 	/* The usb_kill_urb() couldn't be used in atomic.
2746 	 * Therefore, move the list of rx_info to a tmp one.
2747 	 * Then, list_for_each_entry_safe could be used without
2748 	 * spin lock.
2749 	 */
2750 
2751 	spin_lock_irqsave(&tp->rx_lock, flags);
2752 	list_splice_init(&tp->rx_info, &tmp_list);
2753 	spin_unlock_irqrestore(&tp->rx_lock, flags);
2754 
2755 	list_for_each_entry_safe(agg, agg_next, &tmp_list, info_list) {
2756 		/* At least RTL8152_MAX_RX rx_agg have the page_count being
2757 		 * equal to 1, so the other ones could be freed safely.
2758 		 */
2759 		if (page_count(agg->page) > 1)
2760 			free_rx_agg(tp, agg);
2761 		else
2762 			usb_kill_urb(agg->urb);
2763 	}
2764 
2765 	/* Move back the list of temp to the rx_info */
2766 	spin_lock_irqsave(&tp->rx_lock, flags);
2767 	WARN_ON(!list_empty(&tp->rx_info));
2768 	list_splice(&tmp_list, &tp->rx_info);
2769 	spin_unlock_irqrestore(&tp->rx_lock, flags);
2770 
2771 	while (!skb_queue_empty(&tp->rx_queue))
2772 		dev_kfree_skb(__skb_dequeue(&tp->rx_queue));
2773 
2774 	return 0;
2775 }
2776 
2777 static inline void r8153b_rx_agg_chg_indicate(struct r8152 *tp)
2778 {
2779 	ocp_write_byte(tp, MCU_TYPE_USB, USB_UPT_RXDMA_OWN,
2780 		       OWN_UPDATE | OWN_CLEAR);
2781 }
2782 
2783 static int rtl_enable(struct r8152 *tp)
2784 {
2785 	u32 ocp_data;
2786 
2787 	r8152b_reset_packet_filter(tp);
2788 
2789 	ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
2790 	ocp_data |= CR_RE | CR_TE;
2791 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
2792 
2793 	switch (tp->version) {
2794 	case RTL_VER_08:
2795 	case RTL_VER_09:
2796 		r8153b_rx_agg_chg_indicate(tp);
2797 		break;
2798 	default:
2799 		break;
2800 	}
2801 
2802 	rxdy_gated_en(tp, false);
2803 
2804 	return 0;
2805 }
2806 
2807 static int rtl8152_enable(struct r8152 *tp)
2808 {
2809 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
2810 		return -ENODEV;
2811 
2812 	set_tx_qlen(tp);
2813 	rtl_set_eee_plus(tp);
2814 
2815 	return rtl_enable(tp);
2816 }
2817 
2818 static void r8153_set_rx_early_timeout(struct r8152 *tp)
2819 {
2820 	u32 ocp_data = tp->coalesce / 8;
2821 
2822 	switch (tp->version) {
2823 	case RTL_VER_03:
2824 	case RTL_VER_04:
2825 	case RTL_VER_05:
2826 	case RTL_VER_06:
2827 		ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
2828 			       ocp_data);
2829 		break;
2830 
2831 	case RTL_VER_08:
2832 	case RTL_VER_09:
2833 		/* The RTL8153B uses USB_RX_EXTRA_AGGR_TMR for rx timeout
2834 		 * primarily. For USB_RX_EARLY_TIMEOUT, we fix it to 128ns.
2835 		 */
2836 		ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
2837 			       128 / 8);
2838 		ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EXTRA_AGGR_TMR,
2839 			       ocp_data);
2840 		break;
2841 
2842 	default:
2843 		break;
2844 	}
2845 }
2846 
2847 static void r8153_set_rx_early_size(struct r8152 *tp)
2848 {
2849 	u32 ocp_data = tp->rx_buf_sz - rx_reserved_size(tp->netdev->mtu);
2850 
2851 	switch (tp->version) {
2852 	case RTL_VER_03:
2853 	case RTL_VER_04:
2854 	case RTL_VER_05:
2855 	case RTL_VER_06:
2856 		ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
2857 			       ocp_data / 4);
2858 		break;
2859 	case RTL_VER_08:
2860 	case RTL_VER_09:
2861 		ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
2862 			       ocp_data / 8);
2863 		break;
2864 	default:
2865 		WARN_ON_ONCE(1);
2866 		break;
2867 	}
2868 }
2869 
2870 static int rtl8153_enable(struct r8152 *tp)
2871 {
2872 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
2873 		return -ENODEV;
2874 
2875 	set_tx_qlen(tp);
2876 	rtl_set_eee_plus(tp);
2877 	r8153_set_rx_early_timeout(tp);
2878 	r8153_set_rx_early_size(tp);
2879 
2880 	if (tp->version == RTL_VER_09) {
2881 		u32 ocp_data;
2882 
2883 		ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK);
2884 		ocp_data &= ~FC_PATCH_TASK;
2885 		ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
2886 		usleep_range(1000, 2000);
2887 		ocp_data |= FC_PATCH_TASK;
2888 		ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
2889 	}
2890 
2891 	return rtl_enable(tp);
2892 }
2893 
2894 static void rtl_disable(struct r8152 *tp)
2895 {
2896 	u32 ocp_data;
2897 	int i;
2898 
2899 	if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
2900 		rtl_drop_queued_tx(tp);
2901 		return;
2902 	}
2903 
2904 	ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2905 	ocp_data &= ~RCR_ACPT_ALL;
2906 	ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2907 
2908 	rtl_drop_queued_tx(tp);
2909 
2910 	for (i = 0; i < RTL8152_MAX_TX; i++)
2911 		usb_kill_urb(tp->tx_info[i].urb);
2912 
2913 	rxdy_gated_en(tp, true);
2914 
2915 	for (i = 0; i < 1000; i++) {
2916 		ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2917 		if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
2918 			break;
2919 		usleep_range(1000, 2000);
2920 	}
2921 
2922 	for (i = 0; i < 1000; i++) {
2923 		if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
2924 			break;
2925 		usleep_range(1000, 2000);
2926 	}
2927 
2928 	rtl_stop_rx(tp);
2929 
2930 	rtl8152_nic_reset(tp);
2931 }
2932 
2933 static void r8152_power_cut_en(struct r8152 *tp, bool enable)
2934 {
2935 	u32 ocp_data;
2936 
2937 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
2938 	if (enable)
2939 		ocp_data |= POWER_CUT;
2940 	else
2941 		ocp_data &= ~POWER_CUT;
2942 	ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
2943 
2944 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
2945 	ocp_data &= ~RESUME_INDICATE;
2946 	ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
2947 }
2948 
2949 static void rtl_rx_vlan_en(struct r8152 *tp, bool enable)
2950 {
2951 	u32 ocp_data;
2952 
2953 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2954 	if (enable)
2955 		ocp_data |= CPCR_RX_VLAN;
2956 	else
2957 		ocp_data &= ~CPCR_RX_VLAN;
2958 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2959 }
2960 
2961 static int rtl8152_set_features(struct net_device *dev,
2962 				netdev_features_t features)
2963 {
2964 	netdev_features_t changed = features ^ dev->features;
2965 	struct r8152 *tp = netdev_priv(dev);
2966 	int ret;
2967 
2968 	ret = usb_autopm_get_interface(tp->intf);
2969 	if (ret < 0)
2970 		goto out;
2971 
2972 	mutex_lock(&tp->control);
2973 
2974 	if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
2975 		if (features & NETIF_F_HW_VLAN_CTAG_RX)
2976 			rtl_rx_vlan_en(tp, true);
2977 		else
2978 			rtl_rx_vlan_en(tp, false);
2979 	}
2980 
2981 	mutex_unlock(&tp->control);
2982 
2983 	usb_autopm_put_interface(tp->intf);
2984 
2985 out:
2986 	return ret;
2987 }
2988 
2989 #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
2990 
2991 static u32 __rtl_get_wol(struct r8152 *tp)
2992 {
2993 	u32 ocp_data;
2994 	u32 wolopts = 0;
2995 
2996 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2997 	if (ocp_data & LINK_ON_WAKE_EN)
2998 		wolopts |= WAKE_PHY;
2999 
3000 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
3001 	if (ocp_data & UWF_EN)
3002 		wolopts |= WAKE_UCAST;
3003 	if (ocp_data & BWF_EN)
3004 		wolopts |= WAKE_BCAST;
3005 	if (ocp_data & MWF_EN)
3006 		wolopts |= WAKE_MCAST;
3007 
3008 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
3009 	if (ocp_data & MAGIC_EN)
3010 		wolopts |= WAKE_MAGIC;
3011 
3012 	return wolopts;
3013 }
3014 
3015 static void __rtl_set_wol(struct r8152 *tp, u32 wolopts)
3016 {
3017 	u32 ocp_data;
3018 
3019 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
3020 
3021 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
3022 	ocp_data &= ~LINK_ON_WAKE_EN;
3023 	if (wolopts & WAKE_PHY)
3024 		ocp_data |= LINK_ON_WAKE_EN;
3025 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
3026 
3027 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
3028 	ocp_data &= ~(UWF_EN | BWF_EN | MWF_EN);
3029 	if (wolopts & WAKE_UCAST)
3030 		ocp_data |= UWF_EN;
3031 	if (wolopts & WAKE_BCAST)
3032 		ocp_data |= BWF_EN;
3033 	if (wolopts & WAKE_MCAST)
3034 		ocp_data |= MWF_EN;
3035 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data);
3036 
3037 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
3038 
3039 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
3040 	ocp_data &= ~MAGIC_EN;
3041 	if (wolopts & WAKE_MAGIC)
3042 		ocp_data |= MAGIC_EN;
3043 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
3044 
3045 	if (wolopts & WAKE_ANY)
3046 		device_set_wakeup_enable(&tp->udev->dev, true);
3047 	else
3048 		device_set_wakeup_enable(&tp->udev->dev, false);
3049 }
3050 
3051 static void r8153_mac_clk_spd(struct r8152 *tp, bool enable)
3052 {
3053 	/* MAC clock speed down */
3054 	if (enable) {
3055 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL,
3056 			       ALDPS_SPDWN_RATIO);
3057 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2,
3058 			       EEE_SPDWN_RATIO);
3059 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3,
3060 			       PKT_AVAIL_SPDWN_EN | SUSPEND_SPDWN_EN |
3061 			       U1U2_SPDWN_EN | L1_SPDWN_EN);
3062 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4,
3063 			       PWRSAVE_SPDWN_EN | RXDV_SPDWN_EN | TX10MIDLE_EN |
3064 			       TP100_SPDWN_EN | TP500_SPDWN_EN | EEE_SPDWN_EN |
3065 			       TP1000_SPDWN_EN);
3066 	} else {
3067 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, 0);
3068 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, 0);
3069 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, 0);
3070 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, 0);
3071 	}
3072 }
3073 
3074 static void r8153_u1u2en(struct r8152 *tp, bool enable)
3075 {
3076 	u8 u1u2[8];
3077 
3078 	if (enable)
3079 		memset(u1u2, 0xff, sizeof(u1u2));
3080 	else
3081 		memset(u1u2, 0x00, sizeof(u1u2));
3082 
3083 	usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2);
3084 }
3085 
3086 static void r8153b_u1u2en(struct r8152 *tp, bool enable)
3087 {
3088 	u32 ocp_data;
3089 
3090 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG);
3091 	if (enable)
3092 		ocp_data |= LPM_U1U2_EN;
3093 	else
3094 		ocp_data &= ~LPM_U1U2_EN;
3095 
3096 	ocp_write_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG, ocp_data);
3097 }
3098 
3099 static void r8153_u2p3en(struct r8152 *tp, bool enable)
3100 {
3101 	u32 ocp_data;
3102 
3103 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
3104 	if (enable)
3105 		ocp_data |= U2P3_ENABLE;
3106 	else
3107 		ocp_data &= ~U2P3_ENABLE;
3108 	ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
3109 }
3110 
3111 static void r8153b_ups_flags(struct r8152 *tp)
3112 {
3113 	u32 ups_flags = 0;
3114 
3115 	if (tp->ups_info.green)
3116 		ups_flags |= UPS_FLAGS_EN_GREEN;
3117 
3118 	if (tp->ups_info.aldps)
3119 		ups_flags |= UPS_FLAGS_EN_ALDPS;
3120 
3121 	if (tp->ups_info.eee)
3122 		ups_flags |= UPS_FLAGS_EN_EEE;
3123 
3124 	if (tp->ups_info.flow_control)
3125 		ups_flags |= UPS_FLAGS_EN_FLOW_CTR;
3126 
3127 	if (tp->ups_info.eee_ckdiv)
3128 		ups_flags |= UPS_FLAGS_EN_EEE_CKDIV;
3129 
3130 	if (tp->ups_info.eee_cmod_lv)
3131 		ups_flags |= UPS_FLAGS_EEE_CMOD_LV_EN;
3132 
3133 	if (tp->ups_info._10m_ckdiv)
3134 		ups_flags |= UPS_FLAGS_EN_10M_CKDIV;
3135 
3136 	if (tp->ups_info.eee_plloff_100)
3137 		ups_flags |= UPS_FLAGS_EEE_PLLOFF_100;
3138 
3139 	if (tp->ups_info.eee_plloff_giga)
3140 		ups_flags |= UPS_FLAGS_EEE_PLLOFF_GIGA;
3141 
3142 	if (tp->ups_info._250m_ckdiv)
3143 		ups_flags |= UPS_FLAGS_250M_CKDIV;
3144 
3145 	if (tp->ups_info.ctap_short_off)
3146 		ups_flags |= UPS_FLAGS_CTAP_SHORT_DIS;
3147 
3148 	switch (tp->ups_info.speed_duplex) {
3149 	case NWAY_10M_HALF:
3150 		ups_flags |= ups_flags_speed(1);
3151 		break;
3152 	case NWAY_10M_FULL:
3153 		ups_flags |= ups_flags_speed(2);
3154 		break;
3155 	case NWAY_100M_HALF:
3156 		ups_flags |= ups_flags_speed(3);
3157 		break;
3158 	case NWAY_100M_FULL:
3159 		ups_flags |= ups_flags_speed(4);
3160 		break;
3161 	case NWAY_1000M_FULL:
3162 		ups_flags |= ups_flags_speed(5);
3163 		break;
3164 	case FORCE_10M_HALF:
3165 		ups_flags |= ups_flags_speed(6);
3166 		break;
3167 	case FORCE_10M_FULL:
3168 		ups_flags |= ups_flags_speed(7);
3169 		break;
3170 	case FORCE_100M_HALF:
3171 		ups_flags |= ups_flags_speed(8);
3172 		break;
3173 	case FORCE_100M_FULL:
3174 		ups_flags |= ups_flags_speed(9);
3175 		break;
3176 	default:
3177 		break;
3178 	}
3179 
3180 	ocp_write_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS, ups_flags);
3181 }
3182 
3183 static void r8153b_green_en(struct r8152 *tp, bool enable)
3184 {
3185 	u16 data;
3186 
3187 	if (enable) {
3188 		sram_write(tp, 0x8045, 0);	/* 10M abiq&ldvbias */
3189 		sram_write(tp, 0x804d, 0x1222);	/* 100M short abiq&ldvbias */
3190 		sram_write(tp, 0x805d, 0x0022);	/* 1000M short abiq&ldvbias */
3191 	} else {
3192 		sram_write(tp, 0x8045, 0x2444);	/* 10M abiq&ldvbias */
3193 		sram_write(tp, 0x804d, 0x2444);	/* 100M short abiq&ldvbias */
3194 		sram_write(tp, 0x805d, 0x2444);	/* 1000M short abiq&ldvbias */
3195 	}
3196 
3197 	data = sram_read(tp, SRAM_GREEN_CFG);
3198 	data |= GREEN_ETH_EN;
3199 	sram_write(tp, SRAM_GREEN_CFG, data);
3200 
3201 	tp->ups_info.green = enable;
3202 }
3203 
3204 static u16 r8153_phy_status(struct r8152 *tp, u16 desired)
3205 {
3206 	u16 data;
3207 	int i;
3208 
3209 	for (i = 0; i < 500; i++) {
3210 		data = ocp_reg_read(tp, OCP_PHY_STATUS);
3211 		data &= PHY_STAT_MASK;
3212 		if (desired) {
3213 			if (data == desired)
3214 				break;
3215 		} else if (data == PHY_STAT_LAN_ON || data == PHY_STAT_PWRDN ||
3216 			   data == PHY_STAT_EXT_INIT) {
3217 			break;
3218 		}
3219 
3220 		msleep(20);
3221 	}
3222 
3223 	return data;
3224 }
3225 
3226 static void r8153b_ups_en(struct r8152 *tp, bool enable)
3227 {
3228 	u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_POWER_CUT);
3229 
3230 	if (enable) {
3231 		r8153b_ups_flags(tp);
3232 
3233 		ocp_data |= UPS_EN | USP_PREWAKE | PHASE2_EN;
3234 		ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3235 
3236 		ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, 0xcfff);
3237 		ocp_data |= BIT(0);
3238 		ocp_write_byte(tp, MCU_TYPE_USB, 0xcfff, ocp_data);
3239 	} else {
3240 		u16 data;
3241 
3242 		ocp_data &= ~(UPS_EN | USP_PREWAKE);
3243 		ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3244 
3245 		ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, 0xcfff);
3246 		ocp_data &= ~BIT(0);
3247 		ocp_write_byte(tp, MCU_TYPE_USB, 0xcfff, ocp_data);
3248 
3249 		ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
3250 		ocp_data &= ~PCUT_STATUS;
3251 		ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
3252 
3253 		data = r8153_phy_status(tp, 0);
3254 
3255 		switch (data) {
3256 		case PHY_STAT_PWRDN:
3257 		case PHY_STAT_EXT_INIT:
3258 			r8153b_green_en(tp,
3259 					test_bit(GREEN_ETHERNET, &tp->flags));
3260 
3261 			data = r8152_mdio_read(tp, MII_BMCR);
3262 			data &= ~BMCR_PDOWN;
3263 			data |= BMCR_RESET;
3264 			r8152_mdio_write(tp, MII_BMCR, data);
3265 
3266 			data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
3267 			/* fall through */
3268 
3269 		default:
3270 			if (data != PHY_STAT_LAN_ON)
3271 				netif_warn(tp, link, tp->netdev,
3272 					   "PHY not ready");
3273 			break;
3274 		}
3275 	}
3276 }
3277 
3278 static void r8153_power_cut_en(struct r8152 *tp, bool enable)
3279 {
3280 	u32 ocp_data;
3281 
3282 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
3283 	if (enable)
3284 		ocp_data |= PWR_EN | PHASE2_EN;
3285 	else
3286 		ocp_data &= ~(PWR_EN | PHASE2_EN);
3287 	ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3288 
3289 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
3290 	ocp_data &= ~PCUT_STATUS;
3291 	ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
3292 }
3293 
3294 static void r8153b_power_cut_en(struct r8152 *tp, bool enable)
3295 {
3296 	u32 ocp_data;
3297 
3298 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
3299 	if (enable)
3300 		ocp_data |= PWR_EN | PHASE2_EN;
3301 	else
3302 		ocp_data &= ~PWR_EN;
3303 	ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3304 
3305 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
3306 	ocp_data &= ~PCUT_STATUS;
3307 	ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
3308 }
3309 
3310 static void r8153_queue_wake(struct r8152 *tp, bool enable)
3311 {
3312 	u32 ocp_data;
3313 
3314 	ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG);
3315 	if (enable)
3316 		ocp_data |= UPCOMING_RUNTIME_D3;
3317 	else
3318 		ocp_data &= ~UPCOMING_RUNTIME_D3;
3319 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG, ocp_data);
3320 
3321 	ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG);
3322 	ocp_data &= ~LINK_CHG_EVENT;
3323 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG, ocp_data);
3324 
3325 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
3326 	ocp_data &= ~LINK_CHANGE_FLAG;
3327 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
3328 }
3329 
3330 static bool rtl_can_wakeup(struct r8152 *tp)
3331 {
3332 	struct usb_device *udev = tp->udev;
3333 
3334 	return (udev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_WAKEUP);
3335 }
3336 
3337 static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable)
3338 {
3339 	if (enable) {
3340 		u32 ocp_data;
3341 
3342 		__rtl_set_wol(tp, WAKE_ANY);
3343 
3344 		ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
3345 
3346 		ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
3347 		ocp_data |= LINK_OFF_WAKE_EN;
3348 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
3349 
3350 		ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
3351 	} else {
3352 		u32 ocp_data;
3353 
3354 		__rtl_set_wol(tp, tp->saved_wolopts);
3355 
3356 		ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
3357 
3358 		ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
3359 		ocp_data &= ~LINK_OFF_WAKE_EN;
3360 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
3361 
3362 		ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
3363 	}
3364 }
3365 
3366 static void rtl8153_runtime_enable(struct r8152 *tp, bool enable)
3367 {
3368 	if (enable) {
3369 		r8153_u1u2en(tp, false);
3370 		r8153_u2p3en(tp, false);
3371 		r8153_mac_clk_spd(tp, true);
3372 		rtl_runtime_suspend_enable(tp, true);
3373 	} else {
3374 		rtl_runtime_suspend_enable(tp, false);
3375 		r8153_mac_clk_spd(tp, false);
3376 
3377 		switch (tp->version) {
3378 		case RTL_VER_03:
3379 		case RTL_VER_04:
3380 			break;
3381 		case RTL_VER_05:
3382 		case RTL_VER_06:
3383 		default:
3384 			r8153_u2p3en(tp, true);
3385 			break;
3386 		}
3387 
3388 		r8153_u1u2en(tp, true);
3389 	}
3390 }
3391 
3392 static void rtl8153b_runtime_enable(struct r8152 *tp, bool enable)
3393 {
3394 	if (enable) {
3395 		r8153_queue_wake(tp, true);
3396 		r8153b_u1u2en(tp, false);
3397 		r8153_u2p3en(tp, false);
3398 		rtl_runtime_suspend_enable(tp, true);
3399 		r8153b_ups_en(tp, true);
3400 	} else {
3401 		r8153b_ups_en(tp, false);
3402 		r8153_queue_wake(tp, false);
3403 		rtl_runtime_suspend_enable(tp, false);
3404 		if (tp->udev->speed != USB_SPEED_HIGH)
3405 			r8153b_u1u2en(tp, true);
3406 	}
3407 }
3408 
3409 static void r8153_teredo_off(struct r8152 *tp)
3410 {
3411 	u32 ocp_data;
3412 
3413 	switch (tp->version) {
3414 	case RTL_VER_01:
3415 	case RTL_VER_02:
3416 	case RTL_VER_03:
3417 	case RTL_VER_04:
3418 	case RTL_VER_05:
3419 	case RTL_VER_06:
3420 	case RTL_VER_07:
3421 		ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
3422 		ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK |
3423 			      OOB_TEREDO_EN);
3424 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
3425 		break;
3426 
3427 	case RTL_VER_08:
3428 	case RTL_VER_09:
3429 		/* The bit 0 ~ 7 are relative with teredo settings. They are
3430 		 * W1C (write 1 to clear), so set all 1 to disable it.
3431 		 */
3432 		ocp_write_byte(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, 0xff);
3433 		break;
3434 
3435 	default:
3436 		break;
3437 	}
3438 
3439 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE);
3440 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0);
3441 	ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0);
3442 }
3443 
3444 static void rtl_reset_bmu(struct r8152 *tp)
3445 {
3446 	u32 ocp_data;
3447 
3448 	ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_BMU_RESET);
3449 	ocp_data &= ~(BMU_RESET_EP_IN | BMU_RESET_EP_OUT);
3450 	ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
3451 	ocp_data |= BMU_RESET_EP_IN | BMU_RESET_EP_OUT;
3452 	ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
3453 }
3454 
3455 /* Clear the bp to stop the firmware before loading a new one */
3456 static void rtl_clear_bp(struct r8152 *tp, u16 type)
3457 {
3458 	switch (tp->version) {
3459 	case RTL_VER_01:
3460 	case RTL_VER_02:
3461 	case RTL_VER_07:
3462 		break;
3463 	case RTL_VER_03:
3464 	case RTL_VER_04:
3465 	case RTL_VER_05:
3466 	case RTL_VER_06:
3467 		ocp_write_byte(tp, type, PLA_BP_EN, 0);
3468 		break;
3469 	case RTL_VER_08:
3470 	case RTL_VER_09:
3471 	default:
3472 		if (type == MCU_TYPE_USB) {
3473 			ocp_write_byte(tp, MCU_TYPE_USB, USB_BP2_EN, 0);
3474 
3475 			ocp_write_word(tp, MCU_TYPE_USB, USB_BP_8, 0);
3476 			ocp_write_word(tp, MCU_TYPE_USB, USB_BP_9, 0);
3477 			ocp_write_word(tp, MCU_TYPE_USB, USB_BP_10, 0);
3478 			ocp_write_word(tp, MCU_TYPE_USB, USB_BP_11, 0);
3479 			ocp_write_word(tp, MCU_TYPE_USB, USB_BP_12, 0);
3480 			ocp_write_word(tp, MCU_TYPE_USB, USB_BP_13, 0);
3481 			ocp_write_word(tp, MCU_TYPE_USB, USB_BP_14, 0);
3482 			ocp_write_word(tp, MCU_TYPE_USB, USB_BP_15, 0);
3483 		} else {
3484 			ocp_write_byte(tp, MCU_TYPE_PLA, PLA_BP_EN, 0);
3485 		}
3486 		break;
3487 	}
3488 
3489 	ocp_write_word(tp, type, PLA_BP_0, 0);
3490 	ocp_write_word(tp, type, PLA_BP_1, 0);
3491 	ocp_write_word(tp, type, PLA_BP_2, 0);
3492 	ocp_write_word(tp, type, PLA_BP_3, 0);
3493 	ocp_write_word(tp, type, PLA_BP_4, 0);
3494 	ocp_write_word(tp, type, PLA_BP_5, 0);
3495 	ocp_write_word(tp, type, PLA_BP_6, 0);
3496 	ocp_write_word(tp, type, PLA_BP_7, 0);
3497 
3498 	/* wait 3 ms to make sure the firmware is stopped */
3499 	usleep_range(3000, 6000);
3500 	ocp_write_word(tp, type, PLA_BP_BA, 0);
3501 }
3502 
3503 static int r8153_patch_request(struct r8152 *tp, bool request)
3504 {
3505 	u16 data;
3506 	int i;
3507 
3508 	data = ocp_reg_read(tp, OCP_PHY_PATCH_CMD);
3509 	if (request)
3510 		data |= PATCH_REQUEST;
3511 	else
3512 		data &= ~PATCH_REQUEST;
3513 	ocp_reg_write(tp, OCP_PHY_PATCH_CMD, data);
3514 
3515 	for (i = 0; request && i < 5000; i++) {
3516 		usleep_range(1000, 2000);
3517 		if (ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)
3518 			break;
3519 	}
3520 
3521 	if (request && !(ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)) {
3522 		netif_err(tp, drv, tp->netdev, "patch request fail\n");
3523 		r8153_patch_request(tp, false);
3524 		return -ETIME;
3525 	} else {
3526 		return 0;
3527 	}
3528 }
3529 
3530 static int r8153_pre_ram_code(struct r8152 *tp, u16 key_addr, u16 patch_key)
3531 {
3532 	if (r8153_patch_request(tp, true)) {
3533 		dev_err(&tp->intf->dev, "patch request fail\n");
3534 		return -ETIME;
3535 	}
3536 
3537 	sram_write(tp, key_addr, patch_key);
3538 	sram_write(tp, SRAM_PHY_LOCK, PHY_PATCH_LOCK);
3539 
3540 	return 0;
3541 }
3542 
3543 static int r8153_post_ram_code(struct r8152 *tp, u16 key_addr)
3544 {
3545 	u16 data;
3546 
3547 	sram_write(tp, 0x0000, 0x0000);
3548 
3549 	data = ocp_reg_read(tp, OCP_PHY_LOCK);
3550 	data &= ~PATCH_LOCK;
3551 	ocp_reg_write(tp, OCP_PHY_LOCK, data);
3552 
3553 	sram_write(tp, key_addr, 0x0000);
3554 
3555 	r8153_patch_request(tp, false);
3556 
3557 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, tp->ocp_base);
3558 
3559 	return 0;
3560 }
3561 
3562 static bool rtl8152_is_fw_phy_nc_ok(struct r8152 *tp, struct fw_phy_nc *phy)
3563 {
3564 	u32 length;
3565 	u16 fw_offset, fw_reg, ba_reg, patch_en_addr, mode_reg, bp_start;
3566 	bool rc = false;
3567 
3568 	switch (tp->version) {
3569 	case RTL_VER_04:
3570 	case RTL_VER_05:
3571 	case RTL_VER_06:
3572 		fw_reg = 0xa014;
3573 		ba_reg = 0xa012;
3574 		patch_en_addr = 0xa01a;
3575 		mode_reg = 0xb820;
3576 		bp_start = 0xa000;
3577 		break;
3578 	default:
3579 		goto out;
3580 	}
3581 
3582 	fw_offset = __le16_to_cpu(phy->fw_offset);
3583 	if (fw_offset < sizeof(*phy)) {
3584 		dev_err(&tp->intf->dev, "fw_offset too small\n");
3585 		goto out;
3586 	}
3587 
3588 	length = __le32_to_cpu(phy->blk_hdr.length);
3589 	if (length < fw_offset) {
3590 		dev_err(&tp->intf->dev, "invalid fw_offset\n");
3591 		goto out;
3592 	}
3593 
3594 	length -= __le16_to_cpu(phy->fw_offset);
3595 	if (!length || (length & 1)) {
3596 		dev_err(&tp->intf->dev, "invalid block length\n");
3597 		goto out;
3598 	}
3599 
3600 	if (__le16_to_cpu(phy->fw_reg) != fw_reg) {
3601 		dev_err(&tp->intf->dev, "invalid register to load firmware\n");
3602 		goto out;
3603 	}
3604 
3605 	if (__le16_to_cpu(phy->ba_reg) != ba_reg) {
3606 		dev_err(&tp->intf->dev, "invalid base address register\n");
3607 		goto out;
3608 	}
3609 
3610 	if (__le16_to_cpu(phy->patch_en_addr) != patch_en_addr) {
3611 		dev_err(&tp->intf->dev,
3612 			"invalid patch mode enabled register\n");
3613 		goto out;
3614 	}
3615 
3616 	if (__le16_to_cpu(phy->mode_reg) != mode_reg) {
3617 		dev_err(&tp->intf->dev,
3618 			"invalid register to switch the mode\n");
3619 		goto out;
3620 	}
3621 
3622 	if (__le16_to_cpu(phy->bp_start) != bp_start) {
3623 		dev_err(&tp->intf->dev,
3624 			"invalid start register of break point\n");
3625 		goto out;
3626 	}
3627 
3628 	if (__le16_to_cpu(phy->bp_num) > 4) {
3629 		dev_err(&tp->intf->dev, "invalid break point number\n");
3630 		goto out;
3631 	}
3632 
3633 	rc = true;
3634 out:
3635 	return rc;
3636 }
3637 
3638 static bool rtl8152_is_fw_mac_ok(struct r8152 *tp, struct fw_mac *mac)
3639 {
3640 	u16 fw_reg, bp_ba_addr, bp_en_addr, bp_start, fw_offset;
3641 	bool rc = false;
3642 	u32 length, type;
3643 	int i, max_bp;
3644 
3645 	type = __le32_to_cpu(mac->blk_hdr.type);
3646 	if (type == RTL_FW_PLA) {
3647 		switch (tp->version) {
3648 		case RTL_VER_01:
3649 		case RTL_VER_02:
3650 		case RTL_VER_07:
3651 			fw_reg = 0xf800;
3652 			bp_ba_addr = PLA_BP_BA;
3653 			bp_en_addr = 0;
3654 			bp_start = PLA_BP_0;
3655 			max_bp = 8;
3656 			break;
3657 		case RTL_VER_03:
3658 		case RTL_VER_04:
3659 		case RTL_VER_05:
3660 		case RTL_VER_06:
3661 		case RTL_VER_08:
3662 		case RTL_VER_09:
3663 			fw_reg = 0xf800;
3664 			bp_ba_addr = PLA_BP_BA;
3665 			bp_en_addr = PLA_BP_EN;
3666 			bp_start = PLA_BP_0;
3667 			max_bp = 8;
3668 			break;
3669 		default:
3670 			goto out;
3671 		}
3672 	} else if (type == RTL_FW_USB) {
3673 		switch (tp->version) {
3674 		case RTL_VER_03:
3675 		case RTL_VER_04:
3676 		case RTL_VER_05:
3677 		case RTL_VER_06:
3678 			fw_reg = 0xf800;
3679 			bp_ba_addr = USB_BP_BA;
3680 			bp_en_addr = USB_BP_EN;
3681 			bp_start = USB_BP_0;
3682 			max_bp = 8;
3683 			break;
3684 		case RTL_VER_08:
3685 		case RTL_VER_09:
3686 			fw_reg = 0xe600;
3687 			bp_ba_addr = USB_BP_BA;
3688 			bp_en_addr = USB_BP2_EN;
3689 			bp_start = USB_BP_0;
3690 			max_bp = 16;
3691 			break;
3692 		case RTL_VER_01:
3693 		case RTL_VER_02:
3694 		case RTL_VER_07:
3695 		default:
3696 			goto out;
3697 		}
3698 	} else {
3699 		goto out;
3700 	}
3701 
3702 	fw_offset = __le16_to_cpu(mac->fw_offset);
3703 	if (fw_offset < sizeof(*mac)) {
3704 		dev_err(&tp->intf->dev, "fw_offset too small\n");
3705 		goto out;
3706 	}
3707 
3708 	length = __le32_to_cpu(mac->blk_hdr.length);
3709 	if (length < fw_offset) {
3710 		dev_err(&tp->intf->dev, "invalid fw_offset\n");
3711 		goto out;
3712 	}
3713 
3714 	length -= fw_offset;
3715 	if (length < 4 || (length & 3)) {
3716 		dev_err(&tp->intf->dev, "invalid block length\n");
3717 		goto out;
3718 	}
3719 
3720 	if (__le16_to_cpu(mac->fw_reg) != fw_reg) {
3721 		dev_err(&tp->intf->dev, "invalid register to load firmware\n");
3722 		goto out;
3723 	}
3724 
3725 	if (__le16_to_cpu(mac->bp_ba_addr) != bp_ba_addr) {
3726 		dev_err(&tp->intf->dev, "invalid base address register\n");
3727 		goto out;
3728 	}
3729 
3730 	if (__le16_to_cpu(mac->bp_en_addr) != bp_en_addr) {
3731 		dev_err(&tp->intf->dev, "invalid enabled mask register\n");
3732 		goto out;
3733 	}
3734 
3735 	if (__le16_to_cpu(mac->bp_start) != bp_start) {
3736 		dev_err(&tp->intf->dev,
3737 			"invalid start register of break point\n");
3738 		goto out;
3739 	}
3740 
3741 	if (__le16_to_cpu(mac->bp_num) > max_bp) {
3742 		dev_err(&tp->intf->dev, "invalid break point number\n");
3743 		goto out;
3744 	}
3745 
3746 	for (i = __le16_to_cpu(mac->bp_num); i < max_bp; i++) {
3747 		if (mac->bp[i]) {
3748 			dev_err(&tp->intf->dev, "unused bp%u is not zero\n", i);
3749 			goto out;
3750 		}
3751 	}
3752 
3753 	rc = true;
3754 out:
3755 	return rc;
3756 }
3757 
3758 /* Verify the checksum for the firmware file. It is calculated from the version
3759  * field to the end of the file. Compare the result with the checksum field to
3760  * make sure the file is correct.
3761  */
3762 static long rtl8152_fw_verify_checksum(struct r8152 *tp,
3763 				       struct fw_header *fw_hdr, size_t size)
3764 {
3765 	unsigned char checksum[sizeof(fw_hdr->checksum)];
3766 	struct crypto_shash *alg;
3767 	struct shash_desc *sdesc;
3768 	size_t len;
3769 	long rc;
3770 
3771 	alg = crypto_alloc_shash("sha256", 0, 0);
3772 	if (IS_ERR(alg)) {
3773 		rc = PTR_ERR(alg);
3774 		goto out;
3775 	}
3776 
3777 	if (crypto_shash_digestsize(alg) != sizeof(fw_hdr->checksum)) {
3778 		rc = -EFAULT;
3779 		dev_err(&tp->intf->dev, "digestsize incorrect (%u)\n",
3780 			crypto_shash_digestsize(alg));
3781 		goto free_shash;
3782 	}
3783 
3784 	len = sizeof(*sdesc) + crypto_shash_descsize(alg);
3785 	sdesc = kmalloc(len, GFP_KERNEL);
3786 	if (!sdesc) {
3787 		rc = -ENOMEM;
3788 		goto free_shash;
3789 	}
3790 	sdesc->tfm = alg;
3791 
3792 	len = size - sizeof(fw_hdr->checksum);
3793 	rc = crypto_shash_digest(sdesc, fw_hdr->version, len, checksum);
3794 	kfree(sdesc);
3795 	if (rc)
3796 		goto free_shash;
3797 
3798 	if (memcmp(fw_hdr->checksum, checksum, sizeof(fw_hdr->checksum))) {
3799 		dev_err(&tp->intf->dev, "checksum fail\n");
3800 		rc = -EFAULT;
3801 	}
3802 
3803 free_shash:
3804 	crypto_free_shash(alg);
3805 out:
3806 	return rc;
3807 }
3808 
3809 static long rtl8152_check_firmware(struct r8152 *tp, struct rtl_fw *rtl_fw)
3810 {
3811 	const struct firmware *fw = rtl_fw->fw;
3812 	struct fw_header *fw_hdr = (struct fw_header *)fw->data;
3813 	struct fw_mac *pla = NULL, *usb = NULL;
3814 	struct fw_phy_patch_key *start = NULL;
3815 	struct fw_phy_nc *phy_nc = NULL;
3816 	struct fw_block *stop = NULL;
3817 	long ret = -EFAULT;
3818 	int i;
3819 
3820 	if (fw->size < sizeof(*fw_hdr)) {
3821 		dev_err(&tp->intf->dev, "file too small\n");
3822 		goto fail;
3823 	}
3824 
3825 	ret = rtl8152_fw_verify_checksum(tp, fw_hdr, fw->size);
3826 	if (ret)
3827 		goto fail;
3828 
3829 	ret = -EFAULT;
3830 
3831 	for (i = sizeof(*fw_hdr); i < fw->size;) {
3832 		struct fw_block *block = (struct fw_block *)&fw->data[i];
3833 		u32 type;
3834 
3835 		if ((i + sizeof(*block)) > fw->size)
3836 			goto fail;
3837 
3838 		type = __le32_to_cpu(block->type);
3839 		switch (type) {
3840 		case RTL_FW_END:
3841 			if (__le32_to_cpu(block->length) != sizeof(*block))
3842 				goto fail;
3843 			goto fw_end;
3844 		case RTL_FW_PLA:
3845 			if (pla) {
3846 				dev_err(&tp->intf->dev,
3847 					"multiple PLA firmware encountered");
3848 				goto fail;
3849 			}
3850 
3851 			pla = (struct fw_mac *)block;
3852 			if (!rtl8152_is_fw_mac_ok(tp, pla)) {
3853 				dev_err(&tp->intf->dev,
3854 					"check PLA firmware failed\n");
3855 				goto fail;
3856 			}
3857 			break;
3858 		case RTL_FW_USB:
3859 			if (usb) {
3860 				dev_err(&tp->intf->dev,
3861 					"multiple USB firmware encountered");
3862 				goto fail;
3863 			}
3864 
3865 			usb = (struct fw_mac *)block;
3866 			if (!rtl8152_is_fw_mac_ok(tp, usb)) {
3867 				dev_err(&tp->intf->dev,
3868 					"check USB firmware failed\n");
3869 				goto fail;
3870 			}
3871 			break;
3872 		case RTL_FW_PHY_START:
3873 			if (start || phy_nc || stop) {
3874 				dev_err(&tp->intf->dev,
3875 					"check PHY_START fail\n");
3876 				goto fail;
3877 			}
3878 
3879 			if (__le32_to_cpu(block->length) != sizeof(*start)) {
3880 				dev_err(&tp->intf->dev,
3881 					"Invalid length for PHY_START\n");
3882 				goto fail;
3883 			}
3884 
3885 			start = (struct fw_phy_patch_key *)block;
3886 			break;
3887 		case RTL_FW_PHY_STOP:
3888 			if (stop || !start) {
3889 				dev_err(&tp->intf->dev,
3890 					"Check PHY_STOP fail\n");
3891 				goto fail;
3892 			}
3893 
3894 			if (__le32_to_cpu(block->length) != sizeof(*block)) {
3895 				dev_err(&tp->intf->dev,
3896 					"Invalid length for PHY_STOP\n");
3897 				goto fail;
3898 			}
3899 
3900 			stop = block;
3901 			break;
3902 		case RTL_FW_PHY_NC:
3903 			if (!start || stop) {
3904 				dev_err(&tp->intf->dev,
3905 					"check PHY_NC fail\n");
3906 				goto fail;
3907 			}
3908 
3909 			if (phy_nc) {
3910 				dev_err(&tp->intf->dev,
3911 					"multiple PHY NC encountered\n");
3912 				goto fail;
3913 			}
3914 
3915 			phy_nc = (struct fw_phy_nc *)block;
3916 			if (!rtl8152_is_fw_phy_nc_ok(tp, phy_nc)) {
3917 				dev_err(&tp->intf->dev,
3918 					"check PHY NC firmware failed\n");
3919 				goto fail;
3920 			}
3921 
3922 			break;
3923 		default:
3924 			dev_warn(&tp->intf->dev, "Unknown type %u is found\n",
3925 				 type);
3926 			break;
3927 		}
3928 
3929 		/* next block */
3930 		i += ALIGN(__le32_to_cpu(block->length), 8);
3931 	}
3932 
3933 fw_end:
3934 	if ((phy_nc || start) && !stop) {
3935 		dev_err(&tp->intf->dev, "without PHY_STOP\n");
3936 		goto fail;
3937 	}
3938 
3939 	return 0;
3940 fail:
3941 	return ret;
3942 }
3943 
3944 static void rtl8152_fw_phy_nc_apply(struct r8152 *tp, struct fw_phy_nc *phy)
3945 {
3946 	u16 mode_reg, bp_index;
3947 	u32 length, i, num;
3948 	__le16 *data;
3949 
3950 	mode_reg = __le16_to_cpu(phy->mode_reg);
3951 	sram_write(tp, mode_reg, __le16_to_cpu(phy->mode_pre));
3952 	sram_write(tp, __le16_to_cpu(phy->ba_reg),
3953 		   __le16_to_cpu(phy->ba_data));
3954 
3955 	length = __le32_to_cpu(phy->blk_hdr.length);
3956 	length -= __le16_to_cpu(phy->fw_offset);
3957 	num = length / 2;
3958 	data = (__le16 *)((u8 *)phy + __le16_to_cpu(phy->fw_offset));
3959 
3960 	ocp_reg_write(tp, OCP_SRAM_ADDR, __le16_to_cpu(phy->fw_reg));
3961 	for (i = 0; i < num; i++)
3962 		ocp_reg_write(tp, OCP_SRAM_DATA, __le16_to_cpu(data[i]));
3963 
3964 	sram_write(tp, __le16_to_cpu(phy->patch_en_addr),
3965 		   __le16_to_cpu(phy->patch_en_value));
3966 
3967 	bp_index = __le16_to_cpu(phy->bp_start);
3968 	num = __le16_to_cpu(phy->bp_num);
3969 	for (i = 0; i < num; i++) {
3970 		sram_write(tp, bp_index, __le16_to_cpu(phy->bp[i]));
3971 		bp_index += 2;
3972 	}
3973 
3974 	sram_write(tp, mode_reg, __le16_to_cpu(phy->mode_post));
3975 
3976 	dev_dbg(&tp->intf->dev, "successfully applied %s\n", phy->info);
3977 }
3978 
3979 static void rtl8152_fw_mac_apply(struct r8152 *tp, struct fw_mac *mac)
3980 {
3981 	u16 bp_en_addr, bp_index, type, bp_num, fw_ver_reg;
3982 	u32 length;
3983 	u8 *data;
3984 	int i;
3985 
3986 	switch (__le32_to_cpu(mac->blk_hdr.type)) {
3987 	case RTL_FW_PLA:
3988 		type = MCU_TYPE_PLA;
3989 		break;
3990 	case RTL_FW_USB:
3991 		type = MCU_TYPE_USB;
3992 		break;
3993 	default:
3994 		return;
3995 	}
3996 
3997 	rtl_clear_bp(tp, type);
3998 
3999 	/* Enable backup/restore of MACDBG. This is required after clearing PLA
4000 	 * break points and before applying the PLA firmware.
4001 	 */
4002 	if (tp->version == RTL_VER_04 && type == MCU_TYPE_PLA &&
4003 	    !(ocp_read_word(tp, MCU_TYPE_PLA, PLA_MACDBG_POST) & DEBUG_OE)) {
4004 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_MACDBG_PRE, DEBUG_LTSSM);
4005 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_MACDBG_POST, DEBUG_LTSSM);
4006 	}
4007 
4008 	length = __le32_to_cpu(mac->blk_hdr.length);
4009 	length -= __le16_to_cpu(mac->fw_offset);
4010 
4011 	data = (u8 *)mac;
4012 	data += __le16_to_cpu(mac->fw_offset);
4013 
4014 	generic_ocp_write(tp, __le16_to_cpu(mac->fw_reg), 0xff, length, data,
4015 			  type);
4016 
4017 	ocp_write_word(tp, type, __le16_to_cpu(mac->bp_ba_addr),
4018 		       __le16_to_cpu(mac->bp_ba_value));
4019 
4020 	bp_index = __le16_to_cpu(mac->bp_start);
4021 	bp_num = __le16_to_cpu(mac->bp_num);
4022 	for (i = 0; i < bp_num; i++) {
4023 		ocp_write_word(tp, type, bp_index, __le16_to_cpu(mac->bp[i]));
4024 		bp_index += 2;
4025 	}
4026 
4027 	bp_en_addr = __le16_to_cpu(mac->bp_en_addr);
4028 	if (bp_en_addr)
4029 		ocp_write_word(tp, type, bp_en_addr,
4030 			       __le16_to_cpu(mac->bp_en_value));
4031 
4032 	fw_ver_reg = __le16_to_cpu(mac->fw_ver_reg);
4033 	if (fw_ver_reg)
4034 		ocp_write_byte(tp, MCU_TYPE_USB, fw_ver_reg,
4035 			       mac->fw_ver_data);
4036 
4037 	dev_dbg(&tp->intf->dev, "successfully applied %s\n", mac->info);
4038 }
4039 
4040 static void rtl8152_apply_firmware(struct r8152 *tp)
4041 {
4042 	struct rtl_fw *rtl_fw = &tp->rtl_fw;
4043 	const struct firmware *fw;
4044 	struct fw_header *fw_hdr;
4045 	struct fw_phy_patch_key *key;
4046 	u16 key_addr = 0;
4047 	int i;
4048 
4049 	if (IS_ERR_OR_NULL(rtl_fw->fw))
4050 		return;
4051 
4052 	fw = rtl_fw->fw;
4053 	fw_hdr = (struct fw_header *)fw->data;
4054 
4055 	if (rtl_fw->pre_fw)
4056 		rtl_fw->pre_fw(tp);
4057 
4058 	for (i = offsetof(struct fw_header, blocks); i < fw->size;) {
4059 		struct fw_block *block = (struct fw_block *)&fw->data[i];
4060 
4061 		switch (__le32_to_cpu(block->type)) {
4062 		case RTL_FW_END:
4063 			goto post_fw;
4064 		case RTL_FW_PLA:
4065 		case RTL_FW_USB:
4066 			rtl8152_fw_mac_apply(tp, (struct fw_mac *)block);
4067 			break;
4068 		case RTL_FW_PHY_START:
4069 			key = (struct fw_phy_patch_key *)block;
4070 			key_addr = __le16_to_cpu(key->key_reg);
4071 			r8153_pre_ram_code(tp, key_addr,
4072 					   __le16_to_cpu(key->key_data));
4073 			break;
4074 		case RTL_FW_PHY_STOP:
4075 			WARN_ON(!key_addr);
4076 			r8153_post_ram_code(tp, key_addr);
4077 			break;
4078 		case RTL_FW_PHY_NC:
4079 			rtl8152_fw_phy_nc_apply(tp, (struct fw_phy_nc *)block);
4080 			break;
4081 		default:
4082 			break;
4083 		}
4084 
4085 		i += ALIGN(__le32_to_cpu(block->length), 8);
4086 	}
4087 
4088 post_fw:
4089 	if (rtl_fw->post_fw)
4090 		rtl_fw->post_fw(tp);
4091 
4092 	strscpy(rtl_fw->version, fw_hdr->version, RTL_VER_SIZE);
4093 	dev_info(&tp->intf->dev, "load %s successfully\n", rtl_fw->version);
4094 }
4095 
4096 static void rtl8152_release_firmware(struct r8152 *tp)
4097 {
4098 	struct rtl_fw *rtl_fw = &tp->rtl_fw;
4099 
4100 	if (!IS_ERR_OR_NULL(rtl_fw->fw)) {
4101 		release_firmware(rtl_fw->fw);
4102 		rtl_fw->fw = NULL;
4103 	}
4104 }
4105 
4106 static int rtl8152_request_firmware(struct r8152 *tp)
4107 {
4108 	struct rtl_fw *rtl_fw = &tp->rtl_fw;
4109 	long rc;
4110 
4111 	if (rtl_fw->fw || !rtl_fw->fw_name) {
4112 		dev_info(&tp->intf->dev, "skip request firmware\n");
4113 		rc = 0;
4114 		goto result;
4115 	}
4116 
4117 	rc = request_firmware(&rtl_fw->fw, rtl_fw->fw_name, &tp->intf->dev);
4118 	if (rc < 0)
4119 		goto result;
4120 
4121 	rc = rtl8152_check_firmware(tp, rtl_fw);
4122 	if (rc < 0)
4123 		release_firmware(rtl_fw->fw);
4124 
4125 result:
4126 	if (rc) {
4127 		rtl_fw->fw = ERR_PTR(rc);
4128 
4129 		dev_warn(&tp->intf->dev,
4130 			 "unable to load firmware patch %s (%ld)\n",
4131 			 rtl_fw->fw_name, rc);
4132 	}
4133 
4134 	return rc;
4135 }
4136 
4137 static void r8152_aldps_en(struct r8152 *tp, bool enable)
4138 {
4139 	if (enable) {
4140 		ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
4141 						    LINKENA | DIS_SDSAVE);
4142 	} else {
4143 		ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA |
4144 						    DIS_SDSAVE);
4145 		msleep(20);
4146 	}
4147 }
4148 
4149 static inline void r8152_mmd_indirect(struct r8152 *tp, u16 dev, u16 reg)
4150 {
4151 	ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | dev);
4152 	ocp_reg_write(tp, OCP_EEE_DATA, reg);
4153 	ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | dev);
4154 }
4155 
4156 static u16 r8152_mmd_read(struct r8152 *tp, u16 dev, u16 reg)
4157 {
4158 	u16 data;
4159 
4160 	r8152_mmd_indirect(tp, dev, reg);
4161 	data = ocp_reg_read(tp, OCP_EEE_DATA);
4162 	ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
4163 
4164 	return data;
4165 }
4166 
4167 static void r8152_mmd_write(struct r8152 *tp, u16 dev, u16 reg, u16 data)
4168 {
4169 	r8152_mmd_indirect(tp, dev, reg);
4170 	ocp_reg_write(tp, OCP_EEE_DATA, data);
4171 	ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
4172 }
4173 
4174 static void r8152_eee_en(struct r8152 *tp, bool enable)
4175 {
4176 	u16 config1, config2, config3;
4177 	u32 ocp_data;
4178 
4179 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
4180 	config1 = ocp_reg_read(tp, OCP_EEE_CONFIG1) & ~sd_rise_time_mask;
4181 	config2 = ocp_reg_read(tp, OCP_EEE_CONFIG2);
4182 	config3 = ocp_reg_read(tp, OCP_EEE_CONFIG3) & ~fast_snr_mask;
4183 
4184 	if (enable) {
4185 		ocp_data |= EEE_RX_EN | EEE_TX_EN;
4186 		config1 |= EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | RX_QUIET_EN;
4187 		config1 |= sd_rise_time(1);
4188 		config2 |= RG_DACQUIET_EN | RG_LDVQUIET_EN;
4189 		config3 |= fast_snr(42);
4190 	} else {
4191 		ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
4192 		config1 &= ~(EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN |
4193 			     RX_QUIET_EN);
4194 		config1 |= sd_rise_time(7);
4195 		config2 &= ~(RG_DACQUIET_EN | RG_LDVQUIET_EN);
4196 		config3 |= fast_snr(511);
4197 	}
4198 
4199 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
4200 	ocp_reg_write(tp, OCP_EEE_CONFIG1, config1);
4201 	ocp_reg_write(tp, OCP_EEE_CONFIG2, config2);
4202 	ocp_reg_write(tp, OCP_EEE_CONFIG3, config3);
4203 }
4204 
4205 static void r8153_eee_en(struct r8152 *tp, bool enable)
4206 {
4207 	u32 ocp_data;
4208 	u16 config;
4209 
4210 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
4211 	config = ocp_reg_read(tp, OCP_EEE_CFG);
4212 
4213 	if (enable) {
4214 		ocp_data |= EEE_RX_EN | EEE_TX_EN;
4215 		config |= EEE10_EN;
4216 	} else {
4217 		ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
4218 		config &= ~EEE10_EN;
4219 	}
4220 
4221 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
4222 	ocp_reg_write(tp, OCP_EEE_CFG, config);
4223 
4224 	tp->ups_info.eee = enable;
4225 }
4226 
4227 static void rtl_eee_enable(struct r8152 *tp, bool enable)
4228 {
4229 	switch (tp->version) {
4230 	case RTL_VER_01:
4231 	case RTL_VER_02:
4232 	case RTL_VER_07:
4233 		if (enable) {
4234 			r8152_eee_en(tp, true);
4235 			r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV,
4236 					tp->eee_adv);
4237 		} else {
4238 			r8152_eee_en(tp, false);
4239 			r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, 0);
4240 		}
4241 		break;
4242 	case RTL_VER_03:
4243 	case RTL_VER_04:
4244 	case RTL_VER_05:
4245 	case RTL_VER_06:
4246 	case RTL_VER_08:
4247 	case RTL_VER_09:
4248 		if (enable) {
4249 			r8153_eee_en(tp, true);
4250 			ocp_reg_write(tp, OCP_EEE_ADV, tp->eee_adv);
4251 		} else {
4252 			r8153_eee_en(tp, false);
4253 			ocp_reg_write(tp, OCP_EEE_ADV, 0);
4254 		}
4255 		break;
4256 	default:
4257 		break;
4258 	}
4259 }
4260 
4261 static void r8152b_enable_fc(struct r8152 *tp)
4262 {
4263 	u16 anar;
4264 
4265 	anar = r8152_mdio_read(tp, MII_ADVERTISE);
4266 	anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
4267 	r8152_mdio_write(tp, MII_ADVERTISE, anar);
4268 
4269 	tp->ups_info.flow_control = true;
4270 }
4271 
4272 static void rtl8152_disable(struct r8152 *tp)
4273 {
4274 	r8152_aldps_en(tp, false);
4275 	rtl_disable(tp);
4276 	r8152_aldps_en(tp, true);
4277 }
4278 
4279 static void r8152b_hw_phy_cfg(struct r8152 *tp)
4280 {
4281 	rtl8152_apply_firmware(tp);
4282 	rtl_eee_enable(tp, tp->eee_en);
4283 	r8152_aldps_en(tp, true);
4284 	r8152b_enable_fc(tp);
4285 
4286 	set_bit(PHY_RESET, &tp->flags);
4287 }
4288 
4289 static void wait_oob_link_list_ready(struct r8152 *tp)
4290 {
4291 	u32 ocp_data;
4292 	int i;
4293 
4294 	for (i = 0; i < 1000; i++) {
4295 		ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
4296 		if (ocp_data & LINK_LIST_READY)
4297 			break;
4298 		usleep_range(1000, 2000);
4299 	}
4300 }
4301 
4302 static void r8152b_exit_oob(struct r8152 *tp)
4303 {
4304 	u32 ocp_data;
4305 
4306 	ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
4307 	ocp_data &= ~RCR_ACPT_ALL;
4308 	ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
4309 
4310 	rxdy_gated_en(tp, true);
4311 	r8153_teredo_off(tp);
4312 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
4313 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
4314 
4315 	ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
4316 	ocp_data &= ~NOW_IS_OOB;
4317 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
4318 
4319 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
4320 	ocp_data &= ~MCU_BORW_EN;
4321 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
4322 
4323 	wait_oob_link_list_ready(tp);
4324 
4325 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
4326 	ocp_data |= RE_INIT_LL;
4327 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
4328 
4329 	wait_oob_link_list_ready(tp);
4330 
4331 	rtl8152_nic_reset(tp);
4332 
4333 	/* rx share fifo credit full threshold */
4334 	ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
4335 
4336 	if (tp->udev->speed == USB_SPEED_FULL ||
4337 	    tp->udev->speed == USB_SPEED_LOW) {
4338 		/* rx share fifo credit near full threshold */
4339 		ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
4340 				RXFIFO_THR2_FULL);
4341 		ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
4342 				RXFIFO_THR3_FULL);
4343 	} else {
4344 		/* rx share fifo credit near full threshold */
4345 		ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
4346 				RXFIFO_THR2_HIGH);
4347 		ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
4348 				RXFIFO_THR3_HIGH);
4349 	}
4350 
4351 	/* TX share fifo free credit full threshold */
4352 	ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL);
4353 
4354 	ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
4355 	ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH);
4356 	ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
4357 			TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
4358 
4359 	rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
4360 
4361 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
4362 
4363 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
4364 	ocp_data |= TCR0_AUTO_FIFO;
4365 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
4366 }
4367 
4368 static void r8152b_enter_oob(struct r8152 *tp)
4369 {
4370 	u32 ocp_data;
4371 
4372 	ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
4373 	ocp_data &= ~NOW_IS_OOB;
4374 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
4375 
4376 	ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
4377 	ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
4378 	ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
4379 
4380 	rtl_disable(tp);
4381 
4382 	wait_oob_link_list_ready(tp);
4383 
4384 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
4385 	ocp_data |= RE_INIT_LL;
4386 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
4387 
4388 	wait_oob_link_list_ready(tp);
4389 
4390 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
4391 
4392 	rtl_rx_vlan_en(tp, true);
4393 
4394 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR);
4395 	ocp_data |= ALDPS_PROXY_MODE;
4396 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data);
4397 
4398 	ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
4399 	ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
4400 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
4401 
4402 	rxdy_gated_en(tp, false);
4403 
4404 	ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
4405 	ocp_data |= RCR_APM | RCR_AM | RCR_AB;
4406 	ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
4407 }
4408 
4409 static int r8153_pre_firmware_1(struct r8152 *tp)
4410 {
4411 	int i;
4412 
4413 	/* Wait till the WTD timer is ready. It would take at most 104 ms. */
4414 	for (i = 0; i < 104; i++) {
4415 		u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_WDT1_CTRL);
4416 
4417 		if (!(ocp_data & WTD1_EN))
4418 			break;
4419 		usleep_range(1000, 2000);
4420 	}
4421 
4422 	return 0;
4423 }
4424 
4425 static int r8153_post_firmware_1(struct r8152 *tp)
4426 {
4427 	/* set USB_BP_4 to support USB_SPEED_SUPER only */
4428 	if (ocp_read_byte(tp, MCU_TYPE_USB, USB_CSTMR) & FORCE_SUPER)
4429 		ocp_write_word(tp, MCU_TYPE_USB, USB_BP_4, BP4_SUPER_ONLY);
4430 
4431 	/* reset UPHY timer to 36 ms */
4432 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_UPHY_TIMER, 36000 / 16);
4433 
4434 	return 0;
4435 }
4436 
4437 static int r8153_pre_firmware_2(struct r8152 *tp)
4438 {
4439 	u32 ocp_data;
4440 
4441 	r8153_pre_firmware_1(tp);
4442 
4443 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0);
4444 	ocp_data &= ~FW_FIX_SUSPEND;
4445 	ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0, ocp_data);
4446 
4447 	return 0;
4448 }
4449 
4450 static int r8153_post_firmware_2(struct r8152 *tp)
4451 {
4452 	u32 ocp_data;
4453 
4454 	/* enable bp0 if support USB_SPEED_SUPER only */
4455 	if (ocp_read_byte(tp, MCU_TYPE_USB, USB_CSTMR) & FORCE_SUPER) {
4456 		ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BP_EN);
4457 		ocp_data |= BIT(0);
4458 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_EN, ocp_data);
4459 	}
4460 
4461 	/* reset UPHY timer to 36 ms */
4462 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_UPHY_TIMER, 36000 / 16);
4463 
4464 	/* enable U3P3 check, set the counter to 4 */
4465 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, U3P3_CHECK_EN | 4);
4466 
4467 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0);
4468 	ocp_data |= FW_FIX_SUSPEND;
4469 	ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0, ocp_data);
4470 
4471 	ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
4472 	ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
4473 	ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
4474 
4475 	return 0;
4476 }
4477 
4478 static int r8153_post_firmware_3(struct r8152 *tp)
4479 {
4480 	u32 ocp_data;
4481 
4482 	ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
4483 	ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
4484 	ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
4485 
4486 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1);
4487 	ocp_data |= FW_IP_RESET_EN;
4488 	ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1, ocp_data);
4489 
4490 	return 0;
4491 }
4492 
4493 static int r8153b_pre_firmware_1(struct r8152 *tp)
4494 {
4495 	/* enable fc timer and set timer to 1 second. */
4496 	ocp_write_word(tp, MCU_TYPE_USB, USB_FC_TIMER,
4497 		       CTRL_TIMER_EN | (1000 / 8));
4498 
4499 	return 0;
4500 }
4501 
4502 static int r8153b_post_firmware_1(struct r8152 *tp)
4503 {
4504 	u32 ocp_data;
4505 
4506 	/* enable bp0 for RTL8153-BND */
4507 	ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_1);
4508 	if (ocp_data & BND_MASK) {
4509 		ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BP_EN);
4510 		ocp_data |= BIT(0);
4511 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_EN, ocp_data);
4512 	}
4513 
4514 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_CTRL);
4515 	ocp_data |= FLOW_CTRL_PATCH_OPT;
4516 	ocp_write_word(tp, MCU_TYPE_USB, USB_FW_CTRL, ocp_data);
4517 
4518 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK);
4519 	ocp_data |= FC_PATCH_TASK;
4520 	ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
4521 
4522 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1);
4523 	ocp_data |= FW_IP_RESET_EN;
4524 	ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1, ocp_data);
4525 
4526 	return 0;
4527 }
4528 
4529 static void r8153_aldps_en(struct r8152 *tp, bool enable)
4530 {
4531 	u16 data;
4532 
4533 	data = ocp_reg_read(tp, OCP_POWER_CFG);
4534 	if (enable) {
4535 		data |= EN_ALDPS;
4536 		ocp_reg_write(tp, OCP_POWER_CFG, data);
4537 	} else {
4538 		int i;
4539 
4540 		data &= ~EN_ALDPS;
4541 		ocp_reg_write(tp, OCP_POWER_CFG, data);
4542 		for (i = 0; i < 20; i++) {
4543 			usleep_range(1000, 2000);
4544 			if (ocp_read_word(tp, MCU_TYPE_PLA, 0xe000) & 0x0100)
4545 				break;
4546 		}
4547 	}
4548 
4549 	tp->ups_info.aldps = enable;
4550 }
4551 
4552 static void r8153_hw_phy_cfg(struct r8152 *tp)
4553 {
4554 	u32 ocp_data;
4555 	u16 data;
4556 
4557 	/* disable ALDPS before updating the PHY parameters */
4558 	r8153_aldps_en(tp, false);
4559 
4560 	/* disable EEE before updating the PHY parameters */
4561 	rtl_eee_enable(tp, false);
4562 
4563 	rtl8152_apply_firmware(tp);
4564 
4565 	if (tp->version == RTL_VER_03) {
4566 		data = ocp_reg_read(tp, OCP_EEE_CFG);
4567 		data &= ~CTAP_SHORT_EN;
4568 		ocp_reg_write(tp, OCP_EEE_CFG, data);
4569 	}
4570 
4571 	data = ocp_reg_read(tp, OCP_POWER_CFG);
4572 	data |= EEE_CLKDIV_EN;
4573 	ocp_reg_write(tp, OCP_POWER_CFG, data);
4574 
4575 	data = ocp_reg_read(tp, OCP_DOWN_SPEED);
4576 	data |= EN_10M_BGOFF;
4577 	ocp_reg_write(tp, OCP_DOWN_SPEED, data);
4578 	data = ocp_reg_read(tp, OCP_POWER_CFG);
4579 	data |= EN_10M_PLLOFF;
4580 	ocp_reg_write(tp, OCP_POWER_CFG, data);
4581 	sram_write(tp, SRAM_IMPEDANCE, 0x0b13);
4582 
4583 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
4584 	ocp_data |= PFM_PWM_SWITCH;
4585 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
4586 
4587 	/* Enable LPF corner auto tune */
4588 	sram_write(tp, SRAM_LPF_CFG, 0xf70f);
4589 
4590 	/* Adjust 10M Amplitude */
4591 	sram_write(tp, SRAM_10M_AMP1, 0x00af);
4592 	sram_write(tp, SRAM_10M_AMP2, 0x0208);
4593 
4594 	if (tp->eee_en)
4595 		rtl_eee_enable(tp, true);
4596 
4597 	r8153_aldps_en(tp, true);
4598 	r8152b_enable_fc(tp);
4599 
4600 	switch (tp->version) {
4601 	case RTL_VER_03:
4602 	case RTL_VER_04:
4603 		break;
4604 	case RTL_VER_05:
4605 	case RTL_VER_06:
4606 	default:
4607 		r8153_u2p3en(tp, true);
4608 		break;
4609 	}
4610 
4611 	set_bit(PHY_RESET, &tp->flags);
4612 }
4613 
4614 static u32 r8152_efuse_read(struct r8152 *tp, u8 addr)
4615 {
4616 	u32 ocp_data;
4617 
4618 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD, EFUSE_READ_CMD | addr);
4619 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD);
4620 	ocp_data = (ocp_data & EFUSE_DATA_BIT16) << 9;	/* data of bit16 */
4621 	ocp_data |= ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_DATA);
4622 
4623 	return ocp_data;
4624 }
4625 
4626 static void r8153b_hw_phy_cfg(struct r8152 *tp)
4627 {
4628 	u32 ocp_data;
4629 	u16 data;
4630 
4631 	/* disable ALDPS before updating the PHY parameters */
4632 	r8153_aldps_en(tp, false);
4633 
4634 	/* disable EEE before updating the PHY parameters */
4635 	rtl_eee_enable(tp, false);
4636 
4637 	rtl8152_apply_firmware(tp);
4638 
4639 	r8153b_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags));
4640 
4641 	data = sram_read(tp, SRAM_GREEN_CFG);
4642 	data |= R_TUNE_EN;
4643 	sram_write(tp, SRAM_GREEN_CFG, data);
4644 	data = ocp_reg_read(tp, OCP_NCTL_CFG);
4645 	data |= PGA_RETURN_EN;
4646 	ocp_reg_write(tp, OCP_NCTL_CFG, data);
4647 
4648 	/* ADC Bias Calibration:
4649 	 * read efuse offset 0x7d to get a 17-bit data. Remove the dummy/fake
4650 	 * bit (bit3) to rebuild the real 16-bit data. Write the data to the
4651 	 * ADC ioffset.
4652 	 */
4653 	ocp_data = r8152_efuse_read(tp, 0x7d);
4654 	data = (u16)(((ocp_data & 0x1fff0) >> 1) | (ocp_data & 0x7));
4655 	if (data != 0xffff)
4656 		ocp_reg_write(tp, OCP_ADC_IOFFSET, data);
4657 
4658 	/* ups mode tx-link-pulse timing adjustment:
4659 	 * rg_saw_cnt = OCP reg 0xC426 Bit[13:0]
4660 	 * swr_cnt_1ms_ini = 16000000 / rg_saw_cnt
4661 	 */
4662 	ocp_data = ocp_reg_read(tp, 0xc426);
4663 	ocp_data &= 0x3fff;
4664 	if (ocp_data) {
4665 		u32 swr_cnt_1ms_ini;
4666 
4667 		swr_cnt_1ms_ini = (16000000 / ocp_data) & SAW_CNT_1MS_MASK;
4668 		ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CFG);
4669 		ocp_data = (ocp_data & ~SAW_CNT_1MS_MASK) | swr_cnt_1ms_ini;
4670 		ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CFG, ocp_data);
4671 	}
4672 
4673 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
4674 	ocp_data |= PFM_PWM_SWITCH;
4675 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
4676 
4677 	/* Advnace EEE */
4678 	if (!r8153_patch_request(tp, true)) {
4679 		data = ocp_reg_read(tp, OCP_POWER_CFG);
4680 		data |= EEE_CLKDIV_EN;
4681 		ocp_reg_write(tp, OCP_POWER_CFG, data);
4682 		tp->ups_info.eee_ckdiv = true;
4683 
4684 		data = ocp_reg_read(tp, OCP_DOWN_SPEED);
4685 		data |= EN_EEE_CMODE | EN_EEE_1000 | EN_10M_CLKDIV;
4686 		ocp_reg_write(tp, OCP_DOWN_SPEED, data);
4687 		tp->ups_info.eee_cmod_lv = true;
4688 		tp->ups_info._10m_ckdiv = true;
4689 		tp->ups_info.eee_plloff_giga = true;
4690 
4691 		ocp_reg_write(tp, OCP_SYSCLK_CFG, 0);
4692 		ocp_reg_write(tp, OCP_SYSCLK_CFG, clk_div_expo(5));
4693 		tp->ups_info._250m_ckdiv = true;
4694 
4695 		r8153_patch_request(tp, false);
4696 	}
4697 
4698 	if (tp->eee_en)
4699 		rtl_eee_enable(tp, true);
4700 
4701 	r8153_aldps_en(tp, true);
4702 	r8152b_enable_fc(tp);
4703 
4704 	set_bit(PHY_RESET, &tp->flags);
4705 }
4706 
4707 static void r8153_first_init(struct r8152 *tp)
4708 {
4709 	u32 ocp_data;
4710 
4711 	r8153_mac_clk_spd(tp, false);
4712 	rxdy_gated_en(tp, true);
4713 	r8153_teredo_off(tp);
4714 
4715 	ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
4716 	ocp_data &= ~RCR_ACPT_ALL;
4717 	ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
4718 
4719 	rtl8152_nic_reset(tp);
4720 	rtl_reset_bmu(tp);
4721 
4722 	ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
4723 	ocp_data &= ~NOW_IS_OOB;
4724 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
4725 
4726 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
4727 	ocp_data &= ~MCU_BORW_EN;
4728 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
4729 
4730 	wait_oob_link_list_ready(tp);
4731 
4732 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
4733 	ocp_data |= RE_INIT_LL;
4734 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
4735 
4736 	wait_oob_link_list_ready(tp);
4737 
4738 	rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
4739 
4740 	ocp_data = tp->netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
4741 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, ocp_data);
4742 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO);
4743 
4744 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
4745 	ocp_data |= TCR0_AUTO_FIFO;
4746 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
4747 
4748 	rtl8152_nic_reset(tp);
4749 
4750 	/* rx share fifo credit full threshold */
4751 	ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
4752 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
4753 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
4754 	/* TX share fifo free credit full threshold */
4755 	ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
4756 }
4757 
4758 static void r8153_enter_oob(struct r8152 *tp)
4759 {
4760 	u32 ocp_data;
4761 
4762 	r8153_mac_clk_spd(tp, true);
4763 
4764 	ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
4765 	ocp_data &= ~NOW_IS_OOB;
4766 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
4767 
4768 	rtl_disable(tp);
4769 	rtl_reset_bmu(tp);
4770 
4771 	wait_oob_link_list_ready(tp);
4772 
4773 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
4774 	ocp_data |= RE_INIT_LL;
4775 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
4776 
4777 	wait_oob_link_list_ready(tp);
4778 
4779 	ocp_data = tp->netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
4780 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, ocp_data);
4781 
4782 	switch (tp->version) {
4783 	case RTL_VER_03:
4784 	case RTL_VER_04:
4785 	case RTL_VER_05:
4786 	case RTL_VER_06:
4787 		ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
4788 		ocp_data &= ~TEREDO_WAKE_MASK;
4789 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
4790 		break;
4791 
4792 	case RTL_VER_08:
4793 	case RTL_VER_09:
4794 		/* Clear teredo wake event. bit[15:8] is the teredo wakeup
4795 		 * type. Set it to zero. bits[7:0] are the W1C bits about
4796 		 * the events. Set them to all 1 to clear them.
4797 		 */
4798 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_WAKE_BASE, 0x00ff);
4799 		break;
4800 
4801 	default:
4802 		break;
4803 	}
4804 
4805 	rtl_rx_vlan_en(tp, true);
4806 
4807 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR);
4808 	ocp_data |= ALDPS_PROXY_MODE;
4809 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data);
4810 
4811 	ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
4812 	ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
4813 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
4814 
4815 	rxdy_gated_en(tp, false);
4816 
4817 	ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
4818 	ocp_data |= RCR_APM | RCR_AM | RCR_AB;
4819 	ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
4820 }
4821 
4822 static void rtl8153_disable(struct r8152 *tp)
4823 {
4824 	r8153_aldps_en(tp, false);
4825 	rtl_disable(tp);
4826 	rtl_reset_bmu(tp);
4827 	r8153_aldps_en(tp, true);
4828 }
4829 
4830 static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u32 speed, u8 duplex,
4831 			     u32 advertising)
4832 {
4833 	u16 bmcr;
4834 	int ret = 0;
4835 
4836 	if (autoneg == AUTONEG_DISABLE) {
4837 		if (duplex != DUPLEX_HALF && duplex != DUPLEX_FULL)
4838 			return -EINVAL;
4839 
4840 		switch (speed) {
4841 		case SPEED_10:
4842 			bmcr = BMCR_SPEED10;
4843 			if (duplex == DUPLEX_FULL) {
4844 				bmcr |= BMCR_FULLDPLX;
4845 				tp->ups_info.speed_duplex = FORCE_10M_FULL;
4846 			} else {
4847 				tp->ups_info.speed_duplex = FORCE_10M_HALF;
4848 			}
4849 			break;
4850 		case SPEED_100:
4851 			bmcr = BMCR_SPEED100;
4852 			if (duplex == DUPLEX_FULL) {
4853 				bmcr |= BMCR_FULLDPLX;
4854 				tp->ups_info.speed_duplex = FORCE_100M_FULL;
4855 			} else {
4856 				tp->ups_info.speed_duplex = FORCE_100M_HALF;
4857 			}
4858 			break;
4859 		case SPEED_1000:
4860 			if (tp->mii.supports_gmii) {
4861 				bmcr = BMCR_SPEED1000 | BMCR_FULLDPLX;
4862 				tp->ups_info.speed_duplex = NWAY_1000M_FULL;
4863 				break;
4864 			}
4865 			/* fall through */
4866 		default:
4867 			ret = -EINVAL;
4868 			goto out;
4869 		}
4870 
4871 		if (duplex == DUPLEX_FULL)
4872 			tp->mii.full_duplex = 1;
4873 		else
4874 			tp->mii.full_duplex = 0;
4875 
4876 		tp->mii.force_media = 1;
4877 	} else {
4878 		u16 anar, tmp1;
4879 		u32 support;
4880 
4881 		support = RTL_ADVERTISED_10_HALF | RTL_ADVERTISED_10_FULL |
4882 			  RTL_ADVERTISED_100_HALF | RTL_ADVERTISED_100_FULL;
4883 
4884 		if (tp->mii.supports_gmii)
4885 			support |= RTL_ADVERTISED_1000_FULL;
4886 
4887 		if (!(advertising & support))
4888 			return -EINVAL;
4889 
4890 		anar = r8152_mdio_read(tp, MII_ADVERTISE);
4891 		tmp1 = anar & ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
4892 				ADVERTISE_100HALF | ADVERTISE_100FULL);
4893 		if (advertising & RTL_ADVERTISED_10_HALF) {
4894 			tmp1 |= ADVERTISE_10HALF;
4895 			tp->ups_info.speed_duplex = NWAY_10M_HALF;
4896 		}
4897 		if (advertising & RTL_ADVERTISED_10_FULL) {
4898 			tmp1 |= ADVERTISE_10FULL;
4899 			tp->ups_info.speed_duplex = NWAY_10M_FULL;
4900 		}
4901 
4902 		if (advertising & RTL_ADVERTISED_100_HALF) {
4903 			tmp1 |= ADVERTISE_100HALF;
4904 			tp->ups_info.speed_duplex = NWAY_100M_HALF;
4905 		}
4906 		if (advertising & RTL_ADVERTISED_100_FULL) {
4907 			tmp1 |= ADVERTISE_100FULL;
4908 			tp->ups_info.speed_duplex = NWAY_100M_FULL;
4909 		}
4910 
4911 		if (anar != tmp1) {
4912 			r8152_mdio_write(tp, MII_ADVERTISE, tmp1);
4913 			tp->mii.advertising = tmp1;
4914 		}
4915 
4916 		if (tp->mii.supports_gmii) {
4917 			u16 gbcr;
4918 
4919 			gbcr = r8152_mdio_read(tp, MII_CTRL1000);
4920 			tmp1 = gbcr & ~(ADVERTISE_1000FULL |
4921 					ADVERTISE_1000HALF);
4922 
4923 			if (advertising & RTL_ADVERTISED_1000_FULL) {
4924 				tmp1 |= ADVERTISE_1000FULL;
4925 				tp->ups_info.speed_duplex = NWAY_1000M_FULL;
4926 			}
4927 
4928 			if (gbcr != tmp1)
4929 				r8152_mdio_write(tp, MII_CTRL1000, tmp1);
4930 		}
4931 
4932 		bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
4933 
4934 		tp->mii.force_media = 0;
4935 	}
4936 
4937 	if (test_and_clear_bit(PHY_RESET, &tp->flags))
4938 		bmcr |= BMCR_RESET;
4939 
4940 	r8152_mdio_write(tp, MII_BMCR, bmcr);
4941 
4942 	if (bmcr & BMCR_RESET) {
4943 		int i;
4944 
4945 		for (i = 0; i < 50; i++) {
4946 			msleep(20);
4947 			if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
4948 				break;
4949 		}
4950 	}
4951 
4952 out:
4953 	return ret;
4954 }
4955 
4956 static void rtl8152_up(struct r8152 *tp)
4957 {
4958 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
4959 		return;
4960 
4961 	r8152_aldps_en(tp, false);
4962 	r8152b_exit_oob(tp);
4963 	r8152_aldps_en(tp, true);
4964 }
4965 
4966 static void rtl8152_down(struct r8152 *tp)
4967 {
4968 	if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
4969 		rtl_drop_queued_tx(tp);
4970 		return;
4971 	}
4972 
4973 	r8152_power_cut_en(tp, false);
4974 	r8152_aldps_en(tp, false);
4975 	r8152b_enter_oob(tp);
4976 	r8152_aldps_en(tp, true);
4977 }
4978 
4979 static void rtl8153_up(struct r8152 *tp)
4980 {
4981 	u32 ocp_data;
4982 
4983 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
4984 		return;
4985 
4986 	r8153_u1u2en(tp, false);
4987 	r8153_u2p3en(tp, false);
4988 	r8153_aldps_en(tp, false);
4989 	r8153_first_init(tp);
4990 
4991 	ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6);
4992 	ocp_data |= LANWAKE_CLR_EN;
4993 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6, ocp_data);
4994 
4995 	ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG);
4996 	ocp_data &= ~LANWAKE_PIN;
4997 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG, ocp_data);
4998 
4999 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK1);
5000 	ocp_data &= ~DELAY_PHY_PWR_CHG;
5001 	ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK1, ocp_data);
5002 
5003 	r8153_aldps_en(tp, true);
5004 
5005 	switch (tp->version) {
5006 	case RTL_VER_03:
5007 	case RTL_VER_04:
5008 		break;
5009 	case RTL_VER_05:
5010 	case RTL_VER_06:
5011 	default:
5012 		r8153_u2p3en(tp, true);
5013 		break;
5014 	}
5015 
5016 	r8153_u1u2en(tp, true);
5017 }
5018 
5019 static void rtl8153_down(struct r8152 *tp)
5020 {
5021 	u32 ocp_data;
5022 
5023 	if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
5024 		rtl_drop_queued_tx(tp);
5025 		return;
5026 	}
5027 
5028 	ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6);
5029 	ocp_data &= ~LANWAKE_CLR_EN;
5030 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6, ocp_data);
5031 
5032 	r8153_u1u2en(tp, false);
5033 	r8153_u2p3en(tp, false);
5034 	r8153_power_cut_en(tp, false);
5035 	r8153_aldps_en(tp, false);
5036 	r8153_enter_oob(tp);
5037 	r8153_aldps_en(tp, true);
5038 }
5039 
5040 static void rtl8153b_up(struct r8152 *tp)
5041 {
5042 	u32 ocp_data;
5043 
5044 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
5045 		return;
5046 
5047 	r8153b_u1u2en(tp, false);
5048 	r8153_u2p3en(tp, false);
5049 	r8153_aldps_en(tp, false);
5050 
5051 	r8153_first_init(tp);
5052 	ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_B);
5053 
5054 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
5055 	ocp_data &= ~PLA_MCU_SPDWN_EN;
5056 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
5057 
5058 	r8153_aldps_en(tp, true);
5059 
5060 	if (tp->udev->speed != USB_SPEED_HIGH)
5061 		r8153b_u1u2en(tp, true);
5062 }
5063 
5064 static void rtl8153b_down(struct r8152 *tp)
5065 {
5066 	u32 ocp_data;
5067 
5068 	if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
5069 		rtl_drop_queued_tx(tp);
5070 		return;
5071 	}
5072 
5073 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
5074 	ocp_data |= PLA_MCU_SPDWN_EN;
5075 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
5076 
5077 	r8153b_u1u2en(tp, false);
5078 	r8153_u2p3en(tp, false);
5079 	r8153b_power_cut_en(tp, false);
5080 	r8153_aldps_en(tp, false);
5081 	r8153_enter_oob(tp);
5082 	r8153_aldps_en(tp, true);
5083 }
5084 
5085 static bool rtl8152_in_nway(struct r8152 *tp)
5086 {
5087 	u16 nway_state;
5088 
5089 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, 0x2000);
5090 	tp->ocp_base = 0x2000;
5091 	ocp_write_byte(tp, MCU_TYPE_PLA, 0xb014, 0x4c);		/* phy state */
5092 	nway_state = ocp_read_word(tp, MCU_TYPE_PLA, 0xb01a);
5093 
5094 	/* bit 15: TXDIS_STATE, bit 14: ABD_STATE */
5095 	if (nway_state & 0xc000)
5096 		return false;
5097 	else
5098 		return true;
5099 }
5100 
5101 static bool rtl8153_in_nway(struct r8152 *tp)
5102 {
5103 	u16 phy_state = ocp_reg_read(tp, OCP_PHY_STATE) & 0xff;
5104 
5105 	if (phy_state == TXDIS_STATE || phy_state == ABD_STATE)
5106 		return false;
5107 	else
5108 		return true;
5109 }
5110 
5111 static void set_carrier(struct r8152 *tp)
5112 {
5113 	struct net_device *netdev = tp->netdev;
5114 	struct napi_struct *napi = &tp->napi;
5115 	u8 speed;
5116 
5117 	speed = rtl8152_get_speed(tp);
5118 
5119 	if (speed & LINK_STATUS) {
5120 		if (!netif_carrier_ok(netdev)) {
5121 			tp->rtl_ops.enable(tp);
5122 			netif_stop_queue(netdev);
5123 			napi_disable(napi);
5124 			netif_carrier_on(netdev);
5125 			rtl_start_rx(tp);
5126 			clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
5127 			_rtl8152_set_rx_mode(netdev);
5128 			napi_enable(&tp->napi);
5129 			netif_wake_queue(netdev);
5130 			netif_info(tp, link, netdev, "carrier on\n");
5131 		} else if (netif_queue_stopped(netdev) &&
5132 			   skb_queue_len(&tp->tx_queue) < tp->tx_qlen) {
5133 			netif_wake_queue(netdev);
5134 		}
5135 	} else {
5136 		if (netif_carrier_ok(netdev)) {
5137 			netif_carrier_off(netdev);
5138 			tasklet_disable(&tp->tx_tl);
5139 			napi_disable(napi);
5140 			tp->rtl_ops.disable(tp);
5141 			napi_enable(napi);
5142 			tasklet_enable(&tp->tx_tl);
5143 			netif_info(tp, link, netdev, "carrier off\n");
5144 		}
5145 	}
5146 }
5147 
5148 static void rtl_work_func_t(struct work_struct *work)
5149 {
5150 	struct r8152 *tp = container_of(work, struct r8152, schedule.work);
5151 
5152 	/* If the device is unplugged or !netif_running(), the workqueue
5153 	 * doesn't need to wake the device, and could return directly.
5154 	 */
5155 	if (test_bit(RTL8152_UNPLUG, &tp->flags) || !netif_running(tp->netdev))
5156 		return;
5157 
5158 	if (usb_autopm_get_interface(tp->intf) < 0)
5159 		return;
5160 
5161 	if (!test_bit(WORK_ENABLE, &tp->flags))
5162 		goto out1;
5163 
5164 	if (!mutex_trylock(&tp->control)) {
5165 		schedule_delayed_work(&tp->schedule, 0);
5166 		goto out1;
5167 	}
5168 
5169 	if (test_and_clear_bit(RTL8152_LINK_CHG, &tp->flags))
5170 		set_carrier(tp);
5171 
5172 	if (test_and_clear_bit(RTL8152_SET_RX_MODE, &tp->flags))
5173 		_rtl8152_set_rx_mode(tp->netdev);
5174 
5175 	/* don't schedule tasket before linking */
5176 	if (test_and_clear_bit(SCHEDULE_TASKLET, &tp->flags) &&
5177 	    netif_carrier_ok(tp->netdev))
5178 		tasklet_schedule(&tp->tx_tl);
5179 
5180 	mutex_unlock(&tp->control);
5181 
5182 out1:
5183 	usb_autopm_put_interface(tp->intf);
5184 }
5185 
5186 static void rtl_hw_phy_work_func_t(struct work_struct *work)
5187 {
5188 	struct r8152 *tp = container_of(work, struct r8152, hw_phy_work.work);
5189 
5190 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
5191 		return;
5192 
5193 	if (usb_autopm_get_interface(tp->intf) < 0)
5194 		return;
5195 
5196 	mutex_lock(&tp->control);
5197 
5198 	if (rtl8152_request_firmware(tp) == -ENODEV && tp->rtl_fw.retry) {
5199 		tp->rtl_fw.retry = false;
5200 		tp->rtl_fw.fw = NULL;
5201 
5202 		/* Delay execution in case request_firmware() is not ready yet.
5203 		 */
5204 		queue_delayed_work(system_long_wq, &tp->hw_phy_work, HZ * 10);
5205 		goto ignore_once;
5206 	}
5207 
5208 	tp->rtl_ops.hw_phy_cfg(tp);
5209 
5210 	rtl8152_set_speed(tp, tp->autoneg, tp->speed, tp->duplex,
5211 			  tp->advertising);
5212 
5213 ignore_once:
5214 	mutex_unlock(&tp->control);
5215 
5216 	usb_autopm_put_interface(tp->intf);
5217 }
5218 
5219 #ifdef CONFIG_PM_SLEEP
5220 static int rtl_notifier(struct notifier_block *nb, unsigned long action,
5221 			void *data)
5222 {
5223 	struct r8152 *tp = container_of(nb, struct r8152, pm_notifier);
5224 
5225 	switch (action) {
5226 	case PM_HIBERNATION_PREPARE:
5227 	case PM_SUSPEND_PREPARE:
5228 		usb_autopm_get_interface(tp->intf);
5229 		break;
5230 
5231 	case PM_POST_HIBERNATION:
5232 	case PM_POST_SUSPEND:
5233 		usb_autopm_put_interface(tp->intf);
5234 		break;
5235 
5236 	case PM_POST_RESTORE:
5237 	case PM_RESTORE_PREPARE:
5238 	default:
5239 		break;
5240 	}
5241 
5242 	return NOTIFY_DONE;
5243 }
5244 #endif
5245 
5246 static int rtl8152_open(struct net_device *netdev)
5247 {
5248 	struct r8152 *tp = netdev_priv(netdev);
5249 	int res = 0;
5250 
5251 	if (work_busy(&tp->hw_phy_work.work) & WORK_BUSY_PENDING) {
5252 		cancel_delayed_work_sync(&tp->hw_phy_work);
5253 		rtl_hw_phy_work_func_t(&tp->hw_phy_work.work);
5254 	}
5255 
5256 	res = alloc_all_mem(tp);
5257 	if (res)
5258 		goto out;
5259 
5260 	res = usb_autopm_get_interface(tp->intf);
5261 	if (res < 0)
5262 		goto out_free;
5263 
5264 	mutex_lock(&tp->control);
5265 
5266 	tp->rtl_ops.up(tp);
5267 
5268 	netif_carrier_off(netdev);
5269 	netif_start_queue(netdev);
5270 	set_bit(WORK_ENABLE, &tp->flags);
5271 
5272 	res = usb_submit_urb(tp->intr_urb, GFP_KERNEL);
5273 	if (res) {
5274 		if (res == -ENODEV)
5275 			netif_device_detach(tp->netdev);
5276 		netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n",
5277 			   res);
5278 		goto out_unlock;
5279 	}
5280 	napi_enable(&tp->napi);
5281 	tasklet_enable(&tp->tx_tl);
5282 
5283 	mutex_unlock(&tp->control);
5284 
5285 	usb_autopm_put_interface(tp->intf);
5286 #ifdef CONFIG_PM_SLEEP
5287 	tp->pm_notifier.notifier_call = rtl_notifier;
5288 	register_pm_notifier(&tp->pm_notifier);
5289 #endif
5290 	return 0;
5291 
5292 out_unlock:
5293 	mutex_unlock(&tp->control);
5294 	usb_autopm_put_interface(tp->intf);
5295 out_free:
5296 	free_all_mem(tp);
5297 out:
5298 	return res;
5299 }
5300 
5301 static int rtl8152_close(struct net_device *netdev)
5302 {
5303 	struct r8152 *tp = netdev_priv(netdev);
5304 	int res = 0;
5305 
5306 #ifdef CONFIG_PM_SLEEP
5307 	unregister_pm_notifier(&tp->pm_notifier);
5308 #endif
5309 	tasklet_disable(&tp->tx_tl);
5310 	clear_bit(WORK_ENABLE, &tp->flags);
5311 	usb_kill_urb(tp->intr_urb);
5312 	cancel_delayed_work_sync(&tp->schedule);
5313 	napi_disable(&tp->napi);
5314 	netif_stop_queue(netdev);
5315 
5316 	res = usb_autopm_get_interface(tp->intf);
5317 	if (res < 0 || test_bit(RTL8152_UNPLUG, &tp->flags)) {
5318 		rtl_drop_queued_tx(tp);
5319 		rtl_stop_rx(tp);
5320 	} else {
5321 		mutex_lock(&tp->control);
5322 
5323 		tp->rtl_ops.down(tp);
5324 
5325 		mutex_unlock(&tp->control);
5326 
5327 		usb_autopm_put_interface(tp->intf);
5328 	}
5329 
5330 	free_all_mem(tp);
5331 
5332 	return res;
5333 }
5334 
5335 static void rtl_tally_reset(struct r8152 *tp)
5336 {
5337 	u32 ocp_data;
5338 
5339 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY);
5340 	ocp_data |= TALLY_RESET;
5341 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY, ocp_data);
5342 }
5343 
5344 static void r8152b_init(struct r8152 *tp)
5345 {
5346 	u32 ocp_data;
5347 	u16 data;
5348 
5349 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
5350 		return;
5351 
5352 	data = r8152_mdio_read(tp, MII_BMCR);
5353 	if (data & BMCR_PDOWN) {
5354 		data &= ~BMCR_PDOWN;
5355 		r8152_mdio_write(tp, MII_BMCR, data);
5356 	}
5357 
5358 	r8152_aldps_en(tp, false);
5359 
5360 	if (tp->version == RTL_VER_01) {
5361 		ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
5362 		ocp_data &= ~LED_MODE_MASK;
5363 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
5364 	}
5365 
5366 	r8152_power_cut_en(tp, false);
5367 
5368 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
5369 	ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
5370 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
5371 	ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
5372 	ocp_data &= ~MCU_CLK_RATIO_MASK;
5373 	ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
5374 	ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
5375 	ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
5376 		   SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
5377 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
5378 
5379 	rtl_tally_reset(tp);
5380 
5381 	/* enable rx aggregation */
5382 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
5383 	ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
5384 	ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
5385 }
5386 
5387 static void r8153_init(struct r8152 *tp)
5388 {
5389 	u32 ocp_data;
5390 	u16 data;
5391 	int i;
5392 
5393 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
5394 		return;
5395 
5396 	r8153_u1u2en(tp, false);
5397 
5398 	for (i = 0; i < 500; i++) {
5399 		if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
5400 		    AUTOLOAD_DONE)
5401 			break;
5402 		msleep(20);
5403 	}
5404 
5405 	data = r8153_phy_status(tp, 0);
5406 
5407 	if (tp->version == RTL_VER_03 || tp->version == RTL_VER_04 ||
5408 	    tp->version == RTL_VER_05)
5409 		ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L);
5410 
5411 	data = r8152_mdio_read(tp, MII_BMCR);
5412 	if (data & BMCR_PDOWN) {
5413 		data &= ~BMCR_PDOWN;
5414 		r8152_mdio_write(tp, MII_BMCR, data);
5415 	}
5416 
5417 	data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
5418 
5419 	r8153_u2p3en(tp, false);
5420 
5421 	if (tp->version == RTL_VER_04) {
5422 		ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2);
5423 		ocp_data &= ~pwd_dn_scale_mask;
5424 		ocp_data |= pwd_dn_scale(96);
5425 		ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2, ocp_data);
5426 
5427 		ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
5428 		ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
5429 		ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
5430 	} else if (tp->version == RTL_VER_05) {
5431 		ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0);
5432 		ocp_data &= ~ECM_ALDPS;
5433 		ocp_write_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0, ocp_data);
5434 
5435 		ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
5436 		if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
5437 			ocp_data &= ~DYNAMIC_BURST;
5438 		else
5439 			ocp_data |= DYNAMIC_BURST;
5440 		ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
5441 	} else if (tp->version == RTL_VER_06) {
5442 		ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
5443 		if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
5444 			ocp_data &= ~DYNAMIC_BURST;
5445 		else
5446 			ocp_data |= DYNAMIC_BURST;
5447 		ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
5448 
5449 		r8153_queue_wake(tp, false);
5450 
5451 		ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
5452 		if (rtl8152_get_speed(tp) & LINK_STATUS)
5453 			ocp_data |= CUR_LINK_OK;
5454 		else
5455 			ocp_data &= ~CUR_LINK_OK;
5456 		ocp_data |= POLL_LINK_CHG;
5457 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
5458 	}
5459 
5460 	ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2);
5461 	ocp_data |= EP4_FULL_FC;
5462 	ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2, ocp_data);
5463 
5464 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL);
5465 	ocp_data &= ~TIMER11_EN;
5466 	ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data);
5467 
5468 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
5469 	ocp_data &= ~LED_MODE_MASK;
5470 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
5471 
5472 	ocp_data = FIFO_EMPTY_1FB | ROK_EXIT_LPM;
5473 	if (tp->version == RTL_VER_04 && tp->udev->speed < USB_SPEED_SUPER)
5474 		ocp_data |= LPM_TIMER_500MS;
5475 	else
5476 		ocp_data |= LPM_TIMER_500US;
5477 	ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data);
5478 
5479 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2);
5480 	ocp_data &= ~SEN_VAL_MASK;
5481 	ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE;
5482 	ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data);
5483 
5484 	ocp_write_word(tp, MCU_TYPE_USB, USB_CONNECT_TIMER, 0x0001);
5485 
5486 	r8153_power_cut_en(tp, false);
5487 	rtl_runtime_suspend_enable(tp, false);
5488 	r8153_u1u2en(tp, true);
5489 	r8153_mac_clk_spd(tp, false);
5490 	usb_enable_lpm(tp->udev);
5491 
5492 	ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6);
5493 	ocp_data |= LANWAKE_CLR_EN;
5494 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6, ocp_data);
5495 
5496 	ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG);
5497 	ocp_data &= ~LANWAKE_PIN;
5498 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG, ocp_data);
5499 
5500 	/* rx aggregation */
5501 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
5502 	ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
5503 	if (test_bit(DELL_TB_RX_AGG_BUG, &tp->flags))
5504 		ocp_data |= RX_AGG_DISABLE;
5505 
5506 	ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
5507 
5508 	rtl_tally_reset(tp);
5509 
5510 	switch (tp->udev->speed) {
5511 	case USB_SPEED_SUPER:
5512 	case USB_SPEED_SUPER_PLUS:
5513 		tp->coalesce = COALESCE_SUPER;
5514 		break;
5515 	case USB_SPEED_HIGH:
5516 		tp->coalesce = COALESCE_HIGH;
5517 		break;
5518 	default:
5519 		tp->coalesce = COALESCE_SLOW;
5520 		break;
5521 	}
5522 }
5523 
5524 static void r8153b_init(struct r8152 *tp)
5525 {
5526 	u32 ocp_data;
5527 	u16 data;
5528 	int i;
5529 
5530 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
5531 		return;
5532 
5533 	r8153b_u1u2en(tp, false);
5534 
5535 	for (i = 0; i < 500; i++) {
5536 		if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
5537 		    AUTOLOAD_DONE)
5538 			break;
5539 		msleep(20);
5540 	}
5541 
5542 	data = r8153_phy_status(tp, 0);
5543 
5544 	data = r8152_mdio_read(tp, MII_BMCR);
5545 	if (data & BMCR_PDOWN) {
5546 		data &= ~BMCR_PDOWN;
5547 		r8152_mdio_write(tp, MII_BMCR, data);
5548 	}
5549 
5550 	data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
5551 
5552 	r8153_u2p3en(tp, false);
5553 
5554 	/* MSC timer = 0xfff * 8ms = 32760 ms */
5555 	ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff);
5556 
5557 	/* U1/U2/L1 idle timer. 500 us */
5558 	ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500);
5559 
5560 	r8153b_power_cut_en(tp, false);
5561 	r8153b_ups_en(tp, false);
5562 	r8153_queue_wake(tp, false);
5563 	rtl_runtime_suspend_enable(tp, false);
5564 
5565 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
5566 	if (rtl8152_get_speed(tp) & LINK_STATUS)
5567 		ocp_data |= CUR_LINK_OK;
5568 	else
5569 		ocp_data &= ~CUR_LINK_OK;
5570 	ocp_data |= POLL_LINK_CHG;
5571 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
5572 
5573 	if (tp->udev->speed != USB_SPEED_HIGH)
5574 		r8153b_u1u2en(tp, true);
5575 	usb_enable_lpm(tp->udev);
5576 
5577 	/* MAC clock speed down */
5578 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2);
5579 	ocp_data |= MAC_CLK_SPDWN_EN;
5580 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data);
5581 
5582 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
5583 	ocp_data &= ~PLA_MCU_SPDWN_EN;
5584 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
5585 
5586 	if (tp->version == RTL_VER_09) {
5587 		/* Disable Test IO for 32QFN */
5588 		if (ocp_read_byte(tp, MCU_TYPE_PLA, 0xdc00) & BIT(5)) {
5589 			ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
5590 			ocp_data |= TEST_IO_OFF;
5591 			ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
5592 		}
5593 	}
5594 
5595 	set_bit(GREEN_ETHERNET, &tp->flags);
5596 
5597 	/* rx aggregation */
5598 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
5599 	ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
5600 	ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
5601 
5602 	rtl_tally_reset(tp);
5603 
5604 	tp->coalesce = 15000;	/* 15 us */
5605 }
5606 
5607 static int rtl8152_pre_reset(struct usb_interface *intf)
5608 {
5609 	struct r8152 *tp = usb_get_intfdata(intf);
5610 	struct net_device *netdev;
5611 
5612 	if (!tp)
5613 		return 0;
5614 
5615 	netdev = tp->netdev;
5616 	if (!netif_running(netdev))
5617 		return 0;
5618 
5619 	netif_stop_queue(netdev);
5620 	tasklet_disable(&tp->tx_tl);
5621 	clear_bit(WORK_ENABLE, &tp->flags);
5622 	usb_kill_urb(tp->intr_urb);
5623 	cancel_delayed_work_sync(&tp->schedule);
5624 	napi_disable(&tp->napi);
5625 	if (netif_carrier_ok(netdev)) {
5626 		mutex_lock(&tp->control);
5627 		tp->rtl_ops.disable(tp);
5628 		mutex_unlock(&tp->control);
5629 	}
5630 
5631 	return 0;
5632 }
5633 
5634 static int rtl8152_post_reset(struct usb_interface *intf)
5635 {
5636 	struct r8152 *tp = usb_get_intfdata(intf);
5637 	struct net_device *netdev;
5638 	struct sockaddr sa;
5639 
5640 	if (!tp)
5641 		return 0;
5642 
5643 	/* reset the MAC adddress in case of policy change */
5644 	if (determine_ethernet_addr(tp, &sa) >= 0) {
5645 		rtnl_lock();
5646 		dev_set_mac_address (tp->netdev, &sa, NULL);
5647 		rtnl_unlock();
5648 	}
5649 
5650 	netdev = tp->netdev;
5651 	if (!netif_running(netdev))
5652 		return 0;
5653 
5654 	set_bit(WORK_ENABLE, &tp->flags);
5655 	if (netif_carrier_ok(netdev)) {
5656 		mutex_lock(&tp->control);
5657 		tp->rtl_ops.enable(tp);
5658 		rtl_start_rx(tp);
5659 		_rtl8152_set_rx_mode(netdev);
5660 		mutex_unlock(&tp->control);
5661 	}
5662 
5663 	napi_enable(&tp->napi);
5664 	tasklet_enable(&tp->tx_tl);
5665 	netif_wake_queue(netdev);
5666 	usb_submit_urb(tp->intr_urb, GFP_KERNEL);
5667 
5668 	if (!list_empty(&tp->rx_done))
5669 		napi_schedule(&tp->napi);
5670 
5671 	return 0;
5672 }
5673 
5674 static bool delay_autosuspend(struct r8152 *tp)
5675 {
5676 	bool sw_linking = !!netif_carrier_ok(tp->netdev);
5677 	bool hw_linking = !!(rtl8152_get_speed(tp) & LINK_STATUS);
5678 
5679 	/* This means a linking change occurs and the driver doesn't detect it,
5680 	 * yet. If the driver has disabled tx/rx and hw is linking on, the
5681 	 * device wouldn't wake up by receiving any packet.
5682 	 */
5683 	if (work_busy(&tp->schedule.work) || sw_linking != hw_linking)
5684 		return true;
5685 
5686 	/* If the linking down is occurred by nway, the device may miss the
5687 	 * linking change event. And it wouldn't wake when linking on.
5688 	 */
5689 	if (!sw_linking && tp->rtl_ops.in_nway(tp))
5690 		return true;
5691 	else if (!skb_queue_empty(&tp->tx_queue))
5692 		return true;
5693 	else
5694 		return false;
5695 }
5696 
5697 static int rtl8152_runtime_resume(struct r8152 *tp)
5698 {
5699 	struct net_device *netdev = tp->netdev;
5700 
5701 	if (netif_running(netdev) && netdev->flags & IFF_UP) {
5702 		struct napi_struct *napi = &tp->napi;
5703 
5704 		tp->rtl_ops.autosuspend_en(tp, false);
5705 		napi_disable(napi);
5706 		set_bit(WORK_ENABLE, &tp->flags);
5707 
5708 		if (netif_carrier_ok(netdev)) {
5709 			if (rtl8152_get_speed(tp) & LINK_STATUS) {
5710 				rtl_start_rx(tp);
5711 			} else {
5712 				netif_carrier_off(netdev);
5713 				tp->rtl_ops.disable(tp);
5714 				netif_info(tp, link, netdev, "linking down\n");
5715 			}
5716 		}
5717 
5718 		napi_enable(napi);
5719 		clear_bit(SELECTIVE_SUSPEND, &tp->flags);
5720 		smp_mb__after_atomic();
5721 
5722 		if (!list_empty(&tp->rx_done))
5723 			napi_schedule(&tp->napi);
5724 
5725 		usb_submit_urb(tp->intr_urb, GFP_NOIO);
5726 	} else {
5727 		if (netdev->flags & IFF_UP)
5728 			tp->rtl_ops.autosuspend_en(tp, false);
5729 
5730 		clear_bit(SELECTIVE_SUSPEND, &tp->flags);
5731 	}
5732 
5733 	return 0;
5734 }
5735 
5736 static int rtl8152_system_resume(struct r8152 *tp)
5737 {
5738 	struct net_device *netdev = tp->netdev;
5739 
5740 	netif_device_attach(netdev);
5741 
5742 	if (netif_running(netdev) && (netdev->flags & IFF_UP)) {
5743 		tp->rtl_ops.up(tp);
5744 		netif_carrier_off(netdev);
5745 		set_bit(WORK_ENABLE, &tp->flags);
5746 		usb_submit_urb(tp->intr_urb, GFP_NOIO);
5747 	}
5748 
5749 	return 0;
5750 }
5751 
5752 static int rtl8152_runtime_suspend(struct r8152 *tp)
5753 {
5754 	struct net_device *netdev = tp->netdev;
5755 	int ret = 0;
5756 
5757 	set_bit(SELECTIVE_SUSPEND, &tp->flags);
5758 	smp_mb__after_atomic();
5759 
5760 	if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
5761 		u32 rcr = 0;
5762 
5763 		if (netif_carrier_ok(netdev)) {
5764 			u32 ocp_data;
5765 
5766 			rcr = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
5767 			ocp_data = rcr & ~RCR_ACPT_ALL;
5768 			ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
5769 			rxdy_gated_en(tp, true);
5770 			ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA,
5771 						 PLA_OOB_CTRL);
5772 			if (!(ocp_data & RXFIFO_EMPTY)) {
5773 				rxdy_gated_en(tp, false);
5774 				ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr);
5775 				clear_bit(SELECTIVE_SUSPEND, &tp->flags);
5776 				smp_mb__after_atomic();
5777 				ret = -EBUSY;
5778 				goto out1;
5779 			}
5780 		}
5781 
5782 		clear_bit(WORK_ENABLE, &tp->flags);
5783 		usb_kill_urb(tp->intr_urb);
5784 
5785 		tp->rtl_ops.autosuspend_en(tp, true);
5786 
5787 		if (netif_carrier_ok(netdev)) {
5788 			struct napi_struct *napi = &tp->napi;
5789 
5790 			napi_disable(napi);
5791 			rtl_stop_rx(tp);
5792 			rxdy_gated_en(tp, false);
5793 			ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr);
5794 			napi_enable(napi);
5795 		}
5796 
5797 		if (delay_autosuspend(tp)) {
5798 			rtl8152_runtime_resume(tp);
5799 			ret = -EBUSY;
5800 		}
5801 	}
5802 
5803 out1:
5804 	return ret;
5805 }
5806 
5807 static int rtl8152_system_suspend(struct r8152 *tp)
5808 {
5809 	struct net_device *netdev = tp->netdev;
5810 
5811 	netif_device_detach(netdev);
5812 
5813 	if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
5814 		struct napi_struct *napi = &tp->napi;
5815 
5816 		clear_bit(WORK_ENABLE, &tp->flags);
5817 		usb_kill_urb(tp->intr_urb);
5818 		tasklet_disable(&tp->tx_tl);
5819 		napi_disable(napi);
5820 		cancel_delayed_work_sync(&tp->schedule);
5821 		tp->rtl_ops.down(tp);
5822 		napi_enable(napi);
5823 		tasklet_enable(&tp->tx_tl);
5824 	}
5825 
5826 	return 0;
5827 }
5828 
5829 static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
5830 {
5831 	struct r8152 *tp = usb_get_intfdata(intf);
5832 	int ret;
5833 
5834 	mutex_lock(&tp->control);
5835 
5836 	if (PMSG_IS_AUTO(message))
5837 		ret = rtl8152_runtime_suspend(tp);
5838 	else
5839 		ret = rtl8152_system_suspend(tp);
5840 
5841 	mutex_unlock(&tp->control);
5842 
5843 	return ret;
5844 }
5845 
5846 static int rtl8152_resume(struct usb_interface *intf)
5847 {
5848 	struct r8152 *tp = usb_get_intfdata(intf);
5849 	int ret;
5850 
5851 	mutex_lock(&tp->control);
5852 
5853 	if (test_bit(SELECTIVE_SUSPEND, &tp->flags))
5854 		ret = rtl8152_runtime_resume(tp);
5855 	else
5856 		ret = rtl8152_system_resume(tp);
5857 
5858 	mutex_unlock(&tp->control);
5859 
5860 	return ret;
5861 }
5862 
5863 static int rtl8152_reset_resume(struct usb_interface *intf)
5864 {
5865 	struct r8152 *tp = usb_get_intfdata(intf);
5866 
5867 	clear_bit(SELECTIVE_SUSPEND, &tp->flags);
5868 	tp->rtl_ops.init(tp);
5869 	queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0);
5870 	set_ethernet_addr(tp);
5871 	return rtl8152_resume(intf);
5872 }
5873 
5874 static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
5875 {
5876 	struct r8152 *tp = netdev_priv(dev);
5877 
5878 	if (usb_autopm_get_interface(tp->intf) < 0)
5879 		return;
5880 
5881 	if (!rtl_can_wakeup(tp)) {
5882 		wol->supported = 0;
5883 		wol->wolopts = 0;
5884 	} else {
5885 		mutex_lock(&tp->control);
5886 		wol->supported = WAKE_ANY;
5887 		wol->wolopts = __rtl_get_wol(tp);
5888 		mutex_unlock(&tp->control);
5889 	}
5890 
5891 	usb_autopm_put_interface(tp->intf);
5892 }
5893 
5894 static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
5895 {
5896 	struct r8152 *tp = netdev_priv(dev);
5897 	int ret;
5898 
5899 	if (!rtl_can_wakeup(tp))
5900 		return -EOPNOTSUPP;
5901 
5902 	if (wol->wolopts & ~WAKE_ANY)
5903 		return -EINVAL;
5904 
5905 	ret = usb_autopm_get_interface(tp->intf);
5906 	if (ret < 0)
5907 		goto out_set_wol;
5908 
5909 	mutex_lock(&tp->control);
5910 
5911 	__rtl_set_wol(tp, wol->wolopts);
5912 	tp->saved_wolopts = wol->wolopts & WAKE_ANY;
5913 
5914 	mutex_unlock(&tp->control);
5915 
5916 	usb_autopm_put_interface(tp->intf);
5917 
5918 out_set_wol:
5919 	return ret;
5920 }
5921 
5922 static u32 rtl8152_get_msglevel(struct net_device *dev)
5923 {
5924 	struct r8152 *tp = netdev_priv(dev);
5925 
5926 	return tp->msg_enable;
5927 }
5928 
5929 static void rtl8152_set_msglevel(struct net_device *dev, u32 value)
5930 {
5931 	struct r8152 *tp = netdev_priv(dev);
5932 
5933 	tp->msg_enable = value;
5934 }
5935 
5936 static void rtl8152_get_drvinfo(struct net_device *netdev,
5937 				struct ethtool_drvinfo *info)
5938 {
5939 	struct r8152 *tp = netdev_priv(netdev);
5940 
5941 	strlcpy(info->driver, MODULENAME, sizeof(info->driver));
5942 	strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
5943 	usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
5944 	if (!IS_ERR_OR_NULL(tp->rtl_fw.fw))
5945 		strlcpy(info->fw_version, tp->rtl_fw.version,
5946 			sizeof(info->fw_version));
5947 }
5948 
5949 static
5950 int rtl8152_get_link_ksettings(struct net_device *netdev,
5951 			       struct ethtool_link_ksettings *cmd)
5952 {
5953 	struct r8152 *tp = netdev_priv(netdev);
5954 	int ret;
5955 
5956 	if (!tp->mii.mdio_read)
5957 		return -EOPNOTSUPP;
5958 
5959 	ret = usb_autopm_get_interface(tp->intf);
5960 	if (ret < 0)
5961 		goto out;
5962 
5963 	mutex_lock(&tp->control);
5964 
5965 	mii_ethtool_get_link_ksettings(&tp->mii, cmd);
5966 
5967 	mutex_unlock(&tp->control);
5968 
5969 	usb_autopm_put_interface(tp->intf);
5970 
5971 out:
5972 	return ret;
5973 }
5974 
5975 static int rtl8152_set_link_ksettings(struct net_device *dev,
5976 				      const struct ethtool_link_ksettings *cmd)
5977 {
5978 	struct r8152 *tp = netdev_priv(dev);
5979 	u32 advertising = 0;
5980 	int ret;
5981 
5982 	ret = usb_autopm_get_interface(tp->intf);
5983 	if (ret < 0)
5984 		goto out;
5985 
5986 	if (test_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT,
5987 		     cmd->link_modes.advertising))
5988 		advertising |= RTL_ADVERTISED_10_HALF;
5989 
5990 	if (test_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT,
5991 		     cmd->link_modes.advertising))
5992 		advertising |= RTL_ADVERTISED_10_FULL;
5993 
5994 	if (test_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT,
5995 		     cmd->link_modes.advertising))
5996 		advertising |= RTL_ADVERTISED_100_HALF;
5997 
5998 	if (test_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
5999 		     cmd->link_modes.advertising))
6000 		advertising |= RTL_ADVERTISED_100_FULL;
6001 
6002 	if (test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
6003 		     cmd->link_modes.advertising))
6004 		advertising |= RTL_ADVERTISED_1000_HALF;
6005 
6006 	if (test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
6007 		     cmd->link_modes.advertising))
6008 		advertising |= RTL_ADVERTISED_1000_FULL;
6009 
6010 	mutex_lock(&tp->control);
6011 
6012 	ret = rtl8152_set_speed(tp, cmd->base.autoneg, cmd->base.speed,
6013 				cmd->base.duplex, advertising);
6014 	if (!ret) {
6015 		tp->autoneg = cmd->base.autoneg;
6016 		tp->speed = cmd->base.speed;
6017 		tp->duplex = cmd->base.duplex;
6018 		tp->advertising = advertising;
6019 	}
6020 
6021 	mutex_unlock(&tp->control);
6022 
6023 	usb_autopm_put_interface(tp->intf);
6024 
6025 out:
6026 	return ret;
6027 }
6028 
6029 static const char rtl8152_gstrings[][ETH_GSTRING_LEN] = {
6030 	"tx_packets",
6031 	"rx_packets",
6032 	"tx_errors",
6033 	"rx_errors",
6034 	"rx_missed",
6035 	"align_errors",
6036 	"tx_single_collisions",
6037 	"tx_multi_collisions",
6038 	"rx_unicast",
6039 	"rx_broadcast",
6040 	"rx_multicast",
6041 	"tx_aborted",
6042 	"tx_underrun",
6043 };
6044 
6045 static int rtl8152_get_sset_count(struct net_device *dev, int sset)
6046 {
6047 	switch (sset) {
6048 	case ETH_SS_STATS:
6049 		return ARRAY_SIZE(rtl8152_gstrings);
6050 	default:
6051 		return -EOPNOTSUPP;
6052 	}
6053 }
6054 
6055 static void rtl8152_get_ethtool_stats(struct net_device *dev,
6056 				      struct ethtool_stats *stats, u64 *data)
6057 {
6058 	struct r8152 *tp = netdev_priv(dev);
6059 	struct tally_counter tally;
6060 
6061 	if (usb_autopm_get_interface(tp->intf) < 0)
6062 		return;
6063 
6064 	generic_ocp_read(tp, PLA_TALLYCNT, sizeof(tally), &tally, MCU_TYPE_PLA);
6065 
6066 	usb_autopm_put_interface(tp->intf);
6067 
6068 	data[0] = le64_to_cpu(tally.tx_packets);
6069 	data[1] = le64_to_cpu(tally.rx_packets);
6070 	data[2] = le64_to_cpu(tally.tx_errors);
6071 	data[3] = le32_to_cpu(tally.rx_errors);
6072 	data[4] = le16_to_cpu(tally.rx_missed);
6073 	data[5] = le16_to_cpu(tally.align_errors);
6074 	data[6] = le32_to_cpu(tally.tx_one_collision);
6075 	data[7] = le32_to_cpu(tally.tx_multi_collision);
6076 	data[8] = le64_to_cpu(tally.rx_unicast);
6077 	data[9] = le64_to_cpu(tally.rx_broadcast);
6078 	data[10] = le32_to_cpu(tally.rx_multicast);
6079 	data[11] = le16_to_cpu(tally.tx_aborted);
6080 	data[12] = le16_to_cpu(tally.tx_underrun);
6081 }
6082 
6083 static void rtl8152_get_strings(struct net_device *dev, u32 stringset, u8 *data)
6084 {
6085 	switch (stringset) {
6086 	case ETH_SS_STATS:
6087 		memcpy(data, *rtl8152_gstrings, sizeof(rtl8152_gstrings));
6088 		break;
6089 	}
6090 }
6091 
6092 static int r8152_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
6093 {
6094 	u32 lp, adv, supported = 0;
6095 	u16 val;
6096 
6097 	val = r8152_mmd_read(tp, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
6098 	supported = mmd_eee_cap_to_ethtool_sup_t(val);
6099 
6100 	val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
6101 	adv = mmd_eee_adv_to_ethtool_adv_t(val);
6102 
6103 	val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
6104 	lp = mmd_eee_adv_to_ethtool_adv_t(val);
6105 
6106 	eee->eee_enabled = tp->eee_en;
6107 	eee->eee_active = !!(supported & adv & lp);
6108 	eee->supported = supported;
6109 	eee->advertised = tp->eee_adv;
6110 	eee->lp_advertised = lp;
6111 
6112 	return 0;
6113 }
6114 
6115 static int r8152_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
6116 {
6117 	u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
6118 
6119 	tp->eee_en = eee->eee_enabled;
6120 	tp->eee_adv = val;
6121 
6122 	rtl_eee_enable(tp, tp->eee_en);
6123 
6124 	return 0;
6125 }
6126 
6127 static int r8153_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
6128 {
6129 	u32 lp, adv, supported = 0;
6130 	u16 val;
6131 
6132 	val = ocp_reg_read(tp, OCP_EEE_ABLE);
6133 	supported = mmd_eee_cap_to_ethtool_sup_t(val);
6134 
6135 	val = ocp_reg_read(tp, OCP_EEE_ADV);
6136 	adv = mmd_eee_adv_to_ethtool_adv_t(val);
6137 
6138 	val = ocp_reg_read(tp, OCP_EEE_LPABLE);
6139 	lp = mmd_eee_adv_to_ethtool_adv_t(val);
6140 
6141 	eee->eee_enabled = tp->eee_en;
6142 	eee->eee_active = !!(supported & adv & lp);
6143 	eee->supported = supported;
6144 	eee->advertised = tp->eee_adv;
6145 	eee->lp_advertised = lp;
6146 
6147 	return 0;
6148 }
6149 
6150 static int
6151 rtl_ethtool_get_eee(struct net_device *net, struct ethtool_eee *edata)
6152 {
6153 	struct r8152 *tp = netdev_priv(net);
6154 	int ret;
6155 
6156 	ret = usb_autopm_get_interface(tp->intf);
6157 	if (ret < 0)
6158 		goto out;
6159 
6160 	mutex_lock(&tp->control);
6161 
6162 	ret = tp->rtl_ops.eee_get(tp, edata);
6163 
6164 	mutex_unlock(&tp->control);
6165 
6166 	usb_autopm_put_interface(tp->intf);
6167 
6168 out:
6169 	return ret;
6170 }
6171 
6172 static int
6173 rtl_ethtool_set_eee(struct net_device *net, struct ethtool_eee *edata)
6174 {
6175 	struct r8152 *tp = netdev_priv(net);
6176 	int ret;
6177 
6178 	ret = usb_autopm_get_interface(tp->intf);
6179 	if (ret < 0)
6180 		goto out;
6181 
6182 	mutex_lock(&tp->control);
6183 
6184 	ret = tp->rtl_ops.eee_set(tp, edata);
6185 	if (!ret)
6186 		ret = mii_nway_restart(&tp->mii);
6187 
6188 	mutex_unlock(&tp->control);
6189 
6190 	usb_autopm_put_interface(tp->intf);
6191 
6192 out:
6193 	return ret;
6194 }
6195 
6196 static int rtl8152_nway_reset(struct net_device *dev)
6197 {
6198 	struct r8152 *tp = netdev_priv(dev);
6199 	int ret;
6200 
6201 	ret = usb_autopm_get_interface(tp->intf);
6202 	if (ret < 0)
6203 		goto out;
6204 
6205 	mutex_lock(&tp->control);
6206 
6207 	ret = mii_nway_restart(&tp->mii);
6208 
6209 	mutex_unlock(&tp->control);
6210 
6211 	usb_autopm_put_interface(tp->intf);
6212 
6213 out:
6214 	return ret;
6215 }
6216 
6217 static int rtl8152_get_coalesce(struct net_device *netdev,
6218 				struct ethtool_coalesce *coalesce)
6219 {
6220 	struct r8152 *tp = netdev_priv(netdev);
6221 
6222 	switch (tp->version) {
6223 	case RTL_VER_01:
6224 	case RTL_VER_02:
6225 	case RTL_VER_07:
6226 		return -EOPNOTSUPP;
6227 	default:
6228 		break;
6229 	}
6230 
6231 	coalesce->rx_coalesce_usecs = tp->coalesce;
6232 
6233 	return 0;
6234 }
6235 
6236 static int rtl8152_set_coalesce(struct net_device *netdev,
6237 				struct ethtool_coalesce *coalesce)
6238 {
6239 	struct r8152 *tp = netdev_priv(netdev);
6240 	int ret;
6241 
6242 	switch (tp->version) {
6243 	case RTL_VER_01:
6244 	case RTL_VER_02:
6245 	case RTL_VER_07:
6246 		return -EOPNOTSUPP;
6247 	default:
6248 		break;
6249 	}
6250 
6251 	if (coalesce->rx_coalesce_usecs > COALESCE_SLOW)
6252 		return -EINVAL;
6253 
6254 	ret = usb_autopm_get_interface(tp->intf);
6255 	if (ret < 0)
6256 		return ret;
6257 
6258 	mutex_lock(&tp->control);
6259 
6260 	if (tp->coalesce != coalesce->rx_coalesce_usecs) {
6261 		tp->coalesce = coalesce->rx_coalesce_usecs;
6262 
6263 		if (netif_running(netdev) && netif_carrier_ok(netdev)) {
6264 			netif_stop_queue(netdev);
6265 			napi_disable(&tp->napi);
6266 			tp->rtl_ops.disable(tp);
6267 			tp->rtl_ops.enable(tp);
6268 			rtl_start_rx(tp);
6269 			clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
6270 			_rtl8152_set_rx_mode(netdev);
6271 			napi_enable(&tp->napi);
6272 			netif_wake_queue(netdev);
6273 		}
6274 	}
6275 
6276 	mutex_unlock(&tp->control);
6277 
6278 	usb_autopm_put_interface(tp->intf);
6279 
6280 	return ret;
6281 }
6282 
6283 static int rtl8152_get_tunable(struct net_device *netdev,
6284 			       const struct ethtool_tunable *tunable, void *d)
6285 {
6286 	struct r8152 *tp = netdev_priv(netdev);
6287 
6288 	switch (tunable->id) {
6289 	case ETHTOOL_RX_COPYBREAK:
6290 		*(u32 *)d = tp->rx_copybreak;
6291 		break;
6292 	default:
6293 		return -EOPNOTSUPP;
6294 	}
6295 
6296 	return 0;
6297 }
6298 
6299 static int rtl8152_set_tunable(struct net_device *netdev,
6300 			       const struct ethtool_tunable *tunable,
6301 			       const void *d)
6302 {
6303 	struct r8152 *tp = netdev_priv(netdev);
6304 	u32 val;
6305 
6306 	switch (tunable->id) {
6307 	case ETHTOOL_RX_COPYBREAK:
6308 		val = *(u32 *)d;
6309 		if (val < ETH_ZLEN) {
6310 			netif_err(tp, rx_err, netdev,
6311 				  "Invalid rx copy break value\n");
6312 			return -EINVAL;
6313 		}
6314 
6315 		if (tp->rx_copybreak != val) {
6316 			if (netdev->flags & IFF_UP) {
6317 				mutex_lock(&tp->control);
6318 				napi_disable(&tp->napi);
6319 				tp->rx_copybreak = val;
6320 				napi_enable(&tp->napi);
6321 				mutex_unlock(&tp->control);
6322 			} else {
6323 				tp->rx_copybreak = val;
6324 			}
6325 		}
6326 		break;
6327 	default:
6328 		return -EOPNOTSUPP;
6329 	}
6330 
6331 	return 0;
6332 }
6333 
6334 static void rtl8152_get_ringparam(struct net_device *netdev,
6335 				  struct ethtool_ringparam *ring)
6336 {
6337 	struct r8152 *tp = netdev_priv(netdev);
6338 
6339 	ring->rx_max_pending = RTL8152_RX_MAX_PENDING;
6340 	ring->rx_pending = tp->rx_pending;
6341 }
6342 
6343 static int rtl8152_set_ringparam(struct net_device *netdev,
6344 				 struct ethtool_ringparam *ring)
6345 {
6346 	struct r8152 *tp = netdev_priv(netdev);
6347 
6348 	if (ring->rx_pending < (RTL8152_MAX_RX * 2))
6349 		return -EINVAL;
6350 
6351 	if (tp->rx_pending != ring->rx_pending) {
6352 		if (netdev->flags & IFF_UP) {
6353 			mutex_lock(&tp->control);
6354 			napi_disable(&tp->napi);
6355 			tp->rx_pending = ring->rx_pending;
6356 			napi_enable(&tp->napi);
6357 			mutex_unlock(&tp->control);
6358 		} else {
6359 			tp->rx_pending = ring->rx_pending;
6360 		}
6361 	}
6362 
6363 	return 0;
6364 }
6365 
6366 static const struct ethtool_ops ops = {
6367 	.get_drvinfo = rtl8152_get_drvinfo,
6368 	.get_link = ethtool_op_get_link,
6369 	.nway_reset = rtl8152_nway_reset,
6370 	.get_msglevel = rtl8152_get_msglevel,
6371 	.set_msglevel = rtl8152_set_msglevel,
6372 	.get_wol = rtl8152_get_wol,
6373 	.set_wol = rtl8152_set_wol,
6374 	.get_strings = rtl8152_get_strings,
6375 	.get_sset_count = rtl8152_get_sset_count,
6376 	.get_ethtool_stats = rtl8152_get_ethtool_stats,
6377 	.get_coalesce = rtl8152_get_coalesce,
6378 	.set_coalesce = rtl8152_set_coalesce,
6379 	.get_eee = rtl_ethtool_get_eee,
6380 	.set_eee = rtl_ethtool_set_eee,
6381 	.get_link_ksettings = rtl8152_get_link_ksettings,
6382 	.set_link_ksettings = rtl8152_set_link_ksettings,
6383 	.get_tunable = rtl8152_get_tunable,
6384 	.set_tunable = rtl8152_set_tunable,
6385 	.get_ringparam = rtl8152_get_ringparam,
6386 	.set_ringparam = rtl8152_set_ringparam,
6387 };
6388 
6389 static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
6390 {
6391 	struct r8152 *tp = netdev_priv(netdev);
6392 	struct mii_ioctl_data *data = if_mii(rq);
6393 	int res;
6394 
6395 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
6396 		return -ENODEV;
6397 
6398 	res = usb_autopm_get_interface(tp->intf);
6399 	if (res < 0)
6400 		goto out;
6401 
6402 	switch (cmd) {
6403 	case SIOCGMIIPHY:
6404 		data->phy_id = R8152_PHY_ID; /* Internal PHY */
6405 		break;
6406 
6407 	case SIOCGMIIREG:
6408 		mutex_lock(&tp->control);
6409 		data->val_out = r8152_mdio_read(tp, data->reg_num);
6410 		mutex_unlock(&tp->control);
6411 		break;
6412 
6413 	case SIOCSMIIREG:
6414 		if (!capable(CAP_NET_ADMIN)) {
6415 			res = -EPERM;
6416 			break;
6417 		}
6418 		mutex_lock(&tp->control);
6419 		r8152_mdio_write(tp, data->reg_num, data->val_in);
6420 		mutex_unlock(&tp->control);
6421 		break;
6422 
6423 	default:
6424 		res = -EOPNOTSUPP;
6425 	}
6426 
6427 	usb_autopm_put_interface(tp->intf);
6428 
6429 out:
6430 	return res;
6431 }
6432 
6433 static int rtl8152_change_mtu(struct net_device *dev, int new_mtu)
6434 {
6435 	struct r8152 *tp = netdev_priv(dev);
6436 	int ret;
6437 
6438 	switch (tp->version) {
6439 	case RTL_VER_01:
6440 	case RTL_VER_02:
6441 	case RTL_VER_07:
6442 		dev->mtu = new_mtu;
6443 		return 0;
6444 	default:
6445 		break;
6446 	}
6447 
6448 	ret = usb_autopm_get_interface(tp->intf);
6449 	if (ret < 0)
6450 		return ret;
6451 
6452 	mutex_lock(&tp->control);
6453 
6454 	dev->mtu = new_mtu;
6455 
6456 	if (netif_running(dev)) {
6457 		u32 rms = new_mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
6458 
6459 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, rms);
6460 
6461 		if (netif_carrier_ok(dev))
6462 			r8153_set_rx_early_size(tp);
6463 	}
6464 
6465 	mutex_unlock(&tp->control);
6466 
6467 	usb_autopm_put_interface(tp->intf);
6468 
6469 	return ret;
6470 }
6471 
6472 static const struct net_device_ops rtl8152_netdev_ops = {
6473 	.ndo_open		= rtl8152_open,
6474 	.ndo_stop		= rtl8152_close,
6475 	.ndo_do_ioctl		= rtl8152_ioctl,
6476 	.ndo_start_xmit		= rtl8152_start_xmit,
6477 	.ndo_tx_timeout		= rtl8152_tx_timeout,
6478 	.ndo_set_features	= rtl8152_set_features,
6479 	.ndo_set_rx_mode	= rtl8152_set_rx_mode,
6480 	.ndo_set_mac_address	= rtl8152_set_mac_address,
6481 	.ndo_change_mtu		= rtl8152_change_mtu,
6482 	.ndo_validate_addr	= eth_validate_addr,
6483 	.ndo_features_check	= rtl8152_features_check,
6484 };
6485 
6486 static void rtl8152_unload(struct r8152 *tp)
6487 {
6488 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
6489 		return;
6490 
6491 	if (tp->version != RTL_VER_01)
6492 		r8152_power_cut_en(tp, true);
6493 }
6494 
6495 static void rtl8153_unload(struct r8152 *tp)
6496 {
6497 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
6498 		return;
6499 
6500 	r8153_power_cut_en(tp, false);
6501 }
6502 
6503 static void rtl8153b_unload(struct r8152 *tp)
6504 {
6505 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
6506 		return;
6507 
6508 	r8153b_power_cut_en(tp, false);
6509 }
6510 
6511 static int rtl_ops_init(struct r8152 *tp)
6512 {
6513 	struct rtl_ops *ops = &tp->rtl_ops;
6514 	int ret = 0;
6515 
6516 	switch (tp->version) {
6517 	case RTL_VER_01:
6518 	case RTL_VER_02:
6519 	case RTL_VER_07:
6520 		ops->init		= r8152b_init;
6521 		ops->enable		= rtl8152_enable;
6522 		ops->disable		= rtl8152_disable;
6523 		ops->up			= rtl8152_up;
6524 		ops->down		= rtl8152_down;
6525 		ops->unload		= rtl8152_unload;
6526 		ops->eee_get		= r8152_get_eee;
6527 		ops->eee_set		= r8152_set_eee;
6528 		ops->in_nway		= rtl8152_in_nway;
6529 		ops->hw_phy_cfg		= r8152b_hw_phy_cfg;
6530 		ops->autosuspend_en	= rtl_runtime_suspend_enable;
6531 		tp->rx_buf_sz		= 16 * 1024;
6532 		tp->eee_en		= true;
6533 		tp->eee_adv		= MDIO_EEE_100TX;
6534 		break;
6535 
6536 	case RTL_VER_03:
6537 	case RTL_VER_04:
6538 	case RTL_VER_05:
6539 	case RTL_VER_06:
6540 		ops->init		= r8153_init;
6541 		ops->enable		= rtl8153_enable;
6542 		ops->disable		= rtl8153_disable;
6543 		ops->up			= rtl8153_up;
6544 		ops->down		= rtl8153_down;
6545 		ops->unload		= rtl8153_unload;
6546 		ops->eee_get		= r8153_get_eee;
6547 		ops->eee_set		= r8152_set_eee;
6548 		ops->in_nway		= rtl8153_in_nway;
6549 		ops->hw_phy_cfg		= r8153_hw_phy_cfg;
6550 		ops->autosuspend_en	= rtl8153_runtime_enable;
6551 		tp->rx_buf_sz		= 32 * 1024;
6552 		tp->eee_en		= true;
6553 		tp->eee_adv		= MDIO_EEE_1000T | MDIO_EEE_100TX;
6554 		break;
6555 
6556 	case RTL_VER_08:
6557 	case RTL_VER_09:
6558 		ops->init		= r8153b_init;
6559 		ops->enable		= rtl8153_enable;
6560 		ops->disable		= rtl8153_disable;
6561 		ops->up			= rtl8153b_up;
6562 		ops->down		= rtl8153b_down;
6563 		ops->unload		= rtl8153b_unload;
6564 		ops->eee_get		= r8153_get_eee;
6565 		ops->eee_set		= r8152_set_eee;
6566 		ops->in_nway		= rtl8153_in_nway;
6567 		ops->hw_phy_cfg		= r8153b_hw_phy_cfg;
6568 		ops->autosuspend_en	= rtl8153b_runtime_enable;
6569 		tp->rx_buf_sz		= 32 * 1024;
6570 		tp->eee_en		= true;
6571 		tp->eee_adv		= MDIO_EEE_1000T | MDIO_EEE_100TX;
6572 		break;
6573 
6574 	default:
6575 		ret = -ENODEV;
6576 		netif_err(tp, probe, tp->netdev, "Unknown Device\n");
6577 		break;
6578 	}
6579 
6580 	return ret;
6581 }
6582 
6583 #define FIRMWARE_8153A_2	"rtl_nic/rtl8153a-2.fw"
6584 #define FIRMWARE_8153A_3	"rtl_nic/rtl8153a-3.fw"
6585 #define FIRMWARE_8153A_4	"rtl_nic/rtl8153a-4.fw"
6586 #define FIRMWARE_8153B_2	"rtl_nic/rtl8153b-2.fw"
6587 
6588 MODULE_FIRMWARE(FIRMWARE_8153A_2);
6589 MODULE_FIRMWARE(FIRMWARE_8153A_3);
6590 MODULE_FIRMWARE(FIRMWARE_8153A_4);
6591 MODULE_FIRMWARE(FIRMWARE_8153B_2);
6592 
6593 static int rtl_fw_init(struct r8152 *tp)
6594 {
6595 	struct rtl_fw *rtl_fw = &tp->rtl_fw;
6596 
6597 	switch (tp->version) {
6598 	case RTL_VER_04:
6599 		rtl_fw->fw_name		= FIRMWARE_8153A_2;
6600 		rtl_fw->pre_fw		= r8153_pre_firmware_1;
6601 		rtl_fw->post_fw		= r8153_post_firmware_1;
6602 		break;
6603 	case RTL_VER_05:
6604 		rtl_fw->fw_name		= FIRMWARE_8153A_3;
6605 		rtl_fw->pre_fw		= r8153_pre_firmware_2;
6606 		rtl_fw->post_fw		= r8153_post_firmware_2;
6607 		break;
6608 	case RTL_VER_06:
6609 		rtl_fw->fw_name		= FIRMWARE_8153A_4;
6610 		rtl_fw->post_fw		= r8153_post_firmware_3;
6611 		break;
6612 	case RTL_VER_09:
6613 		rtl_fw->fw_name		= FIRMWARE_8153B_2;
6614 		rtl_fw->pre_fw		= r8153b_pre_firmware_1;
6615 		rtl_fw->post_fw		= r8153b_post_firmware_1;
6616 		break;
6617 	default:
6618 		break;
6619 	}
6620 
6621 	return 0;
6622 }
6623 
6624 static u8 rtl_get_version(struct usb_interface *intf)
6625 {
6626 	struct usb_device *udev = interface_to_usbdev(intf);
6627 	u32 ocp_data = 0;
6628 	__le32 *tmp;
6629 	u8 version;
6630 	int ret;
6631 
6632 	tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
6633 	if (!tmp)
6634 		return 0;
6635 
6636 	ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
6637 			      RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
6638 			      PLA_TCR0, MCU_TYPE_PLA, tmp, sizeof(*tmp), 500);
6639 	if (ret > 0)
6640 		ocp_data = (__le32_to_cpu(*tmp) >> 16) & VERSION_MASK;
6641 
6642 	kfree(tmp);
6643 
6644 	switch (ocp_data) {
6645 	case 0x4c00:
6646 		version = RTL_VER_01;
6647 		break;
6648 	case 0x4c10:
6649 		version = RTL_VER_02;
6650 		break;
6651 	case 0x5c00:
6652 		version = RTL_VER_03;
6653 		break;
6654 	case 0x5c10:
6655 		version = RTL_VER_04;
6656 		break;
6657 	case 0x5c20:
6658 		version = RTL_VER_05;
6659 		break;
6660 	case 0x5c30:
6661 		version = RTL_VER_06;
6662 		break;
6663 	case 0x4800:
6664 		version = RTL_VER_07;
6665 		break;
6666 	case 0x6000:
6667 		version = RTL_VER_08;
6668 		break;
6669 	case 0x6010:
6670 		version = RTL_VER_09;
6671 		break;
6672 	default:
6673 		version = RTL_VER_UNKNOWN;
6674 		dev_info(&intf->dev, "Unknown version 0x%04x\n", ocp_data);
6675 		break;
6676 	}
6677 
6678 	dev_dbg(&intf->dev, "Detected version 0x%04x\n", version);
6679 
6680 	return version;
6681 }
6682 
6683 static int rtl8152_probe(struct usb_interface *intf,
6684 			 const struct usb_device_id *id)
6685 {
6686 	struct usb_device *udev = interface_to_usbdev(intf);
6687 	u8 version = rtl_get_version(intf);
6688 	struct r8152 *tp;
6689 	struct net_device *netdev;
6690 	int ret;
6691 
6692 	if (version == RTL_VER_UNKNOWN)
6693 		return -ENODEV;
6694 
6695 	if (udev->actconfig->desc.bConfigurationValue != 1) {
6696 		usb_driver_set_configuration(udev, 1);
6697 		return -ENODEV;
6698 	}
6699 
6700 	if (intf->cur_altsetting->desc.bNumEndpoints < 3)
6701 		return -ENODEV;
6702 
6703 	usb_reset_device(udev);
6704 	netdev = alloc_etherdev(sizeof(struct r8152));
6705 	if (!netdev) {
6706 		dev_err(&intf->dev, "Out of memory\n");
6707 		return -ENOMEM;
6708 	}
6709 
6710 	SET_NETDEV_DEV(netdev, &intf->dev);
6711 	tp = netdev_priv(netdev);
6712 	tp->msg_enable = 0x7FFF;
6713 
6714 	tp->udev = udev;
6715 	tp->netdev = netdev;
6716 	tp->intf = intf;
6717 	tp->version = version;
6718 
6719 	switch (version) {
6720 	case RTL_VER_01:
6721 	case RTL_VER_02:
6722 	case RTL_VER_07:
6723 		tp->mii.supports_gmii = 0;
6724 		break;
6725 	default:
6726 		tp->mii.supports_gmii = 1;
6727 		break;
6728 	}
6729 
6730 	ret = rtl_ops_init(tp);
6731 	if (ret)
6732 		goto out;
6733 
6734 	rtl_fw_init(tp);
6735 
6736 	mutex_init(&tp->control);
6737 	INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
6738 	INIT_DELAYED_WORK(&tp->hw_phy_work, rtl_hw_phy_work_func_t);
6739 	tasklet_init(&tp->tx_tl, bottom_half, (unsigned long)tp);
6740 	tasklet_disable(&tp->tx_tl);
6741 
6742 	netdev->netdev_ops = &rtl8152_netdev_ops;
6743 	netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
6744 
6745 	netdev->features |= NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
6746 			    NETIF_F_TSO | NETIF_F_FRAGLIST | NETIF_F_IPV6_CSUM |
6747 			    NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_RX |
6748 			    NETIF_F_HW_VLAN_CTAG_TX;
6749 	netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
6750 			      NETIF_F_TSO | NETIF_F_FRAGLIST |
6751 			      NETIF_F_IPV6_CSUM | NETIF_F_TSO6 |
6752 			      NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX;
6753 	netdev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
6754 				NETIF_F_HIGHDMA | NETIF_F_FRAGLIST |
6755 				NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
6756 
6757 	if (tp->version == RTL_VER_01) {
6758 		netdev->features &= ~NETIF_F_RXCSUM;
6759 		netdev->hw_features &= ~NETIF_F_RXCSUM;
6760 	}
6761 
6762 	if (le16_to_cpu(udev->descriptor.idVendor) == VENDOR_ID_LENOVO &&
6763 	    le16_to_cpu(udev->descriptor.idProduct) == 0x3082)
6764 		set_bit(LENOVO_MACPASSTHRU, &tp->flags);
6765 
6766 	if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x3011 && udev->serial &&
6767 	    (!strcmp(udev->serial, "000001000000") ||
6768 	     !strcmp(udev->serial, "000002000000"))) {
6769 		dev_info(&udev->dev, "Dell TB16 Dock, disable RX aggregation");
6770 		set_bit(DELL_TB_RX_AGG_BUG, &tp->flags);
6771 	}
6772 
6773 	netdev->ethtool_ops = &ops;
6774 	netif_set_gso_max_size(netdev, RTL_LIMITED_TSO_SIZE);
6775 
6776 	/* MTU range: 68 - 1500 or 9194 */
6777 	netdev->min_mtu = ETH_MIN_MTU;
6778 	switch (tp->version) {
6779 	case RTL_VER_01:
6780 	case RTL_VER_02:
6781 		netdev->max_mtu = ETH_DATA_LEN;
6782 		break;
6783 	default:
6784 		netdev->max_mtu = RTL8153_MAX_MTU;
6785 		break;
6786 	}
6787 
6788 	tp->mii.dev = netdev;
6789 	tp->mii.mdio_read = read_mii_word;
6790 	tp->mii.mdio_write = write_mii_word;
6791 	tp->mii.phy_id_mask = 0x3f;
6792 	tp->mii.reg_num_mask = 0x1f;
6793 	tp->mii.phy_id = R8152_PHY_ID;
6794 
6795 	tp->autoneg = AUTONEG_ENABLE;
6796 	tp->speed = SPEED_100;
6797 	tp->advertising = RTL_ADVERTISED_10_HALF | RTL_ADVERTISED_10_FULL |
6798 			  RTL_ADVERTISED_100_HALF | RTL_ADVERTISED_100_FULL;
6799 	if (tp->mii.supports_gmii) {
6800 		tp->speed = SPEED_1000;
6801 		tp->advertising |= RTL_ADVERTISED_1000_FULL;
6802 	}
6803 	tp->duplex = DUPLEX_FULL;
6804 
6805 	tp->rx_copybreak = RTL8152_RXFG_HEADSZ;
6806 	tp->rx_pending = 10 * RTL8152_MAX_RX;
6807 
6808 	intf->needs_remote_wakeup = 1;
6809 
6810 	if (!rtl_can_wakeup(tp))
6811 		__rtl_set_wol(tp, 0);
6812 	else
6813 		tp->saved_wolopts = __rtl_get_wol(tp);
6814 
6815 	tp->rtl_ops.init(tp);
6816 #if IS_BUILTIN(CONFIG_USB_RTL8152)
6817 	/* Retry in case request_firmware() is not ready yet. */
6818 	tp->rtl_fw.retry = true;
6819 #endif
6820 	queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0);
6821 	set_ethernet_addr(tp);
6822 
6823 	usb_set_intfdata(intf, tp);
6824 	netif_napi_add(netdev, &tp->napi, r8152_poll, RTL8152_NAPI_WEIGHT);
6825 
6826 	ret = register_netdev(netdev);
6827 	if (ret != 0) {
6828 		netif_err(tp, probe, netdev, "couldn't register the device\n");
6829 		goto out1;
6830 	}
6831 
6832 	if (tp->saved_wolopts)
6833 		device_set_wakeup_enable(&udev->dev, true);
6834 	else
6835 		device_set_wakeup_enable(&udev->dev, false);
6836 
6837 	netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION);
6838 
6839 	return 0;
6840 
6841 out1:
6842 	tasklet_kill(&tp->tx_tl);
6843 	usb_set_intfdata(intf, NULL);
6844 out:
6845 	free_netdev(netdev);
6846 	return ret;
6847 }
6848 
6849 static void rtl8152_disconnect(struct usb_interface *intf)
6850 {
6851 	struct r8152 *tp = usb_get_intfdata(intf);
6852 
6853 	usb_set_intfdata(intf, NULL);
6854 	if (tp) {
6855 		rtl_set_unplug(tp);
6856 
6857 		unregister_netdev(tp->netdev);
6858 		tasklet_kill(&tp->tx_tl);
6859 		cancel_delayed_work_sync(&tp->hw_phy_work);
6860 		tp->rtl_ops.unload(tp);
6861 		rtl8152_release_firmware(tp);
6862 		free_netdev(tp->netdev);
6863 	}
6864 }
6865 
6866 #define REALTEK_USB_DEVICE(vend, prod)	\
6867 	.match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
6868 		       USB_DEVICE_ID_MATCH_INT_CLASS, \
6869 	.idVendor = (vend), \
6870 	.idProduct = (prod), \
6871 	.bInterfaceClass = USB_CLASS_VENDOR_SPEC \
6872 }, \
6873 { \
6874 	.match_flags = USB_DEVICE_ID_MATCH_INT_INFO | \
6875 		       USB_DEVICE_ID_MATCH_DEVICE, \
6876 	.idVendor = (vend), \
6877 	.idProduct = (prod), \
6878 	.bInterfaceClass = USB_CLASS_COMM, \
6879 	.bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \
6880 	.bInterfaceProtocol = USB_CDC_PROTO_NONE
6881 
6882 /* table of devices that work with this driver */
6883 static const struct usb_device_id rtl8152_table[] = {
6884 	{REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8050)},
6885 	{REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8152)},
6886 	{REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8153)},
6887 	{REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07ab)},
6888 	{REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07c6)},
6889 	{REALTEK_USB_DEVICE(VENDOR_ID_SAMSUNG, 0xa101)},
6890 	{REALTEK_USB_DEVICE(VENDOR_ID_LENOVO,  0x304f)},
6891 	{REALTEK_USB_DEVICE(VENDOR_ID_LENOVO,  0x3062)},
6892 	{REALTEK_USB_DEVICE(VENDOR_ID_LENOVO,  0x3069)},
6893 	{REALTEK_USB_DEVICE(VENDOR_ID_LENOVO,  0x3082)},
6894 	{REALTEK_USB_DEVICE(VENDOR_ID_LENOVO,  0x7205)},
6895 	{REALTEK_USB_DEVICE(VENDOR_ID_LENOVO,  0x720c)},
6896 	{REALTEK_USB_DEVICE(VENDOR_ID_LENOVO,  0x7214)},
6897 	{REALTEK_USB_DEVICE(VENDOR_ID_LENOVO,  0xa387)},
6898 	{REALTEK_USB_DEVICE(VENDOR_ID_LINKSYS, 0x0041)},
6899 	{REALTEK_USB_DEVICE(VENDOR_ID_NVIDIA,  0x09ff)},
6900 	{REALTEK_USB_DEVICE(VENDOR_ID_TPLINK,  0x0601)},
6901 	{}
6902 };
6903 
6904 MODULE_DEVICE_TABLE(usb, rtl8152_table);
6905 
6906 static struct usb_driver rtl8152_driver = {
6907 	.name =		MODULENAME,
6908 	.id_table =	rtl8152_table,
6909 	.probe =	rtl8152_probe,
6910 	.disconnect =	rtl8152_disconnect,
6911 	.suspend =	rtl8152_suspend,
6912 	.resume =	rtl8152_resume,
6913 	.reset_resume =	rtl8152_reset_resume,
6914 	.pre_reset =	rtl8152_pre_reset,
6915 	.post_reset =	rtl8152_post_reset,
6916 	.supports_autosuspend = 1,
6917 	.disable_hub_initiated_lpm = 1,
6918 };
6919 
6920 module_usb_driver(rtl8152_driver);
6921 
6922 MODULE_AUTHOR(DRIVER_AUTHOR);
6923 MODULE_DESCRIPTION(DRIVER_DESC);
6924 MODULE_LICENSE("GPL");
6925 MODULE_VERSION(DRIVER_VERSION);
6926