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 return 0; 544 } 545 546 /* Context: process, rtnl_lock() held. 547 * Note that the kernel will ignore our return code; this method 548 * should really be a void. 549 */ 550 int efx_net_stop(struct net_device *net_dev) 551 { 552 struct efx_nic *efx = efx_netdev_priv(net_dev); 553 554 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n", 555 raw_smp_processor_id()); 556 557 /* Stop the device and flush all the channels */ 558 efx_stop_all(efx); 559 560 return 0; 561 } 562 563 static int efx_vlan_rx_add_vid(struct net_device *net_dev, __be16 proto, u16 vid) 564 { 565 struct efx_nic *efx = efx_netdev_priv(net_dev); 566 567 if (efx->type->vlan_rx_add_vid) 568 return efx->type->vlan_rx_add_vid(efx, proto, vid); 569 else 570 return -EOPNOTSUPP; 571 } 572 573 static int efx_vlan_rx_kill_vid(struct net_device *net_dev, __be16 proto, u16 vid) 574 { 575 struct efx_nic *efx = efx_netdev_priv(net_dev); 576 577 if (efx->type->vlan_rx_kill_vid) 578 return efx->type->vlan_rx_kill_vid(efx, proto, vid); 579 else 580 return -EOPNOTSUPP; 581 } 582 583 static const struct net_device_ops efx_netdev_ops = { 584 .ndo_open = efx_net_open, 585 .ndo_stop = efx_net_stop, 586 .ndo_get_stats64 = efx_net_stats, 587 .ndo_tx_timeout = efx_watchdog, 588 .ndo_start_xmit = efx_hard_start_xmit, 589 .ndo_validate_addr = eth_validate_addr, 590 .ndo_eth_ioctl = efx_ioctl, 591 .ndo_change_mtu = efx_change_mtu, 592 .ndo_set_mac_address = efx_set_mac_address, 593 .ndo_set_rx_mode = efx_set_rx_mode, 594 .ndo_set_features = efx_set_features, 595 .ndo_features_check = efx_features_check, 596 .ndo_vlan_rx_add_vid = efx_vlan_rx_add_vid, 597 .ndo_vlan_rx_kill_vid = efx_vlan_rx_kill_vid, 598 #ifdef CONFIG_SFC_SRIOV 599 .ndo_set_vf_mac = efx_sriov_set_vf_mac, 600 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan, 601 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk, 602 .ndo_get_vf_config = efx_sriov_get_vf_config, 603 .ndo_set_vf_link_state = efx_sriov_set_vf_link_state, 604 #endif 605 .ndo_get_phys_port_id = efx_get_phys_port_id, 606 .ndo_get_phys_port_name = efx_get_phys_port_name, 607 .ndo_setup_tc = efx_setup_tc, 608 #ifdef CONFIG_RFS_ACCEL 609 .ndo_rx_flow_steer = efx_filter_rfs, 610 #endif 611 .ndo_xdp_xmit = efx_xdp_xmit, 612 .ndo_bpf = efx_xdp 613 }; 614 615 static int efx_xdp_setup_prog(struct efx_nic *efx, struct bpf_prog *prog) 616 { 617 struct bpf_prog *old_prog; 618 619 if (efx->xdp_rxq_info_failed) { 620 netif_err(efx, drv, efx->net_dev, 621 "Unable to bind XDP program due to previous failure of rxq_info\n"); 622 return -EINVAL; 623 } 624 625 if (prog && efx->net_dev->mtu > efx_xdp_max_mtu(efx)) { 626 netif_err(efx, drv, efx->net_dev, 627 "Unable to configure XDP with MTU of %d (max: %d)\n", 628 efx->net_dev->mtu, efx_xdp_max_mtu(efx)); 629 return -EINVAL; 630 } 631 632 old_prog = rtnl_dereference(efx->xdp_prog); 633 rcu_assign_pointer(efx->xdp_prog, prog); 634 /* Release the reference that was originally passed by the caller. */ 635 if (old_prog) 636 bpf_prog_put(old_prog); 637 638 return 0; 639 } 640 641 /* Context: process, rtnl_lock() held. */ 642 static int efx_xdp(struct net_device *dev, struct netdev_bpf *xdp) 643 { 644 struct efx_nic *efx = efx_netdev_priv(dev); 645 646 switch (xdp->command) { 647 case XDP_SETUP_PROG: 648 return efx_xdp_setup_prog(efx, xdp->prog); 649 default: 650 return -EINVAL; 651 } 652 } 653 654 static int efx_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **xdpfs, 655 u32 flags) 656 { 657 struct efx_nic *efx = efx_netdev_priv(dev); 658 659 if (!netif_running(dev)) 660 return -EINVAL; 661 662 return efx_xdp_tx_buffers(efx, n, xdpfs, flags & XDP_XMIT_FLUSH); 663 } 664 665 static void efx_update_name(struct efx_nic *efx) 666 { 667 strcpy(efx->name, efx->net_dev->name); 668 efx_mtd_rename(efx); 669 efx_set_channel_names(efx); 670 } 671 672 static int efx_netdev_event(struct notifier_block *this, 673 unsigned long event, void *ptr) 674 { 675 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr); 676 677 if ((net_dev->netdev_ops == &efx_netdev_ops) && 678 event == NETDEV_CHANGENAME) 679 efx_update_name(efx_netdev_priv(net_dev)); 680 681 return NOTIFY_DONE; 682 } 683 684 static struct notifier_block efx_netdev_notifier = { 685 .notifier_call = efx_netdev_event, 686 }; 687 688 static ssize_t phy_type_show(struct device *dev, 689 struct device_attribute *attr, char *buf) 690 { 691 struct efx_nic *efx = dev_get_drvdata(dev); 692 return sprintf(buf, "%d\n", efx->phy_type); 693 } 694 static DEVICE_ATTR_RO(phy_type); 695 696 static int efx_register_netdev(struct efx_nic *efx) 697 { 698 struct net_device *net_dev = efx->net_dev; 699 struct efx_channel *channel; 700 int rc; 701 702 net_dev->watchdog_timeo = 5 * HZ; 703 net_dev->irq = efx->pci_dev->irq; 704 net_dev->netdev_ops = &efx_netdev_ops; 705 if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0) 706 net_dev->priv_flags |= IFF_UNICAST_FLT; 707 net_dev->ethtool_ops = &efx_ethtool_ops; 708 netif_set_tso_max_segs(net_dev, EFX_TSO_MAX_SEGS); 709 net_dev->min_mtu = EFX_MIN_MTU; 710 net_dev->max_mtu = EFX_MAX_MTU; 711 712 rtnl_lock(); 713 714 /* Enable resets to be scheduled and check whether any were 715 * already requested. If so, the NIC is probably hosed so we 716 * abort. 717 */ 718 if (efx->reset_pending) { 719 pci_err(efx->pci_dev, "aborting probe due to scheduled reset\n"); 720 rc = -EIO; 721 goto fail_locked; 722 } 723 724 rc = dev_alloc_name(net_dev, net_dev->name); 725 if (rc < 0) 726 goto fail_locked; 727 efx_update_name(efx); 728 729 /* Always start with carrier off; PHY events will detect the link */ 730 netif_carrier_off(net_dev); 731 732 rc = register_netdevice(net_dev); 733 if (rc) 734 goto fail_locked; 735 736 efx_for_each_channel(channel, efx) { 737 struct efx_tx_queue *tx_queue; 738 efx_for_each_channel_tx_queue(tx_queue, channel) 739 efx_init_tx_queue_core_txq(tx_queue); 740 } 741 742 efx_associate(efx); 743 744 efx->state = STATE_NET_DOWN; 745 746 rtnl_unlock(); 747 748 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type); 749 if (rc) { 750 netif_err(efx, drv, efx->net_dev, 751 "failed to init net dev attributes\n"); 752 goto fail_registered; 753 } 754 755 efx_init_mcdi_logging(efx); 756 757 return 0; 758 759 fail_registered: 760 rtnl_lock(); 761 efx_dissociate(efx); 762 unregister_netdevice(net_dev); 763 fail_locked: 764 efx->state = STATE_UNINIT; 765 rtnl_unlock(); 766 netif_err(efx, drv, efx->net_dev, "could not register net dev\n"); 767 return rc; 768 } 769 770 static void efx_unregister_netdev(struct efx_nic *efx) 771 { 772 if (!efx->net_dev) 773 return; 774 775 if (WARN_ON(efx_netdev_priv(efx->net_dev) != efx)) 776 return; 777 778 if (efx_dev_registered(efx)) { 779 strscpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name)); 780 efx_fini_mcdi_logging(efx); 781 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type); 782 unregister_netdev(efx->net_dev); 783 } 784 } 785 786 /************************************************************************** 787 * 788 * List of NICs we support 789 * 790 **************************************************************************/ 791 792 /* PCI device ID table */ 793 static const struct pci_device_id efx_pci_table[] = { 794 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0903), /* SFC9120 PF */ 795 .driver_data = (unsigned long) &efx_hunt_a0_nic_type}, 796 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1903), /* SFC9120 VF */ 797 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type}, 798 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0923), /* SFC9140 PF */ 799 .driver_data = (unsigned long) &efx_hunt_a0_nic_type}, 800 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1923), /* SFC9140 VF */ 801 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type}, 802 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0a03), /* SFC9220 PF */ 803 .driver_data = (unsigned long) &efx_hunt_a0_nic_type}, 804 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1a03), /* SFC9220 VF */ 805 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type}, 806 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0b03), /* SFC9250 PF */ 807 .driver_data = (unsigned long) &efx_hunt_a0_nic_type}, 808 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1b03), /* SFC9250 VF */ 809 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type}, 810 {0} /* end of list */ 811 }; 812 813 /************************************************************************** 814 * 815 * Data housekeeping 816 * 817 **************************************************************************/ 818 819 void efx_update_sw_stats(struct efx_nic *efx, u64 *stats) 820 { 821 u64 n_rx_nodesc_trunc = 0; 822 struct efx_channel *channel; 823 824 efx_for_each_channel(channel, efx) 825 n_rx_nodesc_trunc += channel->n_rx_nodesc_trunc; 826 stats[GENERIC_STAT_rx_nodesc_trunc] = n_rx_nodesc_trunc; 827 stats[GENERIC_STAT_rx_noskb_drops] = atomic_read(&efx->n_rx_noskb_drops); 828 } 829 830 /************************************************************************** 831 * 832 * PCI interface 833 * 834 **************************************************************************/ 835 836 /* Main body of final NIC shutdown code 837 * This is called only at module unload (or hotplug removal). 838 */ 839 static void efx_pci_remove_main(struct efx_nic *efx) 840 { 841 /* Flush reset_work. It can no longer be scheduled since we 842 * are not READY. 843 */ 844 WARN_ON(efx_net_active(efx->state)); 845 efx_flush_reset_workqueue(efx); 846 847 efx_disable_interrupts(efx); 848 efx_clear_interrupt_affinity(efx); 849 efx_nic_fini_interrupt(efx); 850 efx_fini_port(efx); 851 efx->type->fini(efx); 852 efx_fini_napi(efx); 853 efx_remove_all(efx); 854 } 855 856 /* Final NIC shutdown 857 * This is called only at module unload (or hotplug removal). A PF can call 858 * this on its VFs to ensure they are unbound first. 859 */ 860 static void efx_pci_remove(struct pci_dev *pci_dev) 861 { 862 struct efx_probe_data *probe_data; 863 struct efx_nic *efx; 864 865 efx = pci_get_drvdata(pci_dev); 866 if (!efx) 867 return; 868 869 /* Mark the NIC as fini, then stop the interface */ 870 rtnl_lock(); 871 efx_dissociate(efx); 872 dev_close(efx->net_dev); 873 efx_disable_interrupts(efx); 874 efx->state = STATE_UNINIT; 875 rtnl_unlock(); 876 877 if (efx->type->sriov_fini) 878 efx->type->sriov_fini(efx); 879 880 efx_unregister_netdev(efx); 881 882 efx_mtd_remove(efx); 883 884 efx_pci_remove_main(efx); 885 886 efx_fini_io(efx); 887 pci_dbg(efx->pci_dev, "shutdown successful\n"); 888 889 efx_fini_struct(efx); 890 free_netdev(efx->net_dev); 891 probe_data = container_of(efx, struct efx_probe_data, efx); 892 kfree(probe_data); 893 }; 894 895 /* NIC VPD information 896 * Called during probe to display the part number of the 897 * installed NIC. 898 */ 899 static void efx_probe_vpd_strings(struct efx_nic *efx) 900 { 901 struct pci_dev *dev = efx->pci_dev; 902 unsigned int vpd_size, kw_len; 903 u8 *vpd_data; 904 int start; 905 906 vpd_data = pci_vpd_alloc(dev, &vpd_size); 907 if (IS_ERR(vpd_data)) { 908 pci_warn(dev, "Unable to read VPD\n"); 909 return; 910 } 911 912 start = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size, 913 PCI_VPD_RO_KEYWORD_PARTNO, &kw_len); 914 if (start < 0) 915 pci_err(dev, "Part number not found or incomplete\n"); 916 else 917 pci_info(dev, "Part Number : %.*s\n", kw_len, vpd_data + start); 918 919 start = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size, 920 PCI_VPD_RO_KEYWORD_SERIALNO, &kw_len); 921 if (start < 0) 922 pci_err(dev, "Serial number not found or incomplete\n"); 923 else 924 efx->vpd_sn = kmemdup_nul(vpd_data + start, kw_len, GFP_KERNEL); 925 926 kfree(vpd_data); 927 } 928 929 930 /* Main body of NIC initialisation 931 * This is called at module load (or hotplug insertion, theoretically). 932 */ 933 static int efx_pci_probe_main(struct efx_nic *efx) 934 { 935 int rc; 936 937 /* Do start-of-day initialisation */ 938 rc = efx_probe_all(efx); 939 if (rc) 940 goto fail1; 941 942 efx_init_napi(efx); 943 944 down_write(&efx->filter_sem); 945 rc = efx->type->init(efx); 946 up_write(&efx->filter_sem); 947 if (rc) { 948 pci_err(efx->pci_dev, "failed to initialise NIC\n"); 949 goto fail3; 950 } 951 952 rc = efx_init_port(efx); 953 if (rc) { 954 netif_err(efx, probe, efx->net_dev, 955 "failed to initialise port\n"); 956 goto fail4; 957 } 958 959 rc = efx_nic_init_interrupt(efx); 960 if (rc) 961 goto fail5; 962 963 efx_set_interrupt_affinity(efx); 964 rc = efx_enable_interrupts(efx); 965 if (rc) 966 goto fail6; 967 968 return 0; 969 970 fail6: 971 efx_clear_interrupt_affinity(efx); 972 efx_nic_fini_interrupt(efx); 973 fail5: 974 efx_fini_port(efx); 975 fail4: 976 efx->type->fini(efx); 977 fail3: 978 efx_fini_napi(efx); 979 efx_remove_all(efx); 980 fail1: 981 return rc; 982 } 983 984 static int efx_pci_probe_post_io(struct efx_nic *efx) 985 { 986 struct net_device *net_dev = efx->net_dev; 987 int rc = efx_pci_probe_main(efx); 988 989 if (rc) 990 return rc; 991 992 if (efx->type->sriov_init) { 993 rc = efx->type->sriov_init(efx); 994 if (rc) 995 pci_err(efx->pci_dev, "SR-IOV can't be enabled rc %d\n", 996 rc); 997 } 998 999 /* Determine netdevice features */ 1000 net_dev->features |= efx->type->offload_features; 1001 1002 /* Add TSO features */ 1003 if (efx->type->tso_versions && efx->type->tso_versions(efx)) 1004 net_dev->features |= NETIF_F_TSO | NETIF_F_TSO6; 1005 1006 /* Mask for features that also apply to VLAN devices */ 1007 net_dev->vlan_features |= (NETIF_F_HW_CSUM | NETIF_F_SG | 1008 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO | 1009 NETIF_F_RXCSUM); 1010 1011 /* Determine user configurable features */ 1012 net_dev->hw_features |= net_dev->features & ~efx->fixed_features; 1013 1014 /* Disable receiving frames with bad FCS, by default. */ 1015 net_dev->features &= ~NETIF_F_RXALL; 1016 1017 /* Disable VLAN filtering by default. It may be enforced if 1018 * the feature is fixed (i.e. VLAN filters are required to 1019 * receive VLAN tagged packets due to vPort restrictions). 1020 */ 1021 net_dev->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER; 1022 net_dev->features |= efx->fixed_features; 1023 1024 net_dev->xdp_features = NETDEV_XDP_ACT_BASIC | 1025 NETDEV_XDP_ACT_REDIRECT | 1026 NETDEV_XDP_ACT_NDO_XMIT; 1027 1028 rc = efx_register_netdev(efx); 1029 if (!rc) 1030 return 0; 1031 1032 efx_pci_remove_main(efx); 1033 return rc; 1034 } 1035 1036 /* NIC initialisation 1037 * 1038 * This is called at module load (or hotplug insertion, 1039 * theoretically). It sets up PCI mappings, resets the NIC, 1040 * sets up and registers the network devices with the kernel and hooks 1041 * the interrupt service routine. It does not prepare the device for 1042 * transmission; this is left to the first time one of the network 1043 * interfaces is brought up (i.e. efx_net_open). 1044 */ 1045 static int efx_pci_probe(struct pci_dev *pci_dev, 1046 const struct pci_device_id *entry) 1047 { 1048 struct efx_probe_data *probe_data, **probe_ptr; 1049 struct net_device *net_dev; 1050 struct efx_nic *efx; 1051 int rc; 1052 1053 /* Allocate probe data and struct efx_nic */ 1054 probe_data = kzalloc(sizeof(*probe_data), GFP_KERNEL); 1055 if (!probe_data) 1056 return -ENOMEM; 1057 probe_data->pci_dev = pci_dev; 1058 efx = &probe_data->efx; 1059 1060 /* Allocate and initialise a struct net_device */ 1061 net_dev = alloc_etherdev_mq(sizeof(probe_data), EFX_MAX_CORE_TX_QUEUES); 1062 if (!net_dev) { 1063 rc = -ENOMEM; 1064 goto fail0; 1065 } 1066 probe_ptr = netdev_priv(net_dev); 1067 *probe_ptr = probe_data; 1068 efx->net_dev = net_dev; 1069 efx->type = (const struct efx_nic_type *) entry->driver_data; 1070 efx->fixed_features |= NETIF_F_HIGHDMA; 1071 1072 pci_set_drvdata(pci_dev, efx); 1073 SET_NETDEV_DEV(net_dev, &pci_dev->dev); 1074 rc = efx_init_struct(efx, pci_dev); 1075 if (rc) 1076 goto fail1; 1077 efx->mdio.dev = net_dev; 1078 1079 pci_info(pci_dev, "Solarflare NIC detected\n"); 1080 1081 if (!efx->type->is_vf) 1082 efx_probe_vpd_strings(efx); 1083 1084 /* Set up basic I/O (BAR mappings etc) */ 1085 rc = efx_init_io(efx, efx->type->mem_bar(efx), efx->type->max_dma_mask, 1086 efx->type->mem_map_size(efx)); 1087 if (rc) 1088 goto fail2; 1089 1090 rc = efx_pci_probe_post_io(efx); 1091 if (rc) { 1092 /* On failure, retry once immediately. 1093 * If we aborted probe due to a scheduled reset, dismiss it. 1094 */ 1095 efx->reset_pending = 0; 1096 rc = efx_pci_probe_post_io(efx); 1097 if (rc) { 1098 /* On another failure, retry once more 1099 * after a 50-305ms delay. 1100 */ 1101 unsigned char r; 1102 1103 get_random_bytes(&r, 1); 1104 msleep((unsigned int)r + 50); 1105 efx->reset_pending = 0; 1106 rc = efx_pci_probe_post_io(efx); 1107 } 1108 } 1109 if (rc) 1110 goto fail3; 1111 1112 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n"); 1113 1114 /* Try to create MTDs, but allow this to fail */ 1115 rtnl_lock(); 1116 rc = efx_mtd_probe(efx); 1117 rtnl_unlock(); 1118 if (rc && rc != -EPERM) 1119 netif_warn(efx, probe, efx->net_dev, 1120 "failed to create MTDs (%d)\n", rc); 1121 1122 if (efx->type->udp_tnl_push_ports) 1123 efx->type->udp_tnl_push_ports(efx); 1124 1125 return 0; 1126 1127 fail3: 1128 efx_fini_io(efx); 1129 fail2: 1130 efx_fini_struct(efx); 1131 fail1: 1132 WARN_ON(rc > 0); 1133 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc); 1134 free_netdev(net_dev); 1135 fail0: 1136 kfree(probe_data); 1137 return rc; 1138 } 1139 1140 /* efx_pci_sriov_configure returns the actual number of Virtual Functions 1141 * enabled on success 1142 */ 1143 #ifdef CONFIG_SFC_SRIOV 1144 static int efx_pci_sriov_configure(struct pci_dev *dev, int num_vfs) 1145 { 1146 int rc; 1147 struct efx_nic *efx = pci_get_drvdata(dev); 1148 1149 if (efx->type->sriov_configure) { 1150 rc = efx->type->sriov_configure(efx, num_vfs); 1151 if (rc) 1152 return rc; 1153 else 1154 return num_vfs; 1155 } else 1156 return -EOPNOTSUPP; 1157 } 1158 #endif 1159 1160 static int efx_pm_freeze(struct device *dev) 1161 { 1162 struct efx_nic *efx = dev_get_drvdata(dev); 1163 1164 rtnl_lock(); 1165 1166 if (efx_net_active(efx->state)) { 1167 efx_device_detach_sync(efx); 1168 1169 efx_stop_all(efx); 1170 efx_disable_interrupts(efx); 1171 1172 efx->state = efx_freeze(efx->state); 1173 } 1174 1175 rtnl_unlock(); 1176 1177 return 0; 1178 } 1179 1180 static void efx_pci_shutdown(struct pci_dev *pci_dev) 1181 { 1182 struct efx_nic *efx = pci_get_drvdata(pci_dev); 1183 1184 if (!efx) 1185 return; 1186 1187 efx_pm_freeze(&pci_dev->dev); 1188 pci_disable_device(pci_dev); 1189 } 1190 1191 static int efx_pm_thaw(struct device *dev) 1192 { 1193 int rc; 1194 struct efx_nic *efx = dev_get_drvdata(dev); 1195 1196 rtnl_lock(); 1197 1198 if (efx_frozen(efx->state)) { 1199 rc = efx_enable_interrupts(efx); 1200 if (rc) 1201 goto fail; 1202 1203 mutex_lock(&efx->mac_lock); 1204 efx_mcdi_port_reconfigure(efx); 1205 mutex_unlock(&efx->mac_lock); 1206 1207 efx_start_all(efx); 1208 1209 efx_device_attach_if_not_resetting(efx); 1210 1211 efx->state = efx_thaw(efx->state); 1212 1213 efx->type->resume_wol(efx); 1214 } 1215 1216 rtnl_unlock(); 1217 1218 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */ 1219 efx_queue_reset_work(efx); 1220 1221 return 0; 1222 1223 fail: 1224 rtnl_unlock(); 1225 1226 return rc; 1227 } 1228 1229 static int efx_pm_poweroff(struct device *dev) 1230 { 1231 struct pci_dev *pci_dev = to_pci_dev(dev); 1232 struct efx_nic *efx = pci_get_drvdata(pci_dev); 1233 1234 efx->type->fini(efx); 1235 1236 efx->reset_pending = 0; 1237 1238 pci_save_state(pci_dev); 1239 return pci_set_power_state(pci_dev, PCI_D3hot); 1240 } 1241 1242 /* Used for both resume and restore */ 1243 static int efx_pm_resume(struct device *dev) 1244 { 1245 struct pci_dev *pci_dev = to_pci_dev(dev); 1246 struct efx_nic *efx = pci_get_drvdata(pci_dev); 1247 int rc; 1248 1249 rc = pci_set_power_state(pci_dev, PCI_D0); 1250 if (rc) 1251 return rc; 1252 pci_restore_state(pci_dev); 1253 rc = pci_enable_device(pci_dev); 1254 if (rc) 1255 return rc; 1256 pci_set_master(efx->pci_dev); 1257 rc = efx->type->reset(efx, RESET_TYPE_ALL); 1258 if (rc) 1259 return rc; 1260 down_write(&efx->filter_sem); 1261 rc = efx->type->init(efx); 1262 up_write(&efx->filter_sem); 1263 if (rc) 1264 return rc; 1265 rc = efx_pm_thaw(dev); 1266 return rc; 1267 } 1268 1269 static int efx_pm_suspend(struct device *dev) 1270 { 1271 int rc; 1272 1273 efx_pm_freeze(dev); 1274 rc = efx_pm_poweroff(dev); 1275 if (rc) 1276 efx_pm_resume(dev); 1277 return rc; 1278 } 1279 1280 static const struct dev_pm_ops efx_pm_ops = { 1281 .suspend = efx_pm_suspend, 1282 .resume = efx_pm_resume, 1283 .freeze = efx_pm_freeze, 1284 .thaw = efx_pm_thaw, 1285 .poweroff = efx_pm_poweroff, 1286 .restore = efx_pm_resume, 1287 }; 1288 1289 static struct pci_driver efx_pci_driver = { 1290 .name = KBUILD_MODNAME, 1291 .id_table = efx_pci_table, 1292 .probe = efx_pci_probe, 1293 .remove = efx_pci_remove, 1294 .driver.pm = &efx_pm_ops, 1295 .shutdown = efx_pci_shutdown, 1296 .err_handler = &efx_err_handlers, 1297 #ifdef CONFIG_SFC_SRIOV 1298 .sriov_configure = efx_pci_sriov_configure, 1299 #endif 1300 }; 1301 1302 /************************************************************************** 1303 * 1304 * Kernel module interface 1305 * 1306 *************************************************************************/ 1307 1308 static int __init efx_init_module(void) 1309 { 1310 int rc; 1311 1312 printk(KERN_INFO "Solarflare NET driver\n"); 1313 1314 rc = register_netdevice_notifier(&efx_netdev_notifier); 1315 if (rc) 1316 goto err_notifier; 1317 1318 rc = efx_create_reset_workqueue(); 1319 if (rc) 1320 goto err_reset; 1321 1322 rc = pci_register_driver(&efx_pci_driver); 1323 if (rc < 0) 1324 goto err_pci; 1325 1326 rc = pci_register_driver(&ef100_pci_driver); 1327 if (rc < 0) 1328 goto err_pci_ef100; 1329 1330 return 0; 1331 1332 err_pci_ef100: 1333 pci_unregister_driver(&efx_pci_driver); 1334 err_pci: 1335 efx_destroy_reset_workqueue(); 1336 err_reset: 1337 unregister_netdevice_notifier(&efx_netdev_notifier); 1338 err_notifier: 1339 return rc; 1340 } 1341 1342 static void __exit efx_exit_module(void) 1343 { 1344 printk(KERN_INFO "Solarflare NET driver unloading\n"); 1345 1346 pci_unregister_driver(&ef100_pci_driver); 1347 pci_unregister_driver(&efx_pci_driver); 1348 efx_destroy_reset_workqueue(); 1349 unregister_netdevice_notifier(&efx_netdev_notifier); 1350 1351 } 1352 1353 module_init(efx_init_module); 1354 module_exit(efx_exit_module); 1355 1356 MODULE_AUTHOR("Solarflare Communications and " 1357 "Michael Brown <mbrown@fensystems.co.uk>"); 1358 MODULE_DESCRIPTION("Solarflare network driver"); 1359 MODULE_LICENSE("GPL"); 1360 MODULE_DEVICE_TABLE(pci, efx_pci_table); 1361