1 /*
2 * Copyright (c) 2014 Realtek Semiconductor Corp. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * version 2 as published by the Free Software Foundation.
7 *
8 */
9
10 #include <linux/signal.h>
11 #include <linux/slab.h>
12 #include <linux/module.h>
13 #include <linux/netdevice.h>
14 #include <linux/etherdevice.h>
15 #include <linux/mii.h>
16 #include <linux/ethtool.h>
17 #include <linux/usb.h>
18 #include <linux/crc32.h>
19 #include <linux/if_vlan.h>
20 #include <linux/uaccess.h>
21 #include <linux/list.h>
22 #include <linux/ip.h>
23 #include <linux/ipv6.h>
24 #include <net/ip6_checksum.h>
25 #include <uapi/linux/mdio.h>
26 #include <linux/mdio.h>
27 #include <linux/usb/cdc.h>
28 #include <linux/suspend.h>
29 #include <linux/acpi.h>
30
31 /* Information for net-next */
32 #define NETNEXT_VERSION "09"
33
34 /* Information for net */
35 #define NET_VERSION "9"
36
37 #define DRIVER_VERSION "v1." NETNEXT_VERSION "." NET_VERSION
38 #define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>"
39 #define DRIVER_DESC "Realtek RTL8152/RTL8153 Based USB Ethernet Adapters"
40 #define MODULENAME "r8152"
41
42 #define R8152_PHY_ID 32
43
44 #define PLA_IDR 0xc000
45 #define PLA_RCR 0xc010
46 #define PLA_RMS 0xc016
47 #define PLA_RXFIFO_CTRL0 0xc0a0
48 #define PLA_RXFIFO_CTRL1 0xc0a4
49 #define PLA_RXFIFO_CTRL2 0xc0a8
50 #define PLA_DMY_REG0 0xc0b0
51 #define PLA_FMC 0xc0b4
52 #define PLA_CFG_WOL 0xc0b6
53 #define PLA_TEREDO_CFG 0xc0bc
54 #define PLA_TEREDO_WAKE_BASE 0xc0c4
55 #define PLA_MAR 0xcd00
56 #define PLA_BACKUP 0xd000
57 #define PAL_BDC_CR 0xd1a0
58 #define PLA_TEREDO_TIMER 0xd2cc
59 #define PLA_REALWOW_TIMER 0xd2e8
60 #define PLA_EFUSE_DATA 0xdd00
61 #define PLA_EFUSE_CMD 0xdd02
62 #define PLA_LEDSEL 0xdd90
63 #define PLA_LED_FEATURE 0xdd92
64 #define PLA_PHYAR 0xde00
65 #define PLA_BOOT_CTRL 0xe004
66 #define PLA_GPHY_INTR_IMR 0xe022
67 #define PLA_EEE_CR 0xe040
68 #define PLA_EEEP_CR 0xe080
69 #define PLA_MAC_PWR_CTRL 0xe0c0
70 #define PLA_MAC_PWR_CTRL2 0xe0ca
71 #define PLA_MAC_PWR_CTRL3 0xe0cc
72 #define PLA_MAC_PWR_CTRL4 0xe0ce
73 #define PLA_WDT6_CTRL 0xe428
74 #define PLA_TCR0 0xe610
75 #define PLA_TCR1 0xe612
76 #define PLA_MTPS 0xe615
77 #define PLA_TXFIFO_CTRL 0xe618
78 #define PLA_RSTTALLY 0xe800
79 #define PLA_CR 0xe813
80 #define PLA_CRWECR 0xe81c
81 #define PLA_CONFIG12 0xe81e /* CONFIG1, CONFIG2 */
82 #define PLA_CONFIG34 0xe820 /* CONFIG3, CONFIG4 */
83 #define PLA_CONFIG5 0xe822
84 #define PLA_PHY_PWR 0xe84c
85 #define PLA_OOB_CTRL 0xe84f
86 #define PLA_CPCR 0xe854
87 #define PLA_MISC_0 0xe858
88 #define PLA_MISC_1 0xe85a
89 #define PLA_OCP_GPHY_BASE 0xe86c
90 #define PLA_TALLYCNT 0xe890
91 #define PLA_SFF_STS_7 0xe8de
92 #define PLA_PHYSTATUS 0xe908
93 #define PLA_BP_BA 0xfc26
94 #define PLA_BP_0 0xfc28
95 #define PLA_BP_1 0xfc2a
96 #define PLA_BP_2 0xfc2c
97 #define PLA_BP_3 0xfc2e
98 #define PLA_BP_4 0xfc30
99 #define PLA_BP_5 0xfc32
100 #define PLA_BP_6 0xfc34
101 #define PLA_BP_7 0xfc36
102 #define PLA_BP_EN 0xfc38
103
104 #define USB_USB2PHY 0xb41e
105 #define USB_SSPHYLINK2 0xb428
106 #define USB_U2P3_CTRL 0xb460
107 #define USB_CSR_DUMMY1 0xb464
108 #define USB_CSR_DUMMY2 0xb466
109 #define USB_DEV_STAT 0xb808
110 #define USB_CONNECT_TIMER 0xcbf8
111 #define USB_MSC_TIMER 0xcbfc
112 #define USB_BURST_SIZE 0xcfc0
113 #define USB_LPM_CONFIG 0xcfd8
114 #define USB_USB_CTRL 0xd406
115 #define USB_PHY_CTRL 0xd408
116 #define USB_TX_AGG 0xd40a
117 #define USB_RX_BUF_TH 0xd40c
118 #define USB_USB_TIMER 0xd428
119 #define USB_RX_EARLY_TIMEOUT 0xd42c
120 #define USB_RX_EARLY_SIZE 0xd42e
121 #define USB_PM_CTRL_STATUS 0xd432 /* RTL8153A */
122 #define USB_RX_EXTRA_AGGR_TMR 0xd432 /* RTL8153B */
123 #define USB_TX_DMA 0xd434
124 #define USB_UPT_RXDMA_OWN 0xd437
125 #define USB_TOLERANCE 0xd490
126 #define USB_LPM_CTRL 0xd41a
127 #define USB_BMU_RESET 0xd4b0
128 #define USB_U1U2_TIMER 0xd4da
129 #define USB_UPS_CTRL 0xd800
130 #define USB_POWER_CUT 0xd80a
131 #define USB_MISC_0 0xd81a
132 #define USB_AFE_CTRL2 0xd824
133 #define USB_UPS_CFG 0xd842
134 #define USB_UPS_FLAGS 0xd848
135 #define USB_WDT11_CTRL 0xe43c
136 #define USB_BP_BA 0xfc26
137 #define USB_BP_0 0xfc28
138 #define USB_BP_1 0xfc2a
139 #define USB_BP_2 0xfc2c
140 #define USB_BP_3 0xfc2e
141 #define USB_BP_4 0xfc30
142 #define USB_BP_5 0xfc32
143 #define USB_BP_6 0xfc34
144 #define USB_BP_7 0xfc36
145 #define USB_BP_EN 0xfc38
146 #define USB_BP_8 0xfc38
147 #define USB_BP_9 0xfc3a
148 #define USB_BP_10 0xfc3c
149 #define USB_BP_11 0xfc3e
150 #define USB_BP_12 0xfc40
151 #define USB_BP_13 0xfc42
152 #define USB_BP_14 0xfc44
153 #define USB_BP_15 0xfc46
154 #define USB_BP2_EN 0xfc48
155
156 /* OCP Registers */
157 #define OCP_ALDPS_CONFIG 0x2010
158 #define OCP_EEE_CONFIG1 0x2080
159 #define OCP_EEE_CONFIG2 0x2092
160 #define OCP_EEE_CONFIG3 0x2094
161 #define OCP_BASE_MII 0xa400
162 #define OCP_EEE_AR 0xa41a
163 #define OCP_EEE_DATA 0xa41c
164 #define OCP_PHY_STATUS 0xa420
165 #define OCP_NCTL_CFG 0xa42c
166 #define OCP_POWER_CFG 0xa430
167 #define OCP_EEE_CFG 0xa432
168 #define OCP_SRAM_ADDR 0xa436
169 #define OCP_SRAM_DATA 0xa438
170 #define OCP_DOWN_SPEED 0xa442
171 #define OCP_EEE_ABLE 0xa5c4
172 #define OCP_EEE_ADV 0xa5d0
173 #define OCP_EEE_LPABLE 0xa5d2
174 #define OCP_PHY_STATE 0xa708 /* nway state for 8153 */
175 #define OCP_PHY_PATCH_STAT 0xb800
176 #define OCP_PHY_PATCH_CMD 0xb820
177 #define OCP_ADC_IOFFSET 0xbcfc
178 #define OCP_ADC_CFG 0xbc06
179 #define OCP_SYSCLK_CFG 0xc416
180
181 /* SRAM Register */
182 #define SRAM_GREEN_CFG 0x8011
183 #define SRAM_LPF_CFG 0x8012
184 #define SRAM_10M_AMP1 0x8080
185 #define SRAM_10M_AMP2 0x8082
186 #define SRAM_IMPEDANCE 0x8084
187
188 /* PLA_RCR */
189 #define RCR_AAP 0x00000001
190 #define RCR_APM 0x00000002
191 #define RCR_AM 0x00000004
192 #define RCR_AB 0x00000008
193 #define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB)
194
195 /* PLA_RXFIFO_CTRL0 */
196 #define RXFIFO_THR1_NORMAL 0x00080002
197 #define RXFIFO_THR1_OOB 0x01800003
198
199 /* PLA_RXFIFO_CTRL1 */
200 #define RXFIFO_THR2_FULL 0x00000060
201 #define RXFIFO_THR2_HIGH 0x00000038
202 #define RXFIFO_THR2_OOB 0x0000004a
203 #define RXFIFO_THR2_NORMAL 0x00a0
204
205 /* PLA_RXFIFO_CTRL2 */
206 #define RXFIFO_THR3_FULL 0x00000078
207 #define RXFIFO_THR3_HIGH 0x00000048
208 #define RXFIFO_THR3_OOB 0x0000005a
209 #define RXFIFO_THR3_NORMAL 0x0110
210
211 /* PLA_TXFIFO_CTRL */
212 #define TXFIFO_THR_NORMAL 0x00400008
213 #define TXFIFO_THR_NORMAL2 0x01000008
214
215 /* PLA_DMY_REG0 */
216 #define ECM_ALDPS 0x0002
217
218 /* PLA_FMC */
219 #define FMC_FCR_MCU_EN 0x0001
220
221 /* PLA_EEEP_CR */
222 #define EEEP_CR_EEEP_TX 0x0002
223
224 /* PLA_WDT6_CTRL */
225 #define WDT6_SET_MODE 0x0010
226
227 /* PLA_TCR0 */
228 #define TCR0_TX_EMPTY 0x0800
229 #define TCR0_AUTO_FIFO 0x0080
230
231 /* PLA_TCR1 */
232 #define VERSION_MASK 0x7cf0
233
234 /* PLA_MTPS */
235 #define MTPS_JUMBO (12 * 1024 / 64)
236 #define MTPS_DEFAULT (6 * 1024 / 64)
237
238 /* PLA_RSTTALLY */
239 #define TALLY_RESET 0x0001
240
241 /* PLA_CR */
242 #define CR_RST 0x10
243 #define CR_RE 0x08
244 #define CR_TE 0x04
245
246 /* PLA_CRWECR */
247 #define CRWECR_NORAML 0x00
248 #define CRWECR_CONFIG 0xc0
249
250 /* PLA_OOB_CTRL */
251 #define NOW_IS_OOB 0x80
252 #define TXFIFO_EMPTY 0x20
253 #define RXFIFO_EMPTY 0x10
254 #define LINK_LIST_READY 0x02
255 #define DIS_MCU_CLROOB 0x01
256 #define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY)
257
258 /* PLA_MISC_1 */
259 #define RXDY_GATED_EN 0x0008
260
261 /* PLA_SFF_STS_7 */
262 #define RE_INIT_LL 0x8000
263 #define MCU_BORW_EN 0x4000
264
265 /* PLA_CPCR */
266 #define CPCR_RX_VLAN 0x0040
267
268 /* PLA_CFG_WOL */
269 #define MAGIC_EN 0x0001
270
271 /* PLA_TEREDO_CFG */
272 #define TEREDO_SEL 0x8000
273 #define TEREDO_WAKE_MASK 0x7f00
274 #define TEREDO_RS_EVENT_MASK 0x00fe
275 #define OOB_TEREDO_EN 0x0001
276
277 /* PAL_BDC_CR */
278 #define ALDPS_PROXY_MODE 0x0001
279
280 /* PLA_EFUSE_CMD */
281 #define EFUSE_READ_CMD BIT(15)
282 #define EFUSE_DATA_BIT16 BIT(7)
283
284 /* PLA_CONFIG34 */
285 #define LINK_ON_WAKE_EN 0x0010
286 #define LINK_OFF_WAKE_EN 0x0008
287
288 /* PLA_CONFIG5 */
289 #define BWF_EN 0x0040
290 #define MWF_EN 0x0020
291 #define UWF_EN 0x0010
292 #define LAN_WAKE_EN 0x0002
293
294 /* PLA_LED_FEATURE */
295 #define LED_MODE_MASK 0x0700
296
297 /* PLA_PHY_PWR */
298 #define TX_10M_IDLE_EN 0x0080
299 #define PFM_PWM_SWITCH 0x0040
300
301 /* PLA_MAC_PWR_CTRL */
302 #define D3_CLK_GATED_EN 0x00004000
303 #define MCU_CLK_RATIO 0x07010f07
304 #define MCU_CLK_RATIO_MASK 0x0f0f0f0f
305 #define ALDPS_SPDWN_RATIO 0x0f87
306
307 /* PLA_MAC_PWR_CTRL2 */
308 #define EEE_SPDWN_RATIO 0x8007
309 #define MAC_CLK_SPDWN_EN BIT(15)
310
311 /* PLA_MAC_PWR_CTRL3 */
312 #define PKT_AVAIL_SPDWN_EN 0x0100
313 #define SUSPEND_SPDWN_EN 0x0004
314 #define U1U2_SPDWN_EN 0x0002
315 #define L1_SPDWN_EN 0x0001
316
317 /* PLA_MAC_PWR_CTRL4 */
318 #define PWRSAVE_SPDWN_EN 0x1000
319 #define RXDV_SPDWN_EN 0x0800
320 #define TX10MIDLE_EN 0x0100
321 #define TP100_SPDWN_EN 0x0020
322 #define TP500_SPDWN_EN 0x0010
323 #define TP1000_SPDWN_EN 0x0008
324 #define EEE_SPDWN_EN 0x0001
325
326 /* PLA_GPHY_INTR_IMR */
327 #define GPHY_STS_MSK 0x0001
328 #define SPEED_DOWN_MSK 0x0002
329 #define SPDWN_RXDV_MSK 0x0004
330 #define SPDWN_LINKCHG_MSK 0x0008
331
332 /* PLA_PHYAR */
333 #define PHYAR_FLAG 0x80000000
334
335 /* PLA_EEE_CR */
336 #define EEE_RX_EN 0x0001
337 #define EEE_TX_EN 0x0002
338
339 /* PLA_BOOT_CTRL */
340 #define AUTOLOAD_DONE 0x0002
341
342 /* USB_USB2PHY */
343 #define USB2PHY_SUSPEND 0x0001
344 #define USB2PHY_L1 0x0002
345
346 /* USB_SSPHYLINK2 */
347 #define pwd_dn_scale_mask 0x3ffe
348 #define pwd_dn_scale(x) ((x) << 1)
349
350 /* USB_CSR_DUMMY1 */
351 #define DYNAMIC_BURST 0x0001
352
353 /* USB_CSR_DUMMY2 */
354 #define EP4_FULL_FC 0x0001
355
356 /* USB_DEV_STAT */
357 #define STAT_SPEED_MASK 0x0006
358 #define STAT_SPEED_HIGH 0x0000
359 #define STAT_SPEED_FULL 0x0002
360
361 /* USB_LPM_CONFIG */
362 #define LPM_U1U2_EN BIT(0)
363
364 /* USB_TX_AGG */
365 #define TX_AGG_MAX_THRESHOLD 0x03
366
367 /* USB_RX_BUF_TH */
368 #define RX_THR_SUPPER 0x0c350180
369 #define RX_THR_HIGH 0x7a120180
370 #define RX_THR_SLOW 0xffff0180
371 #define RX_THR_B 0x00010001
372
373 /* USB_TX_DMA */
374 #define TEST_MODE_DISABLE 0x00000001
375 #define TX_SIZE_ADJUST1 0x00000100
376
377 /* USB_BMU_RESET */
378 #define BMU_RESET_EP_IN 0x01
379 #define BMU_RESET_EP_OUT 0x02
380
381 /* USB_UPT_RXDMA_OWN */
382 #define OWN_UPDATE BIT(0)
383 #define OWN_CLEAR BIT(1)
384
385 /* USB_UPS_CTRL */
386 #define POWER_CUT 0x0100
387
388 /* USB_PM_CTRL_STATUS */
389 #define RESUME_INDICATE 0x0001
390
391 /* USB_USB_CTRL */
392 #define RX_AGG_DISABLE 0x0010
393 #define RX_ZERO_EN 0x0080
394
395 /* USB_U2P3_CTRL */
396 #define U2P3_ENABLE 0x0001
397
398 /* USB_POWER_CUT */
399 #define PWR_EN 0x0001
400 #define PHASE2_EN 0x0008
401 #define UPS_EN BIT(4)
402 #define USP_PREWAKE BIT(5)
403
404 /* USB_MISC_0 */
405 #define PCUT_STATUS 0x0001
406
407 /* USB_RX_EARLY_TIMEOUT */
408 #define COALESCE_SUPER 85000U
409 #define COALESCE_HIGH 250000U
410 #define COALESCE_SLOW 524280U
411
412 /* USB_WDT11_CTRL */
413 #define TIMER11_EN 0x0001
414
415 /* USB_LPM_CTRL */
416 /* bit 4 ~ 5: fifo empty boundary */
417 #define FIFO_EMPTY_1FB 0x30 /* 0x1fb * 64 = 32448 bytes */
418 /* bit 2 ~ 3: LMP timer */
419 #define LPM_TIMER_MASK 0x0c
420 #define LPM_TIMER_500MS 0x04 /* 500 ms */
421 #define LPM_TIMER_500US 0x0c /* 500 us */
422 #define ROK_EXIT_LPM 0x02
423
424 /* USB_AFE_CTRL2 */
425 #define SEN_VAL_MASK 0xf800
426 #define SEN_VAL_NORMAL 0xa000
427 #define SEL_RXIDLE 0x0100
428
429 /* USB_UPS_CFG */
430 #define SAW_CNT_1MS_MASK 0x0fff
431
432 /* USB_UPS_FLAGS */
433 #define UPS_FLAGS_R_TUNE BIT(0)
434 #define UPS_FLAGS_EN_10M_CKDIV BIT(1)
435 #define UPS_FLAGS_250M_CKDIV BIT(2)
436 #define UPS_FLAGS_EN_ALDPS BIT(3)
437 #define UPS_FLAGS_CTAP_SHORT_DIS BIT(4)
438 #define UPS_FLAGS_SPEED_MASK (0xf << 16)
439 #define ups_flags_speed(x) ((x) << 16)
440 #define UPS_FLAGS_EN_EEE BIT(20)
441 #define UPS_FLAGS_EN_500M_EEE BIT(21)
442 #define UPS_FLAGS_EN_EEE_CKDIV BIT(22)
443 #define UPS_FLAGS_EEE_PLLOFF_GIGA BIT(24)
444 #define UPS_FLAGS_EEE_CMOD_LV_EN BIT(25)
445 #define UPS_FLAGS_EN_GREEN BIT(26)
446 #define UPS_FLAGS_EN_FLOW_CTR BIT(27)
447
448 enum spd_duplex {
449 NWAY_10M_HALF = 1,
450 NWAY_10M_FULL,
451 NWAY_100M_HALF,
452 NWAY_100M_FULL,
453 NWAY_1000M_FULL,
454 FORCE_10M_HALF,
455 FORCE_10M_FULL,
456 FORCE_100M_HALF,
457 FORCE_100M_FULL,
458 };
459
460 /* OCP_ALDPS_CONFIG */
461 #define ENPWRSAVE 0x8000
462 #define ENPDNPS 0x0200
463 #define LINKENA 0x0100
464 #define DIS_SDSAVE 0x0010
465
466 /* OCP_PHY_STATUS */
467 #define PHY_STAT_MASK 0x0007
468 #define PHY_STAT_EXT_INIT 2
469 #define PHY_STAT_LAN_ON 3
470 #define PHY_STAT_PWRDN 5
471
472 /* OCP_NCTL_CFG */
473 #define PGA_RETURN_EN BIT(1)
474
475 /* OCP_POWER_CFG */
476 #define EEE_CLKDIV_EN 0x8000
477 #define EN_ALDPS 0x0004
478 #define EN_10M_PLLOFF 0x0001
479
480 /* OCP_EEE_CONFIG1 */
481 #define RG_TXLPI_MSK_HFDUP 0x8000
482 #define RG_MATCLR_EN 0x4000
483 #define EEE_10_CAP 0x2000
484 #define EEE_NWAY_EN 0x1000
485 #define TX_QUIET_EN 0x0200
486 #define RX_QUIET_EN 0x0100
487 #define sd_rise_time_mask 0x0070
488 #define sd_rise_time(x) (min(x, 7) << 4) /* bit 4 ~ 6 */
489 #define RG_RXLPI_MSK_HFDUP 0x0008
490 #define SDFALLTIME 0x0007 /* bit 0 ~ 2 */
491
492 /* OCP_EEE_CONFIG2 */
493 #define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */
494 #define RG_DACQUIET_EN 0x0400
495 #define RG_LDVQUIET_EN 0x0200
496 #define RG_CKRSEL 0x0020
497 #define RG_EEEPRG_EN 0x0010
498
499 /* OCP_EEE_CONFIG3 */
500 #define fast_snr_mask 0xff80
501 #define fast_snr(x) (min(x, 0x1ff) << 7) /* bit 7 ~ 15 */
502 #define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */
503 #define MSK_PH 0x0006 /* bit 0 ~ 3 */
504
505 /* OCP_EEE_AR */
506 /* bit[15:14] function */
507 #define FUN_ADDR 0x0000
508 #define FUN_DATA 0x4000
509 /* bit[4:0] device addr */
510
511 /* OCP_EEE_CFG */
512 #define CTAP_SHORT_EN 0x0040
513 #define EEE10_EN 0x0010
514
515 /* OCP_DOWN_SPEED */
516 #define EN_EEE_CMODE BIT(14)
517 #define EN_EEE_1000 BIT(13)
518 #define EN_EEE_100 BIT(12)
519 #define EN_10M_CLKDIV BIT(11)
520 #define EN_10M_BGOFF 0x0080
521
522 /* OCP_PHY_STATE */
523 #define TXDIS_STATE 0x01
524 #define ABD_STATE 0x02
525
526 /* OCP_PHY_PATCH_STAT */
527 #define PATCH_READY BIT(6)
528
529 /* OCP_PHY_PATCH_CMD */
530 #define PATCH_REQUEST BIT(4)
531
532 /* OCP_ADC_CFG */
533 #define CKADSEL_L 0x0100
534 #define ADC_EN 0x0080
535 #define EN_EMI_L 0x0040
536
537 /* OCP_SYSCLK_CFG */
538 #define clk_div_expo(x) (min(x, 5) << 8)
539
540 /* SRAM_GREEN_CFG */
541 #define GREEN_ETH_EN BIT(15)
542 #define R_TUNE_EN BIT(11)
543
544 /* SRAM_LPF_CFG */
545 #define LPF_AUTO_TUNE 0x8000
546
547 /* SRAM_10M_AMP1 */
548 #define GDAC_IB_UPALL 0x0008
549
550 /* SRAM_10M_AMP2 */
551 #define AMP_DN 0x0200
552
553 /* SRAM_IMPEDANCE */
554 #define RX_DRIVING_MASK 0x6000
555
556 /* MAC PASSTHRU */
557 #define AD_MASK 0xfee0
558 #define EFUSE 0xcfdb
559 #define PASS_THRU_MASK 0x1
560
561 enum rtl_register_content {
562 _1000bps = 0x10,
563 _100bps = 0x08,
564 _10bps = 0x04,
565 LINK_STATUS = 0x02,
566 FULL_DUP = 0x01,
567 };
568
569 #define RTL8152_MAX_TX 4
570 #define RTL8152_MAX_RX 10
571 #define INTBUFSIZE 2
572 #define TX_ALIGN 4
573 #define RX_ALIGN 8
574
575 #define INTR_LINK 0x0004
576
577 #define RTL8152_REQT_READ 0xc0
578 #define RTL8152_REQT_WRITE 0x40
579 #define RTL8152_REQ_GET_REGS 0x05
580 #define RTL8152_REQ_SET_REGS 0x05
581
582 #define BYTE_EN_DWORD 0xff
583 #define BYTE_EN_WORD 0x33
584 #define BYTE_EN_BYTE 0x11
585 #define BYTE_EN_SIX_BYTES 0x3f
586 #define BYTE_EN_START_MASK 0x0f
587 #define BYTE_EN_END_MASK 0xf0
588
589 #define RTL8153_MAX_PACKET 9216 /* 9K */
590 #define RTL8153_MAX_MTU (RTL8153_MAX_PACKET - VLAN_ETH_HLEN - \
591 ETH_FCS_LEN)
592 #define RTL8152_RMS (VLAN_ETH_FRAME_LEN + ETH_FCS_LEN)
593 #define RTL8153_RMS RTL8153_MAX_PACKET
594 #define RTL8152_TX_TIMEOUT (5 * HZ)
595 #define RTL8152_NAPI_WEIGHT 64
596 #define rx_reserved_size(x) ((x) + VLAN_ETH_HLEN + ETH_FCS_LEN + \
597 sizeof(struct rx_desc) + RX_ALIGN)
598
599 /* rtl8152 flags */
600 enum rtl8152_flags {
601 RTL8152_UNPLUG = 0,
602 RTL8152_SET_RX_MODE,
603 WORK_ENABLE,
604 RTL8152_LINK_CHG,
605 SELECTIVE_SUSPEND,
606 PHY_RESET,
607 SCHEDULE_NAPI,
608 GREEN_ETHERNET,
609 DELL_TB_RX_AGG_BUG,
610 };
611
612 /* Define these values to match your device */
613 #define VENDOR_ID_REALTEK 0x0bda
614 #define VENDOR_ID_MICROSOFT 0x045e
615 #define VENDOR_ID_SAMSUNG 0x04e8
616 #define VENDOR_ID_LENOVO 0x17ef
617 #define VENDOR_ID_LINKSYS 0x13b1
618 #define VENDOR_ID_NVIDIA 0x0955
619 #define VENDOR_ID_TPLINK 0x2357
620
621 #define MCU_TYPE_PLA 0x0100
622 #define MCU_TYPE_USB 0x0000
623
624 struct tally_counter {
625 __le64 tx_packets;
626 __le64 rx_packets;
627 __le64 tx_errors;
628 __le32 rx_errors;
629 __le16 rx_missed;
630 __le16 align_errors;
631 __le32 tx_one_collision;
632 __le32 tx_multi_collision;
633 __le64 rx_unicast;
634 __le64 rx_broadcast;
635 __le32 rx_multicast;
636 __le16 tx_aborted;
637 __le16 tx_underrun;
638 };
639
640 struct rx_desc {
641 __le32 opts1;
642 #define RX_LEN_MASK 0x7fff
643
644 __le32 opts2;
645 #define RD_UDP_CS BIT(23)
646 #define RD_TCP_CS BIT(22)
647 #define RD_IPV6_CS BIT(20)
648 #define RD_IPV4_CS BIT(19)
649
650 __le32 opts3;
651 #define IPF BIT(23) /* IP checksum fail */
652 #define UDPF BIT(22) /* UDP checksum fail */
653 #define TCPF BIT(21) /* TCP checksum fail */
654 #define RX_VLAN_TAG BIT(16)
655
656 __le32 opts4;
657 __le32 opts5;
658 __le32 opts6;
659 };
660
661 struct tx_desc {
662 __le32 opts1;
663 #define TX_FS BIT(31) /* First segment of a packet */
664 #define TX_LS BIT(30) /* Final segment of a packet */
665 #define GTSENDV4 BIT(28)
666 #define GTSENDV6 BIT(27)
667 #define GTTCPHO_SHIFT 18
668 #define GTTCPHO_MAX 0x7fU
669 #define TX_LEN_MAX 0x3ffffU
670
671 __le32 opts2;
672 #define UDP_CS BIT(31) /* Calculate UDP/IP checksum */
673 #define TCP_CS BIT(30) /* Calculate TCP/IP checksum */
674 #define IPV4_CS BIT(29) /* Calculate IPv4 checksum */
675 #define IPV6_CS BIT(28) /* Calculate IPv6 checksum */
676 #define MSS_SHIFT 17
677 #define MSS_MAX 0x7ffU
678 #define TCPHO_SHIFT 17
679 #define TCPHO_MAX 0x7ffU
680 #define TX_VLAN_TAG BIT(16)
681 };
682
683 struct r8152;
684
685 struct rx_agg {
686 struct list_head list;
687 struct urb *urb;
688 struct r8152 *context;
689 void *buffer;
690 void *head;
691 };
692
693 struct tx_agg {
694 struct list_head list;
695 struct urb *urb;
696 struct r8152 *context;
697 void *buffer;
698 void *head;
699 u32 skb_num;
700 u32 skb_len;
701 };
702
703 struct r8152 {
704 unsigned long flags;
705 struct usb_device *udev;
706 struct napi_struct napi;
707 struct usb_interface *intf;
708 struct net_device *netdev;
709 struct urb *intr_urb;
710 struct tx_agg tx_info[RTL8152_MAX_TX];
711 struct rx_agg rx_info[RTL8152_MAX_RX];
712 struct list_head rx_done, tx_free;
713 struct sk_buff_head tx_queue, rx_queue;
714 spinlock_t rx_lock, tx_lock;
715 struct delayed_work schedule, hw_phy_work;
716 struct mii_if_info mii;
717 struct mutex control; /* use for hw setting */
718 #ifdef CONFIG_PM_SLEEP
719 struct notifier_block pm_notifier;
720 #endif
721
722 struct rtl_ops {
723 void (*init)(struct r8152 *);
724 int (*enable)(struct r8152 *);
725 void (*disable)(struct r8152 *);
726 void (*up)(struct r8152 *);
727 void (*down)(struct r8152 *);
728 void (*unload)(struct r8152 *);
729 int (*eee_get)(struct r8152 *, struct ethtool_eee *);
730 int (*eee_set)(struct r8152 *, struct ethtool_eee *);
731 bool (*in_nway)(struct r8152 *);
732 void (*hw_phy_cfg)(struct r8152 *);
733 void (*autosuspend_en)(struct r8152 *tp, bool enable);
734 } rtl_ops;
735
736 int intr_interval;
737 u32 saved_wolopts;
738 u32 msg_enable;
739 u32 tx_qlen;
740 u32 coalesce;
741 u16 ocp_base;
742 u16 speed;
743 u8 *intr_buff;
744 u8 version;
745 u8 duplex;
746 u8 autoneg;
747 };
748
749 enum rtl_version {
750 RTL_VER_UNKNOWN = 0,
751 RTL_VER_01,
752 RTL_VER_02,
753 RTL_VER_03,
754 RTL_VER_04,
755 RTL_VER_05,
756 RTL_VER_06,
757 RTL_VER_07,
758 RTL_VER_08,
759 RTL_VER_09,
760 RTL_VER_MAX
761 };
762
763 enum tx_csum_stat {
764 TX_CSUM_SUCCESS = 0,
765 TX_CSUM_TSO,
766 TX_CSUM_NONE
767 };
768
769 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
770 * The RTL chips use a 64 element hash table based on the Ethernet CRC.
771 */
772 static const int multicast_filter_limit = 32;
773 static unsigned int agg_buf_sz = 16384;
774
775 #define RTL_LIMITED_TSO_SIZE (agg_buf_sz - sizeof(struct tx_desc) - \
776 VLAN_ETH_HLEN - ETH_FCS_LEN)
777
778 static
get_registers(struct r8152 * tp,u16 value,u16 index,u16 size,void * data)779 int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
780 {
781 int ret;
782 void *tmp;
783
784 tmp = kmalloc(size, GFP_KERNEL);
785 if (!tmp)
786 return -ENOMEM;
787
788 ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0),
789 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
790 value, index, tmp, size, 500);
791 if (ret < 0)
792 memset(data, 0xff, size);
793 else
794 memcpy(data, tmp, size);
795
796 kfree(tmp);
797
798 return ret;
799 }
800
801 static
set_registers(struct r8152 * tp,u16 value,u16 index,u16 size,void * data)802 int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
803 {
804 int ret;
805 void *tmp;
806
807 tmp = kmemdup(data, size, GFP_KERNEL);
808 if (!tmp)
809 return -ENOMEM;
810
811 ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0),
812 RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
813 value, index, tmp, size, 500);
814
815 kfree(tmp);
816
817 return ret;
818 }
819
generic_ocp_read(struct r8152 * tp,u16 index,u16 size,void * data,u16 type)820 static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
821 void *data, u16 type)
822 {
823 u16 limit = 64;
824 int ret = 0;
825
826 if (test_bit(RTL8152_UNPLUG, &tp->flags))
827 return -ENODEV;
828
829 /* both size and indix must be 4 bytes align */
830 if ((size & 3) || !size || (index & 3) || !data)
831 return -EPERM;
832
833 if ((u32)index + (u32)size > 0xffff)
834 return -EPERM;
835
836 while (size) {
837 if (size > limit) {
838 ret = get_registers(tp, index, type, limit, data);
839 if (ret < 0)
840 break;
841
842 index += limit;
843 data += limit;
844 size -= limit;
845 } else {
846 ret = get_registers(tp, index, type, size, data);
847 if (ret < 0)
848 break;
849
850 index += size;
851 data += size;
852 size = 0;
853 break;
854 }
855 }
856
857 if (ret == -ENODEV)
858 set_bit(RTL8152_UNPLUG, &tp->flags);
859
860 return ret;
861 }
862
generic_ocp_write(struct r8152 * tp,u16 index,u16 byteen,u16 size,void * data,u16 type)863 static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
864 u16 size, void *data, u16 type)
865 {
866 int ret;
867 u16 byteen_start, byteen_end, byen;
868 u16 limit = 512;
869
870 if (test_bit(RTL8152_UNPLUG, &tp->flags))
871 return -ENODEV;
872
873 /* both size and indix must be 4 bytes align */
874 if ((size & 3) || !size || (index & 3) || !data)
875 return -EPERM;
876
877 if ((u32)index + (u32)size > 0xffff)
878 return -EPERM;
879
880 byteen_start = byteen & BYTE_EN_START_MASK;
881 byteen_end = byteen & BYTE_EN_END_MASK;
882
883 byen = byteen_start | (byteen_start << 4);
884 ret = set_registers(tp, index, type | byen, 4, data);
885 if (ret < 0)
886 goto error1;
887
888 index += 4;
889 data += 4;
890 size -= 4;
891
892 if (size) {
893 size -= 4;
894
895 while (size) {
896 if (size > limit) {
897 ret = set_registers(tp, index,
898 type | BYTE_EN_DWORD,
899 limit, data);
900 if (ret < 0)
901 goto error1;
902
903 index += limit;
904 data += limit;
905 size -= limit;
906 } else {
907 ret = set_registers(tp, index,
908 type | BYTE_EN_DWORD,
909 size, data);
910 if (ret < 0)
911 goto error1;
912
913 index += size;
914 data += size;
915 size = 0;
916 break;
917 }
918 }
919
920 byen = byteen_end | (byteen_end >> 4);
921 ret = set_registers(tp, index, type | byen, 4, data);
922 if (ret < 0)
923 goto error1;
924 }
925
926 error1:
927 if (ret == -ENODEV)
928 set_bit(RTL8152_UNPLUG, &tp->flags);
929
930 return ret;
931 }
932
933 static inline
pla_ocp_read(struct r8152 * tp,u16 index,u16 size,void * data)934 int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
935 {
936 return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
937 }
938
939 static inline
pla_ocp_write(struct r8152 * tp,u16 index,u16 byteen,u16 size,void * data)940 int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
941 {
942 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
943 }
944
945 static inline
usb_ocp_write(struct r8152 * tp,u16 index,u16 byteen,u16 size,void * data)946 int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
947 {
948 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
949 }
950
ocp_read_dword(struct r8152 * tp,u16 type,u16 index)951 static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
952 {
953 __le32 data;
954
955 generic_ocp_read(tp, index, sizeof(data), &data, type);
956
957 return __le32_to_cpu(data);
958 }
959
ocp_write_dword(struct r8152 * tp,u16 type,u16 index,u32 data)960 static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
961 {
962 __le32 tmp = __cpu_to_le32(data);
963
964 generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
965 }
966
ocp_read_word(struct r8152 * tp,u16 type,u16 index)967 static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
968 {
969 u32 data;
970 __le32 tmp;
971 u16 byen = BYTE_EN_WORD;
972 u8 shift = index & 2;
973
974 index &= ~3;
975 byen <<= shift;
976
977 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type | byen);
978
979 data = __le32_to_cpu(tmp);
980 data >>= (shift * 8);
981 data &= 0xffff;
982
983 return (u16)data;
984 }
985
ocp_write_word(struct r8152 * tp,u16 type,u16 index,u32 data)986 static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
987 {
988 u32 mask = 0xffff;
989 __le32 tmp;
990 u16 byen = BYTE_EN_WORD;
991 u8 shift = index & 2;
992
993 data &= mask;
994
995 if (index & 2) {
996 byen <<= shift;
997 mask <<= (shift * 8);
998 data <<= (shift * 8);
999 index &= ~3;
1000 }
1001
1002 tmp = __cpu_to_le32(data);
1003
1004 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
1005 }
1006
ocp_read_byte(struct r8152 * tp,u16 type,u16 index)1007 static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
1008 {
1009 u32 data;
1010 __le32 tmp;
1011 u8 shift = index & 3;
1012
1013 index &= ~3;
1014
1015 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
1016
1017 data = __le32_to_cpu(tmp);
1018 data >>= (shift * 8);
1019 data &= 0xff;
1020
1021 return (u8)data;
1022 }
1023
ocp_write_byte(struct r8152 * tp,u16 type,u16 index,u32 data)1024 static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
1025 {
1026 u32 mask = 0xff;
1027 __le32 tmp;
1028 u16 byen = BYTE_EN_BYTE;
1029 u8 shift = index & 3;
1030
1031 data &= mask;
1032
1033 if (index & 3) {
1034 byen <<= shift;
1035 mask <<= (shift * 8);
1036 data <<= (shift * 8);
1037 index &= ~3;
1038 }
1039
1040 tmp = __cpu_to_le32(data);
1041
1042 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
1043 }
1044
ocp_reg_read(struct r8152 * tp,u16 addr)1045 static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
1046 {
1047 u16 ocp_base, ocp_index;
1048
1049 ocp_base = addr & 0xf000;
1050 if (ocp_base != tp->ocp_base) {
1051 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
1052 tp->ocp_base = ocp_base;
1053 }
1054
1055 ocp_index = (addr & 0x0fff) | 0xb000;
1056 return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
1057 }
1058
ocp_reg_write(struct r8152 * tp,u16 addr,u16 data)1059 static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
1060 {
1061 u16 ocp_base, ocp_index;
1062
1063 ocp_base = addr & 0xf000;
1064 if (ocp_base != tp->ocp_base) {
1065 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
1066 tp->ocp_base = ocp_base;
1067 }
1068
1069 ocp_index = (addr & 0x0fff) | 0xb000;
1070 ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
1071 }
1072
r8152_mdio_write(struct r8152 * tp,u32 reg_addr,u32 value)1073 static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
1074 {
1075 ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
1076 }
1077
r8152_mdio_read(struct r8152 * tp,u32 reg_addr)1078 static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
1079 {
1080 return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2);
1081 }
1082
sram_write(struct r8152 * tp,u16 addr,u16 data)1083 static void sram_write(struct r8152 *tp, u16 addr, u16 data)
1084 {
1085 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
1086 ocp_reg_write(tp, OCP_SRAM_DATA, data);
1087 }
1088
sram_read(struct r8152 * tp,u16 addr)1089 static u16 sram_read(struct r8152 *tp, u16 addr)
1090 {
1091 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
1092 return ocp_reg_read(tp, OCP_SRAM_DATA);
1093 }
1094
read_mii_word(struct net_device * netdev,int phy_id,int reg)1095 static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
1096 {
1097 struct r8152 *tp = netdev_priv(netdev);
1098 int ret;
1099
1100 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1101 return -ENODEV;
1102
1103 if (phy_id != R8152_PHY_ID)
1104 return -EINVAL;
1105
1106 ret = r8152_mdio_read(tp, reg);
1107
1108 return ret;
1109 }
1110
1111 static
write_mii_word(struct net_device * netdev,int phy_id,int reg,int val)1112 void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
1113 {
1114 struct r8152 *tp = netdev_priv(netdev);
1115
1116 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1117 return;
1118
1119 if (phy_id != R8152_PHY_ID)
1120 return;
1121
1122 r8152_mdio_write(tp, reg, val);
1123 }
1124
1125 static int
1126 r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);
1127
rtl8152_set_mac_address(struct net_device * netdev,void * p)1128 static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
1129 {
1130 struct r8152 *tp = netdev_priv(netdev);
1131 struct sockaddr *addr = p;
1132 int ret = -EADDRNOTAVAIL;
1133
1134 if (!is_valid_ether_addr(addr->sa_data))
1135 goto out1;
1136
1137 ret = usb_autopm_get_interface(tp->intf);
1138 if (ret < 0)
1139 goto out1;
1140
1141 mutex_lock(&tp->control);
1142
1143 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1144
1145 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
1146 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
1147 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
1148
1149 mutex_unlock(&tp->control);
1150
1151 usb_autopm_put_interface(tp->intf);
1152 out1:
1153 return ret;
1154 }
1155
1156 /* Devices containing RTL8153-AD can support a persistent
1157 * host system provided MAC address.
1158 * Examples of this are Dell TB15 and Dell WD15 docks
1159 */
vendor_mac_passthru_addr_read(struct r8152 * tp,struct sockaddr * sa)1160 static int vendor_mac_passthru_addr_read(struct r8152 *tp, struct sockaddr *sa)
1161 {
1162 acpi_status status;
1163 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1164 union acpi_object *obj;
1165 int ret = -EINVAL;
1166 u32 ocp_data;
1167 unsigned char buf[6];
1168
1169 /* test for -AD variant of RTL8153 */
1170 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
1171 if ((ocp_data & AD_MASK) != 0x1000)
1172 return -ENODEV;
1173
1174 /* test for MAC address pass-through bit */
1175 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, EFUSE);
1176 if ((ocp_data & PASS_THRU_MASK) != 1)
1177 return -ENODEV;
1178
1179 /* returns _AUXMAC_#AABBCCDDEEFF# */
1180 status = acpi_evaluate_object(NULL, "\\_SB.AMAC", NULL, &buffer);
1181 obj = (union acpi_object *)buffer.pointer;
1182 if (!ACPI_SUCCESS(status))
1183 return -ENODEV;
1184 if (obj->type != ACPI_TYPE_BUFFER || obj->string.length != 0x17) {
1185 netif_warn(tp, probe, tp->netdev,
1186 "Invalid buffer for pass-thru MAC addr: (%d, %d)\n",
1187 obj->type, obj->string.length);
1188 goto amacout;
1189 }
1190 if (strncmp(obj->string.pointer, "_AUXMAC_#", 9) != 0 ||
1191 strncmp(obj->string.pointer + 0x15, "#", 1) != 0) {
1192 netif_warn(tp, probe, tp->netdev,
1193 "Invalid header when reading pass-thru MAC addr\n");
1194 goto amacout;
1195 }
1196 ret = hex2bin(buf, obj->string.pointer + 9, 6);
1197 if (!(ret == 0 && is_valid_ether_addr(buf))) {
1198 netif_warn(tp, probe, tp->netdev,
1199 "Invalid MAC for pass-thru MAC addr: %d, %pM\n",
1200 ret, buf);
1201 ret = -EINVAL;
1202 goto amacout;
1203 }
1204 memcpy(sa->sa_data, buf, 6);
1205 ether_addr_copy(tp->netdev->dev_addr, sa->sa_data);
1206 netif_info(tp, probe, tp->netdev,
1207 "Using pass-thru MAC addr %pM\n", sa->sa_data);
1208
1209 amacout:
1210 kfree(obj);
1211 return ret;
1212 }
1213
set_ethernet_addr(struct r8152 * tp)1214 static int set_ethernet_addr(struct r8152 *tp)
1215 {
1216 struct net_device *dev = tp->netdev;
1217 struct sockaddr sa;
1218 int ret;
1219
1220 if (tp->version == RTL_VER_01) {
1221 ret = pla_ocp_read(tp, PLA_IDR, 8, sa.sa_data);
1222 } else {
1223 /* if this is not an RTL8153-AD, no eFuse mac pass thru set,
1224 * or system doesn't provide valid _SB.AMAC this will be
1225 * be expected to non-zero
1226 */
1227 ret = vendor_mac_passthru_addr_read(tp, &sa);
1228 if (ret < 0)
1229 ret = pla_ocp_read(tp, PLA_BACKUP, 8, sa.sa_data);
1230 }
1231
1232 if (ret < 0) {
1233 netif_err(tp, probe, dev, "Get ether addr fail\n");
1234 } else if (!is_valid_ether_addr(sa.sa_data)) {
1235 netif_err(tp, probe, dev, "Invalid ether addr %pM\n",
1236 sa.sa_data);
1237 eth_hw_addr_random(dev);
1238 ether_addr_copy(sa.sa_data, dev->dev_addr);
1239 ret = rtl8152_set_mac_address(dev, &sa);
1240 netif_info(tp, probe, dev, "Random ether addr %pM\n",
1241 sa.sa_data);
1242 } else {
1243 if (tp->version == RTL_VER_01)
1244 ether_addr_copy(dev->dev_addr, sa.sa_data);
1245 else
1246 ret = rtl8152_set_mac_address(dev, &sa);
1247 }
1248
1249 return ret;
1250 }
1251
read_bulk_callback(struct urb * urb)1252 static void read_bulk_callback(struct urb *urb)
1253 {
1254 struct net_device *netdev;
1255 int status = urb->status;
1256 struct rx_agg *agg;
1257 struct r8152 *tp;
1258 unsigned long flags;
1259
1260 agg = urb->context;
1261 if (!agg)
1262 return;
1263
1264 tp = agg->context;
1265 if (!tp)
1266 return;
1267
1268 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1269 return;
1270
1271 if (!test_bit(WORK_ENABLE, &tp->flags))
1272 return;
1273
1274 netdev = tp->netdev;
1275
1276 /* When link down, the driver would cancel all bulks. */
1277 /* This avoid the re-submitting bulk */
1278 if (!netif_carrier_ok(netdev))
1279 return;
1280
1281 usb_mark_last_busy(tp->udev);
1282
1283 switch (status) {
1284 case 0:
1285 if (urb->actual_length < ETH_ZLEN)
1286 break;
1287
1288 spin_lock_irqsave(&tp->rx_lock, flags);
1289 list_add_tail(&agg->list, &tp->rx_done);
1290 spin_unlock_irqrestore(&tp->rx_lock, flags);
1291 napi_schedule(&tp->napi);
1292 return;
1293 case -ESHUTDOWN:
1294 set_bit(RTL8152_UNPLUG, &tp->flags);
1295 netif_device_detach(tp->netdev);
1296 return;
1297 case -ENOENT:
1298 return; /* the urb is in unlink state */
1299 case -ETIME:
1300 if (net_ratelimit())
1301 netdev_warn(netdev, "maybe reset is needed?\n");
1302 break;
1303 default:
1304 if (net_ratelimit())
1305 netdev_warn(netdev, "Rx status %d\n", status);
1306 break;
1307 }
1308
1309 r8152_submit_rx(tp, agg, GFP_ATOMIC);
1310 }
1311
write_bulk_callback(struct urb * urb)1312 static void write_bulk_callback(struct urb *urb)
1313 {
1314 struct net_device_stats *stats;
1315 struct net_device *netdev;
1316 struct tx_agg *agg;
1317 struct r8152 *tp;
1318 unsigned long flags;
1319 int status = urb->status;
1320
1321 agg = urb->context;
1322 if (!agg)
1323 return;
1324
1325 tp = agg->context;
1326 if (!tp)
1327 return;
1328
1329 netdev = tp->netdev;
1330 stats = &netdev->stats;
1331 if (status) {
1332 if (net_ratelimit())
1333 netdev_warn(netdev, "Tx status %d\n", status);
1334 stats->tx_errors += agg->skb_num;
1335 } else {
1336 stats->tx_packets += agg->skb_num;
1337 stats->tx_bytes += agg->skb_len;
1338 }
1339
1340 spin_lock_irqsave(&tp->tx_lock, flags);
1341 list_add_tail(&agg->list, &tp->tx_free);
1342 spin_unlock_irqrestore(&tp->tx_lock, flags);
1343
1344 usb_autopm_put_interface_async(tp->intf);
1345
1346 if (!netif_carrier_ok(netdev))
1347 return;
1348
1349 if (!test_bit(WORK_ENABLE, &tp->flags))
1350 return;
1351
1352 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1353 return;
1354
1355 if (!skb_queue_empty(&tp->tx_queue))
1356 napi_schedule(&tp->napi);
1357 }
1358
intr_callback(struct urb * urb)1359 static void intr_callback(struct urb *urb)
1360 {
1361 struct r8152 *tp;
1362 __le16 *d;
1363 int status = urb->status;
1364 int res;
1365
1366 tp = urb->context;
1367 if (!tp)
1368 return;
1369
1370 if (!test_bit(WORK_ENABLE, &tp->flags))
1371 return;
1372
1373 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1374 return;
1375
1376 switch (status) {
1377 case 0: /* success */
1378 break;
1379 case -ECONNRESET: /* unlink */
1380 case -ESHUTDOWN:
1381 netif_device_detach(tp->netdev);
1382 /* fall through */
1383 case -ENOENT:
1384 case -EPROTO:
1385 netif_info(tp, intr, tp->netdev,
1386 "Stop submitting intr, status %d\n", status);
1387 return;
1388 case -EOVERFLOW:
1389 if (net_ratelimit())
1390 netif_info(tp, intr, tp->netdev,
1391 "intr status -EOVERFLOW\n");
1392 goto resubmit;
1393 /* -EPIPE: should clear the halt */
1394 default:
1395 netif_info(tp, intr, tp->netdev, "intr status %d\n", status);
1396 goto resubmit;
1397 }
1398
1399 d = urb->transfer_buffer;
1400 if (INTR_LINK & __le16_to_cpu(d[0])) {
1401 if (!netif_carrier_ok(tp->netdev)) {
1402 set_bit(RTL8152_LINK_CHG, &tp->flags);
1403 schedule_delayed_work(&tp->schedule, 0);
1404 }
1405 } else {
1406 if (netif_carrier_ok(tp->netdev)) {
1407 netif_stop_queue(tp->netdev);
1408 set_bit(RTL8152_LINK_CHG, &tp->flags);
1409 schedule_delayed_work(&tp->schedule, 0);
1410 }
1411 }
1412
1413 resubmit:
1414 res = usb_submit_urb(urb, GFP_ATOMIC);
1415 if (res == -ENODEV) {
1416 set_bit(RTL8152_UNPLUG, &tp->flags);
1417 netif_device_detach(tp->netdev);
1418 } else if (res) {
1419 netif_err(tp, intr, tp->netdev,
1420 "can't resubmit intr, status %d\n", res);
1421 }
1422 }
1423
rx_agg_align(void * data)1424 static inline void *rx_agg_align(void *data)
1425 {
1426 return (void *)ALIGN((uintptr_t)data, RX_ALIGN);
1427 }
1428
tx_agg_align(void * data)1429 static inline void *tx_agg_align(void *data)
1430 {
1431 return (void *)ALIGN((uintptr_t)data, TX_ALIGN);
1432 }
1433
free_all_mem(struct r8152 * tp)1434 static void free_all_mem(struct r8152 *tp)
1435 {
1436 int i;
1437
1438 for (i = 0; i < RTL8152_MAX_RX; i++) {
1439 usb_free_urb(tp->rx_info[i].urb);
1440 tp->rx_info[i].urb = NULL;
1441
1442 kfree(tp->rx_info[i].buffer);
1443 tp->rx_info[i].buffer = NULL;
1444 tp->rx_info[i].head = NULL;
1445 }
1446
1447 for (i = 0; i < RTL8152_MAX_TX; i++) {
1448 usb_free_urb(tp->tx_info[i].urb);
1449 tp->tx_info[i].urb = NULL;
1450
1451 kfree(tp->tx_info[i].buffer);
1452 tp->tx_info[i].buffer = NULL;
1453 tp->tx_info[i].head = NULL;
1454 }
1455
1456 usb_free_urb(tp->intr_urb);
1457 tp->intr_urb = NULL;
1458
1459 kfree(tp->intr_buff);
1460 tp->intr_buff = NULL;
1461 }
1462
alloc_all_mem(struct r8152 * tp)1463 static int alloc_all_mem(struct r8152 *tp)
1464 {
1465 struct net_device *netdev = tp->netdev;
1466 struct usb_interface *intf = tp->intf;
1467 struct usb_host_interface *alt = intf->cur_altsetting;
1468 struct usb_host_endpoint *ep_intr = alt->endpoint + 2;
1469 struct urb *urb;
1470 int node, i;
1471 u8 *buf;
1472
1473 node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
1474
1475 spin_lock_init(&tp->rx_lock);
1476 spin_lock_init(&tp->tx_lock);
1477 INIT_LIST_HEAD(&tp->tx_free);
1478 INIT_LIST_HEAD(&tp->rx_done);
1479 skb_queue_head_init(&tp->tx_queue);
1480 skb_queue_head_init(&tp->rx_queue);
1481
1482 for (i = 0; i < RTL8152_MAX_RX; i++) {
1483 buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node);
1484 if (!buf)
1485 goto err1;
1486
1487 if (buf != rx_agg_align(buf)) {
1488 kfree(buf);
1489 buf = kmalloc_node(agg_buf_sz + RX_ALIGN, GFP_KERNEL,
1490 node);
1491 if (!buf)
1492 goto err1;
1493 }
1494
1495 urb = usb_alloc_urb(0, GFP_KERNEL);
1496 if (!urb) {
1497 kfree(buf);
1498 goto err1;
1499 }
1500
1501 INIT_LIST_HEAD(&tp->rx_info[i].list);
1502 tp->rx_info[i].context = tp;
1503 tp->rx_info[i].urb = urb;
1504 tp->rx_info[i].buffer = buf;
1505 tp->rx_info[i].head = rx_agg_align(buf);
1506 }
1507
1508 for (i = 0; i < RTL8152_MAX_TX; i++) {
1509 buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node);
1510 if (!buf)
1511 goto err1;
1512
1513 if (buf != tx_agg_align(buf)) {
1514 kfree(buf);
1515 buf = kmalloc_node(agg_buf_sz + TX_ALIGN, GFP_KERNEL,
1516 node);
1517 if (!buf)
1518 goto err1;
1519 }
1520
1521 urb = usb_alloc_urb(0, GFP_KERNEL);
1522 if (!urb) {
1523 kfree(buf);
1524 goto err1;
1525 }
1526
1527 INIT_LIST_HEAD(&tp->tx_info[i].list);
1528 tp->tx_info[i].context = tp;
1529 tp->tx_info[i].urb = urb;
1530 tp->tx_info[i].buffer = buf;
1531 tp->tx_info[i].head = tx_agg_align(buf);
1532
1533 list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
1534 }
1535
1536 tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1537 if (!tp->intr_urb)
1538 goto err1;
1539
1540 tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
1541 if (!tp->intr_buff)
1542 goto err1;
1543
1544 tp->intr_interval = (int)ep_intr->desc.bInterval;
1545 usb_fill_int_urb(tp->intr_urb, tp->udev, usb_rcvintpipe(tp->udev, 3),
1546 tp->intr_buff, INTBUFSIZE, intr_callback,
1547 tp, tp->intr_interval);
1548
1549 return 0;
1550
1551 err1:
1552 free_all_mem(tp);
1553 return -ENOMEM;
1554 }
1555
r8152_get_tx_agg(struct r8152 * tp)1556 static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp)
1557 {
1558 struct tx_agg *agg = NULL;
1559 unsigned long flags;
1560
1561 if (list_empty(&tp->tx_free))
1562 return NULL;
1563
1564 spin_lock_irqsave(&tp->tx_lock, flags);
1565 if (!list_empty(&tp->tx_free)) {
1566 struct list_head *cursor;
1567
1568 cursor = tp->tx_free.next;
1569 list_del_init(cursor);
1570 agg = list_entry(cursor, struct tx_agg, list);
1571 }
1572 spin_unlock_irqrestore(&tp->tx_lock, flags);
1573
1574 return agg;
1575 }
1576
1577 /* r8152_csum_workaround()
1578 * The hw limites the value the transport offset. When the offset is out of the
1579 * range, calculate the checksum by sw.
1580 */
r8152_csum_workaround(struct r8152 * tp,struct sk_buff * skb,struct sk_buff_head * list)1581 static void r8152_csum_workaround(struct r8152 *tp, struct sk_buff *skb,
1582 struct sk_buff_head *list)
1583 {
1584 if (skb_shinfo(skb)->gso_size) {
1585 netdev_features_t features = tp->netdev->features;
1586 struct sk_buff_head seg_list;
1587 struct sk_buff *segs, *nskb;
1588
1589 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
1590 segs = skb_gso_segment(skb, features);
1591 if (IS_ERR(segs) || !segs)
1592 goto drop;
1593
1594 __skb_queue_head_init(&seg_list);
1595
1596 do {
1597 nskb = segs;
1598 segs = segs->next;
1599 nskb->next = NULL;
1600 __skb_queue_tail(&seg_list, nskb);
1601 } while (segs);
1602
1603 skb_queue_splice(&seg_list, list);
1604 dev_kfree_skb(skb);
1605 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1606 if (skb_checksum_help(skb) < 0)
1607 goto drop;
1608
1609 __skb_queue_head(list, skb);
1610 } else {
1611 struct net_device_stats *stats;
1612
1613 drop:
1614 stats = &tp->netdev->stats;
1615 stats->tx_dropped++;
1616 dev_kfree_skb(skb);
1617 }
1618 }
1619
1620 /* msdn_giant_send_check()
1621 * According to the document of microsoft, the TCP Pseudo Header excludes the
1622 * packet length for IPv6 TCP large packets.
1623 */
msdn_giant_send_check(struct sk_buff * skb)1624 static int msdn_giant_send_check(struct sk_buff *skb)
1625 {
1626 const struct ipv6hdr *ipv6h;
1627 struct tcphdr *th;
1628 int ret;
1629
1630 ret = skb_cow_head(skb, 0);
1631 if (ret)
1632 return ret;
1633
1634 ipv6h = ipv6_hdr(skb);
1635 th = tcp_hdr(skb);
1636
1637 th->check = 0;
1638 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
1639
1640 return ret;
1641 }
1642
rtl_tx_vlan_tag(struct tx_desc * desc,struct sk_buff * skb)1643 static inline void rtl_tx_vlan_tag(struct tx_desc *desc, struct sk_buff *skb)
1644 {
1645 if (skb_vlan_tag_present(skb)) {
1646 u32 opts2;
1647
1648 opts2 = TX_VLAN_TAG | swab16(skb_vlan_tag_get(skb));
1649 desc->opts2 |= cpu_to_le32(opts2);
1650 }
1651 }
1652
rtl_rx_vlan_tag(struct rx_desc * desc,struct sk_buff * skb)1653 static inline void rtl_rx_vlan_tag(struct rx_desc *desc, struct sk_buff *skb)
1654 {
1655 u32 opts2 = le32_to_cpu(desc->opts2);
1656
1657 if (opts2 & RX_VLAN_TAG)
1658 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1659 swab16(opts2 & 0xffff));
1660 }
1661
r8152_tx_csum(struct r8152 * tp,struct tx_desc * desc,struct sk_buff * skb,u32 len,u32 transport_offset)1662 static int r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc,
1663 struct sk_buff *skb, u32 len, u32 transport_offset)
1664 {
1665 u32 mss = skb_shinfo(skb)->gso_size;
1666 u32 opts1, opts2 = 0;
1667 int ret = TX_CSUM_SUCCESS;
1668
1669 WARN_ON_ONCE(len > TX_LEN_MAX);
1670
1671 opts1 = len | TX_FS | TX_LS;
1672
1673 if (mss) {
1674 if (transport_offset > GTTCPHO_MAX) {
1675 netif_warn(tp, tx_err, tp->netdev,
1676 "Invalid transport offset 0x%x for TSO\n",
1677 transport_offset);
1678 ret = TX_CSUM_TSO;
1679 goto unavailable;
1680 }
1681
1682 switch (vlan_get_protocol(skb)) {
1683 case htons(ETH_P_IP):
1684 opts1 |= GTSENDV4;
1685 break;
1686
1687 case htons(ETH_P_IPV6):
1688 if (msdn_giant_send_check(skb)) {
1689 ret = TX_CSUM_TSO;
1690 goto unavailable;
1691 }
1692 opts1 |= GTSENDV6;
1693 break;
1694
1695 default:
1696 WARN_ON_ONCE(1);
1697 break;
1698 }
1699
1700 opts1 |= transport_offset << GTTCPHO_SHIFT;
1701 opts2 |= min(mss, MSS_MAX) << MSS_SHIFT;
1702 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1703 u8 ip_protocol;
1704
1705 if (transport_offset > TCPHO_MAX) {
1706 netif_warn(tp, tx_err, tp->netdev,
1707 "Invalid transport offset 0x%x\n",
1708 transport_offset);
1709 ret = TX_CSUM_NONE;
1710 goto unavailable;
1711 }
1712
1713 switch (vlan_get_protocol(skb)) {
1714 case htons(ETH_P_IP):
1715 opts2 |= IPV4_CS;
1716 ip_protocol = ip_hdr(skb)->protocol;
1717 break;
1718
1719 case htons(ETH_P_IPV6):
1720 opts2 |= IPV6_CS;
1721 ip_protocol = ipv6_hdr(skb)->nexthdr;
1722 break;
1723
1724 default:
1725 ip_protocol = IPPROTO_RAW;
1726 break;
1727 }
1728
1729 if (ip_protocol == IPPROTO_TCP)
1730 opts2 |= TCP_CS;
1731 else if (ip_protocol == IPPROTO_UDP)
1732 opts2 |= UDP_CS;
1733 else
1734 WARN_ON_ONCE(1);
1735
1736 opts2 |= transport_offset << TCPHO_SHIFT;
1737 }
1738
1739 desc->opts2 = cpu_to_le32(opts2);
1740 desc->opts1 = cpu_to_le32(opts1);
1741
1742 unavailable:
1743 return ret;
1744 }
1745
r8152_tx_agg_fill(struct r8152 * tp,struct tx_agg * agg)1746 static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg)
1747 {
1748 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
1749 int remain, ret;
1750 u8 *tx_data;
1751
1752 __skb_queue_head_init(&skb_head);
1753 spin_lock(&tx_queue->lock);
1754 skb_queue_splice_init(tx_queue, &skb_head);
1755 spin_unlock(&tx_queue->lock);
1756
1757 tx_data = agg->head;
1758 agg->skb_num = 0;
1759 agg->skb_len = 0;
1760 remain = agg_buf_sz;
1761
1762 while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) {
1763 struct tx_desc *tx_desc;
1764 struct sk_buff *skb;
1765 unsigned int len;
1766 u32 offset;
1767
1768 skb = __skb_dequeue(&skb_head);
1769 if (!skb)
1770 break;
1771
1772 len = skb->len + sizeof(*tx_desc);
1773
1774 if (len > remain) {
1775 __skb_queue_head(&skb_head, skb);
1776 break;
1777 }
1778
1779 tx_data = tx_agg_align(tx_data);
1780 tx_desc = (struct tx_desc *)tx_data;
1781
1782 offset = (u32)skb_transport_offset(skb);
1783
1784 if (r8152_tx_csum(tp, tx_desc, skb, skb->len, offset)) {
1785 r8152_csum_workaround(tp, skb, &skb_head);
1786 continue;
1787 }
1788
1789 rtl_tx_vlan_tag(tx_desc, skb);
1790
1791 tx_data += sizeof(*tx_desc);
1792
1793 len = skb->len;
1794 if (skb_copy_bits(skb, 0, tx_data, len) < 0) {
1795 struct net_device_stats *stats = &tp->netdev->stats;
1796
1797 stats->tx_dropped++;
1798 dev_kfree_skb_any(skb);
1799 tx_data -= sizeof(*tx_desc);
1800 continue;
1801 }
1802
1803 tx_data += len;
1804 agg->skb_len += len;
1805 agg->skb_num += skb_shinfo(skb)->gso_segs ?: 1;
1806
1807 dev_kfree_skb_any(skb);
1808
1809 remain = agg_buf_sz - (int)(tx_agg_align(tx_data) - agg->head);
1810
1811 if (test_bit(DELL_TB_RX_AGG_BUG, &tp->flags))
1812 break;
1813 }
1814
1815 if (!skb_queue_empty(&skb_head)) {
1816 spin_lock(&tx_queue->lock);
1817 skb_queue_splice(&skb_head, tx_queue);
1818 spin_unlock(&tx_queue->lock);
1819 }
1820
1821 netif_tx_lock(tp->netdev);
1822
1823 if (netif_queue_stopped(tp->netdev) &&
1824 skb_queue_len(&tp->tx_queue) < tp->tx_qlen)
1825 netif_wake_queue(tp->netdev);
1826
1827 netif_tx_unlock(tp->netdev);
1828
1829 ret = usb_autopm_get_interface_async(tp->intf);
1830 if (ret < 0)
1831 goto out_tx_fill;
1832
1833 usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2),
1834 agg->head, (int)(tx_data - (u8 *)agg->head),
1835 (usb_complete_t)write_bulk_callback, agg);
1836
1837 ret = usb_submit_urb(agg->urb, GFP_ATOMIC);
1838 if (ret < 0)
1839 usb_autopm_put_interface_async(tp->intf);
1840
1841 out_tx_fill:
1842 return ret;
1843 }
1844
r8152_rx_csum(struct r8152 * tp,struct rx_desc * rx_desc)1845 static u8 r8152_rx_csum(struct r8152 *tp, struct rx_desc *rx_desc)
1846 {
1847 u8 checksum = CHECKSUM_NONE;
1848 u32 opts2, opts3;
1849
1850 if (!(tp->netdev->features & NETIF_F_RXCSUM))
1851 goto return_result;
1852
1853 opts2 = le32_to_cpu(rx_desc->opts2);
1854 opts3 = le32_to_cpu(rx_desc->opts3);
1855
1856 if (opts2 & RD_IPV4_CS) {
1857 if (opts3 & IPF)
1858 checksum = CHECKSUM_NONE;
1859 else if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
1860 checksum = CHECKSUM_UNNECESSARY;
1861 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
1862 checksum = CHECKSUM_UNNECESSARY;
1863 } else if (opts2 & RD_IPV6_CS) {
1864 if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
1865 checksum = CHECKSUM_UNNECESSARY;
1866 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
1867 checksum = CHECKSUM_UNNECESSARY;
1868 }
1869
1870 return_result:
1871 return checksum;
1872 }
1873
rx_bottom(struct r8152 * tp,int budget)1874 static int rx_bottom(struct r8152 *tp, int budget)
1875 {
1876 unsigned long flags;
1877 struct list_head *cursor, *next, rx_queue;
1878 int ret = 0, work_done = 0;
1879 struct napi_struct *napi = &tp->napi;
1880
1881 if (!skb_queue_empty(&tp->rx_queue)) {
1882 while (work_done < budget) {
1883 struct sk_buff *skb = __skb_dequeue(&tp->rx_queue);
1884 struct net_device *netdev = tp->netdev;
1885 struct net_device_stats *stats = &netdev->stats;
1886 unsigned int pkt_len;
1887
1888 if (!skb)
1889 break;
1890
1891 pkt_len = skb->len;
1892 napi_gro_receive(napi, skb);
1893 work_done++;
1894 stats->rx_packets++;
1895 stats->rx_bytes += pkt_len;
1896 }
1897 }
1898
1899 if (list_empty(&tp->rx_done))
1900 goto out1;
1901
1902 INIT_LIST_HEAD(&rx_queue);
1903 spin_lock_irqsave(&tp->rx_lock, flags);
1904 list_splice_init(&tp->rx_done, &rx_queue);
1905 spin_unlock_irqrestore(&tp->rx_lock, flags);
1906
1907 list_for_each_safe(cursor, next, &rx_queue) {
1908 struct rx_desc *rx_desc;
1909 struct rx_agg *agg;
1910 int len_used = 0;
1911 struct urb *urb;
1912 u8 *rx_data;
1913
1914 list_del_init(cursor);
1915
1916 agg = list_entry(cursor, struct rx_agg, list);
1917 urb = agg->urb;
1918 if (urb->actual_length < ETH_ZLEN)
1919 goto submit;
1920
1921 rx_desc = agg->head;
1922 rx_data = agg->head;
1923 len_used += sizeof(struct rx_desc);
1924
1925 while (urb->actual_length > len_used) {
1926 struct net_device *netdev = tp->netdev;
1927 struct net_device_stats *stats = &netdev->stats;
1928 unsigned int pkt_len;
1929 struct sk_buff *skb;
1930
1931 /* limite the skb numbers for rx_queue */
1932 if (unlikely(skb_queue_len(&tp->rx_queue) >= 1000))
1933 break;
1934
1935 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
1936 if (pkt_len < ETH_ZLEN)
1937 break;
1938
1939 len_used += pkt_len;
1940 if (urb->actual_length < len_used)
1941 break;
1942
1943 pkt_len -= ETH_FCS_LEN;
1944 rx_data += sizeof(struct rx_desc);
1945
1946 skb = napi_alloc_skb(napi, pkt_len);
1947 if (!skb) {
1948 stats->rx_dropped++;
1949 goto find_next_rx;
1950 }
1951
1952 skb->ip_summed = r8152_rx_csum(tp, rx_desc);
1953 memcpy(skb->data, rx_data, pkt_len);
1954 skb_put(skb, pkt_len);
1955 skb->protocol = eth_type_trans(skb, netdev);
1956 rtl_rx_vlan_tag(rx_desc, skb);
1957 if (work_done < budget) {
1958 napi_gro_receive(napi, skb);
1959 work_done++;
1960 stats->rx_packets++;
1961 stats->rx_bytes += pkt_len;
1962 } else {
1963 __skb_queue_tail(&tp->rx_queue, skb);
1964 }
1965
1966 find_next_rx:
1967 rx_data = rx_agg_align(rx_data + pkt_len + ETH_FCS_LEN);
1968 rx_desc = (struct rx_desc *)rx_data;
1969 len_used = (int)(rx_data - (u8 *)agg->head);
1970 len_used += sizeof(struct rx_desc);
1971 }
1972
1973 submit:
1974 if (!ret) {
1975 ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
1976 } else {
1977 urb->actual_length = 0;
1978 list_add_tail(&agg->list, next);
1979 }
1980 }
1981
1982 if (!list_empty(&rx_queue)) {
1983 spin_lock_irqsave(&tp->rx_lock, flags);
1984 list_splice_tail(&rx_queue, &tp->rx_done);
1985 spin_unlock_irqrestore(&tp->rx_lock, flags);
1986 }
1987
1988 out1:
1989 return work_done;
1990 }
1991
tx_bottom(struct r8152 * tp)1992 static void tx_bottom(struct r8152 *tp)
1993 {
1994 int res;
1995
1996 do {
1997 struct tx_agg *agg;
1998
1999 if (skb_queue_empty(&tp->tx_queue))
2000 break;
2001
2002 agg = r8152_get_tx_agg(tp);
2003 if (!agg)
2004 break;
2005
2006 res = r8152_tx_agg_fill(tp, agg);
2007 if (res) {
2008 struct net_device *netdev = tp->netdev;
2009
2010 if (res == -ENODEV) {
2011 set_bit(RTL8152_UNPLUG, &tp->flags);
2012 netif_device_detach(netdev);
2013 } else {
2014 struct net_device_stats *stats = &netdev->stats;
2015 unsigned long flags;
2016
2017 netif_warn(tp, tx_err, netdev,
2018 "failed tx_urb %d\n", res);
2019 stats->tx_dropped += agg->skb_num;
2020
2021 spin_lock_irqsave(&tp->tx_lock, flags);
2022 list_add_tail(&agg->list, &tp->tx_free);
2023 spin_unlock_irqrestore(&tp->tx_lock, flags);
2024 }
2025 }
2026 } while (res == 0);
2027 }
2028
bottom_half(struct r8152 * tp)2029 static void bottom_half(struct r8152 *tp)
2030 {
2031 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2032 return;
2033
2034 if (!test_bit(WORK_ENABLE, &tp->flags))
2035 return;
2036
2037 /* When link down, the driver would cancel all bulks. */
2038 /* This avoid the re-submitting bulk */
2039 if (!netif_carrier_ok(tp->netdev))
2040 return;
2041
2042 clear_bit(SCHEDULE_NAPI, &tp->flags);
2043
2044 tx_bottom(tp);
2045 }
2046
r8152_poll(struct napi_struct * napi,int budget)2047 static int r8152_poll(struct napi_struct *napi, int budget)
2048 {
2049 struct r8152 *tp = container_of(napi, struct r8152, napi);
2050 int work_done;
2051
2052 work_done = rx_bottom(tp, budget);
2053 bottom_half(tp);
2054
2055 if (work_done < budget) {
2056 if (!napi_complete_done(napi, work_done))
2057 goto out;
2058 if (!list_empty(&tp->rx_done))
2059 napi_schedule(napi);
2060 else if (!skb_queue_empty(&tp->tx_queue) &&
2061 !list_empty(&tp->tx_free))
2062 napi_schedule(napi);
2063 }
2064
2065 out:
2066 return work_done;
2067 }
2068
2069 static
r8152_submit_rx(struct r8152 * tp,struct rx_agg * agg,gfp_t mem_flags)2070 int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
2071 {
2072 int ret;
2073
2074 /* The rx would be stopped, so skip submitting */
2075 if (test_bit(RTL8152_UNPLUG, &tp->flags) ||
2076 !test_bit(WORK_ENABLE, &tp->flags) || !netif_carrier_ok(tp->netdev))
2077 return 0;
2078
2079 usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
2080 agg->head, agg_buf_sz,
2081 (usb_complete_t)read_bulk_callback, agg);
2082
2083 ret = usb_submit_urb(agg->urb, mem_flags);
2084 if (ret == -ENODEV) {
2085 set_bit(RTL8152_UNPLUG, &tp->flags);
2086 netif_device_detach(tp->netdev);
2087 } else if (ret) {
2088 struct urb *urb = agg->urb;
2089 unsigned long flags;
2090
2091 urb->actual_length = 0;
2092 spin_lock_irqsave(&tp->rx_lock, flags);
2093 list_add_tail(&agg->list, &tp->rx_done);
2094 spin_unlock_irqrestore(&tp->rx_lock, flags);
2095
2096 netif_err(tp, rx_err, tp->netdev,
2097 "Couldn't submit rx[%p], ret = %d\n", agg, ret);
2098
2099 napi_schedule(&tp->napi);
2100 }
2101
2102 return ret;
2103 }
2104
rtl_drop_queued_tx(struct r8152 * tp)2105 static void rtl_drop_queued_tx(struct r8152 *tp)
2106 {
2107 struct net_device_stats *stats = &tp->netdev->stats;
2108 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
2109 struct sk_buff *skb;
2110
2111 if (skb_queue_empty(tx_queue))
2112 return;
2113
2114 __skb_queue_head_init(&skb_head);
2115 spin_lock_bh(&tx_queue->lock);
2116 skb_queue_splice_init(tx_queue, &skb_head);
2117 spin_unlock_bh(&tx_queue->lock);
2118
2119 while ((skb = __skb_dequeue(&skb_head))) {
2120 dev_kfree_skb(skb);
2121 stats->tx_dropped++;
2122 }
2123 }
2124
rtl8152_tx_timeout(struct net_device * netdev)2125 static void rtl8152_tx_timeout(struct net_device *netdev)
2126 {
2127 struct r8152 *tp = netdev_priv(netdev);
2128
2129 netif_warn(tp, tx_err, netdev, "Tx timeout\n");
2130
2131 usb_queue_reset_device(tp->intf);
2132 }
2133
rtl8152_set_rx_mode(struct net_device * netdev)2134 static void rtl8152_set_rx_mode(struct net_device *netdev)
2135 {
2136 struct r8152 *tp = netdev_priv(netdev);
2137
2138 if (netif_carrier_ok(netdev)) {
2139 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
2140 schedule_delayed_work(&tp->schedule, 0);
2141 }
2142 }
2143
_rtl8152_set_rx_mode(struct net_device * netdev)2144 static void _rtl8152_set_rx_mode(struct net_device *netdev)
2145 {
2146 struct r8152 *tp = netdev_priv(netdev);
2147 u32 mc_filter[2]; /* Multicast hash filter */
2148 __le32 tmp[2];
2149 u32 ocp_data;
2150
2151 netif_stop_queue(netdev);
2152 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2153 ocp_data &= ~RCR_ACPT_ALL;
2154 ocp_data |= RCR_AB | RCR_APM;
2155
2156 if (netdev->flags & IFF_PROMISC) {
2157 /* Unconditionally log net taps. */
2158 netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
2159 ocp_data |= RCR_AM | RCR_AAP;
2160 mc_filter[1] = 0xffffffff;
2161 mc_filter[0] = 0xffffffff;
2162 } else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
2163 (netdev->flags & IFF_ALLMULTI)) {
2164 /* Too many to filter perfectly -- accept all multicasts. */
2165 ocp_data |= RCR_AM;
2166 mc_filter[1] = 0xffffffff;
2167 mc_filter[0] = 0xffffffff;
2168 } else {
2169 struct netdev_hw_addr *ha;
2170
2171 mc_filter[1] = 0;
2172 mc_filter[0] = 0;
2173 netdev_for_each_mc_addr(ha, netdev) {
2174 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
2175
2176 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
2177 ocp_data |= RCR_AM;
2178 }
2179 }
2180
2181 tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
2182 tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
2183
2184 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
2185 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2186 netif_wake_queue(netdev);
2187 }
2188
2189 static netdev_features_t
rtl8152_features_check(struct sk_buff * skb,struct net_device * dev,netdev_features_t features)2190 rtl8152_features_check(struct sk_buff *skb, struct net_device *dev,
2191 netdev_features_t features)
2192 {
2193 u32 mss = skb_shinfo(skb)->gso_size;
2194 int max_offset = mss ? GTTCPHO_MAX : TCPHO_MAX;
2195 int offset = skb_transport_offset(skb);
2196
2197 if ((mss || skb->ip_summed == CHECKSUM_PARTIAL) && offset > max_offset)
2198 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2199 else if ((skb->len + sizeof(struct tx_desc)) > agg_buf_sz)
2200 features &= ~NETIF_F_GSO_MASK;
2201
2202 return features;
2203 }
2204
rtl8152_start_xmit(struct sk_buff * skb,struct net_device * netdev)2205 static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
2206 struct net_device *netdev)
2207 {
2208 struct r8152 *tp = netdev_priv(netdev);
2209
2210 skb_tx_timestamp(skb);
2211
2212 skb_queue_tail(&tp->tx_queue, skb);
2213
2214 if (!list_empty(&tp->tx_free)) {
2215 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
2216 set_bit(SCHEDULE_NAPI, &tp->flags);
2217 schedule_delayed_work(&tp->schedule, 0);
2218 } else {
2219 usb_mark_last_busy(tp->udev);
2220 napi_schedule(&tp->napi);
2221 }
2222 } else if (skb_queue_len(&tp->tx_queue) > tp->tx_qlen) {
2223 netif_stop_queue(netdev);
2224 }
2225
2226 return NETDEV_TX_OK;
2227 }
2228
r8152b_reset_packet_filter(struct r8152 * tp)2229 static void r8152b_reset_packet_filter(struct r8152 *tp)
2230 {
2231 u32 ocp_data;
2232
2233 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
2234 ocp_data &= ~FMC_FCR_MCU_EN;
2235 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
2236 ocp_data |= FMC_FCR_MCU_EN;
2237 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
2238 }
2239
rtl8152_nic_reset(struct r8152 * tp)2240 static void rtl8152_nic_reset(struct r8152 *tp)
2241 {
2242 int i;
2243
2244 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
2245
2246 for (i = 0; i < 1000; i++) {
2247 if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
2248 break;
2249 usleep_range(100, 400);
2250 }
2251 }
2252
set_tx_qlen(struct r8152 * tp)2253 static void set_tx_qlen(struct r8152 *tp)
2254 {
2255 struct net_device *netdev = tp->netdev;
2256
2257 tp->tx_qlen = agg_buf_sz / (netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN +
2258 sizeof(struct tx_desc));
2259 }
2260
rtl8152_get_speed(struct r8152 * tp)2261 static inline u8 rtl8152_get_speed(struct r8152 *tp)
2262 {
2263 return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
2264 }
2265
rtl_set_eee_plus(struct r8152 * tp)2266 static void rtl_set_eee_plus(struct r8152 *tp)
2267 {
2268 u32 ocp_data;
2269 u8 speed;
2270
2271 speed = rtl8152_get_speed(tp);
2272 if (speed & _10bps) {
2273 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
2274 ocp_data |= EEEP_CR_EEEP_TX;
2275 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
2276 } else {
2277 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
2278 ocp_data &= ~EEEP_CR_EEEP_TX;
2279 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
2280 }
2281 }
2282
rxdy_gated_en(struct r8152 * tp,bool enable)2283 static void rxdy_gated_en(struct r8152 *tp, bool enable)
2284 {
2285 u32 ocp_data;
2286
2287 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
2288 if (enable)
2289 ocp_data |= RXDY_GATED_EN;
2290 else
2291 ocp_data &= ~RXDY_GATED_EN;
2292 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
2293 }
2294
rtl_start_rx(struct r8152 * tp)2295 static int rtl_start_rx(struct r8152 *tp)
2296 {
2297 int i, ret = 0;
2298
2299 INIT_LIST_HEAD(&tp->rx_done);
2300 for (i = 0; i < RTL8152_MAX_RX; i++) {
2301 INIT_LIST_HEAD(&tp->rx_info[i].list);
2302 ret = r8152_submit_rx(tp, &tp->rx_info[i], GFP_KERNEL);
2303 if (ret)
2304 break;
2305 }
2306
2307 if (ret && ++i < RTL8152_MAX_RX) {
2308 struct list_head rx_queue;
2309 unsigned long flags;
2310
2311 INIT_LIST_HEAD(&rx_queue);
2312
2313 do {
2314 struct rx_agg *agg = &tp->rx_info[i++];
2315 struct urb *urb = agg->urb;
2316
2317 urb->actual_length = 0;
2318 list_add_tail(&agg->list, &rx_queue);
2319 } while (i < RTL8152_MAX_RX);
2320
2321 spin_lock_irqsave(&tp->rx_lock, flags);
2322 list_splice_tail(&rx_queue, &tp->rx_done);
2323 spin_unlock_irqrestore(&tp->rx_lock, flags);
2324 }
2325
2326 return ret;
2327 }
2328
rtl_stop_rx(struct r8152 * tp)2329 static int rtl_stop_rx(struct r8152 *tp)
2330 {
2331 int i;
2332
2333 for (i = 0; i < RTL8152_MAX_RX; i++)
2334 usb_kill_urb(tp->rx_info[i].urb);
2335
2336 while (!skb_queue_empty(&tp->rx_queue))
2337 dev_kfree_skb(__skb_dequeue(&tp->rx_queue));
2338
2339 return 0;
2340 }
2341
rtl_enable(struct r8152 * tp)2342 static int rtl_enable(struct r8152 *tp)
2343 {
2344 u32 ocp_data;
2345
2346 r8152b_reset_packet_filter(tp);
2347
2348 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
2349 ocp_data |= CR_RE | CR_TE;
2350 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
2351
2352 rxdy_gated_en(tp, false);
2353
2354 return 0;
2355 }
2356
rtl8152_enable(struct r8152 * tp)2357 static int rtl8152_enable(struct r8152 *tp)
2358 {
2359 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2360 return -ENODEV;
2361
2362 set_tx_qlen(tp);
2363 rtl_set_eee_plus(tp);
2364
2365 return rtl_enable(tp);
2366 }
2367
r8153b_rx_agg_chg_indicate(struct r8152 * tp)2368 static inline void r8153b_rx_agg_chg_indicate(struct r8152 *tp)
2369 {
2370 ocp_write_byte(tp, MCU_TYPE_USB, USB_UPT_RXDMA_OWN,
2371 OWN_UPDATE | OWN_CLEAR);
2372 }
2373
r8153_set_rx_early_timeout(struct r8152 * tp)2374 static void r8153_set_rx_early_timeout(struct r8152 *tp)
2375 {
2376 u32 ocp_data = tp->coalesce / 8;
2377
2378 switch (tp->version) {
2379 case RTL_VER_03:
2380 case RTL_VER_04:
2381 case RTL_VER_05:
2382 case RTL_VER_06:
2383 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
2384 ocp_data);
2385 break;
2386
2387 case RTL_VER_08:
2388 case RTL_VER_09:
2389 /* The RTL8153B uses USB_RX_EXTRA_AGGR_TMR for rx timeout
2390 * primarily. For USB_RX_EARLY_TIMEOUT, we fix it to 128ns.
2391 */
2392 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_TIMEOUT,
2393 128 / 8);
2394 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EXTRA_AGGR_TMR,
2395 ocp_data);
2396 r8153b_rx_agg_chg_indicate(tp);
2397 break;
2398
2399 default:
2400 break;
2401 }
2402 }
2403
r8153_set_rx_early_size(struct r8152 * tp)2404 static void r8153_set_rx_early_size(struct r8152 *tp)
2405 {
2406 u32 ocp_data = agg_buf_sz - rx_reserved_size(tp->netdev->mtu);
2407
2408 switch (tp->version) {
2409 case RTL_VER_03:
2410 case RTL_VER_04:
2411 case RTL_VER_05:
2412 case RTL_VER_06:
2413 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
2414 ocp_data / 4);
2415 break;
2416 case RTL_VER_08:
2417 case RTL_VER_09:
2418 ocp_write_word(tp, MCU_TYPE_USB, USB_RX_EARLY_SIZE,
2419 ocp_data / 8);
2420 r8153b_rx_agg_chg_indicate(tp);
2421 break;
2422 default:
2423 WARN_ON_ONCE(1);
2424 break;
2425 }
2426 }
2427
rtl8153_enable(struct r8152 * tp)2428 static int rtl8153_enable(struct r8152 *tp)
2429 {
2430 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2431 return -ENODEV;
2432
2433 set_tx_qlen(tp);
2434 rtl_set_eee_plus(tp);
2435 r8153_set_rx_early_timeout(tp);
2436 r8153_set_rx_early_size(tp);
2437
2438 return rtl_enable(tp);
2439 }
2440
rtl_disable(struct r8152 * tp)2441 static void rtl_disable(struct r8152 *tp)
2442 {
2443 u32 ocp_data;
2444 int i;
2445
2446 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
2447 rtl_drop_queued_tx(tp);
2448 return;
2449 }
2450
2451 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2452 ocp_data &= ~RCR_ACPT_ALL;
2453 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2454
2455 rtl_drop_queued_tx(tp);
2456
2457 for (i = 0; i < RTL8152_MAX_TX; i++)
2458 usb_kill_urb(tp->tx_info[i].urb);
2459
2460 rxdy_gated_en(tp, true);
2461
2462 for (i = 0; i < 1000; i++) {
2463 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2464 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
2465 break;
2466 usleep_range(1000, 2000);
2467 }
2468
2469 for (i = 0; i < 1000; i++) {
2470 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
2471 break;
2472 usleep_range(1000, 2000);
2473 }
2474
2475 rtl_stop_rx(tp);
2476
2477 rtl8152_nic_reset(tp);
2478 }
2479
r8152_power_cut_en(struct r8152 * tp,bool enable)2480 static void r8152_power_cut_en(struct r8152 *tp, bool enable)
2481 {
2482 u32 ocp_data;
2483
2484 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
2485 if (enable)
2486 ocp_data |= POWER_CUT;
2487 else
2488 ocp_data &= ~POWER_CUT;
2489 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
2490
2491 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
2492 ocp_data &= ~RESUME_INDICATE;
2493 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
2494 }
2495
rtl_rx_vlan_en(struct r8152 * tp,bool enable)2496 static void rtl_rx_vlan_en(struct r8152 *tp, bool enable)
2497 {
2498 u32 ocp_data;
2499
2500 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2501 if (enable)
2502 ocp_data |= CPCR_RX_VLAN;
2503 else
2504 ocp_data &= ~CPCR_RX_VLAN;
2505 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2506 }
2507
rtl8152_set_features(struct net_device * dev,netdev_features_t features)2508 static int rtl8152_set_features(struct net_device *dev,
2509 netdev_features_t features)
2510 {
2511 netdev_features_t changed = features ^ dev->features;
2512 struct r8152 *tp = netdev_priv(dev);
2513 int ret;
2514
2515 ret = usb_autopm_get_interface(tp->intf);
2516 if (ret < 0)
2517 goto out;
2518
2519 mutex_lock(&tp->control);
2520
2521 if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
2522 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2523 rtl_rx_vlan_en(tp, true);
2524 else
2525 rtl_rx_vlan_en(tp, false);
2526 }
2527
2528 mutex_unlock(&tp->control);
2529
2530 usb_autopm_put_interface(tp->intf);
2531
2532 out:
2533 return ret;
2534 }
2535
2536 #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
2537
__rtl_get_wol(struct r8152 * tp)2538 static u32 __rtl_get_wol(struct r8152 *tp)
2539 {
2540 u32 ocp_data;
2541 u32 wolopts = 0;
2542
2543 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2544 if (ocp_data & LINK_ON_WAKE_EN)
2545 wolopts |= WAKE_PHY;
2546
2547 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
2548 if (ocp_data & UWF_EN)
2549 wolopts |= WAKE_UCAST;
2550 if (ocp_data & BWF_EN)
2551 wolopts |= WAKE_BCAST;
2552 if (ocp_data & MWF_EN)
2553 wolopts |= WAKE_MCAST;
2554
2555 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2556 if (ocp_data & MAGIC_EN)
2557 wolopts |= WAKE_MAGIC;
2558
2559 return wolopts;
2560 }
2561
__rtl_set_wol(struct r8152 * tp,u32 wolopts)2562 static void __rtl_set_wol(struct r8152 *tp, u32 wolopts)
2563 {
2564 u32 ocp_data;
2565
2566 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
2567
2568 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2569 ocp_data &= ~LINK_ON_WAKE_EN;
2570 if (wolopts & WAKE_PHY)
2571 ocp_data |= LINK_ON_WAKE_EN;
2572 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
2573
2574 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
2575 ocp_data &= ~(UWF_EN | BWF_EN | MWF_EN);
2576 if (wolopts & WAKE_UCAST)
2577 ocp_data |= UWF_EN;
2578 if (wolopts & WAKE_BCAST)
2579 ocp_data |= BWF_EN;
2580 if (wolopts & WAKE_MCAST)
2581 ocp_data |= MWF_EN;
2582 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data);
2583
2584 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2585
2586 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2587 ocp_data &= ~MAGIC_EN;
2588 if (wolopts & WAKE_MAGIC)
2589 ocp_data |= MAGIC_EN;
2590 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
2591
2592 if (wolopts & WAKE_ANY)
2593 device_set_wakeup_enable(&tp->udev->dev, true);
2594 else
2595 device_set_wakeup_enable(&tp->udev->dev, false);
2596 }
2597
r8153_mac_clk_spd(struct r8152 * tp,bool enable)2598 static void r8153_mac_clk_spd(struct r8152 *tp, bool enable)
2599 {
2600 /* MAC clock speed down */
2601 if (enable) {
2602 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL,
2603 ALDPS_SPDWN_RATIO);
2604 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2,
2605 EEE_SPDWN_RATIO);
2606 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3,
2607 PKT_AVAIL_SPDWN_EN | SUSPEND_SPDWN_EN |
2608 U1U2_SPDWN_EN | L1_SPDWN_EN);
2609 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4,
2610 PWRSAVE_SPDWN_EN | RXDV_SPDWN_EN | TX10MIDLE_EN |
2611 TP100_SPDWN_EN | TP500_SPDWN_EN | EEE_SPDWN_EN |
2612 TP1000_SPDWN_EN);
2613 } else {
2614 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, 0);
2615 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, 0);
2616 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3, 0);
2617 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4, 0);
2618 }
2619 }
2620
r8153_u1u2en(struct r8152 * tp,bool enable)2621 static void r8153_u1u2en(struct r8152 *tp, bool enable)
2622 {
2623 u8 u1u2[8];
2624
2625 if (enable)
2626 memset(u1u2, 0xff, sizeof(u1u2));
2627 else
2628 memset(u1u2, 0x00, sizeof(u1u2));
2629
2630 usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2);
2631 }
2632
r8153b_u1u2en(struct r8152 * tp,bool enable)2633 static void r8153b_u1u2en(struct r8152 *tp, bool enable)
2634 {
2635 u32 ocp_data;
2636
2637 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG);
2638 if (enable)
2639 ocp_data |= LPM_U1U2_EN;
2640 else
2641 ocp_data &= ~LPM_U1U2_EN;
2642
2643 ocp_write_word(tp, MCU_TYPE_USB, USB_LPM_CONFIG, ocp_data);
2644 }
2645
r8153_u2p3en(struct r8152 * tp,bool enable)2646 static void r8153_u2p3en(struct r8152 *tp, bool enable)
2647 {
2648 u32 ocp_data;
2649
2650 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
2651 if (enable)
2652 ocp_data |= U2P3_ENABLE;
2653 else
2654 ocp_data &= ~U2P3_ENABLE;
2655 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
2656 }
2657
r8153b_ups_flags_w1w0(struct r8152 * tp,u32 set,u32 clear)2658 static void r8153b_ups_flags_w1w0(struct r8152 *tp, u32 set, u32 clear)
2659 {
2660 u32 ocp_data;
2661
2662 ocp_data = ocp_read_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS);
2663 ocp_data &= ~clear;
2664 ocp_data |= set;
2665 ocp_write_dword(tp, MCU_TYPE_USB, USB_UPS_FLAGS, ocp_data);
2666 }
2667
r8153b_green_en(struct r8152 * tp,bool enable)2668 static void r8153b_green_en(struct r8152 *tp, bool enable)
2669 {
2670 u16 data;
2671
2672 if (enable) {
2673 sram_write(tp, 0x8045, 0); /* 10M abiq&ldvbias */
2674 sram_write(tp, 0x804d, 0x1222); /* 100M short abiq&ldvbias */
2675 sram_write(tp, 0x805d, 0x0022); /* 1000M short abiq&ldvbias */
2676 } else {
2677 sram_write(tp, 0x8045, 0x2444); /* 10M abiq&ldvbias */
2678 sram_write(tp, 0x804d, 0x2444); /* 100M short abiq&ldvbias */
2679 sram_write(tp, 0x805d, 0x2444); /* 1000M short abiq&ldvbias */
2680 }
2681
2682 data = sram_read(tp, SRAM_GREEN_CFG);
2683 data |= GREEN_ETH_EN;
2684 sram_write(tp, SRAM_GREEN_CFG, data);
2685
2686 r8153b_ups_flags_w1w0(tp, UPS_FLAGS_EN_GREEN, 0);
2687 }
2688
r8153_phy_status(struct r8152 * tp,u16 desired)2689 static u16 r8153_phy_status(struct r8152 *tp, u16 desired)
2690 {
2691 u16 data;
2692 int i;
2693
2694 for (i = 0; i < 500; i++) {
2695 data = ocp_reg_read(tp, OCP_PHY_STATUS);
2696 data &= PHY_STAT_MASK;
2697 if (desired) {
2698 if (data == desired)
2699 break;
2700 } else if (data == PHY_STAT_LAN_ON || data == PHY_STAT_PWRDN ||
2701 data == PHY_STAT_EXT_INIT) {
2702 break;
2703 }
2704
2705 msleep(20);
2706 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2707 break;
2708 }
2709
2710 return data;
2711 }
2712
r8153b_ups_en(struct r8152 * tp,bool enable)2713 static void r8153b_ups_en(struct r8152 *tp, bool enable)
2714 {
2715 u32 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_POWER_CUT);
2716
2717 if (enable) {
2718 ocp_data |= UPS_EN | USP_PREWAKE | PHASE2_EN;
2719 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2720
2721 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, 0xcfff);
2722 ocp_data |= BIT(0);
2723 ocp_write_byte(tp, MCU_TYPE_USB, 0xcfff, ocp_data);
2724 } else {
2725 u16 data;
2726
2727 ocp_data &= ~(UPS_EN | USP_PREWAKE);
2728 ocp_write_byte(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2729
2730 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, 0xcfff);
2731 ocp_data &= ~BIT(0);
2732 ocp_write_byte(tp, MCU_TYPE_USB, 0xcfff, ocp_data);
2733
2734 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
2735 ocp_data &= ~PCUT_STATUS;
2736 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
2737
2738 data = r8153_phy_status(tp, 0);
2739
2740 switch (data) {
2741 case PHY_STAT_PWRDN:
2742 case PHY_STAT_EXT_INIT:
2743 r8153b_green_en(tp,
2744 test_bit(GREEN_ETHERNET, &tp->flags));
2745
2746 data = r8152_mdio_read(tp, MII_BMCR);
2747 data &= ~BMCR_PDOWN;
2748 data |= BMCR_RESET;
2749 r8152_mdio_write(tp, MII_BMCR, data);
2750
2751 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
2752 /* fall through */
2753
2754 default:
2755 if (data != PHY_STAT_LAN_ON)
2756 netif_warn(tp, link, tp->netdev,
2757 "PHY not ready");
2758 break;
2759 }
2760 }
2761 }
2762
r8153_power_cut_en(struct r8152 * tp,bool enable)2763 static void r8153_power_cut_en(struct r8152 *tp, bool enable)
2764 {
2765 u32 ocp_data;
2766
2767 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
2768 if (enable)
2769 ocp_data |= PWR_EN | PHASE2_EN;
2770 else
2771 ocp_data &= ~(PWR_EN | PHASE2_EN);
2772 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2773
2774 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
2775 ocp_data &= ~PCUT_STATUS;
2776 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
2777 }
2778
r8153b_power_cut_en(struct r8152 * tp,bool enable)2779 static void r8153b_power_cut_en(struct r8152 *tp, bool enable)
2780 {
2781 u32 ocp_data;
2782
2783 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
2784 if (enable)
2785 ocp_data |= PWR_EN | PHASE2_EN;
2786 else
2787 ocp_data &= ~PWR_EN;
2788 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2789
2790 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
2791 ocp_data &= ~PCUT_STATUS;
2792 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
2793 }
2794
r8153b_queue_wake(struct r8152 * tp,bool enable)2795 static void r8153b_queue_wake(struct r8152 *tp, bool enable)
2796 {
2797 u32 ocp_data;
2798
2799 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, 0xd38a);
2800 if (enable)
2801 ocp_data |= BIT(0);
2802 else
2803 ocp_data &= ~BIT(0);
2804 ocp_write_byte(tp, MCU_TYPE_PLA, 0xd38a, ocp_data);
2805
2806 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, 0xd38c);
2807 ocp_data &= ~BIT(0);
2808 ocp_write_byte(tp, MCU_TYPE_PLA, 0xd38c, ocp_data);
2809 }
2810
rtl_can_wakeup(struct r8152 * tp)2811 static bool rtl_can_wakeup(struct r8152 *tp)
2812 {
2813 struct usb_device *udev = tp->udev;
2814
2815 return (udev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_WAKEUP);
2816 }
2817
rtl_runtime_suspend_enable(struct r8152 * tp,bool enable)2818 static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable)
2819 {
2820 if (enable) {
2821 u32 ocp_data;
2822
2823 __rtl_set_wol(tp, WAKE_ANY);
2824
2825 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
2826
2827 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2828 ocp_data |= LINK_OFF_WAKE_EN;
2829 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
2830
2831 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2832 } else {
2833 u32 ocp_data;
2834
2835 __rtl_set_wol(tp, tp->saved_wolopts);
2836
2837 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
2838
2839 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2840 ocp_data &= ~LINK_OFF_WAKE_EN;
2841 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
2842
2843 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2844 }
2845 }
2846
rtl8153_runtime_enable(struct r8152 * tp,bool enable)2847 static void rtl8153_runtime_enable(struct r8152 *tp, bool enable)
2848 {
2849 if (enable) {
2850 r8153_u1u2en(tp, false);
2851 r8153_u2p3en(tp, false);
2852 r8153_mac_clk_spd(tp, true);
2853 rtl_runtime_suspend_enable(tp, true);
2854 } else {
2855 rtl_runtime_suspend_enable(tp, false);
2856 r8153_mac_clk_spd(tp, false);
2857
2858 switch (tp->version) {
2859 case RTL_VER_03:
2860 case RTL_VER_04:
2861 break;
2862 case RTL_VER_05:
2863 case RTL_VER_06:
2864 default:
2865 r8153_u2p3en(tp, true);
2866 break;
2867 }
2868
2869 r8153_u1u2en(tp, true);
2870 }
2871 }
2872
rtl8153b_runtime_enable(struct r8152 * tp,bool enable)2873 static void rtl8153b_runtime_enable(struct r8152 *tp, bool enable)
2874 {
2875 if (enable) {
2876 r8153b_queue_wake(tp, true);
2877 r8153b_u1u2en(tp, false);
2878 r8153_u2p3en(tp, false);
2879 rtl_runtime_suspend_enable(tp, true);
2880 r8153b_ups_en(tp, true);
2881 } else {
2882 r8153b_ups_en(tp, false);
2883 r8153b_queue_wake(tp, false);
2884 rtl_runtime_suspend_enable(tp, false);
2885 r8153_u2p3en(tp, true);
2886 r8153b_u1u2en(tp, true);
2887 }
2888 }
2889
r8153_teredo_off(struct r8152 * tp)2890 static void r8153_teredo_off(struct r8152 *tp)
2891 {
2892 u32 ocp_data;
2893
2894 switch (tp->version) {
2895 case RTL_VER_01:
2896 case RTL_VER_02:
2897 case RTL_VER_03:
2898 case RTL_VER_04:
2899 case RTL_VER_05:
2900 case RTL_VER_06:
2901 case RTL_VER_07:
2902 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
2903 ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK |
2904 OOB_TEREDO_EN);
2905 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
2906 break;
2907
2908 case RTL_VER_08:
2909 case RTL_VER_09:
2910 /* The bit 0 ~ 7 are relative with teredo settings. They are
2911 * W1C (write 1 to clear), so set all 1 to disable it.
2912 */
2913 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, 0xff);
2914 break;
2915
2916 default:
2917 break;
2918 }
2919
2920 ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE);
2921 ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0);
2922 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0);
2923 }
2924
rtl_reset_bmu(struct r8152 * tp)2925 static void rtl_reset_bmu(struct r8152 *tp)
2926 {
2927 u32 ocp_data;
2928
2929 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_BMU_RESET);
2930 ocp_data &= ~(BMU_RESET_EP_IN | BMU_RESET_EP_OUT);
2931 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
2932 ocp_data |= BMU_RESET_EP_IN | BMU_RESET_EP_OUT;
2933 ocp_write_byte(tp, MCU_TYPE_USB, USB_BMU_RESET, ocp_data);
2934 }
2935
r8152_aldps_en(struct r8152 * tp,bool enable)2936 static void r8152_aldps_en(struct r8152 *tp, bool enable)
2937 {
2938 if (enable) {
2939 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
2940 LINKENA | DIS_SDSAVE);
2941 } else {
2942 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA |
2943 DIS_SDSAVE);
2944 msleep(20);
2945 }
2946 }
2947
r8152_mmd_indirect(struct r8152 * tp,u16 dev,u16 reg)2948 static inline void r8152_mmd_indirect(struct r8152 *tp, u16 dev, u16 reg)
2949 {
2950 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | dev);
2951 ocp_reg_write(tp, OCP_EEE_DATA, reg);
2952 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | dev);
2953 }
2954
r8152_mmd_read(struct r8152 * tp,u16 dev,u16 reg)2955 static u16 r8152_mmd_read(struct r8152 *tp, u16 dev, u16 reg)
2956 {
2957 u16 data;
2958
2959 r8152_mmd_indirect(tp, dev, reg);
2960 data = ocp_reg_read(tp, OCP_EEE_DATA);
2961 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
2962
2963 return data;
2964 }
2965
r8152_mmd_write(struct r8152 * tp,u16 dev,u16 reg,u16 data)2966 static void r8152_mmd_write(struct r8152 *tp, u16 dev, u16 reg, u16 data)
2967 {
2968 r8152_mmd_indirect(tp, dev, reg);
2969 ocp_reg_write(tp, OCP_EEE_DATA, data);
2970 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
2971 }
2972
r8152_eee_en(struct r8152 * tp,bool enable)2973 static void r8152_eee_en(struct r8152 *tp, bool enable)
2974 {
2975 u16 config1, config2, config3;
2976 u32 ocp_data;
2977
2978 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
2979 config1 = ocp_reg_read(tp, OCP_EEE_CONFIG1) & ~sd_rise_time_mask;
2980 config2 = ocp_reg_read(tp, OCP_EEE_CONFIG2);
2981 config3 = ocp_reg_read(tp, OCP_EEE_CONFIG3) & ~fast_snr_mask;
2982
2983 if (enable) {
2984 ocp_data |= EEE_RX_EN | EEE_TX_EN;
2985 config1 |= EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | RX_QUIET_EN;
2986 config1 |= sd_rise_time(1);
2987 config2 |= RG_DACQUIET_EN | RG_LDVQUIET_EN;
2988 config3 |= fast_snr(42);
2989 } else {
2990 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
2991 config1 &= ~(EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN |
2992 RX_QUIET_EN);
2993 config1 |= sd_rise_time(7);
2994 config2 &= ~(RG_DACQUIET_EN | RG_LDVQUIET_EN);
2995 config3 |= fast_snr(511);
2996 }
2997
2998 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
2999 ocp_reg_write(tp, OCP_EEE_CONFIG1, config1);
3000 ocp_reg_write(tp, OCP_EEE_CONFIG2, config2);
3001 ocp_reg_write(tp, OCP_EEE_CONFIG3, config3);
3002 }
3003
r8152b_enable_eee(struct r8152 * tp)3004 static void r8152b_enable_eee(struct r8152 *tp)
3005 {
3006 r8152_eee_en(tp, true);
3007 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, MDIO_EEE_100TX);
3008 }
3009
r8152b_enable_fc(struct r8152 * tp)3010 static void r8152b_enable_fc(struct r8152 *tp)
3011 {
3012 u16 anar;
3013
3014 anar = r8152_mdio_read(tp, MII_ADVERTISE);
3015 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
3016 r8152_mdio_write(tp, MII_ADVERTISE, anar);
3017 }
3018
rtl8152_disable(struct r8152 * tp)3019 static void rtl8152_disable(struct r8152 *tp)
3020 {
3021 r8152_aldps_en(tp, false);
3022 rtl_disable(tp);
3023 r8152_aldps_en(tp, true);
3024 }
3025
r8152b_hw_phy_cfg(struct r8152 * tp)3026 static void r8152b_hw_phy_cfg(struct r8152 *tp)
3027 {
3028 r8152b_enable_eee(tp);
3029 r8152_aldps_en(tp, true);
3030 r8152b_enable_fc(tp);
3031
3032 set_bit(PHY_RESET, &tp->flags);
3033 }
3034
r8152b_exit_oob(struct r8152 * tp)3035 static void r8152b_exit_oob(struct r8152 *tp)
3036 {
3037 u32 ocp_data;
3038 int i;
3039
3040 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
3041 ocp_data &= ~RCR_ACPT_ALL;
3042 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
3043
3044 rxdy_gated_en(tp, true);
3045 r8153_teredo_off(tp);
3046 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
3047 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
3048
3049 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3050 ocp_data &= ~NOW_IS_OOB;
3051 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3052
3053 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3054 ocp_data &= ~MCU_BORW_EN;
3055 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3056
3057 for (i = 0; i < 1000; i++) {
3058 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3059 if (ocp_data & LINK_LIST_READY)
3060 break;
3061 usleep_range(1000, 2000);
3062 }
3063
3064 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3065 ocp_data |= RE_INIT_LL;
3066 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3067
3068 for (i = 0; i < 1000; i++) {
3069 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3070 if (ocp_data & LINK_LIST_READY)
3071 break;
3072 usleep_range(1000, 2000);
3073 }
3074
3075 rtl8152_nic_reset(tp);
3076
3077 /* rx share fifo credit full threshold */
3078 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
3079
3080 if (tp->udev->speed == USB_SPEED_FULL ||
3081 tp->udev->speed == USB_SPEED_LOW) {
3082 /* rx share fifo credit near full threshold */
3083 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
3084 RXFIFO_THR2_FULL);
3085 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
3086 RXFIFO_THR3_FULL);
3087 } else {
3088 /* rx share fifo credit near full threshold */
3089 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
3090 RXFIFO_THR2_HIGH);
3091 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
3092 RXFIFO_THR3_HIGH);
3093 }
3094
3095 /* TX share fifo free credit full threshold */
3096 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL);
3097
3098 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
3099 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH);
3100 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
3101 TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
3102
3103 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
3104
3105 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
3106
3107 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
3108 ocp_data |= TCR0_AUTO_FIFO;
3109 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
3110 }
3111
r8152b_enter_oob(struct r8152 * tp)3112 static void r8152b_enter_oob(struct r8152 *tp)
3113 {
3114 u32 ocp_data;
3115 int i;
3116
3117 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3118 ocp_data &= ~NOW_IS_OOB;
3119 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3120
3121 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
3122 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
3123 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
3124
3125 rtl_disable(tp);
3126
3127 for (i = 0; i < 1000; i++) {
3128 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3129 if (ocp_data & LINK_LIST_READY)
3130 break;
3131 usleep_range(1000, 2000);
3132 }
3133
3134 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3135 ocp_data |= RE_INIT_LL;
3136 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3137
3138 for (i = 0; i < 1000; i++) {
3139 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3140 if (ocp_data & LINK_LIST_READY)
3141 break;
3142 usleep_range(1000, 2000);
3143 }
3144
3145 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
3146
3147 rtl_rx_vlan_en(tp, true);
3148
3149 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
3150 ocp_data |= ALDPS_PROXY_MODE;
3151 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
3152
3153 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3154 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
3155 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3156
3157 rxdy_gated_en(tp, false);
3158
3159 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
3160 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
3161 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
3162 }
3163
r8153_patch_request(struct r8152 * tp,bool request)3164 static int r8153_patch_request(struct r8152 *tp, bool request)
3165 {
3166 u16 data;
3167 int i;
3168
3169 data = ocp_reg_read(tp, OCP_PHY_PATCH_CMD);
3170 if (request)
3171 data |= PATCH_REQUEST;
3172 else
3173 data &= ~PATCH_REQUEST;
3174 ocp_reg_write(tp, OCP_PHY_PATCH_CMD, data);
3175
3176 for (i = 0; request && i < 5000; i++) {
3177 usleep_range(1000, 2000);
3178 if (ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)
3179 break;
3180 }
3181
3182 if (request && !(ocp_reg_read(tp, OCP_PHY_PATCH_STAT) & PATCH_READY)) {
3183 netif_err(tp, drv, tp->netdev, "patch request fail\n");
3184 r8153_patch_request(tp, false);
3185 return -ETIME;
3186 } else {
3187 return 0;
3188 }
3189 }
3190
r8153_aldps_en(struct r8152 * tp,bool enable)3191 static void r8153_aldps_en(struct r8152 *tp, bool enable)
3192 {
3193 u16 data;
3194
3195 data = ocp_reg_read(tp, OCP_POWER_CFG);
3196 if (enable) {
3197 data |= EN_ALDPS;
3198 ocp_reg_write(tp, OCP_POWER_CFG, data);
3199 } else {
3200 int i;
3201
3202 data &= ~EN_ALDPS;
3203 ocp_reg_write(tp, OCP_POWER_CFG, data);
3204 for (i = 0; i < 20; i++) {
3205 usleep_range(1000, 2000);
3206 if (ocp_read_word(tp, MCU_TYPE_PLA, 0xe000) & 0x0100)
3207 break;
3208 }
3209 }
3210 }
3211
r8153b_aldps_en(struct r8152 * tp,bool enable)3212 static void r8153b_aldps_en(struct r8152 *tp, bool enable)
3213 {
3214 r8153_aldps_en(tp, enable);
3215
3216 if (enable)
3217 r8153b_ups_flags_w1w0(tp, UPS_FLAGS_EN_ALDPS, 0);
3218 else
3219 r8153b_ups_flags_w1w0(tp, 0, UPS_FLAGS_EN_ALDPS);
3220 }
3221
r8153_eee_en(struct r8152 * tp,bool enable)3222 static void r8153_eee_en(struct r8152 *tp, bool enable)
3223 {
3224 u32 ocp_data;
3225 u16 config;
3226
3227 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
3228 config = ocp_reg_read(tp, OCP_EEE_CFG);
3229
3230 if (enable) {
3231 ocp_data |= EEE_RX_EN | EEE_TX_EN;
3232 config |= EEE10_EN;
3233 } else {
3234 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
3235 config &= ~EEE10_EN;
3236 }
3237
3238 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
3239 ocp_reg_write(tp, OCP_EEE_CFG, config);
3240 }
3241
r8153b_eee_en(struct r8152 * tp,bool enable)3242 static void r8153b_eee_en(struct r8152 *tp, bool enable)
3243 {
3244 r8153_eee_en(tp, enable);
3245
3246 if (enable)
3247 r8153b_ups_flags_w1w0(tp, UPS_FLAGS_EN_EEE, 0);
3248 else
3249 r8153b_ups_flags_w1w0(tp, 0, UPS_FLAGS_EN_EEE);
3250 }
3251
r8153b_enable_fc(struct r8152 * tp)3252 static void r8153b_enable_fc(struct r8152 *tp)
3253 {
3254 r8152b_enable_fc(tp);
3255 r8153b_ups_flags_w1w0(tp, UPS_FLAGS_EN_FLOW_CTR, 0);
3256 }
3257
r8153_hw_phy_cfg(struct r8152 * tp)3258 static void r8153_hw_phy_cfg(struct r8152 *tp)
3259 {
3260 u32 ocp_data;
3261 u16 data;
3262
3263 /* disable ALDPS before updating the PHY parameters */
3264 r8153_aldps_en(tp, false);
3265
3266 /* disable EEE before updating the PHY parameters */
3267 r8153_eee_en(tp, false);
3268 ocp_reg_write(tp, OCP_EEE_ADV, 0);
3269
3270 if (tp->version == RTL_VER_03) {
3271 data = ocp_reg_read(tp, OCP_EEE_CFG);
3272 data &= ~CTAP_SHORT_EN;
3273 ocp_reg_write(tp, OCP_EEE_CFG, data);
3274 }
3275
3276 data = ocp_reg_read(tp, OCP_POWER_CFG);
3277 data |= EEE_CLKDIV_EN;
3278 ocp_reg_write(tp, OCP_POWER_CFG, data);
3279
3280 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
3281 data |= EN_10M_BGOFF;
3282 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
3283 data = ocp_reg_read(tp, OCP_POWER_CFG);
3284 data |= EN_10M_PLLOFF;
3285 ocp_reg_write(tp, OCP_POWER_CFG, data);
3286 sram_write(tp, SRAM_IMPEDANCE, 0x0b13);
3287
3288 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
3289 ocp_data |= PFM_PWM_SWITCH;
3290 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
3291
3292 /* Enable LPF corner auto tune */
3293 sram_write(tp, SRAM_LPF_CFG, 0xf70f);
3294
3295 /* Adjust 10M Amplitude */
3296 sram_write(tp, SRAM_10M_AMP1, 0x00af);
3297 sram_write(tp, SRAM_10M_AMP2, 0x0208);
3298
3299 r8153_eee_en(tp, true);
3300 ocp_reg_write(tp, OCP_EEE_ADV, MDIO_EEE_1000T | MDIO_EEE_100TX);
3301
3302 r8153_aldps_en(tp, true);
3303 r8152b_enable_fc(tp);
3304
3305 switch (tp->version) {
3306 case RTL_VER_03:
3307 case RTL_VER_04:
3308 break;
3309 case RTL_VER_05:
3310 case RTL_VER_06:
3311 default:
3312 r8153_u2p3en(tp, true);
3313 break;
3314 }
3315
3316 set_bit(PHY_RESET, &tp->flags);
3317 }
3318
r8152_efuse_read(struct r8152 * tp,u8 addr)3319 static u32 r8152_efuse_read(struct r8152 *tp, u8 addr)
3320 {
3321 u32 ocp_data;
3322
3323 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD, EFUSE_READ_CMD | addr);
3324 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_CMD);
3325 ocp_data = (ocp_data & EFUSE_DATA_BIT16) << 9; /* data of bit16 */
3326 ocp_data |= ocp_read_word(tp, MCU_TYPE_PLA, PLA_EFUSE_DATA);
3327
3328 return ocp_data;
3329 }
3330
r8153b_hw_phy_cfg(struct r8152 * tp)3331 static void r8153b_hw_phy_cfg(struct r8152 *tp)
3332 {
3333 u32 ocp_data, ups_flags = 0;
3334 u16 data;
3335
3336 /* disable ALDPS before updating the PHY parameters */
3337 r8153b_aldps_en(tp, false);
3338
3339 /* disable EEE before updating the PHY parameters */
3340 r8153b_eee_en(tp, false);
3341 ocp_reg_write(tp, OCP_EEE_ADV, 0);
3342
3343 r8153b_green_en(tp, test_bit(GREEN_ETHERNET, &tp->flags));
3344
3345 data = sram_read(tp, SRAM_GREEN_CFG);
3346 data |= R_TUNE_EN;
3347 sram_write(tp, SRAM_GREEN_CFG, data);
3348 data = ocp_reg_read(tp, OCP_NCTL_CFG);
3349 data |= PGA_RETURN_EN;
3350 ocp_reg_write(tp, OCP_NCTL_CFG, data);
3351
3352 /* ADC Bias Calibration:
3353 * read efuse offset 0x7d to get a 17-bit data. Remove the dummy/fake
3354 * bit (bit3) to rebuild the real 16-bit data. Write the data to the
3355 * ADC ioffset.
3356 */
3357 ocp_data = r8152_efuse_read(tp, 0x7d);
3358 data = (u16)(((ocp_data & 0x1fff0) >> 1) | (ocp_data & 0x7));
3359 if (data != 0xffff)
3360 ocp_reg_write(tp, OCP_ADC_IOFFSET, data);
3361
3362 /* ups mode tx-link-pulse timing adjustment:
3363 * rg_saw_cnt = OCP reg 0xC426 Bit[13:0]
3364 * swr_cnt_1ms_ini = 16000000 / rg_saw_cnt
3365 */
3366 ocp_data = ocp_reg_read(tp, 0xc426);
3367 ocp_data &= 0x3fff;
3368 if (ocp_data) {
3369 u32 swr_cnt_1ms_ini;
3370
3371 swr_cnt_1ms_ini = (16000000 / ocp_data) & SAW_CNT_1MS_MASK;
3372 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CFG);
3373 ocp_data = (ocp_data & ~SAW_CNT_1MS_MASK) | swr_cnt_1ms_ini;
3374 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CFG, ocp_data);
3375 }
3376
3377 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
3378 ocp_data |= PFM_PWM_SWITCH;
3379 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
3380
3381 /* Advnace EEE */
3382 if (!r8153_patch_request(tp, true)) {
3383 data = ocp_reg_read(tp, OCP_POWER_CFG);
3384 data |= EEE_CLKDIV_EN;
3385 ocp_reg_write(tp, OCP_POWER_CFG, data);
3386
3387 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
3388 data |= EN_EEE_CMODE | EN_EEE_1000 | EN_10M_CLKDIV;
3389 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
3390
3391 ocp_reg_write(tp, OCP_SYSCLK_CFG, 0);
3392 ocp_reg_write(tp, OCP_SYSCLK_CFG, clk_div_expo(5));
3393
3394 ups_flags |= UPS_FLAGS_EN_10M_CKDIV | UPS_FLAGS_250M_CKDIV |
3395 UPS_FLAGS_EN_EEE_CKDIV | UPS_FLAGS_EEE_CMOD_LV_EN |
3396 UPS_FLAGS_EEE_PLLOFF_GIGA;
3397
3398 r8153_patch_request(tp, false);
3399 }
3400
3401 r8153b_ups_flags_w1w0(tp, ups_flags, 0);
3402
3403 r8153b_eee_en(tp, true);
3404 ocp_reg_write(tp, OCP_EEE_ADV, MDIO_EEE_1000T | MDIO_EEE_100TX);
3405
3406 r8153b_aldps_en(tp, true);
3407 r8153b_enable_fc(tp);
3408 r8153_u2p3en(tp, true);
3409
3410 set_bit(PHY_RESET, &tp->flags);
3411 }
3412
r8153_first_init(struct r8152 * tp)3413 static void r8153_first_init(struct r8152 *tp)
3414 {
3415 u32 ocp_data;
3416 int i;
3417
3418 r8153_mac_clk_spd(tp, false);
3419 rxdy_gated_en(tp, true);
3420 r8153_teredo_off(tp);
3421
3422 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
3423 ocp_data &= ~RCR_ACPT_ALL;
3424 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
3425
3426 rtl8152_nic_reset(tp);
3427 rtl_reset_bmu(tp);
3428
3429 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3430 ocp_data &= ~NOW_IS_OOB;
3431 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3432
3433 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3434 ocp_data &= ~MCU_BORW_EN;
3435 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3436
3437 for (i = 0; i < 1000; i++) {
3438 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3439 if (ocp_data & LINK_LIST_READY)
3440 break;
3441 usleep_range(1000, 2000);
3442 }
3443
3444 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3445 ocp_data |= RE_INIT_LL;
3446 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3447
3448 for (i = 0; i < 1000; i++) {
3449 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3450 if (ocp_data & LINK_LIST_READY)
3451 break;
3452 usleep_range(1000, 2000);
3453 }
3454
3455 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
3456
3457 ocp_data = tp->netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
3458 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, ocp_data);
3459 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO);
3460
3461 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
3462 ocp_data |= TCR0_AUTO_FIFO;
3463 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
3464
3465 rtl8152_nic_reset(tp);
3466
3467 /* rx share fifo credit full threshold */
3468 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
3469 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
3470 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
3471 /* TX share fifo free credit full threshold */
3472 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
3473 }
3474
r8153_enter_oob(struct r8152 * tp)3475 static void r8153_enter_oob(struct r8152 *tp)
3476 {
3477 u32 ocp_data;
3478 int i;
3479
3480 r8153_mac_clk_spd(tp, true);
3481
3482 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3483 ocp_data &= ~NOW_IS_OOB;
3484 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3485
3486 rtl_disable(tp);
3487 rtl_reset_bmu(tp);
3488
3489 for (i = 0; i < 1000; i++) {
3490 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3491 if (ocp_data & LINK_LIST_READY)
3492 break;
3493 usleep_range(1000, 2000);
3494 }
3495
3496 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
3497 ocp_data |= RE_INIT_LL;
3498 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
3499
3500 for (i = 0; i < 1000; i++) {
3501 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3502 if (ocp_data & LINK_LIST_READY)
3503 break;
3504 usleep_range(1000, 2000);
3505 }
3506
3507 ocp_data = tp->netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
3508 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, ocp_data);
3509
3510 switch (tp->version) {
3511 case RTL_VER_03:
3512 case RTL_VER_04:
3513 case RTL_VER_05:
3514 case RTL_VER_06:
3515 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
3516 ocp_data &= ~TEREDO_WAKE_MASK;
3517 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
3518 break;
3519
3520 case RTL_VER_08:
3521 case RTL_VER_09:
3522 /* Clear teredo wake event. bit[15:8] is the teredo wakeup
3523 * type. Set it to zero. bits[7:0] are the W1C bits about
3524 * the events. Set them to all 1 to clear them.
3525 */
3526 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_WAKE_BASE, 0x00ff);
3527 break;
3528
3529 default:
3530 break;
3531 }
3532
3533 rtl_rx_vlan_en(tp, true);
3534
3535 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
3536 ocp_data |= ALDPS_PROXY_MODE;
3537 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
3538
3539 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
3540 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
3541 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
3542
3543 rxdy_gated_en(tp, false);
3544
3545 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
3546 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
3547 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
3548 }
3549
rtl8153_disable(struct r8152 * tp)3550 static void rtl8153_disable(struct r8152 *tp)
3551 {
3552 r8153_aldps_en(tp, false);
3553 rtl_disable(tp);
3554 rtl_reset_bmu(tp);
3555 r8153_aldps_en(tp, true);
3556 }
3557
rtl8153b_disable(struct r8152 * tp)3558 static void rtl8153b_disable(struct r8152 *tp)
3559 {
3560 r8153b_aldps_en(tp, false);
3561 rtl_disable(tp);
3562 rtl_reset_bmu(tp);
3563 r8153b_aldps_en(tp, true);
3564 }
3565
rtl8152_set_speed(struct r8152 * tp,u8 autoneg,u16 speed,u8 duplex)3566 static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex)
3567 {
3568 u16 bmcr, anar, gbcr;
3569 enum spd_duplex speed_duplex;
3570 int ret = 0;
3571
3572 anar = r8152_mdio_read(tp, MII_ADVERTISE);
3573 anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
3574 ADVERTISE_100HALF | ADVERTISE_100FULL);
3575 if (tp->mii.supports_gmii) {
3576 gbcr = r8152_mdio_read(tp, MII_CTRL1000);
3577 gbcr &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
3578 } else {
3579 gbcr = 0;
3580 }
3581
3582 if (autoneg == AUTONEG_DISABLE) {
3583 if (speed == SPEED_10) {
3584 bmcr = 0;
3585 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
3586 speed_duplex = FORCE_10M_HALF;
3587 } else if (speed == SPEED_100) {
3588 bmcr = BMCR_SPEED100;
3589 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
3590 speed_duplex = FORCE_100M_HALF;
3591 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
3592 bmcr = BMCR_SPEED1000;
3593 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
3594 speed_duplex = NWAY_1000M_FULL;
3595 } else {
3596 ret = -EINVAL;
3597 goto out;
3598 }
3599
3600 if (duplex == DUPLEX_FULL) {
3601 bmcr |= BMCR_FULLDPLX;
3602 if (speed != SPEED_1000)
3603 speed_duplex++;
3604 }
3605 } else {
3606 if (speed == SPEED_10) {
3607 if (duplex == DUPLEX_FULL) {
3608 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
3609 speed_duplex = NWAY_10M_FULL;
3610 } else {
3611 anar |= ADVERTISE_10HALF;
3612 speed_duplex = NWAY_10M_HALF;
3613 }
3614 } else if (speed == SPEED_100) {
3615 if (duplex == DUPLEX_FULL) {
3616 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
3617 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
3618 speed_duplex = NWAY_100M_FULL;
3619 } else {
3620 anar |= ADVERTISE_10HALF;
3621 anar |= ADVERTISE_100HALF;
3622 speed_duplex = NWAY_100M_HALF;
3623 }
3624 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
3625 if (duplex == DUPLEX_FULL) {
3626 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
3627 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
3628 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
3629 } else {
3630 anar |= ADVERTISE_10HALF;
3631 anar |= ADVERTISE_100HALF;
3632 gbcr |= ADVERTISE_1000HALF;
3633 }
3634 speed_duplex = NWAY_1000M_FULL;
3635 } else {
3636 ret = -EINVAL;
3637 goto out;
3638 }
3639
3640 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
3641 }
3642
3643 if (test_and_clear_bit(PHY_RESET, &tp->flags))
3644 bmcr |= BMCR_RESET;
3645
3646 if (tp->mii.supports_gmii)
3647 r8152_mdio_write(tp, MII_CTRL1000, gbcr);
3648
3649 r8152_mdio_write(tp, MII_ADVERTISE, anar);
3650 r8152_mdio_write(tp, MII_BMCR, bmcr);
3651
3652 switch (tp->version) {
3653 case RTL_VER_08:
3654 case RTL_VER_09:
3655 r8153b_ups_flags_w1w0(tp, ups_flags_speed(speed_duplex),
3656 UPS_FLAGS_SPEED_MASK);
3657 break;
3658
3659 default:
3660 break;
3661 }
3662
3663 if (bmcr & BMCR_RESET) {
3664 int i;
3665
3666 for (i = 0; i < 50; i++) {
3667 msleep(20);
3668 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
3669 break;
3670 }
3671 }
3672
3673 out:
3674 return ret;
3675 }
3676
rtl8152_up(struct r8152 * tp)3677 static void rtl8152_up(struct r8152 *tp)
3678 {
3679 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3680 return;
3681
3682 r8152_aldps_en(tp, false);
3683 r8152b_exit_oob(tp);
3684 r8152_aldps_en(tp, true);
3685 }
3686
rtl8152_down(struct r8152 * tp)3687 static void rtl8152_down(struct r8152 *tp)
3688 {
3689 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
3690 rtl_drop_queued_tx(tp);
3691 return;
3692 }
3693
3694 r8152_power_cut_en(tp, false);
3695 r8152_aldps_en(tp, false);
3696 r8152b_enter_oob(tp);
3697 r8152_aldps_en(tp, true);
3698 }
3699
rtl8153_up(struct r8152 * tp)3700 static void rtl8153_up(struct r8152 *tp)
3701 {
3702 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3703 return;
3704
3705 r8153_u1u2en(tp, false);
3706 r8153_u2p3en(tp, false);
3707 r8153_aldps_en(tp, false);
3708 r8153_first_init(tp);
3709 r8153_aldps_en(tp, true);
3710
3711 switch (tp->version) {
3712 case RTL_VER_03:
3713 case RTL_VER_04:
3714 break;
3715 case RTL_VER_05:
3716 case RTL_VER_06:
3717 default:
3718 r8153_u2p3en(tp, true);
3719 break;
3720 }
3721
3722 r8153_u1u2en(tp, true);
3723 }
3724
rtl8153_down(struct r8152 * tp)3725 static void rtl8153_down(struct r8152 *tp)
3726 {
3727 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
3728 rtl_drop_queued_tx(tp);
3729 return;
3730 }
3731
3732 r8153_u1u2en(tp, false);
3733 r8153_u2p3en(tp, false);
3734 r8153_power_cut_en(tp, false);
3735 r8153_aldps_en(tp, false);
3736 r8153_enter_oob(tp);
3737 r8153_aldps_en(tp, true);
3738 }
3739
rtl8153b_up(struct r8152 * tp)3740 static void rtl8153b_up(struct r8152 *tp)
3741 {
3742 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3743 return;
3744
3745 r8153b_u1u2en(tp, false);
3746 r8153_u2p3en(tp, false);
3747 r8153b_aldps_en(tp, false);
3748
3749 r8153_first_init(tp);
3750 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_B);
3751
3752 r8153b_aldps_en(tp, true);
3753 r8153_u2p3en(tp, true);
3754 r8153b_u1u2en(tp, true);
3755 }
3756
rtl8153b_down(struct r8152 * tp)3757 static void rtl8153b_down(struct r8152 *tp)
3758 {
3759 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
3760 rtl_drop_queued_tx(tp);
3761 return;
3762 }
3763
3764 r8153b_u1u2en(tp, false);
3765 r8153_u2p3en(tp, false);
3766 r8153b_power_cut_en(tp, false);
3767 r8153b_aldps_en(tp, false);
3768 r8153_enter_oob(tp);
3769 r8153b_aldps_en(tp, true);
3770 }
3771
rtl8152_in_nway(struct r8152 * tp)3772 static bool rtl8152_in_nway(struct r8152 *tp)
3773 {
3774 u16 nway_state;
3775
3776 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, 0x2000);
3777 tp->ocp_base = 0x2000;
3778 ocp_write_byte(tp, MCU_TYPE_PLA, 0xb014, 0x4c); /* phy state */
3779 nway_state = ocp_read_word(tp, MCU_TYPE_PLA, 0xb01a);
3780
3781 /* bit 15: TXDIS_STATE, bit 14: ABD_STATE */
3782 if (nway_state & 0xc000)
3783 return false;
3784 else
3785 return true;
3786 }
3787
rtl8153_in_nway(struct r8152 * tp)3788 static bool rtl8153_in_nway(struct r8152 *tp)
3789 {
3790 u16 phy_state = ocp_reg_read(tp, OCP_PHY_STATE) & 0xff;
3791
3792 if (phy_state == TXDIS_STATE || phy_state == ABD_STATE)
3793 return false;
3794 else
3795 return true;
3796 }
3797
set_carrier(struct r8152 * tp)3798 static void set_carrier(struct r8152 *tp)
3799 {
3800 struct net_device *netdev = tp->netdev;
3801 struct napi_struct *napi = &tp->napi;
3802 u8 speed;
3803
3804 speed = rtl8152_get_speed(tp);
3805
3806 if (speed & LINK_STATUS) {
3807 if (!netif_carrier_ok(netdev)) {
3808 tp->rtl_ops.enable(tp);
3809 netif_stop_queue(netdev);
3810 napi_disable(napi);
3811 netif_carrier_on(netdev);
3812 rtl_start_rx(tp);
3813 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
3814 _rtl8152_set_rx_mode(netdev);
3815 napi_enable(&tp->napi);
3816 netif_wake_queue(netdev);
3817 netif_info(tp, link, netdev, "carrier on\n");
3818 } else if (netif_queue_stopped(netdev) &&
3819 skb_queue_len(&tp->tx_queue) < tp->tx_qlen) {
3820 netif_wake_queue(netdev);
3821 }
3822 } else {
3823 if (netif_carrier_ok(netdev)) {
3824 netif_carrier_off(netdev);
3825 napi_disable(napi);
3826 tp->rtl_ops.disable(tp);
3827 napi_enable(napi);
3828 netif_info(tp, link, netdev, "carrier off\n");
3829 }
3830 }
3831 }
3832
rtl_work_func_t(struct work_struct * work)3833 static void rtl_work_func_t(struct work_struct *work)
3834 {
3835 struct r8152 *tp = container_of(work, struct r8152, schedule.work);
3836
3837 /* If the device is unplugged or !netif_running(), the workqueue
3838 * doesn't need to wake the device, and could return directly.
3839 */
3840 if (test_bit(RTL8152_UNPLUG, &tp->flags) || !netif_running(tp->netdev))
3841 return;
3842
3843 if (usb_autopm_get_interface(tp->intf) < 0)
3844 return;
3845
3846 if (!test_bit(WORK_ENABLE, &tp->flags))
3847 goto out1;
3848
3849 if (!mutex_trylock(&tp->control)) {
3850 schedule_delayed_work(&tp->schedule, 0);
3851 goto out1;
3852 }
3853
3854 if (test_and_clear_bit(RTL8152_LINK_CHG, &tp->flags))
3855 set_carrier(tp);
3856
3857 if (test_and_clear_bit(RTL8152_SET_RX_MODE, &tp->flags))
3858 _rtl8152_set_rx_mode(tp->netdev);
3859
3860 /* don't schedule napi before linking */
3861 if (test_and_clear_bit(SCHEDULE_NAPI, &tp->flags) &&
3862 netif_carrier_ok(tp->netdev))
3863 napi_schedule(&tp->napi);
3864
3865 mutex_unlock(&tp->control);
3866
3867 out1:
3868 usb_autopm_put_interface(tp->intf);
3869 }
3870
rtl_hw_phy_work_func_t(struct work_struct * work)3871 static void rtl_hw_phy_work_func_t(struct work_struct *work)
3872 {
3873 struct r8152 *tp = container_of(work, struct r8152, hw_phy_work.work);
3874
3875 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3876 return;
3877
3878 if (usb_autopm_get_interface(tp->intf) < 0)
3879 return;
3880
3881 mutex_lock(&tp->control);
3882
3883 tp->rtl_ops.hw_phy_cfg(tp);
3884
3885 rtl8152_set_speed(tp, tp->autoneg, tp->speed, tp->duplex);
3886
3887 mutex_unlock(&tp->control);
3888
3889 usb_autopm_put_interface(tp->intf);
3890 }
3891
3892 #ifdef CONFIG_PM_SLEEP
rtl_notifier(struct notifier_block * nb,unsigned long action,void * data)3893 static int rtl_notifier(struct notifier_block *nb, unsigned long action,
3894 void *data)
3895 {
3896 struct r8152 *tp = container_of(nb, struct r8152, pm_notifier);
3897
3898 switch (action) {
3899 case PM_HIBERNATION_PREPARE:
3900 case PM_SUSPEND_PREPARE:
3901 usb_autopm_get_interface(tp->intf);
3902 break;
3903
3904 case PM_POST_HIBERNATION:
3905 case PM_POST_SUSPEND:
3906 usb_autopm_put_interface(tp->intf);
3907 break;
3908
3909 case PM_POST_RESTORE:
3910 case PM_RESTORE_PREPARE:
3911 default:
3912 break;
3913 }
3914
3915 return NOTIFY_DONE;
3916 }
3917 #endif
3918
rtl8152_open(struct net_device * netdev)3919 static int rtl8152_open(struct net_device *netdev)
3920 {
3921 struct r8152 *tp = netdev_priv(netdev);
3922 int res = 0;
3923
3924 res = alloc_all_mem(tp);
3925 if (res)
3926 goto out;
3927
3928 res = usb_autopm_get_interface(tp->intf);
3929 if (res < 0)
3930 goto out_free;
3931
3932 mutex_lock(&tp->control);
3933
3934 tp->rtl_ops.up(tp);
3935
3936 netif_carrier_off(netdev);
3937 netif_start_queue(netdev);
3938 set_bit(WORK_ENABLE, &tp->flags);
3939
3940 res = usb_submit_urb(tp->intr_urb, GFP_KERNEL);
3941 if (res) {
3942 if (res == -ENODEV)
3943 netif_device_detach(tp->netdev);
3944 netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n",
3945 res);
3946 goto out_unlock;
3947 }
3948 napi_enable(&tp->napi);
3949
3950 mutex_unlock(&tp->control);
3951
3952 usb_autopm_put_interface(tp->intf);
3953 #ifdef CONFIG_PM_SLEEP
3954 tp->pm_notifier.notifier_call = rtl_notifier;
3955 register_pm_notifier(&tp->pm_notifier);
3956 #endif
3957 return 0;
3958
3959 out_unlock:
3960 mutex_unlock(&tp->control);
3961 usb_autopm_put_interface(tp->intf);
3962 out_free:
3963 free_all_mem(tp);
3964 out:
3965 return res;
3966 }
3967
rtl8152_close(struct net_device * netdev)3968 static int rtl8152_close(struct net_device *netdev)
3969 {
3970 struct r8152 *tp = netdev_priv(netdev);
3971 int res = 0;
3972
3973 #ifdef CONFIG_PM_SLEEP
3974 unregister_pm_notifier(&tp->pm_notifier);
3975 #endif
3976 if (!test_bit(RTL8152_UNPLUG, &tp->flags))
3977 napi_disable(&tp->napi);
3978 clear_bit(WORK_ENABLE, &tp->flags);
3979 usb_kill_urb(tp->intr_urb);
3980 cancel_delayed_work_sync(&tp->schedule);
3981 netif_stop_queue(netdev);
3982
3983 res = usb_autopm_get_interface(tp->intf);
3984 if (res < 0 || test_bit(RTL8152_UNPLUG, &tp->flags)) {
3985 rtl_drop_queued_tx(tp);
3986 rtl_stop_rx(tp);
3987 } else {
3988 mutex_lock(&tp->control);
3989
3990 tp->rtl_ops.down(tp);
3991
3992 mutex_unlock(&tp->control);
3993
3994 usb_autopm_put_interface(tp->intf);
3995 }
3996
3997 free_all_mem(tp);
3998
3999 return res;
4000 }
4001
rtl_tally_reset(struct r8152 * tp)4002 static void rtl_tally_reset(struct r8152 *tp)
4003 {
4004 u32 ocp_data;
4005
4006 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY);
4007 ocp_data |= TALLY_RESET;
4008 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY, ocp_data);
4009 }
4010
r8152b_init(struct r8152 * tp)4011 static void r8152b_init(struct r8152 *tp)
4012 {
4013 u32 ocp_data;
4014 u16 data;
4015
4016 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4017 return;
4018
4019 data = r8152_mdio_read(tp, MII_BMCR);
4020 if (data & BMCR_PDOWN) {
4021 data &= ~BMCR_PDOWN;
4022 r8152_mdio_write(tp, MII_BMCR, data);
4023 }
4024
4025 r8152_aldps_en(tp, false);
4026
4027 if (tp->version == RTL_VER_01) {
4028 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
4029 ocp_data &= ~LED_MODE_MASK;
4030 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
4031 }
4032
4033 r8152_power_cut_en(tp, false);
4034
4035 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
4036 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
4037 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
4038 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
4039 ocp_data &= ~MCU_CLK_RATIO_MASK;
4040 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
4041 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
4042 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
4043 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
4044 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
4045
4046 rtl_tally_reset(tp);
4047
4048 /* enable rx aggregation */
4049 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
4050 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
4051 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
4052 }
4053
r8153_init(struct r8152 * tp)4054 static void r8153_init(struct r8152 *tp)
4055 {
4056 u32 ocp_data;
4057 u16 data;
4058 int i;
4059
4060 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4061 return;
4062
4063 r8153_u1u2en(tp, false);
4064
4065 for (i = 0; i < 500; i++) {
4066 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
4067 AUTOLOAD_DONE)
4068 break;
4069
4070 msleep(20);
4071 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4072 break;
4073 }
4074
4075 data = r8153_phy_status(tp, 0);
4076
4077 if (tp->version == RTL_VER_03 || tp->version == RTL_VER_04 ||
4078 tp->version == RTL_VER_05)
4079 ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L);
4080
4081 data = r8152_mdio_read(tp, MII_BMCR);
4082 if (data & BMCR_PDOWN) {
4083 data &= ~BMCR_PDOWN;
4084 r8152_mdio_write(tp, MII_BMCR, data);
4085 }
4086
4087 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
4088
4089 r8153_u2p3en(tp, false);
4090
4091 if (tp->version == RTL_VER_04) {
4092 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2);
4093 ocp_data &= ~pwd_dn_scale_mask;
4094 ocp_data |= pwd_dn_scale(96);
4095 ocp_write_word(tp, MCU_TYPE_USB, USB_SSPHYLINK2, ocp_data);
4096
4097 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_USB2PHY);
4098 ocp_data |= USB2PHY_L1 | USB2PHY_SUSPEND;
4099 ocp_write_byte(tp, MCU_TYPE_USB, USB_USB2PHY, ocp_data);
4100 } else if (tp->version == RTL_VER_05) {
4101 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0);
4102 ocp_data &= ~ECM_ALDPS;
4103 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_DMY_REG0, ocp_data);
4104
4105 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
4106 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
4107 ocp_data &= ~DYNAMIC_BURST;
4108 else
4109 ocp_data |= DYNAMIC_BURST;
4110 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
4111 } else if (tp->version == RTL_VER_06) {
4112 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1);
4113 if (ocp_read_word(tp, MCU_TYPE_USB, USB_BURST_SIZE) == 0)
4114 ocp_data &= ~DYNAMIC_BURST;
4115 else
4116 ocp_data |= DYNAMIC_BURST;
4117 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY1, ocp_data);
4118 }
4119
4120 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2);
4121 ocp_data |= EP4_FULL_FC;
4122 ocp_write_byte(tp, MCU_TYPE_USB, USB_CSR_DUMMY2, ocp_data);
4123
4124 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL);
4125 ocp_data &= ~TIMER11_EN;
4126 ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data);
4127
4128 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
4129 ocp_data &= ~LED_MODE_MASK;
4130 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
4131
4132 ocp_data = FIFO_EMPTY_1FB | ROK_EXIT_LPM;
4133 if (tp->version == RTL_VER_04 && tp->udev->speed < USB_SPEED_SUPER)
4134 ocp_data |= LPM_TIMER_500MS;
4135 else
4136 ocp_data |= LPM_TIMER_500US;
4137 ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data);
4138
4139 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2);
4140 ocp_data &= ~SEN_VAL_MASK;
4141 ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE;
4142 ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data);
4143
4144 ocp_write_word(tp, MCU_TYPE_USB, USB_CONNECT_TIMER, 0x0001);
4145
4146 r8153_power_cut_en(tp, false);
4147 r8153_u1u2en(tp, true);
4148 r8153_mac_clk_spd(tp, false);
4149 usb_enable_lpm(tp->udev);
4150
4151 /* rx aggregation */
4152 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
4153 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
4154 if (test_bit(DELL_TB_RX_AGG_BUG, &tp->flags))
4155 ocp_data |= RX_AGG_DISABLE;
4156
4157 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
4158
4159 rtl_tally_reset(tp);
4160
4161 switch (tp->udev->speed) {
4162 case USB_SPEED_SUPER:
4163 case USB_SPEED_SUPER_PLUS:
4164 tp->coalesce = COALESCE_SUPER;
4165 break;
4166 case USB_SPEED_HIGH:
4167 tp->coalesce = COALESCE_HIGH;
4168 break;
4169 default:
4170 tp->coalesce = COALESCE_SLOW;
4171 break;
4172 }
4173 }
4174
r8153b_init(struct r8152 * tp)4175 static void r8153b_init(struct r8152 *tp)
4176 {
4177 u32 ocp_data;
4178 u16 data;
4179 int i;
4180
4181 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4182 return;
4183
4184 r8153b_u1u2en(tp, false);
4185
4186 for (i = 0; i < 500; i++) {
4187 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
4188 AUTOLOAD_DONE)
4189 break;
4190
4191 msleep(20);
4192 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4193 break;
4194 }
4195
4196 data = r8153_phy_status(tp, 0);
4197
4198 data = r8152_mdio_read(tp, MII_BMCR);
4199 if (data & BMCR_PDOWN) {
4200 data &= ~BMCR_PDOWN;
4201 r8152_mdio_write(tp, MII_BMCR, data);
4202 }
4203
4204 data = r8153_phy_status(tp, PHY_STAT_LAN_ON);
4205
4206 r8153_u2p3en(tp, false);
4207
4208 /* MSC timer = 0xfff * 8ms = 32760 ms */
4209 ocp_write_word(tp, MCU_TYPE_USB, USB_MSC_TIMER, 0x0fff);
4210
4211 /* U1/U2/L1 idle timer. 500 us */
4212 ocp_write_word(tp, MCU_TYPE_USB, USB_U1U2_TIMER, 500);
4213
4214 r8153b_power_cut_en(tp, false);
4215 r8153b_ups_en(tp, false);
4216 r8153b_queue_wake(tp, false);
4217 rtl_runtime_suspend_enable(tp, false);
4218 r8153b_u1u2en(tp, true);
4219 usb_enable_lpm(tp->udev);
4220
4221 /* MAC clock speed down */
4222 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2);
4223 ocp_data |= MAC_CLK_SPDWN_EN;
4224 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, ocp_data);
4225
4226 set_bit(GREEN_ETHERNET, &tp->flags);
4227
4228 /* rx aggregation */
4229 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
4230 ocp_data &= ~(RX_AGG_DISABLE | RX_ZERO_EN);
4231 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
4232
4233 rtl_tally_reset(tp);
4234
4235 tp->coalesce = 15000; /* 15 us */
4236 }
4237
rtl8152_pre_reset(struct usb_interface * intf)4238 static int rtl8152_pre_reset(struct usb_interface *intf)
4239 {
4240 struct r8152 *tp = usb_get_intfdata(intf);
4241 struct net_device *netdev;
4242
4243 if (!tp)
4244 return 0;
4245
4246 netdev = tp->netdev;
4247 if (!netif_running(netdev))
4248 return 0;
4249
4250 netif_stop_queue(netdev);
4251 napi_disable(&tp->napi);
4252 clear_bit(WORK_ENABLE, &tp->flags);
4253 usb_kill_urb(tp->intr_urb);
4254 cancel_delayed_work_sync(&tp->schedule);
4255 if (netif_carrier_ok(netdev)) {
4256 mutex_lock(&tp->control);
4257 tp->rtl_ops.disable(tp);
4258 mutex_unlock(&tp->control);
4259 }
4260
4261 return 0;
4262 }
4263
rtl8152_post_reset(struct usb_interface * intf)4264 static int rtl8152_post_reset(struct usb_interface *intf)
4265 {
4266 struct r8152 *tp = usb_get_intfdata(intf);
4267 struct net_device *netdev;
4268
4269 if (!tp)
4270 return 0;
4271
4272 netdev = tp->netdev;
4273 if (!netif_running(netdev))
4274 return 0;
4275
4276 set_bit(WORK_ENABLE, &tp->flags);
4277 if (netif_carrier_ok(netdev)) {
4278 mutex_lock(&tp->control);
4279 tp->rtl_ops.enable(tp);
4280 rtl_start_rx(tp);
4281 _rtl8152_set_rx_mode(netdev);
4282 mutex_unlock(&tp->control);
4283 }
4284
4285 napi_enable(&tp->napi);
4286 netif_wake_queue(netdev);
4287 usb_submit_urb(tp->intr_urb, GFP_KERNEL);
4288
4289 if (!list_empty(&tp->rx_done))
4290 napi_schedule(&tp->napi);
4291
4292 return 0;
4293 }
4294
delay_autosuspend(struct r8152 * tp)4295 static bool delay_autosuspend(struct r8152 *tp)
4296 {
4297 bool sw_linking = !!netif_carrier_ok(tp->netdev);
4298 bool hw_linking = !!(rtl8152_get_speed(tp) & LINK_STATUS);
4299
4300 /* This means a linking change occurs and the driver doesn't detect it,
4301 * yet. If the driver has disabled tx/rx and hw is linking on, the
4302 * device wouldn't wake up by receiving any packet.
4303 */
4304 if (work_busy(&tp->schedule.work) || sw_linking != hw_linking)
4305 return true;
4306
4307 /* If the linking down is occurred by nway, the device may miss the
4308 * linking change event. And it wouldn't wake when linking on.
4309 */
4310 if (!sw_linking && tp->rtl_ops.in_nway(tp))
4311 return true;
4312 else if (!skb_queue_empty(&tp->tx_queue))
4313 return true;
4314 else
4315 return false;
4316 }
4317
rtl8152_runtime_resume(struct r8152 * tp)4318 static int rtl8152_runtime_resume(struct r8152 *tp)
4319 {
4320 struct net_device *netdev = tp->netdev;
4321
4322 if (netif_running(netdev) && netdev->flags & IFF_UP) {
4323 struct napi_struct *napi = &tp->napi;
4324
4325 tp->rtl_ops.autosuspend_en(tp, false);
4326 napi_disable(napi);
4327 set_bit(WORK_ENABLE, &tp->flags);
4328
4329 if (netif_carrier_ok(netdev)) {
4330 if (rtl8152_get_speed(tp) & LINK_STATUS) {
4331 rtl_start_rx(tp);
4332 } else {
4333 netif_carrier_off(netdev);
4334 tp->rtl_ops.disable(tp);
4335 netif_info(tp, link, netdev, "linking down\n");
4336 }
4337 }
4338
4339 napi_enable(napi);
4340 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
4341 smp_mb__after_atomic();
4342
4343 if (!list_empty(&tp->rx_done))
4344 napi_schedule(&tp->napi);
4345
4346 usb_submit_urb(tp->intr_urb, GFP_NOIO);
4347 } else {
4348 if (netdev->flags & IFF_UP)
4349 tp->rtl_ops.autosuspend_en(tp, false);
4350
4351 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
4352 }
4353
4354 return 0;
4355 }
4356
rtl8152_system_resume(struct r8152 * tp)4357 static int rtl8152_system_resume(struct r8152 *tp)
4358 {
4359 struct net_device *netdev = tp->netdev;
4360
4361 netif_device_attach(netdev);
4362
4363 if (netif_running(netdev) && netdev->flags & IFF_UP) {
4364 tp->rtl_ops.up(tp);
4365 netif_carrier_off(netdev);
4366 set_bit(WORK_ENABLE, &tp->flags);
4367 usb_submit_urb(tp->intr_urb, GFP_NOIO);
4368 }
4369
4370 return 0;
4371 }
4372
rtl8152_runtime_suspend(struct r8152 * tp)4373 static int rtl8152_runtime_suspend(struct r8152 *tp)
4374 {
4375 struct net_device *netdev = tp->netdev;
4376 int ret = 0;
4377
4378 set_bit(SELECTIVE_SUSPEND, &tp->flags);
4379 smp_mb__after_atomic();
4380
4381 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
4382 u32 rcr = 0;
4383
4384 if (netif_carrier_ok(netdev)) {
4385 u32 ocp_data;
4386
4387 rcr = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
4388 ocp_data = rcr & ~RCR_ACPT_ALL;
4389 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
4390 rxdy_gated_en(tp, true);
4391 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA,
4392 PLA_OOB_CTRL);
4393 if (!(ocp_data & RXFIFO_EMPTY)) {
4394 rxdy_gated_en(tp, false);
4395 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr);
4396 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
4397 smp_mb__after_atomic();
4398 ret = -EBUSY;
4399 goto out1;
4400 }
4401 }
4402
4403 clear_bit(WORK_ENABLE, &tp->flags);
4404 usb_kill_urb(tp->intr_urb);
4405
4406 tp->rtl_ops.autosuspend_en(tp, true);
4407
4408 if (netif_carrier_ok(netdev)) {
4409 struct napi_struct *napi = &tp->napi;
4410
4411 napi_disable(napi);
4412 rtl_stop_rx(tp);
4413 rxdy_gated_en(tp, false);
4414 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, rcr);
4415 napi_enable(napi);
4416 }
4417
4418 if (delay_autosuspend(tp)) {
4419 rtl8152_runtime_resume(tp);
4420 ret = -EBUSY;
4421 }
4422 }
4423
4424 out1:
4425 return ret;
4426 }
4427
rtl8152_system_suspend(struct r8152 * tp)4428 static int rtl8152_system_suspend(struct r8152 *tp)
4429 {
4430 struct net_device *netdev = tp->netdev;
4431
4432 netif_device_detach(netdev);
4433
4434 if (netif_running(netdev) && test_bit(WORK_ENABLE, &tp->flags)) {
4435 struct napi_struct *napi = &tp->napi;
4436
4437 clear_bit(WORK_ENABLE, &tp->flags);
4438 usb_kill_urb(tp->intr_urb);
4439 napi_disable(napi);
4440 cancel_delayed_work_sync(&tp->schedule);
4441 tp->rtl_ops.down(tp);
4442 napi_enable(napi);
4443 }
4444
4445 return 0;
4446 }
4447
rtl8152_suspend(struct usb_interface * intf,pm_message_t message)4448 static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
4449 {
4450 struct r8152 *tp = usb_get_intfdata(intf);
4451 int ret;
4452
4453 mutex_lock(&tp->control);
4454
4455 if (PMSG_IS_AUTO(message))
4456 ret = rtl8152_runtime_suspend(tp);
4457 else
4458 ret = rtl8152_system_suspend(tp);
4459
4460 mutex_unlock(&tp->control);
4461
4462 return ret;
4463 }
4464
rtl8152_resume(struct usb_interface * intf)4465 static int rtl8152_resume(struct usb_interface *intf)
4466 {
4467 struct r8152 *tp = usb_get_intfdata(intf);
4468 int ret;
4469
4470 mutex_lock(&tp->control);
4471
4472 if (test_bit(SELECTIVE_SUSPEND, &tp->flags))
4473 ret = rtl8152_runtime_resume(tp);
4474 else
4475 ret = rtl8152_system_resume(tp);
4476
4477 mutex_unlock(&tp->control);
4478
4479 return ret;
4480 }
4481
rtl8152_reset_resume(struct usb_interface * intf)4482 static int rtl8152_reset_resume(struct usb_interface *intf)
4483 {
4484 struct r8152 *tp = usb_get_intfdata(intf);
4485
4486 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
4487 tp->rtl_ops.init(tp);
4488 queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0);
4489 set_ethernet_addr(tp);
4490 return rtl8152_resume(intf);
4491 }
4492
rtl8152_get_wol(struct net_device * dev,struct ethtool_wolinfo * wol)4493 static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
4494 {
4495 struct r8152 *tp = netdev_priv(dev);
4496
4497 if (usb_autopm_get_interface(tp->intf) < 0)
4498 return;
4499
4500 if (!rtl_can_wakeup(tp)) {
4501 wol->supported = 0;
4502 wol->wolopts = 0;
4503 } else {
4504 mutex_lock(&tp->control);
4505 wol->supported = WAKE_ANY;
4506 wol->wolopts = __rtl_get_wol(tp);
4507 mutex_unlock(&tp->control);
4508 }
4509
4510 usb_autopm_put_interface(tp->intf);
4511 }
4512
rtl8152_set_wol(struct net_device * dev,struct ethtool_wolinfo * wol)4513 static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
4514 {
4515 struct r8152 *tp = netdev_priv(dev);
4516 int ret;
4517
4518 if (!rtl_can_wakeup(tp))
4519 return -EOPNOTSUPP;
4520
4521 if (wol->wolopts & ~WAKE_ANY)
4522 return -EINVAL;
4523
4524 ret = usb_autopm_get_interface(tp->intf);
4525 if (ret < 0)
4526 goto out_set_wol;
4527
4528 mutex_lock(&tp->control);
4529
4530 __rtl_set_wol(tp, wol->wolopts);
4531 tp->saved_wolopts = wol->wolopts & WAKE_ANY;
4532
4533 mutex_unlock(&tp->control);
4534
4535 usb_autopm_put_interface(tp->intf);
4536
4537 out_set_wol:
4538 return ret;
4539 }
4540
rtl8152_get_msglevel(struct net_device * dev)4541 static u32 rtl8152_get_msglevel(struct net_device *dev)
4542 {
4543 struct r8152 *tp = netdev_priv(dev);
4544
4545 return tp->msg_enable;
4546 }
4547
rtl8152_set_msglevel(struct net_device * dev,u32 value)4548 static void rtl8152_set_msglevel(struct net_device *dev, u32 value)
4549 {
4550 struct r8152 *tp = netdev_priv(dev);
4551
4552 tp->msg_enable = value;
4553 }
4554
rtl8152_get_drvinfo(struct net_device * netdev,struct ethtool_drvinfo * info)4555 static void rtl8152_get_drvinfo(struct net_device *netdev,
4556 struct ethtool_drvinfo *info)
4557 {
4558 struct r8152 *tp = netdev_priv(netdev);
4559
4560 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
4561 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
4562 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
4563 }
4564
4565 static
rtl8152_get_link_ksettings(struct net_device * netdev,struct ethtool_link_ksettings * cmd)4566 int rtl8152_get_link_ksettings(struct net_device *netdev,
4567 struct ethtool_link_ksettings *cmd)
4568 {
4569 struct r8152 *tp = netdev_priv(netdev);
4570 int ret;
4571
4572 if (!tp->mii.mdio_read)
4573 return -EOPNOTSUPP;
4574
4575 ret = usb_autopm_get_interface(tp->intf);
4576 if (ret < 0)
4577 goto out;
4578
4579 mutex_lock(&tp->control);
4580
4581 mii_ethtool_get_link_ksettings(&tp->mii, cmd);
4582
4583 mutex_unlock(&tp->control);
4584
4585 usb_autopm_put_interface(tp->intf);
4586
4587 out:
4588 return ret;
4589 }
4590
rtl8152_set_link_ksettings(struct net_device * dev,const struct ethtool_link_ksettings * cmd)4591 static int rtl8152_set_link_ksettings(struct net_device *dev,
4592 const struct ethtool_link_ksettings *cmd)
4593 {
4594 struct r8152 *tp = netdev_priv(dev);
4595 int ret;
4596
4597 ret = usb_autopm_get_interface(tp->intf);
4598 if (ret < 0)
4599 goto out;
4600
4601 mutex_lock(&tp->control);
4602
4603 ret = rtl8152_set_speed(tp, cmd->base.autoneg, cmd->base.speed,
4604 cmd->base.duplex);
4605 if (!ret) {
4606 tp->autoneg = cmd->base.autoneg;
4607 tp->speed = cmd->base.speed;
4608 tp->duplex = cmd->base.duplex;
4609 }
4610
4611 mutex_unlock(&tp->control);
4612
4613 usb_autopm_put_interface(tp->intf);
4614
4615 out:
4616 return ret;
4617 }
4618
4619 static const char rtl8152_gstrings[][ETH_GSTRING_LEN] = {
4620 "tx_packets",
4621 "rx_packets",
4622 "tx_errors",
4623 "rx_errors",
4624 "rx_missed",
4625 "align_errors",
4626 "tx_single_collisions",
4627 "tx_multi_collisions",
4628 "rx_unicast",
4629 "rx_broadcast",
4630 "rx_multicast",
4631 "tx_aborted",
4632 "tx_underrun",
4633 };
4634
rtl8152_get_sset_count(struct net_device * dev,int sset)4635 static int rtl8152_get_sset_count(struct net_device *dev, int sset)
4636 {
4637 switch (sset) {
4638 case ETH_SS_STATS:
4639 return ARRAY_SIZE(rtl8152_gstrings);
4640 default:
4641 return -EOPNOTSUPP;
4642 }
4643 }
4644
rtl8152_get_ethtool_stats(struct net_device * dev,struct ethtool_stats * stats,u64 * data)4645 static void rtl8152_get_ethtool_stats(struct net_device *dev,
4646 struct ethtool_stats *stats, u64 *data)
4647 {
4648 struct r8152 *tp = netdev_priv(dev);
4649 struct tally_counter tally;
4650
4651 if (usb_autopm_get_interface(tp->intf) < 0)
4652 return;
4653
4654 generic_ocp_read(tp, PLA_TALLYCNT, sizeof(tally), &tally, MCU_TYPE_PLA);
4655
4656 usb_autopm_put_interface(tp->intf);
4657
4658 data[0] = le64_to_cpu(tally.tx_packets);
4659 data[1] = le64_to_cpu(tally.rx_packets);
4660 data[2] = le64_to_cpu(tally.tx_errors);
4661 data[3] = le32_to_cpu(tally.rx_errors);
4662 data[4] = le16_to_cpu(tally.rx_missed);
4663 data[5] = le16_to_cpu(tally.align_errors);
4664 data[6] = le32_to_cpu(tally.tx_one_collision);
4665 data[7] = le32_to_cpu(tally.tx_multi_collision);
4666 data[8] = le64_to_cpu(tally.rx_unicast);
4667 data[9] = le64_to_cpu(tally.rx_broadcast);
4668 data[10] = le32_to_cpu(tally.rx_multicast);
4669 data[11] = le16_to_cpu(tally.tx_aborted);
4670 data[12] = le16_to_cpu(tally.tx_underrun);
4671 }
4672
rtl8152_get_strings(struct net_device * dev,u32 stringset,u8 * data)4673 static void rtl8152_get_strings(struct net_device *dev, u32 stringset, u8 *data)
4674 {
4675 switch (stringset) {
4676 case ETH_SS_STATS:
4677 memcpy(data, *rtl8152_gstrings, sizeof(rtl8152_gstrings));
4678 break;
4679 }
4680 }
4681
r8152_get_eee(struct r8152 * tp,struct ethtool_eee * eee)4682 static int r8152_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
4683 {
4684 u32 ocp_data, lp, adv, supported = 0;
4685 u16 val;
4686
4687 val = r8152_mmd_read(tp, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
4688 supported = mmd_eee_cap_to_ethtool_sup_t(val);
4689
4690 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
4691 adv = mmd_eee_adv_to_ethtool_adv_t(val);
4692
4693 val = r8152_mmd_read(tp, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
4694 lp = mmd_eee_adv_to_ethtool_adv_t(val);
4695
4696 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
4697 ocp_data &= EEE_RX_EN | EEE_TX_EN;
4698
4699 eee->eee_enabled = !!ocp_data;
4700 eee->eee_active = !!(supported & adv & lp);
4701 eee->supported = supported;
4702 eee->advertised = adv;
4703 eee->lp_advertised = lp;
4704
4705 return 0;
4706 }
4707
r8152_set_eee(struct r8152 * tp,struct ethtool_eee * eee)4708 static int r8152_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
4709 {
4710 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
4711
4712 r8152_eee_en(tp, eee->eee_enabled);
4713
4714 if (!eee->eee_enabled)
4715 val = 0;
4716
4717 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
4718
4719 return 0;
4720 }
4721
r8153_get_eee(struct r8152 * tp,struct ethtool_eee * eee)4722 static int r8153_get_eee(struct r8152 *tp, struct ethtool_eee *eee)
4723 {
4724 u32 ocp_data, lp, adv, supported = 0;
4725 u16 val;
4726
4727 val = ocp_reg_read(tp, OCP_EEE_ABLE);
4728 supported = mmd_eee_cap_to_ethtool_sup_t(val);
4729
4730 val = ocp_reg_read(tp, OCP_EEE_ADV);
4731 adv = mmd_eee_adv_to_ethtool_adv_t(val);
4732
4733 val = ocp_reg_read(tp, OCP_EEE_LPABLE);
4734 lp = mmd_eee_adv_to_ethtool_adv_t(val);
4735
4736 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
4737 ocp_data &= EEE_RX_EN | EEE_TX_EN;
4738
4739 eee->eee_enabled = !!ocp_data;
4740 eee->eee_active = !!(supported & adv & lp);
4741 eee->supported = supported;
4742 eee->advertised = adv;
4743 eee->lp_advertised = lp;
4744
4745 return 0;
4746 }
4747
r8153_set_eee(struct r8152 * tp,struct ethtool_eee * eee)4748 static int r8153_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
4749 {
4750 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
4751
4752 r8153_eee_en(tp, eee->eee_enabled);
4753
4754 if (!eee->eee_enabled)
4755 val = 0;
4756
4757 ocp_reg_write(tp, OCP_EEE_ADV, val);
4758
4759 return 0;
4760 }
4761
r8153b_set_eee(struct r8152 * tp,struct ethtool_eee * eee)4762 static int r8153b_set_eee(struct r8152 *tp, struct ethtool_eee *eee)
4763 {
4764 u16 val = ethtool_adv_to_mmd_eee_adv_t(eee->advertised);
4765
4766 r8153b_eee_en(tp, eee->eee_enabled);
4767
4768 if (!eee->eee_enabled)
4769 val = 0;
4770
4771 ocp_reg_write(tp, OCP_EEE_ADV, val);
4772
4773 return 0;
4774 }
4775
4776 static int
rtl_ethtool_get_eee(struct net_device * net,struct ethtool_eee * edata)4777 rtl_ethtool_get_eee(struct net_device *net, struct ethtool_eee *edata)
4778 {
4779 struct r8152 *tp = netdev_priv(net);
4780 int ret;
4781
4782 ret = usb_autopm_get_interface(tp->intf);
4783 if (ret < 0)
4784 goto out;
4785
4786 mutex_lock(&tp->control);
4787
4788 ret = tp->rtl_ops.eee_get(tp, edata);
4789
4790 mutex_unlock(&tp->control);
4791
4792 usb_autopm_put_interface(tp->intf);
4793
4794 out:
4795 return ret;
4796 }
4797
4798 static int
rtl_ethtool_set_eee(struct net_device * net,struct ethtool_eee * edata)4799 rtl_ethtool_set_eee(struct net_device *net, struct ethtool_eee *edata)
4800 {
4801 struct r8152 *tp = netdev_priv(net);
4802 int ret;
4803
4804 ret = usb_autopm_get_interface(tp->intf);
4805 if (ret < 0)
4806 goto out;
4807
4808 mutex_lock(&tp->control);
4809
4810 ret = tp->rtl_ops.eee_set(tp, edata);
4811 if (!ret)
4812 ret = mii_nway_restart(&tp->mii);
4813
4814 mutex_unlock(&tp->control);
4815
4816 usb_autopm_put_interface(tp->intf);
4817
4818 out:
4819 return ret;
4820 }
4821
rtl8152_nway_reset(struct net_device * dev)4822 static int rtl8152_nway_reset(struct net_device *dev)
4823 {
4824 struct r8152 *tp = netdev_priv(dev);
4825 int ret;
4826
4827 ret = usb_autopm_get_interface(tp->intf);
4828 if (ret < 0)
4829 goto out;
4830
4831 mutex_lock(&tp->control);
4832
4833 ret = mii_nway_restart(&tp->mii);
4834
4835 mutex_unlock(&tp->control);
4836
4837 usb_autopm_put_interface(tp->intf);
4838
4839 out:
4840 return ret;
4841 }
4842
rtl8152_get_coalesce(struct net_device * netdev,struct ethtool_coalesce * coalesce)4843 static int rtl8152_get_coalesce(struct net_device *netdev,
4844 struct ethtool_coalesce *coalesce)
4845 {
4846 struct r8152 *tp = netdev_priv(netdev);
4847
4848 switch (tp->version) {
4849 case RTL_VER_01:
4850 case RTL_VER_02:
4851 case RTL_VER_07:
4852 return -EOPNOTSUPP;
4853 default:
4854 break;
4855 }
4856
4857 coalesce->rx_coalesce_usecs = tp->coalesce;
4858
4859 return 0;
4860 }
4861
rtl8152_set_coalesce(struct net_device * netdev,struct ethtool_coalesce * coalesce)4862 static int rtl8152_set_coalesce(struct net_device *netdev,
4863 struct ethtool_coalesce *coalesce)
4864 {
4865 struct r8152 *tp = netdev_priv(netdev);
4866 int ret;
4867
4868 switch (tp->version) {
4869 case RTL_VER_01:
4870 case RTL_VER_02:
4871 case RTL_VER_07:
4872 return -EOPNOTSUPP;
4873 default:
4874 break;
4875 }
4876
4877 if (coalesce->rx_coalesce_usecs > COALESCE_SLOW)
4878 return -EINVAL;
4879
4880 ret = usb_autopm_get_interface(tp->intf);
4881 if (ret < 0)
4882 return ret;
4883
4884 mutex_lock(&tp->control);
4885
4886 if (tp->coalesce != coalesce->rx_coalesce_usecs) {
4887 tp->coalesce = coalesce->rx_coalesce_usecs;
4888
4889 if (netif_running(tp->netdev) && netif_carrier_ok(netdev))
4890 r8153_set_rx_early_timeout(tp);
4891 }
4892
4893 mutex_unlock(&tp->control);
4894
4895 usb_autopm_put_interface(tp->intf);
4896
4897 return ret;
4898 }
4899
4900 static const struct ethtool_ops ops = {
4901 .get_drvinfo = rtl8152_get_drvinfo,
4902 .get_link = ethtool_op_get_link,
4903 .nway_reset = rtl8152_nway_reset,
4904 .get_msglevel = rtl8152_get_msglevel,
4905 .set_msglevel = rtl8152_set_msglevel,
4906 .get_wol = rtl8152_get_wol,
4907 .set_wol = rtl8152_set_wol,
4908 .get_strings = rtl8152_get_strings,
4909 .get_sset_count = rtl8152_get_sset_count,
4910 .get_ethtool_stats = rtl8152_get_ethtool_stats,
4911 .get_coalesce = rtl8152_get_coalesce,
4912 .set_coalesce = rtl8152_set_coalesce,
4913 .get_eee = rtl_ethtool_get_eee,
4914 .set_eee = rtl_ethtool_set_eee,
4915 .get_link_ksettings = rtl8152_get_link_ksettings,
4916 .set_link_ksettings = rtl8152_set_link_ksettings,
4917 };
4918
rtl8152_ioctl(struct net_device * netdev,struct ifreq * rq,int cmd)4919 static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
4920 {
4921 struct r8152 *tp = netdev_priv(netdev);
4922 struct mii_ioctl_data *data = if_mii(rq);
4923 int res;
4924
4925 if (test_bit(RTL8152_UNPLUG, &tp->flags))
4926 return -ENODEV;
4927
4928 res = usb_autopm_get_interface(tp->intf);
4929 if (res < 0)
4930 goto out;
4931
4932 switch (cmd) {
4933 case SIOCGMIIPHY:
4934 data->phy_id = R8152_PHY_ID; /* Internal PHY */
4935 break;
4936
4937 case SIOCGMIIREG:
4938 mutex_lock(&tp->control);
4939 data->val_out = r8152_mdio_read(tp, data->reg_num);
4940 mutex_unlock(&tp->control);
4941 break;
4942
4943 case SIOCSMIIREG:
4944 if (!capable(CAP_NET_ADMIN)) {
4945 res = -EPERM;
4946 break;
4947 }
4948 mutex_lock(&tp->control);
4949 r8152_mdio_write(tp, data->reg_num, data->val_in);
4950 mutex_unlock(&tp->control);
4951 break;
4952
4953 default:
4954 res = -EOPNOTSUPP;
4955 }
4956
4957 usb_autopm_put_interface(tp->intf);
4958
4959 out:
4960 return res;
4961 }
4962
rtl8152_change_mtu(struct net_device * dev,int new_mtu)4963 static int rtl8152_change_mtu(struct net_device *dev, int new_mtu)
4964 {
4965 struct r8152 *tp = netdev_priv(dev);
4966 int ret;
4967
4968 switch (tp->version) {
4969 case RTL_VER_01:
4970 case RTL_VER_02:
4971 case RTL_VER_07:
4972 dev->mtu = new_mtu;
4973 return 0;
4974 default:
4975 break;
4976 }
4977
4978 ret = usb_autopm_get_interface(tp->intf);
4979 if (ret < 0)
4980 return ret;
4981
4982 mutex_lock(&tp->control);
4983
4984 dev->mtu = new_mtu;
4985
4986 if (netif_running(dev)) {
4987 u32 rms = new_mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
4988
4989 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, rms);
4990
4991 if (netif_carrier_ok(dev))
4992 r8153_set_rx_early_size(tp);
4993 }
4994
4995 mutex_unlock(&tp->control);
4996
4997 usb_autopm_put_interface(tp->intf);
4998
4999 return ret;
5000 }
5001
5002 static const struct net_device_ops rtl8152_netdev_ops = {
5003 .ndo_open = rtl8152_open,
5004 .ndo_stop = rtl8152_close,
5005 .ndo_do_ioctl = rtl8152_ioctl,
5006 .ndo_start_xmit = rtl8152_start_xmit,
5007 .ndo_tx_timeout = rtl8152_tx_timeout,
5008 .ndo_set_features = rtl8152_set_features,
5009 .ndo_set_rx_mode = rtl8152_set_rx_mode,
5010 .ndo_set_mac_address = rtl8152_set_mac_address,
5011 .ndo_change_mtu = rtl8152_change_mtu,
5012 .ndo_validate_addr = eth_validate_addr,
5013 .ndo_features_check = rtl8152_features_check,
5014 };
5015
rtl8152_unload(struct r8152 * tp)5016 static void rtl8152_unload(struct r8152 *tp)
5017 {
5018 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5019 return;
5020
5021 if (tp->version != RTL_VER_01)
5022 r8152_power_cut_en(tp, true);
5023 }
5024
rtl8153_unload(struct r8152 * tp)5025 static void rtl8153_unload(struct r8152 *tp)
5026 {
5027 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5028 return;
5029
5030 r8153_power_cut_en(tp, false);
5031 }
5032
rtl8153b_unload(struct r8152 * tp)5033 static void rtl8153b_unload(struct r8152 *tp)
5034 {
5035 if (test_bit(RTL8152_UNPLUG, &tp->flags))
5036 return;
5037
5038 r8153b_power_cut_en(tp, false);
5039 }
5040
rtl_ops_init(struct r8152 * tp)5041 static int rtl_ops_init(struct r8152 *tp)
5042 {
5043 struct rtl_ops *ops = &tp->rtl_ops;
5044 int ret = 0;
5045
5046 switch (tp->version) {
5047 case RTL_VER_01:
5048 case RTL_VER_02:
5049 case RTL_VER_07:
5050 ops->init = r8152b_init;
5051 ops->enable = rtl8152_enable;
5052 ops->disable = rtl8152_disable;
5053 ops->up = rtl8152_up;
5054 ops->down = rtl8152_down;
5055 ops->unload = rtl8152_unload;
5056 ops->eee_get = r8152_get_eee;
5057 ops->eee_set = r8152_set_eee;
5058 ops->in_nway = rtl8152_in_nway;
5059 ops->hw_phy_cfg = r8152b_hw_phy_cfg;
5060 ops->autosuspend_en = rtl_runtime_suspend_enable;
5061 break;
5062
5063 case RTL_VER_03:
5064 case RTL_VER_04:
5065 case RTL_VER_05:
5066 case RTL_VER_06:
5067 ops->init = r8153_init;
5068 ops->enable = rtl8153_enable;
5069 ops->disable = rtl8153_disable;
5070 ops->up = rtl8153_up;
5071 ops->down = rtl8153_down;
5072 ops->unload = rtl8153_unload;
5073 ops->eee_get = r8153_get_eee;
5074 ops->eee_set = r8153_set_eee;
5075 ops->in_nway = rtl8153_in_nway;
5076 ops->hw_phy_cfg = r8153_hw_phy_cfg;
5077 ops->autosuspend_en = rtl8153_runtime_enable;
5078 break;
5079
5080 case RTL_VER_08:
5081 case RTL_VER_09:
5082 ops->init = r8153b_init;
5083 ops->enable = rtl8153_enable;
5084 ops->disable = rtl8153b_disable;
5085 ops->up = rtl8153b_up;
5086 ops->down = rtl8153b_down;
5087 ops->unload = rtl8153b_unload;
5088 ops->eee_get = r8153_get_eee;
5089 ops->eee_set = r8153b_set_eee;
5090 ops->in_nway = rtl8153_in_nway;
5091 ops->hw_phy_cfg = r8153b_hw_phy_cfg;
5092 ops->autosuspend_en = rtl8153b_runtime_enable;
5093 break;
5094
5095 default:
5096 ret = -ENODEV;
5097 netif_err(tp, probe, tp->netdev, "Unknown Device\n");
5098 break;
5099 }
5100
5101 return ret;
5102 }
5103
rtl_get_version(struct usb_interface * intf)5104 static u8 rtl_get_version(struct usb_interface *intf)
5105 {
5106 struct usb_device *udev = interface_to_usbdev(intf);
5107 u32 ocp_data = 0;
5108 __le32 *tmp;
5109 u8 version;
5110 int ret;
5111
5112 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
5113 if (!tmp)
5114 return 0;
5115
5116 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
5117 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
5118 PLA_TCR0, MCU_TYPE_PLA, tmp, sizeof(*tmp), 500);
5119 if (ret > 0)
5120 ocp_data = (__le32_to_cpu(*tmp) >> 16) & VERSION_MASK;
5121
5122 kfree(tmp);
5123
5124 switch (ocp_data) {
5125 case 0x4c00:
5126 version = RTL_VER_01;
5127 break;
5128 case 0x4c10:
5129 version = RTL_VER_02;
5130 break;
5131 case 0x5c00:
5132 version = RTL_VER_03;
5133 break;
5134 case 0x5c10:
5135 version = RTL_VER_04;
5136 break;
5137 case 0x5c20:
5138 version = RTL_VER_05;
5139 break;
5140 case 0x5c30:
5141 version = RTL_VER_06;
5142 break;
5143 case 0x4800:
5144 version = RTL_VER_07;
5145 break;
5146 case 0x6000:
5147 version = RTL_VER_08;
5148 break;
5149 case 0x6010:
5150 version = RTL_VER_09;
5151 break;
5152 default:
5153 version = RTL_VER_UNKNOWN;
5154 dev_info(&intf->dev, "Unknown version 0x%04x\n", ocp_data);
5155 break;
5156 }
5157
5158 dev_dbg(&intf->dev, "Detected version 0x%04x\n", version);
5159
5160 return version;
5161 }
5162
rtl8152_probe(struct usb_interface * intf,const struct usb_device_id * id)5163 static int rtl8152_probe(struct usb_interface *intf,
5164 const struct usb_device_id *id)
5165 {
5166 struct usb_device *udev = interface_to_usbdev(intf);
5167 u8 version = rtl_get_version(intf);
5168 struct r8152 *tp;
5169 struct net_device *netdev;
5170 int ret;
5171
5172 if (version == RTL_VER_UNKNOWN)
5173 return -ENODEV;
5174
5175 if (udev->actconfig->desc.bConfigurationValue != 1) {
5176 usb_driver_set_configuration(udev, 1);
5177 return -ENODEV;
5178 }
5179
5180 if (intf->cur_altsetting->desc.bNumEndpoints < 3)
5181 return -ENODEV;
5182
5183 usb_reset_device(udev);
5184 netdev = alloc_etherdev(sizeof(struct r8152));
5185 if (!netdev) {
5186 dev_err(&intf->dev, "Out of memory\n");
5187 return -ENOMEM;
5188 }
5189
5190 SET_NETDEV_DEV(netdev, &intf->dev);
5191 tp = netdev_priv(netdev);
5192 tp->msg_enable = 0x7FFF;
5193
5194 tp->udev = udev;
5195 tp->netdev = netdev;
5196 tp->intf = intf;
5197 tp->version = version;
5198
5199 switch (version) {
5200 case RTL_VER_01:
5201 case RTL_VER_02:
5202 case RTL_VER_07:
5203 tp->mii.supports_gmii = 0;
5204 break;
5205 default:
5206 tp->mii.supports_gmii = 1;
5207 break;
5208 }
5209
5210 ret = rtl_ops_init(tp);
5211 if (ret)
5212 goto out;
5213
5214 mutex_init(&tp->control);
5215 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
5216 INIT_DELAYED_WORK(&tp->hw_phy_work, rtl_hw_phy_work_func_t);
5217
5218 netdev->netdev_ops = &rtl8152_netdev_ops;
5219 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
5220
5221 netdev->features |= NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
5222 NETIF_F_TSO | NETIF_F_FRAGLIST | NETIF_F_IPV6_CSUM |
5223 NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_RX |
5224 NETIF_F_HW_VLAN_CTAG_TX;
5225 netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
5226 NETIF_F_TSO | NETIF_F_FRAGLIST |
5227 NETIF_F_IPV6_CSUM | NETIF_F_TSO6 |
5228 NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX;
5229 netdev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
5230 NETIF_F_HIGHDMA | NETIF_F_FRAGLIST |
5231 NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
5232
5233 if (tp->version == RTL_VER_01) {
5234 netdev->features &= ~NETIF_F_RXCSUM;
5235 netdev->hw_features &= ~NETIF_F_RXCSUM;
5236 }
5237
5238 if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x3011 && udev->serial &&
5239 (!strcmp(udev->serial, "000001000000") || !strcmp(udev->serial, "000002000000"))) {
5240 dev_info(&udev->dev, "Dell TB16 Dock, disable RX aggregation");
5241 set_bit(DELL_TB_RX_AGG_BUG, &tp->flags);
5242 }
5243
5244 netdev->ethtool_ops = &ops;
5245 netif_set_gso_max_size(netdev, RTL_LIMITED_TSO_SIZE);
5246
5247 /* MTU range: 68 - 1500 or 9194 */
5248 netdev->min_mtu = ETH_MIN_MTU;
5249 switch (tp->version) {
5250 case RTL_VER_01:
5251 case RTL_VER_02:
5252 netdev->max_mtu = ETH_DATA_LEN;
5253 break;
5254 default:
5255 netdev->max_mtu = RTL8153_MAX_MTU;
5256 break;
5257 }
5258
5259 tp->mii.dev = netdev;
5260 tp->mii.mdio_read = read_mii_word;
5261 tp->mii.mdio_write = write_mii_word;
5262 tp->mii.phy_id_mask = 0x3f;
5263 tp->mii.reg_num_mask = 0x1f;
5264 tp->mii.phy_id = R8152_PHY_ID;
5265
5266 tp->autoneg = AUTONEG_ENABLE;
5267 tp->speed = tp->mii.supports_gmii ? SPEED_1000 : SPEED_100;
5268 tp->duplex = DUPLEX_FULL;
5269
5270 intf->needs_remote_wakeup = 1;
5271
5272 if (!rtl_can_wakeup(tp))
5273 __rtl_set_wol(tp, 0);
5274 else
5275 tp->saved_wolopts = __rtl_get_wol(tp);
5276
5277 tp->rtl_ops.init(tp);
5278 queue_delayed_work(system_long_wq, &tp->hw_phy_work, 0);
5279 set_ethernet_addr(tp);
5280
5281 usb_set_intfdata(intf, tp);
5282 netif_napi_add(netdev, &tp->napi, r8152_poll, RTL8152_NAPI_WEIGHT);
5283
5284 ret = register_netdev(netdev);
5285 if (ret != 0) {
5286 netif_err(tp, probe, netdev, "couldn't register the device\n");
5287 goto out1;
5288 }
5289
5290 if (tp->saved_wolopts)
5291 device_set_wakeup_enable(&udev->dev, true);
5292 else
5293 device_set_wakeup_enable(&udev->dev, false);
5294
5295 netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION);
5296
5297 return 0;
5298
5299 out1:
5300 netif_napi_del(&tp->napi);
5301 usb_set_intfdata(intf, NULL);
5302 out:
5303 free_netdev(netdev);
5304 return ret;
5305 }
5306
rtl8152_disconnect(struct usb_interface * intf)5307 static void rtl8152_disconnect(struct usb_interface *intf)
5308 {
5309 struct r8152 *tp = usb_get_intfdata(intf);
5310
5311 usb_set_intfdata(intf, NULL);
5312 if (tp) {
5313 struct usb_device *udev = tp->udev;
5314
5315 if (udev->state == USB_STATE_NOTATTACHED)
5316 set_bit(RTL8152_UNPLUG, &tp->flags);
5317
5318 netif_napi_del(&tp->napi);
5319 unregister_netdev(tp->netdev);
5320 cancel_delayed_work_sync(&tp->hw_phy_work);
5321 tp->rtl_ops.unload(tp);
5322 free_netdev(tp->netdev);
5323 }
5324 }
5325
5326 #define REALTEK_USB_DEVICE(vend, prod) \
5327 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \
5328 USB_DEVICE_ID_MATCH_INT_CLASS, \
5329 .idVendor = (vend), \
5330 .idProduct = (prod), \
5331 .bInterfaceClass = USB_CLASS_VENDOR_SPEC \
5332 }, \
5333 { \
5334 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO | \
5335 USB_DEVICE_ID_MATCH_DEVICE, \
5336 .idVendor = (vend), \
5337 .idProduct = (prod), \
5338 .bInterfaceClass = USB_CLASS_COMM, \
5339 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \
5340 .bInterfaceProtocol = USB_CDC_PROTO_NONE
5341
5342 /* table of devices that work with this driver */
5343 static const struct usb_device_id rtl8152_table[] = {
5344 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8050)},
5345 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8152)},
5346 {REALTEK_USB_DEVICE(VENDOR_ID_REALTEK, 0x8153)},
5347 {REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07ab)},
5348 {REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x07c6)},
5349 {REALTEK_USB_DEVICE(VENDOR_ID_MICROSOFT, 0x0927)},
5350 {REALTEK_USB_DEVICE(VENDOR_ID_SAMSUNG, 0xa101)},
5351 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x304f)},
5352 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3062)},
5353 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x3069)},
5354 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7205)},
5355 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x720c)},
5356 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0x7214)},
5357 {REALTEK_USB_DEVICE(VENDOR_ID_LENOVO, 0xa387)},
5358 {REALTEK_USB_DEVICE(VENDOR_ID_LINKSYS, 0x0041)},
5359 {REALTEK_USB_DEVICE(VENDOR_ID_NVIDIA, 0x09ff)},
5360 {REALTEK_USB_DEVICE(VENDOR_ID_TPLINK, 0x0601)},
5361 {}
5362 };
5363
5364 MODULE_DEVICE_TABLE(usb, rtl8152_table);
5365
5366 static struct usb_driver rtl8152_driver = {
5367 .name = MODULENAME,
5368 .id_table = rtl8152_table,
5369 .probe = rtl8152_probe,
5370 .disconnect = rtl8152_disconnect,
5371 .suspend = rtl8152_suspend,
5372 .resume = rtl8152_resume,
5373 .reset_resume = rtl8152_reset_resume,
5374 .pre_reset = rtl8152_pre_reset,
5375 .post_reset = rtl8152_post_reset,
5376 .supports_autosuspend = 1,
5377 .disable_hub_initiated_lpm = 1,
5378 };
5379
5380 module_usb_driver(rtl8152_driver);
5381
5382 MODULE_AUTHOR(DRIVER_AUTHOR);
5383 MODULE_DESCRIPTION(DRIVER_DESC);
5384 MODULE_LICENSE("GPL");
5385 MODULE_VERSION(DRIVER_VERSION);
5386