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>
30 #include <linux/ipv6.h>
31 #include <linux/mdio.h>
32 #include <linux/phy.h>
33 #include <net/ip6_checksum.h>
34 #include <linux/interrupt.h>
35 #include <linux/irqdomain.h>
36 #include <linux/irq.h>
37 #include <linux/irqchip/chained_irq.h>
38 #include <linux/microchipphy.h>
39 #include <linux/phy.h>
42 #define DRIVER_AUTHOR "WOOJUNG HUH <woojung.huh@microchip.com>"
43 #define DRIVER_DESC "LAN78XX USB 3.0 Gigabit Ethernet Devices"
44 #define DRIVER_NAME "lan78xx"
45 #define DRIVER_VERSION "1.0.6"
47 #define TX_TIMEOUT_JIFFIES (5 * HZ)
48 #define THROTTLE_JIFFIES (HZ / 8)
49 #define UNLINK_TIMEOUT_MS 3
51 #define RX_MAX_QUEUE_MEMORY (60 * 1518)
53 #define SS_USB_PKT_SIZE (1024)
54 #define HS_USB_PKT_SIZE (512)
55 #define FS_USB_PKT_SIZE (64)
57 #define MAX_RX_FIFO_SIZE (12 * 1024)
58 #define MAX_TX_FIFO_SIZE (12 * 1024)
59 #define DEFAULT_BURST_CAP_SIZE (MAX_TX_FIFO_SIZE)
60 #define DEFAULT_BULK_IN_DELAY (0x0800)
61 #define MAX_SINGLE_PACKET_SIZE (9000)
62 #define DEFAULT_TX_CSUM_ENABLE (true)
63 #define DEFAULT_RX_CSUM_ENABLE (true)
64 #define DEFAULT_TSO_CSUM_ENABLE (true)
65 #define DEFAULT_VLAN_FILTER_ENABLE (true)
66 #define TX_OVERHEAD (8)
69 #define LAN78XX_USB_VENDOR_ID (0x0424)
70 #define LAN7800_USB_PRODUCT_ID (0x7800)
71 #define LAN7850_USB_PRODUCT_ID (0x7850)
72 #define LAN7801_USB_PRODUCT_ID (0x7801)
73 #define LAN78XX_EEPROM_MAGIC (0x78A5)
74 #define LAN78XX_OTP_MAGIC (0x78F3)
79 #define EEPROM_INDICATOR (0xA5)
80 #define EEPROM_MAC_OFFSET (0x01)
81 #define MAX_EEPROM_SIZE 512
82 #define OTP_INDICATOR_1 (0xF3)
83 #define OTP_INDICATOR_2 (0xF7)
85 #define WAKE_ALL (WAKE_PHY | WAKE_UCAST | \
86 WAKE_MCAST | WAKE_BCAST | \
87 WAKE_ARP | WAKE_MAGIC)
89 /* USB related defines */
90 #define BULK_IN_PIPE 1
91 #define BULK_OUT_PIPE 2
93 /* default autosuspend delay (mSec)*/
94 #define DEFAULT_AUTOSUSPEND_DELAY (10 * 1000)
96 /* statistic update interval (mSec) */
97 #define STAT_UPDATE_TIMER (1 * 1000)
99 /* defines interrupts from interrupt EP */
100 #define MAX_INT_EP (32)
101 #define INT_EP_INTEP (31)
102 #define INT_EP_OTP_WR_DONE (28)
103 #define INT_EP_EEE_TX_LPI_START (26)
104 #define INT_EP_EEE_TX_LPI_STOP (25)
105 #define INT_EP_EEE_RX_LPI (24)
106 #define INT_EP_MAC_RESET_TIMEOUT (23)
107 #define INT_EP_RDFO (22)
108 #define INT_EP_TXE (21)
109 #define INT_EP_USB_STATUS (20)
110 #define INT_EP_TX_DIS (19)
111 #define INT_EP_RX_DIS (18)
112 #define INT_EP_PHY (17)
113 #define INT_EP_DP (16)
114 #define INT_EP_MAC_ERR (15)
115 #define INT_EP_TDFU (14)
116 #define INT_EP_TDFO (13)
117 #define INT_EP_UTX (12)
118 #define INT_EP_GPIO_11 (11)
119 #define INT_EP_GPIO_10 (10)
120 #define INT_EP_GPIO_9 (9)
121 #define INT_EP_GPIO_8 (8)
122 #define INT_EP_GPIO_7 (7)
123 #define INT_EP_GPIO_6 (6)
124 #define INT_EP_GPIO_5 (5)
125 #define INT_EP_GPIO_4 (4)
126 #define INT_EP_GPIO_3 (3)
127 #define INT_EP_GPIO_2 (2)
128 #define INT_EP_GPIO_1 (1)
129 #define INT_EP_GPIO_0 (0)
131 static const char lan78xx_gstrings
[][ETH_GSTRING_LEN
] = {
133 "RX Alignment Errors",
134 "Rx Fragment Errors",
136 "RX Undersize Frame Errors",
137 "RX Oversize Frame Errors",
139 "RX Unicast Byte Count",
140 "RX Broadcast Byte Count",
141 "RX Multicast Byte Count",
143 "RX Broadcast Frames",
144 "RX Multicast Frames",
147 "RX 65 - 127 Byte Frames",
148 "RX 128 - 255 Byte Frames",
149 "RX 256 - 511 Bytes Frames",
150 "RX 512 - 1023 Byte Frames",
151 "RX 1024 - 1518 Byte Frames",
152 "RX Greater 1518 Byte Frames",
153 "EEE RX LPI Transitions",
156 "TX Excess Deferral Errors",
159 "TX Single Collisions",
160 "TX Multiple Collisions",
161 "TX Excessive Collision",
162 "TX Late Collisions",
163 "TX Unicast Byte Count",
164 "TX Broadcast Byte Count",
165 "TX Multicast Byte Count",
167 "TX Broadcast Frames",
168 "TX Multicast Frames",
171 "TX 65 - 127 Byte Frames",
172 "TX 128 - 255 Byte Frames",
173 "TX 256 - 511 Bytes Frames",
174 "TX 512 - 1023 Byte Frames",
175 "TX 1024 - 1518 Byte Frames",
176 "TX Greater 1518 Byte Frames",
177 "EEE TX LPI Transitions",
181 struct lan78xx_statstage
{
183 u32 rx_alignment_errors
;
184 u32 rx_fragment_errors
;
185 u32 rx_jabber_errors
;
186 u32 rx_undersize_frame_errors
;
187 u32 rx_oversize_frame_errors
;
188 u32 rx_dropped_frames
;
189 u32 rx_unicast_byte_count
;
190 u32 rx_broadcast_byte_count
;
191 u32 rx_multicast_byte_count
;
192 u32 rx_unicast_frames
;
193 u32 rx_broadcast_frames
;
194 u32 rx_multicast_frames
;
196 u32 rx_64_byte_frames
;
197 u32 rx_65_127_byte_frames
;
198 u32 rx_128_255_byte_frames
;
199 u32 rx_256_511_bytes_frames
;
200 u32 rx_512_1023_byte_frames
;
201 u32 rx_1024_1518_byte_frames
;
202 u32 rx_greater_1518_byte_frames
;
203 u32 eee_rx_lpi_transitions
;
206 u32 tx_excess_deferral_errors
;
207 u32 tx_carrier_errors
;
208 u32 tx_bad_byte_count
;
209 u32 tx_single_collisions
;
210 u32 tx_multiple_collisions
;
211 u32 tx_excessive_collision
;
212 u32 tx_late_collisions
;
213 u32 tx_unicast_byte_count
;
214 u32 tx_broadcast_byte_count
;
215 u32 tx_multicast_byte_count
;
216 u32 tx_unicast_frames
;
217 u32 tx_broadcast_frames
;
218 u32 tx_multicast_frames
;
220 u32 tx_64_byte_frames
;
221 u32 tx_65_127_byte_frames
;
222 u32 tx_128_255_byte_frames
;
223 u32 tx_256_511_bytes_frames
;
224 u32 tx_512_1023_byte_frames
;
225 u32 tx_1024_1518_byte_frames
;
226 u32 tx_greater_1518_byte_frames
;
227 u32 eee_tx_lpi_transitions
;
231 struct lan78xx_statstage64
{
233 u64 rx_alignment_errors
;
234 u64 rx_fragment_errors
;
235 u64 rx_jabber_errors
;
236 u64 rx_undersize_frame_errors
;
237 u64 rx_oversize_frame_errors
;
238 u64 rx_dropped_frames
;
239 u64 rx_unicast_byte_count
;
240 u64 rx_broadcast_byte_count
;
241 u64 rx_multicast_byte_count
;
242 u64 rx_unicast_frames
;
243 u64 rx_broadcast_frames
;
244 u64 rx_multicast_frames
;
246 u64 rx_64_byte_frames
;
247 u64 rx_65_127_byte_frames
;
248 u64 rx_128_255_byte_frames
;
249 u64 rx_256_511_bytes_frames
;
250 u64 rx_512_1023_byte_frames
;
251 u64 rx_1024_1518_byte_frames
;
252 u64 rx_greater_1518_byte_frames
;
253 u64 eee_rx_lpi_transitions
;
256 u64 tx_excess_deferral_errors
;
257 u64 tx_carrier_errors
;
258 u64 tx_bad_byte_count
;
259 u64 tx_single_collisions
;
260 u64 tx_multiple_collisions
;
261 u64 tx_excessive_collision
;
262 u64 tx_late_collisions
;
263 u64 tx_unicast_byte_count
;
264 u64 tx_broadcast_byte_count
;
265 u64 tx_multicast_byte_count
;
266 u64 tx_unicast_frames
;
267 u64 tx_broadcast_frames
;
268 u64 tx_multicast_frames
;
270 u64 tx_64_byte_frames
;
271 u64 tx_65_127_byte_frames
;
272 u64 tx_128_255_byte_frames
;
273 u64 tx_256_511_bytes_frames
;
274 u64 tx_512_1023_byte_frames
;
275 u64 tx_1024_1518_byte_frames
;
276 u64 tx_greater_1518_byte_frames
;
277 u64 eee_tx_lpi_transitions
;
283 struct lan78xx_priv
{
284 struct lan78xx_net
*dev
;
286 u32 mchash_table
[DP_SEL_VHF_HASH_LEN
]; /* multicat hash table */
287 u32 pfilter_table
[NUM_OF_MAF
][2]; /* perfect filter table */
288 u32 vlan_table
[DP_SEL_VHF_VLAN_LEN
];
289 struct mutex dataport_mutex
; /* for dataport access */
290 spinlock_t rfe_ctl_lock
; /* for rfe register access */
291 struct work_struct set_multicast
;
292 struct work_struct set_vlan
;
306 struct skb_data
{ /* skb->cb is one of these */
308 struct lan78xx_net
*dev
;
309 enum skb_state state
;
315 struct usb_ctrlrequest req
;
316 struct lan78xx_net
*dev
;
319 #define EVENT_TX_HALT 0
320 #define EVENT_RX_HALT 1
321 #define EVENT_RX_MEMORY 2
322 #define EVENT_STS_SPLIT 3
323 #define EVENT_LINK_RESET 4
324 #define EVENT_RX_PAUSED 5
325 #define EVENT_DEV_WAKING 6
326 #define EVENT_DEV_ASLEEP 7
327 #define EVENT_DEV_OPEN 8
328 #define EVENT_STAT_UPDATE 9
331 struct mutex access_lock
; /* for stats access */
332 struct lan78xx_statstage saved
;
333 struct lan78xx_statstage rollover_count
;
334 struct lan78xx_statstage rollover_max
;
335 struct lan78xx_statstage64 curr_stat
;
338 struct irq_domain_data
{
339 struct irq_domain
*irqdomain
;
341 struct irq_chip
*irqchip
;
342 irq_flow_handler_t irq_handler
;
344 struct mutex irq_lock
; /* for irq bus access */
348 struct net_device
*net
;
349 struct usb_device
*udev
;
350 struct usb_interface
*intf
;
355 struct sk_buff_head rxq
;
356 struct sk_buff_head txq
;
357 struct sk_buff_head done
;
358 struct sk_buff_head rxq_pause
;
359 struct sk_buff_head txq_pend
;
361 struct tasklet_struct bh
;
362 struct delayed_work wq
;
364 struct usb_host_endpoint
*ep_blkin
;
365 struct usb_host_endpoint
*ep_blkout
;
366 struct usb_host_endpoint
*ep_intr
;
370 struct urb
*urb_intr
;
371 struct usb_anchor deferred
;
373 struct mutex phy_mutex
; /* for phy access */
374 unsigned pipe_in
, pipe_out
, pipe_intr
;
376 u32 hard_mtu
; /* count any extra framing */
377 size_t rx_urb_size
; /* size for rx urbs */
381 wait_queue_head_t
*wait
;
382 unsigned char suspend_count
;
385 struct timer_list delay
;
386 struct timer_list stat_monitor
;
388 unsigned long data
[5];
395 struct mii_bus
*mdiobus
;
396 phy_interface_t interface
;
399 u8 fc_request_control
;
402 struct statstage stats
;
404 struct irq_domain_data domain_data
;
407 /* define external phy id */
408 #define PHY_LAN8835 (0x0007C130)
409 #define PHY_KSZ9031RNX (0x00221620)
411 /* use ethtool to change the level for any given device */
412 static int msg_level
= -1;
413 module_param(msg_level
, int, 0);
414 MODULE_PARM_DESC(msg_level
, "Override default message level");
416 static int lan78xx_read_reg(struct lan78xx_net
*dev
, u32 index
, u32
*data
)
418 u32
*buf
= kmalloc(sizeof(u32
), GFP_KERNEL
);
424 ret
= usb_control_msg(dev
->udev
, usb_rcvctrlpipe(dev
->udev
, 0),
425 USB_VENDOR_REQUEST_READ_REGISTER
,
426 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
427 0, index
, buf
, 4, USB_CTRL_GET_TIMEOUT
);
428 if (likely(ret
>= 0)) {
432 netdev_warn(dev
->net
,
433 "Failed to read register index 0x%08x. ret = %d",
442 static int lan78xx_write_reg(struct lan78xx_net
*dev
, u32 index
, u32 data
)
444 u32
*buf
= kmalloc(sizeof(u32
), GFP_KERNEL
);
453 ret
= usb_control_msg(dev
->udev
, usb_sndctrlpipe(dev
->udev
, 0),
454 USB_VENDOR_REQUEST_WRITE_REGISTER
,
455 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
456 0, index
, buf
, 4, USB_CTRL_SET_TIMEOUT
);
457 if (unlikely(ret
< 0)) {
458 netdev_warn(dev
->net
,
459 "Failed to write register index 0x%08x. ret = %d",
468 static int lan78xx_read_stats(struct lan78xx_net
*dev
,
469 struct lan78xx_statstage
*data
)
473 struct lan78xx_statstage
*stats
;
477 stats
= kmalloc(sizeof(*stats
), GFP_KERNEL
);
481 ret
= usb_control_msg(dev
->udev
,
482 usb_rcvctrlpipe(dev
->udev
, 0),
483 USB_VENDOR_REQUEST_GET_STATS
,
484 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
489 USB_CTRL_SET_TIMEOUT
);
490 if (likely(ret
>= 0)) {
493 for (i
= 0; i
< sizeof(*stats
)/sizeof(u32
); i
++) {
494 le32_to_cpus(&src
[i
]);
498 netdev_warn(dev
->net
,
499 "Failed to read stat ret = 0x%x", ret
);
507 #define check_counter_rollover(struct1, dev_stats, member) { \
508 if (struct1->member < dev_stats.saved.member) \
509 dev_stats.rollover_count.member++; \
512 static void lan78xx_check_stat_rollover(struct lan78xx_net
*dev
,
513 struct lan78xx_statstage
*stats
)
515 check_counter_rollover(stats
, dev
->stats
, rx_fcs_errors
);
516 check_counter_rollover(stats
, dev
->stats
, rx_alignment_errors
);
517 check_counter_rollover(stats
, dev
->stats
, rx_fragment_errors
);
518 check_counter_rollover(stats
, dev
->stats
, rx_jabber_errors
);
519 check_counter_rollover(stats
, dev
->stats
, rx_undersize_frame_errors
);
520 check_counter_rollover(stats
, dev
->stats
, rx_oversize_frame_errors
);
521 check_counter_rollover(stats
, dev
->stats
, rx_dropped_frames
);
522 check_counter_rollover(stats
, dev
->stats
, rx_unicast_byte_count
);
523 check_counter_rollover(stats
, dev
->stats
, rx_broadcast_byte_count
);
524 check_counter_rollover(stats
, dev
->stats
, rx_multicast_byte_count
);
525 check_counter_rollover(stats
, dev
->stats
, rx_unicast_frames
);
526 check_counter_rollover(stats
, dev
->stats
, rx_broadcast_frames
);
527 check_counter_rollover(stats
, dev
->stats
, rx_multicast_frames
);
528 check_counter_rollover(stats
, dev
->stats
, rx_pause_frames
);
529 check_counter_rollover(stats
, dev
->stats
, rx_64_byte_frames
);
530 check_counter_rollover(stats
, dev
->stats
, rx_65_127_byte_frames
);
531 check_counter_rollover(stats
, dev
->stats
, rx_128_255_byte_frames
);
532 check_counter_rollover(stats
, dev
->stats
, rx_256_511_bytes_frames
);
533 check_counter_rollover(stats
, dev
->stats
, rx_512_1023_byte_frames
);
534 check_counter_rollover(stats
, dev
->stats
, rx_1024_1518_byte_frames
);
535 check_counter_rollover(stats
, dev
->stats
, rx_greater_1518_byte_frames
);
536 check_counter_rollover(stats
, dev
->stats
, eee_rx_lpi_transitions
);
537 check_counter_rollover(stats
, dev
->stats
, eee_rx_lpi_time
);
538 check_counter_rollover(stats
, dev
->stats
, tx_fcs_errors
);
539 check_counter_rollover(stats
, dev
->stats
, tx_excess_deferral_errors
);
540 check_counter_rollover(stats
, dev
->stats
, tx_carrier_errors
);
541 check_counter_rollover(stats
, dev
->stats
, tx_bad_byte_count
);
542 check_counter_rollover(stats
, dev
->stats
, tx_single_collisions
);
543 check_counter_rollover(stats
, dev
->stats
, tx_multiple_collisions
);
544 check_counter_rollover(stats
, dev
->stats
, tx_excessive_collision
);
545 check_counter_rollover(stats
, dev
->stats
, tx_late_collisions
);
546 check_counter_rollover(stats
, dev
->stats
, tx_unicast_byte_count
);
547 check_counter_rollover(stats
, dev
->stats
, tx_broadcast_byte_count
);
548 check_counter_rollover(stats
, dev
->stats
, tx_multicast_byte_count
);
549 check_counter_rollover(stats
, dev
->stats
, tx_unicast_frames
);
550 check_counter_rollover(stats
, dev
->stats
, tx_broadcast_frames
);
551 check_counter_rollover(stats
, dev
->stats
, tx_multicast_frames
);
552 check_counter_rollover(stats
, dev
->stats
, tx_pause_frames
);
553 check_counter_rollover(stats
, dev
->stats
, tx_64_byte_frames
);
554 check_counter_rollover(stats
, dev
->stats
, tx_65_127_byte_frames
);
555 check_counter_rollover(stats
, dev
->stats
, tx_128_255_byte_frames
);
556 check_counter_rollover(stats
, dev
->stats
, tx_256_511_bytes_frames
);
557 check_counter_rollover(stats
, dev
->stats
, tx_512_1023_byte_frames
);
558 check_counter_rollover(stats
, dev
->stats
, tx_1024_1518_byte_frames
);
559 check_counter_rollover(stats
, dev
->stats
, tx_greater_1518_byte_frames
);
560 check_counter_rollover(stats
, dev
->stats
, eee_tx_lpi_transitions
);
561 check_counter_rollover(stats
, dev
->stats
, eee_tx_lpi_time
);
563 memcpy(&dev
->stats
.saved
, stats
, sizeof(struct lan78xx_statstage
));
566 static void lan78xx_update_stats(struct lan78xx_net
*dev
)
568 u32
*p
, *count
, *max
;
571 struct lan78xx_statstage lan78xx_stats
;
573 if (usb_autopm_get_interface(dev
->intf
) < 0)
576 p
= (u32
*)&lan78xx_stats
;
577 count
= (u32
*)&dev
->stats
.rollover_count
;
578 max
= (u32
*)&dev
->stats
.rollover_max
;
579 data
= (u64
*)&dev
->stats
.curr_stat
;
581 mutex_lock(&dev
->stats
.access_lock
);
583 if (lan78xx_read_stats(dev
, &lan78xx_stats
) > 0)
584 lan78xx_check_stat_rollover(dev
, &lan78xx_stats
);
586 for (i
= 0; i
< (sizeof(lan78xx_stats
) / (sizeof(u32
))); i
++)
587 data
[i
] = (u64
)p
[i
] + ((u64
)count
[i
] * ((u64
)max
[i
] + 1));
589 mutex_unlock(&dev
->stats
.access_lock
);
591 usb_autopm_put_interface(dev
->intf
);
594 /* Loop until the read is completed with timeout called with phy_mutex held */
595 static int lan78xx_phy_wait_not_busy(struct lan78xx_net
*dev
)
597 unsigned long start_time
= jiffies
;
602 ret
= lan78xx_read_reg(dev
, MII_ACC
, &val
);
603 if (unlikely(ret
< 0))
606 if (!(val
& MII_ACC_MII_BUSY_
))
608 } while (!time_after(jiffies
, start_time
+ HZ
));
613 static inline u32
mii_access(int id
, int index
, int read
)
617 ret
= ((u32
)id
<< MII_ACC_PHY_ADDR_SHIFT_
) & MII_ACC_PHY_ADDR_MASK_
;
618 ret
|= ((u32
)index
<< MII_ACC_MIIRINDA_SHIFT_
) & MII_ACC_MIIRINDA_MASK_
;
620 ret
|= MII_ACC_MII_READ_
;
622 ret
|= MII_ACC_MII_WRITE_
;
623 ret
|= MII_ACC_MII_BUSY_
;
628 static int lan78xx_wait_eeprom(struct lan78xx_net
*dev
)
630 unsigned long start_time
= jiffies
;
635 ret
= lan78xx_read_reg(dev
, E2P_CMD
, &val
);
636 if (unlikely(ret
< 0))
639 if (!(val
& E2P_CMD_EPC_BUSY_
) ||
640 (val
& E2P_CMD_EPC_TIMEOUT_
))
642 usleep_range(40, 100);
643 } while (!time_after(jiffies
, start_time
+ HZ
));
645 if (val
& (E2P_CMD_EPC_TIMEOUT_
| E2P_CMD_EPC_BUSY_
)) {
646 netdev_warn(dev
->net
, "EEPROM read operation timeout");
653 static int lan78xx_eeprom_confirm_not_busy(struct lan78xx_net
*dev
)
655 unsigned long start_time
= jiffies
;
660 ret
= lan78xx_read_reg(dev
, E2P_CMD
, &val
);
661 if (unlikely(ret
< 0))
664 if (!(val
& E2P_CMD_EPC_BUSY_
))
667 usleep_range(40, 100);
668 } while (!time_after(jiffies
, start_time
+ HZ
));
670 netdev_warn(dev
->net
, "EEPROM is busy");
674 static int lan78xx_read_raw_eeprom(struct lan78xx_net
*dev
, u32 offset
,
675 u32 length
, u8
*data
)
682 /* depends on chip, some EEPROM pins are muxed with LED function.
683 * disable & restore LED function to access EEPROM.
685 ret
= lan78xx_read_reg(dev
, HW_CFG
, &val
);
687 if (dev
->chipid
== ID_REV_CHIP_ID_7800_
) {
688 val
&= ~(HW_CFG_LED1_EN_
| HW_CFG_LED0_EN_
);
689 ret
= lan78xx_write_reg(dev
, HW_CFG
, val
);
692 retval
= lan78xx_eeprom_confirm_not_busy(dev
);
696 for (i
= 0; i
< length
; i
++) {
697 val
= E2P_CMD_EPC_BUSY_
| E2P_CMD_EPC_CMD_READ_
;
698 val
|= (offset
& E2P_CMD_EPC_ADDR_MASK_
);
699 ret
= lan78xx_write_reg(dev
, E2P_CMD
, val
);
700 if (unlikely(ret
< 0)) {
705 retval
= lan78xx_wait_eeprom(dev
);
709 ret
= lan78xx_read_reg(dev
, E2P_DATA
, &val
);
710 if (unlikely(ret
< 0)) {
715 data
[i
] = val
& 0xFF;
721 if (dev
->chipid
== ID_REV_CHIP_ID_7800_
)
722 ret
= lan78xx_write_reg(dev
, HW_CFG
, saved
);
727 static int lan78xx_read_eeprom(struct lan78xx_net
*dev
, u32 offset
,
728 u32 length
, u8
*data
)
733 ret
= lan78xx_read_raw_eeprom(dev
, 0, 1, &sig
);
734 if ((ret
== 0) && (sig
== EEPROM_INDICATOR
))
735 ret
= lan78xx_read_raw_eeprom(dev
, offset
, length
, data
);
742 static int lan78xx_write_raw_eeprom(struct lan78xx_net
*dev
, u32 offset
,
743 u32 length
, u8
*data
)
750 /* depends on chip, some EEPROM pins are muxed with LED function.
751 * disable & restore LED function to access EEPROM.
753 ret
= lan78xx_read_reg(dev
, HW_CFG
, &val
);
755 if (dev
->chipid
== ID_REV_CHIP_ID_7800_
) {
756 val
&= ~(HW_CFG_LED1_EN_
| HW_CFG_LED0_EN_
);
757 ret
= lan78xx_write_reg(dev
, HW_CFG
, val
);
760 retval
= lan78xx_eeprom_confirm_not_busy(dev
);
764 /* Issue write/erase enable command */
765 val
= E2P_CMD_EPC_BUSY_
| E2P_CMD_EPC_CMD_EWEN_
;
766 ret
= lan78xx_write_reg(dev
, E2P_CMD
, val
);
767 if (unlikely(ret
< 0)) {
772 retval
= lan78xx_wait_eeprom(dev
);
776 for (i
= 0; i
< length
; i
++) {
777 /* Fill data register */
779 ret
= lan78xx_write_reg(dev
, E2P_DATA
, val
);
785 /* Send "write" command */
786 val
= E2P_CMD_EPC_BUSY_
| E2P_CMD_EPC_CMD_WRITE_
;
787 val
|= (offset
& E2P_CMD_EPC_ADDR_MASK_
);
788 ret
= lan78xx_write_reg(dev
, E2P_CMD
, val
);
794 retval
= lan78xx_wait_eeprom(dev
);
803 if (dev
->chipid
== ID_REV_CHIP_ID_7800_
)
804 ret
= lan78xx_write_reg(dev
, HW_CFG
, saved
);
809 static int lan78xx_read_raw_otp(struct lan78xx_net
*dev
, u32 offset
,
810 u32 length
, u8
*data
)
815 unsigned long timeout
;
817 ret
= lan78xx_read_reg(dev
, OTP_PWR_DN
, &buf
);
819 if (buf
& OTP_PWR_DN_PWRDN_N_
) {
820 /* clear it and wait to be cleared */
821 ret
= lan78xx_write_reg(dev
, OTP_PWR_DN
, 0);
823 timeout
= jiffies
+ HZ
;
826 ret
= lan78xx_read_reg(dev
, OTP_PWR_DN
, &buf
);
827 if (time_after(jiffies
, timeout
)) {
828 netdev_warn(dev
->net
,
829 "timeout on OTP_PWR_DN");
832 } while (buf
& OTP_PWR_DN_PWRDN_N_
);
835 for (i
= 0; i
< length
; i
++) {
836 ret
= lan78xx_write_reg(dev
, OTP_ADDR1
,
837 ((offset
+ i
) >> 8) & OTP_ADDR1_15_11
);
838 ret
= lan78xx_write_reg(dev
, OTP_ADDR2
,
839 ((offset
+ i
) & OTP_ADDR2_10_3
));
841 ret
= lan78xx_write_reg(dev
, OTP_FUNC_CMD
, OTP_FUNC_CMD_READ_
);
842 ret
= lan78xx_write_reg(dev
, OTP_CMD_GO
, OTP_CMD_GO_GO_
);
844 timeout
= jiffies
+ HZ
;
847 ret
= lan78xx_read_reg(dev
, OTP_STATUS
, &buf
);
848 if (time_after(jiffies
, timeout
)) {
849 netdev_warn(dev
->net
,
850 "timeout on OTP_STATUS");
853 } while (buf
& OTP_STATUS_BUSY_
);
855 ret
= lan78xx_read_reg(dev
, OTP_RD_DATA
, &buf
);
857 data
[i
] = (u8
)(buf
& 0xFF);
863 static int lan78xx_write_raw_otp(struct lan78xx_net
*dev
, u32 offset
,
864 u32 length
, u8
*data
)
869 unsigned long timeout
;
871 ret
= lan78xx_read_reg(dev
, OTP_PWR_DN
, &buf
);
873 if (buf
& OTP_PWR_DN_PWRDN_N_
) {
874 /* clear it and wait to be cleared */
875 ret
= lan78xx_write_reg(dev
, OTP_PWR_DN
, 0);
877 timeout
= jiffies
+ HZ
;
880 ret
= lan78xx_read_reg(dev
, OTP_PWR_DN
, &buf
);
881 if (time_after(jiffies
, timeout
)) {
882 netdev_warn(dev
->net
,
883 "timeout on OTP_PWR_DN completion");
886 } while (buf
& OTP_PWR_DN_PWRDN_N_
);
889 /* set to BYTE program mode */
890 ret
= lan78xx_write_reg(dev
, OTP_PRGM_MODE
, OTP_PRGM_MODE_BYTE_
);
892 for (i
= 0; i
< length
; i
++) {
893 ret
= lan78xx_write_reg(dev
, OTP_ADDR1
,
894 ((offset
+ i
) >> 8) & OTP_ADDR1_15_11
);
895 ret
= lan78xx_write_reg(dev
, OTP_ADDR2
,
896 ((offset
+ i
) & OTP_ADDR2_10_3
));
897 ret
= lan78xx_write_reg(dev
, OTP_PRGM_DATA
, data
[i
]);
898 ret
= lan78xx_write_reg(dev
, OTP_TST_CMD
, OTP_TST_CMD_PRGVRFY_
);
899 ret
= lan78xx_write_reg(dev
, OTP_CMD_GO
, OTP_CMD_GO_GO_
);
901 timeout
= jiffies
+ HZ
;
904 ret
= lan78xx_read_reg(dev
, OTP_STATUS
, &buf
);
905 if (time_after(jiffies
, timeout
)) {
906 netdev_warn(dev
->net
,
907 "Timeout on OTP_STATUS completion");
910 } while (buf
& OTP_STATUS_BUSY_
);
916 static int lan78xx_read_otp(struct lan78xx_net
*dev
, u32 offset
,
917 u32 length
, u8
*data
)
922 ret
= lan78xx_read_raw_otp(dev
, 0, 1, &sig
);
925 if (sig
== OTP_INDICATOR_1
)
927 else if (sig
== OTP_INDICATOR_2
)
932 ret
= lan78xx_read_raw_otp(dev
, offset
, length
, data
);
938 static int lan78xx_dataport_wait_not_busy(struct lan78xx_net
*dev
)
942 for (i
= 0; i
< 100; i
++) {
945 ret
= lan78xx_read_reg(dev
, DP_SEL
, &dp_sel
);
946 if (unlikely(ret
< 0))
949 if (dp_sel
& DP_SEL_DPRDY_
)
952 usleep_range(40, 100);
955 netdev_warn(dev
->net
, "lan78xx_dataport_wait_not_busy timed out");
960 static int lan78xx_dataport_write(struct lan78xx_net
*dev
, u32 ram_select
,
961 u32 addr
, u32 length
, u32
*buf
)
963 struct lan78xx_priv
*pdata
= (struct lan78xx_priv
*)(dev
->data
[0]);
967 if (usb_autopm_get_interface(dev
->intf
) < 0)
970 mutex_lock(&pdata
->dataport_mutex
);
972 ret
= lan78xx_dataport_wait_not_busy(dev
);
976 ret
= lan78xx_read_reg(dev
, DP_SEL
, &dp_sel
);
978 dp_sel
&= ~DP_SEL_RSEL_MASK_
;
979 dp_sel
|= ram_select
;
980 ret
= lan78xx_write_reg(dev
, DP_SEL
, dp_sel
);
982 for (i
= 0; i
< length
; i
++) {
983 ret
= lan78xx_write_reg(dev
, DP_ADDR
, addr
+ i
);
985 ret
= lan78xx_write_reg(dev
, DP_DATA
, buf
[i
]);
987 ret
= lan78xx_write_reg(dev
, DP_CMD
, DP_CMD_WRITE_
);
989 ret
= lan78xx_dataport_wait_not_busy(dev
);
995 mutex_unlock(&pdata
->dataport_mutex
);
996 usb_autopm_put_interface(dev
->intf
);
1001 static void lan78xx_set_addr_filter(struct lan78xx_priv
*pdata
,
1002 int index
, u8 addr
[ETH_ALEN
])
1006 if ((pdata
) && (index
> 0) && (index
< NUM_OF_MAF
)) {
1008 temp
= addr
[2] | (temp
<< 8);
1009 temp
= addr
[1] | (temp
<< 8);
1010 temp
= addr
[0] | (temp
<< 8);
1011 pdata
->pfilter_table
[index
][1] = temp
;
1013 temp
= addr
[4] | (temp
<< 8);
1014 temp
|= MAF_HI_VALID_
| MAF_HI_TYPE_DST_
;
1015 pdata
->pfilter_table
[index
][0] = temp
;
1019 /* returns hash bit number for given MAC address */
1020 static inline u32
lan78xx_hash(char addr
[ETH_ALEN
])
1022 return (ether_crc(ETH_ALEN
, addr
) >> 23) & 0x1ff;
1025 static void lan78xx_deferred_multicast_write(struct work_struct
*param
)
1027 struct lan78xx_priv
*pdata
=
1028 container_of(param
, struct lan78xx_priv
, set_multicast
);
1029 struct lan78xx_net
*dev
= pdata
->dev
;
1033 netif_dbg(dev
, drv
, dev
->net
, "deferred multicast write 0x%08x\n",
1036 lan78xx_dataport_write(dev
, DP_SEL_RSEL_VLAN_DA_
, DP_SEL_VHF_VLAN_LEN
,
1037 DP_SEL_VHF_HASH_LEN
, pdata
->mchash_table
);
1039 for (i
= 1; i
< NUM_OF_MAF
; i
++) {
1040 ret
= lan78xx_write_reg(dev
, MAF_HI(i
), 0);
1041 ret
= lan78xx_write_reg(dev
, MAF_LO(i
),
1042 pdata
->pfilter_table
[i
][1]);
1043 ret
= lan78xx_write_reg(dev
, MAF_HI(i
),
1044 pdata
->pfilter_table
[i
][0]);
1047 ret
= lan78xx_write_reg(dev
, RFE_CTL
, pdata
->rfe_ctl
);
1050 static void lan78xx_set_multicast(struct net_device
*netdev
)
1052 struct lan78xx_net
*dev
= netdev_priv(netdev
);
1053 struct lan78xx_priv
*pdata
= (struct lan78xx_priv
*)(dev
->data
[0]);
1054 unsigned long flags
;
1057 spin_lock_irqsave(&pdata
->rfe_ctl_lock
, flags
);
1059 pdata
->rfe_ctl
&= ~(RFE_CTL_UCAST_EN_
| RFE_CTL_MCAST_EN_
|
1060 RFE_CTL_DA_PERFECT_
| RFE_CTL_MCAST_HASH_
);
1062 for (i
= 0; i
< DP_SEL_VHF_HASH_LEN
; i
++)
1063 pdata
->mchash_table
[i
] = 0;
1064 /* pfilter_table[0] has own HW address */
1065 for (i
= 1; i
< NUM_OF_MAF
; i
++) {
1066 pdata
->pfilter_table
[i
][0] =
1067 pdata
->pfilter_table
[i
][1] = 0;
1070 pdata
->rfe_ctl
|= RFE_CTL_BCAST_EN_
;
1072 if (dev
->net
->flags
& IFF_PROMISC
) {
1073 netif_dbg(dev
, drv
, dev
->net
, "promiscuous mode enabled");
1074 pdata
->rfe_ctl
|= RFE_CTL_MCAST_EN_
| RFE_CTL_UCAST_EN_
;
1076 if (dev
->net
->flags
& IFF_ALLMULTI
) {
1077 netif_dbg(dev
, drv
, dev
->net
,
1078 "receive all multicast enabled");
1079 pdata
->rfe_ctl
|= RFE_CTL_MCAST_EN_
;
1083 if (netdev_mc_count(dev
->net
)) {
1084 struct netdev_hw_addr
*ha
;
1087 netif_dbg(dev
, drv
, dev
->net
, "receive multicast hash filter");
1089 pdata
->rfe_ctl
|= RFE_CTL_DA_PERFECT_
;
1092 netdev_for_each_mc_addr(ha
, netdev
) {
1093 /* set first 32 into Perfect Filter */
1095 lan78xx_set_addr_filter(pdata
, i
, ha
->addr
);
1097 u32 bitnum
= lan78xx_hash(ha
->addr
);
1099 pdata
->mchash_table
[bitnum
/ 32] |=
1100 (1 << (bitnum
% 32));
1101 pdata
->rfe_ctl
|= RFE_CTL_MCAST_HASH_
;
1107 spin_unlock_irqrestore(&pdata
->rfe_ctl_lock
, flags
);
1109 /* defer register writes to a sleepable context */
1110 schedule_work(&pdata
->set_multicast
);
1113 static int lan78xx_update_flowcontrol(struct lan78xx_net
*dev
, u8 duplex
,
1114 u16 lcladv
, u16 rmtadv
)
1116 u32 flow
= 0, fct_flow
= 0;
1120 if (dev
->fc_autoneg
)
1121 cap
= mii_resolve_flowctrl_fdx(lcladv
, rmtadv
);
1123 cap
= dev
->fc_request_control
;
1125 if (cap
& FLOW_CTRL_TX
)
1126 flow
|= (FLOW_CR_TX_FCEN_
| 0xFFFF);
1128 if (cap
& FLOW_CTRL_RX
)
1129 flow
|= FLOW_CR_RX_FCEN_
;
1131 if (dev
->udev
->speed
== USB_SPEED_SUPER
)
1133 else if (dev
->udev
->speed
== USB_SPEED_HIGH
)
1136 netif_dbg(dev
, link
, dev
->net
, "rx pause %s, tx pause %s",
1137 (cap
& FLOW_CTRL_RX
? "enabled" : "disabled"),
1138 (cap
& FLOW_CTRL_TX
? "enabled" : "disabled"));
1140 ret
= lan78xx_write_reg(dev
, FCT_FLOW
, fct_flow
);
1142 /* threshold value should be set before enabling flow */
1143 ret
= lan78xx_write_reg(dev
, FLOW
, flow
);
1148 static int lan78xx_link_reset(struct lan78xx_net
*dev
)
1150 struct phy_device
*phydev
= dev
->net
->phydev
;
1151 struct ethtool_link_ksettings ecmd
;
1152 int ladv
, radv
, ret
;
1155 /* clear LAN78xx interrupt status */
1156 ret
= lan78xx_write_reg(dev
, INT_STS
, INT_STS_PHY_INT_
);
1157 if (unlikely(ret
< 0))
1160 phy_read_status(phydev
);
1162 if (!phydev
->link
&& dev
->link_on
) {
1163 dev
->link_on
= false;
1166 ret
= lan78xx_read_reg(dev
, MAC_CR
, &buf
);
1167 if (unlikely(ret
< 0))
1170 ret
= lan78xx_write_reg(dev
, MAC_CR
, buf
);
1171 if (unlikely(ret
< 0))
1174 del_timer(&dev
->stat_monitor
);
1175 } else if (phydev
->link
&& !dev
->link_on
) {
1176 dev
->link_on
= true;
1178 phy_ethtool_ksettings_get(phydev
, &ecmd
);
1180 if (dev
->udev
->speed
== USB_SPEED_SUPER
) {
1181 if (ecmd
.base
.speed
== 1000) {
1183 ret
= lan78xx_read_reg(dev
, USB_CFG1
, &buf
);
1184 buf
&= ~USB_CFG1_DEV_U2_INIT_EN_
;
1185 ret
= lan78xx_write_reg(dev
, USB_CFG1
, buf
);
1187 ret
= lan78xx_read_reg(dev
, USB_CFG1
, &buf
);
1188 buf
|= USB_CFG1_DEV_U1_INIT_EN_
;
1189 ret
= lan78xx_write_reg(dev
, USB_CFG1
, buf
);
1191 /* enable U1 & U2 */
1192 ret
= lan78xx_read_reg(dev
, USB_CFG1
, &buf
);
1193 buf
|= USB_CFG1_DEV_U2_INIT_EN_
;
1194 buf
|= USB_CFG1_DEV_U1_INIT_EN_
;
1195 ret
= lan78xx_write_reg(dev
, USB_CFG1
, buf
);
1199 ladv
= phy_read(phydev
, MII_ADVERTISE
);
1203 radv
= phy_read(phydev
, MII_LPA
);
1207 netif_dbg(dev
, link
, dev
->net
,
1208 "speed: %u duplex: %d anadv: 0x%04x anlpa: 0x%04x",
1209 ecmd
.base
.speed
, ecmd
.base
.duplex
, ladv
, radv
);
1211 ret
= lan78xx_update_flowcontrol(dev
, ecmd
.base
.duplex
, ladv
,
1214 if (!timer_pending(&dev
->stat_monitor
)) {
1216 mod_timer(&dev
->stat_monitor
,
1217 jiffies
+ STAT_UPDATE_TIMER
);
1224 /* some work can't be done in tasklets, so we use keventd
1226 * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
1227 * but tasklet_schedule() doesn't. hope the failure is rare.
1229 static void lan78xx_defer_kevent(struct lan78xx_net
*dev
, int work
)
1231 set_bit(work
, &dev
->flags
);
1232 if (!schedule_delayed_work(&dev
->wq
, 0))
1233 netdev_err(dev
->net
, "kevent %d may have been dropped\n", work
);
1236 static void lan78xx_status(struct lan78xx_net
*dev
, struct urb
*urb
)
1240 if (urb
->actual_length
!= 4) {
1241 netdev_warn(dev
->net
,
1242 "unexpected urb length %d", urb
->actual_length
);
1246 memcpy(&intdata
, urb
->transfer_buffer
, 4);
1247 le32_to_cpus(&intdata
);
1249 if (intdata
& INT_ENP_PHY_INT
) {
1250 netif_dbg(dev
, link
, dev
->net
, "PHY INTR: 0x%08x\n", intdata
);
1251 lan78xx_defer_kevent(dev
, EVENT_LINK_RESET
);
1253 if (dev
->domain_data
.phyirq
> 0)
1254 generic_handle_irq(dev
->domain_data
.phyirq
);
1256 netdev_warn(dev
->net
,
1257 "unexpected interrupt: 0x%08x\n", intdata
);
1260 static int lan78xx_ethtool_get_eeprom_len(struct net_device
*netdev
)
1262 return MAX_EEPROM_SIZE
;
1265 static int lan78xx_ethtool_get_eeprom(struct net_device
*netdev
,
1266 struct ethtool_eeprom
*ee
, u8
*data
)
1268 struct lan78xx_net
*dev
= netdev_priv(netdev
);
1271 ret
= usb_autopm_get_interface(dev
->intf
);
1275 ee
->magic
= LAN78XX_EEPROM_MAGIC
;
1277 ret
= lan78xx_read_raw_eeprom(dev
, ee
->offset
, ee
->len
, data
);
1279 usb_autopm_put_interface(dev
->intf
);
1284 static int lan78xx_ethtool_set_eeprom(struct net_device
*netdev
,
1285 struct ethtool_eeprom
*ee
, u8
*data
)
1287 struct lan78xx_net
*dev
= netdev_priv(netdev
);
1290 ret
= usb_autopm_get_interface(dev
->intf
);
1294 /* Invalid EEPROM_INDICATOR at offset zero will result in a failure
1295 * to load data from EEPROM
1297 if (ee
->magic
== LAN78XX_EEPROM_MAGIC
)
1298 ret
= lan78xx_write_raw_eeprom(dev
, ee
->offset
, ee
->len
, data
);
1299 else if ((ee
->magic
== LAN78XX_OTP_MAGIC
) &&
1300 (ee
->offset
== 0) &&
1302 (data
[0] == OTP_INDICATOR_1
))
1303 ret
= lan78xx_write_raw_otp(dev
, ee
->offset
, ee
->len
, data
);
1305 usb_autopm_put_interface(dev
->intf
);
1310 static void lan78xx_get_strings(struct net_device
*netdev
, u32 stringset
,
1313 if (stringset
== ETH_SS_STATS
)
1314 memcpy(data
, lan78xx_gstrings
, sizeof(lan78xx_gstrings
));
1317 static int lan78xx_get_sset_count(struct net_device
*netdev
, int sset
)
1319 if (sset
== ETH_SS_STATS
)
1320 return ARRAY_SIZE(lan78xx_gstrings
);
1325 static void lan78xx_get_stats(struct net_device
*netdev
,
1326 struct ethtool_stats
*stats
, u64
*data
)
1328 struct lan78xx_net
*dev
= netdev_priv(netdev
);
1330 lan78xx_update_stats(dev
);
1332 mutex_lock(&dev
->stats
.access_lock
);
1333 memcpy(data
, &dev
->stats
.curr_stat
, sizeof(dev
->stats
.curr_stat
));
1334 mutex_unlock(&dev
->stats
.access_lock
);
1337 static void lan78xx_get_wol(struct net_device
*netdev
,
1338 struct ethtool_wolinfo
*wol
)
1340 struct lan78xx_net
*dev
= netdev_priv(netdev
);
1343 struct lan78xx_priv
*pdata
= (struct lan78xx_priv
*)(dev
->data
[0]);
1345 if (usb_autopm_get_interface(dev
->intf
) < 0)
1348 ret
= lan78xx_read_reg(dev
, USB_CFG0
, &buf
);
1349 if (unlikely(ret
< 0)) {
1353 if (buf
& USB_CFG_RMT_WKP_
) {
1354 wol
->supported
= WAKE_ALL
;
1355 wol
->wolopts
= pdata
->wol
;
1362 usb_autopm_put_interface(dev
->intf
);
1365 static int lan78xx_set_wol(struct net_device
*netdev
,
1366 struct ethtool_wolinfo
*wol
)
1368 struct lan78xx_net
*dev
= netdev_priv(netdev
);
1369 struct lan78xx_priv
*pdata
= (struct lan78xx_priv
*)(dev
->data
[0]);
1372 ret
= usb_autopm_get_interface(dev
->intf
);
1377 if (wol
->wolopts
& WAKE_UCAST
)
1378 pdata
->wol
|= WAKE_UCAST
;
1379 if (wol
->wolopts
& WAKE_MCAST
)
1380 pdata
->wol
|= WAKE_MCAST
;
1381 if (wol
->wolopts
& WAKE_BCAST
)
1382 pdata
->wol
|= WAKE_BCAST
;
1383 if (wol
->wolopts
& WAKE_MAGIC
)
1384 pdata
->wol
|= WAKE_MAGIC
;
1385 if (wol
->wolopts
& WAKE_PHY
)
1386 pdata
->wol
|= WAKE_PHY
;
1387 if (wol
->wolopts
& WAKE_ARP
)
1388 pdata
->wol
|= WAKE_ARP
;
1390 device_set_wakeup_enable(&dev
->udev
->dev
, (bool)wol
->wolopts
);
1392 phy_ethtool_set_wol(netdev
->phydev
, wol
);
1394 usb_autopm_put_interface(dev
->intf
);
1399 static int lan78xx_get_eee(struct net_device
*net
, struct ethtool_eee
*edata
)
1401 struct lan78xx_net
*dev
= netdev_priv(net
);
1402 struct phy_device
*phydev
= net
->phydev
;
1406 ret
= usb_autopm_get_interface(dev
->intf
);
1410 ret
= phy_ethtool_get_eee(phydev
, edata
);
1414 ret
= lan78xx_read_reg(dev
, MAC_CR
, &buf
);
1415 if (buf
& MAC_CR_EEE_EN_
) {
1416 edata
->eee_enabled
= true;
1417 edata
->eee_active
= !!(edata
->advertised
&
1418 edata
->lp_advertised
);
1419 edata
->tx_lpi_enabled
= true;
1420 /* EEE_TX_LPI_REQ_DLY & tx_lpi_timer are same uSec unit */
1421 ret
= lan78xx_read_reg(dev
, EEE_TX_LPI_REQ_DLY
, &buf
);
1422 edata
->tx_lpi_timer
= buf
;
1424 edata
->eee_enabled
= false;
1425 edata
->eee_active
= false;
1426 edata
->tx_lpi_enabled
= false;
1427 edata
->tx_lpi_timer
= 0;
1432 usb_autopm_put_interface(dev
->intf
);
1437 static int lan78xx_set_eee(struct net_device
*net
, struct ethtool_eee
*edata
)
1439 struct lan78xx_net
*dev
= netdev_priv(net
);
1443 ret
= usb_autopm_get_interface(dev
->intf
);
1447 if (edata
->eee_enabled
) {
1448 ret
= lan78xx_read_reg(dev
, MAC_CR
, &buf
);
1449 buf
|= MAC_CR_EEE_EN_
;
1450 ret
= lan78xx_write_reg(dev
, MAC_CR
, buf
);
1452 phy_ethtool_set_eee(net
->phydev
, edata
);
1454 buf
= (u32
)edata
->tx_lpi_timer
;
1455 ret
= lan78xx_write_reg(dev
, EEE_TX_LPI_REQ_DLY
, buf
);
1457 ret
= lan78xx_read_reg(dev
, MAC_CR
, &buf
);
1458 buf
&= ~MAC_CR_EEE_EN_
;
1459 ret
= lan78xx_write_reg(dev
, MAC_CR
, buf
);
1462 usb_autopm_put_interface(dev
->intf
);
1467 static u32
lan78xx_get_link(struct net_device
*net
)
1469 phy_read_status(net
->phydev
);
1471 return net
->phydev
->link
;
1474 static void lan78xx_get_drvinfo(struct net_device
*net
,
1475 struct ethtool_drvinfo
*info
)
1477 struct lan78xx_net
*dev
= netdev_priv(net
);
1479 strncpy(info
->driver
, DRIVER_NAME
, sizeof(info
->driver
));
1480 strncpy(info
->version
, DRIVER_VERSION
, sizeof(info
->version
));
1481 usb_make_path(dev
->udev
, info
->bus_info
, sizeof(info
->bus_info
));
1484 static u32
lan78xx_get_msglevel(struct net_device
*net
)
1486 struct lan78xx_net
*dev
= netdev_priv(net
);
1488 return dev
->msg_enable
;
1491 static void lan78xx_set_msglevel(struct net_device
*net
, u32 level
)
1493 struct lan78xx_net
*dev
= netdev_priv(net
);
1495 dev
->msg_enable
= level
;
1498 static int lan78xx_get_link_ksettings(struct net_device
*net
,
1499 struct ethtool_link_ksettings
*cmd
)
1501 struct lan78xx_net
*dev
= netdev_priv(net
);
1502 struct phy_device
*phydev
= net
->phydev
;
1505 ret
= usb_autopm_get_interface(dev
->intf
);
1509 phy_ethtool_ksettings_get(phydev
, cmd
);
1511 usb_autopm_put_interface(dev
->intf
);
1516 static int lan78xx_set_link_ksettings(struct net_device
*net
,
1517 const struct ethtool_link_ksettings
*cmd
)
1519 struct lan78xx_net
*dev
= netdev_priv(net
);
1520 struct phy_device
*phydev
= net
->phydev
;
1524 ret
= usb_autopm_get_interface(dev
->intf
);
1528 /* change speed & duplex */
1529 ret
= phy_ethtool_ksettings_set(phydev
, cmd
);
1531 if (!cmd
->base
.autoneg
) {
1532 /* force link down */
1533 temp
= phy_read(phydev
, MII_BMCR
);
1534 phy_write(phydev
, MII_BMCR
, temp
| BMCR_LOOPBACK
);
1536 phy_write(phydev
, MII_BMCR
, temp
);
1539 usb_autopm_put_interface(dev
->intf
);
1544 static void lan78xx_get_pause(struct net_device
*net
,
1545 struct ethtool_pauseparam
*pause
)
1547 struct lan78xx_net
*dev
= netdev_priv(net
);
1548 struct phy_device
*phydev
= net
->phydev
;
1549 struct ethtool_link_ksettings ecmd
;
1551 phy_ethtool_ksettings_get(phydev
, &ecmd
);
1553 pause
->autoneg
= dev
->fc_autoneg
;
1555 if (dev
->fc_request_control
& FLOW_CTRL_TX
)
1556 pause
->tx_pause
= 1;
1558 if (dev
->fc_request_control
& FLOW_CTRL_RX
)
1559 pause
->rx_pause
= 1;
1562 static int lan78xx_set_pause(struct net_device
*net
,
1563 struct ethtool_pauseparam
*pause
)
1565 struct lan78xx_net
*dev
= netdev_priv(net
);
1566 struct phy_device
*phydev
= net
->phydev
;
1567 struct ethtool_link_ksettings ecmd
;
1570 phy_ethtool_ksettings_get(phydev
, &ecmd
);
1572 if (pause
->autoneg
&& !ecmd
.base
.autoneg
) {
1577 dev
->fc_request_control
= 0;
1578 if (pause
->rx_pause
)
1579 dev
->fc_request_control
|= FLOW_CTRL_RX
;
1581 if (pause
->tx_pause
)
1582 dev
->fc_request_control
|= FLOW_CTRL_TX
;
1584 if (ecmd
.base
.autoneg
) {
1588 ethtool_convert_link_mode_to_legacy_u32(
1589 &advertising
, ecmd
.link_modes
.advertising
);
1591 advertising
&= ~(ADVERTISED_Pause
| ADVERTISED_Asym_Pause
);
1592 mii_adv
= (u32
)mii_advertise_flowctrl(dev
->fc_request_control
);
1593 advertising
|= mii_adv_to_ethtool_adv_t(mii_adv
);
1595 ethtool_convert_legacy_u32_to_link_mode(
1596 ecmd
.link_modes
.advertising
, advertising
);
1598 phy_ethtool_ksettings_set(phydev
, &ecmd
);
1601 dev
->fc_autoneg
= pause
->autoneg
;
1608 static const struct ethtool_ops lan78xx_ethtool_ops
= {
1609 .get_link
= lan78xx_get_link
,
1610 .nway_reset
= phy_ethtool_nway_reset
,
1611 .get_drvinfo
= lan78xx_get_drvinfo
,
1612 .get_msglevel
= lan78xx_get_msglevel
,
1613 .set_msglevel
= lan78xx_set_msglevel
,
1614 .get_eeprom_len
= lan78xx_ethtool_get_eeprom_len
,
1615 .get_eeprom
= lan78xx_ethtool_get_eeprom
,
1616 .set_eeprom
= lan78xx_ethtool_set_eeprom
,
1617 .get_ethtool_stats
= lan78xx_get_stats
,
1618 .get_sset_count
= lan78xx_get_sset_count
,
1619 .get_strings
= lan78xx_get_strings
,
1620 .get_wol
= lan78xx_get_wol
,
1621 .set_wol
= lan78xx_set_wol
,
1622 .get_eee
= lan78xx_get_eee
,
1623 .set_eee
= lan78xx_set_eee
,
1624 .get_pauseparam
= lan78xx_get_pause
,
1625 .set_pauseparam
= lan78xx_set_pause
,
1626 .get_link_ksettings
= lan78xx_get_link_ksettings
,
1627 .set_link_ksettings
= lan78xx_set_link_ksettings
,
1630 static int lan78xx_ioctl(struct net_device
*netdev
, struct ifreq
*rq
, int cmd
)
1632 if (!netif_running(netdev
))
1635 return phy_mii_ioctl(netdev
->phydev
, rq
, cmd
);
1638 static void lan78xx_init_mac_address(struct lan78xx_net
*dev
)
1640 u32 addr_lo
, addr_hi
;
1644 ret
= lan78xx_read_reg(dev
, RX_ADDRL
, &addr_lo
);
1645 ret
= lan78xx_read_reg(dev
, RX_ADDRH
, &addr_hi
);
1647 addr
[0] = addr_lo
& 0xFF;
1648 addr
[1] = (addr_lo
>> 8) & 0xFF;
1649 addr
[2] = (addr_lo
>> 16) & 0xFF;
1650 addr
[3] = (addr_lo
>> 24) & 0xFF;
1651 addr
[4] = addr_hi
& 0xFF;
1652 addr
[5] = (addr_hi
>> 8) & 0xFF;
1654 if (!is_valid_ether_addr(addr
)) {
1655 /* reading mac address from EEPROM or OTP */
1656 if ((lan78xx_read_eeprom(dev
, EEPROM_MAC_OFFSET
, ETH_ALEN
,
1658 (lan78xx_read_otp(dev
, EEPROM_MAC_OFFSET
, ETH_ALEN
,
1660 if (is_valid_ether_addr(addr
)) {
1661 /* eeprom values are valid so use them */
1662 netif_dbg(dev
, ifup
, dev
->net
,
1663 "MAC address read from EEPROM");
1665 /* generate random MAC */
1666 random_ether_addr(addr
);
1667 netif_dbg(dev
, ifup
, dev
->net
,
1668 "MAC address set to random addr");
1671 addr_lo
= addr
[0] | (addr
[1] << 8) |
1672 (addr
[2] << 16) | (addr
[3] << 24);
1673 addr_hi
= addr
[4] | (addr
[5] << 8);
1675 ret
= lan78xx_write_reg(dev
, RX_ADDRL
, addr_lo
);
1676 ret
= lan78xx_write_reg(dev
, RX_ADDRH
, addr_hi
);
1678 /* generate random MAC */
1679 random_ether_addr(addr
);
1680 netif_dbg(dev
, ifup
, dev
->net
,
1681 "MAC address set to random addr");
1685 ret
= lan78xx_write_reg(dev
, MAF_LO(0), addr_lo
);
1686 ret
= lan78xx_write_reg(dev
, MAF_HI(0), addr_hi
| MAF_HI_VALID_
);
1688 ether_addr_copy(dev
->net
->dev_addr
, addr
);
1691 /* MDIO read and write wrappers for phylib */
1692 static int lan78xx_mdiobus_read(struct mii_bus
*bus
, int phy_id
, int idx
)
1694 struct lan78xx_net
*dev
= bus
->priv
;
1698 ret
= usb_autopm_get_interface(dev
->intf
);
1702 mutex_lock(&dev
->phy_mutex
);
1704 /* confirm MII not busy */
1705 ret
= lan78xx_phy_wait_not_busy(dev
);
1709 /* set the address, index & direction (read from PHY) */
1710 addr
= mii_access(phy_id
, idx
, MII_READ
);
1711 ret
= lan78xx_write_reg(dev
, MII_ACC
, addr
);
1713 ret
= lan78xx_phy_wait_not_busy(dev
);
1717 ret
= lan78xx_read_reg(dev
, MII_DATA
, &val
);
1719 ret
= (int)(val
& 0xFFFF);
1722 mutex_unlock(&dev
->phy_mutex
);
1723 usb_autopm_put_interface(dev
->intf
);
1728 static int lan78xx_mdiobus_write(struct mii_bus
*bus
, int phy_id
, int idx
,
1731 struct lan78xx_net
*dev
= bus
->priv
;
1735 ret
= usb_autopm_get_interface(dev
->intf
);
1739 mutex_lock(&dev
->phy_mutex
);
1741 /* confirm MII not busy */
1742 ret
= lan78xx_phy_wait_not_busy(dev
);
1747 ret
= lan78xx_write_reg(dev
, MII_DATA
, val
);
1749 /* set the address, index & direction (write to PHY) */
1750 addr
= mii_access(phy_id
, idx
, MII_WRITE
);
1751 ret
= lan78xx_write_reg(dev
, MII_ACC
, addr
);
1753 ret
= lan78xx_phy_wait_not_busy(dev
);
1758 mutex_unlock(&dev
->phy_mutex
);
1759 usb_autopm_put_interface(dev
->intf
);
1763 static int lan78xx_mdio_init(struct lan78xx_net
*dev
)
1767 dev
->mdiobus
= mdiobus_alloc();
1768 if (!dev
->mdiobus
) {
1769 netdev_err(dev
->net
, "can't allocate MDIO bus\n");
1773 dev
->mdiobus
->priv
= (void *)dev
;
1774 dev
->mdiobus
->read
= lan78xx_mdiobus_read
;
1775 dev
->mdiobus
->write
= lan78xx_mdiobus_write
;
1776 dev
->mdiobus
->name
= "lan78xx-mdiobus";
1778 snprintf(dev
->mdiobus
->id
, MII_BUS_ID_SIZE
, "usb-%03d:%03d",
1779 dev
->udev
->bus
->busnum
, dev
->udev
->devnum
);
1781 switch (dev
->chipid
) {
1782 case ID_REV_CHIP_ID_7800_
:
1783 case ID_REV_CHIP_ID_7850_
:
1784 /* set to internal PHY id */
1785 dev
->mdiobus
->phy_mask
= ~(1 << 1);
1787 case ID_REV_CHIP_ID_7801_
:
1788 /* scan thru PHYAD[2..0] */
1789 dev
->mdiobus
->phy_mask
= ~(0xFF);
1793 ret
= mdiobus_register(dev
->mdiobus
);
1795 netdev_err(dev
->net
, "can't register MDIO bus\n");
1799 netdev_dbg(dev
->net
, "registered mdiobus bus %s\n", dev
->mdiobus
->id
);
1802 mdiobus_free(dev
->mdiobus
);
1806 static void lan78xx_remove_mdio(struct lan78xx_net
*dev
)
1808 mdiobus_unregister(dev
->mdiobus
);
1809 mdiobus_free(dev
->mdiobus
);
1812 static void lan78xx_link_status_change(struct net_device
*net
)
1814 struct phy_device
*phydev
= net
->phydev
;
1817 /* At forced 100 F/H mode, chip may fail to set mode correctly
1818 * when cable is switched between long(~50+m) and short one.
1819 * As workaround, set to 10 before setting to 100
1820 * at forced 100 F/H mode.
1822 if (!phydev
->autoneg
&& (phydev
->speed
== 100)) {
1823 /* disable phy interrupt */
1824 temp
= phy_read(phydev
, LAN88XX_INT_MASK
);
1825 temp
&= ~LAN88XX_INT_MASK_MDINTPIN_EN_
;
1826 ret
= phy_write(phydev
, LAN88XX_INT_MASK
, temp
);
1828 temp
= phy_read(phydev
, MII_BMCR
);
1829 temp
&= ~(BMCR_SPEED100
| BMCR_SPEED1000
);
1830 phy_write(phydev
, MII_BMCR
, temp
); /* set to 10 first */
1831 temp
|= BMCR_SPEED100
;
1832 phy_write(phydev
, MII_BMCR
, temp
); /* set to 100 later */
1834 /* clear pending interrupt generated while workaround */
1835 temp
= phy_read(phydev
, LAN88XX_INT_STS
);
1837 /* enable phy interrupt back */
1838 temp
= phy_read(phydev
, LAN88XX_INT_MASK
);
1839 temp
|= LAN88XX_INT_MASK_MDINTPIN_EN_
;
1840 ret
= phy_write(phydev
, LAN88XX_INT_MASK
, temp
);
1844 static int irq_map(struct irq_domain
*d
, unsigned int irq
,
1845 irq_hw_number_t hwirq
)
1847 struct irq_domain_data
*data
= d
->host_data
;
1849 irq_set_chip_data(irq
, data
);
1850 irq_set_chip_and_handler(irq
, data
->irqchip
, data
->irq_handler
);
1851 irq_set_noprobe(irq
);
1856 static void irq_unmap(struct irq_domain
*d
, unsigned int irq
)
1858 irq_set_chip_and_handler(irq
, NULL
, NULL
);
1859 irq_set_chip_data(irq
, NULL
);
1862 static const struct irq_domain_ops chip_domain_ops
= {
1867 static void lan78xx_irq_mask(struct irq_data
*irqd
)
1869 struct irq_domain_data
*data
= irq_data_get_irq_chip_data(irqd
);
1871 data
->irqenable
&= ~BIT(irqd_to_hwirq(irqd
));
1874 static void lan78xx_irq_unmask(struct irq_data
*irqd
)
1876 struct irq_domain_data
*data
= irq_data_get_irq_chip_data(irqd
);
1878 data
->irqenable
|= BIT(irqd_to_hwirq(irqd
));
1881 static void lan78xx_irq_bus_lock(struct irq_data
*irqd
)
1883 struct irq_domain_data
*data
= irq_data_get_irq_chip_data(irqd
);
1885 mutex_lock(&data
->irq_lock
);
1888 static void lan78xx_irq_bus_sync_unlock(struct irq_data
*irqd
)
1890 struct irq_domain_data
*data
= irq_data_get_irq_chip_data(irqd
);
1891 struct lan78xx_net
*dev
=
1892 container_of(data
, struct lan78xx_net
, domain_data
);
1896 /* call register access here because irq_bus_lock & irq_bus_sync_unlock
1897 * are only two callbacks executed in non-atomic contex.
1899 ret
= lan78xx_read_reg(dev
, INT_EP_CTL
, &buf
);
1900 if (buf
!= data
->irqenable
)
1901 ret
= lan78xx_write_reg(dev
, INT_EP_CTL
, data
->irqenable
);
1903 mutex_unlock(&data
->irq_lock
);
1906 static struct irq_chip lan78xx_irqchip
= {
1907 .name
= "lan78xx-irqs",
1908 .irq_mask
= lan78xx_irq_mask
,
1909 .irq_unmask
= lan78xx_irq_unmask
,
1910 .irq_bus_lock
= lan78xx_irq_bus_lock
,
1911 .irq_bus_sync_unlock
= lan78xx_irq_bus_sync_unlock
,
1914 static int lan78xx_setup_irq_domain(struct lan78xx_net
*dev
)
1916 struct device_node
*of_node
;
1917 struct irq_domain
*irqdomain
;
1918 unsigned int irqmap
= 0;
1922 of_node
= dev
->udev
->dev
.parent
->of_node
;
1924 mutex_init(&dev
->domain_data
.irq_lock
);
1926 lan78xx_read_reg(dev
, INT_EP_CTL
, &buf
);
1927 dev
->domain_data
.irqenable
= buf
;
1929 dev
->domain_data
.irqchip
= &lan78xx_irqchip
;
1930 dev
->domain_data
.irq_handler
= handle_simple_irq
;
1932 irqdomain
= irq_domain_add_simple(of_node
, MAX_INT_EP
, 0,
1933 &chip_domain_ops
, &dev
->domain_data
);
1935 /* create mapping for PHY interrupt */
1936 irqmap
= irq_create_mapping(irqdomain
, INT_EP_PHY
);
1938 irq_domain_remove(irqdomain
);
1947 dev
->domain_data
.irqdomain
= irqdomain
;
1948 dev
->domain_data
.phyirq
= irqmap
;
1953 static void lan78xx_remove_irq_domain(struct lan78xx_net
*dev
)
1955 if (dev
->domain_data
.phyirq
> 0) {
1956 irq_dispose_mapping(dev
->domain_data
.phyirq
);
1958 if (dev
->domain_data
.irqdomain
)
1959 irq_domain_remove(dev
->domain_data
.irqdomain
);
1961 dev
->domain_data
.phyirq
= 0;
1962 dev
->domain_data
.irqdomain
= NULL
;
1965 static int lan8835_fixup(struct phy_device
*phydev
)
1969 struct lan78xx_net
*dev
= netdev_priv(phydev
->attached_dev
);
1971 /* LED2/PME_N/IRQ_N/RGMII_ID pin to IRQ_N mode */
1972 buf
= phy_read_mmd(phydev
, MDIO_MMD_PCS
, 0x8010);
1975 phy_write_mmd(phydev
, MDIO_MMD_PCS
, 0x8010, buf
);
1977 /* RGMII MAC TXC Delay Enable */
1978 ret
= lan78xx_write_reg(dev
, MAC_RGMII_ID
,
1979 MAC_RGMII_ID_TXC_DELAY_EN_
);
1981 /* RGMII TX DLL Tune Adjust */
1982 ret
= lan78xx_write_reg(dev
, RGMII_TX_BYP_DLL
, 0x3D00);
1984 dev
->interface
= PHY_INTERFACE_MODE_RGMII_TXID
;
1989 static int ksz9031rnx_fixup(struct phy_device
*phydev
)
1991 struct lan78xx_net
*dev
= netdev_priv(phydev
->attached_dev
);
1993 /* Micrel9301RNX PHY configuration */
1994 /* RGMII Control Signal Pad Skew */
1995 phy_write_mmd(phydev
, MDIO_MMD_WIS
, 4, 0x0077);
1996 /* RGMII RX Data Pad Skew */
1997 phy_write_mmd(phydev
, MDIO_MMD_WIS
, 5, 0x7777);
1998 /* RGMII RX Clock Pad Skew */
1999 phy_write_mmd(phydev
, MDIO_MMD_WIS
, 8, 0x1FF);
2001 dev
->interface
= PHY_INTERFACE_MODE_RGMII_RXID
;
2006 static int lan78xx_phy_init(struct lan78xx_net
*dev
)
2010 struct phy_device
*phydev
;
2012 phydev
= phy_find_first(dev
->mdiobus
);
2014 netdev_err(dev
->net
, "no PHY found\n");
2018 if ((dev
->chipid
== ID_REV_CHIP_ID_7800_
) ||
2019 (dev
->chipid
== ID_REV_CHIP_ID_7850_
)) {
2020 phydev
->is_internal
= true;
2021 dev
->interface
= PHY_INTERFACE_MODE_GMII
;
2023 } else if (dev
->chipid
== ID_REV_CHIP_ID_7801_
) {
2025 netdev_err(dev
->net
, "no PHY driver found\n");
2029 dev
->interface
= PHY_INTERFACE_MODE_RGMII
;
2031 /* external PHY fixup for KSZ9031RNX */
2032 ret
= phy_register_fixup_for_uid(PHY_KSZ9031RNX
, 0xfffffff0,
2035 netdev_err(dev
->net
, "fail to register fixup\n");
2038 /* external PHY fixup for LAN8835 */
2039 ret
= phy_register_fixup_for_uid(PHY_LAN8835
, 0xfffffff0,
2042 netdev_err(dev
->net
, "fail to register fixup\n");
2045 /* add more external PHY fixup here if needed */
2047 phydev
->is_internal
= false;
2049 netdev_err(dev
->net
, "unknown ID found\n");
2054 /* if phyirq is not set, use polling mode in phylib */
2055 if (dev
->domain_data
.phyirq
> 0)
2056 phydev
->irq
= dev
->domain_data
.phyirq
;
2059 netdev_dbg(dev
->net
, "phydev->irq = %d\n", phydev
->irq
);
2061 /* set to AUTOMDIX */
2062 phydev
->mdix
= ETH_TP_MDI_AUTO
;
2064 ret
= phy_connect_direct(dev
->net
, phydev
,
2065 lan78xx_link_status_change
,
2068 netdev_err(dev
->net
, "can't attach PHY to %s\n",
2073 /* MAC doesn't support 1000T Half */
2074 phydev
->supported
&= ~SUPPORTED_1000baseT_Half
;
2076 /* support both flow controls */
2077 dev
->fc_request_control
= (FLOW_CTRL_RX
| FLOW_CTRL_TX
);
2078 phydev
->advertising
&= ~(ADVERTISED_Pause
| ADVERTISED_Asym_Pause
);
2079 mii_adv
= (u32
)mii_advertise_flowctrl(dev
->fc_request_control
);
2080 phydev
->advertising
|= mii_adv_to_ethtool_adv_t(mii_adv
);
2082 genphy_config_aneg(phydev
);
2084 dev
->fc_autoneg
= phydev
->autoneg
;
2088 netif_dbg(dev
, ifup
, dev
->net
, "phy initialised successfully");
2093 phy_unregister_fixup_for_uid(PHY_KSZ9031RNX
, 0xfffffff0);
2094 phy_unregister_fixup_for_uid(PHY_LAN8835
, 0xfffffff0);
2099 static int lan78xx_set_rx_max_frame_length(struct lan78xx_net
*dev
, int size
)
2105 ret
= lan78xx_read_reg(dev
, MAC_RX
, &buf
);
2107 rxenabled
= ((buf
& MAC_RX_RXEN_
) != 0);
2110 buf
&= ~MAC_RX_RXEN_
;
2111 ret
= lan78xx_write_reg(dev
, MAC_RX
, buf
);
2114 /* add 4 to size for FCS */
2115 buf
&= ~MAC_RX_MAX_SIZE_MASK_
;
2116 buf
|= (((size
+ 4) << MAC_RX_MAX_SIZE_SHIFT_
) & MAC_RX_MAX_SIZE_MASK_
);
2118 ret
= lan78xx_write_reg(dev
, MAC_RX
, buf
);
2121 buf
|= MAC_RX_RXEN_
;
2122 ret
= lan78xx_write_reg(dev
, MAC_RX
, buf
);
2128 static int unlink_urbs(struct lan78xx_net
*dev
, struct sk_buff_head
*q
)
2130 struct sk_buff
*skb
;
2131 unsigned long flags
;
2134 spin_lock_irqsave(&q
->lock
, flags
);
2135 while (!skb_queue_empty(q
)) {
2136 struct skb_data
*entry
;
2140 skb_queue_walk(q
, skb
) {
2141 entry
= (struct skb_data
*)skb
->cb
;
2142 if (entry
->state
!= unlink_start
)
2147 entry
->state
= unlink_start
;
2150 /* Get reference count of the URB to avoid it to be
2151 * freed during usb_unlink_urb, which may trigger
2152 * use-after-free problem inside usb_unlink_urb since
2153 * usb_unlink_urb is always racing with .complete
2154 * handler(include defer_bh).
2157 spin_unlock_irqrestore(&q
->lock
, flags
);
2158 /* during some PM-driven resume scenarios,
2159 * these (async) unlinks complete immediately
2161 ret
= usb_unlink_urb(urb
);
2162 if (ret
!= -EINPROGRESS
&& ret
!= 0)
2163 netdev_dbg(dev
->net
, "unlink urb err, %d\n", ret
);
2167 spin_lock_irqsave(&q
->lock
, flags
);
2169 spin_unlock_irqrestore(&q
->lock
, flags
);
2173 static int lan78xx_change_mtu(struct net_device
*netdev
, int new_mtu
)
2175 struct lan78xx_net
*dev
= netdev_priv(netdev
);
2176 int ll_mtu
= new_mtu
+ netdev
->hard_header_len
;
2177 int old_hard_mtu
= dev
->hard_mtu
;
2178 int old_rx_urb_size
= dev
->rx_urb_size
;
2181 /* no second zero-length packet read wanted after mtu-sized packets */
2182 if ((ll_mtu
% dev
->maxpacket
) == 0)
2185 ret
= lan78xx_set_rx_max_frame_length(dev
, new_mtu
+ ETH_HLEN
);
2187 netdev
->mtu
= new_mtu
;
2189 dev
->hard_mtu
= netdev
->mtu
+ netdev
->hard_header_len
;
2190 if (dev
->rx_urb_size
== old_hard_mtu
) {
2191 dev
->rx_urb_size
= dev
->hard_mtu
;
2192 if (dev
->rx_urb_size
> old_rx_urb_size
) {
2193 if (netif_running(dev
->net
)) {
2194 unlink_urbs(dev
, &dev
->rxq
);
2195 tasklet_schedule(&dev
->bh
);
2203 static int lan78xx_set_mac_addr(struct net_device
*netdev
, void *p
)
2205 struct lan78xx_net
*dev
= netdev_priv(netdev
);
2206 struct sockaddr
*addr
= p
;
2207 u32 addr_lo
, addr_hi
;
2210 if (netif_running(netdev
))
2213 if (!is_valid_ether_addr(addr
->sa_data
))
2214 return -EADDRNOTAVAIL
;
2216 ether_addr_copy(netdev
->dev_addr
, addr
->sa_data
);
2218 addr_lo
= netdev
->dev_addr
[0] |
2219 netdev
->dev_addr
[1] << 8 |
2220 netdev
->dev_addr
[2] << 16 |
2221 netdev
->dev_addr
[3] << 24;
2222 addr_hi
= netdev
->dev_addr
[4] |
2223 netdev
->dev_addr
[5] << 8;
2225 ret
= lan78xx_write_reg(dev
, RX_ADDRL
, addr_lo
);
2226 ret
= lan78xx_write_reg(dev
, RX_ADDRH
, addr_hi
);
2231 /* Enable or disable Rx checksum offload engine */
2232 static int lan78xx_set_features(struct net_device
*netdev
,
2233 netdev_features_t features
)
2235 struct lan78xx_net
*dev
= netdev_priv(netdev
);
2236 struct lan78xx_priv
*pdata
= (struct lan78xx_priv
*)(dev
->data
[0]);
2237 unsigned long flags
;
2240 spin_lock_irqsave(&pdata
->rfe_ctl_lock
, flags
);
2242 if (features
& NETIF_F_RXCSUM
) {
2243 pdata
->rfe_ctl
|= RFE_CTL_TCPUDP_COE_
| RFE_CTL_IP_COE_
;
2244 pdata
->rfe_ctl
|= RFE_CTL_ICMP_COE_
| RFE_CTL_IGMP_COE_
;
2246 pdata
->rfe_ctl
&= ~(RFE_CTL_TCPUDP_COE_
| RFE_CTL_IP_COE_
);
2247 pdata
->rfe_ctl
&= ~(RFE_CTL_ICMP_COE_
| RFE_CTL_IGMP_COE_
);
2250 if (features
& NETIF_F_HW_VLAN_CTAG_RX
)
2251 pdata
->rfe_ctl
|= RFE_CTL_VLAN_FILTER_
;
2253 pdata
->rfe_ctl
&= ~RFE_CTL_VLAN_FILTER_
;
2255 spin_unlock_irqrestore(&pdata
->rfe_ctl_lock
, flags
);
2257 ret
= lan78xx_write_reg(dev
, RFE_CTL
, pdata
->rfe_ctl
);
2262 static void lan78xx_deferred_vlan_write(struct work_struct
*param
)
2264 struct lan78xx_priv
*pdata
=
2265 container_of(param
, struct lan78xx_priv
, set_vlan
);
2266 struct lan78xx_net
*dev
= pdata
->dev
;
2268 lan78xx_dataport_write(dev
, DP_SEL_RSEL_VLAN_DA_
, 0,
2269 DP_SEL_VHF_VLAN_LEN
, pdata
->vlan_table
);
2272 static int lan78xx_vlan_rx_add_vid(struct net_device
*netdev
,
2273 __be16 proto
, u16 vid
)
2275 struct lan78xx_net
*dev
= netdev_priv(netdev
);
2276 struct lan78xx_priv
*pdata
= (struct lan78xx_priv
*)(dev
->data
[0]);
2278 u16 vid_dword_index
;
2280 vid_dword_index
= (vid
>> 5) & 0x7F;
2281 vid_bit_index
= vid
& 0x1F;
2283 pdata
->vlan_table
[vid_dword_index
] |= (1 << vid_bit_index
);
2285 /* defer register writes to a sleepable context */
2286 schedule_work(&pdata
->set_vlan
);
2291 static int lan78xx_vlan_rx_kill_vid(struct net_device
*netdev
,
2292 __be16 proto
, u16 vid
)
2294 struct lan78xx_net
*dev
= netdev_priv(netdev
);
2295 struct lan78xx_priv
*pdata
= (struct lan78xx_priv
*)(dev
->data
[0]);
2297 u16 vid_dword_index
;
2299 vid_dword_index
= (vid
>> 5) & 0x7F;
2300 vid_bit_index
= vid
& 0x1F;
2302 pdata
->vlan_table
[vid_dword_index
] &= ~(1 << vid_bit_index
);
2304 /* defer register writes to a sleepable context */
2305 schedule_work(&pdata
->set_vlan
);
2310 static void lan78xx_init_ltm(struct lan78xx_net
*dev
)
2314 u32 regs
[6] = { 0 };
2316 ret
= lan78xx_read_reg(dev
, USB_CFG1
, &buf
);
2317 if (buf
& USB_CFG1_LTM_ENABLE_
) {
2319 /* Get values from EEPROM first */
2320 if (lan78xx_read_eeprom(dev
, 0x3F, 2, temp
) == 0) {
2321 if (temp
[0] == 24) {
2322 ret
= lan78xx_read_raw_eeprom(dev
,
2329 } else if (lan78xx_read_otp(dev
, 0x3F, 2, temp
) == 0) {
2330 if (temp
[0] == 24) {
2331 ret
= lan78xx_read_raw_otp(dev
,
2341 lan78xx_write_reg(dev
, LTM_BELT_IDLE0
, regs
[0]);
2342 lan78xx_write_reg(dev
, LTM_BELT_IDLE1
, regs
[1]);
2343 lan78xx_write_reg(dev
, LTM_BELT_ACT0
, regs
[2]);
2344 lan78xx_write_reg(dev
, LTM_BELT_ACT1
, regs
[3]);
2345 lan78xx_write_reg(dev
, LTM_INACTIVE0
, regs
[4]);
2346 lan78xx_write_reg(dev
, LTM_INACTIVE1
, regs
[5]);
2349 static int lan78xx_reset(struct lan78xx_net
*dev
)
2351 struct lan78xx_priv
*pdata
= (struct lan78xx_priv
*)(dev
->data
[0]);
2354 unsigned long timeout
;
2357 ret
= lan78xx_read_reg(dev
, HW_CFG
, &buf
);
2358 buf
|= HW_CFG_LRST_
;
2359 ret
= lan78xx_write_reg(dev
, HW_CFG
, buf
);
2361 timeout
= jiffies
+ HZ
;
2364 ret
= lan78xx_read_reg(dev
, HW_CFG
, &buf
);
2365 if (time_after(jiffies
, timeout
)) {
2366 netdev_warn(dev
->net
,
2367 "timeout on completion of LiteReset");
2370 } while (buf
& HW_CFG_LRST_
);
2372 lan78xx_init_mac_address(dev
);
2374 /* save DEVID for later usage */
2375 ret
= lan78xx_read_reg(dev
, ID_REV
, &buf
);
2376 dev
->chipid
= (buf
& ID_REV_CHIP_ID_MASK_
) >> 16;
2377 dev
->chiprev
= buf
& ID_REV_CHIP_REV_MASK_
;
2379 /* Respond to the IN token with a NAK */
2380 ret
= lan78xx_read_reg(dev
, USB_CFG0
, &buf
);
2381 buf
|= USB_CFG_BIR_
;
2382 ret
= lan78xx_write_reg(dev
, USB_CFG0
, buf
);
2385 lan78xx_init_ltm(dev
);
2387 if (dev
->udev
->speed
== USB_SPEED_SUPER
) {
2388 buf
= DEFAULT_BURST_CAP_SIZE
/ SS_USB_PKT_SIZE
;
2389 dev
->rx_urb_size
= DEFAULT_BURST_CAP_SIZE
;
2392 } else if (dev
->udev
->speed
== USB_SPEED_HIGH
) {
2393 buf
= DEFAULT_BURST_CAP_SIZE
/ HS_USB_PKT_SIZE
;
2394 dev
->rx_urb_size
= DEFAULT_BURST_CAP_SIZE
;
2395 dev
->rx_qlen
= RX_MAX_QUEUE_MEMORY
/ dev
->rx_urb_size
;
2396 dev
->tx_qlen
= RX_MAX_QUEUE_MEMORY
/ dev
->hard_mtu
;
2398 buf
= DEFAULT_BURST_CAP_SIZE
/ FS_USB_PKT_SIZE
;
2399 dev
->rx_urb_size
= DEFAULT_BURST_CAP_SIZE
;
2404 ret
= lan78xx_write_reg(dev
, BURST_CAP
, buf
);
2405 ret
= lan78xx_write_reg(dev
, BULK_IN_DLY
, DEFAULT_BULK_IN_DELAY
);
2407 ret
= lan78xx_read_reg(dev
, HW_CFG
, &buf
);
2409 ret
= lan78xx_write_reg(dev
, HW_CFG
, buf
);
2411 ret
= lan78xx_read_reg(dev
, USB_CFG0
, &buf
);
2412 buf
|= USB_CFG_BCE_
;
2413 ret
= lan78xx_write_reg(dev
, USB_CFG0
, buf
);
2415 /* set FIFO sizes */
2416 buf
= (MAX_RX_FIFO_SIZE
- 512) / 512;
2417 ret
= lan78xx_write_reg(dev
, FCT_RX_FIFO_END
, buf
);
2419 buf
= (MAX_TX_FIFO_SIZE
- 512) / 512;
2420 ret
= lan78xx_write_reg(dev
, FCT_TX_FIFO_END
, buf
);
2422 ret
= lan78xx_write_reg(dev
, INT_STS
, INT_STS_CLEAR_ALL_
);
2423 ret
= lan78xx_write_reg(dev
, FLOW
, 0);
2424 ret
= lan78xx_write_reg(dev
, FCT_FLOW
, 0);
2426 /* Don't need rfe_ctl_lock during initialisation */
2427 ret
= lan78xx_read_reg(dev
, RFE_CTL
, &pdata
->rfe_ctl
);
2428 pdata
->rfe_ctl
|= RFE_CTL_BCAST_EN_
| RFE_CTL_DA_PERFECT_
;
2429 ret
= lan78xx_write_reg(dev
, RFE_CTL
, pdata
->rfe_ctl
);
2431 /* Enable or disable checksum offload engines */
2432 lan78xx_set_features(dev
->net
, dev
->net
->features
);
2434 lan78xx_set_multicast(dev
->net
);
2437 ret
= lan78xx_read_reg(dev
, PMT_CTL
, &buf
);
2438 buf
|= PMT_CTL_PHY_RST_
;
2439 ret
= lan78xx_write_reg(dev
, PMT_CTL
, buf
);
2441 timeout
= jiffies
+ HZ
;
2444 ret
= lan78xx_read_reg(dev
, PMT_CTL
, &buf
);
2445 if (time_after(jiffies
, timeout
)) {
2446 netdev_warn(dev
->net
, "timeout waiting for PHY Reset");
2449 } while ((buf
& PMT_CTL_PHY_RST_
) || !(buf
& PMT_CTL_READY_
));
2451 ret
= lan78xx_read_reg(dev
, MAC_CR
, &buf
);
2452 /* LAN7801 only has RGMII mode */
2453 if (dev
->chipid
== ID_REV_CHIP_ID_7801_
)
2454 buf
&= ~MAC_CR_GMII_EN_
;
2456 if (dev
->chipid
== ID_REV_CHIP_ID_7800_
) {
2457 ret
= lan78xx_read_raw_eeprom(dev
, 0, 1, &sig
);
2458 if (!ret
&& sig
!= EEPROM_INDICATOR
) {
2459 /* Implies there is no external eeprom. Set mac speed */
2460 netdev_info(dev
->net
, "No External EEPROM. Setting MAC Speed\n");
2461 buf
|= MAC_CR_AUTO_DUPLEX_
| MAC_CR_AUTO_SPEED_
;
2464 ret
= lan78xx_write_reg(dev
, MAC_CR
, buf
);
2466 ret
= lan78xx_read_reg(dev
, MAC_TX
, &buf
);
2467 buf
|= MAC_TX_TXEN_
;
2468 ret
= lan78xx_write_reg(dev
, MAC_TX
, buf
);
2470 ret
= lan78xx_read_reg(dev
, FCT_TX_CTL
, &buf
);
2471 buf
|= FCT_TX_CTL_EN_
;
2472 ret
= lan78xx_write_reg(dev
, FCT_TX_CTL
, buf
);
2474 ret
= lan78xx_set_rx_max_frame_length(dev
, dev
->net
->mtu
+ ETH_HLEN
);
2476 ret
= lan78xx_read_reg(dev
, MAC_RX
, &buf
);
2477 buf
|= MAC_RX_RXEN_
;
2478 ret
= lan78xx_write_reg(dev
, MAC_RX
, buf
);
2480 ret
= lan78xx_read_reg(dev
, FCT_RX_CTL
, &buf
);
2481 buf
|= FCT_RX_CTL_EN_
;
2482 ret
= lan78xx_write_reg(dev
, FCT_RX_CTL
, buf
);
2487 static void lan78xx_init_stats(struct lan78xx_net
*dev
)
2492 /* initialize for stats update
2493 * some counters are 20bits and some are 32bits
2495 p
= (u32
*)&dev
->stats
.rollover_max
;
2496 for (i
= 0; i
< (sizeof(dev
->stats
.rollover_max
) / (sizeof(u32
))); i
++)
2499 dev
->stats
.rollover_max
.rx_unicast_byte_count
= 0xFFFFFFFF;
2500 dev
->stats
.rollover_max
.rx_broadcast_byte_count
= 0xFFFFFFFF;
2501 dev
->stats
.rollover_max
.rx_multicast_byte_count
= 0xFFFFFFFF;
2502 dev
->stats
.rollover_max
.eee_rx_lpi_transitions
= 0xFFFFFFFF;
2503 dev
->stats
.rollover_max
.eee_rx_lpi_time
= 0xFFFFFFFF;
2504 dev
->stats
.rollover_max
.tx_unicast_byte_count
= 0xFFFFFFFF;
2505 dev
->stats
.rollover_max
.tx_broadcast_byte_count
= 0xFFFFFFFF;
2506 dev
->stats
.rollover_max
.tx_multicast_byte_count
= 0xFFFFFFFF;
2507 dev
->stats
.rollover_max
.eee_tx_lpi_transitions
= 0xFFFFFFFF;
2508 dev
->stats
.rollover_max
.eee_tx_lpi_time
= 0xFFFFFFFF;
2510 lan78xx_defer_kevent(dev
, EVENT_STAT_UPDATE
);
2513 static int lan78xx_open(struct net_device
*net
)
2515 struct lan78xx_net
*dev
= netdev_priv(net
);
2518 ret
= usb_autopm_get_interface(dev
->intf
);
2522 ret
= lan78xx_reset(dev
);
2526 ret
= lan78xx_phy_init(dev
);
2530 /* for Link Check */
2531 if (dev
->urb_intr
) {
2532 ret
= usb_submit_urb(dev
->urb_intr
, GFP_KERNEL
);
2534 netif_err(dev
, ifup
, dev
->net
,
2535 "intr submit %d\n", ret
);
2540 lan78xx_init_stats(dev
);
2542 set_bit(EVENT_DEV_OPEN
, &dev
->flags
);
2544 netif_start_queue(net
);
2546 dev
->link_on
= false;
2548 lan78xx_defer_kevent(dev
, EVENT_LINK_RESET
);
2550 usb_autopm_put_interface(dev
->intf
);
2556 static void lan78xx_terminate_urbs(struct lan78xx_net
*dev
)
2558 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(unlink_wakeup
);
2559 DECLARE_WAITQUEUE(wait
, current
);
2562 /* ensure there are no more active urbs */
2563 add_wait_queue(&unlink_wakeup
, &wait
);
2564 set_current_state(TASK_UNINTERRUPTIBLE
);
2565 dev
->wait
= &unlink_wakeup
;
2566 temp
= unlink_urbs(dev
, &dev
->txq
) + unlink_urbs(dev
, &dev
->rxq
);
2568 /* maybe wait for deletions to finish. */
2569 while (!skb_queue_empty(&dev
->rxq
) &&
2570 !skb_queue_empty(&dev
->txq
) &&
2571 !skb_queue_empty(&dev
->done
)) {
2572 schedule_timeout(msecs_to_jiffies(UNLINK_TIMEOUT_MS
));
2573 set_current_state(TASK_UNINTERRUPTIBLE
);
2574 netif_dbg(dev
, ifdown
, dev
->net
,
2575 "waited for %d urb completions\n", temp
);
2577 set_current_state(TASK_RUNNING
);
2579 remove_wait_queue(&unlink_wakeup
, &wait
);
2582 static int lan78xx_stop(struct net_device
*net
)
2584 struct lan78xx_net
*dev
= netdev_priv(net
);
2586 if (timer_pending(&dev
->stat_monitor
))
2587 del_timer_sync(&dev
->stat_monitor
);
2589 phy_unregister_fixup_for_uid(PHY_KSZ9031RNX
, 0xfffffff0);
2590 phy_unregister_fixup_for_uid(PHY_LAN8835
, 0xfffffff0);
2592 phy_stop(net
->phydev
);
2593 phy_disconnect(net
->phydev
);
2597 clear_bit(EVENT_DEV_OPEN
, &dev
->flags
);
2598 netif_stop_queue(net
);
2600 netif_info(dev
, ifdown
, dev
->net
,
2601 "stop stats: rx/tx %lu/%lu, errs %lu/%lu\n",
2602 net
->stats
.rx_packets
, net
->stats
.tx_packets
,
2603 net
->stats
.rx_errors
, net
->stats
.tx_errors
);
2605 lan78xx_terminate_urbs(dev
);
2607 usb_kill_urb(dev
->urb_intr
);
2609 skb_queue_purge(&dev
->rxq_pause
);
2611 /* deferred work (task, timer, softirq) must also stop.
2612 * can't flush_scheduled_work() until we drop rtnl (later),
2613 * else workers could deadlock; so make workers a NOP.
2616 cancel_delayed_work_sync(&dev
->wq
);
2617 tasklet_kill(&dev
->bh
);
2619 usb_autopm_put_interface(dev
->intf
);
2624 static int lan78xx_linearize(struct sk_buff
*skb
)
2626 return skb_linearize(skb
);
2629 static struct sk_buff
*lan78xx_tx_prep(struct lan78xx_net
*dev
,
2630 struct sk_buff
*skb
, gfp_t flags
)
2632 u32 tx_cmd_a
, tx_cmd_b
;
2634 if (skb_cow_head(skb
, TX_OVERHEAD
)) {
2635 dev_kfree_skb_any(skb
);
2639 if (lan78xx_linearize(skb
) < 0)
2642 tx_cmd_a
= (u32
)(skb
->len
& TX_CMD_A_LEN_MASK_
) | TX_CMD_A_FCS_
;
2644 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
2645 tx_cmd_a
|= TX_CMD_A_IPE_
| TX_CMD_A_TPE_
;
2648 if (skb_is_gso(skb
)) {
2649 u16 mss
= max(skb_shinfo(skb
)->gso_size
, TX_CMD_B_MSS_MIN_
);
2651 tx_cmd_b
= (mss
<< TX_CMD_B_MSS_SHIFT_
) & TX_CMD_B_MSS_MASK_
;
2653 tx_cmd_a
|= TX_CMD_A_LSO_
;
2656 if (skb_vlan_tag_present(skb
)) {
2657 tx_cmd_a
|= TX_CMD_A_IVTG_
;
2658 tx_cmd_b
|= skb_vlan_tag_get(skb
) & TX_CMD_B_VTAG_MASK_
;
2662 cpu_to_le32s(&tx_cmd_b
);
2663 memcpy(skb
->data
, &tx_cmd_b
, 4);
2666 cpu_to_le32s(&tx_cmd_a
);
2667 memcpy(skb
->data
, &tx_cmd_a
, 4);
2672 static enum skb_state
defer_bh(struct lan78xx_net
*dev
, struct sk_buff
*skb
,
2673 struct sk_buff_head
*list
, enum skb_state state
)
2675 unsigned long flags
;
2676 enum skb_state old_state
;
2677 struct skb_data
*entry
= (struct skb_data
*)skb
->cb
;
2679 spin_lock_irqsave(&list
->lock
, flags
);
2680 old_state
= entry
->state
;
2681 entry
->state
= state
;
2683 __skb_unlink(skb
, list
);
2684 spin_unlock(&list
->lock
);
2685 spin_lock(&dev
->done
.lock
);
2687 __skb_queue_tail(&dev
->done
, skb
);
2688 if (skb_queue_len(&dev
->done
) == 1)
2689 tasklet_schedule(&dev
->bh
);
2690 spin_unlock_irqrestore(&dev
->done
.lock
, flags
);
2695 static void tx_complete(struct urb
*urb
)
2697 struct sk_buff
*skb
= (struct sk_buff
*)urb
->context
;
2698 struct skb_data
*entry
= (struct skb_data
*)skb
->cb
;
2699 struct lan78xx_net
*dev
= entry
->dev
;
2701 if (urb
->status
== 0) {
2702 dev
->net
->stats
.tx_packets
+= entry
->num_of_packet
;
2703 dev
->net
->stats
.tx_bytes
+= entry
->length
;
2705 dev
->net
->stats
.tx_errors
++;
2707 switch (urb
->status
) {
2709 lan78xx_defer_kevent(dev
, EVENT_TX_HALT
);
2712 /* software-driven interface shutdown */
2720 netif_stop_queue(dev
->net
);
2723 netif_dbg(dev
, tx_err
, dev
->net
,
2724 "tx err %d\n", entry
->urb
->status
);
2729 usb_autopm_put_interface_async(dev
->intf
);
2731 defer_bh(dev
, skb
, &dev
->txq
, tx_done
);
2734 static void lan78xx_queue_skb(struct sk_buff_head
*list
,
2735 struct sk_buff
*newsk
, enum skb_state state
)
2737 struct skb_data
*entry
= (struct skb_data
*)newsk
->cb
;
2739 __skb_queue_tail(list
, newsk
);
2740 entry
->state
= state
;
2744 lan78xx_start_xmit(struct sk_buff
*skb
, struct net_device
*net
)
2746 struct lan78xx_net
*dev
= netdev_priv(net
);
2747 struct sk_buff
*skb2
= NULL
;
2750 skb_tx_timestamp(skb
);
2751 skb2
= lan78xx_tx_prep(dev
, skb
, GFP_ATOMIC
);
2755 skb_queue_tail(&dev
->txq_pend
, skb2
);
2757 /* throttle TX patch at slower than SUPER SPEED USB */
2758 if ((dev
->udev
->speed
< USB_SPEED_SUPER
) &&
2759 (skb_queue_len(&dev
->txq_pend
) > 10))
2760 netif_stop_queue(net
);
2762 netif_dbg(dev
, tx_err
, dev
->net
,
2763 "lan78xx_tx_prep return NULL\n");
2764 dev
->net
->stats
.tx_errors
++;
2765 dev
->net
->stats
.tx_dropped
++;
2768 tasklet_schedule(&dev
->bh
);
2770 return NETDEV_TX_OK
;
2774 lan78xx_get_endpoints(struct lan78xx_net
*dev
, struct usb_interface
*intf
)
2777 struct usb_host_interface
*alt
= NULL
;
2778 struct usb_host_endpoint
*in
= NULL
, *out
= NULL
;
2779 struct usb_host_endpoint
*status
= NULL
;
2781 for (tmp
= 0; tmp
< intf
->num_altsetting
; tmp
++) {
2787 alt
= intf
->altsetting
+ tmp
;
2789 for (ep
= 0; ep
< alt
->desc
.bNumEndpoints
; ep
++) {
2790 struct usb_host_endpoint
*e
;
2793 e
= alt
->endpoint
+ ep
;
2794 switch (e
->desc
.bmAttributes
) {
2795 case USB_ENDPOINT_XFER_INT
:
2796 if (!usb_endpoint_dir_in(&e
->desc
))
2800 case USB_ENDPOINT_XFER_BULK
:
2805 if (usb_endpoint_dir_in(&e
->desc
)) {
2808 else if (intr
&& !status
)
2818 if (!alt
|| !in
|| !out
)
2821 dev
->pipe_in
= usb_rcvbulkpipe(dev
->udev
,
2822 in
->desc
.bEndpointAddress
&
2823 USB_ENDPOINT_NUMBER_MASK
);
2824 dev
->pipe_out
= usb_sndbulkpipe(dev
->udev
,
2825 out
->desc
.bEndpointAddress
&
2826 USB_ENDPOINT_NUMBER_MASK
);
2827 dev
->ep_intr
= status
;
2832 static int lan78xx_bind(struct lan78xx_net
*dev
, struct usb_interface
*intf
)
2834 struct lan78xx_priv
*pdata
= NULL
;
2838 ret
= lan78xx_get_endpoints(dev
, intf
);
2840 dev
->data
[0] = (unsigned long)kzalloc(sizeof(*pdata
), GFP_KERNEL
);
2842 pdata
= (struct lan78xx_priv
*)(dev
->data
[0]);
2844 netdev_warn(dev
->net
, "Unable to allocate lan78xx_priv");
2850 spin_lock_init(&pdata
->rfe_ctl_lock
);
2851 mutex_init(&pdata
->dataport_mutex
);
2853 INIT_WORK(&pdata
->set_multicast
, lan78xx_deferred_multicast_write
);
2855 for (i
= 0; i
< DP_SEL_VHF_VLAN_LEN
; i
++)
2856 pdata
->vlan_table
[i
] = 0;
2858 INIT_WORK(&pdata
->set_vlan
, lan78xx_deferred_vlan_write
);
2860 dev
->net
->features
= 0;
2862 if (DEFAULT_TX_CSUM_ENABLE
)
2863 dev
->net
->features
|= NETIF_F_HW_CSUM
;
2865 if (DEFAULT_RX_CSUM_ENABLE
)
2866 dev
->net
->features
|= NETIF_F_RXCSUM
;
2868 if (DEFAULT_TSO_CSUM_ENABLE
)
2869 dev
->net
->features
|= NETIF_F_TSO
| NETIF_F_TSO6
| NETIF_F_SG
;
2871 dev
->net
->hw_features
= dev
->net
->features
;
2873 ret
= lan78xx_setup_irq_domain(dev
);
2875 netdev_warn(dev
->net
,
2876 "lan78xx_setup_irq_domain() failed : %d", ret
);
2880 dev
->net
->hard_header_len
+= TX_OVERHEAD
;
2881 dev
->hard_mtu
= dev
->net
->mtu
+ dev
->net
->hard_header_len
;
2883 /* Init all registers */
2884 ret
= lan78xx_reset(dev
);
2886 netdev_warn(dev
->net
, "Registers INIT FAILED....");
2890 ret
= lan78xx_mdio_init(dev
);
2892 netdev_warn(dev
->net
, "MDIO INIT FAILED.....");
2896 dev
->net
->flags
|= IFF_MULTICAST
;
2898 pdata
->wol
= WAKE_MAGIC
;
2903 lan78xx_remove_irq_domain(dev
);
2906 netdev_warn(dev
->net
, "Bind routine FAILED");
2907 cancel_work_sync(&pdata
->set_multicast
);
2908 cancel_work_sync(&pdata
->set_vlan
);
2913 static void lan78xx_unbind(struct lan78xx_net
*dev
, struct usb_interface
*intf
)
2915 struct lan78xx_priv
*pdata
= (struct lan78xx_priv
*)(dev
->data
[0]);
2917 lan78xx_remove_irq_domain(dev
);
2919 lan78xx_remove_mdio(dev
);
2922 cancel_work_sync(&pdata
->set_multicast
);
2923 cancel_work_sync(&pdata
->set_vlan
);
2924 netif_dbg(dev
, ifdown
, dev
->net
, "free pdata");
2931 static void lan78xx_rx_csum_offload(struct lan78xx_net
*dev
,
2932 struct sk_buff
*skb
,
2933 u32 rx_cmd_a
, u32 rx_cmd_b
)
2935 if (!(dev
->net
->features
& NETIF_F_RXCSUM
) ||
2936 unlikely(rx_cmd_a
& RX_CMD_A_ICSM_
)) {
2937 skb
->ip_summed
= CHECKSUM_NONE
;
2939 skb
->csum
= ntohs((u16
)(rx_cmd_b
>> RX_CMD_B_CSUM_SHIFT_
));
2940 skb
->ip_summed
= CHECKSUM_COMPLETE
;
2944 static void lan78xx_skb_return(struct lan78xx_net
*dev
, struct sk_buff
*skb
)
2948 if (test_bit(EVENT_RX_PAUSED
, &dev
->flags
)) {
2949 skb_queue_tail(&dev
->rxq_pause
, skb
);
2953 dev
->net
->stats
.rx_packets
++;
2954 dev
->net
->stats
.rx_bytes
+= skb
->len
;
2956 skb
->protocol
= eth_type_trans(skb
, dev
->net
);
2958 netif_dbg(dev
, rx_status
, dev
->net
, "< rx, len %zu, type 0x%x\n",
2959 skb
->len
+ sizeof(struct ethhdr
), skb
->protocol
);
2960 memset(skb
->cb
, 0, sizeof(struct skb_data
));
2962 if (skb_defer_rx_timestamp(skb
))
2965 status
= netif_rx(skb
);
2966 if (status
!= NET_RX_SUCCESS
)
2967 netif_dbg(dev
, rx_err
, dev
->net
,
2968 "netif_rx status %d\n", status
);
2971 static int lan78xx_rx(struct lan78xx_net
*dev
, struct sk_buff
*skb
)
2973 if (skb
->len
< dev
->net
->hard_header_len
)
2976 while (skb
->len
> 0) {
2977 u32 rx_cmd_a
, rx_cmd_b
, align_count
, size
;
2979 struct sk_buff
*skb2
;
2980 unsigned char *packet
;
2982 memcpy(&rx_cmd_a
, skb
->data
, sizeof(rx_cmd_a
));
2983 le32_to_cpus(&rx_cmd_a
);
2984 skb_pull(skb
, sizeof(rx_cmd_a
));
2986 memcpy(&rx_cmd_b
, skb
->data
, sizeof(rx_cmd_b
));
2987 le32_to_cpus(&rx_cmd_b
);
2988 skb_pull(skb
, sizeof(rx_cmd_b
));
2990 memcpy(&rx_cmd_c
, skb
->data
, sizeof(rx_cmd_c
));
2991 le16_to_cpus(&rx_cmd_c
);
2992 skb_pull(skb
, sizeof(rx_cmd_c
));
2996 /* get the packet length */
2997 size
= (rx_cmd_a
& RX_CMD_A_LEN_MASK_
);
2998 align_count
= (4 - ((size
+ RXW_PADDING
) % 4)) % 4;
3000 if (unlikely(rx_cmd_a
& RX_CMD_A_RED_
)) {
3001 netif_dbg(dev
, rx_err
, dev
->net
,
3002 "Error rx_cmd_a=0x%08x", rx_cmd_a
);
3004 /* last frame in this batch */
3005 if (skb
->len
== size
) {
3006 lan78xx_rx_csum_offload(dev
, skb
,
3007 rx_cmd_a
, rx_cmd_b
);
3009 skb_trim(skb
, skb
->len
- 4); /* remove fcs */
3010 skb
->truesize
= size
+ sizeof(struct sk_buff
);
3015 skb2
= skb_clone(skb
, GFP_ATOMIC
);
3016 if (unlikely(!skb2
)) {
3017 netdev_warn(dev
->net
, "Error allocating skb");
3022 skb2
->data
= packet
;
3023 skb_set_tail_pointer(skb2
, size
);
3025 lan78xx_rx_csum_offload(dev
, skb2
, rx_cmd_a
, rx_cmd_b
);
3027 skb_trim(skb2
, skb2
->len
- 4); /* remove fcs */
3028 skb2
->truesize
= size
+ sizeof(struct sk_buff
);
3030 lan78xx_skb_return(dev
, skb2
);
3033 skb_pull(skb
, size
);
3035 /* padding bytes before the next frame starts */
3037 skb_pull(skb
, align_count
);
3043 static inline void rx_process(struct lan78xx_net
*dev
, struct sk_buff
*skb
)
3045 if (!lan78xx_rx(dev
, skb
)) {
3046 dev
->net
->stats
.rx_errors
++;
3051 lan78xx_skb_return(dev
, skb
);
3055 netif_dbg(dev
, rx_err
, dev
->net
, "drop\n");
3056 dev
->net
->stats
.rx_errors
++;
3058 skb_queue_tail(&dev
->done
, skb
);
3061 static void rx_complete(struct urb
*urb
);
3063 static int rx_submit(struct lan78xx_net
*dev
, struct urb
*urb
, gfp_t flags
)
3065 struct sk_buff
*skb
;
3066 struct skb_data
*entry
;
3067 unsigned long lockflags
;
3068 size_t size
= dev
->rx_urb_size
;
3071 skb
= netdev_alloc_skb_ip_align(dev
->net
, size
);
3077 entry
= (struct skb_data
*)skb
->cb
;
3082 usb_fill_bulk_urb(urb
, dev
->udev
, dev
->pipe_in
,
3083 skb
->data
, size
, rx_complete
, skb
);
3085 spin_lock_irqsave(&dev
->rxq
.lock
, lockflags
);
3087 if (netif_device_present(dev
->net
) &&
3088 netif_running(dev
->net
) &&
3089 !test_bit(EVENT_RX_HALT
, &dev
->flags
) &&
3090 !test_bit(EVENT_DEV_ASLEEP
, &dev
->flags
)) {
3091 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
3094 lan78xx_queue_skb(&dev
->rxq
, skb
, rx_start
);
3097 lan78xx_defer_kevent(dev
, EVENT_RX_HALT
);
3100 netif_dbg(dev
, ifdown
, dev
->net
, "device gone\n");
3101 netif_device_detach(dev
->net
);
3107 netif_dbg(dev
, rx_err
, dev
->net
,
3108 "rx submit, %d\n", ret
);
3109 tasklet_schedule(&dev
->bh
);
3112 netif_dbg(dev
, ifdown
, dev
->net
, "rx: stopped\n");
3115 spin_unlock_irqrestore(&dev
->rxq
.lock
, lockflags
);
3117 dev_kfree_skb_any(skb
);
3123 static void rx_complete(struct urb
*urb
)
3125 struct sk_buff
*skb
= (struct sk_buff
*)urb
->context
;
3126 struct skb_data
*entry
= (struct skb_data
*)skb
->cb
;
3127 struct lan78xx_net
*dev
= entry
->dev
;
3128 int urb_status
= urb
->status
;
3129 enum skb_state state
;
3131 skb_put(skb
, urb
->actual_length
);
3135 switch (urb_status
) {
3137 if (skb
->len
< dev
->net
->hard_header_len
) {
3139 dev
->net
->stats
.rx_errors
++;
3140 dev
->net
->stats
.rx_length_errors
++;
3141 netif_dbg(dev
, rx_err
, dev
->net
,
3142 "rx length %d\n", skb
->len
);
3144 usb_mark_last_busy(dev
->udev
);
3147 dev
->net
->stats
.rx_errors
++;
3148 lan78xx_defer_kevent(dev
, EVENT_RX_HALT
);
3150 case -ECONNRESET
: /* async unlink */
3151 case -ESHUTDOWN
: /* hardware gone */
3152 netif_dbg(dev
, ifdown
, dev
->net
,
3153 "rx shutdown, code %d\n", urb_status
);
3161 dev
->net
->stats
.rx_errors
++;
3167 /* data overrun ... flush fifo? */
3169 dev
->net
->stats
.rx_over_errors
++;
3174 dev
->net
->stats
.rx_errors
++;
3175 netif_dbg(dev
, rx_err
, dev
->net
, "rx status %d\n", urb_status
);
3179 state
= defer_bh(dev
, skb
, &dev
->rxq
, state
);
3182 if (netif_running(dev
->net
) &&
3183 !test_bit(EVENT_RX_HALT
, &dev
->flags
) &&
3184 state
!= unlink_start
) {
3185 rx_submit(dev
, urb
, GFP_ATOMIC
);
3190 netif_dbg(dev
, rx_err
, dev
->net
, "no read resubmitted\n");
3193 static void lan78xx_tx_bh(struct lan78xx_net
*dev
)
3196 struct urb
*urb
= NULL
;
3197 struct skb_data
*entry
;
3198 unsigned long flags
;
3199 struct sk_buff_head
*tqp
= &dev
->txq_pend
;
3200 struct sk_buff
*skb
, *skb2
;
3203 int skb_totallen
, pkt_cnt
;
3209 for (skb
= tqp
->next
; pkt_cnt
< tqp
->qlen
; skb
= skb
->next
) {
3210 if (skb_is_gso(skb
)) {
3212 /* handle previous packets first */
3216 length
= skb
->len
- TX_OVERHEAD
;
3217 skb2
= skb_dequeue(tqp
);
3221 if ((skb_totallen
+ skb
->len
) > MAX_SINGLE_PACKET_SIZE
)
3223 skb_totallen
= skb
->len
+ roundup(skb_totallen
, sizeof(u32
));
3227 /* copy to a single skb */
3228 skb
= alloc_skb(skb_totallen
, GFP_ATOMIC
);
3232 skb_put(skb
, skb_totallen
);
3234 for (count
= pos
= 0; count
< pkt_cnt
; count
++) {
3235 skb2
= skb_dequeue(tqp
);
3237 length
+= (skb2
->len
- TX_OVERHEAD
);
3238 memcpy(skb
->data
+ pos
, skb2
->data
, skb2
->len
);
3239 pos
+= roundup(skb2
->len
, sizeof(u32
));
3240 dev_kfree_skb(skb2
);
3245 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
3249 entry
= (struct skb_data
*)skb
->cb
;
3252 entry
->length
= length
;
3253 entry
->num_of_packet
= count
;
3255 spin_lock_irqsave(&dev
->txq
.lock
, flags
);
3256 ret
= usb_autopm_get_interface_async(dev
->intf
);
3258 spin_unlock_irqrestore(&dev
->txq
.lock
, flags
);
3262 usb_fill_bulk_urb(urb
, dev
->udev
, dev
->pipe_out
,
3263 skb
->data
, skb
->len
, tx_complete
, skb
);
3265 if (length
% dev
->maxpacket
== 0) {
3266 /* send USB_ZERO_PACKET */
3267 urb
->transfer_flags
|= URB_ZERO_PACKET
;
3271 /* if this triggers the device is still a sleep */
3272 if (test_bit(EVENT_DEV_ASLEEP
, &dev
->flags
)) {
3273 /* transmission will be done in resume */
3274 usb_anchor_urb(urb
, &dev
->deferred
);
3275 /* no use to process more packets */
3276 netif_stop_queue(dev
->net
);
3278 spin_unlock_irqrestore(&dev
->txq
.lock
, flags
);
3279 netdev_dbg(dev
->net
, "Delaying transmission for resumption\n");
3284 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
3287 netif_trans_update(dev
->net
);
3288 lan78xx_queue_skb(&dev
->txq
, skb
, tx_start
);
3289 if (skb_queue_len(&dev
->txq
) >= dev
->tx_qlen
)
3290 netif_stop_queue(dev
->net
);
3293 netif_stop_queue(dev
->net
);
3294 lan78xx_defer_kevent(dev
, EVENT_TX_HALT
);
3295 usb_autopm_put_interface_async(dev
->intf
);
3298 usb_autopm_put_interface_async(dev
->intf
);
3299 netif_dbg(dev
, tx_err
, dev
->net
,
3300 "tx: submit urb err %d\n", ret
);
3304 spin_unlock_irqrestore(&dev
->txq
.lock
, flags
);
3307 netif_dbg(dev
, tx_err
, dev
->net
, "drop, code %d\n", ret
);
3309 dev
->net
->stats
.tx_dropped
++;
3311 dev_kfree_skb_any(skb
);
3314 netif_dbg(dev
, tx_queued
, dev
->net
,
3315 "> tx, len %d, type 0x%x\n", length
, skb
->protocol
);
3318 static void lan78xx_rx_bh(struct lan78xx_net
*dev
)
3323 if (skb_queue_len(&dev
->rxq
) < dev
->rx_qlen
) {
3324 for (i
= 0; i
< 10; i
++) {
3325 if (skb_queue_len(&dev
->rxq
) >= dev
->rx_qlen
)
3327 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
3329 if (rx_submit(dev
, urb
, GFP_ATOMIC
) == -ENOLINK
)
3333 if (skb_queue_len(&dev
->rxq
) < dev
->rx_qlen
)
3334 tasklet_schedule(&dev
->bh
);
3336 if (skb_queue_len(&dev
->txq
) < dev
->tx_qlen
)
3337 netif_wake_queue(dev
->net
);
3340 static void lan78xx_bh(unsigned long param
)
3342 struct lan78xx_net
*dev
= (struct lan78xx_net
*)param
;
3343 struct sk_buff
*skb
;
3344 struct skb_data
*entry
;
3346 while ((skb
= skb_dequeue(&dev
->done
))) {
3347 entry
= (struct skb_data
*)(skb
->cb
);
3348 switch (entry
->state
) {
3350 entry
->state
= rx_cleanup
;
3351 rx_process(dev
, skb
);
3354 usb_free_urb(entry
->urb
);
3358 usb_free_urb(entry
->urb
);
3362 netdev_dbg(dev
->net
, "skb state %d\n", entry
->state
);
3367 if (netif_device_present(dev
->net
) && netif_running(dev
->net
)) {
3368 /* reset update timer delta */
3369 if (timer_pending(&dev
->stat_monitor
) && (dev
->delta
!= 1)) {
3371 mod_timer(&dev
->stat_monitor
,
3372 jiffies
+ STAT_UPDATE_TIMER
);
3375 if (!skb_queue_empty(&dev
->txq_pend
))
3378 if (!timer_pending(&dev
->delay
) &&
3379 !test_bit(EVENT_RX_HALT
, &dev
->flags
))
3384 static void lan78xx_delayedwork(struct work_struct
*work
)
3387 struct lan78xx_net
*dev
;
3389 dev
= container_of(work
, struct lan78xx_net
, wq
.work
);
3391 if (test_bit(EVENT_TX_HALT
, &dev
->flags
)) {
3392 unlink_urbs(dev
, &dev
->txq
);
3393 status
= usb_autopm_get_interface(dev
->intf
);
3396 status
= usb_clear_halt(dev
->udev
, dev
->pipe_out
);
3397 usb_autopm_put_interface(dev
->intf
);
3400 status
!= -ESHUTDOWN
) {
3401 if (netif_msg_tx_err(dev
))
3403 netdev_err(dev
->net
,
3404 "can't clear tx halt, status %d\n",
3407 clear_bit(EVENT_TX_HALT
, &dev
->flags
);
3408 if (status
!= -ESHUTDOWN
)
3409 netif_wake_queue(dev
->net
);
3412 if (test_bit(EVENT_RX_HALT
, &dev
->flags
)) {
3413 unlink_urbs(dev
, &dev
->rxq
);
3414 status
= usb_autopm_get_interface(dev
->intf
);
3417 status
= usb_clear_halt(dev
->udev
, dev
->pipe_in
);
3418 usb_autopm_put_interface(dev
->intf
);
3421 status
!= -ESHUTDOWN
) {
3422 if (netif_msg_rx_err(dev
))
3424 netdev_err(dev
->net
,
3425 "can't clear rx halt, status %d\n",
3428 clear_bit(EVENT_RX_HALT
, &dev
->flags
);
3429 tasklet_schedule(&dev
->bh
);
3433 if (test_bit(EVENT_LINK_RESET
, &dev
->flags
)) {
3436 clear_bit(EVENT_LINK_RESET
, &dev
->flags
);
3437 status
= usb_autopm_get_interface(dev
->intf
);
3440 if (lan78xx_link_reset(dev
) < 0) {
3441 usb_autopm_put_interface(dev
->intf
);
3443 netdev_info(dev
->net
, "link reset failed (%d)\n",
3446 usb_autopm_put_interface(dev
->intf
);
3450 if (test_bit(EVENT_STAT_UPDATE
, &dev
->flags
)) {
3451 lan78xx_update_stats(dev
);
3453 clear_bit(EVENT_STAT_UPDATE
, &dev
->flags
);
3455 mod_timer(&dev
->stat_monitor
,
3456 jiffies
+ (STAT_UPDATE_TIMER
* dev
->delta
));
3458 dev
->delta
= min((dev
->delta
* 2), 50);
3462 static void intr_complete(struct urb
*urb
)
3464 struct lan78xx_net
*dev
= urb
->context
;
3465 int status
= urb
->status
;
3470 lan78xx_status(dev
, urb
);
3473 /* software-driven interface shutdown */
3474 case -ENOENT
: /* urb killed */
3475 case -ESHUTDOWN
: /* hardware gone */
3476 netif_dbg(dev
, ifdown
, dev
->net
,
3477 "intr shutdown, code %d\n", status
);
3480 /* NOTE: not throttling like RX/TX, since this endpoint
3481 * already polls infrequently
3484 netdev_dbg(dev
->net
, "intr status %d\n", status
);
3488 if (!netif_running(dev
->net
))
3491 memset(urb
->transfer_buffer
, 0, urb
->transfer_buffer_length
);
3492 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
3494 netif_err(dev
, timer
, dev
->net
,
3495 "intr resubmit --> %d\n", status
);
3498 static void lan78xx_disconnect(struct usb_interface
*intf
)
3500 struct lan78xx_net
*dev
;
3501 struct usb_device
*udev
;
3502 struct net_device
*net
;
3504 dev
= usb_get_intfdata(intf
);
3505 usb_set_intfdata(intf
, NULL
);
3509 udev
= interface_to_usbdev(intf
);
3512 unregister_netdev(net
);
3514 cancel_delayed_work_sync(&dev
->wq
);
3516 usb_scuttle_anchored_urbs(&dev
->deferred
);
3518 lan78xx_unbind(dev
, intf
);
3520 usb_kill_urb(dev
->urb_intr
);
3521 usb_free_urb(dev
->urb_intr
);
3527 static void lan78xx_tx_timeout(struct net_device
*net
)
3529 struct lan78xx_net
*dev
= netdev_priv(net
);
3531 unlink_urbs(dev
, &dev
->txq
);
3532 tasklet_schedule(&dev
->bh
);
3535 static const struct net_device_ops lan78xx_netdev_ops
= {
3536 .ndo_open
= lan78xx_open
,
3537 .ndo_stop
= lan78xx_stop
,
3538 .ndo_start_xmit
= lan78xx_start_xmit
,
3539 .ndo_tx_timeout
= lan78xx_tx_timeout
,
3540 .ndo_change_mtu
= lan78xx_change_mtu
,
3541 .ndo_set_mac_address
= lan78xx_set_mac_addr
,
3542 .ndo_validate_addr
= eth_validate_addr
,
3543 .ndo_do_ioctl
= lan78xx_ioctl
,
3544 .ndo_set_rx_mode
= lan78xx_set_multicast
,
3545 .ndo_set_features
= lan78xx_set_features
,
3546 .ndo_vlan_rx_add_vid
= lan78xx_vlan_rx_add_vid
,
3547 .ndo_vlan_rx_kill_vid
= lan78xx_vlan_rx_kill_vid
,
3550 static void lan78xx_stat_monitor(struct timer_list
*t
)
3552 struct lan78xx_net
*dev
= from_timer(dev
, t
, stat_monitor
);
3554 lan78xx_defer_kevent(dev
, EVENT_STAT_UPDATE
);
3557 static int lan78xx_probe(struct usb_interface
*intf
,
3558 const struct usb_device_id
*id
)
3560 struct lan78xx_net
*dev
;
3561 struct net_device
*netdev
;
3562 struct usb_device
*udev
;
3568 udev
= interface_to_usbdev(intf
);
3569 udev
= usb_get_dev(udev
);
3571 netdev
= alloc_etherdev(sizeof(struct lan78xx_net
));
3573 dev_err(&intf
->dev
, "Error: OOM\n");
3578 /* netdev_printk() needs this */
3579 SET_NETDEV_DEV(netdev
, &intf
->dev
);
3581 dev
= netdev_priv(netdev
);
3585 dev
->msg_enable
= netif_msg_init(msg_level
, NETIF_MSG_DRV
3586 | NETIF_MSG_PROBE
| NETIF_MSG_LINK
);
3588 skb_queue_head_init(&dev
->rxq
);
3589 skb_queue_head_init(&dev
->txq
);
3590 skb_queue_head_init(&dev
->done
);
3591 skb_queue_head_init(&dev
->rxq_pause
);
3592 skb_queue_head_init(&dev
->txq_pend
);
3593 mutex_init(&dev
->phy_mutex
);
3595 tasklet_init(&dev
->bh
, lan78xx_bh
, (unsigned long)dev
);
3596 INIT_DELAYED_WORK(&dev
->wq
, lan78xx_delayedwork
);
3597 init_usb_anchor(&dev
->deferred
);
3599 netdev
->netdev_ops
= &lan78xx_netdev_ops
;
3600 netdev
->watchdog_timeo
= TX_TIMEOUT_JIFFIES
;
3601 netdev
->ethtool_ops
= &lan78xx_ethtool_ops
;
3604 timer_setup(&dev
->stat_monitor
, lan78xx_stat_monitor
, 0);
3606 mutex_init(&dev
->stats
.access_lock
);
3608 ret
= lan78xx_bind(dev
, intf
);
3611 strcpy(netdev
->name
, "eth%d");
3613 if (netdev
->mtu
> (dev
->hard_mtu
- netdev
->hard_header_len
))
3614 netdev
->mtu
= dev
->hard_mtu
- netdev
->hard_header_len
;
3616 /* MTU range: 68 - 9000 */
3617 netdev
->max_mtu
= MAX_SINGLE_PACKET_SIZE
;
3619 dev
->ep_blkin
= (intf
->cur_altsetting
)->endpoint
+ 0;
3620 dev
->ep_blkout
= (intf
->cur_altsetting
)->endpoint
+ 1;
3621 dev
->ep_intr
= (intf
->cur_altsetting
)->endpoint
+ 2;
3623 dev
->pipe_in
= usb_rcvbulkpipe(udev
, BULK_IN_PIPE
);
3624 dev
->pipe_out
= usb_sndbulkpipe(udev
, BULK_OUT_PIPE
);
3626 dev
->pipe_intr
= usb_rcvintpipe(dev
->udev
,
3627 dev
->ep_intr
->desc
.bEndpointAddress
&
3628 USB_ENDPOINT_NUMBER_MASK
);
3629 period
= dev
->ep_intr
->desc
.bInterval
;
3631 maxp
= usb_maxpacket(dev
->udev
, dev
->pipe_intr
, 0);
3632 buf
= kmalloc(maxp
, GFP_KERNEL
);
3634 dev
->urb_intr
= usb_alloc_urb(0, GFP_KERNEL
);
3635 if (!dev
->urb_intr
) {
3640 usb_fill_int_urb(dev
->urb_intr
, dev
->udev
,
3641 dev
->pipe_intr
, buf
, maxp
,
3642 intr_complete
, dev
, period
);
3646 dev
->maxpacket
= usb_maxpacket(dev
->udev
, dev
->pipe_out
, 1);
3648 /* driver requires remote-wakeup capability during autosuspend. */
3649 intf
->needs_remote_wakeup
= 1;
3651 ret
= register_netdev(netdev
);
3653 netif_err(dev
, probe
, netdev
, "couldn't register the device\n");
3657 usb_set_intfdata(intf
, dev
);
3659 ret
= device_set_wakeup_enable(&udev
->dev
, true);
3661 /* Default delay of 2sec has more overhead than advantage.
3662 * Set to 10sec as default.
3664 pm_runtime_set_autosuspend_delay(&udev
->dev
,
3665 DEFAULT_AUTOSUSPEND_DELAY
);
3670 lan78xx_unbind(dev
, intf
);
3672 free_netdev(netdev
);
3679 static u16
lan78xx_wakeframe_crc16(const u8
*buf
, int len
)
3681 const u16 crc16poly
= 0x8005;
3687 for (i
= 0; i
< len
; i
++) {
3689 for (bit
= 0; bit
< 8; bit
++) {
3693 if (msb
^ (u16
)(data
& 1)) {
3695 crc
|= (u16
)0x0001U
;
3704 static int lan78xx_set_suspend(struct lan78xx_net
*dev
, u32 wol
)
3712 const u8 ipv4_multicast
[3] = { 0x01, 0x00, 0x5E };
3713 const u8 ipv6_multicast
[3] = { 0x33, 0x33 };
3714 const u8 arp_type
[2] = { 0x08, 0x06 };
3716 ret
= lan78xx_read_reg(dev
, MAC_TX
, &buf
);
3717 buf
&= ~MAC_TX_TXEN_
;
3718 ret
= lan78xx_write_reg(dev
, MAC_TX
, buf
);
3719 ret
= lan78xx_read_reg(dev
, MAC_RX
, &buf
);
3720 buf
&= ~MAC_RX_RXEN_
;
3721 ret
= lan78xx_write_reg(dev
, MAC_RX
, buf
);
3723 ret
= lan78xx_write_reg(dev
, WUCSR
, 0);
3724 ret
= lan78xx_write_reg(dev
, WUCSR2
, 0);
3725 ret
= lan78xx_write_reg(dev
, WK_SRC
, 0xFFF1FF1FUL
);
3730 ret
= lan78xx_read_reg(dev
, PMT_CTL
, &temp_pmt_ctl
);
3731 temp_pmt_ctl
&= ~PMT_CTL_RES_CLR_WKP_EN_
;
3732 temp_pmt_ctl
|= PMT_CTL_RES_CLR_WKP_STS_
;
3734 for (mask_index
= 0; mask_index
< NUM_OF_WUF_CFG
; mask_index
++)
3735 ret
= lan78xx_write_reg(dev
, WUF_CFG(mask_index
), 0);
3738 if (wol
& WAKE_PHY
) {
3739 temp_pmt_ctl
|= PMT_CTL_PHY_WAKE_EN_
;
3741 temp_pmt_ctl
|= PMT_CTL_WOL_EN_
;
3742 temp_pmt_ctl
&= ~PMT_CTL_SUS_MODE_MASK_
;
3743 temp_pmt_ctl
|= PMT_CTL_SUS_MODE_0_
;
3745 if (wol
& WAKE_MAGIC
) {
3746 temp_wucsr
|= WUCSR_MPEN_
;
3748 temp_pmt_ctl
|= PMT_CTL_WOL_EN_
;
3749 temp_pmt_ctl
&= ~PMT_CTL_SUS_MODE_MASK_
;
3750 temp_pmt_ctl
|= PMT_CTL_SUS_MODE_3_
;
3752 if (wol
& WAKE_BCAST
) {
3753 temp_wucsr
|= WUCSR_BCST_EN_
;
3755 temp_pmt_ctl
|= PMT_CTL_WOL_EN_
;
3756 temp_pmt_ctl
&= ~PMT_CTL_SUS_MODE_MASK_
;
3757 temp_pmt_ctl
|= PMT_CTL_SUS_MODE_0_
;
3759 if (wol
& WAKE_MCAST
) {
3760 temp_wucsr
|= WUCSR_WAKE_EN_
;
3762 /* set WUF_CFG & WUF_MASK for IPv4 Multicast */
3763 crc
= lan78xx_wakeframe_crc16(ipv4_multicast
, 3);
3764 ret
= lan78xx_write_reg(dev
, WUF_CFG(mask_index
),
3766 WUF_CFGX_TYPE_MCAST_
|
3767 (0 << WUF_CFGX_OFFSET_SHIFT_
) |
3768 (crc
& WUF_CFGX_CRC16_MASK_
));
3770 ret
= lan78xx_write_reg(dev
, WUF_MASK0(mask_index
), 7);
3771 ret
= lan78xx_write_reg(dev
, WUF_MASK1(mask_index
), 0);
3772 ret
= lan78xx_write_reg(dev
, WUF_MASK2(mask_index
), 0);
3773 ret
= lan78xx_write_reg(dev
, WUF_MASK3(mask_index
), 0);
3776 /* for IPv6 Multicast */
3777 crc
= lan78xx_wakeframe_crc16(ipv6_multicast
, 2);
3778 ret
= lan78xx_write_reg(dev
, WUF_CFG(mask_index
),
3780 WUF_CFGX_TYPE_MCAST_
|
3781 (0 << WUF_CFGX_OFFSET_SHIFT_
) |
3782 (crc
& WUF_CFGX_CRC16_MASK_
));
3784 ret
= lan78xx_write_reg(dev
, WUF_MASK0(mask_index
), 3);
3785 ret
= lan78xx_write_reg(dev
, WUF_MASK1(mask_index
), 0);
3786 ret
= lan78xx_write_reg(dev
, WUF_MASK2(mask_index
), 0);
3787 ret
= lan78xx_write_reg(dev
, WUF_MASK3(mask_index
), 0);
3790 temp_pmt_ctl
|= PMT_CTL_WOL_EN_
;
3791 temp_pmt_ctl
&= ~PMT_CTL_SUS_MODE_MASK_
;
3792 temp_pmt_ctl
|= PMT_CTL_SUS_MODE_0_
;
3794 if (wol
& WAKE_UCAST
) {
3795 temp_wucsr
|= WUCSR_PFDA_EN_
;
3797 temp_pmt_ctl
|= PMT_CTL_WOL_EN_
;
3798 temp_pmt_ctl
&= ~PMT_CTL_SUS_MODE_MASK_
;
3799 temp_pmt_ctl
|= PMT_CTL_SUS_MODE_0_
;
3801 if (wol
& WAKE_ARP
) {
3802 temp_wucsr
|= WUCSR_WAKE_EN_
;
3804 /* set WUF_CFG & WUF_MASK
3805 * for packettype (offset 12,13) = ARP (0x0806)
3807 crc
= lan78xx_wakeframe_crc16(arp_type
, 2);
3808 ret
= lan78xx_write_reg(dev
, WUF_CFG(mask_index
),
3810 WUF_CFGX_TYPE_ALL_
|
3811 (0 << WUF_CFGX_OFFSET_SHIFT_
) |
3812 (crc
& WUF_CFGX_CRC16_MASK_
));
3814 ret
= lan78xx_write_reg(dev
, WUF_MASK0(mask_index
), 0x3000);
3815 ret
= lan78xx_write_reg(dev
, WUF_MASK1(mask_index
), 0);
3816 ret
= lan78xx_write_reg(dev
, WUF_MASK2(mask_index
), 0);
3817 ret
= lan78xx_write_reg(dev
, WUF_MASK3(mask_index
), 0);
3820 temp_pmt_ctl
|= PMT_CTL_WOL_EN_
;
3821 temp_pmt_ctl
&= ~PMT_CTL_SUS_MODE_MASK_
;
3822 temp_pmt_ctl
|= PMT_CTL_SUS_MODE_0_
;
3825 ret
= lan78xx_write_reg(dev
, WUCSR
, temp_wucsr
);
3827 /* when multiple WOL bits are set */
3828 if (hweight_long((unsigned long)wol
) > 1) {
3829 temp_pmt_ctl
|= PMT_CTL_WOL_EN_
;
3830 temp_pmt_ctl
&= ~PMT_CTL_SUS_MODE_MASK_
;
3831 temp_pmt_ctl
|= PMT_CTL_SUS_MODE_0_
;
3833 ret
= lan78xx_write_reg(dev
, PMT_CTL
, temp_pmt_ctl
);
3836 ret
= lan78xx_read_reg(dev
, PMT_CTL
, &buf
);
3837 buf
|= PMT_CTL_WUPS_MASK_
;
3838 ret
= lan78xx_write_reg(dev
, PMT_CTL
, buf
);
3840 ret
= lan78xx_read_reg(dev
, MAC_RX
, &buf
);
3841 buf
|= MAC_RX_RXEN_
;
3842 ret
= lan78xx_write_reg(dev
, MAC_RX
, buf
);
3847 static int lan78xx_suspend(struct usb_interface
*intf
, pm_message_t message
)
3849 struct lan78xx_net
*dev
= usb_get_intfdata(intf
);
3850 struct lan78xx_priv
*pdata
= (struct lan78xx_priv
*)(dev
->data
[0]);
3855 event
= message
.event
;
3857 if (!dev
->suspend_count
++) {
3858 spin_lock_irq(&dev
->txq
.lock
);
3859 /* don't autosuspend while transmitting */
3860 if ((skb_queue_len(&dev
->txq
) ||
3861 skb_queue_len(&dev
->txq_pend
)) &&
3862 PMSG_IS_AUTO(message
)) {
3863 spin_unlock_irq(&dev
->txq
.lock
);
3867 set_bit(EVENT_DEV_ASLEEP
, &dev
->flags
);
3868 spin_unlock_irq(&dev
->txq
.lock
);
3872 ret
= lan78xx_read_reg(dev
, MAC_TX
, &buf
);
3873 buf
&= ~MAC_TX_TXEN_
;
3874 ret
= lan78xx_write_reg(dev
, MAC_TX
, buf
);
3875 ret
= lan78xx_read_reg(dev
, MAC_RX
, &buf
);
3876 buf
&= ~MAC_RX_RXEN_
;
3877 ret
= lan78xx_write_reg(dev
, MAC_RX
, buf
);
3879 /* empty out the rx and queues */
3880 netif_device_detach(dev
->net
);
3881 lan78xx_terminate_urbs(dev
);
3882 usb_kill_urb(dev
->urb_intr
);
3885 netif_device_attach(dev
->net
);
3888 if (test_bit(EVENT_DEV_ASLEEP
, &dev
->flags
)) {
3889 del_timer(&dev
->stat_monitor
);
3891 if (PMSG_IS_AUTO(message
)) {
3892 /* auto suspend (selective suspend) */
3893 ret
= lan78xx_read_reg(dev
, MAC_TX
, &buf
);
3894 buf
&= ~MAC_TX_TXEN_
;
3895 ret
= lan78xx_write_reg(dev
, MAC_TX
, buf
);
3896 ret
= lan78xx_read_reg(dev
, MAC_RX
, &buf
);
3897 buf
&= ~MAC_RX_RXEN_
;
3898 ret
= lan78xx_write_reg(dev
, MAC_RX
, buf
);
3900 ret
= lan78xx_write_reg(dev
, WUCSR
, 0);
3901 ret
= lan78xx_write_reg(dev
, WUCSR2
, 0);
3902 ret
= lan78xx_write_reg(dev
, WK_SRC
, 0xFFF1FF1FUL
);
3904 /* set goodframe wakeup */
3905 ret
= lan78xx_read_reg(dev
, WUCSR
, &buf
);
3907 buf
|= WUCSR_RFE_WAKE_EN_
;
3908 buf
|= WUCSR_STORE_WAKE_
;
3910 ret
= lan78xx_write_reg(dev
, WUCSR
, buf
);
3912 ret
= lan78xx_read_reg(dev
, PMT_CTL
, &buf
);
3914 buf
&= ~PMT_CTL_RES_CLR_WKP_EN_
;
3915 buf
|= PMT_CTL_RES_CLR_WKP_STS_
;
3917 buf
|= PMT_CTL_PHY_WAKE_EN_
;
3918 buf
|= PMT_CTL_WOL_EN_
;
3919 buf
&= ~PMT_CTL_SUS_MODE_MASK_
;
3920 buf
|= PMT_CTL_SUS_MODE_3_
;
3922 ret
= lan78xx_write_reg(dev
, PMT_CTL
, buf
);
3924 ret
= lan78xx_read_reg(dev
, PMT_CTL
, &buf
);
3926 buf
|= PMT_CTL_WUPS_MASK_
;
3928 ret
= lan78xx_write_reg(dev
, PMT_CTL
, buf
);
3930 ret
= lan78xx_read_reg(dev
, MAC_RX
, &buf
);
3931 buf
|= MAC_RX_RXEN_
;
3932 ret
= lan78xx_write_reg(dev
, MAC_RX
, buf
);
3934 lan78xx_set_suspend(dev
, pdata
->wol
);
3943 static int lan78xx_resume(struct usb_interface
*intf
)
3945 struct lan78xx_net
*dev
= usb_get_intfdata(intf
);
3946 struct sk_buff
*skb
;
3951 if (!timer_pending(&dev
->stat_monitor
)) {
3953 mod_timer(&dev
->stat_monitor
,
3954 jiffies
+ STAT_UPDATE_TIMER
);
3957 if (!--dev
->suspend_count
) {
3958 /* resume interrupt URBs */
3959 if (dev
->urb_intr
&& test_bit(EVENT_DEV_OPEN
, &dev
->flags
))
3960 usb_submit_urb(dev
->urb_intr
, GFP_NOIO
);
3962 spin_lock_irq(&dev
->txq
.lock
);
3963 while ((res
= usb_get_from_anchor(&dev
->deferred
))) {
3964 skb
= (struct sk_buff
*)res
->context
;
3965 ret
= usb_submit_urb(res
, GFP_ATOMIC
);
3967 dev_kfree_skb_any(skb
);
3969 usb_autopm_put_interface_async(dev
->intf
);
3971 netif_trans_update(dev
->net
);
3972 lan78xx_queue_skb(&dev
->txq
, skb
, tx_start
);
3976 clear_bit(EVENT_DEV_ASLEEP
, &dev
->flags
);
3977 spin_unlock_irq(&dev
->txq
.lock
);
3979 if (test_bit(EVENT_DEV_OPEN
, &dev
->flags
)) {
3980 if (!(skb_queue_len(&dev
->txq
) >= dev
->tx_qlen
))
3981 netif_start_queue(dev
->net
);
3982 tasklet_schedule(&dev
->bh
);
3986 ret
= lan78xx_write_reg(dev
, WUCSR2
, 0);
3987 ret
= lan78xx_write_reg(dev
, WUCSR
, 0);
3988 ret
= lan78xx_write_reg(dev
, WK_SRC
, 0xFFF1FF1FUL
);
3990 ret
= lan78xx_write_reg(dev
, WUCSR2
, WUCSR2_NS_RCD_
|
3992 WUCSR2_IPV6_TCPSYN_RCD_
|
3993 WUCSR2_IPV4_TCPSYN_RCD_
);
3995 ret
= lan78xx_write_reg(dev
, WUCSR
, WUCSR_EEE_TX_WAKE_
|
3996 WUCSR_EEE_RX_WAKE_
|
3998 WUCSR_RFE_WAKE_FR_
|
4003 ret
= lan78xx_read_reg(dev
, MAC_TX
, &buf
);
4004 buf
|= MAC_TX_TXEN_
;
4005 ret
= lan78xx_write_reg(dev
, MAC_TX
, buf
);
4010 static int lan78xx_reset_resume(struct usb_interface
*intf
)
4012 struct lan78xx_net
*dev
= usb_get_intfdata(intf
);
4016 lan78xx_phy_init(dev
);
4018 return lan78xx_resume(intf
);
4021 static const struct usb_device_id products
[] = {
4023 /* LAN7800 USB Gigabit Ethernet Device */
4024 USB_DEVICE(LAN78XX_USB_VENDOR_ID
, LAN7800_USB_PRODUCT_ID
),
4027 /* LAN7850 USB Gigabit Ethernet Device */
4028 USB_DEVICE(LAN78XX_USB_VENDOR_ID
, LAN7850_USB_PRODUCT_ID
),
4031 /* LAN7801 USB Gigabit Ethernet Device */
4032 USB_DEVICE(LAN78XX_USB_VENDOR_ID
, LAN7801_USB_PRODUCT_ID
),
4036 MODULE_DEVICE_TABLE(usb
, products
);
4038 static struct usb_driver lan78xx_driver
= {
4039 .name
= DRIVER_NAME
,
4040 .id_table
= products
,
4041 .probe
= lan78xx_probe
,
4042 .disconnect
= lan78xx_disconnect
,
4043 .suspend
= lan78xx_suspend
,
4044 .resume
= lan78xx_resume
,
4045 .reset_resume
= lan78xx_reset_resume
,
4046 .supports_autosuspend
= 1,
4047 .disable_hub_initiated_lpm
= 1,
4050 module_usb_driver(lan78xx_driver
);
4052 MODULE_AUTHOR(DRIVER_AUTHOR
);
4053 MODULE_DESCRIPTION(DRIVER_DESC
);
4054 MODULE_LICENSE("GPL");