xref: /openbmc/linux/drivers/net/usb/r8152.c (revision a9d85efb)
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 #include <linux/usb/r8152.h>
30 
31 /* Information for net-next */
32 #define NETNEXT_VERSION		"12"
33 
34 /* Information for net */
35 #define NET_VERSION		"11"
36 
37 #define DRIVER_VERSION		"v1." NETNEXT_VERSION "." NET_VERSION
38 #define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>"
39 #define DRIVER_DESC "Realtek RTL8152/RTL8153 Based USB Ethernet Adapters"
40 #define MODULENAME "r8152"
41 
42 #define R8152_PHY_ID		32
43 
44 #define PLA_IDR			0xc000
45 #define PLA_RCR			0xc010
46 #define PLA_RCR1		0xc012
47 #define PLA_RMS			0xc016
48 #define PLA_RXFIFO_CTRL0	0xc0a0
49 #define PLA_RXFIFO_FULL		0xc0a2
50 #define PLA_RXFIFO_CTRL1	0xc0a4
51 #define PLA_RX_FIFO_FULL	0xc0a6
52 #define PLA_RXFIFO_CTRL2	0xc0a8
53 #define PLA_RX_FIFO_EMPTY	0xc0aa
54 #define PLA_DMY_REG0		0xc0b0
55 #define PLA_FMC			0xc0b4
56 #define PLA_CFG_WOL		0xc0b6
57 #define PLA_TEREDO_CFG		0xc0bc
58 #define PLA_TEREDO_WAKE_BASE	0xc0c4
59 #define PLA_MAR			0xcd00
60 #define PLA_BACKUP		0xd000
61 #define PLA_BDC_CR		0xd1a0
62 #define PLA_TEREDO_TIMER	0xd2cc
63 #define PLA_REALWOW_TIMER	0xd2e8
64 #define PLA_UPHY_TIMER		0xd388
65 #define PLA_SUSPEND_FLAG	0xd38a
66 #define PLA_INDICATE_FALG	0xd38c
67 #define PLA_MACDBG_PRE		0xd38c	/* RTL_VER_04 only */
68 #define PLA_MACDBG_POST		0xd38e	/* RTL_VER_04 only */
69 #define PLA_EXTRA_STATUS	0xd398
70 #define PLA_GPHY_CTRL		0xd3ae
71 #define PLA_POL_GPIO_CTRL	0xdc6a
72 #define PLA_EFUSE_DATA		0xdd00
73 #define PLA_EFUSE_CMD		0xdd02
74 #define PLA_LEDSEL		0xdd90
75 #define PLA_LED_FEATURE		0xdd92
76 #define PLA_PHYAR		0xde00
77 #define PLA_BOOT_CTRL		0xe004
78 #define PLA_LWAKE_CTRL_REG	0xe007
79 #define PLA_GPHY_INTR_IMR	0xe022
80 #define PLA_EEE_CR		0xe040
81 #define PLA_EEE_TXTWSYS		0xe04c
82 #define PLA_EEE_TXTWSYS_2P5G	0xe058
83 #define PLA_EEEP_CR		0xe080
84 #define PLA_MAC_PWR_CTRL	0xe0c0
85 #define PLA_MAC_PWR_CTRL2	0xe0ca
86 #define PLA_MAC_PWR_CTRL3	0xe0cc
87 #define PLA_MAC_PWR_CTRL4	0xe0ce
88 #define PLA_WDT6_CTRL		0xe428
89 #define PLA_TCR0		0xe610
90 #define PLA_TCR1		0xe612
91 #define PLA_MTPS		0xe615
92 #define PLA_TXFIFO_CTRL		0xe618
93 #define PLA_TXFIFO_FULL		0xe61a
94 #define PLA_RSTTALLY		0xe800
95 #define PLA_CR			0xe813
96 #define PLA_CRWECR		0xe81c
97 #define PLA_CONFIG12		0xe81e	/* CONFIG1, CONFIG2 */
98 #define PLA_CONFIG34		0xe820	/* CONFIG3, CONFIG4 */
99 #define PLA_CONFIG5		0xe822
100 #define PLA_PHY_PWR		0xe84c
101 #define PLA_OOB_CTRL		0xe84f
102 #define PLA_CPCR		0xe854
103 #define PLA_MISC_0		0xe858
104 #define PLA_MISC_1		0xe85a
105 #define PLA_OCP_GPHY_BASE	0xe86c
106 #define PLA_TALLYCNT		0xe890
107 #define PLA_SFF_STS_7		0xe8de
108 #define PLA_PHYSTATUS		0xe908
109 #define PLA_CONFIG6		0xe90a /* CONFIG6 */
110 #define PLA_USB_CFG		0xe952
111 #define PLA_BP_BA		0xfc26
112 #define PLA_BP_0		0xfc28
113 #define PLA_BP_1		0xfc2a
114 #define PLA_BP_2		0xfc2c
115 #define PLA_BP_3		0xfc2e
116 #define PLA_BP_4		0xfc30
117 #define PLA_BP_5		0xfc32
118 #define PLA_BP_6		0xfc34
119 #define PLA_BP_7		0xfc36
120 #define PLA_BP_EN		0xfc38
121 
122 #define USB_USB2PHY		0xb41e
123 #define USB_SSPHYLINK1		0xb426
124 #define USB_SSPHYLINK2		0xb428
125 #define USB_L1_CTRL		0xb45e
126 #define USB_U2P3_CTRL		0xb460
127 #define USB_CSR_DUMMY1		0xb464
128 #define USB_CSR_DUMMY2		0xb466
129 #define USB_DEV_STAT		0xb808
130 #define USB_CONNECT_TIMER	0xcbf8
131 #define USB_MSC_TIMER		0xcbfc
132 #define USB_BURST_SIZE		0xcfc0
133 #define USB_FW_FIX_EN0		0xcfca
134 #define USB_FW_FIX_EN1		0xcfcc
135 #define USB_LPM_CONFIG		0xcfd8
136 #define USB_ECM_OPTION		0xcfee
137 #define USB_CSTMR		0xcfef	/* RTL8153A */
138 #define USB_MISC_2		0xcfff
139 #define USB_ECM_OP		0xd26b
140 #define USB_GPHY_CTRL		0xd284
141 #define USB_SPEED_OPTION	0xd32a
142 #define USB_FW_CTRL		0xd334	/* RTL8153B */
143 #define USB_FC_TIMER		0xd340
144 #define USB_USB_CTRL		0xd406
145 #define USB_PHY_CTRL		0xd408
146 #define USB_TX_AGG		0xd40a
147 #define USB_RX_BUF_TH		0xd40c
148 #define USB_USB_TIMER		0xd428
149 #define USB_RX_EARLY_TIMEOUT	0xd42c
150 #define USB_RX_EARLY_SIZE	0xd42e
151 #define USB_PM_CTRL_STATUS	0xd432	/* RTL8153A */
152 #define USB_RX_EXTRA_AGGR_TMR	0xd432	/* RTL8153B */
153 #define USB_TX_DMA		0xd434
154 #define USB_UPT_RXDMA_OWN	0xd437
155 #define USB_UPHY3_MDCMDIO	0xd480
156 #define USB_TOLERANCE		0xd490
157 #define USB_LPM_CTRL		0xd41a
158 #define USB_BMU_RESET		0xd4b0
159 #define USB_BMU_CONFIG		0xd4b4
160 #define USB_U1U2_TIMER		0xd4da
161 #define USB_FW_TASK		0xd4e8	/* RTL8153B */
162 #define USB_RX_AGGR_NUM		0xd4ee
163 #define USB_UPS_CTRL		0xd800
164 #define USB_POWER_CUT		0xd80a
165 #define USB_MISC_0		0xd81a
166 #define USB_MISC_1		0xd81f
167 #define USB_AFE_CTRL2		0xd824
168 #define USB_UPHY_XTAL		0xd826
169 #define USB_UPS_CFG		0xd842
170 #define USB_UPS_FLAGS		0xd848
171 #define USB_WDT1_CTRL		0xe404
172 #define USB_WDT11_CTRL		0xe43c
173 #define USB_BP_BA		PLA_BP_BA
174 #define USB_BP_0		PLA_BP_0
175 #define USB_BP_1		PLA_BP_1
176 #define USB_BP_2		PLA_BP_2
177 #define USB_BP_3		PLA_BP_3
178 #define USB_BP_4		PLA_BP_4
179 #define USB_BP_5		PLA_BP_5
180 #define USB_BP_6		PLA_BP_6
181 #define USB_BP_7		PLA_BP_7
182 #define USB_BP_EN		PLA_BP_EN	/* RTL8153A */
183 #define USB_BP_8		0xfc38		/* RTL8153B */
184 #define USB_BP_9		0xfc3a
185 #define USB_BP_10		0xfc3c
186 #define USB_BP_11		0xfc3e
187 #define USB_BP_12		0xfc40
188 #define USB_BP_13		0xfc42
189 #define USB_BP_14		0xfc44
190 #define USB_BP_15		0xfc46
191 #define USB_BP2_EN		0xfc48
192 
193 /* OCP Registers */
194 #define OCP_ALDPS_CONFIG	0x2010
195 #define OCP_EEE_CONFIG1		0x2080
196 #define OCP_EEE_CONFIG2		0x2092
197 #define OCP_EEE_CONFIG3		0x2094
198 #define OCP_BASE_MII		0xa400
199 #define OCP_EEE_AR		0xa41a
200 #define OCP_EEE_DATA		0xa41c
201 #define OCP_PHY_STATUS		0xa420
202 #define OCP_NCTL_CFG		0xa42c
203 #define OCP_POWER_CFG		0xa430
204 #define OCP_EEE_CFG		0xa432
205 #define OCP_SRAM_ADDR		0xa436
206 #define OCP_SRAM_DATA		0xa438
207 #define OCP_DOWN_SPEED		0xa442
208 #define OCP_EEE_ABLE		0xa5c4
209 #define OCP_EEE_ADV		0xa5d0
210 #define OCP_EEE_LPABLE		0xa5d2
211 #define OCP_10GBT_CTRL		0xa5d4
212 #define OCP_10GBT_STAT		0xa5d6
213 #define OCP_EEE_ADV2		0xa6d4
214 #define OCP_PHY_STATE		0xa708		/* nway state for 8153 */
215 #define OCP_PHY_PATCH_STAT	0xb800
216 #define OCP_PHY_PATCH_CMD	0xb820
217 #define OCP_PHY_LOCK		0xb82e
218 #define OCP_ADC_IOFFSET		0xbcfc
219 #define OCP_ADC_CFG		0xbc06
220 #define OCP_SYSCLK_CFG		0xc416
221 
222 /* SRAM Register */
223 #define SRAM_GREEN_CFG		0x8011
224 #define SRAM_LPF_CFG		0x8012
225 #define SRAM_GPHY_FW_VER	0x801e
226 #define SRAM_10M_AMP1		0x8080
227 #define SRAM_10M_AMP2		0x8082
228 #define SRAM_IMPEDANCE		0x8084
229 #define SRAM_PHY_LOCK		0xb82e
230 
231 /* PLA_RCR */
232 #define RCR_AAP			0x00000001
233 #define RCR_APM			0x00000002
234 #define RCR_AM			0x00000004
235 #define RCR_AB			0x00000008
236 #define RCR_ACPT_ALL		(RCR_AAP | RCR_APM | RCR_AM | RCR_AB)
237 #define SLOT_EN			BIT(11)
238 
239 /* PLA_RCR1 */
240 #define OUTER_VLAN		BIT(7)
241 #define INNER_VLAN		BIT(6)
242 
243 /* PLA_RXFIFO_CTRL0 */
244 #define RXFIFO_THR1_NORMAL	0x00080002
245 #define RXFIFO_THR1_OOB		0x01800003
246 
247 /* PLA_RXFIFO_FULL */
248 #define RXFIFO_FULL_MASK	0xfff
249 
250 /* PLA_RXFIFO_CTRL1 */
251 #define RXFIFO_THR2_FULL	0x00000060
252 #define RXFIFO_THR2_HIGH	0x00000038
253 #define RXFIFO_THR2_OOB		0x0000004a
254 #define RXFIFO_THR2_NORMAL	0x00a0
255 
256 /* PLA_RXFIFO_CTRL2 */
257 #define RXFIFO_THR3_FULL	0x00000078
258 #define RXFIFO_THR3_HIGH	0x00000048
259 #define RXFIFO_THR3_OOB		0x0000005a
260 #define RXFIFO_THR3_NORMAL	0x0110
261 
262 /* PLA_TXFIFO_CTRL */
263 #define TXFIFO_THR_NORMAL	0x00400008
264 #define TXFIFO_THR_NORMAL2	0x01000008
265 
266 /* PLA_DMY_REG0 */
267 #define ECM_ALDPS		0x0002
268 
269 /* PLA_FMC */
270 #define FMC_FCR_MCU_EN		0x0001
271 
272 /* PLA_EEEP_CR */
273 #define EEEP_CR_EEEP_TX		0x0002
274 
275 /* PLA_WDT6_CTRL */
276 #define WDT6_SET_MODE		0x0010
277 
278 /* PLA_TCR0 */
279 #define TCR0_TX_EMPTY		0x0800
280 #define TCR0_AUTO_FIFO		0x0080
281 
282 /* PLA_TCR1 */
283 #define VERSION_MASK		0x7cf0
284 #define IFG_MASK		(BIT(3) | BIT(9) | BIT(8))
285 #define IFG_144NS		BIT(9)
286 #define IFG_96NS		(BIT(9) | BIT(8))
287 
288 /* PLA_MTPS */
289 #define MTPS_JUMBO		(12 * 1024 / 64)
290 #define MTPS_DEFAULT		(6 * 1024 / 64)
291 
292 /* PLA_RSTTALLY */
293 #define TALLY_RESET		0x0001
294 
295 /* PLA_CR */
296 #define CR_RST			0x10
297 #define CR_RE			0x08
298 #define CR_TE			0x04
299 
300 /* PLA_CRWECR */
301 #define CRWECR_NORAML		0x00
302 #define CRWECR_CONFIG		0xc0
303 
304 /* PLA_OOB_CTRL */
305 #define NOW_IS_OOB		0x80
306 #define TXFIFO_EMPTY		0x20
307 #define RXFIFO_EMPTY		0x10
308 #define LINK_LIST_READY		0x02
309 #define DIS_MCU_CLROOB		0x01
310 #define FIFO_EMPTY		(TXFIFO_EMPTY | RXFIFO_EMPTY)
311 
312 /* PLA_MISC_1 */
313 #define RXDY_GATED_EN		0x0008
314 
315 /* PLA_SFF_STS_7 */
316 #define RE_INIT_LL		0x8000
317 #define MCU_BORW_EN		0x4000
318 
319 /* PLA_CPCR */
320 #define FLOW_CTRL_EN		BIT(0)
321 #define CPCR_RX_VLAN		0x0040
322 
323 /* PLA_CFG_WOL */
324 #define MAGIC_EN		0x0001
325 
326 /* PLA_TEREDO_CFG */
327 #define TEREDO_SEL		0x8000
328 #define TEREDO_WAKE_MASK	0x7f00
329 #define TEREDO_RS_EVENT_MASK	0x00fe
330 #define OOB_TEREDO_EN		0x0001
331 
332 /* PLA_BDC_CR */
333 #define ALDPS_PROXY_MODE	0x0001
334 
335 /* PLA_EFUSE_CMD */
336 #define EFUSE_READ_CMD		BIT(15)
337 #define EFUSE_DATA_BIT16	BIT(7)
338 
339 /* PLA_CONFIG34 */
340 #define LINK_ON_WAKE_EN		0x0010
341 #define LINK_OFF_WAKE_EN	0x0008
342 
343 /* PLA_CONFIG6 */
344 #define LANWAKE_CLR_EN		BIT(0)
345 
346 /* PLA_USB_CFG */
347 #define EN_XG_LIP		BIT(1)
348 #define EN_G_LIP		BIT(2)
349 
350 /* PLA_CONFIG5 */
351 #define BWF_EN			0x0040
352 #define MWF_EN			0x0020
353 #define UWF_EN			0x0010
354 #define LAN_WAKE_EN		0x0002
355 
356 /* PLA_LED_FEATURE */
357 #define LED_MODE_MASK		0x0700
358 
359 /* PLA_PHY_PWR */
360 #define TX_10M_IDLE_EN		0x0080
361 #define PFM_PWM_SWITCH		0x0040
362 #define TEST_IO_OFF		BIT(4)
363 
364 /* PLA_MAC_PWR_CTRL */
365 #define D3_CLK_GATED_EN		0x00004000
366 #define MCU_CLK_RATIO		0x07010f07
367 #define MCU_CLK_RATIO_MASK	0x0f0f0f0f
368 #define ALDPS_SPDWN_RATIO	0x0f87
369 
370 /* PLA_MAC_PWR_CTRL2 */
371 #define EEE_SPDWN_RATIO		0x8007
372 #define MAC_CLK_SPDWN_EN	BIT(15)
373 #define EEE_SPDWN_RATIO_MASK	0xff
374 
375 /* PLA_MAC_PWR_CTRL3 */
376 #define PLA_MCU_SPDWN_EN	BIT(14)
377 #define PKT_AVAIL_SPDWN_EN	0x0100
378 #define SUSPEND_SPDWN_EN	0x0004
379 #define U1U2_SPDWN_EN		0x0002
380 #define L1_SPDWN_EN		0x0001
381 
382 /* PLA_MAC_PWR_CTRL4 */
383 #define PWRSAVE_SPDWN_EN	0x1000
384 #define RXDV_SPDWN_EN		0x0800
385 #define TX10MIDLE_EN		0x0100
386 #define IDLE_SPDWN_EN		BIT(6)
387 #define TP100_SPDWN_EN		0x0020
388 #define TP500_SPDWN_EN		0x0010
389 #define TP1000_SPDWN_EN		0x0008
390 #define EEE_SPDWN_EN		0x0001
391 
392 /* PLA_GPHY_INTR_IMR */
393 #define GPHY_STS_MSK		0x0001
394 #define SPEED_DOWN_MSK		0x0002
395 #define SPDWN_RXDV_MSK		0x0004
396 #define SPDWN_LINKCHG_MSK	0x0008
397 
398 /* PLA_PHYAR */
399 #define PHYAR_FLAG		0x80000000
400 
401 /* PLA_EEE_CR */
402 #define EEE_RX_EN		0x0001
403 #define EEE_TX_EN		0x0002
404 
405 /* PLA_BOOT_CTRL */
406 #define AUTOLOAD_DONE		0x0002
407 
408 /* PLA_LWAKE_CTRL_REG */
409 #define LANWAKE_PIN		BIT(7)
410 
411 /* PLA_SUSPEND_FLAG */
412 #define LINK_CHG_EVENT		BIT(0)
413 
414 /* PLA_INDICATE_FALG */
415 #define UPCOMING_RUNTIME_D3	BIT(0)
416 
417 /* PLA_MACDBG_PRE and PLA_MACDBG_POST */
418 #define DEBUG_OE		BIT(0)
419 #define DEBUG_LTSSM		0x0082
420 
421 /* PLA_EXTRA_STATUS */
422 #define CUR_LINK_OK		BIT(15)
423 #define U3P3_CHECK_EN		BIT(7)	/* RTL_VER_05 only */
424 #define LINK_CHANGE_FLAG	BIT(8)
425 #define POLL_LINK_CHG		BIT(0)
426 
427 /* PLA_GPHY_CTRL */
428 #define GPHY_FLASH		BIT(1)
429 
430 /* PLA_POL_GPIO_CTRL */
431 #define DACK_DET_EN		BIT(15)
432 #define POL_GPHY_PATCH		BIT(4)
433 
434 /* USB_USB2PHY */
435 #define USB2PHY_SUSPEND		0x0001
436 #define USB2PHY_L1		0x0002
437 
438 /* USB_SSPHYLINK1 */
439 #define DELAY_PHY_PWR_CHG	BIT(1)
440 
441 /* USB_SSPHYLINK2 */
442 #define pwd_dn_scale_mask	0x3ffe
443 #define pwd_dn_scale(x)		((x) << 1)
444 
445 /* USB_CSR_DUMMY1 */
446 #define DYNAMIC_BURST		0x0001
447 
448 /* USB_CSR_DUMMY2 */
449 #define EP4_FULL_FC		0x0001
450 
451 /* USB_DEV_STAT */
452 #define STAT_SPEED_MASK		0x0006
453 #define STAT_SPEED_HIGH		0x0000
454 #define STAT_SPEED_FULL		0x0002
455 
456 /* USB_FW_FIX_EN0 */
457 #define FW_FIX_SUSPEND		BIT(14)
458 
459 /* USB_FW_FIX_EN1 */
460 #define FW_IP_RESET_EN		BIT(9)
461 
462 /* USB_LPM_CONFIG */
463 #define LPM_U1U2_EN		BIT(0)
464 
465 /* USB_TX_AGG */
466 #define TX_AGG_MAX_THRESHOLD	0x03
467 
468 /* USB_RX_BUF_TH */
469 #define RX_THR_SUPPER		0x0c350180
470 #define RX_THR_HIGH		0x7a120180
471 #define RX_THR_SLOW		0xffff0180
472 #define RX_THR_B		0x00010001
473 
474 /* USB_TX_DMA */
475 #define TEST_MODE_DISABLE	0x00000001
476 #define TX_SIZE_ADJUST1		0x00000100
477 
478 /* USB_BMU_RESET */
479 #define BMU_RESET_EP_IN		0x01
480 #define BMU_RESET_EP_OUT	0x02
481 
482 /* USB_BMU_CONFIG */
483 #define ACT_ODMA		BIT(1)
484 
485 /* USB_UPT_RXDMA_OWN */
486 #define OWN_UPDATE		BIT(0)
487 #define OWN_CLEAR		BIT(1)
488 
489 /* USB_FW_TASK */
490 #define FC_PATCH_TASK		BIT(1)
491 
492 /* USB_RX_AGGR_NUM */
493 #define RX_AGGR_NUM_MASK	0x1ff
494 
495 /* USB_UPS_CTRL */
496 #define POWER_CUT		0x0100
497 
498 /* USB_PM_CTRL_STATUS */
499 #define RESUME_INDICATE		0x0001
500 
501 /* USB_ECM_OPTION */
502 #define BYPASS_MAC_RESET	BIT(5)
503 
504 /* USB_CSTMR */
505 #define FORCE_SUPER		BIT(0)
506 
507 /* USB_MISC_2 */
508 #define UPS_FORCE_PWR_DOWN	BIT(0)
509 
510 /* USB_ECM_OP */
511 #define	EN_ALL_SPEED		BIT(0)
512 
513 /* USB_GPHY_CTRL */
514 #define GPHY_PATCH_DONE		BIT(2)
515 #define BYPASS_FLASH		BIT(5)
516 #define BACKUP_RESTRORE		BIT(6)
517 
518 /* USB_SPEED_OPTION */
519 #define RG_PWRDN_EN		BIT(8)
520 #define ALL_SPEED_OFF		BIT(9)
521 
522 /* USB_FW_CTRL */
523 #define FLOW_CTRL_PATCH_OPT	BIT(1)
524 #define AUTO_SPEEDUP		BIT(3)
525 #define FLOW_CTRL_PATCH_2	BIT(8)
526 
527 /* USB_FC_TIMER */
528 #define CTRL_TIMER_EN		BIT(15)
529 
530 /* USB_USB_CTRL */
531 #define CDC_ECM_EN		BIT(3)
532 #define RX_AGG_DISABLE		0x0010
533 #define RX_ZERO_EN		0x0080
534 
535 /* USB_U2P3_CTRL */
536 #define U2P3_ENABLE		0x0001
537 #define RX_DETECT8		BIT(3)
538 
539 /* USB_POWER_CUT */
540 #define PWR_EN			0x0001
541 #define PHASE2_EN		0x0008
542 #define UPS_EN			BIT(4)
543 #define USP_PREWAKE		BIT(5)
544 
545 /* USB_MISC_0 */
546 #define PCUT_STATUS		0x0001
547 
548 /* USB_RX_EARLY_TIMEOUT */
549 #define COALESCE_SUPER		 85000U
550 #define COALESCE_HIGH		250000U
551 #define COALESCE_SLOW		524280U
552 
553 /* USB_WDT1_CTRL */
554 #define WTD1_EN			BIT(0)
555 
556 /* USB_WDT11_CTRL */
557 #define TIMER11_EN		0x0001
558 
559 /* USB_LPM_CTRL */
560 /* bit 4 ~ 5: fifo empty boundary */
561 #define FIFO_EMPTY_1FB		0x30	/* 0x1fb * 64 = 32448 bytes */
562 /* bit 2 ~ 3: LMP timer */
563 #define LPM_TIMER_MASK		0x0c
564 #define LPM_TIMER_500MS		0x04	/* 500 ms */
565 #define LPM_TIMER_500US		0x0c	/* 500 us */
566 #define ROK_EXIT_LPM		0x02
567 
568 /* USB_AFE_CTRL2 */
569 #define SEN_VAL_MASK		0xf800
570 #define SEN_VAL_NORMAL		0xa000
571 #define SEL_RXIDLE		0x0100
572 
573 /* USB_UPHY_XTAL */
574 #define OOBS_POLLING		BIT(8)
575 
576 /* USB_UPS_CFG */
577 #define SAW_CNT_1MS_MASK	0x0fff
578 #define MID_REVERSE		BIT(5)	/* RTL8156A */
579 
580 /* USB_UPS_FLAGS */
581 #define UPS_FLAGS_R_TUNE		BIT(0)
582 #define UPS_FLAGS_EN_10M_CKDIV		BIT(1)
583 #define UPS_FLAGS_250M_CKDIV		BIT(2)
584 #define UPS_FLAGS_EN_ALDPS		BIT(3)
585 #define UPS_FLAGS_CTAP_SHORT_DIS	BIT(4)
586 #define UPS_FLAGS_SPEED_MASK		(0xf << 16)
587 #define ups_flags_speed(x)		((x) << 16)
588 #define UPS_FLAGS_EN_EEE		BIT(20)
589 #define UPS_FLAGS_EN_500M_EEE		BIT(21)
590 #define UPS_FLAGS_EN_EEE_CKDIV		BIT(22)
591 #define UPS_FLAGS_EEE_PLLOFF_100	BIT(23)
592 #define UPS_FLAGS_EEE_PLLOFF_GIGA	BIT(24)
593 #define UPS_FLAGS_EEE_CMOD_LV_EN	BIT(25)
594 #define UPS_FLAGS_EN_GREEN		BIT(26)
595 #define UPS_FLAGS_EN_FLOW_CTR		BIT(27)
596 
597 enum spd_duplex {
598 	NWAY_10M_HALF,
599 	NWAY_10M_FULL,
600 	NWAY_100M_HALF,
601 	NWAY_100M_FULL,
602 	NWAY_1000M_FULL,
603 	FORCE_10M_HALF,
604 	FORCE_10M_FULL,
605 	FORCE_100M_HALF,
606 	FORCE_100M_FULL,
607 	FORCE_1000M_FULL,
608 	NWAY_2500M_FULL,
609 };
610 
611 /* OCP_ALDPS_CONFIG */
612 #define ENPWRSAVE		0x8000
613 #define ENPDNPS			0x0200
614 #define LINKENA			0x0100
615 #define DIS_SDSAVE		0x0010
616 
617 /* OCP_PHY_STATUS */
618 #define PHY_STAT_MASK		0x0007
619 #define PHY_STAT_EXT_INIT	2
620 #define PHY_STAT_LAN_ON		3
621 #define PHY_STAT_PWRDN		5
622 
623 /* OCP_NCTL_CFG */
624 #define PGA_RETURN_EN		BIT(1)
625 
626 /* OCP_POWER_CFG */
627 #define EEE_CLKDIV_EN		0x8000
628 #define EN_ALDPS		0x0004
629 #define EN_10M_PLLOFF		0x0001
630 
631 /* OCP_EEE_CONFIG1 */
632 #define RG_TXLPI_MSK_HFDUP	0x8000
633 #define RG_MATCLR_EN		0x4000
634 #define EEE_10_CAP		0x2000
635 #define EEE_NWAY_EN		0x1000
636 #define TX_QUIET_EN		0x0200
637 #define RX_QUIET_EN		0x0100
638 #define sd_rise_time_mask	0x0070
639 #define sd_rise_time(x)		(min(x, 7) << 4)	/* bit 4 ~ 6 */
640 #define RG_RXLPI_MSK_HFDUP	0x0008
641 #define SDFALLTIME		0x0007	/* bit 0 ~ 2 */
642 
643 /* OCP_EEE_CONFIG2 */
644 #define RG_LPIHYS_NUM		0x7000	/* bit 12 ~ 15 */
645 #define RG_DACQUIET_EN		0x0400
646 #define RG_LDVQUIET_EN		0x0200
647 #define RG_CKRSEL		0x0020
648 #define RG_EEEPRG_EN		0x0010
649 
650 /* OCP_EEE_CONFIG3 */
651 #define fast_snr_mask		0xff80
652 #define fast_snr(x)		(min(x, 0x1ff) << 7)	/* bit 7 ~ 15 */
653 #define RG_LFS_SEL		0x0060	/* bit 6 ~ 5 */
654 #define MSK_PH			0x0006	/* bit 0 ~ 3 */
655 
656 /* OCP_EEE_AR */
657 /* bit[15:14] function */
658 #define FUN_ADDR		0x0000
659 #define FUN_DATA		0x4000
660 /* bit[4:0] device addr */
661 
662 /* OCP_EEE_CFG */
663 #define CTAP_SHORT_EN		0x0040
664 #define EEE10_EN		0x0010
665 
666 /* OCP_DOWN_SPEED */
667 #define EN_EEE_CMODE		BIT(14)
668 #define EN_EEE_1000		BIT(13)
669 #define EN_EEE_100		BIT(12)
670 #define EN_10M_CLKDIV		BIT(11)
671 #define EN_10M_BGOFF		0x0080
672 
673 /* OCP_10GBT_CTRL */
674 #define RTL_ADV2_5G_F_R		BIT(5)	/* Advertise 2.5GBASE-T fast-retrain */
675 
676 /* OCP_PHY_STATE */
677 #define TXDIS_STATE		0x01
678 #define ABD_STATE		0x02
679 
680 /* OCP_PHY_PATCH_STAT */
681 #define PATCH_READY		BIT(6)
682 
683 /* OCP_PHY_PATCH_CMD */
684 #define PATCH_REQUEST		BIT(4)
685 
686 /* OCP_PHY_LOCK */
687 #define PATCH_LOCK		BIT(0)
688 
689 /* OCP_ADC_CFG */
690 #define CKADSEL_L		0x0100
691 #define ADC_EN			0x0080
692 #define EN_EMI_L		0x0040
693 
694 /* OCP_SYSCLK_CFG */
695 #define sysclk_div_expo(x)	(min(x, 5) << 8)
696 #define clk_div_expo(x)		(min(x, 5) << 4)
697 
698 /* SRAM_GREEN_CFG */
699 #define GREEN_ETH_EN		BIT(15)
700 #define R_TUNE_EN		BIT(11)
701 
702 /* SRAM_LPF_CFG */
703 #define LPF_AUTO_TUNE		0x8000
704 
705 /* SRAM_10M_AMP1 */
706 #define GDAC_IB_UPALL		0x0008
707 
708 /* SRAM_10M_AMP2 */
709 #define AMP_DN			0x0200
710 
711 /* SRAM_IMPEDANCE */
712 #define RX_DRIVING_MASK		0x6000
713 
714 /* SRAM_PHY_LOCK */
715 #define PHY_PATCH_LOCK		0x0001
716 
717 /* MAC PASSTHRU */
718 #define AD_MASK			0xfee0
719 #define BND_MASK		0x0004
720 #define BD_MASK			0x0001
721 #define EFUSE			0xcfdb
722 #define PASS_THRU_MASK		0x1
723 
724 #define BP4_SUPER_ONLY		0x1578	/* RTL_VER_04 only */
725 
726 enum rtl_register_content {
727 	_2500bps	= BIT(10),
728 	_1250bps	= BIT(9),
729 	_500bps		= BIT(8),
730 	_tx_flow	= BIT(6),
731 	_rx_flow	= BIT(5),
732 	_1000bps	= 0x10,
733 	_100bps		= 0x08,
734 	_10bps		= 0x04,
735 	LINK_STATUS	= 0x02,
736 	FULL_DUP	= 0x01,
737 };
738 
739 #define is_speed_2500(_speed)	(((_speed) & (_2500bps | LINK_STATUS)) == (_2500bps | LINK_STATUS))
740 #define is_flow_control(_speed)	(((_speed) & (_tx_flow | _rx_flow)) == (_tx_flow | _rx_flow))
741 
742 #define RTL8152_MAX_TX		4
743 #define RTL8152_MAX_RX		10
744 #define INTBUFSIZE		2
745 #define TX_ALIGN		4
746 #define RX_ALIGN		8
747 
748 #define RTL8152_RX_MAX_PENDING	4096
749 #define RTL8152_RXFG_HEADSZ	256
750 
751 #define INTR_LINK		0x0004
752 
753 #define RTL8152_RMS		(VLAN_ETH_FRAME_LEN + ETH_FCS_LEN)
754 #define RTL8153_RMS		RTL8153_MAX_PACKET
755 #define RTL8152_TX_TIMEOUT	(5 * HZ)
756 #define mtu_to_size(m)		((m) + VLAN_ETH_HLEN + ETH_FCS_LEN)
757 #define size_to_mtu(s)		((s) - VLAN_ETH_HLEN - ETH_FCS_LEN)
758 #define rx_reserved_size(x)	(mtu_to_size(x) + sizeof(struct rx_desc) + RX_ALIGN)
759 
760 /* rtl8152 flags */
761 enum rtl8152_flags {
762 	RTL8152_UNPLUG = 0,
763 	RTL8152_SET_RX_MODE,
764 	WORK_ENABLE,
765 	RTL8152_LINK_CHG,
766 	SELECTIVE_SUSPEND,
767 	PHY_RESET,
768 	SCHEDULE_TASKLET,
769 	GREEN_ETHERNET,
770 };
771 
772 #define DEVICE_ID_THINKPAD_THUNDERBOLT3_DOCK_GEN2	0x3082
773 #define DEVICE_ID_THINKPAD_USB_C_DOCK_GEN2		0xa387
774 
775 struct tally_counter {
776 	__le64	tx_packets;
777 	__le64	rx_packets;
778 	__le64	tx_errors;
779 	__le32	rx_errors;
780 	__le16	rx_missed;
781 	__le16	align_errors;
782 	__le32	tx_one_collision;
783 	__le32	tx_multi_collision;
784 	__le64	rx_unicast;
785 	__le64	rx_broadcast;
786 	__le32	rx_multicast;
787 	__le16	tx_aborted;
788 	__le16	tx_underrun;
789 };
790 
791 struct rx_desc {
792 	__le32 opts1;
793 #define RX_LEN_MASK			0x7fff
794 
795 	__le32 opts2;
796 #define RD_UDP_CS			BIT(23)
797 #define RD_TCP_CS			BIT(22)
798 #define RD_IPV6_CS			BIT(20)
799 #define RD_IPV4_CS			BIT(19)
800 
801 	__le32 opts3;
802 #define IPF				BIT(23) /* IP checksum fail */
803 #define UDPF				BIT(22) /* UDP checksum fail */
804 #define TCPF				BIT(21) /* TCP checksum fail */
805 #define RX_VLAN_TAG			BIT(16)
806 
807 	__le32 opts4;
808 	__le32 opts5;
809 	__le32 opts6;
810 };
811 
812 struct tx_desc {
813 	__le32 opts1;
814 #define TX_FS			BIT(31) /* First segment of a packet */
815 #define TX_LS			BIT(30) /* Final segment of a packet */
816 #define GTSENDV4		BIT(28)
817 #define GTSENDV6		BIT(27)
818 #define GTTCPHO_SHIFT		18
819 #define GTTCPHO_MAX		0x7fU
820 #define TX_LEN_MAX		0x3ffffU
821 
822 	__le32 opts2;
823 #define UDP_CS			BIT(31) /* Calculate UDP/IP checksum */
824 #define TCP_CS			BIT(30) /* Calculate TCP/IP checksum */
825 #define IPV4_CS			BIT(29) /* Calculate IPv4 checksum */
826 #define IPV6_CS			BIT(28) /* Calculate IPv6 checksum */
827 #define MSS_SHIFT		17
828 #define MSS_MAX			0x7ffU
829 #define TCPHO_SHIFT		17
830 #define TCPHO_MAX		0x7ffU
831 #define TX_VLAN_TAG		BIT(16)
832 };
833 
834 struct r8152;
835 
836 struct rx_agg {
837 	struct list_head list, info_list;
838 	struct urb *urb;
839 	struct r8152 *context;
840 	struct page *page;
841 	void *buffer;
842 };
843 
844 struct tx_agg {
845 	struct list_head list;
846 	struct urb *urb;
847 	struct r8152 *context;
848 	void *buffer;
849 	void *head;
850 	u32 skb_num;
851 	u32 skb_len;
852 };
853 
854 struct r8152 {
855 	unsigned long flags;
856 	struct usb_device *udev;
857 	struct napi_struct napi;
858 	struct usb_interface *intf;
859 	struct net_device *netdev;
860 	struct urb *intr_urb;
861 	struct tx_agg tx_info[RTL8152_MAX_TX];
862 	struct list_head rx_info, rx_used;
863 	struct list_head rx_done, tx_free;
864 	struct sk_buff_head tx_queue, rx_queue;
865 	spinlock_t rx_lock, tx_lock;
866 	struct delayed_work schedule, hw_phy_work;
867 	struct mii_if_info mii;
868 	struct mutex control;	/* use for hw setting */
869 #ifdef CONFIG_PM_SLEEP
870 	struct notifier_block pm_notifier;
871 #endif
872 	struct tasklet_struct tx_tl;
873 
874 	struct rtl_ops {
875 		void (*init)(struct r8152 *tp);
876 		int (*enable)(struct r8152 *tp);
877 		void (*disable)(struct r8152 *tp);
878 		void (*up)(struct r8152 *tp);
879 		void (*down)(struct r8152 *tp);
880 		void (*unload)(struct r8152 *tp);
881 		int (*eee_get)(struct r8152 *tp, struct ethtool_eee *eee);
882 		int (*eee_set)(struct r8152 *tp, struct ethtool_eee *eee);
883 		bool (*in_nway)(struct r8152 *tp);
884 		void (*hw_phy_cfg)(struct r8152 *tp);
885 		void (*autosuspend_en)(struct r8152 *tp, bool enable);
886 		void (*change_mtu)(struct r8152 *tp);
887 	} rtl_ops;
888 
889 	struct ups_info {
890 		u32 r_tune:1;
891 		u32 _10m_ckdiv:1;
892 		u32 _250m_ckdiv:1;
893 		u32 aldps:1;
894 		u32 lite_mode:2;
895 		u32 speed_duplex:4;
896 		u32 eee:1;
897 		u32 eee_lite:1;
898 		u32 eee_ckdiv:1;
899 		u32 eee_plloff_100:1;
900 		u32 eee_plloff_giga:1;
901 		u32 eee_cmod_lv:1;
902 		u32 green:1;
903 		u32 flow_control:1;
904 		u32 ctap_short_off:1;
905 	} ups_info;
906 
907 #define RTL_VER_SIZE		32
908 
909 	struct rtl_fw {
910 		const char *fw_name;
911 		const struct firmware *fw;
912 
913 		char version[RTL_VER_SIZE];
914 		int (*pre_fw)(struct r8152 *tp);
915 		int (*post_fw)(struct r8152 *tp);
916 
917 		bool retry;
918 	} rtl_fw;
919 
920 	atomic_t rx_count;
921 
922 	bool eee_en;
923 	int intr_interval;
924 	u32 saved_wolopts;
925 	u32 msg_enable;
926 	u32 tx_qlen;
927 	u32 coalesce;
928 	u32 advertising;
929 	u32 rx_buf_sz;
930 	u32 rx_copybreak;
931 	u32 rx_pending;
932 	u32 fc_pause_on, fc_pause_off;
933 
934 	unsigned int pipe_in, pipe_out, pipe_intr, pipe_ctrl_in, pipe_ctrl_out;
935 
936 	u32 support_2500full:1;
937 	u32 lenovo_macpassthru:1;
938 	u32 dell_tb_rx_agg_bug:1;
939 	u16 ocp_base;
940 	u16 speed;
941 	u16 eee_adv;
942 	u8 *intr_buff;
943 	u8 version;
944 	u8 duplex;
945 	u8 autoneg;
946 };
947 
948 /**
949  * struct fw_block - block type and total length
950  * @type: type of the current block, such as RTL_FW_END, RTL_FW_PLA,
951  *	RTL_FW_USB and so on.
952  * @length: total length of the current block.
953  */
954 struct fw_block {
955 	__le32 type;
956 	__le32 length;
957 } __packed;
958 
959 /**
960  * struct fw_header - header of the firmware file
961  * @checksum: checksum of sha256 which is calculated from the whole file
962  *	except the checksum field of the file. That is, calculate sha256
963  *	from the version field to the end of the file.
964  * @version: version of this firmware.
965  * @blocks: the first firmware block of the file
966  */
967 struct fw_header {
968 	u8 checksum[32];
969 	char version[RTL_VER_SIZE];
970 	struct fw_block blocks[];
971 } __packed;
972 
973 enum rtl8152_fw_flags {
974 	FW_FLAGS_USB = 0,
975 	FW_FLAGS_PLA,
976 	FW_FLAGS_START,
977 	FW_FLAGS_STOP,
978 	FW_FLAGS_NC,
979 	FW_FLAGS_NC1,
980 	FW_FLAGS_NC2,
981 	FW_FLAGS_UC2,
982 	FW_FLAGS_UC,
983 	FW_FLAGS_SPEED_UP,
984 	FW_FLAGS_VER,
985 };
986 
987 enum rtl8152_fw_fixup_cmd {
988 	FW_FIXUP_AND = 0,
989 	FW_FIXUP_OR,
990 	FW_FIXUP_NOT,
991 	FW_FIXUP_XOR,
992 };
993 
994 struct fw_phy_set {
995 	__le16 addr;
996 	__le16 data;
997 } __packed;
998 
999 struct fw_phy_speed_up {
1000 	struct fw_block blk_hdr;
1001 	__le16 fw_offset;
1002 	__le16 version;
1003 	__le16 fw_reg;
1004 	__le16 reserved;
1005 	char info[];
1006 } __packed;
1007 
1008 struct fw_phy_ver {
1009 	struct fw_block blk_hdr;
1010 	struct fw_phy_set ver;
1011 	__le32 reserved;
1012 } __packed;
1013 
1014 struct fw_phy_fixup {
1015 	struct fw_block blk_hdr;
1016 	struct fw_phy_set setting;
1017 	__le16 bit_cmd;
1018 	__le16 reserved;
1019 } __packed;
1020 
1021 struct fw_phy_union {
1022 	struct fw_block blk_hdr;
1023 	__le16 fw_offset;
1024 	__le16 fw_reg;
1025 	struct fw_phy_set pre_set[2];
1026 	struct fw_phy_set bp[8];
1027 	struct fw_phy_set bp_en;
1028 	u8 pre_num;
1029 	u8 bp_num;
1030 	char info[];
1031 } __packed;
1032 
1033 /**
1034  * struct fw_mac - a firmware block used by RTL_FW_PLA and RTL_FW_USB.
1035  *	The layout of the firmware block is:
1036  *	<struct fw_mac> + <info> + <firmware data>.
1037  * @blk_hdr: firmware descriptor (type, length)
1038  * @fw_offset: offset of the firmware binary data. The start address of
1039  *	the data would be the address of struct fw_mac + @fw_offset.
1040  * @fw_reg: the register to load the firmware. Depends on chip.
1041  * @bp_ba_addr: the register to write break point base address. Depends on
1042  *	chip.
1043  * @bp_ba_value: break point base address. Depends on chip.
1044  * @bp_en_addr: the register to write break point enabled mask. Depends
1045  *	on chip.
1046  * @bp_en_value: break point enabled mask. Depends on the firmware.
1047  * @bp_start: the start register of break points. Depends on chip.
1048  * @bp_num: the break point number which needs to be set for this firmware.
1049  *	Depends on the firmware.
1050  * @bp: break points. Depends on firmware.
1051  * @reserved: reserved space (unused)
1052  * @fw_ver_reg: the register to store the fw version.
1053  * @fw_ver_data: the firmware version of the current type.
1054  * @info: additional information for debugging, and is followed by the
1055  *	binary data of firmware.
1056  */
1057 struct fw_mac {
1058 	struct fw_block blk_hdr;
1059 	__le16 fw_offset;
1060 	__le16 fw_reg;
1061 	__le16 bp_ba_addr;
1062 	__le16 bp_ba_value;
1063 	__le16 bp_en_addr;
1064 	__le16 bp_en_value;
1065 	__le16 bp_start;
1066 	__le16 bp_num;
1067 	__le16 bp[16]; /* any value determined by firmware */
1068 	__le32 reserved;
1069 	__le16 fw_ver_reg;
1070 	u8 fw_ver_data;
1071 	char info[];
1072 } __packed;
1073 
1074 /**
1075  * struct fw_phy_patch_key - a firmware block used by RTL_FW_PHY_START.
1076  *	This is used to set patch key when loading the firmware of PHY.
1077  * @blk_hdr: firmware descriptor (type, length)
1078  * @key_reg: the register to write the patch key.
1079  * @key_data: patch key.
1080  * @reserved: reserved space (unused)
1081  */
1082 struct fw_phy_patch_key {
1083 	struct fw_block blk_hdr;
1084 	__le16 key_reg;
1085 	__le16 key_data;
1086 	__le32 reserved;
1087 } __packed;
1088 
1089 /**
1090  * struct fw_phy_nc - a firmware block used by RTL_FW_PHY_NC.
1091  *	The layout of the firmware block is:
1092  *	<struct fw_phy_nc> + <info> + <firmware data>.
1093  * @blk_hdr: firmware descriptor (type, length)
1094  * @fw_offset: offset of the firmware binary data. The start address of
1095  *	the data would be the address of struct fw_phy_nc + @fw_offset.
1096  * @fw_reg: the register to load the firmware. Depends on chip.
1097  * @ba_reg: the register to write the base address. Depends on chip.
1098  * @ba_data: base address. Depends on chip.
1099  * @patch_en_addr: the register of enabling patch mode. Depends on chip.
1100  * @patch_en_value: patch mode enabled mask. Depends on the firmware.
1101  * @mode_reg: the regitster of switching the mode.
1102  * @mode_pre: the mode needing to be set before loading the firmware.
1103  * @mode_post: the mode to be set when finishing to load the firmware.
1104  * @reserved: reserved space (unused)
1105  * @bp_start: the start register of break points. Depends on chip.
1106  * @bp_num: the break point number which needs to be set for this firmware.
1107  *	Depends on the firmware.
1108  * @bp: break points. Depends on firmware.
1109  * @info: additional information for debugging, and is followed by the
1110  *	binary data of firmware.
1111  */
1112 struct fw_phy_nc {
1113 	struct fw_block blk_hdr;
1114 	__le16 fw_offset;
1115 	__le16 fw_reg;
1116 	__le16 ba_reg;
1117 	__le16 ba_data;
1118 	__le16 patch_en_addr;
1119 	__le16 patch_en_value;
1120 	__le16 mode_reg;
1121 	__le16 mode_pre;
1122 	__le16 mode_post;
1123 	__le16 reserved;
1124 	__le16 bp_start;
1125 	__le16 bp_num;
1126 	__le16 bp[4];
1127 	char info[];
1128 } __packed;
1129 
1130 enum rtl_fw_type {
1131 	RTL_FW_END = 0,
1132 	RTL_FW_PLA,
1133 	RTL_FW_USB,
1134 	RTL_FW_PHY_START,
1135 	RTL_FW_PHY_STOP,
1136 	RTL_FW_PHY_NC,
1137 	RTL_FW_PHY_FIXUP,
1138 	RTL_FW_PHY_UNION_NC,
1139 	RTL_FW_PHY_UNION_NC1,
1140 	RTL_FW_PHY_UNION_NC2,
1141 	RTL_FW_PHY_UNION_UC2,
1142 	RTL_FW_PHY_UNION_UC,
1143 	RTL_FW_PHY_UNION_MISC,
1144 	RTL_FW_PHY_SPEED_UP,
1145 	RTL_FW_PHY_VER,
1146 };
1147 
1148 enum rtl_version {
1149 	RTL_VER_UNKNOWN = 0,
1150 	RTL_VER_01,
1151 	RTL_VER_02,
1152 	RTL_VER_03,
1153 	RTL_VER_04,
1154 	RTL_VER_05,
1155 	RTL_VER_06,
1156 	RTL_VER_07,
1157 	RTL_VER_08,
1158 	RTL_VER_09,
1159 
1160 	RTL_TEST_01,
1161 	RTL_VER_10,
1162 	RTL_VER_11,
1163 	RTL_VER_12,
1164 	RTL_VER_13,
1165 	RTL_VER_14,
1166 	RTL_VER_15,
1167 
1168 	RTL_VER_MAX
1169 };
1170 
1171 enum tx_csum_stat {
1172 	TX_CSUM_SUCCESS = 0,
1173 	TX_CSUM_TSO,
1174 	TX_CSUM_NONE
1175 };
1176 
1177 #define RTL_ADVERTISED_10_HALF			BIT(0)
1178 #define RTL_ADVERTISED_10_FULL			BIT(1)
1179 #define RTL_ADVERTISED_100_HALF			BIT(2)
1180 #define RTL_ADVERTISED_100_FULL			BIT(3)
1181 #define RTL_ADVERTISED_1000_HALF		BIT(4)
1182 #define RTL_ADVERTISED_1000_FULL		BIT(5)
1183 #define RTL_ADVERTISED_2500_FULL		BIT(6)
1184 
1185 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
1186  * The RTL chips use a 64 element hash table based on the Ethernet CRC.
1187  */
1188 static const int multicast_filter_limit = 32;
1189 static unsigned int agg_buf_sz = 16384;
1190 
1191 #define RTL_LIMITED_TSO_SIZE	(size_to_mtu(agg_buf_sz) - sizeof(struct tx_desc))
1192 
1193 static
1194 int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
1195 {
1196 	int ret;
1197 	void *tmp;
1198 
1199 	tmp = kmalloc(size, GFP_KERNEL);
1200 	if (!tmp)
1201 		return -ENOMEM;
1202 
1203 	ret = usb_control_msg(tp->udev, tp->pipe_ctrl_in,
1204 			      RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
1205 			      value, index, tmp, size, 500);
1206 	if (ret < 0)
1207 		memset(data, 0xff, size);
1208 	else
1209 		memcpy(data, tmp, size);
1210 
1211 	kfree(tmp);
1212 
1213 	return ret;
1214 }
1215 
1216 static
1217 int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
1218 {
1219 	int ret;
1220 	void *tmp;
1221 
1222 	tmp = kmemdup(data, size, GFP_KERNEL);
1223 	if (!tmp)
1224 		return -ENOMEM;
1225 
1226 	ret = usb_control_msg(tp->udev, tp->pipe_ctrl_out,
1227 			      RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
1228 			      value, index, tmp, size, 500);
1229 
1230 	kfree(tmp);
1231 
1232 	return ret;
1233 }
1234 
1235 static void rtl_set_unplug(struct r8152 *tp)
1236 {
1237 	if (tp->udev->state == USB_STATE_NOTATTACHED) {
1238 		set_bit(RTL8152_UNPLUG, &tp->flags);
1239 		smp_mb__after_atomic();
1240 	}
1241 }
1242 
1243 static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
1244 			    void *data, u16 type)
1245 {
1246 	u16 limit = 64;
1247 	int ret = 0;
1248 
1249 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
1250 		return -ENODEV;
1251 
1252 	/* both size and indix must be 4 bytes align */
1253 	if ((size & 3) || !size || (index & 3) || !data)
1254 		return -EPERM;
1255 
1256 	if ((u32)index + (u32)size > 0xffff)
1257 		return -EPERM;
1258 
1259 	while (size) {
1260 		if (size > limit) {
1261 			ret = get_registers(tp, index, type, limit, data);
1262 			if (ret < 0)
1263 				break;
1264 
1265 			index += limit;
1266 			data += limit;
1267 			size -= limit;
1268 		} else {
1269 			ret = get_registers(tp, index, type, size, data);
1270 			if (ret < 0)
1271 				break;
1272 
1273 			index += size;
1274 			data += size;
1275 			size = 0;
1276 			break;
1277 		}
1278 	}
1279 
1280 	if (ret == -ENODEV)
1281 		rtl_set_unplug(tp);
1282 
1283 	return ret;
1284 }
1285 
1286 static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
1287 			     u16 size, void *data, u16 type)
1288 {
1289 	int ret;
1290 	u16 byteen_start, byteen_end, byen;
1291 	u16 limit = 512;
1292 
1293 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
1294 		return -ENODEV;
1295 
1296 	/* both size and indix must be 4 bytes align */
1297 	if ((size & 3) || !size || (index & 3) || !data)
1298 		return -EPERM;
1299 
1300 	if ((u32)index + (u32)size > 0xffff)
1301 		return -EPERM;
1302 
1303 	byteen_start = byteen & BYTE_EN_START_MASK;
1304 	byteen_end = byteen & BYTE_EN_END_MASK;
1305 
1306 	byen = byteen_start | (byteen_start << 4);
1307 	ret = set_registers(tp, index, type | byen, 4, data);
1308 	if (ret < 0)
1309 		goto error1;
1310 
1311 	index += 4;
1312 	data += 4;
1313 	size -= 4;
1314 
1315 	if (size) {
1316 		size -= 4;
1317 
1318 		while (size) {
1319 			if (size > limit) {
1320 				ret = set_registers(tp, index,
1321 						    type | BYTE_EN_DWORD,
1322 						    limit, data);
1323 				if (ret < 0)
1324 					goto error1;
1325 
1326 				index += limit;
1327 				data += limit;
1328 				size -= limit;
1329 			} else {
1330 				ret = set_registers(tp, index,
1331 						    type | BYTE_EN_DWORD,
1332 						    size, data);
1333 				if (ret < 0)
1334 					goto error1;
1335 
1336 				index += size;
1337 				data += size;
1338 				size = 0;
1339 				break;
1340 			}
1341 		}
1342 
1343 		byen = byteen_end | (byteen_end >> 4);
1344 		ret = set_registers(tp, index, type | byen, 4, data);
1345 		if (ret < 0)
1346 			goto error1;
1347 	}
1348 
1349 error1:
1350 	if (ret == -ENODEV)
1351 		rtl_set_unplug(tp);
1352 
1353 	return ret;
1354 }
1355 
1356 static inline
1357 int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
1358 {
1359 	return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
1360 }
1361 
1362 static inline
1363 int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
1364 {
1365 	return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
1366 }
1367 
1368 static inline
1369 int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
1370 {
1371 	return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
1372 }
1373 
1374 static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
1375 {
1376 	__le32 data;
1377 
1378 	generic_ocp_read(tp, index, sizeof(data), &data, type);
1379 
1380 	return __le32_to_cpu(data);
1381 }
1382 
1383 static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
1384 {
1385 	__le32 tmp = __cpu_to_le32(data);
1386 
1387 	generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
1388 }
1389 
1390 static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
1391 {
1392 	u32 data;
1393 	__le32 tmp;
1394 	u16 byen = BYTE_EN_WORD;
1395 	u8 shift = index & 2;
1396 
1397 	index &= ~3;
1398 	byen <<= shift;
1399 
1400 	generic_ocp_read(tp, index, sizeof(tmp), &tmp, type | byen);
1401 
1402 	data = __le32_to_cpu(tmp);
1403 	data >>= (shift * 8);
1404 	data &= 0xffff;
1405 
1406 	return (u16)data;
1407 }
1408 
1409 static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
1410 {
1411 	u32 mask = 0xffff;
1412 	__le32 tmp;
1413 	u16 byen = BYTE_EN_WORD;
1414 	u8 shift = index & 2;
1415 
1416 	data &= mask;
1417 
1418 	if (index & 2) {
1419 		byen <<= shift;
1420 		mask <<= (shift * 8);
1421 		data <<= (shift * 8);
1422 		index &= ~3;
1423 	}
1424 
1425 	tmp = __cpu_to_le32(data);
1426 
1427 	generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
1428 }
1429 
1430 static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
1431 {
1432 	u32 data;
1433 	__le32 tmp;
1434 	u8 shift = index & 3;
1435 
1436 	index &= ~3;
1437 
1438 	generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
1439 
1440 	data = __le32_to_cpu(tmp);
1441 	data >>= (shift * 8);
1442 	data &= 0xff;
1443 
1444 	return (u8)data;
1445 }
1446 
1447 static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
1448 {
1449 	u32 mask = 0xff;
1450 	__le32 tmp;
1451 	u16 byen = BYTE_EN_BYTE;
1452 	u8 shift = index & 3;
1453 
1454 	data &= mask;
1455 
1456 	if (index & 3) {
1457 		byen <<= shift;
1458 		mask <<= (shift * 8);
1459 		data <<= (shift * 8);
1460 		index &= ~3;
1461 	}
1462 
1463 	tmp = __cpu_to_le32(data);
1464 
1465 	generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
1466 }
1467 
1468 static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
1469 {
1470 	u16 ocp_base, ocp_index;
1471 
1472 	ocp_base = addr & 0xf000;
1473 	if (ocp_base != tp->ocp_base) {
1474 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
1475 		tp->ocp_base = ocp_base;
1476 	}
1477 
1478 	ocp_index = (addr & 0x0fff) | 0xb000;
1479 	return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
1480 }
1481 
1482 static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
1483 {
1484 	u16 ocp_base, ocp_index;
1485 
1486 	ocp_base = addr & 0xf000;
1487 	if (ocp_base != tp->ocp_base) {
1488 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
1489 		tp->ocp_base = ocp_base;
1490 	}
1491 
1492 	ocp_index = (addr & 0x0fff) | 0xb000;
1493 	ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
1494 }
1495 
1496 static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
1497 {
1498 	ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
1499 }
1500 
1501 static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
1502 {
1503 	return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2);
1504 }
1505 
1506 static void sram_write(struct r8152 *tp, u16 addr, u16 data)
1507 {
1508 	ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
1509 	ocp_reg_write(tp, OCP_SRAM_DATA, data);
1510 }
1511 
1512 static u16 sram_read(struct r8152 *tp, u16 addr)
1513 {
1514 	ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
1515 	return ocp_reg_read(tp, OCP_SRAM_DATA);
1516 }
1517 
1518 static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
1519 {
1520 	struct r8152 *tp = netdev_priv(netdev);
1521 	int ret;
1522 
1523 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
1524 		return -ENODEV;
1525 
1526 	if (phy_id != R8152_PHY_ID)
1527 		return -EINVAL;
1528 
1529 	ret = r8152_mdio_read(tp, reg);
1530 
1531 	return ret;
1532 }
1533 
1534 static
1535 void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
1536 {
1537 	struct r8152 *tp = netdev_priv(netdev);
1538 
1539 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
1540 		return;
1541 
1542 	if (phy_id != R8152_PHY_ID)
1543 		return;
1544 
1545 	r8152_mdio_write(tp, reg, val);
1546 }
1547 
1548 static int
1549 r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);
1550 
1551 static int
1552 rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u32 speed, u8 duplex,
1553 		  u32 advertising);
1554 
1555 static int __rtl8152_set_mac_address(struct net_device *netdev, void *p,
1556 				     bool in_resume)
1557 {
1558 	struct r8152 *tp = netdev_priv(netdev);
1559 	struct sockaddr *addr = p;
1560 	int ret = -EADDRNOTAVAIL;
1561 
1562 	if (!is_valid_ether_addr(addr->sa_data))
1563 		goto out1;
1564 
1565 	if (!in_resume) {
1566 		ret = usb_autopm_get_interface(tp->intf);
1567 		if (ret < 0)
1568 			goto out1;
1569 	}
1570 
1571 	mutex_lock(&tp->control);
1572 
1573 	memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1574 
1575 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
1576 	pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
1577 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
1578 
1579 	mutex_unlock(&tp->control);
1580 
1581 	if (!in_resume)
1582 		usb_autopm_put_interface(tp->intf);
1583 out1:
1584 	return ret;
1585 }
1586 
1587 static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
1588 {
1589 	return __rtl8152_set_mac_address(netdev, p, false);
1590 }
1591 
1592 /* Devices containing proper chips can support a persistent
1593  * host system provided MAC address.
1594  * Examples of this are Dell TB15 and Dell WD15 docks
1595  */
1596 static int vendor_mac_passthru_addr_read(struct r8152 *tp, struct sockaddr *sa)
1597 {
1598 	acpi_status status;
1599 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1600 	union acpi_object *obj;
1601 	int ret = -EINVAL;
1602 	u32 ocp_data;
1603 	unsigned char buf[6];
1604 	char *mac_obj_name;
1605 	acpi_object_type mac_obj_type;
1606 	int mac_strlen;
1607 
1608 	if (tp->lenovo_macpassthru) {
1609 		mac_obj_name = "\\MACA";
1610 		mac_obj_type = ACPI_TYPE_STRING;
1611 		mac_strlen = 0x16;
1612 	} else {
1613 		/* test for -AD variant of RTL8153 */
1614 		ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
1615 		if ((ocp_data & AD_MASK) == 0x1000) {
1616 			/* test for MAC address pass-through bit */
1617 			ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, EFUSE);
1618 			if ((ocp_data & PASS_THRU_MASK) != 1) {
1619 				netif_dbg(tp, probe, tp->netdev,
1620 						"No efuse for RTL8153-AD MAC pass through\n");
1621 				return -ENODEV;
1622 			}
1623 		} else {
1624 			/* test for RTL8153-BND and RTL8153-BD */
1625 			ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_1);
1626 			if ((ocp_data & BND_MASK) == 0 && (ocp_data & BD_MASK) == 0) {
1627 				netif_dbg(tp, probe, tp->netdev,
1628 						"Invalid variant for MAC pass through\n");
1629 				return -ENODEV;
1630 			}
1631 		}
1632 
1633 		mac_obj_name = "\\_SB.AMAC";
1634 		mac_obj_type = ACPI_TYPE_BUFFER;
1635 		mac_strlen = 0x17;
1636 	}
1637 
1638 	/* returns _AUXMAC_#AABBCCDDEEFF# */
1639 	status = acpi_evaluate_object(NULL, mac_obj_name, NULL, &buffer);
1640 	obj = (union acpi_object *)buffer.pointer;
1641 	if (!ACPI_SUCCESS(status))
1642 		return -ENODEV;
1643 	if (obj->type != mac_obj_type || obj->string.length != mac_strlen) {
1644 		netif_warn(tp, probe, tp->netdev,
1645 			   "Invalid buffer for pass-thru MAC addr: (%d, %d)\n",
1646 			   obj->type, obj->string.length);
1647 		goto amacout;
1648 	}
1649 
1650 	if (strncmp(obj->string.pointer, "_AUXMAC_#", 9) != 0 ||
1651 	    strncmp(obj->string.pointer + 0x15, "#", 1) != 0) {
1652 		netif_warn(tp, probe, tp->netdev,
1653 			   "Invalid header when reading pass-thru MAC addr\n");
1654 		goto amacout;
1655 	}
1656 	ret = hex2bin(buf, obj->string.pointer + 9, 6);
1657 	if (!(ret == 0 && is_valid_ether_addr(buf))) {
1658 		netif_warn(tp, probe, tp->netdev,
1659 			   "Invalid MAC for pass-thru MAC addr: %d, %pM\n",
1660 			   ret, buf);
1661 		ret = -EINVAL;
1662 		goto amacout;
1663 	}
1664 	memcpy(sa->sa_data, buf, 6);
1665 	netif_info(tp, probe, tp->netdev,
1666 		   "Using pass-thru MAC addr %pM\n", sa->sa_data);
1667 
1668 amacout:
1669 	kfree(obj);
1670 	return ret;
1671 }
1672 
1673 static int determine_ethernet_addr(struct r8152 *tp, struct sockaddr *sa)
1674 {
1675 	struct net_device *dev = tp->netdev;
1676 	int ret;
1677 
1678 	sa->sa_family = dev->type;
1679 
1680 	ret = eth_platform_get_mac_address(&tp->udev->dev, sa->sa_data);
1681 	if (ret < 0) {
1682 		if (tp->version == RTL_VER_01) {
1683 			ret = pla_ocp_read(tp, PLA_IDR, 8, sa->sa_data);
1684 		} else {
1685 			/* if device doesn't support MAC pass through this will
1686 			 * be expected to be non-zero
1687 			 */
1688 			ret = vendor_mac_passthru_addr_read(tp, sa);
1689 			if (ret < 0)
1690 				ret = pla_ocp_read(tp, PLA_BACKUP, 8,
1691 						   sa->sa_data);
1692 		}
1693 	}
1694 
1695 	if (ret < 0) {
1696 		netif_err(tp, probe, dev, "Get ether addr fail\n");
1697 	} else if (!is_valid_ether_addr(sa->sa_data)) {
1698 		netif_err(tp, probe, dev, "Invalid ether addr %pM\n",
1699 			  sa->sa_data);
1700 		eth_hw_addr_random(dev);
1701 		ether_addr_copy(sa->sa_data, dev->dev_addr);
1702 		netif_info(tp, probe, dev, "Random ether addr %pM\n",
1703 			   sa->sa_data);
1704 		return 0;
1705 	}
1706 
1707 	return ret;
1708 }
1709 
1710 static int set_ethernet_addr(struct r8152 *tp, bool in_resume)
1711 {
1712 	struct net_device *dev = tp->netdev;
1713 	struct sockaddr sa;
1714 	int ret;
1715 
1716 	ret = determine_ethernet_addr(tp, &sa);
1717 	if (ret < 0)
1718 		return ret;
1719 
1720 	if (tp->version == RTL_VER_01)
1721 		ether_addr_copy(dev->dev_addr, sa.sa_data);
1722 	else
1723 		ret = __rtl8152_set_mac_address(dev, &sa, in_resume);
1724 
1725 	return ret;
1726 }
1727 
1728 static void read_bulk_callback(struct urb *urb)
1729 {
1730 	struct net_device *netdev;
1731 	int status = urb->status;
1732 	struct rx_agg *agg;
1733 	struct r8152 *tp;
1734 	unsigned long flags;
1735 
1736 	agg = urb->context;
1737 	if (!agg)
1738 		return;
1739 
1740 	tp = agg->context;
1741 	if (!tp)
1742 		return;
1743 
1744 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
1745 		return;
1746 
1747 	if (!test_bit(WORK_ENABLE, &tp->flags))
1748 		return;
1749 
1750 	netdev = tp->netdev;
1751 
1752 	/* When link down, the driver would cancel all bulks. */
1753 	/* This avoid the re-submitting bulk */
1754 	if (!netif_carrier_ok(netdev))
1755 		return;
1756 
1757 	usb_mark_last_busy(tp->udev);
1758 
1759 	switch (status) {
1760 	case 0:
1761 		if (urb->actual_length < ETH_ZLEN)
1762 			break;
1763 
1764 		spin_lock_irqsave(&tp->rx_lock, flags);
1765 		list_add_tail(&agg->list, &tp->rx_done);
1766 		spin_unlock_irqrestore(&tp->rx_lock, flags);
1767 		napi_schedule(&tp->napi);
1768 		return;
1769 	case -ESHUTDOWN:
1770 		rtl_set_unplug(tp);
1771 		netif_device_detach(tp->netdev);
1772 		return;
1773 	case -ENOENT:
1774 		return;	/* the urb is in unlink state */
1775 	case -ETIME:
1776 		if (net_ratelimit())
1777 			netdev_warn(netdev, "maybe reset is needed?\n");
1778 		break;
1779 	default:
1780 		if (net_ratelimit())
1781 			netdev_warn(netdev, "Rx status %d\n", status);
1782 		break;
1783 	}
1784 
1785 	r8152_submit_rx(tp, agg, GFP_ATOMIC);
1786 }
1787 
1788 static void write_bulk_callback(struct urb *urb)
1789 {
1790 	struct net_device_stats *stats;
1791 	struct net_device *netdev;
1792 	struct tx_agg *agg;
1793 	struct r8152 *tp;
1794 	unsigned long flags;
1795 	int status = urb->status;
1796 
1797 	agg = urb->context;
1798 	if (!agg)
1799 		return;
1800 
1801 	tp = agg->context;
1802 	if (!tp)
1803 		return;
1804 
1805 	netdev = tp->netdev;
1806 	stats = &netdev->stats;
1807 	if (status) {
1808 		if (net_ratelimit())
1809 			netdev_warn(netdev, "Tx status %d\n", status);
1810 		stats->tx_errors += agg->skb_num;
1811 	} else {
1812 		stats->tx_packets += agg->skb_num;
1813 		stats->tx_bytes += agg->skb_len;
1814 	}
1815 
1816 	spin_lock_irqsave(&tp->tx_lock, flags);
1817 	list_add_tail(&agg->list, &tp->tx_free);
1818 	spin_unlock_irqrestore(&tp->tx_lock, flags);
1819 
1820 	usb_autopm_put_interface_async(tp->intf);
1821 
1822 	if (!netif_carrier_ok(netdev))
1823 		return;
1824 
1825 	if (!test_bit(WORK_ENABLE, &tp->flags))
1826 		return;
1827 
1828 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
1829 		return;
1830 
1831 	if (!skb_queue_empty(&tp->tx_queue))
1832 		tasklet_schedule(&tp->tx_tl);
1833 }
1834 
1835 static void intr_callback(struct urb *urb)
1836 {
1837 	struct r8152 *tp;
1838 	__le16 *d;
1839 	int status = urb->status;
1840 	int res;
1841 
1842 	tp = urb->context;
1843 	if (!tp)
1844 		return;
1845 
1846 	if (!test_bit(WORK_ENABLE, &tp->flags))
1847 		return;
1848 
1849 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
1850 		return;
1851 
1852 	switch (status) {
1853 	case 0:			/* success */
1854 		break;
1855 	case -ECONNRESET:	/* unlink */
1856 	case -ESHUTDOWN:
1857 		netif_device_detach(tp->netdev);
1858 		fallthrough;
1859 	case -ENOENT:
1860 	case -EPROTO:
1861 		netif_info(tp, intr, tp->netdev,
1862 			   "Stop submitting intr, status %d\n", status);
1863 		return;
1864 	case -EOVERFLOW:
1865 		netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n");
1866 		goto resubmit;
1867 	/* -EPIPE:  should clear the halt */
1868 	default:
1869 		netif_info(tp, intr, tp->netdev, "intr status %d\n", status);
1870 		goto resubmit;
1871 	}
1872 
1873 	d = urb->transfer_buffer;
1874 	if (INTR_LINK & __le16_to_cpu(d[0])) {
1875 		if (!netif_carrier_ok(tp->netdev)) {
1876 			set_bit(RTL8152_LINK_CHG, &tp->flags);
1877 			schedule_delayed_work(&tp->schedule, 0);
1878 		}
1879 	} else {
1880 		if (netif_carrier_ok(tp->netdev)) {
1881 			netif_stop_queue(tp->netdev);
1882 			set_bit(RTL8152_LINK_CHG, &tp->flags);
1883 			schedule_delayed_work(&tp->schedule, 0);
1884 		}
1885 	}
1886 
1887 resubmit:
1888 	res = usb_submit_urb(urb, GFP_ATOMIC);
1889 	if (res == -ENODEV) {
1890 		rtl_set_unplug(tp);
1891 		netif_device_detach(tp->netdev);
1892 	} else if (res) {
1893 		netif_err(tp, intr, tp->netdev,
1894 			  "can't resubmit intr, status %d\n", res);
1895 	}
1896 }
1897 
1898 static inline void *rx_agg_align(void *data)
1899 {
1900 	return (void *)ALIGN((uintptr_t)data, RX_ALIGN);
1901 }
1902 
1903 static inline void *tx_agg_align(void *data)
1904 {
1905 	return (void *)ALIGN((uintptr_t)data, TX_ALIGN);
1906 }
1907 
1908 static void free_rx_agg(struct r8152 *tp, struct rx_agg *agg)
1909 {
1910 	list_del(&agg->info_list);
1911 
1912 	usb_free_urb(agg->urb);
1913 	put_page(agg->page);
1914 	kfree(agg);
1915 
1916 	atomic_dec(&tp->rx_count);
1917 }
1918 
1919 static struct rx_agg *alloc_rx_agg(struct r8152 *tp, gfp_t mflags)
1920 {
1921 	struct net_device *netdev = tp->netdev;
1922 	int node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
1923 	unsigned int order = get_order(tp->rx_buf_sz);
1924 	struct rx_agg *rx_agg;
1925 	unsigned long flags;
1926 
1927 	rx_agg = kmalloc_node(sizeof(*rx_agg), mflags, node);
1928 	if (!rx_agg)
1929 		return NULL;
1930 
1931 	rx_agg->page = alloc_pages(mflags | __GFP_COMP, order);
1932 	if (!rx_agg->page)
1933 		goto free_rx;
1934 
1935 	rx_agg->buffer = page_address(rx_agg->page);
1936 
1937 	rx_agg->urb = usb_alloc_urb(0, mflags);
1938 	if (!rx_agg->urb)
1939 		goto free_buf;
1940 
1941 	rx_agg->context = tp;
1942 
1943 	INIT_LIST_HEAD(&rx_agg->list);
1944 	INIT_LIST_HEAD(&rx_agg->info_list);
1945 	spin_lock_irqsave(&tp->rx_lock, flags);
1946 	list_add_tail(&rx_agg->info_list, &tp->rx_info);
1947 	spin_unlock_irqrestore(&tp->rx_lock, flags);
1948 
1949 	atomic_inc(&tp->rx_count);
1950 
1951 	return rx_agg;
1952 
1953 free_buf:
1954 	__free_pages(rx_agg->page, order);
1955 free_rx:
1956 	kfree(rx_agg);
1957 	return NULL;
1958 }
1959 
1960 static void free_all_mem(struct r8152 *tp)
1961 {
1962 	struct rx_agg *agg, *agg_next;
1963 	unsigned long flags;
1964 	int i;
1965 
1966 	spin_lock_irqsave(&tp->rx_lock, flags);
1967 
1968 	list_for_each_entry_safe(agg, agg_next, &tp->rx_info, info_list)
1969 		free_rx_agg(tp, agg);
1970 
1971 	spin_unlock_irqrestore(&tp->rx_lock, flags);
1972 
1973 	WARN_ON(atomic_read(&tp->rx_count));
1974 
1975 	for (i = 0; i < RTL8152_MAX_TX; i++) {
1976 		usb_free_urb(tp->tx_info[i].urb);
1977 		tp->tx_info[i].urb = NULL;
1978 
1979 		kfree(tp->tx_info[i].buffer);
1980 		tp->tx_info[i].buffer = NULL;
1981 		tp->tx_info[i].head = NULL;
1982 	}
1983 
1984 	usb_free_urb(tp->intr_urb);
1985 	tp->intr_urb = NULL;
1986 
1987 	kfree(tp->intr_buff);
1988 	tp->intr_buff = NULL;
1989 }
1990 
1991 static int alloc_all_mem(struct r8152 *tp)
1992 {
1993 	struct net_device *netdev = tp->netdev;
1994 	struct usb_interface *intf = tp->intf;
1995 	struct usb_host_interface *alt = intf->cur_altsetting;
1996 	struct usb_host_endpoint *ep_intr = alt->endpoint + 2;
1997 	int node, i;
1998 
1999 	node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
2000 
2001 	spin_lock_init(&tp->rx_lock);
2002 	spin_lock_init(&tp->tx_lock);
2003 	INIT_LIST_HEAD(&tp->rx_info);
2004 	INIT_LIST_HEAD(&tp->tx_free);
2005 	INIT_LIST_HEAD(&tp->rx_done);
2006 	skb_queue_head_init(&tp->tx_queue);
2007 	skb_queue_head_init(&tp->rx_queue);
2008 	atomic_set(&tp->rx_count, 0);
2009 
2010 	for (i = 0; i < RTL8152_MAX_RX; i++) {
2011 		if (!alloc_rx_agg(tp, GFP_KERNEL))
2012 			goto err1;
2013 	}
2014 
2015 	for (i = 0; i < RTL8152_MAX_TX; i++) {
2016 		struct urb *urb;
2017 		u8 *buf;
2018 
2019 		buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node);
2020 		if (!buf)
2021 			goto err1;
2022 
2023 		if (buf != tx_agg_align(buf)) {
2024 			kfree(buf);
2025 			buf = kmalloc_node(agg_buf_sz + TX_ALIGN, GFP_KERNEL,
2026 					   node);
2027 			if (!buf)
2028 				goto err1;
2029 		}
2030 
2031 		urb = usb_alloc_urb(0, GFP_KERNEL);
2032 		if (!urb) {
2033 			kfree(buf);
2034 			goto err1;
2035 		}
2036 
2037 		INIT_LIST_HEAD(&tp->tx_info[i].list);
2038 		tp->tx_info[i].context = tp;
2039 		tp->tx_info[i].urb = urb;
2040 		tp->tx_info[i].buffer = buf;
2041 		tp->tx_info[i].head = tx_agg_align(buf);
2042 
2043 		list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
2044 	}
2045 
2046 	tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
2047 	if (!tp->intr_urb)
2048 		goto err1;
2049 
2050 	tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
2051 	if (!tp->intr_buff)
2052 		goto err1;
2053 
2054 	tp->intr_interval = (int)ep_intr->desc.bInterval;
2055 	usb_fill_int_urb(tp->intr_urb, tp->udev, tp->pipe_intr,
2056 			 tp->intr_buff, INTBUFSIZE, intr_callback,
2057 			 tp, tp->intr_interval);
2058 
2059 	return 0;
2060 
2061 err1:
2062 	free_all_mem(tp);
2063 	return -ENOMEM;
2064 }
2065 
2066 static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp)
2067 {
2068 	struct tx_agg *agg = NULL;
2069 	unsigned long flags;
2070 
2071 	if (list_empty(&tp->tx_free))
2072 		return NULL;
2073 
2074 	spin_lock_irqsave(&tp->tx_lock, flags);
2075 	if (!list_empty(&tp->tx_free)) {
2076 		struct list_head *cursor;
2077 
2078 		cursor = tp->tx_free.next;
2079 		list_del_init(cursor);
2080 		agg = list_entry(cursor, struct tx_agg, list);
2081 	}
2082 	spin_unlock_irqrestore(&tp->tx_lock, flags);
2083 
2084 	return agg;
2085 }
2086 
2087 /* r8152_csum_workaround()
2088  * The hw limits the value of the transport offset. When the offset is out of
2089  * range, calculate the checksum by sw.
2090  */
2091 static void r8152_csum_workaround(struct r8152 *tp, struct sk_buff *skb,
2092 				  struct sk_buff_head *list)
2093 {
2094 	if (skb_shinfo(skb)->gso_size) {
2095 		netdev_features_t features = tp->netdev->features;
2096 		struct sk_buff *segs, *seg, *next;
2097 		struct sk_buff_head seg_list;
2098 
2099 		features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
2100 		segs = skb_gso_segment(skb, features);
2101 		if (IS_ERR(segs) || !segs)
2102 			goto drop;
2103 
2104 		__skb_queue_head_init(&seg_list);
2105 
2106 		skb_list_walk_safe(segs, seg, next) {
2107 			skb_mark_not_on_list(seg);
2108 			__skb_queue_tail(&seg_list, seg);
2109 		}
2110 
2111 		skb_queue_splice(&seg_list, list);
2112 		dev_kfree_skb(skb);
2113 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
2114 		if (skb_checksum_help(skb) < 0)
2115 			goto drop;
2116 
2117 		__skb_queue_head(list, skb);
2118 	} else {
2119 		struct net_device_stats *stats;
2120 
2121 drop:
2122 		stats = &tp->netdev->stats;
2123 		stats->tx_dropped++;
2124 		dev_kfree_skb(skb);
2125 	}
2126 }
2127 
2128 static inline void rtl_tx_vlan_tag(struct tx_desc *desc, struct sk_buff *skb)
2129 {
2130 	if (skb_vlan_tag_present(skb)) {
2131 		u32 opts2;
2132 
2133 		opts2 = TX_VLAN_TAG | swab16(skb_vlan_tag_get(skb));
2134 		desc->opts2 |= cpu_to_le32(opts2);
2135 	}
2136 }
2137 
2138 static inline void rtl_rx_vlan_tag(struct rx_desc *desc, struct sk_buff *skb)
2139 {
2140 	u32 opts2 = le32_to_cpu(desc->opts2);
2141 
2142 	if (opts2 & RX_VLAN_TAG)
2143 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2144 				       swab16(opts2 & 0xffff));
2145 }
2146 
2147 static int r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc,
2148 			 struct sk_buff *skb, u32 len, u32 transport_offset)
2149 {
2150 	u32 mss = skb_shinfo(skb)->gso_size;
2151 	u32 opts1, opts2 = 0;
2152 	int ret = TX_CSUM_SUCCESS;
2153 
2154 	WARN_ON_ONCE(len > TX_LEN_MAX);
2155 
2156 	opts1 = len | TX_FS | TX_LS;
2157 
2158 	if (mss) {
2159 		if (transport_offset > GTTCPHO_MAX) {
2160 			netif_warn(tp, tx_err, tp->netdev,
2161 				   "Invalid transport offset 0x%x for TSO\n",
2162 				   transport_offset);
2163 			ret = TX_CSUM_TSO;
2164 			goto unavailable;
2165 		}
2166 
2167 		switch (vlan_get_protocol(skb)) {
2168 		case htons(ETH_P_IP):
2169 			opts1 |= GTSENDV4;
2170 			break;
2171 
2172 		case htons(ETH_P_IPV6):
2173 			if (skb_cow_head(skb, 0)) {
2174 				ret = TX_CSUM_TSO;
2175 				goto unavailable;
2176 			}
2177 			tcp_v6_gso_csum_prep(skb);
2178 			opts1 |= GTSENDV6;
2179 			break;
2180 
2181 		default:
2182 			WARN_ON_ONCE(1);
2183 			break;
2184 		}
2185 
2186 		opts1 |= transport_offset << GTTCPHO_SHIFT;
2187 		opts2 |= min(mss, MSS_MAX) << MSS_SHIFT;
2188 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
2189 		u8 ip_protocol;
2190 
2191 		if (transport_offset > TCPHO_MAX) {
2192 			netif_warn(tp, tx_err, tp->netdev,
2193 				   "Invalid transport offset 0x%x\n",
2194 				   transport_offset);
2195 			ret = TX_CSUM_NONE;
2196 			goto unavailable;
2197 		}
2198 
2199 		switch (vlan_get_protocol(skb)) {
2200 		case htons(ETH_P_IP):
2201 			opts2 |= IPV4_CS;
2202 			ip_protocol = ip_hdr(skb)->protocol;
2203 			break;
2204 
2205 		case htons(ETH_P_IPV6):
2206 			opts2 |= IPV6_CS;
2207 			ip_protocol = ipv6_hdr(skb)->nexthdr;
2208 			break;
2209 
2210 		default:
2211 			ip_protocol = IPPROTO_RAW;
2212 			break;
2213 		}
2214 
2215 		if (ip_protocol == IPPROTO_TCP)
2216 			opts2 |= TCP_CS;
2217 		else if (ip_protocol == IPPROTO_UDP)
2218 			opts2 |= UDP_CS;
2219 		else
2220 			WARN_ON_ONCE(1);
2221 
2222 		opts2 |= transport_offset << TCPHO_SHIFT;
2223 	}
2224 
2225 	desc->opts2 = cpu_to_le32(opts2);
2226 	desc->opts1 = cpu_to_le32(opts1);
2227 
2228 unavailable:
2229 	return ret;
2230 }
2231 
2232 static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg)
2233 {
2234 	struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
2235 	int remain, ret;
2236 	u8 *tx_data;
2237 
2238 	__skb_queue_head_init(&skb_head);
2239 	spin_lock(&tx_queue->lock);
2240 	skb_queue_splice_init(tx_queue, &skb_head);
2241 	spin_unlock(&tx_queue->lock);
2242 
2243 	tx_data = agg->head;
2244 	agg->skb_num = 0;
2245 	agg->skb_len = 0;
2246 	remain = agg_buf_sz;
2247 
2248 	while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) {
2249 		struct tx_desc *tx_desc;
2250 		struct sk_buff *skb;
2251 		unsigned int len;
2252 		u32 offset;
2253 
2254 		skb = __skb_dequeue(&skb_head);
2255 		if (!skb)
2256 			break;
2257 
2258 		len = skb->len + sizeof(*tx_desc);
2259 
2260 		if (len > remain) {
2261 			__skb_queue_head(&skb_head, skb);
2262 			break;
2263 		}
2264 
2265 		tx_data = tx_agg_align(tx_data);
2266 		tx_desc = (struct tx_desc *)tx_data;
2267 
2268 		offset = (u32)skb_transport_offset(skb);
2269 
2270 		if (r8152_tx_csum(tp, tx_desc, skb, skb->len, offset)) {
2271 			r8152_csum_workaround(tp, skb, &skb_head);
2272 			continue;
2273 		}
2274 
2275 		rtl_tx_vlan_tag(tx_desc, skb);
2276 
2277 		tx_data += sizeof(*tx_desc);
2278 
2279 		len = skb->len;
2280 		if (skb_copy_bits(skb, 0, tx_data, len) < 0) {
2281 			struct net_device_stats *stats = &tp->netdev->stats;
2282 
2283 			stats->tx_dropped++;
2284 			dev_kfree_skb_any(skb);
2285 			tx_data -= sizeof(*tx_desc);
2286 			continue;
2287 		}
2288 
2289 		tx_data += len;
2290 		agg->skb_len += len;
2291 		agg->skb_num += skb_shinfo(skb)->gso_segs ?: 1;
2292 
2293 		dev_kfree_skb_any(skb);
2294 
2295 		remain = agg_buf_sz - (int)(tx_agg_align(tx_data) - agg->head);
2296 
2297 		if (tp->dell_tb_rx_agg_bug)
2298 			break;
2299 	}
2300 
2301 	if (!skb_queue_empty(&skb_head)) {
2302 		spin_lock(&tx_queue->lock);
2303 		skb_queue_splice(&skb_head, tx_queue);
2304 		spin_unlock(&tx_queue->lock);
2305 	}
2306 
2307 	netif_tx_lock(tp->netdev);
2308 
2309 	if (netif_queue_stopped(tp->netdev) &&
2310 	    skb_queue_len(&tp->tx_queue) < tp->tx_qlen)
2311 		netif_wake_queue(tp->netdev);
2312 
2313 	netif_tx_unlock(tp->netdev);
2314 
2315 	ret = usb_autopm_get_interface_async(tp->intf);
2316 	if (ret < 0)
2317 		goto out_tx_fill;
2318 
2319 	usb_fill_bulk_urb(agg->urb, tp->udev, tp->pipe_out,
2320 			  agg->head, (int)(tx_data - (u8 *)agg->head),
2321 			  (usb_complete_t)write_bulk_callback, agg);
2322 
2323 	ret = usb_submit_urb(agg->urb, GFP_ATOMIC);
2324 	if (ret < 0)
2325 		usb_autopm_put_interface_async(tp->intf);
2326 
2327 out_tx_fill:
2328 	return ret;
2329 }
2330 
2331 static u8 r8152_rx_csum(struct r8152 *tp, struct rx_desc *rx_desc)
2332 {
2333 	u8 checksum = CHECKSUM_NONE;
2334 	u32 opts2, opts3;
2335 
2336 	if (!(tp->netdev->features & NETIF_F_RXCSUM))
2337 		goto return_result;
2338 
2339 	opts2 = le32_to_cpu(rx_desc->opts2);
2340 	opts3 = le32_to_cpu(rx_desc->opts3);
2341 
2342 	if (opts2 & RD_IPV4_CS) {
2343 		if (opts3 & IPF)
2344 			checksum = CHECKSUM_NONE;
2345 		else if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
2346 			checksum = CHECKSUM_UNNECESSARY;
2347 		else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
2348 			checksum = CHECKSUM_UNNECESSARY;
2349 	} else if (opts2 & RD_IPV6_CS) {
2350 		if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
2351 			checksum = CHECKSUM_UNNECESSARY;
2352 		else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
2353 			checksum = CHECKSUM_UNNECESSARY;
2354 	}
2355 
2356 return_result:
2357 	return checksum;
2358 }
2359 
2360 static inline bool rx_count_exceed(struct r8152 *tp)
2361 {
2362 	return atomic_read(&tp->rx_count) > RTL8152_MAX_RX;
2363 }
2364 
2365 static inline int agg_offset(struct rx_agg *agg, void *addr)
2366 {
2367 	return (int)(addr - agg->buffer);
2368 }
2369 
2370 static struct rx_agg *rtl_get_free_rx(struct r8152 *tp, gfp_t mflags)
2371 {
2372 	struct rx_agg *agg, *agg_next, *agg_free = NULL;
2373 	unsigned long flags;
2374 
2375 	spin_lock_irqsave(&tp->rx_lock, flags);
2376 
2377 	list_for_each_entry_safe(agg, agg_next, &tp->rx_used, list) {
2378 		if (page_count(agg->page) == 1) {
2379 			if (!agg_free) {
2380 				list_del_init(&agg->list);
2381 				agg_free = agg;
2382 				continue;
2383 			}
2384 			if (rx_count_exceed(tp)) {
2385 				list_del_init(&agg->list);
2386 				free_rx_agg(tp, agg);
2387 			}
2388 			break;
2389 		}
2390 	}
2391 
2392 	spin_unlock_irqrestore(&tp->rx_lock, flags);
2393 
2394 	if (!agg_free && atomic_read(&tp->rx_count) < tp->rx_pending)
2395 		agg_free = alloc_rx_agg(tp, mflags);
2396 
2397 	return agg_free;
2398 }
2399 
2400 static int rx_bottom(struct r8152 *tp, int budget)
2401 {
2402 	unsigned long flags;
2403 	struct list_head *cursor, *next, rx_queue;
2404 	int ret = 0, work_done = 0;
2405 	struct napi_struct *napi = &tp->napi;
2406 
2407 	if (!skb_queue_empty(&tp->rx_queue)) {
2408 		while (work_done < budget) {
2409 			struct sk_buff *skb = __skb_dequeue(&tp->rx_queue);
2410 			struct net_device *netdev = tp->netdev;
2411 			struct net_device_stats *stats = &netdev->stats;
2412 			unsigned int pkt_len;
2413 
2414 			if (!skb)
2415 				break;
2416 
2417 			pkt_len = skb->len;
2418 			napi_gro_receive(napi, skb);
2419 			work_done++;
2420 			stats->rx_packets++;
2421 			stats->rx_bytes += pkt_len;
2422 		}
2423 	}
2424 
2425 	if (list_empty(&tp->rx_done))
2426 		goto out1;
2427 
2428 	INIT_LIST_HEAD(&rx_queue);
2429 	spin_lock_irqsave(&tp->rx_lock, flags);
2430 	list_splice_init(&tp->rx_done, &rx_queue);
2431 	spin_unlock_irqrestore(&tp->rx_lock, flags);
2432 
2433 	list_for_each_safe(cursor, next, &rx_queue) {
2434 		struct rx_desc *rx_desc;
2435 		struct rx_agg *agg, *agg_free;
2436 		int len_used = 0;
2437 		struct urb *urb;
2438 		u8 *rx_data;
2439 
2440 		list_del_init(cursor);
2441 
2442 		agg = list_entry(cursor, struct rx_agg, list);
2443 		urb = agg->urb;
2444 		if (urb->actual_length < ETH_ZLEN)
2445 			goto submit;
2446 
2447 		agg_free = rtl_get_free_rx(tp, GFP_ATOMIC);
2448 
2449 		rx_desc = agg->buffer;
2450 		rx_data = agg->buffer;
2451 		len_used += sizeof(struct rx_desc);
2452 
2453 		while (urb->actual_length > len_used) {
2454 			struct net_device *netdev = tp->netdev;
2455 			struct net_device_stats *stats = &netdev->stats;
2456 			unsigned int pkt_len, rx_frag_head_sz;
2457 			struct sk_buff *skb;
2458 
2459 			/* limit the skb numbers for rx_queue */
2460 			if (unlikely(skb_queue_len(&tp->rx_queue) >= 1000))
2461 				break;
2462 
2463 			pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
2464 			if (pkt_len < ETH_ZLEN)
2465 				break;
2466 
2467 			len_used += pkt_len;
2468 			if (urb->actual_length < len_used)
2469 				break;
2470 
2471 			pkt_len -= ETH_FCS_LEN;
2472 			rx_data += sizeof(struct rx_desc);
2473 
2474 			if (!agg_free || tp->rx_copybreak > pkt_len)
2475 				rx_frag_head_sz = pkt_len;
2476 			else
2477 				rx_frag_head_sz = tp->rx_copybreak;
2478 
2479 			skb = napi_alloc_skb(napi, rx_frag_head_sz);
2480 			if (!skb) {
2481 				stats->rx_dropped++;
2482 				goto find_next_rx;
2483 			}
2484 
2485 			skb->ip_summed = r8152_rx_csum(tp, rx_desc);
2486 			memcpy(skb->data, rx_data, rx_frag_head_sz);
2487 			skb_put(skb, rx_frag_head_sz);
2488 			pkt_len -= rx_frag_head_sz;
2489 			rx_data += rx_frag_head_sz;
2490 			if (pkt_len) {
2491 				skb_add_rx_frag(skb, 0, agg->page,
2492 						agg_offset(agg, rx_data),
2493 						pkt_len,
2494 						SKB_DATA_ALIGN(pkt_len));
2495 				get_page(agg->page);
2496 			}
2497 
2498 			skb->protocol = eth_type_trans(skb, netdev);
2499 			rtl_rx_vlan_tag(rx_desc, skb);
2500 			if (work_done < budget) {
2501 				work_done++;
2502 				stats->rx_packets++;
2503 				stats->rx_bytes += skb->len;
2504 				napi_gro_receive(napi, skb);
2505 			} else {
2506 				__skb_queue_tail(&tp->rx_queue, skb);
2507 			}
2508 
2509 find_next_rx:
2510 			rx_data = rx_agg_align(rx_data + pkt_len + ETH_FCS_LEN);
2511 			rx_desc = (struct rx_desc *)rx_data;
2512 			len_used = agg_offset(agg, rx_data);
2513 			len_used += sizeof(struct rx_desc);
2514 		}
2515 
2516 		WARN_ON(!agg_free && page_count(agg->page) > 1);
2517 
2518 		if (agg_free) {
2519 			spin_lock_irqsave(&tp->rx_lock, flags);
2520 			if (page_count(agg->page) == 1) {
2521 				list_add(&agg_free->list, &tp->rx_used);
2522 			} else {
2523 				list_add_tail(&agg->list, &tp->rx_used);
2524 				agg = agg_free;
2525 				urb = agg->urb;
2526 			}
2527 			spin_unlock_irqrestore(&tp->rx_lock, flags);
2528 		}
2529 
2530 submit:
2531 		if (!ret) {
2532 			ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
2533 		} else {
2534 			urb->actual_length = 0;
2535 			list_add_tail(&agg->list, next);
2536 		}
2537 	}
2538 
2539 	if (!list_empty(&rx_queue)) {
2540 		spin_lock_irqsave(&tp->rx_lock, flags);
2541 		list_splice_tail(&rx_queue, &tp->rx_done);
2542 		spin_unlock_irqrestore(&tp->rx_lock, flags);
2543 	}
2544 
2545 out1:
2546 	return work_done;
2547 }
2548 
2549 static void tx_bottom(struct r8152 *tp)
2550 {
2551 	int res;
2552 
2553 	do {
2554 		struct net_device *netdev = tp->netdev;
2555 		struct tx_agg *agg;
2556 
2557 		if (skb_queue_empty(&tp->tx_queue))
2558 			break;
2559 
2560 		agg = r8152_get_tx_agg(tp);
2561 		if (!agg)
2562 			break;
2563 
2564 		res = r8152_tx_agg_fill(tp, agg);
2565 		if (!res)
2566 			continue;
2567 
2568 		if (res == -ENODEV) {
2569 			rtl_set_unplug(tp);
2570 			netif_device_detach(netdev);
2571 		} else {
2572 			struct net_device_stats *stats = &netdev->stats;
2573 			unsigned long flags;
2574 
2575 			netif_warn(tp, tx_err, netdev,
2576 				   "failed tx_urb %d\n", res);
2577 			stats->tx_dropped += agg->skb_num;
2578 
2579 			spin_lock_irqsave(&tp->tx_lock, flags);
2580 			list_add_tail(&agg->list, &tp->tx_free);
2581 			spin_unlock_irqrestore(&tp->tx_lock, flags);
2582 		}
2583 	} while (res == 0);
2584 }
2585 
2586 static void bottom_half(struct tasklet_struct *t)
2587 {
2588 	struct r8152 *tp = from_tasklet(tp, t, tx_tl);
2589 
2590 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
2591 		return;
2592 
2593 	if (!test_bit(WORK_ENABLE, &tp->flags))
2594 		return;
2595 
2596 	/* When link down, the driver would cancel all bulks. */
2597 	/* This avoid the re-submitting bulk */
2598 	if (!netif_carrier_ok(tp->netdev))
2599 		return;
2600 
2601 	clear_bit(SCHEDULE_TASKLET, &tp->flags);
2602 
2603 	tx_bottom(tp);
2604 }
2605 
2606 static int r8152_poll(struct napi_struct *napi, int budget)
2607 {
2608 	struct r8152 *tp = container_of(napi, struct r8152, napi);
2609 	int work_done;
2610 
2611 	work_done = rx_bottom(tp, budget);
2612 
2613 	if (work_done < budget) {
2614 		if (!napi_complete_done(napi, work_done))
2615 			goto out;
2616 		if (!list_empty(&tp->rx_done))
2617 			napi_schedule(napi);
2618 	}
2619 
2620 out:
2621 	return work_done;
2622 }
2623 
2624 static
2625 int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
2626 {
2627 	int ret;
2628 
2629 	/* The rx would be stopped, so skip submitting */
2630 	if (test_bit(RTL8152_UNPLUG, &tp->flags) ||
2631 	    !test_bit(WORK_ENABLE, &tp->flags) || !netif_carrier_ok(tp->netdev))
2632 		return 0;
2633 
2634 	usb_fill_bulk_urb(agg->urb, tp->udev, tp->pipe_in,
2635 			  agg->buffer, tp->rx_buf_sz,
2636 			  (usb_complete_t)read_bulk_callback, agg);
2637 
2638 	ret = usb_submit_urb(agg->urb, mem_flags);
2639 	if (ret == -ENODEV) {
2640 		rtl_set_unplug(tp);
2641 		netif_device_detach(tp->netdev);
2642 	} else if (ret) {
2643 		struct urb *urb = agg->urb;
2644 		unsigned long flags;
2645 
2646 		urb->actual_length = 0;
2647 		spin_lock_irqsave(&tp->rx_lock, flags);
2648 		list_add_tail(&agg->list, &tp->rx_done);
2649 		spin_unlock_irqrestore(&tp->rx_lock, flags);
2650 
2651 		netif_err(tp, rx_err, tp->netdev,
2652 			  "Couldn't submit rx[%p], ret = %d\n", agg, ret);
2653 
2654 		napi_schedule(&tp->napi);
2655 	}
2656 
2657 	return ret;
2658 }
2659 
2660 static void rtl_drop_queued_tx(struct r8152 *tp)
2661 {
2662 	struct net_device_stats *stats = &tp->netdev->stats;
2663 	struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
2664 	struct sk_buff *skb;
2665 
2666 	if (skb_queue_empty(tx_queue))
2667 		return;
2668 
2669 	__skb_queue_head_init(&skb_head);
2670 	spin_lock_bh(&tx_queue->lock);
2671 	skb_queue_splice_init(tx_queue, &skb_head);
2672 	spin_unlock_bh(&tx_queue->lock);
2673 
2674 	while ((skb = __skb_dequeue(&skb_head))) {
2675 		dev_kfree_skb(skb);
2676 		stats->tx_dropped++;
2677 	}
2678 }
2679 
2680 static void rtl8152_tx_timeout(struct net_device *netdev, unsigned int txqueue)
2681 {
2682 	struct r8152 *tp = netdev_priv(netdev);
2683 
2684 	netif_warn(tp, tx_err, netdev, "Tx timeout\n");
2685 
2686 	usb_queue_reset_device(tp->intf);
2687 }
2688 
2689 static void rtl8152_set_rx_mode(struct net_device *netdev)
2690 {
2691 	struct r8152 *tp = netdev_priv(netdev);
2692 
2693 	if (netif_carrier_ok(netdev)) {
2694 		set_bit(RTL8152_SET_RX_MODE, &tp->flags);
2695 		schedule_delayed_work(&tp->schedule, 0);
2696 	}
2697 }
2698 
2699 static void _rtl8152_set_rx_mode(struct net_device *netdev)
2700 {
2701 	struct r8152 *tp = netdev_priv(netdev);
2702 	u32 mc_filter[2];	/* Multicast hash filter */
2703 	__le32 tmp[2];
2704 	u32 ocp_data;
2705 
2706 	netif_stop_queue(netdev);
2707 	ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2708 	ocp_data &= ~RCR_ACPT_ALL;
2709 	ocp_data |= RCR_AB | RCR_APM;
2710 
2711 	if (netdev->flags & IFF_PROMISC) {
2712 		/* Unconditionally log net taps. */
2713 		netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
2714 		ocp_data |= RCR_AM | RCR_AAP;
2715 		mc_filter[1] = 0xffffffff;
2716 		mc_filter[0] = 0xffffffff;
2717 	} else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
2718 		   (netdev->flags & IFF_ALLMULTI)) {
2719 		/* Too many to filter perfectly -- accept all multicasts. */
2720 		ocp_data |= RCR_AM;
2721 		mc_filter[1] = 0xffffffff;
2722 		mc_filter[0] = 0xffffffff;
2723 	} else {
2724 		struct netdev_hw_addr *ha;
2725 
2726 		mc_filter[1] = 0;
2727 		mc_filter[0] = 0;
2728 		netdev_for_each_mc_addr(ha, netdev) {
2729 			int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
2730 
2731 			mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
2732 			ocp_data |= RCR_AM;
2733 		}
2734 	}
2735 
2736 	tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
2737 	tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
2738 
2739 	pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
2740 	ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2741 	netif_wake_queue(netdev);
2742 }
2743 
2744 static netdev_features_t
2745 rtl8152_features_check(struct sk_buff *skb, struct net_device *dev,
2746 		       netdev_features_t features)
2747 {
2748 	u32 mss = skb_shinfo(skb)->gso_size;
2749 	int max_offset = mss ? GTTCPHO_MAX : TCPHO_MAX;
2750 	int offset = skb_transport_offset(skb);
2751 
2752 	if ((mss || skb->ip_summed == CHECKSUM_PARTIAL) && offset > max_offset)
2753 		features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2754 	else if ((skb->len + sizeof(struct tx_desc)) > agg_buf_sz)
2755 		features &= ~NETIF_F_GSO_MASK;
2756 
2757 	return features;
2758 }
2759 
2760 static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
2761 				      struct net_device *netdev)
2762 {
2763 	struct r8152 *tp = netdev_priv(netdev);
2764 
2765 	skb_tx_timestamp(skb);
2766 
2767 	skb_queue_tail(&tp->tx_queue, skb);
2768 
2769 	if (!list_empty(&tp->tx_free)) {
2770 		if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
2771 			set_bit(SCHEDULE_TASKLET, &tp->flags);
2772 			schedule_delayed_work(&tp->schedule, 0);
2773 		} else {
2774 			usb_mark_last_busy(tp->udev);
2775 			tasklet_schedule(&tp->tx_tl);
2776 		}
2777 	} else if (skb_queue_len(&tp->tx_queue) > tp->tx_qlen) {
2778 		netif_stop_queue(netdev);
2779 	}
2780 
2781 	return NETDEV_TX_OK;
2782 }
2783 
2784 static void r8152b_reset_packet_filter(struct r8152 *tp)
2785 {
2786 	u32 ocp_data;
2787 
2788 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
2789 	ocp_data &= ~FMC_FCR_MCU_EN;
2790 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
2791 	ocp_data |= FMC_FCR_MCU_EN;
2792 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
2793 }
2794 
2795 static void rtl8152_nic_reset(struct r8152 *tp)
2796 {
2797 	u32 ocp_data;
2798 	int i;
2799 
2800 	switch (tp->version) {
2801 	case RTL_TEST_01:
2802 	case RTL_VER_10:
2803 	case RTL_VER_11:
2804 		ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
2805 		ocp_data &= ~CR_TE;
2806 		ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
2807 
2808 		ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_BMU_RESET);
2809 		ocp_data &= ~BMU_RESET_EP_IN;
2810 		ocp_write_word(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
2811 
2812 		ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
2813 		ocp_data |= CDC_ECM_EN;
2814 		ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
2815 
2816 		ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
2817 		ocp_data &= ~CR_RE;
2818 		ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
2819 
2820 		ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_BMU_RESET);
2821 		ocp_data |= BMU_RESET_EP_IN;
2822 		ocp_write_word(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
2823 
2824 		ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
2825 		ocp_data &= ~CDC_ECM_EN;
2826 		ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
2827 		break;
2828 
2829 	default:
2830 		ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
2831 
2832 		for (i = 0; i < 1000; i++) {
2833 			if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
2834 				break;
2835 			usleep_range(100, 400);
2836 		}
2837 		break;
2838 	}
2839 }
2840 
2841 static void set_tx_qlen(struct r8152 *tp)
2842 {
2843 	tp->tx_qlen = agg_buf_sz / (mtu_to_size(tp->netdev->mtu) + sizeof(struct tx_desc));
2844 }
2845 
2846 static inline u16 rtl8152_get_speed(struct r8152 *tp)
2847 {
2848 	return ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
2849 }
2850 
2851 static void rtl_eee_plus_en(struct r8152 *tp, bool enable)
2852 {
2853 	u32 ocp_data;
2854 
2855 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
2856 	if (enable)
2857 		ocp_data |= EEEP_CR_EEEP_TX;
2858 	else
2859 		ocp_data &= ~EEEP_CR_EEEP_TX;
2860 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
2861 }
2862 
2863 static void rtl_set_eee_plus(struct r8152 *tp)
2864 {
2865 	if (rtl8152_get_speed(tp) & _10bps)
2866 		rtl_eee_plus_en(tp, true);
2867 	else
2868 		rtl_eee_plus_en(tp, false);
2869 }
2870 
2871 static void rxdy_gated_en(struct r8152 *tp, bool enable)
2872 {
2873 	u32 ocp_data;
2874 
2875 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
2876 	if (enable)
2877 		ocp_data |= RXDY_GATED_EN;
2878 	else
2879 		ocp_data &= ~RXDY_GATED_EN;
2880 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
2881 }
2882 
2883 static int rtl_start_rx(struct r8152 *tp)
2884 {
2885 	struct rx_agg *agg, *agg_next;
2886 	struct list_head tmp_list;
2887 	unsigned long flags;
2888 	int ret = 0, i = 0;
2889 
2890 	INIT_LIST_HEAD(&tmp_list);
2891 
2892 	spin_lock_irqsave(&tp->rx_lock, flags);
2893 
2894 	INIT_LIST_HEAD(&tp->rx_done);
2895 	INIT_LIST_HEAD(&tp->rx_used);
2896 
2897 	list_splice_init(&tp->rx_info, &tmp_list);
2898 
2899 	spin_unlock_irqrestore(&tp->rx_lock, flags);
2900 
2901 	list_for_each_entry_safe(agg, agg_next, &tmp_list, info_list) {
2902 		INIT_LIST_HEAD(&agg->list);
2903 
2904 		/* Only RTL8152_MAX_RX rx_agg need to be submitted. */
2905 		if (++i > RTL8152_MAX_RX) {
2906 			spin_lock_irqsave(&tp->rx_lock, flags);
2907 			list_add_tail(&agg->list, &tp->rx_used);
2908 			spin_unlock_irqrestore(&tp->rx_lock, flags);
2909 		} else if (unlikely(ret < 0)) {
2910 			spin_lock_irqsave(&tp->rx_lock, flags);
2911 			list_add_tail(&agg->list, &tp->rx_done);
2912 			spin_unlock_irqrestore(&tp->rx_lock, flags);
2913 		} else {
2914 			ret = r8152_submit_rx(tp, agg, GFP_KERNEL);
2915 		}
2916 	}
2917 
2918 	spin_lock_irqsave(&tp->rx_lock, flags);
2919 	WARN_ON(!list_empty(&tp->rx_info));
2920 	list_splice(&tmp_list, &tp->rx_info);
2921 	spin_unlock_irqrestore(&tp->rx_lock, flags);
2922 
2923 	return ret;
2924 }
2925 
2926 static int rtl_stop_rx(struct r8152 *tp)
2927 {
2928 	struct rx_agg *agg, *agg_next;
2929 	struct list_head tmp_list;
2930 	unsigned long flags;
2931 
2932 	INIT_LIST_HEAD(&tmp_list);
2933 
2934 	/* The usb_kill_urb() couldn't be used in atomic.
2935 	 * Therefore, move the list of rx_info to a tmp one.
2936 	 * Then, list_for_each_entry_safe could be used without
2937 	 * spin lock.
2938 	 */
2939 
2940 	spin_lock_irqsave(&tp->rx_lock, flags);
2941 	list_splice_init(&tp->rx_info, &tmp_list);
2942 	spin_unlock_irqrestore(&tp->rx_lock, flags);
2943 
2944 	list_for_each_entry_safe(agg, agg_next, &tmp_list, info_list) {
2945 		/* At least RTL8152_MAX_RX rx_agg have the page_count being
2946 		 * equal to 1, so the other ones could be freed safely.
2947 		 */
2948 		if (page_count(agg->page) > 1)
2949 			free_rx_agg(tp, agg);
2950 		else
2951 			usb_kill_urb(agg->urb);
2952 	}
2953 
2954 	/* Move back the list of temp to the rx_info */
2955 	spin_lock_irqsave(&tp->rx_lock, flags);
2956 	WARN_ON(!list_empty(&tp->rx_info));
2957 	list_splice(&tmp_list, &tp->rx_info);
2958 	spin_unlock_irqrestore(&tp->rx_lock, flags);
2959 
2960 	while (!skb_queue_empty(&tp->rx_queue))
2961 		dev_kfree_skb(__skb_dequeue(&tp->rx_queue));
2962 
2963 	return 0;
2964 }
2965 
2966 static void rtl_set_ifg(struct r8152 *tp, u16 speed)
2967 {
2968 	u32 ocp_data;
2969 
2970 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR1);
2971 	ocp_data &= ~IFG_MASK;
2972 	if ((speed & (_10bps | _100bps)) && !(speed & FULL_DUP)) {
2973 		ocp_data |= IFG_144NS;
2974 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR1, ocp_data);
2975 
2976 		ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4);
2977 		ocp_data &= ~TX10MIDLE_EN;
2978 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data);
2979 	} else {
2980 		ocp_data |= IFG_96NS;
2981 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR1, ocp_data);
2982 
2983 		ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4);
2984 		ocp_data |= TX10MIDLE_EN;
2985 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data);
2986 	}
2987 }
2988 
2989 static inline void r8153b_rx_agg_chg_indicate(struct r8152 *tp)
2990 {
2991 	ocp_write_byte(tp, MCU_TYPE_USB, USB_UPT_RXDMA_OWN,
2992 		       OWN_UPDATE | OWN_CLEAR);
2993 }
2994 
2995 static int rtl_enable(struct r8152 *tp)
2996 {
2997 	u32 ocp_data;
2998 
2999 	r8152b_reset_packet_filter(tp);
3000 
3001 	ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
3002 	ocp_data |= CR_RE | CR_TE;
3003 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
3004 
3005 	switch (tp->version) {
3006 	case RTL_VER_08:
3007 	case RTL_VER_09:
3008 	case RTL_VER_14:
3009 		r8153b_rx_agg_chg_indicate(tp);
3010 		break;
3011 	default:
3012 		break;
3013 	}
3014 
3015 	rxdy_gated_en(tp, false);
3016 
3017 	return 0;
3018 }
3019 
3020 static int rtl8152_enable(struct r8152 *tp)
3021 {
3022 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
3023 		return -ENODEV;
3024 
3025 	set_tx_qlen(tp);
3026 	rtl_set_eee_plus(tp);
3027 
3028 	return rtl_enable(tp);
3029 }
3030 
3031 static void r8153_set_rx_early_timeout(struct r8152 *tp)
3032 {
3033 	u32 ocp_data = tp->coalesce / 8;
3034 
3035 	switch (tp->version) {
3036 	case RTL_VER_03:
3037 	case RTL_VER_04:
3038 	case RTL_VER_05:
3039 	case RTL_VER_06:
3040 		ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
3041 			       ocp_data);
3042 		break;
3043 
3044 	case RTL_VER_08:
3045 	case RTL_VER_09:
3046 	case RTL_VER_14:
3047 		/* The RTL8153B uses USB_RX_EXTRA_AGGR_TMR for rx timeout
3048 		 * primarily. For USB_RX_EARLY_TIMEOUT, we fix it to 128ns.
3049 		 */
3050 		ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
3051 			       128 / 8);
3052 		ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EXTRA_AGGR_TMR,
3053 			       ocp_data);
3054 		break;
3055 
3056 	case RTL_VER_10:
3057 	case RTL_VER_11:
3058 	case RTL_VER_12:
3059 	case RTL_VER_13:
3060 	case RTL_VER_15:
3061 		ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
3062 			       640 / 8);
3063 		ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EXTRA_AGGR_TMR,
3064 			       ocp_data);
3065 		r8153b_rx_agg_chg_indicate(tp);
3066 		break;
3067 
3068 	default:
3069 		break;
3070 	}
3071 }
3072 
3073 static void r8153_set_rx_early_size(struct r8152 *tp)
3074 {
3075 	u32 ocp_data = tp->rx_buf_sz - rx_reserved_size(tp->netdev->mtu);
3076 
3077 	switch (tp->version) {
3078 	case RTL_VER_03:
3079 	case RTL_VER_04:
3080 	case RTL_VER_05:
3081 	case RTL_VER_06:
3082 		ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
3083 			       ocp_data / 4);
3084 		break;
3085 	case RTL_VER_08:
3086 	case RTL_VER_09:
3087 	case RTL_VER_14:
3088 		ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
3089 			       ocp_data / 8);
3090 		break;
3091 	case RTL_TEST_01:
3092 	case RTL_VER_10:
3093 	case RTL_VER_11:
3094 	case RTL_VER_12:
3095 	case RTL_VER_13:
3096 	case RTL_VER_15:
3097 		ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
3098 			       ocp_data / 8);
3099 		r8153b_rx_agg_chg_indicate(tp);
3100 		break;
3101 	default:
3102 		WARN_ON_ONCE(1);
3103 		break;
3104 	}
3105 }
3106 
3107 static int rtl8153_enable(struct r8152 *tp)
3108 {
3109 	u32 ocp_data;
3110 
3111 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
3112 		return -ENODEV;
3113 
3114 	set_tx_qlen(tp);
3115 	rtl_set_eee_plus(tp);
3116 	r8153_set_rx_early_timeout(tp);
3117 	r8153_set_rx_early_size(tp);
3118 
3119 	rtl_set_ifg(tp, rtl8152_get_speed(tp));
3120 
3121 	switch (tp->version) {
3122 	case RTL_VER_09:
3123 	case RTL_VER_14:
3124 		ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK);
3125 		ocp_data &= ~FC_PATCH_TASK;
3126 		ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
3127 		usleep_range(1000, 2000);
3128 		ocp_data |= FC_PATCH_TASK;
3129 		ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
3130 		break;
3131 	default:
3132 		break;
3133 	}
3134 
3135 	return rtl_enable(tp);
3136 }
3137 
3138 static void rtl_disable(struct r8152 *tp)
3139 {
3140 	u32 ocp_data;
3141 	int i;
3142 
3143 	if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
3144 		rtl_drop_queued_tx(tp);
3145 		return;
3146 	}
3147 
3148 	ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
3149 	ocp_data &= ~RCR_ACPT_ALL;
3150 	ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
3151 
3152 	rtl_drop_queued_tx(tp);
3153 
3154 	for (i = 0; i < RTL8152_MAX_TX; i++)
3155 		usb_kill_urb(tp->tx_info[i].urb);
3156 
3157 	rxdy_gated_en(tp, true);
3158 
3159 	for (i = 0; i < 1000; i++) {
3160 		ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3161 		if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
3162 			break;
3163 		usleep_range(1000, 2000);
3164 	}
3165 
3166 	for (i = 0; i < 1000; i++) {
3167 		if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
3168 			break;
3169 		usleep_range(1000, 2000);
3170 	}
3171 
3172 	rtl_stop_rx(tp);
3173 
3174 	rtl8152_nic_reset(tp);
3175 }
3176 
3177 static void r8152_power_cut_en(struct r8152 *tp, bool enable)
3178 {
3179 	u32 ocp_data;
3180 
3181 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
3182 	if (enable)
3183 		ocp_data |= POWER_CUT;
3184 	else
3185 		ocp_data &= ~POWER_CUT;
3186 	ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
3187 
3188 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
3189 	ocp_data &= ~RESUME_INDICATE;
3190 	ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
3191 }
3192 
3193 static void rtl_rx_vlan_en(struct r8152 *tp, bool enable)
3194 {
3195 	u32 ocp_data;
3196 
3197 	switch (tp->version) {
3198 	case RTL_VER_01:
3199 	case RTL_VER_02:
3200 	case RTL_VER_03:
3201 	case RTL_VER_04:
3202 	case RTL_VER_05:
3203 	case RTL_VER_06:
3204 	case RTL_VER_07:
3205 	case RTL_VER_08:
3206 	case RTL_VER_09:
3207 	case RTL_VER_14:
3208 		ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
3209 		if (enable)
3210 			ocp_data |= CPCR_RX_VLAN;
3211 		else
3212 			ocp_data &= ~CPCR_RX_VLAN;
3213 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
3214 		break;
3215 
3216 	case RTL_TEST_01:
3217 	case RTL_VER_10:
3218 	case RTL_VER_11:
3219 	case RTL_VER_12:
3220 	case RTL_VER_13:
3221 	case RTL_VER_15:
3222 	default:
3223 		ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RCR1);
3224 		if (enable)
3225 			ocp_data |= OUTER_VLAN | INNER_VLAN;
3226 		else
3227 			ocp_data &= ~(OUTER_VLAN | INNER_VLAN);
3228 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_RCR1, ocp_data);
3229 		break;
3230 	}
3231 }
3232 
3233 static int rtl8152_set_features(struct net_device *dev,
3234 				netdev_features_t features)
3235 {
3236 	netdev_features_t changed = features ^ dev->features;
3237 	struct r8152 *tp = netdev_priv(dev);
3238 	int ret;
3239 
3240 	ret = usb_autopm_get_interface(tp->intf);
3241 	if (ret < 0)
3242 		goto out;
3243 
3244 	mutex_lock(&tp->control);
3245 
3246 	if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
3247 		if (features & NETIF_F_HW_VLAN_CTAG_RX)
3248 			rtl_rx_vlan_en(tp, true);
3249 		else
3250 			rtl_rx_vlan_en(tp, false);
3251 	}
3252 
3253 	mutex_unlock(&tp->control);
3254 
3255 	usb_autopm_put_interface(tp->intf);
3256 
3257 out:
3258 	return ret;
3259 }
3260 
3261 #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
3262 
3263 static u32 __rtl_get_wol(struct r8152 *tp)
3264 {
3265 	u32 ocp_data;
3266 	u32 wolopts = 0;
3267 
3268 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
3269 	if (ocp_data & LINK_ON_WAKE_EN)
3270 		wolopts |= WAKE_PHY;
3271 
3272 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
3273 	if (ocp_data & UWF_EN)
3274 		wolopts |= WAKE_UCAST;
3275 	if (ocp_data & BWF_EN)
3276 		wolopts |= WAKE_BCAST;
3277 	if (ocp_data & MWF_EN)
3278 		wolopts |= WAKE_MCAST;
3279 
3280 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
3281 	if (ocp_data & MAGIC_EN)
3282 		wolopts |= WAKE_MAGIC;
3283 
3284 	return wolopts;
3285 }
3286 
3287 static void __rtl_set_wol(struct r8152 *tp, u32 wolopts)
3288 {
3289 	u32 ocp_data;
3290 
3291 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
3292 
3293 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
3294 	ocp_data &= ~LINK_ON_WAKE_EN;
3295 	if (wolopts & WAKE_PHY)
3296 		ocp_data |= LINK_ON_WAKE_EN;
3297 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
3298 
3299 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
3300 	ocp_data &= ~(UWF_EN | BWF_EN | MWF_EN);
3301 	if (wolopts & WAKE_UCAST)
3302 		ocp_data |= UWF_EN;
3303 	if (wolopts & WAKE_BCAST)
3304 		ocp_data |= BWF_EN;
3305 	if (wolopts & WAKE_MCAST)
3306 		ocp_data |= MWF_EN;
3307 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data);
3308 
3309 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
3310 
3311 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
3312 	ocp_data &= ~MAGIC_EN;
3313 	if (wolopts & WAKE_MAGIC)
3314 		ocp_data |= MAGIC_EN;
3315 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
3316 
3317 	if (wolopts & WAKE_ANY)
3318 		device_set_wakeup_enable(&tp->udev->dev, true);
3319 	else
3320 		device_set_wakeup_enable(&tp->udev->dev, false);
3321 }
3322 
3323 static void r8153_mac_clk_speed_down(struct r8152 *tp, bool enable)
3324 {
3325 	u32 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2);
3326 
3327 	/* MAC clock speed down */
3328 	if (enable)
3329 		ocp_data |= MAC_CLK_SPDWN_EN;
3330 	else
3331 		ocp_data &= ~MAC_CLK_SPDWN_EN;
3332 
3333 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data);
3334 }
3335 
3336 static void r8156_mac_clk_spd(struct r8152 *tp, bool enable)
3337 {
3338 	u32 ocp_data;
3339 
3340 	/* MAC clock speed down */
3341 	if (enable) {
3342 		/* aldps_spdwn_ratio, tp10_spdwn_ratio */
3343 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL,
3344 			       0x0403);
3345 
3346 		ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2);
3347 		ocp_data &= ~EEE_SPDWN_RATIO_MASK;
3348 		ocp_data |= MAC_CLK_SPDWN_EN | 0x03; /* eee_spdwn_ratio */
3349 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data);
3350 	} else {
3351 		ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2);
3352 		ocp_data &= ~MAC_CLK_SPDWN_EN;
3353 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data);
3354 	}
3355 }
3356 
3357 static void r8153_u1u2en(struct r8152 *tp, bool enable)
3358 {
3359 	u8 u1u2[8];
3360 
3361 	if (enable)
3362 		memset(u1u2, 0xff, sizeof(u1u2));
3363 	else
3364 		memset(u1u2, 0x00, sizeof(u1u2));
3365 
3366 	usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2);
3367 }
3368 
3369 static void r8153b_u1u2en(struct r8152 *tp, bool enable)
3370 {
3371 	u32 ocp_data;
3372 
3373 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG);
3374 	if (enable)
3375 		ocp_data |= LPM_U1U2_EN;
3376 	else
3377 		ocp_data &= ~LPM_U1U2_EN;
3378 
3379 	ocp_write_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG, ocp_data);
3380 }
3381 
3382 static void r8153_u2p3en(struct r8152 *tp, bool enable)
3383 {
3384 	u32 ocp_data;
3385 
3386 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
3387 	if (enable)
3388 		ocp_data |= U2P3_ENABLE;
3389 	else
3390 		ocp_data &= ~U2P3_ENABLE;
3391 	ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
3392 }
3393 
3394 static void r8153b_ups_flags(struct r8152 *tp)
3395 {
3396 	u32 ups_flags = 0;
3397 
3398 	if (tp->ups_info.green)
3399 		ups_flags |= UPS_FLAGS_EN_GREEN;
3400 
3401 	if (tp->ups_info.aldps)
3402 		ups_flags |= UPS_FLAGS_EN_ALDPS;
3403 
3404 	if (tp->ups_info.eee)
3405 		ups_flags |= UPS_FLAGS_EN_EEE;
3406 
3407 	if (tp->ups_info.flow_control)
3408 		ups_flags |= UPS_FLAGS_EN_FLOW_CTR;
3409 
3410 	if (tp->ups_info.eee_ckdiv)
3411 		ups_flags |= UPS_FLAGS_EN_EEE_CKDIV;
3412 
3413 	if (tp->ups_info.eee_cmod_lv)
3414 		ups_flags |= UPS_FLAGS_EEE_CMOD_LV_EN;
3415 
3416 	if (tp->ups_info.r_tune)
3417 		ups_flags |= UPS_FLAGS_R_TUNE;
3418 
3419 	if (tp->ups_info._10m_ckdiv)
3420 		ups_flags |= UPS_FLAGS_EN_10M_CKDIV;
3421 
3422 	if (tp->ups_info.eee_plloff_100)
3423 		ups_flags |= UPS_FLAGS_EEE_PLLOFF_100;
3424 
3425 	if (tp->ups_info.eee_plloff_giga)
3426 		ups_flags |= UPS_FLAGS_EEE_PLLOFF_GIGA;
3427 
3428 	if (tp->ups_info._250m_ckdiv)
3429 		ups_flags |= UPS_FLAGS_250M_CKDIV;
3430 
3431 	if (tp->ups_info.ctap_short_off)
3432 		ups_flags |= UPS_FLAGS_CTAP_SHORT_DIS;
3433 
3434 	switch (tp->ups_info.speed_duplex) {
3435 	case NWAY_10M_HALF:
3436 		ups_flags |= ups_flags_speed(1);
3437 		break;
3438 	case NWAY_10M_FULL:
3439 		ups_flags |= ups_flags_speed(2);
3440 		break;
3441 	case NWAY_100M_HALF:
3442 		ups_flags |= ups_flags_speed(3);
3443 		break;
3444 	case NWAY_100M_FULL:
3445 		ups_flags |= ups_flags_speed(4);
3446 		break;
3447 	case NWAY_1000M_FULL:
3448 		ups_flags |= ups_flags_speed(5);
3449 		break;
3450 	case FORCE_10M_HALF:
3451 		ups_flags |= ups_flags_speed(6);
3452 		break;
3453 	case FORCE_10M_FULL:
3454 		ups_flags |= ups_flags_speed(7);
3455 		break;
3456 	case FORCE_100M_HALF:
3457 		ups_flags |= ups_flags_speed(8);
3458 		break;
3459 	case FORCE_100M_FULL:
3460 		ups_flags |= ups_flags_speed(9);
3461 		break;
3462 	default:
3463 		break;
3464 	}
3465 
3466 	ocp_write_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS, ups_flags);
3467 }
3468 
3469 static void r8156_ups_flags(struct r8152 *tp)
3470 {
3471 	u32 ups_flags = 0;
3472 
3473 	if (tp->ups_info.green)
3474 		ups_flags |= UPS_FLAGS_EN_GREEN;
3475 
3476 	if (tp->ups_info.aldps)
3477 		ups_flags |= UPS_FLAGS_EN_ALDPS;
3478 
3479 	if (tp->ups_info.eee)
3480 		ups_flags |= UPS_FLAGS_EN_EEE;
3481 
3482 	if (tp->ups_info.flow_control)
3483 		ups_flags |= UPS_FLAGS_EN_FLOW_CTR;
3484 
3485 	if (tp->ups_info.eee_ckdiv)
3486 		ups_flags |= UPS_FLAGS_EN_EEE_CKDIV;
3487 
3488 	if (tp->ups_info._10m_ckdiv)
3489 		ups_flags |= UPS_FLAGS_EN_10M_CKDIV;
3490 
3491 	if (tp->ups_info.eee_plloff_100)
3492 		ups_flags |= UPS_FLAGS_EEE_PLLOFF_100;
3493 
3494 	if (tp->ups_info.eee_plloff_giga)
3495 		ups_flags |= UPS_FLAGS_EEE_PLLOFF_GIGA;
3496 
3497 	if (tp->ups_info._250m_ckdiv)
3498 		ups_flags |= UPS_FLAGS_250M_CKDIV;
3499 
3500 	switch (tp->ups_info.speed_duplex) {
3501 	case FORCE_10M_HALF:
3502 		ups_flags |= ups_flags_speed(0);
3503 		break;
3504 	case FORCE_10M_FULL:
3505 		ups_flags |= ups_flags_speed(1);
3506 		break;
3507 	case FORCE_100M_HALF:
3508 		ups_flags |= ups_flags_speed(2);
3509 		break;
3510 	case FORCE_100M_FULL:
3511 		ups_flags |= ups_flags_speed(3);
3512 		break;
3513 	case NWAY_10M_HALF:
3514 		ups_flags |= ups_flags_speed(4);
3515 		break;
3516 	case NWAY_10M_FULL:
3517 		ups_flags |= ups_flags_speed(5);
3518 		break;
3519 	case NWAY_100M_HALF:
3520 		ups_flags |= ups_flags_speed(6);
3521 		break;
3522 	case NWAY_100M_FULL:
3523 		ups_flags |= ups_flags_speed(7);
3524 		break;
3525 	case NWAY_1000M_FULL:
3526 		ups_flags |= ups_flags_speed(8);
3527 		break;
3528 	case NWAY_2500M_FULL:
3529 		ups_flags |= ups_flags_speed(9);
3530 		break;
3531 	default:
3532 		break;
3533 	}
3534 
3535 	switch (tp->ups_info.lite_mode) {
3536 	case 1:
3537 		ups_flags |= 0 << 5;
3538 		break;
3539 	case 2:
3540 		ups_flags |= 2 << 5;
3541 		break;
3542 	case 0:
3543 	default:
3544 		ups_flags |= 1 << 5;
3545 		break;
3546 	}
3547 
3548 	ocp_write_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS, ups_flags);
3549 }
3550 
3551 static void rtl_green_en(struct r8152 *tp, bool enable)
3552 {
3553 	u16 data;
3554 
3555 	data = sram_read(tp, SRAM_GREEN_CFG);
3556 	if (enable)
3557 		data |= GREEN_ETH_EN;
3558 	else
3559 		data &= ~GREEN_ETH_EN;
3560 	sram_write(tp, SRAM_GREEN_CFG, data);
3561 
3562 	tp->ups_info.green = enable;
3563 }
3564 
3565 static void r8153b_green_en(struct r8152 *tp, bool enable)
3566 {
3567 	if (enable) {
3568 		sram_write(tp, 0x8045, 0);	/* 10M abiq&ldvbias */
3569 		sram_write(tp, 0x804d, 0x1222);	/* 100M short abiq&ldvbias */
3570 		sram_write(tp, 0x805d, 0x0022);	/* 1000M short abiq&ldvbias */
3571 	} else {
3572 		sram_write(tp, 0x8045, 0x2444);	/* 10M abiq&ldvbias */
3573 		sram_write(tp, 0x804d, 0x2444);	/* 100M short abiq&ldvbias */
3574 		sram_write(tp, 0x805d, 0x2444);	/* 1000M short abiq&ldvbias */
3575 	}
3576 
3577 	rtl_green_en(tp, true);
3578 }
3579 
3580 static u16 r8153_phy_status(struct r8152 *tp, u16 desired)
3581 {
3582 	u16 data;
3583 	int i;
3584 
3585 	for (i = 0; i < 500; i++) {
3586 		data = ocp_reg_read(tp, OCP_PHY_STATUS);
3587 		data &= PHY_STAT_MASK;
3588 		if (desired) {
3589 			if (data == desired)
3590 				break;
3591 		} else if (data == PHY_STAT_LAN_ON || data == PHY_STAT_PWRDN ||
3592 			   data == PHY_STAT_EXT_INIT) {
3593 			break;
3594 		}
3595 
3596 		msleep(20);
3597 		if (test_bit(RTL8152_UNPLUG, &tp->flags))
3598 			break;
3599 	}
3600 
3601 	return data;
3602 }
3603 
3604 static void r8153b_ups_en(struct r8152 *tp, bool enable)
3605 {
3606 	u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_POWER_CUT);
3607 
3608 	if (enable) {
3609 		r8153b_ups_flags(tp);
3610 
3611 		ocp_data |= UPS_EN | USP_PREWAKE | PHASE2_EN;
3612 		ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3613 
3614 		ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2);
3615 		ocp_data |= UPS_FORCE_PWR_DOWN;
3616 		ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data);
3617 	} else {
3618 		ocp_data &= ~(UPS_EN | USP_PREWAKE);
3619 		ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3620 
3621 		ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2);
3622 		ocp_data &= ~UPS_FORCE_PWR_DOWN;
3623 		ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data);
3624 
3625 		if (ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0) & PCUT_STATUS) {
3626 			int i;
3627 
3628 			for (i = 0; i < 500; i++) {
3629 				if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
3630 				    AUTOLOAD_DONE)
3631 					break;
3632 				msleep(20);
3633 			}
3634 
3635 			tp->rtl_ops.hw_phy_cfg(tp);
3636 
3637 			rtl8152_set_speed(tp, tp->autoneg, tp->speed,
3638 					  tp->duplex, tp->advertising);
3639 		}
3640 	}
3641 }
3642 
3643 static void r8153c_ups_en(struct r8152 *tp, bool enable)
3644 {
3645 	u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_POWER_CUT);
3646 
3647 	if (enable) {
3648 		r8153b_ups_flags(tp);
3649 
3650 		ocp_data |= UPS_EN | USP_PREWAKE | PHASE2_EN;
3651 		ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3652 
3653 		ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2);
3654 		ocp_data |= UPS_FORCE_PWR_DOWN;
3655 		ocp_data &= ~BIT(7);
3656 		ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data);
3657 	} else {
3658 		ocp_data &= ~(UPS_EN | USP_PREWAKE);
3659 		ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3660 
3661 		ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2);
3662 		ocp_data &= ~UPS_FORCE_PWR_DOWN;
3663 		ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data);
3664 
3665 		if (ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0) & PCUT_STATUS) {
3666 			int i;
3667 
3668 			for (i = 0; i < 500; i++) {
3669 				if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
3670 				    AUTOLOAD_DONE)
3671 					break;
3672 				msleep(20);
3673 			}
3674 
3675 			tp->rtl_ops.hw_phy_cfg(tp);
3676 
3677 			rtl8152_set_speed(tp, tp->autoneg, tp->speed,
3678 					  tp->duplex, tp->advertising);
3679 		}
3680 
3681 		ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
3682 
3683 		ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
3684 		ocp_data |= BIT(8);
3685 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
3686 
3687 		ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
3688 	}
3689 }
3690 
3691 static void r8156_ups_en(struct r8152 *tp, bool enable)
3692 {
3693 	u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_POWER_CUT);
3694 
3695 	if (enable) {
3696 		r8156_ups_flags(tp);
3697 
3698 		ocp_data |= UPS_EN | USP_PREWAKE | PHASE2_EN;
3699 		ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3700 
3701 		ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2);
3702 		ocp_data |= UPS_FORCE_PWR_DOWN;
3703 		ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data);
3704 
3705 		switch (tp->version) {
3706 		case RTL_VER_13:
3707 		case RTL_VER_15:
3708 			ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPHY_XTAL);
3709 			ocp_data &= ~OOBS_POLLING;
3710 			ocp_write_byte(tp, MCU_TYPE_USB, USB_UPHY_XTAL, ocp_data);
3711 			break;
3712 		default:
3713 			break;
3714 		}
3715 	} else {
3716 		ocp_data &= ~(UPS_EN | USP_PREWAKE);
3717 		ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3718 
3719 		ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2);
3720 		ocp_data &= ~UPS_FORCE_PWR_DOWN;
3721 		ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data);
3722 
3723 		if (ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0) & PCUT_STATUS) {
3724 			tp->rtl_ops.hw_phy_cfg(tp);
3725 
3726 			rtl8152_set_speed(tp, tp->autoneg, tp->speed,
3727 					  tp->duplex, tp->advertising);
3728 		}
3729 	}
3730 }
3731 
3732 static void r8153_power_cut_en(struct r8152 *tp, bool enable)
3733 {
3734 	u32 ocp_data;
3735 
3736 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
3737 	if (enable)
3738 		ocp_data |= PWR_EN | PHASE2_EN;
3739 	else
3740 		ocp_data &= ~(PWR_EN | PHASE2_EN);
3741 	ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3742 
3743 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
3744 	ocp_data &= ~PCUT_STATUS;
3745 	ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
3746 }
3747 
3748 static void r8153b_power_cut_en(struct r8152 *tp, bool enable)
3749 {
3750 	u32 ocp_data;
3751 
3752 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
3753 	if (enable)
3754 		ocp_data |= PWR_EN | PHASE2_EN;
3755 	else
3756 		ocp_data &= ~PWR_EN;
3757 	ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
3758 
3759 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
3760 	ocp_data &= ~PCUT_STATUS;
3761 	ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
3762 }
3763 
3764 static void r8153_queue_wake(struct r8152 *tp, bool enable)
3765 {
3766 	u32 ocp_data;
3767 
3768 	ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG);
3769 	if (enable)
3770 		ocp_data |= UPCOMING_RUNTIME_D3;
3771 	else
3772 		ocp_data &= ~UPCOMING_RUNTIME_D3;
3773 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_INDICATE_FALG, ocp_data);
3774 
3775 	ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG);
3776 	ocp_data &= ~LINK_CHG_EVENT;
3777 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_SUSPEND_FLAG, ocp_data);
3778 
3779 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
3780 	ocp_data &= ~LINK_CHANGE_FLAG;
3781 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
3782 }
3783 
3784 static bool rtl_can_wakeup(struct r8152 *tp)
3785 {
3786 	struct usb_device *udev = tp->udev;
3787 
3788 	return (udev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_WAKEUP);
3789 }
3790 
3791 static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable)
3792 {
3793 	if (enable) {
3794 		u32 ocp_data;
3795 
3796 		__rtl_set_wol(tp, WAKE_ANY);
3797 
3798 		ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
3799 
3800 		ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
3801 		ocp_data |= LINK_OFF_WAKE_EN;
3802 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
3803 
3804 		ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
3805 	} else {
3806 		u32 ocp_data;
3807 
3808 		__rtl_set_wol(tp, tp->saved_wolopts);
3809 
3810 		ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
3811 
3812 		ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
3813 		ocp_data &= ~LINK_OFF_WAKE_EN;
3814 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
3815 
3816 		ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
3817 	}
3818 }
3819 
3820 static void rtl8153_runtime_enable(struct r8152 *tp, bool enable)
3821 {
3822 	if (enable) {
3823 		r8153_u1u2en(tp, false);
3824 		r8153_u2p3en(tp, false);
3825 		rtl_runtime_suspend_enable(tp, true);
3826 	} else {
3827 		rtl_runtime_suspend_enable(tp, false);
3828 
3829 		switch (tp->version) {
3830 		case RTL_VER_03:
3831 		case RTL_VER_04:
3832 			break;
3833 		case RTL_VER_05:
3834 		case RTL_VER_06:
3835 		default:
3836 			r8153_u2p3en(tp, true);
3837 			break;
3838 		}
3839 
3840 		r8153_u1u2en(tp, true);
3841 	}
3842 }
3843 
3844 static void rtl8153b_runtime_enable(struct r8152 *tp, bool enable)
3845 {
3846 	if (enable) {
3847 		r8153_queue_wake(tp, true);
3848 		r8153b_u1u2en(tp, false);
3849 		r8153_u2p3en(tp, false);
3850 		rtl_runtime_suspend_enable(tp, true);
3851 		r8153b_ups_en(tp, true);
3852 	} else {
3853 		r8153b_ups_en(tp, false);
3854 		r8153_queue_wake(tp, false);
3855 		rtl_runtime_suspend_enable(tp, false);
3856 		if (tp->udev->speed >= USB_SPEED_SUPER)
3857 			r8153b_u1u2en(tp, true);
3858 	}
3859 }
3860 
3861 static void rtl8153c_runtime_enable(struct r8152 *tp, bool enable)
3862 {
3863 	if (enable) {
3864 		r8153_queue_wake(tp, true);
3865 		r8153b_u1u2en(tp, false);
3866 		r8153_u2p3en(tp, false);
3867 		rtl_runtime_suspend_enable(tp, true);
3868 		r8153c_ups_en(tp, true);
3869 	} else {
3870 		r8153c_ups_en(tp, false);
3871 		r8153_queue_wake(tp, false);
3872 		rtl_runtime_suspend_enable(tp, false);
3873 		r8153b_u1u2en(tp, true);
3874 	}
3875 }
3876 
3877 static void rtl8156_runtime_enable(struct r8152 *tp, bool enable)
3878 {
3879 	if (enable) {
3880 		r8153_queue_wake(tp, true);
3881 		r8153b_u1u2en(tp, false);
3882 		r8153_u2p3en(tp, false);
3883 		rtl_runtime_suspend_enable(tp, true);
3884 	} else {
3885 		r8153_queue_wake(tp, false);
3886 		rtl_runtime_suspend_enable(tp, false);
3887 		r8153_u2p3en(tp, true);
3888 		if (tp->udev->speed >= USB_SPEED_SUPER)
3889 			r8153b_u1u2en(tp, true);
3890 	}
3891 }
3892 
3893 static void r8153_teredo_off(struct r8152 *tp)
3894 {
3895 	u32 ocp_data;
3896 
3897 	switch (tp->version) {
3898 	case RTL_VER_01:
3899 	case RTL_VER_02:
3900 	case RTL_VER_03:
3901 	case RTL_VER_04:
3902 	case RTL_VER_05:
3903 	case RTL_VER_06:
3904 	case RTL_VER_07:
3905 		ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
3906 		ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK |
3907 			      OOB_TEREDO_EN);
3908 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
3909 		break;
3910 
3911 	case RTL_VER_08:
3912 	case RTL_VER_09:
3913 	case RTL_TEST_01:
3914 	case RTL_VER_10:
3915 	case RTL_VER_11:
3916 	case RTL_VER_12:
3917 	case RTL_VER_13:
3918 	case RTL_VER_14:
3919 	case RTL_VER_15:
3920 	default:
3921 		/* The bit 0 ~ 7 are relative with teredo settings. They are
3922 		 * W1C (write 1 to clear), so set all 1 to disable it.
3923 		 */
3924 		ocp_write_byte(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, 0xff);
3925 		break;
3926 	}
3927 
3928 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE);
3929 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0);
3930 	ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0);
3931 }
3932 
3933 static void rtl_reset_bmu(struct r8152 *tp)
3934 {
3935 	u32 ocp_data;
3936 
3937 	ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_BMU_RESET);
3938 	ocp_data &= ~(BMU_RESET_EP_IN | BMU_RESET_EP_OUT);
3939 	ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
3940 	ocp_data |= BMU_RESET_EP_IN | BMU_RESET_EP_OUT;
3941 	ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
3942 }
3943 
3944 /* Clear the bp to stop the firmware before loading a new one */
3945 static void rtl_clear_bp(struct r8152 *tp, u16 type)
3946 {
3947 	switch (tp->version) {
3948 	case RTL_VER_01:
3949 	case RTL_VER_02:
3950 	case RTL_VER_07:
3951 		break;
3952 	case RTL_VER_03:
3953 	case RTL_VER_04:
3954 	case RTL_VER_05:
3955 	case RTL_VER_06:
3956 		ocp_write_byte(tp, type, PLA_BP_EN, 0);
3957 		break;
3958 	case RTL_VER_14:
3959 		ocp_write_word(tp, type, USB_BP2_EN, 0);
3960 
3961 		ocp_write_word(tp, type, USB_BP_8, 0);
3962 		ocp_write_word(tp, type, USB_BP_9, 0);
3963 		ocp_write_word(tp, type, USB_BP_10, 0);
3964 		ocp_write_word(tp, type, USB_BP_11, 0);
3965 		ocp_write_word(tp, type, USB_BP_12, 0);
3966 		ocp_write_word(tp, type, USB_BP_13, 0);
3967 		ocp_write_word(tp, type, USB_BP_14, 0);
3968 		ocp_write_word(tp, type, USB_BP_15, 0);
3969 		break;
3970 	case RTL_VER_08:
3971 	case RTL_VER_09:
3972 	case RTL_VER_10:
3973 	case RTL_VER_11:
3974 	case RTL_VER_12:
3975 	case RTL_VER_13:
3976 	case RTL_VER_15:
3977 	default:
3978 		if (type == MCU_TYPE_USB) {
3979 			ocp_write_word(tp, MCU_TYPE_USB, USB_BP2_EN, 0);
3980 
3981 			ocp_write_word(tp, MCU_TYPE_USB, USB_BP_8, 0);
3982 			ocp_write_word(tp, MCU_TYPE_USB, USB_BP_9, 0);
3983 			ocp_write_word(tp, MCU_TYPE_USB, USB_BP_10, 0);
3984 			ocp_write_word(tp, MCU_TYPE_USB, USB_BP_11, 0);
3985 			ocp_write_word(tp, MCU_TYPE_USB, USB_BP_12, 0);
3986 			ocp_write_word(tp, MCU_TYPE_USB, USB_BP_13, 0);
3987 			ocp_write_word(tp, MCU_TYPE_USB, USB_BP_14, 0);
3988 			ocp_write_word(tp, MCU_TYPE_USB, USB_BP_15, 0);
3989 		} else {
3990 			ocp_write_byte(tp, MCU_TYPE_PLA, PLA_BP_EN, 0);
3991 		}
3992 		break;
3993 	}
3994 
3995 	ocp_write_word(tp, type, PLA_BP_0, 0);
3996 	ocp_write_word(tp, type, PLA_BP_1, 0);
3997 	ocp_write_word(tp, type, PLA_BP_2, 0);
3998 	ocp_write_word(tp, type, PLA_BP_3, 0);
3999 	ocp_write_word(tp, type, PLA_BP_4, 0);
4000 	ocp_write_word(tp, type, PLA_BP_5, 0);
4001 	ocp_write_word(tp, type, PLA_BP_6, 0);
4002 	ocp_write_word(tp, type, PLA_BP_7, 0);
4003 
4004 	/* wait 3 ms to make sure the firmware is stopped */
4005 	usleep_range(3000, 6000);
4006 	ocp_write_word(tp, type, PLA_BP_BA, 0);
4007 }
4008 
4009 static int rtl_phy_patch_request(struct r8152 *tp, bool request, bool wait)
4010 {
4011 	u16 data, check;
4012 	int i;
4013 
4014 	data = ocp_reg_read(tp, OCP_PHY_PATCH_CMD);
4015 	if (request) {
4016 		data |= PATCH_REQUEST;
4017 		check = 0;
4018 	} else {
4019 		data &= ~PATCH_REQUEST;
4020 		check = PATCH_READY;
4021 	}
4022 	ocp_reg_write(tp, OCP_PHY_PATCH_CMD, data);
4023 
4024 	for (i = 0; wait && i < 5000; i++) {
4025 		u32 ocp_data;
4026 
4027 		usleep_range(1000, 2000);
4028 		ocp_data = ocp_reg_read(tp, OCP_PHY_PATCH_STAT);
4029 		if ((ocp_data & PATCH_READY) ^ check)
4030 			break;
4031 	}
4032 
4033 	if (request && wait &&
4034 	    !(ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)) {
4035 		dev_err(&tp->intf->dev, "PHY patch request fail\n");
4036 		rtl_phy_patch_request(tp, false, false);
4037 		return -ETIME;
4038 	} else {
4039 		return 0;
4040 	}
4041 }
4042 
4043 static void rtl_patch_key_set(struct r8152 *tp, u16 key_addr, u16 patch_key)
4044 {
4045 	if (patch_key && key_addr) {
4046 		sram_write(tp, key_addr, patch_key);
4047 		sram_write(tp, SRAM_PHY_LOCK, PHY_PATCH_LOCK);
4048 	} else if (key_addr) {
4049 		u16 data;
4050 
4051 		sram_write(tp, 0x0000, 0x0000);
4052 
4053 		data = ocp_reg_read(tp, OCP_PHY_LOCK);
4054 		data &= ~PATCH_LOCK;
4055 		ocp_reg_write(tp, OCP_PHY_LOCK, data);
4056 
4057 		sram_write(tp, key_addr, 0x0000);
4058 	} else {
4059 		WARN_ON_ONCE(1);
4060 	}
4061 }
4062 
4063 static int
4064 rtl_pre_ram_code(struct r8152 *tp, u16 key_addr, u16 patch_key, bool wait)
4065 {
4066 	if (rtl_phy_patch_request(tp, true, wait))
4067 		return -ETIME;
4068 
4069 	rtl_patch_key_set(tp, key_addr, patch_key);
4070 
4071 	return 0;
4072 }
4073 
4074 static int rtl_post_ram_code(struct r8152 *tp, u16 key_addr, bool wait)
4075 {
4076 	rtl_patch_key_set(tp, key_addr, 0);
4077 
4078 	rtl_phy_patch_request(tp, false, wait);
4079 
4080 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, tp->ocp_base);
4081 
4082 	return 0;
4083 }
4084 
4085 static bool rtl8152_is_fw_phy_speed_up_ok(struct r8152 *tp, struct fw_phy_speed_up *phy)
4086 {
4087 	u16 fw_offset;
4088 	u32 length;
4089 	bool rc = false;
4090 
4091 	switch (tp->version) {
4092 	case RTL_VER_01:
4093 	case RTL_VER_02:
4094 	case RTL_VER_03:
4095 	case RTL_VER_04:
4096 	case RTL_VER_05:
4097 	case RTL_VER_06:
4098 	case RTL_VER_07:
4099 	case RTL_VER_08:
4100 	case RTL_VER_09:
4101 	case RTL_VER_10:
4102 	case RTL_VER_11:
4103 	case RTL_VER_12:
4104 	case RTL_VER_14:
4105 		goto out;
4106 	case RTL_VER_13:
4107 	case RTL_VER_15:
4108 	default:
4109 		break;
4110 	}
4111 
4112 	fw_offset = __le16_to_cpu(phy->fw_offset);
4113 	length = __le32_to_cpu(phy->blk_hdr.length);
4114 	if (fw_offset < sizeof(*phy) || length <= fw_offset) {
4115 		dev_err(&tp->intf->dev, "invalid fw_offset\n");
4116 		goto out;
4117 	}
4118 
4119 	length -= fw_offset;
4120 	if (length & 3) {
4121 		dev_err(&tp->intf->dev, "invalid block length\n");
4122 		goto out;
4123 	}
4124 
4125 	if (__le16_to_cpu(phy->fw_reg) != 0x9A00) {
4126 		dev_err(&tp->intf->dev, "invalid register to load firmware\n");
4127 		goto out;
4128 	}
4129 
4130 	rc = true;
4131 out:
4132 	return rc;
4133 }
4134 
4135 static bool rtl8152_is_fw_phy_ver_ok(struct r8152 *tp, struct fw_phy_ver *ver)
4136 {
4137 	bool rc = false;
4138 
4139 	switch (tp->version) {
4140 	case RTL_VER_10:
4141 	case RTL_VER_11:
4142 	case RTL_VER_12:
4143 	case RTL_VER_13:
4144 	case RTL_VER_15:
4145 		break;
4146 	default:
4147 		goto out;
4148 	}
4149 
4150 	if (__le32_to_cpu(ver->blk_hdr.length) != sizeof(*ver)) {
4151 		dev_err(&tp->intf->dev, "invalid block length\n");
4152 		goto out;
4153 	}
4154 
4155 	if (__le16_to_cpu(ver->ver.addr) != SRAM_GPHY_FW_VER) {
4156 		dev_err(&tp->intf->dev, "invalid phy ver addr\n");
4157 		goto out;
4158 	}
4159 
4160 	rc = true;
4161 out:
4162 	return rc;
4163 }
4164 
4165 static bool rtl8152_is_fw_phy_fixup_ok(struct r8152 *tp, struct fw_phy_fixup *fix)
4166 {
4167 	bool rc = false;
4168 
4169 	switch (tp->version) {
4170 	case RTL_VER_10:
4171 	case RTL_VER_11:
4172 	case RTL_VER_12:
4173 	case RTL_VER_13:
4174 	case RTL_VER_15:
4175 		break;
4176 	default:
4177 		goto out;
4178 	}
4179 
4180 	if (__le32_to_cpu(fix->blk_hdr.length) != sizeof(*fix)) {
4181 		dev_err(&tp->intf->dev, "invalid block length\n");
4182 		goto out;
4183 	}
4184 
4185 	if (__le16_to_cpu(fix->setting.addr) != OCP_PHY_PATCH_CMD ||
4186 	    __le16_to_cpu(fix->setting.data) != BIT(7)) {
4187 		dev_err(&tp->intf->dev, "invalid phy fixup\n");
4188 		goto out;
4189 	}
4190 
4191 	rc = true;
4192 out:
4193 	return rc;
4194 }
4195 
4196 static bool rtl8152_is_fw_phy_union_ok(struct r8152 *tp, struct fw_phy_union *phy)
4197 {
4198 	u16 fw_offset;
4199 	u32 length;
4200 	bool rc = false;
4201 
4202 	switch (tp->version) {
4203 	case RTL_VER_10:
4204 	case RTL_VER_11:
4205 	case RTL_VER_12:
4206 	case RTL_VER_13:
4207 	case RTL_VER_15:
4208 		break;
4209 	default:
4210 		goto out;
4211 	}
4212 
4213 	fw_offset = __le16_to_cpu(phy->fw_offset);
4214 	length = __le32_to_cpu(phy->blk_hdr.length);
4215 	if (fw_offset < sizeof(*phy) || length <= fw_offset) {
4216 		dev_err(&tp->intf->dev, "invalid fw_offset\n");
4217 		goto out;
4218 	}
4219 
4220 	length -= fw_offset;
4221 	if (length & 1) {
4222 		dev_err(&tp->intf->dev, "invalid block length\n");
4223 		goto out;
4224 	}
4225 
4226 	if (phy->pre_num > 2) {
4227 		dev_err(&tp->intf->dev, "invalid pre_num %d\n", phy->pre_num);
4228 		goto out;
4229 	}
4230 
4231 	if (phy->bp_num > 8) {
4232 		dev_err(&tp->intf->dev, "invalid bp_num %d\n", phy->bp_num);
4233 		goto out;
4234 	}
4235 
4236 	rc = true;
4237 out:
4238 	return rc;
4239 }
4240 
4241 static bool rtl8152_is_fw_phy_nc_ok(struct r8152 *tp, struct fw_phy_nc *phy)
4242 {
4243 	u32 length;
4244 	u16 fw_offset, fw_reg, ba_reg, patch_en_addr, mode_reg, bp_start;
4245 	bool rc = false;
4246 
4247 	switch (tp->version) {
4248 	case RTL_VER_04:
4249 	case RTL_VER_05:
4250 	case RTL_VER_06:
4251 		fw_reg = 0xa014;
4252 		ba_reg = 0xa012;
4253 		patch_en_addr = 0xa01a;
4254 		mode_reg = 0xb820;
4255 		bp_start = 0xa000;
4256 		break;
4257 	default:
4258 		goto out;
4259 	}
4260 
4261 	fw_offset = __le16_to_cpu(phy->fw_offset);
4262 	if (fw_offset < sizeof(*phy)) {
4263 		dev_err(&tp->intf->dev, "fw_offset too small\n");
4264 		goto out;
4265 	}
4266 
4267 	length = __le32_to_cpu(phy->blk_hdr.length);
4268 	if (length < fw_offset) {
4269 		dev_err(&tp->intf->dev, "invalid fw_offset\n");
4270 		goto out;
4271 	}
4272 
4273 	length -= __le16_to_cpu(phy->fw_offset);
4274 	if (!length || (length & 1)) {
4275 		dev_err(&tp->intf->dev, "invalid block length\n");
4276 		goto out;
4277 	}
4278 
4279 	if (__le16_to_cpu(phy->fw_reg) != fw_reg) {
4280 		dev_err(&tp->intf->dev, "invalid register to load firmware\n");
4281 		goto out;
4282 	}
4283 
4284 	if (__le16_to_cpu(phy->ba_reg) != ba_reg) {
4285 		dev_err(&tp->intf->dev, "invalid base address register\n");
4286 		goto out;
4287 	}
4288 
4289 	if (__le16_to_cpu(phy->patch_en_addr) != patch_en_addr) {
4290 		dev_err(&tp->intf->dev,
4291 			"invalid patch mode enabled register\n");
4292 		goto out;
4293 	}
4294 
4295 	if (__le16_to_cpu(phy->mode_reg) != mode_reg) {
4296 		dev_err(&tp->intf->dev,
4297 			"invalid register to switch the mode\n");
4298 		goto out;
4299 	}
4300 
4301 	if (__le16_to_cpu(phy->bp_start) != bp_start) {
4302 		dev_err(&tp->intf->dev,
4303 			"invalid start register of break point\n");
4304 		goto out;
4305 	}
4306 
4307 	if (__le16_to_cpu(phy->bp_num) > 4) {
4308 		dev_err(&tp->intf->dev, "invalid break point number\n");
4309 		goto out;
4310 	}
4311 
4312 	rc = true;
4313 out:
4314 	return rc;
4315 }
4316 
4317 static bool rtl8152_is_fw_mac_ok(struct r8152 *tp, struct fw_mac *mac)
4318 {
4319 	u16 fw_reg, bp_ba_addr, bp_en_addr, bp_start, fw_offset;
4320 	bool rc = false;
4321 	u32 length, type;
4322 	int i, max_bp;
4323 
4324 	type = __le32_to_cpu(mac->blk_hdr.type);
4325 	if (type == RTL_FW_PLA) {
4326 		switch (tp->version) {
4327 		case RTL_VER_01:
4328 		case RTL_VER_02:
4329 		case RTL_VER_07:
4330 			fw_reg = 0xf800;
4331 			bp_ba_addr = PLA_BP_BA;
4332 			bp_en_addr = 0;
4333 			bp_start = PLA_BP_0;
4334 			max_bp = 8;
4335 			break;
4336 		case RTL_VER_03:
4337 		case RTL_VER_04:
4338 		case RTL_VER_05:
4339 		case RTL_VER_06:
4340 		case RTL_VER_08:
4341 		case RTL_VER_09:
4342 		case RTL_VER_11:
4343 		case RTL_VER_12:
4344 		case RTL_VER_13:
4345 		case RTL_VER_15:
4346 			fw_reg = 0xf800;
4347 			bp_ba_addr = PLA_BP_BA;
4348 			bp_en_addr = PLA_BP_EN;
4349 			bp_start = PLA_BP_0;
4350 			max_bp = 8;
4351 			break;
4352 		case RTL_VER_14:
4353 			fw_reg = 0xf800;
4354 			bp_ba_addr = PLA_BP_BA;
4355 			bp_en_addr = USB_BP2_EN;
4356 			bp_start = PLA_BP_0;
4357 			max_bp = 16;
4358 			break;
4359 		default:
4360 			goto out;
4361 		}
4362 	} else if (type == RTL_FW_USB) {
4363 		switch (tp->version) {
4364 		case RTL_VER_03:
4365 		case RTL_VER_04:
4366 		case RTL_VER_05:
4367 		case RTL_VER_06:
4368 			fw_reg = 0xf800;
4369 			bp_ba_addr = USB_BP_BA;
4370 			bp_en_addr = USB_BP_EN;
4371 			bp_start = USB_BP_0;
4372 			max_bp = 8;
4373 			break;
4374 		case RTL_VER_08:
4375 		case RTL_VER_09:
4376 		case RTL_VER_11:
4377 		case RTL_VER_12:
4378 		case RTL_VER_13:
4379 		case RTL_VER_14:
4380 		case RTL_VER_15:
4381 			fw_reg = 0xe600;
4382 			bp_ba_addr = USB_BP_BA;
4383 			bp_en_addr = USB_BP2_EN;
4384 			bp_start = USB_BP_0;
4385 			max_bp = 16;
4386 			break;
4387 		case RTL_VER_01:
4388 		case RTL_VER_02:
4389 		case RTL_VER_07:
4390 		default:
4391 			goto out;
4392 		}
4393 	} else {
4394 		goto out;
4395 	}
4396 
4397 	fw_offset = __le16_to_cpu(mac->fw_offset);
4398 	if (fw_offset < sizeof(*mac)) {
4399 		dev_err(&tp->intf->dev, "fw_offset too small\n");
4400 		goto out;
4401 	}
4402 
4403 	length = __le32_to_cpu(mac->blk_hdr.length);
4404 	if (length < fw_offset) {
4405 		dev_err(&tp->intf->dev, "invalid fw_offset\n");
4406 		goto out;
4407 	}
4408 
4409 	length -= fw_offset;
4410 	if (length < 4 || (length & 3)) {
4411 		dev_err(&tp->intf->dev, "invalid block length\n");
4412 		goto out;
4413 	}
4414 
4415 	if (__le16_to_cpu(mac->fw_reg) != fw_reg) {
4416 		dev_err(&tp->intf->dev, "invalid register to load firmware\n");
4417 		goto out;
4418 	}
4419 
4420 	if (__le16_to_cpu(mac->bp_ba_addr) != bp_ba_addr) {
4421 		dev_err(&tp->intf->dev, "invalid base address register\n");
4422 		goto out;
4423 	}
4424 
4425 	if (__le16_to_cpu(mac->bp_en_addr) != bp_en_addr) {
4426 		dev_err(&tp->intf->dev, "invalid enabled mask register\n");
4427 		goto out;
4428 	}
4429 
4430 	if (__le16_to_cpu(mac->bp_start) != bp_start) {
4431 		dev_err(&tp->intf->dev,
4432 			"invalid start register of break point\n");
4433 		goto out;
4434 	}
4435 
4436 	if (__le16_to_cpu(mac->bp_num) > max_bp) {
4437 		dev_err(&tp->intf->dev, "invalid break point number\n");
4438 		goto out;
4439 	}
4440 
4441 	for (i = __le16_to_cpu(mac->bp_num); i < max_bp; i++) {
4442 		if (mac->bp[i]) {
4443 			dev_err(&tp->intf->dev, "unused bp%u is not zero\n", i);
4444 			goto out;
4445 		}
4446 	}
4447 
4448 	rc = true;
4449 out:
4450 	return rc;
4451 }
4452 
4453 /* Verify the checksum for the firmware file. It is calculated from the version
4454  * field to the end of the file. Compare the result with the checksum field to
4455  * make sure the file is correct.
4456  */
4457 static long rtl8152_fw_verify_checksum(struct r8152 *tp,
4458 				       struct fw_header *fw_hdr, size_t size)
4459 {
4460 	unsigned char checksum[sizeof(fw_hdr->checksum)];
4461 	struct crypto_shash *alg;
4462 	struct shash_desc *sdesc;
4463 	size_t len;
4464 	long rc;
4465 
4466 	alg = crypto_alloc_shash("sha256", 0, 0);
4467 	if (IS_ERR(alg)) {
4468 		rc = PTR_ERR(alg);
4469 		goto out;
4470 	}
4471 
4472 	if (crypto_shash_digestsize(alg) != sizeof(fw_hdr->checksum)) {
4473 		rc = -EFAULT;
4474 		dev_err(&tp->intf->dev, "digestsize incorrect (%u)\n",
4475 			crypto_shash_digestsize(alg));
4476 		goto free_shash;
4477 	}
4478 
4479 	len = sizeof(*sdesc) + crypto_shash_descsize(alg);
4480 	sdesc = kmalloc(len, GFP_KERNEL);
4481 	if (!sdesc) {
4482 		rc = -ENOMEM;
4483 		goto free_shash;
4484 	}
4485 	sdesc->tfm = alg;
4486 
4487 	len = size - sizeof(fw_hdr->checksum);
4488 	rc = crypto_shash_digest(sdesc, fw_hdr->version, len, checksum);
4489 	kfree(sdesc);
4490 	if (rc)
4491 		goto free_shash;
4492 
4493 	if (memcmp(fw_hdr->checksum, checksum, sizeof(fw_hdr->checksum))) {
4494 		dev_err(&tp->intf->dev, "checksum fail\n");
4495 		rc = -EFAULT;
4496 	}
4497 
4498 free_shash:
4499 	crypto_free_shash(alg);
4500 out:
4501 	return rc;
4502 }
4503 
4504 static long rtl8152_check_firmware(struct r8152 *tp, struct rtl_fw *rtl_fw)
4505 {
4506 	const struct firmware *fw = rtl_fw->fw;
4507 	struct fw_header *fw_hdr = (struct fw_header *)fw->data;
4508 	unsigned long fw_flags = 0;
4509 	long ret = -EFAULT;
4510 	int i;
4511 
4512 	if (fw->size < sizeof(*fw_hdr)) {
4513 		dev_err(&tp->intf->dev, "file too small\n");
4514 		goto fail;
4515 	}
4516 
4517 	ret = rtl8152_fw_verify_checksum(tp, fw_hdr, fw->size);
4518 	if (ret)
4519 		goto fail;
4520 
4521 	ret = -EFAULT;
4522 
4523 	for (i = sizeof(*fw_hdr); i < fw->size;) {
4524 		struct fw_block *block = (struct fw_block *)&fw->data[i];
4525 		u32 type;
4526 
4527 		if ((i + sizeof(*block)) > fw->size)
4528 			goto fail;
4529 
4530 		type = __le32_to_cpu(block->type);
4531 		switch (type) {
4532 		case RTL_FW_END:
4533 			if (__le32_to_cpu(block->length) != sizeof(*block))
4534 				goto fail;
4535 			goto fw_end;
4536 		case RTL_FW_PLA:
4537 			if (test_bit(FW_FLAGS_PLA, &fw_flags)) {
4538 				dev_err(&tp->intf->dev,
4539 					"multiple PLA firmware encountered");
4540 				goto fail;
4541 			}
4542 
4543 			if (!rtl8152_is_fw_mac_ok(tp, (struct fw_mac *)block)) {
4544 				dev_err(&tp->intf->dev,
4545 					"check PLA firmware failed\n");
4546 				goto fail;
4547 			}
4548 			__set_bit(FW_FLAGS_PLA, &fw_flags);
4549 			break;
4550 		case RTL_FW_USB:
4551 			if (test_bit(FW_FLAGS_USB, &fw_flags)) {
4552 				dev_err(&tp->intf->dev,
4553 					"multiple USB firmware encountered");
4554 				goto fail;
4555 			}
4556 
4557 			if (!rtl8152_is_fw_mac_ok(tp, (struct fw_mac *)block)) {
4558 				dev_err(&tp->intf->dev,
4559 					"check USB firmware failed\n");
4560 				goto fail;
4561 			}
4562 			__set_bit(FW_FLAGS_USB, &fw_flags);
4563 			break;
4564 		case RTL_FW_PHY_START:
4565 			if (test_bit(FW_FLAGS_START, &fw_flags) ||
4566 			    test_bit(FW_FLAGS_NC, &fw_flags) ||
4567 			    test_bit(FW_FLAGS_NC1, &fw_flags) ||
4568 			    test_bit(FW_FLAGS_NC2, &fw_flags) ||
4569 			    test_bit(FW_FLAGS_UC2, &fw_flags) ||
4570 			    test_bit(FW_FLAGS_UC, &fw_flags) ||
4571 			    test_bit(FW_FLAGS_STOP, &fw_flags)) {
4572 				dev_err(&tp->intf->dev,
4573 					"check PHY_START fail\n");
4574 				goto fail;
4575 			}
4576 
4577 			if (__le32_to_cpu(block->length) != sizeof(struct fw_phy_patch_key)) {
4578 				dev_err(&tp->intf->dev,
4579 					"Invalid length for PHY_START\n");
4580 				goto fail;
4581 			}
4582 			__set_bit(FW_FLAGS_START, &fw_flags);
4583 			break;
4584 		case RTL_FW_PHY_STOP:
4585 			if (test_bit(FW_FLAGS_STOP, &fw_flags) ||
4586 			    !test_bit(FW_FLAGS_START, &fw_flags)) {
4587 				dev_err(&tp->intf->dev,
4588 					"Check PHY_STOP fail\n");
4589 				goto fail;
4590 			}
4591 
4592 			if (__le32_to_cpu(block->length) != sizeof(*block)) {
4593 				dev_err(&tp->intf->dev,
4594 					"Invalid length for PHY_STOP\n");
4595 				goto fail;
4596 			}
4597 			__set_bit(FW_FLAGS_STOP, &fw_flags);
4598 			break;
4599 		case RTL_FW_PHY_NC:
4600 			if (!test_bit(FW_FLAGS_START, &fw_flags) ||
4601 			    test_bit(FW_FLAGS_STOP, &fw_flags)) {
4602 				dev_err(&tp->intf->dev,
4603 					"check PHY_NC fail\n");
4604 				goto fail;
4605 			}
4606 
4607 			if (test_bit(FW_FLAGS_NC, &fw_flags)) {
4608 				dev_err(&tp->intf->dev,
4609 					"multiple PHY NC encountered\n");
4610 				goto fail;
4611 			}
4612 
4613 			if (!rtl8152_is_fw_phy_nc_ok(tp, (struct fw_phy_nc *)block)) {
4614 				dev_err(&tp->intf->dev,
4615 					"check PHY NC firmware failed\n");
4616 				goto fail;
4617 			}
4618 			__set_bit(FW_FLAGS_NC, &fw_flags);
4619 			break;
4620 		case RTL_FW_PHY_UNION_NC:
4621 			if (!test_bit(FW_FLAGS_START, &fw_flags) ||
4622 			    test_bit(FW_FLAGS_NC1, &fw_flags) ||
4623 			    test_bit(FW_FLAGS_NC2, &fw_flags) ||
4624 			    test_bit(FW_FLAGS_UC2, &fw_flags) ||
4625 			    test_bit(FW_FLAGS_UC, &fw_flags) ||
4626 			    test_bit(FW_FLAGS_STOP, &fw_flags)) {
4627 				dev_err(&tp->intf->dev, "PHY_UNION_NC out of order\n");
4628 				goto fail;
4629 			}
4630 
4631 			if (test_bit(FW_FLAGS_NC, &fw_flags)) {
4632 				dev_err(&tp->intf->dev, "multiple PHY_UNION_NC encountered\n");
4633 				goto fail;
4634 			}
4635 
4636 			if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) {
4637 				dev_err(&tp->intf->dev, "check PHY_UNION_NC failed\n");
4638 				goto fail;
4639 			}
4640 			__set_bit(FW_FLAGS_NC, &fw_flags);
4641 			break;
4642 		case RTL_FW_PHY_UNION_NC1:
4643 			if (!test_bit(FW_FLAGS_START, &fw_flags) ||
4644 			    test_bit(FW_FLAGS_NC2, &fw_flags) ||
4645 			    test_bit(FW_FLAGS_UC2, &fw_flags) ||
4646 			    test_bit(FW_FLAGS_UC, &fw_flags) ||
4647 			    test_bit(FW_FLAGS_STOP, &fw_flags)) {
4648 				dev_err(&tp->intf->dev, "PHY_UNION_NC1 out of order\n");
4649 				goto fail;
4650 			}
4651 
4652 			if (test_bit(FW_FLAGS_NC1, &fw_flags)) {
4653 				dev_err(&tp->intf->dev, "multiple PHY NC1 encountered\n");
4654 				goto fail;
4655 			}
4656 
4657 			if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) {
4658 				dev_err(&tp->intf->dev, "check PHY_UNION_NC1 failed\n");
4659 				goto fail;
4660 			}
4661 			__set_bit(FW_FLAGS_NC1, &fw_flags);
4662 			break;
4663 		case RTL_FW_PHY_UNION_NC2:
4664 			if (!test_bit(FW_FLAGS_START, &fw_flags) ||
4665 			    test_bit(FW_FLAGS_UC2, &fw_flags) ||
4666 			    test_bit(FW_FLAGS_UC, &fw_flags) ||
4667 			    test_bit(FW_FLAGS_STOP, &fw_flags)) {
4668 				dev_err(&tp->intf->dev, "PHY_UNION_NC2 out of order\n");
4669 				goto fail;
4670 			}
4671 
4672 			if (test_bit(FW_FLAGS_NC2, &fw_flags)) {
4673 				dev_err(&tp->intf->dev, "multiple PHY NC2 encountered\n");
4674 				goto fail;
4675 			}
4676 
4677 			if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) {
4678 				dev_err(&tp->intf->dev, "check PHY_UNION_NC2 failed\n");
4679 				goto fail;
4680 			}
4681 			__set_bit(FW_FLAGS_NC2, &fw_flags);
4682 			break;
4683 		case RTL_FW_PHY_UNION_UC2:
4684 			if (!test_bit(FW_FLAGS_START, &fw_flags) ||
4685 			    test_bit(FW_FLAGS_UC, &fw_flags) ||
4686 			    test_bit(FW_FLAGS_STOP, &fw_flags)) {
4687 				dev_err(&tp->intf->dev, "PHY_UNION_UC2 out of order\n");
4688 				goto fail;
4689 			}
4690 
4691 			if (test_bit(FW_FLAGS_UC2, &fw_flags)) {
4692 				dev_err(&tp->intf->dev, "multiple PHY UC2 encountered\n");
4693 				goto fail;
4694 			}
4695 
4696 			if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) {
4697 				dev_err(&tp->intf->dev, "check PHY_UNION_UC2 failed\n");
4698 				goto fail;
4699 			}
4700 			__set_bit(FW_FLAGS_UC2, &fw_flags);
4701 			break;
4702 		case RTL_FW_PHY_UNION_UC:
4703 			if (!test_bit(FW_FLAGS_START, &fw_flags) ||
4704 			    test_bit(FW_FLAGS_STOP, &fw_flags)) {
4705 				dev_err(&tp->intf->dev, "PHY_UNION_UC out of order\n");
4706 				goto fail;
4707 			}
4708 
4709 			if (test_bit(FW_FLAGS_UC, &fw_flags)) {
4710 				dev_err(&tp->intf->dev, "multiple PHY UC encountered\n");
4711 				goto fail;
4712 			}
4713 
4714 			if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) {
4715 				dev_err(&tp->intf->dev, "check PHY_UNION_UC failed\n");
4716 				goto fail;
4717 			}
4718 			__set_bit(FW_FLAGS_UC, &fw_flags);
4719 			break;
4720 		case RTL_FW_PHY_UNION_MISC:
4721 			if (!rtl8152_is_fw_phy_union_ok(tp, (struct fw_phy_union *)block)) {
4722 				dev_err(&tp->intf->dev, "check RTL_FW_PHY_UNION_MISC failed\n");
4723 				goto fail;
4724 			}
4725 			break;
4726 		case RTL_FW_PHY_FIXUP:
4727 			if (!rtl8152_is_fw_phy_fixup_ok(tp, (struct fw_phy_fixup *)block)) {
4728 				dev_err(&tp->intf->dev, "check PHY fixup failed\n");
4729 				goto fail;
4730 			}
4731 			break;
4732 		case RTL_FW_PHY_SPEED_UP:
4733 			if (test_bit(FW_FLAGS_SPEED_UP, &fw_flags)) {
4734 				dev_err(&tp->intf->dev, "multiple PHY firmware encountered");
4735 				goto fail;
4736 			}
4737 
4738 			if (!rtl8152_is_fw_phy_speed_up_ok(tp, (struct fw_phy_speed_up *)block)) {
4739 				dev_err(&tp->intf->dev, "check PHY speed up failed\n");
4740 				goto fail;
4741 			}
4742 			__set_bit(FW_FLAGS_SPEED_UP, &fw_flags);
4743 			break;
4744 		case RTL_FW_PHY_VER:
4745 			if (test_bit(FW_FLAGS_START, &fw_flags) ||
4746 			    test_bit(FW_FLAGS_NC, &fw_flags) ||
4747 			    test_bit(FW_FLAGS_NC1, &fw_flags) ||
4748 			    test_bit(FW_FLAGS_NC2, &fw_flags) ||
4749 			    test_bit(FW_FLAGS_UC2, &fw_flags) ||
4750 			    test_bit(FW_FLAGS_UC, &fw_flags) ||
4751 			    test_bit(FW_FLAGS_STOP, &fw_flags)) {
4752 				dev_err(&tp->intf->dev, "Invalid order to set PHY version\n");
4753 				goto fail;
4754 			}
4755 
4756 			if (test_bit(FW_FLAGS_VER, &fw_flags)) {
4757 				dev_err(&tp->intf->dev, "multiple PHY version encountered");
4758 				goto fail;
4759 			}
4760 
4761 			if (!rtl8152_is_fw_phy_ver_ok(tp, (struct fw_phy_ver *)block)) {
4762 				dev_err(&tp->intf->dev, "check PHY version failed\n");
4763 				goto fail;
4764 			}
4765 			__set_bit(FW_FLAGS_VER, &fw_flags);
4766 			break;
4767 		default:
4768 			dev_warn(&tp->intf->dev, "Unknown type %u is found\n",
4769 				 type);
4770 			break;
4771 		}
4772 
4773 		/* next block */
4774 		i += ALIGN(__le32_to_cpu(block->length), 8);
4775 	}
4776 
4777 fw_end:
4778 	if (test_bit(FW_FLAGS_START, &fw_flags) && !test_bit(FW_FLAGS_STOP, &fw_flags)) {
4779 		dev_err(&tp->intf->dev, "without PHY_STOP\n");
4780 		goto fail;
4781 	}
4782 
4783 	return 0;
4784 fail:
4785 	return ret;
4786 }
4787 
4788 static void rtl_ram_code_speed_up(struct r8152 *tp, struct fw_phy_speed_up *phy, bool wait)
4789 {
4790 	u32 len;
4791 	u8 *data;
4792 
4793 	if (sram_read(tp, SRAM_GPHY_FW_VER) >= __le16_to_cpu(phy->version)) {
4794 		dev_dbg(&tp->intf->dev, "PHY firmware has been the newest\n");
4795 		return;
4796 	}
4797 
4798 	len = __le32_to_cpu(phy->blk_hdr.length);
4799 	len -= __le16_to_cpu(phy->fw_offset);
4800 	data = (u8 *)phy + __le16_to_cpu(phy->fw_offset);
4801 
4802 	if (rtl_phy_patch_request(tp, true, wait))
4803 		return;
4804 
4805 	while (len) {
4806 		u32 ocp_data, size;
4807 		int i;
4808 
4809 		if (len < 2048)
4810 			size = len;
4811 		else
4812 			size = 2048;
4813 
4814 		ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_GPHY_CTRL);
4815 		ocp_data |= GPHY_PATCH_DONE | BACKUP_RESTRORE;
4816 		ocp_write_word(tp, MCU_TYPE_USB, USB_GPHY_CTRL, ocp_data);
4817 
4818 		generic_ocp_write(tp, __le16_to_cpu(phy->fw_reg), 0xff, size, data, MCU_TYPE_USB);
4819 
4820 		data += size;
4821 		len -= size;
4822 
4823 		ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_POL_GPIO_CTRL);
4824 		ocp_data |= POL_GPHY_PATCH;
4825 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_POL_GPIO_CTRL, ocp_data);
4826 
4827 		for (i = 0; i < 1000; i++) {
4828 			if (!(ocp_read_word(tp, MCU_TYPE_PLA, PLA_POL_GPIO_CTRL) & POL_GPHY_PATCH))
4829 				break;
4830 		}
4831 
4832 		if (i == 1000) {
4833 			dev_err(&tp->intf->dev, "ram code speedup mode timeout\n");
4834 			break;
4835 		}
4836 	}
4837 
4838 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, tp->ocp_base);
4839 	rtl_phy_patch_request(tp, false, wait);
4840 
4841 	if (sram_read(tp, SRAM_GPHY_FW_VER) == __le16_to_cpu(phy->version))
4842 		dev_dbg(&tp->intf->dev, "successfully applied %s\n", phy->info);
4843 	else
4844 		dev_err(&tp->intf->dev, "ram code speedup mode fail\n");
4845 }
4846 
4847 static int rtl8152_fw_phy_ver(struct r8152 *tp, struct fw_phy_ver *phy_ver)
4848 {
4849 	u16 ver_addr, ver;
4850 
4851 	ver_addr = __le16_to_cpu(phy_ver->ver.addr);
4852 	ver = __le16_to_cpu(phy_ver->ver.data);
4853 
4854 	if (sram_read(tp, ver_addr) >= ver) {
4855 		dev_dbg(&tp->intf->dev, "PHY firmware has been the newest\n");
4856 		return 0;
4857 	}
4858 
4859 	sram_write(tp, ver_addr, ver);
4860 
4861 	dev_dbg(&tp->intf->dev, "PHY firmware version %x\n", ver);
4862 
4863 	return ver;
4864 }
4865 
4866 static void rtl8152_fw_phy_fixup(struct r8152 *tp, struct fw_phy_fixup *fix)
4867 {
4868 	u16 addr, data;
4869 
4870 	addr = __le16_to_cpu(fix->setting.addr);
4871 	data = ocp_reg_read(tp, addr);
4872 
4873 	switch (__le16_to_cpu(fix->bit_cmd)) {
4874 	case FW_FIXUP_AND:
4875 		data &= __le16_to_cpu(fix->setting.data);
4876 		break;
4877 	case FW_FIXUP_OR:
4878 		data |= __le16_to_cpu(fix->setting.data);
4879 		break;
4880 	case FW_FIXUP_NOT:
4881 		data &= ~__le16_to_cpu(fix->setting.data);
4882 		break;
4883 	case FW_FIXUP_XOR:
4884 		data ^= __le16_to_cpu(fix->setting.data);
4885 		break;
4886 	default:
4887 		return;
4888 	}
4889 
4890 	ocp_reg_write(tp, addr, data);
4891 
4892 	dev_dbg(&tp->intf->dev, "applied ocp %x %x\n", addr, data);
4893 }
4894 
4895 static void rtl8152_fw_phy_union_apply(struct r8152 *tp, struct fw_phy_union *phy)
4896 {
4897 	__le16 *data;
4898 	u32 length;
4899 	int i, num;
4900 
4901 	num = phy->pre_num;
4902 	for (i = 0; i < num; i++)
4903 		sram_write(tp, __le16_to_cpu(phy->pre_set[i].addr),
4904 			   __le16_to_cpu(phy->pre_set[i].data));
4905 
4906 	length = __le32_to_cpu(phy->blk_hdr.length);
4907 	length -= __le16_to_cpu(phy->fw_offset);
4908 	num = length / 2;
4909 	data = (__le16 *)((u8 *)phy + __le16_to_cpu(phy->fw_offset));
4910 
4911 	ocp_reg_write(tp, OCP_SRAM_ADDR, __le16_to_cpu(phy->fw_reg));
4912 	for (i = 0; i < num; i++)
4913 		ocp_reg_write(tp, OCP_SRAM_DATA, __le16_to_cpu(data[i]));
4914 
4915 	num = phy->bp_num;
4916 	for (i = 0; i < num; i++)
4917 		sram_write(tp, __le16_to_cpu(phy->bp[i].addr), __le16_to_cpu(phy->bp[i].data));
4918 
4919 	if (phy->bp_num && phy->bp_en.addr)
4920 		sram_write(tp, __le16_to_cpu(phy->bp_en.addr), __le16_to_cpu(phy->bp_en.data));
4921 
4922 	dev_dbg(&tp->intf->dev, "successfully applied %s\n", phy->info);
4923 }
4924 
4925 static void rtl8152_fw_phy_nc_apply(struct r8152 *tp, struct fw_phy_nc *phy)
4926 {
4927 	u16 mode_reg, bp_index;
4928 	u32 length, i, num;
4929 	__le16 *data;
4930 
4931 	mode_reg = __le16_to_cpu(phy->mode_reg);
4932 	sram_write(tp, mode_reg, __le16_to_cpu(phy->mode_pre));
4933 	sram_write(tp, __le16_to_cpu(phy->ba_reg),
4934 		   __le16_to_cpu(phy->ba_data));
4935 
4936 	length = __le32_to_cpu(phy->blk_hdr.length);
4937 	length -= __le16_to_cpu(phy->fw_offset);
4938 	num = length / 2;
4939 	data = (__le16 *)((u8 *)phy + __le16_to_cpu(phy->fw_offset));
4940 
4941 	ocp_reg_write(tp, OCP_SRAM_ADDR, __le16_to_cpu(phy->fw_reg));
4942 	for (i = 0; i < num; i++)
4943 		ocp_reg_write(tp, OCP_SRAM_DATA, __le16_to_cpu(data[i]));
4944 
4945 	sram_write(tp, __le16_to_cpu(phy->patch_en_addr),
4946 		   __le16_to_cpu(phy->patch_en_value));
4947 
4948 	bp_index = __le16_to_cpu(phy->bp_start);
4949 	num = __le16_to_cpu(phy->bp_num);
4950 	for (i = 0; i < num; i++) {
4951 		sram_write(tp, bp_index, __le16_to_cpu(phy->bp[i]));
4952 		bp_index += 2;
4953 	}
4954 
4955 	sram_write(tp, mode_reg, __le16_to_cpu(phy->mode_post));
4956 
4957 	dev_dbg(&tp->intf->dev, "successfully applied %s\n", phy->info);
4958 }
4959 
4960 static void rtl8152_fw_mac_apply(struct r8152 *tp, struct fw_mac *mac)
4961 {
4962 	u16 bp_en_addr, bp_index, type, bp_num, fw_ver_reg;
4963 	u32 length;
4964 	u8 *data;
4965 	int i;
4966 
4967 	switch (__le32_to_cpu(mac->blk_hdr.type)) {
4968 	case RTL_FW_PLA:
4969 		type = MCU_TYPE_PLA;
4970 		break;
4971 	case RTL_FW_USB:
4972 		type = MCU_TYPE_USB;
4973 		break;
4974 	default:
4975 		return;
4976 	}
4977 
4978 	fw_ver_reg = __le16_to_cpu(mac->fw_ver_reg);
4979 	if (fw_ver_reg && ocp_read_byte(tp, MCU_TYPE_USB, fw_ver_reg) >= mac->fw_ver_data) {
4980 		dev_dbg(&tp->intf->dev, "%s firmware has been the newest\n", type ? "PLA" : "USB");
4981 		return;
4982 	}
4983 
4984 	rtl_clear_bp(tp, type);
4985 
4986 	/* Enable backup/restore of MACDBG. This is required after clearing PLA
4987 	 * break points and before applying the PLA firmware.
4988 	 */
4989 	if (tp->version == RTL_VER_04 && type == MCU_TYPE_PLA &&
4990 	    !(ocp_read_word(tp, MCU_TYPE_PLA, PLA_MACDBG_POST) & DEBUG_OE)) {
4991 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_MACDBG_PRE, DEBUG_LTSSM);
4992 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_MACDBG_POST, DEBUG_LTSSM);
4993 	}
4994 
4995 	length = __le32_to_cpu(mac->blk_hdr.length);
4996 	length -= __le16_to_cpu(mac->fw_offset);
4997 
4998 	data = (u8 *)mac;
4999 	data += __le16_to_cpu(mac->fw_offset);
5000 
5001 	generic_ocp_write(tp, __le16_to_cpu(mac->fw_reg), 0xff, length, data,
5002 			  type);
5003 
5004 	ocp_write_word(tp, type, __le16_to_cpu(mac->bp_ba_addr),
5005 		       __le16_to_cpu(mac->bp_ba_value));
5006 
5007 	bp_index = __le16_to_cpu(mac->bp_start);
5008 	bp_num = __le16_to_cpu(mac->bp_num);
5009 	for (i = 0; i < bp_num; i++) {
5010 		ocp_write_word(tp, type, bp_index, __le16_to_cpu(mac->bp[i]));
5011 		bp_index += 2;
5012 	}
5013 
5014 	bp_en_addr = __le16_to_cpu(mac->bp_en_addr);
5015 	if (bp_en_addr)
5016 		ocp_write_word(tp, type, bp_en_addr,
5017 			       __le16_to_cpu(mac->bp_en_value));
5018 
5019 	if (fw_ver_reg)
5020 		ocp_write_byte(tp, MCU_TYPE_USB, fw_ver_reg,
5021 			       mac->fw_ver_data);
5022 
5023 	dev_dbg(&tp->intf->dev, "successfully applied %s\n", mac->info);
5024 }
5025 
5026 static void rtl8152_apply_firmware(struct r8152 *tp, bool power_cut)
5027 {
5028 	struct rtl_fw *rtl_fw = &tp->rtl_fw;
5029 	const struct firmware *fw;
5030 	struct fw_header *fw_hdr;
5031 	struct fw_phy_patch_key *key;
5032 	u16 key_addr = 0;
5033 	int i, patch_phy = 1;
5034 
5035 	if (IS_ERR_OR_NULL(rtl_fw->fw))
5036 		return;
5037 
5038 	fw = rtl_fw->fw;
5039 	fw_hdr = (struct fw_header *)fw->data;
5040 
5041 	if (rtl_fw->pre_fw)
5042 		rtl_fw->pre_fw(tp);
5043 
5044 	for (i = offsetof(struct fw_header, blocks); i < fw->size;) {
5045 		struct fw_block *block = (struct fw_block *)&fw->data[i];
5046 
5047 		switch (__le32_to_cpu(block->type)) {
5048 		case RTL_FW_END:
5049 			goto post_fw;
5050 		case RTL_FW_PLA:
5051 		case RTL_FW_USB:
5052 			rtl8152_fw_mac_apply(tp, (struct fw_mac *)block);
5053 			break;
5054 		case RTL_FW_PHY_START:
5055 			if (!patch_phy)
5056 				break;
5057 			key = (struct fw_phy_patch_key *)block;
5058 			key_addr = __le16_to_cpu(key->key_reg);
5059 			rtl_pre_ram_code(tp, key_addr, __le16_to_cpu(key->key_data), !power_cut);
5060 			break;
5061 		case RTL_FW_PHY_STOP:
5062 			if (!patch_phy)
5063 				break;
5064 			WARN_ON(!key_addr);
5065 			rtl_post_ram_code(tp, key_addr, !power_cut);
5066 			break;
5067 		case RTL_FW_PHY_NC:
5068 			rtl8152_fw_phy_nc_apply(tp, (struct fw_phy_nc *)block);
5069 			break;
5070 		case RTL_FW_PHY_VER:
5071 			patch_phy = rtl8152_fw_phy_ver(tp, (struct fw_phy_ver *)block);
5072 			break;
5073 		case RTL_FW_PHY_UNION_NC:
5074 		case RTL_FW_PHY_UNION_NC1:
5075 		case RTL_FW_PHY_UNION_NC2:
5076 		case RTL_FW_PHY_UNION_UC2:
5077 		case RTL_FW_PHY_UNION_UC:
5078 		case RTL_FW_PHY_UNION_MISC:
5079 			if (patch_phy)
5080 				rtl8152_fw_phy_union_apply(tp, (struct fw_phy_union *)block);
5081 			break;
5082 		case RTL_FW_PHY_FIXUP:
5083 			if (patch_phy)
5084 				rtl8152_fw_phy_fixup(tp, (struct fw_phy_fixup *)block);
5085 			break;
5086 		case RTL_FW_PHY_SPEED_UP:
5087 			rtl_ram_code_speed_up(tp, (struct fw_phy_speed_up *)block, !power_cut);
5088 			break;
5089 		default:
5090 			break;
5091 		}
5092 
5093 		i += ALIGN(__le32_to_cpu(block->length), 8);
5094 	}
5095 
5096 post_fw:
5097 	if (rtl_fw->post_fw)
5098 		rtl_fw->post_fw(tp);
5099 
5100 	strscpy(rtl_fw->version, fw_hdr->version, RTL_VER_SIZE);
5101 	dev_info(&tp->intf->dev, "load %s successfully\n", rtl_fw->version);
5102 }
5103 
5104 static void rtl8152_release_firmware(struct r8152 *tp)
5105 {
5106 	struct rtl_fw *rtl_fw = &tp->rtl_fw;
5107 
5108 	if (!IS_ERR_OR_NULL(rtl_fw->fw)) {
5109 		release_firmware(rtl_fw->fw);
5110 		rtl_fw->fw = NULL;
5111 	}
5112 }
5113 
5114 static int rtl8152_request_firmware(struct r8152 *tp)
5115 {
5116 	struct rtl_fw *rtl_fw = &tp->rtl_fw;
5117 	long rc;
5118 
5119 	if (rtl_fw->fw || !rtl_fw->fw_name) {
5120 		dev_info(&tp->intf->dev, "skip request firmware\n");
5121 		rc = 0;
5122 		goto result;
5123 	}
5124 
5125 	rc = request_firmware(&rtl_fw->fw, rtl_fw->fw_name, &tp->intf->dev);
5126 	if (rc < 0)
5127 		goto result;
5128 
5129 	rc = rtl8152_check_firmware(tp, rtl_fw);
5130 	if (rc < 0)
5131 		release_firmware(rtl_fw->fw);
5132 
5133 result:
5134 	if (rc) {
5135 		rtl_fw->fw = ERR_PTR(rc);
5136 
5137 		dev_warn(&tp->intf->dev,
5138 			 "unable to load firmware patch %s (%ld)\n",
5139 			 rtl_fw->fw_name, rc);
5140 	}
5141 
5142 	return rc;
5143 }
5144 
5145 static void r8152_aldps_en(struct r8152 *tp, bool enable)
5146 {
5147 	if (enable) {
5148 		ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
5149 						    LINKENA | DIS_SDSAVE);
5150 	} else {
5151 		ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA |
5152 						    DIS_SDSAVE);
5153 		msleep(20);
5154 	}
5155 }
5156 
5157 static inline void r8152_mmd_indirect(struct r8152 *tp, u16 dev, u16 reg)
5158 {
5159 	ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | dev);
5160 	ocp_reg_write(tp, OCP_EEE_DATA, reg);
5161 	ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | dev);
5162 }
5163 
5164 static u16 r8152_mmd_read(struct r8152 *tp, u16 dev, u16 reg)
5165 {
5166 	u16 data;
5167 
5168 	r8152_mmd_indirect(tp, dev, reg);
5169 	data = ocp_reg_read(tp, OCP_EEE_DATA);
5170 	ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
5171 
5172 	return data;
5173 }
5174 
5175 static void r8152_mmd_write(struct r8152 *tp, u16 dev, u16 reg, u16 data)
5176 {
5177 	r8152_mmd_indirect(tp, dev, reg);
5178 	ocp_reg_write(tp, OCP_EEE_DATA, data);
5179 	ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
5180 }
5181 
5182 static void r8152_eee_en(struct r8152 *tp, bool enable)
5183 {
5184 	u16 config1, config2, config3;
5185 	u32 ocp_data;
5186 
5187 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
5188 	config1 = ocp_reg_read(tp, OCP_EEE_CONFIG1) & ~sd_rise_time_mask;
5189 	config2 = ocp_reg_read(tp, OCP_EEE_CONFIG2);
5190 	config3 = ocp_reg_read(tp, OCP_EEE_CONFIG3) & ~fast_snr_mask;
5191 
5192 	if (enable) {
5193 		ocp_data |= EEE_RX_EN | EEE_TX_EN;
5194 		config1 |= EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | RX_QUIET_EN;
5195 		config1 |= sd_rise_time(1);
5196 		config2 |= RG_DACQUIET_EN | RG_LDVQUIET_EN;
5197 		config3 |= fast_snr(42);
5198 	} else {
5199 		ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
5200 		config1 &= ~(EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN |
5201 			     RX_QUIET_EN);
5202 		config1 |= sd_rise_time(7);
5203 		config2 &= ~(RG_DACQUIET_EN | RG_LDVQUIET_EN);
5204 		config3 |= fast_snr(511);
5205 	}
5206 
5207 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
5208 	ocp_reg_write(tp, OCP_EEE_CONFIG1, config1);
5209 	ocp_reg_write(tp, OCP_EEE_CONFIG2, config2);
5210 	ocp_reg_write(tp, OCP_EEE_CONFIG3, config3);
5211 }
5212 
5213 static void r8153_eee_en(struct r8152 *tp, bool enable)
5214 {
5215 	u32 ocp_data;
5216 	u16 config;
5217 
5218 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
5219 	config = ocp_reg_read(tp, OCP_EEE_CFG);
5220 
5221 	if (enable) {
5222 		ocp_data |= EEE_RX_EN | EEE_TX_EN;
5223 		config |= EEE10_EN;
5224 	} else {
5225 		ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
5226 		config &= ~EEE10_EN;
5227 	}
5228 
5229 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
5230 	ocp_reg_write(tp, OCP_EEE_CFG, config);
5231 
5232 	tp->ups_info.eee = enable;
5233 }
5234 
5235 static void r8156_eee_en(struct r8152 *tp, bool enable)
5236 {
5237 	u16 config;
5238 
5239 	r8153_eee_en(tp, enable);
5240 
5241 	config = ocp_reg_read(tp, OCP_EEE_ADV2);
5242 
5243 	if (enable)
5244 		config |= MDIO_EEE_2_5GT;
5245 	else
5246 		config &= ~MDIO_EEE_2_5GT;
5247 
5248 	ocp_reg_write(tp, OCP_EEE_ADV2, config);
5249 }
5250 
5251 static void rtl_eee_enable(struct r8152 *tp, bool enable)
5252 {
5253 	switch (tp->version) {
5254 	case RTL_VER_01:
5255 	case RTL_VER_02:
5256 	case RTL_VER_07:
5257 		if (enable) {
5258 			r8152_eee_en(tp, true);
5259 			r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV,
5260 					tp->eee_adv);
5261 		} else {
5262 			r8152_eee_en(tp, false);
5263 			r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, 0);
5264 		}
5265 		break;
5266 	case RTL_VER_03:
5267 	case RTL_VER_04:
5268 	case RTL_VER_05:
5269 	case RTL_VER_06:
5270 	case RTL_VER_08:
5271 	case RTL_VER_09:
5272 	case RTL_VER_14:
5273 		if (enable) {
5274 			r8153_eee_en(tp, true);
5275 			ocp_reg_write(tp, OCP_EEE_ADV, tp->eee_adv);
5276 		} else {
5277 			r8153_eee_en(tp, false);
5278 			ocp_reg_write(tp, OCP_EEE_ADV, 0);
5279 		}
5280 		break;
5281 	case RTL_VER_10:
5282 	case RTL_VER_11:
5283 	case RTL_VER_12:
5284 	case RTL_VER_13:
5285 	case RTL_VER_15:
5286 		if (enable) {
5287 			r8156_eee_en(tp, true);
5288 			ocp_reg_write(tp, OCP_EEE_ADV, tp->eee_adv);
5289 		} else {
5290 			r8156_eee_en(tp, false);
5291 			ocp_reg_write(tp, OCP_EEE_ADV, 0);
5292 		}
5293 		break;
5294 	default:
5295 		break;
5296 	}
5297 }
5298 
5299 static void r8152b_enable_fc(struct r8152 *tp)
5300 {
5301 	u16 anar;
5302 
5303 	anar = r8152_mdio_read(tp, MII_ADVERTISE);
5304 	anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
5305 	r8152_mdio_write(tp, MII_ADVERTISE, anar);
5306 
5307 	tp->ups_info.flow_control = true;
5308 }
5309 
5310 static void rtl8152_disable(struct r8152 *tp)
5311 {
5312 	r8152_aldps_en(tp, false);
5313 	rtl_disable(tp);
5314 	r8152_aldps_en(tp, true);
5315 }
5316 
5317 static void r8152b_hw_phy_cfg(struct r8152 *tp)
5318 {
5319 	rtl8152_apply_firmware(tp, false);
5320 	rtl_eee_enable(tp, tp->eee_en);
5321 	r8152_aldps_en(tp, true);
5322 	r8152b_enable_fc(tp);
5323 
5324 	set_bit(PHY_RESET, &tp->flags);
5325 }
5326 
5327 static void wait_oob_link_list_ready(struct r8152 *tp)
5328 {
5329 	u32 ocp_data;
5330 	int i;
5331 
5332 	for (i = 0; i < 1000; i++) {
5333 		ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
5334 		if (ocp_data & LINK_LIST_READY)
5335 			break;
5336 		usleep_range(1000, 2000);
5337 	}
5338 }
5339 
5340 static void r8156b_wait_loading_flash(struct r8152 *tp)
5341 {
5342 	if ((ocp_read_word(tp, MCU_TYPE_PLA, PLA_GPHY_CTRL) & GPHY_FLASH) &&
5343 	    !(ocp_read_word(tp, MCU_TYPE_USB, USB_GPHY_CTRL) & BYPASS_FLASH)) {
5344 		int i;
5345 
5346 		for (i = 0; i < 100; i++) {
5347 			if (ocp_read_word(tp, MCU_TYPE_USB, USB_GPHY_CTRL) & GPHY_PATCH_DONE)
5348 				break;
5349 			usleep_range(1000, 2000);
5350 		}
5351 	}
5352 }
5353 
5354 static void r8152b_exit_oob(struct r8152 *tp)
5355 {
5356 	u32 ocp_data;
5357 
5358 	ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
5359 	ocp_data &= ~RCR_ACPT_ALL;
5360 	ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
5361 
5362 	rxdy_gated_en(tp, true);
5363 	r8153_teredo_off(tp);
5364 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
5365 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
5366 
5367 	ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
5368 	ocp_data &= ~NOW_IS_OOB;
5369 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
5370 
5371 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
5372 	ocp_data &= ~MCU_BORW_EN;
5373 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
5374 
5375 	wait_oob_link_list_ready(tp);
5376 
5377 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
5378 	ocp_data |= RE_INIT_LL;
5379 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
5380 
5381 	wait_oob_link_list_ready(tp);
5382 
5383 	rtl8152_nic_reset(tp);
5384 
5385 	/* rx share fifo credit full threshold */
5386 	ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
5387 
5388 	if (tp->udev->speed == USB_SPEED_FULL ||
5389 	    tp->udev->speed == USB_SPEED_LOW) {
5390 		/* rx share fifo credit near full threshold */
5391 		ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
5392 				RXFIFO_THR2_FULL);
5393 		ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
5394 				RXFIFO_THR3_FULL);
5395 	} else {
5396 		/* rx share fifo credit near full threshold */
5397 		ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
5398 				RXFIFO_THR2_HIGH);
5399 		ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
5400 				RXFIFO_THR3_HIGH);
5401 	}
5402 
5403 	/* TX share fifo free credit full threshold */
5404 	ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
5405 
5406 	ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
5407 	ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH);
5408 	ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
5409 			TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
5410 
5411 	rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
5412 
5413 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
5414 
5415 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
5416 	ocp_data |= TCR0_AUTO_FIFO;
5417 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
5418 }
5419 
5420 static void r8152b_enter_oob(struct r8152 *tp)
5421 {
5422 	u32 ocp_data;
5423 
5424 	ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
5425 	ocp_data &= ~NOW_IS_OOB;
5426 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
5427 
5428 	ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
5429 	ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
5430 	ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
5431 
5432 	rtl_disable(tp);
5433 
5434 	wait_oob_link_list_ready(tp);
5435 
5436 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
5437 	ocp_data |= RE_INIT_LL;
5438 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
5439 
5440 	wait_oob_link_list_ready(tp);
5441 
5442 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
5443 
5444 	rtl_rx_vlan_en(tp, true);
5445 
5446 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR);
5447 	ocp_data |= ALDPS_PROXY_MODE;
5448 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data);
5449 
5450 	ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
5451 	ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
5452 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
5453 
5454 	rxdy_gated_en(tp, false);
5455 
5456 	ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
5457 	ocp_data |= RCR_APM | RCR_AM | RCR_AB;
5458 	ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
5459 }
5460 
5461 static int r8153_pre_firmware_1(struct r8152 *tp)
5462 {
5463 	int i;
5464 
5465 	/* Wait till the WTD timer is ready. It would take at most 104 ms. */
5466 	for (i = 0; i < 104; i++) {
5467 		u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_WDT1_CTRL);
5468 
5469 		if (!(ocp_data & WTD1_EN))
5470 			break;
5471 		usleep_range(1000, 2000);
5472 	}
5473 
5474 	return 0;
5475 }
5476 
5477 static int r8153_post_firmware_1(struct r8152 *tp)
5478 {
5479 	/* set USB_BP_4 to support USB_SPEED_SUPER only */
5480 	if (ocp_read_byte(tp, MCU_TYPE_USB, USB_CSTMR) & FORCE_SUPER)
5481 		ocp_write_word(tp, MCU_TYPE_USB, USB_BP_4, BP4_SUPER_ONLY);
5482 
5483 	/* reset UPHY timer to 36 ms */
5484 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_UPHY_TIMER, 36000 / 16);
5485 
5486 	return 0;
5487 }
5488 
5489 static int r8153_pre_firmware_2(struct r8152 *tp)
5490 {
5491 	u32 ocp_data;
5492 
5493 	r8153_pre_firmware_1(tp);
5494 
5495 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0);
5496 	ocp_data &= ~FW_FIX_SUSPEND;
5497 	ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0, ocp_data);
5498 
5499 	return 0;
5500 }
5501 
5502 static int r8153_post_firmware_2(struct r8152 *tp)
5503 {
5504 	u32 ocp_data;
5505 
5506 	/* enable bp0 if support USB_SPEED_SUPER only */
5507 	if (ocp_read_byte(tp, MCU_TYPE_USB, USB_CSTMR) & FORCE_SUPER) {
5508 		ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BP_EN);
5509 		ocp_data |= BIT(0);
5510 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_EN, ocp_data);
5511 	}
5512 
5513 	/* reset UPHY timer to 36 ms */
5514 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_UPHY_TIMER, 36000 / 16);
5515 
5516 	/* enable U3P3 check, set the counter to 4 */
5517 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, U3P3_CHECK_EN | 4);
5518 
5519 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0);
5520 	ocp_data |= FW_FIX_SUSPEND;
5521 	ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN0, ocp_data);
5522 
5523 	ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
5524 	ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
5525 	ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
5526 
5527 	return 0;
5528 }
5529 
5530 static int r8153_post_firmware_3(struct r8152 *tp)
5531 {
5532 	u32 ocp_data;
5533 
5534 	ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
5535 	ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
5536 	ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
5537 
5538 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1);
5539 	ocp_data |= FW_IP_RESET_EN;
5540 	ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1, ocp_data);
5541 
5542 	return 0;
5543 }
5544 
5545 static int r8153b_pre_firmware_1(struct r8152 *tp)
5546 {
5547 	/* enable fc timer and set timer to 1 second. */
5548 	ocp_write_word(tp, MCU_TYPE_USB, USB_FC_TIMER,
5549 		       CTRL_TIMER_EN | (1000 / 8));
5550 
5551 	return 0;
5552 }
5553 
5554 static int r8153b_post_firmware_1(struct r8152 *tp)
5555 {
5556 	u32 ocp_data;
5557 
5558 	/* enable bp0 for RTL8153-BND */
5559 	ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_1);
5560 	if (ocp_data & BND_MASK) {
5561 		ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BP_EN);
5562 		ocp_data |= BIT(0);
5563 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_EN, ocp_data);
5564 	}
5565 
5566 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_CTRL);
5567 	ocp_data |= FLOW_CTRL_PATCH_OPT;
5568 	ocp_write_word(tp, MCU_TYPE_USB, USB_FW_CTRL, ocp_data);
5569 
5570 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK);
5571 	ocp_data |= FC_PATCH_TASK;
5572 	ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
5573 
5574 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1);
5575 	ocp_data |= FW_IP_RESET_EN;
5576 	ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1, ocp_data);
5577 
5578 	return 0;
5579 }
5580 
5581 static int r8153c_post_firmware_1(struct r8152 *tp)
5582 {
5583 	u32 ocp_data;
5584 
5585 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_CTRL);
5586 	ocp_data |= FLOW_CTRL_PATCH_2;
5587 	ocp_write_word(tp, MCU_TYPE_USB, USB_FW_CTRL, ocp_data);
5588 
5589 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK);
5590 	ocp_data |= FC_PATCH_TASK;
5591 	ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
5592 
5593 	return 0;
5594 }
5595 
5596 static int r8156a_post_firmware_1(struct r8152 *tp)
5597 {
5598 	u32 ocp_data;
5599 
5600 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1);
5601 	ocp_data |= FW_IP_RESET_EN;
5602 	ocp_write_word(tp, MCU_TYPE_USB, USB_FW_FIX_EN1, ocp_data);
5603 
5604 	/* Modify U3PHY parameter for compatibility issue */
5605 	ocp_write_dword(tp, MCU_TYPE_USB, USB_UPHY3_MDCMDIO, 0x4026840e);
5606 	ocp_write_dword(tp, MCU_TYPE_USB, USB_UPHY3_MDCMDIO, 0x4001acc9);
5607 
5608 	return 0;
5609 }
5610 
5611 static void r8153_aldps_en(struct r8152 *tp, bool enable)
5612 {
5613 	u16 data;
5614 
5615 	data = ocp_reg_read(tp, OCP_POWER_CFG);
5616 	if (enable) {
5617 		data |= EN_ALDPS;
5618 		ocp_reg_write(tp, OCP_POWER_CFG, data);
5619 	} else {
5620 		int i;
5621 
5622 		data &= ~EN_ALDPS;
5623 		ocp_reg_write(tp, OCP_POWER_CFG, data);
5624 		for (i = 0; i < 20; i++) {
5625 			usleep_range(1000, 2000);
5626 			if (ocp_read_word(tp, MCU_TYPE_PLA, 0xe000) & 0x0100)
5627 				break;
5628 		}
5629 	}
5630 
5631 	tp->ups_info.aldps = enable;
5632 }
5633 
5634 static void r8153_hw_phy_cfg(struct r8152 *tp)
5635 {
5636 	u32 ocp_data;
5637 	u16 data;
5638 
5639 	/* disable ALDPS before updating the PHY parameters */
5640 	r8153_aldps_en(tp, false);
5641 
5642 	/* disable EEE before updating the PHY parameters */
5643 	rtl_eee_enable(tp, false);
5644 
5645 	rtl8152_apply_firmware(tp, false);
5646 
5647 	if (tp->version == RTL_VER_03) {
5648 		data = ocp_reg_read(tp, OCP_EEE_CFG);
5649 		data &= ~CTAP_SHORT_EN;
5650 		ocp_reg_write(tp, OCP_EEE_CFG, data);
5651 	}
5652 
5653 	data = ocp_reg_read(tp, OCP_POWER_CFG);
5654 	data |= EEE_CLKDIV_EN;
5655 	ocp_reg_write(tp, OCP_POWER_CFG, data);
5656 
5657 	data = ocp_reg_read(tp, OCP_DOWN_SPEED);
5658 	data |= EN_10M_BGOFF;
5659 	ocp_reg_write(tp, OCP_DOWN_SPEED, data);
5660 	data = ocp_reg_read(tp, OCP_POWER_CFG);
5661 	data |= EN_10M_PLLOFF;
5662 	ocp_reg_write(tp, OCP_POWER_CFG, data);
5663 	sram_write(tp, SRAM_IMPEDANCE, 0x0b13);
5664 
5665 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
5666 	ocp_data |= PFM_PWM_SWITCH;
5667 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
5668 
5669 	/* Enable LPF corner auto tune */
5670 	sram_write(tp, SRAM_LPF_CFG, 0xf70f);
5671 
5672 	/* Adjust 10M Amplitude */
5673 	sram_write(tp, SRAM_10M_AMP1, 0x00af);
5674 	sram_write(tp, SRAM_10M_AMP2, 0x0208);
5675 
5676 	if (tp->eee_en)
5677 		rtl_eee_enable(tp, true);
5678 
5679 	r8153_aldps_en(tp, true);
5680 	r8152b_enable_fc(tp);
5681 
5682 	switch (tp->version) {
5683 	case RTL_VER_03:
5684 	case RTL_VER_04:
5685 		break;
5686 	case RTL_VER_05:
5687 	case RTL_VER_06:
5688 	default:
5689 		r8153_u2p3en(tp, true);
5690 		break;
5691 	}
5692 
5693 	set_bit(PHY_RESET, &tp->flags);
5694 }
5695 
5696 static u32 r8152_efuse_read(struct r8152 *tp, u8 addr)
5697 {
5698 	u32 ocp_data;
5699 
5700 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD, EFUSE_READ_CMD | addr);
5701 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD);
5702 	ocp_data = (ocp_data & EFUSE_DATA_BIT16) << 9;	/* data of bit16 */
5703 	ocp_data |= ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_DATA);
5704 
5705 	return ocp_data;
5706 }
5707 
5708 static void r8153b_hw_phy_cfg(struct r8152 *tp)
5709 {
5710 	u32 ocp_data;
5711 	u16 data;
5712 
5713 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
5714 	if (ocp_data & PCUT_STATUS) {
5715 		ocp_data &= ~PCUT_STATUS;
5716 		ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
5717 	}
5718 
5719 	/* disable ALDPS before updating the PHY parameters */
5720 	r8153_aldps_en(tp, false);
5721 
5722 	/* disable EEE before updating the PHY parameters */
5723 	rtl_eee_enable(tp, false);
5724 
5725 	/* U1/U2/L1 idle timer. 500 us */
5726 	ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500);
5727 
5728 	data = r8153_phy_status(tp, 0);
5729 
5730 	switch (data) {
5731 	case PHY_STAT_PWRDN:
5732 	case PHY_STAT_EXT_INIT:
5733 		rtl8152_apply_firmware(tp, true);
5734 
5735 		data = r8152_mdio_read(tp, MII_BMCR);
5736 		data &= ~BMCR_PDOWN;
5737 		r8152_mdio_write(tp, MII_BMCR, data);
5738 		break;
5739 	case PHY_STAT_LAN_ON:
5740 	default:
5741 		rtl8152_apply_firmware(tp, false);
5742 		break;
5743 	}
5744 
5745 	r8153b_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags));
5746 
5747 	data = sram_read(tp, SRAM_GREEN_CFG);
5748 	data |= R_TUNE_EN;
5749 	sram_write(tp, SRAM_GREEN_CFG, data);
5750 	data = ocp_reg_read(tp, OCP_NCTL_CFG);
5751 	data |= PGA_RETURN_EN;
5752 	ocp_reg_write(tp, OCP_NCTL_CFG, data);
5753 
5754 	/* ADC Bias Calibration:
5755 	 * read efuse offset 0x7d to get a 17-bit data. Remove the dummy/fake
5756 	 * bit (bit3) to rebuild the real 16-bit data. Write the data to the
5757 	 * ADC ioffset.
5758 	 */
5759 	ocp_data = r8152_efuse_read(tp, 0x7d);
5760 	data = (u16)(((ocp_data & 0x1fff0) >> 1) | (ocp_data & 0x7));
5761 	if (data != 0xffff)
5762 		ocp_reg_write(tp, OCP_ADC_IOFFSET, data);
5763 
5764 	/* ups mode tx-link-pulse timing adjustment:
5765 	 * rg_saw_cnt = OCP reg 0xC426 Bit[13:0]
5766 	 * swr_cnt_1ms_ini = 16000000 / rg_saw_cnt
5767 	 */
5768 	ocp_data = ocp_reg_read(tp, 0xc426);
5769 	ocp_data &= 0x3fff;
5770 	if (ocp_data) {
5771 		u32 swr_cnt_1ms_ini;
5772 
5773 		swr_cnt_1ms_ini = (16000000 / ocp_data) & SAW_CNT_1MS_MASK;
5774 		ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CFG);
5775 		ocp_data = (ocp_data & ~SAW_CNT_1MS_MASK) | swr_cnt_1ms_ini;
5776 		ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CFG, ocp_data);
5777 	}
5778 
5779 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
5780 	ocp_data |= PFM_PWM_SWITCH;
5781 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
5782 
5783 	/* Advnace EEE */
5784 	if (!rtl_phy_patch_request(tp, true, true)) {
5785 		data = ocp_reg_read(tp, OCP_POWER_CFG);
5786 		data |= EEE_CLKDIV_EN;
5787 		ocp_reg_write(tp, OCP_POWER_CFG, data);
5788 		tp->ups_info.eee_ckdiv = true;
5789 
5790 		data = ocp_reg_read(tp, OCP_DOWN_SPEED);
5791 		data |= EN_EEE_CMODE | EN_EEE_1000 | EN_10M_CLKDIV;
5792 		ocp_reg_write(tp, OCP_DOWN_SPEED, data);
5793 		tp->ups_info.eee_cmod_lv = true;
5794 		tp->ups_info._10m_ckdiv = true;
5795 		tp->ups_info.eee_plloff_giga = true;
5796 
5797 		ocp_reg_write(tp, OCP_SYSCLK_CFG, 0);
5798 		ocp_reg_write(tp, OCP_SYSCLK_CFG, clk_div_expo(5));
5799 		tp->ups_info._250m_ckdiv = true;
5800 
5801 		rtl_phy_patch_request(tp, false, true);
5802 	}
5803 
5804 	if (tp->eee_en)
5805 		rtl_eee_enable(tp, true);
5806 
5807 	r8153_aldps_en(tp, true);
5808 	r8152b_enable_fc(tp);
5809 
5810 	set_bit(PHY_RESET, &tp->flags);
5811 }
5812 
5813 static void r8153c_hw_phy_cfg(struct r8152 *tp)
5814 {
5815 	r8153b_hw_phy_cfg(tp);
5816 
5817 	tp->ups_info.r_tune = true;
5818 }
5819 
5820 static void rtl8153_change_mtu(struct r8152 *tp)
5821 {
5822 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, mtu_to_size(tp->netdev->mtu));
5823 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO);
5824 }
5825 
5826 static void r8153_first_init(struct r8152 *tp)
5827 {
5828 	u32 ocp_data;
5829 
5830 	rxdy_gated_en(tp, true);
5831 	r8153_teredo_off(tp);
5832 
5833 	ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
5834 	ocp_data &= ~RCR_ACPT_ALL;
5835 	ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
5836 
5837 	rtl8152_nic_reset(tp);
5838 	rtl_reset_bmu(tp);
5839 
5840 	ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
5841 	ocp_data &= ~NOW_IS_OOB;
5842 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
5843 
5844 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
5845 	ocp_data &= ~MCU_BORW_EN;
5846 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
5847 
5848 	wait_oob_link_list_ready(tp);
5849 
5850 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
5851 	ocp_data |= RE_INIT_LL;
5852 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
5853 
5854 	wait_oob_link_list_ready(tp);
5855 
5856 	rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
5857 
5858 	rtl8153_change_mtu(tp);
5859 
5860 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
5861 	ocp_data |= TCR0_AUTO_FIFO;
5862 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
5863 
5864 	rtl8152_nic_reset(tp);
5865 
5866 	/* rx share fifo credit full threshold */
5867 	ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
5868 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
5869 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
5870 	/* TX share fifo free credit full threshold */
5871 	ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
5872 }
5873 
5874 static void r8153_enter_oob(struct r8152 *tp)
5875 {
5876 	u32 ocp_data;
5877 
5878 	ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
5879 	ocp_data &= ~NOW_IS_OOB;
5880 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
5881 
5882 	rtl_disable(tp);
5883 	rtl_reset_bmu(tp);
5884 
5885 	wait_oob_link_list_ready(tp);
5886 
5887 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
5888 	ocp_data |= RE_INIT_LL;
5889 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
5890 
5891 	wait_oob_link_list_ready(tp);
5892 
5893 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, mtu_to_size(tp->netdev->mtu));
5894 
5895 	switch (tp->version) {
5896 	case RTL_VER_03:
5897 	case RTL_VER_04:
5898 	case RTL_VER_05:
5899 	case RTL_VER_06:
5900 		ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
5901 		ocp_data &= ~TEREDO_WAKE_MASK;
5902 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
5903 		break;
5904 
5905 	case RTL_VER_08:
5906 	case RTL_VER_09:
5907 	case RTL_VER_14:
5908 		/* Clear teredo wake event. bit[15:8] is the teredo wakeup
5909 		 * type. Set it to zero. bits[7:0] are the W1C bits about
5910 		 * the events. Set them to all 1 to clear them.
5911 		 */
5912 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_WAKE_BASE, 0x00ff);
5913 		break;
5914 
5915 	default:
5916 		break;
5917 	}
5918 
5919 	rtl_rx_vlan_en(tp, true);
5920 
5921 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_BDC_CR);
5922 	ocp_data |= ALDPS_PROXY_MODE;
5923 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_BDC_CR, ocp_data);
5924 
5925 	ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
5926 	ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
5927 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
5928 
5929 	rxdy_gated_en(tp, false);
5930 
5931 	ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
5932 	ocp_data |= RCR_APM | RCR_AM | RCR_AB;
5933 	ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
5934 }
5935 
5936 static void rtl8153_disable(struct r8152 *tp)
5937 {
5938 	r8153_aldps_en(tp, false);
5939 	rtl_disable(tp);
5940 	rtl_reset_bmu(tp);
5941 	r8153_aldps_en(tp, true);
5942 }
5943 
5944 static int rtl8156_enable(struct r8152 *tp)
5945 {
5946 	u32 ocp_data;
5947 	u16 speed;
5948 
5949 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
5950 		return -ENODEV;
5951 
5952 	set_tx_qlen(tp);
5953 	rtl_set_eee_plus(tp);
5954 	r8153_set_rx_early_timeout(tp);
5955 	r8153_set_rx_early_size(tp);
5956 
5957 	speed = rtl8152_get_speed(tp);
5958 	rtl_set_ifg(tp, speed);
5959 
5960 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4);
5961 	if (speed & _2500bps)
5962 		ocp_data &= ~IDLE_SPDWN_EN;
5963 	else
5964 		ocp_data |= IDLE_SPDWN_EN;
5965 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data);
5966 
5967 	if (speed & _1000bps)
5968 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_TXTWSYS, 0x11);
5969 	else if (speed & _500bps)
5970 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_TXTWSYS, 0x3d);
5971 
5972 	if (tp->udev->speed == USB_SPEED_HIGH) {
5973 		/* USB 0xb45e[3:0] l1_nyet_hird */
5974 		ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_L1_CTRL);
5975 		ocp_data &= ~0xf;
5976 		if (is_flow_control(speed))
5977 			ocp_data |= 0xf;
5978 		else
5979 			ocp_data |= 0x1;
5980 		ocp_write_word(tp, MCU_TYPE_USB, USB_L1_CTRL, ocp_data);
5981 	}
5982 
5983 	return rtl_enable(tp);
5984 }
5985 
5986 static int rtl8156b_enable(struct r8152 *tp)
5987 {
5988 	u32 ocp_data;
5989 	u16 speed;
5990 
5991 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
5992 		return -ENODEV;
5993 
5994 	set_tx_qlen(tp);
5995 	rtl_set_eee_plus(tp);
5996 
5997 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_RX_AGGR_NUM);
5998 	ocp_data &= ~RX_AGGR_NUM_MASK;
5999 	ocp_write_word(tp, MCU_TYPE_USB, USB_RX_AGGR_NUM, ocp_data);
6000 
6001 	r8153_set_rx_early_timeout(tp);
6002 	r8153_set_rx_early_size(tp);
6003 
6004 	speed = rtl8152_get_speed(tp);
6005 	rtl_set_ifg(tp, speed);
6006 
6007 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4);
6008 	if (speed & _2500bps)
6009 		ocp_data &= ~IDLE_SPDWN_EN;
6010 	else
6011 		ocp_data |= IDLE_SPDWN_EN;
6012 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data);
6013 
6014 	if (tp->udev->speed == USB_SPEED_HIGH) {
6015 		ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_L1_CTRL);
6016 		ocp_data &= ~0xf;
6017 		if (is_flow_control(speed))
6018 			ocp_data |= 0xf;
6019 		else
6020 			ocp_data |= 0x1;
6021 		ocp_write_word(tp, MCU_TYPE_USB, USB_L1_CTRL, ocp_data);
6022 	}
6023 
6024 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK);
6025 	ocp_data &= ~FC_PATCH_TASK;
6026 	ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
6027 	usleep_range(1000, 2000);
6028 	ocp_data |= FC_PATCH_TASK;
6029 	ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
6030 
6031 	return rtl_enable(tp);
6032 }
6033 
6034 static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u32 speed, u8 duplex,
6035 			     u32 advertising)
6036 {
6037 	u16 bmcr;
6038 	int ret = 0;
6039 
6040 	if (autoneg == AUTONEG_DISABLE) {
6041 		if (duplex != DUPLEX_HALF && duplex != DUPLEX_FULL)
6042 			return -EINVAL;
6043 
6044 		switch (speed) {
6045 		case SPEED_10:
6046 			bmcr = BMCR_SPEED10;
6047 			if (duplex == DUPLEX_FULL) {
6048 				bmcr |= BMCR_FULLDPLX;
6049 				tp->ups_info.speed_duplex = FORCE_10M_FULL;
6050 			} else {
6051 				tp->ups_info.speed_duplex = FORCE_10M_HALF;
6052 			}
6053 			break;
6054 		case SPEED_100:
6055 			bmcr = BMCR_SPEED100;
6056 			if (duplex == DUPLEX_FULL) {
6057 				bmcr |= BMCR_FULLDPLX;
6058 				tp->ups_info.speed_duplex = FORCE_100M_FULL;
6059 			} else {
6060 				tp->ups_info.speed_duplex = FORCE_100M_HALF;
6061 			}
6062 			break;
6063 		case SPEED_1000:
6064 			if (tp->mii.supports_gmii) {
6065 				bmcr = BMCR_SPEED1000 | BMCR_FULLDPLX;
6066 				tp->ups_info.speed_duplex = NWAY_1000M_FULL;
6067 				break;
6068 			}
6069 			fallthrough;
6070 		default:
6071 			ret = -EINVAL;
6072 			goto out;
6073 		}
6074 
6075 		if (duplex == DUPLEX_FULL)
6076 			tp->mii.full_duplex = 1;
6077 		else
6078 			tp->mii.full_duplex = 0;
6079 
6080 		tp->mii.force_media = 1;
6081 	} else {
6082 		u16 orig, new1;
6083 		u32 support;
6084 
6085 		support = RTL_ADVERTISED_10_HALF | RTL_ADVERTISED_10_FULL |
6086 			  RTL_ADVERTISED_100_HALF | RTL_ADVERTISED_100_FULL;
6087 
6088 		if (tp->mii.supports_gmii) {
6089 			support |= RTL_ADVERTISED_1000_FULL;
6090 
6091 			if (tp->support_2500full)
6092 				support |= RTL_ADVERTISED_2500_FULL;
6093 		}
6094 
6095 		if (!(advertising & support))
6096 			return -EINVAL;
6097 
6098 		orig = r8152_mdio_read(tp, MII_ADVERTISE);
6099 		new1 = orig & ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
6100 				ADVERTISE_100HALF | ADVERTISE_100FULL);
6101 		if (advertising & RTL_ADVERTISED_10_HALF) {
6102 			new1 |= ADVERTISE_10HALF;
6103 			tp->ups_info.speed_duplex = NWAY_10M_HALF;
6104 		}
6105 		if (advertising & RTL_ADVERTISED_10_FULL) {
6106 			new1 |= ADVERTISE_10FULL;
6107 			tp->ups_info.speed_duplex = NWAY_10M_FULL;
6108 		}
6109 
6110 		if (advertising & RTL_ADVERTISED_100_HALF) {
6111 			new1 |= ADVERTISE_100HALF;
6112 			tp->ups_info.speed_duplex = NWAY_100M_HALF;
6113 		}
6114 		if (advertising & RTL_ADVERTISED_100_FULL) {
6115 			new1 |= ADVERTISE_100FULL;
6116 			tp->ups_info.speed_duplex = NWAY_100M_FULL;
6117 		}
6118 
6119 		if (orig != new1) {
6120 			r8152_mdio_write(tp, MII_ADVERTISE, new1);
6121 			tp->mii.advertising = new1;
6122 		}
6123 
6124 		if (tp->mii.supports_gmii) {
6125 			orig = r8152_mdio_read(tp, MII_CTRL1000);
6126 			new1 = orig & ~(ADVERTISE_1000FULL |
6127 					ADVERTISE_1000HALF);
6128 
6129 			if (advertising & RTL_ADVERTISED_1000_FULL) {
6130 				new1 |= ADVERTISE_1000FULL;
6131 				tp->ups_info.speed_duplex = NWAY_1000M_FULL;
6132 			}
6133 
6134 			if (orig != new1)
6135 				r8152_mdio_write(tp, MII_CTRL1000, new1);
6136 		}
6137 
6138 		if (tp->support_2500full) {
6139 			orig = ocp_reg_read(tp, OCP_10GBT_CTRL);
6140 			new1 = orig & ~MDIO_AN_10GBT_CTRL_ADV2_5G;
6141 
6142 			if (advertising & RTL_ADVERTISED_2500_FULL) {
6143 				new1 |= MDIO_AN_10GBT_CTRL_ADV2_5G;
6144 				tp->ups_info.speed_duplex = NWAY_2500M_FULL;
6145 			}
6146 
6147 			if (orig != new1)
6148 				ocp_reg_write(tp, OCP_10GBT_CTRL, new1);
6149 		}
6150 
6151 		bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
6152 
6153 		tp->mii.force_media = 0;
6154 	}
6155 
6156 	if (test_and_clear_bit(PHY_RESET, &tp->flags))
6157 		bmcr |= BMCR_RESET;
6158 
6159 	r8152_mdio_write(tp, MII_BMCR, bmcr);
6160 
6161 	if (bmcr & BMCR_RESET) {
6162 		int i;
6163 
6164 		for (i = 0; i < 50; i++) {
6165 			msleep(20);
6166 			if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
6167 				break;
6168 		}
6169 	}
6170 
6171 out:
6172 	return ret;
6173 }
6174 
6175 static void rtl8152_up(struct r8152 *tp)
6176 {
6177 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
6178 		return;
6179 
6180 	r8152_aldps_en(tp, false);
6181 	r8152b_exit_oob(tp);
6182 	r8152_aldps_en(tp, true);
6183 }
6184 
6185 static void rtl8152_down(struct r8152 *tp)
6186 {
6187 	if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
6188 		rtl_drop_queued_tx(tp);
6189 		return;
6190 	}
6191 
6192 	r8152_power_cut_en(tp, false);
6193 	r8152_aldps_en(tp, false);
6194 	r8152b_enter_oob(tp);
6195 	r8152_aldps_en(tp, true);
6196 }
6197 
6198 static void rtl8153_up(struct r8152 *tp)
6199 {
6200 	u32 ocp_data;
6201 
6202 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
6203 		return;
6204 
6205 	r8153_u1u2en(tp, false);
6206 	r8153_u2p3en(tp, false);
6207 	r8153_aldps_en(tp, false);
6208 	r8153_first_init(tp);
6209 
6210 	ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6);
6211 	ocp_data |= LANWAKE_CLR_EN;
6212 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6, ocp_data);
6213 
6214 	ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG);
6215 	ocp_data &= ~LANWAKE_PIN;
6216 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG, ocp_data);
6217 
6218 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK1);
6219 	ocp_data &= ~DELAY_PHY_PWR_CHG;
6220 	ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK1, ocp_data);
6221 
6222 	r8153_aldps_en(tp, true);
6223 
6224 	switch (tp->version) {
6225 	case RTL_VER_03:
6226 	case RTL_VER_04:
6227 		break;
6228 	case RTL_VER_05:
6229 	case RTL_VER_06:
6230 	default:
6231 		r8153_u2p3en(tp, true);
6232 		break;
6233 	}
6234 
6235 	r8153_u1u2en(tp, true);
6236 }
6237 
6238 static void rtl8153_down(struct r8152 *tp)
6239 {
6240 	u32 ocp_data;
6241 
6242 	if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
6243 		rtl_drop_queued_tx(tp);
6244 		return;
6245 	}
6246 
6247 	ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6);
6248 	ocp_data &= ~LANWAKE_CLR_EN;
6249 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6, ocp_data);
6250 
6251 	r8153_u1u2en(tp, false);
6252 	r8153_u2p3en(tp, false);
6253 	r8153_power_cut_en(tp, false);
6254 	r8153_aldps_en(tp, false);
6255 	r8153_enter_oob(tp);
6256 	r8153_aldps_en(tp, true);
6257 }
6258 
6259 static void rtl8153b_up(struct r8152 *tp)
6260 {
6261 	u32 ocp_data;
6262 
6263 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
6264 		return;
6265 
6266 	r8153b_u1u2en(tp, false);
6267 	r8153_u2p3en(tp, false);
6268 	r8153_aldps_en(tp, false);
6269 
6270 	r8153_first_init(tp);
6271 	ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_B);
6272 
6273 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
6274 	ocp_data &= ~PLA_MCU_SPDWN_EN;
6275 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
6276 
6277 	r8153_aldps_en(tp, true);
6278 
6279 	if (tp->udev->speed >= USB_SPEED_SUPER)
6280 		r8153b_u1u2en(tp, true);
6281 }
6282 
6283 static void rtl8153b_down(struct r8152 *tp)
6284 {
6285 	u32 ocp_data;
6286 
6287 	if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
6288 		rtl_drop_queued_tx(tp);
6289 		return;
6290 	}
6291 
6292 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
6293 	ocp_data |= PLA_MCU_SPDWN_EN;
6294 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
6295 
6296 	r8153b_u1u2en(tp, false);
6297 	r8153_u2p3en(tp, false);
6298 	r8153b_power_cut_en(tp, false);
6299 	r8153_aldps_en(tp, false);
6300 	r8153_enter_oob(tp);
6301 	r8153_aldps_en(tp, true);
6302 }
6303 
6304 static void rtl8153c_change_mtu(struct r8152 *tp)
6305 {
6306 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, mtu_to_size(tp->netdev->mtu));
6307 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, 10 * 1024 / 64);
6308 
6309 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, 512 / 64);
6310 
6311 	/* Adjust the tx fifo free credit full threshold, otherwise
6312 	 * the fifo would be too small to send a jumbo frame packet.
6313 	 */
6314 	if (tp->netdev->mtu < 8000)
6315 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_FULL, 2048 / 8);
6316 	else
6317 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_FULL, 900 / 8);
6318 }
6319 
6320 static void rtl8153c_up(struct r8152 *tp)
6321 {
6322 	u32 ocp_data;
6323 
6324 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
6325 		return;
6326 
6327 	r8153b_u1u2en(tp, false);
6328 	r8153_u2p3en(tp, false);
6329 	r8153_aldps_en(tp, false);
6330 
6331 	rxdy_gated_en(tp, true);
6332 	r8153_teredo_off(tp);
6333 
6334 	ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
6335 	ocp_data &= ~RCR_ACPT_ALL;
6336 	ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
6337 
6338 	rtl8152_nic_reset(tp);
6339 	rtl_reset_bmu(tp);
6340 
6341 	ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
6342 	ocp_data &= ~NOW_IS_OOB;
6343 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
6344 
6345 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
6346 	ocp_data &= ~MCU_BORW_EN;
6347 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
6348 
6349 	wait_oob_link_list_ready(tp);
6350 
6351 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
6352 	ocp_data |= RE_INIT_LL;
6353 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
6354 
6355 	wait_oob_link_list_ready(tp);
6356 
6357 	rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
6358 
6359 	rtl8153c_change_mtu(tp);
6360 
6361 	rtl8152_nic_reset(tp);
6362 
6363 	/* rx share fifo credit full threshold */
6364 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, 0x02);
6365 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_RXFIFO_FULL, 0x08);
6366 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
6367 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
6368 
6369 	ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_B);
6370 
6371 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
6372 
6373 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
6374 	ocp_data |= BIT(8);
6375 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
6376 
6377 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
6378 
6379 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
6380 	ocp_data &= ~PLA_MCU_SPDWN_EN;
6381 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
6382 
6383 	r8153_aldps_en(tp, true);
6384 	r8153b_u1u2en(tp, true);
6385 }
6386 
6387 static inline u32 fc_pause_on_auto(struct r8152 *tp)
6388 {
6389 	return (ALIGN(mtu_to_size(tp->netdev->mtu), 1024) + 6 * 1024);
6390 }
6391 
6392 static inline u32 fc_pause_off_auto(struct r8152 *tp)
6393 {
6394 	return (ALIGN(mtu_to_size(tp->netdev->mtu), 1024) + 14 * 1024);
6395 }
6396 
6397 static void r8156_fc_parameter(struct r8152 *tp)
6398 {
6399 	u32 pause_on = tp->fc_pause_on ? tp->fc_pause_on : fc_pause_on_auto(tp);
6400 	u32 pause_off = tp->fc_pause_off ? tp->fc_pause_off : fc_pause_off_auto(tp);
6401 
6402 	switch (tp->version) {
6403 	case RTL_VER_10:
6404 	case RTL_VER_11:
6405 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_FULL, pause_on / 8);
6406 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_EMPTY, pause_off / 8);
6407 		break;
6408 	case RTL_VER_12:
6409 	case RTL_VER_13:
6410 	case RTL_VER_15:
6411 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_FULL, pause_on / 16);
6412 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_RX_FIFO_EMPTY, pause_off / 16);
6413 		break;
6414 	default:
6415 		break;
6416 	}
6417 }
6418 
6419 static void rtl8156_change_mtu(struct r8152 *tp)
6420 {
6421 	u32 rx_max_size = mtu_to_size(tp->netdev->mtu);
6422 
6423 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, rx_max_size);
6424 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO);
6425 	r8156_fc_parameter(tp);
6426 
6427 	/* TX share fifo free credit full threshold */
6428 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, 512 / 64);
6429 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_TXFIFO_FULL,
6430 		       ALIGN(rx_max_size + sizeof(struct tx_desc), 1024) / 16);
6431 }
6432 
6433 static void rtl8156_up(struct r8152 *tp)
6434 {
6435 	u32 ocp_data;
6436 
6437 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
6438 		return;
6439 
6440 	r8153b_u1u2en(tp, false);
6441 	r8153_u2p3en(tp, false);
6442 	r8153_aldps_en(tp, false);
6443 
6444 	rxdy_gated_en(tp, true);
6445 	r8153_teredo_off(tp);
6446 
6447 	ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
6448 	ocp_data &= ~RCR_ACPT_ALL;
6449 	ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
6450 
6451 	rtl8152_nic_reset(tp);
6452 	rtl_reset_bmu(tp);
6453 
6454 	ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
6455 	ocp_data &= ~NOW_IS_OOB;
6456 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
6457 
6458 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
6459 	ocp_data &= ~MCU_BORW_EN;
6460 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
6461 
6462 	rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
6463 
6464 	rtl8156_change_mtu(tp);
6465 
6466 	switch (tp->version) {
6467 	case RTL_TEST_01:
6468 	case RTL_VER_10:
6469 	case RTL_VER_11:
6470 		ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_BMU_CONFIG);
6471 		ocp_data |= ACT_ODMA;
6472 		ocp_write_word(tp, MCU_TYPE_USB, USB_BMU_CONFIG, ocp_data);
6473 		break;
6474 	default:
6475 		break;
6476 	}
6477 
6478 	/* share FIFO settings */
6479 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_FULL);
6480 	ocp_data &= ~RXFIFO_FULL_MASK;
6481 	ocp_data |= 0x08;
6482 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_FULL, ocp_data);
6483 
6484 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
6485 	ocp_data &= ~PLA_MCU_SPDWN_EN;
6486 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
6487 
6488 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SPEED_OPTION);
6489 	ocp_data &= ~(RG_PWRDN_EN | ALL_SPEED_OFF);
6490 	ocp_write_word(tp, MCU_TYPE_USB, USB_SPEED_OPTION, ocp_data);
6491 
6492 	ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, 0x00600400);
6493 
6494 	if (tp->saved_wolopts != __rtl_get_wol(tp)) {
6495 		netif_warn(tp, ifup, tp->netdev, "wol setting is changed\n");
6496 		__rtl_set_wol(tp, tp->saved_wolopts);
6497 	}
6498 
6499 	r8153_aldps_en(tp, true);
6500 	r8153_u2p3en(tp, true);
6501 
6502 	if (tp->udev->speed >= USB_SPEED_SUPER)
6503 		r8153b_u1u2en(tp, true);
6504 }
6505 
6506 static void rtl8156_down(struct r8152 *tp)
6507 {
6508 	u32 ocp_data;
6509 
6510 	if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
6511 		rtl_drop_queued_tx(tp);
6512 		return;
6513 	}
6514 
6515 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
6516 	ocp_data |= PLA_MCU_SPDWN_EN;
6517 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
6518 
6519 	r8153b_u1u2en(tp, false);
6520 	r8153_u2p3en(tp, false);
6521 	r8153b_power_cut_en(tp, false);
6522 	r8153_aldps_en(tp, false);
6523 
6524 	ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
6525 	ocp_data &= ~NOW_IS_OOB;
6526 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
6527 
6528 	rtl_disable(tp);
6529 	rtl_reset_bmu(tp);
6530 
6531 	/* Clear teredo wake event. bit[15:8] is the teredo wakeup
6532 	 * type. Set it to zero. bits[7:0] are the W1C bits about
6533 	 * the events. Set them to all 1 to clear them.
6534 	 */
6535 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_WAKE_BASE, 0x00ff);
6536 
6537 	ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
6538 	ocp_data |= NOW_IS_OOB;
6539 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
6540 
6541 	rtl_rx_vlan_en(tp, true);
6542 	rxdy_gated_en(tp, false);
6543 
6544 	ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
6545 	ocp_data |= RCR_APM | RCR_AM | RCR_AB;
6546 	ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
6547 
6548 	r8153_aldps_en(tp, true);
6549 }
6550 
6551 static bool rtl8152_in_nway(struct r8152 *tp)
6552 {
6553 	u16 nway_state;
6554 
6555 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, 0x2000);
6556 	tp->ocp_base = 0x2000;
6557 	ocp_write_byte(tp, MCU_TYPE_PLA, 0xb014, 0x4c);		/* phy state */
6558 	nway_state = ocp_read_word(tp, MCU_TYPE_PLA, 0xb01a);
6559 
6560 	/* bit 15: TXDIS_STATE, bit 14: ABD_STATE */
6561 	if (nway_state & 0xc000)
6562 		return false;
6563 	else
6564 		return true;
6565 }
6566 
6567 static bool rtl8153_in_nway(struct r8152 *tp)
6568 {
6569 	u16 phy_state = ocp_reg_read(tp, OCP_PHY_STATE) & 0xff;
6570 
6571 	if (phy_state == TXDIS_STATE || phy_state == ABD_STATE)
6572 		return false;
6573 	else
6574 		return true;
6575 }
6576 
6577 static void set_carrier(struct r8152 *tp)
6578 {
6579 	struct net_device *netdev = tp->netdev;
6580 	struct napi_struct *napi = &tp->napi;
6581 	u16 speed;
6582 
6583 	speed = rtl8152_get_speed(tp);
6584 
6585 	if (speed & LINK_STATUS) {
6586 		if (!netif_carrier_ok(netdev)) {
6587 			tp->rtl_ops.enable(tp);
6588 			netif_stop_queue(netdev);
6589 			napi_disable(napi);
6590 			netif_carrier_on(netdev);
6591 			rtl_start_rx(tp);
6592 			clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
6593 			_rtl8152_set_rx_mode(netdev);
6594 			napi_enable(napi);
6595 			netif_wake_queue(netdev);
6596 			netif_info(tp, link, netdev, "carrier on\n");
6597 		} else if (netif_queue_stopped(netdev) &&
6598 			   skb_queue_len(&tp->tx_queue) < tp->tx_qlen) {
6599 			netif_wake_queue(netdev);
6600 		}
6601 	} else {
6602 		if (netif_carrier_ok(netdev)) {
6603 			netif_carrier_off(netdev);
6604 			tasklet_disable(&tp->tx_tl);
6605 			napi_disable(napi);
6606 			tp->rtl_ops.disable(tp);
6607 			napi_enable(napi);
6608 			tasklet_enable(&tp->tx_tl);
6609 			netif_info(tp, link, netdev, "carrier off\n");
6610 		}
6611 	}
6612 }
6613 
6614 static void rtl_work_func_t(struct work_struct *work)
6615 {
6616 	struct r8152 *tp = container_of(work, struct r8152, schedule.work);
6617 
6618 	/* If the device is unplugged or !netif_running(), the workqueue
6619 	 * doesn't need to wake the device, and could return directly.
6620 	 */
6621 	if (test_bit(RTL8152_UNPLUG, &tp->flags) || !netif_running(tp->netdev))
6622 		return;
6623 
6624 	if (usb_autopm_get_interface(tp->intf) < 0)
6625 		return;
6626 
6627 	if (!test_bit(WORK_ENABLE, &tp->flags))
6628 		goto out1;
6629 
6630 	if (!mutex_trylock(&tp->control)) {
6631 		schedule_delayed_work(&tp->schedule, 0);
6632 		goto out1;
6633 	}
6634 
6635 	if (test_and_clear_bit(RTL8152_LINK_CHG, &tp->flags))
6636 		set_carrier(tp);
6637 
6638 	if (test_and_clear_bit(RTL8152_SET_RX_MODE, &tp->flags))
6639 		_rtl8152_set_rx_mode(tp->netdev);
6640 
6641 	/* don't schedule tasket before linking */
6642 	if (test_and_clear_bit(SCHEDULE_TASKLET, &tp->flags) &&
6643 	    netif_carrier_ok(tp->netdev))
6644 		tasklet_schedule(&tp->tx_tl);
6645 
6646 	mutex_unlock(&tp->control);
6647 
6648 out1:
6649 	usb_autopm_put_interface(tp->intf);
6650 }
6651 
6652 static void rtl_hw_phy_work_func_t(struct work_struct *work)
6653 {
6654 	struct r8152 *tp = container_of(work, struct r8152, hw_phy_work.work);
6655 
6656 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
6657 		return;
6658 
6659 	if (usb_autopm_get_interface(tp->intf) < 0)
6660 		return;
6661 
6662 	mutex_lock(&tp->control);
6663 
6664 	if (rtl8152_request_firmware(tp) == -ENODEV && tp->rtl_fw.retry) {
6665 		tp->rtl_fw.retry = false;
6666 		tp->rtl_fw.fw = NULL;
6667 
6668 		/* Delay execution in case request_firmware() is not ready yet.
6669 		 */
6670 		queue_delayed_work(system_long_wq, &tp->hw_phy_work, HZ * 10);
6671 		goto ignore_once;
6672 	}
6673 
6674 	tp->rtl_ops.hw_phy_cfg(tp);
6675 
6676 	rtl8152_set_speed(tp, tp->autoneg, tp->speed, tp->duplex,
6677 			  tp->advertising);
6678 
6679 ignore_once:
6680 	mutex_unlock(&tp->control);
6681 
6682 	usb_autopm_put_interface(tp->intf);
6683 }
6684 
6685 #ifdef CONFIG_PM_SLEEP
6686 static int rtl_notifier(struct notifier_block *nb, unsigned long action,
6687 			void *data)
6688 {
6689 	struct r8152 *tp = container_of(nb, struct r8152, pm_notifier);
6690 
6691 	switch (action) {
6692 	case PM_HIBERNATION_PREPARE:
6693 	case PM_SUSPEND_PREPARE:
6694 		usb_autopm_get_interface(tp->intf);
6695 		break;
6696 
6697 	case PM_POST_HIBERNATION:
6698 	case PM_POST_SUSPEND:
6699 		usb_autopm_put_interface(tp->intf);
6700 		break;
6701 
6702 	case PM_POST_RESTORE:
6703 	case PM_RESTORE_PREPARE:
6704 	default:
6705 		break;
6706 	}
6707 
6708 	return NOTIFY_DONE;
6709 }
6710 #endif
6711 
6712 static int rtl8152_open(struct net_device *netdev)
6713 {
6714 	struct r8152 *tp = netdev_priv(netdev);
6715 	int res = 0;
6716 
6717 	if (work_busy(&tp->hw_phy_work.work) & WORK_BUSY_PENDING) {
6718 		cancel_delayed_work_sync(&tp->hw_phy_work);
6719 		rtl_hw_phy_work_func_t(&tp->hw_phy_work.work);
6720 	}
6721 
6722 	res = alloc_all_mem(tp);
6723 	if (res)
6724 		goto out;
6725 
6726 	res = usb_autopm_get_interface(tp->intf);
6727 	if (res < 0)
6728 		goto out_free;
6729 
6730 	mutex_lock(&tp->control);
6731 
6732 	tp->rtl_ops.up(tp);
6733 
6734 	netif_carrier_off(netdev);
6735 	netif_start_queue(netdev);
6736 	set_bit(WORK_ENABLE, &tp->flags);
6737 
6738 	res = usb_submit_urb(tp->intr_urb, GFP_KERNEL);
6739 	if (res) {
6740 		if (res == -ENODEV)
6741 			netif_device_detach(tp->netdev);
6742 		netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n",
6743 			   res);
6744 		goto out_unlock;
6745 	}
6746 	napi_enable(&tp->napi);
6747 	tasklet_enable(&tp->tx_tl);
6748 
6749 	mutex_unlock(&tp->control);
6750 
6751 	usb_autopm_put_interface(tp->intf);
6752 #ifdef CONFIG_PM_SLEEP
6753 	tp->pm_notifier.notifier_call = rtl_notifier;
6754 	register_pm_notifier(&tp->pm_notifier);
6755 #endif
6756 	return 0;
6757 
6758 out_unlock:
6759 	mutex_unlock(&tp->control);
6760 	usb_autopm_put_interface(tp->intf);
6761 out_free:
6762 	free_all_mem(tp);
6763 out:
6764 	return res;
6765 }
6766 
6767 static int rtl8152_close(struct net_device *netdev)
6768 {
6769 	struct r8152 *tp = netdev_priv(netdev);
6770 	int res = 0;
6771 
6772 #ifdef CONFIG_PM_SLEEP
6773 	unregister_pm_notifier(&tp->pm_notifier);
6774 #endif
6775 	tasklet_disable(&tp->tx_tl);
6776 	clear_bit(WORK_ENABLE, &tp->flags);
6777 	usb_kill_urb(tp->intr_urb);
6778 	cancel_delayed_work_sync(&tp->schedule);
6779 	napi_disable(&tp->napi);
6780 	netif_stop_queue(netdev);
6781 
6782 	res = usb_autopm_get_interface(tp->intf);
6783 	if (res < 0 || test_bit(RTL8152_UNPLUG, &tp->flags)) {
6784 		rtl_drop_queued_tx(tp);
6785 		rtl_stop_rx(tp);
6786 	} else {
6787 		mutex_lock(&tp->control);
6788 
6789 		tp->rtl_ops.down(tp);
6790 
6791 		mutex_unlock(&tp->control);
6792 	}
6793 
6794 	if (!res)
6795 		usb_autopm_put_interface(tp->intf);
6796 
6797 	free_all_mem(tp);
6798 
6799 	return res;
6800 }
6801 
6802 static void rtl_tally_reset(struct r8152 *tp)
6803 {
6804 	u32 ocp_data;
6805 
6806 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY);
6807 	ocp_data |= TALLY_RESET;
6808 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY, ocp_data);
6809 }
6810 
6811 static void r8152b_init(struct r8152 *tp)
6812 {
6813 	u32 ocp_data;
6814 	u16 data;
6815 
6816 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
6817 		return;
6818 
6819 	data = r8152_mdio_read(tp, MII_BMCR);
6820 	if (data & BMCR_PDOWN) {
6821 		data &= ~BMCR_PDOWN;
6822 		r8152_mdio_write(tp, MII_BMCR, data);
6823 	}
6824 
6825 	r8152_aldps_en(tp, false);
6826 
6827 	if (tp->version == RTL_VER_01) {
6828 		ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
6829 		ocp_data &= ~LED_MODE_MASK;
6830 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
6831 	}
6832 
6833 	r8152_power_cut_en(tp, false);
6834 
6835 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
6836 	ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
6837 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
6838 	ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
6839 	ocp_data &= ~MCU_CLK_RATIO_MASK;
6840 	ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
6841 	ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
6842 	ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
6843 		   SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
6844 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
6845 
6846 	rtl_tally_reset(tp);
6847 
6848 	/* enable rx aggregation */
6849 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
6850 	ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
6851 	ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
6852 }
6853 
6854 static void r8153_init(struct r8152 *tp)
6855 {
6856 	u32 ocp_data;
6857 	u16 data;
6858 	int i;
6859 
6860 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
6861 		return;
6862 
6863 	r8153_u1u2en(tp, false);
6864 
6865 	for (i = 0; i < 500; i++) {
6866 		if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
6867 		    AUTOLOAD_DONE)
6868 			break;
6869 
6870 		msleep(20);
6871 		if (test_bit(RTL8152_UNPLUG, &tp->flags))
6872 			break;
6873 	}
6874 
6875 	data = r8153_phy_status(tp, 0);
6876 
6877 	if (tp->version == RTL_VER_03 || tp->version == RTL_VER_04 ||
6878 	    tp->version == RTL_VER_05)
6879 		ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L);
6880 
6881 	data = r8152_mdio_read(tp, MII_BMCR);
6882 	if (data & BMCR_PDOWN) {
6883 		data &= ~BMCR_PDOWN;
6884 		r8152_mdio_write(tp, MII_BMCR, data);
6885 	}
6886 
6887 	data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
6888 
6889 	r8153_u2p3en(tp, false);
6890 
6891 	if (tp->version == RTL_VER_04) {
6892 		ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2);
6893 		ocp_data &= ~pwd_dn_scale_mask;
6894 		ocp_data |= pwd_dn_scale(96);
6895 		ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2, ocp_data);
6896 
6897 		ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
6898 		ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
6899 		ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
6900 	} else if (tp->version == RTL_VER_05) {
6901 		ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0);
6902 		ocp_data &= ~ECM_ALDPS;
6903 		ocp_write_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0, ocp_data);
6904 
6905 		ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
6906 		if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
6907 			ocp_data &= ~DYNAMIC_BURST;
6908 		else
6909 			ocp_data |= DYNAMIC_BURST;
6910 		ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
6911 	} else if (tp->version == RTL_VER_06) {
6912 		ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
6913 		if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
6914 			ocp_data &= ~DYNAMIC_BURST;
6915 		else
6916 			ocp_data |= DYNAMIC_BURST;
6917 		ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
6918 
6919 		r8153_queue_wake(tp, false);
6920 
6921 		ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
6922 		if (rtl8152_get_speed(tp) & LINK_STATUS)
6923 			ocp_data |= CUR_LINK_OK;
6924 		else
6925 			ocp_data &= ~CUR_LINK_OK;
6926 		ocp_data |= POLL_LINK_CHG;
6927 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
6928 	}
6929 
6930 	ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2);
6931 	ocp_data |= EP4_FULL_FC;
6932 	ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2, ocp_data);
6933 
6934 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL);
6935 	ocp_data &= ~TIMER11_EN;
6936 	ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data);
6937 
6938 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
6939 	ocp_data &= ~LED_MODE_MASK;
6940 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
6941 
6942 	ocp_data = FIFO_EMPTY_1FB | ROK_EXIT_LPM;
6943 	if (tp->version == RTL_VER_04 && tp->udev->speed < USB_SPEED_SUPER)
6944 		ocp_data |= LPM_TIMER_500MS;
6945 	else
6946 		ocp_data |= LPM_TIMER_500US;
6947 	ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data);
6948 
6949 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2);
6950 	ocp_data &= ~SEN_VAL_MASK;
6951 	ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE;
6952 	ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data);
6953 
6954 	ocp_write_word(tp, MCU_TYPE_USB, USB_CONNECT_TIMER, 0x0001);
6955 
6956 	r8153_power_cut_en(tp, false);
6957 	rtl_runtime_suspend_enable(tp, false);
6958 	r8153_mac_clk_speed_down(tp, false);
6959 	r8153_u1u2en(tp, true);
6960 	usb_enable_lpm(tp->udev);
6961 
6962 	ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6);
6963 	ocp_data |= LANWAKE_CLR_EN;
6964 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG6, ocp_data);
6965 
6966 	ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG);
6967 	ocp_data &= ~LANWAKE_PIN;
6968 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_LWAKE_CTRL_REG, ocp_data);
6969 
6970 	/* rx aggregation */
6971 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
6972 	ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
6973 	if (tp->dell_tb_rx_agg_bug)
6974 		ocp_data |= RX_AGG_DISABLE;
6975 
6976 	ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
6977 
6978 	rtl_tally_reset(tp);
6979 
6980 	switch (tp->udev->speed) {
6981 	case USB_SPEED_SUPER:
6982 	case USB_SPEED_SUPER_PLUS:
6983 		tp->coalesce = COALESCE_SUPER;
6984 		break;
6985 	case USB_SPEED_HIGH:
6986 		tp->coalesce = COALESCE_HIGH;
6987 		break;
6988 	default:
6989 		tp->coalesce = COALESCE_SLOW;
6990 		break;
6991 	}
6992 }
6993 
6994 static void r8153b_init(struct r8152 *tp)
6995 {
6996 	u32 ocp_data;
6997 	u16 data;
6998 	int i;
6999 
7000 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
7001 		return;
7002 
7003 	r8153b_u1u2en(tp, false);
7004 
7005 	for (i = 0; i < 500; i++) {
7006 		if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
7007 		    AUTOLOAD_DONE)
7008 			break;
7009 
7010 		msleep(20);
7011 		if (test_bit(RTL8152_UNPLUG, &tp->flags))
7012 			break;
7013 	}
7014 
7015 	data = r8153_phy_status(tp, 0);
7016 
7017 	data = r8152_mdio_read(tp, MII_BMCR);
7018 	if (data & BMCR_PDOWN) {
7019 		data &= ~BMCR_PDOWN;
7020 		r8152_mdio_write(tp, MII_BMCR, data);
7021 	}
7022 
7023 	data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
7024 
7025 	r8153_u2p3en(tp, false);
7026 
7027 	/* MSC timer = 0xfff * 8ms = 32760 ms */
7028 	ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff);
7029 
7030 	r8153b_power_cut_en(tp, false);
7031 	r8153b_ups_en(tp, false);
7032 	r8153_queue_wake(tp, false);
7033 	rtl_runtime_suspend_enable(tp, false);
7034 
7035 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
7036 	if (rtl8152_get_speed(tp) & LINK_STATUS)
7037 		ocp_data |= CUR_LINK_OK;
7038 	else
7039 		ocp_data &= ~CUR_LINK_OK;
7040 	ocp_data |= POLL_LINK_CHG;
7041 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
7042 
7043 	if (tp->udev->speed >= USB_SPEED_SUPER)
7044 		r8153b_u1u2en(tp, true);
7045 
7046 	usb_enable_lpm(tp->udev);
7047 
7048 	/* MAC clock speed down */
7049 	r8153_mac_clk_speed_down(tp, true);
7050 
7051 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
7052 	ocp_data &= ~PLA_MCU_SPDWN_EN;
7053 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
7054 
7055 	if (tp->version == RTL_VER_09) {
7056 		/* Disable Test IO for 32QFN */
7057 		if (ocp_read_byte(tp, MCU_TYPE_PLA, 0xdc00) & BIT(5)) {
7058 			ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
7059 			ocp_data |= TEST_IO_OFF;
7060 			ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
7061 		}
7062 	}
7063 
7064 	set_bit(GREEN_ETHERNET, &tp->flags);
7065 
7066 	/* rx aggregation */
7067 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
7068 	ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
7069 	ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
7070 
7071 	rtl_tally_reset(tp);
7072 
7073 	tp->coalesce = 15000;	/* 15 us */
7074 }
7075 
7076 static void r8153c_init(struct r8152 *tp)
7077 {
7078 	u32 ocp_data;
7079 	u16 data;
7080 	int i;
7081 
7082 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
7083 		return;
7084 
7085 	r8153b_u1u2en(tp, false);
7086 
7087 	/* Disable spi_en */
7088 	ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
7089 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
7090 	ocp_data &= ~BIT(3);
7091 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data);
7092 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, 0xcbf0);
7093 	ocp_data |= BIT(1);
7094 	ocp_write_word(tp, MCU_TYPE_USB, 0xcbf0, ocp_data);
7095 
7096 	for (i = 0; i < 500; i++) {
7097 		if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
7098 		    AUTOLOAD_DONE)
7099 			break;
7100 
7101 		msleep(20);
7102 		if (test_bit(RTL8152_UNPLUG, &tp->flags))
7103 			return;
7104 	}
7105 
7106 	data = r8153_phy_status(tp, 0);
7107 
7108 	data = r8152_mdio_read(tp, MII_BMCR);
7109 	if (data & BMCR_PDOWN) {
7110 		data &= ~BMCR_PDOWN;
7111 		r8152_mdio_write(tp, MII_BMCR, data);
7112 	}
7113 
7114 	data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
7115 
7116 	r8153_u2p3en(tp, false);
7117 
7118 	/* MSC timer = 0xfff * 8ms = 32760 ms */
7119 	ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff);
7120 
7121 	r8153b_power_cut_en(tp, false);
7122 	r8153c_ups_en(tp, false);
7123 	r8153_queue_wake(tp, false);
7124 	rtl_runtime_suspend_enable(tp, false);
7125 
7126 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
7127 	if (rtl8152_get_speed(tp) & LINK_STATUS)
7128 		ocp_data |= CUR_LINK_OK;
7129 	else
7130 		ocp_data &= ~CUR_LINK_OK;
7131 
7132 	ocp_data |= POLL_LINK_CHG;
7133 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
7134 
7135 	r8153b_u1u2en(tp, true);
7136 
7137 	usb_enable_lpm(tp->udev);
7138 
7139 	/* MAC clock speed down */
7140 	r8153_mac_clk_speed_down(tp, true);
7141 
7142 	ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_MISC_2);
7143 	ocp_data &= ~BIT(7);
7144 	ocp_write_byte(tp, MCU_TYPE_USB, USB_MISC_2, ocp_data);
7145 
7146 	set_bit(GREEN_ETHERNET, &tp->flags);
7147 
7148 	/* rx aggregation */
7149 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
7150 	ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
7151 	ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
7152 
7153 	rtl_tally_reset(tp);
7154 
7155 	tp->coalesce = 15000;	/* 15 us */
7156 }
7157 
7158 static void r8156_hw_phy_cfg(struct r8152 *tp)
7159 {
7160 	u32 ocp_data;
7161 	u16 data;
7162 
7163 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
7164 	if (ocp_data & PCUT_STATUS) {
7165 		ocp_data &= ~PCUT_STATUS;
7166 		ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
7167 	}
7168 
7169 	data = r8153_phy_status(tp, 0);
7170 	switch (data) {
7171 	case PHY_STAT_EXT_INIT:
7172 		rtl8152_apply_firmware(tp, true);
7173 
7174 		data = ocp_reg_read(tp, 0xa468);
7175 		data &= ~(BIT(3) | BIT(1));
7176 		ocp_reg_write(tp, 0xa468, data);
7177 		break;
7178 	case PHY_STAT_LAN_ON:
7179 	case PHY_STAT_PWRDN:
7180 	default:
7181 		rtl8152_apply_firmware(tp, false);
7182 		break;
7183 	}
7184 
7185 	/* disable ALDPS before updating the PHY parameters */
7186 	r8153_aldps_en(tp, false);
7187 
7188 	/* disable EEE before updating the PHY parameters */
7189 	rtl_eee_enable(tp, false);
7190 
7191 	data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
7192 	WARN_ON_ONCE(data != PHY_STAT_LAN_ON);
7193 
7194 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
7195 	ocp_data |= PFM_PWM_SWITCH;
7196 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
7197 
7198 	switch (tp->version) {
7199 	case RTL_VER_10:
7200 		data = ocp_reg_read(tp, 0xad40);
7201 		data &= ~0x3ff;
7202 		data |= BIT(7) | BIT(2);
7203 		ocp_reg_write(tp, 0xad40, data);
7204 
7205 		data = ocp_reg_read(tp, 0xad4e);
7206 		data |= BIT(4);
7207 		ocp_reg_write(tp, 0xad4e, data);
7208 		data = ocp_reg_read(tp, 0xad16);
7209 		data &= ~0x3ff;
7210 		data |= 0x6;
7211 		ocp_reg_write(tp, 0xad16, data);
7212 		data = ocp_reg_read(tp, 0xad32);
7213 		data &= ~0x3f;
7214 		data |= 6;
7215 		ocp_reg_write(tp, 0xad32, data);
7216 		data = ocp_reg_read(tp, 0xac08);
7217 		data &= ~(BIT(12) | BIT(8));
7218 		ocp_reg_write(tp, 0xac08, data);
7219 		data = ocp_reg_read(tp, 0xac8a);
7220 		data |= BIT(12) | BIT(13) | BIT(14);
7221 		data &= ~BIT(15);
7222 		ocp_reg_write(tp, 0xac8a, data);
7223 		data = ocp_reg_read(tp, 0xad18);
7224 		data |= BIT(10);
7225 		ocp_reg_write(tp, 0xad18, data);
7226 		data = ocp_reg_read(tp, 0xad1a);
7227 		data |= 0x3ff;
7228 		ocp_reg_write(tp, 0xad1a, data);
7229 		data = ocp_reg_read(tp, 0xad1c);
7230 		data |= 0x3ff;
7231 		ocp_reg_write(tp, 0xad1c, data);
7232 
7233 		data = sram_read(tp, 0x80ea);
7234 		data &= ~0xff00;
7235 		data |= 0xc400;
7236 		sram_write(tp, 0x80ea, data);
7237 		data = sram_read(tp, 0x80eb);
7238 		data &= ~0x0700;
7239 		data |= 0x0300;
7240 		sram_write(tp, 0x80eb, data);
7241 		data = sram_read(tp, 0x80f8);
7242 		data &= ~0xff00;
7243 		data |= 0x1c00;
7244 		sram_write(tp, 0x80f8, data);
7245 		data = sram_read(tp, 0x80f1);
7246 		data &= ~0xff00;
7247 		data |= 0x3000;
7248 		sram_write(tp, 0x80f1, data);
7249 
7250 		data = sram_read(tp, 0x80fe);
7251 		data &= ~0xff00;
7252 		data |= 0xa500;
7253 		sram_write(tp, 0x80fe, data);
7254 		data = sram_read(tp, 0x8102);
7255 		data &= ~0xff00;
7256 		data |= 0x5000;
7257 		sram_write(tp, 0x8102, data);
7258 		data = sram_read(tp, 0x8015);
7259 		data &= ~0xff00;
7260 		data |= 0x3300;
7261 		sram_write(tp, 0x8015, data);
7262 		data = sram_read(tp, 0x8100);
7263 		data &= ~0xff00;
7264 		data |= 0x7000;
7265 		sram_write(tp, 0x8100, data);
7266 		data = sram_read(tp, 0x8014);
7267 		data &= ~0xff00;
7268 		data |= 0xf000;
7269 		sram_write(tp, 0x8014, data);
7270 		data = sram_read(tp, 0x8016);
7271 		data &= ~0xff00;
7272 		data |= 0x6500;
7273 		sram_write(tp, 0x8016, data);
7274 		data = sram_read(tp, 0x80dc);
7275 		data &= ~0xff00;
7276 		data |= 0xed00;
7277 		sram_write(tp, 0x80dc, data);
7278 		data = sram_read(tp, 0x80df);
7279 		data |= BIT(8);
7280 		sram_write(tp, 0x80df, data);
7281 		data = sram_read(tp, 0x80e1);
7282 		data &= ~BIT(8);
7283 		sram_write(tp, 0x80e1, data);
7284 
7285 		data = ocp_reg_read(tp, 0xbf06);
7286 		data &= ~0x003f;
7287 		data |= 0x0038;
7288 		ocp_reg_write(tp, 0xbf06, data);
7289 
7290 		sram_write(tp, 0x819f, 0xddb6);
7291 
7292 		ocp_reg_write(tp, 0xbc34, 0x5555);
7293 		data = ocp_reg_read(tp, 0xbf0a);
7294 		data &= ~0x0e00;
7295 		data |= 0x0a00;
7296 		ocp_reg_write(tp, 0xbf0a, data);
7297 
7298 		data = ocp_reg_read(tp, 0xbd2c);
7299 		data &= ~BIT(13);
7300 		ocp_reg_write(tp, 0xbd2c, data);
7301 		break;
7302 	case RTL_VER_11:
7303 		data = ocp_reg_read(tp, 0xad16);
7304 		data |= 0x3ff;
7305 		ocp_reg_write(tp, 0xad16, data);
7306 		data = ocp_reg_read(tp, 0xad32);
7307 		data &= ~0x3f;
7308 		data |= 6;
7309 		ocp_reg_write(tp, 0xad32, data);
7310 		data = ocp_reg_read(tp, 0xac08);
7311 		data &= ~(BIT(12) | BIT(8));
7312 		ocp_reg_write(tp, 0xac08, data);
7313 		data = ocp_reg_read(tp, 0xacc0);
7314 		data &= ~0x3;
7315 		data |= BIT(1);
7316 		ocp_reg_write(tp, 0xacc0, data);
7317 		data = ocp_reg_read(tp, 0xad40);
7318 		data &= ~0xe7;
7319 		data |= BIT(6) | BIT(2);
7320 		ocp_reg_write(tp, 0xad40, data);
7321 		data = ocp_reg_read(tp, 0xac14);
7322 		data &= ~BIT(7);
7323 		ocp_reg_write(tp, 0xac14, data);
7324 		data = ocp_reg_read(tp, 0xac80);
7325 		data &= ~(BIT(8) | BIT(9));
7326 		ocp_reg_write(tp, 0xac80, data);
7327 		data = ocp_reg_read(tp, 0xac5e);
7328 		data &= ~0x7;
7329 		data |= BIT(1);
7330 		ocp_reg_write(tp, 0xac5e, data);
7331 		ocp_reg_write(tp, 0xad4c, 0x00a8);
7332 		ocp_reg_write(tp, 0xac5c, 0x01ff);
7333 		data = ocp_reg_read(tp, 0xac8a);
7334 		data &= ~0xf0;
7335 		data |= BIT(4) | BIT(5);
7336 		ocp_reg_write(tp, 0xac8a, data);
7337 		ocp_reg_write(tp, 0xb87c, 0x8157);
7338 		data = ocp_reg_read(tp, 0xb87e);
7339 		data &= ~0xff00;
7340 		data |= 0x0500;
7341 		ocp_reg_write(tp, 0xb87e, data);
7342 		ocp_reg_write(tp, 0xb87c, 0x8159);
7343 		data = ocp_reg_read(tp, 0xb87e);
7344 		data &= ~0xff00;
7345 		data |= 0x0700;
7346 		ocp_reg_write(tp, 0xb87e, data);
7347 
7348 		/* AAGC */
7349 		ocp_reg_write(tp, 0xb87c, 0x80a2);
7350 		ocp_reg_write(tp, 0xb87e, 0x0153);
7351 		ocp_reg_write(tp, 0xb87c, 0x809c);
7352 		ocp_reg_write(tp, 0xb87e, 0x0153);
7353 
7354 		/* EEE parameter */
7355 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_TXTWSYS_2P5G, 0x0056);
7356 
7357 		ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_USB_CFG);
7358 		ocp_data |= EN_XG_LIP | EN_G_LIP;
7359 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_USB_CFG, ocp_data);
7360 
7361 		sram_write(tp, 0x8257, 0x020f); /*  XG PLL */
7362 		sram_write(tp, 0x80ea, 0x7843); /* GIGA Master */
7363 
7364 		if (rtl_phy_patch_request(tp, true, true))
7365 			return;
7366 
7367 		/* Advance EEE */
7368 		ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4);
7369 		ocp_data |= EEE_SPDWN_EN;
7370 		ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data);
7371 
7372 		data = ocp_reg_read(tp, OCP_DOWN_SPEED);
7373 		data &= ~(EN_EEE_100 | EN_EEE_1000);
7374 		data |= EN_10M_CLKDIV;
7375 		ocp_reg_write(tp, OCP_DOWN_SPEED, data);
7376 		tp->ups_info._10m_ckdiv = true;
7377 		tp->ups_info.eee_plloff_100 = false;
7378 		tp->ups_info.eee_plloff_giga = false;
7379 
7380 		data = ocp_reg_read(tp, OCP_POWER_CFG);
7381 		data &= ~EEE_CLKDIV_EN;
7382 		ocp_reg_write(tp, OCP_POWER_CFG, data);
7383 		tp->ups_info.eee_ckdiv = false;
7384 
7385 		ocp_reg_write(tp, OCP_SYSCLK_CFG, 0);
7386 		ocp_reg_write(tp, OCP_SYSCLK_CFG, sysclk_div_expo(5));
7387 		tp->ups_info._250m_ckdiv = false;
7388 
7389 		rtl_phy_patch_request(tp, false, true);
7390 
7391 		/* enable ADC Ibias Cal */
7392 		data = ocp_reg_read(tp, 0xd068);
7393 		data |= BIT(13);
7394 		ocp_reg_write(tp, 0xd068, data);
7395 
7396 		/* enable Thermal Sensor */
7397 		data = sram_read(tp, 0x81a2);
7398 		data &= ~BIT(8);
7399 		sram_write(tp, 0x81a2, data);
7400 		data = ocp_reg_read(tp, 0xb54c);
7401 		data &= ~0xff00;
7402 		data |= 0xdb00;
7403 		ocp_reg_write(tp, 0xb54c, data);
7404 
7405 		/* Nway 2.5G Lite */
7406 		data = ocp_reg_read(tp, 0xa454);
7407 		data &= ~BIT(0);
7408 		ocp_reg_write(tp, 0xa454, data);
7409 
7410 		/* CS DSP solution */
7411 		data = ocp_reg_read(tp, OCP_10GBT_CTRL);
7412 		data |= RTL_ADV2_5G_F_R;
7413 		ocp_reg_write(tp, OCP_10GBT_CTRL, data);
7414 		data = ocp_reg_read(tp, 0xad4e);
7415 		data &= ~BIT(4);
7416 		ocp_reg_write(tp, 0xad4e, data);
7417 		data = ocp_reg_read(tp, 0xa86a);
7418 		data &= ~BIT(0);
7419 		ocp_reg_write(tp, 0xa86a, data);
7420 
7421 		/* MDI SWAP */
7422 		if ((ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CFG) & MID_REVERSE) &&
7423 		    (ocp_reg_read(tp, 0xd068) & BIT(1))) {
7424 			u16 swap_a, swap_b;
7425 
7426 			data = ocp_reg_read(tp, 0xd068);
7427 			data &= ~0x1f;
7428 			data |= 0x1; /* p0 */
7429 			ocp_reg_write(tp, 0xd068, data);
7430 			swap_a = ocp_reg_read(tp, 0xd06a);
7431 			data &= ~0x18;
7432 			data |= 0x18; /* p3 */
7433 			ocp_reg_write(tp, 0xd068, data);
7434 			swap_b = ocp_reg_read(tp, 0xd06a);
7435 			data &= ~0x18; /* p0 */
7436 			ocp_reg_write(tp, 0xd068, data);
7437 			ocp_reg_write(tp, 0xd06a,
7438 				      (swap_a & ~0x7ff) | (swap_b & 0x7ff));
7439 			data |= 0x18; /* p3 */
7440 			ocp_reg_write(tp, 0xd068, data);
7441 			ocp_reg_write(tp, 0xd06a,
7442 				      (swap_b & ~0x7ff) | (swap_a & 0x7ff));
7443 			data &= ~0x18;
7444 			data |= 0x08; /* p1 */
7445 			ocp_reg_write(tp, 0xd068, data);
7446 			swap_a = ocp_reg_read(tp, 0xd06a);
7447 			data &= ~0x18;
7448 			data |= 0x10; /* p2 */
7449 			ocp_reg_write(tp, 0xd068, data);
7450 			swap_b = ocp_reg_read(tp, 0xd06a);
7451 			data &= ~0x18;
7452 			data |= 0x08; /* p1 */
7453 			ocp_reg_write(tp, 0xd068, data);
7454 			ocp_reg_write(tp, 0xd06a,
7455 				      (swap_a & ~0x7ff) | (swap_b & 0x7ff));
7456 			data &= ~0x18;
7457 			data |= 0x10; /* p2 */
7458 			ocp_reg_write(tp, 0xd068, data);
7459 			ocp_reg_write(tp, 0xd06a,
7460 				      (swap_b & ~0x7ff) | (swap_a & 0x7ff));
7461 			swap_a = ocp_reg_read(tp, 0xbd5a);
7462 			swap_b = ocp_reg_read(tp, 0xbd5c);
7463 			ocp_reg_write(tp, 0xbd5a, (swap_a & ~0x1f1f) |
7464 				      ((swap_b & 0x1f) << 8) |
7465 				      ((swap_b >> 8) & 0x1f));
7466 			ocp_reg_write(tp, 0xbd5c, (swap_b & ~0x1f1f) |
7467 				      ((swap_a & 0x1f) << 8) |
7468 				      ((swap_a >> 8) & 0x1f));
7469 			swap_a = ocp_reg_read(tp, 0xbc18);
7470 			swap_b = ocp_reg_read(tp, 0xbc1a);
7471 			ocp_reg_write(tp, 0xbc18, (swap_a & ~0x1f1f) |
7472 				      ((swap_b & 0x1f) << 8) |
7473 				      ((swap_b >> 8) & 0x1f));
7474 			ocp_reg_write(tp, 0xbc1a, (swap_b & ~0x1f1f) |
7475 				      ((swap_a & 0x1f) << 8) |
7476 				      ((swap_a >> 8) & 0x1f));
7477 		}
7478 		break;
7479 	default:
7480 		break;
7481 	}
7482 
7483 	rtl_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags));
7484 
7485 	data = ocp_reg_read(tp, 0xa428);
7486 	data &= ~BIT(9);
7487 	ocp_reg_write(tp, 0xa428, data);
7488 	data = ocp_reg_read(tp, 0xa5ea);
7489 	data &= ~BIT(0);
7490 	ocp_reg_write(tp, 0xa5ea, data);
7491 	tp->ups_info.lite_mode = 0;
7492 
7493 	if (tp->eee_en)
7494 		rtl_eee_enable(tp, true);
7495 
7496 	r8153_aldps_en(tp, true);
7497 	r8152b_enable_fc(tp);
7498 	r8153_u2p3en(tp, true);
7499 
7500 	set_bit(PHY_RESET, &tp->flags);
7501 }
7502 
7503 static void r8156b_hw_phy_cfg(struct r8152 *tp)
7504 {
7505 	u32 ocp_data;
7506 	u16 data;
7507 
7508 	switch (tp->version) {
7509 	case RTL_VER_12:
7510 		ocp_reg_write(tp, 0xbf86, 0x9000);
7511 		data = ocp_reg_read(tp, 0xc402);
7512 		data |= BIT(10);
7513 		ocp_reg_write(tp, 0xc402, data);
7514 		data &= ~BIT(10);
7515 		ocp_reg_write(tp, 0xc402, data);
7516 		ocp_reg_write(tp, 0xbd86, 0x1010);
7517 		ocp_reg_write(tp, 0xbd88, 0x1010);
7518 		data = ocp_reg_read(tp, 0xbd4e);
7519 		data &= ~(BIT(10) | BIT(11));
7520 		data |= BIT(11);
7521 		ocp_reg_write(tp, 0xbd4e, data);
7522 		data = ocp_reg_read(tp, 0xbf46);
7523 		data &= ~0xf00;
7524 		data |= 0x700;
7525 		ocp_reg_write(tp, 0xbf46, data);
7526 		break;
7527 	case RTL_VER_13:
7528 	case RTL_VER_15:
7529 		r8156b_wait_loading_flash(tp);
7530 		break;
7531 	default:
7532 		break;
7533 	}
7534 
7535 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
7536 	if (ocp_data & PCUT_STATUS) {
7537 		ocp_data &= ~PCUT_STATUS;
7538 		ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
7539 	}
7540 
7541 	data = r8153_phy_status(tp, 0);
7542 	switch (data) {
7543 	case PHY_STAT_EXT_INIT:
7544 		rtl8152_apply_firmware(tp, true);
7545 
7546 		data = ocp_reg_read(tp, 0xa466);
7547 		data &= ~BIT(0);
7548 		ocp_reg_write(tp, 0xa466, data);
7549 
7550 		data = ocp_reg_read(tp, 0xa468);
7551 		data &= ~(BIT(3) | BIT(1));
7552 		ocp_reg_write(tp, 0xa468, data);
7553 		break;
7554 	case PHY_STAT_LAN_ON:
7555 	case PHY_STAT_PWRDN:
7556 	default:
7557 		rtl8152_apply_firmware(tp, false);
7558 		break;
7559 	}
7560 
7561 	data = r8152_mdio_read(tp, MII_BMCR);
7562 	if (data & BMCR_PDOWN) {
7563 		data &= ~BMCR_PDOWN;
7564 		r8152_mdio_write(tp, MII_BMCR, data);
7565 	}
7566 
7567 	/* disable ALDPS before updating the PHY parameters */
7568 	r8153_aldps_en(tp, false);
7569 
7570 	/* disable EEE before updating the PHY parameters */
7571 	rtl_eee_enable(tp, false);
7572 
7573 	data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
7574 	WARN_ON_ONCE(data != PHY_STAT_LAN_ON);
7575 
7576 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
7577 	ocp_data |= PFM_PWM_SWITCH;
7578 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
7579 
7580 	switch (tp->version) {
7581 	case RTL_VER_12:
7582 		data = ocp_reg_read(tp, 0xbc08);
7583 		data |= BIT(3) | BIT(2);
7584 		ocp_reg_write(tp, 0xbc08, data);
7585 
7586 		data = sram_read(tp, 0x8fff);
7587 		data &= ~0xff00;
7588 		data |= 0x0400;
7589 		sram_write(tp, 0x8fff, data);
7590 
7591 		data = ocp_reg_read(tp, 0xacda);
7592 		data |= 0xff00;
7593 		ocp_reg_write(tp, 0xacda, data);
7594 		data = ocp_reg_read(tp, 0xacde);
7595 		data |= 0xf000;
7596 		ocp_reg_write(tp, 0xacde, data);
7597 		ocp_reg_write(tp, 0xac8c, 0x0ffc);
7598 		ocp_reg_write(tp, 0xac46, 0xb7b4);
7599 		ocp_reg_write(tp, 0xac50, 0x0fbc);
7600 		ocp_reg_write(tp, 0xac3c, 0x9240);
7601 		ocp_reg_write(tp, 0xac4e, 0x0db4);
7602 		ocp_reg_write(tp, 0xacc6, 0x0707);
7603 		ocp_reg_write(tp, 0xacc8, 0xa0d3);
7604 		ocp_reg_write(tp, 0xad08, 0x0007);
7605 
7606 		ocp_reg_write(tp, 0xb87c, 0x8560);
7607 		ocp_reg_write(tp, 0xb87e, 0x19cc);
7608 		ocp_reg_write(tp, 0xb87c, 0x8562);
7609 		ocp_reg_write(tp, 0xb87e, 0x19cc);
7610 		ocp_reg_write(tp, 0xb87c, 0x8564);
7611 		ocp_reg_write(tp, 0xb87e, 0x19cc);
7612 		ocp_reg_write(tp, 0xb87c, 0x8566);
7613 		ocp_reg_write(tp, 0xb87e, 0x147d);
7614 		ocp_reg_write(tp, 0xb87c, 0x8568);
7615 		ocp_reg_write(tp, 0xb87e, 0x147d);
7616 		ocp_reg_write(tp, 0xb87c, 0x856a);
7617 		ocp_reg_write(tp, 0xb87e, 0x147d);
7618 		ocp_reg_write(tp, 0xb87c, 0x8ffe);
7619 		ocp_reg_write(tp, 0xb87e, 0x0907);
7620 		ocp_reg_write(tp, 0xb87c, 0x80d6);
7621 		ocp_reg_write(tp, 0xb87e, 0x2801);
7622 		ocp_reg_write(tp, 0xb87c, 0x80f2);
7623 		ocp_reg_write(tp, 0xb87e, 0x2801);
7624 		ocp_reg_write(tp, 0xb87c, 0x80f4);
7625 		ocp_reg_write(tp, 0xb87e, 0x6077);
7626 		ocp_reg_write(tp, 0xb506, 0x01e7);
7627 
7628 		ocp_reg_write(tp, 0xb87c, 0x8013);
7629 		ocp_reg_write(tp, 0xb87e, 0x0700);
7630 		ocp_reg_write(tp, 0xb87c, 0x8fb9);
7631 		ocp_reg_write(tp, 0xb87e, 0x2801);
7632 		ocp_reg_write(tp, 0xb87c, 0x8fba);
7633 		ocp_reg_write(tp, 0xb87e, 0x0100);
7634 		ocp_reg_write(tp, 0xb87c, 0x8fbc);
7635 		ocp_reg_write(tp, 0xb87e, 0x1900);
7636 		ocp_reg_write(tp, 0xb87c, 0x8fbe);
7637 		ocp_reg_write(tp, 0xb87e, 0xe100);
7638 		ocp_reg_write(tp, 0xb87c, 0x8fc0);
7639 		ocp_reg_write(tp, 0xb87e, 0x0800);
7640 		ocp_reg_write(tp, 0xb87c, 0x8fc2);
7641 		ocp_reg_write(tp, 0xb87e, 0xe500);
7642 		ocp_reg_write(tp, 0xb87c, 0x8fc4);
7643 		ocp_reg_write(tp, 0xb87e, 0x0f00);
7644 		ocp_reg_write(tp, 0xb87c, 0x8fc6);
7645 		ocp_reg_write(tp, 0xb87e, 0xf100);
7646 		ocp_reg_write(tp, 0xb87c, 0x8fc8);
7647 		ocp_reg_write(tp, 0xb87e, 0x0400);
7648 		ocp_reg_write(tp, 0xb87c, 0x8fca);
7649 		ocp_reg_write(tp, 0xb87e, 0xf300);
7650 		ocp_reg_write(tp, 0xb87c, 0x8fcc);
7651 		ocp_reg_write(tp, 0xb87e, 0xfd00);
7652 		ocp_reg_write(tp, 0xb87c, 0x8fce);
7653 		ocp_reg_write(tp, 0xb87e, 0xff00);
7654 		ocp_reg_write(tp, 0xb87c, 0x8fd0);
7655 		ocp_reg_write(tp, 0xb87e, 0xfb00);
7656 		ocp_reg_write(tp, 0xb87c, 0x8fd2);
7657 		ocp_reg_write(tp, 0xb87e, 0x0100);
7658 		ocp_reg_write(tp, 0xb87c, 0x8fd4);
7659 		ocp_reg_write(tp, 0xb87e, 0xf400);
7660 		ocp_reg_write(tp, 0xb87c, 0x8fd6);
7661 		ocp_reg_write(tp, 0xb87e, 0xff00);
7662 		ocp_reg_write(tp, 0xb87c, 0x8fd8);
7663 		ocp_reg_write(tp, 0xb87e, 0xf600);
7664 
7665 		ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_USB_CFG);
7666 		ocp_data |= EN_XG_LIP | EN_G_LIP;
7667 		ocp_write_byte(tp, MCU_TYPE_PLA, PLA_USB_CFG, ocp_data);
7668 		ocp_reg_write(tp, 0xb87c, 0x813d);
7669 		ocp_reg_write(tp, 0xb87e, 0x390e);
7670 		ocp_reg_write(tp, 0xb87c, 0x814f);
7671 		ocp_reg_write(tp, 0xb87e, 0x790e);
7672 		ocp_reg_write(tp, 0xb87c, 0x80b0);
7673 		ocp_reg_write(tp, 0xb87e, 0x0f31);
7674 		data = ocp_reg_read(tp, 0xbf4c);
7675 		data |= BIT(1);
7676 		ocp_reg_write(tp, 0xbf4c, data);
7677 		data = ocp_reg_read(tp, 0xbcca);
7678 		data |= BIT(9) | BIT(8);
7679 		ocp_reg_write(tp, 0xbcca, data);
7680 		ocp_reg_write(tp, 0xb87c, 0x8141);
7681 		ocp_reg_write(tp, 0xb87e, 0x320e);
7682 		ocp_reg_write(tp, 0xb87c, 0x8153);
7683 		ocp_reg_write(tp, 0xb87e, 0x720e);
7684 		ocp_reg_write(tp, 0xb87c, 0x8529);
7685 		ocp_reg_write(tp, 0xb87e, 0x050e);
7686 		data = ocp_reg_read(tp, OCP_EEE_CFG);
7687 		data &= ~CTAP_SHORT_EN;
7688 		ocp_reg_write(tp, OCP_EEE_CFG, data);
7689 
7690 		sram_write(tp, 0x816c, 0xc4a0);
7691 		sram_write(tp, 0x8170, 0xc4a0);
7692 		sram_write(tp, 0x8174, 0x04a0);
7693 		sram_write(tp, 0x8178, 0x04a0);
7694 		sram_write(tp, 0x817c, 0x0719);
7695 		sram_write(tp, 0x8ff4, 0x0400);
7696 		sram_write(tp, 0x8ff1, 0x0404);
7697 
7698 		ocp_reg_write(tp, 0xbf4a, 0x001b);
7699 		ocp_reg_write(tp, 0xb87c, 0x8033);
7700 		ocp_reg_write(tp, 0xb87e, 0x7c13);
7701 		ocp_reg_write(tp, 0xb87c, 0x8037);
7702 		ocp_reg_write(tp, 0xb87e, 0x7c13);
7703 		ocp_reg_write(tp, 0xb87c, 0x803b);
7704 		ocp_reg_write(tp, 0xb87e, 0xfc32);
7705 		ocp_reg_write(tp, 0xb87c, 0x803f);
7706 		ocp_reg_write(tp, 0xb87e, 0x7c13);
7707 		ocp_reg_write(tp, 0xb87c, 0x8043);
7708 		ocp_reg_write(tp, 0xb87e, 0x7c13);
7709 		ocp_reg_write(tp, 0xb87c, 0x8047);
7710 		ocp_reg_write(tp, 0xb87e, 0x7c13);
7711 
7712 		ocp_reg_write(tp, 0xb87c, 0x8145);
7713 		ocp_reg_write(tp, 0xb87e, 0x370e);
7714 		ocp_reg_write(tp, 0xb87c, 0x8157);
7715 		ocp_reg_write(tp, 0xb87e, 0x770e);
7716 		ocp_reg_write(tp, 0xb87c, 0x8169);
7717 		ocp_reg_write(tp, 0xb87e, 0x0d0a);
7718 		ocp_reg_write(tp, 0xb87c, 0x817b);
7719 		ocp_reg_write(tp, 0xb87e, 0x1d0a);
7720 
7721 		data = sram_read(tp, 0x8217);
7722 		data &= ~0xff00;
7723 		data |= 0x5000;
7724 		sram_write(tp, 0x8217, data);
7725 		data = sram_read(tp, 0x821a);
7726 		data &= ~0xff00;
7727 		data |= 0x5000;
7728 		sram_write(tp, 0x821a, data);
7729 		sram_write(tp, 0x80da, 0x0403);
7730 		data = sram_read(tp, 0x80dc);
7731 		data &= ~0xff00;
7732 		data |= 0x1000;
7733 		sram_write(tp, 0x80dc, data);
7734 		sram_write(tp, 0x80b3, 0x0384);
7735 		sram_write(tp, 0x80b7, 0x2007);
7736 		data = sram_read(tp, 0x80ba);
7737 		data &= ~0xff00;
7738 		data |= 0x6c00;
7739 		sram_write(tp, 0x80ba, data);
7740 		sram_write(tp, 0x80b5, 0xf009);
7741 		data = sram_read(tp, 0x80bd);
7742 		data &= ~0xff00;
7743 		data |= 0x9f00;
7744 		sram_write(tp, 0x80bd, data);
7745 		sram_write(tp, 0x80c7, 0xf083);
7746 		sram_write(tp, 0x80dd, 0x03f0);
7747 		data = sram_read(tp, 0x80df);
7748 		data &= ~0xff00;
7749 		data |= 0x1000;
7750 		sram_write(tp, 0x80df, data);
7751 		sram_write(tp, 0x80cb, 0x2007);
7752 		data = sram_read(tp, 0x80ce);
7753 		data &= ~0xff00;
7754 		data |= 0x6c00;
7755 		sram_write(tp, 0x80ce, data);
7756 		sram_write(tp, 0x80c9, 0x8009);
7757 		data = sram_read(tp, 0x80d1);
7758 		data &= ~0xff00;
7759 		data |= 0x8000;
7760 		sram_write(tp, 0x80d1, data);
7761 		sram_write(tp, 0x80a3, 0x200a);
7762 		sram_write(tp, 0x80a5, 0xf0ad);
7763 		sram_write(tp, 0x809f, 0x6073);
7764 		sram_write(tp, 0x80a1, 0x000b);
7765 		data = sram_read(tp, 0x80a9);
7766 		data &= ~0xff00;
7767 		data |= 0xc000;
7768 		sram_write(tp, 0x80a9, data);
7769 
7770 		if (rtl_phy_patch_request(tp, true, true))
7771 			return;
7772 
7773 		data = ocp_reg_read(tp, 0xb896);
7774 		data &= ~BIT(0);
7775 		ocp_reg_write(tp, 0xb896, data);
7776 		data = ocp_reg_read(tp, 0xb892);
7777 		data &= ~0xff00;
7778 		ocp_reg_write(tp, 0xb892, data);
7779 		ocp_reg_write(tp, 0xb88e, 0xc23e);
7780 		ocp_reg_write(tp, 0xb890, 0x0000);
7781 		ocp_reg_write(tp, 0xb88e, 0xc240);
7782 		ocp_reg_write(tp, 0xb890, 0x0103);
7783 		ocp_reg_write(tp, 0xb88e, 0xc242);
7784 		ocp_reg_write(tp, 0xb890, 0x0507);
7785 		ocp_reg_write(tp, 0xb88e, 0xc244);
7786 		ocp_reg_write(tp, 0xb890, 0x090b);
7787 		ocp_reg_write(tp, 0xb88e, 0xc246);
7788 		ocp_reg_write(tp, 0xb890, 0x0c0e);
7789 		ocp_reg_write(tp, 0xb88e, 0xc248);
7790 		ocp_reg_write(tp, 0xb890, 0x1012);
7791 		ocp_reg_write(tp, 0xb88e, 0xc24a);
7792 		ocp_reg_write(tp, 0xb890, 0x1416);
7793 		data = ocp_reg_read(tp, 0xb896);
7794 		data |= BIT(0);
7795 		ocp_reg_write(tp, 0xb896, data);
7796 
7797 		rtl_phy_patch_request(tp, false, true);
7798 
7799 		data = ocp_reg_read(tp, 0xa86a);
7800 		data |= BIT(0);
7801 		ocp_reg_write(tp, 0xa86a, data);
7802 		data = ocp_reg_read(tp, 0xa6f0);
7803 		data |= BIT(0);
7804 		ocp_reg_write(tp, 0xa6f0, data);
7805 
7806 		ocp_reg_write(tp, 0xbfa0, 0xd70d);
7807 		ocp_reg_write(tp, 0xbfa2, 0x4100);
7808 		ocp_reg_write(tp, 0xbfa4, 0xe868);
7809 		ocp_reg_write(tp, 0xbfa6, 0xdc59);
7810 		ocp_reg_write(tp, 0xb54c, 0x3c18);
7811 		data = ocp_reg_read(tp, 0xbfa4);
7812 		data &= ~BIT(5);
7813 		ocp_reg_write(tp, 0xbfa4, data);
7814 		data = sram_read(tp, 0x817d);
7815 		data |= BIT(12);
7816 		sram_write(tp, 0x817d, data);
7817 		break;
7818 	case RTL_VER_13:
7819 		/* 2.5G INRX */
7820 		data = ocp_reg_read(tp, 0xac46);
7821 		data &= ~0x00f0;
7822 		data |= 0x0090;
7823 		ocp_reg_write(tp, 0xac46, data);
7824 		data = ocp_reg_read(tp, 0xad30);
7825 		data &= ~0x0003;
7826 		data |= 0x0001;
7827 		ocp_reg_write(tp, 0xad30, data);
7828 		fallthrough;
7829 	case RTL_VER_15:
7830 		/* EEE parameter */
7831 		ocp_reg_write(tp, 0xb87c, 0x80f5);
7832 		ocp_reg_write(tp, 0xb87e, 0x760e);
7833 		ocp_reg_write(tp, 0xb87c, 0x8107);
7834 		ocp_reg_write(tp, 0xb87e, 0x360e);
7835 		ocp_reg_write(tp, 0xb87c, 0x8551);
7836 		data = ocp_reg_read(tp, 0xb87e);
7837 		data &= ~0xff00;
7838 		data |= 0x0800;
7839 		ocp_reg_write(tp, 0xb87e, data);
7840 
7841 		/* ADC_PGA parameter */
7842 		data = ocp_reg_read(tp, 0xbf00);
7843 		data &= ~0xe000;
7844 		data |= 0xa000;
7845 		ocp_reg_write(tp, 0xbf00, data);
7846 		data = ocp_reg_read(tp, 0xbf46);
7847 		data &= ~0x0f00;
7848 		data |= 0x0300;
7849 		ocp_reg_write(tp, 0xbf46, data);
7850 
7851 		/* Green Table-PGA, 1G full viterbi */
7852 		sram_write(tp, 0x8044, 0x2417);
7853 		sram_write(tp, 0x804a, 0x2417);
7854 		sram_write(tp, 0x8050, 0x2417);
7855 		sram_write(tp, 0x8056, 0x2417);
7856 		sram_write(tp, 0x805c, 0x2417);
7857 		sram_write(tp, 0x8062, 0x2417);
7858 		sram_write(tp, 0x8068, 0x2417);
7859 		sram_write(tp, 0x806e, 0x2417);
7860 		sram_write(tp, 0x8074, 0x2417);
7861 		sram_write(tp, 0x807a, 0x2417);
7862 
7863 		/* XG PLL */
7864 		data = ocp_reg_read(tp, 0xbf84);
7865 		data &= ~0xe000;
7866 		data |= 0xa000;
7867 		ocp_reg_write(tp, 0xbf84, data);
7868 		break;
7869 	default:
7870 		break;
7871 	}
7872 
7873 	if (rtl_phy_patch_request(tp, true, true))
7874 		return;
7875 
7876 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4);
7877 	ocp_data |= EEE_SPDWN_EN;
7878 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, ocp_data);
7879 
7880 	data = ocp_reg_read(tp, OCP_DOWN_SPEED);
7881 	data &= ~(EN_EEE_100 | EN_EEE_1000);
7882 	data |= EN_10M_CLKDIV;
7883 	ocp_reg_write(tp, OCP_DOWN_SPEED, data);
7884 	tp->ups_info._10m_ckdiv = true;
7885 	tp->ups_info.eee_plloff_100 = false;
7886 	tp->ups_info.eee_plloff_giga = false;
7887 
7888 	data = ocp_reg_read(tp, OCP_POWER_CFG);
7889 	data &= ~EEE_CLKDIV_EN;
7890 	ocp_reg_write(tp, OCP_POWER_CFG, data);
7891 	tp->ups_info.eee_ckdiv = false;
7892 
7893 	rtl_phy_patch_request(tp, false, true);
7894 
7895 	rtl_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags));
7896 
7897 	data = ocp_reg_read(tp, 0xa428);
7898 	data &= ~BIT(9);
7899 	ocp_reg_write(tp, 0xa428, data);
7900 	data = ocp_reg_read(tp, 0xa5ea);
7901 	data &= ~BIT(0);
7902 	ocp_reg_write(tp, 0xa5ea, data);
7903 	tp->ups_info.lite_mode = 0;
7904 
7905 	if (tp->eee_en)
7906 		rtl_eee_enable(tp, true);
7907 
7908 	r8153_aldps_en(tp, true);
7909 	r8152b_enable_fc(tp);
7910 	r8153_u2p3en(tp, true);
7911 
7912 	set_bit(PHY_RESET, &tp->flags);
7913 }
7914 
7915 static void r8156_init(struct r8152 *tp)
7916 {
7917 	u32 ocp_data;
7918 	u16 data;
7919 	int i;
7920 
7921 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
7922 		return;
7923 
7924 	ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_ECM_OP);
7925 	ocp_data &= ~EN_ALL_SPEED;
7926 	ocp_write_byte(tp, MCU_TYPE_USB, USB_ECM_OP, ocp_data);
7927 
7928 	ocp_write_word(tp, MCU_TYPE_USB, USB_SPEED_OPTION, 0);
7929 
7930 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_ECM_OPTION);
7931 	ocp_data |= BYPASS_MAC_RESET;
7932 	ocp_write_word(tp, MCU_TYPE_USB, USB_ECM_OPTION, ocp_data);
7933 
7934 	r8153b_u1u2en(tp, false);
7935 
7936 	for (i = 0; i < 500; i++) {
7937 		if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
7938 		    AUTOLOAD_DONE)
7939 			break;
7940 
7941 		msleep(20);
7942 		if (test_bit(RTL8152_UNPLUG, &tp->flags))
7943 			return;
7944 	}
7945 
7946 	data = r8153_phy_status(tp, 0);
7947 	if (data == PHY_STAT_EXT_INIT) {
7948 		data = ocp_reg_read(tp, 0xa468);
7949 		data &= ~(BIT(3) | BIT(1));
7950 		ocp_reg_write(tp, 0xa468, data);
7951 	}
7952 
7953 	data = r8152_mdio_read(tp, MII_BMCR);
7954 	if (data & BMCR_PDOWN) {
7955 		data &= ~BMCR_PDOWN;
7956 		r8152_mdio_write(tp, MII_BMCR, data);
7957 	}
7958 
7959 	data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
7960 	WARN_ON_ONCE(data != PHY_STAT_LAN_ON);
7961 
7962 	r8153_u2p3en(tp, false);
7963 
7964 	/* MSC timer = 0xfff * 8ms = 32760 ms */
7965 	ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff);
7966 
7967 	/* U1/U2/L1 idle timer. 500 us */
7968 	ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500);
7969 
7970 	r8153b_power_cut_en(tp, false);
7971 	r8156_ups_en(tp, false);
7972 	r8153_queue_wake(tp, false);
7973 	rtl_runtime_suspend_enable(tp, false);
7974 
7975 	if (tp->udev->speed >= USB_SPEED_SUPER)
7976 		r8153b_u1u2en(tp, true);
7977 
7978 	usb_enable_lpm(tp->udev);
7979 
7980 	r8156_mac_clk_spd(tp, true);
7981 
7982 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
7983 	ocp_data &= ~PLA_MCU_SPDWN_EN;
7984 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
7985 
7986 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
7987 	if (rtl8152_get_speed(tp) & LINK_STATUS)
7988 		ocp_data |= CUR_LINK_OK;
7989 	else
7990 		ocp_data &= ~CUR_LINK_OK;
7991 	ocp_data |= POLL_LINK_CHG;
7992 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
7993 
7994 	set_bit(GREEN_ETHERNET, &tp->flags);
7995 
7996 	/* rx aggregation */
7997 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
7998 	ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
7999 	ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
8000 
8001 	ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_BMU_CONFIG);
8002 	ocp_data |= ACT_ODMA;
8003 	ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_CONFIG, ocp_data);
8004 
8005 	rtl_tally_reset(tp);
8006 
8007 	tp->coalesce = 15000;	/* 15 us */
8008 }
8009 
8010 static void r8156b_init(struct r8152 *tp)
8011 {
8012 	u32 ocp_data;
8013 	u16 data;
8014 	int i;
8015 
8016 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
8017 		return;
8018 
8019 	ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_ECM_OP);
8020 	ocp_data &= ~EN_ALL_SPEED;
8021 	ocp_write_byte(tp, MCU_TYPE_USB, USB_ECM_OP, ocp_data);
8022 
8023 	ocp_write_word(tp, MCU_TYPE_USB, USB_SPEED_OPTION, 0);
8024 
8025 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_ECM_OPTION);
8026 	ocp_data |= BYPASS_MAC_RESET;
8027 	ocp_write_word(tp, MCU_TYPE_USB, USB_ECM_OPTION, ocp_data);
8028 
8029 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
8030 	ocp_data |= RX_DETECT8;
8031 	ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
8032 
8033 	r8153b_u1u2en(tp, false);
8034 
8035 	switch (tp->version) {
8036 	case RTL_VER_13:
8037 	case RTL_VER_15:
8038 		r8156b_wait_loading_flash(tp);
8039 		break;
8040 	default:
8041 		break;
8042 	}
8043 
8044 	for (i = 0; i < 500; i++) {
8045 		if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
8046 		    AUTOLOAD_DONE)
8047 			break;
8048 
8049 		msleep(20);
8050 		if (test_bit(RTL8152_UNPLUG, &tp->flags))
8051 			return;
8052 	}
8053 
8054 	data = r8153_phy_status(tp, 0);
8055 	if (data == PHY_STAT_EXT_INIT) {
8056 		data = ocp_reg_read(tp, 0xa468);
8057 		data &= ~(BIT(3) | BIT(1));
8058 		ocp_reg_write(tp, 0xa468, data);
8059 
8060 		data = ocp_reg_read(tp, 0xa466);
8061 		data &= ~BIT(0);
8062 		ocp_reg_write(tp, 0xa466, data);
8063 	}
8064 
8065 	data = r8152_mdio_read(tp, MII_BMCR);
8066 	if (data & BMCR_PDOWN) {
8067 		data &= ~BMCR_PDOWN;
8068 		r8152_mdio_write(tp, MII_BMCR, data);
8069 	}
8070 
8071 	data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
8072 
8073 	r8153_u2p3en(tp, false);
8074 
8075 	/* MSC timer = 0xfff * 8ms = 32760 ms */
8076 	ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff);
8077 
8078 	/* U1/U2/L1 idle timer. 500 us */
8079 	ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500);
8080 
8081 	r8153b_power_cut_en(tp, false);
8082 	r8156_ups_en(tp, false);
8083 	r8153_queue_wake(tp, false);
8084 	rtl_runtime_suspend_enable(tp, false);
8085 
8086 	if (tp->udev->speed >= USB_SPEED_SUPER)
8087 		r8153b_u1u2en(tp, true);
8088 
8089 	usb_enable_lpm(tp->udev);
8090 
8091 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RCR);
8092 	ocp_data &= ~SLOT_EN;
8093 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
8094 
8095 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
8096 	ocp_data |= FLOW_CTRL_EN;
8097 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
8098 
8099 	/* enable fc timer and set timer to 600 ms. */
8100 	ocp_write_word(tp, MCU_TYPE_USB, USB_FC_TIMER,
8101 		       CTRL_TIMER_EN | (600 / 8));
8102 
8103 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_CTRL);
8104 	if (!(ocp_read_word(tp, MCU_TYPE_PLA, PLA_POL_GPIO_CTRL) & DACK_DET_EN))
8105 		ocp_data |= FLOW_CTRL_PATCH_2;
8106 	ocp_data &= ~AUTO_SPEEDUP;
8107 	ocp_write_word(tp, MCU_TYPE_USB, USB_FW_CTRL, ocp_data);
8108 
8109 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_FW_TASK);
8110 	ocp_data |= FC_PATCH_TASK;
8111 	ocp_write_word(tp, MCU_TYPE_USB, USB_FW_TASK, ocp_data);
8112 
8113 	r8156_mac_clk_spd(tp, true);
8114 
8115 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3);
8116 	ocp_data &= ~PLA_MCU_SPDWN_EN;
8117 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, ocp_data);
8118 
8119 	ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS);
8120 	if (rtl8152_get_speed(tp) & LINK_STATUS)
8121 		ocp_data |= CUR_LINK_OK;
8122 	else
8123 		ocp_data &= ~CUR_LINK_OK;
8124 	ocp_data |= POLL_LINK_CHG;
8125 	ocp_write_word(tp, MCU_TYPE_PLA, PLA_EXTRA_STATUS, ocp_data);
8126 
8127 	set_bit(GREEN_ETHERNET, &tp->flags);
8128 
8129 	/* rx aggregation */
8130 	ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
8131 	ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
8132 	ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
8133 
8134 	rtl_tally_reset(tp);
8135 
8136 	tp->coalesce = 15000;	/* 15 us */
8137 }
8138 
8139 static bool rtl_check_vendor_ok(struct usb_interface *intf)
8140 {
8141 	struct usb_host_interface *alt = intf->cur_altsetting;
8142 	struct usb_endpoint_descriptor *in, *out, *intr;
8143 
8144 	if (usb_find_common_endpoints(alt, &in, &out, &intr, NULL) < 0) {
8145 		dev_err(&intf->dev, "Expected endpoints are not found\n");
8146 		return false;
8147 	}
8148 
8149 	/* Check Rx endpoint address */
8150 	if (usb_endpoint_num(in) != 1) {
8151 		dev_err(&intf->dev, "Invalid Rx endpoint address\n");
8152 		return false;
8153 	}
8154 
8155 	/* Check Tx endpoint address */
8156 	if (usb_endpoint_num(out) != 2) {
8157 		dev_err(&intf->dev, "Invalid Tx endpoint address\n");
8158 		return false;
8159 	}
8160 
8161 	/* Check interrupt endpoint address */
8162 	if (usb_endpoint_num(intr) != 3) {
8163 		dev_err(&intf->dev, "Invalid interrupt endpoint address\n");
8164 		return false;
8165 	}
8166 
8167 	return true;
8168 }
8169 
8170 static bool rtl_vendor_mode(struct usb_interface *intf)
8171 {
8172 	struct usb_host_interface *alt = intf->cur_altsetting;
8173 	struct usb_device *udev;
8174 	struct usb_host_config *c;
8175 	int i, num_configs;
8176 
8177 	if (alt->desc.bInterfaceClass == USB_CLASS_VENDOR_SPEC)
8178 		return rtl_check_vendor_ok(intf);
8179 
8180 	/* The vendor mode is not always config #1, so to find it out. */
8181 	udev = interface_to_usbdev(intf);
8182 	c = udev->config;
8183 	num_configs = udev->descriptor.bNumConfigurations;
8184 	if (num_configs < 2)
8185 		return false;
8186 
8187 	for (i = 0; i < num_configs; (i++, c++)) {
8188 		struct usb_interface_descriptor	*desc = NULL;
8189 
8190 		if (c->desc.bNumInterfaces > 0)
8191 			desc = &c->intf_cache[0]->altsetting->desc;
8192 		else
8193 			continue;
8194 
8195 		if (desc->bInterfaceClass == USB_CLASS_VENDOR_SPEC) {
8196 			usb_driver_set_configuration(udev, c->desc.bConfigurationValue);
8197 			break;
8198 		}
8199 	}
8200 
8201 	if (i == num_configs)
8202 		dev_err(&intf->dev, "Unexpected Device\n");
8203 
8204 	return false;
8205 }
8206 
8207 static int rtl8152_pre_reset(struct usb_interface *intf)
8208 {
8209 	struct r8152 *tp = usb_get_intfdata(intf);
8210 	struct net_device *netdev;
8211 
8212 	if (!tp)
8213 		return 0;
8214 
8215 	netdev = tp->netdev;
8216 	if (!netif_running(netdev))
8217 		return 0;
8218 
8219 	netif_stop_queue(netdev);
8220 	tasklet_disable(&tp->tx_tl);
8221 	clear_bit(WORK_ENABLE, &tp->flags);
8222 	usb_kill_urb(tp->intr_urb);
8223 	cancel_delayed_work_sync(&tp->schedule);
8224 	napi_disable(&tp->napi);
8225 	if (netif_carrier_ok(netdev)) {
8226 		mutex_lock(&tp->control);
8227 		tp->rtl_ops.disable(tp);
8228 		mutex_unlock(&tp->control);
8229 	}
8230 
8231 	return 0;
8232 }
8233 
8234 static int rtl8152_post_reset(struct usb_interface *intf)
8235 {
8236 	struct r8152 *tp = usb_get_intfdata(intf);
8237 	struct net_device *netdev;
8238 	struct sockaddr sa;
8239 
8240 	if (!tp)
8241 		return 0;
8242 
8243 	/* reset the MAC address in case of policy change */
8244 	if (determine_ethernet_addr(tp, &sa) >= 0) {
8245 		rtnl_lock();
8246 		dev_set_mac_address (tp->netdev, &sa, NULL);
8247 		rtnl_unlock();
8248 	}
8249 
8250 	netdev = tp->netdev;
8251 	if (!netif_running(netdev))
8252 		return 0;
8253 
8254 	set_bit(WORK_ENABLE, &tp->flags);
8255 	if (netif_carrier_ok(netdev)) {
8256 		mutex_lock(&tp->control);
8257 		tp->rtl_ops.enable(tp);
8258 		rtl_start_rx(tp);
8259 		_rtl8152_set_rx_mode(netdev);
8260 		mutex_unlock(&tp->control);
8261 	}
8262 
8263 	napi_enable(&tp->napi);
8264 	tasklet_enable(&tp->tx_tl);
8265 	netif_wake_queue(netdev);
8266 	usb_submit_urb(tp->intr_urb, GFP_KERNEL);
8267 
8268 	if (!list_empty(&tp->rx_done))
8269 		napi_schedule(&tp->napi);
8270 
8271 	return 0;
8272 }
8273 
8274 static bool delay_autosuspend(struct r8152 *tp)
8275 {
8276 	bool sw_linking = !!netif_carrier_ok(tp->netdev);
8277 	bool hw_linking = !!(rtl8152_get_speed(tp) & LINK_STATUS);
8278 
8279 	/* This means a linking change occurs and the driver doesn't detect it,
8280 	 * yet. If the driver has disabled tx/rx and hw is linking on, the
8281 	 * device wouldn't wake up by receiving any packet.
8282 	 */
8283 	if (work_busy(&tp->schedule.work) || sw_linking != hw_linking)
8284 		return true;
8285 
8286 	/* If the linking down is occurred by nway, the device may miss the
8287 	 * linking change event. And it wouldn't wake when linking on.
8288 	 */
8289 	if (!sw_linking && tp->rtl_ops.in_nway(tp))
8290 		return true;
8291 	else if (!skb_queue_empty(&tp->tx_queue))
8292 		return true;
8293 	else
8294 		return false;
8295 }
8296 
8297 static int rtl8152_runtime_resume(struct r8152 *tp)
8298 {
8299 	struct net_device *netdev = tp->netdev;
8300 
8301 	if (netif_running(netdev) && netdev->flags & IFF_UP) {
8302 		struct napi_struct *napi = &tp->napi;
8303 
8304 		tp->rtl_ops.autosuspend_en(tp, false);
8305 		napi_disable(napi);
8306 		set_bit(WORK_ENABLE, &tp->flags);
8307 
8308 		if (netif_carrier_ok(netdev)) {
8309 			if (rtl8152_get_speed(tp) & LINK_STATUS) {
8310 				rtl_start_rx(tp);
8311 			} else {
8312 				netif_carrier_off(netdev);
8313 				tp->rtl_ops.disable(tp);
8314 				netif_info(tp, link, netdev, "linking down\n");
8315 			}
8316 		}
8317 
8318 		napi_enable(napi);
8319 		clear_bit(SELECTIVE_SUSPEND, &tp->flags);
8320 		smp_mb__after_atomic();
8321 
8322 		if (!list_empty(&tp->rx_done))
8323 			napi_schedule(&tp->napi);
8324 
8325 		usb_submit_urb(tp->intr_urb, GFP_NOIO);
8326 	} else {
8327 		if (netdev->flags & IFF_UP)
8328 			tp->rtl_ops.autosuspend_en(tp, false);
8329 
8330 		clear_bit(SELECTIVE_SUSPEND, &tp->flags);
8331 	}
8332 
8333 	return 0;
8334 }
8335 
8336 static int rtl8152_system_resume(struct r8152 *tp)
8337 {
8338 	struct net_device *netdev = tp->netdev;
8339 
8340 	netif_device_attach(netdev);
8341 
8342 	if (netif_running(netdev) && (netdev->flags & IFF_UP)) {
8343 		tp->rtl_ops.up(tp);
8344 		netif_carrier_off(netdev);
8345 		set_bit(WORK_ENABLE, &tp->flags);
8346 		usb_submit_urb(tp->intr_urb, GFP_NOIO);
8347 	}
8348 
8349 	return 0;
8350 }
8351 
8352 static int rtl8152_runtime_suspend(struct r8152 *tp)
8353 {
8354 	struct net_device *netdev = tp->netdev;
8355 	int ret = 0;
8356 
8357 	if (!tp->rtl_ops.autosuspend_en)
8358 		return -EBUSY;
8359 
8360 	set_bit(SELECTIVE_SUSPEND, &tp->flags);
8361 	smp_mb__after_atomic();
8362 
8363 	if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
8364 		u32 rcr = 0;
8365 
8366 		if (netif_carrier_ok(netdev)) {
8367 			u32 ocp_data;
8368 
8369 			rcr = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
8370 			ocp_data = rcr & ~RCR_ACPT_ALL;
8371 			ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
8372 			rxdy_gated_en(tp, true);
8373 			ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA,
8374 						 PLA_OOB_CTRL);
8375 			if (!(ocp_data & RXFIFO_EMPTY)) {
8376 				rxdy_gated_en(tp, false);
8377 				ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr);
8378 				clear_bit(SELECTIVE_SUSPEND, &tp->flags);
8379 				smp_mb__after_atomic();
8380 				ret = -EBUSY;
8381 				goto out1;
8382 			}
8383 		}
8384 
8385 		clear_bit(WORK_ENABLE, &tp->flags);
8386 		usb_kill_urb(tp->intr_urb);
8387 
8388 		tp->rtl_ops.autosuspend_en(tp, true);
8389 
8390 		if (netif_carrier_ok(netdev)) {
8391 			struct napi_struct *napi = &tp->napi;
8392 
8393 			napi_disable(napi);
8394 			rtl_stop_rx(tp);
8395 			rxdy_gated_en(tp, false);
8396 			ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr);
8397 			napi_enable(napi);
8398 		}
8399 
8400 		if (delay_autosuspend(tp)) {
8401 			rtl8152_runtime_resume(tp);
8402 			ret = -EBUSY;
8403 		}
8404 	}
8405 
8406 out1:
8407 	return ret;
8408 }
8409 
8410 static int rtl8152_system_suspend(struct r8152 *tp)
8411 {
8412 	struct net_device *netdev = tp->netdev;
8413 
8414 	netif_device_detach(netdev);
8415 
8416 	if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
8417 		struct napi_struct *napi = &tp->napi;
8418 
8419 		clear_bit(WORK_ENABLE, &tp->flags);
8420 		usb_kill_urb(tp->intr_urb);
8421 		tasklet_disable(&tp->tx_tl);
8422 		napi_disable(napi);
8423 		cancel_delayed_work_sync(&tp->schedule);
8424 		tp->rtl_ops.down(tp);
8425 		napi_enable(napi);
8426 		tasklet_enable(&tp->tx_tl);
8427 	}
8428 
8429 	return 0;
8430 }
8431 
8432 static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
8433 {
8434 	struct r8152 *tp = usb_get_intfdata(intf);
8435 	int ret;
8436 
8437 	mutex_lock(&tp->control);
8438 
8439 	if (PMSG_IS_AUTO(message))
8440 		ret = rtl8152_runtime_suspend(tp);
8441 	else
8442 		ret = rtl8152_system_suspend(tp);
8443 
8444 	mutex_unlock(&tp->control);
8445 
8446 	return ret;
8447 }
8448 
8449 static int rtl8152_resume(struct usb_interface *intf)
8450 {
8451 	struct r8152 *tp = usb_get_intfdata(intf);
8452 	int ret;
8453 
8454 	mutex_lock(&tp->control);
8455 
8456 	if (test_bit(SELECTIVE_SUSPEND, &tp->flags))
8457 		ret = rtl8152_runtime_resume(tp);
8458 	else
8459 		ret = rtl8152_system_resume(tp);
8460 
8461 	mutex_unlock(&tp->control);
8462 
8463 	return ret;
8464 }
8465 
8466 static int rtl8152_reset_resume(struct usb_interface *intf)
8467 {
8468 	struct r8152 *tp = usb_get_intfdata(intf);
8469 
8470 	clear_bit(SELECTIVE_SUSPEND, &tp->flags);
8471 	tp->rtl_ops.init(tp);
8472 	queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0);
8473 	set_ethernet_addr(tp, true);
8474 	return rtl8152_resume(intf);
8475 }
8476 
8477 static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
8478 {
8479 	struct r8152 *tp = netdev_priv(dev);
8480 
8481 	if (usb_autopm_get_interface(tp->intf) < 0)
8482 		return;
8483 
8484 	if (!rtl_can_wakeup(tp)) {
8485 		wol->supported = 0;
8486 		wol->wolopts = 0;
8487 	} else {
8488 		mutex_lock(&tp->control);
8489 		wol->supported = WAKE_ANY;
8490 		wol->wolopts = __rtl_get_wol(tp);
8491 		mutex_unlock(&tp->control);
8492 	}
8493 
8494 	usb_autopm_put_interface(tp->intf);
8495 }
8496 
8497 static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
8498 {
8499 	struct r8152 *tp = netdev_priv(dev);
8500 	int ret;
8501 
8502 	if (!rtl_can_wakeup(tp))
8503 		return -EOPNOTSUPP;
8504 
8505 	if (wol->wolopts & ~WAKE_ANY)
8506 		return -EINVAL;
8507 
8508 	ret = usb_autopm_get_interface(tp->intf);
8509 	if (ret < 0)
8510 		goto out_set_wol;
8511 
8512 	mutex_lock(&tp->control);
8513 
8514 	__rtl_set_wol(tp, wol->wolopts);
8515 	tp->saved_wolopts = wol->wolopts & WAKE_ANY;
8516 
8517 	mutex_unlock(&tp->control);
8518 
8519 	usb_autopm_put_interface(tp->intf);
8520 
8521 out_set_wol:
8522 	return ret;
8523 }
8524 
8525 static u32 rtl8152_get_msglevel(struct net_device *dev)
8526 {
8527 	struct r8152 *tp = netdev_priv(dev);
8528 
8529 	return tp->msg_enable;
8530 }
8531 
8532 static void rtl8152_set_msglevel(struct net_device *dev, u32 value)
8533 {
8534 	struct r8152 *tp = netdev_priv(dev);
8535 
8536 	tp->msg_enable = value;
8537 }
8538 
8539 static void rtl8152_get_drvinfo(struct net_device *netdev,
8540 				struct ethtool_drvinfo *info)
8541 {
8542 	struct r8152 *tp = netdev_priv(netdev);
8543 
8544 	strlcpy(info->driver, MODULENAME, sizeof(info->driver));
8545 	strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
8546 	usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
8547 	if (!IS_ERR_OR_NULL(tp->rtl_fw.fw))
8548 		strlcpy(info->fw_version, tp->rtl_fw.version,
8549 			sizeof(info->fw_version));
8550 }
8551 
8552 static
8553 int rtl8152_get_link_ksettings(struct net_device *netdev,
8554 			       struct ethtool_link_ksettings *cmd)
8555 {
8556 	struct r8152 *tp = netdev_priv(netdev);
8557 	int ret;
8558 
8559 	if (!tp->mii.mdio_read)
8560 		return -EOPNOTSUPP;
8561 
8562 	ret = usb_autopm_get_interface(tp->intf);
8563 	if (ret < 0)
8564 		goto out;
8565 
8566 	mutex_lock(&tp->control);
8567 
8568 	mii_ethtool_get_link_ksettings(&tp->mii, cmd);
8569 
8570 	linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
8571 			 cmd->link_modes.supported, tp->support_2500full);
8572 
8573 	if (tp->support_2500full) {
8574 		linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
8575 				 cmd->link_modes.advertising,
8576 				 ocp_reg_read(tp, OCP_10GBT_CTRL) & MDIO_AN_10GBT_CTRL_ADV2_5G);
8577 
8578 		linkmode_mod_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
8579 				 cmd->link_modes.lp_advertising,
8580 				 ocp_reg_read(tp, OCP_10GBT_STAT) & MDIO_AN_10GBT_STAT_LP2_5G);
8581 
8582 		if (is_speed_2500(rtl8152_get_speed(tp)))
8583 			cmd->base.speed = SPEED_2500;
8584 	}
8585 
8586 	mutex_unlock(&tp->control);
8587 
8588 	usb_autopm_put_interface(tp->intf);
8589 
8590 out:
8591 	return ret;
8592 }
8593 
8594 static int rtl8152_set_link_ksettings(struct net_device *dev,
8595 				      const struct ethtool_link_ksettings *cmd)
8596 {
8597 	struct r8152 *tp = netdev_priv(dev);
8598 	u32 advertising = 0;
8599 	int ret;
8600 
8601 	ret = usb_autopm_get_interface(tp->intf);
8602 	if (ret < 0)
8603 		goto out;
8604 
8605 	if (test_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT,
8606 		     cmd->link_modes.advertising))
8607 		advertising |= RTL_ADVERTISED_10_HALF;
8608 
8609 	if (test_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT,
8610 		     cmd->link_modes.advertising))
8611 		advertising |= RTL_ADVERTISED_10_FULL;
8612 
8613 	if (test_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT,
8614 		     cmd->link_modes.advertising))
8615 		advertising |= RTL_ADVERTISED_100_HALF;
8616 
8617 	if (test_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
8618 		     cmd->link_modes.advertising))
8619 		advertising |= RTL_ADVERTISED_100_FULL;
8620 
8621 	if (test_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
8622 		     cmd->link_modes.advertising))
8623 		advertising |= RTL_ADVERTISED_1000_HALF;
8624 
8625 	if (test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
8626 		     cmd->link_modes.advertising))
8627 		advertising |= RTL_ADVERTISED_1000_FULL;
8628 
8629 	if (test_bit(ETHTOOL_LINK_MODE_2500baseT_Full_BIT,
8630 		     cmd->link_modes.advertising))
8631 		advertising |= RTL_ADVERTISED_2500_FULL;
8632 
8633 	mutex_lock(&tp->control);
8634 
8635 	ret = rtl8152_set_speed(tp, cmd->base.autoneg, cmd->base.speed,
8636 				cmd->base.duplex, advertising);
8637 	if (!ret) {
8638 		tp->autoneg = cmd->base.autoneg;
8639 		tp->speed = cmd->base.speed;
8640 		tp->duplex = cmd->base.duplex;
8641 		tp->advertising = advertising;
8642 	}
8643 
8644 	mutex_unlock(&tp->control);
8645 
8646 	usb_autopm_put_interface(tp->intf);
8647 
8648 out:
8649 	return ret;
8650 }
8651 
8652 static const char rtl8152_gstrings[][ETH_GSTRING_LEN] = {
8653 	"tx_packets",
8654 	"rx_packets",
8655 	"tx_errors",
8656 	"rx_errors",
8657 	"rx_missed",
8658 	"align_errors",
8659 	"tx_single_collisions",
8660 	"tx_multi_collisions",
8661 	"rx_unicast",
8662 	"rx_broadcast",
8663 	"rx_multicast",
8664 	"tx_aborted",
8665 	"tx_underrun",
8666 };
8667 
8668 static int rtl8152_get_sset_count(struct net_device *dev, int sset)
8669 {
8670 	switch (sset) {
8671 	case ETH_SS_STATS:
8672 		return ARRAY_SIZE(rtl8152_gstrings);
8673 	default:
8674 		return -EOPNOTSUPP;
8675 	}
8676 }
8677 
8678 static void rtl8152_get_ethtool_stats(struct net_device *dev,
8679 				      struct ethtool_stats *stats, u64 *data)
8680 {
8681 	struct r8152 *tp = netdev_priv(dev);
8682 	struct tally_counter tally;
8683 
8684 	if (usb_autopm_get_interface(tp->intf) < 0)
8685 		return;
8686 
8687 	generic_ocp_read(tp, PLA_TALLYCNT, sizeof(tally), &tally, MCU_TYPE_PLA);
8688 
8689 	usb_autopm_put_interface(tp->intf);
8690 
8691 	data[0] = le64_to_cpu(tally.tx_packets);
8692 	data[1] = le64_to_cpu(tally.rx_packets);
8693 	data[2] = le64_to_cpu(tally.tx_errors);
8694 	data[3] = le32_to_cpu(tally.rx_errors);
8695 	data[4] = le16_to_cpu(tally.rx_missed);
8696 	data[5] = le16_to_cpu(tally.align_errors);
8697 	data[6] = le32_to_cpu(tally.tx_one_collision);
8698 	data[7] = le32_to_cpu(tally.tx_multi_collision);
8699 	data[8] = le64_to_cpu(tally.rx_unicast);
8700 	data[9] = le64_to_cpu(tally.rx_broadcast);
8701 	data[10] = le32_to_cpu(tally.rx_multicast);
8702 	data[11] = le16_to_cpu(tally.tx_aborted);
8703 	data[12] = le16_to_cpu(tally.tx_underrun);
8704 }
8705 
8706 static void rtl8152_get_strings(struct net_device *dev, u32 stringset, u8 *data)
8707 {
8708 	switch (stringset) {
8709 	case ETH_SS_STATS:
8710 		memcpy(data, rtl8152_gstrings, sizeof(rtl8152_gstrings));
8711 		break;
8712 	}
8713 }
8714 
8715 static int r8152_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
8716 {
8717 	u32 lp, adv, supported = 0;
8718 	u16 val;
8719 
8720 	val = r8152_mmd_read(tp, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
8721 	supported = mmd_eee_cap_to_ethtool_sup_t(val);
8722 
8723 	val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
8724 	adv = mmd_eee_adv_to_ethtool_adv_t(val);
8725 
8726 	val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
8727 	lp = mmd_eee_adv_to_ethtool_adv_t(val);
8728 
8729 	eee->eee_enabled = tp->eee_en;
8730 	eee->eee_active = !!(supported & adv & lp);
8731 	eee->supported = supported;
8732 	eee->advertised = tp->eee_adv;
8733 	eee->lp_advertised = lp;
8734 
8735 	return 0;
8736 }
8737 
8738 static int r8152_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
8739 {
8740 	u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
8741 
8742 	tp->eee_en = eee->eee_enabled;
8743 	tp->eee_adv = val;
8744 
8745 	rtl_eee_enable(tp, tp->eee_en);
8746 
8747 	return 0;
8748 }
8749 
8750 static int r8153_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
8751 {
8752 	u32 lp, adv, supported = 0;
8753 	u16 val;
8754 
8755 	val = ocp_reg_read(tp, OCP_EEE_ABLE);
8756 	supported = mmd_eee_cap_to_ethtool_sup_t(val);
8757 
8758 	val = ocp_reg_read(tp, OCP_EEE_ADV);
8759 	adv = mmd_eee_adv_to_ethtool_adv_t(val);
8760 
8761 	val = ocp_reg_read(tp, OCP_EEE_LPABLE);
8762 	lp = mmd_eee_adv_to_ethtool_adv_t(val);
8763 
8764 	eee->eee_enabled = tp->eee_en;
8765 	eee->eee_active = !!(supported & adv & lp);
8766 	eee->supported = supported;
8767 	eee->advertised = tp->eee_adv;
8768 	eee->lp_advertised = lp;
8769 
8770 	return 0;
8771 }
8772 
8773 static int
8774 rtl_ethtool_get_eee(struct net_device *net, struct ethtool_eee *edata)
8775 {
8776 	struct r8152 *tp = netdev_priv(net);
8777 	int ret;
8778 
8779 	if (!tp->rtl_ops.eee_get) {
8780 		ret = -EOPNOTSUPP;
8781 		goto out;
8782 	}
8783 
8784 	ret = usb_autopm_get_interface(tp->intf);
8785 	if (ret < 0)
8786 		goto out;
8787 
8788 	mutex_lock(&tp->control);
8789 
8790 	ret = tp->rtl_ops.eee_get(tp, edata);
8791 
8792 	mutex_unlock(&tp->control);
8793 
8794 	usb_autopm_put_interface(tp->intf);
8795 
8796 out:
8797 	return ret;
8798 }
8799 
8800 static int
8801 rtl_ethtool_set_eee(struct net_device *net, struct ethtool_eee *edata)
8802 {
8803 	struct r8152 *tp = netdev_priv(net);
8804 	int ret;
8805 
8806 	if (!tp->rtl_ops.eee_set) {
8807 		ret = -EOPNOTSUPP;
8808 		goto out;
8809 	}
8810 
8811 	ret = usb_autopm_get_interface(tp->intf);
8812 	if (ret < 0)
8813 		goto out;
8814 
8815 	mutex_lock(&tp->control);
8816 
8817 	ret = tp->rtl_ops.eee_set(tp, edata);
8818 	if (!ret)
8819 		ret = mii_nway_restart(&tp->mii);
8820 
8821 	mutex_unlock(&tp->control);
8822 
8823 	usb_autopm_put_interface(tp->intf);
8824 
8825 out:
8826 	return ret;
8827 }
8828 
8829 static int rtl8152_nway_reset(struct net_device *dev)
8830 {
8831 	struct r8152 *tp = netdev_priv(dev);
8832 	int ret;
8833 
8834 	ret = usb_autopm_get_interface(tp->intf);
8835 	if (ret < 0)
8836 		goto out;
8837 
8838 	mutex_lock(&tp->control);
8839 
8840 	ret = mii_nway_restart(&tp->mii);
8841 
8842 	mutex_unlock(&tp->control);
8843 
8844 	usb_autopm_put_interface(tp->intf);
8845 
8846 out:
8847 	return ret;
8848 }
8849 
8850 static int rtl8152_get_coalesce(struct net_device *netdev,
8851 				struct ethtool_coalesce *coalesce,
8852 				struct kernel_ethtool_coalesce *kernel_coal,
8853 				struct netlink_ext_ack *extack)
8854 {
8855 	struct r8152 *tp = netdev_priv(netdev);
8856 
8857 	switch (tp->version) {
8858 	case RTL_VER_01:
8859 	case RTL_VER_02:
8860 	case RTL_VER_07:
8861 		return -EOPNOTSUPP;
8862 	default:
8863 		break;
8864 	}
8865 
8866 	coalesce->rx_coalesce_usecs = tp->coalesce;
8867 
8868 	return 0;
8869 }
8870 
8871 static int rtl8152_set_coalesce(struct net_device *netdev,
8872 				struct ethtool_coalesce *coalesce,
8873 				struct kernel_ethtool_coalesce *kernel_coal,
8874 				struct netlink_ext_ack *extack)
8875 {
8876 	struct r8152 *tp = netdev_priv(netdev);
8877 	int ret;
8878 
8879 	switch (tp->version) {
8880 	case RTL_VER_01:
8881 	case RTL_VER_02:
8882 	case RTL_VER_07:
8883 		return -EOPNOTSUPP;
8884 	default:
8885 		break;
8886 	}
8887 
8888 	if (coalesce->rx_coalesce_usecs > COALESCE_SLOW)
8889 		return -EINVAL;
8890 
8891 	ret = usb_autopm_get_interface(tp->intf);
8892 	if (ret < 0)
8893 		return ret;
8894 
8895 	mutex_lock(&tp->control);
8896 
8897 	if (tp->coalesce != coalesce->rx_coalesce_usecs) {
8898 		tp->coalesce = coalesce->rx_coalesce_usecs;
8899 
8900 		if (netif_running(netdev) && netif_carrier_ok(netdev)) {
8901 			netif_stop_queue(netdev);
8902 			napi_disable(&tp->napi);
8903 			tp->rtl_ops.disable(tp);
8904 			tp->rtl_ops.enable(tp);
8905 			rtl_start_rx(tp);
8906 			clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
8907 			_rtl8152_set_rx_mode(netdev);
8908 			napi_enable(&tp->napi);
8909 			netif_wake_queue(netdev);
8910 		}
8911 	}
8912 
8913 	mutex_unlock(&tp->control);
8914 
8915 	usb_autopm_put_interface(tp->intf);
8916 
8917 	return ret;
8918 }
8919 
8920 static int rtl8152_get_tunable(struct net_device *netdev,
8921 			       const struct ethtool_tunable *tunable, void *d)
8922 {
8923 	struct r8152 *tp = netdev_priv(netdev);
8924 
8925 	switch (tunable->id) {
8926 	case ETHTOOL_RX_COPYBREAK:
8927 		*(u32 *)d = tp->rx_copybreak;
8928 		break;
8929 	default:
8930 		return -EOPNOTSUPP;
8931 	}
8932 
8933 	return 0;
8934 }
8935 
8936 static int rtl8152_set_tunable(struct net_device *netdev,
8937 			       const struct ethtool_tunable *tunable,
8938 			       const void *d)
8939 {
8940 	struct r8152 *tp = netdev_priv(netdev);
8941 	u32 val;
8942 
8943 	switch (tunable->id) {
8944 	case ETHTOOL_RX_COPYBREAK:
8945 		val = *(u32 *)d;
8946 		if (val < ETH_ZLEN) {
8947 			netif_err(tp, rx_err, netdev,
8948 				  "Invalid rx copy break value\n");
8949 			return -EINVAL;
8950 		}
8951 
8952 		if (tp->rx_copybreak != val) {
8953 			if (netdev->flags & IFF_UP) {
8954 				mutex_lock(&tp->control);
8955 				napi_disable(&tp->napi);
8956 				tp->rx_copybreak = val;
8957 				napi_enable(&tp->napi);
8958 				mutex_unlock(&tp->control);
8959 			} else {
8960 				tp->rx_copybreak = val;
8961 			}
8962 		}
8963 		break;
8964 	default:
8965 		return -EOPNOTSUPP;
8966 	}
8967 
8968 	return 0;
8969 }
8970 
8971 static void rtl8152_get_ringparam(struct net_device *netdev,
8972 				  struct ethtool_ringparam *ring)
8973 {
8974 	struct r8152 *tp = netdev_priv(netdev);
8975 
8976 	ring->rx_max_pending = RTL8152_RX_MAX_PENDING;
8977 	ring->rx_pending = tp->rx_pending;
8978 }
8979 
8980 static int rtl8152_set_ringparam(struct net_device *netdev,
8981 				 struct ethtool_ringparam *ring)
8982 {
8983 	struct r8152 *tp = netdev_priv(netdev);
8984 
8985 	if (ring->rx_pending < (RTL8152_MAX_RX * 2))
8986 		return -EINVAL;
8987 
8988 	if (tp->rx_pending != ring->rx_pending) {
8989 		if (netdev->flags & IFF_UP) {
8990 			mutex_lock(&tp->control);
8991 			napi_disable(&tp->napi);
8992 			tp->rx_pending = ring->rx_pending;
8993 			napi_enable(&tp->napi);
8994 			mutex_unlock(&tp->control);
8995 		} else {
8996 			tp->rx_pending = ring->rx_pending;
8997 		}
8998 	}
8999 
9000 	return 0;
9001 }
9002 
9003 static void rtl8152_get_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause)
9004 {
9005 	struct r8152 *tp = netdev_priv(netdev);
9006 	u16 bmcr, lcladv, rmtadv;
9007 	u8 cap;
9008 
9009 	if (usb_autopm_get_interface(tp->intf) < 0)
9010 		return;
9011 
9012 	mutex_lock(&tp->control);
9013 
9014 	bmcr = r8152_mdio_read(tp, MII_BMCR);
9015 	lcladv = r8152_mdio_read(tp, MII_ADVERTISE);
9016 	rmtadv = r8152_mdio_read(tp, MII_LPA);
9017 
9018 	mutex_unlock(&tp->control);
9019 
9020 	usb_autopm_put_interface(tp->intf);
9021 
9022 	if (!(bmcr & BMCR_ANENABLE)) {
9023 		pause->autoneg = 0;
9024 		pause->rx_pause = 0;
9025 		pause->tx_pause = 0;
9026 		return;
9027 	}
9028 
9029 	pause->autoneg = 1;
9030 
9031 	cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
9032 
9033 	if (cap & FLOW_CTRL_RX)
9034 		pause->rx_pause = 1;
9035 
9036 	if (cap & FLOW_CTRL_TX)
9037 		pause->tx_pause = 1;
9038 }
9039 
9040 static int rtl8152_set_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause)
9041 {
9042 	struct r8152 *tp = netdev_priv(netdev);
9043 	u16 old, new1;
9044 	u8 cap = 0;
9045 	int ret;
9046 
9047 	ret = usb_autopm_get_interface(tp->intf);
9048 	if (ret < 0)
9049 		return ret;
9050 
9051 	mutex_lock(&tp->control);
9052 
9053 	if (pause->autoneg && !(r8152_mdio_read(tp, MII_BMCR) & BMCR_ANENABLE)) {
9054 		ret = -EINVAL;
9055 		goto out;
9056 	}
9057 
9058 	if (pause->rx_pause)
9059 		cap |= FLOW_CTRL_RX;
9060 
9061 	if (pause->tx_pause)
9062 		cap |= FLOW_CTRL_TX;
9063 
9064 	old = r8152_mdio_read(tp, MII_ADVERTISE);
9065 	new1 = (old & ~(ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM)) | mii_advertise_flowctrl(cap);
9066 	if (old != new1)
9067 		r8152_mdio_write(tp, MII_ADVERTISE, new1);
9068 
9069 out:
9070 	mutex_unlock(&tp->control);
9071 	usb_autopm_put_interface(tp->intf);
9072 
9073 	return ret;
9074 }
9075 
9076 static const struct ethtool_ops ops = {
9077 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS,
9078 	.get_drvinfo = rtl8152_get_drvinfo,
9079 	.get_link = ethtool_op_get_link,
9080 	.nway_reset = rtl8152_nway_reset,
9081 	.get_msglevel = rtl8152_get_msglevel,
9082 	.set_msglevel = rtl8152_set_msglevel,
9083 	.get_wol = rtl8152_get_wol,
9084 	.set_wol = rtl8152_set_wol,
9085 	.get_strings = rtl8152_get_strings,
9086 	.get_sset_count = rtl8152_get_sset_count,
9087 	.get_ethtool_stats = rtl8152_get_ethtool_stats,
9088 	.get_coalesce = rtl8152_get_coalesce,
9089 	.set_coalesce = rtl8152_set_coalesce,
9090 	.get_eee = rtl_ethtool_get_eee,
9091 	.set_eee = rtl_ethtool_set_eee,
9092 	.get_link_ksettings = rtl8152_get_link_ksettings,
9093 	.set_link_ksettings = rtl8152_set_link_ksettings,
9094 	.get_tunable = rtl8152_get_tunable,
9095 	.set_tunable = rtl8152_set_tunable,
9096 	.get_ringparam = rtl8152_get_ringparam,
9097 	.set_ringparam = rtl8152_set_ringparam,
9098 	.get_pauseparam = rtl8152_get_pauseparam,
9099 	.set_pauseparam = rtl8152_set_pauseparam,
9100 };
9101 
9102 static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
9103 {
9104 	struct r8152 *tp = netdev_priv(netdev);
9105 	struct mii_ioctl_data *data = if_mii(rq);
9106 	int res;
9107 
9108 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
9109 		return -ENODEV;
9110 
9111 	res = usb_autopm_get_interface(tp->intf);
9112 	if (res < 0)
9113 		goto out;
9114 
9115 	switch (cmd) {
9116 	case SIOCGMIIPHY:
9117 		data->phy_id = R8152_PHY_ID; /* Internal PHY */
9118 		break;
9119 
9120 	case SIOCGMIIREG:
9121 		mutex_lock(&tp->control);
9122 		data->val_out = r8152_mdio_read(tp, data->reg_num);
9123 		mutex_unlock(&tp->control);
9124 		break;
9125 
9126 	case SIOCSMIIREG:
9127 		if (!capable(CAP_NET_ADMIN)) {
9128 			res = -EPERM;
9129 			break;
9130 		}
9131 		mutex_lock(&tp->control);
9132 		r8152_mdio_write(tp, data->reg_num, data->val_in);
9133 		mutex_unlock(&tp->control);
9134 		break;
9135 
9136 	default:
9137 		res = -EOPNOTSUPP;
9138 	}
9139 
9140 	usb_autopm_put_interface(tp->intf);
9141 
9142 out:
9143 	return res;
9144 }
9145 
9146 static int rtl8152_change_mtu(struct net_device *dev, int new_mtu)
9147 {
9148 	struct r8152 *tp = netdev_priv(dev);
9149 	int ret;
9150 
9151 	switch (tp->version) {
9152 	case RTL_VER_01:
9153 	case RTL_VER_02:
9154 	case RTL_VER_07:
9155 		dev->mtu = new_mtu;
9156 		return 0;
9157 	default:
9158 		break;
9159 	}
9160 
9161 	ret = usb_autopm_get_interface(tp->intf);
9162 	if (ret < 0)
9163 		return ret;
9164 
9165 	mutex_lock(&tp->control);
9166 
9167 	dev->mtu = new_mtu;
9168 
9169 	if (netif_running(dev)) {
9170 		if (tp->rtl_ops.change_mtu)
9171 			tp->rtl_ops.change_mtu(tp);
9172 
9173 		if (netif_carrier_ok(dev)) {
9174 			netif_stop_queue(dev);
9175 			napi_disable(&tp->napi);
9176 			tasklet_disable(&tp->tx_tl);
9177 			tp->rtl_ops.disable(tp);
9178 			tp->rtl_ops.enable(tp);
9179 			rtl_start_rx(tp);
9180 			tasklet_enable(&tp->tx_tl);
9181 			napi_enable(&tp->napi);
9182 			rtl8152_set_rx_mode(dev);
9183 			netif_wake_queue(dev);
9184 		}
9185 	}
9186 
9187 	mutex_unlock(&tp->control);
9188 
9189 	usb_autopm_put_interface(tp->intf);
9190 
9191 	return ret;
9192 }
9193 
9194 static const struct net_device_ops rtl8152_netdev_ops = {
9195 	.ndo_open		= rtl8152_open,
9196 	.ndo_stop		= rtl8152_close,
9197 	.ndo_eth_ioctl		= rtl8152_ioctl,
9198 	.ndo_start_xmit		= rtl8152_start_xmit,
9199 	.ndo_tx_timeout		= rtl8152_tx_timeout,
9200 	.ndo_set_features	= rtl8152_set_features,
9201 	.ndo_set_rx_mode	= rtl8152_set_rx_mode,
9202 	.ndo_set_mac_address	= rtl8152_set_mac_address,
9203 	.ndo_change_mtu		= rtl8152_change_mtu,
9204 	.ndo_validate_addr	= eth_validate_addr,
9205 	.ndo_features_check	= rtl8152_features_check,
9206 };
9207 
9208 static void rtl8152_unload(struct r8152 *tp)
9209 {
9210 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
9211 		return;
9212 
9213 	if (tp->version != RTL_VER_01)
9214 		r8152_power_cut_en(tp, true);
9215 }
9216 
9217 static void rtl8153_unload(struct r8152 *tp)
9218 {
9219 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
9220 		return;
9221 
9222 	r8153_power_cut_en(tp, false);
9223 }
9224 
9225 static void rtl8153b_unload(struct r8152 *tp)
9226 {
9227 	if (test_bit(RTL8152_UNPLUG, &tp->flags))
9228 		return;
9229 
9230 	r8153b_power_cut_en(tp, false);
9231 }
9232 
9233 static int rtl_ops_init(struct r8152 *tp)
9234 {
9235 	struct rtl_ops *ops = &tp->rtl_ops;
9236 	int ret = 0;
9237 
9238 	switch (tp->version) {
9239 	case RTL_VER_01:
9240 	case RTL_VER_02:
9241 	case RTL_VER_07:
9242 		ops->init		= r8152b_init;
9243 		ops->enable		= rtl8152_enable;
9244 		ops->disable		= rtl8152_disable;
9245 		ops->up			= rtl8152_up;
9246 		ops->down		= rtl8152_down;
9247 		ops->unload		= rtl8152_unload;
9248 		ops->eee_get		= r8152_get_eee;
9249 		ops->eee_set		= r8152_set_eee;
9250 		ops->in_nway		= rtl8152_in_nway;
9251 		ops->hw_phy_cfg		= r8152b_hw_phy_cfg;
9252 		ops->autosuspend_en	= rtl_runtime_suspend_enable;
9253 		tp->rx_buf_sz		= 16 * 1024;
9254 		tp->eee_en		= true;
9255 		tp->eee_adv		= MDIO_EEE_100TX;
9256 		break;
9257 
9258 	case RTL_VER_03:
9259 	case RTL_VER_04:
9260 	case RTL_VER_05:
9261 	case RTL_VER_06:
9262 		ops->init		= r8153_init;
9263 		ops->enable		= rtl8153_enable;
9264 		ops->disable		= rtl8153_disable;
9265 		ops->up			= rtl8153_up;
9266 		ops->down		= rtl8153_down;
9267 		ops->unload		= rtl8153_unload;
9268 		ops->eee_get		= r8153_get_eee;
9269 		ops->eee_set		= r8152_set_eee;
9270 		ops->in_nway		= rtl8153_in_nway;
9271 		ops->hw_phy_cfg		= r8153_hw_phy_cfg;
9272 		ops->autosuspend_en	= rtl8153_runtime_enable;
9273 		ops->change_mtu		= rtl8153_change_mtu;
9274 		if (tp->udev->speed < USB_SPEED_SUPER)
9275 			tp->rx_buf_sz	= 16 * 1024;
9276 		else
9277 			tp->rx_buf_sz	= 32 * 1024;
9278 		tp->eee_en		= true;
9279 		tp->eee_adv		= MDIO_EEE_1000T | MDIO_EEE_100TX;
9280 		break;
9281 
9282 	case RTL_VER_08:
9283 	case RTL_VER_09:
9284 		ops->init		= r8153b_init;
9285 		ops->enable		= rtl8153_enable;
9286 		ops->disable		= rtl8153_disable;
9287 		ops->up			= rtl8153b_up;
9288 		ops->down		= rtl8153b_down;
9289 		ops->unload		= rtl8153b_unload;
9290 		ops->eee_get		= r8153_get_eee;
9291 		ops->eee_set		= r8152_set_eee;
9292 		ops->in_nway		= rtl8153_in_nway;
9293 		ops->hw_phy_cfg		= r8153b_hw_phy_cfg;
9294 		ops->autosuspend_en	= rtl8153b_runtime_enable;
9295 		ops->change_mtu		= rtl8153_change_mtu;
9296 		tp->rx_buf_sz		= 32 * 1024;
9297 		tp->eee_en		= true;
9298 		tp->eee_adv		= MDIO_EEE_1000T | MDIO_EEE_100TX;
9299 		break;
9300 
9301 	case RTL_VER_11:
9302 		tp->eee_en		= true;
9303 		tp->eee_adv		= MDIO_EEE_1000T | MDIO_EEE_100TX;
9304 		fallthrough;
9305 	case RTL_VER_10:
9306 		ops->init		= r8156_init;
9307 		ops->enable		= rtl8156_enable;
9308 		ops->disable		= rtl8153_disable;
9309 		ops->up			= rtl8156_up;
9310 		ops->down		= rtl8156_down;
9311 		ops->unload		= rtl8153_unload;
9312 		ops->eee_get		= r8153_get_eee;
9313 		ops->eee_set		= r8152_set_eee;
9314 		ops->in_nway		= rtl8153_in_nway;
9315 		ops->hw_phy_cfg		= r8156_hw_phy_cfg;
9316 		ops->autosuspend_en	= rtl8156_runtime_enable;
9317 		ops->change_mtu		= rtl8156_change_mtu;
9318 		tp->rx_buf_sz		= 48 * 1024;
9319 		tp->support_2500full	= 1;
9320 		break;
9321 
9322 	case RTL_VER_12:
9323 	case RTL_VER_13:
9324 		tp->support_2500full	= 1;
9325 		fallthrough;
9326 	case RTL_VER_15:
9327 		tp->eee_en		= true;
9328 		tp->eee_adv		= MDIO_EEE_1000T | MDIO_EEE_100TX;
9329 		ops->init		= r8156b_init;
9330 		ops->enable		= rtl8156b_enable;
9331 		ops->disable		= rtl8153_disable;
9332 		ops->up			= rtl8156_up;
9333 		ops->down		= rtl8156_down;
9334 		ops->unload		= rtl8153_unload;
9335 		ops->eee_get		= r8153_get_eee;
9336 		ops->eee_set		= r8152_set_eee;
9337 		ops->in_nway		= rtl8153_in_nway;
9338 		ops->hw_phy_cfg		= r8156b_hw_phy_cfg;
9339 		ops->autosuspend_en	= rtl8156_runtime_enable;
9340 		ops->change_mtu		= rtl8156_change_mtu;
9341 		tp->rx_buf_sz		= 48 * 1024;
9342 		break;
9343 
9344 	case RTL_VER_14:
9345 		ops->init		= r8153c_init;
9346 		ops->enable		= rtl8153_enable;
9347 		ops->disable		= rtl8153_disable;
9348 		ops->up			= rtl8153c_up;
9349 		ops->down		= rtl8153b_down;
9350 		ops->unload		= rtl8153_unload;
9351 		ops->eee_get		= r8153_get_eee;
9352 		ops->eee_set		= r8152_set_eee;
9353 		ops->in_nway		= rtl8153_in_nway;
9354 		ops->hw_phy_cfg		= r8153c_hw_phy_cfg;
9355 		ops->autosuspend_en	= rtl8153c_runtime_enable;
9356 		ops->change_mtu		= rtl8153c_change_mtu;
9357 		tp->rx_buf_sz		= 32 * 1024;
9358 		tp->eee_en		= true;
9359 		tp->eee_adv		= MDIO_EEE_1000T | MDIO_EEE_100TX;
9360 		break;
9361 
9362 	default:
9363 		ret = -ENODEV;
9364 		dev_err(&tp->intf->dev, "Unknown Device\n");
9365 		break;
9366 	}
9367 
9368 	return ret;
9369 }
9370 
9371 #define FIRMWARE_8153A_2	"rtl_nic/rtl8153a-2.fw"
9372 #define FIRMWARE_8153A_3	"rtl_nic/rtl8153a-3.fw"
9373 #define FIRMWARE_8153A_4	"rtl_nic/rtl8153a-4.fw"
9374 #define FIRMWARE_8153B_2	"rtl_nic/rtl8153b-2.fw"
9375 #define FIRMWARE_8153C_1	"rtl_nic/rtl8153c-1.fw"
9376 #define FIRMWARE_8156A_2	"rtl_nic/rtl8156a-2.fw"
9377 #define FIRMWARE_8156B_2	"rtl_nic/rtl8156b-2.fw"
9378 
9379 MODULE_FIRMWARE(FIRMWARE_8153A_2);
9380 MODULE_FIRMWARE(FIRMWARE_8153A_3);
9381 MODULE_FIRMWARE(FIRMWARE_8153A_4);
9382 MODULE_FIRMWARE(FIRMWARE_8153B_2);
9383 MODULE_FIRMWARE(FIRMWARE_8153C_1);
9384 MODULE_FIRMWARE(FIRMWARE_8156A_2);
9385 MODULE_FIRMWARE(FIRMWARE_8156B_2);
9386 
9387 static int rtl_fw_init(struct r8152 *tp)
9388 {
9389 	struct rtl_fw *rtl_fw = &tp->rtl_fw;
9390 
9391 	switch (tp->version) {
9392 	case RTL_VER_04:
9393 		rtl_fw->fw_name		= FIRMWARE_8153A_2;
9394 		rtl_fw->pre_fw		= r8153_pre_firmware_1;
9395 		rtl_fw->post_fw		= r8153_post_firmware_1;
9396 		break;
9397 	case RTL_VER_05:
9398 		rtl_fw->fw_name		= FIRMWARE_8153A_3;
9399 		rtl_fw->pre_fw		= r8153_pre_firmware_2;
9400 		rtl_fw->post_fw		= r8153_post_firmware_2;
9401 		break;
9402 	case RTL_VER_06:
9403 		rtl_fw->fw_name		= FIRMWARE_8153A_4;
9404 		rtl_fw->post_fw		= r8153_post_firmware_3;
9405 		break;
9406 	case RTL_VER_09:
9407 		rtl_fw->fw_name		= FIRMWARE_8153B_2;
9408 		rtl_fw->pre_fw		= r8153b_pre_firmware_1;
9409 		rtl_fw->post_fw		= r8153b_post_firmware_1;
9410 		break;
9411 	case RTL_VER_11:
9412 		rtl_fw->fw_name		= FIRMWARE_8156A_2;
9413 		rtl_fw->post_fw		= r8156a_post_firmware_1;
9414 		break;
9415 	case RTL_VER_13:
9416 	case RTL_VER_15:
9417 		rtl_fw->fw_name		= FIRMWARE_8156B_2;
9418 		break;
9419 	case RTL_VER_14:
9420 		rtl_fw->fw_name		= FIRMWARE_8153C_1;
9421 		rtl_fw->pre_fw		= r8153b_pre_firmware_1;
9422 		rtl_fw->post_fw		= r8153c_post_firmware_1;
9423 		break;
9424 	default:
9425 		break;
9426 	}
9427 
9428 	return 0;
9429 }
9430 
9431 u8 rtl8152_get_version(struct usb_interface *intf)
9432 {
9433 	struct usb_device *udev = interface_to_usbdev(intf);
9434 	u32 ocp_data = 0;
9435 	__le32 *tmp;
9436 	u8 version;
9437 	int ret;
9438 
9439 	tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
9440 	if (!tmp)
9441 		return 0;
9442 
9443 	ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
9444 			      RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
9445 			      PLA_TCR0, MCU_TYPE_PLA, tmp, sizeof(*tmp), 500);
9446 	if (ret > 0)
9447 		ocp_data = (__le32_to_cpu(*tmp) >> 16) & VERSION_MASK;
9448 
9449 	kfree(tmp);
9450 
9451 	switch (ocp_data) {
9452 	case 0x4c00:
9453 		version = RTL_VER_01;
9454 		break;
9455 	case 0x4c10:
9456 		version = RTL_VER_02;
9457 		break;
9458 	case 0x5c00:
9459 		version = RTL_VER_03;
9460 		break;
9461 	case 0x5c10:
9462 		version = RTL_VER_04;
9463 		break;
9464 	case 0x5c20:
9465 		version = RTL_VER_05;
9466 		break;
9467 	case 0x5c30:
9468 		version = RTL_VER_06;
9469 		break;
9470 	case 0x4800:
9471 		version = RTL_VER_07;
9472 		break;
9473 	case 0x6000:
9474 		version = RTL_VER_08;
9475 		break;
9476 	case 0x6010:
9477 		version = RTL_VER_09;
9478 		break;
9479 	case 0x7010:
9480 		version = RTL_TEST_01;
9481 		break;
9482 	case 0x7020:
9483 		version = RTL_VER_10;
9484 		break;
9485 	case 0x7030:
9486 		version = RTL_VER_11;
9487 		break;
9488 	case 0x7400:
9489 		version = RTL_VER_12;
9490 		break;
9491 	case 0x7410:
9492 		version = RTL_VER_13;
9493 		break;
9494 	case 0x6400:
9495 		version = RTL_VER_14;
9496 		break;
9497 	case 0x7420:
9498 		version = RTL_VER_15;
9499 		break;
9500 	default:
9501 		version = RTL_VER_UNKNOWN;
9502 		dev_info(&intf->dev, "Unknown version 0x%04x\n", ocp_data);
9503 		break;
9504 	}
9505 
9506 	dev_dbg(&intf->dev, "Detected version 0x%04x\n", version);
9507 
9508 	return version;
9509 }
9510 EXPORT_SYMBOL_GPL(rtl8152_get_version);
9511 
9512 static int rtl8152_probe(struct usb_interface *intf,
9513 			 const struct usb_device_id *id)
9514 {
9515 	struct usb_device *udev = interface_to_usbdev(intf);
9516 	u8 version = rtl8152_get_version(intf);
9517 	struct r8152 *tp;
9518 	struct net_device *netdev;
9519 	int ret;
9520 
9521 	if (version == RTL_VER_UNKNOWN)
9522 		return -ENODEV;
9523 
9524 	if (!rtl_vendor_mode(intf))
9525 		return -ENODEV;
9526 
9527 	usb_reset_device(udev);
9528 	netdev = alloc_etherdev(sizeof(struct r8152));
9529 	if (!netdev) {
9530 		dev_err(&intf->dev, "Out of memory\n");
9531 		return -ENOMEM;
9532 	}
9533 
9534 	SET_NETDEV_DEV(netdev, &intf->dev);
9535 	tp = netdev_priv(netdev);
9536 	tp->msg_enable = 0x7FFF;
9537 
9538 	tp->udev = udev;
9539 	tp->netdev = netdev;
9540 	tp->intf = intf;
9541 	tp->version = version;
9542 
9543 	tp->pipe_ctrl_in = usb_rcvctrlpipe(udev, 0);
9544 	tp->pipe_ctrl_out = usb_sndctrlpipe(udev, 0);
9545 	tp->pipe_in = usb_rcvbulkpipe(udev, 1);
9546 	tp->pipe_out = usb_sndbulkpipe(udev, 2);
9547 	tp->pipe_intr = usb_rcvintpipe(udev, 3);
9548 
9549 	switch (version) {
9550 	case RTL_VER_01:
9551 	case RTL_VER_02:
9552 	case RTL_VER_07:
9553 		tp->mii.supports_gmii = 0;
9554 		break;
9555 	default:
9556 		tp->mii.supports_gmii = 1;
9557 		break;
9558 	}
9559 
9560 	ret = rtl_ops_init(tp);
9561 	if (ret)
9562 		goto out;
9563 
9564 	rtl_fw_init(tp);
9565 
9566 	mutex_init(&tp->control);
9567 	INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
9568 	INIT_DELAYED_WORK(&tp->hw_phy_work, rtl_hw_phy_work_func_t);
9569 	tasklet_setup(&tp->tx_tl, bottom_half);
9570 	tasklet_disable(&tp->tx_tl);
9571 
9572 	netdev->netdev_ops = &rtl8152_netdev_ops;
9573 	netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
9574 
9575 	netdev->features |= NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
9576 			    NETIF_F_TSO | NETIF_F_FRAGLIST | NETIF_F_IPV6_CSUM |
9577 			    NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_RX |
9578 			    NETIF_F_HW_VLAN_CTAG_TX;
9579 	netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
9580 			      NETIF_F_TSO | NETIF_F_FRAGLIST |
9581 			      NETIF_F_IPV6_CSUM | NETIF_F_TSO6 |
9582 			      NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX;
9583 	netdev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
9584 				NETIF_F_HIGHDMA | NETIF_F_FRAGLIST |
9585 				NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
9586 
9587 	if (tp->version == RTL_VER_01) {
9588 		netdev->features &= ~NETIF_F_RXCSUM;
9589 		netdev->hw_features &= ~NETIF_F_RXCSUM;
9590 	}
9591 
9592 	if (le16_to_cpu(udev->descriptor.idVendor) == VENDOR_ID_LENOVO) {
9593 		switch (le16_to_cpu(udev->descriptor.idProduct)) {
9594 		case DEVICE_ID_THINKPAD_THUNDERBOLT3_DOCK_GEN2:
9595 		case DEVICE_ID_THINKPAD_USB_C_DOCK_GEN2:
9596 			tp->lenovo_macpassthru = 1;
9597 		}
9598 	}
9599 
9600 	if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x3011 && udev->serial &&
9601 	    (!strcmp(udev->serial, "000001000000") ||
9602 	     !strcmp(udev->serial, "000002000000"))) {
9603 		dev_info(&udev->dev, "Dell TB16 Dock, disable RX aggregation");
9604 		tp->dell_tb_rx_agg_bug = 1;
9605 	}
9606 
9607 	netdev->ethtool_ops = &ops;
9608 	netif_set_gso_max_size(netdev, RTL_LIMITED_TSO_SIZE);
9609 
9610 	/* MTU range: 68 - 1500 or 9194 */
9611 	netdev->min_mtu = ETH_MIN_MTU;
9612 	switch (tp->version) {
9613 	case RTL_VER_03:
9614 	case RTL_VER_04:
9615 	case RTL_VER_05:
9616 	case RTL_VER_06:
9617 	case RTL_VER_08:
9618 	case RTL_VER_09:
9619 	case RTL_VER_14:
9620 		netdev->max_mtu = size_to_mtu(9 * 1024);
9621 		break;
9622 	case RTL_VER_10:
9623 	case RTL_VER_11:
9624 		netdev->max_mtu = size_to_mtu(15 * 1024);
9625 		break;
9626 	case RTL_VER_12:
9627 	case RTL_VER_13:
9628 	case RTL_VER_15:
9629 		netdev->max_mtu = size_to_mtu(16 * 1024);
9630 		break;
9631 	case RTL_VER_01:
9632 	case RTL_VER_02:
9633 	case RTL_VER_07:
9634 	default:
9635 		netdev->max_mtu = ETH_DATA_LEN;
9636 		break;
9637 	}
9638 
9639 	tp->mii.dev = netdev;
9640 	tp->mii.mdio_read = read_mii_word;
9641 	tp->mii.mdio_write = write_mii_word;
9642 	tp->mii.phy_id_mask = 0x3f;
9643 	tp->mii.reg_num_mask = 0x1f;
9644 	tp->mii.phy_id = R8152_PHY_ID;
9645 
9646 	tp->autoneg = AUTONEG_ENABLE;
9647 	tp->speed = SPEED_100;
9648 	tp->advertising = RTL_ADVERTISED_10_HALF | RTL_ADVERTISED_10_FULL |
9649 			  RTL_ADVERTISED_100_HALF | RTL_ADVERTISED_100_FULL;
9650 	if (tp->mii.supports_gmii) {
9651 		if (tp->support_2500full &&
9652 		    tp->udev->speed >= USB_SPEED_SUPER) {
9653 			tp->speed = SPEED_2500;
9654 			tp->advertising |= RTL_ADVERTISED_2500_FULL;
9655 		} else {
9656 			tp->speed = SPEED_1000;
9657 		}
9658 		tp->advertising |= RTL_ADVERTISED_1000_FULL;
9659 	}
9660 	tp->duplex = DUPLEX_FULL;
9661 
9662 	tp->rx_copybreak = RTL8152_RXFG_HEADSZ;
9663 	tp->rx_pending = 10 * RTL8152_MAX_RX;
9664 
9665 	intf->needs_remote_wakeup = 1;
9666 
9667 	if (!rtl_can_wakeup(tp))
9668 		__rtl_set_wol(tp, 0);
9669 	else
9670 		tp->saved_wolopts = __rtl_get_wol(tp);
9671 
9672 	tp->rtl_ops.init(tp);
9673 #if IS_BUILTIN(CONFIG_USB_RTL8152)
9674 	/* Retry in case request_firmware() is not ready yet. */
9675 	tp->rtl_fw.retry = true;
9676 #endif
9677 	queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0);
9678 	set_ethernet_addr(tp, false);
9679 
9680 	usb_set_intfdata(intf, tp);
9681 
9682 	if (tp->support_2500full)
9683 		netif_napi_add(netdev, &tp->napi, r8152_poll, 256);
9684 	else
9685 		netif_napi_add(netdev, &tp->napi, r8152_poll, 64);
9686 
9687 	ret = register_netdev(netdev);
9688 	if (ret != 0) {
9689 		dev_err(&intf->dev, "couldn't register the device\n");
9690 		goto out1;
9691 	}
9692 
9693 	if (tp->saved_wolopts)
9694 		device_set_wakeup_enable(&udev->dev, true);
9695 	else
9696 		device_set_wakeup_enable(&udev->dev, false);
9697 
9698 	netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION);
9699 
9700 	return 0;
9701 
9702 out1:
9703 	tasklet_kill(&tp->tx_tl);
9704 	usb_set_intfdata(intf, NULL);
9705 out:
9706 	free_netdev(netdev);
9707 	return ret;
9708 }
9709 
9710 static void rtl8152_disconnect(struct usb_interface *intf)
9711 {
9712 	struct r8152 *tp = usb_get_intfdata(intf);
9713 
9714 	usb_set_intfdata(intf, NULL);
9715 	if (tp) {
9716 		rtl_set_unplug(tp);
9717 
9718 		unregister_netdev(tp->netdev);
9719 		tasklet_kill(&tp->tx_tl);
9720 		cancel_delayed_work_sync(&tp->hw_phy_work);
9721 		if (tp->rtl_ops.unload)
9722 			tp->rtl_ops.unload(tp);
9723 		rtl8152_release_firmware(tp);
9724 		free_netdev(tp->netdev);
9725 	}
9726 }
9727 
9728 #define REALTEK_USB_DEVICE(vend, prod)	{ \
9729 	USB_DEVICE_INTERFACE_CLASS(vend, prod, USB_CLASS_VENDOR_SPEC), \
9730 }, \
9731 { \
9732 	USB_DEVICE_AND_INTERFACE_INFO(vend, prod, USB_CLASS_COMM, \
9733 			USB_CDC_SUBCLASS_ETHERNET, USB_CDC_PROTO_NONE), \
9734 }
9735 
9736 /* table of devices that work with this driver */
9737 static const struct usb_device_id rtl8152_table[] = {
9738 	/* Realtek */
9739 	REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8050),
9740 	REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8053),
9741 	REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8152),
9742 	REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8153),
9743 	REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8155),
9744 	REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8156),
9745 
9746 	/* Microsoft */
9747 	REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07ab),
9748 	REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07c6),
9749 	REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x0927),
9750 	REALTEK_USB_DEVICE(VENDOR_ID_SAMSUNG, 0xa101),
9751 	REALTEK_USB_DEVICE(VENDOR_ID_LENOVO,  0x304f),
9752 	REALTEK_USB_DEVICE(VENDOR_ID_LENOVO,  0x3062),
9753 	REALTEK_USB_DEVICE(VENDOR_ID_LENOVO,  0x3069),
9754 	REALTEK_USB_DEVICE(VENDOR_ID_LENOVO,  0x3082),
9755 	REALTEK_USB_DEVICE(VENDOR_ID_LENOVO,  0x7205),
9756 	REALTEK_USB_DEVICE(VENDOR_ID_LENOVO,  0x720c),
9757 	REALTEK_USB_DEVICE(VENDOR_ID_LENOVO,  0x7214),
9758 	REALTEK_USB_DEVICE(VENDOR_ID_LENOVO,  0x721e),
9759 	REALTEK_USB_DEVICE(VENDOR_ID_LENOVO,  0xa387),
9760 	REALTEK_USB_DEVICE(VENDOR_ID_LINKSYS, 0x0041),
9761 	REALTEK_USB_DEVICE(VENDOR_ID_NVIDIA,  0x09ff),
9762 	REALTEK_USB_DEVICE(VENDOR_ID_TPLINK,  0x0601),
9763 	{}
9764 };
9765 
9766 MODULE_DEVICE_TABLE(usb, rtl8152_table);
9767 
9768 static struct usb_driver rtl8152_driver = {
9769 	.name =		MODULENAME,
9770 	.id_table =	rtl8152_table,
9771 	.probe =	rtl8152_probe,
9772 	.disconnect =	rtl8152_disconnect,
9773 	.suspend =	rtl8152_suspend,
9774 	.resume =	rtl8152_resume,
9775 	.reset_resume =	rtl8152_reset_resume,
9776 	.pre_reset =	rtl8152_pre_reset,
9777 	.post_reset =	rtl8152_post_reset,
9778 	.supports_autosuspend = 1,
9779 	.disable_hub_initiated_lpm = 1,
9780 };
9781 
9782 module_usb_driver(rtl8152_driver);
9783 
9784 MODULE_AUTHOR(DRIVER_AUTHOR);
9785 MODULE_DESCRIPTION(DRIVER_DESC);
9786 MODULE_LICENSE("GPL");
9787 MODULE_VERSION(DRIVER_VERSION);
9788