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 /* This is the weight assigned to each of the (per-channel) virtual
83 static int napi_weight = 64;
85 /* Initial interrupt moderation settings. They can be modified after
86 * module load with ethtool.
88 * The default for RX should strike a balance between increasing the
89 * round-trip latency and reducing overhead.
91 static unsigned int rx_irq_mod_usec = 60;
93 /* Initial interrupt moderation settings. They can be modified after
94 * module load with ethtool.
96 * This default is chosen to ensure that a 10G link does not go idle
97 * while a TX queue is stopped after it has become full. A queue is
98 * restarted when it drops below half full. The time this takes (assuming
99 * worst case 3 descriptors per packet and 1024 descriptors) is
100 * 512 / 3 * 1.2 = 205 usec.
102 static unsigned int tx_irq_mod_usec = 150;
104 /* This is the first interrupt mode to try out of:
109 static unsigned int interrupt_mode;
111 /* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
112 * i.e. the number of CPUs among which we may distribute simultaneous
113 * interrupt handling.
115 * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
116 * The default (0) means to assign an interrupt to each core.
118 static unsigned int rss_cpus;
119 module_param(rss_cpus, uint, 0444);
120 MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
122 static bool phy_flash_cfg;
123 module_param(phy_flash_cfg, bool, 0644);
124 MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
126 static unsigned irq_adapt_low_thresh = 8000;
127 module_param(irq_adapt_low_thresh, uint, 0644);
128 MODULE_PARM_DESC(irq_adapt_low_thresh,
129 "Threshold score for reducing IRQ moderation");
131 static unsigned irq_adapt_high_thresh = 16000;
132 module_param(irq_adapt_high_thresh, uint, 0644);
133 MODULE_PARM_DESC(irq_adapt_high_thresh,
134 "Threshold score for increasing IRQ moderation");
136 static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
137 NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
138 NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
139 NETIF_MSG_TX_ERR | NETIF_MSG_HW);
140 module_param(debug, uint, 0);
141 MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
143 /**************************************************************************
145 * Utility functions and prototypes
147 *************************************************************************/
149 static const struct efx_channel_type efx_default_channel_type;
150 static void efx_remove_port(struct efx_nic *efx);
151 static int efx_xdp_setup_prog(struct efx_nic *efx, struct bpf_prog *prog);
152 static int efx_xdp(struct net_device *dev, struct netdev_bpf *xdp);
153 static int efx_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **xdpfs,
156 #define EFX_ASSERT_RESET_SERIALISED(efx) \
158 if ((efx->state == STATE_READY) || \
159 (efx->state == STATE_RECOVERY) || \
160 (efx->state == STATE_DISABLED)) \
164 /**************************************************************************
166 * Event queue processing
168 *************************************************************************/
170 /* Process channel's event queue
172 * This function is responsible for processing the event queue of a
173 * single channel. The caller must guarantee that this function will
174 * never be concurrently called more than once on the same channel,
175 * though different channels may be being processed concurrently.
177 static int efx_process_channel(struct efx_channel *channel, int budget)
179 struct efx_tx_queue *tx_queue;
180 struct list_head rx_list;
183 if (unlikely(!channel->enabled))
186 /* Prepare the batch receive list */
187 EFX_WARN_ON_PARANOID(channel->rx_list != NULL);
188 INIT_LIST_HEAD(&rx_list);
189 channel->rx_list = &rx_list;
191 efx_for_each_channel_tx_queue(tx_queue, channel) {
192 tx_queue->pkts_compl = 0;
193 tx_queue->bytes_compl = 0;
196 spent = efx_nic_process_eventq(channel, budget);
197 if (spent && efx_channel_has_rx_queue(channel)) {
198 struct efx_rx_queue *rx_queue =
199 efx_channel_get_rx_queue(channel);
201 efx_rx_flush_packet(channel);
202 efx_fast_push_rx_descriptors(rx_queue, true);
206 efx_for_each_channel_tx_queue(tx_queue, channel) {
207 if (tx_queue->bytes_compl) {
208 netdev_tx_completed_queue(tx_queue->core_txq,
209 tx_queue->pkts_compl, tx_queue->bytes_compl);
213 /* Receive any packets we queued up */
214 netif_receive_skb_list(channel->rx_list);
215 channel->rx_list = NULL;
222 * NAPI guarantees serialisation of polls of the same device, which
223 * provides the guarantee required by efx_process_channel().
225 static void efx_update_irq_mod(struct efx_nic *efx, struct efx_channel *channel)
227 int step = efx->irq_mod_step_us;
229 if (channel->irq_mod_score < irq_adapt_low_thresh) {
230 if (channel->irq_moderation_us > step) {
231 channel->irq_moderation_us -= step;
232 efx->type->push_irq_moderation(channel);
234 } else if (channel->irq_mod_score > irq_adapt_high_thresh) {
235 if (channel->irq_moderation_us <
236 efx->irq_rx_moderation_us) {
237 channel->irq_moderation_us += step;
238 efx->type->push_irq_moderation(channel);
242 channel->irq_count = 0;
243 channel->irq_mod_score = 0;
246 static int efx_poll(struct napi_struct *napi, int budget)
248 struct efx_channel *channel =
249 container_of(napi, struct efx_channel, napi_str);
250 struct efx_nic *efx = channel->efx;
253 netif_vdbg(efx, intr, efx->net_dev,
254 "channel %d NAPI poll executing on CPU %d\n",
255 channel->channel, raw_smp_processor_id());
257 spent = efx_process_channel(channel, budget);
261 if (spent < budget) {
262 if (efx_channel_has_rx_queue(channel) &&
263 efx->irq_rx_adaptive &&
264 unlikely(++channel->irq_count == 1000)) {
265 efx_update_irq_mod(efx, channel);
268 #ifdef CONFIG_RFS_ACCEL
269 /* Perhaps expire some ARFS filters */
270 mod_delayed_work(system_wq, &channel->filter_work, 0);
273 /* There is no race here; although napi_disable() will
274 * only wait for napi_complete(), this isn't a problem
275 * since efx_nic_eventq_read_ack() will have no effect if
276 * interrupts have already been disabled.
278 if (napi_complete_done(napi, spent))
279 efx_nic_eventq_read_ack(channel);
285 /* Create event queue
286 * Event queue memory allocations are done only once. If the channel
287 * is reset, the memory buffer will be reused; this guards against
288 * errors during channel reset and also simplifies interrupt handling.
290 int efx_probe_eventq(struct efx_channel *channel)
292 struct efx_nic *efx = channel->efx;
293 unsigned long entries;
295 netif_dbg(efx, probe, efx->net_dev,
296 "chan %d create event queue\n", channel->channel);
298 /* Build an event queue with room for one event per tx and rx buffer,
299 * plus some extra for link state events and MCDI completions. */
300 entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
301 EFX_WARN_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
302 channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
304 return efx_nic_probe_eventq(channel);
307 /* Prepare channel's event queue */
308 int efx_init_eventq(struct efx_channel *channel)
310 struct efx_nic *efx = channel->efx;
313 EFX_WARN_ON_PARANOID(channel->eventq_init);
315 netif_dbg(efx, drv, efx->net_dev,
316 "chan %d init event queue\n", channel->channel);
318 rc = efx_nic_init_eventq(channel);
320 efx->type->push_irq_moderation(channel);
321 channel->eventq_read_ptr = 0;
322 channel->eventq_init = true;
327 /* Enable event queue processing and NAPI */
328 void efx_start_eventq(struct efx_channel *channel)
330 netif_dbg(channel->efx, ifup, channel->efx->net_dev,
331 "chan %d start event queue\n", channel->channel);
333 /* Make sure the NAPI handler sees the enabled flag set */
334 channel->enabled = true;
337 napi_enable(&channel->napi_str);
338 efx_nic_eventq_read_ack(channel);
341 /* Disable event queue processing and NAPI */
342 void efx_stop_eventq(struct efx_channel *channel)
344 if (!channel->enabled)
347 napi_disable(&channel->napi_str);
348 channel->enabled = false;
351 void efx_fini_eventq(struct efx_channel *channel)
353 if (!channel->eventq_init)
356 netif_dbg(channel->efx, drv, channel->efx->net_dev,
357 "chan %d fini event queue\n", channel->channel);
359 efx_nic_fini_eventq(channel);
360 channel->eventq_init = false;
363 void efx_remove_eventq(struct efx_channel *channel)
365 netif_dbg(channel->efx, drv, channel->efx->net_dev,
366 "chan %d remove event queue\n", channel->channel);
368 efx_nic_remove_eventq(channel);
371 /**************************************************************************
375 *************************************************************************/
377 /* Allocate and initialise a channel structure. */
379 efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
381 struct efx_channel *channel;
382 struct efx_rx_queue *rx_queue;
383 struct efx_tx_queue *tx_queue;
386 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
391 channel->channel = i;
392 channel->type = &efx_default_channel_type;
394 for (j = 0; j < EFX_TXQ_TYPES; j++) {
395 tx_queue = &channel->tx_queue[j];
397 tx_queue->queue = i * EFX_TXQ_TYPES + j;
398 tx_queue->channel = channel;
401 #ifdef CONFIG_RFS_ACCEL
402 INIT_DELAYED_WORK(&channel->filter_work, efx_filter_rfs_expire);
405 rx_queue = &channel->rx_queue;
407 timer_setup(&rx_queue->slow_fill, efx_rx_slow_fill, 0);
412 /* Allocate and initialise a channel structure, copying parameters
413 * (but not resources) from an old channel structure.
415 struct efx_channel *efx_copy_channel(const struct efx_channel *old_channel)
417 struct efx_channel *channel;
418 struct efx_rx_queue *rx_queue;
419 struct efx_tx_queue *tx_queue;
422 channel = kmalloc(sizeof(*channel), GFP_KERNEL);
426 *channel = *old_channel;
428 channel->napi_dev = NULL;
429 INIT_HLIST_NODE(&channel->napi_str.napi_hash_node);
430 channel->napi_str.napi_id = 0;
431 channel->napi_str.state = 0;
432 memset(&channel->eventq, 0, sizeof(channel->eventq));
434 for (j = 0; j < EFX_TXQ_TYPES; j++) {
435 tx_queue = &channel->tx_queue[j];
436 if (tx_queue->channel)
437 tx_queue->channel = channel;
438 tx_queue->buffer = NULL;
439 memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
442 rx_queue = &channel->rx_queue;
443 rx_queue->buffer = NULL;
444 memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
445 timer_setup(&rx_queue->slow_fill, efx_rx_slow_fill, 0);
446 #ifdef CONFIG_RFS_ACCEL
447 INIT_DELAYED_WORK(&channel->filter_work, efx_filter_rfs_expire);
453 static int efx_probe_channel(struct efx_channel *channel)
455 struct efx_tx_queue *tx_queue;
456 struct efx_rx_queue *rx_queue;
459 netif_dbg(channel->efx, probe, channel->efx->net_dev,
460 "creating channel %d\n", channel->channel);
462 rc = channel->type->pre_probe(channel);
466 rc = efx_probe_eventq(channel);
470 efx_for_each_channel_tx_queue(tx_queue, channel) {
471 rc = efx_probe_tx_queue(tx_queue);
476 efx_for_each_channel_rx_queue(rx_queue, channel) {
477 rc = efx_probe_rx_queue(rx_queue);
482 channel->rx_list = NULL;
487 efx_remove_channel(channel);
491 void efx_get_channel_name(struct efx_channel *channel, char *buf, size_t len)
493 struct efx_nic *efx = channel->efx;
497 number = channel->channel;
499 if (number >= efx->xdp_channel_offset &&
500 !WARN_ON_ONCE(!efx->n_xdp_channels)) {
502 number -= efx->xdp_channel_offset;
503 } else if (efx->tx_channel_offset == 0) {
505 } else if (number < efx->tx_channel_offset) {
509 number -= efx->tx_channel_offset;
511 snprintf(buf, len, "%s%s-%d", efx->name, type, number);
514 void efx_set_channel_names(struct efx_nic *efx)
516 struct efx_channel *channel;
518 efx_for_each_channel(channel, efx)
519 channel->type->get_name(channel,
520 efx->msi_context[channel->channel].name,
521 sizeof(efx->msi_context[0].name));
524 int efx_probe_channels(struct efx_nic *efx)
526 struct efx_channel *channel;
529 /* Restart special buffer allocation */
530 efx->next_buffer_table = 0;
532 /* Probe channels in reverse, so that any 'extra' channels
533 * use the start of the buffer table. This allows the traffic
534 * channels to be resized without moving them or wasting the
535 * entries before them.
537 efx_for_each_channel_rev(channel, efx) {
538 rc = efx_probe_channel(channel);
540 netif_err(efx, probe, efx->net_dev,
541 "failed to create channel %d\n",
546 efx_set_channel_names(efx);
551 efx_remove_channels(efx);
555 void efx_remove_channel(struct efx_channel *channel)
557 struct efx_tx_queue *tx_queue;
558 struct efx_rx_queue *rx_queue;
560 netif_dbg(channel->efx, drv, channel->efx->net_dev,
561 "destroy chan %d\n", channel->channel);
563 efx_for_each_channel_rx_queue(rx_queue, channel)
564 efx_remove_rx_queue(rx_queue);
565 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
566 efx_remove_tx_queue(tx_queue);
567 efx_remove_eventq(channel);
568 channel->type->post_remove(channel);
571 void efx_remove_channels(struct efx_nic *efx)
573 struct efx_channel *channel;
575 efx_for_each_channel(channel, efx)
576 efx_remove_channel(channel);
578 kfree(efx->xdp_tx_queues);
581 int efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
583 struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
584 u32 old_rxq_entries, old_txq_entries;
585 unsigned i, next_buffer_table = 0;
588 rc = efx_check_disabled(efx);
592 /* Not all channels should be reallocated. We must avoid
593 * reallocating their buffer table entries.
595 efx_for_each_channel(channel, efx) {
596 struct efx_rx_queue *rx_queue;
597 struct efx_tx_queue *tx_queue;
599 if (channel->type->copy)
601 next_buffer_table = max(next_buffer_table,
602 channel->eventq.index +
603 channel->eventq.entries);
604 efx_for_each_channel_rx_queue(rx_queue, channel)
605 next_buffer_table = max(next_buffer_table,
606 rx_queue->rxd.index +
607 rx_queue->rxd.entries);
608 efx_for_each_channel_tx_queue(tx_queue, channel)
609 next_buffer_table = max(next_buffer_table,
610 tx_queue->txd.index +
611 tx_queue->txd.entries);
614 efx_device_detach_sync(efx);
616 efx_soft_disable_interrupts(efx);
618 /* Clone channels (where possible) */
619 memset(other_channel, 0, sizeof(other_channel));
620 for (i = 0; i < efx->n_channels; i++) {
621 channel = efx->channel[i];
622 if (channel->type->copy)
623 channel = channel->type->copy(channel);
628 other_channel[i] = channel;
631 /* Swap entry counts and channel pointers */
632 old_rxq_entries = efx->rxq_entries;
633 old_txq_entries = efx->txq_entries;
634 efx->rxq_entries = rxq_entries;
635 efx->txq_entries = txq_entries;
636 for (i = 0; i < efx->n_channels; i++) {
637 channel = efx->channel[i];
638 efx->channel[i] = other_channel[i];
639 other_channel[i] = channel;
642 /* Restart buffer table allocation */
643 efx->next_buffer_table = next_buffer_table;
645 for (i = 0; i < efx->n_channels; i++) {
646 channel = efx->channel[i];
647 if (!channel->type->copy)
649 rc = efx_probe_channel(channel);
652 efx_init_napi_channel(efx->channel[i]);
656 /* Destroy unused channel structures */
657 for (i = 0; i < efx->n_channels; i++) {
658 channel = other_channel[i];
659 if (channel && channel->type->copy) {
660 efx_fini_napi_channel(channel);
661 efx_remove_channel(channel);
666 rc2 = efx_soft_enable_interrupts(efx);
669 netif_err(efx, drv, efx->net_dev,
670 "unable to restart interrupts on channel reallocation\n");
671 efx_schedule_reset(efx, RESET_TYPE_DISABLE);
674 efx_device_attach_if_not_resetting(efx);
680 efx->rxq_entries = old_rxq_entries;
681 efx->txq_entries = old_txq_entries;
682 for (i = 0; i < efx->n_channels; i++) {
683 channel = efx->channel[i];
684 efx->channel[i] = other_channel[i];
685 other_channel[i] = channel;
690 void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
692 mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(10));
695 bool efx_default_channel_want_txqs(struct efx_channel *channel)
697 return channel->channel - channel->efx->tx_channel_offset <
698 channel->efx->n_tx_channels;
701 static const struct efx_channel_type efx_default_channel_type = {
702 .pre_probe = efx_channel_dummy_op_int,
703 .post_remove = efx_channel_dummy_op_void,
704 .get_name = efx_get_channel_name,
705 .copy = efx_copy_channel,
706 .want_txqs = efx_default_channel_want_txqs,
707 .keep_eventq = false,
711 int efx_channel_dummy_op_int(struct efx_channel *channel)
716 void efx_channel_dummy_op_void(struct efx_channel *channel)
720 /**************************************************************************
724 **************************************************************************/
726 void efx_link_set_advertising(struct efx_nic *efx,
727 const unsigned long *advertising)
729 memcpy(efx->link_advertising, advertising,
730 sizeof(__ETHTOOL_DECLARE_LINK_MODE_MASK()));
732 efx->link_advertising[0] |= ADVERTISED_Autoneg;
733 if (advertising[0] & ADVERTISED_Pause)
734 efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
736 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
737 if (advertising[0] & ADVERTISED_Asym_Pause)
738 efx->wanted_fc ^= EFX_FC_TX;
741 /* Equivalent to efx_link_set_advertising with all-zeroes, except does not
742 * force the Autoneg bit on.
744 void efx_link_clear_advertising(struct efx_nic *efx)
746 bitmap_zero(efx->link_advertising, __ETHTOOL_LINK_MODE_MASK_NBITS);
747 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
750 void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
752 efx->wanted_fc = wanted_fc;
753 if (efx->link_advertising[0]) {
754 if (wanted_fc & EFX_FC_RX)
755 efx->link_advertising[0] |= (ADVERTISED_Pause |
756 ADVERTISED_Asym_Pause);
758 efx->link_advertising[0] &= ~(ADVERTISED_Pause |
759 ADVERTISED_Asym_Pause);
760 if (wanted_fc & EFX_FC_TX)
761 efx->link_advertising[0] ^= ADVERTISED_Asym_Pause;
765 static void efx_fini_port(struct efx_nic *efx);
767 static int efx_probe_port(struct efx_nic *efx)
771 netif_dbg(efx, probe, efx->net_dev, "create port\n");
774 efx->phy_mode = PHY_MODE_SPECIAL;
776 /* Connect up MAC/PHY operations table */
777 rc = efx->type->probe_port(efx);
781 /* Initialise MAC address to permanent address */
782 ether_addr_copy(efx->net_dev->dev_addr, efx->net_dev->perm_addr);
787 static int efx_init_port(struct efx_nic *efx)
791 netif_dbg(efx, drv, efx->net_dev, "init port\n");
793 mutex_lock(&efx->mac_lock);
795 rc = efx->phy_op->init(efx);
799 efx->port_initialized = true;
801 /* Reconfigure the MAC before creating dma queues (required for
802 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
803 efx_mac_reconfigure(efx);
805 /* Ensure the PHY advertises the correct flow control settings */
806 rc = efx->phy_op->reconfigure(efx);
807 if (rc && rc != -EPERM)
810 mutex_unlock(&efx->mac_lock);
814 efx->phy_op->fini(efx);
816 mutex_unlock(&efx->mac_lock);
820 static void efx_fini_port(struct efx_nic *efx)
822 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
824 if (!efx->port_initialized)
827 efx->phy_op->fini(efx);
828 efx->port_initialized = false;
830 efx->link_state.up = false;
831 efx_link_status_changed(efx);
834 static void efx_remove_port(struct efx_nic *efx)
836 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
838 efx->type->remove_port(efx);
841 /**************************************************************************
845 **************************************************************************/
847 static LIST_HEAD(efx_primary_list);
848 static LIST_HEAD(efx_unassociated_list);
850 static bool efx_same_controller(struct efx_nic *left, struct efx_nic *right)
852 return left->type == right->type &&
853 left->vpd_sn && right->vpd_sn &&
854 !strcmp(left->vpd_sn, right->vpd_sn);
857 static void efx_associate(struct efx_nic *efx)
859 struct efx_nic *other, *next;
861 if (efx->primary == efx) {
862 /* Adding primary function; look for secondaries */
864 netif_dbg(efx, probe, efx->net_dev, "adding to primary list\n");
865 list_add_tail(&efx->node, &efx_primary_list);
867 list_for_each_entry_safe(other, next, &efx_unassociated_list,
869 if (efx_same_controller(efx, other)) {
870 list_del(&other->node);
871 netif_dbg(other, probe, other->net_dev,
872 "moving to secondary list of %s %s\n",
873 pci_name(efx->pci_dev),
875 list_add_tail(&other->node,
876 &efx->secondary_list);
877 other->primary = efx;
881 /* Adding secondary function; look for primary */
883 list_for_each_entry(other, &efx_primary_list, node) {
884 if (efx_same_controller(efx, other)) {
885 netif_dbg(efx, probe, efx->net_dev,
886 "adding to secondary list of %s %s\n",
887 pci_name(other->pci_dev),
888 other->net_dev->name);
889 list_add_tail(&efx->node,
890 &other->secondary_list);
891 efx->primary = other;
896 netif_dbg(efx, probe, efx->net_dev,
897 "adding to unassociated list\n");
898 list_add_tail(&efx->node, &efx_unassociated_list);
902 static void efx_dissociate(struct efx_nic *efx)
904 struct efx_nic *other, *next;
906 list_del(&efx->node);
909 list_for_each_entry_safe(other, next, &efx->secondary_list, node) {
910 list_del(&other->node);
911 netif_dbg(other, probe, other->net_dev,
912 "moving to unassociated list\n");
913 list_add_tail(&other->node, &efx_unassociated_list);
914 other->primary = NULL;
918 void efx_set_default_rx_indir_table(struct efx_nic *efx,
919 struct efx_rss_context *ctx)
923 for (i = 0; i < ARRAY_SIZE(ctx->rx_indir_table); i++)
924 ctx->rx_indir_table[i] =
925 ethtool_rxfh_indir_default(i, efx->rss_spread);
928 static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
930 cpumask_var_t thread_mask;
937 if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
938 netif_warn(efx, probe, efx->net_dev,
939 "RSS disabled due to allocation failure\n");
944 for_each_online_cpu(cpu) {
945 if (!cpumask_test_cpu(cpu, thread_mask)) {
947 cpumask_or(thread_mask, thread_mask,
948 topology_sibling_cpumask(cpu));
952 free_cpumask_var(thread_mask);
955 if (count > EFX_MAX_RX_QUEUES) {
956 netif_cond_dbg(efx, probe, efx->net_dev, !rss_cpus, warn,
957 "Reducing number of rx queues from %u to %u.\n",
958 count, EFX_MAX_RX_QUEUES);
959 count = EFX_MAX_RX_QUEUES;
962 /* If RSS is requested for the PF *and* VFs then we can't write RSS
963 * table entries that are inaccessible to VFs
965 #ifdef CONFIG_SFC_SRIOV
966 if (efx->type->sriov_wanted) {
967 if (efx->type->sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
968 count > efx_vf_size(efx)) {
969 netif_warn(efx, probe, efx->net_dev,
970 "Reducing number of RSS channels from %u to %u for "
971 "VF support. Increase vf-msix-limit to use more "
972 "channels on the PF.\n",
973 count, efx_vf_size(efx));
974 count = efx_vf_size(efx);
982 static int efx_allocate_msix_channels(struct efx_nic *efx,
983 unsigned int max_channels,
984 unsigned int extra_channels,
985 unsigned int parallelism)
987 unsigned int n_channels = parallelism;
992 if (efx_separate_tx_channels)
994 n_channels += extra_channels;
996 /* To allow XDP transmit to happen from arbitrary NAPI contexts
997 * we allocate a TX queue per CPU. We share event queues across
998 * multiple tx queues, assuming tx and ev queues are both
1002 n_xdp_tx = num_possible_cpus();
1003 n_xdp_ev = DIV_ROUND_UP(n_xdp_tx, EFX_TXQ_TYPES);
1005 vec_count = pci_msix_vec_count(efx->pci_dev);
1009 max_channels = min_t(unsigned int, vec_count, max_channels);
1012 * We need a channel per event queue, plus a VI per tx queue.
1013 * This may be more pessimistic than it needs to be.
1015 if (n_channels + n_xdp_ev > max_channels) {
1016 netif_err(efx, drv, efx->net_dev,
1017 "Insufficient resources for %d XDP event queues (%d other channels, max %d)\n",
1018 n_xdp_ev, n_channels, max_channels);
1019 efx->n_xdp_channels = 0;
1020 efx->xdp_tx_per_channel = 0;
1021 efx->xdp_tx_queue_count = 0;
1023 efx->n_xdp_channels = n_xdp_ev;
1024 efx->xdp_tx_per_channel = EFX_TXQ_TYPES;
1025 efx->xdp_tx_queue_count = n_xdp_tx;
1026 n_channels += n_xdp_ev;
1027 netif_dbg(efx, drv, efx->net_dev,
1028 "Allocating %d TX and %d event queues for XDP\n",
1029 n_xdp_tx, n_xdp_ev);
1032 if (vec_count < n_channels) {
1033 netif_err(efx, drv, efx->net_dev,
1034 "WARNING: Insufficient MSI-X vectors available (%d < %u).\n",
1035 vec_count, n_channels);
1036 netif_err(efx, drv, efx->net_dev,
1037 "WARNING: Performance may be reduced.\n");
1038 n_channels = vec_count;
1041 n_channels = min(n_channels, max_channels);
1043 efx->n_channels = n_channels;
1045 /* Ignore XDP tx channels when creating rx channels. */
1046 n_channels -= efx->n_xdp_channels;
1048 if (efx_separate_tx_channels) {
1049 efx->n_tx_channels =
1050 min(max(n_channels / 2, 1U),
1051 efx->max_tx_channels);
1052 efx->tx_channel_offset =
1053 n_channels - efx->n_tx_channels;
1054 efx->n_rx_channels =
1056 efx->n_tx_channels, 1U);
1058 efx->n_tx_channels = min(n_channels, efx->max_tx_channels);
1059 efx->tx_channel_offset = 0;
1060 efx->n_rx_channels = n_channels;
1063 efx->n_rx_channels = min(efx->n_rx_channels, parallelism);
1064 efx->n_tx_channels = min(efx->n_tx_channels, parallelism);
1066 efx->xdp_channel_offset = n_channels;
1068 netif_dbg(efx, drv, efx->net_dev,
1069 "Allocating %u RX channels\n",
1070 efx->n_rx_channels);
1072 return efx->n_channels;
1075 /* Probe the number and type of interrupts we are able to obtain, and
1076 * the resulting numbers of channels and RX queues.
1078 int efx_probe_interrupts(struct efx_nic *efx)
1080 unsigned int extra_channels = 0;
1081 unsigned int rss_spread;
1085 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
1086 if (efx->extra_channel_type[i])
1089 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
1090 unsigned int parallelism = efx_wanted_parallelism(efx);
1091 struct msix_entry xentries[EFX_MAX_CHANNELS];
1092 unsigned int n_channels;
1094 rc = efx_allocate_msix_channels(efx, efx->max_channels,
1095 extra_channels, parallelism);
1098 for (i = 0; i < n_channels; i++)
1099 xentries[i].entry = i;
1100 rc = pci_enable_msix_range(efx->pci_dev, xentries, 1,
1104 /* Fall back to single channel MSI */
1105 netif_err(efx, drv, efx->net_dev,
1106 "could not enable MSI-X\n");
1107 if (efx->type->min_interrupt_mode >= EFX_INT_MODE_MSI)
1108 efx->interrupt_mode = EFX_INT_MODE_MSI;
1111 } else if (rc < n_channels) {
1112 netif_err(efx, drv, efx->net_dev,
1113 "WARNING: Insufficient MSI-X vectors"
1114 " available (%d < %u).\n", rc, n_channels);
1115 netif_err(efx, drv, efx->net_dev,
1116 "WARNING: Performance may be reduced.\n");
1121 for (i = 0; i < efx->n_channels; i++)
1122 efx_get_channel(efx, i)->irq =
1127 /* Try single interrupt MSI */
1128 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
1129 efx->n_channels = 1;
1130 efx->n_rx_channels = 1;
1131 efx->n_tx_channels = 1;
1132 efx->n_xdp_channels = 0;
1133 efx->xdp_channel_offset = efx->n_channels;
1134 rc = pci_enable_msi(efx->pci_dev);
1136 efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
1138 netif_err(efx, drv, efx->net_dev,
1139 "could not enable MSI\n");
1140 if (efx->type->min_interrupt_mode >= EFX_INT_MODE_LEGACY)
1141 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
1147 /* Assume legacy interrupts */
1148 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
1149 efx->n_channels = 1 + (efx_separate_tx_channels ? 1 : 0);
1150 efx->n_rx_channels = 1;
1151 efx->n_tx_channels = 1;
1152 efx->n_xdp_channels = 0;
1153 efx->xdp_channel_offset = efx->n_channels;
1154 efx->legacy_irq = efx->pci_dev->irq;
1157 /* Assign extra channels if possible, before XDP channels */
1158 efx->n_extra_tx_channels = 0;
1159 j = efx->xdp_channel_offset;
1160 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
1161 if (!efx->extra_channel_type[i])
1163 if (j <= efx->tx_channel_offset + efx->n_tx_channels) {
1164 efx->extra_channel_type[i]->handle_no_channel(efx);
1167 efx_get_channel(efx, j)->type =
1168 efx->extra_channel_type[i];
1169 if (efx_channel_has_tx_queues(efx_get_channel(efx, j)))
1170 efx->n_extra_tx_channels++;
1174 rss_spread = efx->n_rx_channels;
1175 /* RSS might be usable on VFs even if it is disabled on the PF */
1176 #ifdef CONFIG_SFC_SRIOV
1177 if (efx->type->sriov_wanted) {
1178 efx->rss_spread = ((rss_spread > 1 ||
1179 !efx->type->sriov_wanted(efx)) ?
1180 rss_spread : efx_vf_size(efx));
1184 efx->rss_spread = rss_spread;
1189 #if defined(CONFIG_SMP)
1190 void efx_set_interrupt_affinity(struct efx_nic *efx)
1192 struct efx_channel *channel;
1195 efx_for_each_channel(channel, efx) {
1196 cpu = cpumask_local_spread(channel->channel,
1197 pcibus_to_node(efx->pci_dev->bus));
1198 irq_set_affinity_hint(channel->irq, cpumask_of(cpu));
1202 void efx_clear_interrupt_affinity(struct efx_nic *efx)
1204 struct efx_channel *channel;
1206 efx_for_each_channel(channel, efx)
1207 irq_set_affinity_hint(channel->irq, NULL);
1210 void efx_set_interrupt_affinity(struct efx_nic *efx __attribute__ ((unused)))
1214 void efx_clear_interrupt_affinity(struct efx_nic *efx __attribute__ ((unused)))
1217 #endif /* CONFIG_SMP */
1219 int efx_soft_enable_interrupts(struct efx_nic *efx)
1221 struct efx_channel *channel, *end_channel;
1224 BUG_ON(efx->state == STATE_DISABLED);
1226 efx->irq_soft_enabled = true;
1229 efx_for_each_channel(channel, efx) {
1230 if (!channel->type->keep_eventq) {
1231 rc = efx_init_eventq(channel);
1235 efx_start_eventq(channel);
1238 efx_mcdi_mode_event(efx);
1242 end_channel = channel;
1243 efx_for_each_channel(channel, efx) {
1244 if (channel == end_channel)
1246 efx_stop_eventq(channel);
1247 if (!channel->type->keep_eventq)
1248 efx_fini_eventq(channel);
1254 void efx_soft_disable_interrupts(struct efx_nic *efx)
1256 struct efx_channel *channel;
1258 if (efx->state == STATE_DISABLED)
1261 efx_mcdi_mode_poll(efx);
1263 efx->irq_soft_enabled = false;
1266 if (efx->legacy_irq)
1267 synchronize_irq(efx->legacy_irq);
1269 efx_for_each_channel(channel, efx) {
1271 synchronize_irq(channel->irq);
1273 efx_stop_eventq(channel);
1274 if (!channel->type->keep_eventq)
1275 efx_fini_eventq(channel);
1278 /* Flush the asynchronous MCDI request queue */
1279 efx_mcdi_flush_async(efx);
1282 int efx_enable_interrupts(struct efx_nic *efx)
1284 struct efx_channel *channel, *end_channel;
1287 BUG_ON(efx->state == STATE_DISABLED);
1289 if (efx->eeh_disabled_legacy_irq) {
1290 enable_irq(efx->legacy_irq);
1291 efx->eeh_disabled_legacy_irq = false;
1294 efx->type->irq_enable_master(efx);
1296 efx_for_each_channel(channel, efx) {
1297 if (channel->type->keep_eventq) {
1298 rc = efx_init_eventq(channel);
1304 rc = efx_soft_enable_interrupts(efx);
1311 end_channel = channel;
1312 efx_for_each_channel(channel, efx) {
1313 if (channel == end_channel)
1315 if (channel->type->keep_eventq)
1316 efx_fini_eventq(channel);
1319 efx->type->irq_disable_non_ev(efx);
1324 void efx_disable_interrupts(struct efx_nic *efx)
1326 struct efx_channel *channel;
1328 efx_soft_disable_interrupts(efx);
1330 efx_for_each_channel(channel, efx) {
1331 if (channel->type->keep_eventq)
1332 efx_fini_eventq(channel);
1335 efx->type->irq_disable_non_ev(efx);
1338 void efx_remove_interrupts(struct efx_nic *efx)
1340 struct efx_channel *channel;
1342 /* Remove MSI/MSI-X interrupts */
1343 efx_for_each_channel(channel, efx)
1345 pci_disable_msi(efx->pci_dev);
1346 pci_disable_msix(efx->pci_dev);
1348 /* Remove legacy interrupt */
1349 efx->legacy_irq = 0;
1352 int efx_set_channels(struct efx_nic *efx)
1354 struct efx_channel *channel;
1355 struct efx_tx_queue *tx_queue;
1356 int xdp_queue_number;
1358 efx->tx_channel_offset =
1359 efx_separate_tx_channels ?
1360 efx->n_channels - efx->n_tx_channels : 0;
1362 if (efx->xdp_tx_queue_count) {
1363 EFX_WARN_ON_PARANOID(efx->xdp_tx_queues);
1365 /* Allocate array for XDP TX queue lookup. */
1366 efx->xdp_tx_queues = kcalloc(efx->xdp_tx_queue_count,
1367 sizeof(*efx->xdp_tx_queues),
1369 if (!efx->xdp_tx_queues)
1373 /* We need to mark which channels really have RX and TX
1374 * queues, and adjust the TX queue numbers if we have separate
1375 * RX-only and TX-only channels.
1377 xdp_queue_number = 0;
1378 efx_for_each_channel(channel, efx) {
1379 if (channel->channel < efx->n_rx_channels)
1380 channel->rx_queue.core_index = channel->channel;
1382 channel->rx_queue.core_index = -1;
1384 efx_for_each_channel_tx_queue(tx_queue, channel) {
1385 tx_queue->queue -= (efx->tx_channel_offset *
1388 if (efx_channel_is_xdp_tx(channel) &&
1389 xdp_queue_number < efx->xdp_tx_queue_count) {
1390 efx->xdp_tx_queues[xdp_queue_number] = tx_queue;
1398 static int efx_probe_nic(struct efx_nic *efx)
1402 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
1404 /* Carry out hardware-type specific initialisation */
1405 rc = efx->type->probe(efx);
1410 if (!efx->max_channels || !efx->max_tx_channels) {
1411 netif_err(efx, drv, efx->net_dev,
1412 "Insufficient resources to allocate"
1418 /* Determine the number of channels and queues by trying
1419 * to hook in MSI-X interrupts.
1421 rc = efx_probe_interrupts(efx);
1425 rc = efx_set_channels(efx);
1429 /* dimension_resources can fail with EAGAIN */
1430 rc = efx->type->dimension_resources(efx);
1431 if (rc != 0 && rc != -EAGAIN)
1435 /* try again with new max_channels */
1436 efx_remove_interrupts(efx);
1438 } while (rc == -EAGAIN);
1440 if (efx->n_channels > 1)
1441 netdev_rss_key_fill(efx->rss_context.rx_hash_key,
1442 sizeof(efx->rss_context.rx_hash_key));
1443 efx_set_default_rx_indir_table(efx, &efx->rss_context);
1445 netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1446 netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
1448 /* Initialise the interrupt moderation settings */
1449 efx->irq_mod_step_us = DIV_ROUND_UP(efx->timer_quantum_ns, 1000);
1450 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
1456 efx_remove_interrupts(efx);
1458 efx->type->remove(efx);
1462 static void efx_remove_nic(struct efx_nic *efx)
1464 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
1466 efx_remove_interrupts(efx);
1467 efx->type->remove(efx);
1470 static int efx_probe_filters(struct efx_nic *efx)
1474 init_rwsem(&efx->filter_sem);
1475 mutex_lock(&efx->mac_lock);
1476 down_write(&efx->filter_sem);
1477 rc = efx->type->filter_table_probe(efx);
1481 #ifdef CONFIG_RFS_ACCEL
1482 if (efx->type->offload_features & NETIF_F_NTUPLE) {
1483 struct efx_channel *channel;
1486 efx_for_each_channel(channel, efx) {
1487 channel->rps_flow_id =
1488 kcalloc(efx->type->max_rx_ip_filters,
1489 sizeof(*channel->rps_flow_id),
1491 if (!channel->rps_flow_id)
1495 i < efx->type->max_rx_ip_filters;
1497 channel->rps_flow_id[i] =
1498 RPS_FLOW_ID_INVALID;
1499 channel->rfs_expire_index = 0;
1500 channel->rfs_filter_count = 0;
1504 efx_for_each_channel(channel, efx)
1505 kfree(channel->rps_flow_id);
1506 efx->type->filter_table_remove(efx);
1513 up_write(&efx->filter_sem);
1514 mutex_unlock(&efx->mac_lock);
1518 static void efx_remove_filters(struct efx_nic *efx)
1520 #ifdef CONFIG_RFS_ACCEL
1521 struct efx_channel *channel;
1523 efx_for_each_channel(channel, efx) {
1524 cancel_delayed_work_sync(&channel->filter_work);
1525 kfree(channel->rps_flow_id);
1528 down_write(&efx->filter_sem);
1529 efx->type->filter_table_remove(efx);
1530 up_write(&efx->filter_sem);
1534 /**************************************************************************
1536 * NIC startup/shutdown
1538 *************************************************************************/
1540 static int efx_probe_all(struct efx_nic *efx)
1544 rc = efx_probe_nic(efx);
1546 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
1550 rc = efx_probe_port(efx);
1552 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
1556 BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
1557 if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
1561 efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
1563 #ifdef CONFIG_SFC_SRIOV
1564 rc = efx->type->vswitching_probe(efx);
1565 if (rc) /* not fatal; the PF will still work fine */
1566 netif_warn(efx, probe, efx->net_dev,
1567 "failed to setup vswitching rc=%d;"
1568 " VFs may not function\n", rc);
1571 rc = efx_probe_filters(efx);
1573 netif_err(efx, probe, efx->net_dev,
1574 "failed to create filter tables\n");
1578 rc = efx_probe_channels(efx);
1585 efx_remove_filters(efx);
1587 #ifdef CONFIG_SFC_SRIOV
1588 efx->type->vswitching_remove(efx);
1591 efx_remove_port(efx);
1593 efx_remove_nic(efx);
1598 static void efx_remove_all(struct efx_nic *efx)
1601 efx_xdp_setup_prog(efx, NULL);
1604 efx_remove_channels(efx);
1605 efx_remove_filters(efx);
1606 #ifdef CONFIG_SFC_SRIOV
1607 efx->type->vswitching_remove(efx);
1609 efx_remove_port(efx);
1610 efx_remove_nic(efx);
1613 /**************************************************************************
1615 * Interrupt moderation
1617 **************************************************************************/
1618 unsigned int efx_usecs_to_ticks(struct efx_nic *efx, unsigned int usecs)
1622 if (usecs * 1000 < efx->timer_quantum_ns)
1623 return 1; /* never round down to 0 */
1624 return usecs * 1000 / efx->timer_quantum_ns;
1627 unsigned int efx_ticks_to_usecs(struct efx_nic *efx, unsigned int ticks)
1629 /* We must round up when converting ticks to microseconds
1630 * because we round down when converting the other way.
1632 return DIV_ROUND_UP(ticks * efx->timer_quantum_ns, 1000);
1635 /* Set interrupt moderation parameters */
1636 int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
1637 unsigned int rx_usecs, bool rx_adaptive,
1638 bool rx_may_override_tx)
1640 struct efx_channel *channel;
1641 unsigned int timer_max_us;
1643 EFX_ASSERT_RESET_SERIALISED(efx);
1645 timer_max_us = efx->timer_max_ns / 1000;
1647 if (tx_usecs > timer_max_us || rx_usecs > timer_max_us)
1650 if (tx_usecs != rx_usecs && efx->tx_channel_offset == 0 &&
1651 !rx_may_override_tx) {
1652 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
1653 "RX and TX IRQ moderation must be equal\n");
1657 efx->irq_rx_adaptive = rx_adaptive;
1658 efx->irq_rx_moderation_us = rx_usecs;
1659 efx_for_each_channel(channel, efx) {
1660 if (efx_channel_has_rx_queue(channel))
1661 channel->irq_moderation_us = rx_usecs;
1662 else if (efx_channel_has_tx_queues(channel))
1663 channel->irq_moderation_us = tx_usecs;
1664 else if (efx_channel_is_xdp_tx(channel))
1665 channel->irq_moderation_us = tx_usecs;
1671 void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
1672 unsigned int *rx_usecs, bool *rx_adaptive)
1674 *rx_adaptive = efx->irq_rx_adaptive;
1675 *rx_usecs = efx->irq_rx_moderation_us;
1677 /* If channels are shared between RX and TX, so is IRQ
1678 * moderation. Otherwise, IRQ moderation is the same for all
1679 * TX channels and is not adaptive.
1681 if (efx->tx_channel_offset == 0) {
1682 *tx_usecs = *rx_usecs;
1684 struct efx_channel *tx_channel;
1686 tx_channel = efx->channel[efx->tx_channel_offset];
1687 *tx_usecs = tx_channel->irq_moderation_us;
1691 /**************************************************************************
1695 *************************************************************************/
1698 * Context: process, rtnl_lock() held.
1700 static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1702 struct efx_nic *efx = netdev_priv(net_dev);
1703 struct mii_ioctl_data *data = if_mii(ifr);
1705 if (cmd == SIOCSHWTSTAMP)
1706 return efx_ptp_set_ts_config(efx, ifr);
1707 if (cmd == SIOCGHWTSTAMP)
1708 return efx_ptp_get_ts_config(efx, ifr);
1710 /* Convert phy_id from older PRTAD/DEVAD format */
1711 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
1712 (data->phy_id & 0xfc00) == 0x0400)
1713 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
1715 return mdio_mii_ioctl(&efx->mdio, data, cmd);
1718 /**************************************************************************
1722 **************************************************************************/
1724 void efx_init_napi_channel(struct efx_channel *channel)
1726 struct efx_nic *efx = channel->efx;
1728 channel->napi_dev = efx->net_dev;
1729 netif_napi_add(channel->napi_dev, &channel->napi_str,
1730 efx_poll, napi_weight);
1733 void efx_init_napi(struct efx_nic *efx)
1735 struct efx_channel *channel;
1737 efx_for_each_channel(channel, efx)
1738 efx_init_napi_channel(channel);
1741 void efx_fini_napi_channel(struct efx_channel *channel)
1743 if (channel->napi_dev)
1744 netif_napi_del(&channel->napi_str);
1746 channel->napi_dev = NULL;
1749 void efx_fini_napi(struct efx_nic *efx)
1751 struct efx_channel *channel;
1753 efx_for_each_channel(channel, efx)
1754 efx_fini_napi_channel(channel);
1757 /**************************************************************************
1759 * Kernel net device interface
1761 *************************************************************************/
1763 /* Context: process, rtnl_lock() held. */
1764 int efx_net_open(struct net_device *net_dev)
1766 struct efx_nic *efx = netdev_priv(net_dev);
1769 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
1770 raw_smp_processor_id());
1772 rc = efx_check_disabled(efx);
1775 if (efx->phy_mode & PHY_MODE_SPECIAL)
1777 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
1780 /* Notify the kernel of the link state polled during driver load,
1781 * before the monitor starts running */
1782 efx_link_status_changed(efx);
1785 if (efx->state == STATE_DISABLED || efx->reset_pending)
1786 netif_device_detach(efx->net_dev);
1787 efx_selftest_async_start(efx);
1791 /* Context: process, rtnl_lock() held.
1792 * Note that the kernel will ignore our return code; this method
1793 * should really be a void.
1795 int efx_net_stop(struct net_device *net_dev)
1797 struct efx_nic *efx = netdev_priv(net_dev);
1799 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
1800 raw_smp_processor_id());
1802 /* Stop the device and flush all the channels */
1808 /* Context: process, dev_base_lock or RTNL held, non-blocking. */
1809 static void efx_net_stats(struct net_device *net_dev,
1810 struct rtnl_link_stats64 *stats)
1812 struct efx_nic *efx = netdev_priv(net_dev);
1814 spin_lock_bh(&efx->stats_lock);
1815 efx->type->update_stats(efx, NULL, stats);
1816 spin_unlock_bh(&efx->stats_lock);
1819 /* Context: netif_tx_lock held, BHs disabled. */
1820 static void efx_watchdog(struct net_device *net_dev, unsigned int txqueue)
1822 struct efx_nic *efx = netdev_priv(net_dev);
1824 netif_err(efx, tx_err, efx->net_dev,
1825 "TX stuck with port_enabled=%d: resetting channels\n",
1828 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
1831 static unsigned int efx_xdp_max_mtu(struct efx_nic *efx)
1833 /* The maximum MTU that we can fit in a single page, allowing for
1834 * framing, overhead and XDP headroom.
1836 int overhead = EFX_MAX_FRAME_LEN(0) + sizeof(struct efx_rx_page_state) +
1837 efx->rx_prefix_size + efx->type->rx_buffer_padding +
1838 efx->rx_ip_align + XDP_PACKET_HEADROOM;
1840 return PAGE_SIZE - overhead;
1843 /* Context: process, rtnl_lock() held. */
1844 static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
1846 struct efx_nic *efx = netdev_priv(net_dev);
1849 rc = efx_check_disabled(efx);
1853 if (rtnl_dereference(efx->xdp_prog) &&
1854 new_mtu > efx_xdp_max_mtu(efx)) {
1855 netif_err(efx, drv, efx->net_dev,
1856 "Requested MTU of %d too big for XDP (max: %d)\n",
1857 new_mtu, efx_xdp_max_mtu(efx));
1861 netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
1863 efx_device_detach_sync(efx);
1866 mutex_lock(&efx->mac_lock);
1867 net_dev->mtu = new_mtu;
1868 efx_mac_reconfigure(efx);
1869 mutex_unlock(&efx->mac_lock);
1872 efx_device_attach_if_not_resetting(efx);
1876 static int efx_set_mac_address(struct net_device *net_dev, void *data)
1878 struct efx_nic *efx = netdev_priv(net_dev);
1879 struct sockaddr *addr = data;
1880 u8 *new_addr = addr->sa_data;
1884 if (!is_valid_ether_addr(new_addr)) {
1885 netif_err(efx, drv, efx->net_dev,
1886 "invalid ethernet MAC address requested: %pM\n",
1888 return -EADDRNOTAVAIL;
1891 /* save old address */
1892 ether_addr_copy(old_addr, net_dev->dev_addr);
1893 ether_addr_copy(net_dev->dev_addr, new_addr);
1894 if (efx->type->set_mac_address) {
1895 rc = efx->type->set_mac_address(efx);
1897 ether_addr_copy(net_dev->dev_addr, old_addr);
1902 /* Reconfigure the MAC */
1903 mutex_lock(&efx->mac_lock);
1904 efx_mac_reconfigure(efx);
1905 mutex_unlock(&efx->mac_lock);
1910 /* Context: netif_addr_lock held, BHs disabled. */
1911 static void efx_set_rx_mode(struct net_device *net_dev)
1913 struct efx_nic *efx = netdev_priv(net_dev);
1915 if (efx->port_enabled)
1916 queue_work(efx->workqueue, &efx->mac_work);
1917 /* Otherwise efx_start_port() will do this */
1920 static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
1922 struct efx_nic *efx = netdev_priv(net_dev);
1925 /* If disabling RX n-tuple filtering, clear existing filters */
1926 if (net_dev->features & ~data & NETIF_F_NTUPLE) {
1927 rc = efx->type->filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
1932 /* If Rx VLAN filter is changed, update filters via mac_reconfigure.
1933 * If rx-fcs is changed, mac_reconfigure updates that too.
1935 if ((net_dev->features ^ data) & (NETIF_F_HW_VLAN_CTAG_FILTER |
1937 /* efx_set_rx_mode() will schedule MAC work to update filters
1938 * when a new features are finally set in net_dev.
1940 efx_set_rx_mode(net_dev);
1946 static int efx_get_phys_port_id(struct net_device *net_dev,
1947 struct netdev_phys_item_id *ppid)
1949 struct efx_nic *efx = netdev_priv(net_dev);
1951 if (efx->type->get_phys_port_id)
1952 return efx->type->get_phys_port_id(efx, ppid);
1957 static int efx_get_phys_port_name(struct net_device *net_dev,
1958 char *name, size_t len)
1960 struct efx_nic *efx = netdev_priv(net_dev);
1962 if (snprintf(name, len, "p%u", efx->port_num) >= len)
1967 static int efx_vlan_rx_add_vid(struct net_device *net_dev, __be16 proto, u16 vid)
1969 struct efx_nic *efx = netdev_priv(net_dev);
1971 if (efx->type->vlan_rx_add_vid)
1972 return efx->type->vlan_rx_add_vid(efx, proto, vid);
1977 static int efx_vlan_rx_kill_vid(struct net_device *net_dev, __be16 proto, u16 vid)
1979 struct efx_nic *efx = netdev_priv(net_dev);
1981 if (efx->type->vlan_rx_kill_vid)
1982 return efx->type->vlan_rx_kill_vid(efx, proto, vid);
1987 static int efx_udp_tunnel_type_map(enum udp_parsable_tunnel_type in)
1990 case UDP_TUNNEL_TYPE_VXLAN:
1991 return TUNNEL_ENCAP_UDP_PORT_ENTRY_VXLAN;
1992 case UDP_TUNNEL_TYPE_GENEVE:
1993 return TUNNEL_ENCAP_UDP_PORT_ENTRY_GENEVE;
1999 static void efx_udp_tunnel_add(struct net_device *dev, struct udp_tunnel_info *ti)
2001 struct efx_nic *efx = netdev_priv(dev);
2002 struct efx_udp_tunnel tnl;
2003 int efx_tunnel_type;
2005 efx_tunnel_type = efx_udp_tunnel_type_map(ti->type);
2006 if (efx_tunnel_type < 0)
2009 tnl.type = (u16)efx_tunnel_type;
2010 tnl.port = ti->port;
2012 if (efx->type->udp_tnl_add_port)
2013 (void)efx->type->udp_tnl_add_port(efx, tnl);
2016 static void efx_udp_tunnel_del(struct net_device *dev, struct udp_tunnel_info *ti)
2018 struct efx_nic *efx = netdev_priv(dev);
2019 struct efx_udp_tunnel tnl;
2020 int efx_tunnel_type;
2022 efx_tunnel_type = efx_udp_tunnel_type_map(ti->type);
2023 if (efx_tunnel_type < 0)
2026 tnl.type = (u16)efx_tunnel_type;
2027 tnl.port = ti->port;
2029 if (efx->type->udp_tnl_del_port)
2030 (void)efx->type->udp_tnl_del_port(efx, tnl);
2033 static const struct net_device_ops efx_netdev_ops = {
2034 .ndo_open = efx_net_open,
2035 .ndo_stop = efx_net_stop,
2036 .ndo_get_stats64 = efx_net_stats,
2037 .ndo_tx_timeout = efx_watchdog,
2038 .ndo_start_xmit = efx_hard_start_xmit,
2039 .ndo_validate_addr = eth_validate_addr,
2040 .ndo_do_ioctl = efx_ioctl,
2041 .ndo_change_mtu = efx_change_mtu,
2042 .ndo_set_mac_address = efx_set_mac_address,
2043 .ndo_set_rx_mode = efx_set_rx_mode,
2044 .ndo_set_features = efx_set_features,
2045 .ndo_vlan_rx_add_vid = efx_vlan_rx_add_vid,
2046 .ndo_vlan_rx_kill_vid = efx_vlan_rx_kill_vid,
2047 #ifdef CONFIG_SFC_SRIOV
2048 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
2049 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
2050 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
2051 .ndo_get_vf_config = efx_sriov_get_vf_config,
2052 .ndo_set_vf_link_state = efx_sriov_set_vf_link_state,
2054 .ndo_get_phys_port_id = efx_get_phys_port_id,
2055 .ndo_get_phys_port_name = efx_get_phys_port_name,
2056 .ndo_setup_tc = efx_setup_tc,
2057 #ifdef CONFIG_RFS_ACCEL
2058 .ndo_rx_flow_steer = efx_filter_rfs,
2060 .ndo_udp_tunnel_add = efx_udp_tunnel_add,
2061 .ndo_udp_tunnel_del = efx_udp_tunnel_del,
2062 .ndo_xdp_xmit = efx_xdp_xmit,
2066 static int efx_xdp_setup_prog(struct efx_nic *efx, struct bpf_prog *prog)
2068 struct bpf_prog *old_prog;
2070 if (efx->xdp_rxq_info_failed) {
2071 netif_err(efx, drv, efx->net_dev,
2072 "Unable to bind XDP program due to previous failure of rxq_info\n");
2076 if (prog && efx->net_dev->mtu > efx_xdp_max_mtu(efx)) {
2077 netif_err(efx, drv, efx->net_dev,
2078 "Unable to configure XDP with MTU of %d (max: %d)\n",
2079 efx->net_dev->mtu, efx_xdp_max_mtu(efx));
2083 old_prog = rtnl_dereference(efx->xdp_prog);
2084 rcu_assign_pointer(efx->xdp_prog, prog);
2085 /* Release the reference that was originally passed by the caller. */
2087 bpf_prog_put(old_prog);
2092 /* Context: process, rtnl_lock() held. */
2093 static int efx_xdp(struct net_device *dev, struct netdev_bpf *xdp)
2095 struct efx_nic *efx = netdev_priv(dev);
2096 struct bpf_prog *xdp_prog;
2098 switch (xdp->command) {
2099 case XDP_SETUP_PROG:
2100 return efx_xdp_setup_prog(efx, xdp->prog);
2101 case XDP_QUERY_PROG:
2102 xdp_prog = rtnl_dereference(efx->xdp_prog);
2103 xdp->prog_id = xdp_prog ? xdp_prog->aux->id : 0;
2110 static int efx_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **xdpfs,
2113 struct efx_nic *efx = netdev_priv(dev);
2115 if (!netif_running(dev))
2118 return efx_xdp_tx_buffers(efx, n, xdpfs, flags & XDP_XMIT_FLUSH);
2121 static void efx_update_name(struct efx_nic *efx)
2123 strcpy(efx->name, efx->net_dev->name);
2124 efx_mtd_rename(efx);
2125 efx_set_channel_names(efx);
2128 static int efx_netdev_event(struct notifier_block *this,
2129 unsigned long event, void *ptr)
2131 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
2133 if ((net_dev->netdev_ops == &efx_netdev_ops) &&
2134 event == NETDEV_CHANGENAME)
2135 efx_update_name(netdev_priv(net_dev));
2140 static struct notifier_block efx_netdev_notifier = {
2141 .notifier_call = efx_netdev_event,
2145 show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
2147 struct efx_nic *efx = dev_get_drvdata(dev);
2148 return sprintf(buf, "%d\n", efx->phy_type);
2150 static DEVICE_ATTR(phy_type, 0444, show_phy_type, NULL);
2152 #ifdef CONFIG_SFC_MCDI_LOGGING
2153 static ssize_t show_mcdi_log(struct device *dev, struct device_attribute *attr,
2156 struct efx_nic *efx = dev_get_drvdata(dev);
2157 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
2159 return scnprintf(buf, PAGE_SIZE, "%d\n", mcdi->logging_enabled);
2161 static ssize_t set_mcdi_log(struct device *dev, struct device_attribute *attr,
2162 const char *buf, size_t count)
2164 struct efx_nic *efx = dev_get_drvdata(dev);
2165 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
2166 bool enable = count > 0 && *buf != '0';
2168 mcdi->logging_enabled = enable;
2171 static DEVICE_ATTR(mcdi_logging, 0644, show_mcdi_log, set_mcdi_log);
2174 static int efx_register_netdev(struct efx_nic *efx)
2176 struct net_device *net_dev = efx->net_dev;
2177 struct efx_channel *channel;
2180 net_dev->watchdog_timeo = 5 * HZ;
2181 net_dev->irq = efx->pci_dev->irq;
2182 net_dev->netdev_ops = &efx_netdev_ops;
2183 if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0)
2184 net_dev->priv_flags |= IFF_UNICAST_FLT;
2185 net_dev->ethtool_ops = &efx_ethtool_ops;
2186 net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
2187 net_dev->min_mtu = EFX_MIN_MTU;
2188 net_dev->max_mtu = EFX_MAX_MTU;
2192 /* Enable resets to be scheduled and check whether any were
2193 * already requested. If so, the NIC is probably hosed so we
2196 efx->state = STATE_READY;
2197 smp_mb(); /* ensure we change state before checking reset_pending */
2198 if (efx->reset_pending) {
2199 netif_err(efx, probe, efx->net_dev,
2200 "aborting probe due to scheduled reset\n");
2205 rc = dev_alloc_name(net_dev, net_dev->name);
2208 efx_update_name(efx);
2210 /* Always start with carrier off; PHY events will detect the link */
2211 netif_carrier_off(net_dev);
2213 rc = register_netdevice(net_dev);
2217 efx_for_each_channel(channel, efx) {
2218 struct efx_tx_queue *tx_queue;
2219 efx_for_each_channel_tx_queue(tx_queue, channel)
2220 efx_init_tx_queue_core_txq(tx_queue);
2227 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2229 netif_err(efx, drv, efx->net_dev,
2230 "failed to init net dev attributes\n");
2231 goto fail_registered;
2233 #ifdef CONFIG_SFC_MCDI_LOGGING
2234 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_mcdi_logging);
2236 netif_err(efx, drv, efx->net_dev,
2237 "failed to init net dev attributes\n");
2238 goto fail_attr_mcdi_logging;
2244 #ifdef CONFIG_SFC_MCDI_LOGGING
2245 fail_attr_mcdi_logging:
2246 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2250 efx_dissociate(efx);
2251 unregister_netdevice(net_dev);
2253 efx->state = STATE_UNINIT;
2255 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
2259 static void efx_unregister_netdev(struct efx_nic *efx)
2264 BUG_ON(netdev_priv(efx->net_dev) != efx);
2266 if (efx_dev_registered(efx)) {
2267 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2268 #ifdef CONFIG_SFC_MCDI_LOGGING
2269 device_remove_file(&efx->pci_dev->dev, &dev_attr_mcdi_logging);
2271 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2272 unregister_netdev(efx->net_dev);
2276 /**************************************************************************
2278 * List of NICs we support
2280 **************************************************************************/
2282 /* PCI device ID table */
2283 static const struct pci_device_id efx_pci_table[] = {
2284 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
2285 .driver_data = (unsigned long) &siena_a0_nic_type},
2286 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
2287 .driver_data = (unsigned long) &siena_a0_nic_type},
2288 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0903), /* SFC9120 PF */
2289 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
2290 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1903), /* SFC9120 VF */
2291 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
2292 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0923), /* SFC9140 PF */
2293 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
2294 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1923), /* SFC9140 VF */
2295 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
2296 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0a03), /* SFC9220 PF */
2297 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
2298 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1a03), /* SFC9220 VF */
2299 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
2300 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0b03), /* SFC9250 PF */
2301 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
2302 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1b03), /* SFC9250 VF */
2303 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
2304 {0} /* end of list */
2307 /**************************************************************************
2311 **************************************************************************/
2313 int efx_init_channels(struct efx_nic *efx)
2317 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
2318 efx->channel[i] = efx_alloc_channel(efx, i, NULL);
2319 if (!efx->channel[i])
2321 efx->msi_context[i].efx = efx;
2322 efx->msi_context[i].index = i;
2325 /* Higher numbered interrupt modes are less capable! */
2326 if (WARN_ON_ONCE(efx->type->max_interrupt_mode >
2327 efx->type->min_interrupt_mode)) {
2330 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2332 efx->interrupt_mode = min(efx->type->min_interrupt_mode,
2338 void efx_update_sw_stats(struct efx_nic *efx, u64 *stats)
2340 u64 n_rx_nodesc_trunc = 0;
2341 struct efx_channel *channel;
2343 efx_for_each_channel(channel, efx)
2344 n_rx_nodesc_trunc += channel->n_rx_nodesc_trunc;
2345 stats[GENERIC_STAT_rx_nodesc_trunc] = n_rx_nodesc_trunc;
2346 stats[GENERIC_STAT_rx_noskb_drops] = atomic_read(&efx->n_rx_noskb_drops);
2349 bool efx_filter_spec_equal(const struct efx_filter_spec *left,
2350 const struct efx_filter_spec *right)
2352 if ((left->match_flags ^ right->match_flags) |
2353 ((left->flags ^ right->flags) &
2354 (EFX_FILTER_FLAG_RX | EFX_FILTER_FLAG_TX)))
2357 return memcmp(&left->outer_vid, &right->outer_vid,
2358 sizeof(struct efx_filter_spec) -
2359 offsetof(struct efx_filter_spec, outer_vid)) == 0;
2362 u32 efx_filter_spec_hash(const struct efx_filter_spec *spec)
2364 BUILD_BUG_ON(offsetof(struct efx_filter_spec, outer_vid) & 3);
2365 return jhash2((const u32 *)&spec->outer_vid,
2366 (sizeof(struct efx_filter_spec) -
2367 offsetof(struct efx_filter_spec, outer_vid)) / 4,
2371 #ifdef CONFIG_RFS_ACCEL
2372 bool efx_rps_check_rule(struct efx_arfs_rule *rule, unsigned int filter_idx,
2375 if (rule->filter_id == EFX_ARFS_FILTER_ID_PENDING) {
2376 /* ARFS is currently updating this entry, leave it */
2379 if (rule->filter_id == EFX_ARFS_FILTER_ID_ERROR) {
2380 /* ARFS tried and failed to update this, so it's probably out
2381 * of date. Remove the filter and the ARFS rule entry.
2383 rule->filter_id = EFX_ARFS_FILTER_ID_REMOVING;
2386 } else if (WARN_ON(rule->filter_id != filter_idx)) { /* can't happen */
2387 /* ARFS has moved on, so old filter is not needed. Since we did
2388 * not mark the rule with EFX_ARFS_FILTER_ID_REMOVING, it will
2389 * not be removed by efx_rps_hash_del() subsequently.
2394 /* Remove it iff ARFS wants to. */
2399 struct hlist_head *efx_rps_hash_bucket(struct efx_nic *efx,
2400 const struct efx_filter_spec *spec)
2402 u32 hash = efx_filter_spec_hash(spec);
2404 lockdep_assert_held(&efx->rps_hash_lock);
2405 if (!efx->rps_hash_table)
2407 return &efx->rps_hash_table[hash % EFX_ARFS_HASH_TABLE_SIZE];
2410 struct efx_arfs_rule *efx_rps_hash_find(struct efx_nic *efx,
2411 const struct efx_filter_spec *spec)
2413 struct efx_arfs_rule *rule;
2414 struct hlist_head *head;
2415 struct hlist_node *node;
2417 head = efx_rps_hash_bucket(efx, spec);
2420 hlist_for_each(node, head) {
2421 rule = container_of(node, struct efx_arfs_rule, node);
2422 if (efx_filter_spec_equal(spec, &rule->spec))
2428 struct efx_arfs_rule *efx_rps_hash_add(struct efx_nic *efx,
2429 const struct efx_filter_spec *spec,
2432 struct efx_arfs_rule *rule;
2433 struct hlist_head *head;
2434 struct hlist_node *node;
2436 head = efx_rps_hash_bucket(efx, spec);
2439 hlist_for_each(node, head) {
2440 rule = container_of(node, struct efx_arfs_rule, node);
2441 if (efx_filter_spec_equal(spec, &rule->spec)) {
2446 rule = kmalloc(sizeof(*rule), GFP_ATOMIC);
2449 memcpy(&rule->spec, spec, sizeof(rule->spec));
2450 hlist_add_head(&rule->node, head);
2455 void efx_rps_hash_del(struct efx_nic *efx, const struct efx_filter_spec *spec)
2457 struct efx_arfs_rule *rule;
2458 struct hlist_head *head;
2459 struct hlist_node *node;
2461 head = efx_rps_hash_bucket(efx, spec);
2464 hlist_for_each(node, head) {
2465 rule = container_of(node, struct efx_arfs_rule, node);
2466 if (efx_filter_spec_equal(spec, &rule->spec)) {
2467 /* Someone already reused the entry. We know that if
2468 * this check doesn't fire (i.e. filter_id == REMOVING)
2469 * then the REMOVING mark was put there by our caller,
2470 * because caller is holding a lock on filter table and
2471 * only holders of that lock set REMOVING.
2473 if (rule->filter_id != EFX_ARFS_FILTER_ID_REMOVING)
2480 /* We didn't find it. */
2485 /* RSS contexts. We're using linked lists and crappy O(n) algorithms, because
2486 * (a) this is an infrequent control-plane operation and (b) n is small (max 64)
2488 struct efx_rss_context *efx_alloc_rss_context_entry(struct efx_nic *efx)
2490 struct list_head *head = &efx->rss_context.list;
2491 struct efx_rss_context *ctx, *new;
2492 u32 id = 1; /* Don't use zero, that refers to the master RSS context */
2494 WARN_ON(!mutex_is_locked(&efx->rss_lock));
2496 /* Search for first gap in the numbering */
2497 list_for_each_entry(ctx, head, list) {
2498 if (ctx->user_id != id)
2501 /* Check for wrap. If this happens, we have nearly 2^32
2502 * allocated RSS contexts, which seems unlikely.
2504 if (WARN_ON_ONCE(!id))
2508 /* Create the new entry */
2509 new = kmalloc(sizeof(struct efx_rss_context), GFP_KERNEL);
2512 new->context_id = EFX_EF10_RSS_CONTEXT_INVALID;
2513 new->rx_hash_udp_4tuple = false;
2515 /* Insert the new entry into the gap */
2517 list_add_tail(&new->list, &ctx->list);
2521 struct efx_rss_context *efx_find_rss_context_entry(struct efx_nic *efx, u32 id)
2523 struct list_head *head = &efx->rss_context.list;
2524 struct efx_rss_context *ctx;
2526 WARN_ON(!mutex_is_locked(&efx->rss_lock));
2528 list_for_each_entry(ctx, head, list)
2529 if (ctx->user_id == id)
2534 void efx_free_rss_context_entry(struct efx_rss_context *ctx)
2536 list_del(&ctx->list);
2540 /**************************************************************************
2544 **************************************************************************/
2546 /* Main body of final NIC shutdown code
2547 * This is called only at module unload (or hotplug removal).
2549 static void efx_pci_remove_main(struct efx_nic *efx)
2551 /* Flush reset_work. It can no longer be scheduled since we
2554 BUG_ON(efx->state == STATE_READY);
2555 efx_flush_reset_workqueue(efx);
2557 efx_disable_interrupts(efx);
2558 efx_clear_interrupt_affinity(efx);
2559 efx_nic_fini_interrupt(efx);
2561 efx->type->fini(efx);
2563 efx_remove_all(efx);
2566 /* Final NIC shutdown
2567 * This is called only at module unload (or hotplug removal). A PF can call
2568 * this on its VFs to ensure they are unbound first.
2570 static void efx_pci_remove(struct pci_dev *pci_dev)
2572 struct efx_nic *efx;
2574 efx = pci_get_drvdata(pci_dev);
2578 /* Mark the NIC as fini, then stop the interface */
2580 efx_dissociate(efx);
2581 dev_close(efx->net_dev);
2582 efx_disable_interrupts(efx);
2583 efx->state = STATE_UNINIT;
2586 if (efx->type->sriov_fini)
2587 efx->type->sriov_fini(efx);
2589 efx_unregister_netdev(efx);
2591 efx_mtd_remove(efx);
2593 efx_pci_remove_main(efx);
2595 efx_fini_io(efx, efx->type->mem_bar(efx));
2596 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
2598 efx_fini_struct(efx);
2599 free_netdev(efx->net_dev);
2601 pci_disable_pcie_error_reporting(pci_dev);
2604 /* NIC VPD information
2605 * Called during probe to display the part number of the
2606 * installed NIC. VPD is potentially very large but this should
2607 * always appear within the first 512 bytes.
2609 #define SFC_VPD_LEN 512
2610 static void efx_probe_vpd_strings(struct efx_nic *efx)
2612 struct pci_dev *dev = efx->pci_dev;
2613 char vpd_data[SFC_VPD_LEN];
2615 int ro_start, ro_size, i, j;
2617 /* Get the vpd data from the device */
2618 vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
2619 if (vpd_size <= 0) {
2620 netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
2624 /* Get the Read only section */
2625 ro_start = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
2627 netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
2631 ro_size = pci_vpd_lrdt_size(&vpd_data[ro_start]);
2633 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
2634 if (i + j > vpd_size)
2637 /* Get the Part number */
2638 i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
2640 netif_err(efx, drv, efx->net_dev, "Part number not found\n");
2644 j = pci_vpd_info_field_size(&vpd_data[i]);
2645 i += PCI_VPD_INFO_FLD_HDR_SIZE;
2646 if (i + j > vpd_size) {
2647 netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
2651 netif_info(efx, drv, efx->net_dev,
2652 "Part Number : %.*s\n", j, &vpd_data[i]);
2654 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
2656 i = pci_vpd_find_info_keyword(vpd_data, i, j, "SN");
2658 netif_err(efx, drv, efx->net_dev, "Serial number not found\n");
2662 j = pci_vpd_info_field_size(&vpd_data[i]);
2663 i += PCI_VPD_INFO_FLD_HDR_SIZE;
2664 if (i + j > vpd_size) {
2665 netif_err(efx, drv, efx->net_dev, "Incomplete serial number\n");
2669 efx->vpd_sn = kmalloc(j + 1, GFP_KERNEL);
2673 snprintf(efx->vpd_sn, j + 1, "%s", &vpd_data[i]);
2677 /* Main body of NIC initialisation
2678 * This is called at module load (or hotplug insertion, theoretically).
2680 static int efx_pci_probe_main(struct efx_nic *efx)
2684 /* Do start-of-day initialisation */
2685 rc = efx_probe_all(efx);
2691 down_write(&efx->filter_sem);
2692 rc = efx->type->init(efx);
2693 up_write(&efx->filter_sem);
2695 netif_err(efx, probe, efx->net_dev,
2696 "failed to initialise NIC\n");
2700 rc = efx_init_port(efx);
2702 netif_err(efx, probe, efx->net_dev,
2703 "failed to initialise port\n");
2707 rc = efx_nic_init_interrupt(efx);
2711 efx_set_interrupt_affinity(efx);
2712 rc = efx_enable_interrupts(efx);
2719 efx_clear_interrupt_affinity(efx);
2720 efx_nic_fini_interrupt(efx);
2724 efx->type->fini(efx);
2727 efx_remove_all(efx);
2732 static int efx_pci_probe_post_io(struct efx_nic *efx)
2734 struct net_device *net_dev = efx->net_dev;
2735 int rc = efx_pci_probe_main(efx);
2740 if (efx->type->sriov_init) {
2741 rc = efx->type->sriov_init(efx);
2743 netif_err(efx, probe, efx->net_dev,
2744 "SR-IOV can't be enabled rc %d\n", rc);
2747 /* Determine netdevice features */
2748 net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
2749 NETIF_F_TSO | NETIF_F_RXCSUM | NETIF_F_RXALL);
2750 if (efx->type->offload_features & (NETIF_F_IPV6_CSUM | NETIF_F_HW_CSUM))
2751 net_dev->features |= NETIF_F_TSO6;
2752 /* Check whether device supports TSO */
2753 if (!efx->type->tso_versions || !efx->type->tso_versions(efx))
2754 net_dev->features &= ~NETIF_F_ALL_TSO;
2755 /* Mask for features that also apply to VLAN devices */
2756 net_dev->vlan_features |= (NETIF_F_HW_CSUM | NETIF_F_SG |
2757 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
2760 net_dev->hw_features |= net_dev->features & ~efx->fixed_features;
2762 /* Disable receiving frames with bad FCS, by default. */
2763 net_dev->features &= ~NETIF_F_RXALL;
2765 /* Disable VLAN filtering by default. It may be enforced if
2766 * the feature is fixed (i.e. VLAN filters are required to
2767 * receive VLAN tagged packets due to vPort restrictions).
2769 net_dev->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
2770 net_dev->features |= efx->fixed_features;
2772 rc = efx_register_netdev(efx);
2776 efx_pci_remove_main(efx);
2780 /* NIC initialisation
2782 * This is called at module load (or hotplug insertion,
2783 * theoretically). It sets up PCI mappings, resets the NIC,
2784 * sets up and registers the network devices with the kernel and hooks
2785 * the interrupt service routine. It does not prepare the device for
2786 * transmission; this is left to the first time one of the network
2787 * interfaces is brought up (i.e. efx_net_open).
2789 static int efx_pci_probe(struct pci_dev *pci_dev,
2790 const struct pci_device_id *entry)
2792 struct net_device *net_dev;
2793 struct efx_nic *efx;
2796 /* Allocate and initialise a struct net_device and struct efx_nic */
2797 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
2801 efx = netdev_priv(net_dev);
2802 efx->type = (const struct efx_nic_type *) entry->driver_data;
2803 efx->fixed_features |= NETIF_F_HIGHDMA;
2805 pci_set_drvdata(pci_dev, efx);
2806 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
2807 rc = efx_init_struct(efx, pci_dev, net_dev);
2811 netif_info(efx, probe, efx->net_dev,
2812 "Solarflare NIC detected\n");
2814 if (!efx->type->is_vf)
2815 efx_probe_vpd_strings(efx);
2817 /* Set up basic I/O (BAR mappings etc) */
2818 rc = efx_init_io(efx, efx->type->mem_bar(efx), efx->type->max_dma_mask,
2819 efx->type->mem_map_size(efx));
2823 rc = efx_pci_probe_post_io(efx);
2825 /* On failure, retry once immediately.
2826 * If we aborted probe due to a scheduled reset, dismiss it.
2828 efx->reset_pending = 0;
2829 rc = efx_pci_probe_post_io(efx);
2831 /* On another failure, retry once more
2832 * after a 50-305ms delay.
2836 get_random_bytes(&r, 1);
2837 msleep((unsigned int)r + 50);
2838 efx->reset_pending = 0;
2839 rc = efx_pci_probe_post_io(efx);
2845 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
2847 /* Try to create MTDs, but allow this to fail */
2849 rc = efx_mtd_probe(efx);
2851 if (rc && rc != -EPERM)
2852 netif_warn(efx, probe, efx->net_dev,
2853 "failed to create MTDs (%d)\n", rc);
2855 (void)pci_enable_pcie_error_reporting(pci_dev);
2857 if (efx->type->udp_tnl_push_ports)
2858 efx->type->udp_tnl_push_ports(efx);
2863 efx_fini_io(efx, efx->type->mem_bar(efx));
2865 efx_fini_struct(efx);
2868 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
2869 free_netdev(net_dev);
2873 /* efx_pci_sriov_configure returns the actual number of Virtual Functions
2874 * enabled on success
2876 #ifdef CONFIG_SFC_SRIOV
2877 static int efx_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
2880 struct efx_nic *efx = pci_get_drvdata(dev);
2882 if (efx->type->sriov_configure) {
2883 rc = efx->type->sriov_configure(efx, num_vfs);
2893 static int efx_pm_freeze(struct device *dev)
2895 struct efx_nic *efx = dev_get_drvdata(dev);
2899 if (efx->state != STATE_DISABLED) {
2900 efx->state = STATE_UNINIT;
2902 efx_device_detach_sync(efx);
2905 efx_disable_interrupts(efx);
2913 static int efx_pm_thaw(struct device *dev)
2916 struct efx_nic *efx = dev_get_drvdata(dev);
2920 if (efx->state != STATE_DISABLED) {
2921 rc = efx_enable_interrupts(efx);
2925 mutex_lock(&efx->mac_lock);
2926 efx->phy_op->reconfigure(efx);
2927 mutex_unlock(&efx->mac_lock);
2931 efx_device_attach_if_not_resetting(efx);
2933 efx->state = STATE_READY;
2935 efx->type->resume_wol(efx);
2940 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
2941 efx_queue_reset_work(efx);
2951 static int efx_pm_poweroff(struct device *dev)
2953 struct pci_dev *pci_dev = to_pci_dev(dev);
2954 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2956 efx->type->fini(efx);
2958 efx->reset_pending = 0;
2960 pci_save_state(pci_dev);
2961 return pci_set_power_state(pci_dev, PCI_D3hot);
2964 /* Used for both resume and restore */
2965 static int efx_pm_resume(struct device *dev)
2967 struct pci_dev *pci_dev = to_pci_dev(dev);
2968 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2971 rc = pci_set_power_state(pci_dev, PCI_D0);
2974 pci_restore_state(pci_dev);
2975 rc = pci_enable_device(pci_dev);
2978 pci_set_master(efx->pci_dev);
2979 rc = efx->type->reset(efx, RESET_TYPE_ALL);
2982 down_write(&efx->filter_sem);
2983 rc = efx->type->init(efx);
2984 up_write(&efx->filter_sem);
2987 rc = efx_pm_thaw(dev);
2991 static int efx_pm_suspend(struct device *dev)
2996 rc = efx_pm_poweroff(dev);
3002 static const struct dev_pm_ops efx_pm_ops = {
3003 .suspend = efx_pm_suspend,
3004 .resume = efx_pm_resume,
3005 .freeze = efx_pm_freeze,
3006 .thaw = efx_pm_thaw,
3007 .poweroff = efx_pm_poweroff,
3008 .restore = efx_pm_resume,
3011 /* A PCI error affecting this device was detected.
3012 * At this point MMIO and DMA may be disabled.
3013 * Stop the software path and request a slot reset.
3015 static pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
3016 enum pci_channel_state state)
3018 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
3019 struct efx_nic *efx = pci_get_drvdata(pdev);
3021 if (state == pci_channel_io_perm_failure)
3022 return PCI_ERS_RESULT_DISCONNECT;
3026 if (efx->state != STATE_DISABLED) {
3027 efx->state = STATE_RECOVERY;
3028 efx->reset_pending = 0;
3030 efx_device_detach_sync(efx);
3033 efx_disable_interrupts(efx);
3035 status = PCI_ERS_RESULT_NEED_RESET;
3037 /* If the interface is disabled we don't want to do anything
3040 status = PCI_ERS_RESULT_RECOVERED;
3045 pci_disable_device(pdev);
3050 /* Fake a successful reset, which will be performed later in efx_io_resume. */
3051 static pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
3053 struct efx_nic *efx = pci_get_drvdata(pdev);
3054 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
3056 if (pci_enable_device(pdev)) {
3057 netif_err(efx, hw, efx->net_dev,
3058 "Cannot re-enable PCI device after reset.\n");
3059 status = PCI_ERS_RESULT_DISCONNECT;
3065 /* Perform the actual reset and resume I/O operations. */
3066 static void efx_io_resume(struct pci_dev *pdev)
3068 struct efx_nic *efx = pci_get_drvdata(pdev);
3073 if (efx->state == STATE_DISABLED)
3076 rc = efx_reset(efx, RESET_TYPE_ALL);
3078 netif_err(efx, hw, efx->net_dev,
3079 "efx_reset failed after PCI error (%d)\n", rc);
3081 efx->state = STATE_READY;
3082 netif_dbg(efx, hw, efx->net_dev,
3083 "Done resetting and resuming IO after PCI error.\n");
3090 /* For simplicity and reliability, we always require a slot reset and try to
3091 * reset the hardware when a pci error affecting the device is detected.
3092 * We leave both the link_reset and mmio_enabled callback unimplemented:
3093 * with our request for slot reset the mmio_enabled callback will never be
3094 * called, and the link_reset callback is not used by AER or EEH mechanisms.
3096 static const struct pci_error_handlers efx_err_handlers = {
3097 .error_detected = efx_io_error_detected,
3098 .slot_reset = efx_io_slot_reset,
3099 .resume = efx_io_resume,
3102 static struct pci_driver efx_pci_driver = {
3103 .name = KBUILD_MODNAME,
3104 .id_table = efx_pci_table,
3105 .probe = efx_pci_probe,
3106 .remove = efx_pci_remove,
3107 .driver.pm = &efx_pm_ops,
3108 .err_handler = &efx_err_handlers,
3109 #ifdef CONFIG_SFC_SRIOV
3110 .sriov_configure = efx_pci_sriov_configure,
3114 /**************************************************************************
3116 * Kernel module interface
3118 *************************************************************************/
3120 module_param(interrupt_mode, uint, 0444);
3121 MODULE_PARM_DESC(interrupt_mode,
3122 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
3124 static int __init efx_init_module(void)
3128 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
3130 rc = register_netdevice_notifier(&efx_netdev_notifier);
3134 #ifdef CONFIG_SFC_SRIOV
3135 rc = efx_init_sriov();
3140 rc = efx_create_reset_workqueue();
3144 rc = pci_register_driver(&efx_pci_driver);
3151 efx_destroy_reset_workqueue();
3153 #ifdef CONFIG_SFC_SRIOV
3157 unregister_netdevice_notifier(&efx_netdev_notifier);
3162 static void __exit efx_exit_module(void)
3164 printk(KERN_INFO "Solarflare NET driver unloading\n");
3166 pci_unregister_driver(&efx_pci_driver);
3167 efx_destroy_reset_workqueue();
3168 #ifdef CONFIG_SFC_SRIOV
3171 unregister_netdevice_notifier(&efx_netdev_notifier);
3175 module_init(efx_init_module);
3176 module_exit(efx_exit_module);
3178 MODULE_AUTHOR("Solarflare Communications and "
3179 "Michael Brown <mbrown@fensystems.co.uk>");
3180 MODULE_DESCRIPTION("Solarflare network driver");
3181 MODULE_LICENSE("GPL");
3182 MODULE_DEVICE_TABLE(pci, efx_pci_table);
3183 MODULE_VERSION(EFX_DRIVER_VERSION);