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