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