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