Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* Aquantia Corp. Aquantia AQtion USB to 5GbE Controller
0003  * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com>
0004  * Copyright (C) 2005 Phil Chang <pchang23@sbcglobal.net>
0005  * Copyright (C) 2002-2003 TiVo Inc.
0006  * Copyright (C) 2017-2018 ASIX
0007  * Copyright (C) 2018 Aquantia Corp.
0008  */
0009 
0010 #include <linux/module.h>
0011 #include <linux/netdevice.h>
0012 #include <linux/ethtool.h>
0013 #include <linux/mii.h>
0014 #include <linux/usb.h>
0015 #include <linux/crc32.h>
0016 #include <linux/if_vlan.h>
0017 #include <linux/usb/cdc.h>
0018 #include <linux/usb/usbnet.h>
0019 #include <linux/linkmode.h>
0020 
0021 #include "aqc111.h"
0022 
0023 #define DRIVER_NAME "aqc111"
0024 
0025 static int aqc111_read_cmd_nopm(struct usbnet *dev, u8 cmd, u16 value,
0026                 u16 index, u16 size, void *data)
0027 {
0028     int ret;
0029 
0030     ret = usbnet_read_cmd_nopm(dev, cmd, USB_DIR_IN | USB_TYPE_VENDOR |
0031                    USB_RECIP_DEVICE, value, index, data, size);
0032 
0033     if (unlikely(ret < 0))
0034         netdev_warn(dev->net,
0035                 "Failed to read(0x%x) reg index 0x%04x: %d\n",
0036                 cmd, index, ret);
0037 
0038     return ret;
0039 }
0040 
0041 static int aqc111_read_cmd(struct usbnet *dev, u8 cmd, u16 value,
0042                u16 index, u16 size, void *data)
0043 {
0044     int ret;
0045 
0046     ret = usbnet_read_cmd(dev, cmd, USB_DIR_IN | USB_TYPE_VENDOR |
0047                   USB_RECIP_DEVICE, value, index, data, size);
0048 
0049     if (unlikely(ret < 0))
0050         netdev_warn(dev->net,
0051                 "Failed to read(0x%x) reg index 0x%04x: %d\n",
0052                 cmd, index, ret);
0053 
0054     return ret;
0055 }
0056 
0057 static int aqc111_read16_cmd_nopm(struct usbnet *dev, u8 cmd, u16 value,
0058                   u16 index, u16 *data)
0059 {
0060     int ret = 0;
0061 
0062     ret = aqc111_read_cmd_nopm(dev, cmd, value, index, sizeof(*data), data);
0063     le16_to_cpus(data);
0064 
0065     return ret;
0066 }
0067 
0068 static int aqc111_read16_cmd(struct usbnet *dev, u8 cmd, u16 value,
0069                  u16 index, u16 *data)
0070 {
0071     int ret = 0;
0072 
0073     ret = aqc111_read_cmd(dev, cmd, value, index, sizeof(*data), data);
0074     le16_to_cpus(data);
0075 
0076     return ret;
0077 }
0078 
0079 static int __aqc111_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
0080                   u16 value, u16 index, u16 size, const void *data)
0081 {
0082     int err = -ENOMEM;
0083     void *buf = NULL;
0084 
0085     netdev_dbg(dev->net,
0086            "%s cmd=%#x reqtype=%#x value=%#x index=%#x size=%d\n",
0087            __func__, cmd, reqtype, value, index, size);
0088 
0089     if (data) {
0090         buf = kmemdup(data, size, GFP_KERNEL);
0091         if (!buf)
0092             goto out;
0093     }
0094 
0095     err = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
0096                   cmd, reqtype, value, index, buf, size,
0097                   (cmd == AQ_PHY_POWER) ? AQ_USB_PHY_SET_TIMEOUT :
0098                   AQ_USB_SET_TIMEOUT);
0099 
0100     if (unlikely(err < 0))
0101         netdev_warn(dev->net,
0102                 "Failed to write(0x%x) reg index 0x%04x: %d\n",
0103                 cmd, index, err);
0104     kfree(buf);
0105 
0106 out:
0107     return err;
0108 }
0109 
0110 static int aqc111_write_cmd_nopm(struct usbnet *dev, u8 cmd, u16 value,
0111                  u16 index, u16 size, void *data)
0112 {
0113     int ret;
0114 
0115     ret = __aqc111_write_cmd(dev, cmd, USB_DIR_OUT | USB_TYPE_VENDOR |
0116                  USB_RECIP_DEVICE, value, index, size, data);
0117 
0118     return ret;
0119 }
0120 
0121 static int aqc111_write_cmd(struct usbnet *dev, u8 cmd, u16 value,
0122                 u16 index, u16 size, const void *data)
0123 {
0124     int ret;
0125 
0126     if (usb_autopm_get_interface(dev->intf) < 0)
0127         return -ENODEV;
0128 
0129     ret = __aqc111_write_cmd(dev, cmd, USB_DIR_OUT | USB_TYPE_VENDOR |
0130                  USB_RECIP_DEVICE, value, index, size, data);
0131 
0132     usb_autopm_put_interface(dev->intf);
0133 
0134     return ret;
0135 }
0136 
0137 static int aqc111_write16_cmd_nopm(struct usbnet *dev, u8 cmd, u16 value,
0138                    u16 index, u16 *data)
0139 {
0140     u16 tmp = *data;
0141 
0142     cpu_to_le16s(&tmp);
0143 
0144     return aqc111_write_cmd_nopm(dev, cmd, value, index, sizeof(tmp), &tmp);
0145 }
0146 
0147 static int aqc111_write16_cmd(struct usbnet *dev, u8 cmd, u16 value,
0148                   u16 index, u16 *data)
0149 {
0150     u16 tmp = *data;
0151 
0152     cpu_to_le16s(&tmp);
0153 
0154     return aqc111_write_cmd(dev, cmd, value, index, sizeof(tmp), &tmp);
0155 }
0156 
0157 static int aqc111_write32_cmd_nopm(struct usbnet *dev, u8 cmd, u16 value,
0158                    u16 index, u32 *data)
0159 {
0160     u32 tmp = *data;
0161 
0162     cpu_to_le32s(&tmp);
0163 
0164     return aqc111_write_cmd_nopm(dev, cmd, value, index, sizeof(tmp), &tmp);
0165 }
0166 
0167 static int aqc111_write32_cmd(struct usbnet *dev, u8 cmd, u16 value,
0168                   u16 index, u32 *data)
0169 {
0170     u32 tmp = *data;
0171 
0172     cpu_to_le32s(&tmp);
0173 
0174     return aqc111_write_cmd(dev, cmd, value, index, sizeof(tmp), &tmp);
0175 }
0176 
0177 static int aqc111_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value,
0178                   u16 index, u16 size, void *data)
0179 {
0180     return usbnet_write_cmd_async(dev, cmd, USB_DIR_OUT | USB_TYPE_VENDOR |
0181                       USB_RECIP_DEVICE, value, index, data,
0182                       size);
0183 }
0184 
0185 static int aqc111_write16_cmd_async(struct usbnet *dev, u8 cmd, u16 value,
0186                     u16 index, u16 *data)
0187 {
0188     u16 tmp = *data;
0189 
0190     cpu_to_le16s(&tmp);
0191 
0192     return aqc111_write_cmd_async(dev, cmd, value, index,
0193                       sizeof(tmp), &tmp);
0194 }
0195 
0196 static void aqc111_get_drvinfo(struct net_device *net,
0197                    struct ethtool_drvinfo *info)
0198 {
0199     struct usbnet *dev = netdev_priv(net);
0200     struct aqc111_data *aqc111_data = dev->driver_priv;
0201 
0202     /* Inherit standard device info */
0203     usbnet_get_drvinfo(net, info);
0204     strlcpy(info->driver, DRIVER_NAME, sizeof(info->driver));
0205     snprintf(info->fw_version, sizeof(info->fw_version), "%u.%u.%u",
0206          aqc111_data->fw_ver.major,
0207          aqc111_data->fw_ver.minor,
0208          aqc111_data->fw_ver.rev);
0209     info->eedump_len = 0x00;
0210     info->regdump_len = 0x00;
0211 }
0212 
0213 static void aqc111_get_wol(struct net_device *net,
0214                struct ethtool_wolinfo *wolinfo)
0215 {
0216     struct usbnet *dev = netdev_priv(net);
0217     struct aqc111_data *aqc111_data = dev->driver_priv;
0218 
0219     wolinfo->supported = WAKE_MAGIC;
0220     wolinfo->wolopts = 0;
0221 
0222     if (aqc111_data->wol_flags & AQ_WOL_FLAG_MP)
0223         wolinfo->wolopts |= WAKE_MAGIC;
0224 }
0225 
0226 static int aqc111_set_wol(struct net_device *net,
0227               struct ethtool_wolinfo *wolinfo)
0228 {
0229     struct usbnet *dev = netdev_priv(net);
0230     struct aqc111_data *aqc111_data = dev->driver_priv;
0231 
0232     if (wolinfo->wolopts & ~WAKE_MAGIC)
0233         return -EINVAL;
0234 
0235     aqc111_data->wol_flags = 0;
0236     if (wolinfo->wolopts & WAKE_MAGIC)
0237         aqc111_data->wol_flags |= AQ_WOL_FLAG_MP;
0238 
0239     return 0;
0240 }
0241 
0242 static void aqc111_speed_to_link_mode(u32 speed,
0243                       struct ethtool_link_ksettings *elk)
0244 {
0245     switch (speed) {
0246     case SPEED_5000:
0247         ethtool_link_ksettings_add_link_mode(elk, advertising,
0248                              5000baseT_Full);
0249         break;
0250     case SPEED_2500:
0251         ethtool_link_ksettings_add_link_mode(elk, advertising,
0252                              2500baseT_Full);
0253         break;
0254     case SPEED_1000:
0255         ethtool_link_ksettings_add_link_mode(elk, advertising,
0256                              1000baseT_Full);
0257         break;
0258     case SPEED_100:
0259         ethtool_link_ksettings_add_link_mode(elk, advertising,
0260                              100baseT_Full);
0261         break;
0262     }
0263 }
0264 
0265 static int aqc111_get_link_ksettings(struct net_device *net,
0266                      struct ethtool_link_ksettings *elk)
0267 {
0268     struct usbnet *dev = netdev_priv(net);
0269     struct aqc111_data *aqc111_data = dev->driver_priv;
0270     enum usb_device_speed usb_speed = dev->udev->speed;
0271     u32 speed = SPEED_UNKNOWN;
0272 
0273     ethtool_link_ksettings_zero_link_mode(elk, supported);
0274     ethtool_link_ksettings_add_link_mode(elk, supported,
0275                          100baseT_Full);
0276     ethtool_link_ksettings_add_link_mode(elk, supported,
0277                          1000baseT_Full);
0278     if (usb_speed == USB_SPEED_SUPER) {
0279         ethtool_link_ksettings_add_link_mode(elk, supported,
0280                              2500baseT_Full);
0281         ethtool_link_ksettings_add_link_mode(elk, supported,
0282                              5000baseT_Full);
0283     }
0284     ethtool_link_ksettings_add_link_mode(elk, supported, TP);
0285     ethtool_link_ksettings_add_link_mode(elk, supported, Autoneg);
0286 
0287     elk->base.port = PORT_TP;
0288     elk->base.transceiver = XCVR_INTERNAL;
0289 
0290     elk->base.mdio_support = 0x00; /*Not supported*/
0291 
0292     if (aqc111_data->autoneg)
0293         linkmode_copy(elk->link_modes.advertising,
0294                   elk->link_modes.supported);
0295     else
0296         aqc111_speed_to_link_mode(aqc111_data->advertised_speed, elk);
0297 
0298     elk->base.autoneg = aqc111_data->autoneg;
0299 
0300     switch (aqc111_data->link_speed) {
0301     case AQ_INT_SPEED_5G:
0302         speed = SPEED_5000;
0303         break;
0304     case AQ_INT_SPEED_2_5G:
0305         speed = SPEED_2500;
0306         break;
0307     case AQ_INT_SPEED_1G:
0308         speed = SPEED_1000;
0309         break;
0310     case AQ_INT_SPEED_100M:
0311         speed = SPEED_100;
0312         break;
0313     }
0314     elk->base.duplex = DUPLEX_FULL;
0315     elk->base.speed = speed;
0316 
0317     return 0;
0318 }
0319 
0320 static void aqc111_set_phy_speed(struct usbnet *dev, u8 autoneg, u16 speed)
0321 {
0322     struct aqc111_data *aqc111_data = dev->driver_priv;
0323 
0324     aqc111_data->phy_cfg &= ~AQ_ADV_MASK;
0325     aqc111_data->phy_cfg |= AQ_PAUSE;
0326     aqc111_data->phy_cfg |= AQ_ASYM_PAUSE;
0327     aqc111_data->phy_cfg |= AQ_DOWNSHIFT;
0328     aqc111_data->phy_cfg &= ~AQ_DSH_RETRIES_MASK;
0329     aqc111_data->phy_cfg |= (3 << AQ_DSH_RETRIES_SHIFT) &
0330                 AQ_DSH_RETRIES_MASK;
0331 
0332     if (autoneg == AUTONEG_ENABLE) {
0333         switch (speed) {
0334         case SPEED_5000:
0335             aqc111_data->phy_cfg |= AQ_ADV_5G;
0336             fallthrough;
0337         case SPEED_2500:
0338             aqc111_data->phy_cfg |= AQ_ADV_2G5;
0339             fallthrough;
0340         case SPEED_1000:
0341             aqc111_data->phy_cfg |= AQ_ADV_1G;
0342             fallthrough;
0343         case SPEED_100:
0344             aqc111_data->phy_cfg |= AQ_ADV_100M;
0345             /* fall-through */
0346         }
0347     } else {
0348         switch (speed) {
0349         case SPEED_5000:
0350             aqc111_data->phy_cfg |= AQ_ADV_5G;
0351             break;
0352         case SPEED_2500:
0353             aqc111_data->phy_cfg |= AQ_ADV_2G5;
0354             break;
0355         case SPEED_1000:
0356             aqc111_data->phy_cfg |= AQ_ADV_1G;
0357             break;
0358         case SPEED_100:
0359             aqc111_data->phy_cfg |= AQ_ADV_100M;
0360             break;
0361         }
0362     }
0363 
0364     aqc111_write32_cmd(dev, AQ_PHY_OPS, 0, 0, &aqc111_data->phy_cfg);
0365 }
0366 
0367 static int aqc111_set_link_ksettings(struct net_device *net,
0368                      const struct ethtool_link_ksettings *elk)
0369 {
0370     struct usbnet *dev = netdev_priv(net);
0371     struct aqc111_data *aqc111_data = dev->driver_priv;
0372     enum usb_device_speed usb_speed = dev->udev->speed;
0373     u8 autoneg = elk->base.autoneg;
0374     u32 speed = elk->base.speed;
0375 
0376     if (autoneg == AUTONEG_ENABLE) {
0377         if (aqc111_data->autoneg != AUTONEG_ENABLE) {
0378             aqc111_data->autoneg = AUTONEG_ENABLE;
0379             aqc111_data->advertised_speed =
0380                     (usb_speed == USB_SPEED_SUPER) ?
0381                      SPEED_5000 : SPEED_1000;
0382             aqc111_set_phy_speed(dev, aqc111_data->autoneg,
0383                          aqc111_data->advertised_speed);
0384         }
0385     } else {
0386         if (speed != SPEED_100 &&
0387             speed != SPEED_1000 &&
0388             speed != SPEED_2500 &&
0389             speed != SPEED_5000 &&
0390             speed != SPEED_UNKNOWN)
0391             return -EINVAL;
0392 
0393         if (elk->base.duplex != DUPLEX_FULL)
0394             return -EINVAL;
0395 
0396         if (usb_speed != USB_SPEED_SUPER && speed > SPEED_1000)
0397             return -EINVAL;
0398 
0399         aqc111_data->autoneg = AUTONEG_DISABLE;
0400         if (speed != SPEED_UNKNOWN)
0401             aqc111_data->advertised_speed = speed;
0402 
0403         aqc111_set_phy_speed(dev, aqc111_data->autoneg,
0404                      aqc111_data->advertised_speed);
0405     }
0406 
0407     return 0;
0408 }
0409 
0410 static const struct ethtool_ops aqc111_ethtool_ops = {
0411     .get_drvinfo = aqc111_get_drvinfo,
0412     .get_wol = aqc111_get_wol,
0413     .set_wol = aqc111_set_wol,
0414     .get_msglevel = usbnet_get_msglevel,
0415     .set_msglevel = usbnet_set_msglevel,
0416     .get_link = ethtool_op_get_link,
0417     .get_link_ksettings = aqc111_get_link_ksettings,
0418     .set_link_ksettings = aqc111_set_link_ksettings
0419 };
0420 
0421 static int aqc111_change_mtu(struct net_device *net, int new_mtu)
0422 {
0423     struct usbnet *dev = netdev_priv(net);
0424     u16 reg16 = 0;
0425     u8 buf[5];
0426 
0427     net->mtu = new_mtu;
0428     dev->hard_mtu = net->mtu + net->hard_header_len;
0429 
0430     aqc111_read16_cmd(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
0431               2, &reg16);
0432     if (net->mtu > 1500)
0433         reg16 |= SFR_MEDIUM_JUMBO_EN;
0434     else
0435         reg16 &= ~SFR_MEDIUM_JUMBO_EN;
0436 
0437     aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
0438                2, &reg16);
0439 
0440     if (dev->net->mtu > 12500) {
0441         memcpy(buf, &AQC111_BULKIN_SIZE[2], 5);
0442         /* RX bulk configuration */
0443         aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_RX_BULKIN_QCTRL,
0444                  5, 5, buf);
0445     }
0446 
0447     /* Set high low water level */
0448     if (dev->net->mtu <= 4500)
0449         reg16 = 0x0810;
0450     else if (dev->net->mtu <= 9500)
0451         reg16 = 0x1020;
0452     else if (dev->net->mtu <= 12500)
0453         reg16 = 0x1420;
0454     else
0455         reg16 = 0x1A20;
0456 
0457     aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_PAUSE_WATERLVL_LOW,
0458                2, &reg16);
0459 
0460     return 0;
0461 }
0462 
0463 static int aqc111_set_mac_addr(struct net_device *net, void *p)
0464 {
0465     struct usbnet *dev = netdev_priv(net);
0466     int ret = 0;
0467 
0468     ret = eth_mac_addr(net, p);
0469     if (ret < 0)
0470         return ret;
0471 
0472     /* Set the MAC address */
0473     return aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_NODE_ID, ETH_ALEN,
0474                 ETH_ALEN, net->dev_addr);
0475 }
0476 
0477 static int aqc111_vlan_rx_kill_vid(struct net_device *net,
0478                    __be16 proto, u16 vid)
0479 {
0480     struct usbnet *dev = netdev_priv(net);
0481     u8 vlan_ctrl = 0;
0482     u16 reg16 = 0;
0483     u8 reg8 = 0;
0484 
0485     aqc111_read_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_CONTROL, 1, 1, &reg8);
0486     vlan_ctrl = reg8;
0487 
0488     /* Address */
0489     reg8 = (vid / 16);
0490     aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_ADDRESS, 1, 1, &reg8);
0491     /* Data */
0492     reg8 = vlan_ctrl | SFR_VLAN_CONTROL_RD;
0493     aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_CONTROL, 1, 1, &reg8);
0494     aqc111_read16_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_DATA0, 2, &reg16);
0495     reg16 &= ~(1 << (vid % 16));
0496     aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_DATA0, 2, &reg16);
0497     reg8 = vlan_ctrl | SFR_VLAN_CONTROL_WE;
0498     aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_CONTROL, 1, 1, &reg8);
0499 
0500     return 0;
0501 }
0502 
0503 static int aqc111_vlan_rx_add_vid(struct net_device *net, __be16 proto, u16 vid)
0504 {
0505     struct usbnet *dev = netdev_priv(net);
0506     u8 vlan_ctrl = 0;
0507     u16 reg16 = 0;
0508     u8 reg8 = 0;
0509 
0510     aqc111_read_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_CONTROL, 1, 1, &reg8);
0511     vlan_ctrl = reg8;
0512 
0513     /* Address */
0514     reg8 = (vid / 16);
0515     aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_ADDRESS, 1, 1, &reg8);
0516     /* Data */
0517     reg8 = vlan_ctrl | SFR_VLAN_CONTROL_RD;
0518     aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_CONTROL, 1, 1, &reg8);
0519     aqc111_read16_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_DATA0, 2, &reg16);
0520     reg16 |= (1 << (vid % 16));
0521     aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_DATA0, 2, &reg16);
0522     reg8 = vlan_ctrl | SFR_VLAN_CONTROL_WE;
0523     aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_CONTROL, 1, 1, &reg8);
0524 
0525     return 0;
0526 }
0527 
0528 static void aqc111_set_rx_mode(struct net_device *net)
0529 {
0530     struct usbnet *dev = netdev_priv(net);
0531     struct aqc111_data *aqc111_data = dev->driver_priv;
0532     int mc_count = 0;
0533 
0534     mc_count = netdev_mc_count(net);
0535 
0536     aqc111_data->rxctl &= ~(SFR_RX_CTL_PRO | SFR_RX_CTL_AMALL |
0537                 SFR_RX_CTL_AM);
0538 
0539     if (net->flags & IFF_PROMISC) {
0540         aqc111_data->rxctl |= SFR_RX_CTL_PRO;
0541     } else if ((net->flags & IFF_ALLMULTI) || mc_count > AQ_MAX_MCAST) {
0542         aqc111_data->rxctl |= SFR_RX_CTL_AMALL;
0543     } else if (!netdev_mc_empty(net)) {
0544         u8 m_filter[AQ_MCAST_FILTER_SIZE] = { 0 };
0545         struct netdev_hw_addr *ha = NULL;
0546         u32 crc_bits = 0;
0547 
0548         netdev_for_each_mc_addr(ha, net) {
0549             crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
0550             m_filter[crc_bits >> 3] |= BIT(crc_bits & 7);
0551         }
0552 
0553         aqc111_write_cmd_async(dev, AQ_ACCESS_MAC,
0554                        SFR_MULTI_FILTER_ARRY,
0555                        AQ_MCAST_FILTER_SIZE,
0556                        AQ_MCAST_FILTER_SIZE, m_filter);
0557 
0558         aqc111_data->rxctl |= SFR_RX_CTL_AM;
0559     }
0560 
0561     aqc111_write16_cmd_async(dev, AQ_ACCESS_MAC, SFR_RX_CTL,
0562                  2, &aqc111_data->rxctl);
0563 }
0564 
0565 static int aqc111_set_features(struct net_device *net,
0566                    netdev_features_t features)
0567 {
0568     struct usbnet *dev = netdev_priv(net);
0569     struct aqc111_data *aqc111_data = dev->driver_priv;
0570     netdev_features_t changed = net->features ^ features;
0571     u16 reg16 = 0;
0572     u8 reg8 = 0;
0573 
0574     if (changed & NETIF_F_IP_CSUM) {
0575         aqc111_read_cmd(dev, AQ_ACCESS_MAC, SFR_TXCOE_CTL, 1, 1, &reg8);
0576         reg8 ^= SFR_TXCOE_TCP | SFR_TXCOE_UDP;
0577         aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_TXCOE_CTL,
0578                  1, 1, &reg8);
0579     }
0580 
0581     if (changed & NETIF_F_IPV6_CSUM) {
0582         aqc111_read_cmd(dev, AQ_ACCESS_MAC, SFR_TXCOE_CTL, 1, 1, &reg8);
0583         reg8 ^= SFR_TXCOE_TCPV6 | SFR_TXCOE_UDPV6;
0584         aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_TXCOE_CTL,
0585                  1, 1, &reg8);
0586     }
0587 
0588     if (changed & NETIF_F_RXCSUM) {
0589         aqc111_read_cmd(dev, AQ_ACCESS_MAC, SFR_RXCOE_CTL, 1, 1, &reg8);
0590         if (features & NETIF_F_RXCSUM) {
0591             aqc111_data->rx_checksum = 1;
0592             reg8 &= ~(SFR_RXCOE_IP | SFR_RXCOE_TCP | SFR_RXCOE_UDP |
0593                   SFR_RXCOE_TCPV6 | SFR_RXCOE_UDPV6);
0594         } else {
0595             aqc111_data->rx_checksum = 0;
0596             reg8 |= SFR_RXCOE_IP | SFR_RXCOE_TCP | SFR_RXCOE_UDP |
0597                 SFR_RXCOE_TCPV6 | SFR_RXCOE_UDPV6;
0598         }
0599 
0600         aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_RXCOE_CTL,
0601                  1, 1, &reg8);
0602     }
0603     if (changed & NETIF_F_HW_VLAN_CTAG_FILTER) {
0604         if (features & NETIF_F_HW_VLAN_CTAG_FILTER) {
0605             u16 i = 0;
0606 
0607             for (i = 0; i < 256; i++) {
0608                 /* Address */
0609                 reg8 = i;
0610                 aqc111_write_cmd(dev, AQ_ACCESS_MAC,
0611                          SFR_VLAN_ID_ADDRESS,
0612                          1, 1, &reg8);
0613                 /* Data */
0614                 aqc111_write16_cmd(dev, AQ_ACCESS_MAC,
0615                            SFR_VLAN_ID_DATA0,
0616                            2, &reg16);
0617                 reg8 = SFR_VLAN_CONTROL_WE;
0618                 aqc111_write_cmd(dev, AQ_ACCESS_MAC,
0619                          SFR_VLAN_ID_CONTROL,
0620                          1, 1, &reg8);
0621             }
0622             aqc111_read_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_CONTROL,
0623                     1, 1, &reg8);
0624             reg8 |= SFR_VLAN_CONTROL_VFE;
0625             aqc111_write_cmd(dev, AQ_ACCESS_MAC,
0626                      SFR_VLAN_ID_CONTROL, 1, 1, &reg8);
0627         } else {
0628             aqc111_read_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_CONTROL,
0629                     1, 1, &reg8);
0630             reg8 &= ~SFR_VLAN_CONTROL_VFE;
0631             aqc111_write_cmd(dev, AQ_ACCESS_MAC,
0632                      SFR_VLAN_ID_CONTROL, 1, 1, &reg8);
0633         }
0634     }
0635 
0636     return 0;
0637 }
0638 
0639 static const struct net_device_ops aqc111_netdev_ops = {
0640     .ndo_open       = usbnet_open,
0641     .ndo_stop       = usbnet_stop,
0642     .ndo_start_xmit     = usbnet_start_xmit,
0643     .ndo_tx_timeout     = usbnet_tx_timeout,
0644     .ndo_get_stats64    = dev_get_tstats64,
0645     .ndo_change_mtu     = aqc111_change_mtu,
0646     .ndo_set_mac_address    = aqc111_set_mac_addr,
0647     .ndo_validate_addr  = eth_validate_addr,
0648     .ndo_vlan_rx_add_vid    = aqc111_vlan_rx_add_vid,
0649     .ndo_vlan_rx_kill_vid   = aqc111_vlan_rx_kill_vid,
0650     .ndo_set_rx_mode    = aqc111_set_rx_mode,
0651     .ndo_set_features   = aqc111_set_features,
0652 };
0653 
0654 static int aqc111_read_perm_mac(struct usbnet *dev)
0655 {
0656     u8 buf[ETH_ALEN];
0657     int ret;
0658 
0659     ret = aqc111_read_cmd(dev, AQ_FLASH_PARAMETERS, 0, 0, ETH_ALEN, buf);
0660     if (ret < 0)
0661         goto out;
0662 
0663     ether_addr_copy(dev->net->perm_addr, buf);
0664 
0665     return 0;
0666 out:
0667     return ret;
0668 }
0669 
0670 static void aqc111_read_fw_version(struct usbnet *dev,
0671                    struct aqc111_data *aqc111_data)
0672 {
0673     aqc111_read_cmd(dev, AQ_ACCESS_MAC, AQ_FW_VER_MAJOR,
0674             1, 1, &aqc111_data->fw_ver.major);
0675     aqc111_read_cmd(dev, AQ_ACCESS_MAC, AQ_FW_VER_MINOR,
0676             1, 1, &aqc111_data->fw_ver.minor);
0677     aqc111_read_cmd(dev, AQ_ACCESS_MAC, AQ_FW_VER_REV,
0678             1, 1, &aqc111_data->fw_ver.rev);
0679 
0680     if (aqc111_data->fw_ver.major & 0x80)
0681         aqc111_data->fw_ver.major &= ~0x80;
0682 }
0683 
0684 static int aqc111_bind(struct usbnet *dev, struct usb_interface *intf)
0685 {
0686     struct usb_device *udev = interface_to_usbdev(intf);
0687     enum usb_device_speed usb_speed = udev->speed;
0688     struct aqc111_data *aqc111_data;
0689     int ret;
0690 
0691     /* Check if vendor configuration */
0692     if (udev->actconfig->desc.bConfigurationValue != 1) {
0693         usb_driver_set_configuration(udev, 1);
0694         return -ENODEV;
0695     }
0696 
0697     usb_reset_configuration(dev->udev);
0698 
0699     ret = usbnet_get_endpoints(dev, intf);
0700     if (ret < 0) {
0701         netdev_dbg(dev->net, "usbnet_get_endpoints failed");
0702         return ret;
0703     }
0704 
0705     aqc111_data = kzalloc(sizeof(*aqc111_data), GFP_KERNEL);
0706     if (!aqc111_data)
0707         return -ENOMEM;
0708 
0709     /* store aqc111_data pointer in device data field */
0710     dev->driver_priv = aqc111_data;
0711 
0712     /* Init the MAC address */
0713     ret = aqc111_read_perm_mac(dev);
0714     if (ret)
0715         goto out;
0716 
0717     eth_hw_addr_set(dev->net, dev->net->perm_addr);
0718 
0719     /* Set Rx urb size */
0720     dev->rx_urb_size = URB_SIZE;
0721 
0722     /* Set TX needed headroom & tailroom */
0723     dev->net->needed_headroom += sizeof(u64);
0724     dev->net->needed_tailroom += sizeof(u64);
0725 
0726     dev->net->max_mtu = 16334;
0727 
0728     dev->net->netdev_ops = &aqc111_netdev_ops;
0729     dev->net->ethtool_ops = &aqc111_ethtool_ops;
0730 
0731     if (usb_device_no_sg_constraint(dev->udev))
0732         dev->can_dma_sg = 1;
0733 
0734     dev->net->hw_features |= AQ_SUPPORT_HW_FEATURE;
0735     dev->net->features |= AQ_SUPPORT_FEATURE;
0736     dev->net->vlan_features |= AQ_SUPPORT_VLAN_FEATURE;
0737 
0738     netif_set_tso_max_size(dev->net, 65535);
0739 
0740     aqc111_read_fw_version(dev, aqc111_data);
0741     aqc111_data->autoneg = AUTONEG_ENABLE;
0742     aqc111_data->advertised_speed = (usb_speed == USB_SPEED_SUPER) ?
0743                      SPEED_5000 : SPEED_1000;
0744 
0745     return 0;
0746 
0747 out:
0748     kfree(aqc111_data);
0749     return ret;
0750 }
0751 
0752 static void aqc111_unbind(struct usbnet *dev, struct usb_interface *intf)
0753 {
0754     struct aqc111_data *aqc111_data = dev->driver_priv;
0755     u16 reg16;
0756 
0757     /* Force bz */
0758     reg16 = SFR_PHYPWR_RSTCTL_BZ;
0759     aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_PHYPWR_RSTCTL,
0760                 2, &reg16);
0761     reg16 = 0;
0762     aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_PHYPWR_RSTCTL,
0763                 2, &reg16);
0764 
0765     /* Power down ethernet PHY */
0766     aqc111_data->phy_cfg &= ~AQ_ADV_MASK;
0767     aqc111_data->phy_cfg |= AQ_LOW_POWER;
0768     aqc111_data->phy_cfg &= ~AQ_PHY_POWER_EN;
0769     aqc111_write32_cmd_nopm(dev, AQ_PHY_OPS, 0, 0,
0770                 &aqc111_data->phy_cfg);
0771 
0772     kfree(aqc111_data);
0773 }
0774 
0775 static void aqc111_status(struct usbnet *dev, struct urb *urb)
0776 {
0777     struct aqc111_data *aqc111_data = dev->driver_priv;
0778     u64 *event_data = NULL;
0779     int link = 0;
0780 
0781     if (urb->actual_length < sizeof(*event_data))
0782         return;
0783 
0784     event_data = urb->transfer_buffer;
0785     le64_to_cpus(event_data);
0786 
0787     if (*event_data & AQ_LS_MASK)
0788         link = 1;
0789     else
0790         link = 0;
0791 
0792     aqc111_data->link_speed = (*event_data & AQ_SPEED_MASK) >>
0793                   AQ_SPEED_SHIFT;
0794     aqc111_data->link = link;
0795 
0796     if (netif_carrier_ok(dev->net) != link)
0797         usbnet_defer_kevent(dev, EVENT_LINK_RESET);
0798 }
0799 
0800 static void aqc111_configure_rx(struct usbnet *dev,
0801                 struct aqc111_data *aqc111_data)
0802 {
0803     enum usb_device_speed usb_speed = dev->udev->speed;
0804     u16 link_speed = 0, usb_host = 0;
0805     u8 buf[5] = { 0 };
0806     u8 queue_num = 0;
0807     u16 reg16 = 0;
0808     u8 reg8 = 0;
0809 
0810     buf[0] = 0x00;
0811     buf[1] = 0xF8;
0812     buf[2] = 0x07;
0813     switch (aqc111_data->link_speed) {
0814     case AQ_INT_SPEED_5G:
0815         link_speed = 5000;
0816         reg8 = 0x05;
0817         reg16 = 0x001F;
0818         break;
0819     case AQ_INT_SPEED_2_5G:
0820         link_speed = 2500;
0821         reg16 = 0x003F;
0822         break;
0823     case AQ_INT_SPEED_1G:
0824         link_speed = 1000;
0825         reg16 = 0x009F;
0826         break;
0827     case AQ_INT_SPEED_100M:
0828         link_speed = 100;
0829         queue_num = 1;
0830         reg16 = 0x063F;
0831         buf[1] = 0xFB;
0832         buf[2] = 0x4;
0833         break;
0834     }
0835 
0836     aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_INTER_PACKET_GAP_0,
0837              1, 1, &reg8);
0838 
0839     aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_TX_PAUSE_RESEND_T, 3, 3, buf);
0840 
0841     switch (usb_speed) {
0842     case USB_SPEED_SUPER:
0843         usb_host = 3;
0844         break;
0845     case USB_SPEED_HIGH:
0846         usb_host = 2;
0847         break;
0848     case USB_SPEED_FULL:
0849     case USB_SPEED_LOW:
0850         usb_host = 1;
0851         queue_num = 0;
0852         break;
0853     default:
0854         usb_host = 0;
0855         break;
0856     }
0857 
0858     if (dev->net->mtu > 12500 && dev->net->mtu <= 16334)
0859         queue_num = 2; /* For Jumbo packet 16KB */
0860 
0861     memcpy(buf, &AQC111_BULKIN_SIZE[queue_num], 5);
0862     /* RX bulk configuration */
0863     aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_RX_BULKIN_QCTRL, 5, 5, buf);
0864 
0865     /* Set high low water level */
0866     if (dev->net->mtu <= 4500)
0867         reg16 = 0x0810;
0868     else if (dev->net->mtu <= 9500)
0869         reg16 = 0x1020;
0870     else if (dev->net->mtu <= 12500)
0871         reg16 = 0x1420;
0872     else if (dev->net->mtu <= 16334)
0873         reg16 = 0x1A20;
0874 
0875     aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_PAUSE_WATERLVL_LOW,
0876                2, &reg16);
0877     netdev_info(dev->net, "Link Speed %d, USB %d", link_speed, usb_host);
0878 }
0879 
0880 static void aqc111_configure_csum_offload(struct usbnet *dev)
0881 {
0882     u8 reg8 = 0;
0883 
0884     if (dev->net->features & NETIF_F_RXCSUM) {
0885         reg8 |= SFR_RXCOE_IP | SFR_RXCOE_TCP | SFR_RXCOE_UDP |
0886             SFR_RXCOE_TCPV6 | SFR_RXCOE_UDPV6;
0887     }
0888     aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_RXCOE_CTL, 1, 1, &reg8);
0889 
0890     reg8 = 0;
0891     if (dev->net->features & NETIF_F_IP_CSUM)
0892         reg8 |= SFR_TXCOE_IP | SFR_TXCOE_TCP | SFR_TXCOE_UDP;
0893 
0894     if (dev->net->features & NETIF_F_IPV6_CSUM)
0895         reg8 |= SFR_TXCOE_TCPV6 | SFR_TXCOE_UDPV6;
0896 
0897     aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_TXCOE_CTL, 1, 1, &reg8);
0898 }
0899 
0900 static int aqc111_link_reset(struct usbnet *dev)
0901 {
0902     struct aqc111_data *aqc111_data = dev->driver_priv;
0903     u16 reg16 = 0;
0904     u8 reg8 = 0;
0905 
0906     if (aqc111_data->link == 1) { /* Link up */
0907         aqc111_configure_rx(dev, aqc111_data);
0908 
0909         /* Vlan Tag Filter */
0910         reg8 = SFR_VLAN_CONTROL_VSO;
0911         if (dev->net->features & NETIF_F_HW_VLAN_CTAG_FILTER)
0912             reg8 |= SFR_VLAN_CONTROL_VFE;
0913 
0914         aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_VLAN_ID_CONTROL,
0915                  1, 1, &reg8);
0916 
0917         reg8 = 0x0;
0918         aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_BMRX_DMA_CONTROL,
0919                  1, 1, &reg8);
0920 
0921         aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_BMTX_DMA_CONTROL,
0922                  1, 1, &reg8);
0923 
0924         aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_ARC_CTRL, 1, 1, &reg8);
0925 
0926         reg16 = SFR_RX_CTL_IPE | SFR_RX_CTL_AB;
0927         aqc111_data->rxctl = reg16;
0928         aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_RX_CTL, 2, &reg16);
0929 
0930         reg8 = SFR_RX_PATH_READY;
0931         aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_ETH_MAC_PATH,
0932                  1, 1, &reg8);
0933 
0934         reg8 = SFR_BULK_OUT_EFF_EN;
0935         aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_BULK_OUT_CTRL,
0936                  1, 1, &reg8);
0937 
0938         reg16 = 0;
0939         aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
0940                    2, &reg16);
0941 
0942         reg16 = SFR_MEDIUM_XGMIIMODE | SFR_MEDIUM_FULL_DUPLEX;
0943         aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
0944                    2, &reg16);
0945 
0946         aqc111_configure_csum_offload(dev);
0947 
0948         aqc111_set_rx_mode(dev->net);
0949 
0950         aqc111_read16_cmd(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
0951                   2, &reg16);
0952 
0953         if (dev->net->mtu > 1500)
0954             reg16 |= SFR_MEDIUM_JUMBO_EN;
0955 
0956         reg16 |= SFR_MEDIUM_RECEIVE_EN | SFR_MEDIUM_RXFLOW_CTRLEN |
0957              SFR_MEDIUM_TXFLOW_CTRLEN;
0958         aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
0959                    2, &reg16);
0960 
0961         aqc111_data->rxctl |= SFR_RX_CTL_START;
0962         aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_RX_CTL,
0963                    2, &aqc111_data->rxctl);
0964 
0965         netif_carrier_on(dev->net);
0966     } else {
0967         aqc111_read16_cmd(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
0968                   2, &reg16);
0969         reg16 &= ~SFR_MEDIUM_RECEIVE_EN;
0970         aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
0971                    2, &reg16);
0972 
0973         aqc111_data->rxctl &= ~SFR_RX_CTL_START;
0974         aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_RX_CTL,
0975                    2, &aqc111_data->rxctl);
0976 
0977         reg8 = SFR_BULK_OUT_FLUSH_EN | SFR_BULK_OUT_EFF_EN;
0978         aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_BULK_OUT_CTRL,
0979                  1, 1, &reg8);
0980         reg8 = SFR_BULK_OUT_EFF_EN;
0981         aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_BULK_OUT_CTRL,
0982                  1, 1, &reg8);
0983 
0984         netif_carrier_off(dev->net);
0985     }
0986     return 0;
0987 }
0988 
0989 static int aqc111_reset(struct usbnet *dev)
0990 {
0991     struct aqc111_data *aqc111_data = dev->driver_priv;
0992     u8 reg8 = 0;
0993 
0994     dev->rx_urb_size = URB_SIZE;
0995 
0996     if (usb_device_no_sg_constraint(dev->udev))
0997         dev->can_dma_sg = 1;
0998 
0999     dev->net->hw_features |= AQ_SUPPORT_HW_FEATURE;
1000     dev->net->features |= AQ_SUPPORT_FEATURE;
1001     dev->net->vlan_features |= AQ_SUPPORT_VLAN_FEATURE;
1002 
1003     /* Power up ethernet PHY */
1004     aqc111_data->phy_cfg = AQ_PHY_POWER_EN;
1005     aqc111_write32_cmd(dev, AQ_PHY_OPS, 0, 0,
1006                &aqc111_data->phy_cfg);
1007 
1008     /* Set the MAC address */
1009     aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_NODE_ID, ETH_ALEN,
1010              ETH_ALEN, dev->net->dev_addr);
1011 
1012     reg8 = 0xFF;
1013     aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_BM_INT_MASK, 1, 1, &reg8);
1014 
1015     reg8 = 0x0;
1016     aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_SWP_CTRL, 1, 1, &reg8);
1017 
1018     aqc111_read_cmd(dev, AQ_ACCESS_MAC, SFR_MONITOR_MODE, 1, 1, &reg8);
1019     reg8 &= ~(SFR_MONITOR_MODE_EPHYRW | SFR_MONITOR_MODE_RWLC |
1020           SFR_MONITOR_MODE_RWMP | SFR_MONITOR_MODE_RWWF |
1021           SFR_MONITOR_MODE_RW_FLAG);
1022     aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_MONITOR_MODE, 1, 1, &reg8);
1023 
1024     netif_carrier_off(dev->net);
1025 
1026     /* Phy advertise */
1027     aqc111_set_phy_speed(dev, aqc111_data->autoneg,
1028                  aqc111_data->advertised_speed);
1029 
1030     return 0;
1031 }
1032 
1033 static int aqc111_stop(struct usbnet *dev)
1034 {
1035     struct aqc111_data *aqc111_data = dev->driver_priv;
1036     u16 reg16 = 0;
1037 
1038     aqc111_read16_cmd(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
1039               2, &reg16);
1040     reg16 &= ~SFR_MEDIUM_RECEIVE_EN;
1041     aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
1042                2, &reg16);
1043     reg16 = 0;
1044     aqc111_write16_cmd(dev, AQ_ACCESS_MAC, SFR_RX_CTL, 2, &reg16);
1045 
1046     /* Put PHY to low power*/
1047     aqc111_data->phy_cfg |= AQ_LOW_POWER;
1048     aqc111_write32_cmd(dev, AQ_PHY_OPS, 0, 0,
1049                &aqc111_data->phy_cfg);
1050 
1051     netif_carrier_off(dev->net);
1052 
1053     return 0;
1054 }
1055 
1056 static void aqc111_rx_checksum(struct sk_buff *skb, u64 pkt_desc)
1057 {
1058     u32 pkt_type = 0;
1059 
1060     skb->ip_summed = CHECKSUM_NONE;
1061     /* checksum error bit is set */
1062     if (pkt_desc & AQ_RX_PD_L4_ERR || pkt_desc & AQ_RX_PD_L3_ERR)
1063         return;
1064 
1065     pkt_type = pkt_desc & AQ_RX_PD_L4_TYPE_MASK;
1066     /* It must be a TCP or UDP packet with a valid checksum */
1067     if (pkt_type == AQ_RX_PD_L4_TCP || pkt_type == AQ_RX_PD_L4_UDP)
1068         skb->ip_summed = CHECKSUM_UNNECESSARY;
1069 }
1070 
1071 static int aqc111_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
1072 {
1073     struct aqc111_data *aqc111_data = dev->driver_priv;
1074     struct sk_buff *new_skb = NULL;
1075     u32 pkt_total_offset = 0;
1076     u64 *pkt_desc_ptr = NULL;
1077     u32 start_of_descs = 0;
1078     u32 desc_offset = 0; /*RX Header Offset*/
1079     u16 pkt_count = 0;
1080     u64 desc_hdr = 0;
1081     u16 vlan_tag = 0;
1082     u32 skb_len = 0;
1083 
1084     if (!skb)
1085         goto err;
1086 
1087     if (skb->len == 0)
1088         goto err;
1089 
1090     skb_len = skb->len;
1091     /* RX Descriptor Header */
1092     skb_trim(skb, skb->len - sizeof(desc_hdr));
1093     desc_hdr = le64_to_cpup((u64 *)skb_tail_pointer(skb));
1094 
1095     /* Check these packets */
1096     desc_offset = (desc_hdr & AQ_RX_DH_DESC_OFFSET_MASK) >>
1097               AQ_RX_DH_DESC_OFFSET_SHIFT;
1098     pkt_count = desc_hdr & AQ_RX_DH_PKT_CNT_MASK;
1099     start_of_descs = skb_len - ((pkt_count + 1) *  sizeof(desc_hdr));
1100 
1101     /* self check descs position */
1102     if (start_of_descs != desc_offset)
1103         goto err;
1104 
1105     /* self check desc_offset from header and make sure that the
1106      * bounds of the metadata array are inside the SKB
1107      */
1108     if (pkt_count * 2 + desc_offset >= skb_len)
1109         goto err;
1110 
1111     /* Packets must not overlap the metadata array */
1112     skb_trim(skb, desc_offset);
1113 
1114     if (pkt_count == 0)
1115         goto err;
1116 
1117     /* Get the first RX packet descriptor */
1118     pkt_desc_ptr = (u64 *)(skb->data + desc_offset);
1119 
1120     while (pkt_count--) {
1121         u64 pkt_desc = le64_to_cpup(pkt_desc_ptr);
1122         u32 pkt_len_with_padd = 0;
1123         u32 pkt_len = 0;
1124 
1125         pkt_len = (u32)((pkt_desc & AQ_RX_PD_LEN_MASK) >>
1126               AQ_RX_PD_LEN_SHIFT);
1127         pkt_len_with_padd = ((pkt_len + 7) & 0x7FFF8);
1128 
1129         pkt_total_offset += pkt_len_with_padd;
1130         if (pkt_total_offset > desc_offset ||
1131             (pkt_count == 0 && pkt_total_offset != desc_offset)) {
1132             goto err;
1133         }
1134 
1135         if (pkt_desc & AQ_RX_PD_DROP ||
1136             !(pkt_desc & AQ_RX_PD_RX_OK) ||
1137             pkt_len > (dev->hard_mtu + AQ_RX_HW_PAD)) {
1138             skb_pull(skb, pkt_len_with_padd);
1139             /* Next RX Packet Descriptor */
1140             pkt_desc_ptr++;
1141             continue;
1142         }
1143 
1144         /* Clone SKB */
1145         new_skb = skb_clone(skb, GFP_ATOMIC);
1146 
1147         if (!new_skb)
1148             goto err;
1149 
1150         new_skb->len = pkt_len;
1151         skb_pull(new_skb, AQ_RX_HW_PAD);
1152         skb_set_tail_pointer(new_skb, new_skb->len);
1153 
1154         new_skb->truesize = SKB_TRUESIZE(new_skb->len);
1155         if (aqc111_data->rx_checksum)
1156             aqc111_rx_checksum(new_skb, pkt_desc);
1157 
1158         if (pkt_desc & AQ_RX_PD_VLAN) {
1159             vlan_tag = pkt_desc >> AQ_RX_PD_VLAN_SHIFT;
1160             __vlan_hwaccel_put_tag(new_skb, htons(ETH_P_8021Q),
1161                            vlan_tag & VLAN_VID_MASK);
1162         }
1163 
1164         usbnet_skb_return(dev, new_skb);
1165         if (pkt_count == 0)
1166             break;
1167 
1168         skb_pull(skb, pkt_len_with_padd);
1169 
1170         /* Next RX Packet Header */
1171         pkt_desc_ptr++;
1172 
1173         new_skb = NULL;
1174     }
1175 
1176     return 1;
1177 
1178 err:
1179     return 0;
1180 }
1181 
1182 static struct sk_buff *aqc111_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
1183                        gfp_t flags)
1184 {
1185     int frame_size = dev->maxpacket;
1186     struct sk_buff *new_skb = NULL;
1187     u64 *tx_desc_ptr = NULL;
1188     int padding_size = 0;
1189     int headroom = 0;
1190     int tailroom = 0;
1191     u64 tx_desc = 0;
1192     u16 tci = 0;
1193 
1194     /*Length of actual data*/
1195     tx_desc |= skb->len & AQ_TX_DESC_LEN_MASK;
1196 
1197     /* TSO MSS */
1198     tx_desc |= ((u64)(skb_shinfo(skb)->gso_size & AQ_TX_DESC_MSS_MASK)) <<
1199            AQ_TX_DESC_MSS_SHIFT;
1200 
1201     headroom = (skb->len + sizeof(tx_desc)) % 8;
1202     if (headroom != 0)
1203         padding_size = 8 - headroom;
1204 
1205     if (((skb->len + sizeof(tx_desc) + padding_size) % frame_size) == 0) {
1206         padding_size += 8;
1207         tx_desc |= AQ_TX_DESC_DROP_PADD;
1208     }
1209 
1210     /* Vlan Tag */
1211     if (vlan_get_tag(skb, &tci) >= 0) {
1212         tx_desc |= AQ_TX_DESC_VLAN;
1213         tx_desc |= ((u64)tci & AQ_TX_DESC_VLAN_MASK) <<
1214                AQ_TX_DESC_VLAN_SHIFT;
1215     }
1216 
1217     if (!dev->can_dma_sg && (dev->net->features & NETIF_F_SG) &&
1218         skb_linearize(skb))
1219         return NULL;
1220 
1221     headroom = skb_headroom(skb);
1222     tailroom = skb_tailroom(skb);
1223 
1224     if (!(headroom >= sizeof(tx_desc) && tailroom >= padding_size)) {
1225         new_skb = skb_copy_expand(skb, sizeof(tx_desc),
1226                       padding_size, flags);
1227         dev_kfree_skb_any(skb);
1228         skb = new_skb;
1229         if (!skb)
1230             return NULL;
1231     }
1232     if (padding_size != 0)
1233         skb_put_zero(skb, padding_size);
1234     /* Copy TX header */
1235     tx_desc_ptr = skb_push(skb, sizeof(tx_desc));
1236     *tx_desc_ptr = cpu_to_le64(tx_desc);
1237 
1238     usbnet_set_skb_tx_stats(skb, 1, 0);
1239 
1240     return skb;
1241 }
1242 
1243 static const struct driver_info aqc111_info = {
1244     .description    = "Aquantia AQtion USB to 5GbE Controller",
1245     .bind       = aqc111_bind,
1246     .unbind     = aqc111_unbind,
1247     .status     = aqc111_status,
1248     .link_reset = aqc111_link_reset,
1249     .reset      = aqc111_reset,
1250     .stop       = aqc111_stop,
1251     .flags      = FLAG_ETHER | FLAG_FRAMING_AX |
1252               FLAG_AVOID_UNLINK_URBS | FLAG_MULTI_PACKET,
1253     .rx_fixup   = aqc111_rx_fixup,
1254     .tx_fixup   = aqc111_tx_fixup,
1255 };
1256 
1257 #define ASIX111_DESC \
1258 "ASIX USB 3.1 Gen1 to 5G Multi-Gigabit Ethernet Adapter"
1259 
1260 static const struct driver_info asix111_info = {
1261     .description    = ASIX111_DESC,
1262     .bind       = aqc111_bind,
1263     .unbind     = aqc111_unbind,
1264     .status     = aqc111_status,
1265     .link_reset = aqc111_link_reset,
1266     .reset      = aqc111_reset,
1267     .stop       = aqc111_stop,
1268     .flags      = FLAG_ETHER | FLAG_FRAMING_AX |
1269               FLAG_AVOID_UNLINK_URBS | FLAG_MULTI_PACKET,
1270     .rx_fixup   = aqc111_rx_fixup,
1271     .tx_fixup   = aqc111_tx_fixup,
1272 };
1273 
1274 #undef ASIX111_DESC
1275 
1276 #define ASIX112_DESC \
1277 "ASIX USB 3.1 Gen1 to 2.5G Multi-Gigabit Ethernet Adapter"
1278 
1279 static const struct driver_info asix112_info = {
1280     .description    = ASIX112_DESC,
1281     .bind       = aqc111_bind,
1282     .unbind     = aqc111_unbind,
1283     .status     = aqc111_status,
1284     .link_reset = aqc111_link_reset,
1285     .reset      = aqc111_reset,
1286     .stop       = aqc111_stop,
1287     .flags      = FLAG_ETHER | FLAG_FRAMING_AX |
1288               FLAG_AVOID_UNLINK_URBS | FLAG_MULTI_PACKET,
1289     .rx_fixup   = aqc111_rx_fixup,
1290     .tx_fixup   = aqc111_tx_fixup,
1291 };
1292 
1293 #undef ASIX112_DESC
1294 
1295 static const struct driver_info trendnet_info = {
1296     .description    = "USB-C 3.1 to 5GBASE-T Ethernet Adapter",
1297     .bind       = aqc111_bind,
1298     .unbind     = aqc111_unbind,
1299     .status     = aqc111_status,
1300     .link_reset = aqc111_link_reset,
1301     .reset      = aqc111_reset,
1302     .stop       = aqc111_stop,
1303     .flags      = FLAG_ETHER | FLAG_FRAMING_AX |
1304               FLAG_AVOID_UNLINK_URBS | FLAG_MULTI_PACKET,
1305     .rx_fixup   = aqc111_rx_fixup,
1306     .tx_fixup   = aqc111_tx_fixup,
1307 };
1308 
1309 static const struct driver_info qnap_info = {
1310     .description    = "QNAP QNA-UC5G1T USB to 5GbE Adapter",
1311     .bind       = aqc111_bind,
1312     .unbind     = aqc111_unbind,
1313     .status     = aqc111_status,
1314     .link_reset = aqc111_link_reset,
1315     .reset      = aqc111_reset,
1316     .stop       = aqc111_stop,
1317     .flags      = FLAG_ETHER | FLAG_FRAMING_AX |
1318               FLAG_AVOID_UNLINK_URBS | FLAG_MULTI_PACKET,
1319     .rx_fixup   = aqc111_rx_fixup,
1320     .tx_fixup   = aqc111_tx_fixup,
1321 };
1322 
1323 static int aqc111_suspend(struct usb_interface *intf, pm_message_t message)
1324 {
1325     struct usbnet *dev = usb_get_intfdata(intf);
1326     struct aqc111_data *aqc111_data = dev->driver_priv;
1327     u16 temp_rx_ctrl = 0x00;
1328     u16 reg16;
1329     u8 reg8;
1330 
1331     usbnet_suspend(intf, message);
1332 
1333     aqc111_read16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_RX_CTL, 2, &reg16);
1334     temp_rx_ctrl = reg16;
1335     /* Stop RX operations*/
1336     reg16 &= ~SFR_RX_CTL_START;
1337     aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_RX_CTL, 2, &reg16);
1338     /* Force bz */
1339     aqc111_read16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_PHYPWR_RSTCTL,
1340                    2, &reg16);
1341     reg16 |= SFR_PHYPWR_RSTCTL_BZ;
1342     aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_PHYPWR_RSTCTL,
1343                 2, &reg16);
1344 
1345     reg8 = SFR_BULK_OUT_EFF_EN;
1346     aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_BULK_OUT_CTRL,
1347                   1, 1, &reg8);
1348 
1349     temp_rx_ctrl &= ~(SFR_RX_CTL_START | SFR_RX_CTL_RF_WAK |
1350               SFR_RX_CTL_AP | SFR_RX_CTL_AM);
1351     aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_RX_CTL,
1352                 2, &temp_rx_ctrl);
1353 
1354     reg8 = 0x00;
1355     aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_ETH_MAC_PATH,
1356                   1, 1, &reg8);
1357 
1358     if (aqc111_data->wol_flags) {
1359         struct aqc111_wol_cfg wol_cfg;
1360 
1361         memset(&wol_cfg, 0, sizeof(struct aqc111_wol_cfg));
1362 
1363         aqc111_data->phy_cfg |= AQ_WOL;
1364         ether_addr_copy(wol_cfg.hw_addr, dev->net->dev_addr);
1365         wol_cfg.flags = aqc111_data->wol_flags;
1366 
1367         temp_rx_ctrl |= (SFR_RX_CTL_AB | SFR_RX_CTL_START);
1368         aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_RX_CTL,
1369                     2, &temp_rx_ctrl);
1370         reg8 = 0x00;
1371         aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_BM_INT_MASK,
1372                       1, 1, &reg8);
1373         reg8 = SFR_BMRX_DMA_EN;
1374         aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_BMRX_DMA_CONTROL,
1375                       1, 1, &reg8);
1376         reg8 = SFR_RX_PATH_READY;
1377         aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_ETH_MAC_PATH,
1378                       1, 1, &reg8);
1379         reg8 = 0x07;
1380         aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_RX_BULKIN_QCTRL,
1381                       1, 1, &reg8);
1382         reg8 = 0x00;
1383         aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC,
1384                       SFR_RX_BULKIN_QTIMR_LOW, 1, 1, &reg8);
1385         aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC,
1386                       SFR_RX_BULKIN_QTIMR_HIGH, 1, 1, &reg8);
1387         reg8 = 0xFF;
1388         aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_RX_BULKIN_QSIZE,
1389                       1, 1, &reg8);
1390         aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_RX_BULKIN_QIFG,
1391                       1, 1, &reg8);
1392 
1393         aqc111_read16_cmd_nopm(dev, AQ_ACCESS_MAC,
1394                        SFR_MEDIUM_STATUS_MODE, 2, &reg16);
1395         reg16 |= SFR_MEDIUM_RECEIVE_EN;
1396         aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC,
1397                     SFR_MEDIUM_STATUS_MODE, 2, &reg16);
1398 
1399         aqc111_write_cmd(dev, AQ_WOL_CFG, 0, 0,
1400                  WOL_CFG_SIZE, &wol_cfg);
1401         aqc111_write32_cmd(dev, AQ_PHY_OPS, 0, 0,
1402                    &aqc111_data->phy_cfg);
1403     } else {
1404         aqc111_data->phy_cfg |= AQ_LOW_POWER;
1405         aqc111_write32_cmd(dev, AQ_PHY_OPS, 0, 0,
1406                    &aqc111_data->phy_cfg);
1407 
1408         /* Disable RX path */
1409         aqc111_read16_cmd_nopm(dev, AQ_ACCESS_MAC,
1410                        SFR_MEDIUM_STATUS_MODE, 2, &reg16);
1411         reg16 &= ~SFR_MEDIUM_RECEIVE_EN;
1412         aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC,
1413                     SFR_MEDIUM_STATUS_MODE, 2, &reg16);
1414     }
1415 
1416     return 0;
1417 }
1418 
1419 static int aqc111_resume(struct usb_interface *intf)
1420 {
1421     struct usbnet *dev = usb_get_intfdata(intf);
1422     struct aqc111_data *aqc111_data = dev->driver_priv;
1423     u16 reg16;
1424     u8 reg8;
1425 
1426     netif_carrier_off(dev->net);
1427 
1428     /* Power up ethernet PHY */
1429     aqc111_data->phy_cfg |= AQ_PHY_POWER_EN;
1430     aqc111_data->phy_cfg &= ~AQ_LOW_POWER;
1431     aqc111_data->phy_cfg &= ~AQ_WOL;
1432 
1433     reg8 = 0xFF;
1434     aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_BM_INT_MASK,
1435                   1, 1, &reg8);
1436     /* Configure RX control register => start operation */
1437     reg16 = aqc111_data->rxctl;
1438     reg16 &= ~SFR_RX_CTL_START;
1439     aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_RX_CTL, 2, &reg16);
1440 
1441     reg16 |= SFR_RX_CTL_START;
1442     aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_RX_CTL, 2, &reg16);
1443 
1444     aqc111_set_phy_speed(dev, aqc111_data->autoneg,
1445                  aqc111_data->advertised_speed);
1446 
1447     aqc111_read16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
1448                    2, &reg16);
1449     reg16 |= SFR_MEDIUM_RECEIVE_EN;
1450     aqc111_write16_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_MEDIUM_STATUS_MODE,
1451                 2, &reg16);
1452     reg8 = SFR_RX_PATH_READY;
1453     aqc111_write_cmd_nopm(dev, AQ_ACCESS_MAC, SFR_ETH_MAC_PATH,
1454                   1, 1, &reg8);
1455     reg8 = 0x0;
1456     aqc111_write_cmd(dev, AQ_ACCESS_MAC, SFR_BMRX_DMA_CONTROL, 1, 1, &reg8);
1457 
1458     return usbnet_resume(intf);
1459 }
1460 
1461 #define AQC111_USB_ETH_DEV(vid, pid, table) \
1462     USB_DEVICE_INTERFACE_CLASS((vid), (pid), USB_CLASS_VENDOR_SPEC), \
1463     .driver_info = (unsigned long)&(table) \
1464 }, \
1465 { \
1466     USB_DEVICE_AND_INTERFACE_INFO((vid), (pid), \
1467                       USB_CLASS_COMM, \
1468                       USB_CDC_SUBCLASS_ETHERNET, \
1469                       USB_CDC_PROTO_NONE), \
1470     .driver_info = (unsigned long)&(table),
1471 
1472 static const struct usb_device_id products[] = {
1473     {AQC111_USB_ETH_DEV(0x2eca, 0xc101, aqc111_info)},
1474     {AQC111_USB_ETH_DEV(0x0b95, 0x2790, asix111_info)},
1475     {AQC111_USB_ETH_DEV(0x0b95, 0x2791, asix112_info)},
1476     {AQC111_USB_ETH_DEV(0x20f4, 0xe05a, trendnet_info)},
1477     {AQC111_USB_ETH_DEV(0x1c04, 0x0015, qnap_info)},
1478     { },/* END */
1479 };
1480 MODULE_DEVICE_TABLE(usb, products);
1481 
1482 static struct usb_driver aq_driver = {
1483     .name       = "aqc111",
1484     .id_table   = products,
1485     .probe      = usbnet_probe,
1486     .suspend    = aqc111_suspend,
1487     .resume     = aqc111_resume,
1488     .disconnect = usbnet_disconnect,
1489 };
1490 
1491 module_usb_driver(aq_driver);
1492 
1493 MODULE_DESCRIPTION("Aquantia AQtion USB to 5/2.5GbE Controllers");
1494 MODULE_LICENSE("GPL");