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 162f7ca802SSteve Glendinning * along with this program; if not, write to the Free Software 172f7ca802SSteve Glendinning * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 182f7ca802SSteve Glendinning * 192f7ca802SSteve Glendinning *****************************************************************************/ 202f7ca802SSteve Glendinning 212f7ca802SSteve Glendinning #include <linux/module.h> 222f7ca802SSteve Glendinning #include <linux/kmod.h> 232f7ca802SSteve Glendinning #include <linux/init.h> 242f7ca802SSteve Glendinning #include <linux/netdevice.h> 252f7ca802SSteve Glendinning #include <linux/etherdevice.h> 262f7ca802SSteve Glendinning #include <linux/ethtool.h> 272f7ca802SSteve Glendinning #include <linux/mii.h> 282f7ca802SSteve Glendinning #include <linux/usb.h> 292f7ca802SSteve Glendinning #include <linux/crc32.h> 302f7ca802SSteve Glendinning #include <linux/usb/usbnet.h> 315a0e3ad6STejun Heo #include <linux/slab.h> 322f7ca802SSteve Glendinning #include "smsc95xx.h" 332f7ca802SSteve Glendinning 342f7ca802SSteve Glendinning #define SMSC_CHIPNAME "smsc95xx" 35f7b29271SSteve Glendinning #define SMSC_DRIVER_VERSION "1.0.4" 362f7ca802SSteve Glendinning #define HS_USB_PKT_SIZE (512) 372f7ca802SSteve Glendinning #define FS_USB_PKT_SIZE (64) 382f7ca802SSteve Glendinning #define DEFAULT_HS_BURST_CAP_SIZE (16 * 1024 + 5 * HS_USB_PKT_SIZE) 392f7ca802SSteve Glendinning #define DEFAULT_FS_BURST_CAP_SIZE (6 * 1024 + 33 * FS_USB_PKT_SIZE) 402f7ca802SSteve Glendinning #define DEFAULT_BULK_IN_DELAY (0x00002000) 412f7ca802SSteve Glendinning #define MAX_SINGLE_PACKET_SIZE (2048) 422f7ca802SSteve Glendinning #define LAN95XX_EEPROM_MAGIC (0x9500) 432f7ca802SSteve Glendinning #define EEPROM_MAC_OFFSET (0x01) 44f7b29271SSteve Glendinning #define DEFAULT_TX_CSUM_ENABLE (true) 452f7ca802SSteve Glendinning #define DEFAULT_RX_CSUM_ENABLE (true) 462f7ca802SSteve Glendinning #define SMSC95XX_INTERNAL_PHY_ID (1) 472f7ca802SSteve Glendinning #define SMSC95XX_TX_OVERHEAD (8) 48f7b29271SSteve Glendinning #define SMSC95XX_TX_OVERHEAD_CSUM (12) 492f7ca802SSteve Glendinning 502f7ca802SSteve Glendinning struct smsc95xx_priv { 512f7ca802SSteve Glendinning u32 mac_cr; 523c0f3c60SMarc Zyngier u32 hash_hi; 533c0f3c60SMarc Zyngier u32 hash_lo; 542f7ca802SSteve Glendinning spinlock_t mac_cr_lock; 552f7ca802SSteve Glendinning }; 562f7ca802SSteve Glendinning 572f7ca802SSteve Glendinning struct usb_context { 582f7ca802SSteve Glendinning struct usb_ctrlrequest req; 592f7ca802SSteve Glendinning struct usbnet *dev; 602f7ca802SSteve Glendinning }; 612f7ca802SSteve Glendinning 62eb939922SRusty Russell static bool turbo_mode = true; 632f7ca802SSteve Glendinning module_param(turbo_mode, bool, 0644); 642f7ca802SSteve Glendinning MODULE_PARM_DESC(turbo_mode, "Enable multiple frames per Rx transaction"); 652f7ca802SSteve Glendinning 662f7ca802SSteve Glendinning static int smsc95xx_read_reg(struct usbnet *dev, u32 index, u32 *data) 672f7ca802SSteve Glendinning { 682f7ca802SSteve Glendinning u32 *buf = kmalloc(4, GFP_KERNEL); 692f7ca802SSteve Glendinning int ret; 702f7ca802SSteve Glendinning 712f7ca802SSteve Glendinning BUG_ON(!dev); 722f7ca802SSteve Glendinning 732f7ca802SSteve Glendinning if (!buf) 742f7ca802SSteve Glendinning return -ENOMEM; 752f7ca802SSteve Glendinning 762f7ca802SSteve Glendinning ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0), 772f7ca802SSteve Glendinning USB_VENDOR_REQUEST_READ_REGISTER, 782f7ca802SSteve Glendinning USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 792f7ca802SSteve Glendinning 00, index, buf, 4, USB_CTRL_GET_TIMEOUT); 802f7ca802SSteve Glendinning 812f7ca802SSteve Glendinning if (unlikely(ret < 0)) 8260b86755SJoe Perches netdev_warn(dev->net, "Failed to read register index 0x%08x\n", index); 832f7ca802SSteve Glendinning 842f7ca802SSteve Glendinning le32_to_cpus(buf); 852f7ca802SSteve Glendinning *data = *buf; 862f7ca802SSteve Glendinning kfree(buf); 872f7ca802SSteve Glendinning 882f7ca802SSteve Glendinning return ret; 892f7ca802SSteve Glendinning } 902f7ca802SSteve Glendinning 912f7ca802SSteve Glendinning static int smsc95xx_write_reg(struct usbnet *dev, u32 index, u32 data) 922f7ca802SSteve Glendinning { 932f7ca802SSteve Glendinning u32 *buf = kmalloc(4, GFP_KERNEL); 942f7ca802SSteve Glendinning int ret; 952f7ca802SSteve Glendinning 962f7ca802SSteve Glendinning BUG_ON(!dev); 972f7ca802SSteve Glendinning 982f7ca802SSteve Glendinning if (!buf) 992f7ca802SSteve Glendinning return -ENOMEM; 1002f7ca802SSteve Glendinning 1012f7ca802SSteve Glendinning *buf = data; 1022f7ca802SSteve Glendinning cpu_to_le32s(buf); 1032f7ca802SSteve Glendinning 1042f7ca802SSteve Glendinning ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0), 1052f7ca802SSteve Glendinning USB_VENDOR_REQUEST_WRITE_REGISTER, 1062f7ca802SSteve Glendinning USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 1072f7ca802SSteve Glendinning 00, index, buf, 4, USB_CTRL_SET_TIMEOUT); 1082f7ca802SSteve Glendinning 1092f7ca802SSteve Glendinning if (unlikely(ret < 0)) 11060b86755SJoe Perches netdev_warn(dev->net, "Failed to write register index 0x%08x\n", index); 1112f7ca802SSteve Glendinning 1122f7ca802SSteve Glendinning kfree(buf); 1132f7ca802SSteve Glendinning 1142f7ca802SSteve Glendinning return ret; 1152f7ca802SSteve Glendinning } 1162f7ca802SSteve Glendinning 1172f7ca802SSteve Glendinning /* Loop until the read is completed with timeout 1182f7ca802SSteve Glendinning * called with phy_mutex held */ 1192f7ca802SSteve Glendinning static int smsc95xx_phy_wait_not_busy(struct usbnet *dev) 1202f7ca802SSteve Glendinning { 1212f7ca802SSteve Glendinning unsigned long start_time = jiffies; 1222f7ca802SSteve Glendinning u32 val; 1232f7ca802SSteve Glendinning 1242f7ca802SSteve Glendinning do { 1252f7ca802SSteve Glendinning smsc95xx_read_reg(dev, MII_ADDR, &val); 1262f7ca802SSteve Glendinning if (!(val & MII_BUSY_)) 1272f7ca802SSteve Glendinning return 0; 1282f7ca802SSteve Glendinning } while (!time_after(jiffies, start_time + HZ)); 1292f7ca802SSteve Glendinning 1302f7ca802SSteve Glendinning return -EIO; 1312f7ca802SSteve Glendinning } 1322f7ca802SSteve Glendinning 1332f7ca802SSteve Glendinning static int smsc95xx_mdio_read(struct net_device *netdev, int phy_id, int idx) 1342f7ca802SSteve Glendinning { 1352f7ca802SSteve Glendinning struct usbnet *dev = netdev_priv(netdev); 1362f7ca802SSteve Glendinning u32 val, addr; 1372f7ca802SSteve Glendinning 1382f7ca802SSteve Glendinning mutex_lock(&dev->phy_mutex); 1392f7ca802SSteve Glendinning 1402f7ca802SSteve Glendinning /* confirm MII not busy */ 1412f7ca802SSteve Glendinning if (smsc95xx_phy_wait_not_busy(dev)) { 14260b86755SJoe Perches netdev_warn(dev->net, "MII is busy in smsc95xx_mdio_read\n"); 1432f7ca802SSteve Glendinning mutex_unlock(&dev->phy_mutex); 1442f7ca802SSteve Glendinning return -EIO; 1452f7ca802SSteve Glendinning } 1462f7ca802SSteve Glendinning 1472f7ca802SSteve Glendinning /* set the address, index & direction (read from PHY) */ 1482f7ca802SSteve Glendinning phy_id &= dev->mii.phy_id_mask; 1492f7ca802SSteve Glendinning idx &= dev->mii.reg_num_mask; 1502f7ca802SSteve Glendinning addr = (phy_id << 11) | (idx << 6) | MII_READ_; 1512f7ca802SSteve Glendinning smsc95xx_write_reg(dev, MII_ADDR, addr); 1522f7ca802SSteve Glendinning 1532f7ca802SSteve Glendinning if (smsc95xx_phy_wait_not_busy(dev)) { 15460b86755SJoe Perches netdev_warn(dev->net, "Timed out reading MII reg %02X\n", idx); 1552f7ca802SSteve Glendinning mutex_unlock(&dev->phy_mutex); 1562f7ca802SSteve Glendinning return -EIO; 1572f7ca802SSteve Glendinning } 1582f7ca802SSteve Glendinning 1592f7ca802SSteve Glendinning smsc95xx_read_reg(dev, MII_DATA, &val); 1602f7ca802SSteve Glendinning 1612f7ca802SSteve Glendinning mutex_unlock(&dev->phy_mutex); 1622f7ca802SSteve Glendinning 1632f7ca802SSteve Glendinning return (u16)(val & 0xFFFF); 1642f7ca802SSteve Glendinning } 1652f7ca802SSteve Glendinning 1662f7ca802SSteve Glendinning static void smsc95xx_mdio_write(struct net_device *netdev, int phy_id, int idx, 1672f7ca802SSteve Glendinning int regval) 1682f7ca802SSteve Glendinning { 1692f7ca802SSteve Glendinning struct usbnet *dev = netdev_priv(netdev); 1702f7ca802SSteve Glendinning u32 val, addr; 1712f7ca802SSteve Glendinning 1722f7ca802SSteve Glendinning mutex_lock(&dev->phy_mutex); 1732f7ca802SSteve Glendinning 1742f7ca802SSteve Glendinning /* confirm MII not busy */ 1752f7ca802SSteve Glendinning if (smsc95xx_phy_wait_not_busy(dev)) { 17660b86755SJoe Perches netdev_warn(dev->net, "MII is busy in smsc95xx_mdio_write\n"); 1772f7ca802SSteve Glendinning mutex_unlock(&dev->phy_mutex); 1782f7ca802SSteve Glendinning return; 1792f7ca802SSteve Glendinning } 1802f7ca802SSteve Glendinning 1812f7ca802SSteve Glendinning val = regval; 1822f7ca802SSteve Glendinning smsc95xx_write_reg(dev, MII_DATA, val); 1832f7ca802SSteve Glendinning 1842f7ca802SSteve Glendinning /* set the address, index & direction (write to PHY) */ 1852f7ca802SSteve Glendinning phy_id &= dev->mii.phy_id_mask; 1862f7ca802SSteve Glendinning idx &= dev->mii.reg_num_mask; 1872f7ca802SSteve Glendinning addr = (phy_id << 11) | (idx << 6) | MII_WRITE_; 1882f7ca802SSteve Glendinning smsc95xx_write_reg(dev, MII_ADDR, addr); 1892f7ca802SSteve Glendinning 1902f7ca802SSteve Glendinning if (smsc95xx_phy_wait_not_busy(dev)) 19160b86755SJoe Perches netdev_warn(dev->net, "Timed out writing MII reg %02X\n", idx); 1922f7ca802SSteve Glendinning 1932f7ca802SSteve Glendinning mutex_unlock(&dev->phy_mutex); 1942f7ca802SSteve Glendinning } 1952f7ca802SSteve Glendinning 1962f7ca802SSteve Glendinning static int smsc95xx_wait_eeprom(struct usbnet *dev) 1972f7ca802SSteve Glendinning { 1982f7ca802SSteve Glendinning unsigned long start_time = jiffies; 1992f7ca802SSteve Glendinning u32 val; 2002f7ca802SSteve Glendinning 2012f7ca802SSteve Glendinning do { 2022f7ca802SSteve Glendinning smsc95xx_read_reg(dev, E2P_CMD, &val); 2032f7ca802SSteve Glendinning if (!(val & E2P_CMD_BUSY_) || (val & E2P_CMD_TIMEOUT_)) 2042f7ca802SSteve Glendinning break; 2052f7ca802SSteve Glendinning udelay(40); 2062f7ca802SSteve Glendinning } while (!time_after(jiffies, start_time + HZ)); 2072f7ca802SSteve Glendinning 2082f7ca802SSteve Glendinning if (val & (E2P_CMD_TIMEOUT_ | E2P_CMD_BUSY_)) { 20960b86755SJoe Perches netdev_warn(dev->net, "EEPROM read operation timeout\n"); 2102f7ca802SSteve Glendinning return -EIO; 2112f7ca802SSteve Glendinning } 2122f7ca802SSteve Glendinning 2132f7ca802SSteve Glendinning return 0; 2142f7ca802SSteve Glendinning } 2152f7ca802SSteve Glendinning 2162f7ca802SSteve Glendinning static int smsc95xx_eeprom_confirm_not_busy(struct usbnet *dev) 2172f7ca802SSteve Glendinning { 2182f7ca802SSteve Glendinning unsigned long start_time = jiffies; 2192f7ca802SSteve Glendinning u32 val; 2202f7ca802SSteve Glendinning 2212f7ca802SSteve Glendinning do { 2222f7ca802SSteve Glendinning smsc95xx_read_reg(dev, E2P_CMD, &val); 2232f7ca802SSteve Glendinning 2242f7ca802SSteve Glendinning if (!(val & E2P_CMD_BUSY_)) 2252f7ca802SSteve Glendinning return 0; 2262f7ca802SSteve Glendinning 2272f7ca802SSteve Glendinning udelay(40); 2282f7ca802SSteve Glendinning } while (!time_after(jiffies, start_time + HZ)); 2292f7ca802SSteve Glendinning 23060b86755SJoe Perches netdev_warn(dev->net, "EEPROM is busy\n"); 2312f7ca802SSteve Glendinning return -EIO; 2322f7ca802SSteve Glendinning } 2332f7ca802SSteve Glendinning 2342f7ca802SSteve Glendinning static int smsc95xx_read_eeprom(struct usbnet *dev, u32 offset, u32 length, 2352f7ca802SSteve Glendinning u8 *data) 2362f7ca802SSteve Glendinning { 2372f7ca802SSteve Glendinning u32 val; 2382f7ca802SSteve Glendinning int i, ret; 2392f7ca802SSteve Glendinning 2402f7ca802SSteve Glendinning BUG_ON(!dev); 2412f7ca802SSteve Glendinning BUG_ON(!data); 2422f7ca802SSteve Glendinning 2432f7ca802SSteve Glendinning ret = smsc95xx_eeprom_confirm_not_busy(dev); 2442f7ca802SSteve Glendinning if (ret) 2452f7ca802SSteve Glendinning return ret; 2462f7ca802SSteve Glendinning 2472f7ca802SSteve Glendinning for (i = 0; i < length; i++) { 2482f7ca802SSteve Glendinning val = E2P_CMD_BUSY_ | E2P_CMD_READ_ | (offset & E2P_CMD_ADDR_); 2492f7ca802SSteve Glendinning smsc95xx_write_reg(dev, E2P_CMD, val); 2502f7ca802SSteve Glendinning 2512f7ca802SSteve Glendinning ret = smsc95xx_wait_eeprom(dev); 2522f7ca802SSteve Glendinning if (ret < 0) 2532f7ca802SSteve Glendinning return ret; 2542f7ca802SSteve Glendinning 2552f7ca802SSteve Glendinning smsc95xx_read_reg(dev, E2P_DATA, &val); 2562f7ca802SSteve Glendinning 2572f7ca802SSteve Glendinning data[i] = val & 0xFF; 2582f7ca802SSteve Glendinning offset++; 2592f7ca802SSteve Glendinning } 2602f7ca802SSteve Glendinning 2612f7ca802SSteve Glendinning return 0; 2622f7ca802SSteve Glendinning } 2632f7ca802SSteve Glendinning 2642f7ca802SSteve Glendinning static int smsc95xx_write_eeprom(struct usbnet *dev, u32 offset, u32 length, 2652f7ca802SSteve Glendinning u8 *data) 2662f7ca802SSteve Glendinning { 2672f7ca802SSteve Glendinning u32 val; 2682f7ca802SSteve Glendinning int i, ret; 2692f7ca802SSteve Glendinning 2702f7ca802SSteve Glendinning BUG_ON(!dev); 2712f7ca802SSteve Glendinning BUG_ON(!data); 2722f7ca802SSteve Glendinning 2732f7ca802SSteve Glendinning ret = smsc95xx_eeprom_confirm_not_busy(dev); 2742f7ca802SSteve Glendinning if (ret) 2752f7ca802SSteve Glendinning return ret; 2762f7ca802SSteve Glendinning 2772f7ca802SSteve Glendinning /* Issue write/erase enable command */ 2782f7ca802SSteve Glendinning val = E2P_CMD_BUSY_ | E2P_CMD_EWEN_; 2792f7ca802SSteve Glendinning smsc95xx_write_reg(dev, E2P_CMD, val); 2802f7ca802SSteve Glendinning 2812f7ca802SSteve Glendinning ret = smsc95xx_wait_eeprom(dev); 2822f7ca802SSteve Glendinning if (ret < 0) 2832f7ca802SSteve Glendinning return ret; 2842f7ca802SSteve Glendinning 2852f7ca802SSteve Glendinning for (i = 0; i < length; i++) { 2862f7ca802SSteve Glendinning 2872f7ca802SSteve Glendinning /* Fill data register */ 2882f7ca802SSteve Glendinning val = data[i]; 2892f7ca802SSteve Glendinning smsc95xx_write_reg(dev, E2P_DATA, val); 2902f7ca802SSteve Glendinning 2912f7ca802SSteve Glendinning /* Send "write" command */ 2922f7ca802SSteve Glendinning val = E2P_CMD_BUSY_ | E2P_CMD_WRITE_ | (offset & E2P_CMD_ADDR_); 2932f7ca802SSteve Glendinning smsc95xx_write_reg(dev, E2P_CMD, val); 2942f7ca802SSteve Glendinning 2952f7ca802SSteve Glendinning ret = smsc95xx_wait_eeprom(dev); 2962f7ca802SSteve Glendinning if (ret < 0) 2972f7ca802SSteve Glendinning return ret; 2982f7ca802SSteve Glendinning 2992f7ca802SSteve Glendinning offset++; 3002f7ca802SSteve Glendinning } 3012f7ca802SSteve Glendinning 3022f7ca802SSteve Glendinning return 0; 3032f7ca802SSteve Glendinning } 3042f7ca802SSteve Glendinning 305150a7fccSSteve Glendinning static void smsc95xx_async_cmd_callback(struct urb *urb) 3062f7ca802SSteve Glendinning { 3072f7ca802SSteve Glendinning struct usb_context *usb_context = urb->context; 3082f7ca802SSteve Glendinning struct usbnet *dev = usb_context->dev; 309c94cb314SOliver Neukum int status = urb->status; 3102f7ca802SSteve Glendinning 311c94cb314SOliver Neukum if (status < 0) 31260b86755SJoe Perches netdev_warn(dev->net, "async callback failed with %d\n", status); 3132f7ca802SSteve Glendinning 3142f7ca802SSteve Glendinning kfree(usb_context); 3152f7ca802SSteve Glendinning usb_free_urb(urb); 3162f7ca802SSteve Glendinning } 3172f7ca802SSteve Glendinning 3181d74a6bdSSteve Glendinning static int smsc95xx_write_reg_async(struct usbnet *dev, u16 index, u32 *data) 3192f7ca802SSteve Glendinning { 3202f7ca802SSteve Glendinning struct usb_context *usb_context; 3212f7ca802SSteve Glendinning int status; 3222f7ca802SSteve Glendinning struct urb *urb; 3231d74a6bdSSteve Glendinning const u16 size = 4; 3242f7ca802SSteve Glendinning 3252f7ca802SSteve Glendinning urb = usb_alloc_urb(0, GFP_ATOMIC); 3262f7ca802SSteve Glendinning if (!urb) { 32760b86755SJoe Perches netdev_warn(dev->net, "Error allocating URB\n"); 3282f7ca802SSteve Glendinning return -ENOMEM; 3292f7ca802SSteve Glendinning } 3302f7ca802SSteve Glendinning 3312f7ca802SSteve Glendinning usb_context = kmalloc(sizeof(struct usb_context), GFP_ATOMIC); 3322f7ca802SSteve Glendinning if (usb_context == NULL) { 33360b86755SJoe Perches netdev_warn(dev->net, "Error allocating control msg\n"); 3342f7ca802SSteve Glendinning usb_free_urb(urb); 3352f7ca802SSteve Glendinning return -ENOMEM; 3362f7ca802SSteve Glendinning } 3372f7ca802SSteve Glendinning 3382f7ca802SSteve Glendinning usb_context->req.bRequestType = 3392f7ca802SSteve Glendinning USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE; 3402f7ca802SSteve Glendinning usb_context->req.bRequest = USB_VENDOR_REQUEST_WRITE_REGISTER; 3412f7ca802SSteve Glendinning usb_context->req.wValue = 00; 3421d74a6bdSSteve Glendinning usb_context->req.wIndex = cpu_to_le16(index); 3431d74a6bdSSteve Glendinning usb_context->req.wLength = cpu_to_le16(size); 3442f7ca802SSteve Glendinning 3452f7ca802SSteve Glendinning usb_fill_control_urb(urb, dev->udev, usb_sndctrlpipe(dev->udev, 0), 3462f7ca802SSteve Glendinning (void *)&usb_context->req, data, size, 347150a7fccSSteve Glendinning smsc95xx_async_cmd_callback, 3482f7ca802SSteve Glendinning (void *)usb_context); 3492f7ca802SSteve Glendinning 3502f7ca802SSteve Glendinning status = usb_submit_urb(urb, GFP_ATOMIC); 3512f7ca802SSteve Glendinning if (status < 0) { 35260b86755SJoe Perches netdev_warn(dev->net, "Error submitting control msg, sts=%d\n", 35360b86755SJoe Perches status); 3542f7ca802SSteve Glendinning kfree(usb_context); 3552f7ca802SSteve Glendinning usb_free_urb(urb); 3562f7ca802SSteve Glendinning } 3572f7ca802SSteve Glendinning 3582f7ca802SSteve Glendinning return status; 3592f7ca802SSteve Glendinning } 3602f7ca802SSteve Glendinning 3612f7ca802SSteve Glendinning /* returns hash bit number for given MAC address 3622f7ca802SSteve Glendinning * example: 3632f7ca802SSteve Glendinning * 01 00 5E 00 00 01 -> returns bit number 31 */ 3642f7ca802SSteve Glendinning static unsigned int smsc95xx_hash(char addr[ETH_ALEN]) 3652f7ca802SSteve Glendinning { 3662f7ca802SSteve Glendinning return (ether_crc(ETH_ALEN, addr) >> 26) & 0x3f; 3672f7ca802SSteve Glendinning } 3682f7ca802SSteve Glendinning 3692f7ca802SSteve Glendinning static void smsc95xx_set_multicast(struct net_device *netdev) 3702f7ca802SSteve Glendinning { 3712f7ca802SSteve Glendinning struct usbnet *dev = netdev_priv(netdev); 3722f7ca802SSteve Glendinning struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]); 3732f7ca802SSteve Glendinning unsigned long flags; 3742f7ca802SSteve Glendinning 3753c0f3c60SMarc Zyngier pdata->hash_hi = 0; 3763c0f3c60SMarc Zyngier pdata->hash_lo = 0; 3773c0f3c60SMarc Zyngier 3782f7ca802SSteve Glendinning spin_lock_irqsave(&pdata->mac_cr_lock, flags); 3792f7ca802SSteve Glendinning 3802f7ca802SSteve Glendinning if (dev->net->flags & IFF_PROMISC) { 381a475f603SJoe Perches netif_dbg(dev, drv, dev->net, "promiscuous mode enabled\n"); 3822f7ca802SSteve Glendinning pdata->mac_cr |= MAC_CR_PRMS_; 3832f7ca802SSteve Glendinning pdata->mac_cr &= ~(MAC_CR_MCPAS_ | MAC_CR_HPFILT_); 3842f7ca802SSteve Glendinning } else if (dev->net->flags & IFF_ALLMULTI) { 385a475f603SJoe Perches netif_dbg(dev, drv, dev->net, "receive all multicast enabled\n"); 3862f7ca802SSteve Glendinning pdata->mac_cr |= MAC_CR_MCPAS_; 3872f7ca802SSteve Glendinning pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_HPFILT_); 3884cd24eafSJiri Pirko } else if (!netdev_mc_empty(dev->net)) { 38922bedad3SJiri Pirko struct netdev_hw_addr *ha; 3902f7ca802SSteve Glendinning 3912f7ca802SSteve Glendinning pdata->mac_cr |= MAC_CR_HPFILT_; 3922f7ca802SSteve Glendinning pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_); 3932f7ca802SSteve Glendinning 39422bedad3SJiri Pirko netdev_for_each_mc_addr(ha, netdev) { 39522bedad3SJiri Pirko u32 bitnum = smsc95xx_hash(ha->addr); 3962f7ca802SSteve Glendinning u32 mask = 0x01 << (bitnum & 0x1F); 3972f7ca802SSteve Glendinning if (bitnum & 0x20) 3983c0f3c60SMarc Zyngier pdata->hash_hi |= mask; 3992f7ca802SSteve Glendinning else 4003c0f3c60SMarc Zyngier pdata->hash_lo |= mask; 4012f7ca802SSteve Glendinning } 4022f7ca802SSteve Glendinning 403a475f603SJoe Perches netif_dbg(dev, drv, dev->net, "HASHH=0x%08X, HASHL=0x%08X\n", 4043c0f3c60SMarc Zyngier pdata->hash_hi, pdata->hash_lo); 4052f7ca802SSteve Glendinning } else { 406a475f603SJoe Perches netif_dbg(dev, drv, dev->net, "receive own packets only\n"); 4072f7ca802SSteve Glendinning pdata->mac_cr &= 4082f7ca802SSteve Glendinning ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_ | MAC_CR_HPFILT_); 4092f7ca802SSteve Glendinning } 4102f7ca802SSteve Glendinning 4112f7ca802SSteve Glendinning spin_unlock_irqrestore(&pdata->mac_cr_lock, flags); 4122f7ca802SSteve Glendinning 4132f7ca802SSteve Glendinning /* Initiate async writes, as we can't wait for completion here */ 4143c0f3c60SMarc Zyngier smsc95xx_write_reg_async(dev, HASHH, &pdata->hash_hi); 4153c0f3c60SMarc Zyngier smsc95xx_write_reg_async(dev, HASHL, &pdata->hash_lo); 4162f7ca802SSteve Glendinning smsc95xx_write_reg_async(dev, MAC_CR, &pdata->mac_cr); 4172f7ca802SSteve Glendinning } 4182f7ca802SSteve Glendinning 4192f7ca802SSteve Glendinning static void smsc95xx_phy_update_flowcontrol(struct usbnet *dev, u8 duplex, 4202f7ca802SSteve Glendinning u16 lcladv, u16 rmtadv) 4212f7ca802SSteve Glendinning { 4222f7ca802SSteve Glendinning u32 flow, afc_cfg = 0; 4232f7ca802SSteve Glendinning 4242f7ca802SSteve Glendinning int ret = smsc95xx_read_reg(dev, AFC_CFG, &afc_cfg); 4252f7ca802SSteve Glendinning if (ret < 0) { 42660b86755SJoe Perches netdev_warn(dev->net, "error reading AFC_CFG\n"); 4272f7ca802SSteve Glendinning return; 4282f7ca802SSteve Glendinning } 4292f7ca802SSteve Glendinning 4302f7ca802SSteve Glendinning if (duplex == DUPLEX_FULL) { 431bc02ff95SSteve Glendinning u8 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv); 4322f7ca802SSteve Glendinning 4332f7ca802SSteve Glendinning if (cap & FLOW_CTRL_RX) 4342f7ca802SSteve Glendinning flow = 0xFFFF0002; 4352f7ca802SSteve Glendinning else 4362f7ca802SSteve Glendinning flow = 0; 4372f7ca802SSteve Glendinning 4382f7ca802SSteve Glendinning if (cap & FLOW_CTRL_TX) 4392f7ca802SSteve Glendinning afc_cfg |= 0xF; 4402f7ca802SSteve Glendinning else 4412f7ca802SSteve Glendinning afc_cfg &= ~0xF; 4422f7ca802SSteve Glendinning 443a475f603SJoe Perches netif_dbg(dev, link, dev->net, "rx pause %s, tx pause %s\n", 44460b86755SJoe Perches cap & FLOW_CTRL_RX ? "enabled" : "disabled", 44560b86755SJoe Perches cap & FLOW_CTRL_TX ? "enabled" : "disabled"); 4462f7ca802SSteve Glendinning } else { 447a475f603SJoe Perches netif_dbg(dev, link, dev->net, "half duplex\n"); 4482f7ca802SSteve Glendinning flow = 0; 4492f7ca802SSteve Glendinning afc_cfg |= 0xF; 4502f7ca802SSteve Glendinning } 4512f7ca802SSteve Glendinning 4522f7ca802SSteve Glendinning smsc95xx_write_reg(dev, FLOW, flow); 4532f7ca802SSteve Glendinning smsc95xx_write_reg(dev, AFC_CFG, afc_cfg); 4542f7ca802SSteve Glendinning } 4552f7ca802SSteve Glendinning 4562f7ca802SSteve Glendinning static int smsc95xx_link_reset(struct usbnet *dev) 4572f7ca802SSteve Glendinning { 4582f7ca802SSteve Glendinning struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]); 4592f7ca802SSteve Glendinning struct mii_if_info *mii = &dev->mii; 4608ae6dacaSDavid Decotigny struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET }; 4612f7ca802SSteve Glendinning unsigned long flags; 4622f7ca802SSteve Glendinning u16 lcladv, rmtadv; 4632f7ca802SSteve Glendinning 4642f7ca802SSteve Glendinning /* clear interrupt status */ 4652f7ca802SSteve Glendinning smsc95xx_mdio_read(dev->net, mii->phy_id, PHY_INT_SRC); 466*4436761bSSteve Glendinning smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_); 4672f7ca802SSteve Glendinning 4682f7ca802SSteve Glendinning mii_check_media(mii, 1, 1); 4692f7ca802SSteve Glendinning mii_ethtool_gset(&dev->mii, &ecmd); 4702f7ca802SSteve Glendinning lcladv = smsc95xx_mdio_read(dev->net, mii->phy_id, MII_ADVERTISE); 4712f7ca802SSteve Glendinning rmtadv = smsc95xx_mdio_read(dev->net, mii->phy_id, MII_LPA); 4722f7ca802SSteve Glendinning 4738ae6dacaSDavid Decotigny netif_dbg(dev, link, dev->net, 4748ae6dacaSDavid Decotigny "speed: %u duplex: %d lcladv: %04x rmtadv: %04x\n", 4758ae6dacaSDavid Decotigny ethtool_cmd_speed(&ecmd), ecmd.duplex, lcladv, rmtadv); 4762f7ca802SSteve Glendinning 4772f7ca802SSteve Glendinning spin_lock_irqsave(&pdata->mac_cr_lock, flags); 4782f7ca802SSteve Glendinning if (ecmd.duplex != DUPLEX_FULL) { 4792f7ca802SSteve Glendinning pdata->mac_cr &= ~MAC_CR_FDPX_; 4802f7ca802SSteve Glendinning pdata->mac_cr |= MAC_CR_RCVOWN_; 4812f7ca802SSteve Glendinning } else { 4822f7ca802SSteve Glendinning pdata->mac_cr &= ~MAC_CR_RCVOWN_; 4832f7ca802SSteve Glendinning pdata->mac_cr |= MAC_CR_FDPX_; 4842f7ca802SSteve Glendinning } 4852f7ca802SSteve Glendinning spin_unlock_irqrestore(&pdata->mac_cr_lock, flags); 4862f7ca802SSteve Glendinning 4872f7ca802SSteve Glendinning smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr); 4882f7ca802SSteve Glendinning 4892f7ca802SSteve Glendinning smsc95xx_phy_update_flowcontrol(dev, ecmd.duplex, lcladv, rmtadv); 4902f7ca802SSteve Glendinning 4912f7ca802SSteve Glendinning return 0; 4922f7ca802SSteve Glendinning } 4932f7ca802SSteve Glendinning 4942f7ca802SSteve Glendinning static void smsc95xx_status(struct usbnet *dev, struct urb *urb) 4952f7ca802SSteve Glendinning { 4962f7ca802SSteve Glendinning u32 intdata; 4972f7ca802SSteve Glendinning 4982f7ca802SSteve Glendinning if (urb->actual_length != 4) { 49960b86755SJoe Perches netdev_warn(dev->net, "unexpected urb length %d\n", 50060b86755SJoe Perches urb->actual_length); 5012f7ca802SSteve Glendinning return; 5022f7ca802SSteve Glendinning } 5032f7ca802SSteve Glendinning 5042f7ca802SSteve Glendinning memcpy(&intdata, urb->transfer_buffer, 4); 5051d74a6bdSSteve Glendinning le32_to_cpus(&intdata); 5062f7ca802SSteve Glendinning 507a475f603SJoe Perches netif_dbg(dev, link, dev->net, "intdata: 0x%08X\n", intdata); 5082f7ca802SSteve Glendinning 5092f7ca802SSteve Glendinning if (intdata & INT_ENP_PHY_INT_) 5102f7ca802SSteve Glendinning usbnet_defer_kevent(dev, EVENT_LINK_RESET); 5112f7ca802SSteve Glendinning else 51260b86755SJoe Perches netdev_warn(dev->net, "unexpected interrupt, intdata=0x%08X\n", 51360b86755SJoe Perches intdata); 5142f7ca802SSteve Glendinning } 5152f7ca802SSteve Glendinning 516f7b29271SSteve Glendinning /* Enable or disable Tx & Rx checksum offload engines */ 517c8f44affSMichał Mirosław static int smsc95xx_set_features(struct net_device *netdev, 518c8f44affSMichał Mirosław netdev_features_t features) 5192f7ca802SSteve Glendinning { 52078e47fe4SMichał Mirosław struct usbnet *dev = netdev_priv(netdev); 5212f7ca802SSteve Glendinning u32 read_buf; 52278e47fe4SMichał Mirosław int ret; 52378e47fe4SMichał Mirosław 52478e47fe4SMichał Mirosław ret = smsc95xx_read_reg(dev, COE_CR, &read_buf); 5252f7ca802SSteve Glendinning if (ret < 0) { 52660b86755SJoe Perches netdev_warn(dev->net, "Failed to read COE_CR: %d\n", ret); 5272f7ca802SSteve Glendinning return ret; 5282f7ca802SSteve Glendinning } 5292f7ca802SSteve Glendinning 53078e47fe4SMichał Mirosław if (features & NETIF_F_HW_CSUM) 531f7b29271SSteve Glendinning read_buf |= Tx_COE_EN_; 532f7b29271SSteve Glendinning else 533f7b29271SSteve Glendinning read_buf &= ~Tx_COE_EN_; 534f7b29271SSteve Glendinning 53578e47fe4SMichał Mirosław if (features & NETIF_F_RXCSUM) 5362f7ca802SSteve Glendinning read_buf |= Rx_COE_EN_; 5372f7ca802SSteve Glendinning else 5382f7ca802SSteve Glendinning read_buf &= ~Rx_COE_EN_; 5392f7ca802SSteve Glendinning 5402f7ca802SSteve Glendinning ret = smsc95xx_write_reg(dev, COE_CR, read_buf); 5412f7ca802SSteve Glendinning if (ret < 0) { 54260b86755SJoe Perches netdev_warn(dev->net, "Failed to write COE_CR: %d\n", ret); 5432f7ca802SSteve Glendinning return ret; 5442f7ca802SSteve Glendinning } 5452f7ca802SSteve Glendinning 546a475f603SJoe Perches netif_dbg(dev, hw, dev->net, "COE_CR = 0x%08x\n", read_buf); 5472f7ca802SSteve Glendinning return 0; 5482f7ca802SSteve Glendinning } 5492f7ca802SSteve Glendinning 5502f7ca802SSteve Glendinning static int smsc95xx_ethtool_get_eeprom_len(struct net_device *net) 5512f7ca802SSteve Glendinning { 5522f7ca802SSteve Glendinning return MAX_EEPROM_SIZE; 5532f7ca802SSteve Glendinning } 5542f7ca802SSteve Glendinning 5552f7ca802SSteve Glendinning static int smsc95xx_ethtool_get_eeprom(struct net_device *netdev, 5562f7ca802SSteve Glendinning struct ethtool_eeprom *ee, u8 *data) 5572f7ca802SSteve Glendinning { 5582f7ca802SSteve Glendinning struct usbnet *dev = netdev_priv(netdev); 5592f7ca802SSteve Glendinning 5602f7ca802SSteve Glendinning ee->magic = LAN95XX_EEPROM_MAGIC; 5612f7ca802SSteve Glendinning 5622f7ca802SSteve Glendinning return smsc95xx_read_eeprom(dev, ee->offset, ee->len, data); 5632f7ca802SSteve Glendinning } 5642f7ca802SSteve Glendinning 5652f7ca802SSteve Glendinning static int smsc95xx_ethtool_set_eeprom(struct net_device *netdev, 5662f7ca802SSteve Glendinning struct ethtool_eeprom *ee, u8 *data) 5672f7ca802SSteve Glendinning { 5682f7ca802SSteve Glendinning struct usbnet *dev = netdev_priv(netdev); 5692f7ca802SSteve Glendinning 5702f7ca802SSteve Glendinning if (ee->magic != LAN95XX_EEPROM_MAGIC) { 57160b86755SJoe Perches netdev_warn(dev->net, "EEPROM: magic value mismatch, magic = 0x%x\n", 5722f7ca802SSteve Glendinning ee->magic); 5732f7ca802SSteve Glendinning return -EINVAL; 5742f7ca802SSteve Glendinning } 5752f7ca802SSteve Glendinning 5762f7ca802SSteve Glendinning return smsc95xx_write_eeprom(dev, ee->offset, ee->len, data); 5772f7ca802SSteve Glendinning } 5782f7ca802SSteve Glendinning 5799fa32e94SEmeric Vigier static int smsc95xx_ethtool_getregslen(struct net_device *netdev) 5809fa32e94SEmeric Vigier { 5819fa32e94SEmeric Vigier /* all smsc95xx registers */ 5829fa32e94SEmeric Vigier return COE_CR - ID_REV + 1; 5839fa32e94SEmeric Vigier } 5849fa32e94SEmeric Vigier 5859fa32e94SEmeric Vigier static void 5869fa32e94SEmeric Vigier smsc95xx_ethtool_getregs(struct net_device *netdev, struct ethtool_regs *regs, 5879fa32e94SEmeric Vigier void *buf) 5889fa32e94SEmeric Vigier { 5899fa32e94SEmeric Vigier struct usbnet *dev = netdev_priv(netdev); 590d348446bSDan Carpenter unsigned int i, j; 591d348446bSDan Carpenter int retval; 5929fa32e94SEmeric Vigier u32 *data = buf; 5939fa32e94SEmeric Vigier 5949fa32e94SEmeric Vigier retval = smsc95xx_read_reg(dev, ID_REV, ®s->version); 5959fa32e94SEmeric Vigier if (retval < 0) { 5969fa32e94SEmeric Vigier netdev_warn(netdev, "REGS: cannot read ID_REV\n"); 5979fa32e94SEmeric Vigier return; 5989fa32e94SEmeric Vigier } 5999fa32e94SEmeric Vigier 6009fa32e94SEmeric Vigier for (i = ID_REV, j = 0; i <= COE_CR; i += (sizeof(u32)), j++) { 6019fa32e94SEmeric Vigier retval = smsc95xx_read_reg(dev, i, &data[j]); 6029fa32e94SEmeric Vigier if (retval < 0) { 6039fa32e94SEmeric Vigier netdev_warn(netdev, "REGS: cannot read reg[%x]\n", i); 6049fa32e94SEmeric Vigier return; 6059fa32e94SEmeric Vigier } 6069fa32e94SEmeric Vigier } 6079fa32e94SEmeric Vigier } 6089fa32e94SEmeric Vigier 6090fc0b732SStephen Hemminger static const struct ethtool_ops smsc95xx_ethtool_ops = { 6102f7ca802SSteve Glendinning .get_link = usbnet_get_link, 6112f7ca802SSteve Glendinning .nway_reset = usbnet_nway_reset, 6122f7ca802SSteve Glendinning .get_drvinfo = usbnet_get_drvinfo, 6132f7ca802SSteve Glendinning .get_msglevel = usbnet_get_msglevel, 6142f7ca802SSteve Glendinning .set_msglevel = usbnet_set_msglevel, 6152f7ca802SSteve Glendinning .get_settings = usbnet_get_settings, 6162f7ca802SSteve Glendinning .set_settings = usbnet_set_settings, 6172f7ca802SSteve Glendinning .get_eeprom_len = smsc95xx_ethtool_get_eeprom_len, 6182f7ca802SSteve Glendinning .get_eeprom = smsc95xx_ethtool_get_eeprom, 6192f7ca802SSteve Glendinning .set_eeprom = smsc95xx_ethtool_set_eeprom, 6209fa32e94SEmeric Vigier .get_regs_len = smsc95xx_ethtool_getregslen, 6219fa32e94SEmeric Vigier .get_regs = smsc95xx_ethtool_getregs, 6222f7ca802SSteve Glendinning }; 6232f7ca802SSteve Glendinning 6242f7ca802SSteve Glendinning static int smsc95xx_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd) 6252f7ca802SSteve Glendinning { 6262f7ca802SSteve Glendinning struct usbnet *dev = netdev_priv(netdev); 6272f7ca802SSteve Glendinning 6282f7ca802SSteve Glendinning if (!netif_running(netdev)) 6292f7ca802SSteve Glendinning return -EINVAL; 6302f7ca802SSteve Glendinning 6312f7ca802SSteve Glendinning return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL); 6322f7ca802SSteve Glendinning } 6332f7ca802SSteve Glendinning 6342f7ca802SSteve Glendinning static void smsc95xx_init_mac_address(struct usbnet *dev) 6352f7ca802SSteve Glendinning { 6362f7ca802SSteve Glendinning /* try reading mac address from EEPROM */ 6372f7ca802SSteve Glendinning if (smsc95xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN, 6382f7ca802SSteve Glendinning dev->net->dev_addr) == 0) { 6392f7ca802SSteve Glendinning if (is_valid_ether_addr(dev->net->dev_addr)) { 6402f7ca802SSteve Glendinning /* eeprom values are valid so use them */ 641a475f603SJoe Perches netif_dbg(dev, ifup, dev->net, "MAC address read from EEPROM\n"); 6422f7ca802SSteve Glendinning return; 6432f7ca802SSteve Glendinning } 6442f7ca802SSteve Glendinning } 6452f7ca802SSteve Glendinning 6462f7ca802SSteve Glendinning /* no eeprom, or eeprom values are invalid. generate random MAC */ 647f2cedb63SDanny Kukawka eth_hw_addr_random(dev->net); 648c7e12eadSJoe Perches netif_dbg(dev, ifup, dev->net, "MAC address set to eth_random_addr\n"); 6492f7ca802SSteve Glendinning } 6502f7ca802SSteve Glendinning 6512f7ca802SSteve Glendinning static int smsc95xx_set_mac_address(struct usbnet *dev) 6522f7ca802SSteve Glendinning { 6532f7ca802SSteve Glendinning u32 addr_lo = dev->net->dev_addr[0] | dev->net->dev_addr[1] << 8 | 6542f7ca802SSteve Glendinning dev->net->dev_addr[2] << 16 | dev->net->dev_addr[3] << 24; 6552f7ca802SSteve Glendinning u32 addr_hi = dev->net->dev_addr[4] | dev->net->dev_addr[5] << 8; 6562f7ca802SSteve Glendinning int ret; 6572f7ca802SSteve Glendinning 6582f7ca802SSteve Glendinning ret = smsc95xx_write_reg(dev, ADDRL, addr_lo); 6592f7ca802SSteve Glendinning if (ret < 0) { 66060b86755SJoe Perches netdev_warn(dev->net, "Failed to write ADDRL: %d\n", ret); 6612f7ca802SSteve Glendinning return ret; 6622f7ca802SSteve Glendinning } 6632f7ca802SSteve Glendinning 6642f7ca802SSteve Glendinning ret = smsc95xx_write_reg(dev, ADDRH, addr_hi); 6652f7ca802SSteve Glendinning if (ret < 0) { 66660b86755SJoe Perches netdev_warn(dev->net, "Failed to write ADDRH: %d\n", ret); 6672f7ca802SSteve Glendinning return ret; 6682f7ca802SSteve Glendinning } 6692f7ca802SSteve Glendinning 6702f7ca802SSteve Glendinning return 0; 6712f7ca802SSteve Glendinning } 6722f7ca802SSteve Glendinning 6732f7ca802SSteve Glendinning /* starts the TX path */ 6742f7ca802SSteve Glendinning static void smsc95xx_start_tx_path(struct usbnet *dev) 6752f7ca802SSteve Glendinning { 6762f7ca802SSteve Glendinning struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]); 6772f7ca802SSteve Glendinning unsigned long flags; 6782f7ca802SSteve Glendinning 6792f7ca802SSteve Glendinning /* Enable Tx at MAC */ 6802f7ca802SSteve Glendinning spin_lock_irqsave(&pdata->mac_cr_lock, flags); 6812f7ca802SSteve Glendinning pdata->mac_cr |= MAC_CR_TXEN_; 6822f7ca802SSteve Glendinning spin_unlock_irqrestore(&pdata->mac_cr_lock, flags); 6832f7ca802SSteve Glendinning 6842f7ca802SSteve Glendinning smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr); 6852f7ca802SSteve Glendinning 6862f7ca802SSteve Glendinning /* Enable Tx at SCSRs */ 687*4436761bSSteve Glendinning smsc95xx_write_reg(dev, TX_CFG, TX_CFG_ON_); 6882f7ca802SSteve Glendinning } 6892f7ca802SSteve Glendinning 6902f7ca802SSteve Glendinning /* Starts the Receive path */ 6912f7ca802SSteve Glendinning static void smsc95xx_start_rx_path(struct usbnet *dev) 6922f7ca802SSteve Glendinning { 6932f7ca802SSteve Glendinning struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]); 6942f7ca802SSteve Glendinning unsigned long flags; 6952f7ca802SSteve Glendinning 6962f7ca802SSteve Glendinning spin_lock_irqsave(&pdata->mac_cr_lock, flags); 6972f7ca802SSteve Glendinning pdata->mac_cr |= MAC_CR_RXEN_; 6982f7ca802SSteve Glendinning spin_unlock_irqrestore(&pdata->mac_cr_lock, flags); 6992f7ca802SSteve Glendinning 7002f7ca802SSteve Glendinning smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr); 7012f7ca802SSteve Glendinning } 7022f7ca802SSteve Glendinning 7032f7ca802SSteve Glendinning static int smsc95xx_phy_initialize(struct usbnet *dev) 7042f7ca802SSteve Glendinning { 705db443c44SSteve Glendinning int bmcr, timeout = 0; 706db443c44SSteve Glendinning 7072f7ca802SSteve Glendinning /* Initialize MII structure */ 7082f7ca802SSteve Glendinning dev->mii.dev = dev->net; 7092f7ca802SSteve Glendinning dev->mii.mdio_read = smsc95xx_mdio_read; 7102f7ca802SSteve Glendinning dev->mii.mdio_write = smsc95xx_mdio_write; 7112f7ca802SSteve Glendinning dev->mii.phy_id_mask = 0x1f; 7122f7ca802SSteve Glendinning dev->mii.reg_num_mask = 0x1f; 7132f7ca802SSteve Glendinning dev->mii.phy_id = SMSC95XX_INTERNAL_PHY_ID; 7142f7ca802SSteve Glendinning 715db443c44SSteve Glendinning /* reset phy and wait for reset to complete */ 7162f7ca802SSteve Glendinning smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET); 717db443c44SSteve Glendinning 718db443c44SSteve Glendinning do { 719db443c44SSteve Glendinning msleep(10); 720db443c44SSteve Glendinning bmcr = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, MII_BMCR); 721db443c44SSteve Glendinning timeout++; 722d9460920SRabin Vincent } while ((bmcr & BMCR_RESET) && (timeout < 100)); 723db443c44SSteve Glendinning 724db443c44SSteve Glendinning if (timeout >= 100) { 725db443c44SSteve Glendinning netdev_warn(dev->net, "timeout on PHY Reset"); 726db443c44SSteve Glendinning return -EIO; 727db443c44SSteve Glendinning } 728db443c44SSteve Glendinning 7292f7ca802SSteve Glendinning smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE, 7302f7ca802SSteve Glendinning ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP | 7312f7ca802SSteve Glendinning ADVERTISE_PAUSE_ASYM); 7322f7ca802SSteve Glendinning 7332f7ca802SSteve Glendinning /* read to clear */ 7342f7ca802SSteve Glendinning smsc95xx_mdio_read(dev->net, dev->mii.phy_id, PHY_INT_SRC); 7352f7ca802SSteve Glendinning 7362f7ca802SSteve Glendinning smsc95xx_mdio_write(dev->net, dev->mii.phy_id, PHY_INT_MASK, 7372f7ca802SSteve Glendinning PHY_INT_MASK_DEFAULT_); 7382f7ca802SSteve Glendinning mii_nway_restart(&dev->mii); 7392f7ca802SSteve Glendinning 740a475f603SJoe Perches netif_dbg(dev, ifup, dev->net, "phy initialised successfully\n"); 7412f7ca802SSteve Glendinning return 0; 7422f7ca802SSteve Glendinning } 7432f7ca802SSteve Glendinning 7442f7ca802SSteve Glendinning static int smsc95xx_reset(struct usbnet *dev) 7452f7ca802SSteve Glendinning { 7462f7ca802SSteve Glendinning struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]); 7472f7ca802SSteve Glendinning u32 read_buf, write_buf, burst_cap; 7482f7ca802SSteve Glendinning int ret = 0, timeout; 7492f7ca802SSteve Glendinning 750a475f603SJoe Perches netif_dbg(dev, ifup, dev->net, "entering smsc95xx_reset\n"); 7512f7ca802SSteve Glendinning 752*4436761bSSteve Glendinning ret = smsc95xx_write_reg(dev, HW_CFG, HW_CFG_LRST_); 7532f7ca802SSteve Glendinning if (ret < 0) { 75460b86755SJoe Perches netdev_warn(dev->net, "Failed to write HW_CFG_LRST_ bit in HW_CFG register, ret = %d\n", 75560b86755SJoe Perches ret); 7562f7ca802SSteve Glendinning return ret; 7572f7ca802SSteve Glendinning } 7582f7ca802SSteve Glendinning 7592f7ca802SSteve Glendinning timeout = 0; 7602f7ca802SSteve Glendinning do { 761cf2acec2SSteve Glendinning msleep(10); 7622f7ca802SSteve Glendinning ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf); 7632f7ca802SSteve Glendinning if (ret < 0) { 76460b86755SJoe Perches netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret); 7652f7ca802SSteve Glendinning return ret; 7662f7ca802SSteve Glendinning } 7672f7ca802SSteve Glendinning timeout++; 7682f7ca802SSteve Glendinning } while ((read_buf & HW_CFG_LRST_) && (timeout < 100)); 7692f7ca802SSteve Glendinning 7702f7ca802SSteve Glendinning if (timeout >= 100) { 77160b86755SJoe Perches netdev_warn(dev->net, "timeout waiting for completion of Lite Reset\n"); 7722f7ca802SSteve Glendinning return ret; 7732f7ca802SSteve Glendinning } 7742f7ca802SSteve Glendinning 775*4436761bSSteve Glendinning ret = smsc95xx_write_reg(dev, PM_CTRL, PM_CTL_PHY_RST_); 7762f7ca802SSteve Glendinning if (ret < 0) { 77760b86755SJoe Perches netdev_warn(dev->net, "Failed to write PM_CTRL: %d\n", ret); 7782f7ca802SSteve Glendinning return ret; 7792f7ca802SSteve Glendinning } 7802f7ca802SSteve Glendinning 7812f7ca802SSteve Glendinning timeout = 0; 7822f7ca802SSteve Glendinning do { 783cf2acec2SSteve Glendinning msleep(10); 7842f7ca802SSteve Glendinning ret = smsc95xx_read_reg(dev, PM_CTRL, &read_buf); 7852f7ca802SSteve Glendinning if (ret < 0) { 78660b86755SJoe Perches netdev_warn(dev->net, "Failed to read PM_CTRL: %d\n", ret); 7872f7ca802SSteve Glendinning return ret; 7882f7ca802SSteve Glendinning } 7892f7ca802SSteve Glendinning timeout++; 7902f7ca802SSteve Glendinning } while ((read_buf & PM_CTL_PHY_RST_) && (timeout < 100)); 7912f7ca802SSteve Glendinning 7922f7ca802SSteve Glendinning if (timeout >= 100) { 79360b86755SJoe Perches netdev_warn(dev->net, "timeout waiting for PHY Reset\n"); 7942f7ca802SSteve Glendinning return ret; 7952f7ca802SSteve Glendinning } 7962f7ca802SSteve Glendinning 7972f7ca802SSteve Glendinning ret = smsc95xx_set_mac_address(dev); 7982f7ca802SSteve Glendinning if (ret < 0) 7992f7ca802SSteve Glendinning return ret; 8002f7ca802SSteve Glendinning 801a475f603SJoe Perches netif_dbg(dev, ifup, dev->net, 802a475f603SJoe Perches "MAC Address: %pM\n", dev->net->dev_addr); 8032f7ca802SSteve Glendinning 8042f7ca802SSteve Glendinning ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf); 8052f7ca802SSteve Glendinning if (ret < 0) { 80660b86755SJoe Perches netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret); 8072f7ca802SSteve Glendinning return ret; 8082f7ca802SSteve Glendinning } 8092f7ca802SSteve Glendinning 810a475f603SJoe Perches netif_dbg(dev, ifup, dev->net, 811a475f603SJoe Perches "Read Value from HW_CFG : 0x%08x\n", read_buf); 8122f7ca802SSteve Glendinning 8132f7ca802SSteve Glendinning read_buf |= HW_CFG_BIR_; 8142f7ca802SSteve Glendinning 8152f7ca802SSteve Glendinning ret = smsc95xx_write_reg(dev, HW_CFG, read_buf); 8162f7ca802SSteve Glendinning if (ret < 0) { 81760b86755SJoe Perches netdev_warn(dev->net, "Failed to write HW_CFG_BIR_ bit in HW_CFG register, ret = %d\n", 81860b86755SJoe Perches ret); 8192f7ca802SSteve Glendinning return ret; 8202f7ca802SSteve Glendinning } 8212f7ca802SSteve Glendinning 8222f7ca802SSteve Glendinning ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf); 8232f7ca802SSteve Glendinning if (ret < 0) { 82460b86755SJoe Perches netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret); 8252f7ca802SSteve Glendinning return ret; 8262f7ca802SSteve Glendinning } 827a475f603SJoe Perches netif_dbg(dev, ifup, dev->net, 828a475f603SJoe Perches "Read Value from HW_CFG after writing HW_CFG_BIR_: 0x%08x\n", 82960b86755SJoe Perches read_buf); 8302f7ca802SSteve Glendinning 8312f7ca802SSteve Glendinning if (!turbo_mode) { 8322f7ca802SSteve Glendinning burst_cap = 0; 8332f7ca802SSteve Glendinning dev->rx_urb_size = MAX_SINGLE_PACKET_SIZE; 8342f7ca802SSteve Glendinning } else if (dev->udev->speed == USB_SPEED_HIGH) { 8352f7ca802SSteve Glendinning burst_cap = DEFAULT_HS_BURST_CAP_SIZE / HS_USB_PKT_SIZE; 8362f7ca802SSteve Glendinning dev->rx_urb_size = DEFAULT_HS_BURST_CAP_SIZE; 8372f7ca802SSteve Glendinning } else { 8382f7ca802SSteve Glendinning burst_cap = DEFAULT_FS_BURST_CAP_SIZE / FS_USB_PKT_SIZE; 8392f7ca802SSteve Glendinning dev->rx_urb_size = DEFAULT_FS_BURST_CAP_SIZE; 8402f7ca802SSteve Glendinning } 8412f7ca802SSteve Glendinning 842a475f603SJoe Perches netif_dbg(dev, ifup, dev->net, 843a475f603SJoe Perches "rx_urb_size=%ld\n", (ulong)dev->rx_urb_size); 8442f7ca802SSteve Glendinning 8452f7ca802SSteve Glendinning ret = smsc95xx_write_reg(dev, BURST_CAP, burst_cap); 8462f7ca802SSteve Glendinning if (ret < 0) { 84760b86755SJoe Perches netdev_warn(dev->net, "Failed to write BURST_CAP: %d\n", ret); 8482f7ca802SSteve Glendinning return ret; 8492f7ca802SSteve Glendinning } 8502f7ca802SSteve Glendinning 8512f7ca802SSteve Glendinning ret = smsc95xx_read_reg(dev, BURST_CAP, &read_buf); 8522f7ca802SSteve Glendinning if (ret < 0) { 85360b86755SJoe Perches netdev_warn(dev->net, "Failed to read BURST_CAP: %d\n", ret); 8542f7ca802SSteve Glendinning return ret; 8552f7ca802SSteve Glendinning } 856a475f603SJoe Perches netif_dbg(dev, ifup, dev->net, 857a475f603SJoe Perches "Read Value from BURST_CAP after writing: 0x%08x\n", 8582f7ca802SSteve Glendinning read_buf); 8592f7ca802SSteve Glendinning 860*4436761bSSteve Glendinning ret = smsc95xx_write_reg(dev, BULK_IN_DLY, DEFAULT_BULK_IN_DELAY); 8612f7ca802SSteve Glendinning if (ret < 0) { 86260b86755SJoe Perches netdev_warn(dev->net, "ret = %d\n", ret); 8632f7ca802SSteve Glendinning return ret; 8642f7ca802SSteve Glendinning } 8652f7ca802SSteve Glendinning 8662f7ca802SSteve Glendinning ret = smsc95xx_read_reg(dev, BULK_IN_DLY, &read_buf); 8672f7ca802SSteve Glendinning if (ret < 0) { 86860b86755SJoe Perches netdev_warn(dev->net, "Failed to read BULK_IN_DLY: %d\n", ret); 8692f7ca802SSteve Glendinning return ret; 8702f7ca802SSteve Glendinning } 871a475f603SJoe Perches netif_dbg(dev, ifup, dev->net, 872a475f603SJoe Perches "Read Value from BULK_IN_DLY after writing: 0x%08x\n", 87360b86755SJoe Perches read_buf); 8742f7ca802SSteve Glendinning 8752f7ca802SSteve Glendinning ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf); 8762f7ca802SSteve Glendinning if (ret < 0) { 87760b86755SJoe Perches netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret); 8782f7ca802SSteve Glendinning return ret; 8792f7ca802SSteve Glendinning } 880a475f603SJoe Perches netif_dbg(dev, ifup, dev->net, 881a475f603SJoe Perches "Read Value from HW_CFG: 0x%08x\n", read_buf); 8822f7ca802SSteve Glendinning 8832f7ca802SSteve Glendinning if (turbo_mode) 8842f7ca802SSteve Glendinning read_buf |= (HW_CFG_MEF_ | HW_CFG_BCE_); 8852f7ca802SSteve Glendinning 8862f7ca802SSteve Glendinning read_buf &= ~HW_CFG_RXDOFF_; 8872f7ca802SSteve Glendinning 8882f7ca802SSteve Glendinning /* set Rx data offset=2, Make IP header aligns on word boundary. */ 8892f7ca802SSteve Glendinning read_buf |= NET_IP_ALIGN << 9; 8902f7ca802SSteve Glendinning 8912f7ca802SSteve Glendinning ret = smsc95xx_write_reg(dev, HW_CFG, read_buf); 8922f7ca802SSteve Glendinning if (ret < 0) { 89360b86755SJoe Perches netdev_warn(dev->net, "Failed to write HW_CFG register, ret=%d\n", 89460b86755SJoe Perches ret); 8952f7ca802SSteve Glendinning return ret; 8962f7ca802SSteve Glendinning } 8972f7ca802SSteve Glendinning 8982f7ca802SSteve Glendinning ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf); 8992f7ca802SSteve Glendinning if (ret < 0) { 90060b86755SJoe Perches netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret); 9012f7ca802SSteve Glendinning return ret; 9022f7ca802SSteve Glendinning } 903a475f603SJoe Perches netif_dbg(dev, ifup, dev->net, 904a475f603SJoe Perches "Read Value from HW_CFG after writing: 0x%08x\n", read_buf); 9052f7ca802SSteve Glendinning 906*4436761bSSteve Glendinning ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_); 9072f7ca802SSteve Glendinning if (ret < 0) { 90860b86755SJoe Perches netdev_warn(dev->net, "Failed to write INT_STS register, ret=%d\n", 90960b86755SJoe Perches ret); 9102f7ca802SSteve Glendinning return ret; 9112f7ca802SSteve Glendinning } 9122f7ca802SSteve Glendinning 9132f7ca802SSteve Glendinning ret = smsc95xx_read_reg(dev, ID_REV, &read_buf); 9142f7ca802SSteve Glendinning if (ret < 0) { 91560b86755SJoe Perches netdev_warn(dev->net, "Failed to read ID_REV: %d\n", ret); 9162f7ca802SSteve Glendinning return ret; 9172f7ca802SSteve Glendinning } 918a475f603SJoe Perches netif_dbg(dev, ifup, dev->net, "ID_REV = 0x%08x\n", read_buf); 9192f7ca802SSteve Glendinning 920f293501cSSteve Glendinning /* Configure GPIO pins as LED outputs */ 921f293501cSSteve Glendinning write_buf = LED_GPIO_CFG_SPD_LED | LED_GPIO_CFG_LNK_LED | 922f293501cSSteve Glendinning LED_GPIO_CFG_FDX_LED; 923f293501cSSteve Glendinning ret = smsc95xx_write_reg(dev, LED_GPIO_CFG, write_buf); 924f293501cSSteve Glendinning if (ret < 0) { 92560b86755SJoe Perches netdev_warn(dev->net, "Failed to write LED_GPIO_CFG register, ret=%d\n", 926f293501cSSteve Glendinning ret); 927f293501cSSteve Glendinning return ret; 928f293501cSSteve Glendinning } 929f293501cSSteve Glendinning 9302f7ca802SSteve Glendinning /* Init Tx */ 931*4436761bSSteve Glendinning ret = smsc95xx_write_reg(dev, FLOW, 0); 9322f7ca802SSteve Glendinning if (ret < 0) { 93360b86755SJoe Perches netdev_warn(dev->net, "Failed to write FLOW: %d\n", ret); 9342f7ca802SSteve Glendinning return ret; 9352f7ca802SSteve Glendinning } 9362f7ca802SSteve Glendinning 937*4436761bSSteve Glendinning ret = smsc95xx_write_reg(dev, AFC_CFG, AFC_CFG_DEFAULT); 9382f7ca802SSteve Glendinning if (ret < 0) { 93960b86755SJoe Perches netdev_warn(dev->net, "Failed to write AFC_CFG: %d\n", ret); 9402f7ca802SSteve Glendinning return ret; 9412f7ca802SSteve Glendinning } 9422f7ca802SSteve Glendinning 9432f7ca802SSteve Glendinning /* Don't need mac_cr_lock during initialisation */ 9442f7ca802SSteve Glendinning ret = smsc95xx_read_reg(dev, MAC_CR, &pdata->mac_cr); 9452f7ca802SSteve Glendinning if (ret < 0) { 94660b86755SJoe Perches netdev_warn(dev->net, "Failed to read MAC_CR: %d\n", ret); 9472f7ca802SSteve Glendinning return ret; 9482f7ca802SSteve Glendinning } 9492f7ca802SSteve Glendinning 9502f7ca802SSteve Glendinning /* Init Rx */ 9512f7ca802SSteve Glendinning /* Set Vlan */ 952*4436761bSSteve Glendinning ret = smsc95xx_write_reg(dev, VLAN1, (u32)ETH_P_8021Q); 9532f7ca802SSteve Glendinning if (ret < 0) { 95460b86755SJoe Perches netdev_warn(dev->net, "Failed to write VAN1: %d\n", ret); 9552f7ca802SSteve Glendinning return ret; 9562f7ca802SSteve Glendinning } 9572f7ca802SSteve Glendinning 958f7b29271SSteve Glendinning /* Enable or disable checksum offload engines */ 95978e47fe4SMichał Mirosław smsc95xx_set_features(dev->net, dev->net->features); 9602f7ca802SSteve Glendinning 9612f7ca802SSteve Glendinning smsc95xx_set_multicast(dev->net); 9622f7ca802SSteve Glendinning 9632f7ca802SSteve Glendinning if (smsc95xx_phy_initialize(dev) < 0) 9642f7ca802SSteve Glendinning return -EIO; 9652f7ca802SSteve Glendinning 9662f7ca802SSteve Glendinning ret = smsc95xx_read_reg(dev, INT_EP_CTL, &read_buf); 9672f7ca802SSteve Glendinning if (ret < 0) { 96860b86755SJoe Perches netdev_warn(dev->net, "Failed to read INT_EP_CTL: %d\n", ret); 9692f7ca802SSteve Glendinning return ret; 9702f7ca802SSteve Glendinning } 9712f7ca802SSteve Glendinning 9722f7ca802SSteve Glendinning /* enable PHY interrupts */ 9732f7ca802SSteve Glendinning read_buf |= INT_EP_CTL_PHY_INT_; 9742f7ca802SSteve Glendinning 9752f7ca802SSteve Glendinning ret = smsc95xx_write_reg(dev, INT_EP_CTL, read_buf); 9762f7ca802SSteve Glendinning if (ret < 0) { 97760b86755SJoe Perches netdev_warn(dev->net, "Failed to write INT_EP_CTL: %d\n", ret); 9782f7ca802SSteve Glendinning return ret; 9792f7ca802SSteve Glendinning } 9802f7ca802SSteve Glendinning 9812f7ca802SSteve Glendinning smsc95xx_start_tx_path(dev); 9822f7ca802SSteve Glendinning smsc95xx_start_rx_path(dev); 9832f7ca802SSteve Glendinning 984a475f603SJoe Perches netif_dbg(dev, ifup, dev->net, "smsc95xx_reset, return 0\n"); 9852f7ca802SSteve Glendinning return 0; 9862f7ca802SSteve Glendinning } 9872f7ca802SSteve Glendinning 98863e77b39SStephen Hemminger static const struct net_device_ops smsc95xx_netdev_ops = { 98963e77b39SStephen Hemminger .ndo_open = usbnet_open, 99063e77b39SStephen Hemminger .ndo_stop = usbnet_stop, 99163e77b39SStephen Hemminger .ndo_start_xmit = usbnet_start_xmit, 99263e77b39SStephen Hemminger .ndo_tx_timeout = usbnet_tx_timeout, 99363e77b39SStephen Hemminger .ndo_change_mtu = usbnet_change_mtu, 99463e77b39SStephen Hemminger .ndo_set_mac_address = eth_mac_addr, 99563e77b39SStephen Hemminger .ndo_validate_addr = eth_validate_addr, 99663e77b39SStephen Hemminger .ndo_do_ioctl = smsc95xx_ioctl, 997afc4b13dSJiri Pirko .ndo_set_rx_mode = smsc95xx_set_multicast, 99878e47fe4SMichał Mirosław .ndo_set_features = smsc95xx_set_features, 99963e77b39SStephen Hemminger }; 100063e77b39SStephen Hemminger 10012f7ca802SSteve Glendinning static int smsc95xx_bind(struct usbnet *dev, struct usb_interface *intf) 10022f7ca802SSteve Glendinning { 10032f7ca802SSteve Glendinning struct smsc95xx_priv *pdata = NULL; 10042f7ca802SSteve Glendinning int ret; 10052f7ca802SSteve Glendinning 10062f7ca802SSteve Glendinning printk(KERN_INFO SMSC_CHIPNAME " v" SMSC_DRIVER_VERSION "\n"); 10072f7ca802SSteve Glendinning 10082f7ca802SSteve Glendinning ret = usbnet_get_endpoints(dev, intf); 10092f7ca802SSteve Glendinning if (ret < 0) { 101060b86755SJoe Perches netdev_warn(dev->net, "usbnet_get_endpoints failed: %d\n", ret); 10112f7ca802SSteve Glendinning return ret; 10122f7ca802SSteve Glendinning } 10132f7ca802SSteve Glendinning 10142f7ca802SSteve Glendinning dev->data[0] = (unsigned long)kzalloc(sizeof(struct smsc95xx_priv), 10152f7ca802SSteve Glendinning GFP_KERNEL); 10162f7ca802SSteve Glendinning 10172f7ca802SSteve Glendinning pdata = (struct smsc95xx_priv *)(dev->data[0]); 10182f7ca802SSteve Glendinning if (!pdata) { 101960b86755SJoe Perches netdev_warn(dev->net, "Unable to allocate struct smsc95xx_priv\n"); 10202f7ca802SSteve Glendinning return -ENOMEM; 10212f7ca802SSteve Glendinning } 10222f7ca802SSteve Glendinning 10232f7ca802SSteve Glendinning spin_lock_init(&pdata->mac_cr_lock); 10242f7ca802SSteve Glendinning 102578e47fe4SMichał Mirosław if (DEFAULT_TX_CSUM_ENABLE) 102678e47fe4SMichał Mirosław dev->net->features |= NETIF_F_HW_CSUM; 102778e47fe4SMichał Mirosław if (DEFAULT_RX_CSUM_ENABLE) 102878e47fe4SMichał Mirosław dev->net->features |= NETIF_F_RXCSUM; 102978e47fe4SMichał Mirosław 103078e47fe4SMichał Mirosław dev->net->hw_features = NETIF_F_HW_CSUM | NETIF_F_RXCSUM; 10312f7ca802SSteve Glendinning 1032f4e8ab7cSBernard Blackham smsc95xx_init_mac_address(dev); 1033f4e8ab7cSBernard Blackham 10342f7ca802SSteve Glendinning /* Init all registers */ 10352f7ca802SSteve Glendinning ret = smsc95xx_reset(dev); 10362f7ca802SSteve Glendinning 103763e77b39SStephen Hemminger dev->net->netdev_ops = &smsc95xx_netdev_ops; 10382f7ca802SSteve Glendinning dev->net->ethtool_ops = &smsc95xx_ethtool_ops; 10392f7ca802SSteve Glendinning dev->net->flags |= IFF_MULTICAST; 104078e47fe4SMichał Mirosław dev->net->hard_header_len += SMSC95XX_TX_OVERHEAD_CSUM; 10419bbf5660SStephane Fillod dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len; 10422f7ca802SSteve Glendinning return 0; 10432f7ca802SSteve Glendinning } 10442f7ca802SSteve Glendinning 10452f7ca802SSteve Glendinning static void smsc95xx_unbind(struct usbnet *dev, struct usb_interface *intf) 10462f7ca802SSteve Glendinning { 10472f7ca802SSteve Glendinning struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]); 10482f7ca802SSteve Glendinning if (pdata) { 1049a475f603SJoe Perches netif_dbg(dev, ifdown, dev->net, "free pdata\n"); 10502f7ca802SSteve Glendinning kfree(pdata); 10512f7ca802SSteve Glendinning pdata = NULL; 10522f7ca802SSteve Glendinning dev->data[0] = 0; 10532f7ca802SSteve Glendinning } 10542f7ca802SSteve Glendinning } 10552f7ca802SSteve Glendinning 10562f7ca802SSteve Glendinning static void smsc95xx_rx_csum_offload(struct sk_buff *skb) 10572f7ca802SSteve Glendinning { 10582f7ca802SSteve Glendinning skb->csum = *(u16 *)(skb_tail_pointer(skb) - 2); 10592f7ca802SSteve Glendinning skb->ip_summed = CHECKSUM_COMPLETE; 10602f7ca802SSteve Glendinning skb_trim(skb, skb->len - 2); 10612f7ca802SSteve Glendinning } 10622f7ca802SSteve Glendinning 10632f7ca802SSteve Glendinning static int smsc95xx_rx_fixup(struct usbnet *dev, struct sk_buff *skb) 10642f7ca802SSteve Glendinning { 10652f7ca802SSteve Glendinning while (skb->len > 0) { 10662f7ca802SSteve Glendinning u32 header, align_count; 10672f7ca802SSteve Glendinning struct sk_buff *ax_skb; 10682f7ca802SSteve Glendinning unsigned char *packet; 10692f7ca802SSteve Glendinning u16 size; 10702f7ca802SSteve Glendinning 10712f7ca802SSteve Glendinning memcpy(&header, skb->data, sizeof(header)); 10722f7ca802SSteve Glendinning le32_to_cpus(&header); 10732f7ca802SSteve Glendinning skb_pull(skb, 4 + NET_IP_ALIGN); 10742f7ca802SSteve Glendinning packet = skb->data; 10752f7ca802SSteve Glendinning 10762f7ca802SSteve Glendinning /* get the packet length */ 10772f7ca802SSteve Glendinning size = (u16)((header & RX_STS_FL_) >> 16); 10782f7ca802SSteve Glendinning align_count = (4 - ((size + NET_IP_ALIGN) % 4)) % 4; 10792f7ca802SSteve Glendinning 10802f7ca802SSteve Glendinning if (unlikely(header & RX_STS_ES_)) { 1081a475f603SJoe Perches netif_dbg(dev, rx_err, dev->net, 1082a475f603SJoe Perches "Error header=0x%08x\n", header); 108380667ac1SHerbert Xu dev->net->stats.rx_errors++; 108480667ac1SHerbert Xu dev->net->stats.rx_dropped++; 10852f7ca802SSteve Glendinning 10862f7ca802SSteve Glendinning if (header & RX_STS_CRC_) { 108780667ac1SHerbert Xu dev->net->stats.rx_crc_errors++; 10882f7ca802SSteve Glendinning } else { 10892f7ca802SSteve Glendinning if (header & (RX_STS_TL_ | RX_STS_RF_)) 109080667ac1SHerbert Xu dev->net->stats.rx_frame_errors++; 10912f7ca802SSteve Glendinning 10922f7ca802SSteve Glendinning if ((header & RX_STS_LE_) && 10932f7ca802SSteve Glendinning (!(header & RX_STS_FT_))) 109480667ac1SHerbert Xu dev->net->stats.rx_length_errors++; 10952f7ca802SSteve Glendinning } 10962f7ca802SSteve Glendinning } else { 10972f7ca802SSteve Glendinning /* ETH_FRAME_LEN + 4(CRC) + 2(COE) + 4(Vlan) */ 10982f7ca802SSteve Glendinning if (unlikely(size > (ETH_FRAME_LEN + 12))) { 1099a475f603SJoe Perches netif_dbg(dev, rx_err, dev->net, 1100a475f603SJoe Perches "size err header=0x%08x\n", header); 11012f7ca802SSteve Glendinning return 0; 11022f7ca802SSteve Glendinning } 11032f7ca802SSteve Glendinning 11042f7ca802SSteve Glendinning /* last frame in this batch */ 11052f7ca802SSteve Glendinning if (skb->len == size) { 110678e47fe4SMichał Mirosław if (dev->net->features & NETIF_F_RXCSUM) 11072f7ca802SSteve Glendinning smsc95xx_rx_csum_offload(skb); 1108df18accaSPeter Korsgaard skb_trim(skb, skb->len - 4); /* remove fcs */ 11092f7ca802SSteve Glendinning skb->truesize = size + sizeof(struct sk_buff); 11102f7ca802SSteve Glendinning 11112f7ca802SSteve Glendinning return 1; 11122f7ca802SSteve Glendinning } 11132f7ca802SSteve Glendinning 11142f7ca802SSteve Glendinning ax_skb = skb_clone(skb, GFP_ATOMIC); 11152f7ca802SSteve Glendinning if (unlikely(!ax_skb)) { 111660b86755SJoe Perches netdev_warn(dev->net, "Error allocating skb\n"); 11172f7ca802SSteve Glendinning return 0; 11182f7ca802SSteve Glendinning } 11192f7ca802SSteve Glendinning 11202f7ca802SSteve Glendinning ax_skb->len = size; 11212f7ca802SSteve Glendinning ax_skb->data = packet; 11222f7ca802SSteve Glendinning skb_set_tail_pointer(ax_skb, size); 11232f7ca802SSteve Glendinning 112478e47fe4SMichał Mirosław if (dev->net->features & NETIF_F_RXCSUM) 11252f7ca802SSteve Glendinning smsc95xx_rx_csum_offload(ax_skb); 1126df18accaSPeter Korsgaard skb_trim(ax_skb, ax_skb->len - 4); /* remove fcs */ 11272f7ca802SSteve Glendinning ax_skb->truesize = size + sizeof(struct sk_buff); 11282f7ca802SSteve Glendinning 11292f7ca802SSteve Glendinning usbnet_skb_return(dev, ax_skb); 11302f7ca802SSteve Glendinning } 11312f7ca802SSteve Glendinning 11322f7ca802SSteve Glendinning skb_pull(skb, size); 11332f7ca802SSteve Glendinning 11342f7ca802SSteve Glendinning /* padding bytes before the next frame starts */ 11352f7ca802SSteve Glendinning if (skb->len) 11362f7ca802SSteve Glendinning skb_pull(skb, align_count); 11372f7ca802SSteve Glendinning } 11382f7ca802SSteve Glendinning 11392f7ca802SSteve Glendinning if (unlikely(skb->len < 0)) { 114060b86755SJoe Perches netdev_warn(dev->net, "invalid rx length<0 %d\n", skb->len); 11412f7ca802SSteve Glendinning return 0; 11422f7ca802SSteve Glendinning } 11432f7ca802SSteve Glendinning 11442f7ca802SSteve Glendinning return 1; 11452f7ca802SSteve Glendinning } 11462f7ca802SSteve Glendinning 1147f7b29271SSteve Glendinning static u32 smsc95xx_calc_csum_preamble(struct sk_buff *skb) 1148f7b29271SSteve Glendinning { 114955508d60SMichał Mirosław u16 low_16 = (u16)skb_checksum_start_offset(skb); 115055508d60SMichał Mirosław u16 high_16 = low_16 + skb->csum_offset; 1151f7b29271SSteve Glendinning return (high_16 << 16) | low_16; 1152f7b29271SSteve Glendinning } 1153f7b29271SSteve Glendinning 11542f7ca802SSteve Glendinning static struct sk_buff *smsc95xx_tx_fixup(struct usbnet *dev, 11552f7ca802SSteve Glendinning struct sk_buff *skb, gfp_t flags) 11562f7ca802SSteve Glendinning { 115778e47fe4SMichał Mirosław bool csum = skb->ip_summed == CHECKSUM_PARTIAL; 1158f7b29271SSteve Glendinning int overhead = csum ? SMSC95XX_TX_OVERHEAD_CSUM : SMSC95XX_TX_OVERHEAD; 11592f7ca802SSteve Glendinning u32 tx_cmd_a, tx_cmd_b; 11602f7ca802SSteve Glendinning 1161f7b29271SSteve Glendinning /* We do not advertise SG, so skbs should be already linearized */ 1162f7b29271SSteve Glendinning BUG_ON(skb_shinfo(skb)->nr_frags); 1163f7b29271SSteve Glendinning 1164f7b29271SSteve Glendinning if (skb_headroom(skb) < overhead) { 11652f7ca802SSteve Glendinning struct sk_buff *skb2 = skb_copy_expand(skb, 1166f7b29271SSteve Glendinning overhead, 0, flags); 11672f7ca802SSteve Glendinning dev_kfree_skb_any(skb); 11682f7ca802SSteve Glendinning skb = skb2; 11692f7ca802SSteve Glendinning if (!skb) 11702f7ca802SSteve Glendinning return NULL; 11712f7ca802SSteve Glendinning } 11722f7ca802SSteve Glendinning 1173f7b29271SSteve Glendinning if (csum) { 117411bc3088SSteve Glendinning if (skb->len <= 45) { 117511bc3088SSteve Glendinning /* workaround - hardware tx checksum does not work 117611bc3088SSteve Glendinning * properly with extremely small packets */ 117755508d60SMichał Mirosław long csstart = skb_checksum_start_offset(skb); 117811bc3088SSteve Glendinning __wsum calc = csum_partial(skb->data + csstart, 117911bc3088SSteve Glendinning skb->len - csstart, 0); 118011bc3088SSteve Glendinning *((__sum16 *)(skb->data + csstart 118111bc3088SSteve Glendinning + skb->csum_offset)) = csum_fold(calc); 118211bc3088SSteve Glendinning 118311bc3088SSteve Glendinning csum = false; 118411bc3088SSteve Glendinning } else { 1185f7b29271SSteve Glendinning u32 csum_preamble = smsc95xx_calc_csum_preamble(skb); 1186f7b29271SSteve Glendinning skb_push(skb, 4); 1187f7b29271SSteve Glendinning memcpy(skb->data, &csum_preamble, 4); 1188f7b29271SSteve Glendinning } 118911bc3088SSteve Glendinning } 1190f7b29271SSteve Glendinning 11912f7ca802SSteve Glendinning skb_push(skb, 4); 11922f7ca802SSteve Glendinning tx_cmd_b = (u32)(skb->len - 4); 1193f7b29271SSteve Glendinning if (csum) 1194f7b29271SSteve Glendinning tx_cmd_b |= TX_CMD_B_CSUM_ENABLE; 11952f7ca802SSteve Glendinning cpu_to_le32s(&tx_cmd_b); 11962f7ca802SSteve Glendinning memcpy(skb->data, &tx_cmd_b, 4); 11972f7ca802SSteve Glendinning 11982f7ca802SSteve Glendinning skb_push(skb, 4); 11992f7ca802SSteve Glendinning tx_cmd_a = (u32)(skb->len - 8) | TX_CMD_A_FIRST_SEG_ | 12002f7ca802SSteve Glendinning TX_CMD_A_LAST_SEG_; 12012f7ca802SSteve Glendinning cpu_to_le32s(&tx_cmd_a); 12022f7ca802SSteve Glendinning memcpy(skb->data, &tx_cmd_a, 4); 12032f7ca802SSteve Glendinning 12042f7ca802SSteve Glendinning return skb; 12052f7ca802SSteve Glendinning } 12062f7ca802SSteve Glendinning 12072f7ca802SSteve Glendinning static const struct driver_info smsc95xx_info = { 12082f7ca802SSteve Glendinning .description = "smsc95xx USB 2.0 Ethernet", 12092f7ca802SSteve Glendinning .bind = smsc95xx_bind, 12102f7ca802SSteve Glendinning .unbind = smsc95xx_unbind, 12112f7ca802SSteve Glendinning .link_reset = smsc95xx_link_reset, 12122f7ca802SSteve Glendinning .reset = smsc95xx_reset, 12132f7ca802SSteve Glendinning .rx_fixup = smsc95xx_rx_fixup, 12142f7ca802SSteve Glendinning .tx_fixup = smsc95xx_tx_fixup, 12152f7ca802SSteve Glendinning .status = smsc95xx_status, 121607d69d42SPaolo Pisati .flags = FLAG_ETHER | FLAG_SEND_ZLP | FLAG_LINK_INTR, 12172f7ca802SSteve Glendinning }; 12182f7ca802SSteve Glendinning 12192f7ca802SSteve Glendinning static const struct usb_device_id products[] = { 12202f7ca802SSteve Glendinning { 12212f7ca802SSteve Glendinning /* SMSC9500 USB Ethernet Device */ 12222f7ca802SSteve Glendinning USB_DEVICE(0x0424, 0x9500), 12232f7ca802SSteve Glendinning .driver_info = (unsigned long) &smsc95xx_info, 12242f7ca802SSteve Glendinning }, 1225726474b8SSteve Glendinning { 12266f41d12bSSteve Glendinning /* SMSC9505 USB Ethernet Device */ 12276f41d12bSSteve Glendinning USB_DEVICE(0x0424, 0x9505), 12286f41d12bSSteve Glendinning .driver_info = (unsigned long) &smsc95xx_info, 12296f41d12bSSteve Glendinning }, 12306f41d12bSSteve Glendinning { 12316f41d12bSSteve Glendinning /* SMSC9500A USB Ethernet Device */ 12326f41d12bSSteve Glendinning USB_DEVICE(0x0424, 0x9E00), 12336f41d12bSSteve Glendinning .driver_info = (unsigned long) &smsc95xx_info, 12346f41d12bSSteve Glendinning }, 12356f41d12bSSteve Glendinning { 12366f41d12bSSteve Glendinning /* SMSC9505A USB Ethernet Device */ 12376f41d12bSSteve Glendinning USB_DEVICE(0x0424, 0x9E01), 12386f41d12bSSteve Glendinning .driver_info = (unsigned long) &smsc95xx_info, 12396f41d12bSSteve Glendinning }, 12406f41d12bSSteve Glendinning { 1241726474b8SSteve Glendinning /* SMSC9512/9514 USB Hub & Ethernet Device */ 1242726474b8SSteve Glendinning USB_DEVICE(0x0424, 0xec00), 1243726474b8SSteve Glendinning .driver_info = (unsigned long) &smsc95xx_info, 1244726474b8SSteve Glendinning }, 12456f41d12bSSteve Glendinning { 12466f41d12bSSteve Glendinning /* SMSC9500 USB Ethernet Device (SAL10) */ 12476f41d12bSSteve Glendinning USB_DEVICE(0x0424, 0x9900), 12486f41d12bSSteve Glendinning .driver_info = (unsigned long) &smsc95xx_info, 12496f41d12bSSteve Glendinning }, 12506f41d12bSSteve Glendinning { 12516f41d12bSSteve Glendinning /* SMSC9505 USB Ethernet Device (SAL10) */ 12526f41d12bSSteve Glendinning USB_DEVICE(0x0424, 0x9901), 12536f41d12bSSteve Glendinning .driver_info = (unsigned long) &smsc95xx_info, 12546f41d12bSSteve Glendinning }, 12556f41d12bSSteve Glendinning { 12566f41d12bSSteve Glendinning /* SMSC9500A USB Ethernet Device (SAL10) */ 12576f41d12bSSteve Glendinning USB_DEVICE(0x0424, 0x9902), 12586f41d12bSSteve Glendinning .driver_info = (unsigned long) &smsc95xx_info, 12596f41d12bSSteve Glendinning }, 12606f41d12bSSteve Glendinning { 12616f41d12bSSteve Glendinning /* SMSC9505A USB Ethernet Device (SAL10) */ 12626f41d12bSSteve Glendinning USB_DEVICE(0x0424, 0x9903), 12636f41d12bSSteve Glendinning .driver_info = (unsigned long) &smsc95xx_info, 12646f41d12bSSteve Glendinning }, 12656f41d12bSSteve Glendinning { 12666f41d12bSSteve Glendinning /* SMSC9512/9514 USB Hub & Ethernet Device (SAL10) */ 12676f41d12bSSteve Glendinning USB_DEVICE(0x0424, 0x9904), 12686f41d12bSSteve Glendinning .driver_info = (unsigned long) &smsc95xx_info, 12696f41d12bSSteve Glendinning }, 12706f41d12bSSteve Glendinning { 12716f41d12bSSteve Glendinning /* SMSC9500A USB Ethernet Device (HAL) */ 12726f41d12bSSteve Glendinning USB_DEVICE(0x0424, 0x9905), 12736f41d12bSSteve Glendinning .driver_info = (unsigned long) &smsc95xx_info, 12746f41d12bSSteve Glendinning }, 12756f41d12bSSteve Glendinning { 12766f41d12bSSteve Glendinning /* SMSC9505A USB Ethernet Device (HAL) */ 12776f41d12bSSteve Glendinning USB_DEVICE(0x0424, 0x9906), 12786f41d12bSSteve Glendinning .driver_info = (unsigned long) &smsc95xx_info, 12796f41d12bSSteve Glendinning }, 12806f41d12bSSteve Glendinning { 12816f41d12bSSteve Glendinning /* SMSC9500 USB Ethernet Device (Alternate ID) */ 12826f41d12bSSteve Glendinning USB_DEVICE(0x0424, 0x9907), 12836f41d12bSSteve Glendinning .driver_info = (unsigned long) &smsc95xx_info, 12846f41d12bSSteve Glendinning }, 12856f41d12bSSteve Glendinning { 12866f41d12bSSteve Glendinning /* SMSC9500A USB Ethernet Device (Alternate ID) */ 12876f41d12bSSteve Glendinning USB_DEVICE(0x0424, 0x9908), 12886f41d12bSSteve Glendinning .driver_info = (unsigned long) &smsc95xx_info, 12896f41d12bSSteve Glendinning }, 12906f41d12bSSteve Glendinning { 12916f41d12bSSteve Glendinning /* SMSC9512/9514 USB Hub & Ethernet Device (Alternate ID) */ 12926f41d12bSSteve Glendinning USB_DEVICE(0x0424, 0x9909), 12936f41d12bSSteve Glendinning .driver_info = (unsigned long) &smsc95xx_info, 12946f41d12bSSteve Glendinning }, 129588edaa41SSteve Glendinning { 129688edaa41SSteve Glendinning /* SMSC LAN9530 USB Ethernet Device */ 129788edaa41SSteve Glendinning USB_DEVICE(0x0424, 0x9530), 129888edaa41SSteve Glendinning .driver_info = (unsigned long) &smsc95xx_info, 129988edaa41SSteve Glendinning }, 130088edaa41SSteve Glendinning { 130188edaa41SSteve Glendinning /* SMSC LAN9730 USB Ethernet Device */ 130288edaa41SSteve Glendinning USB_DEVICE(0x0424, 0x9730), 130388edaa41SSteve Glendinning .driver_info = (unsigned long) &smsc95xx_info, 130488edaa41SSteve Glendinning }, 130588edaa41SSteve Glendinning { 130688edaa41SSteve Glendinning /* SMSC LAN89530 USB Ethernet Device */ 130788edaa41SSteve Glendinning USB_DEVICE(0x0424, 0x9E08), 130888edaa41SSteve Glendinning .driver_info = (unsigned long) &smsc95xx_info, 130988edaa41SSteve Glendinning }, 13102f7ca802SSteve Glendinning { }, /* END */ 13112f7ca802SSteve Glendinning }; 13122f7ca802SSteve Glendinning MODULE_DEVICE_TABLE(usb, products); 13132f7ca802SSteve Glendinning 13142f7ca802SSteve Glendinning static struct usb_driver smsc95xx_driver = { 13152f7ca802SSteve Glendinning .name = "smsc95xx", 13162f7ca802SSteve Glendinning .id_table = products, 13172f7ca802SSteve Glendinning .probe = usbnet_probe, 13182f7ca802SSteve Glendinning .suspend = usbnet_suspend, 13192f7ca802SSteve Glendinning .resume = usbnet_resume, 13202f7ca802SSteve Glendinning .disconnect = usbnet_disconnect, 1321e1f12eb6SSarah Sharp .disable_hub_initiated_lpm = 1, 13222f7ca802SSteve Glendinning }; 13232f7ca802SSteve Glendinning 1324d632eb1bSGreg Kroah-Hartman module_usb_driver(smsc95xx_driver); 13252f7ca802SSteve Glendinning 13262f7ca802SSteve Glendinning MODULE_AUTHOR("Nancy Lin"); 132790b24cfbSSteve Glendinning MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>"); 13282f7ca802SSteve Glendinning MODULE_DESCRIPTION("SMSC95XX USB 2.0 Ethernet Devices"); 13292f7ca802SSteve Glendinning MODULE_LICENSE("GPL"); 1330