1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /***************************************************************************
4 * Copyright (C) 2007-2008 SMSC
6 *****************************************************************************/
8 #include <linux/module.h>
9 #include <linux/kmod.h>
10 #include <linux/netdevice.h>
11 #include <linux/etherdevice.h>
12 #include <linux/ethtool.h>
13 #include <linux/mii.h>
14 #include <linux/usb.h>
15 #include <linux/bitrev.h>
16 #include <linux/crc16.h>
17 #include <linux/crc32.h>
18 #include <linux/usb/usbnet.h>
19 #include <linux/slab.h>
20 #include <linux/of_net.h>
21 #include <linux/irq.h>
22 #include <linux/irqdomain.h>
23 #include <linux/mdio.h>
24 #include <linux/phy.h>
25 #include <net/selftests.h>
29 #define SMSC_CHIPNAME "smsc95xx"
30 #define SMSC_DRIVER_VERSION "2.0.0"
31 #define HS_USB_PKT_SIZE (512)
32 #define FS_USB_PKT_SIZE (64)
33 #define DEFAULT_HS_BURST_CAP_SIZE (16 * 1024 + 5 * HS_USB_PKT_SIZE)
34 #define DEFAULT_FS_BURST_CAP_SIZE (6 * 1024 + 33 * FS_USB_PKT_SIZE)
35 #define DEFAULT_BULK_IN_DELAY (0x00002000)
36 #define MAX_SINGLE_PACKET_SIZE (2048)
37 #define LAN95XX_EEPROM_MAGIC (0x9500)
38 #define EEPROM_MAC_OFFSET (0x01)
39 #define DEFAULT_TX_CSUM_ENABLE (true)
40 #define DEFAULT_RX_CSUM_ENABLE (true)
41 #define SMSC95XX_INTERNAL_PHY_ID (1)
42 #define SMSC95XX_TX_OVERHEAD (8)
43 #define SMSC95XX_TX_OVERHEAD_CSUM (12)
44 #define SUPPORTED_WAKE (WAKE_PHY | WAKE_UCAST | WAKE_BCAST | \
45 WAKE_MCAST | WAKE_ARP | WAKE_MAGIC)
47 #define FEATURE_8_WAKEUP_FILTERS (0x01)
48 #define FEATURE_PHY_NLP_CROSSOVER (0x02)
49 #define FEATURE_REMOTE_WAKEUP (0x04)
51 #define SUSPEND_SUSPEND0 (0x01)
52 #define SUSPEND_SUSPEND1 (0x02)
53 #define SUSPEND_SUSPEND2 (0x04)
54 #define SUSPEND_SUSPEND3 (0x08)
55 #define SUSPEND_ALLMODES (SUSPEND_SUSPEND0 | SUSPEND_SUSPEND1 | \
56 SUSPEND_SUSPEND2 | SUSPEND_SUSPEND3)
58 #define SMSC95XX_NR_IRQS (1) /* raise to 12 for GPIOs */
59 #define PHY_HWIRQ (SMSC95XX_NR_IRQS - 1)
61 struct smsc95xx_priv {
66 spinlock_t mac_cr_lock;
69 struct irq_chip irqchip;
70 struct irq_domain *irqdomain;
71 struct fwnode_handle *irqfwnode;
72 struct mii_bus *mdiobus;
73 struct phy_device *phydev;
76 static bool turbo_mode = true;
77 module_param(turbo_mode, bool, 0644);
78 MODULE_PARM_DESC(turbo_mode, "Enable multiple frames per Rx transaction");
80 static int __must_check __smsc95xx_read_reg(struct usbnet *dev, u32 index,
85 int (*fn)(struct usbnet *, u8, u8, u16, u16, void *, u16);
92 fn = usbnet_read_cmd_nopm;
94 ret = fn(dev, USB_VENDOR_REQUEST_READ_REGISTER, USB_DIR_IN
95 | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
99 netdev_warn(dev->net, "Failed to read reg index 0x%08x: %d\n",
110 static int __must_check __smsc95xx_write_reg(struct usbnet *dev, u32 index,
115 int (*fn)(struct usbnet *, u8, u8, u16, u16, const void *, u16);
120 fn = usbnet_write_cmd;
122 fn = usbnet_write_cmd_nopm;
127 ret = fn(dev, USB_VENDOR_REQUEST_WRITE_REGISTER, USB_DIR_OUT
128 | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
130 if (ret < 0 && ret != -ENODEV)
131 netdev_warn(dev->net, "Failed to write reg index 0x%08x: %d\n",
137 static int __must_check smsc95xx_read_reg_nopm(struct usbnet *dev, u32 index,
140 return __smsc95xx_read_reg(dev, index, data, 1);
143 static int __must_check smsc95xx_write_reg_nopm(struct usbnet *dev, u32 index,
146 return __smsc95xx_write_reg(dev, index, data, 1);
149 static int __must_check smsc95xx_read_reg(struct usbnet *dev, u32 index,
152 return __smsc95xx_read_reg(dev, index, data, 0);
155 static int __must_check smsc95xx_write_reg(struct usbnet *dev, u32 index,
158 return __smsc95xx_write_reg(dev, index, data, 0);
161 /* Loop until the read is completed with timeout
162 * called with phy_mutex held */
163 static int __must_check __smsc95xx_phy_wait_not_busy(struct usbnet *dev,
166 unsigned long start_time = jiffies;
171 ret = __smsc95xx_read_reg(dev, MII_ADDR, &val, in_pm);
173 /* Ignore -ENODEV error during disconnect() */
176 netdev_warn(dev->net, "Error reading MII_ACCESS\n");
180 if (!(val & MII_BUSY_))
182 } while (!time_after(jiffies, start_time + HZ));
187 static u32 mii_address_cmd(int phy_id, int idx, u16 op)
189 return (phy_id & 0x1f) << 11 | (idx & 0x1f) << 6 | op;
192 static int __smsc95xx_mdio_read(struct usbnet *dev, int phy_id, int idx,
198 mutex_lock(&dev->phy_mutex);
200 /* confirm MII not busy */
201 ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
203 netdev_warn(dev->net, "%s: MII is busy\n", __func__);
207 /* set the address, index & direction (read from PHY) */
208 addr = mii_address_cmd(phy_id, idx, MII_READ_ | MII_BUSY_);
209 ret = __smsc95xx_write_reg(dev, MII_ADDR, addr, in_pm);
212 netdev_warn(dev->net, "Error writing MII_ADDR\n");
216 ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
218 netdev_warn(dev->net, "Timed out reading MII reg %02X\n", idx);
222 ret = __smsc95xx_read_reg(dev, MII_DATA, &val, in_pm);
225 netdev_warn(dev->net, "Error reading MII_DATA\n");
229 ret = (u16)(val & 0xFFFF);
232 mutex_unlock(&dev->phy_mutex);
234 /* Ignore -ENODEV error during disconnect() */
240 static void __smsc95xx_mdio_write(struct usbnet *dev, int phy_id,
241 int idx, int regval, int in_pm)
246 mutex_lock(&dev->phy_mutex);
248 /* confirm MII not busy */
249 ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
251 netdev_warn(dev->net, "%s: MII is busy\n", __func__);
256 ret = __smsc95xx_write_reg(dev, MII_DATA, val, in_pm);
259 netdev_warn(dev->net, "Error writing MII_DATA\n");
263 /* set the address, index & direction (write to PHY) */
264 addr = mii_address_cmd(phy_id, idx, MII_WRITE_ | MII_BUSY_);
265 ret = __smsc95xx_write_reg(dev, MII_ADDR, addr, in_pm);
268 netdev_warn(dev->net, "Error writing MII_ADDR\n");
272 ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
274 netdev_warn(dev->net, "Timed out writing MII reg %02X\n", idx);
279 mutex_unlock(&dev->phy_mutex);
282 static int smsc95xx_mdio_read_nopm(struct usbnet *dev, int idx)
284 struct smsc95xx_priv *pdata = dev->driver_priv;
286 return __smsc95xx_mdio_read(dev, pdata->phydev->mdio.addr, idx, 1);
289 static void smsc95xx_mdio_write_nopm(struct usbnet *dev, int idx, int regval)
291 struct smsc95xx_priv *pdata = dev->driver_priv;
293 __smsc95xx_mdio_write(dev, pdata->phydev->mdio.addr, idx, regval, 1);
296 static int smsc95xx_mdiobus_read(struct mii_bus *bus, int phy_id, int idx)
298 struct usbnet *dev = bus->priv;
300 return __smsc95xx_mdio_read(dev, phy_id, idx, 0);
303 static int smsc95xx_mdiobus_write(struct mii_bus *bus, int phy_id, int idx,
306 struct usbnet *dev = bus->priv;
308 __smsc95xx_mdio_write(dev, phy_id, idx, regval, 0);
312 static int __must_check smsc95xx_wait_eeprom(struct usbnet *dev)
314 unsigned long start_time = jiffies;
319 ret = smsc95xx_read_reg(dev, E2P_CMD, &val);
321 netdev_warn(dev->net, "Error reading E2P_CMD\n");
325 if (!(val & E2P_CMD_BUSY_) || (val & E2P_CMD_TIMEOUT_))
328 } while (!time_after(jiffies, start_time + HZ));
330 if (val & (E2P_CMD_TIMEOUT_ | E2P_CMD_BUSY_)) {
331 netdev_warn(dev->net, "EEPROM read operation timeout\n");
338 static int __must_check smsc95xx_eeprom_confirm_not_busy(struct usbnet *dev)
340 unsigned long start_time = jiffies;
345 ret = smsc95xx_read_reg(dev, E2P_CMD, &val);
347 netdev_warn(dev->net, "Error reading E2P_CMD\n");
351 if (!(val & E2P_CMD_BUSY_))
355 } while (!time_after(jiffies, start_time + HZ));
357 netdev_warn(dev->net, "EEPROM is busy\n");
361 static int smsc95xx_read_eeprom(struct usbnet *dev, u32 offset, u32 length,
370 ret = smsc95xx_eeprom_confirm_not_busy(dev);
374 for (i = 0; i < length; i++) {
375 val = E2P_CMD_BUSY_ | E2P_CMD_READ_ | (offset & E2P_CMD_ADDR_);
376 ret = smsc95xx_write_reg(dev, E2P_CMD, val);
378 netdev_warn(dev->net, "Error writing E2P_CMD\n");
382 ret = smsc95xx_wait_eeprom(dev);
386 ret = smsc95xx_read_reg(dev, E2P_DATA, &val);
388 netdev_warn(dev->net, "Error reading E2P_DATA\n");
392 data[i] = val & 0xFF;
399 static int smsc95xx_write_eeprom(struct usbnet *dev, u32 offset, u32 length,
408 ret = smsc95xx_eeprom_confirm_not_busy(dev);
412 /* Issue write/erase enable command */
413 val = E2P_CMD_BUSY_ | E2P_CMD_EWEN_;
414 ret = smsc95xx_write_reg(dev, E2P_CMD, val);
416 netdev_warn(dev->net, "Error writing E2P_DATA\n");
420 ret = smsc95xx_wait_eeprom(dev);
424 for (i = 0; i < length; i++) {
426 /* Fill data register */
428 ret = smsc95xx_write_reg(dev, E2P_DATA, val);
430 netdev_warn(dev->net, "Error writing E2P_DATA\n");
434 /* Send "write" command */
435 val = E2P_CMD_BUSY_ | E2P_CMD_WRITE_ | (offset & E2P_CMD_ADDR_);
436 ret = smsc95xx_write_reg(dev, E2P_CMD, val);
438 netdev_warn(dev->net, "Error writing E2P_CMD\n");
442 ret = smsc95xx_wait_eeprom(dev);
452 static int __must_check smsc95xx_write_reg_async(struct usbnet *dev, u16 index,
462 ret = usbnet_write_cmd_async(dev, USB_VENDOR_REQUEST_WRITE_REGISTER,
463 USB_DIR_OUT | USB_TYPE_VENDOR |
465 0, index, &buf, size);
467 netdev_warn(dev->net, "Error write async cmd, sts=%d\n",
472 /* returns hash bit number for given MAC address
474 * 01 00 5E 00 00 01 -> returns bit number 31 */
475 static unsigned int smsc95xx_hash(char addr[ETH_ALEN])
477 return (ether_crc(ETH_ALEN, addr) >> 26) & 0x3f;
480 static void smsc95xx_set_multicast(struct net_device *netdev)
482 struct usbnet *dev = netdev_priv(netdev);
483 struct smsc95xx_priv *pdata = dev->driver_priv;
490 spin_lock_irqsave(&pdata->mac_cr_lock, flags);
492 if (dev->net->flags & IFF_PROMISC) {
493 netif_dbg(dev, drv, dev->net, "promiscuous mode enabled\n");
494 pdata->mac_cr |= MAC_CR_PRMS_;
495 pdata->mac_cr &= ~(MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
496 } else if (dev->net->flags & IFF_ALLMULTI) {
497 netif_dbg(dev, drv, dev->net, "receive all multicast enabled\n");
498 pdata->mac_cr |= MAC_CR_MCPAS_;
499 pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_HPFILT_);
500 } else if (!netdev_mc_empty(dev->net)) {
501 struct netdev_hw_addr *ha;
503 pdata->mac_cr |= MAC_CR_HPFILT_;
504 pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_);
506 netdev_for_each_mc_addr(ha, netdev) {
507 u32 bitnum = smsc95xx_hash(ha->addr);
508 u32 mask = 0x01 << (bitnum & 0x1F);
510 pdata->hash_hi |= mask;
512 pdata->hash_lo |= mask;
515 netif_dbg(dev, drv, dev->net, "HASHH=0x%08X, HASHL=0x%08X\n",
516 pdata->hash_hi, pdata->hash_lo);
518 netif_dbg(dev, drv, dev->net, "receive own packets only\n");
520 ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
523 spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
525 /* Initiate async writes, as we can't wait for completion here */
526 ret = smsc95xx_write_reg_async(dev, HASHH, pdata->hash_hi);
528 netdev_warn(dev->net, "failed to initiate async write to HASHH\n");
530 ret = smsc95xx_write_reg_async(dev, HASHL, pdata->hash_lo);
532 netdev_warn(dev->net, "failed to initiate async write to HASHL\n");
534 ret = smsc95xx_write_reg_async(dev, MAC_CR, pdata->mac_cr);
536 netdev_warn(dev->net, "failed to initiate async write to MAC_CR\n");
539 static int smsc95xx_phy_update_flowcontrol(struct usbnet *dev)
541 u32 flow = 0, afc_cfg;
542 struct smsc95xx_priv *pdata = dev->driver_priv;
543 bool tx_pause, rx_pause;
545 int ret = smsc95xx_read_reg(dev, AFC_CFG, &afc_cfg);
549 if (pdata->phydev->duplex == DUPLEX_FULL) {
550 phy_get_pause(pdata->phydev, &tx_pause, &rx_pause);
562 netif_dbg(dev, link, dev->net, "rx pause %s, tx pause %s\n",
563 rx_pause ? "enabled" : "disabled",
564 tx_pause ? "enabled" : "disabled");
566 netif_dbg(dev, link, dev->net, "half duplex\n");
570 ret = smsc95xx_write_reg(dev, FLOW, flow);
574 return smsc95xx_write_reg(dev, AFC_CFG, afc_cfg);
577 static void smsc95xx_mac_update_fullduplex(struct usbnet *dev)
579 struct smsc95xx_priv *pdata = dev->driver_priv;
583 spin_lock_irqsave(&pdata->mac_cr_lock, flags);
584 if (pdata->phydev->duplex != DUPLEX_FULL) {
585 pdata->mac_cr &= ~MAC_CR_FDPX_;
586 pdata->mac_cr |= MAC_CR_RCVOWN_;
588 pdata->mac_cr &= ~MAC_CR_RCVOWN_;
589 pdata->mac_cr |= MAC_CR_FDPX_;
591 spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
593 ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
596 netdev_warn(dev->net,
597 "Error updating MAC full duplex mode\n");
601 ret = smsc95xx_phy_update_flowcontrol(dev);
603 netdev_warn(dev->net, "Error updating PHY flow control\n");
606 static void smsc95xx_status(struct usbnet *dev, struct urb *urb)
608 struct smsc95xx_priv *pdata = dev->driver_priv;
612 if (urb->actual_length != 4) {
613 netdev_warn(dev->net, "unexpected urb length %d\n",
618 intdata = get_unaligned_le32(urb->transfer_buffer);
619 netif_dbg(dev, link, dev->net, "intdata: 0x%08X\n", intdata);
621 local_irq_save(flags);
623 if (intdata & INT_ENP_PHY_INT_)
624 generic_handle_domain_irq(pdata->irqdomain, PHY_HWIRQ);
626 netdev_warn(dev->net, "unexpected interrupt, intdata=0x%08X\n",
629 local_irq_restore(flags);
632 /* Enable or disable Tx & Rx checksum offload engines */
633 static int smsc95xx_set_features(struct net_device *netdev,
634 netdev_features_t features)
636 struct usbnet *dev = netdev_priv(netdev);
640 ret = smsc95xx_read_reg(dev, COE_CR, &read_buf);
644 if (features & NETIF_F_IP_CSUM)
645 read_buf |= Tx_COE_EN_;
647 read_buf &= ~Tx_COE_EN_;
649 if (features & NETIF_F_RXCSUM)
650 read_buf |= Rx_COE_EN_;
652 read_buf &= ~Rx_COE_EN_;
654 ret = smsc95xx_write_reg(dev, COE_CR, read_buf);
658 netif_dbg(dev, hw, dev->net, "COE_CR = 0x%08x\n", read_buf);
662 static int smsc95xx_ethtool_get_eeprom_len(struct net_device *net)
664 return MAX_EEPROM_SIZE;
667 static int smsc95xx_ethtool_get_eeprom(struct net_device *netdev,
668 struct ethtool_eeprom *ee, u8 *data)
670 struct usbnet *dev = netdev_priv(netdev);
672 ee->magic = LAN95XX_EEPROM_MAGIC;
674 return smsc95xx_read_eeprom(dev, ee->offset, ee->len, data);
677 static int smsc95xx_ethtool_set_eeprom(struct net_device *netdev,
678 struct ethtool_eeprom *ee, u8 *data)
680 struct usbnet *dev = netdev_priv(netdev);
682 if (ee->magic != LAN95XX_EEPROM_MAGIC) {
683 netdev_warn(dev->net, "EEPROM: magic value mismatch, magic = 0x%x\n",
688 return smsc95xx_write_eeprom(dev, ee->offset, ee->len, data);
691 static int smsc95xx_ethtool_getregslen(struct net_device *netdev)
693 /* all smsc95xx registers */
694 return COE_CR - ID_REV + sizeof(u32);
698 smsc95xx_ethtool_getregs(struct net_device *netdev, struct ethtool_regs *regs,
701 struct usbnet *dev = netdev_priv(netdev);
706 retval = smsc95xx_read_reg(dev, ID_REV, ®s->version);
708 netdev_warn(netdev, "REGS: cannot read ID_REV\n");
712 for (i = ID_REV, j = 0; i <= COE_CR; i += (sizeof(u32)), j++) {
713 retval = smsc95xx_read_reg(dev, i, &data[j]);
715 netdev_warn(netdev, "REGS: cannot read reg[%x]\n", i);
721 static void smsc95xx_ethtool_get_wol(struct net_device *net,
722 struct ethtool_wolinfo *wolinfo)
724 struct usbnet *dev = netdev_priv(net);
725 struct smsc95xx_priv *pdata = dev->driver_priv;
727 wolinfo->supported = SUPPORTED_WAKE;
728 wolinfo->wolopts = pdata->wolopts;
731 static int smsc95xx_ethtool_set_wol(struct net_device *net,
732 struct ethtool_wolinfo *wolinfo)
734 struct usbnet *dev = netdev_priv(net);
735 struct smsc95xx_priv *pdata = dev->driver_priv;
738 if (wolinfo->wolopts & ~SUPPORTED_WAKE)
741 pdata->wolopts = wolinfo->wolopts & SUPPORTED_WAKE;
743 ret = device_set_wakeup_enable(&dev->udev->dev, pdata->wolopts);
745 netdev_warn(dev->net, "device_set_wakeup_enable error %d\n", ret);
750 static u32 smsc95xx_get_link(struct net_device *net)
752 phy_read_status(net->phydev);
753 return net->phydev->link;
756 static void smsc95xx_ethtool_get_strings(struct net_device *netdev, u32 sset,
761 net_selftest_get_strings(data);
766 static int smsc95xx_ethtool_get_sset_count(struct net_device *ndev, int sset)
770 return net_selftest_get_count();
776 static const struct ethtool_ops smsc95xx_ethtool_ops = {
777 .get_link = smsc95xx_get_link,
778 .nway_reset = phy_ethtool_nway_reset,
779 .get_drvinfo = usbnet_get_drvinfo,
780 .get_msglevel = usbnet_get_msglevel,
781 .set_msglevel = usbnet_set_msglevel,
782 .get_eeprom_len = smsc95xx_ethtool_get_eeprom_len,
783 .get_eeprom = smsc95xx_ethtool_get_eeprom,
784 .set_eeprom = smsc95xx_ethtool_set_eeprom,
785 .get_regs_len = smsc95xx_ethtool_getregslen,
786 .get_regs = smsc95xx_ethtool_getregs,
787 .get_wol = smsc95xx_ethtool_get_wol,
788 .set_wol = smsc95xx_ethtool_set_wol,
789 .get_link_ksettings = phy_ethtool_get_link_ksettings,
790 .set_link_ksettings = phy_ethtool_set_link_ksettings,
791 .get_ts_info = ethtool_op_get_ts_info,
792 .self_test = net_selftest,
793 .get_strings = smsc95xx_ethtool_get_strings,
794 .get_sset_count = smsc95xx_ethtool_get_sset_count,
797 static int smsc95xx_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
799 if (!netif_running(netdev))
802 return phy_mii_ioctl(netdev->phydev, rq, cmd);
805 static void smsc95xx_init_mac_address(struct usbnet *dev)
809 /* maybe the boot loader passed the MAC address in devicetree */
810 if (!platform_get_ethdev_address(&dev->udev->dev, dev->net)) {
811 if (is_valid_ether_addr(dev->net->dev_addr)) {
812 /* device tree values are valid so use them */
813 netif_dbg(dev, ifup, dev->net, "MAC address read from the device tree\n");
818 /* try reading mac address from EEPROM */
819 if (smsc95xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN, addr) == 0) {
820 eth_hw_addr_set(dev->net, addr);
821 if (is_valid_ether_addr(dev->net->dev_addr)) {
822 /* eeprom values are valid so use them */
823 netif_dbg(dev, ifup, dev->net, "MAC address read from EEPROM\n");
828 /* no useful static MAC address found. generate a random one */
829 eth_hw_addr_random(dev->net);
830 netif_dbg(dev, ifup, dev->net, "MAC address set to eth_random_addr\n");
833 static int smsc95xx_set_mac_address(struct usbnet *dev)
835 u32 addr_lo = dev->net->dev_addr[0] | dev->net->dev_addr[1] << 8 |
836 dev->net->dev_addr[2] << 16 | dev->net->dev_addr[3] << 24;
837 u32 addr_hi = dev->net->dev_addr[4] | dev->net->dev_addr[5] << 8;
840 ret = smsc95xx_write_reg(dev, ADDRL, addr_lo);
844 return smsc95xx_write_reg(dev, ADDRH, addr_hi);
847 /* starts the TX path */
848 static int smsc95xx_start_tx_path(struct usbnet *dev)
850 struct smsc95xx_priv *pdata = dev->driver_priv;
854 /* Enable Tx at MAC */
855 spin_lock_irqsave(&pdata->mac_cr_lock, flags);
856 pdata->mac_cr |= MAC_CR_TXEN_;
857 spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
859 ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
863 /* Enable Tx at SCSRs */
864 return smsc95xx_write_reg(dev, TX_CFG, TX_CFG_ON_);
867 /* Starts the Receive path */
868 static int smsc95xx_start_rx_path(struct usbnet *dev, int in_pm)
870 struct smsc95xx_priv *pdata = dev->driver_priv;
873 spin_lock_irqsave(&pdata->mac_cr_lock, flags);
874 pdata->mac_cr |= MAC_CR_RXEN_;
875 spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
877 return __smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr, in_pm);
880 static int smsc95xx_reset(struct usbnet *dev)
882 struct smsc95xx_priv *pdata = dev->driver_priv;
883 u32 read_buf, write_buf, burst_cap;
884 int ret = 0, timeout;
886 netif_dbg(dev, ifup, dev->net, "entering smsc95xx_reset\n");
888 ret = smsc95xx_write_reg(dev, HW_CFG, HW_CFG_LRST_);
895 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
899 } while ((read_buf & HW_CFG_LRST_) && (timeout < 100));
901 if (timeout >= 100) {
902 netdev_warn(dev->net, "timeout waiting for completion of Lite Reset\n");
906 ret = smsc95xx_set_mac_address(dev);
910 netif_dbg(dev, ifup, dev->net, "MAC Address: %pM\n",
913 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
917 netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG : 0x%08x\n",
920 read_buf |= HW_CFG_BIR_;
922 ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
926 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
930 netif_dbg(dev, ifup, dev->net,
931 "Read Value from HW_CFG after writing HW_CFG_BIR_: 0x%08x\n",
936 dev->rx_urb_size = MAX_SINGLE_PACKET_SIZE;
937 } else if (dev->udev->speed == USB_SPEED_HIGH) {
938 burst_cap = DEFAULT_HS_BURST_CAP_SIZE / HS_USB_PKT_SIZE;
939 dev->rx_urb_size = DEFAULT_HS_BURST_CAP_SIZE;
941 burst_cap = DEFAULT_FS_BURST_CAP_SIZE / FS_USB_PKT_SIZE;
942 dev->rx_urb_size = DEFAULT_FS_BURST_CAP_SIZE;
945 netif_dbg(dev, ifup, dev->net, "rx_urb_size=%ld\n",
946 (ulong)dev->rx_urb_size);
948 ret = smsc95xx_write_reg(dev, BURST_CAP, burst_cap);
952 ret = smsc95xx_read_reg(dev, BURST_CAP, &read_buf);
956 netif_dbg(dev, ifup, dev->net,
957 "Read Value from BURST_CAP after writing: 0x%08x\n",
960 ret = smsc95xx_write_reg(dev, BULK_IN_DLY, DEFAULT_BULK_IN_DELAY);
964 ret = smsc95xx_read_reg(dev, BULK_IN_DLY, &read_buf);
968 netif_dbg(dev, ifup, dev->net,
969 "Read Value from BULK_IN_DLY after writing: 0x%08x\n",
972 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
976 netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG: 0x%08x\n",
980 read_buf |= (HW_CFG_MEF_ | HW_CFG_BCE_);
982 read_buf &= ~HW_CFG_RXDOFF_;
984 /* set Rx data offset=2, Make IP header aligns on word boundary. */
985 read_buf |= NET_IP_ALIGN << 9;
987 ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
991 ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
995 netif_dbg(dev, ifup, dev->net,
996 "Read Value from HW_CFG after writing: 0x%08x\n", read_buf);
998 ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
1002 ret = smsc95xx_read_reg(dev, ID_REV, &read_buf);
1005 netif_dbg(dev, ifup, dev->net, "ID_REV = 0x%08x\n", read_buf);
1007 /* Configure GPIO pins as LED outputs */
1008 write_buf = LED_GPIO_CFG_SPD_LED | LED_GPIO_CFG_LNK_LED |
1009 LED_GPIO_CFG_FDX_LED;
1010 ret = smsc95xx_write_reg(dev, LED_GPIO_CFG, write_buf);
1015 ret = smsc95xx_write_reg(dev, FLOW, 0);
1019 ret = smsc95xx_write_reg(dev, AFC_CFG, AFC_CFG_DEFAULT);
1023 /* Don't need mac_cr_lock during initialisation */
1024 ret = smsc95xx_read_reg(dev, MAC_CR, &pdata->mac_cr);
1030 ret = smsc95xx_write_reg(dev, VLAN1, (u32)ETH_P_8021Q);
1034 /* Enable or disable checksum offload engines */
1035 ret = smsc95xx_set_features(dev->net, dev->net->features);
1037 netdev_warn(dev->net, "Failed to set checksum offload features\n");
1041 smsc95xx_set_multicast(dev->net);
1043 ret = smsc95xx_read_reg(dev, INT_EP_CTL, &read_buf);
1047 /* enable PHY interrupts */
1048 read_buf |= INT_EP_CTL_PHY_INT_;
1050 ret = smsc95xx_write_reg(dev, INT_EP_CTL, read_buf);
1054 ret = smsc95xx_start_tx_path(dev);
1056 netdev_warn(dev->net, "Failed to start TX path\n");
1060 ret = smsc95xx_start_rx_path(dev, 0);
1062 netdev_warn(dev->net, "Failed to start RX path\n");
1066 netif_dbg(dev, ifup, dev->net, "smsc95xx_reset, return 0\n");
1070 static const struct net_device_ops smsc95xx_netdev_ops = {
1071 .ndo_open = usbnet_open,
1072 .ndo_stop = usbnet_stop,
1073 .ndo_start_xmit = usbnet_start_xmit,
1074 .ndo_tx_timeout = usbnet_tx_timeout,
1075 .ndo_change_mtu = usbnet_change_mtu,
1076 .ndo_get_stats64 = dev_get_tstats64,
1077 .ndo_set_mac_address = eth_mac_addr,
1078 .ndo_validate_addr = eth_validate_addr,
1079 .ndo_eth_ioctl = smsc95xx_ioctl,
1080 .ndo_set_rx_mode = smsc95xx_set_multicast,
1081 .ndo_set_features = smsc95xx_set_features,
1084 static void smsc95xx_handle_link_change(struct net_device *net)
1086 struct usbnet *dev = netdev_priv(net);
1088 phy_print_status(net->phydev);
1089 smsc95xx_mac_update_fullduplex(dev);
1090 usbnet_defer_kevent(dev, EVENT_LINK_CHANGE);
1093 static int smsc95xx_bind(struct usbnet *dev, struct usb_interface *intf)
1095 struct smsc95xx_priv *pdata;
1096 bool is_internal_phy;
1101 printk(KERN_INFO SMSC_CHIPNAME " v" SMSC_DRIVER_VERSION "\n");
1103 ret = usbnet_get_endpoints(dev, intf);
1105 netdev_warn(dev->net, "usbnet_get_endpoints failed: %d\n", ret);
1109 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
1113 dev->driver_priv = pdata;
1115 spin_lock_init(&pdata->mac_cr_lock);
1117 /* LAN95xx devices do not alter the computed checksum of 0 to 0xffff.
1118 * RFC 2460, ipv6 UDP calculated checksum yields a result of zero must
1119 * be changed to 0xffff. RFC 768, ipv4 UDP computed checksum is zero,
1120 * it is transmitted as all ones. The zero transmitted checksum means
1121 * transmitter generated no checksum. Hence, enable csum offload only
1124 if (DEFAULT_TX_CSUM_ENABLE)
1125 dev->net->features |= NETIF_F_IP_CSUM;
1126 if (DEFAULT_RX_CSUM_ENABLE)
1127 dev->net->features |= NETIF_F_RXCSUM;
1129 dev->net->hw_features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
1130 set_bit(EVENT_NO_IP_ALIGN, &dev->flags);
1132 smsc95xx_init_mac_address(dev);
1134 /* Init all registers */
1135 ret = smsc95xx_reset(dev);
1139 /* create irq domain for use by PHY driver and GPIO consumers */
1140 usb_make_path(dev->udev, usb_path, sizeof(usb_path));
1141 pdata->irqfwnode = irq_domain_alloc_named_fwnode(usb_path);
1142 if (!pdata->irqfwnode) {
1147 pdata->irqdomain = irq_domain_create_linear(pdata->irqfwnode,
1149 &irq_domain_simple_ops,
1151 if (!pdata->irqdomain) {
1153 goto free_irqfwnode;
1156 phy_irq = irq_create_mapping(pdata->irqdomain, PHY_HWIRQ);
1159 goto remove_irqdomain;
1162 pdata->irqchip = dummy_irq_chip;
1163 pdata->irqchip.name = SMSC_CHIPNAME;
1164 irq_set_chip_and_handler_name(phy_irq, &pdata->irqchip,
1165 handle_simple_irq, "phy");
1167 pdata->mdiobus = mdiobus_alloc();
1168 if (!pdata->mdiobus) {
1173 ret = smsc95xx_read_reg(dev, HW_CFG, &val);
1177 is_internal_phy = !(val & HW_CFG_PSEL_);
1178 if (is_internal_phy)
1179 pdata->mdiobus->phy_mask = ~(1u << SMSC95XX_INTERNAL_PHY_ID);
1181 pdata->mdiobus->priv = dev;
1182 pdata->mdiobus->read = smsc95xx_mdiobus_read;
1183 pdata->mdiobus->write = smsc95xx_mdiobus_write;
1184 pdata->mdiobus->name = "smsc95xx-mdiobus";
1185 pdata->mdiobus->parent = &dev->udev->dev;
1187 snprintf(pdata->mdiobus->id, ARRAY_SIZE(pdata->mdiobus->id),
1188 "usb-%03d:%03d", dev->udev->bus->busnum, dev->udev->devnum);
1190 ret = mdiobus_register(pdata->mdiobus);
1192 netdev_err(dev->net, "Could not register MDIO bus\n");
1196 pdata->phydev = phy_find_first(pdata->mdiobus);
1197 if (!pdata->phydev) {
1198 netdev_err(dev->net, "no PHY found\n");
1200 goto unregister_mdio;
1203 pdata->phydev->irq = phy_irq;
1204 pdata->phydev->is_internal = is_internal_phy;
1206 /* detect device revision as different features may be available */
1207 ret = smsc95xx_read_reg(dev, ID_REV, &val);
1209 goto unregister_mdio;
1212 if ((val == ID_REV_CHIP_ID_9500A_) || (val == ID_REV_CHIP_ID_9530_) ||
1213 (val == ID_REV_CHIP_ID_89530_) || (val == ID_REV_CHIP_ID_9730_))
1214 pdata->features = (FEATURE_8_WAKEUP_FILTERS |
1215 FEATURE_PHY_NLP_CROSSOVER |
1216 FEATURE_REMOTE_WAKEUP);
1217 else if (val == ID_REV_CHIP_ID_9512_)
1218 pdata->features = FEATURE_8_WAKEUP_FILTERS;
1220 dev->net->netdev_ops = &smsc95xx_netdev_ops;
1221 dev->net->ethtool_ops = &smsc95xx_ethtool_ops;
1222 dev->net->flags |= IFF_MULTICAST;
1223 dev->net->hard_header_len += SMSC95XX_TX_OVERHEAD_CSUM;
1224 dev->net->min_mtu = ETH_MIN_MTU;
1225 dev->net->max_mtu = ETH_DATA_LEN;
1226 dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
1228 ret = phy_connect_direct(dev->net, pdata->phydev,
1229 &smsc95xx_handle_link_change,
1230 PHY_INTERFACE_MODE_MII);
1232 netdev_err(dev->net, "can't attach PHY to %s\n", pdata->mdiobus->id);
1233 goto unregister_mdio;
1236 phy_attached_info(dev->net->phydev);
1241 mdiobus_unregister(pdata->mdiobus);
1244 mdiobus_free(pdata->mdiobus);
1247 irq_dispose_mapping(phy_irq);
1250 irq_domain_remove(pdata->irqdomain);
1253 irq_domain_free_fwnode(pdata->irqfwnode);
1260 static void smsc95xx_unbind(struct usbnet *dev, struct usb_interface *intf)
1262 struct smsc95xx_priv *pdata = dev->driver_priv;
1264 phy_disconnect(dev->net->phydev);
1265 mdiobus_unregister(pdata->mdiobus);
1266 mdiobus_free(pdata->mdiobus);
1267 irq_dispose_mapping(irq_find_mapping(pdata->irqdomain, PHY_HWIRQ));
1268 irq_domain_remove(pdata->irqdomain);
1269 irq_domain_free_fwnode(pdata->irqfwnode);
1270 netif_dbg(dev, ifdown, dev->net, "free pdata\n");
1274 static int smsc95xx_start_phy(struct usbnet *dev)
1276 phy_start(dev->net->phydev);
1281 static int smsc95xx_stop(struct usbnet *dev)
1283 phy_stop(dev->net->phydev);
1288 static u32 smsc_crc(const u8 *buffer, size_t len, int filter)
1290 u32 crc = bitrev16(crc16(0xFFFF, buffer, len));
1291 return crc << ((filter % 2) * 16);
1294 static int smsc95xx_link_ok_nopm(struct usbnet *dev)
1298 /* first, a dummy read, needed to latch some MII phys */
1299 ret = smsc95xx_mdio_read_nopm(dev, MII_BMSR);
1303 ret = smsc95xx_mdio_read_nopm(dev, MII_BMSR);
1307 return !!(ret & BMSR_LSTATUS);
1310 static int smsc95xx_enter_suspend0(struct usbnet *dev)
1312 struct smsc95xx_priv *pdata = dev->driver_priv;
1316 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1320 val &= (~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_));
1321 val |= PM_CTL_SUS_MODE_0;
1323 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1327 /* clear wol status */
1328 val &= ~PM_CTL_WUPS_;
1329 val |= PM_CTL_WUPS_WOL_;
1331 /* enable energy detection */
1332 if (pdata->wolopts & WAKE_PHY)
1333 val |= PM_CTL_WUPS_ED_;
1335 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1339 /* read back PM_CTRL */
1340 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1344 pdata->suspend_flags |= SUSPEND_SUSPEND0;
1349 static int smsc95xx_enter_suspend1(struct usbnet *dev)
1351 struct smsc95xx_priv *pdata = dev->driver_priv;
1355 /* reconfigure link pulse detection timing for
1356 * compatibility with non-standard link partners
1358 if (pdata->features & FEATURE_PHY_NLP_CROSSOVER)
1359 smsc95xx_mdio_write_nopm(dev, PHY_EDPD_CONFIG,
1360 PHY_EDPD_CONFIG_DEFAULT);
1362 /* enable energy detect power-down mode */
1363 ret = smsc95xx_mdio_read_nopm(dev, PHY_MODE_CTRL_STS);
1367 ret |= MODE_CTRL_STS_EDPWRDOWN_;
1369 smsc95xx_mdio_write_nopm(dev, PHY_MODE_CTRL_STS, ret);
1371 /* enter SUSPEND1 mode */
1372 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1376 val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1377 val |= PM_CTL_SUS_MODE_1;
1379 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1383 /* clear wol status, enable energy detection */
1384 val &= ~PM_CTL_WUPS_;
1385 val |= (PM_CTL_WUPS_ED_ | PM_CTL_ED_EN_);
1387 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1391 pdata->suspend_flags |= SUSPEND_SUSPEND1;
1396 static int smsc95xx_enter_suspend2(struct usbnet *dev)
1398 struct smsc95xx_priv *pdata = dev->driver_priv;
1402 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1406 val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1407 val |= PM_CTL_SUS_MODE_2;
1409 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1413 pdata->suspend_flags |= SUSPEND_SUSPEND2;
1418 static int smsc95xx_enter_suspend3(struct usbnet *dev)
1420 struct smsc95xx_priv *pdata = dev->driver_priv;
1424 ret = smsc95xx_read_reg_nopm(dev, RX_FIFO_INF, &val);
1428 if (val & RX_FIFO_INF_USED_) {
1429 netdev_info(dev->net, "rx fifo not empty in autosuspend\n");
1433 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1437 val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1438 val |= PM_CTL_SUS_MODE_3 | PM_CTL_RES_CLR_WKP_STS;
1440 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1444 /* clear wol status */
1445 val &= ~PM_CTL_WUPS_;
1446 val |= PM_CTL_WUPS_WOL_;
1448 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1452 pdata->suspend_flags |= SUSPEND_SUSPEND3;
1457 static int smsc95xx_autosuspend(struct usbnet *dev, u32 link_up)
1459 struct smsc95xx_priv *pdata = dev->driver_priv;
1461 if (!netif_running(dev->net)) {
1462 /* interface is ifconfig down so fully power down hw */
1463 netdev_dbg(dev->net, "autosuspend entering SUSPEND2\n");
1464 return smsc95xx_enter_suspend2(dev);
1468 /* link is down so enter EDPD mode, but only if device can
1469 * reliably resume from it. This check should be redundant
1470 * as current FEATURE_REMOTE_WAKEUP parts also support
1471 * FEATURE_PHY_NLP_CROSSOVER but it's included for clarity */
1472 if (!(pdata->features & FEATURE_PHY_NLP_CROSSOVER)) {
1473 netdev_warn(dev->net, "EDPD not supported\n");
1477 netdev_dbg(dev->net, "autosuspend entering SUSPEND1\n");
1478 netdev_info(dev->net, "entering SUSPEND1 mode\n");
1479 return smsc95xx_enter_suspend1(dev);
1482 netdev_dbg(dev->net, "autosuspend entering SUSPEND3\n");
1483 return smsc95xx_enter_suspend3(dev);
1486 static int smsc95xx_suspend(struct usb_interface *intf, pm_message_t message)
1488 struct usbnet *dev = usb_get_intfdata(intf);
1489 struct smsc95xx_priv *pdata = dev->driver_priv;
1493 ret = usbnet_suspend(intf, message);
1495 netdev_warn(dev->net, "usbnet_suspend error\n");
1499 if (pdata->suspend_flags) {
1500 netdev_warn(dev->net, "error during last resume\n");
1501 pdata->suspend_flags = 0;
1504 /* determine if link is up using only _nopm functions */
1505 link_up = smsc95xx_link_ok_nopm(dev);
1507 if (message.event == PM_EVENT_AUTO_SUSPEND &&
1508 (pdata->features & FEATURE_REMOTE_WAKEUP)) {
1509 ret = smsc95xx_autosuspend(dev, link_up);
1513 /* if we get this far we're not autosuspending */
1514 /* if no wol options set, or if link is down and we're not waking on
1515 * PHY activity, enter lowest power SUSPEND2 mode
1517 if (!(pdata->wolopts & SUPPORTED_WAKE) ||
1518 !(link_up || (pdata->wolopts & WAKE_PHY))) {
1519 netdev_info(dev->net, "entering SUSPEND2 mode\n");
1521 /* disable energy detect (link up) & wake up events */
1522 ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1526 val &= ~(WUCSR_MPEN_ | WUCSR_WAKE_EN_);
1528 ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1532 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1536 val &= ~(PM_CTL_ED_EN_ | PM_CTL_WOL_EN_);
1538 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1542 ret = smsc95xx_enter_suspend2(dev);
1546 if (pdata->wolopts & WAKE_PHY) {
1547 /* if link is down then configure EDPD and enter SUSPEND1,
1548 * otherwise enter SUSPEND0 below
1551 netdev_info(dev->net, "entering SUSPEND1 mode\n");
1552 ret = smsc95xx_enter_suspend1(dev);
1557 if (pdata->wolopts & (WAKE_BCAST | WAKE_MCAST | WAKE_ARP | WAKE_UCAST)) {
1558 u32 *filter_mask = kcalloc(32, sizeof(u32), GFP_KERNEL);
1562 int wuff_filter_count =
1563 (pdata->features & FEATURE_8_WAKEUP_FILTERS) ?
1564 LAN9500A_WUFF_NUM : LAN9500_WUFF_NUM;
1568 netdev_warn(dev->net, "Unable to allocate filter_mask\n");
1573 memset(command, 0, sizeof(command));
1574 memset(offset, 0, sizeof(offset));
1575 memset(crc, 0, sizeof(crc));
1577 if (pdata->wolopts & WAKE_BCAST) {
1578 const u8 bcast[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1579 netdev_info(dev->net, "enabling broadcast detection\n");
1580 filter_mask[filter * 4] = 0x003F;
1581 filter_mask[filter * 4 + 1] = 0x00;
1582 filter_mask[filter * 4 + 2] = 0x00;
1583 filter_mask[filter * 4 + 3] = 0x00;
1584 command[filter/4] |= 0x05UL << ((filter % 4) * 8);
1585 offset[filter/4] |= 0x00 << ((filter % 4) * 8);
1586 crc[filter/2] |= smsc_crc(bcast, 6, filter);
1590 if (pdata->wolopts & WAKE_MCAST) {
1591 const u8 mcast[] = {0x01, 0x00, 0x5E};
1592 netdev_info(dev->net, "enabling multicast detection\n");
1593 filter_mask[filter * 4] = 0x0007;
1594 filter_mask[filter * 4 + 1] = 0x00;
1595 filter_mask[filter * 4 + 2] = 0x00;
1596 filter_mask[filter * 4 + 3] = 0x00;
1597 command[filter/4] |= 0x09UL << ((filter % 4) * 8);
1598 offset[filter/4] |= 0x00 << ((filter % 4) * 8);
1599 crc[filter/2] |= smsc_crc(mcast, 3, filter);
1603 if (pdata->wolopts & WAKE_ARP) {
1604 const u8 arp[] = {0x08, 0x06};
1605 netdev_info(dev->net, "enabling ARP detection\n");
1606 filter_mask[filter * 4] = 0x0003;
1607 filter_mask[filter * 4 + 1] = 0x00;
1608 filter_mask[filter * 4 + 2] = 0x00;
1609 filter_mask[filter * 4 + 3] = 0x00;
1610 command[filter/4] |= 0x05UL << ((filter % 4) * 8);
1611 offset[filter/4] |= 0x0C << ((filter % 4) * 8);
1612 crc[filter/2] |= smsc_crc(arp, 2, filter);
1616 if (pdata->wolopts & WAKE_UCAST) {
1617 netdev_info(dev->net, "enabling unicast detection\n");
1618 filter_mask[filter * 4] = 0x003F;
1619 filter_mask[filter * 4 + 1] = 0x00;
1620 filter_mask[filter * 4 + 2] = 0x00;
1621 filter_mask[filter * 4 + 3] = 0x00;
1622 command[filter/4] |= 0x01UL << ((filter % 4) * 8);
1623 offset[filter/4] |= 0x00 << ((filter % 4) * 8);
1624 crc[filter/2] |= smsc_crc(dev->net->dev_addr, ETH_ALEN, filter);
1628 for (i = 0; i < (wuff_filter_count * 4); i++) {
1629 ret = smsc95xx_write_reg_nopm(dev, WUFF, filter_mask[i]);
1637 for (i = 0; i < (wuff_filter_count / 4); i++) {
1638 ret = smsc95xx_write_reg_nopm(dev, WUFF, command[i]);
1643 for (i = 0; i < (wuff_filter_count / 4); i++) {
1644 ret = smsc95xx_write_reg_nopm(dev, WUFF, offset[i]);
1649 for (i = 0; i < (wuff_filter_count / 2); i++) {
1650 ret = smsc95xx_write_reg_nopm(dev, WUFF, crc[i]);
1655 /* clear any pending pattern match packet status */
1656 ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1662 ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1667 if (pdata->wolopts & WAKE_MAGIC) {
1668 /* clear any pending magic packet status */
1669 ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1675 ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1680 /* enable/disable wakeup sources */
1681 ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1685 if (pdata->wolopts & (WAKE_BCAST | WAKE_MCAST | WAKE_ARP | WAKE_UCAST)) {
1686 netdev_info(dev->net, "enabling pattern match wakeup\n");
1687 val |= WUCSR_WAKE_EN_;
1689 netdev_info(dev->net, "disabling pattern match wakeup\n");
1690 val &= ~WUCSR_WAKE_EN_;
1693 if (pdata->wolopts & WAKE_MAGIC) {
1694 netdev_info(dev->net, "enabling magic packet wakeup\n");
1697 netdev_info(dev->net, "disabling magic packet wakeup\n");
1698 val &= ~WUCSR_MPEN_;
1701 ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1705 /* enable wol wakeup source */
1706 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1710 val |= PM_CTL_WOL_EN_;
1712 /* phy energy detect wakeup source */
1713 if (pdata->wolopts & WAKE_PHY)
1714 val |= PM_CTL_ED_EN_;
1716 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1720 /* enable receiver to enable frame reception */
1721 smsc95xx_start_rx_path(dev, 1);
1723 /* some wol options are enabled, so enter SUSPEND0 */
1724 netdev_info(dev->net, "entering SUSPEND0 mode\n");
1725 ret = smsc95xx_enter_suspend0(dev);
1729 * TODO: resume() might need to handle the suspend failure
1732 if (ret && PMSG_IS_AUTO(message))
1733 usbnet_resume(intf);
1738 static int smsc95xx_resume(struct usb_interface *intf)
1740 struct usbnet *dev = usb_get_intfdata(intf);
1741 struct smsc95xx_priv *pdata;
1747 pdata = dev->driver_priv;
1748 suspend_flags = pdata->suspend_flags;
1750 netdev_dbg(dev->net, "resume suspend_flags=0x%02x\n", suspend_flags);
1752 /* do this first to ensure it's cleared even in error case */
1753 pdata->suspend_flags = 0;
1755 if (suspend_flags & SUSPEND_ALLMODES) {
1756 /* clear wake-up sources */
1757 ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1761 val &= ~(WUCSR_WAKE_EN_ | WUCSR_MPEN_);
1763 ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1767 /* clear wake-up status */
1768 ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1772 val &= ~PM_CTL_WOL_EN_;
1773 val |= PM_CTL_WUPS_;
1775 ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1780 phy_init_hw(pdata->phydev);
1782 ret = usbnet_resume(intf);
1784 netdev_warn(dev->net, "usbnet_resume error\n");
1789 static int smsc95xx_reset_resume(struct usb_interface *intf)
1791 struct usbnet *dev = usb_get_intfdata(intf);
1794 ret = smsc95xx_reset(dev);
1798 return smsc95xx_resume(intf);
1801 static void smsc95xx_rx_csum_offload(struct sk_buff *skb)
1803 skb->csum = *(u16 *)(skb_tail_pointer(skb) - 2);
1804 skb->ip_summed = CHECKSUM_COMPLETE;
1805 skb_trim(skb, skb->len - 2);
1808 static int smsc95xx_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
1810 /* This check is no longer done by usbnet */
1811 if (skb->len < dev->net->hard_header_len)
1814 while (skb->len > 0) {
1815 u32 header, align_count;
1816 struct sk_buff *ax_skb;
1817 unsigned char *packet;
1820 header = get_unaligned_le32(skb->data);
1821 skb_pull(skb, 4 + NET_IP_ALIGN);
1824 /* get the packet length */
1825 size = (u16)((header & RX_STS_FL_) >> 16);
1826 align_count = (4 - ((size + NET_IP_ALIGN) % 4)) % 4;
1828 if (unlikely(header & RX_STS_ES_)) {
1829 netif_dbg(dev, rx_err, dev->net,
1830 "Error header=0x%08x\n", header);
1831 dev->net->stats.rx_errors++;
1832 dev->net->stats.rx_dropped++;
1834 if (header & RX_STS_CRC_) {
1835 dev->net->stats.rx_crc_errors++;
1837 if (header & (RX_STS_TL_ | RX_STS_RF_))
1838 dev->net->stats.rx_frame_errors++;
1840 if ((header & RX_STS_LE_) &&
1841 (!(header & RX_STS_FT_)))
1842 dev->net->stats.rx_length_errors++;
1845 /* ETH_FRAME_LEN + 4(CRC) + 2(COE) + 4(Vlan) */
1846 if (unlikely(size > (ETH_FRAME_LEN + 12))) {
1847 netif_dbg(dev, rx_err, dev->net,
1848 "size err header=0x%08x\n", header);
1852 /* last frame in this batch */
1853 if (skb->len == size) {
1854 if (dev->net->features & NETIF_F_RXCSUM)
1855 smsc95xx_rx_csum_offload(skb);
1856 skb_trim(skb, skb->len - 4); /* remove fcs */
1857 skb->truesize = size + sizeof(struct sk_buff);
1862 ax_skb = skb_clone(skb, GFP_ATOMIC);
1863 if (unlikely(!ax_skb)) {
1864 netdev_warn(dev->net, "Error allocating skb\n");
1869 ax_skb->data = packet;
1870 skb_set_tail_pointer(ax_skb, size);
1872 if (dev->net->features & NETIF_F_RXCSUM)
1873 smsc95xx_rx_csum_offload(ax_skb);
1874 skb_trim(ax_skb, ax_skb->len - 4); /* remove fcs */
1875 ax_skb->truesize = size + sizeof(struct sk_buff);
1877 usbnet_skb_return(dev, ax_skb);
1880 skb_pull(skb, size);
1882 /* padding bytes before the next frame starts */
1884 skb_pull(skb, align_count);
1890 static u32 smsc95xx_calc_csum_preamble(struct sk_buff *skb)
1892 u16 low_16 = (u16)skb_checksum_start_offset(skb);
1893 u16 high_16 = low_16 + skb->csum_offset;
1894 return (high_16 << 16) | low_16;
1897 /* The TX CSUM won't work if the checksum lies in the last 4 bytes of the
1898 * transmission. This is fairly unlikely, only seems to trigger with some
1899 * short TCP ACK packets sent.
1901 * Note, this calculation should probably check for the alignment of the
1902 * data as well, but a straight check for csum being in the last four bytes
1903 * of the packet should be ok for now.
1905 static bool smsc95xx_can_tx_checksum(struct sk_buff *skb)
1907 unsigned int len = skb->len - skb_checksum_start_offset(skb);
1911 return skb->csum_offset < (len - (4 + 1));
1914 static struct sk_buff *smsc95xx_tx_fixup(struct usbnet *dev,
1915 struct sk_buff *skb, gfp_t flags)
1917 bool csum = skb->ip_summed == CHECKSUM_PARTIAL;
1918 int overhead = csum ? SMSC95XX_TX_OVERHEAD_CSUM : SMSC95XX_TX_OVERHEAD;
1919 u32 tx_cmd_a, tx_cmd_b;
1922 /* We do not advertise SG, so skbs should be already linearized */
1923 BUG_ON(skb_shinfo(skb)->nr_frags);
1925 /* Make writable and expand header space by overhead if required */
1926 if (skb_cow_head(skb, overhead)) {
1927 /* Must deallocate here as returning NULL to indicate error
1928 * means the skb won't be deallocated in the caller.
1930 dev_kfree_skb_any(skb);
1934 tx_cmd_b = (u32)skb->len;
1935 tx_cmd_a = tx_cmd_b | TX_CMD_A_FIRST_SEG_ | TX_CMD_A_LAST_SEG_;
1938 if (!smsc95xx_can_tx_checksum(skb)) {
1939 /* workaround - hardware tx checksum does not work
1940 * properly with extremely small packets */
1941 long csstart = skb_checksum_start_offset(skb);
1942 __wsum calc = csum_partial(skb->data + csstart,
1943 skb->len - csstart, 0);
1944 *((__sum16 *)(skb->data + csstart
1945 + skb->csum_offset)) = csum_fold(calc);
1949 u32 csum_preamble = smsc95xx_calc_csum_preamble(skb);
1950 ptr = skb_push(skb, 4);
1951 put_unaligned_le32(csum_preamble, ptr);
1955 tx_cmd_b |= TX_CMD_B_CSUM_ENABLE;
1959 ptr = skb_push(skb, 8);
1960 put_unaligned_le32(tx_cmd_a, ptr);
1961 put_unaligned_le32(tx_cmd_b, ptr+4);
1966 static int smsc95xx_manage_power(struct usbnet *dev, int on)
1968 struct smsc95xx_priv *pdata = dev->driver_priv;
1970 dev->intf->needs_remote_wakeup = on;
1972 if (pdata->features & FEATURE_REMOTE_WAKEUP)
1975 /* this chip revision isn't capable of remote wakeup */
1976 netdev_info(dev->net, "hardware isn't capable of remote wakeup\n");
1979 usb_autopm_get_interface_no_resume(dev->intf);
1981 usb_autopm_put_interface(dev->intf);
1986 static const struct driver_info smsc95xx_info = {
1987 .description = "smsc95xx USB 2.0 Ethernet",
1988 .bind = smsc95xx_bind,
1989 .unbind = smsc95xx_unbind,
1990 .reset = smsc95xx_reset,
1991 .check_connect = smsc95xx_start_phy,
1992 .stop = smsc95xx_stop,
1993 .rx_fixup = smsc95xx_rx_fixup,
1994 .tx_fixup = smsc95xx_tx_fixup,
1995 .status = smsc95xx_status,
1996 .manage_power = smsc95xx_manage_power,
1997 .flags = FLAG_ETHER | FLAG_SEND_ZLP | FLAG_LINK_INTR,
2000 static const struct usb_device_id products[] = {
2002 /* SMSC9500 USB Ethernet Device */
2003 USB_DEVICE(0x0424, 0x9500),
2004 .driver_info = (unsigned long) &smsc95xx_info,
2007 /* SMSC9505 USB Ethernet Device */
2008 USB_DEVICE(0x0424, 0x9505),
2009 .driver_info = (unsigned long) &smsc95xx_info,
2012 /* SMSC9500A USB Ethernet Device */
2013 USB_DEVICE(0x0424, 0x9E00),
2014 .driver_info = (unsigned long) &smsc95xx_info,
2017 /* SMSC9505A USB Ethernet Device */
2018 USB_DEVICE(0x0424, 0x9E01),
2019 .driver_info = (unsigned long) &smsc95xx_info,
2022 /* SMSC9512/9514 USB Hub & Ethernet Device */
2023 USB_DEVICE(0x0424, 0xec00),
2024 .driver_info = (unsigned long) &smsc95xx_info,
2027 /* SMSC9500 USB Ethernet Device (SAL10) */
2028 USB_DEVICE(0x0424, 0x9900),
2029 .driver_info = (unsigned long) &smsc95xx_info,
2032 /* SMSC9505 USB Ethernet Device (SAL10) */
2033 USB_DEVICE(0x0424, 0x9901),
2034 .driver_info = (unsigned long) &smsc95xx_info,
2037 /* SMSC9500A USB Ethernet Device (SAL10) */
2038 USB_DEVICE(0x0424, 0x9902),
2039 .driver_info = (unsigned long) &smsc95xx_info,
2042 /* SMSC9505A USB Ethernet Device (SAL10) */
2043 USB_DEVICE(0x0424, 0x9903),
2044 .driver_info = (unsigned long) &smsc95xx_info,
2047 /* SMSC9512/9514 USB Hub & Ethernet Device (SAL10) */
2048 USB_DEVICE(0x0424, 0x9904),
2049 .driver_info = (unsigned long) &smsc95xx_info,
2052 /* SMSC9500A USB Ethernet Device (HAL) */
2053 USB_DEVICE(0x0424, 0x9905),
2054 .driver_info = (unsigned long) &smsc95xx_info,
2057 /* SMSC9505A USB Ethernet Device (HAL) */
2058 USB_DEVICE(0x0424, 0x9906),
2059 .driver_info = (unsigned long) &smsc95xx_info,
2062 /* SMSC9500 USB Ethernet Device (Alternate ID) */
2063 USB_DEVICE(0x0424, 0x9907),
2064 .driver_info = (unsigned long) &smsc95xx_info,
2067 /* SMSC9500A USB Ethernet Device (Alternate ID) */
2068 USB_DEVICE(0x0424, 0x9908),
2069 .driver_info = (unsigned long) &smsc95xx_info,
2072 /* SMSC9512/9514 USB Hub & Ethernet Device (Alternate ID) */
2073 USB_DEVICE(0x0424, 0x9909),
2074 .driver_info = (unsigned long) &smsc95xx_info,
2077 /* SMSC LAN9530 USB Ethernet Device */
2078 USB_DEVICE(0x0424, 0x9530),
2079 .driver_info = (unsigned long) &smsc95xx_info,
2082 /* SMSC LAN9730 USB Ethernet Device */
2083 USB_DEVICE(0x0424, 0x9730),
2084 .driver_info = (unsigned long) &smsc95xx_info,
2087 /* SMSC LAN89530 USB Ethernet Device */
2088 USB_DEVICE(0x0424, 0x9E08),
2089 .driver_info = (unsigned long) &smsc95xx_info,
2093 MODULE_DEVICE_TABLE(usb, products);
2095 static struct usb_driver smsc95xx_driver = {
2097 .id_table = products,
2098 .probe = usbnet_probe,
2099 .suspend = smsc95xx_suspend,
2100 .resume = smsc95xx_resume,
2101 .reset_resume = smsc95xx_reset_resume,
2102 .disconnect = usbnet_disconnect,
2103 .disable_hub_initiated_lpm = 1,
2104 .supports_autosuspend = 1,
2107 module_usb_driver(smsc95xx_driver);
2109 MODULE_AUTHOR("Nancy Lin");
2110 MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>");
2111 MODULE_DESCRIPTION("SMSC95XX USB 2.0 Ethernet Devices");
2112 MODULE_LICENSE("GPL");