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