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