1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ASIX AX8817X based USB 2.0 Ethernet Devices
4 * Copyright (C) 2003-2006 David Hollis <dhollis@davehollis.com>
5 * Copyright (C) 2005 Phil Chang <pchang23@sbcglobal.net>
6 * Copyright (C) 2006 James Painter <jamie.painter@iname.com>
7 * Copyright (c) 2002-2003 TiVo Inc.
12 #define AX_HOST_EN_RETRIES 30
14 int asix_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
15 u16 size, void *data, int in_pm)
18 int (*fn)(struct usbnet *, u8, u8, u16, u16, void *, u16);
25 fn = usbnet_read_cmd_nopm;
27 ret = fn(dev, cmd, USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
28 value, index, data, size);
30 if (unlikely(ret < 0))
31 netdev_warn(dev->net, "Failed to read reg index 0x%04x: %d\n",
37 int asix_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
38 u16 size, void *data, int in_pm)
41 int (*fn)(struct usbnet *, u8, u8, u16, u16, const void *, u16);
46 fn = usbnet_write_cmd;
48 fn = usbnet_write_cmd_nopm;
50 ret = fn(dev, cmd, USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
51 value, index, data, size);
53 if (unlikely(ret < 0))
54 netdev_warn(dev->net, "Failed to write reg index 0x%04x: %d\n",
60 void asix_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value, u16 index,
63 usbnet_write_cmd_async(dev, cmd,
64 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
65 value, index, data, size);
68 static int asix_check_host_enable(struct usbnet *dev, int in_pm)
73 for (i = 0; i < AX_HOST_EN_RETRIES; ++i) {
74 ret = asix_set_sw_mii(dev, in_pm);
75 if (ret == -ENODEV || ret == -ETIMEDOUT)
77 usleep_range(1000, 1100);
78 ret = asix_read_cmd(dev, AX_CMD_STATMNGSTS_REG,
79 0, 0, 1, &smsr, in_pm);
82 else if (ret < sizeof(smsr))
84 else if (smsr & AX_HOST_EN)
88 return i >= AX_HOST_EN_RETRIES ? -ETIMEDOUT : ret;
91 static void reset_asix_rx_fixup_info(struct asix_rx_fixup_info *rx)
93 /* Reset the variables that have a lifetime outside of
94 * asix_rx_fixup_internal() so that future processing starts from a
95 * known set of initial conditions.
99 /* Discard any incomplete Ethernet frame in the netdev buffer */
100 kfree_skb(rx->ax_skb);
104 /* Assume the Data header 32-bit word is at the start of the current
105 * or next URB socket buffer so reset all the state variables.
108 rx->split_head = false;
112 int asix_rx_fixup_internal(struct usbnet *dev, struct sk_buff *skb,
113 struct asix_rx_fixup_info *rx)
118 /* When an Ethernet frame spans multiple URB socket buffers,
119 * do a sanity test for the Data header synchronisation.
120 * Attempt to detect the situation of the previous socket buffer having
121 * been truncated or a socket buffer was missing. These situations
122 * cause a discontinuity in the data stream and therefore need to avoid
123 * appending bad data to the end of the current netdev socket buffer.
124 * Also avoid unnecessarily discarding a good current netdev socket
127 if (rx->remaining && (rx->remaining + sizeof(u32) <= skb->len)) {
128 offset = ((rx->remaining + 1) & 0xfffe);
129 rx->header = get_unaligned_le32(skb->data + offset);
132 size = (u16)(rx->header & 0x7ff);
133 if (size != ((~rx->header >> 16) & 0x7ff)) {
134 netdev_err(dev->net, "asix_rx_fixup() Data Header synchronisation was lost, remaining %d\n",
136 reset_asix_rx_fixup_info(rx);
140 while (offset + sizeof(u16) <= skb->len) {
143 if (!rx->remaining) {
144 if (skb->len - offset == sizeof(u16)) {
145 rx->header = get_unaligned_le16(
147 rx->split_head = true;
148 offset += sizeof(u16);
152 if (rx->split_head == true) {
153 rx->header |= (get_unaligned_le16(
154 skb->data + offset) << 16);
155 rx->split_head = false;
156 offset += sizeof(u16);
158 rx->header = get_unaligned_le32(skb->data +
160 offset += sizeof(u32);
163 /* take frame length from Data header 32-bit word */
164 size = (u16)(rx->header & 0x7ff);
165 if (size != ((~rx->header >> 16) & 0x7ff)) {
166 netdev_err(dev->net, "asix_rx_fixup() Bad Header Length 0x%x, offset %d\n",
168 reset_asix_rx_fixup_info(rx);
171 if (size > dev->net->mtu + ETH_HLEN + VLAN_HLEN) {
172 netdev_dbg(dev->net, "asix_rx_fixup() Bad RX Length %d\n",
174 reset_asix_rx_fixup_info(rx);
178 /* Sometimes may fail to get a netdev socket buffer but
179 * continue to process the URB socket buffer so that
180 * synchronisation of the Ethernet frame Data header
181 * word is maintained.
183 rx->ax_skb = netdev_alloc_skb_ip_align(dev->net, size);
185 rx->remaining = size;
188 if (rx->remaining > skb->len - offset) {
189 copy_length = skb->len - offset;
190 rx->remaining -= copy_length;
192 copy_length = rx->remaining;
197 skb_put_data(rx->ax_skb, skb->data + offset,
199 if (!rx->remaining) {
200 usbnet_skb_return(dev, rx->ax_skb);
205 offset += (copy_length + 1) & 0xfffe;
208 if (skb->len != offset) {
209 netdev_err(dev->net, "asix_rx_fixup() Bad SKB Length %d, %d\n",
211 reset_asix_rx_fixup_info(rx);
218 int asix_rx_fixup_common(struct usbnet *dev, struct sk_buff *skb)
220 struct asix_common_private *dp = dev->driver_priv;
221 struct asix_rx_fixup_info *rx = &dp->rx_fixup_info;
223 return asix_rx_fixup_internal(dev, skb, rx);
226 void asix_rx_fixup_common_free(struct asix_common_private *dp)
228 struct asix_rx_fixup_info *rx;
233 rx = &dp->rx_fixup_info;
236 kfree_skb(rx->ax_skb);
241 struct sk_buff *asix_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
245 int headroom = skb_headroom(skb);
246 int tailroom = skb_tailroom(skb);
248 u32 padbytes = 0xffff0000;
251 padlen = ((skb->len + 4) & (dev->maxpacket - 1)) ? 0 : 4;
253 /* We need to push 4 bytes in front of frame (packet_len)
254 * and maybe add 4 bytes after the end (if padlen is 4)
256 * Avoid skb_copy_expand() expensive call, using following rules :
257 * - We are allowed to push 4 bytes in headroom if skb_header_cloned()
258 * is false (and if we have 4 bytes of headroom)
259 * - We are allowed to put 4 bytes at tail if skb_cloned()
260 * is false (and if we have 4 bytes of tailroom)
262 * TCP packets for example are cloned, but __skb_header_release()
263 * was called in tcp stack, allowing us to use headroom for our needs.
265 if (!skb_header_cloned(skb) &&
266 !(padlen && skb_cloned(skb)) &&
267 headroom + tailroom >= 4 + padlen) {
268 /* following should not happen, but better be safe */
271 skb->data = memmove(skb->head + 4, skb->data, skb->len);
272 skb_set_tail_pointer(skb, skb->len);
275 struct sk_buff *skb2;
277 skb2 = skb_copy_expand(skb, 4, padlen, flags);
278 dev_kfree_skb_any(skb);
284 packet_len = ((skb->len ^ 0x0000ffff) << 16) + skb->len;
285 ptr = skb_push(skb, 4);
286 put_unaligned_le32(packet_len, ptr);
289 put_unaligned_le32(padbytes, skb_tail_pointer(skb));
290 skb_put(skb, sizeof(padbytes));
293 usbnet_set_skb_tx_stats(skb, 1, 0);
297 int asix_set_sw_mii(struct usbnet *dev, int in_pm)
300 ret = asix_write_cmd(dev, AX_CMD_SET_SW_MII, 0x0000, 0, 0, NULL, in_pm);
303 netdev_err(dev->net, "Failed to enable software MII access\n");
307 int asix_set_hw_mii(struct usbnet *dev, int in_pm)
310 ret = asix_write_cmd(dev, AX_CMD_SET_HW_MII, 0x0000, 0, 0, NULL, in_pm);
312 netdev_err(dev->net, "Failed to enable hardware MII access\n");
316 int asix_read_phy_addr(struct usbnet *dev, bool internal)
321 ret = asix_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf, 0);
330 offset = (internal ? 1 : 0);
333 netdev_dbg(dev->net, "%s PHY address 0x%x\n",
334 internal ? "internal" : "external", ret);
339 netdev_err(dev->net, "Error reading PHY_ID register: %02x\n", ret);
344 int asix_sw_reset(struct usbnet *dev, u8 flags, int in_pm)
348 ret = asix_write_cmd(dev, AX_CMD_SW_RESET, flags, 0, 0, NULL, in_pm);
350 netdev_err(dev->net, "Failed to send software reset: %02x\n", ret);
355 u16 asix_read_rx_ctl(struct usbnet *dev, int in_pm)
358 int ret = asix_read_cmd(dev, AX_CMD_READ_RX_CTL, 0, 0, 2, &v, in_pm);
361 netdev_err(dev->net, "Error reading RX_CTL register: %02x\n", ret);
364 ret = le16_to_cpu(v);
369 int asix_write_rx_ctl(struct usbnet *dev, u16 mode, int in_pm)
373 netdev_dbg(dev->net, "asix_write_rx_ctl() - mode = 0x%04x\n", mode);
374 ret = asix_write_cmd(dev, AX_CMD_WRITE_RX_CTL, mode, 0, 0, NULL, in_pm);
376 netdev_err(dev->net, "Failed to write RX_CTL mode to 0x%04x: %02x\n",
382 u16 asix_read_medium_status(struct usbnet *dev, int in_pm)
385 int ret = asix_read_cmd(dev, AX_CMD_READ_MEDIUM_STATUS,
389 netdev_err(dev->net, "Error reading Medium Status register: %02x\n",
391 return ret; /* TODO: callers not checking for error ret */
394 return le16_to_cpu(v);
398 int asix_write_medium_mode(struct usbnet *dev, u16 mode, int in_pm)
402 netdev_dbg(dev->net, "asix_write_medium_mode() - mode = 0x%04x\n", mode);
403 ret = asix_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE,
404 mode, 0, 0, NULL, in_pm);
406 netdev_err(dev->net, "Failed to write Medium Mode mode to 0x%04x: %02x\n",
412 /* set MAC link settings according to information from phylib */
413 void asix_adjust_link(struct net_device *netdev)
415 struct phy_device *phydev = netdev->phydev;
416 struct usbnet *dev = netdev_priv(netdev);
420 mode = AX88772_MEDIUM_DEFAULT;
422 if (phydev->duplex == DUPLEX_HALF)
423 mode &= ~AX_MEDIUM_FD;
425 if (phydev->speed != SPEED_100)
426 mode &= ~AX_MEDIUM_PS;
429 asix_write_medium_mode(dev, mode, 0);
430 phy_print_status(phydev);
433 int asix_write_gpio(struct usbnet *dev, u16 value, int sleep, int in_pm)
437 netdev_dbg(dev->net, "asix_write_gpio() - value = 0x%04x\n", value);
438 ret = asix_write_cmd(dev, AX_CMD_WRITE_GPIOS, value, 0, 0, NULL, in_pm);
440 netdev_err(dev->net, "Failed to write GPIO value 0x%04x: %02x\n",
450 * AX88772 & AX88178 have a 16-bit RX_CTL value
452 void asix_set_multicast(struct net_device *net)
454 struct usbnet *dev = netdev_priv(net);
455 struct asix_data *data = (struct asix_data *)&dev->data;
456 u16 rx_ctl = AX_DEFAULT_RX_CTL;
458 if (net->flags & IFF_PROMISC) {
459 rx_ctl |= AX_RX_CTL_PRO;
460 } else if (net->flags & IFF_ALLMULTI ||
461 netdev_mc_count(net) > AX_MAX_MCAST) {
462 rx_ctl |= AX_RX_CTL_AMALL;
463 } else if (netdev_mc_empty(net)) {
464 /* just broadcast and directed */
466 /* We use the 20 byte dev->data
467 * for our 8 byte filter buffer
468 * to avoid allocating memory that
469 * is tricky to free later */
470 struct netdev_hw_addr *ha;
473 memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
475 /* Build the multicast hash filter. */
476 netdev_for_each_mc_addr(ha, net) {
477 crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
478 data->multi_filter[crc_bits >> 3] |=
482 asix_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
483 AX_MCAST_FILTER_SIZE, data->multi_filter);
485 rx_ctl |= AX_RX_CTL_AM;
488 asix_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
491 int asix_mdio_read(struct net_device *netdev, int phy_id, int loc)
493 struct usbnet *dev = netdev_priv(netdev);
497 mutex_lock(&dev->phy_mutex);
499 ret = asix_check_host_enable(dev, 0);
500 if (ret == -ENODEV || ret == -ETIMEDOUT) {
501 mutex_unlock(&dev->phy_mutex);
505 ret = asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id, (__u16)loc, 2,
510 ret = asix_set_hw_mii(dev, 0);
512 mutex_unlock(&dev->phy_mutex);
514 netdev_dbg(dev->net, "asix_mdio_read() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
515 phy_id, loc, le16_to_cpu(res));
517 return ret < 0 ? ret : le16_to_cpu(res);
520 static int __asix_mdio_write(struct net_device *netdev, int phy_id, int loc,
523 struct usbnet *dev = netdev_priv(netdev);
524 __le16 res = cpu_to_le16(val);
527 netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
530 mutex_lock(&dev->phy_mutex);
532 ret = asix_check_host_enable(dev, 0);
536 ret = asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2,
541 ret = asix_set_hw_mii(dev, 0);
543 mutex_unlock(&dev->phy_mutex);
545 return ret < 0 ? ret : 0;
548 void asix_mdio_write(struct net_device *netdev, int phy_id, int loc, int val)
550 __asix_mdio_write(netdev, phy_id, loc, val);
553 /* MDIO read and write wrappers for phylib */
554 int asix_mdio_bus_read(struct mii_bus *bus, int phy_id, int regnum)
556 struct usbnet *priv = bus->priv;
558 return asix_mdio_read(priv->net, phy_id, regnum);
561 int asix_mdio_bus_write(struct mii_bus *bus, int phy_id, int regnum, u16 val)
563 struct usbnet *priv = bus->priv;
565 return __asix_mdio_write(priv->net, phy_id, regnum, val);
568 int asix_mdio_read_nopm(struct net_device *netdev, int phy_id, int loc)
570 struct usbnet *dev = netdev_priv(netdev);
574 mutex_lock(&dev->phy_mutex);
576 ret = asix_check_host_enable(dev, 1);
577 if (ret == -ENODEV || ret == -ETIMEDOUT) {
578 mutex_unlock(&dev->phy_mutex);
582 asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id,
583 (__u16)loc, 2, &res, 1);
584 asix_set_hw_mii(dev, 1);
585 mutex_unlock(&dev->phy_mutex);
587 netdev_dbg(dev->net, "asix_mdio_read_nopm() phy_id=0x%02x, loc=0x%02x, returns=0x%04x\n",
588 phy_id, loc, le16_to_cpu(res));
590 return le16_to_cpu(res);
594 asix_mdio_write_nopm(struct net_device *netdev, int phy_id, int loc, int val)
596 struct usbnet *dev = netdev_priv(netdev);
597 __le16 res = cpu_to_le16(val);
600 netdev_dbg(dev->net, "asix_mdio_write() phy_id=0x%02x, loc=0x%02x, val=0x%04x\n",
603 mutex_lock(&dev->phy_mutex);
605 ret = asix_check_host_enable(dev, 1);
606 if (ret == -ENODEV) {
607 mutex_unlock(&dev->phy_mutex);
611 asix_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id,
612 (__u16)loc, 2, &res, 1);
613 asix_set_hw_mii(dev, 1);
614 mutex_unlock(&dev->phy_mutex);
617 void asix_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
619 struct usbnet *dev = netdev_priv(net);
622 if (asix_read_cmd(dev, AX_CMD_READ_MONITOR_MODE,
623 0, 0, 1, &opt, 0) < 0) {
624 wolinfo->supported = 0;
625 wolinfo->wolopts = 0;
628 wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
629 wolinfo->wolopts = 0;
630 if (opt & AX_MONITOR_LINK)
631 wolinfo->wolopts |= WAKE_PHY;
632 if (opt & AX_MONITOR_MAGIC)
633 wolinfo->wolopts |= WAKE_MAGIC;
636 int asix_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
638 struct usbnet *dev = netdev_priv(net);
641 if (wolinfo->wolopts & ~(WAKE_PHY | WAKE_MAGIC))
644 if (wolinfo->wolopts & WAKE_PHY)
645 opt |= AX_MONITOR_LINK;
646 if (wolinfo->wolopts & WAKE_MAGIC)
647 opt |= AX_MONITOR_MAGIC;
649 if (asix_write_cmd(dev, AX_CMD_WRITE_MONITOR_MODE,
650 opt, 0, 0, NULL, 0) < 0)
656 int asix_get_eeprom_len(struct net_device *net)
658 return AX_EEPROM_LEN;
661 int asix_get_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
664 struct usbnet *dev = netdev_priv(net);
666 int first_word, last_word;
669 if (eeprom->len == 0)
672 eeprom->magic = AX_EEPROM_MAGIC;
674 first_word = eeprom->offset >> 1;
675 last_word = (eeprom->offset + eeprom->len - 1) >> 1;
677 eeprom_buff = kmalloc_array(last_word - first_word + 1, sizeof(u16),
682 /* ax8817x returns 2 bytes from eeprom on read */
683 for (i = first_word; i <= last_word; i++) {
684 if (asix_read_cmd(dev, AX_CMD_READ_EEPROM, i, 0, 2,
685 &eeprom_buff[i - first_word], 0) < 0) {
691 memcpy(data, (u8 *)eeprom_buff + (eeprom->offset & 1), eeprom->len);
696 int asix_set_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
699 struct usbnet *dev = netdev_priv(net);
701 int first_word, last_word;
705 netdev_dbg(net, "write EEPROM len %d, offset %d, magic 0x%x\n",
706 eeprom->len, eeprom->offset, eeprom->magic);
708 if (eeprom->len == 0)
711 if (eeprom->magic != AX_EEPROM_MAGIC)
714 first_word = eeprom->offset >> 1;
715 last_word = (eeprom->offset + eeprom->len - 1) >> 1;
717 eeprom_buff = kmalloc_array(last_word - first_word + 1, sizeof(u16),
722 /* align data to 16 bit boundaries, read the missing data from
724 if (eeprom->offset & 1) {
725 ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, first_word, 0, 2,
728 netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", first_word);
733 if ((eeprom->offset + eeprom->len) & 1) {
734 ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, last_word, 0, 2,
735 &eeprom_buff[last_word - first_word], 0);
737 netdev_err(net, "Failed to read EEPROM at offset 0x%02x.\n", last_word);
742 memcpy((u8 *)eeprom_buff + (eeprom->offset & 1), data, eeprom->len);
744 /* write data to EEPROM */
745 ret = asix_write_cmd(dev, AX_CMD_WRITE_ENABLE, 0x0000, 0, 0, NULL, 0);
747 netdev_err(net, "Failed to enable EEPROM write\n");
752 for (i = first_word; i <= last_word; i++) {
753 netdev_dbg(net, "write to EEPROM at offset 0x%02x, data 0x%04x\n",
754 i, eeprom_buff[i - first_word]);
755 ret = asix_write_cmd(dev, AX_CMD_WRITE_EEPROM, i,
756 eeprom_buff[i - first_word], 0, NULL, 0);
758 netdev_err(net, "Failed to write EEPROM at offset 0x%02x.\n",
765 ret = asix_write_cmd(dev, AX_CMD_WRITE_DISABLE, 0x0000, 0, 0, NULL, 0);
767 netdev_err(net, "Failed to disable EEPROM write\n");
777 void asix_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
779 /* Inherit standard device info */
780 usbnet_get_drvinfo(net, info);
781 strlcpy(info->driver, DRIVER_NAME, sizeof(info->driver));
782 strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
785 int asix_set_mac_address(struct net_device *net, void *p)
787 struct usbnet *dev = netdev_priv(net);
788 struct asix_data *data = (struct asix_data *)&dev->data;
789 struct sockaddr *addr = p;
791 if (netif_running(net))
793 if (!is_valid_ether_addr(addr->sa_data))
794 return -EADDRNOTAVAIL;
796 eth_hw_addr_set(net, addr->sa_data);
798 /* We use the 20 byte dev->data
799 * for our 6 byte mac buffer
800 * to avoid allocating memory that
801 * is tricky to free later */
802 memcpy(data->mac_addr, addr->sa_data, ETH_ALEN);
803 asix_write_cmd_async(dev, AX_CMD_WRITE_NODE_ID, 0, 0, ETH_ALEN,