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"
30 #include "rx_common.h"
37 #include "mcdi_pcol.h"
38 #include "workarounds.h"
40 /**************************************************************************
44 **************************************************************************
47 /* UDP tunnel type names */
48 static const char *const efx_udp_tunnel_type_names[] = {
49 [TUNNEL_ENCAP_UDP_PORT_ENTRY_VXLAN] = "vxlan",
50 [TUNNEL_ENCAP_UDP_PORT_ENTRY_GENEVE] = "geneve",
53 void efx_get_udp_tunnel_type_name(u16 type, char *buf, size_t buflen)
55 if (type < ARRAY_SIZE(efx_udp_tunnel_type_names) &&
56 efx_udp_tunnel_type_names[type] != NULL)
57 snprintf(buf, buflen, "%s", efx_udp_tunnel_type_names[type]);
59 snprintf(buf, buflen, "type %d", type);
62 /**************************************************************************
66 *************************************************************************/
69 * Use separate channels for TX and RX events
71 * Set this to 1 to use separate channels for TX and RX. It allows us
72 * to control interrupt affinity separately for TX and RX.
74 * This is only used in MSI-X interrupt mode
76 bool efx_separate_tx_channels;
77 module_param(efx_separate_tx_channels, bool, 0444);
78 MODULE_PARM_DESC(efx_separate_tx_channels,
79 "Use separate channels for TX and RX");
81 /* Initial interrupt moderation settings. They can be modified after
82 * module load with ethtool.
84 * The default for RX should strike a balance between increasing the
85 * round-trip latency and reducing overhead.
87 static unsigned int rx_irq_mod_usec = 60;
89 /* Initial interrupt moderation settings. They can be modified after
90 * module load with ethtool.
92 * This default is chosen to ensure that a 10G link does not go idle
93 * while a TX queue is stopped after it has become full. A queue is
94 * restarted when it drops below half full. The time this takes (assuming
95 * worst case 3 descriptors per packet and 1024 descriptors) is
96 * 512 / 3 * 1.2 = 205 usec.
98 static unsigned int tx_irq_mod_usec = 150;
100 static bool phy_flash_cfg;
101 module_param(phy_flash_cfg, bool, 0644);
102 MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
104 static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
105 NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
106 NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
107 NETIF_MSG_TX_ERR | NETIF_MSG_HW);
108 module_param(debug, uint, 0);
109 MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
111 /**************************************************************************
113 * Utility functions and prototypes
115 *************************************************************************/
117 static const struct efx_channel_type efx_default_channel_type;
118 static void efx_remove_port(struct efx_nic *efx);
119 static int efx_xdp_setup_prog(struct efx_nic *efx, struct bpf_prog *prog);
120 static int efx_xdp(struct net_device *dev, struct netdev_bpf *xdp);
121 static int efx_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **xdpfs,
124 #define EFX_ASSERT_RESET_SERIALISED(efx) \
126 if ((efx->state == STATE_READY) || \
127 (efx->state == STATE_RECOVERY) || \
128 (efx->state == STATE_DISABLED)) \
132 /**************************************************************************
136 **************************************************************************/
138 /* Equivalent to efx_link_set_advertising with all-zeroes, except does not
139 * force the Autoneg bit on.
141 void efx_link_clear_advertising(struct efx_nic *efx)
143 bitmap_zero(efx->link_advertising, __ETHTOOL_LINK_MODE_MASK_NBITS);
144 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
147 void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
149 efx->wanted_fc = wanted_fc;
150 if (efx->link_advertising[0]) {
151 if (wanted_fc & EFX_FC_RX)
152 efx->link_advertising[0] |= (ADVERTISED_Pause |
153 ADVERTISED_Asym_Pause);
155 efx->link_advertising[0] &= ~(ADVERTISED_Pause |
156 ADVERTISED_Asym_Pause);
157 if (wanted_fc & EFX_FC_TX)
158 efx->link_advertising[0] ^= ADVERTISED_Asym_Pause;
162 static void efx_fini_port(struct efx_nic *efx);
164 static int efx_probe_port(struct efx_nic *efx)
168 netif_dbg(efx, probe, efx->net_dev, "create port\n");
171 efx->phy_mode = PHY_MODE_SPECIAL;
173 /* Connect up MAC/PHY operations table */
174 rc = efx->type->probe_port(efx);
178 /* Initialise MAC address to permanent address */
179 ether_addr_copy(efx->net_dev->dev_addr, efx->net_dev->perm_addr);
184 static int efx_init_port(struct efx_nic *efx)
188 netif_dbg(efx, drv, efx->net_dev, "init port\n");
190 mutex_lock(&efx->mac_lock);
192 rc = efx->phy_op->init(efx);
196 efx->port_initialized = true;
198 /* Reconfigure the MAC before creating dma queues (required for
199 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
200 efx_mac_reconfigure(efx);
202 /* Ensure the PHY advertises the correct flow control settings */
203 rc = efx->phy_op->reconfigure(efx);
204 if (rc && rc != -EPERM)
207 mutex_unlock(&efx->mac_lock);
211 efx->phy_op->fini(efx);
213 mutex_unlock(&efx->mac_lock);
217 static void efx_fini_port(struct efx_nic *efx)
219 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
221 if (!efx->port_initialized)
224 efx->phy_op->fini(efx);
225 efx->port_initialized = false;
227 efx->link_state.up = false;
228 efx_link_status_changed(efx);
231 static void efx_remove_port(struct efx_nic *efx)
233 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
235 efx->type->remove_port(efx);
238 /**************************************************************************
242 **************************************************************************/
244 static LIST_HEAD(efx_primary_list);
245 static LIST_HEAD(efx_unassociated_list);
247 static bool efx_same_controller(struct efx_nic *left, struct efx_nic *right)
249 return left->type == right->type &&
250 left->vpd_sn && right->vpd_sn &&
251 !strcmp(left->vpd_sn, right->vpd_sn);
254 static void efx_associate(struct efx_nic *efx)
256 struct efx_nic *other, *next;
258 if (efx->primary == efx) {
259 /* Adding primary function; look for secondaries */
261 netif_dbg(efx, probe, efx->net_dev, "adding to primary list\n");
262 list_add_tail(&efx->node, &efx_primary_list);
264 list_for_each_entry_safe(other, next, &efx_unassociated_list,
266 if (efx_same_controller(efx, other)) {
267 list_del(&other->node);
268 netif_dbg(other, probe, other->net_dev,
269 "moving to secondary list of %s %s\n",
270 pci_name(efx->pci_dev),
272 list_add_tail(&other->node,
273 &efx->secondary_list);
274 other->primary = efx;
278 /* Adding secondary function; look for primary */
280 list_for_each_entry(other, &efx_primary_list, node) {
281 if (efx_same_controller(efx, other)) {
282 netif_dbg(efx, probe, efx->net_dev,
283 "adding to secondary list of %s %s\n",
284 pci_name(other->pci_dev),
285 other->net_dev->name);
286 list_add_tail(&efx->node,
287 &other->secondary_list);
288 efx->primary = other;
293 netif_dbg(efx, probe, efx->net_dev,
294 "adding to unassociated list\n");
295 list_add_tail(&efx->node, &efx_unassociated_list);
299 static void efx_dissociate(struct efx_nic *efx)
301 struct efx_nic *other, *next;
303 list_del(&efx->node);
306 list_for_each_entry_safe(other, next, &efx->secondary_list, node) {
307 list_del(&other->node);
308 netif_dbg(other, probe, other->net_dev,
309 "moving to unassociated list\n");
310 list_add_tail(&other->node, &efx_unassociated_list);
311 other->primary = NULL;
315 static int efx_probe_nic(struct efx_nic *efx)
319 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
321 /* Carry out hardware-type specific initialisation */
322 rc = efx->type->probe(efx);
327 if (!efx->max_channels || !efx->max_tx_channels) {
328 netif_err(efx, drv, efx->net_dev,
329 "Insufficient resources to allocate"
335 /* Determine the number of channels and queues by trying
336 * to hook in MSI-X interrupts.
338 rc = efx_probe_interrupts(efx);
342 rc = efx_set_channels(efx);
346 /* dimension_resources can fail with EAGAIN */
347 rc = efx->type->dimension_resources(efx);
348 if (rc != 0 && rc != -EAGAIN)
352 /* try again with new max_channels */
353 efx_remove_interrupts(efx);
355 } while (rc == -EAGAIN);
357 if (efx->n_channels > 1)
358 netdev_rss_key_fill(efx->rss_context.rx_hash_key,
359 sizeof(efx->rss_context.rx_hash_key));
360 efx_set_default_rx_indir_table(efx, &efx->rss_context);
362 netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
363 netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
365 /* Initialise the interrupt moderation settings */
366 efx->irq_mod_step_us = DIV_ROUND_UP(efx->timer_quantum_ns, 1000);
367 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
373 efx_remove_interrupts(efx);
375 efx->type->remove(efx);
379 static void efx_remove_nic(struct efx_nic *efx)
381 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
383 efx_remove_interrupts(efx);
384 efx->type->remove(efx);
387 /**************************************************************************
389 * NIC startup/shutdown
391 *************************************************************************/
393 static int efx_probe_all(struct efx_nic *efx)
397 rc = efx_probe_nic(efx);
399 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
403 rc = efx_probe_port(efx);
405 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
409 BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
410 if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
414 efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
416 #ifdef CONFIG_SFC_SRIOV
417 rc = efx->type->vswitching_probe(efx);
418 if (rc) /* not fatal; the PF will still work fine */
419 netif_warn(efx, probe, efx->net_dev,
420 "failed to setup vswitching rc=%d;"
421 " VFs may not function\n", rc);
424 rc = efx_probe_filters(efx);
426 netif_err(efx, probe, efx->net_dev,
427 "failed to create filter tables\n");
431 rc = efx_probe_channels(efx);
438 efx_remove_filters(efx);
440 #ifdef CONFIG_SFC_SRIOV
441 efx->type->vswitching_remove(efx);
444 efx_remove_port(efx);
451 static void efx_remove_all(struct efx_nic *efx)
454 efx_xdp_setup_prog(efx, NULL);
457 efx_remove_channels(efx);
458 efx_remove_filters(efx);
459 #ifdef CONFIG_SFC_SRIOV
460 efx->type->vswitching_remove(efx);
462 efx_remove_port(efx);
466 /**************************************************************************
468 * Interrupt moderation
470 **************************************************************************/
471 unsigned int efx_usecs_to_ticks(struct efx_nic *efx, unsigned int usecs)
475 if (usecs * 1000 < efx->timer_quantum_ns)
476 return 1; /* never round down to 0 */
477 return usecs * 1000 / efx->timer_quantum_ns;
480 unsigned int efx_ticks_to_usecs(struct efx_nic *efx, unsigned int ticks)
482 /* We must round up when converting ticks to microseconds
483 * because we round down when converting the other way.
485 return DIV_ROUND_UP(ticks * efx->timer_quantum_ns, 1000);
488 /* Set interrupt moderation parameters */
489 int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
490 unsigned int rx_usecs, bool rx_adaptive,
491 bool rx_may_override_tx)
493 struct efx_channel *channel;
494 unsigned int timer_max_us;
496 EFX_ASSERT_RESET_SERIALISED(efx);
498 timer_max_us = efx->timer_max_ns / 1000;
500 if (tx_usecs > timer_max_us || rx_usecs > timer_max_us)
503 if (tx_usecs != rx_usecs && efx->tx_channel_offset == 0 &&
504 !rx_may_override_tx) {
505 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
506 "RX and TX IRQ moderation must be equal\n");
510 efx->irq_rx_adaptive = rx_adaptive;
511 efx->irq_rx_moderation_us = rx_usecs;
512 efx_for_each_channel(channel, efx) {
513 if (efx_channel_has_rx_queue(channel))
514 channel->irq_moderation_us = rx_usecs;
515 else if (efx_channel_has_tx_queues(channel))
516 channel->irq_moderation_us = tx_usecs;
517 else if (efx_channel_is_xdp_tx(channel))
518 channel->irq_moderation_us = tx_usecs;
524 void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
525 unsigned int *rx_usecs, bool *rx_adaptive)
527 *rx_adaptive = efx->irq_rx_adaptive;
528 *rx_usecs = efx->irq_rx_moderation_us;
530 /* If channels are shared between RX and TX, so is IRQ
531 * moderation. Otherwise, IRQ moderation is the same for all
532 * TX channels and is not adaptive.
534 if (efx->tx_channel_offset == 0) {
535 *tx_usecs = *rx_usecs;
537 struct efx_channel *tx_channel;
539 tx_channel = efx->channel[efx->tx_channel_offset];
540 *tx_usecs = tx_channel->irq_moderation_us;
544 /**************************************************************************
548 *************************************************************************/
551 * Context: process, rtnl_lock() held.
553 static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
555 struct efx_nic *efx = netdev_priv(net_dev);
556 struct mii_ioctl_data *data = if_mii(ifr);
558 if (cmd == SIOCSHWTSTAMP)
559 return efx_ptp_set_ts_config(efx, ifr);
560 if (cmd == SIOCGHWTSTAMP)
561 return efx_ptp_get_ts_config(efx, ifr);
563 /* Convert phy_id from older PRTAD/DEVAD format */
564 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
565 (data->phy_id & 0xfc00) == 0x0400)
566 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
568 return mdio_mii_ioctl(&efx->mdio, data, cmd);
571 /**************************************************************************
573 * Kernel net device interface
575 *************************************************************************/
577 /* Context: process, rtnl_lock() held. */
578 int efx_net_open(struct net_device *net_dev)
580 struct efx_nic *efx = netdev_priv(net_dev);
583 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
584 raw_smp_processor_id());
586 rc = efx_check_disabled(efx);
589 if (efx->phy_mode & PHY_MODE_SPECIAL)
591 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
594 /* Notify the kernel of the link state polled during driver load,
595 * before the monitor starts running */
596 efx_link_status_changed(efx);
599 if (efx->state == STATE_DISABLED || efx->reset_pending)
600 netif_device_detach(efx->net_dev);
601 efx_selftest_async_start(efx);
605 /* Context: process, rtnl_lock() held.
606 * Note that the kernel will ignore our return code; this method
607 * should really be a void.
609 int efx_net_stop(struct net_device *net_dev)
611 struct efx_nic *efx = netdev_priv(net_dev);
613 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
614 raw_smp_processor_id());
616 /* Stop the device and flush all the channels */
622 /* Context: netif_tx_lock held, BHs disabled. */
623 static void efx_watchdog(struct net_device *net_dev, unsigned int txqueue)
625 struct efx_nic *efx = netdev_priv(net_dev);
627 netif_err(efx, tx_err, efx->net_dev,
628 "TX stuck with port_enabled=%d: resetting channels\n",
631 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
634 static int efx_set_mac_address(struct net_device *net_dev, void *data)
636 struct efx_nic *efx = netdev_priv(net_dev);
637 struct sockaddr *addr = data;
638 u8 *new_addr = addr->sa_data;
642 if (!is_valid_ether_addr(new_addr)) {
643 netif_err(efx, drv, efx->net_dev,
644 "invalid ethernet MAC address requested: %pM\n",
646 return -EADDRNOTAVAIL;
649 /* save old address */
650 ether_addr_copy(old_addr, net_dev->dev_addr);
651 ether_addr_copy(net_dev->dev_addr, new_addr);
652 if (efx->type->set_mac_address) {
653 rc = efx->type->set_mac_address(efx);
655 ether_addr_copy(net_dev->dev_addr, old_addr);
660 /* Reconfigure the MAC */
661 mutex_lock(&efx->mac_lock);
662 efx_mac_reconfigure(efx);
663 mutex_unlock(&efx->mac_lock);
668 /* Context: netif_addr_lock held, BHs disabled. */
669 static void efx_set_rx_mode(struct net_device *net_dev)
671 struct efx_nic *efx = netdev_priv(net_dev);
673 if (efx->port_enabled)
674 queue_work(efx->workqueue, &efx->mac_work);
675 /* Otherwise efx_start_port() will do this */
678 static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
680 struct efx_nic *efx = netdev_priv(net_dev);
683 /* If disabling RX n-tuple filtering, clear existing filters */
684 if (net_dev->features & ~data & NETIF_F_NTUPLE) {
685 rc = efx->type->filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
690 /* If Rx VLAN filter is changed, update filters via mac_reconfigure.
691 * If rx-fcs is changed, mac_reconfigure updates that too.
693 if ((net_dev->features ^ data) & (NETIF_F_HW_VLAN_CTAG_FILTER |
695 /* efx_set_rx_mode() will schedule MAC work to update filters
696 * when a new features are finally set in net_dev.
698 efx_set_rx_mode(net_dev);
704 static int efx_get_phys_port_id(struct net_device *net_dev,
705 struct netdev_phys_item_id *ppid)
707 struct efx_nic *efx = netdev_priv(net_dev);
709 if (efx->type->get_phys_port_id)
710 return efx->type->get_phys_port_id(efx, ppid);
715 static int efx_get_phys_port_name(struct net_device *net_dev,
716 char *name, size_t len)
718 struct efx_nic *efx = netdev_priv(net_dev);
720 if (snprintf(name, len, "p%u", efx->port_num) >= len)
725 static int efx_vlan_rx_add_vid(struct net_device *net_dev, __be16 proto, u16 vid)
727 struct efx_nic *efx = netdev_priv(net_dev);
729 if (efx->type->vlan_rx_add_vid)
730 return efx->type->vlan_rx_add_vid(efx, proto, vid);
735 static int efx_vlan_rx_kill_vid(struct net_device *net_dev, __be16 proto, u16 vid)
737 struct efx_nic *efx = netdev_priv(net_dev);
739 if (efx->type->vlan_rx_kill_vid)
740 return efx->type->vlan_rx_kill_vid(efx, proto, vid);
745 static int efx_udp_tunnel_type_map(enum udp_parsable_tunnel_type in)
748 case UDP_TUNNEL_TYPE_VXLAN:
749 return TUNNEL_ENCAP_UDP_PORT_ENTRY_VXLAN;
750 case UDP_TUNNEL_TYPE_GENEVE:
751 return TUNNEL_ENCAP_UDP_PORT_ENTRY_GENEVE;
757 static void efx_udp_tunnel_add(struct net_device *dev, struct udp_tunnel_info *ti)
759 struct efx_nic *efx = netdev_priv(dev);
760 struct efx_udp_tunnel tnl;
763 efx_tunnel_type = efx_udp_tunnel_type_map(ti->type);
764 if (efx_tunnel_type < 0)
767 tnl.type = (u16)efx_tunnel_type;
770 if (efx->type->udp_tnl_add_port)
771 (void)efx->type->udp_tnl_add_port(efx, tnl);
774 static void efx_udp_tunnel_del(struct net_device *dev, struct udp_tunnel_info *ti)
776 struct efx_nic *efx = netdev_priv(dev);
777 struct efx_udp_tunnel tnl;
780 efx_tunnel_type = efx_udp_tunnel_type_map(ti->type);
781 if (efx_tunnel_type < 0)
784 tnl.type = (u16)efx_tunnel_type;
787 if (efx->type->udp_tnl_del_port)
788 (void)efx->type->udp_tnl_del_port(efx, tnl);
791 static const struct net_device_ops efx_netdev_ops = {
792 .ndo_open = efx_net_open,
793 .ndo_stop = efx_net_stop,
794 .ndo_get_stats64 = efx_net_stats,
795 .ndo_tx_timeout = efx_watchdog,
796 .ndo_start_xmit = efx_hard_start_xmit,
797 .ndo_validate_addr = eth_validate_addr,
798 .ndo_do_ioctl = efx_ioctl,
799 .ndo_change_mtu = efx_change_mtu,
800 .ndo_set_mac_address = efx_set_mac_address,
801 .ndo_set_rx_mode = efx_set_rx_mode,
802 .ndo_set_features = efx_set_features,
803 .ndo_vlan_rx_add_vid = efx_vlan_rx_add_vid,
804 .ndo_vlan_rx_kill_vid = efx_vlan_rx_kill_vid,
805 #ifdef CONFIG_SFC_SRIOV
806 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
807 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
808 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
809 .ndo_get_vf_config = efx_sriov_get_vf_config,
810 .ndo_set_vf_link_state = efx_sriov_set_vf_link_state,
812 .ndo_get_phys_port_id = efx_get_phys_port_id,
813 .ndo_get_phys_port_name = efx_get_phys_port_name,
814 .ndo_setup_tc = efx_setup_tc,
815 #ifdef CONFIG_RFS_ACCEL
816 .ndo_rx_flow_steer = efx_filter_rfs,
818 .ndo_udp_tunnel_add = efx_udp_tunnel_add,
819 .ndo_udp_tunnel_del = efx_udp_tunnel_del,
820 .ndo_xdp_xmit = efx_xdp_xmit,
824 static int efx_xdp_setup_prog(struct efx_nic *efx, struct bpf_prog *prog)
826 struct bpf_prog *old_prog;
828 if (efx->xdp_rxq_info_failed) {
829 netif_err(efx, drv, efx->net_dev,
830 "Unable to bind XDP program due to previous failure of rxq_info\n");
834 if (prog && efx->net_dev->mtu > efx_xdp_max_mtu(efx)) {
835 netif_err(efx, drv, efx->net_dev,
836 "Unable to configure XDP with MTU of %d (max: %d)\n",
837 efx->net_dev->mtu, efx_xdp_max_mtu(efx));
841 old_prog = rtnl_dereference(efx->xdp_prog);
842 rcu_assign_pointer(efx->xdp_prog, prog);
843 /* Release the reference that was originally passed by the caller. */
845 bpf_prog_put(old_prog);
850 /* Context: process, rtnl_lock() held. */
851 static int efx_xdp(struct net_device *dev, struct netdev_bpf *xdp)
853 struct efx_nic *efx = netdev_priv(dev);
854 struct bpf_prog *xdp_prog;
856 switch (xdp->command) {
858 return efx_xdp_setup_prog(efx, xdp->prog);
860 xdp_prog = rtnl_dereference(efx->xdp_prog);
861 xdp->prog_id = xdp_prog ? xdp_prog->aux->id : 0;
868 static int efx_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **xdpfs,
871 struct efx_nic *efx = netdev_priv(dev);
873 if (!netif_running(dev))
876 return efx_xdp_tx_buffers(efx, n, xdpfs, flags & XDP_XMIT_FLUSH);
879 static void efx_update_name(struct efx_nic *efx)
881 strcpy(efx->name, efx->net_dev->name);
883 efx_set_channel_names(efx);
886 static int efx_netdev_event(struct notifier_block *this,
887 unsigned long event, void *ptr)
889 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
891 if ((net_dev->netdev_ops == &efx_netdev_ops) &&
892 event == NETDEV_CHANGENAME)
893 efx_update_name(netdev_priv(net_dev));
898 static struct notifier_block efx_netdev_notifier = {
899 .notifier_call = efx_netdev_event,
903 show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
905 struct efx_nic *efx = dev_get_drvdata(dev);
906 return sprintf(buf, "%d\n", efx->phy_type);
908 static DEVICE_ATTR(phy_type, 0444, show_phy_type, NULL);
910 static int efx_register_netdev(struct efx_nic *efx)
912 struct net_device *net_dev = efx->net_dev;
913 struct efx_channel *channel;
916 net_dev->watchdog_timeo = 5 * HZ;
917 net_dev->irq = efx->pci_dev->irq;
918 net_dev->netdev_ops = &efx_netdev_ops;
919 if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0)
920 net_dev->priv_flags |= IFF_UNICAST_FLT;
921 net_dev->ethtool_ops = &efx_ethtool_ops;
922 net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
923 net_dev->min_mtu = EFX_MIN_MTU;
924 net_dev->max_mtu = EFX_MAX_MTU;
928 /* Enable resets to be scheduled and check whether any were
929 * already requested. If so, the NIC is probably hosed so we
932 efx->state = STATE_READY;
933 smp_mb(); /* ensure we change state before checking reset_pending */
934 if (efx->reset_pending) {
935 netif_err(efx, probe, efx->net_dev,
936 "aborting probe due to scheduled reset\n");
941 rc = dev_alloc_name(net_dev, net_dev->name);
944 efx_update_name(efx);
946 /* Always start with carrier off; PHY events will detect the link */
947 netif_carrier_off(net_dev);
949 rc = register_netdevice(net_dev);
953 efx_for_each_channel(channel, efx) {
954 struct efx_tx_queue *tx_queue;
955 efx_for_each_channel_tx_queue(tx_queue, channel)
956 efx_init_tx_queue_core_txq(tx_queue);
963 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
965 netif_err(efx, drv, efx->net_dev,
966 "failed to init net dev attributes\n");
967 goto fail_registered;
970 efx_init_mcdi_logging(efx);
977 unregister_netdevice(net_dev);
979 efx->state = STATE_UNINIT;
981 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
985 static void efx_unregister_netdev(struct efx_nic *efx)
990 BUG_ON(netdev_priv(efx->net_dev) != efx);
992 if (efx_dev_registered(efx)) {
993 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
994 efx_fini_mcdi_logging(efx);
995 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
996 unregister_netdev(efx->net_dev);
1000 /**************************************************************************
1002 * List of NICs we support
1004 **************************************************************************/
1006 /* PCI device ID table */
1007 static const struct pci_device_id efx_pci_table[] = {
1008 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
1009 .driver_data = (unsigned long) &siena_a0_nic_type},
1010 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
1011 .driver_data = (unsigned long) &siena_a0_nic_type},
1012 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0903), /* SFC9120 PF */
1013 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
1014 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1903), /* SFC9120 VF */
1015 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
1016 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0923), /* SFC9140 PF */
1017 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
1018 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1923), /* SFC9140 VF */
1019 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
1020 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0a03), /* SFC9220 PF */
1021 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
1022 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1a03), /* SFC9220 VF */
1023 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
1024 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0b03), /* SFC9250 PF */
1025 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
1026 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1b03), /* SFC9250 VF */
1027 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
1028 {0} /* end of list */
1031 /**************************************************************************
1035 **************************************************************************/
1037 void efx_update_sw_stats(struct efx_nic *efx, u64 *stats)
1039 u64 n_rx_nodesc_trunc = 0;
1040 struct efx_channel *channel;
1042 efx_for_each_channel(channel, efx)
1043 n_rx_nodesc_trunc += channel->n_rx_nodesc_trunc;
1044 stats[GENERIC_STAT_rx_nodesc_trunc] = n_rx_nodesc_trunc;
1045 stats[GENERIC_STAT_rx_noskb_drops] = atomic_read(&efx->n_rx_noskb_drops);
1048 /**************************************************************************
1052 **************************************************************************/
1054 /* Main body of final NIC shutdown code
1055 * This is called only at module unload (or hotplug removal).
1057 static void efx_pci_remove_main(struct efx_nic *efx)
1059 /* Flush reset_work. It can no longer be scheduled since we
1062 BUG_ON(efx->state == STATE_READY);
1063 efx_flush_reset_workqueue(efx);
1065 efx_disable_interrupts(efx);
1066 efx_clear_interrupt_affinity(efx);
1067 efx_nic_fini_interrupt(efx);
1069 efx->type->fini(efx);
1071 efx_remove_all(efx);
1074 /* Final NIC shutdown
1075 * This is called only at module unload (or hotplug removal). A PF can call
1076 * this on its VFs to ensure they are unbound first.
1078 static void efx_pci_remove(struct pci_dev *pci_dev)
1080 struct efx_nic *efx;
1082 efx = pci_get_drvdata(pci_dev);
1086 /* Mark the NIC as fini, then stop the interface */
1088 efx_dissociate(efx);
1089 dev_close(efx->net_dev);
1090 efx_disable_interrupts(efx);
1091 efx->state = STATE_UNINIT;
1094 if (efx->type->sriov_fini)
1095 efx->type->sriov_fini(efx);
1097 efx_unregister_netdev(efx);
1099 efx_mtd_remove(efx);
1101 efx_pci_remove_main(efx);
1103 efx_fini_io(efx, efx->type->mem_bar(efx));
1104 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
1106 efx_fini_struct(efx);
1107 free_netdev(efx->net_dev);
1109 pci_disable_pcie_error_reporting(pci_dev);
1112 /* NIC VPD information
1113 * Called during probe to display the part number of the
1114 * installed NIC. VPD is potentially very large but this should
1115 * always appear within the first 512 bytes.
1117 #define SFC_VPD_LEN 512
1118 static void efx_probe_vpd_strings(struct efx_nic *efx)
1120 struct pci_dev *dev = efx->pci_dev;
1121 char vpd_data[SFC_VPD_LEN];
1123 int ro_start, ro_size, i, j;
1125 /* Get the vpd data from the device */
1126 vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
1127 if (vpd_size <= 0) {
1128 netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
1132 /* Get the Read only section */
1133 ro_start = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
1135 netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
1139 ro_size = pci_vpd_lrdt_size(&vpd_data[ro_start]);
1141 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
1142 if (i + j > vpd_size)
1145 /* Get the Part number */
1146 i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
1148 netif_err(efx, drv, efx->net_dev, "Part number not found\n");
1152 j = pci_vpd_info_field_size(&vpd_data[i]);
1153 i += PCI_VPD_INFO_FLD_HDR_SIZE;
1154 if (i + j > vpd_size) {
1155 netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
1159 netif_info(efx, drv, efx->net_dev,
1160 "Part Number : %.*s\n", j, &vpd_data[i]);
1162 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
1164 i = pci_vpd_find_info_keyword(vpd_data, i, j, "SN");
1166 netif_err(efx, drv, efx->net_dev, "Serial number not found\n");
1170 j = pci_vpd_info_field_size(&vpd_data[i]);
1171 i += PCI_VPD_INFO_FLD_HDR_SIZE;
1172 if (i + j > vpd_size) {
1173 netif_err(efx, drv, efx->net_dev, "Incomplete serial number\n");
1177 efx->vpd_sn = kmalloc(j + 1, GFP_KERNEL);
1181 snprintf(efx->vpd_sn, j + 1, "%s", &vpd_data[i]);
1185 /* Main body of NIC initialisation
1186 * This is called at module load (or hotplug insertion, theoretically).
1188 static int efx_pci_probe_main(struct efx_nic *efx)
1192 /* Do start-of-day initialisation */
1193 rc = efx_probe_all(efx);
1199 down_write(&efx->filter_sem);
1200 rc = efx->type->init(efx);
1201 up_write(&efx->filter_sem);
1203 netif_err(efx, probe, efx->net_dev,
1204 "failed to initialise NIC\n");
1208 rc = efx_init_port(efx);
1210 netif_err(efx, probe, efx->net_dev,
1211 "failed to initialise port\n");
1215 rc = efx_nic_init_interrupt(efx);
1219 efx_set_interrupt_affinity(efx);
1220 rc = efx_enable_interrupts(efx);
1227 efx_clear_interrupt_affinity(efx);
1228 efx_nic_fini_interrupt(efx);
1232 efx->type->fini(efx);
1235 efx_remove_all(efx);
1240 static int efx_pci_probe_post_io(struct efx_nic *efx)
1242 struct net_device *net_dev = efx->net_dev;
1243 int rc = efx_pci_probe_main(efx);
1248 if (efx->type->sriov_init) {
1249 rc = efx->type->sriov_init(efx);
1251 netif_err(efx, probe, efx->net_dev,
1252 "SR-IOV can't be enabled rc %d\n", rc);
1255 /* Determine netdevice features */
1256 net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
1257 NETIF_F_TSO | NETIF_F_RXCSUM | NETIF_F_RXALL);
1258 if (efx->type->offload_features & (NETIF_F_IPV6_CSUM | NETIF_F_HW_CSUM))
1259 net_dev->features |= NETIF_F_TSO6;
1260 /* Check whether device supports TSO */
1261 if (!efx->type->tso_versions || !efx->type->tso_versions(efx))
1262 net_dev->features &= ~NETIF_F_ALL_TSO;
1263 /* Mask for features that also apply to VLAN devices */
1264 net_dev->vlan_features |= (NETIF_F_HW_CSUM | NETIF_F_SG |
1265 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
1268 net_dev->hw_features |= net_dev->features & ~efx->fixed_features;
1270 /* Disable receiving frames with bad FCS, by default. */
1271 net_dev->features &= ~NETIF_F_RXALL;
1273 /* Disable VLAN filtering by default. It may be enforced if
1274 * the feature is fixed (i.e. VLAN filters are required to
1275 * receive VLAN tagged packets due to vPort restrictions).
1277 net_dev->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1278 net_dev->features |= efx->fixed_features;
1280 rc = efx_register_netdev(efx);
1284 efx_pci_remove_main(efx);
1288 /* NIC initialisation
1290 * This is called at module load (or hotplug insertion,
1291 * theoretically). It sets up PCI mappings, resets the NIC,
1292 * sets up and registers the network devices with the kernel and hooks
1293 * the interrupt service routine. It does not prepare the device for
1294 * transmission; this is left to the first time one of the network
1295 * interfaces is brought up (i.e. efx_net_open).
1297 static int efx_pci_probe(struct pci_dev *pci_dev,
1298 const struct pci_device_id *entry)
1300 struct net_device *net_dev;
1301 struct efx_nic *efx;
1304 /* Allocate and initialise a struct net_device and struct efx_nic */
1305 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
1309 efx = netdev_priv(net_dev);
1310 efx->type = (const struct efx_nic_type *) entry->driver_data;
1311 efx->fixed_features |= NETIF_F_HIGHDMA;
1313 pci_set_drvdata(pci_dev, efx);
1314 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
1315 rc = efx_init_struct(efx, pci_dev, net_dev);
1319 netif_info(efx, probe, efx->net_dev,
1320 "Solarflare NIC detected\n");
1322 if (!efx->type->is_vf)
1323 efx_probe_vpd_strings(efx);
1325 /* Set up basic I/O (BAR mappings etc) */
1326 rc = efx_init_io(efx, efx->type->mem_bar(efx), efx->type->max_dma_mask,
1327 efx->type->mem_map_size(efx));
1331 rc = efx_pci_probe_post_io(efx);
1333 /* On failure, retry once immediately.
1334 * If we aborted probe due to a scheduled reset, dismiss it.
1336 efx->reset_pending = 0;
1337 rc = efx_pci_probe_post_io(efx);
1339 /* On another failure, retry once more
1340 * after a 50-305ms delay.
1344 get_random_bytes(&r, 1);
1345 msleep((unsigned int)r + 50);
1346 efx->reset_pending = 0;
1347 rc = efx_pci_probe_post_io(efx);
1353 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
1355 /* Try to create MTDs, but allow this to fail */
1357 rc = efx_mtd_probe(efx);
1359 if (rc && rc != -EPERM)
1360 netif_warn(efx, probe, efx->net_dev,
1361 "failed to create MTDs (%d)\n", rc);
1363 (void)pci_enable_pcie_error_reporting(pci_dev);
1365 if (efx->type->udp_tnl_push_ports)
1366 efx->type->udp_tnl_push_ports(efx);
1371 efx_fini_io(efx, efx->type->mem_bar(efx));
1373 efx_fini_struct(efx);
1376 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
1377 free_netdev(net_dev);
1381 /* efx_pci_sriov_configure returns the actual number of Virtual Functions
1382 * enabled on success
1384 #ifdef CONFIG_SFC_SRIOV
1385 static int efx_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
1388 struct efx_nic *efx = pci_get_drvdata(dev);
1390 if (efx->type->sriov_configure) {
1391 rc = efx->type->sriov_configure(efx, num_vfs);
1401 static int efx_pm_freeze(struct device *dev)
1403 struct efx_nic *efx = dev_get_drvdata(dev);
1407 if (efx->state != STATE_DISABLED) {
1408 efx->state = STATE_UNINIT;
1410 efx_device_detach_sync(efx);
1413 efx_disable_interrupts(efx);
1421 static int efx_pm_thaw(struct device *dev)
1424 struct efx_nic *efx = dev_get_drvdata(dev);
1428 if (efx->state != STATE_DISABLED) {
1429 rc = efx_enable_interrupts(efx);
1433 mutex_lock(&efx->mac_lock);
1434 efx->phy_op->reconfigure(efx);
1435 mutex_unlock(&efx->mac_lock);
1439 efx_device_attach_if_not_resetting(efx);
1441 efx->state = STATE_READY;
1443 efx->type->resume_wol(efx);
1448 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
1449 efx_queue_reset_work(efx);
1459 static int efx_pm_poweroff(struct device *dev)
1461 struct pci_dev *pci_dev = to_pci_dev(dev);
1462 struct efx_nic *efx = pci_get_drvdata(pci_dev);
1464 efx->type->fini(efx);
1466 efx->reset_pending = 0;
1468 pci_save_state(pci_dev);
1469 return pci_set_power_state(pci_dev, PCI_D3hot);
1472 /* Used for both resume and restore */
1473 static int efx_pm_resume(struct device *dev)
1475 struct pci_dev *pci_dev = to_pci_dev(dev);
1476 struct efx_nic *efx = pci_get_drvdata(pci_dev);
1479 rc = pci_set_power_state(pci_dev, PCI_D0);
1482 pci_restore_state(pci_dev);
1483 rc = pci_enable_device(pci_dev);
1486 pci_set_master(efx->pci_dev);
1487 rc = efx->type->reset(efx, RESET_TYPE_ALL);
1490 down_write(&efx->filter_sem);
1491 rc = efx->type->init(efx);
1492 up_write(&efx->filter_sem);
1495 rc = efx_pm_thaw(dev);
1499 static int efx_pm_suspend(struct device *dev)
1504 rc = efx_pm_poweroff(dev);
1510 static const struct dev_pm_ops efx_pm_ops = {
1511 .suspend = efx_pm_suspend,
1512 .resume = efx_pm_resume,
1513 .freeze = efx_pm_freeze,
1514 .thaw = efx_pm_thaw,
1515 .poweroff = efx_pm_poweroff,
1516 .restore = efx_pm_resume,
1519 /* A PCI error affecting this device was detected.
1520 * At this point MMIO and DMA may be disabled.
1521 * Stop the software path and request a slot reset.
1523 static pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
1524 enum pci_channel_state state)
1526 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
1527 struct efx_nic *efx = pci_get_drvdata(pdev);
1529 if (state == pci_channel_io_perm_failure)
1530 return PCI_ERS_RESULT_DISCONNECT;
1534 if (efx->state != STATE_DISABLED) {
1535 efx->state = STATE_RECOVERY;
1536 efx->reset_pending = 0;
1538 efx_device_detach_sync(efx);
1541 efx_disable_interrupts(efx);
1543 status = PCI_ERS_RESULT_NEED_RESET;
1545 /* If the interface is disabled we don't want to do anything
1548 status = PCI_ERS_RESULT_RECOVERED;
1553 pci_disable_device(pdev);
1558 /* Fake a successful reset, which will be performed later in efx_io_resume. */
1559 static pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
1561 struct efx_nic *efx = pci_get_drvdata(pdev);
1562 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
1564 if (pci_enable_device(pdev)) {
1565 netif_err(efx, hw, efx->net_dev,
1566 "Cannot re-enable PCI device after reset.\n");
1567 status = PCI_ERS_RESULT_DISCONNECT;
1573 /* Perform the actual reset and resume I/O operations. */
1574 static void efx_io_resume(struct pci_dev *pdev)
1576 struct efx_nic *efx = pci_get_drvdata(pdev);
1581 if (efx->state == STATE_DISABLED)
1584 rc = efx_reset(efx, RESET_TYPE_ALL);
1586 netif_err(efx, hw, efx->net_dev,
1587 "efx_reset failed after PCI error (%d)\n", rc);
1589 efx->state = STATE_READY;
1590 netif_dbg(efx, hw, efx->net_dev,
1591 "Done resetting and resuming IO after PCI error.\n");
1598 /* For simplicity and reliability, we always require a slot reset and try to
1599 * reset the hardware when a pci error affecting the device is detected.
1600 * We leave both the link_reset and mmio_enabled callback unimplemented:
1601 * with our request for slot reset the mmio_enabled callback will never be
1602 * called, and the link_reset callback is not used by AER or EEH mechanisms.
1604 static const struct pci_error_handlers efx_err_handlers = {
1605 .error_detected = efx_io_error_detected,
1606 .slot_reset = efx_io_slot_reset,
1607 .resume = efx_io_resume,
1610 static struct pci_driver efx_pci_driver = {
1611 .name = KBUILD_MODNAME,
1612 .id_table = efx_pci_table,
1613 .probe = efx_pci_probe,
1614 .remove = efx_pci_remove,
1615 .driver.pm = &efx_pm_ops,
1616 .err_handler = &efx_err_handlers,
1617 #ifdef CONFIG_SFC_SRIOV
1618 .sriov_configure = efx_pci_sriov_configure,
1622 /**************************************************************************
1624 * Kernel module interface
1626 *************************************************************************/
1628 static int __init efx_init_module(void)
1632 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
1634 rc = register_netdevice_notifier(&efx_netdev_notifier);
1638 #ifdef CONFIG_SFC_SRIOV
1639 rc = efx_init_sriov();
1644 rc = efx_create_reset_workqueue();
1648 rc = pci_register_driver(&efx_pci_driver);
1655 efx_destroy_reset_workqueue();
1657 #ifdef CONFIG_SFC_SRIOV
1661 unregister_netdevice_notifier(&efx_netdev_notifier);
1666 static void __exit efx_exit_module(void)
1668 printk(KERN_INFO "Solarflare NET driver unloading\n");
1670 pci_unregister_driver(&efx_pci_driver);
1671 efx_destroy_reset_workqueue();
1672 #ifdef CONFIG_SFC_SRIOV
1675 unregister_netdevice_notifier(&efx_netdev_notifier);
1679 module_init(efx_init_module);
1680 module_exit(efx_exit_module);
1682 MODULE_AUTHOR("Solarflare Communications and "
1683 "Michael Brown <mbrown@fensystems.co.uk>");
1684 MODULE_DESCRIPTION("Solarflare network driver");
1685 MODULE_LICENSE("GPL");
1686 MODULE_DEVICE_TABLE(pci, efx_pci_table);
1687 MODULE_VERSION(EFX_DRIVER_VERSION);