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