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