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