1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Microchip Technology
5 #include <linux/module.h>
6 #include <linux/netdevice.h>
7 #include <linux/etherdevice.h>
8 #include <linux/ethtool.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>
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>
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"
37 #define TX_TIMEOUT_JIFFIES (5 * HZ)
38 #define THROTTLE_JIFFIES (HZ / 8)
39 #define UNLINK_TIMEOUT_MS 3
41 #define RX_MAX_QUEUE_MEMORY (60 * 1518)
43 #define SS_USB_PKT_SIZE (1024)
44 #define HS_USB_PKT_SIZE (512)
45 #define FS_USB_PKT_SIZE (64)
47 #define MAX_RX_FIFO_SIZE (12 * 1024)
48 #define MAX_TX_FIFO_SIZE (12 * 1024)
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))
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
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
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)
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)
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)
91 #define WAKE_ALL (WAKE_PHY | WAKE_UCAST | \
92 WAKE_MCAST | WAKE_BCAST | \
93 WAKE_ARP | WAKE_MAGIC)
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
100 /* A single URB buffer must be large enough to hold a complete jumbo packet
102 #define TX_SS_URB_SIZE (32 * 1024)
103 #define TX_HS_URB_SIZE (16 * 1024)
104 #define TX_FS_URB_SIZE (10 * 1024)
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
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
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)
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)
128 /* USB related defines */
129 #define BULK_IN_PIPE 1
130 #define BULK_OUT_PIPE 2
132 /* default autosuspend delay (mSec)*/
133 #define DEFAULT_AUTOSUSPEND_DELAY (10 * 1000)
135 /* statistic update interval (mSec) */
136 #define STAT_UPDATE_TIMER (1 * 1000)
138 /* time to wait for MAC or FCT to stop (jiffies) */
139 #define HW_DISABLE_TIMEOUT (HZ / 10)
141 /* time to wait between polling MAC or FCT state (ms) */
142 #define HW_DISABLE_DELAY_MS 1
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)
176 static const char lan78xx_gstrings[][ETH_GSTRING_LEN] = {
178 "RX Alignment Errors",
179 "Rx Fragment Errors",
181 "RX Undersize Frame Errors",
182 "RX Oversize Frame Errors",
184 "RX Unicast Byte Count",
185 "RX Broadcast Byte Count",
186 "RX Multicast Byte Count",
188 "RX Broadcast Frames",
189 "RX Multicast 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",
201 "TX Excess Deferral Errors",
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",
212 "TX Broadcast Frames",
213 "TX Multicast 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",
226 struct lan78xx_statstage {
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;
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;
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;
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;
276 struct lan78xx_statstage64 {
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;
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;
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;
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;
326 static u32 lan78xx_regs[] = {
348 #define PHY_REG_SIZE (32 * sizeof(u32))
352 struct lan78xx_priv {
353 struct lan78xx_net *dev;
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;
375 struct skb_data { /* skb->cb is one of these */
377 struct lan78xx_net *dev;
378 enum skb_state state;
384 struct usb_ctrlrequest req;
385 struct lan78xx_net *dev;
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
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;
408 struct irq_domain_data {
409 struct irq_domain *irqdomain;
411 struct irq_chip *irqchip;
412 irq_flow_handler_t irq_handler;
414 struct mutex irq_lock; /* for irq bus access */
418 struct net_device *net;
419 struct usb_device *udev;
420 struct usb_interface *intf;
423 unsigned int tx_pend_data_len;
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;
437 struct napi_struct napi;
439 struct delayed_work wq;
443 struct urb *urb_intr;
444 struct usb_anchor deferred;
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;
450 unsigned int bulk_in_delay;
451 unsigned int burst_cap;
455 wait_queue_head_t *wait;
456 unsigned char suspend_count;
458 unsigned int maxpacket;
459 struct timer_list stat_monitor;
461 unsigned long data[5];
468 struct mii_bus *mdiobus;
469 phy_interface_t interface;
472 u8 fc_request_control;
475 struct statstage stats;
477 struct irq_domain_data domain_data;
480 /* define external phy id */
481 #define PHY_LAN8835 (0x0007C130)
482 #define PHY_KSZ9031RNX (0x00221620)
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");
489 static struct sk_buff *lan78xx_get_buf(struct sk_buff_head *buf_pool)
491 if (skb_queue_empty(buf_pool))
494 return skb_dequeue(buf_pool);
497 static void lan78xx_release_buf(struct sk_buff_head *buf_pool,
500 buf->data = buf->head;
501 skb_reset_tail_pointer(buf);
506 skb_queue_tail(buf_pool, buf);
509 static void lan78xx_free_buf_pool(struct sk_buff_head *buf_pool)
511 struct skb_data *entry;
514 while (!skb_queue_empty(buf_pool)) {
515 buf = skb_dequeue(buf_pool);
517 entry = (struct skb_data *)buf->cb;
518 usb_free_urb(entry->urb);
519 dev_kfree_skb_any(buf);
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)
528 struct skb_data *entry;
533 skb_queue_head_init(buf_pool);
535 for (i = 0; i < n_urbs; i++) {
536 buf = alloc_skb(urb_size, GFP_ATOMIC);
540 if (skb_linearize(buf) != 0) {
541 dev_kfree_skb_any(buf);
545 urb = usb_alloc_urb(0, GFP_ATOMIC);
547 dev_kfree_skb_any(buf);
551 entry = (struct skb_data *)buf->cb;
555 entry->num_of_packet = 0;
557 skb_queue_tail(buf_pool, buf);
563 lan78xx_free_buf_pool(buf_pool);
568 static struct sk_buff *lan78xx_get_rx_buf(struct lan78xx_net *dev)
570 return lan78xx_get_buf(&dev->rxq_free);
573 static void lan78xx_release_rx_buf(struct lan78xx_net *dev,
574 struct sk_buff *rx_buf)
576 lan78xx_release_buf(&dev->rxq_free, rx_buf);
579 static void lan78xx_free_rx_resources(struct lan78xx_net *dev)
581 lan78xx_free_buf_pool(&dev->rxq_free);
584 static int lan78xx_alloc_rx_resources(struct lan78xx_net *dev)
586 return lan78xx_alloc_buf_pool(&dev->rxq_free,
587 dev->n_rx_urbs, dev->rx_urb_size, dev);
590 static struct sk_buff *lan78xx_get_tx_buf(struct lan78xx_net *dev)
592 return lan78xx_get_buf(&dev->txq_free);
595 static void lan78xx_release_tx_buf(struct lan78xx_net *dev,
596 struct sk_buff *tx_buf)
598 lan78xx_release_buf(&dev->txq_free, tx_buf);
601 static void lan78xx_free_tx_resources(struct lan78xx_net *dev)
603 lan78xx_free_buf_pool(&dev->txq_free);
606 static int lan78xx_alloc_tx_resources(struct lan78xx_net *dev)
608 return lan78xx_alloc_buf_pool(&dev->txq_free,
609 dev->n_tx_urbs, dev->tx_urb_size, dev);
612 static int lan78xx_read_reg(struct lan78xx_net *dev, u32 index, u32 *data)
617 if (test_bit(EVENT_DEV_DISCONNECT, &dev->flags))
620 buf = kmalloc(sizeof(u32), GFP_KERNEL);
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)) {
631 } else if (net_ratelimit()) {
632 netdev_warn(dev->net,
633 "Failed to read register index 0x%08x. ret = %d",
642 static int lan78xx_write_reg(struct lan78xx_net *dev, u32 index, u32 data)
647 if (test_bit(EVENT_DEV_DISCONNECT, &dev->flags))
650 buf = kmalloc(sizeof(u32), GFP_KERNEL);
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) &&
663 netdev_warn(dev->net,
664 "Failed to write register index 0x%08x. ret = %d",
673 static int lan78xx_update_reg(struct lan78xx_net *dev, u32 reg, u32 mask,
679 ret = lan78xx_read_reg(dev, reg, &buf);
684 buf |= (mask & data);
686 ret = lan78xx_write_reg(dev, reg, buf);
693 static int lan78xx_read_stats(struct lan78xx_net *dev,
694 struct lan78xx_statstage *data)
698 struct lan78xx_statstage *stats;
702 stats = kmalloc(sizeof(*stats), GFP_KERNEL);
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,
714 USB_CTRL_SET_TIMEOUT);
715 if (likely(ret >= 0)) {
718 for (i = 0; i < sizeof(*stats) / sizeof(u32); i++) {
719 le32_to_cpus(&src[i]);
723 netdev_warn(dev->net,
724 "Failed to read stat ret = %d", ret);
732 #define check_counter_rollover(struct1, dev_stats, member) \
734 if ((struct1)->member < (dev_stats).saved.member) \
735 (dev_stats).rollover_count.member++; \
738 static void lan78xx_check_stat_rollover(struct lan78xx_net *dev,
739 struct lan78xx_statstage *stats)
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);
789 memcpy(&dev->stats.saved, stats, sizeof(struct lan78xx_statstage));
792 static void lan78xx_update_stats(struct lan78xx_net *dev)
794 u32 *p, *count, *max;
797 struct lan78xx_statstage lan78xx_stats;
799 if (usb_autopm_get_interface(dev->intf) < 0)
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;
807 mutex_lock(&dev->stats.access_lock);
809 if (lan78xx_read_stats(dev, &lan78xx_stats) > 0)
810 lan78xx_check_stat_rollover(dev, &lan78xx_stats);
812 for (i = 0; i < (sizeof(lan78xx_stats) / (sizeof(u32))); i++)
813 data[i] = (u64)p[i] + ((u64)count[i] * ((u64)max[i] + 1));
815 mutex_unlock(&dev->stats.access_lock);
817 usb_autopm_put_interface(dev->intf);
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)
823 unsigned long start_time = jiffies;
828 ret = lan78xx_read_reg(dev, MII_ACC, &val);
829 if (unlikely(ret < 0))
832 if (!(val & MII_ACC_MII_BUSY_))
834 } while (!time_after(jiffies, start_time + HZ));
839 static inline u32 mii_access(int id, int index, int read)
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_;
846 ret |= MII_ACC_MII_READ_;
848 ret |= MII_ACC_MII_WRITE_;
849 ret |= MII_ACC_MII_BUSY_;
854 static int lan78xx_wait_eeprom(struct lan78xx_net *dev)
856 unsigned long start_time = jiffies;
861 ret = lan78xx_read_reg(dev, E2P_CMD, &val);
862 if (unlikely(ret < 0))
865 if (!(val & E2P_CMD_EPC_BUSY_) ||
866 (val & E2P_CMD_EPC_TIMEOUT_))
868 usleep_range(40, 100);
869 } while (!time_after(jiffies, start_time + HZ));
871 if (val & (E2P_CMD_EPC_TIMEOUT_ | E2P_CMD_EPC_BUSY_)) {
872 netdev_warn(dev->net, "EEPROM read operation timeout");
879 static int lan78xx_eeprom_confirm_not_busy(struct lan78xx_net *dev)
881 unsigned long start_time = jiffies;
886 ret = lan78xx_read_reg(dev, E2P_CMD, &val);
887 if (unlikely(ret < 0))
890 if (!(val & E2P_CMD_EPC_BUSY_))
893 usleep_range(40, 100);
894 } while (!time_after(jiffies, start_time + HZ));
896 netdev_warn(dev->net, "EEPROM is busy");
900 static int lan78xx_read_raw_eeprom(struct lan78xx_net *dev, u32 offset,
901 u32 length, u8 *data)
908 /* depends on chip, some EEPROM pins are muxed with LED function.
909 * disable & restore LED function to access EEPROM.
911 ret = lan78xx_read_reg(dev, HW_CFG, &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);
918 retval = lan78xx_eeprom_confirm_not_busy(dev);
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)) {
931 retval = lan78xx_wait_eeprom(dev);
935 ret = lan78xx_read_reg(dev, E2P_DATA, &val);
936 if (unlikely(ret < 0)) {
941 data[i] = val & 0xFF;
947 if (dev->chipid == ID_REV_CHIP_ID_7800_)
948 ret = lan78xx_write_reg(dev, HW_CFG, saved);
953 static int lan78xx_read_eeprom(struct lan78xx_net *dev, u32 offset,
954 u32 length, u8 *data)
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);
968 static int lan78xx_write_raw_eeprom(struct lan78xx_net *dev, u32 offset,
969 u32 length, u8 *data)
976 /* depends on chip, some EEPROM pins are muxed with LED function.
977 * disable & restore LED function to access EEPROM.
979 ret = lan78xx_read_reg(dev, HW_CFG, &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);
986 retval = lan78xx_eeprom_confirm_not_busy(dev);
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)) {
998 retval = lan78xx_wait_eeprom(dev);
1002 for (i = 0; i < length; i++) {
1003 /* Fill data register */
1005 ret = lan78xx_write_reg(dev, E2P_DATA, val);
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);
1020 retval = lan78xx_wait_eeprom(dev);
1029 if (dev->chipid == ID_REV_CHIP_ID_7800_)
1030 ret = lan78xx_write_reg(dev, HW_CFG, saved);
1035 static int lan78xx_read_raw_otp(struct lan78xx_net *dev, u32 offset,
1036 u32 length, u8 *data)
1040 unsigned long timeout;
1042 lan78xx_read_reg(dev, OTP_PWR_DN, &buf);
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);
1048 timeout = jiffies + HZ;
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");
1057 } while (buf & OTP_PWR_DN_PWRDN_N_);
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));
1066 lan78xx_write_reg(dev, OTP_FUNC_CMD, OTP_FUNC_CMD_READ_);
1067 lan78xx_write_reg(dev, OTP_CMD_GO, OTP_CMD_GO_GO_);
1069 timeout = jiffies + HZ;
1072 lan78xx_read_reg(dev, OTP_STATUS, &buf);
1073 if (time_after(jiffies, timeout)) {
1074 netdev_warn(dev->net,
1075 "timeout on OTP_STATUS");
1078 } while (buf & OTP_STATUS_BUSY_);
1080 lan78xx_read_reg(dev, OTP_RD_DATA, &buf);
1082 data[i] = (u8)(buf & 0xFF);
1088 static int lan78xx_write_raw_otp(struct lan78xx_net *dev, u32 offset,
1089 u32 length, u8 *data)
1093 unsigned long timeout;
1095 lan78xx_read_reg(dev, OTP_PWR_DN, &buf);
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);
1101 timeout = jiffies + HZ;
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");
1110 } while (buf & OTP_PWR_DN_PWRDN_N_);
1113 /* set to BYTE program mode */
1114 lan78xx_write_reg(dev, OTP_PRGM_MODE, OTP_PRGM_MODE_BYTE_);
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_);
1125 timeout = jiffies + HZ;
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");
1134 } while (buf & OTP_STATUS_BUSY_);
1140 static int lan78xx_read_otp(struct lan78xx_net *dev, u32 offset,
1141 u32 length, u8 *data)
1146 ret = lan78xx_read_raw_otp(dev, 0, 1, &sig);
1149 if (sig == OTP_INDICATOR_2)
1151 else if (sig != OTP_INDICATOR_1)
1154 ret = lan78xx_read_raw_otp(dev, offset, length, data);
1160 static int lan78xx_dataport_wait_not_busy(struct lan78xx_net *dev)
1164 for (i = 0; i < 100; i++) {
1167 ret = lan78xx_read_reg(dev, DP_SEL, &dp_sel);
1168 if (unlikely(ret < 0))
1171 if (dp_sel & DP_SEL_DPRDY_)
1174 usleep_range(40, 100);
1177 netdev_warn(dev->net, "%s timed out", __func__);
1182 static int lan78xx_dataport_write(struct lan78xx_net *dev, u32 ram_select,
1183 u32 addr, u32 length, u32 *buf)
1185 struct lan78xx_priv *pdata = (struct lan78xx_priv *)(dev->data[0]);
1189 if (usb_autopm_get_interface(dev->intf) < 0)
1192 mutex_lock(&pdata->dataport_mutex);
1194 ret = lan78xx_dataport_wait_not_busy(dev);
1198 ret = lan78xx_read_reg(dev, DP_SEL, &dp_sel);
1200 dp_sel &= ~DP_SEL_RSEL_MASK_;
1201 dp_sel |= ram_select;
1202 ret = lan78xx_write_reg(dev, DP_SEL, dp_sel);
1204 for (i = 0; i < length; i++) {
1205 ret = lan78xx_write_reg(dev, DP_ADDR, addr + i);
1207 ret = lan78xx_write_reg(dev, DP_DATA, buf[i]);
1209 ret = lan78xx_write_reg(dev, DP_CMD, DP_CMD_WRITE_);
1211 ret = lan78xx_dataport_wait_not_busy(dev);
1217 mutex_unlock(&pdata->dataport_mutex);
1218 usb_autopm_put_interface(dev->intf);
1223 static void lan78xx_set_addr_filter(struct lan78xx_priv *pdata,
1224 int index, u8 addr[ETH_ALEN])
1228 if ((pdata) && (index > 0) && (index < NUM_OF_MAF)) {
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;
1235 temp = addr[4] | (temp << 8);
1236 temp |= MAF_HI_VALID_ | MAF_HI_TYPE_DST_;
1237 pdata->pfilter_table[index][0] = temp;
1241 /* returns hash bit number for given MAC address */
1242 static inline u32 lan78xx_hash(char addr[ETH_ALEN])
1244 return (ether_crc(ETH_ALEN, addr) >> 23) & 0x1ff;
1247 static void lan78xx_deferred_multicast_write(struct work_struct *param)
1249 struct lan78xx_priv *pdata =
1250 container_of(param, struct lan78xx_priv, set_multicast);
1251 struct lan78xx_net *dev = pdata->dev;
1254 netif_dbg(dev, drv, dev->net, "deferred multicast write 0x%08x\n",
1257 lan78xx_dataport_write(dev, DP_SEL_RSEL_VLAN_DA_, DP_SEL_VHF_VLAN_LEN,
1258 DP_SEL_VHF_HASH_LEN, pdata->mchash_table);
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]);
1268 lan78xx_write_reg(dev, RFE_CTL, pdata->rfe_ctl);
1271 static void lan78xx_set_multicast(struct net_device *netdev)
1273 struct lan78xx_net *dev = netdev_priv(netdev);
1274 struct lan78xx_priv *pdata = (struct lan78xx_priv *)(dev->data[0]);
1275 unsigned long flags;
1278 spin_lock_irqsave(&pdata->rfe_ctl_lock, flags);
1280 pdata->rfe_ctl &= ~(RFE_CTL_UCAST_EN_ | RFE_CTL_MCAST_EN_ |
1281 RFE_CTL_DA_PERFECT_ | RFE_CTL_MCAST_HASH_);
1283 for (i = 0; i < DP_SEL_VHF_HASH_LEN; i++)
1284 pdata->mchash_table[i] = 0;
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;
1292 pdata->rfe_ctl |= RFE_CTL_BCAST_EN_;
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_;
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_;
1305 if (netdev_mc_count(dev->net)) {
1306 struct netdev_hw_addr *ha;
1309 netif_dbg(dev, drv, dev->net, "receive multicast hash filter");
1311 pdata->rfe_ctl |= RFE_CTL_DA_PERFECT_;
1314 netdev_for_each_mc_addr(ha, netdev) {
1315 /* set first 32 into Perfect Filter */
1317 lan78xx_set_addr_filter(pdata, i, ha->addr);
1319 u32 bitnum = lan78xx_hash(ha->addr);
1321 pdata->mchash_table[bitnum / 32] |=
1322 (1 << (bitnum % 32));
1323 pdata->rfe_ctl |= RFE_CTL_MCAST_HASH_;
1329 spin_unlock_irqrestore(&pdata->rfe_ctl_lock, flags);
1331 /* defer register writes to a sleepable context */
1332 schedule_work(&pdata->set_multicast);
1335 static int lan78xx_update_flowcontrol(struct lan78xx_net *dev, u8 duplex,
1336 u16 lcladv, u16 rmtadv)
1338 u32 flow = 0, fct_flow = 0;
1341 if (dev->fc_autoneg)
1342 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
1344 cap = dev->fc_request_control;
1346 if (cap & FLOW_CTRL_TX)
1347 flow |= (FLOW_CR_TX_FCEN_ | 0xFFFF);
1349 if (cap & FLOW_CTRL_RX)
1350 flow |= FLOW_CR_RX_FCEN_;
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);
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"));
1361 lan78xx_write_reg(dev, FCT_FLOW, fct_flow);
1363 /* threshold value should be set before enabling flow */
1364 lan78xx_write_reg(dev, FLOW, flow);
1369 static void lan78xx_rx_urb_submit_all(struct lan78xx_net *dev);
1371 static int lan78xx_mac_reset(struct lan78xx_net *dev)
1373 unsigned long start_time = jiffies;
1377 mutex_lock(&dev->phy_mutex);
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.
1383 ret = lan78xx_phy_wait_not_busy(dev);
1387 ret = lan78xx_read_reg(dev, MAC_CR, &val);
1392 ret = lan78xx_write_reg(dev, MAC_CR, val);
1396 /* Wait for the reset to complete before allowing any further
1397 * MAC register accesses otherwise the MAC may lock up.
1400 ret = lan78xx_read_reg(dev, MAC_CR, &val);
1404 if (!(val & MAC_CR_RST_)) {
1408 } while (!time_after(jiffies, start_time + HZ));
1412 mutex_unlock(&dev->phy_mutex);
1417 static int lan78xx_link_reset(struct lan78xx_net *dev)
1419 struct phy_device *phydev = dev->net->phydev;
1420 struct ethtool_link_ksettings ecmd;
1421 int ladv, radv, ret, link;
1424 /* clear LAN78xx interrupt status */
1425 ret = lan78xx_write_reg(dev, INT_STS, INT_STS_PHY_INT_);
1426 if (unlikely(ret < 0))
1429 mutex_lock(&phydev->lock);
1430 phy_read_status(phydev);
1431 link = phydev->link;
1432 mutex_unlock(&phydev->lock);
1434 if (!link && dev->link_on) {
1435 dev->link_on = false;
1438 ret = lan78xx_mac_reset(dev);
1442 del_timer(&dev->stat_monitor);
1443 } else if (link && !dev->link_on) {
1444 dev->link_on = true;
1446 phy_ethtool_ksettings_get(phydev, &ecmd);
1448 if (dev->udev->speed == USB_SPEED_SUPER) {
1449 if (ecmd.base.speed == 1000) {
1451 ret = lan78xx_read_reg(dev, USB_CFG1, &buf);
1454 buf &= ~USB_CFG1_DEV_U2_INIT_EN_;
1455 ret = lan78xx_write_reg(dev, USB_CFG1, buf);
1459 ret = lan78xx_read_reg(dev, USB_CFG1, &buf);
1462 buf |= USB_CFG1_DEV_U1_INIT_EN_;
1463 ret = lan78xx_write_reg(dev, USB_CFG1, buf);
1467 /* enable U1 & U2 */
1468 ret = lan78xx_read_reg(dev, USB_CFG1, &buf);
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);
1479 ladv = phy_read(phydev, MII_ADVERTISE);
1483 radv = phy_read(phydev, MII_LPA);
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);
1491 ret = lan78xx_update_flowcontrol(dev, ecmd.base.duplex, ladv,
1496 if (!timer_pending(&dev->stat_monitor)) {
1498 mod_timer(&dev->stat_monitor,
1499 jiffies + STAT_UPDATE_TIMER);
1502 lan78xx_rx_urb_submit_all(dev);
1504 napi_schedule(&dev->napi);
1510 /* some work can't be done in tasklets, so we use keventd
1512 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
1513 * but tasklet_schedule() doesn't. hope the failure is rare.
1515 static void lan78xx_defer_kevent(struct lan78xx_net *dev, int work)
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);
1522 static void lan78xx_status(struct lan78xx_net *dev, struct urb *urb)
1526 if (urb->actual_length != 4) {
1527 netdev_warn(dev->net,
1528 "unexpected urb length %d", urb->actual_length);
1532 intdata = get_unaligned_le32(urb->transfer_buffer);
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);
1538 if (dev->domain_data.phyirq > 0)
1539 generic_handle_irq_safe(dev->domain_data.phyirq);
1541 netdev_warn(dev->net,
1542 "unexpected interrupt: 0x%08x\n", intdata);
1546 static int lan78xx_ethtool_get_eeprom_len(struct net_device *netdev)
1548 return MAX_EEPROM_SIZE;
1551 static int lan78xx_ethtool_get_eeprom(struct net_device *netdev,
1552 struct ethtool_eeprom *ee, u8 *data)
1554 struct lan78xx_net *dev = netdev_priv(netdev);
1557 ret = usb_autopm_get_interface(dev->intf);
1561 ee->magic = LAN78XX_EEPROM_MAGIC;
1563 ret = lan78xx_read_raw_eeprom(dev, ee->offset, ee->len, data);
1565 usb_autopm_put_interface(dev->intf);
1570 static int lan78xx_ethtool_set_eeprom(struct net_device *netdev,
1571 struct ethtool_eeprom *ee, u8 *data)
1573 struct lan78xx_net *dev = netdev_priv(netdev);
1576 ret = usb_autopm_get_interface(dev->intf);
1580 /* Invalid EEPROM_INDICATOR at offset zero will result in a failure
1581 * to load data from EEPROM
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) &&
1588 (data[0] == OTP_INDICATOR_1))
1589 ret = lan78xx_write_raw_otp(dev, ee->offset, ee->len, data);
1591 usb_autopm_put_interface(dev->intf);
1596 static void lan78xx_get_strings(struct net_device *netdev, u32 stringset,
1599 if (stringset == ETH_SS_STATS)
1600 memcpy(data, lan78xx_gstrings, sizeof(lan78xx_gstrings));
1603 static int lan78xx_get_sset_count(struct net_device *netdev, int sset)
1605 if (sset == ETH_SS_STATS)
1606 return ARRAY_SIZE(lan78xx_gstrings);
1611 static void lan78xx_get_stats(struct net_device *netdev,
1612 struct ethtool_stats *stats, u64 *data)
1614 struct lan78xx_net *dev = netdev_priv(netdev);
1616 lan78xx_update_stats(dev);
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);
1623 static void lan78xx_get_wol(struct net_device *netdev,
1624 struct ethtool_wolinfo *wol)
1626 struct lan78xx_net *dev = netdev_priv(netdev);
1629 struct lan78xx_priv *pdata = (struct lan78xx_priv *)(dev->data[0]);
1631 if (usb_autopm_get_interface(dev->intf) < 0)
1634 ret = lan78xx_read_reg(dev, USB_CFG0, &buf);
1635 if (unlikely(ret < 0)) {
1639 if (buf & USB_CFG_RMT_WKP_) {
1640 wol->supported = WAKE_ALL;
1641 wol->wolopts = pdata->wol;
1648 usb_autopm_put_interface(dev->intf);
1651 static int lan78xx_set_wol(struct net_device *netdev,
1652 struct ethtool_wolinfo *wol)
1654 struct lan78xx_net *dev = netdev_priv(netdev);
1655 struct lan78xx_priv *pdata = (struct lan78xx_priv *)(dev->data[0]);
1658 ret = usb_autopm_get_interface(dev->intf);
1662 if (wol->wolopts & ~WAKE_ALL)
1665 pdata->wol = wol->wolopts;
1667 device_set_wakeup_enable(&dev->udev->dev, (bool)wol->wolopts);
1669 phy_ethtool_set_wol(netdev->phydev, wol);
1671 usb_autopm_put_interface(dev->intf);
1676 static int lan78xx_get_eee(struct net_device *net, struct ethtool_eee *edata)
1678 struct lan78xx_net *dev = netdev_priv(net);
1679 struct phy_device *phydev = net->phydev;
1683 ret = usb_autopm_get_interface(dev->intf);
1687 ret = phy_ethtool_get_eee(phydev, edata);
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;
1701 edata->eee_enabled = false;
1702 edata->eee_active = false;
1703 edata->tx_lpi_enabled = false;
1704 edata->tx_lpi_timer = 0;
1709 usb_autopm_put_interface(dev->intf);
1714 static int lan78xx_set_eee(struct net_device *net, struct ethtool_eee *edata)
1716 struct lan78xx_net *dev = netdev_priv(net);
1720 ret = usb_autopm_get_interface(dev->intf);
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);
1729 phy_ethtool_set_eee(net->phydev, edata);
1731 buf = (u32)edata->tx_lpi_timer;
1732 ret = lan78xx_write_reg(dev, EEE_TX_LPI_REQ_DLY, buf);
1734 ret = lan78xx_read_reg(dev, MAC_CR, &buf);
1735 buf &= ~MAC_CR_EEE_EN_;
1736 ret = lan78xx_write_reg(dev, MAC_CR, buf);
1739 usb_autopm_put_interface(dev->intf);
1744 static u32 lan78xx_get_link(struct net_device *net)
1748 mutex_lock(&net->phydev->lock);
1749 phy_read_status(net->phydev);
1750 link = net->phydev->link;
1751 mutex_unlock(&net->phydev->lock);
1756 static void lan78xx_get_drvinfo(struct net_device *net,
1757 struct ethtool_drvinfo *info)
1759 struct lan78xx_net *dev = netdev_priv(net);
1761 strncpy(info->driver, DRIVER_NAME, sizeof(info->driver));
1762 usb_make_path(dev->udev, info->bus_info, sizeof(info->bus_info));
1765 static u32 lan78xx_get_msglevel(struct net_device *net)
1767 struct lan78xx_net *dev = netdev_priv(net);
1769 return dev->msg_enable;
1772 static void lan78xx_set_msglevel(struct net_device *net, u32 level)
1774 struct lan78xx_net *dev = netdev_priv(net);
1776 dev->msg_enable = level;
1779 static int lan78xx_get_link_ksettings(struct net_device *net,
1780 struct ethtool_link_ksettings *cmd)
1782 struct lan78xx_net *dev = netdev_priv(net);
1783 struct phy_device *phydev = net->phydev;
1786 ret = usb_autopm_get_interface(dev->intf);
1790 phy_ethtool_ksettings_get(phydev, cmd);
1792 usb_autopm_put_interface(dev->intf);
1797 static int lan78xx_set_link_ksettings(struct net_device *net,
1798 const struct ethtool_link_ksettings *cmd)
1800 struct lan78xx_net *dev = netdev_priv(net);
1801 struct phy_device *phydev = net->phydev;
1805 ret = usb_autopm_get_interface(dev->intf);
1809 /* change speed & duplex */
1810 ret = phy_ethtool_ksettings_set(phydev, cmd);
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);
1817 phy_write(phydev, MII_BMCR, temp);
1820 usb_autopm_put_interface(dev->intf);
1825 static void lan78xx_get_pause(struct net_device *net,
1826 struct ethtool_pauseparam *pause)
1828 struct lan78xx_net *dev = netdev_priv(net);
1829 struct phy_device *phydev = net->phydev;
1830 struct ethtool_link_ksettings ecmd;
1832 phy_ethtool_ksettings_get(phydev, &ecmd);
1834 pause->autoneg = dev->fc_autoneg;
1836 if (dev->fc_request_control & FLOW_CTRL_TX)
1837 pause->tx_pause = 1;
1839 if (dev->fc_request_control & FLOW_CTRL_RX)
1840 pause->rx_pause = 1;
1843 static int lan78xx_set_pause(struct net_device *net,
1844 struct ethtool_pauseparam *pause)
1846 struct lan78xx_net *dev = netdev_priv(net);
1847 struct phy_device *phydev = net->phydev;
1848 struct ethtool_link_ksettings ecmd;
1851 phy_ethtool_ksettings_get(phydev, &ecmd);
1853 if (pause->autoneg && !ecmd.base.autoneg) {
1858 dev->fc_request_control = 0;
1859 if (pause->rx_pause)
1860 dev->fc_request_control |= FLOW_CTRL_RX;
1862 if (pause->tx_pause)
1863 dev->fc_request_control |= FLOW_CTRL_TX;
1865 if (ecmd.base.autoneg) {
1866 __ETHTOOL_DECLARE_LINK_MODE_MASK(fc) = { 0, };
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);
1878 phy_ethtool_ksettings_set(phydev, &ecmd);
1881 dev->fc_autoneg = pause->autoneg;
1888 static int lan78xx_get_regs_len(struct net_device *netdev)
1890 if (!netdev->phydev)
1891 return (sizeof(lan78xx_regs));
1893 return (sizeof(lan78xx_regs) + PHY_REG_SIZE);
1897 lan78xx_get_regs(struct net_device *netdev, struct ethtool_regs *regs,
1902 struct lan78xx_net *dev = netdev_priv(netdev);
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]);
1908 if (!netdev->phydev)
1911 /* Read PHY registers */
1912 for (j = 0; j < 32; i++, j++)
1913 data[i] = phy_read(netdev->phydev, j);
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,
1941 static void lan78xx_init_mac_address(struct lan78xx_net *dev)
1943 u32 addr_lo, addr_hi;
1946 lan78xx_read_reg(dev, RX_ADDRL, &addr_lo);
1947 lan78xx_read_reg(dev, RX_ADDRH, &addr_hi);
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;
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");
1970 /* generate random MAC */
1971 eth_random_addr(addr);
1972 netif_dbg(dev, ifup, dev->net,
1973 "MAC address set to random addr");
1976 addr_lo = addr[0] | (addr[1] << 8) |
1977 (addr[2] << 16) | (addr[3] << 24);
1978 addr_hi = addr[4] | (addr[5] << 8);
1980 lan78xx_write_reg(dev, RX_ADDRL, addr_lo);
1981 lan78xx_write_reg(dev, RX_ADDRH, addr_hi);
1984 lan78xx_write_reg(dev, MAF_LO(0), addr_lo);
1985 lan78xx_write_reg(dev, MAF_HI(0), addr_hi | MAF_HI_VALID_);
1987 eth_hw_addr_set(dev->net, addr);
1990 /* MDIO read and write wrappers for phylib */
1991 static int lan78xx_mdiobus_read(struct mii_bus *bus, int phy_id, int idx)
1993 struct lan78xx_net *dev = bus->priv;
1997 ret = usb_autopm_get_interface(dev->intf);
2001 mutex_lock(&dev->phy_mutex);
2003 /* confirm MII not busy */
2004 ret = lan78xx_phy_wait_not_busy(dev);
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);
2012 ret = lan78xx_phy_wait_not_busy(dev);
2016 ret = lan78xx_read_reg(dev, MII_DATA, &val);
2018 ret = (int)(val & 0xFFFF);
2021 mutex_unlock(&dev->phy_mutex);
2022 usb_autopm_put_interface(dev->intf);
2027 static int lan78xx_mdiobus_write(struct mii_bus *bus, int phy_id, int idx,
2030 struct lan78xx_net *dev = bus->priv;
2034 ret = usb_autopm_get_interface(dev->intf);
2038 mutex_lock(&dev->phy_mutex);
2040 /* confirm MII not busy */
2041 ret = lan78xx_phy_wait_not_busy(dev);
2046 ret = lan78xx_write_reg(dev, MII_DATA, val);
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);
2052 ret = lan78xx_phy_wait_not_busy(dev);
2057 mutex_unlock(&dev->phy_mutex);
2058 usb_autopm_put_interface(dev->intf);
2062 static int lan78xx_mdio_init(struct lan78xx_net *dev)
2064 struct device_node *node;
2067 dev->mdiobus = mdiobus_alloc();
2068 if (!dev->mdiobus) {
2069 netdev_err(dev->net, "can't allocate MDIO bus\n");
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;
2079 snprintf(dev->mdiobus->id, MII_BUS_ID_SIZE, "usb-%03d:%03d",
2080 dev->udev->bus->busnum, dev->udev->devnum);
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);
2088 case ID_REV_CHIP_ID_7801_:
2089 /* scan thru PHYAD[2..0] */
2090 dev->mdiobus->phy_mask = ~(0xFF);
2094 node = of_get_child_by_name(dev->udev->dev.of_node, "mdio");
2095 ret = of_mdiobus_register(dev->mdiobus, node);
2098 netdev_err(dev->net, "can't register MDIO bus\n");
2102 netdev_dbg(dev->net, "registered mdiobus bus %s\n", dev->mdiobus->id);
2105 mdiobus_free(dev->mdiobus);
2109 static void lan78xx_remove_mdio(struct lan78xx_net *dev)
2111 mdiobus_unregister(dev->mdiobus);
2112 mdiobus_free(dev->mdiobus);
2115 static void lan78xx_link_status_change(struct net_device *net)
2117 struct phy_device *phydev = net->phydev;
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.
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);
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 */
2137 /* clear pending interrupt generated while workaround */
2138 temp = phy_read(phydev, LAN88XX_INT_STS);
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);
2147 static int irq_map(struct irq_domain *d, unsigned int irq,
2148 irq_hw_number_t hwirq)
2150 struct irq_domain_data *data = d->host_data;
2152 irq_set_chip_data(irq, data);
2153 irq_set_chip_and_handler(irq, data->irqchip, data->irq_handler);
2154 irq_set_noprobe(irq);
2159 static void irq_unmap(struct irq_domain *d, unsigned int irq)
2161 irq_set_chip_and_handler(irq, NULL, NULL);
2162 irq_set_chip_data(irq, NULL);
2165 static const struct irq_domain_ops chip_domain_ops = {
2170 static void lan78xx_irq_mask(struct irq_data *irqd)
2172 struct irq_domain_data *data = irq_data_get_irq_chip_data(irqd);
2174 data->irqenable &= ~BIT(irqd_to_hwirq(irqd));
2177 static void lan78xx_irq_unmask(struct irq_data *irqd)
2179 struct irq_domain_data *data = irq_data_get_irq_chip_data(irqd);
2181 data->irqenable |= BIT(irqd_to_hwirq(irqd));
2184 static void lan78xx_irq_bus_lock(struct irq_data *irqd)
2186 struct irq_domain_data *data = irq_data_get_irq_chip_data(irqd);
2188 mutex_lock(&data->irq_lock);
2191 static void lan78xx_irq_bus_sync_unlock(struct irq_data *irqd)
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);
2198 /* call register access here because irq_bus_lock & irq_bus_sync_unlock
2199 * are only two callbacks executed in non-atomic contex.
2201 lan78xx_read_reg(dev, INT_EP_CTL, &buf);
2202 if (buf != data->irqenable)
2203 lan78xx_write_reg(dev, INT_EP_CTL, data->irqenable);
2205 mutex_unlock(&data->irq_lock);
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,
2216 static int lan78xx_setup_irq_domain(struct lan78xx_net *dev)
2218 struct device_node *of_node;
2219 struct irq_domain *irqdomain;
2220 unsigned int irqmap = 0;
2224 of_node = dev->udev->dev.parent->of_node;
2226 mutex_init(&dev->domain_data.irq_lock);
2228 lan78xx_read_reg(dev, INT_EP_CTL, &buf);
2229 dev->domain_data.irqenable = buf;
2231 dev->domain_data.irqchip = &lan78xx_irqchip;
2232 dev->domain_data.irq_handler = handle_simple_irq;
2234 irqdomain = irq_domain_add_simple(of_node, MAX_INT_EP, 0,
2235 &chip_domain_ops, &dev->domain_data);
2237 /* create mapping for PHY interrupt */
2238 irqmap = irq_create_mapping(irqdomain, INT_EP_PHY);
2240 irq_domain_remove(irqdomain);
2249 dev->domain_data.irqdomain = irqdomain;
2250 dev->domain_data.phyirq = irqmap;
2255 static void lan78xx_remove_irq_domain(struct lan78xx_net *dev)
2257 if (dev->domain_data.phyirq > 0) {
2258 irq_dispose_mapping(dev->domain_data.phyirq);
2260 if (dev->domain_data.irqdomain)
2261 irq_domain_remove(dev->domain_data.irqdomain);
2263 dev->domain_data.phyirq = 0;
2264 dev->domain_data.irqdomain = NULL;
2267 static int lan8835_fixup(struct phy_device *phydev)
2270 struct lan78xx_net *dev = netdev_priv(phydev->attached_dev);
2272 /* LED2/PME_N/IRQ_N/RGMII_ID pin to IRQ_N mode */
2273 buf = phy_read_mmd(phydev, MDIO_MMD_PCS, 0x8010);
2276 phy_write_mmd(phydev, MDIO_MMD_PCS, 0x8010, buf);
2278 /* RGMII MAC TXC Delay Enable */
2279 lan78xx_write_reg(dev, MAC_RGMII_ID,
2280 MAC_RGMII_ID_TXC_DELAY_EN_);
2282 /* RGMII TX DLL Tune Adjust */
2283 lan78xx_write_reg(dev, RGMII_TX_BYP_DLL, 0x3D00);
2285 dev->interface = PHY_INTERFACE_MODE_RGMII_TXID;
2290 static int ksz9031rnx_fixup(struct phy_device *phydev)
2292 struct lan78xx_net *dev = netdev_priv(phydev->attached_dev);
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);
2302 dev->interface = PHY_INTERFACE_MODE_RGMII_RXID;
2307 static struct phy_device *lan7801_phy_init(struct lan78xx_net *dev)
2311 struct fixed_phy_status fphy_status = {
2313 .speed = SPEED_1000,
2314 .duplex = DUPLEX_FULL,
2316 struct phy_device *phydev;
2318 phydev = phy_find_first(dev->mdiobus);
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");
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);
2337 netdev_err(dev->net, "no PHY driver found\n");
2340 dev->interface = PHY_INTERFACE_MODE_RGMII;
2341 /* external PHY fixup for KSZ9031RNX */
2342 ret = phy_register_fixup_for_uid(PHY_KSZ9031RNX, 0xfffffff0,
2345 netdev_err(dev->net, "Failed to register fixup for PHY_KSZ9031RNX\n");
2348 /* external PHY fixup for LAN8835 */
2349 ret = phy_register_fixup_for_uid(PHY_LAN8835, 0xfffffff0,
2352 netdev_err(dev->net, "Failed to register fixup for PHY_LAN8835\n");
2355 /* add more external PHY fixup here if needed */
2357 phydev->is_internal = false;
2362 static int lan78xx_phy_init(struct lan78xx_net *dev)
2364 __ETHTOOL_DECLARE_LINK_MODE_MASK(fc) = { 0, };
2367 struct phy_device *phydev;
2369 switch (dev->chipid) {
2370 case ID_REV_CHIP_ID_7801_:
2371 phydev = lan7801_phy_init(dev);
2373 netdev_err(dev->net, "lan7801: PHY Init Failed");
2378 case ID_REV_CHIP_ID_7800_:
2379 case ID_REV_CHIP_ID_7850_:
2380 phydev = phy_find_first(dev->mdiobus);
2382 netdev_err(dev->net, "no PHY found\n");
2385 phydev->is_internal = true;
2386 dev->interface = PHY_INTERFACE_MODE_GMII;
2390 netdev_err(dev->net, "Unknown CHIP ID found\n");
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;
2398 phydev->irq = PHY_POLL;
2399 netdev_dbg(dev->net, "phydev->irq = %d\n", phydev->irq);
2401 /* set to AUTOMDIX */
2402 phydev->mdix = ETH_TP_MDI_AUTO;
2404 ret = phy_connect_direct(dev->net, phydev,
2405 lan78xx_link_status_change,
2408 netdev_err(dev->net, "can't attach PHY to %s\n",
2410 if (dev->chipid == ID_REV_CHIP_ID_7801_) {
2411 if (phy_is_pseudo_fixed_link(phydev)) {
2412 fixed_phy_unregister(phydev);
2414 phy_unregister_fixup_for_uid(PHY_KSZ9031RNX,
2416 phy_unregister_fixup_for_uid(PHY_LAN8835,
2423 /* MAC doesn't support 1000T Half */
2424 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_1000baseT_Half_BIT);
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);
2436 if (phydev->mdio.dev.of_node) {
2440 len = of_property_count_elems_of_size(phydev->mdio.dev.of_node,
2441 "microchip,led-modes",
2444 /* Ensure the appropriate LEDs are enabled */
2445 lan78xx_read_reg(dev, HW_CFG, ®);
2446 reg &= ~(HW_CFG_LED0_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);
2458 genphy_config_aneg(phydev);
2460 dev->fc_autoneg = phydev->autoneg;
2465 static int lan78xx_set_rx_max_frame_length(struct lan78xx_net *dev, int size)
2470 lan78xx_read_reg(dev, MAC_RX, &buf);
2472 rxenabled = ((buf & MAC_RX_RXEN_) != 0);
2475 buf &= ~MAC_RX_RXEN_;
2476 lan78xx_write_reg(dev, MAC_RX, buf);
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_);
2483 lan78xx_write_reg(dev, MAC_RX, buf);
2486 buf |= MAC_RX_RXEN_;
2487 lan78xx_write_reg(dev, MAC_RX, buf);
2493 static int unlink_urbs(struct lan78xx_net *dev, struct sk_buff_head *q)
2495 struct sk_buff *skb;
2496 unsigned long flags;
2499 spin_lock_irqsave(&q->lock, flags);
2500 while (!skb_queue_empty(q)) {
2501 struct skb_data *entry;
2505 skb_queue_walk(q, skb) {
2506 entry = (struct skb_data *)skb->cb;
2507 if (entry->state != unlink_start)
2512 entry->state = unlink_start;
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).
2522 spin_unlock_irqrestore(&q->lock, flags);
2523 /* during some PM-driven resume scenarios,
2524 * these (async) unlinks complete immediately
2526 ret = usb_unlink_urb(urb);
2527 if (ret != -EINPROGRESS && ret != 0)
2528 netdev_dbg(dev->net, "unlink urb err, %d\n", ret);
2532 spin_lock_irqsave(&q->lock, flags);
2534 spin_unlock_irqrestore(&q->lock, flags);
2538 static int lan78xx_change_mtu(struct net_device *netdev, int new_mtu)
2540 struct lan78xx_net *dev = netdev_priv(netdev);
2541 int max_frame_len = RX_MAX_FRAME_LEN(new_mtu);
2544 /* no second zero-length packet read wanted after mtu-sized packets */
2545 if ((max_frame_len % dev->maxpacket) == 0)
2548 ret = usb_autopm_get_interface(dev->intf);
2552 ret = lan78xx_set_rx_max_frame_length(dev, max_frame_len);
2554 netdev->mtu = new_mtu;
2556 usb_autopm_put_interface(dev->intf);
2561 static int lan78xx_set_mac_addr(struct net_device *netdev, void *p)
2563 struct lan78xx_net *dev = netdev_priv(netdev);
2564 struct sockaddr *addr = p;
2565 u32 addr_lo, addr_hi;
2567 if (netif_running(netdev))
2570 if (!is_valid_ether_addr(addr->sa_data))
2571 return -EADDRNOTAVAIL;
2573 eth_hw_addr_set(netdev, addr->sa_data);
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;
2582 lan78xx_write_reg(dev, RX_ADDRL, addr_lo);
2583 lan78xx_write_reg(dev, RX_ADDRH, addr_hi);
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_);
2592 /* Enable or disable Rx checksum offload engine */
2593 static int lan78xx_set_features(struct net_device *netdev,
2594 netdev_features_t features)
2596 struct lan78xx_net *dev = netdev_priv(netdev);
2597 struct lan78xx_priv *pdata = (struct lan78xx_priv *)(dev->data[0]);
2598 unsigned long flags;
2600 spin_lock_irqsave(&pdata->rfe_ctl_lock, flags);
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_;
2606 pdata->rfe_ctl &= ~(RFE_CTL_TCPUDP_COE_ | RFE_CTL_IP_COE_);
2607 pdata->rfe_ctl &= ~(RFE_CTL_ICMP_COE_ | RFE_CTL_IGMP_COE_);
2610 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2611 pdata->rfe_ctl |= RFE_CTL_VLAN_STRIP_;
2613 pdata->rfe_ctl &= ~RFE_CTL_VLAN_STRIP_;
2615 if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
2616 pdata->rfe_ctl |= RFE_CTL_VLAN_FILTER_;
2618 pdata->rfe_ctl &= ~RFE_CTL_VLAN_FILTER_;
2620 spin_unlock_irqrestore(&pdata->rfe_ctl_lock, flags);
2622 lan78xx_write_reg(dev, RFE_CTL, pdata->rfe_ctl);
2627 static void lan78xx_deferred_vlan_write(struct work_struct *param)
2629 struct lan78xx_priv *pdata =
2630 container_of(param, struct lan78xx_priv, set_vlan);
2631 struct lan78xx_net *dev = pdata->dev;
2633 lan78xx_dataport_write(dev, DP_SEL_RSEL_VLAN_DA_, 0,
2634 DP_SEL_VHF_VLAN_LEN, pdata->vlan_table);
2637 static int lan78xx_vlan_rx_add_vid(struct net_device *netdev,
2638 __be16 proto, u16 vid)
2640 struct lan78xx_net *dev = netdev_priv(netdev);
2641 struct lan78xx_priv *pdata = (struct lan78xx_priv *)(dev->data[0]);
2643 u16 vid_dword_index;
2645 vid_dword_index = (vid >> 5) & 0x7F;
2646 vid_bit_index = vid & 0x1F;
2648 pdata->vlan_table[vid_dword_index] |= (1 << vid_bit_index);
2650 /* defer register writes to a sleepable context */
2651 schedule_work(&pdata->set_vlan);
2656 static int lan78xx_vlan_rx_kill_vid(struct net_device *netdev,
2657 __be16 proto, u16 vid)
2659 struct lan78xx_net *dev = netdev_priv(netdev);
2660 struct lan78xx_priv *pdata = (struct lan78xx_priv *)(dev->data[0]);
2662 u16 vid_dword_index;
2664 vid_dword_index = (vid >> 5) & 0x7F;
2665 vid_bit_index = vid & 0x1F;
2667 pdata->vlan_table[vid_dword_index] &= ~(1 << vid_bit_index);
2669 /* defer register writes to a sleepable context */
2670 schedule_work(&pdata->set_vlan);
2675 static void lan78xx_init_ltm(struct lan78xx_net *dev)
2679 u32 regs[6] = { 0 };
2681 ret = lan78xx_read_reg(dev, USB_CFG1, &buf);
2682 if (buf & USB_CFG1_LTM_ENABLE_) {
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,
2694 } else if (lan78xx_read_otp(dev, 0x3F, 2, temp) == 0) {
2695 if (temp[0] == 24) {
2696 ret = lan78xx_read_raw_otp(dev,
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]);
2714 static int lan78xx_urb_config_init(struct lan78xx_net *dev)
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;
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;
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;
2744 netdev_warn(dev->net, "USB bus speed not supported\n");
2752 static int lan78xx_start_hw(struct lan78xx_net *dev, u32 reg, u32 hw_enable)
2754 return lan78xx_update_reg(dev, reg, hw_enable, hw_enable);
2757 static int lan78xx_stop_hw(struct lan78xx_net *dev, u32 reg, u32 hw_enabled,
2760 unsigned long timeout;
2761 bool stopped = true;
2765 /* Stop the h/w block (if not already stopped) */
2767 ret = lan78xx_read_reg(dev, reg, &buf);
2771 if (buf & hw_enabled) {
2774 ret = lan78xx_write_reg(dev, reg, buf);
2779 timeout = jiffies + HW_DISABLE_TIMEOUT;
2781 ret = lan78xx_read_reg(dev, reg, &buf);
2785 if (buf & hw_disabled)
2788 msleep(HW_DISABLE_DELAY_MS);
2789 } while (!stopped && !time_after(jiffies, timeout));
2792 ret = stopped ? 0 : -ETIME;
2797 static int lan78xx_flush_fifo(struct lan78xx_net *dev, u32 reg, u32 fifo_flush)
2799 return lan78xx_update_reg(dev, reg, fifo_flush, fifo_flush);
2802 static int lan78xx_start_tx_path(struct lan78xx_net *dev)
2806 netif_dbg(dev, drv, dev->net, "start tx path");
2808 /* Start the MAC transmitter */
2810 ret = lan78xx_start_hw(dev, MAC_TX, MAC_TX_TXEN_);
2814 /* Start the Tx FIFO */
2816 ret = lan78xx_start_hw(dev, FCT_TX_CTL, FCT_TX_CTL_EN_);
2823 static int lan78xx_stop_tx_path(struct lan78xx_net *dev)
2827 netif_dbg(dev, drv, dev->net, "stop tx path");
2829 /* Stop the Tx FIFO */
2831 ret = lan78xx_stop_hw(dev, FCT_TX_CTL, FCT_TX_CTL_EN_, FCT_TX_CTL_DIS_);
2835 /* Stop the MAC transmitter */
2837 ret = lan78xx_stop_hw(dev, MAC_TX, MAC_TX_TXEN_, MAC_TX_TXD_);
2844 /* The caller must ensure the Tx path is stopped before calling
2845 * lan78xx_flush_tx_fifo().
2847 static int lan78xx_flush_tx_fifo(struct lan78xx_net *dev)
2849 return lan78xx_flush_fifo(dev, FCT_TX_CTL, FCT_TX_CTL_RST_);
2852 static int lan78xx_start_rx_path(struct lan78xx_net *dev)
2856 netif_dbg(dev, drv, dev->net, "start rx path");
2858 /* Start the Rx FIFO */
2860 ret = lan78xx_start_hw(dev, FCT_RX_CTL, FCT_RX_CTL_EN_);
2864 /* Start the MAC receiver*/
2866 ret = lan78xx_start_hw(dev, MAC_RX, MAC_RX_RXEN_);
2873 static int lan78xx_stop_rx_path(struct lan78xx_net *dev)
2877 netif_dbg(dev, drv, dev->net, "stop rx path");
2879 /* Stop the MAC receiver */
2881 ret = lan78xx_stop_hw(dev, MAC_RX, MAC_RX_RXEN_, MAC_RX_RXD_);
2885 /* Stop the Rx FIFO */
2887 ret = lan78xx_stop_hw(dev, FCT_RX_CTL, FCT_RX_CTL_EN_, FCT_RX_CTL_DIS_);
2894 /* The caller must ensure the Rx path is stopped before calling
2895 * lan78xx_flush_rx_fifo().
2897 static int lan78xx_flush_rx_fifo(struct lan78xx_net *dev)
2899 return lan78xx_flush_fifo(dev, FCT_RX_CTL, FCT_RX_CTL_RST_);
2902 static int lan78xx_reset(struct lan78xx_net *dev)
2904 struct lan78xx_priv *pdata = (struct lan78xx_priv *)(dev->data[0]);
2905 unsigned long timeout;
2910 ret = lan78xx_read_reg(dev, HW_CFG, &buf);
2914 buf |= HW_CFG_LRST_;
2916 ret = lan78xx_write_reg(dev, HW_CFG, buf);
2920 timeout = jiffies + HZ;
2923 ret = lan78xx_read_reg(dev, HW_CFG, &buf);
2927 if (time_after(jiffies, timeout)) {
2928 netdev_warn(dev->net,
2929 "timeout on completion of LiteReset");
2933 } while (buf & HW_CFG_LRST_);
2935 lan78xx_init_mac_address(dev);
2937 /* save DEVID for later usage */
2938 ret = lan78xx_read_reg(dev, ID_REV, &buf);
2942 dev->chipid = (buf & ID_REV_CHIP_ID_MASK_) >> 16;
2943 dev->chiprev = buf & ID_REV_CHIP_REV_MASK_;
2945 /* Respond to the IN token with a NAK */
2946 ret = lan78xx_read_reg(dev, USB_CFG0, &buf);
2950 buf |= USB_CFG_BIR_;
2952 ret = lan78xx_write_reg(dev, USB_CFG0, buf);
2957 lan78xx_init_ltm(dev);
2959 ret = lan78xx_write_reg(dev, BURST_CAP, dev->burst_cap);
2963 ret = lan78xx_write_reg(dev, BULK_IN_DLY, dev->bulk_in_delay);
2967 ret = lan78xx_read_reg(dev, HW_CFG, &buf);
2973 ret = lan78xx_write_reg(dev, HW_CFG, buf);
2977 ret = lan78xx_read_reg(dev, USB_CFG0, &buf);
2981 buf |= USB_CFG_BCE_;
2983 ret = lan78xx_write_reg(dev, USB_CFG0, buf);
2987 /* set FIFO sizes */
2988 buf = (MAX_RX_FIFO_SIZE - 512) / 512;
2990 ret = lan78xx_write_reg(dev, FCT_RX_FIFO_END, buf);
2994 buf = (MAX_TX_FIFO_SIZE - 512) / 512;
2996 ret = lan78xx_write_reg(dev, FCT_TX_FIFO_END, buf);
3000 ret = lan78xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
3004 ret = lan78xx_write_reg(dev, FLOW, 0);
3008 ret = lan78xx_write_reg(dev, FCT_FLOW, 0);
3012 /* Don't need rfe_ctl_lock during initialisation */
3013 ret = lan78xx_read_reg(dev, RFE_CTL, &pdata->rfe_ctl);
3017 pdata->rfe_ctl |= RFE_CTL_BCAST_EN_ | RFE_CTL_DA_PERFECT_;
3019 ret = lan78xx_write_reg(dev, RFE_CTL, pdata->rfe_ctl);
3023 /* Enable or disable checksum offload engines */
3024 ret = lan78xx_set_features(dev->net, dev->net->features);
3028 lan78xx_set_multicast(dev->net);
3031 ret = lan78xx_read_reg(dev, PMT_CTL, &buf);
3035 buf |= PMT_CTL_PHY_RST_;
3037 ret = lan78xx_write_reg(dev, PMT_CTL, buf);
3041 timeout = jiffies + HZ;
3044 ret = lan78xx_read_reg(dev, PMT_CTL, &buf);
3048 if (time_after(jiffies, timeout)) {
3049 netdev_warn(dev->net, "timeout waiting for PHY Reset");
3053 } while ((buf & PMT_CTL_PHY_RST_) || !(buf & PMT_CTL_READY_));
3055 ret = lan78xx_read_reg(dev, MAC_CR, &buf);
3059 /* LAN7801 only has RGMII mode */
3060 if (dev->chipid == ID_REV_CHIP_ID_7801_)
3061 buf &= ~MAC_CR_GMII_EN_;
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_;
3071 ret = lan78xx_write_reg(dev, MAC_CR, buf);
3075 ret = lan78xx_set_rx_max_frame_length(dev,
3076 RX_MAX_FRAME_LEN(dev->net->mtu));
3081 static void lan78xx_init_stats(struct lan78xx_net *dev)
3086 /* initialize for stats update
3087 * some counters are 20bits and some are 32bits
3089 p = (u32 *)&dev->stats.rollover_max;
3090 for (i = 0; i < (sizeof(dev->stats.rollover_max) / (sizeof(u32))); i++)
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;
3104 set_bit(EVENT_STAT_UPDATE, &dev->flags);
3107 static int lan78xx_open(struct net_device *net)
3109 struct lan78xx_net *dev = netdev_priv(net);
3112 netif_dbg(dev, ifup, dev->net, "open device");
3114 ret = usb_autopm_get_interface(dev->intf);
3118 mutex_lock(&dev->dev_mutex);
3120 phy_start(net->phydev);
3122 netif_dbg(dev, ifup, dev->net, "phy initialised successfully");
3124 /* for Link Check */
3125 if (dev->urb_intr) {
3126 ret = usb_submit_urb(dev->urb_intr, GFP_KERNEL);
3128 netif_err(dev, ifup, dev->net,
3129 "intr submit %d\n", ret);
3134 ret = lan78xx_flush_rx_fifo(dev);
3137 ret = lan78xx_flush_tx_fifo(dev);
3141 ret = lan78xx_start_tx_path(dev);
3144 ret = lan78xx_start_rx_path(dev);
3148 lan78xx_init_stats(dev);
3150 set_bit(EVENT_DEV_OPEN, &dev->flags);
3152 netif_start_queue(net);
3154 dev->link_on = false;
3156 napi_enable(&dev->napi);
3158 lan78xx_defer_kevent(dev, EVENT_LINK_RESET);
3160 mutex_unlock(&dev->dev_mutex);
3162 usb_autopm_put_interface(dev->intf);
3167 static void lan78xx_terminate_urbs(struct lan78xx_net *dev)
3169 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(unlink_wakeup);
3170 DECLARE_WAITQUEUE(wait, current);
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);
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);
3187 set_current_state(TASK_RUNNING);
3189 remove_wait_queue(&unlink_wakeup, &wait);
3191 /* empty Rx done, Rx overflow and Tx pend queues
3193 while (!skb_queue_empty(&dev->rxq_done)) {
3194 struct sk_buff *skb = skb_dequeue(&dev->rxq_done);
3196 lan78xx_release_rx_buf(dev, skb);
3199 skb_queue_purge(&dev->rxq_overflow);
3200 skb_queue_purge(&dev->txq_pend);
3203 static int lan78xx_stop(struct net_device *net)
3205 struct lan78xx_net *dev = netdev_priv(net);
3207 netif_dbg(dev, ifup, dev->net, "stop device");
3209 mutex_lock(&dev->dev_mutex);
3211 if (timer_pending(&dev->stat_monitor))
3212 del_timer_sync(&dev->stat_monitor);
3214 clear_bit(EVENT_DEV_OPEN, &dev->flags);
3215 netif_stop_queue(net);
3216 napi_disable(&dev->napi);
3218 lan78xx_terminate_urbs(dev);
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);
3225 /* ignore errors that occur stopping the Tx and Rx data paths */
3226 lan78xx_stop_tx_path(dev);
3227 lan78xx_stop_rx_path(dev);
3230 phy_stop(net->phydev);
3232 usb_kill_urb(dev->urb_intr);
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.
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);
3243 cancel_delayed_work_sync(&dev->wq);
3245 usb_autopm_put_interface(dev->intf);
3247 mutex_unlock(&dev->dev_mutex);
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)
3255 unsigned long flags;
3256 enum skb_state old_state;
3257 struct skb_data *entry = (struct skb_data *)skb->cb;
3259 spin_lock_irqsave(&list->lock, flags);
3260 old_state = entry->state;
3261 entry->state = state;
3263 __skb_unlink(skb, list);
3264 spin_unlock(&list->lock);
3265 spin_lock(&dev->rxq_done.lock);
3267 __skb_queue_tail(&dev->rxq_done, skb);
3268 if (skb_queue_len(&dev->rxq_done) == 1)
3269 napi_schedule(&dev->napi);
3271 spin_unlock_irqrestore(&dev->rxq_done.lock, flags);
3276 static void tx_complete(struct urb *urb)
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;
3282 if (urb->status == 0) {
3283 dev->net->stats.tx_packets += entry->num_of_packet;
3284 dev->net->stats.tx_bytes += entry->length;
3286 dev->net->stats.tx_errors += entry->num_of_packet;
3288 switch (urb->status) {
3290 lan78xx_defer_kevent(dev, EVENT_TX_HALT);
3293 /* software-driven interface shutdown */
3296 netif_dbg(dev, tx_err, dev->net,
3297 "tx err interface gone %d\n",
3298 entry->urb->status);
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);
3310 netif_dbg(dev, tx_err, dev->net,
3311 "unknown tx err %d\n",
3312 entry->urb->status);
3317 usb_autopm_put_interface_async(dev->intf);
3319 skb_unlink(skb, &dev->txq);
3321 lan78xx_release_tx_buf(dev, skb);
3323 /* Re-schedule NAPI if Tx data pending but no URBs in progress.
3325 if (skb_queue_empty(&dev->txq) &&
3326 !skb_queue_empty(&dev->txq_pend))
3327 napi_schedule(&dev->napi);
3330 static void lan78xx_queue_skb(struct sk_buff_head *list,
3331 struct sk_buff *newsk, enum skb_state state)
3333 struct skb_data *entry = (struct skb_data *)newsk->cb;
3335 __skb_queue_tail(list, newsk);
3336 entry->state = state;
3339 static unsigned int lan78xx_tx_urb_space(struct lan78xx_net *dev)
3341 return skb_queue_len(&dev->txq_free) * dev->tx_urb_size;
3344 static unsigned int lan78xx_tx_pend_data_len(struct lan78xx_net *dev)
3346 return dev->tx_pend_data_len;
3349 static void lan78xx_tx_pend_skb_add(struct lan78xx_net *dev,
3350 struct sk_buff *skb,
3351 unsigned int *tx_pend_data_len)
3353 unsigned long flags;
3355 spin_lock_irqsave(&dev->txq_pend.lock, flags);
3357 __skb_queue_tail(&dev->txq_pend, skb);
3359 dev->tx_pend_data_len += skb->len;
3360 *tx_pend_data_len = dev->tx_pend_data_len;
3362 spin_unlock_irqrestore(&dev->txq_pend.lock, flags);
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)
3369 unsigned long flags;
3371 spin_lock_irqsave(&dev->txq_pend.lock, flags);
3373 __skb_queue_head(&dev->txq_pend, skb);
3375 dev->tx_pend_data_len += skb->len;
3376 *tx_pend_data_len = dev->tx_pend_data_len;
3378 spin_unlock_irqrestore(&dev->txq_pend.lock, flags);
3381 static void lan78xx_tx_pend_skb_get(struct lan78xx_net *dev,
3382 struct sk_buff **skb,
3383 unsigned int *tx_pend_data_len)
3385 unsigned long flags;
3387 spin_lock_irqsave(&dev->txq_pend.lock, flags);
3389 *skb = __skb_dequeue(&dev->txq_pend);
3391 dev->tx_pend_data_len -= (*skb)->len;
3392 *tx_pend_data_len = dev->tx_pend_data_len;
3394 spin_unlock_irqrestore(&dev->txq_pend.lock, flags);
3398 lan78xx_start_xmit(struct sk_buff *skb, struct net_device *net)
3400 struct lan78xx_net *dev = netdev_priv(net);
3401 unsigned int tx_pend_data_len;
3403 if (test_bit(EVENT_DEV_ASLEEP, &dev->flags))
3404 schedule_delayed_work(&dev->wq, 0);
3406 skb_tx_timestamp(skb);
3408 lan78xx_tx_pend_skb_add(dev, skb, &tx_pend_data_len);
3410 /* Set up a Tx URB if none is in progress */
3412 if (skb_queue_empty(&dev->txq))
3413 napi_schedule(&dev->napi);
3415 /* Stop stack Tx queue if we have enough data to fill
3416 * all the free Tx URBs.
3418 if (tx_pend_data_len > lan78xx_tx_urb_space(dev)) {
3419 netif_stop_queue(net);
3421 netif_dbg(dev, hw, dev->net, "tx data len: %u, urb space %u",
3422 tx_pend_data_len, lan78xx_tx_urb_space(dev));
3424 /* Kick off transmission of pending data */
3426 if (!skb_queue_empty(&dev->txq_free))
3427 napi_schedule(&dev->napi);
3430 return NETDEV_TX_OK;
3433 static int lan78xx_bind(struct lan78xx_net *dev, struct usb_interface *intf)
3435 struct lan78xx_priv *pdata = NULL;
3439 dev->data[0] = (unsigned long)kzalloc(sizeof(*pdata), GFP_KERNEL);
3441 pdata = (struct lan78xx_priv *)(dev->data[0]);
3443 netdev_warn(dev->net, "Unable to allocate lan78xx_priv");
3449 spin_lock_init(&pdata->rfe_ctl_lock);
3450 mutex_init(&pdata->dataport_mutex);
3452 INIT_WORK(&pdata->set_multicast, lan78xx_deferred_multicast_write);
3454 for (i = 0; i < DP_SEL_VHF_VLAN_LEN; i++)
3455 pdata->vlan_table[i] = 0;
3457 INIT_WORK(&pdata->set_vlan, lan78xx_deferred_vlan_write);
3459 dev->net->features = 0;
3461 if (DEFAULT_TX_CSUM_ENABLE)
3462 dev->net->features |= NETIF_F_HW_CSUM;
3464 if (DEFAULT_RX_CSUM_ENABLE)
3465 dev->net->features |= NETIF_F_RXCSUM;
3467 if (DEFAULT_TSO_CSUM_ENABLE)
3468 dev->net->features |= NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_SG;
3470 if (DEFAULT_VLAN_RX_OFFLOAD)
3471 dev->net->features |= NETIF_F_HW_VLAN_CTAG_RX;
3473 if (DEFAULT_VLAN_FILTER_ENABLE)
3474 dev->net->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
3476 dev->net->hw_features = dev->net->features;
3478 ret = lan78xx_setup_irq_domain(dev);
3480 netdev_warn(dev->net,
3481 "lan78xx_setup_irq_domain() failed : %d", ret);
3485 /* Init all registers */
3486 ret = lan78xx_reset(dev);
3488 netdev_warn(dev->net, "Registers INIT FAILED....");
3492 ret = lan78xx_mdio_init(dev);
3494 netdev_warn(dev->net, "MDIO INIT FAILED.....");
3498 dev->net->flags |= IFF_MULTICAST;
3500 pdata->wol = WAKE_MAGIC;
3505 lan78xx_remove_irq_domain(dev);
3508 netdev_warn(dev->net, "Bind routine FAILED");
3509 cancel_work_sync(&pdata->set_multicast);
3510 cancel_work_sync(&pdata->set_vlan);
3515 static void lan78xx_unbind(struct lan78xx_net *dev, struct usb_interface *intf)
3517 struct lan78xx_priv *pdata = (struct lan78xx_priv *)(dev->data[0]);
3519 lan78xx_remove_irq_domain(dev);
3521 lan78xx_remove_mdio(dev);
3524 cancel_work_sync(&pdata->set_multicast);
3525 cancel_work_sync(&pdata->set_vlan);
3526 netif_dbg(dev, ifdown, dev->net, "free pdata");
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)
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.
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;
3546 skb->csum = ntohs((u16)(rx_cmd_b >> RX_CMD_B_CSUM_SHIFT_));
3547 skb->ip_summed = CHECKSUM_COMPLETE;
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)
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));
3561 static void lan78xx_skb_return(struct lan78xx_net *dev, struct sk_buff *skb)
3563 dev->net->stats.rx_packets++;
3564 dev->net->stats.rx_bytes += skb->len;
3566 skb->protocol = eth_type_trans(skb, dev->net);
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));
3572 if (skb_defer_rx_timestamp(skb))
3575 napi_gro_receive(&dev->napi, skb);
3578 static int lan78xx_rx(struct lan78xx_net *dev, struct sk_buff *skb,
3579 int budget, int *work_done)
3581 if (skb->len < RX_SKB_MIN_LEN)
3584 /* Extract frames from the URB buffer and pass each one to
3585 * the stack in a new NAPI SKB.
3587 while (skb->len > 0) {
3588 u32 rx_cmd_a, rx_cmd_b, align_count, size;
3590 unsigned char *packet;
3592 rx_cmd_a = get_unaligned_le32(skb->data);
3593 skb_pull(skb, sizeof(rx_cmd_a));
3595 rx_cmd_b = get_unaligned_le32(skb->data);
3596 skb_pull(skb, sizeof(rx_cmd_b));
3598 rx_cmd_c = get_unaligned_le16(skb->data);
3599 skb_pull(skb, sizeof(rx_cmd_c));
3603 /* get the packet length */
3604 size = (rx_cmd_a & RX_CMD_A_LEN_MASK_);
3605 align_count = (4 - ((size + RXW_PADDING) % 4)) % 4;
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);
3611 u32 frame_len = size - ETH_FCS_LEN;
3612 struct sk_buff *skb2;
3614 skb2 = napi_alloc_skb(&dev->napi, frame_len);
3618 memcpy(skb2->data, packet, frame_len);
3620 skb_put(skb2, frame_len);
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);
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.
3630 if (*work_done < budget) {
3631 lan78xx_skb_return(dev, skb2);
3634 skb_queue_tail(&dev->rxq_overflow, skb2);
3638 skb_pull(skb, size);
3640 /* skip padding bytes before the next frame starts */
3642 skb_pull(skb, align_count);
3648 static inline void rx_process(struct lan78xx_net *dev, struct sk_buff *skb,
3649 int budget, int *work_done)
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++;
3657 static void rx_complete(struct urb *urb)
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;
3665 netif_dbg(dev, rx_status, dev->net,
3666 "rx done: status %d", urb->status);
3668 skb_put(skb, urb->actual_length);
3671 if (urb != entry->urb)
3672 netif_warn(dev, rx_err, dev->net, "URB pointer mismatch");
3674 switch (urb_status) {
3676 if (skb->len < RX_SKB_MIN_LEN) {
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);
3683 usb_mark_last_busy(dev->udev);
3686 dev->net->stats.rx_errors++;
3687 lan78xx_defer_kevent(dev, EVENT_RX_HALT);
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);
3698 dev->net->stats.rx_errors++;
3702 /* data overrun ... flush fifo? */
3704 dev->net->stats.rx_over_errors++;
3709 dev->net->stats.rx_errors++;
3710 netif_dbg(dev, rx_err, dev->net, "rx status %d\n", urb_status);
3714 state = defer_bh(dev, skb, &dev->rxq, state);
3717 static int rx_submit(struct lan78xx_net *dev, struct sk_buff *skb, gfp_t flags)
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;
3725 usb_fill_bulk_urb(urb, dev->udev, dev->pipe_in,
3726 skb->data, size, rx_complete, skb);
3728 spin_lock_irqsave(&dev->rxq.lock, lockflags);
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);
3737 lan78xx_queue_skb(&dev->rxq, skb, rx_start);
3740 lan78xx_defer_kevent(dev, EVENT_RX_HALT);
3744 netif_dbg(dev, ifdown, dev->net, "device gone\n");
3745 netif_device_detach(dev->net);
3749 napi_schedule(&dev->napi);
3752 netif_dbg(dev, rx_err, dev->net,
3753 "rx submit, %d\n", ret);
3754 napi_schedule(&dev->napi);
3758 netif_dbg(dev, ifdown, dev->net, "rx: stopped\n");
3761 spin_unlock_irqrestore(&dev->rxq.lock, lockflags);
3764 lan78xx_release_rx_buf(dev, skb);
3769 static void lan78xx_rx_urb_submit_all(struct lan78xx_net *dev)
3771 struct sk_buff *rx_buf;
3773 /* Ensure the maximum number of Rx URBs is submitted
3775 while ((rx_buf = lan78xx_get_rx_buf(dev)) != NULL) {
3776 if (rx_submit(dev, rx_buf, GFP_ATOMIC) != 0)
3781 static void lan78xx_rx_urb_resubmit(struct lan78xx_net *dev,
3782 struct sk_buff *rx_buf)
3784 /* reset SKB data pointers */
3786 rx_buf->data = rx_buf->head;
3787 skb_reset_tail_pointer(rx_buf);
3789 rx_buf->data_len = 0;
3791 rx_submit(dev, rx_buf, GFP_ATOMIC);
3794 static void lan78xx_fill_tx_cmd_words(struct sk_buff *skb, u8 *buffer)
3799 tx_cmd_a = (u32)(skb->len & TX_CMD_A_LEN_MASK_) | TX_CMD_A_FCS_;
3801 if (skb->ip_summed == CHECKSUM_PARTIAL)
3802 tx_cmd_a |= TX_CMD_A_IPE_ | TX_CMD_A_TPE_;
3805 if (skb_is_gso(skb)) {
3806 u16 mss = max(skb_shinfo(skb)->gso_size, TX_CMD_B_MSS_MIN_);
3808 tx_cmd_b = (mss << TX_CMD_B_MSS_SHIFT_) & TX_CMD_B_MSS_MASK_;
3810 tx_cmd_a |= TX_CMD_A_LSO_;
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_;
3818 put_unaligned_le32(tx_cmd_a, buffer);
3819 put_unaligned_le32(tx_cmd_b, buffer + 4);
3822 static struct skb_data *lan78xx_tx_buf_fill(struct lan78xx_net *dev,
3823 struct sk_buff *tx_buf)
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;
3830 entry->num_of_packet = 0;
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.
3836 * There must be at least DST+SRC+TYPE in the SKB (with padding enabled)
3838 while (remain >= TX_SKB_MIN_LEN) {
3839 unsigned int pending_bytes;
3840 unsigned int align_bytes;
3841 struct sk_buff *skb;
3844 lan78xx_tx_pend_skb_get(dev, &skb, &pending_bytes);
3849 align_bytes = (TX_ALIGNMENT - (urb_len % TX_ALIGNMENT)) %
3851 len = align_bytes + TX_CMD_LEN + skb->len;
3853 lan78xx_tx_pend_skb_head_add(dev, skb, &pending_bytes);
3857 tx_data += align_bytes;
3859 lan78xx_fill_tx_cmd_words(skb, tx_data);
3860 tx_data += TX_CMD_LEN;
3863 if (skb_copy_bits(skb, 0, tx_data, len) < 0) {
3864 struct net_device_stats *stats = &dev->net->stats;
3866 stats->tx_dropped++;
3867 dev_kfree_skb_any(skb);
3868 tx_data -= TX_CMD_LEN;
3873 entry->length += len;
3874 entry->num_of_packet += skb_shinfo(skb)->gso_segs ?: 1;
3876 dev_kfree_skb_any(skb);
3878 urb_len = (u32)(tx_data - (u8 *)tx_buf->data);
3880 remain = dev->tx_urb_size - urb_len;
3883 skb_put(tx_buf, urb_len);
3888 static void lan78xx_tx_bh(struct lan78xx_net *dev)
3892 /* Start the stack Tx queue if it was stopped
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);
3899 netif_tx_unlock(dev->net);
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.
3906 struct skb_data *entry;
3907 struct sk_buff *tx_buf;
3908 unsigned long flags;
3910 if (skb_queue_empty(&dev->txq_pend))
3913 tx_buf = lan78xx_get_tx_buf(dev);
3917 entry = lan78xx_tx_buf_fill(dev, tx_buf);
3919 spin_lock_irqsave(&dev->txq.lock, flags);
3920 ret = usb_autopm_get_interface_async(dev->intf);
3922 spin_unlock_irqrestore(&dev->txq.lock, flags);
3926 usb_fill_bulk_urb(entry->urb, dev->udev, dev->pipe_out,
3927 tx_buf->data, tx_buf->len, tx_complete,
3930 if (tx_buf->len % dev->maxpacket == 0) {
3931 /* send USB_ZERO_PACKET */
3932 entry->urb->transfer_flags |= URB_ZERO_PACKET;
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");
3946 ret = usb_submit_urb(entry->urb, GFP_ATOMIC);
3949 netif_trans_update(dev->net);
3950 lan78xx_queue_skb(&dev->txq, tx_buf, tx_start);
3953 netif_stop_queue(dev->net);
3954 lan78xx_defer_kevent(dev, EVENT_TX_HALT);
3955 usb_autopm_put_interface_async(dev->intf);
3959 netif_dbg(dev, tx_err, dev->net,
3960 "tx submit urb err %d (disconnected?)", ret);
3961 netif_device_detach(dev->net);
3964 usb_autopm_put_interface_async(dev->intf);
3965 netif_dbg(dev, tx_err, dev->net,
3966 "tx submit urb err %d\n", ret);
3970 spin_unlock_irqrestore(&dev->txq.lock, flags);
3973 netdev_warn(dev->net, "failed to tx urb %d\n", ret);
3975 dev->net->stats.tx_dropped += entry->num_of_packet;
3976 lan78xx_release_tx_buf(dev, tx_buf);
3981 static int lan78xx_bh(struct lan78xx_net *dev, int budget)
3983 struct sk_buff_head done;
3984 struct sk_buff *rx_buf;
3985 struct skb_data *entry;
3986 unsigned long flags;
3989 /* Pass frames received in the last NAPI cycle before
3990 * working on newly completed URBs.
3992 while (!skb_queue_empty(&dev->rxq_overflow)) {
3993 lan78xx_skb_return(dev, skb_dequeue(&dev->rxq_overflow));
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.
4001 __skb_queue_head_init(&done);
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);
4007 /* Extract receive frames from completed URBs and
4008 * pass them to the stack. Re-submit each completed URB.
4010 while ((work_done < budget) &&
4011 (rx_buf = __skb_dequeue(&done))) {
4012 entry = (struct skb_data *)(rx_buf->cb);
4013 switch (entry->state) {
4015 rx_process(dev, rx_buf, budget, &work_done);
4020 netdev_dbg(dev->net, "rx buf state %d\n",
4025 lan78xx_rx_urb_resubmit(dev, rx_buf);
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.
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);
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)) {
4040 mod_timer(&dev->stat_monitor,
4041 jiffies + STAT_UPDATE_TIMER);
4044 /* Submit all free Rx URBs */
4046 if (!test_bit(EVENT_RX_HALT, &dev->flags))
4047 lan78xx_rx_urb_submit_all(dev);
4049 /* Submit new Tx URBs */
4057 static int lan78xx_poll(struct napi_struct *napi, int budget)
4059 struct lan78xx_net *dev = container_of(napi, struct lan78xx_net, napi);
4060 int result = budget;
4063 /* Don't do any work if the device is suspended */
4065 if (test_bit(EVENT_DEV_ASLEEP, &dev->flags)) {
4066 napi_complete_done(napi, 0);
4070 /* Process completed URBs and submit new URBs */
4072 work_done = lan78xx_bh(dev, budget);
4074 if (work_done < budget) {
4075 napi_complete_done(napi, work_done);
4077 /* Start a new polling cycle if data was received or
4078 * data is waiting to be transmitted.
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);
4087 netif_tx_lock(dev->net);
4088 if (netif_queue_stopped(dev->net)) {
4089 netif_wake_queue(dev->net);
4090 napi_schedule(napi);
4092 netif_tx_unlock(dev->net);
4101 static void lan78xx_delayedwork(struct work_struct *work)
4104 struct lan78xx_net *dev;
4106 dev = container_of(work, struct lan78xx_net, wq.work);
4108 if (test_bit(EVENT_DEV_DISCONNECT, &dev->flags))
4111 if (usb_autopm_get_interface(dev->intf) < 0)
4114 if (test_bit(EVENT_TX_HALT, &dev->flags)) {
4115 unlink_urbs(dev, &dev->txq);
4117 status = usb_clear_halt(dev->udev, dev->pipe_out);
4120 status != -ESHUTDOWN) {
4121 if (netif_msg_tx_err(dev))
4122 netdev_err(dev->net,
4123 "can't clear tx halt, status %d\n",
4126 clear_bit(EVENT_TX_HALT, &dev->flags);
4127 if (status != -ESHUTDOWN)
4128 netif_wake_queue(dev->net);
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);
4137 status != -ESHUTDOWN) {
4138 if (netif_msg_rx_err(dev))
4139 netdev_err(dev->net,
4140 "can't clear rx halt, status %d\n",
4143 clear_bit(EVENT_RX_HALT, &dev->flags);
4144 napi_schedule(&dev->napi);
4148 if (test_bit(EVENT_LINK_RESET, &dev->flags)) {
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",
4158 if (test_bit(EVENT_STAT_UPDATE, &dev->flags)) {
4159 lan78xx_update_stats(dev);
4161 clear_bit(EVENT_STAT_UPDATE, &dev->flags);
4163 mod_timer(&dev->stat_monitor,
4164 jiffies + (STAT_UPDATE_TIMER * dev->delta));
4166 dev->delta = min((dev->delta * 2), 50);
4169 usb_autopm_put_interface(dev->intf);
4172 static void intr_complete(struct urb *urb)
4174 struct lan78xx_net *dev = urb->context;
4175 int status = urb->status;
4180 lan78xx_status(dev, urb);
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);
4191 /* NOTE: not throttling like RX/TX, since this endpoint
4192 * already polls infrequently
4195 netdev_dbg(dev->net, "intr status %d\n", status);
4199 if (!netif_device_present(dev->net) ||
4200 !netif_running(dev->net)) {
4201 netdev_warn(dev->net, "not submitting new status URB");
4205 memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
4206 status = usb_submit_urb(urb, GFP_ATOMIC);
4213 netif_dbg(dev, timer, dev->net,
4214 "intr resubmit %d (disconnect?)", status);
4215 netif_device_detach(dev->net);
4218 netif_err(dev, timer, dev->net,
4219 "intr resubmit --> %d\n", status);
4224 static void lan78xx_disconnect(struct usb_interface *intf)
4226 struct lan78xx_net *dev;
4227 struct usb_device *udev;
4228 struct net_device *net;
4229 struct phy_device *phydev;
4231 dev = usb_get_intfdata(intf);
4232 usb_set_intfdata(intf, NULL);
4236 set_bit(EVENT_DEV_DISCONNECT, &dev->flags);
4238 netif_napi_del(&dev->napi);
4240 udev = interface_to_usbdev(intf);
4243 unregister_netdev(net);
4245 cancel_delayed_work_sync(&dev->wq);
4247 phydev = net->phydev;
4249 phy_unregister_fixup_for_uid(PHY_KSZ9031RNX, 0xfffffff0);
4250 phy_unregister_fixup_for_uid(PHY_LAN8835, 0xfffffff0);
4252 phy_disconnect(net->phydev);
4254 if (phy_is_pseudo_fixed_link(phydev))
4255 fixed_phy_unregister(phydev);
4257 usb_scuttle_anchored_urbs(&dev->deferred);
4259 if (timer_pending(&dev->stat_monitor))
4260 del_timer_sync(&dev->stat_monitor);
4262 lan78xx_unbind(dev, intf);
4264 lan78xx_free_tx_resources(dev);
4265 lan78xx_free_rx_resources(dev);
4267 usb_kill_urb(dev->urb_intr);
4268 usb_free_urb(dev->urb_intr);
4274 static void lan78xx_tx_timeout(struct net_device *net, unsigned int txqueue)
4276 struct lan78xx_net *dev = netdev_priv(net);
4278 unlink_urbs(dev, &dev->txq);
4279 napi_schedule(&dev->napi);
4282 static netdev_features_t lan78xx_features_check(struct sk_buff *skb,
4283 struct net_device *netdev,
4284 netdev_features_t features)
4286 struct lan78xx_net *dev = netdev_priv(netdev);
4288 if (skb->len > LAN78XX_TSO_SIZE(dev))
4289 features &= ~NETIF_F_GSO_MASK;
4291 features = vlan_features_check(skb, features);
4292 features = vxlan_features_check(skb, features);
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,
4313 static void lan78xx_stat_monitor(struct timer_list *t)
4315 struct lan78xx_net *dev = from_timer(dev, t, stat_monitor);
4317 lan78xx_defer_kevent(dev, EVENT_STAT_UPDATE);
4320 static int lan78xx_probe(struct usb_interface *intf,
4321 const struct usb_device_id *id)
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;
4329 unsigned int period;
4332 udev = interface_to_usbdev(intf);
4333 udev = usb_get_dev(udev);
4335 netdev = alloc_etherdev(sizeof(struct lan78xx_net));
4337 dev_err(&intf->dev, "Error: OOM\n");
4342 /* netdev_printk() needs this */
4343 SET_NETDEV_DEV(netdev, &intf->dev);
4345 dev = netdev_priv(netdev);
4349 dev->msg_enable = netif_msg_init(msg_level, NETIF_MSG_DRV
4350 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
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);
4360 ret = lan78xx_urb_config_init(dev);
4364 ret = lan78xx_alloc_tx_resources(dev);
4368 ret = lan78xx_alloc_rx_resources(dev);
4372 /* MTU range: 68 - 9000 */
4373 netdev->max_mtu = MAX_SINGLE_PACKET_SIZE;
4375 netif_set_tso_max_size(netdev, LAN78XX_TSO_SIZE(dev));
4377 netif_napi_add(netdev, &dev->napi, lan78xx_poll);
4379 INIT_DELAYED_WORK(&dev->wq, lan78xx_delayedwork);
4380 init_usb_anchor(&dev->deferred);
4382 netdev->netdev_ops = &lan78xx_netdev_ops;
4383 netdev->watchdog_timeo = TX_TIMEOUT_JIFFIES;
4384 netdev->ethtool_ops = &lan78xx_ethtool_ops;
4387 timer_setup(&dev->stat_monitor, lan78xx_stat_monitor, 0);
4389 mutex_init(&dev->stats.access_lock);
4391 if (intf->cur_altsetting->desc.bNumEndpoints < 3) {
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)) {
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)) {
4410 ep_intr = &intf->cur_altsetting->endpoint[2];
4411 if (!usb_endpoint_is_int_in(&ep_intr->desc)) {
4416 dev->pipe_intr = usb_rcvintpipe(dev->udev,
4417 usb_endpoint_num(&ep_intr->desc));
4419 ret = lan78xx_bind(dev, intf);
4423 period = ep_intr->desc.bInterval;
4424 maxp = usb_maxpacket(dev->udev, dev->pipe_intr);
4425 buf = kmalloc(maxp, GFP_KERNEL);
4431 dev->urb_intr = usb_alloc_urb(0, GFP_KERNEL);
4432 if (!dev->urb_intr) {
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;
4442 dev->maxpacket = usb_maxpacket(dev->udev, dev->pipe_out);
4444 /* Reject broken descriptors. */
4445 if (dev->maxpacket == 0) {
4450 /* driver requires remote-wakeup capability during autosuspend. */
4451 intf->needs_remote_wakeup = 1;
4453 ret = lan78xx_phy_init(dev);
4457 ret = register_netdev(netdev);
4459 netif_err(dev, probe, netdev, "couldn't register the device\n");
4463 usb_set_intfdata(intf, dev);
4465 ret = device_set_wakeup_enable(&udev->dev, true);
4467 /* Default delay of 2sec has more overhead than advantage.
4468 * Set to 10sec as default.
4470 pm_runtime_set_autosuspend_delay(&udev->dev,
4471 DEFAULT_AUTOSUSPEND_DELAY);
4476 phy_disconnect(netdev->phydev);
4478 usb_free_urb(dev->urb_intr);
4482 lan78xx_unbind(dev, intf);
4484 netif_napi_del(&dev->napi);
4485 lan78xx_free_rx_resources(dev);
4487 lan78xx_free_tx_resources(dev);
4489 free_netdev(netdev);
4496 static u16 lan78xx_wakeframe_crc16(const u8 *buf, int len)
4498 const u16 crc16poly = 0x8005;
4504 for (i = 0; i < len; i++) {
4506 for (bit = 0; bit < 8; bit++) {
4510 if (msb ^ (u16)(data & 1)) {
4512 crc |= (u16)0x0001U;
4521 static int lan78xx_set_auto_suspend(struct lan78xx_net *dev)
4526 ret = lan78xx_stop_tx_path(dev);
4530 ret = lan78xx_stop_rx_path(dev);
4534 /* auto suspend (selective suspend) */
4536 ret = lan78xx_write_reg(dev, WUCSR, 0);
4539 ret = lan78xx_write_reg(dev, WUCSR2, 0);
4542 ret = lan78xx_write_reg(dev, WK_SRC, 0xFFF1FF1FUL);
4546 /* set goodframe wakeup */
4548 ret = lan78xx_read_reg(dev, WUCSR, &buf);
4552 buf |= WUCSR_RFE_WAKE_EN_;
4553 buf |= WUCSR_STORE_WAKE_;
4555 ret = lan78xx_write_reg(dev, WUCSR, buf);
4559 ret = lan78xx_read_reg(dev, PMT_CTL, &buf);
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_;
4570 ret = lan78xx_write_reg(dev, PMT_CTL, buf);
4574 ret = lan78xx_read_reg(dev, PMT_CTL, &buf);
4578 buf |= PMT_CTL_WUPS_MASK_;
4580 ret = lan78xx_write_reg(dev, PMT_CTL, buf);
4584 ret = lan78xx_start_rx_path(dev);
4589 static int lan78xx_set_suspend(struct lan78xx_net *dev, u32 wol)
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 };
4601 ret = lan78xx_stop_tx_path(dev);
4604 ret = lan78xx_stop_rx_path(dev);
4608 ret = lan78xx_write_reg(dev, WUCSR, 0);
4611 ret = lan78xx_write_reg(dev, WUCSR2, 0);
4614 ret = lan78xx_write_reg(dev, WK_SRC, 0xFFF1FF1FUL);
4622 ret = lan78xx_read_reg(dev, PMT_CTL, &temp_pmt_ctl);
4626 temp_pmt_ctl &= ~PMT_CTL_RES_CLR_WKP_EN_;
4627 temp_pmt_ctl |= PMT_CTL_RES_CLR_WKP_STS_;
4629 for (mask_index = 0; mask_index < NUM_OF_WUF_CFG; mask_index++) {
4630 ret = lan78xx_write_reg(dev, WUF_CFG(mask_index), 0);
4636 if (wol & WAKE_PHY) {
4637 temp_pmt_ctl |= PMT_CTL_PHY_WAKE_EN_;
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_;
4643 if (wol & WAKE_MAGIC) {
4644 temp_wucsr |= WUCSR_MPEN_;
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_;
4650 if (wol & WAKE_BCAST) {
4651 temp_wucsr |= WUCSR_BCST_EN_;
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_;
4657 if (wol & WAKE_MCAST) {
4658 temp_wucsr |= WUCSR_WAKE_EN_;
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),
4664 WUF_CFGX_TYPE_MCAST_ |
4665 (0 << WUF_CFGX_OFFSET_SHIFT_) |
4666 (crc & WUF_CFGX_CRC16_MASK_));
4670 ret = lan78xx_write_reg(dev, WUF_MASK0(mask_index), 7);
4673 ret = lan78xx_write_reg(dev, WUF_MASK1(mask_index), 0);
4676 ret = lan78xx_write_reg(dev, WUF_MASK2(mask_index), 0);
4679 ret = lan78xx_write_reg(dev, WUF_MASK3(mask_index), 0);
4685 /* for IPv6 Multicast */
4686 crc = lan78xx_wakeframe_crc16(ipv6_multicast, 2);
4687 ret = lan78xx_write_reg(dev, WUF_CFG(mask_index),
4689 WUF_CFGX_TYPE_MCAST_ |
4690 (0 << WUF_CFGX_OFFSET_SHIFT_) |
4691 (crc & WUF_CFGX_CRC16_MASK_));
4695 ret = lan78xx_write_reg(dev, WUF_MASK0(mask_index), 3);
4698 ret = lan78xx_write_reg(dev, WUF_MASK1(mask_index), 0);
4701 ret = lan78xx_write_reg(dev, WUF_MASK2(mask_index), 0);
4704 ret = lan78xx_write_reg(dev, WUF_MASK3(mask_index), 0);
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_;
4714 if (wol & WAKE_UCAST) {
4715 temp_wucsr |= WUCSR_PFDA_EN_;
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_;
4721 if (wol & WAKE_ARP) {
4722 temp_wucsr |= WUCSR_WAKE_EN_;
4724 /* set WUF_CFG & WUF_MASK
4725 * for packettype (offset 12,13) = ARP (0x0806)
4727 crc = lan78xx_wakeframe_crc16(arp_type, 2);
4728 ret = lan78xx_write_reg(dev, WUF_CFG(mask_index),
4730 WUF_CFGX_TYPE_ALL_ |
4731 (0 << WUF_CFGX_OFFSET_SHIFT_) |
4732 (crc & WUF_CFGX_CRC16_MASK_));
4736 ret = lan78xx_write_reg(dev, WUF_MASK0(mask_index), 0x3000);
4739 ret = lan78xx_write_reg(dev, WUF_MASK1(mask_index), 0);
4742 ret = lan78xx_write_reg(dev, WUF_MASK2(mask_index), 0);
4745 ret = lan78xx_write_reg(dev, WUF_MASK3(mask_index), 0);
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_;
4756 ret = lan78xx_write_reg(dev, WUCSR, temp_wucsr);
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_;
4766 ret = lan78xx_write_reg(dev, PMT_CTL, temp_pmt_ctl);
4771 ret = lan78xx_read_reg(dev, PMT_CTL, &buf);
4775 buf |= PMT_CTL_WUPS_MASK_;
4777 ret = lan78xx_write_reg(dev, PMT_CTL, buf);
4781 ret = lan78xx_start_rx_path(dev);
4786 static int lan78xx_suspend(struct usb_interface *intf, pm_message_t message)
4788 struct lan78xx_net *dev = usb_get_intfdata(intf);
4792 mutex_lock(&dev->dev_mutex);
4794 netif_dbg(dev, ifdown, dev->net,
4795 "suspending: pm event %#x", message.event);
4797 dev_open = test_bit(EVENT_DEV_OPEN, &dev->flags);
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);
4809 set_bit(EVENT_DEV_ASLEEP, &dev->flags);
4810 spin_unlock_irq(&dev->txq.lock);
4814 ret = lan78xx_stop_rx_path(dev);
4818 ret = lan78xx_flush_rx_fifo(dev);
4823 ret = lan78xx_stop_tx_path(dev);
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);
4833 netif_device_attach(dev->net);
4835 del_timer(&dev->stat_monitor);
4837 if (PMSG_IS_AUTO(message)) {
4838 ret = lan78xx_set_auto_suspend(dev);
4842 struct lan78xx_priv *pdata;
4844 pdata = (struct lan78xx_priv *)(dev->data[0]);
4845 netif_carrier_off(dev->net);
4846 ret = lan78xx_set_suspend(dev, pdata->wol);
4851 /* Interface is down; don't allow WOL and PHY
4852 * events to wake up the host
4856 set_bit(EVENT_DEV_ASLEEP, &dev->flags);
4858 ret = lan78xx_write_reg(dev, WUCSR, 0);
4861 ret = lan78xx_write_reg(dev, WUCSR2, 0);
4865 ret = lan78xx_read_reg(dev, PMT_CTL, &buf);
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_;
4874 ret = lan78xx_write_reg(dev, PMT_CTL, buf);
4878 ret = lan78xx_read_reg(dev, PMT_CTL, &buf);
4882 buf |= PMT_CTL_WUPS_MASK_;
4884 ret = lan78xx_write_reg(dev, PMT_CTL, buf);
4891 mutex_unlock(&dev->dev_mutex);
4896 static bool lan78xx_submit_deferred_urbs(struct lan78xx_net *dev)
4898 bool pipe_halted = false;
4901 while ((urb = usb_get_from_anchor(&dev->deferred))) {
4902 struct sk_buff *skb = urb->context;
4905 if (!netif_device_present(dev->net) ||
4906 !netif_carrier_ok(dev->net) ||
4908 lan78xx_release_tx_buf(dev, skb);
4912 ret = usb_submit_urb(urb, GFP_ATOMIC);
4915 netif_trans_update(dev->net);
4916 lan78xx_queue_skb(&dev->txq, skb, tx_start);
4918 if (ret == -EPIPE) {
4919 netif_stop_queue(dev->net);
4921 } else if (ret == -ENODEV) {
4922 netif_device_detach(dev->net);
4925 lan78xx_release_tx_buf(dev, skb);
4932 static int lan78xx_resume(struct usb_interface *intf)
4934 struct lan78xx_net *dev = usb_get_intfdata(intf);
4938 mutex_lock(&dev->dev_mutex);
4940 netif_dbg(dev, ifup, dev->net, "resuming device");
4942 dev_open = test_bit(EVENT_DEV_OPEN, &dev->flags);
4945 bool pipe_halted = false;
4947 ret = lan78xx_flush_tx_fifo(dev);
4951 if (dev->urb_intr) {
4952 int ret = usb_submit_urb(dev->urb_intr, GFP_KERNEL);
4956 netif_device_detach(dev->net);
4957 netdev_warn(dev->net, "Failed to submit intr URB");
4961 spin_lock_irq(&dev->txq.lock);
4963 if (netif_device_present(dev->net)) {
4964 pipe_halted = lan78xx_submit_deferred_urbs(dev);
4967 lan78xx_defer_kevent(dev, EVENT_TX_HALT);
4970 clear_bit(EVENT_DEV_ASLEEP, &dev->flags);
4972 spin_unlock_irq(&dev->txq.lock);
4975 netif_device_present(dev->net) &&
4976 (lan78xx_tx_pend_data_len(dev) < lan78xx_tx_urb_space(dev)))
4977 netif_start_queue(dev->net);
4979 ret = lan78xx_start_tx_path(dev);
4983 napi_schedule(&dev->napi);
4985 if (!timer_pending(&dev->stat_monitor)) {
4987 mod_timer(&dev->stat_monitor,
4988 jiffies + STAT_UPDATE_TIMER);
4992 clear_bit(EVENT_DEV_ASLEEP, &dev->flags);
4995 ret = lan78xx_write_reg(dev, WUCSR2, 0);
4998 ret = lan78xx_write_reg(dev, WUCSR, 0);
5001 ret = lan78xx_write_reg(dev, WK_SRC, 0xFFF1FF1FUL);
5005 ret = lan78xx_write_reg(dev, WUCSR2, WUCSR2_NS_RCD_ |
5007 WUCSR2_IPV6_TCPSYN_RCD_ |
5008 WUCSR2_IPV4_TCPSYN_RCD_);
5012 ret = lan78xx_write_reg(dev, WUCSR, WUCSR_EEE_TX_WAKE_ |
5013 WUCSR_EEE_RX_WAKE_ |
5015 WUCSR_RFE_WAKE_FR_ |
5024 mutex_unlock(&dev->dev_mutex);
5029 static int lan78xx_reset_resume(struct usb_interface *intf)
5031 struct lan78xx_net *dev = usb_get_intfdata(intf);
5034 netif_dbg(dev, ifup, dev->net, "(reset) resuming device");
5036 ret = lan78xx_reset(dev);
5040 phy_start(dev->net->phydev);
5042 ret = lan78xx_resume(intf);
5047 static const struct usb_device_id products[] = {
5049 /* LAN7800 USB Gigabit Ethernet Device */
5050 USB_DEVICE(LAN78XX_USB_VENDOR_ID, LAN7800_USB_PRODUCT_ID),
5053 /* LAN7850 USB Gigabit Ethernet Device */
5054 USB_DEVICE(LAN78XX_USB_VENDOR_ID, LAN7850_USB_PRODUCT_ID),
5057 /* LAN7801 USB Gigabit Ethernet Device */
5058 USB_DEVICE(LAN78XX_USB_VENDOR_ID, LAN7801_USB_PRODUCT_ID),
5061 /* ATM2-AF USB Gigabit Ethernet Device */
5062 USB_DEVICE(AT29M2AF_USB_VENDOR_ID, AT29M2AF_USB_PRODUCT_ID),
5066 MODULE_DEVICE_TABLE(usb, products);
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,
5080 module_usb_driver(lan78xx_driver);
5082 MODULE_AUTHOR(DRIVER_AUTHOR);
5083 MODULE_DESCRIPTION(DRIVER_DESC);
5084 MODULE_LICENSE("GPL");