xref: /openbmc/linux/drivers/net/usb/smsc95xx.c (revision 72108fd26b4bea58762d266d7da6dffa5003648e)
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)
49e0e474a8SSteve Glendinning #define SUPPORTED_WAKE			(WAKE_MAGIC)
502f7ca802SSteve Glendinning 
51769ea6d8SSteve Glendinning #define check_warn(ret, fmt, args...) \
52769ea6d8SSteve Glendinning 	({ if (ret < 0) netdev_warn(dev->net, fmt, ##args); })
53769ea6d8SSteve Glendinning 
54769ea6d8SSteve Glendinning #define check_warn_return(ret, fmt, args...) \
55769ea6d8SSteve Glendinning 	({ if (ret < 0) { netdev_warn(dev->net, fmt, ##args); return ret; } })
56769ea6d8SSteve Glendinning 
57769ea6d8SSteve Glendinning #define check_warn_goto_done(ret, fmt, args...) \
58769ea6d8SSteve Glendinning 	({ if (ret < 0) { netdev_warn(dev->net, fmt, ##args); goto done; } })
59769ea6d8SSteve Glendinning 
602f7ca802SSteve Glendinning struct smsc95xx_priv {
612f7ca802SSteve Glendinning 	u32 mac_cr;
623c0f3c60SMarc Zyngier 	u32 hash_hi;
633c0f3c60SMarc Zyngier 	u32 hash_lo;
64e0e474a8SSteve Glendinning 	u32 wolopts;
652f7ca802SSteve Glendinning 	spinlock_t mac_cr_lock;
662f7ca802SSteve Glendinning };
672f7ca802SSteve Glendinning 
68eb939922SRusty Russell static bool turbo_mode = true;
692f7ca802SSteve Glendinning module_param(turbo_mode, bool, 0644);
702f7ca802SSteve Glendinning MODULE_PARM_DESC(turbo_mode, "Enable multiple frames per Rx transaction");
712f7ca802SSteve Glendinning 
72769ea6d8SSteve Glendinning static int __must_check smsc95xx_read_reg(struct usbnet *dev, u32 index,
73769ea6d8SSteve Glendinning 					  u32 *data)
742f7ca802SSteve Glendinning {
75*72108fd2SMing Lei 	u32 buf;
762f7ca802SSteve Glendinning 	int ret;
772f7ca802SSteve Glendinning 
782f7ca802SSteve Glendinning 	BUG_ON(!dev);
792f7ca802SSteve Glendinning 
80*72108fd2SMing Lei 	ret = usbnet_read_cmd(dev, USB_VENDOR_REQUEST_READ_REGISTER,
81*72108fd2SMing Lei 			      USB_DIR_IN | USB_TYPE_VENDOR |
82*72108fd2SMing Lei 			      USB_RECIP_DEVICE,
83*72108fd2SMing Lei 			      0, index, &buf, 4);
842f7ca802SSteve Glendinning 	if (unlikely(ret < 0))
8560b86755SJoe Perches 		netdev_warn(dev->net, "Failed to read register index 0x%08x\n", index);
862f7ca802SSteve Glendinning 
87*72108fd2SMing Lei 	le32_to_cpus(&buf);
88*72108fd2SMing Lei 	*data = buf;
892f7ca802SSteve Glendinning 
902f7ca802SSteve Glendinning 	return ret;
912f7ca802SSteve Glendinning }
922f7ca802SSteve Glendinning 
93769ea6d8SSteve Glendinning static int __must_check smsc95xx_write_reg(struct usbnet *dev, u32 index,
94769ea6d8SSteve Glendinning 					   u32 data)
952f7ca802SSteve Glendinning {
96*72108fd2SMing Lei 	u32 buf;
972f7ca802SSteve Glendinning 	int ret;
982f7ca802SSteve Glendinning 
992f7ca802SSteve Glendinning 	BUG_ON(!dev);
1002f7ca802SSteve Glendinning 
101*72108fd2SMing Lei 	buf = data;
102*72108fd2SMing Lei 	cpu_to_le32s(&buf);
1032f7ca802SSteve Glendinning 
1042f7ca802SSteve Glendinning 
105*72108fd2SMing Lei 	ret = usbnet_write_cmd(dev, USB_VENDOR_REQUEST_WRITE_REGISTER,
106*72108fd2SMing Lei 			       USB_DIR_OUT | USB_TYPE_VENDOR |
107*72108fd2SMing Lei 			       USB_RECIP_DEVICE,
108*72108fd2SMing Lei 			       0, index, &buf, 4);
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 	return ret;
1132f7ca802SSteve Glendinning }
1142f7ca802SSteve Glendinning 
115e0e474a8SSteve Glendinning static int smsc95xx_set_feature(struct usbnet *dev, u32 feature)
116e0e474a8SSteve Glendinning {
117e0e474a8SSteve Glendinning 	if (WARN_ON_ONCE(!dev))
118e0e474a8SSteve Glendinning 		return -EINVAL;
119e0e474a8SSteve Glendinning 
120*72108fd2SMing Lei 	return usbnet_write_cmd(dev, USB_REQ_SET_FEATURE,
121*72108fd2SMing Lei 				USB_RECIP_DEVICE, feature, 0, NULL, 0);
122e0e474a8SSteve Glendinning }
123e0e474a8SSteve Glendinning 
124e0e474a8SSteve Glendinning static int smsc95xx_clear_feature(struct usbnet *dev, u32 feature)
125e0e474a8SSteve Glendinning {
126e0e474a8SSteve Glendinning 	if (WARN_ON_ONCE(!dev))
127e0e474a8SSteve Glendinning 		return -EINVAL;
128e0e474a8SSteve Glendinning 
129*72108fd2SMing Lei 	return usbnet_write_cmd(dev, USB_REQ_CLEAR_FEATURE,
130*72108fd2SMing Lei 				USB_RECIP_DEVICE, feature, 0, NULL, 0);
131e0e474a8SSteve Glendinning }
132e0e474a8SSteve Glendinning 
1332f7ca802SSteve Glendinning /* Loop until the read is completed with timeout
1342f7ca802SSteve Glendinning  * called with phy_mutex held */
135769ea6d8SSteve Glendinning static int __must_check smsc95xx_phy_wait_not_busy(struct usbnet *dev)
1362f7ca802SSteve Glendinning {
1372f7ca802SSteve Glendinning 	unsigned long start_time = jiffies;
1382f7ca802SSteve Glendinning 	u32 val;
139769ea6d8SSteve Glendinning 	int ret;
1402f7ca802SSteve Glendinning 
1412f7ca802SSteve Glendinning 	do {
142769ea6d8SSteve Glendinning 		ret = smsc95xx_read_reg(dev, MII_ADDR, &val);
143769ea6d8SSteve Glendinning 		check_warn_return(ret, "Error reading MII_ACCESS");
1442f7ca802SSteve Glendinning 		if (!(val & MII_BUSY_))
1452f7ca802SSteve Glendinning 			return 0;
1462f7ca802SSteve Glendinning 	} while (!time_after(jiffies, start_time + HZ));
1472f7ca802SSteve Glendinning 
1482f7ca802SSteve Glendinning 	return -EIO;
1492f7ca802SSteve Glendinning }
1502f7ca802SSteve Glendinning 
1512f7ca802SSteve Glendinning static int smsc95xx_mdio_read(struct net_device *netdev, int phy_id, int idx)
1522f7ca802SSteve Glendinning {
1532f7ca802SSteve Glendinning 	struct usbnet *dev = netdev_priv(netdev);
1542f7ca802SSteve Glendinning 	u32 val, addr;
155769ea6d8SSteve Glendinning 	int ret;
1562f7ca802SSteve Glendinning 
1572f7ca802SSteve Glendinning 	mutex_lock(&dev->phy_mutex);
1582f7ca802SSteve Glendinning 
1592f7ca802SSteve Glendinning 	/* confirm MII not busy */
160769ea6d8SSteve Glendinning 	ret = smsc95xx_phy_wait_not_busy(dev);
161769ea6d8SSteve Glendinning 	check_warn_goto_done(ret, "MII is busy in smsc95xx_mdio_read");
1622f7ca802SSteve Glendinning 
1632f7ca802SSteve Glendinning 	/* set the address, index & direction (read from PHY) */
1642f7ca802SSteve Glendinning 	phy_id &= dev->mii.phy_id_mask;
1652f7ca802SSteve Glendinning 	idx &= dev->mii.reg_num_mask;
1662f7ca802SSteve Glendinning 	addr = (phy_id << 11) | (idx << 6) | MII_READ_;
167769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, MII_ADDR, addr);
168769ea6d8SSteve Glendinning 	check_warn_goto_done(ret, "Error writing MII_ADDR");
1692f7ca802SSteve Glendinning 
170769ea6d8SSteve Glendinning 	ret = smsc95xx_phy_wait_not_busy(dev);
171769ea6d8SSteve Glendinning 	check_warn_goto_done(ret, "Timed out reading MII reg %02X", idx);
172769ea6d8SSteve Glendinning 
173769ea6d8SSteve Glendinning 	ret = smsc95xx_read_reg(dev, MII_DATA, &val);
174769ea6d8SSteve Glendinning 	check_warn_goto_done(ret, "Error reading MII_DATA");
175769ea6d8SSteve Glendinning 
176769ea6d8SSteve Glendinning 	ret = (u16)(val & 0xFFFF);
177769ea6d8SSteve Glendinning 
178769ea6d8SSteve Glendinning done:
1792f7ca802SSteve Glendinning 	mutex_unlock(&dev->phy_mutex);
180769ea6d8SSteve Glendinning 	return ret;
1812f7ca802SSteve Glendinning }
1822f7ca802SSteve Glendinning 
1832f7ca802SSteve Glendinning static void smsc95xx_mdio_write(struct net_device *netdev, int phy_id, int idx,
1842f7ca802SSteve Glendinning 				int regval)
1852f7ca802SSteve Glendinning {
1862f7ca802SSteve Glendinning 	struct usbnet *dev = netdev_priv(netdev);
1872f7ca802SSteve Glendinning 	u32 val, addr;
188769ea6d8SSteve Glendinning 	int ret;
1892f7ca802SSteve Glendinning 
1902f7ca802SSteve Glendinning 	mutex_lock(&dev->phy_mutex);
1912f7ca802SSteve Glendinning 
1922f7ca802SSteve Glendinning 	/* confirm MII not busy */
193769ea6d8SSteve Glendinning 	ret = smsc95xx_phy_wait_not_busy(dev);
194769ea6d8SSteve Glendinning 	check_warn_goto_done(ret, "MII is busy in smsc95xx_mdio_write");
1952f7ca802SSteve Glendinning 
1962f7ca802SSteve Glendinning 	val = regval;
197769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, MII_DATA, val);
198769ea6d8SSteve Glendinning 	check_warn_goto_done(ret, "Error writing MII_DATA");
1992f7ca802SSteve Glendinning 
2002f7ca802SSteve Glendinning 	/* set the address, index & direction (write to PHY) */
2012f7ca802SSteve Glendinning 	phy_id &= dev->mii.phy_id_mask;
2022f7ca802SSteve Glendinning 	idx &= dev->mii.reg_num_mask;
2032f7ca802SSteve Glendinning 	addr = (phy_id << 11) | (idx << 6) | MII_WRITE_;
204769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, MII_ADDR, addr);
205769ea6d8SSteve Glendinning 	check_warn_goto_done(ret, "Error writing MII_ADDR");
2062f7ca802SSteve Glendinning 
207769ea6d8SSteve Glendinning 	ret = smsc95xx_phy_wait_not_busy(dev);
208769ea6d8SSteve Glendinning 	check_warn_goto_done(ret, "Timed out writing MII reg %02X", idx);
2092f7ca802SSteve Glendinning 
210769ea6d8SSteve Glendinning done:
2112f7ca802SSteve Glendinning 	mutex_unlock(&dev->phy_mutex);
2122f7ca802SSteve Glendinning }
2132f7ca802SSteve Glendinning 
214769ea6d8SSteve Glendinning static int __must_check smsc95xx_wait_eeprom(struct usbnet *dev)
2152f7ca802SSteve Glendinning {
2162f7ca802SSteve Glendinning 	unsigned long start_time = jiffies;
2172f7ca802SSteve Glendinning 	u32 val;
218769ea6d8SSteve Glendinning 	int ret;
2192f7ca802SSteve Glendinning 
2202f7ca802SSteve Glendinning 	do {
221769ea6d8SSteve Glendinning 		ret = smsc95xx_read_reg(dev, E2P_CMD, &val);
222769ea6d8SSteve Glendinning 		check_warn_return(ret, "Error reading E2P_CMD");
2232f7ca802SSteve Glendinning 		if (!(val & E2P_CMD_BUSY_) || (val & E2P_CMD_TIMEOUT_))
2242f7ca802SSteve Glendinning 			break;
2252f7ca802SSteve Glendinning 		udelay(40);
2262f7ca802SSteve Glendinning 	} while (!time_after(jiffies, start_time + HZ));
2272f7ca802SSteve Glendinning 
2282f7ca802SSteve Glendinning 	if (val & (E2P_CMD_TIMEOUT_ | E2P_CMD_BUSY_)) {
22960b86755SJoe Perches 		netdev_warn(dev->net, "EEPROM read operation timeout\n");
2302f7ca802SSteve Glendinning 		return -EIO;
2312f7ca802SSteve Glendinning 	}
2322f7ca802SSteve Glendinning 
2332f7ca802SSteve Glendinning 	return 0;
2342f7ca802SSteve Glendinning }
2352f7ca802SSteve Glendinning 
236769ea6d8SSteve Glendinning static int __must_check smsc95xx_eeprom_confirm_not_busy(struct usbnet *dev)
2372f7ca802SSteve Glendinning {
2382f7ca802SSteve Glendinning 	unsigned long start_time = jiffies;
2392f7ca802SSteve Glendinning 	u32 val;
240769ea6d8SSteve Glendinning 	int ret;
2412f7ca802SSteve Glendinning 
2422f7ca802SSteve Glendinning 	do {
243769ea6d8SSteve Glendinning 		ret = smsc95xx_read_reg(dev, E2P_CMD, &val);
244769ea6d8SSteve Glendinning 		check_warn_return(ret, "Error reading E2P_CMD");
2452f7ca802SSteve Glendinning 
2462f7ca802SSteve Glendinning 		if (!(val & E2P_CMD_BUSY_))
2472f7ca802SSteve Glendinning 			return 0;
2482f7ca802SSteve Glendinning 
2492f7ca802SSteve Glendinning 		udelay(40);
2502f7ca802SSteve Glendinning 	} while (!time_after(jiffies, start_time + HZ));
2512f7ca802SSteve Glendinning 
25260b86755SJoe Perches 	netdev_warn(dev->net, "EEPROM is busy\n");
2532f7ca802SSteve Glendinning 	return -EIO;
2542f7ca802SSteve Glendinning }
2552f7ca802SSteve Glendinning 
2562f7ca802SSteve Glendinning static int smsc95xx_read_eeprom(struct usbnet *dev, u32 offset, u32 length,
2572f7ca802SSteve Glendinning 				u8 *data)
2582f7ca802SSteve Glendinning {
2592f7ca802SSteve Glendinning 	u32 val;
2602f7ca802SSteve Glendinning 	int i, ret;
2612f7ca802SSteve Glendinning 
2622f7ca802SSteve Glendinning 	BUG_ON(!dev);
2632f7ca802SSteve Glendinning 	BUG_ON(!data);
2642f7ca802SSteve Glendinning 
2652f7ca802SSteve Glendinning 	ret = smsc95xx_eeprom_confirm_not_busy(dev);
2662f7ca802SSteve Glendinning 	if (ret)
2672f7ca802SSteve Glendinning 		return ret;
2682f7ca802SSteve Glendinning 
2692f7ca802SSteve Glendinning 	for (i = 0; i < length; i++) {
2702f7ca802SSteve Glendinning 		val = E2P_CMD_BUSY_ | E2P_CMD_READ_ | (offset & E2P_CMD_ADDR_);
271769ea6d8SSteve Glendinning 		ret = smsc95xx_write_reg(dev, E2P_CMD, val);
272769ea6d8SSteve Glendinning 		check_warn_return(ret, "Error writing E2P_CMD");
2732f7ca802SSteve Glendinning 
2742f7ca802SSteve Glendinning 		ret = smsc95xx_wait_eeprom(dev);
2752f7ca802SSteve Glendinning 		if (ret < 0)
2762f7ca802SSteve Glendinning 			return ret;
2772f7ca802SSteve Glendinning 
278769ea6d8SSteve Glendinning 		ret = smsc95xx_read_reg(dev, E2P_DATA, &val);
279769ea6d8SSteve Glendinning 		check_warn_return(ret, "Error reading E2P_DATA");
2802f7ca802SSteve Glendinning 
2812f7ca802SSteve Glendinning 		data[i] = val & 0xFF;
2822f7ca802SSteve Glendinning 		offset++;
2832f7ca802SSteve Glendinning 	}
2842f7ca802SSteve Glendinning 
2852f7ca802SSteve Glendinning 	return 0;
2862f7ca802SSteve Glendinning }
2872f7ca802SSteve Glendinning 
2882f7ca802SSteve Glendinning static int smsc95xx_write_eeprom(struct usbnet *dev, u32 offset, u32 length,
2892f7ca802SSteve Glendinning 				 u8 *data)
2902f7ca802SSteve Glendinning {
2912f7ca802SSteve Glendinning 	u32 val;
2922f7ca802SSteve Glendinning 	int i, ret;
2932f7ca802SSteve Glendinning 
2942f7ca802SSteve Glendinning 	BUG_ON(!dev);
2952f7ca802SSteve Glendinning 	BUG_ON(!data);
2962f7ca802SSteve Glendinning 
2972f7ca802SSteve Glendinning 	ret = smsc95xx_eeprom_confirm_not_busy(dev);
2982f7ca802SSteve Glendinning 	if (ret)
2992f7ca802SSteve Glendinning 		return ret;
3002f7ca802SSteve Glendinning 
3012f7ca802SSteve Glendinning 	/* Issue write/erase enable command */
3022f7ca802SSteve Glendinning 	val = E2P_CMD_BUSY_ | E2P_CMD_EWEN_;
303769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, E2P_CMD, val);
304769ea6d8SSteve Glendinning 	check_warn_return(ret, "Error writing E2P_DATA");
3052f7ca802SSteve Glendinning 
3062f7ca802SSteve Glendinning 	ret = smsc95xx_wait_eeprom(dev);
3072f7ca802SSteve Glendinning 	if (ret < 0)
3082f7ca802SSteve Glendinning 		return ret;
3092f7ca802SSteve Glendinning 
3102f7ca802SSteve Glendinning 	for (i = 0; i < length; i++) {
3112f7ca802SSteve Glendinning 
3122f7ca802SSteve Glendinning 		/* Fill data register */
3132f7ca802SSteve Glendinning 		val = data[i];
314769ea6d8SSteve Glendinning 		ret = smsc95xx_write_reg(dev, E2P_DATA, val);
315769ea6d8SSteve Glendinning 		check_warn_return(ret, "Error writing E2P_DATA");
3162f7ca802SSteve Glendinning 
3172f7ca802SSteve Glendinning 		/* Send "write" command */
3182f7ca802SSteve Glendinning 		val = E2P_CMD_BUSY_ | E2P_CMD_WRITE_ | (offset & E2P_CMD_ADDR_);
319769ea6d8SSteve Glendinning 		ret = smsc95xx_write_reg(dev, E2P_CMD, val);
320769ea6d8SSteve Glendinning 		check_warn_return(ret, "Error writing E2P_CMD");
3212f7ca802SSteve Glendinning 
3222f7ca802SSteve Glendinning 		ret = smsc95xx_wait_eeprom(dev);
3232f7ca802SSteve Glendinning 		if (ret < 0)
3242f7ca802SSteve Glendinning 			return ret;
3252f7ca802SSteve Glendinning 
3262f7ca802SSteve Glendinning 		offset++;
3272f7ca802SSteve Glendinning 	}
3282f7ca802SSteve Glendinning 
3292f7ca802SSteve Glendinning 	return 0;
3302f7ca802SSteve Glendinning }
3312f7ca802SSteve Glendinning 
332769ea6d8SSteve Glendinning static int __must_check smsc95xx_write_reg_async(struct usbnet *dev, u16 index,
333769ea6d8SSteve Glendinning 						 u32 *data)
3342f7ca802SSteve Glendinning {
3351d74a6bdSSteve Glendinning 	const u16 size = 4;
336*72108fd2SMing Lei 	int ret;
3372f7ca802SSteve Glendinning 
338*72108fd2SMing Lei 	ret = usbnet_write_cmd_async(dev, USB_VENDOR_REQUEST_WRITE_REGISTER,
339*72108fd2SMing Lei 				     USB_DIR_OUT | USB_TYPE_VENDOR |
340*72108fd2SMing Lei 				     USB_RECIP_DEVICE,
341*72108fd2SMing Lei 				     0, index, data, size);
342*72108fd2SMing Lei 	if (ret < 0)
343*72108fd2SMing Lei 		netdev_warn(dev->net, "Error write async cmd, sts=%d\n",
344*72108fd2SMing Lei 			    ret);
345*72108fd2SMing Lei 	return ret;
3462f7ca802SSteve Glendinning }
3472f7ca802SSteve Glendinning 
3482f7ca802SSteve Glendinning /* returns hash bit number for given MAC address
3492f7ca802SSteve Glendinning  * example:
3502f7ca802SSteve Glendinning  * 01 00 5E 00 00 01 -> returns bit number 31 */
3512f7ca802SSteve Glendinning static unsigned int smsc95xx_hash(char addr[ETH_ALEN])
3522f7ca802SSteve Glendinning {
3532f7ca802SSteve Glendinning 	return (ether_crc(ETH_ALEN, addr) >> 26) & 0x3f;
3542f7ca802SSteve Glendinning }
3552f7ca802SSteve Glendinning 
3562f7ca802SSteve Glendinning static void smsc95xx_set_multicast(struct net_device *netdev)
3572f7ca802SSteve Glendinning {
3582f7ca802SSteve Glendinning 	struct usbnet *dev = netdev_priv(netdev);
3592f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
3602f7ca802SSteve Glendinning 	unsigned long flags;
361769ea6d8SSteve Glendinning 	int ret;
3622f7ca802SSteve Glendinning 
3633c0f3c60SMarc Zyngier 	pdata->hash_hi = 0;
3643c0f3c60SMarc Zyngier 	pdata->hash_lo = 0;
3653c0f3c60SMarc Zyngier 
3662f7ca802SSteve Glendinning 	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
3672f7ca802SSteve Glendinning 
3682f7ca802SSteve Glendinning 	if (dev->net->flags & IFF_PROMISC) {
369a475f603SJoe Perches 		netif_dbg(dev, drv, dev->net, "promiscuous mode enabled\n");
3702f7ca802SSteve Glendinning 		pdata->mac_cr |= MAC_CR_PRMS_;
3712f7ca802SSteve Glendinning 		pdata->mac_cr &= ~(MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
3722f7ca802SSteve Glendinning 	} else if (dev->net->flags & IFF_ALLMULTI) {
373a475f603SJoe Perches 		netif_dbg(dev, drv, dev->net, "receive all multicast enabled\n");
3742f7ca802SSteve Glendinning 		pdata->mac_cr |= MAC_CR_MCPAS_;
3752f7ca802SSteve Glendinning 		pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_HPFILT_);
3764cd24eafSJiri Pirko 	} else if (!netdev_mc_empty(dev->net)) {
37722bedad3SJiri Pirko 		struct netdev_hw_addr *ha;
3782f7ca802SSteve Glendinning 
3792f7ca802SSteve Glendinning 		pdata->mac_cr |= MAC_CR_HPFILT_;
3802f7ca802SSteve Glendinning 		pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_);
3812f7ca802SSteve Glendinning 
38222bedad3SJiri Pirko 		netdev_for_each_mc_addr(ha, netdev) {
38322bedad3SJiri Pirko 			u32 bitnum = smsc95xx_hash(ha->addr);
3842f7ca802SSteve Glendinning 			u32 mask = 0x01 << (bitnum & 0x1F);
3852f7ca802SSteve Glendinning 			if (bitnum & 0x20)
3863c0f3c60SMarc Zyngier 				pdata->hash_hi |= mask;
3872f7ca802SSteve Glendinning 			else
3883c0f3c60SMarc Zyngier 				pdata->hash_lo |= mask;
3892f7ca802SSteve Glendinning 		}
3902f7ca802SSteve Glendinning 
391a475f603SJoe Perches 		netif_dbg(dev, drv, dev->net, "HASHH=0x%08X, HASHL=0x%08X\n",
3923c0f3c60SMarc Zyngier 				   pdata->hash_hi, pdata->hash_lo);
3932f7ca802SSteve Glendinning 	} else {
394a475f603SJoe Perches 		netif_dbg(dev, drv, dev->net, "receive own packets only\n");
3952f7ca802SSteve Glendinning 		pdata->mac_cr &=
3962f7ca802SSteve Glendinning 			~(MAC_CR_PRMS_ | MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
3972f7ca802SSteve Glendinning 	}
3982f7ca802SSteve Glendinning 
3992f7ca802SSteve Glendinning 	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
4002f7ca802SSteve Glendinning 
4012f7ca802SSteve Glendinning 	/* Initiate async writes, as we can't wait for completion here */
402769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg_async(dev, HASHH, &pdata->hash_hi);
403769ea6d8SSteve Glendinning 	check_warn(ret, "failed to initiate async write to HASHH");
404769ea6d8SSteve Glendinning 
405769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg_async(dev, HASHL, &pdata->hash_lo);
406769ea6d8SSteve Glendinning 	check_warn(ret, "failed to initiate async write to HASHL");
407769ea6d8SSteve Glendinning 
408769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg_async(dev, MAC_CR, &pdata->mac_cr);
409769ea6d8SSteve Glendinning 	check_warn(ret, "failed to initiate async write to MAC_CR");
4102f7ca802SSteve Glendinning }
4112f7ca802SSteve Glendinning 
412769ea6d8SSteve Glendinning static int smsc95xx_phy_update_flowcontrol(struct usbnet *dev, u8 duplex,
4132f7ca802SSteve Glendinning 					   u16 lcladv, u16 rmtadv)
4142f7ca802SSteve Glendinning {
4152f7ca802SSteve Glendinning 	u32 flow, afc_cfg = 0;
4162f7ca802SSteve Glendinning 
4172f7ca802SSteve Glendinning 	int ret = smsc95xx_read_reg(dev, AFC_CFG, &afc_cfg);
418769ea6d8SSteve Glendinning 	check_warn_return(ret, "Error reading AFC_CFG");
4192f7ca802SSteve Glendinning 
4202f7ca802SSteve Glendinning 	if (duplex == DUPLEX_FULL) {
421bc02ff95SSteve Glendinning 		u8 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
4222f7ca802SSteve Glendinning 
4232f7ca802SSteve Glendinning 		if (cap & FLOW_CTRL_RX)
4242f7ca802SSteve Glendinning 			flow = 0xFFFF0002;
4252f7ca802SSteve Glendinning 		else
4262f7ca802SSteve Glendinning 			flow = 0;
4272f7ca802SSteve Glendinning 
4282f7ca802SSteve Glendinning 		if (cap & FLOW_CTRL_TX)
4292f7ca802SSteve Glendinning 			afc_cfg |= 0xF;
4302f7ca802SSteve Glendinning 		else
4312f7ca802SSteve Glendinning 			afc_cfg &= ~0xF;
4322f7ca802SSteve Glendinning 
433a475f603SJoe Perches 		netif_dbg(dev, link, dev->net, "rx pause %s, tx pause %s\n",
43460b86755SJoe Perches 				   cap & FLOW_CTRL_RX ? "enabled" : "disabled",
43560b86755SJoe Perches 				   cap & FLOW_CTRL_TX ? "enabled" : "disabled");
4362f7ca802SSteve Glendinning 	} else {
437a475f603SJoe Perches 		netif_dbg(dev, link, dev->net, "half duplex\n");
4382f7ca802SSteve Glendinning 		flow = 0;
4392f7ca802SSteve Glendinning 		afc_cfg |= 0xF;
4402f7ca802SSteve Glendinning 	}
4412f7ca802SSteve Glendinning 
442769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, FLOW, flow);
443769ea6d8SSteve Glendinning 	check_warn_return(ret, "Error writing FLOW");
444769ea6d8SSteve Glendinning 
445769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, AFC_CFG, afc_cfg);
446769ea6d8SSteve Glendinning 	check_warn_return(ret, "Error writing AFC_CFG");
447769ea6d8SSteve Glendinning 
448769ea6d8SSteve Glendinning 	return 0;
4492f7ca802SSteve Glendinning }
4502f7ca802SSteve Glendinning 
4512f7ca802SSteve Glendinning static int smsc95xx_link_reset(struct usbnet *dev)
4522f7ca802SSteve Glendinning {
4532f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
4542f7ca802SSteve Glendinning 	struct mii_if_info *mii = &dev->mii;
4558ae6dacaSDavid Decotigny 	struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
4562f7ca802SSteve Glendinning 	unsigned long flags;
4572f7ca802SSteve Glendinning 	u16 lcladv, rmtadv;
458769ea6d8SSteve Glendinning 	int ret;
4592f7ca802SSteve Glendinning 
4602f7ca802SSteve Glendinning 	/* clear interrupt status */
461769ea6d8SSteve Glendinning 	ret = smsc95xx_mdio_read(dev->net, mii->phy_id, PHY_INT_SRC);
462769ea6d8SSteve Glendinning 	check_warn_return(ret, "Error reading PHY_INT_SRC");
463769ea6d8SSteve Glendinning 
464769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
465769ea6d8SSteve Glendinning 	check_warn_return(ret, "Error writing INT_STS");
4662f7ca802SSteve Glendinning 
4672f7ca802SSteve Glendinning 	mii_check_media(mii, 1, 1);
4682f7ca802SSteve Glendinning 	mii_ethtool_gset(&dev->mii, &ecmd);
4692f7ca802SSteve Glendinning 	lcladv = smsc95xx_mdio_read(dev->net, mii->phy_id, MII_ADVERTISE);
4702f7ca802SSteve Glendinning 	rmtadv = smsc95xx_mdio_read(dev->net, mii->phy_id, MII_LPA);
4712f7ca802SSteve Glendinning 
4728ae6dacaSDavid Decotigny 	netif_dbg(dev, link, dev->net,
4738ae6dacaSDavid Decotigny 		  "speed: %u duplex: %d lcladv: %04x rmtadv: %04x\n",
4748ae6dacaSDavid Decotigny 		  ethtool_cmd_speed(&ecmd), ecmd.duplex, lcladv, rmtadv);
4752f7ca802SSteve Glendinning 
4762f7ca802SSteve Glendinning 	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
4772f7ca802SSteve Glendinning 	if (ecmd.duplex != DUPLEX_FULL) {
4782f7ca802SSteve Glendinning 		pdata->mac_cr &= ~MAC_CR_FDPX_;
4792f7ca802SSteve Glendinning 		pdata->mac_cr |= MAC_CR_RCVOWN_;
4802f7ca802SSteve Glendinning 	} else {
4812f7ca802SSteve Glendinning 		pdata->mac_cr &= ~MAC_CR_RCVOWN_;
4822f7ca802SSteve Glendinning 		pdata->mac_cr |= MAC_CR_FDPX_;
4832f7ca802SSteve Glendinning 	}
4842f7ca802SSteve Glendinning 	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
4852f7ca802SSteve Glendinning 
486769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
487769ea6d8SSteve Glendinning 	check_warn_return(ret, "Error writing MAC_CR");
4882f7ca802SSteve Glendinning 
489769ea6d8SSteve Glendinning 	ret = smsc95xx_phy_update_flowcontrol(dev, ecmd.duplex, lcladv, rmtadv);
490769ea6d8SSteve Glendinning 	check_warn_return(ret, "Error updating PHY flow control");
4912f7ca802SSteve Glendinning 
4922f7ca802SSteve Glendinning 	return 0;
4932f7ca802SSteve Glendinning }
4942f7ca802SSteve Glendinning 
4952f7ca802SSteve Glendinning static void smsc95xx_status(struct usbnet *dev, struct urb *urb)
4962f7ca802SSteve Glendinning {
4972f7ca802SSteve Glendinning 	u32 intdata;
4982f7ca802SSteve Glendinning 
4992f7ca802SSteve Glendinning 	if (urb->actual_length != 4) {
50060b86755SJoe Perches 		netdev_warn(dev->net, "unexpected urb length %d\n",
50160b86755SJoe Perches 			    urb->actual_length);
5022f7ca802SSteve Glendinning 		return;
5032f7ca802SSteve Glendinning 	}
5042f7ca802SSteve Glendinning 
5052f7ca802SSteve Glendinning 	memcpy(&intdata, urb->transfer_buffer, 4);
5061d74a6bdSSteve Glendinning 	le32_to_cpus(&intdata);
5072f7ca802SSteve Glendinning 
508a475f603SJoe Perches 	netif_dbg(dev, link, dev->net, "intdata: 0x%08X\n", intdata);
5092f7ca802SSteve Glendinning 
5102f7ca802SSteve Glendinning 	if (intdata & INT_ENP_PHY_INT_)
5112f7ca802SSteve Glendinning 		usbnet_defer_kevent(dev, EVENT_LINK_RESET);
5122f7ca802SSteve Glendinning 	else
51360b86755SJoe Perches 		netdev_warn(dev->net, "unexpected interrupt, intdata=0x%08X\n",
51460b86755SJoe Perches 			    intdata);
5152f7ca802SSteve Glendinning }
5162f7ca802SSteve Glendinning 
517f7b29271SSteve Glendinning /* Enable or disable Tx & Rx checksum offload engines */
518c8f44affSMichał Mirosław static int smsc95xx_set_features(struct net_device *netdev,
519c8f44affSMichał Mirosław 	netdev_features_t features)
5202f7ca802SSteve Glendinning {
52178e47fe4SMichał Mirosław 	struct usbnet *dev = netdev_priv(netdev);
5222f7ca802SSteve Glendinning 	u32 read_buf;
52378e47fe4SMichał Mirosław 	int ret;
52478e47fe4SMichał Mirosław 
52578e47fe4SMichał Mirosław 	ret = smsc95xx_read_reg(dev, COE_CR, &read_buf);
526769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to read COE_CR: %d\n", ret);
5272f7ca802SSteve Glendinning 
52878e47fe4SMichał Mirosław 	if (features & NETIF_F_HW_CSUM)
529f7b29271SSteve Glendinning 		read_buf |= Tx_COE_EN_;
530f7b29271SSteve Glendinning 	else
531f7b29271SSteve Glendinning 		read_buf &= ~Tx_COE_EN_;
532f7b29271SSteve Glendinning 
53378e47fe4SMichał Mirosław 	if (features & NETIF_F_RXCSUM)
5342f7ca802SSteve Glendinning 		read_buf |= Rx_COE_EN_;
5352f7ca802SSteve Glendinning 	else
5362f7ca802SSteve Glendinning 		read_buf &= ~Rx_COE_EN_;
5372f7ca802SSteve Glendinning 
5382f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, COE_CR, read_buf);
539769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write COE_CR: %d\n", ret);
5402f7ca802SSteve Glendinning 
541a475f603SJoe Perches 	netif_dbg(dev, hw, dev->net, "COE_CR = 0x%08x\n", read_buf);
5422f7ca802SSteve Glendinning 	return 0;
5432f7ca802SSteve Glendinning }
5442f7ca802SSteve Glendinning 
5452f7ca802SSteve Glendinning static int smsc95xx_ethtool_get_eeprom_len(struct net_device *net)
5462f7ca802SSteve Glendinning {
5472f7ca802SSteve Glendinning 	return MAX_EEPROM_SIZE;
5482f7ca802SSteve Glendinning }
5492f7ca802SSteve Glendinning 
5502f7ca802SSteve Glendinning static int smsc95xx_ethtool_get_eeprom(struct net_device *netdev,
5512f7ca802SSteve Glendinning 				       struct ethtool_eeprom *ee, u8 *data)
5522f7ca802SSteve Glendinning {
5532f7ca802SSteve Glendinning 	struct usbnet *dev = netdev_priv(netdev);
5542f7ca802SSteve Glendinning 
5552f7ca802SSteve Glendinning 	ee->magic = LAN95XX_EEPROM_MAGIC;
5562f7ca802SSteve Glendinning 
5572f7ca802SSteve Glendinning 	return smsc95xx_read_eeprom(dev, ee->offset, ee->len, data);
5582f7ca802SSteve Glendinning }
5592f7ca802SSteve Glendinning 
5602f7ca802SSteve Glendinning static int smsc95xx_ethtool_set_eeprom(struct net_device *netdev,
5612f7ca802SSteve Glendinning 				       struct ethtool_eeprom *ee, u8 *data)
5622f7ca802SSteve Glendinning {
5632f7ca802SSteve Glendinning 	struct usbnet *dev = netdev_priv(netdev);
5642f7ca802SSteve Glendinning 
5652f7ca802SSteve Glendinning 	if (ee->magic != LAN95XX_EEPROM_MAGIC) {
56660b86755SJoe Perches 		netdev_warn(dev->net, "EEPROM: magic value mismatch, magic = 0x%x\n",
5672f7ca802SSteve Glendinning 			    ee->magic);
5682f7ca802SSteve Glendinning 		return -EINVAL;
5692f7ca802SSteve Glendinning 	}
5702f7ca802SSteve Glendinning 
5712f7ca802SSteve Glendinning 	return smsc95xx_write_eeprom(dev, ee->offset, ee->len, data);
5722f7ca802SSteve Glendinning }
5732f7ca802SSteve Glendinning 
5749fa32e94SEmeric Vigier static int smsc95xx_ethtool_getregslen(struct net_device *netdev)
5759fa32e94SEmeric Vigier {
5769fa32e94SEmeric Vigier 	/* all smsc95xx registers */
5779fa32e94SEmeric Vigier 	return COE_CR - ID_REV + 1;
5789fa32e94SEmeric Vigier }
5799fa32e94SEmeric Vigier 
5809fa32e94SEmeric Vigier static void
5819fa32e94SEmeric Vigier smsc95xx_ethtool_getregs(struct net_device *netdev, struct ethtool_regs *regs,
5829fa32e94SEmeric Vigier 			 void *buf)
5839fa32e94SEmeric Vigier {
5849fa32e94SEmeric Vigier 	struct usbnet *dev = netdev_priv(netdev);
585d348446bSDan Carpenter 	unsigned int i, j;
586d348446bSDan Carpenter 	int retval;
5879fa32e94SEmeric Vigier 	u32 *data = buf;
5889fa32e94SEmeric Vigier 
5899fa32e94SEmeric Vigier 	retval = smsc95xx_read_reg(dev, ID_REV, &regs->version);
5909fa32e94SEmeric Vigier 	if (retval < 0) {
5919fa32e94SEmeric Vigier 		netdev_warn(netdev, "REGS: cannot read ID_REV\n");
5929fa32e94SEmeric Vigier 		return;
5939fa32e94SEmeric Vigier 	}
5949fa32e94SEmeric Vigier 
5959fa32e94SEmeric Vigier 	for (i = ID_REV, j = 0; i <= COE_CR; i += (sizeof(u32)), j++) {
5969fa32e94SEmeric Vigier 		retval = smsc95xx_read_reg(dev, i, &data[j]);
5979fa32e94SEmeric Vigier 		if (retval < 0) {
5989fa32e94SEmeric Vigier 			netdev_warn(netdev, "REGS: cannot read reg[%x]\n", i);
5999fa32e94SEmeric Vigier 			return;
6009fa32e94SEmeric Vigier 		}
6019fa32e94SEmeric Vigier 	}
6029fa32e94SEmeric Vigier }
6039fa32e94SEmeric Vigier 
604e0e474a8SSteve Glendinning static void smsc95xx_ethtool_get_wol(struct net_device *net,
605e0e474a8SSteve Glendinning 				     struct ethtool_wolinfo *wolinfo)
606e0e474a8SSteve Glendinning {
607e0e474a8SSteve Glendinning 	struct usbnet *dev = netdev_priv(net);
608e0e474a8SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
609e0e474a8SSteve Glendinning 
610e0e474a8SSteve Glendinning 	wolinfo->supported = SUPPORTED_WAKE;
611e0e474a8SSteve Glendinning 	wolinfo->wolopts = pdata->wolopts;
612e0e474a8SSteve Glendinning }
613e0e474a8SSteve Glendinning 
614e0e474a8SSteve Glendinning static int smsc95xx_ethtool_set_wol(struct net_device *net,
615e0e474a8SSteve Glendinning 				    struct ethtool_wolinfo *wolinfo)
616e0e474a8SSteve Glendinning {
617e0e474a8SSteve Glendinning 	struct usbnet *dev = netdev_priv(net);
618e0e474a8SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
619e0e474a8SSteve Glendinning 
620e0e474a8SSteve Glendinning 	pdata->wolopts = wolinfo->wolopts & SUPPORTED_WAKE;
621e0e474a8SSteve Glendinning 	return 0;
622e0e474a8SSteve Glendinning }
623e0e474a8SSteve Glendinning 
6240fc0b732SStephen Hemminger static const struct ethtool_ops smsc95xx_ethtool_ops = {
6252f7ca802SSteve Glendinning 	.get_link	= usbnet_get_link,
6262f7ca802SSteve Glendinning 	.nway_reset	= usbnet_nway_reset,
6272f7ca802SSteve Glendinning 	.get_drvinfo	= usbnet_get_drvinfo,
6282f7ca802SSteve Glendinning 	.get_msglevel	= usbnet_get_msglevel,
6292f7ca802SSteve Glendinning 	.set_msglevel	= usbnet_set_msglevel,
6302f7ca802SSteve Glendinning 	.get_settings	= usbnet_get_settings,
6312f7ca802SSteve Glendinning 	.set_settings	= usbnet_set_settings,
6322f7ca802SSteve Glendinning 	.get_eeprom_len	= smsc95xx_ethtool_get_eeprom_len,
6332f7ca802SSteve Glendinning 	.get_eeprom	= smsc95xx_ethtool_get_eeprom,
6342f7ca802SSteve Glendinning 	.set_eeprom	= smsc95xx_ethtool_set_eeprom,
6359fa32e94SEmeric Vigier 	.get_regs_len	= smsc95xx_ethtool_getregslen,
6369fa32e94SEmeric Vigier 	.get_regs	= smsc95xx_ethtool_getregs,
637e0e474a8SSteve Glendinning 	.get_wol	= smsc95xx_ethtool_get_wol,
638e0e474a8SSteve Glendinning 	.set_wol	= smsc95xx_ethtool_set_wol,
6392f7ca802SSteve Glendinning };
6402f7ca802SSteve Glendinning 
6412f7ca802SSteve Glendinning static int smsc95xx_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
6422f7ca802SSteve Glendinning {
6432f7ca802SSteve Glendinning 	struct usbnet *dev = netdev_priv(netdev);
6442f7ca802SSteve Glendinning 
6452f7ca802SSteve Glendinning 	if (!netif_running(netdev))
6462f7ca802SSteve Glendinning 		return -EINVAL;
6472f7ca802SSteve Glendinning 
6482f7ca802SSteve Glendinning 	return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
6492f7ca802SSteve Glendinning }
6502f7ca802SSteve Glendinning 
6512f7ca802SSteve Glendinning static void smsc95xx_init_mac_address(struct usbnet *dev)
6522f7ca802SSteve Glendinning {
6532f7ca802SSteve Glendinning 	/* try reading mac address from EEPROM */
6542f7ca802SSteve Glendinning 	if (smsc95xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN,
6552f7ca802SSteve Glendinning 			dev->net->dev_addr) == 0) {
6562f7ca802SSteve Glendinning 		if (is_valid_ether_addr(dev->net->dev_addr)) {
6572f7ca802SSteve Glendinning 			/* eeprom values are valid so use them */
658a475f603SJoe Perches 			netif_dbg(dev, ifup, dev->net, "MAC address read from EEPROM\n");
6592f7ca802SSteve Glendinning 			return;
6602f7ca802SSteve Glendinning 		}
6612f7ca802SSteve Glendinning 	}
6622f7ca802SSteve Glendinning 
6632f7ca802SSteve Glendinning 	/* no eeprom, or eeprom values are invalid. generate random MAC */
664f2cedb63SDanny Kukawka 	eth_hw_addr_random(dev->net);
665c7e12eadSJoe Perches 	netif_dbg(dev, ifup, dev->net, "MAC address set to eth_random_addr\n");
6662f7ca802SSteve Glendinning }
6672f7ca802SSteve Glendinning 
6682f7ca802SSteve Glendinning static int smsc95xx_set_mac_address(struct usbnet *dev)
6692f7ca802SSteve Glendinning {
6702f7ca802SSteve Glendinning 	u32 addr_lo = dev->net->dev_addr[0] | dev->net->dev_addr[1] << 8 |
6712f7ca802SSteve Glendinning 		dev->net->dev_addr[2] << 16 | dev->net->dev_addr[3] << 24;
6722f7ca802SSteve Glendinning 	u32 addr_hi = dev->net->dev_addr[4] | dev->net->dev_addr[5] << 8;
6732f7ca802SSteve Glendinning 	int ret;
6742f7ca802SSteve Glendinning 
6752f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, ADDRL, addr_lo);
676769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write ADDRL: %d\n", ret);
6772f7ca802SSteve Glendinning 
6782f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, ADDRH, addr_hi);
679769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write ADDRH: %d\n", ret);
6802f7ca802SSteve Glendinning 
6812f7ca802SSteve Glendinning 	return 0;
6822f7ca802SSteve Glendinning }
6832f7ca802SSteve Glendinning 
6842f7ca802SSteve Glendinning /* starts the TX path */
685769ea6d8SSteve Glendinning static int smsc95xx_start_tx_path(struct usbnet *dev)
6862f7ca802SSteve Glendinning {
6872f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
6882f7ca802SSteve Glendinning 	unsigned long flags;
689769ea6d8SSteve Glendinning 	int ret;
6902f7ca802SSteve Glendinning 
6912f7ca802SSteve Glendinning 	/* Enable Tx at MAC */
6922f7ca802SSteve Glendinning 	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
6932f7ca802SSteve Glendinning 	pdata->mac_cr |= MAC_CR_TXEN_;
6942f7ca802SSteve Glendinning 	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
6952f7ca802SSteve Glendinning 
696769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
697769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write MAC_CR: %d\n", ret);
6982f7ca802SSteve Glendinning 
6992f7ca802SSteve Glendinning 	/* Enable Tx at SCSRs */
700769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, TX_CFG, TX_CFG_ON_);
701769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write TX_CFG: %d\n", ret);
702769ea6d8SSteve Glendinning 
703769ea6d8SSteve Glendinning 	return 0;
7042f7ca802SSteve Glendinning }
7052f7ca802SSteve Glendinning 
7062f7ca802SSteve Glendinning /* Starts the Receive path */
707769ea6d8SSteve Glendinning static int smsc95xx_start_rx_path(struct usbnet *dev)
7082f7ca802SSteve Glendinning {
7092f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
7102f7ca802SSteve Glendinning 	unsigned long flags;
711769ea6d8SSteve Glendinning 	int ret;
7122f7ca802SSteve Glendinning 
7132f7ca802SSteve Glendinning 	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
7142f7ca802SSteve Glendinning 	pdata->mac_cr |= MAC_CR_RXEN_;
7152f7ca802SSteve Glendinning 	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
7162f7ca802SSteve Glendinning 
717769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
718769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write MAC_CR: %d\n", ret);
719769ea6d8SSteve Glendinning 
720769ea6d8SSteve Glendinning 	return 0;
7212f7ca802SSteve Glendinning }
7222f7ca802SSteve Glendinning 
7232f7ca802SSteve Glendinning static int smsc95xx_phy_initialize(struct usbnet *dev)
7242f7ca802SSteve Glendinning {
725769ea6d8SSteve Glendinning 	int bmcr, ret, timeout = 0;
726db443c44SSteve Glendinning 
7272f7ca802SSteve Glendinning 	/* Initialize MII structure */
7282f7ca802SSteve Glendinning 	dev->mii.dev = dev->net;
7292f7ca802SSteve Glendinning 	dev->mii.mdio_read = smsc95xx_mdio_read;
7302f7ca802SSteve Glendinning 	dev->mii.mdio_write = smsc95xx_mdio_write;
7312f7ca802SSteve Glendinning 	dev->mii.phy_id_mask = 0x1f;
7322f7ca802SSteve Glendinning 	dev->mii.reg_num_mask = 0x1f;
7332f7ca802SSteve Glendinning 	dev->mii.phy_id = SMSC95XX_INTERNAL_PHY_ID;
7342f7ca802SSteve Glendinning 
735db443c44SSteve Glendinning 	/* reset phy and wait for reset to complete */
7362f7ca802SSteve Glendinning 	smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
737db443c44SSteve Glendinning 
738db443c44SSteve Glendinning 	do {
739db443c44SSteve Glendinning 		msleep(10);
740db443c44SSteve Glendinning 		bmcr = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, MII_BMCR);
741db443c44SSteve Glendinning 		timeout++;
742d9460920SRabin Vincent 	} while ((bmcr & BMCR_RESET) && (timeout < 100));
743db443c44SSteve Glendinning 
744db443c44SSteve Glendinning 	if (timeout >= 100) {
745db443c44SSteve Glendinning 		netdev_warn(dev->net, "timeout on PHY Reset");
746db443c44SSteve Glendinning 		return -EIO;
747db443c44SSteve Glendinning 	}
748db443c44SSteve Glendinning 
7492f7ca802SSteve Glendinning 	smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
7502f7ca802SSteve Glendinning 		ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP |
7512f7ca802SSteve Glendinning 		ADVERTISE_PAUSE_ASYM);
7522f7ca802SSteve Glendinning 
7532f7ca802SSteve Glendinning 	/* read to clear */
754769ea6d8SSteve Glendinning 	ret = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, PHY_INT_SRC);
755769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to read PHY_INT_SRC during init");
7562f7ca802SSteve Glendinning 
7572f7ca802SSteve Glendinning 	smsc95xx_mdio_write(dev->net, dev->mii.phy_id, PHY_INT_MASK,
7582f7ca802SSteve Glendinning 		PHY_INT_MASK_DEFAULT_);
7592f7ca802SSteve Glendinning 	mii_nway_restart(&dev->mii);
7602f7ca802SSteve Glendinning 
761a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net, "phy initialised successfully\n");
7622f7ca802SSteve Glendinning 	return 0;
7632f7ca802SSteve Glendinning }
7642f7ca802SSteve Glendinning 
7652f7ca802SSteve Glendinning static int smsc95xx_reset(struct usbnet *dev)
7662f7ca802SSteve Glendinning {
7672f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
7682f7ca802SSteve Glendinning 	u32 read_buf, write_buf, burst_cap;
7692f7ca802SSteve Glendinning 	int ret = 0, timeout;
7702f7ca802SSteve Glendinning 
771a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net, "entering smsc95xx_reset\n");
7722f7ca802SSteve Glendinning 
7734436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, HW_CFG, HW_CFG_LRST_);
774769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write HW_CFG_LRST_ bit in HW_CFG\n");
7752f7ca802SSteve Glendinning 
7762f7ca802SSteve Glendinning 	timeout = 0;
7772f7ca802SSteve Glendinning 	do {
778cf2acec2SSteve Glendinning 		msleep(10);
7792f7ca802SSteve Glendinning 		ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
780769ea6d8SSteve Glendinning 		check_warn_return(ret, "Failed to read HW_CFG: %d\n", ret);
7812f7ca802SSteve Glendinning 		timeout++;
7822f7ca802SSteve Glendinning 	} while ((read_buf & HW_CFG_LRST_) && (timeout < 100));
7832f7ca802SSteve Glendinning 
7842f7ca802SSteve Glendinning 	if (timeout >= 100) {
78560b86755SJoe Perches 		netdev_warn(dev->net, "timeout waiting for completion of Lite Reset\n");
7862f7ca802SSteve Glendinning 		return ret;
7872f7ca802SSteve Glendinning 	}
7882f7ca802SSteve Glendinning 
7894436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, PM_CTRL, PM_CTL_PHY_RST_);
790769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write PM_CTRL: %d\n", ret);
7912f7ca802SSteve Glendinning 
7922f7ca802SSteve Glendinning 	timeout = 0;
7932f7ca802SSteve Glendinning 	do {
794cf2acec2SSteve Glendinning 		msleep(10);
7952f7ca802SSteve Glendinning 		ret = smsc95xx_read_reg(dev, PM_CTRL, &read_buf);
796769ea6d8SSteve Glendinning 		check_warn_return(ret, "Failed to read PM_CTRL: %d\n", ret);
7972f7ca802SSteve Glendinning 		timeout++;
7982f7ca802SSteve Glendinning 	} while ((read_buf & PM_CTL_PHY_RST_) && (timeout < 100));
7992f7ca802SSteve Glendinning 
8002f7ca802SSteve Glendinning 	if (timeout >= 100) {
80160b86755SJoe Perches 		netdev_warn(dev->net, "timeout waiting for PHY Reset\n");
8022f7ca802SSteve Glendinning 		return ret;
8032f7ca802SSteve Glendinning 	}
8042f7ca802SSteve Glendinning 
8052f7ca802SSteve Glendinning 	ret = smsc95xx_set_mac_address(dev);
8062f7ca802SSteve Glendinning 	if (ret < 0)
8072f7ca802SSteve Glendinning 		return ret;
8082f7ca802SSteve Glendinning 
809a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net,
810a475f603SJoe Perches 		  "MAC Address: %pM\n", dev->net->dev_addr);
8112f7ca802SSteve Glendinning 
8122f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
813769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to read HW_CFG: %d\n", ret);
8142f7ca802SSteve Glendinning 
815a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net,
816a475f603SJoe Perches 		  "Read Value from HW_CFG : 0x%08x\n", read_buf);
8172f7ca802SSteve Glendinning 
8182f7ca802SSteve Glendinning 	read_buf |= HW_CFG_BIR_;
8192f7ca802SSteve Glendinning 
8202f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
821769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write HW_CFG_BIR_ bit in HW_CFG\n");
8222f7ca802SSteve Glendinning 
8232f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
824769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to read HW_CFG: %d\n", ret);
825a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net,
826a475f603SJoe Perches 		  "Read Value from HW_CFG after writing HW_CFG_BIR_: 0x%08x\n",
82760b86755SJoe Perches 		  read_buf);
8282f7ca802SSteve Glendinning 
8292f7ca802SSteve Glendinning 	if (!turbo_mode) {
8302f7ca802SSteve Glendinning 		burst_cap = 0;
8312f7ca802SSteve Glendinning 		dev->rx_urb_size = MAX_SINGLE_PACKET_SIZE;
8322f7ca802SSteve Glendinning 	} else if (dev->udev->speed == USB_SPEED_HIGH) {
8332f7ca802SSteve Glendinning 		burst_cap = DEFAULT_HS_BURST_CAP_SIZE / HS_USB_PKT_SIZE;
8342f7ca802SSteve Glendinning 		dev->rx_urb_size = DEFAULT_HS_BURST_CAP_SIZE;
8352f7ca802SSteve Glendinning 	} else {
8362f7ca802SSteve Glendinning 		burst_cap = DEFAULT_FS_BURST_CAP_SIZE / FS_USB_PKT_SIZE;
8372f7ca802SSteve Glendinning 		dev->rx_urb_size = DEFAULT_FS_BURST_CAP_SIZE;
8382f7ca802SSteve Glendinning 	}
8392f7ca802SSteve Glendinning 
840a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net,
841a475f603SJoe Perches 		  "rx_urb_size=%ld\n", (ulong)dev->rx_urb_size);
8422f7ca802SSteve Glendinning 
8432f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, BURST_CAP, burst_cap);
844769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write BURST_CAP: %d\n", ret);
8452f7ca802SSteve Glendinning 
8462f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, BURST_CAP, &read_buf);
847769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to read BURST_CAP: %d\n", ret);
848769ea6d8SSteve Glendinning 
849a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net,
850a475f603SJoe Perches 		  "Read Value from BURST_CAP after writing: 0x%08x\n",
8512f7ca802SSteve Glendinning 		  read_buf);
8522f7ca802SSteve Glendinning 
8534436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, BULK_IN_DLY, DEFAULT_BULK_IN_DELAY);
854769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write BULK_IN_DLY: %d\n", ret);
8552f7ca802SSteve Glendinning 
8562f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, BULK_IN_DLY, &read_buf);
857769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to read BULK_IN_DLY: %d\n", ret);
858769ea6d8SSteve Glendinning 
859a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net,
860a475f603SJoe Perches 		  "Read Value from BULK_IN_DLY after writing: 0x%08x\n",
86160b86755SJoe Perches 		  read_buf);
8622f7ca802SSteve Glendinning 
8632f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
864769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to read HW_CFG: %d\n", ret);
865769ea6d8SSteve Glendinning 
866a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net,
867a475f603SJoe Perches 		  "Read Value from HW_CFG: 0x%08x\n", read_buf);
8682f7ca802SSteve Glendinning 
8692f7ca802SSteve Glendinning 	if (turbo_mode)
8702f7ca802SSteve Glendinning 		read_buf |= (HW_CFG_MEF_ | HW_CFG_BCE_);
8712f7ca802SSteve Glendinning 
8722f7ca802SSteve Glendinning 	read_buf &= ~HW_CFG_RXDOFF_;
8732f7ca802SSteve Glendinning 
8742f7ca802SSteve Glendinning 	/* set Rx data offset=2, Make IP header aligns on word boundary. */
8752f7ca802SSteve Glendinning 	read_buf |= NET_IP_ALIGN << 9;
8762f7ca802SSteve Glendinning 
8772f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
878769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write HW_CFG: %d\n", ret);
8792f7ca802SSteve Glendinning 
8802f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
881769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to read HW_CFG: %d\n", ret);
882769ea6d8SSteve Glendinning 
883a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net,
884a475f603SJoe Perches 		  "Read Value from HW_CFG after writing: 0x%08x\n", read_buf);
8852f7ca802SSteve Glendinning 
8864436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
887769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write INT_STS: %d\n", ret);
8882f7ca802SSteve Glendinning 
8892f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, ID_REV, &read_buf);
890769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to read ID_REV: %d\n", ret);
891a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net, "ID_REV = 0x%08x\n", read_buf);
8922f7ca802SSteve Glendinning 
893f293501cSSteve Glendinning 	/* Configure GPIO pins as LED outputs */
894f293501cSSteve Glendinning 	write_buf = LED_GPIO_CFG_SPD_LED | LED_GPIO_CFG_LNK_LED |
895f293501cSSteve Glendinning 		LED_GPIO_CFG_FDX_LED;
896f293501cSSteve Glendinning 	ret = smsc95xx_write_reg(dev, LED_GPIO_CFG, write_buf);
897769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write LED_GPIO_CFG: %d\n", ret);
898f293501cSSteve Glendinning 
8992f7ca802SSteve Glendinning 	/* Init Tx */
9004436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, FLOW, 0);
901769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write FLOW: %d\n", ret);
9022f7ca802SSteve Glendinning 
9034436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, AFC_CFG, AFC_CFG_DEFAULT);
904769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write AFC_CFG: %d\n", ret);
9052f7ca802SSteve Glendinning 
9062f7ca802SSteve Glendinning 	/* Don't need mac_cr_lock during initialisation */
9072f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, MAC_CR, &pdata->mac_cr);
908769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to read MAC_CR: %d\n", ret);
9092f7ca802SSteve Glendinning 
9102f7ca802SSteve Glendinning 	/* Init Rx */
9112f7ca802SSteve Glendinning 	/* Set Vlan */
9124436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, VLAN1, (u32)ETH_P_8021Q);
913769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write VLAN1: %d\n", ret);
9142f7ca802SSteve Glendinning 
915f7b29271SSteve Glendinning 	/* Enable or disable checksum offload engines */
916769ea6d8SSteve Glendinning 	ret = smsc95xx_set_features(dev->net, dev->net->features);
917769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to set checksum offload features");
9182f7ca802SSteve Glendinning 
9192f7ca802SSteve Glendinning 	smsc95xx_set_multicast(dev->net);
9202f7ca802SSteve Glendinning 
921769ea6d8SSteve Glendinning 	ret = smsc95xx_phy_initialize(dev);
922769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to init PHY");
9232f7ca802SSteve Glendinning 
9242f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, INT_EP_CTL, &read_buf);
925769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to read INT_EP_CTL: %d\n", ret);
9262f7ca802SSteve Glendinning 
9272f7ca802SSteve Glendinning 	/* enable PHY interrupts */
9282f7ca802SSteve Glendinning 	read_buf |= INT_EP_CTL_PHY_INT_;
9292f7ca802SSteve Glendinning 
9302f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, INT_EP_CTL, read_buf);
931769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write INT_EP_CTL: %d\n", ret);
9322f7ca802SSteve Glendinning 
933769ea6d8SSteve Glendinning 	ret = smsc95xx_start_tx_path(dev);
934769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to start TX path");
935769ea6d8SSteve Glendinning 
936769ea6d8SSteve Glendinning 	ret = smsc95xx_start_rx_path(dev);
937769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to start RX path");
9382f7ca802SSteve Glendinning 
939a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net, "smsc95xx_reset, return 0\n");
9402f7ca802SSteve Glendinning 	return 0;
9412f7ca802SSteve Glendinning }
9422f7ca802SSteve Glendinning 
94363e77b39SStephen Hemminger static const struct net_device_ops smsc95xx_netdev_ops = {
94463e77b39SStephen Hemminger 	.ndo_open		= usbnet_open,
94563e77b39SStephen Hemminger 	.ndo_stop		= usbnet_stop,
94663e77b39SStephen Hemminger 	.ndo_start_xmit		= usbnet_start_xmit,
94763e77b39SStephen Hemminger 	.ndo_tx_timeout		= usbnet_tx_timeout,
94863e77b39SStephen Hemminger 	.ndo_change_mtu		= usbnet_change_mtu,
94963e77b39SStephen Hemminger 	.ndo_set_mac_address 	= eth_mac_addr,
95063e77b39SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
95163e77b39SStephen Hemminger 	.ndo_do_ioctl 		= smsc95xx_ioctl,
952afc4b13dSJiri Pirko 	.ndo_set_rx_mode	= smsc95xx_set_multicast,
95378e47fe4SMichał Mirosław 	.ndo_set_features	= smsc95xx_set_features,
95463e77b39SStephen Hemminger };
95563e77b39SStephen Hemminger 
9562f7ca802SSteve Glendinning static int smsc95xx_bind(struct usbnet *dev, struct usb_interface *intf)
9572f7ca802SSteve Glendinning {
9582f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = NULL;
9592f7ca802SSteve Glendinning 	int ret;
9602f7ca802SSteve Glendinning 
9612f7ca802SSteve Glendinning 	printk(KERN_INFO SMSC_CHIPNAME " v" SMSC_DRIVER_VERSION "\n");
9622f7ca802SSteve Glendinning 
9632f7ca802SSteve Glendinning 	ret = usbnet_get_endpoints(dev, intf);
964769ea6d8SSteve Glendinning 	check_warn_return(ret, "usbnet_get_endpoints failed: %d\n", ret);
9652f7ca802SSteve Glendinning 
9662f7ca802SSteve Glendinning 	dev->data[0] = (unsigned long)kzalloc(sizeof(struct smsc95xx_priv),
9672f7ca802SSteve Glendinning 		GFP_KERNEL);
9682f7ca802SSteve Glendinning 
9692f7ca802SSteve Glendinning 	pdata = (struct smsc95xx_priv *)(dev->data[0]);
9702f7ca802SSteve Glendinning 	if (!pdata) {
97160b86755SJoe Perches 		netdev_warn(dev->net, "Unable to allocate struct smsc95xx_priv\n");
9722f7ca802SSteve Glendinning 		return -ENOMEM;
9732f7ca802SSteve Glendinning 	}
9742f7ca802SSteve Glendinning 
9752f7ca802SSteve Glendinning 	spin_lock_init(&pdata->mac_cr_lock);
9762f7ca802SSteve Glendinning 
97778e47fe4SMichał Mirosław 	if (DEFAULT_TX_CSUM_ENABLE)
97878e47fe4SMichał Mirosław 		dev->net->features |= NETIF_F_HW_CSUM;
97978e47fe4SMichał Mirosław 	if (DEFAULT_RX_CSUM_ENABLE)
98078e47fe4SMichał Mirosław 		dev->net->features |= NETIF_F_RXCSUM;
98178e47fe4SMichał Mirosław 
98278e47fe4SMichał Mirosław 	dev->net->hw_features = NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
9832f7ca802SSteve Glendinning 
984f4e8ab7cSBernard Blackham 	smsc95xx_init_mac_address(dev);
985f4e8ab7cSBernard Blackham 
9862f7ca802SSteve Glendinning 	/* Init all registers */
9872f7ca802SSteve Glendinning 	ret = smsc95xx_reset(dev);
9882f7ca802SSteve Glendinning 
98963e77b39SStephen Hemminger 	dev->net->netdev_ops = &smsc95xx_netdev_ops;
9902f7ca802SSteve Glendinning 	dev->net->ethtool_ops = &smsc95xx_ethtool_ops;
9912f7ca802SSteve Glendinning 	dev->net->flags |= IFF_MULTICAST;
99278e47fe4SMichał Mirosław 	dev->net->hard_header_len += SMSC95XX_TX_OVERHEAD_CSUM;
9939bbf5660SStephane Fillod 	dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
9942f7ca802SSteve Glendinning 	return 0;
9952f7ca802SSteve Glendinning }
9962f7ca802SSteve Glendinning 
9972f7ca802SSteve Glendinning static void smsc95xx_unbind(struct usbnet *dev, struct usb_interface *intf)
9982f7ca802SSteve Glendinning {
9992f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
10002f7ca802SSteve Glendinning 	if (pdata) {
1001a475f603SJoe Perches 		netif_dbg(dev, ifdown, dev->net, "free pdata\n");
10022f7ca802SSteve Glendinning 		kfree(pdata);
10032f7ca802SSteve Glendinning 		pdata = NULL;
10042f7ca802SSteve Glendinning 		dev->data[0] = 0;
10052f7ca802SSteve Glendinning 	}
10062f7ca802SSteve Glendinning }
10072f7ca802SSteve Glendinning 
1008b5a04475SSteve Glendinning static int smsc95xx_suspend(struct usb_interface *intf, pm_message_t message)
1009b5a04475SSteve Glendinning {
1010b5a04475SSteve Glendinning 	struct usbnet *dev = usb_get_intfdata(intf);
1011e0e474a8SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1012b5a04475SSteve Glendinning 	int ret;
1013b5a04475SSteve Glendinning 	u32 val;
1014b5a04475SSteve Glendinning 
1015b5a04475SSteve Glendinning 	ret = usbnet_suspend(intf, message);
1016b5a04475SSteve Glendinning 	check_warn_return(ret, "usbnet_suspend error");
1017b5a04475SSteve Glendinning 
1018e0e474a8SSteve Glendinning 	/* if no wol options set, enter lowest power SUSPEND2 mode */
1019e0e474a8SSteve Glendinning 	if (!(pdata->wolopts & SUPPORTED_WAKE)) {
1020b5a04475SSteve Glendinning 		netdev_info(dev->net, "entering SUSPEND2 mode");
1021b5a04475SSteve Glendinning 
1022e0e474a8SSteve Glendinning 		/* disable energy detect (link up) & wake up events */
1023e0e474a8SSteve Glendinning 		ret = smsc95xx_read_reg(dev, WUCSR, &val);
1024e0e474a8SSteve Glendinning 		check_warn_return(ret, "Error reading WUCSR");
1025e0e474a8SSteve Glendinning 
1026e0e474a8SSteve Glendinning 		val &= ~(WUCSR_MPEN_ | WUCSR_WAKE_EN_);
1027e0e474a8SSteve Glendinning 
1028e0e474a8SSteve Glendinning 		ret = smsc95xx_write_reg(dev, WUCSR, val);
1029e0e474a8SSteve Glendinning 		check_warn_return(ret, "Error writing WUCSR");
1030e0e474a8SSteve Glendinning 
1031e0e474a8SSteve Glendinning 		ret = smsc95xx_read_reg(dev, PM_CTRL, &val);
1032e0e474a8SSteve Glendinning 		check_warn_return(ret, "Error reading PM_CTRL");
1033e0e474a8SSteve Glendinning 
1034e0e474a8SSteve Glendinning 		val &= ~(PM_CTL_ED_EN_ | PM_CTL_WOL_EN_);
1035e0e474a8SSteve Glendinning 
1036e0e474a8SSteve Glendinning 		ret = smsc95xx_write_reg(dev, PM_CTRL, val);
1037e0e474a8SSteve Glendinning 		check_warn_return(ret, "Error writing PM_CTRL");
1038e0e474a8SSteve Glendinning 
1039e0e474a8SSteve Glendinning 		/* enter suspend2 mode */
1040b5a04475SSteve Glendinning 		ret = smsc95xx_read_reg(dev, PM_CTRL, &val);
1041b5a04475SSteve Glendinning 		check_warn_return(ret, "Error reading PM_CTRL");
1042b5a04475SSteve Glendinning 
1043b5a04475SSteve Glendinning 		val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1044b5a04475SSteve Glendinning 		val |= PM_CTL_SUS_MODE_2;
1045b5a04475SSteve Glendinning 
1046b5a04475SSteve Glendinning 		ret = smsc95xx_write_reg(dev, PM_CTRL, val);
1047b5a04475SSteve Glendinning 		check_warn_return(ret, "Error writing PM_CTRL");
1048b5a04475SSteve Glendinning 
1049b5a04475SSteve Glendinning 		return 0;
1050b5a04475SSteve Glendinning 	}
1051b5a04475SSteve Glendinning 
1052e0e474a8SSteve Glendinning 	if (pdata->wolopts & WAKE_MAGIC) {
1053e0e474a8SSteve Glendinning 		/* clear any pending magic packet status */
1054e0e474a8SSteve Glendinning 		ret = smsc95xx_read_reg(dev, WUCSR, &val);
1055e0e474a8SSteve Glendinning 		check_warn_return(ret, "Error reading WUCSR");
1056e0e474a8SSteve Glendinning 
1057e0e474a8SSteve Glendinning 		val |= WUCSR_MPR_;
1058e0e474a8SSteve Glendinning 
1059e0e474a8SSteve Glendinning 		ret = smsc95xx_write_reg(dev, WUCSR, val);
1060e0e474a8SSteve Glendinning 		check_warn_return(ret, "Error writing WUCSR");
1061e0e474a8SSteve Glendinning 	}
1062e0e474a8SSteve Glendinning 
1063e0e474a8SSteve Glendinning 	/* enable/disable magic packup wake */
1064e0e474a8SSteve Glendinning 	ret = smsc95xx_read_reg(dev, WUCSR, &val);
1065e0e474a8SSteve Glendinning 	check_warn_return(ret, "Error reading WUCSR");
1066e0e474a8SSteve Glendinning 
1067e0e474a8SSteve Glendinning 	if (pdata->wolopts & WAKE_MAGIC) {
1068e0e474a8SSteve Glendinning 		netdev_info(dev->net, "enabling magic packet wakeup");
1069e0e474a8SSteve Glendinning 		val |= WUCSR_MPEN_;
1070e0e474a8SSteve Glendinning 	} else {
1071e0e474a8SSteve Glendinning 		netdev_info(dev->net, "disabling magic packet wakeup");
1072e0e474a8SSteve Glendinning 		val &= ~WUCSR_MPEN_;
1073e0e474a8SSteve Glendinning 	}
1074e0e474a8SSteve Glendinning 
1075e0e474a8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, WUCSR, val);
1076e0e474a8SSteve Glendinning 	check_warn_return(ret, "Error writing WUCSR");
1077e0e474a8SSteve Glendinning 
1078e0e474a8SSteve Glendinning 	/* enable wol wakeup source */
1079e0e474a8SSteve Glendinning 	ret = smsc95xx_read_reg(dev, PM_CTRL, &val);
1080e0e474a8SSteve Glendinning 	check_warn_return(ret, "Error reading PM_CTRL");
1081e0e474a8SSteve Glendinning 
1082e0e474a8SSteve Glendinning 	val |= PM_CTL_WOL_EN_;
1083e0e474a8SSteve Glendinning 
1084e0e474a8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, PM_CTRL, val);
1085e0e474a8SSteve Glendinning 	check_warn_return(ret, "Error writing PM_CTRL");
1086e0e474a8SSteve Glendinning 
1087e0e474a8SSteve Glendinning 	/* enable receiver */
1088e0e474a8SSteve Glendinning 	smsc95xx_start_rx_path(dev);
1089e0e474a8SSteve Glendinning 
1090e0e474a8SSteve Glendinning 	/* some wol options are enabled, so enter SUSPEND0 */
1091e0e474a8SSteve Glendinning 	netdev_info(dev->net, "entering SUSPEND0 mode");
1092e0e474a8SSteve Glendinning 
1093e0e474a8SSteve Glendinning 	ret = smsc95xx_read_reg(dev, PM_CTRL, &val);
1094e0e474a8SSteve Glendinning 	check_warn_return(ret, "Error reading PM_CTRL");
1095e0e474a8SSteve Glendinning 
1096e0e474a8SSteve Glendinning 	val &= (~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_));
1097e0e474a8SSteve Glendinning 	val |= PM_CTL_SUS_MODE_0;
1098e0e474a8SSteve Glendinning 
1099e0e474a8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, PM_CTRL, val);
1100e0e474a8SSteve Glendinning 	check_warn_return(ret, "Error writing PM_CTRL");
1101e0e474a8SSteve Glendinning 
1102e0e474a8SSteve Glendinning 	/* clear wol status */
1103e0e474a8SSteve Glendinning 	val &= ~PM_CTL_WUPS_;
1104e0e474a8SSteve Glendinning 	val |= PM_CTL_WUPS_WOL_;
1105e0e474a8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, PM_CTRL, val);
1106e0e474a8SSteve Glendinning 	check_warn_return(ret, "Error writing PM_CTRL");
1107e0e474a8SSteve Glendinning 
1108e0e474a8SSteve Glendinning 	/* read back PM_CTRL */
1109e0e474a8SSteve Glendinning 	ret = smsc95xx_read_reg(dev, PM_CTRL, &val);
1110e0e474a8SSteve Glendinning 	check_warn_return(ret, "Error reading PM_CTRL");
1111e0e474a8SSteve Glendinning 
1112e0e474a8SSteve Glendinning 	smsc95xx_set_feature(dev, USB_DEVICE_REMOTE_WAKEUP);
1113e0e474a8SSteve Glendinning 
1114e0e474a8SSteve Glendinning 	return 0;
1115e0e474a8SSteve Glendinning }
1116e0e474a8SSteve Glendinning 
1117e0e474a8SSteve Glendinning static int smsc95xx_resume(struct usb_interface *intf)
1118e0e474a8SSteve Glendinning {
1119e0e474a8SSteve Glendinning 	struct usbnet *dev = usb_get_intfdata(intf);
1120e0e474a8SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1121e0e474a8SSteve Glendinning 	int ret;
1122e0e474a8SSteve Glendinning 	u32 val;
1123e0e474a8SSteve Glendinning 
1124e0e474a8SSteve Glendinning 	BUG_ON(!dev);
1125e0e474a8SSteve Glendinning 
1126e0e474a8SSteve Glendinning 	if (pdata->wolopts & WAKE_MAGIC) {
1127e0e474a8SSteve Glendinning 		smsc95xx_clear_feature(dev, USB_DEVICE_REMOTE_WAKEUP);
1128e0e474a8SSteve Glendinning 
1129e0e474a8SSteve Glendinning 		/* Disable magic packup wake */
1130e0e474a8SSteve Glendinning 		ret = smsc95xx_read_reg(dev, WUCSR, &val);
1131e0e474a8SSteve Glendinning 		check_warn_return(ret, "Error reading WUCSR");
1132e0e474a8SSteve Glendinning 
1133e0e474a8SSteve Glendinning 		val &= ~WUCSR_MPEN_;
1134e0e474a8SSteve Glendinning 
1135e0e474a8SSteve Glendinning 		ret = smsc95xx_write_reg(dev, WUCSR, val);
1136e0e474a8SSteve Glendinning 		check_warn_return(ret, "Error writing WUCSR");
1137e0e474a8SSteve Glendinning 
1138e0e474a8SSteve Glendinning 		/* clear wake-up status */
1139e0e474a8SSteve Glendinning 		ret = smsc95xx_read_reg(dev, PM_CTRL, &val);
1140e0e474a8SSteve Glendinning 		check_warn_return(ret, "Error reading PM_CTRL");
1141e0e474a8SSteve Glendinning 
1142e0e474a8SSteve Glendinning 		val &= ~PM_CTL_WOL_EN_;
1143e0e474a8SSteve Glendinning 		val |= PM_CTL_WUPS_;
1144e0e474a8SSteve Glendinning 
1145e0e474a8SSteve Glendinning 		ret = smsc95xx_write_reg(dev, PM_CTRL, val);
1146e0e474a8SSteve Glendinning 		check_warn_return(ret, "Error writing PM_CTRL");
1147e0e474a8SSteve Glendinning 	}
1148e0e474a8SSteve Glendinning 
1149e0e474a8SSteve Glendinning 	return usbnet_resume(intf);
1150e0e474a8SSteve Glendinning 	check_warn_return(ret, "usbnet_resume error");
1151e0e474a8SSteve Glendinning 
1152e0e474a8SSteve Glendinning 	return 0;
1153e0e474a8SSteve Glendinning }
1154e0e474a8SSteve Glendinning 
11552f7ca802SSteve Glendinning static void smsc95xx_rx_csum_offload(struct sk_buff *skb)
11562f7ca802SSteve Glendinning {
11572f7ca802SSteve Glendinning 	skb->csum = *(u16 *)(skb_tail_pointer(skb) - 2);
11582f7ca802SSteve Glendinning 	skb->ip_summed = CHECKSUM_COMPLETE;
11592f7ca802SSteve Glendinning 	skb_trim(skb, skb->len - 2);
11602f7ca802SSteve Glendinning }
11612f7ca802SSteve Glendinning 
11622f7ca802SSteve Glendinning static int smsc95xx_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
11632f7ca802SSteve Glendinning {
11642f7ca802SSteve Glendinning 	while (skb->len > 0) {
11652f7ca802SSteve Glendinning 		u32 header, align_count;
11662f7ca802SSteve Glendinning 		struct sk_buff *ax_skb;
11672f7ca802SSteve Glendinning 		unsigned char *packet;
11682f7ca802SSteve Glendinning 		u16 size;
11692f7ca802SSteve Glendinning 
11702f7ca802SSteve Glendinning 		memcpy(&header, skb->data, sizeof(header));
11712f7ca802SSteve Glendinning 		le32_to_cpus(&header);
11722f7ca802SSteve Glendinning 		skb_pull(skb, 4 + NET_IP_ALIGN);
11732f7ca802SSteve Glendinning 		packet = skb->data;
11742f7ca802SSteve Glendinning 
11752f7ca802SSteve Glendinning 		/* get the packet length */
11762f7ca802SSteve Glendinning 		size = (u16)((header & RX_STS_FL_) >> 16);
11772f7ca802SSteve Glendinning 		align_count = (4 - ((size + NET_IP_ALIGN) % 4)) % 4;
11782f7ca802SSteve Glendinning 
11792f7ca802SSteve Glendinning 		if (unlikely(header & RX_STS_ES_)) {
1180a475f603SJoe Perches 			netif_dbg(dev, rx_err, dev->net,
1181a475f603SJoe Perches 				  "Error header=0x%08x\n", header);
118280667ac1SHerbert Xu 			dev->net->stats.rx_errors++;
118380667ac1SHerbert Xu 			dev->net->stats.rx_dropped++;
11842f7ca802SSteve Glendinning 
11852f7ca802SSteve Glendinning 			if (header & RX_STS_CRC_) {
118680667ac1SHerbert Xu 				dev->net->stats.rx_crc_errors++;
11872f7ca802SSteve Glendinning 			} else {
11882f7ca802SSteve Glendinning 				if (header & (RX_STS_TL_ | RX_STS_RF_))
118980667ac1SHerbert Xu 					dev->net->stats.rx_frame_errors++;
11902f7ca802SSteve Glendinning 
11912f7ca802SSteve Glendinning 				if ((header & RX_STS_LE_) &&
11922f7ca802SSteve Glendinning 					(!(header & RX_STS_FT_)))
119380667ac1SHerbert Xu 					dev->net->stats.rx_length_errors++;
11942f7ca802SSteve Glendinning 			}
11952f7ca802SSteve Glendinning 		} else {
11962f7ca802SSteve Glendinning 			/* ETH_FRAME_LEN + 4(CRC) + 2(COE) + 4(Vlan) */
11972f7ca802SSteve Glendinning 			if (unlikely(size > (ETH_FRAME_LEN + 12))) {
1198a475f603SJoe Perches 				netif_dbg(dev, rx_err, dev->net,
1199a475f603SJoe Perches 					  "size err header=0x%08x\n", header);
12002f7ca802SSteve Glendinning 				return 0;
12012f7ca802SSteve Glendinning 			}
12022f7ca802SSteve Glendinning 
12032f7ca802SSteve Glendinning 			/* last frame in this batch */
12042f7ca802SSteve Glendinning 			if (skb->len == size) {
120578e47fe4SMichał Mirosław 				if (dev->net->features & NETIF_F_RXCSUM)
12062f7ca802SSteve Glendinning 					smsc95xx_rx_csum_offload(skb);
1207df18accaSPeter Korsgaard 				skb_trim(skb, skb->len - 4); /* remove fcs */
12082f7ca802SSteve Glendinning 				skb->truesize = size + sizeof(struct sk_buff);
12092f7ca802SSteve Glendinning 
12102f7ca802SSteve Glendinning 				return 1;
12112f7ca802SSteve Glendinning 			}
12122f7ca802SSteve Glendinning 
12132f7ca802SSteve Glendinning 			ax_skb = skb_clone(skb, GFP_ATOMIC);
12142f7ca802SSteve Glendinning 			if (unlikely(!ax_skb)) {
121560b86755SJoe Perches 				netdev_warn(dev->net, "Error allocating skb\n");
12162f7ca802SSteve Glendinning 				return 0;
12172f7ca802SSteve Glendinning 			}
12182f7ca802SSteve Glendinning 
12192f7ca802SSteve Glendinning 			ax_skb->len = size;
12202f7ca802SSteve Glendinning 			ax_skb->data = packet;
12212f7ca802SSteve Glendinning 			skb_set_tail_pointer(ax_skb, size);
12222f7ca802SSteve Glendinning 
122378e47fe4SMichał Mirosław 			if (dev->net->features & NETIF_F_RXCSUM)
12242f7ca802SSteve Glendinning 				smsc95xx_rx_csum_offload(ax_skb);
1225df18accaSPeter Korsgaard 			skb_trim(ax_skb, ax_skb->len - 4); /* remove fcs */
12262f7ca802SSteve Glendinning 			ax_skb->truesize = size + sizeof(struct sk_buff);
12272f7ca802SSteve Glendinning 
12282f7ca802SSteve Glendinning 			usbnet_skb_return(dev, ax_skb);
12292f7ca802SSteve Glendinning 		}
12302f7ca802SSteve Glendinning 
12312f7ca802SSteve Glendinning 		skb_pull(skb, size);
12322f7ca802SSteve Glendinning 
12332f7ca802SSteve Glendinning 		/* padding bytes before the next frame starts */
12342f7ca802SSteve Glendinning 		if (skb->len)
12352f7ca802SSteve Glendinning 			skb_pull(skb, align_count);
12362f7ca802SSteve Glendinning 	}
12372f7ca802SSteve Glendinning 
12382f7ca802SSteve Glendinning 	if (unlikely(skb->len < 0)) {
123960b86755SJoe Perches 		netdev_warn(dev->net, "invalid rx length<0 %d\n", skb->len);
12402f7ca802SSteve Glendinning 		return 0;
12412f7ca802SSteve Glendinning 	}
12422f7ca802SSteve Glendinning 
12432f7ca802SSteve Glendinning 	return 1;
12442f7ca802SSteve Glendinning }
12452f7ca802SSteve Glendinning 
1246f7b29271SSteve Glendinning static u32 smsc95xx_calc_csum_preamble(struct sk_buff *skb)
1247f7b29271SSteve Glendinning {
124855508d60SMichał Mirosław 	u16 low_16 = (u16)skb_checksum_start_offset(skb);
124955508d60SMichał Mirosław 	u16 high_16 = low_16 + skb->csum_offset;
1250f7b29271SSteve Glendinning 	return (high_16 << 16) | low_16;
1251f7b29271SSteve Glendinning }
1252f7b29271SSteve Glendinning 
12532f7ca802SSteve Glendinning static struct sk_buff *smsc95xx_tx_fixup(struct usbnet *dev,
12542f7ca802SSteve Glendinning 					 struct sk_buff *skb, gfp_t flags)
12552f7ca802SSteve Glendinning {
125678e47fe4SMichał Mirosław 	bool csum = skb->ip_summed == CHECKSUM_PARTIAL;
1257f7b29271SSteve Glendinning 	int overhead = csum ? SMSC95XX_TX_OVERHEAD_CSUM : SMSC95XX_TX_OVERHEAD;
12582f7ca802SSteve Glendinning 	u32 tx_cmd_a, tx_cmd_b;
12592f7ca802SSteve Glendinning 
1260f7b29271SSteve Glendinning 	/* We do not advertise SG, so skbs should be already linearized */
1261f7b29271SSteve Glendinning 	BUG_ON(skb_shinfo(skb)->nr_frags);
1262f7b29271SSteve Glendinning 
1263f7b29271SSteve Glendinning 	if (skb_headroom(skb) < overhead) {
12642f7ca802SSteve Glendinning 		struct sk_buff *skb2 = skb_copy_expand(skb,
1265f7b29271SSteve Glendinning 			overhead, 0, flags);
12662f7ca802SSteve Glendinning 		dev_kfree_skb_any(skb);
12672f7ca802SSteve Glendinning 		skb = skb2;
12682f7ca802SSteve Glendinning 		if (!skb)
12692f7ca802SSteve Glendinning 			return NULL;
12702f7ca802SSteve Glendinning 	}
12712f7ca802SSteve Glendinning 
1272f7b29271SSteve Glendinning 	if (csum) {
127311bc3088SSteve Glendinning 		if (skb->len <= 45) {
127411bc3088SSteve Glendinning 			/* workaround - hardware tx checksum does not work
127511bc3088SSteve Glendinning 			 * properly with extremely small packets */
127655508d60SMichał Mirosław 			long csstart = skb_checksum_start_offset(skb);
127711bc3088SSteve Glendinning 			__wsum calc = csum_partial(skb->data + csstart,
127811bc3088SSteve Glendinning 				skb->len - csstart, 0);
127911bc3088SSteve Glendinning 			*((__sum16 *)(skb->data + csstart
128011bc3088SSteve Glendinning 				+ skb->csum_offset)) = csum_fold(calc);
128111bc3088SSteve Glendinning 
128211bc3088SSteve Glendinning 			csum = false;
128311bc3088SSteve Glendinning 		} else {
1284f7b29271SSteve Glendinning 			u32 csum_preamble = smsc95xx_calc_csum_preamble(skb);
1285f7b29271SSteve Glendinning 			skb_push(skb, 4);
1286f7b29271SSteve Glendinning 			memcpy(skb->data, &csum_preamble, 4);
1287f7b29271SSteve Glendinning 		}
128811bc3088SSteve Glendinning 	}
1289f7b29271SSteve Glendinning 
12902f7ca802SSteve Glendinning 	skb_push(skb, 4);
12912f7ca802SSteve Glendinning 	tx_cmd_b = (u32)(skb->len - 4);
1292f7b29271SSteve Glendinning 	if (csum)
1293f7b29271SSteve Glendinning 		tx_cmd_b |= TX_CMD_B_CSUM_ENABLE;
12942f7ca802SSteve Glendinning 	cpu_to_le32s(&tx_cmd_b);
12952f7ca802SSteve Glendinning 	memcpy(skb->data, &tx_cmd_b, 4);
12962f7ca802SSteve Glendinning 
12972f7ca802SSteve Glendinning 	skb_push(skb, 4);
12982f7ca802SSteve Glendinning 	tx_cmd_a = (u32)(skb->len - 8) | TX_CMD_A_FIRST_SEG_ |
12992f7ca802SSteve Glendinning 		TX_CMD_A_LAST_SEG_;
13002f7ca802SSteve Glendinning 	cpu_to_le32s(&tx_cmd_a);
13012f7ca802SSteve Glendinning 	memcpy(skb->data, &tx_cmd_a, 4);
13022f7ca802SSteve Glendinning 
13032f7ca802SSteve Glendinning 	return skb;
13042f7ca802SSteve Glendinning }
13052f7ca802SSteve Glendinning 
13062f7ca802SSteve Glendinning static const struct driver_info smsc95xx_info = {
13072f7ca802SSteve Glendinning 	.description	= "smsc95xx USB 2.0 Ethernet",
13082f7ca802SSteve Glendinning 	.bind		= smsc95xx_bind,
13092f7ca802SSteve Glendinning 	.unbind		= smsc95xx_unbind,
13102f7ca802SSteve Glendinning 	.link_reset	= smsc95xx_link_reset,
13112f7ca802SSteve Glendinning 	.reset		= smsc95xx_reset,
13122f7ca802SSteve Glendinning 	.rx_fixup	= smsc95xx_rx_fixup,
13132f7ca802SSteve Glendinning 	.tx_fixup	= smsc95xx_tx_fixup,
13142f7ca802SSteve Glendinning 	.status		= smsc95xx_status,
131507d69d42SPaolo Pisati 	.flags		= FLAG_ETHER | FLAG_SEND_ZLP | FLAG_LINK_INTR,
13162f7ca802SSteve Glendinning };
13172f7ca802SSteve Glendinning 
13182f7ca802SSteve Glendinning static const struct usb_device_id products[] = {
13192f7ca802SSteve Glendinning 	{
13202f7ca802SSteve Glendinning 		/* SMSC9500 USB Ethernet Device */
13212f7ca802SSteve Glendinning 		USB_DEVICE(0x0424, 0x9500),
13222f7ca802SSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
13232f7ca802SSteve Glendinning 	},
1324726474b8SSteve Glendinning 	{
13256f41d12bSSteve Glendinning 		/* SMSC9505 USB Ethernet Device */
13266f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9505),
13276f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
13286f41d12bSSteve Glendinning 	},
13296f41d12bSSteve Glendinning 	{
13306f41d12bSSteve Glendinning 		/* SMSC9500A USB Ethernet Device */
13316f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9E00),
13326f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
13336f41d12bSSteve Glendinning 	},
13346f41d12bSSteve Glendinning 	{
13356f41d12bSSteve Glendinning 		/* SMSC9505A USB Ethernet Device */
13366f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9E01),
13376f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
13386f41d12bSSteve Glendinning 	},
13396f41d12bSSteve Glendinning 	{
1340726474b8SSteve Glendinning 		/* SMSC9512/9514 USB Hub & Ethernet Device */
1341726474b8SSteve Glendinning 		USB_DEVICE(0x0424, 0xec00),
1342726474b8SSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
1343726474b8SSteve Glendinning 	},
13446f41d12bSSteve Glendinning 	{
13456f41d12bSSteve Glendinning 		/* SMSC9500 USB Ethernet Device (SAL10) */
13466f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9900),
13476f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
13486f41d12bSSteve Glendinning 	},
13496f41d12bSSteve Glendinning 	{
13506f41d12bSSteve Glendinning 		/* SMSC9505 USB Ethernet Device (SAL10) */
13516f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9901),
13526f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
13536f41d12bSSteve Glendinning 	},
13546f41d12bSSteve Glendinning 	{
13556f41d12bSSteve Glendinning 		/* SMSC9500A USB Ethernet Device (SAL10) */
13566f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9902),
13576f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
13586f41d12bSSteve Glendinning 	},
13596f41d12bSSteve Glendinning 	{
13606f41d12bSSteve Glendinning 		/* SMSC9505A USB Ethernet Device (SAL10) */
13616f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9903),
13626f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
13636f41d12bSSteve Glendinning 	},
13646f41d12bSSteve Glendinning 	{
13656f41d12bSSteve Glendinning 		/* SMSC9512/9514 USB Hub & Ethernet Device (SAL10) */
13666f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9904),
13676f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
13686f41d12bSSteve Glendinning 	},
13696f41d12bSSteve Glendinning 	{
13706f41d12bSSteve Glendinning 		/* SMSC9500A USB Ethernet Device (HAL) */
13716f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9905),
13726f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
13736f41d12bSSteve Glendinning 	},
13746f41d12bSSteve Glendinning 	{
13756f41d12bSSteve Glendinning 		/* SMSC9505A USB Ethernet Device (HAL) */
13766f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9906),
13776f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
13786f41d12bSSteve Glendinning 	},
13796f41d12bSSteve Glendinning 	{
13806f41d12bSSteve Glendinning 		/* SMSC9500 USB Ethernet Device (Alternate ID) */
13816f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9907),
13826f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
13836f41d12bSSteve Glendinning 	},
13846f41d12bSSteve Glendinning 	{
13856f41d12bSSteve Glendinning 		/* SMSC9500A USB Ethernet Device (Alternate ID) */
13866f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9908),
13876f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
13886f41d12bSSteve Glendinning 	},
13896f41d12bSSteve Glendinning 	{
13906f41d12bSSteve Glendinning 		/* SMSC9512/9514 USB Hub & Ethernet Device (Alternate ID) */
13916f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9909),
13926f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
13936f41d12bSSteve Glendinning 	},
139488edaa41SSteve Glendinning 	{
139588edaa41SSteve Glendinning 		/* SMSC LAN9530 USB Ethernet Device */
139688edaa41SSteve Glendinning 		USB_DEVICE(0x0424, 0x9530),
139788edaa41SSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
139888edaa41SSteve Glendinning 	},
139988edaa41SSteve Glendinning 	{
140088edaa41SSteve Glendinning 		/* SMSC LAN9730 USB Ethernet Device */
140188edaa41SSteve Glendinning 		USB_DEVICE(0x0424, 0x9730),
140288edaa41SSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
140388edaa41SSteve Glendinning 	},
140488edaa41SSteve Glendinning 	{
140588edaa41SSteve Glendinning 		/* SMSC LAN89530 USB Ethernet Device */
140688edaa41SSteve Glendinning 		USB_DEVICE(0x0424, 0x9E08),
140788edaa41SSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
140888edaa41SSteve Glendinning 	},
14092f7ca802SSteve Glendinning 	{ },		/* END */
14102f7ca802SSteve Glendinning };
14112f7ca802SSteve Glendinning MODULE_DEVICE_TABLE(usb, products);
14122f7ca802SSteve Glendinning 
14132f7ca802SSteve Glendinning static struct usb_driver smsc95xx_driver = {
14142f7ca802SSteve Glendinning 	.name		= "smsc95xx",
14152f7ca802SSteve Glendinning 	.id_table	= products,
14162f7ca802SSteve Glendinning 	.probe		= usbnet_probe,
1417b5a04475SSteve Glendinning 	.suspend	= smsc95xx_suspend,
1418e0e474a8SSteve Glendinning 	.resume		= smsc95xx_resume,
1419e0e474a8SSteve Glendinning 	.reset_resume	= smsc95xx_resume,
14202f7ca802SSteve Glendinning 	.disconnect	= usbnet_disconnect,
1421e1f12eb6SSarah Sharp 	.disable_hub_initiated_lpm = 1,
14222f7ca802SSteve Glendinning };
14232f7ca802SSteve Glendinning 
1424d632eb1bSGreg Kroah-Hartman module_usb_driver(smsc95xx_driver);
14252f7ca802SSteve Glendinning 
14262f7ca802SSteve Glendinning MODULE_AUTHOR("Nancy Lin");
142790b24cfbSSteve Glendinning MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>");
14282f7ca802SSteve Glendinning MODULE_DESCRIPTION("SMSC95XX USB 2.0 Ethernet Devices");
14292f7ca802SSteve Glendinning MODULE_LICENSE("GPL");
1430