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