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