xref: /openbmc/linux/drivers/net/usb/smsc95xx.c (revision ad90a73f0236c41f7a2dedc2e75c7b5a364eb93e)
11ccea77eSThomas 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 
264368be1caSAndre Edich static int smsc95xx_mdio_read_nopm(struct usbnet *dev, int idx)
265e5e3af83SSteve Glendinning {
266368be1caSAndre Edich 	struct mii_if_info *mii = &dev->mii;
267368be1caSAndre Edich 
268368be1caSAndre Edich 	return __smsc95xx_mdio_read(dev->net, mii->phy_id, idx, 1);
269e5e3af83SSteve Glendinning }
270e5e3af83SSteve Glendinning 
271368be1caSAndre Edich static void smsc95xx_mdio_write_nopm(struct usbnet *dev, int idx, int regval)
272e5e3af83SSteve Glendinning {
273368be1caSAndre Edich 	struct mii_if_info *mii = &dev->mii;
274368be1caSAndre Edich 
275368be1caSAndre Edich 	__smsc95xx_mdio_write(dev->net, mii->phy_id, idx, regval, 1);
276e5e3af83SSteve Glendinning }
277e5e3af83SSteve Glendinning 
278e5e3af83SSteve Glendinning static int smsc95xx_mdio_read(struct net_device *netdev, int phy_id, int idx)
279e5e3af83SSteve Glendinning {
280e5e3af83SSteve Glendinning 	return __smsc95xx_mdio_read(netdev, phy_id, idx, 0);
281e5e3af83SSteve Glendinning }
282e5e3af83SSteve Glendinning 
283e5e3af83SSteve Glendinning static void smsc95xx_mdio_write(struct net_device *netdev, int phy_id, int idx,
284e5e3af83SSteve Glendinning 				int regval)
285e5e3af83SSteve Glendinning {
286e5e3af83SSteve Glendinning 	__smsc95xx_mdio_write(netdev, phy_id, idx, regval, 0);
287e5e3af83SSteve Glendinning }
288e5e3af83SSteve Glendinning 
289769ea6d8SSteve Glendinning static int __must_check smsc95xx_wait_eeprom(struct usbnet *dev)
2902f7ca802SSteve Glendinning {
2912f7ca802SSteve Glendinning 	unsigned long start_time = jiffies;
2922f7ca802SSteve Glendinning 	u32 val;
293769ea6d8SSteve Glendinning 	int ret;
2942f7ca802SSteve Glendinning 
2952f7ca802SSteve Glendinning 	do {
296769ea6d8SSteve Glendinning 		ret = smsc95xx_read_reg(dev, E2P_CMD, &val);
297b052e073SSteve Glendinning 		if (ret < 0) {
298b052e073SSteve Glendinning 			netdev_warn(dev->net, "Error reading E2P_CMD\n");
299b052e073SSteve Glendinning 			return ret;
300b052e073SSteve Glendinning 		}
301b052e073SSteve Glendinning 
3022f7ca802SSteve Glendinning 		if (!(val & E2P_CMD_BUSY_) || (val & E2P_CMD_TIMEOUT_))
3032f7ca802SSteve Glendinning 			break;
3042f7ca802SSteve Glendinning 		udelay(40);
3052f7ca802SSteve Glendinning 	} while (!time_after(jiffies, start_time + HZ));
3062f7ca802SSteve Glendinning 
3072f7ca802SSteve Glendinning 	if (val & (E2P_CMD_TIMEOUT_ | E2P_CMD_BUSY_)) {
30860b86755SJoe Perches 		netdev_warn(dev->net, "EEPROM read operation timeout\n");
3092f7ca802SSteve Glendinning 		return -EIO;
3102f7ca802SSteve Glendinning 	}
3112f7ca802SSteve Glendinning 
3122f7ca802SSteve Glendinning 	return 0;
3132f7ca802SSteve Glendinning }
3142f7ca802SSteve Glendinning 
315769ea6d8SSteve Glendinning static int __must_check smsc95xx_eeprom_confirm_not_busy(struct usbnet *dev)
3162f7ca802SSteve Glendinning {
3172f7ca802SSteve Glendinning 	unsigned long start_time = jiffies;
3182f7ca802SSteve Glendinning 	u32 val;
319769ea6d8SSteve Glendinning 	int ret;
3202f7ca802SSteve Glendinning 
3212f7ca802SSteve Glendinning 	do {
322769ea6d8SSteve Glendinning 		ret = smsc95xx_read_reg(dev, E2P_CMD, &val);
323b052e073SSteve Glendinning 		if (ret < 0) {
324b052e073SSteve Glendinning 			netdev_warn(dev->net, "Error reading E2P_CMD\n");
325b052e073SSteve Glendinning 			return ret;
326b052e073SSteve Glendinning 		}
3272f7ca802SSteve Glendinning 
3282f7ca802SSteve Glendinning 		if (!(val & E2P_CMD_BUSY_))
3292f7ca802SSteve Glendinning 			return 0;
3302f7ca802SSteve Glendinning 
3312f7ca802SSteve Glendinning 		udelay(40);
3322f7ca802SSteve Glendinning 	} while (!time_after(jiffies, start_time + HZ));
3332f7ca802SSteve Glendinning 
33460b86755SJoe Perches 	netdev_warn(dev->net, "EEPROM is busy\n");
3352f7ca802SSteve Glendinning 	return -EIO;
3362f7ca802SSteve Glendinning }
3372f7ca802SSteve Glendinning 
3382f7ca802SSteve Glendinning static int smsc95xx_read_eeprom(struct usbnet *dev, u32 offset, u32 length,
3392f7ca802SSteve Glendinning 				u8 *data)
3402f7ca802SSteve Glendinning {
3412f7ca802SSteve Glendinning 	u32 val;
3422f7ca802SSteve Glendinning 	int i, ret;
3432f7ca802SSteve Glendinning 
3442f7ca802SSteve Glendinning 	BUG_ON(!dev);
3452f7ca802SSteve Glendinning 	BUG_ON(!data);
3462f7ca802SSteve Glendinning 
3472f7ca802SSteve Glendinning 	ret = smsc95xx_eeprom_confirm_not_busy(dev);
3482f7ca802SSteve Glendinning 	if (ret)
3492f7ca802SSteve Glendinning 		return ret;
3502f7ca802SSteve Glendinning 
3512f7ca802SSteve Glendinning 	for (i = 0; i < length; i++) {
3522f7ca802SSteve Glendinning 		val = E2P_CMD_BUSY_ | E2P_CMD_READ_ | (offset & E2P_CMD_ADDR_);
353769ea6d8SSteve Glendinning 		ret = smsc95xx_write_reg(dev, E2P_CMD, val);
354b052e073SSteve Glendinning 		if (ret < 0) {
355b052e073SSteve Glendinning 			netdev_warn(dev->net, "Error writing E2P_CMD\n");
356b052e073SSteve Glendinning 			return ret;
357b052e073SSteve Glendinning 		}
3582f7ca802SSteve Glendinning 
3592f7ca802SSteve Glendinning 		ret = smsc95xx_wait_eeprom(dev);
3602f7ca802SSteve Glendinning 		if (ret < 0)
3612f7ca802SSteve Glendinning 			return ret;
3622f7ca802SSteve Glendinning 
363769ea6d8SSteve Glendinning 		ret = smsc95xx_read_reg(dev, E2P_DATA, &val);
364b052e073SSteve Glendinning 		if (ret < 0) {
365b052e073SSteve Glendinning 			netdev_warn(dev->net, "Error reading E2P_DATA\n");
366b052e073SSteve Glendinning 			return ret;
367b052e073SSteve Glendinning 		}
3682f7ca802SSteve Glendinning 
3692f7ca802SSteve Glendinning 		data[i] = val & 0xFF;
3702f7ca802SSteve Glendinning 		offset++;
3712f7ca802SSteve Glendinning 	}
3722f7ca802SSteve Glendinning 
3732f7ca802SSteve Glendinning 	return 0;
3742f7ca802SSteve Glendinning }
3752f7ca802SSteve Glendinning 
3762f7ca802SSteve Glendinning static int smsc95xx_write_eeprom(struct usbnet *dev, u32 offset, u32 length,
3772f7ca802SSteve Glendinning 				 u8 *data)
3782f7ca802SSteve Glendinning {
3792f7ca802SSteve Glendinning 	u32 val;
3802f7ca802SSteve Glendinning 	int i, ret;
3812f7ca802SSteve Glendinning 
3822f7ca802SSteve Glendinning 	BUG_ON(!dev);
3832f7ca802SSteve Glendinning 	BUG_ON(!data);
3842f7ca802SSteve Glendinning 
3852f7ca802SSteve Glendinning 	ret = smsc95xx_eeprom_confirm_not_busy(dev);
3862f7ca802SSteve Glendinning 	if (ret)
3872f7ca802SSteve Glendinning 		return ret;
3882f7ca802SSteve Glendinning 
3892f7ca802SSteve Glendinning 	/* Issue write/erase enable command */
3902f7ca802SSteve Glendinning 	val = E2P_CMD_BUSY_ | E2P_CMD_EWEN_;
391769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, E2P_CMD, val);
392b052e073SSteve Glendinning 	if (ret < 0) {
393b052e073SSteve Glendinning 		netdev_warn(dev->net, "Error writing E2P_DATA\n");
394b052e073SSteve Glendinning 		return ret;
395b052e073SSteve Glendinning 	}
3962f7ca802SSteve Glendinning 
3972f7ca802SSteve Glendinning 	ret = smsc95xx_wait_eeprom(dev);
3982f7ca802SSteve Glendinning 	if (ret < 0)
3992f7ca802SSteve Glendinning 		return ret;
4002f7ca802SSteve Glendinning 
4012f7ca802SSteve Glendinning 	for (i = 0; i < length; i++) {
4022f7ca802SSteve Glendinning 
4032f7ca802SSteve Glendinning 		/* Fill data register */
4042f7ca802SSteve Glendinning 		val = data[i];
405769ea6d8SSteve Glendinning 		ret = smsc95xx_write_reg(dev, E2P_DATA, val);
406b052e073SSteve Glendinning 		if (ret < 0) {
407b052e073SSteve Glendinning 			netdev_warn(dev->net, "Error writing E2P_DATA\n");
408b052e073SSteve Glendinning 			return ret;
409b052e073SSteve Glendinning 		}
4102f7ca802SSteve Glendinning 
4112f7ca802SSteve Glendinning 		/* Send "write" command */
4122f7ca802SSteve Glendinning 		val = E2P_CMD_BUSY_ | E2P_CMD_WRITE_ | (offset & E2P_CMD_ADDR_);
413769ea6d8SSteve Glendinning 		ret = smsc95xx_write_reg(dev, E2P_CMD, val);
414b052e073SSteve Glendinning 		if (ret < 0) {
415b052e073SSteve Glendinning 			netdev_warn(dev->net, "Error writing E2P_CMD\n");
416b052e073SSteve Glendinning 			return ret;
417b052e073SSteve Glendinning 		}
4182f7ca802SSteve Glendinning 
4192f7ca802SSteve Glendinning 		ret = smsc95xx_wait_eeprom(dev);
4202f7ca802SSteve Glendinning 		if (ret < 0)
4212f7ca802SSteve Glendinning 			return ret;
4222f7ca802SSteve Glendinning 
4232f7ca802SSteve Glendinning 		offset++;
4242f7ca802SSteve Glendinning 	}
4252f7ca802SSteve Glendinning 
4262f7ca802SSteve Glendinning 	return 0;
4272f7ca802SSteve Glendinning }
4282f7ca802SSteve Glendinning 
429769ea6d8SSteve Glendinning static int __must_check smsc95xx_write_reg_async(struct usbnet *dev, u16 index,
4307b9e7580SSteve Glendinning 						 u32 data)
4312f7ca802SSteve Glendinning {
4321d74a6bdSSteve Glendinning 	const u16 size = 4;
4337b9e7580SSteve Glendinning 	u32 buf;
43472108fd2SMing Lei 	int ret;
4352f7ca802SSteve Glendinning 
4367b9e7580SSteve Glendinning 	buf = data;
4377b9e7580SSteve Glendinning 	cpu_to_le32s(&buf);
4387b9e7580SSteve Glendinning 
43972108fd2SMing Lei 	ret = usbnet_write_cmd_async(dev, USB_VENDOR_REQUEST_WRITE_REGISTER,
44072108fd2SMing Lei 				     USB_DIR_OUT | USB_TYPE_VENDOR |
44172108fd2SMing Lei 				     USB_RECIP_DEVICE,
4427b9e7580SSteve Glendinning 				     0, index, &buf, size);
44372108fd2SMing Lei 	if (ret < 0)
44472108fd2SMing Lei 		netdev_warn(dev->net, "Error write async cmd, sts=%d\n",
44572108fd2SMing Lei 			    ret);
44672108fd2SMing Lei 	return ret;
4472f7ca802SSteve Glendinning }
4482f7ca802SSteve Glendinning 
4492f7ca802SSteve Glendinning /* returns hash bit number for given MAC address
4502f7ca802SSteve Glendinning  * example:
4512f7ca802SSteve Glendinning  * 01 00 5E 00 00 01 -> returns bit number 31 */
4522f7ca802SSteve Glendinning static unsigned int smsc95xx_hash(char addr[ETH_ALEN])
4532f7ca802SSteve Glendinning {
4542f7ca802SSteve Glendinning 	return (ether_crc(ETH_ALEN, addr) >> 26) & 0x3f;
4552f7ca802SSteve Glendinning }
4562f7ca802SSteve Glendinning 
4572f7ca802SSteve Glendinning static void smsc95xx_set_multicast(struct net_device *netdev)
4582f7ca802SSteve Glendinning {
4592f7ca802SSteve Glendinning 	struct usbnet *dev = netdev_priv(netdev);
460*ad90a73fSAndre Edich 	struct smsc95xx_priv *pdata = dev->driver_priv;
4612f7ca802SSteve Glendinning 	unsigned long flags;
462769ea6d8SSteve Glendinning 	int ret;
4632f7ca802SSteve Glendinning 
4643c0f3c60SMarc Zyngier 	pdata->hash_hi = 0;
4653c0f3c60SMarc Zyngier 	pdata->hash_lo = 0;
4663c0f3c60SMarc Zyngier 
4672f7ca802SSteve Glendinning 	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
4682f7ca802SSteve Glendinning 
4692f7ca802SSteve Glendinning 	if (dev->net->flags & IFF_PROMISC) {
470a475f603SJoe Perches 		netif_dbg(dev, drv, dev->net, "promiscuous mode enabled\n");
4712f7ca802SSteve Glendinning 		pdata->mac_cr |= MAC_CR_PRMS_;
4722f7ca802SSteve Glendinning 		pdata->mac_cr &= ~(MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
4732f7ca802SSteve Glendinning 	} else if (dev->net->flags & IFF_ALLMULTI) {
474a475f603SJoe Perches 		netif_dbg(dev, drv, dev->net, "receive all multicast enabled\n");
4752f7ca802SSteve Glendinning 		pdata->mac_cr |= MAC_CR_MCPAS_;
4762f7ca802SSteve Glendinning 		pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_HPFILT_);
4774cd24eafSJiri Pirko 	} else if (!netdev_mc_empty(dev->net)) {
47822bedad3SJiri Pirko 		struct netdev_hw_addr *ha;
4792f7ca802SSteve Glendinning 
4802f7ca802SSteve Glendinning 		pdata->mac_cr |= MAC_CR_HPFILT_;
4812f7ca802SSteve Glendinning 		pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_);
4822f7ca802SSteve Glendinning 
48322bedad3SJiri Pirko 		netdev_for_each_mc_addr(ha, netdev) {
48422bedad3SJiri Pirko 			u32 bitnum = smsc95xx_hash(ha->addr);
4852f7ca802SSteve Glendinning 			u32 mask = 0x01 << (bitnum & 0x1F);
4862f7ca802SSteve Glendinning 			if (bitnum & 0x20)
4873c0f3c60SMarc Zyngier 				pdata->hash_hi |= mask;
4882f7ca802SSteve Glendinning 			else
4893c0f3c60SMarc Zyngier 				pdata->hash_lo |= mask;
4902f7ca802SSteve Glendinning 		}
4912f7ca802SSteve Glendinning 
492a475f603SJoe Perches 		netif_dbg(dev, drv, dev->net, "HASHH=0x%08X, HASHL=0x%08X\n",
4933c0f3c60SMarc Zyngier 				   pdata->hash_hi, pdata->hash_lo);
4942f7ca802SSteve Glendinning 	} else {
495a475f603SJoe Perches 		netif_dbg(dev, drv, dev->net, "receive own packets only\n");
4962f7ca802SSteve Glendinning 		pdata->mac_cr &=
4972f7ca802SSteve Glendinning 			~(MAC_CR_PRMS_ | MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
4982f7ca802SSteve Glendinning 	}
4992f7ca802SSteve Glendinning 
5002f7ca802SSteve Glendinning 	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
5012f7ca802SSteve Glendinning 
5022f7ca802SSteve Glendinning 	/* Initiate async writes, as we can't wait for completion here */
5037b9e7580SSteve Glendinning 	ret = smsc95xx_write_reg_async(dev, HASHH, pdata->hash_hi);
504b052e073SSteve Glendinning 	if (ret < 0)
505b052e073SSteve Glendinning 		netdev_warn(dev->net, "failed to initiate async write to HASHH\n");
506769ea6d8SSteve Glendinning 
5077b9e7580SSteve Glendinning 	ret = smsc95xx_write_reg_async(dev, HASHL, pdata->hash_lo);
508b052e073SSteve Glendinning 	if (ret < 0)
509b052e073SSteve Glendinning 		netdev_warn(dev->net, "failed to initiate async write to HASHL\n");
510769ea6d8SSteve Glendinning 
5117b9e7580SSteve Glendinning 	ret = smsc95xx_write_reg_async(dev, MAC_CR, pdata->mac_cr);
512b052e073SSteve Glendinning 	if (ret < 0)
513b052e073SSteve Glendinning 		netdev_warn(dev->net, "failed to initiate async write to MAC_CR\n");
5142f7ca802SSteve Glendinning }
5152f7ca802SSteve Glendinning 
516769ea6d8SSteve Glendinning static int smsc95xx_phy_update_flowcontrol(struct usbnet *dev, u8 duplex,
5172f7ca802SSteve Glendinning 					   u16 lcladv, u16 rmtadv)
5182f7ca802SSteve Glendinning {
5199c082731SNisar Sayed 	u32 flow = 0, afc_cfg;
5202f7ca802SSteve Glendinning 
5212f7ca802SSteve Glendinning 	int ret = smsc95xx_read_reg(dev, AFC_CFG, &afc_cfg);
522e360a8b4SSteve Glendinning 	if (ret < 0)
523b052e073SSteve Glendinning 		return ret;
5242f7ca802SSteve Glendinning 
5252f7ca802SSteve Glendinning 	if (duplex == DUPLEX_FULL) {
526bc02ff95SSteve Glendinning 		u8 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
5272f7ca802SSteve Glendinning 
5282f7ca802SSteve Glendinning 		if (cap & FLOW_CTRL_RX)
5292f7ca802SSteve Glendinning 			flow = 0xFFFF0002;
5302f7ca802SSteve Glendinning 
5319c082731SNisar Sayed 		if (cap & FLOW_CTRL_TX) {
5322f7ca802SSteve Glendinning 			afc_cfg |= 0xF;
5339c082731SNisar Sayed 			flow |= 0xFFFF0000;
5349c082731SNisar Sayed 		} else {
5352f7ca802SSteve Glendinning 			afc_cfg &= ~0xF;
5369c082731SNisar Sayed 		}
5372f7ca802SSteve Glendinning 
538a475f603SJoe Perches 		netif_dbg(dev, link, dev->net, "rx pause %s, tx pause %s\n",
53960b86755SJoe Perches 				   cap & FLOW_CTRL_RX ? "enabled" : "disabled",
54060b86755SJoe Perches 				   cap & FLOW_CTRL_TX ? "enabled" : "disabled");
5412f7ca802SSteve Glendinning 	} else {
542a475f603SJoe Perches 		netif_dbg(dev, link, dev->net, "half duplex\n");
5432f7ca802SSteve Glendinning 		afc_cfg |= 0xF;
5442f7ca802SSteve Glendinning 	}
5452f7ca802SSteve Glendinning 
546769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, FLOW, flow);
547b052e073SSteve Glendinning 	if (ret < 0)
548b052e073SSteve Glendinning 		return ret;
549e360a8b4SSteve Glendinning 
550e360a8b4SSteve Glendinning 	return smsc95xx_write_reg(dev, AFC_CFG, afc_cfg);
5512f7ca802SSteve Glendinning }
5522f7ca802SSteve Glendinning 
5532f7ca802SSteve Glendinning static int smsc95xx_link_reset(struct usbnet *dev)
5542f7ca802SSteve Glendinning {
555*ad90a73fSAndre Edich 	struct smsc95xx_priv *pdata = dev->driver_priv;
5562f7ca802SSteve Glendinning 	struct mii_if_info *mii = &dev->mii;
5578ae6dacaSDavid Decotigny 	struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
5582f7ca802SSteve Glendinning 	unsigned long flags;
5592f7ca802SSteve Glendinning 	u16 lcladv, rmtadv;
560769ea6d8SSteve Glendinning 	int ret;
5612f7ca802SSteve Glendinning 
5622f7ca802SSteve Glendinning 	/* clear interrupt status */
563769ea6d8SSteve Glendinning 	ret = smsc95xx_mdio_read(dev->net, mii->phy_id, PHY_INT_SRC);
564e360a8b4SSteve Glendinning 	if (ret < 0)
565b052e073SSteve Glendinning 		return ret;
566769ea6d8SSteve Glendinning 
567769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
568e360a8b4SSteve Glendinning 	if (ret < 0)
569b052e073SSteve Glendinning 		return ret;
5702f7ca802SSteve Glendinning 
5712f7ca802SSteve Glendinning 	mii_check_media(mii, 1, 1);
5722f7ca802SSteve Glendinning 	mii_ethtool_gset(&dev->mii, &ecmd);
5732f7ca802SSteve Glendinning 	lcladv = smsc95xx_mdio_read(dev->net, mii->phy_id, MII_ADVERTISE);
5742f7ca802SSteve Glendinning 	rmtadv = smsc95xx_mdio_read(dev->net, mii->phy_id, MII_LPA);
5752f7ca802SSteve Glendinning 
5768ae6dacaSDavid Decotigny 	netif_dbg(dev, link, dev->net,
5778ae6dacaSDavid Decotigny 		  "speed: %u duplex: %d lcladv: %04x rmtadv: %04x\n",
5788ae6dacaSDavid Decotigny 		  ethtool_cmd_speed(&ecmd), ecmd.duplex, lcladv, rmtadv);
5792f7ca802SSteve Glendinning 
5802f7ca802SSteve Glendinning 	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
5812f7ca802SSteve Glendinning 	if (ecmd.duplex != DUPLEX_FULL) {
5822f7ca802SSteve Glendinning 		pdata->mac_cr &= ~MAC_CR_FDPX_;
5832f7ca802SSteve Glendinning 		pdata->mac_cr |= MAC_CR_RCVOWN_;
5842f7ca802SSteve Glendinning 	} else {
5852f7ca802SSteve Glendinning 		pdata->mac_cr &= ~MAC_CR_RCVOWN_;
5862f7ca802SSteve Glendinning 		pdata->mac_cr |= MAC_CR_FDPX_;
5872f7ca802SSteve Glendinning 	}
5882f7ca802SSteve Glendinning 	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
5892f7ca802SSteve Glendinning 
590769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
591e360a8b4SSteve Glendinning 	if (ret < 0)
592b052e073SSteve Glendinning 		return ret;
5932f7ca802SSteve Glendinning 
594769ea6d8SSteve Glendinning 	ret = smsc95xx_phy_update_flowcontrol(dev, ecmd.duplex, lcladv, rmtadv);
595b052e073SSteve Glendinning 	if (ret < 0)
596b052e073SSteve Glendinning 		netdev_warn(dev->net, "Error updating PHY flow control\n");
5972f7ca802SSteve Glendinning 
598b052e073SSteve Glendinning 	return ret;
5992f7ca802SSteve Glendinning }
6002f7ca802SSteve Glendinning 
6012f7ca802SSteve Glendinning static void smsc95xx_status(struct usbnet *dev, struct urb *urb)
6022f7ca802SSteve Glendinning {
6032f7ca802SSteve Glendinning 	u32 intdata;
6042f7ca802SSteve Glendinning 
6052f7ca802SSteve Glendinning 	if (urb->actual_length != 4) {
60660b86755SJoe Perches 		netdev_warn(dev->net, "unexpected urb length %d\n",
60760b86755SJoe Perches 			    urb->actual_length);
6082f7ca802SSteve Glendinning 		return;
6092f7ca802SSteve Glendinning 	}
6102f7ca802SSteve Glendinning 
6116809d216SBen Dooks 	intdata = get_unaligned_le32(urb->transfer_buffer);
612a475f603SJoe Perches 	netif_dbg(dev, link, dev->net, "intdata: 0x%08X\n", intdata);
6132f7ca802SSteve Glendinning 
6142f7ca802SSteve Glendinning 	if (intdata & INT_ENP_PHY_INT_)
6152f7ca802SSteve Glendinning 		usbnet_defer_kevent(dev, EVENT_LINK_RESET);
6162f7ca802SSteve Glendinning 	else
61760b86755SJoe Perches 		netdev_warn(dev->net, "unexpected interrupt, intdata=0x%08X\n",
61860b86755SJoe Perches 			    intdata);
6192f7ca802SSteve Glendinning }
6202f7ca802SSteve Glendinning 
621d69d1694SChristoph Fritz static void set_carrier(struct usbnet *dev, bool link)
622d69d1694SChristoph Fritz {
623*ad90a73fSAndre Edich 	struct smsc95xx_priv *pdata = dev->driver_priv;
624d69d1694SChristoph Fritz 
625d69d1694SChristoph Fritz 	if (pdata->link_ok == link)
626d69d1694SChristoph Fritz 		return;
627d69d1694SChristoph Fritz 
628d69d1694SChristoph Fritz 	pdata->link_ok = link;
629d69d1694SChristoph Fritz 
630d69d1694SChristoph Fritz 	if (link)
631d69d1694SChristoph Fritz 		usbnet_link_change(dev, 1, 0);
632d69d1694SChristoph Fritz 	else
633d69d1694SChristoph Fritz 		usbnet_link_change(dev, 0, 0);
634d69d1694SChristoph Fritz }
635d69d1694SChristoph Fritz 
636d69d1694SChristoph Fritz static void check_carrier(struct work_struct *work)
637d69d1694SChristoph Fritz {
638d69d1694SChristoph Fritz 	struct smsc95xx_priv *pdata = container_of(work, struct smsc95xx_priv,
639d69d1694SChristoph Fritz 						carrier_check.work);
640d69d1694SChristoph Fritz 	struct usbnet *dev = pdata->dev;
641d69d1694SChristoph Fritz 	int ret;
642d69d1694SChristoph Fritz 
643d69d1694SChristoph Fritz 	if (pdata->suspend_flags != 0)
644d69d1694SChristoph Fritz 		return;
645d69d1694SChristoph Fritz 
646d69d1694SChristoph Fritz 	ret = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, MII_BMSR);
647d69d1694SChristoph Fritz 	if (ret < 0) {
648d69d1694SChristoph Fritz 		netdev_warn(dev->net, "Failed to read MII_BMSR\n");
649d69d1694SChristoph Fritz 		return;
650d69d1694SChristoph Fritz 	}
651d69d1694SChristoph Fritz 	if (ret & BMSR_LSTATUS)
652d69d1694SChristoph Fritz 		set_carrier(dev, 1);
653d69d1694SChristoph Fritz 	else
654d69d1694SChristoph Fritz 		set_carrier(dev, 0);
655d69d1694SChristoph Fritz 
656d69d1694SChristoph Fritz 	schedule_delayed_work(&pdata->carrier_check, CARRIER_CHECK_DELAY);
657d69d1694SChristoph Fritz }
658d69d1694SChristoph Fritz 
659f7b29271SSteve Glendinning /* Enable or disable Tx & Rx checksum offload engines */
660c8f44affSMichał Mirosław static int smsc95xx_set_features(struct net_device *netdev,
661c8f44affSMichał Mirosław 	netdev_features_t features)
6622f7ca802SSteve Glendinning {
66378e47fe4SMichał Mirosław 	struct usbnet *dev = netdev_priv(netdev);
6642f7ca802SSteve Glendinning 	u32 read_buf;
66578e47fe4SMichał Mirosław 	int ret;
66678e47fe4SMichał Mirosław 
66778e47fe4SMichał Mirosław 	ret = smsc95xx_read_reg(dev, COE_CR, &read_buf);
668e360a8b4SSteve Glendinning 	if (ret < 0)
669b052e073SSteve Glendinning 		return ret;
6702f7ca802SSteve Glendinning 
671fe0cd8caSNisar Sayed 	if (features & NETIF_F_IP_CSUM)
672f7b29271SSteve Glendinning 		read_buf |= Tx_COE_EN_;
673f7b29271SSteve Glendinning 	else
674f7b29271SSteve Glendinning 		read_buf &= ~Tx_COE_EN_;
675f7b29271SSteve Glendinning 
67678e47fe4SMichał Mirosław 	if (features & NETIF_F_RXCSUM)
6772f7ca802SSteve Glendinning 		read_buf |= Rx_COE_EN_;
6782f7ca802SSteve Glendinning 	else
6792f7ca802SSteve Glendinning 		read_buf &= ~Rx_COE_EN_;
6802f7ca802SSteve Glendinning 
6812f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, COE_CR, read_buf);
682e360a8b4SSteve Glendinning 	if (ret < 0)
683b052e073SSteve Glendinning 		return ret;
6842f7ca802SSteve Glendinning 
685a475f603SJoe Perches 	netif_dbg(dev, hw, dev->net, "COE_CR = 0x%08x\n", read_buf);
6862f7ca802SSteve Glendinning 	return 0;
6872f7ca802SSteve Glendinning }
6882f7ca802SSteve Glendinning 
6892f7ca802SSteve Glendinning static int smsc95xx_ethtool_get_eeprom_len(struct net_device *net)
6902f7ca802SSteve Glendinning {
6912f7ca802SSteve Glendinning 	return MAX_EEPROM_SIZE;
6922f7ca802SSteve Glendinning }
6932f7ca802SSteve Glendinning 
6942f7ca802SSteve Glendinning static int smsc95xx_ethtool_get_eeprom(struct net_device *netdev,
6952f7ca802SSteve Glendinning 				       struct ethtool_eeprom *ee, u8 *data)
6962f7ca802SSteve Glendinning {
6972f7ca802SSteve Glendinning 	struct usbnet *dev = netdev_priv(netdev);
6982f7ca802SSteve Glendinning 
6992f7ca802SSteve Glendinning 	ee->magic = LAN95XX_EEPROM_MAGIC;
7002f7ca802SSteve Glendinning 
7012f7ca802SSteve Glendinning 	return smsc95xx_read_eeprom(dev, ee->offset, ee->len, data);
7022f7ca802SSteve Glendinning }
7032f7ca802SSteve Glendinning 
7042f7ca802SSteve Glendinning static int smsc95xx_ethtool_set_eeprom(struct net_device *netdev,
7052f7ca802SSteve Glendinning 				       struct ethtool_eeprom *ee, u8 *data)
7062f7ca802SSteve Glendinning {
7072f7ca802SSteve Glendinning 	struct usbnet *dev = netdev_priv(netdev);
7082f7ca802SSteve Glendinning 
7092f7ca802SSteve Glendinning 	if (ee->magic != LAN95XX_EEPROM_MAGIC) {
71060b86755SJoe Perches 		netdev_warn(dev->net, "EEPROM: magic value mismatch, magic = 0x%x\n",
7112f7ca802SSteve Glendinning 			    ee->magic);
7122f7ca802SSteve Glendinning 		return -EINVAL;
7132f7ca802SSteve Glendinning 	}
7142f7ca802SSteve Glendinning 
7152f7ca802SSteve Glendinning 	return smsc95xx_write_eeprom(dev, ee->offset, ee->len, data);
7162f7ca802SSteve Glendinning }
7172f7ca802SSteve Glendinning 
7189fa32e94SEmeric Vigier static int smsc95xx_ethtool_getregslen(struct net_device *netdev)
7199fa32e94SEmeric Vigier {
7209fa32e94SEmeric Vigier 	/* all smsc95xx registers */
72196245317SSteve Glendinning 	return COE_CR - ID_REV + sizeof(u32);
7229fa32e94SEmeric Vigier }
7239fa32e94SEmeric Vigier 
7249fa32e94SEmeric Vigier static void
7259fa32e94SEmeric Vigier smsc95xx_ethtool_getregs(struct net_device *netdev, struct ethtool_regs *regs,
7269fa32e94SEmeric Vigier 			 void *buf)
7279fa32e94SEmeric Vigier {
7289fa32e94SEmeric Vigier 	struct usbnet *dev = netdev_priv(netdev);
729d348446bSDan Carpenter 	unsigned int i, j;
730d348446bSDan Carpenter 	int retval;
7319fa32e94SEmeric Vigier 	u32 *data = buf;
7329fa32e94SEmeric Vigier 
7339fa32e94SEmeric Vigier 	retval = smsc95xx_read_reg(dev, ID_REV, &regs->version);
7349fa32e94SEmeric Vigier 	if (retval < 0) {
7359fa32e94SEmeric Vigier 		netdev_warn(netdev, "REGS: cannot read ID_REV\n");
7369fa32e94SEmeric Vigier 		return;
7379fa32e94SEmeric Vigier 	}
7389fa32e94SEmeric Vigier 
7399fa32e94SEmeric Vigier 	for (i = ID_REV, j = 0; i <= COE_CR; i += (sizeof(u32)), j++) {
7409fa32e94SEmeric Vigier 		retval = smsc95xx_read_reg(dev, i, &data[j]);
7419fa32e94SEmeric Vigier 		if (retval < 0) {
7429fa32e94SEmeric Vigier 			netdev_warn(netdev, "REGS: cannot read reg[%x]\n", i);
7439fa32e94SEmeric Vigier 			return;
7449fa32e94SEmeric Vigier 		}
7459fa32e94SEmeric Vigier 	}
7469fa32e94SEmeric Vigier }
7479fa32e94SEmeric Vigier 
748e0e474a8SSteve Glendinning static void smsc95xx_ethtool_get_wol(struct net_device *net,
749e0e474a8SSteve Glendinning 				     struct ethtool_wolinfo *wolinfo)
750e0e474a8SSteve Glendinning {
751e0e474a8SSteve Glendinning 	struct usbnet *dev = netdev_priv(net);
752*ad90a73fSAndre Edich 	struct smsc95xx_priv *pdata = dev->driver_priv;
753e0e474a8SSteve Glendinning 
754e0e474a8SSteve Glendinning 	wolinfo->supported = SUPPORTED_WAKE;
755e0e474a8SSteve Glendinning 	wolinfo->wolopts = pdata->wolopts;
756e0e474a8SSteve Glendinning }
757e0e474a8SSteve Glendinning 
758e0e474a8SSteve Glendinning static int smsc95xx_ethtool_set_wol(struct net_device *net,
759e0e474a8SSteve Glendinning 				    struct ethtool_wolinfo *wolinfo)
760e0e474a8SSteve Glendinning {
761e0e474a8SSteve Glendinning 	struct usbnet *dev = netdev_priv(net);
762*ad90a73fSAndre Edich 	struct smsc95xx_priv *pdata = dev->driver_priv;
7633b14692cSSteve Glendinning 	int ret;
764e0e474a8SSteve Glendinning 
765c530c471SFlorian Fainelli 	if (wolinfo->wolopts & ~SUPPORTED_WAKE)
766c530c471SFlorian Fainelli 		return -EINVAL;
767c530c471SFlorian Fainelli 
768e0e474a8SSteve Glendinning 	pdata->wolopts = wolinfo->wolopts & SUPPORTED_WAKE;
7693b14692cSSteve Glendinning 
7703b14692cSSteve Glendinning 	ret = device_set_wakeup_enable(&dev->udev->dev, pdata->wolopts);
771b052e073SSteve Glendinning 	if (ret < 0)
772b052e073SSteve Glendinning 		netdev_warn(dev->net, "device_set_wakeup_enable error %d\n", ret);
7733b14692cSSteve Glendinning 
774b052e073SSteve Glendinning 	return ret;
775e0e474a8SSteve Glendinning }
776e0e474a8SSteve Glendinning 
77713722bbeSWoojung Huh static int get_mdix_status(struct net_device *net)
77813722bbeSWoojung Huh {
77913722bbeSWoojung Huh 	struct usbnet *dev = netdev_priv(net);
78013722bbeSWoojung Huh 	u32 val;
78113722bbeSWoojung Huh 	int buf;
78213722bbeSWoojung Huh 
78313722bbeSWoojung Huh 	buf = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, SPECIAL_CTRL_STS);
78413722bbeSWoojung Huh 	if (buf & SPECIAL_CTRL_STS_OVRRD_AMDIX_) {
78513722bbeSWoojung Huh 		if (buf & SPECIAL_CTRL_STS_AMDIX_ENABLE_)
78613722bbeSWoojung Huh 			return ETH_TP_MDI_AUTO;
78713722bbeSWoojung Huh 		else if (buf & SPECIAL_CTRL_STS_AMDIX_STATE_)
78813722bbeSWoojung Huh 			return ETH_TP_MDI_X;
78913722bbeSWoojung Huh 	} else {
79013722bbeSWoojung Huh 		buf = smsc95xx_read_reg(dev, STRAP_STATUS, &val);
79113722bbeSWoojung Huh 		if (val & STRAP_STATUS_AMDIX_EN_)
79213722bbeSWoojung Huh 			return ETH_TP_MDI_AUTO;
79313722bbeSWoojung Huh 	}
79413722bbeSWoojung Huh 
79513722bbeSWoojung Huh 	return ETH_TP_MDI;
79613722bbeSWoojung Huh }
79713722bbeSWoojung Huh 
79813722bbeSWoojung Huh static void set_mdix_status(struct net_device *net, __u8 mdix_ctrl)
79913722bbeSWoojung Huh {
80013722bbeSWoojung Huh 	struct usbnet *dev = netdev_priv(net);
801*ad90a73fSAndre Edich 	struct smsc95xx_priv *pdata = dev->driver_priv;
80213722bbeSWoojung Huh 	int buf;
80313722bbeSWoojung Huh 
80413722bbeSWoojung Huh 	if ((pdata->chip_id == ID_REV_CHIP_ID_9500A_) ||
80513722bbeSWoojung Huh 	    (pdata->chip_id == ID_REV_CHIP_ID_9530_) ||
80613722bbeSWoojung Huh 	    (pdata->chip_id == ID_REV_CHIP_ID_89530_) ||
80713722bbeSWoojung Huh 	    (pdata->chip_id == ID_REV_CHIP_ID_9730_)) {
80813722bbeSWoojung Huh 		/* Extend Manual AutoMDIX timer for 9500A/9500Ai */
80913722bbeSWoojung Huh 		buf = smsc95xx_mdio_read(dev->net, dev->mii.phy_id,
81013722bbeSWoojung Huh 					 PHY_EDPD_CONFIG);
81113722bbeSWoojung Huh 		buf |= PHY_EDPD_CONFIG_EXT_CROSSOVER_;
81213722bbeSWoojung Huh 		smsc95xx_mdio_write(dev->net, dev->mii.phy_id,
81313722bbeSWoojung Huh 				    PHY_EDPD_CONFIG, buf);
81413722bbeSWoojung Huh 	}
81513722bbeSWoojung Huh 
81613722bbeSWoojung Huh 	if (mdix_ctrl == ETH_TP_MDI) {
81713722bbeSWoojung Huh 		buf = smsc95xx_mdio_read(dev->net, dev->mii.phy_id,
81813722bbeSWoojung Huh 					 SPECIAL_CTRL_STS);
81913722bbeSWoojung Huh 		buf |= SPECIAL_CTRL_STS_OVRRD_AMDIX_;
82013722bbeSWoojung Huh 		buf &= ~(SPECIAL_CTRL_STS_AMDIX_ENABLE_ |
82113722bbeSWoojung Huh 			 SPECIAL_CTRL_STS_AMDIX_STATE_);
82213722bbeSWoojung Huh 		smsc95xx_mdio_write(dev->net, dev->mii.phy_id,
82313722bbeSWoojung Huh 				    SPECIAL_CTRL_STS, buf);
82413722bbeSWoojung Huh 	} else if (mdix_ctrl == ETH_TP_MDI_X) {
82513722bbeSWoojung Huh 		buf = smsc95xx_mdio_read(dev->net, dev->mii.phy_id,
82613722bbeSWoojung Huh 					 SPECIAL_CTRL_STS);
82713722bbeSWoojung Huh 		buf |= SPECIAL_CTRL_STS_OVRRD_AMDIX_;
82813722bbeSWoojung Huh 		buf &= ~(SPECIAL_CTRL_STS_AMDIX_ENABLE_ |
82913722bbeSWoojung Huh 			 SPECIAL_CTRL_STS_AMDIX_STATE_);
83013722bbeSWoojung Huh 		buf |= SPECIAL_CTRL_STS_AMDIX_STATE_;
83113722bbeSWoojung Huh 		smsc95xx_mdio_write(dev->net, dev->mii.phy_id,
83213722bbeSWoojung Huh 				    SPECIAL_CTRL_STS, buf);
83313722bbeSWoojung Huh 	} else if (mdix_ctrl == ETH_TP_MDI_AUTO) {
83413722bbeSWoojung Huh 		buf = smsc95xx_mdio_read(dev->net, dev->mii.phy_id,
83513722bbeSWoojung Huh 					 SPECIAL_CTRL_STS);
83613722bbeSWoojung Huh 		buf &= ~SPECIAL_CTRL_STS_OVRRD_AMDIX_;
83713722bbeSWoojung Huh 		buf &= ~(SPECIAL_CTRL_STS_AMDIX_ENABLE_ |
83813722bbeSWoojung Huh 			 SPECIAL_CTRL_STS_AMDIX_STATE_);
83913722bbeSWoojung Huh 		buf |= SPECIAL_CTRL_STS_AMDIX_ENABLE_;
84013722bbeSWoojung Huh 		smsc95xx_mdio_write(dev->net, dev->mii.phy_id,
84113722bbeSWoojung Huh 				    SPECIAL_CTRL_STS, buf);
84213722bbeSWoojung Huh 	}
84313722bbeSWoojung Huh 	pdata->mdix_ctrl = mdix_ctrl;
84413722bbeSWoojung Huh }
84513722bbeSWoojung Huh 
846eaf9a32aSPhilippe Reynes static int smsc95xx_get_link_ksettings(struct net_device *net,
847eaf9a32aSPhilippe Reynes 				       struct ethtool_link_ksettings *cmd)
84813722bbeSWoojung Huh {
84913722bbeSWoojung Huh 	struct usbnet *dev = netdev_priv(net);
850*ad90a73fSAndre Edich 	struct smsc95xx_priv *pdata = dev->driver_priv;
85113722bbeSWoojung Huh 	int retval;
85213722bbeSWoojung Huh 
853eaf9a32aSPhilippe Reynes 	retval = usbnet_get_link_ksettings(net, cmd);
85413722bbeSWoojung Huh 
855eaf9a32aSPhilippe Reynes 	cmd->base.eth_tp_mdix = pdata->mdix_ctrl;
856eaf9a32aSPhilippe Reynes 	cmd->base.eth_tp_mdix_ctrl = pdata->mdix_ctrl;
85713722bbeSWoojung Huh 
85813722bbeSWoojung Huh 	return retval;
85913722bbeSWoojung Huh }
86013722bbeSWoojung Huh 
861eaf9a32aSPhilippe Reynes static int smsc95xx_set_link_ksettings(struct net_device *net,
862eaf9a32aSPhilippe Reynes 				       const struct ethtool_link_ksettings *cmd)
86313722bbeSWoojung Huh {
86413722bbeSWoojung Huh 	struct usbnet *dev = netdev_priv(net);
865*ad90a73fSAndre Edich 	struct smsc95xx_priv *pdata = dev->driver_priv;
86613722bbeSWoojung Huh 	int retval;
86713722bbeSWoojung Huh 
868eaf9a32aSPhilippe Reynes 	if (pdata->mdix_ctrl != cmd->base.eth_tp_mdix_ctrl)
869eaf9a32aSPhilippe Reynes 		set_mdix_status(net, cmd->base.eth_tp_mdix_ctrl);
87013722bbeSWoojung Huh 
871eaf9a32aSPhilippe Reynes 	retval = usbnet_set_link_ksettings(net, cmd);
87213722bbeSWoojung Huh 
87313722bbeSWoojung Huh 	return retval;
87413722bbeSWoojung Huh }
87513722bbeSWoojung Huh 
8760fc0b732SStephen Hemminger static const struct ethtool_ops smsc95xx_ethtool_ops = {
8772f7ca802SSteve Glendinning 	.get_link	= usbnet_get_link,
8782f7ca802SSteve Glendinning 	.nway_reset	= usbnet_nway_reset,
8792f7ca802SSteve Glendinning 	.get_drvinfo	= usbnet_get_drvinfo,
8802f7ca802SSteve Glendinning 	.get_msglevel	= usbnet_get_msglevel,
8812f7ca802SSteve Glendinning 	.set_msglevel	= usbnet_set_msglevel,
8822f7ca802SSteve Glendinning 	.get_eeprom_len	= smsc95xx_ethtool_get_eeprom_len,
8832f7ca802SSteve Glendinning 	.get_eeprom	= smsc95xx_ethtool_get_eeprom,
8842f7ca802SSteve Glendinning 	.set_eeprom	= smsc95xx_ethtool_set_eeprom,
8859fa32e94SEmeric Vigier 	.get_regs_len	= smsc95xx_ethtool_getregslen,
8869fa32e94SEmeric Vigier 	.get_regs	= smsc95xx_ethtool_getregs,
887e0e474a8SSteve Glendinning 	.get_wol	= smsc95xx_ethtool_get_wol,
888e0e474a8SSteve Glendinning 	.set_wol	= smsc95xx_ethtool_set_wol,
889eaf9a32aSPhilippe Reynes 	.get_link_ksettings	= smsc95xx_get_link_ksettings,
890eaf9a32aSPhilippe Reynes 	.set_link_ksettings	= smsc95xx_set_link_ksettings,
891a8f5cb9eSPetr Kulhavy 	.get_ts_info	= ethtool_op_get_ts_info,
8922f7ca802SSteve Glendinning };
8932f7ca802SSteve Glendinning 
8942f7ca802SSteve Glendinning static int smsc95xx_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
8952f7ca802SSteve Glendinning {
8962f7ca802SSteve Glendinning 	struct usbnet *dev = netdev_priv(netdev);
8972f7ca802SSteve Glendinning 
8982f7ca802SSteve Glendinning 	if (!netif_running(netdev))
8992f7ca802SSteve Glendinning 		return -EINVAL;
9002f7ca802SSteve Glendinning 
9012f7ca802SSteve Glendinning 	return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
9022f7ca802SSteve Glendinning }
9032f7ca802SSteve Glendinning 
9042f7ca802SSteve Glendinning static void smsc95xx_init_mac_address(struct usbnet *dev)
9052f7ca802SSteve Glendinning {
906c489565bSArnd Bergmann 	const u8 *mac_addr;
907c489565bSArnd Bergmann 
908c489565bSArnd Bergmann 	/* maybe the boot loader passed the MAC address in devicetree */
909c489565bSArnd Bergmann 	mac_addr = of_get_mac_address(dev->udev->dev.of_node);
910adfb3cb2SPetr Štetiar 	if (!IS_ERR(mac_addr)) {
9115503a688SPetr Štetiar 		ether_addr_copy(dev->net->dev_addr, mac_addr);
912c489565bSArnd Bergmann 		return;
913c489565bSArnd Bergmann 	}
914c489565bSArnd Bergmann 
9152f7ca802SSteve Glendinning 	/* try reading mac address from EEPROM */
9162f7ca802SSteve Glendinning 	if (smsc95xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN,
9172f7ca802SSteve Glendinning 			dev->net->dev_addr) == 0) {
9182f7ca802SSteve Glendinning 		if (is_valid_ether_addr(dev->net->dev_addr)) {
9192f7ca802SSteve Glendinning 			/* eeprom values are valid so use them */
920a475f603SJoe Perches 			netif_dbg(dev, ifup, dev->net, "MAC address read from EEPROM\n");
9212f7ca802SSteve Glendinning 			return;
9222f7ca802SSteve Glendinning 		}
9232f7ca802SSteve Glendinning 	}
9242f7ca802SSteve Glendinning 
925c489565bSArnd Bergmann 	/* no useful static MAC address found. generate a random one */
926f2cedb63SDanny Kukawka 	eth_hw_addr_random(dev->net);
927c7e12eadSJoe Perches 	netif_dbg(dev, ifup, dev->net, "MAC address set to eth_random_addr\n");
9282f7ca802SSteve Glendinning }
9292f7ca802SSteve Glendinning 
9302f7ca802SSteve Glendinning static int smsc95xx_set_mac_address(struct usbnet *dev)
9312f7ca802SSteve Glendinning {
9322f7ca802SSteve Glendinning 	u32 addr_lo = dev->net->dev_addr[0] | dev->net->dev_addr[1] << 8 |
9332f7ca802SSteve Glendinning 		dev->net->dev_addr[2] << 16 | dev->net->dev_addr[3] << 24;
9342f7ca802SSteve Glendinning 	u32 addr_hi = dev->net->dev_addr[4] | dev->net->dev_addr[5] << 8;
9352f7ca802SSteve Glendinning 	int ret;
9362f7ca802SSteve Glendinning 
9372f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, ADDRL, addr_lo);
938b052e073SSteve Glendinning 	if (ret < 0)
939b052e073SSteve Glendinning 		return ret;
940e360a8b4SSteve Glendinning 
941e360a8b4SSteve Glendinning 	return smsc95xx_write_reg(dev, ADDRH, addr_hi);
9422f7ca802SSteve Glendinning }
9432f7ca802SSteve Glendinning 
9442f7ca802SSteve Glendinning /* starts the TX path */
945769ea6d8SSteve Glendinning static int smsc95xx_start_tx_path(struct usbnet *dev)
9462f7ca802SSteve Glendinning {
947*ad90a73fSAndre Edich 	struct smsc95xx_priv *pdata = dev->driver_priv;
9482f7ca802SSteve Glendinning 	unsigned long flags;
949769ea6d8SSteve Glendinning 	int ret;
9502f7ca802SSteve Glendinning 
9512f7ca802SSteve Glendinning 	/* Enable Tx at MAC */
9522f7ca802SSteve Glendinning 	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
9532f7ca802SSteve Glendinning 	pdata->mac_cr |= MAC_CR_TXEN_;
9542f7ca802SSteve Glendinning 	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
9552f7ca802SSteve Glendinning 
956769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
957e360a8b4SSteve Glendinning 	if (ret < 0)
958b052e073SSteve Glendinning 		return ret;
9592f7ca802SSteve Glendinning 
9602f7ca802SSteve Glendinning 	/* Enable Tx at SCSRs */
961e360a8b4SSteve Glendinning 	return smsc95xx_write_reg(dev, TX_CFG, TX_CFG_ON_);
9622f7ca802SSteve Glendinning }
9632f7ca802SSteve Glendinning 
9642f7ca802SSteve Glendinning /* Starts the Receive path */
965ec32115dSMing Lei static int smsc95xx_start_rx_path(struct usbnet *dev, int in_pm)
9662f7ca802SSteve Glendinning {
967*ad90a73fSAndre Edich 	struct smsc95xx_priv *pdata = dev->driver_priv;
9682f7ca802SSteve Glendinning 	unsigned long flags;
9692f7ca802SSteve Glendinning 
9702f7ca802SSteve Glendinning 	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
9712f7ca802SSteve Glendinning 	pdata->mac_cr |= MAC_CR_RXEN_;
9722f7ca802SSteve Glendinning 	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
9732f7ca802SSteve Glendinning 
974e360a8b4SSteve Glendinning 	return __smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr, in_pm);
9752f7ca802SSteve Glendinning }
9762f7ca802SSteve Glendinning 
9772f7ca802SSteve Glendinning static int smsc95xx_phy_initialize(struct usbnet *dev)
9782f7ca802SSteve Glendinning {
979769ea6d8SSteve Glendinning 	int bmcr, ret, timeout = 0;
980db443c44SSteve Glendinning 
9812f7ca802SSteve Glendinning 	/* Initialize MII structure */
9822f7ca802SSteve Glendinning 	dev->mii.dev = dev->net;
9832f7ca802SSteve Glendinning 	dev->mii.mdio_read = smsc95xx_mdio_read;
9842f7ca802SSteve Glendinning 	dev->mii.mdio_write = smsc95xx_mdio_write;
9852f7ca802SSteve Glendinning 	dev->mii.phy_id_mask = 0x1f;
9862f7ca802SSteve Glendinning 	dev->mii.reg_num_mask = 0x1f;
9872f7ca802SSteve Glendinning 	dev->mii.phy_id = SMSC95XX_INTERNAL_PHY_ID;
9882f7ca802SSteve Glendinning 
989db443c44SSteve Glendinning 	/* reset phy and wait for reset to complete */
9902f7ca802SSteve Glendinning 	smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
991db443c44SSteve Glendinning 
992db443c44SSteve Glendinning 	do {
993db443c44SSteve Glendinning 		msleep(10);
994db443c44SSteve Glendinning 		bmcr = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, MII_BMCR);
995db443c44SSteve Glendinning 		timeout++;
996d9460920SRabin Vincent 	} while ((bmcr & BMCR_RESET) && (timeout < 100));
997db443c44SSteve Glendinning 
998db443c44SSteve Glendinning 	if (timeout >= 100) {
999db443c44SSteve Glendinning 		netdev_warn(dev->net, "timeout on PHY Reset");
1000db443c44SSteve Glendinning 		return -EIO;
1001db443c44SSteve Glendinning 	}
1002db443c44SSteve Glendinning 
10032f7ca802SSteve Glendinning 	smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
10042f7ca802SSteve Glendinning 		ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP |
10052f7ca802SSteve Glendinning 		ADVERTISE_PAUSE_ASYM);
10062f7ca802SSteve Glendinning 
10072f7ca802SSteve Glendinning 	/* read to clear */
1008769ea6d8SSteve Glendinning 	ret = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, PHY_INT_SRC);
1009b052e073SSteve Glendinning 	if (ret < 0) {
1010b052e073SSteve Glendinning 		netdev_warn(dev->net, "Failed to read PHY_INT_SRC during init\n");
1011b052e073SSteve Glendinning 		return ret;
1012b052e073SSteve Glendinning 	}
10132f7ca802SSteve Glendinning 
10142f7ca802SSteve Glendinning 	smsc95xx_mdio_write(dev->net, dev->mii.phy_id, PHY_INT_MASK,
10152f7ca802SSteve Glendinning 		PHY_INT_MASK_DEFAULT_);
10162f7ca802SSteve Glendinning 	mii_nway_restart(&dev->mii);
10172f7ca802SSteve Glendinning 
1018a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net, "phy initialised successfully\n");
10192f7ca802SSteve Glendinning 	return 0;
10202f7ca802SSteve Glendinning }
10212f7ca802SSteve Glendinning 
10222f7ca802SSteve Glendinning static int smsc95xx_reset(struct usbnet *dev)
10232f7ca802SSteve Glendinning {
1024*ad90a73fSAndre Edich 	struct smsc95xx_priv *pdata = dev->driver_priv;
10252f7ca802SSteve Glendinning 	u32 read_buf, write_buf, burst_cap;
10262f7ca802SSteve Glendinning 	int ret = 0, timeout;
10272f7ca802SSteve Glendinning 
1028a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net, "entering smsc95xx_reset\n");
10292f7ca802SSteve Glendinning 
10304436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, HW_CFG, HW_CFG_LRST_);
1031e360a8b4SSteve Glendinning 	if (ret < 0)
1032b052e073SSteve Glendinning 		return ret;
10332f7ca802SSteve Glendinning 
10342f7ca802SSteve Glendinning 	timeout = 0;
10352f7ca802SSteve Glendinning 	do {
1036cf2acec2SSteve Glendinning 		msleep(10);
10372f7ca802SSteve Glendinning 		ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
1038e360a8b4SSteve Glendinning 		if (ret < 0)
1039b052e073SSteve Glendinning 			return ret;
10402f7ca802SSteve Glendinning 		timeout++;
10412f7ca802SSteve Glendinning 	} while ((read_buf & HW_CFG_LRST_) && (timeout < 100));
10422f7ca802SSteve Glendinning 
10432f7ca802SSteve Glendinning 	if (timeout >= 100) {
104460b86755SJoe Perches 		netdev_warn(dev->net, "timeout waiting for completion of Lite Reset\n");
10452f7ca802SSteve Glendinning 		return ret;
10462f7ca802SSteve Glendinning 	}
10472f7ca802SSteve Glendinning 
10484436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, PM_CTRL, PM_CTL_PHY_RST_);
1049e360a8b4SSteve Glendinning 	if (ret < 0)
1050b052e073SSteve Glendinning 		return ret;
10512f7ca802SSteve Glendinning 
10522f7ca802SSteve Glendinning 	timeout = 0;
10532f7ca802SSteve Glendinning 	do {
1054cf2acec2SSteve Glendinning 		msleep(10);
10552f7ca802SSteve Glendinning 		ret = smsc95xx_read_reg(dev, PM_CTRL, &read_buf);
1056e360a8b4SSteve Glendinning 		if (ret < 0)
1057b052e073SSteve Glendinning 			return ret;
10582f7ca802SSteve Glendinning 		timeout++;
10592f7ca802SSteve Glendinning 	} while ((read_buf & PM_CTL_PHY_RST_) && (timeout < 100));
10602f7ca802SSteve Glendinning 
10612f7ca802SSteve Glendinning 	if (timeout >= 100) {
106260b86755SJoe Perches 		netdev_warn(dev->net, "timeout waiting for PHY Reset\n");
10632f7ca802SSteve Glendinning 		return ret;
10642f7ca802SSteve Glendinning 	}
10652f7ca802SSteve Glendinning 
10662f7ca802SSteve Glendinning 	ret = smsc95xx_set_mac_address(dev);
10672f7ca802SSteve Glendinning 	if (ret < 0)
10682f7ca802SSteve Glendinning 		return ret;
10692f7ca802SSteve Glendinning 
10701e1d7412SJoe Perches 	netif_dbg(dev, ifup, dev->net, "MAC Address: %pM\n",
10711e1d7412SJoe Perches 		  dev->net->dev_addr);
10722f7ca802SSteve Glendinning 
10732f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
1074e360a8b4SSteve Glendinning 	if (ret < 0)
1075b052e073SSteve Glendinning 		return ret;
10762f7ca802SSteve Glendinning 
10771e1d7412SJoe Perches 	netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG : 0x%08x\n",
10781e1d7412SJoe Perches 		  read_buf);
10792f7ca802SSteve Glendinning 
10802f7ca802SSteve Glendinning 	read_buf |= HW_CFG_BIR_;
10812f7ca802SSteve Glendinning 
10822f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
1083e360a8b4SSteve Glendinning 	if (ret < 0)
1084b052e073SSteve Glendinning 		return ret;
10852f7ca802SSteve Glendinning 
10862f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
1087e360a8b4SSteve Glendinning 	if (ret < 0)
1088b052e073SSteve Glendinning 		return ret;
1089b052e073SSteve Glendinning 
1090a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net,
1091a475f603SJoe Perches 		  "Read Value from HW_CFG after writing HW_CFG_BIR_: 0x%08x\n",
109260b86755SJoe Perches 		  read_buf);
10932f7ca802SSteve Glendinning 
10942f7ca802SSteve Glendinning 	if (!turbo_mode) {
10952f7ca802SSteve Glendinning 		burst_cap = 0;
10962f7ca802SSteve Glendinning 		dev->rx_urb_size = MAX_SINGLE_PACKET_SIZE;
10972f7ca802SSteve Glendinning 	} else if (dev->udev->speed == USB_SPEED_HIGH) {
10982f7ca802SSteve Glendinning 		burst_cap = DEFAULT_HS_BURST_CAP_SIZE / HS_USB_PKT_SIZE;
10992f7ca802SSteve Glendinning 		dev->rx_urb_size = DEFAULT_HS_BURST_CAP_SIZE;
11002f7ca802SSteve Glendinning 	} else {
11012f7ca802SSteve Glendinning 		burst_cap = DEFAULT_FS_BURST_CAP_SIZE / FS_USB_PKT_SIZE;
11022f7ca802SSteve Glendinning 		dev->rx_urb_size = DEFAULT_FS_BURST_CAP_SIZE;
11032f7ca802SSteve Glendinning 	}
11042f7ca802SSteve Glendinning 
11051e1d7412SJoe Perches 	netif_dbg(dev, ifup, dev->net, "rx_urb_size=%ld\n",
11061e1d7412SJoe Perches 		  (ulong)dev->rx_urb_size);
11072f7ca802SSteve Glendinning 
11082f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, BURST_CAP, burst_cap);
1109e360a8b4SSteve Glendinning 	if (ret < 0)
1110b052e073SSteve Glendinning 		return ret;
11112f7ca802SSteve Glendinning 
11122f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, BURST_CAP, &read_buf);
1113e360a8b4SSteve Glendinning 	if (ret < 0)
1114b052e073SSteve Glendinning 		return ret;
1115769ea6d8SSteve Glendinning 
1116a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net,
1117a475f603SJoe Perches 		  "Read Value from BURST_CAP after writing: 0x%08x\n",
11182f7ca802SSteve Glendinning 		  read_buf);
11192f7ca802SSteve Glendinning 
11204436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, BULK_IN_DLY, DEFAULT_BULK_IN_DELAY);
1121e360a8b4SSteve Glendinning 	if (ret < 0)
1122b052e073SSteve Glendinning 		return ret;
11232f7ca802SSteve Glendinning 
11242f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, BULK_IN_DLY, &read_buf);
1125e360a8b4SSteve Glendinning 	if (ret < 0)
1126b052e073SSteve Glendinning 		return ret;
1127769ea6d8SSteve Glendinning 
1128a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net,
1129a475f603SJoe Perches 		  "Read Value from BULK_IN_DLY after writing: 0x%08x\n",
113060b86755SJoe Perches 		  read_buf);
11312f7ca802SSteve Glendinning 
11322f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
1133e360a8b4SSteve Glendinning 	if (ret < 0)
1134b052e073SSteve Glendinning 		return ret;
1135769ea6d8SSteve Glendinning 
11361e1d7412SJoe Perches 	netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG: 0x%08x\n",
11371e1d7412SJoe Perches 		  read_buf);
11382f7ca802SSteve Glendinning 
11392f7ca802SSteve Glendinning 	if (turbo_mode)
11402f7ca802SSteve Glendinning 		read_buf |= (HW_CFG_MEF_ | HW_CFG_BCE_);
11412f7ca802SSteve Glendinning 
11422f7ca802SSteve Glendinning 	read_buf &= ~HW_CFG_RXDOFF_;
11432f7ca802SSteve Glendinning 
11442f7ca802SSteve Glendinning 	/* set Rx data offset=2, Make IP header aligns on word boundary. */
11452f7ca802SSteve Glendinning 	read_buf |= NET_IP_ALIGN << 9;
11462f7ca802SSteve Glendinning 
11472f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
1148e360a8b4SSteve Glendinning 	if (ret < 0)
1149b052e073SSteve Glendinning 		return ret;
11502f7ca802SSteve Glendinning 
11512f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
1152e360a8b4SSteve Glendinning 	if (ret < 0)
1153b052e073SSteve Glendinning 		return ret;
1154769ea6d8SSteve Glendinning 
1155a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net,
1156a475f603SJoe Perches 		  "Read Value from HW_CFG after writing: 0x%08x\n", read_buf);
11572f7ca802SSteve Glendinning 
11584436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
1159e360a8b4SSteve Glendinning 	if (ret < 0)
1160b052e073SSteve Glendinning 		return ret;
11612f7ca802SSteve Glendinning 
11622f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, ID_REV, &read_buf);
1163e360a8b4SSteve Glendinning 	if (ret < 0)
1164b052e073SSteve Glendinning 		return ret;
1165a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net, "ID_REV = 0x%08x\n", read_buf);
11662f7ca802SSteve Glendinning 
1167f293501cSSteve Glendinning 	/* Configure GPIO pins as LED outputs */
1168f293501cSSteve Glendinning 	write_buf = LED_GPIO_CFG_SPD_LED | LED_GPIO_CFG_LNK_LED |
1169f293501cSSteve Glendinning 		LED_GPIO_CFG_FDX_LED;
1170f293501cSSteve Glendinning 	ret = smsc95xx_write_reg(dev, LED_GPIO_CFG, write_buf);
1171e360a8b4SSteve Glendinning 	if (ret < 0)
1172b052e073SSteve Glendinning 		return ret;
1173f293501cSSteve Glendinning 
11742f7ca802SSteve Glendinning 	/* Init Tx */
11754436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, FLOW, 0);
1176e360a8b4SSteve Glendinning 	if (ret < 0)
1177b052e073SSteve Glendinning 		return ret;
11782f7ca802SSteve Glendinning 
11794436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, AFC_CFG, AFC_CFG_DEFAULT);
1180e360a8b4SSteve Glendinning 	if (ret < 0)
1181b052e073SSteve Glendinning 		return ret;
11822f7ca802SSteve Glendinning 
11832f7ca802SSteve Glendinning 	/* Don't need mac_cr_lock during initialisation */
11842f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, MAC_CR, &pdata->mac_cr);
1185e360a8b4SSteve Glendinning 	if (ret < 0)
1186b052e073SSteve Glendinning 		return ret;
11872f7ca802SSteve Glendinning 
11882f7ca802SSteve Glendinning 	/* Init Rx */
11892f7ca802SSteve Glendinning 	/* Set Vlan */
11904436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, VLAN1, (u32)ETH_P_8021Q);
1191e360a8b4SSteve Glendinning 	if (ret < 0)
1192b052e073SSteve Glendinning 		return ret;
11932f7ca802SSteve Glendinning 
1194f7b29271SSteve Glendinning 	/* Enable or disable checksum offload engines */
1195769ea6d8SSteve Glendinning 	ret = smsc95xx_set_features(dev->net, dev->net->features);
1196b052e073SSteve Glendinning 	if (ret < 0) {
1197b052e073SSteve Glendinning 		netdev_warn(dev->net, "Failed to set checksum offload features\n");
1198b052e073SSteve Glendinning 		return ret;
1199b052e073SSteve Glendinning 	}
12002f7ca802SSteve Glendinning 
12012f7ca802SSteve Glendinning 	smsc95xx_set_multicast(dev->net);
12022f7ca802SSteve Glendinning 
1203769ea6d8SSteve Glendinning 	ret = smsc95xx_phy_initialize(dev);
1204b052e073SSteve Glendinning 	if (ret < 0) {
1205b052e073SSteve Glendinning 		netdev_warn(dev->net, "Failed to init PHY\n");
1206b052e073SSteve Glendinning 		return ret;
1207b052e073SSteve Glendinning 	}
12082f7ca802SSteve Glendinning 
12092f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, INT_EP_CTL, &read_buf);
1210e360a8b4SSteve Glendinning 	if (ret < 0)
1211b052e073SSteve Glendinning 		return ret;
12122f7ca802SSteve Glendinning 
12132f7ca802SSteve Glendinning 	/* enable PHY interrupts */
12142f7ca802SSteve Glendinning 	read_buf |= INT_EP_CTL_PHY_INT_;
12152f7ca802SSteve Glendinning 
12162f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, INT_EP_CTL, read_buf);
1217e360a8b4SSteve Glendinning 	if (ret < 0)
1218b052e073SSteve Glendinning 		return ret;
12192f7ca802SSteve Glendinning 
1220769ea6d8SSteve Glendinning 	ret = smsc95xx_start_tx_path(dev);
1221b052e073SSteve Glendinning 	if (ret < 0) {
1222b052e073SSteve Glendinning 		netdev_warn(dev->net, "Failed to start TX path\n");
1223b052e073SSteve Glendinning 		return ret;
1224b052e073SSteve Glendinning 	}
1225769ea6d8SSteve Glendinning 
1226ec32115dSMing Lei 	ret = smsc95xx_start_rx_path(dev, 0);
1227b052e073SSteve Glendinning 	if (ret < 0) {
1228b052e073SSteve Glendinning 		netdev_warn(dev->net, "Failed to start RX path\n");
1229b052e073SSteve Glendinning 		return ret;
1230b052e073SSteve Glendinning 	}
12312f7ca802SSteve Glendinning 
1232a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net, "smsc95xx_reset, return 0\n");
12332f7ca802SSteve Glendinning 	return 0;
12342f7ca802SSteve Glendinning }
12352f7ca802SSteve Glendinning 
123663e77b39SStephen Hemminger static const struct net_device_ops smsc95xx_netdev_ops = {
123763e77b39SStephen Hemminger 	.ndo_open		= usbnet_open,
123863e77b39SStephen Hemminger 	.ndo_stop		= usbnet_stop,
123963e77b39SStephen Hemminger 	.ndo_start_xmit		= usbnet_start_xmit,
124063e77b39SStephen Hemminger 	.ndo_tx_timeout		= usbnet_tx_timeout,
124163e77b39SStephen Hemminger 	.ndo_change_mtu		= usbnet_change_mtu,
1242c8b5d129SGreg Ungerer 	.ndo_get_stats64	= usbnet_get_stats64,
124363e77b39SStephen Hemminger 	.ndo_set_mac_address 	= eth_mac_addr,
124463e77b39SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
124563e77b39SStephen Hemminger 	.ndo_do_ioctl 		= smsc95xx_ioctl,
1246afc4b13dSJiri Pirko 	.ndo_set_rx_mode	= smsc95xx_set_multicast,
124778e47fe4SMichał Mirosław 	.ndo_set_features	= smsc95xx_set_features,
124863e77b39SStephen Hemminger };
124963e77b39SStephen Hemminger 
12502f7ca802SSteve Glendinning static int smsc95xx_bind(struct usbnet *dev, struct usb_interface *intf)
12512f7ca802SSteve Glendinning {
1252*ad90a73fSAndre Edich 	struct smsc95xx_priv *pdata;
1253bbd9f9eeSSteve Glendinning 	u32 val;
12542f7ca802SSteve Glendinning 	int ret;
12552f7ca802SSteve Glendinning 
12562f7ca802SSteve Glendinning 	printk(KERN_INFO SMSC_CHIPNAME " v" SMSC_DRIVER_VERSION "\n");
12572f7ca802SSteve Glendinning 
12582f7ca802SSteve Glendinning 	ret = usbnet_get_endpoints(dev, intf);
1259b052e073SSteve Glendinning 	if (ret < 0) {
1260b052e073SSteve Glendinning 		netdev_warn(dev->net, "usbnet_get_endpoints failed: %d\n", ret);
1261b052e073SSteve Glendinning 		return ret;
1262b052e073SSteve Glendinning 	}
12632f7ca802SSteve Glendinning 
1264*ad90a73fSAndre Edich 	pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
126538673c82SJoe Perches 	if (!pdata)
12662f7ca802SSteve Glendinning 		return -ENOMEM;
12672f7ca802SSteve Glendinning 
1268*ad90a73fSAndre Edich 	dev->driver_priv = pdata;
1269*ad90a73fSAndre Edich 
12702f7ca802SSteve Glendinning 	spin_lock_init(&pdata->mac_cr_lock);
12712f7ca802SSteve Glendinning 
1272fe0cd8caSNisar Sayed 	/* LAN95xx devices do not alter the computed checksum of 0 to 0xffff.
1273fe0cd8caSNisar Sayed 	 * RFC 2460, ipv6 UDP calculated checksum yields a result of zero must
1274fe0cd8caSNisar Sayed 	 * be changed to 0xffff. RFC 768, ipv4 UDP computed checksum is zero,
1275fe0cd8caSNisar Sayed 	 * it is transmitted as all ones. The zero transmitted checksum means
1276fe0cd8caSNisar Sayed 	 * transmitter generated no checksum. Hence, enable csum offload only
1277fe0cd8caSNisar Sayed 	 * for ipv4 packets.
1278fe0cd8caSNisar Sayed 	 */
127978e47fe4SMichał Mirosław 	if (DEFAULT_TX_CSUM_ENABLE)
1280fe0cd8caSNisar Sayed 		dev->net->features |= NETIF_F_IP_CSUM;
128178e47fe4SMichał Mirosław 	if (DEFAULT_RX_CSUM_ENABLE)
128278e47fe4SMichał Mirosław 		dev->net->features |= NETIF_F_RXCSUM;
128378e47fe4SMichał Mirosław 
1284fe0cd8caSNisar Sayed 	dev->net->hw_features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
1285810eeb1fSBen Dooks 	set_bit(EVENT_NO_IP_ALIGN, &dev->flags);
12862f7ca802SSteve Glendinning 
1287f4e8ab7cSBernard Blackham 	smsc95xx_init_mac_address(dev);
1288f4e8ab7cSBernard Blackham 
12892f7ca802SSteve Glendinning 	/* Init all registers */
12902f7ca802SSteve Glendinning 	ret = smsc95xx_reset(dev);
12917c8b1e85SAndre Edich 	if (ret)
12927c8b1e85SAndre Edich 		goto free_pdata;
12932f7ca802SSteve Glendinning 
1294bbd9f9eeSSteve Glendinning 	/* detect device revision as different features may be available */
1295bbd9f9eeSSteve Glendinning 	ret = smsc95xx_read_reg(dev, ID_REV, &val);
1296e360a8b4SSteve Glendinning 	if (ret < 0)
12973ed58f96SAndre Edich 		goto free_pdata;
12983ed58f96SAndre Edich 
1299bbd9f9eeSSteve Glendinning 	val >>= 16;
130013722bbeSWoojung Huh 	pdata->chip_id = val;
130113722bbeSWoojung Huh 	pdata->mdix_ctrl = get_mdix_status(dev->net);
13029ebca507SSteve Glendinning 
13039ebca507SSteve Glendinning 	if ((val == ID_REV_CHIP_ID_9500A_) || (val == ID_REV_CHIP_ID_9530_) ||
13049ebca507SSteve Glendinning 	    (val == ID_REV_CHIP_ID_89530_) || (val == ID_REV_CHIP_ID_9730_))
13059ebca507SSteve Glendinning 		pdata->features = (FEATURE_8_WAKEUP_FILTERS |
13069ebca507SSteve Glendinning 			FEATURE_PHY_NLP_CROSSOVER |
1307eb970ff0SMing Lei 			FEATURE_REMOTE_WAKEUP);
13089ebca507SSteve Glendinning 	else if (val == ID_REV_CHIP_ID_9512_)
13099ebca507SSteve Glendinning 		pdata->features = FEATURE_8_WAKEUP_FILTERS;
1310bbd9f9eeSSteve Glendinning 
131163e77b39SStephen Hemminger 	dev->net->netdev_ops = &smsc95xx_netdev_ops;
13122f7ca802SSteve Glendinning 	dev->net->ethtool_ops = &smsc95xx_ethtool_ops;
13132f7ca802SSteve Glendinning 	dev->net->flags |= IFF_MULTICAST;
131478e47fe4SMichał Mirosław 	dev->net->hard_header_len += SMSC95XX_TX_OVERHEAD_CSUM;
131585b18b02SStefan Wahren 	dev->net->min_mtu = ETH_MIN_MTU;
131685b18b02SStefan Wahren 	dev->net->max_mtu = ETH_DATA_LEN;
13179bbf5660SStephane Fillod 	dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
1318d69d1694SChristoph Fritz 
1319d69d1694SChristoph Fritz 	pdata->dev = dev;
1320d69d1694SChristoph Fritz 	INIT_DELAYED_WORK(&pdata->carrier_check, check_carrier);
1321d69d1694SChristoph Fritz 	schedule_delayed_work(&pdata->carrier_check, CARRIER_CHECK_DELAY);
1322d69d1694SChristoph Fritz 
13232f7ca802SSteve Glendinning 	return 0;
13247c8b1e85SAndre Edich 
13257c8b1e85SAndre Edich free_pdata:
13267c8b1e85SAndre Edich 	kfree(pdata);
13277c8b1e85SAndre Edich 	return ret;
13282f7ca802SSteve Glendinning }
13292f7ca802SSteve Glendinning 
13302f7ca802SSteve Glendinning static void smsc95xx_unbind(struct usbnet *dev, struct usb_interface *intf)
13312f7ca802SSteve Glendinning {
1332*ad90a73fSAndre Edich 	struct smsc95xx_priv *pdata = dev->driver_priv;
1333d69d1694SChristoph Fritz 
1334b835a71eSTuomas Tynkkynen 	cancel_delayed_work_sync(&pdata->carrier_check);
1335a475f603SJoe Perches 	netif_dbg(dev, ifdown, dev->net, "free pdata\n");
13362f7ca802SSteve Glendinning 	kfree(pdata);
13372f7ca802SSteve Glendinning }
13382f7ca802SSteve Glendinning 
1339068bb1a7SSteve Glendinning static u32 smsc_crc(const u8 *buffer, size_t len, int filter)
1340bbd9f9eeSSteve Glendinning {
1341068bb1a7SSteve Glendinning 	u32 crc = bitrev16(crc16(0xFFFF, buffer, len));
1342068bb1a7SSteve Glendinning 	return crc << ((filter % 2) * 16);
1343bbd9f9eeSSteve Glendinning }
1344bbd9f9eeSSteve Glendinning 
1345e5e3af83SSteve Glendinning static int smsc95xx_enable_phy_wakeup_interrupts(struct usbnet *dev, u16 mask)
1346e5e3af83SSteve Glendinning {
1347e5e3af83SSteve Glendinning 	int ret;
1348e5e3af83SSteve Glendinning 
13491e1d7412SJoe Perches 	netdev_dbg(dev->net, "enabling PHY wakeup interrupts\n");
1350e5e3af83SSteve Glendinning 
1351e5e3af83SSteve Glendinning 	/* read to clear */
1352368be1caSAndre Edich 	ret = smsc95xx_mdio_read_nopm(dev, PHY_INT_SRC);
1353e360a8b4SSteve Glendinning 	if (ret < 0)
1354b052e073SSteve Glendinning 		return ret;
1355e5e3af83SSteve Glendinning 
1356e5e3af83SSteve Glendinning 	/* enable interrupt source */
1357368be1caSAndre Edich 	ret = smsc95xx_mdio_read_nopm(dev, PHY_INT_MASK);
1358e360a8b4SSteve Glendinning 	if (ret < 0)
1359b052e073SSteve Glendinning 		return ret;
1360e5e3af83SSteve Glendinning 
1361e5e3af83SSteve Glendinning 	ret |= mask;
1362e5e3af83SSteve Glendinning 
1363368be1caSAndre Edich 	smsc95xx_mdio_write_nopm(dev, PHY_INT_MASK, ret);
1364e5e3af83SSteve Glendinning 
1365e5e3af83SSteve Glendinning 	return 0;
1366e5e3af83SSteve Glendinning }
1367e5e3af83SSteve Glendinning 
1368e5e3af83SSteve Glendinning static int smsc95xx_link_ok_nopm(struct usbnet *dev)
1369e5e3af83SSteve Glendinning {
1370e5e3af83SSteve Glendinning 	int ret;
1371e5e3af83SSteve Glendinning 
1372e5e3af83SSteve Glendinning 	/* first, a dummy read, needed to latch some MII phys */
1373368be1caSAndre Edich 	ret = smsc95xx_mdio_read_nopm(dev, MII_BMSR);
1374e360a8b4SSteve Glendinning 	if (ret < 0)
1375b052e073SSteve Glendinning 		return ret;
1376e5e3af83SSteve Glendinning 
1377368be1caSAndre Edich 	ret = smsc95xx_mdio_read_nopm(dev, MII_BMSR);
1378e360a8b4SSteve Glendinning 	if (ret < 0)
1379b052e073SSteve Glendinning 		return ret;
1380e5e3af83SSteve Glendinning 
1381e5e3af83SSteve Glendinning 	return !!(ret & BMSR_LSTATUS);
1382e5e3af83SSteve Glendinning }
1383e5e3af83SSteve Glendinning 
1384319b95b5SSteve Glendinning static int smsc95xx_enter_suspend0(struct usbnet *dev)
1385319b95b5SSteve Glendinning {
1386*ad90a73fSAndre Edich 	struct smsc95xx_priv *pdata = dev->driver_priv;
1387319b95b5SSteve Glendinning 	u32 val;
1388319b95b5SSteve Glendinning 	int ret;
1389319b95b5SSteve Glendinning 
1390319b95b5SSteve Glendinning 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1391e360a8b4SSteve Glendinning 	if (ret < 0)
1392b052e073SSteve Glendinning 		return ret;
1393319b95b5SSteve Glendinning 
1394319b95b5SSteve Glendinning 	val &= (~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_));
1395319b95b5SSteve Glendinning 	val |= PM_CTL_SUS_MODE_0;
1396319b95b5SSteve Glendinning 
1397319b95b5SSteve Glendinning 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1398e360a8b4SSteve Glendinning 	if (ret < 0)
1399b052e073SSteve Glendinning 		return ret;
1400319b95b5SSteve Glendinning 
1401319b95b5SSteve Glendinning 	/* clear wol status */
1402319b95b5SSteve Glendinning 	val &= ~PM_CTL_WUPS_;
1403319b95b5SSteve Glendinning 	val |= PM_CTL_WUPS_WOL_;
1404319b95b5SSteve Glendinning 
1405319b95b5SSteve Glendinning 	/* enable energy detection */
1406319b95b5SSteve Glendinning 	if (pdata->wolopts & WAKE_PHY)
1407319b95b5SSteve Glendinning 		val |= PM_CTL_WUPS_ED_;
1408319b95b5SSteve Glendinning 
1409319b95b5SSteve Glendinning 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1410e360a8b4SSteve Glendinning 	if (ret < 0)
1411b052e073SSteve Glendinning 		return ret;
1412319b95b5SSteve Glendinning 
1413319b95b5SSteve Glendinning 	/* read back PM_CTRL */
1414319b95b5SSteve Glendinning 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
141576437214SMing Lei 	if (ret < 0)
141676437214SMing Lei 		return ret;
1417319b95b5SSteve Glendinning 
1418b2d4b150SSteve Glendinning 	pdata->suspend_flags |= SUSPEND_SUSPEND0;
1419b2d4b150SSteve Glendinning 
142076437214SMing Lei 	return 0;
1421319b95b5SSteve Glendinning }
1422319b95b5SSteve Glendinning 
1423319b95b5SSteve Glendinning static int smsc95xx_enter_suspend1(struct usbnet *dev)
1424319b95b5SSteve Glendinning {
1425*ad90a73fSAndre Edich 	struct smsc95xx_priv *pdata = dev->driver_priv;
1426319b95b5SSteve Glendinning 	u32 val;
1427319b95b5SSteve Glendinning 	int ret;
1428319b95b5SSteve Glendinning 
1429319b95b5SSteve Glendinning 	/* reconfigure link pulse detection timing for
1430319b95b5SSteve Glendinning 	 * compatibility with non-standard link partners
1431319b95b5SSteve Glendinning 	 */
1432319b95b5SSteve Glendinning 	if (pdata->features & FEATURE_PHY_NLP_CROSSOVER)
1433368be1caSAndre Edich 		smsc95xx_mdio_write_nopm(dev, PHY_EDPD_CONFIG,
1434319b95b5SSteve Glendinning 					 PHY_EDPD_CONFIG_DEFAULT);
1435319b95b5SSteve Glendinning 
1436319b95b5SSteve Glendinning 	/* enable energy detect power-down mode */
1437368be1caSAndre Edich 	ret = smsc95xx_mdio_read_nopm(dev, PHY_MODE_CTRL_STS);
1438e360a8b4SSteve Glendinning 	if (ret < 0)
1439b052e073SSteve Glendinning 		return ret;
1440319b95b5SSteve Glendinning 
1441319b95b5SSteve Glendinning 	ret |= MODE_CTRL_STS_EDPWRDOWN_;
1442319b95b5SSteve Glendinning 
1443368be1caSAndre Edich 	smsc95xx_mdio_write_nopm(dev, PHY_MODE_CTRL_STS, ret);
1444319b95b5SSteve Glendinning 
1445319b95b5SSteve Glendinning 	/* enter SUSPEND1 mode */
1446319b95b5SSteve Glendinning 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1447e360a8b4SSteve Glendinning 	if (ret < 0)
1448b052e073SSteve Glendinning 		return ret;
1449319b95b5SSteve Glendinning 
1450319b95b5SSteve Glendinning 	val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1451319b95b5SSteve Glendinning 	val |= PM_CTL_SUS_MODE_1;
1452319b95b5SSteve Glendinning 
1453319b95b5SSteve Glendinning 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1454e360a8b4SSteve Glendinning 	if (ret < 0)
1455b052e073SSteve Glendinning 		return ret;
1456319b95b5SSteve Glendinning 
1457319b95b5SSteve Glendinning 	/* clear wol status, enable energy detection */
1458319b95b5SSteve Glendinning 	val &= ~PM_CTL_WUPS_;
1459319b95b5SSteve Glendinning 	val |= (PM_CTL_WUPS_ED_ | PM_CTL_ED_EN_);
1460319b95b5SSteve Glendinning 
1461319b95b5SSteve Glendinning 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
146276437214SMing Lei 	if (ret < 0)
146376437214SMing Lei 		return ret;
1464319b95b5SSteve Glendinning 
1465b2d4b150SSteve Glendinning 	pdata->suspend_flags |= SUSPEND_SUSPEND1;
1466b2d4b150SSteve Glendinning 
146776437214SMing Lei 	return 0;
1468319b95b5SSteve Glendinning }
1469319b95b5SSteve Glendinning 
1470319b95b5SSteve Glendinning static int smsc95xx_enter_suspend2(struct usbnet *dev)
1471319b95b5SSteve Glendinning {
1472*ad90a73fSAndre Edich 	struct smsc95xx_priv *pdata = dev->driver_priv;
1473319b95b5SSteve Glendinning 	u32 val;
1474319b95b5SSteve Glendinning 	int ret;
1475319b95b5SSteve Glendinning 
1476319b95b5SSteve Glendinning 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1477e360a8b4SSteve Glendinning 	if (ret < 0)
1478b052e073SSteve Glendinning 		return ret;
1479319b95b5SSteve Glendinning 
1480319b95b5SSteve Glendinning 	val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1481319b95b5SSteve Glendinning 	val |= PM_CTL_SUS_MODE_2;
1482319b95b5SSteve Glendinning 
1483319b95b5SSteve Glendinning 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
148476437214SMing Lei 	if (ret < 0)
148576437214SMing Lei 		return ret;
1486319b95b5SSteve Glendinning 
1487b2d4b150SSteve Glendinning 	pdata->suspend_flags |= SUSPEND_SUSPEND2;
1488b2d4b150SSteve Glendinning 
148976437214SMing Lei 	return 0;
1490319b95b5SSteve Glendinning }
1491319b95b5SSteve Glendinning 
1492b2d4b150SSteve Glendinning static int smsc95xx_enter_suspend3(struct usbnet *dev)
1493b2d4b150SSteve Glendinning {
1494*ad90a73fSAndre Edich 	struct smsc95xx_priv *pdata = dev->driver_priv;
1495b2d4b150SSteve Glendinning 	u32 val;
1496b2d4b150SSteve Glendinning 	int ret;
1497b2d4b150SSteve Glendinning 
1498b2d4b150SSteve Glendinning 	ret = smsc95xx_read_reg_nopm(dev, RX_FIFO_INF, &val);
1499b2d4b150SSteve Glendinning 	if (ret < 0)
1500b2d4b150SSteve Glendinning 		return ret;
1501b2d4b150SSteve Glendinning 
150253a759c8SMartin Wetterwald 	if (val & RX_FIFO_INF_USED_) {
1503b2d4b150SSteve Glendinning 		netdev_info(dev->net, "rx fifo not empty in autosuspend\n");
1504b2d4b150SSteve Glendinning 		return -EBUSY;
1505b2d4b150SSteve Glendinning 	}
1506b2d4b150SSteve Glendinning 
1507b2d4b150SSteve Glendinning 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1508b2d4b150SSteve Glendinning 	if (ret < 0)
1509b2d4b150SSteve Glendinning 		return ret;
1510b2d4b150SSteve Glendinning 
1511b2d4b150SSteve Glendinning 	val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1512b2d4b150SSteve Glendinning 	val |= PM_CTL_SUS_MODE_3 | PM_CTL_RES_CLR_WKP_STS;
1513b2d4b150SSteve Glendinning 
1514b2d4b150SSteve Glendinning 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1515b2d4b150SSteve Glendinning 	if (ret < 0)
1516b2d4b150SSteve Glendinning 		return ret;
1517b2d4b150SSteve Glendinning 
1518b2d4b150SSteve Glendinning 	/* clear wol status */
1519b2d4b150SSteve Glendinning 	val &= ~PM_CTL_WUPS_;
1520b2d4b150SSteve Glendinning 	val |= PM_CTL_WUPS_WOL_;
1521b2d4b150SSteve Glendinning 
1522b2d4b150SSteve Glendinning 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1523b2d4b150SSteve Glendinning 	if (ret < 0)
1524b2d4b150SSteve Glendinning 		return ret;
1525b2d4b150SSteve Glendinning 
1526b2d4b150SSteve Glendinning 	pdata->suspend_flags |= SUSPEND_SUSPEND3;
1527b2d4b150SSteve Glendinning 
1528b2d4b150SSteve Glendinning 	return 0;
1529b2d4b150SSteve Glendinning }
1530b2d4b150SSteve Glendinning 
1531b2d4b150SSteve Glendinning static int smsc95xx_autosuspend(struct usbnet *dev, u32 link_up)
1532b2d4b150SSteve Glendinning {
1533*ad90a73fSAndre Edich 	struct smsc95xx_priv *pdata = dev->driver_priv;
1534b2d4b150SSteve Glendinning 	int ret;
1535b2d4b150SSteve Glendinning 
1536b2d4b150SSteve Glendinning 	if (!netif_running(dev->net)) {
1537b2d4b150SSteve Glendinning 		/* interface is ifconfig down so fully power down hw */
1538b2d4b150SSteve Glendinning 		netdev_dbg(dev->net, "autosuspend entering SUSPEND2\n");
1539b2d4b150SSteve Glendinning 		return smsc95xx_enter_suspend2(dev);
1540b2d4b150SSteve Glendinning 	}
1541b2d4b150SSteve Glendinning 
1542b2d4b150SSteve Glendinning 	if (!link_up) {
1543b2d4b150SSteve Glendinning 		/* link is down so enter EDPD mode, but only if device can
1544b2d4b150SSteve Glendinning 		 * reliably resume from it.  This check should be redundant
1545eb970ff0SMing Lei 		 * as current FEATURE_REMOTE_WAKEUP parts also support
1546b2d4b150SSteve Glendinning 		 * FEATURE_PHY_NLP_CROSSOVER but it's included for clarity */
1547b2d4b150SSteve Glendinning 		if (!(pdata->features & FEATURE_PHY_NLP_CROSSOVER)) {
1548b2d4b150SSteve Glendinning 			netdev_warn(dev->net, "EDPD not supported\n");
1549b2d4b150SSteve Glendinning 			return -EBUSY;
1550b2d4b150SSteve Glendinning 		}
1551b2d4b150SSteve Glendinning 
1552b2d4b150SSteve Glendinning 		netdev_dbg(dev->net, "autosuspend entering SUSPEND1\n");
1553b2d4b150SSteve Glendinning 
1554b2d4b150SSteve Glendinning 		/* enable PHY wakeup events for if cable is attached */
1555b2d4b150SSteve Glendinning 		ret = smsc95xx_enable_phy_wakeup_interrupts(dev,
1556b2d4b150SSteve Glendinning 			PHY_INT_MASK_ANEG_COMP_);
1557b2d4b150SSteve Glendinning 		if (ret < 0) {
1558b2d4b150SSteve Glendinning 			netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1559b2d4b150SSteve Glendinning 			return ret;
1560b2d4b150SSteve Glendinning 		}
1561b2d4b150SSteve Glendinning 
1562b2d4b150SSteve Glendinning 		netdev_info(dev->net, "entering SUSPEND1 mode\n");
1563b2d4b150SSteve Glendinning 		return smsc95xx_enter_suspend1(dev);
1564b2d4b150SSteve Glendinning 	}
1565b2d4b150SSteve Glendinning 
1566b2d4b150SSteve Glendinning 	/* enable PHY wakeup events so we remote wakeup if cable is pulled */
1567b2d4b150SSteve Glendinning 	ret = smsc95xx_enable_phy_wakeup_interrupts(dev,
1568b2d4b150SSteve Glendinning 		PHY_INT_MASK_LINK_DOWN_);
1569b2d4b150SSteve Glendinning 	if (ret < 0) {
1570b2d4b150SSteve Glendinning 		netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1571b2d4b150SSteve Glendinning 		return ret;
1572b2d4b150SSteve Glendinning 	}
1573b2d4b150SSteve Glendinning 
1574b2d4b150SSteve Glendinning 	netdev_dbg(dev->net, "autosuspend entering SUSPEND3\n");
1575b2d4b150SSteve Glendinning 	return smsc95xx_enter_suspend3(dev);
1576b2d4b150SSteve Glendinning }
1577b2d4b150SSteve Glendinning 
1578b5a04475SSteve Glendinning static int smsc95xx_suspend(struct usb_interface *intf, pm_message_t message)
1579b5a04475SSteve Glendinning {
1580b5a04475SSteve Glendinning 	struct usbnet *dev = usb_get_intfdata(intf);
1581*ad90a73fSAndre Edich 	struct smsc95xx_priv *pdata = dev->driver_priv;
1582e5e3af83SSteve Glendinning 	u32 val, link_up;
1583b5a04475SSteve Glendinning 	int ret;
1584b5a04475SSteve Glendinning 
1585b5a04475SSteve Glendinning 	ret = usbnet_suspend(intf, message);
1586b052e073SSteve Glendinning 	if (ret < 0) {
1587b052e073SSteve Glendinning 		netdev_warn(dev->net, "usbnet_suspend error\n");
1588b052e073SSteve Glendinning 		return ret;
1589b052e073SSteve Glendinning 	}
1590b5a04475SSteve Glendinning 
15917b900eadSFrieder Schrempf 	cancel_delayed_work_sync(&pdata->carrier_check);
15927b900eadSFrieder Schrempf 
1593b2d4b150SSteve Glendinning 	if (pdata->suspend_flags) {
1594b2d4b150SSteve Glendinning 		netdev_warn(dev->net, "error during last resume\n");
1595b2d4b150SSteve Glendinning 		pdata->suspend_flags = 0;
1596b2d4b150SSteve Glendinning 	}
1597b2d4b150SSteve Glendinning 
1598e5e3af83SSteve Glendinning 	/* determine if link is up using only _nopm functions */
1599e5e3af83SSteve Glendinning 	link_up = smsc95xx_link_ok_nopm(dev);
1600e5e3af83SSteve Glendinning 
160142e21c01SMing Lei 	if (message.event == PM_EVENT_AUTO_SUSPEND &&
1602eb970ff0SMing Lei 	    (pdata->features & FEATURE_REMOTE_WAKEUP)) {
1603b2d4b150SSteve Glendinning 		ret = smsc95xx_autosuspend(dev, link_up);
1604b2d4b150SSteve Glendinning 		goto done;
1605b2d4b150SSteve Glendinning 	}
1606b2d4b150SSteve Glendinning 
1607b2d4b150SSteve Glendinning 	/* if we get this far we're not autosuspending */
1608e5e3af83SSteve Glendinning 	/* if no wol options set, or if link is down and we're not waking on
1609e5e3af83SSteve Glendinning 	 * PHY activity, enter lowest power SUSPEND2 mode
1610e5e3af83SSteve Glendinning 	 */
1611e5e3af83SSteve Glendinning 	if (!(pdata->wolopts & SUPPORTED_WAKE) ||
1612e5e3af83SSteve Glendinning 		!(link_up || (pdata->wolopts & WAKE_PHY))) {
16131e1d7412SJoe Perches 		netdev_info(dev->net, "entering SUSPEND2 mode\n");
1614b5a04475SSteve Glendinning 
1615e0e474a8SSteve Glendinning 		/* disable energy detect (link up) & wake up events */
1616ec32115dSMing Lei 		ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1617e360a8b4SSteve Glendinning 		if (ret < 0)
1618b052e073SSteve Glendinning 			goto done;
1619e0e474a8SSteve Glendinning 
1620e0e474a8SSteve Glendinning 		val &= ~(WUCSR_MPEN_ | WUCSR_WAKE_EN_);
1621e0e474a8SSteve Glendinning 
1622ec32115dSMing Lei 		ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1623e360a8b4SSteve Glendinning 		if (ret < 0)
1624b052e073SSteve Glendinning 			goto done;
1625e0e474a8SSteve Glendinning 
1626ec32115dSMing Lei 		ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1627e360a8b4SSteve Glendinning 		if (ret < 0)
1628b052e073SSteve Glendinning 			goto done;
1629e0e474a8SSteve Glendinning 
1630e0e474a8SSteve Glendinning 		val &= ~(PM_CTL_ED_EN_ | PM_CTL_WOL_EN_);
1631e0e474a8SSteve Glendinning 
1632ec32115dSMing Lei 		ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1633e360a8b4SSteve Glendinning 		if (ret < 0)
1634b052e073SSteve Glendinning 			goto done;
1635e0e474a8SSteve Glendinning 
16363b9f7d8cSSteve Glendinning 		ret = smsc95xx_enter_suspend2(dev);
16373b9f7d8cSSteve Glendinning 		goto done;
1638b5a04475SSteve Glendinning 	}
1639b5a04475SSteve Glendinning 
1640e5e3af83SSteve Glendinning 	if (pdata->wolopts & WAKE_PHY) {
1641e5e3af83SSteve Glendinning 		ret = smsc95xx_enable_phy_wakeup_interrupts(dev,
1642e5e3af83SSteve Glendinning 			(PHY_INT_MASK_ANEG_COMP_ | PHY_INT_MASK_LINK_DOWN_));
1643b052e073SSteve Glendinning 		if (ret < 0) {
1644b052e073SSteve Glendinning 			netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1645b052e073SSteve Glendinning 			goto done;
1646b052e073SSteve Glendinning 		}
1647e5e3af83SSteve Glendinning 
1648e5e3af83SSteve Glendinning 		/* if link is down then configure EDPD and enter SUSPEND1,
1649e5e3af83SSteve Glendinning 		 * otherwise enter SUSPEND0 below
1650e5e3af83SSteve Glendinning 		 */
1651e5e3af83SSteve Glendinning 		if (!link_up) {
16521e1d7412SJoe Perches 			netdev_info(dev->net, "entering SUSPEND1 mode\n");
16533b9f7d8cSSteve Glendinning 			ret = smsc95xx_enter_suspend1(dev);
16543b9f7d8cSSteve Glendinning 			goto done;
1655e5e3af83SSteve Glendinning 		}
1656e5e3af83SSteve Glendinning 	}
1657e5e3af83SSteve Glendinning 
1658bbd9f9eeSSteve Glendinning 	if (pdata->wolopts & (WAKE_BCAST | WAKE_MCAST | WAKE_ARP | WAKE_UCAST)) {
16596396bb22SKees Cook 		u32 *filter_mask = kcalloc(32, sizeof(u32), GFP_KERNEL);
166006a221beSMing Lei 		u32 command[2];
166106a221beSMing Lei 		u32 offset[2];
166206a221beSMing Lei 		u32 crc[4];
16639ebca507SSteve Glendinning 		int wuff_filter_count =
16649ebca507SSteve Glendinning 			(pdata->features & FEATURE_8_WAKEUP_FILTERS) ?
16659ebca507SSteve Glendinning 			LAN9500A_WUFF_NUM : LAN9500_WUFF_NUM;
1666bbd9f9eeSSteve Glendinning 		int i, filter = 0;
1667bbd9f9eeSSteve Glendinning 
1668eed9a729SSteve Glendinning 		if (!filter_mask) {
1669eed9a729SSteve Glendinning 			netdev_warn(dev->net, "Unable to allocate filter_mask\n");
16703b9f7d8cSSteve Glendinning 			ret = -ENOMEM;
16713b9f7d8cSSteve Glendinning 			goto done;
1672eed9a729SSteve Glendinning 		}
1673eed9a729SSteve Glendinning 
167406a221beSMing Lei 		memset(command, 0, sizeof(command));
167506a221beSMing Lei 		memset(offset, 0, sizeof(offset));
167606a221beSMing Lei 		memset(crc, 0, sizeof(crc));
167706a221beSMing Lei 
1678bbd9f9eeSSteve Glendinning 		if (pdata->wolopts & WAKE_BCAST) {
1679bbd9f9eeSSteve Glendinning 			const u8 bcast[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
16801e1d7412SJoe Perches 			netdev_info(dev->net, "enabling broadcast detection\n");
1681bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4] = 0x003F;
1682bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 1] = 0x00;
1683bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 2] = 0x00;
1684bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 3] = 0x00;
1685bbd9f9eeSSteve Glendinning 			command[filter/4] |= 0x05UL << ((filter % 4) * 8);
1686bbd9f9eeSSteve Glendinning 			offset[filter/4] |= 0x00 << ((filter % 4) * 8);
1687bbd9f9eeSSteve Glendinning 			crc[filter/2] |= smsc_crc(bcast, 6, filter);
1688bbd9f9eeSSteve Glendinning 			filter++;
1689bbd9f9eeSSteve Glendinning 		}
1690bbd9f9eeSSteve Glendinning 
1691bbd9f9eeSSteve Glendinning 		if (pdata->wolopts & WAKE_MCAST) {
1692bbd9f9eeSSteve Glendinning 			const u8 mcast[] = {0x01, 0x00, 0x5E};
16931e1d7412SJoe Perches 			netdev_info(dev->net, "enabling multicast detection\n");
1694bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4] = 0x0007;
1695bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 1] = 0x00;
1696bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 2] = 0x00;
1697bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 3] = 0x00;
1698bbd9f9eeSSteve Glendinning 			command[filter/4] |= 0x09UL << ((filter % 4) * 8);
1699bbd9f9eeSSteve Glendinning 			offset[filter/4] |= 0x00  << ((filter % 4) * 8);
1700bbd9f9eeSSteve Glendinning 			crc[filter/2] |= smsc_crc(mcast, 3, filter);
1701bbd9f9eeSSteve Glendinning 			filter++;
1702bbd9f9eeSSteve Glendinning 		}
1703bbd9f9eeSSteve Glendinning 
1704bbd9f9eeSSteve Glendinning 		if (pdata->wolopts & WAKE_ARP) {
1705bbd9f9eeSSteve Glendinning 			const u8 arp[] = {0x08, 0x06};
17061e1d7412SJoe Perches 			netdev_info(dev->net, "enabling ARP detection\n");
1707bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4] = 0x0003;
1708bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 1] = 0x00;
1709bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 2] = 0x00;
1710bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 3] = 0x00;
1711bbd9f9eeSSteve Glendinning 			command[filter/4] |= 0x05UL << ((filter % 4) * 8);
1712bbd9f9eeSSteve Glendinning 			offset[filter/4] |= 0x0C << ((filter % 4) * 8);
1713bbd9f9eeSSteve Glendinning 			crc[filter/2] |= smsc_crc(arp, 2, filter);
1714bbd9f9eeSSteve Glendinning 			filter++;
1715bbd9f9eeSSteve Glendinning 		}
1716bbd9f9eeSSteve Glendinning 
1717bbd9f9eeSSteve Glendinning 		if (pdata->wolopts & WAKE_UCAST) {
17181e1d7412SJoe Perches 			netdev_info(dev->net, "enabling unicast detection\n");
1719bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4] = 0x003F;
1720bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 1] = 0x00;
1721bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 2] = 0x00;
1722bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 3] = 0x00;
1723bbd9f9eeSSteve Glendinning 			command[filter/4] |= 0x01UL << ((filter % 4) * 8);
1724bbd9f9eeSSteve Glendinning 			offset[filter/4] |= 0x00 << ((filter % 4) * 8);
1725bbd9f9eeSSteve Glendinning 			crc[filter/2] |= smsc_crc(dev->net->dev_addr, ETH_ALEN, filter);
1726bbd9f9eeSSteve Glendinning 			filter++;
1727bbd9f9eeSSteve Glendinning 		}
1728bbd9f9eeSSteve Glendinning 
17299ebca507SSteve Glendinning 		for (i = 0; i < (wuff_filter_count * 4); i++) {
1730ec32115dSMing Lei 			ret = smsc95xx_write_reg_nopm(dev, WUFF, filter_mask[i]);
1731b052e073SSteve Glendinning 			if (ret < 0) {
173206a221beSMing Lei 				kfree(filter_mask);
1733b052e073SSteve Glendinning 				goto done;
1734b052e073SSteve Glendinning 			}
1735bbd9f9eeSSteve Glendinning 		}
173606a221beSMing Lei 		kfree(filter_mask);
1737bbd9f9eeSSteve Glendinning 
17389ebca507SSteve Glendinning 		for (i = 0; i < (wuff_filter_count / 4); i++) {
1739ec32115dSMing Lei 			ret = smsc95xx_write_reg_nopm(dev, WUFF, command[i]);
1740e360a8b4SSteve Glendinning 			if (ret < 0)
1741b052e073SSteve Glendinning 				goto done;
1742b052e073SSteve Glendinning 		}
1743bbd9f9eeSSteve Glendinning 
17449ebca507SSteve Glendinning 		for (i = 0; i < (wuff_filter_count / 4); i++) {
1745ec32115dSMing Lei 			ret = smsc95xx_write_reg_nopm(dev, WUFF, offset[i]);
1746e360a8b4SSteve Glendinning 			if (ret < 0)
1747b052e073SSteve Glendinning 				goto done;
1748b052e073SSteve Glendinning 		}
1749bbd9f9eeSSteve Glendinning 
17509ebca507SSteve Glendinning 		for (i = 0; i < (wuff_filter_count / 2); i++) {
1751ec32115dSMing Lei 			ret = smsc95xx_write_reg_nopm(dev, WUFF, crc[i]);
1752e360a8b4SSteve Glendinning 			if (ret < 0)
1753b052e073SSteve Glendinning 				goto done;
1754b052e073SSteve Glendinning 		}
1755bbd9f9eeSSteve Glendinning 
1756bbd9f9eeSSteve Glendinning 		/* clear any pending pattern match packet status */
1757ec32115dSMing Lei 		ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1758e360a8b4SSteve Glendinning 		if (ret < 0)
1759b052e073SSteve Glendinning 			goto done;
1760bbd9f9eeSSteve Glendinning 
1761bbd9f9eeSSteve Glendinning 		val |= WUCSR_WUFR_;
1762bbd9f9eeSSteve Glendinning 
1763ec32115dSMing Lei 		ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1764e360a8b4SSteve Glendinning 		if (ret < 0)
1765b052e073SSteve Glendinning 			goto done;
1766b052e073SSteve Glendinning 	}
1767bbd9f9eeSSteve Glendinning 
1768e0e474a8SSteve Glendinning 	if (pdata->wolopts & WAKE_MAGIC) {
1769e0e474a8SSteve Glendinning 		/* clear any pending magic packet status */
1770ec32115dSMing Lei 		ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1771e360a8b4SSteve Glendinning 		if (ret < 0)
1772b052e073SSteve Glendinning 			goto done;
1773e0e474a8SSteve Glendinning 
1774e0e474a8SSteve Glendinning 		val |= WUCSR_MPR_;
1775e0e474a8SSteve Glendinning 
1776ec32115dSMing Lei 		ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1777e360a8b4SSteve Glendinning 		if (ret < 0)
1778b052e073SSteve Glendinning 			goto done;
1779b052e073SSteve Glendinning 	}
1780e0e474a8SSteve Glendinning 
1781bbd9f9eeSSteve Glendinning 	/* enable/disable wakeup sources */
1782ec32115dSMing Lei 	ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1783e360a8b4SSteve Glendinning 	if (ret < 0)
1784b052e073SSteve Glendinning 		goto done;
1785e0e474a8SSteve Glendinning 
1786bbd9f9eeSSteve Glendinning 	if (pdata->wolopts & (WAKE_BCAST | WAKE_MCAST | WAKE_ARP | WAKE_UCAST)) {
17871e1d7412SJoe Perches 		netdev_info(dev->net, "enabling pattern match wakeup\n");
1788bbd9f9eeSSteve Glendinning 		val |= WUCSR_WAKE_EN_;
1789bbd9f9eeSSteve Glendinning 	} else {
17901e1d7412SJoe Perches 		netdev_info(dev->net, "disabling pattern match wakeup\n");
1791bbd9f9eeSSteve Glendinning 		val &= ~WUCSR_WAKE_EN_;
1792bbd9f9eeSSteve Glendinning 	}
1793bbd9f9eeSSteve Glendinning 
1794e0e474a8SSteve Glendinning 	if (pdata->wolopts & WAKE_MAGIC) {
17951e1d7412SJoe Perches 		netdev_info(dev->net, "enabling magic packet wakeup\n");
1796e0e474a8SSteve Glendinning 		val |= WUCSR_MPEN_;
1797e0e474a8SSteve Glendinning 	} else {
17981e1d7412SJoe Perches 		netdev_info(dev->net, "disabling magic packet wakeup\n");
1799e0e474a8SSteve Glendinning 		val &= ~WUCSR_MPEN_;
1800e0e474a8SSteve Glendinning 	}
1801e0e474a8SSteve Glendinning 
1802ec32115dSMing Lei 	ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1803e360a8b4SSteve Glendinning 	if (ret < 0)
1804b052e073SSteve Glendinning 		goto done;
1805e0e474a8SSteve Glendinning 
1806e0e474a8SSteve Glendinning 	/* enable wol wakeup source */
1807ec32115dSMing Lei 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1808e360a8b4SSteve Glendinning 	if (ret < 0)
1809b052e073SSteve Glendinning 		goto done;
1810e0e474a8SSteve Glendinning 
1811e0e474a8SSteve Glendinning 	val |= PM_CTL_WOL_EN_;
1812e0e474a8SSteve Glendinning 
1813e5e3af83SSteve Glendinning 	/* phy energy detect wakeup source */
1814e5e3af83SSteve Glendinning 	if (pdata->wolopts & WAKE_PHY)
1815e5e3af83SSteve Glendinning 		val |= PM_CTL_ED_EN_;
1816e5e3af83SSteve Glendinning 
1817ec32115dSMing Lei 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1818e360a8b4SSteve Glendinning 	if (ret < 0)
1819b052e073SSteve Glendinning 		goto done;
1820e0e474a8SSteve Glendinning 
1821bbd9f9eeSSteve Glendinning 	/* enable receiver to enable frame reception */
1822ec32115dSMing Lei 	smsc95xx_start_rx_path(dev, 1);
1823e0e474a8SSteve Glendinning 
1824e0e474a8SSteve Glendinning 	/* some wol options are enabled, so enter SUSPEND0 */
18251e1d7412SJoe Perches 	netdev_info(dev->net, "entering SUSPEND0 mode\n");
18263b9f7d8cSSteve Glendinning 	ret = smsc95xx_enter_suspend0(dev);
18273b9f7d8cSSteve Glendinning 
18283b9f7d8cSSteve Glendinning done:
18290d41be53SMing Lei 	/*
18300d41be53SMing Lei 	 * TODO: resume() might need to handle the suspend failure
18310d41be53SMing Lei 	 * in system sleep
18320d41be53SMing Lei 	 */
18330d41be53SMing Lei 	if (ret && PMSG_IS_AUTO(message))
18343b9f7d8cSSteve Glendinning 		usbnet_resume(intf);
18357b900eadSFrieder Schrempf 
18367b900eadSFrieder Schrempf 	if (ret)
18377b900eadSFrieder Schrempf 		schedule_delayed_work(&pdata->carrier_check,
18387b900eadSFrieder Schrempf 				      CARRIER_CHECK_DELAY);
18397b900eadSFrieder Schrempf 
18403b9f7d8cSSteve Glendinning 	return ret;
1841e0e474a8SSteve Glendinning }
1842e0e474a8SSteve Glendinning 
1843e0e474a8SSteve Glendinning static int smsc95xx_resume(struct usb_interface *intf)
1844e0e474a8SSteve Glendinning {
1845e0e474a8SSteve Glendinning 	struct usbnet *dev = usb_get_intfdata(intf);
18468bca81d9SSudip Mukherjee 	struct smsc95xx_priv *pdata;
18478bca81d9SSudip Mukherjee 	u8 suspend_flags;
1848e0e474a8SSteve Glendinning 	int ret;
1849e0e474a8SSteve Glendinning 	u32 val;
1850e0e474a8SSteve Glendinning 
1851e0e474a8SSteve Glendinning 	BUG_ON(!dev);
1852*ad90a73fSAndre Edich 	pdata = dev->driver_priv;
18538bca81d9SSudip Mukherjee 	suspend_flags = pdata->suspend_flags;
1854e0e474a8SSteve Glendinning 
1855b2d4b150SSteve Glendinning 	netdev_dbg(dev->net, "resume suspend_flags=0x%02x\n", suspend_flags);
1856b2d4b150SSteve Glendinning 
1857b2d4b150SSteve Glendinning 	/* do this first to ensure it's cleared even in error case */
1858b2d4b150SSteve Glendinning 	pdata->suspend_flags = 0;
1859d69d1694SChristoph Fritz 	schedule_delayed_work(&pdata->carrier_check, CARRIER_CHECK_DELAY);
1860b2d4b150SSteve Glendinning 
1861b2d4b150SSteve Glendinning 	if (suspend_flags & SUSPEND_ALLMODES) {
1862bbd9f9eeSSteve Glendinning 		/* clear wake-up sources */
1863ec32115dSMing Lei 		ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1864e360a8b4SSteve Glendinning 		if (ret < 0)
1865b052e073SSteve Glendinning 			return ret;
1866e0e474a8SSteve Glendinning 
1867bbd9f9eeSSteve Glendinning 		val &= ~(WUCSR_WAKE_EN_ | WUCSR_MPEN_);
1868e0e474a8SSteve Glendinning 
1869ec32115dSMing Lei 		ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1870e360a8b4SSteve Glendinning 		if (ret < 0)
1871b052e073SSteve Glendinning 			return ret;
1872e0e474a8SSteve Glendinning 
1873e0e474a8SSteve Glendinning 		/* clear wake-up status */
1874ec32115dSMing Lei 		ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1875e360a8b4SSteve Glendinning 		if (ret < 0)
1876b052e073SSteve Glendinning 			return ret;
1877e0e474a8SSteve Glendinning 
1878e0e474a8SSteve Glendinning 		val &= ~PM_CTL_WOL_EN_;
1879e0e474a8SSteve Glendinning 		val |= PM_CTL_WUPS_;
1880e0e474a8SSteve Glendinning 
1881ec32115dSMing Lei 		ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1882e360a8b4SSteve Glendinning 		if (ret < 0)
1883b052e073SSteve Glendinning 			return ret;
1884b052e073SSteve Glendinning 	}
1885e0e474a8SSteve Glendinning 
1886af3d7c1eSSteve Glendinning 	ret = usbnet_resume(intf);
1887b052e073SSteve Glendinning 	if (ret < 0)
1888b052e073SSteve Glendinning 		netdev_warn(dev->net, "usbnet_resume error\n");
1889e0e474a8SSteve Glendinning 
1890b052e073SSteve Glendinning 	return ret;
1891e0e474a8SSteve Glendinning }
1892e0e474a8SSteve Glendinning 
1893b4df480fSJoonyoung Shim static int smsc95xx_reset_resume(struct usb_interface *intf)
1894b4df480fSJoonyoung Shim {
1895b4df480fSJoonyoung Shim 	struct usbnet *dev = usb_get_intfdata(intf);
1896b4df480fSJoonyoung Shim 	int ret;
1897b4df480fSJoonyoung Shim 
1898b4df480fSJoonyoung Shim 	ret = smsc95xx_reset(dev);
1899b4df480fSJoonyoung Shim 	if (ret < 0)
1900b4df480fSJoonyoung Shim 		return ret;
1901b4df480fSJoonyoung Shim 
1902b4df480fSJoonyoung Shim 	return smsc95xx_resume(intf);
1903b4df480fSJoonyoung Shim }
1904b4df480fSJoonyoung Shim 
19052f7ca802SSteve Glendinning static void smsc95xx_rx_csum_offload(struct sk_buff *skb)
19062f7ca802SSteve Glendinning {
19072f7ca802SSteve Glendinning 	skb->csum = *(u16 *)(skb_tail_pointer(skb) - 2);
19082f7ca802SSteve Glendinning 	skb->ip_summed = CHECKSUM_COMPLETE;
19092f7ca802SSteve Glendinning 	skb_trim(skb, skb->len - 2);
19102f7ca802SSteve Glendinning }
19112f7ca802SSteve Glendinning 
19122f7ca802SSteve Glendinning static int smsc95xx_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
19132f7ca802SSteve Glendinning {
1914eb85569fSEmil Goode 	/* This check is no longer done by usbnet */
1915eb85569fSEmil Goode 	if (skb->len < dev->net->hard_header_len)
1916eb85569fSEmil Goode 		return 0;
1917eb85569fSEmil Goode 
19182f7ca802SSteve Glendinning 	while (skb->len > 0) {
19192f7ca802SSteve Glendinning 		u32 header, align_count;
19202f7ca802SSteve Glendinning 		struct sk_buff *ax_skb;
19212f7ca802SSteve Glendinning 		unsigned char *packet;
19222f7ca802SSteve Glendinning 		u16 size;
19232f7ca802SSteve Glendinning 
19246809d216SBen Dooks 		header = get_unaligned_le32(skb->data);
19252f7ca802SSteve Glendinning 		skb_pull(skb, 4 + NET_IP_ALIGN);
19262f7ca802SSteve Glendinning 		packet = skb->data;
19272f7ca802SSteve Glendinning 
19282f7ca802SSteve Glendinning 		/* get the packet length */
19292f7ca802SSteve Glendinning 		size = (u16)((header & RX_STS_FL_) >> 16);
19302f7ca802SSteve Glendinning 		align_count = (4 - ((size + NET_IP_ALIGN) % 4)) % 4;
19312f7ca802SSteve Glendinning 
19322f7ca802SSteve Glendinning 		if (unlikely(header & RX_STS_ES_)) {
1933a475f603SJoe Perches 			netif_dbg(dev, rx_err, dev->net,
1934a475f603SJoe Perches 				  "Error header=0x%08x\n", header);
193580667ac1SHerbert Xu 			dev->net->stats.rx_errors++;
193680667ac1SHerbert Xu 			dev->net->stats.rx_dropped++;
19372f7ca802SSteve Glendinning 
19382f7ca802SSteve Glendinning 			if (header & RX_STS_CRC_) {
193980667ac1SHerbert Xu 				dev->net->stats.rx_crc_errors++;
19402f7ca802SSteve Glendinning 			} else {
19412f7ca802SSteve Glendinning 				if (header & (RX_STS_TL_ | RX_STS_RF_))
194280667ac1SHerbert Xu 					dev->net->stats.rx_frame_errors++;
19432f7ca802SSteve Glendinning 
19442f7ca802SSteve Glendinning 				if ((header & RX_STS_LE_) &&
19452f7ca802SSteve Glendinning 					(!(header & RX_STS_FT_)))
194680667ac1SHerbert Xu 					dev->net->stats.rx_length_errors++;
19472f7ca802SSteve Glendinning 			}
19482f7ca802SSteve Glendinning 		} else {
19492f7ca802SSteve Glendinning 			/* ETH_FRAME_LEN + 4(CRC) + 2(COE) + 4(Vlan) */
19502f7ca802SSteve Glendinning 			if (unlikely(size > (ETH_FRAME_LEN + 12))) {
1951a475f603SJoe Perches 				netif_dbg(dev, rx_err, dev->net,
1952a475f603SJoe Perches 					  "size err header=0x%08x\n", header);
19532f7ca802SSteve Glendinning 				return 0;
19542f7ca802SSteve Glendinning 			}
19552f7ca802SSteve Glendinning 
19562f7ca802SSteve Glendinning 			/* last frame in this batch */
19572f7ca802SSteve Glendinning 			if (skb->len == size) {
195878e47fe4SMichał Mirosław 				if (dev->net->features & NETIF_F_RXCSUM)
19592f7ca802SSteve Glendinning 					smsc95xx_rx_csum_offload(skb);
1960df18accaSPeter Korsgaard 				skb_trim(skb, skb->len - 4); /* remove fcs */
19612f7ca802SSteve Glendinning 				skb->truesize = size + sizeof(struct sk_buff);
19622f7ca802SSteve Glendinning 
19632f7ca802SSteve Glendinning 				return 1;
19642f7ca802SSteve Glendinning 			}
19652f7ca802SSteve Glendinning 
19662f7ca802SSteve Glendinning 			ax_skb = skb_clone(skb, GFP_ATOMIC);
19672f7ca802SSteve Glendinning 			if (unlikely(!ax_skb)) {
196860b86755SJoe Perches 				netdev_warn(dev->net, "Error allocating skb\n");
19692f7ca802SSteve Glendinning 				return 0;
19702f7ca802SSteve Glendinning 			}
19712f7ca802SSteve Glendinning 
19722f7ca802SSteve Glendinning 			ax_skb->len = size;
19732f7ca802SSteve Glendinning 			ax_skb->data = packet;
19742f7ca802SSteve Glendinning 			skb_set_tail_pointer(ax_skb, size);
19752f7ca802SSteve Glendinning 
197678e47fe4SMichał Mirosław 			if (dev->net->features & NETIF_F_RXCSUM)
19772f7ca802SSteve Glendinning 				smsc95xx_rx_csum_offload(ax_skb);
1978df18accaSPeter Korsgaard 			skb_trim(ax_skb, ax_skb->len - 4); /* remove fcs */
19792f7ca802SSteve Glendinning 			ax_skb->truesize = size + sizeof(struct sk_buff);
19802f7ca802SSteve Glendinning 
19812f7ca802SSteve Glendinning 			usbnet_skb_return(dev, ax_skb);
19822f7ca802SSteve Glendinning 		}
19832f7ca802SSteve Glendinning 
19842f7ca802SSteve Glendinning 		skb_pull(skb, size);
19852f7ca802SSteve Glendinning 
19862f7ca802SSteve Glendinning 		/* padding bytes before the next frame starts */
19872f7ca802SSteve Glendinning 		if (skb->len)
19882f7ca802SSteve Glendinning 			skb_pull(skb, align_count);
19892f7ca802SSteve Glendinning 	}
19902f7ca802SSteve Glendinning 
19912f7ca802SSteve Glendinning 	return 1;
19922f7ca802SSteve Glendinning }
19932f7ca802SSteve Glendinning 
1994f7b29271SSteve Glendinning static u32 smsc95xx_calc_csum_preamble(struct sk_buff *skb)
1995f7b29271SSteve Glendinning {
199655508d60SMichał Mirosław 	u16 low_16 = (u16)skb_checksum_start_offset(skb);
199755508d60SMichał Mirosław 	u16 high_16 = low_16 + skb->csum_offset;
1998f7b29271SSteve Glendinning 	return (high_16 << 16) | low_16;
1999f7b29271SSteve Glendinning }
2000f7b29271SSteve Glendinning 
200175938f77SBen Dooks /* The TX CSUM won't work if the checksum lies in the last 4 bytes of the
200275938f77SBen Dooks  * transmission. This is fairly unlikely, only seems to trigger with some
200375938f77SBen Dooks  * short TCP ACK packets sent.
200475938f77SBen Dooks  *
200575938f77SBen Dooks  * Note, this calculation should probably check for the alignment of the
200675938f77SBen Dooks  * data as well, but a straight check for csum being in the last four bytes
200775938f77SBen Dooks  * of the packet should be ok for now.
200875938f77SBen Dooks  */
200975938f77SBen Dooks static bool smsc95xx_can_tx_checksum(struct sk_buff *skb)
201075938f77SBen Dooks {
201175938f77SBen Dooks        unsigned int len = skb->len - skb_checksum_start_offset(skb);
201275938f77SBen Dooks 
201375938f77SBen Dooks        if (skb->len <= 45)
201475938f77SBen Dooks 	       return false;
201575938f77SBen Dooks        return skb->csum_offset < (len - (4 + 1));
201675938f77SBen Dooks }
201775938f77SBen Dooks 
20182f7ca802SSteve Glendinning static struct sk_buff *smsc95xx_tx_fixup(struct usbnet *dev,
20192f7ca802SSteve Glendinning 					 struct sk_buff *skb, gfp_t flags)
20202f7ca802SSteve Glendinning {
202178e47fe4SMichał Mirosław 	bool csum = skb->ip_summed == CHECKSUM_PARTIAL;
2022f7b29271SSteve Glendinning 	int overhead = csum ? SMSC95XX_TX_OVERHEAD_CSUM : SMSC95XX_TX_OVERHEAD;
20232f7ca802SSteve Glendinning 	u32 tx_cmd_a, tx_cmd_b;
20240c8b2655SBen Dooks 	void *ptr;
20252f7ca802SSteve Glendinning 
2026f7b29271SSteve Glendinning 	/* We do not advertise SG, so skbs should be already linearized */
2027f7b29271SSteve Glendinning 	BUG_ON(skb_shinfo(skb)->nr_frags);
2028f7b29271SSteve Glendinning 
2029e9156cd2SJames Hughes 	/* Make writable and expand header space by overhead if required */
2030e9156cd2SJames Hughes 	if (skb_cow_head(skb, overhead)) {
2031e9156cd2SJames Hughes 		/* Must deallocate here as returning NULL to indicate error
2032e9156cd2SJames Hughes 		 * means the skb won't be deallocated in the caller.
2033e9156cd2SJames Hughes 		 */
20342f7ca802SSteve Glendinning 		dev_kfree_skb_any(skb);
20352f7ca802SSteve Glendinning 		return NULL;
20362f7ca802SSteve Glendinning 	}
20372f7ca802SSteve Glendinning 
20380c8b2655SBen Dooks 	tx_cmd_b = (u32)skb->len;
20390c8b2655SBen Dooks 	tx_cmd_a = tx_cmd_b | TX_CMD_A_FIRST_SEG_ | TX_CMD_A_LAST_SEG_;
20400c8b2655SBen Dooks 
2041f7b29271SSteve Glendinning 	if (csum) {
204275938f77SBen Dooks 		if (!smsc95xx_can_tx_checksum(skb)) {
204311bc3088SSteve Glendinning 			/* workaround - hardware tx checksum does not work
204411bc3088SSteve Glendinning 			 * properly with extremely small packets */
204555508d60SMichał Mirosław 			long csstart = skb_checksum_start_offset(skb);
204611bc3088SSteve Glendinning 			__wsum calc = csum_partial(skb->data + csstart,
204711bc3088SSteve Glendinning 				skb->len - csstart, 0);
204811bc3088SSteve Glendinning 			*((__sum16 *)(skb->data + csstart
204911bc3088SSteve Glendinning 				+ skb->csum_offset)) = csum_fold(calc);
205011bc3088SSteve Glendinning 
205111bc3088SSteve Glendinning 			csum = false;
205211bc3088SSteve Glendinning 		} else {
2053f7b29271SSteve Glendinning 			u32 csum_preamble = smsc95xx_calc_csum_preamble(skb);
20540c8b2655SBen Dooks 			ptr = skb_push(skb, 4);
20550c8b2655SBen Dooks 			put_unaligned_le32(csum_preamble, ptr);
2056f7b29271SSteve Glendinning 
20570c8b2655SBen Dooks 			tx_cmd_a += 4;
20580c8b2655SBen Dooks 			tx_cmd_b += 4;
2059f7b29271SSteve Glendinning 			tx_cmd_b |= TX_CMD_B_CSUM_ENABLE;
20600c8b2655SBen Dooks 		}
20610c8b2655SBen Dooks 	}
20622f7ca802SSteve Glendinning 
20630c8b2655SBen Dooks 	ptr = skb_push(skb, 8);
20640c8b2655SBen Dooks 	put_unaligned_le32(tx_cmd_a, ptr);
20650c8b2655SBen Dooks 	put_unaligned_le32(tx_cmd_b, ptr+4);
20662f7ca802SSteve Glendinning 
20672f7ca802SSteve Glendinning 	return skb;
20682f7ca802SSteve Glendinning }
20692f7ca802SSteve Glendinning 
2070b2d4b150SSteve Glendinning static int smsc95xx_manage_power(struct usbnet *dev, int on)
2071b2d4b150SSteve Glendinning {
2072*ad90a73fSAndre Edich 	struct smsc95xx_priv *pdata = dev->driver_priv;
2073b2d4b150SSteve Glendinning 
2074b2d4b150SSteve Glendinning 	dev->intf->needs_remote_wakeup = on;
2075b2d4b150SSteve Glendinning 
2076eb970ff0SMing Lei 	if (pdata->features & FEATURE_REMOTE_WAKEUP)
2077b2d4b150SSteve Glendinning 		return 0;
2078b2d4b150SSteve Glendinning 
2079eb970ff0SMing Lei 	/* this chip revision isn't capable of remote wakeup */
2080eb970ff0SMing Lei 	netdev_info(dev->net, "hardware isn't capable of remote wakeup\n");
2081b2d4b150SSteve Glendinning 
2082b2d4b150SSteve Glendinning 	if (on)
2083b2d4b150SSteve Glendinning 		usb_autopm_get_interface_no_resume(dev->intf);
2084b2d4b150SSteve Glendinning 	else
2085b2d4b150SSteve Glendinning 		usb_autopm_put_interface(dev->intf);
2086b2d4b150SSteve Glendinning 
2087b2d4b150SSteve Glendinning 	return 0;
2088b2d4b150SSteve Glendinning }
2089b2d4b150SSteve Glendinning 
20902f7ca802SSteve Glendinning static const struct driver_info smsc95xx_info = {
20912f7ca802SSteve Glendinning 	.description	= "smsc95xx USB 2.0 Ethernet",
20922f7ca802SSteve Glendinning 	.bind		= smsc95xx_bind,
20932f7ca802SSteve Glendinning 	.unbind		= smsc95xx_unbind,
20942f7ca802SSteve Glendinning 	.link_reset	= smsc95xx_link_reset,
20952f7ca802SSteve Glendinning 	.reset		= smsc95xx_reset,
20962f7ca802SSteve Glendinning 	.rx_fixup	= smsc95xx_rx_fixup,
20972f7ca802SSteve Glendinning 	.tx_fixup	= smsc95xx_tx_fixup,
20982f7ca802SSteve Glendinning 	.status		= smsc95xx_status,
2099b2d4b150SSteve Glendinning 	.manage_power	= smsc95xx_manage_power,
210007d69d42SPaolo Pisati 	.flags		= FLAG_ETHER | FLAG_SEND_ZLP | FLAG_LINK_INTR,
21012f7ca802SSteve Glendinning };
21022f7ca802SSteve Glendinning 
21032f7ca802SSteve Glendinning static const struct usb_device_id products[] = {
21042f7ca802SSteve Glendinning 	{
21052f7ca802SSteve Glendinning 		/* SMSC9500 USB Ethernet Device */
21062f7ca802SSteve Glendinning 		USB_DEVICE(0x0424, 0x9500),
21072f7ca802SSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21082f7ca802SSteve Glendinning 	},
2109726474b8SSteve Glendinning 	{
21106f41d12bSSteve Glendinning 		/* SMSC9505 USB Ethernet Device */
21116f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9505),
21126f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21136f41d12bSSteve Glendinning 	},
21146f41d12bSSteve Glendinning 	{
21156f41d12bSSteve Glendinning 		/* SMSC9500A USB Ethernet Device */
21166f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9E00),
21176f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21186f41d12bSSteve Glendinning 	},
21196f41d12bSSteve Glendinning 	{
21206f41d12bSSteve Glendinning 		/* SMSC9505A USB Ethernet Device */
21216f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9E01),
21226f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21236f41d12bSSteve Glendinning 	},
21246f41d12bSSteve Glendinning 	{
2125726474b8SSteve Glendinning 		/* SMSC9512/9514 USB Hub & Ethernet Device */
2126726474b8SSteve Glendinning 		USB_DEVICE(0x0424, 0xec00),
2127726474b8SSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
2128726474b8SSteve Glendinning 	},
21296f41d12bSSteve Glendinning 	{
21306f41d12bSSteve Glendinning 		/* SMSC9500 USB Ethernet Device (SAL10) */
21316f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9900),
21326f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21336f41d12bSSteve Glendinning 	},
21346f41d12bSSteve Glendinning 	{
21356f41d12bSSteve Glendinning 		/* SMSC9505 USB Ethernet Device (SAL10) */
21366f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9901),
21376f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21386f41d12bSSteve Glendinning 	},
21396f41d12bSSteve Glendinning 	{
21406f41d12bSSteve Glendinning 		/* SMSC9500A USB Ethernet Device (SAL10) */
21416f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9902),
21426f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21436f41d12bSSteve Glendinning 	},
21446f41d12bSSteve Glendinning 	{
21456f41d12bSSteve Glendinning 		/* SMSC9505A USB Ethernet Device (SAL10) */
21466f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9903),
21476f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21486f41d12bSSteve Glendinning 	},
21496f41d12bSSteve Glendinning 	{
21506f41d12bSSteve Glendinning 		/* SMSC9512/9514 USB Hub & Ethernet Device (SAL10) */
21516f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9904),
21526f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21536f41d12bSSteve Glendinning 	},
21546f41d12bSSteve Glendinning 	{
21556f41d12bSSteve Glendinning 		/* SMSC9500A USB Ethernet Device (HAL) */
21566f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9905),
21576f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21586f41d12bSSteve Glendinning 	},
21596f41d12bSSteve Glendinning 	{
21606f41d12bSSteve Glendinning 		/* SMSC9505A USB Ethernet Device (HAL) */
21616f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9906),
21626f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21636f41d12bSSteve Glendinning 	},
21646f41d12bSSteve Glendinning 	{
21656f41d12bSSteve Glendinning 		/* SMSC9500 USB Ethernet Device (Alternate ID) */
21666f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9907),
21676f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21686f41d12bSSteve Glendinning 	},
21696f41d12bSSteve Glendinning 	{
21706f41d12bSSteve Glendinning 		/* SMSC9500A USB Ethernet Device (Alternate ID) */
21716f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9908),
21726f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21736f41d12bSSteve Glendinning 	},
21746f41d12bSSteve Glendinning 	{
21756f41d12bSSteve Glendinning 		/* SMSC9512/9514 USB Hub & Ethernet Device (Alternate ID) */
21766f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9909),
21776f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21786f41d12bSSteve Glendinning 	},
217988edaa41SSteve Glendinning 	{
218088edaa41SSteve Glendinning 		/* SMSC LAN9530 USB Ethernet Device */
218188edaa41SSteve Glendinning 		USB_DEVICE(0x0424, 0x9530),
218288edaa41SSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
218388edaa41SSteve Glendinning 	},
218488edaa41SSteve Glendinning 	{
218588edaa41SSteve Glendinning 		/* SMSC LAN9730 USB Ethernet Device */
218688edaa41SSteve Glendinning 		USB_DEVICE(0x0424, 0x9730),
218788edaa41SSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
218888edaa41SSteve Glendinning 	},
218988edaa41SSteve Glendinning 	{
219088edaa41SSteve Glendinning 		/* SMSC LAN89530 USB Ethernet Device */
219188edaa41SSteve Glendinning 		USB_DEVICE(0x0424, 0x9E08),
219288edaa41SSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
219388edaa41SSteve Glendinning 	},
21942f7ca802SSteve Glendinning 	{ },		/* END */
21952f7ca802SSteve Glendinning };
21962f7ca802SSteve Glendinning MODULE_DEVICE_TABLE(usb, products);
21972f7ca802SSteve Glendinning 
21982f7ca802SSteve Glendinning static struct usb_driver smsc95xx_driver = {
21992f7ca802SSteve Glendinning 	.name		= "smsc95xx",
22002f7ca802SSteve Glendinning 	.id_table	= products,
22012f7ca802SSteve Glendinning 	.probe		= usbnet_probe,
2202b5a04475SSteve Glendinning 	.suspend	= smsc95xx_suspend,
2203e0e474a8SSteve Glendinning 	.resume		= smsc95xx_resume,
2204b4df480fSJoonyoung Shim 	.reset_resume	= smsc95xx_reset_resume,
22052f7ca802SSteve Glendinning 	.disconnect	= usbnet_disconnect,
2206e1f12eb6SSarah Sharp 	.disable_hub_initiated_lpm = 1,
2207b2d4b150SSteve Glendinning 	.supports_autosuspend = 1,
22082f7ca802SSteve Glendinning };
22092f7ca802SSteve Glendinning 
2210d632eb1bSGreg Kroah-Hartman module_usb_driver(smsc95xx_driver);
22112f7ca802SSteve Glendinning 
22122f7ca802SSteve Glendinning MODULE_AUTHOR("Nancy Lin");
221390b24cfbSSteve Glendinning MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>");
22142f7ca802SSteve Glendinning MODULE_DESCRIPTION("SMSC95XX USB 2.0 Ethernet Devices");
22152f7ca802SSteve Glendinning MODULE_LICENSE("GPL");
2216