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