xref: /openbmc/linux/drivers/net/usb/lan78xx.c (revision 249592bf)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2015 Microchip Technology
4  */
5 #include <linux/module.h>
6 #include <linux/netdevice.h>
7 #include <linux/etherdevice.h>
8 #include <linux/ethtool.h>
9 #include <linux/usb.h>
10 #include <linux/crc32.h>
11 #include <linux/signal.h>
12 #include <linux/slab.h>
13 #include <linux/if_vlan.h>
14 #include <linux/uaccess.h>
15 #include <linux/linkmode.h>
16 #include <linux/list.h>
17 #include <linux/ip.h>
18 #include <linux/ipv6.h>
19 #include <linux/mdio.h>
20 #include <linux/phy.h>
21 #include <net/ip6_checksum.h>
22 #include <net/vxlan.h>
23 #include <linux/interrupt.h>
24 #include <linux/irqdomain.h>
25 #include <linux/irq.h>
26 #include <linux/irqchip/chained_irq.h>
27 #include <linux/microchipphy.h>
28 #include <linux/phy_fixed.h>
29 #include <linux/of_mdio.h>
30 #include <linux/of_net.h>
31 #include "lan78xx.h"
32 
33 #define DRIVER_AUTHOR	"WOOJUNG HUH <woojung.huh@microchip.com>"
34 #define DRIVER_DESC	"LAN78XX USB 3.0 Gigabit Ethernet Devices"
35 #define DRIVER_NAME	"lan78xx"
36 
37 #define TX_TIMEOUT_JIFFIES		(5 * HZ)
38 #define THROTTLE_JIFFIES		(HZ / 8)
39 #define UNLINK_TIMEOUT_MS		3
40 
41 #define RX_MAX_QUEUE_MEMORY		(60 * 1518)
42 
43 #define SS_USB_PKT_SIZE			(1024)
44 #define HS_USB_PKT_SIZE			(512)
45 #define FS_USB_PKT_SIZE			(64)
46 
47 #define MAX_RX_FIFO_SIZE		(12 * 1024)
48 #define MAX_TX_FIFO_SIZE		(12 * 1024)
49 #define DEFAULT_BURST_CAP_SIZE		(MAX_TX_FIFO_SIZE)
50 #define DEFAULT_BULK_IN_DELAY		(0x0800)
51 #define MAX_SINGLE_PACKET_SIZE		(9000)
52 #define DEFAULT_TX_CSUM_ENABLE		(true)
53 #define DEFAULT_RX_CSUM_ENABLE		(true)
54 #define DEFAULT_TSO_CSUM_ENABLE		(true)
55 #define DEFAULT_VLAN_FILTER_ENABLE	(true)
56 #define DEFAULT_VLAN_RX_OFFLOAD		(true)
57 #define TX_OVERHEAD			(8)
58 #define RXW_PADDING			2
59 
60 #define LAN78XX_USB_VENDOR_ID		(0x0424)
61 #define LAN7800_USB_PRODUCT_ID		(0x7800)
62 #define LAN7850_USB_PRODUCT_ID		(0x7850)
63 #define LAN7801_USB_PRODUCT_ID		(0x7801)
64 #define LAN78XX_EEPROM_MAGIC		(0x78A5)
65 #define LAN78XX_OTP_MAGIC		(0x78F3)
66 
67 #define	MII_READ			1
68 #define	MII_WRITE			0
69 
70 #define EEPROM_INDICATOR		(0xA5)
71 #define EEPROM_MAC_OFFSET		(0x01)
72 #define MAX_EEPROM_SIZE			512
73 #define OTP_INDICATOR_1			(0xF3)
74 #define OTP_INDICATOR_2			(0xF7)
75 
76 #define WAKE_ALL			(WAKE_PHY | WAKE_UCAST | \
77 					 WAKE_MCAST | WAKE_BCAST | \
78 					 WAKE_ARP | WAKE_MAGIC)
79 
80 /* USB related defines */
81 #define BULK_IN_PIPE			1
82 #define BULK_OUT_PIPE			2
83 
84 /* default autosuspend delay (mSec)*/
85 #define DEFAULT_AUTOSUSPEND_DELAY	(10 * 1000)
86 
87 /* statistic update interval (mSec) */
88 #define STAT_UPDATE_TIMER		(1 * 1000)
89 
90 /* defines interrupts from interrupt EP */
91 #define MAX_INT_EP			(32)
92 #define INT_EP_INTEP			(31)
93 #define INT_EP_OTP_WR_DONE		(28)
94 #define INT_EP_EEE_TX_LPI_START		(26)
95 #define INT_EP_EEE_TX_LPI_STOP		(25)
96 #define INT_EP_EEE_RX_LPI		(24)
97 #define INT_EP_MAC_RESET_TIMEOUT	(23)
98 #define INT_EP_RDFO			(22)
99 #define INT_EP_TXE			(21)
100 #define INT_EP_USB_STATUS		(20)
101 #define INT_EP_TX_DIS			(19)
102 #define INT_EP_RX_DIS			(18)
103 #define INT_EP_PHY			(17)
104 #define INT_EP_DP			(16)
105 #define INT_EP_MAC_ERR			(15)
106 #define INT_EP_TDFU			(14)
107 #define INT_EP_TDFO			(13)
108 #define INT_EP_UTX			(12)
109 #define INT_EP_GPIO_11			(11)
110 #define INT_EP_GPIO_10			(10)
111 #define INT_EP_GPIO_9			(9)
112 #define INT_EP_GPIO_8			(8)
113 #define INT_EP_GPIO_7			(7)
114 #define INT_EP_GPIO_6			(6)
115 #define INT_EP_GPIO_5			(5)
116 #define INT_EP_GPIO_4			(4)
117 #define INT_EP_GPIO_3			(3)
118 #define INT_EP_GPIO_2			(2)
119 #define INT_EP_GPIO_1			(1)
120 #define INT_EP_GPIO_0			(0)
121 
122 static const char lan78xx_gstrings[][ETH_GSTRING_LEN] = {
123 	"RX FCS Errors",
124 	"RX Alignment Errors",
125 	"Rx Fragment Errors",
126 	"RX Jabber Errors",
127 	"RX Undersize Frame Errors",
128 	"RX Oversize Frame Errors",
129 	"RX Dropped Frames",
130 	"RX Unicast Byte Count",
131 	"RX Broadcast Byte Count",
132 	"RX Multicast Byte Count",
133 	"RX Unicast Frames",
134 	"RX Broadcast Frames",
135 	"RX Multicast Frames",
136 	"RX Pause Frames",
137 	"RX 64 Byte Frames",
138 	"RX 65 - 127 Byte Frames",
139 	"RX 128 - 255 Byte Frames",
140 	"RX 256 - 511 Bytes Frames",
141 	"RX 512 - 1023 Byte Frames",
142 	"RX 1024 - 1518 Byte Frames",
143 	"RX Greater 1518 Byte Frames",
144 	"EEE RX LPI Transitions",
145 	"EEE RX LPI Time",
146 	"TX FCS Errors",
147 	"TX Excess Deferral Errors",
148 	"TX Carrier Errors",
149 	"TX Bad Byte Count",
150 	"TX Single Collisions",
151 	"TX Multiple Collisions",
152 	"TX Excessive Collision",
153 	"TX Late Collisions",
154 	"TX Unicast Byte Count",
155 	"TX Broadcast Byte Count",
156 	"TX Multicast Byte Count",
157 	"TX Unicast Frames",
158 	"TX Broadcast Frames",
159 	"TX Multicast Frames",
160 	"TX Pause Frames",
161 	"TX 64 Byte Frames",
162 	"TX 65 - 127 Byte Frames",
163 	"TX 128 - 255 Byte Frames",
164 	"TX 256 - 511 Bytes Frames",
165 	"TX 512 - 1023 Byte Frames",
166 	"TX 1024 - 1518 Byte Frames",
167 	"TX Greater 1518 Byte Frames",
168 	"EEE TX LPI Transitions",
169 	"EEE TX LPI Time",
170 };
171 
172 struct lan78xx_statstage {
173 	u32 rx_fcs_errors;
174 	u32 rx_alignment_errors;
175 	u32 rx_fragment_errors;
176 	u32 rx_jabber_errors;
177 	u32 rx_undersize_frame_errors;
178 	u32 rx_oversize_frame_errors;
179 	u32 rx_dropped_frames;
180 	u32 rx_unicast_byte_count;
181 	u32 rx_broadcast_byte_count;
182 	u32 rx_multicast_byte_count;
183 	u32 rx_unicast_frames;
184 	u32 rx_broadcast_frames;
185 	u32 rx_multicast_frames;
186 	u32 rx_pause_frames;
187 	u32 rx_64_byte_frames;
188 	u32 rx_65_127_byte_frames;
189 	u32 rx_128_255_byte_frames;
190 	u32 rx_256_511_bytes_frames;
191 	u32 rx_512_1023_byte_frames;
192 	u32 rx_1024_1518_byte_frames;
193 	u32 rx_greater_1518_byte_frames;
194 	u32 eee_rx_lpi_transitions;
195 	u32 eee_rx_lpi_time;
196 	u32 tx_fcs_errors;
197 	u32 tx_excess_deferral_errors;
198 	u32 tx_carrier_errors;
199 	u32 tx_bad_byte_count;
200 	u32 tx_single_collisions;
201 	u32 tx_multiple_collisions;
202 	u32 tx_excessive_collision;
203 	u32 tx_late_collisions;
204 	u32 tx_unicast_byte_count;
205 	u32 tx_broadcast_byte_count;
206 	u32 tx_multicast_byte_count;
207 	u32 tx_unicast_frames;
208 	u32 tx_broadcast_frames;
209 	u32 tx_multicast_frames;
210 	u32 tx_pause_frames;
211 	u32 tx_64_byte_frames;
212 	u32 tx_65_127_byte_frames;
213 	u32 tx_128_255_byte_frames;
214 	u32 tx_256_511_bytes_frames;
215 	u32 tx_512_1023_byte_frames;
216 	u32 tx_1024_1518_byte_frames;
217 	u32 tx_greater_1518_byte_frames;
218 	u32 eee_tx_lpi_transitions;
219 	u32 eee_tx_lpi_time;
220 };
221 
222 struct lan78xx_statstage64 {
223 	u64 rx_fcs_errors;
224 	u64 rx_alignment_errors;
225 	u64 rx_fragment_errors;
226 	u64 rx_jabber_errors;
227 	u64 rx_undersize_frame_errors;
228 	u64 rx_oversize_frame_errors;
229 	u64 rx_dropped_frames;
230 	u64 rx_unicast_byte_count;
231 	u64 rx_broadcast_byte_count;
232 	u64 rx_multicast_byte_count;
233 	u64 rx_unicast_frames;
234 	u64 rx_broadcast_frames;
235 	u64 rx_multicast_frames;
236 	u64 rx_pause_frames;
237 	u64 rx_64_byte_frames;
238 	u64 rx_65_127_byte_frames;
239 	u64 rx_128_255_byte_frames;
240 	u64 rx_256_511_bytes_frames;
241 	u64 rx_512_1023_byte_frames;
242 	u64 rx_1024_1518_byte_frames;
243 	u64 rx_greater_1518_byte_frames;
244 	u64 eee_rx_lpi_transitions;
245 	u64 eee_rx_lpi_time;
246 	u64 tx_fcs_errors;
247 	u64 tx_excess_deferral_errors;
248 	u64 tx_carrier_errors;
249 	u64 tx_bad_byte_count;
250 	u64 tx_single_collisions;
251 	u64 tx_multiple_collisions;
252 	u64 tx_excessive_collision;
253 	u64 tx_late_collisions;
254 	u64 tx_unicast_byte_count;
255 	u64 tx_broadcast_byte_count;
256 	u64 tx_multicast_byte_count;
257 	u64 tx_unicast_frames;
258 	u64 tx_broadcast_frames;
259 	u64 tx_multicast_frames;
260 	u64 tx_pause_frames;
261 	u64 tx_64_byte_frames;
262 	u64 tx_65_127_byte_frames;
263 	u64 tx_128_255_byte_frames;
264 	u64 tx_256_511_bytes_frames;
265 	u64 tx_512_1023_byte_frames;
266 	u64 tx_1024_1518_byte_frames;
267 	u64 tx_greater_1518_byte_frames;
268 	u64 eee_tx_lpi_transitions;
269 	u64 eee_tx_lpi_time;
270 };
271 
272 static u32 lan78xx_regs[] = {
273 	ID_REV,
274 	INT_STS,
275 	HW_CFG,
276 	PMT_CTL,
277 	E2P_CMD,
278 	E2P_DATA,
279 	USB_STATUS,
280 	VLAN_TYPE,
281 	MAC_CR,
282 	MAC_RX,
283 	MAC_TX,
284 	FLOW,
285 	ERR_STS,
286 	MII_ACC,
287 	MII_DATA,
288 	EEE_TX_LPI_REQ_DLY,
289 	EEE_TW_TX_SYS,
290 	EEE_TX_LPI_REM_DLY,
291 	WUCSR
292 };
293 
294 #define PHY_REG_SIZE (32 * sizeof(u32))
295 
296 struct lan78xx_net;
297 
298 struct lan78xx_priv {
299 	struct lan78xx_net *dev;
300 	u32 rfe_ctl;
301 	u32 mchash_table[DP_SEL_VHF_HASH_LEN]; /* multicat hash table */
302 	u32 pfilter_table[NUM_OF_MAF][2]; /* perfect filter table */
303 	u32 vlan_table[DP_SEL_VHF_VLAN_LEN];
304 	struct mutex dataport_mutex; /* for dataport access */
305 	spinlock_t rfe_ctl_lock; /* for rfe register access */
306 	struct work_struct set_multicast;
307 	struct work_struct set_vlan;
308 	u32 wol;
309 };
310 
311 enum skb_state {
312 	illegal = 0,
313 	tx_start,
314 	tx_done,
315 	rx_start,
316 	rx_done,
317 	rx_cleanup,
318 	unlink_start
319 };
320 
321 struct skb_data {		/* skb->cb is one of these */
322 	struct urb *urb;
323 	struct lan78xx_net *dev;
324 	enum skb_state state;
325 	size_t length;
326 	int num_of_packet;
327 };
328 
329 struct usb_context {
330 	struct usb_ctrlrequest req;
331 	struct lan78xx_net *dev;
332 };
333 
334 #define EVENT_TX_HALT			0
335 #define EVENT_RX_HALT			1
336 #define EVENT_RX_MEMORY			2
337 #define EVENT_STS_SPLIT			3
338 #define EVENT_LINK_RESET		4
339 #define EVENT_RX_PAUSED			5
340 #define EVENT_DEV_WAKING		6
341 #define EVENT_DEV_ASLEEP		7
342 #define EVENT_DEV_OPEN			8
343 #define EVENT_STAT_UPDATE		9
344 
345 struct statstage {
346 	struct mutex			access_lock;	/* for stats access */
347 	struct lan78xx_statstage	saved;
348 	struct lan78xx_statstage	rollover_count;
349 	struct lan78xx_statstage	rollover_max;
350 	struct lan78xx_statstage64	curr_stat;
351 };
352 
353 struct irq_domain_data {
354 	struct irq_domain	*irqdomain;
355 	unsigned int		phyirq;
356 	struct irq_chip		*irqchip;
357 	irq_flow_handler_t	irq_handler;
358 	u32			irqenable;
359 	struct mutex		irq_lock;		/* for irq bus access */
360 };
361 
362 struct lan78xx_net {
363 	struct net_device	*net;
364 	struct usb_device	*udev;
365 	struct usb_interface	*intf;
366 	void			*driver_priv;
367 
368 	int			rx_qlen;
369 	int			tx_qlen;
370 	struct sk_buff_head	rxq;
371 	struct sk_buff_head	txq;
372 	struct sk_buff_head	done;
373 	struct sk_buff_head	rxq_pause;
374 	struct sk_buff_head	txq_pend;
375 
376 	struct tasklet_struct	bh;
377 	struct delayed_work	wq;
378 
379 	int			msg_enable;
380 
381 	struct urb		*urb_intr;
382 	struct usb_anchor	deferred;
383 
384 	struct mutex		phy_mutex; /* for phy access */
385 	unsigned		pipe_in, pipe_out, pipe_intr;
386 
387 	u32			hard_mtu;	/* count any extra framing */
388 	size_t			rx_urb_size;	/* size for rx urbs */
389 
390 	unsigned long		flags;
391 
392 	wait_queue_head_t	*wait;
393 	unsigned char		suspend_count;
394 
395 	unsigned		maxpacket;
396 	struct timer_list	delay;
397 	struct timer_list	stat_monitor;
398 
399 	unsigned long		data[5];
400 
401 	int			link_on;
402 	u8			mdix_ctrl;
403 
404 	u32			chipid;
405 	u32			chiprev;
406 	struct mii_bus		*mdiobus;
407 	phy_interface_t		interface;
408 
409 	int			fc_autoneg;
410 	u8			fc_request_control;
411 
412 	int			delta;
413 	struct statstage	stats;
414 
415 	struct irq_domain_data	domain_data;
416 };
417 
418 /* define external phy id */
419 #define	PHY_LAN8835			(0x0007C130)
420 #define	PHY_KSZ9031RNX			(0x00221620)
421 
422 /* use ethtool to change the level for any given device */
423 static int msg_level = -1;
424 module_param(msg_level, int, 0);
425 MODULE_PARM_DESC(msg_level, "Override default message level");
426 
427 static int lan78xx_read_reg(struct lan78xx_net *dev, u32 index, u32 *data)
428 {
429 	u32 *buf = kmalloc(sizeof(u32), GFP_KERNEL);
430 	int ret;
431 
432 	if (!buf)
433 		return -ENOMEM;
434 
435 	ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
436 			      USB_VENDOR_REQUEST_READ_REGISTER,
437 			      USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
438 			      0, index, buf, 4, USB_CTRL_GET_TIMEOUT);
439 	if (likely(ret >= 0)) {
440 		le32_to_cpus(buf);
441 		*data = *buf;
442 	} else {
443 		netdev_warn(dev->net,
444 			    "Failed to read register index 0x%08x. ret = %d",
445 			    index, ret);
446 	}
447 
448 	kfree(buf);
449 
450 	return ret;
451 }
452 
453 static int lan78xx_write_reg(struct lan78xx_net *dev, u32 index, u32 data)
454 {
455 	u32 *buf = kmalloc(sizeof(u32), GFP_KERNEL);
456 	int ret;
457 
458 	if (!buf)
459 		return -ENOMEM;
460 
461 	*buf = data;
462 	cpu_to_le32s(buf);
463 
464 	ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
465 			      USB_VENDOR_REQUEST_WRITE_REGISTER,
466 			      USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
467 			      0, index, buf, 4, USB_CTRL_SET_TIMEOUT);
468 	if (unlikely(ret < 0)) {
469 		netdev_warn(dev->net,
470 			    "Failed to write register index 0x%08x. ret = %d",
471 			    index, ret);
472 	}
473 
474 	kfree(buf);
475 
476 	return ret;
477 }
478 
479 static int lan78xx_read_stats(struct lan78xx_net *dev,
480 			      struct lan78xx_statstage *data)
481 {
482 	int ret = 0;
483 	int i;
484 	struct lan78xx_statstage *stats;
485 	u32 *src;
486 	u32 *dst;
487 
488 	stats = kmalloc(sizeof(*stats), GFP_KERNEL);
489 	if (!stats)
490 		return -ENOMEM;
491 
492 	ret = usb_control_msg(dev->udev,
493 			      usb_rcvctrlpipe(dev->udev, 0),
494 			      USB_VENDOR_REQUEST_GET_STATS,
495 			      USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
496 			      0,
497 			      0,
498 			      (void *)stats,
499 			      sizeof(*stats),
500 			      USB_CTRL_SET_TIMEOUT);
501 	if (likely(ret >= 0)) {
502 		src = (u32 *)stats;
503 		dst = (u32 *)data;
504 		for (i = 0; i < sizeof(*stats)/sizeof(u32); i++) {
505 			le32_to_cpus(&src[i]);
506 			dst[i] = src[i];
507 		}
508 	} else {
509 		netdev_warn(dev->net,
510 			    "Failed to read stat ret = %d", ret);
511 	}
512 
513 	kfree(stats);
514 
515 	return ret;
516 }
517 
518 #define check_counter_rollover(struct1, dev_stats, member) {	\
519 	if (struct1->member < dev_stats.saved.member)		\
520 		dev_stats.rollover_count.member++;		\
521 	}
522 
523 static void lan78xx_check_stat_rollover(struct lan78xx_net *dev,
524 					struct lan78xx_statstage *stats)
525 {
526 	check_counter_rollover(stats, dev->stats, rx_fcs_errors);
527 	check_counter_rollover(stats, dev->stats, rx_alignment_errors);
528 	check_counter_rollover(stats, dev->stats, rx_fragment_errors);
529 	check_counter_rollover(stats, dev->stats, rx_jabber_errors);
530 	check_counter_rollover(stats, dev->stats, rx_undersize_frame_errors);
531 	check_counter_rollover(stats, dev->stats, rx_oversize_frame_errors);
532 	check_counter_rollover(stats, dev->stats, rx_dropped_frames);
533 	check_counter_rollover(stats, dev->stats, rx_unicast_byte_count);
534 	check_counter_rollover(stats, dev->stats, rx_broadcast_byte_count);
535 	check_counter_rollover(stats, dev->stats, rx_multicast_byte_count);
536 	check_counter_rollover(stats, dev->stats, rx_unicast_frames);
537 	check_counter_rollover(stats, dev->stats, rx_broadcast_frames);
538 	check_counter_rollover(stats, dev->stats, rx_multicast_frames);
539 	check_counter_rollover(stats, dev->stats, rx_pause_frames);
540 	check_counter_rollover(stats, dev->stats, rx_64_byte_frames);
541 	check_counter_rollover(stats, dev->stats, rx_65_127_byte_frames);
542 	check_counter_rollover(stats, dev->stats, rx_128_255_byte_frames);
543 	check_counter_rollover(stats, dev->stats, rx_256_511_bytes_frames);
544 	check_counter_rollover(stats, dev->stats, rx_512_1023_byte_frames);
545 	check_counter_rollover(stats, dev->stats, rx_1024_1518_byte_frames);
546 	check_counter_rollover(stats, dev->stats, rx_greater_1518_byte_frames);
547 	check_counter_rollover(stats, dev->stats, eee_rx_lpi_transitions);
548 	check_counter_rollover(stats, dev->stats, eee_rx_lpi_time);
549 	check_counter_rollover(stats, dev->stats, tx_fcs_errors);
550 	check_counter_rollover(stats, dev->stats, tx_excess_deferral_errors);
551 	check_counter_rollover(stats, dev->stats, tx_carrier_errors);
552 	check_counter_rollover(stats, dev->stats, tx_bad_byte_count);
553 	check_counter_rollover(stats, dev->stats, tx_single_collisions);
554 	check_counter_rollover(stats, dev->stats, tx_multiple_collisions);
555 	check_counter_rollover(stats, dev->stats, tx_excessive_collision);
556 	check_counter_rollover(stats, dev->stats, tx_late_collisions);
557 	check_counter_rollover(stats, dev->stats, tx_unicast_byte_count);
558 	check_counter_rollover(stats, dev->stats, tx_broadcast_byte_count);
559 	check_counter_rollover(stats, dev->stats, tx_multicast_byte_count);
560 	check_counter_rollover(stats, dev->stats, tx_unicast_frames);
561 	check_counter_rollover(stats, dev->stats, tx_broadcast_frames);
562 	check_counter_rollover(stats, dev->stats, tx_multicast_frames);
563 	check_counter_rollover(stats, dev->stats, tx_pause_frames);
564 	check_counter_rollover(stats, dev->stats, tx_64_byte_frames);
565 	check_counter_rollover(stats, dev->stats, tx_65_127_byte_frames);
566 	check_counter_rollover(stats, dev->stats, tx_128_255_byte_frames);
567 	check_counter_rollover(stats, dev->stats, tx_256_511_bytes_frames);
568 	check_counter_rollover(stats, dev->stats, tx_512_1023_byte_frames);
569 	check_counter_rollover(stats, dev->stats, tx_1024_1518_byte_frames);
570 	check_counter_rollover(stats, dev->stats, tx_greater_1518_byte_frames);
571 	check_counter_rollover(stats, dev->stats, eee_tx_lpi_transitions);
572 	check_counter_rollover(stats, dev->stats, eee_tx_lpi_time);
573 
574 	memcpy(&dev->stats.saved, stats, sizeof(struct lan78xx_statstage));
575 }
576 
577 static void lan78xx_update_stats(struct lan78xx_net *dev)
578 {
579 	u32 *p, *count, *max;
580 	u64 *data;
581 	int i;
582 	struct lan78xx_statstage lan78xx_stats;
583 
584 	if (usb_autopm_get_interface(dev->intf) < 0)
585 		return;
586 
587 	p = (u32 *)&lan78xx_stats;
588 	count = (u32 *)&dev->stats.rollover_count;
589 	max = (u32 *)&dev->stats.rollover_max;
590 	data = (u64 *)&dev->stats.curr_stat;
591 
592 	mutex_lock(&dev->stats.access_lock);
593 
594 	if (lan78xx_read_stats(dev, &lan78xx_stats) > 0)
595 		lan78xx_check_stat_rollover(dev, &lan78xx_stats);
596 
597 	for (i = 0; i < (sizeof(lan78xx_stats) / (sizeof(u32))); i++)
598 		data[i] = (u64)p[i] + ((u64)count[i] * ((u64)max[i] + 1));
599 
600 	mutex_unlock(&dev->stats.access_lock);
601 
602 	usb_autopm_put_interface(dev->intf);
603 }
604 
605 /* Loop until the read is completed with timeout called with phy_mutex held */
606 static int lan78xx_phy_wait_not_busy(struct lan78xx_net *dev)
607 {
608 	unsigned long start_time = jiffies;
609 	u32 val;
610 	int ret;
611 
612 	do {
613 		ret = lan78xx_read_reg(dev, MII_ACC, &val);
614 		if (unlikely(ret < 0))
615 			return -EIO;
616 
617 		if (!(val & MII_ACC_MII_BUSY_))
618 			return 0;
619 	} while (!time_after(jiffies, start_time + HZ));
620 
621 	return -EIO;
622 }
623 
624 static inline u32 mii_access(int id, int index, int read)
625 {
626 	u32 ret;
627 
628 	ret = ((u32)id << MII_ACC_PHY_ADDR_SHIFT_) & MII_ACC_PHY_ADDR_MASK_;
629 	ret |= ((u32)index << MII_ACC_MIIRINDA_SHIFT_) & MII_ACC_MIIRINDA_MASK_;
630 	if (read)
631 		ret |= MII_ACC_MII_READ_;
632 	else
633 		ret |= MII_ACC_MII_WRITE_;
634 	ret |= MII_ACC_MII_BUSY_;
635 
636 	return ret;
637 }
638 
639 static int lan78xx_wait_eeprom(struct lan78xx_net *dev)
640 {
641 	unsigned long start_time = jiffies;
642 	u32 val;
643 	int ret;
644 
645 	do {
646 		ret = lan78xx_read_reg(dev, E2P_CMD, &val);
647 		if (unlikely(ret < 0))
648 			return -EIO;
649 
650 		if (!(val & E2P_CMD_EPC_BUSY_) ||
651 		    (val & E2P_CMD_EPC_TIMEOUT_))
652 			break;
653 		usleep_range(40, 100);
654 	} while (!time_after(jiffies, start_time + HZ));
655 
656 	if (val & (E2P_CMD_EPC_TIMEOUT_ | E2P_CMD_EPC_BUSY_)) {
657 		netdev_warn(dev->net, "EEPROM read operation timeout");
658 		return -EIO;
659 	}
660 
661 	return 0;
662 }
663 
664 static int lan78xx_eeprom_confirm_not_busy(struct lan78xx_net *dev)
665 {
666 	unsigned long start_time = jiffies;
667 	u32 val;
668 	int ret;
669 
670 	do {
671 		ret = lan78xx_read_reg(dev, E2P_CMD, &val);
672 		if (unlikely(ret < 0))
673 			return -EIO;
674 
675 		if (!(val & E2P_CMD_EPC_BUSY_))
676 			return 0;
677 
678 		usleep_range(40, 100);
679 	} while (!time_after(jiffies, start_time + HZ));
680 
681 	netdev_warn(dev->net, "EEPROM is busy");
682 	return -EIO;
683 }
684 
685 static int lan78xx_read_raw_eeprom(struct lan78xx_net *dev, u32 offset,
686 				   u32 length, u8 *data)
687 {
688 	u32 val;
689 	u32 saved;
690 	int i, ret;
691 	int retval;
692 
693 	/* depends on chip, some EEPROM pins are muxed with LED function.
694 	 * disable & restore LED function to access EEPROM.
695 	 */
696 	ret = lan78xx_read_reg(dev, HW_CFG, &val);
697 	saved = val;
698 	if (dev->chipid == ID_REV_CHIP_ID_7800_) {
699 		val &= ~(HW_CFG_LED1_EN_ | HW_CFG_LED0_EN_);
700 		ret = lan78xx_write_reg(dev, HW_CFG, val);
701 	}
702 
703 	retval = lan78xx_eeprom_confirm_not_busy(dev);
704 	if (retval)
705 		return retval;
706 
707 	for (i = 0; i < length; i++) {
708 		val = E2P_CMD_EPC_BUSY_ | E2P_CMD_EPC_CMD_READ_;
709 		val |= (offset & E2P_CMD_EPC_ADDR_MASK_);
710 		ret = lan78xx_write_reg(dev, E2P_CMD, val);
711 		if (unlikely(ret < 0)) {
712 			retval = -EIO;
713 			goto exit;
714 		}
715 
716 		retval = lan78xx_wait_eeprom(dev);
717 		if (retval < 0)
718 			goto exit;
719 
720 		ret = lan78xx_read_reg(dev, E2P_DATA, &val);
721 		if (unlikely(ret < 0)) {
722 			retval = -EIO;
723 			goto exit;
724 		}
725 
726 		data[i] = val & 0xFF;
727 		offset++;
728 	}
729 
730 	retval = 0;
731 exit:
732 	if (dev->chipid == ID_REV_CHIP_ID_7800_)
733 		ret = lan78xx_write_reg(dev, HW_CFG, saved);
734 
735 	return retval;
736 }
737 
738 static int lan78xx_read_eeprom(struct lan78xx_net *dev, u32 offset,
739 			       u32 length, u8 *data)
740 {
741 	u8 sig;
742 	int ret;
743 
744 	ret = lan78xx_read_raw_eeprom(dev, 0, 1, &sig);
745 	if ((ret == 0) && (sig == EEPROM_INDICATOR))
746 		ret = lan78xx_read_raw_eeprom(dev, offset, length, data);
747 	else
748 		ret = -EINVAL;
749 
750 	return ret;
751 }
752 
753 static int lan78xx_write_raw_eeprom(struct lan78xx_net *dev, u32 offset,
754 				    u32 length, u8 *data)
755 {
756 	u32 val;
757 	u32 saved;
758 	int i, ret;
759 	int retval;
760 
761 	/* depends on chip, some EEPROM pins are muxed with LED function.
762 	 * disable & restore LED function to access EEPROM.
763 	 */
764 	ret = lan78xx_read_reg(dev, HW_CFG, &val);
765 	saved = val;
766 	if (dev->chipid == ID_REV_CHIP_ID_7800_) {
767 		val &= ~(HW_CFG_LED1_EN_ | HW_CFG_LED0_EN_);
768 		ret = lan78xx_write_reg(dev, HW_CFG, val);
769 	}
770 
771 	retval = lan78xx_eeprom_confirm_not_busy(dev);
772 	if (retval)
773 		goto exit;
774 
775 	/* Issue write/erase enable command */
776 	val = E2P_CMD_EPC_BUSY_ | E2P_CMD_EPC_CMD_EWEN_;
777 	ret = lan78xx_write_reg(dev, E2P_CMD, val);
778 	if (unlikely(ret < 0)) {
779 		retval = -EIO;
780 		goto exit;
781 	}
782 
783 	retval = lan78xx_wait_eeprom(dev);
784 	if (retval < 0)
785 		goto exit;
786 
787 	for (i = 0; i < length; i++) {
788 		/* Fill data register */
789 		val = data[i];
790 		ret = lan78xx_write_reg(dev, E2P_DATA, val);
791 		if (ret < 0) {
792 			retval = -EIO;
793 			goto exit;
794 		}
795 
796 		/* Send "write" command */
797 		val = E2P_CMD_EPC_BUSY_ | E2P_CMD_EPC_CMD_WRITE_;
798 		val |= (offset & E2P_CMD_EPC_ADDR_MASK_);
799 		ret = lan78xx_write_reg(dev, E2P_CMD, val);
800 		if (ret < 0) {
801 			retval = -EIO;
802 			goto exit;
803 		}
804 
805 		retval = lan78xx_wait_eeprom(dev);
806 		if (retval < 0)
807 			goto exit;
808 
809 		offset++;
810 	}
811 
812 	retval = 0;
813 exit:
814 	if (dev->chipid == ID_REV_CHIP_ID_7800_)
815 		ret = lan78xx_write_reg(dev, HW_CFG, saved);
816 
817 	return retval;
818 }
819 
820 static int lan78xx_read_raw_otp(struct lan78xx_net *dev, u32 offset,
821 				u32 length, u8 *data)
822 {
823 	int i;
824 	u32 buf;
825 	unsigned long timeout;
826 
827 	lan78xx_read_reg(dev, OTP_PWR_DN, &buf);
828 
829 	if (buf & OTP_PWR_DN_PWRDN_N_) {
830 		/* clear it and wait to be cleared */
831 		lan78xx_write_reg(dev, OTP_PWR_DN, 0);
832 
833 		timeout = jiffies + HZ;
834 		do {
835 			usleep_range(1, 10);
836 			lan78xx_read_reg(dev, OTP_PWR_DN, &buf);
837 			if (time_after(jiffies, timeout)) {
838 				netdev_warn(dev->net,
839 					    "timeout on OTP_PWR_DN");
840 				return -EIO;
841 			}
842 		} while (buf & OTP_PWR_DN_PWRDN_N_);
843 	}
844 
845 	for (i = 0; i < length; i++) {
846 		lan78xx_write_reg(dev, OTP_ADDR1,
847 					((offset + i) >> 8) & OTP_ADDR1_15_11);
848 		lan78xx_write_reg(dev, OTP_ADDR2,
849 					((offset + i) & OTP_ADDR2_10_3));
850 
851 		lan78xx_write_reg(dev, OTP_FUNC_CMD, OTP_FUNC_CMD_READ_);
852 		lan78xx_write_reg(dev, OTP_CMD_GO, OTP_CMD_GO_GO_);
853 
854 		timeout = jiffies + HZ;
855 		do {
856 			udelay(1);
857 			lan78xx_read_reg(dev, OTP_STATUS, &buf);
858 			if (time_after(jiffies, timeout)) {
859 				netdev_warn(dev->net,
860 					    "timeout on OTP_STATUS");
861 				return -EIO;
862 			}
863 		} while (buf & OTP_STATUS_BUSY_);
864 
865 		lan78xx_read_reg(dev, OTP_RD_DATA, &buf);
866 
867 		data[i] = (u8)(buf & 0xFF);
868 	}
869 
870 	return 0;
871 }
872 
873 static int lan78xx_write_raw_otp(struct lan78xx_net *dev, u32 offset,
874 				 u32 length, u8 *data)
875 {
876 	int i;
877 	u32 buf;
878 	unsigned long timeout;
879 
880 	lan78xx_read_reg(dev, OTP_PWR_DN, &buf);
881 
882 	if (buf & OTP_PWR_DN_PWRDN_N_) {
883 		/* clear it and wait to be cleared */
884 		lan78xx_write_reg(dev, OTP_PWR_DN, 0);
885 
886 		timeout = jiffies + HZ;
887 		do {
888 			udelay(1);
889 			lan78xx_read_reg(dev, OTP_PWR_DN, &buf);
890 			if (time_after(jiffies, timeout)) {
891 				netdev_warn(dev->net,
892 					    "timeout on OTP_PWR_DN completion");
893 				return -EIO;
894 			}
895 		} while (buf & OTP_PWR_DN_PWRDN_N_);
896 	}
897 
898 	/* set to BYTE program mode */
899 	lan78xx_write_reg(dev, OTP_PRGM_MODE, OTP_PRGM_MODE_BYTE_);
900 
901 	for (i = 0; i < length; i++) {
902 		lan78xx_write_reg(dev, OTP_ADDR1,
903 					((offset + i) >> 8) & OTP_ADDR1_15_11);
904 		lan78xx_write_reg(dev, OTP_ADDR2,
905 					((offset + i) & OTP_ADDR2_10_3));
906 		lan78xx_write_reg(dev, OTP_PRGM_DATA, data[i]);
907 		lan78xx_write_reg(dev, OTP_TST_CMD, OTP_TST_CMD_PRGVRFY_);
908 		lan78xx_write_reg(dev, OTP_CMD_GO, OTP_CMD_GO_GO_);
909 
910 		timeout = jiffies + HZ;
911 		do {
912 			udelay(1);
913 			lan78xx_read_reg(dev, OTP_STATUS, &buf);
914 			if (time_after(jiffies, timeout)) {
915 				netdev_warn(dev->net,
916 					    "Timeout on OTP_STATUS completion");
917 				return -EIO;
918 			}
919 		} while (buf & OTP_STATUS_BUSY_);
920 	}
921 
922 	return 0;
923 }
924 
925 static int lan78xx_read_otp(struct lan78xx_net *dev, u32 offset,
926 			    u32 length, u8 *data)
927 {
928 	u8 sig;
929 	int ret;
930 
931 	ret = lan78xx_read_raw_otp(dev, 0, 1, &sig);
932 
933 	if (ret == 0) {
934 		if (sig == OTP_INDICATOR_2)
935 			offset += 0x100;
936 		else if (sig != OTP_INDICATOR_1)
937 			ret = -EINVAL;
938 		if (!ret)
939 			ret = lan78xx_read_raw_otp(dev, offset, length, data);
940 	}
941 
942 	return ret;
943 }
944 
945 static int lan78xx_dataport_wait_not_busy(struct lan78xx_net *dev)
946 {
947 	int i, ret;
948 
949 	for (i = 0; i < 100; i++) {
950 		u32 dp_sel;
951 
952 		ret = lan78xx_read_reg(dev, DP_SEL, &dp_sel);
953 		if (unlikely(ret < 0))
954 			return -EIO;
955 
956 		if (dp_sel & DP_SEL_DPRDY_)
957 			return 0;
958 
959 		usleep_range(40, 100);
960 	}
961 
962 	netdev_warn(dev->net, "lan78xx_dataport_wait_not_busy timed out");
963 
964 	return -EIO;
965 }
966 
967 static int lan78xx_dataport_write(struct lan78xx_net *dev, u32 ram_select,
968 				  u32 addr, u32 length, u32 *buf)
969 {
970 	struct lan78xx_priv *pdata = (struct lan78xx_priv *)(dev->data[0]);
971 	u32 dp_sel;
972 	int i, ret;
973 
974 	if (usb_autopm_get_interface(dev->intf) < 0)
975 			return 0;
976 
977 	mutex_lock(&pdata->dataport_mutex);
978 
979 	ret = lan78xx_dataport_wait_not_busy(dev);
980 	if (ret < 0)
981 		goto done;
982 
983 	ret = lan78xx_read_reg(dev, DP_SEL, &dp_sel);
984 
985 	dp_sel &= ~DP_SEL_RSEL_MASK_;
986 	dp_sel |= ram_select;
987 	ret = lan78xx_write_reg(dev, DP_SEL, dp_sel);
988 
989 	for (i = 0; i < length; i++) {
990 		ret = lan78xx_write_reg(dev, DP_ADDR, addr + i);
991 
992 		ret = lan78xx_write_reg(dev, DP_DATA, buf[i]);
993 
994 		ret = lan78xx_write_reg(dev, DP_CMD, DP_CMD_WRITE_);
995 
996 		ret = lan78xx_dataport_wait_not_busy(dev);
997 		if (ret < 0)
998 			goto done;
999 	}
1000 
1001 done:
1002 	mutex_unlock(&pdata->dataport_mutex);
1003 	usb_autopm_put_interface(dev->intf);
1004 
1005 	return ret;
1006 }
1007 
1008 static void lan78xx_set_addr_filter(struct lan78xx_priv *pdata,
1009 				    int index, u8 addr[ETH_ALEN])
1010 {
1011 	u32 temp;
1012 
1013 	if ((pdata) && (index > 0) && (index < NUM_OF_MAF)) {
1014 		temp = addr[3];
1015 		temp = addr[2] | (temp << 8);
1016 		temp = addr[1] | (temp << 8);
1017 		temp = addr[0] | (temp << 8);
1018 		pdata->pfilter_table[index][1] = temp;
1019 		temp = addr[5];
1020 		temp = addr[4] | (temp << 8);
1021 		temp |= MAF_HI_VALID_ | MAF_HI_TYPE_DST_;
1022 		pdata->pfilter_table[index][0] = temp;
1023 	}
1024 }
1025 
1026 /* returns hash bit number for given MAC address */
1027 static inline u32 lan78xx_hash(char addr[ETH_ALEN])
1028 {
1029 	return (ether_crc(ETH_ALEN, addr) >> 23) & 0x1ff;
1030 }
1031 
1032 static void lan78xx_deferred_multicast_write(struct work_struct *param)
1033 {
1034 	struct lan78xx_priv *pdata =
1035 			container_of(param, struct lan78xx_priv, set_multicast);
1036 	struct lan78xx_net *dev = pdata->dev;
1037 	int i;
1038 
1039 	netif_dbg(dev, drv, dev->net, "deferred multicast write 0x%08x\n",
1040 		  pdata->rfe_ctl);
1041 
1042 	lan78xx_dataport_write(dev, DP_SEL_RSEL_VLAN_DA_, DP_SEL_VHF_VLAN_LEN,
1043 			       DP_SEL_VHF_HASH_LEN, pdata->mchash_table);
1044 
1045 	for (i = 1; i < NUM_OF_MAF; i++) {
1046 		lan78xx_write_reg(dev, MAF_HI(i), 0);
1047 		lan78xx_write_reg(dev, MAF_LO(i),
1048 					pdata->pfilter_table[i][1]);
1049 		lan78xx_write_reg(dev, MAF_HI(i),
1050 					pdata->pfilter_table[i][0]);
1051 	}
1052 
1053 	lan78xx_write_reg(dev, RFE_CTL, pdata->rfe_ctl);
1054 }
1055 
1056 static void lan78xx_set_multicast(struct net_device *netdev)
1057 {
1058 	struct lan78xx_net *dev = netdev_priv(netdev);
1059 	struct lan78xx_priv *pdata = (struct lan78xx_priv *)(dev->data[0]);
1060 	unsigned long flags;
1061 	int i;
1062 
1063 	spin_lock_irqsave(&pdata->rfe_ctl_lock, flags);
1064 
1065 	pdata->rfe_ctl &= ~(RFE_CTL_UCAST_EN_ | RFE_CTL_MCAST_EN_ |
1066 			    RFE_CTL_DA_PERFECT_ | RFE_CTL_MCAST_HASH_);
1067 
1068 	for (i = 0; i < DP_SEL_VHF_HASH_LEN; i++)
1069 			pdata->mchash_table[i] = 0;
1070 	/* pfilter_table[0] has own HW address */
1071 	for (i = 1; i < NUM_OF_MAF; i++) {
1072 			pdata->pfilter_table[i][0] =
1073 			pdata->pfilter_table[i][1] = 0;
1074 	}
1075 
1076 	pdata->rfe_ctl |= RFE_CTL_BCAST_EN_;
1077 
1078 	if (dev->net->flags & IFF_PROMISC) {
1079 		netif_dbg(dev, drv, dev->net, "promiscuous mode enabled");
1080 		pdata->rfe_ctl |= RFE_CTL_MCAST_EN_ | RFE_CTL_UCAST_EN_;
1081 	} else {
1082 		if (dev->net->flags & IFF_ALLMULTI) {
1083 			netif_dbg(dev, drv, dev->net,
1084 				  "receive all multicast enabled");
1085 			pdata->rfe_ctl |= RFE_CTL_MCAST_EN_;
1086 		}
1087 	}
1088 
1089 	if (netdev_mc_count(dev->net)) {
1090 		struct netdev_hw_addr *ha;
1091 		int i;
1092 
1093 		netif_dbg(dev, drv, dev->net, "receive multicast hash filter");
1094 
1095 		pdata->rfe_ctl |= RFE_CTL_DA_PERFECT_;
1096 
1097 		i = 1;
1098 		netdev_for_each_mc_addr(ha, netdev) {
1099 			/* set first 32 into Perfect Filter */
1100 			if (i < 33) {
1101 				lan78xx_set_addr_filter(pdata, i, ha->addr);
1102 			} else {
1103 				u32 bitnum = lan78xx_hash(ha->addr);
1104 
1105 				pdata->mchash_table[bitnum / 32] |=
1106 							(1 << (bitnum % 32));
1107 				pdata->rfe_ctl |= RFE_CTL_MCAST_HASH_;
1108 			}
1109 			i++;
1110 		}
1111 	}
1112 
1113 	spin_unlock_irqrestore(&pdata->rfe_ctl_lock, flags);
1114 
1115 	/* defer register writes to a sleepable context */
1116 	schedule_work(&pdata->set_multicast);
1117 }
1118 
1119 static int lan78xx_update_flowcontrol(struct lan78xx_net *dev, u8 duplex,
1120 				      u16 lcladv, u16 rmtadv)
1121 {
1122 	u32 flow = 0, fct_flow = 0;
1123 	u8 cap;
1124 
1125 	if (dev->fc_autoneg)
1126 		cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
1127 	else
1128 		cap = dev->fc_request_control;
1129 
1130 	if (cap & FLOW_CTRL_TX)
1131 		flow |= (FLOW_CR_TX_FCEN_ | 0xFFFF);
1132 
1133 	if (cap & FLOW_CTRL_RX)
1134 		flow |= FLOW_CR_RX_FCEN_;
1135 
1136 	if (dev->udev->speed == USB_SPEED_SUPER)
1137 		fct_flow = 0x817;
1138 	else if (dev->udev->speed == USB_SPEED_HIGH)
1139 		fct_flow = 0x211;
1140 
1141 	netif_dbg(dev, link, dev->net, "rx pause %s, tx pause %s",
1142 		  (cap & FLOW_CTRL_RX ? "enabled" : "disabled"),
1143 		  (cap & FLOW_CTRL_TX ? "enabled" : "disabled"));
1144 
1145 	lan78xx_write_reg(dev, FCT_FLOW, fct_flow);
1146 
1147 	/* threshold value should be set before enabling flow */
1148 	lan78xx_write_reg(dev, FLOW, flow);
1149 
1150 	return 0;
1151 }
1152 
1153 static int lan78xx_link_reset(struct lan78xx_net *dev)
1154 {
1155 	struct phy_device *phydev = dev->net->phydev;
1156 	struct ethtool_link_ksettings ecmd;
1157 	int ladv, radv, ret;
1158 	u32 buf;
1159 
1160 	/* clear LAN78xx interrupt status */
1161 	ret = lan78xx_write_reg(dev, INT_STS, INT_STS_PHY_INT_);
1162 	if (unlikely(ret < 0))
1163 		return -EIO;
1164 
1165 	phy_read_status(phydev);
1166 
1167 	if (!phydev->link && dev->link_on) {
1168 		dev->link_on = false;
1169 
1170 		/* reset MAC */
1171 		ret = lan78xx_read_reg(dev, MAC_CR, &buf);
1172 		if (unlikely(ret < 0))
1173 			return -EIO;
1174 		buf |= MAC_CR_RST_;
1175 		ret = lan78xx_write_reg(dev, MAC_CR, buf);
1176 		if (unlikely(ret < 0))
1177 			return -EIO;
1178 
1179 		del_timer(&dev->stat_monitor);
1180 	} else if (phydev->link && !dev->link_on) {
1181 		dev->link_on = true;
1182 
1183 		phy_ethtool_ksettings_get(phydev, &ecmd);
1184 
1185 		if (dev->udev->speed == USB_SPEED_SUPER) {
1186 			if (ecmd.base.speed == 1000) {
1187 				/* disable U2 */
1188 				ret = lan78xx_read_reg(dev, USB_CFG1, &buf);
1189 				buf &= ~USB_CFG1_DEV_U2_INIT_EN_;
1190 				ret = lan78xx_write_reg(dev, USB_CFG1, buf);
1191 				/* enable U1 */
1192 				ret = lan78xx_read_reg(dev, USB_CFG1, &buf);
1193 				buf |= USB_CFG1_DEV_U1_INIT_EN_;
1194 				ret = lan78xx_write_reg(dev, USB_CFG1, buf);
1195 			} else {
1196 				/* enable U1 & U2 */
1197 				ret = lan78xx_read_reg(dev, USB_CFG1, &buf);
1198 				buf |= USB_CFG1_DEV_U2_INIT_EN_;
1199 				buf |= USB_CFG1_DEV_U1_INIT_EN_;
1200 				ret = lan78xx_write_reg(dev, USB_CFG1, buf);
1201 			}
1202 		}
1203 
1204 		ladv = phy_read(phydev, MII_ADVERTISE);
1205 		if (ladv < 0)
1206 			return ladv;
1207 
1208 		radv = phy_read(phydev, MII_LPA);
1209 		if (radv < 0)
1210 			return radv;
1211 
1212 		netif_dbg(dev, link, dev->net,
1213 			  "speed: %u duplex: %d anadv: 0x%04x anlpa: 0x%04x",
1214 			  ecmd.base.speed, ecmd.base.duplex, ladv, radv);
1215 
1216 		ret = lan78xx_update_flowcontrol(dev, ecmd.base.duplex, ladv,
1217 						 radv);
1218 
1219 		if (!timer_pending(&dev->stat_monitor)) {
1220 			dev->delta = 1;
1221 			mod_timer(&dev->stat_monitor,
1222 				  jiffies + STAT_UPDATE_TIMER);
1223 		}
1224 
1225 		tasklet_schedule(&dev->bh);
1226 	}
1227 
1228 	return ret;
1229 }
1230 
1231 /* some work can't be done in tasklets, so we use keventd
1232  *
1233  * NOTE:  annoying asymmetry:  if it's active, schedule_work() fails,
1234  * but tasklet_schedule() doesn't.	hope the failure is rare.
1235  */
1236 static void lan78xx_defer_kevent(struct lan78xx_net *dev, int work)
1237 {
1238 	set_bit(work, &dev->flags);
1239 	if (!schedule_delayed_work(&dev->wq, 0))
1240 		netdev_err(dev->net, "kevent %d may have been dropped\n", work);
1241 }
1242 
1243 static void lan78xx_status(struct lan78xx_net *dev, struct urb *urb)
1244 {
1245 	u32 intdata;
1246 
1247 	if (urb->actual_length != 4) {
1248 		netdev_warn(dev->net,
1249 			    "unexpected urb length %d", urb->actual_length);
1250 		return;
1251 	}
1252 
1253 	intdata = get_unaligned_le32(urb->transfer_buffer);
1254 
1255 	if (intdata & INT_ENP_PHY_INT) {
1256 		netif_dbg(dev, link, dev->net, "PHY INTR: 0x%08x\n", intdata);
1257 		lan78xx_defer_kevent(dev, EVENT_LINK_RESET);
1258 
1259 		if (dev->domain_data.phyirq > 0) {
1260 			local_irq_disable();
1261 			generic_handle_irq(dev->domain_data.phyirq);
1262 			local_irq_enable();
1263 		}
1264 	} else
1265 		netdev_warn(dev->net,
1266 			    "unexpected interrupt: 0x%08x\n", intdata);
1267 }
1268 
1269 static int lan78xx_ethtool_get_eeprom_len(struct net_device *netdev)
1270 {
1271 	return MAX_EEPROM_SIZE;
1272 }
1273 
1274 static int lan78xx_ethtool_get_eeprom(struct net_device *netdev,
1275 				      struct ethtool_eeprom *ee, u8 *data)
1276 {
1277 	struct lan78xx_net *dev = netdev_priv(netdev);
1278 	int ret;
1279 
1280 	ret = usb_autopm_get_interface(dev->intf);
1281 	if (ret)
1282 		return ret;
1283 
1284 	ee->magic = LAN78XX_EEPROM_MAGIC;
1285 
1286 	ret = lan78xx_read_raw_eeprom(dev, ee->offset, ee->len, data);
1287 
1288 	usb_autopm_put_interface(dev->intf);
1289 
1290 	return ret;
1291 }
1292 
1293 static int lan78xx_ethtool_set_eeprom(struct net_device *netdev,
1294 				      struct ethtool_eeprom *ee, u8 *data)
1295 {
1296 	struct lan78xx_net *dev = netdev_priv(netdev);
1297 	int ret;
1298 
1299 	ret = usb_autopm_get_interface(dev->intf);
1300 	if (ret)
1301 		return ret;
1302 
1303 	/* Invalid EEPROM_INDICATOR at offset zero will result in a failure
1304 	 * to load data from EEPROM
1305 	 */
1306 	if (ee->magic == LAN78XX_EEPROM_MAGIC)
1307 		ret = lan78xx_write_raw_eeprom(dev, ee->offset, ee->len, data);
1308 	else if ((ee->magic == LAN78XX_OTP_MAGIC) &&
1309 		 (ee->offset == 0) &&
1310 		 (ee->len == 512) &&
1311 		 (data[0] == OTP_INDICATOR_1))
1312 		ret = lan78xx_write_raw_otp(dev, ee->offset, ee->len, data);
1313 
1314 	usb_autopm_put_interface(dev->intf);
1315 
1316 	return ret;
1317 }
1318 
1319 static void lan78xx_get_strings(struct net_device *netdev, u32 stringset,
1320 				u8 *data)
1321 {
1322 	if (stringset == ETH_SS_STATS)
1323 		memcpy(data, lan78xx_gstrings, sizeof(lan78xx_gstrings));
1324 }
1325 
1326 static int lan78xx_get_sset_count(struct net_device *netdev, int sset)
1327 {
1328 	if (sset == ETH_SS_STATS)
1329 		return ARRAY_SIZE(lan78xx_gstrings);
1330 	else
1331 		return -EOPNOTSUPP;
1332 }
1333 
1334 static void lan78xx_get_stats(struct net_device *netdev,
1335 			      struct ethtool_stats *stats, u64 *data)
1336 {
1337 	struct lan78xx_net *dev = netdev_priv(netdev);
1338 
1339 	lan78xx_update_stats(dev);
1340 
1341 	mutex_lock(&dev->stats.access_lock);
1342 	memcpy(data, &dev->stats.curr_stat, sizeof(dev->stats.curr_stat));
1343 	mutex_unlock(&dev->stats.access_lock);
1344 }
1345 
1346 static void lan78xx_get_wol(struct net_device *netdev,
1347 			    struct ethtool_wolinfo *wol)
1348 {
1349 	struct lan78xx_net *dev = netdev_priv(netdev);
1350 	int ret;
1351 	u32 buf;
1352 	struct lan78xx_priv *pdata = (struct lan78xx_priv *)(dev->data[0]);
1353 
1354 	if (usb_autopm_get_interface(dev->intf) < 0)
1355 			return;
1356 
1357 	ret = lan78xx_read_reg(dev, USB_CFG0, &buf);
1358 	if (unlikely(ret < 0)) {
1359 		wol->supported = 0;
1360 		wol->wolopts = 0;
1361 	} else {
1362 		if (buf & USB_CFG_RMT_WKP_) {
1363 			wol->supported = WAKE_ALL;
1364 			wol->wolopts = pdata->wol;
1365 		} else {
1366 			wol->supported = 0;
1367 			wol->wolopts = 0;
1368 		}
1369 	}
1370 
1371 	usb_autopm_put_interface(dev->intf);
1372 }
1373 
1374 static int lan78xx_set_wol(struct net_device *netdev,
1375 			   struct ethtool_wolinfo *wol)
1376 {
1377 	struct lan78xx_net *dev = netdev_priv(netdev);
1378 	struct lan78xx_priv *pdata = (struct lan78xx_priv *)(dev->data[0]);
1379 	int ret;
1380 
1381 	ret = usb_autopm_get_interface(dev->intf);
1382 	if (ret < 0)
1383 		return ret;
1384 
1385 	if (wol->wolopts & ~WAKE_ALL)
1386 		return -EINVAL;
1387 
1388 	pdata->wol = wol->wolopts;
1389 
1390 	device_set_wakeup_enable(&dev->udev->dev, (bool)wol->wolopts);
1391 
1392 	phy_ethtool_set_wol(netdev->phydev, wol);
1393 
1394 	usb_autopm_put_interface(dev->intf);
1395 
1396 	return ret;
1397 }
1398 
1399 static int lan78xx_get_eee(struct net_device *net, struct ethtool_eee *edata)
1400 {
1401 	struct lan78xx_net *dev = netdev_priv(net);
1402 	struct phy_device *phydev = net->phydev;
1403 	int ret;
1404 	u32 buf;
1405 
1406 	ret = usb_autopm_get_interface(dev->intf);
1407 	if (ret < 0)
1408 		return ret;
1409 
1410 	ret = phy_ethtool_get_eee(phydev, edata);
1411 	if (ret < 0)
1412 		goto exit;
1413 
1414 	ret = lan78xx_read_reg(dev, MAC_CR, &buf);
1415 	if (buf & MAC_CR_EEE_EN_) {
1416 		edata->eee_enabled = true;
1417 		edata->eee_active = !!(edata->advertised &
1418 				       edata->lp_advertised);
1419 		edata->tx_lpi_enabled = true;
1420 		/* EEE_TX_LPI_REQ_DLY & tx_lpi_timer are same uSec unit */
1421 		ret = lan78xx_read_reg(dev, EEE_TX_LPI_REQ_DLY, &buf);
1422 		edata->tx_lpi_timer = buf;
1423 	} else {
1424 		edata->eee_enabled = false;
1425 		edata->eee_active = false;
1426 		edata->tx_lpi_enabled = false;
1427 		edata->tx_lpi_timer = 0;
1428 	}
1429 
1430 	ret = 0;
1431 exit:
1432 	usb_autopm_put_interface(dev->intf);
1433 
1434 	return ret;
1435 }
1436 
1437 static int lan78xx_set_eee(struct net_device *net, struct ethtool_eee *edata)
1438 {
1439 	struct lan78xx_net *dev = netdev_priv(net);
1440 	int ret;
1441 	u32 buf;
1442 
1443 	ret = usb_autopm_get_interface(dev->intf);
1444 	if (ret < 0)
1445 		return ret;
1446 
1447 	if (edata->eee_enabled) {
1448 		ret = lan78xx_read_reg(dev, MAC_CR, &buf);
1449 		buf |= MAC_CR_EEE_EN_;
1450 		ret = lan78xx_write_reg(dev, MAC_CR, buf);
1451 
1452 		phy_ethtool_set_eee(net->phydev, edata);
1453 
1454 		buf = (u32)edata->tx_lpi_timer;
1455 		ret = lan78xx_write_reg(dev, EEE_TX_LPI_REQ_DLY, buf);
1456 	} else {
1457 		ret = lan78xx_read_reg(dev, MAC_CR, &buf);
1458 		buf &= ~MAC_CR_EEE_EN_;
1459 		ret = lan78xx_write_reg(dev, MAC_CR, buf);
1460 	}
1461 
1462 	usb_autopm_put_interface(dev->intf);
1463 
1464 	return 0;
1465 }
1466 
1467 static u32 lan78xx_get_link(struct net_device *net)
1468 {
1469 	phy_read_status(net->phydev);
1470 
1471 	return net->phydev->link;
1472 }
1473 
1474 static void lan78xx_get_drvinfo(struct net_device *net,
1475 				struct ethtool_drvinfo *info)
1476 {
1477 	struct lan78xx_net *dev = netdev_priv(net);
1478 
1479 	strncpy(info->driver, DRIVER_NAME, sizeof(info->driver));
1480 	usb_make_path(dev->udev, info->bus_info, sizeof(info->bus_info));
1481 }
1482 
1483 static u32 lan78xx_get_msglevel(struct net_device *net)
1484 {
1485 	struct lan78xx_net *dev = netdev_priv(net);
1486 
1487 	return dev->msg_enable;
1488 }
1489 
1490 static void lan78xx_set_msglevel(struct net_device *net, u32 level)
1491 {
1492 	struct lan78xx_net *dev = netdev_priv(net);
1493 
1494 	dev->msg_enable = level;
1495 }
1496 
1497 static int lan78xx_get_link_ksettings(struct net_device *net,
1498 				      struct ethtool_link_ksettings *cmd)
1499 {
1500 	struct lan78xx_net *dev = netdev_priv(net);
1501 	struct phy_device *phydev = net->phydev;
1502 	int ret;
1503 
1504 	ret = usb_autopm_get_interface(dev->intf);
1505 	if (ret < 0)
1506 		return ret;
1507 
1508 	phy_ethtool_ksettings_get(phydev, cmd);
1509 
1510 	usb_autopm_put_interface(dev->intf);
1511 
1512 	return ret;
1513 }
1514 
1515 static int lan78xx_set_link_ksettings(struct net_device *net,
1516 				      const struct ethtool_link_ksettings *cmd)
1517 {
1518 	struct lan78xx_net *dev = netdev_priv(net);
1519 	struct phy_device *phydev = net->phydev;
1520 	int ret = 0;
1521 	int temp;
1522 
1523 	ret = usb_autopm_get_interface(dev->intf);
1524 	if (ret < 0)
1525 		return ret;
1526 
1527 	/* change speed & duplex */
1528 	ret = phy_ethtool_ksettings_set(phydev, cmd);
1529 
1530 	if (!cmd->base.autoneg) {
1531 		/* force link down */
1532 		temp = phy_read(phydev, MII_BMCR);
1533 		phy_write(phydev, MII_BMCR, temp | BMCR_LOOPBACK);
1534 		mdelay(1);
1535 		phy_write(phydev, MII_BMCR, temp);
1536 	}
1537 
1538 	usb_autopm_put_interface(dev->intf);
1539 
1540 	return ret;
1541 }
1542 
1543 static void lan78xx_get_pause(struct net_device *net,
1544 			      struct ethtool_pauseparam *pause)
1545 {
1546 	struct lan78xx_net *dev = netdev_priv(net);
1547 	struct phy_device *phydev = net->phydev;
1548 	struct ethtool_link_ksettings ecmd;
1549 
1550 	phy_ethtool_ksettings_get(phydev, &ecmd);
1551 
1552 	pause->autoneg = dev->fc_autoneg;
1553 
1554 	if (dev->fc_request_control & FLOW_CTRL_TX)
1555 		pause->tx_pause = 1;
1556 
1557 	if (dev->fc_request_control & FLOW_CTRL_RX)
1558 		pause->rx_pause = 1;
1559 }
1560 
1561 static int lan78xx_set_pause(struct net_device *net,
1562 			     struct ethtool_pauseparam *pause)
1563 {
1564 	struct lan78xx_net *dev = netdev_priv(net);
1565 	struct phy_device *phydev = net->phydev;
1566 	struct ethtool_link_ksettings ecmd;
1567 	int ret;
1568 
1569 	phy_ethtool_ksettings_get(phydev, &ecmd);
1570 
1571 	if (pause->autoneg && !ecmd.base.autoneg) {
1572 		ret = -EINVAL;
1573 		goto exit;
1574 	}
1575 
1576 	dev->fc_request_control = 0;
1577 	if (pause->rx_pause)
1578 		dev->fc_request_control |= FLOW_CTRL_RX;
1579 
1580 	if (pause->tx_pause)
1581 		dev->fc_request_control |= FLOW_CTRL_TX;
1582 
1583 	if (ecmd.base.autoneg) {
1584 		__ETHTOOL_DECLARE_LINK_MODE_MASK(fc) = { 0, };
1585 		u32 mii_adv;
1586 
1587 		linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT,
1588 				   ecmd.link_modes.advertising);
1589 		linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
1590 				   ecmd.link_modes.advertising);
1591 		mii_adv = (u32)mii_advertise_flowctrl(dev->fc_request_control);
1592 		mii_adv_to_linkmode_adv_t(fc, mii_adv);
1593 		linkmode_or(ecmd.link_modes.advertising, fc,
1594 			    ecmd.link_modes.advertising);
1595 
1596 		phy_ethtool_ksettings_set(phydev, &ecmd);
1597 	}
1598 
1599 	dev->fc_autoneg = pause->autoneg;
1600 
1601 	ret = 0;
1602 exit:
1603 	return ret;
1604 }
1605 
1606 static int lan78xx_get_regs_len(struct net_device *netdev)
1607 {
1608 	if (!netdev->phydev)
1609 		return (sizeof(lan78xx_regs));
1610 	else
1611 		return (sizeof(lan78xx_regs) + PHY_REG_SIZE);
1612 }
1613 
1614 static void
1615 lan78xx_get_regs(struct net_device *netdev, struct ethtool_regs *regs,
1616 		 void *buf)
1617 {
1618 	u32 *data = buf;
1619 	int i, j;
1620 	struct lan78xx_net *dev = netdev_priv(netdev);
1621 
1622 	/* Read Device/MAC registers */
1623 	for (i = 0; i < ARRAY_SIZE(lan78xx_regs); i++)
1624 		lan78xx_read_reg(dev, lan78xx_regs[i], &data[i]);
1625 
1626 	if (!netdev->phydev)
1627 		return;
1628 
1629 	/* Read PHY registers */
1630 	for (j = 0; j < 32; i++, j++)
1631 		data[i] = phy_read(netdev->phydev, j);
1632 }
1633 
1634 static const struct ethtool_ops lan78xx_ethtool_ops = {
1635 	.get_link	= lan78xx_get_link,
1636 	.nway_reset	= phy_ethtool_nway_reset,
1637 	.get_drvinfo	= lan78xx_get_drvinfo,
1638 	.get_msglevel	= lan78xx_get_msglevel,
1639 	.set_msglevel	= lan78xx_set_msglevel,
1640 	.get_eeprom_len = lan78xx_ethtool_get_eeprom_len,
1641 	.get_eeprom	= lan78xx_ethtool_get_eeprom,
1642 	.set_eeprom	= lan78xx_ethtool_set_eeprom,
1643 	.get_ethtool_stats = lan78xx_get_stats,
1644 	.get_sset_count = lan78xx_get_sset_count,
1645 	.get_strings	= lan78xx_get_strings,
1646 	.get_wol	= lan78xx_get_wol,
1647 	.set_wol	= lan78xx_set_wol,
1648 	.get_eee	= lan78xx_get_eee,
1649 	.set_eee	= lan78xx_set_eee,
1650 	.get_pauseparam	= lan78xx_get_pause,
1651 	.set_pauseparam	= lan78xx_set_pause,
1652 	.get_link_ksettings = lan78xx_get_link_ksettings,
1653 	.set_link_ksettings = lan78xx_set_link_ksettings,
1654 	.get_regs_len	= lan78xx_get_regs_len,
1655 	.get_regs	= lan78xx_get_regs,
1656 };
1657 
1658 static void lan78xx_init_mac_address(struct lan78xx_net *dev)
1659 {
1660 	u32 addr_lo, addr_hi;
1661 	u8 addr[6];
1662 
1663 	lan78xx_read_reg(dev, RX_ADDRL, &addr_lo);
1664 	lan78xx_read_reg(dev, RX_ADDRH, &addr_hi);
1665 
1666 	addr[0] = addr_lo & 0xFF;
1667 	addr[1] = (addr_lo >> 8) & 0xFF;
1668 	addr[2] = (addr_lo >> 16) & 0xFF;
1669 	addr[3] = (addr_lo >> 24) & 0xFF;
1670 	addr[4] = addr_hi & 0xFF;
1671 	addr[5] = (addr_hi >> 8) & 0xFF;
1672 
1673 	if (!is_valid_ether_addr(addr)) {
1674 		if (!eth_platform_get_mac_address(&dev->udev->dev, addr)) {
1675 			/* valid address present in Device Tree */
1676 			netif_dbg(dev, ifup, dev->net,
1677 				  "MAC address read from Device Tree");
1678 		} else if (((lan78xx_read_eeprom(dev, EEPROM_MAC_OFFSET,
1679 						 ETH_ALEN, addr) == 0) ||
1680 			    (lan78xx_read_otp(dev, EEPROM_MAC_OFFSET,
1681 					      ETH_ALEN, addr) == 0)) &&
1682 			   is_valid_ether_addr(addr)) {
1683 			/* eeprom values are valid so use them */
1684 			netif_dbg(dev, ifup, dev->net,
1685 				  "MAC address read from EEPROM");
1686 		} else {
1687 			/* generate random MAC */
1688 			eth_random_addr(addr);
1689 			netif_dbg(dev, ifup, dev->net,
1690 				  "MAC address set to random addr");
1691 		}
1692 
1693 		addr_lo = addr[0] | (addr[1] << 8) |
1694 			  (addr[2] << 16) | (addr[3] << 24);
1695 		addr_hi = addr[4] | (addr[5] << 8);
1696 
1697 		lan78xx_write_reg(dev, RX_ADDRL, addr_lo);
1698 		lan78xx_write_reg(dev, RX_ADDRH, addr_hi);
1699 	}
1700 
1701 	lan78xx_write_reg(dev, MAF_LO(0), addr_lo);
1702 	lan78xx_write_reg(dev, MAF_HI(0), addr_hi | MAF_HI_VALID_);
1703 
1704 	ether_addr_copy(dev->net->dev_addr, addr);
1705 }
1706 
1707 /* MDIO read and write wrappers for phylib */
1708 static int lan78xx_mdiobus_read(struct mii_bus *bus, int phy_id, int idx)
1709 {
1710 	struct lan78xx_net *dev = bus->priv;
1711 	u32 val, addr;
1712 	int ret;
1713 
1714 	ret = usb_autopm_get_interface(dev->intf);
1715 	if (ret < 0)
1716 		return ret;
1717 
1718 	mutex_lock(&dev->phy_mutex);
1719 
1720 	/* confirm MII not busy */
1721 	ret = lan78xx_phy_wait_not_busy(dev);
1722 	if (ret < 0)
1723 		goto done;
1724 
1725 	/* set the address, index & direction (read from PHY) */
1726 	addr = mii_access(phy_id, idx, MII_READ);
1727 	ret = lan78xx_write_reg(dev, MII_ACC, addr);
1728 
1729 	ret = lan78xx_phy_wait_not_busy(dev);
1730 	if (ret < 0)
1731 		goto done;
1732 
1733 	ret = lan78xx_read_reg(dev, MII_DATA, &val);
1734 
1735 	ret = (int)(val & 0xFFFF);
1736 
1737 done:
1738 	mutex_unlock(&dev->phy_mutex);
1739 	usb_autopm_put_interface(dev->intf);
1740 
1741 	return ret;
1742 }
1743 
1744 static int lan78xx_mdiobus_write(struct mii_bus *bus, int phy_id, int idx,
1745 				 u16 regval)
1746 {
1747 	struct lan78xx_net *dev = bus->priv;
1748 	u32 val, addr;
1749 	int ret;
1750 
1751 	ret = usb_autopm_get_interface(dev->intf);
1752 	if (ret < 0)
1753 		return ret;
1754 
1755 	mutex_lock(&dev->phy_mutex);
1756 
1757 	/* confirm MII not busy */
1758 	ret = lan78xx_phy_wait_not_busy(dev);
1759 	if (ret < 0)
1760 		goto done;
1761 
1762 	val = (u32)regval;
1763 	ret = lan78xx_write_reg(dev, MII_DATA, val);
1764 
1765 	/* set the address, index & direction (write to PHY) */
1766 	addr = mii_access(phy_id, idx, MII_WRITE);
1767 	ret = lan78xx_write_reg(dev, MII_ACC, addr);
1768 
1769 	ret = lan78xx_phy_wait_not_busy(dev);
1770 	if (ret < 0)
1771 		goto done;
1772 
1773 done:
1774 	mutex_unlock(&dev->phy_mutex);
1775 	usb_autopm_put_interface(dev->intf);
1776 	return 0;
1777 }
1778 
1779 static int lan78xx_mdio_init(struct lan78xx_net *dev)
1780 {
1781 	struct device_node *node;
1782 	int ret;
1783 
1784 	dev->mdiobus = mdiobus_alloc();
1785 	if (!dev->mdiobus) {
1786 		netdev_err(dev->net, "can't allocate MDIO bus\n");
1787 		return -ENOMEM;
1788 	}
1789 
1790 	dev->mdiobus->priv = (void *)dev;
1791 	dev->mdiobus->read = lan78xx_mdiobus_read;
1792 	dev->mdiobus->write = lan78xx_mdiobus_write;
1793 	dev->mdiobus->name = "lan78xx-mdiobus";
1794 	dev->mdiobus->parent = &dev->udev->dev;
1795 
1796 	snprintf(dev->mdiobus->id, MII_BUS_ID_SIZE, "usb-%03d:%03d",
1797 		 dev->udev->bus->busnum, dev->udev->devnum);
1798 
1799 	switch (dev->chipid) {
1800 	case ID_REV_CHIP_ID_7800_:
1801 	case ID_REV_CHIP_ID_7850_:
1802 		/* set to internal PHY id */
1803 		dev->mdiobus->phy_mask = ~(1 << 1);
1804 		break;
1805 	case ID_REV_CHIP_ID_7801_:
1806 		/* scan thru PHYAD[2..0] */
1807 		dev->mdiobus->phy_mask = ~(0xFF);
1808 		break;
1809 	}
1810 
1811 	node = of_get_child_by_name(dev->udev->dev.of_node, "mdio");
1812 	ret = of_mdiobus_register(dev->mdiobus, node);
1813 	of_node_put(node);
1814 	if (ret) {
1815 		netdev_err(dev->net, "can't register MDIO bus\n");
1816 		goto exit1;
1817 	}
1818 
1819 	netdev_dbg(dev->net, "registered mdiobus bus %s\n", dev->mdiobus->id);
1820 	return 0;
1821 exit1:
1822 	mdiobus_free(dev->mdiobus);
1823 	return ret;
1824 }
1825 
1826 static void lan78xx_remove_mdio(struct lan78xx_net *dev)
1827 {
1828 	mdiobus_unregister(dev->mdiobus);
1829 	mdiobus_free(dev->mdiobus);
1830 }
1831 
1832 static void lan78xx_link_status_change(struct net_device *net)
1833 {
1834 	struct phy_device *phydev = net->phydev;
1835 	int temp;
1836 
1837 	/* At forced 100 F/H mode, chip may fail to set mode correctly
1838 	 * when cable is switched between long(~50+m) and short one.
1839 	 * As workaround, set to 10 before setting to 100
1840 	 * at forced 100 F/H mode.
1841 	 */
1842 	if (!phydev->autoneg && (phydev->speed == 100)) {
1843 		/* disable phy interrupt */
1844 		temp = phy_read(phydev, LAN88XX_INT_MASK);
1845 		temp &= ~LAN88XX_INT_MASK_MDINTPIN_EN_;
1846 		phy_write(phydev, LAN88XX_INT_MASK, temp);
1847 
1848 		temp = phy_read(phydev, MII_BMCR);
1849 		temp &= ~(BMCR_SPEED100 | BMCR_SPEED1000);
1850 		phy_write(phydev, MII_BMCR, temp); /* set to 10 first */
1851 		temp |= BMCR_SPEED100;
1852 		phy_write(phydev, MII_BMCR, temp); /* set to 100 later */
1853 
1854 		/* clear pending interrupt generated while workaround */
1855 		temp = phy_read(phydev, LAN88XX_INT_STS);
1856 
1857 		/* enable phy interrupt back */
1858 		temp = phy_read(phydev, LAN88XX_INT_MASK);
1859 		temp |= LAN88XX_INT_MASK_MDINTPIN_EN_;
1860 		phy_write(phydev, LAN88XX_INT_MASK, temp);
1861 	}
1862 }
1863 
1864 static int irq_map(struct irq_domain *d, unsigned int irq,
1865 		   irq_hw_number_t hwirq)
1866 {
1867 	struct irq_domain_data *data = d->host_data;
1868 
1869 	irq_set_chip_data(irq, data);
1870 	irq_set_chip_and_handler(irq, data->irqchip, data->irq_handler);
1871 	irq_set_noprobe(irq);
1872 
1873 	return 0;
1874 }
1875 
1876 static void irq_unmap(struct irq_domain *d, unsigned int irq)
1877 {
1878 	irq_set_chip_and_handler(irq, NULL, NULL);
1879 	irq_set_chip_data(irq, NULL);
1880 }
1881 
1882 static const struct irq_domain_ops chip_domain_ops = {
1883 	.map	= irq_map,
1884 	.unmap	= irq_unmap,
1885 };
1886 
1887 static void lan78xx_irq_mask(struct irq_data *irqd)
1888 {
1889 	struct irq_domain_data *data = irq_data_get_irq_chip_data(irqd);
1890 
1891 	data->irqenable &= ~BIT(irqd_to_hwirq(irqd));
1892 }
1893 
1894 static void lan78xx_irq_unmask(struct irq_data *irqd)
1895 {
1896 	struct irq_domain_data *data = irq_data_get_irq_chip_data(irqd);
1897 
1898 	data->irqenable |= BIT(irqd_to_hwirq(irqd));
1899 }
1900 
1901 static void lan78xx_irq_bus_lock(struct irq_data *irqd)
1902 {
1903 	struct irq_domain_data *data = irq_data_get_irq_chip_data(irqd);
1904 
1905 	mutex_lock(&data->irq_lock);
1906 }
1907 
1908 static void lan78xx_irq_bus_sync_unlock(struct irq_data *irqd)
1909 {
1910 	struct irq_domain_data *data = irq_data_get_irq_chip_data(irqd);
1911 	struct lan78xx_net *dev =
1912 			container_of(data, struct lan78xx_net, domain_data);
1913 	u32 buf;
1914 
1915 	/* call register access here because irq_bus_lock & irq_bus_sync_unlock
1916 	 * are only two callbacks executed in non-atomic contex.
1917 	 */
1918 	lan78xx_read_reg(dev, INT_EP_CTL, &buf);
1919 	if (buf != data->irqenable)
1920 		lan78xx_write_reg(dev, INT_EP_CTL, data->irqenable);
1921 
1922 	mutex_unlock(&data->irq_lock);
1923 }
1924 
1925 static struct irq_chip lan78xx_irqchip = {
1926 	.name			= "lan78xx-irqs",
1927 	.irq_mask		= lan78xx_irq_mask,
1928 	.irq_unmask		= lan78xx_irq_unmask,
1929 	.irq_bus_lock		= lan78xx_irq_bus_lock,
1930 	.irq_bus_sync_unlock	= lan78xx_irq_bus_sync_unlock,
1931 };
1932 
1933 static int lan78xx_setup_irq_domain(struct lan78xx_net *dev)
1934 {
1935 	struct device_node *of_node;
1936 	struct irq_domain *irqdomain;
1937 	unsigned int irqmap = 0;
1938 	u32 buf;
1939 	int ret = 0;
1940 
1941 	of_node = dev->udev->dev.parent->of_node;
1942 
1943 	mutex_init(&dev->domain_data.irq_lock);
1944 
1945 	lan78xx_read_reg(dev, INT_EP_CTL, &buf);
1946 	dev->domain_data.irqenable = buf;
1947 
1948 	dev->domain_data.irqchip = &lan78xx_irqchip;
1949 	dev->domain_data.irq_handler = handle_simple_irq;
1950 
1951 	irqdomain = irq_domain_add_simple(of_node, MAX_INT_EP, 0,
1952 					  &chip_domain_ops, &dev->domain_data);
1953 	if (irqdomain) {
1954 		/* create mapping for PHY interrupt */
1955 		irqmap = irq_create_mapping(irqdomain, INT_EP_PHY);
1956 		if (!irqmap) {
1957 			irq_domain_remove(irqdomain);
1958 
1959 			irqdomain = NULL;
1960 			ret = -EINVAL;
1961 		}
1962 	} else {
1963 		ret = -EINVAL;
1964 	}
1965 
1966 	dev->domain_data.irqdomain = irqdomain;
1967 	dev->domain_data.phyirq = irqmap;
1968 
1969 	return ret;
1970 }
1971 
1972 static void lan78xx_remove_irq_domain(struct lan78xx_net *dev)
1973 {
1974 	if (dev->domain_data.phyirq > 0) {
1975 		irq_dispose_mapping(dev->domain_data.phyirq);
1976 
1977 		if (dev->domain_data.irqdomain)
1978 			irq_domain_remove(dev->domain_data.irqdomain);
1979 	}
1980 	dev->domain_data.phyirq = 0;
1981 	dev->domain_data.irqdomain = NULL;
1982 }
1983 
1984 static int lan8835_fixup(struct phy_device *phydev)
1985 {
1986 	int buf;
1987 	struct lan78xx_net *dev = netdev_priv(phydev->attached_dev);
1988 
1989 	/* LED2/PME_N/IRQ_N/RGMII_ID pin to IRQ_N mode */
1990 	buf = phy_read_mmd(phydev, MDIO_MMD_PCS, 0x8010);
1991 	buf &= ~0x1800;
1992 	buf |= 0x0800;
1993 	phy_write_mmd(phydev, MDIO_MMD_PCS, 0x8010, buf);
1994 
1995 	/* RGMII MAC TXC Delay Enable */
1996 	lan78xx_write_reg(dev, MAC_RGMII_ID,
1997 				MAC_RGMII_ID_TXC_DELAY_EN_);
1998 
1999 	/* RGMII TX DLL Tune Adjust */
2000 	lan78xx_write_reg(dev, RGMII_TX_BYP_DLL, 0x3D00);
2001 
2002 	dev->interface = PHY_INTERFACE_MODE_RGMII_TXID;
2003 
2004 	return 1;
2005 }
2006 
2007 static int ksz9031rnx_fixup(struct phy_device *phydev)
2008 {
2009 	struct lan78xx_net *dev = netdev_priv(phydev->attached_dev);
2010 
2011 	/* Micrel9301RNX PHY configuration */
2012 	/* RGMII Control Signal Pad Skew */
2013 	phy_write_mmd(phydev, MDIO_MMD_WIS, 4, 0x0077);
2014 	/* RGMII RX Data Pad Skew */
2015 	phy_write_mmd(phydev, MDIO_MMD_WIS, 5, 0x7777);
2016 	/* RGMII RX Clock Pad Skew */
2017 	phy_write_mmd(phydev, MDIO_MMD_WIS, 8, 0x1FF);
2018 
2019 	dev->interface = PHY_INTERFACE_MODE_RGMII_RXID;
2020 
2021 	return 1;
2022 }
2023 
2024 static struct phy_device *lan7801_phy_init(struct lan78xx_net *dev)
2025 {
2026 	u32 buf;
2027 	int ret;
2028 	struct fixed_phy_status fphy_status = {
2029 		.link = 1,
2030 		.speed = SPEED_1000,
2031 		.duplex = DUPLEX_FULL,
2032 	};
2033 	struct phy_device *phydev;
2034 
2035 	phydev = phy_find_first(dev->mdiobus);
2036 	if (!phydev) {
2037 		netdev_dbg(dev->net, "PHY Not Found!! Registering Fixed PHY\n");
2038 		phydev = fixed_phy_register(PHY_POLL, &fphy_status, NULL);
2039 		if (IS_ERR(phydev)) {
2040 			netdev_err(dev->net, "No PHY/fixed_PHY found\n");
2041 			return NULL;
2042 		}
2043 		netdev_dbg(dev->net, "Registered FIXED PHY\n");
2044 		dev->interface = PHY_INTERFACE_MODE_RGMII;
2045 		ret = lan78xx_write_reg(dev, MAC_RGMII_ID,
2046 					MAC_RGMII_ID_TXC_DELAY_EN_);
2047 		ret = lan78xx_write_reg(dev, RGMII_TX_BYP_DLL, 0x3D00);
2048 		ret = lan78xx_read_reg(dev, HW_CFG, &buf);
2049 		buf |= HW_CFG_CLK125_EN_;
2050 		buf |= HW_CFG_REFCLK25_EN_;
2051 		ret = lan78xx_write_reg(dev, HW_CFG, buf);
2052 	} else {
2053 		if (!phydev->drv) {
2054 			netdev_err(dev->net, "no PHY driver found\n");
2055 			return NULL;
2056 		}
2057 		dev->interface = PHY_INTERFACE_MODE_RGMII;
2058 		/* external PHY fixup for KSZ9031RNX */
2059 		ret = phy_register_fixup_for_uid(PHY_KSZ9031RNX, 0xfffffff0,
2060 						 ksz9031rnx_fixup);
2061 		if (ret < 0) {
2062 			netdev_err(dev->net, "Failed to register fixup for PHY_KSZ9031RNX\n");
2063 			return NULL;
2064 		}
2065 		/* external PHY fixup for LAN8835 */
2066 		ret = phy_register_fixup_for_uid(PHY_LAN8835, 0xfffffff0,
2067 						 lan8835_fixup);
2068 		if (ret < 0) {
2069 			netdev_err(dev->net, "Failed to register fixup for PHY_LAN8835\n");
2070 			return NULL;
2071 		}
2072 		/* add more external PHY fixup here if needed */
2073 
2074 		phydev->is_internal = false;
2075 	}
2076 	return phydev;
2077 }
2078 
2079 static int lan78xx_phy_init(struct lan78xx_net *dev)
2080 {
2081 	__ETHTOOL_DECLARE_LINK_MODE_MASK(fc) = { 0, };
2082 	int ret;
2083 	u32 mii_adv;
2084 	struct phy_device *phydev;
2085 
2086 	switch (dev->chipid) {
2087 	case ID_REV_CHIP_ID_7801_:
2088 		phydev = lan7801_phy_init(dev);
2089 		if (!phydev) {
2090 			netdev_err(dev->net, "lan7801: PHY Init Failed");
2091 			return -EIO;
2092 		}
2093 		break;
2094 
2095 	case ID_REV_CHIP_ID_7800_:
2096 	case ID_REV_CHIP_ID_7850_:
2097 		phydev = phy_find_first(dev->mdiobus);
2098 		if (!phydev) {
2099 			netdev_err(dev->net, "no PHY found\n");
2100 			return -EIO;
2101 		}
2102 		phydev->is_internal = true;
2103 		dev->interface = PHY_INTERFACE_MODE_GMII;
2104 		break;
2105 
2106 	default:
2107 		netdev_err(dev->net, "Unknown CHIP ID found\n");
2108 		return -EIO;
2109 	}
2110 
2111 	/* if phyirq is not set, use polling mode in phylib */
2112 	if (dev->domain_data.phyirq > 0)
2113 		phydev->irq = dev->domain_data.phyirq;
2114 	else
2115 		phydev->irq = 0;
2116 	netdev_dbg(dev->net, "phydev->irq = %d\n", phydev->irq);
2117 
2118 	/* set to AUTOMDIX */
2119 	phydev->mdix = ETH_TP_MDI_AUTO;
2120 
2121 	ret = phy_connect_direct(dev->net, phydev,
2122 				 lan78xx_link_status_change,
2123 				 dev->interface);
2124 	if (ret) {
2125 		netdev_err(dev->net, "can't attach PHY to %s\n",
2126 			   dev->mdiobus->id);
2127 		if (dev->chipid == ID_REV_CHIP_ID_7801_) {
2128 			if (phy_is_pseudo_fixed_link(phydev)) {
2129 				fixed_phy_unregister(phydev);
2130 			} else {
2131 				phy_unregister_fixup_for_uid(PHY_KSZ9031RNX,
2132 							     0xfffffff0);
2133 				phy_unregister_fixup_for_uid(PHY_LAN8835,
2134 							     0xfffffff0);
2135 			}
2136 		}
2137 		return -EIO;
2138 	}
2139 
2140 	/* MAC doesn't support 1000T Half */
2141 	phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_1000baseT_Half_BIT);
2142 
2143 	/* support both flow controls */
2144 	dev->fc_request_control = (FLOW_CTRL_RX | FLOW_CTRL_TX);
2145 	linkmode_clear_bit(ETHTOOL_LINK_MODE_Pause_BIT,
2146 			   phydev->advertising);
2147 	linkmode_clear_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT,
2148 			   phydev->advertising);
2149 	mii_adv = (u32)mii_advertise_flowctrl(dev->fc_request_control);
2150 	mii_adv_to_linkmode_adv_t(fc, mii_adv);
2151 	linkmode_or(phydev->advertising, fc, phydev->advertising);
2152 
2153 	if (phydev->mdio.dev.of_node) {
2154 		u32 reg;
2155 		int len;
2156 
2157 		len = of_property_count_elems_of_size(phydev->mdio.dev.of_node,
2158 						      "microchip,led-modes",
2159 						      sizeof(u32));
2160 		if (len >= 0) {
2161 			/* Ensure the appropriate LEDs are enabled */
2162 			lan78xx_read_reg(dev, HW_CFG, &reg);
2163 			reg &= ~(HW_CFG_LED0_EN_ |
2164 				 HW_CFG_LED1_EN_ |
2165 				 HW_CFG_LED2_EN_ |
2166 				 HW_CFG_LED3_EN_);
2167 			reg |= (len > 0) * HW_CFG_LED0_EN_ |
2168 				(len > 1) * HW_CFG_LED1_EN_ |
2169 				(len > 2) * HW_CFG_LED2_EN_ |
2170 				(len > 3) * HW_CFG_LED3_EN_;
2171 			lan78xx_write_reg(dev, HW_CFG, reg);
2172 		}
2173 	}
2174 
2175 	genphy_config_aneg(phydev);
2176 
2177 	dev->fc_autoneg = phydev->autoneg;
2178 
2179 	return 0;
2180 }
2181 
2182 static int lan78xx_set_rx_max_frame_length(struct lan78xx_net *dev, int size)
2183 {
2184 	u32 buf;
2185 	bool rxenabled;
2186 
2187 	lan78xx_read_reg(dev, MAC_RX, &buf);
2188 
2189 	rxenabled = ((buf & MAC_RX_RXEN_) != 0);
2190 
2191 	if (rxenabled) {
2192 		buf &= ~MAC_RX_RXEN_;
2193 		lan78xx_write_reg(dev, MAC_RX, buf);
2194 	}
2195 
2196 	/* add 4 to size for FCS */
2197 	buf &= ~MAC_RX_MAX_SIZE_MASK_;
2198 	buf |= (((size + 4) << MAC_RX_MAX_SIZE_SHIFT_) & MAC_RX_MAX_SIZE_MASK_);
2199 
2200 	lan78xx_write_reg(dev, MAC_RX, buf);
2201 
2202 	if (rxenabled) {
2203 		buf |= MAC_RX_RXEN_;
2204 		lan78xx_write_reg(dev, MAC_RX, buf);
2205 	}
2206 
2207 	return 0;
2208 }
2209 
2210 static int unlink_urbs(struct lan78xx_net *dev, struct sk_buff_head *q)
2211 {
2212 	struct sk_buff *skb;
2213 	unsigned long flags;
2214 	int count = 0;
2215 
2216 	spin_lock_irqsave(&q->lock, flags);
2217 	while (!skb_queue_empty(q)) {
2218 		struct skb_data	*entry;
2219 		struct urb *urb;
2220 		int ret;
2221 
2222 		skb_queue_walk(q, skb) {
2223 			entry = (struct skb_data *)skb->cb;
2224 			if (entry->state != unlink_start)
2225 				goto found;
2226 		}
2227 		break;
2228 found:
2229 		entry->state = unlink_start;
2230 		urb = entry->urb;
2231 
2232 		/* Get reference count of the URB to avoid it to be
2233 		 * freed during usb_unlink_urb, which may trigger
2234 		 * use-after-free problem inside usb_unlink_urb since
2235 		 * usb_unlink_urb is always racing with .complete
2236 		 * handler(include defer_bh).
2237 		 */
2238 		usb_get_urb(urb);
2239 		spin_unlock_irqrestore(&q->lock, flags);
2240 		/* during some PM-driven resume scenarios,
2241 		 * these (async) unlinks complete immediately
2242 		 */
2243 		ret = usb_unlink_urb(urb);
2244 		if (ret != -EINPROGRESS && ret != 0)
2245 			netdev_dbg(dev->net, "unlink urb err, %d\n", ret);
2246 		else
2247 			count++;
2248 		usb_put_urb(urb);
2249 		spin_lock_irqsave(&q->lock, flags);
2250 	}
2251 	spin_unlock_irqrestore(&q->lock, flags);
2252 	return count;
2253 }
2254 
2255 static int lan78xx_change_mtu(struct net_device *netdev, int new_mtu)
2256 {
2257 	struct lan78xx_net *dev = netdev_priv(netdev);
2258 	int ll_mtu = new_mtu + netdev->hard_header_len;
2259 	int old_hard_mtu = dev->hard_mtu;
2260 	int old_rx_urb_size = dev->rx_urb_size;
2261 
2262 	/* no second zero-length packet read wanted after mtu-sized packets */
2263 	if ((ll_mtu % dev->maxpacket) == 0)
2264 		return -EDOM;
2265 
2266 	lan78xx_set_rx_max_frame_length(dev, new_mtu + VLAN_ETH_HLEN);
2267 
2268 	netdev->mtu = new_mtu;
2269 
2270 	dev->hard_mtu = netdev->mtu + netdev->hard_header_len;
2271 	if (dev->rx_urb_size == old_hard_mtu) {
2272 		dev->rx_urb_size = dev->hard_mtu;
2273 		if (dev->rx_urb_size > old_rx_urb_size) {
2274 			if (netif_running(dev->net)) {
2275 				unlink_urbs(dev, &dev->rxq);
2276 				tasklet_schedule(&dev->bh);
2277 			}
2278 		}
2279 	}
2280 
2281 	return 0;
2282 }
2283 
2284 static int lan78xx_set_mac_addr(struct net_device *netdev, void *p)
2285 {
2286 	struct lan78xx_net *dev = netdev_priv(netdev);
2287 	struct sockaddr *addr = p;
2288 	u32 addr_lo, addr_hi;
2289 
2290 	if (netif_running(netdev))
2291 		return -EBUSY;
2292 
2293 	if (!is_valid_ether_addr(addr->sa_data))
2294 		return -EADDRNOTAVAIL;
2295 
2296 	ether_addr_copy(netdev->dev_addr, addr->sa_data);
2297 
2298 	addr_lo = netdev->dev_addr[0] |
2299 		  netdev->dev_addr[1] << 8 |
2300 		  netdev->dev_addr[2] << 16 |
2301 		  netdev->dev_addr[3] << 24;
2302 	addr_hi = netdev->dev_addr[4] |
2303 		  netdev->dev_addr[5] << 8;
2304 
2305 	lan78xx_write_reg(dev, RX_ADDRL, addr_lo);
2306 	lan78xx_write_reg(dev, RX_ADDRH, addr_hi);
2307 
2308 	/* Added to support MAC address changes */
2309 	lan78xx_write_reg(dev, MAF_LO(0), addr_lo);
2310 	lan78xx_write_reg(dev, MAF_HI(0), addr_hi | MAF_HI_VALID_);
2311 
2312 	return 0;
2313 }
2314 
2315 /* Enable or disable Rx checksum offload engine */
2316 static int lan78xx_set_features(struct net_device *netdev,
2317 				netdev_features_t features)
2318 {
2319 	struct lan78xx_net *dev = netdev_priv(netdev);
2320 	struct lan78xx_priv *pdata = (struct lan78xx_priv *)(dev->data[0]);
2321 	unsigned long flags;
2322 
2323 	spin_lock_irqsave(&pdata->rfe_ctl_lock, flags);
2324 
2325 	if (features & NETIF_F_RXCSUM) {
2326 		pdata->rfe_ctl |= RFE_CTL_TCPUDP_COE_ | RFE_CTL_IP_COE_;
2327 		pdata->rfe_ctl |= RFE_CTL_ICMP_COE_ | RFE_CTL_IGMP_COE_;
2328 	} else {
2329 		pdata->rfe_ctl &= ~(RFE_CTL_TCPUDP_COE_ | RFE_CTL_IP_COE_);
2330 		pdata->rfe_ctl &= ~(RFE_CTL_ICMP_COE_ | RFE_CTL_IGMP_COE_);
2331 	}
2332 
2333 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
2334 		pdata->rfe_ctl |= RFE_CTL_VLAN_STRIP_;
2335 	else
2336 		pdata->rfe_ctl &= ~RFE_CTL_VLAN_STRIP_;
2337 
2338 	if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
2339 		pdata->rfe_ctl |= RFE_CTL_VLAN_FILTER_;
2340 	else
2341 		pdata->rfe_ctl &= ~RFE_CTL_VLAN_FILTER_;
2342 
2343 	spin_unlock_irqrestore(&pdata->rfe_ctl_lock, flags);
2344 
2345 	lan78xx_write_reg(dev, RFE_CTL, pdata->rfe_ctl);
2346 
2347 	return 0;
2348 }
2349 
2350 static void lan78xx_deferred_vlan_write(struct work_struct *param)
2351 {
2352 	struct lan78xx_priv *pdata =
2353 			container_of(param, struct lan78xx_priv, set_vlan);
2354 	struct lan78xx_net *dev = pdata->dev;
2355 
2356 	lan78xx_dataport_write(dev, DP_SEL_RSEL_VLAN_DA_, 0,
2357 			       DP_SEL_VHF_VLAN_LEN, pdata->vlan_table);
2358 }
2359 
2360 static int lan78xx_vlan_rx_add_vid(struct net_device *netdev,
2361 				   __be16 proto, u16 vid)
2362 {
2363 	struct lan78xx_net *dev = netdev_priv(netdev);
2364 	struct lan78xx_priv *pdata = (struct lan78xx_priv *)(dev->data[0]);
2365 	u16 vid_bit_index;
2366 	u16 vid_dword_index;
2367 
2368 	vid_dword_index = (vid >> 5) & 0x7F;
2369 	vid_bit_index = vid & 0x1F;
2370 
2371 	pdata->vlan_table[vid_dword_index] |= (1 << vid_bit_index);
2372 
2373 	/* defer register writes to a sleepable context */
2374 	schedule_work(&pdata->set_vlan);
2375 
2376 	return 0;
2377 }
2378 
2379 static int lan78xx_vlan_rx_kill_vid(struct net_device *netdev,
2380 				    __be16 proto, u16 vid)
2381 {
2382 	struct lan78xx_net *dev = netdev_priv(netdev);
2383 	struct lan78xx_priv *pdata = (struct lan78xx_priv *)(dev->data[0]);
2384 	u16 vid_bit_index;
2385 	u16 vid_dword_index;
2386 
2387 	vid_dword_index = (vid >> 5) & 0x7F;
2388 	vid_bit_index = vid & 0x1F;
2389 
2390 	pdata->vlan_table[vid_dword_index] &= ~(1 << vid_bit_index);
2391 
2392 	/* defer register writes to a sleepable context */
2393 	schedule_work(&pdata->set_vlan);
2394 
2395 	return 0;
2396 }
2397 
2398 static void lan78xx_init_ltm(struct lan78xx_net *dev)
2399 {
2400 	int ret;
2401 	u32 buf;
2402 	u32 regs[6] = { 0 };
2403 
2404 	ret = lan78xx_read_reg(dev, USB_CFG1, &buf);
2405 	if (buf & USB_CFG1_LTM_ENABLE_) {
2406 		u8 temp[2];
2407 		/* Get values from EEPROM first */
2408 		if (lan78xx_read_eeprom(dev, 0x3F, 2, temp) == 0) {
2409 			if (temp[0] == 24) {
2410 				ret = lan78xx_read_raw_eeprom(dev,
2411 							      temp[1] * 2,
2412 							      24,
2413 							      (u8 *)regs);
2414 				if (ret < 0)
2415 					return;
2416 			}
2417 		} else if (lan78xx_read_otp(dev, 0x3F, 2, temp) == 0) {
2418 			if (temp[0] == 24) {
2419 				ret = lan78xx_read_raw_otp(dev,
2420 							   temp[1] * 2,
2421 							   24,
2422 							   (u8 *)regs);
2423 				if (ret < 0)
2424 					return;
2425 			}
2426 		}
2427 	}
2428 
2429 	lan78xx_write_reg(dev, LTM_BELT_IDLE0, regs[0]);
2430 	lan78xx_write_reg(dev, LTM_BELT_IDLE1, regs[1]);
2431 	lan78xx_write_reg(dev, LTM_BELT_ACT0, regs[2]);
2432 	lan78xx_write_reg(dev, LTM_BELT_ACT1, regs[3]);
2433 	lan78xx_write_reg(dev, LTM_INACTIVE0, regs[4]);
2434 	lan78xx_write_reg(dev, LTM_INACTIVE1, regs[5]);
2435 }
2436 
2437 static int lan78xx_reset(struct lan78xx_net *dev)
2438 {
2439 	struct lan78xx_priv *pdata = (struct lan78xx_priv *)(dev->data[0]);
2440 	u32 buf;
2441 	int ret = 0;
2442 	unsigned long timeout;
2443 	u8 sig;
2444 
2445 	ret = lan78xx_read_reg(dev, HW_CFG, &buf);
2446 	buf |= HW_CFG_LRST_;
2447 	ret = lan78xx_write_reg(dev, HW_CFG, buf);
2448 
2449 	timeout = jiffies + HZ;
2450 	do {
2451 		mdelay(1);
2452 		ret = lan78xx_read_reg(dev, HW_CFG, &buf);
2453 		if (time_after(jiffies, timeout)) {
2454 			netdev_warn(dev->net,
2455 				    "timeout on completion of LiteReset");
2456 			return -EIO;
2457 		}
2458 	} while (buf & HW_CFG_LRST_);
2459 
2460 	lan78xx_init_mac_address(dev);
2461 
2462 	/* save DEVID for later usage */
2463 	ret = lan78xx_read_reg(dev, ID_REV, &buf);
2464 	dev->chipid = (buf & ID_REV_CHIP_ID_MASK_) >> 16;
2465 	dev->chiprev = buf & ID_REV_CHIP_REV_MASK_;
2466 
2467 	/* Respond to the IN token with a NAK */
2468 	ret = lan78xx_read_reg(dev, USB_CFG0, &buf);
2469 	buf |= USB_CFG_BIR_;
2470 	ret = lan78xx_write_reg(dev, USB_CFG0, buf);
2471 
2472 	/* Init LTM */
2473 	lan78xx_init_ltm(dev);
2474 
2475 	if (dev->udev->speed == USB_SPEED_SUPER) {
2476 		buf = DEFAULT_BURST_CAP_SIZE / SS_USB_PKT_SIZE;
2477 		dev->rx_urb_size = DEFAULT_BURST_CAP_SIZE;
2478 		dev->rx_qlen = 4;
2479 		dev->tx_qlen = 4;
2480 	} else if (dev->udev->speed == USB_SPEED_HIGH) {
2481 		buf = DEFAULT_BURST_CAP_SIZE / HS_USB_PKT_SIZE;
2482 		dev->rx_urb_size = DEFAULT_BURST_CAP_SIZE;
2483 		dev->rx_qlen = RX_MAX_QUEUE_MEMORY / dev->rx_urb_size;
2484 		dev->tx_qlen = RX_MAX_QUEUE_MEMORY / dev->hard_mtu;
2485 	} else {
2486 		buf = DEFAULT_BURST_CAP_SIZE / FS_USB_PKT_SIZE;
2487 		dev->rx_urb_size = DEFAULT_BURST_CAP_SIZE;
2488 		dev->rx_qlen = 4;
2489 		dev->tx_qlen = 4;
2490 	}
2491 
2492 	ret = lan78xx_write_reg(dev, BURST_CAP, buf);
2493 	ret = lan78xx_write_reg(dev, BULK_IN_DLY, DEFAULT_BULK_IN_DELAY);
2494 
2495 	ret = lan78xx_read_reg(dev, HW_CFG, &buf);
2496 	buf |= HW_CFG_MEF_;
2497 	ret = lan78xx_write_reg(dev, HW_CFG, buf);
2498 
2499 	ret = lan78xx_read_reg(dev, USB_CFG0, &buf);
2500 	buf |= USB_CFG_BCE_;
2501 	ret = lan78xx_write_reg(dev, USB_CFG0, buf);
2502 
2503 	/* set FIFO sizes */
2504 	buf = (MAX_RX_FIFO_SIZE - 512) / 512;
2505 	ret = lan78xx_write_reg(dev, FCT_RX_FIFO_END, buf);
2506 
2507 	buf = (MAX_TX_FIFO_SIZE - 512) / 512;
2508 	ret = lan78xx_write_reg(dev, FCT_TX_FIFO_END, buf);
2509 
2510 	ret = lan78xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
2511 	ret = lan78xx_write_reg(dev, FLOW, 0);
2512 	ret = lan78xx_write_reg(dev, FCT_FLOW, 0);
2513 
2514 	/* Don't need rfe_ctl_lock during initialisation */
2515 	ret = lan78xx_read_reg(dev, RFE_CTL, &pdata->rfe_ctl);
2516 	pdata->rfe_ctl |= RFE_CTL_BCAST_EN_ | RFE_CTL_DA_PERFECT_;
2517 	ret = lan78xx_write_reg(dev, RFE_CTL, pdata->rfe_ctl);
2518 
2519 	/* Enable or disable checksum offload engines */
2520 	lan78xx_set_features(dev->net, dev->net->features);
2521 
2522 	lan78xx_set_multicast(dev->net);
2523 
2524 	/* reset PHY */
2525 	ret = lan78xx_read_reg(dev, PMT_CTL, &buf);
2526 	buf |= PMT_CTL_PHY_RST_;
2527 	ret = lan78xx_write_reg(dev, PMT_CTL, buf);
2528 
2529 	timeout = jiffies + HZ;
2530 	do {
2531 		mdelay(1);
2532 		ret = lan78xx_read_reg(dev, PMT_CTL, &buf);
2533 		if (time_after(jiffies, timeout)) {
2534 			netdev_warn(dev->net, "timeout waiting for PHY Reset");
2535 			return -EIO;
2536 		}
2537 	} while ((buf & PMT_CTL_PHY_RST_) || !(buf & PMT_CTL_READY_));
2538 
2539 	ret = lan78xx_read_reg(dev, MAC_CR, &buf);
2540 	/* LAN7801 only has RGMII mode */
2541 	if (dev->chipid == ID_REV_CHIP_ID_7801_)
2542 		buf &= ~MAC_CR_GMII_EN_;
2543 
2544 	if (dev->chipid == ID_REV_CHIP_ID_7800_) {
2545 		ret = lan78xx_read_raw_eeprom(dev, 0, 1, &sig);
2546 		if (!ret && sig != EEPROM_INDICATOR) {
2547 			/* Implies there is no external eeprom. Set mac speed */
2548 			netdev_info(dev->net, "No External EEPROM. Setting MAC Speed\n");
2549 			buf |= MAC_CR_AUTO_DUPLEX_ | MAC_CR_AUTO_SPEED_;
2550 		}
2551 	}
2552 	ret = lan78xx_write_reg(dev, MAC_CR, buf);
2553 
2554 	ret = lan78xx_read_reg(dev, MAC_TX, &buf);
2555 	buf |= MAC_TX_TXEN_;
2556 	ret = lan78xx_write_reg(dev, MAC_TX, buf);
2557 
2558 	ret = lan78xx_read_reg(dev, FCT_TX_CTL, &buf);
2559 	buf |= FCT_TX_CTL_EN_;
2560 	ret = lan78xx_write_reg(dev, FCT_TX_CTL, buf);
2561 
2562 	ret = lan78xx_set_rx_max_frame_length(dev,
2563 					      dev->net->mtu + VLAN_ETH_HLEN);
2564 
2565 	ret = lan78xx_read_reg(dev, MAC_RX, &buf);
2566 	buf |= MAC_RX_RXEN_;
2567 	ret = lan78xx_write_reg(dev, MAC_RX, buf);
2568 
2569 	ret = lan78xx_read_reg(dev, FCT_RX_CTL, &buf);
2570 	buf |= FCT_RX_CTL_EN_;
2571 	ret = lan78xx_write_reg(dev, FCT_RX_CTL, buf);
2572 
2573 	return 0;
2574 }
2575 
2576 static void lan78xx_init_stats(struct lan78xx_net *dev)
2577 {
2578 	u32 *p;
2579 	int i;
2580 
2581 	/* initialize for stats update
2582 	 * some counters are 20bits and some are 32bits
2583 	 */
2584 	p = (u32 *)&dev->stats.rollover_max;
2585 	for (i = 0; i < (sizeof(dev->stats.rollover_max) / (sizeof(u32))); i++)
2586 		p[i] = 0xFFFFF;
2587 
2588 	dev->stats.rollover_max.rx_unicast_byte_count = 0xFFFFFFFF;
2589 	dev->stats.rollover_max.rx_broadcast_byte_count = 0xFFFFFFFF;
2590 	dev->stats.rollover_max.rx_multicast_byte_count = 0xFFFFFFFF;
2591 	dev->stats.rollover_max.eee_rx_lpi_transitions = 0xFFFFFFFF;
2592 	dev->stats.rollover_max.eee_rx_lpi_time = 0xFFFFFFFF;
2593 	dev->stats.rollover_max.tx_unicast_byte_count = 0xFFFFFFFF;
2594 	dev->stats.rollover_max.tx_broadcast_byte_count = 0xFFFFFFFF;
2595 	dev->stats.rollover_max.tx_multicast_byte_count = 0xFFFFFFFF;
2596 	dev->stats.rollover_max.eee_tx_lpi_transitions = 0xFFFFFFFF;
2597 	dev->stats.rollover_max.eee_tx_lpi_time = 0xFFFFFFFF;
2598 
2599 	set_bit(EVENT_STAT_UPDATE, &dev->flags);
2600 }
2601 
2602 static int lan78xx_open(struct net_device *net)
2603 {
2604 	struct lan78xx_net *dev = netdev_priv(net);
2605 	int ret;
2606 
2607 	ret = usb_autopm_get_interface(dev->intf);
2608 	if (ret < 0)
2609 		goto out;
2610 
2611 	phy_start(net->phydev);
2612 
2613 	netif_dbg(dev, ifup, dev->net, "phy initialised successfully");
2614 
2615 	/* for Link Check */
2616 	if (dev->urb_intr) {
2617 		ret = usb_submit_urb(dev->urb_intr, GFP_KERNEL);
2618 		if (ret < 0) {
2619 			netif_err(dev, ifup, dev->net,
2620 				  "intr submit %d\n", ret);
2621 			goto done;
2622 		}
2623 	}
2624 
2625 	lan78xx_init_stats(dev);
2626 
2627 	set_bit(EVENT_DEV_OPEN, &dev->flags);
2628 
2629 	netif_start_queue(net);
2630 
2631 	dev->link_on = false;
2632 
2633 	lan78xx_defer_kevent(dev, EVENT_LINK_RESET);
2634 done:
2635 	usb_autopm_put_interface(dev->intf);
2636 
2637 out:
2638 	return ret;
2639 }
2640 
2641 static void lan78xx_terminate_urbs(struct lan78xx_net *dev)
2642 {
2643 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(unlink_wakeup);
2644 	DECLARE_WAITQUEUE(wait, current);
2645 	int temp;
2646 
2647 	/* ensure there are no more active urbs */
2648 	add_wait_queue(&unlink_wakeup, &wait);
2649 	set_current_state(TASK_UNINTERRUPTIBLE);
2650 	dev->wait = &unlink_wakeup;
2651 	temp = unlink_urbs(dev, &dev->txq) + unlink_urbs(dev, &dev->rxq);
2652 
2653 	/* maybe wait for deletions to finish. */
2654 	while (!skb_queue_empty(&dev->rxq) &&
2655 	       !skb_queue_empty(&dev->txq) &&
2656 	       !skb_queue_empty(&dev->done)) {
2657 		schedule_timeout(msecs_to_jiffies(UNLINK_TIMEOUT_MS));
2658 		set_current_state(TASK_UNINTERRUPTIBLE);
2659 		netif_dbg(dev, ifdown, dev->net,
2660 			  "waited for %d urb completions\n", temp);
2661 	}
2662 	set_current_state(TASK_RUNNING);
2663 	dev->wait = NULL;
2664 	remove_wait_queue(&unlink_wakeup, &wait);
2665 }
2666 
2667 static int lan78xx_stop(struct net_device *net)
2668 {
2669 	struct lan78xx_net *dev = netdev_priv(net);
2670 
2671 	if (timer_pending(&dev->stat_monitor))
2672 		del_timer_sync(&dev->stat_monitor);
2673 
2674 	if (net->phydev)
2675 		phy_stop(net->phydev);
2676 
2677 	clear_bit(EVENT_DEV_OPEN, &dev->flags);
2678 	netif_stop_queue(net);
2679 
2680 	netif_info(dev, ifdown, dev->net,
2681 		   "stop stats: rx/tx %lu/%lu, errs %lu/%lu\n",
2682 		   net->stats.rx_packets, net->stats.tx_packets,
2683 		   net->stats.rx_errors, net->stats.tx_errors);
2684 
2685 	lan78xx_terminate_urbs(dev);
2686 
2687 	usb_kill_urb(dev->urb_intr);
2688 
2689 	skb_queue_purge(&dev->rxq_pause);
2690 
2691 	/* deferred work (task, timer, softirq) must also stop.
2692 	 * can't flush_scheduled_work() until we drop rtnl (later),
2693 	 * else workers could deadlock; so make workers a NOP.
2694 	 */
2695 	dev->flags = 0;
2696 	cancel_delayed_work_sync(&dev->wq);
2697 	tasklet_kill(&dev->bh);
2698 
2699 	usb_autopm_put_interface(dev->intf);
2700 
2701 	return 0;
2702 }
2703 
2704 static struct sk_buff *lan78xx_tx_prep(struct lan78xx_net *dev,
2705 				       struct sk_buff *skb, gfp_t flags)
2706 {
2707 	u32 tx_cmd_a, tx_cmd_b;
2708 	void *ptr;
2709 
2710 	if (skb_cow_head(skb, TX_OVERHEAD)) {
2711 		dev_kfree_skb_any(skb);
2712 		return NULL;
2713 	}
2714 
2715 	if (skb_linearize(skb)) {
2716 		dev_kfree_skb_any(skb);
2717 		return NULL;
2718 	}
2719 
2720 	tx_cmd_a = (u32)(skb->len & TX_CMD_A_LEN_MASK_) | TX_CMD_A_FCS_;
2721 
2722 	if (skb->ip_summed == CHECKSUM_PARTIAL)
2723 		tx_cmd_a |= TX_CMD_A_IPE_ | TX_CMD_A_TPE_;
2724 
2725 	tx_cmd_b = 0;
2726 	if (skb_is_gso(skb)) {
2727 		u16 mss = max(skb_shinfo(skb)->gso_size, TX_CMD_B_MSS_MIN_);
2728 
2729 		tx_cmd_b = (mss << TX_CMD_B_MSS_SHIFT_) & TX_CMD_B_MSS_MASK_;
2730 
2731 		tx_cmd_a |= TX_CMD_A_LSO_;
2732 	}
2733 
2734 	if (skb_vlan_tag_present(skb)) {
2735 		tx_cmd_a |= TX_CMD_A_IVTG_;
2736 		tx_cmd_b |= skb_vlan_tag_get(skb) & TX_CMD_B_VTAG_MASK_;
2737 	}
2738 
2739 	ptr = skb_push(skb, 8);
2740 	put_unaligned_le32(tx_cmd_a, ptr);
2741 	put_unaligned_le32(tx_cmd_b, ptr + 4);
2742 
2743 	return skb;
2744 }
2745 
2746 static enum skb_state defer_bh(struct lan78xx_net *dev, struct sk_buff *skb,
2747 			       struct sk_buff_head *list, enum skb_state state)
2748 {
2749 	unsigned long flags;
2750 	enum skb_state old_state;
2751 	struct skb_data *entry = (struct skb_data *)skb->cb;
2752 
2753 	spin_lock_irqsave(&list->lock, flags);
2754 	old_state = entry->state;
2755 	entry->state = state;
2756 
2757 	__skb_unlink(skb, list);
2758 	spin_unlock(&list->lock);
2759 	spin_lock(&dev->done.lock);
2760 
2761 	__skb_queue_tail(&dev->done, skb);
2762 	if (skb_queue_len(&dev->done) == 1)
2763 		tasklet_schedule(&dev->bh);
2764 	spin_unlock_irqrestore(&dev->done.lock, flags);
2765 
2766 	return old_state;
2767 }
2768 
2769 static void tx_complete(struct urb *urb)
2770 {
2771 	struct sk_buff *skb = (struct sk_buff *)urb->context;
2772 	struct skb_data *entry = (struct skb_data *)skb->cb;
2773 	struct lan78xx_net *dev = entry->dev;
2774 
2775 	if (urb->status == 0) {
2776 		dev->net->stats.tx_packets += entry->num_of_packet;
2777 		dev->net->stats.tx_bytes += entry->length;
2778 	} else {
2779 		dev->net->stats.tx_errors++;
2780 
2781 		switch (urb->status) {
2782 		case -EPIPE:
2783 			lan78xx_defer_kevent(dev, EVENT_TX_HALT);
2784 			break;
2785 
2786 		/* software-driven interface shutdown */
2787 		case -ECONNRESET:
2788 		case -ESHUTDOWN:
2789 			break;
2790 
2791 		case -EPROTO:
2792 		case -ETIME:
2793 		case -EILSEQ:
2794 			netif_stop_queue(dev->net);
2795 			break;
2796 		default:
2797 			netif_dbg(dev, tx_err, dev->net,
2798 				  "tx err %d\n", entry->urb->status);
2799 			break;
2800 		}
2801 	}
2802 
2803 	usb_autopm_put_interface_async(dev->intf);
2804 
2805 	defer_bh(dev, skb, &dev->txq, tx_done);
2806 }
2807 
2808 static void lan78xx_queue_skb(struct sk_buff_head *list,
2809 			      struct sk_buff *newsk, enum skb_state state)
2810 {
2811 	struct skb_data *entry = (struct skb_data *)newsk->cb;
2812 
2813 	__skb_queue_tail(list, newsk);
2814 	entry->state = state;
2815 }
2816 
2817 static netdev_tx_t
2818 lan78xx_start_xmit(struct sk_buff *skb, struct net_device *net)
2819 {
2820 	struct lan78xx_net *dev = netdev_priv(net);
2821 	struct sk_buff *skb2 = NULL;
2822 
2823 	if (skb) {
2824 		skb_tx_timestamp(skb);
2825 		skb2 = lan78xx_tx_prep(dev, skb, GFP_ATOMIC);
2826 	}
2827 
2828 	if (skb2) {
2829 		skb_queue_tail(&dev->txq_pend, skb2);
2830 
2831 		/* throttle TX patch at slower than SUPER SPEED USB */
2832 		if ((dev->udev->speed < USB_SPEED_SUPER) &&
2833 		    (skb_queue_len(&dev->txq_pend) > 10))
2834 			netif_stop_queue(net);
2835 	} else {
2836 		netif_dbg(dev, tx_err, dev->net,
2837 			  "lan78xx_tx_prep return NULL\n");
2838 		dev->net->stats.tx_errors++;
2839 		dev->net->stats.tx_dropped++;
2840 	}
2841 
2842 	tasklet_schedule(&dev->bh);
2843 
2844 	return NETDEV_TX_OK;
2845 }
2846 
2847 static int lan78xx_bind(struct lan78xx_net *dev, struct usb_interface *intf)
2848 {
2849 	struct lan78xx_priv *pdata = NULL;
2850 	int ret;
2851 	int i;
2852 
2853 	dev->data[0] = (unsigned long)kzalloc(sizeof(*pdata), GFP_KERNEL);
2854 
2855 	pdata = (struct lan78xx_priv *)(dev->data[0]);
2856 	if (!pdata) {
2857 		netdev_warn(dev->net, "Unable to allocate lan78xx_priv");
2858 		return -ENOMEM;
2859 	}
2860 
2861 	pdata->dev = dev;
2862 
2863 	spin_lock_init(&pdata->rfe_ctl_lock);
2864 	mutex_init(&pdata->dataport_mutex);
2865 
2866 	INIT_WORK(&pdata->set_multicast, lan78xx_deferred_multicast_write);
2867 
2868 	for (i = 0; i < DP_SEL_VHF_VLAN_LEN; i++)
2869 		pdata->vlan_table[i] = 0;
2870 
2871 	INIT_WORK(&pdata->set_vlan, lan78xx_deferred_vlan_write);
2872 
2873 	dev->net->features = 0;
2874 
2875 	if (DEFAULT_TX_CSUM_ENABLE)
2876 		dev->net->features |= NETIF_F_HW_CSUM;
2877 
2878 	if (DEFAULT_RX_CSUM_ENABLE)
2879 		dev->net->features |= NETIF_F_RXCSUM;
2880 
2881 	if (DEFAULT_TSO_CSUM_ENABLE)
2882 		dev->net->features |= NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_SG;
2883 
2884 	if (DEFAULT_VLAN_RX_OFFLOAD)
2885 		dev->net->features |= NETIF_F_HW_VLAN_CTAG_RX;
2886 
2887 	if (DEFAULT_VLAN_FILTER_ENABLE)
2888 		dev->net->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
2889 
2890 	dev->net->hw_features = dev->net->features;
2891 
2892 	ret = lan78xx_setup_irq_domain(dev);
2893 	if (ret < 0) {
2894 		netdev_warn(dev->net,
2895 			    "lan78xx_setup_irq_domain() failed : %d", ret);
2896 		goto out1;
2897 	}
2898 
2899 	dev->net->hard_header_len += TX_OVERHEAD;
2900 	dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
2901 
2902 	/* Init all registers */
2903 	ret = lan78xx_reset(dev);
2904 	if (ret) {
2905 		netdev_warn(dev->net, "Registers INIT FAILED....");
2906 		goto out2;
2907 	}
2908 
2909 	ret = lan78xx_mdio_init(dev);
2910 	if (ret) {
2911 		netdev_warn(dev->net, "MDIO INIT FAILED.....");
2912 		goto out2;
2913 	}
2914 
2915 	dev->net->flags |= IFF_MULTICAST;
2916 
2917 	pdata->wol = WAKE_MAGIC;
2918 
2919 	return ret;
2920 
2921 out2:
2922 	lan78xx_remove_irq_domain(dev);
2923 
2924 out1:
2925 	netdev_warn(dev->net, "Bind routine FAILED");
2926 	cancel_work_sync(&pdata->set_multicast);
2927 	cancel_work_sync(&pdata->set_vlan);
2928 	kfree(pdata);
2929 	return ret;
2930 }
2931 
2932 static void lan78xx_unbind(struct lan78xx_net *dev, struct usb_interface *intf)
2933 {
2934 	struct lan78xx_priv *pdata = (struct lan78xx_priv *)(dev->data[0]);
2935 
2936 	lan78xx_remove_irq_domain(dev);
2937 
2938 	lan78xx_remove_mdio(dev);
2939 
2940 	if (pdata) {
2941 		cancel_work_sync(&pdata->set_multicast);
2942 		cancel_work_sync(&pdata->set_vlan);
2943 		netif_dbg(dev, ifdown, dev->net, "free pdata");
2944 		kfree(pdata);
2945 		pdata = NULL;
2946 		dev->data[0] = 0;
2947 	}
2948 }
2949 
2950 static void lan78xx_rx_csum_offload(struct lan78xx_net *dev,
2951 				    struct sk_buff *skb,
2952 				    u32 rx_cmd_a, u32 rx_cmd_b)
2953 {
2954 	/* HW Checksum offload appears to be flawed if used when not stripping
2955 	 * VLAN headers. Drop back to S/W checksums under these conditions.
2956 	 */
2957 	if (!(dev->net->features & NETIF_F_RXCSUM) ||
2958 	    unlikely(rx_cmd_a & RX_CMD_A_ICSM_) ||
2959 	    ((rx_cmd_a & RX_CMD_A_FVTG_) &&
2960 	     !(dev->net->features & NETIF_F_HW_VLAN_CTAG_RX))) {
2961 		skb->ip_summed = CHECKSUM_NONE;
2962 	} else {
2963 		skb->csum = ntohs((u16)(rx_cmd_b >> RX_CMD_B_CSUM_SHIFT_));
2964 		skb->ip_summed = CHECKSUM_COMPLETE;
2965 	}
2966 }
2967 
2968 static void lan78xx_rx_vlan_offload(struct lan78xx_net *dev,
2969 				    struct sk_buff *skb,
2970 				    u32 rx_cmd_a, u32 rx_cmd_b)
2971 {
2972 	if ((dev->net->features & NETIF_F_HW_VLAN_CTAG_RX) &&
2973 	    (rx_cmd_a & RX_CMD_A_FVTG_))
2974 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2975 				       (rx_cmd_b & 0xffff));
2976 }
2977 
2978 static void lan78xx_skb_return(struct lan78xx_net *dev, struct sk_buff *skb)
2979 {
2980 	int status;
2981 
2982 	if (test_bit(EVENT_RX_PAUSED, &dev->flags)) {
2983 		skb_queue_tail(&dev->rxq_pause, skb);
2984 		return;
2985 	}
2986 
2987 	dev->net->stats.rx_packets++;
2988 	dev->net->stats.rx_bytes += skb->len;
2989 
2990 	skb->protocol = eth_type_trans(skb, dev->net);
2991 
2992 	netif_dbg(dev, rx_status, dev->net, "< rx, len %zu, type 0x%x\n",
2993 		  skb->len + sizeof(struct ethhdr), skb->protocol);
2994 	memset(skb->cb, 0, sizeof(struct skb_data));
2995 
2996 	if (skb_defer_rx_timestamp(skb))
2997 		return;
2998 
2999 	status = netif_rx(skb);
3000 	if (status != NET_RX_SUCCESS)
3001 		netif_dbg(dev, rx_err, dev->net,
3002 			  "netif_rx status %d\n", status);
3003 }
3004 
3005 static int lan78xx_rx(struct lan78xx_net *dev, struct sk_buff *skb)
3006 {
3007 	if (skb->len < dev->net->hard_header_len)
3008 		return 0;
3009 
3010 	while (skb->len > 0) {
3011 		u32 rx_cmd_a, rx_cmd_b, align_count, size;
3012 		u16 rx_cmd_c;
3013 		struct sk_buff *skb2;
3014 		unsigned char *packet;
3015 
3016 		rx_cmd_a = get_unaligned_le32(skb->data);
3017 		skb_pull(skb, sizeof(rx_cmd_a));
3018 
3019 		rx_cmd_b = get_unaligned_le32(skb->data);
3020 		skb_pull(skb, sizeof(rx_cmd_b));
3021 
3022 		rx_cmd_c = get_unaligned_le16(skb->data);
3023 		skb_pull(skb, sizeof(rx_cmd_c));
3024 
3025 		packet = skb->data;
3026 
3027 		/* get the packet length */
3028 		size = (rx_cmd_a & RX_CMD_A_LEN_MASK_);
3029 		align_count = (4 - ((size + RXW_PADDING) % 4)) % 4;
3030 
3031 		if (unlikely(rx_cmd_a & RX_CMD_A_RED_)) {
3032 			netif_dbg(dev, rx_err, dev->net,
3033 				  "Error rx_cmd_a=0x%08x", rx_cmd_a);
3034 		} else {
3035 			/* last frame in this batch */
3036 			if (skb->len == size) {
3037 				lan78xx_rx_csum_offload(dev, skb,
3038 							rx_cmd_a, rx_cmd_b);
3039 				lan78xx_rx_vlan_offload(dev, skb,
3040 							rx_cmd_a, rx_cmd_b);
3041 
3042 				skb_trim(skb, skb->len - 4); /* remove fcs */
3043 				skb->truesize = size + sizeof(struct sk_buff);
3044 
3045 				return 1;
3046 			}
3047 
3048 			skb2 = skb_clone(skb, GFP_ATOMIC);
3049 			if (unlikely(!skb2)) {
3050 				netdev_warn(dev->net, "Error allocating skb");
3051 				return 0;
3052 			}
3053 
3054 			skb2->len = size;
3055 			skb2->data = packet;
3056 			skb_set_tail_pointer(skb2, size);
3057 
3058 			lan78xx_rx_csum_offload(dev, skb2, rx_cmd_a, rx_cmd_b);
3059 			lan78xx_rx_vlan_offload(dev, skb2, rx_cmd_a, rx_cmd_b);
3060 
3061 			skb_trim(skb2, skb2->len - 4); /* remove fcs */
3062 			skb2->truesize = size + sizeof(struct sk_buff);
3063 
3064 			lan78xx_skb_return(dev, skb2);
3065 		}
3066 
3067 		skb_pull(skb, size);
3068 
3069 		/* padding bytes before the next frame starts */
3070 		if (skb->len)
3071 			skb_pull(skb, align_count);
3072 	}
3073 
3074 	return 1;
3075 }
3076 
3077 static inline void rx_process(struct lan78xx_net *dev, struct sk_buff *skb)
3078 {
3079 	if (!lan78xx_rx(dev, skb)) {
3080 		dev->net->stats.rx_errors++;
3081 		goto done;
3082 	}
3083 
3084 	if (skb->len) {
3085 		lan78xx_skb_return(dev, skb);
3086 		return;
3087 	}
3088 
3089 	netif_dbg(dev, rx_err, dev->net, "drop\n");
3090 	dev->net->stats.rx_errors++;
3091 done:
3092 	skb_queue_tail(&dev->done, skb);
3093 }
3094 
3095 static void rx_complete(struct urb *urb);
3096 
3097 static int rx_submit(struct lan78xx_net *dev, struct urb *urb, gfp_t flags)
3098 {
3099 	struct sk_buff *skb;
3100 	struct skb_data *entry;
3101 	unsigned long lockflags;
3102 	size_t size = dev->rx_urb_size;
3103 	int ret = 0;
3104 
3105 	skb = netdev_alloc_skb_ip_align(dev->net, size);
3106 	if (!skb) {
3107 		usb_free_urb(urb);
3108 		return -ENOMEM;
3109 	}
3110 
3111 	entry = (struct skb_data *)skb->cb;
3112 	entry->urb = urb;
3113 	entry->dev = dev;
3114 	entry->length = 0;
3115 
3116 	usb_fill_bulk_urb(urb, dev->udev, dev->pipe_in,
3117 			  skb->data, size, rx_complete, skb);
3118 
3119 	spin_lock_irqsave(&dev->rxq.lock, lockflags);
3120 
3121 	if (netif_device_present(dev->net) &&
3122 	    netif_running(dev->net) &&
3123 	    !test_bit(EVENT_RX_HALT, &dev->flags) &&
3124 	    !test_bit(EVENT_DEV_ASLEEP, &dev->flags)) {
3125 		ret = usb_submit_urb(urb, GFP_ATOMIC);
3126 		switch (ret) {
3127 		case 0:
3128 			lan78xx_queue_skb(&dev->rxq, skb, rx_start);
3129 			break;
3130 		case -EPIPE:
3131 			lan78xx_defer_kevent(dev, EVENT_RX_HALT);
3132 			break;
3133 		case -ENODEV:
3134 			netif_dbg(dev, ifdown, dev->net, "device gone\n");
3135 			netif_device_detach(dev->net);
3136 			break;
3137 		case -EHOSTUNREACH:
3138 			ret = -ENOLINK;
3139 			break;
3140 		default:
3141 			netif_dbg(dev, rx_err, dev->net,
3142 				  "rx submit, %d\n", ret);
3143 			tasklet_schedule(&dev->bh);
3144 		}
3145 	} else {
3146 		netif_dbg(dev, ifdown, dev->net, "rx: stopped\n");
3147 		ret = -ENOLINK;
3148 	}
3149 	spin_unlock_irqrestore(&dev->rxq.lock, lockflags);
3150 	if (ret) {
3151 		dev_kfree_skb_any(skb);
3152 		usb_free_urb(urb);
3153 	}
3154 	return ret;
3155 }
3156 
3157 static void rx_complete(struct urb *urb)
3158 {
3159 	struct sk_buff	*skb = (struct sk_buff *)urb->context;
3160 	struct skb_data	*entry = (struct skb_data *)skb->cb;
3161 	struct lan78xx_net *dev = entry->dev;
3162 	int urb_status = urb->status;
3163 	enum skb_state state;
3164 
3165 	skb_put(skb, urb->actual_length);
3166 	state = rx_done;
3167 	entry->urb = NULL;
3168 
3169 	switch (urb_status) {
3170 	case 0:
3171 		if (skb->len < dev->net->hard_header_len) {
3172 			state = rx_cleanup;
3173 			dev->net->stats.rx_errors++;
3174 			dev->net->stats.rx_length_errors++;
3175 			netif_dbg(dev, rx_err, dev->net,
3176 				  "rx length %d\n", skb->len);
3177 		}
3178 		usb_mark_last_busy(dev->udev);
3179 		break;
3180 	case -EPIPE:
3181 		dev->net->stats.rx_errors++;
3182 		lan78xx_defer_kevent(dev, EVENT_RX_HALT);
3183 		fallthrough;
3184 	case -ECONNRESET:				/* async unlink */
3185 	case -ESHUTDOWN:				/* hardware gone */
3186 		netif_dbg(dev, ifdown, dev->net,
3187 			  "rx shutdown, code %d\n", urb_status);
3188 		state = rx_cleanup;
3189 		entry->urb = urb;
3190 		urb = NULL;
3191 		break;
3192 	case -EPROTO:
3193 	case -ETIME:
3194 	case -EILSEQ:
3195 		dev->net->stats.rx_errors++;
3196 		state = rx_cleanup;
3197 		entry->urb = urb;
3198 		urb = NULL;
3199 		break;
3200 
3201 	/* data overrun ... flush fifo? */
3202 	case -EOVERFLOW:
3203 		dev->net->stats.rx_over_errors++;
3204 		fallthrough;
3205 
3206 	default:
3207 		state = rx_cleanup;
3208 		dev->net->stats.rx_errors++;
3209 		netif_dbg(dev, rx_err, dev->net, "rx status %d\n", urb_status);
3210 		break;
3211 	}
3212 
3213 	state = defer_bh(dev, skb, &dev->rxq, state);
3214 
3215 	if (urb) {
3216 		if (netif_running(dev->net) &&
3217 		    !test_bit(EVENT_RX_HALT, &dev->flags) &&
3218 		    state != unlink_start) {
3219 			rx_submit(dev, urb, GFP_ATOMIC);
3220 			return;
3221 		}
3222 		usb_free_urb(urb);
3223 	}
3224 	netif_dbg(dev, rx_err, dev->net, "no read resubmitted\n");
3225 }
3226 
3227 static void lan78xx_tx_bh(struct lan78xx_net *dev)
3228 {
3229 	int length;
3230 	struct urb *urb = NULL;
3231 	struct skb_data *entry;
3232 	unsigned long flags;
3233 	struct sk_buff_head *tqp = &dev->txq_pend;
3234 	struct sk_buff *skb, *skb2;
3235 	int ret;
3236 	int count, pos;
3237 	int skb_totallen, pkt_cnt;
3238 
3239 	skb_totallen = 0;
3240 	pkt_cnt = 0;
3241 	count = 0;
3242 	length = 0;
3243 	spin_lock_irqsave(&tqp->lock, flags);
3244 	skb_queue_walk(tqp, skb) {
3245 		if (skb_is_gso(skb)) {
3246 			if (!skb_queue_is_first(tqp, skb)) {
3247 				/* handle previous packets first */
3248 				break;
3249 			}
3250 			count = 1;
3251 			length = skb->len - TX_OVERHEAD;
3252 			__skb_unlink(skb, tqp);
3253 			spin_unlock_irqrestore(&tqp->lock, flags);
3254 			goto gso_skb;
3255 		}
3256 
3257 		if ((skb_totallen + skb->len) > MAX_SINGLE_PACKET_SIZE)
3258 			break;
3259 		skb_totallen = skb->len + roundup(skb_totallen, sizeof(u32));
3260 		pkt_cnt++;
3261 	}
3262 	spin_unlock_irqrestore(&tqp->lock, flags);
3263 
3264 	/* copy to a single skb */
3265 	skb = alloc_skb(skb_totallen, GFP_ATOMIC);
3266 	if (!skb)
3267 		goto drop;
3268 
3269 	skb_put(skb, skb_totallen);
3270 
3271 	for (count = pos = 0; count < pkt_cnt; count++) {
3272 		skb2 = skb_dequeue(tqp);
3273 		if (skb2) {
3274 			length += (skb2->len - TX_OVERHEAD);
3275 			memcpy(skb->data + pos, skb2->data, skb2->len);
3276 			pos += roundup(skb2->len, sizeof(u32));
3277 			dev_kfree_skb(skb2);
3278 		}
3279 	}
3280 
3281 gso_skb:
3282 	urb = usb_alloc_urb(0, GFP_ATOMIC);
3283 	if (!urb)
3284 		goto drop;
3285 
3286 	entry = (struct skb_data *)skb->cb;
3287 	entry->urb = urb;
3288 	entry->dev = dev;
3289 	entry->length = length;
3290 	entry->num_of_packet = count;
3291 
3292 	spin_lock_irqsave(&dev->txq.lock, flags);
3293 	ret = usb_autopm_get_interface_async(dev->intf);
3294 	if (ret < 0) {
3295 		spin_unlock_irqrestore(&dev->txq.lock, flags);
3296 		goto drop;
3297 	}
3298 
3299 	usb_fill_bulk_urb(urb, dev->udev, dev->pipe_out,
3300 			  skb->data, skb->len, tx_complete, skb);
3301 
3302 	if (length % dev->maxpacket == 0) {
3303 		/* send USB_ZERO_PACKET */
3304 		urb->transfer_flags |= URB_ZERO_PACKET;
3305 	}
3306 
3307 #ifdef CONFIG_PM
3308 	/* if this triggers the device is still a sleep */
3309 	if (test_bit(EVENT_DEV_ASLEEP, &dev->flags)) {
3310 		/* transmission will be done in resume */
3311 		usb_anchor_urb(urb, &dev->deferred);
3312 		/* no use to process more packets */
3313 		netif_stop_queue(dev->net);
3314 		usb_put_urb(urb);
3315 		spin_unlock_irqrestore(&dev->txq.lock, flags);
3316 		netdev_dbg(dev->net, "Delaying transmission for resumption\n");
3317 		return;
3318 	}
3319 #endif
3320 
3321 	ret = usb_submit_urb(urb, GFP_ATOMIC);
3322 	switch (ret) {
3323 	case 0:
3324 		netif_trans_update(dev->net);
3325 		lan78xx_queue_skb(&dev->txq, skb, tx_start);
3326 		if (skb_queue_len(&dev->txq) >= dev->tx_qlen)
3327 			netif_stop_queue(dev->net);
3328 		break;
3329 	case -EPIPE:
3330 		netif_stop_queue(dev->net);
3331 		lan78xx_defer_kevent(dev, EVENT_TX_HALT);
3332 		usb_autopm_put_interface_async(dev->intf);
3333 		break;
3334 	default:
3335 		usb_autopm_put_interface_async(dev->intf);
3336 		netif_dbg(dev, tx_err, dev->net,
3337 			  "tx: submit urb err %d\n", ret);
3338 		break;
3339 	}
3340 
3341 	spin_unlock_irqrestore(&dev->txq.lock, flags);
3342 
3343 	if (ret) {
3344 		netif_dbg(dev, tx_err, dev->net, "drop, code %d\n", ret);
3345 drop:
3346 		dev->net->stats.tx_dropped++;
3347 		if (skb)
3348 			dev_kfree_skb_any(skb);
3349 		usb_free_urb(urb);
3350 	} else
3351 		netif_dbg(dev, tx_queued, dev->net,
3352 			  "> tx, len %d, type 0x%x\n", length, skb->protocol);
3353 }
3354 
3355 static void lan78xx_rx_bh(struct lan78xx_net *dev)
3356 {
3357 	struct urb *urb;
3358 	int i;
3359 
3360 	if (skb_queue_len(&dev->rxq) < dev->rx_qlen) {
3361 		for (i = 0; i < 10; i++) {
3362 			if (skb_queue_len(&dev->rxq) >= dev->rx_qlen)
3363 				break;
3364 			urb = usb_alloc_urb(0, GFP_ATOMIC);
3365 			if (urb)
3366 				if (rx_submit(dev, urb, GFP_ATOMIC) == -ENOLINK)
3367 					return;
3368 		}
3369 
3370 		if (skb_queue_len(&dev->rxq) < dev->rx_qlen)
3371 			tasklet_schedule(&dev->bh);
3372 	}
3373 	if (skb_queue_len(&dev->txq) < dev->tx_qlen)
3374 		netif_wake_queue(dev->net);
3375 }
3376 
3377 static void lan78xx_bh(struct tasklet_struct *t)
3378 {
3379 	struct lan78xx_net *dev = from_tasklet(dev, t, bh);
3380 	struct sk_buff *skb;
3381 	struct skb_data *entry;
3382 
3383 	while ((skb = skb_dequeue(&dev->done))) {
3384 		entry = (struct skb_data *)(skb->cb);
3385 		switch (entry->state) {
3386 		case rx_done:
3387 			entry->state = rx_cleanup;
3388 			rx_process(dev, skb);
3389 			continue;
3390 		case tx_done:
3391 			usb_free_urb(entry->urb);
3392 			dev_kfree_skb(skb);
3393 			continue;
3394 		case rx_cleanup:
3395 			usb_free_urb(entry->urb);
3396 			dev_kfree_skb(skb);
3397 			continue;
3398 		default:
3399 			netdev_dbg(dev->net, "skb state %d\n", entry->state);
3400 			return;
3401 		}
3402 	}
3403 
3404 	if (netif_device_present(dev->net) && netif_running(dev->net)) {
3405 		/* reset update timer delta */
3406 		if (timer_pending(&dev->stat_monitor) && (dev->delta != 1)) {
3407 			dev->delta = 1;
3408 			mod_timer(&dev->stat_monitor,
3409 				  jiffies + STAT_UPDATE_TIMER);
3410 		}
3411 
3412 		if (!skb_queue_empty(&dev->txq_pend))
3413 			lan78xx_tx_bh(dev);
3414 
3415 		if (!timer_pending(&dev->delay) &&
3416 		    !test_bit(EVENT_RX_HALT, &dev->flags))
3417 			lan78xx_rx_bh(dev);
3418 	}
3419 }
3420 
3421 static void lan78xx_delayedwork(struct work_struct *work)
3422 {
3423 	int status;
3424 	struct lan78xx_net *dev;
3425 
3426 	dev = container_of(work, struct lan78xx_net, wq.work);
3427 
3428 	if (test_bit(EVENT_TX_HALT, &dev->flags)) {
3429 		unlink_urbs(dev, &dev->txq);
3430 		status = usb_autopm_get_interface(dev->intf);
3431 		if (status < 0)
3432 			goto fail_pipe;
3433 		status = usb_clear_halt(dev->udev, dev->pipe_out);
3434 		usb_autopm_put_interface(dev->intf);
3435 		if (status < 0 &&
3436 		    status != -EPIPE &&
3437 		    status != -ESHUTDOWN) {
3438 			if (netif_msg_tx_err(dev))
3439 fail_pipe:
3440 				netdev_err(dev->net,
3441 					   "can't clear tx halt, status %d\n",
3442 					   status);
3443 		} else {
3444 			clear_bit(EVENT_TX_HALT, &dev->flags);
3445 			if (status != -ESHUTDOWN)
3446 				netif_wake_queue(dev->net);
3447 		}
3448 	}
3449 	if (test_bit(EVENT_RX_HALT, &dev->flags)) {
3450 		unlink_urbs(dev, &dev->rxq);
3451 		status = usb_autopm_get_interface(dev->intf);
3452 		if (status < 0)
3453 				goto fail_halt;
3454 		status = usb_clear_halt(dev->udev, dev->pipe_in);
3455 		usb_autopm_put_interface(dev->intf);
3456 		if (status < 0 &&
3457 		    status != -EPIPE &&
3458 		    status != -ESHUTDOWN) {
3459 			if (netif_msg_rx_err(dev))
3460 fail_halt:
3461 				netdev_err(dev->net,
3462 					   "can't clear rx halt, status %d\n",
3463 					   status);
3464 		} else {
3465 			clear_bit(EVENT_RX_HALT, &dev->flags);
3466 			tasklet_schedule(&dev->bh);
3467 		}
3468 	}
3469 
3470 	if (test_bit(EVENT_LINK_RESET, &dev->flags)) {
3471 		int ret = 0;
3472 
3473 		clear_bit(EVENT_LINK_RESET, &dev->flags);
3474 		status = usb_autopm_get_interface(dev->intf);
3475 		if (status < 0)
3476 			goto skip_reset;
3477 		if (lan78xx_link_reset(dev) < 0) {
3478 			usb_autopm_put_interface(dev->intf);
3479 skip_reset:
3480 			netdev_info(dev->net, "link reset failed (%d)\n",
3481 				    ret);
3482 		} else {
3483 			usb_autopm_put_interface(dev->intf);
3484 		}
3485 	}
3486 
3487 	if (test_bit(EVENT_STAT_UPDATE, &dev->flags)) {
3488 		lan78xx_update_stats(dev);
3489 
3490 		clear_bit(EVENT_STAT_UPDATE, &dev->flags);
3491 
3492 		mod_timer(&dev->stat_monitor,
3493 			  jiffies + (STAT_UPDATE_TIMER * dev->delta));
3494 
3495 		dev->delta = min((dev->delta * 2), 50);
3496 	}
3497 }
3498 
3499 static void intr_complete(struct urb *urb)
3500 {
3501 	struct lan78xx_net *dev = urb->context;
3502 	int status = urb->status;
3503 
3504 	switch (status) {
3505 	/* success */
3506 	case 0:
3507 		lan78xx_status(dev, urb);
3508 		break;
3509 
3510 	/* software-driven interface shutdown */
3511 	case -ENOENT:			/* urb killed */
3512 	case -ESHUTDOWN:		/* hardware gone */
3513 		netif_dbg(dev, ifdown, dev->net,
3514 			  "intr shutdown, code %d\n", status);
3515 		return;
3516 
3517 	/* NOTE:  not throttling like RX/TX, since this endpoint
3518 	 * already polls infrequently
3519 	 */
3520 	default:
3521 		netdev_dbg(dev->net, "intr status %d\n", status);
3522 		break;
3523 	}
3524 
3525 	if (!netif_running(dev->net))
3526 		return;
3527 
3528 	memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
3529 	status = usb_submit_urb(urb, GFP_ATOMIC);
3530 	if (status != 0)
3531 		netif_err(dev, timer, dev->net,
3532 			  "intr resubmit --> %d\n", status);
3533 }
3534 
3535 static void lan78xx_disconnect(struct usb_interface *intf)
3536 {
3537 	struct lan78xx_net *dev;
3538 	struct usb_device *udev;
3539 	struct net_device *net;
3540 	struct phy_device *phydev;
3541 
3542 	dev = usb_get_intfdata(intf);
3543 	usb_set_intfdata(intf, NULL);
3544 	if (!dev)
3545 		return;
3546 
3547 	udev = interface_to_usbdev(intf);
3548 	net = dev->net;
3549 	phydev = net->phydev;
3550 
3551 	phy_unregister_fixup_for_uid(PHY_KSZ9031RNX, 0xfffffff0);
3552 	phy_unregister_fixup_for_uid(PHY_LAN8835, 0xfffffff0);
3553 
3554 	phy_disconnect(net->phydev);
3555 
3556 	if (phy_is_pseudo_fixed_link(phydev))
3557 		fixed_phy_unregister(phydev);
3558 
3559 	unregister_netdev(net);
3560 
3561 	cancel_delayed_work_sync(&dev->wq);
3562 
3563 	usb_scuttle_anchored_urbs(&dev->deferred);
3564 
3565 	lan78xx_unbind(dev, intf);
3566 
3567 	usb_kill_urb(dev->urb_intr);
3568 	usb_free_urb(dev->urb_intr);
3569 
3570 	free_netdev(net);
3571 	usb_put_dev(udev);
3572 }
3573 
3574 static void lan78xx_tx_timeout(struct net_device *net, unsigned int txqueue)
3575 {
3576 	struct lan78xx_net *dev = netdev_priv(net);
3577 
3578 	unlink_urbs(dev, &dev->txq);
3579 	tasklet_schedule(&dev->bh);
3580 }
3581 
3582 static netdev_features_t lan78xx_features_check(struct sk_buff *skb,
3583 						struct net_device *netdev,
3584 						netdev_features_t features)
3585 {
3586 	if (skb->len + TX_OVERHEAD > MAX_SINGLE_PACKET_SIZE)
3587 		features &= ~NETIF_F_GSO_MASK;
3588 
3589 	features = vlan_features_check(skb, features);
3590 	features = vxlan_features_check(skb, features);
3591 
3592 	return features;
3593 }
3594 
3595 static const struct net_device_ops lan78xx_netdev_ops = {
3596 	.ndo_open		= lan78xx_open,
3597 	.ndo_stop		= lan78xx_stop,
3598 	.ndo_start_xmit		= lan78xx_start_xmit,
3599 	.ndo_tx_timeout		= lan78xx_tx_timeout,
3600 	.ndo_change_mtu		= lan78xx_change_mtu,
3601 	.ndo_set_mac_address	= lan78xx_set_mac_addr,
3602 	.ndo_validate_addr	= eth_validate_addr,
3603 	.ndo_do_ioctl		= phy_do_ioctl_running,
3604 	.ndo_set_rx_mode	= lan78xx_set_multicast,
3605 	.ndo_set_features	= lan78xx_set_features,
3606 	.ndo_vlan_rx_add_vid	= lan78xx_vlan_rx_add_vid,
3607 	.ndo_vlan_rx_kill_vid	= lan78xx_vlan_rx_kill_vid,
3608 	.ndo_features_check	= lan78xx_features_check,
3609 };
3610 
3611 static void lan78xx_stat_monitor(struct timer_list *t)
3612 {
3613 	struct lan78xx_net *dev = from_timer(dev, t, stat_monitor);
3614 
3615 	lan78xx_defer_kevent(dev, EVENT_STAT_UPDATE);
3616 }
3617 
3618 static int lan78xx_probe(struct usb_interface *intf,
3619 			 const struct usb_device_id *id)
3620 {
3621 	struct usb_host_endpoint *ep_blkin, *ep_blkout, *ep_intr;
3622 	struct lan78xx_net *dev;
3623 	struct net_device *netdev;
3624 	struct usb_device *udev;
3625 	int ret;
3626 	unsigned maxp;
3627 	unsigned period;
3628 	u8 *buf = NULL;
3629 
3630 	udev = interface_to_usbdev(intf);
3631 	udev = usb_get_dev(udev);
3632 
3633 	netdev = alloc_etherdev(sizeof(struct lan78xx_net));
3634 	if (!netdev) {
3635 		dev_err(&intf->dev, "Error: OOM\n");
3636 		ret = -ENOMEM;
3637 		goto out1;
3638 	}
3639 
3640 	/* netdev_printk() needs this */
3641 	SET_NETDEV_DEV(netdev, &intf->dev);
3642 
3643 	dev = netdev_priv(netdev);
3644 	dev->udev = udev;
3645 	dev->intf = intf;
3646 	dev->net = netdev;
3647 	dev->msg_enable = netif_msg_init(msg_level, NETIF_MSG_DRV
3648 					| NETIF_MSG_PROBE | NETIF_MSG_LINK);
3649 
3650 	skb_queue_head_init(&dev->rxq);
3651 	skb_queue_head_init(&dev->txq);
3652 	skb_queue_head_init(&dev->done);
3653 	skb_queue_head_init(&dev->rxq_pause);
3654 	skb_queue_head_init(&dev->txq_pend);
3655 	mutex_init(&dev->phy_mutex);
3656 
3657 	tasklet_setup(&dev->bh, lan78xx_bh);
3658 	INIT_DELAYED_WORK(&dev->wq, lan78xx_delayedwork);
3659 	init_usb_anchor(&dev->deferred);
3660 
3661 	netdev->netdev_ops = &lan78xx_netdev_ops;
3662 	netdev->watchdog_timeo = TX_TIMEOUT_JIFFIES;
3663 	netdev->ethtool_ops = &lan78xx_ethtool_ops;
3664 
3665 	dev->delta = 1;
3666 	timer_setup(&dev->stat_monitor, lan78xx_stat_monitor, 0);
3667 
3668 	mutex_init(&dev->stats.access_lock);
3669 
3670 	if (intf->cur_altsetting->desc.bNumEndpoints < 3) {
3671 		ret = -ENODEV;
3672 		goto out2;
3673 	}
3674 
3675 	dev->pipe_in = usb_rcvbulkpipe(udev, BULK_IN_PIPE);
3676 	ep_blkin = usb_pipe_endpoint(udev, dev->pipe_in);
3677 	if (!ep_blkin || !usb_endpoint_is_bulk_in(&ep_blkin->desc)) {
3678 		ret = -ENODEV;
3679 		goto out2;
3680 	}
3681 
3682 	dev->pipe_out = usb_sndbulkpipe(udev, BULK_OUT_PIPE);
3683 	ep_blkout = usb_pipe_endpoint(udev, dev->pipe_out);
3684 	if (!ep_blkout || !usb_endpoint_is_bulk_out(&ep_blkout->desc)) {
3685 		ret = -ENODEV;
3686 		goto out2;
3687 	}
3688 
3689 	ep_intr = &intf->cur_altsetting->endpoint[2];
3690 	if (!usb_endpoint_is_int_in(&ep_intr->desc)) {
3691 		ret = -ENODEV;
3692 		goto out2;
3693 	}
3694 
3695 	dev->pipe_intr = usb_rcvintpipe(dev->udev,
3696 					usb_endpoint_num(&ep_intr->desc));
3697 
3698 	ret = lan78xx_bind(dev, intf);
3699 	if (ret < 0)
3700 		goto out2;
3701 
3702 	if (netdev->mtu > (dev->hard_mtu - netdev->hard_header_len))
3703 		netdev->mtu = dev->hard_mtu - netdev->hard_header_len;
3704 
3705 	/* MTU range: 68 - 9000 */
3706 	netdev->max_mtu = MAX_SINGLE_PACKET_SIZE;
3707 	netif_set_gso_max_size(netdev, MAX_SINGLE_PACKET_SIZE - MAX_HEADER);
3708 
3709 	period = ep_intr->desc.bInterval;
3710 	maxp = usb_maxpacket(dev->udev, dev->pipe_intr, 0);
3711 	buf = kmalloc(maxp, GFP_KERNEL);
3712 	if (buf) {
3713 		dev->urb_intr = usb_alloc_urb(0, GFP_KERNEL);
3714 		if (!dev->urb_intr) {
3715 			ret = -ENOMEM;
3716 			kfree(buf);
3717 			goto out3;
3718 		} else {
3719 			usb_fill_int_urb(dev->urb_intr, dev->udev,
3720 					 dev->pipe_intr, buf, maxp,
3721 					 intr_complete, dev, period);
3722 			dev->urb_intr->transfer_flags |= URB_FREE_BUFFER;
3723 		}
3724 	}
3725 
3726 	dev->maxpacket = usb_maxpacket(dev->udev, dev->pipe_out, 1);
3727 
3728 	/* driver requires remote-wakeup capability during autosuspend. */
3729 	intf->needs_remote_wakeup = 1;
3730 
3731 	ret = lan78xx_phy_init(dev);
3732 	if (ret < 0)
3733 		goto out4;
3734 
3735 	ret = register_netdev(netdev);
3736 	if (ret != 0) {
3737 		netif_err(dev, probe, netdev, "couldn't register the device\n");
3738 		goto out5;
3739 	}
3740 
3741 	usb_set_intfdata(intf, dev);
3742 
3743 	ret = device_set_wakeup_enable(&udev->dev, true);
3744 
3745 	 /* Default delay of 2sec has more overhead than advantage.
3746 	  * Set to 10sec as default.
3747 	  */
3748 	pm_runtime_set_autosuspend_delay(&udev->dev,
3749 					 DEFAULT_AUTOSUSPEND_DELAY);
3750 
3751 	return 0;
3752 
3753 out5:
3754 	phy_disconnect(netdev->phydev);
3755 out4:
3756 	usb_free_urb(dev->urb_intr);
3757 out3:
3758 	lan78xx_unbind(dev, intf);
3759 out2:
3760 	free_netdev(netdev);
3761 out1:
3762 	usb_put_dev(udev);
3763 
3764 	return ret;
3765 }
3766 
3767 static u16 lan78xx_wakeframe_crc16(const u8 *buf, int len)
3768 {
3769 	const u16 crc16poly = 0x8005;
3770 	int i;
3771 	u16 bit, crc, msb;
3772 	u8 data;
3773 
3774 	crc = 0xFFFF;
3775 	for (i = 0; i < len; i++) {
3776 		data = *buf++;
3777 		for (bit = 0; bit < 8; bit++) {
3778 			msb = crc >> 15;
3779 			crc <<= 1;
3780 
3781 			if (msb ^ (u16)(data & 1)) {
3782 				crc ^= crc16poly;
3783 				crc |= (u16)0x0001U;
3784 			}
3785 			data >>= 1;
3786 		}
3787 	}
3788 
3789 	return crc;
3790 }
3791 
3792 static int lan78xx_set_suspend(struct lan78xx_net *dev, u32 wol)
3793 {
3794 	u32 buf;
3795 	int mask_index;
3796 	u16 crc;
3797 	u32 temp_wucsr;
3798 	u32 temp_pmt_ctl;
3799 	const u8 ipv4_multicast[3] = { 0x01, 0x00, 0x5E };
3800 	const u8 ipv6_multicast[3] = { 0x33, 0x33 };
3801 	const u8 arp_type[2] = { 0x08, 0x06 };
3802 
3803 	lan78xx_read_reg(dev, MAC_TX, &buf);
3804 	buf &= ~MAC_TX_TXEN_;
3805 	lan78xx_write_reg(dev, MAC_TX, buf);
3806 	lan78xx_read_reg(dev, MAC_RX, &buf);
3807 	buf &= ~MAC_RX_RXEN_;
3808 	lan78xx_write_reg(dev, MAC_RX, buf);
3809 
3810 	lan78xx_write_reg(dev, WUCSR, 0);
3811 	lan78xx_write_reg(dev, WUCSR2, 0);
3812 	lan78xx_write_reg(dev, WK_SRC, 0xFFF1FF1FUL);
3813 
3814 	temp_wucsr = 0;
3815 
3816 	temp_pmt_ctl = 0;
3817 	lan78xx_read_reg(dev, PMT_CTL, &temp_pmt_ctl);
3818 	temp_pmt_ctl &= ~PMT_CTL_RES_CLR_WKP_EN_;
3819 	temp_pmt_ctl |= PMT_CTL_RES_CLR_WKP_STS_;
3820 
3821 	for (mask_index = 0; mask_index < NUM_OF_WUF_CFG; mask_index++)
3822 		lan78xx_write_reg(dev, WUF_CFG(mask_index), 0);
3823 
3824 	mask_index = 0;
3825 	if (wol & WAKE_PHY) {
3826 		temp_pmt_ctl |= PMT_CTL_PHY_WAKE_EN_;
3827 
3828 		temp_pmt_ctl |= PMT_CTL_WOL_EN_;
3829 		temp_pmt_ctl &= ~PMT_CTL_SUS_MODE_MASK_;
3830 		temp_pmt_ctl |= PMT_CTL_SUS_MODE_0_;
3831 	}
3832 	if (wol & WAKE_MAGIC) {
3833 		temp_wucsr |= WUCSR_MPEN_;
3834 
3835 		temp_pmt_ctl |= PMT_CTL_WOL_EN_;
3836 		temp_pmt_ctl &= ~PMT_CTL_SUS_MODE_MASK_;
3837 		temp_pmt_ctl |= PMT_CTL_SUS_MODE_3_;
3838 	}
3839 	if (wol & WAKE_BCAST) {
3840 		temp_wucsr |= WUCSR_BCST_EN_;
3841 
3842 		temp_pmt_ctl |= PMT_CTL_WOL_EN_;
3843 		temp_pmt_ctl &= ~PMT_CTL_SUS_MODE_MASK_;
3844 		temp_pmt_ctl |= PMT_CTL_SUS_MODE_0_;
3845 	}
3846 	if (wol & WAKE_MCAST) {
3847 		temp_wucsr |= WUCSR_WAKE_EN_;
3848 
3849 		/* set WUF_CFG & WUF_MASK for IPv4 Multicast */
3850 		crc = lan78xx_wakeframe_crc16(ipv4_multicast, 3);
3851 		lan78xx_write_reg(dev, WUF_CFG(mask_index),
3852 					WUF_CFGX_EN_ |
3853 					WUF_CFGX_TYPE_MCAST_ |
3854 					(0 << WUF_CFGX_OFFSET_SHIFT_) |
3855 					(crc & WUF_CFGX_CRC16_MASK_));
3856 
3857 		lan78xx_write_reg(dev, WUF_MASK0(mask_index), 7);
3858 		lan78xx_write_reg(dev, WUF_MASK1(mask_index), 0);
3859 		lan78xx_write_reg(dev, WUF_MASK2(mask_index), 0);
3860 		lan78xx_write_reg(dev, WUF_MASK3(mask_index), 0);
3861 		mask_index++;
3862 
3863 		/* for IPv6 Multicast */
3864 		crc = lan78xx_wakeframe_crc16(ipv6_multicast, 2);
3865 		lan78xx_write_reg(dev, WUF_CFG(mask_index),
3866 					WUF_CFGX_EN_ |
3867 					WUF_CFGX_TYPE_MCAST_ |
3868 					(0 << WUF_CFGX_OFFSET_SHIFT_) |
3869 					(crc & WUF_CFGX_CRC16_MASK_));
3870 
3871 		lan78xx_write_reg(dev, WUF_MASK0(mask_index), 3);
3872 		lan78xx_write_reg(dev, WUF_MASK1(mask_index), 0);
3873 		lan78xx_write_reg(dev, WUF_MASK2(mask_index), 0);
3874 		lan78xx_write_reg(dev, WUF_MASK3(mask_index), 0);
3875 		mask_index++;
3876 
3877 		temp_pmt_ctl |= PMT_CTL_WOL_EN_;
3878 		temp_pmt_ctl &= ~PMT_CTL_SUS_MODE_MASK_;
3879 		temp_pmt_ctl |= PMT_CTL_SUS_MODE_0_;
3880 	}
3881 	if (wol & WAKE_UCAST) {
3882 		temp_wucsr |= WUCSR_PFDA_EN_;
3883 
3884 		temp_pmt_ctl |= PMT_CTL_WOL_EN_;
3885 		temp_pmt_ctl &= ~PMT_CTL_SUS_MODE_MASK_;
3886 		temp_pmt_ctl |= PMT_CTL_SUS_MODE_0_;
3887 	}
3888 	if (wol & WAKE_ARP) {
3889 		temp_wucsr |= WUCSR_WAKE_EN_;
3890 
3891 		/* set WUF_CFG & WUF_MASK
3892 		 * for packettype (offset 12,13) = ARP (0x0806)
3893 		 */
3894 		crc = lan78xx_wakeframe_crc16(arp_type, 2);
3895 		lan78xx_write_reg(dev, WUF_CFG(mask_index),
3896 					WUF_CFGX_EN_ |
3897 					WUF_CFGX_TYPE_ALL_ |
3898 					(0 << WUF_CFGX_OFFSET_SHIFT_) |
3899 					(crc & WUF_CFGX_CRC16_MASK_));
3900 
3901 		lan78xx_write_reg(dev, WUF_MASK0(mask_index), 0x3000);
3902 		lan78xx_write_reg(dev, WUF_MASK1(mask_index), 0);
3903 		lan78xx_write_reg(dev, WUF_MASK2(mask_index), 0);
3904 		lan78xx_write_reg(dev, WUF_MASK3(mask_index), 0);
3905 		mask_index++;
3906 
3907 		temp_pmt_ctl |= PMT_CTL_WOL_EN_;
3908 		temp_pmt_ctl &= ~PMT_CTL_SUS_MODE_MASK_;
3909 		temp_pmt_ctl |= PMT_CTL_SUS_MODE_0_;
3910 	}
3911 
3912 	lan78xx_write_reg(dev, WUCSR, temp_wucsr);
3913 
3914 	/* when multiple WOL bits are set */
3915 	if (hweight_long((unsigned long)wol) > 1) {
3916 		temp_pmt_ctl |= PMT_CTL_WOL_EN_;
3917 		temp_pmt_ctl &= ~PMT_CTL_SUS_MODE_MASK_;
3918 		temp_pmt_ctl |= PMT_CTL_SUS_MODE_0_;
3919 	}
3920 	lan78xx_write_reg(dev, PMT_CTL, temp_pmt_ctl);
3921 
3922 	/* clear WUPS */
3923 	lan78xx_read_reg(dev, PMT_CTL, &buf);
3924 	buf |= PMT_CTL_WUPS_MASK_;
3925 	lan78xx_write_reg(dev, PMT_CTL, buf);
3926 
3927 	lan78xx_read_reg(dev, MAC_RX, &buf);
3928 	buf |= MAC_RX_RXEN_;
3929 	lan78xx_write_reg(dev, MAC_RX, buf);
3930 
3931 	return 0;
3932 }
3933 
3934 static int lan78xx_suspend(struct usb_interface *intf, pm_message_t message)
3935 {
3936 	struct lan78xx_net *dev = usb_get_intfdata(intf);
3937 	struct lan78xx_priv *pdata = (struct lan78xx_priv *)(dev->data[0]);
3938 	u32 buf;
3939 	int ret;
3940 
3941 	if (!dev->suspend_count++) {
3942 		spin_lock_irq(&dev->txq.lock);
3943 		/* don't autosuspend while transmitting */
3944 		if ((skb_queue_len(&dev->txq) ||
3945 		     skb_queue_len(&dev->txq_pend)) &&
3946 			PMSG_IS_AUTO(message)) {
3947 			spin_unlock_irq(&dev->txq.lock);
3948 			ret = -EBUSY;
3949 			goto out;
3950 		} else {
3951 			set_bit(EVENT_DEV_ASLEEP, &dev->flags);
3952 			spin_unlock_irq(&dev->txq.lock);
3953 		}
3954 
3955 		/* stop TX & RX */
3956 		ret = lan78xx_read_reg(dev, MAC_TX, &buf);
3957 		buf &= ~MAC_TX_TXEN_;
3958 		ret = lan78xx_write_reg(dev, MAC_TX, buf);
3959 		ret = lan78xx_read_reg(dev, MAC_RX, &buf);
3960 		buf &= ~MAC_RX_RXEN_;
3961 		ret = lan78xx_write_reg(dev, MAC_RX, buf);
3962 
3963 		/* empty out the rx and queues */
3964 		netif_device_detach(dev->net);
3965 		lan78xx_terminate_urbs(dev);
3966 		usb_kill_urb(dev->urb_intr);
3967 
3968 		/* reattach */
3969 		netif_device_attach(dev->net);
3970 	}
3971 
3972 	if (test_bit(EVENT_DEV_ASLEEP, &dev->flags)) {
3973 		del_timer(&dev->stat_monitor);
3974 
3975 		if (PMSG_IS_AUTO(message)) {
3976 			/* auto suspend (selective suspend) */
3977 			ret = lan78xx_read_reg(dev, MAC_TX, &buf);
3978 			buf &= ~MAC_TX_TXEN_;
3979 			ret = lan78xx_write_reg(dev, MAC_TX, buf);
3980 			ret = lan78xx_read_reg(dev, MAC_RX, &buf);
3981 			buf &= ~MAC_RX_RXEN_;
3982 			ret = lan78xx_write_reg(dev, MAC_RX, buf);
3983 
3984 			ret = lan78xx_write_reg(dev, WUCSR, 0);
3985 			ret = lan78xx_write_reg(dev, WUCSR2, 0);
3986 			ret = lan78xx_write_reg(dev, WK_SRC, 0xFFF1FF1FUL);
3987 
3988 			/* set goodframe wakeup */
3989 			ret = lan78xx_read_reg(dev, WUCSR, &buf);
3990 
3991 			buf |= WUCSR_RFE_WAKE_EN_;
3992 			buf |= WUCSR_STORE_WAKE_;
3993 
3994 			ret = lan78xx_write_reg(dev, WUCSR, buf);
3995 
3996 			ret = lan78xx_read_reg(dev, PMT_CTL, &buf);
3997 
3998 			buf &= ~PMT_CTL_RES_CLR_WKP_EN_;
3999 			buf |= PMT_CTL_RES_CLR_WKP_STS_;
4000 
4001 			buf |= PMT_CTL_PHY_WAKE_EN_;
4002 			buf |= PMT_CTL_WOL_EN_;
4003 			buf &= ~PMT_CTL_SUS_MODE_MASK_;
4004 			buf |= PMT_CTL_SUS_MODE_3_;
4005 
4006 			ret = lan78xx_write_reg(dev, PMT_CTL, buf);
4007 
4008 			ret = lan78xx_read_reg(dev, PMT_CTL, &buf);
4009 
4010 			buf |= PMT_CTL_WUPS_MASK_;
4011 
4012 			ret = lan78xx_write_reg(dev, PMT_CTL, buf);
4013 
4014 			ret = lan78xx_read_reg(dev, MAC_RX, &buf);
4015 			buf |= MAC_RX_RXEN_;
4016 			ret = lan78xx_write_reg(dev, MAC_RX, buf);
4017 		} else {
4018 			lan78xx_set_suspend(dev, pdata->wol);
4019 		}
4020 	}
4021 
4022 	ret = 0;
4023 out:
4024 	return ret;
4025 }
4026 
4027 static int lan78xx_resume(struct usb_interface *intf)
4028 {
4029 	struct lan78xx_net *dev = usb_get_intfdata(intf);
4030 	struct sk_buff *skb;
4031 	struct urb *res;
4032 	int ret;
4033 	u32 buf;
4034 
4035 	if (!timer_pending(&dev->stat_monitor)) {
4036 		dev->delta = 1;
4037 		mod_timer(&dev->stat_monitor,
4038 			  jiffies + STAT_UPDATE_TIMER);
4039 	}
4040 
4041 	if (!--dev->suspend_count) {
4042 		/* resume interrupt URBs */
4043 		if (dev->urb_intr && test_bit(EVENT_DEV_OPEN, &dev->flags))
4044 				usb_submit_urb(dev->urb_intr, GFP_NOIO);
4045 
4046 		spin_lock_irq(&dev->txq.lock);
4047 		while ((res = usb_get_from_anchor(&dev->deferred))) {
4048 			skb = (struct sk_buff *)res->context;
4049 			ret = usb_submit_urb(res, GFP_ATOMIC);
4050 			if (ret < 0) {
4051 				dev_kfree_skb_any(skb);
4052 				usb_free_urb(res);
4053 				usb_autopm_put_interface_async(dev->intf);
4054 			} else {
4055 				netif_trans_update(dev->net);
4056 				lan78xx_queue_skb(&dev->txq, skb, tx_start);
4057 			}
4058 		}
4059 
4060 		clear_bit(EVENT_DEV_ASLEEP, &dev->flags);
4061 		spin_unlock_irq(&dev->txq.lock);
4062 
4063 		if (test_bit(EVENT_DEV_OPEN, &dev->flags)) {
4064 			if (!(skb_queue_len(&dev->txq) >= dev->tx_qlen))
4065 				netif_start_queue(dev->net);
4066 			tasklet_schedule(&dev->bh);
4067 		}
4068 	}
4069 
4070 	ret = lan78xx_write_reg(dev, WUCSR2, 0);
4071 	ret = lan78xx_write_reg(dev, WUCSR, 0);
4072 	ret = lan78xx_write_reg(dev, WK_SRC, 0xFFF1FF1FUL);
4073 
4074 	ret = lan78xx_write_reg(dev, WUCSR2, WUCSR2_NS_RCD_ |
4075 					     WUCSR2_ARP_RCD_ |
4076 					     WUCSR2_IPV6_TCPSYN_RCD_ |
4077 					     WUCSR2_IPV4_TCPSYN_RCD_);
4078 
4079 	ret = lan78xx_write_reg(dev, WUCSR, WUCSR_EEE_TX_WAKE_ |
4080 					    WUCSR_EEE_RX_WAKE_ |
4081 					    WUCSR_PFDA_FR_ |
4082 					    WUCSR_RFE_WAKE_FR_ |
4083 					    WUCSR_WUFR_ |
4084 					    WUCSR_MPR_ |
4085 					    WUCSR_BCST_FR_);
4086 
4087 	ret = lan78xx_read_reg(dev, MAC_TX, &buf);
4088 	buf |= MAC_TX_TXEN_;
4089 	ret = lan78xx_write_reg(dev, MAC_TX, buf);
4090 
4091 	return 0;
4092 }
4093 
4094 static int lan78xx_reset_resume(struct usb_interface *intf)
4095 {
4096 	struct lan78xx_net *dev = usb_get_intfdata(intf);
4097 
4098 	lan78xx_reset(dev);
4099 
4100 	phy_start(dev->net->phydev);
4101 
4102 	return lan78xx_resume(intf);
4103 }
4104 
4105 static const struct usb_device_id products[] = {
4106 	{
4107 	/* LAN7800 USB Gigabit Ethernet Device */
4108 	USB_DEVICE(LAN78XX_USB_VENDOR_ID, LAN7800_USB_PRODUCT_ID),
4109 	},
4110 	{
4111 	/* LAN7850 USB Gigabit Ethernet Device */
4112 	USB_DEVICE(LAN78XX_USB_VENDOR_ID, LAN7850_USB_PRODUCT_ID),
4113 	},
4114 	{
4115 	/* LAN7801 USB Gigabit Ethernet Device */
4116 	USB_DEVICE(LAN78XX_USB_VENDOR_ID, LAN7801_USB_PRODUCT_ID),
4117 	},
4118 	{},
4119 };
4120 MODULE_DEVICE_TABLE(usb, products);
4121 
4122 static struct usb_driver lan78xx_driver = {
4123 	.name			= DRIVER_NAME,
4124 	.id_table		= products,
4125 	.probe			= lan78xx_probe,
4126 	.disconnect		= lan78xx_disconnect,
4127 	.suspend		= lan78xx_suspend,
4128 	.resume			= lan78xx_resume,
4129 	.reset_resume		= lan78xx_reset_resume,
4130 	.supports_autosuspend	= 1,
4131 	.disable_hub_initiated_lpm = 1,
4132 };
4133 
4134 module_usb_driver(lan78xx_driver);
4135 
4136 MODULE_AUTHOR(DRIVER_AUTHOR);
4137 MODULE_DESCRIPTION(DRIVER_DESC);
4138 MODULE_LICENSE("GPL");
4139