]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/net/usb/r8152.c
r8152: add functions to set EEE
[mirror_ubuntu-zesty-kernel.git] / drivers / net / usb / r8152.c
CommitLineData
ac718b69 1/*
c7de7dec 2 * Copyright (c) 2014 Realtek Semiconductor Corp. All rights reserved.
ac718b69 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
ac718b69 10#include <linux/signal.h>
11#include <linux/slab.h>
12#include <linux/module.h>
ac718b69 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>
ebc2ec48 21#include <linux/list.h>
5bd23881 22#include <linux/ip.h>
23#include <linux/ipv6.h>
6128d1bb 24#include <net/ip6_checksum.h>
4c4a6b1b 25#include <uapi/linux/mdio.h>
26#include <linux/mdio.h>
ac718b69 27
28/* Version Information */
60c89071 29#define DRIVER_VERSION "v1.06.0 (2014/03/03)"
ac718b69 30#define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>"
44d942a9 31#define DRIVER_DESC "Realtek RTL8152/RTL8153 Based USB Ethernet Adapters"
ac718b69 32#define MODULENAME "r8152"
33
34#define R8152_PHY_ID 32
35
36#define PLA_IDR 0xc000
37#define PLA_RCR 0xc010
38#define PLA_RMS 0xc016
39#define PLA_RXFIFO_CTRL0 0xc0a0
40#define PLA_RXFIFO_CTRL1 0xc0a4
41#define PLA_RXFIFO_CTRL2 0xc0a8
42#define PLA_FMC 0xc0b4
43#define PLA_CFG_WOL 0xc0b6
43779f8d 44#define PLA_TEREDO_CFG 0xc0bc
ac718b69 45#define PLA_MAR 0xcd00
43779f8d 46#define PLA_BACKUP 0xd000
ac718b69 47#define PAL_BDC_CR 0xd1a0
43779f8d 48#define PLA_TEREDO_TIMER 0xd2cc
49#define PLA_REALWOW_TIMER 0xd2e8
ac718b69 50#define PLA_LEDSEL 0xdd90
51#define PLA_LED_FEATURE 0xdd92
52#define PLA_PHYAR 0xde00
43779f8d 53#define PLA_BOOT_CTRL 0xe004
ac718b69 54#define PLA_GPHY_INTR_IMR 0xe022
55#define PLA_EEE_CR 0xe040
56#define PLA_EEEP_CR 0xe080
57#define PLA_MAC_PWR_CTRL 0xe0c0
43779f8d 58#define PLA_MAC_PWR_CTRL2 0xe0ca
59#define PLA_MAC_PWR_CTRL3 0xe0cc
60#define PLA_MAC_PWR_CTRL4 0xe0ce
61#define PLA_WDT6_CTRL 0xe428
ac718b69 62#define PLA_TCR0 0xe610
63#define PLA_TCR1 0xe612
69b4b7a4 64#define PLA_MTPS 0xe615
ac718b69 65#define PLA_TXFIFO_CTRL 0xe618
4f1d4d54 66#define PLA_RSTTALLY 0xe800
ac718b69 67#define PLA_CR 0xe813
68#define PLA_CRWECR 0xe81c
21ff2e89 69#define PLA_CONFIG12 0xe81e /* CONFIG1, CONFIG2 */
70#define PLA_CONFIG34 0xe820 /* CONFIG3, CONFIG4 */
ac718b69 71#define PLA_CONFIG5 0xe822
72#define PLA_PHY_PWR 0xe84c
73#define PLA_OOB_CTRL 0xe84f
74#define PLA_CPCR 0xe854
75#define PLA_MISC_0 0xe858
76#define PLA_MISC_1 0xe85a
77#define PLA_OCP_GPHY_BASE 0xe86c
4f1d4d54 78#define PLA_TALLYCNT 0xe890
ac718b69 79#define PLA_SFF_STS_7 0xe8de
80#define PLA_PHYSTATUS 0xe908
81#define PLA_BP_BA 0xfc26
82#define PLA_BP_0 0xfc28
83#define PLA_BP_1 0xfc2a
84#define PLA_BP_2 0xfc2c
85#define PLA_BP_3 0xfc2e
86#define PLA_BP_4 0xfc30
87#define PLA_BP_5 0xfc32
88#define PLA_BP_6 0xfc34
89#define PLA_BP_7 0xfc36
43779f8d 90#define PLA_BP_EN 0xfc38
ac718b69 91
43779f8d 92#define USB_U2P3_CTRL 0xb460
ac718b69 93#define USB_DEV_STAT 0xb808
94#define USB_USB_CTRL 0xd406
95#define USB_PHY_CTRL 0xd408
96#define USB_TX_AGG 0xd40a
97#define USB_RX_BUF_TH 0xd40c
98#define USB_USB_TIMER 0xd428
43779f8d 99#define USB_RX_EARLY_AGG 0xd42c
ac718b69 100#define USB_PM_CTRL_STATUS 0xd432
101#define USB_TX_DMA 0xd434
43779f8d 102#define USB_TOLERANCE 0xd490
103#define USB_LPM_CTRL 0xd41a
ac718b69 104#define USB_UPS_CTRL 0xd800
43779f8d 105#define USB_MISC_0 0xd81a
106#define USB_POWER_CUT 0xd80a
107#define USB_AFE_CTRL2 0xd824
108#define USB_WDT11_CTRL 0xe43c
ac718b69 109#define USB_BP_BA 0xfc26
110#define USB_BP_0 0xfc28
111#define USB_BP_1 0xfc2a
112#define USB_BP_2 0xfc2c
113#define USB_BP_3 0xfc2e
114#define USB_BP_4 0xfc30
115#define USB_BP_5 0xfc32
116#define USB_BP_6 0xfc34
117#define USB_BP_7 0xfc36
43779f8d 118#define USB_BP_EN 0xfc38
ac718b69 119
120/* OCP Registers */
121#define OCP_ALDPS_CONFIG 0x2010
122#define OCP_EEE_CONFIG1 0x2080
123#define OCP_EEE_CONFIG2 0x2092
124#define OCP_EEE_CONFIG3 0x2094
ac244d3e 125#define OCP_BASE_MII 0xa400
ac718b69 126#define OCP_EEE_AR 0xa41a
127#define OCP_EEE_DATA 0xa41c
43779f8d 128#define OCP_PHY_STATUS 0xa420
129#define OCP_POWER_CFG 0xa430
130#define OCP_EEE_CFG 0xa432
131#define OCP_SRAM_ADDR 0xa436
132#define OCP_SRAM_DATA 0xa438
133#define OCP_DOWN_SPEED 0xa442
4c4a6b1b 134#define OCP_EEE_ADV 0xa5d0
43779f8d 135#define OCP_ADC_CFG 0xbc06
136
137/* SRAM Register */
138#define SRAM_LPF_CFG 0x8012
139#define SRAM_10M_AMP1 0x8080
140#define SRAM_10M_AMP2 0x8082
141#define SRAM_IMPEDANCE 0x8084
ac718b69 142
143/* PLA_RCR */
144#define RCR_AAP 0x00000001
145#define RCR_APM 0x00000002
146#define RCR_AM 0x00000004
147#define RCR_AB 0x00000008
148#define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB)
149
150/* PLA_RXFIFO_CTRL0 */
151#define RXFIFO_THR1_NORMAL 0x00080002
152#define RXFIFO_THR1_OOB 0x01800003
153
154/* PLA_RXFIFO_CTRL1 */
155#define RXFIFO_THR2_FULL 0x00000060
156#define RXFIFO_THR2_HIGH 0x00000038
157#define RXFIFO_THR2_OOB 0x0000004a
43779f8d 158#define RXFIFO_THR2_NORMAL 0x00a0
ac718b69 159
160/* PLA_RXFIFO_CTRL2 */
161#define RXFIFO_THR3_FULL 0x00000078
162#define RXFIFO_THR3_HIGH 0x00000048
163#define RXFIFO_THR3_OOB 0x0000005a
43779f8d 164#define RXFIFO_THR3_NORMAL 0x0110
ac718b69 165
166/* PLA_TXFIFO_CTRL */
167#define TXFIFO_THR_NORMAL 0x00400008
43779f8d 168#define TXFIFO_THR_NORMAL2 0x01000008
ac718b69 169
170/* PLA_FMC */
171#define FMC_FCR_MCU_EN 0x0001
172
173/* PLA_EEEP_CR */
174#define EEEP_CR_EEEP_TX 0x0002
175
43779f8d 176/* PLA_WDT6_CTRL */
177#define WDT6_SET_MODE 0x0010
178
ac718b69 179/* PLA_TCR0 */
180#define TCR0_TX_EMPTY 0x0800
181#define TCR0_AUTO_FIFO 0x0080
182
183/* PLA_TCR1 */
184#define VERSION_MASK 0x7cf0
185
69b4b7a4 186/* PLA_MTPS */
187#define MTPS_JUMBO (12 * 1024 / 64)
188#define MTPS_DEFAULT (6 * 1024 / 64)
189
4f1d4d54 190/* PLA_RSTTALLY */
191#define TALLY_RESET 0x0001
192
ac718b69 193/* PLA_CR */
194#define CR_RST 0x10
195#define CR_RE 0x08
196#define CR_TE 0x04
197
198/* PLA_CRWECR */
199#define CRWECR_NORAML 0x00
200#define CRWECR_CONFIG 0xc0
201
202/* PLA_OOB_CTRL */
203#define NOW_IS_OOB 0x80
204#define TXFIFO_EMPTY 0x20
205#define RXFIFO_EMPTY 0x10
206#define LINK_LIST_READY 0x02
207#define DIS_MCU_CLROOB 0x01
208#define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY)
209
210/* PLA_MISC_1 */
211#define RXDY_GATED_EN 0x0008
212
213/* PLA_SFF_STS_7 */
214#define RE_INIT_LL 0x8000
215#define MCU_BORW_EN 0x4000
216
217/* PLA_CPCR */
218#define CPCR_RX_VLAN 0x0040
219
220/* PLA_CFG_WOL */
221#define MAGIC_EN 0x0001
222
43779f8d 223/* PLA_TEREDO_CFG */
224#define TEREDO_SEL 0x8000
225#define TEREDO_WAKE_MASK 0x7f00
226#define TEREDO_RS_EVENT_MASK 0x00fe
227#define OOB_TEREDO_EN 0x0001
228
ac718b69 229/* PAL_BDC_CR */
230#define ALDPS_PROXY_MODE 0x0001
231
21ff2e89 232/* PLA_CONFIG34 */
233#define LINK_ON_WAKE_EN 0x0010
234#define LINK_OFF_WAKE_EN 0x0008
235
ac718b69 236/* PLA_CONFIG5 */
21ff2e89 237#define BWF_EN 0x0040
238#define MWF_EN 0x0020
239#define UWF_EN 0x0010
ac718b69 240#define LAN_WAKE_EN 0x0002
241
242/* PLA_LED_FEATURE */
243#define LED_MODE_MASK 0x0700
244
245/* PLA_PHY_PWR */
246#define TX_10M_IDLE_EN 0x0080
247#define PFM_PWM_SWITCH 0x0040
248
249/* PLA_MAC_PWR_CTRL */
250#define D3_CLK_GATED_EN 0x00004000
251#define MCU_CLK_RATIO 0x07010f07
252#define MCU_CLK_RATIO_MASK 0x0f0f0f0f
43779f8d 253#define ALDPS_SPDWN_RATIO 0x0f87
254
255/* PLA_MAC_PWR_CTRL2 */
256#define EEE_SPDWN_RATIO 0x8007
257
258/* PLA_MAC_PWR_CTRL3 */
259#define PKT_AVAIL_SPDWN_EN 0x0100
260#define SUSPEND_SPDWN_EN 0x0004
261#define U1U2_SPDWN_EN 0x0002
262#define L1_SPDWN_EN 0x0001
263
264/* PLA_MAC_PWR_CTRL4 */
265#define PWRSAVE_SPDWN_EN 0x1000
266#define RXDV_SPDWN_EN 0x0800
267#define TX10MIDLE_EN 0x0100
268#define TP100_SPDWN_EN 0x0020
269#define TP500_SPDWN_EN 0x0010
270#define TP1000_SPDWN_EN 0x0008
271#define EEE_SPDWN_EN 0x0001
ac718b69 272
273/* PLA_GPHY_INTR_IMR */
274#define GPHY_STS_MSK 0x0001
275#define SPEED_DOWN_MSK 0x0002
276#define SPDWN_RXDV_MSK 0x0004
277#define SPDWN_LINKCHG_MSK 0x0008
278
279/* PLA_PHYAR */
280#define PHYAR_FLAG 0x80000000
281
282/* PLA_EEE_CR */
283#define EEE_RX_EN 0x0001
284#define EEE_TX_EN 0x0002
285
43779f8d 286/* PLA_BOOT_CTRL */
287#define AUTOLOAD_DONE 0x0002
288
ac718b69 289/* USB_DEV_STAT */
290#define STAT_SPEED_MASK 0x0006
291#define STAT_SPEED_HIGH 0x0000
a3cc465d 292#define STAT_SPEED_FULL 0x0002
ac718b69 293
294/* USB_TX_AGG */
295#define TX_AGG_MAX_THRESHOLD 0x03
296
297/* USB_RX_BUF_TH */
43779f8d 298#define RX_THR_SUPPER 0x0c350180
8e1f51bd 299#define RX_THR_HIGH 0x7a120180
43779f8d 300#define RX_THR_SLOW 0xffff0180
ac718b69 301
302/* USB_TX_DMA */
303#define TEST_MODE_DISABLE 0x00000001
304#define TX_SIZE_ADJUST1 0x00000100
305
306/* USB_UPS_CTRL */
307#define POWER_CUT 0x0100
308
309/* USB_PM_CTRL_STATUS */
8e1f51bd 310#define RESUME_INDICATE 0x0001
ac718b69 311
312/* USB_USB_CTRL */
313#define RX_AGG_DISABLE 0x0010
314
43779f8d 315/* USB_U2P3_CTRL */
316#define U2P3_ENABLE 0x0001
317
318/* USB_POWER_CUT */
319#define PWR_EN 0x0001
320#define PHASE2_EN 0x0008
321
322/* USB_MISC_0 */
323#define PCUT_STATUS 0x0001
324
325/* USB_RX_EARLY_AGG */
326#define EARLY_AGG_SUPPER 0x0e832981
327#define EARLY_AGG_HIGH 0x0e837a12
328#define EARLY_AGG_SLOW 0x0e83ffff
329
330/* USB_WDT11_CTRL */
331#define TIMER11_EN 0x0001
332
333/* USB_LPM_CTRL */
334#define LPM_TIMER_MASK 0x0c
335#define LPM_TIMER_500MS 0x04 /* 500 ms */
336#define LPM_TIMER_500US 0x0c /* 500 us */
337
338/* USB_AFE_CTRL2 */
339#define SEN_VAL_MASK 0xf800
340#define SEN_VAL_NORMAL 0xa000
341#define SEL_RXIDLE 0x0100
342
ac718b69 343/* OCP_ALDPS_CONFIG */
344#define ENPWRSAVE 0x8000
345#define ENPDNPS 0x0200
346#define LINKENA 0x0100
347#define DIS_SDSAVE 0x0010
348
43779f8d 349/* OCP_PHY_STATUS */
350#define PHY_STAT_MASK 0x0007
351#define PHY_STAT_LAN_ON 3
352#define PHY_STAT_PWRDN 5
353
354/* OCP_POWER_CFG */
355#define EEE_CLKDIV_EN 0x8000
356#define EN_ALDPS 0x0004
357#define EN_10M_PLLOFF 0x0001
358
ac718b69 359/* OCP_EEE_CONFIG1 */
360#define RG_TXLPI_MSK_HFDUP 0x8000
361#define RG_MATCLR_EN 0x4000
362#define EEE_10_CAP 0x2000
363#define EEE_NWAY_EN 0x1000
364#define TX_QUIET_EN 0x0200
365#define RX_QUIET_EN 0x0100
d24f6134 366#define sd_rise_time_mask 0x0070
4c4a6b1b 367#define sd_rise_time(x) (min(x, 7) << 4) /* bit 4 ~ 6 */
ac718b69 368#define RG_RXLPI_MSK_HFDUP 0x0008
369#define SDFALLTIME 0x0007 /* bit 0 ~ 2 */
370
371/* OCP_EEE_CONFIG2 */
372#define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */
373#define RG_DACQUIET_EN 0x0400
374#define RG_LDVQUIET_EN 0x0200
375#define RG_CKRSEL 0x0020
376#define RG_EEEPRG_EN 0x0010
377
378/* OCP_EEE_CONFIG3 */
d24f6134 379#define fast_snr_mask 0xff80
4c4a6b1b 380#define fast_snr(x) (min(x, 0x1ff) << 7) /* bit 7 ~ 15 */
ac718b69 381#define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */
382#define MSK_PH 0x0006 /* bit 0 ~ 3 */
383
384/* OCP_EEE_AR */
385/* bit[15:14] function */
386#define FUN_ADDR 0x0000
387#define FUN_DATA 0x4000
388/* bit[4:0] device addr */
ac718b69 389
43779f8d 390/* OCP_EEE_CFG */
391#define CTAP_SHORT_EN 0x0040
392#define EEE10_EN 0x0010
393
394/* OCP_DOWN_SPEED */
395#define EN_10M_BGOFF 0x0080
396
43779f8d 397/* OCP_ADC_CFG */
398#define CKADSEL_L 0x0100
399#define ADC_EN 0x0080
400#define EN_EMI_L 0x0040
401
402/* SRAM_LPF_CFG */
403#define LPF_AUTO_TUNE 0x8000
404
405/* SRAM_10M_AMP1 */
406#define GDAC_IB_UPALL 0x0008
407
408/* SRAM_10M_AMP2 */
409#define AMP_DN 0x0200
410
411/* SRAM_IMPEDANCE */
412#define RX_DRIVING_MASK 0x6000
413
ac718b69 414enum rtl_register_content {
43779f8d 415 _1000bps = 0x10,
ac718b69 416 _100bps = 0x08,
417 _10bps = 0x04,
418 LINK_STATUS = 0x02,
419 FULL_DUP = 0x01,
420};
421
1764bcd9 422#define RTL8152_MAX_TX 4
ebc2ec48 423#define RTL8152_MAX_RX 10
40a82917 424#define INTBUFSIZE 2
8e1f51bd 425#define CRC_SIZE 4
426#define TX_ALIGN 4
427#define RX_ALIGN 8
40a82917 428
429#define INTR_LINK 0x0004
ebc2ec48 430
ac718b69 431#define RTL8152_REQT_READ 0xc0
432#define RTL8152_REQT_WRITE 0x40
433#define RTL8152_REQ_GET_REGS 0x05
434#define RTL8152_REQ_SET_REGS 0x05
435
436#define BYTE_EN_DWORD 0xff
437#define BYTE_EN_WORD 0x33
438#define BYTE_EN_BYTE 0x11
439#define BYTE_EN_SIX_BYTES 0x3f
440#define BYTE_EN_START_MASK 0x0f
441#define BYTE_EN_END_MASK 0xf0
442
69b4b7a4 443#define RTL8153_MAX_PACKET 9216 /* 9K */
444#define RTL8153_MAX_MTU (RTL8153_MAX_PACKET - VLAN_ETH_HLEN - VLAN_HLEN)
ac718b69 445#define RTL8152_RMS (VLAN_ETH_FRAME_LEN + VLAN_HLEN)
69b4b7a4 446#define RTL8153_RMS RTL8153_MAX_PACKET
b8125404 447#define RTL8152_TX_TIMEOUT (5 * HZ)
ac718b69 448
449/* rtl8152 flags */
450enum rtl8152_flags {
451 RTL8152_UNPLUG = 0,
ac718b69 452 RTL8152_SET_RX_MODE,
40a82917 453 WORK_ENABLE,
454 RTL8152_LINK_CHG,
9a4be1bd 455 SELECTIVE_SUSPEND,
aa66a5f1 456 PHY_RESET,
0c3121fc 457 SCHEDULE_TASKLET,
ac718b69 458};
459
460/* Define these values to match your device */
461#define VENDOR_ID_REALTEK 0x0bda
462#define PRODUCT_ID_RTL8152 0x8152
43779f8d 463#define PRODUCT_ID_RTL8153 0x8153
464
465#define VENDOR_ID_SAMSUNG 0x04e8
466#define PRODUCT_ID_SAMSUNG 0xa101
ac718b69 467
468#define MCU_TYPE_PLA 0x0100
469#define MCU_TYPE_USB 0x0000
470
c7de7dec 471#define REALTEK_USB_DEVICE(vend, prod) \
472 USB_DEVICE_INTERFACE_CLASS(vend, prod, USB_CLASS_VENDOR_SPEC)
473
4f1d4d54 474struct tally_counter {
475 __le64 tx_packets;
476 __le64 rx_packets;
477 __le64 tx_errors;
478 __le32 rx_errors;
479 __le16 rx_missed;
480 __le16 align_errors;
481 __le32 tx_one_collision;
482 __le32 tx_multi_collision;
483 __le64 rx_unicast;
484 __le64 rx_broadcast;
485 __le32 rx_multicast;
486 __le16 tx_aborted;
487 __le16 tx_underun;
488};
489
ac718b69 490struct rx_desc {
500b6d7e 491 __le32 opts1;
ac718b69 492#define RX_LEN_MASK 0x7fff
565cab0a 493
500b6d7e 494 __le32 opts2;
565cab0a 495#define RD_UDP_CS (1 << 23)
496#define RD_TCP_CS (1 << 22)
6128d1bb 497#define RD_IPV6_CS (1 << 20)
565cab0a 498#define RD_IPV4_CS (1 << 19)
499
500b6d7e 500 __le32 opts3;
565cab0a 501#define IPF (1 << 23) /* IP checksum fail */
502#define UDPF (1 << 22) /* UDP checksum fail */
503#define TCPF (1 << 21) /* TCP checksum fail */
c5554298 504#define RX_VLAN_TAG (1 << 16)
565cab0a 505
500b6d7e 506 __le32 opts4;
507 __le32 opts5;
508 __le32 opts6;
ac718b69 509};
510
511struct tx_desc {
500b6d7e 512 __le32 opts1;
ac718b69 513#define TX_FS (1 << 31) /* First segment of a packet */
514#define TX_LS (1 << 30) /* Final segment of a packet */
60c89071 515#define GTSENDV4 (1 << 28)
6128d1bb 516#define GTSENDV6 (1 << 27)
60c89071 517#define GTTCPHO_SHIFT 18
6128d1bb 518#define GTTCPHO_MAX 0x7fU
60c89071 519#define TX_LEN_MAX 0x3ffffU
5bd23881 520
500b6d7e 521 __le32 opts2;
5bd23881 522#define UDP_CS (1 << 31) /* Calculate UDP/IP checksum */
523#define TCP_CS (1 << 30) /* Calculate TCP/IP checksum */
524#define IPV4_CS (1 << 29) /* Calculate IPv4 checksum */
525#define IPV6_CS (1 << 28) /* Calculate IPv6 checksum */
60c89071 526#define MSS_SHIFT 17
527#define MSS_MAX 0x7ffU
528#define TCPHO_SHIFT 17
6128d1bb 529#define TCPHO_MAX 0x7ffU
c5554298 530#define TX_VLAN_TAG (1 << 16)
ac718b69 531};
532
dff4e8ad 533struct r8152;
534
ebc2ec48 535struct rx_agg {
536 struct list_head list;
537 struct urb *urb;
dff4e8ad 538 struct r8152 *context;
ebc2ec48 539 void *buffer;
540 void *head;
541};
542
543struct tx_agg {
544 struct list_head list;
545 struct urb *urb;
dff4e8ad 546 struct r8152 *context;
ebc2ec48 547 void *buffer;
548 void *head;
549 u32 skb_num;
550 u32 skb_len;
551};
552
ac718b69 553struct r8152 {
554 unsigned long flags;
555 struct usb_device *udev;
556 struct tasklet_struct tl;
40a82917 557 struct usb_interface *intf;
ac718b69 558 struct net_device *netdev;
40a82917 559 struct urb *intr_urb;
ebc2ec48 560 struct tx_agg tx_info[RTL8152_MAX_TX];
561 struct rx_agg rx_info[RTL8152_MAX_RX];
562 struct list_head rx_done, tx_free;
563 struct sk_buff_head tx_queue;
564 spinlock_t rx_lock, tx_lock;
ac718b69 565 struct delayed_work schedule;
566 struct mii_if_info mii;
c81229c9 567
568 struct rtl_ops {
569 void (*init)(struct r8152 *);
570 int (*enable)(struct r8152 *);
571 void (*disable)(struct r8152 *);
7e9da481 572 void (*up)(struct r8152 *);
c81229c9 573 void (*down)(struct r8152 *);
574 void (*unload)(struct r8152 *);
575 } rtl_ops;
576
40a82917 577 int intr_interval;
21ff2e89 578 u32 saved_wolopts;
ac718b69 579 u32 msg_enable;
dd1b119c 580 u32 tx_qlen;
ac718b69 581 u16 ocp_base;
40a82917 582 u8 *intr_buff;
ac718b69 583 u8 version;
584 u8 speed;
585};
586
587enum rtl_version {
588 RTL_VER_UNKNOWN = 0,
589 RTL_VER_01,
43779f8d 590 RTL_VER_02,
591 RTL_VER_03,
592 RTL_VER_04,
593 RTL_VER_05,
594 RTL_VER_MAX
ac718b69 595};
596
60c89071 597enum tx_csum_stat {
598 TX_CSUM_SUCCESS = 0,
599 TX_CSUM_TSO,
600 TX_CSUM_NONE
601};
602
ac718b69 603/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
604 * The RTL chips use a 64 element hash table based on the Ethernet CRC.
605 */
606static const int multicast_filter_limit = 32;
52aec126 607static unsigned int agg_buf_sz = 16384;
ac718b69 608
52aec126 609#define RTL_LIMITED_TSO_SIZE (agg_buf_sz - sizeof(struct tx_desc) - \
60c89071 610 VLAN_ETH_HLEN - VLAN_HLEN)
611
ac718b69 612static
613int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
614{
31787f53 615 int ret;
616 void *tmp;
617
618 tmp = kmalloc(size, GFP_KERNEL);
619 if (!tmp)
620 return -ENOMEM;
621
622 ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0),
b209af99 623 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
624 value, index, tmp, size, 500);
31787f53 625
626 memcpy(data, tmp, size);
627 kfree(tmp);
628
629 return ret;
ac718b69 630}
631
632static
633int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
634{
31787f53 635 int ret;
636 void *tmp;
637
c4438f03 638 tmp = kmemdup(data, size, GFP_KERNEL);
31787f53 639 if (!tmp)
640 return -ENOMEM;
641
31787f53 642 ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0),
b209af99 643 RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
644 value, index, tmp, size, 500);
31787f53 645
646 kfree(tmp);
db8515ef 647
31787f53 648 return ret;
ac718b69 649}
650
651static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
b209af99 652 void *data, u16 type)
ac718b69 653{
45f4a19f 654 u16 limit = 64;
655 int ret = 0;
ac718b69 656
657 if (test_bit(RTL8152_UNPLUG, &tp->flags))
658 return -ENODEV;
659
660 /* both size and indix must be 4 bytes align */
661 if ((size & 3) || !size || (index & 3) || !data)
662 return -EPERM;
663
664 if ((u32)index + (u32)size > 0xffff)
665 return -EPERM;
666
667 while (size) {
668 if (size > limit) {
669 ret = get_registers(tp, index, type, limit, data);
670 if (ret < 0)
671 break;
672
673 index += limit;
674 data += limit;
675 size -= limit;
676 } else {
677 ret = get_registers(tp, index, type, size, data);
678 if (ret < 0)
679 break;
680
681 index += size;
682 data += size;
683 size = 0;
684 break;
685 }
686 }
687
688 return ret;
689}
690
691static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
b209af99 692 u16 size, void *data, u16 type)
ac718b69 693{
45f4a19f 694 int ret;
695 u16 byteen_start, byteen_end, byen;
696 u16 limit = 512;
ac718b69 697
698 if (test_bit(RTL8152_UNPLUG, &tp->flags))
699 return -ENODEV;
700
701 /* both size and indix must be 4 bytes align */
702 if ((size & 3) || !size || (index & 3) || !data)
703 return -EPERM;
704
705 if ((u32)index + (u32)size > 0xffff)
706 return -EPERM;
707
708 byteen_start = byteen & BYTE_EN_START_MASK;
709 byteen_end = byteen & BYTE_EN_END_MASK;
710
711 byen = byteen_start | (byteen_start << 4);
712 ret = set_registers(tp, index, type | byen, 4, data);
713 if (ret < 0)
714 goto error1;
715
716 index += 4;
717 data += 4;
718 size -= 4;
719
720 if (size) {
721 size -= 4;
722
723 while (size) {
724 if (size > limit) {
725 ret = set_registers(tp, index,
b209af99 726 type | BYTE_EN_DWORD,
727 limit, data);
ac718b69 728 if (ret < 0)
729 goto error1;
730
731 index += limit;
732 data += limit;
733 size -= limit;
734 } else {
735 ret = set_registers(tp, index,
b209af99 736 type | BYTE_EN_DWORD,
737 size, data);
ac718b69 738 if (ret < 0)
739 goto error1;
740
741 index += size;
742 data += size;
743 size = 0;
744 break;
745 }
746 }
747
748 byen = byteen_end | (byteen_end >> 4);
749 ret = set_registers(tp, index, type | byen, 4, data);
750 if (ret < 0)
751 goto error1;
752 }
753
754error1:
755 return ret;
756}
757
758static inline
759int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
760{
761 return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
762}
763
764static inline
765int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
766{
767 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
768}
769
770static inline
771int usb_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
772{
773 return generic_ocp_read(tp, index, size, data, MCU_TYPE_USB);
774}
775
776static inline
777int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
778{
779 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
780}
781
782static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
783{
c8826de8 784 __le32 data;
ac718b69 785
c8826de8 786 generic_ocp_read(tp, index, sizeof(data), &data, type);
ac718b69 787
788 return __le32_to_cpu(data);
789}
790
791static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
792{
c8826de8 793 __le32 tmp = __cpu_to_le32(data);
794
795 generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
ac718b69 796}
797
798static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
799{
800 u32 data;
c8826de8 801 __le32 tmp;
ac718b69 802 u8 shift = index & 2;
803
804 index &= ~3;
805
c8826de8 806 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
ac718b69 807
c8826de8 808 data = __le32_to_cpu(tmp);
ac718b69 809 data >>= (shift * 8);
810 data &= 0xffff;
811
812 return (u16)data;
813}
814
815static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
816{
c8826de8 817 u32 mask = 0xffff;
818 __le32 tmp;
ac718b69 819 u16 byen = BYTE_EN_WORD;
820 u8 shift = index & 2;
821
822 data &= mask;
823
824 if (index & 2) {
825 byen <<= shift;
826 mask <<= (shift * 8);
827 data <<= (shift * 8);
828 index &= ~3;
829 }
830
c8826de8 831 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
ac718b69 832
c8826de8 833 data |= __le32_to_cpu(tmp) & ~mask;
834 tmp = __cpu_to_le32(data);
ac718b69 835
c8826de8 836 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
ac718b69 837}
838
839static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
840{
841 u32 data;
c8826de8 842 __le32 tmp;
ac718b69 843 u8 shift = index & 3;
844
845 index &= ~3;
846
c8826de8 847 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
ac718b69 848
c8826de8 849 data = __le32_to_cpu(tmp);
ac718b69 850 data >>= (shift * 8);
851 data &= 0xff;
852
853 return (u8)data;
854}
855
856static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
857{
c8826de8 858 u32 mask = 0xff;
859 __le32 tmp;
ac718b69 860 u16 byen = BYTE_EN_BYTE;
861 u8 shift = index & 3;
862
863 data &= mask;
864
865 if (index & 3) {
866 byen <<= shift;
867 mask <<= (shift * 8);
868 data <<= (shift * 8);
869 index &= ~3;
870 }
871
c8826de8 872 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
ac718b69 873
c8826de8 874 data |= __le32_to_cpu(tmp) & ~mask;
875 tmp = __cpu_to_le32(data);
ac718b69 876
c8826de8 877 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
ac718b69 878}
879
ac244d3e 880static u16 ocp_reg_read(struct r8152 *tp, u16 addr)
e3fe0b1a 881{
882 u16 ocp_base, ocp_index;
883
884 ocp_base = addr & 0xf000;
885 if (ocp_base != tp->ocp_base) {
886 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
887 tp->ocp_base = ocp_base;
888 }
889
890 ocp_index = (addr & 0x0fff) | 0xb000;
ac244d3e 891 return ocp_read_word(tp, MCU_TYPE_PLA, ocp_index);
e3fe0b1a 892}
893
ac244d3e 894static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
ac718b69 895{
ac244d3e 896 u16 ocp_base, ocp_index;
ac718b69 897
ac244d3e 898 ocp_base = addr & 0xf000;
899 if (ocp_base != tp->ocp_base) {
900 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
901 tp->ocp_base = ocp_base;
ac718b69 902 }
ac244d3e 903
904 ocp_index = (addr & 0x0fff) | 0xb000;
905 ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
ac718b69 906}
907
ac244d3e 908static inline void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
ac718b69 909{
ac244d3e 910 ocp_reg_write(tp, OCP_BASE_MII + reg_addr * 2, value);
911}
ac718b69 912
ac244d3e 913static inline int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
914{
915 return ocp_reg_read(tp, OCP_BASE_MII + reg_addr * 2);
ac718b69 916}
917
43779f8d 918static void sram_write(struct r8152 *tp, u16 addr, u16 data)
919{
920 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
921 ocp_reg_write(tp, OCP_SRAM_DATA, data);
922}
923
924static u16 sram_read(struct r8152 *tp, u16 addr)
925{
926 ocp_reg_write(tp, OCP_SRAM_ADDR, addr);
927 return ocp_reg_read(tp, OCP_SRAM_DATA);
928}
929
ac718b69 930static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
931{
932 struct r8152 *tp = netdev_priv(netdev);
9a4be1bd 933 int ret;
ac718b69 934
6871438c 935 if (test_bit(RTL8152_UNPLUG, &tp->flags))
936 return -ENODEV;
937
ac718b69 938 if (phy_id != R8152_PHY_ID)
939 return -EINVAL;
940
9a4be1bd 941 ret = usb_autopm_get_interface(tp->intf);
942 if (ret < 0)
943 goto out;
944
945 ret = r8152_mdio_read(tp, reg);
946
947 usb_autopm_put_interface(tp->intf);
948
949out:
950 return ret;
ac718b69 951}
952
953static
954void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
955{
956 struct r8152 *tp = netdev_priv(netdev);
957
6871438c 958 if (test_bit(RTL8152_UNPLUG, &tp->flags))
959 return;
960
ac718b69 961 if (phy_id != R8152_PHY_ID)
962 return;
963
9a4be1bd 964 if (usb_autopm_get_interface(tp->intf) < 0)
965 return;
966
ac718b69 967 r8152_mdio_write(tp, reg, val);
9a4be1bd 968
969 usb_autopm_put_interface(tp->intf);
ac718b69 970}
971
b209af99 972static int
973r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);
ebc2ec48 974
8ba789ab 975static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
976{
977 struct r8152 *tp = netdev_priv(netdev);
978 struct sockaddr *addr = p;
979
980 if (!is_valid_ether_addr(addr->sa_data))
981 return -EADDRNOTAVAIL;
982
983 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
984
985 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
986 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
987 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
988
989 return 0;
990}
991
179bb6d7 992static int set_ethernet_addr(struct r8152 *tp)
ac718b69 993{
994 struct net_device *dev = tp->netdev;
179bb6d7 995 struct sockaddr sa;
8a91c824 996 int ret;
ac718b69 997
8a91c824 998 if (tp->version == RTL_VER_01)
179bb6d7 999 ret = pla_ocp_read(tp, PLA_IDR, 8, sa.sa_data);
8a91c824 1000 else
179bb6d7 1001 ret = pla_ocp_read(tp, PLA_BACKUP, 8, sa.sa_data);
8a91c824 1002
1003 if (ret < 0) {
179bb6d7 1004 netif_err(tp, probe, dev, "Get ether addr fail\n");
1005 } else if (!is_valid_ether_addr(sa.sa_data)) {
1006 netif_err(tp, probe, dev, "Invalid ether addr %pM\n",
1007 sa.sa_data);
1008 eth_hw_addr_random(dev);
1009 ether_addr_copy(sa.sa_data, dev->dev_addr);
1010 ret = rtl8152_set_mac_address(dev, &sa);
1011 netif_info(tp, probe, dev, "Random ether addr %pM\n",
1012 sa.sa_data);
8a91c824 1013 } else {
179bb6d7 1014 if (tp->version == RTL_VER_01)
1015 ether_addr_copy(dev->dev_addr, sa.sa_data);
1016 else
1017 ret = rtl8152_set_mac_address(dev, &sa);
ac718b69 1018 }
179bb6d7 1019
1020 return ret;
ac718b69 1021}
1022
ac718b69 1023static void read_bulk_callback(struct urb *urb)
1024{
ac718b69 1025 struct net_device *netdev;
ac718b69 1026 int status = urb->status;
ebc2ec48 1027 struct rx_agg *agg;
1028 struct r8152 *tp;
ac718b69 1029 int result;
ac718b69 1030
ebc2ec48 1031 agg = urb->context;
1032 if (!agg)
1033 return;
1034
1035 tp = agg->context;
ac718b69 1036 if (!tp)
1037 return;
ebc2ec48 1038
ac718b69 1039 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1040 return;
ebc2ec48 1041
1042 if (!test_bit(WORK_ENABLE, &tp->flags))
1043 return;
1044
ac718b69 1045 netdev = tp->netdev;
7559fb2f 1046
1047 /* When link down, the driver would cancel all bulks. */
1048 /* This avoid the re-submitting bulk */
ebc2ec48 1049 if (!netif_carrier_ok(netdev))
ac718b69 1050 return;
1051
9a4be1bd 1052 usb_mark_last_busy(tp->udev);
1053
ac718b69 1054 switch (status) {
1055 case 0:
ebc2ec48 1056 if (urb->actual_length < ETH_ZLEN)
1057 break;
1058
2685d410 1059 spin_lock(&tp->rx_lock);
ebc2ec48 1060 list_add_tail(&agg->list, &tp->rx_done);
2685d410 1061 spin_unlock(&tp->rx_lock);
ebc2ec48 1062 tasklet_schedule(&tp->tl);
1063 return;
ac718b69 1064 case -ESHUTDOWN:
1065 set_bit(RTL8152_UNPLUG, &tp->flags);
1066 netif_device_detach(tp->netdev);
ebc2ec48 1067 return;
ac718b69 1068 case -ENOENT:
1069 return; /* the urb is in unlink state */
1070 case -ETIME:
4a8deae2
HW
1071 if (net_ratelimit())
1072 netdev_warn(netdev, "maybe reset is needed?\n");
ebc2ec48 1073 break;
ac718b69 1074 default:
4a8deae2
HW
1075 if (net_ratelimit())
1076 netdev_warn(netdev, "Rx status %d\n", status);
ebc2ec48 1077 break;
ac718b69 1078 }
1079
ebc2ec48 1080 result = r8152_submit_rx(tp, agg, GFP_ATOMIC);
ac718b69 1081 if (result == -ENODEV) {
1082 netif_device_detach(tp->netdev);
1083 } else if (result) {
2685d410 1084 spin_lock(&tp->rx_lock);
ebc2ec48 1085 list_add_tail(&agg->list, &tp->rx_done);
2685d410 1086 spin_unlock(&tp->rx_lock);
ebc2ec48 1087 tasklet_schedule(&tp->tl);
ac718b69 1088 }
ac718b69 1089}
1090
ebc2ec48 1091static void write_bulk_callback(struct urb *urb)
ac718b69 1092{
ebc2ec48 1093 struct net_device_stats *stats;
d104eafa 1094 struct net_device *netdev;
ebc2ec48 1095 struct tx_agg *agg;
ac718b69 1096 struct r8152 *tp;
ebc2ec48 1097 int status = urb->status;
ac718b69 1098
ebc2ec48 1099 agg = urb->context;
1100 if (!agg)
ac718b69 1101 return;
1102
ebc2ec48 1103 tp = agg->context;
1104 if (!tp)
1105 return;
1106
d104eafa 1107 netdev = tp->netdev;
05e0f1aa 1108 stats = &netdev->stats;
ebc2ec48 1109 if (status) {
4a8deae2 1110 if (net_ratelimit())
d104eafa 1111 netdev_warn(netdev, "Tx status %d\n", status);
ebc2ec48 1112 stats->tx_errors += agg->skb_num;
ac718b69 1113 } else {
ebc2ec48 1114 stats->tx_packets += agg->skb_num;
1115 stats->tx_bytes += agg->skb_len;
ac718b69 1116 }
1117
2685d410 1118 spin_lock(&tp->tx_lock);
ebc2ec48 1119 list_add_tail(&agg->list, &tp->tx_free);
2685d410 1120 spin_unlock(&tp->tx_lock);
ebc2ec48 1121
9a4be1bd 1122 usb_autopm_put_interface_async(tp->intf);
1123
d104eafa 1124 if (!netif_carrier_ok(netdev))
ebc2ec48 1125 return;
1126
1127 if (!test_bit(WORK_ENABLE, &tp->flags))
1128 return;
1129
1130 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1131 return;
1132
1133 if (!skb_queue_empty(&tp->tx_queue))
0c3121fc 1134 tasklet_schedule(&tp->tl);
ac718b69 1135}
1136
40a82917 1137static void intr_callback(struct urb *urb)
1138{
1139 struct r8152 *tp;
500b6d7e 1140 __le16 *d;
40a82917 1141 int status = urb->status;
1142 int res;
1143
1144 tp = urb->context;
1145 if (!tp)
1146 return;
1147
1148 if (!test_bit(WORK_ENABLE, &tp->flags))
1149 return;
1150
1151 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1152 return;
1153
1154 switch (status) {
1155 case 0: /* success */
1156 break;
1157 case -ECONNRESET: /* unlink */
1158 case -ESHUTDOWN:
1159 netif_device_detach(tp->netdev);
1160 case -ENOENT:
1161 return;
1162 case -EOVERFLOW:
1163 netif_info(tp, intr, tp->netdev, "intr status -EOVERFLOW\n");
1164 goto resubmit;
1165 /* -EPIPE: should clear the halt */
1166 default:
1167 netif_info(tp, intr, tp->netdev, "intr status %d\n", status);
1168 goto resubmit;
1169 }
1170
1171 d = urb->transfer_buffer;
1172 if (INTR_LINK & __le16_to_cpu(d[0])) {
1173 if (!(tp->speed & LINK_STATUS)) {
1174 set_bit(RTL8152_LINK_CHG, &tp->flags);
1175 schedule_delayed_work(&tp->schedule, 0);
1176 }
1177 } else {
1178 if (tp->speed & LINK_STATUS) {
1179 set_bit(RTL8152_LINK_CHG, &tp->flags);
1180 schedule_delayed_work(&tp->schedule, 0);
1181 }
1182 }
1183
1184resubmit:
1185 res = usb_submit_urb(urb, GFP_ATOMIC);
1186 if (res == -ENODEV)
1187 netif_device_detach(tp->netdev);
1188 else if (res)
1189 netif_err(tp, intr, tp->netdev,
4a8deae2 1190 "can't resubmit intr, status %d\n", res);
40a82917 1191}
1192
ebc2ec48 1193static inline void *rx_agg_align(void *data)
1194{
8e1f51bd 1195 return (void *)ALIGN((uintptr_t)data, RX_ALIGN);
ebc2ec48 1196}
1197
1198static inline void *tx_agg_align(void *data)
1199{
8e1f51bd 1200 return (void *)ALIGN((uintptr_t)data, TX_ALIGN);
ebc2ec48 1201}
1202
1203static void free_all_mem(struct r8152 *tp)
1204{
1205 int i;
1206
1207 for (i = 0; i < RTL8152_MAX_RX; i++) {
9629e3c0 1208 usb_free_urb(tp->rx_info[i].urb);
1209 tp->rx_info[i].urb = NULL;
ebc2ec48 1210
9629e3c0 1211 kfree(tp->rx_info[i].buffer);
1212 tp->rx_info[i].buffer = NULL;
1213 tp->rx_info[i].head = NULL;
ebc2ec48 1214 }
1215
1216 for (i = 0; i < RTL8152_MAX_TX; i++) {
9629e3c0 1217 usb_free_urb(tp->tx_info[i].urb);
1218 tp->tx_info[i].urb = NULL;
ebc2ec48 1219
9629e3c0 1220 kfree(tp->tx_info[i].buffer);
1221 tp->tx_info[i].buffer = NULL;
1222 tp->tx_info[i].head = NULL;
ebc2ec48 1223 }
40a82917 1224
9629e3c0 1225 usb_free_urb(tp->intr_urb);
1226 tp->intr_urb = NULL;
40a82917 1227
9629e3c0 1228 kfree(tp->intr_buff);
1229 tp->intr_buff = NULL;
ebc2ec48 1230}
1231
1232static int alloc_all_mem(struct r8152 *tp)
1233{
1234 struct net_device *netdev = tp->netdev;
40a82917 1235 struct usb_interface *intf = tp->intf;
1236 struct usb_host_interface *alt = intf->cur_altsetting;
1237 struct usb_host_endpoint *ep_intr = alt->endpoint + 2;
ebc2ec48 1238 struct urb *urb;
1239 int node, i;
1240 u8 *buf;
1241
1242 node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
1243
1244 spin_lock_init(&tp->rx_lock);
1245 spin_lock_init(&tp->tx_lock);
1246 INIT_LIST_HEAD(&tp->rx_done);
1247 INIT_LIST_HEAD(&tp->tx_free);
1248 skb_queue_head_init(&tp->tx_queue);
1249
1250 for (i = 0; i < RTL8152_MAX_RX; i++) {
52aec126 1251 buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node);
ebc2ec48 1252 if (!buf)
1253 goto err1;
1254
1255 if (buf != rx_agg_align(buf)) {
1256 kfree(buf);
52aec126 1257 buf = kmalloc_node(agg_buf_sz + RX_ALIGN, GFP_KERNEL,
8e1f51bd 1258 node);
ebc2ec48 1259 if (!buf)
1260 goto err1;
1261 }
1262
1263 urb = usb_alloc_urb(0, GFP_KERNEL);
1264 if (!urb) {
1265 kfree(buf);
1266 goto err1;
1267 }
1268
1269 INIT_LIST_HEAD(&tp->rx_info[i].list);
1270 tp->rx_info[i].context = tp;
1271 tp->rx_info[i].urb = urb;
1272 tp->rx_info[i].buffer = buf;
1273 tp->rx_info[i].head = rx_agg_align(buf);
1274 }
1275
1276 for (i = 0; i < RTL8152_MAX_TX; i++) {
52aec126 1277 buf = kmalloc_node(agg_buf_sz, GFP_KERNEL, node);
ebc2ec48 1278 if (!buf)
1279 goto err1;
1280
1281 if (buf != tx_agg_align(buf)) {
1282 kfree(buf);
52aec126 1283 buf = kmalloc_node(agg_buf_sz + TX_ALIGN, GFP_KERNEL,
8e1f51bd 1284 node);
ebc2ec48 1285 if (!buf)
1286 goto err1;
1287 }
1288
1289 urb = usb_alloc_urb(0, GFP_KERNEL);
1290 if (!urb) {
1291 kfree(buf);
1292 goto err1;
1293 }
1294
1295 INIT_LIST_HEAD(&tp->tx_info[i].list);
1296 tp->tx_info[i].context = tp;
1297 tp->tx_info[i].urb = urb;
1298 tp->tx_info[i].buffer = buf;
1299 tp->tx_info[i].head = tx_agg_align(buf);
1300
1301 list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
1302 }
1303
40a82917 1304 tp->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
1305 if (!tp->intr_urb)
1306 goto err1;
1307
1308 tp->intr_buff = kmalloc(INTBUFSIZE, GFP_KERNEL);
1309 if (!tp->intr_buff)
1310 goto err1;
1311
1312 tp->intr_interval = (int)ep_intr->desc.bInterval;
1313 usb_fill_int_urb(tp->intr_urb, tp->udev, usb_rcvintpipe(tp->udev, 3),
b209af99 1314 tp->intr_buff, INTBUFSIZE, intr_callback,
1315 tp, tp->intr_interval);
40a82917 1316
ebc2ec48 1317 return 0;
1318
1319err1:
1320 free_all_mem(tp);
1321 return -ENOMEM;
1322}
1323
0de98f6c 1324static struct tx_agg *r8152_get_tx_agg(struct r8152 *tp)
1325{
1326 struct tx_agg *agg = NULL;
1327 unsigned long flags;
1328
21949ab7 1329 if (list_empty(&tp->tx_free))
1330 return NULL;
1331
0de98f6c 1332 spin_lock_irqsave(&tp->tx_lock, flags);
1333 if (!list_empty(&tp->tx_free)) {
1334 struct list_head *cursor;
1335
1336 cursor = tp->tx_free.next;
1337 list_del_init(cursor);
1338 agg = list_entry(cursor, struct tx_agg, list);
1339 }
1340 spin_unlock_irqrestore(&tp->tx_lock, flags);
1341
1342 return agg;
1343}
1344
60c89071 1345static inline __be16 get_protocol(struct sk_buff *skb)
5bd23881 1346{
60c89071 1347 __be16 protocol;
5bd23881 1348
60c89071 1349 if (skb->protocol == htons(ETH_P_8021Q))
1350 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
1351 else
1352 protocol = skb->protocol;
5bd23881 1353
60c89071 1354 return protocol;
1355}
5bd23881 1356
b209af99 1357/* r8152_csum_workaround()
6128d1bb 1358 * The hw limites the value the transport offset. When the offset is out of the
1359 * range, calculate the checksum by sw.
1360 */
1361static void r8152_csum_workaround(struct r8152 *tp, struct sk_buff *skb,
1362 struct sk_buff_head *list)
1363{
1364 if (skb_shinfo(skb)->gso_size) {
1365 netdev_features_t features = tp->netdev->features;
1366 struct sk_buff_head seg_list;
1367 struct sk_buff *segs, *nskb;
1368
a91d45f1 1369 features &= ~(NETIF_F_SG | NETIF_F_IPV6_CSUM | NETIF_F_TSO6);
6128d1bb 1370 segs = skb_gso_segment(skb, features);
1371 if (IS_ERR(segs) || !segs)
1372 goto drop;
1373
1374 __skb_queue_head_init(&seg_list);
1375
1376 do {
1377 nskb = segs;
1378 segs = segs->next;
1379 nskb->next = NULL;
1380 __skb_queue_tail(&seg_list, nskb);
1381 } while (segs);
1382
1383 skb_queue_splice(&seg_list, list);
1384 dev_kfree_skb(skb);
1385 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1386 if (skb_checksum_help(skb) < 0)
1387 goto drop;
1388
1389 __skb_queue_head(list, skb);
1390 } else {
1391 struct net_device_stats *stats;
1392
1393drop:
1394 stats = &tp->netdev->stats;
1395 stats->tx_dropped++;
1396 dev_kfree_skb(skb);
1397 }
1398}
1399
b209af99 1400/* msdn_giant_send_check()
6128d1bb 1401 * According to the document of microsoft, the TCP Pseudo Header excludes the
1402 * packet length for IPv6 TCP large packets.
1403 */
1404static int msdn_giant_send_check(struct sk_buff *skb)
1405{
1406 const struct ipv6hdr *ipv6h;
1407 struct tcphdr *th;
fcb308d5 1408 int ret;
1409
1410 ret = skb_cow_head(skb, 0);
1411 if (ret)
1412 return ret;
6128d1bb 1413
1414 ipv6h = ipv6_hdr(skb);
1415 th = tcp_hdr(skb);
1416
1417 th->check = 0;
1418 th->check = ~tcp_v6_check(0, &ipv6h->saddr, &ipv6h->daddr, 0);
1419
fcb308d5 1420 return ret;
6128d1bb 1421}
1422
c5554298 1423static inline void rtl_tx_vlan_tag(struct tx_desc *desc, struct sk_buff *skb)
1424{
1425 if (vlan_tx_tag_present(skb)) {
1426 u32 opts2;
1427
1428 opts2 = TX_VLAN_TAG | swab16(vlan_tx_tag_get(skb));
1429 desc->opts2 |= cpu_to_le32(opts2);
1430 }
1431}
1432
1433static inline void rtl_rx_vlan_tag(struct rx_desc *desc, struct sk_buff *skb)
1434{
1435 u32 opts2 = le32_to_cpu(desc->opts2);
1436
1437 if (opts2 & RX_VLAN_TAG)
1438 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1439 swab16(opts2 & 0xffff));
1440}
1441
60c89071 1442static int r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc,
1443 struct sk_buff *skb, u32 len, u32 transport_offset)
1444{
1445 u32 mss = skb_shinfo(skb)->gso_size;
1446 u32 opts1, opts2 = 0;
1447 int ret = TX_CSUM_SUCCESS;
1448
1449 WARN_ON_ONCE(len > TX_LEN_MAX);
1450
1451 opts1 = len | TX_FS | TX_LS;
1452
1453 if (mss) {
6128d1bb 1454 if (transport_offset > GTTCPHO_MAX) {
1455 netif_warn(tp, tx_err, tp->netdev,
1456 "Invalid transport offset 0x%x for TSO\n",
1457 transport_offset);
1458 ret = TX_CSUM_TSO;
1459 goto unavailable;
1460 }
1461
60c89071 1462 switch (get_protocol(skb)) {
1463 case htons(ETH_P_IP):
1464 opts1 |= GTSENDV4;
1465 break;
1466
6128d1bb 1467 case htons(ETH_P_IPV6):
fcb308d5 1468 if (msdn_giant_send_check(skb)) {
1469 ret = TX_CSUM_TSO;
1470 goto unavailable;
1471 }
6128d1bb 1472 opts1 |= GTSENDV6;
6128d1bb 1473 break;
1474
60c89071 1475 default:
1476 WARN_ON_ONCE(1);
1477 break;
1478 }
1479
1480 opts1 |= transport_offset << GTTCPHO_SHIFT;
1481 opts2 |= min(mss, MSS_MAX) << MSS_SHIFT;
1482 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1483 u8 ip_protocol;
5bd23881 1484
6128d1bb 1485 if (transport_offset > TCPHO_MAX) {
1486 netif_warn(tp, tx_err, tp->netdev,
1487 "Invalid transport offset 0x%x\n",
1488 transport_offset);
1489 ret = TX_CSUM_NONE;
1490 goto unavailable;
1491 }
1492
60c89071 1493 switch (get_protocol(skb)) {
5bd23881 1494 case htons(ETH_P_IP):
1495 opts2 |= IPV4_CS;
1496 ip_protocol = ip_hdr(skb)->protocol;
1497 break;
1498
1499 case htons(ETH_P_IPV6):
1500 opts2 |= IPV6_CS;
1501 ip_protocol = ipv6_hdr(skb)->nexthdr;
1502 break;
1503
1504 default:
1505 ip_protocol = IPPROTO_RAW;
1506 break;
1507 }
1508
60c89071 1509 if (ip_protocol == IPPROTO_TCP)
5bd23881 1510 opts2 |= TCP_CS;
60c89071 1511 else if (ip_protocol == IPPROTO_UDP)
5bd23881 1512 opts2 |= UDP_CS;
60c89071 1513 else
5bd23881 1514 WARN_ON_ONCE(1);
5bd23881 1515
60c89071 1516 opts2 |= transport_offset << TCPHO_SHIFT;
5bd23881 1517 }
60c89071 1518
1519 desc->opts2 = cpu_to_le32(opts2);
1520 desc->opts1 = cpu_to_le32(opts1);
1521
6128d1bb 1522unavailable:
60c89071 1523 return ret;
5bd23881 1524}
1525
b1379d9a 1526static int r8152_tx_agg_fill(struct r8152 *tp, struct tx_agg *agg)
1527{
d84130a1 1528 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
9a4be1bd 1529 int remain, ret;
b1379d9a 1530 u8 *tx_data;
1531
d84130a1 1532 __skb_queue_head_init(&skb_head);
0c3121fc 1533 spin_lock(&tx_queue->lock);
d84130a1 1534 skb_queue_splice_init(tx_queue, &skb_head);
0c3121fc 1535 spin_unlock(&tx_queue->lock);
d84130a1 1536
b1379d9a 1537 tx_data = agg->head;
b209af99 1538 agg->skb_num = 0;
1539 agg->skb_len = 0;
52aec126 1540 remain = agg_buf_sz;
b1379d9a 1541
7937f9e5 1542 while (remain >= ETH_ZLEN + sizeof(struct tx_desc)) {
b1379d9a 1543 struct tx_desc *tx_desc;
1544 struct sk_buff *skb;
1545 unsigned int len;
60c89071 1546 u32 offset;
b1379d9a 1547
d84130a1 1548 skb = __skb_dequeue(&skb_head);
b1379d9a 1549 if (!skb)
1550 break;
1551
60c89071 1552 len = skb->len + sizeof(*tx_desc);
1553
1554 if (len > remain) {
d84130a1 1555 __skb_queue_head(&skb_head, skb);
b1379d9a 1556 break;
1557 }
1558
7937f9e5 1559 tx_data = tx_agg_align(tx_data);
b1379d9a 1560 tx_desc = (struct tx_desc *)tx_data;
60c89071 1561
1562 offset = (u32)skb_transport_offset(skb);
1563
6128d1bb 1564 if (r8152_tx_csum(tp, tx_desc, skb, skb->len, offset)) {
1565 r8152_csum_workaround(tp, skb, &skb_head);
1566 continue;
1567 }
60c89071 1568
c5554298 1569 rtl_tx_vlan_tag(tx_desc, skb);
1570
b1379d9a 1571 tx_data += sizeof(*tx_desc);
1572
60c89071 1573 len = skb->len;
1574 if (skb_copy_bits(skb, 0, tx_data, len) < 0) {
1575 struct net_device_stats *stats = &tp->netdev->stats;
1576
1577 stats->tx_dropped++;
1578 dev_kfree_skb_any(skb);
1579 tx_data -= sizeof(*tx_desc);
1580 continue;
1581 }
1582
1583 tx_data += len;
b1379d9a 1584 agg->skb_len += len;
60c89071 1585 agg->skb_num++;
1586
b1379d9a 1587 dev_kfree_skb_any(skb);
1588
52aec126 1589 remain = agg_buf_sz - (int)(tx_agg_align(tx_data) - agg->head);
b1379d9a 1590 }
1591
d84130a1 1592 if (!skb_queue_empty(&skb_head)) {
0c3121fc 1593 spin_lock(&tx_queue->lock);
d84130a1 1594 skb_queue_splice(&skb_head, tx_queue);
0c3121fc 1595 spin_unlock(&tx_queue->lock);
d84130a1 1596 }
1597
0c3121fc 1598 netif_tx_lock(tp->netdev);
dd1b119c 1599
1600 if (netif_queue_stopped(tp->netdev) &&
1601 skb_queue_len(&tp->tx_queue) < tp->tx_qlen)
1602 netif_wake_queue(tp->netdev);
1603
0c3121fc 1604 netif_tx_unlock(tp->netdev);
9a4be1bd 1605
0c3121fc 1606 ret = usb_autopm_get_interface_async(tp->intf);
9a4be1bd 1607 if (ret < 0)
1608 goto out_tx_fill;
dd1b119c 1609
b1379d9a 1610 usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2),
1611 agg->head, (int)(tx_data - (u8 *)agg->head),
1612 (usb_complete_t)write_bulk_callback, agg);
1613
0c3121fc 1614 ret = usb_submit_urb(agg->urb, GFP_ATOMIC);
9a4be1bd 1615 if (ret < 0)
0c3121fc 1616 usb_autopm_put_interface_async(tp->intf);
9a4be1bd 1617
1618out_tx_fill:
1619 return ret;
b1379d9a 1620}
1621
565cab0a 1622static u8 r8152_rx_csum(struct r8152 *tp, struct rx_desc *rx_desc)
1623{
1624 u8 checksum = CHECKSUM_NONE;
1625 u32 opts2, opts3;
1626
1627 if (tp->version == RTL_VER_01)
1628 goto return_result;
1629
1630 opts2 = le32_to_cpu(rx_desc->opts2);
1631 opts3 = le32_to_cpu(rx_desc->opts3);
1632
1633 if (opts2 & RD_IPV4_CS) {
1634 if (opts3 & IPF)
1635 checksum = CHECKSUM_NONE;
1636 else if ((opts2 & RD_UDP_CS) && (opts3 & UDPF))
1637 checksum = CHECKSUM_NONE;
1638 else if ((opts2 & RD_TCP_CS) && (opts3 & TCPF))
1639 checksum = CHECKSUM_NONE;
1640 else
1641 checksum = CHECKSUM_UNNECESSARY;
6128d1bb 1642 } else if (RD_IPV6_CS) {
1643 if ((opts2 & RD_UDP_CS) && !(opts3 & UDPF))
1644 checksum = CHECKSUM_UNNECESSARY;
1645 else if ((opts2 & RD_TCP_CS) && !(opts3 & TCPF))
1646 checksum = CHECKSUM_UNNECESSARY;
565cab0a 1647 }
1648
1649return_result:
1650 return checksum;
1651}
1652
ebc2ec48 1653static void rx_bottom(struct r8152 *tp)
1654{
a5a4f468 1655 unsigned long flags;
d84130a1 1656 struct list_head *cursor, *next, rx_queue;
ebc2ec48 1657
d84130a1 1658 if (list_empty(&tp->rx_done))
1659 return;
1660
1661 INIT_LIST_HEAD(&rx_queue);
a5a4f468 1662 spin_lock_irqsave(&tp->rx_lock, flags);
d84130a1 1663 list_splice_init(&tp->rx_done, &rx_queue);
1664 spin_unlock_irqrestore(&tp->rx_lock, flags);
1665
1666 list_for_each_safe(cursor, next, &rx_queue) {
43a4478d 1667 struct rx_desc *rx_desc;
1668 struct rx_agg *agg;
43a4478d 1669 int len_used = 0;
1670 struct urb *urb;
1671 u8 *rx_data;
1672 int ret;
1673
ebc2ec48 1674 list_del_init(cursor);
ebc2ec48 1675
1676 agg = list_entry(cursor, struct rx_agg, list);
1677 urb = agg->urb;
0de98f6c 1678 if (urb->actual_length < ETH_ZLEN)
1679 goto submit;
ebc2ec48 1680
ebc2ec48 1681 rx_desc = agg->head;
1682 rx_data = agg->head;
7937f9e5 1683 len_used += sizeof(struct rx_desc);
ebc2ec48 1684
7937f9e5 1685 while (urb->actual_length > len_used) {
43a4478d 1686 struct net_device *netdev = tp->netdev;
05e0f1aa 1687 struct net_device_stats *stats = &netdev->stats;
7937f9e5 1688 unsigned int pkt_len;
43a4478d 1689 struct sk_buff *skb;
1690
7937f9e5 1691 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
ebc2ec48 1692 if (pkt_len < ETH_ZLEN)
1693 break;
1694
7937f9e5 1695 len_used += pkt_len;
1696 if (urb->actual_length < len_used)
1697 break;
1698
8e1f51bd 1699 pkt_len -= CRC_SIZE;
ebc2ec48 1700 rx_data += sizeof(struct rx_desc);
1701
1702 skb = netdev_alloc_skb_ip_align(netdev, pkt_len);
1703 if (!skb) {
1704 stats->rx_dropped++;
5e2f7485 1705 goto find_next_rx;
ebc2ec48 1706 }
565cab0a 1707
1708 skb->ip_summed = r8152_rx_csum(tp, rx_desc);
ebc2ec48 1709 memcpy(skb->data, rx_data, pkt_len);
1710 skb_put(skb, pkt_len);
1711 skb->protocol = eth_type_trans(skb, netdev);
c5554298 1712 rtl_rx_vlan_tag(rx_desc, skb);
9d9aafa1 1713 netif_receive_skb(skb);
ebc2ec48 1714 stats->rx_packets++;
1715 stats->rx_bytes += pkt_len;
1716
5e2f7485 1717find_next_rx:
8e1f51bd 1718 rx_data = rx_agg_align(rx_data + pkt_len + CRC_SIZE);
ebc2ec48 1719 rx_desc = (struct rx_desc *)rx_data;
ebc2ec48 1720 len_used = (int)(rx_data - (u8 *)agg->head);
7937f9e5 1721 len_used += sizeof(struct rx_desc);
ebc2ec48 1722 }
1723
0de98f6c 1724submit:
ebc2ec48 1725 ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
ebc2ec48 1726 if (ret && ret != -ENODEV) {
d84130a1 1727 spin_lock_irqsave(&tp->rx_lock, flags);
1728 list_add_tail(&agg->list, &tp->rx_done);
1729 spin_unlock_irqrestore(&tp->rx_lock, flags);
ebc2ec48 1730 tasklet_schedule(&tp->tl);
1731 }
1732 }
ebc2ec48 1733}
1734
1735static void tx_bottom(struct r8152 *tp)
1736{
ebc2ec48 1737 int res;
1738
b1379d9a 1739 do {
1740 struct tx_agg *agg;
ebc2ec48 1741
b1379d9a 1742 if (skb_queue_empty(&tp->tx_queue))
ebc2ec48 1743 break;
1744
b1379d9a 1745 agg = r8152_get_tx_agg(tp);
1746 if (!agg)
ebc2ec48 1747 break;
ebc2ec48 1748
b1379d9a 1749 res = r8152_tx_agg_fill(tp, agg);
1750 if (res) {
05e0f1aa 1751 struct net_device *netdev = tp->netdev;
ebc2ec48 1752
b1379d9a 1753 if (res == -ENODEV) {
1754 netif_device_detach(netdev);
1755 } else {
05e0f1aa 1756 struct net_device_stats *stats = &netdev->stats;
1757 unsigned long flags;
1758
b1379d9a 1759 netif_warn(tp, tx_err, netdev,
1760 "failed tx_urb %d\n", res);
1761 stats->tx_dropped += agg->skb_num;
db8515ef 1762
b1379d9a 1763 spin_lock_irqsave(&tp->tx_lock, flags);
1764 list_add_tail(&agg->list, &tp->tx_free);
1765 spin_unlock_irqrestore(&tp->tx_lock, flags);
1766 }
ebc2ec48 1767 }
b1379d9a 1768 } while (res == 0);
ebc2ec48 1769}
1770
1771static void bottom_half(unsigned long data)
ac718b69 1772{
1773 struct r8152 *tp;
ac718b69 1774
ebc2ec48 1775 tp = (struct r8152 *)data;
1776
1777 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1778 return;
1779
1780 if (!test_bit(WORK_ENABLE, &tp->flags))
ac718b69 1781 return;
ebc2ec48 1782
7559fb2f 1783 /* When link down, the driver would cancel all bulks. */
1784 /* This avoid the re-submitting bulk */
ebc2ec48 1785 if (!netif_carrier_ok(tp->netdev))
ac718b69 1786 return;
ebc2ec48 1787
1788 rx_bottom(tp);
0c3121fc 1789 tx_bottom(tp);
ebc2ec48 1790}
1791
1792static
1793int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
1794{
1795 usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
52aec126 1796 agg->head, agg_buf_sz,
b209af99 1797 (usb_complete_t)read_bulk_callback, agg);
ebc2ec48 1798
1799 return usb_submit_urb(agg->urb, mem_flags);
ac718b69 1800}
1801
00a5e360 1802static void rtl_drop_queued_tx(struct r8152 *tp)
1803{
1804 struct net_device_stats *stats = &tp->netdev->stats;
d84130a1 1805 struct sk_buff_head skb_head, *tx_queue = &tp->tx_queue;
00a5e360 1806 struct sk_buff *skb;
1807
d84130a1 1808 if (skb_queue_empty(tx_queue))
1809 return;
1810
1811 __skb_queue_head_init(&skb_head);
2685d410 1812 spin_lock_bh(&tx_queue->lock);
d84130a1 1813 skb_queue_splice_init(tx_queue, &skb_head);
2685d410 1814 spin_unlock_bh(&tx_queue->lock);
d84130a1 1815
1816 while ((skb = __skb_dequeue(&skb_head))) {
00a5e360 1817 dev_kfree_skb(skb);
1818 stats->tx_dropped++;
1819 }
1820}
1821
ac718b69 1822static void rtl8152_tx_timeout(struct net_device *netdev)
1823{
1824 struct r8152 *tp = netdev_priv(netdev);
ebc2ec48 1825 int i;
1826
4a8deae2 1827 netif_warn(tp, tx_err, netdev, "Tx timeout\n");
ebc2ec48 1828 for (i = 0; i < RTL8152_MAX_TX; i++)
1829 usb_unlink_urb(tp->tx_info[i].urb);
ac718b69 1830}
1831
1832static void rtl8152_set_rx_mode(struct net_device *netdev)
1833{
1834 struct r8152 *tp = netdev_priv(netdev);
1835
40a82917 1836 if (tp->speed & LINK_STATUS) {
ac718b69 1837 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
40a82917 1838 schedule_delayed_work(&tp->schedule, 0);
1839 }
ac718b69 1840}
1841
1842static void _rtl8152_set_rx_mode(struct net_device *netdev)
1843{
1844 struct r8152 *tp = netdev_priv(netdev);
31787f53 1845 u32 mc_filter[2]; /* Multicast hash filter */
1846 __le32 tmp[2];
ac718b69 1847 u32 ocp_data;
1848
ac718b69 1849 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
1850 netif_stop_queue(netdev);
1851 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1852 ocp_data &= ~RCR_ACPT_ALL;
1853 ocp_data |= RCR_AB | RCR_APM;
1854
1855 if (netdev->flags & IFF_PROMISC) {
1856 /* Unconditionally log net taps. */
1857 netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
1858 ocp_data |= RCR_AM | RCR_AAP;
b209af99 1859 mc_filter[1] = 0xffffffff;
1860 mc_filter[0] = 0xffffffff;
ac718b69 1861 } else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
1862 (netdev->flags & IFF_ALLMULTI)) {
1863 /* Too many to filter perfectly -- accept all multicasts. */
1864 ocp_data |= RCR_AM;
b209af99 1865 mc_filter[1] = 0xffffffff;
1866 mc_filter[0] = 0xffffffff;
ac718b69 1867 } else {
1868 struct netdev_hw_addr *ha;
1869
b209af99 1870 mc_filter[1] = 0;
1871 mc_filter[0] = 0;
ac718b69 1872 netdev_for_each_mc_addr(ha, netdev) {
1873 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
b209af99 1874
ac718b69 1875 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
1876 ocp_data |= RCR_AM;
1877 }
1878 }
1879
31787f53 1880 tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
1881 tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
ac718b69 1882
31787f53 1883 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
ac718b69 1884 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1885 netif_wake_queue(netdev);
ac718b69 1886}
1887
1888static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
b209af99 1889 struct net_device *netdev)
ac718b69 1890{
1891 struct r8152 *tp = netdev_priv(netdev);
ac718b69 1892
ebc2ec48 1893 skb_tx_timestamp(skb);
ac718b69 1894
61598788 1895 skb_queue_tail(&tp->tx_queue, skb);
ebc2ec48 1896
0c3121fc 1897 if (!list_empty(&tp->tx_free)) {
1898 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
1899 set_bit(SCHEDULE_TASKLET, &tp->flags);
1900 schedule_delayed_work(&tp->schedule, 0);
1901 } else {
1902 usb_mark_last_busy(tp->udev);
1903 tasklet_schedule(&tp->tl);
1904 }
b209af99 1905 } else if (skb_queue_len(&tp->tx_queue) > tp->tx_qlen) {
dd1b119c 1906 netif_stop_queue(netdev);
b209af99 1907 }
dd1b119c 1908
ac718b69 1909 return NETDEV_TX_OK;
1910}
1911
1912static void r8152b_reset_packet_filter(struct r8152 *tp)
1913{
1914 u32 ocp_data;
1915
1916 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
1917 ocp_data &= ~FMC_FCR_MCU_EN;
1918 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
1919 ocp_data |= FMC_FCR_MCU_EN;
1920 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
1921}
1922
1923static void rtl8152_nic_reset(struct r8152 *tp)
1924{
1925 int i;
1926
1927 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
1928
1929 for (i = 0; i < 1000; i++) {
1930 if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
1931 break;
b209af99 1932 usleep_range(100, 400);
ac718b69 1933 }
1934}
1935
dd1b119c 1936static void set_tx_qlen(struct r8152 *tp)
1937{
1938 struct net_device *netdev = tp->netdev;
1939
52aec126 1940 tp->tx_qlen = agg_buf_sz / (netdev->mtu + VLAN_ETH_HLEN + VLAN_HLEN +
1941 sizeof(struct tx_desc));
dd1b119c 1942}
1943
ac718b69 1944static inline u8 rtl8152_get_speed(struct r8152 *tp)
1945{
1946 return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
1947}
1948
507605a8 1949static void rtl_set_eee_plus(struct r8152 *tp)
ac718b69 1950{
ebc2ec48 1951 u32 ocp_data;
ac718b69 1952 u8 speed;
1953
1954 speed = rtl8152_get_speed(tp);
ebc2ec48 1955 if (speed & _10bps) {
ac718b69 1956 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
ebc2ec48 1957 ocp_data |= EEEP_CR_EEEP_TX;
ac718b69 1958 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
1959 } else {
1960 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
ebc2ec48 1961 ocp_data &= ~EEEP_CR_EEEP_TX;
ac718b69 1962 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
1963 }
507605a8 1964}
1965
00a5e360 1966static void rxdy_gated_en(struct r8152 *tp, bool enable)
1967{
1968 u32 ocp_data;
1969
1970 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
1971 if (enable)
1972 ocp_data |= RXDY_GATED_EN;
1973 else
1974 ocp_data &= ~RXDY_GATED_EN;
1975 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
1976}
1977
507605a8 1978static int rtl_enable(struct r8152 *tp)
1979{
1980 u32 ocp_data;
1981 int i, ret;
ac718b69 1982
1983 r8152b_reset_packet_filter(tp);
1984
1985 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
1986 ocp_data |= CR_RE | CR_TE;
1987 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
1988
00a5e360 1989 rxdy_gated_en(tp, false);
ac718b69 1990
ebc2ec48 1991 INIT_LIST_HEAD(&tp->rx_done);
1992 ret = 0;
1993 for (i = 0; i < RTL8152_MAX_RX; i++) {
1994 INIT_LIST_HEAD(&tp->rx_info[i].list);
1995 ret |= r8152_submit_rx(tp, &tp->rx_info[i], GFP_KERNEL);
1996 }
ac718b69 1997
ebc2ec48 1998 return ret;
ac718b69 1999}
2000
507605a8 2001static int rtl8152_enable(struct r8152 *tp)
2002{
6871438c 2003 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2004 return -ENODEV;
2005
507605a8 2006 set_tx_qlen(tp);
2007 rtl_set_eee_plus(tp);
2008
2009 return rtl_enable(tp);
2010}
2011
43779f8d 2012static void r8153_set_rx_agg(struct r8152 *tp)
2013{
2014 u8 speed;
2015
2016 speed = rtl8152_get_speed(tp);
2017 if (speed & _1000bps) {
2018 if (tp->udev->speed == USB_SPEED_SUPER) {
2019 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH,
2020 RX_THR_SUPPER);
2021 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
2022 EARLY_AGG_SUPPER);
2023 } else {
2024 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH,
2025 RX_THR_HIGH);
2026 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
2027 EARLY_AGG_HIGH);
2028 }
2029 } else {
2030 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_SLOW);
2031 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_EARLY_AGG,
2032 EARLY_AGG_SLOW);
2033 }
2034}
2035
2036static int rtl8153_enable(struct r8152 *tp)
2037{
6871438c 2038 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2039 return -ENODEV;
2040
43779f8d 2041 set_tx_qlen(tp);
2042 rtl_set_eee_plus(tp);
2043 r8153_set_rx_agg(tp);
2044
2045 return rtl_enable(tp);
2046}
2047
d70b1137 2048static void rtl_disable(struct r8152 *tp)
ac718b69 2049{
ebc2ec48 2050 u32 ocp_data;
2051 int i;
ac718b69 2052
6871438c 2053 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
2054 rtl_drop_queued_tx(tp);
2055 return;
2056 }
2057
ac718b69 2058 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2059 ocp_data &= ~RCR_ACPT_ALL;
2060 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2061
00a5e360 2062 rtl_drop_queued_tx(tp);
ebc2ec48 2063
2064 for (i = 0; i < RTL8152_MAX_TX; i++)
2065 usb_kill_urb(tp->tx_info[i].urb);
ac718b69 2066
00a5e360 2067 rxdy_gated_en(tp, true);
ac718b69 2068
2069 for (i = 0; i < 1000; i++) {
2070 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2071 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
2072 break;
8ddfa077 2073 usleep_range(1000, 2000);
ac718b69 2074 }
2075
2076 for (i = 0; i < 1000; i++) {
2077 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
2078 break;
8ddfa077 2079 usleep_range(1000, 2000);
ac718b69 2080 }
2081
ebc2ec48 2082 for (i = 0; i < RTL8152_MAX_RX; i++)
2083 usb_kill_urb(tp->rx_info[i].urb);
ac718b69 2084
2085 rtl8152_nic_reset(tp);
2086}
2087
00a5e360 2088static void r8152_power_cut_en(struct r8152 *tp, bool enable)
2089{
2090 u32 ocp_data;
2091
2092 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
2093 if (enable)
2094 ocp_data |= POWER_CUT;
2095 else
2096 ocp_data &= ~POWER_CUT;
2097 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
2098
2099 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
2100 ocp_data &= ~RESUME_INDICATE;
2101 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
00a5e360 2102}
2103
c5554298 2104static void rtl_rx_vlan_en(struct r8152 *tp, bool enable)
2105{
2106 u32 ocp_data;
2107
2108 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
2109 if (enable)
2110 ocp_data |= CPCR_RX_VLAN;
2111 else
2112 ocp_data &= ~CPCR_RX_VLAN;
2113 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
2114}
2115
2116static int rtl8152_set_features(struct net_device *dev,
2117 netdev_features_t features)
2118{
2119 netdev_features_t changed = features ^ dev->features;
2120 struct r8152 *tp = netdev_priv(dev);
2121
2122 if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
2123 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2124 rtl_rx_vlan_en(tp, true);
2125 else
2126 rtl_rx_vlan_en(tp, false);
2127 }
2128
2129 return 0;
2130}
2131
21ff2e89 2132#define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
2133
2134static u32 __rtl_get_wol(struct r8152 *tp)
2135{
2136 u32 ocp_data;
2137 u32 wolopts = 0;
2138
2139 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CONFIG5);
2140 if (!(ocp_data & LAN_WAKE_EN))
2141 return 0;
2142
2143 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2144 if (ocp_data & LINK_ON_WAKE_EN)
2145 wolopts |= WAKE_PHY;
2146
2147 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
2148 if (ocp_data & UWF_EN)
2149 wolopts |= WAKE_UCAST;
2150 if (ocp_data & BWF_EN)
2151 wolopts |= WAKE_BCAST;
2152 if (ocp_data & MWF_EN)
2153 wolopts |= WAKE_MCAST;
2154
2155 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2156 if (ocp_data & MAGIC_EN)
2157 wolopts |= WAKE_MAGIC;
2158
2159 return wolopts;
2160}
2161
2162static void __rtl_set_wol(struct r8152 *tp, u32 wolopts)
2163{
2164 u32 ocp_data;
2165
2166 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
2167
2168 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2169 ocp_data &= ~LINK_ON_WAKE_EN;
2170 if (wolopts & WAKE_PHY)
2171 ocp_data |= LINK_ON_WAKE_EN;
2172 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
2173
2174 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG5);
2175 ocp_data &= ~(UWF_EN | BWF_EN | MWF_EN | LAN_WAKE_EN);
2176 if (wolopts & WAKE_UCAST)
2177 ocp_data |= UWF_EN;
2178 if (wolopts & WAKE_BCAST)
2179 ocp_data |= BWF_EN;
2180 if (wolopts & WAKE_MCAST)
2181 ocp_data |= MWF_EN;
2182 if (wolopts & WAKE_ANY)
2183 ocp_data |= LAN_WAKE_EN;
2184 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG5, ocp_data);
2185
2186 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2187
2188 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
2189 ocp_data &= ~MAGIC_EN;
2190 if (wolopts & WAKE_MAGIC)
2191 ocp_data |= MAGIC_EN;
2192 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
2193
2194 if (wolopts & WAKE_ANY)
2195 device_set_wakeup_enable(&tp->udev->dev, true);
2196 else
2197 device_set_wakeup_enable(&tp->udev->dev, false);
2198}
2199
9a4be1bd 2200static void rtl_runtime_suspend_enable(struct r8152 *tp, bool enable)
2201{
2202 if (enable) {
2203 u32 ocp_data;
2204
2205 __rtl_set_wol(tp, WAKE_ANY);
2206
2207 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
2208
2209 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CONFIG34);
2210 ocp_data |= LINK_OFF_WAKE_EN;
2211 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CONFIG34, ocp_data);
2212
2213 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2214 } else {
2215 __rtl_set_wol(tp, tp->saved_wolopts);
2216 }
2217}
2218
aa66a5f1 2219static void rtl_phy_reset(struct r8152 *tp)
2220{
2221 u16 data;
2222 int i;
2223
2224 clear_bit(PHY_RESET, &tp->flags);
2225
2226 data = r8152_mdio_read(tp, MII_BMCR);
2227
2228 /* don't reset again before the previous one complete */
2229 if (data & BMCR_RESET)
2230 return;
2231
2232 data |= BMCR_RESET;
2233 r8152_mdio_write(tp, MII_BMCR, data);
2234
2235 for (i = 0; i < 50; i++) {
2236 msleep(20);
2237 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
2238 break;
2239 }
2240}
2241
4349968a 2242static void rtl_clear_bp(struct r8152 *tp)
2243{
2244 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_0, 0);
2245 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_2, 0);
2246 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_4, 0);
2247 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_6, 0);
2248 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_0, 0);
2249 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_2, 0);
2250 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_4, 0);
2251 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_6, 0);
8ddfa077 2252 usleep_range(3000, 6000);
4349968a 2253 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_BA, 0);
2254 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_BA, 0);
2255}
2256
2257static void r8153_clear_bp(struct r8152 *tp)
2258{
2259 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_BP_EN, 0);
2260 ocp_write_byte(tp, MCU_TYPE_USB, USB_BP_EN, 0);
2261 rtl_clear_bp(tp);
2262}
2263
2264static void r8153_teredo_off(struct r8152 *tp)
2265{
2266 u32 ocp_data;
2267
2268 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
2269 ocp_data &= ~(TEREDO_SEL | TEREDO_RS_EVENT_MASK | OOB_TEREDO_EN);
2270 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
2271
2272 ocp_write_word(tp, MCU_TYPE_PLA, PLA_WDT6_CTRL, WDT6_SET_MODE);
2273 ocp_write_word(tp, MCU_TYPE_PLA, PLA_REALWOW_TIMER, 0);
2274 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TEREDO_TIMER, 0);
2275}
2276
2277static void r8152b_disable_aldps(struct r8152 *tp)
2278{
2279 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA | DIS_SDSAVE);
2280 msleep(20);
2281}
2282
2283static inline void r8152b_enable_aldps(struct r8152 *tp)
2284{
2285 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
2286 LINKENA | DIS_SDSAVE);
2287}
2288
d70b1137 2289static void rtl8152_disable(struct r8152 *tp)
2290{
2291 r8152b_disable_aldps(tp);
2292 rtl_disable(tp);
2293 r8152b_enable_aldps(tp);
2294}
2295
4349968a 2296static void r8152b_hw_phy_cfg(struct r8152 *tp)
2297{
f0cbe0ac 2298 u16 data;
2299
2300 data = r8152_mdio_read(tp, MII_BMCR);
2301 if (data & BMCR_PDOWN) {
2302 data &= ~BMCR_PDOWN;
2303 r8152_mdio_write(tp, MII_BMCR, data);
2304 }
2305
7e9da481 2306 rtl_clear_bp(tp);
2307
aa66a5f1 2308 set_bit(PHY_RESET, &tp->flags);
4349968a 2309}
2310
ac718b69 2311static void r8152b_exit_oob(struct r8152 *tp)
2312{
db8515ef 2313 u32 ocp_data;
2314 int i;
ac718b69 2315
2316 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2317 ocp_data &= ~RCR_ACPT_ALL;
2318 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2319
00a5e360 2320 rxdy_gated_en(tp, true);
da9bd117 2321 r8153_teredo_off(tp);
7e9da481 2322 r8152b_hw_phy_cfg(tp);
ac718b69 2323
2324 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
2325 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
2326
2327 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2328 ocp_data &= ~NOW_IS_OOB;
2329 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2330
2331 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2332 ocp_data &= ~MCU_BORW_EN;
2333 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2334
2335 for (i = 0; i < 1000; i++) {
2336 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2337 if (ocp_data & LINK_LIST_READY)
2338 break;
8ddfa077 2339 usleep_range(1000, 2000);
ac718b69 2340 }
2341
2342 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2343 ocp_data |= RE_INIT_LL;
2344 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2345
2346 for (i = 0; i < 1000; i++) {
2347 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2348 if (ocp_data & LINK_LIST_READY)
2349 break;
8ddfa077 2350 usleep_range(1000, 2000);
ac718b69 2351 }
2352
2353 rtl8152_nic_reset(tp);
2354
2355 /* rx share fifo credit full threshold */
2356 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
2357
a3cc465d 2358 if (tp->udev->speed == USB_SPEED_FULL ||
2359 tp->udev->speed == USB_SPEED_LOW) {
ac718b69 2360 /* rx share fifo credit near full threshold */
2361 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
2362 RXFIFO_THR2_FULL);
2363 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
2364 RXFIFO_THR3_FULL);
2365 } else {
2366 /* rx share fifo credit near full threshold */
2367 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
2368 RXFIFO_THR2_HIGH);
2369 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
2370 RXFIFO_THR3_HIGH);
2371 }
2372
2373 /* TX share fifo free credit full threshold */
2374 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL);
2375
2376 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
8e1f51bd 2377 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_THR_HIGH);
ac718b69 2378 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
2379 TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
2380
c5554298 2381 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
ac718b69 2382
2383 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2384
2385 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
2386 ocp_data |= TCR0_AUTO_FIFO;
2387 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
2388}
2389
2390static void r8152b_enter_oob(struct r8152 *tp)
2391{
45f4a19f 2392 u32 ocp_data;
2393 int i;
ac718b69 2394
2395 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2396 ocp_data &= ~NOW_IS_OOB;
2397 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2398
2399 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
2400 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
2401 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
2402
d70b1137 2403 rtl_disable(tp);
ac718b69 2404
2405 for (i = 0; i < 1000; i++) {
2406 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2407 if (ocp_data & LINK_LIST_READY)
2408 break;
8ddfa077 2409 usleep_range(1000, 2000);
ac718b69 2410 }
2411
2412 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2413 ocp_data |= RE_INIT_LL;
2414 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2415
2416 for (i = 0; i < 1000; i++) {
2417 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2418 if (ocp_data & LINK_LIST_READY)
2419 break;
8ddfa077 2420 usleep_range(1000, 2000);
ac718b69 2421 }
2422
2423 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
2424
c5554298 2425 rtl_rx_vlan_en(tp, true);
ac718b69 2426
2427 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
2428 ocp_data |= ALDPS_PROXY_MODE;
2429 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
2430
2431 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2432 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
2433 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2434
00a5e360 2435 rxdy_gated_en(tp, false);
ac718b69 2436
2437 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2438 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
2439 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2440}
2441
43779f8d 2442static void r8153_hw_phy_cfg(struct r8152 *tp)
2443{
2444 u32 ocp_data;
2445 u16 data;
2446
2447 ocp_reg_write(tp, OCP_ADC_CFG, CKADSEL_L | ADC_EN | EN_EMI_L);
f0cbe0ac 2448 data = r8152_mdio_read(tp, MII_BMCR);
2449 if (data & BMCR_PDOWN) {
2450 data &= ~BMCR_PDOWN;
2451 r8152_mdio_write(tp, MII_BMCR, data);
2452 }
43779f8d 2453
7e9da481 2454 r8153_clear_bp(tp);
2455
43779f8d 2456 if (tp->version == RTL_VER_03) {
2457 data = ocp_reg_read(tp, OCP_EEE_CFG);
2458 data &= ~CTAP_SHORT_EN;
2459 ocp_reg_write(tp, OCP_EEE_CFG, data);
2460 }
2461
2462 data = ocp_reg_read(tp, OCP_POWER_CFG);
2463 data |= EEE_CLKDIV_EN;
2464 ocp_reg_write(tp, OCP_POWER_CFG, data);
2465
2466 data = ocp_reg_read(tp, OCP_DOWN_SPEED);
2467 data |= EN_10M_BGOFF;
2468 ocp_reg_write(tp, OCP_DOWN_SPEED, data);
2469 data = ocp_reg_read(tp, OCP_POWER_CFG);
2470 data |= EN_10M_PLLOFF;
2471 ocp_reg_write(tp, OCP_POWER_CFG, data);
2472 data = sram_read(tp, SRAM_IMPEDANCE);
2473 data &= ~RX_DRIVING_MASK;
2474 sram_write(tp, SRAM_IMPEDANCE, data);
2475
2476 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
2477 ocp_data |= PFM_PWM_SWITCH;
2478 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
2479
2480 data = sram_read(tp, SRAM_LPF_CFG);
2481 data |= LPF_AUTO_TUNE;
2482 sram_write(tp, SRAM_LPF_CFG, data);
2483
2484 data = sram_read(tp, SRAM_10M_AMP1);
2485 data |= GDAC_IB_UPALL;
2486 sram_write(tp, SRAM_10M_AMP1, data);
2487 data = sram_read(tp, SRAM_10M_AMP2);
2488 data |= AMP_DN;
2489 sram_write(tp, SRAM_10M_AMP2, data);
aa66a5f1 2490
2491 set_bit(PHY_RESET, &tp->flags);
43779f8d 2492}
2493
b9702723 2494static void r8153_u1u2en(struct r8152 *tp, bool enable)
43779f8d 2495{
2496 u8 u1u2[8];
2497
2498 if (enable)
2499 memset(u1u2, 0xff, sizeof(u1u2));
2500 else
2501 memset(u1u2, 0x00, sizeof(u1u2));
2502
2503 usb_ocp_write(tp, USB_TOLERANCE, BYTE_EN_SIX_BYTES, sizeof(u1u2), u1u2);
2504}
2505
b9702723 2506static void r8153_u2p3en(struct r8152 *tp, bool enable)
43779f8d 2507{
2508 u32 ocp_data;
2509
2510 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL);
2511 if (enable)
2512 ocp_data |= U2P3_ENABLE;
2513 else
2514 ocp_data &= ~U2P3_ENABLE;
2515 ocp_write_word(tp, MCU_TYPE_USB, USB_U2P3_CTRL, ocp_data);
2516}
2517
b9702723 2518static void r8153_power_cut_en(struct r8152 *tp, bool enable)
43779f8d 2519{
2520 u32 ocp_data;
2521
2522 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_POWER_CUT);
2523 if (enable)
2524 ocp_data |= PWR_EN | PHASE2_EN;
2525 else
2526 ocp_data &= ~(PWR_EN | PHASE2_EN);
2527 ocp_write_word(tp, MCU_TYPE_USB, USB_POWER_CUT, ocp_data);
2528
2529 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_MISC_0);
2530 ocp_data &= ~PCUT_STATUS;
2531 ocp_write_word(tp, MCU_TYPE_USB, USB_MISC_0, ocp_data);
2532}
2533
43779f8d 2534static void r8153_first_init(struct r8152 *tp)
2535{
2536 u32 ocp_data;
2537 int i;
2538
00a5e360 2539 rxdy_gated_en(tp, true);
43779f8d 2540 r8153_teredo_off(tp);
2541
2542 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2543 ocp_data &= ~RCR_ACPT_ALL;
2544 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2545
2546 r8153_hw_phy_cfg(tp);
2547
2548 rtl8152_nic_reset(tp);
2549
2550 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2551 ocp_data &= ~NOW_IS_OOB;
2552 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2553
2554 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2555 ocp_data &= ~MCU_BORW_EN;
2556 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2557
2558 for (i = 0; i < 1000; i++) {
2559 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2560 if (ocp_data & LINK_LIST_READY)
2561 break;
8ddfa077 2562 usleep_range(1000, 2000);
43779f8d 2563 }
2564
2565 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2566 ocp_data |= RE_INIT_LL;
2567 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2568
2569 for (i = 0; i < 1000; i++) {
2570 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2571 if (ocp_data & LINK_LIST_READY)
2572 break;
8ddfa077 2573 usleep_range(1000, 2000);
43779f8d 2574 }
2575
c5554298 2576 rtl_rx_vlan_en(tp, tp->netdev->features & NETIF_F_HW_VLAN_CTAG_RX);
43779f8d 2577
69b4b7a4 2578 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8153_RMS);
2579 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_MTPS, MTPS_JUMBO);
43779f8d 2580
2581 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
2582 ocp_data |= TCR0_AUTO_FIFO;
2583 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
2584
2585 rtl8152_nic_reset(tp);
2586
2587 /* rx share fifo credit full threshold */
2588 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
2589 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_NORMAL);
2590 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_NORMAL);
2591 /* TX share fifo free credit full threshold */
2592 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL2);
2593
9629e3c0 2594 /* rx aggregation */
43779f8d 2595 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
2596 ocp_data &= ~RX_AGG_DISABLE;
2597 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
2598}
2599
2600static void r8153_enter_oob(struct r8152 *tp)
2601{
2602 u32 ocp_data;
2603 int i;
2604
2605 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2606 ocp_data &= ~NOW_IS_OOB;
2607 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2608
d70b1137 2609 rtl_disable(tp);
43779f8d 2610
2611 for (i = 0; i < 1000; i++) {
2612 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2613 if (ocp_data & LINK_LIST_READY)
2614 break;
8ddfa077 2615 usleep_range(1000, 2000);
43779f8d 2616 }
2617
2618 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
2619 ocp_data |= RE_INIT_LL;
2620 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
2621
2622 for (i = 0; i < 1000; i++) {
2623 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2624 if (ocp_data & LINK_LIST_READY)
2625 break;
8ddfa077 2626 usleep_range(1000, 2000);
43779f8d 2627 }
2628
69b4b7a4 2629 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8153_RMS);
43779f8d 2630
43779f8d 2631 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG);
2632 ocp_data &= ~TEREDO_WAKE_MASK;
2633 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TEREDO_CFG, ocp_data);
2634
c5554298 2635 rtl_rx_vlan_en(tp, true);
43779f8d 2636
2637 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
2638 ocp_data |= ALDPS_PROXY_MODE;
2639 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
2640
2641 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
2642 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
2643 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
2644
00a5e360 2645 rxdy_gated_en(tp, false);
43779f8d 2646
2647 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
2648 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
2649 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
2650}
2651
2652static void r8153_disable_aldps(struct r8152 *tp)
2653{
2654 u16 data;
2655
2656 data = ocp_reg_read(tp, OCP_POWER_CFG);
2657 data &= ~EN_ALDPS;
2658 ocp_reg_write(tp, OCP_POWER_CFG, data);
2659 msleep(20);
2660}
2661
2662static void r8153_enable_aldps(struct r8152 *tp)
2663{
2664 u16 data;
2665
2666 data = ocp_reg_read(tp, OCP_POWER_CFG);
2667 data |= EN_ALDPS;
2668 ocp_reg_write(tp, OCP_POWER_CFG, data);
2669}
2670
d70b1137 2671static void rtl8153_disable(struct r8152 *tp)
2672{
2673 r8153_disable_aldps(tp);
2674 rtl_disable(tp);
2675 r8153_enable_aldps(tp);
2676}
2677
ac718b69 2678static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex)
2679{
43779f8d 2680 u16 bmcr, anar, gbcr;
ac718b69 2681 int ret = 0;
2682
2683 cancel_delayed_work_sync(&tp->schedule);
2684 anar = r8152_mdio_read(tp, MII_ADVERTISE);
2685 anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
2686 ADVERTISE_100HALF | ADVERTISE_100FULL);
43779f8d 2687 if (tp->mii.supports_gmii) {
2688 gbcr = r8152_mdio_read(tp, MII_CTRL1000);
2689 gbcr &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
2690 } else {
2691 gbcr = 0;
2692 }
ac718b69 2693
2694 if (autoneg == AUTONEG_DISABLE) {
2695 if (speed == SPEED_10) {
2696 bmcr = 0;
2697 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2698 } else if (speed == SPEED_100) {
2699 bmcr = BMCR_SPEED100;
2700 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
43779f8d 2701 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
2702 bmcr = BMCR_SPEED1000;
2703 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
ac718b69 2704 } else {
2705 ret = -EINVAL;
2706 goto out;
2707 }
2708
2709 if (duplex == DUPLEX_FULL)
2710 bmcr |= BMCR_FULLDPLX;
2711 } else {
2712 if (speed == SPEED_10) {
2713 if (duplex == DUPLEX_FULL)
2714 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2715 else
2716 anar |= ADVERTISE_10HALF;
2717 } else if (speed == SPEED_100) {
2718 if (duplex == DUPLEX_FULL) {
2719 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2720 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
2721 } else {
2722 anar |= ADVERTISE_10HALF;
2723 anar |= ADVERTISE_100HALF;
2724 }
43779f8d 2725 } else if (speed == SPEED_1000 && tp->mii.supports_gmii) {
2726 if (duplex == DUPLEX_FULL) {
2727 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
2728 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
2729 gbcr |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
2730 } else {
2731 anar |= ADVERTISE_10HALF;
2732 anar |= ADVERTISE_100HALF;
2733 gbcr |= ADVERTISE_1000HALF;
2734 }
ac718b69 2735 } else {
2736 ret = -EINVAL;
2737 goto out;
2738 }
2739
2740 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
2741 }
2742
aa66a5f1 2743 if (test_bit(PHY_RESET, &tp->flags))
2744 bmcr |= BMCR_RESET;
2745
43779f8d 2746 if (tp->mii.supports_gmii)
2747 r8152_mdio_write(tp, MII_CTRL1000, gbcr);
2748
ac718b69 2749 r8152_mdio_write(tp, MII_ADVERTISE, anar);
2750 r8152_mdio_write(tp, MII_BMCR, bmcr);
2751
aa66a5f1 2752 if (test_bit(PHY_RESET, &tp->flags)) {
2753 int i;
2754
2755 clear_bit(PHY_RESET, &tp->flags);
2756 for (i = 0; i < 50; i++) {
2757 msleep(20);
2758 if ((r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET) == 0)
2759 break;
2760 }
2761 }
2762
ac718b69 2763out:
ac718b69 2764
2765 return ret;
2766}
2767
d70b1137 2768static void rtl8152_up(struct r8152 *tp)
2769{
2770 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2771 return;
2772
2773 r8152b_disable_aldps(tp);
2774 r8152b_exit_oob(tp);
2775 r8152b_enable_aldps(tp);
2776}
2777
ac718b69 2778static void rtl8152_down(struct r8152 *tp)
2779{
6871438c 2780 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
2781 rtl_drop_queued_tx(tp);
2782 return;
2783 }
2784
00a5e360 2785 r8152_power_cut_en(tp, false);
ac718b69 2786 r8152b_disable_aldps(tp);
2787 r8152b_enter_oob(tp);
2788 r8152b_enable_aldps(tp);
2789}
2790
d70b1137 2791static void rtl8153_up(struct r8152 *tp)
2792{
2793 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2794 return;
2795
2796 r8153_disable_aldps(tp);
2797 r8153_first_init(tp);
2798 r8153_enable_aldps(tp);
2799}
2800
43779f8d 2801static void rtl8153_down(struct r8152 *tp)
2802{
6871438c 2803 if (test_bit(RTL8152_UNPLUG, &tp->flags)) {
2804 rtl_drop_queued_tx(tp);
2805 return;
2806 }
2807
b9702723 2808 r8153_u1u2en(tp, false);
2809 r8153_power_cut_en(tp, false);
43779f8d 2810 r8153_disable_aldps(tp);
2811 r8153_enter_oob(tp);
2812 r8153_enable_aldps(tp);
2813}
2814
ac718b69 2815static void set_carrier(struct r8152 *tp)
2816{
2817 struct net_device *netdev = tp->netdev;
2818 u8 speed;
2819
40a82917 2820 clear_bit(RTL8152_LINK_CHG, &tp->flags);
ac718b69 2821 speed = rtl8152_get_speed(tp);
2822
2823 if (speed & LINK_STATUS) {
2824 if (!(tp->speed & LINK_STATUS)) {
c81229c9 2825 tp->rtl_ops.enable(tp);
ac718b69 2826 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
2827 netif_carrier_on(netdev);
2828 }
2829 } else {
2830 if (tp->speed & LINK_STATUS) {
2831 netif_carrier_off(netdev);
ebc2ec48 2832 tasklet_disable(&tp->tl);
c81229c9 2833 tp->rtl_ops.disable(tp);
ebc2ec48 2834 tasklet_enable(&tp->tl);
ac718b69 2835 }
2836 }
2837 tp->speed = speed;
2838}
2839
2840static void rtl_work_func_t(struct work_struct *work)
2841{
2842 struct r8152 *tp = container_of(work, struct r8152, schedule.work);
2843
9a4be1bd 2844 if (usb_autopm_get_interface(tp->intf) < 0)
2845 return;
2846
ac718b69 2847 if (!test_bit(WORK_ENABLE, &tp->flags))
2848 goto out1;
2849
2850 if (test_bit(RTL8152_UNPLUG, &tp->flags))
2851 goto out1;
2852
40a82917 2853 if (test_bit(RTL8152_LINK_CHG, &tp->flags))
2854 set_carrier(tp);
ac718b69 2855
2856 if (test_bit(RTL8152_SET_RX_MODE, &tp->flags))
2857 _rtl8152_set_rx_mode(tp->netdev);
2858
0c3121fc 2859 if (test_bit(SCHEDULE_TASKLET, &tp->flags) &&
2860 (tp->speed & LINK_STATUS)) {
2861 clear_bit(SCHEDULE_TASKLET, &tp->flags);
2862 tasklet_schedule(&tp->tl);
2863 }
aa66a5f1 2864
2865 if (test_bit(PHY_RESET, &tp->flags))
2866 rtl_phy_reset(tp);
2867
ac718b69 2868out1:
9a4be1bd 2869 usb_autopm_put_interface(tp->intf);
ac718b69 2870}
2871
2872static int rtl8152_open(struct net_device *netdev)
2873{
2874 struct r8152 *tp = netdev_priv(netdev);
2875 int res = 0;
2876
7e9da481 2877 res = alloc_all_mem(tp);
2878 if (res)
2879 goto out;
2880
9a4be1bd 2881 res = usb_autopm_get_interface(tp->intf);
2882 if (res < 0) {
2883 free_all_mem(tp);
2884 goto out;
2885 }
2886
2887 /* The WORK_ENABLE may be set when autoresume occurs */
2888 if (test_bit(WORK_ENABLE, &tp->flags)) {
2889 clear_bit(WORK_ENABLE, &tp->flags);
2890 usb_kill_urb(tp->intr_urb);
2891 cancel_delayed_work_sync(&tp->schedule);
2892 if (tp->speed & LINK_STATUS)
2893 tp->rtl_ops.disable(tp);
2894 }
2895
7e9da481 2896 tp->rtl_ops.up(tp);
2897
3d55f44f 2898 rtl8152_set_speed(tp, AUTONEG_ENABLE,
2899 tp->mii.supports_gmii ? SPEED_1000 : SPEED_100,
2900 DUPLEX_FULL);
2901 tp->speed = 0;
2902 netif_carrier_off(netdev);
2903 netif_start_queue(netdev);
2904 set_bit(WORK_ENABLE, &tp->flags);
db8515ef 2905
40a82917 2906 res = usb_submit_urb(tp->intr_urb, GFP_KERNEL);
2907 if (res) {
2908 if (res == -ENODEV)
2909 netif_device_detach(tp->netdev);
4a8deae2
HW
2910 netif_warn(tp, ifup, netdev, "intr_urb submit failed: %d\n",
2911 res);
7e9da481 2912 free_all_mem(tp);
ac718b69 2913 }
2914
9a4be1bd 2915 usb_autopm_put_interface(tp->intf);
ac718b69 2916
7e9da481 2917out:
ac718b69 2918 return res;
2919}
2920
2921static int rtl8152_close(struct net_device *netdev)
2922{
2923 struct r8152 *tp = netdev_priv(netdev);
2924 int res = 0;
2925
2926 clear_bit(WORK_ENABLE, &tp->flags);
3d55f44f 2927 usb_kill_urb(tp->intr_urb);
ac718b69 2928 cancel_delayed_work_sync(&tp->schedule);
2929 netif_stop_queue(netdev);
9a4be1bd 2930
2931 res = usb_autopm_get_interface(tp->intf);
2932 if (res < 0) {
2933 rtl_drop_queued_tx(tp);
2934 } else {
b209af99 2935 /* The autosuspend may have been enabled and wouldn't
9a4be1bd 2936 * be disable when autoresume occurs, because the
2937 * netif_running() would be false.
2938 */
2939 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
2940 rtl_runtime_suspend_enable(tp, false);
2941 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
2942 }
2943
2944 tasklet_disable(&tp->tl);
2945 tp->rtl_ops.down(tp);
2946 tasklet_enable(&tp->tl);
2947 usb_autopm_put_interface(tp->intf);
2948 }
ac718b69 2949
7e9da481 2950 free_all_mem(tp);
2951
ac718b69 2952 return res;
2953}
2954
d24f6134 2955static inline void r8152_mmd_indirect(struct r8152 *tp, u16 dev, u16 reg)
2956{
2957 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | dev);
2958 ocp_reg_write(tp, OCP_EEE_DATA, reg);
2959 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | dev);
2960}
2961
2962static u16 r8152_mmd_read(struct r8152 *tp, u16 dev, u16 reg)
2963{
2964 u16 data;
2965
2966 r8152_mmd_indirect(tp, dev, reg);
2967 data = ocp_reg_read(tp, OCP_EEE_DATA);
2968 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
2969
2970 return data;
2971}
2972
2973static void r8152_mmd_write(struct r8152 *tp, u16 dev, u16 reg, u16 data)
ac718b69 2974{
d24f6134 2975 r8152_mmd_indirect(tp, dev, reg);
2976 ocp_reg_write(tp, OCP_EEE_DATA, data);
2977 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
2978}
2979
2980static void r8152_eee_en(struct r8152 *tp, bool enable)
2981{
2982 u16 config1, config2, config3;
45f4a19f 2983 u32 ocp_data;
ac718b69 2984
2985 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
d24f6134 2986 config1 = ocp_reg_read(tp, OCP_EEE_CONFIG1) & ~sd_rise_time_mask;
2987 config2 = ocp_reg_read(tp, OCP_EEE_CONFIG2);
2988 config3 = ocp_reg_read(tp, OCP_EEE_CONFIG3) & ~fast_snr_mask;
2989
2990 if (enable) {
2991 ocp_data |= EEE_RX_EN | EEE_TX_EN;
2992 config1 |= EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN | RX_QUIET_EN;
2993 config1 |= sd_rise_time(1);
2994 config2 |= RG_DACQUIET_EN | RG_LDVQUIET_EN;
2995 config3 |= fast_snr(42);
2996 } else {
2997 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
2998 config1 &= ~(EEE_10_CAP | EEE_NWAY_EN | TX_QUIET_EN |
2999 RX_QUIET_EN);
3000 config1 |= sd_rise_time(7);
3001 config2 &= ~(RG_DACQUIET_EN | RG_LDVQUIET_EN);
3002 config3 |= fast_snr(511);
3003 }
3004
ac718b69 3005 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
d24f6134 3006 ocp_reg_write(tp, OCP_EEE_CONFIG1, config1);
3007 ocp_reg_write(tp, OCP_EEE_CONFIG2, config2);
3008 ocp_reg_write(tp, OCP_EEE_CONFIG3, config3);
ac718b69 3009}
3010
d24f6134 3011static void r8152b_enable_eee(struct r8152 *tp)
3012{
3013 r8152_eee_en(tp, true);
3014 r8152_mmd_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, MDIO_EEE_100TX);
3015}
3016
3017static void r8153_eee_en(struct r8152 *tp, bool enable)
43779f8d 3018{
3019 u32 ocp_data;
d24f6134 3020 u16 config;
43779f8d 3021
3022 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
d24f6134 3023 config = ocp_reg_read(tp, OCP_EEE_CFG);
3024
3025 if (enable) {
3026 ocp_data |= EEE_RX_EN | EEE_TX_EN;
3027 config |= EEE10_EN;
3028 } else {
3029 ocp_data &= ~(EEE_RX_EN | EEE_TX_EN);
3030 config &= ~EEE10_EN;
3031 }
3032
43779f8d 3033 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
d24f6134 3034 ocp_reg_write(tp, OCP_EEE_CFG, config);
3035}
3036
3037static void r8153_enable_eee(struct r8152 *tp)
3038{
3039 r8153_eee_en(tp, true);
3040 ocp_reg_write(tp, OCP_EEE_ADV, MDIO_EEE_1000T | MDIO_EEE_100TX);
43779f8d 3041}
3042
ac718b69 3043static void r8152b_enable_fc(struct r8152 *tp)
3044{
3045 u16 anar;
3046
3047 anar = r8152_mdio_read(tp, MII_ADVERTISE);
3048 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
3049 r8152_mdio_write(tp, MII_ADVERTISE, anar);
3050}
3051
4f1d4d54 3052static void rtl_tally_reset(struct r8152 *tp)
3053{
3054 u32 ocp_data;
3055
3056 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY);
3057 ocp_data |= TALLY_RESET;
3058 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RSTTALLY, ocp_data);
3059}
3060
ac718b69 3061static void r8152b_init(struct r8152 *tp)
3062{
ebc2ec48 3063 u32 ocp_data;
ac718b69 3064
6871438c 3065 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3066 return;
3067
d70b1137 3068 r8152b_disable_aldps(tp);
3069
ac718b69 3070 if (tp->version == RTL_VER_01) {
3071 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
3072 ocp_data &= ~LED_MODE_MASK;
3073 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
3074 }
3075
00a5e360 3076 r8152_power_cut_en(tp, false);
ac718b69 3077
ac718b69 3078 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
3079 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
3080 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
3081 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
3082 ocp_data &= ~MCU_CLK_RATIO_MASK;
3083 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
3084 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
3085 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
3086 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
3087 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
3088
3089 r8152b_enable_eee(tp);
3090 r8152b_enable_aldps(tp);
3091 r8152b_enable_fc(tp);
4f1d4d54 3092 rtl_tally_reset(tp);
ac718b69 3093
ebc2ec48 3094 /* enable rx aggregation */
ac718b69 3095 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
ebc2ec48 3096 ocp_data &= ~RX_AGG_DISABLE;
ac718b69 3097 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
3098}
3099
43779f8d 3100static void r8153_init(struct r8152 *tp)
3101{
3102 u32 ocp_data;
3103 int i;
3104
6871438c 3105 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3106 return;
3107
d70b1137 3108 r8153_disable_aldps(tp);
b9702723 3109 r8153_u1u2en(tp, false);
43779f8d 3110
3111 for (i = 0; i < 500; i++) {
3112 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_BOOT_CTRL) &
3113 AUTOLOAD_DONE)
3114 break;
3115 msleep(20);
3116 }
3117
3118 for (i = 0; i < 500; i++) {
3119 ocp_data = ocp_reg_read(tp, OCP_PHY_STATUS) & PHY_STAT_MASK;
3120 if (ocp_data == PHY_STAT_LAN_ON || ocp_data == PHY_STAT_PWRDN)
3121 break;
3122 msleep(20);
3123 }
3124
b9702723 3125 r8153_u2p3en(tp, false);
43779f8d 3126
3127 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL);
3128 ocp_data &= ~TIMER11_EN;
3129 ocp_write_word(tp, MCU_TYPE_USB, USB_WDT11_CTRL, ocp_data);
3130
43779f8d 3131 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
3132 ocp_data &= ~LED_MODE_MASK;
3133 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
3134
3135 ocp_data = ocp_read_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL);
3136 ocp_data &= ~LPM_TIMER_MASK;
3137 if (tp->udev->speed == USB_SPEED_SUPER)
3138 ocp_data |= LPM_TIMER_500US;
3139 else
3140 ocp_data |= LPM_TIMER_500MS;
3141 ocp_write_byte(tp, MCU_TYPE_USB, USB_LPM_CTRL, ocp_data);
3142
3143 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2);
3144 ocp_data &= ~SEN_VAL_MASK;
3145 ocp_data |= SEN_VAL_NORMAL | SEL_RXIDLE;
3146 ocp_write_word(tp, MCU_TYPE_USB, USB_AFE_CTRL2, ocp_data);
3147
b9702723 3148 r8153_power_cut_en(tp, false);
3149 r8153_u1u2en(tp, true);
43779f8d 3150
43779f8d 3151 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ALDPS_SPDWN_RATIO);
3152 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL2, EEE_SPDWN_RATIO);
3153 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL3,
3154 PKT_AVAIL_SPDWN_EN | SUSPEND_SPDWN_EN |
3155 U1U2_SPDWN_EN | L1_SPDWN_EN);
3156 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL4,
3157 PWRSAVE_SPDWN_EN | RXDV_SPDWN_EN | TX10MIDLE_EN |
3158 TP100_SPDWN_EN | TP500_SPDWN_EN | TP1000_SPDWN_EN |
3159 EEE_SPDWN_EN);
3160
3161 r8153_enable_eee(tp);
3162 r8153_enable_aldps(tp);
3163 r8152b_enable_fc(tp);
4f1d4d54 3164 rtl_tally_reset(tp);
43779f8d 3165}
3166
ac718b69 3167static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
3168{
3169 struct r8152 *tp = usb_get_intfdata(intf);
3170
9a4be1bd 3171 if (PMSG_IS_AUTO(message))
3172 set_bit(SELECTIVE_SUSPEND, &tp->flags);
3173 else
3174 netif_device_detach(tp->netdev);
ac718b69 3175
3176 if (netif_running(tp->netdev)) {
3177 clear_bit(WORK_ENABLE, &tp->flags);
40a82917 3178 usb_kill_urb(tp->intr_urb);
ac718b69 3179 cancel_delayed_work_sync(&tp->schedule);
9a4be1bd 3180 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
3181 rtl_runtime_suspend_enable(tp, true);
3182 } else {
3183 tasklet_disable(&tp->tl);
3184 tp->rtl_ops.down(tp);
3185 tasklet_enable(&tp->tl);
3186 }
ac718b69 3187 }
3188
ac718b69 3189 return 0;
3190}
3191
3192static int rtl8152_resume(struct usb_interface *intf)
3193{
3194 struct r8152 *tp = usb_get_intfdata(intf);
3195
9a4be1bd 3196 if (!test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
3197 tp->rtl_ops.init(tp);
3198 netif_device_attach(tp->netdev);
3199 }
3200
ac718b69 3201 if (netif_running(tp->netdev)) {
9a4be1bd 3202 if (test_bit(SELECTIVE_SUSPEND, &tp->flags)) {
3203 rtl_runtime_suspend_enable(tp, false);
3204 clear_bit(SELECTIVE_SUSPEND, &tp->flags);
3205 if (tp->speed & LINK_STATUS)
3206 tp->rtl_ops.disable(tp);
3207 } else {
3208 tp->rtl_ops.up(tp);
3209 rtl8152_set_speed(tp, AUTONEG_ENABLE,
b209af99 3210 tp->mii.supports_gmii ?
3211 SPEED_1000 : SPEED_100,
3212 DUPLEX_FULL);
9a4be1bd 3213 }
40a82917 3214 tp->speed = 0;
3215 netif_carrier_off(tp->netdev);
ac718b69 3216 set_bit(WORK_ENABLE, &tp->flags);
40a82917 3217 usb_submit_urb(tp->intr_urb, GFP_KERNEL);
ac718b69 3218 }
3219
3220 return 0;
3221}
3222
21ff2e89 3223static void rtl8152_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
3224{
3225 struct r8152 *tp = netdev_priv(dev);
3226
9a4be1bd 3227 if (usb_autopm_get_interface(tp->intf) < 0)
3228 return;
3229
21ff2e89 3230 wol->supported = WAKE_ANY;
3231 wol->wolopts = __rtl_get_wol(tp);
9a4be1bd 3232
3233 usb_autopm_put_interface(tp->intf);
21ff2e89 3234}
3235
3236static int rtl8152_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
3237{
3238 struct r8152 *tp = netdev_priv(dev);
9a4be1bd 3239 int ret;
3240
3241 ret = usb_autopm_get_interface(tp->intf);
3242 if (ret < 0)
3243 goto out_set_wol;
21ff2e89 3244
3245 __rtl_set_wol(tp, wol->wolopts);
3246 tp->saved_wolopts = wol->wolopts & WAKE_ANY;
3247
9a4be1bd 3248 usb_autopm_put_interface(tp->intf);
3249
3250out_set_wol:
3251 return ret;
21ff2e89 3252}
3253
a5ec27c1 3254static u32 rtl8152_get_msglevel(struct net_device *dev)
3255{
3256 struct r8152 *tp = netdev_priv(dev);
3257
3258 return tp->msg_enable;
3259}
3260
3261static void rtl8152_set_msglevel(struct net_device *dev, u32 value)
3262{
3263 struct r8152 *tp = netdev_priv(dev);
3264
3265 tp->msg_enable = value;
3266}
3267
ac718b69 3268static void rtl8152_get_drvinfo(struct net_device *netdev,
3269 struct ethtool_drvinfo *info)
3270{
3271 struct r8152 *tp = netdev_priv(netdev);
3272
b0b46c77 3273 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
3274 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
ac718b69 3275 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
3276}
3277
3278static
3279int rtl8152_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
3280{
3281 struct r8152 *tp = netdev_priv(netdev);
3282
3283 if (!tp->mii.mdio_read)
3284 return -EOPNOTSUPP;
3285
3286 return mii_ethtool_gset(&tp->mii, cmd);
3287}
3288
3289static int rtl8152_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
3290{
3291 struct r8152 *tp = netdev_priv(dev);
9a4be1bd 3292 int ret;
3293
3294 ret = usb_autopm_get_interface(tp->intf);
3295 if (ret < 0)
3296 goto out;
ac718b69 3297
9a4be1bd 3298 ret = rtl8152_set_speed(tp, cmd->autoneg, cmd->speed, cmd->duplex);
3299
3300 usb_autopm_put_interface(tp->intf);
3301
3302out:
3303 return ret;
ac718b69 3304}
3305
4f1d4d54 3306static const char rtl8152_gstrings[][ETH_GSTRING_LEN] = {
3307 "tx_packets",
3308 "rx_packets",
3309 "tx_errors",
3310 "rx_errors",
3311 "rx_missed",
3312 "align_errors",
3313 "tx_single_collisions",
3314 "tx_multi_collisions",
3315 "rx_unicast",
3316 "rx_broadcast",
3317 "rx_multicast",
3318 "tx_aborted",
3319 "tx_underrun",
3320};
3321
3322static int rtl8152_get_sset_count(struct net_device *dev, int sset)
3323{
3324 switch (sset) {
3325 case ETH_SS_STATS:
3326 return ARRAY_SIZE(rtl8152_gstrings);
3327 default:
3328 return -EOPNOTSUPP;
3329 }
3330}
3331
3332static void rtl8152_get_ethtool_stats(struct net_device *dev,
3333 struct ethtool_stats *stats, u64 *data)
3334{
3335 struct r8152 *tp = netdev_priv(dev);
3336 struct tally_counter tally;
3337
0b030244 3338 if (usb_autopm_get_interface(tp->intf) < 0)
3339 return;
3340
4f1d4d54 3341 generic_ocp_read(tp, PLA_TALLYCNT, sizeof(tally), &tally, MCU_TYPE_PLA);
3342
0b030244 3343 usb_autopm_put_interface(tp->intf);
3344
4f1d4d54 3345 data[0] = le64_to_cpu(tally.tx_packets);
3346 data[1] = le64_to_cpu(tally.rx_packets);
3347 data[2] = le64_to_cpu(tally.tx_errors);
3348 data[3] = le32_to_cpu(tally.rx_errors);
3349 data[4] = le16_to_cpu(tally.rx_missed);
3350 data[5] = le16_to_cpu(tally.align_errors);
3351 data[6] = le32_to_cpu(tally.tx_one_collision);
3352 data[7] = le32_to_cpu(tally.tx_multi_collision);
3353 data[8] = le64_to_cpu(tally.rx_unicast);
3354 data[9] = le64_to_cpu(tally.rx_broadcast);
3355 data[10] = le32_to_cpu(tally.rx_multicast);
3356 data[11] = le16_to_cpu(tally.tx_aborted);
3357 data[12] = le16_to_cpu(tally.tx_underun);
3358}
3359
3360static void rtl8152_get_strings(struct net_device *dev, u32 stringset, u8 *data)
3361{
3362 switch (stringset) {
3363 case ETH_SS_STATS:
3364 memcpy(data, *rtl8152_gstrings, sizeof(rtl8152_gstrings));
3365 break;
3366 }
3367}
3368
ac718b69 3369static struct ethtool_ops ops = {
3370 .get_drvinfo = rtl8152_get_drvinfo,
3371 .get_settings = rtl8152_get_settings,
3372 .set_settings = rtl8152_set_settings,
3373 .get_link = ethtool_op_get_link,
a5ec27c1 3374 .get_msglevel = rtl8152_get_msglevel,
3375 .set_msglevel = rtl8152_set_msglevel,
21ff2e89 3376 .get_wol = rtl8152_get_wol,
3377 .set_wol = rtl8152_set_wol,
4f1d4d54 3378 .get_strings = rtl8152_get_strings,
3379 .get_sset_count = rtl8152_get_sset_count,
3380 .get_ethtool_stats = rtl8152_get_ethtool_stats,
ac718b69 3381};
3382
3383static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
3384{
3385 struct r8152 *tp = netdev_priv(netdev);
3386 struct mii_ioctl_data *data = if_mii(rq);
9a4be1bd 3387 int res;
3388
6871438c 3389 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3390 return -ENODEV;
3391
9a4be1bd 3392 res = usb_autopm_get_interface(tp->intf);
3393 if (res < 0)
3394 goto out;
ac718b69 3395
3396 switch (cmd) {
3397 case SIOCGMIIPHY:
3398 data->phy_id = R8152_PHY_ID; /* Internal PHY */
3399 break;
3400
3401 case SIOCGMIIREG:
3402 data->val_out = r8152_mdio_read(tp, data->reg_num);
3403 break;
3404
3405 case SIOCSMIIREG:
3406 if (!capable(CAP_NET_ADMIN)) {
3407 res = -EPERM;
3408 break;
3409 }
3410 r8152_mdio_write(tp, data->reg_num, data->val_in);
3411 break;
3412
3413 default:
3414 res = -EOPNOTSUPP;
3415 }
3416
9a4be1bd 3417 usb_autopm_put_interface(tp->intf);
3418
3419out:
ac718b69 3420 return res;
3421}
3422
69b4b7a4 3423static int rtl8152_change_mtu(struct net_device *dev, int new_mtu)
3424{
3425 struct r8152 *tp = netdev_priv(dev);
3426
3427 switch (tp->version) {
3428 case RTL_VER_01:
3429 case RTL_VER_02:
3430 return eth_change_mtu(dev, new_mtu);
3431 default:
3432 break;
3433 }
3434
3435 if (new_mtu < 68 || new_mtu > RTL8153_MAX_MTU)
3436 return -EINVAL;
3437
3438 dev->mtu = new_mtu;
3439
3440 return 0;
3441}
3442
ac718b69 3443static const struct net_device_ops rtl8152_netdev_ops = {
3444 .ndo_open = rtl8152_open,
3445 .ndo_stop = rtl8152_close,
3446 .ndo_do_ioctl = rtl8152_ioctl,
3447 .ndo_start_xmit = rtl8152_start_xmit,
3448 .ndo_tx_timeout = rtl8152_tx_timeout,
c5554298 3449 .ndo_set_features = rtl8152_set_features,
ac718b69 3450 .ndo_set_rx_mode = rtl8152_set_rx_mode,
3451 .ndo_set_mac_address = rtl8152_set_mac_address,
69b4b7a4 3452 .ndo_change_mtu = rtl8152_change_mtu,
ac718b69 3453 .ndo_validate_addr = eth_validate_addr,
3454};
3455
3456static void r8152b_get_version(struct r8152 *tp)
3457{
3458 u32 ocp_data;
3459 u16 version;
3460
3461 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR1);
3462 version = (u16)(ocp_data & VERSION_MASK);
3463
3464 switch (version) {
3465 case 0x4c00:
3466 tp->version = RTL_VER_01;
3467 break;
3468 case 0x4c10:
3469 tp->version = RTL_VER_02;
3470 break;
43779f8d 3471 case 0x5c00:
3472 tp->version = RTL_VER_03;
3473 tp->mii.supports_gmii = 1;
3474 break;
3475 case 0x5c10:
3476 tp->version = RTL_VER_04;
3477 tp->mii.supports_gmii = 1;
3478 break;
3479 case 0x5c20:
3480 tp->version = RTL_VER_05;
3481 tp->mii.supports_gmii = 1;
3482 break;
ac718b69 3483 default:
3484 netif_info(tp, probe, tp->netdev,
3485 "Unknown version 0x%04x\n", version);
3486 break;
3487 }
3488}
3489
e3fe0b1a 3490static void rtl8152_unload(struct r8152 *tp)
3491{
6871438c 3492 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3493 return;
3494
00a5e360 3495 if (tp->version != RTL_VER_01)
3496 r8152_power_cut_en(tp, true);
e3fe0b1a 3497}
3498
43779f8d 3499static void rtl8153_unload(struct r8152 *tp)
3500{
6871438c 3501 if (test_bit(RTL8152_UNPLUG, &tp->flags))
3502 return;
3503
b9702723 3504 r8153_power_cut_en(tp, true);
43779f8d 3505}
3506
31ca1dec 3507static int rtl_ops_init(struct r8152 *tp, const struct usb_device_id *id)
c81229c9 3508{
3509 struct rtl_ops *ops = &tp->rtl_ops;
31ca1dec 3510 int ret = -ENODEV;
c81229c9 3511
3512 switch (id->idVendor) {
3513 case VENDOR_ID_REALTEK:
3514 switch (id->idProduct) {
3515 case PRODUCT_ID_RTL8152:
3516 ops->init = r8152b_init;
3517 ops->enable = rtl8152_enable;
3518 ops->disable = rtl8152_disable;
d70b1137 3519 ops->up = rtl8152_up;
c81229c9 3520 ops->down = rtl8152_down;
3521 ops->unload = rtl8152_unload;
31ca1dec 3522 ret = 0;
c81229c9 3523 break;
43779f8d 3524 case PRODUCT_ID_RTL8153:
3525 ops->init = r8153_init;
3526 ops->enable = rtl8153_enable;
d70b1137 3527 ops->disable = rtl8153_disable;
3528 ops->up = rtl8153_up;
43779f8d 3529 ops->down = rtl8153_down;
3530 ops->unload = rtl8153_unload;
31ca1dec 3531 ret = 0;
43779f8d 3532 break;
3533 default:
43779f8d 3534 break;
3535 }
3536 break;
3537
3538 case VENDOR_ID_SAMSUNG:
3539 switch (id->idProduct) {
3540 case PRODUCT_ID_SAMSUNG:
3541 ops->init = r8153_init;
3542 ops->enable = rtl8153_enable;
d70b1137 3543 ops->disable = rtl8153_disable;
3544 ops->up = rtl8153_up;
43779f8d 3545 ops->down = rtl8153_down;
3546 ops->unload = rtl8153_unload;
31ca1dec 3547 ret = 0;
43779f8d 3548 break;
c81229c9 3549 default:
c81229c9 3550 break;
3551 }
3552 break;
3553
3554 default:
c81229c9 3555 break;
3556 }
3557
31ca1dec 3558 if (ret)
3559 netif_err(tp, probe, tp->netdev, "Unknown Device\n");
3560
c81229c9 3561 return ret;
3562}
3563
ac718b69 3564static int rtl8152_probe(struct usb_interface *intf,
3565 const struct usb_device_id *id)
3566{
3567 struct usb_device *udev = interface_to_usbdev(intf);
3568 struct r8152 *tp;
3569 struct net_device *netdev;
ebc2ec48 3570 int ret;
ac718b69 3571
10c32717 3572 if (udev->actconfig->desc.bConfigurationValue != 1) {
3573 usb_driver_set_configuration(udev, 1);
3574 return -ENODEV;
3575 }
3576
3577 usb_reset_device(udev);
ac718b69 3578 netdev = alloc_etherdev(sizeof(struct r8152));
3579 if (!netdev) {
4a8deae2 3580 dev_err(&intf->dev, "Out of memory\n");
ac718b69 3581 return -ENOMEM;
3582 }
3583
ebc2ec48 3584 SET_NETDEV_DEV(netdev, &intf->dev);
ac718b69 3585 tp = netdev_priv(netdev);
3586 tp->msg_enable = 0x7FFF;
3587
e3ad412a 3588 tp->udev = udev;
3589 tp->netdev = netdev;
3590 tp->intf = intf;
3591
31ca1dec 3592 ret = rtl_ops_init(tp, id);
3593 if (ret)
3594 goto out;
c81229c9 3595
ebc2ec48 3596 tasklet_init(&tp->tl, bottom_half, (unsigned long)tp);
ac718b69 3597 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
3598
ac718b69 3599 netdev->netdev_ops = &rtl8152_netdev_ops;
3600 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
5bd23881 3601
60c89071 3602 netdev->features |= NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
6128d1bb 3603 NETIF_F_TSO | NETIF_F_FRAGLIST | NETIF_F_IPV6_CSUM |
c5554298 3604 NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_RX |
3605 NETIF_F_HW_VLAN_CTAG_TX;
60c89071 3606 netdev->hw_features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
6128d1bb 3607 NETIF_F_TSO | NETIF_F_FRAGLIST |
c5554298 3608 NETIF_F_IPV6_CSUM | NETIF_F_TSO6 |
3609 NETIF_F_HW_VLAN_CTAG_RX |
3610 NETIF_F_HW_VLAN_CTAG_TX;
3611 netdev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
3612 NETIF_F_HIGHDMA | NETIF_F_FRAGLIST |
3613 NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
db8515ef 3614
7ad24ea4 3615 netdev->ethtool_ops = &ops;
60c89071 3616 netif_set_gso_max_size(netdev, RTL_LIMITED_TSO_SIZE);
ac718b69 3617
3618 tp->mii.dev = netdev;
3619 tp->mii.mdio_read = read_mii_word;
3620 tp->mii.mdio_write = write_mii_word;
3621 tp->mii.phy_id_mask = 0x3f;
3622 tp->mii.reg_num_mask = 0x1f;
3623 tp->mii.phy_id = R8152_PHY_ID;
3624 tp->mii.supports_gmii = 0;
3625
9a4be1bd 3626 intf->needs_remote_wakeup = 1;
3627
ac718b69 3628 r8152b_get_version(tp);
c81229c9 3629 tp->rtl_ops.init(tp);
ac718b69 3630 set_ethernet_addr(tp);
3631
ac718b69 3632 usb_set_intfdata(intf, tp);
ac718b69 3633
ebc2ec48 3634 ret = register_netdev(netdev);
3635 if (ret != 0) {
4a8deae2 3636 netif_err(tp, probe, netdev, "couldn't register the device\n");
ebc2ec48 3637 goto out1;
ac718b69 3638 }
3639
21ff2e89 3640 tp->saved_wolopts = __rtl_get_wol(tp);
3641 if (tp->saved_wolopts)
3642 device_set_wakeup_enable(&udev->dev, true);
3643 else
3644 device_set_wakeup_enable(&udev->dev, false);
3645
4a8deae2 3646 netif_info(tp, probe, netdev, "%s\n", DRIVER_VERSION);
ac718b69 3647
3648 return 0;
3649
ac718b69 3650out1:
ebc2ec48 3651 usb_set_intfdata(intf, NULL);
ac718b69 3652out:
3653 free_netdev(netdev);
ebc2ec48 3654 return ret;
ac718b69 3655}
3656
ac718b69 3657static void rtl8152_disconnect(struct usb_interface *intf)
3658{
3659 struct r8152 *tp = usb_get_intfdata(intf);
3660
3661 usb_set_intfdata(intf, NULL);
3662 if (tp) {
3663 set_bit(RTL8152_UNPLUG, &tp->flags);
3664 tasklet_kill(&tp->tl);
3665 unregister_netdev(tp->netdev);
c81229c9 3666 tp->rtl_ops.unload(tp);
ac718b69 3667 free_netdev(tp->netdev);
3668 }
3669}
3670
3671/* table of devices that work with this driver */
3672static struct usb_device_id rtl8152_table[] = {
10c32717 3673 {USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8152)},
3674 {USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8153)},
3675 {USB_DEVICE(VENDOR_ID_SAMSUNG, PRODUCT_ID_SAMSUNG)},
ac718b69 3676 {}
3677};
3678
3679MODULE_DEVICE_TABLE(usb, rtl8152_table);
3680
3681static struct usb_driver rtl8152_driver = {
3682 .name = MODULENAME,
ebc2ec48 3683 .id_table = rtl8152_table,
ac718b69 3684 .probe = rtl8152_probe,
3685 .disconnect = rtl8152_disconnect,
ac718b69 3686 .suspend = rtl8152_suspend,
ebc2ec48 3687 .resume = rtl8152_resume,
3688 .reset_resume = rtl8152_resume,
9a4be1bd 3689 .supports_autosuspend = 1,
a634782f 3690 .disable_hub_initiated_lpm = 1,
ac718b69 3691};
3692
b4236daa 3693module_usb_driver(rtl8152_driver);
ac718b69 3694
3695MODULE_AUTHOR(DRIVER_AUTHOR);
3696MODULE_DESCRIPTION(DRIVER_DESC);
3697MODULE_LICENSE("GPL");