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