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