xref: /openbmc/linux/drivers/net/ethernet/sfc/efx.c (revision e45a4fed)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2874aeea5SJeff Kirsher /****************************************************************************
3f7a6d2c4SBen Hutchings  * Driver for Solarflare network controllers and boards
4874aeea5SJeff Kirsher  * Copyright 2005-2006 Fen Systems Ltd.
5f7a6d2c4SBen Hutchings  * Copyright 2005-2013 Solarflare Communications Inc.
6874aeea5SJeff Kirsher  */
7874aeea5SJeff Kirsher 
8874aeea5SJeff Kirsher #include <linux/module.h>
9874aeea5SJeff Kirsher #include <linux/pci.h>
10874aeea5SJeff Kirsher #include <linux/netdevice.h>
11874aeea5SJeff Kirsher #include <linux/etherdevice.h>
12874aeea5SJeff Kirsher #include <linux/delay.h>
13874aeea5SJeff Kirsher #include <linux/notifier.h>
14874aeea5SJeff Kirsher #include <linux/ip.h>
15874aeea5SJeff Kirsher #include <linux/tcp.h>
16874aeea5SJeff Kirsher #include <linux/in.h>
17874aeea5SJeff Kirsher #include <linux/ethtool.h>
18874aeea5SJeff Kirsher #include <linux/topology.h>
19874aeea5SJeff Kirsher #include <linux/gfp.h>
20626950dbSAlexandre Rames #include <linux/aer.h>
21b28405b0SAlexandre Rames #include <linux/interrupt.h>
22874aeea5SJeff Kirsher #include "net_driver.h"
23e5fbd977SJon Cooper #include <net/gre.h>
24e5fbd977SJon Cooper #include <net/udp_tunnel.h>
25874aeea5SJeff Kirsher #include "efx.h"
26874aeea5SJeff Kirsher #include "nic.h"
2771827443SEdward Cree #include "io.h"
28dd40781eSBen Hutchings #include "selftest.h"
297fa8d547SShradha Shah #include "sriov.h"
30874aeea5SJeff Kirsher 
31874aeea5SJeff Kirsher #include "mcdi.h"
32e5fbd977SJon Cooper #include "mcdi_pcol.h"
33874aeea5SJeff Kirsher #include "workarounds.h"
34874aeea5SJeff Kirsher 
35874aeea5SJeff Kirsher /**************************************************************************
36874aeea5SJeff Kirsher  *
37874aeea5SJeff Kirsher  * Type name strings
38874aeea5SJeff Kirsher  *
39874aeea5SJeff Kirsher  **************************************************************************
40874aeea5SJeff Kirsher  */
41874aeea5SJeff Kirsher 
42874aeea5SJeff Kirsher /* Loopback mode names (see LOOPBACK_MODE()) */
43874aeea5SJeff Kirsher const unsigned int efx_loopback_mode_max = LOOPBACK_MAX;
4418e83e4cSBen Hutchings const char *const efx_loopback_mode_names[] = {
45874aeea5SJeff Kirsher 	[LOOPBACK_NONE]		= "NONE",
46874aeea5SJeff Kirsher 	[LOOPBACK_DATA]		= "DATAPATH",
47874aeea5SJeff Kirsher 	[LOOPBACK_GMAC]		= "GMAC",
48874aeea5SJeff Kirsher 	[LOOPBACK_XGMII]	= "XGMII",
49874aeea5SJeff Kirsher 	[LOOPBACK_XGXS]		= "XGXS",
50874aeea5SJeff Kirsher 	[LOOPBACK_XAUI]		= "XAUI",
51874aeea5SJeff Kirsher 	[LOOPBACK_GMII]		= "GMII",
52874aeea5SJeff Kirsher 	[LOOPBACK_SGMII]	= "SGMII",
53874aeea5SJeff Kirsher 	[LOOPBACK_XGBR]		= "XGBR",
54874aeea5SJeff Kirsher 	[LOOPBACK_XFI]		= "XFI",
55874aeea5SJeff Kirsher 	[LOOPBACK_XAUI_FAR]	= "XAUI_FAR",
56874aeea5SJeff Kirsher 	[LOOPBACK_GMII_FAR]	= "GMII_FAR",
57874aeea5SJeff Kirsher 	[LOOPBACK_SGMII_FAR]	= "SGMII_FAR",
58874aeea5SJeff Kirsher 	[LOOPBACK_XFI_FAR]	= "XFI_FAR",
59874aeea5SJeff Kirsher 	[LOOPBACK_GPHY]		= "GPHY",
60874aeea5SJeff Kirsher 	[LOOPBACK_PHYXS]	= "PHYXS",
61874aeea5SJeff Kirsher 	[LOOPBACK_PCS]		= "PCS",
62874aeea5SJeff Kirsher 	[LOOPBACK_PMAPMD]	= "PMA/PMD",
63874aeea5SJeff Kirsher 	[LOOPBACK_XPORT]	= "XPORT",
64874aeea5SJeff Kirsher 	[LOOPBACK_XGMII_WS]	= "XGMII_WS",
65874aeea5SJeff Kirsher 	[LOOPBACK_XAUI_WS]	= "XAUI_WS",
66874aeea5SJeff Kirsher 	[LOOPBACK_XAUI_WS_FAR]  = "XAUI_WS_FAR",
67874aeea5SJeff Kirsher 	[LOOPBACK_XAUI_WS_NEAR] = "XAUI_WS_NEAR",
68874aeea5SJeff Kirsher 	[LOOPBACK_GMII_WS]	= "GMII_WS",
69874aeea5SJeff Kirsher 	[LOOPBACK_XFI_WS]	= "XFI_WS",
70874aeea5SJeff Kirsher 	[LOOPBACK_XFI_WS_FAR]	= "XFI_WS_FAR",
71874aeea5SJeff Kirsher 	[LOOPBACK_PHYXS_WS]	= "PHYXS_WS",
72874aeea5SJeff Kirsher };
73874aeea5SJeff Kirsher 
74874aeea5SJeff Kirsher const unsigned int efx_reset_type_max = RESET_TYPE_MAX;
7518e83e4cSBen Hutchings const char *const efx_reset_type_names[] = {
76874aeea5SJeff Kirsher 	[RESET_TYPE_INVISIBLE]          = "INVISIBLE",
77874aeea5SJeff Kirsher 	[RESET_TYPE_ALL]                = "ALL",
78626950dbSAlexandre Rames 	[RESET_TYPE_RECOVER_OR_ALL]     = "RECOVER_OR_ALL",
79874aeea5SJeff Kirsher 	[RESET_TYPE_WORLD]              = "WORLD",
80626950dbSAlexandre Rames 	[RESET_TYPE_RECOVER_OR_DISABLE] = "RECOVER_OR_DISABLE",
81087e9025SJon Cooper 	[RESET_TYPE_DATAPATH]           = "DATAPATH",
82e283546cSEdward Cree 	[RESET_TYPE_MC_BIST]		= "MC_BIST",
83874aeea5SJeff Kirsher 	[RESET_TYPE_DISABLE]            = "DISABLE",
84874aeea5SJeff Kirsher 	[RESET_TYPE_TX_WATCHDOG]        = "TX_WATCHDOG",
85874aeea5SJeff Kirsher 	[RESET_TYPE_INT_ERROR]          = "INT_ERROR",
863de82b91SAlexandre Rames 	[RESET_TYPE_DMA_ERROR]          = "DMA_ERROR",
87874aeea5SJeff Kirsher 	[RESET_TYPE_TX_SKIP]            = "TX_SKIP",
88874aeea5SJeff Kirsher 	[RESET_TYPE_MC_FAILURE]         = "MC_FAILURE",
89e283546cSEdward Cree 	[RESET_TYPE_MCDI_TIMEOUT]	= "MCDI_TIMEOUT (FLR)",
90874aeea5SJeff Kirsher };
91874aeea5SJeff Kirsher 
92e5fbd977SJon Cooper /* UDP tunnel type names */
93e5fbd977SJon Cooper static const char *const efx_udp_tunnel_type_names[] = {
94e5fbd977SJon Cooper 	[TUNNEL_ENCAP_UDP_PORT_ENTRY_VXLAN] = "vxlan",
95e5fbd977SJon Cooper 	[TUNNEL_ENCAP_UDP_PORT_ENTRY_GENEVE] = "geneve",
96e5fbd977SJon Cooper };
97e5fbd977SJon Cooper 
98e5fbd977SJon Cooper void efx_get_udp_tunnel_type_name(u16 type, char *buf, size_t buflen)
99e5fbd977SJon Cooper {
100e5fbd977SJon Cooper 	if (type < ARRAY_SIZE(efx_udp_tunnel_type_names) &&
101e5fbd977SJon Cooper 	    efx_udp_tunnel_type_names[type] != NULL)
102e5fbd977SJon Cooper 		snprintf(buf, buflen, "%s", efx_udp_tunnel_type_names[type]);
103e5fbd977SJon Cooper 	else
104e5fbd977SJon Cooper 		snprintf(buf, buflen, "type %d", type);
105e5fbd977SJon Cooper }
106e5fbd977SJon Cooper 
107874aeea5SJeff Kirsher /* Reset workqueue. If any NIC has a hardware failure then a reset will be
108874aeea5SJeff Kirsher  * queued onto this work queue. This is not a per-nic work queue, because
109874aeea5SJeff Kirsher  * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
110874aeea5SJeff Kirsher  */
111874aeea5SJeff Kirsher static struct workqueue_struct *reset_workqueue;
112874aeea5SJeff Kirsher 
11374cd60a4SJon Cooper /* How often and how many times to poll for a reset while waiting for a
11474cd60a4SJon Cooper  * BIST that another function started to complete.
11574cd60a4SJon Cooper  */
11674cd60a4SJon Cooper #define BIST_WAIT_DELAY_MS	100
11774cd60a4SJon Cooper #define BIST_WAIT_DELAY_COUNT	100
11874cd60a4SJon Cooper 
119874aeea5SJeff Kirsher /**************************************************************************
120874aeea5SJeff Kirsher  *
121874aeea5SJeff Kirsher  * Configurable values
122874aeea5SJeff Kirsher  *
123874aeea5SJeff Kirsher  *************************************************************************/
124874aeea5SJeff Kirsher 
125874aeea5SJeff Kirsher /*
126874aeea5SJeff Kirsher  * Use separate channels for TX and RX events
127874aeea5SJeff Kirsher  *
128874aeea5SJeff Kirsher  * Set this to 1 to use separate channels for TX and RX. It allows us
129874aeea5SJeff Kirsher  * to control interrupt affinity separately for TX and RX.
130874aeea5SJeff Kirsher  *
131874aeea5SJeff Kirsher  * This is only used in MSI-X interrupt mode
132874aeea5SJeff Kirsher  */
133b0fbdae1SShradha Shah bool efx_separate_tx_channels;
134b0fbdae1SShradha Shah module_param(efx_separate_tx_channels, bool, 0444);
135b0fbdae1SShradha Shah MODULE_PARM_DESC(efx_separate_tx_channels,
136874aeea5SJeff Kirsher 		 "Use separate channels for TX and RX");
137874aeea5SJeff Kirsher 
138874aeea5SJeff Kirsher /* This is the weight assigned to each of the (per-channel) virtual
139874aeea5SJeff Kirsher  * NAPI devices.
140874aeea5SJeff Kirsher  */
141874aeea5SJeff Kirsher static int napi_weight = 64;
142874aeea5SJeff Kirsher 
143874aeea5SJeff Kirsher /* This is the time (in jiffies) between invocations of the hardware
144626950dbSAlexandre Rames  * monitor.
145626950dbSAlexandre Rames  * On Falcon-based NICs, this will:
146874aeea5SJeff Kirsher  * - Check the on-board hardware monitor;
147874aeea5SJeff Kirsher  * - Poll the link state and reconfigure the hardware as necessary.
148626950dbSAlexandre Rames  * On Siena-based NICs for power systems with EEH support, this will give EEH a
149626950dbSAlexandre Rames  * chance to start.
150874aeea5SJeff Kirsher  */
151874aeea5SJeff Kirsher static unsigned int efx_monitor_interval = 1 * HZ;
152874aeea5SJeff Kirsher 
153874aeea5SJeff Kirsher /* Initial interrupt moderation settings.  They can be modified after
154874aeea5SJeff Kirsher  * module load with ethtool.
155874aeea5SJeff Kirsher  *
156874aeea5SJeff Kirsher  * The default for RX should strike a balance between increasing the
157874aeea5SJeff Kirsher  * round-trip latency and reducing overhead.
158874aeea5SJeff Kirsher  */
159874aeea5SJeff Kirsher static unsigned int rx_irq_mod_usec = 60;
160874aeea5SJeff Kirsher 
161874aeea5SJeff Kirsher /* Initial interrupt moderation settings.  They can be modified after
162874aeea5SJeff Kirsher  * module load with ethtool.
163874aeea5SJeff Kirsher  *
164874aeea5SJeff Kirsher  * This default is chosen to ensure that a 10G link does not go idle
165874aeea5SJeff Kirsher  * while a TX queue is stopped after it has become full.  A queue is
166874aeea5SJeff Kirsher  * restarted when it drops below half full.  The time this takes (assuming
167874aeea5SJeff Kirsher  * worst case 3 descriptors per packet and 1024 descriptors) is
168874aeea5SJeff Kirsher  *   512 / 3 * 1.2 = 205 usec.
169874aeea5SJeff Kirsher  */
170874aeea5SJeff Kirsher static unsigned int tx_irq_mod_usec = 150;
171874aeea5SJeff Kirsher 
172874aeea5SJeff Kirsher /* This is the first interrupt mode to try out of:
173874aeea5SJeff Kirsher  * 0 => MSI-X
174874aeea5SJeff Kirsher  * 1 => MSI
175874aeea5SJeff Kirsher  * 2 => legacy
176874aeea5SJeff Kirsher  */
177874aeea5SJeff Kirsher static unsigned int interrupt_mode;
178874aeea5SJeff Kirsher 
179874aeea5SJeff Kirsher /* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
180874aeea5SJeff Kirsher  * i.e. the number of CPUs among which we may distribute simultaneous
181874aeea5SJeff Kirsher  * interrupt handling.
182874aeea5SJeff Kirsher  *
183874aeea5SJeff Kirsher  * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
184cdb08f8fSBen Hutchings  * The default (0) means to assign an interrupt to each core.
185874aeea5SJeff Kirsher  */
186874aeea5SJeff Kirsher static unsigned int rss_cpus;
187874aeea5SJeff Kirsher module_param(rss_cpus, uint, 0444);
188874aeea5SJeff Kirsher MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
189874aeea5SJeff Kirsher 
190b9cc977dSBen Hutchings static bool phy_flash_cfg;
191b9cc977dSBen Hutchings module_param(phy_flash_cfg, bool, 0644);
192874aeea5SJeff Kirsher MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
193874aeea5SJeff Kirsher 
194e7bed9c8SBen Hutchings static unsigned irq_adapt_low_thresh = 8000;
195874aeea5SJeff Kirsher module_param(irq_adapt_low_thresh, uint, 0644);
196874aeea5SJeff Kirsher MODULE_PARM_DESC(irq_adapt_low_thresh,
197874aeea5SJeff Kirsher 		 "Threshold score for reducing IRQ moderation");
198874aeea5SJeff Kirsher 
199e7bed9c8SBen Hutchings static unsigned irq_adapt_high_thresh = 16000;
200874aeea5SJeff Kirsher module_param(irq_adapt_high_thresh, uint, 0644);
201874aeea5SJeff Kirsher MODULE_PARM_DESC(irq_adapt_high_thresh,
202874aeea5SJeff Kirsher 		 "Threshold score for increasing IRQ moderation");
203874aeea5SJeff Kirsher 
204874aeea5SJeff Kirsher static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
205874aeea5SJeff Kirsher 			 NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
206874aeea5SJeff Kirsher 			 NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
207874aeea5SJeff Kirsher 			 NETIF_MSG_TX_ERR | NETIF_MSG_HW);
208874aeea5SJeff Kirsher module_param(debug, uint, 0);
209874aeea5SJeff Kirsher MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
210874aeea5SJeff Kirsher 
211874aeea5SJeff Kirsher /**************************************************************************
212874aeea5SJeff Kirsher  *
213874aeea5SJeff Kirsher  * Utility functions and prototypes
214874aeea5SJeff Kirsher  *
215874aeea5SJeff Kirsher  *************************************************************************/
216874aeea5SJeff Kirsher 
217261e4d96SJon Cooper static int efx_soft_enable_interrupts(struct efx_nic *efx);
218d8291187SBen Hutchings static void efx_soft_disable_interrupts(struct efx_nic *efx);
2197f967c01SBen Hutchings static void efx_remove_channel(struct efx_channel *channel);
220874aeea5SJeff Kirsher static void efx_remove_channels(struct efx_nic *efx);
2217f967c01SBen Hutchings static const struct efx_channel_type efx_default_channel_type;
222874aeea5SJeff Kirsher static void efx_remove_port(struct efx_nic *efx);
2237f967c01SBen Hutchings static void efx_init_napi_channel(struct efx_channel *channel);
224874aeea5SJeff Kirsher static void efx_fini_napi(struct efx_nic *efx);
225874aeea5SJeff Kirsher static void efx_fini_napi_channel(struct efx_channel *channel);
226874aeea5SJeff Kirsher static void efx_fini_struct(struct efx_nic *efx);
227874aeea5SJeff Kirsher static void efx_start_all(struct efx_nic *efx);
228874aeea5SJeff Kirsher static void efx_stop_all(struct efx_nic *efx);
229e45a4fedSCharles McLachlan static int efx_xdp_setup_prog(struct efx_nic *efx, struct bpf_prog *prog);
230e45a4fedSCharles McLachlan static int efx_xdp(struct net_device *dev, struct netdev_bpf *xdp);
231874aeea5SJeff Kirsher 
232874aeea5SJeff Kirsher #define EFX_ASSERT_RESET_SERIALISED(efx)		\
233874aeea5SJeff Kirsher 	do {						\
234f16aeea0SBen Hutchings 		if ((efx->state == STATE_READY) ||	\
235626950dbSAlexandre Rames 		    (efx->state == STATE_RECOVERY) ||	\
236874aeea5SJeff Kirsher 		    (efx->state == STATE_DISABLED))	\
237874aeea5SJeff Kirsher 			ASSERT_RTNL();			\
238874aeea5SJeff Kirsher 	} while (0)
239874aeea5SJeff Kirsher 
2408b7325b4SBen Hutchings static int efx_check_disabled(struct efx_nic *efx)
2418b7325b4SBen Hutchings {
242626950dbSAlexandre Rames 	if (efx->state == STATE_DISABLED || efx->state == STATE_RECOVERY) {
2438b7325b4SBen Hutchings 		netif_err(efx, drv, efx->net_dev,
2448b7325b4SBen Hutchings 			  "device is disabled due to earlier errors\n");
2458b7325b4SBen Hutchings 		return -EIO;
2468b7325b4SBen Hutchings 	}
2478b7325b4SBen Hutchings 	return 0;
2488b7325b4SBen Hutchings }
2498b7325b4SBen Hutchings 
250874aeea5SJeff Kirsher /**************************************************************************
251874aeea5SJeff Kirsher  *
252874aeea5SJeff Kirsher  * Event queue processing
253874aeea5SJeff Kirsher  *
254874aeea5SJeff Kirsher  *************************************************************************/
255874aeea5SJeff Kirsher 
256874aeea5SJeff Kirsher /* Process channel's event queue
257874aeea5SJeff Kirsher  *
258874aeea5SJeff Kirsher  * This function is responsible for processing the event queue of a
259874aeea5SJeff Kirsher  * single channel.  The caller must guarantee that this function will
260874aeea5SJeff Kirsher  * never be concurrently called more than once on the same channel,
261874aeea5SJeff Kirsher  * though different channels may be being processed concurrently.
262874aeea5SJeff Kirsher  */
263874aeea5SJeff Kirsher static int efx_process_channel(struct efx_channel *channel, int budget)
264874aeea5SJeff Kirsher {
265c936835cSPeter Dunning 	struct efx_tx_queue *tx_queue;
266e090bfb9SEdward Cree 	struct list_head rx_list;
267874aeea5SJeff Kirsher 	int spent;
268874aeea5SJeff Kirsher 
2699f2cb71cSBen Hutchings 	if (unlikely(!channel->enabled))
270874aeea5SJeff Kirsher 		return 0;
271874aeea5SJeff Kirsher 
272e090bfb9SEdward Cree 	/* Prepare the batch receive list */
273e090bfb9SEdward Cree 	EFX_WARN_ON_PARANOID(channel->rx_list != NULL);
274e090bfb9SEdward Cree 	INIT_LIST_HEAD(&rx_list);
275e090bfb9SEdward Cree 	channel->rx_list = &rx_list;
276e090bfb9SEdward Cree 
277c936835cSPeter Dunning 	efx_for_each_channel_tx_queue(tx_queue, channel) {
278c936835cSPeter Dunning 		tx_queue->pkts_compl = 0;
279c936835cSPeter Dunning 		tx_queue->bytes_compl = 0;
280c936835cSPeter Dunning 	}
281c936835cSPeter Dunning 
282874aeea5SJeff Kirsher 	spent = efx_nic_process_eventq(channel, budget);
283d9ab7007SBen Hutchings 	if (spent && efx_channel_has_rx_queue(channel)) {
284d9ab7007SBen Hutchings 		struct efx_rx_queue *rx_queue =
285d9ab7007SBen Hutchings 			efx_channel_get_rx_queue(channel);
286874aeea5SJeff Kirsher 
287ff734ef4SBen Hutchings 		efx_rx_flush_packet(channel);
288cce28794SJon Cooper 		efx_fast_push_rx_descriptors(rx_queue, true);
289d9ab7007SBen Hutchings 	}
290874aeea5SJeff Kirsher 
291c936835cSPeter Dunning 	/* Update BQL */
292c936835cSPeter Dunning 	efx_for_each_channel_tx_queue(tx_queue, channel) {
293c936835cSPeter Dunning 		if (tx_queue->bytes_compl) {
294c936835cSPeter Dunning 			netdev_tx_completed_queue(tx_queue->core_txq,
295c936835cSPeter Dunning 				tx_queue->pkts_compl, tx_queue->bytes_compl);
296c936835cSPeter Dunning 		}
297c936835cSPeter Dunning 	}
298c936835cSPeter Dunning 
299e090bfb9SEdward Cree 	/* Receive any packets we queued up */
300e090bfb9SEdward Cree 	netif_receive_skb_list(channel->rx_list);
301e090bfb9SEdward Cree 	channel->rx_list = NULL;
302e090bfb9SEdward Cree 
303874aeea5SJeff Kirsher 	return spent;
304874aeea5SJeff Kirsher }
305874aeea5SJeff Kirsher 
306874aeea5SJeff Kirsher /* NAPI poll handler
307874aeea5SJeff Kirsher  *
308874aeea5SJeff Kirsher  * NAPI guarantees serialisation of polls of the same device, which
309874aeea5SJeff Kirsher  * provides the guarantee required by efx_process_channel().
310874aeea5SJeff Kirsher  */
311539de7c5SBert Kenward static void efx_update_irq_mod(struct efx_nic *efx, struct efx_channel *channel)
312539de7c5SBert Kenward {
313539de7c5SBert Kenward 	int step = efx->irq_mod_step_us;
314539de7c5SBert Kenward 
315539de7c5SBert Kenward 	if (channel->irq_mod_score < irq_adapt_low_thresh) {
316539de7c5SBert Kenward 		if (channel->irq_moderation_us > step) {
317539de7c5SBert Kenward 			channel->irq_moderation_us -= step;
318539de7c5SBert Kenward 			efx->type->push_irq_moderation(channel);
319539de7c5SBert Kenward 		}
320539de7c5SBert Kenward 	} else if (channel->irq_mod_score > irq_adapt_high_thresh) {
321539de7c5SBert Kenward 		if (channel->irq_moderation_us <
322539de7c5SBert Kenward 		    efx->irq_rx_moderation_us) {
323539de7c5SBert Kenward 			channel->irq_moderation_us += step;
324539de7c5SBert Kenward 			efx->type->push_irq_moderation(channel);
325539de7c5SBert Kenward 		}
326539de7c5SBert Kenward 	}
327539de7c5SBert Kenward 
328539de7c5SBert Kenward 	channel->irq_count = 0;
329539de7c5SBert Kenward 	channel->irq_mod_score = 0;
330539de7c5SBert Kenward }
331539de7c5SBert Kenward 
332874aeea5SJeff Kirsher static int efx_poll(struct napi_struct *napi, int budget)
333874aeea5SJeff Kirsher {
334874aeea5SJeff Kirsher 	struct efx_channel *channel =
335874aeea5SJeff Kirsher 		container_of(napi, struct efx_channel, napi_str);
336874aeea5SJeff Kirsher 	struct efx_nic *efx = channel->efx;
337874aeea5SJeff Kirsher 	int spent;
338874aeea5SJeff Kirsher 
339874aeea5SJeff Kirsher 	netif_vdbg(efx, intr, efx->net_dev,
340874aeea5SJeff Kirsher 		   "channel %d NAPI poll executing on CPU %d\n",
341874aeea5SJeff Kirsher 		   channel->channel, raw_smp_processor_id());
342874aeea5SJeff Kirsher 
343874aeea5SJeff Kirsher 	spent = efx_process_channel(channel, budget);
344874aeea5SJeff Kirsher 
345eb9a36beSCharles McLachlan 	xdp_do_flush_map();
346eb9a36beSCharles McLachlan 
347874aeea5SJeff Kirsher 	if (spent < budget) {
3489d9a6973SBen Hutchings 		if (efx_channel_has_rx_queue(channel) &&
349874aeea5SJeff Kirsher 		    efx->irq_rx_adaptive &&
350874aeea5SJeff Kirsher 		    unlikely(++channel->irq_count == 1000)) {
351539de7c5SBert Kenward 			efx_update_irq_mod(efx, channel);
352874aeea5SJeff Kirsher 		}
353874aeea5SJeff Kirsher 
3543af0f342SEdward Cree #ifdef CONFIG_RFS_ACCEL
3553af0f342SEdward Cree 		/* Perhaps expire some ARFS filters */
3563af0f342SEdward Cree 		schedule_work(&channel->filter_work);
3573af0f342SEdward Cree #endif
358874aeea5SJeff Kirsher 
359874aeea5SJeff Kirsher 		/* There is no race here; although napi_disable() will
360874aeea5SJeff Kirsher 		 * only wait for napi_complete(), this isn't a problem
361514bedbcSBen Hutchings 		 * since efx_nic_eventq_read_ack() will have no effect if
362874aeea5SJeff Kirsher 		 * interrupts have already been disabled.
363874aeea5SJeff Kirsher 		 */
364f820c0acSBert Kenward 		if (napi_complete_done(napi, spent))
365514bedbcSBen Hutchings 			efx_nic_eventq_read_ack(channel);
366874aeea5SJeff Kirsher 	}
367874aeea5SJeff Kirsher 
368874aeea5SJeff Kirsher 	return spent;
369874aeea5SJeff Kirsher }
370874aeea5SJeff Kirsher 
371874aeea5SJeff Kirsher /* Create event queue
372874aeea5SJeff Kirsher  * Event queue memory allocations are done only once.  If the channel
373874aeea5SJeff Kirsher  * is reset, the memory buffer will be reused; this guards against
374874aeea5SJeff Kirsher  * errors during channel reset and also simplifies interrupt handling.
375874aeea5SJeff Kirsher  */
376874aeea5SJeff Kirsher static int efx_probe_eventq(struct efx_channel *channel)
377874aeea5SJeff Kirsher {
378874aeea5SJeff Kirsher 	struct efx_nic *efx = channel->efx;
379874aeea5SJeff Kirsher 	unsigned long entries;
380874aeea5SJeff Kirsher 
38186ee5302SBen Hutchings 	netif_dbg(efx, probe, efx->net_dev,
382874aeea5SJeff Kirsher 		  "chan %d create event queue\n", channel->channel);
383874aeea5SJeff Kirsher 
384874aeea5SJeff Kirsher 	/* Build an event queue with room for one event per tx and rx buffer,
385874aeea5SJeff Kirsher 	 * plus some extra for link state events and MCDI completions. */
386874aeea5SJeff Kirsher 	entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
387e01b16a7SEdward Cree 	EFX_WARN_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
388874aeea5SJeff Kirsher 	channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
389874aeea5SJeff Kirsher 
390874aeea5SJeff Kirsher 	return efx_nic_probe_eventq(channel);
391874aeea5SJeff Kirsher }
392874aeea5SJeff Kirsher 
393874aeea5SJeff Kirsher /* Prepare channel's event queue */
394261e4d96SJon Cooper static int efx_init_eventq(struct efx_channel *channel)
395874aeea5SJeff Kirsher {
39615acb1ceSBen Hutchings 	struct efx_nic *efx = channel->efx;
397261e4d96SJon Cooper 	int rc;
398261e4d96SJon Cooper 
399261e4d96SJon Cooper 	EFX_WARN_ON_PARANOID(channel->eventq_init);
400261e4d96SJon Cooper 
40115acb1ceSBen Hutchings 	netif_dbg(efx, drv, efx->net_dev,
402874aeea5SJeff Kirsher 		  "chan %d init event queue\n", channel->channel);
403874aeea5SJeff Kirsher 
404261e4d96SJon Cooper 	rc = efx_nic_init_eventq(channel);
405261e4d96SJon Cooper 	if (rc == 0) {
40615acb1ceSBen Hutchings 		efx->type->push_irq_moderation(channel);
407874aeea5SJeff Kirsher 		channel->eventq_read_ptr = 0;
408be3fc09cSBen Hutchings 		channel->eventq_init = true;
409874aeea5SJeff Kirsher 	}
410261e4d96SJon Cooper 	return rc;
411261e4d96SJon Cooper }
412874aeea5SJeff Kirsher 
4139f2cb71cSBen Hutchings /* Enable event queue processing and NAPI */
41436763266SAlexandre Rames void efx_start_eventq(struct efx_channel *channel)
4159f2cb71cSBen Hutchings {
4169f2cb71cSBen Hutchings 	netif_dbg(channel->efx, ifup, channel->efx->net_dev,
4179f2cb71cSBen Hutchings 		  "chan %d start event queue\n", channel->channel);
4189f2cb71cSBen Hutchings 
419514bedbcSBen Hutchings 	/* Make sure the NAPI handler sees the enabled flag set */
4209f2cb71cSBen Hutchings 	channel->enabled = true;
4219f2cb71cSBen Hutchings 	smp_wmb();
4229f2cb71cSBen Hutchings 
4239f2cb71cSBen Hutchings 	napi_enable(&channel->napi_str);
4249f2cb71cSBen Hutchings 	efx_nic_eventq_read_ack(channel);
4259f2cb71cSBen Hutchings }
4269f2cb71cSBen Hutchings 
4279f2cb71cSBen Hutchings /* Disable event queue processing and NAPI */
42836763266SAlexandre Rames void efx_stop_eventq(struct efx_channel *channel)
4299f2cb71cSBen Hutchings {
4309f2cb71cSBen Hutchings 	if (!channel->enabled)
4319f2cb71cSBen Hutchings 		return;
4329f2cb71cSBen Hutchings 
4339f2cb71cSBen Hutchings 	napi_disable(&channel->napi_str);
4349f2cb71cSBen Hutchings 	channel->enabled = false;
4359f2cb71cSBen Hutchings }
4369f2cb71cSBen Hutchings 
437874aeea5SJeff Kirsher static void efx_fini_eventq(struct efx_channel *channel)
438874aeea5SJeff Kirsher {
439be3fc09cSBen Hutchings 	if (!channel->eventq_init)
440be3fc09cSBen Hutchings 		return;
441be3fc09cSBen Hutchings 
442874aeea5SJeff Kirsher 	netif_dbg(channel->efx, drv, channel->efx->net_dev,
443874aeea5SJeff Kirsher 		  "chan %d fini event queue\n", channel->channel);
444874aeea5SJeff Kirsher 
445874aeea5SJeff Kirsher 	efx_nic_fini_eventq(channel);
446be3fc09cSBen Hutchings 	channel->eventq_init = false;
447874aeea5SJeff Kirsher }
448874aeea5SJeff Kirsher 
449874aeea5SJeff Kirsher static void efx_remove_eventq(struct efx_channel *channel)
450874aeea5SJeff Kirsher {
451874aeea5SJeff Kirsher 	netif_dbg(channel->efx, drv, channel->efx->net_dev,
452874aeea5SJeff Kirsher 		  "chan %d remove event queue\n", channel->channel);
453874aeea5SJeff Kirsher 
454874aeea5SJeff Kirsher 	efx_nic_remove_eventq(channel);
455874aeea5SJeff Kirsher }
456874aeea5SJeff Kirsher 
457874aeea5SJeff Kirsher /**************************************************************************
458874aeea5SJeff Kirsher  *
459874aeea5SJeff Kirsher  * Channel handling
460874aeea5SJeff Kirsher  *
461874aeea5SJeff Kirsher  *************************************************************************/
462874aeea5SJeff Kirsher 
4637f967c01SBen Hutchings /* Allocate and initialise a channel structure. */
464874aeea5SJeff Kirsher static struct efx_channel *
465874aeea5SJeff Kirsher efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
466874aeea5SJeff Kirsher {
467874aeea5SJeff Kirsher 	struct efx_channel *channel;
468874aeea5SJeff Kirsher 	struct efx_rx_queue *rx_queue;
469874aeea5SJeff Kirsher 	struct efx_tx_queue *tx_queue;
470874aeea5SJeff Kirsher 	int j;
471874aeea5SJeff Kirsher 
4727f967c01SBen Hutchings 	channel = kzalloc(sizeof(*channel), GFP_KERNEL);
4737f967c01SBen Hutchings 	if (!channel)
4747f967c01SBen Hutchings 		return NULL;
4757f967c01SBen Hutchings 
4767f967c01SBen Hutchings 	channel->efx = efx;
4777f967c01SBen Hutchings 	channel->channel = i;
4787f967c01SBen Hutchings 	channel->type = &efx_default_channel_type;
4797f967c01SBen Hutchings 
4807f967c01SBen Hutchings 	for (j = 0; j < EFX_TXQ_TYPES; j++) {
4817f967c01SBen Hutchings 		tx_queue = &channel->tx_queue[j];
4827f967c01SBen Hutchings 		tx_queue->efx = efx;
4837f967c01SBen Hutchings 		tx_queue->queue = i * EFX_TXQ_TYPES + j;
4847f967c01SBen Hutchings 		tx_queue->channel = channel;
4857f967c01SBen Hutchings 	}
4867f967c01SBen Hutchings 
4873af0f342SEdward Cree #ifdef CONFIG_RFS_ACCEL
4883af0f342SEdward Cree 	INIT_WORK(&channel->filter_work, efx_filter_rfs_expire);
4893af0f342SEdward Cree #endif
4903af0f342SEdward Cree 
4917f967c01SBen Hutchings 	rx_queue = &channel->rx_queue;
4927f967c01SBen Hutchings 	rx_queue->efx = efx;
4937aa1402eSKees Cook 	timer_setup(&rx_queue->slow_fill, efx_rx_slow_fill, 0);
4947f967c01SBen Hutchings 
4957f967c01SBen Hutchings 	return channel;
4967f967c01SBen Hutchings }
4977f967c01SBen Hutchings 
4987f967c01SBen Hutchings /* Allocate and initialise a channel structure, copying parameters
4997f967c01SBen Hutchings  * (but not resources) from an old channel structure.
5007f967c01SBen Hutchings  */
5017f967c01SBen Hutchings static struct efx_channel *
5027f967c01SBen Hutchings efx_copy_channel(const struct efx_channel *old_channel)
5037f967c01SBen Hutchings {
5047f967c01SBen Hutchings 	struct efx_channel *channel;
5057f967c01SBen Hutchings 	struct efx_rx_queue *rx_queue;
5067f967c01SBen Hutchings 	struct efx_tx_queue *tx_queue;
5077f967c01SBen Hutchings 	int j;
5087f967c01SBen Hutchings 
509874aeea5SJeff Kirsher 	channel = kmalloc(sizeof(*channel), GFP_KERNEL);
510874aeea5SJeff Kirsher 	if (!channel)
511874aeea5SJeff Kirsher 		return NULL;
512874aeea5SJeff Kirsher 
513874aeea5SJeff Kirsher 	*channel = *old_channel;
514874aeea5SJeff Kirsher 
515874aeea5SJeff Kirsher 	channel->napi_dev = NULL;
51646d054f8SBert Kenward 	INIT_HLIST_NODE(&channel->napi_str.napi_hash_node);
51746d054f8SBert Kenward 	channel->napi_str.napi_id = 0;
51846d054f8SBert Kenward 	channel->napi_str.state = 0;
519874aeea5SJeff Kirsher 	memset(&channel->eventq, 0, sizeof(channel->eventq));
520874aeea5SJeff Kirsher 
521874aeea5SJeff Kirsher 	for (j = 0; j < EFX_TXQ_TYPES; j++) {
522874aeea5SJeff Kirsher 		tx_queue = &channel->tx_queue[j];
523874aeea5SJeff Kirsher 		if (tx_queue->channel)
524874aeea5SJeff Kirsher 			tx_queue->channel = channel;
525874aeea5SJeff Kirsher 		tx_queue->buffer = NULL;
526874aeea5SJeff Kirsher 		memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
527874aeea5SJeff Kirsher 	}
528874aeea5SJeff Kirsher 
529874aeea5SJeff Kirsher 	rx_queue = &channel->rx_queue;
5307f967c01SBen Hutchings 	rx_queue->buffer = NULL;
5317f967c01SBen Hutchings 	memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
5327aa1402eSKees Cook 	timer_setup(&rx_queue->slow_fill, efx_rx_slow_fill, 0);
5333af0f342SEdward Cree #ifdef CONFIG_RFS_ACCEL
5343af0f342SEdward Cree 	INIT_WORK(&channel->filter_work, efx_filter_rfs_expire);
5353af0f342SEdward Cree #endif
536874aeea5SJeff Kirsher 
537874aeea5SJeff Kirsher 	return channel;
538874aeea5SJeff Kirsher }
539874aeea5SJeff Kirsher 
540874aeea5SJeff Kirsher static int efx_probe_channel(struct efx_channel *channel)
541874aeea5SJeff Kirsher {
542874aeea5SJeff Kirsher 	struct efx_tx_queue *tx_queue;
543874aeea5SJeff Kirsher 	struct efx_rx_queue *rx_queue;
544874aeea5SJeff Kirsher 	int rc;
545874aeea5SJeff Kirsher 
546874aeea5SJeff Kirsher 	netif_dbg(channel->efx, probe, channel->efx->net_dev,
547874aeea5SJeff Kirsher 		  "creating channel %d\n", channel->channel);
548874aeea5SJeff Kirsher 
5497f967c01SBen Hutchings 	rc = channel->type->pre_probe(channel);
5507f967c01SBen Hutchings 	if (rc)
5517f967c01SBen Hutchings 		goto fail;
5527f967c01SBen Hutchings 
553874aeea5SJeff Kirsher 	rc = efx_probe_eventq(channel);
554874aeea5SJeff Kirsher 	if (rc)
5557f967c01SBen Hutchings 		goto fail;
556874aeea5SJeff Kirsher 
557874aeea5SJeff Kirsher 	efx_for_each_channel_tx_queue(tx_queue, channel) {
558874aeea5SJeff Kirsher 		rc = efx_probe_tx_queue(tx_queue);
559874aeea5SJeff Kirsher 		if (rc)
5607f967c01SBen Hutchings 			goto fail;
561874aeea5SJeff Kirsher 	}
562874aeea5SJeff Kirsher 
563874aeea5SJeff Kirsher 	efx_for_each_channel_rx_queue(rx_queue, channel) {
564874aeea5SJeff Kirsher 		rc = efx_probe_rx_queue(rx_queue);
565874aeea5SJeff Kirsher 		if (rc)
5667f967c01SBen Hutchings 			goto fail;
567874aeea5SJeff Kirsher 	}
568874aeea5SJeff Kirsher 
569e090bfb9SEdward Cree 	channel->rx_list = NULL;
570e090bfb9SEdward Cree 
571874aeea5SJeff Kirsher 	return 0;
572874aeea5SJeff Kirsher 
5737f967c01SBen Hutchings fail:
5747f967c01SBen Hutchings 	efx_remove_channel(channel);
575874aeea5SJeff Kirsher 	return rc;
576874aeea5SJeff Kirsher }
577874aeea5SJeff Kirsher 
5787f967c01SBen Hutchings static void
5797f967c01SBen Hutchings efx_get_channel_name(struct efx_channel *channel, char *buf, size_t len)
5807f967c01SBen Hutchings {
5817f967c01SBen Hutchings 	struct efx_nic *efx = channel->efx;
5827f967c01SBen Hutchings 	const char *type;
5837f967c01SBen Hutchings 	int number;
5847f967c01SBen Hutchings 
5857f967c01SBen Hutchings 	number = channel->channel;
5867f967c01SBen Hutchings 	if (efx->tx_channel_offset == 0) {
5877f967c01SBen Hutchings 		type = "";
5887f967c01SBen Hutchings 	} else if (channel->channel < efx->tx_channel_offset) {
5897f967c01SBen Hutchings 		type = "-rx";
5907f967c01SBen Hutchings 	} else {
5917f967c01SBen Hutchings 		type = "-tx";
5927f967c01SBen Hutchings 		number -= efx->tx_channel_offset;
5937f967c01SBen Hutchings 	}
5947f967c01SBen Hutchings 	snprintf(buf, len, "%s%s-%d", efx->name, type, number);
5957f967c01SBen Hutchings }
596874aeea5SJeff Kirsher 
597874aeea5SJeff Kirsher static void efx_set_channel_names(struct efx_nic *efx)
598874aeea5SJeff Kirsher {
599874aeea5SJeff Kirsher 	struct efx_channel *channel;
600874aeea5SJeff Kirsher 
6017f967c01SBen Hutchings 	efx_for_each_channel(channel, efx)
6027f967c01SBen Hutchings 		channel->type->get_name(channel,
603d8291187SBen Hutchings 					efx->msi_context[channel->channel].name,
604d8291187SBen Hutchings 					sizeof(efx->msi_context[0].name));
605874aeea5SJeff Kirsher }
606874aeea5SJeff Kirsher 
607874aeea5SJeff Kirsher static int efx_probe_channels(struct efx_nic *efx)
608874aeea5SJeff Kirsher {
609874aeea5SJeff Kirsher 	struct efx_channel *channel;
610874aeea5SJeff Kirsher 	int rc;
611874aeea5SJeff Kirsher 
612874aeea5SJeff Kirsher 	/* Restart special buffer allocation */
613874aeea5SJeff Kirsher 	efx->next_buffer_table = 0;
614874aeea5SJeff Kirsher 
615c92aaff1SBen Hutchings 	/* Probe channels in reverse, so that any 'extra' channels
616c92aaff1SBen Hutchings 	 * use the start of the buffer table. This allows the traffic
617c92aaff1SBen Hutchings 	 * channels to be resized without moving them or wasting the
618c92aaff1SBen Hutchings 	 * entries before them.
619c92aaff1SBen Hutchings 	 */
620c92aaff1SBen Hutchings 	efx_for_each_channel_rev(channel, efx) {
621874aeea5SJeff Kirsher 		rc = efx_probe_channel(channel);
622874aeea5SJeff Kirsher 		if (rc) {
623874aeea5SJeff Kirsher 			netif_err(efx, probe, efx->net_dev,
624874aeea5SJeff Kirsher 				  "failed to create channel %d\n",
625874aeea5SJeff Kirsher 				  channel->channel);
626874aeea5SJeff Kirsher 			goto fail;
627874aeea5SJeff Kirsher 		}
628874aeea5SJeff Kirsher 	}
629874aeea5SJeff Kirsher 	efx_set_channel_names(efx);
630874aeea5SJeff Kirsher 
631874aeea5SJeff Kirsher 	return 0;
632874aeea5SJeff Kirsher 
633874aeea5SJeff Kirsher fail:
634874aeea5SJeff Kirsher 	efx_remove_channels(efx);
635874aeea5SJeff Kirsher 	return rc;
636874aeea5SJeff Kirsher }
637874aeea5SJeff Kirsher 
638874aeea5SJeff Kirsher /* Channels are shutdown and reinitialised whilst the NIC is running
639874aeea5SJeff Kirsher  * to propagate configuration changes (mtu, checksum offload), or
640874aeea5SJeff Kirsher  * to clear hardware error conditions
641874aeea5SJeff Kirsher  */
6429f2cb71cSBen Hutchings static void efx_start_datapath(struct efx_nic *efx)
643874aeea5SJeff Kirsher {
644ebfcd0fdSAndrew Rybchenko 	netdev_features_t old_features = efx->net_dev->features;
64585740cdfSBen Hutchings 	bool old_rx_scatter = efx->rx_scatter;
646874aeea5SJeff Kirsher 	struct efx_tx_queue *tx_queue;
647874aeea5SJeff Kirsher 	struct efx_rx_queue *rx_queue;
648874aeea5SJeff Kirsher 	struct efx_channel *channel;
64985740cdfSBen Hutchings 	size_t rx_buf_len;
650874aeea5SJeff Kirsher 
651874aeea5SJeff Kirsher 	/* Calculate the rx buffer allocation parameters required to
652874aeea5SJeff Kirsher 	 * support the current MTU, including padding for header
653874aeea5SJeff Kirsher 	 * alignment and overruns.
654874aeea5SJeff Kirsher 	 */
65543a3739dSJon Cooper 	efx->rx_dma_len = (efx->rx_prefix_size +
656874aeea5SJeff Kirsher 			   EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
657874aeea5SJeff Kirsher 			   efx->type->rx_buffer_padding);
658eb9a36beSCharles McLachlan 	rx_buf_len = (sizeof(struct efx_rx_page_state) + XDP_PACKET_HEADROOM +
6592ec03014SAndrew Rybchenko 		      efx->rx_ip_align + efx->rx_dma_len);
66085740cdfSBen Hutchings 	if (rx_buf_len <= PAGE_SIZE) {
661e8c68c0aSJon Cooper 		efx->rx_scatter = efx->type->always_rx_scatter;
66285740cdfSBen Hutchings 		efx->rx_buffer_order = 0;
66385740cdfSBen Hutchings 	} else if (efx->type->can_rx_scatter) {
664950c54dfSBen Hutchings 		BUILD_BUG_ON(EFX_RX_USR_BUF_SIZE % L1_CACHE_BYTES);
66585740cdfSBen Hutchings 		BUILD_BUG_ON(sizeof(struct efx_rx_page_state) +
666950c54dfSBen Hutchings 			     2 * ALIGN(NET_IP_ALIGN + EFX_RX_USR_BUF_SIZE,
667950c54dfSBen Hutchings 				       EFX_RX_BUF_ALIGNMENT) >
668950c54dfSBen Hutchings 			     PAGE_SIZE);
66985740cdfSBen Hutchings 		efx->rx_scatter = true;
67085740cdfSBen Hutchings 		efx->rx_dma_len = EFX_RX_USR_BUF_SIZE;
67185740cdfSBen Hutchings 		efx->rx_buffer_order = 0;
67285740cdfSBen Hutchings 	} else {
67385740cdfSBen Hutchings 		efx->rx_scatter = false;
67485740cdfSBen Hutchings 		efx->rx_buffer_order = get_order(rx_buf_len);
67585740cdfSBen Hutchings 	}
67685740cdfSBen Hutchings 
6771648a23fSDaniel Pieczko 	efx_rx_config_page_split(efx);
6781648a23fSDaniel Pieczko 	if (efx->rx_buffer_order)
6791648a23fSDaniel Pieczko 		netif_dbg(efx, drv, efx->net_dev,
6801648a23fSDaniel Pieczko 			  "RX buf len=%u; page order=%u batch=%u\n",
6811648a23fSDaniel Pieczko 			  efx->rx_dma_len, efx->rx_buffer_order,
6821648a23fSDaniel Pieczko 			  efx->rx_pages_per_batch);
6831648a23fSDaniel Pieczko 	else
6841648a23fSDaniel Pieczko 		netif_dbg(efx, drv, efx->net_dev,
6851648a23fSDaniel Pieczko 			  "RX buf len=%u step=%u bpp=%u; page batch=%u\n",
6861648a23fSDaniel Pieczko 			  efx->rx_dma_len, efx->rx_page_buf_step,
6871648a23fSDaniel Pieczko 			  efx->rx_bufs_per_page, efx->rx_pages_per_batch);
6882768935aSDaniel Pieczko 
689ebfcd0fdSAndrew Rybchenko 	/* Restore previously fixed features in hw_features and remove
690ebfcd0fdSAndrew Rybchenko 	 * features which are fixed now
691ebfcd0fdSAndrew Rybchenko 	 */
692ebfcd0fdSAndrew Rybchenko 	efx->net_dev->hw_features |= efx->net_dev->features;
693ebfcd0fdSAndrew Rybchenko 	efx->net_dev->hw_features &= ~efx->fixed_features;
694ebfcd0fdSAndrew Rybchenko 	efx->net_dev->features |= efx->fixed_features;
695ebfcd0fdSAndrew Rybchenko 	if (efx->net_dev->features != old_features)
696ebfcd0fdSAndrew Rybchenko 		netdev_features_change(efx->net_dev);
697ebfcd0fdSAndrew Rybchenko 
698e8c68c0aSJon Cooper 	/* RX filters may also have scatter-enabled flags */
69985740cdfSBen Hutchings 	if (efx->rx_scatter != old_rx_scatter)
700add72477SBen Hutchings 		efx->type->filter_update_rx_scatter(efx);
701874aeea5SJeff Kirsher 
70214bf718fSBen Hutchings 	/* We must keep at least one descriptor in a TX ring empty.
70314bf718fSBen Hutchings 	 * We could avoid this when the queue size does not exactly
70414bf718fSBen Hutchings 	 * match the hardware ring size, but it's not that important.
70514bf718fSBen Hutchings 	 * Therefore we stop the queue when one more skb might fill
70614bf718fSBen Hutchings 	 * the ring completely.  We wake it when half way back to
70714bf718fSBen Hutchings 	 * empty.
70814bf718fSBen Hutchings 	 */
70914bf718fSBen Hutchings 	efx->txq_stop_thresh = efx->txq_entries - efx_tx_max_skb_descs(efx);
71014bf718fSBen Hutchings 	efx->txq_wake_thresh = efx->txq_stop_thresh / 2;
71114bf718fSBen Hutchings 
712874aeea5SJeff Kirsher 	/* Initialise the channels */
713874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx) {
7143881d8abSAlexandre Rames 		efx_for_each_channel_tx_queue(tx_queue, channel) {
715874aeea5SJeff Kirsher 			efx_init_tx_queue(tx_queue);
7163881d8abSAlexandre Rames 			atomic_inc(&efx->active_queues);
7173881d8abSAlexandre Rames 		}
718874aeea5SJeff Kirsher 
7199f2cb71cSBen Hutchings 		efx_for_each_channel_rx_queue(rx_queue, channel) {
720874aeea5SJeff Kirsher 			efx_init_rx_queue(rx_queue);
7213881d8abSAlexandre Rames 			atomic_inc(&efx->active_queues);
722cce28794SJon Cooper 			efx_stop_eventq(channel);
723cce28794SJon Cooper 			efx_fast_push_rx_descriptors(rx_queue, false);
724cce28794SJon Cooper 			efx_start_eventq(channel);
7259f2cb71cSBen Hutchings 		}
726874aeea5SJeff Kirsher 
72785740cdfSBen Hutchings 		WARN_ON(channel->rx_pkt_n_frags);
728874aeea5SJeff Kirsher 	}
7299f2cb71cSBen Hutchings 
7302ea4dc28SAlexandre Rames 	efx_ptp_start_datapath(efx);
7312ea4dc28SAlexandre Rames 
7329f2cb71cSBen Hutchings 	if (netif_device_present(efx->net_dev))
7339f2cb71cSBen Hutchings 		netif_tx_wake_all_queues(efx->net_dev);
734874aeea5SJeff Kirsher }
735874aeea5SJeff Kirsher 
7369f2cb71cSBen Hutchings static void efx_stop_datapath(struct efx_nic *efx)
737874aeea5SJeff Kirsher {
738874aeea5SJeff Kirsher 	struct efx_channel *channel;
739874aeea5SJeff Kirsher 	struct efx_tx_queue *tx_queue;
740874aeea5SJeff Kirsher 	struct efx_rx_queue *rx_queue;
741874aeea5SJeff Kirsher 	int rc;
742874aeea5SJeff Kirsher 
743874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
744874aeea5SJeff Kirsher 	BUG_ON(efx->port_enabled);
745874aeea5SJeff Kirsher 
7462ea4dc28SAlexandre Rames 	efx_ptp_stop_datapath(efx);
7472ea4dc28SAlexandre Rames 
748d8aec745SBen Hutchings 	/* Stop RX refill */
749d8aec745SBen Hutchings 	efx_for_each_channel(channel, efx) {
750d8aec745SBen Hutchings 		efx_for_each_channel_rx_queue(rx_queue, channel)
751d8aec745SBen Hutchings 			rx_queue->refill_enabled = false;
752d8aec745SBen Hutchings 	}
753d8aec745SBen Hutchings 
754874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx) {
7559f2cb71cSBen Hutchings 		/* RX packet processing is pipelined, so wait for the
7569f2cb71cSBen Hutchings 		 * NAPI handler to complete.  At least event queue 0
7579f2cb71cSBen Hutchings 		 * might be kept active by non-data events, so don't
7589f2cb71cSBen Hutchings 		 * use napi_synchronize() but actually disable NAPI
7599f2cb71cSBen Hutchings 		 * temporarily.
7609f2cb71cSBen Hutchings 		 */
7619f2cb71cSBen Hutchings 		if (efx_channel_has_rx_queue(channel)) {
7629f2cb71cSBen Hutchings 			efx_stop_eventq(channel);
7639f2cb71cSBen Hutchings 			efx_start_eventq(channel);
7649f2cb71cSBen Hutchings 		}
765e42c3d85SBen Hutchings 	}
766874aeea5SJeff Kirsher 
767e42c3d85SBen Hutchings 	rc = efx->type->fini_dmaq(efx);
7685a6681e2SEdward Cree 	if (rc) {
769e42c3d85SBen Hutchings 		netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
770e42c3d85SBen Hutchings 	} else {
771e42c3d85SBen Hutchings 		netif_dbg(efx, drv, efx->net_dev,
772e42c3d85SBen Hutchings 			  "successfully flushed all queues\n");
773e42c3d85SBen Hutchings 	}
774e42c3d85SBen Hutchings 
775e42c3d85SBen Hutchings 	efx_for_each_channel(channel, efx) {
776874aeea5SJeff Kirsher 		efx_for_each_channel_rx_queue(rx_queue, channel)
777874aeea5SJeff Kirsher 			efx_fini_rx_queue(rx_queue);
778874aeea5SJeff Kirsher 		efx_for_each_possible_channel_tx_queue(tx_queue, channel)
779874aeea5SJeff Kirsher 			efx_fini_tx_queue(tx_queue);
780874aeea5SJeff Kirsher 	}
781eb9a36beSCharles McLachlan 	efx->xdp_rxq_info_failed = false;
782874aeea5SJeff Kirsher }
783874aeea5SJeff Kirsher 
784874aeea5SJeff Kirsher static void efx_remove_channel(struct efx_channel *channel)
785874aeea5SJeff Kirsher {
786874aeea5SJeff Kirsher 	struct efx_tx_queue *tx_queue;
787874aeea5SJeff Kirsher 	struct efx_rx_queue *rx_queue;
788874aeea5SJeff Kirsher 
789874aeea5SJeff Kirsher 	netif_dbg(channel->efx, drv, channel->efx->net_dev,
790874aeea5SJeff Kirsher 		  "destroy chan %d\n", channel->channel);
791874aeea5SJeff Kirsher 
792874aeea5SJeff Kirsher 	efx_for_each_channel_rx_queue(rx_queue, channel)
793874aeea5SJeff Kirsher 		efx_remove_rx_queue(rx_queue);
794874aeea5SJeff Kirsher 	efx_for_each_possible_channel_tx_queue(tx_queue, channel)
795874aeea5SJeff Kirsher 		efx_remove_tx_queue(tx_queue);
796874aeea5SJeff Kirsher 	efx_remove_eventq(channel);
797c31e5f9fSStuart Hodgson 	channel->type->post_remove(channel);
798874aeea5SJeff Kirsher }
799874aeea5SJeff Kirsher 
800874aeea5SJeff Kirsher static void efx_remove_channels(struct efx_nic *efx)
801874aeea5SJeff Kirsher {
802874aeea5SJeff Kirsher 	struct efx_channel *channel;
803874aeea5SJeff Kirsher 
804874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx)
805874aeea5SJeff Kirsher 		efx_remove_channel(channel);
806874aeea5SJeff Kirsher }
807874aeea5SJeff Kirsher 
808874aeea5SJeff Kirsher int
809874aeea5SJeff Kirsher efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
810874aeea5SJeff Kirsher {
811874aeea5SJeff Kirsher 	struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
812874aeea5SJeff Kirsher 	u32 old_rxq_entries, old_txq_entries;
8137f967c01SBen Hutchings 	unsigned i, next_buffer_table = 0;
814261e4d96SJon Cooper 	int rc, rc2;
8158b7325b4SBen Hutchings 
8168b7325b4SBen Hutchings 	rc = efx_check_disabled(efx);
8178b7325b4SBen Hutchings 	if (rc)
8188b7325b4SBen Hutchings 		return rc;
8197f967c01SBen Hutchings 
8207f967c01SBen Hutchings 	/* Not all channels should be reallocated. We must avoid
8217f967c01SBen Hutchings 	 * reallocating their buffer table entries.
8227f967c01SBen Hutchings 	 */
8237f967c01SBen Hutchings 	efx_for_each_channel(channel, efx) {
8247f967c01SBen Hutchings 		struct efx_rx_queue *rx_queue;
8257f967c01SBen Hutchings 		struct efx_tx_queue *tx_queue;
8267f967c01SBen Hutchings 
8277f967c01SBen Hutchings 		if (channel->type->copy)
8287f967c01SBen Hutchings 			continue;
8297f967c01SBen Hutchings 		next_buffer_table = max(next_buffer_table,
8307f967c01SBen Hutchings 					channel->eventq.index +
8317f967c01SBen Hutchings 					channel->eventq.entries);
8327f967c01SBen Hutchings 		efx_for_each_channel_rx_queue(rx_queue, channel)
8337f967c01SBen Hutchings 			next_buffer_table = max(next_buffer_table,
8347f967c01SBen Hutchings 						rx_queue->rxd.index +
8357f967c01SBen Hutchings 						rx_queue->rxd.entries);
8367f967c01SBen Hutchings 		efx_for_each_channel_tx_queue(tx_queue, channel)
8377f967c01SBen Hutchings 			next_buffer_table = max(next_buffer_table,
8387f967c01SBen Hutchings 						tx_queue->txd.index +
8397f967c01SBen Hutchings 						tx_queue->txd.entries);
8407f967c01SBen Hutchings 	}
841874aeea5SJeff Kirsher 
84229c69a48SBen Hutchings 	efx_device_detach_sync(efx);
843874aeea5SJeff Kirsher 	efx_stop_all(efx);
844d8291187SBen Hutchings 	efx_soft_disable_interrupts(efx);
845874aeea5SJeff Kirsher 
8467f967c01SBen Hutchings 	/* Clone channels (where possible) */
847874aeea5SJeff Kirsher 	memset(other_channel, 0, sizeof(other_channel));
848874aeea5SJeff Kirsher 	for (i = 0; i < efx->n_channels; i++) {
8497f967c01SBen Hutchings 		channel = efx->channel[i];
8507f967c01SBen Hutchings 		if (channel->type->copy)
8517f967c01SBen Hutchings 			channel = channel->type->copy(channel);
852874aeea5SJeff Kirsher 		if (!channel) {
853874aeea5SJeff Kirsher 			rc = -ENOMEM;
854874aeea5SJeff Kirsher 			goto out;
855874aeea5SJeff Kirsher 		}
856874aeea5SJeff Kirsher 		other_channel[i] = channel;
857874aeea5SJeff Kirsher 	}
858874aeea5SJeff Kirsher 
859874aeea5SJeff Kirsher 	/* Swap entry counts and channel pointers */
860874aeea5SJeff Kirsher 	old_rxq_entries = efx->rxq_entries;
861874aeea5SJeff Kirsher 	old_txq_entries = efx->txq_entries;
862874aeea5SJeff Kirsher 	efx->rxq_entries = rxq_entries;
863874aeea5SJeff Kirsher 	efx->txq_entries = txq_entries;
864874aeea5SJeff Kirsher 	for (i = 0; i < efx->n_channels; i++) {
865874aeea5SJeff Kirsher 		channel = efx->channel[i];
866874aeea5SJeff Kirsher 		efx->channel[i] = other_channel[i];
867874aeea5SJeff Kirsher 		other_channel[i] = channel;
868874aeea5SJeff Kirsher 	}
869874aeea5SJeff Kirsher 
8707f967c01SBen Hutchings 	/* Restart buffer table allocation */
8717f967c01SBen Hutchings 	efx->next_buffer_table = next_buffer_table;
8727f967c01SBen Hutchings 
8737f967c01SBen Hutchings 	for (i = 0; i < efx->n_channels; i++) {
8747f967c01SBen Hutchings 		channel = efx->channel[i];
8757f967c01SBen Hutchings 		if (!channel->type->copy)
8767f967c01SBen Hutchings 			continue;
8777f967c01SBen Hutchings 		rc = efx_probe_channel(channel);
878874aeea5SJeff Kirsher 		if (rc)
879874aeea5SJeff Kirsher 			goto rollback;
8807f967c01SBen Hutchings 		efx_init_napi_channel(efx->channel[i]);
881874aeea5SJeff Kirsher 	}
882874aeea5SJeff Kirsher 
8837f967c01SBen Hutchings out:
8847f967c01SBen Hutchings 	/* Destroy unused channel structures */
8857f967c01SBen Hutchings 	for (i = 0; i < efx->n_channels; i++) {
8867f967c01SBen Hutchings 		channel = other_channel[i];
8877f967c01SBen Hutchings 		if (channel && channel->type->copy) {
8887f967c01SBen Hutchings 			efx_fini_napi_channel(channel);
8897f967c01SBen Hutchings 			efx_remove_channel(channel);
8907f967c01SBen Hutchings 			kfree(channel);
8917f967c01SBen Hutchings 		}
8927f967c01SBen Hutchings 	}
8937f967c01SBen Hutchings 
894261e4d96SJon Cooper 	rc2 = efx_soft_enable_interrupts(efx);
895261e4d96SJon Cooper 	if (rc2) {
896261e4d96SJon Cooper 		rc = rc ? rc : rc2;
897261e4d96SJon Cooper 		netif_err(efx, drv, efx->net_dev,
898261e4d96SJon Cooper 			  "unable to restart interrupts on channel reallocation\n");
899261e4d96SJon Cooper 		efx_schedule_reset(efx, RESET_TYPE_DISABLE);
900261e4d96SJon Cooper 	} else {
901874aeea5SJeff Kirsher 		efx_start_all(efx);
9029c568fd8SPeter Dunning 		efx_device_attach_if_not_resetting(efx);
903261e4d96SJon Cooper 	}
904874aeea5SJeff Kirsher 	return rc;
905874aeea5SJeff Kirsher 
906874aeea5SJeff Kirsher rollback:
907874aeea5SJeff Kirsher 	/* Swap back */
908874aeea5SJeff Kirsher 	efx->rxq_entries = old_rxq_entries;
909874aeea5SJeff Kirsher 	efx->txq_entries = old_txq_entries;
910874aeea5SJeff Kirsher 	for (i = 0; i < efx->n_channels; i++) {
911874aeea5SJeff Kirsher 		channel = efx->channel[i];
912874aeea5SJeff Kirsher 		efx->channel[i] = other_channel[i];
913874aeea5SJeff Kirsher 		other_channel[i] = channel;
914874aeea5SJeff Kirsher 	}
915874aeea5SJeff Kirsher 	goto out;
916874aeea5SJeff Kirsher }
917874aeea5SJeff Kirsher 
918874aeea5SJeff Kirsher void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
919874aeea5SJeff Kirsher {
92050f444aaSRobert Stonehouse 	mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(10));
921874aeea5SJeff Kirsher }
922874aeea5SJeff Kirsher 
923e7345ba3Skbuild test robot static bool efx_default_channel_want_txqs(struct efx_channel *channel)
9242935e3c3SEdward Cree {
9252935e3c3SEdward Cree 	return channel->channel - channel->efx->tx_channel_offset <
9262935e3c3SEdward Cree 		channel->efx->n_tx_channels;
9272935e3c3SEdward Cree }
9282935e3c3SEdward Cree 
9297f967c01SBen Hutchings static const struct efx_channel_type efx_default_channel_type = {
9307f967c01SBen Hutchings 	.pre_probe		= efx_channel_dummy_op_int,
931c31e5f9fSStuart Hodgson 	.post_remove		= efx_channel_dummy_op_void,
9327f967c01SBen Hutchings 	.get_name		= efx_get_channel_name,
9337f967c01SBen Hutchings 	.copy			= efx_copy_channel,
9342935e3c3SEdward Cree 	.want_txqs		= efx_default_channel_want_txqs,
9357f967c01SBen Hutchings 	.keep_eventq		= false,
9362935e3c3SEdward Cree 	.want_pio		= true,
9377f967c01SBen Hutchings };
9387f967c01SBen Hutchings 
9397f967c01SBen Hutchings int efx_channel_dummy_op_int(struct efx_channel *channel)
9407f967c01SBen Hutchings {
9417f967c01SBen Hutchings 	return 0;
9427f967c01SBen Hutchings }
9437f967c01SBen Hutchings 
944c31e5f9fSStuart Hodgson void efx_channel_dummy_op_void(struct efx_channel *channel)
945c31e5f9fSStuart Hodgson {
946c31e5f9fSStuart Hodgson }
947c31e5f9fSStuart Hodgson 
948874aeea5SJeff Kirsher /**************************************************************************
949874aeea5SJeff Kirsher  *
950874aeea5SJeff Kirsher  * Port handling
951874aeea5SJeff Kirsher  *
952874aeea5SJeff Kirsher  **************************************************************************/
953874aeea5SJeff Kirsher 
954874aeea5SJeff Kirsher /* This ensures that the kernel is kept informed (via
955874aeea5SJeff Kirsher  * netif_carrier_on/off) of the link status, and also maintains the
956874aeea5SJeff Kirsher  * link status's stop on the port's TX queue.
957874aeea5SJeff Kirsher  */
958874aeea5SJeff Kirsher void efx_link_status_changed(struct efx_nic *efx)
959874aeea5SJeff Kirsher {
960874aeea5SJeff Kirsher 	struct efx_link_state *link_state = &efx->link_state;
961874aeea5SJeff Kirsher 
962874aeea5SJeff Kirsher 	/* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
963874aeea5SJeff Kirsher 	 * that no events are triggered between unregister_netdev() and the
964874aeea5SJeff Kirsher 	 * driver unloading. A more general condition is that NETDEV_CHANGE
965874aeea5SJeff Kirsher 	 * can only be generated between NETDEV_UP and NETDEV_DOWN */
966874aeea5SJeff Kirsher 	if (!netif_running(efx->net_dev))
967874aeea5SJeff Kirsher 		return;
968874aeea5SJeff Kirsher 
969874aeea5SJeff Kirsher 	if (link_state->up != netif_carrier_ok(efx->net_dev)) {
970874aeea5SJeff Kirsher 		efx->n_link_state_changes++;
971874aeea5SJeff Kirsher 
972874aeea5SJeff Kirsher 		if (link_state->up)
973874aeea5SJeff Kirsher 			netif_carrier_on(efx->net_dev);
974874aeea5SJeff Kirsher 		else
975874aeea5SJeff Kirsher 			netif_carrier_off(efx->net_dev);
976874aeea5SJeff Kirsher 	}
977874aeea5SJeff Kirsher 
978874aeea5SJeff Kirsher 	/* Status message for kernel log */
9792aa9ef11SBen Hutchings 	if (link_state->up)
980874aeea5SJeff Kirsher 		netif_info(efx, link, efx->net_dev,
981964e6135SBen Hutchings 			   "link up at %uMbps %s-duplex (MTU %d)\n",
982874aeea5SJeff Kirsher 			   link_state->speed, link_state->fd ? "full" : "half",
983964e6135SBen Hutchings 			   efx->net_dev->mtu);
9842aa9ef11SBen Hutchings 	else
985874aeea5SJeff Kirsher 		netif_info(efx, link, efx->net_dev, "link down\n");
986874aeea5SJeff Kirsher }
987874aeea5SJeff Kirsher 
988c2ab85d2SEdward Cree void efx_link_set_advertising(struct efx_nic *efx,
989c2ab85d2SEdward Cree 			      const unsigned long *advertising)
990874aeea5SJeff Kirsher {
991c2ab85d2SEdward Cree 	memcpy(efx->link_advertising, advertising,
992c2ab85d2SEdward Cree 	       sizeof(__ETHTOOL_DECLARE_LINK_MODE_MASK()));
993c2ab85d2SEdward Cree 
994c2ab85d2SEdward Cree 	efx->link_advertising[0] |= ADVERTISED_Autoneg;
995c2ab85d2SEdward Cree 	if (advertising[0] & ADVERTISED_Pause)
996874aeea5SJeff Kirsher 		efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
997874aeea5SJeff Kirsher 	else
998874aeea5SJeff Kirsher 		efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
999c2ab85d2SEdward Cree 	if (advertising[0] & ADVERTISED_Asym_Pause)
1000874aeea5SJeff Kirsher 		efx->wanted_fc ^= EFX_FC_TX;
1001874aeea5SJeff Kirsher }
1002c2ab85d2SEdward Cree 
1003c2ab85d2SEdward Cree /* Equivalent to efx_link_set_advertising with all-zeroes, except does not
1004c2ab85d2SEdward Cree  * force the Autoneg bit on.
1005c2ab85d2SEdward Cree  */
1006c2ab85d2SEdward Cree void efx_link_clear_advertising(struct efx_nic *efx)
1007c2ab85d2SEdward Cree {
1008c2ab85d2SEdward Cree 	bitmap_zero(efx->link_advertising, __ETHTOOL_LINK_MODE_MASK_NBITS);
1009c2ab85d2SEdward Cree 	efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
1010874aeea5SJeff Kirsher }
1011874aeea5SJeff Kirsher 
1012874aeea5SJeff Kirsher void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
1013874aeea5SJeff Kirsher {
1014874aeea5SJeff Kirsher 	efx->wanted_fc = wanted_fc;
1015c2ab85d2SEdward Cree 	if (efx->link_advertising[0]) {
1016874aeea5SJeff Kirsher 		if (wanted_fc & EFX_FC_RX)
1017c2ab85d2SEdward Cree 			efx->link_advertising[0] |= (ADVERTISED_Pause |
1018874aeea5SJeff Kirsher 						     ADVERTISED_Asym_Pause);
1019874aeea5SJeff Kirsher 		else
1020c2ab85d2SEdward Cree 			efx->link_advertising[0] &= ~(ADVERTISED_Pause |
1021874aeea5SJeff Kirsher 						      ADVERTISED_Asym_Pause);
1022874aeea5SJeff Kirsher 		if (wanted_fc & EFX_FC_TX)
1023c2ab85d2SEdward Cree 			efx->link_advertising[0] ^= ADVERTISED_Asym_Pause;
1024874aeea5SJeff Kirsher 	}
1025874aeea5SJeff Kirsher }
1026874aeea5SJeff Kirsher 
1027874aeea5SJeff Kirsher static void efx_fini_port(struct efx_nic *efx);
1028874aeea5SJeff Kirsher 
10290d322413SEdward Cree /* We assume that efx->type->reconfigure_mac will always try to sync RX
10300d322413SEdward Cree  * filters and therefore needs to read-lock the filter table against freeing
10310d322413SEdward Cree  */
10320d322413SEdward Cree void efx_mac_reconfigure(struct efx_nic *efx)
10330d322413SEdward Cree {
10340d322413SEdward Cree 	down_read(&efx->filter_sem);
10350d322413SEdward Cree 	efx->type->reconfigure_mac(efx);
10360d322413SEdward Cree 	up_read(&efx->filter_sem);
10370d322413SEdward Cree }
10380d322413SEdward Cree 
1039874aeea5SJeff Kirsher /* Push loopback/power/transmit disable settings to the PHY, and reconfigure
1040874aeea5SJeff Kirsher  * the MAC appropriately. All other PHY configuration changes are pushed
1041874aeea5SJeff Kirsher  * through phy_op->set_settings(), and pushed asynchronously to the MAC
1042874aeea5SJeff Kirsher  * through efx_monitor().
1043874aeea5SJeff Kirsher  *
1044874aeea5SJeff Kirsher  * Callers must hold the mac_lock
1045874aeea5SJeff Kirsher  */
1046874aeea5SJeff Kirsher int __efx_reconfigure_port(struct efx_nic *efx)
1047874aeea5SJeff Kirsher {
1048874aeea5SJeff Kirsher 	enum efx_phy_mode phy_mode;
1049874aeea5SJeff Kirsher 	int rc;
1050874aeea5SJeff Kirsher 
1051874aeea5SJeff Kirsher 	WARN_ON(!mutex_is_locked(&efx->mac_lock));
1052874aeea5SJeff Kirsher 
1053874aeea5SJeff Kirsher 	/* Disable PHY transmit in mac level loopbacks */
1054874aeea5SJeff Kirsher 	phy_mode = efx->phy_mode;
1055874aeea5SJeff Kirsher 	if (LOOPBACK_INTERNAL(efx))
1056874aeea5SJeff Kirsher 		efx->phy_mode |= PHY_MODE_TX_DISABLED;
1057874aeea5SJeff Kirsher 	else
1058874aeea5SJeff Kirsher 		efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
1059874aeea5SJeff Kirsher 
1060874aeea5SJeff Kirsher 	rc = efx->type->reconfigure_port(efx);
1061874aeea5SJeff Kirsher 
1062874aeea5SJeff Kirsher 	if (rc)
1063874aeea5SJeff Kirsher 		efx->phy_mode = phy_mode;
1064874aeea5SJeff Kirsher 
1065874aeea5SJeff Kirsher 	return rc;
1066874aeea5SJeff Kirsher }
1067874aeea5SJeff Kirsher 
1068874aeea5SJeff Kirsher /* Reinitialise the MAC to pick up new PHY settings, even if the port is
1069874aeea5SJeff Kirsher  * disabled. */
1070874aeea5SJeff Kirsher int efx_reconfigure_port(struct efx_nic *efx)
1071874aeea5SJeff Kirsher {
1072874aeea5SJeff Kirsher 	int rc;
1073874aeea5SJeff Kirsher 
1074874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
1075874aeea5SJeff Kirsher 
1076874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
1077874aeea5SJeff Kirsher 	rc = __efx_reconfigure_port(efx);
1078874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
1079874aeea5SJeff Kirsher 
1080874aeea5SJeff Kirsher 	return rc;
1081874aeea5SJeff Kirsher }
1082874aeea5SJeff Kirsher 
1083874aeea5SJeff Kirsher /* Asynchronous work item for changing MAC promiscuity and multicast
1084874aeea5SJeff Kirsher  * hash.  Avoid a drain/rx_ingress enable by reconfiguring the current
1085874aeea5SJeff Kirsher  * MAC directly. */
1086874aeea5SJeff Kirsher static void efx_mac_work(struct work_struct *data)
1087874aeea5SJeff Kirsher {
1088874aeea5SJeff Kirsher 	struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
1089874aeea5SJeff Kirsher 
1090874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
109130b81cdaSBen Hutchings 	if (efx->port_enabled)
10920d322413SEdward Cree 		efx_mac_reconfigure(efx);
1093874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
1094874aeea5SJeff Kirsher }
1095874aeea5SJeff Kirsher 
1096874aeea5SJeff Kirsher static int efx_probe_port(struct efx_nic *efx)
1097874aeea5SJeff Kirsher {
1098874aeea5SJeff Kirsher 	int rc;
1099874aeea5SJeff Kirsher 
1100874aeea5SJeff Kirsher 	netif_dbg(efx, probe, efx->net_dev, "create port\n");
1101874aeea5SJeff Kirsher 
1102874aeea5SJeff Kirsher 	if (phy_flash_cfg)
1103874aeea5SJeff Kirsher 		efx->phy_mode = PHY_MODE_SPECIAL;
1104874aeea5SJeff Kirsher 
1105874aeea5SJeff Kirsher 	/* Connect up MAC/PHY operations table */
1106874aeea5SJeff Kirsher 	rc = efx->type->probe_port(efx);
1107874aeea5SJeff Kirsher 	if (rc)
1108874aeea5SJeff Kirsher 		return rc;
1109874aeea5SJeff Kirsher 
1110e332bcb3SBen Hutchings 	/* Initialise MAC address to permanent address */
1111cd84ff4dSEdward Cree 	ether_addr_copy(efx->net_dev->dev_addr, efx->net_dev->perm_addr);
1112874aeea5SJeff Kirsher 
1113874aeea5SJeff Kirsher 	return 0;
1114874aeea5SJeff Kirsher }
1115874aeea5SJeff Kirsher 
1116874aeea5SJeff Kirsher static int efx_init_port(struct efx_nic *efx)
1117874aeea5SJeff Kirsher {
1118874aeea5SJeff Kirsher 	int rc;
1119874aeea5SJeff Kirsher 
1120874aeea5SJeff Kirsher 	netif_dbg(efx, drv, efx->net_dev, "init port\n");
1121874aeea5SJeff Kirsher 
1122874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
1123874aeea5SJeff Kirsher 
1124874aeea5SJeff Kirsher 	rc = efx->phy_op->init(efx);
1125874aeea5SJeff Kirsher 	if (rc)
1126874aeea5SJeff Kirsher 		goto fail1;
1127874aeea5SJeff Kirsher 
1128874aeea5SJeff Kirsher 	efx->port_initialized = true;
1129874aeea5SJeff Kirsher 
1130874aeea5SJeff Kirsher 	/* Reconfigure the MAC before creating dma queues (required for
1131874aeea5SJeff Kirsher 	 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
11320d322413SEdward Cree 	efx_mac_reconfigure(efx);
1133874aeea5SJeff Kirsher 
1134874aeea5SJeff Kirsher 	/* Ensure the PHY advertises the correct flow control settings */
1135874aeea5SJeff Kirsher 	rc = efx->phy_op->reconfigure(efx);
1136267d9d73SEdward Cree 	if (rc && rc != -EPERM)
1137874aeea5SJeff Kirsher 		goto fail2;
1138874aeea5SJeff Kirsher 
1139874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
1140874aeea5SJeff Kirsher 	return 0;
1141874aeea5SJeff Kirsher 
1142874aeea5SJeff Kirsher fail2:
1143874aeea5SJeff Kirsher 	efx->phy_op->fini(efx);
1144874aeea5SJeff Kirsher fail1:
1145874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
1146874aeea5SJeff Kirsher 	return rc;
1147874aeea5SJeff Kirsher }
1148874aeea5SJeff Kirsher 
1149874aeea5SJeff Kirsher static void efx_start_port(struct efx_nic *efx)
1150874aeea5SJeff Kirsher {
1151874aeea5SJeff Kirsher 	netif_dbg(efx, ifup, efx->net_dev, "start port\n");
1152874aeea5SJeff Kirsher 	BUG_ON(efx->port_enabled);
1153874aeea5SJeff Kirsher 
1154874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
1155874aeea5SJeff Kirsher 	efx->port_enabled = true;
1156874aeea5SJeff Kirsher 
1157d615c039SBen Hutchings 	/* Ensure MAC ingress/egress is enabled */
11580d322413SEdward Cree 	efx_mac_reconfigure(efx);
1159874aeea5SJeff Kirsher 
1160874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
1161874aeea5SJeff Kirsher }
1162874aeea5SJeff Kirsher 
1163d615c039SBen Hutchings /* Cancel work for MAC reconfiguration, periodic hardware monitoring
1164d615c039SBen Hutchings  * and the async self-test, wait for them to finish and prevent them
1165d615c039SBen Hutchings  * being scheduled again.  This doesn't cover online resets, which
1166d615c039SBen Hutchings  * should only be cancelled when removing the device.
1167d615c039SBen Hutchings  */
1168874aeea5SJeff Kirsher static void efx_stop_port(struct efx_nic *efx)
1169874aeea5SJeff Kirsher {
1170874aeea5SJeff Kirsher 	netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
1171874aeea5SJeff Kirsher 
1172d615c039SBen Hutchings 	EFX_ASSERT_RESET_SERIALISED(efx);
1173d615c039SBen Hutchings 
1174874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
1175874aeea5SJeff Kirsher 	efx->port_enabled = false;
1176874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
1177874aeea5SJeff Kirsher 
1178874aeea5SJeff Kirsher 	/* Serialise against efx_set_multicast_list() */
1179874aeea5SJeff Kirsher 	netif_addr_lock_bh(efx->net_dev);
1180874aeea5SJeff Kirsher 	netif_addr_unlock_bh(efx->net_dev);
1181d615c039SBen Hutchings 
1182d615c039SBen Hutchings 	cancel_delayed_work_sync(&efx->monitor_work);
1183d615c039SBen Hutchings 	efx_selftest_async_cancel(efx);
1184d615c039SBen Hutchings 	cancel_work_sync(&efx->mac_work);
1185874aeea5SJeff Kirsher }
1186874aeea5SJeff Kirsher 
1187874aeea5SJeff Kirsher static void efx_fini_port(struct efx_nic *efx)
1188874aeea5SJeff Kirsher {
1189874aeea5SJeff Kirsher 	netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
1190874aeea5SJeff Kirsher 
1191874aeea5SJeff Kirsher 	if (!efx->port_initialized)
1192874aeea5SJeff Kirsher 		return;
1193874aeea5SJeff Kirsher 
1194874aeea5SJeff Kirsher 	efx->phy_op->fini(efx);
1195874aeea5SJeff Kirsher 	efx->port_initialized = false;
1196874aeea5SJeff Kirsher 
1197874aeea5SJeff Kirsher 	efx->link_state.up = false;
1198874aeea5SJeff Kirsher 	efx_link_status_changed(efx);
1199874aeea5SJeff Kirsher }
1200874aeea5SJeff Kirsher 
1201874aeea5SJeff Kirsher static void efx_remove_port(struct efx_nic *efx)
1202874aeea5SJeff Kirsher {
1203874aeea5SJeff Kirsher 	netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
1204874aeea5SJeff Kirsher 
1205874aeea5SJeff Kirsher 	efx->type->remove_port(efx);
1206874aeea5SJeff Kirsher }
1207874aeea5SJeff Kirsher 
1208874aeea5SJeff Kirsher /**************************************************************************
1209874aeea5SJeff Kirsher  *
1210874aeea5SJeff Kirsher  * NIC handling
1211874aeea5SJeff Kirsher  *
1212874aeea5SJeff Kirsher  **************************************************************************/
1213874aeea5SJeff Kirsher 
12140bcf4a64SBen Hutchings static LIST_HEAD(efx_primary_list);
12150bcf4a64SBen Hutchings static LIST_HEAD(efx_unassociated_list);
12160bcf4a64SBen Hutchings 
12170bcf4a64SBen Hutchings static bool efx_same_controller(struct efx_nic *left, struct efx_nic *right)
12180bcf4a64SBen Hutchings {
12190bcf4a64SBen Hutchings 	return left->type == right->type &&
12200bcf4a64SBen Hutchings 		left->vpd_sn && right->vpd_sn &&
12210bcf4a64SBen Hutchings 		!strcmp(left->vpd_sn, right->vpd_sn);
12220bcf4a64SBen Hutchings }
12230bcf4a64SBen Hutchings 
12240bcf4a64SBen Hutchings static void efx_associate(struct efx_nic *efx)
12250bcf4a64SBen Hutchings {
12260bcf4a64SBen Hutchings 	struct efx_nic *other, *next;
12270bcf4a64SBen Hutchings 
12280bcf4a64SBen Hutchings 	if (efx->primary == efx) {
12290bcf4a64SBen Hutchings 		/* Adding primary function; look for secondaries */
12300bcf4a64SBen Hutchings 
12310bcf4a64SBen Hutchings 		netif_dbg(efx, probe, efx->net_dev, "adding to primary list\n");
12320bcf4a64SBen Hutchings 		list_add_tail(&efx->node, &efx_primary_list);
12330bcf4a64SBen Hutchings 
12340bcf4a64SBen Hutchings 		list_for_each_entry_safe(other, next, &efx_unassociated_list,
12350bcf4a64SBen Hutchings 					 node) {
12360bcf4a64SBen Hutchings 			if (efx_same_controller(efx, other)) {
12370bcf4a64SBen Hutchings 				list_del(&other->node);
12380bcf4a64SBen Hutchings 				netif_dbg(other, probe, other->net_dev,
12390bcf4a64SBen Hutchings 					  "moving to secondary list of %s %s\n",
12400bcf4a64SBen Hutchings 					  pci_name(efx->pci_dev),
12410bcf4a64SBen Hutchings 					  efx->net_dev->name);
12420bcf4a64SBen Hutchings 				list_add_tail(&other->node,
12430bcf4a64SBen Hutchings 					      &efx->secondary_list);
12440bcf4a64SBen Hutchings 				other->primary = efx;
12450bcf4a64SBen Hutchings 			}
12460bcf4a64SBen Hutchings 		}
12470bcf4a64SBen Hutchings 	} else {
12480bcf4a64SBen Hutchings 		/* Adding secondary function; look for primary */
12490bcf4a64SBen Hutchings 
12500bcf4a64SBen Hutchings 		list_for_each_entry(other, &efx_primary_list, node) {
12510bcf4a64SBen Hutchings 			if (efx_same_controller(efx, other)) {
12520bcf4a64SBen Hutchings 				netif_dbg(efx, probe, efx->net_dev,
12530bcf4a64SBen Hutchings 					  "adding to secondary list of %s %s\n",
12540bcf4a64SBen Hutchings 					  pci_name(other->pci_dev),
12550bcf4a64SBen Hutchings 					  other->net_dev->name);
12560bcf4a64SBen Hutchings 				list_add_tail(&efx->node,
12570bcf4a64SBen Hutchings 					      &other->secondary_list);
12580bcf4a64SBen Hutchings 				efx->primary = other;
12590bcf4a64SBen Hutchings 				return;
12600bcf4a64SBen Hutchings 			}
12610bcf4a64SBen Hutchings 		}
12620bcf4a64SBen Hutchings 
12630bcf4a64SBen Hutchings 		netif_dbg(efx, probe, efx->net_dev,
12640bcf4a64SBen Hutchings 			  "adding to unassociated list\n");
12650bcf4a64SBen Hutchings 		list_add_tail(&efx->node, &efx_unassociated_list);
12660bcf4a64SBen Hutchings 	}
12670bcf4a64SBen Hutchings }
12680bcf4a64SBen Hutchings 
12690bcf4a64SBen Hutchings static void efx_dissociate(struct efx_nic *efx)
12700bcf4a64SBen Hutchings {
12710bcf4a64SBen Hutchings 	struct efx_nic *other, *next;
12720bcf4a64SBen Hutchings 
12730bcf4a64SBen Hutchings 	list_del(&efx->node);
12740bcf4a64SBen Hutchings 	efx->primary = NULL;
12750bcf4a64SBen Hutchings 
12760bcf4a64SBen Hutchings 	list_for_each_entry_safe(other, next, &efx->secondary_list, node) {
12770bcf4a64SBen Hutchings 		list_del(&other->node);
12780bcf4a64SBen Hutchings 		netif_dbg(other, probe, other->net_dev,
12790bcf4a64SBen Hutchings 			  "moving to unassociated list\n");
12800bcf4a64SBen Hutchings 		list_add_tail(&other->node, &efx_unassociated_list);
12810bcf4a64SBen Hutchings 		other->primary = NULL;
12820bcf4a64SBen Hutchings 	}
12830bcf4a64SBen Hutchings }
12840bcf4a64SBen Hutchings 
1285874aeea5SJeff Kirsher /* This configures the PCI device to enable I/O and DMA. */
1286874aeea5SJeff Kirsher static int efx_init_io(struct efx_nic *efx)
1287874aeea5SJeff Kirsher {
1288874aeea5SJeff Kirsher 	struct pci_dev *pci_dev = efx->pci_dev;
1289874aeea5SJeff Kirsher 	dma_addr_t dma_mask = efx->type->max_dma_mask;
1290b105798fSBen Hutchings 	unsigned int mem_map_size = efx->type->mem_map_size(efx);
129102246a7fSShradha Shah 	int rc, bar;
1292874aeea5SJeff Kirsher 
1293874aeea5SJeff Kirsher 	netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
1294874aeea5SJeff Kirsher 
129503714bbbSEdward Cree 	bar = efx->type->mem_bar(efx);
129602246a7fSShradha Shah 
1297874aeea5SJeff Kirsher 	rc = pci_enable_device(pci_dev);
1298874aeea5SJeff Kirsher 	if (rc) {
1299874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev,
1300874aeea5SJeff Kirsher 			  "failed to enable PCI device\n");
1301874aeea5SJeff Kirsher 		goto fail1;
1302874aeea5SJeff Kirsher 	}
1303874aeea5SJeff Kirsher 
1304874aeea5SJeff Kirsher 	pci_set_master(pci_dev);
1305874aeea5SJeff Kirsher 
130606e9552fSChristoph Hellwig 	/* Set the PCI DMA mask.  Try all possibilities from our genuine mask
130706e9552fSChristoph Hellwig 	 * down to 32 bits, because some architectures will allow 40 bit
1308874aeea5SJeff Kirsher 	 * masks event though they reject 46 bit masks.
1309874aeea5SJeff Kirsher 	 */
1310874aeea5SJeff Kirsher 	while (dma_mask > 0x7fffffffUL) {
13119663dedaSRussell King 		rc = dma_set_mask_and_coherent(&pci_dev->dev, dma_mask);
1312e9e01846SBen Hutchings 		if (rc == 0)
1313874aeea5SJeff Kirsher 			break;
1314874aeea5SJeff Kirsher 		dma_mask >>= 1;
1315874aeea5SJeff Kirsher 	}
1316874aeea5SJeff Kirsher 	if (rc) {
1317874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev,
1318874aeea5SJeff Kirsher 			  "could not find a suitable DMA mask\n");
1319874aeea5SJeff Kirsher 		goto fail2;
1320874aeea5SJeff Kirsher 	}
1321874aeea5SJeff Kirsher 	netif_dbg(efx, probe, efx->net_dev,
1322874aeea5SJeff Kirsher 		  "using DMA mask %llx\n", (unsigned long long) dma_mask);
1323874aeea5SJeff Kirsher 
132402246a7fSShradha Shah 	efx->membase_phys = pci_resource_start(efx->pci_dev, bar);
132502246a7fSShradha Shah 	rc = pci_request_region(pci_dev, bar, "sfc");
1326874aeea5SJeff Kirsher 	if (rc) {
1327874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev,
1328874aeea5SJeff Kirsher 			  "request for memory BAR failed\n");
1329874aeea5SJeff Kirsher 		rc = -EIO;
1330874aeea5SJeff Kirsher 		goto fail3;
1331874aeea5SJeff Kirsher 	}
1332b105798fSBen Hutchings 	efx->membase = ioremap_nocache(efx->membase_phys, mem_map_size);
1333874aeea5SJeff Kirsher 	if (!efx->membase) {
1334874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev,
1335874aeea5SJeff Kirsher 			  "could not map memory BAR at %llx+%x\n",
1336b105798fSBen Hutchings 			  (unsigned long long)efx->membase_phys, mem_map_size);
1337874aeea5SJeff Kirsher 		rc = -ENOMEM;
1338874aeea5SJeff Kirsher 		goto fail4;
1339874aeea5SJeff Kirsher 	}
1340874aeea5SJeff Kirsher 	netif_dbg(efx, probe, efx->net_dev,
1341874aeea5SJeff Kirsher 		  "memory BAR at %llx+%x (virtual %p)\n",
1342b105798fSBen Hutchings 		  (unsigned long long)efx->membase_phys, mem_map_size,
1343b105798fSBen Hutchings 		  efx->membase);
1344874aeea5SJeff Kirsher 
1345874aeea5SJeff Kirsher 	return 0;
1346874aeea5SJeff Kirsher 
1347874aeea5SJeff Kirsher  fail4:
134802246a7fSShradha Shah 	pci_release_region(efx->pci_dev, bar);
1349874aeea5SJeff Kirsher  fail3:
1350874aeea5SJeff Kirsher 	efx->membase_phys = 0;
1351874aeea5SJeff Kirsher  fail2:
1352874aeea5SJeff Kirsher 	pci_disable_device(efx->pci_dev);
1353874aeea5SJeff Kirsher  fail1:
1354874aeea5SJeff Kirsher 	return rc;
1355874aeea5SJeff Kirsher }
1356874aeea5SJeff Kirsher 
1357874aeea5SJeff Kirsher static void efx_fini_io(struct efx_nic *efx)
1358874aeea5SJeff Kirsher {
135902246a7fSShradha Shah 	int bar;
136002246a7fSShradha Shah 
1361874aeea5SJeff Kirsher 	netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
1362874aeea5SJeff Kirsher 
1363874aeea5SJeff Kirsher 	if (efx->membase) {
1364874aeea5SJeff Kirsher 		iounmap(efx->membase);
1365874aeea5SJeff Kirsher 		efx->membase = NULL;
1366874aeea5SJeff Kirsher 	}
1367874aeea5SJeff Kirsher 
1368874aeea5SJeff Kirsher 	if (efx->membase_phys) {
136903714bbbSEdward Cree 		bar = efx->type->mem_bar(efx);
137002246a7fSShradha Shah 		pci_release_region(efx->pci_dev, bar);
1371874aeea5SJeff Kirsher 		efx->membase_phys = 0;
1372874aeea5SJeff Kirsher 	}
1373874aeea5SJeff Kirsher 
13746598dad2SDaniel Pieczko 	/* Don't disable bus-mastering if VFs are assigned */
13756598dad2SDaniel Pieczko 	if (!pci_vfs_assigned(efx->pci_dev))
1376874aeea5SJeff Kirsher 		pci_disable_device(efx->pci_dev);
1377874aeea5SJeff Kirsher }
1378874aeea5SJeff Kirsher 
137942356d9aSEdward Cree void efx_set_default_rx_indir_table(struct efx_nic *efx,
138042356d9aSEdward Cree 				    struct efx_rss_context *ctx)
1381267c0157SJon Cooper {
1382267c0157SJon Cooper 	size_t i;
1383267c0157SJon Cooper 
138442356d9aSEdward Cree 	for (i = 0; i < ARRAY_SIZE(ctx->rx_indir_table); i++)
138542356d9aSEdward Cree 		ctx->rx_indir_table[i] =
1386267c0157SJon Cooper 			ethtool_rxfh_indir_default(i, efx->rss_spread);
1387267c0157SJon Cooper }
1388267c0157SJon Cooper 
1389a9a52506SBen Hutchings static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
1390874aeea5SJeff Kirsher {
1391cdb08f8fSBen Hutchings 	cpumask_var_t thread_mask;
1392a16e5b24SBen Hutchings 	unsigned int count;
1393874aeea5SJeff Kirsher 	int cpu;
1394874aeea5SJeff Kirsher 
1395cd2d5b52SBen Hutchings 	if (rss_cpus) {
1396cd2d5b52SBen Hutchings 		count = rss_cpus;
1397cd2d5b52SBen Hutchings 	} else {
1398cdb08f8fSBen Hutchings 		if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
1399a9a52506SBen Hutchings 			netif_warn(efx, probe, efx->net_dev,
1400a9a52506SBen Hutchings 				   "RSS disabled due to allocation failure\n");
1401874aeea5SJeff Kirsher 			return 1;
1402874aeea5SJeff Kirsher 		}
1403874aeea5SJeff Kirsher 
1404874aeea5SJeff Kirsher 		count = 0;
1405874aeea5SJeff Kirsher 		for_each_online_cpu(cpu) {
1406cdb08f8fSBen Hutchings 			if (!cpumask_test_cpu(cpu, thread_mask)) {
1407874aeea5SJeff Kirsher 				++count;
1408cdb08f8fSBen Hutchings 				cpumask_or(thread_mask, thread_mask,
140906931e62SBartosz Golaszewski 					   topology_sibling_cpumask(cpu));
1410874aeea5SJeff Kirsher 			}
1411874aeea5SJeff Kirsher 		}
1412874aeea5SJeff Kirsher 
1413cdb08f8fSBen Hutchings 		free_cpumask_var(thread_mask);
1414cd2d5b52SBen Hutchings 	}
1415cd2d5b52SBen Hutchings 
1416271a8b42SBert Kenward 	if (count > EFX_MAX_RX_QUEUES) {
1417271a8b42SBert Kenward 		netif_cond_dbg(efx, probe, efx->net_dev, !rss_cpus, warn,
1418271a8b42SBert Kenward 			       "Reducing number of rx queues from %u to %u.\n",
1419271a8b42SBert Kenward 			       count, EFX_MAX_RX_QUEUES);
1420271a8b42SBert Kenward 		count = EFX_MAX_RX_QUEUES;
1421271a8b42SBert Kenward 	}
1422271a8b42SBert Kenward 
1423cd2d5b52SBen Hutchings 	/* If RSS is requested for the PF *and* VFs then we can't write RSS
1424cd2d5b52SBen Hutchings 	 * table entries that are inaccessible to VFs
1425cd2d5b52SBen Hutchings 	 */
14267fa8d547SShradha Shah #ifdef CONFIG_SFC_SRIOV
14277fa8d547SShradha Shah 	if (efx->type->sriov_wanted) {
1428d98a4ffeSShradha Shah 		if (efx->type->sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
1429cd2d5b52SBen Hutchings 		    count > efx_vf_size(efx)) {
1430cd2d5b52SBen Hutchings 			netif_warn(efx, probe, efx->net_dev,
1431cd2d5b52SBen Hutchings 				   "Reducing number of RSS channels from %u to %u for "
1432cd2d5b52SBen Hutchings 				   "VF support. Increase vf-msix-limit to use more "
1433cd2d5b52SBen Hutchings 				   "channels on the PF.\n",
1434cd2d5b52SBen Hutchings 				   count, efx_vf_size(efx));
1435cd2d5b52SBen Hutchings 			count = efx_vf_size(efx);
1436cd2d5b52SBen Hutchings 		}
14377fa8d547SShradha Shah 	}
14387fa8d547SShradha Shah #endif
1439cd2d5b52SBen Hutchings 
1440874aeea5SJeff Kirsher 	return count;
1441874aeea5SJeff Kirsher }
1442874aeea5SJeff Kirsher 
1443874aeea5SJeff Kirsher /* Probe the number and type of interrupts we are able to obtain, and
1444874aeea5SJeff Kirsher  * the resulting numbers of channels and RX queues.
1445874aeea5SJeff Kirsher  */
1446874aeea5SJeff Kirsher static int efx_probe_interrupts(struct efx_nic *efx)
1447874aeea5SJeff Kirsher {
14487f967c01SBen Hutchings 	unsigned int extra_channels = 0;
14497f967c01SBen Hutchings 	unsigned int i, j;
1450a16e5b24SBen Hutchings 	int rc;
1451874aeea5SJeff Kirsher 
14527f967c01SBen Hutchings 	for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
14537f967c01SBen Hutchings 		if (efx->extra_channel_type[i])
14547f967c01SBen Hutchings 			++extra_channels;
14557f967c01SBen Hutchings 
1456874aeea5SJeff Kirsher 	if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
1457874aeea5SJeff Kirsher 		struct msix_entry xentries[EFX_MAX_CHANNELS];
1458a16e5b24SBen Hutchings 		unsigned int n_channels;
1459874aeea5SJeff Kirsher 
1460a9a52506SBen Hutchings 		n_channels = efx_wanted_parallelism(efx);
1461b0fbdae1SShradha Shah 		if (efx_separate_tx_channels)
1462874aeea5SJeff Kirsher 			n_channels *= 2;
14637f967c01SBen Hutchings 		n_channels += extra_channels;
1464b105798fSBen Hutchings 		n_channels = min(n_channels, efx->max_channels);
1465874aeea5SJeff Kirsher 
1466874aeea5SJeff Kirsher 		for (i = 0; i < n_channels; i++)
1467874aeea5SJeff Kirsher 			xentries[i].entry = i;
1468184603d8SAlexander Gordeev 		rc = pci_enable_msix_range(efx->pci_dev,
1469184603d8SAlexander Gordeev 					   xentries, 1, n_channels);
1470184603d8SAlexander Gordeev 		if (rc < 0) {
1471184603d8SAlexander Gordeev 			/* Fall back to single channel MSI */
1472184603d8SAlexander Gordeev 			netif_err(efx, drv, efx->net_dev,
1473184603d8SAlexander Gordeev 				  "could not enable MSI-X\n");
147462980cb6SAndrew Rybchenko 			if (efx->type->min_interrupt_mode >= EFX_INT_MODE_MSI)
147562980cb6SAndrew Rybchenko 				efx->interrupt_mode = EFX_INT_MODE_MSI;
147662980cb6SAndrew Rybchenko 			else
147762980cb6SAndrew Rybchenko 				return rc;
1478184603d8SAlexander Gordeev 		} else if (rc < n_channels) {
1479874aeea5SJeff Kirsher 			netif_err(efx, drv, efx->net_dev,
1480874aeea5SJeff Kirsher 				  "WARNING: Insufficient MSI-X vectors"
1481a16e5b24SBen Hutchings 				  " available (%d < %u).\n", rc, n_channels);
1482874aeea5SJeff Kirsher 			netif_err(efx, drv, efx->net_dev,
1483874aeea5SJeff Kirsher 				  "WARNING: Performance may be reduced.\n");
1484874aeea5SJeff Kirsher 			n_channels = rc;
1485874aeea5SJeff Kirsher 		}
1486874aeea5SJeff Kirsher 
1487184603d8SAlexander Gordeev 		if (rc > 0) {
1488874aeea5SJeff Kirsher 			efx->n_channels = n_channels;
14897f967c01SBen Hutchings 			if (n_channels > extra_channels)
14907f967c01SBen Hutchings 				n_channels -= extra_channels;
1491b0fbdae1SShradha Shah 			if (efx_separate_tx_channels) {
1492b0fbdae1SShradha Shah 				efx->n_tx_channels = min(max(n_channels / 2,
1493b0fbdae1SShradha Shah 							     1U),
1494b0fbdae1SShradha Shah 							 efx->max_tx_channels);
14957f967c01SBen Hutchings 				efx->n_rx_channels = max(n_channels -
14967f967c01SBen Hutchings 							 efx->n_tx_channels,
14977f967c01SBen Hutchings 							 1U);
1498874aeea5SJeff Kirsher 			} else {
1499b0fbdae1SShradha Shah 				efx->n_tx_channels = min(n_channels,
1500b0fbdae1SShradha Shah 							 efx->max_tx_channels);
15017f967c01SBen Hutchings 				efx->n_rx_channels = n_channels;
1502874aeea5SJeff Kirsher 			}
15037f967c01SBen Hutchings 			for (i = 0; i < efx->n_channels; i++)
1504874aeea5SJeff Kirsher 				efx_get_channel(efx, i)->irq =
1505874aeea5SJeff Kirsher 					xentries[i].vector;
1506874aeea5SJeff Kirsher 		}
1507874aeea5SJeff Kirsher 	}
1508874aeea5SJeff Kirsher 
1509874aeea5SJeff Kirsher 	/* Try single interrupt MSI */
1510874aeea5SJeff Kirsher 	if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
1511874aeea5SJeff Kirsher 		efx->n_channels = 1;
1512874aeea5SJeff Kirsher 		efx->n_rx_channels = 1;
1513874aeea5SJeff Kirsher 		efx->n_tx_channels = 1;
1514874aeea5SJeff Kirsher 		rc = pci_enable_msi(efx->pci_dev);
1515874aeea5SJeff Kirsher 		if (rc == 0) {
1516874aeea5SJeff Kirsher 			efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
1517874aeea5SJeff Kirsher 		} else {
1518874aeea5SJeff Kirsher 			netif_err(efx, drv, efx->net_dev,
1519874aeea5SJeff Kirsher 				  "could not enable MSI\n");
152062980cb6SAndrew Rybchenko 			if (efx->type->min_interrupt_mode >= EFX_INT_MODE_LEGACY)
1521874aeea5SJeff Kirsher 				efx->interrupt_mode = EFX_INT_MODE_LEGACY;
152262980cb6SAndrew Rybchenko 			else
152362980cb6SAndrew Rybchenko 				return rc;
1524874aeea5SJeff Kirsher 		}
1525874aeea5SJeff Kirsher 	}
1526874aeea5SJeff Kirsher 
1527874aeea5SJeff Kirsher 	/* Assume legacy interrupts */
1528874aeea5SJeff Kirsher 	if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
1529b0fbdae1SShradha Shah 		efx->n_channels = 1 + (efx_separate_tx_channels ? 1 : 0);
1530874aeea5SJeff Kirsher 		efx->n_rx_channels = 1;
1531874aeea5SJeff Kirsher 		efx->n_tx_channels = 1;
1532874aeea5SJeff Kirsher 		efx->legacy_irq = efx->pci_dev->irq;
1533874aeea5SJeff Kirsher 	}
1534874aeea5SJeff Kirsher 
15357f967c01SBen Hutchings 	/* Assign extra channels if possible */
15362935e3c3SEdward Cree 	efx->n_extra_tx_channels = 0;
15377f967c01SBen Hutchings 	j = efx->n_channels;
15387f967c01SBen Hutchings 	for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
15397f967c01SBen Hutchings 		if (!efx->extra_channel_type[i])
15407f967c01SBen Hutchings 			continue;
15417f967c01SBen Hutchings 		if (efx->interrupt_mode != EFX_INT_MODE_MSIX ||
15427f967c01SBen Hutchings 		    efx->n_channels <= extra_channels) {
15437f967c01SBen Hutchings 			efx->extra_channel_type[i]->handle_no_channel(efx);
15447f967c01SBen Hutchings 		} else {
15457f967c01SBen Hutchings 			--j;
15467f967c01SBen Hutchings 			efx_get_channel(efx, j)->type =
15477f967c01SBen Hutchings 				efx->extra_channel_type[i];
15482935e3c3SEdward Cree 			if (efx_channel_has_tx_queues(efx_get_channel(efx, j)))
15492935e3c3SEdward Cree 				efx->n_extra_tx_channels++;
15507f967c01SBen Hutchings 		}
15517f967c01SBen Hutchings 	}
15527f967c01SBen Hutchings 
1553cd2d5b52SBen Hutchings 	/* RSS might be usable on VFs even if it is disabled on the PF */
15547fa8d547SShradha Shah #ifdef CONFIG_SFC_SRIOV
15557fa8d547SShradha Shah 	if (efx->type->sriov_wanted) {
1556327c685eSShradha Shah 		efx->rss_spread = ((efx->n_rx_channels > 1 ||
1557d98a4ffeSShradha Shah 				    !efx->type->sriov_wanted(efx)) ?
1558cd2d5b52SBen Hutchings 				   efx->n_rx_channels : efx_vf_size(efx));
15597fa8d547SShradha Shah 		return 0;
15607fa8d547SShradha Shah 	}
15617fa8d547SShradha Shah #endif
15627fa8d547SShradha Shah 	efx->rss_spread = efx->n_rx_channels;
1563cd2d5b52SBen Hutchings 
1564874aeea5SJeff Kirsher 	return 0;
1565874aeea5SJeff Kirsher }
1566874aeea5SJeff Kirsher 
1567a83762d9SBert Kenward #if defined(CONFIG_SMP)
1568a83762d9SBert Kenward static void efx_set_interrupt_affinity(struct efx_nic *efx)
1569a83762d9SBert Kenward {
1570a83762d9SBert Kenward 	struct efx_channel *channel;
1571a83762d9SBert Kenward 	unsigned int cpu;
1572a83762d9SBert Kenward 
1573a83762d9SBert Kenward 	efx_for_each_channel(channel, efx) {
1574a83762d9SBert Kenward 		cpu = cpumask_local_spread(channel->channel,
1575a83762d9SBert Kenward 					   pcibus_to_node(efx->pci_dev->bus));
1576a83762d9SBert Kenward 		irq_set_affinity_hint(channel->irq, cpumask_of(cpu));
1577a83762d9SBert Kenward 	}
1578a83762d9SBert Kenward }
1579a83762d9SBert Kenward 
1580a83762d9SBert Kenward static void efx_clear_interrupt_affinity(struct efx_nic *efx)
1581a83762d9SBert Kenward {
1582a83762d9SBert Kenward 	struct efx_channel *channel;
1583a83762d9SBert Kenward 
1584a83762d9SBert Kenward 	efx_for_each_channel(channel, efx)
1585a83762d9SBert Kenward 		irq_set_affinity_hint(channel->irq, NULL);
1586a83762d9SBert Kenward }
1587a83762d9SBert Kenward #else
1588a83762d9SBert Kenward static void
1589a83762d9SBert Kenward efx_set_interrupt_affinity(struct efx_nic *efx __attribute__ ((unused)))
1590a83762d9SBert Kenward {
1591a83762d9SBert Kenward }
1592a83762d9SBert Kenward 
1593a83762d9SBert Kenward static void
1594a83762d9SBert Kenward efx_clear_interrupt_affinity(struct efx_nic *efx __attribute__ ((unused)))
1595a83762d9SBert Kenward {
1596a83762d9SBert Kenward }
1597a83762d9SBert Kenward #endif /* CONFIG_SMP */
1598a83762d9SBert Kenward 
1599261e4d96SJon Cooper static int efx_soft_enable_interrupts(struct efx_nic *efx)
1600d8291187SBen Hutchings {
1601261e4d96SJon Cooper 	struct efx_channel *channel, *end_channel;
1602261e4d96SJon Cooper 	int rc;
1603d8291187SBen Hutchings 
1604d8291187SBen Hutchings 	BUG_ON(efx->state == STATE_DISABLED);
1605d8291187SBen Hutchings 
1606d8291187SBen Hutchings 	efx->irq_soft_enabled = true;
1607d8291187SBen Hutchings 	smp_wmb();
1608d8291187SBen Hutchings 
1609d8291187SBen Hutchings 	efx_for_each_channel(channel, efx) {
1610261e4d96SJon Cooper 		if (!channel->type->keep_eventq) {
1611261e4d96SJon Cooper 			rc = efx_init_eventq(channel);
1612261e4d96SJon Cooper 			if (rc)
1613261e4d96SJon Cooper 				goto fail;
1614261e4d96SJon Cooper 		}
1615d8291187SBen Hutchings 		efx_start_eventq(channel);
1616d8291187SBen Hutchings 	}
1617d8291187SBen Hutchings 
1618d8291187SBen Hutchings 	efx_mcdi_mode_event(efx);
1619261e4d96SJon Cooper 
1620261e4d96SJon Cooper 	return 0;
1621261e4d96SJon Cooper fail:
1622261e4d96SJon Cooper 	end_channel = channel;
1623261e4d96SJon Cooper 	efx_for_each_channel(channel, efx) {
1624261e4d96SJon Cooper 		if (channel == end_channel)
1625261e4d96SJon Cooper 			break;
1626261e4d96SJon Cooper 		efx_stop_eventq(channel);
1627261e4d96SJon Cooper 		if (!channel->type->keep_eventq)
1628261e4d96SJon Cooper 			efx_fini_eventq(channel);
1629261e4d96SJon Cooper 	}
1630261e4d96SJon Cooper 
1631261e4d96SJon Cooper 	return rc;
1632d8291187SBen Hutchings }
1633d8291187SBen Hutchings 
1634d8291187SBen Hutchings static void efx_soft_disable_interrupts(struct efx_nic *efx)
1635d8291187SBen Hutchings {
1636d8291187SBen Hutchings 	struct efx_channel *channel;
1637d8291187SBen Hutchings 
1638d8291187SBen Hutchings 	if (efx->state == STATE_DISABLED)
1639d8291187SBen Hutchings 		return;
1640d8291187SBen Hutchings 
1641d8291187SBen Hutchings 	efx_mcdi_mode_poll(efx);
1642d8291187SBen Hutchings 
1643d8291187SBen Hutchings 	efx->irq_soft_enabled = false;
1644d8291187SBen Hutchings 	smp_wmb();
1645d8291187SBen Hutchings 
1646d8291187SBen Hutchings 	if (efx->legacy_irq)
1647d8291187SBen Hutchings 		synchronize_irq(efx->legacy_irq);
1648d8291187SBen Hutchings 
1649d8291187SBen Hutchings 	efx_for_each_channel(channel, efx) {
1650d8291187SBen Hutchings 		if (channel->irq)
1651d8291187SBen Hutchings 			synchronize_irq(channel->irq);
1652d8291187SBen Hutchings 
1653d8291187SBen Hutchings 		efx_stop_eventq(channel);
1654d8291187SBen Hutchings 		if (!channel->type->keep_eventq)
1655d8291187SBen Hutchings 			efx_fini_eventq(channel);
1656d8291187SBen Hutchings 	}
1657cade715fSBen Hutchings 
1658cade715fSBen Hutchings 	/* Flush the asynchronous MCDI request queue */
1659cade715fSBen Hutchings 	efx_mcdi_flush_async(efx);
1660d8291187SBen Hutchings }
1661d8291187SBen Hutchings 
1662261e4d96SJon Cooper static int efx_enable_interrupts(struct efx_nic *efx)
16639f2cb71cSBen Hutchings {
1664261e4d96SJon Cooper 	struct efx_channel *channel, *end_channel;
1665261e4d96SJon Cooper 	int rc;
16669f2cb71cSBen Hutchings 
16678b7325b4SBen Hutchings 	BUG_ON(efx->state == STATE_DISABLED);
16688b7325b4SBen Hutchings 
1669b28405b0SAlexandre Rames 	if (efx->eeh_disabled_legacy_irq) {
1670b28405b0SAlexandre Rames 		enable_irq(efx->legacy_irq);
1671b28405b0SAlexandre Rames 		efx->eeh_disabled_legacy_irq = false;
1672b28405b0SAlexandre Rames 	}
1673d8291187SBen Hutchings 
167486094f7fSBen Hutchings 	efx->type->irq_enable_master(efx);
16759f2cb71cSBen Hutchings 
16769f2cb71cSBen Hutchings 	efx_for_each_channel(channel, efx) {
1677261e4d96SJon Cooper 		if (channel->type->keep_eventq) {
1678261e4d96SJon Cooper 			rc = efx_init_eventq(channel);
1679261e4d96SJon Cooper 			if (rc)
1680261e4d96SJon Cooper 				goto fail;
1681261e4d96SJon Cooper 		}
16829f2cb71cSBen Hutchings 	}
16839f2cb71cSBen Hutchings 
1684261e4d96SJon Cooper 	rc = efx_soft_enable_interrupts(efx);
1685261e4d96SJon Cooper 	if (rc)
1686261e4d96SJon Cooper 		goto fail;
1687261e4d96SJon Cooper 
1688261e4d96SJon Cooper 	return 0;
1689261e4d96SJon Cooper 
1690261e4d96SJon Cooper fail:
1691261e4d96SJon Cooper 	end_channel = channel;
1692261e4d96SJon Cooper 	efx_for_each_channel(channel, efx) {
1693261e4d96SJon Cooper 		if (channel == end_channel)
1694261e4d96SJon Cooper 			break;
1695261e4d96SJon Cooper 		if (channel->type->keep_eventq)
1696261e4d96SJon Cooper 			efx_fini_eventq(channel);
1697261e4d96SJon Cooper 	}
1698261e4d96SJon Cooper 
1699261e4d96SJon Cooper 	efx->type->irq_disable_non_ev(efx);
1700261e4d96SJon Cooper 
1701261e4d96SJon Cooper 	return rc;
17029f2cb71cSBen Hutchings }
17039f2cb71cSBen Hutchings 
1704d8291187SBen Hutchings static void efx_disable_interrupts(struct efx_nic *efx)
17059f2cb71cSBen Hutchings {
17069f2cb71cSBen Hutchings 	struct efx_channel *channel;
17079f2cb71cSBen Hutchings 
1708d8291187SBen Hutchings 	efx_soft_disable_interrupts(efx);
17099f2cb71cSBen Hutchings 
17109f2cb71cSBen Hutchings 	efx_for_each_channel(channel, efx) {
1711d8291187SBen Hutchings 		if (channel->type->keep_eventq)
17129f2cb71cSBen Hutchings 			efx_fini_eventq(channel);
17139f2cb71cSBen Hutchings 	}
1714d8291187SBen Hutchings 
171586094f7fSBen Hutchings 	efx->type->irq_disable_non_ev(efx);
17169f2cb71cSBen Hutchings }
17179f2cb71cSBen Hutchings 
1718874aeea5SJeff Kirsher static void efx_remove_interrupts(struct efx_nic *efx)
1719874aeea5SJeff Kirsher {
1720874aeea5SJeff Kirsher 	struct efx_channel *channel;
1721874aeea5SJeff Kirsher 
1722874aeea5SJeff Kirsher 	/* Remove MSI/MSI-X interrupts */
1723874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx)
1724874aeea5SJeff Kirsher 		channel->irq = 0;
1725874aeea5SJeff Kirsher 	pci_disable_msi(efx->pci_dev);
1726874aeea5SJeff Kirsher 	pci_disable_msix(efx->pci_dev);
1727874aeea5SJeff Kirsher 
1728874aeea5SJeff Kirsher 	/* Remove legacy interrupt */
1729874aeea5SJeff Kirsher 	efx->legacy_irq = 0;
1730874aeea5SJeff Kirsher }
1731874aeea5SJeff Kirsher 
1732874aeea5SJeff Kirsher static void efx_set_channels(struct efx_nic *efx)
1733874aeea5SJeff Kirsher {
1734874aeea5SJeff Kirsher 	struct efx_channel *channel;
1735874aeea5SJeff Kirsher 	struct efx_tx_queue *tx_queue;
1736874aeea5SJeff Kirsher 
1737874aeea5SJeff Kirsher 	efx->tx_channel_offset =
1738b0fbdae1SShradha Shah 		efx_separate_tx_channels ?
1739b0fbdae1SShradha Shah 		efx->n_channels - efx->n_tx_channels : 0;
1740874aeea5SJeff Kirsher 
174179d68b37SStuart Hodgson 	/* We need to mark which channels really have RX and TX
174279d68b37SStuart Hodgson 	 * queues, and adjust the TX queue numbers if we have separate
1743874aeea5SJeff Kirsher 	 * RX-only and TX-only channels.
1744874aeea5SJeff Kirsher 	 */
1745874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx) {
174679d68b37SStuart Hodgson 		if (channel->channel < efx->n_rx_channels)
174779d68b37SStuart Hodgson 			channel->rx_queue.core_index = channel->channel;
174879d68b37SStuart Hodgson 		else
174979d68b37SStuart Hodgson 			channel->rx_queue.core_index = -1;
175079d68b37SStuart Hodgson 
1751874aeea5SJeff Kirsher 		efx_for_each_channel_tx_queue(tx_queue, channel)
1752874aeea5SJeff Kirsher 			tx_queue->queue -= (efx->tx_channel_offset *
1753874aeea5SJeff Kirsher 					    EFX_TXQ_TYPES);
1754874aeea5SJeff Kirsher 	}
1755874aeea5SJeff Kirsher }
1756874aeea5SJeff Kirsher 
1757874aeea5SJeff Kirsher static int efx_probe_nic(struct efx_nic *efx)
1758874aeea5SJeff Kirsher {
1759874aeea5SJeff Kirsher 	int rc;
1760874aeea5SJeff Kirsher 
1761874aeea5SJeff Kirsher 	netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
1762874aeea5SJeff Kirsher 
1763874aeea5SJeff Kirsher 	/* Carry out hardware-type specific initialisation */
1764874aeea5SJeff Kirsher 	rc = efx->type->probe(efx);
1765874aeea5SJeff Kirsher 	if (rc)
1766874aeea5SJeff Kirsher 		return rc;
1767874aeea5SJeff Kirsher 
1768b0fbdae1SShradha Shah 	do {
1769b0fbdae1SShradha Shah 		if (!efx->max_channels || !efx->max_tx_channels) {
1770b0fbdae1SShradha Shah 			netif_err(efx, drv, efx->net_dev,
1771b0fbdae1SShradha Shah 				  "Insufficient resources to allocate"
1772b0fbdae1SShradha Shah 				  " any channels\n");
1773b0fbdae1SShradha Shah 			rc = -ENOSPC;
1774b0fbdae1SShradha Shah 			goto fail1;
1775b0fbdae1SShradha Shah 		}
1776b0fbdae1SShradha Shah 
1777b0fbdae1SShradha Shah 		/* Determine the number of channels and queues by trying
1778b0fbdae1SShradha Shah 		 * to hook in MSI-X interrupts.
1779b0fbdae1SShradha Shah 		 */
1780874aeea5SJeff Kirsher 		rc = efx_probe_interrupts(efx);
1781874aeea5SJeff Kirsher 		if (rc)
1782c15eed22SBen Hutchings 			goto fail1;
1783874aeea5SJeff Kirsher 
178452ad762bSDaniel Pieczko 		efx_set_channels(efx);
178552ad762bSDaniel Pieczko 
1786b0fbdae1SShradha Shah 		/* dimension_resources can fail with EAGAIN */
1787c15eed22SBen Hutchings 		rc = efx->type->dimension_resources(efx);
1788b0fbdae1SShradha Shah 		if (rc != 0 && rc != -EAGAIN)
1789c15eed22SBen Hutchings 			goto fail2;
179028e47c49SBen Hutchings 
1791b0fbdae1SShradha Shah 		if (rc == -EAGAIN)
1792b0fbdae1SShradha Shah 			/* try again with new max_channels */
1793b0fbdae1SShradha Shah 			efx_remove_interrupts(efx);
1794b0fbdae1SShradha Shah 
1795b0fbdae1SShradha Shah 	} while (rc == -EAGAIN);
1796b0fbdae1SShradha Shah 
1797874aeea5SJeff Kirsher 	if (efx->n_channels > 1)
179842356d9aSEdward Cree 		netdev_rss_key_fill(efx->rss_context.rx_hash_key,
179942356d9aSEdward Cree 				    sizeof(efx->rss_context.rx_hash_key));
180042356d9aSEdward Cree 	efx_set_default_rx_indir_table(efx, &efx->rss_context);
1801874aeea5SJeff Kirsher 
1802874aeea5SJeff Kirsher 	netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1803874aeea5SJeff Kirsher 	netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
1804874aeea5SJeff Kirsher 
1805874aeea5SJeff Kirsher 	/* Initialise the interrupt moderation settings */
1806539de7c5SBert Kenward 	efx->irq_mod_step_us = DIV_ROUND_UP(efx->timer_quantum_ns, 1000);
18079e393b30SBen Hutchings 	efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
18089e393b30SBen Hutchings 				true);
1809874aeea5SJeff Kirsher 
1810874aeea5SJeff Kirsher 	return 0;
1811874aeea5SJeff Kirsher 
1812c15eed22SBen Hutchings fail2:
1813c15eed22SBen Hutchings 	efx_remove_interrupts(efx);
1814c15eed22SBen Hutchings fail1:
1815874aeea5SJeff Kirsher 	efx->type->remove(efx);
1816874aeea5SJeff Kirsher 	return rc;
1817874aeea5SJeff Kirsher }
1818874aeea5SJeff Kirsher 
1819874aeea5SJeff Kirsher static void efx_remove_nic(struct efx_nic *efx)
1820874aeea5SJeff Kirsher {
1821874aeea5SJeff Kirsher 	netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
1822874aeea5SJeff Kirsher 
1823874aeea5SJeff Kirsher 	efx_remove_interrupts(efx);
1824874aeea5SJeff Kirsher 	efx->type->remove(efx);
1825874aeea5SJeff Kirsher }
1826874aeea5SJeff Kirsher 
1827add72477SBen Hutchings static int efx_probe_filters(struct efx_nic *efx)
1828add72477SBen Hutchings {
1829add72477SBen Hutchings 	int rc;
1830add72477SBen Hutchings 
18310d322413SEdward Cree 	init_rwsem(&efx->filter_sem);
1832d248953aSMartin Habets 	mutex_lock(&efx->mac_lock);
18330d322413SEdward Cree 	down_write(&efx->filter_sem);
1834add72477SBen Hutchings 	rc = efx->type->filter_table_probe(efx);
1835add72477SBen Hutchings 	if (rc)
18360d322413SEdward Cree 		goto out_unlock;
1837add72477SBen Hutchings 
1838add72477SBen Hutchings #ifdef CONFIG_RFS_ACCEL
1839add72477SBen Hutchings 	if (efx->type->offload_features & NETIF_F_NTUPLE) {
1840faf8dcc1SJon Cooper 		struct efx_channel *channel;
1841faf8dcc1SJon Cooper 		int i, success = 1;
1842faf8dcc1SJon Cooper 
1843faf8dcc1SJon Cooper 		efx_for_each_channel(channel, efx) {
1844faf8dcc1SJon Cooper 			channel->rps_flow_id =
1845faf8dcc1SJon Cooper 				kcalloc(efx->type->max_rx_ip_filters,
1846faf8dcc1SJon Cooper 					sizeof(*channel->rps_flow_id),
1847add72477SBen Hutchings 					GFP_KERNEL);
1848faf8dcc1SJon Cooper 			if (!channel->rps_flow_id)
1849faf8dcc1SJon Cooper 				success = 0;
1850faf8dcc1SJon Cooper 			else
1851faf8dcc1SJon Cooper 				for (i = 0;
1852faf8dcc1SJon Cooper 				     i < efx->type->max_rx_ip_filters;
1853faf8dcc1SJon Cooper 				     ++i)
1854faf8dcc1SJon Cooper 					channel->rps_flow_id[i] =
1855faf8dcc1SJon Cooper 						RPS_FLOW_ID_INVALID;
1856faf8dcc1SJon Cooper 		}
1857faf8dcc1SJon Cooper 
1858faf8dcc1SJon Cooper 		if (!success) {
1859faf8dcc1SJon Cooper 			efx_for_each_channel(channel, efx)
1860faf8dcc1SJon Cooper 				kfree(channel->rps_flow_id);
1861add72477SBen Hutchings 			efx->type->filter_table_remove(efx);
18620d322413SEdward Cree 			rc = -ENOMEM;
18630d322413SEdward Cree 			goto out_unlock;
1864add72477SBen Hutchings 		}
1865faf8dcc1SJon Cooper 
1866faf8dcc1SJon Cooper 		efx->rps_expire_index = efx->rps_expire_channel = 0;
1867add72477SBen Hutchings 	}
1868add72477SBen Hutchings #endif
18690d322413SEdward Cree out_unlock:
18700d322413SEdward Cree 	up_write(&efx->filter_sem);
1871d248953aSMartin Habets 	mutex_unlock(&efx->mac_lock);
18720d322413SEdward Cree 	return rc;
1873add72477SBen Hutchings }
1874add72477SBen Hutchings 
1875add72477SBen Hutchings static void efx_remove_filters(struct efx_nic *efx)
1876add72477SBen Hutchings {
1877add72477SBen Hutchings #ifdef CONFIG_RFS_ACCEL
1878faf8dcc1SJon Cooper 	struct efx_channel *channel;
1879faf8dcc1SJon Cooper 
1880faf8dcc1SJon Cooper 	efx_for_each_channel(channel, efx)
1881faf8dcc1SJon Cooper 		kfree(channel->rps_flow_id);
1882add72477SBen Hutchings #endif
18830d322413SEdward Cree 	down_write(&efx->filter_sem);
1884add72477SBen Hutchings 	efx->type->filter_table_remove(efx);
18850d322413SEdward Cree 	up_write(&efx->filter_sem);
1886add72477SBen Hutchings }
1887add72477SBen Hutchings 
1888add72477SBen Hutchings 
1889874aeea5SJeff Kirsher /**************************************************************************
1890874aeea5SJeff Kirsher  *
1891874aeea5SJeff Kirsher  * NIC startup/shutdown
1892874aeea5SJeff Kirsher  *
1893874aeea5SJeff Kirsher  *************************************************************************/
1894874aeea5SJeff Kirsher 
1895874aeea5SJeff Kirsher static int efx_probe_all(struct efx_nic *efx)
1896874aeea5SJeff Kirsher {
1897874aeea5SJeff Kirsher 	int rc;
1898874aeea5SJeff Kirsher 
1899874aeea5SJeff Kirsher 	rc = efx_probe_nic(efx);
1900874aeea5SJeff Kirsher 	if (rc) {
1901874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
1902874aeea5SJeff Kirsher 		goto fail1;
1903874aeea5SJeff Kirsher 	}
1904874aeea5SJeff Kirsher 
1905874aeea5SJeff Kirsher 	rc = efx_probe_port(efx);
1906874aeea5SJeff Kirsher 	if (rc) {
1907874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev, "failed to create port\n");
1908874aeea5SJeff Kirsher 		goto fail2;
1909874aeea5SJeff Kirsher 	}
1910874aeea5SJeff Kirsher 
19117e6d06f0SBen Hutchings 	BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
19127e6d06f0SBen Hutchings 	if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
19137e6d06f0SBen Hutchings 		rc = -EINVAL;
19147e6d06f0SBen Hutchings 		goto fail3;
19157e6d06f0SBen Hutchings 	}
1916874aeea5SJeff Kirsher 	efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
1917874aeea5SJeff Kirsher 
19186d8aaaf6SDaniel Pieczko #ifdef CONFIG_SFC_SRIOV
19196d8aaaf6SDaniel Pieczko 	rc = efx->type->vswitching_probe(efx);
19206d8aaaf6SDaniel Pieczko 	if (rc) /* not fatal; the PF will still work fine */
19216d8aaaf6SDaniel Pieczko 		netif_warn(efx, probe, efx->net_dev,
19226d8aaaf6SDaniel Pieczko 			   "failed to setup vswitching rc=%d;"
19236d8aaaf6SDaniel Pieczko 			   " VFs may not function\n", rc);
19246d8aaaf6SDaniel Pieczko #endif
19256d8aaaf6SDaniel Pieczko 
1926874aeea5SJeff Kirsher 	rc = efx_probe_filters(efx);
1927874aeea5SJeff Kirsher 	if (rc) {
1928874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev,
1929874aeea5SJeff Kirsher 			  "failed to create filter tables\n");
19306d8aaaf6SDaniel Pieczko 		goto fail4;
1931874aeea5SJeff Kirsher 	}
1932874aeea5SJeff Kirsher 
19337f967c01SBen Hutchings 	rc = efx_probe_channels(efx);
19347f967c01SBen Hutchings 	if (rc)
19356d8aaaf6SDaniel Pieczko 		goto fail5;
19367f967c01SBen Hutchings 
1937874aeea5SJeff Kirsher 	return 0;
1938874aeea5SJeff Kirsher 
19396d8aaaf6SDaniel Pieczko  fail5:
19407f967c01SBen Hutchings 	efx_remove_filters(efx);
19416d8aaaf6SDaniel Pieczko  fail4:
19426d8aaaf6SDaniel Pieczko #ifdef CONFIG_SFC_SRIOV
19436d8aaaf6SDaniel Pieczko 	efx->type->vswitching_remove(efx);
19446d8aaaf6SDaniel Pieczko #endif
1945874aeea5SJeff Kirsher  fail3:
1946874aeea5SJeff Kirsher 	efx_remove_port(efx);
1947874aeea5SJeff Kirsher  fail2:
1948874aeea5SJeff Kirsher 	efx_remove_nic(efx);
1949874aeea5SJeff Kirsher  fail1:
1950874aeea5SJeff Kirsher 	return rc;
1951874aeea5SJeff Kirsher }
1952874aeea5SJeff Kirsher 
19538b7325b4SBen Hutchings /* If the interface is supposed to be running but is not, start
19548b7325b4SBen Hutchings  * the hardware and software data path, regular activity for the port
19558b7325b4SBen Hutchings  * (MAC statistics, link polling, etc.) and schedule the port to be
19568b7325b4SBen Hutchings  * reconfigured.  Interrupts must already be enabled.  This function
19578b7325b4SBen Hutchings  * is safe to call multiple times, so long as the NIC is not disabled.
19588b7325b4SBen Hutchings  * Requires the RTNL lock.
19599f2cb71cSBen Hutchings  */
1960874aeea5SJeff Kirsher static void efx_start_all(struct efx_nic *efx)
1961874aeea5SJeff Kirsher {
1962874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
19638b7325b4SBen Hutchings 	BUG_ON(efx->state == STATE_DISABLED);
1964874aeea5SJeff Kirsher 
1965874aeea5SJeff Kirsher 	/* Check that it is appropriate to restart the interface. All
1966874aeea5SJeff Kirsher 	 * of these flags are safe to read under just the rtnl lock */
1967e283546cSEdward Cree 	if (efx->port_enabled || !netif_running(efx->net_dev) ||
1968e283546cSEdward Cree 	    efx->reset_pending)
1969874aeea5SJeff Kirsher 		return;
1970874aeea5SJeff Kirsher 
1971874aeea5SJeff Kirsher 	efx_start_port(efx);
19729f2cb71cSBen Hutchings 	efx_start_datapath(efx);
1973874aeea5SJeff Kirsher 
1974626950dbSAlexandre Rames 	/* Start the hardware monitor if there is one */
1975626950dbSAlexandre Rames 	if (efx->type->monitor != NULL)
1976874aeea5SJeff Kirsher 		queue_delayed_work(efx->workqueue, &efx->monitor_work,
1977874aeea5SJeff Kirsher 				   efx_monitor_interval);
1978626950dbSAlexandre Rames 
19795a6681e2SEdward Cree 	/* Link state detection is normally event-driven; we have
1980626950dbSAlexandre Rames 	 * to poll now because we could have missed a change
1981626950dbSAlexandre Rames 	 */
1982874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
1983874aeea5SJeff Kirsher 	if (efx->phy_op->poll(efx))
1984874aeea5SJeff Kirsher 		efx_link_status_changed(efx);
1985874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
1986874aeea5SJeff Kirsher 
1987874aeea5SJeff Kirsher 	efx->type->start_stats(efx);
1988f8f3b5aeSJon Cooper 	efx->type->pull_stats(efx);
1989f8f3b5aeSJon Cooper 	spin_lock_bh(&efx->stats_lock);
1990f8f3b5aeSJon Cooper 	efx->type->update_stats(efx, NULL, NULL);
1991f8f3b5aeSJon Cooper 	spin_unlock_bh(&efx->stats_lock);
1992874aeea5SJeff Kirsher }
1993874aeea5SJeff Kirsher 
19948b7325b4SBen Hutchings /* Quiesce the hardware and software data path, and regular activity
19958b7325b4SBen Hutchings  * for the port without bringing the link down.  Safe to call multiple
19968b7325b4SBen Hutchings  * times with the NIC in almost any state, but interrupts should be
19978b7325b4SBen Hutchings  * enabled.  Requires the RTNL lock.
19988b7325b4SBen Hutchings  */
1999874aeea5SJeff Kirsher static void efx_stop_all(struct efx_nic *efx)
2000874aeea5SJeff Kirsher {
2001874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
2002874aeea5SJeff Kirsher 
2003874aeea5SJeff Kirsher 	/* port_enabled can be read safely under the rtnl lock */
2004874aeea5SJeff Kirsher 	if (!efx->port_enabled)
2005874aeea5SJeff Kirsher 		return;
2006874aeea5SJeff Kirsher 
2007f8f3b5aeSJon Cooper 	/* update stats before we go down so we can accurately count
2008f8f3b5aeSJon Cooper 	 * rx_nodesc_drops
2009f8f3b5aeSJon Cooper 	 */
2010f8f3b5aeSJon Cooper 	efx->type->pull_stats(efx);
2011f8f3b5aeSJon Cooper 	spin_lock_bh(&efx->stats_lock);
2012f8f3b5aeSJon Cooper 	efx->type->update_stats(efx, NULL, NULL);
2013f8f3b5aeSJon Cooper 	spin_unlock_bh(&efx->stats_lock);
2014874aeea5SJeff Kirsher 	efx->type->stop_stats(efx);
2015874aeea5SJeff Kirsher 	efx_stop_port(efx);
2016874aeea5SJeff Kirsher 
201729c69a48SBen Hutchings 	/* Stop the kernel transmit interface.  This is only valid if
201829c69a48SBen Hutchings 	 * the device is stopped or detached; otherwise the watchdog
201929c69a48SBen Hutchings 	 * may fire immediately.
202029c69a48SBen Hutchings 	 */
202129c69a48SBen Hutchings 	WARN_ON(netif_running(efx->net_dev) &&
202229c69a48SBen Hutchings 		netif_device_present(efx->net_dev));
20239f2cb71cSBen Hutchings 	netif_tx_disable(efx->net_dev);
20249f2cb71cSBen Hutchings 
20259f2cb71cSBen Hutchings 	efx_stop_datapath(efx);
2026874aeea5SJeff Kirsher }
2027874aeea5SJeff Kirsher 
2028874aeea5SJeff Kirsher static void efx_remove_all(struct efx_nic *efx)
2029874aeea5SJeff Kirsher {
2030e45a4fedSCharles McLachlan 	rtnl_lock();
2031e45a4fedSCharles McLachlan 	efx_xdp_setup_prog(efx, NULL);
2032e45a4fedSCharles McLachlan 	rtnl_unlock();
2033e45a4fedSCharles McLachlan 
2034874aeea5SJeff Kirsher 	efx_remove_channels(efx);
20357f967c01SBen Hutchings 	efx_remove_filters(efx);
20366d8aaaf6SDaniel Pieczko #ifdef CONFIG_SFC_SRIOV
20376d8aaaf6SDaniel Pieczko 	efx->type->vswitching_remove(efx);
20386d8aaaf6SDaniel Pieczko #endif
2039874aeea5SJeff Kirsher 	efx_remove_port(efx);
2040874aeea5SJeff Kirsher 	efx_remove_nic(efx);
2041874aeea5SJeff Kirsher }
2042874aeea5SJeff Kirsher 
2043874aeea5SJeff Kirsher /**************************************************************************
2044874aeea5SJeff Kirsher  *
2045874aeea5SJeff Kirsher  * Interrupt moderation
2046874aeea5SJeff Kirsher  *
2047874aeea5SJeff Kirsher  **************************************************************************/
2048539de7c5SBert Kenward unsigned int efx_usecs_to_ticks(struct efx_nic *efx, unsigned int usecs)
2049874aeea5SJeff Kirsher {
2050b548f976SBen Hutchings 	if (usecs == 0)
2051b548f976SBen Hutchings 		return 0;
2052539de7c5SBert Kenward 	if (usecs * 1000 < efx->timer_quantum_ns)
2053874aeea5SJeff Kirsher 		return 1; /* never round down to 0 */
2054539de7c5SBert Kenward 	return usecs * 1000 / efx->timer_quantum_ns;
2055539de7c5SBert Kenward }
2056539de7c5SBert Kenward 
2057539de7c5SBert Kenward unsigned int efx_ticks_to_usecs(struct efx_nic *efx, unsigned int ticks)
2058539de7c5SBert Kenward {
2059539de7c5SBert Kenward 	/* We must round up when converting ticks to microseconds
2060539de7c5SBert Kenward 	 * because we round down when converting the other way.
2061539de7c5SBert Kenward 	 */
2062539de7c5SBert Kenward 	return DIV_ROUND_UP(ticks * efx->timer_quantum_ns, 1000);
2063874aeea5SJeff Kirsher }
2064874aeea5SJeff Kirsher 
2065874aeea5SJeff Kirsher /* Set interrupt moderation parameters */
20669e393b30SBen Hutchings int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
20679e393b30SBen Hutchings 			    unsigned int rx_usecs, bool rx_adaptive,
20689e393b30SBen Hutchings 			    bool rx_may_override_tx)
2069874aeea5SJeff Kirsher {
2070874aeea5SJeff Kirsher 	struct efx_channel *channel;
2071d95e329aSBert Kenward 	unsigned int timer_max_us;
2072d95e329aSBert Kenward 
2073874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
2074874aeea5SJeff Kirsher 
2075d95e329aSBert Kenward 	timer_max_us = efx->timer_max_ns / 1000;
2076d95e329aSBert Kenward 
2077d95e329aSBert Kenward 	if (tx_usecs > timer_max_us || rx_usecs > timer_max_us)
20789e393b30SBen Hutchings 		return -EINVAL;
20799e393b30SBen Hutchings 
2080539de7c5SBert Kenward 	if (tx_usecs != rx_usecs && efx->tx_channel_offset == 0 &&
20819e393b30SBen Hutchings 	    !rx_may_override_tx) {
20829e393b30SBen Hutchings 		netif_err(efx, drv, efx->net_dev, "Channels are shared. "
20839e393b30SBen Hutchings 			  "RX and TX IRQ moderation must be equal\n");
20849e393b30SBen Hutchings 		return -EINVAL;
20859e393b30SBen Hutchings 	}
20869e393b30SBen Hutchings 
2087874aeea5SJeff Kirsher 	efx->irq_rx_adaptive = rx_adaptive;
2088539de7c5SBert Kenward 	efx->irq_rx_moderation_us = rx_usecs;
2089874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx) {
2090874aeea5SJeff Kirsher 		if (efx_channel_has_rx_queue(channel))
2091539de7c5SBert Kenward 			channel->irq_moderation_us = rx_usecs;
2092874aeea5SJeff Kirsher 		else if (efx_channel_has_tx_queues(channel))
2093539de7c5SBert Kenward 			channel->irq_moderation_us = tx_usecs;
2094874aeea5SJeff Kirsher 	}
20959e393b30SBen Hutchings 
20969e393b30SBen Hutchings 	return 0;
2097874aeea5SJeff Kirsher }
2098874aeea5SJeff Kirsher 
2099a0c4faf5SBen Hutchings void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
2100a0c4faf5SBen Hutchings 			    unsigned int *rx_usecs, bool *rx_adaptive)
2101a0c4faf5SBen Hutchings {
2102a0c4faf5SBen Hutchings 	*rx_adaptive = efx->irq_rx_adaptive;
2103539de7c5SBert Kenward 	*rx_usecs = efx->irq_rx_moderation_us;
2104a0c4faf5SBen Hutchings 
2105a0c4faf5SBen Hutchings 	/* If channels are shared between RX and TX, so is IRQ
2106a0c4faf5SBen Hutchings 	 * moderation.  Otherwise, IRQ moderation is the same for all
2107a0c4faf5SBen Hutchings 	 * TX channels and is not adaptive.
2108a0c4faf5SBen Hutchings 	 */
2109539de7c5SBert Kenward 	if (efx->tx_channel_offset == 0) {
2110a0c4faf5SBen Hutchings 		*tx_usecs = *rx_usecs;
2111539de7c5SBert Kenward 	} else {
2112539de7c5SBert Kenward 		struct efx_channel *tx_channel;
2113539de7c5SBert Kenward 
2114539de7c5SBert Kenward 		tx_channel = efx->channel[efx->tx_channel_offset];
2115539de7c5SBert Kenward 		*tx_usecs = tx_channel->irq_moderation_us;
2116539de7c5SBert Kenward 	}
2117a0c4faf5SBen Hutchings }
2118a0c4faf5SBen Hutchings 
2119874aeea5SJeff Kirsher /**************************************************************************
2120874aeea5SJeff Kirsher  *
2121874aeea5SJeff Kirsher  * Hardware monitor
2122874aeea5SJeff Kirsher  *
2123874aeea5SJeff Kirsher  **************************************************************************/
2124874aeea5SJeff Kirsher 
2125874aeea5SJeff Kirsher /* Run periodically off the general workqueue */
2126874aeea5SJeff Kirsher static void efx_monitor(struct work_struct *data)
2127874aeea5SJeff Kirsher {
2128874aeea5SJeff Kirsher 	struct efx_nic *efx = container_of(data, struct efx_nic,
2129874aeea5SJeff Kirsher 					   monitor_work.work);
2130874aeea5SJeff Kirsher 
2131874aeea5SJeff Kirsher 	netif_vdbg(efx, timer, efx->net_dev,
2132874aeea5SJeff Kirsher 		   "hardware monitor executing on CPU %d\n",
2133874aeea5SJeff Kirsher 		   raw_smp_processor_id());
2134874aeea5SJeff Kirsher 	BUG_ON(efx->type->monitor == NULL);
2135874aeea5SJeff Kirsher 
2136874aeea5SJeff Kirsher 	/* If the mac_lock is already held then it is likely a port
2137874aeea5SJeff Kirsher 	 * reconfiguration is already in place, which will likely do
2138874aeea5SJeff Kirsher 	 * most of the work of monitor() anyway. */
2139874aeea5SJeff Kirsher 	if (mutex_trylock(&efx->mac_lock)) {
2140874aeea5SJeff Kirsher 		if (efx->port_enabled)
2141874aeea5SJeff Kirsher 			efx->type->monitor(efx);
2142874aeea5SJeff Kirsher 		mutex_unlock(&efx->mac_lock);
2143874aeea5SJeff Kirsher 	}
2144874aeea5SJeff Kirsher 
2145874aeea5SJeff Kirsher 	queue_delayed_work(efx->workqueue, &efx->monitor_work,
2146874aeea5SJeff Kirsher 			   efx_monitor_interval);
2147874aeea5SJeff Kirsher }
2148874aeea5SJeff Kirsher 
2149874aeea5SJeff Kirsher /**************************************************************************
2150874aeea5SJeff Kirsher  *
2151874aeea5SJeff Kirsher  * ioctls
2152874aeea5SJeff Kirsher  *
2153874aeea5SJeff Kirsher  *************************************************************************/
2154874aeea5SJeff Kirsher 
2155874aeea5SJeff Kirsher /* Net device ioctl
2156874aeea5SJeff Kirsher  * Context: process, rtnl_lock() held.
2157874aeea5SJeff Kirsher  */
2158874aeea5SJeff Kirsher static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
2159874aeea5SJeff Kirsher {
2160874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
2161874aeea5SJeff Kirsher 	struct mii_ioctl_data *data = if_mii(ifr);
2162874aeea5SJeff Kirsher 
21637c236c43SStuart Hodgson 	if (cmd == SIOCSHWTSTAMP)
2164433dc9b3SBen Hutchings 		return efx_ptp_set_ts_config(efx, ifr);
2165433dc9b3SBen Hutchings 	if (cmd == SIOCGHWTSTAMP)
2166433dc9b3SBen Hutchings 		return efx_ptp_get_ts_config(efx, ifr);
21677c236c43SStuart Hodgson 
2168874aeea5SJeff Kirsher 	/* Convert phy_id from older PRTAD/DEVAD format */
2169874aeea5SJeff Kirsher 	if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
2170874aeea5SJeff Kirsher 	    (data->phy_id & 0xfc00) == 0x0400)
2171874aeea5SJeff Kirsher 		data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
2172874aeea5SJeff Kirsher 
2173874aeea5SJeff Kirsher 	return mdio_mii_ioctl(&efx->mdio, data, cmd);
2174874aeea5SJeff Kirsher }
2175874aeea5SJeff Kirsher 
2176874aeea5SJeff Kirsher /**************************************************************************
2177874aeea5SJeff Kirsher  *
2178874aeea5SJeff Kirsher  * NAPI interface
2179874aeea5SJeff Kirsher  *
2180874aeea5SJeff Kirsher  **************************************************************************/
2181874aeea5SJeff Kirsher 
21827f967c01SBen Hutchings static void efx_init_napi_channel(struct efx_channel *channel)
2183874aeea5SJeff Kirsher {
21847f967c01SBen Hutchings 	struct efx_nic *efx = channel->efx;
2185874aeea5SJeff Kirsher 
2186874aeea5SJeff Kirsher 	channel->napi_dev = efx->net_dev;
2187874aeea5SJeff Kirsher 	netif_napi_add(channel->napi_dev, &channel->napi_str,
2188874aeea5SJeff Kirsher 		       efx_poll, napi_weight);
2189874aeea5SJeff Kirsher }
21907f967c01SBen Hutchings 
21917f967c01SBen Hutchings static void efx_init_napi(struct efx_nic *efx)
21927f967c01SBen Hutchings {
21937f967c01SBen Hutchings 	struct efx_channel *channel;
21947f967c01SBen Hutchings 
21957f967c01SBen Hutchings 	efx_for_each_channel(channel, efx)
21967f967c01SBen Hutchings 		efx_init_napi_channel(channel);
2197874aeea5SJeff Kirsher }
2198874aeea5SJeff Kirsher 
2199874aeea5SJeff Kirsher static void efx_fini_napi_channel(struct efx_channel *channel)
2200874aeea5SJeff Kirsher {
2201973334a1SEric Dumazet 	if (channel->napi_dev)
2202874aeea5SJeff Kirsher 		netif_napi_del(&channel->napi_str);
2203973334a1SEric Dumazet 
2204874aeea5SJeff Kirsher 	channel->napi_dev = NULL;
2205874aeea5SJeff Kirsher }
2206874aeea5SJeff Kirsher 
2207874aeea5SJeff Kirsher static void efx_fini_napi(struct efx_nic *efx)
2208874aeea5SJeff Kirsher {
2209874aeea5SJeff Kirsher 	struct efx_channel *channel;
2210874aeea5SJeff Kirsher 
2211874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx)
2212874aeea5SJeff Kirsher 		efx_fini_napi_channel(channel);
2213874aeea5SJeff Kirsher }
2214874aeea5SJeff Kirsher 
2215874aeea5SJeff Kirsher /**************************************************************************
2216874aeea5SJeff Kirsher  *
2217874aeea5SJeff Kirsher  * Kernel net device interface
2218874aeea5SJeff Kirsher  *
2219874aeea5SJeff Kirsher  *************************************************************************/
2220874aeea5SJeff Kirsher 
2221874aeea5SJeff Kirsher /* Context: process, rtnl_lock() held. */
2222e340be92SShradha Shah int efx_net_open(struct net_device *net_dev)
2223874aeea5SJeff Kirsher {
2224874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
22258b7325b4SBen Hutchings 	int rc;
22268b7325b4SBen Hutchings 
2227874aeea5SJeff Kirsher 	netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
2228874aeea5SJeff Kirsher 		  raw_smp_processor_id());
2229874aeea5SJeff Kirsher 
22308b7325b4SBen Hutchings 	rc = efx_check_disabled(efx);
22318b7325b4SBen Hutchings 	if (rc)
22328b7325b4SBen Hutchings 		return rc;
2233874aeea5SJeff Kirsher 	if (efx->phy_mode & PHY_MODE_SPECIAL)
2234874aeea5SJeff Kirsher 		return -EBUSY;
2235874aeea5SJeff Kirsher 	if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
2236874aeea5SJeff Kirsher 		return -EIO;
2237874aeea5SJeff Kirsher 
2238874aeea5SJeff Kirsher 	/* Notify the kernel of the link state polled during driver load,
2239874aeea5SJeff Kirsher 	 * before the monitor starts running */
2240874aeea5SJeff Kirsher 	efx_link_status_changed(efx);
2241874aeea5SJeff Kirsher 
2242874aeea5SJeff Kirsher 	efx_start_all(efx);
22439c568fd8SPeter Dunning 	if (efx->state == STATE_DISABLED || efx->reset_pending)
22449c568fd8SPeter Dunning 		netif_device_detach(efx->net_dev);
2245dd40781eSBen Hutchings 	efx_selftest_async_start(efx);
2246874aeea5SJeff Kirsher 	return 0;
2247874aeea5SJeff Kirsher }
2248874aeea5SJeff Kirsher 
2249874aeea5SJeff Kirsher /* Context: process, rtnl_lock() held.
2250874aeea5SJeff Kirsher  * Note that the kernel will ignore our return code; this method
2251874aeea5SJeff Kirsher  * should really be a void.
2252874aeea5SJeff Kirsher  */
2253e340be92SShradha Shah int efx_net_stop(struct net_device *net_dev)
2254874aeea5SJeff Kirsher {
2255874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
2256874aeea5SJeff Kirsher 
2257874aeea5SJeff Kirsher 	netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
2258874aeea5SJeff Kirsher 		  raw_smp_processor_id());
2259874aeea5SJeff Kirsher 
2260874aeea5SJeff Kirsher 	/* Stop the device and flush all the channels */
2261874aeea5SJeff Kirsher 	efx_stop_all(efx);
2262874aeea5SJeff Kirsher 
2263874aeea5SJeff Kirsher 	return 0;
2264874aeea5SJeff Kirsher }
2265874aeea5SJeff Kirsher 
2266874aeea5SJeff Kirsher /* Context: process, dev_base_lock or RTNL held, non-blocking. */
2267bc1f4470Sstephen hemminger static void efx_net_stats(struct net_device *net_dev,
22682aa9ef11SBen Hutchings 			  struct rtnl_link_stats64 *stats)
2269874aeea5SJeff Kirsher {
2270874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
2271874aeea5SJeff Kirsher 
2272874aeea5SJeff Kirsher 	spin_lock_bh(&efx->stats_lock);
2273cd0ecc9aSBen Hutchings 	efx->type->update_stats(efx, NULL, stats);
22741cb34522SBen Hutchings 	spin_unlock_bh(&efx->stats_lock);
2275874aeea5SJeff Kirsher }
2276874aeea5SJeff Kirsher 
2277874aeea5SJeff Kirsher /* Context: netif_tx_lock held, BHs disabled. */
2278874aeea5SJeff Kirsher static void efx_watchdog(struct net_device *net_dev)
2279874aeea5SJeff Kirsher {
2280874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
2281874aeea5SJeff Kirsher 
2282874aeea5SJeff Kirsher 	netif_err(efx, tx_err, efx->net_dev,
2283874aeea5SJeff Kirsher 		  "TX stuck with port_enabled=%d: resetting channels\n",
2284874aeea5SJeff Kirsher 		  efx->port_enabled);
2285874aeea5SJeff Kirsher 
2286874aeea5SJeff Kirsher 	efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
2287874aeea5SJeff Kirsher }
2288874aeea5SJeff Kirsher 
2289e45a4fedSCharles McLachlan static unsigned int efx_xdp_max_mtu(struct efx_nic *efx)
2290e45a4fedSCharles McLachlan {
2291e45a4fedSCharles McLachlan 	/* The maximum MTU that we can fit in a single page, allowing for
2292e45a4fedSCharles McLachlan 	 * framing, overhead and XDP headroom.
2293e45a4fedSCharles McLachlan 	 */
2294e45a4fedSCharles McLachlan 	int overhead = EFX_MAX_FRAME_LEN(0) + sizeof(struct efx_rx_page_state) +
2295e45a4fedSCharles McLachlan 		       efx->rx_prefix_size + efx->type->rx_buffer_padding +
2296e45a4fedSCharles McLachlan 		       efx->rx_ip_align + XDP_PACKET_HEADROOM;
2297e45a4fedSCharles McLachlan 
2298e45a4fedSCharles McLachlan 	return PAGE_SIZE - overhead;
2299e45a4fedSCharles McLachlan }
2300874aeea5SJeff Kirsher 
2301874aeea5SJeff Kirsher /* Context: process, rtnl_lock() held. */
2302874aeea5SJeff Kirsher static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
2303874aeea5SJeff Kirsher {
2304874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
23058b7325b4SBen Hutchings 	int rc;
2306874aeea5SJeff Kirsher 
23078b7325b4SBen Hutchings 	rc = efx_check_disabled(efx);
23088b7325b4SBen Hutchings 	if (rc)
23098b7325b4SBen Hutchings 		return rc;
2310874aeea5SJeff Kirsher 
2311e45a4fedSCharles McLachlan 	if (rtnl_dereference(efx->xdp_prog) &&
2312e45a4fedSCharles McLachlan 	    new_mtu > efx_xdp_max_mtu(efx)) {
2313e45a4fedSCharles McLachlan 		netif_err(efx, drv, efx->net_dev,
2314e45a4fedSCharles McLachlan 			  "Requested MTU of %d too big for XDP (max: %d)\n",
2315e45a4fedSCharles McLachlan 			  new_mtu, efx_xdp_max_mtu(efx));
2316e45a4fedSCharles McLachlan 		return -EINVAL;
2317e45a4fedSCharles McLachlan 	}
2318e45a4fedSCharles McLachlan 
2319874aeea5SJeff Kirsher 	netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
2320874aeea5SJeff Kirsher 
232129c69a48SBen Hutchings 	efx_device_detach_sync(efx);
232229c69a48SBen Hutchings 	efx_stop_all(efx);
232329c69a48SBen Hutchings 
2324874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
2325874aeea5SJeff Kirsher 	net_dev->mtu = new_mtu;
23260d322413SEdward Cree 	efx_mac_reconfigure(efx);
2327874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
2328874aeea5SJeff Kirsher 
2329874aeea5SJeff Kirsher 	efx_start_all(efx);
23309c568fd8SPeter Dunning 	efx_device_attach_if_not_resetting(efx);
23316c8eef4aSBen Hutchings 	return 0;
2332874aeea5SJeff Kirsher }
2333874aeea5SJeff Kirsher 
2334874aeea5SJeff Kirsher static int efx_set_mac_address(struct net_device *net_dev, void *data)
2335874aeea5SJeff Kirsher {
2336874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
2337874aeea5SJeff Kirsher 	struct sockaddr *addr = data;
2338e0b3ae30SBen Hutchings 	u8 *new_addr = addr->sa_data;
2339cfc77c2fSShradha Shah 	u8 old_addr[6];
2340cfc77c2fSShradha Shah 	int rc;
2341874aeea5SJeff Kirsher 
2342874aeea5SJeff Kirsher 	if (!is_valid_ether_addr(new_addr)) {
2343874aeea5SJeff Kirsher 		netif_err(efx, drv, efx->net_dev,
2344874aeea5SJeff Kirsher 			  "invalid ethernet MAC address requested: %pM\n",
2345874aeea5SJeff Kirsher 			  new_addr);
2346504f9b5aSDanny Kukawka 		return -EADDRNOTAVAIL;
2347874aeea5SJeff Kirsher 	}
2348874aeea5SJeff Kirsher 
2349cfc77c2fSShradha Shah 	/* save old address */
2350cfc77c2fSShradha Shah 	ether_addr_copy(old_addr, net_dev->dev_addr);
2351cd84ff4dSEdward Cree 	ether_addr_copy(net_dev->dev_addr, new_addr);
2352910c8789SShradha Shah 	if (efx->type->set_mac_address) {
2353910c8789SShradha Shah 		rc = efx->type->set_mac_address(efx);
2354cfc77c2fSShradha Shah 		if (rc) {
2355cfc77c2fSShradha Shah 			ether_addr_copy(net_dev->dev_addr, old_addr);
2356cfc77c2fSShradha Shah 			return rc;
2357cfc77c2fSShradha Shah 		}
2358cfc77c2fSShradha Shah 	}
2359874aeea5SJeff Kirsher 
2360874aeea5SJeff Kirsher 	/* Reconfigure the MAC */
2361874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
23620d322413SEdward Cree 	efx_mac_reconfigure(efx);
2363874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
2364874aeea5SJeff Kirsher 
2365874aeea5SJeff Kirsher 	return 0;
2366874aeea5SJeff Kirsher }
2367874aeea5SJeff Kirsher 
2368874aeea5SJeff Kirsher /* Context: netif_addr_lock held, BHs disabled. */
23690fca8c97SBen Hutchings static void efx_set_rx_mode(struct net_device *net_dev)
2370874aeea5SJeff Kirsher {
2371874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
2372874aeea5SJeff Kirsher 
2373874aeea5SJeff Kirsher 	if (efx->port_enabled)
2374874aeea5SJeff Kirsher 		queue_work(efx->workqueue, &efx->mac_work);
2375874aeea5SJeff Kirsher 	/* Otherwise efx_start_port() will do this */
2376874aeea5SJeff Kirsher }
2377874aeea5SJeff Kirsher 
2378c8f44affSMichał Mirosław static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
2379874aeea5SJeff Kirsher {
2380874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
23814a53ea8aSAndrew Rybchenko 	int rc;
2382874aeea5SJeff Kirsher 
2383874aeea5SJeff Kirsher 	/* If disabling RX n-tuple filtering, clear existing filters */
23844a53ea8aSAndrew Rybchenko 	if (net_dev->features & ~data & NETIF_F_NTUPLE) {
23854a53ea8aSAndrew Rybchenko 		rc = efx->type->filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
23864a53ea8aSAndrew Rybchenko 		if (rc)
23874a53ea8aSAndrew Rybchenko 			return rc;
23884a53ea8aSAndrew Rybchenko 	}
23894a53ea8aSAndrew Rybchenko 
23906978729fSEdward Cree 	/* If Rx VLAN filter is changed, update filters via mac_reconfigure.
23916978729fSEdward Cree 	 * If rx-fcs is changed, mac_reconfigure updates that too.
23926978729fSEdward Cree 	 */
23936978729fSEdward Cree 	if ((net_dev->features ^ data) & (NETIF_F_HW_VLAN_CTAG_FILTER |
23946978729fSEdward Cree 					  NETIF_F_RXFCS)) {
23954a53ea8aSAndrew Rybchenko 		/* efx_set_rx_mode() will schedule MAC work to update filters
23964a53ea8aSAndrew Rybchenko 		 * when a new features are finally set in net_dev.
23974a53ea8aSAndrew Rybchenko 		 */
23984a53ea8aSAndrew Rybchenko 		efx_set_rx_mode(net_dev);
23994a53ea8aSAndrew Rybchenko 	}
2400874aeea5SJeff Kirsher 
2401874aeea5SJeff Kirsher 	return 0;
2402874aeea5SJeff Kirsher }
2403874aeea5SJeff Kirsher 
2404b40296fcSWei Yongjun static int efx_get_phys_port_id(struct net_device *net_dev,
240508a7b29bSBert Kenward 				struct netdev_phys_item_id *ppid)
240608a7b29bSBert Kenward {
240708a7b29bSBert Kenward 	struct efx_nic *efx = netdev_priv(net_dev);
240808a7b29bSBert Kenward 
240908a7b29bSBert Kenward 	if (efx->type->get_phys_port_id)
241008a7b29bSBert Kenward 		return efx->type->get_phys_port_id(efx, ppid);
241108a7b29bSBert Kenward 	else
241208a7b29bSBert Kenward 		return -EOPNOTSUPP;
241308a7b29bSBert Kenward }
241408a7b29bSBert Kenward 
2415ac019f08SBert Kenward static int efx_get_phys_port_name(struct net_device *net_dev,
2416ac019f08SBert Kenward 				  char *name, size_t len)
2417ac019f08SBert Kenward {
2418ac019f08SBert Kenward 	struct efx_nic *efx = netdev_priv(net_dev);
2419ac019f08SBert Kenward 
2420ac019f08SBert Kenward 	if (snprintf(name, len, "p%u", efx->port_num) >= len)
2421ac019f08SBert Kenward 		return -EINVAL;
2422ac019f08SBert Kenward 	return 0;
2423ac019f08SBert Kenward }
2424ac019f08SBert Kenward 
24254a53ea8aSAndrew Rybchenko static int efx_vlan_rx_add_vid(struct net_device *net_dev, __be16 proto, u16 vid)
24264a53ea8aSAndrew Rybchenko {
24274a53ea8aSAndrew Rybchenko 	struct efx_nic *efx = netdev_priv(net_dev);
24284a53ea8aSAndrew Rybchenko 
24294a53ea8aSAndrew Rybchenko 	if (efx->type->vlan_rx_add_vid)
24304a53ea8aSAndrew Rybchenko 		return efx->type->vlan_rx_add_vid(efx, proto, vid);
24314a53ea8aSAndrew Rybchenko 	else
24324a53ea8aSAndrew Rybchenko 		return -EOPNOTSUPP;
24334a53ea8aSAndrew Rybchenko }
24344a53ea8aSAndrew Rybchenko 
24354a53ea8aSAndrew Rybchenko static int efx_vlan_rx_kill_vid(struct net_device *net_dev, __be16 proto, u16 vid)
24364a53ea8aSAndrew Rybchenko {
24374a53ea8aSAndrew Rybchenko 	struct efx_nic *efx = netdev_priv(net_dev);
24384a53ea8aSAndrew Rybchenko 
24394a53ea8aSAndrew Rybchenko 	if (efx->type->vlan_rx_kill_vid)
24404a53ea8aSAndrew Rybchenko 		return efx->type->vlan_rx_kill_vid(efx, proto, vid);
24414a53ea8aSAndrew Rybchenko 	else
24424a53ea8aSAndrew Rybchenko 		return -EOPNOTSUPP;
24434a53ea8aSAndrew Rybchenko }
24444a53ea8aSAndrew Rybchenko 
2445e5fbd977SJon Cooper static int efx_udp_tunnel_type_map(enum udp_parsable_tunnel_type in)
2446e5fbd977SJon Cooper {
2447e5fbd977SJon Cooper 	switch (in) {
2448e5fbd977SJon Cooper 	case UDP_TUNNEL_TYPE_VXLAN:
2449e5fbd977SJon Cooper 		return TUNNEL_ENCAP_UDP_PORT_ENTRY_VXLAN;
2450e5fbd977SJon Cooper 	case UDP_TUNNEL_TYPE_GENEVE:
2451e5fbd977SJon Cooper 		return TUNNEL_ENCAP_UDP_PORT_ENTRY_GENEVE;
2452e5fbd977SJon Cooper 	default:
2453e5fbd977SJon Cooper 		return -1;
2454e5fbd977SJon Cooper 	}
2455e5fbd977SJon Cooper }
2456e5fbd977SJon Cooper 
2457e5fbd977SJon Cooper static void efx_udp_tunnel_add(struct net_device *dev, struct udp_tunnel_info *ti)
2458e5fbd977SJon Cooper {
2459e5fbd977SJon Cooper 	struct efx_nic *efx = netdev_priv(dev);
2460e5fbd977SJon Cooper 	struct efx_udp_tunnel tnl;
2461e5fbd977SJon Cooper 	int efx_tunnel_type;
2462e5fbd977SJon Cooper 
2463e5fbd977SJon Cooper 	efx_tunnel_type = efx_udp_tunnel_type_map(ti->type);
2464e5fbd977SJon Cooper 	if (efx_tunnel_type < 0)
2465e5fbd977SJon Cooper 		return;
2466e5fbd977SJon Cooper 
2467e5fbd977SJon Cooper 	tnl.type = (u16)efx_tunnel_type;
2468e5fbd977SJon Cooper 	tnl.port = ti->port;
2469e5fbd977SJon Cooper 
2470e5fbd977SJon Cooper 	if (efx->type->udp_tnl_add_port)
2471e5fbd977SJon Cooper 		(void)efx->type->udp_tnl_add_port(efx, tnl);
2472e5fbd977SJon Cooper }
2473e5fbd977SJon Cooper 
2474e5fbd977SJon Cooper static void efx_udp_tunnel_del(struct net_device *dev, struct udp_tunnel_info *ti)
2475e5fbd977SJon Cooper {
2476e5fbd977SJon Cooper 	struct efx_nic *efx = netdev_priv(dev);
2477e5fbd977SJon Cooper 	struct efx_udp_tunnel tnl;
2478e5fbd977SJon Cooper 	int efx_tunnel_type;
2479e5fbd977SJon Cooper 
2480e5fbd977SJon Cooper 	efx_tunnel_type = efx_udp_tunnel_type_map(ti->type);
2481e5fbd977SJon Cooper 	if (efx_tunnel_type < 0)
2482e5fbd977SJon Cooper 		return;
2483e5fbd977SJon Cooper 
2484e5fbd977SJon Cooper 	tnl.type = (u16)efx_tunnel_type;
2485e5fbd977SJon Cooper 	tnl.port = ti->port;
2486e5fbd977SJon Cooper 
2487c04ca616SDan Carpenter 	if (efx->type->udp_tnl_del_port)
2488e5fbd977SJon Cooper 		(void)efx->type->udp_tnl_del_port(efx, tnl);
2489e5fbd977SJon Cooper }
2490e5fbd977SJon Cooper 
24917fa8d547SShradha Shah static const struct net_device_ops efx_netdev_ops = {
2492874aeea5SJeff Kirsher 	.ndo_open		= efx_net_open,
2493874aeea5SJeff Kirsher 	.ndo_stop		= efx_net_stop,
2494874aeea5SJeff Kirsher 	.ndo_get_stats64	= efx_net_stats,
2495874aeea5SJeff Kirsher 	.ndo_tx_timeout		= efx_watchdog,
2496874aeea5SJeff Kirsher 	.ndo_start_xmit		= efx_hard_start_xmit,
2497874aeea5SJeff Kirsher 	.ndo_validate_addr	= eth_validate_addr,
2498874aeea5SJeff Kirsher 	.ndo_do_ioctl		= efx_ioctl,
2499874aeea5SJeff Kirsher 	.ndo_change_mtu		= efx_change_mtu,
2500874aeea5SJeff Kirsher 	.ndo_set_mac_address	= efx_set_mac_address,
25010fca8c97SBen Hutchings 	.ndo_set_rx_mode	= efx_set_rx_mode,
2502874aeea5SJeff Kirsher 	.ndo_set_features	= efx_set_features,
25034a53ea8aSAndrew Rybchenko 	.ndo_vlan_rx_add_vid	= efx_vlan_rx_add_vid,
25044a53ea8aSAndrew Rybchenko 	.ndo_vlan_rx_kill_vid	= efx_vlan_rx_kill_vid,
2505cd2d5b52SBen Hutchings #ifdef CONFIG_SFC_SRIOV
25067fa8d547SShradha Shah 	.ndo_set_vf_mac		= efx_sriov_set_vf_mac,
25077fa8d547SShradha Shah 	.ndo_set_vf_vlan	= efx_sriov_set_vf_vlan,
25087fa8d547SShradha Shah 	.ndo_set_vf_spoofchk	= efx_sriov_set_vf_spoofchk,
25097fa8d547SShradha Shah 	.ndo_get_vf_config	= efx_sriov_get_vf_config,
25104392dc69SEdward Cree 	.ndo_set_vf_link_state  = efx_sriov_set_vf_link_state,
2511cd2d5b52SBen Hutchings #endif
251208a7b29bSBert Kenward 	.ndo_get_phys_port_id   = efx_get_phys_port_id,
2513ac019f08SBert Kenward 	.ndo_get_phys_port_name	= efx_get_phys_port_name,
2514874aeea5SJeff Kirsher 	.ndo_setup_tc		= efx_setup_tc,
2515874aeea5SJeff Kirsher #ifdef CONFIG_RFS_ACCEL
2516874aeea5SJeff Kirsher 	.ndo_rx_flow_steer	= efx_filter_rfs,
2517874aeea5SJeff Kirsher #endif
2518e5fbd977SJon Cooper 	.ndo_udp_tunnel_add	= efx_udp_tunnel_add,
2519e5fbd977SJon Cooper 	.ndo_udp_tunnel_del	= efx_udp_tunnel_del,
2520e45a4fedSCharles McLachlan 	.ndo_bpf		= efx_xdp
2521874aeea5SJeff Kirsher };
2522874aeea5SJeff Kirsher 
2523e45a4fedSCharles McLachlan static int efx_xdp_setup_prog(struct efx_nic *efx, struct bpf_prog *prog)
2524e45a4fedSCharles McLachlan {
2525e45a4fedSCharles McLachlan 	struct bpf_prog *old_prog;
2526e45a4fedSCharles McLachlan 
2527e45a4fedSCharles McLachlan 	if (efx->xdp_rxq_info_failed) {
2528e45a4fedSCharles McLachlan 		netif_err(efx, drv, efx->net_dev,
2529e45a4fedSCharles McLachlan 			  "Unable to bind XDP program due to previous failure of rxq_info\n");
2530e45a4fedSCharles McLachlan 		return -EINVAL;
2531e45a4fedSCharles McLachlan 	}
2532e45a4fedSCharles McLachlan 
2533e45a4fedSCharles McLachlan 	if (prog && efx->net_dev->mtu > efx_xdp_max_mtu(efx)) {
2534e45a4fedSCharles McLachlan 		netif_err(efx, drv, efx->net_dev,
2535e45a4fedSCharles McLachlan 			  "Unable to configure XDP with MTU of %d (max: %d)\n",
2536e45a4fedSCharles McLachlan 			  efx->net_dev->mtu, efx_xdp_max_mtu(efx));
2537e45a4fedSCharles McLachlan 		return -EINVAL;
2538e45a4fedSCharles McLachlan 	}
2539e45a4fedSCharles McLachlan 
2540e45a4fedSCharles McLachlan 	old_prog = rtnl_dereference(efx->xdp_prog);
2541e45a4fedSCharles McLachlan 	rcu_assign_pointer(efx->xdp_prog, prog);
2542e45a4fedSCharles McLachlan 	/* Release the reference that was originally passed by the caller. */
2543e45a4fedSCharles McLachlan 	if (old_prog)
2544e45a4fedSCharles McLachlan 		bpf_prog_put(old_prog);
2545e45a4fedSCharles McLachlan 
2546e45a4fedSCharles McLachlan 	return 0;
2547e45a4fedSCharles McLachlan }
2548e45a4fedSCharles McLachlan 
2549e45a4fedSCharles McLachlan /* Context: process, rtnl_lock() held. */
2550e45a4fedSCharles McLachlan static int efx_xdp(struct net_device *dev, struct netdev_bpf *xdp)
2551e45a4fedSCharles McLachlan {
2552e45a4fedSCharles McLachlan 	struct efx_nic *efx = netdev_priv(dev);
2553e45a4fedSCharles McLachlan 	struct bpf_prog *xdp_prog;
2554e45a4fedSCharles McLachlan 
2555e45a4fedSCharles McLachlan 	switch (xdp->command) {
2556e45a4fedSCharles McLachlan 	case XDP_SETUP_PROG:
2557e45a4fedSCharles McLachlan 		return efx_xdp_setup_prog(efx, xdp->prog);
2558e45a4fedSCharles McLachlan 	case XDP_QUERY_PROG:
2559e45a4fedSCharles McLachlan 		xdp_prog = rtnl_dereference(efx->xdp_prog);
2560e45a4fedSCharles McLachlan 		xdp->prog_id = xdp_prog ? xdp_prog->aux->id : 0;
2561e45a4fedSCharles McLachlan 		return 0;
2562e45a4fedSCharles McLachlan 	default:
2563e45a4fedSCharles McLachlan 		return -EINVAL;
2564e45a4fedSCharles McLachlan 	}
2565e45a4fedSCharles McLachlan }
2566e45a4fedSCharles McLachlan 
2567874aeea5SJeff Kirsher static void efx_update_name(struct efx_nic *efx)
2568874aeea5SJeff Kirsher {
2569874aeea5SJeff Kirsher 	strcpy(efx->name, efx->net_dev->name);
2570874aeea5SJeff Kirsher 	efx_mtd_rename(efx);
2571874aeea5SJeff Kirsher 	efx_set_channel_names(efx);
2572874aeea5SJeff Kirsher }
2573874aeea5SJeff Kirsher 
2574874aeea5SJeff Kirsher static int efx_netdev_event(struct notifier_block *this,
2575874aeea5SJeff Kirsher 			    unsigned long event, void *ptr)
2576874aeea5SJeff Kirsher {
2577351638e7SJiri Pirko 	struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
2578874aeea5SJeff Kirsher 
25797fa8d547SShradha Shah 	if ((net_dev->netdev_ops == &efx_netdev_ops) &&
2580874aeea5SJeff Kirsher 	    event == NETDEV_CHANGENAME)
2581874aeea5SJeff Kirsher 		efx_update_name(netdev_priv(net_dev));
2582874aeea5SJeff Kirsher 
2583874aeea5SJeff Kirsher 	return NOTIFY_DONE;
2584874aeea5SJeff Kirsher }
2585874aeea5SJeff Kirsher 
2586874aeea5SJeff Kirsher static struct notifier_block efx_netdev_notifier = {
2587874aeea5SJeff Kirsher 	.notifier_call = efx_netdev_event,
2588874aeea5SJeff Kirsher };
2589874aeea5SJeff Kirsher 
2590874aeea5SJeff Kirsher static ssize_t
2591874aeea5SJeff Kirsher show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
2592874aeea5SJeff Kirsher {
25933e03a8baSChuhong Yuan 	struct efx_nic *efx = dev_get_drvdata(dev);
2594874aeea5SJeff Kirsher 	return sprintf(buf, "%d\n", efx->phy_type);
2595874aeea5SJeff Kirsher }
2596776fbcc9SBen Hutchings static DEVICE_ATTR(phy_type, 0444, show_phy_type, NULL);
2597874aeea5SJeff Kirsher 
2598e7fef9b4SEdward Cree #ifdef CONFIG_SFC_MCDI_LOGGING
2599e7fef9b4SEdward Cree static ssize_t show_mcdi_log(struct device *dev, struct device_attribute *attr,
2600e7fef9b4SEdward Cree 			     char *buf)
2601e7fef9b4SEdward Cree {
26023e03a8baSChuhong Yuan 	struct efx_nic *efx = dev_get_drvdata(dev);
2603e7fef9b4SEdward Cree 	struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
2604e7fef9b4SEdward Cree 
2605e7fef9b4SEdward Cree 	return scnprintf(buf, PAGE_SIZE, "%d\n", mcdi->logging_enabled);
2606e7fef9b4SEdward Cree }
2607e7fef9b4SEdward Cree static ssize_t set_mcdi_log(struct device *dev, struct device_attribute *attr,
2608e7fef9b4SEdward Cree 			    const char *buf, size_t count)
2609e7fef9b4SEdward Cree {
26103e03a8baSChuhong Yuan 	struct efx_nic *efx = dev_get_drvdata(dev);
2611e7fef9b4SEdward Cree 	struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
2612e7fef9b4SEdward Cree 	bool enable = count > 0 && *buf != '0';
2613e7fef9b4SEdward Cree 
2614e7fef9b4SEdward Cree 	mcdi->logging_enabled = enable;
2615e7fef9b4SEdward Cree 	return count;
2616e7fef9b4SEdward Cree }
2617e7fef9b4SEdward Cree static DEVICE_ATTR(mcdi_logging, 0644, show_mcdi_log, set_mcdi_log);
2618e7fef9b4SEdward Cree #endif
2619e7fef9b4SEdward Cree 
2620874aeea5SJeff Kirsher static int efx_register_netdev(struct efx_nic *efx)
2621874aeea5SJeff Kirsher {
2622874aeea5SJeff Kirsher 	struct net_device *net_dev = efx->net_dev;
2623874aeea5SJeff Kirsher 	struct efx_channel *channel;
2624874aeea5SJeff Kirsher 	int rc;
2625874aeea5SJeff Kirsher 
2626874aeea5SJeff Kirsher 	net_dev->watchdog_timeo = 5 * HZ;
2627874aeea5SJeff Kirsher 	net_dev->irq = efx->pci_dev->irq;
26287fa8d547SShradha Shah 	net_dev->netdev_ops = &efx_netdev_ops;
26297fa8d547SShradha Shah 	if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0)
26308127d661SBen Hutchings 		net_dev->priv_flags |= IFF_UNICAST_FLT;
26317ad24ea4SWilfried Klaebe 	net_dev->ethtool_ops = &efx_ethtool_ops;
26327e6d06f0SBen Hutchings 	net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
2633cd94e519SBert Kenward 	net_dev->min_mtu = EFX_MIN_MTU;
2634cd94e519SBert Kenward 	net_dev->max_mtu = EFX_MAX_MTU;
2635874aeea5SJeff Kirsher 
2636874aeea5SJeff Kirsher 	rtnl_lock();
2637874aeea5SJeff Kirsher 
26387153f623SBen Hutchings 	/* Enable resets to be scheduled and check whether any were
26397153f623SBen Hutchings 	 * already requested.  If so, the NIC is probably hosed so we
26407153f623SBen Hutchings 	 * abort.
26417153f623SBen Hutchings 	 */
26427153f623SBen Hutchings 	efx->state = STATE_READY;
26437153f623SBen Hutchings 	smp_mb(); /* ensure we change state before checking reset_pending */
26447153f623SBen Hutchings 	if (efx->reset_pending) {
26457153f623SBen Hutchings 		netif_err(efx, probe, efx->net_dev,
26467153f623SBen Hutchings 			  "aborting probe due to scheduled reset\n");
26477153f623SBen Hutchings 		rc = -EIO;
26487153f623SBen Hutchings 		goto fail_locked;
26497153f623SBen Hutchings 	}
26507153f623SBen Hutchings 
2651874aeea5SJeff Kirsher 	rc = dev_alloc_name(net_dev, net_dev->name);
2652874aeea5SJeff Kirsher 	if (rc < 0)
2653874aeea5SJeff Kirsher 		goto fail_locked;
2654874aeea5SJeff Kirsher 	efx_update_name(efx);
2655874aeea5SJeff Kirsher 
26568f8b3d51SBen Hutchings 	/* Always start with carrier off; PHY events will detect the link */
26578f8b3d51SBen Hutchings 	netif_carrier_off(net_dev);
26588f8b3d51SBen Hutchings 
2659874aeea5SJeff Kirsher 	rc = register_netdevice(net_dev);
2660874aeea5SJeff Kirsher 	if (rc)
2661874aeea5SJeff Kirsher 		goto fail_locked;
2662874aeea5SJeff Kirsher 
2663874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx) {
2664874aeea5SJeff Kirsher 		struct efx_tx_queue *tx_queue;
2665874aeea5SJeff Kirsher 		efx_for_each_channel_tx_queue(tx_queue, channel)
2666874aeea5SJeff Kirsher 			efx_init_tx_queue_core_txq(tx_queue);
2667874aeea5SJeff Kirsher 	}
2668874aeea5SJeff Kirsher 
26690bcf4a64SBen Hutchings 	efx_associate(efx);
26700bcf4a64SBen Hutchings 
2671874aeea5SJeff Kirsher 	rtnl_unlock();
2672874aeea5SJeff Kirsher 
2673874aeea5SJeff Kirsher 	rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2674874aeea5SJeff Kirsher 	if (rc) {
2675874aeea5SJeff Kirsher 		netif_err(efx, drv, efx->net_dev,
2676874aeea5SJeff Kirsher 			  "failed to init net dev attributes\n");
2677874aeea5SJeff Kirsher 		goto fail_registered;
2678874aeea5SJeff Kirsher 	}
2679e7fef9b4SEdward Cree #ifdef CONFIG_SFC_MCDI_LOGGING
2680e7fef9b4SEdward Cree 	rc = device_create_file(&efx->pci_dev->dev, &dev_attr_mcdi_logging);
2681e7fef9b4SEdward Cree 	if (rc) {
2682e7fef9b4SEdward Cree 		netif_err(efx, drv, efx->net_dev,
2683e7fef9b4SEdward Cree 			  "failed to init net dev attributes\n");
2684e7fef9b4SEdward Cree 		goto fail_attr_mcdi_logging;
2685e7fef9b4SEdward Cree 	}
2686e7fef9b4SEdward Cree #endif
2687874aeea5SJeff Kirsher 
2688874aeea5SJeff Kirsher 	return 0;
2689874aeea5SJeff Kirsher 
2690e7fef9b4SEdward Cree #ifdef CONFIG_SFC_MCDI_LOGGING
2691e7fef9b4SEdward Cree fail_attr_mcdi_logging:
2692e7fef9b4SEdward Cree 	device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2693e7fef9b4SEdward Cree #endif
26947153f623SBen Hutchings fail_registered:
26957153f623SBen Hutchings 	rtnl_lock();
26960bcf4a64SBen Hutchings 	efx_dissociate(efx);
26977153f623SBen Hutchings 	unregister_netdevice(net_dev);
2698874aeea5SJeff Kirsher fail_locked:
26997153f623SBen Hutchings 	efx->state = STATE_UNINIT;
2700874aeea5SJeff Kirsher 	rtnl_unlock();
2701874aeea5SJeff Kirsher 	netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
2702874aeea5SJeff Kirsher 	return rc;
2703874aeea5SJeff Kirsher }
2704874aeea5SJeff Kirsher 
2705874aeea5SJeff Kirsher static void efx_unregister_netdev(struct efx_nic *efx)
2706874aeea5SJeff Kirsher {
2707874aeea5SJeff Kirsher 	if (!efx->net_dev)
2708874aeea5SJeff Kirsher 		return;
2709874aeea5SJeff Kirsher 
2710874aeea5SJeff Kirsher 	BUG_ON(netdev_priv(efx->net_dev) != efx);
2711874aeea5SJeff Kirsher 
2712e7fef9b4SEdward Cree 	if (efx_dev_registered(efx)) {
2713874aeea5SJeff Kirsher 		strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2714e7fef9b4SEdward Cree #ifdef CONFIG_SFC_MCDI_LOGGING
2715e7fef9b4SEdward Cree 		device_remove_file(&efx->pci_dev->dev, &dev_attr_mcdi_logging);
2716e7fef9b4SEdward Cree #endif
2717874aeea5SJeff Kirsher 		device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2718e7fef9b4SEdward Cree 		unregister_netdev(efx->net_dev);
2719e7fef9b4SEdward Cree 	}
2720874aeea5SJeff Kirsher }
2721874aeea5SJeff Kirsher 
2722874aeea5SJeff Kirsher /**************************************************************************
2723874aeea5SJeff Kirsher  *
2724874aeea5SJeff Kirsher  * Device reset and suspend
2725874aeea5SJeff Kirsher  *
2726874aeea5SJeff Kirsher  **************************************************************************/
2727874aeea5SJeff Kirsher 
2728874aeea5SJeff Kirsher /* Tears down the entire software state and most of the hardware state
2729874aeea5SJeff Kirsher  * before reset.  */
2730874aeea5SJeff Kirsher void efx_reset_down(struct efx_nic *efx, enum reset_type method)
2731874aeea5SJeff Kirsher {
2732874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
2733874aeea5SJeff Kirsher 
2734e283546cSEdward Cree 	if (method == RESET_TYPE_MCDI_TIMEOUT)
2735e283546cSEdward Cree 		efx->type->prepare_flr(efx);
2736e283546cSEdward Cree 
2737874aeea5SJeff Kirsher 	efx_stop_all(efx);
2738d8291187SBen Hutchings 	efx_disable_interrupts(efx);
27395642ceefSBen Hutchings 
27405642ceefSBen Hutchings 	mutex_lock(&efx->mac_lock);
2741193f2003SBert Kenward 	down_write(&efx->filter_sem);
2742e0a65e3cSEdward Cree 	mutex_lock(&efx->rss_lock);
2743087e9025SJon Cooper 	if (efx->port_initialized && method != RESET_TYPE_INVISIBLE &&
2744087e9025SJon Cooper 	    method != RESET_TYPE_DATAPATH)
2745874aeea5SJeff Kirsher 		efx->phy_op->fini(efx);
2746874aeea5SJeff Kirsher 	efx->type->fini(efx);
2747874aeea5SJeff Kirsher }
2748874aeea5SJeff Kirsher 
2749874aeea5SJeff Kirsher /* This function will always ensure that the locks acquired in
2750874aeea5SJeff Kirsher  * efx_reset_down() are released. A failure return code indicates
2751874aeea5SJeff Kirsher  * that we were unable to reinitialise the hardware, and the
2752874aeea5SJeff Kirsher  * driver should be disabled. If ok is false, then the rx and tx
2753874aeea5SJeff Kirsher  * engines are not restarted, pending a RESET_DISABLE. */
2754874aeea5SJeff Kirsher int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
2755874aeea5SJeff Kirsher {
2756874aeea5SJeff Kirsher 	int rc;
2757874aeea5SJeff Kirsher 
2758874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
2759874aeea5SJeff Kirsher 
2760e283546cSEdward Cree 	if (method == RESET_TYPE_MCDI_TIMEOUT)
2761e283546cSEdward Cree 		efx->type->finish_flr(efx);
2762e283546cSEdward Cree 
2763e283546cSEdward Cree 	/* Ensure that SRAM is initialised even if we're disabling the device */
2764874aeea5SJeff Kirsher 	rc = efx->type->init(efx);
2765874aeea5SJeff Kirsher 	if (rc) {
2766874aeea5SJeff Kirsher 		netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
2767874aeea5SJeff Kirsher 		goto fail;
2768874aeea5SJeff Kirsher 	}
2769874aeea5SJeff Kirsher 
2770874aeea5SJeff Kirsher 	if (!ok)
2771874aeea5SJeff Kirsher 		goto fail;
2772874aeea5SJeff Kirsher 
2773087e9025SJon Cooper 	if (efx->port_initialized && method != RESET_TYPE_INVISIBLE &&
2774087e9025SJon Cooper 	    method != RESET_TYPE_DATAPATH) {
2775874aeea5SJeff Kirsher 		rc = efx->phy_op->init(efx);
2776874aeea5SJeff Kirsher 		if (rc)
2777874aeea5SJeff Kirsher 			goto fail;
2778267d9d73SEdward Cree 		rc = efx->phy_op->reconfigure(efx);
2779267d9d73SEdward Cree 		if (rc && rc != -EPERM)
2780874aeea5SJeff Kirsher 			netif_err(efx, drv, efx->net_dev,
2781874aeea5SJeff Kirsher 				  "could not restore PHY settings\n");
2782874aeea5SJeff Kirsher 	}
2783874aeea5SJeff Kirsher 
2784261e4d96SJon Cooper 	rc = efx_enable_interrupts(efx);
2785261e4d96SJon Cooper 	if (rc)
2786261e4d96SJon Cooper 		goto fail;
27876d8aaaf6SDaniel Pieczko 
27886d8aaaf6SDaniel Pieczko #ifdef CONFIG_SFC_SRIOV
27896d8aaaf6SDaniel Pieczko 	rc = efx->type->vswitching_restore(efx);
27906d8aaaf6SDaniel Pieczko 	if (rc) /* not fatal; the PF will still work fine */
27916d8aaaf6SDaniel Pieczko 		netif_warn(efx, probe, efx->net_dev,
27926d8aaaf6SDaniel Pieczko 			   "failed to restore vswitching rc=%d;"
27936d8aaaf6SDaniel Pieczko 			   " VFs may not function\n", rc);
27946d8aaaf6SDaniel Pieczko #endif
27956d8aaaf6SDaniel Pieczko 
279642356d9aSEdward Cree 	if (efx->type->rx_restore_rss_contexts)
279742356d9aSEdward Cree 		efx->type->rx_restore_rss_contexts(efx);
2798e0a65e3cSEdward Cree 	mutex_unlock(&efx->rss_lock);
2799193f2003SBert Kenward 	efx->type->filter_table_restore(efx);
2800193f2003SBert Kenward 	up_write(&efx->filter_sem);
28017fa8d547SShradha Shah 	if (efx->type->sriov_reset)
2802d98a4ffeSShradha Shah 		efx->type->sriov_reset(efx);
2803874aeea5SJeff Kirsher 
2804874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
2805874aeea5SJeff Kirsher 
2806874aeea5SJeff Kirsher 	efx_start_all(efx);
2807874aeea5SJeff Kirsher 
2808e5fbd977SJon Cooper 	if (efx->type->udp_tnl_push_ports)
2809e5fbd977SJon Cooper 		efx->type->udp_tnl_push_ports(efx);
2810e5fbd977SJon Cooper 
2811874aeea5SJeff Kirsher 	return 0;
2812874aeea5SJeff Kirsher 
2813874aeea5SJeff Kirsher fail:
2814874aeea5SJeff Kirsher 	efx->port_initialized = false;
2815874aeea5SJeff Kirsher 
2816e0a65e3cSEdward Cree 	mutex_unlock(&efx->rss_lock);
2817193f2003SBert Kenward 	up_write(&efx->filter_sem);
2818874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
2819874aeea5SJeff Kirsher 
2820874aeea5SJeff Kirsher 	return rc;
2821874aeea5SJeff Kirsher }
2822874aeea5SJeff Kirsher 
2823874aeea5SJeff Kirsher /* Reset the NIC using the specified method.  Note that the reset may
2824874aeea5SJeff Kirsher  * fail, in which case the card will be left in an unusable state.
2825874aeea5SJeff Kirsher  *
2826874aeea5SJeff Kirsher  * Caller must hold the rtnl_lock.
2827874aeea5SJeff Kirsher  */
2828874aeea5SJeff Kirsher int efx_reset(struct efx_nic *efx, enum reset_type method)
2829874aeea5SJeff Kirsher {
2830874aeea5SJeff Kirsher 	int rc, rc2;
2831874aeea5SJeff Kirsher 	bool disabled;
2832874aeea5SJeff Kirsher 
2833874aeea5SJeff Kirsher 	netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2834874aeea5SJeff Kirsher 		   RESET_TYPE(method));
2835874aeea5SJeff Kirsher 
2836c2f3b8e3SDaniel Pieczko 	efx_device_detach_sync(efx);
2837874aeea5SJeff Kirsher 	efx_reset_down(efx, method);
2838874aeea5SJeff Kirsher 
2839874aeea5SJeff Kirsher 	rc = efx->type->reset(efx, method);
2840874aeea5SJeff Kirsher 	if (rc) {
2841874aeea5SJeff Kirsher 		netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
2842874aeea5SJeff Kirsher 		goto out;
2843874aeea5SJeff Kirsher 	}
2844874aeea5SJeff Kirsher 
2845874aeea5SJeff Kirsher 	/* Clear flags for the scopes we covered.  We assume the NIC and
2846874aeea5SJeff Kirsher 	 * driver are now quiescent so that there is no race here.
2847874aeea5SJeff Kirsher 	 */
2848e283546cSEdward Cree 	if (method < RESET_TYPE_MAX_METHOD)
2849874aeea5SJeff Kirsher 		efx->reset_pending &= -(1 << (method + 1));
2850e283546cSEdward Cree 	else /* it doesn't fit into the well-ordered scope hierarchy */
2851e283546cSEdward Cree 		__clear_bit(method, &efx->reset_pending);
2852874aeea5SJeff Kirsher 
2853874aeea5SJeff Kirsher 	/* Reinitialise bus-mastering, which may have been turned off before
2854874aeea5SJeff Kirsher 	 * the reset was scheduled. This is still appropriate, even in the
2855874aeea5SJeff Kirsher 	 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2856874aeea5SJeff Kirsher 	 * can respond to requests. */
2857874aeea5SJeff Kirsher 	pci_set_master(efx->pci_dev);
2858874aeea5SJeff Kirsher 
2859874aeea5SJeff Kirsher out:
2860874aeea5SJeff Kirsher 	/* Leave device stopped if necessary */
2861626950dbSAlexandre Rames 	disabled = rc ||
2862626950dbSAlexandre Rames 		method == RESET_TYPE_DISABLE ||
2863626950dbSAlexandre Rames 		method == RESET_TYPE_RECOVER_OR_DISABLE;
2864874aeea5SJeff Kirsher 	rc2 = efx_reset_up(efx, method, !disabled);
2865874aeea5SJeff Kirsher 	if (rc2) {
2866874aeea5SJeff Kirsher 		disabled = true;
2867874aeea5SJeff Kirsher 		if (!rc)
2868874aeea5SJeff Kirsher 			rc = rc2;
2869874aeea5SJeff Kirsher 	}
2870874aeea5SJeff Kirsher 
2871874aeea5SJeff Kirsher 	if (disabled) {
2872874aeea5SJeff Kirsher 		dev_close(efx->net_dev);
2873874aeea5SJeff Kirsher 		netif_err(efx, drv, efx->net_dev, "has been disabled\n");
2874874aeea5SJeff Kirsher 		efx->state = STATE_DISABLED;
2875874aeea5SJeff Kirsher 	} else {
2876874aeea5SJeff Kirsher 		netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
28779c568fd8SPeter Dunning 		efx_device_attach_if_not_resetting(efx);
2878874aeea5SJeff Kirsher 	}
2879874aeea5SJeff Kirsher 	return rc;
2880874aeea5SJeff Kirsher }
2881874aeea5SJeff Kirsher 
2882626950dbSAlexandre Rames /* Try recovery mechanisms.
2883626950dbSAlexandre Rames  * For now only EEH is supported.
2884626950dbSAlexandre Rames  * Returns 0 if the recovery mechanisms are unsuccessful.
2885626950dbSAlexandre Rames  * Returns a non-zero value otherwise.
2886626950dbSAlexandre Rames  */
2887b28405b0SAlexandre Rames int efx_try_recovery(struct efx_nic *efx)
2888626950dbSAlexandre Rames {
2889626950dbSAlexandre Rames #ifdef CONFIG_EEH
2890626950dbSAlexandre Rames 	/* A PCI error can occur and not be seen by EEH because nothing
2891626950dbSAlexandre Rames 	 * happens on the PCI bus. In this case the driver may fail and
2892626950dbSAlexandre Rames 	 * schedule a 'recover or reset', leading to this recovery handler.
2893626950dbSAlexandre Rames 	 * Manually call the eeh failure check function.
2894626950dbSAlexandre Rames 	 */
289512a89dbaSBenjamin Herrenschmidt 	struct eeh_dev *eehdev = pci_dev_to_eeh_dev(efx->pci_dev);
2896626950dbSAlexandre Rames 	if (eeh_dev_check_failure(eehdev)) {
2897626950dbSAlexandre Rames 		/* The EEH mechanisms will handle the error and reset the
2898626950dbSAlexandre Rames 		 * device if necessary.
2899626950dbSAlexandre Rames 		 */
2900626950dbSAlexandre Rames 		return 1;
2901626950dbSAlexandre Rames 	}
2902626950dbSAlexandre Rames #endif
2903626950dbSAlexandre Rames 	return 0;
2904626950dbSAlexandre Rames }
2905626950dbSAlexandre Rames 
290674cd60a4SJon Cooper static void efx_wait_for_bist_end(struct efx_nic *efx)
290774cd60a4SJon Cooper {
290874cd60a4SJon Cooper 	int i;
290974cd60a4SJon Cooper 
291074cd60a4SJon Cooper 	for (i = 0; i < BIST_WAIT_DELAY_COUNT; ++i) {
291174cd60a4SJon Cooper 		if (efx_mcdi_poll_reboot(efx))
291274cd60a4SJon Cooper 			goto out;
291374cd60a4SJon Cooper 		msleep(BIST_WAIT_DELAY_MS);
291474cd60a4SJon Cooper 	}
291574cd60a4SJon Cooper 
291674cd60a4SJon Cooper 	netif_err(efx, drv, efx->net_dev, "Warning: No MC reboot after BIST mode\n");
291774cd60a4SJon Cooper out:
291874cd60a4SJon Cooper 	/* Either way unset the BIST flag. If we found no reboot we probably
291974cd60a4SJon Cooper 	 * won't recover, but we should try.
292074cd60a4SJon Cooper 	 */
292174cd60a4SJon Cooper 	efx->mc_bist_for_other_fn = false;
292274cd60a4SJon Cooper }
292374cd60a4SJon Cooper 
2924874aeea5SJeff Kirsher /* The worker thread exists so that code that cannot sleep can
2925874aeea5SJeff Kirsher  * schedule a reset for later.
2926874aeea5SJeff Kirsher  */
2927874aeea5SJeff Kirsher static void efx_reset_work(struct work_struct *data)
2928874aeea5SJeff Kirsher {
2929874aeea5SJeff Kirsher 	struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
2930626950dbSAlexandre Rames 	unsigned long pending;
2931626950dbSAlexandre Rames 	enum reset_type method;
2932626950dbSAlexandre Rames 
29336aa7de05SMark Rutland 	pending = READ_ONCE(efx->reset_pending);
2934626950dbSAlexandre Rames 	method = fls(pending) - 1;
2935626950dbSAlexandre Rames 
293674cd60a4SJon Cooper 	if (method == RESET_TYPE_MC_BIST)
293774cd60a4SJon Cooper 		efx_wait_for_bist_end(efx);
293874cd60a4SJon Cooper 
2939626950dbSAlexandre Rames 	if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
2940626950dbSAlexandre Rames 	     method == RESET_TYPE_RECOVER_OR_ALL) &&
2941626950dbSAlexandre Rames 	    efx_try_recovery(efx))
2942626950dbSAlexandre Rames 		return;
2943874aeea5SJeff Kirsher 
2944874aeea5SJeff Kirsher 	if (!pending)
2945874aeea5SJeff Kirsher 		return;
2946874aeea5SJeff Kirsher 
2947874aeea5SJeff Kirsher 	rtnl_lock();
29487153f623SBen Hutchings 
29497153f623SBen Hutchings 	/* We checked the state in efx_schedule_reset() but it may
29507153f623SBen Hutchings 	 * have changed by now.  Now that we have the RTNL lock,
29517153f623SBen Hutchings 	 * it cannot change again.
29527153f623SBen Hutchings 	 */
29537153f623SBen Hutchings 	if (efx->state == STATE_READY)
2954626950dbSAlexandre Rames 		(void)efx_reset(efx, method);
29557153f623SBen Hutchings 
2956874aeea5SJeff Kirsher 	rtnl_unlock();
2957874aeea5SJeff Kirsher }
2958874aeea5SJeff Kirsher 
2959874aeea5SJeff Kirsher void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2960874aeea5SJeff Kirsher {
2961874aeea5SJeff Kirsher 	enum reset_type method;
2962874aeea5SJeff Kirsher 
2963626950dbSAlexandre Rames 	if (efx->state == STATE_RECOVERY) {
2964626950dbSAlexandre Rames 		netif_dbg(efx, drv, efx->net_dev,
2965626950dbSAlexandre Rames 			  "recovering: skip scheduling %s reset\n",
2966626950dbSAlexandre Rames 			  RESET_TYPE(type));
2967626950dbSAlexandre Rames 		return;
2968626950dbSAlexandre Rames 	}
2969626950dbSAlexandre Rames 
2970874aeea5SJeff Kirsher 	switch (type) {
2971874aeea5SJeff Kirsher 	case RESET_TYPE_INVISIBLE:
2972874aeea5SJeff Kirsher 	case RESET_TYPE_ALL:
2973626950dbSAlexandre Rames 	case RESET_TYPE_RECOVER_OR_ALL:
2974874aeea5SJeff Kirsher 	case RESET_TYPE_WORLD:
2975874aeea5SJeff Kirsher 	case RESET_TYPE_DISABLE:
2976626950dbSAlexandre Rames 	case RESET_TYPE_RECOVER_OR_DISABLE:
2977087e9025SJon Cooper 	case RESET_TYPE_DATAPATH:
297874cd60a4SJon Cooper 	case RESET_TYPE_MC_BIST:
2979e283546cSEdward Cree 	case RESET_TYPE_MCDI_TIMEOUT:
2980874aeea5SJeff Kirsher 		method = type;
2981874aeea5SJeff Kirsher 		netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2982874aeea5SJeff Kirsher 			  RESET_TYPE(method));
2983874aeea5SJeff Kirsher 		break;
2984874aeea5SJeff Kirsher 	default:
2985874aeea5SJeff Kirsher 		method = efx->type->map_reset_reason(type);
2986874aeea5SJeff Kirsher 		netif_dbg(efx, drv, efx->net_dev,
2987874aeea5SJeff Kirsher 			  "scheduling %s reset for %s\n",
2988874aeea5SJeff Kirsher 			  RESET_TYPE(method), RESET_TYPE(type));
2989874aeea5SJeff Kirsher 		break;
2990874aeea5SJeff Kirsher 	}
2991874aeea5SJeff Kirsher 
2992874aeea5SJeff Kirsher 	set_bit(method, &efx->reset_pending);
29937153f623SBen Hutchings 	smp_mb(); /* ensure we change reset_pending before checking state */
29947153f623SBen Hutchings 
29957153f623SBen Hutchings 	/* If we're not READY then just leave the flags set as the cue
29967153f623SBen Hutchings 	 * to abort probing or reschedule the reset later.
29977153f623SBen Hutchings 	 */
29986aa7de05SMark Rutland 	if (READ_ONCE(efx->state) != STATE_READY)
29997153f623SBen Hutchings 		return;
3000874aeea5SJeff Kirsher 
3001874aeea5SJeff Kirsher 	/* efx_process_channel() will no longer read events once a
3002874aeea5SJeff Kirsher 	 * reset is scheduled. So switch back to poll'd MCDI completions. */
3003874aeea5SJeff Kirsher 	efx_mcdi_mode_poll(efx);
3004874aeea5SJeff Kirsher 
3005874aeea5SJeff Kirsher 	queue_work(reset_workqueue, &efx->reset_work);
3006874aeea5SJeff Kirsher }
3007874aeea5SJeff Kirsher 
3008874aeea5SJeff Kirsher /**************************************************************************
3009874aeea5SJeff Kirsher  *
3010874aeea5SJeff Kirsher  * List of NICs we support
3011874aeea5SJeff Kirsher  *
3012874aeea5SJeff Kirsher  **************************************************************************/
3013874aeea5SJeff Kirsher 
3014874aeea5SJeff Kirsher /* PCI device ID table */
30159baa3c34SBenoit Taine static const struct pci_device_id efx_pci_table[] = {
3016547c474fSBen Hutchings 	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803),	/* SFC9020 */
3017874aeea5SJeff Kirsher 	 .driver_data = (unsigned long) &siena_a0_nic_type},
3018547c474fSBen Hutchings 	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813),	/* SFL9021 */
3019874aeea5SJeff Kirsher 	 .driver_data = (unsigned long) &siena_a0_nic_type},
30208127d661SBen Hutchings 	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0903),  /* SFC9120 PF */
30218127d661SBen Hutchings 	 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
30226f7f8aa6SShradha Shah 	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1903),  /* SFC9120 VF */
30236f7f8aa6SShradha Shah 	 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
30243b06a00eSMateusz Wrzesinski 	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0923),  /* SFC9140 PF */
30253b06a00eSMateusz Wrzesinski 	 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
3026dd248f1bSBert Kenward 	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1923),  /* SFC9140 VF */
3027dd248f1bSBert Kenward 	 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
3028dd248f1bSBert Kenward 	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0a03),  /* SFC9220 PF */
3029dd248f1bSBert Kenward 	 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
3030dd248f1bSBert Kenward 	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1a03),  /* SFC9220 VF */
3031dd248f1bSBert Kenward 	 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
3032aae5a316SEdward Cree 	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0b03),  /* SFC9250 PF */
3033aae5a316SEdward Cree 	 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
3034aae5a316SEdward Cree 	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1b03),  /* SFC9250 VF */
3035aae5a316SEdward Cree 	 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
3036874aeea5SJeff Kirsher 	{0}			/* end of list */
3037874aeea5SJeff Kirsher };
3038874aeea5SJeff Kirsher 
3039874aeea5SJeff Kirsher /**************************************************************************
3040874aeea5SJeff Kirsher  *
3041874aeea5SJeff Kirsher  * Dummy PHY/MAC operations
3042874aeea5SJeff Kirsher  *
3043874aeea5SJeff Kirsher  * Can be used for some unimplemented operations
3044874aeea5SJeff Kirsher  * Needed so all function pointers are valid and do not have to be tested
3045874aeea5SJeff Kirsher  * before use
3046874aeea5SJeff Kirsher  *
3047874aeea5SJeff Kirsher  **************************************************************************/
3048874aeea5SJeff Kirsher int efx_port_dummy_op_int(struct efx_nic *efx)
3049874aeea5SJeff Kirsher {
3050874aeea5SJeff Kirsher 	return 0;
3051874aeea5SJeff Kirsher }
3052874aeea5SJeff Kirsher void efx_port_dummy_op_void(struct efx_nic *efx) {}
3053874aeea5SJeff Kirsher 
3054874aeea5SJeff Kirsher static bool efx_port_dummy_op_poll(struct efx_nic *efx)
3055874aeea5SJeff Kirsher {
3056874aeea5SJeff Kirsher 	return false;
3057874aeea5SJeff Kirsher }
3058874aeea5SJeff Kirsher 
3059874aeea5SJeff Kirsher static const struct efx_phy_operations efx_dummy_phy_operations = {
3060874aeea5SJeff Kirsher 	.init		 = efx_port_dummy_op_int,
3061874aeea5SJeff Kirsher 	.reconfigure	 = efx_port_dummy_op_int,
3062874aeea5SJeff Kirsher 	.poll		 = efx_port_dummy_op_poll,
3063874aeea5SJeff Kirsher 	.fini		 = efx_port_dummy_op_void,
3064874aeea5SJeff Kirsher };
3065874aeea5SJeff Kirsher 
3066874aeea5SJeff Kirsher /**************************************************************************
3067874aeea5SJeff Kirsher  *
3068874aeea5SJeff Kirsher  * Data housekeeping
3069874aeea5SJeff Kirsher  *
3070874aeea5SJeff Kirsher  **************************************************************************/
3071874aeea5SJeff Kirsher 
3072874aeea5SJeff Kirsher /* This zeroes out and then fills in the invariants in a struct
3073874aeea5SJeff Kirsher  * efx_nic (including all sub-structures).
3074874aeea5SJeff Kirsher  */
3075adeb15aaSBen Hutchings static int efx_init_struct(struct efx_nic *efx,
3076874aeea5SJeff Kirsher 			   struct pci_dev *pci_dev, struct net_device *net_dev)
3077874aeea5SJeff Kirsher {
30786f9f6ec2SAndrew Rybchenko 	int rc = -ENOMEM, i;
3079874aeea5SJeff Kirsher 
3080874aeea5SJeff Kirsher 	/* Initialise common structures */
30810bcf4a64SBen Hutchings 	INIT_LIST_HEAD(&efx->node);
30820bcf4a64SBen Hutchings 	INIT_LIST_HEAD(&efx->secondary_list);
3083874aeea5SJeff Kirsher 	spin_lock_init(&efx->biu_lock);
3084874aeea5SJeff Kirsher #ifdef CONFIG_SFC_MTD
3085874aeea5SJeff Kirsher 	INIT_LIST_HEAD(&efx->mtd_list);
3086874aeea5SJeff Kirsher #endif
3087874aeea5SJeff Kirsher 	INIT_WORK(&efx->reset_work, efx_reset_work);
3088874aeea5SJeff Kirsher 	INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
3089dd40781eSBen Hutchings 	INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
3090874aeea5SJeff Kirsher 	efx->pci_dev = pci_dev;
3091874aeea5SJeff Kirsher 	efx->msg_enable = debug;
3092f16aeea0SBen Hutchings 	efx->state = STATE_UNINIT;
3093874aeea5SJeff Kirsher 	strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
3094874aeea5SJeff Kirsher 
3095874aeea5SJeff Kirsher 	efx->net_dev = net_dev;
309643a3739dSJon Cooper 	efx->rx_prefix_size = efx->type->rx_prefix_size;
30972ec03014SAndrew Rybchenko 	efx->rx_ip_align =
30982ec03014SAndrew Rybchenko 		NET_IP_ALIGN ? (efx->rx_prefix_size + NET_IP_ALIGN) % 4 : 0;
309943a3739dSJon Cooper 	efx->rx_packet_hash_offset =
310043a3739dSJon Cooper 		efx->type->rx_hash_offset - efx->type->rx_prefix_size;
3101bd9a265dSJon Cooper 	efx->rx_packet_ts_offset =
3102bd9a265dSJon Cooper 		efx->type->rx_ts_offset - efx->type->rx_prefix_size;
310342356d9aSEdward Cree 	INIT_LIST_HEAD(&efx->rss_context.list);
3104e0a65e3cSEdward Cree 	mutex_init(&efx->rss_lock);
3105874aeea5SJeff Kirsher 	spin_lock_init(&efx->stats_lock);
310671827443SEdward Cree 	efx->vi_stride = EFX_DEFAULT_VI_STRIDE;
3107c1be4821SEdward Cree 	efx->num_mac_stats = MC_CMD_MAC_NSTATS;
3108c1be4821SEdward Cree 	BUILD_BUG_ON(MC_CMD_MAC_NSTATS - 1 != MC_CMD_MAC_GENERATION_END);
3109874aeea5SJeff Kirsher 	mutex_init(&efx->mac_lock);
31103af0f342SEdward Cree #ifdef CONFIG_RFS_ACCEL
31113af0f342SEdward Cree 	mutex_init(&efx->rps_mutex);
3112f8d62037SEdward Cree 	spin_lock_init(&efx->rps_hash_lock);
3113f8d62037SEdward Cree 	/* Failure to allocate is not fatal, but may degrade ARFS performance */
3114f8d62037SEdward Cree 	efx->rps_hash_table = kcalloc(EFX_ARFS_HASH_TABLE_SIZE,
3115f8d62037SEdward Cree 				      sizeof(*efx->rps_hash_table), GFP_KERNEL);
31163af0f342SEdward Cree #endif
3117874aeea5SJeff Kirsher 	efx->phy_op = &efx_dummy_phy_operations;
3118874aeea5SJeff Kirsher 	efx->mdio.dev = net_dev;
3119874aeea5SJeff Kirsher 	INIT_WORK(&efx->mac_work, efx_mac_work);
31209f2cb71cSBen Hutchings 	init_waitqueue_head(&efx->flush_wq);
3121874aeea5SJeff Kirsher 
3122874aeea5SJeff Kirsher 	for (i = 0; i < EFX_MAX_CHANNELS; i++) {
3123874aeea5SJeff Kirsher 		efx->channel[i] = efx_alloc_channel(efx, i, NULL);
3124874aeea5SJeff Kirsher 		if (!efx->channel[i])
3125874aeea5SJeff Kirsher 			goto fail;
3126d8291187SBen Hutchings 		efx->msi_context[i].efx = efx;
3127d8291187SBen Hutchings 		efx->msi_context[i].index = i;
3128874aeea5SJeff Kirsher 	}
3129874aeea5SJeff Kirsher 
3130874aeea5SJeff Kirsher 	/* Higher numbered interrupt modes are less capable! */
31316f9f6ec2SAndrew Rybchenko 	if (WARN_ON_ONCE(efx->type->max_interrupt_mode >
31326f9f6ec2SAndrew Rybchenko 			 efx->type->min_interrupt_mode)) {
31336f9f6ec2SAndrew Rybchenko 		rc = -EIO;
31346f9f6ec2SAndrew Rybchenko 		goto fail;
31356f9f6ec2SAndrew Rybchenko 	}
3136874aeea5SJeff Kirsher 	efx->interrupt_mode = max(efx->type->max_interrupt_mode,
3137874aeea5SJeff Kirsher 				  interrupt_mode);
31386f9f6ec2SAndrew Rybchenko 	efx->interrupt_mode = min(efx->type->min_interrupt_mode,
31396f9f6ec2SAndrew Rybchenko 				  interrupt_mode);
3140874aeea5SJeff Kirsher 
3141874aeea5SJeff Kirsher 	/* Would be good to use the net_dev name, but we're too early */
3142874aeea5SJeff Kirsher 	snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
3143874aeea5SJeff Kirsher 		 pci_name(pci_dev));
3144874aeea5SJeff Kirsher 	efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
3145874aeea5SJeff Kirsher 	if (!efx->workqueue)
3146874aeea5SJeff Kirsher 		goto fail;
3147874aeea5SJeff Kirsher 
3148874aeea5SJeff Kirsher 	return 0;
3149874aeea5SJeff Kirsher 
3150874aeea5SJeff Kirsher fail:
3151874aeea5SJeff Kirsher 	efx_fini_struct(efx);
31526f9f6ec2SAndrew Rybchenko 	return rc;
3153874aeea5SJeff Kirsher }
3154874aeea5SJeff Kirsher 
3155874aeea5SJeff Kirsher static void efx_fini_struct(struct efx_nic *efx)
3156874aeea5SJeff Kirsher {
3157874aeea5SJeff Kirsher 	int i;
3158874aeea5SJeff Kirsher 
3159f8d62037SEdward Cree #ifdef CONFIG_RFS_ACCEL
3160f8d62037SEdward Cree 	kfree(efx->rps_hash_table);
3161f8d62037SEdward Cree #endif
3162f8d62037SEdward Cree 
3163874aeea5SJeff Kirsher 	for (i = 0; i < EFX_MAX_CHANNELS; i++)
3164874aeea5SJeff Kirsher 		kfree(efx->channel[i]);
3165874aeea5SJeff Kirsher 
3166ef215e64SBen Hutchings 	kfree(efx->vpd_sn);
3167ef215e64SBen Hutchings 
3168874aeea5SJeff Kirsher 	if (efx->workqueue) {
3169874aeea5SJeff Kirsher 		destroy_workqueue(efx->workqueue);
3170874aeea5SJeff Kirsher 		efx->workqueue = NULL;
3171874aeea5SJeff Kirsher 	}
3172874aeea5SJeff Kirsher }
3173874aeea5SJeff Kirsher 
3174e4d112e4SEdward Cree void efx_update_sw_stats(struct efx_nic *efx, u64 *stats)
3175e4d112e4SEdward Cree {
3176e4d112e4SEdward Cree 	u64 n_rx_nodesc_trunc = 0;
3177e4d112e4SEdward Cree 	struct efx_channel *channel;
3178e4d112e4SEdward Cree 
3179e4d112e4SEdward Cree 	efx_for_each_channel(channel, efx)
3180e4d112e4SEdward Cree 		n_rx_nodesc_trunc += channel->n_rx_nodesc_trunc;
3181e4d112e4SEdward Cree 	stats[GENERIC_STAT_rx_nodesc_trunc] = n_rx_nodesc_trunc;
3182e4d112e4SEdward Cree 	stats[GENERIC_STAT_rx_noskb_drops] = atomic_read(&efx->n_rx_noskb_drops);
3183e4d112e4SEdward Cree }
3184e4d112e4SEdward Cree 
3185f8d62037SEdward Cree bool efx_filter_spec_equal(const struct efx_filter_spec *left,
3186f8d62037SEdward Cree 			   const struct efx_filter_spec *right)
3187f8d62037SEdward Cree {
3188f8d62037SEdward Cree 	if ((left->match_flags ^ right->match_flags) |
3189f8d62037SEdward Cree 	    ((left->flags ^ right->flags) &
3190f8d62037SEdward Cree 	     (EFX_FILTER_FLAG_RX | EFX_FILTER_FLAG_TX)))
3191f8d62037SEdward Cree 		return false;
3192f8d62037SEdward Cree 
3193f8d62037SEdward Cree 	return memcmp(&left->outer_vid, &right->outer_vid,
3194f8d62037SEdward Cree 		      sizeof(struct efx_filter_spec) -
3195f8d62037SEdward Cree 		      offsetof(struct efx_filter_spec, outer_vid)) == 0;
3196f8d62037SEdward Cree }
3197f8d62037SEdward Cree 
3198f8d62037SEdward Cree u32 efx_filter_spec_hash(const struct efx_filter_spec *spec)
3199f8d62037SEdward Cree {
3200f8d62037SEdward Cree 	BUILD_BUG_ON(offsetof(struct efx_filter_spec, outer_vid) & 3);
3201f8d62037SEdward Cree 	return jhash2((const u32 *)&spec->outer_vid,
3202f8d62037SEdward Cree 		      (sizeof(struct efx_filter_spec) -
3203f8d62037SEdward Cree 		       offsetof(struct efx_filter_spec, outer_vid)) / 4,
3204f8d62037SEdward Cree 		      0);
3205f8d62037SEdward Cree }
3206f8d62037SEdward Cree 
3207f8d62037SEdward Cree #ifdef CONFIG_RFS_ACCEL
3208f8d62037SEdward Cree bool efx_rps_check_rule(struct efx_arfs_rule *rule, unsigned int filter_idx,
3209f8d62037SEdward Cree 			bool *force)
3210f8d62037SEdward Cree {
3211f8d62037SEdward Cree 	if (rule->filter_id == EFX_ARFS_FILTER_ID_PENDING) {
3212f8d62037SEdward Cree 		/* ARFS is currently updating this entry, leave it */
3213f8d62037SEdward Cree 		return false;
3214f8d62037SEdward Cree 	}
3215f8d62037SEdward Cree 	if (rule->filter_id == EFX_ARFS_FILTER_ID_ERROR) {
3216f8d62037SEdward Cree 		/* ARFS tried and failed to update this, so it's probably out
3217f8d62037SEdward Cree 		 * of date.  Remove the filter and the ARFS rule entry.
3218f8d62037SEdward Cree 		 */
3219f8d62037SEdward Cree 		rule->filter_id = EFX_ARFS_FILTER_ID_REMOVING;
3220f8d62037SEdward Cree 		*force = true;
3221f8d62037SEdward Cree 		return true;
3222f8d62037SEdward Cree 	} else if (WARN_ON(rule->filter_id != filter_idx)) { /* can't happen */
3223f8d62037SEdward Cree 		/* ARFS has moved on, so old filter is not needed.  Since we did
3224f8d62037SEdward Cree 		 * not mark the rule with EFX_ARFS_FILTER_ID_REMOVING, it will
3225f8d62037SEdward Cree 		 * not be removed by efx_rps_hash_del() subsequently.
3226f8d62037SEdward Cree 		 */
3227f8d62037SEdward Cree 		*force = true;
3228f8d62037SEdward Cree 		return true;
3229f8d62037SEdward Cree 	}
3230f8d62037SEdward Cree 	/* Remove it iff ARFS wants to. */
3231f8d62037SEdward Cree 	return true;
3232f8d62037SEdward Cree }
3233f8d62037SEdward Cree 
3234829eb053SColin Ian King static
3235f8d62037SEdward Cree struct hlist_head *efx_rps_hash_bucket(struct efx_nic *efx,
3236f8d62037SEdward Cree 				       const struct efx_filter_spec *spec)
3237f8d62037SEdward Cree {
3238f8d62037SEdward Cree 	u32 hash = efx_filter_spec_hash(spec);
3239f8d62037SEdward Cree 
3240013ff4a6SLance Roy 	lockdep_assert_held(&efx->rps_hash_lock);
3241f8d62037SEdward Cree 	if (!efx->rps_hash_table)
3242f8d62037SEdward Cree 		return NULL;
3243f8d62037SEdward Cree 	return &efx->rps_hash_table[hash % EFX_ARFS_HASH_TABLE_SIZE];
3244f8d62037SEdward Cree }
3245f8d62037SEdward Cree 
3246f8d62037SEdward Cree struct efx_arfs_rule *efx_rps_hash_find(struct efx_nic *efx,
3247f8d62037SEdward Cree 					const struct efx_filter_spec *spec)
3248f8d62037SEdward Cree {
3249f8d62037SEdward Cree 	struct efx_arfs_rule *rule;
3250f8d62037SEdward Cree 	struct hlist_head *head;
3251f8d62037SEdward Cree 	struct hlist_node *node;
3252f8d62037SEdward Cree 
3253f8d62037SEdward Cree 	head = efx_rps_hash_bucket(efx, spec);
3254f8d62037SEdward Cree 	if (!head)
3255f8d62037SEdward Cree 		return NULL;
3256f8d62037SEdward Cree 	hlist_for_each(node, head) {
3257f8d62037SEdward Cree 		rule = container_of(node, struct efx_arfs_rule, node);
3258f8d62037SEdward Cree 		if (efx_filter_spec_equal(spec, &rule->spec))
3259f8d62037SEdward Cree 			return rule;
3260f8d62037SEdward Cree 	}
3261f8d62037SEdward Cree 	return NULL;
3262f8d62037SEdward Cree }
3263f8d62037SEdward Cree 
3264f8d62037SEdward Cree struct efx_arfs_rule *efx_rps_hash_add(struct efx_nic *efx,
3265f8d62037SEdward Cree 				       const struct efx_filter_spec *spec,
3266f8d62037SEdward Cree 				       bool *new)
3267f8d62037SEdward Cree {
3268f8d62037SEdward Cree 	struct efx_arfs_rule *rule;
3269f8d62037SEdward Cree 	struct hlist_head *head;
3270f8d62037SEdward Cree 	struct hlist_node *node;
3271f8d62037SEdward Cree 
3272f8d62037SEdward Cree 	head = efx_rps_hash_bucket(efx, spec);
3273f8d62037SEdward Cree 	if (!head)
3274f8d62037SEdward Cree 		return NULL;
3275f8d62037SEdward Cree 	hlist_for_each(node, head) {
3276f8d62037SEdward Cree 		rule = container_of(node, struct efx_arfs_rule, node);
3277f8d62037SEdward Cree 		if (efx_filter_spec_equal(spec, &rule->spec)) {
3278f8d62037SEdward Cree 			*new = false;
3279f8d62037SEdward Cree 			return rule;
3280f8d62037SEdward Cree 		}
3281f8d62037SEdward Cree 	}
3282f8d62037SEdward Cree 	rule = kmalloc(sizeof(*rule), GFP_ATOMIC);
3283f8d62037SEdward Cree 	*new = true;
3284f8d62037SEdward Cree 	if (rule) {
3285f8d62037SEdward Cree 		memcpy(&rule->spec, spec, sizeof(rule->spec));
3286f8d62037SEdward Cree 		hlist_add_head(&rule->node, head);
3287f8d62037SEdward Cree 	}
3288f8d62037SEdward Cree 	return rule;
3289f8d62037SEdward Cree }
3290f8d62037SEdward Cree 
3291f8d62037SEdward Cree void efx_rps_hash_del(struct efx_nic *efx, const struct efx_filter_spec *spec)
3292f8d62037SEdward Cree {
3293f8d62037SEdward Cree 	struct efx_arfs_rule *rule;
3294f8d62037SEdward Cree 	struct hlist_head *head;
3295f8d62037SEdward Cree 	struct hlist_node *node;
3296f8d62037SEdward Cree 
3297f8d62037SEdward Cree 	head = efx_rps_hash_bucket(efx, spec);
3298f8d62037SEdward Cree 	if (WARN_ON(!head))
3299f8d62037SEdward Cree 		return;
3300f8d62037SEdward Cree 	hlist_for_each(node, head) {
3301f8d62037SEdward Cree 		rule = container_of(node, struct efx_arfs_rule, node);
3302f8d62037SEdward Cree 		if (efx_filter_spec_equal(spec, &rule->spec)) {
3303f8d62037SEdward Cree 			/* Someone already reused the entry.  We know that if
3304f8d62037SEdward Cree 			 * this check doesn't fire (i.e. filter_id == REMOVING)
3305f8d62037SEdward Cree 			 * then the REMOVING mark was put there by our caller,
3306f8d62037SEdward Cree 			 * because caller is holding a lock on filter table and
3307f8d62037SEdward Cree 			 * only holders of that lock set REMOVING.
3308f8d62037SEdward Cree 			 */
3309f8d62037SEdward Cree 			if (rule->filter_id != EFX_ARFS_FILTER_ID_REMOVING)
3310f8d62037SEdward Cree 				return;
3311f8d62037SEdward Cree 			hlist_del(node);
3312f8d62037SEdward Cree 			kfree(rule);
3313f8d62037SEdward Cree 			return;
3314f8d62037SEdward Cree 		}
3315f8d62037SEdward Cree 	}
3316f8d62037SEdward Cree 	/* We didn't find it. */
3317f8d62037SEdward Cree 	WARN_ON(1);
3318f8d62037SEdward Cree }
3319f8d62037SEdward Cree #endif
3320f8d62037SEdward Cree 
332142356d9aSEdward Cree /* RSS contexts.  We're using linked lists and crappy O(n) algorithms, because
332242356d9aSEdward Cree  * (a) this is an infrequent control-plane operation and (b) n is small (max 64)
332342356d9aSEdward Cree  */
3324e0a65e3cSEdward Cree struct efx_rss_context *efx_alloc_rss_context_entry(struct efx_nic *efx)
332542356d9aSEdward Cree {
3326e0a65e3cSEdward Cree 	struct list_head *head = &efx->rss_context.list;
332742356d9aSEdward Cree 	struct efx_rss_context *ctx, *new;
332842356d9aSEdward Cree 	u32 id = 1; /* Don't use zero, that refers to the master RSS context */
332942356d9aSEdward Cree 
3330e0a65e3cSEdward Cree 	WARN_ON(!mutex_is_locked(&efx->rss_lock));
3331e0a65e3cSEdward Cree 
333242356d9aSEdward Cree 	/* Search for first gap in the numbering */
333342356d9aSEdward Cree 	list_for_each_entry(ctx, head, list) {
333442356d9aSEdward Cree 		if (ctx->user_id != id)
333542356d9aSEdward Cree 			break;
333642356d9aSEdward Cree 		id++;
333742356d9aSEdward Cree 		/* Check for wrap.  If this happens, we have nearly 2^32
333842356d9aSEdward Cree 		 * allocated RSS contexts, which seems unlikely.
333942356d9aSEdward Cree 		 */
334042356d9aSEdward Cree 		if (WARN_ON_ONCE(!id))
334142356d9aSEdward Cree 			return NULL;
334242356d9aSEdward Cree 	}
334342356d9aSEdward Cree 
334442356d9aSEdward Cree 	/* Create the new entry */
334542356d9aSEdward Cree 	new = kmalloc(sizeof(struct efx_rss_context), GFP_KERNEL);
334642356d9aSEdward Cree 	if (!new)
334742356d9aSEdward Cree 		return NULL;
334842356d9aSEdward Cree 	new->context_id = EFX_EF10_RSS_CONTEXT_INVALID;
334942356d9aSEdward Cree 	new->rx_hash_udp_4tuple = false;
335042356d9aSEdward Cree 
335142356d9aSEdward Cree 	/* Insert the new entry into the gap */
335242356d9aSEdward Cree 	new->user_id = id;
335342356d9aSEdward Cree 	list_add_tail(&new->list, &ctx->list);
335442356d9aSEdward Cree 	return new;
335542356d9aSEdward Cree }
335642356d9aSEdward Cree 
3357e0a65e3cSEdward Cree struct efx_rss_context *efx_find_rss_context_entry(struct efx_nic *efx, u32 id)
335842356d9aSEdward Cree {
3359e0a65e3cSEdward Cree 	struct list_head *head = &efx->rss_context.list;
336042356d9aSEdward Cree 	struct efx_rss_context *ctx;
336142356d9aSEdward Cree 
3362e0a65e3cSEdward Cree 	WARN_ON(!mutex_is_locked(&efx->rss_lock));
3363e0a65e3cSEdward Cree 
336442356d9aSEdward Cree 	list_for_each_entry(ctx, head, list)
336542356d9aSEdward Cree 		if (ctx->user_id == id)
336642356d9aSEdward Cree 			return ctx;
336742356d9aSEdward Cree 	return NULL;
336842356d9aSEdward Cree }
336942356d9aSEdward Cree 
337042356d9aSEdward Cree void efx_free_rss_context_entry(struct efx_rss_context *ctx)
337142356d9aSEdward Cree {
337242356d9aSEdward Cree 	list_del(&ctx->list);
337342356d9aSEdward Cree 	kfree(ctx);
337442356d9aSEdward Cree }
337542356d9aSEdward Cree 
3376874aeea5SJeff Kirsher /**************************************************************************
3377874aeea5SJeff Kirsher  *
3378874aeea5SJeff Kirsher  * PCI interface
3379874aeea5SJeff Kirsher  *
3380874aeea5SJeff Kirsher  **************************************************************************/
3381874aeea5SJeff Kirsher 
3382874aeea5SJeff Kirsher /* Main body of final NIC shutdown code
3383874aeea5SJeff Kirsher  * This is called only at module unload (or hotplug removal).
3384874aeea5SJeff Kirsher  */
3385874aeea5SJeff Kirsher static void efx_pci_remove_main(struct efx_nic *efx)
3386874aeea5SJeff Kirsher {
33877153f623SBen Hutchings 	/* Flush reset_work. It can no longer be scheduled since we
33887153f623SBen Hutchings 	 * are not READY.
33897153f623SBen Hutchings 	 */
33907153f623SBen Hutchings 	BUG_ON(efx->state == STATE_READY);
33917153f623SBen Hutchings 	cancel_work_sync(&efx->reset_work);
33927153f623SBen Hutchings 
3393d8291187SBen Hutchings 	efx_disable_interrupts(efx);
3394a83762d9SBert Kenward 	efx_clear_interrupt_affinity(efx);
3395874aeea5SJeff Kirsher 	efx_nic_fini_interrupt(efx);
3396874aeea5SJeff Kirsher 	efx_fini_port(efx);
3397874aeea5SJeff Kirsher 	efx->type->fini(efx);
3398874aeea5SJeff Kirsher 	efx_fini_napi(efx);
3399874aeea5SJeff Kirsher 	efx_remove_all(efx);
3400874aeea5SJeff Kirsher }
3401874aeea5SJeff Kirsher 
3402874aeea5SJeff Kirsher /* Final NIC shutdown
34032a3fc311SDaniel Pieczko  * This is called only at module unload (or hotplug removal).  A PF can call
34042a3fc311SDaniel Pieczko  * this on its VFs to ensure they are unbound first.
3405874aeea5SJeff Kirsher  */
3406874aeea5SJeff Kirsher static void efx_pci_remove(struct pci_dev *pci_dev)
3407874aeea5SJeff Kirsher {
3408874aeea5SJeff Kirsher 	struct efx_nic *efx;
3409874aeea5SJeff Kirsher 
3410874aeea5SJeff Kirsher 	efx = pci_get_drvdata(pci_dev);
3411874aeea5SJeff Kirsher 	if (!efx)
3412874aeea5SJeff Kirsher 		return;
3413874aeea5SJeff Kirsher 
3414874aeea5SJeff Kirsher 	/* Mark the NIC as fini, then stop the interface */
3415874aeea5SJeff Kirsher 	rtnl_lock();
34160bcf4a64SBen Hutchings 	efx_dissociate(efx);
3417874aeea5SJeff Kirsher 	dev_close(efx->net_dev);
3418d8291187SBen Hutchings 	efx_disable_interrupts(efx);
3419ea6bb99eSEdward Cree 	efx->state = STATE_UNINIT;
3420874aeea5SJeff Kirsher 	rtnl_unlock();
3421874aeea5SJeff Kirsher 
34227fa8d547SShradha Shah 	if (efx->type->sriov_fini)
3423d98a4ffeSShradha Shah 		efx->type->sriov_fini(efx);
34247fa8d547SShradha Shah 
3425874aeea5SJeff Kirsher 	efx_unregister_netdev(efx);
3426874aeea5SJeff Kirsher 
3427874aeea5SJeff Kirsher 	efx_mtd_remove(efx);
3428874aeea5SJeff Kirsher 
3429874aeea5SJeff Kirsher 	efx_pci_remove_main(efx);
3430874aeea5SJeff Kirsher 
3431874aeea5SJeff Kirsher 	efx_fini_io(efx);
3432874aeea5SJeff Kirsher 	netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
3433874aeea5SJeff Kirsher 
3434874aeea5SJeff Kirsher 	efx_fini_struct(efx);
3435874aeea5SJeff Kirsher 	free_netdev(efx->net_dev);
3436626950dbSAlexandre Rames 
3437626950dbSAlexandre Rames 	pci_disable_pcie_error_reporting(pci_dev);
3438874aeea5SJeff Kirsher };
3439874aeea5SJeff Kirsher 
3440460eeaa0SBen Hutchings /* NIC VPD information
3441460eeaa0SBen Hutchings  * Called during probe to display the part number of the
3442460eeaa0SBen Hutchings  * installed NIC.  VPD is potentially very large but this should
3443460eeaa0SBen Hutchings  * always appear within the first 512 bytes.
3444460eeaa0SBen Hutchings  */
3445460eeaa0SBen Hutchings #define SFC_VPD_LEN 512
3446ef215e64SBen Hutchings static void efx_probe_vpd_strings(struct efx_nic *efx)
3447460eeaa0SBen Hutchings {
3448460eeaa0SBen Hutchings 	struct pci_dev *dev = efx->pci_dev;
3449460eeaa0SBen Hutchings 	char vpd_data[SFC_VPD_LEN];
3450460eeaa0SBen Hutchings 	ssize_t vpd_size;
3451ef215e64SBen Hutchings 	int ro_start, ro_size, i, j;
3452460eeaa0SBen Hutchings 
3453460eeaa0SBen Hutchings 	/* Get the vpd data from the device */
3454460eeaa0SBen Hutchings 	vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
3455460eeaa0SBen Hutchings 	if (vpd_size <= 0) {
3456460eeaa0SBen Hutchings 		netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
3457460eeaa0SBen Hutchings 		return;
3458460eeaa0SBen Hutchings 	}
3459460eeaa0SBen Hutchings 
3460460eeaa0SBen Hutchings 	/* Get the Read only section */
3461ef215e64SBen Hutchings 	ro_start = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
3462ef215e64SBen Hutchings 	if (ro_start < 0) {
3463460eeaa0SBen Hutchings 		netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
3464460eeaa0SBen Hutchings 		return;
3465460eeaa0SBen Hutchings 	}
3466460eeaa0SBen Hutchings 
3467ef215e64SBen Hutchings 	ro_size = pci_vpd_lrdt_size(&vpd_data[ro_start]);
3468ef215e64SBen Hutchings 	j = ro_size;
3469ef215e64SBen Hutchings 	i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
3470460eeaa0SBen Hutchings 	if (i + j > vpd_size)
3471460eeaa0SBen Hutchings 		j = vpd_size - i;
3472460eeaa0SBen Hutchings 
3473460eeaa0SBen Hutchings 	/* Get the Part number */
3474460eeaa0SBen Hutchings 	i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
3475460eeaa0SBen Hutchings 	if (i < 0) {
3476460eeaa0SBen Hutchings 		netif_err(efx, drv, efx->net_dev, "Part number not found\n");
3477460eeaa0SBen Hutchings 		return;
3478460eeaa0SBen Hutchings 	}
3479460eeaa0SBen Hutchings 
3480460eeaa0SBen Hutchings 	j = pci_vpd_info_field_size(&vpd_data[i]);
3481460eeaa0SBen Hutchings 	i += PCI_VPD_INFO_FLD_HDR_SIZE;
3482460eeaa0SBen Hutchings 	if (i + j > vpd_size) {
3483460eeaa0SBen Hutchings 		netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
3484460eeaa0SBen Hutchings 		return;
3485460eeaa0SBen Hutchings 	}
3486460eeaa0SBen Hutchings 
3487460eeaa0SBen Hutchings 	netif_info(efx, drv, efx->net_dev,
3488460eeaa0SBen Hutchings 		   "Part Number : %.*s\n", j, &vpd_data[i]);
3489ef215e64SBen Hutchings 
3490ef215e64SBen Hutchings 	i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
3491ef215e64SBen Hutchings 	j = ro_size;
3492ef215e64SBen Hutchings 	i = pci_vpd_find_info_keyword(vpd_data, i, j, "SN");
3493ef215e64SBen Hutchings 	if (i < 0) {
3494ef215e64SBen Hutchings 		netif_err(efx, drv, efx->net_dev, "Serial number not found\n");
3495ef215e64SBen Hutchings 		return;
3496ef215e64SBen Hutchings 	}
3497ef215e64SBen Hutchings 
3498ef215e64SBen Hutchings 	j = pci_vpd_info_field_size(&vpd_data[i]);
3499ef215e64SBen Hutchings 	i += PCI_VPD_INFO_FLD_HDR_SIZE;
3500ef215e64SBen Hutchings 	if (i + j > vpd_size) {
3501ef215e64SBen Hutchings 		netif_err(efx, drv, efx->net_dev, "Incomplete serial number\n");
3502ef215e64SBen Hutchings 		return;
3503ef215e64SBen Hutchings 	}
3504ef215e64SBen Hutchings 
3505ef215e64SBen Hutchings 	efx->vpd_sn = kmalloc(j + 1, GFP_KERNEL);
3506ef215e64SBen Hutchings 	if (!efx->vpd_sn)
3507ef215e64SBen Hutchings 		return;
3508ef215e64SBen Hutchings 
3509ef215e64SBen Hutchings 	snprintf(efx->vpd_sn, j + 1, "%s", &vpd_data[i]);
3510460eeaa0SBen Hutchings }
3511460eeaa0SBen Hutchings 
3512460eeaa0SBen Hutchings 
3513874aeea5SJeff Kirsher /* Main body of NIC initialisation
3514874aeea5SJeff Kirsher  * This is called at module load (or hotplug insertion, theoretically).
3515874aeea5SJeff Kirsher  */
3516874aeea5SJeff Kirsher static int efx_pci_probe_main(struct efx_nic *efx)
3517874aeea5SJeff Kirsher {
3518874aeea5SJeff Kirsher 	int rc;
3519874aeea5SJeff Kirsher 
3520874aeea5SJeff Kirsher 	/* Do start-of-day initialisation */
3521874aeea5SJeff Kirsher 	rc = efx_probe_all(efx);
3522874aeea5SJeff Kirsher 	if (rc)
3523874aeea5SJeff Kirsher 		goto fail1;
3524874aeea5SJeff Kirsher 
3525874aeea5SJeff Kirsher 	efx_init_napi(efx);
3526874aeea5SJeff Kirsher 
3527193f2003SBert Kenward 	down_write(&efx->filter_sem);
3528874aeea5SJeff Kirsher 	rc = efx->type->init(efx);
3529193f2003SBert Kenward 	up_write(&efx->filter_sem);
3530874aeea5SJeff Kirsher 	if (rc) {
3531874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev,
3532874aeea5SJeff Kirsher 			  "failed to initialise NIC\n");
3533874aeea5SJeff Kirsher 		goto fail3;
3534874aeea5SJeff Kirsher 	}
3535874aeea5SJeff Kirsher 
3536874aeea5SJeff Kirsher 	rc = efx_init_port(efx);
3537874aeea5SJeff Kirsher 	if (rc) {
3538874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev,
3539874aeea5SJeff Kirsher 			  "failed to initialise port\n");
3540874aeea5SJeff Kirsher 		goto fail4;
3541874aeea5SJeff Kirsher 	}
3542874aeea5SJeff Kirsher 
3543874aeea5SJeff Kirsher 	rc = efx_nic_init_interrupt(efx);
3544874aeea5SJeff Kirsher 	if (rc)
3545874aeea5SJeff Kirsher 		goto fail5;
3546a83762d9SBert Kenward 
3547a83762d9SBert Kenward 	efx_set_interrupt_affinity(efx);
3548261e4d96SJon Cooper 	rc = efx_enable_interrupts(efx);
3549261e4d96SJon Cooper 	if (rc)
3550261e4d96SJon Cooper 		goto fail6;
3551874aeea5SJeff Kirsher 
3552874aeea5SJeff Kirsher 	return 0;
3553874aeea5SJeff Kirsher 
3554261e4d96SJon Cooper  fail6:
3555a83762d9SBert Kenward 	efx_clear_interrupt_affinity(efx);
3556261e4d96SJon Cooper 	efx_nic_fini_interrupt(efx);
3557874aeea5SJeff Kirsher  fail5:
3558874aeea5SJeff Kirsher 	efx_fini_port(efx);
3559874aeea5SJeff Kirsher  fail4:
3560874aeea5SJeff Kirsher 	efx->type->fini(efx);
3561874aeea5SJeff Kirsher  fail3:
3562874aeea5SJeff Kirsher 	efx_fini_napi(efx);
3563874aeea5SJeff Kirsher 	efx_remove_all(efx);
3564874aeea5SJeff Kirsher  fail1:
3565874aeea5SJeff Kirsher 	return rc;
3566874aeea5SJeff Kirsher }
3567874aeea5SJeff Kirsher 
35688a531400SJon Cooper static int efx_pci_probe_post_io(struct efx_nic *efx)
35698a531400SJon Cooper {
35708a531400SJon Cooper 	struct net_device *net_dev = efx->net_dev;
35718a531400SJon Cooper 	int rc = efx_pci_probe_main(efx);
35728a531400SJon Cooper 
35738a531400SJon Cooper 	if (rc)
35748a531400SJon Cooper 		return rc;
35758a531400SJon Cooper 
35768a531400SJon Cooper 	if (efx->type->sriov_init) {
35778a531400SJon Cooper 		rc = efx->type->sriov_init(efx);
35788a531400SJon Cooper 		if (rc)
35798a531400SJon Cooper 			netif_err(efx, probe, efx->net_dev,
35808a531400SJon Cooper 				  "SR-IOV can't be enabled rc %d\n", rc);
35818a531400SJon Cooper 	}
35828a531400SJon Cooper 
35838a531400SJon Cooper 	/* Determine netdevice features */
35848a531400SJon Cooper 	net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
35856978729fSEdward Cree 			      NETIF_F_TSO | NETIF_F_RXCSUM | NETIF_F_RXALL);
35868a531400SJon Cooper 	if (efx->type->offload_features & (NETIF_F_IPV6_CSUM | NETIF_F_HW_CSUM))
35878a531400SJon Cooper 		net_dev->features |= NETIF_F_TSO6;
35888a531400SJon Cooper 	/* Check whether device supports TSO */
35898a531400SJon Cooper 	if (!efx->type->tso_versions || !efx->type->tso_versions(efx))
35908a531400SJon Cooper 		net_dev->features &= ~NETIF_F_ALL_TSO;
35918a531400SJon Cooper 	/* Mask for features that also apply to VLAN devices */
35928a531400SJon Cooper 	net_dev->vlan_features |= (NETIF_F_HW_CSUM | NETIF_F_SG |
35938a531400SJon Cooper 				   NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
35948a531400SJon Cooper 				   NETIF_F_RXCSUM);
35958a531400SJon Cooper 
35966978729fSEdward Cree 	net_dev->hw_features |= net_dev->features & ~efx->fixed_features;
35976978729fSEdward Cree 
35986978729fSEdward Cree 	/* Disable receiving frames with bad FCS, by default. */
35996978729fSEdward Cree 	net_dev->features &= ~NETIF_F_RXALL;
36008a531400SJon Cooper 
36018a531400SJon Cooper 	/* Disable VLAN filtering by default.  It may be enforced if
36028a531400SJon Cooper 	 * the feature is fixed (i.e. VLAN filters are required to
36038a531400SJon Cooper 	 * receive VLAN tagged packets due to vPort restrictions).
36048a531400SJon Cooper 	 */
36058a531400SJon Cooper 	net_dev->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
36068a531400SJon Cooper 	net_dev->features |= efx->fixed_features;
36078a531400SJon Cooper 
36088a531400SJon Cooper 	rc = efx_register_netdev(efx);
36098a531400SJon Cooper 	if (!rc)
36108a531400SJon Cooper 		return 0;
36118a531400SJon Cooper 
36128a531400SJon Cooper 	efx_pci_remove_main(efx);
36138a531400SJon Cooper 	return rc;
36148a531400SJon Cooper }
36158a531400SJon Cooper 
3616874aeea5SJeff Kirsher /* NIC initialisation
3617874aeea5SJeff Kirsher  *
3618874aeea5SJeff Kirsher  * This is called at module load (or hotplug insertion,
361973ba7b68SBen Hutchings  * theoretically).  It sets up PCI mappings, resets the NIC,
3620874aeea5SJeff Kirsher  * sets up and registers the network devices with the kernel and hooks
3621874aeea5SJeff Kirsher  * the interrupt service routine.  It does not prepare the device for
3622874aeea5SJeff Kirsher  * transmission; this is left to the first time one of the network
3623874aeea5SJeff Kirsher  * interfaces is brought up (i.e. efx_net_open).
3624874aeea5SJeff Kirsher  */
362587d1fc11SBill Pemberton static int efx_pci_probe(struct pci_dev *pci_dev,
3626874aeea5SJeff Kirsher 			 const struct pci_device_id *entry)
3627874aeea5SJeff Kirsher {
3628874aeea5SJeff Kirsher 	struct net_device *net_dev;
3629874aeea5SJeff Kirsher 	struct efx_nic *efx;
3630fadac6aaSBen Hutchings 	int rc;
3631874aeea5SJeff Kirsher 
3632874aeea5SJeff Kirsher 	/* Allocate and initialise a struct net_device and struct efx_nic */
3633874aeea5SJeff Kirsher 	net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
3634874aeea5SJeff Kirsher 				     EFX_MAX_RX_QUEUES);
3635874aeea5SJeff Kirsher 	if (!net_dev)
3636874aeea5SJeff Kirsher 		return -ENOMEM;
3637adeb15aaSBen Hutchings 	efx = netdev_priv(net_dev);
3638adeb15aaSBen Hutchings 	efx->type = (const struct efx_nic_type *) entry->driver_data;
3639ebfcd0fdSAndrew Rybchenko 	efx->fixed_features |= NETIF_F_HIGHDMA;
3640eb7cfd8cSAndrew Rybchenko 
3641874aeea5SJeff Kirsher 	pci_set_drvdata(pci_dev, efx);
3642874aeea5SJeff Kirsher 	SET_NETDEV_DEV(net_dev, &pci_dev->dev);
3643adeb15aaSBen Hutchings 	rc = efx_init_struct(efx, pci_dev, net_dev);
3644874aeea5SJeff Kirsher 	if (rc)
3645874aeea5SJeff Kirsher 		goto fail1;
3646874aeea5SJeff Kirsher 
3647874aeea5SJeff Kirsher 	netif_info(efx, probe, efx->net_dev,
3648874aeea5SJeff Kirsher 		   "Solarflare NIC detected\n");
3649874aeea5SJeff Kirsher 
36506f7f8aa6SShradha Shah 	if (!efx->type->is_vf)
3651ef215e64SBen Hutchings 		efx_probe_vpd_strings(efx);
3652460eeaa0SBen Hutchings 
3653874aeea5SJeff Kirsher 	/* Set up basic I/O (BAR mappings etc) */
3654874aeea5SJeff Kirsher 	rc = efx_init_io(efx);
3655874aeea5SJeff Kirsher 	if (rc)
3656874aeea5SJeff Kirsher 		goto fail2;
3657874aeea5SJeff Kirsher 
36588a531400SJon Cooper 	rc = efx_pci_probe_post_io(efx);
36598a531400SJon Cooper 	if (rc) {
36608a531400SJon Cooper 		/* On failure, retry once immediately.
36618a531400SJon Cooper 		 * If we aborted probe due to a scheduled reset, dismiss it.
36628a531400SJon Cooper 		 */
36638a531400SJon Cooper 		efx->reset_pending = 0;
36648a531400SJon Cooper 		rc = efx_pci_probe_post_io(efx);
36658a531400SJon Cooper 		if (rc) {
36668a531400SJon Cooper 			/* On another failure, retry once more
36678a531400SJon Cooper 			 * after a 50-305ms delay.
36688a531400SJon Cooper 			 */
36698a531400SJon Cooper 			unsigned char r;
36708a531400SJon Cooper 
36718a531400SJon Cooper 			get_random_bytes(&r, 1);
36728a531400SJon Cooper 			msleep((unsigned int)r + 50);
36738a531400SJon Cooper 			efx->reset_pending = 0;
36748a531400SJon Cooper 			rc = efx_pci_probe_post_io(efx);
36758a531400SJon Cooper 		}
36768a531400SJon Cooper 	}
3677fadac6aaSBen Hutchings 	if (rc)
3678874aeea5SJeff Kirsher 		goto fail3;
3679874aeea5SJeff Kirsher 
3680874aeea5SJeff Kirsher 	netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
3681874aeea5SJeff Kirsher 
36827c43161cSBen Hutchings 	/* Try to create MTDs, but allow this to fail */
3683874aeea5SJeff Kirsher 	rtnl_lock();
36847c43161cSBen Hutchings 	rc = efx_mtd_probe(efx);
3685874aeea5SJeff Kirsher 	rtnl_unlock();
368609a04204SBert Kenward 	if (rc && rc != -EPERM)
36877c43161cSBen Hutchings 		netif_warn(efx, probe, efx->net_dev,
36887c43161cSBen Hutchings 			   "failed to create MTDs (%d)\n", rc);
36897c43161cSBen Hutchings 
369005cfee98SMartin Habets 	(void)pci_enable_pcie_error_reporting(pci_dev);
3691626950dbSAlexandre Rames 
3692e5fbd977SJon Cooper 	if (efx->type->udp_tnl_push_ports)
3693e5fbd977SJon Cooper 		efx->type->udp_tnl_push_ports(efx);
3694e5fbd977SJon Cooper 
3695874aeea5SJeff Kirsher 	return 0;
3696874aeea5SJeff Kirsher 
3697874aeea5SJeff Kirsher  fail3:
3698874aeea5SJeff Kirsher 	efx_fini_io(efx);
3699874aeea5SJeff Kirsher  fail2:
3700874aeea5SJeff Kirsher 	efx_fini_struct(efx);
3701874aeea5SJeff Kirsher  fail1:
3702874aeea5SJeff Kirsher 	WARN_ON(rc > 0);
3703874aeea5SJeff Kirsher 	netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
3704874aeea5SJeff Kirsher 	free_netdev(net_dev);
3705874aeea5SJeff Kirsher 	return rc;
3706874aeea5SJeff Kirsher }
3707874aeea5SJeff Kirsher 
3708834e23ddSShradha Shah /* efx_pci_sriov_configure returns the actual number of Virtual Functions
3709834e23ddSShradha Shah  * enabled on success
3710834e23ddSShradha Shah  */
3711834e23ddSShradha Shah #ifdef CONFIG_SFC_SRIOV
3712834e23ddSShradha Shah static int efx_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
3713834e23ddSShradha Shah {
3714834e23ddSShradha Shah 	int rc;
3715834e23ddSShradha Shah 	struct efx_nic *efx = pci_get_drvdata(dev);
3716834e23ddSShradha Shah 
3717834e23ddSShradha Shah 	if (efx->type->sriov_configure) {
3718834e23ddSShradha Shah 		rc = efx->type->sriov_configure(efx, num_vfs);
3719834e23ddSShradha Shah 		if (rc)
3720834e23ddSShradha Shah 			return rc;
3721834e23ddSShradha Shah 		else
3722834e23ddSShradha Shah 			return num_vfs;
3723834e23ddSShradha Shah 	} else
3724834e23ddSShradha Shah 		return -EOPNOTSUPP;
3725834e23ddSShradha Shah }
3726834e23ddSShradha Shah #endif
3727834e23ddSShradha Shah 
3728874aeea5SJeff Kirsher static int efx_pm_freeze(struct device *dev)
3729874aeea5SJeff Kirsher {
37303e03a8baSChuhong Yuan 	struct efx_nic *efx = dev_get_drvdata(dev);
3731874aeea5SJeff Kirsher 
373261da026dSBen Hutchings 	rtnl_lock();
373361da026dSBen Hutchings 
37346032fb56SBen Hutchings 	if (efx->state != STATE_DISABLED) {
3735f16aeea0SBen Hutchings 		efx->state = STATE_UNINIT;
3736874aeea5SJeff Kirsher 
3737c2f3b8e3SDaniel Pieczko 		efx_device_detach_sync(efx);
3738874aeea5SJeff Kirsher 
3739874aeea5SJeff Kirsher 		efx_stop_all(efx);
3740d8291187SBen Hutchings 		efx_disable_interrupts(efx);
37416032fb56SBen Hutchings 	}
3742874aeea5SJeff Kirsher 
374361da026dSBen Hutchings 	rtnl_unlock();
374461da026dSBen Hutchings 
3745874aeea5SJeff Kirsher 	return 0;
3746874aeea5SJeff Kirsher }
3747874aeea5SJeff Kirsher 
3748874aeea5SJeff Kirsher static int efx_pm_thaw(struct device *dev)
3749874aeea5SJeff Kirsher {
3750261e4d96SJon Cooper 	int rc;
37513e03a8baSChuhong Yuan 	struct efx_nic *efx = dev_get_drvdata(dev);
3752874aeea5SJeff Kirsher 
375361da026dSBen Hutchings 	rtnl_lock();
375461da026dSBen Hutchings 
37556032fb56SBen Hutchings 	if (efx->state != STATE_DISABLED) {
3756261e4d96SJon Cooper 		rc = efx_enable_interrupts(efx);
3757261e4d96SJon Cooper 		if (rc)
3758261e4d96SJon Cooper 			goto fail;
3759874aeea5SJeff Kirsher 
3760874aeea5SJeff Kirsher 		mutex_lock(&efx->mac_lock);
3761874aeea5SJeff Kirsher 		efx->phy_op->reconfigure(efx);
3762874aeea5SJeff Kirsher 		mutex_unlock(&efx->mac_lock);
3763874aeea5SJeff Kirsher 
3764874aeea5SJeff Kirsher 		efx_start_all(efx);
3765874aeea5SJeff Kirsher 
37669c568fd8SPeter Dunning 		efx_device_attach_if_not_resetting(efx);
3767874aeea5SJeff Kirsher 
3768f16aeea0SBen Hutchings 		efx->state = STATE_READY;
3769874aeea5SJeff Kirsher 
3770874aeea5SJeff Kirsher 		efx->type->resume_wol(efx);
37716032fb56SBen Hutchings 	}
3772874aeea5SJeff Kirsher 
377361da026dSBen Hutchings 	rtnl_unlock();
377461da026dSBen Hutchings 
3775874aeea5SJeff Kirsher 	/* Reschedule any quenched resets scheduled during efx_pm_freeze() */
3776874aeea5SJeff Kirsher 	queue_work(reset_workqueue, &efx->reset_work);
3777874aeea5SJeff Kirsher 
3778874aeea5SJeff Kirsher 	return 0;
3779261e4d96SJon Cooper 
3780261e4d96SJon Cooper fail:
3781261e4d96SJon Cooper 	rtnl_unlock();
3782261e4d96SJon Cooper 
3783261e4d96SJon Cooper 	return rc;
3784874aeea5SJeff Kirsher }
3785874aeea5SJeff Kirsher 
3786874aeea5SJeff Kirsher static int efx_pm_poweroff(struct device *dev)
3787874aeea5SJeff Kirsher {
3788874aeea5SJeff Kirsher 	struct pci_dev *pci_dev = to_pci_dev(dev);
3789874aeea5SJeff Kirsher 	struct efx_nic *efx = pci_get_drvdata(pci_dev);
3790874aeea5SJeff Kirsher 
3791874aeea5SJeff Kirsher 	efx->type->fini(efx);
3792874aeea5SJeff Kirsher 
3793874aeea5SJeff Kirsher 	efx->reset_pending = 0;
3794874aeea5SJeff Kirsher 
3795874aeea5SJeff Kirsher 	pci_save_state(pci_dev);
3796874aeea5SJeff Kirsher 	return pci_set_power_state(pci_dev, PCI_D3hot);
3797874aeea5SJeff Kirsher }
3798874aeea5SJeff Kirsher 
3799874aeea5SJeff Kirsher /* Used for both resume and restore */
3800874aeea5SJeff Kirsher static int efx_pm_resume(struct device *dev)
3801874aeea5SJeff Kirsher {
3802874aeea5SJeff Kirsher 	struct pci_dev *pci_dev = to_pci_dev(dev);
3803874aeea5SJeff Kirsher 	struct efx_nic *efx = pci_get_drvdata(pci_dev);
3804874aeea5SJeff Kirsher 	int rc;
3805874aeea5SJeff Kirsher 
3806874aeea5SJeff Kirsher 	rc = pci_set_power_state(pci_dev, PCI_D0);
3807874aeea5SJeff Kirsher 	if (rc)
3808874aeea5SJeff Kirsher 		return rc;
3809874aeea5SJeff Kirsher 	pci_restore_state(pci_dev);
3810874aeea5SJeff Kirsher 	rc = pci_enable_device(pci_dev);
3811874aeea5SJeff Kirsher 	if (rc)
3812874aeea5SJeff Kirsher 		return rc;
3813874aeea5SJeff Kirsher 	pci_set_master(efx->pci_dev);
3814874aeea5SJeff Kirsher 	rc = efx->type->reset(efx, RESET_TYPE_ALL);
3815874aeea5SJeff Kirsher 	if (rc)
3816874aeea5SJeff Kirsher 		return rc;
3817193f2003SBert Kenward 	down_write(&efx->filter_sem);
3818874aeea5SJeff Kirsher 	rc = efx->type->init(efx);
3819193f2003SBert Kenward 	up_write(&efx->filter_sem);
3820874aeea5SJeff Kirsher 	if (rc)
3821874aeea5SJeff Kirsher 		return rc;
3822261e4d96SJon Cooper 	rc = efx_pm_thaw(dev);
3823261e4d96SJon Cooper 	return rc;
3824874aeea5SJeff Kirsher }
3825874aeea5SJeff Kirsher 
3826874aeea5SJeff Kirsher static int efx_pm_suspend(struct device *dev)
3827874aeea5SJeff Kirsher {
3828874aeea5SJeff Kirsher 	int rc;
3829874aeea5SJeff Kirsher 
3830874aeea5SJeff Kirsher 	efx_pm_freeze(dev);
3831874aeea5SJeff Kirsher 	rc = efx_pm_poweroff(dev);
3832874aeea5SJeff Kirsher 	if (rc)
3833874aeea5SJeff Kirsher 		efx_pm_resume(dev);
3834874aeea5SJeff Kirsher 	return rc;
3835874aeea5SJeff Kirsher }
3836874aeea5SJeff Kirsher 
383718e83e4cSBen Hutchings static const struct dev_pm_ops efx_pm_ops = {
3838874aeea5SJeff Kirsher 	.suspend	= efx_pm_suspend,
3839874aeea5SJeff Kirsher 	.resume		= efx_pm_resume,
3840874aeea5SJeff Kirsher 	.freeze		= efx_pm_freeze,
3841874aeea5SJeff Kirsher 	.thaw		= efx_pm_thaw,
3842874aeea5SJeff Kirsher 	.poweroff	= efx_pm_poweroff,
3843874aeea5SJeff Kirsher 	.restore	= efx_pm_resume,
3844874aeea5SJeff Kirsher };
3845874aeea5SJeff Kirsher 
3846626950dbSAlexandre Rames /* A PCI error affecting this device was detected.
3847626950dbSAlexandre Rames  * At this point MMIO and DMA may be disabled.
3848626950dbSAlexandre Rames  * Stop the software path and request a slot reset.
3849626950dbSAlexandre Rames  */
3850debd0034Sstephen hemminger static pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
3851626950dbSAlexandre Rames 					      enum pci_channel_state state)
3852626950dbSAlexandre Rames {
3853626950dbSAlexandre Rames 	pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
3854626950dbSAlexandre Rames 	struct efx_nic *efx = pci_get_drvdata(pdev);
3855626950dbSAlexandre Rames 
3856626950dbSAlexandre Rames 	if (state == pci_channel_io_perm_failure)
3857626950dbSAlexandre Rames 		return PCI_ERS_RESULT_DISCONNECT;
3858626950dbSAlexandre Rames 
3859626950dbSAlexandre Rames 	rtnl_lock();
3860626950dbSAlexandre Rames 
3861626950dbSAlexandre Rames 	if (efx->state != STATE_DISABLED) {
3862626950dbSAlexandre Rames 		efx->state = STATE_RECOVERY;
3863626950dbSAlexandre Rames 		efx->reset_pending = 0;
3864626950dbSAlexandre Rames 
3865626950dbSAlexandre Rames 		efx_device_detach_sync(efx);
3866626950dbSAlexandre Rames 
3867626950dbSAlexandre Rames 		efx_stop_all(efx);
3868d8291187SBen Hutchings 		efx_disable_interrupts(efx);
3869626950dbSAlexandre Rames 
3870626950dbSAlexandre Rames 		status = PCI_ERS_RESULT_NEED_RESET;
3871626950dbSAlexandre Rames 	} else {
3872626950dbSAlexandre Rames 		/* If the interface is disabled we don't want to do anything
3873626950dbSAlexandre Rames 		 * with it.
3874626950dbSAlexandre Rames 		 */
3875626950dbSAlexandre Rames 		status = PCI_ERS_RESULT_RECOVERED;
3876626950dbSAlexandre Rames 	}
3877626950dbSAlexandre Rames 
3878626950dbSAlexandre Rames 	rtnl_unlock();
3879626950dbSAlexandre Rames 
3880626950dbSAlexandre Rames 	pci_disable_device(pdev);
3881626950dbSAlexandre Rames 
3882626950dbSAlexandre Rames 	return status;
3883626950dbSAlexandre Rames }
3884626950dbSAlexandre Rames 
3885dbedd44eSJoe Perches /* Fake a successful reset, which will be performed later in efx_io_resume. */
3886debd0034Sstephen hemminger static pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
3887626950dbSAlexandre Rames {
3888626950dbSAlexandre Rames 	struct efx_nic *efx = pci_get_drvdata(pdev);
3889626950dbSAlexandre Rames 	pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
3890626950dbSAlexandre Rames 
3891626950dbSAlexandre Rames 	if (pci_enable_device(pdev)) {
3892626950dbSAlexandre Rames 		netif_err(efx, hw, efx->net_dev,
3893626950dbSAlexandre Rames 			  "Cannot re-enable PCI device after reset.\n");
3894626950dbSAlexandre Rames 		status =  PCI_ERS_RESULT_DISCONNECT;
3895626950dbSAlexandre Rames 	}
3896626950dbSAlexandre Rames 
3897626950dbSAlexandre Rames 	return status;
3898626950dbSAlexandre Rames }
3899626950dbSAlexandre Rames 
3900626950dbSAlexandre Rames /* Perform the actual reset and resume I/O operations. */
3901626950dbSAlexandre Rames static void efx_io_resume(struct pci_dev *pdev)
3902626950dbSAlexandre Rames {
3903626950dbSAlexandre Rames 	struct efx_nic *efx = pci_get_drvdata(pdev);
3904626950dbSAlexandre Rames 	int rc;
3905626950dbSAlexandre Rames 
3906626950dbSAlexandre Rames 	rtnl_lock();
3907626950dbSAlexandre Rames 
3908626950dbSAlexandre Rames 	if (efx->state == STATE_DISABLED)
3909626950dbSAlexandre Rames 		goto out;
3910626950dbSAlexandre Rames 
3911626950dbSAlexandre Rames 	rc = efx_reset(efx, RESET_TYPE_ALL);
3912626950dbSAlexandre Rames 	if (rc) {
3913626950dbSAlexandre Rames 		netif_err(efx, hw, efx->net_dev,
3914626950dbSAlexandre Rames 			  "efx_reset failed after PCI error (%d)\n", rc);
3915626950dbSAlexandre Rames 	} else {
3916626950dbSAlexandre Rames 		efx->state = STATE_READY;
3917626950dbSAlexandre Rames 		netif_dbg(efx, hw, efx->net_dev,
3918626950dbSAlexandre Rames 			  "Done resetting and resuming IO after PCI error.\n");
3919626950dbSAlexandre Rames 	}
3920626950dbSAlexandre Rames 
3921626950dbSAlexandre Rames out:
3922626950dbSAlexandre Rames 	rtnl_unlock();
3923626950dbSAlexandre Rames }
3924626950dbSAlexandre Rames 
3925626950dbSAlexandre Rames /* For simplicity and reliability, we always require a slot reset and try to
3926626950dbSAlexandre Rames  * reset the hardware when a pci error affecting the device is detected.
3927626950dbSAlexandre Rames  * We leave both the link_reset and mmio_enabled callback unimplemented:
3928626950dbSAlexandre Rames  * with our request for slot reset the mmio_enabled callback will never be
3929626950dbSAlexandre Rames  * called, and the link_reset callback is not used by AER or EEH mechanisms.
3930626950dbSAlexandre Rames  */
3931c300366bSJulia Lawall static const struct pci_error_handlers efx_err_handlers = {
3932626950dbSAlexandre Rames 	.error_detected = efx_io_error_detected,
3933626950dbSAlexandre Rames 	.slot_reset	= efx_io_slot_reset,
3934626950dbSAlexandre Rames 	.resume		= efx_io_resume,
3935626950dbSAlexandre Rames };
3936626950dbSAlexandre Rames 
3937874aeea5SJeff Kirsher static struct pci_driver efx_pci_driver = {
3938874aeea5SJeff Kirsher 	.name		= KBUILD_MODNAME,
3939874aeea5SJeff Kirsher 	.id_table	= efx_pci_table,
3940874aeea5SJeff Kirsher 	.probe		= efx_pci_probe,
3941874aeea5SJeff Kirsher 	.remove		= efx_pci_remove,
3942874aeea5SJeff Kirsher 	.driver.pm	= &efx_pm_ops,
3943626950dbSAlexandre Rames 	.err_handler	= &efx_err_handlers,
3944834e23ddSShradha Shah #ifdef CONFIG_SFC_SRIOV
3945834e23ddSShradha Shah 	.sriov_configure = efx_pci_sriov_configure,
3946834e23ddSShradha Shah #endif
3947874aeea5SJeff Kirsher };
3948874aeea5SJeff Kirsher 
3949874aeea5SJeff Kirsher /**************************************************************************
3950874aeea5SJeff Kirsher  *
3951874aeea5SJeff Kirsher  * Kernel module interface
3952874aeea5SJeff Kirsher  *
3953874aeea5SJeff Kirsher  *************************************************************************/
3954874aeea5SJeff Kirsher 
3955874aeea5SJeff Kirsher module_param(interrupt_mode, uint, 0444);
3956874aeea5SJeff Kirsher MODULE_PARM_DESC(interrupt_mode,
3957874aeea5SJeff Kirsher 		 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
3958874aeea5SJeff Kirsher 
3959874aeea5SJeff Kirsher static int __init efx_init_module(void)
3960874aeea5SJeff Kirsher {
3961874aeea5SJeff Kirsher 	int rc;
3962874aeea5SJeff Kirsher 
3963874aeea5SJeff Kirsher 	printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
3964874aeea5SJeff Kirsher 
3965874aeea5SJeff Kirsher 	rc = register_netdevice_notifier(&efx_netdev_notifier);
3966874aeea5SJeff Kirsher 	if (rc)
3967874aeea5SJeff Kirsher 		goto err_notifier;
3968874aeea5SJeff Kirsher 
39697fa8d547SShradha Shah #ifdef CONFIG_SFC_SRIOV
3970cd2d5b52SBen Hutchings 	rc = efx_init_sriov();
3971cd2d5b52SBen Hutchings 	if (rc)
3972cd2d5b52SBen Hutchings 		goto err_sriov;
39737fa8d547SShradha Shah #endif
3974cd2d5b52SBen Hutchings 
3975874aeea5SJeff Kirsher 	reset_workqueue = create_singlethread_workqueue("sfc_reset");
3976874aeea5SJeff Kirsher 	if (!reset_workqueue) {
3977874aeea5SJeff Kirsher 		rc = -ENOMEM;
3978874aeea5SJeff Kirsher 		goto err_reset;
3979874aeea5SJeff Kirsher 	}
3980874aeea5SJeff Kirsher 
3981874aeea5SJeff Kirsher 	rc = pci_register_driver(&efx_pci_driver);
3982874aeea5SJeff Kirsher 	if (rc < 0)
3983874aeea5SJeff Kirsher 		goto err_pci;
3984874aeea5SJeff Kirsher 
3985874aeea5SJeff Kirsher 	return 0;
3986874aeea5SJeff Kirsher 
3987874aeea5SJeff Kirsher  err_pci:
3988874aeea5SJeff Kirsher 	destroy_workqueue(reset_workqueue);
3989874aeea5SJeff Kirsher  err_reset:
39907fa8d547SShradha Shah #ifdef CONFIG_SFC_SRIOV
3991cd2d5b52SBen Hutchings 	efx_fini_sriov();
3992cd2d5b52SBen Hutchings  err_sriov:
39937fa8d547SShradha Shah #endif
3994874aeea5SJeff Kirsher 	unregister_netdevice_notifier(&efx_netdev_notifier);
3995874aeea5SJeff Kirsher  err_notifier:
3996874aeea5SJeff Kirsher 	return rc;
3997874aeea5SJeff Kirsher }
3998874aeea5SJeff Kirsher 
3999874aeea5SJeff Kirsher static void __exit efx_exit_module(void)
4000874aeea5SJeff Kirsher {
4001874aeea5SJeff Kirsher 	printk(KERN_INFO "Solarflare NET driver unloading\n");
4002874aeea5SJeff Kirsher 
4003874aeea5SJeff Kirsher 	pci_unregister_driver(&efx_pci_driver);
4004874aeea5SJeff Kirsher 	destroy_workqueue(reset_workqueue);
40057fa8d547SShradha Shah #ifdef CONFIG_SFC_SRIOV
4006cd2d5b52SBen Hutchings 	efx_fini_sriov();
40077fa8d547SShradha Shah #endif
4008874aeea5SJeff Kirsher 	unregister_netdevice_notifier(&efx_netdev_notifier);
4009874aeea5SJeff Kirsher 
4010874aeea5SJeff Kirsher }
4011874aeea5SJeff Kirsher 
4012874aeea5SJeff Kirsher module_init(efx_init_module);
4013874aeea5SJeff Kirsher module_exit(efx_exit_module);
4014874aeea5SJeff Kirsher 
4015874aeea5SJeff Kirsher MODULE_AUTHOR("Solarflare Communications and "
4016874aeea5SJeff Kirsher 	      "Michael Brown <mbrown@fensystems.co.uk>");
40176a350fdbSBen Hutchings MODULE_DESCRIPTION("Solarflare network driver");
4018874aeea5SJeff Kirsher MODULE_LICENSE("GPL");
4019874aeea5SJeff Kirsher MODULE_DEVICE_TABLE(pci, efx_pci_table);
402014077e9eSEdward Cree MODULE_VERSION(EFX_DRIVER_VERSION);
4021