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