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