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