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.
8 #include <linux/module.h>
10 #include <linux/netdevice.h>
11 #include <linux/etherdevice.h>
12 #include <linux/delay.h>
13 #include <linux/notifier.h>
15 #include <linux/tcp.h>
17 #include <linux/ethtool.h>
18 #include <linux/topology.h>
19 #include <linux/gfp.h>
20 #include <linux/aer.h>
21 #include <linux/interrupt.h>
22 #include "net_driver.h"
24 #include <net/udp_tunnel.h>
26 #include "efx_common.h"
27 #include "efx_channels.h"
29 #include "rx_common.h"
30 #include "tx_common.h"
36 #include "mcdi_port_common.h"
37 #include "mcdi_pcol.h"
38 #include "workarounds.h"
40 /**************************************************************************
44 *************************************************************************/
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)");
50 module_param(rss_cpus, uint, 0444);
51 MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
54 * Use separate channels for TX and RX events
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.
59 * This is only used in MSI-X interrupt mode
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");
66 /* Initial interrupt moderation settings. They can be modified after
67 * module load with ethtool.
69 * The default for RX should strike a balance between increasing the
70 * round-trip latency and reducing overhead.
72 static unsigned int rx_irq_mod_usec = 60;
74 /* Initial interrupt moderation settings. They can be modified after
75 * module load with ethtool.
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.
83 static unsigned int tx_irq_mod_usec = 150;
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");
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");
96 /**************************************************************************
98 * Utility functions and prototypes
100 *************************************************************************/
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,
108 #define EFX_ASSERT_RESET_SERIALISED(efx) \
110 if ((efx->state == STATE_READY) || \
111 (efx->state == STATE_RECOVERY) || \
112 (efx->state == STATE_DISABLED)) \
116 /**************************************************************************
120 **************************************************************************/
122 static void efx_fini_port(struct efx_nic *efx);
124 static int efx_probe_port(struct efx_nic *efx)
128 netif_dbg(efx, probe, efx->net_dev, "create port\n");
131 efx->phy_mode = PHY_MODE_SPECIAL;
133 /* Connect up MAC/PHY operations table */
134 rc = efx->type->probe_port(efx);
138 /* Initialise MAC address to permanent address */
139 ether_addr_copy(efx->net_dev->dev_addr, efx->net_dev->perm_addr);
144 static int efx_init_port(struct efx_nic *efx)
148 netif_dbg(efx, drv, efx->net_dev, "init port\n");
150 mutex_lock(&efx->mac_lock);
152 efx->port_initialized = true;
154 /* Ensure the PHY advertises the correct flow control settings */
155 rc = efx_mcdi_port_reconfigure(efx);
156 if (rc && rc != -EPERM)
159 mutex_unlock(&efx->mac_lock);
163 mutex_unlock(&efx->mac_lock);
167 static void efx_fini_port(struct efx_nic *efx)
169 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
171 if (!efx->port_initialized)
174 efx->port_initialized = false;
176 efx->link_state.up = false;
177 efx_link_status_changed(efx);
180 static void efx_remove_port(struct efx_nic *efx)
182 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
184 efx->type->remove_port(efx);
187 /**************************************************************************
191 **************************************************************************/
193 static LIST_HEAD(efx_primary_list);
194 static LIST_HEAD(efx_unassociated_list);
196 static bool efx_same_controller(struct efx_nic *left, struct efx_nic *right)
198 return left->type == right->type &&
199 left->vpd_sn && right->vpd_sn &&
200 !strcmp(left->vpd_sn, right->vpd_sn);
203 static void efx_associate(struct efx_nic *efx)
205 struct efx_nic *other, *next;
207 if (efx->primary == efx) {
208 /* Adding primary function; look for secondaries */
210 netif_dbg(efx, probe, efx->net_dev, "adding to primary list\n");
211 list_add_tail(&efx->node, &efx_primary_list);
213 list_for_each_entry_safe(other, next, &efx_unassociated_list,
215 if (efx_same_controller(efx, other)) {
216 list_del(&other->node);
217 netif_dbg(other, probe, other->net_dev,
218 "moving to secondary list of %s %s\n",
219 pci_name(efx->pci_dev),
221 list_add_tail(&other->node,
222 &efx->secondary_list);
223 other->primary = efx;
227 /* Adding secondary function; look for primary */
229 list_for_each_entry(other, &efx_primary_list, node) {
230 if (efx_same_controller(efx, other)) {
231 netif_dbg(efx, probe, efx->net_dev,
232 "adding to secondary list of %s %s\n",
233 pci_name(other->pci_dev),
234 other->net_dev->name);
235 list_add_tail(&efx->node,
236 &other->secondary_list);
237 efx->primary = other;
242 netif_dbg(efx, probe, efx->net_dev,
243 "adding to unassociated list\n");
244 list_add_tail(&efx->node, &efx_unassociated_list);
248 static void efx_dissociate(struct efx_nic *efx)
250 struct efx_nic *other, *next;
252 list_del(&efx->node);
255 list_for_each_entry_safe(other, next, &efx->secondary_list, node) {
256 list_del(&other->node);
257 netif_dbg(other, probe, other->net_dev,
258 "moving to unassociated list\n");
259 list_add_tail(&other->node, &efx_unassociated_list);
260 other->primary = NULL;
264 static int efx_probe_nic(struct efx_nic *efx)
268 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
270 /* Carry out hardware-type specific initialisation */
271 rc = efx->type->probe(efx);
276 if (!efx->max_channels || !efx->max_tx_channels) {
277 netif_err(efx, drv, efx->net_dev,
278 "Insufficient resources to allocate"
284 /* Determine the number of channels and queues by trying
285 * to hook in MSI-X interrupts.
287 rc = efx_probe_interrupts(efx);
291 rc = efx_set_channels(efx);
295 /* dimension_resources can fail with EAGAIN */
296 rc = efx->type->dimension_resources(efx);
297 if (rc != 0 && rc != -EAGAIN)
301 /* try again with new max_channels */
302 efx_remove_interrupts(efx);
304 } while (rc == -EAGAIN);
306 if (efx->n_channels > 1)
307 netdev_rss_key_fill(efx->rss_context.rx_hash_key,
308 sizeof(efx->rss_context.rx_hash_key));
309 efx_set_default_rx_indir_table(efx, &efx->rss_context);
311 /* Initialise the interrupt moderation settings */
312 efx->irq_mod_step_us = DIV_ROUND_UP(efx->timer_quantum_ns, 1000);
313 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
319 efx_remove_interrupts(efx);
321 efx->type->remove(efx);
325 static void efx_remove_nic(struct efx_nic *efx)
327 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
329 efx_remove_interrupts(efx);
330 efx->type->remove(efx);
333 /**************************************************************************
335 * NIC startup/shutdown
337 *************************************************************************/
339 static int efx_probe_all(struct efx_nic *efx)
343 rc = efx_probe_nic(efx);
345 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
349 rc = efx_probe_port(efx);
351 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
355 BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
356 if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
361 #ifdef CONFIG_SFC_SRIOV
362 rc = efx->type->vswitching_probe(efx);
363 if (rc) /* not fatal; the PF will still work fine */
364 netif_warn(efx, probe, efx->net_dev,
365 "failed to setup vswitching rc=%d;"
366 " VFs may not function\n", rc);
369 rc = efx_probe_filters(efx);
371 netif_err(efx, probe, efx->net_dev,
372 "failed to create filter tables\n");
376 rc = efx_probe_channels(efx);
383 efx_remove_filters(efx);
385 #ifdef CONFIG_SFC_SRIOV
386 efx->type->vswitching_remove(efx);
389 efx_remove_port(efx);
396 static void efx_remove_all(struct efx_nic *efx)
399 efx_xdp_setup_prog(efx, NULL);
402 efx_remove_channels(efx);
403 efx_remove_filters(efx);
404 #ifdef CONFIG_SFC_SRIOV
405 efx->type->vswitching_remove(efx);
407 efx_remove_port(efx);
411 /**************************************************************************
413 * Interrupt moderation
415 **************************************************************************/
416 unsigned int efx_usecs_to_ticks(struct efx_nic *efx, unsigned int usecs)
420 if (usecs * 1000 < efx->timer_quantum_ns)
421 return 1; /* never round down to 0 */
422 return usecs * 1000 / efx->timer_quantum_ns;
425 unsigned int efx_ticks_to_usecs(struct efx_nic *efx, unsigned int ticks)
427 /* We must round up when converting ticks to microseconds
428 * because we round down when converting the other way.
430 return DIV_ROUND_UP(ticks * efx->timer_quantum_ns, 1000);
433 /* Set interrupt moderation parameters */
434 int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
435 unsigned int rx_usecs, bool rx_adaptive,
436 bool rx_may_override_tx)
438 struct efx_channel *channel;
439 unsigned int timer_max_us;
441 EFX_ASSERT_RESET_SERIALISED(efx);
443 timer_max_us = efx->timer_max_ns / 1000;
445 if (tx_usecs > timer_max_us || rx_usecs > timer_max_us)
448 if (tx_usecs != rx_usecs && efx->tx_channel_offset == 0 &&
449 !rx_may_override_tx) {
450 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
451 "RX and TX IRQ moderation must be equal\n");
455 efx->irq_rx_adaptive = rx_adaptive;
456 efx->irq_rx_moderation_us = rx_usecs;
457 efx_for_each_channel(channel, efx) {
458 if (efx_channel_has_rx_queue(channel))
459 channel->irq_moderation_us = rx_usecs;
460 else if (efx_channel_has_tx_queues(channel))
461 channel->irq_moderation_us = tx_usecs;
462 else if (efx_channel_is_xdp_tx(channel))
463 channel->irq_moderation_us = tx_usecs;
469 void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
470 unsigned int *rx_usecs, bool *rx_adaptive)
472 *rx_adaptive = efx->irq_rx_adaptive;
473 *rx_usecs = efx->irq_rx_moderation_us;
475 /* If channels are shared between RX and TX, so is IRQ
476 * moderation. Otherwise, IRQ moderation is the same for all
477 * TX channels and is not adaptive.
479 if (efx->tx_channel_offset == 0) {
480 *tx_usecs = *rx_usecs;
482 struct efx_channel *tx_channel;
484 tx_channel = efx->channel[efx->tx_channel_offset];
485 *tx_usecs = tx_channel->irq_moderation_us;
489 /**************************************************************************
493 *************************************************************************/
496 * Context: process, rtnl_lock() held.
498 static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
500 struct efx_nic *efx = netdev_priv(net_dev);
501 struct mii_ioctl_data *data = if_mii(ifr);
503 if (cmd == SIOCSHWTSTAMP)
504 return efx_ptp_set_ts_config(efx, ifr);
505 if (cmd == SIOCGHWTSTAMP)
506 return efx_ptp_get_ts_config(efx, ifr);
508 /* Convert phy_id from older PRTAD/DEVAD format */
509 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
510 (data->phy_id & 0xfc00) == 0x0400)
511 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
513 return mdio_mii_ioctl(&efx->mdio, data, cmd);
516 /**************************************************************************
518 * Kernel net device interface
520 *************************************************************************/
522 /* Context: process, rtnl_lock() held. */
523 int efx_net_open(struct net_device *net_dev)
525 struct efx_nic *efx = netdev_priv(net_dev);
528 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
529 raw_smp_processor_id());
531 rc = efx_check_disabled(efx);
534 if (efx->phy_mode & PHY_MODE_SPECIAL)
536 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
539 /* Notify the kernel of the link state polled during driver load,
540 * before the monitor starts running */
541 efx_link_status_changed(efx);
544 if (efx->state == STATE_DISABLED || efx->reset_pending)
545 netif_device_detach(efx->net_dev);
546 efx_selftest_async_start(efx);
550 /* Context: process, rtnl_lock() held.
551 * Note that the kernel will ignore our return code; this method
552 * should really be a void.
554 int efx_net_stop(struct net_device *net_dev)
556 struct efx_nic *efx = netdev_priv(net_dev);
558 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
559 raw_smp_processor_id());
561 /* Stop the device and flush all the channels */
567 static int efx_vlan_rx_add_vid(struct net_device *net_dev, __be16 proto, u16 vid)
569 struct efx_nic *efx = netdev_priv(net_dev);
571 if (efx->type->vlan_rx_add_vid)
572 return efx->type->vlan_rx_add_vid(efx, proto, vid);
577 static int efx_vlan_rx_kill_vid(struct net_device *net_dev, __be16 proto, u16 vid)
579 struct efx_nic *efx = netdev_priv(net_dev);
581 if (efx->type->vlan_rx_kill_vid)
582 return efx->type->vlan_rx_kill_vid(efx, proto, vid);
587 static const struct net_device_ops efx_netdev_ops = {
588 .ndo_open = efx_net_open,
589 .ndo_stop = efx_net_stop,
590 .ndo_get_stats64 = efx_net_stats,
591 .ndo_tx_timeout = efx_watchdog,
592 .ndo_start_xmit = efx_hard_start_xmit,
593 .ndo_validate_addr = eth_validate_addr,
594 .ndo_eth_ioctl = efx_ioctl,
595 .ndo_change_mtu = efx_change_mtu,
596 .ndo_set_mac_address = efx_set_mac_address,
597 .ndo_set_rx_mode = efx_set_rx_mode,
598 .ndo_set_features = efx_set_features,
599 .ndo_features_check = efx_features_check,
600 .ndo_vlan_rx_add_vid = efx_vlan_rx_add_vid,
601 .ndo_vlan_rx_kill_vid = efx_vlan_rx_kill_vid,
602 #ifdef CONFIG_SFC_SRIOV
603 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
604 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
605 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
606 .ndo_get_vf_config = efx_sriov_get_vf_config,
607 .ndo_set_vf_link_state = efx_sriov_set_vf_link_state,
609 .ndo_get_phys_port_id = efx_get_phys_port_id,
610 .ndo_get_phys_port_name = efx_get_phys_port_name,
611 .ndo_setup_tc = efx_setup_tc,
612 #ifdef CONFIG_RFS_ACCEL
613 .ndo_rx_flow_steer = efx_filter_rfs,
615 .ndo_xdp_xmit = efx_xdp_xmit,
619 static int efx_xdp_setup_prog(struct efx_nic *efx, struct bpf_prog *prog)
621 struct bpf_prog *old_prog;
623 if (efx->xdp_rxq_info_failed) {
624 netif_err(efx, drv, efx->net_dev,
625 "Unable to bind XDP program due to previous failure of rxq_info\n");
629 if (prog && efx->net_dev->mtu > efx_xdp_max_mtu(efx)) {
630 netif_err(efx, drv, efx->net_dev,
631 "Unable to configure XDP with MTU of %d (max: %d)\n",
632 efx->net_dev->mtu, efx_xdp_max_mtu(efx));
636 old_prog = rtnl_dereference(efx->xdp_prog);
637 rcu_assign_pointer(efx->xdp_prog, prog);
638 /* Release the reference that was originally passed by the caller. */
640 bpf_prog_put(old_prog);
645 /* Context: process, rtnl_lock() held. */
646 static int efx_xdp(struct net_device *dev, struct netdev_bpf *xdp)
648 struct efx_nic *efx = netdev_priv(dev);
650 switch (xdp->command) {
652 return efx_xdp_setup_prog(efx, xdp->prog);
658 static int efx_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **xdpfs,
661 struct efx_nic *efx = netdev_priv(dev);
663 if (!netif_running(dev))
666 return efx_xdp_tx_buffers(efx, n, xdpfs, flags & XDP_XMIT_FLUSH);
669 static void efx_update_name(struct efx_nic *efx)
671 strcpy(efx->name, efx->net_dev->name);
673 efx_set_channel_names(efx);
676 static int efx_netdev_event(struct notifier_block *this,
677 unsigned long event, void *ptr)
679 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
681 if ((net_dev->netdev_ops == &efx_netdev_ops) &&
682 event == NETDEV_CHANGENAME)
683 efx_update_name(netdev_priv(net_dev));
688 static struct notifier_block efx_netdev_notifier = {
689 .notifier_call = efx_netdev_event,
692 static ssize_t phy_type_show(struct device *dev,
693 struct device_attribute *attr, char *buf)
695 struct efx_nic *efx = dev_get_drvdata(dev);
696 return sprintf(buf, "%d\n", efx->phy_type);
698 static DEVICE_ATTR_RO(phy_type);
700 static int efx_register_netdev(struct efx_nic *efx)
702 struct net_device *net_dev = efx->net_dev;
703 struct efx_channel *channel;
706 net_dev->watchdog_timeo = 5 * HZ;
707 net_dev->irq = efx->pci_dev->irq;
708 net_dev->netdev_ops = &efx_netdev_ops;
709 if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0)
710 net_dev->priv_flags |= IFF_UNICAST_FLT;
711 net_dev->ethtool_ops = &efx_ethtool_ops;
712 net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
713 net_dev->min_mtu = EFX_MIN_MTU;
714 net_dev->max_mtu = EFX_MAX_MTU;
718 /* Enable resets to be scheduled and check whether any were
719 * already requested. If so, the NIC is probably hosed so we
722 efx->state = STATE_READY;
723 smp_mb(); /* ensure we change state before checking reset_pending */
724 if (efx->reset_pending) {
725 pci_err(efx->pci_dev, "aborting probe due to scheduled reset\n");
730 rc = dev_alloc_name(net_dev, net_dev->name);
733 efx_update_name(efx);
735 /* Always start with carrier off; PHY events will detect the link */
736 netif_carrier_off(net_dev);
738 rc = register_netdevice(net_dev);
742 efx_for_each_channel(channel, efx) {
743 struct efx_tx_queue *tx_queue;
744 efx_for_each_channel_tx_queue(tx_queue, channel)
745 efx_init_tx_queue_core_txq(tx_queue);
752 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
754 netif_err(efx, drv, efx->net_dev,
755 "failed to init net dev attributes\n");
756 goto fail_registered;
759 efx_init_mcdi_logging(efx);
766 unregister_netdevice(net_dev);
768 efx->state = STATE_UNINIT;
770 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
774 static void efx_unregister_netdev(struct efx_nic *efx)
779 BUG_ON(netdev_priv(efx->net_dev) != efx);
781 if (efx_dev_registered(efx)) {
782 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
783 efx_fini_mcdi_logging(efx);
784 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
785 unregister_netdev(efx->net_dev);
789 /**************************************************************************
791 * List of NICs we support
793 **************************************************************************/
795 /* PCI device ID table */
796 static const struct pci_device_id efx_pci_table[] = {
797 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
798 .driver_data = (unsigned long) &siena_a0_nic_type},
799 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
800 .driver_data = (unsigned long) &siena_a0_nic_type},
801 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0903), /* SFC9120 PF */
802 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
803 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1903), /* SFC9120 VF */
804 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
805 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0923), /* SFC9140 PF */
806 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
807 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1923), /* SFC9140 VF */
808 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
809 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0a03), /* SFC9220 PF */
810 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
811 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1a03), /* SFC9220 VF */
812 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
813 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0b03), /* SFC9250 PF */
814 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
815 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1b03), /* SFC9250 VF */
816 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
817 {0} /* end of list */
820 /**************************************************************************
824 **************************************************************************/
826 void efx_update_sw_stats(struct efx_nic *efx, u64 *stats)
828 u64 n_rx_nodesc_trunc = 0;
829 struct efx_channel *channel;
831 efx_for_each_channel(channel, efx)
832 n_rx_nodesc_trunc += channel->n_rx_nodesc_trunc;
833 stats[GENERIC_STAT_rx_nodesc_trunc] = n_rx_nodesc_trunc;
834 stats[GENERIC_STAT_rx_noskb_drops] = atomic_read(&efx->n_rx_noskb_drops);
837 /**************************************************************************
841 **************************************************************************/
843 /* Main body of final NIC shutdown code
844 * This is called only at module unload (or hotplug removal).
846 static void efx_pci_remove_main(struct efx_nic *efx)
848 /* Flush reset_work. It can no longer be scheduled since we
851 BUG_ON(efx->state == STATE_READY);
852 efx_flush_reset_workqueue(efx);
854 efx_disable_interrupts(efx);
855 efx_clear_interrupt_affinity(efx);
856 efx_nic_fini_interrupt(efx);
858 efx->type->fini(efx);
863 /* Final NIC shutdown
864 * This is called only at module unload (or hotplug removal). A PF can call
865 * this on its VFs to ensure they are unbound first.
867 static void efx_pci_remove(struct pci_dev *pci_dev)
871 efx = pci_get_drvdata(pci_dev);
875 /* Mark the NIC as fini, then stop the interface */
878 dev_close(efx->net_dev);
879 efx_disable_interrupts(efx);
880 efx->state = STATE_UNINIT;
883 if (efx->type->sriov_fini)
884 efx->type->sriov_fini(efx);
886 efx_unregister_netdev(efx);
890 efx_pci_remove_main(efx);
893 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
895 efx_fini_struct(efx);
896 free_netdev(efx->net_dev);
898 pci_disable_pcie_error_reporting(pci_dev);
901 /* NIC VPD information
902 * Called during probe to display the part number of the
905 static void efx_probe_vpd_strings(struct efx_nic *efx)
907 struct pci_dev *dev = efx->pci_dev;
908 unsigned int vpd_size, kw_len;
912 vpd_data = pci_vpd_alloc(dev, &vpd_size);
913 if (IS_ERR(vpd_data)) {
914 pci_warn(dev, "Unable to read VPD\n");
918 start = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
919 PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
921 pci_err(dev, "Part number not found or incomplete\n");
923 pci_info(dev, "Part Number : %.*s\n", kw_len, vpd_data + start);
925 start = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
926 PCI_VPD_RO_KEYWORD_SERIALNO, &kw_len);
928 pci_err(dev, "Serial number not found or incomplete\n");
930 efx->vpd_sn = kmemdup_nul(vpd_data + start, kw_len, GFP_KERNEL);
936 /* Main body of NIC initialisation
937 * This is called at module load (or hotplug insertion, theoretically).
939 static int efx_pci_probe_main(struct efx_nic *efx)
943 /* Do start-of-day initialisation */
944 rc = efx_probe_all(efx);
950 down_write(&efx->filter_sem);
951 rc = efx->type->init(efx);
952 up_write(&efx->filter_sem);
954 pci_err(efx->pci_dev, "failed to initialise NIC\n");
958 rc = efx_init_port(efx);
960 netif_err(efx, probe, efx->net_dev,
961 "failed to initialise port\n");
965 rc = efx_nic_init_interrupt(efx);
969 efx_set_interrupt_affinity(efx);
970 rc = efx_enable_interrupts(efx);
977 efx_clear_interrupt_affinity(efx);
978 efx_nic_fini_interrupt(efx);
982 efx->type->fini(efx);
990 static int efx_pci_probe_post_io(struct efx_nic *efx)
992 struct net_device *net_dev = efx->net_dev;
993 int rc = efx_pci_probe_main(efx);
998 if (efx->type->sriov_init) {
999 rc = efx->type->sriov_init(efx);
1001 pci_err(efx->pci_dev, "SR-IOV can't be enabled rc %d\n",
1005 /* Determine netdevice features */
1006 net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
1007 NETIF_F_TSO | NETIF_F_RXCSUM | NETIF_F_RXALL);
1008 if (efx->type->offload_features & (NETIF_F_IPV6_CSUM | NETIF_F_HW_CSUM))
1009 net_dev->features |= NETIF_F_TSO6;
1010 /* Check whether device supports TSO */
1011 if (!efx->type->tso_versions || !efx->type->tso_versions(efx))
1012 net_dev->features &= ~NETIF_F_ALL_TSO;
1013 /* Mask for features that also apply to VLAN devices */
1014 net_dev->vlan_features |= (NETIF_F_HW_CSUM | NETIF_F_SG |
1015 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
1018 net_dev->hw_features |= net_dev->features & ~efx->fixed_features;
1020 /* Disable receiving frames with bad FCS, by default. */
1021 net_dev->features &= ~NETIF_F_RXALL;
1023 /* Disable VLAN filtering by default. It may be enforced if
1024 * the feature is fixed (i.e. VLAN filters are required to
1025 * receive VLAN tagged packets due to vPort restrictions).
1027 net_dev->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1028 net_dev->features |= efx->fixed_features;
1030 rc = efx_register_netdev(efx);
1034 efx_pci_remove_main(efx);
1038 /* NIC initialisation
1040 * This is called at module load (or hotplug insertion,
1041 * theoretically). It sets up PCI mappings, resets the NIC,
1042 * sets up and registers the network devices with the kernel and hooks
1043 * the interrupt service routine. It does not prepare the device for
1044 * transmission; this is left to the first time one of the network
1045 * interfaces is brought up (i.e. efx_net_open).
1047 static int efx_pci_probe(struct pci_dev *pci_dev,
1048 const struct pci_device_id *entry)
1050 struct net_device *net_dev;
1051 struct efx_nic *efx;
1054 /* Allocate and initialise a struct net_device and struct efx_nic */
1055 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
1059 efx = netdev_priv(net_dev);
1060 efx->type = (const struct efx_nic_type *) entry->driver_data;
1061 efx->fixed_features |= NETIF_F_HIGHDMA;
1063 pci_set_drvdata(pci_dev, efx);
1064 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
1065 rc = efx_init_struct(efx, pci_dev, net_dev);
1069 pci_info(pci_dev, "Solarflare NIC detected\n");
1071 if (!efx->type->is_vf)
1072 efx_probe_vpd_strings(efx);
1074 /* Set up basic I/O (BAR mappings etc) */
1075 rc = efx_init_io(efx, efx->type->mem_bar(efx), efx->type->max_dma_mask,
1076 efx->type->mem_map_size(efx));
1080 rc = efx_pci_probe_post_io(efx);
1082 /* On failure, retry once immediately.
1083 * If we aborted probe due to a scheduled reset, dismiss it.
1085 efx->reset_pending = 0;
1086 rc = efx_pci_probe_post_io(efx);
1088 /* On another failure, retry once more
1089 * after a 50-305ms delay.
1093 get_random_bytes(&r, 1);
1094 msleep((unsigned int)r + 50);
1095 efx->reset_pending = 0;
1096 rc = efx_pci_probe_post_io(efx);
1102 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
1104 /* Try to create MTDs, but allow this to fail */
1106 rc = efx_mtd_probe(efx);
1108 if (rc && rc != -EPERM)
1109 netif_warn(efx, probe, efx->net_dev,
1110 "failed to create MTDs (%d)\n", rc);
1112 (void)pci_enable_pcie_error_reporting(pci_dev);
1114 if (efx->type->udp_tnl_push_ports)
1115 efx->type->udp_tnl_push_ports(efx);
1122 efx_fini_struct(efx);
1125 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
1126 free_netdev(net_dev);
1130 /* efx_pci_sriov_configure returns the actual number of Virtual Functions
1131 * enabled on success
1133 #ifdef CONFIG_SFC_SRIOV
1134 static int efx_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
1137 struct efx_nic *efx = pci_get_drvdata(dev);
1139 if (efx->type->sriov_configure) {
1140 rc = efx->type->sriov_configure(efx, num_vfs);
1150 static int efx_pm_freeze(struct device *dev)
1152 struct efx_nic *efx = dev_get_drvdata(dev);
1156 if (efx->state != STATE_DISABLED) {
1157 efx->state = STATE_UNINIT;
1159 efx_device_detach_sync(efx);
1162 efx_disable_interrupts(efx);
1170 static int efx_pm_thaw(struct device *dev)
1173 struct efx_nic *efx = dev_get_drvdata(dev);
1177 if (efx->state != STATE_DISABLED) {
1178 rc = efx_enable_interrupts(efx);
1182 mutex_lock(&efx->mac_lock);
1183 efx_mcdi_port_reconfigure(efx);
1184 mutex_unlock(&efx->mac_lock);
1188 efx_device_attach_if_not_resetting(efx);
1190 efx->state = STATE_READY;
1192 efx->type->resume_wol(efx);
1197 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
1198 efx_queue_reset_work(efx);
1208 static int efx_pm_poweroff(struct device *dev)
1210 struct pci_dev *pci_dev = to_pci_dev(dev);
1211 struct efx_nic *efx = pci_get_drvdata(pci_dev);
1213 efx->type->fini(efx);
1215 efx->reset_pending = 0;
1217 pci_save_state(pci_dev);
1218 return pci_set_power_state(pci_dev, PCI_D3hot);
1221 /* Used for both resume and restore */
1222 static int efx_pm_resume(struct device *dev)
1224 struct pci_dev *pci_dev = to_pci_dev(dev);
1225 struct efx_nic *efx = pci_get_drvdata(pci_dev);
1228 rc = pci_set_power_state(pci_dev, PCI_D0);
1231 pci_restore_state(pci_dev);
1232 rc = pci_enable_device(pci_dev);
1235 pci_set_master(efx->pci_dev);
1236 rc = efx->type->reset(efx, RESET_TYPE_ALL);
1239 down_write(&efx->filter_sem);
1240 rc = efx->type->init(efx);
1241 up_write(&efx->filter_sem);
1244 rc = efx_pm_thaw(dev);
1248 static int efx_pm_suspend(struct device *dev)
1253 rc = efx_pm_poweroff(dev);
1259 static const struct dev_pm_ops efx_pm_ops = {
1260 .suspend = efx_pm_suspend,
1261 .resume = efx_pm_resume,
1262 .freeze = efx_pm_freeze,
1263 .thaw = efx_pm_thaw,
1264 .poweroff = efx_pm_poweroff,
1265 .restore = efx_pm_resume,
1268 static struct pci_driver efx_pci_driver = {
1269 .name = KBUILD_MODNAME,
1270 .id_table = efx_pci_table,
1271 .probe = efx_pci_probe,
1272 .remove = efx_pci_remove,
1273 .driver.pm = &efx_pm_ops,
1274 .err_handler = &efx_err_handlers,
1275 #ifdef CONFIG_SFC_SRIOV
1276 .sriov_configure = efx_pci_sriov_configure,
1280 /**************************************************************************
1282 * Kernel module interface
1284 *************************************************************************/
1286 static int __init efx_init_module(void)
1290 printk(KERN_INFO "Solarflare NET driver\n");
1292 rc = register_netdevice_notifier(&efx_netdev_notifier);
1296 #ifdef CONFIG_SFC_SRIOV
1297 rc = efx_init_sriov();
1302 rc = efx_create_reset_workqueue();
1306 rc = pci_register_driver(&efx_pci_driver);
1310 rc = pci_register_driver(&ef100_pci_driver);
1317 pci_unregister_driver(&efx_pci_driver);
1319 efx_destroy_reset_workqueue();
1321 #ifdef CONFIG_SFC_SRIOV
1325 unregister_netdevice_notifier(&efx_netdev_notifier);
1330 static void __exit efx_exit_module(void)
1332 printk(KERN_INFO "Solarflare NET driver unloading\n");
1334 pci_unregister_driver(&ef100_pci_driver);
1335 pci_unregister_driver(&efx_pci_driver);
1336 efx_destroy_reset_workqueue();
1337 #ifdef CONFIG_SFC_SRIOV
1340 unregister_netdevice_notifier(&efx_netdev_notifier);
1344 module_init(efx_init_module);
1345 module_exit(efx_exit_module);
1347 MODULE_AUTHOR("Solarflare Communications and "
1348 "Michael Brown <mbrown@fensystems.co.uk>");
1349 MODULE_DESCRIPTION("Solarflare network driver");
1350 MODULE_LICENSE("GPL");
1351 MODULE_DEVICE_TABLE(pci, efx_pci_table);