1768fd266SAlex Maftei (amaftei) // SPDX-License-Identifier: GPL-2.0-only
2768fd266SAlex Maftei (amaftei) /****************************************************************************
3768fd266SAlex Maftei (amaftei)  * Driver for Solarflare network controllers and boards
4768fd266SAlex Maftei (amaftei)  * Copyright 2018 Solarflare Communications Inc.
5768fd266SAlex Maftei (amaftei)  *
6768fd266SAlex Maftei (amaftei)  * This program is free software; you can redistribute it and/or modify it
7768fd266SAlex Maftei (amaftei)  * under the terms of the GNU General Public License version 2 as published
8768fd266SAlex Maftei (amaftei)  * by the Free Software Foundation, incorporated herein by reference.
9768fd266SAlex Maftei (amaftei)  */
10768fd266SAlex Maftei (amaftei) 
11768fd266SAlex Maftei (amaftei) #include "net_driver.h"
12768fd266SAlex Maftei (amaftei) #include <linux/module.h>
13b6459415SJakub Kicinski #include <linux/filter.h>
14768fd266SAlex Maftei (amaftei) #include "efx_channels.h"
15768fd266SAlex Maftei (amaftei) #include "efx.h"
16768fd266SAlex Maftei (amaftei) #include "efx_common.h"
17768fd266SAlex Maftei (amaftei) #include "tx_common.h"
18768fd266SAlex Maftei (amaftei) #include "rx_common.h"
19768fd266SAlex Maftei (amaftei) #include "nic.h"
20768fd266SAlex Maftei (amaftei) #include "sriov.h"
21e26ca4b5SIvan Babrou #include "workarounds.h"
22768fd266SAlex Maftei (amaftei) 
2383975485SAlex Maftei (amaftei) /* This is the first interrupt mode to try out of:
2483975485SAlex Maftei (amaftei)  * 0 => MSI-X
2583975485SAlex Maftei (amaftei)  * 1 => MSI
2683975485SAlex Maftei (amaftei)  * 2 => legacy
2783975485SAlex Maftei (amaftei)  */
28e4ff3232SEdward Cree unsigned int efx_interrupt_mode = EFX_INT_MODE_MSIX;
2983975485SAlex Maftei (amaftei) 
3037c45a4eSAlex Maftei (amaftei) /* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
3137c45a4eSAlex Maftei (amaftei)  * i.e. the number of CPUs among which we may distribute simultaneous
3237c45a4eSAlex Maftei (amaftei)  * interrupt handling.
3337c45a4eSAlex Maftei (amaftei)  *
3437c45a4eSAlex Maftei (amaftei)  * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
3537c45a4eSAlex Maftei (amaftei)  * The default (0) means to assign an interrupt to each core.
3637c45a4eSAlex Maftei (amaftei)  */
3767e6398eSEdward Cree unsigned int rss_cpus;
3837c45a4eSAlex Maftei (amaftei) 
39768fd266SAlex Maftei (amaftei) static unsigned int irq_adapt_low_thresh = 8000;
40768fd266SAlex Maftei (amaftei) module_param(irq_adapt_low_thresh, uint, 0644);
41768fd266SAlex Maftei (amaftei) MODULE_PARM_DESC(irq_adapt_low_thresh,
42768fd266SAlex Maftei (amaftei) 		 "Threshold score for reducing IRQ moderation");
43768fd266SAlex Maftei (amaftei) 
44768fd266SAlex Maftei (amaftei) static unsigned int irq_adapt_high_thresh = 16000;
45768fd266SAlex Maftei (amaftei) module_param(irq_adapt_high_thresh, uint, 0644);
46768fd266SAlex Maftei (amaftei) MODULE_PARM_DESC(irq_adapt_high_thresh,
47768fd266SAlex Maftei (amaftei) 		 "Threshold score for increasing IRQ moderation");
48768fd266SAlex Maftei (amaftei) 
49768fd266SAlex Maftei (amaftei) /* This is the weight assigned to each of the (per-channel) virtual
50768fd266SAlex Maftei (amaftei)  * NAPI devices.
51768fd266SAlex Maftei (amaftei)  */
52768fd266SAlex Maftei (amaftei) static int napi_weight = 64;
53768fd266SAlex Maftei (amaftei) 
5483975485SAlex Maftei (amaftei) /***************
5583975485SAlex Maftei (amaftei)  * Housekeeping
5683975485SAlex Maftei (amaftei)  ***************/
5783975485SAlex Maftei (amaftei) 
5883975485SAlex Maftei (amaftei) int efx_channel_dummy_op_int(struct efx_channel *channel)
5983975485SAlex Maftei (amaftei) {
6083975485SAlex Maftei (amaftei) 	return 0;
6183975485SAlex Maftei (amaftei) }
6283975485SAlex Maftei (amaftei) 
6383975485SAlex Maftei (amaftei) void efx_channel_dummy_op_void(struct efx_channel *channel)
6483975485SAlex Maftei (amaftei) {
6583975485SAlex Maftei (amaftei) }
6683975485SAlex Maftei (amaftei) 
6783975485SAlex Maftei (amaftei) static const struct efx_channel_type efx_default_channel_type = {
6883975485SAlex Maftei (amaftei) 	.pre_probe		= efx_channel_dummy_op_int,
6983975485SAlex Maftei (amaftei) 	.post_remove		= efx_channel_dummy_op_void,
7083975485SAlex Maftei (amaftei) 	.get_name		= efx_get_channel_name,
7183975485SAlex Maftei (amaftei) 	.copy			= efx_copy_channel,
7283975485SAlex Maftei (amaftei) 	.want_txqs		= efx_default_channel_want_txqs,
7383975485SAlex Maftei (amaftei) 	.keep_eventq		= false,
7483975485SAlex Maftei (amaftei) 	.want_pio		= true,
7583975485SAlex Maftei (amaftei) };
7683975485SAlex Maftei (amaftei) 
7737c45a4eSAlex Maftei (amaftei) /*************
7837c45a4eSAlex Maftei (amaftei)  * INTERRUPTS
7937c45a4eSAlex Maftei (amaftei)  *************/
8037c45a4eSAlex Maftei (amaftei) 
81c265b569SÍñigo Huguet static unsigned int count_online_cores(struct efx_nic *efx, bool local_node)
8237c45a4eSAlex Maftei (amaftei) {
83c265b569SÍñigo Huguet 	cpumask_var_t filter_mask;
8437c45a4eSAlex Maftei (amaftei) 	unsigned int count;
8537c45a4eSAlex Maftei (amaftei) 	int cpu;
8637c45a4eSAlex Maftei (amaftei) 
87c265b569SÍñigo Huguet 	if (unlikely(!zalloc_cpumask_var(&filter_mask, GFP_KERNEL))) {
8837c45a4eSAlex Maftei (amaftei) 		netif_warn(efx, probe, efx->net_dev,
8937c45a4eSAlex Maftei (amaftei) 			   "RSS disabled due to allocation failure\n");
9037c45a4eSAlex Maftei (amaftei) 		return 1;
9137c45a4eSAlex Maftei (amaftei) 	}
9237c45a4eSAlex Maftei (amaftei) 
93c265b569SÍñigo Huguet 	cpumask_copy(filter_mask, cpu_online_mask);
94c265b569SÍñigo Huguet 	if (local_node) {
95c265b569SÍñigo Huguet 		int numa_node = pcibus_to_node(efx->pci_dev->bus);
96c265b569SÍñigo Huguet 
97c265b569SÍñigo Huguet 		cpumask_and(filter_mask, filter_mask, cpumask_of_node(numa_node));
9837c45a4eSAlex Maftei (amaftei) 	}
9937c45a4eSAlex Maftei (amaftei) 
100c265b569SÍñigo Huguet 	count = 0;
101c265b569SÍñigo Huguet 	for_each_cpu(cpu, filter_mask) {
102c265b569SÍñigo Huguet 		++count;
103c265b569SÍñigo Huguet 		cpumask_andnot(filter_mask, filter_mask, topology_sibling_cpumask(cpu));
104c265b569SÍñigo Huguet 	}
105c265b569SÍñigo Huguet 
106c265b569SÍñigo Huguet 	free_cpumask_var(filter_mask);
107c265b569SÍñigo Huguet 
108c265b569SÍñigo Huguet 	return count;
109c265b569SÍñigo Huguet }
110c265b569SÍñigo Huguet 
111c265b569SÍñigo Huguet static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
112c265b569SÍñigo Huguet {
113c265b569SÍñigo Huguet 	unsigned int count;
114c265b569SÍñigo Huguet 
115c265b569SÍñigo Huguet 	if (rss_cpus) {
116c265b569SÍñigo Huguet 		count = rss_cpus;
117c265b569SÍñigo Huguet 	} else {
118c265b569SÍñigo Huguet 		count = count_online_cores(efx, true);
119c265b569SÍñigo Huguet 
120c265b569SÍñigo Huguet 		/* If no online CPUs in local node, fallback to any online CPUs */
121c265b569SÍñigo Huguet 		if (count == 0)
122c265b569SÍñigo Huguet 			count = count_online_cores(efx, false);
12337c45a4eSAlex Maftei (amaftei) 	}
12437c45a4eSAlex Maftei (amaftei) 
12537c45a4eSAlex Maftei (amaftei) 	if (count > EFX_MAX_RX_QUEUES) {
12637c45a4eSAlex Maftei (amaftei) 		netif_cond_dbg(efx, probe, efx->net_dev, !rss_cpus, warn,
12737c45a4eSAlex Maftei (amaftei) 			       "Reducing number of rx queues from %u to %u.\n",
12837c45a4eSAlex Maftei (amaftei) 			       count, EFX_MAX_RX_QUEUES);
12937c45a4eSAlex Maftei (amaftei) 		count = EFX_MAX_RX_QUEUES;
13037c45a4eSAlex Maftei (amaftei) 	}
13137c45a4eSAlex Maftei (amaftei) 
13237c45a4eSAlex Maftei (amaftei) 	/* If RSS is requested for the PF *and* VFs then we can't write RSS
13337c45a4eSAlex Maftei (amaftei) 	 * table entries that are inaccessible to VFs
13437c45a4eSAlex Maftei (amaftei) 	 */
13537c45a4eSAlex Maftei (amaftei) #ifdef CONFIG_SFC_SRIOV
13637c45a4eSAlex Maftei (amaftei) 	if (efx->type->sriov_wanted) {
13737c45a4eSAlex Maftei (amaftei) 		if (efx->type->sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
13837c45a4eSAlex Maftei (amaftei) 		    count > efx_vf_size(efx)) {
13937c45a4eSAlex Maftei (amaftei) 			netif_warn(efx, probe, efx->net_dev,
14037c45a4eSAlex Maftei (amaftei) 				   "Reducing number of RSS channels from %u to %u for "
14137c45a4eSAlex Maftei (amaftei) 				   "VF support. Increase vf-msix-limit to use more "
14237c45a4eSAlex Maftei (amaftei) 				   "channels on the PF.\n",
14337c45a4eSAlex Maftei (amaftei) 				   count, efx_vf_size(efx));
14437c45a4eSAlex Maftei (amaftei) 			count = efx_vf_size(efx);
14537c45a4eSAlex Maftei (amaftei) 		}
14637c45a4eSAlex Maftei (amaftei) 	}
14737c45a4eSAlex Maftei (amaftei) #endif
14837c45a4eSAlex Maftei (amaftei) 
14937c45a4eSAlex Maftei (amaftei) 	return count;
15037c45a4eSAlex Maftei (amaftei) }
15137c45a4eSAlex Maftei (amaftei) 
15237c45a4eSAlex Maftei (amaftei) static int efx_allocate_msix_channels(struct efx_nic *efx,
15337c45a4eSAlex Maftei (amaftei) 				      unsigned int max_channels,
15437c45a4eSAlex Maftei (amaftei) 				      unsigned int extra_channels,
15537c45a4eSAlex Maftei (amaftei) 				      unsigned int parallelism)
15637c45a4eSAlex Maftei (amaftei) {
15737c45a4eSAlex Maftei (amaftei) 	unsigned int n_channels = parallelism;
15837c45a4eSAlex Maftei (amaftei) 	int vec_count;
159e26ca4b5SIvan Babrou 	int tx_per_ev;
16037c45a4eSAlex Maftei (amaftei) 	int n_xdp_tx;
16137c45a4eSAlex Maftei (amaftei) 	int n_xdp_ev;
16237c45a4eSAlex Maftei (amaftei) 
16337c45a4eSAlex Maftei (amaftei) 	if (efx_separate_tx_channels)
16437c45a4eSAlex Maftei (amaftei) 		n_channels *= 2;
16537c45a4eSAlex Maftei (amaftei) 	n_channels += extra_channels;
16637c45a4eSAlex Maftei (amaftei) 
16737c45a4eSAlex Maftei (amaftei) 	/* To allow XDP transmit to happen from arbitrary NAPI contexts
16837c45a4eSAlex Maftei (amaftei) 	 * we allocate a TX queue per CPU. We share event queues across
16937c45a4eSAlex Maftei (amaftei) 	 * multiple tx queues, assuming tx and ev queues are both
17037c45a4eSAlex Maftei (amaftei) 	 * maximum size.
17137c45a4eSAlex Maftei (amaftei) 	 */
172e26ca4b5SIvan Babrou 	tx_per_ev = EFX_MAX_EVQ_SIZE / EFX_TXQ_MAX_ENT(efx);
173f28100cbSÍñigo Huguet 	tx_per_ev = min(tx_per_ev, EFX_MAX_TXQ_PER_CHANNEL);
17437c45a4eSAlex Maftei (amaftei) 	n_xdp_tx = num_possible_cpus();
175e26ca4b5SIvan Babrou 	n_xdp_ev = DIV_ROUND_UP(n_xdp_tx, tx_per_ev);
17637c45a4eSAlex Maftei (amaftei) 
17737c45a4eSAlex Maftei (amaftei) 	vec_count = pci_msix_vec_count(efx->pci_dev);
17837c45a4eSAlex Maftei (amaftei) 	if (vec_count < 0)
17937c45a4eSAlex Maftei (amaftei) 		return vec_count;
18037c45a4eSAlex Maftei (amaftei) 
18137c45a4eSAlex Maftei (amaftei) 	max_channels = min_t(unsigned int, vec_count, max_channels);
18237c45a4eSAlex Maftei (amaftei) 
18337c45a4eSAlex Maftei (amaftei) 	/* Check resources.
18437c45a4eSAlex Maftei (amaftei) 	 * We need a channel per event queue, plus a VI per tx queue.
18537c45a4eSAlex Maftei (amaftei) 	 * This may be more pessimistic than it needs to be.
18637c45a4eSAlex Maftei (amaftei) 	 */
18741544618SÍñigo Huguet 	if (n_channels >= max_channels) {
1886215b608SÍñigo Huguet 		efx->xdp_txq_queues_mode = EFX_XDP_TX_QUEUES_BORROWED;
1896215b608SÍñigo Huguet 		netif_warn(efx, drv, efx->net_dev,
19037c45a4eSAlex Maftei (amaftei) 			   "Insufficient resources for %d XDP event queues (%d other channels, max %d)\n",
19137c45a4eSAlex Maftei (amaftei) 			   n_xdp_ev, n_channels, max_channels);
1926215b608SÍñigo Huguet 		netif_warn(efx, drv, efx->net_dev,
1936215b608SÍñigo Huguet 			   "XDP_TX and XDP_REDIRECT might decrease device's performance\n");
194de5f32e2SEdward Cree 	} else if (n_channels + n_xdp_tx > efx->max_vis) {
1956215b608SÍñigo Huguet 		efx->xdp_txq_queues_mode = EFX_XDP_TX_QUEUES_BORROWED;
1966215b608SÍñigo Huguet 		netif_warn(efx, drv, efx->net_dev,
197de5f32e2SEdward Cree 			   "Insufficient resources for %d XDP TX queues (%d other channels, max VIs %d)\n",
198de5f32e2SEdward Cree 			   n_xdp_tx, n_channels, efx->max_vis);
1996215b608SÍñigo Huguet 		netif_warn(efx, drv, efx->net_dev,
2006215b608SÍñigo Huguet 			   "XDP_TX and XDP_REDIRECT might decrease device's performance\n");
20141544618SÍñigo Huguet 	} else if (n_channels + n_xdp_ev > max_channels) {
20241544618SÍñigo Huguet 		efx->xdp_txq_queues_mode = EFX_XDP_TX_QUEUES_SHARED;
20341544618SÍñigo Huguet 		netif_warn(efx, drv, efx->net_dev,
20441544618SÍñigo Huguet 			   "Insufficient resources for %d XDP event queues (%d other channels, max %d)\n",
20541544618SÍñigo Huguet 			   n_xdp_ev, n_channels, max_channels);
20641544618SÍñigo Huguet 
20741544618SÍñigo Huguet 		n_xdp_ev = max_channels - n_channels;
20841544618SÍñigo Huguet 		netif_warn(efx, drv, efx->net_dev,
20941544618SÍñigo Huguet 			   "XDP_TX and XDP_REDIRECT will work with reduced performance (%d cpus/tx_queue)\n",
21041544618SÍñigo Huguet 			   DIV_ROUND_UP(n_xdp_tx, tx_per_ev * n_xdp_ev));
21137c45a4eSAlex Maftei (amaftei) 	} else {
21241544618SÍñigo Huguet 		efx->xdp_txq_queues_mode = EFX_XDP_TX_QUEUES_DEDICATED;
21341544618SÍñigo Huguet 	}
21441544618SÍñigo Huguet 
2156215b608SÍñigo Huguet 	if (efx->xdp_txq_queues_mode != EFX_XDP_TX_QUEUES_BORROWED) {
21637c45a4eSAlex Maftei (amaftei) 		efx->n_xdp_channels = n_xdp_ev;
217f28100cbSÍñigo Huguet 		efx->xdp_tx_per_channel = tx_per_ev;
21837c45a4eSAlex Maftei (amaftei) 		efx->xdp_tx_queue_count = n_xdp_tx;
21937c45a4eSAlex Maftei (amaftei) 		n_channels += n_xdp_ev;
22037c45a4eSAlex Maftei (amaftei) 		netif_dbg(efx, drv, efx->net_dev,
22137c45a4eSAlex Maftei (amaftei) 			  "Allocating %d TX and %d event queues for XDP\n",
22241544618SÍñigo Huguet 			  n_xdp_ev * tx_per_ev, n_xdp_ev);
22341544618SÍñigo Huguet 	} else {
22441544618SÍñigo Huguet 		efx->n_xdp_channels = 0;
22541544618SÍñigo Huguet 		efx->xdp_tx_per_channel = 0;
2266215b608SÍñigo Huguet 		efx->xdp_tx_queue_count = n_xdp_tx;
22737c45a4eSAlex Maftei (amaftei) 	}
22837c45a4eSAlex Maftei (amaftei) 
22937c45a4eSAlex Maftei (amaftei) 	if (vec_count < n_channels) {
23037c45a4eSAlex Maftei (amaftei) 		netif_err(efx, drv, efx->net_dev,
23137c45a4eSAlex Maftei (amaftei) 			  "WARNING: Insufficient MSI-X vectors available (%d < %u).\n",
23237c45a4eSAlex Maftei (amaftei) 			  vec_count, n_channels);
23337c45a4eSAlex Maftei (amaftei) 		netif_err(efx, drv, efx->net_dev,
23437c45a4eSAlex Maftei (amaftei) 			  "WARNING: Performance may be reduced.\n");
23537c45a4eSAlex Maftei (amaftei) 		n_channels = vec_count;
23637c45a4eSAlex Maftei (amaftei) 	}
23737c45a4eSAlex Maftei (amaftei) 
23837c45a4eSAlex Maftei (amaftei) 	n_channels = min(n_channels, max_channels);
23937c45a4eSAlex Maftei (amaftei) 
24037c45a4eSAlex Maftei (amaftei) 	efx->n_channels = n_channels;
24137c45a4eSAlex Maftei (amaftei) 
24237c45a4eSAlex Maftei (amaftei) 	/* Ignore XDP tx channels when creating rx channels. */
24337c45a4eSAlex Maftei (amaftei) 	n_channels -= efx->n_xdp_channels;
24437c45a4eSAlex Maftei (amaftei) 
24537c45a4eSAlex Maftei (amaftei) 	if (efx_separate_tx_channels) {
24637c45a4eSAlex Maftei (amaftei) 		efx->n_tx_channels =
24737c45a4eSAlex Maftei (amaftei) 			min(max(n_channels / 2, 1U),
24837c45a4eSAlex Maftei (amaftei) 			    efx->max_tx_channels);
24937c45a4eSAlex Maftei (amaftei) 		efx->tx_channel_offset =
25037c45a4eSAlex Maftei (amaftei) 			n_channels - efx->n_tx_channels;
25137c45a4eSAlex Maftei (amaftei) 		efx->n_rx_channels =
25237c45a4eSAlex Maftei (amaftei) 			max(n_channels -
25337c45a4eSAlex Maftei (amaftei) 			    efx->n_tx_channels, 1U);
25437c45a4eSAlex Maftei (amaftei) 	} else {
25537c45a4eSAlex Maftei (amaftei) 		efx->n_tx_channels = min(n_channels, efx->max_tx_channels);
25637c45a4eSAlex Maftei (amaftei) 		efx->tx_channel_offset = 0;
25737c45a4eSAlex Maftei (amaftei) 		efx->n_rx_channels = n_channels;
25837c45a4eSAlex Maftei (amaftei) 	}
25937c45a4eSAlex Maftei (amaftei) 
26037c45a4eSAlex Maftei (amaftei) 	efx->n_rx_channels = min(efx->n_rx_channels, parallelism);
26137c45a4eSAlex Maftei (amaftei) 	efx->n_tx_channels = min(efx->n_tx_channels, parallelism);
26237c45a4eSAlex Maftei (amaftei) 
26337c45a4eSAlex Maftei (amaftei) 	efx->xdp_channel_offset = n_channels;
26437c45a4eSAlex Maftei (amaftei) 
26537c45a4eSAlex Maftei (amaftei) 	netif_dbg(efx, drv, efx->net_dev,
26637c45a4eSAlex Maftei (amaftei) 		  "Allocating %u RX channels\n",
26737c45a4eSAlex Maftei (amaftei) 		  efx->n_rx_channels);
26837c45a4eSAlex Maftei (amaftei) 
26937c45a4eSAlex Maftei (amaftei) 	return efx->n_channels;
27037c45a4eSAlex Maftei (amaftei) }
27137c45a4eSAlex Maftei (amaftei) 
27237c45a4eSAlex Maftei (amaftei) /* Probe the number and type of interrupts we are able to obtain, and
27337c45a4eSAlex Maftei (amaftei)  * the resulting numbers of channels and RX queues.
27437c45a4eSAlex Maftei (amaftei)  */
27537c45a4eSAlex Maftei (amaftei) int efx_probe_interrupts(struct efx_nic *efx)
27637c45a4eSAlex Maftei (amaftei) {
27737c45a4eSAlex Maftei (amaftei) 	unsigned int extra_channels = 0;
27837c45a4eSAlex Maftei (amaftei) 	unsigned int rss_spread;
27937c45a4eSAlex Maftei (amaftei) 	unsigned int i, j;
28037c45a4eSAlex Maftei (amaftei) 	int rc;
28137c45a4eSAlex Maftei (amaftei) 
28237c45a4eSAlex Maftei (amaftei) 	for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
28337c45a4eSAlex Maftei (amaftei) 		if (efx->extra_channel_type[i])
28437c45a4eSAlex Maftei (amaftei) 			++extra_channels;
28537c45a4eSAlex Maftei (amaftei) 
28637c45a4eSAlex Maftei (amaftei) 	if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
28737c45a4eSAlex Maftei (amaftei) 		unsigned int parallelism = efx_wanted_parallelism(efx);
28837c45a4eSAlex Maftei (amaftei) 		struct msix_entry xentries[EFX_MAX_CHANNELS];
28937c45a4eSAlex Maftei (amaftei) 		unsigned int n_channels;
29037c45a4eSAlex Maftei (amaftei) 
29137c45a4eSAlex Maftei (amaftei) 		rc = efx_allocate_msix_channels(efx, efx->max_channels,
29237c45a4eSAlex Maftei (amaftei) 						extra_channels, parallelism);
29337c45a4eSAlex Maftei (amaftei) 		if (rc >= 0) {
29437c45a4eSAlex Maftei (amaftei) 			n_channels = rc;
29537c45a4eSAlex Maftei (amaftei) 			for (i = 0; i < n_channels; i++)
29637c45a4eSAlex Maftei (amaftei) 				xentries[i].entry = i;
29737c45a4eSAlex Maftei (amaftei) 			rc = pci_enable_msix_range(efx->pci_dev, xentries, 1,
29837c45a4eSAlex Maftei (amaftei) 						   n_channels);
29937c45a4eSAlex Maftei (amaftei) 		}
30037c45a4eSAlex Maftei (amaftei) 		if (rc < 0) {
30137c45a4eSAlex Maftei (amaftei) 			/* Fall back to single channel MSI */
30237c45a4eSAlex Maftei (amaftei) 			netif_err(efx, drv, efx->net_dev,
30337c45a4eSAlex Maftei (amaftei) 				  "could not enable MSI-X\n");
30437c45a4eSAlex Maftei (amaftei) 			if (efx->type->min_interrupt_mode >= EFX_INT_MODE_MSI)
30537c45a4eSAlex Maftei (amaftei) 				efx->interrupt_mode = EFX_INT_MODE_MSI;
30637c45a4eSAlex Maftei (amaftei) 			else
30737c45a4eSAlex Maftei (amaftei) 				return rc;
30837c45a4eSAlex Maftei (amaftei) 		} else if (rc < n_channels) {
30937c45a4eSAlex Maftei (amaftei) 			netif_err(efx, drv, efx->net_dev,
31037c45a4eSAlex Maftei (amaftei) 				  "WARNING: Insufficient MSI-X vectors"
31137c45a4eSAlex Maftei (amaftei) 				  " available (%d < %u).\n", rc, n_channels);
31237c45a4eSAlex Maftei (amaftei) 			netif_err(efx, drv, efx->net_dev,
31337c45a4eSAlex Maftei (amaftei) 				  "WARNING: Performance may be reduced.\n");
31437c45a4eSAlex Maftei (amaftei) 			n_channels = rc;
31537c45a4eSAlex Maftei (amaftei) 		}
31637c45a4eSAlex Maftei (amaftei) 
31737c45a4eSAlex Maftei (amaftei) 		if (rc > 0) {
31837c45a4eSAlex Maftei (amaftei) 			for (i = 0; i < efx->n_channels; i++)
31937c45a4eSAlex Maftei (amaftei) 				efx_get_channel(efx, i)->irq =
32037c45a4eSAlex Maftei (amaftei) 					xentries[i].vector;
32137c45a4eSAlex Maftei (amaftei) 		}
32237c45a4eSAlex Maftei (amaftei) 	}
32337c45a4eSAlex Maftei (amaftei) 
32437c45a4eSAlex Maftei (amaftei) 	/* Try single interrupt MSI */
32537c45a4eSAlex Maftei (amaftei) 	if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
32637c45a4eSAlex Maftei (amaftei) 		efx->n_channels = 1;
32737c45a4eSAlex Maftei (amaftei) 		efx->n_rx_channels = 1;
32837c45a4eSAlex Maftei (amaftei) 		efx->n_tx_channels = 1;
32937c45a4eSAlex Maftei (amaftei) 		efx->n_xdp_channels = 0;
33037c45a4eSAlex Maftei (amaftei) 		efx->xdp_channel_offset = efx->n_channels;
33137c45a4eSAlex Maftei (amaftei) 		rc = pci_enable_msi(efx->pci_dev);
33237c45a4eSAlex Maftei (amaftei) 		if (rc == 0) {
33337c45a4eSAlex Maftei (amaftei) 			efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
33437c45a4eSAlex Maftei (amaftei) 		} else {
33537c45a4eSAlex Maftei (amaftei) 			netif_err(efx, drv, efx->net_dev,
33637c45a4eSAlex Maftei (amaftei) 				  "could not enable MSI\n");
33737c45a4eSAlex Maftei (amaftei) 			if (efx->type->min_interrupt_mode >= EFX_INT_MODE_LEGACY)
33837c45a4eSAlex Maftei (amaftei) 				efx->interrupt_mode = EFX_INT_MODE_LEGACY;
33937c45a4eSAlex Maftei (amaftei) 			else
34037c45a4eSAlex Maftei (amaftei) 				return rc;
34137c45a4eSAlex Maftei (amaftei) 		}
34237c45a4eSAlex Maftei (amaftei) 	}
34337c45a4eSAlex Maftei (amaftei) 
34437c45a4eSAlex Maftei (amaftei) 	/* Assume legacy interrupts */
34537c45a4eSAlex Maftei (amaftei) 	if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
34637c45a4eSAlex Maftei (amaftei) 		efx->n_channels = 1 + (efx_separate_tx_channels ? 1 : 0);
34737c45a4eSAlex Maftei (amaftei) 		efx->n_rx_channels = 1;
34837c45a4eSAlex Maftei (amaftei) 		efx->n_tx_channels = 1;
34937c45a4eSAlex Maftei (amaftei) 		efx->n_xdp_channels = 0;
35037c45a4eSAlex Maftei (amaftei) 		efx->xdp_channel_offset = efx->n_channels;
35137c45a4eSAlex Maftei (amaftei) 		efx->legacy_irq = efx->pci_dev->irq;
35237c45a4eSAlex Maftei (amaftei) 	}
35337c45a4eSAlex Maftei (amaftei) 
35437c45a4eSAlex Maftei (amaftei) 	/* Assign extra channels if possible, before XDP channels */
35537c45a4eSAlex Maftei (amaftei) 	efx->n_extra_tx_channels = 0;
35637c45a4eSAlex Maftei (amaftei) 	j = efx->xdp_channel_offset;
35737c45a4eSAlex Maftei (amaftei) 	for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
35837c45a4eSAlex Maftei (amaftei) 		if (!efx->extra_channel_type[i])
35937c45a4eSAlex Maftei (amaftei) 			continue;
36037c45a4eSAlex Maftei (amaftei) 		if (j <= efx->tx_channel_offset + efx->n_tx_channels) {
36137c45a4eSAlex Maftei (amaftei) 			efx->extra_channel_type[i]->handle_no_channel(efx);
36237c45a4eSAlex Maftei (amaftei) 		} else {
36337c45a4eSAlex Maftei (amaftei) 			--j;
36437c45a4eSAlex Maftei (amaftei) 			efx_get_channel(efx, j)->type =
36537c45a4eSAlex Maftei (amaftei) 				efx->extra_channel_type[i];
36637c45a4eSAlex Maftei (amaftei) 			if (efx_channel_has_tx_queues(efx_get_channel(efx, j)))
36737c45a4eSAlex Maftei (amaftei) 				efx->n_extra_tx_channels++;
36837c45a4eSAlex Maftei (amaftei) 		}
36937c45a4eSAlex Maftei (amaftei) 	}
37037c45a4eSAlex Maftei (amaftei) 
37137c45a4eSAlex Maftei (amaftei) 	rss_spread = efx->n_rx_channels;
37237c45a4eSAlex Maftei (amaftei) 	/* RSS might be usable on VFs even if it is disabled on the PF */
37337c45a4eSAlex Maftei (amaftei) #ifdef CONFIG_SFC_SRIOV
37437c45a4eSAlex Maftei (amaftei) 	if (efx->type->sriov_wanted) {
37537c45a4eSAlex Maftei (amaftei) 		efx->rss_spread = ((rss_spread > 1 ||
37637c45a4eSAlex Maftei (amaftei) 				    !efx->type->sriov_wanted(efx)) ?
37737c45a4eSAlex Maftei (amaftei) 				   rss_spread : efx_vf_size(efx));
37837c45a4eSAlex Maftei (amaftei) 		return 0;
37937c45a4eSAlex Maftei (amaftei) 	}
38037c45a4eSAlex Maftei (amaftei) #endif
38137c45a4eSAlex Maftei (amaftei) 	efx->rss_spread = rss_spread;
38237c45a4eSAlex Maftei (amaftei) 
38337c45a4eSAlex Maftei (amaftei) 	return 0;
38437c45a4eSAlex Maftei (amaftei) }
38537c45a4eSAlex Maftei (amaftei) 
38637c45a4eSAlex Maftei (amaftei) #if defined(CONFIG_SMP)
38737c45a4eSAlex Maftei (amaftei) void efx_set_interrupt_affinity(struct efx_nic *efx)
38837c45a4eSAlex Maftei (amaftei) {
389*09a99ab1SÍñigo Huguet 	int numa_node = pcibus_to_node(efx->pci_dev->bus);
390*09a99ab1SÍñigo Huguet 	const struct cpumask *numa_mask = cpumask_of_node(numa_node);
39137c45a4eSAlex Maftei (amaftei) 	struct efx_channel *channel;
39237c45a4eSAlex Maftei (amaftei) 	unsigned int cpu;
39337c45a4eSAlex Maftei (amaftei) 
394*09a99ab1SÍñigo Huguet 	/* If no online CPUs in local node, fallback to any online CPU */
395*09a99ab1SÍñigo Huguet 	if (cpumask_first_and(cpu_online_mask, numa_mask) >= nr_cpu_ids)
396*09a99ab1SÍñigo Huguet 		numa_mask = cpu_online_mask;
397*09a99ab1SÍñigo Huguet 
398*09a99ab1SÍñigo Huguet 	cpu = -1;
39937c45a4eSAlex Maftei (amaftei) 	efx_for_each_channel(channel, efx) {
400*09a99ab1SÍñigo Huguet 		cpu = cpumask_next_and(cpu, cpu_online_mask, numa_mask);
401*09a99ab1SÍñigo Huguet 		if (cpu >= nr_cpu_ids)
402*09a99ab1SÍñigo Huguet 			cpu = cpumask_first_and(cpu_online_mask, numa_mask);
40337c45a4eSAlex Maftei (amaftei) 		irq_set_affinity_hint(channel->irq, cpumask_of(cpu));
40437c45a4eSAlex Maftei (amaftei) 	}
40537c45a4eSAlex Maftei (amaftei) }
40637c45a4eSAlex Maftei (amaftei) 
40737c45a4eSAlex Maftei (amaftei) void efx_clear_interrupt_affinity(struct efx_nic *efx)
40837c45a4eSAlex Maftei (amaftei) {
40937c45a4eSAlex Maftei (amaftei) 	struct efx_channel *channel;
41037c45a4eSAlex Maftei (amaftei) 
41137c45a4eSAlex Maftei (amaftei) 	efx_for_each_channel(channel, efx)
41237c45a4eSAlex Maftei (amaftei) 		irq_set_affinity_hint(channel->irq, NULL);
41337c45a4eSAlex Maftei (amaftei) }
41437c45a4eSAlex Maftei (amaftei) #else
41537c45a4eSAlex Maftei (amaftei) void
41637c45a4eSAlex Maftei (amaftei) efx_set_interrupt_affinity(struct efx_nic *efx __attribute__ ((unused)))
41737c45a4eSAlex Maftei (amaftei) {
41837c45a4eSAlex Maftei (amaftei) }
41937c45a4eSAlex Maftei (amaftei) 
42037c45a4eSAlex Maftei (amaftei) void
42137c45a4eSAlex Maftei (amaftei) efx_clear_interrupt_affinity(struct efx_nic *efx __attribute__ ((unused)))
42237c45a4eSAlex Maftei (amaftei) {
42337c45a4eSAlex Maftei (amaftei) }
42437c45a4eSAlex Maftei (amaftei) #endif /* CONFIG_SMP */
42537c45a4eSAlex Maftei (amaftei) 
42637c45a4eSAlex Maftei (amaftei) void efx_remove_interrupts(struct efx_nic *efx)
42737c45a4eSAlex Maftei (amaftei) {
42837c45a4eSAlex Maftei (amaftei) 	struct efx_channel *channel;
42937c45a4eSAlex Maftei (amaftei) 
43037c45a4eSAlex Maftei (amaftei) 	/* Remove MSI/MSI-X interrupts */
43137c45a4eSAlex Maftei (amaftei) 	efx_for_each_channel(channel, efx)
43237c45a4eSAlex Maftei (amaftei) 		channel->irq = 0;
43337c45a4eSAlex Maftei (amaftei) 	pci_disable_msi(efx->pci_dev);
43437c45a4eSAlex Maftei (amaftei) 	pci_disable_msix(efx->pci_dev);
43537c45a4eSAlex Maftei (amaftei) 
43637c45a4eSAlex Maftei (amaftei) 	/* Remove legacy interrupt */
43737c45a4eSAlex Maftei (amaftei) 	efx->legacy_irq = 0;
43837c45a4eSAlex Maftei (amaftei) }
43937c45a4eSAlex Maftei (amaftei) 
4405f999256SAlex Maftei (amaftei) /***************
4415f999256SAlex Maftei (amaftei)  * EVENT QUEUES
4425f999256SAlex Maftei (amaftei)  ***************/
4435f999256SAlex Maftei (amaftei) 
4445f999256SAlex Maftei (amaftei) /* Create event queue
4455f999256SAlex Maftei (amaftei)  * Event queue memory allocations are done only once.  If the channel
4465f999256SAlex Maftei (amaftei)  * is reset, the memory buffer will be reused; this guards against
4475f999256SAlex Maftei (amaftei)  * errors during channel reset and also simplifies interrupt handling.
4485f999256SAlex Maftei (amaftei)  */
4495f999256SAlex Maftei (amaftei) int efx_probe_eventq(struct efx_channel *channel)
4505f999256SAlex Maftei (amaftei) {
4515f999256SAlex Maftei (amaftei) 	struct efx_nic *efx = channel->efx;
4525f999256SAlex Maftei (amaftei) 	unsigned long entries;
4535f999256SAlex Maftei (amaftei) 
4545f999256SAlex Maftei (amaftei) 	netif_dbg(efx, probe, efx->net_dev,
4555f999256SAlex Maftei (amaftei) 		  "chan %d create event queue\n", channel->channel);
4565f999256SAlex Maftei (amaftei) 
4575f999256SAlex Maftei (amaftei) 	/* Build an event queue with room for one event per tx and rx buffer,
4585f999256SAlex Maftei (amaftei) 	 * plus some extra for link state events and MCDI completions.
4595f999256SAlex Maftei (amaftei) 	 */
4605f999256SAlex Maftei (amaftei) 	entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
4615f999256SAlex Maftei (amaftei) 	EFX_WARN_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
4625f999256SAlex Maftei (amaftei) 	channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
4635f999256SAlex Maftei (amaftei) 
4645f999256SAlex Maftei (amaftei) 	return efx_nic_probe_eventq(channel);
4655f999256SAlex Maftei (amaftei) }
4665f999256SAlex Maftei (amaftei) 
4675f999256SAlex Maftei (amaftei) /* Prepare channel's event queue */
4685f999256SAlex Maftei (amaftei) int efx_init_eventq(struct efx_channel *channel)
4695f999256SAlex Maftei (amaftei) {
4705f999256SAlex Maftei (amaftei) 	struct efx_nic *efx = channel->efx;
4715f999256SAlex Maftei (amaftei) 	int rc;
4725f999256SAlex Maftei (amaftei) 
4735f999256SAlex Maftei (amaftei) 	EFX_WARN_ON_PARANOID(channel->eventq_init);
4745f999256SAlex Maftei (amaftei) 
4755f999256SAlex Maftei (amaftei) 	netif_dbg(efx, drv, efx->net_dev,
4765f999256SAlex Maftei (amaftei) 		  "chan %d init event queue\n", channel->channel);
4775f999256SAlex Maftei (amaftei) 
4785f999256SAlex Maftei (amaftei) 	rc = efx_nic_init_eventq(channel);
4795f999256SAlex Maftei (amaftei) 	if (rc == 0) {
4805f999256SAlex Maftei (amaftei) 		efx->type->push_irq_moderation(channel);
4815f999256SAlex Maftei (amaftei) 		channel->eventq_read_ptr = 0;
4825f999256SAlex Maftei (amaftei) 		channel->eventq_init = true;
4835f999256SAlex Maftei (amaftei) 	}
4845f999256SAlex Maftei (amaftei) 	return rc;
4855f999256SAlex Maftei (amaftei) }
4865f999256SAlex Maftei (amaftei) 
4875f999256SAlex Maftei (amaftei) /* Enable event queue processing and NAPI */
4885f999256SAlex Maftei (amaftei) void efx_start_eventq(struct efx_channel *channel)
4895f999256SAlex Maftei (amaftei) {
4905f999256SAlex Maftei (amaftei) 	netif_dbg(channel->efx, ifup, channel->efx->net_dev,
4915f999256SAlex Maftei (amaftei) 		  "chan %d start event queue\n", channel->channel);
4925f999256SAlex Maftei (amaftei) 
4935f999256SAlex Maftei (amaftei) 	/* Make sure the NAPI handler sees the enabled flag set */
4945f999256SAlex Maftei (amaftei) 	channel->enabled = true;
4955f999256SAlex Maftei (amaftei) 	smp_wmb();
4965f999256SAlex Maftei (amaftei) 
4975f999256SAlex Maftei (amaftei) 	napi_enable(&channel->napi_str);
4985f999256SAlex Maftei (amaftei) 	efx_nic_eventq_read_ack(channel);
4995f999256SAlex Maftei (amaftei) }
5005f999256SAlex Maftei (amaftei) 
5015f999256SAlex Maftei (amaftei) /* Disable event queue processing and NAPI */
5025f999256SAlex Maftei (amaftei) void efx_stop_eventq(struct efx_channel *channel)
5035f999256SAlex Maftei (amaftei) {
5045f999256SAlex Maftei (amaftei) 	if (!channel->enabled)
5055f999256SAlex Maftei (amaftei) 		return;
5065f999256SAlex Maftei (amaftei) 
5075f999256SAlex Maftei (amaftei) 	napi_disable(&channel->napi_str);
5085f999256SAlex Maftei (amaftei) 	channel->enabled = false;
5095f999256SAlex Maftei (amaftei) }
5105f999256SAlex Maftei (amaftei) 
5115f999256SAlex Maftei (amaftei) void efx_fini_eventq(struct efx_channel *channel)
5125f999256SAlex Maftei (amaftei) {
5135f999256SAlex Maftei (amaftei) 	if (!channel->eventq_init)
5145f999256SAlex Maftei (amaftei) 		return;
5155f999256SAlex Maftei (amaftei) 
5165f999256SAlex Maftei (amaftei) 	netif_dbg(channel->efx, drv, channel->efx->net_dev,
5175f999256SAlex Maftei (amaftei) 		  "chan %d fini event queue\n", channel->channel);
5185f999256SAlex Maftei (amaftei) 
5195f999256SAlex Maftei (amaftei) 	efx_nic_fini_eventq(channel);
5205f999256SAlex Maftei (amaftei) 	channel->eventq_init = false;
5215f999256SAlex Maftei (amaftei) }
5225f999256SAlex Maftei (amaftei) 
5235f999256SAlex Maftei (amaftei) void efx_remove_eventq(struct efx_channel *channel)
5245f999256SAlex Maftei (amaftei) {
5255f999256SAlex Maftei (amaftei) 	netif_dbg(channel->efx, drv, channel->efx->net_dev,
5265f999256SAlex Maftei (amaftei) 		  "chan %d remove event queue\n", channel->channel);
5275f999256SAlex Maftei (amaftei) 
5285f999256SAlex Maftei (amaftei) 	efx_nic_remove_eventq(channel);
5295f999256SAlex Maftei (amaftei) }
5305f999256SAlex Maftei (amaftei) 
53183975485SAlex Maftei (amaftei) /**************************************************************************
53283975485SAlex Maftei (amaftei)  *
53383975485SAlex Maftei (amaftei)  * Channel handling
53483975485SAlex Maftei (amaftei)  *
53583975485SAlex Maftei (amaftei)  *************************************************************************/
53683975485SAlex Maftei (amaftei) 
537025c5a0bSEdward Cree #ifdef CONFIG_RFS_ACCEL
538025c5a0bSEdward Cree static void efx_filter_rfs_expire(struct work_struct *data)
539025c5a0bSEdward Cree {
540025c5a0bSEdward Cree 	struct delayed_work *dwork = to_delayed_work(data);
541025c5a0bSEdward Cree 	struct efx_channel *channel;
542025c5a0bSEdward Cree 	unsigned int time, quota;
543025c5a0bSEdward Cree 
544025c5a0bSEdward Cree 	channel = container_of(dwork, struct efx_channel, filter_work);
545025c5a0bSEdward Cree 	time = jiffies - channel->rfs_last_expiry;
546025c5a0bSEdward Cree 	quota = channel->rfs_filter_count * time / (30 * HZ);
547025c5a0bSEdward Cree 	if (quota >= 20 && __efx_filter_rfs_expire(channel, min(channel->rfs_filter_count, quota)))
548025c5a0bSEdward Cree 		channel->rfs_last_expiry += time;
549025c5a0bSEdward Cree 	/* Ensure we do more work eventually even if NAPI poll is not happening */
550025c5a0bSEdward Cree 	schedule_delayed_work(dwork, 30 * HZ);
551025c5a0bSEdward Cree }
552025c5a0bSEdward Cree #endif
553025c5a0bSEdward Cree 
55483975485SAlex Maftei (amaftei) /* Allocate and initialise a channel structure. */
5554da24fa6SEdward Cree static struct efx_channel *efx_alloc_channel(struct efx_nic *efx, int i)
55683975485SAlex Maftei (amaftei) {
55783975485SAlex Maftei (amaftei) 	struct efx_rx_queue *rx_queue;
55883975485SAlex Maftei (amaftei) 	struct efx_tx_queue *tx_queue;
55983975485SAlex Maftei (amaftei) 	struct efx_channel *channel;
56083975485SAlex Maftei (amaftei) 	int j;
56183975485SAlex Maftei (amaftei) 
56283975485SAlex Maftei (amaftei) 	channel = kzalloc(sizeof(*channel), GFP_KERNEL);
56383975485SAlex Maftei (amaftei) 	if (!channel)
56483975485SAlex Maftei (amaftei) 		return NULL;
56583975485SAlex Maftei (amaftei) 
56683975485SAlex Maftei (amaftei) 	channel->efx = efx;
56783975485SAlex Maftei (amaftei) 	channel->channel = i;
56883975485SAlex Maftei (amaftei) 	channel->type = &efx_default_channel_type;
56983975485SAlex Maftei (amaftei) 
57012804793SEdward Cree 	for (j = 0; j < EFX_MAX_TXQ_PER_CHANNEL; j++) {
57183975485SAlex Maftei (amaftei) 		tx_queue = &channel->tx_queue[j];
57283975485SAlex Maftei (amaftei) 		tx_queue->efx = efx;
573a81dcd85SEdward Cree 		tx_queue->queue = -1;
574a81dcd85SEdward Cree 		tx_queue->label = j;
57583975485SAlex Maftei (amaftei) 		tx_queue->channel = channel;
57683975485SAlex Maftei (amaftei) 	}
57783975485SAlex Maftei (amaftei) 
57883975485SAlex Maftei (amaftei) #ifdef CONFIG_RFS_ACCEL
57983975485SAlex Maftei (amaftei) 	INIT_DELAYED_WORK(&channel->filter_work, efx_filter_rfs_expire);
58083975485SAlex Maftei (amaftei) #endif
58183975485SAlex Maftei (amaftei) 
58283975485SAlex Maftei (amaftei) 	rx_queue = &channel->rx_queue;
58383975485SAlex Maftei (amaftei) 	rx_queue->efx = efx;
58483975485SAlex Maftei (amaftei) 	timer_setup(&rx_queue->slow_fill, efx_rx_slow_fill, 0);
58583975485SAlex Maftei (amaftei) 
58683975485SAlex Maftei (amaftei) 	return channel;
58783975485SAlex Maftei (amaftei) }
58883975485SAlex Maftei (amaftei) 
58983975485SAlex Maftei (amaftei) int efx_init_channels(struct efx_nic *efx)
59083975485SAlex Maftei (amaftei) {
59183975485SAlex Maftei (amaftei) 	unsigned int i;
59283975485SAlex Maftei (amaftei) 
59383975485SAlex Maftei (amaftei) 	for (i = 0; i < EFX_MAX_CHANNELS; i++) {
5944da24fa6SEdward Cree 		efx->channel[i] = efx_alloc_channel(efx, i);
59583975485SAlex Maftei (amaftei) 		if (!efx->channel[i])
59683975485SAlex Maftei (amaftei) 			return -ENOMEM;
59783975485SAlex Maftei (amaftei) 		efx->msi_context[i].efx = efx;
59883975485SAlex Maftei (amaftei) 		efx->msi_context[i].index = i;
59983975485SAlex Maftei (amaftei) 	}
60083975485SAlex Maftei (amaftei) 
60183975485SAlex Maftei (amaftei) 	/* Higher numbered interrupt modes are less capable! */
60283975485SAlex Maftei (amaftei) 	efx->interrupt_mode = min(efx->type->min_interrupt_mode,
603e4ff3232SEdward Cree 				  efx_interrupt_mode);
60483975485SAlex Maftei (amaftei) 
605937aa3aeSEdward Cree 	efx->max_channels = EFX_MAX_CHANNELS;
606937aa3aeSEdward Cree 	efx->max_tx_channels = EFX_MAX_CHANNELS;
607937aa3aeSEdward Cree 
60883975485SAlex Maftei (amaftei) 	return 0;
60983975485SAlex Maftei (amaftei) }
61083975485SAlex Maftei (amaftei) 
61183975485SAlex Maftei (amaftei) void efx_fini_channels(struct efx_nic *efx)
61283975485SAlex Maftei (amaftei) {
61383975485SAlex Maftei (amaftei) 	unsigned int i;
61483975485SAlex Maftei (amaftei) 
61583975485SAlex Maftei (amaftei) 	for (i = 0; i < EFX_MAX_CHANNELS; i++)
61683975485SAlex Maftei (amaftei) 		if (efx->channel[i]) {
61783975485SAlex Maftei (amaftei) 			kfree(efx->channel[i]);
61883975485SAlex Maftei (amaftei) 			efx->channel[i] = NULL;
61983975485SAlex Maftei (amaftei) 		}
62083975485SAlex Maftei (amaftei) }
62183975485SAlex Maftei (amaftei) 
62283975485SAlex Maftei (amaftei) /* Allocate and initialise a channel structure, copying parameters
62383975485SAlex Maftei (amaftei)  * (but not resources) from an old channel structure.
62483975485SAlex Maftei (amaftei)  */
62583975485SAlex Maftei (amaftei) struct efx_channel *efx_copy_channel(const struct efx_channel *old_channel)
62683975485SAlex Maftei (amaftei) {
62783975485SAlex Maftei (amaftei) 	struct efx_rx_queue *rx_queue;
62883975485SAlex Maftei (amaftei) 	struct efx_tx_queue *tx_queue;
62983975485SAlex Maftei (amaftei) 	struct efx_channel *channel;
63083975485SAlex Maftei (amaftei) 	int j;
63183975485SAlex Maftei (amaftei) 
63283975485SAlex Maftei (amaftei) 	channel = kmalloc(sizeof(*channel), GFP_KERNEL);
63383975485SAlex Maftei (amaftei) 	if (!channel)
63483975485SAlex Maftei (amaftei) 		return NULL;
63583975485SAlex Maftei (amaftei) 
63683975485SAlex Maftei (amaftei) 	*channel = *old_channel;
63783975485SAlex Maftei (amaftei) 
63883975485SAlex Maftei (amaftei) 	channel->napi_dev = NULL;
63983975485SAlex Maftei (amaftei) 	INIT_HLIST_NODE(&channel->napi_str.napi_hash_node);
64083975485SAlex Maftei (amaftei) 	channel->napi_str.napi_id = 0;
64183975485SAlex Maftei (amaftei) 	channel->napi_str.state = 0;
64283975485SAlex Maftei (amaftei) 	memset(&channel->eventq, 0, sizeof(channel->eventq));
64383975485SAlex Maftei (amaftei) 
64412804793SEdward Cree 	for (j = 0; j < EFX_MAX_TXQ_PER_CHANNEL; j++) {
64583975485SAlex Maftei (amaftei) 		tx_queue = &channel->tx_queue[j];
64683975485SAlex Maftei (amaftei) 		if (tx_queue->channel)
64783975485SAlex Maftei (amaftei) 			tx_queue->channel = channel;
64883975485SAlex Maftei (amaftei) 		tx_queue->buffer = NULL;
6494b1bd9dbSEdward Cree 		tx_queue->cb_page = NULL;
65083975485SAlex Maftei (amaftei) 		memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
65183975485SAlex Maftei (amaftei) 	}
65283975485SAlex Maftei (amaftei) 
65383975485SAlex Maftei (amaftei) 	rx_queue = &channel->rx_queue;
65483975485SAlex Maftei (amaftei) 	rx_queue->buffer = NULL;
65583975485SAlex Maftei (amaftei) 	memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
65683975485SAlex Maftei (amaftei) 	timer_setup(&rx_queue->slow_fill, efx_rx_slow_fill, 0);
65783975485SAlex Maftei (amaftei) #ifdef CONFIG_RFS_ACCEL
65883975485SAlex Maftei (amaftei) 	INIT_DELAYED_WORK(&channel->filter_work, efx_filter_rfs_expire);
65983975485SAlex Maftei (amaftei) #endif
66083975485SAlex Maftei (amaftei) 
66183975485SAlex Maftei (amaftei) 	return channel;
66283975485SAlex Maftei (amaftei) }
66383975485SAlex Maftei (amaftei) 
66483975485SAlex Maftei (amaftei) static int efx_probe_channel(struct efx_channel *channel)
66583975485SAlex Maftei (amaftei) {
66683975485SAlex Maftei (amaftei) 	struct efx_tx_queue *tx_queue;
66783975485SAlex Maftei (amaftei) 	struct efx_rx_queue *rx_queue;
66883975485SAlex Maftei (amaftei) 	int rc;
66983975485SAlex Maftei (amaftei) 
67083975485SAlex Maftei (amaftei) 	netif_dbg(channel->efx, probe, channel->efx->net_dev,
67183975485SAlex Maftei (amaftei) 		  "creating channel %d\n", channel->channel);
67283975485SAlex Maftei (amaftei) 
67383975485SAlex Maftei (amaftei) 	rc = channel->type->pre_probe(channel);
67483975485SAlex Maftei (amaftei) 	if (rc)
67583975485SAlex Maftei (amaftei) 		goto fail;
67683975485SAlex Maftei (amaftei) 
67783975485SAlex Maftei (amaftei) 	rc = efx_probe_eventq(channel);
67883975485SAlex Maftei (amaftei) 	if (rc)
67983975485SAlex Maftei (amaftei) 		goto fail;
68083975485SAlex Maftei (amaftei) 
68183975485SAlex Maftei (amaftei) 	efx_for_each_channel_tx_queue(tx_queue, channel) {
68283975485SAlex Maftei (amaftei) 		rc = efx_probe_tx_queue(tx_queue);
68383975485SAlex Maftei (amaftei) 		if (rc)
68483975485SAlex Maftei (amaftei) 			goto fail;
68583975485SAlex Maftei (amaftei) 	}
68683975485SAlex Maftei (amaftei) 
68783975485SAlex Maftei (amaftei) 	efx_for_each_channel_rx_queue(rx_queue, channel) {
68883975485SAlex Maftei (amaftei) 		rc = efx_probe_rx_queue(rx_queue);
68983975485SAlex Maftei (amaftei) 		if (rc)
69083975485SAlex Maftei (amaftei) 			goto fail;
69183975485SAlex Maftei (amaftei) 	}
69283975485SAlex Maftei (amaftei) 
69383975485SAlex Maftei (amaftei) 	channel->rx_list = NULL;
69483975485SAlex Maftei (amaftei) 
69583975485SAlex Maftei (amaftei) 	return 0;
69683975485SAlex Maftei (amaftei) 
69783975485SAlex Maftei (amaftei) fail:
69883975485SAlex Maftei (amaftei) 	efx_remove_channel(channel);
69983975485SAlex Maftei (amaftei) 	return rc;
70083975485SAlex Maftei (amaftei) }
70183975485SAlex Maftei (amaftei) 
70283975485SAlex Maftei (amaftei) void efx_get_channel_name(struct efx_channel *channel, char *buf, size_t len)
70383975485SAlex Maftei (amaftei) {
70483975485SAlex Maftei (amaftei) 	struct efx_nic *efx = channel->efx;
70583975485SAlex Maftei (amaftei) 	const char *type;
70683975485SAlex Maftei (amaftei) 	int number;
70783975485SAlex Maftei (amaftei) 
70883975485SAlex Maftei (amaftei) 	number = channel->channel;
70983975485SAlex Maftei (amaftei) 
71083975485SAlex Maftei (amaftei) 	if (number >= efx->xdp_channel_offset &&
71183975485SAlex Maftei (amaftei) 	    !WARN_ON_ONCE(!efx->n_xdp_channels)) {
71283975485SAlex Maftei (amaftei) 		type = "-xdp";
71383975485SAlex Maftei (amaftei) 		number -= efx->xdp_channel_offset;
71483975485SAlex Maftei (amaftei) 	} else if (efx->tx_channel_offset == 0) {
71583975485SAlex Maftei (amaftei) 		type = "";
71683975485SAlex Maftei (amaftei) 	} else if (number < efx->tx_channel_offset) {
71783975485SAlex Maftei (amaftei) 		type = "-rx";
71883975485SAlex Maftei (amaftei) 	} else {
71983975485SAlex Maftei (amaftei) 		type = "-tx";
72083975485SAlex Maftei (amaftei) 		number -= efx->tx_channel_offset;
72183975485SAlex Maftei (amaftei) 	}
72283975485SAlex Maftei (amaftei) 	snprintf(buf, len, "%s%s-%d", efx->name, type, number);
72383975485SAlex Maftei (amaftei) }
72483975485SAlex Maftei (amaftei) 
72583975485SAlex Maftei (amaftei) void efx_set_channel_names(struct efx_nic *efx)
72683975485SAlex Maftei (amaftei) {
72783975485SAlex Maftei (amaftei) 	struct efx_channel *channel;
72883975485SAlex Maftei (amaftei) 
72983975485SAlex Maftei (amaftei) 	efx_for_each_channel(channel, efx)
73083975485SAlex Maftei (amaftei) 		channel->type->get_name(channel,
73183975485SAlex Maftei (amaftei) 					efx->msi_context[channel->channel].name,
73283975485SAlex Maftei (amaftei) 					sizeof(efx->msi_context[0].name));
73383975485SAlex Maftei (amaftei) }
73483975485SAlex Maftei (amaftei) 
73583975485SAlex Maftei (amaftei) int efx_probe_channels(struct efx_nic *efx)
73683975485SAlex Maftei (amaftei) {
73783975485SAlex Maftei (amaftei) 	struct efx_channel *channel;
73883975485SAlex Maftei (amaftei) 	int rc;
73983975485SAlex Maftei (amaftei) 
74083975485SAlex Maftei (amaftei) 	/* Restart special buffer allocation */
74183975485SAlex Maftei (amaftei) 	efx->next_buffer_table = 0;
74283975485SAlex Maftei (amaftei) 
74383975485SAlex Maftei (amaftei) 	/* Probe channels in reverse, so that any 'extra' channels
74483975485SAlex Maftei (amaftei) 	 * use the start of the buffer table. This allows the traffic
74583975485SAlex Maftei (amaftei) 	 * channels to be resized without moving them or wasting the
74683975485SAlex Maftei (amaftei) 	 * entries before them.
74783975485SAlex Maftei (amaftei) 	 */
74883975485SAlex Maftei (amaftei) 	efx_for_each_channel_rev(channel, efx) {
74983975485SAlex Maftei (amaftei) 		rc = efx_probe_channel(channel);
75083975485SAlex Maftei (amaftei) 		if (rc) {
75183975485SAlex Maftei (amaftei) 			netif_err(efx, probe, efx->net_dev,
75283975485SAlex Maftei (amaftei) 				  "failed to create channel %d\n",
75383975485SAlex Maftei (amaftei) 				  channel->channel);
75483975485SAlex Maftei (amaftei) 			goto fail;
75583975485SAlex Maftei (amaftei) 		}
75683975485SAlex Maftei (amaftei) 	}
75783975485SAlex Maftei (amaftei) 	efx_set_channel_names(efx);
75883975485SAlex Maftei (amaftei) 
75983975485SAlex Maftei (amaftei) 	return 0;
76083975485SAlex Maftei (amaftei) 
76183975485SAlex Maftei (amaftei) fail:
76283975485SAlex Maftei (amaftei) 	efx_remove_channels(efx);
76383975485SAlex Maftei (amaftei) 	return rc;
76483975485SAlex Maftei (amaftei) }
76583975485SAlex Maftei (amaftei) 
76683975485SAlex Maftei (amaftei) void efx_remove_channel(struct efx_channel *channel)
76783975485SAlex Maftei (amaftei) {
76883975485SAlex Maftei (amaftei) 	struct efx_tx_queue *tx_queue;
76983975485SAlex Maftei (amaftei) 	struct efx_rx_queue *rx_queue;
77083975485SAlex Maftei (amaftei) 
77183975485SAlex Maftei (amaftei) 	netif_dbg(channel->efx, drv, channel->efx->net_dev,
77283975485SAlex Maftei (amaftei) 		  "destroy chan %d\n", channel->channel);
77383975485SAlex Maftei (amaftei) 
77483975485SAlex Maftei (amaftei) 	efx_for_each_channel_rx_queue(rx_queue, channel)
77583975485SAlex Maftei (amaftei) 		efx_remove_rx_queue(rx_queue);
776f9cac93eSEdward Cree 	efx_for_each_channel_tx_queue(tx_queue, channel)
77783975485SAlex Maftei (amaftei) 		efx_remove_tx_queue(tx_queue);
77883975485SAlex Maftei (amaftei) 	efx_remove_eventq(channel);
77983975485SAlex Maftei (amaftei) 	channel->type->post_remove(channel);
78083975485SAlex Maftei (amaftei) }
78183975485SAlex Maftei (amaftei) 
78283975485SAlex Maftei (amaftei) void efx_remove_channels(struct efx_nic *efx)
78383975485SAlex Maftei (amaftei) {
78483975485SAlex Maftei (amaftei) 	struct efx_channel *channel;
78583975485SAlex Maftei (amaftei) 
78683975485SAlex Maftei (amaftei) 	efx_for_each_channel(channel, efx)
78783975485SAlex Maftei (amaftei) 		efx_remove_channel(channel);
78883975485SAlex Maftei (amaftei) 
78983975485SAlex Maftei (amaftei) 	kfree(efx->xdp_tx_queues);
79083975485SAlex Maftei (amaftei) }
79183975485SAlex Maftei (amaftei) 
79283975485SAlex Maftei (amaftei) int efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
79383975485SAlex Maftei (amaftei) {
79483975485SAlex Maftei (amaftei) 	struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
79583975485SAlex Maftei (amaftei) 	unsigned int i, next_buffer_table = 0;
79683975485SAlex Maftei (amaftei) 	u32 old_rxq_entries, old_txq_entries;
79783975485SAlex Maftei (amaftei) 	int rc, rc2;
79883975485SAlex Maftei (amaftei) 
79983975485SAlex Maftei (amaftei) 	rc = efx_check_disabled(efx);
80083975485SAlex Maftei (amaftei) 	if (rc)
80183975485SAlex Maftei (amaftei) 		return rc;
80283975485SAlex Maftei (amaftei) 
80383975485SAlex Maftei (amaftei) 	/* Not all channels should be reallocated. We must avoid
80483975485SAlex Maftei (amaftei) 	 * reallocating their buffer table entries.
80583975485SAlex Maftei (amaftei) 	 */
80683975485SAlex Maftei (amaftei) 	efx_for_each_channel(channel, efx) {
80783975485SAlex Maftei (amaftei) 		struct efx_rx_queue *rx_queue;
80883975485SAlex Maftei (amaftei) 		struct efx_tx_queue *tx_queue;
80983975485SAlex Maftei (amaftei) 
81083975485SAlex Maftei (amaftei) 		if (channel->type->copy)
81183975485SAlex Maftei (amaftei) 			continue;
81283975485SAlex Maftei (amaftei) 		next_buffer_table = max(next_buffer_table,
81383975485SAlex Maftei (amaftei) 					channel->eventq.index +
81483975485SAlex Maftei (amaftei) 					channel->eventq.entries);
81583975485SAlex Maftei (amaftei) 		efx_for_each_channel_rx_queue(rx_queue, channel)
81683975485SAlex Maftei (amaftei) 			next_buffer_table = max(next_buffer_table,
81783975485SAlex Maftei (amaftei) 						rx_queue->rxd.index +
81883975485SAlex Maftei (amaftei) 						rx_queue->rxd.entries);
81983975485SAlex Maftei (amaftei) 		efx_for_each_channel_tx_queue(tx_queue, channel)
82083975485SAlex Maftei (amaftei) 			next_buffer_table = max(next_buffer_table,
82183975485SAlex Maftei (amaftei) 						tx_queue->txd.index +
82283975485SAlex Maftei (amaftei) 						tx_queue->txd.entries);
82383975485SAlex Maftei (amaftei) 	}
82483975485SAlex Maftei (amaftei) 
82583975485SAlex Maftei (amaftei) 	efx_device_detach_sync(efx);
82683975485SAlex Maftei (amaftei) 	efx_stop_all(efx);
82783975485SAlex Maftei (amaftei) 	efx_soft_disable_interrupts(efx);
82883975485SAlex Maftei (amaftei) 
82983975485SAlex Maftei (amaftei) 	/* Clone channels (where possible) */
83083975485SAlex Maftei (amaftei) 	memset(other_channel, 0, sizeof(other_channel));
83183975485SAlex Maftei (amaftei) 	for (i = 0; i < efx->n_channels; i++) {
83283975485SAlex Maftei (amaftei) 		channel = efx->channel[i];
83383975485SAlex Maftei (amaftei) 		if (channel->type->copy)
83483975485SAlex Maftei (amaftei) 			channel = channel->type->copy(channel);
83583975485SAlex Maftei (amaftei) 		if (!channel) {
83683975485SAlex Maftei (amaftei) 			rc = -ENOMEM;
83783975485SAlex Maftei (amaftei) 			goto out;
83883975485SAlex Maftei (amaftei) 		}
83983975485SAlex Maftei (amaftei) 		other_channel[i] = channel;
84083975485SAlex Maftei (amaftei) 	}
84183975485SAlex Maftei (amaftei) 
84283975485SAlex Maftei (amaftei) 	/* Swap entry counts and channel pointers */
84383975485SAlex Maftei (amaftei) 	old_rxq_entries = efx->rxq_entries;
84483975485SAlex Maftei (amaftei) 	old_txq_entries = efx->txq_entries;
84583975485SAlex Maftei (amaftei) 	efx->rxq_entries = rxq_entries;
84683975485SAlex Maftei (amaftei) 	efx->txq_entries = txq_entries;
8470cf765fbSJiapeng Chong 	for (i = 0; i < efx->n_channels; i++)
8480cf765fbSJiapeng Chong 		swap(efx->channel[i], other_channel[i]);
84983975485SAlex Maftei (amaftei) 
85083975485SAlex Maftei (amaftei) 	/* Restart buffer table allocation */
85183975485SAlex Maftei (amaftei) 	efx->next_buffer_table = next_buffer_table;
85283975485SAlex Maftei (amaftei) 
85383975485SAlex Maftei (amaftei) 	for (i = 0; i < efx->n_channels; i++) {
85483975485SAlex Maftei (amaftei) 		channel = efx->channel[i];
85583975485SAlex Maftei (amaftei) 		if (!channel->type->copy)
85683975485SAlex Maftei (amaftei) 			continue;
85783975485SAlex Maftei (amaftei) 		rc = efx_probe_channel(channel);
85883975485SAlex Maftei (amaftei) 		if (rc)
85983975485SAlex Maftei (amaftei) 			goto rollback;
86083975485SAlex Maftei (amaftei) 		efx_init_napi_channel(efx->channel[i]);
86183975485SAlex Maftei (amaftei) 	}
86283975485SAlex Maftei (amaftei) 
86383975485SAlex Maftei (amaftei) out:
86483975485SAlex Maftei (amaftei) 	/* Destroy unused channel structures */
86583975485SAlex Maftei (amaftei) 	for (i = 0; i < efx->n_channels; i++) {
86683975485SAlex Maftei (amaftei) 		channel = other_channel[i];
86783975485SAlex Maftei (amaftei) 		if (channel && channel->type->copy) {
86883975485SAlex Maftei (amaftei) 			efx_fini_napi_channel(channel);
86983975485SAlex Maftei (amaftei) 			efx_remove_channel(channel);
87083975485SAlex Maftei (amaftei) 			kfree(channel);
87183975485SAlex Maftei (amaftei) 		}
87283975485SAlex Maftei (amaftei) 	}
87383975485SAlex Maftei (amaftei) 
87483975485SAlex Maftei (amaftei) 	rc2 = efx_soft_enable_interrupts(efx);
87583975485SAlex Maftei (amaftei) 	if (rc2) {
87683975485SAlex Maftei (amaftei) 		rc = rc ? rc : rc2;
87783975485SAlex Maftei (amaftei) 		netif_err(efx, drv, efx->net_dev,
87883975485SAlex Maftei (amaftei) 			  "unable to restart interrupts on channel reallocation\n");
87983975485SAlex Maftei (amaftei) 		efx_schedule_reset(efx, RESET_TYPE_DISABLE);
88083975485SAlex Maftei (amaftei) 	} else {
88183975485SAlex Maftei (amaftei) 		efx_start_all(efx);
88283975485SAlex Maftei (amaftei) 		efx_device_attach_if_not_resetting(efx);
88383975485SAlex Maftei (amaftei) 	}
88483975485SAlex Maftei (amaftei) 	return rc;
88583975485SAlex Maftei (amaftei) 
88683975485SAlex Maftei (amaftei) rollback:
88783975485SAlex Maftei (amaftei) 	/* Swap back */
88883975485SAlex Maftei (amaftei) 	efx->rxq_entries = old_rxq_entries;
88983975485SAlex Maftei (amaftei) 	efx->txq_entries = old_txq_entries;
8900cf765fbSJiapeng Chong 	for (i = 0; i < efx->n_channels; i++)
8910cf765fbSJiapeng Chong 		swap(efx->channel[i], other_channel[i]);
89283975485SAlex Maftei (amaftei) 	goto out;
89383975485SAlex Maftei (amaftei) }
89483975485SAlex Maftei (amaftei) 
8956215b608SÍñigo Huguet static inline int
8966215b608SÍñigo Huguet efx_set_xdp_tx_queue(struct efx_nic *efx, int xdp_queue_number,
8976215b608SÍñigo Huguet 		     struct efx_tx_queue *tx_queue)
8986215b608SÍñigo Huguet {
8996215b608SÍñigo Huguet 	if (xdp_queue_number >= efx->xdp_tx_queue_count)
9006215b608SÍñigo Huguet 		return -EINVAL;
9016215b608SÍñigo Huguet 
9026215b608SÍñigo Huguet 	netif_dbg(efx, drv, efx->net_dev, "Channel %u TXQ %u is XDP %u, HW %u\n",
9036215b608SÍñigo Huguet 		  tx_queue->channel->channel, tx_queue->label,
9046215b608SÍñigo Huguet 		  xdp_queue_number, tx_queue->queue);
9056215b608SÍñigo Huguet 	efx->xdp_tx_queues[xdp_queue_number] = tx_queue;
9066215b608SÍñigo Huguet 	return 0;
9076215b608SÍñigo Huguet }
9086215b608SÍñigo Huguet 
90983975485SAlex Maftei (amaftei) int efx_set_channels(struct efx_nic *efx)
91083975485SAlex Maftei (amaftei) {
91183975485SAlex Maftei (amaftei) 	struct efx_tx_queue *tx_queue;
912a81dcd85SEdward Cree 	struct efx_channel *channel;
913a81dcd85SEdward Cree 	unsigned int next_queue = 0;
91483975485SAlex Maftei (amaftei) 	int xdp_queue_number;
91569a70496SEdward Cree 	int rc;
91683975485SAlex Maftei (amaftei) 
91783975485SAlex Maftei (amaftei) 	efx->tx_channel_offset =
91883975485SAlex Maftei (amaftei) 		efx_separate_tx_channels ?
91983975485SAlex Maftei (amaftei) 		efx->n_channels - efx->n_tx_channels : 0;
92083975485SAlex Maftei (amaftei) 
92183975485SAlex Maftei (amaftei) 	if (efx->xdp_tx_queue_count) {
92283975485SAlex Maftei (amaftei) 		EFX_WARN_ON_PARANOID(efx->xdp_tx_queues);
92383975485SAlex Maftei (amaftei) 
92483975485SAlex Maftei (amaftei) 		/* Allocate array for XDP TX queue lookup. */
92583975485SAlex Maftei (amaftei) 		efx->xdp_tx_queues = kcalloc(efx->xdp_tx_queue_count,
92683975485SAlex Maftei (amaftei) 					     sizeof(*efx->xdp_tx_queues),
92783975485SAlex Maftei (amaftei) 					     GFP_KERNEL);
92883975485SAlex Maftei (amaftei) 		if (!efx->xdp_tx_queues)
92983975485SAlex Maftei (amaftei) 			return -ENOMEM;
93083975485SAlex Maftei (amaftei) 	}
93183975485SAlex Maftei (amaftei) 
93283975485SAlex Maftei (amaftei) 	/* We need to mark which channels really have RX and TX
93383975485SAlex Maftei (amaftei) 	 * queues, and adjust the TX queue numbers if we have separate
93483975485SAlex Maftei (amaftei) 	 * RX-only and TX-only channels.
93583975485SAlex Maftei (amaftei) 	 */
93683975485SAlex Maftei (amaftei) 	xdp_queue_number = 0;
93783975485SAlex Maftei (amaftei) 	efx_for_each_channel(channel, efx) {
93883975485SAlex Maftei (amaftei) 		if (channel->channel < efx->n_rx_channels)
93983975485SAlex Maftei (amaftei) 			channel->rx_queue.core_index = channel->channel;
94083975485SAlex Maftei (amaftei) 		else
94183975485SAlex Maftei (amaftei) 			channel->rx_queue.core_index = -1;
94283975485SAlex Maftei (amaftei) 
943a81dcd85SEdward Cree 		if (channel->channel >= efx->tx_channel_offset) {
944a81dcd85SEdward Cree 			if (efx_channel_is_xdp_tx(channel)) {
94583975485SAlex Maftei (amaftei) 				efx_for_each_channel_tx_queue(tx_queue, channel) {
946a81dcd85SEdward Cree 					tx_queue->queue = next_queue++;
9476215b608SÍñigo Huguet 					rc = efx_set_xdp_tx_queue(efx, xdp_queue_number, tx_queue);
9486215b608SÍñigo Huguet 					if (rc == 0)
94983975485SAlex Maftei (amaftei) 						xdp_queue_number++;
95083975485SAlex Maftei (amaftei) 				}
951a81dcd85SEdward Cree 			} else {
952a81dcd85SEdward Cree 				efx_for_each_channel_tx_queue(tx_queue, channel) {
953a81dcd85SEdward Cree 					tx_queue->queue = next_queue++;
954a81dcd85SEdward Cree 					netif_dbg(efx, drv, efx->net_dev, "Channel %u TXQ %u is HW %u\n",
955a81dcd85SEdward Cree 						  channel->channel, tx_queue->label,
956a81dcd85SEdward Cree 						  tx_queue->queue);
957a81dcd85SEdward Cree 				}
9586215b608SÍñigo Huguet 
9596215b608SÍñigo Huguet 				/* If XDP is borrowing queues from net stack, it must use the queue
9606215b608SÍñigo Huguet 				 * with no csum offload, which is the first one of the channel
9616215b608SÍñigo Huguet 				 * (note: channel->tx_queue_by_type is not initialized yet)
9626215b608SÍñigo Huguet 				 */
9636215b608SÍñigo Huguet 				if (efx->xdp_txq_queues_mode == EFX_XDP_TX_QUEUES_BORROWED) {
9646215b608SÍñigo Huguet 					tx_queue = &channel->tx_queue[0];
9656215b608SÍñigo Huguet 					rc = efx_set_xdp_tx_queue(efx, xdp_queue_number, tx_queue);
9666215b608SÍñigo Huguet 					if (rc == 0)
9676215b608SÍñigo Huguet 						xdp_queue_number++;
9686215b608SÍñigo Huguet 				}
969a81dcd85SEdward Cree 			}
97083975485SAlex Maftei (amaftei) 		}
97183975485SAlex Maftei (amaftei) 	}
97241544618SÍñigo Huguet 	WARN_ON(efx->xdp_txq_queues_mode == EFX_XDP_TX_QUEUES_DEDICATED &&
97341544618SÍñigo Huguet 		xdp_queue_number != efx->xdp_tx_queue_count);
97441544618SÍñigo Huguet 	WARN_ON(efx->xdp_txq_queues_mode != EFX_XDP_TX_QUEUES_DEDICATED &&
97541544618SÍñigo Huguet 		xdp_queue_number > efx->xdp_tx_queue_count);
97641544618SÍñigo Huguet 
9776215b608SÍñigo Huguet 	/* If we have more CPUs than assigned XDP TX queues, assign the already
9786215b608SÍñigo Huguet 	 * existing queues to the exceeding CPUs
97941544618SÍñigo Huguet 	 */
98041544618SÍñigo Huguet 	next_queue = 0;
98141544618SÍñigo Huguet 	while (xdp_queue_number < efx->xdp_tx_queue_count) {
98241544618SÍñigo Huguet 		tx_queue = efx->xdp_tx_queues[next_queue++];
9836215b608SÍñigo Huguet 		rc = efx_set_xdp_tx_queue(efx, xdp_queue_number, tx_queue);
9846215b608SÍñigo Huguet 		if (rc == 0)
9856215b608SÍñigo Huguet 			xdp_queue_number++;
98641544618SÍñigo Huguet 	}
98769a70496SEdward Cree 
98869a70496SEdward Cree 	rc = netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
98969a70496SEdward Cree 	if (rc)
99069a70496SEdward Cree 		return rc;
99169a70496SEdward Cree 	return netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
99283975485SAlex Maftei (amaftei) }
99383975485SAlex Maftei (amaftei) 
99483975485SAlex Maftei (amaftei) bool efx_default_channel_want_txqs(struct efx_channel *channel)
99583975485SAlex Maftei (amaftei) {
99683975485SAlex Maftei (amaftei) 	return channel->channel - channel->efx->tx_channel_offset <
99783975485SAlex Maftei (amaftei) 		channel->efx->n_tx_channels;
99883975485SAlex Maftei (amaftei) }
99983975485SAlex Maftei (amaftei) 
1000e20ba5b1SAlex Maftei (amaftei) /*************
1001e20ba5b1SAlex Maftei (amaftei)  * START/STOP
1002e20ba5b1SAlex Maftei (amaftei)  *************/
1003e20ba5b1SAlex Maftei (amaftei) 
1004e20ba5b1SAlex Maftei (amaftei) int efx_soft_enable_interrupts(struct efx_nic *efx)
1005e20ba5b1SAlex Maftei (amaftei) {
1006e20ba5b1SAlex Maftei (amaftei) 	struct efx_channel *channel, *end_channel;
1007e20ba5b1SAlex Maftei (amaftei) 	int rc;
1008e20ba5b1SAlex Maftei (amaftei) 
1009e20ba5b1SAlex Maftei (amaftei) 	BUG_ON(efx->state == STATE_DISABLED);
1010e20ba5b1SAlex Maftei (amaftei) 
1011e20ba5b1SAlex Maftei (amaftei) 	efx->irq_soft_enabled = true;
1012e20ba5b1SAlex Maftei (amaftei) 	smp_wmb();
1013e20ba5b1SAlex Maftei (amaftei) 
1014e20ba5b1SAlex Maftei (amaftei) 	efx_for_each_channel(channel, efx) {
1015e20ba5b1SAlex Maftei (amaftei) 		if (!channel->type->keep_eventq) {
1016e20ba5b1SAlex Maftei (amaftei) 			rc = efx_init_eventq(channel);
1017e20ba5b1SAlex Maftei (amaftei) 			if (rc)
1018e20ba5b1SAlex Maftei (amaftei) 				goto fail;
1019e20ba5b1SAlex Maftei (amaftei) 		}
1020e20ba5b1SAlex Maftei (amaftei) 		efx_start_eventq(channel);
1021e20ba5b1SAlex Maftei (amaftei) 	}
1022e20ba5b1SAlex Maftei (amaftei) 
1023e20ba5b1SAlex Maftei (amaftei) 	efx_mcdi_mode_event(efx);
1024e20ba5b1SAlex Maftei (amaftei) 
1025e20ba5b1SAlex Maftei (amaftei) 	return 0;
1026e20ba5b1SAlex Maftei (amaftei) fail:
1027e20ba5b1SAlex Maftei (amaftei) 	end_channel = channel;
1028e20ba5b1SAlex Maftei (amaftei) 	efx_for_each_channel(channel, efx) {
1029e20ba5b1SAlex Maftei (amaftei) 		if (channel == end_channel)
1030e20ba5b1SAlex Maftei (amaftei) 			break;
1031e20ba5b1SAlex Maftei (amaftei) 		efx_stop_eventq(channel);
1032e20ba5b1SAlex Maftei (amaftei) 		if (!channel->type->keep_eventq)
1033e20ba5b1SAlex Maftei (amaftei) 			efx_fini_eventq(channel);
1034e20ba5b1SAlex Maftei (amaftei) 	}
1035e20ba5b1SAlex Maftei (amaftei) 
1036e20ba5b1SAlex Maftei (amaftei) 	return rc;
1037e20ba5b1SAlex Maftei (amaftei) }
1038e20ba5b1SAlex Maftei (amaftei) 
1039e20ba5b1SAlex Maftei (amaftei) void efx_soft_disable_interrupts(struct efx_nic *efx)
1040e20ba5b1SAlex Maftei (amaftei) {
1041e20ba5b1SAlex Maftei (amaftei) 	struct efx_channel *channel;
1042e20ba5b1SAlex Maftei (amaftei) 
1043e20ba5b1SAlex Maftei (amaftei) 	if (efx->state == STATE_DISABLED)
1044e20ba5b1SAlex Maftei (amaftei) 		return;
1045e20ba5b1SAlex Maftei (amaftei) 
1046e20ba5b1SAlex Maftei (amaftei) 	efx_mcdi_mode_poll(efx);
1047e20ba5b1SAlex Maftei (amaftei) 
1048e20ba5b1SAlex Maftei (amaftei) 	efx->irq_soft_enabled = false;
1049e20ba5b1SAlex Maftei (amaftei) 	smp_wmb();
1050e20ba5b1SAlex Maftei (amaftei) 
1051e20ba5b1SAlex Maftei (amaftei) 	if (efx->legacy_irq)
1052e20ba5b1SAlex Maftei (amaftei) 		synchronize_irq(efx->legacy_irq);
1053e20ba5b1SAlex Maftei (amaftei) 
1054e20ba5b1SAlex Maftei (amaftei) 	efx_for_each_channel(channel, efx) {
1055e20ba5b1SAlex Maftei (amaftei) 		if (channel->irq)
1056e20ba5b1SAlex Maftei (amaftei) 			synchronize_irq(channel->irq);
1057e20ba5b1SAlex Maftei (amaftei) 
1058e20ba5b1SAlex Maftei (amaftei) 		efx_stop_eventq(channel);
1059e20ba5b1SAlex Maftei (amaftei) 		if (!channel->type->keep_eventq)
1060e20ba5b1SAlex Maftei (amaftei) 			efx_fini_eventq(channel);
1061e20ba5b1SAlex Maftei (amaftei) 	}
1062e20ba5b1SAlex Maftei (amaftei) 
1063e20ba5b1SAlex Maftei (amaftei) 	/* Flush the asynchronous MCDI request queue */
1064e20ba5b1SAlex Maftei (amaftei) 	efx_mcdi_flush_async(efx);
1065e20ba5b1SAlex Maftei (amaftei) }
1066e20ba5b1SAlex Maftei (amaftei) 
1067e20ba5b1SAlex Maftei (amaftei) int efx_enable_interrupts(struct efx_nic *efx)
1068e20ba5b1SAlex Maftei (amaftei) {
1069e20ba5b1SAlex Maftei (amaftei) 	struct efx_channel *channel, *end_channel;
1070e20ba5b1SAlex Maftei (amaftei) 	int rc;
1071e20ba5b1SAlex Maftei (amaftei) 
1072e20ba5b1SAlex Maftei (amaftei) 	/* TODO: Is this really a bug? */
1073e20ba5b1SAlex Maftei (amaftei) 	BUG_ON(efx->state == STATE_DISABLED);
1074e20ba5b1SAlex Maftei (amaftei) 
1075e20ba5b1SAlex Maftei (amaftei) 	if (efx->eeh_disabled_legacy_irq) {
1076e20ba5b1SAlex Maftei (amaftei) 		enable_irq(efx->legacy_irq);
1077e20ba5b1SAlex Maftei (amaftei) 		efx->eeh_disabled_legacy_irq = false;
1078e20ba5b1SAlex Maftei (amaftei) 	}
1079e20ba5b1SAlex Maftei (amaftei) 
1080e20ba5b1SAlex Maftei (amaftei) 	efx->type->irq_enable_master(efx);
1081e20ba5b1SAlex Maftei (amaftei) 
1082e20ba5b1SAlex Maftei (amaftei) 	efx_for_each_channel(channel, efx) {
1083e20ba5b1SAlex Maftei (amaftei) 		if (channel->type->keep_eventq) {
1084e20ba5b1SAlex Maftei (amaftei) 			rc = efx_init_eventq(channel);
1085e20ba5b1SAlex Maftei (amaftei) 			if (rc)
1086e20ba5b1SAlex Maftei (amaftei) 				goto fail;
1087e20ba5b1SAlex Maftei (amaftei) 		}
1088e20ba5b1SAlex Maftei (amaftei) 	}
1089e20ba5b1SAlex Maftei (amaftei) 
1090e20ba5b1SAlex Maftei (amaftei) 	rc = efx_soft_enable_interrupts(efx);
1091e20ba5b1SAlex Maftei (amaftei) 	if (rc)
1092e20ba5b1SAlex Maftei (amaftei) 		goto fail;
1093e20ba5b1SAlex Maftei (amaftei) 
1094e20ba5b1SAlex Maftei (amaftei) 	return 0;
1095e20ba5b1SAlex Maftei (amaftei) 
1096e20ba5b1SAlex Maftei (amaftei) fail:
1097e20ba5b1SAlex Maftei (amaftei) 	end_channel = channel;
1098e20ba5b1SAlex Maftei (amaftei) 	efx_for_each_channel(channel, efx) {
1099e20ba5b1SAlex Maftei (amaftei) 		if (channel == end_channel)
1100e20ba5b1SAlex Maftei (amaftei) 			break;
1101e20ba5b1SAlex Maftei (amaftei) 		if (channel->type->keep_eventq)
1102e20ba5b1SAlex Maftei (amaftei) 			efx_fini_eventq(channel);
1103e20ba5b1SAlex Maftei (amaftei) 	}
1104e20ba5b1SAlex Maftei (amaftei) 
1105e20ba5b1SAlex Maftei (amaftei) 	efx->type->irq_disable_non_ev(efx);
1106e20ba5b1SAlex Maftei (amaftei) 
1107e20ba5b1SAlex Maftei (amaftei) 	return rc;
1108e20ba5b1SAlex Maftei (amaftei) }
1109e20ba5b1SAlex Maftei (amaftei) 
1110e20ba5b1SAlex Maftei (amaftei) void efx_disable_interrupts(struct efx_nic *efx)
1111e20ba5b1SAlex Maftei (amaftei) {
1112e20ba5b1SAlex Maftei (amaftei) 	struct efx_channel *channel;
1113e20ba5b1SAlex Maftei (amaftei) 
1114e20ba5b1SAlex Maftei (amaftei) 	efx_soft_disable_interrupts(efx);
1115e20ba5b1SAlex Maftei (amaftei) 
1116e20ba5b1SAlex Maftei (amaftei) 	efx_for_each_channel(channel, efx) {
1117e20ba5b1SAlex Maftei (amaftei) 		if (channel->type->keep_eventq)
1118e20ba5b1SAlex Maftei (amaftei) 			efx_fini_eventq(channel);
1119e20ba5b1SAlex Maftei (amaftei) 	}
1120e20ba5b1SAlex Maftei (amaftei) 
1121e20ba5b1SAlex Maftei (amaftei) 	efx->type->irq_disable_non_ev(efx);
1122e20ba5b1SAlex Maftei (amaftei) }
1123e20ba5b1SAlex Maftei (amaftei) 
1124e20ba5b1SAlex Maftei (amaftei) void efx_start_channels(struct efx_nic *efx)
1125e20ba5b1SAlex Maftei (amaftei) {
1126e20ba5b1SAlex Maftei (amaftei) 	struct efx_tx_queue *tx_queue;
1127e20ba5b1SAlex Maftei (amaftei) 	struct efx_rx_queue *rx_queue;
1128e20ba5b1SAlex Maftei (amaftei) 	struct efx_channel *channel;
1129e20ba5b1SAlex Maftei (amaftei) 
1130e20ba5b1SAlex Maftei (amaftei) 	efx_for_each_channel(channel, efx) {
1131e20ba5b1SAlex Maftei (amaftei) 		efx_for_each_channel_tx_queue(tx_queue, channel) {
1132e20ba5b1SAlex Maftei (amaftei) 			efx_init_tx_queue(tx_queue);
1133e20ba5b1SAlex Maftei (amaftei) 			atomic_inc(&efx->active_queues);
1134e20ba5b1SAlex Maftei (amaftei) 		}
1135e20ba5b1SAlex Maftei (amaftei) 
1136e20ba5b1SAlex Maftei (amaftei) 		efx_for_each_channel_rx_queue(rx_queue, channel) {
1137e20ba5b1SAlex Maftei (amaftei) 			efx_init_rx_queue(rx_queue);
1138e20ba5b1SAlex Maftei (amaftei) 			atomic_inc(&efx->active_queues);
1139e20ba5b1SAlex Maftei (amaftei) 			efx_stop_eventq(channel);
1140e20ba5b1SAlex Maftei (amaftei) 			efx_fast_push_rx_descriptors(rx_queue, false);
1141e20ba5b1SAlex Maftei (amaftei) 			efx_start_eventq(channel);
1142e20ba5b1SAlex Maftei (amaftei) 		}
1143e20ba5b1SAlex Maftei (amaftei) 
1144e20ba5b1SAlex Maftei (amaftei) 		WARN_ON(channel->rx_pkt_n_frags);
1145e20ba5b1SAlex Maftei (amaftei) 	}
1146e20ba5b1SAlex Maftei (amaftei) }
1147e20ba5b1SAlex Maftei (amaftei) 
1148e20ba5b1SAlex Maftei (amaftei) void efx_stop_channels(struct efx_nic *efx)
1149e20ba5b1SAlex Maftei (amaftei) {
1150e20ba5b1SAlex Maftei (amaftei) 	struct efx_tx_queue *tx_queue;
1151e20ba5b1SAlex Maftei (amaftei) 	struct efx_rx_queue *rx_queue;
1152e20ba5b1SAlex Maftei (amaftei) 	struct efx_channel *channel;
1153b5775b47SAlex Maftei (amaftei) 	int rc = 0;
1154e20ba5b1SAlex Maftei (amaftei) 
1155e20ba5b1SAlex Maftei (amaftei) 	/* Stop RX refill */
1156e20ba5b1SAlex Maftei (amaftei) 	efx_for_each_channel(channel, efx) {
1157e20ba5b1SAlex Maftei (amaftei) 		efx_for_each_channel_rx_queue(rx_queue, channel)
1158e20ba5b1SAlex Maftei (amaftei) 			rx_queue->refill_enabled = false;
1159e20ba5b1SAlex Maftei (amaftei) 	}
1160e20ba5b1SAlex Maftei (amaftei) 
1161e20ba5b1SAlex Maftei (amaftei) 	efx_for_each_channel(channel, efx) {
1162e20ba5b1SAlex Maftei (amaftei) 		/* RX packet processing is pipelined, so wait for the
1163e20ba5b1SAlex Maftei (amaftei) 		 * NAPI handler to complete.  At least event queue 0
1164e20ba5b1SAlex Maftei (amaftei) 		 * might be kept active by non-data events, so don't
1165e20ba5b1SAlex Maftei (amaftei) 		 * use napi_synchronize() but actually disable NAPI
1166e20ba5b1SAlex Maftei (amaftei) 		 * temporarily.
1167e20ba5b1SAlex Maftei (amaftei) 		 */
1168e20ba5b1SAlex Maftei (amaftei) 		if (efx_channel_has_rx_queue(channel)) {
1169e20ba5b1SAlex Maftei (amaftei) 			efx_stop_eventq(channel);
1170e20ba5b1SAlex Maftei (amaftei) 			efx_start_eventq(channel);
1171e20ba5b1SAlex Maftei (amaftei) 		}
1172e20ba5b1SAlex Maftei (amaftei) 	}
1173e20ba5b1SAlex Maftei (amaftei) 
1174b5775b47SAlex Maftei (amaftei) 	if (efx->type->fini_dmaq)
1175e20ba5b1SAlex Maftei (amaftei) 		rc = efx->type->fini_dmaq(efx);
1176b5775b47SAlex Maftei (amaftei) 
1177e20ba5b1SAlex Maftei (amaftei) 	if (rc) {
1178e20ba5b1SAlex Maftei (amaftei) 		netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
1179e20ba5b1SAlex Maftei (amaftei) 	} else {
1180e20ba5b1SAlex Maftei (amaftei) 		netif_dbg(efx, drv, efx->net_dev,
1181e20ba5b1SAlex Maftei (amaftei) 			  "successfully flushed all queues\n");
1182e20ba5b1SAlex Maftei (amaftei) 	}
1183e20ba5b1SAlex Maftei (amaftei) 
1184e20ba5b1SAlex Maftei (amaftei) 	efx_for_each_channel(channel, efx) {
1185e20ba5b1SAlex Maftei (amaftei) 		efx_for_each_channel_rx_queue(rx_queue, channel)
1186e20ba5b1SAlex Maftei (amaftei) 			efx_fini_rx_queue(rx_queue);
1187f9cac93eSEdward Cree 		efx_for_each_channel_tx_queue(tx_queue, channel)
1188e20ba5b1SAlex Maftei (amaftei) 			efx_fini_tx_queue(tx_queue);
1189e20ba5b1SAlex Maftei (amaftei) 	}
1190e20ba5b1SAlex Maftei (amaftei) }
1191e20ba5b1SAlex Maftei (amaftei) 
1192768fd266SAlex Maftei (amaftei) /**************************************************************************
1193768fd266SAlex Maftei (amaftei)  *
1194768fd266SAlex Maftei (amaftei)  * NAPI interface
1195768fd266SAlex Maftei (amaftei)  *
1196768fd266SAlex Maftei (amaftei)  *************************************************************************/
1197768fd266SAlex Maftei (amaftei) 
1198768fd266SAlex Maftei (amaftei) /* Process channel's event queue
1199768fd266SAlex Maftei (amaftei)  *
1200768fd266SAlex Maftei (amaftei)  * This function is responsible for processing the event queue of a
1201768fd266SAlex Maftei (amaftei)  * single channel.  The caller must guarantee that this function will
1202768fd266SAlex Maftei (amaftei)  * never be concurrently called more than once on the same channel,
1203768fd266SAlex Maftei (amaftei)  * though different channels may be being processed concurrently.
1204768fd266SAlex Maftei (amaftei)  */
1205768fd266SAlex Maftei (amaftei) static int efx_process_channel(struct efx_channel *channel, int budget)
1206768fd266SAlex Maftei (amaftei) {
1207768fd266SAlex Maftei (amaftei) 	struct efx_tx_queue *tx_queue;
1208768fd266SAlex Maftei (amaftei) 	struct list_head rx_list;
1209768fd266SAlex Maftei (amaftei) 	int spent;
1210768fd266SAlex Maftei (amaftei) 
1211768fd266SAlex Maftei (amaftei) 	if (unlikely(!channel->enabled))
1212768fd266SAlex Maftei (amaftei) 		return 0;
1213768fd266SAlex Maftei (amaftei) 
1214768fd266SAlex Maftei (amaftei) 	/* Prepare the batch receive list */
1215768fd266SAlex Maftei (amaftei) 	EFX_WARN_ON_PARANOID(channel->rx_list != NULL);
1216768fd266SAlex Maftei (amaftei) 	INIT_LIST_HEAD(&rx_list);
1217768fd266SAlex Maftei (amaftei) 	channel->rx_list = &rx_list;
1218768fd266SAlex Maftei (amaftei) 
1219768fd266SAlex Maftei (amaftei) 	efx_for_each_channel_tx_queue(tx_queue, channel) {
1220768fd266SAlex Maftei (amaftei) 		tx_queue->pkts_compl = 0;
1221768fd266SAlex Maftei (amaftei) 		tx_queue->bytes_compl = 0;
1222768fd266SAlex Maftei (amaftei) 	}
1223768fd266SAlex Maftei (amaftei) 
1224768fd266SAlex Maftei (amaftei) 	spent = efx_nic_process_eventq(channel, budget);
1225768fd266SAlex Maftei (amaftei) 	if (spent && efx_channel_has_rx_queue(channel)) {
1226768fd266SAlex Maftei (amaftei) 		struct efx_rx_queue *rx_queue =
1227768fd266SAlex Maftei (amaftei) 			efx_channel_get_rx_queue(channel);
1228768fd266SAlex Maftei (amaftei) 
1229768fd266SAlex Maftei (amaftei) 		efx_rx_flush_packet(channel);
1230768fd266SAlex Maftei (amaftei) 		efx_fast_push_rx_descriptors(rx_queue, true);
1231768fd266SAlex Maftei (amaftei) 	}
1232768fd266SAlex Maftei (amaftei) 
1233768fd266SAlex Maftei (amaftei) 	/* Update BQL */
1234768fd266SAlex Maftei (amaftei) 	efx_for_each_channel_tx_queue(tx_queue, channel) {
1235768fd266SAlex Maftei (amaftei) 		if (tx_queue->bytes_compl) {
1236768fd266SAlex Maftei (amaftei) 			netdev_tx_completed_queue(tx_queue->core_txq,
1237768fd266SAlex Maftei (amaftei) 						  tx_queue->pkts_compl,
1238768fd266SAlex Maftei (amaftei) 						  tx_queue->bytes_compl);
1239768fd266SAlex Maftei (amaftei) 		}
1240768fd266SAlex Maftei (amaftei) 	}
1241768fd266SAlex Maftei (amaftei) 
1242768fd266SAlex Maftei (amaftei) 	/* Receive any packets we queued up */
1243768fd266SAlex Maftei (amaftei) 	netif_receive_skb_list(channel->rx_list);
1244768fd266SAlex Maftei (amaftei) 	channel->rx_list = NULL;
1245768fd266SAlex Maftei (amaftei) 
1246768fd266SAlex Maftei (amaftei) 	return spent;
1247768fd266SAlex Maftei (amaftei) }
1248768fd266SAlex Maftei (amaftei) 
1249768fd266SAlex Maftei (amaftei) static void efx_update_irq_mod(struct efx_nic *efx, struct efx_channel *channel)
1250768fd266SAlex Maftei (amaftei) {
1251768fd266SAlex Maftei (amaftei) 	int step = efx->irq_mod_step_us;
1252768fd266SAlex Maftei (amaftei) 
1253768fd266SAlex Maftei (amaftei) 	if (channel->irq_mod_score < irq_adapt_low_thresh) {
1254768fd266SAlex Maftei (amaftei) 		if (channel->irq_moderation_us > step) {
1255768fd266SAlex Maftei (amaftei) 			channel->irq_moderation_us -= step;
1256768fd266SAlex Maftei (amaftei) 			efx->type->push_irq_moderation(channel);
1257768fd266SAlex Maftei (amaftei) 		}
1258768fd266SAlex Maftei (amaftei) 	} else if (channel->irq_mod_score > irq_adapt_high_thresh) {
1259768fd266SAlex Maftei (amaftei) 		if (channel->irq_moderation_us <
1260768fd266SAlex Maftei (amaftei) 		    efx->irq_rx_moderation_us) {
1261768fd266SAlex Maftei (amaftei) 			channel->irq_moderation_us += step;
1262768fd266SAlex Maftei (amaftei) 			efx->type->push_irq_moderation(channel);
1263768fd266SAlex Maftei (amaftei) 		}
1264768fd266SAlex Maftei (amaftei) 	}
1265768fd266SAlex Maftei (amaftei) 
1266768fd266SAlex Maftei (amaftei) 	channel->irq_count = 0;
1267768fd266SAlex Maftei (amaftei) 	channel->irq_mod_score = 0;
1268768fd266SAlex Maftei (amaftei) }
1269768fd266SAlex Maftei (amaftei) 
1270768fd266SAlex Maftei (amaftei) /* NAPI poll handler
1271768fd266SAlex Maftei (amaftei)  *
1272768fd266SAlex Maftei (amaftei)  * NAPI guarantees serialisation of polls of the same device, which
1273768fd266SAlex Maftei (amaftei)  * provides the guarantee required by efx_process_channel().
1274768fd266SAlex Maftei (amaftei)  */
1275768fd266SAlex Maftei (amaftei) static int efx_poll(struct napi_struct *napi, int budget)
1276768fd266SAlex Maftei (amaftei) {
1277768fd266SAlex Maftei (amaftei) 	struct efx_channel *channel =
1278768fd266SAlex Maftei (amaftei) 		container_of(napi, struct efx_channel, napi_str);
1279768fd266SAlex Maftei (amaftei) 	struct efx_nic *efx = channel->efx;
1280b7683155SEdward Cree #ifdef CONFIG_RFS_ACCEL
1281b7683155SEdward Cree 	unsigned int time;
1282b7683155SEdward Cree #endif
1283768fd266SAlex Maftei (amaftei) 	int spent;
1284768fd266SAlex Maftei (amaftei) 
1285768fd266SAlex Maftei (amaftei) 	netif_vdbg(efx, intr, efx->net_dev,
1286768fd266SAlex Maftei (amaftei) 		   "channel %d NAPI poll executing on CPU %d\n",
1287768fd266SAlex Maftei (amaftei) 		   channel->channel, raw_smp_processor_id());
1288768fd266SAlex Maftei (amaftei) 
1289768fd266SAlex Maftei (amaftei) 	spent = efx_process_channel(channel, budget);
1290768fd266SAlex Maftei (amaftei) 
1291768fd266SAlex Maftei (amaftei) 	xdp_do_flush_map();
1292768fd266SAlex Maftei (amaftei) 
1293768fd266SAlex Maftei (amaftei) 	if (spent < budget) {
1294768fd266SAlex Maftei (amaftei) 		if (efx_channel_has_rx_queue(channel) &&
1295768fd266SAlex Maftei (amaftei) 		    efx->irq_rx_adaptive &&
1296768fd266SAlex Maftei (amaftei) 		    unlikely(++channel->irq_count == 1000)) {
1297768fd266SAlex Maftei (amaftei) 			efx_update_irq_mod(efx, channel);
1298768fd266SAlex Maftei (amaftei) 		}
1299768fd266SAlex Maftei (amaftei) 
1300768fd266SAlex Maftei (amaftei) #ifdef CONFIG_RFS_ACCEL
1301768fd266SAlex Maftei (amaftei) 		/* Perhaps expire some ARFS filters */
1302b7683155SEdward Cree 		time = jiffies - channel->rfs_last_expiry;
1303b7683155SEdward Cree 		/* Would our quota be >= 20? */
1304b7683155SEdward Cree 		if (channel->rfs_filter_count * time >= 600 * HZ)
1305768fd266SAlex Maftei (amaftei) 			mod_delayed_work(system_wq, &channel->filter_work, 0);
1306768fd266SAlex Maftei (amaftei) #endif
1307768fd266SAlex Maftei (amaftei) 
1308768fd266SAlex Maftei (amaftei) 		/* There is no race here; although napi_disable() will
1309768fd266SAlex Maftei (amaftei) 		 * only wait for napi_complete(), this isn't a problem
1310768fd266SAlex Maftei (amaftei) 		 * since efx_nic_eventq_read_ack() will have no effect if
1311768fd266SAlex Maftei (amaftei) 		 * interrupts have already been disabled.
1312768fd266SAlex Maftei (amaftei) 		 */
1313768fd266SAlex Maftei (amaftei) 		if (napi_complete_done(napi, spent))
1314768fd266SAlex Maftei (amaftei) 			efx_nic_eventq_read_ack(channel);
1315768fd266SAlex Maftei (amaftei) 	}
1316768fd266SAlex Maftei (amaftei) 
1317768fd266SAlex Maftei (amaftei) 	return spent;
1318768fd266SAlex Maftei (amaftei) }
1319768fd266SAlex Maftei (amaftei) 
1320768fd266SAlex Maftei (amaftei) void efx_init_napi_channel(struct efx_channel *channel)
1321768fd266SAlex Maftei (amaftei) {
1322768fd266SAlex Maftei (amaftei) 	struct efx_nic *efx = channel->efx;
1323768fd266SAlex Maftei (amaftei) 
1324768fd266SAlex Maftei (amaftei) 	channel->napi_dev = efx->net_dev;
1325768fd266SAlex Maftei (amaftei) 	netif_napi_add(channel->napi_dev, &channel->napi_str,
1326768fd266SAlex Maftei (amaftei) 		       efx_poll, napi_weight);
1327768fd266SAlex Maftei (amaftei) }
1328768fd266SAlex Maftei (amaftei) 
1329768fd266SAlex Maftei (amaftei) void efx_init_napi(struct efx_nic *efx)
1330768fd266SAlex Maftei (amaftei) {
1331768fd266SAlex Maftei (amaftei) 	struct efx_channel *channel;
1332768fd266SAlex Maftei (amaftei) 
1333768fd266SAlex Maftei (amaftei) 	efx_for_each_channel(channel, efx)
1334768fd266SAlex Maftei (amaftei) 		efx_init_napi_channel(channel);
1335768fd266SAlex Maftei (amaftei) }
1336768fd266SAlex Maftei (amaftei) 
1337768fd266SAlex Maftei (amaftei) void efx_fini_napi_channel(struct efx_channel *channel)
1338768fd266SAlex Maftei (amaftei) {
1339768fd266SAlex Maftei (amaftei) 	if (channel->napi_dev)
1340768fd266SAlex Maftei (amaftei) 		netif_napi_del(&channel->napi_str);
1341768fd266SAlex Maftei (amaftei) 
1342768fd266SAlex Maftei (amaftei) 	channel->napi_dev = NULL;
1343768fd266SAlex Maftei (amaftei) }
1344768fd266SAlex Maftei (amaftei) 
1345768fd266SAlex Maftei (amaftei) void efx_fini_napi(struct efx_nic *efx)
1346768fd266SAlex Maftei (amaftei) {
1347768fd266SAlex Maftei (amaftei) 	struct efx_channel *channel;
1348768fd266SAlex Maftei (amaftei) 
1349768fd266SAlex Maftei (amaftei) 	efx_for_each_channel(channel, efx)
1350768fd266SAlex Maftei (amaftei) 		efx_fini_napi_channel(channel);
1351768fd266SAlex Maftei (amaftei) }
1352