• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2  /***************************************************************************
3  *
4  * Copyright (C) 2007-2010 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 "smsc75xx.h"
22 
23 #define SMSC_CHIPNAME			"smsc75xx"
24 #define SMSC_DRIVER_VERSION		"1.0.0"
25 #define HS_USB_PKT_SIZE			(512)
26 #define FS_USB_PKT_SIZE			(64)
27 #define DEFAULT_HS_BURST_CAP_SIZE	(16 * 1024 + 5 * HS_USB_PKT_SIZE)
28 #define DEFAULT_FS_BURST_CAP_SIZE	(6 * 1024 + 33 * FS_USB_PKT_SIZE)
29 #define DEFAULT_BULK_IN_DELAY		(0x00002000)
30 #define MAX_SINGLE_PACKET_SIZE		(9000)
31 #define LAN75XX_EEPROM_MAGIC		(0x7500)
32 #define EEPROM_MAC_OFFSET		(0x01)
33 #define DEFAULT_TX_CSUM_ENABLE		(true)
34 #define DEFAULT_RX_CSUM_ENABLE		(true)
35 #define SMSC75XX_INTERNAL_PHY_ID	(1)
36 #define SMSC75XX_TX_OVERHEAD		(8)
37 #define MAX_RX_FIFO_SIZE		(20 * 1024)
38 #define MAX_TX_FIFO_SIZE		(12 * 1024)
39 #define USB_VENDOR_ID_SMSC		(0x0424)
40 #define USB_PRODUCT_ID_LAN7500		(0x7500)
41 #define USB_PRODUCT_ID_LAN7505		(0x7505)
42 #define RXW_PADDING			2
43 #define SUPPORTED_WAKE			(WAKE_PHY | WAKE_UCAST | WAKE_BCAST | \
44 					 WAKE_MCAST | WAKE_ARP | WAKE_MAGIC)
45 
46 #define SUSPEND_SUSPEND0		(0x01)
47 #define SUSPEND_SUSPEND1		(0x02)
48 #define SUSPEND_SUSPEND2		(0x04)
49 #define SUSPEND_SUSPEND3		(0x08)
50 #define SUSPEND_ALLMODES		(SUSPEND_SUSPEND0 | SUSPEND_SUSPEND1 | \
51 					 SUSPEND_SUSPEND2 | SUSPEND_SUSPEND3)
52 
53 struct smsc75xx_priv {
54 	struct usbnet *dev;
55 	u32 rfe_ctl;
56 	u32 wolopts;
57 	u32 multicast_hash_table[DP_SEL_VHF_HASH_LEN];
58 	struct mutex dataport_mutex;
59 	spinlock_t rfe_ctl_lock;
60 	struct work_struct set_multicast;
61 	u8 suspend_flags;
62 };
63 
64 struct usb_context {
65 	struct usb_ctrlrequest req;
66 	struct usbnet *dev;
67 };
68 
69 static bool turbo_mode = true;
70 module_param(turbo_mode, bool, 0644);
71 MODULE_PARM_DESC(turbo_mode, "Enable multiple frames per Rx transaction");
72 
73 static int smsc75xx_link_ok_nopm(struct usbnet *dev);
74 static int smsc75xx_phy_gig_workaround(struct usbnet *dev);
75 
__smsc75xx_read_reg(struct usbnet * dev,u32 index,u32 * data,int in_pm)76 static int __must_check __smsc75xx_read_reg(struct usbnet *dev, u32 index,
77 					    u32 *data, int in_pm)
78 {
79 	u32 buf;
80 	int ret;
81 	int (*fn)(struct usbnet *, u8, u8, u16, u16, void *, u16);
82 
83 	BUG_ON(!dev);
84 
85 	if (!in_pm)
86 		fn = usbnet_read_cmd;
87 	else
88 		fn = usbnet_read_cmd_nopm;
89 
90 	ret = fn(dev, USB_VENDOR_REQUEST_READ_REGISTER, USB_DIR_IN
91 		 | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
92 		 0, index, &buf, 4);
93 	if (unlikely(ret < 4)) {
94 		ret = ret < 0 ? ret : -ENODATA;
95 
96 		netdev_warn(dev->net, "Failed to read reg index 0x%08x: %d\n",
97 			    index, ret);
98 		return ret;
99 	}
100 
101 	le32_to_cpus(&buf);
102 	*data = buf;
103 
104 	return ret;
105 }
106 
__smsc75xx_write_reg(struct usbnet * dev,u32 index,u32 data,int in_pm)107 static int __must_check __smsc75xx_write_reg(struct usbnet *dev, u32 index,
108 					     u32 data, int in_pm)
109 {
110 	u32 buf;
111 	int ret;
112 	int (*fn)(struct usbnet *, u8, u8, u16, u16, const void *, u16);
113 
114 	BUG_ON(!dev);
115 
116 	if (!in_pm)
117 		fn = usbnet_write_cmd;
118 	else
119 		fn = usbnet_write_cmd_nopm;
120 
121 	buf = data;
122 	cpu_to_le32s(&buf);
123 
124 	ret = fn(dev, USB_VENDOR_REQUEST_WRITE_REGISTER, USB_DIR_OUT
125 		 | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
126 		 0, index, &buf, 4);
127 	if (unlikely(ret < 0))
128 		netdev_warn(dev->net, "Failed to write reg index 0x%08x: %d\n",
129 			    index, ret);
130 
131 	return ret;
132 }
133 
smsc75xx_read_reg_nopm(struct usbnet * dev,u32 index,u32 * data)134 static int __must_check smsc75xx_read_reg_nopm(struct usbnet *dev, u32 index,
135 					       u32 *data)
136 {
137 	return __smsc75xx_read_reg(dev, index, data, 1);
138 }
139 
smsc75xx_write_reg_nopm(struct usbnet * dev,u32 index,u32 data)140 static int __must_check smsc75xx_write_reg_nopm(struct usbnet *dev, u32 index,
141 						u32 data)
142 {
143 	return __smsc75xx_write_reg(dev, index, data, 1);
144 }
145 
smsc75xx_read_reg(struct usbnet * dev,u32 index,u32 * data)146 static int __must_check smsc75xx_read_reg(struct usbnet *dev, u32 index,
147 					  u32 *data)
148 {
149 	return __smsc75xx_read_reg(dev, index, data, 0);
150 }
151 
smsc75xx_write_reg(struct usbnet * dev,u32 index,u32 data)152 static int __must_check smsc75xx_write_reg(struct usbnet *dev, u32 index,
153 					   u32 data)
154 {
155 	return __smsc75xx_write_reg(dev, index, data, 0);
156 }
157 
158 /* Loop until the read is completed with timeout
159  * called with phy_mutex held */
__smsc75xx_phy_wait_not_busy(struct usbnet * dev,int in_pm)160 static __must_check int __smsc75xx_phy_wait_not_busy(struct usbnet *dev,
161 						     int in_pm)
162 {
163 	unsigned long start_time = jiffies;
164 	u32 val;
165 	int ret;
166 
167 	do {
168 		ret = __smsc75xx_read_reg(dev, MII_ACCESS, &val, in_pm);
169 		if (ret < 0) {
170 			netdev_warn(dev->net, "Error reading MII_ACCESS\n");
171 			return ret;
172 		}
173 
174 		if (!(val & MII_ACCESS_BUSY))
175 			return 0;
176 	} while (!time_after(jiffies, start_time + HZ));
177 
178 	return -EIO;
179 }
180 
__smsc75xx_mdio_read(struct net_device * netdev,int phy_id,int idx,int in_pm)181 static int __smsc75xx_mdio_read(struct net_device *netdev, int phy_id, int idx,
182 				int in_pm)
183 {
184 	struct usbnet *dev = netdev_priv(netdev);
185 	u32 val, addr;
186 	int ret;
187 
188 	mutex_lock(&dev->phy_mutex);
189 
190 	/* confirm MII not busy */
191 	ret = __smsc75xx_phy_wait_not_busy(dev, in_pm);
192 	if (ret < 0) {
193 		netdev_warn(dev->net, "MII is busy in smsc75xx_mdio_read\n");
194 		goto done;
195 	}
196 
197 	/* set the address, index & direction (read from PHY) */
198 	phy_id &= dev->mii.phy_id_mask;
199 	idx &= dev->mii.reg_num_mask;
200 	addr = ((phy_id << MII_ACCESS_PHY_ADDR_SHIFT) & MII_ACCESS_PHY_ADDR)
201 		| ((idx << MII_ACCESS_REG_ADDR_SHIFT) & MII_ACCESS_REG_ADDR)
202 		| MII_ACCESS_READ | MII_ACCESS_BUSY;
203 	ret = __smsc75xx_write_reg(dev, MII_ACCESS, addr, in_pm);
204 	if (ret < 0) {
205 		netdev_warn(dev->net, "Error writing MII_ACCESS\n");
206 		goto done;
207 	}
208 
209 	ret = __smsc75xx_phy_wait_not_busy(dev, in_pm);
210 	if (ret < 0) {
211 		netdev_warn(dev->net, "Timed out reading MII reg %02X\n", idx);
212 		goto done;
213 	}
214 
215 	ret = __smsc75xx_read_reg(dev, MII_DATA, &val, in_pm);
216 	if (ret < 0) {
217 		netdev_warn(dev->net, "Error reading MII_DATA\n");
218 		goto done;
219 	}
220 
221 	ret = (u16)(val & 0xFFFF);
222 
223 done:
224 	mutex_unlock(&dev->phy_mutex);
225 	return ret;
226 }
227 
__smsc75xx_mdio_write(struct net_device * netdev,int phy_id,int idx,int regval,int in_pm)228 static void __smsc75xx_mdio_write(struct net_device *netdev, int phy_id,
229 				  int idx, int regval, int in_pm)
230 {
231 	struct usbnet *dev = netdev_priv(netdev);
232 	u32 val, addr;
233 	int ret;
234 
235 	mutex_lock(&dev->phy_mutex);
236 
237 	/* confirm MII not busy */
238 	ret = __smsc75xx_phy_wait_not_busy(dev, in_pm);
239 	if (ret < 0) {
240 		netdev_warn(dev->net, "MII is busy in smsc75xx_mdio_write\n");
241 		goto done;
242 	}
243 
244 	val = regval;
245 	ret = __smsc75xx_write_reg(dev, MII_DATA, val, in_pm);
246 	if (ret < 0) {
247 		netdev_warn(dev->net, "Error writing MII_DATA\n");
248 		goto done;
249 	}
250 
251 	/* set the address, index & direction (write to PHY) */
252 	phy_id &= dev->mii.phy_id_mask;
253 	idx &= dev->mii.reg_num_mask;
254 	addr = ((phy_id << MII_ACCESS_PHY_ADDR_SHIFT) & MII_ACCESS_PHY_ADDR)
255 		| ((idx << MII_ACCESS_REG_ADDR_SHIFT) & MII_ACCESS_REG_ADDR)
256 		| MII_ACCESS_WRITE | MII_ACCESS_BUSY;
257 	ret = __smsc75xx_write_reg(dev, MII_ACCESS, addr, in_pm);
258 	if (ret < 0) {
259 		netdev_warn(dev->net, "Error writing MII_ACCESS\n");
260 		goto done;
261 	}
262 
263 	ret = __smsc75xx_phy_wait_not_busy(dev, in_pm);
264 	if (ret < 0) {
265 		netdev_warn(dev->net, "Timed out writing MII reg %02X\n", idx);
266 		goto done;
267 	}
268 
269 done:
270 	mutex_unlock(&dev->phy_mutex);
271 }
272 
smsc75xx_mdio_read_nopm(struct net_device * netdev,int phy_id,int idx)273 static int smsc75xx_mdio_read_nopm(struct net_device *netdev, int phy_id,
274 				   int idx)
275 {
276 	return __smsc75xx_mdio_read(netdev, phy_id, idx, 1);
277 }
278 
smsc75xx_mdio_write_nopm(struct net_device * netdev,int phy_id,int idx,int regval)279 static void smsc75xx_mdio_write_nopm(struct net_device *netdev, int phy_id,
280 				     int idx, int regval)
281 {
282 	__smsc75xx_mdio_write(netdev, phy_id, idx, regval, 1);
283 }
284 
smsc75xx_mdio_read(struct net_device * netdev,int phy_id,int idx)285 static int smsc75xx_mdio_read(struct net_device *netdev, int phy_id, int idx)
286 {
287 	return __smsc75xx_mdio_read(netdev, phy_id, idx, 0);
288 }
289 
smsc75xx_mdio_write(struct net_device * netdev,int phy_id,int idx,int regval)290 static void smsc75xx_mdio_write(struct net_device *netdev, int phy_id, int idx,
291 				int regval)
292 {
293 	__smsc75xx_mdio_write(netdev, phy_id, idx, regval, 0);
294 }
295 
smsc75xx_wait_eeprom(struct usbnet * dev)296 static int smsc75xx_wait_eeprom(struct usbnet *dev)
297 {
298 	unsigned long start_time = jiffies;
299 	u32 val;
300 	int ret;
301 
302 	do {
303 		ret = smsc75xx_read_reg(dev, E2P_CMD, &val);
304 		if (ret < 0) {
305 			netdev_warn(dev->net, "Error reading E2P_CMD\n");
306 			return ret;
307 		}
308 
309 		if (!(val & E2P_CMD_BUSY) || (val & E2P_CMD_TIMEOUT))
310 			break;
311 		udelay(40);
312 	} while (!time_after(jiffies, start_time + HZ));
313 
314 	if (val & (E2P_CMD_TIMEOUT | E2P_CMD_BUSY)) {
315 		netdev_warn(dev->net, "EEPROM read operation timeout\n");
316 		return -EIO;
317 	}
318 
319 	return 0;
320 }
321 
smsc75xx_eeprom_confirm_not_busy(struct usbnet * dev)322 static int smsc75xx_eeprom_confirm_not_busy(struct usbnet *dev)
323 {
324 	unsigned long start_time = jiffies;
325 	u32 val;
326 	int ret;
327 
328 	do {
329 		ret = smsc75xx_read_reg(dev, E2P_CMD, &val);
330 		if (ret < 0) {
331 			netdev_warn(dev->net, "Error reading E2P_CMD\n");
332 			return ret;
333 		}
334 
335 		if (!(val & E2P_CMD_BUSY))
336 			return 0;
337 
338 		udelay(40);
339 	} while (!time_after(jiffies, start_time + HZ));
340 
341 	netdev_warn(dev->net, "EEPROM is busy\n");
342 	return -EIO;
343 }
344 
smsc75xx_read_eeprom(struct usbnet * dev,u32 offset,u32 length,u8 * data)345 static int smsc75xx_read_eeprom(struct usbnet *dev, u32 offset, u32 length,
346 				u8 *data)
347 {
348 	u32 val;
349 	int i, ret;
350 
351 	BUG_ON(!dev);
352 	BUG_ON(!data);
353 
354 	ret = smsc75xx_eeprom_confirm_not_busy(dev);
355 	if (ret)
356 		return ret;
357 
358 	for (i = 0; i < length; i++) {
359 		val = E2P_CMD_BUSY | E2P_CMD_READ | (offset & E2P_CMD_ADDR);
360 		ret = smsc75xx_write_reg(dev, E2P_CMD, val);
361 		if (ret < 0) {
362 			netdev_warn(dev->net, "Error writing E2P_CMD\n");
363 			return ret;
364 		}
365 
366 		ret = smsc75xx_wait_eeprom(dev);
367 		if (ret < 0)
368 			return ret;
369 
370 		ret = smsc75xx_read_reg(dev, E2P_DATA, &val);
371 		if (ret < 0) {
372 			netdev_warn(dev->net, "Error reading E2P_DATA\n");
373 			return ret;
374 		}
375 
376 		data[i] = val & 0xFF;
377 		offset++;
378 	}
379 
380 	return 0;
381 }
382 
smsc75xx_write_eeprom(struct usbnet * dev,u32 offset,u32 length,u8 * data)383 static int smsc75xx_write_eeprom(struct usbnet *dev, u32 offset, u32 length,
384 				 u8 *data)
385 {
386 	u32 val;
387 	int i, ret;
388 
389 	BUG_ON(!dev);
390 	BUG_ON(!data);
391 
392 	ret = smsc75xx_eeprom_confirm_not_busy(dev);
393 	if (ret)
394 		return ret;
395 
396 	/* Issue write/erase enable command */
397 	val = E2P_CMD_BUSY | E2P_CMD_EWEN;
398 	ret = smsc75xx_write_reg(dev, E2P_CMD, val);
399 	if (ret < 0) {
400 		netdev_warn(dev->net, "Error writing E2P_CMD\n");
401 		return ret;
402 	}
403 
404 	ret = smsc75xx_wait_eeprom(dev);
405 	if (ret < 0)
406 		return ret;
407 
408 	for (i = 0; i < length; i++) {
409 
410 		/* Fill data register */
411 		val = data[i];
412 		ret = smsc75xx_write_reg(dev, E2P_DATA, val);
413 		if (ret < 0) {
414 			netdev_warn(dev->net, "Error writing E2P_DATA\n");
415 			return ret;
416 		}
417 
418 		/* Send "write" command */
419 		val = E2P_CMD_BUSY | E2P_CMD_WRITE | (offset & E2P_CMD_ADDR);
420 		ret = smsc75xx_write_reg(dev, E2P_CMD, val);
421 		if (ret < 0) {
422 			netdev_warn(dev->net, "Error writing E2P_CMD\n");
423 			return ret;
424 		}
425 
426 		ret = smsc75xx_wait_eeprom(dev);
427 		if (ret < 0)
428 			return ret;
429 
430 		offset++;
431 	}
432 
433 	return 0;
434 }
435 
smsc75xx_dataport_wait_not_busy(struct usbnet * dev)436 static int smsc75xx_dataport_wait_not_busy(struct usbnet *dev)
437 {
438 	int i, ret;
439 
440 	for (i = 0; i < 100; i++) {
441 		u32 dp_sel;
442 		ret = smsc75xx_read_reg(dev, DP_SEL, &dp_sel);
443 		if (ret < 0) {
444 			netdev_warn(dev->net, "Error reading DP_SEL\n");
445 			return ret;
446 		}
447 
448 		if (dp_sel & DP_SEL_DPRDY)
449 			return 0;
450 
451 		udelay(40);
452 	}
453 
454 	netdev_warn(dev->net, "smsc75xx_dataport_wait_not_busy timed out\n");
455 
456 	return -EIO;
457 }
458 
smsc75xx_dataport_write(struct usbnet * dev,u32 ram_select,u32 addr,u32 length,u32 * buf)459 static int smsc75xx_dataport_write(struct usbnet *dev, u32 ram_select, u32 addr,
460 				   u32 length, u32 *buf)
461 {
462 	struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
463 	u32 dp_sel;
464 	int i, ret;
465 
466 	mutex_lock(&pdata->dataport_mutex);
467 
468 	ret = smsc75xx_dataport_wait_not_busy(dev);
469 	if (ret < 0) {
470 		netdev_warn(dev->net, "smsc75xx_dataport_write busy on entry\n");
471 		goto done;
472 	}
473 
474 	ret = smsc75xx_read_reg(dev, DP_SEL, &dp_sel);
475 	if (ret < 0) {
476 		netdev_warn(dev->net, "Error reading DP_SEL\n");
477 		goto done;
478 	}
479 
480 	dp_sel &= ~DP_SEL_RSEL;
481 	dp_sel |= ram_select;
482 	ret = smsc75xx_write_reg(dev, DP_SEL, dp_sel);
483 	if (ret < 0) {
484 		netdev_warn(dev->net, "Error writing DP_SEL\n");
485 		goto done;
486 	}
487 
488 	for (i = 0; i < length; i++) {
489 		ret = smsc75xx_write_reg(dev, DP_ADDR, addr + i);
490 		if (ret < 0) {
491 			netdev_warn(dev->net, "Error writing DP_ADDR\n");
492 			goto done;
493 		}
494 
495 		ret = smsc75xx_write_reg(dev, DP_DATA, buf[i]);
496 		if (ret < 0) {
497 			netdev_warn(dev->net, "Error writing DP_DATA\n");
498 			goto done;
499 		}
500 
501 		ret = smsc75xx_write_reg(dev, DP_CMD, DP_CMD_WRITE);
502 		if (ret < 0) {
503 			netdev_warn(dev->net, "Error writing DP_CMD\n");
504 			goto done;
505 		}
506 
507 		ret = smsc75xx_dataport_wait_not_busy(dev);
508 		if (ret < 0) {
509 			netdev_warn(dev->net, "smsc75xx_dataport_write timeout\n");
510 			goto done;
511 		}
512 	}
513 
514 done:
515 	mutex_unlock(&pdata->dataport_mutex);
516 	return ret;
517 }
518 
519 /* returns hash bit number for given MAC address */
smsc75xx_hash(char addr[ETH_ALEN])520 static u32 smsc75xx_hash(char addr[ETH_ALEN])
521 {
522 	return (ether_crc(ETH_ALEN, addr) >> 23) & 0x1ff;
523 }
524 
smsc75xx_deferred_multicast_write(struct work_struct * param)525 static void smsc75xx_deferred_multicast_write(struct work_struct *param)
526 {
527 	struct smsc75xx_priv *pdata =
528 		container_of(param, struct smsc75xx_priv, set_multicast);
529 	struct usbnet *dev = pdata->dev;
530 	int ret;
531 
532 	netif_dbg(dev, drv, dev->net, "deferred multicast write 0x%08x\n",
533 		  pdata->rfe_ctl);
534 
535 	smsc75xx_dataport_write(dev, DP_SEL_VHF, DP_SEL_VHF_VLAN_LEN,
536 		DP_SEL_VHF_HASH_LEN, pdata->multicast_hash_table);
537 
538 	ret = smsc75xx_write_reg(dev, RFE_CTL, pdata->rfe_ctl);
539 	if (ret < 0)
540 		netdev_warn(dev->net, "Error writing RFE_CRL\n");
541 }
542 
smsc75xx_set_multicast(struct net_device * netdev)543 static void smsc75xx_set_multicast(struct net_device *netdev)
544 {
545 	struct usbnet *dev = netdev_priv(netdev);
546 	struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
547 	unsigned long flags;
548 	int i;
549 
550 	spin_lock_irqsave(&pdata->rfe_ctl_lock, flags);
551 
552 	pdata->rfe_ctl &=
553 		~(RFE_CTL_AU | RFE_CTL_AM | RFE_CTL_DPF | RFE_CTL_MHF);
554 	pdata->rfe_ctl |= RFE_CTL_AB;
555 
556 	for (i = 0; i < DP_SEL_VHF_HASH_LEN; i++)
557 		pdata->multicast_hash_table[i] = 0;
558 
559 	if (dev->net->flags & IFF_PROMISC) {
560 		netif_dbg(dev, drv, dev->net, "promiscuous mode enabled\n");
561 		pdata->rfe_ctl |= RFE_CTL_AM | RFE_CTL_AU;
562 	} else if (dev->net->flags & IFF_ALLMULTI) {
563 		netif_dbg(dev, drv, dev->net, "receive all multicast enabled\n");
564 		pdata->rfe_ctl |= RFE_CTL_AM | RFE_CTL_DPF;
565 	} else if (!netdev_mc_empty(dev->net)) {
566 		struct netdev_hw_addr *ha;
567 
568 		netif_dbg(dev, drv, dev->net, "receive multicast hash filter\n");
569 
570 		pdata->rfe_ctl |= RFE_CTL_MHF | RFE_CTL_DPF;
571 
572 		netdev_for_each_mc_addr(ha, netdev) {
573 			u32 bitnum = smsc75xx_hash(ha->addr);
574 			pdata->multicast_hash_table[bitnum / 32] |=
575 				(1 << (bitnum % 32));
576 		}
577 	} else {
578 		netif_dbg(dev, drv, dev->net, "receive own packets only\n");
579 		pdata->rfe_ctl |= RFE_CTL_DPF;
580 	}
581 
582 	spin_unlock_irqrestore(&pdata->rfe_ctl_lock, flags);
583 
584 	/* defer register writes to a sleepable context */
585 	schedule_work(&pdata->set_multicast);
586 }
587 
smsc75xx_update_flowcontrol(struct usbnet * dev,u8 duplex,u16 lcladv,u16 rmtadv)588 static int smsc75xx_update_flowcontrol(struct usbnet *dev, u8 duplex,
589 					    u16 lcladv, u16 rmtadv)
590 {
591 	u32 flow = 0, fct_flow = 0;
592 	int ret;
593 
594 	if (duplex == DUPLEX_FULL) {
595 		u8 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
596 
597 		if (cap & FLOW_CTRL_TX) {
598 			flow = (FLOW_TX_FCEN | 0xFFFF);
599 			/* set fct_flow thresholds to 20% and 80% */
600 			fct_flow = (8 << 8) | 32;
601 		}
602 
603 		if (cap & FLOW_CTRL_RX)
604 			flow |= FLOW_RX_FCEN;
605 
606 		netif_dbg(dev, link, dev->net, "rx pause %s, tx pause %s\n",
607 			  (cap & FLOW_CTRL_RX ? "enabled" : "disabled"),
608 			  (cap & FLOW_CTRL_TX ? "enabled" : "disabled"));
609 	} else {
610 		netif_dbg(dev, link, dev->net, "half duplex\n");
611 	}
612 
613 	ret = smsc75xx_write_reg(dev, FLOW, flow);
614 	if (ret < 0) {
615 		netdev_warn(dev->net, "Error writing FLOW\n");
616 		return ret;
617 	}
618 
619 	ret = smsc75xx_write_reg(dev, FCT_FLOW, fct_flow);
620 	if (ret < 0) {
621 		netdev_warn(dev->net, "Error writing FCT_FLOW\n");
622 		return ret;
623 	}
624 
625 	return 0;
626 }
627 
smsc75xx_link_reset(struct usbnet * dev)628 static int smsc75xx_link_reset(struct usbnet *dev)
629 {
630 	struct mii_if_info *mii = &dev->mii;
631 	struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
632 	u16 lcladv, rmtadv;
633 	int ret;
634 
635 	/* write to clear phy interrupt status */
636 	smsc75xx_mdio_write(dev->net, mii->phy_id, PHY_INT_SRC,
637 		PHY_INT_SRC_CLEAR_ALL);
638 
639 	ret = smsc75xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL);
640 	if (ret < 0) {
641 		netdev_warn(dev->net, "Error writing INT_STS\n");
642 		return ret;
643 	}
644 
645 	mii_check_media(mii, 1, 1);
646 	mii_ethtool_gset(&dev->mii, &ecmd);
647 	lcladv = smsc75xx_mdio_read(dev->net, mii->phy_id, MII_ADVERTISE);
648 	rmtadv = smsc75xx_mdio_read(dev->net, mii->phy_id, MII_LPA);
649 
650 	netif_dbg(dev, link, dev->net, "speed: %u duplex: %d lcladv: %04x rmtadv: %04x\n",
651 		  ethtool_cmd_speed(&ecmd), ecmd.duplex, lcladv, rmtadv);
652 
653 	return smsc75xx_update_flowcontrol(dev, ecmd.duplex, lcladv, rmtadv);
654 }
655 
smsc75xx_status(struct usbnet * dev,struct urb * urb)656 static void smsc75xx_status(struct usbnet *dev, struct urb *urb)
657 {
658 	u32 intdata;
659 
660 	if (urb->actual_length != 4) {
661 		netdev_warn(dev->net, "unexpected urb length %d\n",
662 			    urb->actual_length);
663 		return;
664 	}
665 
666 	intdata = get_unaligned_le32(urb->transfer_buffer);
667 
668 	netif_dbg(dev, link, dev->net, "intdata: 0x%08X\n", intdata);
669 
670 	if (intdata & INT_ENP_PHY_INT)
671 		usbnet_defer_kevent(dev, EVENT_LINK_RESET);
672 	else
673 		netdev_warn(dev->net, "unexpected interrupt, intdata=0x%08X\n",
674 			    intdata);
675 }
676 
smsc75xx_ethtool_get_eeprom_len(struct net_device * net)677 static int smsc75xx_ethtool_get_eeprom_len(struct net_device *net)
678 {
679 	return MAX_EEPROM_SIZE;
680 }
681 
smsc75xx_ethtool_get_eeprom(struct net_device * netdev,struct ethtool_eeprom * ee,u8 * data)682 static int smsc75xx_ethtool_get_eeprom(struct net_device *netdev,
683 				       struct ethtool_eeprom *ee, u8 *data)
684 {
685 	struct usbnet *dev = netdev_priv(netdev);
686 
687 	ee->magic = LAN75XX_EEPROM_MAGIC;
688 
689 	return smsc75xx_read_eeprom(dev, ee->offset, ee->len, data);
690 }
691 
smsc75xx_ethtool_set_eeprom(struct net_device * netdev,struct ethtool_eeprom * ee,u8 * data)692 static int smsc75xx_ethtool_set_eeprom(struct net_device *netdev,
693 				       struct ethtool_eeprom *ee, u8 *data)
694 {
695 	struct usbnet *dev = netdev_priv(netdev);
696 
697 	if (ee->magic != LAN75XX_EEPROM_MAGIC) {
698 		netdev_warn(dev->net, "EEPROM: magic value mismatch: 0x%x\n",
699 			    ee->magic);
700 		return -EINVAL;
701 	}
702 
703 	return smsc75xx_write_eeprom(dev, ee->offset, ee->len, data);
704 }
705 
smsc75xx_ethtool_get_wol(struct net_device * net,struct ethtool_wolinfo * wolinfo)706 static void smsc75xx_ethtool_get_wol(struct net_device *net,
707 				     struct ethtool_wolinfo *wolinfo)
708 {
709 	struct usbnet *dev = netdev_priv(net);
710 	struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
711 
712 	wolinfo->supported = SUPPORTED_WAKE;
713 	wolinfo->wolopts = pdata->wolopts;
714 }
715 
smsc75xx_ethtool_set_wol(struct net_device * net,struct ethtool_wolinfo * wolinfo)716 static int smsc75xx_ethtool_set_wol(struct net_device *net,
717 				    struct ethtool_wolinfo *wolinfo)
718 {
719 	struct usbnet *dev = netdev_priv(net);
720 	struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
721 	int ret;
722 
723 	if (wolinfo->wolopts & ~SUPPORTED_WAKE)
724 		return -EINVAL;
725 
726 	pdata->wolopts = wolinfo->wolopts & SUPPORTED_WAKE;
727 
728 	ret = device_set_wakeup_enable(&dev->udev->dev, pdata->wolopts);
729 	if (ret < 0)
730 		netdev_warn(dev->net, "device_set_wakeup_enable error %d\n", ret);
731 
732 	return ret;
733 }
734 
735 static const struct ethtool_ops smsc75xx_ethtool_ops = {
736 	.get_link	= usbnet_get_link,
737 	.nway_reset	= usbnet_nway_reset,
738 	.get_drvinfo	= usbnet_get_drvinfo,
739 	.get_msglevel	= usbnet_get_msglevel,
740 	.set_msglevel	= usbnet_set_msglevel,
741 	.get_eeprom_len	= smsc75xx_ethtool_get_eeprom_len,
742 	.get_eeprom	= smsc75xx_ethtool_get_eeprom,
743 	.set_eeprom	= smsc75xx_ethtool_set_eeprom,
744 	.get_wol	= smsc75xx_ethtool_get_wol,
745 	.set_wol	= smsc75xx_ethtool_set_wol,
746 	.get_link_ksettings	= usbnet_get_link_ksettings_mii,
747 	.set_link_ksettings	= usbnet_set_link_ksettings_mii,
748 };
749 
smsc75xx_ioctl(struct net_device * netdev,struct ifreq * rq,int cmd)750 static int smsc75xx_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
751 {
752 	struct usbnet *dev = netdev_priv(netdev);
753 
754 	if (!netif_running(netdev))
755 		return -EINVAL;
756 
757 	return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
758 }
759 
smsc75xx_init_mac_address(struct usbnet * dev)760 static void smsc75xx_init_mac_address(struct usbnet *dev)
761 {
762 	/* maybe the boot loader passed the MAC address in devicetree */
763 	if (!eth_platform_get_mac_address(&dev->udev->dev,
764 			dev->net->dev_addr)) {
765 		if (is_valid_ether_addr(dev->net->dev_addr)) {
766 			/* device tree values are valid so use them */
767 			netif_dbg(dev, ifup, dev->net, "MAC address read from the device tree\n");
768 			return;
769 		}
770 	}
771 
772 	/* try reading mac address from EEPROM */
773 	if (smsc75xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN,
774 			dev->net->dev_addr) == 0) {
775 		if (is_valid_ether_addr(dev->net->dev_addr)) {
776 			/* eeprom values are valid so use them */
777 			netif_dbg(dev, ifup, dev->net,
778 				  "MAC address read from EEPROM\n");
779 			return;
780 		}
781 	}
782 
783 	/* no useful static MAC address found. generate a random one */
784 	eth_hw_addr_random(dev->net);
785 	netif_dbg(dev, ifup, dev->net, "MAC address set to eth_random_addr\n");
786 }
787 
smsc75xx_set_mac_address(struct usbnet * dev)788 static int smsc75xx_set_mac_address(struct usbnet *dev)
789 {
790 	u32 addr_lo = dev->net->dev_addr[0] | dev->net->dev_addr[1] << 8 |
791 		dev->net->dev_addr[2] << 16 | dev->net->dev_addr[3] << 24;
792 	u32 addr_hi = dev->net->dev_addr[4] | dev->net->dev_addr[5] << 8;
793 
794 	int ret = smsc75xx_write_reg(dev, RX_ADDRH, addr_hi);
795 	if (ret < 0) {
796 		netdev_warn(dev->net, "Failed to write RX_ADDRH: %d\n", ret);
797 		return ret;
798 	}
799 
800 	ret = smsc75xx_write_reg(dev, RX_ADDRL, addr_lo);
801 	if (ret < 0) {
802 		netdev_warn(dev->net, "Failed to write RX_ADDRL: %d\n", ret);
803 		return ret;
804 	}
805 
806 	addr_hi |= ADDR_FILTX_FB_VALID;
807 	ret = smsc75xx_write_reg(dev, ADDR_FILTX, addr_hi);
808 	if (ret < 0) {
809 		netdev_warn(dev->net, "Failed to write ADDR_FILTX: %d\n", ret);
810 		return ret;
811 	}
812 
813 	ret = smsc75xx_write_reg(dev, ADDR_FILTX + 4, addr_lo);
814 	if (ret < 0)
815 		netdev_warn(dev->net, "Failed to write ADDR_FILTX+4: %d\n", ret);
816 
817 	return ret;
818 }
819 
smsc75xx_phy_initialize(struct usbnet * dev)820 static int smsc75xx_phy_initialize(struct usbnet *dev)
821 {
822 	int bmcr, ret, timeout = 0;
823 
824 	/* Initialize MII structure */
825 	dev->mii.dev = dev->net;
826 	dev->mii.mdio_read = smsc75xx_mdio_read;
827 	dev->mii.mdio_write = smsc75xx_mdio_write;
828 	dev->mii.phy_id_mask = 0x1f;
829 	dev->mii.reg_num_mask = 0x1f;
830 	dev->mii.supports_gmii = 1;
831 	dev->mii.phy_id = SMSC75XX_INTERNAL_PHY_ID;
832 
833 	/* reset phy and wait for reset to complete */
834 	smsc75xx_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
835 
836 	do {
837 		msleep(10);
838 		bmcr = smsc75xx_mdio_read(dev->net, dev->mii.phy_id, MII_BMCR);
839 		if (bmcr < 0) {
840 			netdev_warn(dev->net, "Error reading MII_BMCR\n");
841 			return bmcr;
842 		}
843 		timeout++;
844 	} while ((bmcr & BMCR_RESET) && (timeout < 100));
845 
846 	if (timeout >= 100) {
847 		netdev_warn(dev->net, "timeout on PHY Reset\n");
848 		return -EIO;
849 	}
850 
851 	/* phy workaround for gig link */
852 	smsc75xx_phy_gig_workaround(dev);
853 
854 	smsc75xx_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
855 		ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP |
856 		ADVERTISE_PAUSE_ASYM);
857 	smsc75xx_mdio_write(dev->net, dev->mii.phy_id, MII_CTRL1000,
858 		ADVERTISE_1000FULL);
859 
860 	/* read and write to clear phy interrupt status */
861 	ret = smsc75xx_mdio_read(dev->net, dev->mii.phy_id, PHY_INT_SRC);
862 	if (ret < 0) {
863 		netdev_warn(dev->net, "Error reading PHY_INT_SRC\n");
864 		return ret;
865 	}
866 
867 	smsc75xx_mdio_write(dev->net, dev->mii.phy_id, PHY_INT_SRC, 0xffff);
868 
869 	smsc75xx_mdio_write(dev->net, dev->mii.phy_id, PHY_INT_MASK,
870 		PHY_INT_MASK_DEFAULT);
871 	mii_nway_restart(&dev->mii);
872 
873 	netif_dbg(dev, ifup, dev->net, "phy initialised successfully\n");
874 	return 0;
875 }
876 
smsc75xx_set_rx_max_frame_length(struct usbnet * dev,int size)877 static int smsc75xx_set_rx_max_frame_length(struct usbnet *dev, int size)
878 {
879 	int ret = 0;
880 	u32 buf;
881 	bool rxenabled;
882 
883 	ret = smsc75xx_read_reg(dev, MAC_RX, &buf);
884 	if (ret < 0) {
885 		netdev_warn(dev->net, "Failed to read MAC_RX: %d\n", ret);
886 		return ret;
887 	}
888 
889 	rxenabled = ((buf & MAC_RX_RXEN) != 0);
890 
891 	if (rxenabled) {
892 		buf &= ~MAC_RX_RXEN;
893 		ret = smsc75xx_write_reg(dev, MAC_RX, buf);
894 		if (ret < 0) {
895 			netdev_warn(dev->net, "Failed to write MAC_RX: %d\n", ret);
896 			return ret;
897 		}
898 	}
899 
900 	/* add 4 to size for FCS */
901 	buf &= ~MAC_RX_MAX_SIZE;
902 	buf |= (((size + 4) << MAC_RX_MAX_SIZE_SHIFT) & MAC_RX_MAX_SIZE);
903 
904 	ret = smsc75xx_write_reg(dev, MAC_RX, buf);
905 	if (ret < 0) {
906 		netdev_warn(dev->net, "Failed to write MAC_RX: %d\n", ret);
907 		return ret;
908 	}
909 
910 	if (rxenabled) {
911 		buf |= MAC_RX_RXEN;
912 		ret = smsc75xx_write_reg(dev, MAC_RX, buf);
913 		if (ret < 0) {
914 			netdev_warn(dev->net, "Failed to write MAC_RX: %d\n", ret);
915 			return ret;
916 		}
917 	}
918 
919 	return 0;
920 }
921 
smsc75xx_change_mtu(struct net_device * netdev,int new_mtu)922 static int smsc75xx_change_mtu(struct net_device *netdev, int new_mtu)
923 {
924 	struct usbnet *dev = netdev_priv(netdev);
925 	int ret;
926 
927 	ret = smsc75xx_set_rx_max_frame_length(dev, new_mtu + ETH_HLEN);
928 	if (ret < 0) {
929 		netdev_warn(dev->net, "Failed to set mac rx frame length\n");
930 		return ret;
931 	}
932 
933 	return usbnet_change_mtu(netdev, new_mtu);
934 }
935 
936 /* Enable or disable Rx checksum offload engine */
smsc75xx_set_features(struct net_device * netdev,netdev_features_t features)937 static int smsc75xx_set_features(struct net_device *netdev,
938 	netdev_features_t features)
939 {
940 	struct usbnet *dev = netdev_priv(netdev);
941 	struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
942 	unsigned long flags;
943 	int ret;
944 
945 	spin_lock_irqsave(&pdata->rfe_ctl_lock, flags);
946 
947 	if (features & NETIF_F_RXCSUM)
948 		pdata->rfe_ctl |= RFE_CTL_TCPUDP_CKM | RFE_CTL_IP_CKM;
949 	else
950 		pdata->rfe_ctl &= ~(RFE_CTL_TCPUDP_CKM | RFE_CTL_IP_CKM);
951 
952 	spin_unlock_irqrestore(&pdata->rfe_ctl_lock, flags);
953 	/* it's racing here! */
954 
955 	ret = smsc75xx_write_reg(dev, RFE_CTL, pdata->rfe_ctl);
956 	if (ret < 0) {
957 		netdev_warn(dev->net, "Error writing RFE_CTL\n");
958 		return ret;
959 	}
960 	return 0;
961 }
962 
smsc75xx_wait_ready(struct usbnet * dev,int in_pm)963 static int smsc75xx_wait_ready(struct usbnet *dev, int in_pm)
964 {
965 	int timeout = 0;
966 
967 	do {
968 		u32 buf;
969 		int ret;
970 
971 		ret = __smsc75xx_read_reg(dev, PMT_CTL, &buf, in_pm);
972 
973 		if (ret < 0) {
974 			netdev_warn(dev->net, "Failed to read PMT_CTL: %d\n", ret);
975 			return ret;
976 		}
977 
978 		if (buf & PMT_CTL_DEV_RDY)
979 			return 0;
980 
981 		msleep(10);
982 		timeout++;
983 	} while (timeout < 100);
984 
985 	netdev_warn(dev->net, "timeout waiting for device ready\n");
986 	return -EIO;
987 }
988 
smsc75xx_phy_gig_workaround(struct usbnet * dev)989 static int smsc75xx_phy_gig_workaround(struct usbnet *dev)
990 {
991 	struct mii_if_info *mii = &dev->mii;
992 	int ret = 0, timeout = 0;
993 	u32 buf, link_up = 0;
994 
995 	/* Set the phy in Gig loopback */
996 	smsc75xx_mdio_write(dev->net, mii->phy_id, MII_BMCR, 0x4040);
997 
998 	/* Wait for the link up */
999 	do {
1000 		link_up = smsc75xx_link_ok_nopm(dev);
1001 		usleep_range(10000, 20000);
1002 		timeout++;
1003 	} while ((!link_up) && (timeout < 1000));
1004 
1005 	if (timeout >= 1000) {
1006 		netdev_warn(dev->net, "Timeout waiting for PHY link up\n");
1007 		return -EIO;
1008 	}
1009 
1010 	/* phy reset */
1011 	ret = smsc75xx_read_reg(dev, PMT_CTL, &buf);
1012 	if (ret < 0) {
1013 		netdev_warn(dev->net, "Failed to read PMT_CTL: %d\n", ret);
1014 		return ret;
1015 	}
1016 
1017 	buf |= PMT_CTL_PHY_RST;
1018 
1019 	ret = smsc75xx_write_reg(dev, PMT_CTL, buf);
1020 	if (ret < 0) {
1021 		netdev_warn(dev->net, "Failed to write PMT_CTL: %d\n", ret);
1022 		return ret;
1023 	}
1024 
1025 	timeout = 0;
1026 	do {
1027 		usleep_range(10000, 20000);
1028 		ret = smsc75xx_read_reg(dev, PMT_CTL, &buf);
1029 		if (ret < 0) {
1030 			netdev_warn(dev->net, "Failed to read PMT_CTL: %d\n",
1031 				    ret);
1032 			return ret;
1033 		}
1034 		timeout++;
1035 	} while ((buf & PMT_CTL_PHY_RST) && (timeout < 100));
1036 
1037 	if (timeout >= 100) {
1038 		netdev_warn(dev->net, "timeout waiting for PHY Reset\n");
1039 		return -EIO;
1040 	}
1041 
1042 	return 0;
1043 }
1044 
smsc75xx_reset(struct usbnet * dev)1045 static int smsc75xx_reset(struct usbnet *dev)
1046 {
1047 	struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
1048 	u32 buf;
1049 	int ret = 0, timeout;
1050 
1051 	netif_dbg(dev, ifup, dev->net, "entering smsc75xx_reset\n");
1052 
1053 	ret = smsc75xx_wait_ready(dev, 0);
1054 	if (ret < 0) {
1055 		netdev_warn(dev->net, "device not ready in smsc75xx_reset\n");
1056 		return ret;
1057 	}
1058 
1059 	ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
1060 	if (ret < 0) {
1061 		netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret);
1062 		return ret;
1063 	}
1064 
1065 	buf |= HW_CFG_LRST;
1066 
1067 	ret = smsc75xx_write_reg(dev, HW_CFG, buf);
1068 	if (ret < 0) {
1069 		netdev_warn(dev->net, "Failed to write HW_CFG: %d\n", ret);
1070 		return ret;
1071 	}
1072 
1073 	timeout = 0;
1074 	do {
1075 		msleep(10);
1076 		ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
1077 		if (ret < 0) {
1078 			netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret);
1079 			return ret;
1080 		}
1081 		timeout++;
1082 	} while ((buf & HW_CFG_LRST) && (timeout < 100));
1083 
1084 	if (timeout >= 100) {
1085 		netdev_warn(dev->net, "timeout on completion of Lite Reset\n");
1086 		return -EIO;
1087 	}
1088 
1089 	netif_dbg(dev, ifup, dev->net, "Lite reset complete, resetting PHY\n");
1090 
1091 	ret = smsc75xx_read_reg(dev, PMT_CTL, &buf);
1092 	if (ret < 0) {
1093 		netdev_warn(dev->net, "Failed to read PMT_CTL: %d\n", ret);
1094 		return ret;
1095 	}
1096 
1097 	buf |= PMT_CTL_PHY_RST;
1098 
1099 	ret = smsc75xx_write_reg(dev, PMT_CTL, buf);
1100 	if (ret < 0) {
1101 		netdev_warn(dev->net, "Failed to write PMT_CTL: %d\n", ret);
1102 		return ret;
1103 	}
1104 
1105 	timeout = 0;
1106 	do {
1107 		msleep(10);
1108 		ret = smsc75xx_read_reg(dev, PMT_CTL, &buf);
1109 		if (ret < 0) {
1110 			netdev_warn(dev->net, "Failed to read PMT_CTL: %d\n", ret);
1111 			return ret;
1112 		}
1113 		timeout++;
1114 	} while ((buf & PMT_CTL_PHY_RST) && (timeout < 100));
1115 
1116 	if (timeout >= 100) {
1117 		netdev_warn(dev->net, "timeout waiting for PHY Reset\n");
1118 		return -EIO;
1119 	}
1120 
1121 	netif_dbg(dev, ifup, dev->net, "PHY reset complete\n");
1122 
1123 	ret = smsc75xx_set_mac_address(dev);
1124 	if (ret < 0) {
1125 		netdev_warn(dev->net, "Failed to set mac address\n");
1126 		return ret;
1127 	}
1128 
1129 	netif_dbg(dev, ifup, dev->net, "MAC Address: %pM\n",
1130 		  dev->net->dev_addr);
1131 
1132 	ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
1133 	if (ret < 0) {
1134 		netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret);
1135 		return ret;
1136 	}
1137 
1138 	netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG : 0x%08x\n",
1139 		  buf);
1140 
1141 	buf |= HW_CFG_BIR;
1142 
1143 	ret = smsc75xx_write_reg(dev, HW_CFG, buf);
1144 	if (ret < 0) {
1145 		netdev_warn(dev->net,  "Failed to write HW_CFG: %d\n", ret);
1146 		return ret;
1147 	}
1148 
1149 	ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
1150 	if (ret < 0) {
1151 		netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret);
1152 		return ret;
1153 	}
1154 
1155 	netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG after writing HW_CFG_BIR: 0x%08x\n",
1156 		  buf);
1157 
1158 	if (!turbo_mode) {
1159 		buf = 0;
1160 		dev->rx_urb_size = MAX_SINGLE_PACKET_SIZE;
1161 	} else if (dev->udev->speed == USB_SPEED_HIGH) {
1162 		buf = DEFAULT_HS_BURST_CAP_SIZE / HS_USB_PKT_SIZE;
1163 		dev->rx_urb_size = DEFAULT_HS_BURST_CAP_SIZE;
1164 	} else {
1165 		buf = DEFAULT_FS_BURST_CAP_SIZE / FS_USB_PKT_SIZE;
1166 		dev->rx_urb_size = DEFAULT_FS_BURST_CAP_SIZE;
1167 	}
1168 
1169 	netif_dbg(dev, ifup, dev->net, "rx_urb_size=%ld\n",
1170 		  (ulong)dev->rx_urb_size);
1171 
1172 	ret = smsc75xx_write_reg(dev, BURST_CAP, buf);
1173 	if (ret < 0) {
1174 		netdev_warn(dev->net, "Failed to write BURST_CAP: %d\n", ret);
1175 		return ret;
1176 	}
1177 
1178 	ret = smsc75xx_read_reg(dev, BURST_CAP, &buf);
1179 	if (ret < 0) {
1180 		netdev_warn(dev->net, "Failed to read BURST_CAP: %d\n", ret);
1181 		return ret;
1182 	}
1183 
1184 	netif_dbg(dev, ifup, dev->net,
1185 		  "Read Value from BURST_CAP after writing: 0x%08x\n", buf);
1186 
1187 	ret = smsc75xx_write_reg(dev, BULK_IN_DLY, DEFAULT_BULK_IN_DELAY);
1188 	if (ret < 0) {
1189 		netdev_warn(dev->net, "Failed to write BULK_IN_DLY: %d\n", ret);
1190 		return ret;
1191 	}
1192 
1193 	ret = smsc75xx_read_reg(dev, BULK_IN_DLY, &buf);
1194 	if (ret < 0) {
1195 		netdev_warn(dev->net, "Failed to read BULK_IN_DLY: %d\n", ret);
1196 		return ret;
1197 	}
1198 
1199 	netif_dbg(dev, ifup, dev->net,
1200 		  "Read Value from BULK_IN_DLY after writing: 0x%08x\n", buf);
1201 
1202 	if (turbo_mode) {
1203 		ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
1204 		if (ret < 0) {
1205 			netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret);
1206 			return ret;
1207 		}
1208 
1209 		netif_dbg(dev, ifup, dev->net, "HW_CFG: 0x%08x\n", buf);
1210 
1211 		buf |= (HW_CFG_MEF | HW_CFG_BCE);
1212 
1213 		ret = smsc75xx_write_reg(dev, HW_CFG, buf);
1214 		if (ret < 0) {
1215 			netdev_warn(dev->net, "Failed to write HW_CFG: %d\n", ret);
1216 			return ret;
1217 		}
1218 
1219 		ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
1220 		if (ret < 0) {
1221 			netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret);
1222 			return ret;
1223 		}
1224 
1225 		netif_dbg(dev, ifup, dev->net, "HW_CFG: 0x%08x\n", buf);
1226 	}
1227 
1228 	/* set FIFO sizes */
1229 	buf = (MAX_RX_FIFO_SIZE - 512) / 512;
1230 	ret = smsc75xx_write_reg(dev, FCT_RX_FIFO_END, buf);
1231 	if (ret < 0) {
1232 		netdev_warn(dev->net, "Failed to write FCT_RX_FIFO_END: %d\n", ret);
1233 		return ret;
1234 	}
1235 
1236 	netif_dbg(dev, ifup, dev->net, "FCT_RX_FIFO_END set to 0x%08x\n", buf);
1237 
1238 	buf = (MAX_TX_FIFO_SIZE - 512) / 512;
1239 	ret = smsc75xx_write_reg(dev, FCT_TX_FIFO_END, buf);
1240 	if (ret < 0) {
1241 		netdev_warn(dev->net, "Failed to write FCT_TX_FIFO_END: %d\n", ret);
1242 		return ret;
1243 	}
1244 
1245 	netif_dbg(dev, ifup, dev->net, "FCT_TX_FIFO_END set to 0x%08x\n", buf);
1246 
1247 	ret = smsc75xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL);
1248 	if (ret < 0) {
1249 		netdev_warn(dev->net, "Failed to write INT_STS: %d\n", ret);
1250 		return ret;
1251 	}
1252 
1253 	ret = smsc75xx_read_reg(dev, ID_REV, &buf);
1254 	if (ret < 0) {
1255 		netdev_warn(dev->net, "Failed to read ID_REV: %d\n", ret);
1256 		return ret;
1257 	}
1258 
1259 	netif_dbg(dev, ifup, dev->net, "ID_REV = 0x%08x\n", buf);
1260 
1261 	ret = smsc75xx_read_reg(dev, E2P_CMD, &buf);
1262 	if (ret < 0) {
1263 		netdev_warn(dev->net, "Failed to read E2P_CMD: %d\n", ret);
1264 		return ret;
1265 	}
1266 
1267 	/* only set default GPIO/LED settings if no EEPROM is detected */
1268 	if (!(buf & E2P_CMD_LOADED)) {
1269 		ret = smsc75xx_read_reg(dev, LED_GPIO_CFG, &buf);
1270 		if (ret < 0) {
1271 			netdev_warn(dev->net, "Failed to read LED_GPIO_CFG: %d\n", ret);
1272 			return ret;
1273 		}
1274 
1275 		buf &= ~(LED_GPIO_CFG_LED2_FUN_SEL | LED_GPIO_CFG_LED10_FUN_SEL);
1276 		buf |= LED_GPIO_CFG_LEDGPIO_EN | LED_GPIO_CFG_LED2_FUN_SEL;
1277 
1278 		ret = smsc75xx_write_reg(dev, LED_GPIO_CFG, buf);
1279 		if (ret < 0) {
1280 			netdev_warn(dev->net, "Failed to write LED_GPIO_CFG: %d\n", ret);
1281 			return ret;
1282 		}
1283 	}
1284 
1285 	ret = smsc75xx_write_reg(dev, FLOW, 0);
1286 	if (ret < 0) {
1287 		netdev_warn(dev->net, "Failed to write FLOW: %d\n", ret);
1288 		return ret;
1289 	}
1290 
1291 	ret = smsc75xx_write_reg(dev, FCT_FLOW, 0);
1292 	if (ret < 0) {
1293 		netdev_warn(dev->net, "Failed to write FCT_FLOW: %d\n", ret);
1294 		return ret;
1295 	}
1296 
1297 	/* Don't need rfe_ctl_lock during initialisation */
1298 	ret = smsc75xx_read_reg(dev, RFE_CTL, &pdata->rfe_ctl);
1299 	if (ret < 0) {
1300 		netdev_warn(dev->net, "Failed to read RFE_CTL: %d\n", ret);
1301 		return ret;
1302 	}
1303 
1304 	pdata->rfe_ctl |= RFE_CTL_AB | RFE_CTL_DPF;
1305 
1306 	ret = smsc75xx_write_reg(dev, RFE_CTL, pdata->rfe_ctl);
1307 	if (ret < 0) {
1308 		netdev_warn(dev->net, "Failed to write RFE_CTL: %d\n", ret);
1309 		return ret;
1310 	}
1311 
1312 	ret = smsc75xx_read_reg(dev, RFE_CTL, &pdata->rfe_ctl);
1313 	if (ret < 0) {
1314 		netdev_warn(dev->net, "Failed to read RFE_CTL: %d\n", ret);
1315 		return ret;
1316 	}
1317 
1318 	netif_dbg(dev, ifup, dev->net, "RFE_CTL set to 0x%08x\n",
1319 		  pdata->rfe_ctl);
1320 
1321 	/* Enable or disable checksum offload engines */
1322 	smsc75xx_set_features(dev->net, dev->net->features);
1323 
1324 	smsc75xx_set_multicast(dev->net);
1325 
1326 	ret = smsc75xx_phy_initialize(dev);
1327 	if (ret < 0) {
1328 		netdev_warn(dev->net, "Failed to initialize PHY: %d\n", ret);
1329 		return ret;
1330 	}
1331 
1332 	ret = smsc75xx_read_reg(dev, INT_EP_CTL, &buf);
1333 	if (ret < 0) {
1334 		netdev_warn(dev->net, "Failed to read INT_EP_CTL: %d\n", ret);
1335 		return ret;
1336 	}
1337 
1338 	/* enable PHY interrupts */
1339 	buf |= INT_ENP_PHY_INT;
1340 
1341 	ret = smsc75xx_write_reg(dev, INT_EP_CTL, buf);
1342 	if (ret < 0) {
1343 		netdev_warn(dev->net, "Failed to write INT_EP_CTL: %d\n", ret);
1344 		return ret;
1345 	}
1346 
1347 	/* allow mac to detect speed and duplex from phy */
1348 	ret = smsc75xx_read_reg(dev, MAC_CR, &buf);
1349 	if (ret < 0) {
1350 		netdev_warn(dev->net, "Failed to read MAC_CR: %d\n", ret);
1351 		return ret;
1352 	}
1353 
1354 	buf |= (MAC_CR_ADD | MAC_CR_ASD);
1355 	ret = smsc75xx_write_reg(dev, MAC_CR, buf);
1356 	if (ret < 0) {
1357 		netdev_warn(dev->net, "Failed to write MAC_CR: %d\n", ret);
1358 		return ret;
1359 	}
1360 
1361 	ret = smsc75xx_read_reg(dev, MAC_TX, &buf);
1362 	if (ret < 0) {
1363 		netdev_warn(dev->net, "Failed to read MAC_TX: %d\n", ret);
1364 		return ret;
1365 	}
1366 
1367 	buf |= MAC_TX_TXEN;
1368 
1369 	ret = smsc75xx_write_reg(dev, MAC_TX, buf);
1370 	if (ret < 0) {
1371 		netdev_warn(dev->net, "Failed to write MAC_TX: %d\n", ret);
1372 		return ret;
1373 	}
1374 
1375 	netif_dbg(dev, ifup, dev->net, "MAC_TX set to 0x%08x\n", buf);
1376 
1377 	ret = smsc75xx_read_reg(dev, FCT_TX_CTL, &buf);
1378 	if (ret < 0) {
1379 		netdev_warn(dev->net, "Failed to read FCT_TX_CTL: %d\n", ret);
1380 		return ret;
1381 	}
1382 
1383 	buf |= FCT_TX_CTL_EN;
1384 
1385 	ret = smsc75xx_write_reg(dev, FCT_TX_CTL, buf);
1386 	if (ret < 0) {
1387 		netdev_warn(dev->net, "Failed to write FCT_TX_CTL: %d\n", ret);
1388 		return ret;
1389 	}
1390 
1391 	netif_dbg(dev, ifup, dev->net, "FCT_TX_CTL set to 0x%08x\n", buf);
1392 
1393 	ret = smsc75xx_set_rx_max_frame_length(dev, dev->net->mtu + ETH_HLEN);
1394 	if (ret < 0) {
1395 		netdev_warn(dev->net, "Failed to set max rx frame length\n");
1396 		return ret;
1397 	}
1398 
1399 	ret = smsc75xx_read_reg(dev, MAC_RX, &buf);
1400 	if (ret < 0) {
1401 		netdev_warn(dev->net, "Failed to read MAC_RX: %d\n", ret);
1402 		return ret;
1403 	}
1404 
1405 	buf |= MAC_RX_RXEN;
1406 
1407 	ret = smsc75xx_write_reg(dev, MAC_RX, buf);
1408 	if (ret < 0) {
1409 		netdev_warn(dev->net, "Failed to write MAC_RX: %d\n", ret);
1410 		return ret;
1411 	}
1412 
1413 	netif_dbg(dev, ifup, dev->net, "MAC_RX set to 0x%08x\n", buf);
1414 
1415 	ret = smsc75xx_read_reg(dev, FCT_RX_CTL, &buf);
1416 	if (ret < 0) {
1417 		netdev_warn(dev->net, "Failed to read FCT_RX_CTL: %d\n", ret);
1418 		return ret;
1419 	}
1420 
1421 	buf |= FCT_RX_CTL_EN;
1422 
1423 	ret = smsc75xx_write_reg(dev, FCT_RX_CTL, buf);
1424 	if (ret < 0) {
1425 		netdev_warn(dev->net, "Failed to write FCT_RX_CTL: %d\n", ret);
1426 		return ret;
1427 	}
1428 
1429 	netif_dbg(dev, ifup, dev->net, "FCT_RX_CTL set to 0x%08x\n", buf);
1430 
1431 	netif_dbg(dev, ifup, dev->net, "smsc75xx_reset, return 0\n");
1432 	return 0;
1433 }
1434 
1435 static const struct net_device_ops smsc75xx_netdev_ops = {
1436 	.ndo_open		= usbnet_open,
1437 	.ndo_stop		= usbnet_stop,
1438 	.ndo_start_xmit		= usbnet_start_xmit,
1439 	.ndo_tx_timeout		= usbnet_tx_timeout,
1440 	.ndo_get_stats64	= dev_get_tstats64,
1441 	.ndo_change_mtu		= smsc75xx_change_mtu,
1442 	.ndo_set_mac_address 	= eth_mac_addr,
1443 	.ndo_validate_addr	= eth_validate_addr,
1444 	.ndo_eth_ioctl		= smsc75xx_ioctl,
1445 	.ndo_set_rx_mode	= smsc75xx_set_multicast,
1446 	.ndo_set_features	= smsc75xx_set_features,
1447 };
1448 
smsc75xx_bind(struct usbnet * dev,struct usb_interface * intf)1449 static int smsc75xx_bind(struct usbnet *dev, struct usb_interface *intf)
1450 {
1451 	struct smsc75xx_priv *pdata = NULL;
1452 	int ret;
1453 
1454 	printk(KERN_INFO SMSC_CHIPNAME " v" SMSC_DRIVER_VERSION "\n");
1455 
1456 	ret = usbnet_get_endpoints(dev, intf);
1457 	if (ret < 0) {
1458 		netdev_warn(dev->net, "usbnet_get_endpoints failed: %d\n", ret);
1459 		return ret;
1460 	}
1461 
1462 	dev->data[0] = (unsigned long)kzalloc(sizeof(struct smsc75xx_priv),
1463 					      GFP_KERNEL);
1464 
1465 	pdata = (struct smsc75xx_priv *)(dev->data[0]);
1466 	if (!pdata)
1467 		return -ENOMEM;
1468 
1469 	pdata->dev = dev;
1470 
1471 	spin_lock_init(&pdata->rfe_ctl_lock);
1472 	mutex_init(&pdata->dataport_mutex);
1473 
1474 	INIT_WORK(&pdata->set_multicast, smsc75xx_deferred_multicast_write);
1475 
1476 	if (DEFAULT_TX_CSUM_ENABLE)
1477 		dev->net->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
1478 
1479 	if (DEFAULT_RX_CSUM_ENABLE)
1480 		dev->net->features |= NETIF_F_RXCSUM;
1481 
1482 	dev->net->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
1483 				NETIF_F_RXCSUM;
1484 
1485 	ret = smsc75xx_wait_ready(dev, 0);
1486 	if (ret < 0) {
1487 		netdev_warn(dev->net, "device not ready in smsc75xx_bind\n");
1488 		goto free_pdata;
1489 	}
1490 
1491 	smsc75xx_init_mac_address(dev);
1492 
1493 	/* Init all registers */
1494 	ret = smsc75xx_reset(dev);
1495 	if (ret < 0) {
1496 		netdev_warn(dev->net, "smsc75xx_reset error %d\n", ret);
1497 		goto cancel_work;
1498 	}
1499 
1500 	dev->net->netdev_ops = &smsc75xx_netdev_ops;
1501 	dev->net->ethtool_ops = &smsc75xx_ethtool_ops;
1502 	dev->net->flags |= IFF_MULTICAST;
1503 	dev->net->hard_header_len += SMSC75XX_TX_OVERHEAD;
1504 	dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
1505 	dev->net->max_mtu = MAX_SINGLE_PACKET_SIZE;
1506 	return 0;
1507 
1508 cancel_work:
1509 	cancel_work_sync(&pdata->set_multicast);
1510 free_pdata:
1511 	kfree(pdata);
1512 	dev->data[0] = 0;
1513 	return ret;
1514 }
1515 
smsc75xx_unbind(struct usbnet * dev,struct usb_interface * intf)1516 static void smsc75xx_unbind(struct usbnet *dev, struct usb_interface *intf)
1517 {
1518 	struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
1519 	if (pdata) {
1520 		cancel_work_sync(&pdata->set_multicast);
1521 		netif_dbg(dev, ifdown, dev->net, "free pdata\n");
1522 		kfree(pdata);
1523 		dev->data[0] = 0;
1524 	}
1525 }
1526 
smsc_crc(const u8 * buffer,size_t len)1527 static u16 smsc_crc(const u8 *buffer, size_t len)
1528 {
1529 	return bitrev16(crc16(0xFFFF, buffer, len));
1530 }
1531 
smsc75xx_write_wuff(struct usbnet * dev,int filter,u32 wuf_cfg,u32 wuf_mask1)1532 static int smsc75xx_write_wuff(struct usbnet *dev, int filter, u32 wuf_cfg,
1533 			       u32 wuf_mask1)
1534 {
1535 	int cfg_base = WUF_CFGX + filter * 4;
1536 	int mask_base = WUF_MASKX + filter * 16;
1537 	int ret;
1538 
1539 	ret = smsc75xx_write_reg(dev, cfg_base, wuf_cfg);
1540 	if (ret < 0) {
1541 		netdev_warn(dev->net, "Error writing WUF_CFGX\n");
1542 		return ret;
1543 	}
1544 
1545 	ret = smsc75xx_write_reg(dev, mask_base, wuf_mask1);
1546 	if (ret < 0) {
1547 		netdev_warn(dev->net, "Error writing WUF_MASKX\n");
1548 		return ret;
1549 	}
1550 
1551 	ret = smsc75xx_write_reg(dev, mask_base + 4, 0);
1552 	if (ret < 0) {
1553 		netdev_warn(dev->net, "Error writing WUF_MASKX\n");
1554 		return ret;
1555 	}
1556 
1557 	ret = smsc75xx_write_reg(dev, mask_base + 8, 0);
1558 	if (ret < 0) {
1559 		netdev_warn(dev->net, "Error writing WUF_MASKX\n");
1560 		return ret;
1561 	}
1562 
1563 	ret = smsc75xx_write_reg(dev, mask_base + 12, 0);
1564 	if (ret < 0) {
1565 		netdev_warn(dev->net, "Error writing WUF_MASKX\n");
1566 		return ret;
1567 	}
1568 
1569 	return 0;
1570 }
1571 
smsc75xx_enter_suspend0(struct usbnet * dev)1572 static int smsc75xx_enter_suspend0(struct usbnet *dev)
1573 {
1574 	struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
1575 	u32 val;
1576 	int ret;
1577 
1578 	ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
1579 	if (ret < 0) {
1580 		netdev_warn(dev->net, "Error reading PMT_CTL\n");
1581 		return ret;
1582 	}
1583 
1584 	val &= (~(PMT_CTL_SUS_MODE | PMT_CTL_PHY_RST));
1585 	val |= PMT_CTL_SUS_MODE_0 | PMT_CTL_WOL_EN | PMT_CTL_WUPS;
1586 
1587 	ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
1588 	if (ret < 0) {
1589 		netdev_warn(dev->net, "Error writing PMT_CTL\n");
1590 		return ret;
1591 	}
1592 
1593 	pdata->suspend_flags |= SUSPEND_SUSPEND0;
1594 
1595 	return 0;
1596 }
1597 
smsc75xx_enter_suspend1(struct usbnet * dev)1598 static int smsc75xx_enter_suspend1(struct usbnet *dev)
1599 {
1600 	struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
1601 	u32 val;
1602 	int ret;
1603 
1604 	ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
1605 	if (ret < 0) {
1606 		netdev_warn(dev->net, "Error reading PMT_CTL\n");
1607 		return ret;
1608 	}
1609 
1610 	val &= ~(PMT_CTL_SUS_MODE | PMT_CTL_WUPS | PMT_CTL_PHY_RST);
1611 	val |= PMT_CTL_SUS_MODE_1;
1612 
1613 	ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
1614 	if (ret < 0) {
1615 		netdev_warn(dev->net, "Error writing PMT_CTL\n");
1616 		return ret;
1617 	}
1618 
1619 	/* clear wol status, enable energy detection */
1620 	val &= ~PMT_CTL_WUPS;
1621 	val |= (PMT_CTL_WUPS_ED | PMT_CTL_ED_EN);
1622 
1623 	ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
1624 	if (ret < 0) {
1625 		netdev_warn(dev->net, "Error writing PMT_CTL\n");
1626 		return ret;
1627 	}
1628 
1629 	pdata->suspend_flags |= SUSPEND_SUSPEND1;
1630 
1631 	return 0;
1632 }
1633 
smsc75xx_enter_suspend2(struct usbnet * dev)1634 static int smsc75xx_enter_suspend2(struct usbnet *dev)
1635 {
1636 	struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
1637 	u32 val;
1638 	int ret;
1639 
1640 	ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
1641 	if (ret < 0) {
1642 		netdev_warn(dev->net, "Error reading PMT_CTL\n");
1643 		return ret;
1644 	}
1645 
1646 	val &= ~(PMT_CTL_SUS_MODE | PMT_CTL_WUPS | PMT_CTL_PHY_RST);
1647 	val |= PMT_CTL_SUS_MODE_2;
1648 
1649 	ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
1650 	if (ret < 0) {
1651 		netdev_warn(dev->net, "Error writing PMT_CTL\n");
1652 		return ret;
1653 	}
1654 
1655 	pdata->suspend_flags |= SUSPEND_SUSPEND2;
1656 
1657 	return 0;
1658 }
1659 
smsc75xx_enter_suspend3(struct usbnet * dev)1660 static int smsc75xx_enter_suspend3(struct usbnet *dev)
1661 {
1662 	struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
1663 	u32 val;
1664 	int ret;
1665 
1666 	ret = smsc75xx_read_reg_nopm(dev, FCT_RX_CTL, &val);
1667 	if (ret < 0) {
1668 		netdev_warn(dev->net, "Error reading FCT_RX_CTL\n");
1669 		return ret;
1670 	}
1671 
1672 	if (val & FCT_RX_CTL_RXUSED) {
1673 		netdev_dbg(dev->net, "rx fifo not empty in autosuspend\n");
1674 		return -EBUSY;
1675 	}
1676 
1677 	ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
1678 	if (ret < 0) {
1679 		netdev_warn(dev->net, "Error reading PMT_CTL\n");
1680 		return ret;
1681 	}
1682 
1683 	val &= ~(PMT_CTL_SUS_MODE | PMT_CTL_WUPS | PMT_CTL_PHY_RST);
1684 	val |= PMT_CTL_SUS_MODE_3 | PMT_CTL_RES_CLR_WKP_EN;
1685 
1686 	ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
1687 	if (ret < 0) {
1688 		netdev_warn(dev->net, "Error writing PMT_CTL\n");
1689 		return ret;
1690 	}
1691 
1692 	/* clear wol status */
1693 	val &= ~PMT_CTL_WUPS;
1694 	val |= PMT_CTL_WUPS_WOL;
1695 
1696 	ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
1697 	if (ret < 0) {
1698 		netdev_warn(dev->net, "Error writing PMT_CTL\n");
1699 		return ret;
1700 	}
1701 
1702 	pdata->suspend_flags |= SUSPEND_SUSPEND3;
1703 
1704 	return 0;
1705 }
1706 
smsc75xx_enable_phy_wakeup_interrupts(struct usbnet * dev,u16 mask)1707 static int smsc75xx_enable_phy_wakeup_interrupts(struct usbnet *dev, u16 mask)
1708 {
1709 	struct mii_if_info *mii = &dev->mii;
1710 	int ret;
1711 
1712 	netdev_dbg(dev->net, "enabling PHY wakeup interrupts\n");
1713 
1714 	/* read to clear */
1715 	ret = smsc75xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_INT_SRC);
1716 	if (ret < 0) {
1717 		netdev_warn(dev->net, "Error reading PHY_INT_SRC\n");
1718 		return ret;
1719 	}
1720 
1721 	/* enable interrupt source */
1722 	ret = smsc75xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_INT_MASK);
1723 	if (ret < 0) {
1724 		netdev_warn(dev->net, "Error reading PHY_INT_MASK\n");
1725 		return ret;
1726 	}
1727 
1728 	ret |= mask;
1729 
1730 	smsc75xx_mdio_write_nopm(dev->net, mii->phy_id, PHY_INT_MASK, ret);
1731 
1732 	return 0;
1733 }
1734 
smsc75xx_link_ok_nopm(struct usbnet * dev)1735 static int smsc75xx_link_ok_nopm(struct usbnet *dev)
1736 {
1737 	struct mii_if_info *mii = &dev->mii;
1738 	int ret;
1739 
1740 	/* first, a dummy read, needed to latch some MII phys */
1741 	ret = smsc75xx_mdio_read_nopm(dev->net, mii->phy_id, MII_BMSR);
1742 	if (ret < 0) {
1743 		netdev_warn(dev->net, "Error reading MII_BMSR\n");
1744 		return ret;
1745 	}
1746 
1747 	ret = smsc75xx_mdio_read_nopm(dev->net, mii->phy_id, MII_BMSR);
1748 	if (ret < 0) {
1749 		netdev_warn(dev->net, "Error reading MII_BMSR\n");
1750 		return ret;
1751 	}
1752 
1753 	return !!(ret & BMSR_LSTATUS);
1754 }
1755 
smsc75xx_autosuspend(struct usbnet * dev,u32 link_up)1756 static int smsc75xx_autosuspend(struct usbnet *dev, u32 link_up)
1757 {
1758 	int ret;
1759 
1760 	if (!netif_running(dev->net)) {
1761 		/* interface is ifconfig down so fully power down hw */
1762 		netdev_dbg(dev->net, "autosuspend entering SUSPEND2\n");
1763 		return smsc75xx_enter_suspend2(dev);
1764 	}
1765 
1766 	if (!link_up) {
1767 		/* link is down so enter EDPD mode */
1768 		netdev_dbg(dev->net, "autosuspend entering SUSPEND1\n");
1769 
1770 		/* enable PHY wakeup events for if cable is attached */
1771 		ret = smsc75xx_enable_phy_wakeup_interrupts(dev,
1772 			PHY_INT_MASK_ANEG_COMP);
1773 		if (ret < 0) {
1774 			netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1775 			return ret;
1776 		}
1777 
1778 		netdev_info(dev->net, "entering SUSPEND1 mode\n");
1779 		return smsc75xx_enter_suspend1(dev);
1780 	}
1781 
1782 	/* enable PHY wakeup events so we remote wakeup if cable is pulled */
1783 	ret = smsc75xx_enable_phy_wakeup_interrupts(dev,
1784 		PHY_INT_MASK_LINK_DOWN);
1785 	if (ret < 0) {
1786 		netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1787 		return ret;
1788 	}
1789 
1790 	netdev_dbg(dev->net, "autosuspend entering SUSPEND3\n");
1791 	return smsc75xx_enter_suspend3(dev);
1792 }
1793 
smsc75xx_suspend(struct usb_interface * intf,pm_message_t message)1794 static int smsc75xx_suspend(struct usb_interface *intf, pm_message_t message)
1795 {
1796 	struct usbnet *dev = usb_get_intfdata(intf);
1797 	struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
1798 	u32 val, link_up;
1799 	int ret;
1800 
1801 	ret = usbnet_suspend(intf, message);
1802 	if (ret < 0) {
1803 		netdev_warn(dev->net, "usbnet_suspend error\n");
1804 		return ret;
1805 	}
1806 
1807 	if (pdata->suspend_flags) {
1808 		netdev_warn(dev->net, "error during last resume\n");
1809 		pdata->suspend_flags = 0;
1810 	}
1811 
1812 	/* determine if link is up using only _nopm functions */
1813 	link_up = smsc75xx_link_ok_nopm(dev);
1814 
1815 	if (message.event == PM_EVENT_AUTO_SUSPEND) {
1816 		ret = smsc75xx_autosuspend(dev, link_up);
1817 		goto done;
1818 	}
1819 
1820 	/* if we get this far we're not autosuspending */
1821 	/* if no wol options set, or if link is down and we're not waking on
1822 	 * PHY activity, enter lowest power SUSPEND2 mode
1823 	 */
1824 	if (!(pdata->wolopts & SUPPORTED_WAKE) ||
1825 		!(link_up || (pdata->wolopts & WAKE_PHY))) {
1826 		netdev_info(dev->net, "entering SUSPEND2 mode\n");
1827 
1828 		/* disable energy detect (link up) & wake up events */
1829 		ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
1830 		if (ret < 0) {
1831 			netdev_warn(dev->net, "Error reading WUCSR\n");
1832 			goto done;
1833 		}
1834 
1835 		val &= ~(WUCSR_MPEN | WUCSR_WUEN);
1836 
1837 		ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
1838 		if (ret < 0) {
1839 			netdev_warn(dev->net, "Error writing WUCSR\n");
1840 			goto done;
1841 		}
1842 
1843 		ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
1844 		if (ret < 0) {
1845 			netdev_warn(dev->net, "Error reading PMT_CTL\n");
1846 			goto done;
1847 		}
1848 
1849 		val &= ~(PMT_CTL_ED_EN | PMT_CTL_WOL_EN);
1850 
1851 		ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
1852 		if (ret < 0) {
1853 			netdev_warn(dev->net, "Error writing PMT_CTL\n");
1854 			goto done;
1855 		}
1856 
1857 		ret = smsc75xx_enter_suspend2(dev);
1858 		goto done;
1859 	}
1860 
1861 	if (pdata->wolopts & WAKE_PHY) {
1862 		ret = smsc75xx_enable_phy_wakeup_interrupts(dev,
1863 			(PHY_INT_MASK_ANEG_COMP | PHY_INT_MASK_LINK_DOWN));
1864 		if (ret < 0) {
1865 			netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1866 			goto done;
1867 		}
1868 
1869 		/* if link is down then configure EDPD and enter SUSPEND1,
1870 		 * otherwise enter SUSPEND0 below
1871 		 */
1872 		if (!link_up) {
1873 			struct mii_if_info *mii = &dev->mii;
1874 			netdev_info(dev->net, "entering SUSPEND1 mode\n");
1875 
1876 			/* enable energy detect power-down mode */
1877 			ret = smsc75xx_mdio_read_nopm(dev->net, mii->phy_id,
1878 				PHY_MODE_CTRL_STS);
1879 			if (ret < 0) {
1880 				netdev_warn(dev->net, "Error reading PHY_MODE_CTRL_STS\n");
1881 				goto done;
1882 			}
1883 
1884 			ret |= MODE_CTRL_STS_EDPWRDOWN;
1885 
1886 			smsc75xx_mdio_write_nopm(dev->net, mii->phy_id,
1887 				PHY_MODE_CTRL_STS, ret);
1888 
1889 			/* enter SUSPEND1 mode */
1890 			ret = smsc75xx_enter_suspend1(dev);
1891 			goto done;
1892 		}
1893 	}
1894 
1895 	if (pdata->wolopts & (WAKE_MCAST | WAKE_ARP)) {
1896 		int i, filter = 0;
1897 
1898 		/* disable all filters */
1899 		for (i = 0; i < WUF_NUM; i++) {
1900 			ret = smsc75xx_write_reg_nopm(dev, WUF_CFGX + i * 4, 0);
1901 			if (ret < 0) {
1902 				netdev_warn(dev->net, "Error writing WUF_CFGX\n");
1903 				goto done;
1904 			}
1905 		}
1906 
1907 		if (pdata->wolopts & WAKE_MCAST) {
1908 			const u8 mcast[] = {0x01, 0x00, 0x5E};
1909 			netdev_info(dev->net, "enabling multicast detection\n");
1910 
1911 			val = WUF_CFGX_EN | WUF_CFGX_ATYPE_MULTICAST
1912 				| smsc_crc(mcast, 3);
1913 			ret = smsc75xx_write_wuff(dev, filter++, val, 0x0007);
1914 			if (ret < 0) {
1915 				netdev_warn(dev->net, "Error writing wakeup filter\n");
1916 				goto done;
1917 			}
1918 		}
1919 
1920 		if (pdata->wolopts & WAKE_ARP) {
1921 			const u8 arp[] = {0x08, 0x06};
1922 			netdev_info(dev->net, "enabling ARP detection\n");
1923 
1924 			val = WUF_CFGX_EN | WUF_CFGX_ATYPE_ALL | (0x0C << 16)
1925 				| smsc_crc(arp, 2);
1926 			ret = smsc75xx_write_wuff(dev, filter++, val, 0x0003);
1927 			if (ret < 0) {
1928 				netdev_warn(dev->net, "Error writing wakeup filter\n");
1929 				goto done;
1930 			}
1931 		}
1932 
1933 		/* clear any pending pattern match packet status */
1934 		ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
1935 		if (ret < 0) {
1936 			netdev_warn(dev->net, "Error reading WUCSR\n");
1937 			goto done;
1938 		}
1939 
1940 		val |= WUCSR_WUFR;
1941 
1942 		ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
1943 		if (ret < 0) {
1944 			netdev_warn(dev->net, "Error writing WUCSR\n");
1945 			goto done;
1946 		}
1947 
1948 		netdev_info(dev->net, "enabling packet match detection\n");
1949 		ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
1950 		if (ret < 0) {
1951 			netdev_warn(dev->net, "Error reading WUCSR\n");
1952 			goto done;
1953 		}
1954 
1955 		val |= WUCSR_WUEN;
1956 
1957 		ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
1958 		if (ret < 0) {
1959 			netdev_warn(dev->net, "Error writing WUCSR\n");
1960 			goto done;
1961 		}
1962 	} else {
1963 		netdev_info(dev->net, "disabling packet match detection\n");
1964 		ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
1965 		if (ret < 0) {
1966 			netdev_warn(dev->net, "Error reading WUCSR\n");
1967 			goto done;
1968 		}
1969 
1970 		val &= ~WUCSR_WUEN;
1971 
1972 		ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
1973 		if (ret < 0) {
1974 			netdev_warn(dev->net, "Error writing WUCSR\n");
1975 			goto done;
1976 		}
1977 	}
1978 
1979 	/* disable magic, bcast & unicast wakeup sources */
1980 	ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
1981 	if (ret < 0) {
1982 		netdev_warn(dev->net, "Error reading WUCSR\n");
1983 		goto done;
1984 	}
1985 
1986 	val &= ~(WUCSR_MPEN | WUCSR_BCST_EN | WUCSR_PFDA_EN);
1987 
1988 	ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
1989 	if (ret < 0) {
1990 		netdev_warn(dev->net, "Error writing WUCSR\n");
1991 		goto done;
1992 	}
1993 
1994 	if (pdata->wolopts & WAKE_PHY) {
1995 		netdev_info(dev->net, "enabling PHY wakeup\n");
1996 
1997 		ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
1998 		if (ret < 0) {
1999 			netdev_warn(dev->net, "Error reading PMT_CTL\n");
2000 			goto done;
2001 		}
2002 
2003 		/* clear wol status, enable energy detection */
2004 		val &= ~PMT_CTL_WUPS;
2005 		val |= (PMT_CTL_WUPS_ED | PMT_CTL_ED_EN);
2006 
2007 		ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
2008 		if (ret < 0) {
2009 			netdev_warn(dev->net, "Error writing PMT_CTL\n");
2010 			goto done;
2011 		}
2012 	}
2013 
2014 	if (pdata->wolopts & WAKE_MAGIC) {
2015 		netdev_info(dev->net, "enabling magic packet wakeup\n");
2016 		ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
2017 		if (ret < 0) {
2018 			netdev_warn(dev->net, "Error reading WUCSR\n");
2019 			goto done;
2020 		}
2021 
2022 		/* clear any pending magic packet status */
2023 		val |= WUCSR_MPR | WUCSR_MPEN;
2024 
2025 		ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
2026 		if (ret < 0) {
2027 			netdev_warn(dev->net, "Error writing WUCSR\n");
2028 			goto done;
2029 		}
2030 	}
2031 
2032 	if (pdata->wolopts & WAKE_BCAST) {
2033 		netdev_info(dev->net, "enabling broadcast detection\n");
2034 		ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
2035 		if (ret < 0) {
2036 			netdev_warn(dev->net, "Error reading WUCSR\n");
2037 			goto done;
2038 		}
2039 
2040 		val |= WUCSR_BCAST_FR | WUCSR_BCST_EN;
2041 
2042 		ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
2043 		if (ret < 0) {
2044 			netdev_warn(dev->net, "Error writing WUCSR\n");
2045 			goto done;
2046 		}
2047 	}
2048 
2049 	if (pdata->wolopts & WAKE_UCAST) {
2050 		netdev_info(dev->net, "enabling unicast detection\n");
2051 		ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
2052 		if (ret < 0) {
2053 			netdev_warn(dev->net, "Error reading WUCSR\n");
2054 			goto done;
2055 		}
2056 
2057 		val |= WUCSR_WUFR | WUCSR_PFDA_EN;
2058 
2059 		ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
2060 		if (ret < 0) {
2061 			netdev_warn(dev->net, "Error writing WUCSR\n");
2062 			goto done;
2063 		}
2064 	}
2065 
2066 	/* enable receiver to enable frame reception */
2067 	ret = smsc75xx_read_reg_nopm(dev, MAC_RX, &val);
2068 	if (ret < 0) {
2069 		netdev_warn(dev->net, "Failed to read MAC_RX: %d\n", ret);
2070 		goto done;
2071 	}
2072 
2073 	val |= MAC_RX_RXEN;
2074 
2075 	ret = smsc75xx_write_reg_nopm(dev, MAC_RX, val);
2076 	if (ret < 0) {
2077 		netdev_warn(dev->net, "Failed to write MAC_RX: %d\n", ret);
2078 		goto done;
2079 	}
2080 
2081 	/* some wol options are enabled, so enter SUSPEND0 */
2082 	netdev_info(dev->net, "entering SUSPEND0 mode\n");
2083 	ret = smsc75xx_enter_suspend0(dev);
2084 
2085 done:
2086 	/*
2087 	 * TODO: resume() might need to handle the suspend failure
2088 	 * in system sleep
2089 	 */
2090 	if (ret && PMSG_IS_AUTO(message))
2091 		usbnet_resume(intf);
2092 	return ret;
2093 }
2094 
smsc75xx_resume(struct usb_interface * intf)2095 static int smsc75xx_resume(struct usb_interface *intf)
2096 {
2097 	struct usbnet *dev = usb_get_intfdata(intf);
2098 	struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
2099 	u8 suspend_flags = pdata->suspend_flags;
2100 	int ret;
2101 	u32 val;
2102 
2103 	netdev_dbg(dev->net, "resume suspend_flags=0x%02x\n", suspend_flags);
2104 
2105 	/* do this first to ensure it's cleared even in error case */
2106 	pdata->suspend_flags = 0;
2107 
2108 	if (suspend_flags & SUSPEND_ALLMODES) {
2109 		/* Disable wakeup sources */
2110 		ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
2111 		if (ret < 0) {
2112 			netdev_warn(dev->net, "Error reading WUCSR\n");
2113 			return ret;
2114 		}
2115 
2116 		val &= ~(WUCSR_WUEN | WUCSR_MPEN | WUCSR_PFDA_EN
2117 			| WUCSR_BCST_EN);
2118 
2119 		ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
2120 		if (ret < 0) {
2121 			netdev_warn(dev->net, "Error writing WUCSR\n");
2122 			return ret;
2123 		}
2124 
2125 		/* clear wake-up status */
2126 		ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
2127 		if (ret < 0) {
2128 			netdev_warn(dev->net, "Error reading PMT_CTL\n");
2129 			return ret;
2130 		}
2131 
2132 		val &= ~PMT_CTL_WOL_EN;
2133 		val |= PMT_CTL_WUPS;
2134 
2135 		ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
2136 		if (ret < 0) {
2137 			netdev_warn(dev->net, "Error writing PMT_CTL\n");
2138 			return ret;
2139 		}
2140 	}
2141 
2142 	if (suspend_flags & SUSPEND_SUSPEND2) {
2143 		netdev_info(dev->net, "resuming from SUSPEND2\n");
2144 
2145 		ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
2146 		if (ret < 0) {
2147 			netdev_warn(dev->net, "Error reading PMT_CTL\n");
2148 			return ret;
2149 		}
2150 
2151 		val |= PMT_CTL_PHY_PWRUP;
2152 
2153 		ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
2154 		if (ret < 0) {
2155 			netdev_warn(dev->net, "Error writing PMT_CTL\n");
2156 			return ret;
2157 		}
2158 	}
2159 
2160 	ret = smsc75xx_wait_ready(dev, 1);
2161 	if (ret < 0) {
2162 		netdev_warn(dev->net, "device not ready in smsc75xx_resume\n");
2163 		return ret;
2164 	}
2165 
2166 	return usbnet_resume(intf);
2167 }
2168 
smsc75xx_rx_csum_offload(struct usbnet * dev,struct sk_buff * skb,u32 rx_cmd_a,u32 rx_cmd_b)2169 static void smsc75xx_rx_csum_offload(struct usbnet *dev, struct sk_buff *skb,
2170 				     u32 rx_cmd_a, u32 rx_cmd_b)
2171 {
2172 	if (!(dev->net->features & NETIF_F_RXCSUM) ||
2173 	    unlikely(rx_cmd_a & RX_CMD_A_LCSM)) {
2174 		skb->ip_summed = CHECKSUM_NONE;
2175 	} else {
2176 		skb->csum = ntohs((u16)(rx_cmd_b >> RX_CMD_B_CSUM_SHIFT));
2177 		skb->ip_summed = CHECKSUM_COMPLETE;
2178 	}
2179 }
2180 
smsc75xx_rx_fixup(struct usbnet * dev,struct sk_buff * skb)2181 static int smsc75xx_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
2182 {
2183 	/* This check is no longer done by usbnet */
2184 	if (skb->len < dev->net->hard_header_len)
2185 		return 0;
2186 
2187 	while (skb->len > 0) {
2188 		u32 rx_cmd_a, rx_cmd_b, align_count, size;
2189 		struct sk_buff *ax_skb;
2190 		unsigned char *packet;
2191 
2192 		rx_cmd_a = get_unaligned_le32(skb->data);
2193 		skb_pull(skb, 4);
2194 
2195 		rx_cmd_b = get_unaligned_le32(skb->data);
2196 		skb_pull(skb, 4 + RXW_PADDING);
2197 
2198 		packet = skb->data;
2199 
2200 		/* get the packet length */
2201 		size = (rx_cmd_a & RX_CMD_A_LEN) - RXW_PADDING;
2202 		align_count = (4 - ((size + RXW_PADDING) % 4)) % 4;
2203 
2204 		if (unlikely(size > skb->len)) {
2205 			netif_dbg(dev, rx_err, dev->net,
2206 				  "size err rx_cmd_a=0x%08x\n",
2207 				  rx_cmd_a);
2208 			return 0;
2209 		}
2210 
2211 		if (unlikely(rx_cmd_a & RX_CMD_A_RED)) {
2212 			netif_dbg(dev, rx_err, dev->net,
2213 				  "Error rx_cmd_a=0x%08x\n", rx_cmd_a);
2214 			dev->net->stats.rx_errors++;
2215 			dev->net->stats.rx_dropped++;
2216 
2217 			if (rx_cmd_a & RX_CMD_A_FCS)
2218 				dev->net->stats.rx_crc_errors++;
2219 			else if (rx_cmd_a & (RX_CMD_A_LONG | RX_CMD_A_RUNT))
2220 				dev->net->stats.rx_frame_errors++;
2221 		} else {
2222 			/* MAX_SINGLE_PACKET_SIZE + 4(CRC) + 2(COE) + 4(Vlan) */
2223 			if (unlikely(size > (MAX_SINGLE_PACKET_SIZE + ETH_HLEN + 12))) {
2224 				netif_dbg(dev, rx_err, dev->net,
2225 					  "size err rx_cmd_a=0x%08x\n",
2226 					  rx_cmd_a);
2227 				return 0;
2228 			}
2229 
2230 			/* last frame in this batch */
2231 			if (skb->len == size) {
2232 				smsc75xx_rx_csum_offload(dev, skb, rx_cmd_a,
2233 					rx_cmd_b);
2234 
2235 				skb_trim(skb, skb->len - 4); /* remove fcs */
2236 				skb->truesize = size + sizeof(struct sk_buff);
2237 
2238 				return 1;
2239 			}
2240 
2241 			ax_skb = skb_clone(skb, GFP_ATOMIC);
2242 			if (unlikely(!ax_skb)) {
2243 				netdev_warn(dev->net, "Error allocating skb\n");
2244 				return 0;
2245 			}
2246 
2247 			ax_skb->len = size;
2248 			ax_skb->data = packet;
2249 			skb_set_tail_pointer(ax_skb, size);
2250 
2251 			smsc75xx_rx_csum_offload(dev, ax_skb, rx_cmd_a,
2252 				rx_cmd_b);
2253 
2254 			skb_trim(ax_skb, ax_skb->len - 4); /* remove fcs */
2255 			ax_skb->truesize = size + sizeof(struct sk_buff);
2256 
2257 			usbnet_skb_return(dev, ax_skb);
2258 		}
2259 
2260 		skb_pull(skb, size);
2261 
2262 		/* padding bytes before the next frame starts */
2263 		if (skb->len)
2264 			skb_pull(skb, align_count);
2265 	}
2266 
2267 	return 1;
2268 }
2269 
smsc75xx_tx_fixup(struct usbnet * dev,struct sk_buff * skb,gfp_t flags)2270 static struct sk_buff *smsc75xx_tx_fixup(struct usbnet *dev,
2271 					 struct sk_buff *skb, gfp_t flags)
2272 {
2273 	u32 tx_cmd_a, tx_cmd_b;
2274 	void *ptr;
2275 
2276 	if (skb_cow_head(skb, SMSC75XX_TX_OVERHEAD)) {
2277 		dev_kfree_skb_any(skb);
2278 		return NULL;
2279 	}
2280 
2281 	tx_cmd_a = (u32)(skb->len & TX_CMD_A_LEN) | TX_CMD_A_FCS;
2282 
2283 	if (skb->ip_summed == CHECKSUM_PARTIAL)
2284 		tx_cmd_a |= TX_CMD_A_IPE | TX_CMD_A_TPE;
2285 
2286 	if (skb_is_gso(skb)) {
2287 		u16 mss = max(skb_shinfo(skb)->gso_size, TX_MSS_MIN);
2288 		tx_cmd_b = (mss << TX_CMD_B_MSS_SHIFT) & TX_CMD_B_MSS;
2289 
2290 		tx_cmd_a |= TX_CMD_A_LSO;
2291 	} else {
2292 		tx_cmd_b = 0;
2293 	}
2294 
2295 	ptr = skb_push(skb, 8);
2296 	put_unaligned_le32(tx_cmd_a, ptr);
2297 	put_unaligned_le32(tx_cmd_b, ptr + 4);
2298 
2299 	return skb;
2300 }
2301 
smsc75xx_manage_power(struct usbnet * dev,int on)2302 static int smsc75xx_manage_power(struct usbnet *dev, int on)
2303 {
2304 	dev->intf->needs_remote_wakeup = on;
2305 	return 0;
2306 }
2307 
2308 static const struct driver_info smsc75xx_info = {
2309 	.description	= "smsc75xx USB 2.0 Gigabit Ethernet",
2310 	.bind		= smsc75xx_bind,
2311 	.unbind		= smsc75xx_unbind,
2312 	.link_reset	= smsc75xx_link_reset,
2313 	.reset		= smsc75xx_reset,
2314 	.rx_fixup	= smsc75xx_rx_fixup,
2315 	.tx_fixup	= smsc75xx_tx_fixup,
2316 	.status		= smsc75xx_status,
2317 	.manage_power	= smsc75xx_manage_power,
2318 	.flags		= FLAG_ETHER | FLAG_SEND_ZLP | FLAG_LINK_INTR,
2319 };
2320 
2321 static const struct usb_device_id products[] = {
2322 	{
2323 		/* SMSC7500 USB Gigabit Ethernet Device */
2324 		USB_DEVICE(USB_VENDOR_ID_SMSC, USB_PRODUCT_ID_LAN7500),
2325 		.driver_info = (unsigned long) &smsc75xx_info,
2326 	},
2327 	{
2328 		/* SMSC7500 USB Gigabit Ethernet Device */
2329 		USB_DEVICE(USB_VENDOR_ID_SMSC, USB_PRODUCT_ID_LAN7505),
2330 		.driver_info = (unsigned long) &smsc75xx_info,
2331 	},
2332 	{ },		/* END */
2333 };
2334 MODULE_DEVICE_TABLE(usb, products);
2335 
2336 static struct usb_driver smsc75xx_driver = {
2337 	.name		= SMSC_CHIPNAME,
2338 	.id_table	= products,
2339 	.probe		= usbnet_probe,
2340 	.suspend	= smsc75xx_suspend,
2341 	.resume		= smsc75xx_resume,
2342 	.reset_resume	= smsc75xx_resume,
2343 	.disconnect	= usbnet_disconnect,
2344 	.disable_hub_initiated_lpm = 1,
2345 	.supports_autosuspend = 1,
2346 };
2347 
2348 module_usb_driver(smsc75xx_driver);
2349 
2350 MODULE_AUTHOR("Nancy Lin");
2351 MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>");
2352 MODULE_DESCRIPTION("SMSC75XX USB 2.0 Gigabit Ethernet Devices");
2353 MODULE_LICENSE("GPL");
2354