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