Linux 4.19.133
[linux/fpc-iii.git] / drivers / net / usb / lan78xx.c
blob92548887df2fe4c46e4f14c2648794456dfe3dcf
1 /*
2 * Copyright (C) 2015 Microchip Technology
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17 #include <linux/version.h>
18 #include <linux/module.h>
19 #include <linux/netdevice.h>
20 #include <linux/etherdevice.h>
21 #include <linux/ethtool.h>
22 #include <linux/usb.h>
23 #include <linux/crc32.h>
24 #include <linux/signal.h>
25 #include <linux/slab.h>
26 #include <linux/if_vlan.h>
27 #include <linux/uaccess.h>
28 #include <linux/list.h>
29 #include <linux/ip.h>
30 #include <linux/ipv6.h>
31 #include <linux/mdio.h>
32 #include <linux/phy.h>
33 #include <net/ip6_checksum.h>
34 #include <net/vxlan.h>
35 #include <linux/interrupt.h>
36 #include <linux/irqdomain.h>
37 #include <linux/irq.h>
38 #include <linux/irqchip/chained_irq.h>
39 #include <linux/microchipphy.h>
40 #include <linux/phy_fixed.h>
41 #include <linux/of_mdio.h>
42 #include <linux/of_net.h>
43 #include "lan78xx.h"
45 #define DRIVER_AUTHOR "WOOJUNG HUH <woojung.huh@microchip.com>"
46 #define DRIVER_DESC "LAN78XX USB 3.0 Gigabit Ethernet Devices"
47 #define DRIVER_NAME "lan78xx"
49 #define TX_TIMEOUT_JIFFIES (5 * HZ)
50 #define THROTTLE_JIFFIES (HZ / 8)
51 #define UNLINK_TIMEOUT_MS 3
53 #define RX_MAX_QUEUE_MEMORY (60 * 1518)
55 #define SS_USB_PKT_SIZE (1024)
56 #define HS_USB_PKT_SIZE (512)
57 #define FS_USB_PKT_SIZE (64)
59 #define MAX_RX_FIFO_SIZE (12 * 1024)
60 #define MAX_TX_FIFO_SIZE (12 * 1024)
61 #define DEFAULT_BURST_CAP_SIZE (MAX_TX_FIFO_SIZE)
62 #define DEFAULT_BULK_IN_DELAY (0x0800)
63 #define MAX_SINGLE_PACKET_SIZE (9000)
64 #define DEFAULT_TX_CSUM_ENABLE (true)
65 #define DEFAULT_RX_CSUM_ENABLE (true)
66 #define DEFAULT_TSO_CSUM_ENABLE (true)
67 #define DEFAULT_VLAN_FILTER_ENABLE (true)
68 #define DEFAULT_VLAN_RX_OFFLOAD (true)
69 #define TX_OVERHEAD (8)
70 #define RXW_PADDING 2
72 #define LAN78XX_USB_VENDOR_ID (0x0424)
73 #define LAN7800_USB_PRODUCT_ID (0x7800)
74 #define LAN7850_USB_PRODUCT_ID (0x7850)
75 #define LAN7801_USB_PRODUCT_ID (0x7801)
76 #define LAN78XX_EEPROM_MAGIC (0x78A5)
77 #define LAN78XX_OTP_MAGIC (0x78F3)
79 #define MII_READ 1
80 #define MII_WRITE 0
82 #define EEPROM_INDICATOR (0xA5)
83 #define EEPROM_MAC_OFFSET (0x01)
84 #define MAX_EEPROM_SIZE 512
85 #define OTP_INDICATOR_1 (0xF3)
86 #define OTP_INDICATOR_2 (0xF7)
88 #define WAKE_ALL (WAKE_PHY | WAKE_UCAST | \
89 WAKE_MCAST | WAKE_BCAST | \
90 WAKE_ARP | WAKE_MAGIC)
92 /* USB related defines */
93 #define BULK_IN_PIPE 1
94 #define BULK_OUT_PIPE 2
96 /* default autosuspend delay (mSec)*/
97 #define DEFAULT_AUTOSUSPEND_DELAY (10 * 1000)
99 /* statistic update interval (mSec) */
100 #define STAT_UPDATE_TIMER (1 * 1000)
102 /* defines interrupts from interrupt EP */
103 #define MAX_INT_EP (32)
104 #define INT_EP_INTEP (31)
105 #define INT_EP_OTP_WR_DONE (28)
106 #define INT_EP_EEE_TX_LPI_START (26)
107 #define INT_EP_EEE_TX_LPI_STOP (25)
108 #define INT_EP_EEE_RX_LPI (24)
109 #define INT_EP_MAC_RESET_TIMEOUT (23)
110 #define INT_EP_RDFO (22)
111 #define INT_EP_TXE (21)
112 #define INT_EP_USB_STATUS (20)
113 #define INT_EP_TX_DIS (19)
114 #define INT_EP_RX_DIS (18)
115 #define INT_EP_PHY (17)
116 #define INT_EP_DP (16)
117 #define INT_EP_MAC_ERR (15)
118 #define INT_EP_TDFU (14)
119 #define INT_EP_TDFO (13)
120 #define INT_EP_UTX (12)
121 #define INT_EP_GPIO_11 (11)
122 #define INT_EP_GPIO_10 (10)
123 #define INT_EP_GPIO_9 (9)
124 #define INT_EP_GPIO_8 (8)
125 #define INT_EP_GPIO_7 (7)
126 #define INT_EP_GPIO_6 (6)
127 #define INT_EP_GPIO_5 (5)
128 #define INT_EP_GPIO_4 (4)
129 #define INT_EP_GPIO_3 (3)
130 #define INT_EP_GPIO_2 (2)
131 #define INT_EP_GPIO_1 (1)
132 #define INT_EP_GPIO_0 (0)
134 static const char lan78xx_gstrings[][ETH_GSTRING_LEN] = {
135 "RX FCS Errors",
136 "RX Alignment Errors",
137 "Rx Fragment Errors",
138 "RX Jabber Errors",
139 "RX Undersize Frame Errors",
140 "RX Oversize Frame Errors",
141 "RX Dropped Frames",
142 "RX Unicast Byte Count",
143 "RX Broadcast Byte Count",
144 "RX Multicast Byte Count",
145 "RX Unicast Frames",
146 "RX Broadcast Frames",
147 "RX Multicast Frames",
148 "RX Pause Frames",
149 "RX 64 Byte Frames",
150 "RX 65 - 127 Byte Frames",
151 "RX 128 - 255 Byte Frames",
152 "RX 256 - 511 Bytes Frames",
153 "RX 512 - 1023 Byte Frames",
154 "RX 1024 - 1518 Byte Frames",
155 "RX Greater 1518 Byte Frames",
156 "EEE RX LPI Transitions",
157 "EEE RX LPI Time",
158 "TX FCS Errors",
159 "TX Excess Deferral Errors",
160 "TX Carrier Errors",
161 "TX Bad Byte Count",
162 "TX Single Collisions",
163 "TX Multiple Collisions",
164 "TX Excessive Collision",
165 "TX Late Collisions",
166 "TX Unicast Byte Count",
167 "TX Broadcast Byte Count",
168 "TX Multicast Byte Count",
169 "TX Unicast Frames",
170 "TX Broadcast Frames",
171 "TX Multicast Frames",
172 "TX Pause Frames",
173 "TX 64 Byte Frames",
174 "TX 65 - 127 Byte Frames",
175 "TX 128 - 255 Byte Frames",
176 "TX 256 - 511 Bytes Frames",
177 "TX 512 - 1023 Byte Frames",
178 "TX 1024 - 1518 Byte Frames",
179 "TX Greater 1518 Byte Frames",
180 "EEE TX LPI Transitions",
181 "EEE TX LPI Time",
184 struct lan78xx_statstage {
185 u32 rx_fcs_errors;
186 u32 rx_alignment_errors;
187 u32 rx_fragment_errors;
188 u32 rx_jabber_errors;
189 u32 rx_undersize_frame_errors;
190 u32 rx_oversize_frame_errors;
191 u32 rx_dropped_frames;
192 u32 rx_unicast_byte_count;
193 u32 rx_broadcast_byte_count;
194 u32 rx_multicast_byte_count;
195 u32 rx_unicast_frames;
196 u32 rx_broadcast_frames;
197 u32 rx_multicast_frames;
198 u32 rx_pause_frames;
199 u32 rx_64_byte_frames;
200 u32 rx_65_127_byte_frames;
201 u32 rx_128_255_byte_frames;
202 u32 rx_256_511_bytes_frames;
203 u32 rx_512_1023_byte_frames;
204 u32 rx_1024_1518_byte_frames;
205 u32 rx_greater_1518_byte_frames;
206 u32 eee_rx_lpi_transitions;
207 u32 eee_rx_lpi_time;
208 u32 tx_fcs_errors;
209 u32 tx_excess_deferral_errors;
210 u32 tx_carrier_errors;
211 u32 tx_bad_byte_count;
212 u32 tx_single_collisions;
213 u32 tx_multiple_collisions;
214 u32 tx_excessive_collision;
215 u32 tx_late_collisions;
216 u32 tx_unicast_byte_count;
217 u32 tx_broadcast_byte_count;
218 u32 tx_multicast_byte_count;
219 u32 tx_unicast_frames;
220 u32 tx_broadcast_frames;
221 u32 tx_multicast_frames;
222 u32 tx_pause_frames;
223 u32 tx_64_byte_frames;
224 u32 tx_65_127_byte_frames;
225 u32 tx_128_255_byte_frames;
226 u32 tx_256_511_bytes_frames;
227 u32 tx_512_1023_byte_frames;
228 u32 tx_1024_1518_byte_frames;
229 u32 tx_greater_1518_byte_frames;
230 u32 eee_tx_lpi_transitions;
231 u32 eee_tx_lpi_time;
234 struct lan78xx_statstage64 {
235 u64 rx_fcs_errors;
236 u64 rx_alignment_errors;
237 u64 rx_fragment_errors;
238 u64 rx_jabber_errors;
239 u64 rx_undersize_frame_errors;
240 u64 rx_oversize_frame_errors;
241 u64 rx_dropped_frames;
242 u64 rx_unicast_byte_count;
243 u64 rx_broadcast_byte_count;
244 u64 rx_multicast_byte_count;
245 u64 rx_unicast_frames;
246 u64 rx_broadcast_frames;
247 u64 rx_multicast_frames;
248 u64 rx_pause_frames;
249 u64 rx_64_byte_frames;
250 u64 rx_65_127_byte_frames;
251 u64 rx_128_255_byte_frames;
252 u64 rx_256_511_bytes_frames;
253 u64 rx_512_1023_byte_frames;
254 u64 rx_1024_1518_byte_frames;
255 u64 rx_greater_1518_byte_frames;
256 u64 eee_rx_lpi_transitions;
257 u64 eee_rx_lpi_time;
258 u64 tx_fcs_errors;
259 u64 tx_excess_deferral_errors;
260 u64 tx_carrier_errors;
261 u64 tx_bad_byte_count;
262 u64 tx_single_collisions;
263 u64 tx_multiple_collisions;
264 u64 tx_excessive_collision;
265 u64 tx_late_collisions;
266 u64 tx_unicast_byte_count;
267 u64 tx_broadcast_byte_count;
268 u64 tx_multicast_byte_count;
269 u64 tx_unicast_frames;
270 u64 tx_broadcast_frames;
271 u64 tx_multicast_frames;
272 u64 tx_pause_frames;
273 u64 tx_64_byte_frames;
274 u64 tx_65_127_byte_frames;
275 u64 tx_128_255_byte_frames;
276 u64 tx_256_511_bytes_frames;
277 u64 tx_512_1023_byte_frames;
278 u64 tx_1024_1518_byte_frames;
279 u64 tx_greater_1518_byte_frames;
280 u64 eee_tx_lpi_transitions;
281 u64 eee_tx_lpi_time;
284 static u32 lan78xx_regs[] = {
285 ID_REV,
286 INT_STS,
287 HW_CFG,
288 PMT_CTL,
289 E2P_CMD,
290 E2P_DATA,
291 USB_STATUS,
292 VLAN_TYPE,
293 MAC_CR,
294 MAC_RX,
295 MAC_TX,
296 FLOW,
297 ERR_STS,
298 MII_ACC,
299 MII_DATA,
300 EEE_TX_LPI_REQ_DLY,
301 EEE_TW_TX_SYS,
302 EEE_TX_LPI_REM_DLY,
303 WUCSR
306 #define PHY_REG_SIZE (32 * sizeof(u32))
308 struct lan78xx_net;
310 struct lan78xx_priv {
311 struct lan78xx_net *dev;
312 u32 rfe_ctl;
313 u32 mchash_table[DP_SEL_VHF_HASH_LEN]; /* multicat hash table */
314 u32 pfilter_table[NUM_OF_MAF][2]; /* perfect filter table */
315 u32 vlan_table[DP_SEL_VHF_VLAN_LEN];
316 struct mutex dataport_mutex; /* for dataport access */
317 spinlock_t rfe_ctl_lock; /* for rfe register access */
318 struct work_struct set_multicast;
319 struct work_struct set_vlan;
320 u32 wol;
323 enum skb_state {
324 illegal = 0,
325 tx_start,
326 tx_done,
327 rx_start,
328 rx_done,
329 rx_cleanup,
330 unlink_start
333 struct skb_data { /* skb->cb is one of these */
334 struct urb *urb;
335 struct lan78xx_net *dev;
336 enum skb_state state;
337 size_t length;
338 int num_of_packet;
341 struct usb_context {
342 struct usb_ctrlrequest req;
343 struct lan78xx_net *dev;
346 #define EVENT_TX_HALT 0
347 #define EVENT_RX_HALT 1
348 #define EVENT_RX_MEMORY 2
349 #define EVENT_STS_SPLIT 3
350 #define EVENT_LINK_RESET 4
351 #define EVENT_RX_PAUSED 5
352 #define EVENT_DEV_WAKING 6
353 #define EVENT_DEV_ASLEEP 7
354 #define EVENT_DEV_OPEN 8
355 #define EVENT_STAT_UPDATE 9
357 struct statstage {
358 struct mutex access_lock; /* for stats access */
359 struct lan78xx_statstage saved;
360 struct lan78xx_statstage rollover_count;
361 struct lan78xx_statstage rollover_max;
362 struct lan78xx_statstage64 curr_stat;
365 struct irq_domain_data {
366 struct irq_domain *irqdomain;
367 unsigned int phyirq;
368 struct irq_chip *irqchip;
369 irq_flow_handler_t irq_handler;
370 u32 irqenable;
371 struct mutex irq_lock; /* for irq bus access */
374 struct lan78xx_net {
375 struct net_device *net;
376 struct usb_device *udev;
377 struct usb_interface *intf;
378 void *driver_priv;
380 int rx_qlen;
381 int tx_qlen;
382 struct sk_buff_head rxq;
383 struct sk_buff_head txq;
384 struct sk_buff_head done;
385 struct sk_buff_head rxq_pause;
386 struct sk_buff_head txq_pend;
388 struct tasklet_struct bh;
389 struct delayed_work wq;
391 struct usb_host_endpoint *ep_blkin;
392 struct usb_host_endpoint *ep_blkout;
393 struct usb_host_endpoint *ep_intr;
395 int msg_enable;
397 struct urb *urb_intr;
398 struct usb_anchor deferred;
400 struct mutex phy_mutex; /* for phy access */
401 unsigned pipe_in, pipe_out, pipe_intr;
403 u32 hard_mtu; /* count any extra framing */
404 size_t rx_urb_size; /* size for rx urbs */
406 unsigned long flags;
408 wait_queue_head_t *wait;
409 unsigned char suspend_count;
411 unsigned maxpacket;
412 struct timer_list delay;
413 struct timer_list stat_monitor;
415 unsigned long data[5];
417 int link_on;
418 u8 mdix_ctrl;
420 u32 chipid;
421 u32 chiprev;
422 struct mii_bus *mdiobus;
423 phy_interface_t interface;
425 int fc_autoneg;
426 u8 fc_request_control;
428 int delta;
429 struct statstage stats;
431 struct irq_domain_data domain_data;
434 /* define external phy id */
435 #define PHY_LAN8835 (0x0007C130)
436 #define PHY_KSZ9031RNX (0x00221620)
438 /* use ethtool to change the level for any given device */
439 static int msg_level = -1;
440 module_param(msg_level, int, 0);
441 MODULE_PARM_DESC(msg_level, "Override default message level");
443 static int lan78xx_read_reg(struct lan78xx_net *dev, u32 index, u32 *data)
445 u32 *buf = kmalloc(sizeof(u32), GFP_KERNEL);
446 int ret;
448 if (!buf)
449 return -ENOMEM;
451 ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
452 USB_VENDOR_REQUEST_READ_REGISTER,
453 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
454 0, index, buf, 4, USB_CTRL_GET_TIMEOUT);
455 if (likely(ret >= 0)) {
456 le32_to_cpus(buf);
457 *data = *buf;
458 } else {
459 netdev_warn(dev->net,
460 "Failed to read register index 0x%08x. ret = %d",
461 index, ret);
464 kfree(buf);
466 return ret;
469 static int lan78xx_write_reg(struct lan78xx_net *dev, u32 index, u32 data)
471 u32 *buf = kmalloc(sizeof(u32), GFP_KERNEL);
472 int ret;
474 if (!buf)
475 return -ENOMEM;
477 *buf = data;
478 cpu_to_le32s(buf);
480 ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
481 USB_VENDOR_REQUEST_WRITE_REGISTER,
482 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
483 0, index, buf, 4, USB_CTRL_SET_TIMEOUT);
484 if (unlikely(ret < 0)) {
485 netdev_warn(dev->net,
486 "Failed to write register index 0x%08x. ret = %d",
487 index, ret);
490 kfree(buf);
492 return ret;
495 static int lan78xx_read_stats(struct lan78xx_net *dev,
496 struct lan78xx_statstage *data)
498 int ret = 0;
499 int i;
500 struct lan78xx_statstage *stats;
501 u32 *src;
502 u32 *dst;
504 stats = kmalloc(sizeof(*stats), GFP_KERNEL);
505 if (!stats)
506 return -ENOMEM;
508 ret = usb_control_msg(dev->udev,
509 usb_rcvctrlpipe(dev->udev, 0),
510 USB_VENDOR_REQUEST_GET_STATS,
511 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
514 (void *)stats,
515 sizeof(*stats),
516 USB_CTRL_SET_TIMEOUT);
517 if (likely(ret >= 0)) {
518 src = (u32 *)stats;
519 dst = (u32 *)data;
520 for (i = 0; i < sizeof(*stats)/sizeof(u32); i++) {
521 le32_to_cpus(&src[i]);
522 dst[i] = src[i];
524 } else {
525 netdev_warn(dev->net,
526 "Failed to read stat ret = %d", ret);
529 kfree(stats);
531 return ret;
534 #define check_counter_rollover(struct1, dev_stats, member) { \
535 if (struct1->member < dev_stats.saved.member) \
536 dev_stats.rollover_count.member++; \
539 static void lan78xx_check_stat_rollover(struct lan78xx_net *dev,
540 struct lan78xx_statstage *stats)
542 check_counter_rollover(stats, dev->stats, rx_fcs_errors);
543 check_counter_rollover(stats, dev->stats, rx_alignment_errors);
544 check_counter_rollover(stats, dev->stats, rx_fragment_errors);
545 check_counter_rollover(stats, dev->stats, rx_jabber_errors);
546 check_counter_rollover(stats, dev->stats, rx_undersize_frame_errors);
547 check_counter_rollover(stats, dev->stats, rx_oversize_frame_errors);
548 check_counter_rollover(stats, dev->stats, rx_dropped_frames);
549 check_counter_rollover(stats, dev->stats, rx_unicast_byte_count);
550 check_counter_rollover(stats, dev->stats, rx_broadcast_byte_count);
551 check_counter_rollover(stats, dev->stats, rx_multicast_byte_count);
552 check_counter_rollover(stats, dev->stats, rx_unicast_frames);
553 check_counter_rollover(stats, dev->stats, rx_broadcast_frames);
554 check_counter_rollover(stats, dev->stats, rx_multicast_frames);
555 check_counter_rollover(stats, dev->stats, rx_pause_frames);
556 check_counter_rollover(stats, dev->stats, rx_64_byte_frames);
557 check_counter_rollover(stats, dev->stats, rx_65_127_byte_frames);
558 check_counter_rollover(stats, dev->stats, rx_128_255_byte_frames);
559 check_counter_rollover(stats, dev->stats, rx_256_511_bytes_frames);
560 check_counter_rollover(stats, dev->stats, rx_512_1023_byte_frames);
561 check_counter_rollover(stats, dev->stats, rx_1024_1518_byte_frames);
562 check_counter_rollover(stats, dev->stats, rx_greater_1518_byte_frames);
563 check_counter_rollover(stats, dev->stats, eee_rx_lpi_transitions);
564 check_counter_rollover(stats, dev->stats, eee_rx_lpi_time);
565 check_counter_rollover(stats, dev->stats, tx_fcs_errors);
566 check_counter_rollover(stats, dev->stats, tx_excess_deferral_errors);
567 check_counter_rollover(stats, dev->stats, tx_carrier_errors);
568 check_counter_rollover(stats, dev->stats, tx_bad_byte_count);
569 check_counter_rollover(stats, dev->stats, tx_single_collisions);
570 check_counter_rollover(stats, dev->stats, tx_multiple_collisions);
571 check_counter_rollover(stats, dev->stats, tx_excessive_collision);
572 check_counter_rollover(stats, dev->stats, tx_late_collisions);
573 check_counter_rollover(stats, dev->stats, tx_unicast_byte_count);
574 check_counter_rollover(stats, dev->stats, tx_broadcast_byte_count);
575 check_counter_rollover(stats, dev->stats, tx_multicast_byte_count);
576 check_counter_rollover(stats, dev->stats, tx_unicast_frames);
577 check_counter_rollover(stats, dev->stats, tx_broadcast_frames);
578 check_counter_rollover(stats, dev->stats, tx_multicast_frames);
579 check_counter_rollover(stats, dev->stats, tx_pause_frames);
580 check_counter_rollover(stats, dev->stats, tx_64_byte_frames);
581 check_counter_rollover(stats, dev->stats, tx_65_127_byte_frames);
582 check_counter_rollover(stats, dev->stats, tx_128_255_byte_frames);
583 check_counter_rollover(stats, dev->stats, tx_256_511_bytes_frames);
584 check_counter_rollover(stats, dev->stats, tx_512_1023_byte_frames);
585 check_counter_rollover(stats, dev->stats, tx_1024_1518_byte_frames);
586 check_counter_rollover(stats, dev->stats, tx_greater_1518_byte_frames);
587 check_counter_rollover(stats, dev->stats, eee_tx_lpi_transitions);
588 check_counter_rollover(stats, dev->stats, eee_tx_lpi_time);
590 memcpy(&dev->stats.saved, stats, sizeof(struct lan78xx_statstage));
593 static void lan78xx_update_stats(struct lan78xx_net *dev)
595 u32 *p, *count, *max;
596 u64 *data;
597 int i;
598 struct lan78xx_statstage lan78xx_stats;
600 if (usb_autopm_get_interface(dev->intf) < 0)
601 return;
603 p = (u32 *)&lan78xx_stats;
604 count = (u32 *)&dev->stats.rollover_count;
605 max = (u32 *)&dev->stats.rollover_max;
606 data = (u64 *)&dev->stats.curr_stat;
608 mutex_lock(&dev->stats.access_lock);
610 if (lan78xx_read_stats(dev, &lan78xx_stats) > 0)
611 lan78xx_check_stat_rollover(dev, &lan78xx_stats);
613 for (i = 0; i < (sizeof(lan78xx_stats) / (sizeof(u32))); i++)
614 data[i] = (u64)p[i] + ((u64)count[i] * ((u64)max[i] + 1));
616 mutex_unlock(&dev->stats.access_lock);
618 usb_autopm_put_interface(dev->intf);
621 /* Loop until the read is completed with timeout called with phy_mutex held */
622 static int lan78xx_phy_wait_not_busy(struct lan78xx_net *dev)
624 unsigned long start_time = jiffies;
625 u32 val;
626 int ret;
628 do {
629 ret = lan78xx_read_reg(dev, MII_ACC, &val);
630 if (unlikely(ret < 0))
631 return -EIO;
633 if (!(val & MII_ACC_MII_BUSY_))
634 return 0;
635 } while (!time_after(jiffies, start_time + HZ));
637 return -EIO;
640 static inline u32 mii_access(int id, int index, int read)
642 u32 ret;
644 ret = ((u32)id << MII_ACC_PHY_ADDR_SHIFT_) & MII_ACC_PHY_ADDR_MASK_;
645 ret |= ((u32)index << MII_ACC_MIIRINDA_SHIFT_) & MII_ACC_MIIRINDA_MASK_;
646 if (read)
647 ret |= MII_ACC_MII_READ_;
648 else
649 ret |= MII_ACC_MII_WRITE_;
650 ret |= MII_ACC_MII_BUSY_;
652 return ret;
655 static int lan78xx_wait_eeprom(struct lan78xx_net *dev)
657 unsigned long start_time = jiffies;
658 u32 val;
659 int ret;
661 do {
662 ret = lan78xx_read_reg(dev, E2P_CMD, &val);
663 if (unlikely(ret < 0))
664 return -EIO;
666 if (!(val & E2P_CMD_EPC_BUSY_) ||
667 (val & E2P_CMD_EPC_TIMEOUT_))
668 break;
669 usleep_range(40, 100);
670 } while (!time_after(jiffies, start_time + HZ));
672 if (val & (E2P_CMD_EPC_TIMEOUT_ | E2P_CMD_EPC_BUSY_)) {
673 netdev_warn(dev->net, "EEPROM read operation timeout");
674 return -EIO;
677 return 0;
680 static int lan78xx_eeprom_confirm_not_busy(struct lan78xx_net *dev)
682 unsigned long start_time = jiffies;
683 u32 val;
684 int ret;
686 do {
687 ret = lan78xx_read_reg(dev, E2P_CMD, &val);
688 if (unlikely(ret < 0))
689 return -EIO;
691 if (!(val & E2P_CMD_EPC_BUSY_))
692 return 0;
694 usleep_range(40, 100);
695 } while (!time_after(jiffies, start_time + HZ));
697 netdev_warn(dev->net, "EEPROM is busy");
698 return -EIO;
701 static int lan78xx_read_raw_eeprom(struct lan78xx_net *dev, u32 offset,
702 u32 length, u8 *data)
704 u32 val;
705 u32 saved;
706 int i, ret;
707 int retval;
709 /* depends on chip, some EEPROM pins are muxed with LED function.
710 * disable & restore LED function to access EEPROM.
712 ret = lan78xx_read_reg(dev, HW_CFG, &val);
713 saved = val;
714 if (dev->chipid == ID_REV_CHIP_ID_7800_) {
715 val &= ~(HW_CFG_LED1_EN_ | HW_CFG_LED0_EN_);
716 ret = lan78xx_write_reg(dev, HW_CFG, val);
719 retval = lan78xx_eeprom_confirm_not_busy(dev);
720 if (retval)
721 return retval;
723 for (i = 0; i < length; i++) {
724 val = E2P_CMD_EPC_BUSY_ | E2P_CMD_EPC_CMD_READ_;
725 val |= (offset & E2P_CMD_EPC_ADDR_MASK_);
726 ret = lan78xx_write_reg(dev, E2P_CMD, val);
727 if (unlikely(ret < 0)) {
728 retval = -EIO;
729 goto exit;
732 retval = lan78xx_wait_eeprom(dev);
733 if (retval < 0)
734 goto exit;
736 ret = lan78xx_read_reg(dev, E2P_DATA, &val);
737 if (unlikely(ret < 0)) {
738 retval = -EIO;
739 goto exit;
742 data[i] = val & 0xFF;
743 offset++;
746 retval = 0;
747 exit:
748 if (dev->chipid == ID_REV_CHIP_ID_7800_)
749 ret = lan78xx_write_reg(dev, HW_CFG, saved);
751 return retval;
754 static int lan78xx_read_eeprom(struct lan78xx_net *dev, u32 offset,
755 u32 length, u8 *data)
757 u8 sig;
758 int ret;
760 ret = lan78xx_read_raw_eeprom(dev, 0, 1, &sig);
761 if ((ret == 0) && (sig == EEPROM_INDICATOR))
762 ret = lan78xx_read_raw_eeprom(dev, offset, length, data);
763 else
764 ret = -EINVAL;
766 return ret;
769 static int lan78xx_write_raw_eeprom(struct lan78xx_net *dev, u32 offset,
770 u32 length, u8 *data)
772 u32 val;
773 u32 saved;
774 int i, ret;
775 int retval;
777 /* depends on chip, some EEPROM pins are muxed with LED function.
778 * disable & restore LED function to access EEPROM.
780 ret = lan78xx_read_reg(dev, HW_CFG, &val);
781 saved = val;
782 if (dev->chipid == ID_REV_CHIP_ID_7800_) {
783 val &= ~(HW_CFG_LED1_EN_ | HW_CFG_LED0_EN_);
784 ret = lan78xx_write_reg(dev, HW_CFG, val);
787 retval = lan78xx_eeprom_confirm_not_busy(dev);
788 if (retval)
789 goto exit;
791 /* Issue write/erase enable command */
792 val = E2P_CMD_EPC_BUSY_ | E2P_CMD_EPC_CMD_EWEN_;
793 ret = lan78xx_write_reg(dev, E2P_CMD, val);
794 if (unlikely(ret < 0)) {
795 retval = -EIO;
796 goto exit;
799 retval = lan78xx_wait_eeprom(dev);
800 if (retval < 0)
801 goto exit;
803 for (i = 0; i < length; i++) {
804 /* Fill data register */
805 val = data[i];
806 ret = lan78xx_write_reg(dev, E2P_DATA, val);
807 if (ret < 0) {
808 retval = -EIO;
809 goto exit;
812 /* Send "write" command */
813 val = E2P_CMD_EPC_BUSY_ | E2P_CMD_EPC_CMD_WRITE_;
814 val |= (offset & E2P_CMD_EPC_ADDR_MASK_);
815 ret = lan78xx_write_reg(dev, E2P_CMD, val);
816 if (ret < 0) {
817 retval = -EIO;
818 goto exit;
821 retval = lan78xx_wait_eeprom(dev);
822 if (retval < 0)
823 goto exit;
825 offset++;
828 retval = 0;
829 exit:
830 if (dev->chipid == ID_REV_CHIP_ID_7800_)
831 ret = lan78xx_write_reg(dev, HW_CFG, saved);
833 return retval;
836 static int lan78xx_read_raw_otp(struct lan78xx_net *dev, u32 offset,
837 u32 length, u8 *data)
839 int i;
840 int ret;
841 u32 buf;
842 unsigned long timeout;
844 ret = lan78xx_read_reg(dev, OTP_PWR_DN, &buf);
846 if (buf & OTP_PWR_DN_PWRDN_N_) {
847 /* clear it and wait to be cleared */
848 ret = lan78xx_write_reg(dev, OTP_PWR_DN, 0);
850 timeout = jiffies + HZ;
851 do {
852 usleep_range(1, 10);
853 ret = lan78xx_read_reg(dev, OTP_PWR_DN, &buf);
854 if (time_after(jiffies, timeout)) {
855 netdev_warn(dev->net,
856 "timeout on OTP_PWR_DN");
857 return -EIO;
859 } while (buf & OTP_PWR_DN_PWRDN_N_);
862 for (i = 0; i < length; i++) {
863 ret = lan78xx_write_reg(dev, OTP_ADDR1,
864 ((offset + i) >> 8) & OTP_ADDR1_15_11);
865 ret = lan78xx_write_reg(dev, OTP_ADDR2,
866 ((offset + i) & OTP_ADDR2_10_3));
868 ret = lan78xx_write_reg(dev, OTP_FUNC_CMD, OTP_FUNC_CMD_READ_);
869 ret = lan78xx_write_reg(dev, OTP_CMD_GO, OTP_CMD_GO_GO_);
871 timeout = jiffies + HZ;
872 do {
873 udelay(1);
874 ret = lan78xx_read_reg(dev, OTP_STATUS, &buf);
875 if (time_after(jiffies, timeout)) {
876 netdev_warn(dev->net,
877 "timeout on OTP_STATUS");
878 return -EIO;
880 } while (buf & OTP_STATUS_BUSY_);
882 ret = lan78xx_read_reg(dev, OTP_RD_DATA, &buf);
884 data[i] = (u8)(buf & 0xFF);
887 return 0;
890 static int lan78xx_write_raw_otp(struct lan78xx_net *dev, u32 offset,
891 u32 length, u8 *data)
893 int i;
894 int ret;
895 u32 buf;
896 unsigned long timeout;
898 ret = lan78xx_read_reg(dev, OTP_PWR_DN, &buf);
900 if (buf & OTP_PWR_DN_PWRDN_N_) {
901 /* clear it and wait to be cleared */
902 ret = lan78xx_write_reg(dev, OTP_PWR_DN, 0);
904 timeout = jiffies + HZ;
905 do {
906 udelay(1);
907 ret = lan78xx_read_reg(dev, OTP_PWR_DN, &buf);
908 if (time_after(jiffies, timeout)) {
909 netdev_warn(dev->net,
910 "timeout on OTP_PWR_DN completion");
911 return -EIO;
913 } while (buf & OTP_PWR_DN_PWRDN_N_);
916 /* set to BYTE program mode */
917 ret = lan78xx_write_reg(dev, OTP_PRGM_MODE, OTP_PRGM_MODE_BYTE_);
919 for (i = 0; i < length; i++) {
920 ret = lan78xx_write_reg(dev, OTP_ADDR1,
921 ((offset + i) >> 8) & OTP_ADDR1_15_11);
922 ret = lan78xx_write_reg(dev, OTP_ADDR2,
923 ((offset + i) & OTP_ADDR2_10_3));
924 ret = lan78xx_write_reg(dev, OTP_PRGM_DATA, data[i]);
925 ret = lan78xx_write_reg(dev, OTP_TST_CMD, OTP_TST_CMD_PRGVRFY_);
926 ret = lan78xx_write_reg(dev, OTP_CMD_GO, OTP_CMD_GO_GO_);
928 timeout = jiffies + HZ;
929 do {
930 udelay(1);
931 ret = lan78xx_read_reg(dev, OTP_STATUS, &buf);
932 if (time_after(jiffies, timeout)) {
933 netdev_warn(dev->net,
934 "Timeout on OTP_STATUS completion");
935 return -EIO;
937 } while (buf & OTP_STATUS_BUSY_);
940 return 0;
943 static int lan78xx_read_otp(struct lan78xx_net *dev, u32 offset,
944 u32 length, u8 *data)
946 u8 sig;
947 int ret;
949 ret = lan78xx_read_raw_otp(dev, 0, 1, &sig);
951 if (ret == 0) {
952 if (sig == OTP_INDICATOR_1)
953 offset = offset;
954 else if (sig == OTP_INDICATOR_2)
955 offset += 0x100;
956 else
957 ret = -EINVAL;
958 if (!ret)
959 ret = lan78xx_read_raw_otp(dev, offset, length, data);
962 return ret;
965 static int lan78xx_dataport_wait_not_busy(struct lan78xx_net *dev)
967 int i, ret;
969 for (i = 0; i < 100; i++) {
970 u32 dp_sel;
972 ret = lan78xx_read_reg(dev, DP_SEL, &dp_sel);
973 if (unlikely(ret < 0))
974 return -EIO;
976 if (dp_sel & DP_SEL_DPRDY_)
977 return 0;
979 usleep_range(40, 100);
982 netdev_warn(dev->net, "lan78xx_dataport_wait_not_busy timed out");
984 return -EIO;
987 static int lan78xx_dataport_write(struct lan78xx_net *dev, u32 ram_select,
988 u32 addr, u32 length, u32 *buf)
990 struct lan78xx_priv *pdata = (struct lan78xx_priv *)(dev->data[0]);
991 u32 dp_sel;
992 int i, ret;
994 if (usb_autopm_get_interface(dev->intf) < 0)
995 return 0;
997 mutex_lock(&pdata->dataport_mutex);
999 ret = lan78xx_dataport_wait_not_busy(dev);
1000 if (ret < 0)
1001 goto done;
1003 ret = lan78xx_read_reg(dev, DP_SEL, &dp_sel);
1005 dp_sel &= ~DP_SEL_RSEL_MASK_;
1006 dp_sel |= ram_select;
1007 ret = lan78xx_write_reg(dev, DP_SEL, dp_sel);
1009 for (i = 0; i < length; i++) {
1010 ret = lan78xx_write_reg(dev, DP_ADDR, addr + i);
1012 ret = lan78xx_write_reg(dev, DP_DATA, buf[i]);
1014 ret = lan78xx_write_reg(dev, DP_CMD, DP_CMD_WRITE_);
1016 ret = lan78xx_dataport_wait_not_busy(dev);
1017 if (ret < 0)
1018 goto done;
1021 done:
1022 mutex_unlock(&pdata->dataport_mutex);
1023 usb_autopm_put_interface(dev->intf);
1025 return ret;
1028 static void lan78xx_set_addr_filter(struct lan78xx_priv *pdata,
1029 int index, u8 addr[ETH_ALEN])
1031 u32 temp;
1033 if ((pdata) && (index > 0) && (index < NUM_OF_MAF)) {
1034 temp = addr[3];
1035 temp = addr[2] | (temp << 8);
1036 temp = addr[1] | (temp << 8);
1037 temp = addr[0] | (temp << 8);
1038 pdata->pfilter_table[index][1] = temp;
1039 temp = addr[5];
1040 temp = addr[4] | (temp << 8);
1041 temp |= MAF_HI_VALID_ | MAF_HI_TYPE_DST_;
1042 pdata->pfilter_table[index][0] = temp;
1046 /* returns hash bit number for given MAC address */
1047 static inline u32 lan78xx_hash(char addr[ETH_ALEN])
1049 return (ether_crc(ETH_ALEN, addr) >> 23) & 0x1ff;
1052 static void lan78xx_deferred_multicast_write(struct work_struct *param)
1054 struct lan78xx_priv *pdata =
1055 container_of(param, struct lan78xx_priv, set_multicast);
1056 struct lan78xx_net *dev = pdata->dev;
1057 int i;
1058 int ret;
1060 netif_dbg(dev, drv, dev->net, "deferred multicast write 0x%08x\n",
1061 pdata->rfe_ctl);
1063 lan78xx_dataport_write(dev, DP_SEL_RSEL_VLAN_DA_, DP_SEL_VHF_VLAN_LEN,
1064 DP_SEL_VHF_HASH_LEN, pdata->mchash_table);
1066 for (i = 1; i < NUM_OF_MAF; i++) {
1067 ret = lan78xx_write_reg(dev, MAF_HI(i), 0);
1068 ret = lan78xx_write_reg(dev, MAF_LO(i),
1069 pdata->pfilter_table[i][1]);
1070 ret = lan78xx_write_reg(dev, MAF_HI(i),
1071 pdata->pfilter_table[i][0]);
1074 ret = lan78xx_write_reg(dev, RFE_CTL, pdata->rfe_ctl);
1077 static void lan78xx_set_multicast(struct net_device *netdev)
1079 struct lan78xx_net *dev = netdev_priv(netdev);
1080 struct lan78xx_priv *pdata = (struct lan78xx_priv *)(dev->data[0]);
1081 unsigned long flags;
1082 int i;
1084 spin_lock_irqsave(&pdata->rfe_ctl_lock, flags);
1086 pdata->rfe_ctl &= ~(RFE_CTL_UCAST_EN_ | RFE_CTL_MCAST_EN_ |
1087 RFE_CTL_DA_PERFECT_ | RFE_CTL_MCAST_HASH_);
1089 for (i = 0; i < DP_SEL_VHF_HASH_LEN; i++)
1090 pdata->mchash_table[i] = 0;
1091 /* pfilter_table[0] has own HW address */
1092 for (i = 1; i < NUM_OF_MAF; i++) {
1093 pdata->pfilter_table[i][0] =
1094 pdata->pfilter_table[i][1] = 0;
1097 pdata->rfe_ctl |= RFE_CTL_BCAST_EN_;
1099 if (dev->net->flags & IFF_PROMISC) {
1100 netif_dbg(dev, drv, dev->net, "promiscuous mode enabled");
1101 pdata->rfe_ctl |= RFE_CTL_MCAST_EN_ | RFE_CTL_UCAST_EN_;
1102 } else {
1103 if (dev->net->flags & IFF_ALLMULTI) {
1104 netif_dbg(dev, drv, dev->net,
1105 "receive all multicast enabled");
1106 pdata->rfe_ctl |= RFE_CTL_MCAST_EN_;
1110 if (netdev_mc_count(dev->net)) {
1111 struct netdev_hw_addr *ha;
1112 int i;
1114 netif_dbg(dev, drv, dev->net, "receive multicast hash filter");
1116 pdata->rfe_ctl |= RFE_CTL_DA_PERFECT_;
1118 i = 1;
1119 netdev_for_each_mc_addr(ha, netdev) {
1120 /* set first 32 into Perfect Filter */
1121 if (i < 33) {
1122 lan78xx_set_addr_filter(pdata, i, ha->addr);
1123 } else {
1124 u32 bitnum = lan78xx_hash(ha->addr);
1126 pdata->mchash_table[bitnum / 32] |=
1127 (1 << (bitnum % 32));
1128 pdata->rfe_ctl |= RFE_CTL_MCAST_HASH_;
1130 i++;
1134 spin_unlock_irqrestore(&pdata->rfe_ctl_lock, flags);
1136 /* defer register writes to a sleepable context */
1137 schedule_work(&pdata->set_multicast);
1140 static int lan78xx_update_flowcontrol(struct lan78xx_net *dev, u8 duplex,
1141 u16 lcladv, u16 rmtadv)
1143 u32 flow = 0, fct_flow = 0;
1144 int ret;
1145 u8 cap;
1147 if (dev->fc_autoneg)
1148 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
1149 else
1150 cap = dev->fc_request_control;
1152 if (cap & FLOW_CTRL_TX)
1153 flow |= (FLOW_CR_TX_FCEN_ | 0xFFFF);
1155 if (cap & FLOW_CTRL_RX)
1156 flow |= FLOW_CR_RX_FCEN_;
1158 if (dev->udev->speed == USB_SPEED_SUPER)
1159 fct_flow = 0x817;
1160 else if (dev->udev->speed == USB_SPEED_HIGH)
1161 fct_flow = 0x211;
1163 netif_dbg(dev, link, dev->net, "rx pause %s, tx pause %s",
1164 (cap & FLOW_CTRL_RX ? "enabled" : "disabled"),
1165 (cap & FLOW_CTRL_TX ? "enabled" : "disabled"));
1167 ret = lan78xx_write_reg(dev, FCT_FLOW, fct_flow);
1169 /* threshold value should be set before enabling flow */
1170 ret = lan78xx_write_reg(dev, FLOW, flow);
1172 return 0;
1175 static int lan78xx_link_reset(struct lan78xx_net *dev)
1177 struct phy_device *phydev = dev->net->phydev;
1178 struct ethtool_link_ksettings ecmd;
1179 int ladv, radv, ret;
1180 u32 buf;
1182 /* clear LAN78xx interrupt status */
1183 ret = lan78xx_write_reg(dev, INT_STS, INT_STS_PHY_INT_);
1184 if (unlikely(ret < 0))
1185 return -EIO;
1187 phy_read_status(phydev);
1189 if (!phydev->link && dev->link_on) {
1190 dev->link_on = false;
1192 /* reset MAC */
1193 ret = lan78xx_read_reg(dev, MAC_CR, &buf);
1194 if (unlikely(ret < 0))
1195 return -EIO;
1196 buf |= MAC_CR_RST_;
1197 ret = lan78xx_write_reg(dev, MAC_CR, buf);
1198 if (unlikely(ret < 0))
1199 return -EIO;
1201 del_timer(&dev->stat_monitor);
1202 } else if (phydev->link && !dev->link_on) {
1203 dev->link_on = true;
1205 phy_ethtool_ksettings_get(phydev, &ecmd);
1207 if (dev->udev->speed == USB_SPEED_SUPER) {
1208 if (ecmd.base.speed == 1000) {
1209 /* disable U2 */
1210 ret = lan78xx_read_reg(dev, USB_CFG1, &buf);
1211 buf &= ~USB_CFG1_DEV_U2_INIT_EN_;
1212 ret = lan78xx_write_reg(dev, USB_CFG1, buf);
1213 /* enable U1 */
1214 ret = lan78xx_read_reg(dev, USB_CFG1, &buf);
1215 buf |= USB_CFG1_DEV_U1_INIT_EN_;
1216 ret = lan78xx_write_reg(dev, USB_CFG1, buf);
1217 } else {
1218 /* enable U1 & U2 */
1219 ret = lan78xx_read_reg(dev, USB_CFG1, &buf);
1220 buf |= USB_CFG1_DEV_U2_INIT_EN_;
1221 buf |= USB_CFG1_DEV_U1_INIT_EN_;
1222 ret = lan78xx_write_reg(dev, USB_CFG1, buf);
1226 ladv = phy_read(phydev, MII_ADVERTISE);
1227 if (ladv < 0)
1228 return ladv;
1230 radv = phy_read(phydev, MII_LPA);
1231 if (radv < 0)
1232 return radv;
1234 netif_dbg(dev, link, dev->net,
1235 "speed: %u duplex: %d anadv: 0x%04x anlpa: 0x%04x",
1236 ecmd.base.speed, ecmd.base.duplex, ladv, radv);
1238 ret = lan78xx_update_flowcontrol(dev, ecmd.base.duplex, ladv,
1239 radv);
1241 if (!timer_pending(&dev->stat_monitor)) {
1242 dev->delta = 1;
1243 mod_timer(&dev->stat_monitor,
1244 jiffies + STAT_UPDATE_TIMER);
1247 tasklet_schedule(&dev->bh);
1250 return ret;
1253 /* some work can't be done in tasklets, so we use keventd
1255 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
1256 * but tasklet_schedule() doesn't. hope the failure is rare.
1258 static void lan78xx_defer_kevent(struct lan78xx_net *dev, int work)
1260 set_bit(work, &dev->flags);
1261 if (!schedule_delayed_work(&dev->wq, 0))
1262 netdev_err(dev->net, "kevent %d may have been dropped\n", work);
1265 static void lan78xx_status(struct lan78xx_net *dev, struct urb *urb)
1267 u32 intdata;
1269 if (urb->actual_length != 4) {
1270 netdev_warn(dev->net,
1271 "unexpected urb length %d", urb->actual_length);
1272 return;
1275 memcpy(&intdata, urb->transfer_buffer, 4);
1276 le32_to_cpus(&intdata);
1278 if (intdata & INT_ENP_PHY_INT) {
1279 netif_dbg(dev, link, dev->net, "PHY INTR: 0x%08x\n", intdata);
1280 lan78xx_defer_kevent(dev, EVENT_LINK_RESET);
1282 if (dev->domain_data.phyirq > 0) {
1283 local_irq_disable();
1284 generic_handle_irq(dev->domain_data.phyirq);
1285 local_irq_enable();
1287 } else
1288 netdev_warn(dev->net,
1289 "unexpected interrupt: 0x%08x\n", intdata);
1292 static int lan78xx_ethtool_get_eeprom_len(struct net_device *netdev)
1294 return MAX_EEPROM_SIZE;
1297 static int lan78xx_ethtool_get_eeprom(struct net_device *netdev,
1298 struct ethtool_eeprom *ee, u8 *data)
1300 struct lan78xx_net *dev = netdev_priv(netdev);
1301 int ret;
1303 ret = usb_autopm_get_interface(dev->intf);
1304 if (ret)
1305 return ret;
1307 ee->magic = LAN78XX_EEPROM_MAGIC;
1309 ret = lan78xx_read_raw_eeprom(dev, ee->offset, ee->len, data);
1311 usb_autopm_put_interface(dev->intf);
1313 return ret;
1316 static int lan78xx_ethtool_set_eeprom(struct net_device *netdev,
1317 struct ethtool_eeprom *ee, u8 *data)
1319 struct lan78xx_net *dev = netdev_priv(netdev);
1320 int ret;
1322 ret = usb_autopm_get_interface(dev->intf);
1323 if (ret)
1324 return ret;
1326 /* Invalid EEPROM_INDICATOR at offset zero will result in a failure
1327 * to load data from EEPROM
1329 if (ee->magic == LAN78XX_EEPROM_MAGIC)
1330 ret = lan78xx_write_raw_eeprom(dev, ee->offset, ee->len, data);
1331 else if ((ee->magic == LAN78XX_OTP_MAGIC) &&
1332 (ee->offset == 0) &&
1333 (ee->len == 512) &&
1334 (data[0] == OTP_INDICATOR_1))
1335 ret = lan78xx_write_raw_otp(dev, ee->offset, ee->len, data);
1337 usb_autopm_put_interface(dev->intf);
1339 return ret;
1342 static void lan78xx_get_strings(struct net_device *netdev, u32 stringset,
1343 u8 *data)
1345 if (stringset == ETH_SS_STATS)
1346 memcpy(data, lan78xx_gstrings, sizeof(lan78xx_gstrings));
1349 static int lan78xx_get_sset_count(struct net_device *netdev, int sset)
1351 if (sset == ETH_SS_STATS)
1352 return ARRAY_SIZE(lan78xx_gstrings);
1353 else
1354 return -EOPNOTSUPP;
1357 static void lan78xx_get_stats(struct net_device *netdev,
1358 struct ethtool_stats *stats, u64 *data)
1360 struct lan78xx_net *dev = netdev_priv(netdev);
1362 lan78xx_update_stats(dev);
1364 mutex_lock(&dev->stats.access_lock);
1365 memcpy(data, &dev->stats.curr_stat, sizeof(dev->stats.curr_stat));
1366 mutex_unlock(&dev->stats.access_lock);
1369 static void lan78xx_get_wol(struct net_device *netdev,
1370 struct ethtool_wolinfo *wol)
1372 struct lan78xx_net *dev = netdev_priv(netdev);
1373 int ret;
1374 u32 buf;
1375 struct lan78xx_priv *pdata = (struct lan78xx_priv *)(dev->data[0]);
1377 if (usb_autopm_get_interface(dev->intf) < 0)
1378 return;
1380 ret = lan78xx_read_reg(dev, USB_CFG0, &buf);
1381 if (unlikely(ret < 0)) {
1382 wol->supported = 0;
1383 wol->wolopts = 0;
1384 } else {
1385 if (buf & USB_CFG_RMT_WKP_) {
1386 wol->supported = WAKE_ALL;
1387 wol->wolopts = pdata->wol;
1388 } else {
1389 wol->supported = 0;
1390 wol->wolopts = 0;
1394 usb_autopm_put_interface(dev->intf);
1397 static int lan78xx_set_wol(struct net_device *netdev,
1398 struct ethtool_wolinfo *wol)
1400 struct lan78xx_net *dev = netdev_priv(netdev);
1401 struct lan78xx_priv *pdata = (struct lan78xx_priv *)(dev->data[0]);
1402 int ret;
1404 ret = usb_autopm_get_interface(dev->intf);
1405 if (ret < 0)
1406 return ret;
1408 if (wol->wolopts & ~WAKE_ALL)
1409 return -EINVAL;
1411 pdata->wol = wol->wolopts;
1413 device_set_wakeup_enable(&dev->udev->dev, (bool)wol->wolopts);
1415 phy_ethtool_set_wol(netdev->phydev, wol);
1417 usb_autopm_put_interface(dev->intf);
1419 return ret;
1422 static int lan78xx_get_eee(struct net_device *net, struct ethtool_eee *edata)
1424 struct lan78xx_net *dev = netdev_priv(net);
1425 struct phy_device *phydev = net->phydev;
1426 int ret;
1427 u32 buf;
1429 ret = usb_autopm_get_interface(dev->intf);
1430 if (ret < 0)
1431 return ret;
1433 ret = phy_ethtool_get_eee(phydev, edata);
1434 if (ret < 0)
1435 goto exit;
1437 ret = lan78xx_read_reg(dev, MAC_CR, &buf);
1438 if (buf & MAC_CR_EEE_EN_) {
1439 edata->eee_enabled = true;
1440 edata->eee_active = !!(edata->advertised &
1441 edata->lp_advertised);
1442 edata->tx_lpi_enabled = true;
1443 /* EEE_TX_LPI_REQ_DLY & tx_lpi_timer are same uSec unit */
1444 ret = lan78xx_read_reg(dev, EEE_TX_LPI_REQ_DLY, &buf);
1445 edata->tx_lpi_timer = buf;
1446 } else {
1447 edata->eee_enabled = false;
1448 edata->eee_active = false;
1449 edata->tx_lpi_enabled = false;
1450 edata->tx_lpi_timer = 0;
1453 ret = 0;
1454 exit:
1455 usb_autopm_put_interface(dev->intf);
1457 return ret;
1460 static int lan78xx_set_eee(struct net_device *net, struct ethtool_eee *edata)
1462 struct lan78xx_net *dev = netdev_priv(net);
1463 int ret;
1464 u32 buf;
1466 ret = usb_autopm_get_interface(dev->intf);
1467 if (ret < 0)
1468 return ret;
1470 if (edata->eee_enabled) {
1471 ret = lan78xx_read_reg(dev, MAC_CR, &buf);
1472 buf |= MAC_CR_EEE_EN_;
1473 ret = lan78xx_write_reg(dev, MAC_CR, buf);
1475 phy_ethtool_set_eee(net->phydev, edata);
1477 buf = (u32)edata->tx_lpi_timer;
1478 ret = lan78xx_write_reg(dev, EEE_TX_LPI_REQ_DLY, buf);
1479 } else {
1480 ret = lan78xx_read_reg(dev, MAC_CR, &buf);
1481 buf &= ~MAC_CR_EEE_EN_;
1482 ret = lan78xx_write_reg(dev, MAC_CR, buf);
1485 usb_autopm_put_interface(dev->intf);
1487 return 0;
1490 static u32 lan78xx_get_link(struct net_device *net)
1492 phy_read_status(net->phydev);
1494 return net->phydev->link;
1497 static void lan78xx_get_drvinfo(struct net_device *net,
1498 struct ethtool_drvinfo *info)
1500 struct lan78xx_net *dev = netdev_priv(net);
1502 strncpy(info->driver, DRIVER_NAME, sizeof(info->driver));
1503 usb_make_path(dev->udev, info->bus_info, sizeof(info->bus_info));
1506 static u32 lan78xx_get_msglevel(struct net_device *net)
1508 struct lan78xx_net *dev = netdev_priv(net);
1510 return dev->msg_enable;
1513 static void lan78xx_set_msglevel(struct net_device *net, u32 level)
1515 struct lan78xx_net *dev = netdev_priv(net);
1517 dev->msg_enable = level;
1520 static int lan78xx_get_link_ksettings(struct net_device *net,
1521 struct ethtool_link_ksettings *cmd)
1523 struct lan78xx_net *dev = netdev_priv(net);
1524 struct phy_device *phydev = net->phydev;
1525 int ret;
1527 ret = usb_autopm_get_interface(dev->intf);
1528 if (ret < 0)
1529 return ret;
1531 phy_ethtool_ksettings_get(phydev, cmd);
1533 usb_autopm_put_interface(dev->intf);
1535 return ret;
1538 static int lan78xx_set_link_ksettings(struct net_device *net,
1539 const struct ethtool_link_ksettings *cmd)
1541 struct lan78xx_net *dev = netdev_priv(net);
1542 struct phy_device *phydev = net->phydev;
1543 int ret = 0;
1544 int temp;
1546 ret = usb_autopm_get_interface(dev->intf);
1547 if (ret < 0)
1548 return ret;
1550 /* change speed & duplex */
1551 ret = phy_ethtool_ksettings_set(phydev, cmd);
1553 if (!cmd->base.autoneg) {
1554 /* force link down */
1555 temp = phy_read(phydev, MII_BMCR);
1556 phy_write(phydev, MII_BMCR, temp | BMCR_LOOPBACK);
1557 mdelay(1);
1558 phy_write(phydev, MII_BMCR, temp);
1561 usb_autopm_put_interface(dev->intf);
1563 return ret;
1566 static void lan78xx_get_pause(struct net_device *net,
1567 struct ethtool_pauseparam *pause)
1569 struct lan78xx_net *dev = netdev_priv(net);
1570 struct phy_device *phydev = net->phydev;
1571 struct ethtool_link_ksettings ecmd;
1573 phy_ethtool_ksettings_get(phydev, &ecmd);
1575 pause->autoneg = dev->fc_autoneg;
1577 if (dev->fc_request_control & FLOW_CTRL_TX)
1578 pause->tx_pause = 1;
1580 if (dev->fc_request_control & FLOW_CTRL_RX)
1581 pause->rx_pause = 1;
1584 static int lan78xx_set_pause(struct net_device *net,
1585 struct ethtool_pauseparam *pause)
1587 struct lan78xx_net *dev = netdev_priv(net);
1588 struct phy_device *phydev = net->phydev;
1589 struct ethtool_link_ksettings ecmd;
1590 int ret;
1592 phy_ethtool_ksettings_get(phydev, &ecmd);
1594 if (pause->autoneg && !ecmd.base.autoneg) {
1595 ret = -EINVAL;
1596 goto exit;
1599 dev->fc_request_control = 0;
1600 if (pause->rx_pause)
1601 dev->fc_request_control |= FLOW_CTRL_RX;
1603 if (pause->tx_pause)
1604 dev->fc_request_control |= FLOW_CTRL_TX;
1606 if (ecmd.base.autoneg) {
1607 u32 mii_adv;
1608 u32 advertising;
1610 ethtool_convert_link_mode_to_legacy_u32(
1611 &advertising, ecmd.link_modes.advertising);
1613 advertising &= ~(ADVERTISED_Pause | ADVERTISED_Asym_Pause);
1614 mii_adv = (u32)mii_advertise_flowctrl(dev->fc_request_control);
1615 advertising |= mii_adv_to_ethtool_adv_t(mii_adv);
1617 ethtool_convert_legacy_u32_to_link_mode(
1618 ecmd.link_modes.advertising, advertising);
1620 phy_ethtool_ksettings_set(phydev, &ecmd);
1623 dev->fc_autoneg = pause->autoneg;
1625 ret = 0;
1626 exit:
1627 return ret;
1630 static int lan78xx_get_regs_len(struct net_device *netdev)
1632 if (!netdev->phydev)
1633 return (sizeof(lan78xx_regs));
1634 else
1635 return (sizeof(lan78xx_regs) + PHY_REG_SIZE);
1638 static void
1639 lan78xx_get_regs(struct net_device *netdev, struct ethtool_regs *regs,
1640 void *buf)
1642 u32 *data = buf;
1643 int i, j;
1644 struct lan78xx_net *dev = netdev_priv(netdev);
1646 /* Read Device/MAC registers */
1647 for (i = 0; i < ARRAY_SIZE(lan78xx_regs); i++)
1648 lan78xx_read_reg(dev, lan78xx_regs[i], &data[i]);
1650 if (!netdev->phydev)
1651 return;
1653 /* Read PHY registers */
1654 for (j = 0; j < 32; i++, j++)
1655 data[i] = phy_read(netdev->phydev, j);
1658 static const struct ethtool_ops lan78xx_ethtool_ops = {
1659 .get_link = lan78xx_get_link,
1660 .nway_reset = phy_ethtool_nway_reset,
1661 .get_drvinfo = lan78xx_get_drvinfo,
1662 .get_msglevel = lan78xx_get_msglevel,
1663 .set_msglevel = lan78xx_set_msglevel,
1664 .get_eeprom_len = lan78xx_ethtool_get_eeprom_len,
1665 .get_eeprom = lan78xx_ethtool_get_eeprom,
1666 .set_eeprom = lan78xx_ethtool_set_eeprom,
1667 .get_ethtool_stats = lan78xx_get_stats,
1668 .get_sset_count = lan78xx_get_sset_count,
1669 .get_strings = lan78xx_get_strings,
1670 .get_wol = lan78xx_get_wol,
1671 .set_wol = lan78xx_set_wol,
1672 .get_eee = lan78xx_get_eee,
1673 .set_eee = lan78xx_set_eee,
1674 .get_pauseparam = lan78xx_get_pause,
1675 .set_pauseparam = lan78xx_set_pause,
1676 .get_link_ksettings = lan78xx_get_link_ksettings,
1677 .set_link_ksettings = lan78xx_set_link_ksettings,
1678 .get_regs_len = lan78xx_get_regs_len,
1679 .get_regs = lan78xx_get_regs,
1682 static int lan78xx_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
1684 if (!netif_running(netdev))
1685 return -EINVAL;
1687 return phy_mii_ioctl(netdev->phydev, rq, cmd);
1690 static void lan78xx_init_mac_address(struct lan78xx_net *dev)
1692 u32 addr_lo, addr_hi;
1693 int ret;
1694 u8 addr[6];
1696 ret = lan78xx_read_reg(dev, RX_ADDRL, &addr_lo);
1697 ret = lan78xx_read_reg(dev, RX_ADDRH, &addr_hi);
1699 addr[0] = addr_lo & 0xFF;
1700 addr[1] = (addr_lo >> 8) & 0xFF;
1701 addr[2] = (addr_lo >> 16) & 0xFF;
1702 addr[3] = (addr_lo >> 24) & 0xFF;
1703 addr[4] = addr_hi & 0xFF;
1704 addr[5] = (addr_hi >> 8) & 0xFF;
1706 if (!is_valid_ether_addr(addr)) {
1707 if (!eth_platform_get_mac_address(&dev->udev->dev, addr)) {
1708 /* valid address present in Device Tree */
1709 netif_dbg(dev, ifup, dev->net,
1710 "MAC address read from Device Tree");
1711 } else if (((lan78xx_read_eeprom(dev, EEPROM_MAC_OFFSET,
1712 ETH_ALEN, addr) == 0) ||
1713 (lan78xx_read_otp(dev, EEPROM_MAC_OFFSET,
1714 ETH_ALEN, addr) == 0)) &&
1715 is_valid_ether_addr(addr)) {
1716 /* eeprom values are valid so use them */
1717 netif_dbg(dev, ifup, dev->net,
1718 "MAC address read from EEPROM");
1719 } else {
1720 /* generate random MAC */
1721 eth_random_addr(addr);
1722 netif_dbg(dev, ifup, dev->net,
1723 "MAC address set to random addr");
1726 addr_lo = addr[0] | (addr[1] << 8) |
1727 (addr[2] << 16) | (addr[3] << 24);
1728 addr_hi = addr[4] | (addr[5] << 8);
1730 ret = lan78xx_write_reg(dev, RX_ADDRL, addr_lo);
1731 ret = lan78xx_write_reg(dev, RX_ADDRH, addr_hi);
1734 ret = lan78xx_write_reg(dev, MAF_LO(0), addr_lo);
1735 ret = lan78xx_write_reg(dev, MAF_HI(0), addr_hi | MAF_HI_VALID_);
1737 ether_addr_copy(dev->net->dev_addr, addr);
1740 /* MDIO read and write wrappers for phylib */
1741 static int lan78xx_mdiobus_read(struct mii_bus *bus, int phy_id, int idx)
1743 struct lan78xx_net *dev = bus->priv;
1744 u32 val, addr;
1745 int ret;
1747 ret = usb_autopm_get_interface(dev->intf);
1748 if (ret < 0)
1749 return ret;
1751 mutex_lock(&dev->phy_mutex);
1753 /* confirm MII not busy */
1754 ret = lan78xx_phy_wait_not_busy(dev);
1755 if (ret < 0)
1756 goto done;
1758 /* set the address, index & direction (read from PHY) */
1759 addr = mii_access(phy_id, idx, MII_READ);
1760 ret = lan78xx_write_reg(dev, MII_ACC, addr);
1762 ret = lan78xx_phy_wait_not_busy(dev);
1763 if (ret < 0)
1764 goto done;
1766 ret = lan78xx_read_reg(dev, MII_DATA, &val);
1768 ret = (int)(val & 0xFFFF);
1770 done:
1771 mutex_unlock(&dev->phy_mutex);
1772 usb_autopm_put_interface(dev->intf);
1774 return ret;
1777 static int lan78xx_mdiobus_write(struct mii_bus *bus, int phy_id, int idx,
1778 u16 regval)
1780 struct lan78xx_net *dev = bus->priv;
1781 u32 val, addr;
1782 int ret;
1784 ret = usb_autopm_get_interface(dev->intf);
1785 if (ret < 0)
1786 return ret;
1788 mutex_lock(&dev->phy_mutex);
1790 /* confirm MII not busy */
1791 ret = lan78xx_phy_wait_not_busy(dev);
1792 if (ret < 0)
1793 goto done;
1795 val = (u32)regval;
1796 ret = lan78xx_write_reg(dev, MII_DATA, val);
1798 /* set the address, index & direction (write to PHY) */
1799 addr = mii_access(phy_id, idx, MII_WRITE);
1800 ret = lan78xx_write_reg(dev, MII_ACC, addr);
1802 ret = lan78xx_phy_wait_not_busy(dev);
1803 if (ret < 0)
1804 goto done;
1806 done:
1807 mutex_unlock(&dev->phy_mutex);
1808 usb_autopm_put_interface(dev->intf);
1809 return 0;
1812 static int lan78xx_mdio_init(struct lan78xx_net *dev)
1814 struct device_node *node;
1815 int ret;
1817 dev->mdiobus = mdiobus_alloc();
1818 if (!dev->mdiobus) {
1819 netdev_err(dev->net, "can't allocate MDIO bus\n");
1820 return -ENOMEM;
1823 dev->mdiobus->priv = (void *)dev;
1824 dev->mdiobus->read = lan78xx_mdiobus_read;
1825 dev->mdiobus->write = lan78xx_mdiobus_write;
1826 dev->mdiobus->name = "lan78xx-mdiobus";
1827 dev->mdiobus->parent = &dev->udev->dev;
1829 snprintf(dev->mdiobus->id, MII_BUS_ID_SIZE, "usb-%03d:%03d",
1830 dev->udev->bus->busnum, dev->udev->devnum);
1832 switch (dev->chipid) {
1833 case ID_REV_CHIP_ID_7800_:
1834 case ID_REV_CHIP_ID_7850_:
1835 /* set to internal PHY id */
1836 dev->mdiobus->phy_mask = ~(1 << 1);
1837 break;
1838 case ID_REV_CHIP_ID_7801_:
1839 /* scan thru PHYAD[2..0] */
1840 dev->mdiobus->phy_mask = ~(0xFF);
1841 break;
1844 node = of_get_child_by_name(dev->udev->dev.of_node, "mdio");
1845 ret = of_mdiobus_register(dev->mdiobus, node);
1846 if (node)
1847 of_node_put(node);
1848 if (ret) {
1849 netdev_err(dev->net, "can't register MDIO bus\n");
1850 goto exit1;
1853 netdev_dbg(dev->net, "registered mdiobus bus %s\n", dev->mdiobus->id);
1854 return 0;
1855 exit1:
1856 mdiobus_free(dev->mdiobus);
1857 return ret;
1860 static void lan78xx_remove_mdio(struct lan78xx_net *dev)
1862 mdiobus_unregister(dev->mdiobus);
1863 mdiobus_free(dev->mdiobus);
1866 static void lan78xx_link_status_change(struct net_device *net)
1868 struct phy_device *phydev = net->phydev;
1869 int ret, temp;
1871 /* At forced 100 F/H mode, chip may fail to set mode correctly
1872 * when cable is switched between long(~50+m) and short one.
1873 * As workaround, set to 10 before setting to 100
1874 * at forced 100 F/H mode.
1876 if (!phydev->autoneg && (phydev->speed == 100)) {
1877 /* disable phy interrupt */
1878 temp = phy_read(phydev, LAN88XX_INT_MASK);
1879 temp &= ~LAN88XX_INT_MASK_MDINTPIN_EN_;
1880 ret = phy_write(phydev, LAN88XX_INT_MASK, temp);
1882 temp = phy_read(phydev, MII_BMCR);
1883 temp &= ~(BMCR_SPEED100 | BMCR_SPEED1000);
1884 phy_write(phydev, MII_BMCR, temp); /* set to 10 first */
1885 temp |= BMCR_SPEED100;
1886 phy_write(phydev, MII_BMCR, temp); /* set to 100 later */
1888 /* clear pending interrupt generated while workaround */
1889 temp = phy_read(phydev, LAN88XX_INT_STS);
1891 /* enable phy interrupt back */
1892 temp = phy_read(phydev, LAN88XX_INT_MASK);
1893 temp |= LAN88XX_INT_MASK_MDINTPIN_EN_;
1894 ret = phy_write(phydev, LAN88XX_INT_MASK, temp);
1898 static int irq_map(struct irq_domain *d, unsigned int irq,
1899 irq_hw_number_t hwirq)
1901 struct irq_domain_data *data = d->host_data;
1903 irq_set_chip_data(irq, data);
1904 irq_set_chip_and_handler(irq, data->irqchip, data->irq_handler);
1905 irq_set_noprobe(irq);
1907 return 0;
1910 static void irq_unmap(struct irq_domain *d, unsigned int irq)
1912 irq_set_chip_and_handler(irq, NULL, NULL);
1913 irq_set_chip_data(irq, NULL);
1916 static const struct irq_domain_ops chip_domain_ops = {
1917 .map = irq_map,
1918 .unmap = irq_unmap,
1921 static void lan78xx_irq_mask(struct irq_data *irqd)
1923 struct irq_domain_data *data = irq_data_get_irq_chip_data(irqd);
1925 data->irqenable &= ~BIT(irqd_to_hwirq(irqd));
1928 static void lan78xx_irq_unmask(struct irq_data *irqd)
1930 struct irq_domain_data *data = irq_data_get_irq_chip_data(irqd);
1932 data->irqenable |= BIT(irqd_to_hwirq(irqd));
1935 static void lan78xx_irq_bus_lock(struct irq_data *irqd)
1937 struct irq_domain_data *data = irq_data_get_irq_chip_data(irqd);
1939 mutex_lock(&data->irq_lock);
1942 static void lan78xx_irq_bus_sync_unlock(struct irq_data *irqd)
1944 struct irq_domain_data *data = irq_data_get_irq_chip_data(irqd);
1945 struct lan78xx_net *dev =
1946 container_of(data, struct lan78xx_net, domain_data);
1947 u32 buf;
1948 int ret;
1950 /* call register access here because irq_bus_lock & irq_bus_sync_unlock
1951 * are only two callbacks executed in non-atomic contex.
1953 ret = lan78xx_read_reg(dev, INT_EP_CTL, &buf);
1954 if (buf != data->irqenable)
1955 ret = lan78xx_write_reg(dev, INT_EP_CTL, data->irqenable);
1957 mutex_unlock(&data->irq_lock);
1960 static struct irq_chip lan78xx_irqchip = {
1961 .name = "lan78xx-irqs",
1962 .irq_mask = lan78xx_irq_mask,
1963 .irq_unmask = lan78xx_irq_unmask,
1964 .irq_bus_lock = lan78xx_irq_bus_lock,
1965 .irq_bus_sync_unlock = lan78xx_irq_bus_sync_unlock,
1968 static int lan78xx_setup_irq_domain(struct lan78xx_net *dev)
1970 struct device_node *of_node;
1971 struct irq_domain *irqdomain;
1972 unsigned int irqmap = 0;
1973 u32 buf;
1974 int ret = 0;
1976 of_node = dev->udev->dev.parent->of_node;
1978 mutex_init(&dev->domain_data.irq_lock);
1980 lan78xx_read_reg(dev, INT_EP_CTL, &buf);
1981 dev->domain_data.irqenable = buf;
1983 dev->domain_data.irqchip = &lan78xx_irqchip;
1984 dev->domain_data.irq_handler = handle_simple_irq;
1986 irqdomain = irq_domain_add_simple(of_node, MAX_INT_EP, 0,
1987 &chip_domain_ops, &dev->domain_data);
1988 if (irqdomain) {
1989 /* create mapping for PHY interrupt */
1990 irqmap = irq_create_mapping(irqdomain, INT_EP_PHY);
1991 if (!irqmap) {
1992 irq_domain_remove(irqdomain);
1994 irqdomain = NULL;
1995 ret = -EINVAL;
1997 } else {
1998 ret = -EINVAL;
2001 dev->domain_data.irqdomain = irqdomain;
2002 dev->domain_data.phyirq = irqmap;
2004 return ret;
2007 static void lan78xx_remove_irq_domain(struct lan78xx_net *dev)
2009 if (dev->domain_data.phyirq > 0) {
2010 irq_dispose_mapping(dev->domain_data.phyirq);
2012 if (dev->domain_data.irqdomain)
2013 irq_domain_remove(dev->domain_data.irqdomain);
2015 dev->domain_data.phyirq = 0;
2016 dev->domain_data.irqdomain = NULL;
2019 static int lan8835_fixup(struct phy_device *phydev)
2021 int buf;
2022 int ret;
2023 struct lan78xx_net *dev = netdev_priv(phydev->attached_dev);
2025 /* LED2/PME_N/IRQ_N/RGMII_ID pin to IRQ_N mode */
2026 buf = phy_read_mmd(phydev, MDIO_MMD_PCS, 0x8010);
2027 buf &= ~0x1800;
2028 buf |= 0x0800;
2029 phy_write_mmd(phydev, MDIO_MMD_PCS, 0x8010, buf);
2031 /* RGMII MAC TXC Delay Enable */
2032 ret = lan78xx_write_reg(dev, MAC_RGMII_ID,
2033 MAC_RGMII_ID_TXC_DELAY_EN_);
2035 /* RGMII TX DLL Tune Adjust */
2036 ret = lan78xx_write_reg(dev, RGMII_TX_BYP_DLL, 0x3D00);
2038 dev->interface = PHY_INTERFACE_MODE_RGMII_TXID;
2040 return 1;
2043 static int ksz9031rnx_fixup(struct phy_device *phydev)
2045 struct lan78xx_net *dev = netdev_priv(phydev->attached_dev);
2047 /* Micrel9301RNX PHY configuration */
2048 /* RGMII Control Signal Pad Skew */
2049 phy_write_mmd(phydev, MDIO_MMD_WIS, 4, 0x0077);
2050 /* RGMII RX Data Pad Skew */
2051 phy_write_mmd(phydev, MDIO_MMD_WIS, 5, 0x7777);
2052 /* RGMII RX Clock Pad Skew */
2053 phy_write_mmd(phydev, MDIO_MMD_WIS, 8, 0x1FF);
2055 dev->interface = PHY_INTERFACE_MODE_RGMII_RXID;
2057 return 1;
2060 static struct phy_device *lan7801_phy_init(struct lan78xx_net *dev)
2062 u32 buf;
2063 int ret;
2064 struct fixed_phy_status fphy_status = {
2065 .link = 1,
2066 .speed = SPEED_1000,
2067 .duplex = DUPLEX_FULL,
2069 struct phy_device *phydev;
2071 phydev = phy_find_first(dev->mdiobus);
2072 if (!phydev) {
2073 netdev_dbg(dev->net, "PHY Not Found!! Registering Fixed PHY\n");
2074 phydev = fixed_phy_register(PHY_POLL, &fphy_status, -1,
2075 NULL);
2076 if (IS_ERR(phydev)) {
2077 netdev_err(dev->net, "No PHY/fixed_PHY found\n");
2078 return NULL;
2080 netdev_dbg(dev->net, "Registered FIXED PHY\n");
2081 dev->interface = PHY_INTERFACE_MODE_RGMII;
2082 ret = lan78xx_write_reg(dev, MAC_RGMII_ID,
2083 MAC_RGMII_ID_TXC_DELAY_EN_);
2084 ret = lan78xx_write_reg(dev, RGMII_TX_BYP_DLL, 0x3D00);
2085 ret = lan78xx_read_reg(dev, HW_CFG, &buf);
2086 buf |= HW_CFG_CLK125_EN_;
2087 buf |= HW_CFG_REFCLK25_EN_;
2088 ret = lan78xx_write_reg(dev, HW_CFG, buf);
2089 } else {
2090 if (!phydev->drv) {
2091 netdev_err(dev->net, "no PHY driver found\n");
2092 return NULL;
2094 dev->interface = PHY_INTERFACE_MODE_RGMII;
2095 /* external PHY fixup for KSZ9031RNX */
2096 ret = phy_register_fixup_for_uid(PHY_KSZ9031RNX, 0xfffffff0,
2097 ksz9031rnx_fixup);
2098 if (ret < 0) {
2099 netdev_err(dev->net, "Failed to register fixup for PHY_KSZ9031RNX\n");
2100 return NULL;
2102 /* external PHY fixup for LAN8835 */
2103 ret = phy_register_fixup_for_uid(PHY_LAN8835, 0xfffffff0,
2104 lan8835_fixup);
2105 if (ret < 0) {
2106 netdev_err(dev->net, "Failed to register fixup for PHY_LAN8835\n");
2107 return NULL;
2109 /* add more external PHY fixup here if needed */
2111 phydev->is_internal = false;
2113 return phydev;
2116 static int lan78xx_phy_init(struct lan78xx_net *dev)
2118 int ret;
2119 u32 mii_adv;
2120 struct phy_device *phydev;
2122 switch (dev->chipid) {
2123 case ID_REV_CHIP_ID_7801_:
2124 phydev = lan7801_phy_init(dev);
2125 if (!phydev) {
2126 netdev_err(dev->net, "lan7801: PHY Init Failed");
2127 return -EIO;
2129 break;
2131 case ID_REV_CHIP_ID_7800_:
2132 case ID_REV_CHIP_ID_7850_:
2133 phydev = phy_find_first(dev->mdiobus);
2134 if (!phydev) {
2135 netdev_err(dev->net, "no PHY found\n");
2136 return -EIO;
2138 phydev->is_internal = true;
2139 dev->interface = PHY_INTERFACE_MODE_GMII;
2140 break;
2142 default:
2143 netdev_err(dev->net, "Unknown CHIP ID found\n");
2144 return -EIO;
2147 /* if phyirq is not set, use polling mode in phylib */
2148 if (dev->domain_data.phyirq > 0)
2149 phydev->irq = dev->domain_data.phyirq;
2150 else
2151 phydev->irq = 0;
2152 netdev_dbg(dev->net, "phydev->irq = %d\n", phydev->irq);
2154 /* set to AUTOMDIX */
2155 phydev->mdix = ETH_TP_MDI_AUTO;
2157 ret = phy_connect_direct(dev->net, phydev,
2158 lan78xx_link_status_change,
2159 dev->interface);
2160 if (ret) {
2161 netdev_err(dev->net, "can't attach PHY to %s\n",
2162 dev->mdiobus->id);
2163 if (dev->chipid == ID_REV_CHIP_ID_7801_) {
2164 if (phy_is_pseudo_fixed_link(phydev)) {
2165 fixed_phy_unregister(phydev);
2166 } else {
2167 phy_unregister_fixup_for_uid(PHY_KSZ9031RNX,
2168 0xfffffff0);
2169 phy_unregister_fixup_for_uid(PHY_LAN8835,
2170 0xfffffff0);
2173 return -EIO;
2176 /* MAC doesn't support 1000T Half */
2177 phydev->supported &= ~SUPPORTED_1000baseT_Half;
2179 /* support both flow controls */
2180 dev->fc_request_control = (FLOW_CTRL_RX | FLOW_CTRL_TX);
2181 phydev->advertising &= ~(ADVERTISED_Pause | ADVERTISED_Asym_Pause);
2182 mii_adv = (u32)mii_advertise_flowctrl(dev->fc_request_control);
2183 phydev->advertising |= mii_adv_to_ethtool_adv_t(mii_adv);
2185 if (phydev->mdio.dev.of_node) {
2186 u32 reg;
2187 int len;
2189 len = of_property_count_elems_of_size(phydev->mdio.dev.of_node,
2190 "microchip,led-modes",
2191 sizeof(u32));
2192 if (len >= 0) {
2193 /* Ensure the appropriate LEDs are enabled */
2194 lan78xx_read_reg(dev, HW_CFG, &reg);
2195 reg &= ~(HW_CFG_LED0_EN_ |
2196 HW_CFG_LED1_EN_ |
2197 HW_CFG_LED2_EN_ |
2198 HW_CFG_LED3_EN_);
2199 reg |= (len > 0) * HW_CFG_LED0_EN_ |
2200 (len > 1) * HW_CFG_LED1_EN_ |
2201 (len > 2) * HW_CFG_LED2_EN_ |
2202 (len > 3) * HW_CFG_LED3_EN_;
2203 lan78xx_write_reg(dev, HW_CFG, reg);
2207 genphy_config_aneg(phydev);
2209 dev->fc_autoneg = phydev->autoneg;
2211 return 0;
2214 static int lan78xx_set_rx_max_frame_length(struct lan78xx_net *dev, int size)
2216 int ret = 0;
2217 u32 buf;
2218 bool rxenabled;
2220 ret = lan78xx_read_reg(dev, MAC_RX, &buf);
2222 rxenabled = ((buf & MAC_RX_RXEN_) != 0);
2224 if (rxenabled) {
2225 buf &= ~MAC_RX_RXEN_;
2226 ret = lan78xx_write_reg(dev, MAC_RX, buf);
2229 /* add 4 to size for FCS */
2230 buf &= ~MAC_RX_MAX_SIZE_MASK_;
2231 buf |= (((size + 4) << MAC_RX_MAX_SIZE_SHIFT_) & MAC_RX_MAX_SIZE_MASK_);
2233 ret = lan78xx_write_reg(dev, MAC_RX, buf);
2235 if (rxenabled) {
2236 buf |= MAC_RX_RXEN_;
2237 ret = lan78xx_write_reg(dev, MAC_RX, buf);
2240 return 0;
2243 static int unlink_urbs(struct lan78xx_net *dev, struct sk_buff_head *q)
2245 struct sk_buff *skb;
2246 unsigned long flags;
2247 int count = 0;
2249 spin_lock_irqsave(&q->lock, flags);
2250 while (!skb_queue_empty(q)) {
2251 struct skb_data *entry;
2252 struct urb *urb;
2253 int ret;
2255 skb_queue_walk(q, skb) {
2256 entry = (struct skb_data *)skb->cb;
2257 if (entry->state != unlink_start)
2258 goto found;
2260 break;
2261 found:
2262 entry->state = unlink_start;
2263 urb = entry->urb;
2265 /* Get reference count of the URB to avoid it to be
2266 * freed during usb_unlink_urb, which may trigger
2267 * use-after-free problem inside usb_unlink_urb since
2268 * usb_unlink_urb is always racing with .complete
2269 * handler(include defer_bh).
2271 usb_get_urb(urb);
2272 spin_unlock_irqrestore(&q->lock, flags);
2273 /* during some PM-driven resume scenarios,
2274 * these (async) unlinks complete immediately
2276 ret = usb_unlink_urb(urb);
2277 if (ret != -EINPROGRESS && ret != 0)
2278 netdev_dbg(dev->net, "unlink urb err, %d\n", ret);
2279 else
2280 count++;
2281 usb_put_urb(urb);
2282 spin_lock_irqsave(&q->lock, flags);
2284 spin_unlock_irqrestore(&q->lock, flags);
2285 return count;
2288 static int lan78xx_change_mtu(struct net_device *netdev, int new_mtu)
2290 struct lan78xx_net *dev = netdev_priv(netdev);
2291 int ll_mtu = new_mtu + netdev->hard_header_len;
2292 int old_hard_mtu = dev->hard_mtu;
2293 int old_rx_urb_size = dev->rx_urb_size;
2294 int ret;
2296 /* no second zero-length packet read wanted after mtu-sized packets */
2297 if ((ll_mtu % dev->maxpacket) == 0)
2298 return -EDOM;
2300 ret = lan78xx_set_rx_max_frame_length(dev, new_mtu + VLAN_ETH_HLEN);
2302 netdev->mtu = new_mtu;
2304 dev->hard_mtu = netdev->mtu + netdev->hard_header_len;
2305 if (dev->rx_urb_size == old_hard_mtu) {
2306 dev->rx_urb_size = dev->hard_mtu;
2307 if (dev->rx_urb_size > old_rx_urb_size) {
2308 if (netif_running(dev->net)) {
2309 unlink_urbs(dev, &dev->rxq);
2310 tasklet_schedule(&dev->bh);
2315 return 0;
2318 static int lan78xx_set_mac_addr(struct net_device *netdev, void *p)
2320 struct lan78xx_net *dev = netdev_priv(netdev);
2321 struct sockaddr *addr = p;
2322 u32 addr_lo, addr_hi;
2323 int ret;
2325 if (netif_running(netdev))
2326 return -EBUSY;
2328 if (!is_valid_ether_addr(addr->sa_data))
2329 return -EADDRNOTAVAIL;
2331 ether_addr_copy(netdev->dev_addr, addr->sa_data);
2333 addr_lo = netdev->dev_addr[0] |
2334 netdev->dev_addr[1] << 8 |
2335 netdev->dev_addr[2] << 16 |
2336 netdev->dev_addr[3] << 24;
2337 addr_hi = netdev->dev_addr[4] |
2338 netdev->dev_addr[5] << 8;
2340 ret = lan78xx_write_reg(dev, RX_ADDRL, addr_lo);
2341 ret = lan78xx_write_reg(dev, RX_ADDRH, addr_hi);
2343 /* Added to support MAC address changes */
2344 ret = lan78xx_write_reg(dev, MAF_LO(0), addr_lo);
2345 ret = lan78xx_write_reg(dev, MAF_HI(0), addr_hi | MAF_HI_VALID_);
2347 return 0;
2350 /* Enable or disable Rx checksum offload engine */
2351 static int lan78xx_set_features(struct net_device *netdev,
2352 netdev_features_t features)
2354 struct lan78xx_net *dev = netdev_priv(netdev);
2355 struct lan78xx_priv *pdata = (struct lan78xx_priv *)(dev->data[0]);
2356 unsigned long flags;
2357 int ret;
2359 spin_lock_irqsave(&pdata->rfe_ctl_lock, flags);
2361 if (features & NETIF_F_RXCSUM) {
2362 pdata->rfe_ctl |= RFE_CTL_TCPUDP_COE_ | RFE_CTL_IP_COE_;
2363 pdata->rfe_ctl |= RFE_CTL_ICMP_COE_ | RFE_CTL_IGMP_COE_;
2364 } else {
2365 pdata->rfe_ctl &= ~(RFE_CTL_TCPUDP_COE_ | RFE_CTL_IP_COE_);
2366 pdata->rfe_ctl &= ~(RFE_CTL_ICMP_COE_ | RFE_CTL_IGMP_COE_);
2369 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2370 pdata->rfe_ctl |= RFE_CTL_VLAN_STRIP_;
2371 else
2372 pdata->rfe_ctl &= ~RFE_CTL_VLAN_STRIP_;
2374 if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
2375 pdata->rfe_ctl |= RFE_CTL_VLAN_FILTER_;
2376 else
2377 pdata->rfe_ctl &= ~RFE_CTL_VLAN_FILTER_;
2379 spin_unlock_irqrestore(&pdata->rfe_ctl_lock, flags);
2381 ret = lan78xx_write_reg(dev, RFE_CTL, pdata->rfe_ctl);
2383 return 0;
2386 static void lan78xx_deferred_vlan_write(struct work_struct *param)
2388 struct lan78xx_priv *pdata =
2389 container_of(param, struct lan78xx_priv, set_vlan);
2390 struct lan78xx_net *dev = pdata->dev;
2392 lan78xx_dataport_write(dev, DP_SEL_RSEL_VLAN_DA_, 0,
2393 DP_SEL_VHF_VLAN_LEN, pdata->vlan_table);
2396 static int lan78xx_vlan_rx_add_vid(struct net_device *netdev,
2397 __be16 proto, u16 vid)
2399 struct lan78xx_net *dev = netdev_priv(netdev);
2400 struct lan78xx_priv *pdata = (struct lan78xx_priv *)(dev->data[0]);
2401 u16 vid_bit_index;
2402 u16 vid_dword_index;
2404 vid_dword_index = (vid >> 5) & 0x7F;
2405 vid_bit_index = vid & 0x1F;
2407 pdata->vlan_table[vid_dword_index] |= (1 << vid_bit_index);
2409 /* defer register writes to a sleepable context */
2410 schedule_work(&pdata->set_vlan);
2412 return 0;
2415 static int lan78xx_vlan_rx_kill_vid(struct net_device *netdev,
2416 __be16 proto, u16 vid)
2418 struct lan78xx_net *dev = netdev_priv(netdev);
2419 struct lan78xx_priv *pdata = (struct lan78xx_priv *)(dev->data[0]);
2420 u16 vid_bit_index;
2421 u16 vid_dword_index;
2423 vid_dword_index = (vid >> 5) & 0x7F;
2424 vid_bit_index = vid & 0x1F;
2426 pdata->vlan_table[vid_dword_index] &= ~(1 << vid_bit_index);
2428 /* defer register writes to a sleepable context */
2429 schedule_work(&pdata->set_vlan);
2431 return 0;
2434 static void lan78xx_init_ltm(struct lan78xx_net *dev)
2436 int ret;
2437 u32 buf;
2438 u32 regs[6] = { 0 };
2440 ret = lan78xx_read_reg(dev, USB_CFG1, &buf);
2441 if (buf & USB_CFG1_LTM_ENABLE_) {
2442 u8 temp[2];
2443 /* Get values from EEPROM first */
2444 if (lan78xx_read_eeprom(dev, 0x3F, 2, temp) == 0) {
2445 if (temp[0] == 24) {
2446 ret = lan78xx_read_raw_eeprom(dev,
2447 temp[1] * 2,
2449 (u8 *)regs);
2450 if (ret < 0)
2451 return;
2453 } else if (lan78xx_read_otp(dev, 0x3F, 2, temp) == 0) {
2454 if (temp[0] == 24) {
2455 ret = lan78xx_read_raw_otp(dev,
2456 temp[1] * 2,
2458 (u8 *)regs);
2459 if (ret < 0)
2460 return;
2465 lan78xx_write_reg(dev, LTM_BELT_IDLE0, regs[0]);
2466 lan78xx_write_reg(dev, LTM_BELT_IDLE1, regs[1]);
2467 lan78xx_write_reg(dev, LTM_BELT_ACT0, regs[2]);
2468 lan78xx_write_reg(dev, LTM_BELT_ACT1, regs[3]);
2469 lan78xx_write_reg(dev, LTM_INACTIVE0, regs[4]);
2470 lan78xx_write_reg(dev, LTM_INACTIVE1, regs[5]);
2473 static int lan78xx_reset(struct lan78xx_net *dev)
2475 struct lan78xx_priv *pdata = (struct lan78xx_priv *)(dev->data[0]);
2476 u32 buf;
2477 int ret = 0;
2478 unsigned long timeout;
2479 u8 sig;
2481 ret = lan78xx_read_reg(dev, HW_CFG, &buf);
2482 buf |= HW_CFG_LRST_;
2483 ret = lan78xx_write_reg(dev, HW_CFG, buf);
2485 timeout = jiffies + HZ;
2486 do {
2487 mdelay(1);
2488 ret = lan78xx_read_reg(dev, HW_CFG, &buf);
2489 if (time_after(jiffies, timeout)) {
2490 netdev_warn(dev->net,
2491 "timeout on completion of LiteReset");
2492 return -EIO;
2494 } while (buf & HW_CFG_LRST_);
2496 lan78xx_init_mac_address(dev);
2498 /* save DEVID for later usage */
2499 ret = lan78xx_read_reg(dev, ID_REV, &buf);
2500 dev->chipid = (buf & ID_REV_CHIP_ID_MASK_) >> 16;
2501 dev->chiprev = buf & ID_REV_CHIP_REV_MASK_;
2503 /* Respond to the IN token with a NAK */
2504 ret = lan78xx_read_reg(dev, USB_CFG0, &buf);
2505 buf |= USB_CFG_BIR_;
2506 ret = lan78xx_write_reg(dev, USB_CFG0, buf);
2508 /* Init LTM */
2509 lan78xx_init_ltm(dev);
2511 if (dev->udev->speed == USB_SPEED_SUPER) {
2512 buf = DEFAULT_BURST_CAP_SIZE / SS_USB_PKT_SIZE;
2513 dev->rx_urb_size = DEFAULT_BURST_CAP_SIZE;
2514 dev->rx_qlen = 4;
2515 dev->tx_qlen = 4;
2516 } else if (dev->udev->speed == USB_SPEED_HIGH) {
2517 buf = DEFAULT_BURST_CAP_SIZE / HS_USB_PKT_SIZE;
2518 dev->rx_urb_size = DEFAULT_BURST_CAP_SIZE;
2519 dev->rx_qlen = RX_MAX_QUEUE_MEMORY / dev->rx_urb_size;
2520 dev->tx_qlen = RX_MAX_QUEUE_MEMORY / dev->hard_mtu;
2521 } else {
2522 buf = DEFAULT_BURST_CAP_SIZE / FS_USB_PKT_SIZE;
2523 dev->rx_urb_size = DEFAULT_BURST_CAP_SIZE;
2524 dev->rx_qlen = 4;
2525 dev->tx_qlen = 4;
2528 ret = lan78xx_write_reg(dev, BURST_CAP, buf);
2529 ret = lan78xx_write_reg(dev, BULK_IN_DLY, DEFAULT_BULK_IN_DELAY);
2531 ret = lan78xx_read_reg(dev, HW_CFG, &buf);
2532 buf |= HW_CFG_MEF_;
2533 ret = lan78xx_write_reg(dev, HW_CFG, buf);
2535 ret = lan78xx_read_reg(dev, USB_CFG0, &buf);
2536 buf |= USB_CFG_BCE_;
2537 ret = lan78xx_write_reg(dev, USB_CFG0, buf);
2539 /* set FIFO sizes */
2540 buf = (MAX_RX_FIFO_SIZE - 512) / 512;
2541 ret = lan78xx_write_reg(dev, FCT_RX_FIFO_END, buf);
2543 buf = (MAX_TX_FIFO_SIZE - 512) / 512;
2544 ret = lan78xx_write_reg(dev, FCT_TX_FIFO_END, buf);
2546 ret = lan78xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL_);
2547 ret = lan78xx_write_reg(dev, FLOW, 0);
2548 ret = lan78xx_write_reg(dev, FCT_FLOW, 0);
2550 /* Don't need rfe_ctl_lock during initialisation */
2551 ret = lan78xx_read_reg(dev, RFE_CTL, &pdata->rfe_ctl);
2552 pdata->rfe_ctl |= RFE_CTL_BCAST_EN_ | RFE_CTL_DA_PERFECT_;
2553 ret = lan78xx_write_reg(dev, RFE_CTL, pdata->rfe_ctl);
2555 /* Enable or disable checksum offload engines */
2556 lan78xx_set_features(dev->net, dev->net->features);
2558 lan78xx_set_multicast(dev->net);
2560 /* reset PHY */
2561 ret = lan78xx_read_reg(dev, PMT_CTL, &buf);
2562 buf |= PMT_CTL_PHY_RST_;
2563 ret = lan78xx_write_reg(dev, PMT_CTL, buf);
2565 timeout = jiffies + HZ;
2566 do {
2567 mdelay(1);
2568 ret = lan78xx_read_reg(dev, PMT_CTL, &buf);
2569 if (time_after(jiffies, timeout)) {
2570 netdev_warn(dev->net, "timeout waiting for PHY Reset");
2571 return -EIO;
2573 } while ((buf & PMT_CTL_PHY_RST_) || !(buf & PMT_CTL_READY_));
2575 ret = lan78xx_read_reg(dev, MAC_CR, &buf);
2576 /* LAN7801 only has RGMII mode */
2577 if (dev->chipid == ID_REV_CHIP_ID_7801_)
2578 buf &= ~MAC_CR_GMII_EN_;
2580 if (dev->chipid == ID_REV_CHIP_ID_7800_) {
2581 ret = lan78xx_read_raw_eeprom(dev, 0, 1, &sig);
2582 if (!ret && sig != EEPROM_INDICATOR) {
2583 /* Implies there is no external eeprom. Set mac speed */
2584 netdev_info(dev->net, "No External EEPROM. Setting MAC Speed\n");
2585 buf |= MAC_CR_AUTO_DUPLEX_ | MAC_CR_AUTO_SPEED_;
2588 ret = lan78xx_write_reg(dev, MAC_CR, buf);
2590 ret = lan78xx_read_reg(dev, MAC_TX, &buf);
2591 buf |= MAC_TX_TXEN_;
2592 ret = lan78xx_write_reg(dev, MAC_TX, buf);
2594 ret = lan78xx_read_reg(dev, FCT_TX_CTL, &buf);
2595 buf |= FCT_TX_CTL_EN_;
2596 ret = lan78xx_write_reg(dev, FCT_TX_CTL, buf);
2598 ret = lan78xx_set_rx_max_frame_length(dev,
2599 dev->net->mtu + VLAN_ETH_HLEN);
2601 ret = lan78xx_read_reg(dev, MAC_RX, &buf);
2602 buf |= MAC_RX_RXEN_;
2603 ret = lan78xx_write_reg(dev, MAC_RX, buf);
2605 ret = lan78xx_read_reg(dev, FCT_RX_CTL, &buf);
2606 buf |= FCT_RX_CTL_EN_;
2607 ret = lan78xx_write_reg(dev, FCT_RX_CTL, buf);
2609 return 0;
2612 static void lan78xx_init_stats(struct lan78xx_net *dev)
2614 u32 *p;
2615 int i;
2617 /* initialize for stats update
2618 * some counters are 20bits and some are 32bits
2620 p = (u32 *)&dev->stats.rollover_max;
2621 for (i = 0; i < (sizeof(dev->stats.rollover_max) / (sizeof(u32))); i++)
2622 p[i] = 0xFFFFF;
2624 dev->stats.rollover_max.rx_unicast_byte_count = 0xFFFFFFFF;
2625 dev->stats.rollover_max.rx_broadcast_byte_count = 0xFFFFFFFF;
2626 dev->stats.rollover_max.rx_multicast_byte_count = 0xFFFFFFFF;
2627 dev->stats.rollover_max.eee_rx_lpi_transitions = 0xFFFFFFFF;
2628 dev->stats.rollover_max.eee_rx_lpi_time = 0xFFFFFFFF;
2629 dev->stats.rollover_max.tx_unicast_byte_count = 0xFFFFFFFF;
2630 dev->stats.rollover_max.tx_broadcast_byte_count = 0xFFFFFFFF;
2631 dev->stats.rollover_max.tx_multicast_byte_count = 0xFFFFFFFF;
2632 dev->stats.rollover_max.eee_tx_lpi_transitions = 0xFFFFFFFF;
2633 dev->stats.rollover_max.eee_tx_lpi_time = 0xFFFFFFFF;
2635 set_bit(EVENT_STAT_UPDATE, &dev->flags);
2638 static int lan78xx_open(struct net_device *net)
2640 struct lan78xx_net *dev = netdev_priv(net);
2641 int ret;
2643 ret = usb_autopm_get_interface(dev->intf);
2644 if (ret < 0)
2645 goto out;
2647 phy_start(net->phydev);
2649 netif_dbg(dev, ifup, dev->net, "phy initialised successfully");
2651 /* for Link Check */
2652 if (dev->urb_intr) {
2653 ret = usb_submit_urb(dev->urb_intr, GFP_KERNEL);
2654 if (ret < 0) {
2655 netif_err(dev, ifup, dev->net,
2656 "intr submit %d\n", ret);
2657 goto done;
2661 lan78xx_init_stats(dev);
2663 set_bit(EVENT_DEV_OPEN, &dev->flags);
2665 netif_start_queue(net);
2667 dev->link_on = false;
2669 lan78xx_defer_kevent(dev, EVENT_LINK_RESET);
2670 done:
2671 usb_autopm_put_interface(dev->intf);
2673 out:
2674 return ret;
2677 static void lan78xx_terminate_urbs(struct lan78xx_net *dev)
2679 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(unlink_wakeup);
2680 DECLARE_WAITQUEUE(wait, current);
2681 int temp;
2683 /* ensure there are no more active urbs */
2684 add_wait_queue(&unlink_wakeup, &wait);
2685 set_current_state(TASK_UNINTERRUPTIBLE);
2686 dev->wait = &unlink_wakeup;
2687 temp = unlink_urbs(dev, &dev->txq) + unlink_urbs(dev, &dev->rxq);
2689 /* maybe wait for deletions to finish. */
2690 while (!skb_queue_empty(&dev->rxq) &&
2691 !skb_queue_empty(&dev->txq) &&
2692 !skb_queue_empty(&dev->done)) {
2693 schedule_timeout(msecs_to_jiffies(UNLINK_TIMEOUT_MS));
2694 set_current_state(TASK_UNINTERRUPTIBLE);
2695 netif_dbg(dev, ifdown, dev->net,
2696 "waited for %d urb completions\n", temp);
2698 set_current_state(TASK_RUNNING);
2699 dev->wait = NULL;
2700 remove_wait_queue(&unlink_wakeup, &wait);
2703 static int lan78xx_stop(struct net_device *net)
2705 struct lan78xx_net *dev = netdev_priv(net);
2707 if (timer_pending(&dev->stat_monitor))
2708 del_timer_sync(&dev->stat_monitor);
2710 if (net->phydev)
2711 phy_stop(net->phydev);
2713 clear_bit(EVENT_DEV_OPEN, &dev->flags);
2714 netif_stop_queue(net);
2716 netif_info(dev, ifdown, dev->net,
2717 "stop stats: rx/tx %lu/%lu, errs %lu/%lu\n",
2718 net->stats.rx_packets, net->stats.tx_packets,
2719 net->stats.rx_errors, net->stats.tx_errors);
2721 lan78xx_terminate_urbs(dev);
2723 usb_kill_urb(dev->urb_intr);
2725 skb_queue_purge(&dev->rxq_pause);
2727 /* deferred work (task, timer, softirq) must also stop.
2728 * can't flush_scheduled_work() until we drop rtnl (later),
2729 * else workers could deadlock; so make workers a NOP.
2731 dev->flags = 0;
2732 cancel_delayed_work_sync(&dev->wq);
2733 tasklet_kill(&dev->bh);
2735 usb_autopm_put_interface(dev->intf);
2737 return 0;
2740 static struct sk_buff *lan78xx_tx_prep(struct lan78xx_net *dev,
2741 struct sk_buff *skb, gfp_t flags)
2743 u32 tx_cmd_a, tx_cmd_b;
2745 if (skb_cow_head(skb, TX_OVERHEAD)) {
2746 dev_kfree_skb_any(skb);
2747 return NULL;
2750 if (skb_linearize(skb)) {
2751 dev_kfree_skb_any(skb);
2752 return NULL;
2755 tx_cmd_a = (u32)(skb->len & TX_CMD_A_LEN_MASK_) | TX_CMD_A_FCS_;
2757 if (skb->ip_summed == CHECKSUM_PARTIAL)
2758 tx_cmd_a |= TX_CMD_A_IPE_ | TX_CMD_A_TPE_;
2760 tx_cmd_b = 0;
2761 if (skb_is_gso(skb)) {
2762 u16 mss = max(skb_shinfo(skb)->gso_size, TX_CMD_B_MSS_MIN_);
2764 tx_cmd_b = (mss << TX_CMD_B_MSS_SHIFT_) & TX_CMD_B_MSS_MASK_;
2766 tx_cmd_a |= TX_CMD_A_LSO_;
2769 if (skb_vlan_tag_present(skb)) {
2770 tx_cmd_a |= TX_CMD_A_IVTG_;
2771 tx_cmd_b |= skb_vlan_tag_get(skb) & TX_CMD_B_VTAG_MASK_;
2774 skb_push(skb, 4);
2775 cpu_to_le32s(&tx_cmd_b);
2776 memcpy(skb->data, &tx_cmd_b, 4);
2778 skb_push(skb, 4);
2779 cpu_to_le32s(&tx_cmd_a);
2780 memcpy(skb->data, &tx_cmd_a, 4);
2782 return skb;
2785 static enum skb_state defer_bh(struct lan78xx_net *dev, struct sk_buff *skb,
2786 struct sk_buff_head *list, enum skb_state state)
2788 unsigned long flags;
2789 enum skb_state old_state;
2790 struct skb_data *entry = (struct skb_data *)skb->cb;
2792 spin_lock_irqsave(&list->lock, flags);
2793 old_state = entry->state;
2794 entry->state = state;
2796 __skb_unlink(skb, list);
2797 spin_unlock(&list->lock);
2798 spin_lock(&dev->done.lock);
2800 __skb_queue_tail(&dev->done, skb);
2801 if (skb_queue_len(&dev->done) == 1)
2802 tasklet_schedule(&dev->bh);
2803 spin_unlock_irqrestore(&dev->done.lock, flags);
2805 return old_state;
2808 static void tx_complete(struct urb *urb)
2810 struct sk_buff *skb = (struct sk_buff *)urb->context;
2811 struct skb_data *entry = (struct skb_data *)skb->cb;
2812 struct lan78xx_net *dev = entry->dev;
2814 if (urb->status == 0) {
2815 dev->net->stats.tx_packets += entry->num_of_packet;
2816 dev->net->stats.tx_bytes += entry->length;
2817 } else {
2818 dev->net->stats.tx_errors++;
2820 switch (urb->status) {
2821 case -EPIPE:
2822 lan78xx_defer_kevent(dev, EVENT_TX_HALT);
2823 break;
2825 /* software-driven interface shutdown */
2826 case -ECONNRESET:
2827 case -ESHUTDOWN:
2828 break;
2830 case -EPROTO:
2831 case -ETIME:
2832 case -EILSEQ:
2833 netif_stop_queue(dev->net);
2834 break;
2835 default:
2836 netif_dbg(dev, tx_err, dev->net,
2837 "tx err %d\n", entry->urb->status);
2838 break;
2842 usb_autopm_put_interface_async(dev->intf);
2844 defer_bh(dev, skb, &dev->txq, tx_done);
2847 static void lan78xx_queue_skb(struct sk_buff_head *list,
2848 struct sk_buff *newsk, enum skb_state state)
2850 struct skb_data *entry = (struct skb_data *)newsk->cb;
2852 __skb_queue_tail(list, newsk);
2853 entry->state = state;
2856 static netdev_tx_t
2857 lan78xx_start_xmit(struct sk_buff *skb, struct net_device *net)
2859 struct lan78xx_net *dev = netdev_priv(net);
2860 struct sk_buff *skb2 = NULL;
2862 if (skb) {
2863 skb_tx_timestamp(skb);
2864 skb2 = lan78xx_tx_prep(dev, skb, GFP_ATOMIC);
2867 if (skb2) {
2868 skb_queue_tail(&dev->txq_pend, skb2);
2870 /* throttle TX patch at slower than SUPER SPEED USB */
2871 if ((dev->udev->speed < USB_SPEED_SUPER) &&
2872 (skb_queue_len(&dev->txq_pend) > 10))
2873 netif_stop_queue(net);
2874 } else {
2875 netif_dbg(dev, tx_err, dev->net,
2876 "lan78xx_tx_prep return NULL\n");
2877 dev->net->stats.tx_errors++;
2878 dev->net->stats.tx_dropped++;
2881 tasklet_schedule(&dev->bh);
2883 return NETDEV_TX_OK;
2886 static int
2887 lan78xx_get_endpoints(struct lan78xx_net *dev, struct usb_interface *intf)
2889 int tmp;
2890 struct usb_host_interface *alt = NULL;
2891 struct usb_host_endpoint *in = NULL, *out = NULL;
2892 struct usb_host_endpoint *status = NULL;
2894 for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
2895 unsigned ep;
2897 in = NULL;
2898 out = NULL;
2899 status = NULL;
2900 alt = intf->altsetting + tmp;
2902 for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
2903 struct usb_host_endpoint *e;
2904 int intr = 0;
2906 e = alt->endpoint + ep;
2907 switch (e->desc.bmAttributes) {
2908 case USB_ENDPOINT_XFER_INT:
2909 if (!usb_endpoint_dir_in(&e->desc))
2910 continue;
2911 intr = 1;
2912 /* FALLTHROUGH */
2913 case USB_ENDPOINT_XFER_BULK:
2914 break;
2915 default:
2916 continue;
2918 if (usb_endpoint_dir_in(&e->desc)) {
2919 if (!intr && !in)
2920 in = e;
2921 else if (intr && !status)
2922 status = e;
2923 } else {
2924 if (!out)
2925 out = e;
2928 if (in && out)
2929 break;
2931 if (!alt || !in || !out)
2932 return -EINVAL;
2934 dev->pipe_in = usb_rcvbulkpipe(dev->udev,
2935 in->desc.bEndpointAddress &
2936 USB_ENDPOINT_NUMBER_MASK);
2937 dev->pipe_out = usb_sndbulkpipe(dev->udev,
2938 out->desc.bEndpointAddress &
2939 USB_ENDPOINT_NUMBER_MASK);
2940 dev->ep_intr = status;
2942 return 0;
2945 static int lan78xx_bind(struct lan78xx_net *dev, struct usb_interface *intf)
2947 struct lan78xx_priv *pdata = NULL;
2948 int ret;
2949 int i;
2951 ret = lan78xx_get_endpoints(dev, intf);
2952 if (ret) {
2953 netdev_warn(dev->net, "lan78xx_get_endpoints failed: %d\n",
2954 ret);
2955 return ret;
2958 dev->data[0] = (unsigned long)kzalloc(sizeof(*pdata), GFP_KERNEL);
2960 pdata = (struct lan78xx_priv *)(dev->data[0]);
2961 if (!pdata) {
2962 netdev_warn(dev->net, "Unable to allocate lan78xx_priv");
2963 return -ENOMEM;
2966 pdata->dev = dev;
2968 spin_lock_init(&pdata->rfe_ctl_lock);
2969 mutex_init(&pdata->dataport_mutex);
2971 INIT_WORK(&pdata->set_multicast, lan78xx_deferred_multicast_write);
2973 for (i = 0; i < DP_SEL_VHF_VLAN_LEN; i++)
2974 pdata->vlan_table[i] = 0;
2976 INIT_WORK(&pdata->set_vlan, lan78xx_deferred_vlan_write);
2978 dev->net->features = 0;
2980 if (DEFAULT_TX_CSUM_ENABLE)
2981 dev->net->features |= NETIF_F_HW_CSUM;
2983 if (DEFAULT_RX_CSUM_ENABLE)
2984 dev->net->features |= NETIF_F_RXCSUM;
2986 if (DEFAULT_TSO_CSUM_ENABLE)
2987 dev->net->features |= NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_SG;
2989 if (DEFAULT_VLAN_RX_OFFLOAD)
2990 dev->net->features |= NETIF_F_HW_VLAN_CTAG_RX;
2992 if (DEFAULT_VLAN_FILTER_ENABLE)
2993 dev->net->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
2995 dev->net->hw_features = dev->net->features;
2997 ret = lan78xx_setup_irq_domain(dev);
2998 if (ret < 0) {
2999 netdev_warn(dev->net,
3000 "lan78xx_setup_irq_domain() failed : %d", ret);
3001 goto out1;
3004 dev->net->hard_header_len += TX_OVERHEAD;
3005 dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
3007 /* Init all registers */
3008 ret = lan78xx_reset(dev);
3009 if (ret) {
3010 netdev_warn(dev->net, "Registers INIT FAILED....");
3011 goto out2;
3014 ret = lan78xx_mdio_init(dev);
3015 if (ret) {
3016 netdev_warn(dev->net, "MDIO INIT FAILED.....");
3017 goto out2;
3020 dev->net->flags |= IFF_MULTICAST;
3022 pdata->wol = WAKE_MAGIC;
3024 return ret;
3026 out2:
3027 lan78xx_remove_irq_domain(dev);
3029 out1:
3030 netdev_warn(dev->net, "Bind routine FAILED");
3031 cancel_work_sync(&pdata->set_multicast);
3032 cancel_work_sync(&pdata->set_vlan);
3033 kfree(pdata);
3034 return ret;
3037 static void lan78xx_unbind(struct lan78xx_net *dev, struct usb_interface *intf)
3039 struct lan78xx_priv *pdata = (struct lan78xx_priv *)(dev->data[0]);
3041 lan78xx_remove_irq_domain(dev);
3043 lan78xx_remove_mdio(dev);
3045 if (pdata) {
3046 cancel_work_sync(&pdata->set_multicast);
3047 cancel_work_sync(&pdata->set_vlan);
3048 netif_dbg(dev, ifdown, dev->net, "free pdata");
3049 kfree(pdata);
3050 pdata = NULL;
3051 dev->data[0] = 0;
3055 static void lan78xx_rx_csum_offload(struct lan78xx_net *dev,
3056 struct sk_buff *skb,
3057 u32 rx_cmd_a, u32 rx_cmd_b)
3059 /* HW Checksum offload appears to be flawed if used when not stripping
3060 * VLAN headers. Drop back to S/W checksums under these conditions.
3062 if (!(dev->net->features & NETIF_F_RXCSUM) ||
3063 unlikely(rx_cmd_a & RX_CMD_A_ICSM_) ||
3064 ((rx_cmd_a & RX_CMD_A_FVTG_) &&
3065 !(dev->net->features & NETIF_F_HW_VLAN_CTAG_RX))) {
3066 skb->ip_summed = CHECKSUM_NONE;
3067 } else {
3068 skb->csum = ntohs((u16)(rx_cmd_b >> RX_CMD_B_CSUM_SHIFT_));
3069 skb->ip_summed = CHECKSUM_COMPLETE;
3073 static void lan78xx_rx_vlan_offload(struct lan78xx_net *dev,
3074 struct sk_buff *skb,
3075 u32 rx_cmd_a, u32 rx_cmd_b)
3077 if ((dev->net->features & NETIF_F_HW_VLAN_CTAG_RX) &&
3078 (rx_cmd_a & RX_CMD_A_FVTG_))
3079 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
3080 (rx_cmd_b & 0xffff));
3083 static void lan78xx_skb_return(struct lan78xx_net *dev, struct sk_buff *skb)
3085 int status;
3087 if (test_bit(EVENT_RX_PAUSED, &dev->flags)) {
3088 skb_queue_tail(&dev->rxq_pause, skb);
3089 return;
3092 dev->net->stats.rx_packets++;
3093 dev->net->stats.rx_bytes += skb->len;
3095 skb->protocol = eth_type_trans(skb, dev->net);
3097 netif_dbg(dev, rx_status, dev->net, "< rx, len %zu, type 0x%x\n",
3098 skb->len + sizeof(struct ethhdr), skb->protocol);
3099 memset(skb->cb, 0, sizeof(struct skb_data));
3101 if (skb_defer_rx_timestamp(skb))
3102 return;
3104 status = netif_rx(skb);
3105 if (status != NET_RX_SUCCESS)
3106 netif_dbg(dev, rx_err, dev->net,
3107 "netif_rx status %d\n", status);
3110 static int lan78xx_rx(struct lan78xx_net *dev, struct sk_buff *skb)
3112 if (skb->len < dev->net->hard_header_len)
3113 return 0;
3115 while (skb->len > 0) {
3116 u32 rx_cmd_a, rx_cmd_b, align_count, size;
3117 u16 rx_cmd_c;
3118 struct sk_buff *skb2;
3119 unsigned char *packet;
3121 memcpy(&rx_cmd_a, skb->data, sizeof(rx_cmd_a));
3122 le32_to_cpus(&rx_cmd_a);
3123 skb_pull(skb, sizeof(rx_cmd_a));
3125 memcpy(&rx_cmd_b, skb->data, sizeof(rx_cmd_b));
3126 le32_to_cpus(&rx_cmd_b);
3127 skb_pull(skb, sizeof(rx_cmd_b));
3129 memcpy(&rx_cmd_c, skb->data, sizeof(rx_cmd_c));
3130 le16_to_cpus(&rx_cmd_c);
3131 skb_pull(skb, sizeof(rx_cmd_c));
3133 packet = skb->data;
3135 /* get the packet length */
3136 size = (rx_cmd_a & RX_CMD_A_LEN_MASK_);
3137 align_count = (4 - ((size + RXW_PADDING) % 4)) % 4;
3139 if (unlikely(rx_cmd_a & RX_CMD_A_RED_)) {
3140 netif_dbg(dev, rx_err, dev->net,
3141 "Error rx_cmd_a=0x%08x", rx_cmd_a);
3142 } else {
3143 /* last frame in this batch */
3144 if (skb->len == size) {
3145 lan78xx_rx_csum_offload(dev, skb,
3146 rx_cmd_a, rx_cmd_b);
3147 lan78xx_rx_vlan_offload(dev, skb,
3148 rx_cmd_a, rx_cmd_b);
3150 skb_trim(skb, skb->len - 4); /* remove fcs */
3151 skb->truesize = size + sizeof(struct sk_buff);
3153 return 1;
3156 skb2 = skb_clone(skb, GFP_ATOMIC);
3157 if (unlikely(!skb2)) {
3158 netdev_warn(dev->net, "Error allocating skb");
3159 return 0;
3162 skb2->len = size;
3163 skb2->data = packet;
3164 skb_set_tail_pointer(skb2, size);
3166 lan78xx_rx_csum_offload(dev, skb2, rx_cmd_a, rx_cmd_b);
3167 lan78xx_rx_vlan_offload(dev, skb2, rx_cmd_a, rx_cmd_b);
3169 skb_trim(skb2, skb2->len - 4); /* remove fcs */
3170 skb2->truesize = size + sizeof(struct sk_buff);
3172 lan78xx_skb_return(dev, skb2);
3175 skb_pull(skb, size);
3177 /* padding bytes before the next frame starts */
3178 if (skb->len)
3179 skb_pull(skb, align_count);
3182 return 1;
3185 static inline void rx_process(struct lan78xx_net *dev, struct sk_buff *skb)
3187 if (!lan78xx_rx(dev, skb)) {
3188 dev->net->stats.rx_errors++;
3189 goto done;
3192 if (skb->len) {
3193 lan78xx_skb_return(dev, skb);
3194 return;
3197 netif_dbg(dev, rx_err, dev->net, "drop\n");
3198 dev->net->stats.rx_errors++;
3199 done:
3200 skb_queue_tail(&dev->done, skb);
3203 static void rx_complete(struct urb *urb);
3205 static int rx_submit(struct lan78xx_net *dev, struct urb *urb, gfp_t flags)
3207 struct sk_buff *skb;
3208 struct skb_data *entry;
3209 unsigned long lockflags;
3210 size_t size = dev->rx_urb_size;
3211 int ret = 0;
3213 skb = netdev_alloc_skb_ip_align(dev->net, size);
3214 if (!skb) {
3215 usb_free_urb(urb);
3216 return -ENOMEM;
3219 entry = (struct skb_data *)skb->cb;
3220 entry->urb = urb;
3221 entry->dev = dev;
3222 entry->length = 0;
3224 usb_fill_bulk_urb(urb, dev->udev, dev->pipe_in,
3225 skb->data, size, rx_complete, skb);
3227 spin_lock_irqsave(&dev->rxq.lock, lockflags);
3229 if (netif_device_present(dev->net) &&
3230 netif_running(dev->net) &&
3231 !test_bit(EVENT_RX_HALT, &dev->flags) &&
3232 !test_bit(EVENT_DEV_ASLEEP, &dev->flags)) {
3233 ret = usb_submit_urb(urb, GFP_ATOMIC);
3234 switch (ret) {
3235 case 0:
3236 lan78xx_queue_skb(&dev->rxq, skb, rx_start);
3237 break;
3238 case -EPIPE:
3239 lan78xx_defer_kevent(dev, EVENT_RX_HALT);
3240 break;
3241 case -ENODEV:
3242 netif_dbg(dev, ifdown, dev->net, "device gone\n");
3243 netif_device_detach(dev->net);
3244 break;
3245 case -EHOSTUNREACH:
3246 ret = -ENOLINK;
3247 break;
3248 default:
3249 netif_dbg(dev, rx_err, dev->net,
3250 "rx submit, %d\n", ret);
3251 tasklet_schedule(&dev->bh);
3253 } else {
3254 netif_dbg(dev, ifdown, dev->net, "rx: stopped\n");
3255 ret = -ENOLINK;
3257 spin_unlock_irqrestore(&dev->rxq.lock, lockflags);
3258 if (ret) {
3259 dev_kfree_skb_any(skb);
3260 usb_free_urb(urb);
3262 return ret;
3265 static void rx_complete(struct urb *urb)
3267 struct sk_buff *skb = (struct sk_buff *)urb->context;
3268 struct skb_data *entry = (struct skb_data *)skb->cb;
3269 struct lan78xx_net *dev = entry->dev;
3270 int urb_status = urb->status;
3271 enum skb_state state;
3273 skb_put(skb, urb->actual_length);
3274 state = rx_done;
3275 entry->urb = NULL;
3277 switch (urb_status) {
3278 case 0:
3279 if (skb->len < dev->net->hard_header_len) {
3280 state = rx_cleanup;
3281 dev->net->stats.rx_errors++;
3282 dev->net->stats.rx_length_errors++;
3283 netif_dbg(dev, rx_err, dev->net,
3284 "rx length %d\n", skb->len);
3286 usb_mark_last_busy(dev->udev);
3287 break;
3288 case -EPIPE:
3289 dev->net->stats.rx_errors++;
3290 lan78xx_defer_kevent(dev, EVENT_RX_HALT);
3291 /* FALLTHROUGH */
3292 case -ECONNRESET: /* async unlink */
3293 case -ESHUTDOWN: /* hardware gone */
3294 netif_dbg(dev, ifdown, dev->net,
3295 "rx shutdown, code %d\n", urb_status);
3296 state = rx_cleanup;
3297 entry->urb = urb;
3298 urb = NULL;
3299 break;
3300 case -EPROTO:
3301 case -ETIME:
3302 case -EILSEQ:
3303 dev->net->stats.rx_errors++;
3304 state = rx_cleanup;
3305 entry->urb = urb;
3306 urb = NULL;
3307 break;
3309 /* data overrun ... flush fifo? */
3310 case -EOVERFLOW:
3311 dev->net->stats.rx_over_errors++;
3312 /* FALLTHROUGH */
3314 default:
3315 state = rx_cleanup;
3316 dev->net->stats.rx_errors++;
3317 netif_dbg(dev, rx_err, dev->net, "rx status %d\n", urb_status);
3318 break;
3321 state = defer_bh(dev, skb, &dev->rxq, state);
3323 if (urb) {
3324 if (netif_running(dev->net) &&
3325 !test_bit(EVENT_RX_HALT, &dev->flags) &&
3326 state != unlink_start) {
3327 rx_submit(dev, urb, GFP_ATOMIC);
3328 return;
3330 usb_free_urb(urb);
3332 netif_dbg(dev, rx_err, dev->net, "no read resubmitted\n");
3335 static void lan78xx_tx_bh(struct lan78xx_net *dev)
3337 int length;
3338 struct urb *urb = NULL;
3339 struct skb_data *entry;
3340 unsigned long flags;
3341 struct sk_buff_head *tqp = &dev->txq_pend;
3342 struct sk_buff *skb, *skb2;
3343 int ret;
3344 int count, pos;
3345 int skb_totallen, pkt_cnt;
3347 skb_totallen = 0;
3348 pkt_cnt = 0;
3349 count = 0;
3350 length = 0;
3351 spin_lock_irqsave(&tqp->lock, flags);
3352 for (skb = tqp->next; pkt_cnt < tqp->qlen; skb = skb->next) {
3353 if (skb_is_gso(skb)) {
3354 if (pkt_cnt) {
3355 /* handle previous packets first */
3356 break;
3358 count = 1;
3359 length = skb->len - TX_OVERHEAD;
3360 __skb_unlink(skb, tqp);
3361 spin_unlock_irqrestore(&tqp->lock, flags);
3362 goto gso_skb;
3365 if ((skb_totallen + skb->len) > MAX_SINGLE_PACKET_SIZE)
3366 break;
3367 skb_totallen = skb->len + roundup(skb_totallen, sizeof(u32));
3368 pkt_cnt++;
3370 spin_unlock_irqrestore(&tqp->lock, flags);
3372 /* copy to a single skb */
3373 skb = alloc_skb(skb_totallen, GFP_ATOMIC);
3374 if (!skb)
3375 goto drop;
3377 skb_put(skb, skb_totallen);
3379 for (count = pos = 0; count < pkt_cnt; count++) {
3380 skb2 = skb_dequeue(tqp);
3381 if (skb2) {
3382 length += (skb2->len - TX_OVERHEAD);
3383 memcpy(skb->data + pos, skb2->data, skb2->len);
3384 pos += roundup(skb2->len, sizeof(u32));
3385 dev_kfree_skb(skb2);
3389 gso_skb:
3390 urb = usb_alloc_urb(0, GFP_ATOMIC);
3391 if (!urb)
3392 goto drop;
3394 entry = (struct skb_data *)skb->cb;
3395 entry->urb = urb;
3396 entry->dev = dev;
3397 entry->length = length;
3398 entry->num_of_packet = count;
3400 spin_lock_irqsave(&dev->txq.lock, flags);
3401 ret = usb_autopm_get_interface_async(dev->intf);
3402 if (ret < 0) {
3403 spin_unlock_irqrestore(&dev->txq.lock, flags);
3404 goto drop;
3407 usb_fill_bulk_urb(urb, dev->udev, dev->pipe_out,
3408 skb->data, skb->len, tx_complete, skb);
3410 if (length % dev->maxpacket == 0) {
3411 /* send USB_ZERO_PACKET */
3412 urb->transfer_flags |= URB_ZERO_PACKET;
3415 #ifdef CONFIG_PM
3416 /* if this triggers the device is still a sleep */
3417 if (test_bit(EVENT_DEV_ASLEEP, &dev->flags)) {
3418 /* transmission will be done in resume */
3419 usb_anchor_urb(urb, &dev->deferred);
3420 /* no use to process more packets */
3421 netif_stop_queue(dev->net);
3422 usb_put_urb(urb);
3423 spin_unlock_irqrestore(&dev->txq.lock, flags);
3424 netdev_dbg(dev->net, "Delaying transmission for resumption\n");
3425 return;
3427 #endif
3429 ret = usb_submit_urb(urb, GFP_ATOMIC);
3430 switch (ret) {
3431 case 0:
3432 netif_trans_update(dev->net);
3433 lan78xx_queue_skb(&dev->txq, skb, tx_start);
3434 if (skb_queue_len(&dev->txq) >= dev->tx_qlen)
3435 netif_stop_queue(dev->net);
3436 break;
3437 case -EPIPE:
3438 netif_stop_queue(dev->net);
3439 lan78xx_defer_kevent(dev, EVENT_TX_HALT);
3440 usb_autopm_put_interface_async(dev->intf);
3441 break;
3442 default:
3443 usb_autopm_put_interface_async(dev->intf);
3444 netif_dbg(dev, tx_err, dev->net,
3445 "tx: submit urb err %d\n", ret);
3446 break;
3449 spin_unlock_irqrestore(&dev->txq.lock, flags);
3451 if (ret) {
3452 netif_dbg(dev, tx_err, dev->net, "drop, code %d\n", ret);
3453 drop:
3454 dev->net->stats.tx_dropped++;
3455 if (skb)
3456 dev_kfree_skb_any(skb);
3457 usb_free_urb(urb);
3458 } else
3459 netif_dbg(dev, tx_queued, dev->net,
3460 "> tx, len %d, type 0x%x\n", length, skb->protocol);
3463 static void lan78xx_rx_bh(struct lan78xx_net *dev)
3465 struct urb *urb;
3466 int i;
3468 if (skb_queue_len(&dev->rxq) < dev->rx_qlen) {
3469 for (i = 0; i < 10; i++) {
3470 if (skb_queue_len(&dev->rxq) >= dev->rx_qlen)
3471 break;
3472 urb = usb_alloc_urb(0, GFP_ATOMIC);
3473 if (urb)
3474 if (rx_submit(dev, urb, GFP_ATOMIC) == -ENOLINK)
3475 return;
3478 if (skb_queue_len(&dev->rxq) < dev->rx_qlen)
3479 tasklet_schedule(&dev->bh);
3481 if (skb_queue_len(&dev->txq) < dev->tx_qlen)
3482 netif_wake_queue(dev->net);
3485 static void lan78xx_bh(unsigned long param)
3487 struct lan78xx_net *dev = (struct lan78xx_net *)param;
3488 struct sk_buff *skb;
3489 struct skb_data *entry;
3491 while ((skb = skb_dequeue(&dev->done))) {
3492 entry = (struct skb_data *)(skb->cb);
3493 switch (entry->state) {
3494 case rx_done:
3495 entry->state = rx_cleanup;
3496 rx_process(dev, skb);
3497 continue;
3498 case tx_done:
3499 usb_free_urb(entry->urb);
3500 dev_kfree_skb(skb);
3501 continue;
3502 case rx_cleanup:
3503 usb_free_urb(entry->urb);
3504 dev_kfree_skb(skb);
3505 continue;
3506 default:
3507 netdev_dbg(dev->net, "skb state %d\n", entry->state);
3508 return;
3512 if (netif_device_present(dev->net) && netif_running(dev->net)) {
3513 /* reset update timer delta */
3514 if (timer_pending(&dev->stat_monitor) && (dev->delta != 1)) {
3515 dev->delta = 1;
3516 mod_timer(&dev->stat_monitor,
3517 jiffies + STAT_UPDATE_TIMER);
3520 if (!skb_queue_empty(&dev->txq_pend))
3521 lan78xx_tx_bh(dev);
3523 if (!timer_pending(&dev->delay) &&
3524 !test_bit(EVENT_RX_HALT, &dev->flags))
3525 lan78xx_rx_bh(dev);
3529 static void lan78xx_delayedwork(struct work_struct *work)
3531 int status;
3532 struct lan78xx_net *dev;
3534 dev = container_of(work, struct lan78xx_net, wq.work);
3536 if (test_bit(EVENT_TX_HALT, &dev->flags)) {
3537 unlink_urbs(dev, &dev->txq);
3538 status = usb_autopm_get_interface(dev->intf);
3539 if (status < 0)
3540 goto fail_pipe;
3541 status = usb_clear_halt(dev->udev, dev->pipe_out);
3542 usb_autopm_put_interface(dev->intf);
3543 if (status < 0 &&
3544 status != -EPIPE &&
3545 status != -ESHUTDOWN) {
3546 if (netif_msg_tx_err(dev))
3547 fail_pipe:
3548 netdev_err(dev->net,
3549 "can't clear tx halt, status %d\n",
3550 status);
3551 } else {
3552 clear_bit(EVENT_TX_HALT, &dev->flags);
3553 if (status != -ESHUTDOWN)
3554 netif_wake_queue(dev->net);
3557 if (test_bit(EVENT_RX_HALT, &dev->flags)) {
3558 unlink_urbs(dev, &dev->rxq);
3559 status = usb_autopm_get_interface(dev->intf);
3560 if (status < 0)
3561 goto fail_halt;
3562 status = usb_clear_halt(dev->udev, dev->pipe_in);
3563 usb_autopm_put_interface(dev->intf);
3564 if (status < 0 &&
3565 status != -EPIPE &&
3566 status != -ESHUTDOWN) {
3567 if (netif_msg_rx_err(dev))
3568 fail_halt:
3569 netdev_err(dev->net,
3570 "can't clear rx halt, status %d\n",
3571 status);
3572 } else {
3573 clear_bit(EVENT_RX_HALT, &dev->flags);
3574 tasklet_schedule(&dev->bh);
3578 if (test_bit(EVENT_LINK_RESET, &dev->flags)) {
3579 int ret = 0;
3581 clear_bit(EVENT_LINK_RESET, &dev->flags);
3582 status = usb_autopm_get_interface(dev->intf);
3583 if (status < 0)
3584 goto skip_reset;
3585 if (lan78xx_link_reset(dev) < 0) {
3586 usb_autopm_put_interface(dev->intf);
3587 skip_reset:
3588 netdev_info(dev->net, "link reset failed (%d)\n",
3589 ret);
3590 } else {
3591 usb_autopm_put_interface(dev->intf);
3595 if (test_bit(EVENT_STAT_UPDATE, &dev->flags)) {
3596 lan78xx_update_stats(dev);
3598 clear_bit(EVENT_STAT_UPDATE, &dev->flags);
3600 mod_timer(&dev->stat_monitor,
3601 jiffies + (STAT_UPDATE_TIMER * dev->delta));
3603 dev->delta = min((dev->delta * 2), 50);
3607 static void intr_complete(struct urb *urb)
3609 struct lan78xx_net *dev = urb->context;
3610 int status = urb->status;
3612 switch (status) {
3613 /* success */
3614 case 0:
3615 lan78xx_status(dev, urb);
3616 break;
3618 /* software-driven interface shutdown */
3619 case -ENOENT: /* urb killed */
3620 case -ESHUTDOWN: /* hardware gone */
3621 netif_dbg(dev, ifdown, dev->net,
3622 "intr shutdown, code %d\n", status);
3623 return;
3625 /* NOTE: not throttling like RX/TX, since this endpoint
3626 * already polls infrequently
3628 default:
3629 netdev_dbg(dev->net, "intr status %d\n", status);
3630 break;
3633 if (!netif_running(dev->net))
3634 return;
3636 memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
3637 status = usb_submit_urb(urb, GFP_ATOMIC);
3638 if (status != 0)
3639 netif_err(dev, timer, dev->net,
3640 "intr resubmit --> %d\n", status);
3643 static void lan78xx_disconnect(struct usb_interface *intf)
3645 struct lan78xx_net *dev;
3646 struct usb_device *udev;
3647 struct net_device *net;
3648 struct phy_device *phydev;
3650 dev = usb_get_intfdata(intf);
3651 usb_set_intfdata(intf, NULL);
3652 if (!dev)
3653 return;
3655 udev = interface_to_usbdev(intf);
3656 net = dev->net;
3657 phydev = net->phydev;
3659 phy_unregister_fixup_for_uid(PHY_KSZ9031RNX, 0xfffffff0);
3660 phy_unregister_fixup_for_uid(PHY_LAN8835, 0xfffffff0);
3662 phy_disconnect(net->phydev);
3664 if (phy_is_pseudo_fixed_link(phydev))
3665 fixed_phy_unregister(phydev);
3667 unregister_netdev(net);
3669 cancel_delayed_work_sync(&dev->wq);
3671 usb_scuttle_anchored_urbs(&dev->deferred);
3673 lan78xx_unbind(dev, intf);
3675 usb_kill_urb(dev->urb_intr);
3676 usb_free_urb(dev->urb_intr);
3678 free_netdev(net);
3679 usb_put_dev(udev);
3682 static void lan78xx_tx_timeout(struct net_device *net)
3684 struct lan78xx_net *dev = netdev_priv(net);
3686 unlink_urbs(dev, &dev->txq);
3687 tasklet_schedule(&dev->bh);
3690 static netdev_features_t lan78xx_features_check(struct sk_buff *skb,
3691 struct net_device *netdev,
3692 netdev_features_t features)
3694 if (skb->len + TX_OVERHEAD > MAX_SINGLE_PACKET_SIZE)
3695 features &= ~NETIF_F_GSO_MASK;
3697 features = vlan_features_check(skb, features);
3698 features = vxlan_features_check(skb, features);
3700 return features;
3703 static const struct net_device_ops lan78xx_netdev_ops = {
3704 .ndo_open = lan78xx_open,
3705 .ndo_stop = lan78xx_stop,
3706 .ndo_start_xmit = lan78xx_start_xmit,
3707 .ndo_tx_timeout = lan78xx_tx_timeout,
3708 .ndo_change_mtu = lan78xx_change_mtu,
3709 .ndo_set_mac_address = lan78xx_set_mac_addr,
3710 .ndo_validate_addr = eth_validate_addr,
3711 .ndo_do_ioctl = lan78xx_ioctl,
3712 .ndo_set_rx_mode = lan78xx_set_multicast,
3713 .ndo_set_features = lan78xx_set_features,
3714 .ndo_vlan_rx_add_vid = lan78xx_vlan_rx_add_vid,
3715 .ndo_vlan_rx_kill_vid = lan78xx_vlan_rx_kill_vid,
3716 .ndo_features_check = lan78xx_features_check,
3719 static void lan78xx_stat_monitor(struct timer_list *t)
3721 struct lan78xx_net *dev = from_timer(dev, t, stat_monitor);
3723 lan78xx_defer_kevent(dev, EVENT_STAT_UPDATE);
3726 static int lan78xx_probe(struct usb_interface *intf,
3727 const struct usb_device_id *id)
3729 struct lan78xx_net *dev;
3730 struct net_device *netdev;
3731 struct usb_device *udev;
3732 int ret;
3733 unsigned maxp;
3734 unsigned period;
3735 u8 *buf = NULL;
3737 udev = interface_to_usbdev(intf);
3738 udev = usb_get_dev(udev);
3740 netdev = alloc_etherdev(sizeof(struct lan78xx_net));
3741 if (!netdev) {
3742 dev_err(&intf->dev, "Error: OOM\n");
3743 ret = -ENOMEM;
3744 goto out1;
3747 /* netdev_printk() needs this */
3748 SET_NETDEV_DEV(netdev, &intf->dev);
3750 dev = netdev_priv(netdev);
3751 dev->udev = udev;
3752 dev->intf = intf;
3753 dev->net = netdev;
3754 dev->msg_enable = netif_msg_init(msg_level, NETIF_MSG_DRV
3755 | NETIF_MSG_PROBE | NETIF_MSG_LINK);
3757 skb_queue_head_init(&dev->rxq);
3758 skb_queue_head_init(&dev->txq);
3759 skb_queue_head_init(&dev->done);
3760 skb_queue_head_init(&dev->rxq_pause);
3761 skb_queue_head_init(&dev->txq_pend);
3762 mutex_init(&dev->phy_mutex);
3764 tasklet_init(&dev->bh, lan78xx_bh, (unsigned long)dev);
3765 INIT_DELAYED_WORK(&dev->wq, lan78xx_delayedwork);
3766 init_usb_anchor(&dev->deferred);
3768 netdev->netdev_ops = &lan78xx_netdev_ops;
3769 netdev->watchdog_timeo = TX_TIMEOUT_JIFFIES;
3770 netdev->ethtool_ops = &lan78xx_ethtool_ops;
3772 dev->delta = 1;
3773 timer_setup(&dev->stat_monitor, lan78xx_stat_monitor, 0);
3775 mutex_init(&dev->stats.access_lock);
3777 ret = lan78xx_bind(dev, intf);
3778 if (ret < 0)
3779 goto out2;
3780 strcpy(netdev->name, "eth%d");
3782 if (netdev->mtu > (dev->hard_mtu - netdev->hard_header_len))
3783 netdev->mtu = dev->hard_mtu - netdev->hard_header_len;
3785 /* MTU range: 68 - 9000 */
3786 netdev->max_mtu = MAX_SINGLE_PACKET_SIZE;
3787 netif_set_gso_max_size(netdev, MAX_SINGLE_PACKET_SIZE - MAX_HEADER);
3789 dev->ep_blkin = (intf->cur_altsetting)->endpoint + 0;
3790 dev->ep_blkout = (intf->cur_altsetting)->endpoint + 1;
3791 dev->ep_intr = (intf->cur_altsetting)->endpoint + 2;
3793 dev->pipe_in = usb_rcvbulkpipe(udev, BULK_IN_PIPE);
3794 dev->pipe_out = usb_sndbulkpipe(udev, BULK_OUT_PIPE);
3796 dev->pipe_intr = usb_rcvintpipe(dev->udev,
3797 dev->ep_intr->desc.bEndpointAddress &
3798 USB_ENDPOINT_NUMBER_MASK);
3799 period = dev->ep_intr->desc.bInterval;
3801 maxp = usb_maxpacket(dev->udev, dev->pipe_intr, 0);
3802 buf = kmalloc(maxp, GFP_KERNEL);
3803 if (buf) {
3804 dev->urb_intr = usb_alloc_urb(0, GFP_KERNEL);
3805 if (!dev->urb_intr) {
3806 ret = -ENOMEM;
3807 kfree(buf);
3808 goto out3;
3809 } else {
3810 usb_fill_int_urb(dev->urb_intr, dev->udev,
3811 dev->pipe_intr, buf, maxp,
3812 intr_complete, dev, period);
3816 dev->maxpacket = usb_maxpacket(dev->udev, dev->pipe_out, 1);
3818 /* driver requires remote-wakeup capability during autosuspend. */
3819 intf->needs_remote_wakeup = 1;
3821 ret = lan78xx_phy_init(dev);
3822 if (ret < 0)
3823 goto out4;
3825 ret = register_netdev(netdev);
3826 if (ret != 0) {
3827 netif_err(dev, probe, netdev, "couldn't register the device\n");
3828 goto out5;
3831 usb_set_intfdata(intf, dev);
3833 ret = device_set_wakeup_enable(&udev->dev, true);
3835 /* Default delay of 2sec has more overhead than advantage.
3836 * Set to 10sec as default.
3838 pm_runtime_set_autosuspend_delay(&udev->dev,
3839 DEFAULT_AUTOSUSPEND_DELAY);
3841 return 0;
3843 out5:
3844 phy_disconnect(netdev->phydev);
3845 out4:
3846 usb_free_urb(dev->urb_intr);
3847 out3:
3848 lan78xx_unbind(dev, intf);
3849 out2:
3850 free_netdev(netdev);
3851 out1:
3852 usb_put_dev(udev);
3854 return ret;
3857 static u16 lan78xx_wakeframe_crc16(const u8 *buf, int len)
3859 const u16 crc16poly = 0x8005;
3860 int i;
3861 u16 bit, crc, msb;
3862 u8 data;
3864 crc = 0xFFFF;
3865 for (i = 0; i < len; i++) {
3866 data = *buf++;
3867 for (bit = 0; bit < 8; bit++) {
3868 msb = crc >> 15;
3869 crc <<= 1;
3871 if (msb ^ (u16)(data & 1)) {
3872 crc ^= crc16poly;
3873 crc |= (u16)0x0001U;
3875 data >>= 1;
3879 return crc;
3882 static int lan78xx_set_suspend(struct lan78xx_net *dev, u32 wol)
3884 u32 buf;
3885 int ret;
3886 int mask_index;
3887 u16 crc;
3888 u32 temp_wucsr;
3889 u32 temp_pmt_ctl;
3890 const u8 ipv4_multicast[3] = { 0x01, 0x00, 0x5E };
3891 const u8 ipv6_multicast[3] = { 0x33, 0x33 };
3892 const u8 arp_type[2] = { 0x08, 0x06 };
3894 ret = lan78xx_read_reg(dev, MAC_TX, &buf);
3895 buf &= ~MAC_TX_TXEN_;
3896 ret = lan78xx_write_reg(dev, MAC_TX, buf);
3897 ret = lan78xx_read_reg(dev, MAC_RX, &buf);
3898 buf &= ~MAC_RX_RXEN_;
3899 ret = lan78xx_write_reg(dev, MAC_RX, buf);
3901 ret = lan78xx_write_reg(dev, WUCSR, 0);
3902 ret = lan78xx_write_reg(dev, WUCSR2, 0);
3903 ret = lan78xx_write_reg(dev, WK_SRC, 0xFFF1FF1FUL);
3905 temp_wucsr = 0;
3907 temp_pmt_ctl = 0;
3908 ret = lan78xx_read_reg(dev, PMT_CTL, &temp_pmt_ctl);
3909 temp_pmt_ctl &= ~PMT_CTL_RES_CLR_WKP_EN_;
3910 temp_pmt_ctl |= PMT_CTL_RES_CLR_WKP_STS_;
3912 for (mask_index = 0; mask_index < NUM_OF_WUF_CFG; mask_index++)
3913 ret = lan78xx_write_reg(dev, WUF_CFG(mask_index), 0);
3915 mask_index = 0;
3916 if (wol & WAKE_PHY) {
3917 temp_pmt_ctl |= PMT_CTL_PHY_WAKE_EN_;
3919 temp_pmt_ctl |= PMT_CTL_WOL_EN_;
3920 temp_pmt_ctl &= ~PMT_CTL_SUS_MODE_MASK_;
3921 temp_pmt_ctl |= PMT_CTL_SUS_MODE_0_;
3923 if (wol & WAKE_MAGIC) {
3924 temp_wucsr |= WUCSR_MPEN_;
3926 temp_pmt_ctl |= PMT_CTL_WOL_EN_;
3927 temp_pmt_ctl &= ~PMT_CTL_SUS_MODE_MASK_;
3928 temp_pmt_ctl |= PMT_CTL_SUS_MODE_3_;
3930 if (wol & WAKE_BCAST) {
3931 temp_wucsr |= WUCSR_BCST_EN_;
3933 temp_pmt_ctl |= PMT_CTL_WOL_EN_;
3934 temp_pmt_ctl &= ~PMT_CTL_SUS_MODE_MASK_;
3935 temp_pmt_ctl |= PMT_CTL_SUS_MODE_0_;
3937 if (wol & WAKE_MCAST) {
3938 temp_wucsr |= WUCSR_WAKE_EN_;
3940 /* set WUF_CFG & WUF_MASK for IPv4 Multicast */
3941 crc = lan78xx_wakeframe_crc16(ipv4_multicast, 3);
3942 ret = lan78xx_write_reg(dev, WUF_CFG(mask_index),
3943 WUF_CFGX_EN_ |
3944 WUF_CFGX_TYPE_MCAST_ |
3945 (0 << WUF_CFGX_OFFSET_SHIFT_) |
3946 (crc & WUF_CFGX_CRC16_MASK_));
3948 ret = lan78xx_write_reg(dev, WUF_MASK0(mask_index), 7);
3949 ret = lan78xx_write_reg(dev, WUF_MASK1(mask_index), 0);
3950 ret = lan78xx_write_reg(dev, WUF_MASK2(mask_index), 0);
3951 ret = lan78xx_write_reg(dev, WUF_MASK3(mask_index), 0);
3952 mask_index++;
3954 /* for IPv6 Multicast */
3955 crc = lan78xx_wakeframe_crc16(ipv6_multicast, 2);
3956 ret = lan78xx_write_reg(dev, WUF_CFG(mask_index),
3957 WUF_CFGX_EN_ |
3958 WUF_CFGX_TYPE_MCAST_ |
3959 (0 << WUF_CFGX_OFFSET_SHIFT_) |
3960 (crc & WUF_CFGX_CRC16_MASK_));
3962 ret = lan78xx_write_reg(dev, WUF_MASK0(mask_index), 3);
3963 ret = lan78xx_write_reg(dev, WUF_MASK1(mask_index), 0);
3964 ret = lan78xx_write_reg(dev, WUF_MASK2(mask_index), 0);
3965 ret = lan78xx_write_reg(dev, WUF_MASK3(mask_index), 0);
3966 mask_index++;
3968 temp_pmt_ctl |= PMT_CTL_WOL_EN_;
3969 temp_pmt_ctl &= ~PMT_CTL_SUS_MODE_MASK_;
3970 temp_pmt_ctl |= PMT_CTL_SUS_MODE_0_;
3972 if (wol & WAKE_UCAST) {
3973 temp_wucsr |= WUCSR_PFDA_EN_;
3975 temp_pmt_ctl |= PMT_CTL_WOL_EN_;
3976 temp_pmt_ctl &= ~PMT_CTL_SUS_MODE_MASK_;
3977 temp_pmt_ctl |= PMT_CTL_SUS_MODE_0_;
3979 if (wol & WAKE_ARP) {
3980 temp_wucsr |= WUCSR_WAKE_EN_;
3982 /* set WUF_CFG & WUF_MASK
3983 * for packettype (offset 12,13) = ARP (0x0806)
3985 crc = lan78xx_wakeframe_crc16(arp_type, 2);
3986 ret = lan78xx_write_reg(dev, WUF_CFG(mask_index),
3987 WUF_CFGX_EN_ |
3988 WUF_CFGX_TYPE_ALL_ |
3989 (0 << WUF_CFGX_OFFSET_SHIFT_) |
3990 (crc & WUF_CFGX_CRC16_MASK_));
3992 ret = lan78xx_write_reg(dev, WUF_MASK0(mask_index), 0x3000);
3993 ret = lan78xx_write_reg(dev, WUF_MASK1(mask_index), 0);
3994 ret = lan78xx_write_reg(dev, WUF_MASK2(mask_index), 0);
3995 ret = lan78xx_write_reg(dev, WUF_MASK3(mask_index), 0);
3996 mask_index++;
3998 temp_pmt_ctl |= PMT_CTL_WOL_EN_;
3999 temp_pmt_ctl &= ~PMT_CTL_SUS_MODE_MASK_;
4000 temp_pmt_ctl |= PMT_CTL_SUS_MODE_0_;
4003 ret = lan78xx_write_reg(dev, WUCSR, temp_wucsr);
4005 /* when multiple WOL bits are set */
4006 if (hweight_long((unsigned long)wol) > 1) {
4007 temp_pmt_ctl |= PMT_CTL_WOL_EN_;
4008 temp_pmt_ctl &= ~PMT_CTL_SUS_MODE_MASK_;
4009 temp_pmt_ctl |= PMT_CTL_SUS_MODE_0_;
4011 ret = lan78xx_write_reg(dev, PMT_CTL, temp_pmt_ctl);
4013 /* clear WUPS */
4014 ret = lan78xx_read_reg(dev, PMT_CTL, &buf);
4015 buf |= PMT_CTL_WUPS_MASK_;
4016 ret = lan78xx_write_reg(dev, PMT_CTL, buf);
4018 ret = lan78xx_read_reg(dev, MAC_RX, &buf);
4019 buf |= MAC_RX_RXEN_;
4020 ret = lan78xx_write_reg(dev, MAC_RX, buf);
4022 return 0;
4025 static int lan78xx_suspend(struct usb_interface *intf, pm_message_t message)
4027 struct lan78xx_net *dev = usb_get_intfdata(intf);
4028 struct lan78xx_priv *pdata = (struct lan78xx_priv *)(dev->data[0]);
4029 u32 buf;
4030 int ret;
4031 int event;
4033 event = message.event;
4035 if (!dev->suspend_count++) {
4036 spin_lock_irq(&dev->txq.lock);
4037 /* don't autosuspend while transmitting */
4038 if ((skb_queue_len(&dev->txq) ||
4039 skb_queue_len(&dev->txq_pend)) &&
4040 PMSG_IS_AUTO(message)) {
4041 spin_unlock_irq(&dev->txq.lock);
4042 ret = -EBUSY;
4043 goto out;
4044 } else {
4045 set_bit(EVENT_DEV_ASLEEP, &dev->flags);
4046 spin_unlock_irq(&dev->txq.lock);
4049 /* stop TX & RX */
4050 ret = lan78xx_read_reg(dev, MAC_TX, &buf);
4051 buf &= ~MAC_TX_TXEN_;
4052 ret = lan78xx_write_reg(dev, MAC_TX, buf);
4053 ret = lan78xx_read_reg(dev, MAC_RX, &buf);
4054 buf &= ~MAC_RX_RXEN_;
4055 ret = lan78xx_write_reg(dev, MAC_RX, buf);
4057 /* empty out the rx and queues */
4058 netif_device_detach(dev->net);
4059 lan78xx_terminate_urbs(dev);
4060 usb_kill_urb(dev->urb_intr);
4062 /* reattach */
4063 netif_device_attach(dev->net);
4066 if (test_bit(EVENT_DEV_ASLEEP, &dev->flags)) {
4067 del_timer(&dev->stat_monitor);
4069 if (PMSG_IS_AUTO(message)) {
4070 /* auto suspend (selective suspend) */
4071 ret = lan78xx_read_reg(dev, MAC_TX, &buf);
4072 buf &= ~MAC_TX_TXEN_;
4073 ret = lan78xx_write_reg(dev, MAC_TX, buf);
4074 ret = lan78xx_read_reg(dev, MAC_RX, &buf);
4075 buf &= ~MAC_RX_RXEN_;
4076 ret = lan78xx_write_reg(dev, MAC_RX, buf);
4078 ret = lan78xx_write_reg(dev, WUCSR, 0);
4079 ret = lan78xx_write_reg(dev, WUCSR2, 0);
4080 ret = lan78xx_write_reg(dev, WK_SRC, 0xFFF1FF1FUL);
4082 /* set goodframe wakeup */
4083 ret = lan78xx_read_reg(dev, WUCSR, &buf);
4085 buf |= WUCSR_RFE_WAKE_EN_;
4086 buf |= WUCSR_STORE_WAKE_;
4088 ret = lan78xx_write_reg(dev, WUCSR, buf);
4090 ret = lan78xx_read_reg(dev, PMT_CTL, &buf);
4092 buf &= ~PMT_CTL_RES_CLR_WKP_EN_;
4093 buf |= PMT_CTL_RES_CLR_WKP_STS_;
4095 buf |= PMT_CTL_PHY_WAKE_EN_;
4096 buf |= PMT_CTL_WOL_EN_;
4097 buf &= ~PMT_CTL_SUS_MODE_MASK_;
4098 buf |= PMT_CTL_SUS_MODE_3_;
4100 ret = lan78xx_write_reg(dev, PMT_CTL, buf);
4102 ret = lan78xx_read_reg(dev, PMT_CTL, &buf);
4104 buf |= PMT_CTL_WUPS_MASK_;
4106 ret = lan78xx_write_reg(dev, PMT_CTL, buf);
4108 ret = lan78xx_read_reg(dev, MAC_RX, &buf);
4109 buf |= MAC_RX_RXEN_;
4110 ret = lan78xx_write_reg(dev, MAC_RX, buf);
4111 } else {
4112 lan78xx_set_suspend(dev, pdata->wol);
4116 ret = 0;
4117 out:
4118 return ret;
4121 static int lan78xx_resume(struct usb_interface *intf)
4123 struct lan78xx_net *dev = usb_get_intfdata(intf);
4124 struct sk_buff *skb;
4125 struct urb *res;
4126 int ret;
4127 u32 buf;
4129 if (!timer_pending(&dev->stat_monitor)) {
4130 dev->delta = 1;
4131 mod_timer(&dev->stat_monitor,
4132 jiffies + STAT_UPDATE_TIMER);
4135 if (!--dev->suspend_count) {
4136 /* resume interrupt URBs */
4137 if (dev->urb_intr && test_bit(EVENT_DEV_OPEN, &dev->flags))
4138 usb_submit_urb(dev->urb_intr, GFP_NOIO);
4140 spin_lock_irq(&dev->txq.lock);
4141 while ((res = usb_get_from_anchor(&dev->deferred))) {
4142 skb = (struct sk_buff *)res->context;
4143 ret = usb_submit_urb(res, GFP_ATOMIC);
4144 if (ret < 0) {
4145 dev_kfree_skb_any(skb);
4146 usb_free_urb(res);
4147 usb_autopm_put_interface_async(dev->intf);
4148 } else {
4149 netif_trans_update(dev->net);
4150 lan78xx_queue_skb(&dev->txq, skb, tx_start);
4154 clear_bit(EVENT_DEV_ASLEEP, &dev->flags);
4155 spin_unlock_irq(&dev->txq.lock);
4157 if (test_bit(EVENT_DEV_OPEN, &dev->flags)) {
4158 if (!(skb_queue_len(&dev->txq) >= dev->tx_qlen))
4159 netif_start_queue(dev->net);
4160 tasklet_schedule(&dev->bh);
4164 ret = lan78xx_write_reg(dev, WUCSR2, 0);
4165 ret = lan78xx_write_reg(dev, WUCSR, 0);
4166 ret = lan78xx_write_reg(dev, WK_SRC, 0xFFF1FF1FUL);
4168 ret = lan78xx_write_reg(dev, WUCSR2, WUCSR2_NS_RCD_ |
4169 WUCSR2_ARP_RCD_ |
4170 WUCSR2_IPV6_TCPSYN_RCD_ |
4171 WUCSR2_IPV4_TCPSYN_RCD_);
4173 ret = lan78xx_write_reg(dev, WUCSR, WUCSR_EEE_TX_WAKE_ |
4174 WUCSR_EEE_RX_WAKE_ |
4175 WUCSR_PFDA_FR_ |
4176 WUCSR_RFE_WAKE_FR_ |
4177 WUCSR_WUFR_ |
4178 WUCSR_MPR_ |
4179 WUCSR_BCST_FR_);
4181 ret = lan78xx_read_reg(dev, MAC_TX, &buf);
4182 buf |= MAC_TX_TXEN_;
4183 ret = lan78xx_write_reg(dev, MAC_TX, buf);
4185 return 0;
4188 static int lan78xx_reset_resume(struct usb_interface *intf)
4190 struct lan78xx_net *dev = usb_get_intfdata(intf);
4192 lan78xx_reset(dev);
4194 phy_start(dev->net->phydev);
4196 return lan78xx_resume(intf);
4199 static const struct usb_device_id products[] = {
4201 /* LAN7800 USB Gigabit Ethernet Device */
4202 USB_DEVICE(LAN78XX_USB_VENDOR_ID, LAN7800_USB_PRODUCT_ID),
4205 /* LAN7850 USB Gigabit Ethernet Device */
4206 USB_DEVICE(LAN78XX_USB_VENDOR_ID, LAN7850_USB_PRODUCT_ID),
4209 /* LAN7801 USB Gigabit Ethernet Device */
4210 USB_DEVICE(LAN78XX_USB_VENDOR_ID, LAN7801_USB_PRODUCT_ID),
4214 MODULE_DEVICE_TABLE(usb, products);
4216 static struct usb_driver lan78xx_driver = {
4217 .name = DRIVER_NAME,
4218 .id_table = products,
4219 .probe = lan78xx_probe,
4220 .disconnect = lan78xx_disconnect,
4221 .suspend = lan78xx_suspend,
4222 .resume = lan78xx_resume,
4223 .reset_resume = lan78xx_reset_resume,
4224 .supports_autosuspend = 1,
4225 .disable_hub_initiated_lpm = 1,
4228 module_usb_driver(lan78xx_driver);
4230 MODULE_AUTHOR(DRIVER_AUTHOR);
4231 MODULE_DESCRIPTION(DRIVER_DESC);
4232 MODULE_LICENSE("GPL");