xref: /openbmc/linux/drivers/net/usb/smsc95xx.c (revision 151f4e2b)
1  /***************************************************************************
2  *
3  * Copyright (C) 2007-2008 SMSC
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, see <http://www.gnu.org/licenses/>.
17  *
18  *****************************************************************************/
19 
20 #include <linux/module.h>
21 #include <linux/kmod.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/ethtool.h>
25 #include <linux/mii.h>
26 #include <linux/usb.h>
27 #include <linux/bitrev.h>
28 #include <linux/crc16.h>
29 #include <linux/crc32.h>
30 #include <linux/usb/usbnet.h>
31 #include <linux/slab.h>
32 #include <linux/of_net.h>
33 #include "smsc95xx.h"
34 
35 #define SMSC_CHIPNAME			"smsc95xx"
36 #define SMSC_DRIVER_VERSION		"1.0.6"
37 #define HS_USB_PKT_SIZE			(512)
38 #define FS_USB_PKT_SIZE			(64)
39 #define DEFAULT_HS_BURST_CAP_SIZE	(16 * 1024 + 5 * HS_USB_PKT_SIZE)
40 #define DEFAULT_FS_BURST_CAP_SIZE	(6 * 1024 + 33 * FS_USB_PKT_SIZE)
41 #define DEFAULT_BULK_IN_DELAY		(0x00002000)
42 #define MAX_SINGLE_PACKET_SIZE		(2048)
43 #define LAN95XX_EEPROM_MAGIC		(0x9500)
44 #define EEPROM_MAC_OFFSET		(0x01)
45 #define DEFAULT_TX_CSUM_ENABLE		(true)
46 #define DEFAULT_RX_CSUM_ENABLE		(true)
47 #define SMSC95XX_INTERNAL_PHY_ID	(1)
48 #define SMSC95XX_TX_OVERHEAD		(8)
49 #define SMSC95XX_TX_OVERHEAD_CSUM	(12)
50 #define SUPPORTED_WAKE			(WAKE_PHY | WAKE_UCAST | WAKE_BCAST | \
51 					 WAKE_MCAST | WAKE_ARP | WAKE_MAGIC)
52 
53 #define FEATURE_8_WAKEUP_FILTERS	(0x01)
54 #define FEATURE_PHY_NLP_CROSSOVER	(0x02)
55 #define FEATURE_REMOTE_WAKEUP		(0x04)
56 
57 #define SUSPEND_SUSPEND0		(0x01)
58 #define SUSPEND_SUSPEND1		(0x02)
59 #define SUSPEND_SUSPEND2		(0x04)
60 #define SUSPEND_SUSPEND3		(0x08)
61 #define SUSPEND_ALLMODES		(SUSPEND_SUSPEND0 | SUSPEND_SUSPEND1 | \
62 					 SUSPEND_SUSPEND2 | SUSPEND_SUSPEND3)
63 
64 #define CARRIER_CHECK_DELAY (2 * HZ)
65 
66 struct smsc95xx_priv {
67 	u32 chip_id;
68 	u32 mac_cr;
69 	u32 hash_hi;
70 	u32 hash_lo;
71 	u32 wolopts;
72 	spinlock_t mac_cr_lock;
73 	u8 features;
74 	u8 suspend_flags;
75 	u8 mdix_ctrl;
76 	bool link_ok;
77 	struct delayed_work carrier_check;
78 	struct usbnet *dev;
79 };
80 
81 static bool turbo_mode = true;
82 module_param(turbo_mode, bool, 0644);
83 MODULE_PARM_DESC(turbo_mode, "Enable multiple frames per Rx transaction");
84 
85 static int __must_check __smsc95xx_read_reg(struct usbnet *dev, u32 index,
86 					    u32 *data, int in_pm)
87 {
88 	u32 buf;
89 	int ret;
90 	int (*fn)(struct usbnet *, u8, u8, u16, u16, void *, u16);
91 
92 	BUG_ON(!dev);
93 
94 	if (!in_pm)
95 		fn = usbnet_read_cmd;
96 	else
97 		fn = usbnet_read_cmd_nopm;
98 
99 	ret = fn(dev, USB_VENDOR_REQUEST_READ_REGISTER, USB_DIR_IN
100 		 | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
101 		 0, index, &buf, 4);
102 	if (unlikely(ret < 0)) {
103 		netdev_warn(dev->net, "Failed to read reg index 0x%08x: %d\n",
104 			    index, ret);
105 		return ret;
106 	}
107 
108 	le32_to_cpus(&buf);
109 	*data = buf;
110 
111 	return ret;
112 }
113 
114 static int __must_check __smsc95xx_write_reg(struct usbnet *dev, u32 index,
115 					     u32 data, int in_pm)
116 {
117 	u32 buf;
118 	int ret;
119 	int (*fn)(struct usbnet *, u8, u8, u16, u16, const void *, u16);
120 
121 	BUG_ON(!dev);
122 
123 	if (!in_pm)
124 		fn = usbnet_write_cmd;
125 	else
126 		fn = usbnet_write_cmd_nopm;
127 
128 	buf = data;
129 	cpu_to_le32s(&buf);
130 
131 	ret = fn(dev, USB_VENDOR_REQUEST_WRITE_REGISTER, USB_DIR_OUT
132 		 | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
133 		 0, index, &buf, 4);
134 	if (unlikely(ret < 0))
135 		netdev_warn(dev->net, "Failed to write reg index 0x%08x: %d\n",
136 			    index, ret);
137 
138 	return ret;
139 }
140 
141 static int __must_check smsc95xx_read_reg_nopm(struct usbnet *dev, u32 index,
142 					       u32 *data)
143 {
144 	return __smsc95xx_read_reg(dev, index, data, 1);
145 }
146 
147 static int __must_check smsc95xx_write_reg_nopm(struct usbnet *dev, u32 index,
148 						u32 data)
149 {
150 	return __smsc95xx_write_reg(dev, index, data, 1);
151 }
152 
153 static int __must_check smsc95xx_read_reg(struct usbnet *dev, u32 index,
154 					  u32 *data)
155 {
156 	return __smsc95xx_read_reg(dev, index, data, 0);
157 }
158 
159 static int __must_check smsc95xx_write_reg(struct usbnet *dev, u32 index,
160 					   u32 data)
161 {
162 	return __smsc95xx_write_reg(dev, index, data, 0);
163 }
164 
165 /* Loop until the read is completed with timeout
166  * called with phy_mutex held */
167 static int __must_check __smsc95xx_phy_wait_not_busy(struct usbnet *dev,
168 						     int in_pm)
169 {
170 	unsigned long start_time = jiffies;
171 	u32 val;
172 	int ret;
173 
174 	do {
175 		ret = __smsc95xx_read_reg(dev, MII_ADDR, &val, in_pm);
176 		if (ret < 0) {
177 			netdev_warn(dev->net, "Error reading MII_ACCESS\n");
178 			return ret;
179 		}
180 
181 		if (!(val & MII_BUSY_))
182 			return 0;
183 	} while (!time_after(jiffies, start_time + HZ));
184 
185 	return -EIO;
186 }
187 
188 static int __smsc95xx_mdio_read(struct net_device *netdev, int phy_id, int idx,
189 				int in_pm)
190 {
191 	struct usbnet *dev = netdev_priv(netdev);
192 	u32 val, addr;
193 	int ret;
194 
195 	mutex_lock(&dev->phy_mutex);
196 
197 	/* confirm MII not busy */
198 	ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
199 	if (ret < 0) {
200 		netdev_warn(dev->net, "MII is busy in smsc95xx_mdio_read\n");
201 		goto done;
202 	}
203 
204 	/* set the address, index & direction (read from PHY) */
205 	phy_id &= dev->mii.phy_id_mask;
206 	idx &= dev->mii.reg_num_mask;
207 	addr = (phy_id << 11) | (idx << 6) | MII_READ_ | MII_BUSY_;
208 	ret = __smsc95xx_write_reg(dev, MII_ADDR, addr, in_pm);
209 	if (ret < 0) {
210 		netdev_warn(dev->net, "Error writing MII_ADDR\n");
211 		goto done;
212 	}
213 
214 	ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
215 	if (ret < 0) {
216 		netdev_warn(dev->net, "Timed out reading MII reg %02X\n", idx);
217 		goto done;
218 	}
219 
220 	ret = __smsc95xx_read_reg(dev, MII_DATA, &val, in_pm);
221 	if (ret < 0) {
222 		netdev_warn(dev->net, "Error reading MII_DATA\n");
223 		goto done;
224 	}
225 
226 	ret = (u16)(val & 0xFFFF);
227 
228 done:
229 	mutex_unlock(&dev->phy_mutex);
230 	return ret;
231 }
232 
233 static void __smsc95xx_mdio_write(struct net_device *netdev, int phy_id,
234 				  int idx, int regval, int in_pm)
235 {
236 	struct usbnet *dev = netdev_priv(netdev);
237 	u32 val, addr;
238 	int ret;
239 
240 	mutex_lock(&dev->phy_mutex);
241 
242 	/* confirm MII not busy */
243 	ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
244 	if (ret < 0) {
245 		netdev_warn(dev->net, "MII is busy in smsc95xx_mdio_write\n");
246 		goto done;
247 	}
248 
249 	val = regval;
250 	ret = __smsc95xx_write_reg(dev, MII_DATA, val, in_pm);
251 	if (ret < 0) {
252 		netdev_warn(dev->net, "Error writing MII_DATA\n");
253 		goto done;
254 	}
255 
256 	/* set the address, index & direction (write to PHY) */
257 	phy_id &= dev->mii.phy_id_mask;
258 	idx &= dev->mii.reg_num_mask;
259 	addr = (phy_id << 11) | (idx << 6) | MII_WRITE_ | MII_BUSY_;
260 	ret = __smsc95xx_write_reg(dev, MII_ADDR, addr, in_pm);
261 	if (ret < 0) {
262 		netdev_warn(dev->net, "Error writing MII_ADDR\n");
263 		goto done;
264 	}
265 
266 	ret = __smsc95xx_phy_wait_not_busy(dev, in_pm);
267 	if (ret < 0) {
268 		netdev_warn(dev->net, "Timed out writing MII reg %02X\n", idx);
269 		goto done;
270 	}
271 
272 done:
273 	mutex_unlock(&dev->phy_mutex);
274 }
275 
276 static int smsc95xx_mdio_read_nopm(struct net_device *netdev, int phy_id,
277 				   int idx)
278 {
279 	return __smsc95xx_mdio_read(netdev, phy_id, idx, 1);
280 }
281 
282 static void smsc95xx_mdio_write_nopm(struct net_device *netdev, int phy_id,
283 				     int idx, int regval)
284 {
285 	__smsc95xx_mdio_write(netdev, phy_id, idx, regval, 1);
286 }
287 
288 static int smsc95xx_mdio_read(struct net_device *netdev, int phy_id, int idx)
289 {
290 	return __smsc95xx_mdio_read(netdev, phy_id, idx, 0);
291 }
292 
293 static void smsc95xx_mdio_write(struct net_device *netdev, int phy_id, int idx,
294 				int regval)
295 {
296 	__smsc95xx_mdio_write(netdev, phy_id, idx, regval, 0);
297 }
298 
299 static int __must_check smsc95xx_wait_eeprom(struct usbnet *dev)
300 {
301 	unsigned long start_time = jiffies;
302 	u32 val;
303 	int ret;
304 
305 	do {
306 		ret = smsc95xx_read_reg(dev, E2P_CMD, &val);
307 		if (ret < 0) {
308 			netdev_warn(dev->net, "Error reading E2P_CMD\n");
309 			return ret;
310 		}
311 
312 		if (!(val & E2P_CMD_BUSY_) || (val & E2P_CMD_TIMEOUT_))
313 			break;
314 		udelay(40);
315 	} while (!time_after(jiffies, start_time + HZ));
316 
317 	if (val & (E2P_CMD_TIMEOUT_ | E2P_CMD_BUSY_)) {
318 		netdev_warn(dev->net, "EEPROM read operation timeout\n");
319 		return -EIO;
320 	}
321 
322 	return 0;
323 }
324 
325 static int __must_check smsc95xx_eeprom_confirm_not_busy(struct usbnet *dev)
326 {
327 	unsigned long start_time = jiffies;
328 	u32 val;
329 	int ret;
330 
331 	do {
332 		ret = smsc95xx_read_reg(dev, E2P_CMD, &val);
333 		if (ret < 0) {
334 			netdev_warn(dev->net, "Error reading E2P_CMD\n");
335 			return ret;
336 		}
337 
338 		if (!(val & E2P_CMD_BUSY_))
339 			return 0;
340 
341 		udelay(40);
342 	} while (!time_after(jiffies, start_time + HZ));
343 
344 	netdev_warn(dev->net, "EEPROM is busy\n");
345 	return -EIO;
346 }
347 
348 static int smsc95xx_read_eeprom(struct usbnet *dev, u32 offset, u32 length,
349 				u8 *data)
350 {
351 	u32 val;
352 	int i, ret;
353 
354 	BUG_ON(!dev);
355 	BUG_ON(!data);
356 
357 	ret = smsc95xx_eeprom_confirm_not_busy(dev);
358 	if (ret)
359 		return ret;
360 
361 	for (i = 0; i < length; i++) {
362 		val = E2P_CMD_BUSY_ | E2P_CMD_READ_ | (offset & E2P_CMD_ADDR_);
363 		ret = smsc95xx_write_reg(dev, E2P_CMD, val);
364 		if (ret < 0) {
365 			netdev_warn(dev->net, "Error writing E2P_CMD\n");
366 			return ret;
367 		}
368 
369 		ret = smsc95xx_wait_eeprom(dev);
370 		if (ret < 0)
371 			return ret;
372 
373 		ret = smsc95xx_read_reg(dev, E2P_DATA, &val);
374 		if (ret < 0) {
375 			netdev_warn(dev->net, "Error reading E2P_DATA\n");
376 			return ret;
377 		}
378 
379 		data[i] = val & 0xFF;
380 		offset++;
381 	}
382 
383 	return 0;
384 }
385 
386 static int smsc95xx_write_eeprom(struct usbnet *dev, u32 offset, u32 length,
387 				 u8 *data)
388 {
389 	u32 val;
390 	int i, ret;
391 
392 	BUG_ON(!dev);
393 	BUG_ON(!data);
394 
395 	ret = smsc95xx_eeprom_confirm_not_busy(dev);
396 	if (ret)
397 		return ret;
398 
399 	/* Issue write/erase enable command */
400 	val = E2P_CMD_BUSY_ | E2P_CMD_EWEN_;
401 	ret = smsc95xx_write_reg(dev, E2P_CMD, val);
402 	if (ret < 0) {
403 		netdev_warn(dev->net, "Error writing E2P_DATA\n");
404 		return ret;
405 	}
406 
407 	ret = smsc95xx_wait_eeprom(dev);
408 	if (ret < 0)
409 		return ret;
410 
411 	for (i = 0; i < length; i++) {
412 
413 		/* Fill data register */
414 		val = data[i];
415 		ret = smsc95xx_write_reg(dev, E2P_DATA, val);
416 		if (ret < 0) {
417 			netdev_warn(dev->net, "Error writing E2P_DATA\n");
418 			return ret;
419 		}
420 
421 		/* Send "write" command */
422 		val = E2P_CMD_BUSY_ | E2P_CMD_WRITE_ | (offset & E2P_CMD_ADDR_);
423 		ret = smsc95xx_write_reg(dev, E2P_CMD, val);
424 		if (ret < 0) {
425 			netdev_warn(dev->net, "Error writing E2P_CMD\n");
426 			return ret;
427 		}
428 
429 		ret = smsc95xx_wait_eeprom(dev);
430 		if (ret < 0)
431 			return ret;
432 
433 		offset++;
434 	}
435 
436 	return 0;
437 }
438 
439 static int __must_check smsc95xx_write_reg_async(struct usbnet *dev, u16 index,
440 						 u32 data)
441 {
442 	const u16 size = 4;
443 	u32 buf;
444 	int ret;
445 
446 	buf = data;
447 	cpu_to_le32s(&buf);
448 
449 	ret = usbnet_write_cmd_async(dev, USB_VENDOR_REQUEST_WRITE_REGISTER,
450 				     USB_DIR_OUT | USB_TYPE_VENDOR |
451 				     USB_RECIP_DEVICE,
452 				     0, index, &buf, size);
453 	if (ret < 0)
454 		netdev_warn(dev->net, "Error write async cmd, sts=%d\n",
455 			    ret);
456 	return ret;
457 }
458 
459 /* returns hash bit number for given MAC address
460  * example:
461  * 01 00 5E 00 00 01 -> returns bit number 31 */
462 static unsigned int smsc95xx_hash(char addr[ETH_ALEN])
463 {
464 	return (ether_crc(ETH_ALEN, addr) >> 26) & 0x3f;
465 }
466 
467 static void smsc95xx_set_multicast(struct net_device *netdev)
468 {
469 	struct usbnet *dev = netdev_priv(netdev);
470 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
471 	unsigned long flags;
472 	int ret;
473 
474 	pdata->hash_hi = 0;
475 	pdata->hash_lo = 0;
476 
477 	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
478 
479 	if (dev->net->flags & IFF_PROMISC) {
480 		netif_dbg(dev, drv, dev->net, "promiscuous mode enabled\n");
481 		pdata->mac_cr |= MAC_CR_PRMS_;
482 		pdata->mac_cr &= ~(MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
483 	} else if (dev->net->flags & IFF_ALLMULTI) {
484 		netif_dbg(dev, drv, dev->net, "receive all multicast enabled\n");
485 		pdata->mac_cr |= MAC_CR_MCPAS_;
486 		pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_HPFILT_);
487 	} else if (!netdev_mc_empty(dev->net)) {
488 		struct netdev_hw_addr *ha;
489 
490 		pdata->mac_cr |= MAC_CR_HPFILT_;
491 		pdata->mac_cr &= ~(MAC_CR_PRMS_ | MAC_CR_MCPAS_);
492 
493 		netdev_for_each_mc_addr(ha, netdev) {
494 			u32 bitnum = smsc95xx_hash(ha->addr);
495 			u32 mask = 0x01 << (bitnum & 0x1F);
496 			if (bitnum & 0x20)
497 				pdata->hash_hi |= mask;
498 			else
499 				pdata->hash_lo |= mask;
500 		}
501 
502 		netif_dbg(dev, drv, dev->net, "HASHH=0x%08X, HASHL=0x%08X\n",
503 				   pdata->hash_hi, pdata->hash_lo);
504 	} else {
505 		netif_dbg(dev, drv, dev->net, "receive own packets only\n");
506 		pdata->mac_cr &=
507 			~(MAC_CR_PRMS_ | MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
508 	}
509 
510 	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
511 
512 	/* Initiate async writes, as we can't wait for completion here */
513 	ret = smsc95xx_write_reg_async(dev, HASHH, pdata->hash_hi);
514 	if (ret < 0)
515 		netdev_warn(dev->net, "failed to initiate async write to HASHH\n");
516 
517 	ret = smsc95xx_write_reg_async(dev, HASHL, pdata->hash_lo);
518 	if (ret < 0)
519 		netdev_warn(dev->net, "failed to initiate async write to HASHL\n");
520 
521 	ret = smsc95xx_write_reg_async(dev, MAC_CR, pdata->mac_cr);
522 	if (ret < 0)
523 		netdev_warn(dev->net, "failed to initiate async write to MAC_CR\n");
524 }
525 
526 static int smsc95xx_phy_update_flowcontrol(struct usbnet *dev, u8 duplex,
527 					   u16 lcladv, u16 rmtadv)
528 {
529 	u32 flow = 0, afc_cfg;
530 
531 	int ret = smsc95xx_read_reg(dev, AFC_CFG, &afc_cfg);
532 	if (ret < 0)
533 		return ret;
534 
535 	if (duplex == DUPLEX_FULL) {
536 		u8 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
537 
538 		if (cap & FLOW_CTRL_RX)
539 			flow = 0xFFFF0002;
540 
541 		if (cap & FLOW_CTRL_TX) {
542 			afc_cfg |= 0xF;
543 			flow |= 0xFFFF0000;
544 		} else {
545 			afc_cfg &= ~0xF;
546 		}
547 
548 		netif_dbg(dev, link, dev->net, "rx pause %s, tx pause %s\n",
549 				   cap & FLOW_CTRL_RX ? "enabled" : "disabled",
550 				   cap & FLOW_CTRL_TX ? "enabled" : "disabled");
551 	} else {
552 		netif_dbg(dev, link, dev->net, "half duplex\n");
553 		afc_cfg |= 0xF;
554 	}
555 
556 	ret = smsc95xx_write_reg(dev, FLOW, flow);
557 	if (ret < 0)
558 		return ret;
559 
560 	return smsc95xx_write_reg(dev, AFC_CFG, afc_cfg);
561 }
562 
563 static int smsc95xx_link_reset(struct usbnet *dev)
564 {
565 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
566 	struct mii_if_info *mii = &dev->mii;
567 	struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
568 	unsigned long flags;
569 	u16 lcladv, rmtadv;
570 	int ret;
571 
572 	/* clear interrupt status */
573 	ret = smsc95xx_mdio_read(dev->net, mii->phy_id, PHY_INT_SRC);
574 	if (ret < 0)
575 		return ret;
576 
577 	ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
578 	if (ret < 0)
579 		return ret;
580 
581 	mii_check_media(mii, 1, 1);
582 	mii_ethtool_gset(&dev->mii, &ecmd);
583 	lcladv = smsc95xx_mdio_read(dev->net, mii->phy_id, MII_ADVERTISE);
584 	rmtadv = smsc95xx_mdio_read(dev->net, mii->phy_id, MII_LPA);
585 
586 	netif_dbg(dev, link, dev->net,
587 		  "speed: %u duplex: %d lcladv: %04x rmtadv: %04x\n",
588 		  ethtool_cmd_speed(&ecmd), ecmd.duplex, lcladv, rmtadv);
589 
590 	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
591 	if (ecmd.duplex != DUPLEX_FULL) {
592 		pdata->mac_cr &= ~MAC_CR_FDPX_;
593 		pdata->mac_cr |= MAC_CR_RCVOWN_;
594 	} else {
595 		pdata->mac_cr &= ~MAC_CR_RCVOWN_;
596 		pdata->mac_cr |= MAC_CR_FDPX_;
597 	}
598 	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
599 
600 	ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
601 	if (ret < 0)
602 		return ret;
603 
604 	ret = smsc95xx_phy_update_flowcontrol(dev, ecmd.duplex, lcladv, rmtadv);
605 	if (ret < 0)
606 		netdev_warn(dev->net, "Error updating PHY flow control\n");
607 
608 	return ret;
609 }
610 
611 static void smsc95xx_status(struct usbnet *dev, struct urb *urb)
612 {
613 	u32 intdata;
614 
615 	if (urb->actual_length != 4) {
616 		netdev_warn(dev->net, "unexpected urb length %d\n",
617 			    urb->actual_length);
618 		return;
619 	}
620 
621 	intdata = get_unaligned_le32(urb->transfer_buffer);
622 	netif_dbg(dev, link, dev->net, "intdata: 0x%08X\n", intdata);
623 
624 	if (intdata & INT_ENP_PHY_INT_)
625 		usbnet_defer_kevent(dev, EVENT_LINK_RESET);
626 	else
627 		netdev_warn(dev->net, "unexpected interrupt, intdata=0x%08X\n",
628 			    intdata);
629 }
630 
631 static void set_carrier(struct usbnet *dev, bool link)
632 {
633 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
634 
635 	if (pdata->link_ok == link)
636 		return;
637 
638 	pdata->link_ok = link;
639 
640 	if (link)
641 		usbnet_link_change(dev, 1, 0);
642 	else
643 		usbnet_link_change(dev, 0, 0);
644 }
645 
646 static void check_carrier(struct work_struct *work)
647 {
648 	struct smsc95xx_priv *pdata = container_of(work, struct smsc95xx_priv,
649 						carrier_check.work);
650 	struct usbnet *dev = pdata->dev;
651 	int ret;
652 
653 	if (pdata->suspend_flags != 0)
654 		return;
655 
656 	ret = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, MII_BMSR);
657 	if (ret < 0) {
658 		netdev_warn(dev->net, "Failed to read MII_BMSR\n");
659 		return;
660 	}
661 	if (ret & BMSR_LSTATUS)
662 		set_carrier(dev, 1);
663 	else
664 		set_carrier(dev, 0);
665 
666 	schedule_delayed_work(&pdata->carrier_check, CARRIER_CHECK_DELAY);
667 }
668 
669 /* Enable or disable Tx & Rx checksum offload engines */
670 static int smsc95xx_set_features(struct net_device *netdev,
671 	netdev_features_t features)
672 {
673 	struct usbnet *dev = netdev_priv(netdev);
674 	u32 read_buf;
675 	int ret;
676 
677 	ret = smsc95xx_read_reg(dev, COE_CR, &read_buf);
678 	if (ret < 0)
679 		return ret;
680 
681 	if (features & NETIF_F_IP_CSUM)
682 		read_buf |= Tx_COE_EN_;
683 	else
684 		read_buf &= ~Tx_COE_EN_;
685 
686 	if (features & NETIF_F_RXCSUM)
687 		read_buf |= Rx_COE_EN_;
688 	else
689 		read_buf &= ~Rx_COE_EN_;
690 
691 	ret = smsc95xx_write_reg(dev, COE_CR, read_buf);
692 	if (ret < 0)
693 		return ret;
694 
695 	netif_dbg(dev, hw, dev->net, "COE_CR = 0x%08x\n", read_buf);
696 	return 0;
697 }
698 
699 static int smsc95xx_ethtool_get_eeprom_len(struct net_device *net)
700 {
701 	return MAX_EEPROM_SIZE;
702 }
703 
704 static int smsc95xx_ethtool_get_eeprom(struct net_device *netdev,
705 				       struct ethtool_eeprom *ee, u8 *data)
706 {
707 	struct usbnet *dev = netdev_priv(netdev);
708 
709 	ee->magic = LAN95XX_EEPROM_MAGIC;
710 
711 	return smsc95xx_read_eeprom(dev, ee->offset, ee->len, data);
712 }
713 
714 static int smsc95xx_ethtool_set_eeprom(struct net_device *netdev,
715 				       struct ethtool_eeprom *ee, u8 *data)
716 {
717 	struct usbnet *dev = netdev_priv(netdev);
718 
719 	if (ee->magic != LAN95XX_EEPROM_MAGIC) {
720 		netdev_warn(dev->net, "EEPROM: magic value mismatch, magic = 0x%x\n",
721 			    ee->magic);
722 		return -EINVAL;
723 	}
724 
725 	return smsc95xx_write_eeprom(dev, ee->offset, ee->len, data);
726 }
727 
728 static int smsc95xx_ethtool_getregslen(struct net_device *netdev)
729 {
730 	/* all smsc95xx registers */
731 	return COE_CR - ID_REV + sizeof(u32);
732 }
733 
734 static void
735 smsc95xx_ethtool_getregs(struct net_device *netdev, struct ethtool_regs *regs,
736 			 void *buf)
737 {
738 	struct usbnet *dev = netdev_priv(netdev);
739 	unsigned int i, j;
740 	int retval;
741 	u32 *data = buf;
742 
743 	retval = smsc95xx_read_reg(dev, ID_REV, &regs->version);
744 	if (retval < 0) {
745 		netdev_warn(netdev, "REGS: cannot read ID_REV\n");
746 		return;
747 	}
748 
749 	for (i = ID_REV, j = 0; i <= COE_CR; i += (sizeof(u32)), j++) {
750 		retval = smsc95xx_read_reg(dev, i, &data[j]);
751 		if (retval < 0) {
752 			netdev_warn(netdev, "REGS: cannot read reg[%x]\n", i);
753 			return;
754 		}
755 	}
756 }
757 
758 static void smsc95xx_ethtool_get_wol(struct net_device *net,
759 				     struct ethtool_wolinfo *wolinfo)
760 {
761 	struct usbnet *dev = netdev_priv(net);
762 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
763 
764 	wolinfo->supported = SUPPORTED_WAKE;
765 	wolinfo->wolopts = pdata->wolopts;
766 }
767 
768 static int smsc95xx_ethtool_set_wol(struct net_device *net,
769 				    struct ethtool_wolinfo *wolinfo)
770 {
771 	struct usbnet *dev = netdev_priv(net);
772 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
773 	int ret;
774 
775 	if (wolinfo->wolopts & ~SUPPORTED_WAKE)
776 		return -EINVAL;
777 
778 	pdata->wolopts = wolinfo->wolopts & SUPPORTED_WAKE;
779 
780 	ret = device_set_wakeup_enable(&dev->udev->dev, pdata->wolopts);
781 	if (ret < 0)
782 		netdev_warn(dev->net, "device_set_wakeup_enable error %d\n", ret);
783 
784 	return ret;
785 }
786 
787 static int get_mdix_status(struct net_device *net)
788 {
789 	struct usbnet *dev = netdev_priv(net);
790 	u32 val;
791 	int buf;
792 
793 	buf = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, SPECIAL_CTRL_STS);
794 	if (buf & SPECIAL_CTRL_STS_OVRRD_AMDIX_) {
795 		if (buf & SPECIAL_CTRL_STS_AMDIX_ENABLE_)
796 			return ETH_TP_MDI_AUTO;
797 		else if (buf & SPECIAL_CTRL_STS_AMDIX_STATE_)
798 			return ETH_TP_MDI_X;
799 	} else {
800 		buf = smsc95xx_read_reg(dev, STRAP_STATUS, &val);
801 		if (val & STRAP_STATUS_AMDIX_EN_)
802 			return ETH_TP_MDI_AUTO;
803 	}
804 
805 	return ETH_TP_MDI;
806 }
807 
808 static void set_mdix_status(struct net_device *net, __u8 mdix_ctrl)
809 {
810 	struct usbnet *dev = netdev_priv(net);
811 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
812 	int buf;
813 
814 	if ((pdata->chip_id == ID_REV_CHIP_ID_9500A_) ||
815 	    (pdata->chip_id == ID_REV_CHIP_ID_9530_) ||
816 	    (pdata->chip_id == ID_REV_CHIP_ID_89530_) ||
817 	    (pdata->chip_id == ID_REV_CHIP_ID_9730_)) {
818 		/* Extend Manual AutoMDIX timer for 9500A/9500Ai */
819 		buf = smsc95xx_mdio_read(dev->net, dev->mii.phy_id,
820 					 PHY_EDPD_CONFIG);
821 		buf |= PHY_EDPD_CONFIG_EXT_CROSSOVER_;
822 		smsc95xx_mdio_write(dev->net, dev->mii.phy_id,
823 				    PHY_EDPD_CONFIG, buf);
824 	}
825 
826 	if (mdix_ctrl == ETH_TP_MDI) {
827 		buf = smsc95xx_mdio_read(dev->net, dev->mii.phy_id,
828 					 SPECIAL_CTRL_STS);
829 		buf |= SPECIAL_CTRL_STS_OVRRD_AMDIX_;
830 		buf &= ~(SPECIAL_CTRL_STS_AMDIX_ENABLE_ |
831 			 SPECIAL_CTRL_STS_AMDIX_STATE_);
832 		smsc95xx_mdio_write(dev->net, dev->mii.phy_id,
833 				    SPECIAL_CTRL_STS, buf);
834 	} else if (mdix_ctrl == ETH_TP_MDI_X) {
835 		buf = smsc95xx_mdio_read(dev->net, dev->mii.phy_id,
836 					 SPECIAL_CTRL_STS);
837 		buf |= SPECIAL_CTRL_STS_OVRRD_AMDIX_;
838 		buf &= ~(SPECIAL_CTRL_STS_AMDIX_ENABLE_ |
839 			 SPECIAL_CTRL_STS_AMDIX_STATE_);
840 		buf |= SPECIAL_CTRL_STS_AMDIX_STATE_;
841 		smsc95xx_mdio_write(dev->net, dev->mii.phy_id,
842 				    SPECIAL_CTRL_STS, buf);
843 	} else if (mdix_ctrl == ETH_TP_MDI_AUTO) {
844 		buf = smsc95xx_mdio_read(dev->net, dev->mii.phy_id,
845 					 SPECIAL_CTRL_STS);
846 		buf &= ~SPECIAL_CTRL_STS_OVRRD_AMDIX_;
847 		buf &= ~(SPECIAL_CTRL_STS_AMDIX_ENABLE_ |
848 			 SPECIAL_CTRL_STS_AMDIX_STATE_);
849 		buf |= SPECIAL_CTRL_STS_AMDIX_ENABLE_;
850 		smsc95xx_mdio_write(dev->net, dev->mii.phy_id,
851 				    SPECIAL_CTRL_STS, buf);
852 	}
853 	pdata->mdix_ctrl = mdix_ctrl;
854 }
855 
856 static int smsc95xx_get_link_ksettings(struct net_device *net,
857 				       struct ethtool_link_ksettings *cmd)
858 {
859 	struct usbnet *dev = netdev_priv(net);
860 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
861 	int retval;
862 
863 	retval = usbnet_get_link_ksettings(net, cmd);
864 
865 	cmd->base.eth_tp_mdix = pdata->mdix_ctrl;
866 	cmd->base.eth_tp_mdix_ctrl = pdata->mdix_ctrl;
867 
868 	return retval;
869 }
870 
871 static int smsc95xx_set_link_ksettings(struct net_device *net,
872 				       const struct ethtool_link_ksettings *cmd)
873 {
874 	struct usbnet *dev = netdev_priv(net);
875 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
876 	int retval;
877 
878 	if (pdata->mdix_ctrl != cmd->base.eth_tp_mdix_ctrl)
879 		set_mdix_status(net, cmd->base.eth_tp_mdix_ctrl);
880 
881 	retval = usbnet_set_link_ksettings(net, cmd);
882 
883 	return retval;
884 }
885 
886 static const struct ethtool_ops smsc95xx_ethtool_ops = {
887 	.get_link	= usbnet_get_link,
888 	.nway_reset	= usbnet_nway_reset,
889 	.get_drvinfo	= usbnet_get_drvinfo,
890 	.get_msglevel	= usbnet_get_msglevel,
891 	.set_msglevel	= usbnet_set_msglevel,
892 	.get_eeprom_len	= smsc95xx_ethtool_get_eeprom_len,
893 	.get_eeprom	= smsc95xx_ethtool_get_eeprom,
894 	.set_eeprom	= smsc95xx_ethtool_set_eeprom,
895 	.get_regs_len	= smsc95xx_ethtool_getregslen,
896 	.get_regs	= smsc95xx_ethtool_getregs,
897 	.get_wol	= smsc95xx_ethtool_get_wol,
898 	.set_wol	= smsc95xx_ethtool_set_wol,
899 	.get_link_ksettings	= smsc95xx_get_link_ksettings,
900 	.set_link_ksettings	= smsc95xx_set_link_ksettings,
901 	.get_ts_info	= ethtool_op_get_ts_info,
902 };
903 
904 static int smsc95xx_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
905 {
906 	struct usbnet *dev = netdev_priv(netdev);
907 
908 	if (!netif_running(netdev))
909 		return -EINVAL;
910 
911 	return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
912 }
913 
914 static void smsc95xx_init_mac_address(struct usbnet *dev)
915 {
916 	const u8 *mac_addr;
917 
918 	/* maybe the boot loader passed the MAC address in devicetree */
919 	mac_addr = of_get_mac_address(dev->udev->dev.of_node);
920 	if (!IS_ERR(mac_addr)) {
921 		ether_addr_copy(dev->net->dev_addr, mac_addr);
922 		return;
923 	}
924 
925 	/* try reading mac address from EEPROM */
926 	if (smsc95xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN,
927 			dev->net->dev_addr) == 0) {
928 		if (is_valid_ether_addr(dev->net->dev_addr)) {
929 			/* eeprom values are valid so use them */
930 			netif_dbg(dev, ifup, dev->net, "MAC address read from EEPROM\n");
931 			return;
932 		}
933 	}
934 
935 	/* no useful static MAC address found. generate a random one */
936 	eth_hw_addr_random(dev->net);
937 	netif_dbg(dev, ifup, dev->net, "MAC address set to eth_random_addr\n");
938 }
939 
940 static int smsc95xx_set_mac_address(struct usbnet *dev)
941 {
942 	u32 addr_lo = dev->net->dev_addr[0] | dev->net->dev_addr[1] << 8 |
943 		dev->net->dev_addr[2] << 16 | dev->net->dev_addr[3] << 24;
944 	u32 addr_hi = dev->net->dev_addr[4] | dev->net->dev_addr[5] << 8;
945 	int ret;
946 
947 	ret = smsc95xx_write_reg(dev, ADDRL, addr_lo);
948 	if (ret < 0)
949 		return ret;
950 
951 	return smsc95xx_write_reg(dev, ADDRH, addr_hi);
952 }
953 
954 /* starts the TX path */
955 static int smsc95xx_start_tx_path(struct usbnet *dev)
956 {
957 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
958 	unsigned long flags;
959 	int ret;
960 
961 	/* Enable Tx at MAC */
962 	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
963 	pdata->mac_cr |= MAC_CR_TXEN_;
964 	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
965 
966 	ret = smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr);
967 	if (ret < 0)
968 		return ret;
969 
970 	/* Enable Tx at SCSRs */
971 	return smsc95xx_write_reg(dev, TX_CFG, TX_CFG_ON_);
972 }
973 
974 /* Starts the Receive path */
975 static int smsc95xx_start_rx_path(struct usbnet *dev, int in_pm)
976 {
977 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
978 	unsigned long flags;
979 
980 	spin_lock_irqsave(&pdata->mac_cr_lock, flags);
981 	pdata->mac_cr |= MAC_CR_RXEN_;
982 	spin_unlock_irqrestore(&pdata->mac_cr_lock, flags);
983 
984 	return __smsc95xx_write_reg(dev, MAC_CR, pdata->mac_cr, in_pm);
985 }
986 
987 static int smsc95xx_phy_initialize(struct usbnet *dev)
988 {
989 	int bmcr, ret, timeout = 0;
990 
991 	/* Initialize MII structure */
992 	dev->mii.dev = dev->net;
993 	dev->mii.mdio_read = smsc95xx_mdio_read;
994 	dev->mii.mdio_write = smsc95xx_mdio_write;
995 	dev->mii.phy_id_mask = 0x1f;
996 	dev->mii.reg_num_mask = 0x1f;
997 	dev->mii.phy_id = SMSC95XX_INTERNAL_PHY_ID;
998 
999 	/* reset phy and wait for reset to complete */
1000 	smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
1001 
1002 	do {
1003 		msleep(10);
1004 		bmcr = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, MII_BMCR);
1005 		timeout++;
1006 	} while ((bmcr & BMCR_RESET) && (timeout < 100));
1007 
1008 	if (timeout >= 100) {
1009 		netdev_warn(dev->net, "timeout on PHY Reset");
1010 		return -EIO;
1011 	}
1012 
1013 	smsc95xx_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
1014 		ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP |
1015 		ADVERTISE_PAUSE_ASYM);
1016 
1017 	/* read to clear */
1018 	ret = smsc95xx_mdio_read(dev->net, dev->mii.phy_id, PHY_INT_SRC);
1019 	if (ret < 0) {
1020 		netdev_warn(dev->net, "Failed to read PHY_INT_SRC during init\n");
1021 		return ret;
1022 	}
1023 
1024 	smsc95xx_mdio_write(dev->net, dev->mii.phy_id, PHY_INT_MASK,
1025 		PHY_INT_MASK_DEFAULT_);
1026 	mii_nway_restart(&dev->mii);
1027 
1028 	netif_dbg(dev, ifup, dev->net, "phy initialised successfully\n");
1029 	return 0;
1030 }
1031 
1032 static int smsc95xx_reset(struct usbnet *dev)
1033 {
1034 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1035 	u32 read_buf, write_buf, burst_cap;
1036 	int ret = 0, timeout;
1037 
1038 	netif_dbg(dev, ifup, dev->net, "entering smsc95xx_reset\n");
1039 
1040 	ret = smsc95xx_write_reg(dev, HW_CFG, HW_CFG_LRST_);
1041 	if (ret < 0)
1042 		return ret;
1043 
1044 	timeout = 0;
1045 	do {
1046 		msleep(10);
1047 		ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
1048 		if (ret < 0)
1049 			return ret;
1050 		timeout++;
1051 	} while ((read_buf & HW_CFG_LRST_) && (timeout < 100));
1052 
1053 	if (timeout >= 100) {
1054 		netdev_warn(dev->net, "timeout waiting for completion of Lite Reset\n");
1055 		return ret;
1056 	}
1057 
1058 	ret = smsc95xx_write_reg(dev, PM_CTRL, PM_CTL_PHY_RST_);
1059 	if (ret < 0)
1060 		return ret;
1061 
1062 	timeout = 0;
1063 	do {
1064 		msleep(10);
1065 		ret = smsc95xx_read_reg(dev, PM_CTRL, &read_buf);
1066 		if (ret < 0)
1067 			return ret;
1068 		timeout++;
1069 	} while ((read_buf & PM_CTL_PHY_RST_) && (timeout < 100));
1070 
1071 	if (timeout >= 100) {
1072 		netdev_warn(dev->net, "timeout waiting for PHY Reset\n");
1073 		return ret;
1074 	}
1075 
1076 	ret = smsc95xx_set_mac_address(dev);
1077 	if (ret < 0)
1078 		return ret;
1079 
1080 	netif_dbg(dev, ifup, dev->net, "MAC Address: %pM\n",
1081 		  dev->net->dev_addr);
1082 
1083 	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
1084 	if (ret < 0)
1085 		return ret;
1086 
1087 	netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG : 0x%08x\n",
1088 		  read_buf);
1089 
1090 	read_buf |= HW_CFG_BIR_;
1091 
1092 	ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
1093 	if (ret < 0)
1094 		return ret;
1095 
1096 	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
1097 	if (ret < 0)
1098 		return ret;
1099 
1100 	netif_dbg(dev, ifup, dev->net,
1101 		  "Read Value from HW_CFG after writing HW_CFG_BIR_: 0x%08x\n",
1102 		  read_buf);
1103 
1104 	if (!turbo_mode) {
1105 		burst_cap = 0;
1106 		dev->rx_urb_size = MAX_SINGLE_PACKET_SIZE;
1107 	} else if (dev->udev->speed == USB_SPEED_HIGH) {
1108 		burst_cap = DEFAULT_HS_BURST_CAP_SIZE / HS_USB_PKT_SIZE;
1109 		dev->rx_urb_size = DEFAULT_HS_BURST_CAP_SIZE;
1110 	} else {
1111 		burst_cap = DEFAULT_FS_BURST_CAP_SIZE / FS_USB_PKT_SIZE;
1112 		dev->rx_urb_size = DEFAULT_FS_BURST_CAP_SIZE;
1113 	}
1114 
1115 	netif_dbg(dev, ifup, dev->net, "rx_urb_size=%ld\n",
1116 		  (ulong)dev->rx_urb_size);
1117 
1118 	ret = smsc95xx_write_reg(dev, BURST_CAP, burst_cap);
1119 	if (ret < 0)
1120 		return ret;
1121 
1122 	ret = smsc95xx_read_reg(dev, BURST_CAP, &read_buf);
1123 	if (ret < 0)
1124 		return ret;
1125 
1126 	netif_dbg(dev, ifup, dev->net,
1127 		  "Read Value from BURST_CAP after writing: 0x%08x\n",
1128 		  read_buf);
1129 
1130 	ret = smsc95xx_write_reg(dev, BULK_IN_DLY, DEFAULT_BULK_IN_DELAY);
1131 	if (ret < 0)
1132 		return ret;
1133 
1134 	ret = smsc95xx_read_reg(dev, BULK_IN_DLY, &read_buf);
1135 	if (ret < 0)
1136 		return ret;
1137 
1138 	netif_dbg(dev, ifup, dev->net,
1139 		  "Read Value from BULK_IN_DLY after writing: 0x%08x\n",
1140 		  read_buf);
1141 
1142 	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
1143 	if (ret < 0)
1144 		return ret;
1145 
1146 	netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG: 0x%08x\n",
1147 		  read_buf);
1148 
1149 	if (turbo_mode)
1150 		read_buf |= (HW_CFG_MEF_ | HW_CFG_BCE_);
1151 
1152 	read_buf &= ~HW_CFG_RXDOFF_;
1153 
1154 	/* set Rx data offset=2, Make IP header aligns on word boundary. */
1155 	read_buf |= NET_IP_ALIGN << 9;
1156 
1157 	ret = smsc95xx_write_reg(dev, HW_CFG, read_buf);
1158 	if (ret < 0)
1159 		return ret;
1160 
1161 	ret = smsc95xx_read_reg(dev, HW_CFG, &read_buf);
1162 	if (ret < 0)
1163 		return ret;
1164 
1165 	netif_dbg(dev, ifup, dev->net,
1166 		  "Read Value from HW_CFG after writing: 0x%08x\n", read_buf);
1167 
1168 	ret = smsc95xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
1169 	if (ret < 0)
1170 		return ret;
1171 
1172 	ret = smsc95xx_read_reg(dev, ID_REV, &read_buf);
1173 	if (ret < 0)
1174 		return ret;
1175 	netif_dbg(dev, ifup, dev->net, "ID_REV = 0x%08x\n", read_buf);
1176 
1177 	/* Configure GPIO pins as LED outputs */
1178 	write_buf = LED_GPIO_CFG_SPD_LED | LED_GPIO_CFG_LNK_LED |
1179 		LED_GPIO_CFG_FDX_LED;
1180 	ret = smsc95xx_write_reg(dev, LED_GPIO_CFG, write_buf);
1181 	if (ret < 0)
1182 		return ret;
1183 
1184 	/* Init Tx */
1185 	ret = smsc95xx_write_reg(dev, FLOW, 0);
1186 	if (ret < 0)
1187 		return ret;
1188 
1189 	ret = smsc95xx_write_reg(dev, AFC_CFG, AFC_CFG_DEFAULT);
1190 	if (ret < 0)
1191 		return ret;
1192 
1193 	/* Don't need mac_cr_lock during initialisation */
1194 	ret = smsc95xx_read_reg(dev, MAC_CR, &pdata->mac_cr);
1195 	if (ret < 0)
1196 		return ret;
1197 
1198 	/* Init Rx */
1199 	/* Set Vlan */
1200 	ret = smsc95xx_write_reg(dev, VLAN1, (u32)ETH_P_8021Q);
1201 	if (ret < 0)
1202 		return ret;
1203 
1204 	/* Enable or disable checksum offload engines */
1205 	ret = smsc95xx_set_features(dev->net, dev->net->features);
1206 	if (ret < 0) {
1207 		netdev_warn(dev->net, "Failed to set checksum offload features\n");
1208 		return ret;
1209 	}
1210 
1211 	smsc95xx_set_multicast(dev->net);
1212 
1213 	ret = smsc95xx_phy_initialize(dev);
1214 	if (ret < 0) {
1215 		netdev_warn(dev->net, "Failed to init PHY\n");
1216 		return ret;
1217 	}
1218 
1219 	ret = smsc95xx_read_reg(dev, INT_EP_CTL, &read_buf);
1220 	if (ret < 0)
1221 		return ret;
1222 
1223 	/* enable PHY interrupts */
1224 	read_buf |= INT_EP_CTL_PHY_INT_;
1225 
1226 	ret = smsc95xx_write_reg(dev, INT_EP_CTL, read_buf);
1227 	if (ret < 0)
1228 		return ret;
1229 
1230 	ret = smsc95xx_start_tx_path(dev);
1231 	if (ret < 0) {
1232 		netdev_warn(dev->net, "Failed to start TX path\n");
1233 		return ret;
1234 	}
1235 
1236 	ret = smsc95xx_start_rx_path(dev, 0);
1237 	if (ret < 0) {
1238 		netdev_warn(dev->net, "Failed to start RX path\n");
1239 		return ret;
1240 	}
1241 
1242 	netif_dbg(dev, ifup, dev->net, "smsc95xx_reset, return 0\n");
1243 	return 0;
1244 }
1245 
1246 static const struct net_device_ops smsc95xx_netdev_ops = {
1247 	.ndo_open		= usbnet_open,
1248 	.ndo_stop		= usbnet_stop,
1249 	.ndo_start_xmit		= usbnet_start_xmit,
1250 	.ndo_tx_timeout		= usbnet_tx_timeout,
1251 	.ndo_change_mtu		= usbnet_change_mtu,
1252 	.ndo_get_stats64	= usbnet_get_stats64,
1253 	.ndo_set_mac_address 	= eth_mac_addr,
1254 	.ndo_validate_addr	= eth_validate_addr,
1255 	.ndo_do_ioctl 		= smsc95xx_ioctl,
1256 	.ndo_set_rx_mode	= smsc95xx_set_multicast,
1257 	.ndo_set_features	= smsc95xx_set_features,
1258 };
1259 
1260 static int smsc95xx_bind(struct usbnet *dev, struct usb_interface *intf)
1261 {
1262 	struct smsc95xx_priv *pdata = NULL;
1263 	u32 val;
1264 	int ret;
1265 
1266 	printk(KERN_INFO SMSC_CHIPNAME " v" SMSC_DRIVER_VERSION "\n");
1267 
1268 	ret = usbnet_get_endpoints(dev, intf);
1269 	if (ret < 0) {
1270 		netdev_warn(dev->net, "usbnet_get_endpoints failed: %d\n", ret);
1271 		return ret;
1272 	}
1273 
1274 	dev->data[0] = (unsigned long)kzalloc(sizeof(struct smsc95xx_priv),
1275 					      GFP_KERNEL);
1276 
1277 	pdata = (struct smsc95xx_priv *)(dev->data[0]);
1278 	if (!pdata)
1279 		return -ENOMEM;
1280 
1281 	spin_lock_init(&pdata->mac_cr_lock);
1282 
1283 	/* LAN95xx devices do not alter the computed checksum of 0 to 0xffff.
1284 	 * RFC 2460, ipv6 UDP calculated checksum yields a result of zero must
1285 	 * be changed to 0xffff. RFC 768, ipv4 UDP computed checksum is zero,
1286 	 * it is transmitted as all ones. The zero transmitted checksum means
1287 	 * transmitter generated no checksum. Hence, enable csum offload only
1288 	 * for ipv4 packets.
1289 	 */
1290 	if (DEFAULT_TX_CSUM_ENABLE)
1291 		dev->net->features |= NETIF_F_IP_CSUM;
1292 	if (DEFAULT_RX_CSUM_ENABLE)
1293 		dev->net->features |= NETIF_F_RXCSUM;
1294 
1295 	dev->net->hw_features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
1296 	set_bit(EVENT_NO_IP_ALIGN, &dev->flags);
1297 
1298 	smsc95xx_init_mac_address(dev);
1299 
1300 	/* Init all registers */
1301 	ret = smsc95xx_reset(dev);
1302 
1303 	/* detect device revision as different features may be available */
1304 	ret = smsc95xx_read_reg(dev, ID_REV, &val);
1305 	if (ret < 0)
1306 		return ret;
1307 	val >>= 16;
1308 	pdata->chip_id = val;
1309 	pdata->mdix_ctrl = get_mdix_status(dev->net);
1310 
1311 	if ((val == ID_REV_CHIP_ID_9500A_) || (val == ID_REV_CHIP_ID_9530_) ||
1312 	    (val == ID_REV_CHIP_ID_89530_) || (val == ID_REV_CHIP_ID_9730_))
1313 		pdata->features = (FEATURE_8_WAKEUP_FILTERS |
1314 			FEATURE_PHY_NLP_CROSSOVER |
1315 			FEATURE_REMOTE_WAKEUP);
1316 	else if (val == ID_REV_CHIP_ID_9512_)
1317 		pdata->features = FEATURE_8_WAKEUP_FILTERS;
1318 
1319 	dev->net->netdev_ops = &smsc95xx_netdev_ops;
1320 	dev->net->ethtool_ops = &smsc95xx_ethtool_ops;
1321 	dev->net->flags |= IFF_MULTICAST;
1322 	dev->net->hard_header_len += SMSC95XX_TX_OVERHEAD_CSUM;
1323 	dev->net->min_mtu = ETH_MIN_MTU;
1324 	dev->net->max_mtu = ETH_DATA_LEN;
1325 	dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
1326 
1327 	pdata->dev = dev;
1328 	INIT_DELAYED_WORK(&pdata->carrier_check, check_carrier);
1329 	schedule_delayed_work(&pdata->carrier_check, CARRIER_CHECK_DELAY);
1330 
1331 	return 0;
1332 }
1333 
1334 static void smsc95xx_unbind(struct usbnet *dev, struct usb_interface *intf)
1335 {
1336 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1337 
1338 	if (pdata) {
1339 		cancel_delayed_work(&pdata->carrier_check);
1340 		netif_dbg(dev, ifdown, dev->net, "free pdata\n");
1341 		kfree(pdata);
1342 		pdata = NULL;
1343 		dev->data[0] = 0;
1344 	}
1345 }
1346 
1347 static u32 smsc_crc(const u8 *buffer, size_t len, int filter)
1348 {
1349 	u32 crc = bitrev16(crc16(0xFFFF, buffer, len));
1350 	return crc << ((filter % 2) * 16);
1351 }
1352 
1353 static int smsc95xx_enable_phy_wakeup_interrupts(struct usbnet *dev, u16 mask)
1354 {
1355 	struct mii_if_info *mii = &dev->mii;
1356 	int ret;
1357 
1358 	netdev_dbg(dev->net, "enabling PHY wakeup interrupts\n");
1359 
1360 	/* read to clear */
1361 	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_INT_SRC);
1362 	if (ret < 0)
1363 		return ret;
1364 
1365 	/* enable interrupt source */
1366 	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_INT_MASK);
1367 	if (ret < 0)
1368 		return ret;
1369 
1370 	ret |= mask;
1371 
1372 	smsc95xx_mdio_write_nopm(dev->net, mii->phy_id, PHY_INT_MASK, ret);
1373 
1374 	return 0;
1375 }
1376 
1377 static int smsc95xx_link_ok_nopm(struct usbnet *dev)
1378 {
1379 	struct mii_if_info *mii = &dev->mii;
1380 	int ret;
1381 
1382 	/* first, a dummy read, needed to latch some MII phys */
1383 	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, MII_BMSR);
1384 	if (ret < 0)
1385 		return ret;
1386 
1387 	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, MII_BMSR);
1388 	if (ret < 0)
1389 		return ret;
1390 
1391 	return !!(ret & BMSR_LSTATUS);
1392 }
1393 
1394 static int smsc95xx_enter_suspend0(struct usbnet *dev)
1395 {
1396 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1397 	u32 val;
1398 	int ret;
1399 
1400 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1401 	if (ret < 0)
1402 		return ret;
1403 
1404 	val &= (~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_));
1405 	val |= PM_CTL_SUS_MODE_0;
1406 
1407 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1408 	if (ret < 0)
1409 		return ret;
1410 
1411 	/* clear wol status */
1412 	val &= ~PM_CTL_WUPS_;
1413 	val |= PM_CTL_WUPS_WOL_;
1414 
1415 	/* enable energy detection */
1416 	if (pdata->wolopts & WAKE_PHY)
1417 		val |= PM_CTL_WUPS_ED_;
1418 
1419 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1420 	if (ret < 0)
1421 		return ret;
1422 
1423 	/* read back PM_CTRL */
1424 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1425 	if (ret < 0)
1426 		return ret;
1427 
1428 	pdata->suspend_flags |= SUSPEND_SUSPEND0;
1429 
1430 	return 0;
1431 }
1432 
1433 static int smsc95xx_enter_suspend1(struct usbnet *dev)
1434 {
1435 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1436 	struct mii_if_info *mii = &dev->mii;
1437 	u32 val;
1438 	int ret;
1439 
1440 	/* reconfigure link pulse detection timing for
1441 	 * compatibility with non-standard link partners
1442 	 */
1443 	if (pdata->features & FEATURE_PHY_NLP_CROSSOVER)
1444 		smsc95xx_mdio_write_nopm(dev->net, mii->phy_id,	PHY_EDPD_CONFIG,
1445 			PHY_EDPD_CONFIG_DEFAULT);
1446 
1447 	/* enable energy detect power-down mode */
1448 	ret = smsc95xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_MODE_CTRL_STS);
1449 	if (ret < 0)
1450 		return ret;
1451 
1452 	ret |= MODE_CTRL_STS_EDPWRDOWN_;
1453 
1454 	smsc95xx_mdio_write_nopm(dev->net, mii->phy_id, PHY_MODE_CTRL_STS, ret);
1455 
1456 	/* enter SUSPEND1 mode */
1457 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1458 	if (ret < 0)
1459 		return ret;
1460 
1461 	val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1462 	val |= PM_CTL_SUS_MODE_1;
1463 
1464 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1465 	if (ret < 0)
1466 		return ret;
1467 
1468 	/* clear wol status, enable energy detection */
1469 	val &= ~PM_CTL_WUPS_;
1470 	val |= (PM_CTL_WUPS_ED_ | PM_CTL_ED_EN_);
1471 
1472 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1473 	if (ret < 0)
1474 		return ret;
1475 
1476 	pdata->suspend_flags |= SUSPEND_SUSPEND1;
1477 
1478 	return 0;
1479 }
1480 
1481 static int smsc95xx_enter_suspend2(struct usbnet *dev)
1482 {
1483 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1484 	u32 val;
1485 	int ret;
1486 
1487 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1488 	if (ret < 0)
1489 		return ret;
1490 
1491 	val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1492 	val |= PM_CTL_SUS_MODE_2;
1493 
1494 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1495 	if (ret < 0)
1496 		return ret;
1497 
1498 	pdata->suspend_flags |= SUSPEND_SUSPEND2;
1499 
1500 	return 0;
1501 }
1502 
1503 static int smsc95xx_enter_suspend3(struct usbnet *dev)
1504 {
1505 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1506 	u32 val;
1507 	int ret;
1508 
1509 	ret = smsc95xx_read_reg_nopm(dev, RX_FIFO_INF, &val);
1510 	if (ret < 0)
1511 		return ret;
1512 
1513 	if (val & RX_FIFO_INF_USED_) {
1514 		netdev_info(dev->net, "rx fifo not empty in autosuspend\n");
1515 		return -EBUSY;
1516 	}
1517 
1518 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1519 	if (ret < 0)
1520 		return ret;
1521 
1522 	val &= ~(PM_CTL_SUS_MODE_ | PM_CTL_WUPS_ | PM_CTL_PHY_RST_);
1523 	val |= PM_CTL_SUS_MODE_3 | PM_CTL_RES_CLR_WKP_STS;
1524 
1525 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1526 	if (ret < 0)
1527 		return ret;
1528 
1529 	/* clear wol status */
1530 	val &= ~PM_CTL_WUPS_;
1531 	val |= PM_CTL_WUPS_WOL_;
1532 
1533 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1534 	if (ret < 0)
1535 		return ret;
1536 
1537 	pdata->suspend_flags |= SUSPEND_SUSPEND3;
1538 
1539 	return 0;
1540 }
1541 
1542 static int smsc95xx_autosuspend(struct usbnet *dev, u32 link_up)
1543 {
1544 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1545 	int ret;
1546 
1547 	if (!netif_running(dev->net)) {
1548 		/* interface is ifconfig down so fully power down hw */
1549 		netdev_dbg(dev->net, "autosuspend entering SUSPEND2\n");
1550 		return smsc95xx_enter_suspend2(dev);
1551 	}
1552 
1553 	if (!link_up) {
1554 		/* link is down so enter EDPD mode, but only if device can
1555 		 * reliably resume from it.  This check should be redundant
1556 		 * as current FEATURE_REMOTE_WAKEUP parts also support
1557 		 * FEATURE_PHY_NLP_CROSSOVER but it's included for clarity */
1558 		if (!(pdata->features & FEATURE_PHY_NLP_CROSSOVER)) {
1559 			netdev_warn(dev->net, "EDPD not supported\n");
1560 			return -EBUSY;
1561 		}
1562 
1563 		netdev_dbg(dev->net, "autosuspend entering SUSPEND1\n");
1564 
1565 		/* enable PHY wakeup events for if cable is attached */
1566 		ret = smsc95xx_enable_phy_wakeup_interrupts(dev,
1567 			PHY_INT_MASK_ANEG_COMP_);
1568 		if (ret < 0) {
1569 			netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1570 			return ret;
1571 		}
1572 
1573 		netdev_info(dev->net, "entering SUSPEND1 mode\n");
1574 		return smsc95xx_enter_suspend1(dev);
1575 	}
1576 
1577 	/* enable PHY wakeup events so we remote wakeup if cable is pulled */
1578 	ret = smsc95xx_enable_phy_wakeup_interrupts(dev,
1579 		PHY_INT_MASK_LINK_DOWN_);
1580 	if (ret < 0) {
1581 		netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1582 		return ret;
1583 	}
1584 
1585 	netdev_dbg(dev->net, "autosuspend entering SUSPEND3\n");
1586 	return smsc95xx_enter_suspend3(dev);
1587 }
1588 
1589 static int smsc95xx_suspend(struct usb_interface *intf, pm_message_t message)
1590 {
1591 	struct usbnet *dev = usb_get_intfdata(intf);
1592 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
1593 	u32 val, link_up;
1594 	int ret;
1595 
1596 	ret = usbnet_suspend(intf, message);
1597 	if (ret < 0) {
1598 		netdev_warn(dev->net, "usbnet_suspend error\n");
1599 		return ret;
1600 	}
1601 
1602 	cancel_delayed_work_sync(&pdata->carrier_check);
1603 
1604 	if (pdata->suspend_flags) {
1605 		netdev_warn(dev->net, "error during last resume\n");
1606 		pdata->suspend_flags = 0;
1607 	}
1608 
1609 	/* determine if link is up using only _nopm functions */
1610 	link_up = smsc95xx_link_ok_nopm(dev);
1611 
1612 	if (message.event == PM_EVENT_AUTO_SUSPEND &&
1613 	    (pdata->features & FEATURE_REMOTE_WAKEUP)) {
1614 		ret = smsc95xx_autosuspend(dev, link_up);
1615 		goto done;
1616 	}
1617 
1618 	/* if we get this far we're not autosuspending */
1619 	/* if no wol options set, or if link is down and we're not waking on
1620 	 * PHY activity, enter lowest power SUSPEND2 mode
1621 	 */
1622 	if (!(pdata->wolopts & SUPPORTED_WAKE) ||
1623 		!(link_up || (pdata->wolopts & WAKE_PHY))) {
1624 		netdev_info(dev->net, "entering SUSPEND2 mode\n");
1625 
1626 		/* disable energy detect (link up) & wake up events */
1627 		ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1628 		if (ret < 0)
1629 			goto done;
1630 
1631 		val &= ~(WUCSR_MPEN_ | WUCSR_WAKE_EN_);
1632 
1633 		ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1634 		if (ret < 0)
1635 			goto done;
1636 
1637 		ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1638 		if (ret < 0)
1639 			goto done;
1640 
1641 		val &= ~(PM_CTL_ED_EN_ | PM_CTL_WOL_EN_);
1642 
1643 		ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1644 		if (ret < 0)
1645 			goto done;
1646 
1647 		ret = smsc95xx_enter_suspend2(dev);
1648 		goto done;
1649 	}
1650 
1651 	if (pdata->wolopts & WAKE_PHY) {
1652 		ret = smsc95xx_enable_phy_wakeup_interrupts(dev,
1653 			(PHY_INT_MASK_ANEG_COMP_ | PHY_INT_MASK_LINK_DOWN_));
1654 		if (ret < 0) {
1655 			netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1656 			goto done;
1657 		}
1658 
1659 		/* if link is down then configure EDPD and enter SUSPEND1,
1660 		 * otherwise enter SUSPEND0 below
1661 		 */
1662 		if (!link_up) {
1663 			netdev_info(dev->net, "entering SUSPEND1 mode\n");
1664 			ret = smsc95xx_enter_suspend1(dev);
1665 			goto done;
1666 		}
1667 	}
1668 
1669 	if (pdata->wolopts & (WAKE_BCAST | WAKE_MCAST | WAKE_ARP | WAKE_UCAST)) {
1670 		u32 *filter_mask = kcalloc(32, sizeof(u32), GFP_KERNEL);
1671 		u32 command[2];
1672 		u32 offset[2];
1673 		u32 crc[4];
1674 		int wuff_filter_count =
1675 			(pdata->features & FEATURE_8_WAKEUP_FILTERS) ?
1676 			LAN9500A_WUFF_NUM : LAN9500_WUFF_NUM;
1677 		int i, filter = 0;
1678 
1679 		if (!filter_mask) {
1680 			netdev_warn(dev->net, "Unable to allocate filter_mask\n");
1681 			ret = -ENOMEM;
1682 			goto done;
1683 		}
1684 
1685 		memset(command, 0, sizeof(command));
1686 		memset(offset, 0, sizeof(offset));
1687 		memset(crc, 0, sizeof(crc));
1688 
1689 		if (pdata->wolopts & WAKE_BCAST) {
1690 			const u8 bcast[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1691 			netdev_info(dev->net, "enabling broadcast detection\n");
1692 			filter_mask[filter * 4] = 0x003F;
1693 			filter_mask[filter * 4 + 1] = 0x00;
1694 			filter_mask[filter * 4 + 2] = 0x00;
1695 			filter_mask[filter * 4 + 3] = 0x00;
1696 			command[filter/4] |= 0x05UL << ((filter % 4) * 8);
1697 			offset[filter/4] |= 0x00 << ((filter % 4) * 8);
1698 			crc[filter/2] |= smsc_crc(bcast, 6, filter);
1699 			filter++;
1700 		}
1701 
1702 		if (pdata->wolopts & WAKE_MCAST) {
1703 			const u8 mcast[] = {0x01, 0x00, 0x5E};
1704 			netdev_info(dev->net, "enabling multicast detection\n");
1705 			filter_mask[filter * 4] = 0x0007;
1706 			filter_mask[filter * 4 + 1] = 0x00;
1707 			filter_mask[filter * 4 + 2] = 0x00;
1708 			filter_mask[filter * 4 + 3] = 0x00;
1709 			command[filter/4] |= 0x09UL << ((filter % 4) * 8);
1710 			offset[filter/4] |= 0x00  << ((filter % 4) * 8);
1711 			crc[filter/2] |= smsc_crc(mcast, 3, filter);
1712 			filter++;
1713 		}
1714 
1715 		if (pdata->wolopts & WAKE_ARP) {
1716 			const u8 arp[] = {0x08, 0x06};
1717 			netdev_info(dev->net, "enabling ARP detection\n");
1718 			filter_mask[filter * 4] = 0x0003;
1719 			filter_mask[filter * 4 + 1] = 0x00;
1720 			filter_mask[filter * 4 + 2] = 0x00;
1721 			filter_mask[filter * 4 + 3] = 0x00;
1722 			command[filter/4] |= 0x05UL << ((filter % 4) * 8);
1723 			offset[filter/4] |= 0x0C << ((filter % 4) * 8);
1724 			crc[filter/2] |= smsc_crc(arp, 2, filter);
1725 			filter++;
1726 		}
1727 
1728 		if (pdata->wolopts & WAKE_UCAST) {
1729 			netdev_info(dev->net, "enabling unicast detection\n");
1730 			filter_mask[filter * 4] = 0x003F;
1731 			filter_mask[filter * 4 + 1] = 0x00;
1732 			filter_mask[filter * 4 + 2] = 0x00;
1733 			filter_mask[filter * 4 + 3] = 0x00;
1734 			command[filter/4] |= 0x01UL << ((filter % 4) * 8);
1735 			offset[filter/4] |= 0x00 << ((filter % 4) * 8);
1736 			crc[filter/2] |= smsc_crc(dev->net->dev_addr, ETH_ALEN, filter);
1737 			filter++;
1738 		}
1739 
1740 		for (i = 0; i < (wuff_filter_count * 4); i++) {
1741 			ret = smsc95xx_write_reg_nopm(dev, WUFF, filter_mask[i]);
1742 			if (ret < 0) {
1743 				kfree(filter_mask);
1744 				goto done;
1745 			}
1746 		}
1747 		kfree(filter_mask);
1748 
1749 		for (i = 0; i < (wuff_filter_count / 4); i++) {
1750 			ret = smsc95xx_write_reg_nopm(dev, WUFF, command[i]);
1751 			if (ret < 0)
1752 				goto done;
1753 		}
1754 
1755 		for (i = 0; i < (wuff_filter_count / 4); i++) {
1756 			ret = smsc95xx_write_reg_nopm(dev, WUFF, offset[i]);
1757 			if (ret < 0)
1758 				goto done;
1759 		}
1760 
1761 		for (i = 0; i < (wuff_filter_count / 2); i++) {
1762 			ret = smsc95xx_write_reg_nopm(dev, WUFF, crc[i]);
1763 			if (ret < 0)
1764 				goto done;
1765 		}
1766 
1767 		/* clear any pending pattern match packet status */
1768 		ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1769 		if (ret < 0)
1770 			goto done;
1771 
1772 		val |= WUCSR_WUFR_;
1773 
1774 		ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1775 		if (ret < 0)
1776 			goto done;
1777 	}
1778 
1779 	if (pdata->wolopts & WAKE_MAGIC) {
1780 		/* clear any pending magic packet status */
1781 		ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1782 		if (ret < 0)
1783 			goto done;
1784 
1785 		val |= WUCSR_MPR_;
1786 
1787 		ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1788 		if (ret < 0)
1789 			goto done;
1790 	}
1791 
1792 	/* enable/disable wakeup sources */
1793 	ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1794 	if (ret < 0)
1795 		goto done;
1796 
1797 	if (pdata->wolopts & (WAKE_BCAST | WAKE_MCAST | WAKE_ARP | WAKE_UCAST)) {
1798 		netdev_info(dev->net, "enabling pattern match wakeup\n");
1799 		val |= WUCSR_WAKE_EN_;
1800 	} else {
1801 		netdev_info(dev->net, "disabling pattern match wakeup\n");
1802 		val &= ~WUCSR_WAKE_EN_;
1803 	}
1804 
1805 	if (pdata->wolopts & WAKE_MAGIC) {
1806 		netdev_info(dev->net, "enabling magic packet wakeup\n");
1807 		val |= WUCSR_MPEN_;
1808 	} else {
1809 		netdev_info(dev->net, "disabling magic packet wakeup\n");
1810 		val &= ~WUCSR_MPEN_;
1811 	}
1812 
1813 	ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1814 	if (ret < 0)
1815 		goto done;
1816 
1817 	/* enable wol wakeup source */
1818 	ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1819 	if (ret < 0)
1820 		goto done;
1821 
1822 	val |= PM_CTL_WOL_EN_;
1823 
1824 	/* phy energy detect wakeup source */
1825 	if (pdata->wolopts & WAKE_PHY)
1826 		val |= PM_CTL_ED_EN_;
1827 
1828 	ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1829 	if (ret < 0)
1830 		goto done;
1831 
1832 	/* enable receiver to enable frame reception */
1833 	smsc95xx_start_rx_path(dev, 1);
1834 
1835 	/* some wol options are enabled, so enter SUSPEND0 */
1836 	netdev_info(dev->net, "entering SUSPEND0 mode\n");
1837 	ret = smsc95xx_enter_suspend0(dev);
1838 
1839 done:
1840 	/*
1841 	 * TODO: resume() might need to handle the suspend failure
1842 	 * in system sleep
1843 	 */
1844 	if (ret && PMSG_IS_AUTO(message))
1845 		usbnet_resume(intf);
1846 
1847 	if (ret)
1848 		schedule_delayed_work(&pdata->carrier_check,
1849 				      CARRIER_CHECK_DELAY);
1850 
1851 	return ret;
1852 }
1853 
1854 static int smsc95xx_resume(struct usb_interface *intf)
1855 {
1856 	struct usbnet *dev = usb_get_intfdata(intf);
1857 	struct smsc95xx_priv *pdata;
1858 	u8 suspend_flags;
1859 	int ret;
1860 	u32 val;
1861 
1862 	BUG_ON(!dev);
1863 	pdata = (struct smsc95xx_priv *)(dev->data[0]);
1864 	suspend_flags = pdata->suspend_flags;
1865 
1866 	netdev_dbg(dev->net, "resume suspend_flags=0x%02x\n", suspend_flags);
1867 
1868 	/* do this first to ensure it's cleared even in error case */
1869 	pdata->suspend_flags = 0;
1870 	schedule_delayed_work(&pdata->carrier_check, CARRIER_CHECK_DELAY);
1871 
1872 	if (suspend_flags & SUSPEND_ALLMODES) {
1873 		/* clear wake-up sources */
1874 		ret = smsc95xx_read_reg_nopm(dev, WUCSR, &val);
1875 		if (ret < 0)
1876 			return ret;
1877 
1878 		val &= ~(WUCSR_WAKE_EN_ | WUCSR_MPEN_);
1879 
1880 		ret = smsc95xx_write_reg_nopm(dev, WUCSR, val);
1881 		if (ret < 0)
1882 			return ret;
1883 
1884 		/* clear wake-up status */
1885 		ret = smsc95xx_read_reg_nopm(dev, PM_CTRL, &val);
1886 		if (ret < 0)
1887 			return ret;
1888 
1889 		val &= ~PM_CTL_WOL_EN_;
1890 		val |= PM_CTL_WUPS_;
1891 
1892 		ret = smsc95xx_write_reg_nopm(dev, PM_CTRL, val);
1893 		if (ret < 0)
1894 			return ret;
1895 	}
1896 
1897 	ret = usbnet_resume(intf);
1898 	if (ret < 0)
1899 		netdev_warn(dev->net, "usbnet_resume error\n");
1900 
1901 	return ret;
1902 }
1903 
1904 static int smsc95xx_reset_resume(struct usb_interface *intf)
1905 {
1906 	struct usbnet *dev = usb_get_intfdata(intf);
1907 	int ret;
1908 
1909 	ret = smsc95xx_reset(dev);
1910 	if (ret < 0)
1911 		return ret;
1912 
1913 	return smsc95xx_resume(intf);
1914 }
1915 
1916 static void smsc95xx_rx_csum_offload(struct sk_buff *skb)
1917 {
1918 	skb->csum = *(u16 *)(skb_tail_pointer(skb) - 2);
1919 	skb->ip_summed = CHECKSUM_COMPLETE;
1920 	skb_trim(skb, skb->len - 2);
1921 }
1922 
1923 static int smsc95xx_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
1924 {
1925 	/* This check is no longer done by usbnet */
1926 	if (skb->len < dev->net->hard_header_len)
1927 		return 0;
1928 
1929 	while (skb->len > 0) {
1930 		u32 header, align_count;
1931 		struct sk_buff *ax_skb;
1932 		unsigned char *packet;
1933 		u16 size;
1934 
1935 		header = get_unaligned_le32(skb->data);
1936 		skb_pull(skb, 4 + NET_IP_ALIGN);
1937 		packet = skb->data;
1938 
1939 		/* get the packet length */
1940 		size = (u16)((header & RX_STS_FL_) >> 16);
1941 		align_count = (4 - ((size + NET_IP_ALIGN) % 4)) % 4;
1942 
1943 		if (unlikely(header & RX_STS_ES_)) {
1944 			netif_dbg(dev, rx_err, dev->net,
1945 				  "Error header=0x%08x\n", header);
1946 			dev->net->stats.rx_errors++;
1947 			dev->net->stats.rx_dropped++;
1948 
1949 			if (header & RX_STS_CRC_) {
1950 				dev->net->stats.rx_crc_errors++;
1951 			} else {
1952 				if (header & (RX_STS_TL_ | RX_STS_RF_))
1953 					dev->net->stats.rx_frame_errors++;
1954 
1955 				if ((header & RX_STS_LE_) &&
1956 					(!(header & RX_STS_FT_)))
1957 					dev->net->stats.rx_length_errors++;
1958 			}
1959 		} else {
1960 			/* ETH_FRAME_LEN + 4(CRC) + 2(COE) + 4(Vlan) */
1961 			if (unlikely(size > (ETH_FRAME_LEN + 12))) {
1962 				netif_dbg(dev, rx_err, dev->net,
1963 					  "size err header=0x%08x\n", header);
1964 				return 0;
1965 			}
1966 
1967 			/* last frame in this batch */
1968 			if (skb->len == size) {
1969 				if (dev->net->features & NETIF_F_RXCSUM)
1970 					smsc95xx_rx_csum_offload(skb);
1971 				skb_trim(skb, skb->len - 4); /* remove fcs */
1972 				skb->truesize = size + sizeof(struct sk_buff);
1973 
1974 				return 1;
1975 			}
1976 
1977 			ax_skb = skb_clone(skb, GFP_ATOMIC);
1978 			if (unlikely(!ax_skb)) {
1979 				netdev_warn(dev->net, "Error allocating skb\n");
1980 				return 0;
1981 			}
1982 
1983 			ax_skb->len = size;
1984 			ax_skb->data = packet;
1985 			skb_set_tail_pointer(ax_skb, size);
1986 
1987 			if (dev->net->features & NETIF_F_RXCSUM)
1988 				smsc95xx_rx_csum_offload(ax_skb);
1989 			skb_trim(ax_skb, ax_skb->len - 4); /* remove fcs */
1990 			ax_skb->truesize = size + sizeof(struct sk_buff);
1991 
1992 			usbnet_skb_return(dev, ax_skb);
1993 		}
1994 
1995 		skb_pull(skb, size);
1996 
1997 		/* padding bytes before the next frame starts */
1998 		if (skb->len)
1999 			skb_pull(skb, align_count);
2000 	}
2001 
2002 	return 1;
2003 }
2004 
2005 static u32 smsc95xx_calc_csum_preamble(struct sk_buff *skb)
2006 {
2007 	u16 low_16 = (u16)skb_checksum_start_offset(skb);
2008 	u16 high_16 = low_16 + skb->csum_offset;
2009 	return (high_16 << 16) | low_16;
2010 }
2011 
2012 /* The TX CSUM won't work if the checksum lies in the last 4 bytes of the
2013  * transmission. This is fairly unlikely, only seems to trigger with some
2014  * short TCP ACK packets sent.
2015  *
2016  * Note, this calculation should probably check for the alignment of the
2017  * data as well, but a straight check for csum being in the last four bytes
2018  * of the packet should be ok for now.
2019  */
2020 static bool smsc95xx_can_tx_checksum(struct sk_buff *skb)
2021 {
2022        unsigned int len = skb->len - skb_checksum_start_offset(skb);
2023 
2024        if (skb->len <= 45)
2025 	       return false;
2026        return skb->csum_offset < (len - (4 + 1));
2027 }
2028 
2029 static struct sk_buff *smsc95xx_tx_fixup(struct usbnet *dev,
2030 					 struct sk_buff *skb, gfp_t flags)
2031 {
2032 	bool csum = skb->ip_summed == CHECKSUM_PARTIAL;
2033 	int overhead = csum ? SMSC95XX_TX_OVERHEAD_CSUM : SMSC95XX_TX_OVERHEAD;
2034 	u32 tx_cmd_a, tx_cmd_b;
2035 	void *ptr;
2036 
2037 	/* We do not advertise SG, so skbs should be already linearized */
2038 	BUG_ON(skb_shinfo(skb)->nr_frags);
2039 
2040 	/* Make writable and expand header space by overhead if required */
2041 	if (skb_cow_head(skb, overhead)) {
2042 		/* Must deallocate here as returning NULL to indicate error
2043 		 * means the skb won't be deallocated in the caller.
2044 		 */
2045 		dev_kfree_skb_any(skb);
2046 		return NULL;
2047 	}
2048 
2049 	tx_cmd_b = (u32)skb->len;
2050 	tx_cmd_a = tx_cmd_b | TX_CMD_A_FIRST_SEG_ | TX_CMD_A_LAST_SEG_;
2051 
2052 	if (csum) {
2053 		if (!smsc95xx_can_tx_checksum(skb)) {
2054 			/* workaround - hardware tx checksum does not work
2055 			 * properly with extremely small packets */
2056 			long csstart = skb_checksum_start_offset(skb);
2057 			__wsum calc = csum_partial(skb->data + csstart,
2058 				skb->len - csstart, 0);
2059 			*((__sum16 *)(skb->data + csstart
2060 				+ skb->csum_offset)) = csum_fold(calc);
2061 
2062 			csum = false;
2063 		} else {
2064 			u32 csum_preamble = smsc95xx_calc_csum_preamble(skb);
2065 			ptr = skb_push(skb, 4);
2066 			put_unaligned_le32(csum_preamble, ptr);
2067 
2068 			tx_cmd_a += 4;
2069 			tx_cmd_b += 4;
2070 			tx_cmd_b |= TX_CMD_B_CSUM_ENABLE;
2071 		}
2072 	}
2073 
2074 	ptr = skb_push(skb, 8);
2075 	put_unaligned_le32(tx_cmd_a, ptr);
2076 	put_unaligned_le32(tx_cmd_b, ptr+4);
2077 
2078 	return skb;
2079 }
2080 
2081 static int smsc95xx_manage_power(struct usbnet *dev, int on)
2082 {
2083 	struct smsc95xx_priv *pdata = (struct smsc95xx_priv *)(dev->data[0]);
2084 
2085 	dev->intf->needs_remote_wakeup = on;
2086 
2087 	if (pdata->features & FEATURE_REMOTE_WAKEUP)
2088 		return 0;
2089 
2090 	/* this chip revision isn't capable of remote wakeup */
2091 	netdev_info(dev->net, "hardware isn't capable of remote wakeup\n");
2092 
2093 	if (on)
2094 		usb_autopm_get_interface_no_resume(dev->intf);
2095 	else
2096 		usb_autopm_put_interface(dev->intf);
2097 
2098 	return 0;
2099 }
2100 
2101 static const struct driver_info smsc95xx_info = {
2102 	.description	= "smsc95xx USB 2.0 Ethernet",
2103 	.bind		= smsc95xx_bind,
2104 	.unbind		= smsc95xx_unbind,
2105 	.link_reset	= smsc95xx_link_reset,
2106 	.reset		= smsc95xx_reset,
2107 	.rx_fixup	= smsc95xx_rx_fixup,
2108 	.tx_fixup	= smsc95xx_tx_fixup,
2109 	.status		= smsc95xx_status,
2110 	.manage_power	= smsc95xx_manage_power,
2111 	.flags		= FLAG_ETHER | FLAG_SEND_ZLP | FLAG_LINK_INTR,
2112 };
2113 
2114 static const struct usb_device_id products[] = {
2115 	{
2116 		/* SMSC9500 USB Ethernet Device */
2117 		USB_DEVICE(0x0424, 0x9500),
2118 		.driver_info = (unsigned long) &smsc95xx_info,
2119 	},
2120 	{
2121 		/* SMSC9505 USB Ethernet Device */
2122 		USB_DEVICE(0x0424, 0x9505),
2123 		.driver_info = (unsigned long) &smsc95xx_info,
2124 	},
2125 	{
2126 		/* SMSC9500A USB Ethernet Device */
2127 		USB_DEVICE(0x0424, 0x9E00),
2128 		.driver_info = (unsigned long) &smsc95xx_info,
2129 	},
2130 	{
2131 		/* SMSC9505A USB Ethernet Device */
2132 		USB_DEVICE(0x0424, 0x9E01),
2133 		.driver_info = (unsigned long) &smsc95xx_info,
2134 	},
2135 	{
2136 		/* SMSC9512/9514 USB Hub & Ethernet Device */
2137 		USB_DEVICE(0x0424, 0xec00),
2138 		.driver_info = (unsigned long) &smsc95xx_info,
2139 	},
2140 	{
2141 		/* SMSC9500 USB Ethernet Device (SAL10) */
2142 		USB_DEVICE(0x0424, 0x9900),
2143 		.driver_info = (unsigned long) &smsc95xx_info,
2144 	},
2145 	{
2146 		/* SMSC9505 USB Ethernet Device (SAL10) */
2147 		USB_DEVICE(0x0424, 0x9901),
2148 		.driver_info = (unsigned long) &smsc95xx_info,
2149 	},
2150 	{
2151 		/* SMSC9500A USB Ethernet Device (SAL10) */
2152 		USB_DEVICE(0x0424, 0x9902),
2153 		.driver_info = (unsigned long) &smsc95xx_info,
2154 	},
2155 	{
2156 		/* SMSC9505A USB Ethernet Device (SAL10) */
2157 		USB_DEVICE(0x0424, 0x9903),
2158 		.driver_info = (unsigned long) &smsc95xx_info,
2159 	},
2160 	{
2161 		/* SMSC9512/9514 USB Hub & Ethernet Device (SAL10) */
2162 		USB_DEVICE(0x0424, 0x9904),
2163 		.driver_info = (unsigned long) &smsc95xx_info,
2164 	},
2165 	{
2166 		/* SMSC9500A USB Ethernet Device (HAL) */
2167 		USB_DEVICE(0x0424, 0x9905),
2168 		.driver_info = (unsigned long) &smsc95xx_info,
2169 	},
2170 	{
2171 		/* SMSC9505A USB Ethernet Device (HAL) */
2172 		USB_DEVICE(0x0424, 0x9906),
2173 		.driver_info = (unsigned long) &smsc95xx_info,
2174 	},
2175 	{
2176 		/* SMSC9500 USB Ethernet Device (Alternate ID) */
2177 		USB_DEVICE(0x0424, 0x9907),
2178 		.driver_info = (unsigned long) &smsc95xx_info,
2179 	},
2180 	{
2181 		/* SMSC9500A USB Ethernet Device (Alternate ID) */
2182 		USB_DEVICE(0x0424, 0x9908),
2183 		.driver_info = (unsigned long) &smsc95xx_info,
2184 	},
2185 	{
2186 		/* SMSC9512/9514 USB Hub & Ethernet Device (Alternate ID) */
2187 		USB_DEVICE(0x0424, 0x9909),
2188 		.driver_info = (unsigned long) &smsc95xx_info,
2189 	},
2190 	{
2191 		/* SMSC LAN9530 USB Ethernet Device */
2192 		USB_DEVICE(0x0424, 0x9530),
2193 		.driver_info = (unsigned long) &smsc95xx_info,
2194 	},
2195 	{
2196 		/* SMSC LAN9730 USB Ethernet Device */
2197 		USB_DEVICE(0x0424, 0x9730),
2198 		.driver_info = (unsigned long) &smsc95xx_info,
2199 	},
2200 	{
2201 		/* SMSC LAN89530 USB Ethernet Device */
2202 		USB_DEVICE(0x0424, 0x9E08),
2203 		.driver_info = (unsigned long) &smsc95xx_info,
2204 	},
2205 	{ },		/* END */
2206 };
2207 MODULE_DEVICE_TABLE(usb, products);
2208 
2209 static struct usb_driver smsc95xx_driver = {
2210 	.name		= "smsc95xx",
2211 	.id_table	= products,
2212 	.probe		= usbnet_probe,
2213 	.suspend	= smsc95xx_suspend,
2214 	.resume		= smsc95xx_resume,
2215 	.reset_resume	= smsc95xx_reset_resume,
2216 	.disconnect	= usbnet_disconnect,
2217 	.disable_hub_initiated_lpm = 1,
2218 	.supports_autosuspend = 1,
2219 };
2220 
2221 module_usb_driver(smsc95xx_driver);
2222 
2223 MODULE_AUTHOR("Nancy Lin");
2224 MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>");
2225 MODULE_DESCRIPTION("SMSC95XX USB 2.0 Ethernet Devices");
2226 MODULE_LICENSE("GPL");
2227