1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ASIX AX88179/178A USB 3.0/2.0 to Gigabit Ethernet Devices
5 * Copyright (C) 2011-2013 ASIX
8 #include <linux/module.h>
9 #include <linux/etherdevice.h>
10 #include <linux/mii.h>
11 #include <linux/usb.h>
12 #include <linux/crc32.h>
13 #include <linux/usb/usbnet.h>
14 #include <uapi/linux/mdio.h>
15 #include <linux/mdio.h>
17 #define AX88179_PHY_ID 0x03
18 #define AX_EEPROM_LEN 0x100
19 #define AX88179_EEPROM_MAGIC 0x17900b95
20 #define AX_MCAST_FLTSIZE 8
21 #define AX_MAX_MCAST 64
22 #define AX_INT_PPLS_LINK ((u32)BIT(16))
23 #define AX_RXHDR_L4_TYPE_MASK 0x1c
24 #define AX_RXHDR_L4_TYPE_UDP 4
25 #define AX_RXHDR_L4_TYPE_TCP 16
26 #define AX_RXHDR_L3CSUM_ERR 2
27 #define AX_RXHDR_L4CSUM_ERR 1
28 #define AX_RXHDR_CRC_ERR ((u32)BIT(29))
29 #define AX_RXHDR_DROP_ERR ((u32)BIT(31))
30 #define AX_ACCESS_MAC 0x01
31 #define AX_ACCESS_PHY 0x02
32 #define AX_ACCESS_EEPROM 0x04
33 #define AX_ACCESS_EFUS 0x05
34 #define AX_RELOAD_EEPROM_EFUSE 0x06
35 #define AX_PAUSE_WATERLVL_HIGH 0x54
36 #define AX_PAUSE_WATERLVL_LOW 0x55
38 #define PHYSICAL_LINK_STATUS 0x02
39 #define AX_USB_SS 0x04
40 #define AX_USB_HS 0x02
42 #define GENERAL_STATUS 0x03
43 /* Check AX88179 version. UA1:Bit2 = 0, UA2:Bit2 = 1 */
46 #define AX_SROM_ADDR 0x07
47 #define AX_SROM_CMD 0x0a
51 #define AX_SROM_DATA_LOW 0x08
52 #define AX_SROM_DATA_HIGH 0x09
54 #define AX_RX_CTL 0x0b
55 #define AX_RX_CTL_DROPCRCERR 0x0100
56 #define AX_RX_CTL_IPE 0x0200
57 #define AX_RX_CTL_START 0x0080
58 #define AX_RX_CTL_AP 0x0020
59 #define AX_RX_CTL_AM 0x0010
60 #define AX_RX_CTL_AB 0x0008
61 #define AX_RX_CTL_AMALL 0x0002
62 #define AX_RX_CTL_PRO 0x0001
63 #define AX_RX_CTL_STOP 0x0000
65 #define AX_NODE_ID 0x10
66 #define AX_MULFLTARY 0x16
68 #define AX_MEDIUM_STATUS_MODE 0x22
69 #define AX_MEDIUM_GIGAMODE 0x01
70 #define AX_MEDIUM_FULL_DUPLEX 0x02
71 #define AX_MEDIUM_EN_125MHZ 0x08
72 #define AX_MEDIUM_RXFLOW_CTRLEN 0x10
73 #define AX_MEDIUM_TXFLOW_CTRLEN 0x20
74 #define AX_MEDIUM_RECEIVE_EN 0x100
75 #define AX_MEDIUM_PS 0x200
76 #define AX_MEDIUM_JUMBO_EN 0x8040
78 #define AX_MONITOR_MOD 0x24
79 #define AX_MONITOR_MODE_RWLC 0x02
80 #define AX_MONITOR_MODE_RWMP 0x04
81 #define AX_MONITOR_MODE_PMEPOL 0x20
82 #define AX_MONITOR_MODE_PMETYPE 0x40
84 #define AX_GPIO_CTRL 0x25
85 #define AX_GPIO_CTRL_GPIO3EN 0x80
86 #define AX_GPIO_CTRL_GPIO2EN 0x40
87 #define AX_GPIO_CTRL_GPIO1EN 0x20
89 #define AX_PHYPWR_RSTCTL 0x26
90 #define AX_PHYPWR_RSTCTL_BZ 0x0010
91 #define AX_PHYPWR_RSTCTL_IPRL 0x0020
92 #define AX_PHYPWR_RSTCTL_AT 0x1000
94 #define AX_RX_BULKIN_QCTRL 0x2e
95 #define AX_CLK_SELECT 0x33
96 #define AX_CLK_SELECT_BCS 0x01
97 #define AX_CLK_SELECT_ACS 0x02
98 #define AX_CLK_SELECT_ULR 0x08
100 #define AX_RXCOE_CTL 0x34
101 #define AX_RXCOE_IP 0x01
102 #define AX_RXCOE_TCP 0x02
103 #define AX_RXCOE_UDP 0x04
104 #define AX_RXCOE_TCPV6 0x20
105 #define AX_RXCOE_UDPV6 0x40
107 #define AX_TXCOE_CTL 0x35
108 #define AX_TXCOE_IP 0x01
109 #define AX_TXCOE_TCP 0x02
110 #define AX_TXCOE_UDP 0x04
111 #define AX_TXCOE_TCPV6 0x20
112 #define AX_TXCOE_UDPV6 0x40
114 #define AX_LEDCTRL 0x73
116 #define GMII_PHY_PHYSR 0x11
117 #define GMII_PHY_PHYSR_SMASK 0xc000
118 #define GMII_PHY_PHYSR_GIGA 0x8000
119 #define GMII_PHY_PHYSR_100 0x4000
120 #define GMII_PHY_PHYSR_FULL 0x2000
121 #define GMII_PHY_PHYSR_LINK 0x400
123 #define GMII_LED_ACT 0x1a
124 #define GMII_LED_ACTIVE_MASK 0xff8f
125 #define GMII_LED0_ACTIVE BIT(4)
126 #define GMII_LED1_ACTIVE BIT(5)
127 #define GMII_LED2_ACTIVE BIT(6)
129 #define GMII_LED_LINK 0x1c
130 #define GMII_LED_LINK_MASK 0xf888
131 #define GMII_LED0_LINK_10 BIT(0)
132 #define GMII_LED0_LINK_100 BIT(1)
133 #define GMII_LED0_LINK_1000 BIT(2)
134 #define GMII_LED1_LINK_10 BIT(4)
135 #define GMII_LED1_LINK_100 BIT(5)
136 #define GMII_LED1_LINK_1000 BIT(6)
137 #define GMII_LED2_LINK_10 BIT(8)
138 #define GMII_LED2_LINK_100 BIT(9)
139 #define GMII_LED2_LINK_1000 BIT(10)
140 #define LED0_ACTIVE BIT(0)
141 #define LED0_LINK_10 BIT(1)
142 #define LED0_LINK_100 BIT(2)
143 #define LED0_LINK_1000 BIT(3)
144 #define LED0_FD BIT(4)
145 #define LED0_USB3_MASK 0x001f
146 #define LED1_ACTIVE BIT(5)
147 #define LED1_LINK_10 BIT(6)
148 #define LED1_LINK_100 BIT(7)
149 #define LED1_LINK_1000 BIT(8)
150 #define LED1_FD BIT(9)
151 #define LED1_USB3_MASK 0x03e0
152 #define LED2_ACTIVE BIT(10)
153 #define LED2_LINK_1000 BIT(13)
154 #define LED2_LINK_100 BIT(12)
155 #define LED2_LINK_10 BIT(11)
156 #define LED2_FD BIT(14)
157 #define LED_VALID BIT(15)
158 #define LED2_USB3_MASK 0x7c00
160 #define GMII_PHYPAGE 0x1e
161 #define GMII_PHY_PAGE_SELECT 0x1f
162 #define GMII_PHY_PGSEL_EXT 0x0007
163 #define GMII_PHY_PGSEL_PAGE0 0x0000
164 #define GMII_PHY_PGSEL_PAGE3 0x0003
165 #define GMII_PHY_PGSEL_PAGE5 0x0005
167 struct ax88179_data {
174 struct ax88179_int_data {
179 static const struct {
180 unsigned char ctrl, timer_l, timer_h, size, ifg;
181 } AX88179_BULKIN_SIZE[] = {
182 {7, 0x4f, 0, 0x12, 0xff},
183 {7, 0x20, 3, 0x16, 0xff},
184 {7, 0xae, 7, 0x18, 0xff},
185 {7, 0xcc, 0x4c, 0x18, 8},
188 static int __ax88179_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
189 u16 size, void *data, int in_pm)
192 int (*fn)(struct usbnet *, u8, u8, u16, u16, void *, u16);
197 fn = usbnet_read_cmd;
199 fn = usbnet_read_cmd_nopm;
201 ret = fn(dev, cmd, USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
202 value, index, data, size);
204 if (unlikely(ret < 0))
205 netdev_warn(dev->net, "Failed to read reg index 0x%04x: %d\n",
211 static int __ax88179_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
212 u16 size, const void *data, int in_pm)
215 int (*fn)(struct usbnet *, u8, u8, u16, u16, const void *, u16);
220 fn = usbnet_write_cmd;
222 fn = usbnet_write_cmd_nopm;
224 ret = fn(dev, cmd, USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
225 value, index, data, size);
227 if (unlikely(ret < 0))
228 netdev_warn(dev->net, "Failed to write reg index 0x%04x: %d\n",
234 static void ax88179_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value,
235 u16 index, u16 size, void *data)
240 buf = *((u16 *)data);
242 usbnet_write_cmd_async(dev, cmd, USB_DIR_OUT | USB_TYPE_VENDOR |
243 USB_RECIP_DEVICE, value, index, &buf,
246 usbnet_write_cmd_async(dev, cmd, USB_DIR_OUT | USB_TYPE_VENDOR |
247 USB_RECIP_DEVICE, value, index, data,
252 static int ax88179_read_cmd_nopm(struct usbnet *dev, u8 cmd, u16 value,
253 u16 index, u16 size, void *data)
259 ret = __ax88179_read_cmd(dev, cmd, value, index, size, &buf, 1);
261 *((u16 *)data) = buf;
262 } else if (4 == size) {
264 ret = __ax88179_read_cmd(dev, cmd, value, index, size, &buf, 1);
266 *((u32 *)data) = buf;
268 ret = __ax88179_read_cmd(dev, cmd, value, index, size, data, 1);
274 static int ax88179_write_cmd_nopm(struct usbnet *dev, u8 cmd, u16 value,
275 u16 index, u16 size, const void *data)
281 buf = *((u16 *)data);
283 ret = __ax88179_write_cmd(dev, cmd, value, index,
286 ret = __ax88179_write_cmd(dev, cmd, value, index,
293 static int ax88179_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
294 u16 size, void *data)
300 ret = __ax88179_read_cmd(dev, cmd, value, index, size, &buf, 0);
302 *((u16 *)data) = buf;
303 } else if (4 == size) {
305 ret = __ax88179_read_cmd(dev, cmd, value, index, size, &buf, 0);
307 *((u32 *)data) = buf;
309 ret = __ax88179_read_cmd(dev, cmd, value, index, size, data, 0);
315 static int ax88179_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
316 u16 size, const void *data)
322 buf = *((u16 *)data);
324 ret = __ax88179_write_cmd(dev, cmd, value, index,
327 ret = __ax88179_write_cmd(dev, cmd, value, index,
334 static void ax88179_status(struct usbnet *dev, struct urb *urb)
336 struct ax88179_int_data *event;
339 if (urb->actual_length < 8)
342 event = urb->transfer_buffer;
343 le32_to_cpus((void *)&event->intdata1);
345 link = (((__force u32)event->intdata1) & AX_INT_PPLS_LINK) >> 16;
347 if (netif_carrier_ok(dev->net) != link) {
348 usbnet_link_change(dev, link, 1);
349 netdev_info(dev->net, "ax88179 - Link status is: %d\n", link);
353 static int ax88179_mdio_read(struct net_device *netdev, int phy_id, int loc)
355 struct usbnet *dev = netdev_priv(netdev);
358 ax88179_read_cmd(dev, AX_ACCESS_PHY, phy_id, (__u16)loc, 2, &res);
362 static void ax88179_mdio_write(struct net_device *netdev, int phy_id, int loc,
365 struct usbnet *dev = netdev_priv(netdev);
368 ax88179_write_cmd(dev, AX_ACCESS_PHY, phy_id, (__u16)loc, 2, &res);
371 static inline int ax88179_phy_mmd_indirect(struct usbnet *dev, u16 prtad,
378 ret = ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
379 MII_MMD_CTRL, 2, &tmp16);
382 ret = ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
383 MII_MMD_DATA, 2, &tmp16);
385 tmp16 = devad | MII_MMD_CTRL_NOINCR;
386 ret = ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
387 MII_MMD_CTRL, 2, &tmp16);
393 ax88179_phy_read_mmd_indirect(struct usbnet *dev, u16 prtad, u16 devad)
398 ax88179_phy_mmd_indirect(dev, prtad, devad);
400 ret = ax88179_read_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
401 MII_MMD_DATA, 2, &tmp16);
409 ax88179_phy_write_mmd_indirect(struct usbnet *dev, u16 prtad, u16 devad,
414 ax88179_phy_mmd_indirect(dev, prtad, devad);
416 ret = ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
417 MII_MMD_DATA, 2, &data);
425 static int ax88179_suspend(struct usb_interface *intf, pm_message_t message)
427 struct usbnet *dev = usb_get_intfdata(intf);
431 usbnet_suspend(intf, message);
433 /* Disable RX path */
434 ax88179_read_cmd_nopm(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
436 tmp16 &= ~AX_MEDIUM_RECEIVE_EN;
437 ax88179_write_cmd_nopm(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
440 /* Force bulk-in zero length */
441 ax88179_read_cmd_nopm(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL,
444 tmp16 |= AX_PHYPWR_RSTCTL_BZ | AX_PHYPWR_RSTCTL_IPRL;
445 ax88179_write_cmd_nopm(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL,
450 ax88179_write_cmd_nopm(dev, AX_ACCESS_MAC, AX_CLK_SELECT, 1, 1, &tmp8);
452 /* Configure RX control register => stop operation */
453 tmp16 = AX_RX_CTL_STOP;
454 ax88179_write_cmd_nopm(dev, AX_ACCESS_MAC, AX_RX_CTL, 2, 2, &tmp16);
459 /* This function is used to enable the autodetach function. */
460 /* This function is determined by offset 0x43 of EEPROM */
461 static int ax88179_auto_detach(struct usbnet *dev, int in_pm)
465 int (*fnr)(struct usbnet *, u8, u16, u16, u16, void *);
466 int (*fnw)(struct usbnet *, u8, u16, u16, u16, const void *);
469 fnr = ax88179_read_cmd;
470 fnw = ax88179_write_cmd;
472 fnr = ax88179_read_cmd_nopm;
473 fnw = ax88179_write_cmd_nopm;
476 if (fnr(dev, AX_ACCESS_EEPROM, 0x43, 1, 2, &tmp16) < 0)
479 if ((tmp16 == 0xFFFF) || (!(tmp16 & 0x0100)))
482 /* Enable Auto Detach bit */
484 fnr(dev, AX_ACCESS_MAC, AX_CLK_SELECT, 1, 1, &tmp8);
485 tmp8 |= AX_CLK_SELECT_ULR;
486 fnw(dev, AX_ACCESS_MAC, AX_CLK_SELECT, 1, 1, &tmp8);
488 fnr(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL, 2, 2, &tmp16);
489 tmp16 |= AX_PHYPWR_RSTCTL_AT;
490 fnw(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL, 2, 2, &tmp16);
495 static int ax88179_resume(struct usb_interface *intf)
497 struct usbnet *dev = usb_get_intfdata(intf);
501 usbnet_link_change(dev, 0, 0);
503 /* Power up ethernet PHY */
505 ax88179_write_cmd_nopm(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL,
509 tmp16 = AX_PHYPWR_RSTCTL_IPRL;
510 ax88179_write_cmd_nopm(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL,
514 /* Ethernet PHY Auto Detach*/
515 ax88179_auto_detach(dev, 1);
518 ax88179_read_cmd_nopm(dev, AX_ACCESS_MAC, AX_CLK_SELECT, 1, 1, &tmp8);
519 tmp8 |= AX_CLK_SELECT_ACS | AX_CLK_SELECT_BCS;
520 ax88179_write_cmd_nopm(dev, AX_ACCESS_MAC, AX_CLK_SELECT, 1, 1, &tmp8);
523 /* Configure RX control register => start operation */
524 tmp16 = AX_RX_CTL_DROPCRCERR | AX_RX_CTL_IPE | AX_RX_CTL_START |
525 AX_RX_CTL_AP | AX_RX_CTL_AMALL | AX_RX_CTL_AB;
526 ax88179_write_cmd_nopm(dev, AX_ACCESS_MAC, AX_RX_CTL, 2, 2, &tmp16);
528 return usbnet_resume(intf);
532 ax88179_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
534 struct usbnet *dev = netdev_priv(net);
537 if (ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_MONITOR_MOD,
539 wolinfo->supported = 0;
540 wolinfo->wolopts = 0;
544 wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
545 wolinfo->wolopts = 0;
546 if (opt & AX_MONITOR_MODE_RWLC)
547 wolinfo->wolopts |= WAKE_PHY;
548 if (opt & AX_MONITOR_MODE_RWMP)
549 wolinfo->wolopts |= WAKE_MAGIC;
553 ax88179_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
555 struct usbnet *dev = netdev_priv(net);
558 if (wolinfo->wolopts & ~(WAKE_PHY | WAKE_MAGIC))
561 if (wolinfo->wolopts & WAKE_PHY)
562 opt |= AX_MONITOR_MODE_RWLC;
563 if (wolinfo->wolopts & WAKE_MAGIC)
564 opt |= AX_MONITOR_MODE_RWMP;
566 if (ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_MONITOR_MOD,
573 static int ax88179_get_eeprom_len(struct net_device *net)
575 return AX_EEPROM_LEN;
579 ax88179_get_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
582 struct usbnet *dev = netdev_priv(net);
584 int first_word, last_word;
587 if (eeprom->len == 0)
590 eeprom->magic = AX88179_EEPROM_MAGIC;
592 first_word = eeprom->offset >> 1;
593 last_word = (eeprom->offset + eeprom->len - 1) >> 1;
594 eeprom_buff = kmalloc_array(last_word - first_word + 1, sizeof(u16),
599 /* ax88179/178A returns 2 bytes from eeprom on read */
600 for (i = first_word; i <= last_word; i++) {
601 ret = __ax88179_read_cmd(dev, AX_ACCESS_EEPROM, i, 1, 2,
602 &eeprom_buff[i - first_word],
610 memcpy(data, (u8 *)eeprom_buff + (eeprom->offset & 1), eeprom->len);
616 ax88179_set_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
619 struct usbnet *dev = netdev_priv(net);
626 netdev_dbg(net, "write EEPROM len %d, offset %d, magic 0x%x\n",
627 eeprom->len, eeprom->offset, eeprom->magic);
629 if (eeprom->len == 0)
632 if (eeprom->magic != AX88179_EEPROM_MAGIC)
635 first_word = eeprom->offset >> 1;
636 last_word = (eeprom->offset + eeprom->len - 1) >> 1;
638 eeprom_buff = kmalloc_array(last_word - first_word + 1, sizeof(u16),
643 /* align data to 16 bit boundaries, read the missing data from
645 if (eeprom->offset & 1) {
646 ret = ax88179_read_cmd(dev, AX_ACCESS_EEPROM, first_word, 1, 2,
649 netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", first_word);
654 if ((eeprom->offset + eeprom->len) & 1) {
655 ret = ax88179_read_cmd(dev, AX_ACCESS_EEPROM, last_word, 1, 2,
656 &eeprom_buff[last_word - first_word]);
658 netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", last_word);
663 memcpy((u8 *)eeprom_buff + (eeprom->offset & 1), data, eeprom->len);
665 for (i = first_word; i <= last_word; i++) {
666 netdev_dbg(net, "write to EEPROM at offset 0x%02x, data 0x%04x\n",
667 i, eeprom_buff[i - first_word]);
668 ret = ax88179_write_cmd(dev, AX_ACCESS_EEPROM, i, 1, 2,
669 &eeprom_buff[i - first_word]);
671 netdev_err(net, "Failed to write EEPROM at offset 0x%02x.\n", i);
677 /* reload EEPROM data */
678 ret = ax88179_write_cmd(dev, AX_RELOAD_EEPROM_EFUSE, 0x0000, 0, 0, NULL);
680 netdev_err(net, "Failed to reload EEPROM data\n");
690 static int ax88179_get_link_ksettings(struct net_device *net,
691 struct ethtool_link_ksettings *cmd)
693 struct usbnet *dev = netdev_priv(net);
695 mii_ethtool_get_link_ksettings(&dev->mii, cmd);
700 static int ax88179_set_link_ksettings(struct net_device *net,
701 const struct ethtool_link_ksettings *cmd)
703 struct usbnet *dev = netdev_priv(net);
704 return mii_ethtool_set_link_ksettings(&dev->mii, cmd);
708 ax88179_ethtool_get_eee(struct usbnet *dev, struct ethtool_eee *data)
712 /* Get Supported EEE */
713 val = ax88179_phy_read_mmd_indirect(dev, MDIO_PCS_EEE_ABLE,
717 data->supported = mmd_eee_cap_to_ethtool_sup_t(val);
719 /* Get advertisement EEE */
720 val = ax88179_phy_read_mmd_indirect(dev, MDIO_AN_EEE_ADV,
724 data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
726 /* Get LP advertisement EEE */
727 val = ax88179_phy_read_mmd_indirect(dev, MDIO_AN_EEE_LPABLE,
731 data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
737 ax88179_ethtool_set_eee(struct usbnet *dev, struct ethtool_eee *data)
739 u16 tmp16 = ethtool_adv_to_mmd_eee_adv_t(data->advertised);
741 return ax88179_phy_write_mmd_indirect(dev, MDIO_AN_EEE_ADV,
745 static int ax88179_chk_eee(struct usbnet *dev)
747 struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
748 struct ax88179_data *priv = (struct ax88179_data *)dev->data;
750 mii_ethtool_gset(&dev->mii, &ecmd);
752 if (ecmd.duplex & DUPLEX_FULL) {
753 int eee_lp, eee_cap, eee_adv;
754 u32 lp, cap, adv, supported = 0;
756 eee_cap = ax88179_phy_read_mmd_indirect(dev,
760 priv->eee_active = 0;
764 cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap);
766 priv->eee_active = 0;
770 eee_lp = ax88179_phy_read_mmd_indirect(dev,
774 priv->eee_active = 0;
778 eee_adv = ax88179_phy_read_mmd_indirect(dev,
783 priv->eee_active = 0;
787 adv = mmd_eee_adv_to_ethtool_adv_t(eee_adv);
788 lp = mmd_eee_adv_to_ethtool_adv_t(eee_lp);
789 supported = (ecmd.speed == SPEED_1000) ?
790 SUPPORTED_1000baseT_Full :
791 SUPPORTED_100baseT_Full;
793 if (!(lp & adv & supported)) {
794 priv->eee_active = 0;
798 priv->eee_active = 1;
802 priv->eee_active = 0;
806 static void ax88179_disable_eee(struct usbnet *dev)
810 tmp16 = GMII_PHY_PGSEL_PAGE3;
811 ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
812 GMII_PHY_PAGE_SELECT, 2, &tmp16);
815 ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
816 MII_PHYADDR, 2, &tmp16);
818 tmp16 = GMII_PHY_PGSEL_PAGE0;
819 ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
820 GMII_PHY_PAGE_SELECT, 2, &tmp16);
823 static void ax88179_enable_eee(struct usbnet *dev)
827 tmp16 = GMII_PHY_PGSEL_PAGE3;
828 ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
829 GMII_PHY_PAGE_SELECT, 2, &tmp16);
832 ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
833 MII_PHYADDR, 2, &tmp16);
835 tmp16 = GMII_PHY_PGSEL_PAGE5;
836 ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
837 GMII_PHY_PAGE_SELECT, 2, &tmp16);
840 ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
841 MII_BMSR, 2, &tmp16);
843 tmp16 = GMII_PHY_PGSEL_PAGE0;
844 ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
845 GMII_PHY_PAGE_SELECT, 2, &tmp16);
848 static int ax88179_get_eee(struct net_device *net, struct ethtool_eee *edata)
850 struct usbnet *dev = netdev_priv(net);
851 struct ax88179_data *priv = (struct ax88179_data *)dev->data;
853 edata->eee_enabled = priv->eee_enabled;
854 edata->eee_active = priv->eee_active;
856 return ax88179_ethtool_get_eee(dev, edata);
859 static int ax88179_set_eee(struct net_device *net, struct ethtool_eee *edata)
861 struct usbnet *dev = netdev_priv(net);
862 struct ax88179_data *priv = (struct ax88179_data *)dev->data;
865 priv->eee_enabled = edata->eee_enabled;
866 if (!priv->eee_enabled) {
867 ax88179_disable_eee(dev);
869 priv->eee_enabled = ax88179_chk_eee(dev);
870 if (!priv->eee_enabled)
873 ax88179_enable_eee(dev);
876 ret = ax88179_ethtool_set_eee(dev, edata);
880 mii_nway_restart(&dev->mii);
882 usbnet_link_change(dev, 0, 0);
887 static int ax88179_ioctl(struct net_device *net, struct ifreq *rq, int cmd)
889 struct usbnet *dev = netdev_priv(net);
890 return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
893 static const struct ethtool_ops ax88179_ethtool_ops = {
894 .get_link = ethtool_op_get_link,
895 .get_msglevel = usbnet_get_msglevel,
896 .set_msglevel = usbnet_set_msglevel,
897 .get_wol = ax88179_get_wol,
898 .set_wol = ax88179_set_wol,
899 .get_eeprom_len = ax88179_get_eeprom_len,
900 .get_eeprom = ax88179_get_eeprom,
901 .set_eeprom = ax88179_set_eeprom,
902 .get_eee = ax88179_get_eee,
903 .set_eee = ax88179_set_eee,
904 .nway_reset = usbnet_nway_reset,
905 .get_link_ksettings = ax88179_get_link_ksettings,
906 .set_link_ksettings = ax88179_set_link_ksettings,
907 .get_ts_info = ethtool_op_get_ts_info,
910 static void ax88179_set_multicast(struct net_device *net)
912 struct usbnet *dev = netdev_priv(net);
913 struct ax88179_data *data = (struct ax88179_data *)dev->data;
914 u8 *m_filter = ((u8 *)dev->data) + 12;
916 data->rxctl = (AX_RX_CTL_START | AX_RX_CTL_AB | AX_RX_CTL_IPE);
918 if (net->flags & IFF_PROMISC) {
919 data->rxctl |= AX_RX_CTL_PRO;
920 } else if (net->flags & IFF_ALLMULTI ||
921 netdev_mc_count(net) > AX_MAX_MCAST) {
922 data->rxctl |= AX_RX_CTL_AMALL;
923 } else if (netdev_mc_empty(net)) {
924 /* just broadcast and directed */
926 /* We use the 20 byte dev->data for our 8 byte filter buffer
927 * to avoid allocating memory that is tricky to free later
930 struct netdev_hw_addr *ha;
932 memset(m_filter, 0, AX_MCAST_FLTSIZE);
934 netdev_for_each_mc_addr(ha, net) {
935 crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
936 *(m_filter + (crc_bits >> 3)) |= (1 << (crc_bits & 7));
939 ax88179_write_cmd_async(dev, AX_ACCESS_MAC, AX_MULFLTARY,
940 AX_MCAST_FLTSIZE, AX_MCAST_FLTSIZE,
943 data->rxctl |= AX_RX_CTL_AM;
946 ax88179_write_cmd_async(dev, AX_ACCESS_MAC, AX_RX_CTL,
951 ax88179_set_features(struct net_device *net, netdev_features_t features)
954 struct usbnet *dev = netdev_priv(net);
955 netdev_features_t changed = net->features ^ features;
957 if (changed & NETIF_F_IP_CSUM) {
958 ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_TXCOE_CTL, 1, 1, &tmp);
959 tmp ^= AX_TXCOE_TCP | AX_TXCOE_UDP;
960 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_TXCOE_CTL, 1, 1, &tmp);
963 if (changed & NETIF_F_IPV6_CSUM) {
964 ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_TXCOE_CTL, 1, 1, &tmp);
965 tmp ^= AX_TXCOE_TCPV6 | AX_TXCOE_UDPV6;
966 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_TXCOE_CTL, 1, 1, &tmp);
969 if (changed & NETIF_F_RXCSUM) {
970 ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_RXCOE_CTL, 1, 1, &tmp);
971 tmp ^= AX_RXCOE_IP | AX_RXCOE_TCP | AX_RXCOE_UDP |
972 AX_RXCOE_TCPV6 | AX_RXCOE_UDPV6;
973 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RXCOE_CTL, 1, 1, &tmp);
979 static int ax88179_change_mtu(struct net_device *net, int new_mtu)
981 struct usbnet *dev = netdev_priv(net);
985 dev->hard_mtu = net->mtu + net->hard_header_len;
987 if (net->mtu > 1500) {
988 ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
990 tmp16 |= AX_MEDIUM_JUMBO_EN;
991 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
994 ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
996 tmp16 &= ~AX_MEDIUM_JUMBO_EN;
997 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
1001 /* max qlen depend on hard_mtu and rx_urb_size */
1002 usbnet_update_max_qlen(dev);
1007 static int ax88179_set_mac_addr(struct net_device *net, void *p)
1009 struct usbnet *dev = netdev_priv(net);
1010 struct sockaddr *addr = p;
1013 if (netif_running(net))
1015 if (!is_valid_ether_addr(addr->sa_data))
1016 return -EADDRNOTAVAIL;
1018 eth_hw_addr_set(net, addr->sa_data);
1020 /* Set the MAC address */
1021 ret = ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_NODE_ID, ETH_ALEN,
1022 ETH_ALEN, net->dev_addr);
1029 static const struct net_device_ops ax88179_netdev_ops = {
1030 .ndo_open = usbnet_open,
1031 .ndo_stop = usbnet_stop,
1032 .ndo_start_xmit = usbnet_start_xmit,
1033 .ndo_tx_timeout = usbnet_tx_timeout,
1034 .ndo_get_stats64 = dev_get_tstats64,
1035 .ndo_change_mtu = ax88179_change_mtu,
1036 .ndo_set_mac_address = ax88179_set_mac_addr,
1037 .ndo_validate_addr = eth_validate_addr,
1038 .ndo_eth_ioctl = ax88179_ioctl,
1039 .ndo_set_rx_mode = ax88179_set_multicast,
1040 .ndo_set_features = ax88179_set_features,
1043 static int ax88179_check_eeprom(struct usbnet *dev)
1045 u8 i, buf, eeprom[20];
1046 u16 csum, delay = HZ / 10;
1047 unsigned long jtimeout;
1049 /* Read EEPROM content */
1050 for (i = 0; i < 6; i++) {
1052 if (ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_SROM_ADDR,
1057 if (ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_SROM_CMD,
1061 jtimeout = jiffies + delay;
1063 ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_SROM_CMD,
1066 if (time_after(jiffies, jtimeout))
1069 } while (buf & EEP_BUSY);
1071 __ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_SROM_DATA_LOW,
1072 2, 2, &eeprom[i * 2], 0);
1074 if ((i == 0) && (eeprom[0] == 0xFF))
1078 csum = eeprom[6] + eeprom[7] + eeprom[8] + eeprom[9];
1079 csum = (csum >> 8) + (csum & 0xff);
1080 if ((csum + eeprom[10]) != 0xff)
1086 static int ax88179_check_efuse(struct usbnet *dev, u16 *ledmode)
1092 if (ax88179_read_cmd(dev, AX_ACCESS_EFUS, 0, 64, 64, efuse) < 0)
1098 for (i = 0; i < 64; i++)
1099 csum = csum + efuse[i];
1102 csum = (csum & 0x00FF) + ((csum >> 8) & 0x00FF);
1107 *ledmode = (efuse[51] << 8) | efuse[52];
1112 static int ax88179_convert_old_led(struct usbnet *dev, u16 *ledvalue)
1116 /* Loaded the old eFuse LED Mode */
1117 if (ax88179_read_cmd(dev, AX_ACCESS_EEPROM, 0x3C, 1, 2, &led) < 0)
1123 led = LED0_ACTIVE | LED1_LINK_10 | LED1_LINK_100 |
1124 LED1_LINK_1000 | LED2_ACTIVE | LED2_LINK_10 |
1125 LED2_LINK_100 | LED2_LINK_1000 | LED_VALID;
1128 led = LED0_ACTIVE | LED1_LINK_1000 | LED2_LINK_100 | LED_VALID;
1131 led = LED0_ACTIVE | LED1_LINK_1000 | LED2_LINK_100 |
1132 LED2_LINK_10 | LED_VALID;
1135 led = LED0_ACTIVE | LED1_ACTIVE | LED1_LINK_1000 | LED2_ACTIVE |
1136 LED2_LINK_100 | LED2_LINK_10 | LED_VALID;
1139 led = LED0_ACTIVE | LED1_LINK_10 | LED1_LINK_100 |
1140 LED1_LINK_1000 | LED2_ACTIVE | LED2_LINK_10 |
1141 LED2_LINK_100 | LED2_LINK_1000 | LED_VALID;
1150 static int ax88179_led_setting(struct usbnet *dev)
1152 u8 ledfd, value = 0;
1153 u16 tmp, ledact, ledlink, ledvalue = 0, delay = HZ / 10;
1154 unsigned long jtimeout;
1156 /* Check AX88179 version. UA1 or UA2*/
1157 ax88179_read_cmd(dev, AX_ACCESS_MAC, GENERAL_STATUS, 1, 1, &value);
1159 if (!(value & AX_SECLD)) { /* UA1 */
1160 value = AX_GPIO_CTRL_GPIO3EN | AX_GPIO_CTRL_GPIO2EN |
1161 AX_GPIO_CTRL_GPIO1EN;
1162 if (ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_GPIO_CTRL,
1168 if (!ax88179_check_eeprom(dev)) {
1170 if (ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_SROM_ADDR,
1175 if (ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_SROM_CMD,
1179 jtimeout = jiffies + delay;
1181 ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_SROM_CMD,
1184 if (time_after(jiffies, jtimeout))
1187 } while (value & EEP_BUSY);
1189 ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_SROM_DATA_HIGH,
1191 ledvalue = (value << 8);
1193 ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_SROM_DATA_LOW,
1197 /* load internal ROM for defaule setting */
1198 if ((ledvalue == 0xFFFF) || ((ledvalue & LED_VALID) == 0))
1199 ax88179_convert_old_led(dev, &ledvalue);
1201 } else if (!ax88179_check_efuse(dev, &ledvalue)) {
1202 if ((ledvalue == 0xFFFF) || ((ledvalue & LED_VALID) == 0))
1203 ax88179_convert_old_led(dev, &ledvalue);
1205 ax88179_convert_old_led(dev, &ledvalue);
1208 tmp = GMII_PHY_PGSEL_EXT;
1209 ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
1210 GMII_PHY_PAGE_SELECT, 2, &tmp);
1213 ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
1214 GMII_PHYPAGE, 2, &tmp);
1216 ax88179_read_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
1217 GMII_LED_ACT, 2, &ledact);
1219 ax88179_read_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
1220 GMII_LED_LINK, 2, &ledlink);
1222 ledact &= GMII_LED_ACTIVE_MASK;
1223 ledlink &= GMII_LED_LINK_MASK;
1225 if (ledvalue & LED0_ACTIVE)
1226 ledact |= GMII_LED0_ACTIVE;
1228 if (ledvalue & LED1_ACTIVE)
1229 ledact |= GMII_LED1_ACTIVE;
1231 if (ledvalue & LED2_ACTIVE)
1232 ledact |= GMII_LED2_ACTIVE;
1234 if (ledvalue & LED0_LINK_10)
1235 ledlink |= GMII_LED0_LINK_10;
1237 if (ledvalue & LED1_LINK_10)
1238 ledlink |= GMII_LED1_LINK_10;
1240 if (ledvalue & LED2_LINK_10)
1241 ledlink |= GMII_LED2_LINK_10;
1243 if (ledvalue & LED0_LINK_100)
1244 ledlink |= GMII_LED0_LINK_100;
1246 if (ledvalue & LED1_LINK_100)
1247 ledlink |= GMII_LED1_LINK_100;
1249 if (ledvalue & LED2_LINK_100)
1250 ledlink |= GMII_LED2_LINK_100;
1252 if (ledvalue & LED0_LINK_1000)
1253 ledlink |= GMII_LED0_LINK_1000;
1255 if (ledvalue & LED1_LINK_1000)
1256 ledlink |= GMII_LED1_LINK_1000;
1258 if (ledvalue & LED2_LINK_1000)
1259 ledlink |= GMII_LED2_LINK_1000;
1262 ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
1263 GMII_LED_ACT, 2, &tmp);
1266 ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
1267 GMII_LED_LINK, 2, &tmp);
1269 tmp = GMII_PHY_PGSEL_PAGE0;
1270 ax88179_write_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
1271 GMII_PHY_PAGE_SELECT, 2, &tmp);
1273 /* LED full duplex setting */
1275 if (ledvalue & LED0_FD)
1277 else if ((ledvalue & LED0_USB3_MASK) == 0)
1280 if (ledvalue & LED1_FD)
1282 else if ((ledvalue & LED1_USB3_MASK) == 0)
1285 if (ledvalue & LED2_FD)
1287 else if ((ledvalue & LED2_USB3_MASK) == 0)
1290 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_LEDCTRL, 1, 1, &ledfd);
1295 static void ax88179_get_mac_addr(struct usbnet *dev)
1299 memset(mac, 0, sizeof(mac));
1301 /* Maybe the boot loader passed the MAC address via device tree */
1302 if (!eth_platform_get_mac_address(&dev->udev->dev, mac)) {
1303 netif_dbg(dev, ifup, dev->net,
1304 "MAC address read from device tree");
1306 ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_NODE_ID, ETH_ALEN,
1308 netif_dbg(dev, ifup, dev->net,
1309 "MAC address read from ASIX chip");
1312 if (is_valid_ether_addr(mac)) {
1313 eth_hw_addr_set(dev->net, mac);
1315 netdev_info(dev->net, "invalid MAC address, using random\n");
1316 eth_hw_addr_random(dev->net);
1319 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_NODE_ID, ETH_ALEN, ETH_ALEN,
1320 dev->net->dev_addr);
1323 static int ax88179_bind(struct usbnet *dev, struct usb_interface *intf)
1328 struct ax88179_data *ax179_data = (struct ax88179_data *)dev->data;
1329 struct ethtool_eee eee_data;
1331 usbnet_get_endpoints(dev, intf);
1336 memset(ax179_data, 0, sizeof(*ax179_data));
1338 /* Power up ethernet PHY */
1340 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL, 2, 2, tmp16);
1341 *tmp16 = AX_PHYPWR_RSTCTL_IPRL;
1342 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL, 2, 2, tmp16);
1345 *tmp = AX_CLK_SELECT_ACS | AX_CLK_SELECT_BCS;
1346 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_CLK_SELECT, 1, 1, tmp);
1349 /* Read MAC address from DTB or asix chip */
1350 ax88179_get_mac_addr(dev);
1351 memcpy(dev->net->perm_addr, dev->net->dev_addr, ETH_ALEN);
1353 /* RX bulk configuration */
1354 memcpy(tmp, &AX88179_BULKIN_SIZE[0], 5);
1355 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RX_BULKIN_QCTRL, 5, 5, tmp);
1357 dev->rx_urb_size = 1024 * 20;
1360 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_PAUSE_WATERLVL_LOW, 1, 1, tmp);
1363 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_PAUSE_WATERLVL_HIGH,
1366 dev->net->netdev_ops = &ax88179_netdev_ops;
1367 dev->net->ethtool_ops = &ax88179_ethtool_ops;
1368 dev->net->needed_headroom = 8;
1369 dev->net->max_mtu = 4088;
1371 /* Initialize MII structure */
1372 dev->mii.dev = dev->net;
1373 dev->mii.mdio_read = ax88179_mdio_read;
1374 dev->mii.mdio_write = ax88179_mdio_write;
1375 dev->mii.phy_id_mask = 0xff;
1376 dev->mii.reg_num_mask = 0xff;
1377 dev->mii.phy_id = 0x03;
1378 dev->mii.supports_gmii = 1;
1380 dev->net->features |= NETIF_F_SG | NETIF_F_IP_CSUM |
1381 NETIF_F_IPV6_CSUM | NETIF_F_RXCSUM | NETIF_F_TSO;
1383 dev->net->hw_features |= dev->net->features;
1385 netif_set_tso_max_size(dev->net, 16384);
1387 /* Enable checksum offload */
1388 *tmp = AX_RXCOE_IP | AX_RXCOE_TCP | AX_RXCOE_UDP |
1389 AX_RXCOE_TCPV6 | AX_RXCOE_UDPV6;
1390 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RXCOE_CTL, 1, 1, tmp);
1392 *tmp = AX_TXCOE_IP | AX_TXCOE_TCP | AX_TXCOE_UDP |
1393 AX_TXCOE_TCPV6 | AX_TXCOE_UDPV6;
1394 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_TXCOE_CTL, 1, 1, tmp);
1396 /* Configure RX control register => start operation */
1397 *tmp16 = AX_RX_CTL_DROPCRCERR | AX_RX_CTL_IPE | AX_RX_CTL_START |
1398 AX_RX_CTL_AP | AX_RX_CTL_AMALL | AX_RX_CTL_AB;
1399 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RX_CTL, 2, 2, tmp16);
1401 *tmp = AX_MONITOR_MODE_PMETYPE | AX_MONITOR_MODE_PMEPOL |
1402 AX_MONITOR_MODE_RWMP;
1403 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_MONITOR_MOD, 1, 1, tmp);
1405 /* Configure default medium type => giga */
1406 *tmp16 = AX_MEDIUM_RECEIVE_EN | AX_MEDIUM_TXFLOW_CTRLEN |
1407 AX_MEDIUM_RXFLOW_CTRLEN | AX_MEDIUM_FULL_DUPLEX |
1409 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
1412 ax88179_led_setting(dev);
1414 ax179_data->eee_enabled = 0;
1415 ax179_data->eee_active = 0;
1417 ax88179_disable_eee(dev);
1419 ax88179_ethtool_get_eee(dev, &eee_data);
1420 eee_data.advertised = 0;
1421 ax88179_ethtool_set_eee(dev, &eee_data);
1423 /* Restart autoneg */
1424 mii_nway_restart(&dev->mii);
1426 usbnet_link_change(dev, 0, 0);
1431 static void ax88179_unbind(struct usbnet *dev, struct usb_interface *intf)
1435 /* Configure RX control register => stop operation */
1436 tmp16 = AX_RX_CTL_STOP;
1437 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RX_CTL, 2, 2, &tmp16);
1440 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_CLK_SELECT, 1, 1, &tmp16);
1442 /* Power down ethernet PHY */
1444 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL, 2, 2, &tmp16);
1448 ax88179_rx_checksum(struct sk_buff *skb, u32 *pkt_hdr)
1450 skb->ip_summed = CHECKSUM_NONE;
1452 /* checksum error bit is set */
1453 if ((*pkt_hdr & AX_RXHDR_L3CSUM_ERR) ||
1454 (*pkt_hdr & AX_RXHDR_L4CSUM_ERR))
1457 /* It must be a TCP or UDP packet with a valid checksum */
1458 if (((*pkt_hdr & AX_RXHDR_L4_TYPE_MASK) == AX_RXHDR_L4_TYPE_TCP) ||
1459 ((*pkt_hdr & AX_RXHDR_L4_TYPE_MASK) == AX_RXHDR_L4_TYPE_UDP))
1460 skb->ip_summed = CHECKSUM_UNNECESSARY;
1463 static int ax88179_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
1465 struct sk_buff *ax_skb;
1471 /* At the end of the SKB, there's a header telling us how many packets
1472 * are bundled into this buffer and where we can find an array of
1473 * per-packet metadata (which contains elements encoded into u16).
1477 skb_trim(skb, skb->len - 4);
1478 rx_hdr = get_unaligned_le32(skb_tail_pointer(skb));
1479 pkt_cnt = (u16)rx_hdr;
1480 hdr_off = (u16)(rx_hdr >> 16);
1485 /* Make sure that the bounds of the metadata array are inside the SKB
1486 * (and in front of the counter at the end).
1488 if (pkt_cnt * 2 + hdr_off > skb->len)
1490 pkt_hdr = (u32 *)(skb->data + hdr_off);
1492 /* Packets must not overlap the metadata array */
1493 skb_trim(skb, hdr_off);
1495 for (; ; pkt_cnt--, pkt_hdr++) {
1498 le32_to_cpus(pkt_hdr);
1499 pkt_len = (*pkt_hdr >> 16) & 0x1fff;
1501 if (pkt_len > skb->len)
1504 /* Check CRC or runt packet */
1505 if (((*pkt_hdr & (AX_RXHDR_CRC_ERR | AX_RXHDR_DROP_ERR)) == 0) &&
1506 pkt_len >= 2 + ETH_HLEN) {
1507 bool last = (pkt_cnt == 0);
1512 ax_skb = skb_clone(skb, GFP_ATOMIC);
1516 ax_skb->len = pkt_len;
1517 /* Skip IP alignment pseudo header */
1518 skb_pull(ax_skb, 2);
1519 skb_set_tail_pointer(ax_skb, ax_skb->len);
1520 ax_skb->truesize = pkt_len + sizeof(struct sk_buff);
1521 ax88179_rx_checksum(ax_skb, pkt_hdr);
1526 usbnet_skb_return(dev, ax_skb);
1529 /* Trim this packet away from the SKB */
1530 if (!skb_pull(skb, (pkt_len + 7) & 0xFFF8))
1535 static struct sk_buff *
1536 ax88179_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
1538 u32 tx_hdr1, tx_hdr2;
1539 int frame_size = dev->maxpacket;
1544 tx_hdr2 = skb_shinfo(skb)->gso_size; /* Set TSO mss */
1545 if (((skb->len + 8) % frame_size) == 0)
1546 tx_hdr2 |= 0x80008000; /* Enable padding */
1548 headroom = skb_headroom(skb) - 8;
1550 if ((dev->net->features & NETIF_F_SG) && skb_linearize(skb))
1553 if ((skb_header_cloned(skb) || headroom < 0) &&
1554 pskb_expand_head(skb, headroom < 0 ? 8 : 0, 0, GFP_ATOMIC)) {
1555 dev_kfree_skb_any(skb);
1559 ptr = skb_push(skb, 8);
1560 put_unaligned_le32(tx_hdr1, ptr);
1561 put_unaligned_le32(tx_hdr2, ptr + 4);
1563 usbnet_set_skb_tx_stats(skb, (skb_shinfo(skb)->gso_segs ?: 1), 0);
1568 static int ax88179_link_reset(struct usbnet *dev)
1570 struct ax88179_data *ax179_data = (struct ax88179_data *)dev->data;
1571 u8 tmp[5], link_sts;
1572 u16 mode, tmp16, delay = HZ / 10;
1573 u32 tmp32 = 0x40000000;
1574 unsigned long jtimeout;
1576 jtimeout = jiffies + delay;
1577 while (tmp32 & 0x40000000) {
1579 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RX_CTL, 2, 2, &mode);
1580 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RX_CTL, 2, 2,
1581 &ax179_data->rxctl);
1583 /*link up, check the usb device control TX FIFO full or empty*/
1584 ax88179_read_cmd(dev, 0x81, 0x8c, 0, 4, &tmp32);
1586 if (time_after(jiffies, jtimeout))
1590 mode = AX_MEDIUM_RECEIVE_EN | AX_MEDIUM_TXFLOW_CTRLEN |
1591 AX_MEDIUM_RXFLOW_CTRLEN;
1593 ax88179_read_cmd(dev, AX_ACCESS_MAC, PHYSICAL_LINK_STATUS,
1596 ax88179_read_cmd(dev, AX_ACCESS_PHY, AX88179_PHY_ID,
1597 GMII_PHY_PHYSR, 2, &tmp16);
1599 if (!(tmp16 & GMII_PHY_PHYSR_LINK)) {
1601 } else if (GMII_PHY_PHYSR_GIGA == (tmp16 & GMII_PHY_PHYSR_SMASK)) {
1602 mode |= AX_MEDIUM_GIGAMODE | AX_MEDIUM_EN_125MHZ;
1603 if (dev->net->mtu > 1500)
1604 mode |= AX_MEDIUM_JUMBO_EN;
1606 if (link_sts & AX_USB_SS)
1607 memcpy(tmp, &AX88179_BULKIN_SIZE[0], 5);
1608 else if (link_sts & AX_USB_HS)
1609 memcpy(tmp, &AX88179_BULKIN_SIZE[1], 5);
1611 memcpy(tmp, &AX88179_BULKIN_SIZE[3], 5);
1612 } else if (GMII_PHY_PHYSR_100 == (tmp16 & GMII_PHY_PHYSR_SMASK)) {
1613 mode |= AX_MEDIUM_PS;
1615 if (link_sts & (AX_USB_SS | AX_USB_HS))
1616 memcpy(tmp, &AX88179_BULKIN_SIZE[2], 5);
1618 memcpy(tmp, &AX88179_BULKIN_SIZE[3], 5);
1620 memcpy(tmp, &AX88179_BULKIN_SIZE[3], 5);
1623 /* RX bulk configuration */
1624 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RX_BULKIN_QCTRL, 5, 5, tmp);
1626 dev->rx_urb_size = (1024 * (tmp[3] + 2));
1628 if (tmp16 & GMII_PHY_PHYSR_FULL)
1629 mode |= AX_MEDIUM_FULL_DUPLEX;
1630 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
1633 ax179_data->eee_enabled = ax88179_chk_eee(dev);
1635 netif_carrier_on(dev->net);
1640 static int ax88179_reset(struct usbnet *dev)
1645 struct ax88179_data *ax179_data = (struct ax88179_data *)dev->data;
1646 struct ethtool_eee eee_data;
1651 /* Power up ethernet PHY */
1653 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL, 2, 2, tmp16);
1655 *tmp16 = AX_PHYPWR_RSTCTL_IPRL;
1656 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_PHYPWR_RSTCTL, 2, 2, tmp16);
1659 *tmp = AX_CLK_SELECT_ACS | AX_CLK_SELECT_BCS;
1660 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_CLK_SELECT, 1, 1, tmp);
1663 /* Ethernet PHY Auto Detach*/
1664 ax88179_auto_detach(dev, 0);
1666 /* Read MAC address from DTB or asix chip */
1667 ax88179_get_mac_addr(dev);
1669 /* RX bulk configuration */
1670 memcpy(tmp, &AX88179_BULKIN_SIZE[0], 5);
1671 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RX_BULKIN_QCTRL, 5, 5, tmp);
1673 dev->rx_urb_size = 1024 * 20;
1676 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_PAUSE_WATERLVL_LOW, 1, 1, tmp);
1679 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_PAUSE_WATERLVL_HIGH,
1682 dev->net->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
1685 dev->net->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
1688 /* Enable checksum offload */
1689 *tmp = AX_RXCOE_IP | AX_RXCOE_TCP | AX_RXCOE_UDP |
1690 AX_RXCOE_TCPV6 | AX_RXCOE_UDPV6;
1691 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RXCOE_CTL, 1, 1, tmp);
1693 *tmp = AX_TXCOE_IP | AX_TXCOE_TCP | AX_TXCOE_UDP |
1694 AX_TXCOE_TCPV6 | AX_TXCOE_UDPV6;
1695 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_TXCOE_CTL, 1, 1, tmp);
1697 /* Configure RX control register => start operation */
1698 *tmp16 = AX_RX_CTL_DROPCRCERR | AX_RX_CTL_IPE | AX_RX_CTL_START |
1699 AX_RX_CTL_AP | AX_RX_CTL_AMALL | AX_RX_CTL_AB;
1700 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_RX_CTL, 2, 2, tmp16);
1702 *tmp = AX_MONITOR_MODE_PMETYPE | AX_MONITOR_MODE_PMEPOL |
1703 AX_MONITOR_MODE_RWMP;
1704 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_MONITOR_MOD, 1, 1, tmp);
1706 /* Configure default medium type => giga */
1707 *tmp16 = AX_MEDIUM_RECEIVE_EN | AX_MEDIUM_TXFLOW_CTRLEN |
1708 AX_MEDIUM_RXFLOW_CTRLEN | AX_MEDIUM_FULL_DUPLEX |
1710 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
1713 ax88179_led_setting(dev);
1715 ax179_data->eee_enabled = 0;
1716 ax179_data->eee_active = 0;
1718 ax88179_disable_eee(dev);
1720 ax88179_ethtool_get_eee(dev, &eee_data);
1721 eee_data.advertised = 0;
1722 ax88179_ethtool_set_eee(dev, &eee_data);
1724 /* Restart autoneg */
1725 mii_nway_restart(&dev->mii);
1727 usbnet_link_change(dev, 0, 0);
1732 static int ax88179_stop(struct usbnet *dev)
1736 ax88179_read_cmd(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
1738 tmp16 &= ~AX_MEDIUM_RECEIVE_EN;
1739 ax88179_write_cmd(dev, AX_ACCESS_MAC, AX_MEDIUM_STATUS_MODE,
1745 static const struct driver_info ax88179_info = {
1746 .description = "ASIX AX88179 USB 3.0 Gigabit Ethernet",
1747 .bind = ax88179_bind,
1748 .unbind = ax88179_unbind,
1749 .status = ax88179_status,
1750 .link_reset = ax88179_link_reset,
1751 .reset = ax88179_reset,
1752 .stop = ax88179_stop,
1753 .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_SEND_ZLP,
1754 .rx_fixup = ax88179_rx_fixup,
1755 .tx_fixup = ax88179_tx_fixup,
1758 static const struct driver_info ax88178a_info = {
1759 .description = "ASIX AX88178A USB 2.0 Gigabit Ethernet",
1760 .bind = ax88179_bind,
1761 .unbind = ax88179_unbind,
1762 .status = ax88179_status,
1763 .link_reset = ax88179_link_reset,
1764 .reset = ax88179_reset,
1765 .stop = ax88179_stop,
1766 .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_SEND_ZLP,
1767 .rx_fixup = ax88179_rx_fixup,
1768 .tx_fixup = ax88179_tx_fixup,
1771 static const struct driver_info cypress_GX3_info = {
1772 .description = "Cypress GX3 SuperSpeed to Gigabit Ethernet Controller",
1773 .bind = ax88179_bind,
1774 .unbind = ax88179_unbind,
1775 .status = ax88179_status,
1776 .link_reset = ax88179_link_reset,
1777 .reset = ax88179_reset,
1778 .stop = ax88179_stop,
1779 .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_SEND_ZLP,
1780 .rx_fixup = ax88179_rx_fixup,
1781 .tx_fixup = ax88179_tx_fixup,
1784 static const struct driver_info dlink_dub1312_info = {
1785 .description = "D-Link DUB-1312 USB 3.0 to Gigabit Ethernet Adapter",
1786 .bind = ax88179_bind,
1787 .unbind = ax88179_unbind,
1788 .status = ax88179_status,
1789 .link_reset = ax88179_link_reset,
1790 .reset = ax88179_reset,
1791 .stop = ax88179_stop,
1792 .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_SEND_ZLP,
1793 .rx_fixup = ax88179_rx_fixup,
1794 .tx_fixup = ax88179_tx_fixup,
1797 static const struct driver_info sitecom_info = {
1798 .description = "Sitecom USB 3.0 to Gigabit Adapter",
1799 .bind = ax88179_bind,
1800 .unbind = ax88179_unbind,
1801 .status = ax88179_status,
1802 .link_reset = ax88179_link_reset,
1803 .reset = ax88179_reset,
1804 .stop = ax88179_stop,
1805 .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_SEND_ZLP,
1806 .rx_fixup = ax88179_rx_fixup,
1807 .tx_fixup = ax88179_tx_fixup,
1810 static const struct driver_info samsung_info = {
1811 .description = "Samsung USB Ethernet Adapter",
1812 .bind = ax88179_bind,
1813 .unbind = ax88179_unbind,
1814 .status = ax88179_status,
1815 .link_reset = ax88179_link_reset,
1816 .reset = ax88179_reset,
1817 .stop = ax88179_stop,
1818 .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_SEND_ZLP,
1819 .rx_fixup = ax88179_rx_fixup,
1820 .tx_fixup = ax88179_tx_fixup,
1823 static const struct driver_info lenovo_info = {
1824 .description = "Lenovo OneLinkDock Gigabit LAN",
1825 .bind = ax88179_bind,
1826 .unbind = ax88179_unbind,
1827 .status = ax88179_status,
1828 .link_reset = ax88179_link_reset,
1829 .reset = ax88179_reset,
1830 .stop = ax88179_stop,
1831 .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_SEND_ZLP,
1832 .rx_fixup = ax88179_rx_fixup,
1833 .tx_fixup = ax88179_tx_fixup,
1836 static const struct driver_info belkin_info = {
1837 .description = "Belkin USB Ethernet Adapter",
1838 .bind = ax88179_bind,
1839 .unbind = ax88179_unbind,
1840 .status = ax88179_status,
1841 .link_reset = ax88179_link_reset,
1842 .reset = ax88179_reset,
1843 .stop = ax88179_stop,
1844 .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_SEND_ZLP,
1845 .rx_fixup = ax88179_rx_fixup,
1846 .tx_fixup = ax88179_tx_fixup,
1849 static const struct driver_info toshiba_info = {
1850 .description = "Toshiba USB Ethernet Adapter",
1851 .bind = ax88179_bind,
1852 .unbind = ax88179_unbind,
1853 .status = ax88179_status,
1854 .link_reset = ax88179_link_reset,
1855 .reset = ax88179_reset,
1856 .stop = ax88179_stop,
1857 .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_SEND_ZLP,
1858 .rx_fixup = ax88179_rx_fixup,
1859 .tx_fixup = ax88179_tx_fixup,
1862 static const struct driver_info mct_info = {
1863 .description = "MCT USB 3.0 Gigabit Ethernet Adapter",
1864 .bind = ax88179_bind,
1865 .unbind = ax88179_unbind,
1866 .status = ax88179_status,
1867 .link_reset = ax88179_link_reset,
1868 .reset = ax88179_reset,
1869 .stop = ax88179_stop,
1870 .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_SEND_ZLP,
1871 .rx_fixup = ax88179_rx_fixup,
1872 .tx_fixup = ax88179_tx_fixup,
1875 static const struct driver_info at_umc2000_info = {
1876 .description = "AT-UMC2000 USB 3.0/USB 3.1 Gen 1 to Gigabit Ethernet Adapter",
1877 .bind = ax88179_bind,
1878 .unbind = ax88179_unbind,
1879 .status = ax88179_status,
1880 .link_reset = ax88179_link_reset,
1881 .reset = ax88179_reset,
1882 .stop = ax88179_stop,
1883 .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_SEND_ZLP,
1884 .rx_fixup = ax88179_rx_fixup,
1885 .tx_fixup = ax88179_tx_fixup,
1888 static const struct driver_info at_umc200_info = {
1889 .description = "AT-UMC200 USB 3.0/USB 3.1 Gen 1 to Fast Ethernet Adapter",
1890 .bind = ax88179_bind,
1891 .unbind = ax88179_unbind,
1892 .status = ax88179_status,
1893 .link_reset = ax88179_link_reset,
1894 .reset = ax88179_reset,
1895 .stop = ax88179_stop,
1896 .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_SEND_ZLP,
1897 .rx_fixup = ax88179_rx_fixup,
1898 .tx_fixup = ax88179_tx_fixup,
1901 static const struct driver_info at_umc2000sp_info = {
1902 .description = "AT-UMC2000/SP USB 3.0/USB 3.1 Gen 1 to Gigabit Ethernet Adapter",
1903 .bind = ax88179_bind,
1904 .unbind = ax88179_unbind,
1905 .status = ax88179_status,
1906 .link_reset = ax88179_link_reset,
1907 .reset = ax88179_reset,
1908 .stop = ax88179_stop,
1909 .flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_SEND_ZLP,
1910 .rx_fixup = ax88179_rx_fixup,
1911 .tx_fixup = ax88179_tx_fixup,
1914 static const struct usb_device_id products[] = {
1916 /* ASIX AX88179 10/100/1000 */
1917 USB_DEVICE(0x0b95, 0x1790),
1918 .driver_info = (unsigned long)&ax88179_info,
1920 /* ASIX AX88178A 10/100/1000 */
1921 USB_DEVICE(0x0b95, 0x178a),
1922 .driver_info = (unsigned long)&ax88178a_info,
1924 /* Cypress GX3 SuperSpeed to Gigabit Ethernet Bridge Controller */
1925 USB_DEVICE(0x04b4, 0x3610),
1926 .driver_info = (unsigned long)&cypress_GX3_info,
1928 /* D-Link DUB-1312 USB 3.0 to Gigabit Ethernet Adapter */
1929 USB_DEVICE(0x2001, 0x4a00),
1930 .driver_info = (unsigned long)&dlink_dub1312_info,
1932 /* Sitecom USB 3.0 to Gigabit Adapter */
1933 USB_DEVICE(0x0df6, 0x0072),
1934 .driver_info = (unsigned long)&sitecom_info,
1936 /* Samsung USB Ethernet Adapter */
1937 USB_DEVICE(0x04e8, 0xa100),
1938 .driver_info = (unsigned long)&samsung_info,
1940 /* Lenovo OneLinkDock Gigabit LAN */
1941 USB_DEVICE(0x17ef, 0x304b),
1942 .driver_info = (unsigned long)&lenovo_info,
1944 /* Belkin B2B128 USB 3.0 Hub + Gigabit Ethernet Adapter */
1945 USB_DEVICE(0x050d, 0x0128),
1946 .driver_info = (unsigned long)&belkin_info,
1948 /* Toshiba USB 3.0 GBit Ethernet Adapter */
1949 USB_DEVICE(0x0930, 0x0a13),
1950 .driver_info = (unsigned long)&toshiba_info,
1952 /* Magic Control Technology U3-A9003 USB 3.0 Gigabit Ethernet Adapter */
1953 USB_DEVICE(0x0711, 0x0179),
1954 .driver_info = (unsigned long)&mct_info,
1956 /* Allied Telesis AT-UMC2000 USB 3.0/USB 3.1 Gen 1 to Gigabit Ethernet Adapter */
1957 USB_DEVICE(0x07c9, 0x000e),
1958 .driver_info = (unsigned long)&at_umc2000_info,
1960 /* Allied Telesis AT-UMC200 USB 3.0/USB 3.1 Gen 1 to Fast Ethernet Adapter */
1961 USB_DEVICE(0x07c9, 0x000f),
1962 .driver_info = (unsigned long)&at_umc200_info,
1964 /* Allied Telesis AT-UMC2000/SP USB 3.0/USB 3.1 Gen 1 to Gigabit Ethernet Adapter */
1965 USB_DEVICE(0x07c9, 0x0010),
1966 .driver_info = (unsigned long)&at_umc2000sp_info,
1970 MODULE_DEVICE_TABLE(usb, products);
1972 static struct usb_driver ax88179_178a_driver = {
1973 .name = "ax88179_178a",
1974 .id_table = products,
1975 .probe = usbnet_probe,
1976 .suspend = ax88179_suspend,
1977 .resume = ax88179_resume,
1978 .reset_resume = ax88179_resume,
1979 .disconnect = usbnet_disconnect,
1980 .supports_autosuspend = 1,
1981 .disable_hub_initiated_lpm = 1,
1984 module_usb_driver(ax88179_178a_driver);
1986 MODULE_DESCRIPTION("ASIX AX88179/178A based USB 3.0/2.0 Gigabit Ethernet Devices");
1987 MODULE_LICENSE("GPL");