blob: 4d938a7fd5f2393dcc355b6b9dd0d75b50388dec [file] [log] [blame]
hayeswangac718b62013-05-02 16:01:25 +00001/*
2 * Copyright (c) 2013 Realtek Semiconductor Corp. All rights reserved.
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
10#include <linux/init.h>
11#include <linux/signal.h>
12#include <linux/slab.h>
13#include <linux/module.h>
hayeswangac718b62013-05-02 16:01:25 +000014#include <linux/netdevice.h>
15#include <linux/etherdevice.h>
16#include <linux/mii.h>
17#include <linux/ethtool.h>
18#include <linux/usb.h>
19#include <linux/crc32.h>
20#include <linux/if_vlan.h>
21#include <linux/uaccess.h>
hayeswangebc2ec482013-08-14 20:54:38 +080022#include <linux/list.h>
hayeswang5bd23882013-08-14 20:54:39 +080023#include <linux/ip.h>
24#include <linux/ipv6.h>
hayeswangac718b62013-05-02 16:01:25 +000025
26/* Version Information */
hayeswangebc2ec482013-08-14 20:54:38 +080027#define DRIVER_VERSION "v1.01.0 (2013/08/12)"
hayeswangac718b62013-05-02 16:01:25 +000028#define DRIVER_AUTHOR "Realtek linux nic maintainers <nic_swsd@realtek.com>"
29#define DRIVER_DESC "Realtek RTL8152 Based USB 2.0 Ethernet Adapters"
30#define MODULENAME "r8152"
31
32#define R8152_PHY_ID 32
33
34#define PLA_IDR 0xc000
35#define PLA_RCR 0xc010
36#define PLA_RMS 0xc016
37#define PLA_RXFIFO_CTRL0 0xc0a0
38#define PLA_RXFIFO_CTRL1 0xc0a4
39#define PLA_RXFIFO_CTRL2 0xc0a8
40#define PLA_FMC 0xc0b4
41#define PLA_CFG_WOL 0xc0b6
42#define PLA_MAR 0xcd00
43#define PAL_BDC_CR 0xd1a0
44#define PLA_LEDSEL 0xdd90
45#define PLA_LED_FEATURE 0xdd92
46#define PLA_PHYAR 0xde00
47#define PLA_GPHY_INTR_IMR 0xe022
48#define PLA_EEE_CR 0xe040
49#define PLA_EEEP_CR 0xe080
50#define PLA_MAC_PWR_CTRL 0xe0c0
51#define PLA_TCR0 0xe610
52#define PLA_TCR1 0xe612
53#define PLA_TXFIFO_CTRL 0xe618
54#define PLA_RSTTELLY 0xe800
55#define PLA_CR 0xe813
56#define PLA_CRWECR 0xe81c
57#define PLA_CONFIG5 0xe822
58#define PLA_PHY_PWR 0xe84c
59#define PLA_OOB_CTRL 0xe84f
60#define PLA_CPCR 0xe854
61#define PLA_MISC_0 0xe858
62#define PLA_MISC_1 0xe85a
63#define PLA_OCP_GPHY_BASE 0xe86c
64#define PLA_TELLYCNT 0xe890
65#define PLA_SFF_STS_7 0xe8de
66#define PLA_PHYSTATUS 0xe908
67#define PLA_BP_BA 0xfc26
68#define PLA_BP_0 0xfc28
69#define PLA_BP_1 0xfc2a
70#define PLA_BP_2 0xfc2c
71#define PLA_BP_3 0xfc2e
72#define PLA_BP_4 0xfc30
73#define PLA_BP_5 0xfc32
74#define PLA_BP_6 0xfc34
75#define PLA_BP_7 0xfc36
76
77#define USB_DEV_STAT 0xb808
78#define USB_USB_CTRL 0xd406
79#define USB_PHY_CTRL 0xd408
80#define USB_TX_AGG 0xd40a
81#define USB_RX_BUF_TH 0xd40c
82#define USB_USB_TIMER 0xd428
83#define USB_PM_CTRL_STATUS 0xd432
84#define USB_TX_DMA 0xd434
85#define USB_UPS_CTRL 0xd800
86#define USB_BP_BA 0xfc26
87#define USB_BP_0 0xfc28
88#define USB_BP_1 0xfc2a
89#define USB_BP_2 0xfc2c
90#define USB_BP_3 0xfc2e
91#define USB_BP_4 0xfc30
92#define USB_BP_5 0xfc32
93#define USB_BP_6 0xfc34
94#define USB_BP_7 0xfc36
95
96/* OCP Registers */
97#define OCP_ALDPS_CONFIG 0x2010
98#define OCP_EEE_CONFIG1 0x2080
99#define OCP_EEE_CONFIG2 0x2092
100#define OCP_EEE_CONFIG3 0x2094
101#define OCP_EEE_AR 0xa41a
102#define OCP_EEE_DATA 0xa41c
103
104/* PLA_RCR */
105#define RCR_AAP 0x00000001
106#define RCR_APM 0x00000002
107#define RCR_AM 0x00000004
108#define RCR_AB 0x00000008
109#define RCR_ACPT_ALL (RCR_AAP | RCR_APM | RCR_AM | RCR_AB)
110
111/* PLA_RXFIFO_CTRL0 */
112#define RXFIFO_THR1_NORMAL 0x00080002
113#define RXFIFO_THR1_OOB 0x01800003
114
115/* PLA_RXFIFO_CTRL1 */
116#define RXFIFO_THR2_FULL 0x00000060
117#define RXFIFO_THR2_HIGH 0x00000038
118#define RXFIFO_THR2_OOB 0x0000004a
119
120/* PLA_RXFIFO_CTRL2 */
121#define RXFIFO_THR3_FULL 0x00000078
122#define RXFIFO_THR3_HIGH 0x00000048
123#define RXFIFO_THR3_OOB 0x0000005a
124
125/* PLA_TXFIFO_CTRL */
126#define TXFIFO_THR_NORMAL 0x00400008
127
128/* PLA_FMC */
129#define FMC_FCR_MCU_EN 0x0001
130
131/* PLA_EEEP_CR */
132#define EEEP_CR_EEEP_TX 0x0002
133
134/* PLA_TCR0 */
135#define TCR0_TX_EMPTY 0x0800
136#define TCR0_AUTO_FIFO 0x0080
137
138/* PLA_TCR1 */
139#define VERSION_MASK 0x7cf0
140
141/* PLA_CR */
142#define CR_RST 0x10
143#define CR_RE 0x08
144#define CR_TE 0x04
145
146/* PLA_CRWECR */
147#define CRWECR_NORAML 0x00
148#define CRWECR_CONFIG 0xc0
149
150/* PLA_OOB_CTRL */
151#define NOW_IS_OOB 0x80
152#define TXFIFO_EMPTY 0x20
153#define RXFIFO_EMPTY 0x10
154#define LINK_LIST_READY 0x02
155#define DIS_MCU_CLROOB 0x01
156#define FIFO_EMPTY (TXFIFO_EMPTY | RXFIFO_EMPTY)
157
158/* PLA_MISC_1 */
159#define RXDY_GATED_EN 0x0008
160
161/* PLA_SFF_STS_7 */
162#define RE_INIT_LL 0x8000
163#define MCU_BORW_EN 0x4000
164
165/* PLA_CPCR */
166#define CPCR_RX_VLAN 0x0040
167
168/* PLA_CFG_WOL */
169#define MAGIC_EN 0x0001
170
171/* PAL_BDC_CR */
172#define ALDPS_PROXY_MODE 0x0001
173
174/* PLA_CONFIG5 */
175#define LAN_WAKE_EN 0x0002
176
177/* PLA_LED_FEATURE */
178#define LED_MODE_MASK 0x0700
179
180/* PLA_PHY_PWR */
181#define TX_10M_IDLE_EN 0x0080
182#define PFM_PWM_SWITCH 0x0040
183
184/* PLA_MAC_PWR_CTRL */
185#define D3_CLK_GATED_EN 0x00004000
186#define MCU_CLK_RATIO 0x07010f07
187#define MCU_CLK_RATIO_MASK 0x0f0f0f0f
188
189/* PLA_GPHY_INTR_IMR */
190#define GPHY_STS_MSK 0x0001
191#define SPEED_DOWN_MSK 0x0002
192#define SPDWN_RXDV_MSK 0x0004
193#define SPDWN_LINKCHG_MSK 0x0008
194
195/* PLA_PHYAR */
196#define PHYAR_FLAG 0x80000000
197
198/* PLA_EEE_CR */
199#define EEE_RX_EN 0x0001
200#define EEE_TX_EN 0x0002
201
202/* USB_DEV_STAT */
203#define STAT_SPEED_MASK 0x0006
204#define STAT_SPEED_HIGH 0x0000
205#define STAT_SPEED_FULL 0x0001
206
207/* USB_TX_AGG */
208#define TX_AGG_MAX_THRESHOLD 0x03
209
210/* USB_RX_BUF_TH */
211#define RX_BUF_THR 0x7a120180
212
213/* USB_TX_DMA */
214#define TEST_MODE_DISABLE 0x00000001
215#define TX_SIZE_ADJUST1 0x00000100
216
217/* USB_UPS_CTRL */
218#define POWER_CUT 0x0100
219
220/* USB_PM_CTRL_STATUS */
221#define RWSUME_INDICATE 0x0001
222
223/* USB_USB_CTRL */
224#define RX_AGG_DISABLE 0x0010
225
226/* OCP_ALDPS_CONFIG */
227#define ENPWRSAVE 0x8000
228#define ENPDNPS 0x0200
229#define LINKENA 0x0100
230#define DIS_SDSAVE 0x0010
231
232/* OCP_EEE_CONFIG1 */
233#define RG_TXLPI_MSK_HFDUP 0x8000
234#define RG_MATCLR_EN 0x4000
235#define EEE_10_CAP 0x2000
236#define EEE_NWAY_EN 0x1000
237#define TX_QUIET_EN 0x0200
238#define RX_QUIET_EN 0x0100
239#define SDRISETIME 0x0010 /* bit 4 ~ 6 */
240#define RG_RXLPI_MSK_HFDUP 0x0008
241#define SDFALLTIME 0x0007 /* bit 0 ~ 2 */
242
243/* OCP_EEE_CONFIG2 */
244#define RG_LPIHYS_NUM 0x7000 /* bit 12 ~ 15 */
245#define RG_DACQUIET_EN 0x0400
246#define RG_LDVQUIET_EN 0x0200
247#define RG_CKRSEL 0x0020
248#define RG_EEEPRG_EN 0x0010
249
250/* OCP_EEE_CONFIG3 */
251#define FST_SNR_EYE_R 0x1500 /* bit 7 ~ 15 */
252#define RG_LFS_SEL 0x0060 /* bit 6 ~ 5 */
253#define MSK_PH 0x0006 /* bit 0 ~ 3 */
254
255/* OCP_EEE_AR */
256/* bit[15:14] function */
257#define FUN_ADDR 0x0000
258#define FUN_DATA 0x4000
259/* bit[4:0] device addr */
260#define DEVICE_ADDR 0x0007
261
262/* OCP_EEE_DATA */
263#define EEE_ADDR 0x003C
264#define EEE_DATA 0x0002
265
266enum rtl_register_content {
267 _100bps = 0x08,
268 _10bps = 0x04,
269 LINK_STATUS = 0x02,
270 FULL_DUP = 0x01,
271};
272
hayeswangebc2ec482013-08-14 20:54:38 +0800273#define RTL8152_MAX_TX 10
274#define RTL8152_MAX_RX 10
275
hayeswangac718b62013-05-02 16:01:25 +0000276#define RTL8152_REQT_READ 0xc0
277#define RTL8152_REQT_WRITE 0x40
278#define RTL8152_REQ_GET_REGS 0x05
279#define RTL8152_REQ_SET_REGS 0x05
280
281#define BYTE_EN_DWORD 0xff
282#define BYTE_EN_WORD 0x33
283#define BYTE_EN_BYTE 0x11
284#define BYTE_EN_SIX_BYTES 0x3f
285#define BYTE_EN_START_MASK 0x0f
286#define BYTE_EN_END_MASK 0xf0
287
288#define RTL8152_RMS (VLAN_ETH_FRAME_LEN + VLAN_HLEN)
289#define RTL8152_TX_TIMEOUT (HZ)
290
291/* rtl8152 flags */
292enum rtl8152_flags {
293 RTL8152_UNPLUG = 0,
hayeswangac718b62013-05-02 16:01:25 +0000294 RTL8152_SET_RX_MODE,
295 WORK_ENABLE
296};
297
298/* Define these values to match your device */
299#define VENDOR_ID_REALTEK 0x0bda
300#define PRODUCT_ID_RTL8152 0x8152
301
302#define MCU_TYPE_PLA 0x0100
303#define MCU_TYPE_USB 0x0000
304
305struct rx_desc {
306 u32 opts1;
307#define RX_LEN_MASK 0x7fff
308 u32 opts2;
309 u32 opts3;
310 u32 opts4;
311 u32 opts5;
312 u32 opts6;
313};
314
315struct tx_desc {
316 u32 opts1;
317#define TX_FS (1 << 31) /* First segment of a packet */
318#define TX_LS (1 << 30) /* Final segment of a packet */
hayeswang5bd23882013-08-14 20:54:39 +0800319#define TX_LEN_MASK 0x3ffff
320
hayeswangac718b62013-05-02 16:01:25 +0000321 u32 opts2;
hayeswang5bd23882013-08-14 20:54:39 +0800322#define UDP_CS (1 << 31) /* Calculate UDP/IP checksum */
323#define TCP_CS (1 << 30) /* Calculate TCP/IP checksum */
324#define IPV4_CS (1 << 29) /* Calculate IPv4 checksum */
325#define IPV6_CS (1 << 28) /* Calculate IPv6 checksum */
hayeswangac718b62013-05-02 16:01:25 +0000326};
327
hayeswangebc2ec482013-08-14 20:54:38 +0800328struct rx_agg {
329 struct list_head list;
330 struct urb *urb;
331 void *context;
332 void *buffer;
333 void *head;
334};
335
336struct tx_agg {
337 struct list_head list;
338 struct urb *urb;
339 void *context;
340 void *buffer;
341 void *head;
342 u32 skb_num;
343 u32 skb_len;
344};
345
hayeswangac718b62013-05-02 16:01:25 +0000346struct r8152 {
347 unsigned long flags;
348 struct usb_device *udev;
349 struct tasklet_struct tl;
350 struct net_device *netdev;
hayeswangebc2ec482013-08-14 20:54:38 +0800351 struct tx_agg tx_info[RTL8152_MAX_TX];
352 struct rx_agg rx_info[RTL8152_MAX_RX];
353 struct list_head rx_done, tx_free;
354 struct sk_buff_head tx_queue;
355 spinlock_t rx_lock, tx_lock;
hayeswangac718b62013-05-02 16:01:25 +0000356 struct delayed_work schedule;
357 struct mii_if_info mii;
358 u32 msg_enable;
359 u16 ocp_base;
360 u8 version;
361 u8 speed;
362};
363
364enum rtl_version {
365 RTL_VER_UNKNOWN = 0,
366 RTL_VER_01,
367 RTL_VER_02
368};
369
370/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
371 * The RTL chips use a 64 element hash table based on the Ethernet CRC.
372 */
373static const int multicast_filter_limit = 32;
hayeswangebc2ec482013-08-14 20:54:38 +0800374static unsigned int rx_buf_sz = 16384;
hayeswangac718b62013-05-02 16:01:25 +0000375
376static
377int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
378{
hayeswang31787f52013-07-31 17:21:25 +0800379 int ret;
380 void *tmp;
381
382 tmp = kmalloc(size, GFP_KERNEL);
383 if (!tmp)
384 return -ENOMEM;
385
386 ret = usb_control_msg(tp->udev, usb_rcvctrlpipe(tp->udev, 0),
hayeswangac718b62013-05-02 16:01:25 +0000387 RTL8152_REQ_GET_REGS, RTL8152_REQT_READ,
hayeswang31787f52013-07-31 17:21:25 +0800388 value, index, tmp, size, 500);
389
390 memcpy(data, tmp, size);
391 kfree(tmp);
392
393 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000394}
395
396static
397int set_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
398{
hayeswang31787f52013-07-31 17:21:25 +0800399 int ret;
400 void *tmp;
401
402 tmp = kmalloc(size, GFP_KERNEL);
403 if (!tmp)
404 return -ENOMEM;
405
406 memcpy(tmp, data, size);
407
408 ret = usb_control_msg(tp->udev, usb_sndctrlpipe(tp->udev, 0),
hayeswangac718b62013-05-02 16:01:25 +0000409 RTL8152_REQ_SET_REGS, RTL8152_REQT_WRITE,
hayeswang31787f52013-07-31 17:21:25 +0800410 value, index, tmp, size, 500);
411
412 kfree(tmp);
413 return ret;
hayeswangac718b62013-05-02 16:01:25 +0000414}
415
416static int generic_ocp_read(struct r8152 *tp, u16 index, u16 size,
417 void *data, u16 type)
418{
419 u16 limit = 64;
420 int ret = 0;
421
422 if (test_bit(RTL8152_UNPLUG, &tp->flags))
423 return -ENODEV;
424
425 /* both size and indix must be 4 bytes align */
426 if ((size & 3) || !size || (index & 3) || !data)
427 return -EPERM;
428
429 if ((u32)index + (u32)size > 0xffff)
430 return -EPERM;
431
432 while (size) {
433 if (size > limit) {
434 ret = get_registers(tp, index, type, limit, data);
435 if (ret < 0)
436 break;
437
438 index += limit;
439 data += limit;
440 size -= limit;
441 } else {
442 ret = get_registers(tp, index, type, size, data);
443 if (ret < 0)
444 break;
445
446 index += size;
447 data += size;
448 size = 0;
449 break;
450 }
451 }
452
453 return ret;
454}
455
456static int generic_ocp_write(struct r8152 *tp, u16 index, u16 byteen,
457 u16 size, void *data, u16 type)
458{
459 int ret;
460 u16 byteen_start, byteen_end, byen;
461 u16 limit = 512;
462
463 if (test_bit(RTL8152_UNPLUG, &tp->flags))
464 return -ENODEV;
465
466 /* both size and indix must be 4 bytes align */
467 if ((size & 3) || !size || (index & 3) || !data)
468 return -EPERM;
469
470 if ((u32)index + (u32)size > 0xffff)
471 return -EPERM;
472
473 byteen_start = byteen & BYTE_EN_START_MASK;
474 byteen_end = byteen & BYTE_EN_END_MASK;
475
476 byen = byteen_start | (byteen_start << 4);
477 ret = set_registers(tp, index, type | byen, 4, data);
478 if (ret < 0)
479 goto error1;
480
481 index += 4;
482 data += 4;
483 size -= 4;
484
485 if (size) {
486 size -= 4;
487
488 while (size) {
489 if (size > limit) {
490 ret = set_registers(tp, index,
491 type | BYTE_EN_DWORD,
492 limit, data);
493 if (ret < 0)
494 goto error1;
495
496 index += limit;
497 data += limit;
498 size -= limit;
499 } else {
500 ret = set_registers(tp, index,
501 type | BYTE_EN_DWORD,
502 size, data);
503 if (ret < 0)
504 goto error1;
505
506 index += size;
507 data += size;
508 size = 0;
509 break;
510 }
511 }
512
513 byen = byteen_end | (byteen_end >> 4);
514 ret = set_registers(tp, index, type | byen, 4, data);
515 if (ret < 0)
516 goto error1;
517 }
518
519error1:
520 return ret;
521}
522
523static inline
524int pla_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
525{
526 return generic_ocp_read(tp, index, size, data, MCU_TYPE_PLA);
527}
528
529static inline
530int pla_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
531{
532 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_PLA);
533}
534
535static inline
536int usb_ocp_read(struct r8152 *tp, u16 index, u16 size, void *data)
537{
538 return generic_ocp_read(tp, index, size, data, MCU_TYPE_USB);
539}
540
541static inline
542int usb_ocp_write(struct r8152 *tp, u16 index, u16 byteen, u16 size, void *data)
543{
544 return generic_ocp_write(tp, index, byteen, size, data, MCU_TYPE_USB);
545}
546
547static u32 ocp_read_dword(struct r8152 *tp, u16 type, u16 index)
548{
hayeswangc8826de2013-07-31 17:21:26 +0800549 __le32 data;
hayeswangac718b62013-05-02 16:01:25 +0000550
hayeswangc8826de2013-07-31 17:21:26 +0800551 generic_ocp_read(tp, index, sizeof(data), &data, type);
hayeswangac718b62013-05-02 16:01:25 +0000552
553 return __le32_to_cpu(data);
554}
555
556static void ocp_write_dword(struct r8152 *tp, u16 type, u16 index, u32 data)
557{
hayeswangc8826de2013-07-31 17:21:26 +0800558 __le32 tmp = __cpu_to_le32(data);
559
560 generic_ocp_write(tp, index, BYTE_EN_DWORD, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000561}
562
563static u16 ocp_read_word(struct r8152 *tp, u16 type, u16 index)
564{
565 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +0800566 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000567 u8 shift = index & 2;
568
569 index &= ~3;
570
hayeswangc8826de2013-07-31 17:21:26 +0800571 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000572
hayeswangc8826de2013-07-31 17:21:26 +0800573 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +0000574 data >>= (shift * 8);
575 data &= 0xffff;
576
577 return (u16)data;
578}
579
580static void ocp_write_word(struct r8152 *tp, u16 type, u16 index, u32 data)
581{
hayeswangc8826de2013-07-31 17:21:26 +0800582 u32 mask = 0xffff;
583 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000584 u16 byen = BYTE_EN_WORD;
585 u8 shift = index & 2;
586
587 data &= mask;
588
589 if (index & 2) {
590 byen <<= shift;
591 mask <<= (shift * 8);
592 data <<= (shift * 8);
593 index &= ~3;
594 }
595
hayeswangc8826de2013-07-31 17:21:26 +0800596 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000597
hayeswangc8826de2013-07-31 17:21:26 +0800598 data |= __le32_to_cpu(tmp) & ~mask;
599 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +0000600
hayeswangc8826de2013-07-31 17:21:26 +0800601 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000602}
603
604static u8 ocp_read_byte(struct r8152 *tp, u16 type, u16 index)
605{
606 u32 data;
hayeswangc8826de2013-07-31 17:21:26 +0800607 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000608 u8 shift = index & 3;
609
610 index &= ~3;
611
hayeswangc8826de2013-07-31 17:21:26 +0800612 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000613
hayeswangc8826de2013-07-31 17:21:26 +0800614 data = __le32_to_cpu(tmp);
hayeswangac718b62013-05-02 16:01:25 +0000615 data >>= (shift * 8);
616 data &= 0xff;
617
618 return (u8)data;
619}
620
621static void ocp_write_byte(struct r8152 *tp, u16 type, u16 index, u32 data)
622{
hayeswangc8826de2013-07-31 17:21:26 +0800623 u32 mask = 0xff;
624 __le32 tmp;
hayeswangac718b62013-05-02 16:01:25 +0000625 u16 byen = BYTE_EN_BYTE;
626 u8 shift = index & 3;
627
628 data &= mask;
629
630 if (index & 3) {
631 byen <<= shift;
632 mask <<= (shift * 8);
633 data <<= (shift * 8);
634 index &= ~3;
635 }
636
hayeswangc8826de2013-07-31 17:21:26 +0800637 generic_ocp_read(tp, index, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000638
hayeswangc8826de2013-07-31 17:21:26 +0800639 data |= __le32_to_cpu(tmp) & ~mask;
640 tmp = __cpu_to_le32(data);
hayeswangac718b62013-05-02 16:01:25 +0000641
hayeswangc8826de2013-07-31 17:21:26 +0800642 generic_ocp_write(tp, index, byen, sizeof(tmp), &tmp, type);
hayeswangac718b62013-05-02 16:01:25 +0000643}
644
645static void r8152_mdio_write(struct r8152 *tp, u32 reg_addr, u32 value)
646{
647 u32 ocp_data;
648 int i;
649
650 ocp_data = PHYAR_FLAG | ((reg_addr & 0x1f) << 16) |
651 (value & 0xffff);
652
653 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_PHYAR, ocp_data);
654
655 for (i = 20; i > 0; i--) {
656 udelay(25);
657 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_PHYAR);
658 if (!(ocp_data & PHYAR_FLAG))
659 break;
660 }
661 udelay(20);
662}
663
664static int r8152_mdio_read(struct r8152 *tp, u32 reg_addr)
665{
666 u32 ocp_data;
667 int i;
668
669 ocp_data = (reg_addr & 0x1f) << 16;
670 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_PHYAR, ocp_data);
671
672 for (i = 20; i > 0; i--) {
673 udelay(25);
674 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_PHYAR);
675 if (ocp_data & PHYAR_FLAG)
676 break;
677 }
678 udelay(20);
679
680 if (!(ocp_data & PHYAR_FLAG))
681 return -EAGAIN;
682
683 return (u16)(ocp_data & 0xffff);
684}
685
686static int read_mii_word(struct net_device *netdev, int phy_id, int reg)
687{
688 struct r8152 *tp = netdev_priv(netdev);
689
690 if (phy_id != R8152_PHY_ID)
691 return -EINVAL;
692
693 return r8152_mdio_read(tp, reg);
694}
695
696static
697void write_mii_word(struct net_device *netdev, int phy_id, int reg, int val)
698{
699 struct r8152 *tp = netdev_priv(netdev);
700
701 if (phy_id != R8152_PHY_ID)
702 return;
703
704 r8152_mdio_write(tp, reg, val);
705}
706
707static void ocp_reg_write(struct r8152 *tp, u16 addr, u16 data)
708{
709 u16 ocp_base, ocp_index;
710
711 ocp_base = addr & 0xf000;
712 if (ocp_base != tp->ocp_base) {
713 ocp_write_word(tp, MCU_TYPE_PLA, PLA_OCP_GPHY_BASE, ocp_base);
714 tp->ocp_base = ocp_base;
715 }
716
717 ocp_index = (addr & 0x0fff) | 0xb000;
718 ocp_write_word(tp, MCU_TYPE_PLA, ocp_index, data);
719}
720
hayeswangebc2ec482013-08-14 20:54:38 +0800721static
722int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags);
723
hayeswangac718b62013-05-02 16:01:25 +0000724static inline void set_ethernet_addr(struct r8152 *tp)
725{
726 struct net_device *dev = tp->netdev;
hayeswang31787f52013-07-31 17:21:25 +0800727 u8 node_id[8] = {0};
hayeswangac718b62013-05-02 16:01:25 +0000728
hayeswang31787f52013-07-31 17:21:25 +0800729 if (pla_ocp_read(tp, PLA_IDR, sizeof(node_id), node_id) < 0)
hayeswangac718b62013-05-02 16:01:25 +0000730 netif_notice(tp, probe, dev, "inet addr fail\n");
731 else {
732 memcpy(dev->dev_addr, node_id, dev->addr_len);
733 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
734 }
hayeswangac718b62013-05-02 16:01:25 +0000735}
736
737static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
738{
739 struct r8152 *tp = netdev_priv(netdev);
740 struct sockaddr *addr = p;
741
742 if (!is_valid_ether_addr(addr->sa_data))
743 return -EADDRNOTAVAIL;
744
745 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
746
747 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_CONFIG);
748 pla_ocp_write(tp, PLA_IDR, BYTE_EN_SIX_BYTES, 8, addr->sa_data);
749 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
750
751 return 0;
752}
753
hayeswangac718b62013-05-02 16:01:25 +0000754static struct net_device_stats *rtl8152_get_stats(struct net_device *dev)
755{
756 return &dev->stats;
757}
758
759static void read_bulk_callback(struct urb *urb)
760{
hayeswangac718b62013-05-02 16:01:25 +0000761 struct net_device *netdev;
hayeswangebc2ec482013-08-14 20:54:38 +0800762 unsigned long lockflags;
hayeswangac718b62013-05-02 16:01:25 +0000763 int status = urb->status;
hayeswangebc2ec482013-08-14 20:54:38 +0800764 struct rx_agg *agg;
765 struct r8152 *tp;
hayeswangac718b62013-05-02 16:01:25 +0000766 int result;
hayeswangac718b62013-05-02 16:01:25 +0000767
hayeswangebc2ec482013-08-14 20:54:38 +0800768 agg = urb->context;
769 if (!agg)
770 return;
771
772 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +0000773 if (!tp)
774 return;
hayeswangebc2ec482013-08-14 20:54:38 +0800775
hayeswangac718b62013-05-02 16:01:25 +0000776 if (test_bit(RTL8152_UNPLUG, &tp->flags))
777 return;
hayeswangebc2ec482013-08-14 20:54:38 +0800778
779 if (!test_bit(WORK_ENABLE, &tp->flags))
hayeswangac718b62013-05-02 16:01:25 +0000780 return;
781
hayeswangebc2ec482013-08-14 20:54:38 +0800782 netdev = tp->netdev;
783 if (!netif_carrier_ok(netdev))
784 return;
785
hayeswangac718b62013-05-02 16:01:25 +0000786 switch (status) {
787 case 0:
hayeswangebc2ec482013-08-14 20:54:38 +0800788 if (urb->actual_length < ETH_ZLEN)
789 break;
790
791 spin_lock_irqsave(&tp->rx_lock, lockflags);
792 list_add_tail(&agg->list, &tp->rx_done);
793 spin_unlock_irqrestore(&tp->rx_lock, lockflags);
794 tasklet_schedule(&tp->tl);
795 return;
hayeswangac718b62013-05-02 16:01:25 +0000796 case -ESHUTDOWN:
797 set_bit(RTL8152_UNPLUG, &tp->flags);
798 netif_device_detach(tp->netdev);
hayeswangebc2ec482013-08-14 20:54:38 +0800799 return;
hayeswangac718b62013-05-02 16:01:25 +0000800 case -ENOENT:
801 return; /* the urb is in unlink state */
802 case -ETIME:
803 pr_warn_ratelimited("may be reset is needed?..\n");
hayeswangebc2ec482013-08-14 20:54:38 +0800804 break;
hayeswangac718b62013-05-02 16:01:25 +0000805 default:
806 pr_warn_ratelimited("Rx status %d\n", status);
hayeswangebc2ec482013-08-14 20:54:38 +0800807 break;
hayeswangac718b62013-05-02 16:01:25 +0000808 }
809
hayeswangebc2ec482013-08-14 20:54:38 +0800810 result = r8152_submit_rx(tp, agg, GFP_ATOMIC);
hayeswangac718b62013-05-02 16:01:25 +0000811 if (result == -ENODEV) {
812 netif_device_detach(tp->netdev);
813 } else if (result) {
hayeswangebc2ec482013-08-14 20:54:38 +0800814 spin_lock_irqsave(&tp->rx_lock, lockflags);
815 list_add_tail(&agg->list, &tp->rx_done);
816 spin_unlock_irqrestore(&tp->rx_lock, lockflags);
817 tasklet_schedule(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +0000818 }
hayeswangac718b62013-05-02 16:01:25 +0000819}
820
821static void write_bulk_callback(struct urb *urb)
822{
hayeswangebc2ec482013-08-14 20:54:38 +0800823 struct net_device_stats *stats;
824 unsigned long lockflags;
825 struct tx_agg *agg;
hayeswangac718b62013-05-02 16:01:25 +0000826 struct r8152 *tp;
827 int status = urb->status;
828
hayeswangebc2ec482013-08-14 20:54:38 +0800829 agg = urb->context;
830 if (!agg)
831 return;
832
833 tp = agg->context;
hayeswangac718b62013-05-02 16:01:25 +0000834 if (!tp)
835 return;
hayeswangebc2ec482013-08-14 20:54:38 +0800836
837 stats = rtl8152_get_stats(tp->netdev);
838 if (status) {
839 pr_warn_ratelimited("Tx status %d\n", status);
840 stats->tx_errors += agg->skb_num;
841 } else {
842 stats->tx_packets += agg->skb_num;
843 stats->tx_bytes += agg->skb_len;
844 }
845
846 spin_lock_irqsave(&tp->tx_lock, lockflags);
847 list_add_tail(&agg->list, &tp->tx_free);
848 spin_unlock_irqrestore(&tp->tx_lock, lockflags);
849
850 if (!netif_carrier_ok(tp->netdev))
hayeswangac718b62013-05-02 16:01:25 +0000851 return;
hayeswangebc2ec482013-08-14 20:54:38 +0800852
853 if (!test_bit(WORK_ENABLE, &tp->flags))
854 return;
855
856 if (test_bit(RTL8152_UNPLUG, &tp->flags))
857 return;
858
859 if (!skb_queue_empty(&tp->tx_queue))
860 tasklet_schedule(&tp->tl);
861}
862
863static inline void *rx_agg_align(void *data)
864{
865 return (void *)ALIGN((uintptr_t)data, 8);
866}
867
868static inline void *tx_agg_align(void *data)
869{
870 return (void *)ALIGN((uintptr_t)data, 4);
871}
872
873static void free_all_mem(struct r8152 *tp)
874{
875 int i;
876
877 for (i = 0; i < RTL8152_MAX_RX; i++) {
878 if (tp->rx_info[i].urb) {
879 usb_free_urb(tp->rx_info[i].urb);
880 tp->rx_info[i].urb = NULL;
881 }
882
883 if (tp->rx_info[i].buffer) {
884 kfree(tp->rx_info[i].buffer);
885 tp->rx_info[i].buffer = NULL;
886 tp->rx_info[i].head = NULL;
887 }
888 }
889
890 for (i = 0; i < RTL8152_MAX_TX; i++) {
891 if (tp->tx_info[i].urb) {
892 usb_free_urb(tp->tx_info[i].urb);
893 tp->tx_info[i].urb = NULL;
894 }
895
896 if (tp->tx_info[i].buffer) {
897 kfree(tp->tx_info[i].buffer);
898 tp->tx_info[i].buffer = NULL;
899 tp->tx_info[i].head = NULL;
900 }
901 }
902}
903
904static int alloc_all_mem(struct r8152 *tp)
905{
906 struct net_device *netdev = tp->netdev;
907 struct urb *urb;
908 int node, i;
909 u8 *buf;
910
911 node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
912
913 spin_lock_init(&tp->rx_lock);
914 spin_lock_init(&tp->tx_lock);
915 INIT_LIST_HEAD(&tp->rx_done);
916 INIT_LIST_HEAD(&tp->tx_free);
917 skb_queue_head_init(&tp->tx_queue);
918
919 for (i = 0; i < RTL8152_MAX_RX; i++) {
920 buf = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
921 if (!buf)
922 goto err1;
923
924 if (buf != rx_agg_align(buf)) {
925 kfree(buf);
926 buf = kmalloc_node(rx_buf_sz + 8, GFP_KERNEL, node);
927 if (!buf)
928 goto err1;
929 }
930
931 urb = usb_alloc_urb(0, GFP_KERNEL);
932 if (!urb) {
933 kfree(buf);
934 goto err1;
935 }
936
937 INIT_LIST_HEAD(&tp->rx_info[i].list);
938 tp->rx_info[i].context = tp;
939 tp->rx_info[i].urb = urb;
940 tp->rx_info[i].buffer = buf;
941 tp->rx_info[i].head = rx_agg_align(buf);
942 }
943
944 for (i = 0; i < RTL8152_MAX_TX; i++) {
945 buf = kmalloc_node(rx_buf_sz, GFP_KERNEL, node);
946 if (!buf)
947 goto err1;
948
949 if (buf != tx_agg_align(buf)) {
950 kfree(buf);
951 buf = kmalloc_node(rx_buf_sz + 4, GFP_KERNEL, node);
952 if (!buf)
953 goto err1;
954 }
955
956 urb = usb_alloc_urb(0, GFP_KERNEL);
957 if (!urb) {
958 kfree(buf);
959 goto err1;
960 }
961
962 INIT_LIST_HEAD(&tp->tx_info[i].list);
963 tp->tx_info[i].context = tp;
964 tp->tx_info[i].urb = urb;
965 tp->tx_info[i].buffer = buf;
966 tp->tx_info[i].head = tx_agg_align(buf);
967
968 list_add_tail(&tp->tx_info[i].list, &tp->tx_free);
969 }
970
971 return 0;
972
973err1:
974 free_all_mem(tp);
975 return -ENOMEM;
976}
977
hayeswang5bd23882013-08-14 20:54:39 +0800978static void
979r8152_tx_csum(struct r8152 *tp, struct tx_desc *desc, struct sk_buff *skb)
980{
981 memset(desc, 0, sizeof(*desc));
982
983 desc->opts1 = cpu_to_le32((skb->len & TX_LEN_MASK) | TX_FS | TX_LS);
984
985 if (skb->ip_summed == CHECKSUM_PARTIAL) {
986 __be16 protocol;
987 u8 ip_protocol;
988 u32 opts2 = 0;
989
990 if (skb->protocol == htons(ETH_P_8021Q))
991 protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
992 else
993 protocol = skb->protocol;
994
995 switch (protocol) {
996 case htons(ETH_P_IP):
997 opts2 |= IPV4_CS;
998 ip_protocol = ip_hdr(skb)->protocol;
999 break;
1000
1001 case htons(ETH_P_IPV6):
1002 opts2 |= IPV6_CS;
1003 ip_protocol = ipv6_hdr(skb)->nexthdr;
1004 break;
1005
1006 default:
1007 ip_protocol = IPPROTO_RAW;
1008 break;
1009 }
1010
1011 if (ip_protocol == IPPROTO_TCP) {
1012 opts2 |= TCP_CS;
1013 opts2 |= (skb_transport_offset(skb) & 0x7fff) << 17;
1014 } else if (ip_protocol == IPPROTO_UDP) {
1015 opts2 |= UDP_CS;
1016 } else {
1017 WARN_ON_ONCE(1);
1018 }
1019
1020 desc->opts2 = cpu_to_le32(opts2);
1021 }
1022}
1023
hayeswangebc2ec482013-08-14 20:54:38 +08001024static void rx_bottom(struct r8152 *tp)
1025{
1026 struct net_device_stats *stats;
1027 struct net_device *netdev;
1028 struct rx_agg *agg;
1029 struct rx_desc *rx_desc;
1030 unsigned long lockflags;
1031 struct list_head *cursor, *next;
1032 struct sk_buff *skb;
1033 struct urb *urb;
1034 unsigned pkt_len;
1035 int len_used;
1036 u8 *rx_data;
1037 int ret;
1038
1039 netdev = tp->netdev;
1040
1041 stats = rtl8152_get_stats(netdev);
1042
1043 spin_lock_irqsave(&tp->rx_lock, lockflags);
1044 list_for_each_safe(cursor, next, &tp->rx_done) {
1045 list_del_init(cursor);
1046 spin_unlock_irqrestore(&tp->rx_lock, lockflags);
1047
1048 agg = list_entry(cursor, struct rx_agg, list);
1049 urb = agg->urb;
1050 if (urb->actual_length < ETH_ZLEN) {
1051 ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
1052 spin_lock_irqsave(&tp->rx_lock, lockflags);
1053 if (ret && ret != -ENODEV) {
1054 list_add_tail(&agg->list, next);
1055 tasklet_schedule(&tp->tl);
1056 }
1057 continue;
1058 }
1059
1060 len_used = 0;
1061 rx_desc = agg->head;
1062 rx_data = agg->head;
1063 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
1064 len_used += sizeof(struct rx_desc) + pkt_len;
1065
1066 while (urb->actual_length >= len_used) {
1067 if (pkt_len < ETH_ZLEN)
1068 break;
1069
1070 pkt_len -= 4; /* CRC */
1071 rx_data += sizeof(struct rx_desc);
1072
1073 skb = netdev_alloc_skb_ip_align(netdev, pkt_len);
1074 if (!skb) {
1075 stats->rx_dropped++;
1076 break;
1077 }
1078 memcpy(skb->data, rx_data, pkt_len);
1079 skb_put(skb, pkt_len);
1080 skb->protocol = eth_type_trans(skb, netdev);
1081 netif_rx(skb);
1082 stats->rx_packets++;
1083 stats->rx_bytes += pkt_len;
1084
1085 rx_data = rx_agg_align(rx_data + pkt_len + 4);
1086 rx_desc = (struct rx_desc *)rx_data;
1087 pkt_len = le32_to_cpu(rx_desc->opts1) & RX_LEN_MASK;
1088 len_used = (int)(rx_data - (u8 *)agg->head);
1089 len_used += sizeof(struct rx_desc) + pkt_len;
1090 }
1091
1092 ret = r8152_submit_rx(tp, agg, GFP_ATOMIC);
1093 spin_lock_irqsave(&tp->rx_lock, lockflags);
1094 if (ret && ret != -ENODEV) {
1095 list_add_tail(&agg->list, next);
1096 tasklet_schedule(&tp->tl);
1097 }
1098 }
1099 spin_unlock_irqrestore(&tp->rx_lock, lockflags);
1100}
1101
1102static void tx_bottom(struct r8152 *tp)
1103{
1104 struct net_device_stats *stats;
1105 struct net_device *netdev;
1106 struct tx_agg *agg;
1107 unsigned long lockflags;
1108 u32 remain, total;
1109 u8 *tx_data;
1110 int res;
1111
1112 netdev = tp->netdev;
1113
1114next_agg:
1115 agg = NULL;
1116 spin_lock_irqsave(&tp->tx_lock, lockflags);
1117 if (!skb_queue_empty(&tp->tx_queue) && !list_empty(&tp->tx_free)) {
1118 struct list_head *cursor;
1119
1120 cursor = tp->tx_free.next;
1121 list_del_init(cursor);
1122 agg = list_entry(cursor, struct tx_agg, list);
1123 }
1124 spin_unlock_irqrestore(&tp->tx_lock, lockflags);
1125
1126 if (!agg)
1127 return;
1128
1129 tx_data = agg->head;
1130 agg->skb_num = agg->skb_len = 0;
1131 remain = rx_buf_sz - sizeof(struct tx_desc);
1132 total = 0;
1133
1134 while (remain >= ETH_ZLEN) {
1135 struct tx_desc *tx_desc;
1136 struct sk_buff *skb;
1137 unsigned int len;
1138
1139 skb = skb_dequeue(&tp->tx_queue);
1140 if (!skb)
1141 break;
1142
1143 len = skb->len;
1144 if (remain < len) {
1145 skb_queue_head(&tp->tx_queue, skb);
1146 break;
1147 }
1148
1149 tx_data = tx_agg_align(tx_data);
1150 tx_desc = (struct tx_desc *)tx_data;
1151 tx_data += sizeof(*tx_desc);
1152
hayeswang5bd23882013-08-14 20:54:39 +08001153 r8152_tx_csum(tp, tx_desc, skb);
hayeswangebc2ec482013-08-14 20:54:38 +08001154 memcpy(tx_data, skb->data, len);
1155 agg->skb_num++;
1156 agg->skb_len += len;
1157 dev_kfree_skb_any(skb);
1158
1159 tx_data += len;
1160 remain = rx_buf_sz - sizeof(*tx_desc) -
1161 (u32)(tx_agg_align(tx_data) - agg->head);
1162 }
1163
1164 usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2),
1165 agg->head, (int)(tx_data - (u8 *)agg->head),
1166 (usb_complete_t)write_bulk_callback, agg);
1167 res = usb_submit_urb(agg->urb, GFP_ATOMIC);
1168
1169 stats = rtl8152_get_stats(netdev);
1170
1171 if (res) {
1172 /* Can we get/handle EPIPE here? */
1173 if (res == -ENODEV) {
1174 netif_device_detach(netdev);
1175 } else {
1176 netif_warn(tp, tx_err, netdev,
1177 "failed tx_urb %d\n", res);
1178 stats->tx_dropped += agg->skb_num;
1179 spin_lock_irqsave(&tp->tx_lock, lockflags);
1180 list_add_tail(&agg->list, &tp->tx_free);
1181 spin_unlock_irqrestore(&tp->tx_lock, lockflags);
1182 }
1183 return;
1184 }
1185 goto next_agg;
1186}
1187
1188static void bottom_half(unsigned long data)
1189{
1190 struct r8152 *tp;
1191
1192 tp = (struct r8152 *)data;
1193
1194 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1195 return;
1196
1197 if (!test_bit(WORK_ENABLE, &tp->flags))
1198 return;
1199
1200 if (!netif_carrier_ok(tp->netdev))
1201 return;
1202
1203 rx_bottom(tp);
1204 tx_bottom(tp);
1205}
1206
1207static
1208int r8152_submit_rx(struct r8152 *tp, struct rx_agg *agg, gfp_t mem_flags)
1209{
1210 usb_fill_bulk_urb(agg->urb, tp->udev, usb_rcvbulkpipe(tp->udev, 1),
1211 agg->head, rx_buf_sz,
1212 (usb_complete_t)read_bulk_callback, agg);
1213
1214 return usb_submit_urb(agg->urb, mem_flags);
hayeswangac718b62013-05-02 16:01:25 +00001215}
1216
1217static void rtl8152_tx_timeout(struct net_device *netdev)
1218{
1219 struct r8152 *tp = netdev_priv(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08001220 int i;
1221
hayeswangac718b62013-05-02 16:01:25 +00001222 netif_warn(tp, tx_err, netdev, "Tx timeout.\n");
hayeswangebc2ec482013-08-14 20:54:38 +08001223 for (i = 0; i < RTL8152_MAX_TX; i++)
1224 usb_unlink_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00001225}
1226
1227static void rtl8152_set_rx_mode(struct net_device *netdev)
1228{
1229 struct r8152 *tp = netdev_priv(netdev);
1230
1231 if (tp->speed & LINK_STATUS)
1232 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
1233}
1234
1235static void _rtl8152_set_rx_mode(struct net_device *netdev)
1236{
1237 struct r8152 *tp = netdev_priv(netdev);
hayeswang31787f52013-07-31 17:21:25 +08001238 u32 mc_filter[2]; /* Multicast hash filter */
1239 __le32 tmp[2];
hayeswangac718b62013-05-02 16:01:25 +00001240 u32 ocp_data;
1241
hayeswangac718b62013-05-02 16:01:25 +00001242 clear_bit(RTL8152_SET_RX_MODE, &tp->flags);
1243 netif_stop_queue(netdev);
1244 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1245 ocp_data &= ~RCR_ACPT_ALL;
1246 ocp_data |= RCR_AB | RCR_APM;
1247
1248 if (netdev->flags & IFF_PROMISC) {
1249 /* Unconditionally log net taps. */
1250 netif_notice(tp, link, netdev, "Promiscuous mode enabled\n");
1251 ocp_data |= RCR_AM | RCR_AAP;
1252 mc_filter[1] = mc_filter[0] = 0xffffffff;
1253 } else if ((netdev_mc_count(netdev) > multicast_filter_limit) ||
1254 (netdev->flags & IFF_ALLMULTI)) {
1255 /* Too many to filter perfectly -- accept all multicasts. */
1256 ocp_data |= RCR_AM;
1257 mc_filter[1] = mc_filter[0] = 0xffffffff;
1258 } else {
1259 struct netdev_hw_addr *ha;
1260
1261 mc_filter[1] = mc_filter[0] = 0;
1262 netdev_for_each_mc_addr(ha, netdev) {
1263 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
1264 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
1265 ocp_data |= RCR_AM;
1266 }
1267 }
1268
hayeswang31787f52013-07-31 17:21:25 +08001269 tmp[0] = __cpu_to_le32(swab32(mc_filter[1]));
1270 tmp[1] = __cpu_to_le32(swab32(mc_filter[0]));
hayeswangac718b62013-05-02 16:01:25 +00001271
hayeswang31787f52013-07-31 17:21:25 +08001272 pla_ocp_write(tp, PLA_MAR, BYTE_EN_DWORD, sizeof(tmp), tmp);
hayeswangac718b62013-05-02 16:01:25 +00001273 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1274 netif_wake_queue(netdev);
hayeswangac718b62013-05-02 16:01:25 +00001275}
1276
1277static netdev_tx_t rtl8152_start_xmit(struct sk_buff *skb,
1278 struct net_device *netdev)
1279{
1280 struct r8152 *tp = netdev_priv(netdev);
1281 struct net_device_stats *stats = rtl8152_get_stats(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08001282 unsigned long lockflags;
1283 struct tx_agg *agg = NULL;
hayeswangac718b62013-05-02 16:01:25 +00001284 struct tx_desc *tx_desc;
hayeswang3ff25e32013-07-12 16:26:15 +08001285 unsigned int len;
hayeswangebc2ec482013-08-14 20:54:38 +08001286 u8 *tx_data;
hayeswang3ff25e32013-07-12 16:26:15 +08001287 int res;
hayeswangac718b62013-05-02 16:01:25 +00001288
hayeswangac718b62013-05-02 16:01:25 +00001289 skb_tx_timestamp(skb);
hayeswangebc2ec482013-08-14 20:54:38 +08001290
1291 spin_lock_irqsave(&tp->tx_lock, lockflags);
1292 if (!list_empty(&tp->tx_free) && skb_queue_empty(&tp->tx_queue)) {
1293 struct list_head *cursor;
1294
1295 cursor = tp->tx_free.next;
1296 list_del_init(cursor);
1297 agg = list_entry(cursor, struct tx_agg, list);
1298 }
1299 spin_unlock_irqrestore(&tp->tx_lock, lockflags);
1300
1301 if (!agg) {
1302 skb_queue_tail(&tp->tx_queue, skb);
1303 return NETDEV_TX_OK;
1304 }
1305
1306 tx_desc = (struct tx_desc *)agg->head;
1307 tx_data = agg->head + sizeof(*tx_desc);
1308 agg->skb_num = agg->skb_len = 0;
1309
1310 len = skb->len;
hayeswang5bd23882013-08-14 20:54:39 +08001311 r8152_tx_csum(tp, tx_desc, skb);
hayeswangebc2ec482013-08-14 20:54:38 +08001312 memcpy(tx_data, skb->data, len);
1313 dev_kfree_skb_any(skb);
1314 agg->skb_num++;
1315 agg->skb_len += len;
1316 usb_fill_bulk_urb(agg->urb, tp->udev, usb_sndbulkpipe(tp->udev, 2),
1317 agg->head, len + sizeof(*tx_desc),
1318 (usb_complete_t)write_bulk_callback, agg);
1319 res = usb_submit_urb(agg->urb, GFP_ATOMIC);
hayeswangac718b62013-05-02 16:01:25 +00001320 if (res) {
1321 /* Can we get/handle EPIPE here? */
1322 if (res == -ENODEV) {
1323 netif_device_detach(tp->netdev);
1324 } else {
1325 netif_warn(tp, tx_err, netdev,
1326 "failed tx_urb %d\n", res);
hayeswangebc2ec482013-08-14 20:54:38 +08001327 stats->tx_dropped++;
1328 spin_lock_irqsave(&tp->tx_lock, lockflags);
1329 list_add_tail(&agg->list, &tp->tx_free);
1330 spin_unlock_irqrestore(&tp->tx_lock, lockflags);
hayeswangac718b62013-05-02 16:01:25 +00001331 }
hayeswangac718b62013-05-02 16:01:25 +00001332 }
1333
1334 return NETDEV_TX_OK;
1335}
1336
1337static void r8152b_reset_packet_filter(struct r8152 *tp)
1338{
1339 u32 ocp_data;
1340
1341 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_FMC);
1342 ocp_data &= ~FMC_FCR_MCU_EN;
1343 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
1344 ocp_data |= FMC_FCR_MCU_EN;
1345 ocp_write_word(tp, MCU_TYPE_PLA, PLA_FMC, ocp_data);
1346}
1347
1348static void rtl8152_nic_reset(struct r8152 *tp)
1349{
1350 int i;
1351
1352 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, CR_RST);
1353
1354 for (i = 0; i < 1000; i++) {
1355 if (!(ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR) & CR_RST))
1356 break;
1357 udelay(100);
1358 }
1359}
1360
1361static inline u8 rtl8152_get_speed(struct r8152 *tp)
1362{
1363 return ocp_read_byte(tp, MCU_TYPE_PLA, PLA_PHYSTATUS);
1364}
1365
1366static int rtl8152_enable(struct r8152 *tp)
1367{
hayeswangebc2ec482013-08-14 20:54:38 +08001368 u32 ocp_data;
1369 int i, ret;
hayeswangac718b62013-05-02 16:01:25 +00001370 u8 speed;
1371
1372 speed = rtl8152_get_speed(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08001373 if (speed & _10bps) {
hayeswangac718b62013-05-02 16:01:25 +00001374 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec482013-08-14 20:54:38 +08001375 ocp_data |= EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00001376 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
1377 } else {
1378 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR);
hayeswangebc2ec482013-08-14 20:54:38 +08001379 ocp_data &= ~EEEP_CR_EEEP_TX;
hayeswangac718b62013-05-02 16:01:25 +00001380 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEEP_CR, ocp_data);
1381 }
1382
1383 r8152b_reset_packet_filter(tp);
1384
1385 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_CR);
1386 ocp_data |= CR_RE | CR_TE;
1387 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, ocp_data);
1388
1389 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
1390 ocp_data &= ~RXDY_GATED_EN;
1391 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
1392
hayeswangebc2ec482013-08-14 20:54:38 +08001393 INIT_LIST_HEAD(&tp->rx_done);
1394 ret = 0;
1395 for (i = 0; i < RTL8152_MAX_RX; i++) {
1396 INIT_LIST_HEAD(&tp->rx_info[i].list);
1397 ret |= r8152_submit_rx(tp, &tp->rx_info[i], GFP_KERNEL);
1398 }
hayeswangac718b62013-05-02 16:01:25 +00001399
hayeswangebc2ec482013-08-14 20:54:38 +08001400 return ret;
hayeswangac718b62013-05-02 16:01:25 +00001401}
1402
1403static void rtl8152_disable(struct r8152 *tp)
1404{
hayeswangebc2ec482013-08-14 20:54:38 +08001405 struct net_device_stats *stats = rtl8152_get_stats(tp->netdev);
1406 struct sk_buff *skb;
1407 u32 ocp_data;
1408 int i;
hayeswangac718b62013-05-02 16:01:25 +00001409
1410 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1411 ocp_data &= ~RCR_ACPT_ALL;
1412 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1413
hayeswangebc2ec482013-08-14 20:54:38 +08001414 while ((skb = skb_dequeue(&tp->tx_queue))) {
1415 dev_kfree_skb(skb);
1416 stats->tx_dropped++;
1417 }
1418
1419 for (i = 0; i < RTL8152_MAX_TX; i++)
1420 usb_kill_urb(tp->tx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00001421
1422 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
1423 ocp_data |= RXDY_GATED_EN;
1424 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
1425
1426 for (i = 0; i < 1000; i++) {
1427 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1428 if ((ocp_data & FIFO_EMPTY) == FIFO_EMPTY)
1429 break;
1430 mdelay(1);
1431 }
1432
1433 for (i = 0; i < 1000; i++) {
1434 if (ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0) & TCR0_TX_EMPTY)
1435 break;
1436 mdelay(1);
1437 }
1438
hayeswangebc2ec482013-08-14 20:54:38 +08001439 for (i = 0; i < RTL8152_MAX_RX; i++)
1440 usb_kill_urb(tp->rx_info[i].urb);
hayeswangac718b62013-05-02 16:01:25 +00001441
1442 rtl8152_nic_reset(tp);
1443}
1444
1445static void r8152b_exit_oob(struct r8152 *tp)
1446{
1447 u32 ocp_data;
1448 int i;
1449
1450 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1451 ocp_data &= ~RCR_ACPT_ALL;
1452 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1453
1454 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
1455 ocp_data |= RXDY_GATED_EN;
1456 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
1457
1458 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CRWECR, CRWECR_NORAML);
1459 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CR, 0x00);
1460
1461 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1462 ocp_data &= ~NOW_IS_OOB;
1463 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
1464
1465 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
1466 ocp_data &= ~MCU_BORW_EN;
1467 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
1468
1469 for (i = 0; i < 1000; i++) {
1470 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1471 if (ocp_data & LINK_LIST_READY)
1472 break;
1473 mdelay(1);
1474 }
1475
1476 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
1477 ocp_data |= RE_INIT_LL;
1478 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
1479
1480 for (i = 0; i < 1000; i++) {
1481 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1482 if (ocp_data & LINK_LIST_READY)
1483 break;
1484 mdelay(1);
1485 }
1486
1487 rtl8152_nic_reset(tp);
1488
1489 /* rx share fifo credit full threshold */
1490 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_NORMAL);
1491
1492 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_DEV_STAT);
1493 ocp_data &= STAT_SPEED_MASK;
1494 if (ocp_data == STAT_SPEED_FULL) {
1495 /* rx share fifo credit near full threshold */
1496 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
1497 RXFIFO_THR2_FULL);
1498 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
1499 RXFIFO_THR3_FULL);
1500 } else {
1501 /* rx share fifo credit near full threshold */
1502 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1,
1503 RXFIFO_THR2_HIGH);
1504 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2,
1505 RXFIFO_THR3_HIGH);
1506 }
1507
1508 /* TX share fifo free credit full threshold */
1509 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_TXFIFO_CTRL, TXFIFO_THR_NORMAL);
1510
1511 ocp_write_byte(tp, MCU_TYPE_USB, USB_TX_AGG, TX_AGG_MAX_THRESHOLD);
1512 ocp_write_dword(tp, MCU_TYPE_USB, USB_RX_BUF_TH, RX_BUF_THR);
1513 ocp_write_dword(tp, MCU_TYPE_USB, USB_TX_DMA,
1514 TEST_MODE_DISABLE | TX_SIZE_ADJUST1);
1515
1516 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
1517 ocp_data &= ~CPCR_RX_VLAN;
1518 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
1519
1520 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
1521
1522 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR0);
1523 ocp_data |= TCR0_AUTO_FIFO;
1524 ocp_write_word(tp, MCU_TYPE_PLA, PLA_TCR0, ocp_data);
1525}
1526
1527static void r8152b_enter_oob(struct r8152 *tp)
1528{
1529 u32 ocp_data;
1530 int i;
1531
1532 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1533 ocp_data &= ~NOW_IS_OOB;
1534 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
1535
1536 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL0, RXFIFO_THR1_OOB);
1537 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL1, RXFIFO_THR2_OOB);
1538 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RXFIFO_CTRL2, RXFIFO_THR3_OOB);
1539
1540 rtl8152_disable(tp);
1541
1542 for (i = 0; i < 1000; i++) {
1543 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1544 if (ocp_data & LINK_LIST_READY)
1545 break;
1546 mdelay(1);
1547 }
1548
1549 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7);
1550 ocp_data |= RE_INIT_LL;
1551 ocp_write_word(tp, MCU_TYPE_PLA, PLA_SFF_STS_7, ocp_data);
1552
1553 for (i = 0; i < 1000; i++) {
1554 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1555 if (ocp_data & LINK_LIST_READY)
1556 break;
1557 mdelay(1);
1558 }
1559
1560 ocp_write_word(tp, MCU_TYPE_PLA, PLA_RMS, RTL8152_RMS);
1561
1562 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL);
1563 ocp_data |= MAGIC_EN;
1564 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CFG_WOL, ocp_data);
1565
1566 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_CPCR);
1567 ocp_data |= CPCR_RX_VLAN;
1568 ocp_write_word(tp, MCU_TYPE_PLA, PLA_CPCR, ocp_data);
1569
1570 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PAL_BDC_CR);
1571 ocp_data |= ALDPS_PROXY_MODE;
1572 ocp_write_word(tp, MCU_TYPE_PLA, PAL_BDC_CR, ocp_data);
1573
1574 ocp_data = ocp_read_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL);
1575 ocp_data |= NOW_IS_OOB | DIS_MCU_CLROOB;
1576 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_OOB_CTRL, ocp_data);
1577
1578 ocp_write_byte(tp, MCU_TYPE_PLA, PLA_CONFIG5, LAN_WAKE_EN);
1579
1580 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_MISC_1);
1581 ocp_data &= ~RXDY_GATED_EN;
1582 ocp_write_word(tp, MCU_TYPE_PLA, PLA_MISC_1, ocp_data);
1583
1584 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_RCR);
1585 ocp_data |= RCR_APM | RCR_AM | RCR_AB;
1586 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_RCR, ocp_data);
1587}
1588
1589static void r8152b_disable_aldps(struct r8152 *tp)
1590{
1591 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPDNPS | LINKENA | DIS_SDSAVE);
1592 msleep(20);
1593}
1594
1595static inline void r8152b_enable_aldps(struct r8152 *tp)
1596{
1597 ocp_reg_write(tp, OCP_ALDPS_CONFIG, ENPWRSAVE | ENPDNPS |
1598 LINKENA | DIS_SDSAVE);
1599}
1600
1601static int rtl8152_set_speed(struct r8152 *tp, u8 autoneg, u16 speed, u8 duplex)
1602{
1603 u16 bmcr, anar;
1604 int ret = 0;
1605
1606 cancel_delayed_work_sync(&tp->schedule);
1607 anar = r8152_mdio_read(tp, MII_ADVERTISE);
1608 anar &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
1609 ADVERTISE_100HALF | ADVERTISE_100FULL);
1610
1611 if (autoneg == AUTONEG_DISABLE) {
1612 if (speed == SPEED_10) {
1613 bmcr = 0;
1614 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
1615 } else if (speed == SPEED_100) {
1616 bmcr = BMCR_SPEED100;
1617 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
1618 } else {
1619 ret = -EINVAL;
1620 goto out;
1621 }
1622
1623 if (duplex == DUPLEX_FULL)
1624 bmcr |= BMCR_FULLDPLX;
1625 } else {
1626 if (speed == SPEED_10) {
1627 if (duplex == DUPLEX_FULL)
1628 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
1629 else
1630 anar |= ADVERTISE_10HALF;
1631 } else if (speed == SPEED_100) {
1632 if (duplex == DUPLEX_FULL) {
1633 anar |= ADVERTISE_10HALF | ADVERTISE_10FULL;
1634 anar |= ADVERTISE_100HALF | ADVERTISE_100FULL;
1635 } else {
1636 anar |= ADVERTISE_10HALF;
1637 anar |= ADVERTISE_100HALF;
1638 }
1639 } else {
1640 ret = -EINVAL;
1641 goto out;
1642 }
1643
1644 bmcr = BMCR_ANENABLE | BMCR_ANRESTART;
1645 }
1646
1647 r8152_mdio_write(tp, MII_ADVERTISE, anar);
1648 r8152_mdio_write(tp, MII_BMCR, bmcr);
1649
1650out:
1651 schedule_delayed_work(&tp->schedule, 5 * HZ);
1652
1653 return ret;
1654}
1655
1656static void rtl8152_down(struct r8152 *tp)
1657{
1658 u32 ocp_data;
1659
1660 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
1661 ocp_data &= ~POWER_CUT;
1662 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
1663
1664 r8152b_disable_aldps(tp);
1665 r8152b_enter_oob(tp);
1666 r8152b_enable_aldps(tp);
1667}
1668
1669static void set_carrier(struct r8152 *tp)
1670{
1671 struct net_device *netdev = tp->netdev;
1672 u8 speed;
1673
1674 speed = rtl8152_get_speed(tp);
1675
1676 if (speed & LINK_STATUS) {
1677 if (!(tp->speed & LINK_STATUS)) {
1678 rtl8152_enable(tp);
1679 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
1680 netif_carrier_on(netdev);
1681 }
1682 } else {
1683 if (tp->speed & LINK_STATUS) {
1684 netif_carrier_off(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08001685 tasklet_disable(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00001686 rtl8152_disable(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08001687 tasklet_enable(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00001688 }
1689 }
1690 tp->speed = speed;
1691}
1692
1693static void rtl_work_func_t(struct work_struct *work)
1694{
1695 struct r8152 *tp = container_of(work, struct r8152, schedule.work);
1696
1697 if (!test_bit(WORK_ENABLE, &tp->flags))
1698 goto out1;
1699
1700 if (test_bit(RTL8152_UNPLUG, &tp->flags))
1701 goto out1;
1702
1703 set_carrier(tp);
1704
1705 if (test_bit(RTL8152_SET_RX_MODE, &tp->flags))
1706 _rtl8152_set_rx_mode(tp->netdev);
1707
1708 schedule_delayed_work(&tp->schedule, HZ);
1709
1710out1:
1711 return;
1712}
1713
1714static int rtl8152_open(struct net_device *netdev)
1715{
1716 struct r8152 *tp = netdev_priv(netdev);
1717 int res = 0;
1718
1719 tp->speed = rtl8152_get_speed(tp);
1720 if (tp->speed & LINK_STATUS) {
1721 res = rtl8152_enable(tp);
1722 if (res) {
1723 if (res == -ENODEV)
1724 netif_device_detach(tp->netdev);
1725
1726 netif_err(tp, ifup, netdev,
1727 "rtl8152_open failed: %d\n", res);
1728 return res;
1729 }
1730
1731 netif_carrier_on(netdev);
1732 } else {
1733 netif_stop_queue(netdev);
1734 netif_carrier_off(netdev);
1735 }
1736
1737 rtl8152_set_speed(tp, AUTONEG_ENABLE, SPEED_100, DUPLEX_FULL);
1738 netif_start_queue(netdev);
1739 set_bit(WORK_ENABLE, &tp->flags);
1740 schedule_delayed_work(&tp->schedule, 0);
1741
1742 return res;
1743}
1744
1745static int rtl8152_close(struct net_device *netdev)
1746{
1747 struct r8152 *tp = netdev_priv(netdev);
1748 int res = 0;
1749
1750 clear_bit(WORK_ENABLE, &tp->flags);
1751 cancel_delayed_work_sync(&tp->schedule);
1752 netif_stop_queue(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08001753 tasklet_disable(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00001754 rtl8152_disable(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08001755 tasklet_enable(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00001756
1757 return res;
1758}
1759
1760static void rtl_clear_bp(struct r8152 *tp)
1761{
1762 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_0, 0);
1763 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_2, 0);
1764 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_4, 0);
1765 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_BP_6, 0);
1766 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_0, 0);
1767 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_2, 0);
1768 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_4, 0);
1769 ocp_write_dword(tp, MCU_TYPE_USB, USB_BP_6, 0);
1770 mdelay(3);
1771 ocp_write_word(tp, MCU_TYPE_PLA, PLA_BP_BA, 0);
1772 ocp_write_word(tp, MCU_TYPE_USB, USB_BP_BA, 0);
1773}
1774
1775static void r8152b_enable_eee(struct r8152 *tp)
1776{
1777 u32 ocp_data;
1778
1779 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_EEE_CR);
1780 ocp_data |= EEE_RX_EN | EEE_TX_EN;
1781 ocp_write_word(tp, MCU_TYPE_PLA, PLA_EEE_CR, ocp_data);
1782 ocp_reg_write(tp, OCP_EEE_CONFIG1, RG_TXLPI_MSK_HFDUP | RG_MATCLR_EN |
1783 EEE_10_CAP | EEE_NWAY_EN |
1784 TX_QUIET_EN | RX_QUIET_EN |
1785 SDRISETIME | RG_RXLPI_MSK_HFDUP |
1786 SDFALLTIME);
1787 ocp_reg_write(tp, OCP_EEE_CONFIG2, RG_LPIHYS_NUM | RG_DACQUIET_EN |
1788 RG_LDVQUIET_EN | RG_CKRSEL |
1789 RG_EEEPRG_EN);
1790 ocp_reg_write(tp, OCP_EEE_CONFIG3, FST_SNR_EYE_R | RG_LFS_SEL | MSK_PH);
1791 ocp_reg_write(tp, OCP_EEE_AR, FUN_ADDR | DEVICE_ADDR);
1792 ocp_reg_write(tp, OCP_EEE_DATA, EEE_ADDR);
1793 ocp_reg_write(tp, OCP_EEE_AR, FUN_DATA | DEVICE_ADDR);
1794 ocp_reg_write(tp, OCP_EEE_DATA, EEE_DATA);
1795 ocp_reg_write(tp, OCP_EEE_AR, 0x0000);
1796}
1797
1798static void r8152b_enable_fc(struct r8152 *tp)
1799{
1800 u16 anar;
1801
1802 anar = r8152_mdio_read(tp, MII_ADVERTISE);
1803 anar |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1804 r8152_mdio_write(tp, MII_ADVERTISE, anar);
1805}
1806
1807static void r8152b_hw_phy_cfg(struct r8152 *tp)
1808{
1809 r8152_mdio_write(tp, MII_BMCR, BMCR_ANENABLE);
1810 r8152b_disable_aldps(tp);
1811}
1812
1813static void r8152b_init(struct r8152 *tp)
1814{
hayeswangebc2ec482013-08-14 20:54:38 +08001815 u32 ocp_data;
1816 int i;
hayeswangac718b62013-05-02 16:01:25 +00001817
1818 rtl_clear_bp(tp);
1819
1820 if (tp->version == RTL_VER_01) {
1821 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE);
1822 ocp_data &= ~LED_MODE_MASK;
1823 ocp_write_word(tp, MCU_TYPE_PLA, PLA_LED_FEATURE, ocp_data);
1824 }
1825
1826 r8152b_hw_phy_cfg(tp);
1827
1828 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
1829 ocp_data &= ~POWER_CUT;
1830 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
1831
1832 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
1833 ocp_data &= ~RWSUME_INDICATE;
1834 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
1835
1836 r8152b_exit_oob(tp);
1837
1838 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR);
1839 ocp_data |= TX_10M_IDLE_EN | PFM_PWM_SWITCH;
1840 ocp_write_word(tp, MCU_TYPE_PLA, PLA_PHY_PWR, ocp_data);
1841 ocp_data = ocp_read_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL);
1842 ocp_data &= ~MCU_CLK_RATIO_MASK;
1843 ocp_data |= MCU_CLK_RATIO | D3_CLK_GATED_EN;
1844 ocp_write_dword(tp, MCU_TYPE_PLA, PLA_MAC_PWR_CTRL, ocp_data);
1845 ocp_data = GPHY_STS_MSK | SPEED_DOWN_MSK |
1846 SPDWN_RXDV_MSK | SPDWN_LINKCHG_MSK;
1847 ocp_write_word(tp, MCU_TYPE_PLA, PLA_GPHY_INTR_IMR, ocp_data);
1848
1849 r8152b_enable_eee(tp);
1850 r8152b_enable_aldps(tp);
1851 r8152b_enable_fc(tp);
1852
1853 r8152_mdio_write(tp, MII_BMCR, BMCR_RESET | BMCR_ANENABLE |
1854 BMCR_ANRESTART);
1855 for (i = 0; i < 100; i++) {
1856 udelay(100);
1857 if (!(r8152_mdio_read(tp, MII_BMCR) & BMCR_RESET))
1858 break;
1859 }
1860
hayeswangebc2ec482013-08-14 20:54:38 +08001861 /* enable rx aggregation */
hayeswangac718b62013-05-02 16:01:25 +00001862 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_USB_CTRL);
hayeswangebc2ec482013-08-14 20:54:38 +08001863 ocp_data &= ~RX_AGG_DISABLE;
hayeswangac718b62013-05-02 16:01:25 +00001864 ocp_write_word(tp, MCU_TYPE_USB, USB_USB_CTRL, ocp_data);
1865}
1866
1867static int rtl8152_suspend(struct usb_interface *intf, pm_message_t message)
1868{
1869 struct r8152 *tp = usb_get_intfdata(intf);
1870
1871 netif_device_detach(tp->netdev);
1872
1873 if (netif_running(tp->netdev)) {
1874 clear_bit(WORK_ENABLE, &tp->flags);
1875 cancel_delayed_work_sync(&tp->schedule);
hayeswangebc2ec482013-08-14 20:54:38 +08001876 tasklet_disable(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00001877 }
1878
1879 rtl8152_down(tp);
1880
1881 return 0;
1882}
1883
1884static int rtl8152_resume(struct usb_interface *intf)
1885{
1886 struct r8152 *tp = usb_get_intfdata(intf);
1887
1888 r8152b_init(tp);
1889 netif_device_attach(tp->netdev);
1890 if (netif_running(tp->netdev)) {
1891 rtl8152_enable(tp);
1892 set_bit(WORK_ENABLE, &tp->flags);
1893 set_bit(RTL8152_SET_RX_MODE, &tp->flags);
1894 schedule_delayed_work(&tp->schedule, 0);
hayeswangebc2ec482013-08-14 20:54:38 +08001895 tasklet_enable(&tp->tl);
hayeswangac718b62013-05-02 16:01:25 +00001896 }
1897
1898 return 0;
1899}
1900
1901static void rtl8152_get_drvinfo(struct net_device *netdev,
1902 struct ethtool_drvinfo *info)
1903{
1904 struct r8152 *tp = netdev_priv(netdev);
1905
1906 strncpy(info->driver, MODULENAME, ETHTOOL_BUSINFO_LEN);
1907 strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
1908 usb_make_path(tp->udev, info->bus_info, sizeof(info->bus_info));
1909}
1910
1911static
1912int rtl8152_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
1913{
1914 struct r8152 *tp = netdev_priv(netdev);
1915
1916 if (!tp->mii.mdio_read)
1917 return -EOPNOTSUPP;
1918
1919 return mii_ethtool_gset(&tp->mii, cmd);
1920}
1921
1922static int rtl8152_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1923{
1924 struct r8152 *tp = netdev_priv(dev);
1925
1926 return rtl8152_set_speed(tp, cmd->autoneg, cmd->speed, cmd->duplex);
1927}
1928
1929static struct ethtool_ops ops = {
1930 .get_drvinfo = rtl8152_get_drvinfo,
1931 .get_settings = rtl8152_get_settings,
1932 .set_settings = rtl8152_set_settings,
1933 .get_link = ethtool_op_get_link,
1934};
1935
1936static int rtl8152_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
1937{
1938 struct r8152 *tp = netdev_priv(netdev);
1939 struct mii_ioctl_data *data = if_mii(rq);
1940 int res = 0;
1941
1942 switch (cmd) {
1943 case SIOCGMIIPHY:
1944 data->phy_id = R8152_PHY_ID; /* Internal PHY */
1945 break;
1946
1947 case SIOCGMIIREG:
1948 data->val_out = r8152_mdio_read(tp, data->reg_num);
1949 break;
1950
1951 case SIOCSMIIREG:
1952 if (!capable(CAP_NET_ADMIN)) {
1953 res = -EPERM;
1954 break;
1955 }
1956 r8152_mdio_write(tp, data->reg_num, data->val_in);
1957 break;
1958
1959 default:
1960 res = -EOPNOTSUPP;
1961 }
1962
1963 return res;
1964}
1965
1966static const struct net_device_ops rtl8152_netdev_ops = {
1967 .ndo_open = rtl8152_open,
1968 .ndo_stop = rtl8152_close,
1969 .ndo_do_ioctl = rtl8152_ioctl,
1970 .ndo_start_xmit = rtl8152_start_xmit,
1971 .ndo_tx_timeout = rtl8152_tx_timeout,
1972 .ndo_set_rx_mode = rtl8152_set_rx_mode,
1973 .ndo_set_mac_address = rtl8152_set_mac_address,
1974
1975 .ndo_change_mtu = eth_change_mtu,
1976 .ndo_validate_addr = eth_validate_addr,
1977};
1978
1979static void r8152b_get_version(struct r8152 *tp)
1980{
1981 u32 ocp_data;
1982 u16 version;
1983
1984 ocp_data = ocp_read_word(tp, MCU_TYPE_PLA, PLA_TCR1);
1985 version = (u16)(ocp_data & VERSION_MASK);
1986
1987 switch (version) {
1988 case 0x4c00:
1989 tp->version = RTL_VER_01;
1990 break;
1991 case 0x4c10:
1992 tp->version = RTL_VER_02;
1993 break;
1994 default:
1995 netif_info(tp, probe, tp->netdev,
1996 "Unknown version 0x%04x\n", version);
1997 break;
1998 }
1999}
2000
2001static int rtl8152_probe(struct usb_interface *intf,
2002 const struct usb_device_id *id)
2003{
2004 struct usb_device *udev = interface_to_usbdev(intf);
2005 struct r8152 *tp;
2006 struct net_device *netdev;
hayeswangebc2ec482013-08-14 20:54:38 +08002007 int ret;
hayeswangac718b62013-05-02 16:01:25 +00002008
2009 if (udev->actconfig->desc.bConfigurationValue != 1) {
2010 usb_driver_set_configuration(udev, 1);
2011 return -ENODEV;
2012 }
2013
2014 netdev = alloc_etherdev(sizeof(struct r8152));
2015 if (!netdev) {
2016 dev_err(&intf->dev, "Out of memory");
2017 return -ENOMEM;
2018 }
2019
hayeswangebc2ec482013-08-14 20:54:38 +08002020 SET_NETDEV_DEV(netdev, &intf->dev);
hayeswangac718b62013-05-02 16:01:25 +00002021 tp = netdev_priv(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08002022 memset(tp, 0, sizeof(*tp));
hayeswangac718b62013-05-02 16:01:25 +00002023 tp->msg_enable = 0x7FFF;
2024
hayeswangebc2ec482013-08-14 20:54:38 +08002025 tasklet_init(&tp->tl, bottom_half, (unsigned long)tp);
hayeswangac718b62013-05-02 16:01:25 +00002026 INIT_DELAYED_WORK(&tp->schedule, rtl_work_func_t);
2027
2028 tp->udev = udev;
2029 tp->netdev = netdev;
2030 netdev->netdev_ops = &rtl8152_netdev_ops;
2031 netdev->watchdog_timeo = RTL8152_TX_TIMEOUT;
hayeswang5bd23882013-08-14 20:54:39 +08002032
2033 netdev->features |= NETIF_F_IP_CSUM;
2034 netdev->hw_features = NETIF_F_IP_CSUM;
hayeswangac718b62013-05-02 16:01:25 +00002035 SET_ETHTOOL_OPS(netdev, &ops);
2036 tp->speed = 0;
2037
2038 tp->mii.dev = netdev;
2039 tp->mii.mdio_read = read_mii_word;
2040 tp->mii.mdio_write = write_mii_word;
2041 tp->mii.phy_id_mask = 0x3f;
2042 tp->mii.reg_num_mask = 0x1f;
2043 tp->mii.phy_id = R8152_PHY_ID;
2044 tp->mii.supports_gmii = 0;
2045
2046 r8152b_get_version(tp);
2047 r8152b_init(tp);
2048 set_ethernet_addr(tp);
2049
hayeswangebc2ec482013-08-14 20:54:38 +08002050 ret = alloc_all_mem(tp);
2051 if (ret)
hayeswangac718b62013-05-02 16:01:25 +00002052 goto out;
hayeswangac718b62013-05-02 16:01:25 +00002053
2054 usb_set_intfdata(intf, tp);
hayeswangac718b62013-05-02 16:01:25 +00002055
hayeswangebc2ec482013-08-14 20:54:38 +08002056 ret = register_netdev(netdev);
2057 if (ret != 0) {
hayeswangac718b62013-05-02 16:01:25 +00002058 netif_err(tp, probe, netdev, "couldn't register the device");
hayeswangebc2ec482013-08-14 20:54:38 +08002059 goto out1;
hayeswangac718b62013-05-02 16:01:25 +00002060 }
2061
2062 netif_info(tp, probe, netdev, "%s", DRIVER_VERSION);
2063
2064 return 0;
2065
hayeswangac718b62013-05-02 16:01:25 +00002066out1:
hayeswangebc2ec482013-08-14 20:54:38 +08002067 usb_set_intfdata(intf, NULL);
hayeswangac718b62013-05-02 16:01:25 +00002068out:
2069 free_netdev(netdev);
hayeswangebc2ec482013-08-14 20:54:38 +08002070 return ret;
hayeswangac718b62013-05-02 16:01:25 +00002071}
2072
2073static void rtl8152_unload(struct r8152 *tp)
2074{
2075 u32 ocp_data;
2076
2077 if (tp->version != RTL_VER_01) {
2078 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_UPS_CTRL);
2079 ocp_data |= POWER_CUT;
2080 ocp_write_word(tp, MCU_TYPE_USB, USB_UPS_CTRL, ocp_data);
2081 }
2082
2083 ocp_data = ocp_read_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS);
2084 ocp_data &= ~RWSUME_INDICATE;
2085 ocp_write_word(tp, MCU_TYPE_USB, USB_PM_CTRL_STATUS, ocp_data);
2086}
2087
2088static void rtl8152_disconnect(struct usb_interface *intf)
2089{
2090 struct r8152 *tp = usb_get_intfdata(intf);
2091
2092 usb_set_intfdata(intf, NULL);
2093 if (tp) {
2094 set_bit(RTL8152_UNPLUG, &tp->flags);
2095 tasklet_kill(&tp->tl);
2096 unregister_netdev(tp->netdev);
2097 rtl8152_unload(tp);
hayeswangebc2ec482013-08-14 20:54:38 +08002098 free_all_mem(tp);
hayeswangac718b62013-05-02 16:01:25 +00002099 free_netdev(tp->netdev);
2100 }
2101}
2102
2103/* table of devices that work with this driver */
2104static struct usb_device_id rtl8152_table[] = {
2105 {USB_DEVICE(VENDOR_ID_REALTEK, PRODUCT_ID_RTL8152)},
2106 {}
2107};
2108
2109MODULE_DEVICE_TABLE(usb, rtl8152_table);
2110
2111static struct usb_driver rtl8152_driver = {
2112 .name = MODULENAME,
hayeswangebc2ec482013-08-14 20:54:38 +08002113 .id_table = rtl8152_table,
hayeswangac718b62013-05-02 16:01:25 +00002114 .probe = rtl8152_probe,
2115 .disconnect = rtl8152_disconnect,
hayeswangac718b62013-05-02 16:01:25 +00002116 .suspend = rtl8152_suspend,
hayeswangebc2ec482013-08-14 20:54:38 +08002117 .resume = rtl8152_resume,
2118 .reset_resume = rtl8152_resume,
hayeswangac718b62013-05-02 16:01:25 +00002119};
2120
Sachin Kamatb4236daa2013-05-16 17:48:08 +00002121module_usb_driver(rtl8152_driver);
hayeswangac718b62013-05-02 16:01:25 +00002122
2123MODULE_AUTHOR(DRIVER_AUTHOR);
2124MODULE_DESCRIPTION(DRIVER_DESC);
2125MODULE_LICENSE("GPL");