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