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