xref: /linux/drivers/net/usb/smsc95xx.c (revision 6809d2167c404c2c8cbbbe1667c70ea88576ccac)
12f7ca802SSteve Glendinning  /***************************************************************************
22f7ca802SSteve Glendinning  *
32f7ca802SSteve Glendinning  * Copyright (C) 2007-2008 SMSC
42f7ca802SSteve Glendinning  *
52f7ca802SSteve Glendinning  * This program is free software; you can redistribute it and/or
62f7ca802SSteve Glendinning  * modify it under the terms of the GNU General Public License
72f7ca802SSteve Glendinning  * as published by the Free Software Foundation; either version 2
82f7ca802SSteve Glendinning  * of the License, or (at your option) any later version.
92f7ca802SSteve Glendinning  *
102f7ca802SSteve Glendinning  * This program is distributed in the hope that it will be useful,
112f7ca802SSteve Glendinning  * but WITHOUT ANY WARRANTY; without even the implied warranty of
122f7ca802SSteve Glendinning  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
132f7ca802SSteve Glendinning  * GNU General Public License for more details.
142f7ca802SSteve Glendinning  *
152f7ca802SSteve Glendinning  * You should have received a copy of the GNU General Public License
169cb00073SJeff Kirsher  * along with this program; if not, see <http://www.gnu.org/licenses/>.
172f7ca802SSteve Glendinning  *
182f7ca802SSteve Glendinning  *****************************************************************************/
192f7ca802SSteve Glendinning 
202f7ca802SSteve Glendinning #include <linux/module.h>
212f7ca802SSteve Glendinning #include <linux/kmod.h>
222f7ca802SSteve Glendinning #include <linux/netdevice.h>
232f7ca802SSteve Glendinning #include <linux/etherdevice.h>
242f7ca802SSteve Glendinning #include <linux/ethtool.h>
252f7ca802SSteve Glendinning #include <linux/mii.h>
262f7ca802SSteve Glendinning #include <linux/usb.h>
27bbd9f9eeSSteve Glendinning #include <linux/bitrev.h>
28bbd9f9eeSSteve Glendinning #include <linux/crc16.h>
292f7ca802SSteve Glendinning #include <linux/crc32.h>
302f7ca802SSteve Glendinning #include <linux/usb/usbnet.h>
315a0e3ad6STejun Heo #include <linux/slab.h>
32c489565bSArnd Bergmann #include <linux/of_net.h>
332f7ca802SSteve Glendinning #include "smsc95xx.h"
342f7ca802SSteve Glendinning 
352f7ca802SSteve Glendinning #define SMSC_CHIPNAME			"smsc95xx"
3653a759c8SMartin Wetterwald #define SMSC_DRIVER_VERSION		"1.0.6"
372f7ca802SSteve Glendinning #define HS_USB_PKT_SIZE			(512)
382f7ca802SSteve Glendinning #define FS_USB_PKT_SIZE			(64)
392f7ca802SSteve Glendinning #define DEFAULT_HS_BURST_CAP_SIZE	(16 * 1024 + 5 * HS_USB_PKT_SIZE)
402f7ca802SSteve Glendinning #define DEFAULT_FS_BURST_CAP_SIZE	(6 * 1024 + 33 * FS_USB_PKT_SIZE)
412f7ca802SSteve Glendinning #define DEFAULT_BULK_IN_DELAY		(0x00002000)
422f7ca802SSteve Glendinning #define MAX_SINGLE_PACKET_SIZE		(2048)
432f7ca802SSteve Glendinning #define LAN95XX_EEPROM_MAGIC		(0x9500)
442f7ca802SSteve Glendinning #define EEPROM_MAC_OFFSET		(0x01)
45f7b29271SSteve Glendinning #define DEFAULT_TX_CSUM_ENABLE		(true)
462f7ca802SSteve Glendinning #define DEFAULT_RX_CSUM_ENABLE		(true)
472f7ca802SSteve Glendinning #define SMSC95XX_INTERNAL_PHY_ID	(1)
482f7ca802SSteve Glendinning #define SMSC95XX_TX_OVERHEAD		(8)
49f7b29271SSteve Glendinning #define SMSC95XX_TX_OVERHEAD_CSUM	(12)
50e5e3af83SSteve Glendinning #define SUPPORTED_WAKE			(WAKE_PHY | WAKE_UCAST | WAKE_BCAST | \
51bbd9f9eeSSteve Glendinning 					 WAKE_MCAST | WAKE_ARP | WAKE_MAGIC)
522f7ca802SSteve Glendinning 
539ebca507SSteve Glendinning #define FEATURE_8_WAKEUP_FILTERS	(0x01)
549ebca507SSteve Glendinning #define FEATURE_PHY_NLP_CROSSOVER	(0x02)
55eb970ff0SMing Lei #define FEATURE_REMOTE_WAKEUP		(0x04)
569ebca507SSteve Glendinning 
57b2d4b150SSteve Glendinning #define SUSPEND_SUSPEND0		(0x01)
58b2d4b150SSteve Glendinning #define SUSPEND_SUSPEND1		(0x02)
59b2d4b150SSteve Glendinning #define SUSPEND_SUSPEND2		(0x04)
60b2d4b150SSteve Glendinning #define SUSPEND_SUSPEND3		(0x08)
61b2d4b150SSteve Glendinning #define SUSPEND_ALLMODES		(SUSPEND_SUSPEND0 | SUSPEND_SUSPEND1 | \
62b2d4b150SSteve Glendinning 					 SUSPEND_SUSPEND2 | SUSPEND_SUSPEND3)
63b2d4b150SSteve Glendinning 
64d69d1694SChristoph Fritz #define CARRIER_CHECK_DELAY (2 * HZ)
65d69d1694SChristoph Fritz 
662f7ca802SSteve Glendinning struct smsc95xx_priv {
6713722bbeSWoojung Huh 	u32 chip_id;
682f7ca802SSteve Glendinning 	u32 mac_cr;
693c0f3c60SMarc Zyngier 	u32 hash_hi;
703c0f3c60SMarc Zyngier 	u32 hash_lo;
71e0e474a8SSteve Glendinning 	u32 wolopts;
722f7ca802SSteve Glendinning 	spinlock_t mac_cr_lock;
739ebca507SSteve Glendinning 	u8 features;
74b2d4b150SSteve Glendinning 	u8 suspend_flags;
7513722bbeSWoojung Huh 	u8 mdix_ctrl;
76d69d1694SChristoph Fritz 	bool link_ok;
77d69d1694SChristoph Fritz 	struct delayed_work carrier_check;
78d69d1694SChristoph Fritz 	struct usbnet *dev;
792f7ca802SSteve Glendinning };
802f7ca802SSteve Glendinning 
81eb939922SRusty Russell static bool turbo_mode = true;
822f7ca802SSteve Glendinning module_param(turbo_mode, bool, 0644);
832f7ca802SSteve Glendinning MODULE_PARM_DESC(turbo_mode, "Enable multiple frames per Rx transaction");
842f7ca802SSteve Glendinning 
85ec32115dSMing Lei static int __must_check __smsc95xx_read_reg(struct usbnet *dev, u32 index,
86ec32115dSMing Lei 					    u32 *data, int in_pm)
872f7ca802SSteve Glendinning {
8872108fd2SMing Lei 	u32 buf;
892f7ca802SSteve Glendinning 	int ret;
90ec32115dSMing Lei 	int (*fn)(struct usbnet *, u8, u8, u16, u16, void *, u16);
912f7ca802SSteve Glendinning 
922f7ca802SSteve Glendinning 	BUG_ON(!dev);
932f7ca802SSteve Glendinning 
94ec32115dSMing Lei 	if (!in_pm)
95ec32115dSMing Lei 		fn = usbnet_read_cmd;
96ec32115dSMing Lei 	else
97ec32115dSMing Lei 		fn = usbnet_read_cmd_nopm;
98ec32115dSMing Lei 
99ec32115dSMing Lei 	ret = fn(dev, USB_VENDOR_REQUEST_READ_REGISTER, USB_DIR_IN
100ec32115dSMing Lei 		 | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
10172108fd2SMing Lei 		 0, index, &buf, 4);
1025a36b68bSDan Carpenter 	if (unlikely(ret < 0)) {
1031e1d7412SJoe Perches 		netdev_warn(dev->net, "Failed to read reg index 0x%08x: %d\n",
1041e1d7412SJoe Perches 			    index, ret);
1055a36b68bSDan Carpenter 		return ret;
1065a36b68bSDan Carpenter 	}
1072f7ca802SSteve Glendinning 
10872108fd2SMing Lei 	le32_to_cpus(&buf);
10972108fd2SMing Lei 	*data = buf;
1102f7ca802SSteve Glendinning 
1112f7ca802SSteve Glendinning 	return ret;
1122f7ca802SSteve Glendinning }
1132f7ca802SSteve Glendinning 
114ec32115dSMing Lei static int __must_check __smsc95xx_write_reg(struct usbnet *dev, u32 index,
115ec32115dSMing Lei 					     u32 data, int in_pm)
1162f7ca802SSteve Glendinning {
11772108fd2SMing Lei 	u32 buf;
1182f7ca802SSteve Glendinning 	int ret;
119ec32115dSMing Lei 	int (*fn)(struct usbnet *, u8, u8, u16, u16, const void *, u16);
1202f7ca802SSteve Glendinning 
1212f7ca802SSteve Glendinning 	BUG_ON(!dev);
1222f7ca802SSteve Glendinning 
123ec32115dSMing Lei 	if (!in_pm)
124ec32115dSMing Lei 		fn = usbnet_write_cmd;
125ec32115dSMing Lei 	else
126ec32115dSMing Lei 		fn = usbnet_write_cmd_nopm;
127ec32115dSMing Lei 
12872108fd2SMing Lei 	buf = data;
12972108fd2SMing Lei 	cpu_to_le32s(&buf);
1302f7ca802SSteve Glendinning 
131ec32115dSMing Lei 	ret = fn(dev, USB_VENDOR_REQUEST_WRITE_REGISTER, USB_DIR_OUT
132ec32115dSMing Lei 		 | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
13372108fd2SMing Lei 		 0, index, &buf, 4);
1342f7ca802SSteve Glendinning 	if (unlikely(ret < 0))
1351e1d7412SJoe Perches 		netdev_warn(dev->net, "Failed to write reg index 0x%08x: %d\n",
1361e1d7412SJoe Perches 			    index, ret);
1372f7ca802SSteve Glendinning 
1382f7ca802SSteve Glendinning 	return ret;
1392f7ca802SSteve Glendinning }
1402f7ca802SSteve Glendinning 
141ec32115dSMing Lei static int __must_check smsc95xx_read_reg_nopm(struct usbnet *dev, u32 index,
142ec32115dSMing Lei 					       u32 *data)
143ec32115dSMing Lei {
144ec32115dSMing Lei 	return __smsc95xx_read_reg(dev, index, data, 1);
145ec32115dSMing Lei }
146ec32115dSMing Lei 
147ec32115dSMing Lei static int __must_check smsc95xx_write_reg_nopm(struct usbnet *dev, u32 index,
148ec32115dSMing Lei 						u32 data)
149ec32115dSMing Lei {
150ec32115dSMing Lei 	return __smsc95xx_write_reg(dev, index, data, 1);
151ec32115dSMing Lei }
152ec32115dSMing Lei 
153ec32115dSMing Lei static int __must_check smsc95xx_read_reg(struct usbnet *dev, u32 index,
154ec32115dSMing Lei 					  u32 *data)
155ec32115dSMing Lei {
156ec32115dSMing Lei 	return __smsc95xx_read_reg(dev, index, data, 0);
157ec32115dSMing Lei }
158ec32115dSMing Lei 
159ec32115dSMing Lei static int __must_check smsc95xx_write_reg(struct usbnet *dev, u32 index,
160ec32115dSMing Lei 					   u32 data)
161ec32115dSMing Lei {
162ec32115dSMing Lei 	return __smsc95xx_write_reg(dev, index, data, 0);
163ec32115dSMing Lei }
164e0e474a8SSteve Glendinning 
1652f7ca802SSteve Glendinning /* Loop until the read is completed with timeout
1662f7ca802SSteve Glendinning  * called with phy_mutex held */
167e5e3af83SSteve Glendinning static int __must_check __smsc95xx_phy_wait_not_busy(struct usbnet *dev,
168e5e3af83SSteve Glendinning 						     int in_pm)
1692f7ca802SSteve Glendinning {
1702f7ca802SSteve Glendinning 	unsigned long start_time = jiffies;
1712f7ca802SSteve Glendinning 	u32 val;
172769ea6d8SSteve Glendinning 	int ret;
1732f7ca802SSteve Glendinning 
1742f7ca802SSteve Glendinning 	do {
175e5e3af83SSteve Glendinning 		ret = __smsc95xx_read_reg(dev, MII_ADDR, &val, in_pm);
176b052e073SSteve Glendinning 		if (ret < 0) {
177b052e073SSteve Glendinning 			netdev_warn(dev->net, "Error reading MII_ACCESS\n");
178b052e073SSteve Glendinning 			return ret;
179b052e073SSteve Glendinning 		}
180b052e073SSteve Glendinning 
1812f7ca802SSteve Glendinning 		if (!(val & MII_BUSY_))
1822f7ca802SSteve Glendinning 			return 0;
1832f7ca802SSteve Glendinning 	} while (!time_after(jiffies, start_time + HZ));
1842f7ca802SSteve Glendinning 
1852f7ca802SSteve Glendinning 	return -EIO;
1862f7ca802SSteve Glendinning }
1872f7ca802SSteve Glendinning 
188e5e3af83SSteve Glendinning static int __smsc95xx_mdio_read(struct net_device *netdev, int phy_id, int idx,
189e5e3af83SSteve Glendinning 				int in_pm)
1902f7ca802SSteve Glendinning {
1912f7ca802SSteve Glendinning 	struct usbnet *dev = netdev_priv(netdev);
1922f7ca802SSteve Glendinning 	u32 val, addr;
193769ea6d8SSteve Glendinning 	int ret;
1942f7ca802SSteve Glendinning 
1952f7ca802SSteve Glendinning 	mutex_lock(&dev->phy_mutex);
1962f7ca802SSteve Glendinning 
1972f7ca802SSteve Glendinning 	/* confirm MII not busy */
198e5e3af83SSteve Glendinning 	ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
199b052e073SSteve Glendinning 	if (ret < 0) {
200b052e073SSteve Glendinning 		netdev_warn(dev->net, "MII is busy in smsc95xx_mdio_read\n");
201b052e073SSteve Glendinning 		goto done;
202b052e073SSteve Glendinning 	}
2032f7ca802SSteve Glendinning 
2042f7ca802SSteve Glendinning 	/* set the address, index & direction (read from PHY) */
2052f7ca802SSteve Glendinning 	phy_id &= dev->mii.phy_id_mask;
2062f7ca802SSteve Glendinning 	idx &= dev->mii.reg_num_mask;
20780928805SSteve Glendinning 	addr = (phy_id << 11) | (idx << 6) | MII_READ_ | MII_BUSY_;
208e5e3af83SSteve Glendinning 	ret = __smsc95xx_write_reg(dev, MII_ADDR, addr, in_pm);
209b052e073SSteve Glendinning 	if (ret < 0) {
210b052e073SSteve Glendinning 		netdev_warn(dev->net, "Error writing MII_ADDR\n");
211b052e073SSteve Glendinning 		goto done;
212b052e073SSteve Glendinning 	}
2132f7ca802SSteve Glendinning 
214e5e3af83SSteve Glendinning 	ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
215b052e073SSteve Glendinning 	if (ret < 0) {
216b052e073SSteve Glendinning 		netdev_warn(dev->net, "Timed out reading MII reg %02X\n", idx);
217b052e073SSteve Glendinning 		goto done;
218b052e073SSteve Glendinning 	}
219769ea6d8SSteve Glendinning 
220e5e3af83SSteve Glendinning 	ret = __smsc95xx_read_reg(dev, MII_DATA, &val, in_pm);
221b052e073SSteve Glendinning 	if (ret < 0) {
222b052e073SSteve Glendinning 		netdev_warn(dev->net, "Error reading MII_DATA\n");
223b052e073SSteve Glendinning 		goto done;
224b052e073SSteve Glendinning 	}
225769ea6d8SSteve Glendinning 
226769ea6d8SSteve Glendinning 	ret = (u16)(val & 0xFFFF);
227769ea6d8SSteve Glendinning 
228769ea6d8SSteve Glendinning done:
2292f7ca802SSteve Glendinning 	mutex_unlock(&dev->phy_mutex);
230769ea6d8SSteve Glendinning 	return ret;
2312f7ca802SSteve Glendinning }
2322f7ca802SSteve Glendinning 
233e5e3af83SSteve Glendinning static void __smsc95xx_mdio_write(struct net_device *netdev, int phy_id,
234e5e3af83SSteve Glendinning 				  int idx, int regval, int in_pm)
2352f7ca802SSteve Glendinning {
2362f7ca802SSteve Glendinning 	struct usbnet *dev = netdev_priv(netdev);
2372f7ca802SSteve Glendinning 	u32 val, addr;
238769ea6d8SSteve Glendinning 	int ret;
2392f7ca802SSteve Glendinning 
2402f7ca802SSteve Glendinning 	mutex_lock(&dev->phy_mutex);
2412f7ca802SSteve Glendinning 
2422f7ca802SSteve Glendinning 	/* confirm MII not busy */
243e5e3af83SSteve Glendinning 	ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
244b052e073SSteve Glendinning 	if (ret < 0) {
245b052e073SSteve Glendinning 		netdev_warn(dev->net, "MII is busy in smsc95xx_mdio_write\n");
246b052e073SSteve Glendinning 		goto done;
247b052e073SSteve Glendinning 	}
2482f7ca802SSteve Glendinning 
2492f7ca802SSteve Glendinning 	val = regval;
250e5e3af83SSteve Glendinning 	ret = __smsc95xx_write_reg(dev, MII_DATA, val, in_pm);
251b052e073SSteve Glendinning 	if (ret < 0) {
252b052e073SSteve Glendinning 		netdev_warn(dev->net, "Error writing MII_DATA\n");
253b052e073SSteve Glendinning 		goto done;
254b052e073SSteve Glendinning 	}
2552f7ca802SSteve Glendinning 
2562f7ca802SSteve Glendinning 	/* set the address, index & direction (write to PHY) */
2572f7ca802SSteve Glendinning 	phy_id &= dev->mii.phy_id_mask;
2582f7ca802SSteve Glendinning 	idx &= dev->mii.reg_num_mask;
25980928805SSteve Glendinning 	addr = (phy_id << 11) | (idx << 6) | MII_WRITE_ | MII_BUSY_;
260e5e3af83SSteve Glendinning 	ret = __smsc95xx_write_reg(dev, MII_ADDR, addr, in_pm);
261b052e073SSteve Glendinning 	if (ret < 0) {
262b052e073SSteve Glendinning 		netdev_warn(dev->net, "Error writing MII_ADDR\n");
263b052e073SSteve Glendinning 		goto done;
264b052e073SSteve Glendinning 	}
2652f7ca802SSteve Glendinning 
266e5e3af83SSteve Glendinning 	ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
267b052e073SSteve Glendinning 	if (ret < 0) {
268b052e073SSteve Glendinning 		netdev_warn(dev->net, "Timed out writing MII reg %02X\n", idx);
269b052e073SSteve Glendinning 		goto done;
270b052e073SSteve Glendinning 	}
2712f7ca802SSteve Glendinning 
272769ea6d8SSteve Glendinning done:
2732f7ca802SSteve Glendinning 	mutex_unlock(&dev->phy_mutex);
2742f7ca802SSteve Glendinning }
2752f7ca802SSteve Glendinning 
276e5e3af83SSteve Glendinning static int smsc95xx_mdio_read_nopm(struct net_device *netdev, int phy_id,
277e5e3af83SSteve Glendinning 				   int idx)
278e5e3af83SSteve Glendinning {
279e5e3af83SSteve Glendinning 	return __smsc95xx_mdio_read(netdev, phy_id, idx, 1);
280e5e3af83SSteve Glendinning }
281e5e3af83SSteve Glendinning 
282e5e3af83SSteve Glendinning static void smsc95xx_mdio_write_nopm(struct net_device *netdev, int phy_id,
283e5e3af83SSteve Glendinning 				     int idx, int regval)
284e5e3af83SSteve Glendinning {
285e5e3af83SSteve Glendinning 	__smsc95xx_mdio_write(netdev, phy_id, idx, regval, 1);
286e5e3af83SSteve Glendinning }
287e5e3af83SSteve Glendinning 
288e5e3af83SSteve Glendinning static int smsc95xx_mdio_read(struct net_device *netdev, int phy_id, int idx)
289e5e3af83SSteve Glendinning {
290e5e3af83SSteve Glendinning 	return __smsc95xx_mdio_read(netdev, phy_id, idx, 0);
291e5e3af83SSteve Glendinning }
292e5e3af83SSteve Glendinning 
293e5e3af83SSteve Glendinning static void smsc95xx_mdio_write(struct net_device *netdev, int phy_id, int idx,
294e5e3af83SSteve Glendinning 				int regval)
295e5e3af83SSteve Glendinning {
296e5e3af83SSteve Glendinning 	__smsc95xx_mdio_write(netdev, phy_id, idx, regval, 0);
297e5e3af83SSteve Glendinning }
298e5e3af83SSteve Glendinning 
299769ea6d8SSteve Glendinning static int __must_check smsc95xx_wait_eeprom(struct usbnet *dev)
3002f7ca802SSteve Glendinning {
3012f7ca802SSteve Glendinning 	unsigned long start_time = jiffies;
3022f7ca802SSteve Glendinning 	u32 val;
303769ea6d8SSteve Glendinning 	int ret;
3042f7ca802SSteve Glendinning 
3052f7ca802SSteve Glendinning 	do {
306769ea6d8SSteve Glendinning 		ret = smsc95xx_read_reg(dev, E2P_CMD, &val);
307b052e073SSteve Glendinning 		if (ret < 0) {
308b052e073SSteve Glendinning 			netdev_warn(dev->net, "Error reading E2P_CMD\n");
309b052e073SSteve Glendinning 			return ret;
310b052e073SSteve Glendinning 		}
311b052e073SSteve Glendinning 
3122f7ca802SSteve Glendinning 		if (!(val & E2P_CMD_BUSY_) || (val & E2P_CMD_TIMEOUT_))
3132f7ca802SSteve Glendinning 			break;
3142f7ca802SSteve Glendinning 		udelay(40);
3152f7ca802SSteve Glendinning 	} while (!time_after(jiffies, start_time + HZ));
3162f7ca802SSteve Glendinning 
3172f7ca802SSteve Glendinning 	if (val & (E2P_CMD_TIMEOUT_ | E2P_CMD_BUSY_)) {
31860b86755SJoe Perches 		netdev_warn(dev->net, "EEPROM read operation timeout\n");
3192f7ca802SSteve Glendinning 		return -EIO;
3202f7ca802SSteve Glendinning 	}
3212f7ca802SSteve Glendinning 
3222f7ca802SSteve Glendinning 	return 0;
3232f7ca802SSteve Glendinning }
3242f7ca802SSteve Glendinning 
325769ea6d8SSteve Glendinning static int __must_check smsc95xx_eeprom_confirm_not_busy(struct usbnet *dev)
3262f7ca802SSteve Glendinning {
3272f7ca802SSteve Glendinning 	unsigned long start_time = jiffies;
3282f7ca802SSteve Glendinning 	u32 val;
329769ea6d8SSteve Glendinning 	int ret;
3302f7ca802SSteve Glendinning 
3312f7ca802SSteve Glendinning 	do {
332769ea6d8SSteve Glendinning 		ret = smsc95xx_read_reg(dev, E2P_CMD, &val);
333b052e073SSteve Glendinning 		if (ret < 0) {
334b052e073SSteve Glendinning 			netdev_warn(dev->net, "Error reading E2P_CMD\n");
335b052e073SSteve Glendinning 			return ret;
336b052e073SSteve Glendinning 		}
3372f7ca802SSteve Glendinning 
3382f7ca802SSteve Glendinning 		if (!(val & E2P_CMD_BUSY_))
3392f7ca802SSteve Glendinning 			return 0;
3402f7ca802SSteve Glendinning 
3412f7ca802SSteve Glendinning 		udelay(40);
3422f7ca802SSteve Glendinning 	} while (!time_after(jiffies, start_time + HZ));
3432f7ca802SSteve Glendinning 
34460b86755SJoe Perches 	netdev_warn(dev->net, "EEPROM is busy\n");
3452f7ca802SSteve Glendinning 	return -EIO;
3462f7ca802SSteve Glendinning }
3472f7ca802SSteve Glendinning 
3482f7ca802SSteve Glendinning static int smsc95xx_read_eeprom(struct usbnet *dev, u32 offset, u32 length,
3492f7ca802SSteve Glendinning 				u8 *data)
3502f7ca802SSteve Glendinning {
3512f7ca802SSteve Glendinning 	u32 val;
3522f7ca802SSteve Glendinning 	int i, ret;
3532f7ca802SSteve Glendinning 
3542f7ca802SSteve Glendinning 	BUG_ON(!dev);
3552f7ca802SSteve Glendinning 	BUG_ON(!data);
3562f7ca802SSteve Glendinning 
3572f7ca802SSteve Glendinning 	ret = smsc95xx_eeprom_confirm_not_busy(dev);
3582f7ca802SSteve Glendinning 	if (ret)
3592f7ca802SSteve Glendinning 		return ret;
3602f7ca802SSteve Glendinning 
3612f7ca802SSteve Glendinning 	for (i = 0; i < length; i++) {
3622f7ca802SSteve Glendinning 		val = E2P_CMD_BUSY_ | E2P_CMD_READ_ | (offset & E2P_CMD_ADDR_);
363769ea6d8SSteve Glendinning 		ret = smsc95xx_write_reg(dev, E2P_CMD, val);
364b052e073SSteve Glendinning 		if (ret < 0) {
365b052e073SSteve Glendinning 			netdev_warn(dev->net, "Error writing E2P_CMD\n");
366b052e073SSteve Glendinning 			return ret;
367b052e073SSteve Glendinning 		}
3682f7ca802SSteve Glendinning 
3692f7ca802SSteve Glendinning 		ret = smsc95xx_wait_eeprom(dev);
3702f7ca802SSteve Glendinning 		if (ret < 0)
3712f7ca802SSteve Glendinning 			return ret;
3722f7ca802SSteve Glendinning 
373769ea6d8SSteve Glendinning 		ret = smsc95xx_read_reg(dev, E2P_DATA, &val);
374b052e073SSteve Glendinning 		if (ret < 0) {
375b052e073SSteve Glendinning 			netdev_warn(dev->net, "Error reading E2P_DATA\n");
376b052e073SSteve Glendinning 			return ret;
377b052e073SSteve Glendinning 		}
3782f7ca802SSteve Glendinning 
3792f7ca802SSteve Glendinning 		data[i] = val & 0xFF;
3802f7ca802SSteve Glendinning 		offset++;
3812f7ca802SSteve Glendinning 	}
3822f7ca802SSteve Glendinning 
3832f7ca802SSteve Glendinning 	return 0;
3842f7ca802SSteve Glendinning }
3852f7ca802SSteve Glendinning 
3862f7ca802SSteve Glendinning static int smsc95xx_write_eeprom(struct usbnet *dev, u32 offset, u32 length,
3872f7ca802SSteve Glendinning 				 u8 *data)
3882f7ca802SSteve Glendinning {
3892f7ca802SSteve Glendinning 	u32 val;
3902f7ca802SSteve Glendinning 	int i, ret;
3912f7ca802SSteve Glendinning 
3922f7ca802SSteve Glendinning 	BUG_ON(!dev);
3932f7ca802SSteve Glendinning 	BUG_ON(!data);
3942f7ca802SSteve Glendinning 
3952f7ca802SSteve Glendinning 	ret = smsc95xx_eeprom_confirm_not_busy(dev);
3962f7ca802SSteve Glendinning 	if (ret)
3972f7ca802SSteve Glendinning 		return ret;
3982f7ca802SSteve Glendinning 
3992f7ca802SSteve Glendinning 	/* Issue write/erase enable command */
4002f7ca802SSteve Glendinning 	val = E2P_CMD_BUSY_ | E2P_CMD_EWEN_;
401769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, E2P_CMD, val);
402b052e073SSteve Glendinning 	if (ret < 0) {
403b052e073SSteve Glendinning 		netdev_warn(dev->net, "Error writing E2P_DATA\n");
404b052e073SSteve Glendinning 		return ret;
405b052e073SSteve Glendinning 	}
4062f7ca802SSteve Glendinning 
4072f7ca802SSteve Glendinning 	ret = smsc95xx_wait_eeprom(dev);
4082f7ca802SSteve Glendinning 	if (ret < 0)
4092f7ca802SSteve Glendinning 		return ret;
4102f7ca802SSteve Glendinning 
4112f7ca802SSteve Glendinning 	for (i = 0; i < length; i++) {
4122f7ca802SSteve Glendinning 
4132f7ca802SSteve Glendinning 		/* Fill data register */
4142f7ca802SSteve Glendinning 		val = data[i];
415769ea6d8SSteve Glendinning 		ret = smsc95xx_write_reg(dev, E2P_DATA, val);
416b052e073SSteve Glendinning 		if (ret < 0) {
417b052e073SSteve Glendinning 			netdev_warn(dev->net, "Error writing E2P_DATA\n");
418b052e073SSteve Glendinning 			return ret;
419b052e073SSteve Glendinning 		}
4202f7ca802SSteve Glendinning 
4212f7ca802SSteve Glendinning 		/* Send "write" command */
4222f7ca802SSteve Glendinning 		val = E2P_CMD_BUSY_ | E2P_CMD_WRITE_ | (offset & E2P_CMD_ADDR_);
423769ea6d8SSteve Glendinning 		ret = smsc95xx_write_reg(dev, E2P_CMD, val);
424b052e073SSteve Glendinning 		if (ret < 0) {
425b052e073SSteve Glendinning 			netdev_warn(dev->net, "Error writing E2P_CMD\n");
426b052e073SSteve Glendinning 			return ret;
427b052e073SSteve Glendinning 		}
4282f7ca802SSteve Glendinning 
4292f7ca802SSteve Glendinning 		ret = smsc95xx_wait_eeprom(dev);
4302f7ca802SSteve Glendinning 		if (ret < 0)
4312f7ca802SSteve Glendinning 			return ret;
4322f7ca802SSteve Glendinning 
4332f7ca802SSteve Glendinning 		offset++;
4342f7ca802SSteve Glendinning 	}
4352f7ca802SSteve Glendinning 
4362f7ca802SSteve Glendinning 	return 0;
4372f7ca802SSteve Glendinning }
4382f7ca802SSteve Glendinning 
439769ea6d8SSteve Glendinning static int __must_check smsc95xx_write_reg_async(struct usbnet *dev, u16 index,
4407b9e7580SSteve Glendinning 						 u32 data)
4412f7ca802SSteve Glendinning {
4421d74a6bdSSteve Glendinning 	const u16 size = 4;
4437b9e7580SSteve Glendinning 	u32 buf;
44472108fd2SMing Lei 	int ret;
4452f7ca802SSteve Glendinning 
4467b9e7580SSteve Glendinning 	buf = data;
4477b9e7580SSteve Glendinning 	cpu_to_le32s(&buf);
4487b9e7580SSteve Glendinning 
44972108fd2SMing Lei 	ret = usbnet_write_cmd_async(dev, USB_VENDOR_REQUEST_WRITE_REGISTER,
45072108fd2SMing Lei 				     USB_DIR_OUT | USB_TYPE_VENDOR |
45172108fd2SMing Lei 				     USB_RECIP_DEVICE,
4527b9e7580SSteve Glendinning 				     0, index, &buf, size);
45372108fd2SMing Lei 	if (ret < 0)
45472108fd2SMing Lei 		netdev_warn(dev->net, "Error write async cmd, sts=%d\n",
45572108fd2SMing Lei 			    ret);
45672108fd2SMing Lei 	return ret;
4572f7ca802SSteve Glendinning }
4582f7ca802SSteve Glendinning 
4592f7ca802SSteve Glendinning /* returns hash bit number for given MAC address
4602f7ca802SSteve Glendinning  * example:
4612f7ca802SSteve Glendinning  * 01 00 5E 00 00 01 -> returns bit number 31 */
4622f7ca802SSteve Glendinning static unsigned int smsc95xx_hash(char addr[ETH_ALEN])
4632f7ca802SSteve Glendinning {
4642f7ca802SSteve Glendinning 	return (ether_crc(ETH_ALEN, addr) >> 26) & 0x3f;
4652f7ca802SSteve Glendinning }
4662f7ca802SSteve Glendinning 
4672f7ca802SSteve Glendinning static void smsc95xx_set_multicast(struct net_device *netdev)
4682f7ca802SSteve Glendinning {
4692f7ca802SSteve Glendinning 	struct usbnet *dev = netdev_priv(netdev);
4702f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
4712f7ca802SSteve Glendinning 	unsigned long flags;
472769ea6d8SSteve Glendinning 	int ret;
4732f7ca802SSteve Glendinning 
4743c0f3c60SMarc Zyngier 	pdata->hash_hi = 0;
4753c0f3c60SMarc Zyngier 	pdata->hash_lo = 0;
4763c0f3c60SMarc Zyngier 
4772f7ca802SSteve Glendinning 	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
4782f7ca802SSteve Glendinning 
4792f7ca802SSteve Glendinning 	if (dev->net->flags & IFF_PROMISC) {
480a475f603SJoe Perches 		netif_dbg(dev, drv, dev->net, "promiscuous mode enabled\n");
4812f7ca802SSteve Glendinning 		pdata->mac_cr |= MAC_CR_PRMS_;
4822f7ca802SSteve Glendinning 		pdata->mac_cr &= ~(MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
4832f7ca802SSteve Glendinning 	} else if (dev->net->flags & IFF_ALLMULTI) {
484a475f603SJoe Perches 		netif_dbg(dev, drv, dev->net, "receive all multicast enabled\n");
4852f7ca802SSteve Glendinning 		pdata->mac_cr |= MAC_CR_MCPAS_;
4862f7ca802SSteve Glendinning 		pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_HPFILT_);
4874cd24eafSJiri Pirko 	} else if (!netdev_mc_empty(dev->net)) {
48822bedad3SJiri Pirko 		struct netdev_hw_addr *ha;
4892f7ca802SSteve Glendinning 
4902f7ca802SSteve Glendinning 		pdata->mac_cr |= MAC_CR_HPFILT_;
4912f7ca802SSteve Glendinning 		pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_);
4922f7ca802SSteve Glendinning 
49322bedad3SJiri Pirko 		netdev_for_each_mc_addr(ha, netdev) {
49422bedad3SJiri Pirko 			u32 bitnum = smsc95xx_hash(ha->addr);
4952f7ca802SSteve Glendinning 			u32 mask = 0x01 << (bitnum & 0x1F);
4962f7ca802SSteve Glendinning 			if (bitnum & 0x20)
4973c0f3c60SMarc Zyngier 				pdata->hash_hi |= mask;
4982f7ca802SSteve Glendinning 			else
4993c0f3c60SMarc Zyngier 				pdata->hash_lo |= mask;
5002f7ca802SSteve Glendinning 		}
5012f7ca802SSteve Glendinning 
502a475f603SJoe Perches 		netif_dbg(dev, drv, dev->net, "HASHH=0x%08X, HASHL=0x%08X\n",
5033c0f3c60SMarc Zyngier 				   pdata->hash_hi, pdata->hash_lo);
5042f7ca802SSteve Glendinning 	} else {
505a475f603SJoe Perches 		netif_dbg(dev, drv, dev->net, "receive own packets only\n");
5062f7ca802SSteve Glendinning 		pdata->mac_cr &=
5072f7ca802SSteve Glendinning 			~(MAC_CR_PRMS_ | MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
5082f7ca802SSteve Glendinning 	}
5092f7ca802SSteve Glendinning 
5102f7ca802SSteve Glendinning 	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
5112f7ca802SSteve Glendinning 
5122f7ca802SSteve Glendinning 	/* Initiate async writes, as we can't wait for completion here */
5137b9e7580SSteve Glendinning 	ret = smsc95xx_write_reg_async(dev, HASHH, pdata->hash_hi);
514b052e073SSteve Glendinning 	if (ret < 0)
515b052e073SSteve Glendinning 		netdev_warn(dev->net, "failed to initiate async write to HASHH\n");
516769ea6d8SSteve Glendinning 
5177b9e7580SSteve Glendinning 	ret = smsc95xx_write_reg_async(dev, HASHL, pdata->hash_lo);
518b052e073SSteve Glendinning 	if (ret < 0)
519b052e073SSteve Glendinning 		netdev_warn(dev->net, "failed to initiate async write to HASHL\n");
520769ea6d8SSteve Glendinning 
5217b9e7580SSteve Glendinning 	ret = smsc95xx_write_reg_async(dev, MAC_CR, pdata->mac_cr);
522b052e073SSteve Glendinning 	if (ret < 0)
523b052e073SSteve Glendinning 		netdev_warn(dev->net, "failed to initiate async write to MAC_CR\n");
5242f7ca802SSteve Glendinning }
5252f7ca802SSteve Glendinning 
526769ea6d8SSteve Glendinning static int smsc95xx_phy_update_flowcontrol(struct usbnet *dev, u8 duplex,
5272f7ca802SSteve Glendinning 					   u16 lcladv, u16 rmtadv)
5282f7ca802SSteve Glendinning {
5299c082731SNisar Sayed 	u32 flow = 0, afc_cfg;
5302f7ca802SSteve Glendinning 
5312f7ca802SSteve Glendinning 	int ret = smsc95xx_read_reg(dev, AFC_CFG, &afc_cfg);
532e360a8b4SSteve Glendinning 	if (ret < 0)
533b052e073SSteve Glendinning 		return ret;
5342f7ca802SSteve Glendinning 
5352f7ca802SSteve Glendinning 	if (duplex == DUPLEX_FULL) {
536bc02ff95SSteve Glendinning 		u8 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
5372f7ca802SSteve Glendinning 
5382f7ca802SSteve Glendinning 		if (cap & FLOW_CTRL_RX)
5392f7ca802SSteve Glendinning 			flow = 0xFFFF0002;
5402f7ca802SSteve Glendinning 
5419c082731SNisar Sayed 		if (cap & FLOW_CTRL_TX) {
5422f7ca802SSteve Glendinning 			afc_cfg |= 0xF;
5439c082731SNisar Sayed 			flow |= 0xFFFF0000;
5449c082731SNisar Sayed 		} else {
5452f7ca802SSteve Glendinning 			afc_cfg &= ~0xF;
5469c082731SNisar Sayed 		}
5472f7ca802SSteve Glendinning 
548a475f603SJoe Perches 		netif_dbg(dev, link, dev->net, "rx pause %s, tx pause %s\n",
54960b86755SJoe Perches 				   cap & FLOW_CTRL_RX ? "enabled" : "disabled",
55060b86755SJoe Perches 				   cap & FLOW_CTRL_TX ? "enabled" : "disabled");
5512f7ca802SSteve Glendinning 	} else {
552a475f603SJoe Perches 		netif_dbg(dev, link, dev->net, "half duplex\n");
5532f7ca802SSteve Glendinning 		afc_cfg |= 0xF;
5542f7ca802SSteve Glendinning 	}
5552f7ca802SSteve Glendinning 
556769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, FLOW, flow);
557b052e073SSteve Glendinning 	if (ret < 0)
558b052e073SSteve Glendinning 		return ret;
559e360a8b4SSteve Glendinning 
560e360a8b4SSteve Glendinning 	return smsc95xx_write_reg(dev, AFC_CFG, afc_cfg);
5612f7ca802SSteve Glendinning }
5622f7ca802SSteve Glendinning 
5632f7ca802SSteve Glendinning static int smsc95xx_link_reset(struct usbnet *dev)
5642f7ca802SSteve Glendinning {
5652f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
5662f7ca802SSteve Glendinning 	struct mii_if_info *mii = &dev->mii;
5678ae6dacaSDavid Decotigny 	struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
5682f7ca802SSteve Glendinning 	unsigned long flags;
5692f7ca802SSteve Glendinning 	u16 lcladv, rmtadv;
570769ea6d8SSteve Glendinning 	int ret;
5712f7ca802SSteve Glendinning 
5722f7ca802SSteve Glendinning 	/* clear interrupt status */
573769ea6d8SSteve Glendinning 	ret = smsc95xx_mdio_read(dev->net, mii->phy_id, PHY_INT_SRC);
574e360a8b4SSteve Glendinning 	if (ret < 0)
575b052e073SSteve Glendinning 		return ret;
576769ea6d8SSteve Glendinning 
577769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
578e360a8b4SSteve Glendinning 	if (ret < 0)
579b052e073SSteve Glendinning 		return ret;
5802f7ca802SSteve Glendinning 
5812f7ca802SSteve Glendinning 	mii_check_media(mii, 1, 1);
5822f7ca802SSteve Glendinning 	mii_ethtool_gset(&dev->mii, &ecmd);
5832f7ca802SSteve Glendinning 	lcladv = smsc95xx_mdio_read(dev->net, mii->phy_id, MII_ADVERTISE);
5842f7ca802SSteve Glendinning 	rmtadv = smsc95xx_mdio_read(dev->net, mii->phy_id, MII_LPA);
5852f7ca802SSteve Glendinning 
5868ae6dacaSDavid Decotigny 	netif_dbg(dev, link, dev->net,
5878ae6dacaSDavid Decotigny 		  "speed: %u duplex: %d lcladv: %04x rmtadv: %04x\n",
5888ae6dacaSDavid Decotigny 		  ethtool_cmd_speed(&ecmd), ecmd.duplex, lcladv, rmtadv);
5892f7ca802SSteve Glendinning 
5902f7ca802SSteve Glendinning 	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
5912f7ca802SSteve Glendinning 	if (ecmd.duplex != DUPLEX_FULL) {
5922f7ca802SSteve Glendinning 		pdata->mac_cr &= ~MAC_CR_FDPX_;
5932f7ca802SSteve Glendinning 		pdata->mac_cr |= MAC_CR_RCVOWN_;
5942f7ca802SSteve Glendinning 	} else {
5952f7ca802SSteve Glendinning 		pdata->mac_cr &= ~MAC_CR_RCVOWN_;
5962f7ca802SSteve Glendinning 		pdata->mac_cr |= MAC_CR_FDPX_;
5972f7ca802SSteve Glendinning 	}
5982f7ca802SSteve Glendinning 	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
5992f7ca802SSteve Glendinning 
600769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
601e360a8b4SSteve Glendinning 	if (ret < 0)
602b052e073SSteve Glendinning 		return ret;
6032f7ca802SSteve Glendinning 
604769ea6d8SSteve Glendinning 	ret = smsc95xx_phy_update_flowcontrol(dev, ecmd.duplex, lcladv, rmtadv);
605b052e073SSteve Glendinning 	if (ret < 0)
606b052e073SSteve Glendinning 		netdev_warn(dev->net, "Error updating PHY flow control\n");
6072f7ca802SSteve Glendinning 
608b052e073SSteve Glendinning 	return ret;
6092f7ca802SSteve Glendinning }
6102f7ca802SSteve Glendinning 
6112f7ca802SSteve Glendinning static void smsc95xx_status(struct usbnet *dev, struct urb *urb)
6122f7ca802SSteve Glendinning {
6132f7ca802SSteve Glendinning 	u32 intdata;
6142f7ca802SSteve Glendinning 
6152f7ca802SSteve Glendinning 	if (urb->actual_length != 4) {
61660b86755SJoe Perches 		netdev_warn(dev->net, "unexpected urb length %d\n",
61760b86755SJoe Perches 			    urb->actual_length);
6182f7ca802SSteve Glendinning 		return;
6192f7ca802SSteve Glendinning 	}
6202f7ca802SSteve Glendinning 
621*6809d216SBen Dooks 	intdata = get_unaligned_le32(urb->transfer_buffer);
622a475f603SJoe Perches 	netif_dbg(dev, link, dev->net, "intdata: 0x%08X\n", intdata);
6232f7ca802SSteve Glendinning 
6242f7ca802SSteve Glendinning 	if (intdata & INT_ENP_PHY_INT_)
6252f7ca802SSteve Glendinning 		usbnet_defer_kevent(dev, EVENT_LINK_RESET);
6262f7ca802SSteve Glendinning 	else
62760b86755SJoe Perches 		netdev_warn(dev->net, "unexpected interrupt, intdata=0x%08X\n",
62860b86755SJoe Perches 			    intdata);
6292f7ca802SSteve Glendinning }
6302f7ca802SSteve Glendinning 
631d69d1694SChristoph Fritz static void set_carrier(struct usbnet *dev, bool link)
632d69d1694SChristoph Fritz {
633d69d1694SChristoph Fritz 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
634d69d1694SChristoph Fritz 
635d69d1694SChristoph Fritz 	if (pdata->link_ok == link)
636d69d1694SChristoph Fritz 		return;
637d69d1694SChristoph Fritz 
638d69d1694SChristoph Fritz 	pdata->link_ok = link;
639d69d1694SChristoph Fritz 
640d69d1694SChristoph Fritz 	if (link)
641d69d1694SChristoph Fritz 		usbnet_link_change(dev, 1, 0);
642d69d1694SChristoph Fritz 	else
643d69d1694SChristoph Fritz 		usbnet_link_change(dev, 0, 0);
644d69d1694SChristoph Fritz }
645d69d1694SChristoph Fritz 
646d69d1694SChristoph Fritz static void check_carrier(struct work_struct *work)
647d69d1694SChristoph Fritz {
648d69d1694SChristoph Fritz 	struct smsc95xx_priv *pdata = container_of(work, struct smsc95xx_priv,
649d69d1694SChristoph Fritz 						carrier_check.work);
650d69d1694SChristoph Fritz 	struct usbnet *dev = pdata->dev;
651d69d1694SChristoph Fritz 	int ret;
652d69d1694SChristoph Fritz 
653d69d1694SChristoph Fritz 	if (pdata->suspend_flags != 0)
654d69d1694SChristoph Fritz 		return;
655d69d1694SChristoph Fritz 
656d69d1694SChristoph Fritz 	ret = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, MII_BMSR);
657d69d1694SChristoph Fritz 	if (ret < 0) {
658d69d1694SChristoph Fritz 		netdev_warn(dev->net, "Failed to read MII_BMSR\n");
659d69d1694SChristoph Fritz 		return;
660d69d1694SChristoph Fritz 	}
661d69d1694SChristoph Fritz 	if (ret & BMSR_LSTATUS)
662d69d1694SChristoph Fritz 		set_carrier(dev, 1);
663d69d1694SChristoph Fritz 	else
664d69d1694SChristoph Fritz 		set_carrier(dev, 0);
665d69d1694SChristoph Fritz 
666d69d1694SChristoph Fritz 	schedule_delayed_work(&pdata->carrier_check, CARRIER_CHECK_DELAY);
667d69d1694SChristoph Fritz }
668d69d1694SChristoph Fritz 
669f7b29271SSteve Glendinning /* Enable or disable Tx & Rx checksum offload engines */
670c8f44affSMichał Mirosław static int smsc95xx_set_features(struct net_device *netdev,
671c8f44affSMichał Mirosław 	netdev_features_t features)
6722f7ca802SSteve Glendinning {
67378e47fe4SMichał Mirosław 	struct usbnet *dev = netdev_priv(netdev);
6742f7ca802SSteve Glendinning 	u32 read_buf;
67578e47fe4SMichał Mirosław 	int ret;
67678e47fe4SMichał Mirosław 
67778e47fe4SMichał Mirosław 	ret = smsc95xx_read_reg(dev, COE_CR, &read_buf);
678e360a8b4SSteve Glendinning 	if (ret < 0)
679b052e073SSteve Glendinning 		return ret;
6802f7ca802SSteve Glendinning 
681fe0cd8caSNisar Sayed 	if (features & NETIF_F_IP_CSUM)
682f7b29271SSteve Glendinning 		read_buf |= Tx_COE_EN_;
683f7b29271SSteve Glendinning 	else
684f7b29271SSteve Glendinning 		read_buf &= ~Tx_COE_EN_;
685f7b29271SSteve Glendinning 
68678e47fe4SMichał Mirosław 	if (features & NETIF_F_RXCSUM)
6872f7ca802SSteve Glendinning 		read_buf |= Rx_COE_EN_;
6882f7ca802SSteve Glendinning 	else
6892f7ca802SSteve Glendinning 		read_buf &= ~Rx_COE_EN_;
6902f7ca802SSteve Glendinning 
6912f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, COE_CR, read_buf);
692e360a8b4SSteve Glendinning 	if (ret < 0)
693b052e073SSteve Glendinning 		return ret;
6942f7ca802SSteve Glendinning 
695a475f603SJoe Perches 	netif_dbg(dev, hw, dev->net, "COE_CR = 0x%08x\n", read_buf);
6962f7ca802SSteve Glendinning 	return 0;
6972f7ca802SSteve Glendinning }
6982f7ca802SSteve Glendinning 
6992f7ca802SSteve Glendinning static int smsc95xx_ethtool_get_eeprom_len(struct net_device *net)
7002f7ca802SSteve Glendinning {
7012f7ca802SSteve Glendinning 	return MAX_EEPROM_SIZE;
7022f7ca802SSteve Glendinning }
7032f7ca802SSteve Glendinning 
7042f7ca802SSteve Glendinning static int smsc95xx_ethtool_get_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 	ee->magic = LAN95XX_EEPROM_MAGIC;
7102f7ca802SSteve Glendinning 
7112f7ca802SSteve Glendinning 	return smsc95xx_read_eeprom(dev, ee->offset, ee->len, data);
7122f7ca802SSteve Glendinning }
7132f7ca802SSteve Glendinning 
7142f7ca802SSteve Glendinning static int smsc95xx_ethtool_set_eeprom(struct net_device *netdev,
7152f7ca802SSteve Glendinning 				       struct ethtool_eeprom *ee, u8 *data)
7162f7ca802SSteve Glendinning {
7172f7ca802SSteve Glendinning 	struct usbnet *dev = netdev_priv(netdev);
7182f7ca802SSteve Glendinning 
7192f7ca802SSteve Glendinning 	if (ee->magic != LAN95XX_EEPROM_MAGIC) {
72060b86755SJoe Perches 		netdev_warn(dev->net, "EEPROM: magic value mismatch, magic = 0x%x\n",
7212f7ca802SSteve Glendinning 			    ee->magic);
7222f7ca802SSteve Glendinning 		return -EINVAL;
7232f7ca802SSteve Glendinning 	}
7242f7ca802SSteve Glendinning 
7252f7ca802SSteve Glendinning 	return smsc95xx_write_eeprom(dev, ee->offset, ee->len, data);
7262f7ca802SSteve Glendinning }
7272f7ca802SSteve Glendinning 
7289fa32e94SEmeric Vigier static int smsc95xx_ethtool_getregslen(struct net_device *netdev)
7299fa32e94SEmeric Vigier {
7309fa32e94SEmeric Vigier 	/* all smsc95xx registers */
73196245317SSteve Glendinning 	return COE_CR - ID_REV + sizeof(u32);
7329fa32e94SEmeric Vigier }
7339fa32e94SEmeric Vigier 
7349fa32e94SEmeric Vigier static void
7359fa32e94SEmeric Vigier smsc95xx_ethtool_getregs(struct net_device *netdev, struct ethtool_regs *regs,
7369fa32e94SEmeric Vigier 			 void *buf)
7379fa32e94SEmeric Vigier {
7389fa32e94SEmeric Vigier 	struct usbnet *dev = netdev_priv(netdev);
739d348446bSDan Carpenter 	unsigned int i, j;
740d348446bSDan Carpenter 	int retval;
7419fa32e94SEmeric Vigier 	u32 *data = buf;
7429fa32e94SEmeric Vigier 
7439fa32e94SEmeric Vigier 	retval = smsc95xx_read_reg(dev, ID_REV, &regs->version);
7449fa32e94SEmeric Vigier 	if (retval < 0) {
7459fa32e94SEmeric Vigier 		netdev_warn(netdev, "REGS: cannot read ID_REV\n");
7469fa32e94SEmeric Vigier 		return;
7479fa32e94SEmeric Vigier 	}
7489fa32e94SEmeric Vigier 
7499fa32e94SEmeric Vigier 	for (i = ID_REV, j = 0; i <= COE_CR; i += (sizeof(u32)), j++) {
7509fa32e94SEmeric Vigier 		retval = smsc95xx_read_reg(dev, i, &data[j]);
7519fa32e94SEmeric Vigier 		if (retval < 0) {
7529fa32e94SEmeric Vigier 			netdev_warn(netdev, "REGS: cannot read reg[%x]\n", i);
7539fa32e94SEmeric Vigier 			return;
7549fa32e94SEmeric Vigier 		}
7559fa32e94SEmeric Vigier 	}
7569fa32e94SEmeric Vigier }
7579fa32e94SEmeric Vigier 
758e0e474a8SSteve Glendinning static void smsc95xx_ethtool_get_wol(struct net_device *net,
759e0e474a8SSteve Glendinning 				     struct ethtool_wolinfo *wolinfo)
760e0e474a8SSteve Glendinning {
761e0e474a8SSteve Glendinning 	struct usbnet *dev = netdev_priv(net);
762e0e474a8SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
763e0e474a8SSteve Glendinning 
764e0e474a8SSteve Glendinning 	wolinfo->supported = SUPPORTED_WAKE;
765e0e474a8SSteve Glendinning 	wolinfo->wolopts = pdata->wolopts;
766e0e474a8SSteve Glendinning }
767e0e474a8SSteve Glendinning 
768e0e474a8SSteve Glendinning static int smsc95xx_ethtool_set_wol(struct net_device *net,
769e0e474a8SSteve Glendinning 				    struct ethtool_wolinfo *wolinfo)
770e0e474a8SSteve Glendinning {
771e0e474a8SSteve Glendinning 	struct usbnet *dev = netdev_priv(net);
772e0e474a8SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
7733b14692cSSteve Glendinning 	int ret;
774e0e474a8SSteve Glendinning 
775c530c471SFlorian Fainelli 	if (wolinfo->wolopts & ~SUPPORTED_WAKE)
776c530c471SFlorian Fainelli 		return -EINVAL;
777c530c471SFlorian Fainelli 
778e0e474a8SSteve Glendinning 	pdata->wolopts = wolinfo->wolopts & SUPPORTED_WAKE;
7793b14692cSSteve Glendinning 
7803b14692cSSteve Glendinning 	ret = device_set_wakeup_enable(&dev->udev->dev, pdata->wolopts);
781b052e073SSteve Glendinning 	if (ret < 0)
782b052e073SSteve Glendinning 		netdev_warn(dev->net, "device_set_wakeup_enable error %d\n", ret);
7833b14692cSSteve Glendinning 
784b052e073SSteve Glendinning 	return ret;
785e0e474a8SSteve Glendinning }
786e0e474a8SSteve Glendinning 
78713722bbeSWoojung Huh static int get_mdix_status(struct net_device *net)
78813722bbeSWoojung Huh {
78913722bbeSWoojung Huh 	struct usbnet *dev = netdev_priv(net);
79013722bbeSWoojung Huh 	u32 val;
79113722bbeSWoojung Huh 	int buf;
79213722bbeSWoojung Huh 
79313722bbeSWoojung Huh 	buf = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, SPECIAL_CTRL_STS);
79413722bbeSWoojung Huh 	if (buf & SPECIAL_CTRL_STS_OVRRD_AMDIX_) {
79513722bbeSWoojung Huh 		if (buf & SPECIAL_CTRL_STS_AMDIX_ENABLE_)
79613722bbeSWoojung Huh 			return ETH_TP_MDI_AUTO;
79713722bbeSWoojung Huh 		else if (buf & SPECIAL_CTRL_STS_AMDIX_STATE_)
79813722bbeSWoojung Huh 			return ETH_TP_MDI_X;
79913722bbeSWoojung Huh 	} else {
80013722bbeSWoojung Huh 		buf = smsc95xx_read_reg(dev, STRAP_STATUS, &val);
80113722bbeSWoojung Huh 		if (val & STRAP_STATUS_AMDIX_EN_)
80213722bbeSWoojung Huh 			return ETH_TP_MDI_AUTO;
80313722bbeSWoojung Huh 	}
80413722bbeSWoojung Huh 
80513722bbeSWoojung Huh 	return ETH_TP_MDI;
80613722bbeSWoojung Huh }
80713722bbeSWoojung Huh 
80813722bbeSWoojung Huh static void set_mdix_status(struct net_device *net, __u8 mdix_ctrl)
80913722bbeSWoojung Huh {
81013722bbeSWoojung Huh 	struct usbnet *dev = netdev_priv(net);
81113722bbeSWoojung Huh 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
81213722bbeSWoojung Huh 	int buf;
81313722bbeSWoojung Huh 
81413722bbeSWoojung Huh 	if ((pdata->chip_id == ID_REV_CHIP_ID_9500A_) ||
81513722bbeSWoojung Huh 	    (pdata->chip_id == ID_REV_CHIP_ID_9530_) ||
81613722bbeSWoojung Huh 	    (pdata->chip_id == ID_REV_CHIP_ID_89530_) ||
81713722bbeSWoojung Huh 	    (pdata->chip_id == ID_REV_CHIP_ID_9730_)) {
81813722bbeSWoojung Huh 		/* Extend Manual AutoMDIX timer for 9500A/9500Ai */
81913722bbeSWoojung Huh 		buf = smsc95xx_mdio_read(dev->net, dev->mii.phy_id,
82013722bbeSWoojung Huh 					 PHY_EDPD_CONFIG);
82113722bbeSWoojung Huh 		buf |= PHY_EDPD_CONFIG_EXT_CROSSOVER_;
82213722bbeSWoojung Huh 		smsc95xx_mdio_write(dev->net, dev->mii.phy_id,
82313722bbeSWoojung Huh 				    PHY_EDPD_CONFIG, buf);
82413722bbeSWoojung Huh 	}
82513722bbeSWoojung Huh 
82613722bbeSWoojung Huh 	if (mdix_ctrl == ETH_TP_MDI) {
82713722bbeSWoojung Huh 		buf = smsc95xx_mdio_read(dev->net, dev->mii.phy_id,
82813722bbeSWoojung Huh 					 SPECIAL_CTRL_STS);
82913722bbeSWoojung Huh 		buf |= SPECIAL_CTRL_STS_OVRRD_AMDIX_;
83013722bbeSWoojung Huh 		buf &= ~(SPECIAL_CTRL_STS_AMDIX_ENABLE_ |
83113722bbeSWoojung Huh 			 SPECIAL_CTRL_STS_AMDIX_STATE_);
83213722bbeSWoojung Huh 		smsc95xx_mdio_write(dev->net, dev->mii.phy_id,
83313722bbeSWoojung Huh 				    SPECIAL_CTRL_STS, buf);
83413722bbeSWoojung Huh 	} else if (mdix_ctrl == ETH_TP_MDI_X) {
83513722bbeSWoojung Huh 		buf = smsc95xx_mdio_read(dev->net, dev->mii.phy_id,
83613722bbeSWoojung Huh 					 SPECIAL_CTRL_STS);
83713722bbeSWoojung Huh 		buf |= SPECIAL_CTRL_STS_OVRRD_AMDIX_;
83813722bbeSWoojung Huh 		buf &= ~(SPECIAL_CTRL_STS_AMDIX_ENABLE_ |
83913722bbeSWoojung Huh 			 SPECIAL_CTRL_STS_AMDIX_STATE_);
84013722bbeSWoojung Huh 		buf |= SPECIAL_CTRL_STS_AMDIX_STATE_;
84113722bbeSWoojung Huh 		smsc95xx_mdio_write(dev->net, dev->mii.phy_id,
84213722bbeSWoojung Huh 				    SPECIAL_CTRL_STS, buf);
84313722bbeSWoojung Huh 	} else if (mdix_ctrl == ETH_TP_MDI_AUTO) {
84413722bbeSWoojung Huh 		buf = smsc95xx_mdio_read(dev->net, dev->mii.phy_id,
84513722bbeSWoojung Huh 					 SPECIAL_CTRL_STS);
84613722bbeSWoojung Huh 		buf &= ~SPECIAL_CTRL_STS_OVRRD_AMDIX_;
84713722bbeSWoojung Huh 		buf &= ~(SPECIAL_CTRL_STS_AMDIX_ENABLE_ |
84813722bbeSWoojung Huh 			 SPECIAL_CTRL_STS_AMDIX_STATE_);
84913722bbeSWoojung Huh 		buf |= SPECIAL_CTRL_STS_AMDIX_ENABLE_;
85013722bbeSWoojung Huh 		smsc95xx_mdio_write(dev->net, dev->mii.phy_id,
85113722bbeSWoojung Huh 				    SPECIAL_CTRL_STS, buf);
85213722bbeSWoojung Huh 	}
85313722bbeSWoojung Huh 	pdata->mdix_ctrl = mdix_ctrl;
85413722bbeSWoojung Huh }
85513722bbeSWoojung Huh 
856eaf9a32aSPhilippe Reynes static int smsc95xx_get_link_ksettings(struct net_device *net,
857eaf9a32aSPhilippe Reynes 				       struct ethtool_link_ksettings *cmd)
85813722bbeSWoojung Huh {
85913722bbeSWoojung Huh 	struct usbnet *dev = netdev_priv(net);
86013722bbeSWoojung Huh 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
86113722bbeSWoojung Huh 	int retval;
86213722bbeSWoojung Huh 
863eaf9a32aSPhilippe Reynes 	retval = usbnet_get_link_ksettings(net, cmd);
86413722bbeSWoojung Huh 
865eaf9a32aSPhilippe Reynes 	cmd->base.eth_tp_mdix = pdata->mdix_ctrl;
866eaf9a32aSPhilippe Reynes 	cmd->base.eth_tp_mdix_ctrl = pdata->mdix_ctrl;
86713722bbeSWoojung Huh 
86813722bbeSWoojung Huh 	return retval;
86913722bbeSWoojung Huh }
87013722bbeSWoojung Huh 
871eaf9a32aSPhilippe Reynes static int smsc95xx_set_link_ksettings(struct net_device *net,
872eaf9a32aSPhilippe Reynes 				       const struct ethtool_link_ksettings *cmd)
87313722bbeSWoojung Huh {
87413722bbeSWoojung Huh 	struct usbnet *dev = netdev_priv(net);
87513722bbeSWoojung Huh 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
87613722bbeSWoojung Huh 	int retval;
87713722bbeSWoojung Huh 
878eaf9a32aSPhilippe Reynes 	if (pdata->mdix_ctrl != cmd->base.eth_tp_mdix_ctrl)
879eaf9a32aSPhilippe Reynes 		set_mdix_status(net, cmd->base.eth_tp_mdix_ctrl);
88013722bbeSWoojung Huh 
881eaf9a32aSPhilippe Reynes 	retval = usbnet_set_link_ksettings(net, cmd);
88213722bbeSWoojung Huh 
88313722bbeSWoojung Huh 	return retval;
88413722bbeSWoojung Huh }
88513722bbeSWoojung Huh 
8860fc0b732SStephen Hemminger static const struct ethtool_ops smsc95xx_ethtool_ops = {
8872f7ca802SSteve Glendinning 	.get_link	= usbnet_get_link,
8882f7ca802SSteve Glendinning 	.nway_reset	= usbnet_nway_reset,
8892f7ca802SSteve Glendinning 	.get_drvinfo	= usbnet_get_drvinfo,
8902f7ca802SSteve Glendinning 	.get_msglevel	= usbnet_get_msglevel,
8912f7ca802SSteve Glendinning 	.set_msglevel	= usbnet_set_msglevel,
8922f7ca802SSteve Glendinning 	.get_eeprom_len	= smsc95xx_ethtool_get_eeprom_len,
8932f7ca802SSteve Glendinning 	.get_eeprom	= smsc95xx_ethtool_get_eeprom,
8942f7ca802SSteve Glendinning 	.set_eeprom	= smsc95xx_ethtool_set_eeprom,
8959fa32e94SEmeric Vigier 	.get_regs_len	= smsc95xx_ethtool_getregslen,
8969fa32e94SEmeric Vigier 	.get_regs	= smsc95xx_ethtool_getregs,
897e0e474a8SSteve Glendinning 	.get_wol	= smsc95xx_ethtool_get_wol,
898e0e474a8SSteve Glendinning 	.set_wol	= smsc95xx_ethtool_set_wol,
899eaf9a32aSPhilippe Reynes 	.get_link_ksettings	= smsc95xx_get_link_ksettings,
900eaf9a32aSPhilippe Reynes 	.set_link_ksettings	= smsc95xx_set_link_ksettings,
901a8f5cb9eSPetr Kulhavy 	.get_ts_info	= ethtool_op_get_ts_info,
9022f7ca802SSteve Glendinning };
9032f7ca802SSteve Glendinning 
9042f7ca802SSteve Glendinning static int smsc95xx_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
9052f7ca802SSteve Glendinning {
9062f7ca802SSteve Glendinning 	struct usbnet *dev = netdev_priv(netdev);
9072f7ca802SSteve Glendinning 
9082f7ca802SSteve Glendinning 	if (!netif_running(netdev))
9092f7ca802SSteve Glendinning 		return -EINVAL;
9102f7ca802SSteve Glendinning 
9112f7ca802SSteve Glendinning 	return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
9122f7ca802SSteve Glendinning }
9132f7ca802SSteve Glendinning 
9142f7ca802SSteve Glendinning static void smsc95xx_init_mac_address(struct usbnet *dev)
9152f7ca802SSteve Glendinning {
916c489565bSArnd Bergmann 	const u8 *mac_addr;
917c489565bSArnd Bergmann 
918c489565bSArnd Bergmann 	/* maybe the boot loader passed the MAC address in devicetree */
919c489565bSArnd Bergmann 	mac_addr = of_get_mac_address(dev->udev->dev.of_node);
920c489565bSArnd Bergmann 	if (mac_addr) {
921c489565bSArnd Bergmann 		memcpy(dev->net->dev_addr, mac_addr, ETH_ALEN);
922c489565bSArnd Bergmann 		return;
923c489565bSArnd Bergmann 	}
924c489565bSArnd Bergmann 
9252f7ca802SSteve Glendinning 	/* try reading mac address from EEPROM */
9262f7ca802SSteve Glendinning 	if (smsc95xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN,
9272f7ca802SSteve Glendinning 			dev->net->dev_addr) == 0) {
9282f7ca802SSteve Glendinning 		if (is_valid_ether_addr(dev->net->dev_addr)) {
9292f7ca802SSteve Glendinning 			/* eeprom values are valid so use them */
930a475f603SJoe Perches 			netif_dbg(dev, ifup, dev->net, "MAC address read from EEPROM\n");
9312f7ca802SSteve Glendinning 			return;
9322f7ca802SSteve Glendinning 		}
9332f7ca802SSteve Glendinning 	}
9342f7ca802SSteve Glendinning 
935c489565bSArnd Bergmann 	/* no useful static MAC address found. generate a random one */
936f2cedb63SDanny Kukawka 	eth_hw_addr_random(dev->net);
937c7e12eadSJoe Perches 	netif_dbg(dev, ifup, dev->net, "MAC address set to eth_random_addr\n");
9382f7ca802SSteve Glendinning }
9392f7ca802SSteve Glendinning 
9402f7ca802SSteve Glendinning static int smsc95xx_set_mac_address(struct usbnet *dev)
9412f7ca802SSteve Glendinning {
9422f7ca802SSteve Glendinning 	u32 addr_lo = dev->net->dev_addr[0] | dev->net->dev_addr[1] << 8 |
9432f7ca802SSteve Glendinning 		dev->net->dev_addr[2] << 16 | dev->net->dev_addr[3] << 24;
9442f7ca802SSteve Glendinning 	u32 addr_hi = dev->net->dev_addr[4] | dev->net->dev_addr[5] << 8;
9452f7ca802SSteve Glendinning 	int ret;
9462f7ca802SSteve Glendinning 
9472f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, ADDRL, addr_lo);
948b052e073SSteve Glendinning 	if (ret < 0)
949b052e073SSteve Glendinning 		return ret;
950e360a8b4SSteve Glendinning 
951e360a8b4SSteve Glendinning 	return smsc95xx_write_reg(dev, ADDRH, addr_hi);
9522f7ca802SSteve Glendinning }
9532f7ca802SSteve Glendinning 
9542f7ca802SSteve Glendinning /* starts the TX path */
955769ea6d8SSteve Glendinning static int smsc95xx_start_tx_path(struct usbnet *dev)
9562f7ca802SSteve Glendinning {
9572f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
9582f7ca802SSteve Glendinning 	unsigned long flags;
959769ea6d8SSteve Glendinning 	int ret;
9602f7ca802SSteve Glendinning 
9612f7ca802SSteve Glendinning 	/* Enable Tx at MAC */
9622f7ca802SSteve Glendinning 	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
9632f7ca802SSteve Glendinning 	pdata->mac_cr |= MAC_CR_TXEN_;
9642f7ca802SSteve Glendinning 	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
9652f7ca802SSteve Glendinning 
966769ea6d8SSteve Glendinning 	ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
967e360a8b4SSteve Glendinning 	if (ret < 0)
968b052e073SSteve Glendinning 		return ret;
9692f7ca802SSteve Glendinning 
9702f7ca802SSteve Glendinning 	/* Enable Tx at SCSRs */
971e360a8b4SSteve Glendinning 	return smsc95xx_write_reg(dev, TX_CFG, TX_CFG_ON_);
9722f7ca802SSteve Glendinning }
9732f7ca802SSteve Glendinning 
9742f7ca802SSteve Glendinning /* Starts the Receive path */
975ec32115dSMing Lei static int smsc95xx_start_rx_path(struct usbnet *dev, int in_pm)
9762f7ca802SSteve Glendinning {
9772f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
9782f7ca802SSteve Glendinning 	unsigned long flags;
9792f7ca802SSteve Glendinning 
9802f7ca802SSteve Glendinning 	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
9812f7ca802SSteve Glendinning 	pdata->mac_cr |= MAC_CR_RXEN_;
9822f7ca802SSteve Glendinning 	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
9832f7ca802SSteve Glendinning 
984e360a8b4SSteve Glendinning 	return __smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr, in_pm);
9852f7ca802SSteve Glendinning }
9862f7ca802SSteve Glendinning 
9872f7ca802SSteve Glendinning static int smsc95xx_phy_initialize(struct usbnet *dev)
9882f7ca802SSteve Glendinning {
989769ea6d8SSteve Glendinning 	int bmcr, ret, timeout = 0;
990db443c44SSteve Glendinning 
9912f7ca802SSteve Glendinning 	/* Initialize MII structure */
9922f7ca802SSteve Glendinning 	dev->mii.dev = dev->net;
9932f7ca802SSteve Glendinning 	dev->mii.mdio_read = smsc95xx_mdio_read;
9942f7ca802SSteve Glendinning 	dev->mii.mdio_write = smsc95xx_mdio_write;
9952f7ca802SSteve Glendinning 	dev->mii.phy_id_mask = 0x1f;
9962f7ca802SSteve Glendinning 	dev->mii.reg_num_mask = 0x1f;
9972f7ca802SSteve Glendinning 	dev->mii.phy_id = SMSC95XX_INTERNAL_PHY_ID;
9982f7ca802SSteve Glendinning 
999db443c44SSteve Glendinning 	/* reset phy and wait for reset to complete */
10002f7ca802SSteve Glendinning 	smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
1001db443c44SSteve Glendinning 
1002db443c44SSteve Glendinning 	do {
1003db443c44SSteve Glendinning 		msleep(10);
1004db443c44SSteve Glendinning 		bmcr = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, MII_BMCR);
1005db443c44SSteve Glendinning 		timeout++;
1006d9460920SRabin Vincent 	} while ((bmcr & BMCR_RESET) && (timeout < 100));
1007db443c44SSteve Glendinning 
1008db443c44SSteve Glendinning 	if (timeout >= 100) {
1009db443c44SSteve Glendinning 		netdev_warn(dev->net, "timeout on PHY Reset");
1010db443c44SSteve Glendinning 		return -EIO;
1011db443c44SSteve Glendinning 	}
1012db443c44SSteve Glendinning 
10132f7ca802SSteve Glendinning 	smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
10142f7ca802SSteve Glendinning 		ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP |
10152f7ca802SSteve Glendinning 		ADVERTISE_PAUSE_ASYM);
10162f7ca802SSteve Glendinning 
10172f7ca802SSteve Glendinning 	/* read to clear */
1018769ea6d8SSteve Glendinning 	ret = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, PHY_INT_SRC);
1019b052e073SSteve Glendinning 	if (ret < 0) {
1020b052e073SSteve Glendinning 		netdev_warn(dev->net, "Failed to read PHY_INT_SRC during init\n");
1021b052e073SSteve Glendinning 		return ret;
1022b052e073SSteve Glendinning 	}
10232f7ca802SSteve Glendinning 
10242f7ca802SSteve Glendinning 	smsc95xx_mdio_write(dev->net, dev->mii.phy_id, PHY_INT_MASK,
10252f7ca802SSteve Glendinning 		PHY_INT_MASK_DEFAULT_);
10262f7ca802SSteve Glendinning 	mii_nway_restart(&dev->mii);
10272f7ca802SSteve Glendinning 
1028a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net, "phy initialised successfully\n");
10292f7ca802SSteve Glendinning 	return 0;
10302f7ca802SSteve Glendinning }
10312f7ca802SSteve Glendinning 
10322f7ca802SSteve Glendinning static int smsc95xx_reset(struct usbnet *dev)
10332f7ca802SSteve Glendinning {
10342f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
10352f7ca802SSteve Glendinning 	u32 read_buf, write_buf, burst_cap;
10362f7ca802SSteve Glendinning 	int ret = 0, timeout;
10372f7ca802SSteve Glendinning 
1038a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net, "entering smsc95xx_reset\n");
10392f7ca802SSteve Glendinning 
10404436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, HW_CFG, HW_CFG_LRST_);
1041e360a8b4SSteve Glendinning 	if (ret < 0)
1042b052e073SSteve Glendinning 		return ret;
10432f7ca802SSteve Glendinning 
10442f7ca802SSteve Glendinning 	timeout = 0;
10452f7ca802SSteve Glendinning 	do {
1046cf2acec2SSteve Glendinning 		msleep(10);
10472f7ca802SSteve Glendinning 		ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
1048e360a8b4SSteve Glendinning 		if (ret < 0)
1049b052e073SSteve Glendinning 			return ret;
10502f7ca802SSteve Glendinning 		timeout++;
10512f7ca802SSteve Glendinning 	} while ((read_buf & HW_CFG_LRST_) && (timeout < 100));
10522f7ca802SSteve Glendinning 
10532f7ca802SSteve Glendinning 	if (timeout >= 100) {
105460b86755SJoe Perches 		netdev_warn(dev->net, "timeout waiting for completion of Lite Reset\n");
10552f7ca802SSteve Glendinning 		return ret;
10562f7ca802SSteve Glendinning 	}
10572f7ca802SSteve Glendinning 
10584436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, PM_CTRL, PM_CTL_PHY_RST_);
1059e360a8b4SSteve Glendinning 	if (ret < 0)
1060b052e073SSteve Glendinning 		return ret;
10612f7ca802SSteve Glendinning 
10622f7ca802SSteve Glendinning 	timeout = 0;
10632f7ca802SSteve Glendinning 	do {
1064cf2acec2SSteve Glendinning 		msleep(10);
10652f7ca802SSteve Glendinning 		ret = smsc95xx_read_reg(dev, PM_CTRL, &read_buf);
1066e360a8b4SSteve Glendinning 		if (ret < 0)
1067b052e073SSteve Glendinning 			return ret;
10682f7ca802SSteve Glendinning 		timeout++;
10692f7ca802SSteve Glendinning 	} while ((read_buf & PM_CTL_PHY_RST_) && (timeout < 100));
10702f7ca802SSteve Glendinning 
10712f7ca802SSteve Glendinning 	if (timeout >= 100) {
107260b86755SJoe Perches 		netdev_warn(dev->net, "timeout waiting for PHY Reset\n");
10732f7ca802SSteve Glendinning 		return ret;
10742f7ca802SSteve Glendinning 	}
10752f7ca802SSteve Glendinning 
10762f7ca802SSteve Glendinning 	ret = smsc95xx_set_mac_address(dev);
10772f7ca802SSteve Glendinning 	if (ret < 0)
10782f7ca802SSteve Glendinning 		return ret;
10792f7ca802SSteve Glendinning 
10801e1d7412SJoe Perches 	netif_dbg(dev, ifup, dev->net, "MAC Address: %pM\n",
10811e1d7412SJoe Perches 		  dev->net->dev_addr);
10822f7ca802SSteve Glendinning 
10832f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
1084e360a8b4SSteve Glendinning 	if (ret < 0)
1085b052e073SSteve Glendinning 		return ret;
10862f7ca802SSteve Glendinning 
10871e1d7412SJoe Perches 	netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG : 0x%08x\n",
10881e1d7412SJoe Perches 		  read_buf);
10892f7ca802SSteve Glendinning 
10902f7ca802SSteve Glendinning 	read_buf |= HW_CFG_BIR_;
10912f7ca802SSteve Glendinning 
10922f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
1093e360a8b4SSteve Glendinning 	if (ret < 0)
1094b052e073SSteve Glendinning 		return ret;
10952f7ca802SSteve Glendinning 
10962f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
1097e360a8b4SSteve Glendinning 	if (ret < 0)
1098b052e073SSteve Glendinning 		return ret;
1099b052e073SSteve Glendinning 
1100a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net,
1101a475f603SJoe Perches 		  "Read Value from HW_CFG after writing HW_CFG_BIR_: 0x%08x\n",
110260b86755SJoe Perches 		  read_buf);
11032f7ca802SSteve Glendinning 
11042f7ca802SSteve Glendinning 	if (!turbo_mode) {
11052f7ca802SSteve Glendinning 		burst_cap = 0;
11062f7ca802SSteve Glendinning 		dev->rx_urb_size = MAX_SINGLE_PACKET_SIZE;
11072f7ca802SSteve Glendinning 	} else if (dev->udev->speed == USB_SPEED_HIGH) {
11082f7ca802SSteve Glendinning 		burst_cap = DEFAULT_HS_BURST_CAP_SIZE / HS_USB_PKT_SIZE;
11092f7ca802SSteve Glendinning 		dev->rx_urb_size = DEFAULT_HS_BURST_CAP_SIZE;
11102f7ca802SSteve Glendinning 	} else {
11112f7ca802SSteve Glendinning 		burst_cap = DEFAULT_FS_BURST_CAP_SIZE / FS_USB_PKT_SIZE;
11122f7ca802SSteve Glendinning 		dev->rx_urb_size = DEFAULT_FS_BURST_CAP_SIZE;
11132f7ca802SSteve Glendinning 	}
11142f7ca802SSteve Glendinning 
11151e1d7412SJoe Perches 	netif_dbg(dev, ifup, dev->net, "rx_urb_size=%ld\n",
11161e1d7412SJoe Perches 		  (ulong)dev->rx_urb_size);
11172f7ca802SSteve Glendinning 
11182f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, BURST_CAP, burst_cap);
1119e360a8b4SSteve Glendinning 	if (ret < 0)
1120b052e073SSteve Glendinning 		return ret;
11212f7ca802SSteve Glendinning 
11222f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, BURST_CAP, &read_buf);
1123e360a8b4SSteve Glendinning 	if (ret < 0)
1124b052e073SSteve Glendinning 		return ret;
1125769ea6d8SSteve Glendinning 
1126a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net,
1127a475f603SJoe Perches 		  "Read Value from BURST_CAP after writing: 0x%08x\n",
11282f7ca802SSteve Glendinning 		  read_buf);
11292f7ca802SSteve Glendinning 
11304436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, BULK_IN_DLY, DEFAULT_BULK_IN_DELAY);
1131e360a8b4SSteve Glendinning 	if (ret < 0)
1132b052e073SSteve Glendinning 		return ret;
11332f7ca802SSteve Glendinning 
11342f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, BULK_IN_DLY, &read_buf);
1135e360a8b4SSteve Glendinning 	if (ret < 0)
1136b052e073SSteve Glendinning 		return ret;
1137769ea6d8SSteve Glendinning 
1138a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net,
1139a475f603SJoe Perches 		  "Read Value from BULK_IN_DLY after writing: 0x%08x\n",
114060b86755SJoe Perches 		  read_buf);
11412f7ca802SSteve Glendinning 
11422f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
1143e360a8b4SSteve Glendinning 	if (ret < 0)
1144b052e073SSteve Glendinning 		return ret;
1145769ea6d8SSteve Glendinning 
11461e1d7412SJoe Perches 	netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG: 0x%08x\n",
11471e1d7412SJoe Perches 		  read_buf);
11482f7ca802SSteve Glendinning 
11492f7ca802SSteve Glendinning 	if (turbo_mode)
11502f7ca802SSteve Glendinning 		read_buf |= (HW_CFG_MEF_ | HW_CFG_BCE_);
11512f7ca802SSteve Glendinning 
11522f7ca802SSteve Glendinning 	read_buf &= ~HW_CFG_RXDOFF_;
11532f7ca802SSteve Glendinning 
11542f7ca802SSteve Glendinning 	/* set Rx data offset=2, Make IP header aligns on word boundary. */
11552f7ca802SSteve Glendinning 	read_buf |= NET_IP_ALIGN << 9;
11562f7ca802SSteve Glendinning 
11572f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
1158e360a8b4SSteve Glendinning 	if (ret < 0)
1159b052e073SSteve Glendinning 		return ret;
11602f7ca802SSteve Glendinning 
11612f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
1162e360a8b4SSteve Glendinning 	if (ret < 0)
1163b052e073SSteve Glendinning 		return ret;
1164769ea6d8SSteve Glendinning 
1165a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net,
1166a475f603SJoe Perches 		  "Read Value from HW_CFG after writing: 0x%08x\n", read_buf);
11672f7ca802SSteve Glendinning 
11684436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
1169e360a8b4SSteve Glendinning 	if (ret < 0)
1170b052e073SSteve Glendinning 		return ret;
11712f7ca802SSteve Glendinning 
11722f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, ID_REV, &read_buf);
1173e360a8b4SSteve Glendinning 	if (ret < 0)
1174b052e073SSteve Glendinning 		return ret;
1175a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net, "ID_REV = 0x%08x\n", read_buf);
11762f7ca802SSteve Glendinning 
1177f293501cSSteve Glendinning 	/* Configure GPIO pins as LED outputs */
1178f293501cSSteve Glendinning 	write_buf = LED_GPIO_CFG_SPD_LED | LED_GPIO_CFG_LNK_LED |
1179f293501cSSteve Glendinning 		LED_GPIO_CFG_FDX_LED;
1180f293501cSSteve Glendinning 	ret = smsc95xx_write_reg(dev, LED_GPIO_CFG, write_buf);
1181e360a8b4SSteve Glendinning 	if (ret < 0)
1182b052e073SSteve Glendinning 		return ret;
1183f293501cSSteve Glendinning 
11842f7ca802SSteve Glendinning 	/* Init Tx */
11854436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, FLOW, 0);
1186e360a8b4SSteve Glendinning 	if (ret < 0)
1187b052e073SSteve Glendinning 		return ret;
11882f7ca802SSteve Glendinning 
11894436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, AFC_CFG, AFC_CFG_DEFAULT);
1190e360a8b4SSteve Glendinning 	if (ret < 0)
1191b052e073SSteve Glendinning 		return ret;
11922f7ca802SSteve Glendinning 
11932f7ca802SSteve Glendinning 	/* Don't need mac_cr_lock during initialisation */
11942f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, MAC_CR, &pdata->mac_cr);
1195e360a8b4SSteve Glendinning 	if (ret < 0)
1196b052e073SSteve Glendinning 		return ret;
11972f7ca802SSteve Glendinning 
11982f7ca802SSteve Glendinning 	/* Init Rx */
11992f7ca802SSteve Glendinning 	/* Set Vlan */
12004436761bSSteve Glendinning 	ret = smsc95xx_write_reg(dev, VLAN1, (u32)ETH_P_8021Q);
1201e360a8b4SSteve Glendinning 	if (ret < 0)
1202b052e073SSteve Glendinning 		return ret;
12032f7ca802SSteve Glendinning 
1204f7b29271SSteve Glendinning 	/* Enable or disable checksum offload engines */
1205769ea6d8SSteve Glendinning 	ret = smsc95xx_set_features(dev->net, dev->net->features);
1206b052e073SSteve Glendinning 	if (ret < 0) {
1207b052e073SSteve Glendinning 		netdev_warn(dev->net, "Failed to set checksum offload features\n");
1208b052e073SSteve Glendinning 		return ret;
1209b052e073SSteve Glendinning 	}
12102f7ca802SSteve Glendinning 
12112f7ca802SSteve Glendinning 	smsc95xx_set_multicast(dev->net);
12122f7ca802SSteve Glendinning 
1213769ea6d8SSteve Glendinning 	ret = smsc95xx_phy_initialize(dev);
1214b052e073SSteve Glendinning 	if (ret < 0) {
1215b052e073SSteve Glendinning 		netdev_warn(dev->net, "Failed to init PHY\n");
1216b052e073SSteve Glendinning 		return ret;
1217b052e073SSteve Glendinning 	}
12182f7ca802SSteve Glendinning 
12192f7ca802SSteve Glendinning 	ret = smsc95xx_read_reg(dev, INT_EP_CTL, &read_buf);
1220e360a8b4SSteve Glendinning 	if (ret < 0)
1221b052e073SSteve Glendinning 		return ret;
12222f7ca802SSteve Glendinning 
12232f7ca802SSteve Glendinning 	/* enable PHY interrupts */
12242f7ca802SSteve Glendinning 	read_buf |= INT_EP_CTL_PHY_INT_;
12252f7ca802SSteve Glendinning 
12262f7ca802SSteve Glendinning 	ret = smsc95xx_write_reg(dev, INT_EP_CTL, read_buf);
1227e360a8b4SSteve Glendinning 	if (ret < 0)
1228b052e073SSteve Glendinning 		return ret;
12292f7ca802SSteve Glendinning 
1230769ea6d8SSteve Glendinning 	ret = smsc95xx_start_tx_path(dev);
1231b052e073SSteve Glendinning 	if (ret < 0) {
1232b052e073SSteve Glendinning 		netdev_warn(dev->net, "Failed to start TX path\n");
1233b052e073SSteve Glendinning 		return ret;
1234b052e073SSteve Glendinning 	}
1235769ea6d8SSteve Glendinning 
1236ec32115dSMing Lei 	ret = smsc95xx_start_rx_path(dev, 0);
1237b052e073SSteve Glendinning 	if (ret < 0) {
1238b052e073SSteve Glendinning 		netdev_warn(dev->net, "Failed to start RX path\n");
1239b052e073SSteve Glendinning 		return ret;
1240b052e073SSteve Glendinning 	}
12412f7ca802SSteve Glendinning 
1242a475f603SJoe Perches 	netif_dbg(dev, ifup, dev->net, "smsc95xx_reset, return 0\n");
12432f7ca802SSteve Glendinning 	return 0;
12442f7ca802SSteve Glendinning }
12452f7ca802SSteve Glendinning 
124663e77b39SStephen Hemminger static const struct net_device_ops smsc95xx_netdev_ops = {
124763e77b39SStephen Hemminger 	.ndo_open		= usbnet_open,
124863e77b39SStephen Hemminger 	.ndo_stop		= usbnet_stop,
124963e77b39SStephen Hemminger 	.ndo_start_xmit		= usbnet_start_xmit,
125063e77b39SStephen Hemminger 	.ndo_tx_timeout		= usbnet_tx_timeout,
125163e77b39SStephen Hemminger 	.ndo_change_mtu		= usbnet_change_mtu,
1252c8b5d129SGreg Ungerer 	.ndo_get_stats64	= usbnet_get_stats64,
125363e77b39SStephen Hemminger 	.ndo_set_mac_address 	= eth_mac_addr,
125463e77b39SStephen Hemminger 	.ndo_validate_addr	= eth_validate_addr,
125563e77b39SStephen Hemminger 	.ndo_do_ioctl 		= smsc95xx_ioctl,
1256afc4b13dSJiri Pirko 	.ndo_set_rx_mode	= smsc95xx_set_multicast,
125778e47fe4SMichał Mirosław 	.ndo_set_features	= smsc95xx_set_features,
125863e77b39SStephen Hemminger };
125963e77b39SStephen Hemminger 
12602f7ca802SSteve Glendinning static int smsc95xx_bind(struct usbnet *dev, struct usb_interface *intf)
12612f7ca802SSteve Glendinning {
12622f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = NULL;
1263bbd9f9eeSSteve Glendinning 	u32 val;
12642f7ca802SSteve Glendinning 	int ret;
12652f7ca802SSteve Glendinning 
12662f7ca802SSteve Glendinning 	printk(KERN_INFO SMSC_CHIPNAME " v" SMSC_DRIVER_VERSION "\n");
12672f7ca802SSteve Glendinning 
12682f7ca802SSteve Glendinning 	ret = usbnet_get_endpoints(dev, intf);
1269b052e073SSteve Glendinning 	if (ret < 0) {
1270b052e073SSteve Glendinning 		netdev_warn(dev->net, "usbnet_get_endpoints failed: %d\n", ret);
1271b052e073SSteve Glendinning 		return ret;
1272b052e073SSteve Glendinning 	}
12732f7ca802SSteve Glendinning 
12742f7ca802SSteve Glendinning 	dev->data[0] = (unsigned long)kzalloc(sizeof(struct smsc95xx_priv),
12752f7ca802SSteve Glendinning 					      GFP_KERNEL);
12762f7ca802SSteve Glendinning 
12772f7ca802SSteve Glendinning 	pdata = (struct smsc95xx_priv *)(dev->data[0]);
127838673c82SJoe Perches 	if (!pdata)
12792f7ca802SSteve Glendinning 		return -ENOMEM;
12802f7ca802SSteve Glendinning 
12812f7ca802SSteve Glendinning 	spin_lock_init(&pdata->mac_cr_lock);
12822f7ca802SSteve Glendinning 
1283fe0cd8caSNisar Sayed 	/* LAN95xx devices do not alter the computed checksum of 0 to 0xffff.
1284fe0cd8caSNisar Sayed 	 * RFC 2460, ipv6 UDP calculated checksum yields a result of zero must
1285fe0cd8caSNisar Sayed 	 * be changed to 0xffff. RFC 768, ipv4 UDP computed checksum is zero,
1286fe0cd8caSNisar Sayed 	 * it is transmitted as all ones. The zero transmitted checksum means
1287fe0cd8caSNisar Sayed 	 * transmitter generated no checksum. Hence, enable csum offload only
1288fe0cd8caSNisar Sayed 	 * for ipv4 packets.
1289fe0cd8caSNisar Sayed 	 */
129078e47fe4SMichał Mirosław 	if (DEFAULT_TX_CSUM_ENABLE)
1291fe0cd8caSNisar Sayed 		dev->net->features |= NETIF_F_IP_CSUM;
129278e47fe4SMichał Mirosław 	if (DEFAULT_RX_CSUM_ENABLE)
129378e47fe4SMichał Mirosław 		dev->net->features |= NETIF_F_RXCSUM;
129478e47fe4SMichał Mirosław 
1295fe0cd8caSNisar Sayed 	dev->net->hw_features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
1296810eeb1fSBen Dooks 	set_bit(EVENT_NO_IP_ALIGN, &dev->flags);
12972f7ca802SSteve Glendinning 
1298f4e8ab7cSBernard Blackham 	smsc95xx_init_mac_address(dev);
1299f4e8ab7cSBernard Blackham 
13002f7ca802SSteve Glendinning 	/* Init all registers */
13012f7ca802SSteve Glendinning 	ret = smsc95xx_reset(dev);
13022f7ca802SSteve Glendinning 
1303bbd9f9eeSSteve Glendinning 	/* detect device revision as different features may be available */
1304bbd9f9eeSSteve Glendinning 	ret = smsc95xx_read_reg(dev, ID_REV, &val);
1305e360a8b4SSteve Glendinning 	if (ret < 0)
1306b052e073SSteve Glendinning 		return ret;
1307bbd9f9eeSSteve Glendinning 	val >>= 16;
130813722bbeSWoojung Huh 	pdata->chip_id = val;
130913722bbeSWoojung Huh 	pdata->mdix_ctrl = get_mdix_status(dev->net);
13109ebca507SSteve Glendinning 
13119ebca507SSteve Glendinning 	if ((val == ID_REV_CHIP_ID_9500A_) || (val == ID_REV_CHIP_ID_9530_) ||
13129ebca507SSteve Glendinning 	    (val == ID_REV_CHIP_ID_89530_) || (val == ID_REV_CHIP_ID_9730_))
13139ebca507SSteve Glendinning 		pdata->features = (FEATURE_8_WAKEUP_FILTERS |
13149ebca507SSteve Glendinning 			FEATURE_PHY_NLP_CROSSOVER |
1315eb970ff0SMing Lei 			FEATURE_REMOTE_WAKEUP);
13169ebca507SSteve Glendinning 	else if (val == ID_REV_CHIP_ID_9512_)
13179ebca507SSteve Glendinning 		pdata->features = FEATURE_8_WAKEUP_FILTERS;
1318bbd9f9eeSSteve Glendinning 
131963e77b39SStephen Hemminger 	dev->net->netdev_ops = &smsc95xx_netdev_ops;
13202f7ca802SSteve Glendinning 	dev->net->ethtool_ops = &smsc95xx_ethtool_ops;
13212f7ca802SSteve Glendinning 	dev->net->flags |= IFF_MULTICAST;
132278e47fe4SMichał Mirosław 	dev->net->hard_header_len += SMSC95XX_TX_OVERHEAD_CSUM;
132385b18b02SStefan Wahren 	dev->net->min_mtu = ETH_MIN_MTU;
132485b18b02SStefan Wahren 	dev->net->max_mtu = ETH_DATA_LEN;
13259bbf5660SStephane Fillod 	dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
1326d69d1694SChristoph Fritz 
1327d69d1694SChristoph Fritz 	pdata->dev = dev;
1328d69d1694SChristoph Fritz 	INIT_DELAYED_WORK(&pdata->carrier_check, check_carrier);
1329d69d1694SChristoph Fritz 	schedule_delayed_work(&pdata->carrier_check, CARRIER_CHECK_DELAY);
1330d69d1694SChristoph Fritz 
13312f7ca802SSteve Glendinning 	return 0;
13322f7ca802SSteve Glendinning }
13332f7ca802SSteve Glendinning 
13342f7ca802SSteve Glendinning static void smsc95xx_unbind(struct usbnet *dev, struct usb_interface *intf)
13352f7ca802SSteve Glendinning {
13362f7ca802SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1337d69d1694SChristoph Fritz 
13382f7ca802SSteve Glendinning 	if (pdata) {
1339d69d1694SChristoph Fritz 		cancel_delayed_work(&pdata->carrier_check);
1340a475f603SJoe Perches 		netif_dbg(dev, ifdown, dev->net, "free pdata\n");
13412f7ca802SSteve Glendinning 		kfree(pdata);
13422f7ca802SSteve Glendinning 		pdata = NULL;
13432f7ca802SSteve Glendinning 		dev->data[0] = 0;
13442f7ca802SSteve Glendinning 	}
13452f7ca802SSteve Glendinning }
13462f7ca802SSteve Glendinning 
1347068bb1a7SSteve Glendinning static u32 smsc_crc(const u8 *buffer, size_t len, int filter)
1348bbd9f9eeSSteve Glendinning {
1349068bb1a7SSteve Glendinning 	u32 crc = bitrev16(crc16(0xFFFF, buffer, len));
1350068bb1a7SSteve Glendinning 	return crc << ((filter % 2) * 16);
1351bbd9f9eeSSteve Glendinning }
1352bbd9f9eeSSteve Glendinning 
1353e5e3af83SSteve Glendinning static int smsc95xx_enable_phy_wakeup_interrupts(struct usbnet *dev, u16 mask)
1354e5e3af83SSteve Glendinning {
1355e5e3af83SSteve Glendinning 	struct mii_if_info *mii = &dev->mii;
1356e5e3af83SSteve Glendinning 	int ret;
1357e5e3af83SSteve Glendinning 
13581e1d7412SJoe Perches 	netdev_dbg(dev->net, "enabling PHY wakeup interrupts\n");
1359e5e3af83SSteve Glendinning 
1360e5e3af83SSteve Glendinning 	/* read to clear */
1361e5e3af83SSteve Glendinning 	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_INT_SRC);
1362e360a8b4SSteve Glendinning 	if (ret < 0)
1363b052e073SSteve Glendinning 		return ret;
1364e5e3af83SSteve Glendinning 
1365e5e3af83SSteve Glendinning 	/* enable interrupt source */
1366e5e3af83SSteve Glendinning 	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_INT_MASK);
1367e360a8b4SSteve Glendinning 	if (ret < 0)
1368b052e073SSteve Glendinning 		return ret;
1369e5e3af83SSteve Glendinning 
1370e5e3af83SSteve Glendinning 	ret |= mask;
1371e5e3af83SSteve Glendinning 
1372e5e3af83SSteve Glendinning 	smsc95xx_mdio_write_nopm(dev->net, mii->phy_id, PHY_INT_MASK, ret);
1373e5e3af83SSteve Glendinning 
1374e5e3af83SSteve Glendinning 	return 0;
1375e5e3af83SSteve Glendinning }
1376e5e3af83SSteve Glendinning 
1377e5e3af83SSteve Glendinning static int smsc95xx_link_ok_nopm(struct usbnet *dev)
1378e5e3af83SSteve Glendinning {
1379e5e3af83SSteve Glendinning 	struct mii_if_info *mii = &dev->mii;
1380e5e3af83SSteve Glendinning 	int ret;
1381e5e3af83SSteve Glendinning 
1382e5e3af83SSteve Glendinning 	/* first, a dummy read, needed to latch some MII phys */
1383e5e3af83SSteve Glendinning 	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, MII_BMSR);
1384e360a8b4SSteve Glendinning 	if (ret < 0)
1385b052e073SSteve Glendinning 		return ret;
1386e5e3af83SSteve Glendinning 
1387e5e3af83SSteve Glendinning 	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, MII_BMSR);
1388e360a8b4SSteve Glendinning 	if (ret < 0)
1389b052e073SSteve Glendinning 		return ret;
1390e5e3af83SSteve Glendinning 
1391e5e3af83SSteve Glendinning 	return !!(ret & BMSR_LSTATUS);
1392e5e3af83SSteve Glendinning }
1393e5e3af83SSteve Glendinning 
1394319b95b5SSteve Glendinning static int smsc95xx_enter_suspend0(struct usbnet *dev)
1395319b95b5SSteve Glendinning {
1396319b95b5SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1397319b95b5SSteve Glendinning 	u32 val;
1398319b95b5SSteve Glendinning 	int ret;
1399319b95b5SSteve Glendinning 
1400319b95b5SSteve Glendinning 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1401e360a8b4SSteve Glendinning 	if (ret < 0)
1402b052e073SSteve Glendinning 		return ret;
1403319b95b5SSteve Glendinning 
1404319b95b5SSteve Glendinning 	val &= (~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_));
1405319b95b5SSteve Glendinning 	val |= PM_CTL_SUS_MODE_0;
1406319b95b5SSteve Glendinning 
1407319b95b5SSteve Glendinning 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1408e360a8b4SSteve Glendinning 	if (ret < 0)
1409b052e073SSteve Glendinning 		return ret;
1410319b95b5SSteve Glendinning 
1411319b95b5SSteve Glendinning 	/* clear wol status */
1412319b95b5SSteve Glendinning 	val &= ~PM_CTL_WUPS_;
1413319b95b5SSteve Glendinning 	val |= PM_CTL_WUPS_WOL_;
1414319b95b5SSteve Glendinning 
1415319b95b5SSteve Glendinning 	/* enable energy detection */
1416319b95b5SSteve Glendinning 	if (pdata->wolopts & WAKE_PHY)
1417319b95b5SSteve Glendinning 		val |= PM_CTL_WUPS_ED_;
1418319b95b5SSteve Glendinning 
1419319b95b5SSteve Glendinning 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1420e360a8b4SSteve Glendinning 	if (ret < 0)
1421b052e073SSteve Glendinning 		return ret;
1422319b95b5SSteve Glendinning 
1423319b95b5SSteve Glendinning 	/* read back PM_CTRL */
1424319b95b5SSteve Glendinning 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
142576437214SMing Lei 	if (ret < 0)
142676437214SMing Lei 		return ret;
1427319b95b5SSteve Glendinning 
1428b2d4b150SSteve Glendinning 	pdata->suspend_flags |= SUSPEND_SUSPEND0;
1429b2d4b150SSteve Glendinning 
143076437214SMing Lei 	return 0;
1431319b95b5SSteve Glendinning }
1432319b95b5SSteve Glendinning 
1433319b95b5SSteve Glendinning static int smsc95xx_enter_suspend1(struct usbnet *dev)
1434319b95b5SSteve Glendinning {
1435319b95b5SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1436319b95b5SSteve Glendinning 	struct mii_if_info *mii = &dev->mii;
1437319b95b5SSteve Glendinning 	u32 val;
1438319b95b5SSteve Glendinning 	int ret;
1439319b95b5SSteve Glendinning 
1440319b95b5SSteve Glendinning 	/* reconfigure link pulse detection timing for
1441319b95b5SSteve Glendinning 	 * compatibility with non-standard link partners
1442319b95b5SSteve Glendinning 	 */
1443319b95b5SSteve Glendinning 	if (pdata->features & FEATURE_PHY_NLP_CROSSOVER)
1444319b95b5SSteve Glendinning 		smsc95xx_mdio_write_nopm(dev->net, mii->phy_id,	PHY_EDPD_CONFIG,
1445319b95b5SSteve Glendinning 			PHY_EDPD_CONFIG_DEFAULT);
1446319b95b5SSteve Glendinning 
1447319b95b5SSteve Glendinning 	/* enable energy detect power-down mode */
1448319b95b5SSteve Glendinning 	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_MODE_CTRL_STS);
1449e360a8b4SSteve Glendinning 	if (ret < 0)
1450b052e073SSteve Glendinning 		return ret;
1451319b95b5SSteve Glendinning 
1452319b95b5SSteve Glendinning 	ret |= MODE_CTRL_STS_EDPWRDOWN_;
1453319b95b5SSteve Glendinning 
1454319b95b5SSteve Glendinning 	smsc95xx_mdio_write_nopm(dev->net, mii->phy_id, PHY_MODE_CTRL_STS, ret);
1455319b95b5SSteve Glendinning 
1456319b95b5SSteve Glendinning 	/* enter SUSPEND1 mode */
1457319b95b5SSteve Glendinning 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1458e360a8b4SSteve Glendinning 	if (ret < 0)
1459b052e073SSteve Glendinning 		return ret;
1460319b95b5SSteve Glendinning 
1461319b95b5SSteve Glendinning 	val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1462319b95b5SSteve Glendinning 	val |= PM_CTL_SUS_MODE_1;
1463319b95b5SSteve Glendinning 
1464319b95b5SSteve Glendinning 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1465e360a8b4SSteve Glendinning 	if (ret < 0)
1466b052e073SSteve Glendinning 		return ret;
1467319b95b5SSteve Glendinning 
1468319b95b5SSteve Glendinning 	/* clear wol status, enable energy detection */
1469319b95b5SSteve Glendinning 	val &= ~PM_CTL_WUPS_;
1470319b95b5SSteve Glendinning 	val |= (PM_CTL_WUPS_ED_ | PM_CTL_ED_EN_);
1471319b95b5SSteve Glendinning 
1472319b95b5SSteve Glendinning 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
147376437214SMing Lei 	if (ret < 0)
147476437214SMing Lei 		return ret;
1475319b95b5SSteve Glendinning 
1476b2d4b150SSteve Glendinning 	pdata->suspend_flags |= SUSPEND_SUSPEND1;
1477b2d4b150SSteve Glendinning 
147876437214SMing Lei 	return 0;
1479319b95b5SSteve Glendinning }
1480319b95b5SSteve Glendinning 
1481319b95b5SSteve Glendinning static int smsc95xx_enter_suspend2(struct usbnet *dev)
1482319b95b5SSteve Glendinning {
1483b2d4b150SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1484319b95b5SSteve Glendinning 	u32 val;
1485319b95b5SSteve Glendinning 	int ret;
1486319b95b5SSteve Glendinning 
1487319b95b5SSteve Glendinning 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1488e360a8b4SSteve Glendinning 	if (ret < 0)
1489b052e073SSteve Glendinning 		return ret;
1490319b95b5SSteve Glendinning 
1491319b95b5SSteve Glendinning 	val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1492319b95b5SSteve Glendinning 	val |= PM_CTL_SUS_MODE_2;
1493319b95b5SSteve Glendinning 
1494319b95b5SSteve Glendinning 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
149576437214SMing Lei 	if (ret < 0)
149676437214SMing Lei 		return ret;
1497319b95b5SSteve Glendinning 
1498b2d4b150SSteve Glendinning 	pdata->suspend_flags |= SUSPEND_SUSPEND2;
1499b2d4b150SSteve Glendinning 
150076437214SMing Lei 	return 0;
1501319b95b5SSteve Glendinning }
1502319b95b5SSteve Glendinning 
1503b2d4b150SSteve Glendinning static int smsc95xx_enter_suspend3(struct usbnet *dev)
1504b2d4b150SSteve Glendinning {
1505b2d4b150SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1506b2d4b150SSteve Glendinning 	u32 val;
1507b2d4b150SSteve Glendinning 	int ret;
1508b2d4b150SSteve Glendinning 
1509b2d4b150SSteve Glendinning 	ret = smsc95xx_read_reg_nopm(dev, RX_FIFO_INF, &val);
1510b2d4b150SSteve Glendinning 	if (ret < 0)
1511b2d4b150SSteve Glendinning 		return ret;
1512b2d4b150SSteve Glendinning 
151353a759c8SMartin Wetterwald 	if (val & RX_FIFO_INF_USED_) {
1514b2d4b150SSteve Glendinning 		netdev_info(dev->net, "rx fifo not empty in autosuspend\n");
1515b2d4b150SSteve Glendinning 		return -EBUSY;
1516b2d4b150SSteve Glendinning 	}
1517b2d4b150SSteve Glendinning 
1518b2d4b150SSteve Glendinning 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1519b2d4b150SSteve Glendinning 	if (ret < 0)
1520b2d4b150SSteve Glendinning 		return ret;
1521b2d4b150SSteve Glendinning 
1522b2d4b150SSteve Glendinning 	val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1523b2d4b150SSteve Glendinning 	val |= PM_CTL_SUS_MODE_3 | PM_CTL_RES_CLR_WKP_STS;
1524b2d4b150SSteve Glendinning 
1525b2d4b150SSteve Glendinning 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1526b2d4b150SSteve Glendinning 	if (ret < 0)
1527b2d4b150SSteve Glendinning 		return ret;
1528b2d4b150SSteve Glendinning 
1529b2d4b150SSteve Glendinning 	/* clear wol status */
1530b2d4b150SSteve Glendinning 	val &= ~PM_CTL_WUPS_;
1531b2d4b150SSteve Glendinning 	val |= PM_CTL_WUPS_WOL_;
1532b2d4b150SSteve Glendinning 
1533b2d4b150SSteve Glendinning 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1534b2d4b150SSteve Glendinning 	if (ret < 0)
1535b2d4b150SSteve Glendinning 		return ret;
1536b2d4b150SSteve Glendinning 
1537b2d4b150SSteve Glendinning 	pdata->suspend_flags |= SUSPEND_SUSPEND3;
1538b2d4b150SSteve Glendinning 
1539b2d4b150SSteve Glendinning 	return 0;
1540b2d4b150SSteve Glendinning }
1541b2d4b150SSteve Glendinning 
1542b2d4b150SSteve Glendinning static int smsc95xx_autosuspend(struct usbnet *dev, u32 link_up)
1543b2d4b150SSteve Glendinning {
1544b2d4b150SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1545b2d4b150SSteve Glendinning 	int ret;
1546b2d4b150SSteve Glendinning 
1547b2d4b150SSteve Glendinning 	if (!netif_running(dev->net)) {
1548b2d4b150SSteve Glendinning 		/* interface is ifconfig down so fully power down hw */
1549b2d4b150SSteve Glendinning 		netdev_dbg(dev->net, "autosuspend entering SUSPEND2\n");
1550b2d4b150SSteve Glendinning 		return smsc95xx_enter_suspend2(dev);
1551b2d4b150SSteve Glendinning 	}
1552b2d4b150SSteve Glendinning 
1553b2d4b150SSteve Glendinning 	if (!link_up) {
1554b2d4b150SSteve Glendinning 		/* link is down so enter EDPD mode, but only if device can
1555b2d4b150SSteve Glendinning 		 * reliably resume from it.  This check should be redundant
1556eb970ff0SMing Lei 		 * as current FEATURE_REMOTE_WAKEUP parts also support
1557b2d4b150SSteve Glendinning 		 * FEATURE_PHY_NLP_CROSSOVER but it's included for clarity */
1558b2d4b150SSteve Glendinning 		if (!(pdata->features & FEATURE_PHY_NLP_CROSSOVER)) {
1559b2d4b150SSteve Glendinning 			netdev_warn(dev->net, "EDPD not supported\n");
1560b2d4b150SSteve Glendinning 			return -EBUSY;
1561b2d4b150SSteve Glendinning 		}
1562b2d4b150SSteve Glendinning 
1563b2d4b150SSteve Glendinning 		netdev_dbg(dev->net, "autosuspend entering SUSPEND1\n");
1564b2d4b150SSteve Glendinning 
1565b2d4b150SSteve Glendinning 		/* enable PHY wakeup events for if cable is attached */
1566b2d4b150SSteve Glendinning 		ret = smsc95xx_enable_phy_wakeup_interrupts(dev,
1567b2d4b150SSteve Glendinning 			PHY_INT_MASK_ANEG_COMP_);
1568b2d4b150SSteve Glendinning 		if (ret < 0) {
1569b2d4b150SSteve Glendinning 			netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1570b2d4b150SSteve Glendinning 			return ret;
1571b2d4b150SSteve Glendinning 		}
1572b2d4b150SSteve Glendinning 
1573b2d4b150SSteve Glendinning 		netdev_info(dev->net, "entering SUSPEND1 mode\n");
1574b2d4b150SSteve Glendinning 		return smsc95xx_enter_suspend1(dev);
1575b2d4b150SSteve Glendinning 	}
1576b2d4b150SSteve Glendinning 
1577b2d4b150SSteve Glendinning 	/* enable PHY wakeup events so we remote wakeup if cable is pulled */
1578b2d4b150SSteve Glendinning 	ret = smsc95xx_enable_phy_wakeup_interrupts(dev,
1579b2d4b150SSteve Glendinning 		PHY_INT_MASK_LINK_DOWN_);
1580b2d4b150SSteve Glendinning 	if (ret < 0) {
1581b2d4b150SSteve Glendinning 		netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1582b2d4b150SSteve Glendinning 		return ret;
1583b2d4b150SSteve Glendinning 	}
1584b2d4b150SSteve Glendinning 
1585b2d4b150SSteve Glendinning 	netdev_dbg(dev->net, "autosuspend entering SUSPEND3\n");
1586b2d4b150SSteve Glendinning 	return smsc95xx_enter_suspend3(dev);
1587b2d4b150SSteve Glendinning }
1588b2d4b150SSteve Glendinning 
1589b5a04475SSteve Glendinning static int smsc95xx_suspend(struct usb_interface *intf, pm_message_t message)
1590b5a04475SSteve Glendinning {
1591b5a04475SSteve Glendinning 	struct usbnet *dev = usb_get_intfdata(intf);
1592e0e474a8SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1593e5e3af83SSteve Glendinning 	u32 val, link_up;
1594b5a04475SSteve Glendinning 	int ret;
1595b5a04475SSteve Glendinning 
1596b5a04475SSteve Glendinning 	ret = usbnet_suspend(intf, message);
1597b052e073SSteve Glendinning 	if (ret < 0) {
1598b052e073SSteve Glendinning 		netdev_warn(dev->net, "usbnet_suspend error\n");
1599b052e073SSteve Glendinning 		return ret;
1600b052e073SSteve Glendinning 	}
1601b5a04475SSteve Glendinning 
16027b900eadSFrieder Schrempf 	cancel_delayed_work_sync(&pdata->carrier_check);
16037b900eadSFrieder Schrempf 
1604b2d4b150SSteve Glendinning 	if (pdata->suspend_flags) {
1605b2d4b150SSteve Glendinning 		netdev_warn(dev->net, "error during last resume\n");
1606b2d4b150SSteve Glendinning 		pdata->suspend_flags = 0;
1607b2d4b150SSteve Glendinning 	}
1608b2d4b150SSteve Glendinning 
1609e5e3af83SSteve Glendinning 	/* determine if link is up using only _nopm functions */
1610e5e3af83SSteve Glendinning 	link_up = smsc95xx_link_ok_nopm(dev);
1611e5e3af83SSteve Glendinning 
161242e21c01SMing Lei 	if (message.event == PM_EVENT_AUTO_SUSPEND &&
1613eb970ff0SMing Lei 	    (pdata->features & FEATURE_REMOTE_WAKEUP)) {
1614b2d4b150SSteve Glendinning 		ret = smsc95xx_autosuspend(dev, link_up);
1615b2d4b150SSteve Glendinning 		goto done;
1616b2d4b150SSteve Glendinning 	}
1617b2d4b150SSteve Glendinning 
1618b2d4b150SSteve Glendinning 	/* if we get this far we're not autosuspending */
1619e5e3af83SSteve Glendinning 	/* if no wol options set, or if link is down and we're not waking on
1620e5e3af83SSteve Glendinning 	 * PHY activity, enter lowest power SUSPEND2 mode
1621e5e3af83SSteve Glendinning 	 */
1622e5e3af83SSteve Glendinning 	if (!(pdata->wolopts & SUPPORTED_WAKE) ||
1623e5e3af83SSteve Glendinning 		!(link_up || (pdata->wolopts & WAKE_PHY))) {
16241e1d7412SJoe Perches 		netdev_info(dev->net, "entering SUSPEND2 mode\n");
1625b5a04475SSteve Glendinning 
1626e0e474a8SSteve Glendinning 		/* disable energy detect (link up) & wake up events */
1627ec32115dSMing Lei 		ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1628e360a8b4SSteve Glendinning 		if (ret < 0)
1629b052e073SSteve Glendinning 			goto done;
1630e0e474a8SSteve Glendinning 
1631e0e474a8SSteve Glendinning 		val &= ~(WUCSR_MPEN_ | WUCSR_WAKE_EN_);
1632e0e474a8SSteve Glendinning 
1633ec32115dSMing Lei 		ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1634e360a8b4SSteve Glendinning 		if (ret < 0)
1635b052e073SSteve Glendinning 			goto done;
1636e0e474a8SSteve Glendinning 
1637ec32115dSMing Lei 		ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1638e360a8b4SSteve Glendinning 		if (ret < 0)
1639b052e073SSteve Glendinning 			goto done;
1640e0e474a8SSteve Glendinning 
1641e0e474a8SSteve Glendinning 		val &= ~(PM_CTL_ED_EN_ | PM_CTL_WOL_EN_);
1642e0e474a8SSteve Glendinning 
1643ec32115dSMing Lei 		ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1644e360a8b4SSteve Glendinning 		if (ret < 0)
1645b052e073SSteve Glendinning 			goto done;
1646e0e474a8SSteve Glendinning 
16473b9f7d8cSSteve Glendinning 		ret = smsc95xx_enter_suspend2(dev);
16483b9f7d8cSSteve Glendinning 		goto done;
1649b5a04475SSteve Glendinning 	}
1650b5a04475SSteve Glendinning 
1651e5e3af83SSteve Glendinning 	if (pdata->wolopts & WAKE_PHY) {
1652e5e3af83SSteve Glendinning 		ret = smsc95xx_enable_phy_wakeup_interrupts(dev,
1653e5e3af83SSteve Glendinning 			(PHY_INT_MASK_ANEG_COMP_ | PHY_INT_MASK_LINK_DOWN_));
1654b052e073SSteve Glendinning 		if (ret < 0) {
1655b052e073SSteve Glendinning 			netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1656b052e073SSteve Glendinning 			goto done;
1657b052e073SSteve Glendinning 		}
1658e5e3af83SSteve Glendinning 
1659e5e3af83SSteve Glendinning 		/* if link is down then configure EDPD and enter SUSPEND1,
1660e5e3af83SSteve Glendinning 		 * otherwise enter SUSPEND0 below
1661e5e3af83SSteve Glendinning 		 */
1662e5e3af83SSteve Glendinning 		if (!link_up) {
16631e1d7412SJoe Perches 			netdev_info(dev->net, "entering SUSPEND1 mode\n");
16643b9f7d8cSSteve Glendinning 			ret = smsc95xx_enter_suspend1(dev);
16653b9f7d8cSSteve Glendinning 			goto done;
1666e5e3af83SSteve Glendinning 		}
1667e5e3af83SSteve Glendinning 	}
1668e5e3af83SSteve Glendinning 
1669bbd9f9eeSSteve Glendinning 	if (pdata->wolopts & (WAKE_BCAST | WAKE_MCAST | WAKE_ARP | WAKE_UCAST)) {
16706396bb22SKees Cook 		u32 *filter_mask = kcalloc(32, sizeof(u32), GFP_KERNEL);
167106a221beSMing Lei 		u32 command[2];
167206a221beSMing Lei 		u32 offset[2];
167306a221beSMing Lei 		u32 crc[4];
16749ebca507SSteve Glendinning 		int wuff_filter_count =
16759ebca507SSteve Glendinning 			(pdata->features & FEATURE_8_WAKEUP_FILTERS) ?
16769ebca507SSteve Glendinning 			LAN9500A_WUFF_NUM : LAN9500_WUFF_NUM;
1677bbd9f9eeSSteve Glendinning 		int i, filter = 0;
1678bbd9f9eeSSteve Glendinning 
1679eed9a729SSteve Glendinning 		if (!filter_mask) {
1680eed9a729SSteve Glendinning 			netdev_warn(dev->net, "Unable to allocate filter_mask\n");
16813b9f7d8cSSteve Glendinning 			ret = -ENOMEM;
16823b9f7d8cSSteve Glendinning 			goto done;
1683eed9a729SSteve Glendinning 		}
1684eed9a729SSteve Glendinning 
168506a221beSMing Lei 		memset(command, 0, sizeof(command));
168606a221beSMing Lei 		memset(offset, 0, sizeof(offset));
168706a221beSMing Lei 		memset(crc, 0, sizeof(crc));
168806a221beSMing Lei 
1689bbd9f9eeSSteve Glendinning 		if (pdata->wolopts & WAKE_BCAST) {
1690bbd9f9eeSSteve Glendinning 			const u8 bcast[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
16911e1d7412SJoe Perches 			netdev_info(dev->net, "enabling broadcast detection\n");
1692bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4] = 0x003F;
1693bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 1] = 0x00;
1694bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 2] = 0x00;
1695bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 3] = 0x00;
1696bbd9f9eeSSteve Glendinning 			command[filter/4] |= 0x05UL << ((filter % 4) * 8);
1697bbd9f9eeSSteve Glendinning 			offset[filter/4] |= 0x00 << ((filter % 4) * 8);
1698bbd9f9eeSSteve Glendinning 			crc[filter/2] |= smsc_crc(bcast, 6, filter);
1699bbd9f9eeSSteve Glendinning 			filter++;
1700bbd9f9eeSSteve Glendinning 		}
1701bbd9f9eeSSteve Glendinning 
1702bbd9f9eeSSteve Glendinning 		if (pdata->wolopts & WAKE_MCAST) {
1703bbd9f9eeSSteve Glendinning 			const u8 mcast[] = {0x01, 0x00, 0x5E};
17041e1d7412SJoe Perches 			netdev_info(dev->net, "enabling multicast detection\n");
1705bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4] = 0x0007;
1706bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 1] = 0x00;
1707bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 2] = 0x00;
1708bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 3] = 0x00;
1709bbd9f9eeSSteve Glendinning 			command[filter/4] |= 0x09UL << ((filter % 4) * 8);
1710bbd9f9eeSSteve Glendinning 			offset[filter/4] |= 0x00  << ((filter % 4) * 8);
1711bbd9f9eeSSteve Glendinning 			crc[filter/2] |= smsc_crc(mcast, 3, filter);
1712bbd9f9eeSSteve Glendinning 			filter++;
1713bbd9f9eeSSteve Glendinning 		}
1714bbd9f9eeSSteve Glendinning 
1715bbd9f9eeSSteve Glendinning 		if (pdata->wolopts & WAKE_ARP) {
1716bbd9f9eeSSteve Glendinning 			const u8 arp[] = {0x08, 0x06};
17171e1d7412SJoe Perches 			netdev_info(dev->net, "enabling ARP detection\n");
1718bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4] = 0x0003;
1719bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 1] = 0x00;
1720bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 2] = 0x00;
1721bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 3] = 0x00;
1722bbd9f9eeSSteve Glendinning 			command[filter/4] |= 0x05UL << ((filter % 4) * 8);
1723bbd9f9eeSSteve Glendinning 			offset[filter/4] |= 0x0C << ((filter % 4) * 8);
1724bbd9f9eeSSteve Glendinning 			crc[filter/2] |= smsc_crc(arp, 2, filter);
1725bbd9f9eeSSteve Glendinning 			filter++;
1726bbd9f9eeSSteve Glendinning 		}
1727bbd9f9eeSSteve Glendinning 
1728bbd9f9eeSSteve Glendinning 		if (pdata->wolopts & WAKE_UCAST) {
17291e1d7412SJoe Perches 			netdev_info(dev->net, "enabling unicast detection\n");
1730bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4] = 0x003F;
1731bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 1] = 0x00;
1732bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 2] = 0x00;
1733bbd9f9eeSSteve Glendinning 			filter_mask[filter * 4 + 3] = 0x00;
1734bbd9f9eeSSteve Glendinning 			command[filter/4] |= 0x01UL << ((filter % 4) * 8);
1735bbd9f9eeSSteve Glendinning 			offset[filter/4] |= 0x00 << ((filter % 4) * 8);
1736bbd9f9eeSSteve Glendinning 			crc[filter/2] |= smsc_crc(dev->net->dev_addr, ETH_ALEN, filter);
1737bbd9f9eeSSteve Glendinning 			filter++;
1738bbd9f9eeSSteve Glendinning 		}
1739bbd9f9eeSSteve Glendinning 
17409ebca507SSteve Glendinning 		for (i = 0; i < (wuff_filter_count * 4); i++) {
1741ec32115dSMing Lei 			ret = smsc95xx_write_reg_nopm(dev, WUFF, filter_mask[i]);
1742b052e073SSteve Glendinning 			if (ret < 0) {
174306a221beSMing Lei 				kfree(filter_mask);
1744b052e073SSteve Glendinning 				goto done;
1745b052e073SSteve Glendinning 			}
1746bbd9f9eeSSteve Glendinning 		}
174706a221beSMing Lei 		kfree(filter_mask);
1748bbd9f9eeSSteve Glendinning 
17499ebca507SSteve Glendinning 		for (i = 0; i < (wuff_filter_count / 4); i++) {
1750ec32115dSMing Lei 			ret = smsc95xx_write_reg_nopm(dev, WUFF, command[i]);
1751e360a8b4SSteve Glendinning 			if (ret < 0)
1752b052e073SSteve Glendinning 				goto done;
1753b052e073SSteve Glendinning 		}
1754bbd9f9eeSSteve Glendinning 
17559ebca507SSteve Glendinning 		for (i = 0; i < (wuff_filter_count / 4); i++) {
1756ec32115dSMing Lei 			ret = smsc95xx_write_reg_nopm(dev, WUFF, offset[i]);
1757e360a8b4SSteve Glendinning 			if (ret < 0)
1758b052e073SSteve Glendinning 				goto done;
1759b052e073SSteve Glendinning 		}
1760bbd9f9eeSSteve Glendinning 
17619ebca507SSteve Glendinning 		for (i = 0; i < (wuff_filter_count / 2); i++) {
1762ec32115dSMing Lei 			ret = smsc95xx_write_reg_nopm(dev, WUFF, crc[i]);
1763e360a8b4SSteve Glendinning 			if (ret < 0)
1764b052e073SSteve Glendinning 				goto done;
1765b052e073SSteve Glendinning 		}
1766bbd9f9eeSSteve Glendinning 
1767bbd9f9eeSSteve Glendinning 		/* clear any pending pattern match packet status */
1768ec32115dSMing Lei 		ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1769e360a8b4SSteve Glendinning 		if (ret < 0)
1770b052e073SSteve Glendinning 			goto done;
1771bbd9f9eeSSteve Glendinning 
1772bbd9f9eeSSteve Glendinning 		val |= WUCSR_WUFR_;
1773bbd9f9eeSSteve Glendinning 
1774ec32115dSMing Lei 		ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1775e360a8b4SSteve Glendinning 		if (ret < 0)
1776b052e073SSteve Glendinning 			goto done;
1777b052e073SSteve Glendinning 	}
1778bbd9f9eeSSteve Glendinning 
1779e0e474a8SSteve Glendinning 	if (pdata->wolopts & WAKE_MAGIC) {
1780e0e474a8SSteve Glendinning 		/* clear any pending magic packet status */
1781ec32115dSMing Lei 		ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1782e360a8b4SSteve Glendinning 		if (ret < 0)
1783b052e073SSteve Glendinning 			goto done;
1784e0e474a8SSteve Glendinning 
1785e0e474a8SSteve Glendinning 		val |= WUCSR_MPR_;
1786e0e474a8SSteve Glendinning 
1787ec32115dSMing Lei 		ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1788e360a8b4SSteve Glendinning 		if (ret < 0)
1789b052e073SSteve Glendinning 			goto done;
1790b052e073SSteve Glendinning 	}
1791e0e474a8SSteve Glendinning 
1792bbd9f9eeSSteve Glendinning 	/* enable/disable wakeup sources */
1793ec32115dSMing Lei 	ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1794e360a8b4SSteve Glendinning 	if (ret < 0)
1795b052e073SSteve Glendinning 		goto done;
1796e0e474a8SSteve Glendinning 
1797bbd9f9eeSSteve Glendinning 	if (pdata->wolopts & (WAKE_BCAST | WAKE_MCAST | WAKE_ARP | WAKE_UCAST)) {
17981e1d7412SJoe Perches 		netdev_info(dev->net, "enabling pattern match wakeup\n");
1799bbd9f9eeSSteve Glendinning 		val |= WUCSR_WAKE_EN_;
1800bbd9f9eeSSteve Glendinning 	} else {
18011e1d7412SJoe Perches 		netdev_info(dev->net, "disabling pattern match wakeup\n");
1802bbd9f9eeSSteve Glendinning 		val &= ~WUCSR_WAKE_EN_;
1803bbd9f9eeSSteve Glendinning 	}
1804bbd9f9eeSSteve Glendinning 
1805e0e474a8SSteve Glendinning 	if (pdata->wolopts & WAKE_MAGIC) {
18061e1d7412SJoe Perches 		netdev_info(dev->net, "enabling magic packet wakeup\n");
1807e0e474a8SSteve Glendinning 		val |= WUCSR_MPEN_;
1808e0e474a8SSteve Glendinning 	} else {
18091e1d7412SJoe Perches 		netdev_info(dev->net, "disabling magic packet wakeup\n");
1810e0e474a8SSteve Glendinning 		val &= ~WUCSR_MPEN_;
1811e0e474a8SSteve Glendinning 	}
1812e0e474a8SSteve Glendinning 
1813ec32115dSMing Lei 	ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1814e360a8b4SSteve Glendinning 	if (ret < 0)
1815b052e073SSteve Glendinning 		goto done;
1816e0e474a8SSteve Glendinning 
1817e0e474a8SSteve Glendinning 	/* enable wol wakeup source */
1818ec32115dSMing Lei 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1819e360a8b4SSteve Glendinning 	if (ret < 0)
1820b052e073SSteve Glendinning 		goto done;
1821e0e474a8SSteve Glendinning 
1822e0e474a8SSteve Glendinning 	val |= PM_CTL_WOL_EN_;
1823e0e474a8SSteve Glendinning 
1824e5e3af83SSteve Glendinning 	/* phy energy detect wakeup source */
1825e5e3af83SSteve Glendinning 	if (pdata->wolopts & WAKE_PHY)
1826e5e3af83SSteve Glendinning 		val |= PM_CTL_ED_EN_;
1827e5e3af83SSteve Glendinning 
1828ec32115dSMing Lei 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1829e360a8b4SSteve Glendinning 	if (ret < 0)
1830b052e073SSteve Glendinning 		goto done;
1831e0e474a8SSteve Glendinning 
1832bbd9f9eeSSteve Glendinning 	/* enable receiver to enable frame reception */
1833ec32115dSMing Lei 	smsc95xx_start_rx_path(dev, 1);
1834e0e474a8SSteve Glendinning 
1835e0e474a8SSteve Glendinning 	/* some wol options are enabled, so enter SUSPEND0 */
18361e1d7412SJoe Perches 	netdev_info(dev->net, "entering SUSPEND0 mode\n");
18373b9f7d8cSSteve Glendinning 	ret = smsc95xx_enter_suspend0(dev);
18383b9f7d8cSSteve Glendinning 
18393b9f7d8cSSteve Glendinning done:
18400d41be53SMing Lei 	/*
18410d41be53SMing Lei 	 * TODO: resume() might need to handle the suspend failure
18420d41be53SMing Lei 	 * in system sleep
18430d41be53SMing Lei 	 */
18440d41be53SMing Lei 	if (ret && PMSG_IS_AUTO(message))
18453b9f7d8cSSteve Glendinning 		usbnet_resume(intf);
18467b900eadSFrieder Schrempf 
18477b900eadSFrieder Schrempf 	if (ret)
18487b900eadSFrieder Schrempf 		schedule_delayed_work(&pdata->carrier_check,
18497b900eadSFrieder Schrempf 				      CARRIER_CHECK_DELAY);
18507b900eadSFrieder Schrempf 
18513b9f7d8cSSteve Glendinning 	return ret;
1852e0e474a8SSteve Glendinning }
1853e0e474a8SSteve Glendinning 
1854e0e474a8SSteve Glendinning static int smsc95xx_resume(struct usb_interface *intf)
1855e0e474a8SSteve Glendinning {
1856e0e474a8SSteve Glendinning 	struct usbnet *dev = usb_get_intfdata(intf);
18578bca81d9SSudip Mukherjee 	struct smsc95xx_priv *pdata;
18588bca81d9SSudip Mukherjee 	u8 suspend_flags;
1859e0e474a8SSteve Glendinning 	int ret;
1860e0e474a8SSteve Glendinning 	u32 val;
1861e0e474a8SSteve Glendinning 
1862e0e474a8SSteve Glendinning 	BUG_ON(!dev);
18638bca81d9SSudip Mukherjee 	pdata = (struct smsc95xx_priv *)(dev->data[0]);
18648bca81d9SSudip Mukherjee 	suspend_flags = pdata->suspend_flags;
1865e0e474a8SSteve Glendinning 
1866b2d4b150SSteve Glendinning 	netdev_dbg(dev->net, "resume suspend_flags=0x%02x\n", suspend_flags);
1867b2d4b150SSteve Glendinning 
1868b2d4b150SSteve Glendinning 	/* do this first to ensure it's cleared even in error case */
1869b2d4b150SSteve Glendinning 	pdata->suspend_flags = 0;
1870d69d1694SChristoph Fritz 	schedule_delayed_work(&pdata->carrier_check, CARRIER_CHECK_DELAY);
1871b2d4b150SSteve Glendinning 
1872b2d4b150SSteve Glendinning 	if (suspend_flags & SUSPEND_ALLMODES) {
1873bbd9f9eeSSteve Glendinning 		/* clear wake-up sources */
1874ec32115dSMing Lei 		ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1875e360a8b4SSteve Glendinning 		if (ret < 0)
1876b052e073SSteve Glendinning 			return ret;
1877e0e474a8SSteve Glendinning 
1878bbd9f9eeSSteve Glendinning 		val &= ~(WUCSR_WAKE_EN_ | WUCSR_MPEN_);
1879e0e474a8SSteve Glendinning 
1880ec32115dSMing Lei 		ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1881e360a8b4SSteve Glendinning 		if (ret < 0)
1882b052e073SSteve Glendinning 			return ret;
1883e0e474a8SSteve Glendinning 
1884e0e474a8SSteve Glendinning 		/* clear wake-up status */
1885ec32115dSMing Lei 		ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1886e360a8b4SSteve Glendinning 		if (ret < 0)
1887b052e073SSteve Glendinning 			return ret;
1888e0e474a8SSteve Glendinning 
1889e0e474a8SSteve Glendinning 		val &= ~PM_CTL_WOL_EN_;
1890e0e474a8SSteve Glendinning 		val |= PM_CTL_WUPS_;
1891e0e474a8SSteve Glendinning 
1892ec32115dSMing Lei 		ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1893e360a8b4SSteve Glendinning 		if (ret < 0)
1894b052e073SSteve Glendinning 			return ret;
1895b052e073SSteve Glendinning 	}
1896e0e474a8SSteve Glendinning 
1897af3d7c1eSSteve Glendinning 	ret = usbnet_resume(intf);
1898b052e073SSteve Glendinning 	if (ret < 0)
1899b052e073SSteve Glendinning 		netdev_warn(dev->net, "usbnet_resume error\n");
1900e0e474a8SSteve Glendinning 
1901b052e073SSteve Glendinning 	return ret;
1902e0e474a8SSteve Glendinning }
1903e0e474a8SSteve Glendinning 
1904b4df480fSJoonyoung Shim static int smsc95xx_reset_resume(struct usb_interface *intf)
1905b4df480fSJoonyoung Shim {
1906b4df480fSJoonyoung Shim 	struct usbnet *dev = usb_get_intfdata(intf);
1907b4df480fSJoonyoung Shim 	int ret;
1908b4df480fSJoonyoung Shim 
1909b4df480fSJoonyoung Shim 	ret = smsc95xx_reset(dev);
1910b4df480fSJoonyoung Shim 	if (ret < 0)
1911b4df480fSJoonyoung Shim 		return ret;
1912b4df480fSJoonyoung Shim 
1913b4df480fSJoonyoung Shim 	return smsc95xx_resume(intf);
1914b4df480fSJoonyoung Shim }
1915b4df480fSJoonyoung Shim 
19162f7ca802SSteve Glendinning static void smsc95xx_rx_csum_offload(struct sk_buff *skb)
19172f7ca802SSteve Glendinning {
19182f7ca802SSteve Glendinning 	skb->csum = *(u16 *)(skb_tail_pointer(skb) - 2);
19192f7ca802SSteve Glendinning 	skb->ip_summed = CHECKSUM_COMPLETE;
19202f7ca802SSteve Glendinning 	skb_trim(skb, skb->len - 2);
19212f7ca802SSteve Glendinning }
19222f7ca802SSteve Glendinning 
19232f7ca802SSteve Glendinning static int smsc95xx_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
19242f7ca802SSteve Glendinning {
1925eb85569fSEmil Goode 	/* This check is no longer done by usbnet */
1926eb85569fSEmil Goode 	if (skb->len < dev->net->hard_header_len)
1927eb85569fSEmil Goode 		return 0;
1928eb85569fSEmil Goode 
19292f7ca802SSteve Glendinning 	while (skb->len > 0) {
19302f7ca802SSteve Glendinning 		u32 header, align_count;
19312f7ca802SSteve Glendinning 		struct sk_buff *ax_skb;
19322f7ca802SSteve Glendinning 		unsigned char *packet;
19332f7ca802SSteve Glendinning 		u16 size;
19342f7ca802SSteve Glendinning 
1935*6809d216SBen Dooks 		header = get_unaligned_le32(skb->data);
19362f7ca802SSteve Glendinning 		skb_pull(skb, 4 + NET_IP_ALIGN);
19372f7ca802SSteve Glendinning 		packet = skb->data;
19382f7ca802SSteve Glendinning 
19392f7ca802SSteve Glendinning 		/* get the packet length */
19402f7ca802SSteve Glendinning 		size = (u16)((header & RX_STS_FL_) >> 16);
19412f7ca802SSteve Glendinning 		align_count = (4 - ((size + NET_IP_ALIGN) % 4)) % 4;
19422f7ca802SSteve Glendinning 
19432f7ca802SSteve Glendinning 		if (unlikely(header & RX_STS_ES_)) {
1944a475f603SJoe Perches 			netif_dbg(dev, rx_err, dev->net,
1945a475f603SJoe Perches 				  "Error header=0x%08x\n", header);
194680667ac1SHerbert Xu 			dev->net->stats.rx_errors++;
194780667ac1SHerbert Xu 			dev->net->stats.rx_dropped++;
19482f7ca802SSteve Glendinning 
19492f7ca802SSteve Glendinning 			if (header & RX_STS_CRC_) {
195080667ac1SHerbert Xu 				dev->net->stats.rx_crc_errors++;
19512f7ca802SSteve Glendinning 			} else {
19522f7ca802SSteve Glendinning 				if (header & (RX_STS_TL_ | RX_STS_RF_))
195380667ac1SHerbert Xu 					dev->net->stats.rx_frame_errors++;
19542f7ca802SSteve Glendinning 
19552f7ca802SSteve Glendinning 				if ((header & RX_STS_LE_) &&
19562f7ca802SSteve Glendinning 					(!(header & RX_STS_FT_)))
195780667ac1SHerbert Xu 					dev->net->stats.rx_length_errors++;
19582f7ca802SSteve Glendinning 			}
19592f7ca802SSteve Glendinning 		} else {
19602f7ca802SSteve Glendinning 			/* ETH_FRAME_LEN + 4(CRC) + 2(COE) + 4(Vlan) */
19612f7ca802SSteve Glendinning 			if (unlikely(size > (ETH_FRAME_LEN + 12))) {
1962a475f603SJoe Perches 				netif_dbg(dev, rx_err, dev->net,
1963a475f603SJoe Perches 					  "size err header=0x%08x\n", header);
19642f7ca802SSteve Glendinning 				return 0;
19652f7ca802SSteve Glendinning 			}
19662f7ca802SSteve Glendinning 
19672f7ca802SSteve Glendinning 			/* last frame in this batch */
19682f7ca802SSteve Glendinning 			if (skb->len == size) {
196978e47fe4SMichał Mirosław 				if (dev->net->features & NETIF_F_RXCSUM)
19702f7ca802SSteve Glendinning 					smsc95xx_rx_csum_offload(skb);
1971df18accaSPeter Korsgaard 				skb_trim(skb, skb->len - 4); /* remove fcs */
19722f7ca802SSteve Glendinning 				skb->truesize = size + sizeof(struct sk_buff);
19732f7ca802SSteve Glendinning 
19742f7ca802SSteve Glendinning 				return 1;
19752f7ca802SSteve Glendinning 			}
19762f7ca802SSteve Glendinning 
19772f7ca802SSteve Glendinning 			ax_skb = skb_clone(skb, GFP_ATOMIC);
19782f7ca802SSteve Glendinning 			if (unlikely(!ax_skb)) {
197960b86755SJoe Perches 				netdev_warn(dev->net, "Error allocating skb\n");
19802f7ca802SSteve Glendinning 				return 0;
19812f7ca802SSteve Glendinning 			}
19822f7ca802SSteve Glendinning 
19832f7ca802SSteve Glendinning 			ax_skb->len = size;
19842f7ca802SSteve Glendinning 			ax_skb->data = packet;
19852f7ca802SSteve Glendinning 			skb_set_tail_pointer(ax_skb, size);
19862f7ca802SSteve Glendinning 
198778e47fe4SMichał Mirosław 			if (dev->net->features & NETIF_F_RXCSUM)
19882f7ca802SSteve Glendinning 				smsc95xx_rx_csum_offload(ax_skb);
1989df18accaSPeter Korsgaard 			skb_trim(ax_skb, ax_skb->len - 4); /* remove fcs */
19902f7ca802SSteve Glendinning 			ax_skb->truesize = size + sizeof(struct sk_buff);
19912f7ca802SSteve Glendinning 
19922f7ca802SSteve Glendinning 			usbnet_skb_return(dev, ax_skb);
19932f7ca802SSteve Glendinning 		}
19942f7ca802SSteve Glendinning 
19952f7ca802SSteve Glendinning 		skb_pull(skb, size);
19962f7ca802SSteve Glendinning 
19972f7ca802SSteve Glendinning 		/* padding bytes before the next frame starts */
19982f7ca802SSteve Glendinning 		if (skb->len)
19992f7ca802SSteve Glendinning 			skb_pull(skb, align_count);
20002f7ca802SSteve Glendinning 	}
20012f7ca802SSteve Glendinning 
20022f7ca802SSteve Glendinning 	return 1;
20032f7ca802SSteve Glendinning }
20042f7ca802SSteve Glendinning 
2005f7b29271SSteve Glendinning static u32 smsc95xx_calc_csum_preamble(struct sk_buff *skb)
2006f7b29271SSteve Glendinning {
200755508d60SMichał Mirosław 	u16 low_16 = (u16)skb_checksum_start_offset(skb);
200855508d60SMichał Mirosław 	u16 high_16 = low_16 + skb->csum_offset;
2009f7b29271SSteve Glendinning 	return (high_16 << 16) | low_16;
2010f7b29271SSteve Glendinning }
2011f7b29271SSteve Glendinning 
20122f7ca802SSteve Glendinning static struct sk_buff *smsc95xx_tx_fixup(struct usbnet *dev,
20132f7ca802SSteve Glendinning 					 struct sk_buff *skb, gfp_t flags)
20142f7ca802SSteve Glendinning {
201578e47fe4SMichał Mirosław 	bool csum = skb->ip_summed == CHECKSUM_PARTIAL;
2016f7b29271SSteve Glendinning 	int overhead = csum ? SMSC95XX_TX_OVERHEAD_CSUM : SMSC95XX_TX_OVERHEAD;
20172f7ca802SSteve Glendinning 	u32 tx_cmd_a, tx_cmd_b;
20180c8b2655SBen Dooks 	void *ptr;
20192f7ca802SSteve Glendinning 
2020f7b29271SSteve Glendinning 	/* We do not advertise SG, so skbs should be already linearized */
2021f7b29271SSteve Glendinning 	BUG_ON(skb_shinfo(skb)->nr_frags);
2022f7b29271SSteve Glendinning 
2023e9156cd2SJames Hughes 	/* Make writable and expand header space by overhead if required */
2024e9156cd2SJames Hughes 	if (skb_cow_head(skb, overhead)) {
2025e9156cd2SJames Hughes 		/* Must deallocate here as returning NULL to indicate error
2026e9156cd2SJames Hughes 		 * means the skb won't be deallocated in the caller.
2027e9156cd2SJames Hughes 		 */
20282f7ca802SSteve Glendinning 		dev_kfree_skb_any(skb);
20292f7ca802SSteve Glendinning 		return NULL;
20302f7ca802SSteve Glendinning 	}
20312f7ca802SSteve Glendinning 
20320c8b2655SBen Dooks 	tx_cmd_b = (u32)skb->len;
20330c8b2655SBen Dooks 	tx_cmd_a = tx_cmd_b | TX_CMD_A_FIRST_SEG_ | TX_CMD_A_LAST_SEG_;
20340c8b2655SBen Dooks 
2035f7b29271SSteve Glendinning 	if (csum) {
203611bc3088SSteve Glendinning 		if (skb->len <= 45) {
203711bc3088SSteve Glendinning 			/* workaround - hardware tx checksum does not work
203811bc3088SSteve Glendinning 			 * properly with extremely small packets */
203955508d60SMichał Mirosław 			long csstart = skb_checksum_start_offset(skb);
204011bc3088SSteve Glendinning 			__wsum calc = csum_partial(skb->data + csstart,
204111bc3088SSteve Glendinning 				skb->len - csstart, 0);
204211bc3088SSteve Glendinning 			*((__sum16 *)(skb->data + csstart
204311bc3088SSteve Glendinning 				+ skb->csum_offset)) = csum_fold(calc);
204411bc3088SSteve Glendinning 
204511bc3088SSteve Glendinning 			csum = false;
204611bc3088SSteve Glendinning 		} else {
2047f7b29271SSteve Glendinning 			u32 csum_preamble = smsc95xx_calc_csum_preamble(skb);
20480c8b2655SBen Dooks 			ptr = skb_push(skb, 4);
20490c8b2655SBen Dooks 			put_unaligned_le32(csum_preamble, ptr);
2050f7b29271SSteve Glendinning 
20510c8b2655SBen Dooks 			tx_cmd_a += 4;
20520c8b2655SBen Dooks 			tx_cmd_b += 4;
2053f7b29271SSteve Glendinning 			tx_cmd_b |= TX_CMD_B_CSUM_ENABLE;
20540c8b2655SBen Dooks 		}
20550c8b2655SBen Dooks 	}
20562f7ca802SSteve Glendinning 
20570c8b2655SBen Dooks 	ptr = skb_push(skb, 8);
20580c8b2655SBen Dooks 	put_unaligned_le32(tx_cmd_a, ptr);
20590c8b2655SBen Dooks 	put_unaligned_le32(tx_cmd_b, ptr+4);
20602f7ca802SSteve Glendinning 
20612f7ca802SSteve Glendinning 	return skb;
20622f7ca802SSteve Glendinning }
20632f7ca802SSteve Glendinning 
2064b2d4b150SSteve Glendinning static int smsc95xx_manage_power(struct usbnet *dev, int on)
2065b2d4b150SSteve Glendinning {
2066b2d4b150SSteve Glendinning 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
2067b2d4b150SSteve Glendinning 
2068b2d4b150SSteve Glendinning 	dev->intf->needs_remote_wakeup = on;
2069b2d4b150SSteve Glendinning 
2070eb970ff0SMing Lei 	if (pdata->features & FEATURE_REMOTE_WAKEUP)
2071b2d4b150SSteve Glendinning 		return 0;
2072b2d4b150SSteve Glendinning 
2073eb970ff0SMing Lei 	/* this chip revision isn't capable of remote wakeup */
2074eb970ff0SMing Lei 	netdev_info(dev->net, "hardware isn't capable of remote wakeup\n");
2075b2d4b150SSteve Glendinning 
2076b2d4b150SSteve Glendinning 	if (on)
2077b2d4b150SSteve Glendinning 		usb_autopm_get_interface_no_resume(dev->intf);
2078b2d4b150SSteve Glendinning 	else
2079b2d4b150SSteve Glendinning 		usb_autopm_put_interface(dev->intf);
2080b2d4b150SSteve Glendinning 
2081b2d4b150SSteve Glendinning 	return 0;
2082b2d4b150SSteve Glendinning }
2083b2d4b150SSteve Glendinning 
20842f7ca802SSteve Glendinning static const struct driver_info smsc95xx_info = {
20852f7ca802SSteve Glendinning 	.description	= "smsc95xx USB 2.0 Ethernet",
20862f7ca802SSteve Glendinning 	.bind		= smsc95xx_bind,
20872f7ca802SSteve Glendinning 	.unbind		= smsc95xx_unbind,
20882f7ca802SSteve Glendinning 	.link_reset	= smsc95xx_link_reset,
20892f7ca802SSteve Glendinning 	.reset		= smsc95xx_reset,
20902f7ca802SSteve Glendinning 	.rx_fixup	= smsc95xx_rx_fixup,
20912f7ca802SSteve Glendinning 	.tx_fixup	= smsc95xx_tx_fixup,
20922f7ca802SSteve Glendinning 	.status		= smsc95xx_status,
2093b2d4b150SSteve Glendinning 	.manage_power	= smsc95xx_manage_power,
209407d69d42SPaolo Pisati 	.flags		= FLAG_ETHER | FLAG_SEND_ZLP | FLAG_LINK_INTR,
20952f7ca802SSteve Glendinning };
20962f7ca802SSteve Glendinning 
20972f7ca802SSteve Glendinning static const struct usb_device_id products[] = {
20982f7ca802SSteve Glendinning 	{
20992f7ca802SSteve Glendinning 		/* SMSC9500 USB Ethernet Device */
21002f7ca802SSteve Glendinning 		USB_DEVICE(0x0424, 0x9500),
21012f7ca802SSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21022f7ca802SSteve Glendinning 	},
2103726474b8SSteve Glendinning 	{
21046f41d12bSSteve Glendinning 		/* SMSC9505 USB Ethernet Device */
21056f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9505),
21066f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21076f41d12bSSteve Glendinning 	},
21086f41d12bSSteve Glendinning 	{
21096f41d12bSSteve Glendinning 		/* SMSC9500A USB Ethernet Device */
21106f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9E00),
21116f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21126f41d12bSSteve Glendinning 	},
21136f41d12bSSteve Glendinning 	{
21146f41d12bSSteve Glendinning 		/* SMSC9505A USB Ethernet Device */
21156f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9E01),
21166f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21176f41d12bSSteve Glendinning 	},
21186f41d12bSSteve Glendinning 	{
2119726474b8SSteve Glendinning 		/* SMSC9512/9514 USB Hub & Ethernet Device */
2120726474b8SSteve Glendinning 		USB_DEVICE(0x0424, 0xec00),
2121726474b8SSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
2122726474b8SSteve Glendinning 	},
21236f41d12bSSteve Glendinning 	{
21246f41d12bSSteve Glendinning 		/* SMSC9500 USB Ethernet Device (SAL10) */
21256f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9900),
21266f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21276f41d12bSSteve Glendinning 	},
21286f41d12bSSteve Glendinning 	{
21296f41d12bSSteve Glendinning 		/* SMSC9505 USB Ethernet Device (SAL10) */
21306f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9901),
21316f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21326f41d12bSSteve Glendinning 	},
21336f41d12bSSteve Glendinning 	{
21346f41d12bSSteve Glendinning 		/* SMSC9500A USB Ethernet Device (SAL10) */
21356f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9902),
21366f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21376f41d12bSSteve Glendinning 	},
21386f41d12bSSteve Glendinning 	{
21396f41d12bSSteve Glendinning 		/* SMSC9505A USB Ethernet Device (SAL10) */
21406f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9903),
21416f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21426f41d12bSSteve Glendinning 	},
21436f41d12bSSteve Glendinning 	{
21446f41d12bSSteve Glendinning 		/* SMSC9512/9514 USB Hub & Ethernet Device (SAL10) */
21456f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9904),
21466f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21476f41d12bSSteve Glendinning 	},
21486f41d12bSSteve Glendinning 	{
21496f41d12bSSteve Glendinning 		/* SMSC9500A USB Ethernet Device (HAL) */
21506f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9905),
21516f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21526f41d12bSSteve Glendinning 	},
21536f41d12bSSteve Glendinning 	{
21546f41d12bSSteve Glendinning 		/* SMSC9505A USB Ethernet Device (HAL) */
21556f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9906),
21566f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21576f41d12bSSteve Glendinning 	},
21586f41d12bSSteve Glendinning 	{
21596f41d12bSSteve Glendinning 		/* SMSC9500 USB Ethernet Device (Alternate ID) */
21606f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9907),
21616f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21626f41d12bSSteve Glendinning 	},
21636f41d12bSSteve Glendinning 	{
21646f41d12bSSteve Glendinning 		/* SMSC9500A USB Ethernet Device (Alternate ID) */
21656f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9908),
21666f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21676f41d12bSSteve Glendinning 	},
21686f41d12bSSteve Glendinning 	{
21696f41d12bSSteve Glendinning 		/* SMSC9512/9514 USB Hub & Ethernet Device (Alternate ID) */
21706f41d12bSSteve Glendinning 		USB_DEVICE(0x0424, 0x9909),
21716f41d12bSSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
21726f41d12bSSteve Glendinning 	},
217388edaa41SSteve Glendinning 	{
217488edaa41SSteve Glendinning 		/* SMSC LAN9530 USB Ethernet Device */
217588edaa41SSteve Glendinning 		USB_DEVICE(0x0424, 0x9530),
217688edaa41SSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
217788edaa41SSteve Glendinning 	},
217888edaa41SSteve Glendinning 	{
217988edaa41SSteve Glendinning 		/* SMSC LAN9730 USB Ethernet Device */
218088edaa41SSteve Glendinning 		USB_DEVICE(0x0424, 0x9730),
218188edaa41SSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
218288edaa41SSteve Glendinning 	},
218388edaa41SSteve Glendinning 	{
218488edaa41SSteve Glendinning 		/* SMSC LAN89530 USB Ethernet Device */
218588edaa41SSteve Glendinning 		USB_DEVICE(0x0424, 0x9E08),
218688edaa41SSteve Glendinning 		.driver_info = (unsigned long) &smsc95xx_info,
218788edaa41SSteve Glendinning 	},
21882f7ca802SSteve Glendinning 	{ },		/* END */
21892f7ca802SSteve Glendinning };
21902f7ca802SSteve Glendinning MODULE_DEVICE_TABLE(usb, products);
21912f7ca802SSteve Glendinning 
21922f7ca802SSteve Glendinning static struct usb_driver smsc95xx_driver = {
21932f7ca802SSteve Glendinning 	.name		= "smsc95xx",
21942f7ca802SSteve Glendinning 	.id_table	= products,
21952f7ca802SSteve Glendinning 	.probe		= usbnet_probe,
2196b5a04475SSteve Glendinning 	.suspend	= smsc95xx_suspend,
2197e0e474a8SSteve Glendinning 	.resume		= smsc95xx_resume,
2198b4df480fSJoonyoung Shim 	.reset_resume	= smsc95xx_reset_resume,
21992f7ca802SSteve Glendinning 	.disconnect	= usbnet_disconnect,
2200e1f12eb6SSarah Sharp 	.disable_hub_initiated_lpm = 1,
2201b2d4b150SSteve Glendinning 	.supports_autosuspend = 1,
22022f7ca802SSteve Glendinning };
22032f7ca802SSteve Glendinning 
2204d632eb1bSGreg Kroah-Hartman module_usb_driver(smsc95xx_driver);
22052f7ca802SSteve Glendinning 
22062f7ca802SSteve Glendinning MODULE_AUTHOR("Nancy Lin");
220790b24cfbSSteve Glendinning MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>");
22082f7ca802SSteve Glendinning MODULE_DESCRIPTION("SMSC95XX USB 2.0 Ethernet Devices");
22092f7ca802SSteve Glendinning MODULE_LICENSE("GPL");
2210