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