12f7ca802SSteve Glendinning /*************************************************************************** 22f7ca802SSteve Glendinning * 32f7ca802SSteve Glendinning * Copyright (C) 2007-2008 SMSC 42f7ca802SSteve Glendinning * 52f7ca802SSteve Glendinning * This program is free software; you can redistribute it and/or 62f7ca802SSteve Glendinning * modify it under the terms of the GNU General Public License 72f7ca802SSteve Glendinning * as published by the Free Software Foundation; either version 2 82f7ca802SSteve Glendinning * of the License, or (at your option) any later version. 92f7ca802SSteve Glendinning * 102f7ca802SSteve Glendinning * This program is distributed in the hope that it will be useful, 112f7ca802SSteve Glendinning * but WITHOUT ANY WARRANTY; without even the implied warranty of 122f7ca802SSteve Glendinning * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 132f7ca802SSteve Glendinning * GNU General Public License for more details. 142f7ca802SSteve Glendinning * 152f7ca802SSteve Glendinning * You should have received a copy of the GNU General Public License 169cb00073SJeff Kirsher * along with this program; if not, see <http://www.gnu.org/licenses/>. 172f7ca802SSteve Glendinning * 182f7ca802SSteve Glendinning *****************************************************************************/ 192f7ca802SSteve Glendinning 202f7ca802SSteve Glendinning #include <linux/module.h> 212f7ca802SSteve Glendinning #include <linux/kmod.h> 222f7ca802SSteve Glendinning #include <linux/netdevice.h> 232f7ca802SSteve Glendinning #include <linux/etherdevice.h> 242f7ca802SSteve Glendinning #include <linux/ethtool.h> 252f7ca802SSteve Glendinning #include <linux/mii.h> 262f7ca802SSteve Glendinning #include <linux/usb.h> 27bbd9f9eeSSteve Glendinning #include <linux/bitrev.h> 28bbd9f9eeSSteve Glendinning #include <linux/crc16.h> 292f7ca802SSteve Glendinning #include <linux/crc32.h> 302f7ca802SSteve Glendinning #include <linux/usb/usbnet.h> 315a0e3ad6STejun Heo #include <linux/slab.h> 32c489565bSArnd Bergmann #include <linux/of_net.h> 332f7ca802SSteve Glendinning #include "smsc95xx.h" 342f7ca802SSteve Glendinning 352f7ca802SSteve Glendinning #define SMSC_CHIPNAME "smsc95xx" 36f7b29271SSteve Glendinning #define SMSC_DRIVER_VERSION "1.0.4" 372f7ca802SSteve Glendinning #define HS_USB_PKT_SIZE (512) 382f7ca802SSteve Glendinning #define FS_USB_PKT_SIZE (64) 392f7ca802SSteve Glendinning #define DEFAULT_HS_BURST_CAP_SIZE (16 * 1024 + 5 * HS_USB_PKT_SIZE) 402f7ca802SSteve Glendinning #define DEFAULT_FS_BURST_CAP_SIZE (6 * 1024 + 33 * FS_USB_PKT_SIZE) 412f7ca802SSteve Glendinning #define DEFAULT_BULK_IN_DELAY (0x00002000) 422f7ca802SSteve Glendinning #define MAX_SINGLE_PACKET_SIZE (2048) 432f7ca802SSteve Glendinning #define LAN95XX_EEPROM_MAGIC (0x9500) 442f7ca802SSteve Glendinning #define EEPROM_MAC_OFFSET (0x01) 45f7b29271SSteve Glendinning #define DEFAULT_TX_CSUM_ENABLE (true) 462f7ca802SSteve Glendinning #define DEFAULT_RX_CSUM_ENABLE (true) 472f7ca802SSteve Glendinning #define SMSC95XX_INTERNAL_PHY_ID (1) 482f7ca802SSteve Glendinning #define SMSC95XX_TX_OVERHEAD (8) 49f7b29271SSteve Glendinning #define SMSC95XX_TX_OVERHEAD_CSUM (12) 50e5e3af83SSteve Glendinning #define SUPPORTED_WAKE (WAKE_PHY | WAKE_UCAST | WAKE_BCAST | \ 51bbd9f9eeSSteve Glendinning WAKE_MCAST | WAKE_ARP | WAKE_MAGIC) 522f7ca802SSteve Glendinning 539ebca507SSteve Glendinning #define FEATURE_8_WAKEUP_FILTERS (0x01) 549ebca507SSteve Glendinning #define FEATURE_PHY_NLP_CROSSOVER (0x02) 55eb970ff0SMing Lei #define FEATURE_REMOTE_WAKEUP (0x04) 569ebca507SSteve Glendinning 57b2d4b150SSteve Glendinning #define SUSPEND_SUSPEND0 (0x01) 58b2d4b150SSteve Glendinning #define SUSPEND_SUSPEND1 (0x02) 59b2d4b150SSteve Glendinning #define SUSPEND_SUSPEND2 (0x04) 60b2d4b150SSteve Glendinning #define SUSPEND_SUSPEND3 (0x08) 61b2d4b150SSteve Glendinning #define SUSPEND_ALLMODES (SUSPEND_SUSPEND0 | SUSPEND_SUSPEND1 | \ 62b2d4b150SSteve Glendinning SUSPEND_SUSPEND2 | SUSPEND_SUSPEND3) 63b2d4b150SSteve Glendinning 642f7ca802SSteve Glendinning struct smsc95xx_priv { 652f7ca802SSteve Glendinning u32 mac_cr; 663c0f3c60SMarc Zyngier u32 hash_hi; 673c0f3c60SMarc Zyngier u32 hash_lo; 68e0e474a8SSteve Glendinning u32 wolopts; 692f7ca802SSteve Glendinning spinlock_t mac_cr_lock; 709ebca507SSteve Glendinning u8 features; 71b2d4b150SSteve Glendinning u8 suspend_flags; 722f7ca802SSteve Glendinning }; 732f7ca802SSteve Glendinning 74eb939922SRusty Russell static bool turbo_mode = true; 752f7ca802SSteve Glendinning module_param(turbo_mode, bool, 0644); 762f7ca802SSteve Glendinning MODULE_PARM_DESC(turbo_mode, "Enable multiple frames per Rx transaction"); 772f7ca802SSteve Glendinning 78ec32115dSMing Lei static int __must_check __smsc95xx_read_reg(struct usbnet *dev, u32 index, 79ec32115dSMing Lei u32 *data, int in_pm) 802f7ca802SSteve Glendinning { 8172108fd2SMing Lei u32 buf; 822f7ca802SSteve Glendinning int ret; 83ec32115dSMing Lei int (*fn)(struct usbnet *, u8, u8, u16, u16, void *, u16); 842f7ca802SSteve Glendinning 852f7ca802SSteve Glendinning BUG_ON(!dev); 862f7ca802SSteve Glendinning 87ec32115dSMing Lei if (!in_pm) 88ec32115dSMing Lei fn = usbnet_read_cmd; 89ec32115dSMing Lei else 90ec32115dSMing Lei fn = usbnet_read_cmd_nopm; 91ec32115dSMing Lei 92ec32115dSMing Lei ret = fn(dev, USB_VENDOR_REQUEST_READ_REGISTER, USB_DIR_IN 93ec32115dSMing Lei | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 9472108fd2SMing Lei 0, index, &buf, 4); 95*5a36b68bSDan Carpenter if (unlikely(ret < 0)) { 961e1d7412SJoe Perches netdev_warn(dev->net, "Failed to read reg index 0x%08x: %d\n", 971e1d7412SJoe Perches index, ret); 98*5a36b68bSDan Carpenter return ret; 99*5a36b68bSDan Carpenter } 1002f7ca802SSteve Glendinning 10172108fd2SMing Lei le32_to_cpus(&buf); 10272108fd2SMing Lei *data = buf; 1032f7ca802SSteve Glendinning 1042f7ca802SSteve Glendinning return ret; 1052f7ca802SSteve Glendinning } 1062f7ca802SSteve Glendinning 107ec32115dSMing Lei static int __must_check __smsc95xx_write_reg(struct usbnet *dev, u32 index, 108ec32115dSMing Lei u32 data, int in_pm) 1092f7ca802SSteve Glendinning { 11072108fd2SMing Lei u32 buf; 1112f7ca802SSteve Glendinning int ret; 112ec32115dSMing Lei int (*fn)(struct usbnet *, u8, u8, u16, u16, const void *, u16); 1132f7ca802SSteve Glendinning 1142f7ca802SSteve Glendinning BUG_ON(!dev); 1152f7ca802SSteve Glendinning 116ec32115dSMing Lei if (!in_pm) 117ec32115dSMing Lei fn = usbnet_write_cmd; 118ec32115dSMing Lei else 119ec32115dSMing Lei fn = usbnet_write_cmd_nopm; 120ec32115dSMing Lei 12172108fd2SMing Lei buf = data; 12272108fd2SMing Lei cpu_to_le32s(&buf); 1232f7ca802SSteve Glendinning 124ec32115dSMing Lei ret = fn(dev, USB_VENDOR_REQUEST_WRITE_REGISTER, USB_DIR_OUT 125ec32115dSMing Lei | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 12672108fd2SMing Lei 0, index, &buf, 4); 1272f7ca802SSteve Glendinning if (unlikely(ret < 0)) 1281e1d7412SJoe Perches netdev_warn(dev->net, "Failed to write reg index 0x%08x: %d\n", 1291e1d7412SJoe Perches index, ret); 1302f7ca802SSteve Glendinning 1312f7ca802SSteve Glendinning return ret; 1322f7ca802SSteve Glendinning } 1332f7ca802SSteve Glendinning 134ec32115dSMing Lei static int __must_check smsc95xx_read_reg_nopm(struct usbnet *dev, u32 index, 135ec32115dSMing Lei u32 *data) 136ec32115dSMing Lei { 137ec32115dSMing Lei return __smsc95xx_read_reg(dev, index, data, 1); 138ec32115dSMing Lei } 139ec32115dSMing Lei 140ec32115dSMing Lei static int __must_check smsc95xx_write_reg_nopm(struct usbnet *dev, u32 index, 141ec32115dSMing Lei u32 data) 142ec32115dSMing Lei { 143ec32115dSMing Lei return __smsc95xx_write_reg(dev, index, data, 1); 144ec32115dSMing Lei } 145ec32115dSMing Lei 146ec32115dSMing Lei static int __must_check smsc95xx_read_reg(struct usbnet *dev, u32 index, 147ec32115dSMing Lei u32 *data) 148ec32115dSMing Lei { 149ec32115dSMing Lei return __smsc95xx_read_reg(dev, index, data, 0); 150ec32115dSMing Lei } 151ec32115dSMing Lei 152ec32115dSMing Lei static int __must_check smsc95xx_write_reg(struct usbnet *dev, u32 index, 153ec32115dSMing Lei u32 data) 154ec32115dSMing Lei { 155ec32115dSMing Lei return __smsc95xx_write_reg(dev, index, data, 0); 156ec32115dSMing Lei } 157e0e474a8SSteve Glendinning 1582f7ca802SSteve Glendinning /* Loop until the read is completed with timeout 1592f7ca802SSteve Glendinning * called with phy_mutex held */ 160e5e3af83SSteve Glendinning static int __must_check __smsc95xx_phy_wait_not_busy(struct usbnet *dev, 161e5e3af83SSteve Glendinning int in_pm) 1622f7ca802SSteve Glendinning { 1632f7ca802SSteve Glendinning unsigned long start_time = jiffies; 1642f7ca802SSteve Glendinning u32 val; 165769ea6d8SSteve Glendinning int ret; 1662f7ca802SSteve Glendinning 1672f7ca802SSteve Glendinning do { 168e5e3af83SSteve Glendinning ret = __smsc95xx_read_reg(dev, MII_ADDR, &val, in_pm); 169b052e073SSteve Glendinning if (ret < 0) { 170b052e073SSteve Glendinning netdev_warn(dev->net, "Error reading MII_ACCESS\n"); 171b052e073SSteve Glendinning return ret; 172b052e073SSteve Glendinning } 173b052e073SSteve Glendinning 1742f7ca802SSteve Glendinning if (!(val & MII_BUSY_)) 1752f7ca802SSteve Glendinning return 0; 1762f7ca802SSteve Glendinning } while (!time_after(jiffies, start_time + HZ)); 1772f7ca802SSteve Glendinning 1782f7ca802SSteve Glendinning return -EIO; 1792f7ca802SSteve Glendinning } 1802f7ca802SSteve Glendinning 181e5e3af83SSteve Glendinning static int __smsc95xx_mdio_read(struct net_device *netdev, int phy_id, int idx, 182e5e3af83SSteve Glendinning int in_pm) 1832f7ca802SSteve Glendinning { 1842f7ca802SSteve Glendinning struct usbnet *dev = netdev_priv(netdev); 1852f7ca802SSteve Glendinning u32 val, addr; 186769ea6d8SSteve Glendinning int ret; 1872f7ca802SSteve Glendinning 1882f7ca802SSteve Glendinning mutex_lock(&dev->phy_mutex); 1892f7ca802SSteve Glendinning 1902f7ca802SSteve Glendinning /* confirm MII not busy */ 191e5e3af83SSteve Glendinning ret = __smsc95xx_phy_wait_not_busy(dev, in_pm); 192b052e073SSteve Glendinning if (ret < 0) { 193b052e073SSteve Glendinning netdev_warn(dev->net, "MII is busy in smsc95xx_mdio_read\n"); 194b052e073SSteve Glendinning goto done; 195b052e073SSteve Glendinning } 1962f7ca802SSteve Glendinning 1972f7ca802SSteve Glendinning /* set the address, index & direction (read from PHY) */ 1982f7ca802SSteve Glendinning phy_id &= dev->mii.phy_id_mask; 1992f7ca802SSteve Glendinning idx &= dev->mii.reg_num_mask; 20080928805SSteve Glendinning addr = (phy_id << 11) | (idx << 6) | MII_READ_ | MII_BUSY_; 201e5e3af83SSteve Glendinning ret = __smsc95xx_write_reg(dev, MII_ADDR, addr, in_pm); 202b052e073SSteve Glendinning if (ret < 0) { 203b052e073SSteve Glendinning netdev_warn(dev->net, "Error writing MII_ADDR\n"); 204b052e073SSteve Glendinning goto done; 205b052e073SSteve Glendinning } 2062f7ca802SSteve Glendinning 207e5e3af83SSteve Glendinning ret = __smsc95xx_phy_wait_not_busy(dev, in_pm); 208b052e073SSteve Glendinning if (ret < 0) { 209b052e073SSteve Glendinning netdev_warn(dev->net, "Timed out reading MII reg %02X\n", idx); 210b052e073SSteve Glendinning goto done; 211b052e073SSteve Glendinning } 212769ea6d8SSteve Glendinning 213e5e3af83SSteve Glendinning ret = __smsc95xx_read_reg(dev, MII_DATA, &val, in_pm); 214b052e073SSteve Glendinning if (ret < 0) { 215b052e073SSteve Glendinning netdev_warn(dev->net, "Error reading MII_DATA\n"); 216b052e073SSteve Glendinning goto done; 217b052e073SSteve Glendinning } 218769ea6d8SSteve Glendinning 219769ea6d8SSteve Glendinning ret = (u16)(val & 0xFFFF); 220769ea6d8SSteve Glendinning 221769ea6d8SSteve Glendinning done: 2222f7ca802SSteve Glendinning mutex_unlock(&dev->phy_mutex); 223769ea6d8SSteve Glendinning return ret; 2242f7ca802SSteve Glendinning } 2252f7ca802SSteve Glendinning 226e5e3af83SSteve Glendinning static void __smsc95xx_mdio_write(struct net_device *netdev, int phy_id, 227e5e3af83SSteve Glendinning int idx, int regval, int in_pm) 2282f7ca802SSteve Glendinning { 2292f7ca802SSteve Glendinning struct usbnet *dev = netdev_priv(netdev); 2302f7ca802SSteve Glendinning u32 val, addr; 231769ea6d8SSteve Glendinning int ret; 2322f7ca802SSteve Glendinning 2332f7ca802SSteve Glendinning mutex_lock(&dev->phy_mutex); 2342f7ca802SSteve Glendinning 2352f7ca802SSteve Glendinning /* confirm MII not busy */ 236e5e3af83SSteve Glendinning ret = __smsc95xx_phy_wait_not_busy(dev, in_pm); 237b052e073SSteve Glendinning if (ret < 0) { 238b052e073SSteve Glendinning netdev_warn(dev->net, "MII is busy in smsc95xx_mdio_write\n"); 239b052e073SSteve Glendinning goto done; 240b052e073SSteve Glendinning } 2412f7ca802SSteve Glendinning 2422f7ca802SSteve Glendinning val = regval; 243e5e3af83SSteve Glendinning ret = __smsc95xx_write_reg(dev, MII_DATA, val, in_pm); 244b052e073SSteve Glendinning if (ret < 0) { 245b052e073SSteve Glendinning netdev_warn(dev->net, "Error writing MII_DATA\n"); 246b052e073SSteve Glendinning goto done; 247b052e073SSteve Glendinning } 2482f7ca802SSteve Glendinning 2492f7ca802SSteve Glendinning /* set the address, index & direction (write to PHY) */ 2502f7ca802SSteve Glendinning phy_id &= dev->mii.phy_id_mask; 2512f7ca802SSteve Glendinning idx &= dev->mii.reg_num_mask; 25280928805SSteve Glendinning addr = (phy_id << 11) | (idx << 6) | MII_WRITE_ | MII_BUSY_; 253e5e3af83SSteve Glendinning ret = __smsc95xx_write_reg(dev, MII_ADDR, addr, in_pm); 254b052e073SSteve Glendinning if (ret < 0) { 255b052e073SSteve Glendinning netdev_warn(dev->net, "Error writing MII_ADDR\n"); 256b052e073SSteve Glendinning goto done; 257b052e073SSteve Glendinning } 2582f7ca802SSteve Glendinning 259e5e3af83SSteve Glendinning ret = __smsc95xx_phy_wait_not_busy(dev, in_pm); 260b052e073SSteve Glendinning if (ret < 0) { 261b052e073SSteve Glendinning netdev_warn(dev->net, "Timed out writing MII reg %02X\n", idx); 262b052e073SSteve Glendinning goto done; 263b052e073SSteve Glendinning } 2642f7ca802SSteve Glendinning 265769ea6d8SSteve Glendinning done: 2662f7ca802SSteve Glendinning mutex_unlock(&dev->phy_mutex); 2672f7ca802SSteve Glendinning } 2682f7ca802SSteve Glendinning 269e5e3af83SSteve Glendinning static int smsc95xx_mdio_read_nopm(struct net_device *netdev, int phy_id, 270e5e3af83SSteve Glendinning int idx) 271e5e3af83SSteve Glendinning { 272e5e3af83SSteve Glendinning return __smsc95xx_mdio_read(netdev, phy_id, idx, 1); 273e5e3af83SSteve Glendinning } 274e5e3af83SSteve Glendinning 275e5e3af83SSteve Glendinning static void smsc95xx_mdio_write_nopm(struct net_device *netdev, int phy_id, 276e5e3af83SSteve Glendinning int idx, int regval) 277e5e3af83SSteve Glendinning { 278e5e3af83SSteve Glendinning __smsc95xx_mdio_write(netdev, phy_id, idx, regval, 1); 279e5e3af83SSteve Glendinning } 280e5e3af83SSteve Glendinning 281e5e3af83SSteve Glendinning static int smsc95xx_mdio_read(struct net_device *netdev, int phy_id, int idx) 282e5e3af83SSteve Glendinning { 283e5e3af83SSteve Glendinning return __smsc95xx_mdio_read(netdev, phy_id, idx, 0); 284e5e3af83SSteve Glendinning } 285e5e3af83SSteve Glendinning 286e5e3af83SSteve Glendinning static void smsc95xx_mdio_write(struct net_device *netdev, int phy_id, int idx, 287e5e3af83SSteve Glendinning int regval) 288e5e3af83SSteve Glendinning { 289e5e3af83SSteve Glendinning __smsc95xx_mdio_write(netdev, phy_id, idx, regval, 0); 290e5e3af83SSteve Glendinning } 291e5e3af83SSteve Glendinning 292769ea6d8SSteve Glendinning static int __must_check smsc95xx_wait_eeprom(struct usbnet *dev) 2932f7ca802SSteve Glendinning { 2942f7ca802SSteve Glendinning unsigned long start_time = jiffies; 2952f7ca802SSteve Glendinning u32 val; 296769ea6d8SSteve Glendinning int ret; 2972f7ca802SSteve Glendinning 2982f7ca802SSteve Glendinning do { 299769ea6d8SSteve Glendinning ret = smsc95xx_read_reg(dev, E2P_CMD, &val); 300b052e073SSteve Glendinning if (ret < 0) { 301b052e073SSteve Glendinning netdev_warn(dev->net, "Error reading E2P_CMD\n"); 302b052e073SSteve Glendinning return ret; 303b052e073SSteve Glendinning } 304b052e073SSteve Glendinning 3052f7ca802SSteve Glendinning if (!(val & E2P_CMD_BUSY_) || (val & E2P_CMD_TIMEOUT_)) 3062f7ca802SSteve Glendinning break; 3072f7ca802SSteve Glendinning udelay(40); 3082f7ca802SSteve Glendinning } while (!time_after(jiffies, start_time + HZ)); 3092f7ca802SSteve Glendinning 3102f7ca802SSteve Glendinning if (val & (E2P_CMD_TIMEOUT_ | E2P_CMD_BUSY_)) { 31160b86755SJoe Perches netdev_warn(dev->net, "EEPROM read operation timeout\n"); 3122f7ca802SSteve Glendinning return -EIO; 3132f7ca802SSteve Glendinning } 3142f7ca802SSteve Glendinning 3152f7ca802SSteve Glendinning return 0; 3162f7ca802SSteve Glendinning } 3172f7ca802SSteve Glendinning 318769ea6d8SSteve Glendinning static int __must_check smsc95xx_eeprom_confirm_not_busy(struct usbnet *dev) 3192f7ca802SSteve Glendinning { 3202f7ca802SSteve Glendinning unsigned long start_time = jiffies; 3212f7ca802SSteve Glendinning u32 val; 322769ea6d8SSteve Glendinning int ret; 3232f7ca802SSteve Glendinning 3242f7ca802SSteve Glendinning do { 325769ea6d8SSteve Glendinning ret = smsc95xx_read_reg(dev, E2P_CMD, &val); 326b052e073SSteve Glendinning if (ret < 0) { 327b052e073SSteve Glendinning netdev_warn(dev->net, "Error reading E2P_CMD\n"); 328b052e073SSteve Glendinning return ret; 329b052e073SSteve Glendinning } 3302f7ca802SSteve Glendinning 3312f7ca802SSteve Glendinning if (!(val & E2P_CMD_BUSY_)) 3322f7ca802SSteve Glendinning return 0; 3332f7ca802SSteve Glendinning 3342f7ca802SSteve Glendinning udelay(40); 3352f7ca802SSteve Glendinning } while (!time_after(jiffies, start_time + HZ)); 3362f7ca802SSteve Glendinning 33760b86755SJoe Perches netdev_warn(dev->net, "EEPROM is busy\n"); 3382f7ca802SSteve Glendinning return -EIO; 3392f7ca802SSteve Glendinning } 3402f7ca802SSteve Glendinning 3412f7ca802SSteve Glendinning static int smsc95xx_read_eeprom(struct usbnet *dev, u32 offset, u32 length, 3422f7ca802SSteve Glendinning u8 *data) 3432f7ca802SSteve Glendinning { 3442f7ca802SSteve Glendinning u32 val; 3452f7ca802SSteve Glendinning int i, ret; 3462f7ca802SSteve Glendinning 3472f7ca802SSteve Glendinning BUG_ON(!dev); 3482f7ca802SSteve Glendinning BUG_ON(!data); 3492f7ca802SSteve Glendinning 3502f7ca802SSteve Glendinning ret = smsc95xx_eeprom_confirm_not_busy(dev); 3512f7ca802SSteve Glendinning if (ret) 3522f7ca802SSteve Glendinning return ret; 3532f7ca802SSteve Glendinning 3542f7ca802SSteve Glendinning for (i = 0; i < length; i++) { 3552f7ca802SSteve Glendinning val = E2P_CMD_BUSY_ | E2P_CMD_READ_ | (offset & E2P_CMD_ADDR_); 356769ea6d8SSteve Glendinning ret = smsc95xx_write_reg(dev, E2P_CMD, val); 357b052e073SSteve Glendinning if (ret < 0) { 358b052e073SSteve Glendinning netdev_warn(dev->net, "Error writing E2P_CMD\n"); 359b052e073SSteve Glendinning return ret; 360b052e073SSteve Glendinning } 3612f7ca802SSteve Glendinning 3622f7ca802SSteve Glendinning ret = smsc95xx_wait_eeprom(dev); 3632f7ca802SSteve Glendinning if (ret < 0) 3642f7ca802SSteve Glendinning return ret; 3652f7ca802SSteve Glendinning 366769ea6d8SSteve Glendinning ret = smsc95xx_read_reg(dev, E2P_DATA, &val); 367b052e073SSteve Glendinning if (ret < 0) { 368b052e073SSteve Glendinning netdev_warn(dev->net, "Error reading E2P_DATA\n"); 369b052e073SSteve Glendinning return ret; 370b052e073SSteve Glendinning } 3712f7ca802SSteve Glendinning 3722f7ca802SSteve Glendinning data[i] = val & 0xFF; 3732f7ca802SSteve Glendinning offset++; 3742f7ca802SSteve Glendinning } 3752f7ca802SSteve Glendinning 3762f7ca802SSteve Glendinning return 0; 3772f7ca802SSteve Glendinning } 3782f7ca802SSteve Glendinning 3792f7ca802SSteve Glendinning static int smsc95xx_write_eeprom(struct usbnet *dev, u32 offset, u32 length, 3802f7ca802SSteve Glendinning u8 *data) 3812f7ca802SSteve Glendinning { 3822f7ca802SSteve Glendinning u32 val; 3832f7ca802SSteve Glendinning int i, ret; 3842f7ca802SSteve Glendinning 3852f7ca802SSteve Glendinning BUG_ON(!dev); 3862f7ca802SSteve Glendinning BUG_ON(!data); 3872f7ca802SSteve Glendinning 3882f7ca802SSteve Glendinning ret = smsc95xx_eeprom_confirm_not_busy(dev); 3892f7ca802SSteve Glendinning if (ret) 3902f7ca802SSteve Glendinning return ret; 3912f7ca802SSteve Glendinning 3922f7ca802SSteve Glendinning /* Issue write/erase enable command */ 3932f7ca802SSteve Glendinning val = E2P_CMD_BUSY_ | E2P_CMD_EWEN_; 394769ea6d8SSteve Glendinning ret = smsc95xx_write_reg(dev, E2P_CMD, val); 395b052e073SSteve Glendinning if (ret < 0) { 396b052e073SSteve Glendinning netdev_warn(dev->net, "Error writing E2P_DATA\n"); 397b052e073SSteve Glendinning return ret; 398b052e073SSteve Glendinning } 3992f7ca802SSteve Glendinning 4002f7ca802SSteve Glendinning ret = smsc95xx_wait_eeprom(dev); 4012f7ca802SSteve Glendinning if (ret < 0) 4022f7ca802SSteve Glendinning return ret; 4032f7ca802SSteve Glendinning 4042f7ca802SSteve Glendinning for (i = 0; i < length; i++) { 4052f7ca802SSteve Glendinning 4062f7ca802SSteve Glendinning /* Fill data register */ 4072f7ca802SSteve Glendinning val = data[i]; 408769ea6d8SSteve Glendinning ret = smsc95xx_write_reg(dev, E2P_DATA, val); 409b052e073SSteve Glendinning if (ret < 0) { 410b052e073SSteve Glendinning netdev_warn(dev->net, "Error writing E2P_DATA\n"); 411b052e073SSteve Glendinning return ret; 412b052e073SSteve Glendinning } 4132f7ca802SSteve Glendinning 4142f7ca802SSteve Glendinning /* Send "write" command */ 4152f7ca802SSteve Glendinning val = E2P_CMD_BUSY_ | E2P_CMD_WRITE_ | (offset & E2P_CMD_ADDR_); 416769ea6d8SSteve Glendinning ret = smsc95xx_write_reg(dev, E2P_CMD, val); 417b052e073SSteve Glendinning if (ret < 0) { 418b052e073SSteve Glendinning netdev_warn(dev->net, "Error writing E2P_CMD\n"); 419b052e073SSteve Glendinning return ret; 420b052e073SSteve Glendinning } 4212f7ca802SSteve Glendinning 4222f7ca802SSteve Glendinning ret = smsc95xx_wait_eeprom(dev); 4232f7ca802SSteve Glendinning if (ret < 0) 4242f7ca802SSteve Glendinning return ret; 4252f7ca802SSteve Glendinning 4262f7ca802SSteve Glendinning offset++; 4272f7ca802SSteve Glendinning } 4282f7ca802SSteve Glendinning 4292f7ca802SSteve Glendinning return 0; 4302f7ca802SSteve Glendinning } 4312f7ca802SSteve Glendinning 432769ea6d8SSteve Glendinning static int __must_check smsc95xx_write_reg_async(struct usbnet *dev, u16 index, 4337b9e7580SSteve Glendinning u32 data) 4342f7ca802SSteve Glendinning { 4351d74a6bdSSteve Glendinning const u16 size = 4; 4367b9e7580SSteve Glendinning u32 buf; 43772108fd2SMing Lei int ret; 4382f7ca802SSteve Glendinning 4397b9e7580SSteve Glendinning buf = data; 4407b9e7580SSteve Glendinning cpu_to_le32s(&buf); 4417b9e7580SSteve Glendinning 44272108fd2SMing Lei ret = usbnet_write_cmd_async(dev, USB_VENDOR_REQUEST_WRITE_REGISTER, 44372108fd2SMing Lei USB_DIR_OUT | USB_TYPE_VENDOR | 44472108fd2SMing Lei USB_RECIP_DEVICE, 4457b9e7580SSteve Glendinning 0, index, &buf, size); 44672108fd2SMing Lei if (ret < 0) 44772108fd2SMing Lei netdev_warn(dev->net, "Error write async cmd, sts=%d\n", 44872108fd2SMing Lei ret); 44972108fd2SMing Lei return ret; 4502f7ca802SSteve Glendinning } 4512f7ca802SSteve Glendinning 4522f7ca802SSteve Glendinning /* returns hash bit number for given MAC address 4532f7ca802SSteve Glendinning * example: 4542f7ca802SSteve Glendinning * 01 00 5E 00 00 01 -> returns bit number 31 */ 4552f7ca802SSteve Glendinning static unsigned int smsc95xx_hash(char addr[ETH_ALEN]) 4562f7ca802SSteve Glendinning { 4572f7ca802SSteve Glendinning return (ether_crc(ETH_ALEN, addr) >> 26) & 0x3f; 4582f7ca802SSteve Glendinning } 4592f7ca802SSteve Glendinning 4602f7ca802SSteve Glendinning static void smsc95xx_set_multicast(struct net_device *netdev) 4612f7ca802SSteve Glendinning { 4622f7ca802SSteve Glendinning struct usbnet *dev = netdev_priv(netdev); 4632f7ca802SSteve Glendinning struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]); 4642f7ca802SSteve Glendinning unsigned long flags; 465769ea6d8SSteve Glendinning int ret; 4662f7ca802SSteve Glendinning 4673c0f3c60SMarc Zyngier pdata->hash_hi = 0; 4683c0f3c60SMarc Zyngier pdata->hash_lo = 0; 4693c0f3c60SMarc Zyngier 4702f7ca802SSteve Glendinning spin_lock_irqsave(&pdata->mac_cr_lock, flags); 4712f7ca802SSteve Glendinning 4722f7ca802SSteve Glendinning if (dev->net->flags & IFF_PROMISC) { 473a475f603SJoe Perches netif_dbg(dev, drv, dev->net, "promiscuous mode enabled\n"); 4742f7ca802SSteve Glendinning pdata->mac_cr |= MAC_CR_PRMS_; 4752f7ca802SSteve Glendinning pdata->mac_cr &= ~(MAC_CR_MCPAS_ | MAC_CR_HPFILT_); 4762f7ca802SSteve Glendinning } else if (dev->net->flags & IFF_ALLMULTI) { 477a475f603SJoe Perches netif_dbg(dev, drv, dev->net, "receive all multicast enabled\n"); 4782f7ca802SSteve Glendinning pdata->mac_cr |= MAC_CR_MCPAS_; 4792f7ca802SSteve Glendinning pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_HPFILT_); 4804cd24eafSJiri Pirko } else if (!netdev_mc_empty(dev->net)) { 48122bedad3SJiri Pirko struct netdev_hw_addr *ha; 4822f7ca802SSteve Glendinning 4832f7ca802SSteve Glendinning pdata->mac_cr |= MAC_CR_HPFILT_; 4842f7ca802SSteve Glendinning pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_); 4852f7ca802SSteve Glendinning 48622bedad3SJiri Pirko netdev_for_each_mc_addr(ha, netdev) { 48722bedad3SJiri Pirko u32 bitnum = smsc95xx_hash(ha->addr); 4882f7ca802SSteve Glendinning u32 mask = 0x01 << (bitnum & 0x1F); 4892f7ca802SSteve Glendinning if (bitnum & 0x20) 4903c0f3c60SMarc Zyngier pdata->hash_hi |= mask; 4912f7ca802SSteve Glendinning else 4923c0f3c60SMarc Zyngier pdata->hash_lo |= mask; 4932f7ca802SSteve Glendinning } 4942f7ca802SSteve Glendinning 495a475f603SJoe Perches netif_dbg(dev, drv, dev->net, "HASHH=0x%08X, HASHL=0x%08X\n", 4963c0f3c60SMarc Zyngier pdata->hash_hi, pdata->hash_lo); 4972f7ca802SSteve Glendinning } else { 498a475f603SJoe Perches netif_dbg(dev, drv, dev->net, "receive own packets only\n"); 4992f7ca802SSteve Glendinning pdata->mac_cr &= 5002f7ca802SSteve Glendinning ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_ | MAC_CR_HPFILT_); 5012f7ca802SSteve Glendinning } 5022f7ca802SSteve Glendinning 5032f7ca802SSteve Glendinning spin_unlock_irqrestore(&pdata->mac_cr_lock, flags); 5042f7ca802SSteve Glendinning 5052f7ca802SSteve Glendinning /* Initiate async writes, as we can't wait for completion here */ 5067b9e7580SSteve Glendinning ret = smsc95xx_write_reg_async(dev, HASHH, pdata->hash_hi); 507b052e073SSteve Glendinning if (ret < 0) 508b052e073SSteve Glendinning netdev_warn(dev->net, "failed to initiate async write to HASHH\n"); 509769ea6d8SSteve Glendinning 5107b9e7580SSteve Glendinning ret = smsc95xx_write_reg_async(dev, HASHL, pdata->hash_lo); 511b052e073SSteve Glendinning if (ret < 0) 512b052e073SSteve Glendinning netdev_warn(dev->net, "failed to initiate async write to HASHL\n"); 513769ea6d8SSteve Glendinning 5147b9e7580SSteve Glendinning ret = smsc95xx_write_reg_async(dev, MAC_CR, pdata->mac_cr); 515b052e073SSteve Glendinning if (ret < 0) 516b052e073SSteve Glendinning netdev_warn(dev->net, "failed to initiate async write to MAC_CR\n"); 5172f7ca802SSteve Glendinning } 5182f7ca802SSteve Glendinning 519769ea6d8SSteve Glendinning static int smsc95xx_phy_update_flowcontrol(struct usbnet *dev, u8 duplex, 5202f7ca802SSteve Glendinning u16 lcladv, u16 rmtadv) 5212f7ca802SSteve Glendinning { 5222f7ca802SSteve Glendinning u32 flow, afc_cfg = 0; 5232f7ca802SSteve Glendinning 5242f7ca802SSteve Glendinning int ret = smsc95xx_read_reg(dev, AFC_CFG, &afc_cfg); 525e360a8b4SSteve Glendinning if (ret < 0) 526b052e073SSteve Glendinning return ret; 5272f7ca802SSteve Glendinning 5282f7ca802SSteve Glendinning if (duplex == DUPLEX_FULL) { 529bc02ff95SSteve Glendinning u8 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv); 5302f7ca802SSteve Glendinning 5312f7ca802SSteve Glendinning if (cap & FLOW_CTRL_RX) 5322f7ca802SSteve Glendinning flow = 0xFFFF0002; 5332f7ca802SSteve Glendinning else 5342f7ca802SSteve Glendinning flow = 0; 5352f7ca802SSteve Glendinning 5362f7ca802SSteve Glendinning if (cap & FLOW_CTRL_TX) 5372f7ca802SSteve Glendinning afc_cfg |= 0xF; 5382f7ca802SSteve Glendinning else 5392f7ca802SSteve Glendinning afc_cfg &= ~0xF; 5402f7ca802SSteve Glendinning 541a475f603SJoe Perches netif_dbg(dev, link, dev->net, "rx pause %s, tx pause %s\n", 54260b86755SJoe Perches cap & FLOW_CTRL_RX ? "enabled" : "disabled", 54360b86755SJoe Perches cap & FLOW_CTRL_TX ? "enabled" : "disabled"); 5442f7ca802SSteve Glendinning } else { 545a475f603SJoe Perches netif_dbg(dev, link, dev->net, "half duplex\n"); 5462f7ca802SSteve Glendinning flow = 0; 5472f7ca802SSteve Glendinning afc_cfg |= 0xF; 5482f7ca802SSteve Glendinning } 5492f7ca802SSteve Glendinning 550769ea6d8SSteve Glendinning ret = smsc95xx_write_reg(dev, FLOW, flow); 551b052e073SSteve Glendinning if (ret < 0) 552b052e073SSteve Glendinning return ret; 553e360a8b4SSteve Glendinning 554e360a8b4SSteve Glendinning return smsc95xx_write_reg(dev, AFC_CFG, afc_cfg); 5552f7ca802SSteve Glendinning } 5562f7ca802SSteve Glendinning 5572f7ca802SSteve Glendinning static int smsc95xx_link_reset(struct usbnet *dev) 5582f7ca802SSteve Glendinning { 5592f7ca802SSteve Glendinning struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]); 5602f7ca802SSteve Glendinning struct mii_if_info *mii = &dev->mii; 5618ae6dacaSDavid Decotigny struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET }; 5622f7ca802SSteve Glendinning unsigned long flags; 5632f7ca802SSteve Glendinning u16 lcladv, rmtadv; 564769ea6d8SSteve Glendinning int ret; 5652f7ca802SSteve Glendinning 5662f7ca802SSteve Glendinning /* clear interrupt status */ 567769ea6d8SSteve Glendinning ret = smsc95xx_mdio_read(dev->net, mii->phy_id, PHY_INT_SRC); 568e360a8b4SSteve Glendinning if (ret < 0) 569b052e073SSteve Glendinning return ret; 570769ea6d8SSteve Glendinning 571769ea6d8SSteve Glendinning ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_); 572e360a8b4SSteve Glendinning if (ret < 0) 573b052e073SSteve Glendinning return ret; 5742f7ca802SSteve Glendinning 5752f7ca802SSteve Glendinning mii_check_media(mii, 1, 1); 5762f7ca802SSteve Glendinning mii_ethtool_gset(&dev->mii, &ecmd); 5772f7ca802SSteve Glendinning lcladv = smsc95xx_mdio_read(dev->net, mii->phy_id, MII_ADVERTISE); 5782f7ca802SSteve Glendinning rmtadv = smsc95xx_mdio_read(dev->net, mii->phy_id, MII_LPA); 5792f7ca802SSteve Glendinning 5808ae6dacaSDavid Decotigny netif_dbg(dev, link, dev->net, 5818ae6dacaSDavid Decotigny "speed: %u duplex: %d lcladv: %04x rmtadv: %04x\n", 5828ae6dacaSDavid Decotigny ethtool_cmd_speed(&ecmd), ecmd.duplex, lcladv, rmtadv); 5832f7ca802SSteve Glendinning 5842f7ca802SSteve Glendinning spin_lock_irqsave(&pdata->mac_cr_lock, flags); 5852f7ca802SSteve Glendinning if (ecmd.duplex != DUPLEX_FULL) { 5862f7ca802SSteve Glendinning pdata->mac_cr &= ~MAC_CR_FDPX_; 5872f7ca802SSteve Glendinning pdata->mac_cr |= MAC_CR_RCVOWN_; 5882f7ca802SSteve Glendinning } else { 5892f7ca802SSteve Glendinning pdata->mac_cr &= ~MAC_CR_RCVOWN_; 5902f7ca802SSteve Glendinning pdata->mac_cr |= MAC_CR_FDPX_; 5912f7ca802SSteve Glendinning } 5922f7ca802SSteve Glendinning spin_unlock_irqrestore(&pdata->mac_cr_lock, flags); 5932f7ca802SSteve Glendinning 594769ea6d8SSteve Glendinning ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr); 595e360a8b4SSteve Glendinning if (ret < 0) 596b052e073SSteve Glendinning return ret; 5972f7ca802SSteve Glendinning 598769ea6d8SSteve Glendinning ret = smsc95xx_phy_update_flowcontrol(dev, ecmd.duplex, lcladv, rmtadv); 599b052e073SSteve Glendinning if (ret < 0) 600b052e073SSteve Glendinning netdev_warn(dev->net, "Error updating PHY flow control\n"); 6012f7ca802SSteve Glendinning 602b052e073SSteve Glendinning return ret; 6032f7ca802SSteve Glendinning } 6042f7ca802SSteve Glendinning 6052f7ca802SSteve Glendinning static void smsc95xx_status(struct usbnet *dev, struct urb *urb) 6062f7ca802SSteve Glendinning { 6072f7ca802SSteve Glendinning u32 intdata; 6082f7ca802SSteve Glendinning 6092f7ca802SSteve Glendinning if (urb->actual_length != 4) { 61060b86755SJoe Perches netdev_warn(dev->net, "unexpected urb length %d\n", 61160b86755SJoe Perches urb->actual_length); 6122f7ca802SSteve Glendinning return; 6132f7ca802SSteve Glendinning } 6142f7ca802SSteve Glendinning 6152f7ca802SSteve Glendinning memcpy(&intdata, urb->transfer_buffer, 4); 6161d74a6bdSSteve Glendinning le32_to_cpus(&intdata); 6172f7ca802SSteve Glendinning 618a475f603SJoe Perches netif_dbg(dev, link, dev->net, "intdata: 0x%08X\n", intdata); 6192f7ca802SSteve Glendinning 6202f7ca802SSteve Glendinning if (intdata & INT_ENP_PHY_INT_) 6212f7ca802SSteve Glendinning usbnet_defer_kevent(dev, EVENT_LINK_RESET); 6222f7ca802SSteve Glendinning else 62360b86755SJoe Perches netdev_warn(dev->net, "unexpected interrupt, intdata=0x%08X\n", 62460b86755SJoe Perches intdata); 6252f7ca802SSteve Glendinning } 6262f7ca802SSteve Glendinning 627f7b29271SSteve Glendinning /* Enable or disable Tx & Rx checksum offload engines */ 628c8f44affSMichał Mirosław static int smsc95xx_set_features(struct net_device *netdev, 629c8f44affSMichał Mirosław netdev_features_t features) 6302f7ca802SSteve Glendinning { 63178e47fe4SMichał Mirosław struct usbnet *dev = netdev_priv(netdev); 6322f7ca802SSteve Glendinning u32 read_buf; 63378e47fe4SMichał Mirosław int ret; 63478e47fe4SMichał Mirosław 63578e47fe4SMichał Mirosław ret = smsc95xx_read_reg(dev, COE_CR, &read_buf); 636e360a8b4SSteve Glendinning if (ret < 0) 637b052e073SSteve Glendinning return ret; 6382f7ca802SSteve Glendinning 63978e47fe4SMichał Mirosław if (features & NETIF_F_HW_CSUM) 640f7b29271SSteve Glendinning read_buf |= Tx_COE_EN_; 641f7b29271SSteve Glendinning else 642f7b29271SSteve Glendinning read_buf &= ~Tx_COE_EN_; 643f7b29271SSteve Glendinning 64478e47fe4SMichał Mirosław if (features & NETIF_F_RXCSUM) 6452f7ca802SSteve Glendinning read_buf |= Rx_COE_EN_; 6462f7ca802SSteve Glendinning else 6472f7ca802SSteve Glendinning read_buf &= ~Rx_COE_EN_; 6482f7ca802SSteve Glendinning 6492f7ca802SSteve Glendinning ret = smsc95xx_write_reg(dev, COE_CR, read_buf); 650e360a8b4SSteve Glendinning if (ret < 0) 651b052e073SSteve Glendinning return ret; 6522f7ca802SSteve Glendinning 653a475f603SJoe Perches netif_dbg(dev, hw, dev->net, "COE_CR = 0x%08x\n", read_buf); 6542f7ca802SSteve Glendinning return 0; 6552f7ca802SSteve Glendinning } 6562f7ca802SSteve Glendinning 6572f7ca802SSteve Glendinning static int smsc95xx_ethtool_get_eeprom_len(struct net_device *net) 6582f7ca802SSteve Glendinning { 6592f7ca802SSteve Glendinning return MAX_EEPROM_SIZE; 6602f7ca802SSteve Glendinning } 6612f7ca802SSteve Glendinning 6622f7ca802SSteve Glendinning static int smsc95xx_ethtool_get_eeprom(struct net_device *netdev, 6632f7ca802SSteve Glendinning struct ethtool_eeprom *ee, u8 *data) 6642f7ca802SSteve Glendinning { 6652f7ca802SSteve Glendinning struct usbnet *dev = netdev_priv(netdev); 6662f7ca802SSteve Glendinning 6672f7ca802SSteve Glendinning ee->magic = LAN95XX_EEPROM_MAGIC; 6682f7ca802SSteve Glendinning 6692f7ca802SSteve Glendinning return smsc95xx_read_eeprom(dev, ee->offset, ee->len, data); 6702f7ca802SSteve Glendinning } 6712f7ca802SSteve Glendinning 6722f7ca802SSteve Glendinning static int smsc95xx_ethtool_set_eeprom(struct net_device *netdev, 6732f7ca802SSteve Glendinning struct ethtool_eeprom *ee, u8 *data) 6742f7ca802SSteve Glendinning { 6752f7ca802SSteve Glendinning struct usbnet *dev = netdev_priv(netdev); 6762f7ca802SSteve Glendinning 6772f7ca802SSteve Glendinning if (ee->magic != LAN95XX_EEPROM_MAGIC) { 67860b86755SJoe Perches netdev_warn(dev->net, "EEPROM: magic value mismatch, magic = 0x%x\n", 6792f7ca802SSteve Glendinning ee->magic); 6802f7ca802SSteve Glendinning return -EINVAL; 6812f7ca802SSteve Glendinning } 6822f7ca802SSteve Glendinning 6832f7ca802SSteve Glendinning return smsc95xx_write_eeprom(dev, ee->offset, ee->len, data); 6842f7ca802SSteve Glendinning } 6852f7ca802SSteve Glendinning 6869fa32e94SEmeric Vigier static int smsc95xx_ethtool_getregslen(struct net_device *netdev) 6879fa32e94SEmeric Vigier { 6889fa32e94SEmeric Vigier /* all smsc95xx registers */ 68996245317SSteve Glendinning return COE_CR - ID_REV + sizeof(u32); 6909fa32e94SEmeric Vigier } 6919fa32e94SEmeric Vigier 6929fa32e94SEmeric Vigier static void 6939fa32e94SEmeric Vigier smsc95xx_ethtool_getregs(struct net_device *netdev, struct ethtool_regs *regs, 6949fa32e94SEmeric Vigier void *buf) 6959fa32e94SEmeric Vigier { 6969fa32e94SEmeric Vigier struct usbnet *dev = netdev_priv(netdev); 697d348446bSDan Carpenter unsigned int i, j; 698d348446bSDan Carpenter int retval; 6999fa32e94SEmeric Vigier u32 *data = buf; 7009fa32e94SEmeric Vigier 7019fa32e94SEmeric Vigier retval = smsc95xx_read_reg(dev, ID_REV, ®s->version); 7029fa32e94SEmeric Vigier if (retval < 0) { 7039fa32e94SEmeric Vigier netdev_warn(netdev, "REGS: cannot read ID_REV\n"); 7049fa32e94SEmeric Vigier return; 7059fa32e94SEmeric Vigier } 7069fa32e94SEmeric Vigier 7079fa32e94SEmeric Vigier for (i = ID_REV, j = 0; i <= COE_CR; i += (sizeof(u32)), j++) { 7089fa32e94SEmeric Vigier retval = smsc95xx_read_reg(dev, i, &data[j]); 7099fa32e94SEmeric Vigier if (retval < 0) { 7109fa32e94SEmeric Vigier netdev_warn(netdev, "REGS: cannot read reg[%x]\n", i); 7119fa32e94SEmeric Vigier return; 7129fa32e94SEmeric Vigier } 7139fa32e94SEmeric Vigier } 7149fa32e94SEmeric Vigier } 7159fa32e94SEmeric Vigier 716e0e474a8SSteve Glendinning static void smsc95xx_ethtool_get_wol(struct net_device *net, 717e0e474a8SSteve Glendinning struct ethtool_wolinfo *wolinfo) 718e0e474a8SSteve Glendinning { 719e0e474a8SSteve Glendinning struct usbnet *dev = netdev_priv(net); 720e0e474a8SSteve Glendinning struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]); 721e0e474a8SSteve Glendinning 722e0e474a8SSteve Glendinning wolinfo->supported = SUPPORTED_WAKE; 723e0e474a8SSteve Glendinning wolinfo->wolopts = pdata->wolopts; 724e0e474a8SSteve Glendinning } 725e0e474a8SSteve Glendinning 726e0e474a8SSteve Glendinning static int smsc95xx_ethtool_set_wol(struct net_device *net, 727e0e474a8SSteve Glendinning struct ethtool_wolinfo *wolinfo) 728e0e474a8SSteve Glendinning { 729e0e474a8SSteve Glendinning struct usbnet *dev = netdev_priv(net); 730e0e474a8SSteve Glendinning struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]); 7313b14692cSSteve Glendinning int ret; 732e0e474a8SSteve Glendinning 733e0e474a8SSteve Glendinning pdata->wolopts = wolinfo->wolopts & SUPPORTED_WAKE; 7343b14692cSSteve Glendinning 7353b14692cSSteve Glendinning ret = device_set_wakeup_enable(&dev->udev->dev, pdata->wolopts); 736b052e073SSteve Glendinning if (ret < 0) 737b052e073SSteve Glendinning netdev_warn(dev->net, "device_set_wakeup_enable error %d\n", ret); 7383b14692cSSteve Glendinning 739b052e073SSteve Glendinning return ret; 740e0e474a8SSteve Glendinning } 741e0e474a8SSteve Glendinning 7420fc0b732SStephen Hemminger static const struct ethtool_ops smsc95xx_ethtool_ops = { 7432f7ca802SSteve Glendinning .get_link = usbnet_get_link, 7442f7ca802SSteve Glendinning .nway_reset = usbnet_nway_reset, 7452f7ca802SSteve Glendinning .get_drvinfo = usbnet_get_drvinfo, 7462f7ca802SSteve Glendinning .get_msglevel = usbnet_get_msglevel, 7472f7ca802SSteve Glendinning .set_msglevel = usbnet_set_msglevel, 7482f7ca802SSteve Glendinning .get_settings = usbnet_get_settings, 7492f7ca802SSteve Glendinning .set_settings = usbnet_set_settings, 7502f7ca802SSteve Glendinning .get_eeprom_len = smsc95xx_ethtool_get_eeprom_len, 7512f7ca802SSteve Glendinning .get_eeprom = smsc95xx_ethtool_get_eeprom, 7522f7ca802SSteve Glendinning .set_eeprom = smsc95xx_ethtool_set_eeprom, 7539fa32e94SEmeric Vigier .get_regs_len = smsc95xx_ethtool_getregslen, 7549fa32e94SEmeric Vigier .get_regs = smsc95xx_ethtool_getregs, 755e0e474a8SSteve Glendinning .get_wol = smsc95xx_ethtool_get_wol, 756e0e474a8SSteve Glendinning .set_wol = smsc95xx_ethtool_set_wol, 7572f7ca802SSteve Glendinning }; 7582f7ca802SSteve Glendinning 7592f7ca802SSteve Glendinning static int smsc95xx_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd) 7602f7ca802SSteve Glendinning { 7612f7ca802SSteve Glendinning struct usbnet *dev = netdev_priv(netdev); 7622f7ca802SSteve Glendinning 7632f7ca802SSteve Glendinning if (!netif_running(netdev)) 7642f7ca802SSteve Glendinning return -EINVAL; 7652f7ca802SSteve Glendinning 7662f7ca802SSteve Glendinning return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL); 7672f7ca802SSteve Glendinning } 7682f7ca802SSteve Glendinning 7692f7ca802SSteve Glendinning static void smsc95xx_init_mac_address(struct usbnet *dev) 7702f7ca802SSteve Glendinning { 771c489565bSArnd Bergmann const u8 *mac_addr; 772c489565bSArnd Bergmann 773c489565bSArnd Bergmann /* maybe the boot loader passed the MAC address in devicetree */ 774c489565bSArnd Bergmann mac_addr = of_get_mac_address(dev->udev->dev.of_node); 775c489565bSArnd Bergmann if (mac_addr) { 776c489565bSArnd Bergmann memcpy(dev->net->dev_addr, mac_addr, ETH_ALEN); 777c489565bSArnd Bergmann return; 778c489565bSArnd Bergmann } 779c489565bSArnd Bergmann 7802f7ca802SSteve Glendinning /* try reading mac address from EEPROM */ 7812f7ca802SSteve Glendinning if (smsc95xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN, 7822f7ca802SSteve Glendinning dev->net->dev_addr) == 0) { 7832f7ca802SSteve Glendinning if (is_valid_ether_addr(dev->net->dev_addr)) { 7842f7ca802SSteve Glendinning /* eeprom values are valid so use them */ 785a475f603SJoe Perches netif_dbg(dev, ifup, dev->net, "MAC address read from EEPROM\n"); 7862f7ca802SSteve Glendinning return; 7872f7ca802SSteve Glendinning } 7882f7ca802SSteve Glendinning } 7892f7ca802SSteve Glendinning 790c489565bSArnd Bergmann /* no useful static MAC address found. generate a random one */ 791f2cedb63SDanny Kukawka eth_hw_addr_random(dev->net); 792c7e12eadSJoe Perches netif_dbg(dev, ifup, dev->net, "MAC address set to eth_random_addr\n"); 7932f7ca802SSteve Glendinning } 7942f7ca802SSteve Glendinning 7952f7ca802SSteve Glendinning static int smsc95xx_set_mac_address(struct usbnet *dev) 7962f7ca802SSteve Glendinning { 7972f7ca802SSteve Glendinning u32 addr_lo = dev->net->dev_addr[0] | dev->net->dev_addr[1] << 8 | 7982f7ca802SSteve Glendinning dev->net->dev_addr[2] << 16 | dev->net->dev_addr[3] << 24; 7992f7ca802SSteve Glendinning u32 addr_hi = dev->net->dev_addr[4] | dev->net->dev_addr[5] << 8; 8002f7ca802SSteve Glendinning int ret; 8012f7ca802SSteve Glendinning 8022f7ca802SSteve Glendinning ret = smsc95xx_write_reg(dev, ADDRL, addr_lo); 803b052e073SSteve Glendinning if (ret < 0) 804b052e073SSteve Glendinning return ret; 805e360a8b4SSteve Glendinning 806e360a8b4SSteve Glendinning return smsc95xx_write_reg(dev, ADDRH, addr_hi); 8072f7ca802SSteve Glendinning } 8082f7ca802SSteve Glendinning 8092f7ca802SSteve Glendinning /* starts the TX path */ 810769ea6d8SSteve Glendinning static int smsc95xx_start_tx_path(struct usbnet *dev) 8112f7ca802SSteve Glendinning { 8122f7ca802SSteve Glendinning struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]); 8132f7ca802SSteve Glendinning unsigned long flags; 814769ea6d8SSteve Glendinning int ret; 8152f7ca802SSteve Glendinning 8162f7ca802SSteve Glendinning /* Enable Tx at MAC */ 8172f7ca802SSteve Glendinning spin_lock_irqsave(&pdata->mac_cr_lock, flags); 8182f7ca802SSteve Glendinning pdata->mac_cr |= MAC_CR_TXEN_; 8192f7ca802SSteve Glendinning spin_unlock_irqrestore(&pdata->mac_cr_lock, flags); 8202f7ca802SSteve Glendinning 821769ea6d8SSteve Glendinning ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr); 822e360a8b4SSteve Glendinning if (ret < 0) 823b052e073SSteve Glendinning return ret; 8242f7ca802SSteve Glendinning 8252f7ca802SSteve Glendinning /* Enable Tx at SCSRs */ 826e360a8b4SSteve Glendinning return smsc95xx_write_reg(dev, TX_CFG, TX_CFG_ON_); 8272f7ca802SSteve Glendinning } 8282f7ca802SSteve Glendinning 8292f7ca802SSteve Glendinning /* Starts the Receive path */ 830ec32115dSMing Lei static int smsc95xx_start_rx_path(struct usbnet *dev, int in_pm) 8312f7ca802SSteve Glendinning { 8322f7ca802SSteve Glendinning struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]); 8332f7ca802SSteve Glendinning unsigned long flags; 8342f7ca802SSteve Glendinning 8352f7ca802SSteve Glendinning spin_lock_irqsave(&pdata->mac_cr_lock, flags); 8362f7ca802SSteve Glendinning pdata->mac_cr |= MAC_CR_RXEN_; 8372f7ca802SSteve Glendinning spin_unlock_irqrestore(&pdata->mac_cr_lock, flags); 8382f7ca802SSteve Glendinning 839e360a8b4SSteve Glendinning return __smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr, in_pm); 8402f7ca802SSteve Glendinning } 8412f7ca802SSteve Glendinning 8422f7ca802SSteve Glendinning static int smsc95xx_phy_initialize(struct usbnet *dev) 8432f7ca802SSteve Glendinning { 844769ea6d8SSteve Glendinning int bmcr, ret, timeout = 0; 845db443c44SSteve Glendinning 8462f7ca802SSteve Glendinning /* Initialize MII structure */ 8472f7ca802SSteve Glendinning dev->mii.dev = dev->net; 8482f7ca802SSteve Glendinning dev->mii.mdio_read = smsc95xx_mdio_read; 8492f7ca802SSteve Glendinning dev->mii.mdio_write = smsc95xx_mdio_write; 8502f7ca802SSteve Glendinning dev->mii.phy_id_mask = 0x1f; 8512f7ca802SSteve Glendinning dev->mii.reg_num_mask = 0x1f; 8522f7ca802SSteve Glendinning dev->mii.phy_id = SMSC95XX_INTERNAL_PHY_ID; 8532f7ca802SSteve Glendinning 854db443c44SSteve Glendinning /* reset phy and wait for reset to complete */ 8552f7ca802SSteve Glendinning smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET); 856db443c44SSteve Glendinning 857db443c44SSteve Glendinning do { 858db443c44SSteve Glendinning msleep(10); 859db443c44SSteve Glendinning bmcr = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, MII_BMCR); 860db443c44SSteve Glendinning timeout++; 861d9460920SRabin Vincent } while ((bmcr & BMCR_RESET) && (timeout < 100)); 862db443c44SSteve Glendinning 863db443c44SSteve Glendinning if (timeout >= 100) { 864db443c44SSteve Glendinning netdev_warn(dev->net, "timeout on PHY Reset"); 865db443c44SSteve Glendinning return -EIO; 866db443c44SSteve Glendinning } 867db443c44SSteve Glendinning 8682f7ca802SSteve Glendinning smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE, 8692f7ca802SSteve Glendinning ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP | 8702f7ca802SSteve Glendinning ADVERTISE_PAUSE_ASYM); 8712f7ca802SSteve Glendinning 8722f7ca802SSteve Glendinning /* read to clear */ 873769ea6d8SSteve Glendinning ret = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, PHY_INT_SRC); 874b052e073SSteve Glendinning if (ret < 0) { 875b052e073SSteve Glendinning netdev_warn(dev->net, "Failed to read PHY_INT_SRC during init\n"); 876b052e073SSteve Glendinning return ret; 877b052e073SSteve Glendinning } 8782f7ca802SSteve Glendinning 8792f7ca802SSteve Glendinning smsc95xx_mdio_write(dev->net, dev->mii.phy_id, PHY_INT_MASK, 8802f7ca802SSteve Glendinning PHY_INT_MASK_DEFAULT_); 8812f7ca802SSteve Glendinning mii_nway_restart(&dev->mii); 8822f7ca802SSteve Glendinning 883a475f603SJoe Perches netif_dbg(dev, ifup, dev->net, "phy initialised successfully\n"); 8842f7ca802SSteve Glendinning return 0; 8852f7ca802SSteve Glendinning } 8862f7ca802SSteve Glendinning 8872f7ca802SSteve Glendinning static int smsc95xx_reset(struct usbnet *dev) 8882f7ca802SSteve Glendinning { 8892f7ca802SSteve Glendinning struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]); 8902f7ca802SSteve Glendinning u32 read_buf, write_buf, burst_cap; 8912f7ca802SSteve Glendinning int ret = 0, timeout; 8922f7ca802SSteve Glendinning 893a475f603SJoe Perches netif_dbg(dev, ifup, dev->net, "entering smsc95xx_reset\n"); 8942f7ca802SSteve Glendinning 8954436761bSSteve Glendinning ret = smsc95xx_write_reg(dev, HW_CFG, HW_CFG_LRST_); 896e360a8b4SSteve Glendinning if (ret < 0) 897b052e073SSteve Glendinning return ret; 8982f7ca802SSteve Glendinning 8992f7ca802SSteve Glendinning timeout = 0; 9002f7ca802SSteve Glendinning do { 901cf2acec2SSteve Glendinning msleep(10); 9022f7ca802SSteve Glendinning ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf); 903e360a8b4SSteve Glendinning if (ret < 0) 904b052e073SSteve Glendinning return ret; 9052f7ca802SSteve Glendinning timeout++; 9062f7ca802SSteve Glendinning } while ((read_buf & HW_CFG_LRST_) && (timeout < 100)); 9072f7ca802SSteve Glendinning 9082f7ca802SSteve Glendinning if (timeout >= 100) { 90960b86755SJoe Perches netdev_warn(dev->net, "timeout waiting for completion of Lite Reset\n"); 9102f7ca802SSteve Glendinning return ret; 9112f7ca802SSteve Glendinning } 9122f7ca802SSteve Glendinning 9134436761bSSteve Glendinning ret = smsc95xx_write_reg(dev, PM_CTRL, PM_CTL_PHY_RST_); 914e360a8b4SSteve Glendinning if (ret < 0) 915b052e073SSteve Glendinning return ret; 9162f7ca802SSteve Glendinning 9172f7ca802SSteve Glendinning timeout = 0; 9182f7ca802SSteve Glendinning do { 919cf2acec2SSteve Glendinning msleep(10); 9202f7ca802SSteve Glendinning ret = smsc95xx_read_reg(dev, PM_CTRL, &read_buf); 921e360a8b4SSteve Glendinning if (ret < 0) 922b052e073SSteve Glendinning return ret; 9232f7ca802SSteve Glendinning timeout++; 9242f7ca802SSteve Glendinning } while ((read_buf & PM_CTL_PHY_RST_) && (timeout < 100)); 9252f7ca802SSteve Glendinning 9262f7ca802SSteve Glendinning if (timeout >= 100) { 92760b86755SJoe Perches netdev_warn(dev->net, "timeout waiting for PHY Reset\n"); 9282f7ca802SSteve Glendinning return ret; 9292f7ca802SSteve Glendinning } 9302f7ca802SSteve Glendinning 9312f7ca802SSteve Glendinning ret = smsc95xx_set_mac_address(dev); 9322f7ca802SSteve Glendinning if (ret < 0) 9332f7ca802SSteve Glendinning return ret; 9342f7ca802SSteve Glendinning 9351e1d7412SJoe Perches netif_dbg(dev, ifup, dev->net, "MAC Address: %pM\n", 9361e1d7412SJoe Perches dev->net->dev_addr); 9372f7ca802SSteve Glendinning 9382f7ca802SSteve Glendinning ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf); 939e360a8b4SSteve Glendinning if (ret < 0) 940b052e073SSteve Glendinning return ret; 9412f7ca802SSteve Glendinning 9421e1d7412SJoe Perches netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG : 0x%08x\n", 9431e1d7412SJoe Perches read_buf); 9442f7ca802SSteve Glendinning 9452f7ca802SSteve Glendinning read_buf |= HW_CFG_BIR_; 9462f7ca802SSteve Glendinning 9472f7ca802SSteve Glendinning ret = smsc95xx_write_reg(dev, HW_CFG, read_buf); 948e360a8b4SSteve Glendinning if (ret < 0) 949b052e073SSteve Glendinning return ret; 9502f7ca802SSteve Glendinning 9512f7ca802SSteve Glendinning ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf); 952e360a8b4SSteve Glendinning if (ret < 0) 953b052e073SSteve Glendinning return ret; 954b052e073SSteve Glendinning 955a475f603SJoe Perches netif_dbg(dev, ifup, dev->net, 956a475f603SJoe Perches "Read Value from HW_CFG after writing HW_CFG_BIR_: 0x%08x\n", 95760b86755SJoe Perches read_buf); 9582f7ca802SSteve Glendinning 9592f7ca802SSteve Glendinning if (!turbo_mode) { 9602f7ca802SSteve Glendinning burst_cap = 0; 9612f7ca802SSteve Glendinning dev->rx_urb_size = MAX_SINGLE_PACKET_SIZE; 9622f7ca802SSteve Glendinning } else if (dev->udev->speed == USB_SPEED_HIGH) { 9632f7ca802SSteve Glendinning burst_cap = DEFAULT_HS_BURST_CAP_SIZE / HS_USB_PKT_SIZE; 9642f7ca802SSteve Glendinning dev->rx_urb_size = DEFAULT_HS_BURST_CAP_SIZE; 9652f7ca802SSteve Glendinning } else { 9662f7ca802SSteve Glendinning burst_cap = DEFAULT_FS_BURST_CAP_SIZE / FS_USB_PKT_SIZE; 9672f7ca802SSteve Glendinning dev->rx_urb_size = DEFAULT_FS_BURST_CAP_SIZE; 9682f7ca802SSteve Glendinning } 9692f7ca802SSteve Glendinning 9701e1d7412SJoe Perches netif_dbg(dev, ifup, dev->net, "rx_urb_size=%ld\n", 9711e1d7412SJoe Perches (ulong)dev->rx_urb_size); 9722f7ca802SSteve Glendinning 9732f7ca802SSteve Glendinning ret = smsc95xx_write_reg(dev, BURST_CAP, burst_cap); 974e360a8b4SSteve Glendinning if (ret < 0) 975b052e073SSteve Glendinning return ret; 9762f7ca802SSteve Glendinning 9772f7ca802SSteve Glendinning ret = smsc95xx_read_reg(dev, BURST_CAP, &read_buf); 978e360a8b4SSteve Glendinning if (ret < 0) 979b052e073SSteve Glendinning return ret; 980769ea6d8SSteve Glendinning 981a475f603SJoe Perches netif_dbg(dev, ifup, dev->net, 982a475f603SJoe Perches "Read Value from BURST_CAP after writing: 0x%08x\n", 9832f7ca802SSteve Glendinning read_buf); 9842f7ca802SSteve Glendinning 9854436761bSSteve Glendinning ret = smsc95xx_write_reg(dev, BULK_IN_DLY, DEFAULT_BULK_IN_DELAY); 986e360a8b4SSteve Glendinning if (ret < 0) 987b052e073SSteve Glendinning return ret; 9882f7ca802SSteve Glendinning 9892f7ca802SSteve Glendinning ret = smsc95xx_read_reg(dev, BULK_IN_DLY, &read_buf); 990e360a8b4SSteve Glendinning if (ret < 0) 991b052e073SSteve Glendinning return ret; 992769ea6d8SSteve Glendinning 993a475f603SJoe Perches netif_dbg(dev, ifup, dev->net, 994a475f603SJoe Perches "Read Value from BULK_IN_DLY after writing: 0x%08x\n", 99560b86755SJoe Perches read_buf); 9962f7ca802SSteve Glendinning 9972f7ca802SSteve Glendinning ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf); 998e360a8b4SSteve Glendinning if (ret < 0) 999b052e073SSteve Glendinning return ret; 1000769ea6d8SSteve Glendinning 10011e1d7412SJoe Perches netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG: 0x%08x\n", 10021e1d7412SJoe Perches read_buf); 10032f7ca802SSteve Glendinning 10042f7ca802SSteve Glendinning if (turbo_mode) 10052f7ca802SSteve Glendinning read_buf |= (HW_CFG_MEF_ | HW_CFG_BCE_); 10062f7ca802SSteve Glendinning 10072f7ca802SSteve Glendinning read_buf &= ~HW_CFG_RXDOFF_; 10082f7ca802SSteve Glendinning 10092f7ca802SSteve Glendinning /* set Rx data offset=2, Make IP header aligns on word boundary. */ 10102f7ca802SSteve Glendinning read_buf |= NET_IP_ALIGN << 9; 10112f7ca802SSteve Glendinning 10122f7ca802SSteve Glendinning ret = smsc95xx_write_reg(dev, HW_CFG, read_buf); 1013e360a8b4SSteve Glendinning if (ret < 0) 1014b052e073SSteve Glendinning return ret; 10152f7ca802SSteve Glendinning 10162f7ca802SSteve Glendinning ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf); 1017e360a8b4SSteve Glendinning if (ret < 0) 1018b052e073SSteve Glendinning return ret; 1019769ea6d8SSteve Glendinning 1020a475f603SJoe Perches netif_dbg(dev, ifup, dev->net, 1021a475f603SJoe Perches "Read Value from HW_CFG after writing: 0x%08x\n", read_buf); 10222f7ca802SSteve Glendinning 10234436761bSSteve Glendinning ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_); 1024e360a8b4SSteve Glendinning if (ret < 0) 1025b052e073SSteve Glendinning return ret; 10262f7ca802SSteve Glendinning 10272f7ca802SSteve Glendinning ret = smsc95xx_read_reg(dev, ID_REV, &read_buf); 1028e360a8b4SSteve Glendinning if (ret < 0) 1029b052e073SSteve Glendinning return ret; 1030a475f603SJoe Perches netif_dbg(dev, ifup, dev->net, "ID_REV = 0x%08x\n", read_buf); 10312f7ca802SSteve Glendinning 1032f293501cSSteve Glendinning /* Configure GPIO pins as LED outputs */ 1033f293501cSSteve Glendinning write_buf = LED_GPIO_CFG_SPD_LED | LED_GPIO_CFG_LNK_LED | 1034f293501cSSteve Glendinning LED_GPIO_CFG_FDX_LED; 1035f293501cSSteve Glendinning ret = smsc95xx_write_reg(dev, LED_GPIO_CFG, write_buf); 1036e360a8b4SSteve Glendinning if (ret < 0) 1037b052e073SSteve Glendinning return ret; 1038f293501cSSteve Glendinning 10392f7ca802SSteve Glendinning /* Init Tx */ 10404436761bSSteve Glendinning ret = smsc95xx_write_reg(dev, FLOW, 0); 1041e360a8b4SSteve Glendinning if (ret < 0) 1042b052e073SSteve Glendinning return ret; 10432f7ca802SSteve Glendinning 10444436761bSSteve Glendinning ret = smsc95xx_write_reg(dev, AFC_CFG, AFC_CFG_DEFAULT); 1045e360a8b4SSteve Glendinning if (ret < 0) 1046b052e073SSteve Glendinning return ret; 10472f7ca802SSteve Glendinning 10482f7ca802SSteve Glendinning /* Don't need mac_cr_lock during initialisation */ 10492f7ca802SSteve Glendinning ret = smsc95xx_read_reg(dev, MAC_CR, &pdata->mac_cr); 1050e360a8b4SSteve Glendinning if (ret < 0) 1051b052e073SSteve Glendinning return ret; 10522f7ca802SSteve Glendinning 10532f7ca802SSteve Glendinning /* Init Rx */ 10542f7ca802SSteve Glendinning /* Set Vlan */ 10554436761bSSteve Glendinning ret = smsc95xx_write_reg(dev, VLAN1, (u32)ETH_P_8021Q); 1056e360a8b4SSteve Glendinning if (ret < 0) 1057b052e073SSteve Glendinning return ret; 10582f7ca802SSteve Glendinning 1059f7b29271SSteve Glendinning /* Enable or disable checksum offload engines */ 1060769ea6d8SSteve Glendinning ret = smsc95xx_set_features(dev->net, dev->net->features); 1061b052e073SSteve Glendinning if (ret < 0) { 1062b052e073SSteve Glendinning netdev_warn(dev->net, "Failed to set checksum offload features\n"); 1063b052e073SSteve Glendinning return ret; 1064b052e073SSteve Glendinning } 10652f7ca802SSteve Glendinning 10662f7ca802SSteve Glendinning smsc95xx_set_multicast(dev->net); 10672f7ca802SSteve Glendinning 1068769ea6d8SSteve Glendinning ret = smsc95xx_phy_initialize(dev); 1069b052e073SSteve Glendinning if (ret < 0) { 1070b052e073SSteve Glendinning netdev_warn(dev->net, "Failed to init PHY\n"); 1071b052e073SSteve Glendinning return ret; 1072b052e073SSteve Glendinning } 10732f7ca802SSteve Glendinning 10742f7ca802SSteve Glendinning ret = smsc95xx_read_reg(dev, INT_EP_CTL, &read_buf); 1075e360a8b4SSteve Glendinning if (ret < 0) 1076b052e073SSteve Glendinning return ret; 10772f7ca802SSteve Glendinning 10782f7ca802SSteve Glendinning /* enable PHY interrupts */ 10792f7ca802SSteve Glendinning read_buf |= INT_EP_CTL_PHY_INT_; 10802f7ca802SSteve Glendinning 10812f7ca802SSteve Glendinning ret = smsc95xx_write_reg(dev, INT_EP_CTL, read_buf); 1082e360a8b4SSteve Glendinning if (ret < 0) 1083b052e073SSteve Glendinning return ret; 10842f7ca802SSteve Glendinning 1085769ea6d8SSteve Glendinning ret = smsc95xx_start_tx_path(dev); 1086b052e073SSteve Glendinning if (ret < 0) { 1087b052e073SSteve Glendinning netdev_warn(dev->net, "Failed to start TX path\n"); 1088b052e073SSteve Glendinning return ret; 1089b052e073SSteve Glendinning } 1090769ea6d8SSteve Glendinning 1091ec32115dSMing Lei ret = smsc95xx_start_rx_path(dev, 0); 1092b052e073SSteve Glendinning if (ret < 0) { 1093b052e073SSteve Glendinning netdev_warn(dev->net, "Failed to start RX path\n"); 1094b052e073SSteve Glendinning return ret; 1095b052e073SSteve Glendinning } 10962f7ca802SSteve Glendinning 1097a475f603SJoe Perches netif_dbg(dev, ifup, dev->net, "smsc95xx_reset, return 0\n"); 10982f7ca802SSteve Glendinning return 0; 10992f7ca802SSteve Glendinning } 11002f7ca802SSteve Glendinning 110163e77b39SStephen Hemminger static const struct net_device_ops smsc95xx_netdev_ops = { 110263e77b39SStephen Hemminger .ndo_open = usbnet_open, 110363e77b39SStephen Hemminger .ndo_stop = usbnet_stop, 110463e77b39SStephen Hemminger .ndo_start_xmit = usbnet_start_xmit, 110563e77b39SStephen Hemminger .ndo_tx_timeout = usbnet_tx_timeout, 110663e77b39SStephen Hemminger .ndo_change_mtu = usbnet_change_mtu, 110763e77b39SStephen Hemminger .ndo_set_mac_address = eth_mac_addr, 110863e77b39SStephen Hemminger .ndo_validate_addr = eth_validate_addr, 110963e77b39SStephen Hemminger .ndo_do_ioctl = smsc95xx_ioctl, 1110afc4b13dSJiri Pirko .ndo_set_rx_mode = smsc95xx_set_multicast, 111178e47fe4SMichał Mirosław .ndo_set_features = smsc95xx_set_features, 111263e77b39SStephen Hemminger }; 111363e77b39SStephen Hemminger 11142f7ca802SSteve Glendinning static int smsc95xx_bind(struct usbnet *dev, struct usb_interface *intf) 11152f7ca802SSteve Glendinning { 11162f7ca802SSteve Glendinning struct smsc95xx_priv *pdata = NULL; 1117bbd9f9eeSSteve Glendinning u32 val; 11182f7ca802SSteve Glendinning int ret; 11192f7ca802SSteve Glendinning 11202f7ca802SSteve Glendinning printk(KERN_INFO SMSC_CHIPNAME " v" SMSC_DRIVER_VERSION "\n"); 11212f7ca802SSteve Glendinning 11222f7ca802SSteve Glendinning ret = usbnet_get_endpoints(dev, intf); 1123b052e073SSteve Glendinning if (ret < 0) { 1124b052e073SSteve Glendinning netdev_warn(dev->net, "usbnet_get_endpoints failed: %d\n", ret); 1125b052e073SSteve Glendinning return ret; 1126b052e073SSteve Glendinning } 11272f7ca802SSteve Glendinning 11282f7ca802SSteve Glendinning dev->data[0] = (unsigned long)kzalloc(sizeof(struct smsc95xx_priv), 11292f7ca802SSteve Glendinning GFP_KERNEL); 11302f7ca802SSteve Glendinning 11312f7ca802SSteve Glendinning pdata = (struct smsc95xx_priv *)(dev->data[0]); 113238673c82SJoe Perches if (!pdata) 11332f7ca802SSteve Glendinning return -ENOMEM; 11342f7ca802SSteve Glendinning 11352f7ca802SSteve Glendinning spin_lock_init(&pdata->mac_cr_lock); 11362f7ca802SSteve Glendinning 113778e47fe4SMichał Mirosław if (DEFAULT_TX_CSUM_ENABLE) 113878e47fe4SMichał Mirosław dev->net->features |= NETIF_F_HW_CSUM; 113978e47fe4SMichał Mirosław if (DEFAULT_RX_CSUM_ENABLE) 114078e47fe4SMichał Mirosław dev->net->features |= NETIF_F_RXCSUM; 114178e47fe4SMichał Mirosław 114278e47fe4SMichał Mirosław dev->net->hw_features = NETIF_F_HW_CSUM | NETIF_F_RXCSUM; 11432f7ca802SSteve Glendinning 1144f4e8ab7cSBernard Blackham smsc95xx_init_mac_address(dev); 1145f4e8ab7cSBernard Blackham 11462f7ca802SSteve Glendinning /* Init all registers */ 11472f7ca802SSteve Glendinning ret = smsc95xx_reset(dev); 11482f7ca802SSteve Glendinning 1149bbd9f9eeSSteve Glendinning /* detect device revision as different features may be available */ 1150bbd9f9eeSSteve Glendinning ret = smsc95xx_read_reg(dev, ID_REV, &val); 1151e360a8b4SSteve Glendinning if (ret < 0) 1152b052e073SSteve Glendinning return ret; 1153bbd9f9eeSSteve Glendinning val >>= 16; 11549ebca507SSteve Glendinning 11559ebca507SSteve Glendinning if ((val == ID_REV_CHIP_ID_9500A_) || (val == ID_REV_CHIP_ID_9530_) || 11569ebca507SSteve Glendinning (val == ID_REV_CHIP_ID_89530_) || (val == ID_REV_CHIP_ID_9730_)) 11579ebca507SSteve Glendinning pdata->features = (FEATURE_8_WAKEUP_FILTERS | 11589ebca507SSteve Glendinning FEATURE_PHY_NLP_CROSSOVER | 1159eb970ff0SMing Lei FEATURE_REMOTE_WAKEUP); 11609ebca507SSteve Glendinning else if (val == ID_REV_CHIP_ID_9512_) 11619ebca507SSteve Glendinning pdata->features = FEATURE_8_WAKEUP_FILTERS; 1162bbd9f9eeSSteve Glendinning 116363e77b39SStephen Hemminger dev->net->netdev_ops = &smsc95xx_netdev_ops; 11642f7ca802SSteve Glendinning dev->net->ethtool_ops = &smsc95xx_ethtool_ops; 11652f7ca802SSteve Glendinning dev->net->flags |= IFF_MULTICAST; 116678e47fe4SMichał Mirosław dev->net->hard_header_len += SMSC95XX_TX_OVERHEAD_CSUM; 11679bbf5660SStephane Fillod dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len; 11682f7ca802SSteve Glendinning return 0; 11692f7ca802SSteve Glendinning } 11702f7ca802SSteve Glendinning 11712f7ca802SSteve Glendinning static void smsc95xx_unbind(struct usbnet *dev, struct usb_interface *intf) 11722f7ca802SSteve Glendinning { 11732f7ca802SSteve Glendinning struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]); 11742f7ca802SSteve Glendinning if (pdata) { 1175a475f603SJoe Perches netif_dbg(dev, ifdown, dev->net, "free pdata\n"); 11762f7ca802SSteve Glendinning kfree(pdata); 11772f7ca802SSteve Glendinning pdata = NULL; 11782f7ca802SSteve Glendinning dev->data[0] = 0; 11792f7ca802SSteve Glendinning } 11802f7ca802SSteve Glendinning } 11812f7ca802SSteve Glendinning 1182068bb1a7SSteve Glendinning static u32 smsc_crc(const u8 *buffer, size_t len, int filter) 1183bbd9f9eeSSteve Glendinning { 1184068bb1a7SSteve Glendinning u32 crc = bitrev16(crc16(0xFFFF, buffer, len)); 1185068bb1a7SSteve Glendinning return crc << ((filter % 2) * 16); 1186bbd9f9eeSSteve Glendinning } 1187bbd9f9eeSSteve Glendinning 1188e5e3af83SSteve Glendinning static int smsc95xx_enable_phy_wakeup_interrupts(struct usbnet *dev, u16 mask) 1189e5e3af83SSteve Glendinning { 1190e5e3af83SSteve Glendinning struct mii_if_info *mii = &dev->mii; 1191e5e3af83SSteve Glendinning int ret; 1192e5e3af83SSteve Glendinning 11931e1d7412SJoe Perches netdev_dbg(dev->net, "enabling PHY wakeup interrupts\n"); 1194e5e3af83SSteve Glendinning 1195e5e3af83SSteve Glendinning /* read to clear */ 1196e5e3af83SSteve Glendinning ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_INT_SRC); 1197e360a8b4SSteve Glendinning if (ret < 0) 1198b052e073SSteve Glendinning return ret; 1199e5e3af83SSteve Glendinning 1200e5e3af83SSteve Glendinning /* enable interrupt source */ 1201e5e3af83SSteve Glendinning ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_INT_MASK); 1202e360a8b4SSteve Glendinning if (ret < 0) 1203b052e073SSteve Glendinning return ret; 1204e5e3af83SSteve Glendinning 1205e5e3af83SSteve Glendinning ret |= mask; 1206e5e3af83SSteve Glendinning 1207e5e3af83SSteve Glendinning smsc95xx_mdio_write_nopm(dev->net, mii->phy_id, PHY_INT_MASK, ret); 1208e5e3af83SSteve Glendinning 1209e5e3af83SSteve Glendinning return 0; 1210e5e3af83SSteve Glendinning } 1211e5e3af83SSteve Glendinning 1212e5e3af83SSteve Glendinning static int smsc95xx_link_ok_nopm(struct usbnet *dev) 1213e5e3af83SSteve Glendinning { 1214e5e3af83SSteve Glendinning struct mii_if_info *mii = &dev->mii; 1215e5e3af83SSteve Glendinning int ret; 1216e5e3af83SSteve Glendinning 1217e5e3af83SSteve Glendinning /* first, a dummy read, needed to latch some MII phys */ 1218e5e3af83SSteve Glendinning ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, MII_BMSR); 1219e360a8b4SSteve Glendinning if (ret < 0) 1220b052e073SSteve Glendinning return ret; 1221e5e3af83SSteve Glendinning 1222e5e3af83SSteve Glendinning ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, MII_BMSR); 1223e360a8b4SSteve Glendinning if (ret < 0) 1224b052e073SSteve Glendinning return ret; 1225e5e3af83SSteve Glendinning 1226e5e3af83SSteve Glendinning return !!(ret & BMSR_LSTATUS); 1227e5e3af83SSteve Glendinning } 1228e5e3af83SSteve Glendinning 1229319b95b5SSteve Glendinning static int smsc95xx_enter_suspend0(struct usbnet *dev) 1230319b95b5SSteve Glendinning { 1231319b95b5SSteve Glendinning struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]); 1232319b95b5SSteve Glendinning u32 val; 1233319b95b5SSteve Glendinning int ret; 1234319b95b5SSteve Glendinning 1235319b95b5SSteve Glendinning ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val); 1236e360a8b4SSteve Glendinning if (ret < 0) 1237b052e073SSteve Glendinning return ret; 1238319b95b5SSteve Glendinning 1239319b95b5SSteve Glendinning val &= (~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_)); 1240319b95b5SSteve Glendinning val |= PM_CTL_SUS_MODE_0; 1241319b95b5SSteve Glendinning 1242319b95b5SSteve Glendinning ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val); 1243e360a8b4SSteve Glendinning if (ret < 0) 1244b052e073SSteve Glendinning return ret; 1245319b95b5SSteve Glendinning 1246319b95b5SSteve Glendinning /* clear wol status */ 1247319b95b5SSteve Glendinning val &= ~PM_CTL_WUPS_; 1248319b95b5SSteve Glendinning val |= PM_CTL_WUPS_WOL_; 1249319b95b5SSteve Glendinning 1250319b95b5SSteve Glendinning /* enable energy detection */ 1251319b95b5SSteve Glendinning if (pdata->wolopts & WAKE_PHY) 1252319b95b5SSteve Glendinning val |= PM_CTL_WUPS_ED_; 1253319b95b5SSteve Glendinning 1254319b95b5SSteve Glendinning ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val); 1255e360a8b4SSteve Glendinning if (ret < 0) 1256b052e073SSteve Glendinning return ret; 1257319b95b5SSteve Glendinning 1258319b95b5SSteve Glendinning /* read back PM_CTRL */ 1259319b95b5SSteve Glendinning ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val); 126076437214SMing Lei if (ret < 0) 126176437214SMing Lei return ret; 1262319b95b5SSteve Glendinning 1263b2d4b150SSteve Glendinning pdata->suspend_flags |= SUSPEND_SUSPEND0; 1264b2d4b150SSteve Glendinning 126576437214SMing Lei return 0; 1266319b95b5SSteve Glendinning } 1267319b95b5SSteve Glendinning 1268319b95b5SSteve Glendinning static int smsc95xx_enter_suspend1(struct usbnet *dev) 1269319b95b5SSteve Glendinning { 1270319b95b5SSteve Glendinning struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]); 1271319b95b5SSteve Glendinning struct mii_if_info *mii = &dev->mii; 1272319b95b5SSteve Glendinning u32 val; 1273319b95b5SSteve Glendinning int ret; 1274319b95b5SSteve Glendinning 1275319b95b5SSteve Glendinning /* reconfigure link pulse detection timing for 1276319b95b5SSteve Glendinning * compatibility with non-standard link partners 1277319b95b5SSteve Glendinning */ 1278319b95b5SSteve Glendinning if (pdata->features & FEATURE_PHY_NLP_CROSSOVER) 1279319b95b5SSteve Glendinning smsc95xx_mdio_write_nopm(dev->net, mii->phy_id, PHY_EDPD_CONFIG, 1280319b95b5SSteve Glendinning PHY_EDPD_CONFIG_DEFAULT); 1281319b95b5SSteve Glendinning 1282319b95b5SSteve Glendinning /* enable energy detect power-down mode */ 1283319b95b5SSteve Glendinning ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_MODE_CTRL_STS); 1284e360a8b4SSteve Glendinning if (ret < 0) 1285b052e073SSteve Glendinning return ret; 1286319b95b5SSteve Glendinning 1287319b95b5SSteve Glendinning ret |= MODE_CTRL_STS_EDPWRDOWN_; 1288319b95b5SSteve Glendinning 1289319b95b5SSteve Glendinning smsc95xx_mdio_write_nopm(dev->net, mii->phy_id, PHY_MODE_CTRL_STS, ret); 1290319b95b5SSteve Glendinning 1291319b95b5SSteve Glendinning /* enter SUSPEND1 mode */ 1292319b95b5SSteve Glendinning ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val); 1293e360a8b4SSteve Glendinning if (ret < 0) 1294b052e073SSteve Glendinning return ret; 1295319b95b5SSteve Glendinning 1296319b95b5SSteve Glendinning val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_); 1297319b95b5SSteve Glendinning val |= PM_CTL_SUS_MODE_1; 1298319b95b5SSteve Glendinning 1299319b95b5SSteve Glendinning ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val); 1300e360a8b4SSteve Glendinning if (ret < 0) 1301b052e073SSteve Glendinning return ret; 1302319b95b5SSteve Glendinning 1303319b95b5SSteve Glendinning /* clear wol status, enable energy detection */ 1304319b95b5SSteve Glendinning val &= ~PM_CTL_WUPS_; 1305319b95b5SSteve Glendinning val |= (PM_CTL_WUPS_ED_ | PM_CTL_ED_EN_); 1306319b95b5SSteve Glendinning 1307319b95b5SSteve Glendinning ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val); 130876437214SMing Lei if (ret < 0) 130976437214SMing Lei return ret; 1310319b95b5SSteve Glendinning 1311b2d4b150SSteve Glendinning pdata->suspend_flags |= SUSPEND_SUSPEND1; 1312b2d4b150SSteve Glendinning 131376437214SMing Lei return 0; 1314319b95b5SSteve Glendinning } 1315319b95b5SSteve Glendinning 1316319b95b5SSteve Glendinning static int smsc95xx_enter_suspend2(struct usbnet *dev) 1317319b95b5SSteve Glendinning { 1318b2d4b150SSteve Glendinning struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]); 1319319b95b5SSteve Glendinning u32 val; 1320319b95b5SSteve Glendinning int ret; 1321319b95b5SSteve Glendinning 1322319b95b5SSteve Glendinning ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val); 1323e360a8b4SSteve Glendinning if (ret < 0) 1324b052e073SSteve Glendinning return ret; 1325319b95b5SSteve Glendinning 1326319b95b5SSteve Glendinning val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_); 1327319b95b5SSteve Glendinning val |= PM_CTL_SUS_MODE_2; 1328319b95b5SSteve Glendinning 1329319b95b5SSteve Glendinning ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val); 133076437214SMing Lei if (ret < 0) 133176437214SMing Lei return ret; 1332319b95b5SSteve Glendinning 1333b2d4b150SSteve Glendinning pdata->suspend_flags |= SUSPEND_SUSPEND2; 1334b2d4b150SSteve Glendinning 133576437214SMing Lei return 0; 1336319b95b5SSteve Glendinning } 1337319b95b5SSteve Glendinning 1338b2d4b150SSteve Glendinning static int smsc95xx_enter_suspend3(struct usbnet *dev) 1339b2d4b150SSteve Glendinning { 1340b2d4b150SSteve Glendinning struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]); 1341b2d4b150SSteve Glendinning u32 val; 1342b2d4b150SSteve Glendinning int ret; 1343b2d4b150SSteve Glendinning 1344b2d4b150SSteve Glendinning ret = smsc95xx_read_reg_nopm(dev, RX_FIFO_INF, &val); 1345b2d4b150SSteve Glendinning if (ret < 0) 1346b2d4b150SSteve Glendinning return ret; 1347b2d4b150SSteve Glendinning 1348b2d4b150SSteve Glendinning if (val & 0xFFFF) { 1349b2d4b150SSteve Glendinning netdev_info(dev->net, "rx fifo not empty in autosuspend\n"); 1350b2d4b150SSteve Glendinning return -EBUSY; 1351b2d4b150SSteve Glendinning } 1352b2d4b150SSteve Glendinning 1353b2d4b150SSteve Glendinning ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val); 1354b2d4b150SSteve Glendinning if (ret < 0) 1355b2d4b150SSteve Glendinning return ret; 1356b2d4b150SSteve Glendinning 1357b2d4b150SSteve Glendinning val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_); 1358b2d4b150SSteve Glendinning val |= PM_CTL_SUS_MODE_3 | PM_CTL_RES_CLR_WKP_STS; 1359b2d4b150SSteve Glendinning 1360b2d4b150SSteve Glendinning ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val); 1361b2d4b150SSteve Glendinning if (ret < 0) 1362b2d4b150SSteve Glendinning return ret; 1363b2d4b150SSteve Glendinning 1364b2d4b150SSteve Glendinning /* clear wol status */ 1365b2d4b150SSteve Glendinning val &= ~PM_CTL_WUPS_; 1366b2d4b150SSteve Glendinning val |= PM_CTL_WUPS_WOL_; 1367b2d4b150SSteve Glendinning 1368b2d4b150SSteve Glendinning ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val); 1369b2d4b150SSteve Glendinning if (ret < 0) 1370b2d4b150SSteve Glendinning return ret; 1371b2d4b150SSteve Glendinning 1372b2d4b150SSteve Glendinning pdata->suspend_flags |= SUSPEND_SUSPEND3; 1373b2d4b150SSteve Glendinning 1374b2d4b150SSteve Glendinning return 0; 1375b2d4b150SSteve Glendinning } 1376b2d4b150SSteve Glendinning 1377b2d4b150SSteve Glendinning static int smsc95xx_autosuspend(struct usbnet *dev, u32 link_up) 1378b2d4b150SSteve Glendinning { 1379b2d4b150SSteve Glendinning struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]); 1380b2d4b150SSteve Glendinning int ret; 1381b2d4b150SSteve Glendinning 1382b2d4b150SSteve Glendinning if (!netif_running(dev->net)) { 1383b2d4b150SSteve Glendinning /* interface is ifconfig down so fully power down hw */ 1384b2d4b150SSteve Glendinning netdev_dbg(dev->net, "autosuspend entering SUSPEND2\n"); 1385b2d4b150SSteve Glendinning return smsc95xx_enter_suspend2(dev); 1386b2d4b150SSteve Glendinning } 1387b2d4b150SSteve Glendinning 1388b2d4b150SSteve Glendinning if (!link_up) { 1389b2d4b150SSteve Glendinning /* link is down so enter EDPD mode, but only if device can 1390b2d4b150SSteve Glendinning * reliably resume from it. This check should be redundant 1391eb970ff0SMing Lei * as current FEATURE_REMOTE_WAKEUP parts also support 1392b2d4b150SSteve Glendinning * FEATURE_PHY_NLP_CROSSOVER but it's included for clarity */ 1393b2d4b150SSteve Glendinning if (!(pdata->features & FEATURE_PHY_NLP_CROSSOVER)) { 1394b2d4b150SSteve Glendinning netdev_warn(dev->net, "EDPD not supported\n"); 1395b2d4b150SSteve Glendinning return -EBUSY; 1396b2d4b150SSteve Glendinning } 1397b2d4b150SSteve Glendinning 1398b2d4b150SSteve Glendinning netdev_dbg(dev->net, "autosuspend entering SUSPEND1\n"); 1399b2d4b150SSteve Glendinning 1400b2d4b150SSteve Glendinning /* enable PHY wakeup events for if cable is attached */ 1401b2d4b150SSteve Glendinning ret = smsc95xx_enable_phy_wakeup_interrupts(dev, 1402b2d4b150SSteve Glendinning PHY_INT_MASK_ANEG_COMP_); 1403b2d4b150SSteve Glendinning if (ret < 0) { 1404b2d4b150SSteve Glendinning netdev_warn(dev->net, "error enabling PHY wakeup ints\n"); 1405b2d4b150SSteve Glendinning return ret; 1406b2d4b150SSteve Glendinning } 1407b2d4b150SSteve Glendinning 1408b2d4b150SSteve Glendinning netdev_info(dev->net, "entering SUSPEND1 mode\n"); 1409b2d4b150SSteve Glendinning return smsc95xx_enter_suspend1(dev); 1410b2d4b150SSteve Glendinning } 1411b2d4b150SSteve Glendinning 1412b2d4b150SSteve Glendinning /* enable PHY wakeup events so we remote wakeup if cable is pulled */ 1413b2d4b150SSteve Glendinning ret = smsc95xx_enable_phy_wakeup_interrupts(dev, 1414b2d4b150SSteve Glendinning PHY_INT_MASK_LINK_DOWN_); 1415b2d4b150SSteve Glendinning if (ret < 0) { 1416b2d4b150SSteve Glendinning netdev_warn(dev->net, "error enabling PHY wakeup ints\n"); 1417b2d4b150SSteve Glendinning return ret; 1418b2d4b150SSteve Glendinning } 1419b2d4b150SSteve Glendinning 1420b2d4b150SSteve Glendinning netdev_dbg(dev->net, "autosuspend entering SUSPEND3\n"); 1421b2d4b150SSteve Glendinning return smsc95xx_enter_suspend3(dev); 1422b2d4b150SSteve Glendinning } 1423b2d4b150SSteve Glendinning 1424b5a04475SSteve Glendinning static int smsc95xx_suspend(struct usb_interface *intf, pm_message_t message) 1425b5a04475SSteve Glendinning { 1426b5a04475SSteve Glendinning struct usbnet *dev = usb_get_intfdata(intf); 1427e0e474a8SSteve Glendinning struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]); 1428e5e3af83SSteve Glendinning u32 val, link_up; 1429b5a04475SSteve Glendinning int ret; 1430b5a04475SSteve Glendinning 1431b5a04475SSteve Glendinning ret = usbnet_suspend(intf, message); 1432b052e073SSteve Glendinning if (ret < 0) { 1433b052e073SSteve Glendinning netdev_warn(dev->net, "usbnet_suspend error\n"); 1434b052e073SSteve Glendinning return ret; 1435b052e073SSteve Glendinning } 1436b5a04475SSteve Glendinning 1437b2d4b150SSteve Glendinning if (pdata->suspend_flags) { 1438b2d4b150SSteve Glendinning netdev_warn(dev->net, "error during last resume\n"); 1439b2d4b150SSteve Glendinning pdata->suspend_flags = 0; 1440b2d4b150SSteve Glendinning } 1441b2d4b150SSteve Glendinning 1442e5e3af83SSteve Glendinning /* determine if link is up using only _nopm functions */ 1443e5e3af83SSteve Glendinning link_up = smsc95xx_link_ok_nopm(dev); 1444e5e3af83SSteve Glendinning 144542e21c01SMing Lei if (message.event == PM_EVENT_AUTO_SUSPEND && 1446eb970ff0SMing Lei (pdata->features & FEATURE_REMOTE_WAKEUP)) { 1447b2d4b150SSteve Glendinning ret = smsc95xx_autosuspend(dev, link_up); 1448b2d4b150SSteve Glendinning goto done; 1449b2d4b150SSteve Glendinning } 1450b2d4b150SSteve Glendinning 1451b2d4b150SSteve Glendinning /* if we get this far we're not autosuspending */ 1452e5e3af83SSteve Glendinning /* if no wol options set, or if link is down and we're not waking on 1453e5e3af83SSteve Glendinning * PHY activity, enter lowest power SUSPEND2 mode 1454e5e3af83SSteve Glendinning */ 1455e5e3af83SSteve Glendinning if (!(pdata->wolopts & SUPPORTED_WAKE) || 1456e5e3af83SSteve Glendinning !(link_up || (pdata->wolopts & WAKE_PHY))) { 14571e1d7412SJoe Perches netdev_info(dev->net, "entering SUSPEND2 mode\n"); 1458b5a04475SSteve Glendinning 1459e0e474a8SSteve Glendinning /* disable energy detect (link up) & wake up events */ 1460ec32115dSMing Lei ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val); 1461e360a8b4SSteve Glendinning if (ret < 0) 1462b052e073SSteve Glendinning goto done; 1463e0e474a8SSteve Glendinning 1464e0e474a8SSteve Glendinning val &= ~(WUCSR_MPEN_ | WUCSR_WAKE_EN_); 1465e0e474a8SSteve Glendinning 1466ec32115dSMing Lei ret = smsc95xx_write_reg_nopm(dev, WUCSR, val); 1467e360a8b4SSteve Glendinning if (ret < 0) 1468b052e073SSteve Glendinning goto done; 1469e0e474a8SSteve Glendinning 1470ec32115dSMing Lei ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val); 1471e360a8b4SSteve Glendinning if (ret < 0) 1472b052e073SSteve Glendinning goto done; 1473e0e474a8SSteve Glendinning 1474e0e474a8SSteve Glendinning val &= ~(PM_CTL_ED_EN_ | PM_CTL_WOL_EN_); 1475e0e474a8SSteve Glendinning 1476ec32115dSMing Lei ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val); 1477e360a8b4SSteve Glendinning if (ret < 0) 1478b052e073SSteve Glendinning goto done; 1479e0e474a8SSteve Glendinning 14803b9f7d8cSSteve Glendinning ret = smsc95xx_enter_suspend2(dev); 14813b9f7d8cSSteve Glendinning goto done; 1482b5a04475SSteve Glendinning } 1483b5a04475SSteve Glendinning 1484e5e3af83SSteve Glendinning if (pdata->wolopts & WAKE_PHY) { 1485e5e3af83SSteve Glendinning ret = smsc95xx_enable_phy_wakeup_interrupts(dev, 1486e5e3af83SSteve Glendinning (PHY_INT_MASK_ANEG_COMP_ | PHY_INT_MASK_LINK_DOWN_)); 1487b052e073SSteve Glendinning if (ret < 0) { 1488b052e073SSteve Glendinning netdev_warn(dev->net, "error enabling PHY wakeup ints\n"); 1489b052e073SSteve Glendinning goto done; 1490b052e073SSteve Glendinning } 1491e5e3af83SSteve Glendinning 1492e5e3af83SSteve Glendinning /* if link is down then configure EDPD and enter SUSPEND1, 1493e5e3af83SSteve Glendinning * otherwise enter SUSPEND0 below 1494e5e3af83SSteve Glendinning */ 1495e5e3af83SSteve Glendinning if (!link_up) { 14961e1d7412SJoe Perches netdev_info(dev->net, "entering SUSPEND1 mode\n"); 14973b9f7d8cSSteve Glendinning ret = smsc95xx_enter_suspend1(dev); 14983b9f7d8cSSteve Glendinning goto done; 1499e5e3af83SSteve Glendinning } 1500e5e3af83SSteve Glendinning } 1501e5e3af83SSteve Glendinning 1502bbd9f9eeSSteve Glendinning if (pdata->wolopts & (WAKE_BCAST | WAKE_MCAST | WAKE_ARP | WAKE_UCAST)) { 1503eed9a729SSteve Glendinning u32 *filter_mask = kzalloc(sizeof(u32) * 32, GFP_KERNEL); 150406a221beSMing Lei u32 command[2]; 150506a221beSMing Lei u32 offset[2]; 150606a221beSMing Lei u32 crc[4]; 15079ebca507SSteve Glendinning int wuff_filter_count = 15089ebca507SSteve Glendinning (pdata->features & FEATURE_8_WAKEUP_FILTERS) ? 15099ebca507SSteve Glendinning LAN9500A_WUFF_NUM : LAN9500_WUFF_NUM; 1510bbd9f9eeSSteve Glendinning int i, filter = 0; 1511bbd9f9eeSSteve Glendinning 1512eed9a729SSteve Glendinning if (!filter_mask) { 1513eed9a729SSteve Glendinning netdev_warn(dev->net, "Unable to allocate filter_mask\n"); 15143b9f7d8cSSteve Glendinning ret = -ENOMEM; 15153b9f7d8cSSteve Glendinning goto done; 1516eed9a729SSteve Glendinning } 1517eed9a729SSteve Glendinning 151806a221beSMing Lei memset(command, 0, sizeof(command)); 151906a221beSMing Lei memset(offset, 0, sizeof(offset)); 152006a221beSMing Lei memset(crc, 0, sizeof(crc)); 152106a221beSMing Lei 1522bbd9f9eeSSteve Glendinning if (pdata->wolopts & WAKE_BCAST) { 1523bbd9f9eeSSteve Glendinning const u8 bcast[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; 15241e1d7412SJoe Perches netdev_info(dev->net, "enabling broadcast detection\n"); 1525bbd9f9eeSSteve Glendinning filter_mask[filter * 4] = 0x003F; 1526bbd9f9eeSSteve Glendinning filter_mask[filter * 4 + 1] = 0x00; 1527bbd9f9eeSSteve Glendinning filter_mask[filter * 4 + 2] = 0x00; 1528bbd9f9eeSSteve Glendinning filter_mask[filter * 4 + 3] = 0x00; 1529bbd9f9eeSSteve Glendinning command[filter/4] |= 0x05UL << ((filter % 4) * 8); 1530bbd9f9eeSSteve Glendinning offset[filter/4] |= 0x00 << ((filter % 4) * 8); 1531bbd9f9eeSSteve Glendinning crc[filter/2] |= smsc_crc(bcast, 6, filter); 1532bbd9f9eeSSteve Glendinning filter++; 1533bbd9f9eeSSteve Glendinning } 1534bbd9f9eeSSteve Glendinning 1535bbd9f9eeSSteve Glendinning if (pdata->wolopts & WAKE_MCAST) { 1536bbd9f9eeSSteve Glendinning const u8 mcast[] = {0x01, 0x00, 0x5E}; 15371e1d7412SJoe Perches netdev_info(dev->net, "enabling multicast detection\n"); 1538bbd9f9eeSSteve Glendinning filter_mask[filter * 4] = 0x0007; 1539bbd9f9eeSSteve Glendinning filter_mask[filter * 4 + 1] = 0x00; 1540bbd9f9eeSSteve Glendinning filter_mask[filter * 4 + 2] = 0x00; 1541bbd9f9eeSSteve Glendinning filter_mask[filter * 4 + 3] = 0x00; 1542bbd9f9eeSSteve Glendinning command[filter/4] |= 0x09UL << ((filter % 4) * 8); 1543bbd9f9eeSSteve Glendinning offset[filter/4] |= 0x00 << ((filter % 4) * 8); 1544bbd9f9eeSSteve Glendinning crc[filter/2] |= smsc_crc(mcast, 3, filter); 1545bbd9f9eeSSteve Glendinning filter++; 1546bbd9f9eeSSteve Glendinning } 1547bbd9f9eeSSteve Glendinning 1548bbd9f9eeSSteve Glendinning if (pdata->wolopts & WAKE_ARP) { 1549bbd9f9eeSSteve Glendinning const u8 arp[] = {0x08, 0x06}; 15501e1d7412SJoe Perches netdev_info(dev->net, "enabling ARP detection\n"); 1551bbd9f9eeSSteve Glendinning filter_mask[filter * 4] = 0x0003; 1552bbd9f9eeSSteve Glendinning filter_mask[filter * 4 + 1] = 0x00; 1553bbd9f9eeSSteve Glendinning filter_mask[filter * 4 + 2] = 0x00; 1554bbd9f9eeSSteve Glendinning filter_mask[filter * 4 + 3] = 0x00; 1555bbd9f9eeSSteve Glendinning command[filter/4] |= 0x05UL << ((filter % 4) * 8); 1556bbd9f9eeSSteve Glendinning offset[filter/4] |= 0x0C << ((filter % 4) * 8); 1557bbd9f9eeSSteve Glendinning crc[filter/2] |= smsc_crc(arp, 2, filter); 1558bbd9f9eeSSteve Glendinning filter++; 1559bbd9f9eeSSteve Glendinning } 1560bbd9f9eeSSteve Glendinning 1561bbd9f9eeSSteve Glendinning if (pdata->wolopts & WAKE_UCAST) { 15621e1d7412SJoe Perches netdev_info(dev->net, "enabling unicast detection\n"); 1563bbd9f9eeSSteve Glendinning filter_mask[filter * 4] = 0x003F; 1564bbd9f9eeSSteve Glendinning filter_mask[filter * 4 + 1] = 0x00; 1565bbd9f9eeSSteve Glendinning filter_mask[filter * 4 + 2] = 0x00; 1566bbd9f9eeSSteve Glendinning filter_mask[filter * 4 + 3] = 0x00; 1567bbd9f9eeSSteve Glendinning command[filter/4] |= 0x01UL << ((filter % 4) * 8); 1568bbd9f9eeSSteve Glendinning offset[filter/4] |= 0x00 << ((filter % 4) * 8); 1569bbd9f9eeSSteve Glendinning crc[filter/2] |= smsc_crc(dev->net->dev_addr, ETH_ALEN, filter); 1570bbd9f9eeSSteve Glendinning filter++; 1571bbd9f9eeSSteve Glendinning } 1572bbd9f9eeSSteve Glendinning 15739ebca507SSteve Glendinning for (i = 0; i < (wuff_filter_count * 4); i++) { 1574ec32115dSMing Lei ret = smsc95xx_write_reg_nopm(dev, WUFF, filter_mask[i]); 1575b052e073SSteve Glendinning if (ret < 0) { 157606a221beSMing Lei kfree(filter_mask); 1577b052e073SSteve Glendinning goto done; 1578b052e073SSteve Glendinning } 1579bbd9f9eeSSteve Glendinning } 158006a221beSMing Lei kfree(filter_mask); 1581bbd9f9eeSSteve Glendinning 15829ebca507SSteve Glendinning for (i = 0; i < (wuff_filter_count / 4); i++) { 1583ec32115dSMing Lei ret = smsc95xx_write_reg_nopm(dev, WUFF, command[i]); 1584e360a8b4SSteve Glendinning if (ret < 0) 1585b052e073SSteve Glendinning goto done; 1586b052e073SSteve Glendinning } 1587bbd9f9eeSSteve Glendinning 15889ebca507SSteve Glendinning for (i = 0; i < (wuff_filter_count / 4); i++) { 1589ec32115dSMing Lei ret = smsc95xx_write_reg_nopm(dev, WUFF, offset[i]); 1590e360a8b4SSteve Glendinning if (ret < 0) 1591b052e073SSteve Glendinning goto done; 1592b052e073SSteve Glendinning } 1593bbd9f9eeSSteve Glendinning 15949ebca507SSteve Glendinning for (i = 0; i < (wuff_filter_count / 2); i++) { 1595ec32115dSMing Lei ret = smsc95xx_write_reg_nopm(dev, WUFF, crc[i]); 1596e360a8b4SSteve Glendinning if (ret < 0) 1597b052e073SSteve Glendinning goto done; 1598b052e073SSteve Glendinning } 1599bbd9f9eeSSteve Glendinning 1600bbd9f9eeSSteve Glendinning /* clear any pending pattern match packet status */ 1601ec32115dSMing Lei ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val); 1602e360a8b4SSteve Glendinning if (ret < 0) 1603b052e073SSteve Glendinning goto done; 1604bbd9f9eeSSteve Glendinning 1605bbd9f9eeSSteve Glendinning val |= WUCSR_WUFR_; 1606bbd9f9eeSSteve Glendinning 1607ec32115dSMing Lei ret = smsc95xx_write_reg_nopm(dev, WUCSR, val); 1608e360a8b4SSteve Glendinning if (ret < 0) 1609b052e073SSteve Glendinning goto done; 1610b052e073SSteve Glendinning } 1611bbd9f9eeSSteve Glendinning 1612e0e474a8SSteve Glendinning if (pdata->wolopts & WAKE_MAGIC) { 1613e0e474a8SSteve Glendinning /* clear any pending magic packet status */ 1614ec32115dSMing Lei ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val); 1615e360a8b4SSteve Glendinning if (ret < 0) 1616b052e073SSteve Glendinning goto done; 1617e0e474a8SSteve Glendinning 1618e0e474a8SSteve Glendinning val |= WUCSR_MPR_; 1619e0e474a8SSteve Glendinning 1620ec32115dSMing Lei ret = smsc95xx_write_reg_nopm(dev, WUCSR, val); 1621e360a8b4SSteve Glendinning if (ret < 0) 1622b052e073SSteve Glendinning goto done; 1623b052e073SSteve Glendinning } 1624e0e474a8SSteve Glendinning 1625bbd9f9eeSSteve Glendinning /* enable/disable wakeup sources */ 1626ec32115dSMing Lei ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val); 1627e360a8b4SSteve Glendinning if (ret < 0) 1628b052e073SSteve Glendinning goto done; 1629e0e474a8SSteve Glendinning 1630bbd9f9eeSSteve Glendinning if (pdata->wolopts & (WAKE_BCAST | WAKE_MCAST | WAKE_ARP | WAKE_UCAST)) { 16311e1d7412SJoe Perches netdev_info(dev->net, "enabling pattern match wakeup\n"); 1632bbd9f9eeSSteve Glendinning val |= WUCSR_WAKE_EN_; 1633bbd9f9eeSSteve Glendinning } else { 16341e1d7412SJoe Perches netdev_info(dev->net, "disabling pattern match wakeup\n"); 1635bbd9f9eeSSteve Glendinning val &= ~WUCSR_WAKE_EN_; 1636bbd9f9eeSSteve Glendinning } 1637bbd9f9eeSSteve Glendinning 1638e0e474a8SSteve Glendinning if (pdata->wolopts & WAKE_MAGIC) { 16391e1d7412SJoe Perches netdev_info(dev->net, "enabling magic packet wakeup\n"); 1640e0e474a8SSteve Glendinning val |= WUCSR_MPEN_; 1641e0e474a8SSteve Glendinning } else { 16421e1d7412SJoe Perches netdev_info(dev->net, "disabling magic packet wakeup\n"); 1643e0e474a8SSteve Glendinning val &= ~WUCSR_MPEN_; 1644e0e474a8SSteve Glendinning } 1645e0e474a8SSteve Glendinning 1646ec32115dSMing Lei ret = smsc95xx_write_reg_nopm(dev, WUCSR, val); 1647e360a8b4SSteve Glendinning if (ret < 0) 1648b052e073SSteve Glendinning goto done; 1649e0e474a8SSteve Glendinning 1650e0e474a8SSteve Glendinning /* enable wol wakeup source */ 1651ec32115dSMing Lei ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val); 1652e360a8b4SSteve Glendinning if (ret < 0) 1653b052e073SSteve Glendinning goto done; 1654e0e474a8SSteve Glendinning 1655e0e474a8SSteve Glendinning val |= PM_CTL_WOL_EN_; 1656e0e474a8SSteve Glendinning 1657e5e3af83SSteve Glendinning /* phy energy detect wakeup source */ 1658e5e3af83SSteve Glendinning if (pdata->wolopts & WAKE_PHY) 1659e5e3af83SSteve Glendinning val |= PM_CTL_ED_EN_; 1660e5e3af83SSteve Glendinning 1661ec32115dSMing Lei ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val); 1662e360a8b4SSteve Glendinning if (ret < 0) 1663b052e073SSteve Glendinning goto done; 1664e0e474a8SSteve Glendinning 1665bbd9f9eeSSteve Glendinning /* enable receiver to enable frame reception */ 1666ec32115dSMing Lei smsc95xx_start_rx_path(dev, 1); 1667e0e474a8SSteve Glendinning 1668e0e474a8SSteve Glendinning /* some wol options are enabled, so enter SUSPEND0 */ 16691e1d7412SJoe Perches netdev_info(dev->net, "entering SUSPEND0 mode\n"); 16703b9f7d8cSSteve Glendinning ret = smsc95xx_enter_suspend0(dev); 16713b9f7d8cSSteve Glendinning 16723b9f7d8cSSteve Glendinning done: 16730d41be53SMing Lei /* 16740d41be53SMing Lei * TODO: resume() might need to handle the suspend failure 16750d41be53SMing Lei * in system sleep 16760d41be53SMing Lei */ 16770d41be53SMing Lei if (ret && PMSG_IS_AUTO(message)) 16783b9f7d8cSSteve Glendinning usbnet_resume(intf); 16793b9f7d8cSSteve Glendinning return ret; 1680e0e474a8SSteve Glendinning } 1681e0e474a8SSteve Glendinning 1682e0e474a8SSteve Glendinning static int smsc95xx_resume(struct usb_interface *intf) 1683e0e474a8SSteve Glendinning { 1684e0e474a8SSteve Glendinning struct usbnet *dev = usb_get_intfdata(intf); 16858bca81d9SSudip Mukherjee struct smsc95xx_priv *pdata; 16868bca81d9SSudip Mukherjee u8 suspend_flags; 1687e0e474a8SSteve Glendinning int ret; 1688e0e474a8SSteve Glendinning u32 val; 1689e0e474a8SSteve Glendinning 1690e0e474a8SSteve Glendinning BUG_ON(!dev); 16918bca81d9SSudip Mukherjee pdata = (struct smsc95xx_priv *)(dev->data[0]); 16928bca81d9SSudip Mukherjee suspend_flags = pdata->suspend_flags; 1693e0e474a8SSteve Glendinning 1694b2d4b150SSteve Glendinning netdev_dbg(dev->net, "resume suspend_flags=0x%02x\n", suspend_flags); 1695b2d4b150SSteve Glendinning 1696b2d4b150SSteve Glendinning /* do this first to ensure it's cleared even in error case */ 1697b2d4b150SSteve Glendinning pdata->suspend_flags = 0; 1698b2d4b150SSteve Glendinning 1699b2d4b150SSteve Glendinning if (suspend_flags & SUSPEND_ALLMODES) { 1700bbd9f9eeSSteve Glendinning /* clear wake-up sources */ 1701ec32115dSMing Lei ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val); 1702e360a8b4SSteve Glendinning if (ret < 0) 1703b052e073SSteve Glendinning return ret; 1704e0e474a8SSteve Glendinning 1705bbd9f9eeSSteve Glendinning val &= ~(WUCSR_WAKE_EN_ | WUCSR_MPEN_); 1706e0e474a8SSteve Glendinning 1707ec32115dSMing Lei ret = smsc95xx_write_reg_nopm(dev, WUCSR, val); 1708e360a8b4SSteve Glendinning if (ret < 0) 1709b052e073SSteve Glendinning return ret; 1710e0e474a8SSteve Glendinning 1711e0e474a8SSteve Glendinning /* clear wake-up status */ 1712ec32115dSMing Lei ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val); 1713e360a8b4SSteve Glendinning if (ret < 0) 1714b052e073SSteve Glendinning return ret; 1715e0e474a8SSteve Glendinning 1716e0e474a8SSteve Glendinning val &= ~PM_CTL_WOL_EN_; 1717e0e474a8SSteve Glendinning val |= PM_CTL_WUPS_; 1718e0e474a8SSteve Glendinning 1719ec32115dSMing Lei ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val); 1720e360a8b4SSteve Glendinning if (ret < 0) 1721b052e073SSteve Glendinning return ret; 1722b052e073SSteve Glendinning } 1723e0e474a8SSteve Glendinning 1724af3d7c1eSSteve Glendinning ret = usbnet_resume(intf); 1725b052e073SSteve Glendinning if (ret < 0) 1726b052e073SSteve Glendinning netdev_warn(dev->net, "usbnet_resume error\n"); 1727e0e474a8SSteve Glendinning 1728b052e073SSteve Glendinning return ret; 1729e0e474a8SSteve Glendinning } 1730e0e474a8SSteve Glendinning 1731b4df480fSJoonyoung Shim static int smsc95xx_reset_resume(struct usb_interface *intf) 1732b4df480fSJoonyoung Shim { 1733b4df480fSJoonyoung Shim struct usbnet *dev = usb_get_intfdata(intf); 1734b4df480fSJoonyoung Shim int ret; 1735b4df480fSJoonyoung Shim 1736b4df480fSJoonyoung Shim ret = smsc95xx_reset(dev); 1737b4df480fSJoonyoung Shim if (ret < 0) 1738b4df480fSJoonyoung Shim return ret; 1739b4df480fSJoonyoung Shim 1740b4df480fSJoonyoung Shim return smsc95xx_resume(intf); 1741b4df480fSJoonyoung Shim } 1742b4df480fSJoonyoung Shim 17432f7ca802SSteve Glendinning static void smsc95xx_rx_csum_offload(struct sk_buff *skb) 17442f7ca802SSteve Glendinning { 17452f7ca802SSteve Glendinning skb->csum = *(u16 *)(skb_tail_pointer(skb) - 2); 17462f7ca802SSteve Glendinning skb->ip_summed = CHECKSUM_COMPLETE; 17472f7ca802SSteve Glendinning skb_trim(skb, skb->len - 2); 17482f7ca802SSteve Glendinning } 17492f7ca802SSteve Glendinning 17502f7ca802SSteve Glendinning static int smsc95xx_rx_fixup(struct usbnet *dev, struct sk_buff *skb) 17512f7ca802SSteve Glendinning { 1752eb85569fSEmil Goode /* This check is no longer done by usbnet */ 1753eb85569fSEmil Goode if (skb->len < dev->net->hard_header_len) 1754eb85569fSEmil Goode return 0; 1755eb85569fSEmil Goode 17562f7ca802SSteve Glendinning while (skb->len > 0) { 17572f7ca802SSteve Glendinning u32 header, align_count; 17582f7ca802SSteve Glendinning struct sk_buff *ax_skb; 17592f7ca802SSteve Glendinning unsigned char *packet; 17602f7ca802SSteve Glendinning u16 size; 17612f7ca802SSteve Glendinning 17622f7ca802SSteve Glendinning memcpy(&header, skb->data, sizeof(header)); 17632f7ca802SSteve Glendinning le32_to_cpus(&header); 17642f7ca802SSteve Glendinning skb_pull(skb, 4 + NET_IP_ALIGN); 17652f7ca802SSteve Glendinning packet = skb->data; 17662f7ca802SSteve Glendinning 17672f7ca802SSteve Glendinning /* get the packet length */ 17682f7ca802SSteve Glendinning size = (u16)((header & RX_STS_FL_) >> 16); 17692f7ca802SSteve Glendinning align_count = (4 - ((size + NET_IP_ALIGN) % 4)) % 4; 17702f7ca802SSteve Glendinning 17712f7ca802SSteve Glendinning if (unlikely(header & RX_STS_ES_)) { 1772a475f603SJoe Perches netif_dbg(dev, rx_err, dev->net, 1773a475f603SJoe Perches "Error header=0x%08x\n", header); 177480667ac1SHerbert Xu dev->net->stats.rx_errors++; 177580667ac1SHerbert Xu dev->net->stats.rx_dropped++; 17762f7ca802SSteve Glendinning 17772f7ca802SSteve Glendinning if (header & RX_STS_CRC_) { 177880667ac1SHerbert Xu dev->net->stats.rx_crc_errors++; 17792f7ca802SSteve Glendinning } else { 17802f7ca802SSteve Glendinning if (header & (RX_STS_TL_ | RX_STS_RF_)) 178180667ac1SHerbert Xu dev->net->stats.rx_frame_errors++; 17822f7ca802SSteve Glendinning 17832f7ca802SSteve Glendinning if ((header & RX_STS_LE_) && 17842f7ca802SSteve Glendinning (!(header & RX_STS_FT_))) 178580667ac1SHerbert Xu dev->net->stats.rx_length_errors++; 17862f7ca802SSteve Glendinning } 17872f7ca802SSteve Glendinning } else { 17882f7ca802SSteve Glendinning /* ETH_FRAME_LEN + 4(CRC) + 2(COE) + 4(Vlan) */ 17892f7ca802SSteve Glendinning if (unlikely(size > (ETH_FRAME_LEN + 12))) { 1790a475f603SJoe Perches netif_dbg(dev, rx_err, dev->net, 1791a475f603SJoe Perches "size err header=0x%08x\n", header); 17922f7ca802SSteve Glendinning return 0; 17932f7ca802SSteve Glendinning } 17942f7ca802SSteve Glendinning 17952f7ca802SSteve Glendinning /* last frame in this batch */ 17962f7ca802SSteve Glendinning if (skb->len == size) { 179778e47fe4SMichał Mirosław if (dev->net->features & NETIF_F_RXCSUM) 17982f7ca802SSteve Glendinning smsc95xx_rx_csum_offload(skb); 1799df18accaSPeter Korsgaard skb_trim(skb, skb->len - 4); /* remove fcs */ 18002f7ca802SSteve Glendinning skb->truesize = size + sizeof(struct sk_buff); 18012f7ca802SSteve Glendinning 18022f7ca802SSteve Glendinning return 1; 18032f7ca802SSteve Glendinning } 18042f7ca802SSteve Glendinning 18052f7ca802SSteve Glendinning ax_skb = skb_clone(skb, GFP_ATOMIC); 18062f7ca802SSteve Glendinning if (unlikely(!ax_skb)) { 180760b86755SJoe Perches netdev_warn(dev->net, "Error allocating skb\n"); 18082f7ca802SSteve Glendinning return 0; 18092f7ca802SSteve Glendinning } 18102f7ca802SSteve Glendinning 18112f7ca802SSteve Glendinning ax_skb->len = size; 18122f7ca802SSteve Glendinning ax_skb->data = packet; 18132f7ca802SSteve Glendinning skb_set_tail_pointer(ax_skb, size); 18142f7ca802SSteve Glendinning 181578e47fe4SMichał Mirosław if (dev->net->features & NETIF_F_RXCSUM) 18162f7ca802SSteve Glendinning smsc95xx_rx_csum_offload(ax_skb); 1817df18accaSPeter Korsgaard skb_trim(ax_skb, ax_skb->len - 4); /* remove fcs */ 18182f7ca802SSteve Glendinning ax_skb->truesize = size + sizeof(struct sk_buff); 18192f7ca802SSteve Glendinning 18202f7ca802SSteve Glendinning usbnet_skb_return(dev, ax_skb); 18212f7ca802SSteve Glendinning } 18222f7ca802SSteve Glendinning 18232f7ca802SSteve Glendinning skb_pull(skb, size); 18242f7ca802SSteve Glendinning 18252f7ca802SSteve Glendinning /* padding bytes before the next frame starts */ 18262f7ca802SSteve Glendinning if (skb->len) 18272f7ca802SSteve Glendinning skb_pull(skb, align_count); 18282f7ca802SSteve Glendinning } 18292f7ca802SSteve Glendinning 18302f7ca802SSteve Glendinning return 1; 18312f7ca802SSteve Glendinning } 18322f7ca802SSteve Glendinning 1833f7b29271SSteve Glendinning static u32 smsc95xx_calc_csum_preamble(struct sk_buff *skb) 1834f7b29271SSteve Glendinning { 183555508d60SMichał Mirosław u16 low_16 = (u16)skb_checksum_start_offset(skb); 183655508d60SMichał Mirosław u16 high_16 = low_16 + skb->csum_offset; 1837f7b29271SSteve Glendinning return (high_16 << 16) | low_16; 1838f7b29271SSteve Glendinning } 1839f7b29271SSteve Glendinning 18402f7ca802SSteve Glendinning static struct sk_buff *smsc95xx_tx_fixup(struct usbnet *dev, 18412f7ca802SSteve Glendinning struct sk_buff *skb, gfp_t flags) 18422f7ca802SSteve Glendinning { 184378e47fe4SMichał Mirosław bool csum = skb->ip_summed == CHECKSUM_PARTIAL; 1844f7b29271SSteve Glendinning int overhead = csum ? SMSC95XX_TX_OVERHEAD_CSUM : SMSC95XX_TX_OVERHEAD; 18452f7ca802SSteve Glendinning u32 tx_cmd_a, tx_cmd_b; 18462f7ca802SSteve Glendinning 1847f7b29271SSteve Glendinning /* We do not advertise SG, so skbs should be already linearized */ 1848f7b29271SSteve Glendinning BUG_ON(skb_shinfo(skb)->nr_frags); 1849f7b29271SSteve Glendinning 1850f7b29271SSteve Glendinning if (skb_headroom(skb) < overhead) { 18512f7ca802SSteve Glendinning struct sk_buff *skb2 = skb_copy_expand(skb, 1852f7b29271SSteve Glendinning overhead, 0, flags); 18532f7ca802SSteve Glendinning dev_kfree_skb_any(skb); 18542f7ca802SSteve Glendinning skb = skb2; 18552f7ca802SSteve Glendinning if (!skb) 18562f7ca802SSteve Glendinning return NULL; 18572f7ca802SSteve Glendinning } 18582f7ca802SSteve Glendinning 1859f7b29271SSteve Glendinning if (csum) { 186011bc3088SSteve Glendinning if (skb->len <= 45) { 186111bc3088SSteve Glendinning /* workaround - hardware tx checksum does not work 186211bc3088SSteve Glendinning * properly with extremely small packets */ 186355508d60SMichał Mirosław long csstart = skb_checksum_start_offset(skb); 186411bc3088SSteve Glendinning __wsum calc = csum_partial(skb->data + csstart, 186511bc3088SSteve Glendinning skb->len - csstart, 0); 186611bc3088SSteve Glendinning *((__sum16 *)(skb->data + csstart 186711bc3088SSteve Glendinning + skb->csum_offset)) = csum_fold(calc); 186811bc3088SSteve Glendinning 186911bc3088SSteve Glendinning csum = false; 187011bc3088SSteve Glendinning } else { 1871f7b29271SSteve Glendinning u32 csum_preamble = smsc95xx_calc_csum_preamble(skb); 1872f7b29271SSteve Glendinning skb_push(skb, 4); 187300acda68SSteve Glendinning cpu_to_le32s(&csum_preamble); 1874f7b29271SSteve Glendinning memcpy(skb->data, &csum_preamble, 4); 1875f7b29271SSteve Glendinning } 187611bc3088SSteve Glendinning } 1877f7b29271SSteve Glendinning 18782f7ca802SSteve Glendinning skb_push(skb, 4); 18792f7ca802SSteve Glendinning tx_cmd_b = (u32)(skb->len - 4); 1880f7b29271SSteve Glendinning if (csum) 1881f7b29271SSteve Glendinning tx_cmd_b |= TX_CMD_B_CSUM_ENABLE; 18822f7ca802SSteve Glendinning cpu_to_le32s(&tx_cmd_b); 18832f7ca802SSteve Glendinning memcpy(skb->data, &tx_cmd_b, 4); 18842f7ca802SSteve Glendinning 18852f7ca802SSteve Glendinning skb_push(skb, 4); 18862f7ca802SSteve Glendinning tx_cmd_a = (u32)(skb->len - 8) | TX_CMD_A_FIRST_SEG_ | 18872f7ca802SSteve Glendinning TX_CMD_A_LAST_SEG_; 18882f7ca802SSteve Glendinning cpu_to_le32s(&tx_cmd_a); 18892f7ca802SSteve Glendinning memcpy(skb->data, &tx_cmd_a, 4); 18902f7ca802SSteve Glendinning 18912f7ca802SSteve Glendinning return skb; 18922f7ca802SSteve Glendinning } 18932f7ca802SSteve Glendinning 1894b2d4b150SSteve Glendinning static int smsc95xx_manage_power(struct usbnet *dev, int on) 1895b2d4b150SSteve Glendinning { 1896b2d4b150SSteve Glendinning struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]); 1897b2d4b150SSteve Glendinning 1898b2d4b150SSteve Glendinning dev->intf->needs_remote_wakeup = on; 1899b2d4b150SSteve Glendinning 1900eb970ff0SMing Lei if (pdata->features & FEATURE_REMOTE_WAKEUP) 1901b2d4b150SSteve Glendinning return 0; 1902b2d4b150SSteve Glendinning 1903eb970ff0SMing Lei /* this chip revision isn't capable of remote wakeup */ 1904eb970ff0SMing Lei netdev_info(dev->net, "hardware isn't capable of remote wakeup\n"); 1905b2d4b150SSteve Glendinning 1906b2d4b150SSteve Glendinning if (on) 1907b2d4b150SSteve Glendinning usb_autopm_get_interface_no_resume(dev->intf); 1908b2d4b150SSteve Glendinning else 1909b2d4b150SSteve Glendinning usb_autopm_put_interface(dev->intf); 1910b2d4b150SSteve Glendinning 1911b2d4b150SSteve Glendinning return 0; 1912b2d4b150SSteve Glendinning } 1913b2d4b150SSteve Glendinning 19142f7ca802SSteve Glendinning static const struct driver_info smsc95xx_info = { 19152f7ca802SSteve Glendinning .description = "smsc95xx USB 2.0 Ethernet", 19162f7ca802SSteve Glendinning .bind = smsc95xx_bind, 19172f7ca802SSteve Glendinning .unbind = smsc95xx_unbind, 19182f7ca802SSteve Glendinning .link_reset = smsc95xx_link_reset, 19192f7ca802SSteve Glendinning .reset = smsc95xx_reset, 19202f7ca802SSteve Glendinning .rx_fixup = smsc95xx_rx_fixup, 19212f7ca802SSteve Glendinning .tx_fixup = smsc95xx_tx_fixup, 19222f7ca802SSteve Glendinning .status = smsc95xx_status, 1923b2d4b150SSteve Glendinning .manage_power = smsc95xx_manage_power, 192407d69d42SPaolo Pisati .flags = FLAG_ETHER | FLAG_SEND_ZLP | FLAG_LINK_INTR, 19252f7ca802SSteve Glendinning }; 19262f7ca802SSteve Glendinning 19272f7ca802SSteve Glendinning static const struct usb_device_id products[] = { 19282f7ca802SSteve Glendinning { 19292f7ca802SSteve Glendinning /* SMSC9500 USB Ethernet Device */ 19302f7ca802SSteve Glendinning USB_DEVICE(0x0424, 0x9500), 19312f7ca802SSteve Glendinning .driver_info = (unsigned long) &smsc95xx_info, 19322f7ca802SSteve Glendinning }, 1933726474b8SSteve Glendinning { 19346f41d12bSSteve Glendinning /* SMSC9505 USB Ethernet Device */ 19356f41d12bSSteve Glendinning USB_DEVICE(0x0424, 0x9505), 19366f41d12bSSteve Glendinning .driver_info = (unsigned long) &smsc95xx_info, 19376f41d12bSSteve Glendinning }, 19386f41d12bSSteve Glendinning { 19396f41d12bSSteve Glendinning /* SMSC9500A USB Ethernet Device */ 19406f41d12bSSteve Glendinning USB_DEVICE(0x0424, 0x9E00), 19416f41d12bSSteve Glendinning .driver_info = (unsigned long) &smsc95xx_info, 19426f41d12bSSteve Glendinning }, 19436f41d12bSSteve Glendinning { 19446f41d12bSSteve Glendinning /* SMSC9505A USB Ethernet Device */ 19456f41d12bSSteve Glendinning USB_DEVICE(0x0424, 0x9E01), 19466f41d12bSSteve Glendinning .driver_info = (unsigned long) &smsc95xx_info, 19476f41d12bSSteve Glendinning }, 19486f41d12bSSteve Glendinning { 1949726474b8SSteve Glendinning /* SMSC9512/9514 USB Hub & Ethernet Device */ 1950726474b8SSteve Glendinning USB_DEVICE(0x0424, 0xec00), 1951726474b8SSteve Glendinning .driver_info = (unsigned long) &smsc95xx_info, 1952726474b8SSteve Glendinning }, 19536f41d12bSSteve Glendinning { 19546f41d12bSSteve Glendinning /* SMSC9500 USB Ethernet Device (SAL10) */ 19556f41d12bSSteve Glendinning USB_DEVICE(0x0424, 0x9900), 19566f41d12bSSteve Glendinning .driver_info = (unsigned long) &smsc95xx_info, 19576f41d12bSSteve Glendinning }, 19586f41d12bSSteve Glendinning { 19596f41d12bSSteve Glendinning /* SMSC9505 USB Ethernet Device (SAL10) */ 19606f41d12bSSteve Glendinning USB_DEVICE(0x0424, 0x9901), 19616f41d12bSSteve Glendinning .driver_info = (unsigned long) &smsc95xx_info, 19626f41d12bSSteve Glendinning }, 19636f41d12bSSteve Glendinning { 19646f41d12bSSteve Glendinning /* SMSC9500A USB Ethernet Device (SAL10) */ 19656f41d12bSSteve Glendinning USB_DEVICE(0x0424, 0x9902), 19666f41d12bSSteve Glendinning .driver_info = (unsigned long) &smsc95xx_info, 19676f41d12bSSteve Glendinning }, 19686f41d12bSSteve Glendinning { 19696f41d12bSSteve Glendinning /* SMSC9505A USB Ethernet Device (SAL10) */ 19706f41d12bSSteve Glendinning USB_DEVICE(0x0424, 0x9903), 19716f41d12bSSteve Glendinning .driver_info = (unsigned long) &smsc95xx_info, 19726f41d12bSSteve Glendinning }, 19736f41d12bSSteve Glendinning { 19746f41d12bSSteve Glendinning /* SMSC9512/9514 USB Hub & Ethernet Device (SAL10) */ 19756f41d12bSSteve Glendinning USB_DEVICE(0x0424, 0x9904), 19766f41d12bSSteve Glendinning .driver_info = (unsigned long) &smsc95xx_info, 19776f41d12bSSteve Glendinning }, 19786f41d12bSSteve Glendinning { 19796f41d12bSSteve Glendinning /* SMSC9500A USB Ethernet Device (HAL) */ 19806f41d12bSSteve Glendinning USB_DEVICE(0x0424, 0x9905), 19816f41d12bSSteve Glendinning .driver_info = (unsigned long) &smsc95xx_info, 19826f41d12bSSteve Glendinning }, 19836f41d12bSSteve Glendinning { 19846f41d12bSSteve Glendinning /* SMSC9505A USB Ethernet Device (HAL) */ 19856f41d12bSSteve Glendinning USB_DEVICE(0x0424, 0x9906), 19866f41d12bSSteve Glendinning .driver_info = (unsigned long) &smsc95xx_info, 19876f41d12bSSteve Glendinning }, 19886f41d12bSSteve Glendinning { 19896f41d12bSSteve Glendinning /* SMSC9500 USB Ethernet Device (Alternate ID) */ 19906f41d12bSSteve Glendinning USB_DEVICE(0x0424, 0x9907), 19916f41d12bSSteve Glendinning .driver_info = (unsigned long) &smsc95xx_info, 19926f41d12bSSteve Glendinning }, 19936f41d12bSSteve Glendinning { 19946f41d12bSSteve Glendinning /* SMSC9500A USB Ethernet Device (Alternate ID) */ 19956f41d12bSSteve Glendinning USB_DEVICE(0x0424, 0x9908), 19966f41d12bSSteve Glendinning .driver_info = (unsigned long) &smsc95xx_info, 19976f41d12bSSteve Glendinning }, 19986f41d12bSSteve Glendinning { 19996f41d12bSSteve Glendinning /* SMSC9512/9514 USB Hub & Ethernet Device (Alternate ID) */ 20006f41d12bSSteve Glendinning USB_DEVICE(0x0424, 0x9909), 20016f41d12bSSteve Glendinning .driver_info = (unsigned long) &smsc95xx_info, 20026f41d12bSSteve Glendinning }, 200388edaa41SSteve Glendinning { 200488edaa41SSteve Glendinning /* SMSC LAN9530 USB Ethernet Device */ 200588edaa41SSteve Glendinning USB_DEVICE(0x0424, 0x9530), 200688edaa41SSteve Glendinning .driver_info = (unsigned long) &smsc95xx_info, 200788edaa41SSteve Glendinning }, 200888edaa41SSteve Glendinning { 200988edaa41SSteve Glendinning /* SMSC LAN9730 USB Ethernet Device */ 201088edaa41SSteve Glendinning USB_DEVICE(0x0424, 0x9730), 201188edaa41SSteve Glendinning .driver_info = (unsigned long) &smsc95xx_info, 201288edaa41SSteve Glendinning }, 201388edaa41SSteve Glendinning { 201488edaa41SSteve Glendinning /* SMSC LAN89530 USB Ethernet Device */ 201588edaa41SSteve Glendinning USB_DEVICE(0x0424, 0x9E08), 201688edaa41SSteve Glendinning .driver_info = (unsigned long) &smsc95xx_info, 201788edaa41SSteve Glendinning }, 20182f7ca802SSteve Glendinning { }, /* END */ 20192f7ca802SSteve Glendinning }; 20202f7ca802SSteve Glendinning MODULE_DEVICE_TABLE(usb, products); 20212f7ca802SSteve Glendinning 20222f7ca802SSteve Glendinning static struct usb_driver smsc95xx_driver = { 20232f7ca802SSteve Glendinning .name = "smsc95xx", 20242f7ca802SSteve Glendinning .id_table = products, 20252f7ca802SSteve Glendinning .probe = usbnet_probe, 2026b5a04475SSteve Glendinning .suspend = smsc95xx_suspend, 2027e0e474a8SSteve Glendinning .resume = smsc95xx_resume, 2028b4df480fSJoonyoung Shim .reset_resume = smsc95xx_reset_resume, 20292f7ca802SSteve Glendinning .disconnect = usbnet_disconnect, 2030e1f12eb6SSarah Sharp .disable_hub_initiated_lpm = 1, 2031b2d4b150SSteve Glendinning .supports_autosuspend = 1, 20322f7ca802SSteve Glendinning }; 20332f7ca802SSteve Glendinning 2034d632eb1bSGreg Kroah-Hartman module_usb_driver(smsc95xx_driver); 20352f7ca802SSteve Glendinning 20362f7ca802SSteve Glendinning MODULE_AUTHOR("Nancy Lin"); 203790b24cfbSSteve Glendinning MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>"); 20382f7ca802SSteve Glendinning MODULE_DESCRIPTION("SMSC95XX USB 2.0 Ethernet Devices"); 20392f7ca802SSteve Glendinning MODULE_LICENSE("GPL"); 2040