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/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>
16 #include <linux/tcp.h>
18 #include <linux/ethtool.h>
19 #include <linux/topology.h>
20 #include <linux/gfp.h>
21 #include <linux/aer.h>
22 #include <linux/interrupt.h>
23 #include "net_driver.h"
25 #include <net/udp_tunnel.h>
27 #include "efx_common.h"
28 #include "efx_channels.h"
30 #include "rx_common.h"
31 #include "tx_common.h"
37 #include "mcdi_port_common.h"
38 #include "mcdi_pcol.h"
39 #include "workarounds.h"
41 /**************************************************************************
45 *************************************************************************/
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)");
51 module_param(rss_cpus, uint, 0444);
52 MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
55 * Use separate channels for TX and RX events
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.
60 * This is only used in MSI-X interrupt mode
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");
67 /* Initial interrupt moderation settings. They can be modified after
68 * module load with ethtool.
70 * The default for RX should strike a balance between increasing the
71 * round-trip latency and reducing overhead.
73 static unsigned int rx_irq_mod_usec = 60;
75 /* Initial interrupt moderation settings. They can be modified after
76 * module load with ethtool.
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.
84 static unsigned int tx_irq_mod_usec = 150;
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");
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");
97 /**************************************************************************
99 * Utility functions and prototypes
101 *************************************************************************/
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,
109 /**************************************************************************
113 **************************************************************************/
115 static void efx_fini_port(struct efx_nic *efx);
117 static int efx_probe_port(struct efx_nic *efx)
121 netif_dbg(efx, probe, efx->net_dev, "create port\n");
124 efx->phy_mode = PHY_MODE_SPECIAL;
126 /* Connect up MAC/PHY operations table */
127 rc = efx->type->probe_port(efx);
131 /* Initialise MAC address to permanent address */
132 eth_hw_addr_set(efx->net_dev, efx->net_dev->perm_addr);
137 static int efx_init_port(struct efx_nic *efx)
141 netif_dbg(efx, drv, efx->net_dev, "init port\n");
143 mutex_lock(&efx->mac_lock);
145 efx->port_initialized = true;
147 /* Ensure the PHY advertises the correct flow control settings */
148 rc = efx_mcdi_port_reconfigure(efx);
149 if (rc && rc != -EPERM)
152 mutex_unlock(&efx->mac_lock);
156 mutex_unlock(&efx->mac_lock);
160 static void efx_fini_port(struct efx_nic *efx)
162 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
164 if (!efx->port_initialized)
167 efx->port_initialized = false;
169 efx->link_state.up = false;
170 efx_link_status_changed(efx);
173 static void efx_remove_port(struct efx_nic *efx)
175 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
177 efx->type->remove_port(efx);
180 /**************************************************************************
184 **************************************************************************/
186 static LIST_HEAD(efx_primary_list);
187 static LIST_HEAD(efx_unassociated_list);
189 static bool efx_same_controller(struct efx_nic *left, struct efx_nic *right)
191 return left->type == right->type &&
192 left->vpd_sn && right->vpd_sn &&
193 !strcmp(left->vpd_sn, right->vpd_sn);
196 static void efx_associate(struct efx_nic *efx)
198 struct efx_nic *other, *next;
200 if (efx->primary == efx) {
201 /* Adding primary function; look for secondaries */
203 netif_dbg(efx, probe, efx->net_dev, "adding to primary list\n");
204 list_add_tail(&efx->node, &efx_primary_list);
206 list_for_each_entry_safe(other, next, &efx_unassociated_list,
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),
214 list_add_tail(&other->node,
215 &efx->secondary_list);
216 other->primary = efx;
220 /* Adding secondary function; look for primary */
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;
235 netif_dbg(efx, probe, efx->net_dev,
236 "adding to unassociated list\n");
237 list_add_tail(&efx->node, &efx_unassociated_list);
241 static void efx_dissociate(struct efx_nic *efx)
243 struct efx_nic *other, *next;
245 list_del(&efx->node);
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;
257 static int efx_probe_nic(struct efx_nic *efx)
261 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
263 /* Carry out hardware-type specific initialisation */
264 rc = efx->type->probe(efx);
269 if (!efx->max_channels || !efx->max_tx_channels) {
270 netif_err(efx, drv, efx->net_dev,
271 "Insufficient resources to allocate"
277 /* Determine the number of channels and queues by trying
278 * to hook in MSI-X interrupts.
280 rc = efx_probe_interrupts(efx);
284 rc = efx_set_channels(efx);
288 /* dimension_resources can fail with EAGAIN */
289 rc = efx->type->dimension_resources(efx);
290 if (rc != 0 && rc != -EAGAIN)
294 /* try again with new max_channels */
295 efx_remove_interrupts(efx);
297 } while (rc == -EAGAIN);
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);
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,
312 efx_remove_interrupts(efx);
314 efx->type->remove(efx);
318 static void efx_remove_nic(struct efx_nic *efx)
320 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
322 efx_remove_interrupts(efx);
323 efx->type->remove(efx);
326 /**************************************************************************
328 * NIC startup/shutdown
330 *************************************************************************/
332 static int efx_probe_all(struct efx_nic *efx)
336 rc = efx_probe_nic(efx);
338 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
342 rc = efx_probe_port(efx);
344 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
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))) {
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);
362 rc = efx_probe_filters(efx);
364 netif_err(efx, probe, efx->net_dev,
365 "failed to create filter tables\n");
369 rc = efx_probe_channels(efx);
373 efx->state = STATE_NET_DOWN;
378 efx_remove_filters(efx);
380 #ifdef CONFIG_SFC_SRIOV
381 efx->type->vswitching_remove(efx);
384 efx_remove_port(efx);
391 static void efx_remove_all(struct efx_nic *efx)
394 efx_xdp_setup_prog(efx, NULL);
397 efx_remove_channels(efx);
398 efx_remove_filters(efx);
399 #ifdef CONFIG_SFC_SRIOV
400 efx->type->vswitching_remove(efx);
402 efx_remove_port(efx);
406 /**************************************************************************
408 * Interrupt moderation
410 **************************************************************************/
411 unsigned int efx_usecs_to_ticks(struct efx_nic *efx, unsigned int usecs)
415 if (usecs * 1000 < efx->timer_quantum_ns)
416 return 1; /* never round down to 0 */
417 return usecs * 1000 / efx->timer_quantum_ns;
420 unsigned int efx_ticks_to_usecs(struct efx_nic *efx, unsigned int ticks)
422 /* We must round up when converting ticks to microseconds
423 * because we round down when converting the other way.
425 return DIV_ROUND_UP(ticks * efx->timer_quantum_ns, 1000);
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)
433 struct efx_channel *channel;
434 unsigned int timer_max_us;
436 EFX_ASSERT_RESET_SERIALISED(efx);
438 timer_max_us = efx->timer_max_ns / 1000;
440 if (tx_usecs > timer_max_us || rx_usecs > timer_max_us)
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");
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;
464 void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
465 unsigned int *rx_usecs, bool *rx_adaptive)
467 *rx_adaptive = efx->irq_rx_adaptive;
468 *rx_usecs = efx->irq_rx_moderation_us;
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.
474 if (efx->tx_channel_offset == 0) {
475 *tx_usecs = *rx_usecs;
477 struct efx_channel *tx_channel;
479 tx_channel = efx->channel[efx->tx_channel_offset];
480 *tx_usecs = tx_channel->irq_moderation_us;
484 /**************************************************************************
488 *************************************************************************/
491 * Context: process, rtnl_lock() held.
493 static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
495 struct efx_nic *efx = efx_netdev_priv(net_dev);
496 struct mii_ioctl_data *data = if_mii(ifr);
498 if (cmd == SIOCSHWTSTAMP)
499 return efx_ptp_set_ts_config(efx, ifr);
500 if (cmd == SIOCGHWTSTAMP)
501 return efx_ptp_get_ts_config(efx, ifr);
503 /* Convert phy_id from older PRTAD/DEVAD format */
504 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
505 (data->phy_id & 0xfc00) == 0x0400)
506 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
508 return mdio_mii_ioctl(&efx->mdio, data, cmd);
511 /**************************************************************************
513 * Kernel net device interface
515 *************************************************************************/
517 /* Context: process, rtnl_lock() held. */
518 int efx_net_open(struct net_device *net_dev)
520 struct efx_nic *efx = efx_netdev_priv(net_dev);
523 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
524 raw_smp_processor_id());
526 rc = efx_check_disabled(efx);
529 if (efx->phy_mode & PHY_MODE_SPECIAL)
531 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
534 /* Notify the kernel of the link state polled during driver load,
535 * before the monitor starts running */
536 efx_link_status_changed(efx);
539 if (efx->state == STATE_DISABLED || efx->reset_pending)
540 netif_device_detach(efx->net_dev);
542 efx->state = STATE_NET_UP;
544 efx_selftest_async_start(efx);
548 /* Context: process, rtnl_lock() held.
549 * Note that the kernel will ignore our return code; this method
550 * should really be a void.
552 int efx_net_stop(struct net_device *net_dev)
554 struct efx_nic *efx = efx_netdev_priv(net_dev);
556 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
557 raw_smp_processor_id());
559 /* Stop the device and flush all the channels */
565 static int efx_vlan_rx_add_vid(struct net_device *net_dev, __be16 proto, u16 vid)
567 struct efx_nic *efx = efx_netdev_priv(net_dev);
569 if (efx->type->vlan_rx_add_vid)
570 return efx->type->vlan_rx_add_vid(efx, proto, vid);
575 static int efx_vlan_rx_kill_vid(struct net_device *net_dev, __be16 proto, u16 vid)
577 struct efx_nic *efx = efx_netdev_priv(net_dev);
579 if (efx->type->vlan_rx_kill_vid)
580 return efx->type->vlan_rx_kill_vid(efx, proto, vid);
585 static const struct net_device_ops efx_netdev_ops = {
586 .ndo_open = efx_net_open,
587 .ndo_stop = efx_net_stop,
588 .ndo_get_stats64 = efx_net_stats,
589 .ndo_tx_timeout = efx_watchdog,
590 .ndo_start_xmit = efx_hard_start_xmit,
591 .ndo_validate_addr = eth_validate_addr,
592 .ndo_eth_ioctl = efx_ioctl,
593 .ndo_change_mtu = efx_change_mtu,
594 .ndo_set_mac_address = efx_set_mac_address,
595 .ndo_set_rx_mode = efx_set_rx_mode,
596 .ndo_set_features = efx_set_features,
597 .ndo_features_check = efx_features_check,
598 .ndo_vlan_rx_add_vid = efx_vlan_rx_add_vid,
599 .ndo_vlan_rx_kill_vid = efx_vlan_rx_kill_vid,
600 #ifdef CONFIG_SFC_SRIOV
601 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
602 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
603 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
604 .ndo_get_vf_config = efx_sriov_get_vf_config,
605 .ndo_set_vf_link_state = efx_sriov_set_vf_link_state,
607 .ndo_get_phys_port_id = efx_get_phys_port_id,
608 .ndo_get_phys_port_name = efx_get_phys_port_name,
609 .ndo_setup_tc = efx_setup_tc,
610 #ifdef CONFIG_RFS_ACCEL
611 .ndo_rx_flow_steer = efx_filter_rfs,
613 .ndo_xdp_xmit = efx_xdp_xmit,
617 static int efx_xdp_setup_prog(struct efx_nic *efx, struct bpf_prog *prog)
619 struct bpf_prog *old_prog;
621 if (efx->xdp_rxq_info_failed) {
622 netif_err(efx, drv, efx->net_dev,
623 "Unable to bind XDP program due to previous failure of rxq_info\n");
627 if (prog && efx->net_dev->mtu > efx_xdp_max_mtu(efx)) {
628 netif_err(efx, drv, efx->net_dev,
629 "Unable to configure XDP with MTU of %d (max: %d)\n",
630 efx->net_dev->mtu, efx_xdp_max_mtu(efx));
634 old_prog = rtnl_dereference(efx->xdp_prog);
635 rcu_assign_pointer(efx->xdp_prog, prog);
636 /* Release the reference that was originally passed by the caller. */
638 bpf_prog_put(old_prog);
643 /* Context: process, rtnl_lock() held. */
644 static int efx_xdp(struct net_device *dev, struct netdev_bpf *xdp)
646 struct efx_nic *efx = efx_netdev_priv(dev);
648 switch (xdp->command) {
650 return efx_xdp_setup_prog(efx, xdp->prog);
656 static int efx_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **xdpfs,
659 struct efx_nic *efx = efx_netdev_priv(dev);
661 if (!netif_running(dev))
664 return efx_xdp_tx_buffers(efx, n, xdpfs, flags & XDP_XMIT_FLUSH);
667 static void efx_update_name(struct efx_nic *efx)
669 strcpy(efx->name, efx->net_dev->name);
671 efx_set_channel_names(efx);
674 static int efx_netdev_event(struct notifier_block *this,
675 unsigned long event, void *ptr)
677 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
679 if ((net_dev->netdev_ops == &efx_netdev_ops) &&
680 event == NETDEV_CHANGENAME)
681 efx_update_name(efx_netdev_priv(net_dev));
686 static struct notifier_block efx_netdev_notifier = {
687 .notifier_call = efx_netdev_event,
690 static ssize_t phy_type_show(struct device *dev,
691 struct device_attribute *attr, char *buf)
693 struct efx_nic *efx = dev_get_drvdata(dev);
694 return sprintf(buf, "%d\n", efx->phy_type);
696 static DEVICE_ATTR_RO(phy_type);
698 static int efx_register_netdev(struct efx_nic *efx)
700 struct net_device *net_dev = efx->net_dev;
701 struct efx_channel *channel;
704 net_dev->watchdog_timeo = 5 * HZ;
705 net_dev->irq = efx->pci_dev->irq;
706 net_dev->netdev_ops = &efx_netdev_ops;
707 if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0)
708 net_dev->priv_flags |= IFF_UNICAST_FLT;
709 net_dev->ethtool_ops = &efx_ethtool_ops;
710 netif_set_tso_max_segs(net_dev, EFX_TSO_MAX_SEGS);
711 net_dev->min_mtu = EFX_MIN_MTU;
712 net_dev->max_mtu = EFX_MAX_MTU;
716 /* Enable resets to be scheduled and check whether any were
717 * already requested. If so, the NIC is probably hosed so we
720 if (efx->reset_pending) {
721 pci_err(efx->pci_dev, "aborting probe due to scheduled reset\n");
726 rc = dev_alloc_name(net_dev, net_dev->name);
729 efx_update_name(efx);
731 /* Always start with carrier off; PHY events will detect the link */
732 netif_carrier_off(net_dev);
734 rc = register_netdevice(net_dev);
738 efx_for_each_channel(channel, efx) {
739 struct efx_tx_queue *tx_queue;
740 efx_for_each_channel_tx_queue(tx_queue, channel)
741 efx_init_tx_queue_core_txq(tx_queue);
746 efx->state = STATE_NET_DOWN;
750 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
752 netif_err(efx, drv, efx->net_dev,
753 "failed to init net dev attributes\n");
754 goto fail_registered;
757 efx_init_mcdi_logging(efx);
764 unregister_netdevice(net_dev);
766 efx->state = STATE_UNINIT;
768 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
772 static void efx_unregister_netdev(struct efx_nic *efx)
777 if (WARN_ON(efx_netdev_priv(efx->net_dev) != efx))
780 if (efx_dev_registered(efx)) {
781 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
782 efx_fini_mcdi_logging(efx);
783 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
784 unregister_netdev(efx->net_dev);
788 /**************************************************************************
790 * List of NICs we support
792 **************************************************************************/
794 /* PCI device ID table */
795 static const struct pci_device_id efx_pci_table[] = {
796 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0903), /* SFC9120 PF */
797 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
798 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1903), /* SFC9120 VF */
799 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
800 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0923), /* SFC9140 PF */
801 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
802 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1923), /* SFC9140 VF */
803 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
804 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0a03), /* SFC9220 PF */
805 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
806 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1a03), /* SFC9220 VF */
807 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
808 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0b03), /* SFC9250 PF */
809 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
810 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1b03), /* SFC9250 VF */
811 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
812 {0} /* end of list */
815 /**************************************************************************
819 **************************************************************************/
821 void efx_update_sw_stats(struct efx_nic *efx, u64 *stats)
823 u64 n_rx_nodesc_trunc = 0;
824 struct efx_channel *channel;
826 efx_for_each_channel(channel, efx)
827 n_rx_nodesc_trunc += channel->n_rx_nodesc_trunc;
828 stats[GENERIC_STAT_rx_nodesc_trunc] = n_rx_nodesc_trunc;
829 stats[GENERIC_STAT_rx_noskb_drops] = atomic_read(&efx->n_rx_noskb_drops);
832 /**************************************************************************
836 **************************************************************************/
838 /* Main body of final NIC shutdown code
839 * This is called only at module unload (or hotplug removal).
841 static void efx_pci_remove_main(struct efx_nic *efx)
843 /* Flush reset_work. It can no longer be scheduled since we
846 WARN_ON(efx_net_active(efx->state));
847 efx_flush_reset_workqueue(efx);
849 efx_disable_interrupts(efx);
850 efx_clear_interrupt_affinity(efx);
851 efx_nic_fini_interrupt(efx);
853 efx->type->fini(efx);
858 /* Final NIC shutdown
859 * This is called only at module unload (or hotplug removal). A PF can call
860 * this on its VFs to ensure they are unbound first.
862 static void efx_pci_remove(struct pci_dev *pci_dev)
864 struct efx_probe_data *probe_data;
867 efx = pci_get_drvdata(pci_dev);
871 /* Mark the NIC as fini, then stop the interface */
874 dev_close(efx->net_dev);
875 efx_disable_interrupts(efx);
876 efx->state = STATE_UNINIT;
879 if (efx->type->sriov_fini)
880 efx->type->sriov_fini(efx);
882 efx_unregister_netdev(efx);
886 efx_pci_remove_main(efx);
889 pci_dbg(efx->pci_dev, "shutdown successful\n");
891 efx_fini_struct(efx);
892 free_netdev(efx->net_dev);
893 probe_data = container_of(efx, struct efx_probe_data, efx);
896 pci_disable_pcie_error_reporting(pci_dev);
899 /* NIC VPD information
900 * Called during probe to display the part number of the
903 static void efx_probe_vpd_strings(struct efx_nic *efx)
905 struct pci_dev *dev = efx->pci_dev;
906 unsigned int vpd_size, kw_len;
910 vpd_data = pci_vpd_alloc(dev, &vpd_size);
911 if (IS_ERR(vpd_data)) {
912 pci_warn(dev, "Unable to read VPD\n");
916 start = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
917 PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
919 pci_err(dev, "Part number not found or incomplete\n");
921 pci_info(dev, "Part Number : %.*s\n", kw_len, vpd_data + start);
923 start = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
924 PCI_VPD_RO_KEYWORD_SERIALNO, &kw_len);
926 pci_err(dev, "Serial number not found or incomplete\n");
928 efx->vpd_sn = kmemdup_nul(vpd_data + start, kw_len, GFP_KERNEL);
934 /* Main body of NIC initialisation
935 * This is called at module load (or hotplug insertion, theoretically).
937 static int efx_pci_probe_main(struct efx_nic *efx)
941 /* Do start-of-day initialisation */
942 rc = efx_probe_all(efx);
948 down_write(&efx->filter_sem);
949 rc = efx->type->init(efx);
950 up_write(&efx->filter_sem);
952 pci_err(efx->pci_dev, "failed to initialise NIC\n");
956 rc = efx_init_port(efx);
958 netif_err(efx, probe, efx->net_dev,
959 "failed to initialise port\n");
963 rc = efx_nic_init_interrupt(efx);
967 efx_set_interrupt_affinity(efx);
968 rc = efx_enable_interrupts(efx);
975 efx_clear_interrupt_affinity(efx);
976 efx_nic_fini_interrupt(efx);
980 efx->type->fini(efx);
988 static int efx_pci_probe_post_io(struct efx_nic *efx)
990 struct net_device *net_dev = efx->net_dev;
991 int rc = efx_pci_probe_main(efx);
996 if (efx->type->sriov_init) {
997 rc = efx->type->sriov_init(efx);
999 pci_err(efx->pci_dev, "SR-IOV can't be enabled rc %d\n",
1003 /* Determine netdevice features */
1004 net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
1005 NETIF_F_TSO | NETIF_F_RXCSUM | NETIF_F_RXALL);
1006 if (efx->type->offload_features & (NETIF_F_IPV6_CSUM | NETIF_F_HW_CSUM))
1007 net_dev->features |= NETIF_F_TSO6;
1008 /* Check whether device supports TSO */
1009 if (!efx->type->tso_versions || !efx->type->tso_versions(efx))
1010 net_dev->features &= ~NETIF_F_ALL_TSO;
1011 /* Mask for features that also apply to VLAN devices */
1012 net_dev->vlan_features |= (NETIF_F_HW_CSUM | NETIF_F_SG |
1013 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
1016 net_dev->hw_features |= net_dev->features & ~efx->fixed_features;
1018 /* Disable receiving frames with bad FCS, by default. */
1019 net_dev->features &= ~NETIF_F_RXALL;
1021 /* Disable VLAN filtering by default. It may be enforced if
1022 * the feature is fixed (i.e. VLAN filters are required to
1023 * receive VLAN tagged packets due to vPort restrictions).
1025 net_dev->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1026 net_dev->features |= efx->fixed_features;
1028 rc = efx_register_netdev(efx);
1032 efx_pci_remove_main(efx);
1036 /* NIC initialisation
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).
1045 static int efx_pci_probe(struct pci_dev *pci_dev,
1046 const struct pci_device_id *entry)
1048 struct efx_probe_data *probe_data, **probe_ptr;
1049 struct net_device *net_dev;
1050 struct efx_nic *efx;
1053 /* Allocate probe data and struct efx_nic */
1054 probe_data = kzalloc(sizeof(*probe_data), GFP_KERNEL);
1057 probe_data->pci_dev = pci_dev;
1058 efx = &probe_data->efx;
1060 /* Allocate and initialise a struct net_device */
1061 net_dev = alloc_etherdev_mq(sizeof(probe_data), EFX_MAX_CORE_TX_QUEUES);
1064 probe_ptr = netdev_priv(net_dev);
1065 *probe_ptr = probe_data;
1066 efx->net_dev = net_dev;
1067 efx->type = (const struct efx_nic_type *) entry->driver_data;
1068 efx->fixed_features |= NETIF_F_HIGHDMA;
1070 pci_set_drvdata(pci_dev, efx);
1071 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
1072 rc = efx_init_struct(efx, pci_dev);
1075 efx->mdio.dev = net_dev;
1077 pci_info(pci_dev, "Solarflare NIC detected\n");
1079 if (!efx->type->is_vf)
1080 efx_probe_vpd_strings(efx);
1082 /* Set up basic I/O (BAR mappings etc) */
1083 rc = efx_init_io(efx, efx->type->mem_bar(efx), efx->type->max_dma_mask,
1084 efx->type->mem_map_size(efx));
1088 rc = efx_pci_probe_post_io(efx);
1090 /* On failure, retry once immediately.
1091 * If we aborted probe due to a scheduled reset, dismiss it.
1093 efx->reset_pending = 0;
1094 rc = efx_pci_probe_post_io(efx);
1096 /* On another failure, retry once more
1097 * after a 50-305ms delay.
1101 get_random_bytes(&r, 1);
1102 msleep((unsigned int)r + 50);
1103 efx->reset_pending = 0;
1104 rc = efx_pci_probe_post_io(efx);
1110 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
1112 /* Try to create MTDs, but allow this to fail */
1114 rc = efx_mtd_probe(efx);
1116 if (rc && rc != -EPERM)
1117 netif_warn(efx, probe, efx->net_dev,
1118 "failed to create MTDs (%d)\n", rc);
1120 (void)pci_enable_pcie_error_reporting(pci_dev);
1122 if (efx->type->udp_tnl_push_ports)
1123 efx->type->udp_tnl_push_ports(efx);
1130 efx_fini_struct(efx);
1133 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
1134 free_netdev(net_dev);
1138 /* efx_pci_sriov_configure returns the actual number of Virtual Functions
1139 * enabled on success
1141 #ifdef CONFIG_SFC_SRIOV
1142 static int efx_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
1145 struct efx_nic *efx = pci_get_drvdata(dev);
1147 if (efx->type->sriov_configure) {
1148 rc = efx->type->sriov_configure(efx, num_vfs);
1158 static int efx_pm_freeze(struct device *dev)
1160 struct efx_nic *efx = dev_get_drvdata(dev);
1164 if (efx_net_active(efx->state)) {
1165 efx_device_detach_sync(efx);
1168 efx_disable_interrupts(efx);
1170 efx->state = efx_freeze(efx->state);
1178 static int efx_pm_thaw(struct device *dev)
1181 struct efx_nic *efx = dev_get_drvdata(dev);
1185 if (efx_frozen(efx->state)) {
1186 rc = efx_enable_interrupts(efx);
1190 mutex_lock(&efx->mac_lock);
1191 efx_mcdi_port_reconfigure(efx);
1192 mutex_unlock(&efx->mac_lock);
1196 efx_device_attach_if_not_resetting(efx);
1198 efx->state = efx_thaw(efx->state);
1200 efx->type->resume_wol(efx);
1205 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
1206 efx_queue_reset_work(efx);
1216 static int efx_pm_poweroff(struct device *dev)
1218 struct pci_dev *pci_dev = to_pci_dev(dev);
1219 struct efx_nic *efx = pci_get_drvdata(pci_dev);
1221 efx->type->fini(efx);
1223 efx->reset_pending = 0;
1225 pci_save_state(pci_dev);
1226 return pci_set_power_state(pci_dev, PCI_D3hot);
1229 /* Used for both resume and restore */
1230 static int efx_pm_resume(struct device *dev)
1232 struct pci_dev *pci_dev = to_pci_dev(dev);
1233 struct efx_nic *efx = pci_get_drvdata(pci_dev);
1236 rc = pci_set_power_state(pci_dev, PCI_D0);
1239 pci_restore_state(pci_dev);
1240 rc = pci_enable_device(pci_dev);
1243 pci_set_master(efx->pci_dev);
1244 rc = efx->type->reset(efx, RESET_TYPE_ALL);
1247 down_write(&efx->filter_sem);
1248 rc = efx->type->init(efx);
1249 up_write(&efx->filter_sem);
1252 rc = efx_pm_thaw(dev);
1256 static int efx_pm_suspend(struct device *dev)
1261 rc = efx_pm_poweroff(dev);
1267 static const struct dev_pm_ops efx_pm_ops = {
1268 .suspend = efx_pm_suspend,
1269 .resume = efx_pm_resume,
1270 .freeze = efx_pm_freeze,
1271 .thaw = efx_pm_thaw,
1272 .poweroff = efx_pm_poweroff,
1273 .restore = efx_pm_resume,
1276 static struct pci_driver efx_pci_driver = {
1277 .name = KBUILD_MODNAME,
1278 .id_table = efx_pci_table,
1279 .probe = efx_pci_probe,
1280 .remove = efx_pci_remove,
1281 .driver.pm = &efx_pm_ops,
1282 .err_handler = &efx_err_handlers,
1283 #ifdef CONFIG_SFC_SRIOV
1284 .sriov_configure = efx_pci_sriov_configure,
1288 /**************************************************************************
1290 * Kernel module interface
1292 *************************************************************************/
1294 static int __init efx_init_module(void)
1298 printk(KERN_INFO "Solarflare NET driver\n");
1300 rc = register_netdevice_notifier(&efx_netdev_notifier);
1304 rc = efx_create_reset_workqueue();
1308 rc = pci_register_driver(&efx_pci_driver);
1312 rc = pci_register_driver(&ef100_pci_driver);
1319 pci_unregister_driver(&efx_pci_driver);
1321 efx_destroy_reset_workqueue();
1323 unregister_netdevice_notifier(&efx_netdev_notifier);
1328 static void __exit efx_exit_module(void)
1330 printk(KERN_INFO "Solarflare NET driver unloading\n");
1332 pci_unregister_driver(&ef100_pci_driver);
1333 pci_unregister_driver(&efx_pci_driver);
1334 efx_destroy_reset_workqueue();
1335 unregister_netdevice_notifier(&efx_netdev_notifier);
1339 module_init(efx_init_module);
1340 module_exit(efx_exit_module);
1342 MODULE_AUTHOR("Solarflare Communications and "
1344 MODULE_DESCRIPTION("Solarflare network driver");
1345 MODULE_LICENSE("GPL");
1346 MODULE_DEVICE_TABLE(pci, efx_pci_table);