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