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