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