xref: /openbmc/linux/drivers/net/usb/smsc95xx.c (revision 1ccea77e2a2687cae171b7987eb44730ec8c6d5f)
1*1ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
22f7ca802SSteve Glendinning  /***************************************************************************
32f7ca802SSteve Glendinning  *
42f7ca802SSteve Glendinning  * Copyright (C) 2007-2008 SMSC
52f7ca802SSteve Glendinning  *
62f7ca802SSteve Glendinning  *****************************************************************************/
72f7ca802SSteve Glendinning 
82f7ca802SSteve Glendinning #include <linux/module.h>
92f7ca802SSteve Glendinning #include <linux/kmod.h>
102f7ca802SSteve Glendinning #include <linux/netdevice.h>
112f7ca802SSteve Glendinning #include <linux/etherdevice.h>
122f7ca802SSteve Glendinning #include <linux/ethtool.h>
132f7ca802SSteve Glendinning #include <linux/mii.h>
142f7ca802SSteve Glendinning #include <linux/usb.h>
15bbd9f9eeSSteve Glendinning #include <linux/bitrev.h>
16bbd9f9eeSSteve Glendinning #include <linux/crc16.h>
172f7ca802SSteve Glendinning #include <linux/crc32.h>
182f7ca802SSteve Glendinning #include <linux/usb/usbnet.h>
195a0e3ad6STejun Heo #include <linux/slab.h>
20c489565bSArnd Bergmann #include <linux/of_net.h>
212f7ca802SSteve Glendinning #include "smsc95xx.h"
222f7ca802SSteve Glendinning 
232f7ca802SSteve Glendinning #define SMSC_CHIPNAME			"smsc95xx"
2453a759c8SMartin Wetterwald #define SMSC_DRIVER_VERSION		"1.0.6"
252f7ca802SSteve Glendinning #define HS_USB_PKT_SIZE			(512)
262f7ca802SSteve Glendinning #define FS_USB_PKT_SIZE			(64)
272f7ca802SSteve Glendinning #define DEFAULT_HS_BURST_CAP_SIZE	(16 * 1024 + 5 * HS_USB_PKT_SIZE)
282f7ca802SSteve Glendinning #define DEFAULT_FS_BURST_CAP_SIZE	(6 * 1024 + 33 * FS_USB_PKT_SIZE)
292f7ca802SSteve Glendinning #define DEFAULT_BULK_IN_DELAY		(0x00002000)
302f7ca802SSteve Glendinning #define MAX_SINGLE_PACKET_SIZE		(2048)
312f7ca802SSteve Glendinning #define LAN95XX_EEPROM_MAGIC		(0x9500)
322f7ca802SSteve Glendinning #define EEPROM_MAC_OFFSET		(0x01)
33f7b29271SSteve Glendinning #define DEFAULT_TX_CSUM_ENABLE		(true)
342f7ca802SSteve Glendinning #define DEFAULT_RX_CSUM_ENABLE		(true)
352f7ca802SSteve Glendinning #define SMSC95XX_INTERNAL_PHY_ID	(1)
362f7ca802SSteve Glendinning #define SMSC95XX_TX_OVERHEAD		(8)
37f7b29271SSteve Glendinning #define SMSC95XX_TX_OVERHEAD_CSUM	(12)
38e5e3af83SSteve Glendinning #define SUPPORTED_WAKE			(WAKE_PHY | WAKE_UCAST | WAKE_BCAST | \
39bbd9f9eeSSteve Glendinning 					 WAKE_MCAST | WAKE_ARP | WAKE_MAGIC)
402f7ca802SSteve Glendinning 
419ebca507SSteve Glendinning #define FEATURE_8_WAKEUP_FILTERS	(0x01)
429ebca507SSteve Glendinning #define FEATURE_PHY_NLP_CROSSOVER	(0x02)
43eb970ff0SMing Lei #define FEATURE_REMOTE_WAKEUP		(0x04)
449ebca507SSteve Glendinning 
45b2d4b150SSteve Glendinning #define SUSPEND_SUSPEND0		(0x01)
46b2d4b150SSteve Glendinning #define SUSPEND_SUSPEND1		(0x02)
47b2d4b150SSteve Glendinning #define SUSPEND_SUSPEND2		(0x04)
48b2d4b150SSteve Glendinning #define SUSPEND_SUSPEND3		(0x08)
49b2d4b150SSteve Glendinning #define SUSPEND_ALLMODES		(SUSPEND_SUSPEND0 | SUSPEND_SUSPEND1 | \
50b2d4b150SSteve Glendinning 					 SUSPEND_SUSPEND2 | SUSPEND_SUSPEND3)
51b2d4b150SSteve Glendinning 
52d69d1694SChristoph Fritz #define CARRIER_CHECK_DELAY (2 * HZ)
53d69d1694SChristoph Fritz 
542f7ca802SSteve Glendinning struct smsc95xx_priv {
5513722bbeSWoojung Huh 	u32 chip_id;
562f7ca802SSteve Glendinning 	u32 mac_cr;
573c0f3c60SMarc Zyngier 	u32 hash_hi;
583c0f3c60SMarc Zyngier 	u32 hash_lo;
59e0e474a8SSteve Glendinning 	u32 wolopts;
602f7ca802SSteve Glendinning 	spinlock_t mac_cr_lock;
619ebca507SSteve Glendinning 	u8 features;
62b2d4b150SSteve Glendinning 	u8 suspend_flags;
6313722bbeSWoojung Huh 	u8 mdix_ctrl;
64d69d1694SChristoph Fritz 	bool link_ok;
65d69d1694SChristoph Fritz 	struct delayed_work carrier_check;
66d69d1694SChristoph Fritz 	struct usbnet *dev;
672f7ca802SSteve Glendinning };
682f7ca802SSteve Glendinning 
69eb939922SRusty Russell static bool turbo_mode = true;
702f7ca802SSteve Glendinning module_param(turbo_mode, bool, 0644);
712f7ca802SSteve Glendinning MODULE_PARM_DESC(turbo_mode, "Enable multiple frames per Rx transaction");
722f7ca802SSteve Glendinning 
73ec32115dSMing Lei static int __must_check __smsc95xx_read_reg(struct usbnet *dev, u32 index,
74ec32115dSMing Lei 					    u32 *data, int in_pm)
752f7ca802SSteve Glendinning {
7672108fd2SMing Lei 	u32 buf;
772f7ca802SSteve Glendinning 	int ret;
78ec32115dSMing Lei 	int (*fn)(struct usbnet *, u8, u8, u16, u16, void *, u16);
792f7ca802SSteve Glendinning 
802f7ca802SSteve Glendinning 	BUG_ON(!dev);
812f7ca802SSteve Glendinning 
82ec32115dSMing Lei 	if (!in_pm)
83ec32115dSMing Lei 		fn = usbnet_read_cmd;
84ec32115dSMing Lei 	else
85ec32115dSMing Lei 		fn = usbnet_read_cmd_nopm;
86ec32115dSMing Lei 
87ec32115dSMing Lei 	ret = fn(dev, USB_VENDOR_REQUEST_READ_REGISTER, USB_DIR_IN
88ec32115dSMing Lei 		 | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
8972108fd2SMing Lei 		 0, index, &buf, 4);
905a36b68bSDan Carpenter 	if (unlikely(ret < 0)) {
911e1d7412SJoe Perches 		netdev_warn(dev->net, "Failed to read reg index 0x%08x: %d\n",
921e1d7412SJoe Perches 			    index, ret);
935a36b68bSDan Carpenter 		return ret;
945a36b68bSDan Carpenter 	}
952f7ca802SSteve Glendinning 
9672108fd2SMing Lei 	le32_to_cpus(&buf);
9772108fd2SMing Lei 	*data = buf;
982f7ca802SSteve Glendinning 
992f7ca802SSteve Glendinning 	return ret;
1002f7ca802SSteve Glendinning }
1012f7ca802SSteve Glendinning 
102ec32115dSMing Lei static int __must_check __smsc95xx_write_reg(struct usbnet *dev, u32 index,
103ec32115dSMing Lei 					     u32 data, int in_pm)
1042f7ca802SSteve Glendinning {
10572108fd2SMing Lei 	u32 buf;
1062f7ca802SSteve Glendinning 	int ret;
107ec32115dSMing Lei 	int (*fn)(struct usbnet *, u8, u8, u16, u16, const void *, u16);
1082f7ca802SSteve Glendinning 
1092f7ca802SSteve Glendinning 	BUG_ON(!dev);
1102f7ca802SSteve Glendinning 
111ec32115dSMing Lei 	if (!in_pm)
112ec32115dSMing Lei 		fn = usbnet_write_cmd;
113ec32115dSMing Lei 	else
114ec32115dSMing Lei 		fn = usbnet_write_cmd_nopm;
115ec32115dSMing Lei 
11672108fd2SMing Lei 	buf = data;
11772108fd2SMing Lei 	cpu_to_le32s(&buf);
1182f7ca802SSteve Glendinning 
119ec32115dSMing Lei 	ret = fn(dev, USB_VENDOR_REQUEST_WRITE_REGISTER, USB_DIR_OUT
120ec32115dSMing Lei 		 | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
12172108fd2SMing Lei 		 0, index, &buf, 4);
1222f7ca802SSteve Glendinning 	if (unlikely(ret < 0))
1231e1d7412SJoe Perches 		netdev_warn(dev->net, "Failed to write reg index 0x%08x: %d\n",
1241e1d7412SJoe Perches 			    index, ret);
1252f7ca802SSteve Glendinning 
1262f7ca802SSteve Glendinning 	return ret;
1272f7ca802SSteve Glendinning }
1282f7ca802SSteve Glendinning 
129ec32115dSMing Lei static int __must_check smsc95xx_read_reg_nopm(struct usbnet *dev, u32 index,
130ec32115dSMing Lei 					       u32 *data)
131ec32115dSMing Lei {
132ec32115dSMing Lei 	return __smsc95xx_read_reg(dev, index, data, 1);
133ec32115dSMing Lei }
134ec32115dSMing Lei 
135ec32115dSMing Lei static int __must_check smsc95xx_write_reg_nopm(struct usbnet *dev, u32 index,
136ec32115dSMing Lei 						u32 data)
137ec32115dSMing Lei {
138ec32115dSMing Lei 	return __smsc95xx_write_reg(dev, index, data, 1);
139ec32115dSMing Lei }
140ec32115dSMing Lei 
141ec32115dSMing Lei static int __must_check smsc95xx_read_reg(struct usbnet *dev, u32 index,
142ec32115dSMing Lei 					  u32 *data)
143ec32115dSMing Lei {
144ec32115dSMing Lei 	return __smsc95xx_read_reg(dev, index, data, 0);
145ec32115dSMing Lei }
146ec32115dSMing Lei 
147ec32115dSMing Lei static int __must_check smsc95xx_write_reg(struct usbnet *dev, u32 index,
148ec32115dSMing Lei 					   u32 data)
149ec32115dSMing Lei {
150ec32115dSMing Lei 	return __smsc95xx_write_reg(dev, index, data, 0);
151ec32115dSMing Lei }
152e0e474a8SSteve Glendinning 
1532f7ca802SSteve Glendinning /* Loop until the read is completed with timeout
1542f7ca802SSteve Glendinning  * called with phy_mutex held */
155e5e3af83SSteve Glendinning static int __must_check __smsc95xx_phy_wait_not_busy(struct usbnet *dev,
156e5e3af83SSteve Glendinning 						     int in_pm)
1572f7ca802SSteve Glendinning {
1582f7ca802SSteve Glendinning 	unsigned long start_time = jiffies;
1592f7ca802SSteve Glendinning 	u32 val;
160769ea6d8SSteve Glendinning 	int ret;
1612f7ca802SSteve Glendinning 
1622f7ca802SSteve Glendinning 	do {
163e5e3af83SSteve Glendinning 		ret = __smsc95xx_read_reg(dev, MII_ADDR, &val, in_pm);
164b052e073SSteve Glendinning 		if (ret < 0) {
165b052e073SSteve Glendinning 			netdev_warn(dev->net, "Error reading MII_ACCESS\n");
166b052e073SSteve Glendinning 			return ret;
167b052e073SSteve Glendinning 		}
168b052e073SSteve Glendinning 
1692f7ca802SSteve Glendinning 		if (!(val & MII_BUSY_))
1702f7ca802SSteve Glendinning 			return 0;
1712f7ca802SSteve Glendinning 	} while (!time_after(jiffies, start_time + HZ));
1722f7ca802SSteve Glendinning 
1732f7ca802SSteve Glendinning 	return -EIO;
1742f7ca802SSteve Glendinning }
1752f7ca802SSteve Glendinning 
176e5e3af83SSteve Glendinning static int __smsc95xx_mdio_read(struct net_device *netdev, int phy_id, int idx,
177e5e3af83SSteve Glendinning 				int in_pm)
1782f7ca802SSteve Glendinning {
1792f7ca802SSteve Glendinning 	struct usbnet *dev = netdev_priv(netdev);
1802f7ca802SSteve Glendinning 	u32 val, addr;
181769ea6d8SSteve Glendinning 	int ret;
1822f7ca802SSteve Glendinning 
1832f7ca802SSteve Glendinning 	mutex_lock(&dev->phy_mutex);
1842f7ca802SSteve Glendinning 
1852f7ca802SSteve Glendinning 	/* confirm MII not busy */
186e5e3af83SSteve Glendinning 	ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
187b052e073SSteve Glendinning 	if (ret < 0) {
188b052e073SSteve Glendinning 		netdev_warn(dev->net, "MII is busy in smsc95xx_mdio_read\n");
189b052e073SSteve Glendinning 		goto done;
190b052e073SSteve Glendinning 	}
1912f7ca802SSteve Glendinning 
1922f7ca802SSteve Glendinning 	/* set the address, index & direction (read from PHY) */
1932f7ca802SSteve Glendinning 	phy_id &= dev->mii.phy_id_mask;
1942f7ca802SSteve Glendinning 	idx &= dev->mii.reg_num_mask;
19580928805SSteve Glendinning 	addr = (phy_id << 11) | (idx << 6) | MII_READ_ | MII_BUSY_;
196e5e3af83SSteve Glendinning 	ret = __smsc95xx_write_reg(dev, MII_ADDR, addr, in_pm);
197b052e073SSteve Glendinning 	if (ret < 0) {
198b052e073SSteve Glendinning 		netdev_warn(dev->net, "Error writing MII_ADDR\n");
199b052e073SSteve Glendinning 		goto done;
200b052e073SSteve Glendinning 	}
2012f7ca802SSteve Glendinning 
202e5e3af83SSteve Glendinning 	ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
203b052e073SSteve Glendinning 	if (ret < 0) {
204b052e073SSteve Glendinning 		netdev_warn(dev->net, "Timed out reading MII reg %02X\n", idx);
205b052e073SSteve Glendinning 		goto done;
206b052e073SSteve Glendinning 	}
207769ea6d8SSteve Glendinning 
208e5e3af83SSteve Glendinning 	ret = __smsc95xx_read_reg(dev, MII_DATA, &val, in_pm);
209b052e073SSteve Glendinning 	if (ret < 0) {
210b052e073SSteve Glendinning 		netdev_warn(dev->net, "Error reading MII_DATA\n");
211b052e073SSteve Glendinning 		goto done;
212b052e073SSteve Glendinning 	}
213769ea6d8SSteve Glendinning 
214769ea6d8SSteve Glendinning 	ret = (u16)(val & 0xFFFF);
215769ea6d8SSteve Glendinning 
216769ea6d8SSteve Glendinning done:
2172f7ca802SSteve Glendinning 	mutex_unlock(&dev->phy_mutex);
218769ea6d8SSteve Glendinning 	return ret;
2192f7ca802SSteve Glendinning }
2202f7ca802SSteve Glendinning 
221e5e3af83SSteve Glendinning static void __smsc95xx_mdio_write(struct net_device *netdev, int phy_id,
222e5e3af83SSteve Glendinning 				  int idx, int regval, int in_pm)
2232f7ca802SSteve Glendinning {
2242f7ca802SSteve Glendinning 	struct usbnet *dev = netdev_priv(netdev);
2252f7ca802SSteve Glendinning 	u32 val, addr;
226769ea6d8SSteve Glendinning 	int ret;
2272f7ca802SSteve Glendinning 
2282f7ca802SSteve Glendinning 	mutex_lock(&dev->phy_mutex);
2292f7ca802SSteve Glendinning 
2302f7ca802SSteve Glendinning 	/* confirm MII not busy */
231e5e3af83SSteve Glendinning 	ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
232b052e073SSteve Glendinning 	if (ret < 0) {
233b052e073SSteve Glendinning 		netdev_warn(dev->net, "MII is busy in smsc95xx_mdio_write\n");
234b052e073SSteve Glendinning 		goto done;
235b052e073SSteve Glendinning 	}
2362f7ca802SSteve Glendinning 
2372f7ca802SSteve Glendinning 	val = regval;
238e5e3af83SSteve Glendinning 	ret = __smsc95xx_write_reg(dev, MII_DATA, val, in_pm);
239b052e073SSteve Glendinning 	if (ret < 0) {
240b052e073SSteve Glendinning 		netdev_warn(dev->net, "Error writing MII_DATA\n");
241b052e073SSteve Glendinning 		goto done;
242b052e073SSteve Glendinning 	}
2432f7ca802SSteve Glendinning 
2442f7ca802SSteve Glendinning 	/* set the address, index & direction (write to PHY) */
2452f7ca802SSteve Glendinning 	phy_id &= dev->mii.phy_id_mask;
2462f7ca802SSteve Glendinning 	idx &= dev->mii.reg_num_mask;
24780928805SSteve Glendinning 	addr = (phy_id << 11) | (idx << 6) | MII_WRITE_ | MII_BUSY_;
248e5e3af83SSteve Glendinning 	ret = __smsc95xx_write_reg(dev, MII_ADDR, addr, in_pm);
249b052e073SSteve Glendinning 	if (ret < 0) {
250b052e073SSteve Glendinning 		netdev_warn(dev->net, "Error writing MII_ADDR\n");
251b052e073SSteve Glendinning 		goto done;
252b052e073SSteve Glendinning 	}
2532f7ca802SSteve Glendinning 
254e5e3af83SSteve Glendinning 	ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
255b052e073SSteve Glendinning 	if (ret < 0) {
256b052e073SSteve Glendinning 		netdev_warn(dev->net, "Timed out writing MII reg %02X\n", idx);
257b052e073SSteve Glendinning 		goto done;
258b052e073SSteve Glendinning 	}
2592f7ca802SSteve Glendinning 
260769ea6d8SSteve Glendinning done:
2612f7ca802SSteve Glendinning 	mutex_unlock(&dev->phy_mutex);
2622f7ca802SSteve Glendinning }
2632f7ca802SSteve Glendinning 
264e5e3af83SSteve Glendinning static int smsc95xx_mdio_read_nopm(struct net_device *netdev, int phy_id,
265e5e3af83SSteve Glendinning 				   int idx)
266e5e3af83SSteve Glendinning {
267e5e3af83SSteve Glendinning 	return __smsc95xx_mdio_read(netdev, phy_id, idx, 1);
268e5e3af83SSteve Glendinning }
269e5e3af83SSteve Glendinning 
270e5e3af83SSteve Glendinning static void smsc95xx_mdio_write_nopm(struct net_device *netdev, int phy_id,
271e5e3af83SSteve Glendinning 				     int idx, int regval)
272e5e3af83SSteve Glendinning {
273e5e3af83SSteve Glendinning 	__smsc95xx_mdio_write(netdev, phy_id, idx, regval, 1);
274e5e3af83SSteve Glendinning }
275e5e3af83SSteve Glendinning 
276e5e3af83SSteve Glendinning static int smsc95xx_mdio_read(struct net_device *netdev, int phy_id, int idx)
277e5e3af83SSteve Glendinning {
278e5e3af83SSteve Glendinning 	return __smsc95xx_mdio_read(netdev, phy_id, idx, 0);
279e5e3af83SSteve Glendinning }
280e5e3af83SSteve Glendinning 
281e5e3af83SSteve Glendinning static void smsc95xx_mdio_write(struct net_device *netdev, int phy_id, int idx,
282e5e3af83SSteve Glendinning 				int regval)
283e5e3af83SSteve Glendinning {
284e5e3af83SSteve Glendinning 	__smsc95xx_mdio_write(netdev, phy_id, idx, regval, 0);
285e5e3af83SSteve Glendinning }
286e5e3af83SSteve Glendinning 
287769ea6d8SSteve Glendinning static int __must_check smsc95xx_wait_eeprom(struct usbnet *dev)
2882f7ca802SSteve Glendinning {
2892f7ca802SSteve Glendinning 	unsigned long start_time = jiffies;
2902f7ca802SSteve Glendinning 	u32 val;
291769ea6d8SSteve Glendinning 	int ret;
2922f7ca802SSteve Glendinning 
2932f7ca802SSteve Glendinning 	do {
294769ea6d8SSteve Glendinning 		ret = smsc95xx_read_reg(dev, E2P_CMD, &val);
295b052e073SSteve Glendinning 		if (ret < 0) {
296b052e073SSteve Glendinning 			netdev_warn(dev->net, "Error reading E2P_CMD\n");
297b052e073SSteve Glendinning 			return ret;
298b052e073SSteve Glendinning 		}
299b052e073SSteve Glendinning 
3002f7ca802SSteve Glendinning 		if (!(val & E2P_CMD_BUSY_) || (val & E2P_CMD_TIMEOUT_))
3012f7ca802SSteve Glendinning 			break;
3022f7ca802SSteve Glendinning 		udelay(40);
3032f7ca802SSteve Glendinning 	} while (!time_after(jiffies, start_time + HZ));
3042f7ca802SSteve Glendinning 
3052f7ca802SSteve Glendinning 	if (val & (E2P_CMD_TIMEOUT_ | E2P_CMD_BUSY_)) {
30660b86755SJoe Perches 		netdev_warn(dev->net, "EEPROM read operation timeout\n");
3072f7ca802SSteve Glendinning 		return -EIO;
3082f7ca802SSteve Glendinning 	}
3092f7ca802SSteve Glendinning 
3102f7ca802SSteve Glendinning 	return 0;
3112f7ca802SSteve Glendinning }
3122f7ca802SSteve Glendinning 
313769ea6d8SSteve Glendinning static int __must_check smsc95xx_eeprom_confirm_not_busy(struct usbnet *dev)
3142f7ca802SSteve Glendinning {
3152f7ca802SSteve Glendinning 	unsigned long start_time = jiffies;
3162f7ca802SSteve Glendinning 	u32 val;
317769ea6d8SSteve Glendinning 	int ret;
3182f7ca802SSteve Glendinning 
3192f7ca802SSteve Glendinning 	do {
320769ea6d8SSteve Glendinning 		ret = smsc95xx_read_reg(dev, E2P_CMD, &val);
321b052e073SSteve Glendinning 		if (ret < 0) {
322b052e073SSteve Glendinning 			netdev_warn(dev->net, "Error reading E2P_CMD\n");
323b052e073SSteve Glendinning 			return ret;
324b052e073SSteve Glendinning 		}
3252f7ca802SSteve Glendinning 
3262f7ca802SSteve Glendinning 		if (!(val & E2P_CMD_BUSY_))
3272f7ca802SSteve Glendinning 			return 0;
3282f7ca802SSteve Glendinning 
3292f7ca802SSteve Glendinning 		udelay(40);
3302f7ca802SSteve Glendinning 	} while (!time_after(jiffies, start_time + HZ));
3312f7ca802SSteve Glendinning 
33260b86755SJoe Perches 	netdev_warn(dev->net, "EEPROM is busy\n");
3332f7ca802SSteve Glendinning 	return -EIO;
3342f7ca802SSteve Glendinning }
3352f7ca802SSteve Glendinning 
3362f7ca802SSteve Glendinning static int smsc95xx_read_eeprom(struct usbnet *dev, u32 offset, u32 length,
3372f7ca802SSteve Glendinning 				u8 *data)
3382f7ca802SSteve Glendinning {
3392f7ca802SSteve Glendinning 	u32 val;
3402f7ca802SSteve Glendinning 	int i, ret;
3412f7ca802SSteve Glendinning 
3422f7ca802SSteve Glendinning 	BUG_ON(!dev);
3432f7ca802SSteve Glendinning 	BUG_ON(!data);
3442f7ca802SSteve Glendinning 
3452f7ca802SSteve Glendinning 	ret = smsc95xx_eeprom_confirm_not_busy(dev);
3462f7ca802SSteve Glendinning 	if (ret)
3472f7ca802SSteve Glendinning 		return ret;
3482f7ca802SSteve Glendinning 
3492f7ca802SSteve Glendinning 	for (i = 0; i < length; i++) {
3502f7ca802SSteve Glendinning 		val = E2P_CMD_BUSY_ | E2P_CMD_READ_ | (offset & E2P_CMD_ADDR_);
351769ea6d8SSteve Glendinning 		ret = smsc95xx_write_reg(dev, E2P_CMD, val);
352b052e073SSteve Glendinning 		if (ret < 0) {
353b052e073SSteve Glendinning 			netdev_warn(dev->net, "Error writing E2P_CMD\n");
354b052e073SSteve Glendinning 			return ret;
355b052e073SSteve Glendinning 		}
3562f7ca802SSteve Glendinning 
3572f7ca802SSteve Glendinning 		ret = smsc95xx_wait_eeprom(dev);
3582f7ca802SSteve Glendinning 		if (ret < 0)
3592f7ca802SSteve Glendinning 			return ret;
3602f7ca802SSteve Glendinning 
361769ea6d8SSteve Glendinning 		ret = smsc95xx_read_reg(dev, E2P_DATA, &val);
362b052e073SSteve Glendinning 		if (ret < 0) {
363b052e073SSteve Glendinning 			netdev_warn(dev->net, "Error reading E2P_DATA\n");
364b052e073SSteve Glendinning 			return ret;
365b052e073SSteve Glendinning 		}
3662f7ca802SSteve Glendinning 
3672f7ca802SSteve Glendinning 		data[i] = val & 0xFF;
3682f7ca802SSteve Glendinning 		offset++;
3692f7ca802SSteve Glendinning 	}
3702f7ca802SSteve Glendinning 
3712f7ca802SSteve Glendinning 	return 0;
3722f7ca802SSteve Glendinning }
3732f7ca802SSteve Glendinning 
3742f7ca802SSteve Glendinning static int smsc95xx_write_eeprom(struct usbnet *dev, u32 offset, u32 length,
3752f7ca802SSteve Glendinning 				 u8 *data)
3762f7ca802SSteve Glendinning {
3772f7ca802SSteve Glendinning 	u32 val;
3782f7ca802SSteve Glendinning 	int i, ret;
3792f7ca802SSteve Glendinning 
3802f7ca802SSteve Glendinning 	BUG_ON(!dev);
3812f7ca802SSteve Glendinning 	BUG_ON(!data);
3822f7ca802SSteve Glendinning 
3832f7ca802SSteve Glendinning 	ret = smsc95xx_eeprom_confirm_not_busy(dev);
3842f7ca802SSteve Glendinning 	if (ret)
3852f7ca802SSteve Glendinning 		return ret;
3862f7ca802SSteve Glendinning 
3872f7ca802SSteve Glendinning 	/* Issue write/erase enable command */
3882f7ca802SSteve Glendinning 	val = E2P_CMD_BUSY_ | E2P_CMD_EWEN_;
389769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, E2P_CMD, val);
390b052e073SSteve Glendinning 	if (ret < 0) {
391b052e073SSteve Glendinning 		netdev_warn(dev->net, "Error writing E2P_DATA\n");
392b052e073SSteve Glendinning 		return ret;
393b052e073SSteve Glendinning 	}
3942f7ca802SSteve Glendinning 
3952f7ca802SSteve Glendinning 	ret = smsc95xx_wait_eeprom(dev);
3962f7ca802SSteve Glendinning 	if (ret < 0)
3972f7ca802SSteve Glendinning 		return ret;
3982f7ca802SSteve Glendinning 
3992f7ca802SSteve Glendinning 	for (i = 0; i < length; i++) {
4002f7ca802SSteve Glendinning 
4012f7ca802SSteve Glendinning 		/* Fill data register */
4022f7ca802SSteve Glendinning 		val = data[i];
403769ea6d8SSteve Glendinning 		ret = smsc95xx_write_reg(dev, E2P_DATA, val);
404b052e073SSteve Glendinning 		if (ret < 0) {
405b052e073SSteve Glendinning 			netdev_warn(dev->net, "Error writing E2P_DATA\n");
406b052e073SSteve Glendinning 			return ret;
407b052e073SSteve Glendinning 		}
4082f7ca802SSteve Glendinning 
4092f7ca802SSteve Glendinning 		/* Send "write" command */
4102f7ca802SSteve Glendinning 		val = E2P_CMD_BUSY_ | E2P_CMD_WRITE_ | (offset & E2P_CMD_ADDR_);
411769ea6d8SSteve Glendinning 		ret = smsc95xx_write_reg(dev, E2P_CMD, val);
412b052e073SSteve Glendinning 		if (ret < 0) {
413b052e073SSteve Glendinning 			netdev_warn(dev->net, "Error writing E2P_CMD\n");
414b052e073SSteve Glendinning 			return ret;
415b052e073SSteve Glendinning 		}
4162f7ca802SSteve Glendinning 
4172f7ca802SSteve Glendinning 		ret = smsc95xx_wait_eeprom(dev);
4182f7ca802SSteve Glendinning 		if (ret < 0)
4192f7ca802SSteve Glendinning 			return ret;
4202f7ca802SSteve Glendinning 
4212f7ca802SSteve Glendinning 		offset++;
4222f7ca802SSteve Glendinning 	}
4232f7ca802SSteve Glendinning 
4242f7ca802SSteve Glendinning 	return 0;
4252f7ca802SSteve Glendinning }
4262f7ca802SSteve Glendinning 
427769ea6d8SSteve Glendinning static int __must_check smsc95xx_write_reg_async(struct usbnet *dev, u16 index,
4287b9e7580SSteve Glendinning 						 u32 data)
4292f7ca802SSteve Glendinning {
4301d74a6bdSSteve Glendinning 	const u16 size = 4;
4317b9e7580SSteve Glendinning 	u32 buf;
43272108fd2SMing Lei 	int ret;
4332f7ca802SSteve Glendinning 
4347b9e7580SSteve Glendinning 	buf = data;
4357b9e7580SSteve Glendinning 	cpu_to_le32s(&buf);
4367b9e7580SSteve Glendinning 
43772108fd2SMing Lei 	ret = usbnet_write_cmd_async(dev, USB_VENDOR_REQUEST_WRITE_REGISTER,
43872108fd2SMing Lei 				     USB_DIR_OUT | USB_TYPE_VENDOR |
43972108fd2SMing Lei 				     USB_RECIP_DEVICE,
4407b9e7580SSteve Glendinning 				     0, index, &buf, size);
44172108fd2SMing Lei 	if (ret < 0)
44272108fd2SMing Lei 		netdev_warn(dev->net, "Error write async cmd, sts=%d\n",
44372108fd2SMing Lei 			    ret);
44472108fd2SMing Lei 	return ret;
4452f7ca802SSteve Glendinning }
4462f7ca802SSteve Glendinning 
4472f7ca802SSteve Glendinning /* returns hash bit number for given MAC address
4482f7ca802SSteve Glendinning  * example:
4492f7ca802SSteve Glendinning  * 01 00 5E 00 00 01 -> returns bit number 31 */
4502f7ca802SSteve Glendinning static unsigned int smsc95xx_hash(char addr[ETH_ALEN])
4512f7ca802SSteve Glendinning {
4522f7ca802SSteve Glendinning 	return (ether_crc(ETH_ALEN, addr) >> 26) & 0x3f;
4532f7ca802SSteve Glendinning }
4542f7ca802SSteve Glendinning 
4552f7ca802SSteve Glendinning static void smsc95xx_set_multicast(struct net_device *netdev)
4562f7ca802SSteve Glendinning {
4572f7ca802SSteve Glendinning 	struct usbnet *dev = netdev_priv(netdev);
4582f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
4592f7ca802SSteve Glendinning 	unsigned long flags;
460769ea6d8SSteve Glendinning 	int ret;
4612f7ca802SSteve Glendinning 
4623c0f3c60SMarc Zyngier 	pdata->hash_hi = 0;
4633c0f3c60SMarc Zyngier 	pdata->hash_lo = 0;
4643c0f3c60SMarc Zyngier 
4652f7ca802SSteve Glendinning 	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
4662f7ca802SSteve Glendinning 
4672f7ca802SSteve Glendinning 	if (dev->net->flags & IFF_PROMISC) {
468a475f603SJoe Perches 		netif_dbg(dev, drv, dev->net, "promiscuous mode enabled\n");
4692f7ca802SSteve Glendinning 		pdata->mac_cr |= MAC_CR_PRMS_;
4702f7ca802SSteve Glendinning 		pdata->mac_cr &= ~(MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
4712f7ca802SSteve Glendinning 	} else if (dev->net->flags & IFF_ALLMULTI) {
472a475f603SJoe Perches 		netif_dbg(dev, drv, dev->net, "receive all multicast enabled\n");
4732f7ca802SSteve Glendinning 		pdata->mac_cr |= MAC_CR_MCPAS_;
4742f7ca802SSteve Glendinning 		pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_HPFILT_);
4754cd24eafSJiri Pirko 	} else if (!netdev_mc_empty(dev->net)) {
47622bedad3SJiri Pirko 		struct netdev_hw_addr *ha;
4772f7ca802SSteve Glendinning 
4782f7ca802SSteve Glendinning 		pdata->mac_cr |= MAC_CR_HPFILT_;
4792f7ca802SSteve Glendinning 		pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_);
4802f7ca802SSteve Glendinning 
48122bedad3SJiri Pirko 		netdev_for_each_mc_addr(ha, netdev) {
48222bedad3SJiri Pirko 			u32 bitnum = smsc95xx_hash(ha->addr);
4832f7ca802SSteve Glendinning 			u32 mask = 0x01 << (bitnum & 0x1F);
4842f7ca802SSteve Glendinning 			if (bitnum & 0x20)
4853c0f3c60SMarc Zyngier 				pdata->hash_hi |= mask;
4862f7ca802SSteve Glendinning 			else
4873c0f3c60SMarc Zyngier 				pdata->hash_lo |= mask;
4882f7ca802SSteve Glendinning 		}
4892f7ca802SSteve Glendinning 
490a475f603SJoe Perches 		netif_dbg(dev, drv, dev->net, "HASHH=0x%08X, HASHL=0x%08X\n",
4913c0f3c60SMarc Zyngier 				   pdata->hash_hi, pdata->hash_lo);
4922f7ca802SSteve Glendinning 	} else {
493a475f603SJoe Perches 		netif_dbg(dev, drv, dev->net, "receive own packets only\n");
4942f7ca802SSteve Glendinning 		pdata->mac_cr &=
4952f7ca802SSteve Glendinning 			~(MAC_CR_PRMS_ | MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
4962f7ca802SSteve Glendinning 	}
4972f7ca802SSteve Glendinning 
4982f7ca802SSteve Glendinning 	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
4992f7ca802SSteve Glendinning 
5002f7ca802SSteve Glendinning 	/* Initiate async writes, as we can't wait for completion here */
5017b9e7580SSteve Glendinning 	ret = smsc95xx_write_reg_async(dev, HASHH, pdata->hash_hi);
502b052e073SSteve Glendinning 	if (ret < 0)
503b052e073SSteve Glendinning 		netdev_warn(dev->net, "failed to initiate async write to HASHH\n");
504769ea6d8SSteve Glendinning 
5057b9e7580SSteve Glendinning 	ret = smsc95xx_write_reg_async(dev, HASHL, pdata->hash_lo);
506b052e073SSteve Glendinning 	if (ret < 0)
507b052e073SSteve Glendinning 		netdev_warn(dev->net, "failed to initiate async write to HASHL\n");
508769ea6d8SSteve Glendinning 
5097b9e7580SSteve Glendinning 	ret = smsc95xx_write_reg_async(dev, MAC_CR, pdata->mac_cr);
510b052e073SSteve Glendinning 	if (ret < 0)
511b052e073SSteve Glendinning 		netdev_warn(dev->net, "failed to initiate async write to MAC_CR\n");
5122f7ca802SSteve Glendinning }
5132f7ca802SSteve Glendinning 
514769ea6d8SSteve Glendinning static int smsc95xx_phy_update_flowcontrol(struct usbnet *dev, u8 duplex,
5152f7ca802SSteve Glendinning 					   u16 lcladv, u16 rmtadv)
5162f7ca802SSteve Glendinning {
5179c082731SNisar Sayed 	u32 flow = 0, afc_cfg;
5182f7ca802SSteve Glendinning 
5192f7ca802SSteve Glendinning 	int ret = smsc95xx_read_reg(dev, AFC_CFG, &afc_cfg);
520e360a8b4SSteve Glendinning 	if (ret < 0)
521b052e073SSteve Glendinning 		return ret;
5222f7ca802SSteve Glendinning 
5232f7ca802SSteve Glendinning 	if (duplex == DUPLEX_FULL) {
524bc02ff95SSteve Glendinning 		u8 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
5252f7ca802SSteve Glendinning 
5262f7ca802SSteve Glendinning 		if (cap & FLOW_CTRL_RX)
5272f7ca802SSteve Glendinning 			flow = 0xFFFF0002;
5282f7ca802SSteve Glendinning 
5299c082731SNisar Sayed 		if (cap & FLOW_CTRL_TX) {
5302f7ca802SSteve Glendinning 			afc_cfg |= 0xF;
5319c082731SNisar Sayed 			flow |= 0xFFFF0000;
5329c082731SNisar Sayed 		} else {
5332f7ca802SSteve Glendinning 			afc_cfg &= ~0xF;
5349c082731SNisar Sayed 		}
5352f7ca802SSteve Glendinning 
536a475f603SJoe Perches 		netif_dbg(dev, link, dev->net, "rx pause %s, tx pause %s\n",
53760b86755SJoe Perches 				   cap & FLOW_CTRL_RX ? "enabled" : "disabled",
53860b86755SJoe Perches 				   cap & FLOW_CTRL_TX ? "enabled" : "disabled");
5392f7ca802SSteve Glendinning 	} else {
540a475f603SJoe Perches 		netif_dbg(dev, link, dev->net, "half duplex\n");
5412f7ca802SSteve Glendinning 		afc_cfg |= 0xF;
5422f7ca802SSteve Glendinning 	}
5432f7ca802SSteve Glendinning 
544769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, FLOW, flow);
545b052e073SSteve Glendinning 	if (ret < 0)
546b052e073SSteve Glendinning 		return ret;
547e360a8b4SSteve Glendinning 
548e360a8b4SSteve Glendinning 	return smsc95xx_write_reg(dev, AFC_CFG, afc_cfg);
5492f7ca802SSteve Glendinning }
5502f7ca802SSteve Glendinning 
5512f7ca802SSteve Glendinning static int smsc95xx_link_reset(struct usbnet *dev)
5522f7ca802SSteve Glendinning {
5532f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
5542f7ca802SSteve Glendinning 	struct mii_if_info *mii = &dev->mii;
5558ae6dacaSDavid Decotigny 	struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
5562f7ca802SSteve Glendinning 	unsigned long flags;
5572f7ca802SSteve Glendinning 	u16 lcladv, rmtadv;
558769ea6d8SSteve Glendinning 	int ret;
5592f7ca802SSteve Glendinning 
5602f7ca802SSteve Glendinning 	/* clear interrupt status */
561769ea6d8SSteve Glendinning 	ret = smsc95xx_mdio_read(dev->net, mii->phy_id, PHY_INT_SRC);
562e360a8b4SSteve Glendinning 	if (ret < 0)
563b052e073SSteve Glendinning 		return ret;
564769ea6d8SSteve Glendinning 
565769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
566e360a8b4SSteve Glendinning 	if (ret < 0)
567b052e073SSteve Glendinning 		return ret;
5682f7ca802SSteve Glendinning 
5692f7ca802SSteve Glendinning 	mii_check_media(mii, 1, 1);
5702f7ca802SSteve Glendinning 	mii_ethtool_gset(&dev->mii, &ecmd);
5712f7ca802SSteve Glendinning 	lcladv = smsc95xx_mdio_read(dev->net, mii->phy_id, MII_ADVERTISE);
5722f7ca802SSteve Glendinning 	rmtadv = smsc95xx_mdio_read(dev->net, mii->phy_id, MII_LPA);
5732f7ca802SSteve Glendinning 
5748ae6dacaSDavid Decotigny 	netif_dbg(dev, link, dev->net,
5758ae6dacaSDavid Decotigny 		  "speed: %u duplex: %d lcladv: %04x rmtadv: %04x\n",
5768ae6dacaSDavid Decotigny 		  ethtool_cmd_speed(&ecmd), ecmd.duplex, lcladv, rmtadv);
5772f7ca802SSteve Glendinning 
5782f7ca802SSteve Glendinning 	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
5792f7ca802SSteve Glendinning 	if (ecmd.duplex != DUPLEX_FULL) {
5802f7ca802SSteve Glendinning 		pdata->mac_cr &= ~MAC_CR_FDPX_;
5812f7ca802SSteve Glendinning 		pdata->mac_cr |= MAC_CR_RCVOWN_;
5822f7ca802SSteve Glendinning 	} else {
5832f7ca802SSteve Glendinning 		pdata->mac_cr &= ~MAC_CR_RCVOWN_;
5842f7ca802SSteve Glendinning 		pdata->mac_cr |= MAC_CR_FDPX_;
5852f7ca802SSteve Glendinning 	}
5862f7ca802SSteve Glendinning 	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
5872f7ca802SSteve Glendinning 
588769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
589e360a8b4SSteve Glendinning 	if (ret < 0)
590b052e073SSteve Glendinning 		return ret;
5912f7ca802SSteve Glendinning 
592769ea6d8SSteve Glendinning 	ret = smsc95xx_phy_update_flowcontrol(dev, ecmd.duplex, lcladv, rmtadv);
593b052e073SSteve Glendinning 	if (ret < 0)
594b052e073SSteve Glendinning 		netdev_warn(dev->net, "Error updating PHY flow control\n");
5952f7ca802SSteve Glendinning 
596b052e073SSteve Glendinning 	return ret;
5972f7ca802SSteve Glendinning }
5982f7ca802SSteve Glendinning 
5992f7ca802SSteve Glendinning static void smsc95xx_status(struct usbnet *dev, struct urb *urb)
6002f7ca802SSteve Glendinning {
6012f7ca802SSteve Glendinning 	u32 intdata;
6022f7ca802SSteve Glendinning 
6032f7ca802SSteve Glendinning 	if (urb->actual_length != 4) {
60460b86755SJoe Perches 		netdev_warn(dev->net, "unexpected urb length %d\n",
60560b86755SJoe Perches 			    urb->actual_length);
6062f7ca802SSteve Glendinning 		return;
6072f7ca802SSteve Glendinning 	}
6082f7ca802SSteve Glendinning 
6096809d216SBen Dooks 	intdata = get_unaligned_le32(urb->transfer_buffer);
610a475f603SJoe Perches 	netif_dbg(dev, link, dev->net, "intdata: 0x%08X\n", intdata);
6112f7ca802SSteve Glendinning 
6122f7ca802SSteve Glendinning 	if (intdata & INT_ENP_PHY_INT_)
6132f7ca802SSteve Glendinning 		usbnet_defer_kevent(dev, EVENT_LINK_RESET);
6142f7ca802SSteve Glendinning 	else
61560b86755SJoe Perches 		netdev_warn(dev->net, "unexpected interrupt, intdata=0x%08X\n",
61660b86755SJoe Perches 			    intdata);
6172f7ca802SSteve Glendinning }
6182f7ca802SSteve Glendinning 
619d69d1694SChristoph Fritz static void set_carrier(struct usbnet *dev, bool link)
620d69d1694SChristoph Fritz {
621d69d1694SChristoph Fritz 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
622d69d1694SChristoph Fritz 
623d69d1694SChristoph Fritz 	if (pdata->link_ok == link)
624d69d1694SChristoph Fritz 		return;
625d69d1694SChristoph Fritz 
626d69d1694SChristoph Fritz 	pdata->link_ok = link;
627d69d1694SChristoph Fritz 
628d69d1694SChristoph Fritz 	if (link)
629d69d1694SChristoph Fritz 		usbnet_link_change(dev, 1, 0);
630d69d1694SChristoph Fritz 	else
631d69d1694SChristoph Fritz 		usbnet_link_change(dev, 0, 0);
632d69d1694SChristoph Fritz }
633d69d1694SChristoph Fritz 
634d69d1694SChristoph Fritz static void check_carrier(struct work_struct *work)
635d69d1694SChristoph Fritz {
636d69d1694SChristoph Fritz 	struct smsc95xx_priv *pdata = container_of(work, struct smsc95xx_priv,
637d69d1694SChristoph Fritz 						carrier_check.work);
638d69d1694SChristoph Fritz 	struct usbnet *dev = pdata->dev;
639d69d1694SChristoph Fritz 	int ret;
640d69d1694SChristoph Fritz 
641d69d1694SChristoph Fritz 	if (pdata->suspend_flags != 0)
642d69d1694SChristoph Fritz 		return;
643d69d1694SChristoph Fritz 
644d69d1694SChristoph Fritz 	ret = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, MII_BMSR);
645d69d1694SChristoph Fritz 	if (ret < 0) {
646d69d1694SChristoph Fritz 		netdev_warn(dev->net, "Failed to read MII_BMSR\n");
647d69d1694SChristoph Fritz 		return;
648d69d1694SChristoph Fritz 	}
649d69d1694SChristoph Fritz 	if (ret & BMSR_LSTATUS)
650d69d1694SChristoph Fritz 		set_carrier(dev, 1);
651d69d1694SChristoph Fritz 	else
652d69d1694SChristoph Fritz 		set_carrier(dev, 0);
653d69d1694SChristoph Fritz 
654d69d1694SChristoph Fritz 	schedule_delayed_work(&pdata->carrier_check, CARRIER_CHECK_DELAY);
655d69d1694SChristoph Fritz }
656d69d1694SChristoph Fritz 
657f7b29271SSteve Glendinning /* Enable or disable Tx & Rx checksum offload engines */
658c8f44affSMichał Mirosław static int smsc95xx_set_features(struct net_device *netdev,
659c8f44affSMichał Mirosław 	netdev_features_t features)
6602f7ca802SSteve Glendinning {
66178e47fe4SMichał Mirosław 	struct usbnet *dev = netdev_priv(netdev);
6622f7ca802SSteve Glendinning 	u32 read_buf;
66378e47fe4SMichał Mirosław 	int ret;
66478e47fe4SMichał Mirosław 
66578e47fe4SMichał Mirosław 	ret = smsc95xx_read_reg(dev, COE_CR, &read_buf);
666e360a8b4SSteve Glendinning 	if (ret < 0)
667b052e073SSteve Glendinning 		return ret;
6682f7ca802SSteve Glendinning 
669fe0cd8caSNisar Sayed 	if (features & NETIF_F_IP_CSUM)
670f7b29271SSteve Glendinning 		read_buf |= Tx_COE_EN_;
671f7b29271SSteve Glendinning 	else
672f7b29271SSteve Glendinning 		read_buf &= ~Tx_COE_EN_;
673f7b29271SSteve Glendinning 
67478e47fe4SMichał Mirosław 	if (features & NETIF_F_RXCSUM)
6752f7ca802SSteve Glendinning 		read_buf |= Rx_COE_EN_;
6762f7ca802SSteve Glendinning 	else
6772f7ca802SSteve Glendinning 		read_buf &= ~Rx_COE_EN_;
6782f7ca802SSteve Glendinning 
6792f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, COE_CR, read_buf);
680e360a8b4SSteve Glendinning 	if (ret < 0)
681b052e073SSteve Glendinning 		return ret;
6822f7ca802SSteve Glendinning 
683a475f603SJoe Perches 	netif_dbg(dev, hw, dev->net, "COE_CR = 0x%08x\n", read_buf);
6842f7ca802SSteve Glendinning 	return 0;
6852f7ca802SSteve Glendinning }
6862f7ca802SSteve Glendinning 
6872f7ca802SSteve Glendinning static int smsc95xx_ethtool_get_eeprom_len(struct net_device *net)
6882f7ca802SSteve Glendinning {
6892f7ca802SSteve Glendinning 	return MAX_EEPROM_SIZE;
6902f7ca802SSteve Glendinning }
6912f7ca802SSteve Glendinning 
6922f7ca802SSteve Glendinning static int smsc95xx_ethtool_get_eeprom(struct net_device *netdev,
6932f7ca802SSteve Glendinning 				       struct ethtool_eeprom *ee, u8 *data)
6942f7ca802SSteve Glendinning {
6952f7ca802SSteve Glendinning 	struct usbnet *dev = netdev_priv(netdev);
6962f7ca802SSteve Glendinning 
6972f7ca802SSteve Glendinning 	ee->magic = LAN95XX_EEPROM_MAGIC;
6982f7ca802SSteve Glendinning 
6992f7ca802SSteve Glendinning 	return smsc95xx_read_eeprom(dev, ee->offset, ee->len, data);
7002f7ca802SSteve Glendinning }
7012f7ca802SSteve Glendinning 
7022f7ca802SSteve Glendinning static int smsc95xx_ethtool_set_eeprom(struct net_device *netdev,
7032f7ca802SSteve Glendinning 				       struct ethtool_eeprom *ee, u8 *data)
7042f7ca802SSteve Glendinning {
7052f7ca802SSteve Glendinning 	struct usbnet *dev = netdev_priv(netdev);
7062f7ca802SSteve Glendinning 
7072f7ca802SSteve Glendinning 	if (ee->magic != LAN95XX_EEPROM_MAGIC) {
70860b86755SJoe Perches 		netdev_warn(dev->net, "EEPROM: magic value mismatch, magic = 0x%x\n",
7092f7ca802SSteve Glendinning 			    ee->magic);
7102f7ca802SSteve Glendinning 		return -EINVAL;
7112f7ca802SSteve Glendinning 	}
7122f7ca802SSteve Glendinning 
7132f7ca802SSteve Glendinning 	return smsc95xx_write_eeprom(dev, ee->offset, ee->len, data);
7142f7ca802SSteve Glendinning }
7152f7ca802SSteve Glendinning 
7169fa32e94SEmeric Vigier static int smsc95xx_ethtool_getregslen(struct net_device *netdev)
7179fa32e94SEmeric Vigier {
7189fa32e94SEmeric Vigier 	/* all smsc95xx registers */
71996245317SSteve Glendinning 	return COE_CR - ID_REV + sizeof(u32);
7209fa32e94SEmeric Vigier }
7219fa32e94SEmeric Vigier 
7229fa32e94SEmeric Vigier static void
7239fa32e94SEmeric Vigier smsc95xx_ethtool_getregs(struct net_device *netdev, struct ethtool_regs *regs,
7249fa32e94SEmeric Vigier 			 void *buf)
7259fa32e94SEmeric Vigier {
7269fa32e94SEmeric Vigier 	struct usbnet *dev = netdev_priv(netdev);
727d348446bSDan Carpenter 	unsigned int i, j;
728d348446bSDan Carpenter 	int retval;
7299fa32e94SEmeric Vigier 	u32 *data = buf;
7309fa32e94SEmeric Vigier 
7319fa32e94SEmeric Vigier 	retval = smsc95xx_read_reg(dev, ID_REV, &regs->version);
7329fa32e94SEmeric Vigier 	if (retval < 0) {
7339fa32e94SEmeric Vigier 		netdev_warn(netdev, "REGS: cannot read ID_REV\n");
7349fa32e94SEmeric Vigier 		return;
7359fa32e94SEmeric Vigier 	}
7369fa32e94SEmeric Vigier 
7379fa32e94SEmeric Vigier 	for (i = ID_REV, j = 0; i <= COE_CR; i += (sizeof(u32)), j++) {
7389fa32e94SEmeric Vigier 		retval = smsc95xx_read_reg(dev, i, &data[j]);
7399fa32e94SEmeric Vigier 		if (retval < 0) {
7409fa32e94SEmeric Vigier 			netdev_warn(netdev, "REGS: cannot read reg[%x]\n", i);
7419fa32e94SEmeric Vigier 			return;
7429fa32e94SEmeric Vigier 		}
7439fa32e94SEmeric Vigier 	}
7449fa32e94SEmeric Vigier }
7459fa32e94SEmeric Vigier 
746e0e474a8SSteve Glendinning static void smsc95xx_ethtool_get_wol(struct net_device *net,
747e0e474a8SSteve Glendinning 				     struct ethtool_wolinfo *wolinfo)
748e0e474a8SSteve Glendinning {
749e0e474a8SSteve Glendinning 	struct usbnet *dev = netdev_priv(net);
750e0e474a8SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
751e0e474a8SSteve Glendinning 
752e0e474a8SSteve Glendinning 	wolinfo->supported = SUPPORTED_WAKE;
753e0e474a8SSteve Glendinning 	wolinfo->wolopts = pdata->wolopts;
754e0e474a8SSteve Glendinning }
755e0e474a8SSteve Glendinning 
756e0e474a8SSteve Glendinning static int smsc95xx_ethtool_set_wol(struct net_device *net,
757e0e474a8SSteve Glendinning 				    struct ethtool_wolinfo *wolinfo)
758e0e474a8SSteve Glendinning {
759e0e474a8SSteve Glendinning 	struct usbnet *dev = netdev_priv(net);
760e0e474a8SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
7613b14692cSSteve Glendinning 	int ret;
762e0e474a8SSteve Glendinning 
763c530c471SFlorian Fainelli 	if (wolinfo->wolopts & ~SUPPORTED_WAKE)
764c530c471SFlorian Fainelli 		return -EINVAL;
765c530c471SFlorian Fainelli 
766e0e474a8SSteve Glendinning 	pdata->wolopts = wolinfo->wolopts & SUPPORTED_WAKE;
7673b14692cSSteve Glendinning 
7683b14692cSSteve Glendinning 	ret = device_set_wakeup_enable(&dev->udev->dev, pdata->wolopts);
769b052e073SSteve Glendinning 	if (ret < 0)
770b052e073SSteve Glendinning 		netdev_warn(dev->net, "device_set_wakeup_enable error %d\n", ret);
7713b14692cSSteve Glendinning 
772b052e073SSteve Glendinning 	return ret;
773e0e474a8SSteve Glendinning }
774e0e474a8SSteve Glendinning 
77513722bbeSWoojung Huh static int get_mdix_status(struct net_device *net)
77613722bbeSWoojung Huh {
77713722bbeSWoojung Huh 	struct usbnet *dev = netdev_priv(net);
77813722bbeSWoojung Huh 	u32 val;
77913722bbeSWoojung Huh 	int buf;
78013722bbeSWoojung Huh 
78113722bbeSWoojung Huh 	buf = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, SPECIAL_CTRL_STS);
78213722bbeSWoojung Huh 	if (buf & SPECIAL_CTRL_STS_OVRRD_AMDIX_) {
78313722bbeSWoojung Huh 		if (buf & SPECIAL_CTRL_STS_AMDIX_ENABLE_)
78413722bbeSWoojung Huh 			return ETH_TP_MDI_AUTO;
78513722bbeSWoojung Huh 		else if (buf & SPECIAL_CTRL_STS_AMDIX_STATE_)
78613722bbeSWoojung Huh 			return ETH_TP_MDI_X;
78713722bbeSWoojung Huh 	} else {
78813722bbeSWoojung Huh 		buf = smsc95xx_read_reg(dev, STRAP_STATUS, &val);
78913722bbeSWoojung Huh 		if (val & STRAP_STATUS_AMDIX_EN_)
79013722bbeSWoojung Huh 			return ETH_TP_MDI_AUTO;
79113722bbeSWoojung Huh 	}
79213722bbeSWoojung Huh 
79313722bbeSWoojung Huh 	return ETH_TP_MDI;
79413722bbeSWoojung Huh }
79513722bbeSWoojung Huh 
79613722bbeSWoojung Huh static void set_mdix_status(struct net_device *net, __u8 mdix_ctrl)
79713722bbeSWoojung Huh {
79813722bbeSWoojung Huh 	struct usbnet *dev = netdev_priv(net);
79913722bbeSWoojung Huh 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
80013722bbeSWoojung Huh 	int buf;
80113722bbeSWoojung Huh 
80213722bbeSWoojung Huh 	if ((pdata->chip_id == ID_REV_CHIP_ID_9500A_) ||
80313722bbeSWoojung Huh 	    (pdata->chip_id == ID_REV_CHIP_ID_9530_) ||
80413722bbeSWoojung Huh 	    (pdata->chip_id == ID_REV_CHIP_ID_89530_) ||
80513722bbeSWoojung Huh 	    (pdata->chip_id == ID_REV_CHIP_ID_9730_)) {
80613722bbeSWoojung Huh 		/* Extend Manual AutoMDIX timer for 9500A/9500Ai */
80713722bbeSWoojung Huh 		buf = smsc95xx_mdio_read(dev->net, dev->mii.phy_id,
80813722bbeSWoojung Huh 					 PHY_EDPD_CONFIG);
80913722bbeSWoojung Huh 		buf |= PHY_EDPD_CONFIG_EXT_CROSSOVER_;
81013722bbeSWoojung Huh 		smsc95xx_mdio_write(dev->net, dev->mii.phy_id,
81113722bbeSWoojung Huh 				    PHY_EDPD_CONFIG, buf);
81213722bbeSWoojung Huh 	}
81313722bbeSWoojung Huh 
81413722bbeSWoojung Huh 	if (mdix_ctrl == ETH_TP_MDI) {
81513722bbeSWoojung Huh 		buf = smsc95xx_mdio_read(dev->net, dev->mii.phy_id,
81613722bbeSWoojung Huh 					 SPECIAL_CTRL_STS);
81713722bbeSWoojung Huh 		buf |= SPECIAL_CTRL_STS_OVRRD_AMDIX_;
81813722bbeSWoojung Huh 		buf &= ~(SPECIAL_CTRL_STS_AMDIX_ENABLE_ |
81913722bbeSWoojung Huh 			 SPECIAL_CTRL_STS_AMDIX_STATE_);
82013722bbeSWoojung Huh 		smsc95xx_mdio_write(dev->net, dev->mii.phy_id,
82113722bbeSWoojung Huh 				    SPECIAL_CTRL_STS, buf);
82213722bbeSWoojung Huh 	} else if (mdix_ctrl == ETH_TP_MDI_X) {
82313722bbeSWoojung Huh 		buf = smsc95xx_mdio_read(dev->net, dev->mii.phy_id,
82413722bbeSWoojung Huh 					 SPECIAL_CTRL_STS);
82513722bbeSWoojung Huh 		buf |= SPECIAL_CTRL_STS_OVRRD_AMDIX_;
82613722bbeSWoojung Huh 		buf &= ~(SPECIAL_CTRL_STS_AMDIX_ENABLE_ |
82713722bbeSWoojung Huh 			 SPECIAL_CTRL_STS_AMDIX_STATE_);
82813722bbeSWoojung Huh 		buf |= SPECIAL_CTRL_STS_AMDIX_STATE_;
82913722bbeSWoojung Huh 		smsc95xx_mdio_write(dev->net, dev->mii.phy_id,
83013722bbeSWoojung Huh 				    SPECIAL_CTRL_STS, buf);
83113722bbeSWoojung Huh 	} else if (mdix_ctrl == ETH_TP_MDI_AUTO) {
83213722bbeSWoojung Huh 		buf = smsc95xx_mdio_read(dev->net, dev->mii.phy_id,
83313722bbeSWoojung Huh 					 SPECIAL_CTRL_STS);
83413722bbeSWoojung Huh 		buf &= ~SPECIAL_CTRL_STS_OVRRD_AMDIX_;
83513722bbeSWoojung Huh 		buf &= ~(SPECIAL_CTRL_STS_AMDIX_ENABLE_ |
83613722bbeSWoojung Huh 			 SPECIAL_CTRL_STS_AMDIX_STATE_);
83713722bbeSWoojung Huh 		buf |= SPECIAL_CTRL_STS_AMDIX_ENABLE_;
83813722bbeSWoojung Huh 		smsc95xx_mdio_write(dev->net, dev->mii.phy_id,
83913722bbeSWoojung Huh 				    SPECIAL_CTRL_STS, buf);
84013722bbeSWoojung Huh 	}
84113722bbeSWoojung Huh 	pdata->mdix_ctrl = mdix_ctrl;
84213722bbeSWoojung Huh }
84313722bbeSWoojung Huh 
844eaf9a32aSPhilippe Reynes static int smsc95xx_get_link_ksettings(struct net_device *net,
845eaf9a32aSPhilippe Reynes 				       struct ethtool_link_ksettings *cmd)
84613722bbeSWoojung Huh {
84713722bbeSWoojung Huh 	struct usbnet *dev = netdev_priv(net);
84813722bbeSWoojung Huh 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
84913722bbeSWoojung Huh 	int retval;
85013722bbeSWoojung Huh 
851eaf9a32aSPhilippe Reynes 	retval = usbnet_get_link_ksettings(net, cmd);
85213722bbeSWoojung Huh 
853eaf9a32aSPhilippe Reynes 	cmd->base.eth_tp_mdix = pdata->mdix_ctrl;
854eaf9a32aSPhilippe Reynes 	cmd->base.eth_tp_mdix_ctrl = pdata->mdix_ctrl;
85513722bbeSWoojung Huh 
85613722bbeSWoojung Huh 	return retval;
85713722bbeSWoojung Huh }
85813722bbeSWoojung Huh 
859eaf9a32aSPhilippe Reynes static int smsc95xx_set_link_ksettings(struct net_device *net,
860eaf9a32aSPhilippe Reynes 				       const struct ethtool_link_ksettings *cmd)
86113722bbeSWoojung Huh {
86213722bbeSWoojung Huh 	struct usbnet *dev = netdev_priv(net);
86313722bbeSWoojung Huh 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
86413722bbeSWoojung Huh 	int retval;
86513722bbeSWoojung Huh 
866eaf9a32aSPhilippe Reynes 	if (pdata->mdix_ctrl != cmd->base.eth_tp_mdix_ctrl)
867eaf9a32aSPhilippe Reynes 		set_mdix_status(net, cmd->base.eth_tp_mdix_ctrl);
86813722bbeSWoojung Huh 
869eaf9a32aSPhilippe Reynes 	retval = usbnet_set_link_ksettings(net, cmd);
87013722bbeSWoojung Huh 
87113722bbeSWoojung Huh 	return retval;
87213722bbeSWoojung Huh }
87313722bbeSWoojung Huh 
8740fc0b732SStephen Hemminger static const struct ethtool_ops smsc95xx_ethtool_ops = {
8752f7ca802SSteve Glendinning 	.get_link	= usbnet_get_link,
8762f7ca802SSteve Glendinning 	.nway_reset	= usbnet_nway_reset,
8772f7ca802SSteve Glendinning 	.get_drvinfo	= usbnet_get_drvinfo,
8782f7ca802SSteve Glendinning 	.get_msglevel	= usbnet_get_msglevel,
8792f7ca802SSteve Glendinning 	.set_msglevel	= usbnet_set_msglevel,
8802f7ca802SSteve Glendinning 	.get_eeprom_len	= smsc95xx_ethtool_get_eeprom_len,
8812f7ca802SSteve Glendinning 	.get_eeprom	= smsc95xx_ethtool_get_eeprom,
8822f7ca802SSteve Glendinning 	.set_eeprom	= smsc95xx_ethtool_set_eeprom,
8839fa32e94SEmeric Vigier 	.get_regs_len	= smsc95xx_ethtool_getregslen,
8849fa32e94SEmeric Vigier 	.get_regs	= smsc95xx_ethtool_getregs,
885e0e474a8SSteve Glendinning 	.get_wol	= smsc95xx_ethtool_get_wol,
886e0e474a8SSteve Glendinning 	.set_wol	= smsc95xx_ethtool_set_wol,
887eaf9a32aSPhilippe Reynes 	.get_link_ksettings	= smsc95xx_get_link_ksettings,
888eaf9a32aSPhilippe Reynes 	.set_link_ksettings	= smsc95xx_set_link_ksettings,
889a8f5cb9eSPetr Kulhavy 	.get_ts_info	= ethtool_op_get_ts_info,
8902f7ca802SSteve Glendinning };
8912f7ca802SSteve Glendinning 
8922f7ca802SSteve Glendinning static int smsc95xx_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
8932f7ca802SSteve Glendinning {
8942f7ca802SSteve Glendinning 	struct usbnet *dev = netdev_priv(netdev);
8952f7ca802SSteve Glendinning 
8962f7ca802SSteve Glendinning 	if (!netif_running(netdev))
8972f7ca802SSteve Glendinning 		return -EINVAL;
8982f7ca802SSteve Glendinning 
8992f7ca802SSteve Glendinning 	return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
9002f7ca802SSteve Glendinning }
9012f7ca802SSteve Glendinning 
9022f7ca802SSteve Glendinning static void smsc95xx_init_mac_address(struct usbnet *dev)
9032f7ca802SSteve Glendinning {
904c489565bSArnd Bergmann 	const u8 *mac_addr;
905c489565bSArnd Bergmann 
906c489565bSArnd Bergmann 	/* maybe the boot loader passed the MAC address in devicetree */
907c489565bSArnd Bergmann 	mac_addr = of_get_mac_address(dev->udev->dev.of_node);
908adfb3cb2SPetr Štetiar 	if (!IS_ERR(mac_addr)) {
9095503a688SPetr Štetiar 		ether_addr_copy(dev->net->dev_addr, mac_addr);
910c489565bSArnd Bergmann 		return;
911c489565bSArnd Bergmann 	}
912c489565bSArnd Bergmann 
9132f7ca802SSteve Glendinning 	/* try reading mac address from EEPROM */
9142f7ca802SSteve Glendinning 	if (smsc95xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN,
9152f7ca802SSteve Glendinning 			dev->net->dev_addr) == 0) {
9162f7ca802SSteve Glendinning 		if (is_valid_ether_addr(dev->net->dev_addr)) {
9172f7ca802SSteve Glendinning 			/* eeprom values are valid so use them */
918a475f603SJoe Perches 			netif_dbg(dev, ifup, dev->net, "MAC address read from EEPROM\n");
9192f7ca802SSteve Glendinning 			return;
9202f7ca802SSteve Glendinning 		}
9212f7ca802SSteve Glendinning 	}
9222f7ca802SSteve Glendinning 
923c489565bSArnd Bergmann 	/* no useful static MAC address found. generate a random one */
924f2cedb63SDanny Kukawka 	eth_hw_addr_random(dev->net);
925c7e12eadSJoe Perches 	netif_dbg(dev, ifup, dev->net, "MAC address set to eth_random_addr\n");
9262f7ca802SSteve Glendinning }
9272f7ca802SSteve Glendinning 
9282f7ca802SSteve Glendinning static int smsc95xx_set_mac_address(struct usbnet *dev)
9292f7ca802SSteve Glendinning {
9302f7ca802SSteve Glendinning 	u32 addr_lo = dev->net->dev_addr[0] | dev->net->dev_addr[1] << 8 |
9312f7ca802SSteve Glendinning 		dev->net->dev_addr[2] << 16 | dev->net->dev_addr[3] << 24;
9322f7ca802SSteve Glendinning 	u32 addr_hi = dev->net->dev_addr[4] | dev->net->dev_addr[5] << 8;
9332f7ca802SSteve Glendinning 	int ret;
9342f7ca802SSteve Glendinning 
9352f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, ADDRL, addr_lo);
936b052e073SSteve Glendinning 	if (ret < 0)
937b052e073SSteve Glendinning 		return ret;
938e360a8b4SSteve Glendinning 
939e360a8b4SSteve Glendinning 	return smsc95xx_write_reg(dev, ADDRH, addr_hi);
9402f7ca802SSteve Glendinning }
9412f7ca802SSteve Glendinning 
9422f7ca802SSteve Glendinning /* starts the TX path */
943769ea6d8SSteve Glendinning static int smsc95xx_start_tx_path(struct usbnet *dev)
9442f7ca802SSteve Glendinning {
9452f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
9462f7ca802SSteve Glendinning 	unsigned long flags;
947769ea6d8SSteve Glendinning 	int ret;
9482f7ca802SSteve Glendinning 
9492f7ca802SSteve Glendinning 	/* Enable Tx at MAC */
9502f7ca802SSteve Glendinning 	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
9512f7ca802SSteve Glendinning 	pdata->mac_cr |= MAC_CR_TXEN_;
9522f7ca802SSteve Glendinning 	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
9532f7ca802SSteve Glendinning 
954769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
955e360a8b4SSteve Glendinning 	if (ret < 0)
956b052e073SSteve Glendinning 		return ret;
9572f7ca802SSteve Glendinning 
9582f7ca802SSteve Glendinning 	/* Enable Tx at SCSRs */
959e360a8b4SSteve Glendinning 	return smsc95xx_write_reg(dev, TX_CFG, TX_CFG_ON_);
9602f7ca802SSteve Glendinning }
9612f7ca802SSteve Glendinning 
9622f7ca802SSteve Glendinning /* Starts the Receive path */
963ec32115dSMing Lei static int smsc95xx_start_rx_path(struct usbnet *dev, int in_pm)
9642f7ca802SSteve Glendinning {
9652f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
9662f7ca802SSteve Glendinning 	unsigned long flags;
9672f7ca802SSteve Glendinning 
9682f7ca802SSteve Glendinning 	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
9692f7ca802SSteve Glendinning 	pdata->mac_cr |= MAC_CR_RXEN_;
9702f7ca802SSteve Glendinning 	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
9712f7ca802SSteve Glendinning 
972e360a8b4SSteve Glendinning 	return __smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr, in_pm);
9732f7ca802SSteve Glendinning }
9742f7ca802SSteve Glendinning 
9752f7ca802SSteve Glendinning static int smsc95xx_phy_initialize(struct usbnet *dev)
9762f7ca802SSteve Glendinning {
977769ea6d8SSteve Glendinning 	int bmcr, ret, timeout = 0;
978db443c44SSteve Glendinning 
9792f7ca802SSteve Glendinning 	/* Initialize MII structure */
9802f7ca802SSteve Glendinning 	dev->mii.dev = dev->net;
9812f7ca802SSteve Glendinning 	dev->mii.mdio_read = smsc95xx_mdio_read;
9822f7ca802SSteve Glendinning 	dev->mii.mdio_write = smsc95xx_mdio_write;
9832f7ca802SSteve Glendinning 	dev->mii.phy_id_mask = 0x1f;
9842f7ca802SSteve Glendinning 	dev->mii.reg_num_mask = 0x1f;
9852f7ca802SSteve Glendinning 	dev->mii.phy_id = SMSC95XX_INTERNAL_PHY_ID;
9862f7ca802SSteve Glendinning 
987db443c44SSteve Glendinning 	/* reset phy and wait for reset to complete */
9882f7ca802SSteve Glendinning 	smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
989db443c44SSteve Glendinning 
990db443c44SSteve Glendinning 	do {
991db443c44SSteve Glendinning 		msleep(10);
992db443c44SSteve Glendinning 		bmcr = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, MII_BMCR);
993db443c44SSteve Glendinning 		timeout++;
994d9460920SRabin Vincent 	} while ((bmcr & BMCR_RESET) && (timeout < 100));
995db443c44SSteve Glendinning 
996db443c44SSteve Glendinning 	if (timeout >= 100) {
997db443c44SSteve Glendinning 		netdev_warn(dev->net, "timeout on PHY Reset");
998db443c44SSteve Glendinning 		return -EIO;
999db443c44SSteve Glendinning 	}
1000db443c44SSteve Glendinning 
10012f7ca802SSteve Glendinning 	smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
10022f7ca802SSteve Glendinning 		ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP |
10032f7ca802SSteve Glendinning 		ADVERTISE_PAUSE_ASYM);
10042f7ca802SSteve Glendinning 
10052f7ca802SSteve Glendinning 	/* read to clear */
1006769ea6d8SSteve Glendinning 	ret = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, PHY_INT_SRC);
1007b052e073SSteve Glendinning 	if (ret < 0) {
1008b052e073SSteve Glendinning 		netdev_warn(dev->net, "Failed to read PHY_INT_SRC during init\n");
1009b052e073SSteve Glendinning 		return ret;
1010b052e073SSteve Glendinning 	}
10112f7ca802SSteve Glendinning 
10122f7ca802SSteve Glendinning 	smsc95xx_mdio_write(dev->net, dev->mii.phy_id, PHY_INT_MASK,
10132f7ca802SSteve Glendinning 		PHY_INT_MASK_DEFAULT_);
10142f7ca802SSteve Glendinning 	mii_nway_restart(&dev->mii);
10152f7ca802SSteve Glendinning 
1016a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net, "phy initialised successfully\n");
10172f7ca802SSteve Glendinning 	return 0;
10182f7ca802SSteve Glendinning }
10192f7ca802SSteve Glendinning 
10202f7ca802SSteve Glendinning static int smsc95xx_reset(struct usbnet *dev)
10212f7ca802SSteve Glendinning {
10222f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
10232f7ca802SSteve Glendinning 	u32 read_buf, write_buf, burst_cap;
10242f7ca802SSteve Glendinning 	int ret = 0, timeout;
10252f7ca802SSteve Glendinning 
1026a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net, "entering smsc95xx_reset\n");
10272f7ca802SSteve Glendinning 
10284436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, HW_CFG, HW_CFG_LRST_);
1029e360a8b4SSteve Glendinning 	if (ret < 0)
1030b052e073SSteve Glendinning 		return ret;
10312f7ca802SSteve Glendinning 
10322f7ca802SSteve Glendinning 	timeout = 0;
10332f7ca802SSteve Glendinning 	do {
1034cf2acec2SSteve Glendinning 		msleep(10);
10352f7ca802SSteve Glendinning 		ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
1036e360a8b4SSteve Glendinning 		if (ret < 0)
1037b052e073SSteve Glendinning 			return ret;
10382f7ca802SSteve Glendinning 		timeout++;
10392f7ca802SSteve Glendinning 	} while ((read_buf & HW_CFG_LRST_) && (timeout < 100));
10402f7ca802SSteve Glendinning 
10412f7ca802SSteve Glendinning 	if (timeout >= 100) {
104260b86755SJoe Perches 		netdev_warn(dev->net, "timeout waiting for completion of Lite Reset\n");
10432f7ca802SSteve Glendinning 		return ret;
10442f7ca802SSteve Glendinning 	}
10452f7ca802SSteve Glendinning 
10464436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, PM_CTRL, PM_CTL_PHY_RST_);
1047e360a8b4SSteve Glendinning 	if (ret < 0)
1048b052e073SSteve Glendinning 		return ret;
10492f7ca802SSteve Glendinning 
10502f7ca802SSteve Glendinning 	timeout = 0;
10512f7ca802SSteve Glendinning 	do {
1052cf2acec2SSteve Glendinning 		msleep(10);
10532f7ca802SSteve Glendinning 		ret = smsc95xx_read_reg(dev, PM_CTRL, &read_buf);
1054e360a8b4SSteve Glendinning 		if (ret < 0)
1055b052e073SSteve Glendinning 			return ret;
10562f7ca802SSteve Glendinning 		timeout++;
10572f7ca802SSteve Glendinning 	} while ((read_buf & PM_CTL_PHY_RST_) && (timeout < 100));
10582f7ca802SSteve Glendinning 
10592f7ca802SSteve Glendinning 	if (timeout >= 100) {
106060b86755SJoe Perches 		netdev_warn(dev->net, "timeout waiting for PHY Reset\n");
10612f7ca802SSteve Glendinning 		return ret;
10622f7ca802SSteve Glendinning 	}
10632f7ca802SSteve Glendinning 
10642f7ca802SSteve Glendinning 	ret = smsc95xx_set_mac_address(dev);
10652f7ca802SSteve Glendinning 	if (ret < 0)
10662f7ca802SSteve Glendinning 		return ret;
10672f7ca802SSteve Glendinning 
10681e1d7412SJoe Perches 	netif_dbg(dev, ifup, dev->net, "MAC Address: %pM\n",
10691e1d7412SJoe Perches 		  dev->net->dev_addr);
10702f7ca802SSteve Glendinning 
10712f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
1072e360a8b4SSteve Glendinning 	if (ret < 0)
1073b052e073SSteve Glendinning 		return ret;
10742f7ca802SSteve Glendinning 
10751e1d7412SJoe Perches 	netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG : 0x%08x\n",
10761e1d7412SJoe Perches 		  read_buf);
10772f7ca802SSteve Glendinning 
10782f7ca802SSteve Glendinning 	read_buf |= HW_CFG_BIR_;
10792f7ca802SSteve Glendinning 
10802f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
1081e360a8b4SSteve Glendinning 	if (ret < 0)
1082b052e073SSteve Glendinning 		return ret;
10832f7ca802SSteve Glendinning 
10842f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
1085e360a8b4SSteve Glendinning 	if (ret < 0)
1086b052e073SSteve Glendinning 		return ret;
1087b052e073SSteve Glendinning 
1088a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net,
1089a475f603SJoe Perches 		  "Read Value from HW_CFG after writing HW_CFG_BIR_: 0x%08x\n",
109060b86755SJoe Perches 		  read_buf);
10912f7ca802SSteve Glendinning 
10922f7ca802SSteve Glendinning 	if (!turbo_mode) {
10932f7ca802SSteve Glendinning 		burst_cap = 0;
10942f7ca802SSteve Glendinning 		dev->rx_urb_size = MAX_SINGLE_PACKET_SIZE;
10952f7ca802SSteve Glendinning 	} else if (dev->udev->speed == USB_SPEED_HIGH) {
10962f7ca802SSteve Glendinning 		burst_cap = DEFAULT_HS_BURST_CAP_SIZE / HS_USB_PKT_SIZE;
10972f7ca802SSteve Glendinning 		dev->rx_urb_size = DEFAULT_HS_BURST_CAP_SIZE;
10982f7ca802SSteve Glendinning 	} else {
10992f7ca802SSteve Glendinning 		burst_cap = DEFAULT_FS_BURST_CAP_SIZE / FS_USB_PKT_SIZE;
11002f7ca802SSteve Glendinning 		dev->rx_urb_size = DEFAULT_FS_BURST_CAP_SIZE;
11012f7ca802SSteve Glendinning 	}
11022f7ca802SSteve Glendinning 
11031e1d7412SJoe Perches 	netif_dbg(dev, ifup, dev->net, "rx_urb_size=%ld\n",
11041e1d7412SJoe Perches 		  (ulong)dev->rx_urb_size);
11052f7ca802SSteve Glendinning 
11062f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, BURST_CAP, burst_cap);
1107e360a8b4SSteve Glendinning 	if (ret < 0)
1108b052e073SSteve Glendinning 		return ret;
11092f7ca802SSteve Glendinning 
11102f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, BURST_CAP, &read_buf);
1111e360a8b4SSteve Glendinning 	if (ret < 0)
1112b052e073SSteve Glendinning 		return ret;
1113769ea6d8SSteve Glendinning 
1114a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net,
1115a475f603SJoe Perches 		  "Read Value from BURST_CAP after writing: 0x%08x\n",
11162f7ca802SSteve Glendinning 		  read_buf);
11172f7ca802SSteve Glendinning 
11184436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, BULK_IN_DLY, DEFAULT_BULK_IN_DELAY);
1119e360a8b4SSteve Glendinning 	if (ret < 0)
1120b052e073SSteve Glendinning 		return ret;
11212f7ca802SSteve Glendinning 
11222f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, BULK_IN_DLY, &read_buf);
1123e360a8b4SSteve Glendinning 	if (ret < 0)
1124b052e073SSteve Glendinning 		return ret;
1125769ea6d8SSteve Glendinning 
1126a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net,
1127a475f603SJoe Perches 		  "Read Value from BULK_IN_DLY after writing: 0x%08x\n",
112860b86755SJoe Perches 		  read_buf);
11292f7ca802SSteve Glendinning 
11302f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
1131e360a8b4SSteve Glendinning 	if (ret < 0)
1132b052e073SSteve Glendinning 		return ret;
1133769ea6d8SSteve Glendinning 
11341e1d7412SJoe Perches 	netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG: 0x%08x\n",
11351e1d7412SJoe Perches 		  read_buf);
11362f7ca802SSteve Glendinning 
11372f7ca802SSteve Glendinning 	if (turbo_mode)
11382f7ca802SSteve Glendinning 		read_buf |= (HW_CFG_MEF_ | HW_CFG_BCE_);
11392f7ca802SSteve Glendinning 
11402f7ca802SSteve Glendinning 	read_buf &= ~HW_CFG_RXDOFF_;
11412f7ca802SSteve Glendinning 
11422f7ca802SSteve Glendinning 	/* set Rx data offset=2, Make IP header aligns on word boundary. */
11432f7ca802SSteve Glendinning 	read_buf |= NET_IP_ALIGN << 9;
11442f7ca802SSteve Glendinning 
11452f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
1146e360a8b4SSteve Glendinning 	if (ret < 0)
1147b052e073SSteve Glendinning 		return ret;
11482f7ca802SSteve Glendinning 
11492f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
1150e360a8b4SSteve Glendinning 	if (ret < 0)
1151b052e073SSteve Glendinning 		return ret;
1152769ea6d8SSteve Glendinning 
1153a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net,
1154a475f603SJoe Perches 		  "Read Value from HW_CFG after writing: 0x%08x\n", read_buf);
11552f7ca802SSteve Glendinning 
11564436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
1157e360a8b4SSteve Glendinning 	if (ret < 0)
1158b052e073SSteve Glendinning 		return ret;
11592f7ca802SSteve Glendinning 
11602f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, ID_REV, &read_buf);
1161e360a8b4SSteve Glendinning 	if (ret < 0)
1162b052e073SSteve Glendinning 		return ret;
1163a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net, "ID_REV = 0x%08x\n", read_buf);
11642f7ca802SSteve Glendinning 
1165f293501cSSteve Glendinning 	/* Configure GPIO pins as LED outputs */
1166f293501cSSteve Glendinning 	write_buf = LED_GPIO_CFG_SPD_LED | LED_GPIO_CFG_LNK_LED |
1167f293501cSSteve Glendinning 		LED_GPIO_CFG_FDX_LED;
1168f293501cSSteve Glendinning 	ret = smsc95xx_write_reg(dev, LED_GPIO_CFG, write_buf);
1169e360a8b4SSteve Glendinning 	if (ret < 0)
1170b052e073SSteve Glendinning 		return ret;
1171f293501cSSteve Glendinning 
11722f7ca802SSteve Glendinning 	/* Init Tx */
11734436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, FLOW, 0);
1174e360a8b4SSteve Glendinning 	if (ret < 0)
1175b052e073SSteve Glendinning 		return ret;
11762f7ca802SSteve Glendinning 
11774436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, AFC_CFG, AFC_CFG_DEFAULT);
1178e360a8b4SSteve Glendinning 	if (ret < 0)
1179b052e073SSteve Glendinning 		return ret;
11802f7ca802SSteve Glendinning 
11812f7ca802SSteve Glendinning 	/* Don't need mac_cr_lock during initialisation */
11822f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, MAC_CR, &pdata->mac_cr);
1183e360a8b4SSteve Glendinning 	if (ret < 0)
1184b052e073SSteve Glendinning 		return ret;
11852f7ca802SSteve Glendinning 
11862f7ca802SSteve Glendinning 	/* Init Rx */
11872f7ca802SSteve Glendinning 	/* Set Vlan */
11884436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, VLAN1, (u32)ETH_P_8021Q);
1189e360a8b4SSteve Glendinning 	if (ret < 0)
1190b052e073SSteve Glendinning 		return ret;
11912f7ca802SSteve Glendinning 
1192f7b29271SSteve Glendinning 	/* Enable or disable checksum offload engines */
1193769ea6d8SSteve Glendinning 	ret = smsc95xx_set_features(dev->net, dev->net->features);
1194b052e073SSteve Glendinning 	if (ret < 0) {
1195b052e073SSteve Glendinning 		netdev_warn(dev->net, "Failed to set checksum offload features\n");
1196b052e073SSteve Glendinning 		return ret;
1197b052e073SSteve Glendinning 	}
11982f7ca802SSteve Glendinning 
11992f7ca802SSteve Glendinning 	smsc95xx_set_multicast(dev->net);
12002f7ca802SSteve Glendinning 
1201769ea6d8SSteve Glendinning 	ret = smsc95xx_phy_initialize(dev);
1202b052e073SSteve Glendinning 	if (ret < 0) {
1203b052e073SSteve Glendinning 		netdev_warn(dev->net, "Failed to init PHY\n");
1204b052e073SSteve Glendinning 		return ret;
1205b052e073SSteve Glendinning 	}
12062f7ca802SSteve Glendinning 
12072f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, INT_EP_CTL, &read_buf);
1208e360a8b4SSteve Glendinning 	if (ret < 0)
1209b052e073SSteve Glendinning 		return ret;
12102f7ca802SSteve Glendinning 
12112f7ca802SSteve Glendinning 	/* enable PHY interrupts */
12122f7ca802SSteve Glendinning 	read_buf |= INT_EP_CTL_PHY_INT_;
12132f7ca802SSteve Glendinning 
12142f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, INT_EP_CTL, read_buf);
1215e360a8b4SSteve Glendinning 	if (ret < 0)
1216b052e073SSteve Glendinning 		return ret;
12172f7ca802SSteve Glendinning 
1218769ea6d8SSteve Glendinning 	ret = smsc95xx_start_tx_path(dev);
1219b052e073SSteve Glendinning 	if (ret < 0) {
1220b052e073SSteve Glendinning 		netdev_warn(dev->net, "Failed to start TX path\n");
1221b052e073SSteve Glendinning 		return ret;
1222b052e073SSteve Glendinning 	}
1223769ea6d8SSteve Glendinning 
1224ec32115dSMing Lei 	ret = smsc95xx_start_rx_path(dev, 0);
1225b052e073SSteve Glendinning 	if (ret < 0) {
1226b052e073SSteve Glendinning 		netdev_warn(dev->net, "Failed to start RX path\n");
1227b052e073SSteve Glendinning 		return ret;
1228b052e073SSteve Glendinning 	}
12292f7ca802SSteve Glendinning 
1230a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net, "smsc95xx_reset, return 0\n");
12312f7ca802SSteve Glendinning 	return 0;
12322f7ca802SSteve Glendinning }
12332f7ca802SSteve Glendinning 
123463e77b39SStephen Hemminger static const struct net_device_ops smsc95xx_netdev_ops = {
123563e77b39SStephen Hemminger 	.ndo_open		= usbnet_open,
123663e77b39SStephen Hemminger 	.ndo_stop		= usbnet_stop,
123763e77b39SStephen Hemminger 	.ndo_start_xmit		= usbnet_start_xmit,
123863e77b39SStephen Hemminger 	.ndo_tx_timeout		= usbnet_tx_timeout,
123963e77b39SStephen Hemminger 	.ndo_change_mtu		= usbnet_change_mtu,
1240c8b5d129SGreg Ungerer 	.ndo_get_stats64	= usbnet_get_stats64,
124163e77b39SStephen Hemminger 	.ndo_set_mac_address 	= eth_mac_addr,
124263e77b39SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
124363e77b39SStephen Hemminger 	.ndo_do_ioctl 		= smsc95xx_ioctl,
1244afc4b13dSJiri Pirko 	.ndo_set_rx_mode	= smsc95xx_set_multicast,
124578e47fe4SMichał Mirosław 	.ndo_set_features	= smsc95xx_set_features,
124663e77b39SStephen Hemminger };
124763e77b39SStephen Hemminger 
12482f7ca802SSteve Glendinning static int smsc95xx_bind(struct usbnet *dev, struct usb_interface *intf)
12492f7ca802SSteve Glendinning {
12502f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = NULL;
1251bbd9f9eeSSteve Glendinning 	u32 val;
12522f7ca802SSteve Glendinning 	int ret;
12532f7ca802SSteve Glendinning 
12542f7ca802SSteve Glendinning 	printk(KERN_INFO SMSC_CHIPNAME " v" SMSC_DRIVER_VERSION "\n");
12552f7ca802SSteve Glendinning 
12562f7ca802SSteve Glendinning 	ret = usbnet_get_endpoints(dev, intf);
1257b052e073SSteve Glendinning 	if (ret < 0) {
1258b052e073SSteve Glendinning 		netdev_warn(dev->net, "usbnet_get_endpoints failed: %d\n", ret);
1259b052e073SSteve Glendinning 		return ret;
1260b052e073SSteve Glendinning 	}
12612f7ca802SSteve Glendinning 
12622f7ca802SSteve Glendinning 	dev->data[0] = (unsigned long)kzalloc(sizeof(struct smsc95xx_priv),
12632f7ca802SSteve Glendinning 					      GFP_KERNEL);
12642f7ca802SSteve Glendinning 
12652f7ca802SSteve Glendinning 	pdata = (struct smsc95xx_priv *)(dev->data[0]);
126638673c82SJoe Perches 	if (!pdata)
12672f7ca802SSteve Glendinning 		return -ENOMEM;
12682f7ca802SSteve Glendinning 
12692f7ca802SSteve Glendinning 	spin_lock_init(&pdata->mac_cr_lock);
12702f7ca802SSteve Glendinning 
1271fe0cd8caSNisar Sayed 	/* LAN95xx devices do not alter the computed checksum of 0 to 0xffff.
1272fe0cd8caSNisar Sayed 	 * RFC 2460, ipv6 UDP calculated checksum yields a result of zero must
1273fe0cd8caSNisar Sayed 	 * be changed to 0xffff. RFC 768, ipv4 UDP computed checksum is zero,
1274fe0cd8caSNisar Sayed 	 * it is transmitted as all ones. The zero transmitted checksum means
1275fe0cd8caSNisar Sayed 	 * transmitter generated no checksum. Hence, enable csum offload only
1276fe0cd8caSNisar Sayed 	 * for ipv4 packets.
1277fe0cd8caSNisar Sayed 	 */
127878e47fe4SMichał Mirosław 	if (DEFAULT_TX_CSUM_ENABLE)
1279fe0cd8caSNisar Sayed 		dev->net->features |= NETIF_F_IP_CSUM;
128078e47fe4SMichał Mirosław 	if (DEFAULT_RX_CSUM_ENABLE)
128178e47fe4SMichał Mirosław 		dev->net->features |= NETIF_F_RXCSUM;
128278e47fe4SMichał Mirosław 
1283fe0cd8caSNisar Sayed 	dev->net->hw_features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
1284810eeb1fSBen Dooks 	set_bit(EVENT_NO_IP_ALIGN, &dev->flags);
12852f7ca802SSteve Glendinning 
1286f4e8ab7cSBernard Blackham 	smsc95xx_init_mac_address(dev);
1287f4e8ab7cSBernard Blackham 
12882f7ca802SSteve Glendinning 	/* Init all registers */
12892f7ca802SSteve Glendinning 	ret = smsc95xx_reset(dev);
12902f7ca802SSteve Glendinning 
1291bbd9f9eeSSteve Glendinning 	/* detect device revision as different features may be available */
1292bbd9f9eeSSteve Glendinning 	ret = smsc95xx_read_reg(dev, ID_REV, &val);
1293e360a8b4SSteve Glendinning 	if (ret < 0)
1294b052e073SSteve Glendinning 		return ret;
1295bbd9f9eeSSteve Glendinning 	val >>= 16;
129613722bbeSWoojung Huh 	pdata->chip_id = val;
129713722bbeSWoojung Huh 	pdata->mdix_ctrl = get_mdix_status(dev->net);
12989ebca507SSteve Glendinning 
12999ebca507SSteve Glendinning 	if ((val == ID_REV_CHIP_ID_9500A_) || (val == ID_REV_CHIP_ID_9530_) ||
13009ebca507SSteve Glendinning 	    (val == ID_REV_CHIP_ID_89530_) || (val == ID_REV_CHIP_ID_9730_))
13019ebca507SSteve Glendinning 		pdata->features = (FEATURE_8_WAKEUP_FILTERS |
13029ebca507SSteve Glendinning 			FEATURE_PHY_NLP_CROSSOVER |
1303eb970ff0SMing Lei 			FEATURE_REMOTE_WAKEUP);
13049ebca507SSteve Glendinning 	else if (val == ID_REV_CHIP_ID_9512_)
13059ebca507SSteve Glendinning 		pdata->features = FEATURE_8_WAKEUP_FILTERS;
1306bbd9f9eeSSteve Glendinning 
130763e77b39SStephen Hemminger 	dev->net->netdev_ops = &smsc95xx_netdev_ops;
13082f7ca802SSteve Glendinning 	dev->net->ethtool_ops = &smsc95xx_ethtool_ops;
13092f7ca802SSteve Glendinning 	dev->net->flags |= IFF_MULTICAST;
131078e47fe4SMichał Mirosław 	dev->net->hard_header_len += SMSC95XX_TX_OVERHEAD_CSUM;
131185b18b02SStefan Wahren 	dev->net->min_mtu = ETH_MIN_MTU;
131285b18b02SStefan Wahren 	dev->net->max_mtu = ETH_DATA_LEN;
13139bbf5660SStephane Fillod 	dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
1314d69d1694SChristoph Fritz 
1315d69d1694SChristoph Fritz 	pdata->dev = dev;
1316d69d1694SChristoph Fritz 	INIT_DELAYED_WORK(&pdata->carrier_check, check_carrier);
1317d69d1694SChristoph Fritz 	schedule_delayed_work(&pdata->carrier_check, CARRIER_CHECK_DELAY);
1318d69d1694SChristoph Fritz 
13192f7ca802SSteve Glendinning 	return 0;
13202f7ca802SSteve Glendinning }
13212f7ca802SSteve Glendinning 
13222f7ca802SSteve Glendinning static void smsc95xx_unbind(struct usbnet *dev, struct usb_interface *intf)
13232f7ca802SSteve Glendinning {
13242f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1325d69d1694SChristoph Fritz 
13262f7ca802SSteve Glendinning 	if (pdata) {
1327d69d1694SChristoph Fritz 		cancel_delayed_work(&pdata->carrier_check);
1328a475f603SJoe Perches 		netif_dbg(dev, ifdown, dev->net, "free pdata\n");
13292f7ca802SSteve Glendinning 		kfree(pdata);
13302f7ca802SSteve Glendinning 		pdata = NULL;
13312f7ca802SSteve Glendinning 		dev->data[0] = 0;
13322f7ca802SSteve Glendinning 	}
13332f7ca802SSteve Glendinning }
13342f7ca802SSteve Glendinning 
1335068bb1a7SSteve Glendinning static u32 smsc_crc(const u8 *buffer, size_t len, int filter)
1336bbd9f9eeSSteve Glendinning {
1337068bb1a7SSteve Glendinning 	u32 crc = bitrev16(crc16(0xFFFF, buffer, len));
1338068bb1a7SSteve Glendinning 	return crc << ((filter % 2) * 16);
1339bbd9f9eeSSteve Glendinning }
1340bbd9f9eeSSteve Glendinning 
1341e5e3af83SSteve Glendinning static int smsc95xx_enable_phy_wakeup_interrupts(struct usbnet *dev, u16 mask)
1342e5e3af83SSteve Glendinning {
1343e5e3af83SSteve Glendinning 	struct mii_if_info *mii = &dev->mii;
1344e5e3af83SSteve Glendinning 	int ret;
1345e5e3af83SSteve Glendinning 
13461e1d7412SJoe Perches 	netdev_dbg(dev->net, "enabling PHY wakeup interrupts\n");
1347e5e3af83SSteve Glendinning 
1348e5e3af83SSteve Glendinning 	/* read to clear */
1349e5e3af83SSteve Glendinning 	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_INT_SRC);
1350e360a8b4SSteve Glendinning 	if (ret < 0)
1351b052e073SSteve Glendinning 		return ret;
1352e5e3af83SSteve Glendinning 
1353e5e3af83SSteve Glendinning 	/* enable interrupt source */
1354e5e3af83SSteve Glendinning 	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_INT_MASK);
1355e360a8b4SSteve Glendinning 	if (ret < 0)
1356b052e073SSteve Glendinning 		return ret;
1357e5e3af83SSteve Glendinning 
1358e5e3af83SSteve Glendinning 	ret |= mask;
1359e5e3af83SSteve Glendinning 
1360e5e3af83SSteve Glendinning 	smsc95xx_mdio_write_nopm(dev->net, mii->phy_id, PHY_INT_MASK, ret);
1361e5e3af83SSteve Glendinning 
1362e5e3af83SSteve Glendinning 	return 0;
1363e5e3af83SSteve Glendinning }
1364e5e3af83SSteve Glendinning 
1365e5e3af83SSteve Glendinning static int smsc95xx_link_ok_nopm(struct usbnet *dev)
1366e5e3af83SSteve Glendinning {
1367e5e3af83SSteve Glendinning 	struct mii_if_info *mii = &dev->mii;
1368e5e3af83SSteve Glendinning 	int ret;
1369e5e3af83SSteve Glendinning 
1370e5e3af83SSteve Glendinning 	/* first, a dummy read, needed to latch some MII phys */
1371e5e3af83SSteve Glendinning 	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, MII_BMSR);
1372e360a8b4SSteve Glendinning 	if (ret < 0)
1373b052e073SSteve Glendinning 		return ret;
1374e5e3af83SSteve Glendinning 
1375e5e3af83SSteve Glendinning 	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, MII_BMSR);
1376e360a8b4SSteve Glendinning 	if (ret < 0)
1377b052e073SSteve Glendinning 		return ret;
1378e5e3af83SSteve Glendinning 
1379e5e3af83SSteve Glendinning 	return !!(ret & BMSR_LSTATUS);
1380e5e3af83SSteve Glendinning }
1381e5e3af83SSteve Glendinning 
1382319b95b5SSteve Glendinning static int smsc95xx_enter_suspend0(struct usbnet *dev)
1383319b95b5SSteve Glendinning {
1384319b95b5SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1385319b95b5SSteve Glendinning 	u32 val;
1386319b95b5SSteve Glendinning 	int ret;
1387319b95b5SSteve Glendinning 
1388319b95b5SSteve Glendinning 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1389e360a8b4SSteve Glendinning 	if (ret < 0)
1390b052e073SSteve Glendinning 		return ret;
1391319b95b5SSteve Glendinning 
1392319b95b5SSteve Glendinning 	val &= (~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_));
1393319b95b5SSteve Glendinning 	val |= PM_CTL_SUS_MODE_0;
1394319b95b5SSteve Glendinning 
1395319b95b5SSteve Glendinning 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1396e360a8b4SSteve Glendinning 	if (ret < 0)
1397b052e073SSteve Glendinning 		return ret;
1398319b95b5SSteve Glendinning 
1399319b95b5SSteve Glendinning 	/* clear wol status */
1400319b95b5SSteve Glendinning 	val &= ~PM_CTL_WUPS_;
1401319b95b5SSteve Glendinning 	val |= PM_CTL_WUPS_WOL_;
1402319b95b5SSteve Glendinning 
1403319b95b5SSteve Glendinning 	/* enable energy detection */
1404319b95b5SSteve Glendinning 	if (pdata->wolopts & WAKE_PHY)
1405319b95b5SSteve Glendinning 		val |= PM_CTL_WUPS_ED_;
1406319b95b5SSteve Glendinning 
1407319b95b5SSteve Glendinning 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1408e360a8b4SSteve Glendinning 	if (ret < 0)
1409b052e073SSteve Glendinning 		return ret;
1410319b95b5SSteve Glendinning 
1411319b95b5SSteve Glendinning 	/* read back PM_CTRL */
1412319b95b5SSteve Glendinning 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
141376437214SMing Lei 	if (ret < 0)
141476437214SMing Lei 		return ret;
1415319b95b5SSteve Glendinning 
1416b2d4b150SSteve Glendinning 	pdata->suspend_flags |= SUSPEND_SUSPEND0;
1417b2d4b150SSteve Glendinning 
141876437214SMing Lei 	return 0;
1419319b95b5SSteve Glendinning }
1420319b95b5SSteve Glendinning 
1421319b95b5SSteve Glendinning static int smsc95xx_enter_suspend1(struct usbnet *dev)
1422319b95b5SSteve Glendinning {
1423319b95b5SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1424319b95b5SSteve Glendinning 	struct mii_if_info *mii = &dev->mii;
1425319b95b5SSteve Glendinning 	u32 val;
1426319b95b5SSteve Glendinning 	int ret;
1427319b95b5SSteve Glendinning 
1428319b95b5SSteve Glendinning 	/* reconfigure link pulse detection timing for
1429319b95b5SSteve Glendinning 	 * compatibility with non-standard link partners
1430319b95b5SSteve Glendinning 	 */
1431319b95b5SSteve Glendinning 	if (pdata->features & FEATURE_PHY_NLP_CROSSOVER)
1432319b95b5SSteve Glendinning 		smsc95xx_mdio_write_nopm(dev->net, mii->phy_id,	PHY_EDPD_CONFIG,
1433319b95b5SSteve Glendinning 			PHY_EDPD_CONFIG_DEFAULT);
1434319b95b5SSteve Glendinning 
1435319b95b5SSteve Glendinning 	/* enable energy detect power-down mode */
1436319b95b5SSteve Glendinning 	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_MODE_CTRL_STS);
1437e360a8b4SSteve Glendinning 	if (ret < 0)
1438b052e073SSteve Glendinning 		return ret;
1439319b95b5SSteve Glendinning 
1440319b95b5SSteve Glendinning 	ret |= MODE_CTRL_STS_EDPWRDOWN_;
1441319b95b5SSteve Glendinning 
1442319b95b5SSteve Glendinning 	smsc95xx_mdio_write_nopm(dev->net, mii->phy_id, PHY_MODE_CTRL_STS, ret);
1443319b95b5SSteve Glendinning 
1444319b95b5SSteve Glendinning 	/* enter SUSPEND1 mode */
1445319b95b5SSteve Glendinning 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1446e360a8b4SSteve Glendinning 	if (ret < 0)
1447b052e073SSteve Glendinning 		return ret;
1448319b95b5SSteve Glendinning 
1449319b95b5SSteve Glendinning 	val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1450319b95b5SSteve Glendinning 	val |= PM_CTL_SUS_MODE_1;
1451319b95b5SSteve Glendinning 
1452319b95b5SSteve Glendinning 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1453e360a8b4SSteve Glendinning 	if (ret < 0)
1454b052e073SSteve Glendinning 		return ret;
1455319b95b5SSteve Glendinning 
1456319b95b5SSteve Glendinning 	/* clear wol status, enable energy detection */
1457319b95b5SSteve Glendinning 	val &= ~PM_CTL_WUPS_;
1458319b95b5SSteve Glendinning 	val |= (PM_CTL_WUPS_ED_ | PM_CTL_ED_EN_);
1459319b95b5SSteve Glendinning 
1460319b95b5SSteve Glendinning 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
146176437214SMing Lei 	if (ret < 0)
146276437214SMing Lei 		return ret;
1463319b95b5SSteve Glendinning 
1464b2d4b150SSteve Glendinning 	pdata->suspend_flags |= SUSPEND_SUSPEND1;
1465b2d4b150SSteve Glendinning 
146676437214SMing Lei 	return 0;
1467319b95b5SSteve Glendinning }
1468319b95b5SSteve Glendinning 
1469319b95b5SSteve Glendinning static int smsc95xx_enter_suspend2(struct usbnet *dev)
1470319b95b5SSteve Glendinning {
1471b2d4b150SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1472319b95b5SSteve Glendinning 	u32 val;
1473319b95b5SSteve Glendinning 	int ret;
1474319b95b5SSteve Glendinning 
1475319b95b5SSteve Glendinning 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1476e360a8b4SSteve Glendinning 	if (ret < 0)
1477b052e073SSteve Glendinning 		return ret;
1478319b95b5SSteve Glendinning 
1479319b95b5SSteve Glendinning 	val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1480319b95b5SSteve Glendinning 	val |= PM_CTL_SUS_MODE_2;
1481319b95b5SSteve Glendinning 
1482319b95b5SSteve Glendinning 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
148376437214SMing Lei 	if (ret < 0)
148476437214SMing Lei 		return ret;
1485319b95b5SSteve Glendinning 
1486b2d4b150SSteve Glendinning 	pdata->suspend_flags |= SUSPEND_SUSPEND2;
1487b2d4b150SSteve Glendinning 
148876437214SMing Lei 	return 0;
1489319b95b5SSteve Glendinning }
1490319b95b5SSteve Glendinning 
1491b2d4b150SSteve Glendinning static int smsc95xx_enter_suspend3(struct usbnet *dev)
1492b2d4b150SSteve Glendinning {
1493b2d4b150SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1494b2d4b150SSteve Glendinning 	u32 val;
1495b2d4b150SSteve Glendinning 	int ret;
1496b2d4b150SSteve Glendinning 
1497b2d4b150SSteve Glendinning 	ret = smsc95xx_read_reg_nopm(dev, RX_FIFO_INF, &val);
1498b2d4b150SSteve Glendinning 	if (ret < 0)
1499b2d4b150SSteve Glendinning 		return ret;
1500b2d4b150SSteve Glendinning 
150153a759c8SMartin Wetterwald 	if (val & RX_FIFO_INF_USED_) {
1502b2d4b150SSteve Glendinning 		netdev_info(dev->net, "rx fifo not empty in autosuspend\n");
1503b2d4b150SSteve Glendinning 		return -EBUSY;
1504b2d4b150SSteve Glendinning 	}
1505b2d4b150SSteve Glendinning 
1506b2d4b150SSteve Glendinning 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1507b2d4b150SSteve Glendinning 	if (ret < 0)
1508b2d4b150SSteve Glendinning 		return ret;
1509b2d4b150SSteve Glendinning 
1510b2d4b150SSteve Glendinning 	val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1511b2d4b150SSteve Glendinning 	val |= PM_CTL_SUS_MODE_3 | PM_CTL_RES_CLR_WKP_STS;
1512b2d4b150SSteve Glendinning 
1513b2d4b150SSteve Glendinning 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1514b2d4b150SSteve Glendinning 	if (ret < 0)
1515b2d4b150SSteve Glendinning 		return ret;
1516b2d4b150SSteve Glendinning 
1517b2d4b150SSteve Glendinning 	/* clear wol status */
1518b2d4b150SSteve Glendinning 	val &= ~PM_CTL_WUPS_;
1519b2d4b150SSteve Glendinning 	val |= PM_CTL_WUPS_WOL_;
1520b2d4b150SSteve Glendinning 
1521b2d4b150SSteve Glendinning 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1522b2d4b150SSteve Glendinning 	if (ret < 0)
1523b2d4b150SSteve Glendinning 		return ret;
1524b2d4b150SSteve Glendinning 
1525b2d4b150SSteve Glendinning 	pdata->suspend_flags |= SUSPEND_SUSPEND3;
1526b2d4b150SSteve Glendinning 
1527b2d4b150SSteve Glendinning 	return 0;
1528b2d4b150SSteve Glendinning }
1529b2d4b150SSteve Glendinning 
1530b2d4b150SSteve Glendinning static int smsc95xx_autosuspend(struct usbnet *dev, u32 link_up)
1531b2d4b150SSteve Glendinning {
1532b2d4b150SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1533b2d4b150SSteve Glendinning 	int ret;
1534b2d4b150SSteve Glendinning 
1535b2d4b150SSteve Glendinning 	if (!netif_running(dev->net)) {
1536b2d4b150SSteve Glendinning 		/* interface is ifconfig down so fully power down hw */
1537b2d4b150SSteve Glendinning 		netdev_dbg(dev->net, "autosuspend entering SUSPEND2\n");
1538b2d4b150SSteve Glendinning 		return smsc95xx_enter_suspend2(dev);
1539b2d4b150SSteve Glendinning 	}
1540b2d4b150SSteve Glendinning 
1541b2d4b150SSteve Glendinning 	if (!link_up) {
1542b2d4b150SSteve Glendinning 		/* link is down so enter EDPD mode, but only if device can
1543b2d4b150SSteve Glendinning 		 * reliably resume from it.  This check should be redundant
1544eb970ff0SMing Lei 		 * as current FEATURE_REMOTE_WAKEUP parts also support
1545b2d4b150SSteve Glendinning 		 * FEATURE_PHY_NLP_CROSSOVER but it's included for clarity */
1546b2d4b150SSteve Glendinning 		if (!(pdata->features & FEATURE_PHY_NLP_CROSSOVER)) {
1547b2d4b150SSteve Glendinning 			netdev_warn(dev->net, "EDPD not supported\n");
1548b2d4b150SSteve Glendinning 			return -EBUSY;
1549b2d4b150SSteve Glendinning 		}
1550b2d4b150SSteve Glendinning 
1551b2d4b150SSteve Glendinning 		netdev_dbg(dev->net, "autosuspend entering SUSPEND1\n");
1552b2d4b150SSteve Glendinning 
1553b2d4b150SSteve Glendinning 		/* enable PHY wakeup events for if cable is attached */
1554b2d4b150SSteve Glendinning 		ret = smsc95xx_enable_phy_wakeup_interrupts(dev,
1555b2d4b150SSteve Glendinning 			PHY_INT_MASK_ANEG_COMP_);
1556b2d4b150SSteve Glendinning 		if (ret < 0) {
1557b2d4b150SSteve Glendinning 			netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1558b2d4b150SSteve Glendinning 			return ret;
1559b2d4b150SSteve Glendinning 		}
1560b2d4b150SSteve Glendinning 
1561b2d4b150SSteve Glendinning 		netdev_info(dev->net, "entering SUSPEND1 mode\n");
1562b2d4b150SSteve Glendinning 		return smsc95xx_enter_suspend1(dev);
1563b2d4b150SSteve Glendinning 	}
1564b2d4b150SSteve Glendinning 
1565b2d4b150SSteve Glendinning 	/* enable PHY wakeup events so we remote wakeup if cable is pulled */
1566b2d4b150SSteve Glendinning 	ret = smsc95xx_enable_phy_wakeup_interrupts(dev,
1567b2d4b150SSteve Glendinning 		PHY_INT_MASK_LINK_DOWN_);
1568b2d4b150SSteve Glendinning 	if (ret < 0) {
1569b2d4b150SSteve Glendinning 		netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1570b2d4b150SSteve Glendinning 		return ret;
1571b2d4b150SSteve Glendinning 	}
1572b2d4b150SSteve Glendinning 
1573b2d4b150SSteve Glendinning 	netdev_dbg(dev->net, "autosuspend entering SUSPEND3\n");
1574b2d4b150SSteve Glendinning 	return smsc95xx_enter_suspend3(dev);
1575b2d4b150SSteve Glendinning }
1576b2d4b150SSteve Glendinning 
1577b5a04475SSteve Glendinning static int smsc95xx_suspend(struct usb_interface *intf, pm_message_t message)
1578b5a04475SSteve Glendinning {
1579b5a04475SSteve Glendinning 	struct usbnet *dev = usb_get_intfdata(intf);
1580e0e474a8SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1581e5e3af83SSteve Glendinning 	u32 val, link_up;
1582b5a04475SSteve Glendinning 	int ret;
1583b5a04475SSteve Glendinning 
1584b5a04475SSteve Glendinning 	ret = usbnet_suspend(intf, message);
1585b052e073SSteve Glendinning 	if (ret < 0) {
1586b052e073SSteve Glendinning 		netdev_warn(dev->net, "usbnet_suspend error\n");
1587b052e073SSteve Glendinning 		return ret;
1588b052e073SSteve Glendinning 	}
1589b5a04475SSteve Glendinning 
15907b900eadSFrieder Schrempf 	cancel_delayed_work_sync(&pdata->carrier_check);
15917b900eadSFrieder Schrempf 
1592b2d4b150SSteve Glendinning 	if (pdata->suspend_flags) {
1593b2d4b150SSteve Glendinning 		netdev_warn(dev->net, "error during last resume\n");
1594b2d4b150SSteve Glendinning 		pdata->suspend_flags = 0;
1595b2d4b150SSteve Glendinning 	}
1596b2d4b150SSteve Glendinning 
1597e5e3af83SSteve Glendinning 	/* determine if link is up using only _nopm functions */
1598e5e3af83SSteve Glendinning 	link_up = smsc95xx_link_ok_nopm(dev);
1599e5e3af83SSteve Glendinning 
160042e21c01SMing Lei 	if (message.event == PM_EVENT_AUTO_SUSPEND &&
1601eb970ff0SMing Lei 	    (pdata->features & FEATURE_REMOTE_WAKEUP)) {
1602b2d4b150SSteve Glendinning 		ret = smsc95xx_autosuspend(dev, link_up);
1603b2d4b150SSteve Glendinning 		goto done;
1604b2d4b150SSteve Glendinning 	}
1605b2d4b150SSteve Glendinning 
1606b2d4b150SSteve Glendinning 	/* if we get this far we're not autosuspending */
1607e5e3af83SSteve Glendinning 	/* if no wol options set, or if link is down and we're not waking on
1608e5e3af83SSteve Glendinning 	 * PHY activity, enter lowest power SUSPEND2 mode
1609e5e3af83SSteve Glendinning 	 */
1610e5e3af83SSteve Glendinning 	if (!(pdata->wolopts & SUPPORTED_WAKE) ||
1611e5e3af83SSteve Glendinning 		!(link_up || (pdata->wolopts & WAKE_PHY))) {
16121e1d7412SJoe Perches 		netdev_info(dev->net, "entering SUSPEND2 mode\n");
1613b5a04475SSteve Glendinning 
1614e0e474a8SSteve Glendinning 		/* disable energy detect (link up) & wake up events */
1615ec32115dSMing Lei 		ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1616e360a8b4SSteve Glendinning 		if (ret < 0)
1617b052e073SSteve Glendinning 			goto done;
1618e0e474a8SSteve Glendinning 
1619e0e474a8SSteve Glendinning 		val &= ~(WUCSR_MPEN_ | WUCSR_WAKE_EN_);
1620e0e474a8SSteve Glendinning 
1621ec32115dSMing Lei 		ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1622e360a8b4SSteve Glendinning 		if (ret < 0)
1623b052e073SSteve Glendinning 			goto done;
1624e0e474a8SSteve Glendinning 
1625ec32115dSMing Lei 		ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1626e360a8b4SSteve Glendinning 		if (ret < 0)
1627b052e073SSteve Glendinning 			goto done;
1628e0e474a8SSteve Glendinning 
1629e0e474a8SSteve Glendinning 		val &= ~(PM_CTL_ED_EN_ | PM_CTL_WOL_EN_);
1630e0e474a8SSteve Glendinning 
1631ec32115dSMing Lei 		ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1632e360a8b4SSteve Glendinning 		if (ret < 0)
1633b052e073SSteve Glendinning 			goto done;
1634e0e474a8SSteve Glendinning 
16353b9f7d8cSSteve Glendinning 		ret = smsc95xx_enter_suspend2(dev);
16363b9f7d8cSSteve Glendinning 		goto done;
1637b5a04475SSteve Glendinning 	}
1638b5a04475SSteve Glendinning 
1639e5e3af83SSteve Glendinning 	if (pdata->wolopts & WAKE_PHY) {
1640e5e3af83SSteve Glendinning 		ret = smsc95xx_enable_phy_wakeup_interrupts(dev,
1641e5e3af83SSteve Glendinning 			(PHY_INT_MASK_ANEG_COMP_ | PHY_INT_MASK_LINK_DOWN_));
1642b052e073SSteve Glendinning 		if (ret < 0) {
1643b052e073SSteve Glendinning 			netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1644b052e073SSteve Glendinning 			goto done;
1645b052e073SSteve Glendinning 		}
1646e5e3af83SSteve Glendinning 
1647e5e3af83SSteve Glendinning 		/* if link is down then configure EDPD and enter SUSPEND1,
1648e5e3af83SSteve Glendinning 		 * otherwise enter SUSPEND0 below
1649e5e3af83SSteve Glendinning 		 */
1650e5e3af83SSteve Glendinning 		if (!link_up) {
16511e1d7412SJoe Perches 			netdev_info(dev->net, "entering SUSPEND1 mode\n");
16523b9f7d8cSSteve Glendinning 			ret = smsc95xx_enter_suspend1(dev);
16533b9f7d8cSSteve Glendinning 			goto done;
1654e5e3af83SSteve Glendinning 		}
1655e5e3af83SSteve Glendinning 	}
1656e5e3af83SSteve Glendinning 
1657bbd9f9eeSSteve Glendinning 	if (pdata->wolopts & (WAKE_BCAST | WAKE_MCAST | WAKE_ARP | WAKE_UCAST)) {
16586396bb22SKees Cook 		u32 *filter_mask = kcalloc(32, sizeof(u32), GFP_KERNEL);
165906a221beSMing Lei 		u32 command[2];
166006a221beSMing Lei 		u32 offset[2];
166106a221beSMing Lei 		u32 crc[4];
16629ebca507SSteve Glendinning 		int wuff_filter_count =
16639ebca507SSteve Glendinning 			(pdata->features & FEATURE_8_WAKEUP_FILTERS) ?
16649ebca507SSteve Glendinning 			LAN9500A_WUFF_NUM : LAN9500_WUFF_NUM;
1665bbd9f9eeSSteve Glendinning 		int i, filter = 0;
1666bbd9f9eeSSteve Glendinning 
1667eed9a729SSteve Glendinning 		if (!filter_mask) {
1668eed9a729SSteve Glendinning 			netdev_warn(dev->net, "Unable to allocate filter_mask\n");
16693b9f7d8cSSteve Glendinning 			ret = -ENOMEM;
16703b9f7d8cSSteve Glendinning 			goto done;
1671eed9a729SSteve Glendinning 		}
1672eed9a729SSteve Glendinning 
167306a221beSMing Lei 		memset(command, 0, sizeof(command));
167406a221beSMing Lei 		memset(offset, 0, sizeof(offset));
167506a221beSMing Lei 		memset(crc, 0, sizeof(crc));
167606a221beSMing Lei 
1677bbd9f9eeSSteve Glendinning 		if (pdata->wolopts & WAKE_BCAST) {
1678bbd9f9eeSSteve Glendinning 			const u8 bcast[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
16791e1d7412SJoe Perches 			netdev_info(dev->net, "enabling broadcast detection\n");
1680bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4] = 0x003F;
1681bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 1] = 0x00;
1682bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 2] = 0x00;
1683bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 3] = 0x00;
1684bbd9f9eeSSteve Glendinning 			command[filter/4] |= 0x05UL << ((filter % 4) * 8);
1685bbd9f9eeSSteve Glendinning 			offset[filter/4] |= 0x00 << ((filter % 4) * 8);
1686bbd9f9eeSSteve Glendinning 			crc[filter/2] |= smsc_crc(bcast, 6, filter);
1687bbd9f9eeSSteve Glendinning 			filter++;
1688bbd9f9eeSSteve Glendinning 		}
1689bbd9f9eeSSteve Glendinning 
1690bbd9f9eeSSteve Glendinning 		if (pdata->wolopts & WAKE_MCAST) {
1691bbd9f9eeSSteve Glendinning 			const u8 mcast[] = {0x01, 0x00, 0x5E};
16921e1d7412SJoe Perches 			netdev_info(dev->net, "enabling multicast detection\n");
1693bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4] = 0x0007;
1694bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 1] = 0x00;
1695bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 2] = 0x00;
1696bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 3] = 0x00;
1697bbd9f9eeSSteve Glendinning 			command[filter/4] |= 0x09UL << ((filter % 4) * 8);
1698bbd9f9eeSSteve Glendinning 			offset[filter/4] |= 0x00  << ((filter % 4) * 8);
1699bbd9f9eeSSteve Glendinning 			crc[filter/2] |= smsc_crc(mcast, 3, filter);
1700bbd9f9eeSSteve Glendinning 			filter++;
1701bbd9f9eeSSteve Glendinning 		}
1702bbd9f9eeSSteve Glendinning 
1703bbd9f9eeSSteve Glendinning 		if (pdata->wolopts & WAKE_ARP) {
1704bbd9f9eeSSteve Glendinning 			const u8 arp[] = {0x08, 0x06};
17051e1d7412SJoe Perches 			netdev_info(dev->net, "enabling ARP detection\n");
1706bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4] = 0x0003;
1707bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 1] = 0x00;
1708bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 2] = 0x00;
1709bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 3] = 0x00;
1710bbd9f9eeSSteve Glendinning 			command[filter/4] |= 0x05UL << ((filter % 4) * 8);
1711bbd9f9eeSSteve Glendinning 			offset[filter/4] |= 0x0C << ((filter % 4) * 8);
1712bbd9f9eeSSteve Glendinning 			crc[filter/2] |= smsc_crc(arp, 2, filter);
1713bbd9f9eeSSteve Glendinning 			filter++;
1714bbd9f9eeSSteve Glendinning 		}
1715bbd9f9eeSSteve Glendinning 
1716bbd9f9eeSSteve Glendinning 		if (pdata->wolopts & WAKE_UCAST) {
17171e1d7412SJoe Perches 			netdev_info(dev->net, "enabling unicast detection\n");
1718bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4] = 0x003F;
1719bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 1] = 0x00;
1720bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 2] = 0x00;
1721bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 3] = 0x00;
1722bbd9f9eeSSteve Glendinning 			command[filter/4] |= 0x01UL << ((filter % 4) * 8);
1723bbd9f9eeSSteve Glendinning 			offset[filter/4] |= 0x00 << ((filter % 4) * 8);
1724bbd9f9eeSSteve Glendinning 			crc[filter/2] |= smsc_crc(dev->net->dev_addr, ETH_ALEN, filter);
1725bbd9f9eeSSteve Glendinning 			filter++;
1726bbd9f9eeSSteve Glendinning 		}
1727bbd9f9eeSSteve Glendinning 
17289ebca507SSteve Glendinning 		for (i = 0; i < (wuff_filter_count * 4); i++) {
1729ec32115dSMing Lei 			ret = smsc95xx_write_reg_nopm(dev, WUFF, filter_mask[i]);
1730b052e073SSteve Glendinning 			if (ret < 0) {
173106a221beSMing Lei 				kfree(filter_mask);
1732b052e073SSteve Glendinning 				goto done;
1733b052e073SSteve Glendinning 			}
1734bbd9f9eeSSteve Glendinning 		}
173506a221beSMing Lei 		kfree(filter_mask);
1736bbd9f9eeSSteve Glendinning 
17379ebca507SSteve Glendinning 		for (i = 0; i < (wuff_filter_count / 4); i++) {
1738ec32115dSMing Lei 			ret = smsc95xx_write_reg_nopm(dev, WUFF, command[i]);
1739e360a8b4SSteve Glendinning 			if (ret < 0)
1740b052e073SSteve Glendinning 				goto done;
1741b052e073SSteve Glendinning 		}
1742bbd9f9eeSSteve Glendinning 
17439ebca507SSteve Glendinning 		for (i = 0; i < (wuff_filter_count / 4); i++) {
1744ec32115dSMing Lei 			ret = smsc95xx_write_reg_nopm(dev, WUFF, offset[i]);
1745e360a8b4SSteve Glendinning 			if (ret < 0)
1746b052e073SSteve Glendinning 				goto done;
1747b052e073SSteve Glendinning 		}
1748bbd9f9eeSSteve Glendinning 
17499ebca507SSteve Glendinning 		for (i = 0; i < (wuff_filter_count / 2); i++) {
1750ec32115dSMing Lei 			ret = smsc95xx_write_reg_nopm(dev, WUFF, crc[i]);
1751e360a8b4SSteve Glendinning 			if (ret < 0)
1752b052e073SSteve Glendinning 				goto done;
1753b052e073SSteve Glendinning 		}
1754bbd9f9eeSSteve Glendinning 
1755bbd9f9eeSSteve Glendinning 		/* clear any pending pattern match packet status */
1756ec32115dSMing Lei 		ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1757e360a8b4SSteve Glendinning 		if (ret < 0)
1758b052e073SSteve Glendinning 			goto done;
1759bbd9f9eeSSteve Glendinning 
1760bbd9f9eeSSteve Glendinning 		val |= WUCSR_WUFR_;
1761bbd9f9eeSSteve Glendinning 
1762ec32115dSMing Lei 		ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1763e360a8b4SSteve Glendinning 		if (ret < 0)
1764b052e073SSteve Glendinning 			goto done;
1765b052e073SSteve Glendinning 	}
1766bbd9f9eeSSteve Glendinning 
1767e0e474a8SSteve Glendinning 	if (pdata->wolopts & WAKE_MAGIC) {
1768e0e474a8SSteve Glendinning 		/* clear any pending magic packet status */
1769ec32115dSMing Lei 		ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1770e360a8b4SSteve Glendinning 		if (ret < 0)
1771b052e073SSteve Glendinning 			goto done;
1772e0e474a8SSteve Glendinning 
1773e0e474a8SSteve Glendinning 		val |= WUCSR_MPR_;
1774e0e474a8SSteve Glendinning 
1775ec32115dSMing Lei 		ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1776e360a8b4SSteve Glendinning 		if (ret < 0)
1777b052e073SSteve Glendinning 			goto done;
1778b052e073SSteve Glendinning 	}
1779e0e474a8SSteve Glendinning 
1780bbd9f9eeSSteve Glendinning 	/* enable/disable wakeup sources */
1781ec32115dSMing Lei 	ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1782e360a8b4SSteve Glendinning 	if (ret < 0)
1783b052e073SSteve Glendinning 		goto done;
1784e0e474a8SSteve Glendinning 
1785bbd9f9eeSSteve Glendinning 	if (pdata->wolopts & (WAKE_BCAST | WAKE_MCAST | WAKE_ARP | WAKE_UCAST)) {
17861e1d7412SJoe Perches 		netdev_info(dev->net, "enabling pattern match wakeup\n");
1787bbd9f9eeSSteve Glendinning 		val |= WUCSR_WAKE_EN_;
1788bbd9f9eeSSteve Glendinning 	} else {
17891e1d7412SJoe Perches 		netdev_info(dev->net, "disabling pattern match wakeup\n");
1790bbd9f9eeSSteve Glendinning 		val &= ~WUCSR_WAKE_EN_;
1791bbd9f9eeSSteve Glendinning 	}
1792bbd9f9eeSSteve Glendinning 
1793e0e474a8SSteve Glendinning 	if (pdata->wolopts & WAKE_MAGIC) {
17941e1d7412SJoe Perches 		netdev_info(dev->net, "enabling magic packet wakeup\n");
1795e0e474a8SSteve Glendinning 		val |= WUCSR_MPEN_;
1796e0e474a8SSteve Glendinning 	} else {
17971e1d7412SJoe Perches 		netdev_info(dev->net, "disabling magic packet wakeup\n");
1798e0e474a8SSteve Glendinning 		val &= ~WUCSR_MPEN_;
1799e0e474a8SSteve Glendinning 	}
1800e0e474a8SSteve Glendinning 
1801ec32115dSMing Lei 	ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1802e360a8b4SSteve Glendinning 	if (ret < 0)
1803b052e073SSteve Glendinning 		goto done;
1804e0e474a8SSteve Glendinning 
1805e0e474a8SSteve Glendinning 	/* enable wol wakeup source */
1806ec32115dSMing Lei 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1807e360a8b4SSteve Glendinning 	if (ret < 0)
1808b052e073SSteve Glendinning 		goto done;
1809e0e474a8SSteve Glendinning 
1810e0e474a8SSteve Glendinning 	val |= PM_CTL_WOL_EN_;
1811e0e474a8SSteve Glendinning 
1812e5e3af83SSteve Glendinning 	/* phy energy detect wakeup source */
1813e5e3af83SSteve Glendinning 	if (pdata->wolopts & WAKE_PHY)
1814e5e3af83SSteve Glendinning 		val |= PM_CTL_ED_EN_;
1815e5e3af83SSteve Glendinning 
1816ec32115dSMing Lei 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1817e360a8b4SSteve Glendinning 	if (ret < 0)
1818b052e073SSteve Glendinning 		goto done;
1819e0e474a8SSteve Glendinning 
1820bbd9f9eeSSteve Glendinning 	/* enable receiver to enable frame reception */
1821ec32115dSMing Lei 	smsc95xx_start_rx_path(dev, 1);
1822e0e474a8SSteve Glendinning 
1823e0e474a8SSteve Glendinning 	/* some wol options are enabled, so enter SUSPEND0 */
18241e1d7412SJoe Perches 	netdev_info(dev->net, "entering SUSPEND0 mode\n");
18253b9f7d8cSSteve Glendinning 	ret = smsc95xx_enter_suspend0(dev);
18263b9f7d8cSSteve Glendinning 
18273b9f7d8cSSteve Glendinning done:
18280d41be53SMing Lei 	/*
18290d41be53SMing Lei 	 * TODO: resume() might need to handle the suspend failure
18300d41be53SMing Lei 	 * in system sleep
18310d41be53SMing Lei 	 */
18320d41be53SMing Lei 	if (ret && PMSG_IS_AUTO(message))
18333b9f7d8cSSteve Glendinning 		usbnet_resume(intf);
18347b900eadSFrieder Schrempf 
18357b900eadSFrieder Schrempf 	if (ret)
18367b900eadSFrieder Schrempf 		schedule_delayed_work(&pdata->carrier_check,
18377b900eadSFrieder Schrempf 				      CARRIER_CHECK_DELAY);
18387b900eadSFrieder Schrempf 
18393b9f7d8cSSteve Glendinning 	return ret;
1840e0e474a8SSteve Glendinning }
1841e0e474a8SSteve Glendinning 
1842e0e474a8SSteve Glendinning static int smsc95xx_resume(struct usb_interface *intf)
1843e0e474a8SSteve Glendinning {
1844e0e474a8SSteve Glendinning 	struct usbnet *dev = usb_get_intfdata(intf);
18458bca81d9SSudip Mukherjee 	struct smsc95xx_priv *pdata;
18468bca81d9SSudip Mukherjee 	u8 suspend_flags;
1847e0e474a8SSteve Glendinning 	int ret;
1848e0e474a8SSteve Glendinning 	u32 val;
1849e0e474a8SSteve Glendinning 
1850e0e474a8SSteve Glendinning 	BUG_ON(!dev);
18518bca81d9SSudip Mukherjee 	pdata = (struct smsc95xx_priv *)(dev->data[0]);
18528bca81d9SSudip Mukherjee 	suspend_flags = pdata->suspend_flags;
1853e0e474a8SSteve Glendinning 
1854b2d4b150SSteve Glendinning 	netdev_dbg(dev->net, "resume suspend_flags=0x%02x\n", suspend_flags);
1855b2d4b150SSteve Glendinning 
1856b2d4b150SSteve Glendinning 	/* do this first to ensure it's cleared even in error case */
1857b2d4b150SSteve Glendinning 	pdata->suspend_flags = 0;
1858d69d1694SChristoph Fritz 	schedule_delayed_work(&pdata->carrier_check, CARRIER_CHECK_DELAY);
1859b2d4b150SSteve Glendinning 
1860b2d4b150SSteve Glendinning 	if (suspend_flags & SUSPEND_ALLMODES) {
1861bbd9f9eeSSteve Glendinning 		/* clear wake-up sources */
1862ec32115dSMing Lei 		ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1863e360a8b4SSteve Glendinning 		if (ret < 0)
1864b052e073SSteve Glendinning 			return ret;
1865e0e474a8SSteve Glendinning 
1866bbd9f9eeSSteve Glendinning 		val &= ~(WUCSR_WAKE_EN_ | WUCSR_MPEN_);
1867e0e474a8SSteve Glendinning 
1868ec32115dSMing Lei 		ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1869e360a8b4SSteve Glendinning 		if (ret < 0)
1870b052e073SSteve Glendinning 			return ret;
1871e0e474a8SSteve Glendinning 
1872e0e474a8SSteve Glendinning 		/* clear wake-up status */
1873ec32115dSMing Lei 		ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1874e360a8b4SSteve Glendinning 		if (ret < 0)
1875b052e073SSteve Glendinning 			return ret;
1876e0e474a8SSteve Glendinning 
1877e0e474a8SSteve Glendinning 		val &= ~PM_CTL_WOL_EN_;
1878e0e474a8SSteve Glendinning 		val |= PM_CTL_WUPS_;
1879e0e474a8SSteve Glendinning 
1880ec32115dSMing Lei 		ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1881e360a8b4SSteve Glendinning 		if (ret < 0)
1882b052e073SSteve Glendinning 			return ret;
1883b052e073SSteve Glendinning 	}
1884e0e474a8SSteve Glendinning 
1885af3d7c1eSSteve Glendinning 	ret = usbnet_resume(intf);
1886b052e073SSteve Glendinning 	if (ret < 0)
1887b052e073SSteve Glendinning 		netdev_warn(dev->net, "usbnet_resume error\n");
1888e0e474a8SSteve Glendinning 
1889b052e073SSteve Glendinning 	return ret;
1890e0e474a8SSteve Glendinning }
1891e0e474a8SSteve Glendinning 
1892b4df480fSJoonyoung Shim static int smsc95xx_reset_resume(struct usb_interface *intf)
1893b4df480fSJoonyoung Shim {
1894b4df480fSJoonyoung Shim 	struct usbnet *dev = usb_get_intfdata(intf);
1895b4df480fSJoonyoung Shim 	int ret;
1896b4df480fSJoonyoung Shim 
1897b4df480fSJoonyoung Shim 	ret = smsc95xx_reset(dev);
1898b4df480fSJoonyoung Shim 	if (ret < 0)
1899b4df480fSJoonyoung Shim 		return ret;
1900b4df480fSJoonyoung Shim 
1901b4df480fSJoonyoung Shim 	return smsc95xx_resume(intf);
1902b4df480fSJoonyoung Shim }
1903b4df480fSJoonyoung Shim 
19042f7ca802SSteve Glendinning static void smsc95xx_rx_csum_offload(struct sk_buff *skb)
19052f7ca802SSteve Glendinning {
19062f7ca802SSteve Glendinning 	skb->csum = *(u16 *)(skb_tail_pointer(skb) - 2);
19072f7ca802SSteve Glendinning 	skb->ip_summed = CHECKSUM_COMPLETE;
19082f7ca802SSteve Glendinning 	skb_trim(skb, skb->len - 2);
19092f7ca802SSteve Glendinning }
19102f7ca802SSteve Glendinning 
19112f7ca802SSteve Glendinning static int smsc95xx_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
19122f7ca802SSteve Glendinning {
1913eb85569fSEmil Goode 	/* This check is no longer done by usbnet */
1914eb85569fSEmil Goode 	if (skb->len < dev->net->hard_header_len)
1915eb85569fSEmil Goode 		return 0;
1916eb85569fSEmil Goode 
19172f7ca802SSteve Glendinning 	while (skb->len > 0) {
19182f7ca802SSteve Glendinning 		u32 header, align_count;
19192f7ca802SSteve Glendinning 		struct sk_buff *ax_skb;
19202f7ca802SSteve Glendinning 		unsigned char *packet;
19212f7ca802SSteve Glendinning 		u16 size;
19222f7ca802SSteve Glendinning 
19236809d216SBen Dooks 		header = get_unaligned_le32(skb->data);
19242f7ca802SSteve Glendinning 		skb_pull(skb, 4 + NET_IP_ALIGN);
19252f7ca802SSteve Glendinning 		packet = skb->data;
19262f7ca802SSteve Glendinning 
19272f7ca802SSteve Glendinning 		/* get the packet length */
19282f7ca802SSteve Glendinning 		size = (u16)((header & RX_STS_FL_) >> 16);
19292f7ca802SSteve Glendinning 		align_count = (4 - ((size + NET_IP_ALIGN) % 4)) % 4;
19302f7ca802SSteve Glendinning 
19312f7ca802SSteve Glendinning 		if (unlikely(header & RX_STS_ES_)) {
1932a475f603SJoe Perches 			netif_dbg(dev, rx_err, dev->net,
1933a475f603SJoe Perches 				  "Error header=0x%08x\n", header);
193480667ac1SHerbert Xu 			dev->net->stats.rx_errors++;
193580667ac1SHerbert Xu 			dev->net->stats.rx_dropped++;
19362f7ca802SSteve Glendinning 
19372f7ca802SSteve Glendinning 			if (header & RX_STS_CRC_) {
193880667ac1SHerbert Xu 				dev->net->stats.rx_crc_errors++;
19392f7ca802SSteve Glendinning 			} else {
19402f7ca802SSteve Glendinning 				if (header & (RX_STS_TL_ | RX_STS_RF_))
194180667ac1SHerbert Xu 					dev->net->stats.rx_frame_errors++;
19422f7ca802SSteve Glendinning 
19432f7ca802SSteve Glendinning 				if ((header & RX_STS_LE_) &&
19442f7ca802SSteve Glendinning 					(!(header & RX_STS_FT_)))
194580667ac1SHerbert Xu 					dev->net->stats.rx_length_errors++;
19462f7ca802SSteve Glendinning 			}
19472f7ca802SSteve Glendinning 		} else {
19482f7ca802SSteve Glendinning 			/* ETH_FRAME_LEN + 4(CRC) + 2(COE) + 4(Vlan) */
19492f7ca802SSteve Glendinning 			if (unlikely(size > (ETH_FRAME_LEN + 12))) {
1950a475f603SJoe Perches 				netif_dbg(dev, rx_err, dev->net,
1951a475f603SJoe Perches 					  "size err header=0x%08x\n", header);
19522f7ca802SSteve Glendinning 				return 0;
19532f7ca802SSteve Glendinning 			}
19542f7ca802SSteve Glendinning 
19552f7ca802SSteve Glendinning 			/* last frame in this batch */
19562f7ca802SSteve Glendinning 			if (skb->len == size) {
195778e47fe4SMichał Mirosław 				if (dev->net->features & NETIF_F_RXCSUM)
19582f7ca802SSteve Glendinning 					smsc95xx_rx_csum_offload(skb);
1959df18accaSPeter Korsgaard 				skb_trim(skb, skb->len - 4); /* remove fcs */
19602f7ca802SSteve Glendinning 				skb->truesize = size + sizeof(struct sk_buff);
19612f7ca802SSteve Glendinning 
19622f7ca802SSteve Glendinning 				return 1;
19632f7ca802SSteve Glendinning 			}
19642f7ca802SSteve Glendinning 
19652f7ca802SSteve Glendinning 			ax_skb = skb_clone(skb, GFP_ATOMIC);
19662f7ca802SSteve Glendinning 			if (unlikely(!ax_skb)) {
196760b86755SJoe Perches 				netdev_warn(dev->net, "Error allocating skb\n");
19682f7ca802SSteve Glendinning 				return 0;
19692f7ca802SSteve Glendinning 			}
19702f7ca802SSteve Glendinning 
19712f7ca802SSteve Glendinning 			ax_skb->len = size;
19722f7ca802SSteve Glendinning 			ax_skb->data = packet;
19732f7ca802SSteve Glendinning 			skb_set_tail_pointer(ax_skb, size);
19742f7ca802SSteve Glendinning 
197578e47fe4SMichał Mirosław 			if (dev->net->features & NETIF_F_RXCSUM)
19762f7ca802SSteve Glendinning 				smsc95xx_rx_csum_offload(ax_skb);
1977df18accaSPeter Korsgaard 			skb_trim(ax_skb, ax_skb->len - 4); /* remove fcs */
19782f7ca802SSteve Glendinning 			ax_skb->truesize = size + sizeof(struct sk_buff);
19792f7ca802SSteve Glendinning 
19802f7ca802SSteve Glendinning 			usbnet_skb_return(dev, ax_skb);
19812f7ca802SSteve Glendinning 		}
19822f7ca802SSteve Glendinning 
19832f7ca802SSteve Glendinning 		skb_pull(skb, size);
19842f7ca802SSteve Glendinning 
19852f7ca802SSteve Glendinning 		/* padding bytes before the next frame starts */
19862f7ca802SSteve Glendinning 		if (skb->len)
19872f7ca802SSteve Glendinning 			skb_pull(skb, align_count);
19882f7ca802SSteve Glendinning 	}
19892f7ca802SSteve Glendinning 
19902f7ca802SSteve Glendinning 	return 1;
19912f7ca802SSteve Glendinning }
19922f7ca802SSteve Glendinning 
1993f7b29271SSteve Glendinning static u32 smsc95xx_calc_csum_preamble(struct sk_buff *skb)
1994f7b29271SSteve Glendinning {
199555508d60SMichał Mirosław 	u16 low_16 = (u16)skb_checksum_start_offset(skb);
199655508d60SMichał Mirosław 	u16 high_16 = low_16 + skb->csum_offset;
1997f7b29271SSteve Glendinning 	return (high_16 << 16) | low_16;
1998f7b29271SSteve Glendinning }
1999f7b29271SSteve Glendinning 
200075938f77SBen Dooks /* The TX CSUM won't work if the checksum lies in the last 4 bytes of the
200175938f77SBen Dooks  * transmission. This is fairly unlikely, only seems to trigger with some
200275938f77SBen Dooks  * short TCP ACK packets sent.
200375938f77SBen Dooks  *
200475938f77SBen Dooks  * Note, this calculation should probably check for the alignment of the
200575938f77SBen Dooks  * data as well, but a straight check for csum being in the last four bytes
200675938f77SBen Dooks  * of the packet should be ok for now.
200775938f77SBen Dooks  */
200875938f77SBen Dooks static bool smsc95xx_can_tx_checksum(struct sk_buff *skb)
200975938f77SBen Dooks {
201075938f77SBen Dooks        unsigned int len = skb->len - skb_checksum_start_offset(skb);
201175938f77SBen Dooks 
201275938f77SBen Dooks        if (skb->len <= 45)
201375938f77SBen Dooks 	       return false;
201475938f77SBen Dooks        return skb->csum_offset < (len - (4 + 1));
201575938f77SBen Dooks }
201675938f77SBen Dooks 
20172f7ca802SSteve Glendinning static struct sk_buff *smsc95xx_tx_fixup(struct usbnet *dev,
20182f7ca802SSteve Glendinning 					 struct sk_buff *skb, gfp_t flags)
20192f7ca802SSteve Glendinning {
202078e47fe4SMichał Mirosław 	bool csum = skb->ip_summed == CHECKSUM_PARTIAL;
2021f7b29271SSteve Glendinning 	int overhead = csum ? SMSC95XX_TX_OVERHEAD_CSUM : SMSC95XX_TX_OVERHEAD;
20222f7ca802SSteve Glendinning 	u32 tx_cmd_a, tx_cmd_b;
20230c8b2655SBen Dooks 	void *ptr;
20242f7ca802SSteve Glendinning 
2025f7b29271SSteve Glendinning 	/* We do not advertise SG, so skbs should be already linearized */
2026f7b29271SSteve Glendinning 	BUG_ON(skb_shinfo(skb)->nr_frags);
2027f7b29271SSteve Glendinning 
2028e9156cd2SJames Hughes 	/* Make writable and expand header space by overhead if required */
2029e9156cd2SJames Hughes 	if (skb_cow_head(skb, overhead)) {
2030e9156cd2SJames Hughes 		/* Must deallocate here as returning NULL to indicate error
2031e9156cd2SJames Hughes 		 * means the skb won't be deallocated in the caller.
2032e9156cd2SJames Hughes 		 */
20332f7ca802SSteve Glendinning 		dev_kfree_skb_any(skb);
20342f7ca802SSteve Glendinning 		return NULL;
20352f7ca802SSteve Glendinning 	}
20362f7ca802SSteve Glendinning 
20370c8b2655SBen Dooks 	tx_cmd_b = (u32)skb->len;
20380c8b2655SBen Dooks 	tx_cmd_a = tx_cmd_b | TX_CMD_A_FIRST_SEG_ | TX_CMD_A_LAST_SEG_;
20390c8b2655SBen Dooks 
2040f7b29271SSteve Glendinning 	if (csum) {
204175938f77SBen Dooks 		if (!smsc95xx_can_tx_checksum(skb)) {
204211bc3088SSteve Glendinning 			/* workaround - hardware tx checksum does not work
204311bc3088SSteve Glendinning 			 * properly with extremely small packets */
204455508d60SMichał Mirosław 			long csstart = skb_checksum_start_offset(skb);
204511bc3088SSteve Glendinning 			__wsum calc = csum_partial(skb->data + csstart,
204611bc3088SSteve Glendinning 				skb->len - csstart, 0);
204711bc3088SSteve Glendinning 			*((__sum16 *)(skb->data + csstart
204811bc3088SSteve Glendinning 				+ skb->csum_offset)) = csum_fold(calc);
204911bc3088SSteve Glendinning 
205011bc3088SSteve Glendinning 			csum = false;
205111bc3088SSteve Glendinning 		} else {
2052f7b29271SSteve Glendinning 			u32 csum_preamble = smsc95xx_calc_csum_preamble(skb);
20530c8b2655SBen Dooks 			ptr = skb_push(skb, 4);
20540c8b2655SBen Dooks 			put_unaligned_le32(csum_preamble, ptr);
2055f7b29271SSteve Glendinning 
20560c8b2655SBen Dooks 			tx_cmd_a += 4;
20570c8b2655SBen Dooks 			tx_cmd_b += 4;
2058f7b29271SSteve Glendinning 			tx_cmd_b |= TX_CMD_B_CSUM_ENABLE;
20590c8b2655SBen Dooks 		}
20600c8b2655SBen Dooks 	}
20612f7ca802SSteve Glendinning 
20620c8b2655SBen Dooks 	ptr = skb_push(skb, 8);
20630c8b2655SBen Dooks 	put_unaligned_le32(tx_cmd_a, ptr);
20640c8b2655SBen Dooks 	put_unaligned_le32(tx_cmd_b, ptr+4);
20652f7ca802SSteve Glendinning 
20662f7ca802SSteve Glendinning 	return skb;
20672f7ca802SSteve Glendinning }
20682f7ca802SSteve Glendinning 
2069b2d4b150SSteve Glendinning static int smsc95xx_manage_power(struct usbnet *dev, int on)
2070b2d4b150SSteve Glendinning {
2071b2d4b150SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
2072b2d4b150SSteve Glendinning 
2073b2d4b150SSteve Glendinning 	dev->intf->needs_remote_wakeup = on;
2074b2d4b150SSteve Glendinning 
2075eb970ff0SMing Lei 	if (pdata->features & FEATURE_REMOTE_WAKEUP)
2076b2d4b150SSteve Glendinning 		return 0;
2077b2d4b150SSteve Glendinning 
2078eb970ff0SMing Lei 	/* this chip revision isn't capable of remote wakeup */
2079eb970ff0SMing Lei 	netdev_info(dev->net, "hardware isn't capable of remote wakeup\n");
2080b2d4b150SSteve Glendinning 
2081b2d4b150SSteve Glendinning 	if (on)
2082b2d4b150SSteve Glendinning 		usb_autopm_get_interface_no_resume(dev->intf);
2083b2d4b150SSteve Glendinning 	else
2084b2d4b150SSteve Glendinning 		usb_autopm_put_interface(dev->intf);
2085b2d4b150SSteve Glendinning 
2086b2d4b150SSteve Glendinning 	return 0;
2087b2d4b150SSteve Glendinning }
2088b2d4b150SSteve Glendinning 
20892f7ca802SSteve Glendinning static const struct driver_info smsc95xx_info = {
20902f7ca802SSteve Glendinning 	.description	= "smsc95xx USB 2.0 Ethernet",
20912f7ca802SSteve Glendinning 	.bind		= smsc95xx_bind,
20922f7ca802SSteve Glendinning 	.unbind		= smsc95xx_unbind,
20932f7ca802SSteve Glendinning 	.link_reset	= smsc95xx_link_reset,
20942f7ca802SSteve Glendinning 	.reset		= smsc95xx_reset,
20952f7ca802SSteve Glendinning 	.rx_fixup	= smsc95xx_rx_fixup,
20962f7ca802SSteve Glendinning 	.tx_fixup	= smsc95xx_tx_fixup,
20972f7ca802SSteve Glendinning 	.status		= smsc95xx_status,
2098b2d4b150SSteve Glendinning 	.manage_power	= smsc95xx_manage_power,
209907d69d42SPaolo Pisati 	.flags		= FLAG_ETHER | FLAG_SEND_ZLP | FLAG_LINK_INTR,
21002f7ca802SSteve Glendinning };
21012f7ca802SSteve Glendinning 
21022f7ca802SSteve Glendinning static const struct usb_device_id products[] = {
21032f7ca802SSteve Glendinning 	{
21042f7ca802SSteve Glendinning 		/* SMSC9500 USB Ethernet Device */
21052f7ca802SSteve Glendinning 		USB_DEVICE(0x0424, 0x9500),
21062f7ca802SSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21072f7ca802SSteve Glendinning 	},
2108726474b8SSteve Glendinning 	{
21096f41d12bSSteve Glendinning 		/* SMSC9505 USB Ethernet Device */
21106f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9505),
21116f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21126f41d12bSSteve Glendinning 	},
21136f41d12bSSteve Glendinning 	{
21146f41d12bSSteve Glendinning 		/* SMSC9500A USB Ethernet Device */
21156f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9E00),
21166f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21176f41d12bSSteve Glendinning 	},
21186f41d12bSSteve Glendinning 	{
21196f41d12bSSteve Glendinning 		/* SMSC9505A USB Ethernet Device */
21206f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9E01),
21216f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21226f41d12bSSteve Glendinning 	},
21236f41d12bSSteve Glendinning 	{
2124726474b8SSteve Glendinning 		/* SMSC9512/9514 USB Hub & Ethernet Device */
2125726474b8SSteve Glendinning 		USB_DEVICE(0x0424, 0xec00),
2126726474b8SSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
2127726474b8SSteve Glendinning 	},
21286f41d12bSSteve Glendinning 	{
21296f41d12bSSteve Glendinning 		/* SMSC9500 USB Ethernet Device (SAL10) */
21306f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9900),
21316f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21326f41d12bSSteve Glendinning 	},
21336f41d12bSSteve Glendinning 	{
21346f41d12bSSteve Glendinning 		/* SMSC9505 USB Ethernet Device (SAL10) */
21356f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9901),
21366f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21376f41d12bSSteve Glendinning 	},
21386f41d12bSSteve Glendinning 	{
21396f41d12bSSteve Glendinning 		/* SMSC9500A USB Ethernet Device (SAL10) */
21406f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9902),
21416f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21426f41d12bSSteve Glendinning 	},
21436f41d12bSSteve Glendinning 	{
21446f41d12bSSteve Glendinning 		/* SMSC9505A USB Ethernet Device (SAL10) */
21456f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9903),
21466f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21476f41d12bSSteve Glendinning 	},
21486f41d12bSSteve Glendinning 	{
21496f41d12bSSteve Glendinning 		/* SMSC9512/9514 USB Hub & Ethernet Device (SAL10) */
21506f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9904),
21516f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21526f41d12bSSteve Glendinning 	},
21536f41d12bSSteve Glendinning 	{
21546f41d12bSSteve Glendinning 		/* SMSC9500A USB Ethernet Device (HAL) */
21556f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9905),
21566f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21576f41d12bSSteve Glendinning 	},
21586f41d12bSSteve Glendinning 	{
21596f41d12bSSteve Glendinning 		/* SMSC9505A USB Ethernet Device (HAL) */
21606f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9906),
21616f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21626f41d12bSSteve Glendinning 	},
21636f41d12bSSteve Glendinning 	{
21646f41d12bSSteve Glendinning 		/* SMSC9500 USB Ethernet Device (Alternate ID) */
21656f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9907),
21666f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21676f41d12bSSteve Glendinning 	},
21686f41d12bSSteve Glendinning 	{
21696f41d12bSSteve Glendinning 		/* SMSC9500A USB Ethernet Device (Alternate ID) */
21706f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9908),
21716f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21726f41d12bSSteve Glendinning 	},
21736f41d12bSSteve Glendinning 	{
21746f41d12bSSteve Glendinning 		/* SMSC9512/9514 USB Hub & Ethernet Device (Alternate ID) */
21756f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9909),
21766f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21776f41d12bSSteve Glendinning 	},
217888edaa41SSteve Glendinning 	{
217988edaa41SSteve Glendinning 		/* SMSC LAN9530 USB Ethernet Device */
218088edaa41SSteve Glendinning 		USB_DEVICE(0x0424, 0x9530),
218188edaa41SSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
218288edaa41SSteve Glendinning 	},
218388edaa41SSteve Glendinning 	{
218488edaa41SSteve Glendinning 		/* SMSC LAN9730 USB Ethernet Device */
218588edaa41SSteve Glendinning 		USB_DEVICE(0x0424, 0x9730),
218688edaa41SSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
218788edaa41SSteve Glendinning 	},
218888edaa41SSteve Glendinning 	{
218988edaa41SSteve Glendinning 		/* SMSC LAN89530 USB Ethernet Device */
219088edaa41SSteve Glendinning 		USB_DEVICE(0x0424, 0x9E08),
219188edaa41SSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
219288edaa41SSteve Glendinning 	},
21932f7ca802SSteve Glendinning 	{ },		/* END */
21942f7ca802SSteve Glendinning };
21952f7ca802SSteve Glendinning MODULE_DEVICE_TABLE(usb, products);
21962f7ca802SSteve Glendinning 
21972f7ca802SSteve Glendinning static struct usb_driver smsc95xx_driver = {
21982f7ca802SSteve Glendinning 	.name		= "smsc95xx",
21992f7ca802SSteve Glendinning 	.id_table	= products,
22002f7ca802SSteve Glendinning 	.probe		= usbnet_probe,
2201b5a04475SSteve Glendinning 	.suspend	= smsc95xx_suspend,
2202e0e474a8SSteve Glendinning 	.resume		= smsc95xx_resume,
2203b4df480fSJoonyoung Shim 	.reset_resume	= smsc95xx_reset_resume,
22042f7ca802SSteve Glendinning 	.disconnect	= usbnet_disconnect,
2205e1f12eb6SSarah Sharp 	.disable_hub_initiated_lpm = 1,
2206b2d4b150SSteve Glendinning 	.supports_autosuspend = 1,
22072f7ca802SSteve Glendinning };
22082f7ca802SSteve Glendinning 
2209d632eb1bSGreg Kroah-Hartman module_usb_driver(smsc95xx_driver);
22102f7ca802SSteve Glendinning 
22112f7ca802SSteve Glendinning MODULE_AUTHOR("Nancy Lin");
221290b24cfbSSteve Glendinning MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>");
22132f7ca802SSteve Glendinning MODULE_DESCRIPTION("SMSC95XX USB 2.0 Ethernet Devices");
22142f7ca802SSteve Glendinning MODULE_LICENSE("GPL");
2215