1 // SPDX-License-Identifier: GPL-2.0-only 2 /**************************************************************************** 3 * Driver for Solarflare network controllers and boards 4 * Copyright 2005-2006 Fen Systems Ltd. 5 * Copyright 2005-2013 Solarflare Communications Inc. 6 */ 7 8 #include <linux/filter.h> 9 #include <linux/module.h> 10 #include <linux/pci.h> 11 #include <linux/netdevice.h> 12 #include <linux/etherdevice.h> 13 #include <linux/delay.h> 14 #include <linux/notifier.h> 15 #include <linux/ip.h> 16 #include <linux/tcp.h> 17 #include <linux/in.h> 18 #include <linux/ethtool.h> 19 #include <linux/topology.h> 20 #include <linux/gfp.h> 21 #include <linux/interrupt.h> 22 #include "net_driver.h" 23 #include <net/gre.h> 24 #include <net/udp_tunnel.h> 25 #include "efx.h" 26 #include "efx_common.h" 27 #include "efx_channels.h" 28 #include "ef100.h" 29 #include "rx_common.h" 30 #include "tx_common.h" 31 #include "nic.h" 32 #include "io.h" 33 #include "selftest.h" 34 #include "sriov.h" 35 36 #include "mcdi_port_common.h" 37 #include "mcdi_pcol.h" 38 #include "workarounds.h" 39 40 /************************************************************************** 41 * 42 * Configurable values 43 * 44 *************************************************************************/ 45 46 module_param_named(interrupt_mode, efx_interrupt_mode, uint, 0444); 47 MODULE_PARM_DESC(interrupt_mode, 48 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)"); 49 50 module_param(rss_cpus, uint, 0444); 51 MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling"); 52 53 /* 54 * Use separate channels for TX and RX events 55 * 56 * Set this to 1 to use separate channels for TX and RX. It allows us 57 * to control interrupt affinity separately for TX and RX. 58 * 59 * This is only used in MSI-X interrupt mode 60 */ 61 bool efx_separate_tx_channels; 62 module_param(efx_separate_tx_channels, bool, 0444); 63 MODULE_PARM_DESC(efx_separate_tx_channels, 64 "Use separate channels for TX and RX"); 65 66 /* Initial interrupt moderation settings. They can be modified after 67 * module load with ethtool. 68 * 69 * The default for RX should strike a balance between increasing the 70 * round-trip latency and reducing overhead. 71 */ 72 static unsigned int rx_irq_mod_usec = 60; 73 74 /* Initial interrupt moderation settings. They can be modified after 75 * module load with ethtool. 76 * 77 * This default is chosen to ensure that a 10G link does not go idle 78 * while a TX queue is stopped after it has become full. A queue is 79 * restarted when it drops below half full. The time this takes (assuming 80 * worst case 3 descriptors per packet and 1024 descriptors) is 81 * 512 / 3 * 1.2 = 205 usec. 82 */ 83 static unsigned int tx_irq_mod_usec = 150; 84 85 static bool phy_flash_cfg; 86 module_param(phy_flash_cfg, bool, 0644); 87 MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially"); 88 89 static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE | 90 NETIF_MSG_LINK | NETIF_MSG_IFDOWN | 91 NETIF_MSG_IFUP | NETIF_MSG_RX_ERR | 92 NETIF_MSG_TX_ERR | NETIF_MSG_HW); 93 module_param(debug, uint, 0); 94 MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value"); 95 96 /************************************************************************** 97 * 98 * Utility functions and prototypes 99 * 100 *************************************************************************/ 101 102 static void efx_remove_port(struct efx_nic *efx); 103 static int efx_xdp_setup_prog(struct efx_nic *efx, struct bpf_prog *prog); 104 static int efx_xdp(struct net_device *dev, struct netdev_bpf *xdp); 105 static int efx_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **xdpfs, 106 u32 flags); 107 108 /************************************************************************** 109 * 110 * Port handling 111 * 112 **************************************************************************/ 113 114 static void efx_fini_port(struct efx_nic *efx); 115 116 static int efx_probe_port(struct efx_nic *efx) 117 { 118 int rc; 119 120 netif_dbg(efx, probe, efx->net_dev, "create port\n"); 121 122 if (phy_flash_cfg) 123 efx->phy_mode = PHY_MODE_SPECIAL; 124 125 /* Connect up MAC/PHY operations table */ 126 rc = efx->type->probe_port(efx); 127 if (rc) 128 return rc; 129 130 /* Initialise MAC address to permanent address */ 131 eth_hw_addr_set(efx->net_dev, efx->net_dev->perm_addr); 132 133 return 0; 134 } 135 136 static int efx_init_port(struct efx_nic *efx) 137 { 138 int rc; 139 140 netif_dbg(efx, drv, efx->net_dev, "init port\n"); 141 142 mutex_lock(&efx->mac_lock); 143 144 efx->port_initialized = true; 145 146 /* Ensure the PHY advertises the correct flow control settings */ 147 rc = efx_mcdi_port_reconfigure(efx); 148 if (rc && rc != -EPERM) 149 goto fail; 150 151 mutex_unlock(&efx->mac_lock); 152 return 0; 153 154 fail: 155 mutex_unlock(&efx->mac_lock); 156 return rc; 157 } 158 159 static void efx_fini_port(struct efx_nic *efx) 160 { 161 netif_dbg(efx, drv, efx->net_dev, "shut down port\n"); 162 163 if (!efx->port_initialized) 164 return; 165 166 efx->port_initialized = false; 167 168 efx->link_state.up = false; 169 efx_link_status_changed(efx); 170 } 171 172 static void efx_remove_port(struct efx_nic *efx) 173 { 174 netif_dbg(efx, drv, efx->net_dev, "destroying port\n"); 175 176 efx->type->remove_port(efx); 177 } 178 179 /************************************************************************** 180 * 181 * NIC handling 182 * 183 **************************************************************************/ 184 185 static LIST_HEAD(efx_primary_list); 186 static LIST_HEAD(efx_unassociated_list); 187 188 static bool efx_same_controller(struct efx_nic *left, struct efx_nic *right) 189 { 190 return left->type == right->type && 191 left->vpd_sn && right->vpd_sn && 192 !strcmp(left->vpd_sn, right->vpd_sn); 193 } 194 195 static void efx_associate(struct efx_nic *efx) 196 { 197 struct efx_nic *other, *next; 198 199 if (efx->primary == efx) { 200 /* Adding primary function; look for secondaries */ 201 202 netif_dbg(efx, probe, efx->net_dev, "adding to primary list\n"); 203 list_add_tail(&efx->node, &efx_primary_list); 204 205 list_for_each_entry_safe(other, next, &efx_unassociated_list, 206 node) { 207 if (efx_same_controller(efx, other)) { 208 list_del(&other->node); 209 netif_dbg(other, probe, other->net_dev, 210 "moving to secondary list of %s %s\n", 211 pci_name(efx->pci_dev), 212 efx->net_dev->name); 213 list_add_tail(&other->node, 214 &efx->secondary_list); 215 other->primary = efx; 216 } 217 } 218 } else { 219 /* Adding secondary function; look for primary */ 220 221 list_for_each_entry(other, &efx_primary_list, node) { 222 if (efx_same_controller(efx, other)) { 223 netif_dbg(efx, probe, efx->net_dev, 224 "adding to secondary list of %s %s\n", 225 pci_name(other->pci_dev), 226 other->net_dev->name); 227 list_add_tail(&efx->node, 228 &other->secondary_list); 229 efx->primary = other; 230 return; 231 } 232 } 233 234 netif_dbg(efx, probe, efx->net_dev, 235 "adding to unassociated list\n"); 236 list_add_tail(&efx->node, &efx_unassociated_list); 237 } 238 } 239 240 static void efx_dissociate(struct efx_nic *efx) 241 { 242 struct efx_nic *other, *next; 243 244 list_del(&efx->node); 245 efx->primary = NULL; 246 247 list_for_each_entry_safe(other, next, &efx->secondary_list, node) { 248 list_del(&other->node); 249 netif_dbg(other, probe, other->net_dev, 250 "moving to unassociated list\n"); 251 list_add_tail(&other->node, &efx_unassociated_list); 252 other->primary = NULL; 253 } 254 } 255 256 static int efx_probe_nic(struct efx_nic *efx) 257 { 258 int rc; 259 260 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n"); 261 262 /* Carry out hardware-type specific initialisation */ 263 rc = efx->type->probe(efx); 264 if (rc) 265 return rc; 266 267 do { 268 if (!efx->max_channels || !efx->max_tx_channels) { 269 netif_err(efx, drv, efx->net_dev, 270 "Insufficient resources to allocate" 271 " any channels\n"); 272 rc = -ENOSPC; 273 goto fail1; 274 } 275 276 /* Determine the number of channels and queues by trying 277 * to hook in MSI-X interrupts. 278 */ 279 rc = efx_probe_interrupts(efx); 280 if (rc) 281 goto fail1; 282 283 rc = efx_set_channels(efx); 284 if (rc) 285 goto fail1; 286 287 /* dimension_resources can fail with EAGAIN */ 288 rc = efx->type->dimension_resources(efx); 289 if (rc != 0 && rc != -EAGAIN) 290 goto fail2; 291 292 if (rc == -EAGAIN) 293 /* try again with new max_channels */ 294 efx_remove_interrupts(efx); 295 296 } while (rc == -EAGAIN); 297 298 if (efx->n_channels > 1) 299 netdev_rss_key_fill(efx->rss_context.rx_hash_key, 300 sizeof(efx->rss_context.rx_hash_key)); 301 efx_set_default_rx_indir_table(efx, &efx->rss_context); 302 303 /* Initialise the interrupt moderation settings */ 304 efx->irq_mod_step_us = DIV_ROUND_UP(efx->timer_quantum_ns, 1000); 305 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true, 306 true); 307 308 return 0; 309 310 fail2: 311 efx_remove_interrupts(efx); 312 fail1: 313 efx->type->remove(efx); 314 return rc; 315 } 316 317 static void efx_remove_nic(struct efx_nic *efx) 318 { 319 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n"); 320 321 efx_remove_interrupts(efx); 322 efx->type->remove(efx); 323 } 324 325 /************************************************************************** 326 * 327 * NIC startup/shutdown 328 * 329 *************************************************************************/ 330 331 static int efx_probe_all(struct efx_nic *efx) 332 { 333 int rc; 334 335 rc = efx_probe_nic(efx); 336 if (rc) { 337 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n"); 338 goto fail1; 339 } 340 341 rc = efx_probe_port(efx); 342 if (rc) { 343 netif_err(efx, probe, efx->net_dev, "failed to create port\n"); 344 goto fail2; 345 } 346 347 BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT); 348 if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) { 349 rc = -EINVAL; 350 goto fail3; 351 } 352 353 #ifdef CONFIG_SFC_SRIOV 354 rc = efx->type->vswitching_probe(efx); 355 if (rc) /* not fatal; the PF will still work fine */ 356 netif_warn(efx, probe, efx->net_dev, 357 "failed to setup vswitching rc=%d;" 358 " VFs may not function\n", rc); 359 #endif 360 361 rc = efx_probe_filters(efx); 362 if (rc) { 363 netif_err(efx, probe, efx->net_dev, 364 "failed to create filter tables\n"); 365 goto fail4; 366 } 367 368 rc = efx_probe_channels(efx); 369 if (rc) 370 goto fail5; 371 372 efx->state = STATE_NET_DOWN; 373 374 return 0; 375 376 fail5: 377 efx_remove_filters(efx); 378 fail4: 379 #ifdef CONFIG_SFC_SRIOV 380 efx->type->vswitching_remove(efx); 381 #endif 382 fail3: 383 efx_remove_port(efx); 384 fail2: 385 efx_remove_nic(efx); 386 fail1: 387 return rc; 388 } 389 390 static void efx_remove_all(struct efx_nic *efx) 391 { 392 rtnl_lock(); 393 efx_xdp_setup_prog(efx, NULL); 394 rtnl_unlock(); 395 396 efx_remove_channels(efx); 397 efx_remove_filters(efx); 398 #ifdef CONFIG_SFC_SRIOV 399 efx->type->vswitching_remove(efx); 400 #endif 401 efx_remove_port(efx); 402 efx_remove_nic(efx); 403 } 404 405 /************************************************************************** 406 * 407 * Interrupt moderation 408 * 409 **************************************************************************/ 410 unsigned int efx_usecs_to_ticks(struct efx_nic *efx, unsigned int usecs) 411 { 412 if (usecs == 0) 413 return 0; 414 if (usecs * 1000 < efx->timer_quantum_ns) 415 return 1; /* never round down to 0 */ 416 return usecs * 1000 / efx->timer_quantum_ns; 417 } 418 419 unsigned int efx_ticks_to_usecs(struct efx_nic *efx, unsigned int ticks) 420 { 421 /* We must round up when converting ticks to microseconds 422 * because we round down when converting the other way. 423 */ 424 return DIV_ROUND_UP(ticks * efx->timer_quantum_ns, 1000); 425 } 426 427 /* Set interrupt moderation parameters */ 428 int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs, 429 unsigned int rx_usecs, bool rx_adaptive, 430 bool rx_may_override_tx) 431 { 432 struct efx_channel *channel; 433 unsigned int timer_max_us; 434 435 EFX_ASSERT_RESET_SERIALISED(efx); 436 437 timer_max_us = efx->timer_max_ns / 1000; 438 439 if (tx_usecs > timer_max_us || rx_usecs > timer_max_us) 440 return -EINVAL; 441 442 if (tx_usecs != rx_usecs && efx->tx_channel_offset == 0 && 443 !rx_may_override_tx) { 444 netif_err(efx, drv, efx->net_dev, "Channels are shared. " 445 "RX and TX IRQ moderation must be equal\n"); 446 return -EINVAL; 447 } 448 449 efx->irq_rx_adaptive = rx_adaptive; 450 efx->irq_rx_moderation_us = rx_usecs; 451 efx_for_each_channel(channel, efx) { 452 if (efx_channel_has_rx_queue(channel)) 453 channel->irq_moderation_us = rx_usecs; 454 else if (efx_channel_has_tx_queues(channel)) 455 channel->irq_moderation_us = tx_usecs; 456 else if (efx_channel_is_xdp_tx(channel)) 457 channel->irq_moderation_us = tx_usecs; 458 } 459 460 return 0; 461 } 462 463 void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs, 464 unsigned int *rx_usecs, bool *rx_adaptive) 465 { 466 *rx_adaptive = efx->irq_rx_adaptive; 467 *rx_usecs = efx->irq_rx_moderation_us; 468 469 /* If channels are shared between RX and TX, so is IRQ 470 * moderation. Otherwise, IRQ moderation is the same for all 471 * TX channels and is not adaptive. 472 */ 473 if (efx->tx_channel_offset == 0) { 474 *tx_usecs = *rx_usecs; 475 } else { 476 struct efx_channel *tx_channel; 477 478 tx_channel = efx->channel[efx->tx_channel_offset]; 479 *tx_usecs = tx_channel->irq_moderation_us; 480 } 481 } 482 483 /************************************************************************** 484 * 485 * ioctls 486 * 487 *************************************************************************/ 488 489 /* Net device ioctl 490 * Context: process, rtnl_lock() held. 491 */ 492 static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd) 493 { 494 struct efx_nic *efx = efx_netdev_priv(net_dev); 495 struct mii_ioctl_data *data = if_mii(ifr); 496 497 if (cmd == SIOCSHWTSTAMP) 498 return efx_ptp_set_ts_config(efx, ifr); 499 if (cmd == SIOCGHWTSTAMP) 500 return efx_ptp_get_ts_config(efx, ifr); 501 502 /* Convert phy_id from older PRTAD/DEVAD format */ 503 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) && 504 (data->phy_id & 0xfc00) == 0x0400) 505 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400; 506 507 return mdio_mii_ioctl(&efx->mdio, data, cmd); 508 } 509 510 /************************************************************************** 511 * 512 * Kernel net device interface 513 * 514 *************************************************************************/ 515 516 /* Context: process, rtnl_lock() held. */ 517 int efx_net_open(struct net_device *net_dev) 518 { 519 struct efx_nic *efx = efx_netdev_priv(net_dev); 520 int rc; 521 522 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n", 523 raw_smp_processor_id()); 524 525 rc = efx_check_disabled(efx); 526 if (rc) 527 return rc; 528 if (efx->phy_mode & PHY_MODE_SPECIAL) 529 return -EBUSY; 530 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL)) 531 return -EIO; 532 533 /* Notify the kernel of the link state polled during driver load, 534 * before the monitor starts running */ 535 efx_link_status_changed(efx); 536 537 efx_start_all(efx); 538 if (efx->state == STATE_DISABLED || efx->reset_pending) 539 netif_device_detach(efx->net_dev); 540 else 541 efx->state = STATE_NET_UP; 542 543 efx_selftest_async_start(efx); 544 return 0; 545 } 546 547 /* Context: process, rtnl_lock() held. 548 * Note that the kernel will ignore our return code; this method 549 * should really be a void. 550 */ 551 int efx_net_stop(struct net_device *net_dev) 552 { 553 struct efx_nic *efx = efx_netdev_priv(net_dev); 554 555 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n", 556 raw_smp_processor_id()); 557 558 /* Stop the device and flush all the channels */ 559 efx_stop_all(efx); 560 561 return 0; 562 } 563 564 static int efx_vlan_rx_add_vid(struct net_device *net_dev, __be16 proto, u16 vid) 565 { 566 struct efx_nic *efx = efx_netdev_priv(net_dev); 567 568 if (efx->type->vlan_rx_add_vid) 569 return efx->type->vlan_rx_add_vid(efx, proto, vid); 570 else 571 return -EOPNOTSUPP; 572 } 573 574 static int efx_vlan_rx_kill_vid(struct net_device *net_dev, __be16 proto, u16 vid) 575 { 576 struct efx_nic *efx = efx_netdev_priv(net_dev); 577 578 if (efx->type->vlan_rx_kill_vid) 579 return efx->type->vlan_rx_kill_vid(efx, proto, vid); 580 else 581 return -EOPNOTSUPP; 582 } 583 584 static const struct net_device_ops efx_netdev_ops = { 585 .ndo_open = efx_net_open, 586 .ndo_stop = efx_net_stop, 587 .ndo_get_stats64 = efx_net_stats, 588 .ndo_tx_timeout = efx_watchdog, 589 .ndo_start_xmit = efx_hard_start_xmit, 590 .ndo_validate_addr = eth_validate_addr, 591 .ndo_eth_ioctl = efx_ioctl, 592 .ndo_change_mtu = efx_change_mtu, 593 .ndo_set_mac_address = efx_set_mac_address, 594 .ndo_set_rx_mode = efx_set_rx_mode, 595 .ndo_set_features = efx_set_features, 596 .ndo_features_check = efx_features_check, 597 .ndo_vlan_rx_add_vid = efx_vlan_rx_add_vid, 598 .ndo_vlan_rx_kill_vid = efx_vlan_rx_kill_vid, 599 #ifdef CONFIG_SFC_SRIOV 600 .ndo_set_vf_mac = efx_sriov_set_vf_mac, 601 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan, 602 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk, 603 .ndo_get_vf_config = efx_sriov_get_vf_config, 604 .ndo_set_vf_link_state = efx_sriov_set_vf_link_state, 605 #endif 606 .ndo_get_phys_port_id = efx_get_phys_port_id, 607 .ndo_get_phys_port_name = efx_get_phys_port_name, 608 .ndo_setup_tc = efx_setup_tc, 609 #ifdef CONFIG_RFS_ACCEL 610 .ndo_rx_flow_steer = efx_filter_rfs, 611 #endif 612 .ndo_xdp_xmit = efx_xdp_xmit, 613 .ndo_bpf = efx_xdp 614 }; 615 616 static int efx_xdp_setup_prog(struct efx_nic *efx, struct bpf_prog *prog) 617 { 618 struct bpf_prog *old_prog; 619 620 if (efx->xdp_rxq_info_failed) { 621 netif_err(efx, drv, efx->net_dev, 622 "Unable to bind XDP program due to previous failure of rxq_info\n"); 623 return -EINVAL; 624 } 625 626 if (prog && efx->net_dev->mtu > efx_xdp_max_mtu(efx)) { 627 netif_err(efx, drv, efx->net_dev, 628 "Unable to configure XDP with MTU of %d (max: %d)\n", 629 efx->net_dev->mtu, efx_xdp_max_mtu(efx)); 630 return -EINVAL; 631 } 632 633 old_prog = rtnl_dereference(efx->xdp_prog); 634 rcu_assign_pointer(efx->xdp_prog, prog); 635 /* Release the reference that was originally passed by the caller. */ 636 if (old_prog) 637 bpf_prog_put(old_prog); 638 639 return 0; 640 } 641 642 /* Context: process, rtnl_lock() held. */ 643 static int efx_xdp(struct net_device *dev, struct netdev_bpf *xdp) 644 { 645 struct efx_nic *efx = efx_netdev_priv(dev); 646 647 switch (xdp->command) { 648 case XDP_SETUP_PROG: 649 return efx_xdp_setup_prog(efx, xdp->prog); 650 default: 651 return -EINVAL; 652 } 653 } 654 655 static int efx_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **xdpfs, 656 u32 flags) 657 { 658 struct efx_nic *efx = efx_netdev_priv(dev); 659 660 if (!netif_running(dev)) 661 return -EINVAL; 662 663 return efx_xdp_tx_buffers(efx, n, xdpfs, flags & XDP_XMIT_FLUSH); 664 } 665 666 static void efx_update_name(struct efx_nic *efx) 667 { 668 strcpy(efx->name, efx->net_dev->name); 669 efx_mtd_rename(efx); 670 efx_set_channel_names(efx); 671 } 672 673 static int efx_netdev_event(struct notifier_block *this, 674 unsigned long event, void *ptr) 675 { 676 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr); 677 678 if ((net_dev->netdev_ops == &efx_netdev_ops) && 679 event == NETDEV_CHANGENAME) 680 efx_update_name(efx_netdev_priv(net_dev)); 681 682 return NOTIFY_DONE; 683 } 684 685 static struct notifier_block efx_netdev_notifier = { 686 .notifier_call = efx_netdev_event, 687 }; 688 689 static ssize_t phy_type_show(struct device *dev, 690 struct device_attribute *attr, char *buf) 691 { 692 struct efx_nic *efx = dev_get_drvdata(dev); 693 return sprintf(buf, "%d\n", efx->phy_type); 694 } 695 static DEVICE_ATTR_RO(phy_type); 696 697 static int efx_register_netdev(struct efx_nic *efx) 698 { 699 struct net_device *net_dev = efx->net_dev; 700 struct efx_channel *channel; 701 int rc; 702 703 net_dev->watchdog_timeo = 5 * HZ; 704 net_dev->irq = efx->pci_dev->irq; 705 net_dev->netdev_ops = &efx_netdev_ops; 706 if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0) 707 net_dev->priv_flags |= IFF_UNICAST_FLT; 708 net_dev->ethtool_ops = &efx_ethtool_ops; 709 netif_set_tso_max_segs(net_dev, EFX_TSO_MAX_SEGS); 710 net_dev->min_mtu = EFX_MIN_MTU; 711 net_dev->max_mtu = EFX_MAX_MTU; 712 713 rtnl_lock(); 714 715 /* Enable resets to be scheduled and check whether any were 716 * already requested. If so, the NIC is probably hosed so we 717 * abort. 718 */ 719 if (efx->reset_pending) { 720 pci_err(efx->pci_dev, "aborting probe due to scheduled reset\n"); 721 rc = -EIO; 722 goto fail_locked; 723 } 724 725 rc = dev_alloc_name(net_dev, net_dev->name); 726 if (rc < 0) 727 goto fail_locked; 728 efx_update_name(efx); 729 730 /* Always start with carrier off; PHY events will detect the link */ 731 netif_carrier_off(net_dev); 732 733 rc = register_netdevice(net_dev); 734 if (rc) 735 goto fail_locked; 736 737 efx_for_each_channel(channel, efx) { 738 struct efx_tx_queue *tx_queue; 739 efx_for_each_channel_tx_queue(tx_queue, channel) 740 efx_init_tx_queue_core_txq(tx_queue); 741 } 742 743 efx_associate(efx); 744 745 efx->state = STATE_NET_DOWN; 746 747 rtnl_unlock(); 748 749 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type); 750 if (rc) { 751 netif_err(efx, drv, efx->net_dev, 752 "failed to init net dev attributes\n"); 753 goto fail_registered; 754 } 755 756 efx_init_mcdi_logging(efx); 757 758 return 0; 759 760 fail_registered: 761 rtnl_lock(); 762 efx_dissociate(efx); 763 unregister_netdevice(net_dev); 764 fail_locked: 765 efx->state = STATE_UNINIT; 766 rtnl_unlock(); 767 netif_err(efx, drv, efx->net_dev, "could not register net dev\n"); 768 return rc; 769 } 770 771 static void efx_unregister_netdev(struct efx_nic *efx) 772 { 773 if (!efx->net_dev) 774 return; 775 776 if (WARN_ON(efx_netdev_priv(efx->net_dev) != efx)) 777 return; 778 779 if (efx_dev_registered(efx)) { 780 strscpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name)); 781 efx_fini_mcdi_logging(efx); 782 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type); 783 unregister_netdev(efx->net_dev); 784 } 785 } 786 787 /************************************************************************** 788 * 789 * List of NICs we support 790 * 791 **************************************************************************/ 792 793 /* PCI device ID table */ 794 static const struct pci_device_id efx_pci_table[] = { 795 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0903), /* SFC9120 PF */ 796 .driver_data = (unsigned long) &efx_hunt_a0_nic_type}, 797 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1903), /* SFC9120 VF */ 798 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type}, 799 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0923), /* SFC9140 PF */ 800 .driver_data = (unsigned long) &efx_hunt_a0_nic_type}, 801 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1923), /* SFC9140 VF */ 802 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type}, 803 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0a03), /* SFC9220 PF */ 804 .driver_data = (unsigned long) &efx_hunt_a0_nic_type}, 805 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1a03), /* SFC9220 VF */ 806 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type}, 807 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0b03), /* SFC9250 PF */ 808 .driver_data = (unsigned long) &efx_hunt_a0_nic_type}, 809 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1b03), /* SFC9250 VF */ 810 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type}, 811 {0} /* end of list */ 812 }; 813 814 /************************************************************************** 815 * 816 * Data housekeeping 817 * 818 **************************************************************************/ 819 820 void efx_update_sw_stats(struct efx_nic *efx, u64 *stats) 821 { 822 u64 n_rx_nodesc_trunc = 0; 823 struct efx_channel *channel; 824 825 efx_for_each_channel(channel, efx) 826 n_rx_nodesc_trunc += channel->n_rx_nodesc_trunc; 827 stats[GENERIC_STAT_rx_nodesc_trunc] = n_rx_nodesc_trunc; 828 stats[GENERIC_STAT_rx_noskb_drops] = atomic_read(&efx->n_rx_noskb_drops); 829 } 830 831 /************************************************************************** 832 * 833 * PCI interface 834 * 835 **************************************************************************/ 836 837 /* Main body of final NIC shutdown code 838 * This is called only at module unload (or hotplug removal). 839 */ 840 static void efx_pci_remove_main(struct efx_nic *efx) 841 { 842 /* Flush reset_work. It can no longer be scheduled since we 843 * are not READY. 844 */ 845 WARN_ON(efx_net_active(efx->state)); 846 efx_flush_reset_workqueue(efx); 847 848 efx_disable_interrupts(efx); 849 efx_clear_interrupt_affinity(efx); 850 efx_nic_fini_interrupt(efx); 851 efx_fini_port(efx); 852 efx->type->fini(efx); 853 efx_fini_napi(efx); 854 efx_remove_all(efx); 855 } 856 857 /* Final NIC shutdown 858 * This is called only at module unload (or hotplug removal). A PF can call 859 * this on its VFs to ensure they are unbound first. 860 */ 861 static void efx_pci_remove(struct pci_dev *pci_dev) 862 { 863 struct efx_probe_data *probe_data; 864 struct efx_nic *efx; 865 866 efx = pci_get_drvdata(pci_dev); 867 if (!efx) 868 return; 869 870 /* Mark the NIC as fini, then stop the interface */ 871 rtnl_lock(); 872 efx_dissociate(efx); 873 dev_close(efx->net_dev); 874 efx_disable_interrupts(efx); 875 efx->state = STATE_UNINIT; 876 rtnl_unlock(); 877 878 if (efx->type->sriov_fini) 879 efx->type->sriov_fini(efx); 880 881 efx_unregister_netdev(efx); 882 883 efx_mtd_remove(efx); 884 885 efx_pci_remove_main(efx); 886 887 efx_fini_io(efx); 888 pci_dbg(efx->pci_dev, "shutdown successful\n"); 889 890 efx_fini_struct(efx); 891 free_netdev(efx->net_dev); 892 probe_data = container_of(efx, struct efx_probe_data, efx); 893 kfree(probe_data); 894 }; 895 896 /* NIC VPD information 897 * Called during probe to display the part number of the 898 * installed NIC. 899 */ 900 static void efx_probe_vpd_strings(struct efx_nic *efx) 901 { 902 struct pci_dev *dev = efx->pci_dev; 903 unsigned int vpd_size, kw_len; 904 u8 *vpd_data; 905 int start; 906 907 vpd_data = pci_vpd_alloc(dev, &vpd_size); 908 if (IS_ERR(vpd_data)) { 909 pci_warn(dev, "Unable to read VPD\n"); 910 return; 911 } 912 913 start = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size, 914 PCI_VPD_RO_KEYWORD_PARTNO, &kw_len); 915 if (start < 0) 916 pci_err(dev, "Part number not found or incomplete\n"); 917 else 918 pci_info(dev, "Part Number : %.*s\n", kw_len, vpd_data + start); 919 920 start = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size, 921 PCI_VPD_RO_KEYWORD_SERIALNO, &kw_len); 922 if (start < 0) 923 pci_err(dev, "Serial number not found or incomplete\n"); 924 else 925 efx->vpd_sn = kmemdup_nul(vpd_data + start, kw_len, GFP_KERNEL); 926 927 kfree(vpd_data); 928 } 929 930 931 /* Main body of NIC initialisation 932 * This is called at module load (or hotplug insertion, theoretically). 933 */ 934 static int efx_pci_probe_main(struct efx_nic *efx) 935 { 936 int rc; 937 938 /* Do start-of-day initialisation */ 939 rc = efx_probe_all(efx); 940 if (rc) 941 goto fail1; 942 943 efx_init_napi(efx); 944 945 down_write(&efx->filter_sem); 946 rc = efx->type->init(efx); 947 up_write(&efx->filter_sem); 948 if (rc) { 949 pci_err(efx->pci_dev, "failed to initialise NIC\n"); 950 goto fail3; 951 } 952 953 rc = efx_init_port(efx); 954 if (rc) { 955 netif_err(efx, probe, efx->net_dev, 956 "failed to initialise port\n"); 957 goto fail4; 958 } 959 960 rc = efx_nic_init_interrupt(efx); 961 if (rc) 962 goto fail5; 963 964 efx_set_interrupt_affinity(efx); 965 rc = efx_enable_interrupts(efx); 966 if (rc) 967 goto fail6; 968 969 return 0; 970 971 fail6: 972 efx_clear_interrupt_affinity(efx); 973 efx_nic_fini_interrupt(efx); 974 fail5: 975 efx_fini_port(efx); 976 fail4: 977 efx->type->fini(efx); 978 fail3: 979 efx_fini_napi(efx); 980 efx_remove_all(efx); 981 fail1: 982 return rc; 983 } 984 985 static int efx_pci_probe_post_io(struct efx_nic *efx) 986 { 987 struct net_device *net_dev = efx->net_dev; 988 int rc = efx_pci_probe_main(efx); 989 990 if (rc) 991 return rc; 992 993 if (efx->type->sriov_init) { 994 rc = efx->type->sriov_init(efx); 995 if (rc) 996 pci_err(efx->pci_dev, "SR-IOV can't be enabled rc %d\n", 997 rc); 998 } 999 1000 /* Determine netdevice features */ 1001 net_dev->features |= (efx->type->offload_features | NETIF_F_SG | 1002 NETIF_F_TSO | NETIF_F_RXCSUM | NETIF_F_RXALL); 1003 if (efx->type->offload_features & (NETIF_F_IPV6_CSUM | NETIF_F_HW_CSUM)) { 1004 net_dev->features |= NETIF_F_TSO6; 1005 if (efx_has_cap(efx, TX_TSO_V2_ENCAP)) 1006 net_dev->hw_enc_features |= NETIF_F_TSO6; 1007 } 1008 /* Check whether device supports TSO */ 1009 if (!efx->type->tso_versions || !efx->type->tso_versions(efx)) 1010 net_dev->features &= ~NETIF_F_ALL_TSO; 1011 /* Mask for features that also apply to VLAN devices */ 1012 net_dev->vlan_features |= (NETIF_F_HW_CSUM | NETIF_F_SG | 1013 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO | 1014 NETIF_F_RXCSUM); 1015 1016 net_dev->hw_features |= net_dev->features & ~efx->fixed_features; 1017 1018 /* Disable receiving frames with bad FCS, by default. */ 1019 net_dev->features &= ~NETIF_F_RXALL; 1020 1021 /* Disable VLAN filtering by default. It may be enforced if 1022 * the feature is fixed (i.e. VLAN filters are required to 1023 * receive VLAN tagged packets due to vPort restrictions). 1024 */ 1025 net_dev->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER; 1026 net_dev->features |= efx->fixed_features; 1027 1028 net_dev->xdp_features = NETDEV_XDP_ACT_BASIC | 1029 NETDEV_XDP_ACT_REDIRECT | 1030 NETDEV_XDP_ACT_NDO_XMIT; 1031 1032 rc = efx_register_netdev(efx); 1033 if (!rc) 1034 return 0; 1035 1036 efx_pci_remove_main(efx); 1037 return rc; 1038 } 1039 1040 /* NIC initialisation 1041 * 1042 * This is called at module load (or hotplug insertion, 1043 * theoretically). It sets up PCI mappings, resets the NIC, 1044 * sets up and registers the network devices with the kernel and hooks 1045 * the interrupt service routine. It does not prepare the device for 1046 * transmission; this is left to the first time one of the network 1047 * interfaces is brought up (i.e. efx_net_open). 1048 */ 1049 static int efx_pci_probe(struct pci_dev *pci_dev, 1050 const struct pci_device_id *entry) 1051 { 1052 struct efx_probe_data *probe_data, **probe_ptr; 1053 struct net_device *net_dev; 1054 struct efx_nic *efx; 1055 int rc; 1056 1057 /* Allocate probe data and struct efx_nic */ 1058 probe_data = kzalloc(sizeof(*probe_data), GFP_KERNEL); 1059 if (!probe_data) 1060 return -ENOMEM; 1061 probe_data->pci_dev = pci_dev; 1062 efx = &probe_data->efx; 1063 1064 /* Allocate and initialise a struct net_device */ 1065 net_dev = alloc_etherdev_mq(sizeof(probe_data), EFX_MAX_CORE_TX_QUEUES); 1066 if (!net_dev) { 1067 rc = -ENOMEM; 1068 goto fail0; 1069 } 1070 probe_ptr = netdev_priv(net_dev); 1071 *probe_ptr = probe_data; 1072 efx->net_dev = net_dev; 1073 efx->type = (const struct efx_nic_type *) entry->driver_data; 1074 efx->fixed_features |= NETIF_F_HIGHDMA; 1075 1076 pci_set_drvdata(pci_dev, efx); 1077 SET_NETDEV_DEV(net_dev, &pci_dev->dev); 1078 rc = efx_init_struct(efx, pci_dev); 1079 if (rc) 1080 goto fail1; 1081 efx->mdio.dev = net_dev; 1082 1083 pci_info(pci_dev, "Solarflare NIC detected\n"); 1084 1085 if (!efx->type->is_vf) 1086 efx_probe_vpd_strings(efx); 1087 1088 /* Set up basic I/O (BAR mappings etc) */ 1089 rc = efx_init_io(efx, efx->type->mem_bar(efx), efx->type->max_dma_mask, 1090 efx->type->mem_map_size(efx)); 1091 if (rc) 1092 goto fail2; 1093 1094 rc = efx_pci_probe_post_io(efx); 1095 if (rc) { 1096 /* On failure, retry once immediately. 1097 * If we aborted probe due to a scheduled reset, dismiss it. 1098 */ 1099 efx->reset_pending = 0; 1100 rc = efx_pci_probe_post_io(efx); 1101 if (rc) { 1102 /* On another failure, retry once more 1103 * after a 50-305ms delay. 1104 */ 1105 unsigned char r; 1106 1107 get_random_bytes(&r, 1); 1108 msleep((unsigned int)r + 50); 1109 efx->reset_pending = 0; 1110 rc = efx_pci_probe_post_io(efx); 1111 } 1112 } 1113 if (rc) 1114 goto fail3; 1115 1116 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n"); 1117 1118 /* Try to create MTDs, but allow this to fail */ 1119 rtnl_lock(); 1120 rc = efx_mtd_probe(efx); 1121 rtnl_unlock(); 1122 if (rc && rc != -EPERM) 1123 netif_warn(efx, probe, efx->net_dev, 1124 "failed to create MTDs (%d)\n", rc); 1125 1126 if (efx->type->udp_tnl_push_ports) 1127 efx->type->udp_tnl_push_ports(efx); 1128 1129 return 0; 1130 1131 fail3: 1132 efx_fini_io(efx); 1133 fail2: 1134 efx_fini_struct(efx); 1135 fail1: 1136 WARN_ON(rc > 0); 1137 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc); 1138 free_netdev(net_dev); 1139 fail0: 1140 kfree(probe_data); 1141 return rc; 1142 } 1143 1144 /* efx_pci_sriov_configure returns the actual number of Virtual Functions 1145 * enabled on success 1146 */ 1147 #ifdef CONFIG_SFC_SRIOV 1148 static int efx_pci_sriov_configure(struct pci_dev *dev, int num_vfs) 1149 { 1150 int rc; 1151 struct efx_nic *efx = pci_get_drvdata(dev); 1152 1153 if (efx->type->sriov_configure) { 1154 rc = efx->type->sriov_configure(efx, num_vfs); 1155 if (rc) 1156 return rc; 1157 else 1158 return num_vfs; 1159 } else 1160 return -EOPNOTSUPP; 1161 } 1162 #endif 1163 1164 static int efx_pm_freeze(struct device *dev) 1165 { 1166 struct efx_nic *efx = dev_get_drvdata(dev); 1167 1168 rtnl_lock(); 1169 1170 if (efx_net_active(efx->state)) { 1171 efx_device_detach_sync(efx); 1172 1173 efx_stop_all(efx); 1174 efx_disable_interrupts(efx); 1175 1176 efx->state = efx_freeze(efx->state); 1177 } 1178 1179 rtnl_unlock(); 1180 1181 return 0; 1182 } 1183 1184 static void efx_pci_shutdown(struct pci_dev *pci_dev) 1185 { 1186 struct efx_nic *efx = pci_get_drvdata(pci_dev); 1187 1188 if (!efx) 1189 return; 1190 1191 efx_pm_freeze(&pci_dev->dev); 1192 pci_disable_device(pci_dev); 1193 } 1194 1195 static int efx_pm_thaw(struct device *dev) 1196 { 1197 int rc; 1198 struct efx_nic *efx = dev_get_drvdata(dev); 1199 1200 rtnl_lock(); 1201 1202 if (efx_frozen(efx->state)) { 1203 rc = efx_enable_interrupts(efx); 1204 if (rc) 1205 goto fail; 1206 1207 mutex_lock(&efx->mac_lock); 1208 efx_mcdi_port_reconfigure(efx); 1209 mutex_unlock(&efx->mac_lock); 1210 1211 efx_start_all(efx); 1212 1213 efx_device_attach_if_not_resetting(efx); 1214 1215 efx->state = efx_thaw(efx->state); 1216 1217 efx->type->resume_wol(efx); 1218 } 1219 1220 rtnl_unlock(); 1221 1222 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */ 1223 efx_queue_reset_work(efx); 1224 1225 return 0; 1226 1227 fail: 1228 rtnl_unlock(); 1229 1230 return rc; 1231 } 1232 1233 static int efx_pm_poweroff(struct device *dev) 1234 { 1235 struct pci_dev *pci_dev = to_pci_dev(dev); 1236 struct efx_nic *efx = pci_get_drvdata(pci_dev); 1237 1238 efx->type->fini(efx); 1239 1240 efx->reset_pending = 0; 1241 1242 pci_save_state(pci_dev); 1243 return pci_set_power_state(pci_dev, PCI_D3hot); 1244 } 1245 1246 /* Used for both resume and restore */ 1247 static int efx_pm_resume(struct device *dev) 1248 { 1249 struct pci_dev *pci_dev = to_pci_dev(dev); 1250 struct efx_nic *efx = pci_get_drvdata(pci_dev); 1251 int rc; 1252 1253 rc = pci_set_power_state(pci_dev, PCI_D0); 1254 if (rc) 1255 return rc; 1256 pci_restore_state(pci_dev); 1257 rc = pci_enable_device(pci_dev); 1258 if (rc) 1259 return rc; 1260 pci_set_master(efx->pci_dev); 1261 rc = efx->type->reset(efx, RESET_TYPE_ALL); 1262 if (rc) 1263 return rc; 1264 down_write(&efx->filter_sem); 1265 rc = efx->type->init(efx); 1266 up_write(&efx->filter_sem); 1267 if (rc) 1268 return rc; 1269 rc = efx_pm_thaw(dev); 1270 return rc; 1271 } 1272 1273 static int efx_pm_suspend(struct device *dev) 1274 { 1275 int rc; 1276 1277 efx_pm_freeze(dev); 1278 rc = efx_pm_poweroff(dev); 1279 if (rc) 1280 efx_pm_resume(dev); 1281 return rc; 1282 } 1283 1284 static const struct dev_pm_ops efx_pm_ops = { 1285 .suspend = efx_pm_suspend, 1286 .resume = efx_pm_resume, 1287 .freeze = efx_pm_freeze, 1288 .thaw = efx_pm_thaw, 1289 .poweroff = efx_pm_poweroff, 1290 .restore = efx_pm_resume, 1291 }; 1292 1293 static struct pci_driver efx_pci_driver = { 1294 .name = KBUILD_MODNAME, 1295 .id_table = efx_pci_table, 1296 .probe = efx_pci_probe, 1297 .remove = efx_pci_remove, 1298 .driver.pm = &efx_pm_ops, 1299 .shutdown = efx_pci_shutdown, 1300 .err_handler = &efx_err_handlers, 1301 #ifdef CONFIG_SFC_SRIOV 1302 .sriov_configure = efx_pci_sriov_configure, 1303 #endif 1304 }; 1305 1306 /************************************************************************** 1307 * 1308 * Kernel module interface 1309 * 1310 *************************************************************************/ 1311 1312 static int __init efx_init_module(void) 1313 { 1314 int rc; 1315 1316 printk(KERN_INFO "Solarflare NET driver\n"); 1317 1318 rc = register_netdevice_notifier(&efx_netdev_notifier); 1319 if (rc) 1320 goto err_notifier; 1321 1322 rc = efx_create_reset_workqueue(); 1323 if (rc) 1324 goto err_reset; 1325 1326 rc = pci_register_driver(&efx_pci_driver); 1327 if (rc < 0) 1328 goto err_pci; 1329 1330 rc = pci_register_driver(&ef100_pci_driver); 1331 if (rc < 0) 1332 goto err_pci_ef100; 1333 1334 return 0; 1335 1336 err_pci_ef100: 1337 pci_unregister_driver(&efx_pci_driver); 1338 err_pci: 1339 efx_destroy_reset_workqueue(); 1340 err_reset: 1341 unregister_netdevice_notifier(&efx_netdev_notifier); 1342 err_notifier: 1343 return rc; 1344 } 1345 1346 static void __exit efx_exit_module(void) 1347 { 1348 printk(KERN_INFO "Solarflare NET driver unloading\n"); 1349 1350 pci_unregister_driver(&ef100_pci_driver); 1351 pci_unregister_driver(&efx_pci_driver); 1352 efx_destroy_reset_workqueue(); 1353 unregister_netdevice_notifier(&efx_netdev_notifier); 1354 1355 } 1356 1357 module_init(efx_init_module); 1358 module_exit(efx_exit_module); 1359 1360 MODULE_AUTHOR("Solarflare Communications and " 1361 "Michael Brown <mbrown@fensystems.co.uk>"); 1362 MODULE_DESCRIPTION("Solarflare network driver"); 1363 MODULE_LICENSE("GPL"); 1364 MODULE_DEVICE_TABLE(pci, efx_pci_table); 1365