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