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"
28 #include "rx_common.h"
29 #include "tx_common.h"
36 #include "mcdi_pcol.h"
37 #include "workarounds.h"
39 /**************************************************************************
43 **************************************************************************
46 /* UDP tunnel type names */
47 static const char *const efx_udp_tunnel_type_names[] = {
48 [TUNNEL_ENCAP_UDP_PORT_ENTRY_VXLAN] = "vxlan",
49 [TUNNEL_ENCAP_UDP_PORT_ENTRY_GENEVE] = "geneve",
52 void efx_get_udp_tunnel_type_name(u16 type, char *buf, size_t buflen)
54 if (type < ARRAY_SIZE(efx_udp_tunnel_type_names) &&
55 efx_udp_tunnel_type_names[type] != NULL)
56 snprintf(buf, buflen, "%s", efx_udp_tunnel_type_names[type]);
58 snprintf(buf, buflen, "type %d", type);
61 /**************************************************************************
65 *************************************************************************/
68 * Use separate channels for TX and RX events
70 * Set this to 1 to use separate channels for TX and RX. It allows us
71 * to control interrupt affinity separately for TX and RX.
73 * This is only used in MSI-X interrupt mode
75 bool efx_separate_tx_channels;
76 module_param(efx_separate_tx_channels, bool, 0444);
77 MODULE_PARM_DESC(efx_separate_tx_channels,
78 "Use separate channels for TX and RX");
80 /* Initial interrupt moderation settings. They can be modified after
81 * module load with ethtool.
83 * The default for RX should strike a balance between increasing the
84 * round-trip latency and reducing overhead.
86 static unsigned int rx_irq_mod_usec = 60;
88 /* Initial interrupt moderation settings. They can be modified after
89 * module load with ethtool.
91 * This default is chosen to ensure that a 10G link does not go idle
92 * while a TX queue is stopped after it has become full. A queue is
93 * restarted when it drops below half full. The time this takes (assuming
94 * worst case 3 descriptors per packet and 1024 descriptors) is
95 * 512 / 3 * 1.2 = 205 usec.
97 static unsigned int tx_irq_mod_usec = 150;
99 static bool phy_flash_cfg;
100 module_param(phy_flash_cfg, bool, 0644);
101 MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
103 static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
104 NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
105 NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
106 NETIF_MSG_TX_ERR | NETIF_MSG_HW);
107 module_param(debug, uint, 0);
108 MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
110 /**************************************************************************
112 * Utility functions and prototypes
114 *************************************************************************/
116 static const struct efx_channel_type efx_default_channel_type;
117 static void efx_remove_port(struct efx_nic *efx);
118 static int efx_xdp_setup_prog(struct efx_nic *efx, struct bpf_prog *prog);
119 static int efx_xdp(struct net_device *dev, struct netdev_bpf *xdp);
120 static int efx_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **xdpfs,
123 #define EFX_ASSERT_RESET_SERIALISED(efx) \
125 if ((efx->state == STATE_READY) || \
126 (efx->state == STATE_RECOVERY) || \
127 (efx->state == STATE_DISABLED)) \
131 /**************************************************************************
135 **************************************************************************/
137 /* Equivalent to efx_link_set_advertising with all-zeroes, except does not
138 * force the Autoneg bit on.
140 void efx_link_clear_advertising(struct efx_nic *efx)
142 bitmap_zero(efx->link_advertising, __ETHTOOL_LINK_MODE_MASK_NBITS);
143 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
146 void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
148 efx->wanted_fc = wanted_fc;
149 if (efx->link_advertising[0]) {
150 if (wanted_fc & EFX_FC_RX)
151 efx->link_advertising[0] |= (ADVERTISED_Pause |
152 ADVERTISED_Asym_Pause);
154 efx->link_advertising[0] &= ~(ADVERTISED_Pause |
155 ADVERTISED_Asym_Pause);
156 if (wanted_fc & EFX_FC_TX)
157 efx->link_advertising[0] ^= ADVERTISED_Asym_Pause;
161 static void efx_fini_port(struct efx_nic *efx);
163 static int efx_probe_port(struct efx_nic *efx)
167 netif_dbg(efx, probe, efx->net_dev, "create port\n");
170 efx->phy_mode = PHY_MODE_SPECIAL;
172 /* Connect up MAC/PHY operations table */
173 rc = efx->type->probe_port(efx);
177 /* Initialise MAC address to permanent address */
178 ether_addr_copy(efx->net_dev->dev_addr, efx->net_dev->perm_addr);
183 static int efx_init_port(struct efx_nic *efx)
187 netif_dbg(efx, drv, efx->net_dev, "init port\n");
189 mutex_lock(&efx->mac_lock);
191 rc = efx->phy_op->init(efx);
195 efx->port_initialized = true;
197 /* Reconfigure the MAC before creating dma queues (required for
198 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
199 efx_mac_reconfigure(efx);
201 /* Ensure the PHY advertises the correct flow control settings */
202 rc = efx->phy_op->reconfigure(efx);
203 if (rc && rc != -EPERM)
206 mutex_unlock(&efx->mac_lock);
210 efx->phy_op->fini(efx);
212 mutex_unlock(&efx->mac_lock);
216 static void efx_fini_port(struct efx_nic *efx)
218 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
220 if (!efx->port_initialized)
223 efx->phy_op->fini(efx);
224 efx->port_initialized = false;
226 efx->link_state.up = false;
227 efx_link_status_changed(efx);
230 static void efx_remove_port(struct efx_nic *efx)
232 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
234 efx->type->remove_port(efx);
237 /**************************************************************************
241 **************************************************************************/
243 static LIST_HEAD(efx_primary_list);
244 static LIST_HEAD(efx_unassociated_list);
246 static bool efx_same_controller(struct efx_nic *left, struct efx_nic *right)
248 return left->type == right->type &&
249 left->vpd_sn && right->vpd_sn &&
250 !strcmp(left->vpd_sn, right->vpd_sn);
253 static void efx_associate(struct efx_nic *efx)
255 struct efx_nic *other, *next;
257 if (efx->primary == efx) {
258 /* Adding primary function; look for secondaries */
260 netif_dbg(efx, probe, efx->net_dev, "adding to primary list\n");
261 list_add_tail(&efx->node, &efx_primary_list);
263 list_for_each_entry_safe(other, next, &efx_unassociated_list,
265 if (efx_same_controller(efx, other)) {
266 list_del(&other->node);
267 netif_dbg(other, probe, other->net_dev,
268 "moving to secondary list of %s %s\n",
269 pci_name(efx->pci_dev),
271 list_add_tail(&other->node,
272 &efx->secondary_list);
273 other->primary = efx;
277 /* Adding secondary function; look for primary */
279 list_for_each_entry(other, &efx_primary_list, node) {
280 if (efx_same_controller(efx, other)) {
281 netif_dbg(efx, probe, efx->net_dev,
282 "adding to secondary list of %s %s\n",
283 pci_name(other->pci_dev),
284 other->net_dev->name);
285 list_add_tail(&efx->node,
286 &other->secondary_list);
287 efx->primary = other;
292 netif_dbg(efx, probe, efx->net_dev,
293 "adding to unassociated list\n");
294 list_add_tail(&efx->node, &efx_unassociated_list);
298 static void efx_dissociate(struct efx_nic *efx)
300 struct efx_nic *other, *next;
302 list_del(&efx->node);
305 list_for_each_entry_safe(other, next, &efx->secondary_list, node) {
306 list_del(&other->node);
307 netif_dbg(other, probe, other->net_dev,
308 "moving to unassociated list\n");
309 list_add_tail(&other->node, &efx_unassociated_list);
310 other->primary = NULL;
314 void efx_set_default_rx_indir_table(struct efx_nic *efx,
315 struct efx_rss_context *ctx)
319 for (i = 0; i < ARRAY_SIZE(ctx->rx_indir_table); i++)
320 ctx->rx_indir_table[i] =
321 ethtool_rxfh_indir_default(i, efx->rss_spread);
324 static int efx_probe_nic(struct efx_nic *efx)
328 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
330 /* Carry out hardware-type specific initialisation */
331 rc = efx->type->probe(efx);
336 if (!efx->max_channels || !efx->max_tx_channels) {
337 netif_err(efx, drv, efx->net_dev,
338 "Insufficient resources to allocate"
344 /* Determine the number of channels and queues by trying
345 * to hook in MSI-X interrupts.
347 rc = efx_probe_interrupts(efx);
351 rc = efx_set_channels(efx);
355 /* dimension_resources can fail with EAGAIN */
356 rc = efx->type->dimension_resources(efx);
357 if (rc != 0 && rc != -EAGAIN)
361 /* try again with new max_channels */
362 efx_remove_interrupts(efx);
364 } while (rc == -EAGAIN);
366 if (efx->n_channels > 1)
367 netdev_rss_key_fill(efx->rss_context.rx_hash_key,
368 sizeof(efx->rss_context.rx_hash_key));
369 efx_set_default_rx_indir_table(efx, &efx->rss_context);
371 netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
372 netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
374 /* Initialise the interrupt moderation settings */
375 efx->irq_mod_step_us = DIV_ROUND_UP(efx->timer_quantum_ns, 1000);
376 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
382 efx_remove_interrupts(efx);
384 efx->type->remove(efx);
388 static void efx_remove_nic(struct efx_nic *efx)
390 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
392 efx_remove_interrupts(efx);
393 efx->type->remove(efx);
396 static int efx_probe_filters(struct efx_nic *efx)
400 init_rwsem(&efx->filter_sem);
401 mutex_lock(&efx->mac_lock);
402 down_write(&efx->filter_sem);
403 rc = efx->type->filter_table_probe(efx);
407 #ifdef CONFIG_RFS_ACCEL
408 if (efx->type->offload_features & NETIF_F_NTUPLE) {
409 struct efx_channel *channel;
412 efx_for_each_channel(channel, efx) {
413 channel->rps_flow_id =
414 kcalloc(efx->type->max_rx_ip_filters,
415 sizeof(*channel->rps_flow_id),
417 if (!channel->rps_flow_id)
421 i < efx->type->max_rx_ip_filters;
423 channel->rps_flow_id[i] =
425 channel->rfs_expire_index = 0;
426 channel->rfs_filter_count = 0;
430 efx_for_each_channel(channel, efx)
431 kfree(channel->rps_flow_id);
432 efx->type->filter_table_remove(efx);
439 up_write(&efx->filter_sem);
440 mutex_unlock(&efx->mac_lock);
444 static void efx_remove_filters(struct efx_nic *efx)
446 #ifdef CONFIG_RFS_ACCEL
447 struct efx_channel *channel;
449 efx_for_each_channel(channel, efx) {
450 cancel_delayed_work_sync(&channel->filter_work);
451 kfree(channel->rps_flow_id);
454 down_write(&efx->filter_sem);
455 efx->type->filter_table_remove(efx);
456 up_write(&efx->filter_sem);
460 /**************************************************************************
462 * NIC startup/shutdown
464 *************************************************************************/
466 static int efx_probe_all(struct efx_nic *efx)
470 rc = efx_probe_nic(efx);
472 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
476 rc = efx_probe_port(efx);
478 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
482 BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
483 if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
487 efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
489 #ifdef CONFIG_SFC_SRIOV
490 rc = efx->type->vswitching_probe(efx);
491 if (rc) /* not fatal; the PF will still work fine */
492 netif_warn(efx, probe, efx->net_dev,
493 "failed to setup vswitching rc=%d;"
494 " VFs may not function\n", rc);
497 rc = efx_probe_filters(efx);
499 netif_err(efx, probe, efx->net_dev,
500 "failed to create filter tables\n");
504 rc = efx_probe_channels(efx);
511 efx_remove_filters(efx);
513 #ifdef CONFIG_SFC_SRIOV
514 efx->type->vswitching_remove(efx);
517 efx_remove_port(efx);
524 static void efx_remove_all(struct efx_nic *efx)
527 efx_xdp_setup_prog(efx, NULL);
530 efx_remove_channels(efx);
531 efx_remove_filters(efx);
532 #ifdef CONFIG_SFC_SRIOV
533 efx->type->vswitching_remove(efx);
535 efx_remove_port(efx);
539 /**************************************************************************
541 * Interrupt moderation
543 **************************************************************************/
544 unsigned int efx_usecs_to_ticks(struct efx_nic *efx, unsigned int usecs)
548 if (usecs * 1000 < efx->timer_quantum_ns)
549 return 1; /* never round down to 0 */
550 return usecs * 1000 / efx->timer_quantum_ns;
553 unsigned int efx_ticks_to_usecs(struct efx_nic *efx, unsigned int ticks)
555 /* We must round up when converting ticks to microseconds
556 * because we round down when converting the other way.
558 return DIV_ROUND_UP(ticks * efx->timer_quantum_ns, 1000);
561 /* Set interrupt moderation parameters */
562 int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
563 unsigned int rx_usecs, bool rx_adaptive,
564 bool rx_may_override_tx)
566 struct efx_channel *channel;
567 unsigned int timer_max_us;
569 EFX_ASSERT_RESET_SERIALISED(efx);
571 timer_max_us = efx->timer_max_ns / 1000;
573 if (tx_usecs > timer_max_us || rx_usecs > timer_max_us)
576 if (tx_usecs != rx_usecs && efx->tx_channel_offset == 0 &&
577 !rx_may_override_tx) {
578 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
579 "RX and TX IRQ moderation must be equal\n");
583 efx->irq_rx_adaptive = rx_adaptive;
584 efx->irq_rx_moderation_us = rx_usecs;
585 efx_for_each_channel(channel, efx) {
586 if (efx_channel_has_rx_queue(channel))
587 channel->irq_moderation_us = rx_usecs;
588 else if (efx_channel_has_tx_queues(channel))
589 channel->irq_moderation_us = tx_usecs;
590 else if (efx_channel_is_xdp_tx(channel))
591 channel->irq_moderation_us = tx_usecs;
597 void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
598 unsigned int *rx_usecs, bool *rx_adaptive)
600 *rx_adaptive = efx->irq_rx_adaptive;
601 *rx_usecs = efx->irq_rx_moderation_us;
603 /* If channels are shared between RX and TX, so is IRQ
604 * moderation. Otherwise, IRQ moderation is the same for all
605 * TX channels and is not adaptive.
607 if (efx->tx_channel_offset == 0) {
608 *tx_usecs = *rx_usecs;
610 struct efx_channel *tx_channel;
612 tx_channel = efx->channel[efx->tx_channel_offset];
613 *tx_usecs = tx_channel->irq_moderation_us;
617 /**************************************************************************
621 *************************************************************************/
624 * Context: process, rtnl_lock() held.
626 static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
628 struct efx_nic *efx = netdev_priv(net_dev);
629 struct mii_ioctl_data *data = if_mii(ifr);
631 if (cmd == SIOCSHWTSTAMP)
632 return efx_ptp_set_ts_config(efx, ifr);
633 if (cmd == SIOCGHWTSTAMP)
634 return efx_ptp_get_ts_config(efx, ifr);
636 /* Convert phy_id from older PRTAD/DEVAD format */
637 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
638 (data->phy_id & 0xfc00) == 0x0400)
639 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
641 return mdio_mii_ioctl(&efx->mdio, data, cmd);
644 /**************************************************************************
646 * Kernel net device interface
648 *************************************************************************/
650 /* Context: process, rtnl_lock() held. */
651 int efx_net_open(struct net_device *net_dev)
653 struct efx_nic *efx = netdev_priv(net_dev);
656 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
657 raw_smp_processor_id());
659 rc = efx_check_disabled(efx);
662 if (efx->phy_mode & PHY_MODE_SPECIAL)
664 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
667 /* Notify the kernel of the link state polled during driver load,
668 * before the monitor starts running */
669 efx_link_status_changed(efx);
672 if (efx->state == STATE_DISABLED || efx->reset_pending)
673 netif_device_detach(efx->net_dev);
674 efx_selftest_async_start(efx);
678 /* Context: process, rtnl_lock() held.
679 * Note that the kernel will ignore our return code; this method
680 * should really be a void.
682 int efx_net_stop(struct net_device *net_dev)
684 struct efx_nic *efx = netdev_priv(net_dev);
686 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
687 raw_smp_processor_id());
689 /* Stop the device and flush all the channels */
695 /* Context: process, dev_base_lock or RTNL held, non-blocking. */
696 static void efx_net_stats(struct net_device *net_dev,
697 struct rtnl_link_stats64 *stats)
699 struct efx_nic *efx = netdev_priv(net_dev);
701 spin_lock_bh(&efx->stats_lock);
702 efx->type->update_stats(efx, NULL, stats);
703 spin_unlock_bh(&efx->stats_lock);
706 /* Context: netif_tx_lock held, BHs disabled. */
707 static void efx_watchdog(struct net_device *net_dev, unsigned int txqueue)
709 struct efx_nic *efx = netdev_priv(net_dev);
711 netif_err(efx, tx_err, efx->net_dev,
712 "TX stuck with port_enabled=%d: resetting channels\n",
715 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
718 static unsigned int efx_xdp_max_mtu(struct efx_nic *efx)
720 /* The maximum MTU that we can fit in a single page, allowing for
721 * framing, overhead and XDP headroom.
723 int overhead = EFX_MAX_FRAME_LEN(0) + sizeof(struct efx_rx_page_state) +
724 efx->rx_prefix_size + efx->type->rx_buffer_padding +
725 efx->rx_ip_align + XDP_PACKET_HEADROOM;
727 return PAGE_SIZE - overhead;
730 /* Context: process, rtnl_lock() held. */
731 static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
733 struct efx_nic *efx = netdev_priv(net_dev);
736 rc = efx_check_disabled(efx);
740 if (rtnl_dereference(efx->xdp_prog) &&
741 new_mtu > efx_xdp_max_mtu(efx)) {
742 netif_err(efx, drv, efx->net_dev,
743 "Requested MTU of %d too big for XDP (max: %d)\n",
744 new_mtu, efx_xdp_max_mtu(efx));
748 netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
750 efx_device_detach_sync(efx);
753 mutex_lock(&efx->mac_lock);
754 net_dev->mtu = new_mtu;
755 efx_mac_reconfigure(efx);
756 mutex_unlock(&efx->mac_lock);
759 efx_device_attach_if_not_resetting(efx);
763 static int efx_set_mac_address(struct net_device *net_dev, void *data)
765 struct efx_nic *efx = netdev_priv(net_dev);
766 struct sockaddr *addr = data;
767 u8 *new_addr = addr->sa_data;
771 if (!is_valid_ether_addr(new_addr)) {
772 netif_err(efx, drv, efx->net_dev,
773 "invalid ethernet MAC address requested: %pM\n",
775 return -EADDRNOTAVAIL;
778 /* save old address */
779 ether_addr_copy(old_addr, net_dev->dev_addr);
780 ether_addr_copy(net_dev->dev_addr, new_addr);
781 if (efx->type->set_mac_address) {
782 rc = efx->type->set_mac_address(efx);
784 ether_addr_copy(net_dev->dev_addr, old_addr);
789 /* Reconfigure the MAC */
790 mutex_lock(&efx->mac_lock);
791 efx_mac_reconfigure(efx);
792 mutex_unlock(&efx->mac_lock);
797 /* Context: netif_addr_lock held, BHs disabled. */
798 static void efx_set_rx_mode(struct net_device *net_dev)
800 struct efx_nic *efx = netdev_priv(net_dev);
802 if (efx->port_enabled)
803 queue_work(efx->workqueue, &efx->mac_work);
804 /* Otherwise efx_start_port() will do this */
807 static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
809 struct efx_nic *efx = netdev_priv(net_dev);
812 /* If disabling RX n-tuple filtering, clear existing filters */
813 if (net_dev->features & ~data & NETIF_F_NTUPLE) {
814 rc = efx->type->filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
819 /* If Rx VLAN filter is changed, update filters via mac_reconfigure.
820 * If rx-fcs is changed, mac_reconfigure updates that too.
822 if ((net_dev->features ^ data) & (NETIF_F_HW_VLAN_CTAG_FILTER |
824 /* efx_set_rx_mode() will schedule MAC work to update filters
825 * when a new features are finally set in net_dev.
827 efx_set_rx_mode(net_dev);
833 static int efx_get_phys_port_id(struct net_device *net_dev,
834 struct netdev_phys_item_id *ppid)
836 struct efx_nic *efx = netdev_priv(net_dev);
838 if (efx->type->get_phys_port_id)
839 return efx->type->get_phys_port_id(efx, ppid);
844 static int efx_get_phys_port_name(struct net_device *net_dev,
845 char *name, size_t len)
847 struct efx_nic *efx = netdev_priv(net_dev);
849 if (snprintf(name, len, "p%u", efx->port_num) >= len)
854 static int efx_vlan_rx_add_vid(struct net_device *net_dev, __be16 proto, u16 vid)
856 struct efx_nic *efx = netdev_priv(net_dev);
858 if (efx->type->vlan_rx_add_vid)
859 return efx->type->vlan_rx_add_vid(efx, proto, vid);
864 static int efx_vlan_rx_kill_vid(struct net_device *net_dev, __be16 proto, u16 vid)
866 struct efx_nic *efx = netdev_priv(net_dev);
868 if (efx->type->vlan_rx_kill_vid)
869 return efx->type->vlan_rx_kill_vid(efx, proto, vid);
874 static int efx_udp_tunnel_type_map(enum udp_parsable_tunnel_type in)
877 case UDP_TUNNEL_TYPE_VXLAN:
878 return TUNNEL_ENCAP_UDP_PORT_ENTRY_VXLAN;
879 case UDP_TUNNEL_TYPE_GENEVE:
880 return TUNNEL_ENCAP_UDP_PORT_ENTRY_GENEVE;
886 static void efx_udp_tunnel_add(struct net_device *dev, struct udp_tunnel_info *ti)
888 struct efx_nic *efx = netdev_priv(dev);
889 struct efx_udp_tunnel tnl;
892 efx_tunnel_type = efx_udp_tunnel_type_map(ti->type);
893 if (efx_tunnel_type < 0)
896 tnl.type = (u16)efx_tunnel_type;
899 if (efx->type->udp_tnl_add_port)
900 (void)efx->type->udp_tnl_add_port(efx, tnl);
903 static void efx_udp_tunnel_del(struct net_device *dev, struct udp_tunnel_info *ti)
905 struct efx_nic *efx = netdev_priv(dev);
906 struct efx_udp_tunnel tnl;
909 efx_tunnel_type = efx_udp_tunnel_type_map(ti->type);
910 if (efx_tunnel_type < 0)
913 tnl.type = (u16)efx_tunnel_type;
916 if (efx->type->udp_tnl_del_port)
917 (void)efx->type->udp_tnl_del_port(efx, tnl);
920 static const struct net_device_ops efx_netdev_ops = {
921 .ndo_open = efx_net_open,
922 .ndo_stop = efx_net_stop,
923 .ndo_get_stats64 = efx_net_stats,
924 .ndo_tx_timeout = efx_watchdog,
925 .ndo_start_xmit = efx_hard_start_xmit,
926 .ndo_validate_addr = eth_validate_addr,
927 .ndo_do_ioctl = efx_ioctl,
928 .ndo_change_mtu = efx_change_mtu,
929 .ndo_set_mac_address = efx_set_mac_address,
930 .ndo_set_rx_mode = efx_set_rx_mode,
931 .ndo_set_features = efx_set_features,
932 .ndo_vlan_rx_add_vid = efx_vlan_rx_add_vid,
933 .ndo_vlan_rx_kill_vid = efx_vlan_rx_kill_vid,
934 #ifdef CONFIG_SFC_SRIOV
935 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
936 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
937 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
938 .ndo_get_vf_config = efx_sriov_get_vf_config,
939 .ndo_set_vf_link_state = efx_sriov_set_vf_link_state,
941 .ndo_get_phys_port_id = efx_get_phys_port_id,
942 .ndo_get_phys_port_name = efx_get_phys_port_name,
943 .ndo_setup_tc = efx_setup_tc,
944 #ifdef CONFIG_RFS_ACCEL
945 .ndo_rx_flow_steer = efx_filter_rfs,
947 .ndo_udp_tunnel_add = efx_udp_tunnel_add,
948 .ndo_udp_tunnel_del = efx_udp_tunnel_del,
949 .ndo_xdp_xmit = efx_xdp_xmit,
953 static int efx_xdp_setup_prog(struct efx_nic *efx, struct bpf_prog *prog)
955 struct bpf_prog *old_prog;
957 if (efx->xdp_rxq_info_failed) {
958 netif_err(efx, drv, efx->net_dev,
959 "Unable to bind XDP program due to previous failure of rxq_info\n");
963 if (prog && efx->net_dev->mtu > efx_xdp_max_mtu(efx)) {
964 netif_err(efx, drv, efx->net_dev,
965 "Unable to configure XDP with MTU of %d (max: %d)\n",
966 efx->net_dev->mtu, efx_xdp_max_mtu(efx));
970 old_prog = rtnl_dereference(efx->xdp_prog);
971 rcu_assign_pointer(efx->xdp_prog, prog);
972 /* Release the reference that was originally passed by the caller. */
974 bpf_prog_put(old_prog);
979 /* Context: process, rtnl_lock() held. */
980 static int efx_xdp(struct net_device *dev, struct netdev_bpf *xdp)
982 struct efx_nic *efx = netdev_priv(dev);
983 struct bpf_prog *xdp_prog;
985 switch (xdp->command) {
987 return efx_xdp_setup_prog(efx, xdp->prog);
989 xdp_prog = rtnl_dereference(efx->xdp_prog);
990 xdp->prog_id = xdp_prog ? xdp_prog->aux->id : 0;
997 static int efx_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **xdpfs,
1000 struct efx_nic *efx = netdev_priv(dev);
1002 if (!netif_running(dev))
1005 return efx_xdp_tx_buffers(efx, n, xdpfs, flags & XDP_XMIT_FLUSH);
1008 static void efx_update_name(struct efx_nic *efx)
1010 strcpy(efx->name, efx->net_dev->name);
1011 efx_mtd_rename(efx);
1012 efx_set_channel_names(efx);
1015 static int efx_netdev_event(struct notifier_block *this,
1016 unsigned long event, void *ptr)
1018 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
1020 if ((net_dev->netdev_ops == &efx_netdev_ops) &&
1021 event == NETDEV_CHANGENAME)
1022 efx_update_name(netdev_priv(net_dev));
1027 static struct notifier_block efx_netdev_notifier = {
1028 .notifier_call = efx_netdev_event,
1032 show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
1034 struct efx_nic *efx = dev_get_drvdata(dev);
1035 return sprintf(buf, "%d\n", efx->phy_type);
1037 static DEVICE_ATTR(phy_type, 0444, show_phy_type, NULL);
1039 static int efx_register_netdev(struct efx_nic *efx)
1041 struct net_device *net_dev = efx->net_dev;
1042 struct efx_channel *channel;
1045 net_dev->watchdog_timeo = 5 * HZ;
1046 net_dev->irq = efx->pci_dev->irq;
1047 net_dev->netdev_ops = &efx_netdev_ops;
1048 if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0)
1049 net_dev->priv_flags |= IFF_UNICAST_FLT;
1050 net_dev->ethtool_ops = &efx_ethtool_ops;
1051 net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
1052 net_dev->min_mtu = EFX_MIN_MTU;
1053 net_dev->max_mtu = EFX_MAX_MTU;
1057 /* Enable resets to be scheduled and check whether any were
1058 * already requested. If so, the NIC is probably hosed so we
1061 efx->state = STATE_READY;
1062 smp_mb(); /* ensure we change state before checking reset_pending */
1063 if (efx->reset_pending) {
1064 netif_err(efx, probe, efx->net_dev,
1065 "aborting probe due to scheduled reset\n");
1070 rc = dev_alloc_name(net_dev, net_dev->name);
1073 efx_update_name(efx);
1075 /* Always start with carrier off; PHY events will detect the link */
1076 netif_carrier_off(net_dev);
1078 rc = register_netdevice(net_dev);
1082 efx_for_each_channel(channel, efx) {
1083 struct efx_tx_queue *tx_queue;
1084 efx_for_each_channel_tx_queue(tx_queue, channel)
1085 efx_init_tx_queue_core_txq(tx_queue);
1092 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
1094 netif_err(efx, drv, efx->net_dev,
1095 "failed to init net dev attributes\n");
1096 goto fail_registered;
1099 efx_init_mcdi_logging(efx);
1105 efx_dissociate(efx);
1106 unregister_netdevice(net_dev);
1108 efx->state = STATE_UNINIT;
1110 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
1114 static void efx_unregister_netdev(struct efx_nic *efx)
1119 BUG_ON(netdev_priv(efx->net_dev) != efx);
1121 if (efx_dev_registered(efx)) {
1122 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
1123 efx_fini_mcdi_logging(efx);
1124 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
1125 unregister_netdev(efx->net_dev);
1129 /**************************************************************************
1131 * List of NICs we support
1133 **************************************************************************/
1135 /* PCI device ID table */
1136 static const struct pci_device_id efx_pci_table[] = {
1137 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
1138 .driver_data = (unsigned long) &siena_a0_nic_type},
1139 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
1140 .driver_data = (unsigned long) &siena_a0_nic_type},
1141 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0903), /* SFC9120 PF */
1142 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
1143 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1903), /* SFC9120 VF */
1144 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
1145 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0923), /* SFC9140 PF */
1146 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
1147 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1923), /* SFC9140 VF */
1148 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
1149 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0a03), /* SFC9220 PF */
1150 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
1151 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1a03), /* SFC9220 VF */
1152 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
1153 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0b03), /* SFC9250 PF */
1154 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
1155 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1b03), /* SFC9250 VF */
1156 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
1157 {0} /* end of list */
1160 /**************************************************************************
1164 **************************************************************************/
1166 void efx_update_sw_stats(struct efx_nic *efx, u64 *stats)
1168 u64 n_rx_nodesc_trunc = 0;
1169 struct efx_channel *channel;
1171 efx_for_each_channel(channel, efx)
1172 n_rx_nodesc_trunc += channel->n_rx_nodesc_trunc;
1173 stats[GENERIC_STAT_rx_nodesc_trunc] = n_rx_nodesc_trunc;
1174 stats[GENERIC_STAT_rx_noskb_drops] = atomic_read(&efx->n_rx_noskb_drops);
1177 bool efx_filter_spec_equal(const struct efx_filter_spec *left,
1178 const struct efx_filter_spec *right)
1180 if ((left->match_flags ^ right->match_flags) |
1181 ((left->flags ^ right->flags) &
1182 (EFX_FILTER_FLAG_RX | EFX_FILTER_FLAG_TX)))
1185 return memcmp(&left->outer_vid, &right->outer_vid,
1186 sizeof(struct efx_filter_spec) -
1187 offsetof(struct efx_filter_spec, outer_vid)) == 0;
1190 u32 efx_filter_spec_hash(const struct efx_filter_spec *spec)
1192 BUILD_BUG_ON(offsetof(struct efx_filter_spec, outer_vid) & 3);
1193 return jhash2((const u32 *)&spec->outer_vid,
1194 (sizeof(struct efx_filter_spec) -
1195 offsetof(struct efx_filter_spec, outer_vid)) / 4,
1199 #ifdef CONFIG_RFS_ACCEL
1200 bool efx_rps_check_rule(struct efx_arfs_rule *rule, unsigned int filter_idx,
1203 if (rule->filter_id == EFX_ARFS_FILTER_ID_PENDING) {
1204 /* ARFS is currently updating this entry, leave it */
1207 if (rule->filter_id == EFX_ARFS_FILTER_ID_ERROR) {
1208 /* ARFS tried and failed to update this, so it's probably out
1209 * of date. Remove the filter and the ARFS rule entry.
1211 rule->filter_id = EFX_ARFS_FILTER_ID_REMOVING;
1214 } else if (WARN_ON(rule->filter_id != filter_idx)) { /* can't happen */
1215 /* ARFS has moved on, so old filter is not needed. Since we did
1216 * not mark the rule with EFX_ARFS_FILTER_ID_REMOVING, it will
1217 * not be removed by efx_rps_hash_del() subsequently.
1222 /* Remove it iff ARFS wants to. */
1227 struct hlist_head *efx_rps_hash_bucket(struct efx_nic *efx,
1228 const struct efx_filter_spec *spec)
1230 u32 hash = efx_filter_spec_hash(spec);
1232 lockdep_assert_held(&efx->rps_hash_lock);
1233 if (!efx->rps_hash_table)
1235 return &efx->rps_hash_table[hash % EFX_ARFS_HASH_TABLE_SIZE];
1238 struct efx_arfs_rule *efx_rps_hash_find(struct efx_nic *efx,
1239 const struct efx_filter_spec *spec)
1241 struct efx_arfs_rule *rule;
1242 struct hlist_head *head;
1243 struct hlist_node *node;
1245 head = efx_rps_hash_bucket(efx, spec);
1248 hlist_for_each(node, head) {
1249 rule = container_of(node, struct efx_arfs_rule, node);
1250 if (efx_filter_spec_equal(spec, &rule->spec))
1256 struct efx_arfs_rule *efx_rps_hash_add(struct efx_nic *efx,
1257 const struct efx_filter_spec *spec,
1260 struct efx_arfs_rule *rule;
1261 struct hlist_head *head;
1262 struct hlist_node *node;
1264 head = efx_rps_hash_bucket(efx, spec);
1267 hlist_for_each(node, head) {
1268 rule = container_of(node, struct efx_arfs_rule, node);
1269 if (efx_filter_spec_equal(spec, &rule->spec)) {
1274 rule = kmalloc(sizeof(*rule), GFP_ATOMIC);
1277 memcpy(&rule->spec, spec, sizeof(rule->spec));
1278 hlist_add_head(&rule->node, head);
1283 void efx_rps_hash_del(struct efx_nic *efx, const struct efx_filter_spec *spec)
1285 struct efx_arfs_rule *rule;
1286 struct hlist_head *head;
1287 struct hlist_node *node;
1289 head = efx_rps_hash_bucket(efx, spec);
1292 hlist_for_each(node, head) {
1293 rule = container_of(node, struct efx_arfs_rule, node);
1294 if (efx_filter_spec_equal(spec, &rule->spec)) {
1295 /* Someone already reused the entry. We know that if
1296 * this check doesn't fire (i.e. filter_id == REMOVING)
1297 * then the REMOVING mark was put there by our caller,
1298 * because caller is holding a lock on filter table and
1299 * only holders of that lock set REMOVING.
1301 if (rule->filter_id != EFX_ARFS_FILTER_ID_REMOVING)
1308 /* We didn't find it. */
1313 /* RSS contexts. We're using linked lists and crappy O(n) algorithms, because
1314 * (a) this is an infrequent control-plane operation and (b) n is small (max 64)
1316 struct efx_rss_context *efx_alloc_rss_context_entry(struct efx_nic *efx)
1318 struct list_head *head = &efx->rss_context.list;
1319 struct efx_rss_context *ctx, *new;
1320 u32 id = 1; /* Don't use zero, that refers to the master RSS context */
1322 WARN_ON(!mutex_is_locked(&efx->rss_lock));
1324 /* Search for first gap in the numbering */
1325 list_for_each_entry(ctx, head, list) {
1326 if (ctx->user_id != id)
1329 /* Check for wrap. If this happens, we have nearly 2^32
1330 * allocated RSS contexts, which seems unlikely.
1332 if (WARN_ON_ONCE(!id))
1336 /* Create the new entry */
1337 new = kmalloc(sizeof(struct efx_rss_context), GFP_KERNEL);
1340 new->context_id = EFX_EF10_RSS_CONTEXT_INVALID;
1341 new->rx_hash_udp_4tuple = false;
1343 /* Insert the new entry into the gap */
1345 list_add_tail(&new->list, &ctx->list);
1349 struct efx_rss_context *efx_find_rss_context_entry(struct efx_nic *efx, u32 id)
1351 struct list_head *head = &efx->rss_context.list;
1352 struct efx_rss_context *ctx;
1354 WARN_ON(!mutex_is_locked(&efx->rss_lock));
1356 list_for_each_entry(ctx, head, list)
1357 if (ctx->user_id == id)
1362 void efx_free_rss_context_entry(struct efx_rss_context *ctx)
1364 list_del(&ctx->list);
1368 /**************************************************************************
1372 **************************************************************************/
1374 /* Main body of final NIC shutdown code
1375 * This is called only at module unload (or hotplug removal).
1377 static void efx_pci_remove_main(struct efx_nic *efx)
1379 /* Flush reset_work. It can no longer be scheduled since we
1382 BUG_ON(efx->state == STATE_READY);
1383 efx_flush_reset_workqueue(efx);
1385 efx_disable_interrupts(efx);
1386 efx_clear_interrupt_affinity(efx);
1387 efx_nic_fini_interrupt(efx);
1389 efx->type->fini(efx);
1391 efx_remove_all(efx);
1394 /* Final NIC shutdown
1395 * This is called only at module unload (or hotplug removal). A PF can call
1396 * this on its VFs to ensure they are unbound first.
1398 static void efx_pci_remove(struct pci_dev *pci_dev)
1400 struct efx_nic *efx;
1402 efx = pci_get_drvdata(pci_dev);
1406 /* Mark the NIC as fini, then stop the interface */
1408 efx_dissociate(efx);
1409 dev_close(efx->net_dev);
1410 efx_disable_interrupts(efx);
1411 efx->state = STATE_UNINIT;
1414 if (efx->type->sriov_fini)
1415 efx->type->sriov_fini(efx);
1417 efx_unregister_netdev(efx);
1419 efx_mtd_remove(efx);
1421 efx_pci_remove_main(efx);
1423 efx_fini_io(efx, efx->type->mem_bar(efx));
1424 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
1426 efx_fini_struct(efx);
1427 free_netdev(efx->net_dev);
1429 pci_disable_pcie_error_reporting(pci_dev);
1432 /* NIC VPD information
1433 * Called during probe to display the part number of the
1434 * installed NIC. VPD is potentially very large but this should
1435 * always appear within the first 512 bytes.
1437 #define SFC_VPD_LEN 512
1438 static void efx_probe_vpd_strings(struct efx_nic *efx)
1440 struct pci_dev *dev = efx->pci_dev;
1441 char vpd_data[SFC_VPD_LEN];
1443 int ro_start, ro_size, i, j;
1445 /* Get the vpd data from the device */
1446 vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
1447 if (vpd_size <= 0) {
1448 netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
1452 /* Get the Read only section */
1453 ro_start = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
1455 netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
1459 ro_size = pci_vpd_lrdt_size(&vpd_data[ro_start]);
1461 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
1462 if (i + j > vpd_size)
1465 /* Get the Part number */
1466 i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
1468 netif_err(efx, drv, efx->net_dev, "Part number not found\n");
1472 j = pci_vpd_info_field_size(&vpd_data[i]);
1473 i += PCI_VPD_INFO_FLD_HDR_SIZE;
1474 if (i + j > vpd_size) {
1475 netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
1479 netif_info(efx, drv, efx->net_dev,
1480 "Part Number : %.*s\n", j, &vpd_data[i]);
1482 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
1484 i = pci_vpd_find_info_keyword(vpd_data, i, j, "SN");
1486 netif_err(efx, drv, efx->net_dev, "Serial number not found\n");
1490 j = pci_vpd_info_field_size(&vpd_data[i]);
1491 i += PCI_VPD_INFO_FLD_HDR_SIZE;
1492 if (i + j > vpd_size) {
1493 netif_err(efx, drv, efx->net_dev, "Incomplete serial number\n");
1497 efx->vpd_sn = kmalloc(j + 1, GFP_KERNEL);
1501 snprintf(efx->vpd_sn, j + 1, "%s", &vpd_data[i]);
1505 /* Main body of NIC initialisation
1506 * This is called at module load (or hotplug insertion, theoretically).
1508 static int efx_pci_probe_main(struct efx_nic *efx)
1512 /* Do start-of-day initialisation */
1513 rc = efx_probe_all(efx);
1519 down_write(&efx->filter_sem);
1520 rc = efx->type->init(efx);
1521 up_write(&efx->filter_sem);
1523 netif_err(efx, probe, efx->net_dev,
1524 "failed to initialise NIC\n");
1528 rc = efx_init_port(efx);
1530 netif_err(efx, probe, efx->net_dev,
1531 "failed to initialise port\n");
1535 rc = efx_nic_init_interrupt(efx);
1539 efx_set_interrupt_affinity(efx);
1540 rc = efx_enable_interrupts(efx);
1547 efx_clear_interrupt_affinity(efx);
1548 efx_nic_fini_interrupt(efx);
1552 efx->type->fini(efx);
1555 efx_remove_all(efx);
1560 static int efx_pci_probe_post_io(struct efx_nic *efx)
1562 struct net_device *net_dev = efx->net_dev;
1563 int rc = efx_pci_probe_main(efx);
1568 if (efx->type->sriov_init) {
1569 rc = efx->type->sriov_init(efx);
1571 netif_err(efx, probe, efx->net_dev,
1572 "SR-IOV can't be enabled rc %d\n", rc);
1575 /* Determine netdevice features */
1576 net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
1577 NETIF_F_TSO | NETIF_F_RXCSUM | NETIF_F_RXALL);
1578 if (efx->type->offload_features & (NETIF_F_IPV6_CSUM | NETIF_F_HW_CSUM))
1579 net_dev->features |= NETIF_F_TSO6;
1580 /* Check whether device supports TSO */
1581 if (!efx->type->tso_versions || !efx->type->tso_versions(efx))
1582 net_dev->features &= ~NETIF_F_ALL_TSO;
1583 /* Mask for features that also apply to VLAN devices */
1584 net_dev->vlan_features |= (NETIF_F_HW_CSUM | NETIF_F_SG |
1585 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
1588 net_dev->hw_features |= net_dev->features & ~efx->fixed_features;
1590 /* Disable receiving frames with bad FCS, by default. */
1591 net_dev->features &= ~NETIF_F_RXALL;
1593 /* Disable VLAN filtering by default. It may be enforced if
1594 * the feature is fixed (i.e. VLAN filters are required to
1595 * receive VLAN tagged packets due to vPort restrictions).
1597 net_dev->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1598 net_dev->features |= efx->fixed_features;
1600 rc = efx_register_netdev(efx);
1604 efx_pci_remove_main(efx);
1608 /* NIC initialisation
1610 * This is called at module load (or hotplug insertion,
1611 * theoretically). It sets up PCI mappings, resets the NIC,
1612 * sets up and registers the network devices with the kernel and hooks
1613 * the interrupt service routine. It does not prepare the device for
1614 * transmission; this is left to the first time one of the network
1615 * interfaces is brought up (i.e. efx_net_open).
1617 static int efx_pci_probe(struct pci_dev *pci_dev,
1618 const struct pci_device_id *entry)
1620 struct net_device *net_dev;
1621 struct efx_nic *efx;
1624 /* Allocate and initialise a struct net_device and struct efx_nic */
1625 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
1629 efx = netdev_priv(net_dev);
1630 efx->type = (const struct efx_nic_type *) entry->driver_data;
1631 efx->fixed_features |= NETIF_F_HIGHDMA;
1633 pci_set_drvdata(pci_dev, efx);
1634 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
1635 rc = efx_init_struct(efx, pci_dev, net_dev);
1639 netif_info(efx, probe, efx->net_dev,
1640 "Solarflare NIC detected\n");
1642 if (!efx->type->is_vf)
1643 efx_probe_vpd_strings(efx);
1645 /* Set up basic I/O (BAR mappings etc) */
1646 rc = efx_init_io(efx, efx->type->mem_bar(efx), efx->type->max_dma_mask,
1647 efx->type->mem_map_size(efx));
1651 rc = efx_pci_probe_post_io(efx);
1653 /* On failure, retry once immediately.
1654 * If we aborted probe due to a scheduled reset, dismiss it.
1656 efx->reset_pending = 0;
1657 rc = efx_pci_probe_post_io(efx);
1659 /* On another failure, retry once more
1660 * after a 50-305ms delay.
1664 get_random_bytes(&r, 1);
1665 msleep((unsigned int)r + 50);
1666 efx->reset_pending = 0;
1667 rc = efx_pci_probe_post_io(efx);
1673 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
1675 /* Try to create MTDs, but allow this to fail */
1677 rc = efx_mtd_probe(efx);
1679 if (rc && rc != -EPERM)
1680 netif_warn(efx, probe, efx->net_dev,
1681 "failed to create MTDs (%d)\n", rc);
1683 (void)pci_enable_pcie_error_reporting(pci_dev);
1685 if (efx->type->udp_tnl_push_ports)
1686 efx->type->udp_tnl_push_ports(efx);
1691 efx_fini_io(efx, efx->type->mem_bar(efx));
1693 efx_fini_struct(efx);
1696 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
1697 free_netdev(net_dev);
1701 /* efx_pci_sriov_configure returns the actual number of Virtual Functions
1702 * enabled on success
1704 #ifdef CONFIG_SFC_SRIOV
1705 static int efx_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
1708 struct efx_nic *efx = pci_get_drvdata(dev);
1710 if (efx->type->sriov_configure) {
1711 rc = efx->type->sriov_configure(efx, num_vfs);
1721 static int efx_pm_freeze(struct device *dev)
1723 struct efx_nic *efx = dev_get_drvdata(dev);
1727 if (efx->state != STATE_DISABLED) {
1728 efx->state = STATE_UNINIT;
1730 efx_device_detach_sync(efx);
1733 efx_disable_interrupts(efx);
1741 static int efx_pm_thaw(struct device *dev)
1744 struct efx_nic *efx = dev_get_drvdata(dev);
1748 if (efx->state != STATE_DISABLED) {
1749 rc = efx_enable_interrupts(efx);
1753 mutex_lock(&efx->mac_lock);
1754 efx->phy_op->reconfigure(efx);
1755 mutex_unlock(&efx->mac_lock);
1759 efx_device_attach_if_not_resetting(efx);
1761 efx->state = STATE_READY;
1763 efx->type->resume_wol(efx);
1768 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
1769 efx_queue_reset_work(efx);
1779 static int efx_pm_poweroff(struct device *dev)
1781 struct pci_dev *pci_dev = to_pci_dev(dev);
1782 struct efx_nic *efx = pci_get_drvdata(pci_dev);
1784 efx->type->fini(efx);
1786 efx->reset_pending = 0;
1788 pci_save_state(pci_dev);
1789 return pci_set_power_state(pci_dev, PCI_D3hot);
1792 /* Used for both resume and restore */
1793 static int efx_pm_resume(struct device *dev)
1795 struct pci_dev *pci_dev = to_pci_dev(dev);
1796 struct efx_nic *efx = pci_get_drvdata(pci_dev);
1799 rc = pci_set_power_state(pci_dev, PCI_D0);
1802 pci_restore_state(pci_dev);
1803 rc = pci_enable_device(pci_dev);
1806 pci_set_master(efx->pci_dev);
1807 rc = efx->type->reset(efx, RESET_TYPE_ALL);
1810 down_write(&efx->filter_sem);
1811 rc = efx->type->init(efx);
1812 up_write(&efx->filter_sem);
1815 rc = efx_pm_thaw(dev);
1819 static int efx_pm_suspend(struct device *dev)
1824 rc = efx_pm_poweroff(dev);
1830 static const struct dev_pm_ops efx_pm_ops = {
1831 .suspend = efx_pm_suspend,
1832 .resume = efx_pm_resume,
1833 .freeze = efx_pm_freeze,
1834 .thaw = efx_pm_thaw,
1835 .poweroff = efx_pm_poweroff,
1836 .restore = efx_pm_resume,
1839 /* A PCI error affecting this device was detected.
1840 * At this point MMIO and DMA may be disabled.
1841 * Stop the software path and request a slot reset.
1843 static pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
1844 enum pci_channel_state state)
1846 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
1847 struct efx_nic *efx = pci_get_drvdata(pdev);
1849 if (state == pci_channel_io_perm_failure)
1850 return PCI_ERS_RESULT_DISCONNECT;
1854 if (efx->state != STATE_DISABLED) {
1855 efx->state = STATE_RECOVERY;
1856 efx->reset_pending = 0;
1858 efx_device_detach_sync(efx);
1861 efx_disable_interrupts(efx);
1863 status = PCI_ERS_RESULT_NEED_RESET;
1865 /* If the interface is disabled we don't want to do anything
1868 status = PCI_ERS_RESULT_RECOVERED;
1873 pci_disable_device(pdev);
1878 /* Fake a successful reset, which will be performed later in efx_io_resume. */
1879 static pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
1881 struct efx_nic *efx = pci_get_drvdata(pdev);
1882 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
1884 if (pci_enable_device(pdev)) {
1885 netif_err(efx, hw, efx->net_dev,
1886 "Cannot re-enable PCI device after reset.\n");
1887 status = PCI_ERS_RESULT_DISCONNECT;
1893 /* Perform the actual reset and resume I/O operations. */
1894 static void efx_io_resume(struct pci_dev *pdev)
1896 struct efx_nic *efx = pci_get_drvdata(pdev);
1901 if (efx->state == STATE_DISABLED)
1904 rc = efx_reset(efx, RESET_TYPE_ALL);
1906 netif_err(efx, hw, efx->net_dev,
1907 "efx_reset failed after PCI error (%d)\n", rc);
1909 efx->state = STATE_READY;
1910 netif_dbg(efx, hw, efx->net_dev,
1911 "Done resetting and resuming IO after PCI error.\n");
1918 /* For simplicity and reliability, we always require a slot reset and try to
1919 * reset the hardware when a pci error affecting the device is detected.
1920 * We leave both the link_reset and mmio_enabled callback unimplemented:
1921 * with our request for slot reset the mmio_enabled callback will never be
1922 * called, and the link_reset callback is not used by AER or EEH mechanisms.
1924 static const struct pci_error_handlers efx_err_handlers = {
1925 .error_detected = efx_io_error_detected,
1926 .slot_reset = efx_io_slot_reset,
1927 .resume = efx_io_resume,
1930 static struct pci_driver efx_pci_driver = {
1931 .name = KBUILD_MODNAME,
1932 .id_table = efx_pci_table,
1933 .probe = efx_pci_probe,
1934 .remove = efx_pci_remove,
1935 .driver.pm = &efx_pm_ops,
1936 .err_handler = &efx_err_handlers,
1937 #ifdef CONFIG_SFC_SRIOV
1938 .sriov_configure = efx_pci_sriov_configure,
1942 /**************************************************************************
1944 * Kernel module interface
1946 *************************************************************************/
1948 static int __init efx_init_module(void)
1952 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
1954 rc = register_netdevice_notifier(&efx_netdev_notifier);
1958 #ifdef CONFIG_SFC_SRIOV
1959 rc = efx_init_sriov();
1964 rc = efx_create_reset_workqueue();
1968 rc = pci_register_driver(&efx_pci_driver);
1975 efx_destroy_reset_workqueue();
1977 #ifdef CONFIG_SFC_SRIOV
1981 unregister_netdevice_notifier(&efx_netdev_notifier);
1986 static void __exit efx_exit_module(void)
1988 printk(KERN_INFO "Solarflare NET driver unloading\n");
1990 pci_unregister_driver(&efx_pci_driver);
1991 efx_destroy_reset_workqueue();
1992 #ifdef CONFIG_SFC_SRIOV
1995 unregister_netdevice_notifier(&efx_netdev_notifier);
1999 module_init(efx_init_module);
2000 module_exit(efx_exit_module);
2002 MODULE_AUTHOR("Solarflare Communications and "
2003 "Michael Brown <mbrown@fensystems.co.uk>");
2004 MODULE_DESCRIPTION("Solarflare network driver");
2005 MODULE_LICENSE("GPL");
2006 MODULE_DEVICE_TABLE(pci, efx_pci_table);
2007 MODULE_VERSION(EFX_DRIVER_VERSION);