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