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