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