xref: /linux/drivers/net/usb/smsc95xx.c (revision eed9a72914a2b2737a0d91b80579f878999574ef)
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>
29bbd9f9eeSSteve Glendinning #include <linux/bitrev.h>
30bbd9f9eeSSteve Glendinning #include <linux/crc16.h>
312f7ca802SSteve Glendinning #include <linux/crc32.h>
322f7ca802SSteve Glendinning #include <linux/usb/usbnet.h>
335a0e3ad6STejun Heo #include <linux/slab.h>
342f7ca802SSteve Glendinning #include "smsc95xx.h"
352f7ca802SSteve Glendinning 
362f7ca802SSteve Glendinning #define SMSC_CHIPNAME			"smsc95xx"
37f7b29271SSteve Glendinning #define SMSC_DRIVER_VERSION		"1.0.4"
382f7ca802SSteve Glendinning #define HS_USB_PKT_SIZE			(512)
392f7ca802SSteve Glendinning #define FS_USB_PKT_SIZE			(64)
402f7ca802SSteve Glendinning #define DEFAULT_HS_BURST_CAP_SIZE	(16 * 1024 + 5 * HS_USB_PKT_SIZE)
412f7ca802SSteve Glendinning #define DEFAULT_FS_BURST_CAP_SIZE	(6 * 1024 + 33 * FS_USB_PKT_SIZE)
422f7ca802SSteve Glendinning #define DEFAULT_BULK_IN_DELAY		(0x00002000)
432f7ca802SSteve Glendinning #define MAX_SINGLE_PACKET_SIZE		(2048)
442f7ca802SSteve Glendinning #define LAN95XX_EEPROM_MAGIC		(0x9500)
452f7ca802SSteve Glendinning #define EEPROM_MAC_OFFSET		(0x01)
46f7b29271SSteve Glendinning #define DEFAULT_TX_CSUM_ENABLE		(true)
472f7ca802SSteve Glendinning #define DEFAULT_RX_CSUM_ENABLE		(true)
482f7ca802SSteve Glendinning #define SMSC95XX_INTERNAL_PHY_ID	(1)
492f7ca802SSteve Glendinning #define SMSC95XX_TX_OVERHEAD		(8)
50f7b29271SSteve Glendinning #define SMSC95XX_TX_OVERHEAD_CSUM	(12)
51e5e3af83SSteve Glendinning #define SUPPORTED_WAKE			(WAKE_PHY | WAKE_UCAST | WAKE_BCAST | \
52bbd9f9eeSSteve Glendinning 					 WAKE_MCAST | WAKE_ARP | WAKE_MAGIC)
532f7ca802SSteve Glendinning 
549ebca507SSteve Glendinning #define FEATURE_8_WAKEUP_FILTERS	(0x01)
559ebca507SSteve Glendinning #define FEATURE_PHY_NLP_CROSSOVER	(0x02)
569ebca507SSteve Glendinning #define FEATURE_AUTOSUSPEND		(0x04)
579ebca507SSteve Glendinning 
58769ea6d8SSteve Glendinning #define check_warn(ret, fmt, args...) \
59769ea6d8SSteve Glendinning 	({ if (ret < 0) netdev_warn(dev->net, fmt, ##args); })
60769ea6d8SSteve Glendinning 
61769ea6d8SSteve Glendinning #define check_warn_return(ret, fmt, args...) \
62769ea6d8SSteve Glendinning 	({ if (ret < 0) { netdev_warn(dev->net, fmt, ##args); return ret; } })
63769ea6d8SSteve Glendinning 
64769ea6d8SSteve Glendinning #define check_warn_goto_done(ret, fmt, args...) \
65769ea6d8SSteve Glendinning 	({ if (ret < 0) { netdev_warn(dev->net, fmt, ##args); goto done; } })
66769ea6d8SSteve Glendinning 
672f7ca802SSteve Glendinning struct smsc95xx_priv {
682f7ca802SSteve Glendinning 	u32 mac_cr;
693c0f3c60SMarc Zyngier 	u32 hash_hi;
703c0f3c60SMarc Zyngier 	u32 hash_lo;
71e0e474a8SSteve Glendinning 	u32 wolopts;
722f7ca802SSteve Glendinning 	spinlock_t mac_cr_lock;
739ebca507SSteve Glendinning 	u8 features;
742f7ca802SSteve Glendinning };
752f7ca802SSteve Glendinning 
76eb939922SRusty Russell static bool turbo_mode = true;
772f7ca802SSteve Glendinning module_param(turbo_mode, bool, 0644);
782f7ca802SSteve Glendinning MODULE_PARM_DESC(turbo_mode, "Enable multiple frames per Rx transaction");
792f7ca802SSteve Glendinning 
80ec32115dSMing Lei static int __must_check __smsc95xx_read_reg(struct usbnet *dev, u32 index,
81ec32115dSMing Lei 					    u32 *data, int in_pm)
822f7ca802SSteve Glendinning {
8372108fd2SMing Lei 	u32 buf;
842f7ca802SSteve Glendinning 	int ret;
85ec32115dSMing Lei 	int (*fn)(struct usbnet *, u8, u8, u16, u16, void *, u16);
862f7ca802SSteve Glendinning 
872f7ca802SSteve Glendinning 	BUG_ON(!dev);
882f7ca802SSteve Glendinning 
89ec32115dSMing Lei 	if (!in_pm)
90ec32115dSMing Lei 		fn = usbnet_read_cmd;
91ec32115dSMing Lei 	else
92ec32115dSMing Lei 		fn = usbnet_read_cmd_nopm;
93ec32115dSMing Lei 
94ec32115dSMing Lei 	ret = fn(dev, USB_VENDOR_REQUEST_READ_REGISTER, USB_DIR_IN
95ec32115dSMing Lei 		 | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
9672108fd2SMing Lei 		 0, index, &buf, 4);
972f7ca802SSteve Glendinning 	if (unlikely(ret < 0))
981e1d7412SJoe Perches 		netdev_warn(dev->net, "Failed to read reg index 0x%08x: %d\n",
991e1d7412SJoe Perches 			    index, ret);
1002f7ca802SSteve Glendinning 
10172108fd2SMing Lei 	le32_to_cpus(&buf);
10272108fd2SMing Lei 	*data = buf;
1032f7ca802SSteve Glendinning 
1042f7ca802SSteve Glendinning 	return ret;
1052f7ca802SSteve Glendinning }
1062f7ca802SSteve Glendinning 
107ec32115dSMing Lei static int __must_check __smsc95xx_write_reg(struct usbnet *dev, u32 index,
108ec32115dSMing Lei 					     u32 data, int in_pm)
1092f7ca802SSteve Glendinning {
11072108fd2SMing Lei 	u32 buf;
1112f7ca802SSteve Glendinning 	int ret;
112ec32115dSMing Lei 	int (*fn)(struct usbnet *, u8, u8, u16, u16, const void *, u16);
1132f7ca802SSteve Glendinning 
1142f7ca802SSteve Glendinning 	BUG_ON(!dev);
1152f7ca802SSteve Glendinning 
116ec32115dSMing Lei 	if (!in_pm)
117ec32115dSMing Lei 		fn = usbnet_write_cmd;
118ec32115dSMing Lei 	else
119ec32115dSMing Lei 		fn = usbnet_write_cmd_nopm;
120ec32115dSMing Lei 
12172108fd2SMing Lei 	buf = data;
12272108fd2SMing Lei 	cpu_to_le32s(&buf);
1232f7ca802SSteve Glendinning 
124ec32115dSMing Lei 	ret = fn(dev, USB_VENDOR_REQUEST_WRITE_REGISTER, USB_DIR_OUT
125ec32115dSMing Lei 		 | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
12672108fd2SMing Lei 		 0, index, &buf, 4);
1272f7ca802SSteve Glendinning 	if (unlikely(ret < 0))
1281e1d7412SJoe Perches 		netdev_warn(dev->net, "Failed to write reg index 0x%08x: %d\n",
1291e1d7412SJoe Perches 			    index, ret);
1302f7ca802SSteve Glendinning 
1312f7ca802SSteve Glendinning 	return ret;
1322f7ca802SSteve Glendinning }
1332f7ca802SSteve Glendinning 
134ec32115dSMing Lei static int __must_check smsc95xx_read_reg_nopm(struct usbnet *dev, u32 index,
135ec32115dSMing Lei 					       u32 *data)
136ec32115dSMing Lei {
137ec32115dSMing Lei 	return __smsc95xx_read_reg(dev, index, data, 1);
138ec32115dSMing Lei }
139ec32115dSMing Lei 
140ec32115dSMing Lei static int __must_check smsc95xx_write_reg_nopm(struct usbnet *dev, u32 index,
141ec32115dSMing Lei 						u32 data)
142ec32115dSMing Lei {
143ec32115dSMing Lei 	return __smsc95xx_write_reg(dev, index, data, 1);
144ec32115dSMing Lei }
145ec32115dSMing Lei 
146ec32115dSMing Lei static int __must_check smsc95xx_read_reg(struct usbnet *dev, u32 index,
147ec32115dSMing Lei 					  u32 *data)
148ec32115dSMing Lei {
149ec32115dSMing Lei 	return __smsc95xx_read_reg(dev, index, data, 0);
150ec32115dSMing Lei }
151ec32115dSMing Lei 
152ec32115dSMing Lei static int __must_check smsc95xx_write_reg(struct usbnet *dev, u32 index,
153ec32115dSMing Lei 					   u32 data)
154ec32115dSMing Lei {
155ec32115dSMing Lei 	return __smsc95xx_write_reg(dev, index, data, 0);
156ec32115dSMing Lei }
157e0e474a8SSteve Glendinning static int smsc95xx_set_feature(struct usbnet *dev, u32 feature)
158e0e474a8SSteve Glendinning {
159e0e474a8SSteve Glendinning 	if (WARN_ON_ONCE(!dev))
160e0e474a8SSteve Glendinning 		return -EINVAL;
161e0e474a8SSteve Glendinning 
162ec32115dSMing Lei 	return usbnet_write_cmd_nopm(dev, USB_REQ_SET_FEATURE,
163ec32115dSMing Lei 				     USB_RECIP_DEVICE, feature, 0,
164ec32115dSMing Lei 				     NULL, 0);
165e0e474a8SSteve Glendinning }
166e0e474a8SSteve Glendinning 
167e0e474a8SSteve Glendinning static int smsc95xx_clear_feature(struct usbnet *dev, u32 feature)
168e0e474a8SSteve Glendinning {
169e0e474a8SSteve Glendinning 	if (WARN_ON_ONCE(!dev))
170e0e474a8SSteve Glendinning 		return -EINVAL;
171e0e474a8SSteve Glendinning 
172ec32115dSMing Lei 	return usbnet_write_cmd_nopm(dev, USB_REQ_CLEAR_FEATURE,
173ec32115dSMing Lei 				     USB_RECIP_DEVICE, feature,
174ec32115dSMing Lei 				     0, NULL, 0);
175e0e474a8SSteve Glendinning }
176e0e474a8SSteve Glendinning 
1772f7ca802SSteve Glendinning /* Loop until the read is completed with timeout
1782f7ca802SSteve Glendinning  * called with phy_mutex held */
179e5e3af83SSteve Glendinning static int __must_check __smsc95xx_phy_wait_not_busy(struct usbnet *dev,
180e5e3af83SSteve Glendinning 						     int in_pm)
1812f7ca802SSteve Glendinning {
1822f7ca802SSteve Glendinning 	unsigned long start_time = jiffies;
1832f7ca802SSteve Glendinning 	u32 val;
184769ea6d8SSteve Glendinning 	int ret;
1852f7ca802SSteve Glendinning 
1862f7ca802SSteve Glendinning 	do {
187e5e3af83SSteve Glendinning 		ret = __smsc95xx_read_reg(dev, MII_ADDR, &val, in_pm);
1881e1d7412SJoe Perches 		check_warn_return(ret, "Error reading MII_ACCESS\n");
1892f7ca802SSteve Glendinning 		if (!(val & MII_BUSY_))
1902f7ca802SSteve Glendinning 			return 0;
1912f7ca802SSteve Glendinning 	} while (!time_after(jiffies, start_time + HZ));
1922f7ca802SSteve Glendinning 
1932f7ca802SSteve Glendinning 	return -EIO;
1942f7ca802SSteve Glendinning }
1952f7ca802SSteve Glendinning 
196e5e3af83SSteve Glendinning static int __smsc95xx_mdio_read(struct net_device *netdev, int phy_id, int idx,
197e5e3af83SSteve Glendinning 				int in_pm)
1982f7ca802SSteve Glendinning {
1992f7ca802SSteve Glendinning 	struct usbnet *dev = netdev_priv(netdev);
2002f7ca802SSteve Glendinning 	u32 val, addr;
201769ea6d8SSteve Glendinning 	int ret;
2022f7ca802SSteve Glendinning 
2032f7ca802SSteve Glendinning 	mutex_lock(&dev->phy_mutex);
2042f7ca802SSteve Glendinning 
2052f7ca802SSteve Glendinning 	/* confirm MII not busy */
206e5e3af83SSteve Glendinning 	ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
2071e1d7412SJoe Perches 	check_warn_goto_done(ret, "MII is busy in smsc95xx_mdio_read\n");
2082f7ca802SSteve Glendinning 
2092f7ca802SSteve Glendinning 	/* set the address, index & direction (read from PHY) */
2102f7ca802SSteve Glendinning 	phy_id &= dev->mii.phy_id_mask;
2112f7ca802SSteve Glendinning 	idx &= dev->mii.reg_num_mask;
21280928805SSteve Glendinning 	addr = (phy_id << 11) | (idx << 6) | MII_READ_ | MII_BUSY_;
213e5e3af83SSteve Glendinning 	ret = __smsc95xx_write_reg(dev, MII_ADDR, addr, in_pm);
2141e1d7412SJoe Perches 	check_warn_goto_done(ret, "Error writing MII_ADDR\n");
2152f7ca802SSteve Glendinning 
216e5e3af83SSteve Glendinning 	ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
2171e1d7412SJoe Perches 	check_warn_goto_done(ret, "Timed out reading MII reg %02X\n", idx);
218769ea6d8SSteve Glendinning 
219e5e3af83SSteve Glendinning 	ret = __smsc95xx_read_reg(dev, MII_DATA, &val, in_pm);
2201e1d7412SJoe Perches 	check_warn_goto_done(ret, "Error reading MII_DATA\n");
221769ea6d8SSteve Glendinning 
222769ea6d8SSteve Glendinning 	ret = (u16)(val & 0xFFFF);
223769ea6d8SSteve Glendinning 
224769ea6d8SSteve Glendinning done:
2252f7ca802SSteve Glendinning 	mutex_unlock(&dev->phy_mutex);
226769ea6d8SSteve Glendinning 	return ret;
2272f7ca802SSteve Glendinning }
2282f7ca802SSteve Glendinning 
229e5e3af83SSteve Glendinning static void __smsc95xx_mdio_write(struct net_device *netdev, int phy_id,
230e5e3af83SSteve Glendinning 				  int idx, int regval, int in_pm)
2312f7ca802SSteve Glendinning {
2322f7ca802SSteve Glendinning 	struct usbnet *dev = netdev_priv(netdev);
2332f7ca802SSteve Glendinning 	u32 val, addr;
234769ea6d8SSteve Glendinning 	int ret;
2352f7ca802SSteve Glendinning 
2362f7ca802SSteve Glendinning 	mutex_lock(&dev->phy_mutex);
2372f7ca802SSteve Glendinning 
2382f7ca802SSteve Glendinning 	/* confirm MII not busy */
239e5e3af83SSteve Glendinning 	ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
2401e1d7412SJoe Perches 	check_warn_goto_done(ret, "MII is busy in smsc95xx_mdio_write\n");
2412f7ca802SSteve Glendinning 
2422f7ca802SSteve Glendinning 	val = regval;
243e5e3af83SSteve Glendinning 	ret = __smsc95xx_write_reg(dev, MII_DATA, val, in_pm);
2441e1d7412SJoe Perches 	check_warn_goto_done(ret, "Error writing MII_DATA\n");
2452f7ca802SSteve Glendinning 
2462f7ca802SSteve Glendinning 	/* set the address, index & direction (write to PHY) */
2472f7ca802SSteve Glendinning 	phy_id &= dev->mii.phy_id_mask;
2482f7ca802SSteve Glendinning 	idx &= dev->mii.reg_num_mask;
24980928805SSteve Glendinning 	addr = (phy_id << 11) | (idx << 6) | MII_WRITE_ | MII_BUSY_;
250e5e3af83SSteve Glendinning 	ret = __smsc95xx_write_reg(dev, MII_ADDR, addr, in_pm);
2511e1d7412SJoe Perches 	check_warn_goto_done(ret, "Error writing MII_ADDR\n");
2522f7ca802SSteve Glendinning 
253e5e3af83SSteve Glendinning 	ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
2541e1d7412SJoe Perches 	check_warn_goto_done(ret, "Timed out writing MII reg %02X\n", idx);
2552f7ca802SSteve Glendinning 
256769ea6d8SSteve Glendinning done:
2572f7ca802SSteve Glendinning 	mutex_unlock(&dev->phy_mutex);
2582f7ca802SSteve Glendinning }
2592f7ca802SSteve Glendinning 
260e5e3af83SSteve Glendinning static int smsc95xx_mdio_read_nopm(struct net_device *netdev, int phy_id,
261e5e3af83SSteve Glendinning 				   int idx)
262e5e3af83SSteve Glendinning {
263e5e3af83SSteve Glendinning 	return __smsc95xx_mdio_read(netdev, phy_id, idx, 1);
264e5e3af83SSteve Glendinning }
265e5e3af83SSteve Glendinning 
266e5e3af83SSteve Glendinning static void smsc95xx_mdio_write_nopm(struct net_device *netdev, int phy_id,
267e5e3af83SSteve Glendinning 				     int idx, int regval)
268e5e3af83SSteve Glendinning {
269e5e3af83SSteve Glendinning 	__smsc95xx_mdio_write(netdev, phy_id, idx, regval, 1);
270e5e3af83SSteve Glendinning }
271e5e3af83SSteve Glendinning 
272e5e3af83SSteve Glendinning static int smsc95xx_mdio_read(struct net_device *netdev, int phy_id, int idx)
273e5e3af83SSteve Glendinning {
274e5e3af83SSteve Glendinning 	return __smsc95xx_mdio_read(netdev, phy_id, idx, 0);
275e5e3af83SSteve Glendinning }
276e5e3af83SSteve Glendinning 
277e5e3af83SSteve Glendinning static void smsc95xx_mdio_write(struct net_device *netdev, int phy_id, int idx,
278e5e3af83SSteve Glendinning 				int regval)
279e5e3af83SSteve Glendinning {
280e5e3af83SSteve Glendinning 	__smsc95xx_mdio_write(netdev, phy_id, idx, regval, 0);
281e5e3af83SSteve Glendinning }
282e5e3af83SSteve Glendinning 
283769ea6d8SSteve Glendinning static int __must_check smsc95xx_wait_eeprom(struct usbnet *dev)
2842f7ca802SSteve Glendinning {
2852f7ca802SSteve Glendinning 	unsigned long start_time = jiffies;
2862f7ca802SSteve Glendinning 	u32 val;
287769ea6d8SSteve Glendinning 	int ret;
2882f7ca802SSteve Glendinning 
2892f7ca802SSteve Glendinning 	do {
290769ea6d8SSteve Glendinning 		ret = smsc95xx_read_reg(dev, E2P_CMD, &val);
2911e1d7412SJoe Perches 		check_warn_return(ret, "Error reading E2P_CMD\n");
2922f7ca802SSteve Glendinning 		if (!(val & E2P_CMD_BUSY_) || (val & E2P_CMD_TIMEOUT_))
2932f7ca802SSteve Glendinning 			break;
2942f7ca802SSteve Glendinning 		udelay(40);
2952f7ca802SSteve Glendinning 	} while (!time_after(jiffies, start_time + HZ));
2962f7ca802SSteve Glendinning 
2972f7ca802SSteve Glendinning 	if (val & (E2P_CMD_TIMEOUT_ | E2P_CMD_BUSY_)) {
29860b86755SJoe Perches 		netdev_warn(dev->net, "EEPROM read operation timeout\n");
2992f7ca802SSteve Glendinning 		return -EIO;
3002f7ca802SSteve Glendinning 	}
3012f7ca802SSteve Glendinning 
3022f7ca802SSteve Glendinning 	return 0;
3032f7ca802SSteve Glendinning }
3042f7ca802SSteve Glendinning 
305769ea6d8SSteve Glendinning static int __must_check smsc95xx_eeprom_confirm_not_busy(struct usbnet *dev)
3062f7ca802SSteve Glendinning {
3072f7ca802SSteve Glendinning 	unsigned long start_time = jiffies;
3082f7ca802SSteve Glendinning 	u32 val;
309769ea6d8SSteve Glendinning 	int ret;
3102f7ca802SSteve Glendinning 
3112f7ca802SSteve Glendinning 	do {
312769ea6d8SSteve Glendinning 		ret = smsc95xx_read_reg(dev, E2P_CMD, &val);
3131e1d7412SJoe Perches 		check_warn_return(ret, "Error reading E2P_CMD\n");
3142f7ca802SSteve Glendinning 
3152f7ca802SSteve Glendinning 		if (!(val & E2P_CMD_BUSY_))
3162f7ca802SSteve Glendinning 			return 0;
3172f7ca802SSteve Glendinning 
3182f7ca802SSteve Glendinning 		udelay(40);
3192f7ca802SSteve Glendinning 	} while (!time_after(jiffies, start_time + HZ));
3202f7ca802SSteve Glendinning 
32160b86755SJoe Perches 	netdev_warn(dev->net, "EEPROM is busy\n");
3222f7ca802SSteve Glendinning 	return -EIO;
3232f7ca802SSteve Glendinning }
3242f7ca802SSteve Glendinning 
3252f7ca802SSteve Glendinning static int smsc95xx_read_eeprom(struct usbnet *dev, u32 offset, u32 length,
3262f7ca802SSteve Glendinning 				u8 *data)
3272f7ca802SSteve Glendinning {
3282f7ca802SSteve Glendinning 	u32 val;
3292f7ca802SSteve Glendinning 	int i, ret;
3302f7ca802SSteve Glendinning 
3312f7ca802SSteve Glendinning 	BUG_ON(!dev);
3322f7ca802SSteve Glendinning 	BUG_ON(!data);
3332f7ca802SSteve Glendinning 
3342f7ca802SSteve Glendinning 	ret = smsc95xx_eeprom_confirm_not_busy(dev);
3352f7ca802SSteve Glendinning 	if (ret)
3362f7ca802SSteve Glendinning 		return ret;
3372f7ca802SSteve Glendinning 
3382f7ca802SSteve Glendinning 	for (i = 0; i < length; i++) {
3392f7ca802SSteve Glendinning 		val = E2P_CMD_BUSY_ | E2P_CMD_READ_ | (offset & E2P_CMD_ADDR_);
340769ea6d8SSteve Glendinning 		ret = smsc95xx_write_reg(dev, E2P_CMD, val);
3411e1d7412SJoe Perches 		check_warn_return(ret, "Error writing E2P_CMD\n");
3422f7ca802SSteve Glendinning 
3432f7ca802SSteve Glendinning 		ret = smsc95xx_wait_eeprom(dev);
3442f7ca802SSteve Glendinning 		if (ret < 0)
3452f7ca802SSteve Glendinning 			return ret;
3462f7ca802SSteve Glendinning 
347769ea6d8SSteve Glendinning 		ret = smsc95xx_read_reg(dev, E2P_DATA, &val);
3481e1d7412SJoe Perches 		check_warn_return(ret, "Error reading E2P_DATA\n");
3492f7ca802SSteve Glendinning 
3502f7ca802SSteve Glendinning 		data[i] = val & 0xFF;
3512f7ca802SSteve Glendinning 		offset++;
3522f7ca802SSteve Glendinning 	}
3532f7ca802SSteve Glendinning 
3542f7ca802SSteve Glendinning 	return 0;
3552f7ca802SSteve Glendinning }
3562f7ca802SSteve Glendinning 
3572f7ca802SSteve Glendinning static int smsc95xx_write_eeprom(struct usbnet *dev, u32 offset, u32 length,
3582f7ca802SSteve Glendinning 				 u8 *data)
3592f7ca802SSteve Glendinning {
3602f7ca802SSteve Glendinning 	u32 val;
3612f7ca802SSteve Glendinning 	int i, ret;
3622f7ca802SSteve Glendinning 
3632f7ca802SSteve Glendinning 	BUG_ON(!dev);
3642f7ca802SSteve Glendinning 	BUG_ON(!data);
3652f7ca802SSteve Glendinning 
3662f7ca802SSteve Glendinning 	ret = smsc95xx_eeprom_confirm_not_busy(dev);
3672f7ca802SSteve Glendinning 	if (ret)
3682f7ca802SSteve Glendinning 		return ret;
3692f7ca802SSteve Glendinning 
3702f7ca802SSteve Glendinning 	/* Issue write/erase enable command */
3712f7ca802SSteve Glendinning 	val = E2P_CMD_BUSY_ | E2P_CMD_EWEN_;
372769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, E2P_CMD, val);
3731e1d7412SJoe Perches 	check_warn_return(ret, "Error writing E2P_DATA\n");
3742f7ca802SSteve Glendinning 
3752f7ca802SSteve Glendinning 	ret = smsc95xx_wait_eeprom(dev);
3762f7ca802SSteve Glendinning 	if (ret < 0)
3772f7ca802SSteve Glendinning 		return ret;
3782f7ca802SSteve Glendinning 
3792f7ca802SSteve Glendinning 	for (i = 0; i < length; i++) {
3802f7ca802SSteve Glendinning 
3812f7ca802SSteve Glendinning 		/* Fill data register */
3822f7ca802SSteve Glendinning 		val = data[i];
383769ea6d8SSteve Glendinning 		ret = smsc95xx_write_reg(dev, E2P_DATA, val);
3841e1d7412SJoe Perches 		check_warn_return(ret, "Error writing E2P_DATA\n");
3852f7ca802SSteve Glendinning 
3862f7ca802SSteve Glendinning 		/* Send "write" command */
3872f7ca802SSteve Glendinning 		val = E2P_CMD_BUSY_ | E2P_CMD_WRITE_ | (offset & E2P_CMD_ADDR_);
388769ea6d8SSteve Glendinning 		ret = smsc95xx_write_reg(dev, E2P_CMD, val);
3891e1d7412SJoe Perches 		check_warn_return(ret, "Error writing E2P_CMD\n");
3902f7ca802SSteve Glendinning 
3912f7ca802SSteve Glendinning 		ret = smsc95xx_wait_eeprom(dev);
3922f7ca802SSteve Glendinning 		if (ret < 0)
3932f7ca802SSteve Glendinning 			return ret;
3942f7ca802SSteve Glendinning 
3952f7ca802SSteve Glendinning 		offset++;
3962f7ca802SSteve Glendinning 	}
3972f7ca802SSteve Glendinning 
3982f7ca802SSteve Glendinning 	return 0;
3992f7ca802SSteve Glendinning }
4002f7ca802SSteve Glendinning 
401769ea6d8SSteve Glendinning static int __must_check smsc95xx_write_reg_async(struct usbnet *dev, u16 index,
402769ea6d8SSteve Glendinning 						 u32 *data)
4032f7ca802SSteve Glendinning {
4041d74a6bdSSteve Glendinning 	const u16 size = 4;
40572108fd2SMing Lei 	int ret;
4062f7ca802SSteve Glendinning 
40772108fd2SMing Lei 	ret = usbnet_write_cmd_async(dev, USB_VENDOR_REQUEST_WRITE_REGISTER,
40872108fd2SMing Lei 				     USB_DIR_OUT | USB_TYPE_VENDOR |
40972108fd2SMing Lei 				     USB_RECIP_DEVICE,
41072108fd2SMing Lei 				     0, index, data, size);
41172108fd2SMing Lei 	if (ret < 0)
41272108fd2SMing Lei 		netdev_warn(dev->net, "Error write async cmd, sts=%d\n",
41372108fd2SMing Lei 			    ret);
41472108fd2SMing Lei 	return ret;
4152f7ca802SSteve Glendinning }
4162f7ca802SSteve Glendinning 
4172f7ca802SSteve Glendinning /* returns hash bit number for given MAC address
4182f7ca802SSteve Glendinning  * example:
4192f7ca802SSteve Glendinning  * 01 00 5E 00 00 01 -> returns bit number 31 */
4202f7ca802SSteve Glendinning static unsigned int smsc95xx_hash(char addr[ETH_ALEN])
4212f7ca802SSteve Glendinning {
4222f7ca802SSteve Glendinning 	return (ether_crc(ETH_ALEN, addr) >> 26) & 0x3f;
4232f7ca802SSteve Glendinning }
4242f7ca802SSteve Glendinning 
4252f7ca802SSteve Glendinning static void smsc95xx_set_multicast(struct net_device *netdev)
4262f7ca802SSteve Glendinning {
4272f7ca802SSteve Glendinning 	struct usbnet *dev = netdev_priv(netdev);
4282f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
4292f7ca802SSteve Glendinning 	unsigned long flags;
430769ea6d8SSteve Glendinning 	int ret;
4312f7ca802SSteve Glendinning 
4323c0f3c60SMarc Zyngier 	pdata->hash_hi = 0;
4333c0f3c60SMarc Zyngier 	pdata->hash_lo = 0;
4343c0f3c60SMarc Zyngier 
4352f7ca802SSteve Glendinning 	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
4362f7ca802SSteve Glendinning 
4372f7ca802SSteve Glendinning 	if (dev->net->flags & IFF_PROMISC) {
438a475f603SJoe Perches 		netif_dbg(dev, drv, dev->net, "promiscuous mode enabled\n");
4392f7ca802SSteve Glendinning 		pdata->mac_cr |= MAC_CR_PRMS_;
4402f7ca802SSteve Glendinning 		pdata->mac_cr &= ~(MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
4412f7ca802SSteve Glendinning 	} else if (dev->net->flags & IFF_ALLMULTI) {
442a475f603SJoe Perches 		netif_dbg(dev, drv, dev->net, "receive all multicast enabled\n");
4432f7ca802SSteve Glendinning 		pdata->mac_cr |= MAC_CR_MCPAS_;
4442f7ca802SSteve Glendinning 		pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_HPFILT_);
4454cd24eafSJiri Pirko 	} else if (!netdev_mc_empty(dev->net)) {
44622bedad3SJiri Pirko 		struct netdev_hw_addr *ha;
4472f7ca802SSteve Glendinning 
4482f7ca802SSteve Glendinning 		pdata->mac_cr |= MAC_CR_HPFILT_;
4492f7ca802SSteve Glendinning 		pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_);
4502f7ca802SSteve Glendinning 
45122bedad3SJiri Pirko 		netdev_for_each_mc_addr(ha, netdev) {
45222bedad3SJiri Pirko 			u32 bitnum = smsc95xx_hash(ha->addr);
4532f7ca802SSteve Glendinning 			u32 mask = 0x01 << (bitnum & 0x1F);
4542f7ca802SSteve Glendinning 			if (bitnum & 0x20)
4553c0f3c60SMarc Zyngier 				pdata->hash_hi |= mask;
4562f7ca802SSteve Glendinning 			else
4573c0f3c60SMarc Zyngier 				pdata->hash_lo |= mask;
4582f7ca802SSteve Glendinning 		}
4592f7ca802SSteve Glendinning 
460a475f603SJoe Perches 		netif_dbg(dev, drv, dev->net, "HASHH=0x%08X, HASHL=0x%08X\n",
4613c0f3c60SMarc Zyngier 				   pdata->hash_hi, pdata->hash_lo);
4622f7ca802SSteve Glendinning 	} else {
463a475f603SJoe Perches 		netif_dbg(dev, drv, dev->net, "receive own packets only\n");
4642f7ca802SSteve Glendinning 		pdata->mac_cr &=
4652f7ca802SSteve Glendinning 			~(MAC_CR_PRMS_ | MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
4662f7ca802SSteve Glendinning 	}
4672f7ca802SSteve Glendinning 
4682f7ca802SSteve Glendinning 	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
4692f7ca802SSteve Glendinning 
4702f7ca802SSteve Glendinning 	/* Initiate async writes, as we can't wait for completion here */
471769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg_async(dev, HASHH, &pdata->hash_hi);
4721e1d7412SJoe Perches 	check_warn(ret, "failed to initiate async write to HASHH\n");
473769ea6d8SSteve Glendinning 
474769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg_async(dev, HASHL, &pdata->hash_lo);
4751e1d7412SJoe Perches 	check_warn(ret, "failed to initiate async write to HASHL\n");
476769ea6d8SSteve Glendinning 
477769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg_async(dev, MAC_CR, &pdata->mac_cr);
4781e1d7412SJoe Perches 	check_warn(ret, "failed to initiate async write to MAC_CR\n");
4792f7ca802SSteve Glendinning }
4802f7ca802SSteve Glendinning 
481769ea6d8SSteve Glendinning static int smsc95xx_phy_update_flowcontrol(struct usbnet *dev, u8 duplex,
4822f7ca802SSteve Glendinning 					   u16 lcladv, u16 rmtadv)
4832f7ca802SSteve Glendinning {
4842f7ca802SSteve Glendinning 	u32 flow, afc_cfg = 0;
4852f7ca802SSteve Glendinning 
4862f7ca802SSteve Glendinning 	int ret = smsc95xx_read_reg(dev, AFC_CFG, &afc_cfg);
4871e1d7412SJoe Perches 	check_warn_return(ret, "Error reading AFC_CFG\n");
4882f7ca802SSteve Glendinning 
4892f7ca802SSteve Glendinning 	if (duplex == DUPLEX_FULL) {
490bc02ff95SSteve Glendinning 		u8 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
4912f7ca802SSteve Glendinning 
4922f7ca802SSteve Glendinning 		if (cap & FLOW_CTRL_RX)
4932f7ca802SSteve Glendinning 			flow = 0xFFFF0002;
4942f7ca802SSteve Glendinning 		else
4952f7ca802SSteve Glendinning 			flow = 0;
4962f7ca802SSteve Glendinning 
4972f7ca802SSteve Glendinning 		if (cap & FLOW_CTRL_TX)
4982f7ca802SSteve Glendinning 			afc_cfg |= 0xF;
4992f7ca802SSteve Glendinning 		else
5002f7ca802SSteve Glendinning 			afc_cfg &= ~0xF;
5012f7ca802SSteve Glendinning 
502a475f603SJoe Perches 		netif_dbg(dev, link, dev->net, "rx pause %s, tx pause %s\n",
50360b86755SJoe Perches 				   cap & FLOW_CTRL_RX ? "enabled" : "disabled",
50460b86755SJoe Perches 				   cap & FLOW_CTRL_TX ? "enabled" : "disabled");
5052f7ca802SSteve Glendinning 	} else {
506a475f603SJoe Perches 		netif_dbg(dev, link, dev->net, "half duplex\n");
5072f7ca802SSteve Glendinning 		flow = 0;
5082f7ca802SSteve Glendinning 		afc_cfg |= 0xF;
5092f7ca802SSteve Glendinning 	}
5102f7ca802SSteve Glendinning 
511769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, FLOW, flow);
5121e1d7412SJoe Perches 	check_warn_return(ret, "Error writing FLOW\n");
513769ea6d8SSteve Glendinning 
514769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, AFC_CFG, afc_cfg);
5151e1d7412SJoe Perches 	check_warn_return(ret, "Error writing AFC_CFG\n");
516769ea6d8SSteve Glendinning 
517769ea6d8SSteve Glendinning 	return 0;
5182f7ca802SSteve Glendinning }
5192f7ca802SSteve Glendinning 
5202f7ca802SSteve Glendinning static int smsc95xx_link_reset(struct usbnet *dev)
5212f7ca802SSteve Glendinning {
5222f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
5232f7ca802SSteve Glendinning 	struct mii_if_info *mii = &dev->mii;
5248ae6dacaSDavid Decotigny 	struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
5252f7ca802SSteve Glendinning 	unsigned long flags;
5262f7ca802SSteve Glendinning 	u16 lcladv, rmtadv;
527769ea6d8SSteve Glendinning 	int ret;
5282f7ca802SSteve Glendinning 
5292f7ca802SSteve Glendinning 	/* clear interrupt status */
530769ea6d8SSteve Glendinning 	ret = smsc95xx_mdio_read(dev->net, mii->phy_id, PHY_INT_SRC);
5311e1d7412SJoe Perches 	check_warn_return(ret, "Error reading PHY_INT_SRC\n");
532769ea6d8SSteve Glendinning 
533769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
5341e1d7412SJoe Perches 	check_warn_return(ret, "Error writing INT_STS\n");
5352f7ca802SSteve Glendinning 
5362f7ca802SSteve Glendinning 	mii_check_media(mii, 1, 1);
5372f7ca802SSteve Glendinning 	mii_ethtool_gset(&dev->mii, &ecmd);
5382f7ca802SSteve Glendinning 	lcladv = smsc95xx_mdio_read(dev->net, mii->phy_id, MII_ADVERTISE);
5392f7ca802SSteve Glendinning 	rmtadv = smsc95xx_mdio_read(dev->net, mii->phy_id, MII_LPA);
5402f7ca802SSteve Glendinning 
5418ae6dacaSDavid Decotigny 	netif_dbg(dev, link, dev->net,
5428ae6dacaSDavid Decotigny 		  "speed: %u duplex: %d lcladv: %04x rmtadv: %04x\n",
5438ae6dacaSDavid Decotigny 		  ethtool_cmd_speed(&ecmd), ecmd.duplex, lcladv, rmtadv);
5442f7ca802SSteve Glendinning 
5452f7ca802SSteve Glendinning 	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
5462f7ca802SSteve Glendinning 	if (ecmd.duplex != DUPLEX_FULL) {
5472f7ca802SSteve Glendinning 		pdata->mac_cr &= ~MAC_CR_FDPX_;
5482f7ca802SSteve Glendinning 		pdata->mac_cr |= MAC_CR_RCVOWN_;
5492f7ca802SSteve Glendinning 	} else {
5502f7ca802SSteve Glendinning 		pdata->mac_cr &= ~MAC_CR_RCVOWN_;
5512f7ca802SSteve Glendinning 		pdata->mac_cr |= MAC_CR_FDPX_;
5522f7ca802SSteve Glendinning 	}
5532f7ca802SSteve Glendinning 	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
5542f7ca802SSteve Glendinning 
555769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
5561e1d7412SJoe Perches 	check_warn_return(ret, "Error writing MAC_CR\n");
5572f7ca802SSteve Glendinning 
558769ea6d8SSteve Glendinning 	ret = smsc95xx_phy_update_flowcontrol(dev, ecmd.duplex, lcladv, rmtadv);
5591e1d7412SJoe Perches 	check_warn_return(ret, "Error updating PHY flow control\n");
5602f7ca802SSteve Glendinning 
5612f7ca802SSteve Glendinning 	return 0;
5622f7ca802SSteve Glendinning }
5632f7ca802SSteve Glendinning 
5642f7ca802SSteve Glendinning static void smsc95xx_status(struct usbnet *dev, struct urb *urb)
5652f7ca802SSteve Glendinning {
5662f7ca802SSteve Glendinning 	u32 intdata;
5672f7ca802SSteve Glendinning 
5682f7ca802SSteve Glendinning 	if (urb->actual_length != 4) {
56960b86755SJoe Perches 		netdev_warn(dev->net, "unexpected urb length %d\n",
57060b86755SJoe Perches 			    urb->actual_length);
5712f7ca802SSteve Glendinning 		return;
5722f7ca802SSteve Glendinning 	}
5732f7ca802SSteve Glendinning 
5742f7ca802SSteve Glendinning 	memcpy(&intdata, urb->transfer_buffer, 4);
5751d74a6bdSSteve Glendinning 	le32_to_cpus(&intdata);
5762f7ca802SSteve Glendinning 
577a475f603SJoe Perches 	netif_dbg(dev, link, dev->net, "intdata: 0x%08X\n", intdata);
5782f7ca802SSteve Glendinning 
5792f7ca802SSteve Glendinning 	if (intdata & INT_ENP_PHY_INT_)
5802f7ca802SSteve Glendinning 		usbnet_defer_kevent(dev, EVENT_LINK_RESET);
5812f7ca802SSteve Glendinning 	else
58260b86755SJoe Perches 		netdev_warn(dev->net, "unexpected interrupt, intdata=0x%08X\n",
58360b86755SJoe Perches 			    intdata);
5842f7ca802SSteve Glendinning }
5852f7ca802SSteve Glendinning 
586f7b29271SSteve Glendinning /* Enable or disable Tx & Rx checksum offload engines */
587c8f44affSMichał Mirosław static int smsc95xx_set_features(struct net_device *netdev,
588c8f44affSMichał Mirosław 	netdev_features_t features)
5892f7ca802SSteve Glendinning {
59078e47fe4SMichał Mirosław 	struct usbnet *dev = netdev_priv(netdev);
5912f7ca802SSteve Glendinning 	u32 read_buf;
59278e47fe4SMichał Mirosław 	int ret;
59378e47fe4SMichał Mirosław 
59478e47fe4SMichał Mirosław 	ret = smsc95xx_read_reg(dev, COE_CR, &read_buf);
595769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to read COE_CR: %d\n", ret);
5962f7ca802SSteve Glendinning 
59778e47fe4SMichał Mirosław 	if (features & NETIF_F_HW_CSUM)
598f7b29271SSteve Glendinning 		read_buf |= Tx_COE_EN_;
599f7b29271SSteve Glendinning 	else
600f7b29271SSteve Glendinning 		read_buf &= ~Tx_COE_EN_;
601f7b29271SSteve Glendinning 
60278e47fe4SMichał Mirosław 	if (features & NETIF_F_RXCSUM)
6032f7ca802SSteve Glendinning 		read_buf |= Rx_COE_EN_;
6042f7ca802SSteve Glendinning 	else
6052f7ca802SSteve Glendinning 		read_buf &= ~Rx_COE_EN_;
6062f7ca802SSteve Glendinning 
6072f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, COE_CR, read_buf);
608769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write COE_CR: %d\n", ret);
6092f7ca802SSteve Glendinning 
610a475f603SJoe Perches 	netif_dbg(dev, hw, dev->net, "COE_CR = 0x%08x\n", read_buf);
6112f7ca802SSteve Glendinning 	return 0;
6122f7ca802SSteve Glendinning }
6132f7ca802SSteve Glendinning 
6142f7ca802SSteve Glendinning static int smsc95xx_ethtool_get_eeprom_len(struct net_device *net)
6152f7ca802SSteve Glendinning {
6162f7ca802SSteve Glendinning 	return MAX_EEPROM_SIZE;
6172f7ca802SSteve Glendinning }
6182f7ca802SSteve Glendinning 
6192f7ca802SSteve Glendinning static int smsc95xx_ethtool_get_eeprom(struct net_device *netdev,
6202f7ca802SSteve Glendinning 				       struct ethtool_eeprom *ee, u8 *data)
6212f7ca802SSteve Glendinning {
6222f7ca802SSteve Glendinning 	struct usbnet *dev = netdev_priv(netdev);
6232f7ca802SSteve Glendinning 
6242f7ca802SSteve Glendinning 	ee->magic = LAN95XX_EEPROM_MAGIC;
6252f7ca802SSteve Glendinning 
6262f7ca802SSteve Glendinning 	return smsc95xx_read_eeprom(dev, ee->offset, ee->len, data);
6272f7ca802SSteve Glendinning }
6282f7ca802SSteve Glendinning 
6292f7ca802SSteve Glendinning static int smsc95xx_ethtool_set_eeprom(struct net_device *netdev,
6302f7ca802SSteve Glendinning 				       struct ethtool_eeprom *ee, u8 *data)
6312f7ca802SSteve Glendinning {
6322f7ca802SSteve Glendinning 	struct usbnet *dev = netdev_priv(netdev);
6332f7ca802SSteve Glendinning 
6342f7ca802SSteve Glendinning 	if (ee->magic != LAN95XX_EEPROM_MAGIC) {
63560b86755SJoe Perches 		netdev_warn(dev->net, "EEPROM: magic value mismatch, magic = 0x%x\n",
6362f7ca802SSteve Glendinning 			    ee->magic);
6372f7ca802SSteve Glendinning 		return -EINVAL;
6382f7ca802SSteve Glendinning 	}
6392f7ca802SSteve Glendinning 
6402f7ca802SSteve Glendinning 	return smsc95xx_write_eeprom(dev, ee->offset, ee->len, data);
6412f7ca802SSteve Glendinning }
6422f7ca802SSteve Glendinning 
6439fa32e94SEmeric Vigier static int smsc95xx_ethtool_getregslen(struct net_device *netdev)
6449fa32e94SEmeric Vigier {
6459fa32e94SEmeric Vigier 	/* all smsc95xx registers */
6469fa32e94SEmeric Vigier 	return COE_CR - ID_REV + 1;
6479fa32e94SEmeric Vigier }
6489fa32e94SEmeric Vigier 
6499fa32e94SEmeric Vigier static void
6509fa32e94SEmeric Vigier smsc95xx_ethtool_getregs(struct net_device *netdev, struct ethtool_regs *regs,
6519fa32e94SEmeric Vigier 			 void *buf)
6529fa32e94SEmeric Vigier {
6539fa32e94SEmeric Vigier 	struct usbnet *dev = netdev_priv(netdev);
654d348446bSDan Carpenter 	unsigned int i, j;
655d348446bSDan Carpenter 	int retval;
6569fa32e94SEmeric Vigier 	u32 *data = buf;
6579fa32e94SEmeric Vigier 
6589fa32e94SEmeric Vigier 	retval = smsc95xx_read_reg(dev, ID_REV, &regs->version);
6599fa32e94SEmeric Vigier 	if (retval < 0) {
6609fa32e94SEmeric Vigier 		netdev_warn(netdev, "REGS: cannot read ID_REV\n");
6619fa32e94SEmeric Vigier 		return;
6629fa32e94SEmeric Vigier 	}
6639fa32e94SEmeric Vigier 
6649fa32e94SEmeric Vigier 	for (i = ID_REV, j = 0; i <= COE_CR; i += (sizeof(u32)), j++) {
6659fa32e94SEmeric Vigier 		retval = smsc95xx_read_reg(dev, i, &data[j]);
6669fa32e94SEmeric Vigier 		if (retval < 0) {
6679fa32e94SEmeric Vigier 			netdev_warn(netdev, "REGS: cannot read reg[%x]\n", i);
6689fa32e94SEmeric Vigier 			return;
6699fa32e94SEmeric Vigier 		}
6709fa32e94SEmeric Vigier 	}
6719fa32e94SEmeric Vigier }
6729fa32e94SEmeric Vigier 
673e0e474a8SSteve Glendinning static void smsc95xx_ethtool_get_wol(struct net_device *net,
674e0e474a8SSteve Glendinning 				     struct ethtool_wolinfo *wolinfo)
675e0e474a8SSteve Glendinning {
676e0e474a8SSteve Glendinning 	struct usbnet *dev = netdev_priv(net);
677e0e474a8SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
678e0e474a8SSteve Glendinning 
679e0e474a8SSteve Glendinning 	wolinfo->supported = SUPPORTED_WAKE;
680e0e474a8SSteve Glendinning 	wolinfo->wolopts = pdata->wolopts;
681e0e474a8SSteve Glendinning }
682e0e474a8SSteve Glendinning 
683e0e474a8SSteve Glendinning static int smsc95xx_ethtool_set_wol(struct net_device *net,
684e0e474a8SSteve Glendinning 				    struct ethtool_wolinfo *wolinfo)
685e0e474a8SSteve Glendinning {
686e0e474a8SSteve Glendinning 	struct usbnet *dev = netdev_priv(net);
687e0e474a8SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
688e0e474a8SSteve Glendinning 
689e0e474a8SSteve Glendinning 	pdata->wolopts = wolinfo->wolopts & SUPPORTED_WAKE;
690e0e474a8SSteve Glendinning 	return 0;
691e0e474a8SSteve Glendinning }
692e0e474a8SSteve Glendinning 
6930fc0b732SStephen Hemminger static const struct ethtool_ops smsc95xx_ethtool_ops = {
6942f7ca802SSteve Glendinning 	.get_link	= usbnet_get_link,
6952f7ca802SSteve Glendinning 	.nway_reset	= usbnet_nway_reset,
6962f7ca802SSteve Glendinning 	.get_drvinfo	= usbnet_get_drvinfo,
6972f7ca802SSteve Glendinning 	.get_msglevel	= usbnet_get_msglevel,
6982f7ca802SSteve Glendinning 	.set_msglevel	= usbnet_set_msglevel,
6992f7ca802SSteve Glendinning 	.get_settings	= usbnet_get_settings,
7002f7ca802SSteve Glendinning 	.set_settings	= usbnet_set_settings,
7012f7ca802SSteve Glendinning 	.get_eeprom_len	= smsc95xx_ethtool_get_eeprom_len,
7022f7ca802SSteve Glendinning 	.get_eeprom	= smsc95xx_ethtool_get_eeprom,
7032f7ca802SSteve Glendinning 	.set_eeprom	= smsc95xx_ethtool_set_eeprom,
7049fa32e94SEmeric Vigier 	.get_regs_len	= smsc95xx_ethtool_getregslen,
7059fa32e94SEmeric Vigier 	.get_regs	= smsc95xx_ethtool_getregs,
706e0e474a8SSteve Glendinning 	.get_wol	= smsc95xx_ethtool_get_wol,
707e0e474a8SSteve Glendinning 	.set_wol	= smsc95xx_ethtool_set_wol,
7082f7ca802SSteve Glendinning };
7092f7ca802SSteve Glendinning 
7102f7ca802SSteve Glendinning static int smsc95xx_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
7112f7ca802SSteve Glendinning {
7122f7ca802SSteve Glendinning 	struct usbnet *dev = netdev_priv(netdev);
7132f7ca802SSteve Glendinning 
7142f7ca802SSteve Glendinning 	if (!netif_running(netdev))
7152f7ca802SSteve Glendinning 		return -EINVAL;
7162f7ca802SSteve Glendinning 
7172f7ca802SSteve Glendinning 	return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
7182f7ca802SSteve Glendinning }
7192f7ca802SSteve Glendinning 
7202f7ca802SSteve Glendinning static void smsc95xx_init_mac_address(struct usbnet *dev)
7212f7ca802SSteve Glendinning {
7222f7ca802SSteve Glendinning 	/* try reading mac address from EEPROM */
7232f7ca802SSteve Glendinning 	if (smsc95xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN,
7242f7ca802SSteve Glendinning 			dev->net->dev_addr) == 0) {
7252f7ca802SSteve Glendinning 		if (is_valid_ether_addr(dev->net->dev_addr)) {
7262f7ca802SSteve Glendinning 			/* eeprom values are valid so use them */
727a475f603SJoe Perches 			netif_dbg(dev, ifup, dev->net, "MAC address read from EEPROM\n");
7282f7ca802SSteve Glendinning 			return;
7292f7ca802SSteve Glendinning 		}
7302f7ca802SSteve Glendinning 	}
7312f7ca802SSteve Glendinning 
7322f7ca802SSteve Glendinning 	/* no eeprom, or eeprom values are invalid. generate random MAC */
733f2cedb63SDanny Kukawka 	eth_hw_addr_random(dev->net);
734c7e12eadSJoe Perches 	netif_dbg(dev, ifup, dev->net, "MAC address set to eth_random_addr\n");
7352f7ca802SSteve Glendinning }
7362f7ca802SSteve Glendinning 
7372f7ca802SSteve Glendinning static int smsc95xx_set_mac_address(struct usbnet *dev)
7382f7ca802SSteve Glendinning {
7392f7ca802SSteve Glendinning 	u32 addr_lo = dev->net->dev_addr[0] | dev->net->dev_addr[1] << 8 |
7402f7ca802SSteve Glendinning 		dev->net->dev_addr[2] << 16 | dev->net->dev_addr[3] << 24;
7412f7ca802SSteve Glendinning 	u32 addr_hi = dev->net->dev_addr[4] | dev->net->dev_addr[5] << 8;
7422f7ca802SSteve Glendinning 	int ret;
7432f7ca802SSteve Glendinning 
7442f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, ADDRL, addr_lo);
745769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write ADDRL: %d\n", ret);
7462f7ca802SSteve Glendinning 
7472f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, ADDRH, addr_hi);
748769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write ADDRH: %d\n", ret);
7492f7ca802SSteve Glendinning 
7502f7ca802SSteve Glendinning 	return 0;
7512f7ca802SSteve Glendinning }
7522f7ca802SSteve Glendinning 
7532f7ca802SSteve Glendinning /* starts the TX path */
754769ea6d8SSteve Glendinning static int smsc95xx_start_tx_path(struct usbnet *dev)
7552f7ca802SSteve Glendinning {
7562f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
7572f7ca802SSteve Glendinning 	unsigned long flags;
758769ea6d8SSteve Glendinning 	int ret;
7592f7ca802SSteve Glendinning 
7602f7ca802SSteve Glendinning 	/* Enable Tx at MAC */
7612f7ca802SSteve Glendinning 	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
7622f7ca802SSteve Glendinning 	pdata->mac_cr |= MAC_CR_TXEN_;
7632f7ca802SSteve Glendinning 	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
7642f7ca802SSteve Glendinning 
765769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
766769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write MAC_CR: %d\n", ret);
7672f7ca802SSteve Glendinning 
7682f7ca802SSteve Glendinning 	/* Enable Tx at SCSRs */
769769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, TX_CFG, TX_CFG_ON_);
770769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write TX_CFG: %d\n", ret);
771769ea6d8SSteve Glendinning 
772769ea6d8SSteve Glendinning 	return 0;
7732f7ca802SSteve Glendinning }
7742f7ca802SSteve Glendinning 
7752f7ca802SSteve Glendinning /* Starts the Receive path */
776ec32115dSMing Lei static int smsc95xx_start_rx_path(struct usbnet *dev, int in_pm)
7772f7ca802SSteve Glendinning {
7782f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
7792f7ca802SSteve Glendinning 	unsigned long flags;
780769ea6d8SSteve Glendinning 	int ret;
7812f7ca802SSteve Glendinning 
7822f7ca802SSteve Glendinning 	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
7832f7ca802SSteve Glendinning 	pdata->mac_cr |= MAC_CR_RXEN_;
7842f7ca802SSteve Glendinning 	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
7852f7ca802SSteve Glendinning 
786ec32115dSMing Lei 	ret = __smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr, in_pm);
787769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write MAC_CR: %d\n", ret);
788769ea6d8SSteve Glendinning 
789769ea6d8SSteve Glendinning 	return 0;
7902f7ca802SSteve Glendinning }
7912f7ca802SSteve Glendinning 
7922f7ca802SSteve Glendinning static int smsc95xx_phy_initialize(struct usbnet *dev)
7932f7ca802SSteve Glendinning {
794769ea6d8SSteve Glendinning 	int bmcr, ret, timeout = 0;
795db443c44SSteve Glendinning 
7962f7ca802SSteve Glendinning 	/* Initialize MII structure */
7972f7ca802SSteve Glendinning 	dev->mii.dev = dev->net;
7982f7ca802SSteve Glendinning 	dev->mii.mdio_read = smsc95xx_mdio_read;
7992f7ca802SSteve Glendinning 	dev->mii.mdio_write = smsc95xx_mdio_write;
8002f7ca802SSteve Glendinning 	dev->mii.phy_id_mask = 0x1f;
8012f7ca802SSteve Glendinning 	dev->mii.reg_num_mask = 0x1f;
8022f7ca802SSteve Glendinning 	dev->mii.phy_id = SMSC95XX_INTERNAL_PHY_ID;
8032f7ca802SSteve Glendinning 
804db443c44SSteve Glendinning 	/* reset phy and wait for reset to complete */
8052f7ca802SSteve Glendinning 	smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
806db443c44SSteve Glendinning 
807db443c44SSteve Glendinning 	do {
808db443c44SSteve Glendinning 		msleep(10);
809db443c44SSteve Glendinning 		bmcr = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, MII_BMCR);
810db443c44SSteve Glendinning 		timeout++;
811d9460920SRabin Vincent 	} while ((bmcr & BMCR_RESET) && (timeout < 100));
812db443c44SSteve Glendinning 
813db443c44SSteve Glendinning 	if (timeout >= 100) {
814db443c44SSteve Glendinning 		netdev_warn(dev->net, "timeout on PHY Reset");
815db443c44SSteve Glendinning 		return -EIO;
816db443c44SSteve Glendinning 	}
817db443c44SSteve Glendinning 
8182f7ca802SSteve Glendinning 	smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
8192f7ca802SSteve Glendinning 		ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP |
8202f7ca802SSteve Glendinning 		ADVERTISE_PAUSE_ASYM);
8212f7ca802SSteve Glendinning 
8222f7ca802SSteve Glendinning 	/* read to clear */
823769ea6d8SSteve Glendinning 	ret = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, PHY_INT_SRC);
8241e1d7412SJoe Perches 	check_warn_return(ret, "Failed to read PHY_INT_SRC during init\n");
8252f7ca802SSteve Glendinning 
8262f7ca802SSteve Glendinning 	smsc95xx_mdio_write(dev->net, dev->mii.phy_id, PHY_INT_MASK,
8272f7ca802SSteve Glendinning 		PHY_INT_MASK_DEFAULT_);
8282f7ca802SSteve Glendinning 	mii_nway_restart(&dev->mii);
8292f7ca802SSteve Glendinning 
830a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net, "phy initialised successfully\n");
8312f7ca802SSteve Glendinning 	return 0;
8322f7ca802SSteve Glendinning }
8332f7ca802SSteve Glendinning 
8342f7ca802SSteve Glendinning static int smsc95xx_reset(struct usbnet *dev)
8352f7ca802SSteve Glendinning {
8362f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
8372f7ca802SSteve Glendinning 	u32 read_buf, write_buf, burst_cap;
8382f7ca802SSteve Glendinning 	int ret = 0, timeout;
8392f7ca802SSteve Glendinning 
840a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net, "entering smsc95xx_reset\n");
8412f7ca802SSteve Glendinning 
8424436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, HW_CFG, HW_CFG_LRST_);
843769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write HW_CFG_LRST_ bit in HW_CFG\n");
8442f7ca802SSteve Glendinning 
8452f7ca802SSteve Glendinning 	timeout = 0;
8462f7ca802SSteve Glendinning 	do {
847cf2acec2SSteve Glendinning 		msleep(10);
8482f7ca802SSteve Glendinning 		ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
849769ea6d8SSteve Glendinning 		check_warn_return(ret, "Failed to read HW_CFG: %d\n", ret);
8502f7ca802SSteve Glendinning 		timeout++;
8512f7ca802SSteve Glendinning 	} while ((read_buf & HW_CFG_LRST_) && (timeout < 100));
8522f7ca802SSteve Glendinning 
8532f7ca802SSteve Glendinning 	if (timeout >= 100) {
85460b86755SJoe Perches 		netdev_warn(dev->net, "timeout waiting for completion of Lite Reset\n");
8552f7ca802SSteve Glendinning 		return ret;
8562f7ca802SSteve Glendinning 	}
8572f7ca802SSteve Glendinning 
8584436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, PM_CTRL, PM_CTL_PHY_RST_);
859769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write PM_CTRL: %d\n", ret);
8602f7ca802SSteve Glendinning 
8612f7ca802SSteve Glendinning 	timeout = 0;
8622f7ca802SSteve Glendinning 	do {
863cf2acec2SSteve Glendinning 		msleep(10);
8642f7ca802SSteve Glendinning 		ret = smsc95xx_read_reg(dev, PM_CTRL, &read_buf);
865769ea6d8SSteve Glendinning 		check_warn_return(ret, "Failed to read PM_CTRL: %d\n", ret);
8662f7ca802SSteve Glendinning 		timeout++;
8672f7ca802SSteve Glendinning 	} while ((read_buf & PM_CTL_PHY_RST_) && (timeout < 100));
8682f7ca802SSteve Glendinning 
8692f7ca802SSteve Glendinning 	if (timeout >= 100) {
87060b86755SJoe Perches 		netdev_warn(dev->net, "timeout waiting for PHY Reset\n");
8712f7ca802SSteve Glendinning 		return ret;
8722f7ca802SSteve Glendinning 	}
8732f7ca802SSteve Glendinning 
8742f7ca802SSteve Glendinning 	ret = smsc95xx_set_mac_address(dev);
8752f7ca802SSteve Glendinning 	if (ret < 0)
8762f7ca802SSteve Glendinning 		return ret;
8772f7ca802SSteve Glendinning 
8781e1d7412SJoe Perches 	netif_dbg(dev, ifup, dev->net, "MAC Address: %pM\n",
8791e1d7412SJoe Perches 		  dev->net->dev_addr);
8802f7ca802SSteve Glendinning 
8812f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
882769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to read HW_CFG: %d\n", ret);
8832f7ca802SSteve Glendinning 
8841e1d7412SJoe Perches 	netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG : 0x%08x\n",
8851e1d7412SJoe Perches 		  read_buf);
8862f7ca802SSteve Glendinning 
8872f7ca802SSteve Glendinning 	read_buf |= HW_CFG_BIR_;
8882f7ca802SSteve Glendinning 
8892f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
890769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write HW_CFG_BIR_ bit in HW_CFG\n");
8912f7ca802SSteve Glendinning 
8922f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
893769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to read HW_CFG: %d\n", ret);
894a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net,
895a475f603SJoe Perches 		  "Read Value from HW_CFG after writing HW_CFG_BIR_: 0x%08x\n",
89660b86755SJoe Perches 		  read_buf);
8972f7ca802SSteve Glendinning 
8982f7ca802SSteve Glendinning 	if (!turbo_mode) {
8992f7ca802SSteve Glendinning 		burst_cap = 0;
9002f7ca802SSteve Glendinning 		dev->rx_urb_size = MAX_SINGLE_PACKET_SIZE;
9012f7ca802SSteve Glendinning 	} else if (dev->udev->speed == USB_SPEED_HIGH) {
9022f7ca802SSteve Glendinning 		burst_cap = DEFAULT_HS_BURST_CAP_SIZE / HS_USB_PKT_SIZE;
9032f7ca802SSteve Glendinning 		dev->rx_urb_size = DEFAULT_HS_BURST_CAP_SIZE;
9042f7ca802SSteve Glendinning 	} else {
9052f7ca802SSteve Glendinning 		burst_cap = DEFAULT_FS_BURST_CAP_SIZE / FS_USB_PKT_SIZE;
9062f7ca802SSteve Glendinning 		dev->rx_urb_size = DEFAULT_FS_BURST_CAP_SIZE;
9072f7ca802SSteve Glendinning 	}
9082f7ca802SSteve Glendinning 
9091e1d7412SJoe Perches 	netif_dbg(dev, ifup, dev->net, "rx_urb_size=%ld\n",
9101e1d7412SJoe Perches 		  (ulong)dev->rx_urb_size);
9112f7ca802SSteve Glendinning 
9122f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, BURST_CAP, burst_cap);
913769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write BURST_CAP: %d\n", ret);
9142f7ca802SSteve Glendinning 
9152f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, BURST_CAP, &read_buf);
916769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to read BURST_CAP: %d\n", ret);
917769ea6d8SSteve Glendinning 
918a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net,
919a475f603SJoe Perches 		  "Read Value from BURST_CAP after writing: 0x%08x\n",
9202f7ca802SSteve Glendinning 		  read_buf);
9212f7ca802SSteve Glendinning 
9224436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, BULK_IN_DLY, DEFAULT_BULK_IN_DELAY);
923769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write BULK_IN_DLY: %d\n", ret);
9242f7ca802SSteve Glendinning 
9252f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, BULK_IN_DLY, &read_buf);
926769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to read BULK_IN_DLY: %d\n", ret);
927769ea6d8SSteve Glendinning 
928a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net,
929a475f603SJoe Perches 		  "Read Value from BULK_IN_DLY after writing: 0x%08x\n",
93060b86755SJoe Perches 		  read_buf);
9312f7ca802SSteve Glendinning 
9322f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
933769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to read HW_CFG: %d\n", ret);
934769ea6d8SSteve Glendinning 
9351e1d7412SJoe Perches 	netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG: 0x%08x\n",
9361e1d7412SJoe Perches 		  read_buf);
9372f7ca802SSteve Glendinning 
9382f7ca802SSteve Glendinning 	if (turbo_mode)
9392f7ca802SSteve Glendinning 		read_buf |= (HW_CFG_MEF_ | HW_CFG_BCE_);
9402f7ca802SSteve Glendinning 
9412f7ca802SSteve Glendinning 	read_buf &= ~HW_CFG_RXDOFF_;
9422f7ca802SSteve Glendinning 
9432f7ca802SSteve Glendinning 	/* set Rx data offset=2, Make IP header aligns on word boundary. */
9442f7ca802SSteve Glendinning 	read_buf |= NET_IP_ALIGN << 9;
9452f7ca802SSteve Glendinning 
9462f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
947769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write HW_CFG: %d\n", ret);
9482f7ca802SSteve Glendinning 
9492f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
950769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to read HW_CFG: %d\n", ret);
951769ea6d8SSteve Glendinning 
952a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net,
953a475f603SJoe Perches 		  "Read Value from HW_CFG after writing: 0x%08x\n", read_buf);
9542f7ca802SSteve Glendinning 
9554436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
956769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write INT_STS: %d\n", ret);
9572f7ca802SSteve Glendinning 
9582f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, ID_REV, &read_buf);
959769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to read ID_REV: %d\n", ret);
960a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net, "ID_REV = 0x%08x\n", read_buf);
9612f7ca802SSteve Glendinning 
962f293501cSSteve Glendinning 	/* Configure GPIO pins as LED outputs */
963f293501cSSteve Glendinning 	write_buf = LED_GPIO_CFG_SPD_LED | LED_GPIO_CFG_LNK_LED |
964f293501cSSteve Glendinning 		LED_GPIO_CFG_FDX_LED;
965f293501cSSteve Glendinning 	ret = smsc95xx_write_reg(dev, LED_GPIO_CFG, write_buf);
966769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write LED_GPIO_CFG: %d\n", ret);
967f293501cSSteve Glendinning 
9682f7ca802SSteve Glendinning 	/* Init Tx */
9694436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, FLOW, 0);
970769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write FLOW: %d\n", ret);
9712f7ca802SSteve Glendinning 
9724436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, AFC_CFG, AFC_CFG_DEFAULT);
973769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write AFC_CFG: %d\n", ret);
9742f7ca802SSteve Glendinning 
9752f7ca802SSteve Glendinning 	/* Don't need mac_cr_lock during initialisation */
9762f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, MAC_CR, &pdata->mac_cr);
977769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to read MAC_CR: %d\n", ret);
9782f7ca802SSteve Glendinning 
9792f7ca802SSteve Glendinning 	/* Init Rx */
9802f7ca802SSteve Glendinning 	/* Set Vlan */
9814436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, VLAN1, (u32)ETH_P_8021Q);
982769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write VLAN1: %d\n", ret);
9832f7ca802SSteve Glendinning 
984f7b29271SSteve Glendinning 	/* Enable or disable checksum offload engines */
985769ea6d8SSteve Glendinning 	ret = smsc95xx_set_features(dev->net, dev->net->features);
9861e1d7412SJoe Perches 	check_warn_return(ret, "Failed to set checksum offload features\n");
9872f7ca802SSteve Glendinning 
9882f7ca802SSteve Glendinning 	smsc95xx_set_multicast(dev->net);
9892f7ca802SSteve Glendinning 
990769ea6d8SSteve Glendinning 	ret = smsc95xx_phy_initialize(dev);
9911e1d7412SJoe Perches 	check_warn_return(ret, "Failed to init PHY\n");
9922f7ca802SSteve Glendinning 
9932f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, INT_EP_CTL, &read_buf);
994769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to read INT_EP_CTL: %d\n", ret);
9952f7ca802SSteve Glendinning 
9962f7ca802SSteve Glendinning 	/* enable PHY interrupts */
9972f7ca802SSteve Glendinning 	read_buf |= INT_EP_CTL_PHY_INT_;
9982f7ca802SSteve Glendinning 
9992f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, INT_EP_CTL, read_buf);
1000769ea6d8SSteve Glendinning 	check_warn_return(ret, "Failed to write INT_EP_CTL: %d\n", ret);
10012f7ca802SSteve Glendinning 
1002769ea6d8SSteve Glendinning 	ret = smsc95xx_start_tx_path(dev);
10031e1d7412SJoe Perches 	check_warn_return(ret, "Failed to start TX path\n");
1004769ea6d8SSteve Glendinning 
1005ec32115dSMing Lei 	ret = smsc95xx_start_rx_path(dev, 0);
10061e1d7412SJoe Perches 	check_warn_return(ret, "Failed to start RX path\n");
10072f7ca802SSteve Glendinning 
1008a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net, "smsc95xx_reset, return 0\n");
10092f7ca802SSteve Glendinning 	return 0;
10102f7ca802SSteve Glendinning }
10112f7ca802SSteve Glendinning 
101263e77b39SStephen Hemminger static const struct net_device_ops smsc95xx_netdev_ops = {
101363e77b39SStephen Hemminger 	.ndo_open		= usbnet_open,
101463e77b39SStephen Hemminger 	.ndo_stop		= usbnet_stop,
101563e77b39SStephen Hemminger 	.ndo_start_xmit		= usbnet_start_xmit,
101663e77b39SStephen Hemminger 	.ndo_tx_timeout		= usbnet_tx_timeout,
101763e77b39SStephen Hemminger 	.ndo_change_mtu		= usbnet_change_mtu,
101863e77b39SStephen Hemminger 	.ndo_set_mac_address 	= eth_mac_addr,
101963e77b39SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
102063e77b39SStephen Hemminger 	.ndo_do_ioctl 		= smsc95xx_ioctl,
1021afc4b13dSJiri Pirko 	.ndo_set_rx_mode	= smsc95xx_set_multicast,
102278e47fe4SMichał Mirosław 	.ndo_set_features	= smsc95xx_set_features,
102363e77b39SStephen Hemminger };
102463e77b39SStephen Hemminger 
10252f7ca802SSteve Glendinning static int smsc95xx_bind(struct usbnet *dev, struct usb_interface *intf)
10262f7ca802SSteve Glendinning {
10272f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = NULL;
1028bbd9f9eeSSteve Glendinning 	u32 val;
10292f7ca802SSteve Glendinning 	int ret;
10302f7ca802SSteve Glendinning 
10312f7ca802SSteve Glendinning 	printk(KERN_INFO SMSC_CHIPNAME " v" SMSC_DRIVER_VERSION "\n");
10322f7ca802SSteve Glendinning 
10332f7ca802SSteve Glendinning 	ret = usbnet_get_endpoints(dev, intf);
1034769ea6d8SSteve Glendinning 	check_warn_return(ret, "usbnet_get_endpoints failed: %d\n", ret);
10352f7ca802SSteve Glendinning 
10362f7ca802SSteve Glendinning 	dev->data[0] = (unsigned long)kzalloc(sizeof(struct smsc95xx_priv),
10372f7ca802SSteve Glendinning 		GFP_KERNEL);
10382f7ca802SSteve Glendinning 
10392f7ca802SSteve Glendinning 	pdata = (struct smsc95xx_priv *)(dev->data[0]);
10402f7ca802SSteve Glendinning 	if (!pdata) {
104160b86755SJoe Perches 		netdev_warn(dev->net, "Unable to allocate struct smsc95xx_priv\n");
10422f7ca802SSteve Glendinning 		return -ENOMEM;
10432f7ca802SSteve Glendinning 	}
10442f7ca802SSteve Glendinning 
10452f7ca802SSteve Glendinning 	spin_lock_init(&pdata->mac_cr_lock);
10462f7ca802SSteve Glendinning 
104778e47fe4SMichał Mirosław 	if (DEFAULT_TX_CSUM_ENABLE)
104878e47fe4SMichał Mirosław 		dev->net->features |= NETIF_F_HW_CSUM;
104978e47fe4SMichał Mirosław 	if (DEFAULT_RX_CSUM_ENABLE)
105078e47fe4SMichał Mirosław 		dev->net->features |= NETIF_F_RXCSUM;
105178e47fe4SMichał Mirosław 
105278e47fe4SMichał Mirosław 	dev->net->hw_features = NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
10532f7ca802SSteve Glendinning 
1054f4e8ab7cSBernard Blackham 	smsc95xx_init_mac_address(dev);
1055f4e8ab7cSBernard Blackham 
10562f7ca802SSteve Glendinning 	/* Init all registers */
10572f7ca802SSteve Glendinning 	ret = smsc95xx_reset(dev);
10582f7ca802SSteve Glendinning 
1059bbd9f9eeSSteve Glendinning 	/* detect device revision as different features may be available */
1060bbd9f9eeSSteve Glendinning 	ret = smsc95xx_read_reg(dev, ID_REV, &val);
1061bbd9f9eeSSteve Glendinning 	check_warn_return(ret, "Failed to read ID_REV: %d\n", ret);
1062bbd9f9eeSSteve Glendinning 	val >>= 16;
10639ebca507SSteve Glendinning 
10649ebca507SSteve Glendinning 	if ((val == ID_REV_CHIP_ID_9500A_) || (val == ID_REV_CHIP_ID_9530_) ||
10659ebca507SSteve Glendinning 	    (val == ID_REV_CHIP_ID_89530_) || (val == ID_REV_CHIP_ID_9730_))
10669ebca507SSteve Glendinning 		pdata->features = (FEATURE_8_WAKEUP_FILTERS |
10679ebca507SSteve Glendinning 			FEATURE_PHY_NLP_CROSSOVER |
10689ebca507SSteve Glendinning 			FEATURE_AUTOSUSPEND);
10699ebca507SSteve Glendinning 	else if (val == ID_REV_CHIP_ID_9512_)
10709ebca507SSteve Glendinning 		pdata->features = FEATURE_8_WAKEUP_FILTERS;
1071bbd9f9eeSSteve Glendinning 
107263e77b39SStephen Hemminger 	dev->net->netdev_ops = &smsc95xx_netdev_ops;
10732f7ca802SSteve Glendinning 	dev->net->ethtool_ops = &smsc95xx_ethtool_ops;
10742f7ca802SSteve Glendinning 	dev->net->flags |= IFF_MULTICAST;
107578e47fe4SMichał Mirosław 	dev->net->hard_header_len += SMSC95XX_TX_OVERHEAD_CSUM;
10769bbf5660SStephane Fillod 	dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
10772f7ca802SSteve Glendinning 	return 0;
10782f7ca802SSteve Glendinning }
10792f7ca802SSteve Glendinning 
10802f7ca802SSteve Glendinning static void smsc95xx_unbind(struct usbnet *dev, struct usb_interface *intf)
10812f7ca802SSteve Glendinning {
10822f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
10832f7ca802SSteve Glendinning 	if (pdata) {
1084a475f603SJoe Perches 		netif_dbg(dev, ifdown, dev->net, "free pdata\n");
10852f7ca802SSteve Glendinning 		kfree(pdata);
10862f7ca802SSteve Glendinning 		pdata = NULL;
10872f7ca802SSteve Glendinning 		dev->data[0] = 0;
10882f7ca802SSteve Glendinning 	}
10892f7ca802SSteve Glendinning }
10902f7ca802SSteve Glendinning 
1091bbd9f9eeSSteve Glendinning static u16 smsc_crc(const u8 *buffer, size_t len, int filter)
1092bbd9f9eeSSteve Glendinning {
1093bbd9f9eeSSteve Glendinning 	return bitrev16(crc16(0xFFFF, buffer, len)) << ((filter % 2) * 16);
1094bbd9f9eeSSteve Glendinning }
1095bbd9f9eeSSteve Glendinning 
1096e5e3af83SSteve Glendinning static int smsc95xx_enable_phy_wakeup_interrupts(struct usbnet *dev, u16 mask)
1097e5e3af83SSteve Glendinning {
1098e5e3af83SSteve Glendinning 	struct mii_if_info *mii = &dev->mii;
1099e5e3af83SSteve Glendinning 	int ret;
1100e5e3af83SSteve Glendinning 
11011e1d7412SJoe Perches 	netdev_dbg(dev->net, "enabling PHY wakeup interrupts\n");
1102e5e3af83SSteve Glendinning 
1103e5e3af83SSteve Glendinning 	/* read to clear */
1104e5e3af83SSteve Glendinning 	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_INT_SRC);
11051e1d7412SJoe Perches 	check_warn_return(ret, "Error reading PHY_INT_SRC\n");
1106e5e3af83SSteve Glendinning 
1107e5e3af83SSteve Glendinning 	/* enable interrupt source */
1108e5e3af83SSteve Glendinning 	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_INT_MASK);
11091e1d7412SJoe Perches 	check_warn_return(ret, "Error reading PHY_INT_MASK\n");
1110e5e3af83SSteve Glendinning 
1111e5e3af83SSteve Glendinning 	ret |= mask;
1112e5e3af83SSteve Glendinning 
1113e5e3af83SSteve Glendinning 	smsc95xx_mdio_write_nopm(dev->net, mii->phy_id, PHY_INT_MASK, ret);
1114e5e3af83SSteve Glendinning 
1115e5e3af83SSteve Glendinning 	return 0;
1116e5e3af83SSteve Glendinning }
1117e5e3af83SSteve Glendinning 
1118e5e3af83SSteve Glendinning static int smsc95xx_link_ok_nopm(struct usbnet *dev)
1119e5e3af83SSteve Glendinning {
1120e5e3af83SSteve Glendinning 	struct mii_if_info *mii = &dev->mii;
1121e5e3af83SSteve Glendinning 	int ret;
1122e5e3af83SSteve Glendinning 
1123e5e3af83SSteve Glendinning 	/* first, a dummy read, needed to latch some MII phys */
1124e5e3af83SSteve Glendinning 	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, MII_BMSR);
11251e1d7412SJoe Perches 	check_warn_return(ret, "Error reading MII_BMSR\n");
1126e5e3af83SSteve Glendinning 
1127e5e3af83SSteve Glendinning 	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, MII_BMSR);
11281e1d7412SJoe Perches 	check_warn_return(ret, "Error reading MII_BMSR\n");
1129e5e3af83SSteve Glendinning 
1130e5e3af83SSteve Glendinning 	return !!(ret & BMSR_LSTATUS);
1131e5e3af83SSteve Glendinning }
1132e5e3af83SSteve Glendinning 
1133319b95b5SSteve Glendinning static int smsc95xx_enter_suspend0(struct usbnet *dev)
1134319b95b5SSteve Glendinning {
1135319b95b5SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1136319b95b5SSteve Glendinning 	u32 val;
1137319b95b5SSteve Glendinning 	int ret;
1138319b95b5SSteve Glendinning 
1139319b95b5SSteve Glendinning 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
11401e1d7412SJoe Perches 	check_warn_return(ret, "Error reading PM_CTRL\n");
1141319b95b5SSteve Glendinning 
1142319b95b5SSteve Glendinning 	val &= (~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_));
1143319b95b5SSteve Glendinning 	val |= PM_CTL_SUS_MODE_0;
1144319b95b5SSteve Glendinning 
1145319b95b5SSteve Glendinning 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
11461e1d7412SJoe Perches 	check_warn_return(ret, "Error writing PM_CTRL\n");
1147319b95b5SSteve Glendinning 
1148319b95b5SSteve Glendinning 	/* clear wol status */
1149319b95b5SSteve Glendinning 	val &= ~PM_CTL_WUPS_;
1150319b95b5SSteve Glendinning 	val |= PM_CTL_WUPS_WOL_;
1151319b95b5SSteve Glendinning 
1152319b95b5SSteve Glendinning 	/* enable energy detection */
1153319b95b5SSteve Glendinning 	if (pdata->wolopts & WAKE_PHY)
1154319b95b5SSteve Glendinning 		val |= PM_CTL_WUPS_ED_;
1155319b95b5SSteve Glendinning 
1156319b95b5SSteve Glendinning 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
11571e1d7412SJoe Perches 	check_warn_return(ret, "Error writing PM_CTRL\n");
1158319b95b5SSteve Glendinning 
1159319b95b5SSteve Glendinning 	/* read back PM_CTRL */
1160319b95b5SSteve Glendinning 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
11611e1d7412SJoe Perches 	check_warn_return(ret, "Error reading PM_CTRL\n");
1162319b95b5SSteve Glendinning 
1163319b95b5SSteve Glendinning 	smsc95xx_set_feature(dev, USB_DEVICE_REMOTE_WAKEUP);
1164319b95b5SSteve Glendinning 
1165319b95b5SSteve Glendinning 	return 0;
1166319b95b5SSteve Glendinning }
1167319b95b5SSteve Glendinning 
1168319b95b5SSteve Glendinning static int smsc95xx_enter_suspend1(struct usbnet *dev)
1169319b95b5SSteve Glendinning {
1170319b95b5SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1171319b95b5SSteve Glendinning 	struct mii_if_info *mii = &dev->mii;
1172319b95b5SSteve Glendinning 	u32 val;
1173319b95b5SSteve Glendinning 	int ret;
1174319b95b5SSteve Glendinning 
1175319b95b5SSteve Glendinning 	/* reconfigure link pulse detection timing for
1176319b95b5SSteve Glendinning 	 * compatibility with non-standard link partners
1177319b95b5SSteve Glendinning 	 */
1178319b95b5SSteve Glendinning 	if (pdata->features & FEATURE_PHY_NLP_CROSSOVER)
1179319b95b5SSteve Glendinning 		smsc95xx_mdio_write_nopm(dev->net, mii->phy_id,	PHY_EDPD_CONFIG,
1180319b95b5SSteve Glendinning 			PHY_EDPD_CONFIG_DEFAULT);
1181319b95b5SSteve Glendinning 
1182319b95b5SSteve Glendinning 	/* enable energy detect power-down mode */
1183319b95b5SSteve Glendinning 	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_MODE_CTRL_STS);
11841e1d7412SJoe Perches 	check_warn_return(ret, "Error reading PHY_MODE_CTRL_STS\n");
1185319b95b5SSteve Glendinning 
1186319b95b5SSteve Glendinning 	ret |= MODE_CTRL_STS_EDPWRDOWN_;
1187319b95b5SSteve Glendinning 
1188319b95b5SSteve Glendinning 	smsc95xx_mdio_write_nopm(dev->net, mii->phy_id, PHY_MODE_CTRL_STS, ret);
1189319b95b5SSteve Glendinning 
1190319b95b5SSteve Glendinning 	/* enter SUSPEND1 mode */
1191319b95b5SSteve Glendinning 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
11921e1d7412SJoe Perches 	check_warn_return(ret, "Error reading PM_CTRL\n");
1193319b95b5SSteve Glendinning 
1194319b95b5SSteve Glendinning 	val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1195319b95b5SSteve Glendinning 	val |= PM_CTL_SUS_MODE_1;
1196319b95b5SSteve Glendinning 
1197319b95b5SSteve Glendinning 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
11981e1d7412SJoe Perches 	check_warn_return(ret, "Error writing PM_CTRL\n");
1199319b95b5SSteve Glendinning 
1200319b95b5SSteve Glendinning 	/* clear wol status, enable energy detection */
1201319b95b5SSteve Glendinning 	val &= ~PM_CTL_WUPS_;
1202319b95b5SSteve Glendinning 	val |= (PM_CTL_WUPS_ED_ | PM_CTL_ED_EN_);
1203319b95b5SSteve Glendinning 
1204319b95b5SSteve Glendinning 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
12051e1d7412SJoe Perches 	check_warn_return(ret, "Error writing PM_CTRL\n");
1206319b95b5SSteve Glendinning 
1207319b95b5SSteve Glendinning 	smsc95xx_set_feature(dev, USB_DEVICE_REMOTE_WAKEUP);
1208319b95b5SSteve Glendinning 
1209319b95b5SSteve Glendinning 	return 0;
1210319b95b5SSteve Glendinning }
1211319b95b5SSteve Glendinning 
1212319b95b5SSteve Glendinning static int smsc95xx_enter_suspend2(struct usbnet *dev)
1213319b95b5SSteve Glendinning {
1214319b95b5SSteve Glendinning 	u32 val;
1215319b95b5SSteve Glendinning 	int ret;
1216319b95b5SSteve Glendinning 
1217319b95b5SSteve Glendinning 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
12181e1d7412SJoe Perches 	check_warn_return(ret, "Error reading PM_CTRL\n");
1219319b95b5SSteve Glendinning 
1220319b95b5SSteve Glendinning 	val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1221319b95b5SSteve Glendinning 	val |= PM_CTL_SUS_MODE_2;
1222319b95b5SSteve Glendinning 
1223319b95b5SSteve Glendinning 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
12241e1d7412SJoe Perches 	check_warn_return(ret, "Error writing PM_CTRL\n");
1225319b95b5SSteve Glendinning 
1226319b95b5SSteve Glendinning 	return 0;
1227319b95b5SSteve Glendinning }
1228319b95b5SSteve Glendinning 
1229b5a04475SSteve Glendinning static int smsc95xx_suspend(struct usb_interface *intf, pm_message_t message)
1230b5a04475SSteve Glendinning {
1231b5a04475SSteve Glendinning 	struct usbnet *dev = usb_get_intfdata(intf);
1232e0e474a8SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1233e5e3af83SSteve Glendinning 	u32 val, link_up;
1234b5a04475SSteve Glendinning 	int ret;
1235b5a04475SSteve Glendinning 
1236b5a04475SSteve Glendinning 	ret = usbnet_suspend(intf, message);
12371e1d7412SJoe Perches 	check_warn_return(ret, "usbnet_suspend error\n");
1238b5a04475SSteve Glendinning 
1239e5e3af83SSteve Glendinning 	/* determine if link is up using only _nopm functions */
1240e5e3af83SSteve Glendinning 	link_up = smsc95xx_link_ok_nopm(dev);
1241e5e3af83SSteve Glendinning 
1242e5e3af83SSteve Glendinning 	/* if no wol options set, or if link is down and we're not waking on
1243e5e3af83SSteve Glendinning 	 * PHY activity, enter lowest power SUSPEND2 mode
1244e5e3af83SSteve Glendinning 	 */
1245e5e3af83SSteve Glendinning 	if (!(pdata->wolopts & SUPPORTED_WAKE) ||
1246e5e3af83SSteve Glendinning 		!(link_up || (pdata->wolopts & WAKE_PHY))) {
12471e1d7412SJoe Perches 		netdev_info(dev->net, "entering SUSPEND2 mode\n");
1248b5a04475SSteve Glendinning 
1249e0e474a8SSteve Glendinning 		/* disable energy detect (link up) & wake up events */
1250ec32115dSMing Lei 		ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
12511e1d7412SJoe Perches 		check_warn_return(ret, "Error reading WUCSR\n");
1252e0e474a8SSteve Glendinning 
1253e0e474a8SSteve Glendinning 		val &= ~(WUCSR_MPEN_ | WUCSR_WAKE_EN_);
1254e0e474a8SSteve Glendinning 
1255ec32115dSMing Lei 		ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
12561e1d7412SJoe Perches 		check_warn_return(ret, "Error writing WUCSR\n");
1257e0e474a8SSteve Glendinning 
1258ec32115dSMing Lei 		ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
12591e1d7412SJoe Perches 		check_warn_return(ret, "Error reading PM_CTRL\n");
1260e0e474a8SSteve Glendinning 
1261e0e474a8SSteve Glendinning 		val &= ~(PM_CTL_ED_EN_ | PM_CTL_WOL_EN_);
1262e0e474a8SSteve Glendinning 
1263ec32115dSMing Lei 		ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
12641e1d7412SJoe Perches 		check_warn_return(ret, "Error writing PM_CTRL\n");
1265e0e474a8SSteve Glendinning 
1266319b95b5SSteve Glendinning 		return smsc95xx_enter_suspend2(dev);
1267b5a04475SSteve Glendinning 	}
1268b5a04475SSteve Glendinning 
1269e5e3af83SSteve Glendinning 	if (pdata->wolopts & WAKE_PHY) {
1270e5e3af83SSteve Glendinning 		ret = smsc95xx_enable_phy_wakeup_interrupts(dev,
1271e5e3af83SSteve Glendinning 			(PHY_INT_MASK_ANEG_COMP_ | PHY_INT_MASK_LINK_DOWN_));
12721e1d7412SJoe Perches 		check_warn_return(ret, "error enabling PHY wakeup ints\n");
1273e5e3af83SSteve Glendinning 
1274e5e3af83SSteve Glendinning 		/* if link is down then configure EDPD and enter SUSPEND1,
1275e5e3af83SSteve Glendinning 		 * otherwise enter SUSPEND0 below
1276e5e3af83SSteve Glendinning 		 */
1277e5e3af83SSteve Glendinning 		if (!link_up) {
12781e1d7412SJoe Perches 			netdev_info(dev->net, "entering SUSPEND1 mode\n");
1279319b95b5SSteve Glendinning 			return smsc95xx_enter_suspend1(dev);
1280e5e3af83SSteve Glendinning 		}
1281e5e3af83SSteve Glendinning 	}
1282e5e3af83SSteve Glendinning 
1283bbd9f9eeSSteve Glendinning 	if (pdata->wolopts & (WAKE_BCAST | WAKE_MCAST | WAKE_ARP | WAKE_UCAST)) {
1284*eed9a729SSteve Glendinning 		u32 *filter_mask = kzalloc(sizeof(u32) * 32, GFP_KERNEL);
128506a221beSMing Lei 		u32 command[2];
128606a221beSMing Lei 		u32 offset[2];
128706a221beSMing Lei 		u32 crc[4];
12889ebca507SSteve Glendinning 		int wuff_filter_count =
12899ebca507SSteve Glendinning 			(pdata->features & FEATURE_8_WAKEUP_FILTERS) ?
12909ebca507SSteve Glendinning 			LAN9500A_WUFF_NUM : LAN9500_WUFF_NUM;
1291bbd9f9eeSSteve Glendinning 		int i, filter = 0;
1292bbd9f9eeSSteve Glendinning 
1293*eed9a729SSteve Glendinning 		if (!filter_mask) {
1294*eed9a729SSteve Glendinning 			netdev_warn(dev->net, "Unable to allocate filter_mask\n");
1295*eed9a729SSteve Glendinning 			return -ENOMEM;
1296*eed9a729SSteve Glendinning 		}
1297*eed9a729SSteve Glendinning 
129806a221beSMing Lei 		memset(command, 0, sizeof(command));
129906a221beSMing Lei 		memset(offset, 0, sizeof(offset));
130006a221beSMing Lei 		memset(crc, 0, sizeof(crc));
130106a221beSMing Lei 
1302bbd9f9eeSSteve Glendinning 		if (pdata->wolopts & WAKE_BCAST) {
1303bbd9f9eeSSteve Glendinning 			const u8 bcast[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
13041e1d7412SJoe Perches 			netdev_info(dev->net, "enabling broadcast detection\n");
1305bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4] = 0x003F;
1306bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 1] = 0x00;
1307bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 2] = 0x00;
1308bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 3] = 0x00;
1309bbd9f9eeSSteve Glendinning 			command[filter/4] |= 0x05UL << ((filter % 4) * 8);
1310bbd9f9eeSSteve Glendinning 			offset[filter/4] |= 0x00 << ((filter % 4) * 8);
1311bbd9f9eeSSteve Glendinning 			crc[filter/2] |= smsc_crc(bcast, 6, filter);
1312bbd9f9eeSSteve Glendinning 			filter++;
1313bbd9f9eeSSteve Glendinning 		}
1314bbd9f9eeSSteve Glendinning 
1315bbd9f9eeSSteve Glendinning 		if (pdata->wolopts & WAKE_MCAST) {
1316bbd9f9eeSSteve Glendinning 			const u8 mcast[] = {0x01, 0x00, 0x5E};
13171e1d7412SJoe Perches 			netdev_info(dev->net, "enabling multicast detection\n");
1318bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4] = 0x0007;
1319bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 1] = 0x00;
1320bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 2] = 0x00;
1321bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 3] = 0x00;
1322bbd9f9eeSSteve Glendinning 			command[filter/4] |= 0x09UL << ((filter % 4) * 8);
1323bbd9f9eeSSteve Glendinning 			offset[filter/4] |= 0x00  << ((filter % 4) * 8);
1324bbd9f9eeSSteve Glendinning 			crc[filter/2] |= smsc_crc(mcast, 3, filter);
1325bbd9f9eeSSteve Glendinning 			filter++;
1326bbd9f9eeSSteve Glendinning 		}
1327bbd9f9eeSSteve Glendinning 
1328bbd9f9eeSSteve Glendinning 		if (pdata->wolopts & WAKE_ARP) {
1329bbd9f9eeSSteve Glendinning 			const u8 arp[] = {0x08, 0x06};
13301e1d7412SJoe Perches 			netdev_info(dev->net, "enabling ARP detection\n");
1331bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4] = 0x0003;
1332bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 1] = 0x00;
1333bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 2] = 0x00;
1334bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 3] = 0x00;
1335bbd9f9eeSSteve Glendinning 			command[filter/4] |= 0x05UL << ((filter % 4) * 8);
1336bbd9f9eeSSteve Glendinning 			offset[filter/4] |= 0x0C << ((filter % 4) * 8);
1337bbd9f9eeSSteve Glendinning 			crc[filter/2] |= smsc_crc(arp, 2, filter);
1338bbd9f9eeSSteve Glendinning 			filter++;
1339bbd9f9eeSSteve Glendinning 		}
1340bbd9f9eeSSteve Glendinning 
1341bbd9f9eeSSteve Glendinning 		if (pdata->wolopts & WAKE_UCAST) {
13421e1d7412SJoe Perches 			netdev_info(dev->net, "enabling unicast detection\n");
1343bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4] = 0x003F;
1344bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 1] = 0x00;
1345bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 2] = 0x00;
1346bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 3] = 0x00;
1347bbd9f9eeSSteve Glendinning 			command[filter/4] |= 0x01UL << ((filter % 4) * 8);
1348bbd9f9eeSSteve Glendinning 			offset[filter/4] |= 0x00 << ((filter % 4) * 8);
1349bbd9f9eeSSteve Glendinning 			crc[filter/2] |= smsc_crc(dev->net->dev_addr, ETH_ALEN, filter);
1350bbd9f9eeSSteve Glendinning 			filter++;
1351bbd9f9eeSSteve Glendinning 		}
1352bbd9f9eeSSteve Glendinning 
13539ebca507SSteve Glendinning 		for (i = 0; i < (wuff_filter_count * 4); i++) {
1354ec32115dSMing Lei 			ret = smsc95xx_write_reg_nopm(dev, WUFF, filter_mask[i]);
135506a221beSMing Lei 			if (ret < 0)
135606a221beSMing Lei 				kfree(filter_mask);
13571e1d7412SJoe Perches 			check_warn_return(ret, "Error writing WUFF\n");
1358bbd9f9eeSSteve Glendinning 		}
135906a221beSMing Lei 		kfree(filter_mask);
1360bbd9f9eeSSteve Glendinning 
13619ebca507SSteve Glendinning 		for (i = 0; i < (wuff_filter_count / 4); i++) {
1362ec32115dSMing Lei 			ret = smsc95xx_write_reg_nopm(dev, WUFF, command[i]);
13631e1d7412SJoe Perches 			check_warn_return(ret, "Error writing WUFF\n");
1364bbd9f9eeSSteve Glendinning 		}
1365bbd9f9eeSSteve Glendinning 
13669ebca507SSteve Glendinning 		for (i = 0; i < (wuff_filter_count / 4); i++) {
1367ec32115dSMing Lei 			ret = smsc95xx_write_reg_nopm(dev, WUFF, offset[i]);
13681e1d7412SJoe Perches 			check_warn_return(ret, "Error writing WUFF\n");
1369bbd9f9eeSSteve Glendinning 		}
1370bbd9f9eeSSteve Glendinning 
13719ebca507SSteve Glendinning 		for (i = 0; i < (wuff_filter_count / 2); i++) {
1372ec32115dSMing Lei 			ret = smsc95xx_write_reg_nopm(dev, WUFF, crc[i]);
13731e1d7412SJoe Perches 			check_warn_return(ret, "Error writing WUFF\n");
1374bbd9f9eeSSteve Glendinning 		}
1375bbd9f9eeSSteve Glendinning 
1376bbd9f9eeSSteve Glendinning 		/* clear any pending pattern match packet status */
1377ec32115dSMing Lei 		ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
13781e1d7412SJoe Perches 		check_warn_return(ret, "Error reading WUCSR\n");
1379bbd9f9eeSSteve Glendinning 
1380bbd9f9eeSSteve Glendinning 		val |= WUCSR_WUFR_;
1381bbd9f9eeSSteve Glendinning 
1382ec32115dSMing Lei 		ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
13831e1d7412SJoe Perches 		check_warn_return(ret, "Error writing WUCSR\n");
1384bbd9f9eeSSteve Glendinning 	}
1385bbd9f9eeSSteve Glendinning 
1386e0e474a8SSteve Glendinning 	if (pdata->wolopts & WAKE_MAGIC) {
1387e0e474a8SSteve Glendinning 		/* clear any pending magic packet status */
1388ec32115dSMing Lei 		ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
13891e1d7412SJoe Perches 		check_warn_return(ret, "Error reading WUCSR\n");
1390e0e474a8SSteve Glendinning 
1391e0e474a8SSteve Glendinning 		val |= WUCSR_MPR_;
1392e0e474a8SSteve Glendinning 
1393ec32115dSMing Lei 		ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
13941e1d7412SJoe Perches 		check_warn_return(ret, "Error writing WUCSR\n");
1395e0e474a8SSteve Glendinning 	}
1396e0e474a8SSteve Glendinning 
1397bbd9f9eeSSteve Glendinning 	/* enable/disable wakeup sources */
1398ec32115dSMing Lei 	ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
13991e1d7412SJoe Perches 	check_warn_return(ret, "Error reading WUCSR\n");
1400e0e474a8SSteve Glendinning 
1401bbd9f9eeSSteve Glendinning 	if (pdata->wolopts & (WAKE_BCAST | WAKE_MCAST | WAKE_ARP | WAKE_UCAST)) {
14021e1d7412SJoe Perches 		netdev_info(dev->net, "enabling pattern match wakeup\n");
1403bbd9f9eeSSteve Glendinning 		val |= WUCSR_WAKE_EN_;
1404bbd9f9eeSSteve Glendinning 	} else {
14051e1d7412SJoe Perches 		netdev_info(dev->net, "disabling pattern match wakeup\n");
1406bbd9f9eeSSteve Glendinning 		val &= ~WUCSR_WAKE_EN_;
1407bbd9f9eeSSteve Glendinning 	}
1408bbd9f9eeSSteve Glendinning 
1409e0e474a8SSteve Glendinning 	if (pdata->wolopts & WAKE_MAGIC) {
14101e1d7412SJoe Perches 		netdev_info(dev->net, "enabling magic packet wakeup\n");
1411e0e474a8SSteve Glendinning 		val |= WUCSR_MPEN_;
1412e0e474a8SSteve Glendinning 	} else {
14131e1d7412SJoe Perches 		netdev_info(dev->net, "disabling magic packet wakeup\n");
1414e0e474a8SSteve Glendinning 		val &= ~WUCSR_MPEN_;
1415e0e474a8SSteve Glendinning 	}
1416e0e474a8SSteve Glendinning 
1417ec32115dSMing Lei 	ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
14181e1d7412SJoe Perches 	check_warn_return(ret, "Error writing WUCSR\n");
1419e0e474a8SSteve Glendinning 
1420e0e474a8SSteve Glendinning 	/* enable wol wakeup source */
1421ec32115dSMing Lei 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
14221e1d7412SJoe Perches 	check_warn_return(ret, "Error reading PM_CTRL\n");
1423e0e474a8SSteve Glendinning 
1424e0e474a8SSteve Glendinning 	val |= PM_CTL_WOL_EN_;
1425e0e474a8SSteve Glendinning 
1426e5e3af83SSteve Glendinning 	/* phy energy detect wakeup source */
1427e5e3af83SSteve Glendinning 	if (pdata->wolopts & WAKE_PHY)
1428e5e3af83SSteve Glendinning 		val |= PM_CTL_ED_EN_;
1429e5e3af83SSteve Glendinning 
1430ec32115dSMing Lei 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
14311e1d7412SJoe Perches 	check_warn_return(ret, "Error writing PM_CTRL\n");
1432e0e474a8SSteve Glendinning 
1433bbd9f9eeSSteve Glendinning 	/* enable receiver to enable frame reception */
1434ec32115dSMing Lei 	smsc95xx_start_rx_path(dev, 1);
1435e0e474a8SSteve Glendinning 
1436e0e474a8SSteve Glendinning 	/* some wol options are enabled, so enter SUSPEND0 */
14371e1d7412SJoe Perches 	netdev_info(dev->net, "entering SUSPEND0 mode\n");
1438319b95b5SSteve Glendinning 	return smsc95xx_enter_suspend0(dev);
1439e0e474a8SSteve Glendinning }
1440e0e474a8SSteve Glendinning 
1441e0e474a8SSteve Glendinning static int smsc95xx_resume(struct usb_interface *intf)
1442e0e474a8SSteve Glendinning {
1443e0e474a8SSteve Glendinning 	struct usbnet *dev = usb_get_intfdata(intf);
1444e0e474a8SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1445e0e474a8SSteve Glendinning 	int ret;
1446e0e474a8SSteve Glendinning 	u32 val;
1447e0e474a8SSteve Glendinning 
1448e0e474a8SSteve Glendinning 	BUG_ON(!dev);
1449e0e474a8SSteve Glendinning 
1450bbd9f9eeSSteve Glendinning 	if (pdata->wolopts) {
1451e0e474a8SSteve Glendinning 		smsc95xx_clear_feature(dev, USB_DEVICE_REMOTE_WAKEUP);
1452e0e474a8SSteve Glendinning 
1453bbd9f9eeSSteve Glendinning 		/* clear wake-up sources */
1454ec32115dSMing Lei 		ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
14551e1d7412SJoe Perches 		check_warn_return(ret, "Error reading WUCSR\n");
1456e0e474a8SSteve Glendinning 
1457bbd9f9eeSSteve Glendinning 		val &= ~(WUCSR_WAKE_EN_ | WUCSR_MPEN_);
1458e0e474a8SSteve Glendinning 
1459ec32115dSMing Lei 		ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
14601e1d7412SJoe Perches 		check_warn_return(ret, "Error writing WUCSR\n");
1461e0e474a8SSteve Glendinning 
1462e0e474a8SSteve Glendinning 		/* clear wake-up status */
1463ec32115dSMing Lei 		ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
14641e1d7412SJoe Perches 		check_warn_return(ret, "Error reading PM_CTRL\n");
1465e0e474a8SSteve Glendinning 
1466e0e474a8SSteve Glendinning 		val &= ~PM_CTL_WOL_EN_;
1467e0e474a8SSteve Glendinning 		val |= PM_CTL_WUPS_;
1468e0e474a8SSteve Glendinning 
1469ec32115dSMing Lei 		ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
14701e1d7412SJoe Perches 		check_warn_return(ret, "Error writing PM_CTRL\n");
1471e0e474a8SSteve Glendinning 	}
1472e0e474a8SSteve Glendinning 
1473af3d7c1eSSteve Glendinning 	ret = usbnet_resume(intf);
14741e1d7412SJoe Perches 	check_warn_return(ret, "usbnet_resume error\n");
1475e0e474a8SSteve Glendinning 
1476e0e474a8SSteve Glendinning 	return 0;
1477e0e474a8SSteve Glendinning }
1478e0e474a8SSteve Glendinning 
14792f7ca802SSteve Glendinning static void smsc95xx_rx_csum_offload(struct sk_buff *skb)
14802f7ca802SSteve Glendinning {
14812f7ca802SSteve Glendinning 	skb->csum = *(u16 *)(skb_tail_pointer(skb) - 2);
14822f7ca802SSteve Glendinning 	skb->ip_summed = CHECKSUM_COMPLETE;
14832f7ca802SSteve Glendinning 	skb_trim(skb, skb->len - 2);
14842f7ca802SSteve Glendinning }
14852f7ca802SSteve Glendinning 
14862f7ca802SSteve Glendinning static int smsc95xx_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
14872f7ca802SSteve Glendinning {
14882f7ca802SSteve Glendinning 	while (skb->len > 0) {
14892f7ca802SSteve Glendinning 		u32 header, align_count;
14902f7ca802SSteve Glendinning 		struct sk_buff *ax_skb;
14912f7ca802SSteve Glendinning 		unsigned char *packet;
14922f7ca802SSteve Glendinning 		u16 size;
14932f7ca802SSteve Glendinning 
14942f7ca802SSteve Glendinning 		memcpy(&header, skb->data, sizeof(header));
14952f7ca802SSteve Glendinning 		le32_to_cpus(&header);
14962f7ca802SSteve Glendinning 		skb_pull(skb, 4 + NET_IP_ALIGN);
14972f7ca802SSteve Glendinning 		packet = skb->data;
14982f7ca802SSteve Glendinning 
14992f7ca802SSteve Glendinning 		/* get the packet length */
15002f7ca802SSteve Glendinning 		size = (u16)((header & RX_STS_FL_) >> 16);
15012f7ca802SSteve Glendinning 		align_count = (4 - ((size + NET_IP_ALIGN) % 4)) % 4;
15022f7ca802SSteve Glendinning 
15032f7ca802SSteve Glendinning 		if (unlikely(header & RX_STS_ES_)) {
1504a475f603SJoe Perches 			netif_dbg(dev, rx_err, dev->net,
1505a475f603SJoe Perches 				  "Error header=0x%08x\n", header);
150680667ac1SHerbert Xu 			dev->net->stats.rx_errors++;
150780667ac1SHerbert Xu 			dev->net->stats.rx_dropped++;
15082f7ca802SSteve Glendinning 
15092f7ca802SSteve Glendinning 			if (header & RX_STS_CRC_) {
151080667ac1SHerbert Xu 				dev->net->stats.rx_crc_errors++;
15112f7ca802SSteve Glendinning 			} else {
15122f7ca802SSteve Glendinning 				if (header & (RX_STS_TL_ | RX_STS_RF_))
151380667ac1SHerbert Xu 					dev->net->stats.rx_frame_errors++;
15142f7ca802SSteve Glendinning 
15152f7ca802SSteve Glendinning 				if ((header & RX_STS_LE_) &&
15162f7ca802SSteve Glendinning 					(!(header & RX_STS_FT_)))
151780667ac1SHerbert Xu 					dev->net->stats.rx_length_errors++;
15182f7ca802SSteve Glendinning 			}
15192f7ca802SSteve Glendinning 		} else {
15202f7ca802SSteve Glendinning 			/* ETH_FRAME_LEN + 4(CRC) + 2(COE) + 4(Vlan) */
15212f7ca802SSteve Glendinning 			if (unlikely(size > (ETH_FRAME_LEN + 12))) {
1522a475f603SJoe Perches 				netif_dbg(dev, rx_err, dev->net,
1523a475f603SJoe Perches 					  "size err header=0x%08x\n", header);
15242f7ca802SSteve Glendinning 				return 0;
15252f7ca802SSteve Glendinning 			}
15262f7ca802SSteve Glendinning 
15272f7ca802SSteve Glendinning 			/* last frame in this batch */
15282f7ca802SSteve Glendinning 			if (skb->len == size) {
152978e47fe4SMichał Mirosław 				if (dev->net->features & NETIF_F_RXCSUM)
15302f7ca802SSteve Glendinning 					smsc95xx_rx_csum_offload(skb);
1531df18accaSPeter Korsgaard 				skb_trim(skb, skb->len - 4); /* remove fcs */
15322f7ca802SSteve Glendinning 				skb->truesize = size + sizeof(struct sk_buff);
15332f7ca802SSteve Glendinning 
15342f7ca802SSteve Glendinning 				return 1;
15352f7ca802SSteve Glendinning 			}
15362f7ca802SSteve Glendinning 
15372f7ca802SSteve Glendinning 			ax_skb = skb_clone(skb, GFP_ATOMIC);
15382f7ca802SSteve Glendinning 			if (unlikely(!ax_skb)) {
153960b86755SJoe Perches 				netdev_warn(dev->net, "Error allocating skb\n");
15402f7ca802SSteve Glendinning 				return 0;
15412f7ca802SSteve Glendinning 			}
15422f7ca802SSteve Glendinning 
15432f7ca802SSteve Glendinning 			ax_skb->len = size;
15442f7ca802SSteve Glendinning 			ax_skb->data = packet;
15452f7ca802SSteve Glendinning 			skb_set_tail_pointer(ax_skb, size);
15462f7ca802SSteve Glendinning 
154778e47fe4SMichał Mirosław 			if (dev->net->features & NETIF_F_RXCSUM)
15482f7ca802SSteve Glendinning 				smsc95xx_rx_csum_offload(ax_skb);
1549df18accaSPeter Korsgaard 			skb_trim(ax_skb, ax_skb->len - 4); /* remove fcs */
15502f7ca802SSteve Glendinning 			ax_skb->truesize = size + sizeof(struct sk_buff);
15512f7ca802SSteve Glendinning 
15522f7ca802SSteve Glendinning 			usbnet_skb_return(dev, ax_skb);
15532f7ca802SSteve Glendinning 		}
15542f7ca802SSteve Glendinning 
15552f7ca802SSteve Glendinning 		skb_pull(skb, size);
15562f7ca802SSteve Glendinning 
15572f7ca802SSteve Glendinning 		/* padding bytes before the next frame starts */
15582f7ca802SSteve Glendinning 		if (skb->len)
15592f7ca802SSteve Glendinning 			skb_pull(skb, align_count);
15602f7ca802SSteve Glendinning 	}
15612f7ca802SSteve Glendinning 
15622f7ca802SSteve Glendinning 	if (unlikely(skb->len < 0)) {
156360b86755SJoe Perches 		netdev_warn(dev->net, "invalid rx length<0 %d\n", skb->len);
15642f7ca802SSteve Glendinning 		return 0;
15652f7ca802SSteve Glendinning 	}
15662f7ca802SSteve Glendinning 
15672f7ca802SSteve Glendinning 	return 1;
15682f7ca802SSteve Glendinning }
15692f7ca802SSteve Glendinning 
1570f7b29271SSteve Glendinning static u32 smsc95xx_calc_csum_preamble(struct sk_buff *skb)
1571f7b29271SSteve Glendinning {
157255508d60SMichał Mirosław 	u16 low_16 = (u16)skb_checksum_start_offset(skb);
157355508d60SMichał Mirosław 	u16 high_16 = low_16 + skb->csum_offset;
1574f7b29271SSteve Glendinning 	return (high_16 << 16) | low_16;
1575f7b29271SSteve Glendinning }
1576f7b29271SSteve Glendinning 
15772f7ca802SSteve Glendinning static struct sk_buff *smsc95xx_tx_fixup(struct usbnet *dev,
15782f7ca802SSteve Glendinning 					 struct sk_buff *skb, gfp_t flags)
15792f7ca802SSteve Glendinning {
158078e47fe4SMichał Mirosław 	bool csum = skb->ip_summed == CHECKSUM_PARTIAL;
1581f7b29271SSteve Glendinning 	int overhead = csum ? SMSC95XX_TX_OVERHEAD_CSUM : SMSC95XX_TX_OVERHEAD;
15822f7ca802SSteve Glendinning 	u32 tx_cmd_a, tx_cmd_b;
15832f7ca802SSteve Glendinning 
1584f7b29271SSteve Glendinning 	/* We do not advertise SG, so skbs should be already linearized */
1585f7b29271SSteve Glendinning 	BUG_ON(skb_shinfo(skb)->nr_frags);
1586f7b29271SSteve Glendinning 
1587f7b29271SSteve Glendinning 	if (skb_headroom(skb) < overhead) {
15882f7ca802SSteve Glendinning 		struct sk_buff *skb2 = skb_copy_expand(skb,
1589f7b29271SSteve Glendinning 			overhead, 0, flags);
15902f7ca802SSteve Glendinning 		dev_kfree_skb_any(skb);
15912f7ca802SSteve Glendinning 		skb = skb2;
15922f7ca802SSteve Glendinning 		if (!skb)
15932f7ca802SSteve Glendinning 			return NULL;
15942f7ca802SSteve Glendinning 	}
15952f7ca802SSteve Glendinning 
1596f7b29271SSteve Glendinning 	if (csum) {
159711bc3088SSteve Glendinning 		if (skb->len <= 45) {
159811bc3088SSteve Glendinning 			/* workaround - hardware tx checksum does not work
159911bc3088SSteve Glendinning 			 * properly with extremely small packets */
160055508d60SMichał Mirosław 			long csstart = skb_checksum_start_offset(skb);
160111bc3088SSteve Glendinning 			__wsum calc = csum_partial(skb->data + csstart,
160211bc3088SSteve Glendinning 				skb->len - csstart, 0);
160311bc3088SSteve Glendinning 			*((__sum16 *)(skb->data + csstart
160411bc3088SSteve Glendinning 				+ skb->csum_offset)) = csum_fold(calc);
160511bc3088SSteve Glendinning 
160611bc3088SSteve Glendinning 			csum = false;
160711bc3088SSteve Glendinning 		} else {
1608f7b29271SSteve Glendinning 			u32 csum_preamble = smsc95xx_calc_csum_preamble(skb);
1609f7b29271SSteve Glendinning 			skb_push(skb, 4);
161000acda68SSteve Glendinning 			cpu_to_le32s(&csum_preamble);
1611f7b29271SSteve Glendinning 			memcpy(skb->data, &csum_preamble, 4);
1612f7b29271SSteve Glendinning 		}
161311bc3088SSteve Glendinning 	}
1614f7b29271SSteve Glendinning 
16152f7ca802SSteve Glendinning 	skb_push(skb, 4);
16162f7ca802SSteve Glendinning 	tx_cmd_b = (u32)(skb->len - 4);
1617f7b29271SSteve Glendinning 	if (csum)
1618f7b29271SSteve Glendinning 		tx_cmd_b |= TX_CMD_B_CSUM_ENABLE;
16192f7ca802SSteve Glendinning 	cpu_to_le32s(&tx_cmd_b);
16202f7ca802SSteve Glendinning 	memcpy(skb->data, &tx_cmd_b, 4);
16212f7ca802SSteve Glendinning 
16222f7ca802SSteve Glendinning 	skb_push(skb, 4);
16232f7ca802SSteve Glendinning 	tx_cmd_a = (u32)(skb->len - 8) | TX_CMD_A_FIRST_SEG_ |
16242f7ca802SSteve Glendinning 		TX_CMD_A_LAST_SEG_;
16252f7ca802SSteve Glendinning 	cpu_to_le32s(&tx_cmd_a);
16262f7ca802SSteve Glendinning 	memcpy(skb->data, &tx_cmd_a, 4);
16272f7ca802SSteve Glendinning 
16282f7ca802SSteve Glendinning 	return skb;
16292f7ca802SSteve Glendinning }
16302f7ca802SSteve Glendinning 
16312f7ca802SSteve Glendinning static const struct driver_info smsc95xx_info = {
16322f7ca802SSteve Glendinning 	.description	= "smsc95xx USB 2.0 Ethernet",
16332f7ca802SSteve Glendinning 	.bind		= smsc95xx_bind,
16342f7ca802SSteve Glendinning 	.unbind		= smsc95xx_unbind,
16352f7ca802SSteve Glendinning 	.link_reset	= smsc95xx_link_reset,
16362f7ca802SSteve Glendinning 	.reset		= smsc95xx_reset,
16372f7ca802SSteve Glendinning 	.rx_fixup	= smsc95xx_rx_fixup,
16382f7ca802SSteve Glendinning 	.tx_fixup	= smsc95xx_tx_fixup,
16392f7ca802SSteve Glendinning 	.status		= smsc95xx_status,
164007d69d42SPaolo Pisati 	.flags		= FLAG_ETHER | FLAG_SEND_ZLP | FLAG_LINK_INTR,
16412f7ca802SSteve Glendinning };
16422f7ca802SSteve Glendinning 
16432f7ca802SSteve Glendinning static const struct usb_device_id products[] = {
16442f7ca802SSteve Glendinning 	{
16452f7ca802SSteve Glendinning 		/* SMSC9500 USB Ethernet Device */
16462f7ca802SSteve Glendinning 		USB_DEVICE(0x0424, 0x9500),
16472f7ca802SSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
16482f7ca802SSteve Glendinning 	},
1649726474b8SSteve Glendinning 	{
16506f41d12bSSteve Glendinning 		/* SMSC9505 USB Ethernet Device */
16516f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9505),
16526f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
16536f41d12bSSteve Glendinning 	},
16546f41d12bSSteve Glendinning 	{
16556f41d12bSSteve Glendinning 		/* SMSC9500A USB Ethernet Device */
16566f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9E00),
16576f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
16586f41d12bSSteve Glendinning 	},
16596f41d12bSSteve Glendinning 	{
16606f41d12bSSteve Glendinning 		/* SMSC9505A USB Ethernet Device */
16616f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9E01),
16626f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
16636f41d12bSSteve Glendinning 	},
16646f41d12bSSteve Glendinning 	{
1665726474b8SSteve Glendinning 		/* SMSC9512/9514 USB Hub & Ethernet Device */
1666726474b8SSteve Glendinning 		USB_DEVICE(0x0424, 0xec00),
1667726474b8SSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
1668726474b8SSteve Glendinning 	},
16696f41d12bSSteve Glendinning 	{
16706f41d12bSSteve Glendinning 		/* SMSC9500 USB Ethernet Device (SAL10) */
16716f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9900),
16726f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
16736f41d12bSSteve Glendinning 	},
16746f41d12bSSteve Glendinning 	{
16756f41d12bSSteve Glendinning 		/* SMSC9505 USB Ethernet Device (SAL10) */
16766f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9901),
16776f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
16786f41d12bSSteve Glendinning 	},
16796f41d12bSSteve Glendinning 	{
16806f41d12bSSteve Glendinning 		/* SMSC9500A USB Ethernet Device (SAL10) */
16816f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9902),
16826f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
16836f41d12bSSteve Glendinning 	},
16846f41d12bSSteve Glendinning 	{
16856f41d12bSSteve Glendinning 		/* SMSC9505A USB Ethernet Device (SAL10) */
16866f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9903),
16876f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
16886f41d12bSSteve Glendinning 	},
16896f41d12bSSteve Glendinning 	{
16906f41d12bSSteve Glendinning 		/* SMSC9512/9514 USB Hub & Ethernet Device (SAL10) */
16916f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9904),
16926f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
16936f41d12bSSteve Glendinning 	},
16946f41d12bSSteve Glendinning 	{
16956f41d12bSSteve Glendinning 		/* SMSC9500A USB Ethernet Device (HAL) */
16966f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9905),
16976f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
16986f41d12bSSteve Glendinning 	},
16996f41d12bSSteve Glendinning 	{
17006f41d12bSSteve Glendinning 		/* SMSC9505A USB Ethernet Device (HAL) */
17016f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9906),
17026f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
17036f41d12bSSteve Glendinning 	},
17046f41d12bSSteve Glendinning 	{
17056f41d12bSSteve Glendinning 		/* SMSC9500 USB Ethernet Device (Alternate ID) */
17066f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9907),
17076f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
17086f41d12bSSteve Glendinning 	},
17096f41d12bSSteve Glendinning 	{
17106f41d12bSSteve Glendinning 		/* SMSC9500A USB Ethernet Device (Alternate ID) */
17116f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9908),
17126f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
17136f41d12bSSteve Glendinning 	},
17146f41d12bSSteve Glendinning 	{
17156f41d12bSSteve Glendinning 		/* SMSC9512/9514 USB Hub & Ethernet Device (Alternate ID) */
17166f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9909),
17176f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
17186f41d12bSSteve Glendinning 	},
171988edaa41SSteve Glendinning 	{
172088edaa41SSteve Glendinning 		/* SMSC LAN9530 USB Ethernet Device */
172188edaa41SSteve Glendinning 		USB_DEVICE(0x0424, 0x9530),
172288edaa41SSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
172388edaa41SSteve Glendinning 	},
172488edaa41SSteve Glendinning 	{
172588edaa41SSteve Glendinning 		/* SMSC LAN9730 USB Ethernet Device */
172688edaa41SSteve Glendinning 		USB_DEVICE(0x0424, 0x9730),
172788edaa41SSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
172888edaa41SSteve Glendinning 	},
172988edaa41SSteve Glendinning 	{
173088edaa41SSteve Glendinning 		/* SMSC LAN89530 USB Ethernet Device */
173188edaa41SSteve Glendinning 		USB_DEVICE(0x0424, 0x9E08),
173288edaa41SSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
173388edaa41SSteve Glendinning 	},
17342f7ca802SSteve Glendinning 	{ },		/* END */
17352f7ca802SSteve Glendinning };
17362f7ca802SSteve Glendinning MODULE_DEVICE_TABLE(usb, products);
17372f7ca802SSteve Glendinning 
17382f7ca802SSteve Glendinning static struct usb_driver smsc95xx_driver = {
17392f7ca802SSteve Glendinning 	.name		= "smsc95xx",
17402f7ca802SSteve Glendinning 	.id_table	= products,
17412f7ca802SSteve Glendinning 	.probe		= usbnet_probe,
1742b5a04475SSteve Glendinning 	.suspend	= smsc95xx_suspend,
1743e0e474a8SSteve Glendinning 	.resume		= smsc95xx_resume,
1744e0e474a8SSteve Glendinning 	.reset_resume	= smsc95xx_resume,
17452f7ca802SSteve Glendinning 	.disconnect	= usbnet_disconnect,
1746e1f12eb6SSarah Sharp 	.disable_hub_initiated_lpm = 1,
17472f7ca802SSteve Glendinning };
17482f7ca802SSteve Glendinning 
1749d632eb1bSGreg Kroah-Hartman module_usb_driver(smsc95xx_driver);
17502f7ca802SSteve Glendinning 
17512f7ca802SSteve Glendinning MODULE_AUTHOR("Nancy Lin");
175290b24cfbSSteve Glendinning MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>");
17532f7ca802SSteve Glendinning MODULE_DESCRIPTION("SMSC95XX USB 2.0 Ethernet Devices");
17542f7ca802SSteve Glendinning MODULE_LICENSE("GPL");
1755