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