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