xref: /openbmc/linux/drivers/net/ethernet/sfc/efx.c (revision f820c0ac)
1874aeea5SJeff Kirsher /****************************************************************************
2f7a6d2c4SBen Hutchings  * Driver for Solarflare network controllers and boards
3874aeea5SJeff Kirsher  * Copyright 2005-2006 Fen Systems Ltd.
4f7a6d2c4SBen Hutchings  * Copyright 2005-2013 Solarflare Communications Inc.
5874aeea5SJeff Kirsher  *
6874aeea5SJeff Kirsher  * This program is free software; you can redistribute it and/or modify it
7874aeea5SJeff Kirsher  * under the terms of the GNU General Public License version 2 as published
8874aeea5SJeff Kirsher  * by the Free Software Foundation, incorporated herein by reference.
9874aeea5SJeff Kirsher  */
10874aeea5SJeff Kirsher 
11874aeea5SJeff Kirsher #include <linux/module.h>
12874aeea5SJeff Kirsher #include <linux/pci.h>
13874aeea5SJeff Kirsher #include <linux/netdevice.h>
14874aeea5SJeff Kirsher #include <linux/etherdevice.h>
15874aeea5SJeff Kirsher #include <linux/delay.h>
16874aeea5SJeff Kirsher #include <linux/notifier.h>
17874aeea5SJeff Kirsher #include <linux/ip.h>
18874aeea5SJeff Kirsher #include <linux/tcp.h>
19874aeea5SJeff Kirsher #include <linux/in.h>
20874aeea5SJeff Kirsher #include <linux/ethtool.h>
21874aeea5SJeff Kirsher #include <linux/topology.h>
22874aeea5SJeff Kirsher #include <linux/gfp.h>
23626950dbSAlexandre Rames #include <linux/aer.h>
24b28405b0SAlexandre Rames #include <linux/interrupt.h>
25874aeea5SJeff Kirsher #include "net_driver.h"
26874aeea5SJeff Kirsher #include "efx.h"
27874aeea5SJeff Kirsher #include "nic.h"
28dd40781eSBen Hutchings #include "selftest.h"
297fa8d547SShradha Shah #include "sriov.h"
30874aeea5SJeff Kirsher 
31874aeea5SJeff Kirsher #include "mcdi.h"
32874aeea5SJeff Kirsher #include "workarounds.h"
33874aeea5SJeff Kirsher 
34874aeea5SJeff Kirsher /**************************************************************************
35874aeea5SJeff Kirsher  *
36874aeea5SJeff Kirsher  * Type name strings
37874aeea5SJeff Kirsher  *
38874aeea5SJeff Kirsher  **************************************************************************
39874aeea5SJeff Kirsher  */
40874aeea5SJeff Kirsher 
41874aeea5SJeff Kirsher /* Loopback mode names (see LOOPBACK_MODE()) */
42874aeea5SJeff Kirsher const unsigned int efx_loopback_mode_max = LOOPBACK_MAX;
4318e83e4cSBen Hutchings const char *const efx_loopback_mode_names[] = {
44874aeea5SJeff Kirsher 	[LOOPBACK_NONE]		= "NONE",
45874aeea5SJeff Kirsher 	[LOOPBACK_DATA]		= "DATAPATH",
46874aeea5SJeff Kirsher 	[LOOPBACK_GMAC]		= "GMAC",
47874aeea5SJeff Kirsher 	[LOOPBACK_XGMII]	= "XGMII",
48874aeea5SJeff Kirsher 	[LOOPBACK_XGXS]		= "XGXS",
49874aeea5SJeff Kirsher 	[LOOPBACK_XAUI]		= "XAUI",
50874aeea5SJeff Kirsher 	[LOOPBACK_GMII]		= "GMII",
51874aeea5SJeff Kirsher 	[LOOPBACK_SGMII]	= "SGMII",
52874aeea5SJeff Kirsher 	[LOOPBACK_XGBR]		= "XGBR",
53874aeea5SJeff Kirsher 	[LOOPBACK_XFI]		= "XFI",
54874aeea5SJeff Kirsher 	[LOOPBACK_XAUI_FAR]	= "XAUI_FAR",
55874aeea5SJeff Kirsher 	[LOOPBACK_GMII_FAR]	= "GMII_FAR",
56874aeea5SJeff Kirsher 	[LOOPBACK_SGMII_FAR]	= "SGMII_FAR",
57874aeea5SJeff Kirsher 	[LOOPBACK_XFI_FAR]	= "XFI_FAR",
58874aeea5SJeff Kirsher 	[LOOPBACK_GPHY]		= "GPHY",
59874aeea5SJeff Kirsher 	[LOOPBACK_PHYXS]	= "PHYXS",
60874aeea5SJeff Kirsher 	[LOOPBACK_PCS]		= "PCS",
61874aeea5SJeff Kirsher 	[LOOPBACK_PMAPMD]	= "PMA/PMD",
62874aeea5SJeff Kirsher 	[LOOPBACK_XPORT]	= "XPORT",
63874aeea5SJeff Kirsher 	[LOOPBACK_XGMII_WS]	= "XGMII_WS",
64874aeea5SJeff Kirsher 	[LOOPBACK_XAUI_WS]	= "XAUI_WS",
65874aeea5SJeff Kirsher 	[LOOPBACK_XAUI_WS_FAR]  = "XAUI_WS_FAR",
66874aeea5SJeff Kirsher 	[LOOPBACK_XAUI_WS_NEAR] = "XAUI_WS_NEAR",
67874aeea5SJeff Kirsher 	[LOOPBACK_GMII_WS]	= "GMII_WS",
68874aeea5SJeff Kirsher 	[LOOPBACK_XFI_WS]	= "XFI_WS",
69874aeea5SJeff Kirsher 	[LOOPBACK_XFI_WS_FAR]	= "XFI_WS_FAR",
70874aeea5SJeff Kirsher 	[LOOPBACK_PHYXS_WS]	= "PHYXS_WS",
71874aeea5SJeff Kirsher };
72874aeea5SJeff Kirsher 
73874aeea5SJeff Kirsher const unsigned int efx_reset_type_max = RESET_TYPE_MAX;
7418e83e4cSBen Hutchings const char *const efx_reset_type_names[] = {
75874aeea5SJeff Kirsher 	[RESET_TYPE_INVISIBLE]          = "INVISIBLE",
76874aeea5SJeff Kirsher 	[RESET_TYPE_ALL]                = "ALL",
77626950dbSAlexandre Rames 	[RESET_TYPE_RECOVER_OR_ALL]     = "RECOVER_OR_ALL",
78874aeea5SJeff Kirsher 	[RESET_TYPE_WORLD]              = "WORLD",
79626950dbSAlexandre Rames 	[RESET_TYPE_RECOVER_OR_DISABLE] = "RECOVER_OR_DISABLE",
80087e9025SJon Cooper 	[RESET_TYPE_DATAPATH]           = "DATAPATH",
81e283546cSEdward Cree 	[RESET_TYPE_MC_BIST]		= "MC_BIST",
82874aeea5SJeff Kirsher 	[RESET_TYPE_DISABLE]            = "DISABLE",
83874aeea5SJeff Kirsher 	[RESET_TYPE_TX_WATCHDOG]        = "TX_WATCHDOG",
84874aeea5SJeff Kirsher 	[RESET_TYPE_INT_ERROR]          = "INT_ERROR",
853de82b91SAlexandre Rames 	[RESET_TYPE_DMA_ERROR]          = "DMA_ERROR",
86874aeea5SJeff Kirsher 	[RESET_TYPE_TX_SKIP]            = "TX_SKIP",
87874aeea5SJeff Kirsher 	[RESET_TYPE_MC_FAILURE]         = "MC_FAILURE",
88e283546cSEdward Cree 	[RESET_TYPE_MCDI_TIMEOUT]	= "MCDI_TIMEOUT (FLR)",
89874aeea5SJeff Kirsher };
90874aeea5SJeff Kirsher 
91874aeea5SJeff Kirsher /* Reset workqueue. If any NIC has a hardware failure then a reset will be
92874aeea5SJeff Kirsher  * queued onto this work queue. This is not a per-nic work queue, because
93874aeea5SJeff Kirsher  * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
94874aeea5SJeff Kirsher  */
95874aeea5SJeff Kirsher static struct workqueue_struct *reset_workqueue;
96874aeea5SJeff Kirsher 
9774cd60a4SJon Cooper /* How often and how many times to poll for a reset while waiting for a
9874cd60a4SJon Cooper  * BIST that another function started to complete.
9974cd60a4SJon Cooper  */
10074cd60a4SJon Cooper #define BIST_WAIT_DELAY_MS	100
10174cd60a4SJon Cooper #define BIST_WAIT_DELAY_COUNT	100
10274cd60a4SJon Cooper 
103874aeea5SJeff Kirsher /**************************************************************************
104874aeea5SJeff Kirsher  *
105874aeea5SJeff Kirsher  * Configurable values
106874aeea5SJeff Kirsher  *
107874aeea5SJeff Kirsher  *************************************************************************/
108874aeea5SJeff Kirsher 
109874aeea5SJeff Kirsher /*
110874aeea5SJeff Kirsher  * Use separate channels for TX and RX events
111874aeea5SJeff Kirsher  *
112874aeea5SJeff Kirsher  * Set this to 1 to use separate channels for TX and RX. It allows us
113874aeea5SJeff Kirsher  * to control interrupt affinity separately for TX and RX.
114874aeea5SJeff Kirsher  *
115874aeea5SJeff Kirsher  * This is only used in MSI-X interrupt mode
116874aeea5SJeff Kirsher  */
117b0fbdae1SShradha Shah bool efx_separate_tx_channels;
118b0fbdae1SShradha Shah module_param(efx_separate_tx_channels, bool, 0444);
119b0fbdae1SShradha Shah MODULE_PARM_DESC(efx_separate_tx_channels,
120874aeea5SJeff Kirsher 		 "Use separate channels for TX and RX");
121874aeea5SJeff Kirsher 
122874aeea5SJeff Kirsher /* This is the weight assigned to each of the (per-channel) virtual
123874aeea5SJeff Kirsher  * NAPI devices.
124874aeea5SJeff Kirsher  */
125874aeea5SJeff Kirsher static int napi_weight = 64;
126874aeea5SJeff Kirsher 
127874aeea5SJeff Kirsher /* This is the time (in jiffies) between invocations of the hardware
128626950dbSAlexandre Rames  * monitor.
129626950dbSAlexandre Rames  * On Falcon-based NICs, this will:
130874aeea5SJeff Kirsher  * - Check the on-board hardware monitor;
131874aeea5SJeff Kirsher  * - Poll the link state and reconfigure the hardware as necessary.
132626950dbSAlexandre Rames  * On Siena-based NICs for power systems with EEH support, this will give EEH a
133626950dbSAlexandre Rames  * chance to start.
134874aeea5SJeff Kirsher  */
135874aeea5SJeff Kirsher static unsigned int efx_monitor_interval = 1 * HZ;
136874aeea5SJeff Kirsher 
137874aeea5SJeff Kirsher /* Initial interrupt moderation settings.  They can be modified after
138874aeea5SJeff Kirsher  * module load with ethtool.
139874aeea5SJeff Kirsher  *
140874aeea5SJeff Kirsher  * The default for RX should strike a balance between increasing the
141874aeea5SJeff Kirsher  * round-trip latency and reducing overhead.
142874aeea5SJeff Kirsher  */
143874aeea5SJeff Kirsher static unsigned int rx_irq_mod_usec = 60;
144874aeea5SJeff Kirsher 
145874aeea5SJeff Kirsher /* Initial interrupt moderation settings.  They can be modified after
146874aeea5SJeff Kirsher  * module load with ethtool.
147874aeea5SJeff Kirsher  *
148874aeea5SJeff Kirsher  * This default is chosen to ensure that a 10G link does not go idle
149874aeea5SJeff Kirsher  * while a TX queue is stopped after it has become full.  A queue is
150874aeea5SJeff Kirsher  * restarted when it drops below half full.  The time this takes (assuming
151874aeea5SJeff Kirsher  * worst case 3 descriptors per packet and 1024 descriptors) is
152874aeea5SJeff Kirsher  *   512 / 3 * 1.2 = 205 usec.
153874aeea5SJeff Kirsher  */
154874aeea5SJeff Kirsher static unsigned int tx_irq_mod_usec = 150;
155874aeea5SJeff Kirsher 
156874aeea5SJeff Kirsher /* This is the first interrupt mode to try out of:
157874aeea5SJeff Kirsher  * 0 => MSI-X
158874aeea5SJeff Kirsher  * 1 => MSI
159874aeea5SJeff Kirsher  * 2 => legacy
160874aeea5SJeff Kirsher  */
161874aeea5SJeff Kirsher static unsigned int interrupt_mode;
162874aeea5SJeff Kirsher 
163874aeea5SJeff Kirsher /* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
164874aeea5SJeff Kirsher  * i.e. the number of CPUs among which we may distribute simultaneous
165874aeea5SJeff Kirsher  * interrupt handling.
166874aeea5SJeff Kirsher  *
167874aeea5SJeff Kirsher  * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
168cdb08f8fSBen Hutchings  * The default (0) means to assign an interrupt to each core.
169874aeea5SJeff Kirsher  */
170874aeea5SJeff Kirsher static unsigned int rss_cpus;
171874aeea5SJeff Kirsher module_param(rss_cpus, uint, 0444);
172874aeea5SJeff Kirsher MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
173874aeea5SJeff Kirsher 
174b9cc977dSBen Hutchings static bool phy_flash_cfg;
175b9cc977dSBen Hutchings module_param(phy_flash_cfg, bool, 0644);
176874aeea5SJeff Kirsher MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
177874aeea5SJeff Kirsher 
178e7bed9c8SBen Hutchings static unsigned irq_adapt_low_thresh = 8000;
179874aeea5SJeff Kirsher module_param(irq_adapt_low_thresh, uint, 0644);
180874aeea5SJeff Kirsher MODULE_PARM_DESC(irq_adapt_low_thresh,
181874aeea5SJeff Kirsher 		 "Threshold score for reducing IRQ moderation");
182874aeea5SJeff Kirsher 
183e7bed9c8SBen Hutchings static unsigned irq_adapt_high_thresh = 16000;
184874aeea5SJeff Kirsher module_param(irq_adapt_high_thresh, uint, 0644);
185874aeea5SJeff Kirsher MODULE_PARM_DESC(irq_adapt_high_thresh,
186874aeea5SJeff Kirsher 		 "Threshold score for increasing IRQ moderation");
187874aeea5SJeff Kirsher 
188874aeea5SJeff Kirsher static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
189874aeea5SJeff Kirsher 			 NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
190874aeea5SJeff Kirsher 			 NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
191874aeea5SJeff Kirsher 			 NETIF_MSG_TX_ERR | NETIF_MSG_HW);
192874aeea5SJeff Kirsher module_param(debug, uint, 0);
193874aeea5SJeff Kirsher MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
194874aeea5SJeff Kirsher 
195874aeea5SJeff Kirsher /**************************************************************************
196874aeea5SJeff Kirsher  *
197874aeea5SJeff Kirsher  * Utility functions and prototypes
198874aeea5SJeff Kirsher  *
199874aeea5SJeff Kirsher  *************************************************************************/
200874aeea5SJeff Kirsher 
201261e4d96SJon Cooper static int efx_soft_enable_interrupts(struct efx_nic *efx);
202d8291187SBen Hutchings static void efx_soft_disable_interrupts(struct efx_nic *efx);
2037f967c01SBen Hutchings static void efx_remove_channel(struct efx_channel *channel);
204874aeea5SJeff Kirsher static void efx_remove_channels(struct efx_nic *efx);
2057f967c01SBen Hutchings static const struct efx_channel_type efx_default_channel_type;
206874aeea5SJeff Kirsher static void efx_remove_port(struct efx_nic *efx);
2077f967c01SBen Hutchings static void efx_init_napi_channel(struct efx_channel *channel);
208874aeea5SJeff Kirsher static void efx_fini_napi(struct efx_nic *efx);
209874aeea5SJeff Kirsher static void efx_fini_napi_channel(struct efx_channel *channel);
210874aeea5SJeff Kirsher static void efx_fini_struct(struct efx_nic *efx);
211874aeea5SJeff Kirsher static void efx_start_all(struct efx_nic *efx);
212874aeea5SJeff Kirsher static void efx_stop_all(struct efx_nic *efx);
213874aeea5SJeff Kirsher 
214874aeea5SJeff Kirsher #define EFX_ASSERT_RESET_SERIALISED(efx)		\
215874aeea5SJeff Kirsher 	do {						\
216f16aeea0SBen Hutchings 		if ((efx->state == STATE_READY) ||	\
217626950dbSAlexandre Rames 		    (efx->state == STATE_RECOVERY) ||	\
218874aeea5SJeff Kirsher 		    (efx->state == STATE_DISABLED))	\
219874aeea5SJeff Kirsher 			ASSERT_RTNL();			\
220874aeea5SJeff Kirsher 	} while (0)
221874aeea5SJeff Kirsher 
2228b7325b4SBen Hutchings static int efx_check_disabled(struct efx_nic *efx)
2238b7325b4SBen Hutchings {
224626950dbSAlexandre Rames 	if (efx->state == STATE_DISABLED || efx->state == STATE_RECOVERY) {
2258b7325b4SBen Hutchings 		netif_err(efx, drv, efx->net_dev,
2268b7325b4SBen Hutchings 			  "device is disabled due to earlier errors\n");
2278b7325b4SBen Hutchings 		return -EIO;
2288b7325b4SBen Hutchings 	}
2298b7325b4SBen Hutchings 	return 0;
2308b7325b4SBen Hutchings }
2318b7325b4SBen Hutchings 
232874aeea5SJeff Kirsher /**************************************************************************
233874aeea5SJeff Kirsher  *
234874aeea5SJeff Kirsher  * Event queue processing
235874aeea5SJeff Kirsher  *
236874aeea5SJeff Kirsher  *************************************************************************/
237874aeea5SJeff Kirsher 
238874aeea5SJeff Kirsher /* Process channel's event queue
239874aeea5SJeff Kirsher  *
240874aeea5SJeff Kirsher  * This function is responsible for processing the event queue of a
241874aeea5SJeff Kirsher  * single channel.  The caller must guarantee that this function will
242874aeea5SJeff Kirsher  * never be concurrently called more than once on the same channel,
243874aeea5SJeff Kirsher  * though different channels may be being processed concurrently.
244874aeea5SJeff Kirsher  */
245874aeea5SJeff Kirsher static int efx_process_channel(struct efx_channel *channel, int budget)
246874aeea5SJeff Kirsher {
247c936835cSPeter Dunning 	struct efx_tx_queue *tx_queue;
248874aeea5SJeff Kirsher 	int spent;
249874aeea5SJeff Kirsher 
2509f2cb71cSBen Hutchings 	if (unlikely(!channel->enabled))
251874aeea5SJeff Kirsher 		return 0;
252874aeea5SJeff Kirsher 
253c936835cSPeter Dunning 	efx_for_each_channel_tx_queue(tx_queue, channel) {
254c936835cSPeter Dunning 		tx_queue->pkts_compl = 0;
255c936835cSPeter Dunning 		tx_queue->bytes_compl = 0;
256c936835cSPeter Dunning 	}
257c936835cSPeter Dunning 
258874aeea5SJeff Kirsher 	spent = efx_nic_process_eventq(channel, budget);
259d9ab7007SBen Hutchings 	if (spent && efx_channel_has_rx_queue(channel)) {
260d9ab7007SBen Hutchings 		struct efx_rx_queue *rx_queue =
261d9ab7007SBen Hutchings 			efx_channel_get_rx_queue(channel);
262874aeea5SJeff Kirsher 
263ff734ef4SBen Hutchings 		efx_rx_flush_packet(channel);
264cce28794SJon Cooper 		efx_fast_push_rx_descriptors(rx_queue, true);
265d9ab7007SBen Hutchings 	}
266874aeea5SJeff Kirsher 
267c936835cSPeter Dunning 	/* Update BQL */
268c936835cSPeter Dunning 	efx_for_each_channel_tx_queue(tx_queue, channel) {
269c936835cSPeter Dunning 		if (tx_queue->bytes_compl) {
270c936835cSPeter Dunning 			netdev_tx_completed_queue(tx_queue->core_txq,
271c936835cSPeter Dunning 				tx_queue->pkts_compl, tx_queue->bytes_compl);
272c936835cSPeter Dunning 		}
273c936835cSPeter Dunning 	}
274c936835cSPeter Dunning 
275874aeea5SJeff Kirsher 	return spent;
276874aeea5SJeff Kirsher }
277874aeea5SJeff Kirsher 
278874aeea5SJeff Kirsher /* NAPI poll handler
279874aeea5SJeff Kirsher  *
280874aeea5SJeff Kirsher  * NAPI guarantees serialisation of polls of the same device, which
281874aeea5SJeff Kirsher  * provides the guarantee required by efx_process_channel().
282874aeea5SJeff Kirsher  */
283539de7c5SBert Kenward static void efx_update_irq_mod(struct efx_nic *efx, struct efx_channel *channel)
284539de7c5SBert Kenward {
285539de7c5SBert Kenward 	int step = efx->irq_mod_step_us;
286539de7c5SBert Kenward 
287539de7c5SBert Kenward 	if (channel->irq_mod_score < irq_adapt_low_thresh) {
288539de7c5SBert Kenward 		if (channel->irq_moderation_us > step) {
289539de7c5SBert Kenward 			channel->irq_moderation_us -= step;
290539de7c5SBert Kenward 			efx->type->push_irq_moderation(channel);
291539de7c5SBert Kenward 		}
292539de7c5SBert Kenward 	} else if (channel->irq_mod_score > irq_adapt_high_thresh) {
293539de7c5SBert Kenward 		if (channel->irq_moderation_us <
294539de7c5SBert Kenward 		    efx->irq_rx_moderation_us) {
295539de7c5SBert Kenward 			channel->irq_moderation_us += step;
296539de7c5SBert Kenward 			efx->type->push_irq_moderation(channel);
297539de7c5SBert Kenward 		}
298539de7c5SBert Kenward 	}
299539de7c5SBert Kenward 
300539de7c5SBert Kenward 	channel->irq_count = 0;
301539de7c5SBert Kenward 	channel->irq_mod_score = 0;
302539de7c5SBert Kenward }
303539de7c5SBert Kenward 
304874aeea5SJeff Kirsher static int efx_poll(struct napi_struct *napi, int budget)
305874aeea5SJeff Kirsher {
306874aeea5SJeff Kirsher 	struct efx_channel *channel =
307874aeea5SJeff Kirsher 		container_of(napi, struct efx_channel, napi_str);
308874aeea5SJeff Kirsher 	struct efx_nic *efx = channel->efx;
309874aeea5SJeff Kirsher 	int spent;
310874aeea5SJeff Kirsher 
311874aeea5SJeff Kirsher 	netif_vdbg(efx, intr, efx->net_dev,
312874aeea5SJeff Kirsher 		   "channel %d NAPI poll executing on CPU %d\n",
313874aeea5SJeff Kirsher 		   channel->channel, raw_smp_processor_id());
314874aeea5SJeff Kirsher 
315874aeea5SJeff Kirsher 	spent = efx_process_channel(channel, budget);
316874aeea5SJeff Kirsher 
317874aeea5SJeff Kirsher 	if (spent < budget) {
3189d9a6973SBen Hutchings 		if (efx_channel_has_rx_queue(channel) &&
319874aeea5SJeff Kirsher 		    efx->irq_rx_adaptive &&
320874aeea5SJeff Kirsher 		    unlikely(++channel->irq_count == 1000)) {
321539de7c5SBert Kenward 			efx_update_irq_mod(efx, channel);
322874aeea5SJeff Kirsher 		}
323874aeea5SJeff Kirsher 
324874aeea5SJeff Kirsher 		efx_filter_rfs_expire(channel);
325874aeea5SJeff Kirsher 
326874aeea5SJeff Kirsher 		/* There is no race here; although napi_disable() will
327874aeea5SJeff Kirsher 		 * only wait for napi_complete(), this isn't a problem
328514bedbcSBen Hutchings 		 * since efx_nic_eventq_read_ack() will have no effect if
329874aeea5SJeff Kirsher 		 * interrupts have already been disabled.
330874aeea5SJeff Kirsher 		 */
331f820c0acSBert Kenward 		if (napi_complete_done(napi, spent))
332514bedbcSBen Hutchings 			efx_nic_eventq_read_ack(channel);
333874aeea5SJeff Kirsher 	}
334874aeea5SJeff Kirsher 
335874aeea5SJeff Kirsher 	return spent;
336874aeea5SJeff Kirsher }
337874aeea5SJeff Kirsher 
338874aeea5SJeff Kirsher /* Create event queue
339874aeea5SJeff Kirsher  * Event queue memory allocations are done only once.  If the channel
340874aeea5SJeff Kirsher  * is reset, the memory buffer will be reused; this guards against
341874aeea5SJeff Kirsher  * errors during channel reset and also simplifies interrupt handling.
342874aeea5SJeff Kirsher  */
343874aeea5SJeff Kirsher static int efx_probe_eventq(struct efx_channel *channel)
344874aeea5SJeff Kirsher {
345874aeea5SJeff Kirsher 	struct efx_nic *efx = channel->efx;
346874aeea5SJeff Kirsher 	unsigned long entries;
347874aeea5SJeff Kirsher 
34886ee5302SBen Hutchings 	netif_dbg(efx, probe, efx->net_dev,
349874aeea5SJeff Kirsher 		  "chan %d create event queue\n", channel->channel);
350874aeea5SJeff Kirsher 
351874aeea5SJeff Kirsher 	/* Build an event queue with room for one event per tx and rx buffer,
352874aeea5SJeff Kirsher 	 * plus some extra for link state events and MCDI completions. */
353874aeea5SJeff Kirsher 	entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
354e01b16a7SEdward Cree 	EFX_WARN_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
355874aeea5SJeff Kirsher 	channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
356874aeea5SJeff Kirsher 
357874aeea5SJeff Kirsher 	return efx_nic_probe_eventq(channel);
358874aeea5SJeff Kirsher }
359874aeea5SJeff Kirsher 
360874aeea5SJeff Kirsher /* Prepare channel's event queue */
361261e4d96SJon Cooper static int efx_init_eventq(struct efx_channel *channel)
362874aeea5SJeff Kirsher {
36315acb1ceSBen Hutchings 	struct efx_nic *efx = channel->efx;
364261e4d96SJon Cooper 	int rc;
365261e4d96SJon Cooper 
366261e4d96SJon Cooper 	EFX_WARN_ON_PARANOID(channel->eventq_init);
367261e4d96SJon Cooper 
36815acb1ceSBen Hutchings 	netif_dbg(efx, drv, efx->net_dev,
369874aeea5SJeff Kirsher 		  "chan %d init event queue\n", channel->channel);
370874aeea5SJeff Kirsher 
371261e4d96SJon Cooper 	rc = efx_nic_init_eventq(channel);
372261e4d96SJon Cooper 	if (rc == 0) {
37315acb1ceSBen Hutchings 		efx->type->push_irq_moderation(channel);
374874aeea5SJeff Kirsher 		channel->eventq_read_ptr = 0;
375be3fc09cSBen Hutchings 		channel->eventq_init = true;
376874aeea5SJeff Kirsher 	}
377261e4d96SJon Cooper 	return rc;
378261e4d96SJon Cooper }
379874aeea5SJeff Kirsher 
3809f2cb71cSBen Hutchings /* Enable event queue processing and NAPI */
38136763266SAlexandre Rames void efx_start_eventq(struct efx_channel *channel)
3829f2cb71cSBen Hutchings {
3839f2cb71cSBen Hutchings 	netif_dbg(channel->efx, ifup, channel->efx->net_dev,
3849f2cb71cSBen Hutchings 		  "chan %d start event queue\n", channel->channel);
3859f2cb71cSBen Hutchings 
386514bedbcSBen Hutchings 	/* Make sure the NAPI handler sees the enabled flag set */
3879f2cb71cSBen Hutchings 	channel->enabled = true;
3889f2cb71cSBen Hutchings 	smp_wmb();
3899f2cb71cSBen Hutchings 
3909f2cb71cSBen Hutchings 	napi_enable(&channel->napi_str);
3919f2cb71cSBen Hutchings 	efx_nic_eventq_read_ack(channel);
3929f2cb71cSBen Hutchings }
3939f2cb71cSBen Hutchings 
3949f2cb71cSBen Hutchings /* Disable event queue processing and NAPI */
39536763266SAlexandre Rames void efx_stop_eventq(struct efx_channel *channel)
3969f2cb71cSBen Hutchings {
3979f2cb71cSBen Hutchings 	if (!channel->enabled)
3989f2cb71cSBen Hutchings 		return;
3999f2cb71cSBen Hutchings 
4009f2cb71cSBen Hutchings 	napi_disable(&channel->napi_str);
4019f2cb71cSBen Hutchings 	channel->enabled = false;
4029f2cb71cSBen Hutchings }
4039f2cb71cSBen Hutchings 
404874aeea5SJeff Kirsher static void efx_fini_eventq(struct efx_channel *channel)
405874aeea5SJeff Kirsher {
406be3fc09cSBen Hutchings 	if (!channel->eventq_init)
407be3fc09cSBen Hutchings 		return;
408be3fc09cSBen Hutchings 
409874aeea5SJeff Kirsher 	netif_dbg(channel->efx, drv, channel->efx->net_dev,
410874aeea5SJeff Kirsher 		  "chan %d fini event queue\n", channel->channel);
411874aeea5SJeff Kirsher 
412874aeea5SJeff Kirsher 	efx_nic_fini_eventq(channel);
413be3fc09cSBen Hutchings 	channel->eventq_init = false;
414874aeea5SJeff Kirsher }
415874aeea5SJeff Kirsher 
416874aeea5SJeff Kirsher static void efx_remove_eventq(struct efx_channel *channel)
417874aeea5SJeff Kirsher {
418874aeea5SJeff Kirsher 	netif_dbg(channel->efx, drv, channel->efx->net_dev,
419874aeea5SJeff Kirsher 		  "chan %d remove event queue\n", channel->channel);
420874aeea5SJeff Kirsher 
421874aeea5SJeff Kirsher 	efx_nic_remove_eventq(channel);
422874aeea5SJeff Kirsher }
423874aeea5SJeff Kirsher 
424874aeea5SJeff Kirsher /**************************************************************************
425874aeea5SJeff Kirsher  *
426874aeea5SJeff Kirsher  * Channel handling
427874aeea5SJeff Kirsher  *
428874aeea5SJeff Kirsher  *************************************************************************/
429874aeea5SJeff Kirsher 
4307f967c01SBen Hutchings /* Allocate and initialise a channel structure. */
431874aeea5SJeff Kirsher static struct efx_channel *
432874aeea5SJeff Kirsher efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
433874aeea5SJeff Kirsher {
434874aeea5SJeff Kirsher 	struct efx_channel *channel;
435874aeea5SJeff Kirsher 	struct efx_rx_queue *rx_queue;
436874aeea5SJeff Kirsher 	struct efx_tx_queue *tx_queue;
437874aeea5SJeff Kirsher 	int j;
438874aeea5SJeff Kirsher 
4397f967c01SBen Hutchings 	channel = kzalloc(sizeof(*channel), GFP_KERNEL);
4407f967c01SBen Hutchings 	if (!channel)
4417f967c01SBen Hutchings 		return NULL;
4427f967c01SBen Hutchings 
4437f967c01SBen Hutchings 	channel->efx = efx;
4447f967c01SBen Hutchings 	channel->channel = i;
4457f967c01SBen Hutchings 	channel->type = &efx_default_channel_type;
4467f967c01SBen Hutchings 
4477f967c01SBen Hutchings 	for (j = 0; j < EFX_TXQ_TYPES; j++) {
4487f967c01SBen Hutchings 		tx_queue = &channel->tx_queue[j];
4497f967c01SBen Hutchings 		tx_queue->efx = efx;
4507f967c01SBen Hutchings 		tx_queue->queue = i * EFX_TXQ_TYPES + j;
4517f967c01SBen Hutchings 		tx_queue->channel = channel;
4527f967c01SBen Hutchings 	}
4537f967c01SBen Hutchings 
4547f967c01SBen Hutchings 	rx_queue = &channel->rx_queue;
4557f967c01SBen Hutchings 	rx_queue->efx = efx;
4567f967c01SBen Hutchings 	setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
4577f967c01SBen Hutchings 		    (unsigned long)rx_queue);
4587f967c01SBen Hutchings 
4597f967c01SBen Hutchings 	return channel;
4607f967c01SBen Hutchings }
4617f967c01SBen Hutchings 
4627f967c01SBen Hutchings /* Allocate and initialise a channel structure, copying parameters
4637f967c01SBen Hutchings  * (but not resources) from an old channel structure.
4647f967c01SBen Hutchings  */
4657f967c01SBen Hutchings static struct efx_channel *
4667f967c01SBen Hutchings efx_copy_channel(const struct efx_channel *old_channel)
4677f967c01SBen Hutchings {
4687f967c01SBen Hutchings 	struct efx_channel *channel;
4697f967c01SBen Hutchings 	struct efx_rx_queue *rx_queue;
4707f967c01SBen Hutchings 	struct efx_tx_queue *tx_queue;
4717f967c01SBen Hutchings 	int j;
4727f967c01SBen Hutchings 
473874aeea5SJeff Kirsher 	channel = kmalloc(sizeof(*channel), GFP_KERNEL);
474874aeea5SJeff Kirsher 	if (!channel)
475874aeea5SJeff Kirsher 		return NULL;
476874aeea5SJeff Kirsher 
477874aeea5SJeff Kirsher 	*channel = *old_channel;
478874aeea5SJeff Kirsher 
479874aeea5SJeff Kirsher 	channel->napi_dev = NULL;
48046d054f8SBert Kenward 	INIT_HLIST_NODE(&channel->napi_str.napi_hash_node);
48146d054f8SBert Kenward 	channel->napi_str.napi_id = 0;
48246d054f8SBert Kenward 	channel->napi_str.state = 0;
483874aeea5SJeff Kirsher 	memset(&channel->eventq, 0, sizeof(channel->eventq));
484874aeea5SJeff Kirsher 
485874aeea5SJeff Kirsher 	for (j = 0; j < EFX_TXQ_TYPES; j++) {
486874aeea5SJeff Kirsher 		tx_queue = &channel->tx_queue[j];
487874aeea5SJeff Kirsher 		if (tx_queue->channel)
488874aeea5SJeff Kirsher 			tx_queue->channel = channel;
489874aeea5SJeff Kirsher 		tx_queue->buffer = NULL;
490874aeea5SJeff Kirsher 		memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
491874aeea5SJeff Kirsher 	}
492874aeea5SJeff Kirsher 
493874aeea5SJeff Kirsher 	rx_queue = &channel->rx_queue;
4947f967c01SBen Hutchings 	rx_queue->buffer = NULL;
4957f967c01SBen Hutchings 	memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
496874aeea5SJeff Kirsher 	setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
497874aeea5SJeff Kirsher 		    (unsigned long)rx_queue);
498874aeea5SJeff Kirsher 
499874aeea5SJeff Kirsher 	return channel;
500874aeea5SJeff Kirsher }
501874aeea5SJeff Kirsher 
502874aeea5SJeff Kirsher static int efx_probe_channel(struct efx_channel *channel)
503874aeea5SJeff Kirsher {
504874aeea5SJeff Kirsher 	struct efx_tx_queue *tx_queue;
505874aeea5SJeff Kirsher 	struct efx_rx_queue *rx_queue;
506874aeea5SJeff Kirsher 	int rc;
507874aeea5SJeff Kirsher 
508874aeea5SJeff Kirsher 	netif_dbg(channel->efx, probe, channel->efx->net_dev,
509874aeea5SJeff Kirsher 		  "creating channel %d\n", channel->channel);
510874aeea5SJeff Kirsher 
5117f967c01SBen Hutchings 	rc = channel->type->pre_probe(channel);
5127f967c01SBen Hutchings 	if (rc)
5137f967c01SBen Hutchings 		goto fail;
5147f967c01SBen Hutchings 
515874aeea5SJeff Kirsher 	rc = efx_probe_eventq(channel);
516874aeea5SJeff Kirsher 	if (rc)
5177f967c01SBen Hutchings 		goto fail;
518874aeea5SJeff Kirsher 
519874aeea5SJeff Kirsher 	efx_for_each_channel_tx_queue(tx_queue, channel) {
520874aeea5SJeff Kirsher 		rc = efx_probe_tx_queue(tx_queue);
521874aeea5SJeff Kirsher 		if (rc)
5227f967c01SBen Hutchings 			goto fail;
523874aeea5SJeff Kirsher 	}
524874aeea5SJeff Kirsher 
525874aeea5SJeff Kirsher 	efx_for_each_channel_rx_queue(rx_queue, channel) {
526874aeea5SJeff Kirsher 		rc = efx_probe_rx_queue(rx_queue);
527874aeea5SJeff Kirsher 		if (rc)
5287f967c01SBen Hutchings 			goto fail;
529874aeea5SJeff Kirsher 	}
530874aeea5SJeff Kirsher 
531874aeea5SJeff Kirsher 	return 0;
532874aeea5SJeff Kirsher 
5337f967c01SBen Hutchings fail:
5347f967c01SBen Hutchings 	efx_remove_channel(channel);
535874aeea5SJeff Kirsher 	return rc;
536874aeea5SJeff Kirsher }
537874aeea5SJeff Kirsher 
5387f967c01SBen Hutchings static void
5397f967c01SBen Hutchings efx_get_channel_name(struct efx_channel *channel, char *buf, size_t len)
5407f967c01SBen Hutchings {
5417f967c01SBen Hutchings 	struct efx_nic *efx = channel->efx;
5427f967c01SBen Hutchings 	const char *type;
5437f967c01SBen Hutchings 	int number;
5447f967c01SBen Hutchings 
5457f967c01SBen Hutchings 	number = channel->channel;
5467f967c01SBen Hutchings 	if (efx->tx_channel_offset == 0) {
5477f967c01SBen Hutchings 		type = "";
5487f967c01SBen Hutchings 	} else if (channel->channel < efx->tx_channel_offset) {
5497f967c01SBen Hutchings 		type = "-rx";
5507f967c01SBen Hutchings 	} else {
5517f967c01SBen Hutchings 		type = "-tx";
5527f967c01SBen Hutchings 		number -= efx->tx_channel_offset;
5537f967c01SBen Hutchings 	}
5547f967c01SBen Hutchings 	snprintf(buf, len, "%s%s-%d", efx->name, type, number);
5557f967c01SBen Hutchings }
556874aeea5SJeff Kirsher 
557874aeea5SJeff Kirsher static void efx_set_channel_names(struct efx_nic *efx)
558874aeea5SJeff Kirsher {
559874aeea5SJeff Kirsher 	struct efx_channel *channel;
560874aeea5SJeff Kirsher 
5617f967c01SBen Hutchings 	efx_for_each_channel(channel, efx)
5627f967c01SBen Hutchings 		channel->type->get_name(channel,
563d8291187SBen Hutchings 					efx->msi_context[channel->channel].name,
564d8291187SBen Hutchings 					sizeof(efx->msi_context[0].name));
565874aeea5SJeff Kirsher }
566874aeea5SJeff Kirsher 
567874aeea5SJeff Kirsher static int efx_probe_channels(struct efx_nic *efx)
568874aeea5SJeff Kirsher {
569874aeea5SJeff Kirsher 	struct efx_channel *channel;
570874aeea5SJeff Kirsher 	int rc;
571874aeea5SJeff Kirsher 
572874aeea5SJeff Kirsher 	/* Restart special buffer allocation */
573874aeea5SJeff Kirsher 	efx->next_buffer_table = 0;
574874aeea5SJeff Kirsher 
575c92aaff1SBen Hutchings 	/* Probe channels in reverse, so that any 'extra' channels
576c92aaff1SBen Hutchings 	 * use the start of the buffer table. This allows the traffic
577c92aaff1SBen Hutchings 	 * channels to be resized without moving them or wasting the
578c92aaff1SBen Hutchings 	 * entries before them.
579c92aaff1SBen Hutchings 	 */
580c92aaff1SBen Hutchings 	efx_for_each_channel_rev(channel, efx) {
581874aeea5SJeff Kirsher 		rc = efx_probe_channel(channel);
582874aeea5SJeff Kirsher 		if (rc) {
583874aeea5SJeff Kirsher 			netif_err(efx, probe, efx->net_dev,
584874aeea5SJeff Kirsher 				  "failed to create channel %d\n",
585874aeea5SJeff Kirsher 				  channel->channel);
586874aeea5SJeff Kirsher 			goto fail;
587874aeea5SJeff Kirsher 		}
588874aeea5SJeff Kirsher 	}
589874aeea5SJeff Kirsher 	efx_set_channel_names(efx);
590874aeea5SJeff Kirsher 
591874aeea5SJeff Kirsher 	return 0;
592874aeea5SJeff Kirsher 
593874aeea5SJeff Kirsher fail:
594874aeea5SJeff Kirsher 	efx_remove_channels(efx);
595874aeea5SJeff Kirsher 	return rc;
596874aeea5SJeff Kirsher }
597874aeea5SJeff Kirsher 
598874aeea5SJeff Kirsher /* Channels are shutdown and reinitialised whilst the NIC is running
599874aeea5SJeff Kirsher  * to propagate configuration changes (mtu, checksum offload), or
600874aeea5SJeff Kirsher  * to clear hardware error conditions
601874aeea5SJeff Kirsher  */
6029f2cb71cSBen Hutchings static void efx_start_datapath(struct efx_nic *efx)
603874aeea5SJeff Kirsher {
604ebfcd0fdSAndrew Rybchenko 	netdev_features_t old_features = efx->net_dev->features;
60585740cdfSBen Hutchings 	bool old_rx_scatter = efx->rx_scatter;
606874aeea5SJeff Kirsher 	struct efx_tx_queue *tx_queue;
607874aeea5SJeff Kirsher 	struct efx_rx_queue *rx_queue;
608874aeea5SJeff Kirsher 	struct efx_channel *channel;
60985740cdfSBen Hutchings 	size_t rx_buf_len;
610874aeea5SJeff Kirsher 
611874aeea5SJeff Kirsher 	/* Calculate the rx buffer allocation parameters required to
612874aeea5SJeff Kirsher 	 * support the current MTU, including padding for header
613874aeea5SJeff Kirsher 	 * alignment and overruns.
614874aeea5SJeff Kirsher 	 */
61543a3739dSJon Cooper 	efx->rx_dma_len = (efx->rx_prefix_size +
616874aeea5SJeff Kirsher 			   EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
617874aeea5SJeff Kirsher 			   efx->type->rx_buffer_padding);
61885740cdfSBen Hutchings 	rx_buf_len = (sizeof(struct efx_rx_page_state) +
6192ec03014SAndrew Rybchenko 		      efx->rx_ip_align + efx->rx_dma_len);
62085740cdfSBen Hutchings 	if (rx_buf_len <= PAGE_SIZE) {
621e8c68c0aSJon Cooper 		efx->rx_scatter = efx->type->always_rx_scatter;
62285740cdfSBen Hutchings 		efx->rx_buffer_order = 0;
62385740cdfSBen Hutchings 	} else if (efx->type->can_rx_scatter) {
624950c54dfSBen Hutchings 		BUILD_BUG_ON(EFX_RX_USR_BUF_SIZE % L1_CACHE_BYTES);
62585740cdfSBen Hutchings 		BUILD_BUG_ON(sizeof(struct efx_rx_page_state) +
626950c54dfSBen Hutchings 			     2 * ALIGN(NET_IP_ALIGN + EFX_RX_USR_BUF_SIZE,
627950c54dfSBen Hutchings 				       EFX_RX_BUF_ALIGNMENT) >
628950c54dfSBen Hutchings 			     PAGE_SIZE);
62985740cdfSBen Hutchings 		efx->rx_scatter = true;
63085740cdfSBen Hutchings 		efx->rx_dma_len = EFX_RX_USR_BUF_SIZE;
63185740cdfSBen Hutchings 		efx->rx_buffer_order = 0;
63285740cdfSBen Hutchings 	} else {
63385740cdfSBen Hutchings 		efx->rx_scatter = false;
63485740cdfSBen Hutchings 		efx->rx_buffer_order = get_order(rx_buf_len);
63585740cdfSBen Hutchings 	}
63685740cdfSBen Hutchings 
6371648a23fSDaniel Pieczko 	efx_rx_config_page_split(efx);
6381648a23fSDaniel Pieczko 	if (efx->rx_buffer_order)
6391648a23fSDaniel Pieczko 		netif_dbg(efx, drv, efx->net_dev,
6401648a23fSDaniel Pieczko 			  "RX buf len=%u; page order=%u batch=%u\n",
6411648a23fSDaniel Pieczko 			  efx->rx_dma_len, efx->rx_buffer_order,
6421648a23fSDaniel Pieczko 			  efx->rx_pages_per_batch);
6431648a23fSDaniel Pieczko 	else
6441648a23fSDaniel Pieczko 		netif_dbg(efx, drv, efx->net_dev,
6451648a23fSDaniel Pieczko 			  "RX buf len=%u step=%u bpp=%u; page batch=%u\n",
6461648a23fSDaniel Pieczko 			  efx->rx_dma_len, efx->rx_page_buf_step,
6471648a23fSDaniel Pieczko 			  efx->rx_bufs_per_page, efx->rx_pages_per_batch);
6482768935aSDaniel Pieczko 
649ebfcd0fdSAndrew Rybchenko 	/* Restore previously fixed features in hw_features and remove
650ebfcd0fdSAndrew Rybchenko 	 * features which are fixed now
651ebfcd0fdSAndrew Rybchenko 	 */
652ebfcd0fdSAndrew Rybchenko 	efx->net_dev->hw_features |= efx->net_dev->features;
653ebfcd0fdSAndrew Rybchenko 	efx->net_dev->hw_features &= ~efx->fixed_features;
654ebfcd0fdSAndrew Rybchenko 	efx->net_dev->features |= efx->fixed_features;
655ebfcd0fdSAndrew Rybchenko 	if (efx->net_dev->features != old_features)
656ebfcd0fdSAndrew Rybchenko 		netdev_features_change(efx->net_dev);
657ebfcd0fdSAndrew Rybchenko 
658e8c68c0aSJon Cooper 	/* RX filters may also have scatter-enabled flags */
65985740cdfSBen Hutchings 	if (efx->rx_scatter != old_rx_scatter)
660add72477SBen Hutchings 		efx->type->filter_update_rx_scatter(efx);
661874aeea5SJeff Kirsher 
66214bf718fSBen Hutchings 	/* We must keep at least one descriptor in a TX ring empty.
66314bf718fSBen Hutchings 	 * We could avoid this when the queue size does not exactly
66414bf718fSBen Hutchings 	 * match the hardware ring size, but it's not that important.
66514bf718fSBen Hutchings 	 * Therefore we stop the queue when one more skb might fill
66614bf718fSBen Hutchings 	 * the ring completely.  We wake it when half way back to
66714bf718fSBen Hutchings 	 * empty.
66814bf718fSBen Hutchings 	 */
66914bf718fSBen Hutchings 	efx->txq_stop_thresh = efx->txq_entries - efx_tx_max_skb_descs(efx);
67014bf718fSBen Hutchings 	efx->txq_wake_thresh = efx->txq_stop_thresh / 2;
67114bf718fSBen Hutchings 
672874aeea5SJeff Kirsher 	/* Initialise the channels */
673874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx) {
6743881d8abSAlexandre Rames 		efx_for_each_channel_tx_queue(tx_queue, channel) {
675874aeea5SJeff Kirsher 			efx_init_tx_queue(tx_queue);
6763881d8abSAlexandre Rames 			atomic_inc(&efx->active_queues);
6773881d8abSAlexandre Rames 		}
678874aeea5SJeff Kirsher 
6799f2cb71cSBen Hutchings 		efx_for_each_channel_rx_queue(rx_queue, channel) {
680874aeea5SJeff Kirsher 			efx_init_rx_queue(rx_queue);
6813881d8abSAlexandre Rames 			atomic_inc(&efx->active_queues);
682cce28794SJon Cooper 			efx_stop_eventq(channel);
683cce28794SJon Cooper 			efx_fast_push_rx_descriptors(rx_queue, false);
684cce28794SJon Cooper 			efx_start_eventq(channel);
6859f2cb71cSBen Hutchings 		}
686874aeea5SJeff Kirsher 
68785740cdfSBen Hutchings 		WARN_ON(channel->rx_pkt_n_frags);
688874aeea5SJeff Kirsher 	}
6899f2cb71cSBen Hutchings 
6902ea4dc28SAlexandre Rames 	efx_ptp_start_datapath(efx);
6912ea4dc28SAlexandre Rames 
6929f2cb71cSBen Hutchings 	if (netif_device_present(efx->net_dev))
6939f2cb71cSBen Hutchings 		netif_tx_wake_all_queues(efx->net_dev);
694874aeea5SJeff Kirsher }
695874aeea5SJeff Kirsher 
6969f2cb71cSBen Hutchings static void efx_stop_datapath(struct efx_nic *efx)
697874aeea5SJeff Kirsher {
698874aeea5SJeff Kirsher 	struct efx_channel *channel;
699874aeea5SJeff Kirsher 	struct efx_tx_queue *tx_queue;
700874aeea5SJeff Kirsher 	struct efx_rx_queue *rx_queue;
701874aeea5SJeff Kirsher 	int rc;
702874aeea5SJeff Kirsher 
703874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
704874aeea5SJeff Kirsher 	BUG_ON(efx->port_enabled);
705874aeea5SJeff Kirsher 
7062ea4dc28SAlexandre Rames 	efx_ptp_stop_datapath(efx);
7072ea4dc28SAlexandre Rames 
708d8aec745SBen Hutchings 	/* Stop RX refill */
709d8aec745SBen Hutchings 	efx_for_each_channel(channel, efx) {
710d8aec745SBen Hutchings 		efx_for_each_channel_rx_queue(rx_queue, channel)
711d8aec745SBen Hutchings 			rx_queue->refill_enabled = false;
712d8aec745SBen Hutchings 	}
713d8aec745SBen Hutchings 
714874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx) {
7159f2cb71cSBen Hutchings 		/* RX packet processing is pipelined, so wait for the
7169f2cb71cSBen Hutchings 		 * NAPI handler to complete.  At least event queue 0
7179f2cb71cSBen Hutchings 		 * might be kept active by non-data events, so don't
7189f2cb71cSBen Hutchings 		 * use napi_synchronize() but actually disable NAPI
7199f2cb71cSBen Hutchings 		 * temporarily.
7209f2cb71cSBen Hutchings 		 */
7219f2cb71cSBen Hutchings 		if (efx_channel_has_rx_queue(channel)) {
7229f2cb71cSBen Hutchings 			efx_stop_eventq(channel);
7239f2cb71cSBen Hutchings 			efx_start_eventq(channel);
7249f2cb71cSBen Hutchings 		}
725e42c3d85SBen Hutchings 	}
726874aeea5SJeff Kirsher 
727e42c3d85SBen Hutchings 	rc = efx->type->fini_dmaq(efx);
7285a6681e2SEdward Cree 	if (rc) {
729e42c3d85SBen Hutchings 		netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
730e42c3d85SBen Hutchings 	} else {
731e42c3d85SBen Hutchings 		netif_dbg(efx, drv, efx->net_dev,
732e42c3d85SBen Hutchings 			  "successfully flushed all queues\n");
733e42c3d85SBen Hutchings 	}
734e42c3d85SBen Hutchings 
735e42c3d85SBen Hutchings 	efx_for_each_channel(channel, efx) {
736874aeea5SJeff Kirsher 		efx_for_each_channel_rx_queue(rx_queue, channel)
737874aeea5SJeff Kirsher 			efx_fini_rx_queue(rx_queue);
738874aeea5SJeff Kirsher 		efx_for_each_possible_channel_tx_queue(tx_queue, channel)
739874aeea5SJeff Kirsher 			efx_fini_tx_queue(tx_queue);
740874aeea5SJeff Kirsher 	}
741874aeea5SJeff Kirsher }
742874aeea5SJeff Kirsher 
743874aeea5SJeff Kirsher static void efx_remove_channel(struct efx_channel *channel)
744874aeea5SJeff Kirsher {
745874aeea5SJeff Kirsher 	struct efx_tx_queue *tx_queue;
746874aeea5SJeff Kirsher 	struct efx_rx_queue *rx_queue;
747874aeea5SJeff Kirsher 
748874aeea5SJeff Kirsher 	netif_dbg(channel->efx, drv, channel->efx->net_dev,
749874aeea5SJeff Kirsher 		  "destroy chan %d\n", channel->channel);
750874aeea5SJeff Kirsher 
751874aeea5SJeff Kirsher 	efx_for_each_channel_rx_queue(rx_queue, channel)
752874aeea5SJeff Kirsher 		efx_remove_rx_queue(rx_queue);
753874aeea5SJeff Kirsher 	efx_for_each_possible_channel_tx_queue(tx_queue, channel)
754874aeea5SJeff Kirsher 		efx_remove_tx_queue(tx_queue);
755874aeea5SJeff Kirsher 	efx_remove_eventq(channel);
756c31e5f9fSStuart Hodgson 	channel->type->post_remove(channel);
757874aeea5SJeff Kirsher }
758874aeea5SJeff Kirsher 
759874aeea5SJeff Kirsher static void efx_remove_channels(struct efx_nic *efx)
760874aeea5SJeff Kirsher {
761874aeea5SJeff Kirsher 	struct efx_channel *channel;
762874aeea5SJeff Kirsher 
763874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx)
764874aeea5SJeff Kirsher 		efx_remove_channel(channel);
765874aeea5SJeff Kirsher }
766874aeea5SJeff Kirsher 
767874aeea5SJeff Kirsher int
768874aeea5SJeff Kirsher efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
769874aeea5SJeff Kirsher {
770874aeea5SJeff Kirsher 	struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
771874aeea5SJeff Kirsher 	u32 old_rxq_entries, old_txq_entries;
7727f967c01SBen Hutchings 	unsigned i, next_buffer_table = 0;
773261e4d96SJon Cooper 	int rc, rc2;
7748b7325b4SBen Hutchings 
7758b7325b4SBen Hutchings 	rc = efx_check_disabled(efx);
7768b7325b4SBen Hutchings 	if (rc)
7778b7325b4SBen Hutchings 		return rc;
7787f967c01SBen Hutchings 
7797f967c01SBen Hutchings 	/* Not all channels should be reallocated. We must avoid
7807f967c01SBen Hutchings 	 * reallocating their buffer table entries.
7817f967c01SBen Hutchings 	 */
7827f967c01SBen Hutchings 	efx_for_each_channel(channel, efx) {
7837f967c01SBen Hutchings 		struct efx_rx_queue *rx_queue;
7847f967c01SBen Hutchings 		struct efx_tx_queue *tx_queue;
7857f967c01SBen Hutchings 
7867f967c01SBen Hutchings 		if (channel->type->copy)
7877f967c01SBen Hutchings 			continue;
7887f967c01SBen Hutchings 		next_buffer_table = max(next_buffer_table,
7897f967c01SBen Hutchings 					channel->eventq.index +
7907f967c01SBen Hutchings 					channel->eventq.entries);
7917f967c01SBen Hutchings 		efx_for_each_channel_rx_queue(rx_queue, channel)
7927f967c01SBen Hutchings 			next_buffer_table = max(next_buffer_table,
7937f967c01SBen Hutchings 						rx_queue->rxd.index +
7947f967c01SBen Hutchings 						rx_queue->rxd.entries);
7957f967c01SBen Hutchings 		efx_for_each_channel_tx_queue(tx_queue, channel)
7967f967c01SBen Hutchings 			next_buffer_table = max(next_buffer_table,
7977f967c01SBen Hutchings 						tx_queue->txd.index +
7987f967c01SBen Hutchings 						tx_queue->txd.entries);
7997f967c01SBen Hutchings 	}
800874aeea5SJeff Kirsher 
80129c69a48SBen Hutchings 	efx_device_detach_sync(efx);
802874aeea5SJeff Kirsher 	efx_stop_all(efx);
803d8291187SBen Hutchings 	efx_soft_disable_interrupts(efx);
804874aeea5SJeff Kirsher 
8057f967c01SBen Hutchings 	/* Clone channels (where possible) */
806874aeea5SJeff Kirsher 	memset(other_channel, 0, sizeof(other_channel));
807874aeea5SJeff Kirsher 	for (i = 0; i < efx->n_channels; i++) {
8087f967c01SBen Hutchings 		channel = efx->channel[i];
8097f967c01SBen Hutchings 		if (channel->type->copy)
8107f967c01SBen Hutchings 			channel = channel->type->copy(channel);
811874aeea5SJeff Kirsher 		if (!channel) {
812874aeea5SJeff Kirsher 			rc = -ENOMEM;
813874aeea5SJeff Kirsher 			goto out;
814874aeea5SJeff Kirsher 		}
815874aeea5SJeff Kirsher 		other_channel[i] = channel;
816874aeea5SJeff Kirsher 	}
817874aeea5SJeff Kirsher 
818874aeea5SJeff Kirsher 	/* Swap entry counts and channel pointers */
819874aeea5SJeff Kirsher 	old_rxq_entries = efx->rxq_entries;
820874aeea5SJeff Kirsher 	old_txq_entries = efx->txq_entries;
821874aeea5SJeff Kirsher 	efx->rxq_entries = rxq_entries;
822874aeea5SJeff Kirsher 	efx->txq_entries = txq_entries;
823874aeea5SJeff Kirsher 	for (i = 0; i < efx->n_channels; i++) {
824874aeea5SJeff Kirsher 		channel = efx->channel[i];
825874aeea5SJeff Kirsher 		efx->channel[i] = other_channel[i];
826874aeea5SJeff Kirsher 		other_channel[i] = channel;
827874aeea5SJeff Kirsher 	}
828874aeea5SJeff Kirsher 
8297f967c01SBen Hutchings 	/* Restart buffer table allocation */
8307f967c01SBen Hutchings 	efx->next_buffer_table = next_buffer_table;
8317f967c01SBen Hutchings 
8327f967c01SBen Hutchings 	for (i = 0; i < efx->n_channels; i++) {
8337f967c01SBen Hutchings 		channel = efx->channel[i];
8347f967c01SBen Hutchings 		if (!channel->type->copy)
8357f967c01SBen Hutchings 			continue;
8367f967c01SBen Hutchings 		rc = efx_probe_channel(channel);
837874aeea5SJeff Kirsher 		if (rc)
838874aeea5SJeff Kirsher 			goto rollback;
8397f967c01SBen Hutchings 		efx_init_napi_channel(efx->channel[i]);
840874aeea5SJeff Kirsher 	}
841874aeea5SJeff Kirsher 
8427f967c01SBen Hutchings out:
8437f967c01SBen Hutchings 	/* Destroy unused channel structures */
8447f967c01SBen Hutchings 	for (i = 0; i < efx->n_channels; i++) {
8457f967c01SBen Hutchings 		channel = other_channel[i];
8467f967c01SBen Hutchings 		if (channel && channel->type->copy) {
8477f967c01SBen Hutchings 			efx_fini_napi_channel(channel);
8487f967c01SBen Hutchings 			efx_remove_channel(channel);
8497f967c01SBen Hutchings 			kfree(channel);
8507f967c01SBen Hutchings 		}
8517f967c01SBen Hutchings 	}
8527f967c01SBen Hutchings 
853261e4d96SJon Cooper 	rc2 = efx_soft_enable_interrupts(efx);
854261e4d96SJon Cooper 	if (rc2) {
855261e4d96SJon Cooper 		rc = rc ? rc : rc2;
856261e4d96SJon Cooper 		netif_err(efx, drv, efx->net_dev,
857261e4d96SJon Cooper 			  "unable to restart interrupts on channel reallocation\n");
858261e4d96SJon Cooper 		efx_schedule_reset(efx, RESET_TYPE_DISABLE);
859261e4d96SJon Cooper 	} else {
860874aeea5SJeff Kirsher 		efx_start_all(efx);
86129c69a48SBen Hutchings 		netif_device_attach(efx->net_dev);
862261e4d96SJon Cooper 	}
863874aeea5SJeff Kirsher 	return rc;
864874aeea5SJeff Kirsher 
865874aeea5SJeff Kirsher rollback:
866874aeea5SJeff Kirsher 	/* Swap back */
867874aeea5SJeff Kirsher 	efx->rxq_entries = old_rxq_entries;
868874aeea5SJeff Kirsher 	efx->txq_entries = old_txq_entries;
869874aeea5SJeff Kirsher 	for (i = 0; i < efx->n_channels; i++) {
870874aeea5SJeff Kirsher 		channel = efx->channel[i];
871874aeea5SJeff Kirsher 		efx->channel[i] = other_channel[i];
872874aeea5SJeff Kirsher 		other_channel[i] = channel;
873874aeea5SJeff Kirsher 	}
874874aeea5SJeff Kirsher 	goto out;
875874aeea5SJeff Kirsher }
876874aeea5SJeff Kirsher 
877874aeea5SJeff Kirsher void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
878874aeea5SJeff Kirsher {
879874aeea5SJeff Kirsher 	mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
880874aeea5SJeff Kirsher }
881874aeea5SJeff Kirsher 
8827f967c01SBen Hutchings static const struct efx_channel_type efx_default_channel_type = {
8837f967c01SBen Hutchings 	.pre_probe		= efx_channel_dummy_op_int,
884c31e5f9fSStuart Hodgson 	.post_remove		= efx_channel_dummy_op_void,
8857f967c01SBen Hutchings 	.get_name		= efx_get_channel_name,
8867f967c01SBen Hutchings 	.copy			= efx_copy_channel,
8877f967c01SBen Hutchings 	.keep_eventq		= false,
8887f967c01SBen Hutchings };
8897f967c01SBen Hutchings 
8907f967c01SBen Hutchings int efx_channel_dummy_op_int(struct efx_channel *channel)
8917f967c01SBen Hutchings {
8927f967c01SBen Hutchings 	return 0;
8937f967c01SBen Hutchings }
8947f967c01SBen Hutchings 
895c31e5f9fSStuart Hodgson void efx_channel_dummy_op_void(struct efx_channel *channel)
896c31e5f9fSStuart Hodgson {
897c31e5f9fSStuart Hodgson }
898c31e5f9fSStuart Hodgson 
899874aeea5SJeff Kirsher /**************************************************************************
900874aeea5SJeff Kirsher  *
901874aeea5SJeff Kirsher  * Port handling
902874aeea5SJeff Kirsher  *
903874aeea5SJeff Kirsher  **************************************************************************/
904874aeea5SJeff Kirsher 
905874aeea5SJeff Kirsher /* This ensures that the kernel is kept informed (via
906874aeea5SJeff Kirsher  * netif_carrier_on/off) of the link status, and also maintains the
907874aeea5SJeff Kirsher  * link status's stop on the port's TX queue.
908874aeea5SJeff Kirsher  */
909874aeea5SJeff Kirsher void efx_link_status_changed(struct efx_nic *efx)
910874aeea5SJeff Kirsher {
911874aeea5SJeff Kirsher 	struct efx_link_state *link_state = &efx->link_state;
912874aeea5SJeff Kirsher 
913874aeea5SJeff Kirsher 	/* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
914874aeea5SJeff Kirsher 	 * that no events are triggered between unregister_netdev() and the
915874aeea5SJeff Kirsher 	 * driver unloading. A more general condition is that NETDEV_CHANGE
916874aeea5SJeff Kirsher 	 * can only be generated between NETDEV_UP and NETDEV_DOWN */
917874aeea5SJeff Kirsher 	if (!netif_running(efx->net_dev))
918874aeea5SJeff Kirsher 		return;
919874aeea5SJeff Kirsher 
920874aeea5SJeff Kirsher 	if (link_state->up != netif_carrier_ok(efx->net_dev)) {
921874aeea5SJeff Kirsher 		efx->n_link_state_changes++;
922874aeea5SJeff Kirsher 
923874aeea5SJeff Kirsher 		if (link_state->up)
924874aeea5SJeff Kirsher 			netif_carrier_on(efx->net_dev);
925874aeea5SJeff Kirsher 		else
926874aeea5SJeff Kirsher 			netif_carrier_off(efx->net_dev);
927874aeea5SJeff Kirsher 	}
928874aeea5SJeff Kirsher 
929874aeea5SJeff Kirsher 	/* Status message for kernel log */
9302aa9ef11SBen Hutchings 	if (link_state->up)
931874aeea5SJeff Kirsher 		netif_info(efx, link, efx->net_dev,
932964e6135SBen Hutchings 			   "link up at %uMbps %s-duplex (MTU %d)\n",
933874aeea5SJeff Kirsher 			   link_state->speed, link_state->fd ? "full" : "half",
934964e6135SBen Hutchings 			   efx->net_dev->mtu);
9352aa9ef11SBen Hutchings 	else
936874aeea5SJeff Kirsher 		netif_info(efx, link, efx->net_dev, "link down\n");
937874aeea5SJeff Kirsher }
938874aeea5SJeff Kirsher 
939874aeea5SJeff Kirsher void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
940874aeea5SJeff Kirsher {
941874aeea5SJeff Kirsher 	efx->link_advertising = advertising;
942874aeea5SJeff Kirsher 	if (advertising) {
943874aeea5SJeff Kirsher 		if (advertising & ADVERTISED_Pause)
944874aeea5SJeff Kirsher 			efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
945874aeea5SJeff Kirsher 		else
946874aeea5SJeff Kirsher 			efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
947874aeea5SJeff Kirsher 		if (advertising & ADVERTISED_Asym_Pause)
948874aeea5SJeff Kirsher 			efx->wanted_fc ^= EFX_FC_TX;
949874aeea5SJeff Kirsher 	}
950874aeea5SJeff Kirsher }
951874aeea5SJeff Kirsher 
952874aeea5SJeff Kirsher void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
953874aeea5SJeff Kirsher {
954874aeea5SJeff Kirsher 	efx->wanted_fc = wanted_fc;
955874aeea5SJeff Kirsher 	if (efx->link_advertising) {
956874aeea5SJeff Kirsher 		if (wanted_fc & EFX_FC_RX)
957874aeea5SJeff Kirsher 			efx->link_advertising |= (ADVERTISED_Pause |
958874aeea5SJeff Kirsher 						  ADVERTISED_Asym_Pause);
959874aeea5SJeff Kirsher 		else
960874aeea5SJeff Kirsher 			efx->link_advertising &= ~(ADVERTISED_Pause |
961874aeea5SJeff Kirsher 						   ADVERTISED_Asym_Pause);
962874aeea5SJeff Kirsher 		if (wanted_fc & EFX_FC_TX)
963874aeea5SJeff Kirsher 			efx->link_advertising ^= ADVERTISED_Asym_Pause;
964874aeea5SJeff Kirsher 	}
965874aeea5SJeff Kirsher }
966874aeea5SJeff Kirsher 
967874aeea5SJeff Kirsher static void efx_fini_port(struct efx_nic *efx);
968874aeea5SJeff Kirsher 
9690d322413SEdward Cree /* We assume that efx->type->reconfigure_mac will always try to sync RX
9700d322413SEdward Cree  * filters and therefore needs to read-lock the filter table against freeing
9710d322413SEdward Cree  */
9720d322413SEdward Cree void efx_mac_reconfigure(struct efx_nic *efx)
9730d322413SEdward Cree {
9740d322413SEdward Cree 	down_read(&efx->filter_sem);
9750d322413SEdward Cree 	efx->type->reconfigure_mac(efx);
9760d322413SEdward Cree 	up_read(&efx->filter_sem);
9770d322413SEdward Cree }
9780d322413SEdward Cree 
979874aeea5SJeff Kirsher /* Push loopback/power/transmit disable settings to the PHY, and reconfigure
980874aeea5SJeff Kirsher  * the MAC appropriately. All other PHY configuration changes are pushed
981874aeea5SJeff Kirsher  * through phy_op->set_settings(), and pushed asynchronously to the MAC
982874aeea5SJeff Kirsher  * through efx_monitor().
983874aeea5SJeff Kirsher  *
984874aeea5SJeff Kirsher  * Callers must hold the mac_lock
985874aeea5SJeff Kirsher  */
986874aeea5SJeff Kirsher int __efx_reconfigure_port(struct efx_nic *efx)
987874aeea5SJeff Kirsher {
988874aeea5SJeff Kirsher 	enum efx_phy_mode phy_mode;
989874aeea5SJeff Kirsher 	int rc;
990874aeea5SJeff Kirsher 
991874aeea5SJeff Kirsher 	WARN_ON(!mutex_is_locked(&efx->mac_lock));
992874aeea5SJeff Kirsher 
993874aeea5SJeff Kirsher 	/* Disable PHY transmit in mac level loopbacks */
994874aeea5SJeff Kirsher 	phy_mode = efx->phy_mode;
995874aeea5SJeff Kirsher 	if (LOOPBACK_INTERNAL(efx))
996874aeea5SJeff Kirsher 		efx->phy_mode |= PHY_MODE_TX_DISABLED;
997874aeea5SJeff Kirsher 	else
998874aeea5SJeff Kirsher 		efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
999874aeea5SJeff Kirsher 
1000874aeea5SJeff Kirsher 	rc = efx->type->reconfigure_port(efx);
1001874aeea5SJeff Kirsher 
1002874aeea5SJeff Kirsher 	if (rc)
1003874aeea5SJeff Kirsher 		efx->phy_mode = phy_mode;
1004874aeea5SJeff Kirsher 
1005874aeea5SJeff Kirsher 	return rc;
1006874aeea5SJeff Kirsher }
1007874aeea5SJeff Kirsher 
1008874aeea5SJeff Kirsher /* Reinitialise the MAC to pick up new PHY settings, even if the port is
1009874aeea5SJeff Kirsher  * disabled. */
1010874aeea5SJeff Kirsher int efx_reconfigure_port(struct efx_nic *efx)
1011874aeea5SJeff Kirsher {
1012874aeea5SJeff Kirsher 	int rc;
1013874aeea5SJeff Kirsher 
1014874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
1015874aeea5SJeff Kirsher 
1016874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
1017874aeea5SJeff Kirsher 	rc = __efx_reconfigure_port(efx);
1018874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
1019874aeea5SJeff Kirsher 
1020874aeea5SJeff Kirsher 	return rc;
1021874aeea5SJeff Kirsher }
1022874aeea5SJeff Kirsher 
1023874aeea5SJeff Kirsher /* Asynchronous work item for changing MAC promiscuity and multicast
1024874aeea5SJeff Kirsher  * hash.  Avoid a drain/rx_ingress enable by reconfiguring the current
1025874aeea5SJeff Kirsher  * MAC directly. */
1026874aeea5SJeff Kirsher static void efx_mac_work(struct work_struct *data)
1027874aeea5SJeff Kirsher {
1028874aeea5SJeff Kirsher 	struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
1029874aeea5SJeff Kirsher 
1030874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
103130b81cdaSBen Hutchings 	if (efx->port_enabled)
10320d322413SEdward Cree 		efx_mac_reconfigure(efx);
1033874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
1034874aeea5SJeff Kirsher }
1035874aeea5SJeff Kirsher 
1036874aeea5SJeff Kirsher static int efx_probe_port(struct efx_nic *efx)
1037874aeea5SJeff Kirsher {
1038874aeea5SJeff Kirsher 	int rc;
1039874aeea5SJeff Kirsher 
1040874aeea5SJeff Kirsher 	netif_dbg(efx, probe, efx->net_dev, "create port\n");
1041874aeea5SJeff Kirsher 
1042874aeea5SJeff Kirsher 	if (phy_flash_cfg)
1043874aeea5SJeff Kirsher 		efx->phy_mode = PHY_MODE_SPECIAL;
1044874aeea5SJeff Kirsher 
1045874aeea5SJeff Kirsher 	/* Connect up MAC/PHY operations table */
1046874aeea5SJeff Kirsher 	rc = efx->type->probe_port(efx);
1047874aeea5SJeff Kirsher 	if (rc)
1048874aeea5SJeff Kirsher 		return rc;
1049874aeea5SJeff Kirsher 
1050e332bcb3SBen Hutchings 	/* Initialise MAC address to permanent address */
1051cd84ff4dSEdward Cree 	ether_addr_copy(efx->net_dev->dev_addr, efx->net_dev->perm_addr);
1052874aeea5SJeff Kirsher 
1053874aeea5SJeff Kirsher 	return 0;
1054874aeea5SJeff Kirsher }
1055874aeea5SJeff Kirsher 
1056874aeea5SJeff Kirsher static int efx_init_port(struct efx_nic *efx)
1057874aeea5SJeff Kirsher {
1058874aeea5SJeff Kirsher 	int rc;
1059874aeea5SJeff Kirsher 
1060874aeea5SJeff Kirsher 	netif_dbg(efx, drv, efx->net_dev, "init port\n");
1061874aeea5SJeff Kirsher 
1062874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
1063874aeea5SJeff Kirsher 
1064874aeea5SJeff Kirsher 	rc = efx->phy_op->init(efx);
1065874aeea5SJeff Kirsher 	if (rc)
1066874aeea5SJeff Kirsher 		goto fail1;
1067874aeea5SJeff Kirsher 
1068874aeea5SJeff Kirsher 	efx->port_initialized = true;
1069874aeea5SJeff Kirsher 
1070874aeea5SJeff Kirsher 	/* Reconfigure the MAC before creating dma queues (required for
1071874aeea5SJeff Kirsher 	 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
10720d322413SEdward Cree 	efx_mac_reconfigure(efx);
1073874aeea5SJeff Kirsher 
1074874aeea5SJeff Kirsher 	/* Ensure the PHY advertises the correct flow control settings */
1075874aeea5SJeff Kirsher 	rc = efx->phy_op->reconfigure(efx);
1076267d9d73SEdward Cree 	if (rc && rc != -EPERM)
1077874aeea5SJeff Kirsher 		goto fail2;
1078874aeea5SJeff Kirsher 
1079874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
1080874aeea5SJeff Kirsher 	return 0;
1081874aeea5SJeff Kirsher 
1082874aeea5SJeff Kirsher fail2:
1083874aeea5SJeff Kirsher 	efx->phy_op->fini(efx);
1084874aeea5SJeff Kirsher fail1:
1085874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
1086874aeea5SJeff Kirsher 	return rc;
1087874aeea5SJeff Kirsher }
1088874aeea5SJeff Kirsher 
1089874aeea5SJeff Kirsher static void efx_start_port(struct efx_nic *efx)
1090874aeea5SJeff Kirsher {
1091874aeea5SJeff Kirsher 	netif_dbg(efx, ifup, efx->net_dev, "start port\n");
1092874aeea5SJeff Kirsher 	BUG_ON(efx->port_enabled);
1093874aeea5SJeff Kirsher 
1094874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
1095874aeea5SJeff Kirsher 	efx->port_enabled = true;
1096874aeea5SJeff Kirsher 
1097d615c039SBen Hutchings 	/* Ensure MAC ingress/egress is enabled */
10980d322413SEdward Cree 	efx_mac_reconfigure(efx);
1099874aeea5SJeff Kirsher 
1100874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
1101874aeea5SJeff Kirsher }
1102874aeea5SJeff Kirsher 
1103d615c039SBen Hutchings /* Cancel work for MAC reconfiguration, periodic hardware monitoring
1104d615c039SBen Hutchings  * and the async self-test, wait for them to finish and prevent them
1105d615c039SBen Hutchings  * being scheduled again.  This doesn't cover online resets, which
1106d615c039SBen Hutchings  * should only be cancelled when removing the device.
1107d615c039SBen Hutchings  */
1108874aeea5SJeff Kirsher static void efx_stop_port(struct efx_nic *efx)
1109874aeea5SJeff Kirsher {
1110874aeea5SJeff Kirsher 	netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
1111874aeea5SJeff Kirsher 
1112d615c039SBen Hutchings 	EFX_ASSERT_RESET_SERIALISED(efx);
1113d615c039SBen Hutchings 
1114874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
1115874aeea5SJeff Kirsher 	efx->port_enabled = false;
1116874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
1117874aeea5SJeff Kirsher 
1118874aeea5SJeff Kirsher 	/* Serialise against efx_set_multicast_list() */
1119874aeea5SJeff Kirsher 	netif_addr_lock_bh(efx->net_dev);
1120874aeea5SJeff Kirsher 	netif_addr_unlock_bh(efx->net_dev);
1121d615c039SBen Hutchings 
1122d615c039SBen Hutchings 	cancel_delayed_work_sync(&efx->monitor_work);
1123d615c039SBen Hutchings 	efx_selftest_async_cancel(efx);
1124d615c039SBen Hutchings 	cancel_work_sync(&efx->mac_work);
1125874aeea5SJeff Kirsher }
1126874aeea5SJeff Kirsher 
1127874aeea5SJeff Kirsher static void efx_fini_port(struct efx_nic *efx)
1128874aeea5SJeff Kirsher {
1129874aeea5SJeff Kirsher 	netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
1130874aeea5SJeff Kirsher 
1131874aeea5SJeff Kirsher 	if (!efx->port_initialized)
1132874aeea5SJeff Kirsher 		return;
1133874aeea5SJeff Kirsher 
1134874aeea5SJeff Kirsher 	efx->phy_op->fini(efx);
1135874aeea5SJeff Kirsher 	efx->port_initialized = false;
1136874aeea5SJeff Kirsher 
1137874aeea5SJeff Kirsher 	efx->link_state.up = false;
1138874aeea5SJeff Kirsher 	efx_link_status_changed(efx);
1139874aeea5SJeff Kirsher }
1140874aeea5SJeff Kirsher 
1141874aeea5SJeff Kirsher static void efx_remove_port(struct efx_nic *efx)
1142874aeea5SJeff Kirsher {
1143874aeea5SJeff Kirsher 	netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
1144874aeea5SJeff Kirsher 
1145874aeea5SJeff Kirsher 	efx->type->remove_port(efx);
1146874aeea5SJeff Kirsher }
1147874aeea5SJeff Kirsher 
1148874aeea5SJeff Kirsher /**************************************************************************
1149874aeea5SJeff Kirsher  *
1150874aeea5SJeff Kirsher  * NIC handling
1151874aeea5SJeff Kirsher  *
1152874aeea5SJeff Kirsher  **************************************************************************/
1153874aeea5SJeff Kirsher 
11540bcf4a64SBen Hutchings static LIST_HEAD(efx_primary_list);
11550bcf4a64SBen Hutchings static LIST_HEAD(efx_unassociated_list);
11560bcf4a64SBen Hutchings 
11570bcf4a64SBen Hutchings static bool efx_same_controller(struct efx_nic *left, struct efx_nic *right)
11580bcf4a64SBen Hutchings {
11590bcf4a64SBen Hutchings 	return left->type == right->type &&
11600bcf4a64SBen Hutchings 		left->vpd_sn && right->vpd_sn &&
11610bcf4a64SBen Hutchings 		!strcmp(left->vpd_sn, right->vpd_sn);
11620bcf4a64SBen Hutchings }
11630bcf4a64SBen Hutchings 
11640bcf4a64SBen Hutchings static void efx_associate(struct efx_nic *efx)
11650bcf4a64SBen Hutchings {
11660bcf4a64SBen Hutchings 	struct efx_nic *other, *next;
11670bcf4a64SBen Hutchings 
11680bcf4a64SBen Hutchings 	if (efx->primary == efx) {
11690bcf4a64SBen Hutchings 		/* Adding primary function; look for secondaries */
11700bcf4a64SBen Hutchings 
11710bcf4a64SBen Hutchings 		netif_dbg(efx, probe, efx->net_dev, "adding to primary list\n");
11720bcf4a64SBen Hutchings 		list_add_tail(&efx->node, &efx_primary_list);
11730bcf4a64SBen Hutchings 
11740bcf4a64SBen Hutchings 		list_for_each_entry_safe(other, next, &efx_unassociated_list,
11750bcf4a64SBen Hutchings 					 node) {
11760bcf4a64SBen Hutchings 			if (efx_same_controller(efx, other)) {
11770bcf4a64SBen Hutchings 				list_del(&other->node);
11780bcf4a64SBen Hutchings 				netif_dbg(other, probe, other->net_dev,
11790bcf4a64SBen Hutchings 					  "moving to secondary list of %s %s\n",
11800bcf4a64SBen Hutchings 					  pci_name(efx->pci_dev),
11810bcf4a64SBen Hutchings 					  efx->net_dev->name);
11820bcf4a64SBen Hutchings 				list_add_tail(&other->node,
11830bcf4a64SBen Hutchings 					      &efx->secondary_list);
11840bcf4a64SBen Hutchings 				other->primary = efx;
11850bcf4a64SBen Hutchings 			}
11860bcf4a64SBen Hutchings 		}
11870bcf4a64SBen Hutchings 	} else {
11880bcf4a64SBen Hutchings 		/* Adding secondary function; look for primary */
11890bcf4a64SBen Hutchings 
11900bcf4a64SBen Hutchings 		list_for_each_entry(other, &efx_primary_list, node) {
11910bcf4a64SBen Hutchings 			if (efx_same_controller(efx, other)) {
11920bcf4a64SBen Hutchings 				netif_dbg(efx, probe, efx->net_dev,
11930bcf4a64SBen Hutchings 					  "adding to secondary list of %s %s\n",
11940bcf4a64SBen Hutchings 					  pci_name(other->pci_dev),
11950bcf4a64SBen Hutchings 					  other->net_dev->name);
11960bcf4a64SBen Hutchings 				list_add_tail(&efx->node,
11970bcf4a64SBen Hutchings 					      &other->secondary_list);
11980bcf4a64SBen Hutchings 				efx->primary = other;
11990bcf4a64SBen Hutchings 				return;
12000bcf4a64SBen Hutchings 			}
12010bcf4a64SBen Hutchings 		}
12020bcf4a64SBen Hutchings 
12030bcf4a64SBen Hutchings 		netif_dbg(efx, probe, efx->net_dev,
12040bcf4a64SBen Hutchings 			  "adding to unassociated list\n");
12050bcf4a64SBen Hutchings 		list_add_tail(&efx->node, &efx_unassociated_list);
12060bcf4a64SBen Hutchings 	}
12070bcf4a64SBen Hutchings }
12080bcf4a64SBen Hutchings 
12090bcf4a64SBen Hutchings static void efx_dissociate(struct efx_nic *efx)
12100bcf4a64SBen Hutchings {
12110bcf4a64SBen Hutchings 	struct efx_nic *other, *next;
12120bcf4a64SBen Hutchings 
12130bcf4a64SBen Hutchings 	list_del(&efx->node);
12140bcf4a64SBen Hutchings 	efx->primary = NULL;
12150bcf4a64SBen Hutchings 
12160bcf4a64SBen Hutchings 	list_for_each_entry_safe(other, next, &efx->secondary_list, node) {
12170bcf4a64SBen Hutchings 		list_del(&other->node);
12180bcf4a64SBen Hutchings 		netif_dbg(other, probe, other->net_dev,
12190bcf4a64SBen Hutchings 			  "moving to unassociated list\n");
12200bcf4a64SBen Hutchings 		list_add_tail(&other->node, &efx_unassociated_list);
12210bcf4a64SBen Hutchings 		other->primary = NULL;
12220bcf4a64SBen Hutchings 	}
12230bcf4a64SBen Hutchings }
12240bcf4a64SBen Hutchings 
1225874aeea5SJeff Kirsher /* This configures the PCI device to enable I/O and DMA. */
1226874aeea5SJeff Kirsher static int efx_init_io(struct efx_nic *efx)
1227874aeea5SJeff Kirsher {
1228874aeea5SJeff Kirsher 	struct pci_dev *pci_dev = efx->pci_dev;
1229874aeea5SJeff Kirsher 	dma_addr_t dma_mask = efx->type->max_dma_mask;
1230b105798fSBen Hutchings 	unsigned int mem_map_size = efx->type->mem_map_size(efx);
123102246a7fSShradha Shah 	int rc, bar;
1232874aeea5SJeff Kirsher 
1233874aeea5SJeff Kirsher 	netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
1234874aeea5SJeff Kirsher 
123502246a7fSShradha Shah 	bar = efx->type->mem_bar;
123602246a7fSShradha Shah 
1237874aeea5SJeff Kirsher 	rc = pci_enable_device(pci_dev);
1238874aeea5SJeff Kirsher 	if (rc) {
1239874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev,
1240874aeea5SJeff Kirsher 			  "failed to enable PCI device\n");
1241874aeea5SJeff Kirsher 		goto fail1;
1242874aeea5SJeff Kirsher 	}
1243874aeea5SJeff Kirsher 
1244874aeea5SJeff Kirsher 	pci_set_master(pci_dev);
1245874aeea5SJeff Kirsher 
1246874aeea5SJeff Kirsher 	/* Set the PCI DMA mask.  Try all possibilities from our
1247874aeea5SJeff Kirsher 	 * genuine mask down to 32 bits, because some architectures
1248874aeea5SJeff Kirsher 	 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
1249874aeea5SJeff Kirsher 	 * masks event though they reject 46 bit masks.
1250874aeea5SJeff Kirsher 	 */
1251874aeea5SJeff Kirsher 	while (dma_mask > 0x7fffffffUL) {
12529663dedaSRussell King 		rc = dma_set_mask_and_coherent(&pci_dev->dev, dma_mask);
1253e9e01846SBen Hutchings 		if (rc == 0)
1254874aeea5SJeff Kirsher 			break;
1255874aeea5SJeff Kirsher 		dma_mask >>= 1;
1256874aeea5SJeff Kirsher 	}
1257874aeea5SJeff Kirsher 	if (rc) {
1258874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev,
1259874aeea5SJeff Kirsher 			  "could not find a suitable DMA mask\n");
1260874aeea5SJeff Kirsher 		goto fail2;
1261874aeea5SJeff Kirsher 	}
1262874aeea5SJeff Kirsher 	netif_dbg(efx, probe, efx->net_dev,
1263874aeea5SJeff Kirsher 		  "using DMA mask %llx\n", (unsigned long long) dma_mask);
1264874aeea5SJeff Kirsher 
126502246a7fSShradha Shah 	efx->membase_phys = pci_resource_start(efx->pci_dev, bar);
126602246a7fSShradha Shah 	rc = pci_request_region(pci_dev, bar, "sfc");
1267874aeea5SJeff Kirsher 	if (rc) {
1268874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev,
1269874aeea5SJeff Kirsher 			  "request for memory BAR failed\n");
1270874aeea5SJeff Kirsher 		rc = -EIO;
1271874aeea5SJeff Kirsher 		goto fail3;
1272874aeea5SJeff Kirsher 	}
1273b105798fSBen Hutchings 	efx->membase = ioremap_nocache(efx->membase_phys, mem_map_size);
1274874aeea5SJeff Kirsher 	if (!efx->membase) {
1275874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev,
1276874aeea5SJeff Kirsher 			  "could not map memory BAR at %llx+%x\n",
1277b105798fSBen Hutchings 			  (unsigned long long)efx->membase_phys, mem_map_size);
1278874aeea5SJeff Kirsher 		rc = -ENOMEM;
1279874aeea5SJeff Kirsher 		goto fail4;
1280874aeea5SJeff Kirsher 	}
1281874aeea5SJeff Kirsher 	netif_dbg(efx, probe, efx->net_dev,
1282874aeea5SJeff Kirsher 		  "memory BAR at %llx+%x (virtual %p)\n",
1283b105798fSBen Hutchings 		  (unsigned long long)efx->membase_phys, mem_map_size,
1284b105798fSBen Hutchings 		  efx->membase);
1285874aeea5SJeff Kirsher 
1286874aeea5SJeff Kirsher 	return 0;
1287874aeea5SJeff Kirsher 
1288874aeea5SJeff Kirsher  fail4:
128902246a7fSShradha Shah 	pci_release_region(efx->pci_dev, bar);
1290874aeea5SJeff Kirsher  fail3:
1291874aeea5SJeff Kirsher 	efx->membase_phys = 0;
1292874aeea5SJeff Kirsher  fail2:
1293874aeea5SJeff Kirsher 	pci_disable_device(efx->pci_dev);
1294874aeea5SJeff Kirsher  fail1:
1295874aeea5SJeff Kirsher 	return rc;
1296874aeea5SJeff Kirsher }
1297874aeea5SJeff Kirsher 
1298874aeea5SJeff Kirsher static void efx_fini_io(struct efx_nic *efx)
1299874aeea5SJeff Kirsher {
130002246a7fSShradha Shah 	int bar;
130102246a7fSShradha Shah 
1302874aeea5SJeff Kirsher 	netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
1303874aeea5SJeff Kirsher 
1304874aeea5SJeff Kirsher 	if (efx->membase) {
1305874aeea5SJeff Kirsher 		iounmap(efx->membase);
1306874aeea5SJeff Kirsher 		efx->membase = NULL;
1307874aeea5SJeff Kirsher 	}
1308874aeea5SJeff Kirsher 
1309874aeea5SJeff Kirsher 	if (efx->membase_phys) {
131002246a7fSShradha Shah 		bar = efx->type->mem_bar;
131102246a7fSShradha Shah 		pci_release_region(efx->pci_dev, bar);
1312874aeea5SJeff Kirsher 		efx->membase_phys = 0;
1313874aeea5SJeff Kirsher 	}
1314874aeea5SJeff Kirsher 
13156598dad2SDaniel Pieczko 	/* Don't disable bus-mastering if VFs are assigned */
13166598dad2SDaniel Pieczko 	if (!pci_vfs_assigned(efx->pci_dev))
1317874aeea5SJeff Kirsher 		pci_disable_device(efx->pci_dev);
1318874aeea5SJeff Kirsher }
1319874aeea5SJeff Kirsher 
1320267c0157SJon Cooper void efx_set_default_rx_indir_table(struct efx_nic *efx)
1321267c0157SJon Cooper {
1322267c0157SJon Cooper 	size_t i;
1323267c0157SJon Cooper 
1324267c0157SJon Cooper 	for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
1325267c0157SJon Cooper 		efx->rx_indir_table[i] =
1326267c0157SJon Cooper 			ethtool_rxfh_indir_default(i, efx->rss_spread);
1327267c0157SJon Cooper }
1328267c0157SJon Cooper 
1329a9a52506SBen Hutchings static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
1330874aeea5SJeff Kirsher {
1331cdb08f8fSBen Hutchings 	cpumask_var_t thread_mask;
1332a16e5b24SBen Hutchings 	unsigned int count;
1333874aeea5SJeff Kirsher 	int cpu;
1334874aeea5SJeff Kirsher 
1335cd2d5b52SBen Hutchings 	if (rss_cpus) {
1336cd2d5b52SBen Hutchings 		count = rss_cpus;
1337cd2d5b52SBen Hutchings 	} else {
1338cdb08f8fSBen Hutchings 		if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
1339a9a52506SBen Hutchings 			netif_warn(efx, probe, efx->net_dev,
1340a9a52506SBen Hutchings 				   "RSS disabled due to allocation failure\n");
1341874aeea5SJeff Kirsher 			return 1;
1342874aeea5SJeff Kirsher 		}
1343874aeea5SJeff Kirsher 
1344874aeea5SJeff Kirsher 		count = 0;
1345874aeea5SJeff Kirsher 		for_each_online_cpu(cpu) {
1346cdb08f8fSBen Hutchings 			if (!cpumask_test_cpu(cpu, thread_mask)) {
1347874aeea5SJeff Kirsher 				++count;
1348cdb08f8fSBen Hutchings 				cpumask_or(thread_mask, thread_mask,
134906931e62SBartosz Golaszewski 					   topology_sibling_cpumask(cpu));
1350874aeea5SJeff Kirsher 			}
1351874aeea5SJeff Kirsher 		}
1352874aeea5SJeff Kirsher 
1353cdb08f8fSBen Hutchings 		free_cpumask_var(thread_mask);
1354cd2d5b52SBen Hutchings 	}
1355cd2d5b52SBen Hutchings 
1356cd2d5b52SBen Hutchings 	/* If RSS is requested for the PF *and* VFs then we can't write RSS
1357cd2d5b52SBen Hutchings 	 * table entries that are inaccessible to VFs
1358cd2d5b52SBen Hutchings 	 */
13597fa8d547SShradha Shah #ifdef CONFIG_SFC_SRIOV
13607fa8d547SShradha Shah 	if (efx->type->sriov_wanted) {
1361d98a4ffeSShradha Shah 		if (efx->type->sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
1362cd2d5b52SBen Hutchings 		    count > efx_vf_size(efx)) {
1363cd2d5b52SBen Hutchings 			netif_warn(efx, probe, efx->net_dev,
1364cd2d5b52SBen Hutchings 				   "Reducing number of RSS channels from %u to %u for "
1365cd2d5b52SBen Hutchings 				   "VF support. Increase vf-msix-limit to use more "
1366cd2d5b52SBen Hutchings 				   "channels on the PF.\n",
1367cd2d5b52SBen Hutchings 				   count, efx_vf_size(efx));
1368cd2d5b52SBen Hutchings 			count = efx_vf_size(efx);
1369cd2d5b52SBen Hutchings 		}
13707fa8d547SShradha Shah 	}
13717fa8d547SShradha Shah #endif
1372cd2d5b52SBen Hutchings 
1373874aeea5SJeff Kirsher 	return count;
1374874aeea5SJeff Kirsher }
1375874aeea5SJeff Kirsher 
1376874aeea5SJeff Kirsher /* Probe the number and type of interrupts we are able to obtain, and
1377874aeea5SJeff Kirsher  * the resulting numbers of channels and RX queues.
1378874aeea5SJeff Kirsher  */
1379874aeea5SJeff Kirsher static int efx_probe_interrupts(struct efx_nic *efx)
1380874aeea5SJeff Kirsher {
13817f967c01SBen Hutchings 	unsigned int extra_channels = 0;
13827f967c01SBen Hutchings 	unsigned int i, j;
1383a16e5b24SBen Hutchings 	int rc;
1384874aeea5SJeff Kirsher 
13857f967c01SBen Hutchings 	for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
13867f967c01SBen Hutchings 		if (efx->extra_channel_type[i])
13877f967c01SBen Hutchings 			++extra_channels;
13887f967c01SBen Hutchings 
1389874aeea5SJeff Kirsher 	if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
1390874aeea5SJeff Kirsher 		struct msix_entry xentries[EFX_MAX_CHANNELS];
1391a16e5b24SBen Hutchings 		unsigned int n_channels;
1392874aeea5SJeff Kirsher 
1393a9a52506SBen Hutchings 		n_channels = efx_wanted_parallelism(efx);
1394b0fbdae1SShradha Shah 		if (efx_separate_tx_channels)
1395874aeea5SJeff Kirsher 			n_channels *= 2;
13967f967c01SBen Hutchings 		n_channels += extra_channels;
1397b105798fSBen Hutchings 		n_channels = min(n_channels, efx->max_channels);
1398874aeea5SJeff Kirsher 
1399874aeea5SJeff Kirsher 		for (i = 0; i < n_channels; i++)
1400874aeea5SJeff Kirsher 			xentries[i].entry = i;
1401184603d8SAlexander Gordeev 		rc = pci_enable_msix_range(efx->pci_dev,
1402184603d8SAlexander Gordeev 					   xentries, 1, n_channels);
1403184603d8SAlexander Gordeev 		if (rc < 0) {
1404184603d8SAlexander Gordeev 			/* Fall back to single channel MSI */
1405184603d8SAlexander Gordeev 			efx->interrupt_mode = EFX_INT_MODE_MSI;
1406184603d8SAlexander Gordeev 			netif_err(efx, drv, efx->net_dev,
1407184603d8SAlexander Gordeev 				  "could not enable MSI-X\n");
1408184603d8SAlexander Gordeev 		} else if (rc < n_channels) {
1409874aeea5SJeff Kirsher 			netif_err(efx, drv, efx->net_dev,
1410874aeea5SJeff Kirsher 				  "WARNING: Insufficient MSI-X vectors"
1411a16e5b24SBen Hutchings 				  " available (%d < %u).\n", rc, n_channels);
1412874aeea5SJeff Kirsher 			netif_err(efx, drv, efx->net_dev,
1413874aeea5SJeff Kirsher 				  "WARNING: Performance may be reduced.\n");
1414874aeea5SJeff Kirsher 			n_channels = rc;
1415874aeea5SJeff Kirsher 		}
1416874aeea5SJeff Kirsher 
1417184603d8SAlexander Gordeev 		if (rc > 0) {
1418874aeea5SJeff Kirsher 			efx->n_channels = n_channels;
14197f967c01SBen Hutchings 			if (n_channels > extra_channels)
14207f967c01SBen Hutchings 				n_channels -= extra_channels;
1421b0fbdae1SShradha Shah 			if (efx_separate_tx_channels) {
1422b0fbdae1SShradha Shah 				efx->n_tx_channels = min(max(n_channels / 2,
1423b0fbdae1SShradha Shah 							     1U),
1424b0fbdae1SShradha Shah 							 efx->max_tx_channels);
14257f967c01SBen Hutchings 				efx->n_rx_channels = max(n_channels -
14267f967c01SBen Hutchings 							 efx->n_tx_channels,
14277f967c01SBen Hutchings 							 1U);
1428874aeea5SJeff Kirsher 			} else {
1429b0fbdae1SShradha Shah 				efx->n_tx_channels = min(n_channels,
1430b0fbdae1SShradha Shah 							 efx->max_tx_channels);
14317f967c01SBen Hutchings 				efx->n_rx_channels = n_channels;
1432874aeea5SJeff Kirsher 			}
14337f967c01SBen Hutchings 			for (i = 0; i < efx->n_channels; i++)
1434874aeea5SJeff Kirsher 				efx_get_channel(efx, i)->irq =
1435874aeea5SJeff Kirsher 					xentries[i].vector;
1436874aeea5SJeff Kirsher 		}
1437874aeea5SJeff Kirsher 	}
1438874aeea5SJeff Kirsher 
1439874aeea5SJeff Kirsher 	/* Try single interrupt MSI */
1440874aeea5SJeff Kirsher 	if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
1441874aeea5SJeff Kirsher 		efx->n_channels = 1;
1442874aeea5SJeff Kirsher 		efx->n_rx_channels = 1;
1443874aeea5SJeff Kirsher 		efx->n_tx_channels = 1;
1444874aeea5SJeff Kirsher 		rc = pci_enable_msi(efx->pci_dev);
1445874aeea5SJeff Kirsher 		if (rc == 0) {
1446874aeea5SJeff Kirsher 			efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
1447874aeea5SJeff Kirsher 		} else {
1448874aeea5SJeff Kirsher 			netif_err(efx, drv, efx->net_dev,
1449874aeea5SJeff Kirsher 				  "could not enable MSI\n");
1450874aeea5SJeff Kirsher 			efx->interrupt_mode = EFX_INT_MODE_LEGACY;
1451874aeea5SJeff Kirsher 		}
1452874aeea5SJeff Kirsher 	}
1453874aeea5SJeff Kirsher 
1454874aeea5SJeff Kirsher 	/* Assume legacy interrupts */
1455874aeea5SJeff Kirsher 	if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
1456b0fbdae1SShradha Shah 		efx->n_channels = 1 + (efx_separate_tx_channels ? 1 : 0);
1457874aeea5SJeff Kirsher 		efx->n_rx_channels = 1;
1458874aeea5SJeff Kirsher 		efx->n_tx_channels = 1;
1459874aeea5SJeff Kirsher 		efx->legacy_irq = efx->pci_dev->irq;
1460874aeea5SJeff Kirsher 	}
1461874aeea5SJeff Kirsher 
14627f967c01SBen Hutchings 	/* Assign extra channels if possible */
14637f967c01SBen Hutchings 	j = efx->n_channels;
14647f967c01SBen Hutchings 	for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
14657f967c01SBen Hutchings 		if (!efx->extra_channel_type[i])
14667f967c01SBen Hutchings 			continue;
14677f967c01SBen Hutchings 		if (efx->interrupt_mode != EFX_INT_MODE_MSIX ||
14687f967c01SBen Hutchings 		    efx->n_channels <= extra_channels) {
14697f967c01SBen Hutchings 			efx->extra_channel_type[i]->handle_no_channel(efx);
14707f967c01SBen Hutchings 		} else {
14717f967c01SBen Hutchings 			--j;
14727f967c01SBen Hutchings 			efx_get_channel(efx, j)->type =
14737f967c01SBen Hutchings 				efx->extra_channel_type[i];
14747f967c01SBen Hutchings 		}
14757f967c01SBen Hutchings 	}
14767f967c01SBen Hutchings 
1477cd2d5b52SBen Hutchings 	/* RSS might be usable on VFs even if it is disabled on the PF */
14787fa8d547SShradha Shah #ifdef CONFIG_SFC_SRIOV
14797fa8d547SShradha Shah 	if (efx->type->sriov_wanted) {
1480327c685eSShradha Shah 		efx->rss_spread = ((efx->n_rx_channels > 1 ||
1481d98a4ffeSShradha Shah 				    !efx->type->sriov_wanted(efx)) ?
1482cd2d5b52SBen Hutchings 				   efx->n_rx_channels : efx_vf_size(efx));
14837fa8d547SShradha Shah 		return 0;
14847fa8d547SShradha Shah 	}
14857fa8d547SShradha Shah #endif
14867fa8d547SShradha Shah 	efx->rss_spread = efx->n_rx_channels;
1487cd2d5b52SBen Hutchings 
1488874aeea5SJeff Kirsher 	return 0;
1489874aeea5SJeff Kirsher }
1490874aeea5SJeff Kirsher 
1491261e4d96SJon Cooper static int efx_soft_enable_interrupts(struct efx_nic *efx)
1492d8291187SBen Hutchings {
1493261e4d96SJon Cooper 	struct efx_channel *channel, *end_channel;
1494261e4d96SJon Cooper 	int rc;
1495d8291187SBen Hutchings 
1496d8291187SBen Hutchings 	BUG_ON(efx->state == STATE_DISABLED);
1497d8291187SBen Hutchings 
1498d8291187SBen Hutchings 	efx->irq_soft_enabled = true;
1499d8291187SBen Hutchings 	smp_wmb();
1500d8291187SBen Hutchings 
1501d8291187SBen Hutchings 	efx_for_each_channel(channel, efx) {
1502261e4d96SJon Cooper 		if (!channel->type->keep_eventq) {
1503261e4d96SJon Cooper 			rc = efx_init_eventq(channel);
1504261e4d96SJon Cooper 			if (rc)
1505261e4d96SJon Cooper 				goto fail;
1506261e4d96SJon Cooper 		}
1507d8291187SBen Hutchings 		efx_start_eventq(channel);
1508d8291187SBen Hutchings 	}
1509d8291187SBen Hutchings 
1510d8291187SBen Hutchings 	efx_mcdi_mode_event(efx);
1511261e4d96SJon Cooper 
1512261e4d96SJon Cooper 	return 0;
1513261e4d96SJon Cooper fail:
1514261e4d96SJon Cooper 	end_channel = channel;
1515261e4d96SJon Cooper 	efx_for_each_channel(channel, efx) {
1516261e4d96SJon Cooper 		if (channel == end_channel)
1517261e4d96SJon Cooper 			break;
1518261e4d96SJon Cooper 		efx_stop_eventq(channel);
1519261e4d96SJon Cooper 		if (!channel->type->keep_eventq)
1520261e4d96SJon Cooper 			efx_fini_eventq(channel);
1521261e4d96SJon Cooper 	}
1522261e4d96SJon Cooper 
1523261e4d96SJon Cooper 	return rc;
1524d8291187SBen Hutchings }
1525d8291187SBen Hutchings 
1526d8291187SBen Hutchings static void efx_soft_disable_interrupts(struct efx_nic *efx)
1527d8291187SBen Hutchings {
1528d8291187SBen Hutchings 	struct efx_channel *channel;
1529d8291187SBen Hutchings 
1530d8291187SBen Hutchings 	if (efx->state == STATE_DISABLED)
1531d8291187SBen Hutchings 		return;
1532d8291187SBen Hutchings 
1533d8291187SBen Hutchings 	efx_mcdi_mode_poll(efx);
1534d8291187SBen Hutchings 
1535d8291187SBen Hutchings 	efx->irq_soft_enabled = false;
1536d8291187SBen Hutchings 	smp_wmb();
1537d8291187SBen Hutchings 
1538d8291187SBen Hutchings 	if (efx->legacy_irq)
1539d8291187SBen Hutchings 		synchronize_irq(efx->legacy_irq);
1540d8291187SBen Hutchings 
1541d8291187SBen Hutchings 	efx_for_each_channel(channel, efx) {
1542d8291187SBen Hutchings 		if (channel->irq)
1543d8291187SBen Hutchings 			synchronize_irq(channel->irq);
1544d8291187SBen Hutchings 
1545d8291187SBen Hutchings 		efx_stop_eventq(channel);
1546d8291187SBen Hutchings 		if (!channel->type->keep_eventq)
1547d8291187SBen Hutchings 			efx_fini_eventq(channel);
1548d8291187SBen Hutchings 	}
1549cade715fSBen Hutchings 
1550cade715fSBen Hutchings 	/* Flush the asynchronous MCDI request queue */
1551cade715fSBen Hutchings 	efx_mcdi_flush_async(efx);
1552d8291187SBen Hutchings }
1553d8291187SBen Hutchings 
1554261e4d96SJon Cooper static int efx_enable_interrupts(struct efx_nic *efx)
15559f2cb71cSBen Hutchings {
1556261e4d96SJon Cooper 	struct efx_channel *channel, *end_channel;
1557261e4d96SJon Cooper 	int rc;
15589f2cb71cSBen Hutchings 
15598b7325b4SBen Hutchings 	BUG_ON(efx->state == STATE_DISABLED);
15608b7325b4SBen Hutchings 
1561b28405b0SAlexandre Rames 	if (efx->eeh_disabled_legacy_irq) {
1562b28405b0SAlexandre Rames 		enable_irq(efx->legacy_irq);
1563b28405b0SAlexandre Rames 		efx->eeh_disabled_legacy_irq = false;
1564b28405b0SAlexandre Rames 	}
1565d8291187SBen Hutchings 
156686094f7fSBen Hutchings 	efx->type->irq_enable_master(efx);
15679f2cb71cSBen Hutchings 
15689f2cb71cSBen Hutchings 	efx_for_each_channel(channel, efx) {
1569261e4d96SJon Cooper 		if (channel->type->keep_eventq) {
1570261e4d96SJon Cooper 			rc = efx_init_eventq(channel);
1571261e4d96SJon Cooper 			if (rc)
1572261e4d96SJon Cooper 				goto fail;
1573261e4d96SJon Cooper 		}
15749f2cb71cSBen Hutchings 	}
15759f2cb71cSBen Hutchings 
1576261e4d96SJon Cooper 	rc = efx_soft_enable_interrupts(efx);
1577261e4d96SJon Cooper 	if (rc)
1578261e4d96SJon Cooper 		goto fail;
1579261e4d96SJon Cooper 
1580261e4d96SJon Cooper 	return 0;
1581261e4d96SJon Cooper 
1582261e4d96SJon Cooper fail:
1583261e4d96SJon Cooper 	end_channel = channel;
1584261e4d96SJon Cooper 	efx_for_each_channel(channel, efx) {
1585261e4d96SJon Cooper 		if (channel == end_channel)
1586261e4d96SJon Cooper 			break;
1587261e4d96SJon Cooper 		if (channel->type->keep_eventq)
1588261e4d96SJon Cooper 			efx_fini_eventq(channel);
1589261e4d96SJon Cooper 	}
1590261e4d96SJon Cooper 
1591261e4d96SJon Cooper 	efx->type->irq_disable_non_ev(efx);
1592261e4d96SJon Cooper 
1593261e4d96SJon Cooper 	return rc;
15949f2cb71cSBen Hutchings }
15959f2cb71cSBen Hutchings 
1596d8291187SBen Hutchings static void efx_disable_interrupts(struct efx_nic *efx)
15979f2cb71cSBen Hutchings {
15989f2cb71cSBen Hutchings 	struct efx_channel *channel;
15999f2cb71cSBen Hutchings 
1600d8291187SBen Hutchings 	efx_soft_disable_interrupts(efx);
16019f2cb71cSBen Hutchings 
16029f2cb71cSBen Hutchings 	efx_for_each_channel(channel, efx) {
1603d8291187SBen Hutchings 		if (channel->type->keep_eventq)
16049f2cb71cSBen Hutchings 			efx_fini_eventq(channel);
16059f2cb71cSBen Hutchings 	}
1606d8291187SBen Hutchings 
160786094f7fSBen Hutchings 	efx->type->irq_disable_non_ev(efx);
16089f2cb71cSBen Hutchings }
16099f2cb71cSBen Hutchings 
1610874aeea5SJeff Kirsher static void efx_remove_interrupts(struct efx_nic *efx)
1611874aeea5SJeff Kirsher {
1612874aeea5SJeff Kirsher 	struct efx_channel *channel;
1613874aeea5SJeff Kirsher 
1614874aeea5SJeff Kirsher 	/* Remove MSI/MSI-X interrupts */
1615874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx)
1616874aeea5SJeff Kirsher 		channel->irq = 0;
1617874aeea5SJeff Kirsher 	pci_disable_msi(efx->pci_dev);
1618874aeea5SJeff Kirsher 	pci_disable_msix(efx->pci_dev);
1619874aeea5SJeff Kirsher 
1620874aeea5SJeff Kirsher 	/* Remove legacy interrupt */
1621874aeea5SJeff Kirsher 	efx->legacy_irq = 0;
1622874aeea5SJeff Kirsher }
1623874aeea5SJeff Kirsher 
1624874aeea5SJeff Kirsher static void efx_set_channels(struct efx_nic *efx)
1625874aeea5SJeff Kirsher {
1626874aeea5SJeff Kirsher 	struct efx_channel *channel;
1627874aeea5SJeff Kirsher 	struct efx_tx_queue *tx_queue;
1628874aeea5SJeff Kirsher 
1629874aeea5SJeff Kirsher 	efx->tx_channel_offset =
1630b0fbdae1SShradha Shah 		efx_separate_tx_channels ?
1631b0fbdae1SShradha Shah 		efx->n_channels - efx->n_tx_channels : 0;
1632874aeea5SJeff Kirsher 
163379d68b37SStuart Hodgson 	/* We need to mark which channels really have RX and TX
163479d68b37SStuart Hodgson 	 * queues, and adjust the TX queue numbers if we have separate
1635874aeea5SJeff Kirsher 	 * RX-only and TX-only channels.
1636874aeea5SJeff Kirsher 	 */
1637874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx) {
163879d68b37SStuart Hodgson 		if (channel->channel < efx->n_rx_channels)
163979d68b37SStuart Hodgson 			channel->rx_queue.core_index = channel->channel;
164079d68b37SStuart Hodgson 		else
164179d68b37SStuart Hodgson 			channel->rx_queue.core_index = -1;
164279d68b37SStuart Hodgson 
1643874aeea5SJeff Kirsher 		efx_for_each_channel_tx_queue(tx_queue, channel)
1644874aeea5SJeff Kirsher 			tx_queue->queue -= (efx->tx_channel_offset *
1645874aeea5SJeff Kirsher 					    EFX_TXQ_TYPES);
1646874aeea5SJeff Kirsher 	}
1647874aeea5SJeff Kirsher }
1648874aeea5SJeff Kirsher 
1649874aeea5SJeff Kirsher static int efx_probe_nic(struct efx_nic *efx)
1650874aeea5SJeff Kirsher {
1651874aeea5SJeff Kirsher 	int rc;
1652874aeea5SJeff Kirsher 
1653874aeea5SJeff Kirsher 	netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
1654874aeea5SJeff Kirsher 
1655874aeea5SJeff Kirsher 	/* Carry out hardware-type specific initialisation */
1656874aeea5SJeff Kirsher 	rc = efx->type->probe(efx);
1657874aeea5SJeff Kirsher 	if (rc)
1658874aeea5SJeff Kirsher 		return rc;
1659874aeea5SJeff Kirsher 
1660b0fbdae1SShradha Shah 	do {
1661b0fbdae1SShradha Shah 		if (!efx->max_channels || !efx->max_tx_channels) {
1662b0fbdae1SShradha Shah 			netif_err(efx, drv, efx->net_dev,
1663b0fbdae1SShradha Shah 				  "Insufficient resources to allocate"
1664b0fbdae1SShradha Shah 				  " any channels\n");
1665b0fbdae1SShradha Shah 			rc = -ENOSPC;
1666b0fbdae1SShradha Shah 			goto fail1;
1667b0fbdae1SShradha Shah 		}
1668b0fbdae1SShradha Shah 
1669b0fbdae1SShradha Shah 		/* Determine the number of channels and queues by trying
1670b0fbdae1SShradha Shah 		 * to hook in MSI-X interrupts.
1671b0fbdae1SShradha Shah 		 */
1672874aeea5SJeff Kirsher 		rc = efx_probe_interrupts(efx);
1673874aeea5SJeff Kirsher 		if (rc)
1674c15eed22SBen Hutchings 			goto fail1;
1675874aeea5SJeff Kirsher 
167652ad762bSDaniel Pieczko 		efx_set_channels(efx);
167752ad762bSDaniel Pieczko 
1678b0fbdae1SShradha Shah 		/* dimension_resources can fail with EAGAIN */
1679c15eed22SBen Hutchings 		rc = efx->type->dimension_resources(efx);
1680b0fbdae1SShradha Shah 		if (rc != 0 && rc != -EAGAIN)
1681c15eed22SBen Hutchings 			goto fail2;
168228e47c49SBen Hutchings 
1683b0fbdae1SShradha Shah 		if (rc == -EAGAIN)
1684b0fbdae1SShradha Shah 			/* try again with new max_channels */
1685b0fbdae1SShradha Shah 			efx_remove_interrupts(efx);
1686b0fbdae1SShradha Shah 
1687b0fbdae1SShradha Shah 	} while (rc == -EAGAIN);
1688b0fbdae1SShradha Shah 
1689874aeea5SJeff Kirsher 	if (efx->n_channels > 1)
1690267c0157SJon Cooper 		netdev_rss_key_fill(&efx->rx_hash_key,
1691267c0157SJon Cooper 				    sizeof(efx->rx_hash_key));
1692267c0157SJon Cooper 	efx_set_default_rx_indir_table(efx);
1693874aeea5SJeff Kirsher 
1694874aeea5SJeff Kirsher 	netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1695874aeea5SJeff Kirsher 	netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
1696874aeea5SJeff Kirsher 
1697874aeea5SJeff Kirsher 	/* Initialise the interrupt moderation settings */
1698539de7c5SBert Kenward 	efx->irq_mod_step_us = DIV_ROUND_UP(efx->timer_quantum_ns, 1000);
16999e393b30SBen Hutchings 	efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
17009e393b30SBen Hutchings 				true);
1701874aeea5SJeff Kirsher 
1702874aeea5SJeff Kirsher 	return 0;
1703874aeea5SJeff Kirsher 
1704c15eed22SBen Hutchings fail2:
1705c15eed22SBen Hutchings 	efx_remove_interrupts(efx);
1706c15eed22SBen Hutchings fail1:
1707874aeea5SJeff Kirsher 	efx->type->remove(efx);
1708874aeea5SJeff Kirsher 	return rc;
1709874aeea5SJeff Kirsher }
1710874aeea5SJeff Kirsher 
1711874aeea5SJeff Kirsher static void efx_remove_nic(struct efx_nic *efx)
1712874aeea5SJeff Kirsher {
1713874aeea5SJeff Kirsher 	netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
1714874aeea5SJeff Kirsher 
1715874aeea5SJeff Kirsher 	efx_remove_interrupts(efx);
1716874aeea5SJeff Kirsher 	efx->type->remove(efx);
1717874aeea5SJeff Kirsher }
1718874aeea5SJeff Kirsher 
1719add72477SBen Hutchings static int efx_probe_filters(struct efx_nic *efx)
1720add72477SBen Hutchings {
1721add72477SBen Hutchings 	int rc;
1722add72477SBen Hutchings 
1723add72477SBen Hutchings 	spin_lock_init(&efx->filter_lock);
17240d322413SEdward Cree 	init_rwsem(&efx->filter_sem);
1725d248953aSMartin Habets 	mutex_lock(&efx->mac_lock);
17260d322413SEdward Cree 	down_write(&efx->filter_sem);
1727add72477SBen Hutchings 	rc = efx->type->filter_table_probe(efx);
1728add72477SBen Hutchings 	if (rc)
17290d322413SEdward Cree 		goto out_unlock;
1730add72477SBen Hutchings 
1731add72477SBen Hutchings #ifdef CONFIG_RFS_ACCEL
1732add72477SBen Hutchings 	if (efx->type->offload_features & NETIF_F_NTUPLE) {
1733faf8dcc1SJon Cooper 		struct efx_channel *channel;
1734faf8dcc1SJon Cooper 		int i, success = 1;
1735faf8dcc1SJon Cooper 
1736faf8dcc1SJon Cooper 		efx_for_each_channel(channel, efx) {
1737faf8dcc1SJon Cooper 			channel->rps_flow_id =
1738faf8dcc1SJon Cooper 				kcalloc(efx->type->max_rx_ip_filters,
1739faf8dcc1SJon Cooper 					sizeof(*channel->rps_flow_id),
1740add72477SBen Hutchings 					GFP_KERNEL);
1741faf8dcc1SJon Cooper 			if (!channel->rps_flow_id)
1742faf8dcc1SJon Cooper 				success = 0;
1743faf8dcc1SJon Cooper 			else
1744faf8dcc1SJon Cooper 				for (i = 0;
1745faf8dcc1SJon Cooper 				     i < efx->type->max_rx_ip_filters;
1746faf8dcc1SJon Cooper 				     ++i)
1747faf8dcc1SJon Cooper 					channel->rps_flow_id[i] =
1748faf8dcc1SJon Cooper 						RPS_FLOW_ID_INVALID;
1749faf8dcc1SJon Cooper 		}
1750faf8dcc1SJon Cooper 
1751faf8dcc1SJon Cooper 		if (!success) {
1752faf8dcc1SJon Cooper 			efx_for_each_channel(channel, efx)
1753faf8dcc1SJon Cooper 				kfree(channel->rps_flow_id);
1754add72477SBen Hutchings 			efx->type->filter_table_remove(efx);
17550d322413SEdward Cree 			rc = -ENOMEM;
17560d322413SEdward Cree 			goto out_unlock;
1757add72477SBen Hutchings 		}
1758faf8dcc1SJon Cooper 
1759faf8dcc1SJon Cooper 		efx->rps_expire_index = efx->rps_expire_channel = 0;
1760add72477SBen Hutchings 	}
1761add72477SBen Hutchings #endif
17620d322413SEdward Cree out_unlock:
17630d322413SEdward Cree 	up_write(&efx->filter_sem);
1764d248953aSMartin Habets 	mutex_unlock(&efx->mac_lock);
17650d322413SEdward Cree 	return rc;
1766add72477SBen Hutchings }
1767add72477SBen Hutchings 
1768add72477SBen Hutchings static void efx_remove_filters(struct efx_nic *efx)
1769add72477SBen Hutchings {
1770add72477SBen Hutchings #ifdef CONFIG_RFS_ACCEL
1771faf8dcc1SJon Cooper 	struct efx_channel *channel;
1772faf8dcc1SJon Cooper 
1773faf8dcc1SJon Cooper 	efx_for_each_channel(channel, efx)
1774faf8dcc1SJon Cooper 		kfree(channel->rps_flow_id);
1775add72477SBen Hutchings #endif
17760d322413SEdward Cree 	down_write(&efx->filter_sem);
1777add72477SBen Hutchings 	efx->type->filter_table_remove(efx);
17780d322413SEdward Cree 	up_write(&efx->filter_sem);
1779add72477SBen Hutchings }
1780add72477SBen Hutchings 
1781add72477SBen Hutchings static void efx_restore_filters(struct efx_nic *efx)
1782add72477SBen Hutchings {
17830d322413SEdward Cree 	down_read(&efx->filter_sem);
1784add72477SBen Hutchings 	efx->type->filter_table_restore(efx);
17850d322413SEdward Cree 	up_read(&efx->filter_sem);
1786add72477SBen Hutchings }
1787add72477SBen Hutchings 
1788874aeea5SJeff Kirsher /**************************************************************************
1789874aeea5SJeff Kirsher  *
1790874aeea5SJeff Kirsher  * NIC startup/shutdown
1791874aeea5SJeff Kirsher  *
1792874aeea5SJeff Kirsher  *************************************************************************/
1793874aeea5SJeff Kirsher 
1794874aeea5SJeff Kirsher static int efx_probe_all(struct efx_nic *efx)
1795874aeea5SJeff Kirsher {
1796874aeea5SJeff Kirsher 	int rc;
1797874aeea5SJeff Kirsher 
1798874aeea5SJeff Kirsher 	rc = efx_probe_nic(efx);
1799874aeea5SJeff Kirsher 	if (rc) {
1800874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
1801874aeea5SJeff Kirsher 		goto fail1;
1802874aeea5SJeff Kirsher 	}
1803874aeea5SJeff Kirsher 
1804874aeea5SJeff Kirsher 	rc = efx_probe_port(efx);
1805874aeea5SJeff Kirsher 	if (rc) {
1806874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev, "failed to create port\n");
1807874aeea5SJeff Kirsher 		goto fail2;
1808874aeea5SJeff Kirsher 	}
1809874aeea5SJeff Kirsher 
18107e6d06f0SBen Hutchings 	BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
18117e6d06f0SBen Hutchings 	if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
18127e6d06f0SBen Hutchings 		rc = -EINVAL;
18137e6d06f0SBen Hutchings 		goto fail3;
18147e6d06f0SBen Hutchings 	}
1815874aeea5SJeff Kirsher 	efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
1816874aeea5SJeff Kirsher 
18176d8aaaf6SDaniel Pieczko #ifdef CONFIG_SFC_SRIOV
18186d8aaaf6SDaniel Pieczko 	rc = efx->type->vswitching_probe(efx);
18196d8aaaf6SDaniel Pieczko 	if (rc) /* not fatal; the PF will still work fine */
18206d8aaaf6SDaniel Pieczko 		netif_warn(efx, probe, efx->net_dev,
18216d8aaaf6SDaniel Pieczko 			   "failed to setup vswitching rc=%d;"
18226d8aaaf6SDaniel Pieczko 			   " VFs may not function\n", rc);
18236d8aaaf6SDaniel Pieczko #endif
18246d8aaaf6SDaniel Pieczko 
1825874aeea5SJeff Kirsher 	rc = efx_probe_filters(efx);
1826874aeea5SJeff Kirsher 	if (rc) {
1827874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev,
1828874aeea5SJeff Kirsher 			  "failed to create filter tables\n");
18296d8aaaf6SDaniel Pieczko 		goto fail4;
1830874aeea5SJeff Kirsher 	}
1831874aeea5SJeff Kirsher 
18327f967c01SBen Hutchings 	rc = efx_probe_channels(efx);
18337f967c01SBen Hutchings 	if (rc)
18346d8aaaf6SDaniel Pieczko 		goto fail5;
18357f967c01SBen Hutchings 
1836874aeea5SJeff Kirsher 	return 0;
1837874aeea5SJeff Kirsher 
18386d8aaaf6SDaniel Pieczko  fail5:
18397f967c01SBen Hutchings 	efx_remove_filters(efx);
18406d8aaaf6SDaniel Pieczko  fail4:
18416d8aaaf6SDaniel Pieczko #ifdef CONFIG_SFC_SRIOV
18426d8aaaf6SDaniel Pieczko 	efx->type->vswitching_remove(efx);
18436d8aaaf6SDaniel Pieczko #endif
1844874aeea5SJeff Kirsher  fail3:
1845874aeea5SJeff Kirsher 	efx_remove_port(efx);
1846874aeea5SJeff Kirsher  fail2:
1847874aeea5SJeff Kirsher 	efx_remove_nic(efx);
1848874aeea5SJeff Kirsher  fail1:
1849874aeea5SJeff Kirsher 	return rc;
1850874aeea5SJeff Kirsher }
1851874aeea5SJeff Kirsher 
18528b7325b4SBen Hutchings /* If the interface is supposed to be running but is not, start
18538b7325b4SBen Hutchings  * the hardware and software data path, regular activity for the port
18548b7325b4SBen Hutchings  * (MAC statistics, link polling, etc.) and schedule the port to be
18558b7325b4SBen Hutchings  * reconfigured.  Interrupts must already be enabled.  This function
18568b7325b4SBen Hutchings  * is safe to call multiple times, so long as the NIC is not disabled.
18578b7325b4SBen Hutchings  * Requires the RTNL lock.
18589f2cb71cSBen Hutchings  */
1859874aeea5SJeff Kirsher static void efx_start_all(struct efx_nic *efx)
1860874aeea5SJeff Kirsher {
1861874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
18628b7325b4SBen Hutchings 	BUG_ON(efx->state == STATE_DISABLED);
1863874aeea5SJeff Kirsher 
1864874aeea5SJeff Kirsher 	/* Check that it is appropriate to restart the interface. All
1865874aeea5SJeff Kirsher 	 * of these flags are safe to read under just the rtnl lock */
1866e283546cSEdward Cree 	if (efx->port_enabled || !netif_running(efx->net_dev) ||
1867e283546cSEdward Cree 	    efx->reset_pending)
1868874aeea5SJeff Kirsher 		return;
1869874aeea5SJeff Kirsher 
1870874aeea5SJeff Kirsher 	efx_start_port(efx);
18719f2cb71cSBen Hutchings 	efx_start_datapath(efx);
1872874aeea5SJeff Kirsher 
1873626950dbSAlexandre Rames 	/* Start the hardware monitor if there is one */
1874626950dbSAlexandre Rames 	if (efx->type->monitor != NULL)
1875874aeea5SJeff Kirsher 		queue_delayed_work(efx->workqueue, &efx->monitor_work,
1876874aeea5SJeff Kirsher 				   efx_monitor_interval);
1877626950dbSAlexandre Rames 
18785a6681e2SEdward Cree 	/* Link state detection is normally event-driven; we have
1879626950dbSAlexandre Rames 	 * to poll now because we could have missed a change
1880626950dbSAlexandre Rames 	 */
1881874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
1882874aeea5SJeff Kirsher 	if (efx->phy_op->poll(efx))
1883874aeea5SJeff Kirsher 		efx_link_status_changed(efx);
1884874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
1885874aeea5SJeff Kirsher 
1886874aeea5SJeff Kirsher 	efx->type->start_stats(efx);
1887f8f3b5aeSJon Cooper 	efx->type->pull_stats(efx);
1888f8f3b5aeSJon Cooper 	spin_lock_bh(&efx->stats_lock);
1889f8f3b5aeSJon Cooper 	efx->type->update_stats(efx, NULL, NULL);
1890f8f3b5aeSJon Cooper 	spin_unlock_bh(&efx->stats_lock);
1891874aeea5SJeff Kirsher }
1892874aeea5SJeff Kirsher 
18938b7325b4SBen Hutchings /* Quiesce the hardware and software data path, and regular activity
18948b7325b4SBen Hutchings  * for the port without bringing the link down.  Safe to call multiple
18958b7325b4SBen Hutchings  * times with the NIC in almost any state, but interrupts should be
18968b7325b4SBen Hutchings  * enabled.  Requires the RTNL lock.
18978b7325b4SBen Hutchings  */
1898874aeea5SJeff Kirsher static void efx_stop_all(struct efx_nic *efx)
1899874aeea5SJeff Kirsher {
1900874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
1901874aeea5SJeff Kirsher 
1902874aeea5SJeff Kirsher 	/* port_enabled can be read safely under the rtnl lock */
1903874aeea5SJeff Kirsher 	if (!efx->port_enabled)
1904874aeea5SJeff Kirsher 		return;
1905874aeea5SJeff Kirsher 
1906f8f3b5aeSJon Cooper 	/* update stats before we go down so we can accurately count
1907f8f3b5aeSJon Cooper 	 * rx_nodesc_drops
1908f8f3b5aeSJon Cooper 	 */
1909f8f3b5aeSJon Cooper 	efx->type->pull_stats(efx);
1910f8f3b5aeSJon Cooper 	spin_lock_bh(&efx->stats_lock);
1911f8f3b5aeSJon Cooper 	efx->type->update_stats(efx, NULL, NULL);
1912f8f3b5aeSJon Cooper 	spin_unlock_bh(&efx->stats_lock);
1913874aeea5SJeff Kirsher 	efx->type->stop_stats(efx);
1914874aeea5SJeff Kirsher 	efx_stop_port(efx);
1915874aeea5SJeff Kirsher 
191629c69a48SBen Hutchings 	/* Stop the kernel transmit interface.  This is only valid if
191729c69a48SBen Hutchings 	 * the device is stopped or detached; otherwise the watchdog
191829c69a48SBen Hutchings 	 * may fire immediately.
191929c69a48SBen Hutchings 	 */
192029c69a48SBen Hutchings 	WARN_ON(netif_running(efx->net_dev) &&
192129c69a48SBen Hutchings 		netif_device_present(efx->net_dev));
19229f2cb71cSBen Hutchings 	netif_tx_disable(efx->net_dev);
19239f2cb71cSBen Hutchings 
19249f2cb71cSBen Hutchings 	efx_stop_datapath(efx);
1925874aeea5SJeff Kirsher }
1926874aeea5SJeff Kirsher 
1927874aeea5SJeff Kirsher static void efx_remove_all(struct efx_nic *efx)
1928874aeea5SJeff Kirsher {
1929874aeea5SJeff Kirsher 	efx_remove_channels(efx);
19307f967c01SBen Hutchings 	efx_remove_filters(efx);
19316d8aaaf6SDaniel Pieczko #ifdef CONFIG_SFC_SRIOV
19326d8aaaf6SDaniel Pieczko 	efx->type->vswitching_remove(efx);
19336d8aaaf6SDaniel Pieczko #endif
1934874aeea5SJeff Kirsher 	efx_remove_port(efx);
1935874aeea5SJeff Kirsher 	efx_remove_nic(efx);
1936874aeea5SJeff Kirsher }
1937874aeea5SJeff Kirsher 
1938874aeea5SJeff Kirsher /**************************************************************************
1939874aeea5SJeff Kirsher  *
1940874aeea5SJeff Kirsher  * Interrupt moderation
1941874aeea5SJeff Kirsher  *
1942874aeea5SJeff Kirsher  **************************************************************************/
1943539de7c5SBert Kenward unsigned int efx_usecs_to_ticks(struct efx_nic *efx, unsigned int usecs)
1944874aeea5SJeff Kirsher {
1945b548f976SBen Hutchings 	if (usecs == 0)
1946b548f976SBen Hutchings 		return 0;
1947539de7c5SBert Kenward 	if (usecs * 1000 < efx->timer_quantum_ns)
1948874aeea5SJeff Kirsher 		return 1; /* never round down to 0 */
1949539de7c5SBert Kenward 	return usecs * 1000 / efx->timer_quantum_ns;
1950539de7c5SBert Kenward }
1951539de7c5SBert Kenward 
1952539de7c5SBert Kenward unsigned int efx_ticks_to_usecs(struct efx_nic *efx, unsigned int ticks)
1953539de7c5SBert Kenward {
1954539de7c5SBert Kenward 	/* We must round up when converting ticks to microseconds
1955539de7c5SBert Kenward 	 * because we round down when converting the other way.
1956539de7c5SBert Kenward 	 */
1957539de7c5SBert Kenward 	return DIV_ROUND_UP(ticks * efx->timer_quantum_ns, 1000);
1958874aeea5SJeff Kirsher }
1959874aeea5SJeff Kirsher 
1960874aeea5SJeff Kirsher /* Set interrupt moderation parameters */
19619e393b30SBen Hutchings int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
19629e393b30SBen Hutchings 			    unsigned int rx_usecs, bool rx_adaptive,
19639e393b30SBen Hutchings 			    bool rx_may_override_tx)
1964874aeea5SJeff Kirsher {
1965874aeea5SJeff Kirsher 	struct efx_channel *channel;
1966d95e329aSBert Kenward 	unsigned int timer_max_us;
1967d95e329aSBert Kenward 
1968874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
1969874aeea5SJeff Kirsher 
1970d95e329aSBert Kenward 	timer_max_us = efx->timer_max_ns / 1000;
1971d95e329aSBert Kenward 
1972d95e329aSBert Kenward 	if (tx_usecs > timer_max_us || rx_usecs > timer_max_us)
19739e393b30SBen Hutchings 		return -EINVAL;
19749e393b30SBen Hutchings 
1975539de7c5SBert Kenward 	if (tx_usecs != rx_usecs && efx->tx_channel_offset == 0 &&
19769e393b30SBen Hutchings 	    !rx_may_override_tx) {
19779e393b30SBen Hutchings 		netif_err(efx, drv, efx->net_dev, "Channels are shared. "
19789e393b30SBen Hutchings 			  "RX and TX IRQ moderation must be equal\n");
19799e393b30SBen Hutchings 		return -EINVAL;
19809e393b30SBen Hutchings 	}
19819e393b30SBen Hutchings 
1982874aeea5SJeff Kirsher 	efx->irq_rx_adaptive = rx_adaptive;
1983539de7c5SBert Kenward 	efx->irq_rx_moderation_us = rx_usecs;
1984874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx) {
1985874aeea5SJeff Kirsher 		if (efx_channel_has_rx_queue(channel))
1986539de7c5SBert Kenward 			channel->irq_moderation_us = rx_usecs;
1987874aeea5SJeff Kirsher 		else if (efx_channel_has_tx_queues(channel))
1988539de7c5SBert Kenward 			channel->irq_moderation_us = tx_usecs;
1989874aeea5SJeff Kirsher 	}
19909e393b30SBen Hutchings 
19919e393b30SBen Hutchings 	return 0;
1992874aeea5SJeff Kirsher }
1993874aeea5SJeff Kirsher 
1994a0c4faf5SBen Hutchings void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
1995a0c4faf5SBen Hutchings 			    unsigned int *rx_usecs, bool *rx_adaptive)
1996a0c4faf5SBen Hutchings {
1997a0c4faf5SBen Hutchings 	*rx_adaptive = efx->irq_rx_adaptive;
1998539de7c5SBert Kenward 	*rx_usecs = efx->irq_rx_moderation_us;
1999a0c4faf5SBen Hutchings 
2000a0c4faf5SBen Hutchings 	/* If channels are shared between RX and TX, so is IRQ
2001a0c4faf5SBen Hutchings 	 * moderation.  Otherwise, IRQ moderation is the same for all
2002a0c4faf5SBen Hutchings 	 * TX channels and is not adaptive.
2003a0c4faf5SBen Hutchings 	 */
2004539de7c5SBert Kenward 	if (efx->tx_channel_offset == 0) {
2005a0c4faf5SBen Hutchings 		*tx_usecs = *rx_usecs;
2006539de7c5SBert Kenward 	} else {
2007539de7c5SBert Kenward 		struct efx_channel *tx_channel;
2008539de7c5SBert Kenward 
2009539de7c5SBert Kenward 		tx_channel = efx->channel[efx->tx_channel_offset];
2010539de7c5SBert Kenward 		*tx_usecs = tx_channel->irq_moderation_us;
2011539de7c5SBert Kenward 	}
2012a0c4faf5SBen Hutchings }
2013a0c4faf5SBen Hutchings 
2014874aeea5SJeff Kirsher /**************************************************************************
2015874aeea5SJeff Kirsher  *
2016874aeea5SJeff Kirsher  * Hardware monitor
2017874aeea5SJeff Kirsher  *
2018874aeea5SJeff Kirsher  **************************************************************************/
2019874aeea5SJeff Kirsher 
2020874aeea5SJeff Kirsher /* Run periodically off the general workqueue */
2021874aeea5SJeff Kirsher static void efx_monitor(struct work_struct *data)
2022874aeea5SJeff Kirsher {
2023874aeea5SJeff Kirsher 	struct efx_nic *efx = container_of(data, struct efx_nic,
2024874aeea5SJeff Kirsher 					   monitor_work.work);
2025874aeea5SJeff Kirsher 
2026874aeea5SJeff Kirsher 	netif_vdbg(efx, timer, efx->net_dev,
2027874aeea5SJeff Kirsher 		   "hardware monitor executing on CPU %d\n",
2028874aeea5SJeff Kirsher 		   raw_smp_processor_id());
2029874aeea5SJeff Kirsher 	BUG_ON(efx->type->monitor == NULL);
2030874aeea5SJeff Kirsher 
2031874aeea5SJeff Kirsher 	/* If the mac_lock is already held then it is likely a port
2032874aeea5SJeff Kirsher 	 * reconfiguration is already in place, which will likely do
2033874aeea5SJeff Kirsher 	 * most of the work of monitor() anyway. */
2034874aeea5SJeff Kirsher 	if (mutex_trylock(&efx->mac_lock)) {
2035874aeea5SJeff Kirsher 		if (efx->port_enabled)
2036874aeea5SJeff Kirsher 			efx->type->monitor(efx);
2037874aeea5SJeff Kirsher 		mutex_unlock(&efx->mac_lock);
2038874aeea5SJeff Kirsher 	}
2039874aeea5SJeff Kirsher 
2040874aeea5SJeff Kirsher 	queue_delayed_work(efx->workqueue, &efx->monitor_work,
2041874aeea5SJeff Kirsher 			   efx_monitor_interval);
2042874aeea5SJeff Kirsher }
2043874aeea5SJeff Kirsher 
2044874aeea5SJeff Kirsher /**************************************************************************
2045874aeea5SJeff Kirsher  *
2046874aeea5SJeff Kirsher  * ioctls
2047874aeea5SJeff Kirsher  *
2048874aeea5SJeff Kirsher  *************************************************************************/
2049874aeea5SJeff Kirsher 
2050874aeea5SJeff Kirsher /* Net device ioctl
2051874aeea5SJeff Kirsher  * Context: process, rtnl_lock() held.
2052874aeea5SJeff Kirsher  */
2053874aeea5SJeff Kirsher static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
2054874aeea5SJeff Kirsher {
2055874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
2056874aeea5SJeff Kirsher 	struct mii_ioctl_data *data = if_mii(ifr);
2057874aeea5SJeff Kirsher 
20587c236c43SStuart Hodgson 	if (cmd == SIOCSHWTSTAMP)
2059433dc9b3SBen Hutchings 		return efx_ptp_set_ts_config(efx, ifr);
2060433dc9b3SBen Hutchings 	if (cmd == SIOCGHWTSTAMP)
2061433dc9b3SBen Hutchings 		return efx_ptp_get_ts_config(efx, ifr);
20627c236c43SStuart Hodgson 
2063874aeea5SJeff Kirsher 	/* Convert phy_id from older PRTAD/DEVAD format */
2064874aeea5SJeff Kirsher 	if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
2065874aeea5SJeff Kirsher 	    (data->phy_id & 0xfc00) == 0x0400)
2066874aeea5SJeff Kirsher 		data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
2067874aeea5SJeff Kirsher 
2068874aeea5SJeff Kirsher 	return mdio_mii_ioctl(&efx->mdio, data, cmd);
2069874aeea5SJeff Kirsher }
2070874aeea5SJeff Kirsher 
2071874aeea5SJeff Kirsher /**************************************************************************
2072874aeea5SJeff Kirsher  *
2073874aeea5SJeff Kirsher  * NAPI interface
2074874aeea5SJeff Kirsher  *
2075874aeea5SJeff Kirsher  **************************************************************************/
2076874aeea5SJeff Kirsher 
20777f967c01SBen Hutchings static void efx_init_napi_channel(struct efx_channel *channel)
2078874aeea5SJeff Kirsher {
20797f967c01SBen Hutchings 	struct efx_nic *efx = channel->efx;
2080874aeea5SJeff Kirsher 
2081874aeea5SJeff Kirsher 	channel->napi_dev = efx->net_dev;
2082874aeea5SJeff Kirsher 	netif_napi_add(channel->napi_dev, &channel->napi_str,
2083874aeea5SJeff Kirsher 		       efx_poll, napi_weight);
2084874aeea5SJeff Kirsher }
20857f967c01SBen Hutchings 
20867f967c01SBen Hutchings static void efx_init_napi(struct efx_nic *efx)
20877f967c01SBen Hutchings {
20887f967c01SBen Hutchings 	struct efx_channel *channel;
20897f967c01SBen Hutchings 
20907f967c01SBen Hutchings 	efx_for_each_channel(channel, efx)
20917f967c01SBen Hutchings 		efx_init_napi_channel(channel);
2092874aeea5SJeff Kirsher }
2093874aeea5SJeff Kirsher 
2094874aeea5SJeff Kirsher static void efx_fini_napi_channel(struct efx_channel *channel)
2095874aeea5SJeff Kirsher {
2096973334a1SEric Dumazet 	if (channel->napi_dev)
2097874aeea5SJeff Kirsher 		netif_napi_del(&channel->napi_str);
2098973334a1SEric Dumazet 
2099874aeea5SJeff Kirsher 	channel->napi_dev = NULL;
2100874aeea5SJeff Kirsher }
2101874aeea5SJeff Kirsher 
2102874aeea5SJeff Kirsher static void efx_fini_napi(struct efx_nic *efx)
2103874aeea5SJeff Kirsher {
2104874aeea5SJeff Kirsher 	struct efx_channel *channel;
2105874aeea5SJeff Kirsher 
2106874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx)
2107874aeea5SJeff Kirsher 		efx_fini_napi_channel(channel);
2108874aeea5SJeff Kirsher }
2109874aeea5SJeff Kirsher 
2110874aeea5SJeff Kirsher /**************************************************************************
2111874aeea5SJeff Kirsher  *
2112874aeea5SJeff Kirsher  * Kernel netpoll interface
2113874aeea5SJeff Kirsher  *
2114874aeea5SJeff Kirsher  *************************************************************************/
2115874aeea5SJeff Kirsher 
2116874aeea5SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
2117874aeea5SJeff Kirsher 
2118874aeea5SJeff Kirsher /* Although in the common case interrupts will be disabled, this is not
2119874aeea5SJeff Kirsher  * guaranteed. However, all our work happens inside the NAPI callback,
2120874aeea5SJeff Kirsher  * so no locking is required.
2121874aeea5SJeff Kirsher  */
2122874aeea5SJeff Kirsher static void efx_netpoll(struct net_device *net_dev)
2123874aeea5SJeff Kirsher {
2124874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
2125874aeea5SJeff Kirsher 	struct efx_channel *channel;
2126874aeea5SJeff Kirsher 
2127874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx)
2128874aeea5SJeff Kirsher 		efx_schedule_channel(channel);
2129874aeea5SJeff Kirsher }
2130874aeea5SJeff Kirsher 
2131874aeea5SJeff Kirsher #endif
2132874aeea5SJeff Kirsher 
2133874aeea5SJeff Kirsher /**************************************************************************
2134874aeea5SJeff Kirsher  *
2135874aeea5SJeff Kirsher  * Kernel net device interface
2136874aeea5SJeff Kirsher  *
2137874aeea5SJeff Kirsher  *************************************************************************/
2138874aeea5SJeff Kirsher 
2139874aeea5SJeff Kirsher /* Context: process, rtnl_lock() held. */
2140e340be92SShradha Shah int efx_net_open(struct net_device *net_dev)
2141874aeea5SJeff Kirsher {
2142874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
21438b7325b4SBen Hutchings 	int rc;
21448b7325b4SBen Hutchings 
2145874aeea5SJeff Kirsher 	netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
2146874aeea5SJeff Kirsher 		  raw_smp_processor_id());
2147874aeea5SJeff Kirsher 
21488b7325b4SBen Hutchings 	rc = efx_check_disabled(efx);
21498b7325b4SBen Hutchings 	if (rc)
21508b7325b4SBen Hutchings 		return rc;
2151874aeea5SJeff Kirsher 	if (efx->phy_mode & PHY_MODE_SPECIAL)
2152874aeea5SJeff Kirsher 		return -EBUSY;
2153874aeea5SJeff Kirsher 	if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
2154874aeea5SJeff Kirsher 		return -EIO;
2155874aeea5SJeff Kirsher 
2156874aeea5SJeff Kirsher 	/* Notify the kernel of the link state polled during driver load,
2157874aeea5SJeff Kirsher 	 * before the monitor starts running */
2158874aeea5SJeff Kirsher 	efx_link_status_changed(efx);
2159874aeea5SJeff Kirsher 
2160874aeea5SJeff Kirsher 	efx_start_all(efx);
2161dd40781eSBen Hutchings 	efx_selftest_async_start(efx);
2162874aeea5SJeff Kirsher 	return 0;
2163874aeea5SJeff Kirsher }
2164874aeea5SJeff Kirsher 
2165874aeea5SJeff Kirsher /* Context: process, rtnl_lock() held.
2166874aeea5SJeff Kirsher  * Note that the kernel will ignore our return code; this method
2167874aeea5SJeff Kirsher  * should really be a void.
2168874aeea5SJeff Kirsher  */
2169e340be92SShradha Shah int efx_net_stop(struct net_device *net_dev)
2170874aeea5SJeff Kirsher {
2171874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
2172874aeea5SJeff Kirsher 
2173874aeea5SJeff Kirsher 	netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
2174874aeea5SJeff Kirsher 		  raw_smp_processor_id());
2175874aeea5SJeff Kirsher 
2176874aeea5SJeff Kirsher 	/* Stop the device and flush all the channels */
2177874aeea5SJeff Kirsher 	efx_stop_all(efx);
2178874aeea5SJeff Kirsher 
2179874aeea5SJeff Kirsher 	return 0;
2180874aeea5SJeff Kirsher }
2181874aeea5SJeff Kirsher 
2182874aeea5SJeff Kirsher /* Context: process, dev_base_lock or RTNL held, non-blocking. */
2183bc1f4470Sstephen hemminger static void efx_net_stats(struct net_device *net_dev,
21842aa9ef11SBen Hutchings 			  struct rtnl_link_stats64 *stats)
2185874aeea5SJeff Kirsher {
2186874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
2187874aeea5SJeff Kirsher 
2188874aeea5SJeff Kirsher 	spin_lock_bh(&efx->stats_lock);
2189cd0ecc9aSBen Hutchings 	efx->type->update_stats(efx, NULL, stats);
21901cb34522SBen Hutchings 	spin_unlock_bh(&efx->stats_lock);
2191874aeea5SJeff Kirsher }
2192874aeea5SJeff Kirsher 
2193874aeea5SJeff Kirsher /* Context: netif_tx_lock held, BHs disabled. */
2194874aeea5SJeff Kirsher static void efx_watchdog(struct net_device *net_dev)
2195874aeea5SJeff Kirsher {
2196874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
2197874aeea5SJeff Kirsher 
2198874aeea5SJeff Kirsher 	netif_err(efx, tx_err, efx->net_dev,
2199874aeea5SJeff Kirsher 		  "TX stuck with port_enabled=%d: resetting channels\n",
2200874aeea5SJeff Kirsher 		  efx->port_enabled);
2201874aeea5SJeff Kirsher 
2202874aeea5SJeff Kirsher 	efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
2203874aeea5SJeff Kirsher }
2204874aeea5SJeff Kirsher 
2205874aeea5SJeff Kirsher 
2206874aeea5SJeff Kirsher /* Context: process, rtnl_lock() held. */
2207874aeea5SJeff Kirsher static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
2208874aeea5SJeff Kirsher {
2209874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
22108b7325b4SBen Hutchings 	int rc;
2211874aeea5SJeff Kirsher 
22128b7325b4SBen Hutchings 	rc = efx_check_disabled(efx);
22138b7325b4SBen Hutchings 	if (rc)
22148b7325b4SBen Hutchings 		return rc;
2215874aeea5SJeff Kirsher 
2216874aeea5SJeff Kirsher 	netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
2217874aeea5SJeff Kirsher 
221829c69a48SBen Hutchings 	efx_device_detach_sync(efx);
221929c69a48SBen Hutchings 	efx_stop_all(efx);
222029c69a48SBen Hutchings 
2221874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
2222874aeea5SJeff Kirsher 	net_dev->mtu = new_mtu;
22230d322413SEdward Cree 	efx_mac_reconfigure(efx);
2224874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
2225874aeea5SJeff Kirsher 
2226874aeea5SJeff Kirsher 	efx_start_all(efx);
222729c69a48SBen Hutchings 	netif_device_attach(efx->net_dev);
22286c8eef4aSBen Hutchings 	return 0;
2229874aeea5SJeff Kirsher }
2230874aeea5SJeff Kirsher 
2231874aeea5SJeff Kirsher static int efx_set_mac_address(struct net_device *net_dev, void *data)
2232874aeea5SJeff Kirsher {
2233874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
2234874aeea5SJeff Kirsher 	struct sockaddr *addr = data;
2235e0b3ae30SBen Hutchings 	u8 *new_addr = addr->sa_data;
2236cfc77c2fSShradha Shah 	u8 old_addr[6];
2237cfc77c2fSShradha Shah 	int rc;
2238874aeea5SJeff Kirsher 
2239874aeea5SJeff Kirsher 	if (!is_valid_ether_addr(new_addr)) {
2240874aeea5SJeff Kirsher 		netif_err(efx, drv, efx->net_dev,
2241874aeea5SJeff Kirsher 			  "invalid ethernet MAC address requested: %pM\n",
2242874aeea5SJeff Kirsher 			  new_addr);
2243504f9b5aSDanny Kukawka 		return -EADDRNOTAVAIL;
2244874aeea5SJeff Kirsher 	}
2245874aeea5SJeff Kirsher 
2246cfc77c2fSShradha Shah 	/* save old address */
2247cfc77c2fSShradha Shah 	ether_addr_copy(old_addr, net_dev->dev_addr);
2248cd84ff4dSEdward Cree 	ether_addr_copy(net_dev->dev_addr, new_addr);
2249910c8789SShradha Shah 	if (efx->type->set_mac_address) {
2250910c8789SShradha Shah 		rc = efx->type->set_mac_address(efx);
2251cfc77c2fSShradha Shah 		if (rc) {
2252cfc77c2fSShradha Shah 			ether_addr_copy(net_dev->dev_addr, old_addr);
2253cfc77c2fSShradha Shah 			return rc;
2254cfc77c2fSShradha Shah 		}
2255cfc77c2fSShradha Shah 	}
2256874aeea5SJeff Kirsher 
2257874aeea5SJeff Kirsher 	/* Reconfigure the MAC */
2258874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
22590d322413SEdward Cree 	efx_mac_reconfigure(efx);
2260874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
2261874aeea5SJeff Kirsher 
2262874aeea5SJeff Kirsher 	return 0;
2263874aeea5SJeff Kirsher }
2264874aeea5SJeff Kirsher 
2265874aeea5SJeff Kirsher /* Context: netif_addr_lock held, BHs disabled. */
22660fca8c97SBen Hutchings static void efx_set_rx_mode(struct net_device *net_dev)
2267874aeea5SJeff Kirsher {
2268874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
2269874aeea5SJeff Kirsher 
2270874aeea5SJeff Kirsher 	if (efx->port_enabled)
2271874aeea5SJeff Kirsher 		queue_work(efx->workqueue, &efx->mac_work);
2272874aeea5SJeff Kirsher 	/* Otherwise efx_start_port() will do this */
2273874aeea5SJeff Kirsher }
2274874aeea5SJeff Kirsher 
2275c8f44affSMichał Mirosław static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
2276874aeea5SJeff Kirsher {
2277874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
22784a53ea8aSAndrew Rybchenko 	int rc;
2279874aeea5SJeff Kirsher 
2280874aeea5SJeff Kirsher 	/* If disabling RX n-tuple filtering, clear existing filters */
22814a53ea8aSAndrew Rybchenko 	if (net_dev->features & ~data & NETIF_F_NTUPLE) {
22824a53ea8aSAndrew Rybchenko 		rc = efx->type->filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
22834a53ea8aSAndrew Rybchenko 		if (rc)
22844a53ea8aSAndrew Rybchenko 			return rc;
22854a53ea8aSAndrew Rybchenko 	}
22864a53ea8aSAndrew Rybchenko 
22874a53ea8aSAndrew Rybchenko 	/* If Rx VLAN filter is changed, update filters via mac_reconfigure */
22884a53ea8aSAndrew Rybchenko 	if ((net_dev->features ^ data) & NETIF_F_HW_VLAN_CTAG_FILTER) {
22894a53ea8aSAndrew Rybchenko 		/* efx_set_rx_mode() will schedule MAC work to update filters
22904a53ea8aSAndrew Rybchenko 		 * when a new features are finally set in net_dev.
22914a53ea8aSAndrew Rybchenko 		 */
22924a53ea8aSAndrew Rybchenko 		efx_set_rx_mode(net_dev);
22934a53ea8aSAndrew Rybchenko 	}
2294874aeea5SJeff Kirsher 
2295874aeea5SJeff Kirsher 	return 0;
2296874aeea5SJeff Kirsher }
2297874aeea5SJeff Kirsher 
2298b40296fcSWei Yongjun static int efx_get_phys_port_id(struct net_device *net_dev,
229908a7b29bSBert Kenward 				struct netdev_phys_item_id *ppid)
230008a7b29bSBert Kenward {
230108a7b29bSBert Kenward 	struct efx_nic *efx = netdev_priv(net_dev);
230208a7b29bSBert Kenward 
230308a7b29bSBert Kenward 	if (efx->type->get_phys_port_id)
230408a7b29bSBert Kenward 		return efx->type->get_phys_port_id(efx, ppid);
230508a7b29bSBert Kenward 	else
230608a7b29bSBert Kenward 		return -EOPNOTSUPP;
230708a7b29bSBert Kenward }
230808a7b29bSBert Kenward 
2309ac019f08SBert Kenward static int efx_get_phys_port_name(struct net_device *net_dev,
2310ac019f08SBert Kenward 				  char *name, size_t len)
2311ac019f08SBert Kenward {
2312ac019f08SBert Kenward 	struct efx_nic *efx = netdev_priv(net_dev);
2313ac019f08SBert Kenward 
2314ac019f08SBert Kenward 	if (snprintf(name, len, "p%u", efx->port_num) >= len)
2315ac019f08SBert Kenward 		return -EINVAL;
2316ac019f08SBert Kenward 	return 0;
2317ac019f08SBert Kenward }
2318ac019f08SBert Kenward 
23194a53ea8aSAndrew Rybchenko static int efx_vlan_rx_add_vid(struct net_device *net_dev, __be16 proto, u16 vid)
23204a53ea8aSAndrew Rybchenko {
23214a53ea8aSAndrew Rybchenko 	struct efx_nic *efx = netdev_priv(net_dev);
23224a53ea8aSAndrew Rybchenko 
23234a53ea8aSAndrew Rybchenko 	if (efx->type->vlan_rx_add_vid)
23244a53ea8aSAndrew Rybchenko 		return efx->type->vlan_rx_add_vid(efx, proto, vid);
23254a53ea8aSAndrew Rybchenko 	else
23264a53ea8aSAndrew Rybchenko 		return -EOPNOTSUPP;
23274a53ea8aSAndrew Rybchenko }
23284a53ea8aSAndrew Rybchenko 
23294a53ea8aSAndrew Rybchenko static int efx_vlan_rx_kill_vid(struct net_device *net_dev, __be16 proto, u16 vid)
23304a53ea8aSAndrew Rybchenko {
23314a53ea8aSAndrew Rybchenko 	struct efx_nic *efx = netdev_priv(net_dev);
23324a53ea8aSAndrew Rybchenko 
23334a53ea8aSAndrew Rybchenko 	if (efx->type->vlan_rx_kill_vid)
23344a53ea8aSAndrew Rybchenko 		return efx->type->vlan_rx_kill_vid(efx, proto, vid);
23354a53ea8aSAndrew Rybchenko 	else
23364a53ea8aSAndrew Rybchenko 		return -EOPNOTSUPP;
23374a53ea8aSAndrew Rybchenko }
23384a53ea8aSAndrew Rybchenko 
23397fa8d547SShradha Shah static const struct net_device_ops efx_netdev_ops = {
2340874aeea5SJeff Kirsher 	.ndo_open		= efx_net_open,
2341874aeea5SJeff Kirsher 	.ndo_stop		= efx_net_stop,
2342874aeea5SJeff Kirsher 	.ndo_get_stats64	= efx_net_stats,
2343874aeea5SJeff Kirsher 	.ndo_tx_timeout		= efx_watchdog,
2344874aeea5SJeff Kirsher 	.ndo_start_xmit		= efx_hard_start_xmit,
2345874aeea5SJeff Kirsher 	.ndo_validate_addr	= eth_validate_addr,
2346874aeea5SJeff Kirsher 	.ndo_do_ioctl		= efx_ioctl,
2347874aeea5SJeff Kirsher 	.ndo_change_mtu		= efx_change_mtu,
2348874aeea5SJeff Kirsher 	.ndo_set_mac_address	= efx_set_mac_address,
23490fca8c97SBen Hutchings 	.ndo_set_rx_mode	= efx_set_rx_mode,
2350874aeea5SJeff Kirsher 	.ndo_set_features	= efx_set_features,
23514a53ea8aSAndrew Rybchenko 	.ndo_vlan_rx_add_vid	= efx_vlan_rx_add_vid,
23524a53ea8aSAndrew Rybchenko 	.ndo_vlan_rx_kill_vid	= efx_vlan_rx_kill_vid,
2353cd2d5b52SBen Hutchings #ifdef CONFIG_SFC_SRIOV
23547fa8d547SShradha Shah 	.ndo_set_vf_mac		= efx_sriov_set_vf_mac,
23557fa8d547SShradha Shah 	.ndo_set_vf_vlan	= efx_sriov_set_vf_vlan,
23567fa8d547SShradha Shah 	.ndo_set_vf_spoofchk	= efx_sriov_set_vf_spoofchk,
23577fa8d547SShradha Shah 	.ndo_get_vf_config	= efx_sriov_get_vf_config,
23584392dc69SEdward Cree 	.ndo_set_vf_link_state  = efx_sriov_set_vf_link_state,
2359cd2d5b52SBen Hutchings #endif
236008a7b29bSBert Kenward 	.ndo_get_phys_port_id   = efx_get_phys_port_id,
2361ac019f08SBert Kenward 	.ndo_get_phys_port_name	= efx_get_phys_port_name,
2362874aeea5SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
2363874aeea5SJeff Kirsher 	.ndo_poll_controller = efx_netpoll,
2364874aeea5SJeff Kirsher #endif
2365874aeea5SJeff Kirsher 	.ndo_setup_tc		= efx_setup_tc,
2366874aeea5SJeff Kirsher #ifdef CONFIG_RFS_ACCEL
2367874aeea5SJeff Kirsher 	.ndo_rx_flow_steer	= efx_filter_rfs,
2368874aeea5SJeff Kirsher #endif
2369874aeea5SJeff Kirsher };
2370874aeea5SJeff Kirsher 
2371874aeea5SJeff Kirsher static void efx_update_name(struct efx_nic *efx)
2372874aeea5SJeff Kirsher {
2373874aeea5SJeff Kirsher 	strcpy(efx->name, efx->net_dev->name);
2374874aeea5SJeff Kirsher 	efx_mtd_rename(efx);
2375874aeea5SJeff Kirsher 	efx_set_channel_names(efx);
2376874aeea5SJeff Kirsher }
2377874aeea5SJeff Kirsher 
2378874aeea5SJeff Kirsher static int efx_netdev_event(struct notifier_block *this,
2379874aeea5SJeff Kirsher 			    unsigned long event, void *ptr)
2380874aeea5SJeff Kirsher {
2381351638e7SJiri Pirko 	struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
2382874aeea5SJeff Kirsher 
23837fa8d547SShradha Shah 	if ((net_dev->netdev_ops == &efx_netdev_ops) &&
2384874aeea5SJeff Kirsher 	    event == NETDEV_CHANGENAME)
2385874aeea5SJeff Kirsher 		efx_update_name(netdev_priv(net_dev));
2386874aeea5SJeff Kirsher 
2387874aeea5SJeff Kirsher 	return NOTIFY_DONE;
2388874aeea5SJeff Kirsher }
2389874aeea5SJeff Kirsher 
2390874aeea5SJeff Kirsher static struct notifier_block efx_netdev_notifier = {
2391874aeea5SJeff Kirsher 	.notifier_call = efx_netdev_event,
2392874aeea5SJeff Kirsher };
2393874aeea5SJeff Kirsher 
2394874aeea5SJeff Kirsher static ssize_t
2395874aeea5SJeff Kirsher show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
2396874aeea5SJeff Kirsher {
2397874aeea5SJeff Kirsher 	struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2398874aeea5SJeff Kirsher 	return sprintf(buf, "%d\n", efx->phy_type);
2399874aeea5SJeff Kirsher }
2400776fbcc9SBen Hutchings static DEVICE_ATTR(phy_type, 0444, show_phy_type, NULL);
2401874aeea5SJeff Kirsher 
2402e7fef9b4SEdward Cree #ifdef CONFIG_SFC_MCDI_LOGGING
2403e7fef9b4SEdward Cree static ssize_t show_mcdi_log(struct device *dev, struct device_attribute *attr,
2404e7fef9b4SEdward Cree 			     char *buf)
2405e7fef9b4SEdward Cree {
2406e7fef9b4SEdward Cree 	struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2407e7fef9b4SEdward Cree 	struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
2408e7fef9b4SEdward Cree 
2409e7fef9b4SEdward Cree 	return scnprintf(buf, PAGE_SIZE, "%d\n", mcdi->logging_enabled);
2410e7fef9b4SEdward Cree }
2411e7fef9b4SEdward Cree static ssize_t set_mcdi_log(struct device *dev, struct device_attribute *attr,
2412e7fef9b4SEdward Cree 			    const char *buf, size_t count)
2413e7fef9b4SEdward Cree {
2414e7fef9b4SEdward Cree 	struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2415e7fef9b4SEdward Cree 	struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
2416e7fef9b4SEdward Cree 	bool enable = count > 0 && *buf != '0';
2417e7fef9b4SEdward Cree 
2418e7fef9b4SEdward Cree 	mcdi->logging_enabled = enable;
2419e7fef9b4SEdward Cree 	return count;
2420e7fef9b4SEdward Cree }
2421e7fef9b4SEdward Cree static DEVICE_ATTR(mcdi_logging, 0644, show_mcdi_log, set_mcdi_log);
2422e7fef9b4SEdward Cree #endif
2423e7fef9b4SEdward Cree 
2424874aeea5SJeff Kirsher static int efx_register_netdev(struct efx_nic *efx)
2425874aeea5SJeff Kirsher {
2426874aeea5SJeff Kirsher 	struct net_device *net_dev = efx->net_dev;
2427874aeea5SJeff Kirsher 	struct efx_channel *channel;
2428874aeea5SJeff Kirsher 	int rc;
2429874aeea5SJeff Kirsher 
2430874aeea5SJeff Kirsher 	net_dev->watchdog_timeo = 5 * HZ;
2431874aeea5SJeff Kirsher 	net_dev->irq = efx->pci_dev->irq;
24327fa8d547SShradha Shah 	net_dev->netdev_ops = &efx_netdev_ops;
24337fa8d547SShradha Shah 	if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0)
24348127d661SBen Hutchings 		net_dev->priv_flags |= IFF_UNICAST_FLT;
24357ad24ea4SWilfried Klaebe 	net_dev->ethtool_ops = &efx_ethtool_ops;
24367e6d06f0SBen Hutchings 	net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
2437cd94e519SBert Kenward 	net_dev->min_mtu = EFX_MIN_MTU;
2438cd94e519SBert Kenward 	net_dev->max_mtu = EFX_MAX_MTU;
2439874aeea5SJeff Kirsher 
2440874aeea5SJeff Kirsher 	rtnl_lock();
2441874aeea5SJeff Kirsher 
24427153f623SBen Hutchings 	/* Enable resets to be scheduled and check whether any were
24437153f623SBen Hutchings 	 * already requested.  If so, the NIC is probably hosed so we
24447153f623SBen Hutchings 	 * abort.
24457153f623SBen Hutchings 	 */
24467153f623SBen Hutchings 	efx->state = STATE_READY;
24477153f623SBen Hutchings 	smp_mb(); /* ensure we change state before checking reset_pending */
24487153f623SBen Hutchings 	if (efx->reset_pending) {
24497153f623SBen Hutchings 		netif_err(efx, probe, efx->net_dev,
24507153f623SBen Hutchings 			  "aborting probe due to scheduled reset\n");
24517153f623SBen Hutchings 		rc = -EIO;
24527153f623SBen Hutchings 		goto fail_locked;
24537153f623SBen Hutchings 	}
24547153f623SBen Hutchings 
2455874aeea5SJeff Kirsher 	rc = dev_alloc_name(net_dev, net_dev->name);
2456874aeea5SJeff Kirsher 	if (rc < 0)
2457874aeea5SJeff Kirsher 		goto fail_locked;
2458874aeea5SJeff Kirsher 	efx_update_name(efx);
2459874aeea5SJeff Kirsher 
24608f8b3d51SBen Hutchings 	/* Always start with carrier off; PHY events will detect the link */
24618f8b3d51SBen Hutchings 	netif_carrier_off(net_dev);
24628f8b3d51SBen Hutchings 
2463874aeea5SJeff Kirsher 	rc = register_netdevice(net_dev);
2464874aeea5SJeff Kirsher 	if (rc)
2465874aeea5SJeff Kirsher 		goto fail_locked;
2466874aeea5SJeff Kirsher 
2467874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx) {
2468874aeea5SJeff Kirsher 		struct efx_tx_queue *tx_queue;
2469874aeea5SJeff Kirsher 		efx_for_each_channel_tx_queue(tx_queue, channel)
2470874aeea5SJeff Kirsher 			efx_init_tx_queue_core_txq(tx_queue);
2471874aeea5SJeff Kirsher 	}
2472874aeea5SJeff Kirsher 
24730bcf4a64SBen Hutchings 	efx_associate(efx);
24740bcf4a64SBen Hutchings 
2475874aeea5SJeff Kirsher 	rtnl_unlock();
2476874aeea5SJeff Kirsher 
2477874aeea5SJeff Kirsher 	rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2478874aeea5SJeff Kirsher 	if (rc) {
2479874aeea5SJeff Kirsher 		netif_err(efx, drv, efx->net_dev,
2480874aeea5SJeff Kirsher 			  "failed to init net dev attributes\n");
2481874aeea5SJeff Kirsher 		goto fail_registered;
2482874aeea5SJeff Kirsher 	}
2483e7fef9b4SEdward Cree #ifdef CONFIG_SFC_MCDI_LOGGING
2484e7fef9b4SEdward Cree 	rc = device_create_file(&efx->pci_dev->dev, &dev_attr_mcdi_logging);
2485e7fef9b4SEdward Cree 	if (rc) {
2486e7fef9b4SEdward Cree 		netif_err(efx, drv, efx->net_dev,
2487e7fef9b4SEdward Cree 			  "failed to init net dev attributes\n");
2488e7fef9b4SEdward Cree 		goto fail_attr_mcdi_logging;
2489e7fef9b4SEdward Cree 	}
2490e7fef9b4SEdward Cree #endif
2491874aeea5SJeff Kirsher 
2492874aeea5SJeff Kirsher 	return 0;
2493874aeea5SJeff Kirsher 
2494e7fef9b4SEdward Cree #ifdef CONFIG_SFC_MCDI_LOGGING
2495e7fef9b4SEdward Cree fail_attr_mcdi_logging:
2496e7fef9b4SEdward Cree 	device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2497e7fef9b4SEdward Cree #endif
24987153f623SBen Hutchings fail_registered:
24997153f623SBen Hutchings 	rtnl_lock();
25000bcf4a64SBen Hutchings 	efx_dissociate(efx);
25017153f623SBen Hutchings 	unregister_netdevice(net_dev);
2502874aeea5SJeff Kirsher fail_locked:
25037153f623SBen Hutchings 	efx->state = STATE_UNINIT;
2504874aeea5SJeff Kirsher 	rtnl_unlock();
2505874aeea5SJeff Kirsher 	netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
2506874aeea5SJeff Kirsher 	return rc;
2507874aeea5SJeff Kirsher }
2508874aeea5SJeff Kirsher 
2509874aeea5SJeff Kirsher static void efx_unregister_netdev(struct efx_nic *efx)
2510874aeea5SJeff Kirsher {
2511874aeea5SJeff Kirsher 	if (!efx->net_dev)
2512874aeea5SJeff Kirsher 		return;
2513874aeea5SJeff Kirsher 
2514874aeea5SJeff Kirsher 	BUG_ON(netdev_priv(efx->net_dev) != efx);
2515874aeea5SJeff Kirsher 
2516e7fef9b4SEdward Cree 	if (efx_dev_registered(efx)) {
2517874aeea5SJeff Kirsher 		strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2518e7fef9b4SEdward Cree #ifdef CONFIG_SFC_MCDI_LOGGING
2519e7fef9b4SEdward Cree 		device_remove_file(&efx->pci_dev->dev, &dev_attr_mcdi_logging);
2520e7fef9b4SEdward Cree #endif
2521874aeea5SJeff Kirsher 		device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2522e7fef9b4SEdward Cree 		unregister_netdev(efx->net_dev);
2523e7fef9b4SEdward Cree 	}
2524874aeea5SJeff Kirsher }
2525874aeea5SJeff Kirsher 
2526874aeea5SJeff Kirsher /**************************************************************************
2527874aeea5SJeff Kirsher  *
2528874aeea5SJeff Kirsher  * Device reset and suspend
2529874aeea5SJeff Kirsher  *
2530874aeea5SJeff Kirsher  **************************************************************************/
2531874aeea5SJeff Kirsher 
2532874aeea5SJeff Kirsher /* Tears down the entire software state and most of the hardware state
2533874aeea5SJeff Kirsher  * before reset.  */
2534874aeea5SJeff Kirsher void efx_reset_down(struct efx_nic *efx, enum reset_type method)
2535874aeea5SJeff Kirsher {
2536874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
2537874aeea5SJeff Kirsher 
2538e283546cSEdward Cree 	if (method == RESET_TYPE_MCDI_TIMEOUT)
2539e283546cSEdward Cree 		efx->type->prepare_flr(efx);
2540e283546cSEdward Cree 
2541874aeea5SJeff Kirsher 	efx_stop_all(efx);
2542d8291187SBen Hutchings 	efx_disable_interrupts(efx);
25435642ceefSBen Hutchings 
25445642ceefSBen Hutchings 	mutex_lock(&efx->mac_lock);
2545087e9025SJon Cooper 	if (efx->port_initialized && method != RESET_TYPE_INVISIBLE &&
2546087e9025SJon Cooper 	    method != RESET_TYPE_DATAPATH)
2547874aeea5SJeff Kirsher 		efx->phy_op->fini(efx);
2548874aeea5SJeff Kirsher 	efx->type->fini(efx);
2549874aeea5SJeff Kirsher }
2550874aeea5SJeff Kirsher 
2551874aeea5SJeff Kirsher /* This function will always ensure that the locks acquired in
2552874aeea5SJeff Kirsher  * efx_reset_down() are released. A failure return code indicates
2553874aeea5SJeff Kirsher  * that we were unable to reinitialise the hardware, and the
2554874aeea5SJeff Kirsher  * driver should be disabled. If ok is false, then the rx and tx
2555874aeea5SJeff Kirsher  * engines are not restarted, pending a RESET_DISABLE. */
2556874aeea5SJeff Kirsher int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
2557874aeea5SJeff Kirsher {
2558874aeea5SJeff Kirsher 	int rc;
2559874aeea5SJeff Kirsher 
2560874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
2561874aeea5SJeff Kirsher 
2562e283546cSEdward Cree 	if (method == RESET_TYPE_MCDI_TIMEOUT)
2563e283546cSEdward Cree 		efx->type->finish_flr(efx);
2564e283546cSEdward Cree 
2565e283546cSEdward Cree 	/* Ensure that SRAM is initialised even if we're disabling the device */
2566874aeea5SJeff Kirsher 	rc = efx->type->init(efx);
2567874aeea5SJeff Kirsher 	if (rc) {
2568874aeea5SJeff Kirsher 		netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
2569874aeea5SJeff Kirsher 		goto fail;
2570874aeea5SJeff Kirsher 	}
2571874aeea5SJeff Kirsher 
2572874aeea5SJeff Kirsher 	if (!ok)
2573874aeea5SJeff Kirsher 		goto fail;
2574874aeea5SJeff Kirsher 
2575087e9025SJon Cooper 	if (efx->port_initialized && method != RESET_TYPE_INVISIBLE &&
2576087e9025SJon Cooper 	    method != RESET_TYPE_DATAPATH) {
2577874aeea5SJeff Kirsher 		rc = efx->phy_op->init(efx);
2578874aeea5SJeff Kirsher 		if (rc)
2579874aeea5SJeff Kirsher 			goto fail;
2580267d9d73SEdward Cree 		rc = efx->phy_op->reconfigure(efx);
2581267d9d73SEdward Cree 		if (rc && rc != -EPERM)
2582874aeea5SJeff Kirsher 			netif_err(efx, drv, efx->net_dev,
2583874aeea5SJeff Kirsher 				  "could not restore PHY settings\n");
2584874aeea5SJeff Kirsher 	}
2585874aeea5SJeff Kirsher 
2586261e4d96SJon Cooper 	rc = efx_enable_interrupts(efx);
2587261e4d96SJon Cooper 	if (rc)
2588261e4d96SJon Cooper 		goto fail;
25896d8aaaf6SDaniel Pieczko 
25906d8aaaf6SDaniel Pieczko #ifdef CONFIG_SFC_SRIOV
25916d8aaaf6SDaniel Pieczko 	rc = efx->type->vswitching_restore(efx);
25926d8aaaf6SDaniel Pieczko 	if (rc) /* not fatal; the PF will still work fine */
25936d8aaaf6SDaniel Pieczko 		netif_warn(efx, probe, efx->net_dev,
25946d8aaaf6SDaniel Pieczko 			   "failed to restore vswitching rc=%d;"
25956d8aaaf6SDaniel Pieczko 			   " VFs may not function\n", rc);
25966d8aaaf6SDaniel Pieczko #endif
25976d8aaaf6SDaniel Pieczko 
25980d322413SEdward Cree 	down_read(&efx->filter_sem);
2599874aeea5SJeff Kirsher 	efx_restore_filters(efx);
26000d322413SEdward Cree 	up_read(&efx->filter_sem);
26017fa8d547SShradha Shah 	if (efx->type->sriov_reset)
2602d98a4ffeSShradha Shah 		efx->type->sriov_reset(efx);
2603874aeea5SJeff Kirsher 
2604874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
2605874aeea5SJeff Kirsher 
2606874aeea5SJeff Kirsher 	efx_start_all(efx);
2607874aeea5SJeff Kirsher 
2608874aeea5SJeff Kirsher 	return 0;
2609874aeea5SJeff Kirsher 
2610874aeea5SJeff Kirsher fail:
2611874aeea5SJeff Kirsher 	efx->port_initialized = false;
2612874aeea5SJeff Kirsher 
2613874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
2614874aeea5SJeff Kirsher 
2615874aeea5SJeff Kirsher 	return rc;
2616874aeea5SJeff Kirsher }
2617874aeea5SJeff Kirsher 
2618874aeea5SJeff Kirsher /* Reset the NIC using the specified method.  Note that the reset may
2619874aeea5SJeff Kirsher  * fail, in which case the card will be left in an unusable state.
2620874aeea5SJeff Kirsher  *
2621874aeea5SJeff Kirsher  * Caller must hold the rtnl_lock.
2622874aeea5SJeff Kirsher  */
2623874aeea5SJeff Kirsher int efx_reset(struct efx_nic *efx, enum reset_type method)
2624874aeea5SJeff Kirsher {
2625874aeea5SJeff Kirsher 	int rc, rc2;
2626874aeea5SJeff Kirsher 	bool disabled;
2627874aeea5SJeff Kirsher 
2628874aeea5SJeff Kirsher 	netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2629874aeea5SJeff Kirsher 		   RESET_TYPE(method));
2630874aeea5SJeff Kirsher 
2631c2f3b8e3SDaniel Pieczko 	efx_device_detach_sync(efx);
2632874aeea5SJeff Kirsher 	efx_reset_down(efx, method);
2633874aeea5SJeff Kirsher 
2634874aeea5SJeff Kirsher 	rc = efx->type->reset(efx, method);
2635874aeea5SJeff Kirsher 	if (rc) {
2636874aeea5SJeff Kirsher 		netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
2637874aeea5SJeff Kirsher 		goto out;
2638874aeea5SJeff Kirsher 	}
2639874aeea5SJeff Kirsher 
2640874aeea5SJeff Kirsher 	/* Clear flags for the scopes we covered.  We assume the NIC and
2641874aeea5SJeff Kirsher 	 * driver are now quiescent so that there is no race here.
2642874aeea5SJeff Kirsher 	 */
2643e283546cSEdward Cree 	if (method < RESET_TYPE_MAX_METHOD)
2644874aeea5SJeff Kirsher 		efx->reset_pending &= -(1 << (method + 1));
2645e283546cSEdward Cree 	else /* it doesn't fit into the well-ordered scope hierarchy */
2646e283546cSEdward Cree 		__clear_bit(method, &efx->reset_pending);
2647874aeea5SJeff Kirsher 
2648874aeea5SJeff Kirsher 	/* Reinitialise bus-mastering, which may have been turned off before
2649874aeea5SJeff Kirsher 	 * the reset was scheduled. This is still appropriate, even in the
2650874aeea5SJeff Kirsher 	 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2651874aeea5SJeff Kirsher 	 * can respond to requests. */
2652874aeea5SJeff Kirsher 	pci_set_master(efx->pci_dev);
2653874aeea5SJeff Kirsher 
2654874aeea5SJeff Kirsher out:
2655874aeea5SJeff Kirsher 	/* Leave device stopped if necessary */
2656626950dbSAlexandre Rames 	disabled = rc ||
2657626950dbSAlexandre Rames 		method == RESET_TYPE_DISABLE ||
2658626950dbSAlexandre Rames 		method == RESET_TYPE_RECOVER_OR_DISABLE;
2659874aeea5SJeff Kirsher 	rc2 = efx_reset_up(efx, method, !disabled);
2660874aeea5SJeff Kirsher 	if (rc2) {
2661874aeea5SJeff Kirsher 		disabled = true;
2662874aeea5SJeff Kirsher 		if (!rc)
2663874aeea5SJeff Kirsher 			rc = rc2;
2664874aeea5SJeff Kirsher 	}
2665874aeea5SJeff Kirsher 
2666874aeea5SJeff Kirsher 	if (disabled) {
2667874aeea5SJeff Kirsher 		dev_close(efx->net_dev);
2668874aeea5SJeff Kirsher 		netif_err(efx, drv, efx->net_dev, "has been disabled\n");
2669874aeea5SJeff Kirsher 		efx->state = STATE_DISABLED;
2670874aeea5SJeff Kirsher 	} else {
2671874aeea5SJeff Kirsher 		netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
2672874aeea5SJeff Kirsher 		netif_device_attach(efx->net_dev);
2673874aeea5SJeff Kirsher 	}
2674874aeea5SJeff Kirsher 	return rc;
2675874aeea5SJeff Kirsher }
2676874aeea5SJeff Kirsher 
2677626950dbSAlexandre Rames /* Try recovery mechanisms.
2678626950dbSAlexandre Rames  * For now only EEH is supported.
2679626950dbSAlexandre Rames  * Returns 0 if the recovery mechanisms are unsuccessful.
2680626950dbSAlexandre Rames  * Returns a non-zero value otherwise.
2681626950dbSAlexandre Rames  */
2682b28405b0SAlexandre Rames int efx_try_recovery(struct efx_nic *efx)
2683626950dbSAlexandre Rames {
2684626950dbSAlexandre Rames #ifdef CONFIG_EEH
2685626950dbSAlexandre Rames 	/* A PCI error can occur and not be seen by EEH because nothing
2686626950dbSAlexandre Rames 	 * happens on the PCI bus. In this case the driver may fail and
2687626950dbSAlexandre Rames 	 * schedule a 'recover or reset', leading to this recovery handler.
2688626950dbSAlexandre Rames 	 * Manually call the eeh failure check function.
2689626950dbSAlexandre Rames 	 */
269012a89dbaSBenjamin Herrenschmidt 	struct eeh_dev *eehdev = pci_dev_to_eeh_dev(efx->pci_dev);
2691626950dbSAlexandre Rames 	if (eeh_dev_check_failure(eehdev)) {
2692626950dbSAlexandre Rames 		/* The EEH mechanisms will handle the error and reset the
2693626950dbSAlexandre Rames 		 * device if necessary.
2694626950dbSAlexandre Rames 		 */
2695626950dbSAlexandre Rames 		return 1;
2696626950dbSAlexandre Rames 	}
2697626950dbSAlexandre Rames #endif
2698626950dbSAlexandre Rames 	return 0;
2699626950dbSAlexandre Rames }
2700626950dbSAlexandre Rames 
270174cd60a4SJon Cooper static void efx_wait_for_bist_end(struct efx_nic *efx)
270274cd60a4SJon Cooper {
270374cd60a4SJon Cooper 	int i;
270474cd60a4SJon Cooper 
270574cd60a4SJon Cooper 	for (i = 0; i < BIST_WAIT_DELAY_COUNT; ++i) {
270674cd60a4SJon Cooper 		if (efx_mcdi_poll_reboot(efx))
270774cd60a4SJon Cooper 			goto out;
270874cd60a4SJon Cooper 		msleep(BIST_WAIT_DELAY_MS);
270974cd60a4SJon Cooper 	}
271074cd60a4SJon Cooper 
271174cd60a4SJon Cooper 	netif_err(efx, drv, efx->net_dev, "Warning: No MC reboot after BIST mode\n");
271274cd60a4SJon Cooper out:
271374cd60a4SJon Cooper 	/* Either way unset the BIST flag. If we found no reboot we probably
271474cd60a4SJon Cooper 	 * won't recover, but we should try.
271574cd60a4SJon Cooper 	 */
271674cd60a4SJon Cooper 	efx->mc_bist_for_other_fn = false;
271774cd60a4SJon Cooper }
271874cd60a4SJon Cooper 
2719874aeea5SJeff Kirsher /* The worker thread exists so that code that cannot sleep can
2720874aeea5SJeff Kirsher  * schedule a reset for later.
2721874aeea5SJeff Kirsher  */
2722874aeea5SJeff Kirsher static void efx_reset_work(struct work_struct *data)
2723874aeea5SJeff Kirsher {
2724874aeea5SJeff Kirsher 	struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
2725626950dbSAlexandre Rames 	unsigned long pending;
2726626950dbSAlexandre Rames 	enum reset_type method;
2727626950dbSAlexandre Rames 
2728626950dbSAlexandre Rames 	pending = ACCESS_ONCE(efx->reset_pending);
2729626950dbSAlexandre Rames 	method = fls(pending) - 1;
2730626950dbSAlexandre Rames 
273174cd60a4SJon Cooper 	if (method == RESET_TYPE_MC_BIST)
273274cd60a4SJon Cooper 		efx_wait_for_bist_end(efx);
273374cd60a4SJon Cooper 
2734626950dbSAlexandre Rames 	if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
2735626950dbSAlexandre Rames 	     method == RESET_TYPE_RECOVER_OR_ALL) &&
2736626950dbSAlexandre Rames 	    efx_try_recovery(efx))
2737626950dbSAlexandre Rames 		return;
2738874aeea5SJeff Kirsher 
2739874aeea5SJeff Kirsher 	if (!pending)
2740874aeea5SJeff Kirsher 		return;
2741874aeea5SJeff Kirsher 
2742874aeea5SJeff Kirsher 	rtnl_lock();
27437153f623SBen Hutchings 
27447153f623SBen Hutchings 	/* We checked the state in efx_schedule_reset() but it may
27457153f623SBen Hutchings 	 * have changed by now.  Now that we have the RTNL lock,
27467153f623SBen Hutchings 	 * it cannot change again.
27477153f623SBen Hutchings 	 */
27487153f623SBen Hutchings 	if (efx->state == STATE_READY)
2749626950dbSAlexandre Rames 		(void)efx_reset(efx, method);
27507153f623SBen Hutchings 
2751874aeea5SJeff Kirsher 	rtnl_unlock();
2752874aeea5SJeff Kirsher }
2753874aeea5SJeff Kirsher 
2754874aeea5SJeff Kirsher void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2755874aeea5SJeff Kirsher {
2756874aeea5SJeff Kirsher 	enum reset_type method;
2757874aeea5SJeff Kirsher 
2758626950dbSAlexandre Rames 	if (efx->state == STATE_RECOVERY) {
2759626950dbSAlexandre Rames 		netif_dbg(efx, drv, efx->net_dev,
2760626950dbSAlexandre Rames 			  "recovering: skip scheduling %s reset\n",
2761626950dbSAlexandre Rames 			  RESET_TYPE(type));
2762626950dbSAlexandre Rames 		return;
2763626950dbSAlexandre Rames 	}
2764626950dbSAlexandre Rames 
2765874aeea5SJeff Kirsher 	switch (type) {
2766874aeea5SJeff Kirsher 	case RESET_TYPE_INVISIBLE:
2767874aeea5SJeff Kirsher 	case RESET_TYPE_ALL:
2768626950dbSAlexandre Rames 	case RESET_TYPE_RECOVER_OR_ALL:
2769874aeea5SJeff Kirsher 	case RESET_TYPE_WORLD:
2770874aeea5SJeff Kirsher 	case RESET_TYPE_DISABLE:
2771626950dbSAlexandre Rames 	case RESET_TYPE_RECOVER_OR_DISABLE:
2772087e9025SJon Cooper 	case RESET_TYPE_DATAPATH:
277374cd60a4SJon Cooper 	case RESET_TYPE_MC_BIST:
2774e283546cSEdward Cree 	case RESET_TYPE_MCDI_TIMEOUT:
2775874aeea5SJeff Kirsher 		method = type;
2776874aeea5SJeff Kirsher 		netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2777874aeea5SJeff Kirsher 			  RESET_TYPE(method));
2778874aeea5SJeff Kirsher 		break;
2779874aeea5SJeff Kirsher 	default:
2780874aeea5SJeff Kirsher 		method = efx->type->map_reset_reason(type);
2781874aeea5SJeff Kirsher 		netif_dbg(efx, drv, efx->net_dev,
2782874aeea5SJeff Kirsher 			  "scheduling %s reset for %s\n",
2783874aeea5SJeff Kirsher 			  RESET_TYPE(method), RESET_TYPE(type));
2784874aeea5SJeff Kirsher 		break;
2785874aeea5SJeff Kirsher 	}
2786874aeea5SJeff Kirsher 
2787874aeea5SJeff Kirsher 	set_bit(method, &efx->reset_pending);
27887153f623SBen Hutchings 	smp_mb(); /* ensure we change reset_pending before checking state */
27897153f623SBen Hutchings 
27907153f623SBen Hutchings 	/* If we're not READY then just leave the flags set as the cue
27917153f623SBen Hutchings 	 * to abort probing or reschedule the reset later.
27927153f623SBen Hutchings 	 */
27937153f623SBen Hutchings 	if (ACCESS_ONCE(efx->state) != STATE_READY)
27947153f623SBen Hutchings 		return;
2795874aeea5SJeff Kirsher 
2796874aeea5SJeff Kirsher 	/* efx_process_channel() will no longer read events once a
2797874aeea5SJeff Kirsher 	 * reset is scheduled. So switch back to poll'd MCDI completions. */
2798874aeea5SJeff Kirsher 	efx_mcdi_mode_poll(efx);
2799874aeea5SJeff Kirsher 
2800874aeea5SJeff Kirsher 	queue_work(reset_workqueue, &efx->reset_work);
2801874aeea5SJeff Kirsher }
2802874aeea5SJeff Kirsher 
2803874aeea5SJeff Kirsher /**************************************************************************
2804874aeea5SJeff Kirsher  *
2805874aeea5SJeff Kirsher  * List of NICs we support
2806874aeea5SJeff Kirsher  *
2807874aeea5SJeff Kirsher  **************************************************************************/
2808874aeea5SJeff Kirsher 
2809874aeea5SJeff Kirsher /* PCI device ID table */
28109baa3c34SBenoit Taine static const struct pci_device_id efx_pci_table[] = {
2811547c474fSBen Hutchings 	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803),	/* SFC9020 */
2812874aeea5SJeff Kirsher 	 .driver_data = (unsigned long) &siena_a0_nic_type},
2813547c474fSBen Hutchings 	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813),	/* SFL9021 */
2814874aeea5SJeff Kirsher 	 .driver_data = (unsigned long) &siena_a0_nic_type},
28158127d661SBen Hutchings 	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0903),  /* SFC9120 PF */
28168127d661SBen Hutchings 	 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
28176f7f8aa6SShradha Shah 	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1903),  /* SFC9120 VF */
28186f7f8aa6SShradha Shah 	 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
28193b06a00eSMateusz Wrzesinski 	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0923),  /* SFC9140 PF */
28203b06a00eSMateusz Wrzesinski 	 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
2821dd248f1bSBert Kenward 	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1923),  /* SFC9140 VF */
2822dd248f1bSBert Kenward 	 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
2823dd248f1bSBert Kenward 	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0a03),  /* SFC9220 PF */
2824dd248f1bSBert Kenward 	 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
2825dd248f1bSBert Kenward 	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1a03),  /* SFC9220 VF */
2826dd248f1bSBert Kenward 	 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
2827874aeea5SJeff Kirsher 	{0}			/* end of list */
2828874aeea5SJeff Kirsher };
2829874aeea5SJeff Kirsher 
2830874aeea5SJeff Kirsher /**************************************************************************
2831874aeea5SJeff Kirsher  *
2832874aeea5SJeff Kirsher  * Dummy PHY/MAC operations
2833874aeea5SJeff Kirsher  *
2834874aeea5SJeff Kirsher  * Can be used for some unimplemented operations
2835874aeea5SJeff Kirsher  * Needed so all function pointers are valid and do not have to be tested
2836874aeea5SJeff Kirsher  * before use
2837874aeea5SJeff Kirsher  *
2838874aeea5SJeff Kirsher  **************************************************************************/
2839874aeea5SJeff Kirsher int efx_port_dummy_op_int(struct efx_nic *efx)
2840874aeea5SJeff Kirsher {
2841874aeea5SJeff Kirsher 	return 0;
2842874aeea5SJeff Kirsher }
2843874aeea5SJeff Kirsher void efx_port_dummy_op_void(struct efx_nic *efx) {}
2844874aeea5SJeff Kirsher 
2845874aeea5SJeff Kirsher static bool efx_port_dummy_op_poll(struct efx_nic *efx)
2846874aeea5SJeff Kirsher {
2847874aeea5SJeff Kirsher 	return false;
2848874aeea5SJeff Kirsher }
2849874aeea5SJeff Kirsher 
2850874aeea5SJeff Kirsher static const struct efx_phy_operations efx_dummy_phy_operations = {
2851874aeea5SJeff Kirsher 	.init		 = efx_port_dummy_op_int,
2852874aeea5SJeff Kirsher 	.reconfigure	 = efx_port_dummy_op_int,
2853874aeea5SJeff Kirsher 	.poll		 = efx_port_dummy_op_poll,
2854874aeea5SJeff Kirsher 	.fini		 = efx_port_dummy_op_void,
2855874aeea5SJeff Kirsher };
2856874aeea5SJeff Kirsher 
2857874aeea5SJeff Kirsher /**************************************************************************
2858874aeea5SJeff Kirsher  *
2859874aeea5SJeff Kirsher  * Data housekeeping
2860874aeea5SJeff Kirsher  *
2861874aeea5SJeff Kirsher  **************************************************************************/
2862874aeea5SJeff Kirsher 
2863874aeea5SJeff Kirsher /* This zeroes out and then fills in the invariants in a struct
2864874aeea5SJeff Kirsher  * efx_nic (including all sub-structures).
2865874aeea5SJeff Kirsher  */
2866adeb15aaSBen Hutchings static int efx_init_struct(struct efx_nic *efx,
2867874aeea5SJeff Kirsher 			   struct pci_dev *pci_dev, struct net_device *net_dev)
2868874aeea5SJeff Kirsher {
2869874aeea5SJeff Kirsher 	int i;
2870874aeea5SJeff Kirsher 
2871874aeea5SJeff Kirsher 	/* Initialise common structures */
28720bcf4a64SBen Hutchings 	INIT_LIST_HEAD(&efx->node);
28730bcf4a64SBen Hutchings 	INIT_LIST_HEAD(&efx->secondary_list);
2874874aeea5SJeff Kirsher 	spin_lock_init(&efx->biu_lock);
2875874aeea5SJeff Kirsher #ifdef CONFIG_SFC_MTD
2876874aeea5SJeff Kirsher 	INIT_LIST_HEAD(&efx->mtd_list);
2877874aeea5SJeff Kirsher #endif
2878874aeea5SJeff Kirsher 	INIT_WORK(&efx->reset_work, efx_reset_work);
2879874aeea5SJeff Kirsher 	INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
2880dd40781eSBen Hutchings 	INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
2881874aeea5SJeff Kirsher 	efx->pci_dev = pci_dev;
2882874aeea5SJeff Kirsher 	efx->msg_enable = debug;
2883f16aeea0SBen Hutchings 	efx->state = STATE_UNINIT;
2884874aeea5SJeff Kirsher 	strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
2885874aeea5SJeff Kirsher 
2886874aeea5SJeff Kirsher 	efx->net_dev = net_dev;
288743a3739dSJon Cooper 	efx->rx_prefix_size = efx->type->rx_prefix_size;
28882ec03014SAndrew Rybchenko 	efx->rx_ip_align =
28892ec03014SAndrew Rybchenko 		NET_IP_ALIGN ? (efx->rx_prefix_size + NET_IP_ALIGN) % 4 : 0;
289043a3739dSJon Cooper 	efx->rx_packet_hash_offset =
289143a3739dSJon Cooper 		efx->type->rx_hash_offset - efx->type->rx_prefix_size;
2892bd9a265dSJon Cooper 	efx->rx_packet_ts_offset =
2893bd9a265dSJon Cooper 		efx->type->rx_ts_offset - efx->type->rx_prefix_size;
2894874aeea5SJeff Kirsher 	spin_lock_init(&efx->stats_lock);
2895874aeea5SJeff Kirsher 	mutex_init(&efx->mac_lock);
2896874aeea5SJeff Kirsher 	efx->phy_op = &efx_dummy_phy_operations;
2897874aeea5SJeff Kirsher 	efx->mdio.dev = net_dev;
2898874aeea5SJeff Kirsher 	INIT_WORK(&efx->mac_work, efx_mac_work);
28999f2cb71cSBen Hutchings 	init_waitqueue_head(&efx->flush_wq);
2900874aeea5SJeff Kirsher 
2901874aeea5SJeff Kirsher 	for (i = 0; i < EFX_MAX_CHANNELS; i++) {
2902874aeea5SJeff Kirsher 		efx->channel[i] = efx_alloc_channel(efx, i, NULL);
2903874aeea5SJeff Kirsher 		if (!efx->channel[i])
2904874aeea5SJeff Kirsher 			goto fail;
2905d8291187SBen Hutchings 		efx->msi_context[i].efx = efx;
2906d8291187SBen Hutchings 		efx->msi_context[i].index = i;
2907874aeea5SJeff Kirsher 	}
2908874aeea5SJeff Kirsher 
2909874aeea5SJeff Kirsher 	/* Higher numbered interrupt modes are less capable! */
2910874aeea5SJeff Kirsher 	efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2911874aeea5SJeff Kirsher 				  interrupt_mode);
2912874aeea5SJeff Kirsher 
2913874aeea5SJeff Kirsher 	/* Would be good to use the net_dev name, but we're too early */
2914874aeea5SJeff Kirsher 	snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2915874aeea5SJeff Kirsher 		 pci_name(pci_dev));
2916874aeea5SJeff Kirsher 	efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
2917874aeea5SJeff Kirsher 	if (!efx->workqueue)
2918874aeea5SJeff Kirsher 		goto fail;
2919874aeea5SJeff Kirsher 
2920874aeea5SJeff Kirsher 	return 0;
2921874aeea5SJeff Kirsher 
2922874aeea5SJeff Kirsher fail:
2923874aeea5SJeff Kirsher 	efx_fini_struct(efx);
2924874aeea5SJeff Kirsher 	return -ENOMEM;
2925874aeea5SJeff Kirsher }
2926874aeea5SJeff Kirsher 
2927874aeea5SJeff Kirsher static void efx_fini_struct(struct efx_nic *efx)
2928874aeea5SJeff Kirsher {
2929874aeea5SJeff Kirsher 	int i;
2930874aeea5SJeff Kirsher 
2931874aeea5SJeff Kirsher 	for (i = 0; i < EFX_MAX_CHANNELS; i++)
2932874aeea5SJeff Kirsher 		kfree(efx->channel[i]);
2933874aeea5SJeff Kirsher 
2934ef215e64SBen Hutchings 	kfree(efx->vpd_sn);
2935ef215e64SBen Hutchings 
2936874aeea5SJeff Kirsher 	if (efx->workqueue) {
2937874aeea5SJeff Kirsher 		destroy_workqueue(efx->workqueue);
2938874aeea5SJeff Kirsher 		efx->workqueue = NULL;
2939874aeea5SJeff Kirsher 	}
2940874aeea5SJeff Kirsher }
2941874aeea5SJeff Kirsher 
2942e4d112e4SEdward Cree void efx_update_sw_stats(struct efx_nic *efx, u64 *stats)
2943e4d112e4SEdward Cree {
2944e4d112e4SEdward Cree 	u64 n_rx_nodesc_trunc = 0;
2945e4d112e4SEdward Cree 	struct efx_channel *channel;
2946e4d112e4SEdward Cree 
2947e4d112e4SEdward Cree 	efx_for_each_channel(channel, efx)
2948e4d112e4SEdward Cree 		n_rx_nodesc_trunc += channel->n_rx_nodesc_trunc;
2949e4d112e4SEdward Cree 	stats[GENERIC_STAT_rx_nodesc_trunc] = n_rx_nodesc_trunc;
2950e4d112e4SEdward Cree 	stats[GENERIC_STAT_rx_noskb_drops] = atomic_read(&efx->n_rx_noskb_drops);
2951e4d112e4SEdward Cree }
2952e4d112e4SEdward Cree 
2953874aeea5SJeff Kirsher /**************************************************************************
2954874aeea5SJeff Kirsher  *
2955874aeea5SJeff Kirsher  * PCI interface
2956874aeea5SJeff Kirsher  *
2957874aeea5SJeff Kirsher  **************************************************************************/
2958874aeea5SJeff Kirsher 
2959874aeea5SJeff Kirsher /* Main body of final NIC shutdown code
2960874aeea5SJeff Kirsher  * This is called only at module unload (or hotplug removal).
2961874aeea5SJeff Kirsher  */
2962874aeea5SJeff Kirsher static void efx_pci_remove_main(struct efx_nic *efx)
2963874aeea5SJeff Kirsher {
29647153f623SBen Hutchings 	/* Flush reset_work. It can no longer be scheduled since we
29657153f623SBen Hutchings 	 * are not READY.
29667153f623SBen Hutchings 	 */
29677153f623SBen Hutchings 	BUG_ON(efx->state == STATE_READY);
29687153f623SBen Hutchings 	cancel_work_sync(&efx->reset_work);
29697153f623SBen Hutchings 
2970d8291187SBen Hutchings 	efx_disable_interrupts(efx);
2971874aeea5SJeff Kirsher 	efx_nic_fini_interrupt(efx);
2972874aeea5SJeff Kirsher 	efx_fini_port(efx);
2973874aeea5SJeff Kirsher 	efx->type->fini(efx);
2974874aeea5SJeff Kirsher 	efx_fini_napi(efx);
2975874aeea5SJeff Kirsher 	efx_remove_all(efx);
2976874aeea5SJeff Kirsher }
2977874aeea5SJeff Kirsher 
2978874aeea5SJeff Kirsher /* Final NIC shutdown
29792a3fc311SDaniel Pieczko  * This is called only at module unload (or hotplug removal).  A PF can call
29802a3fc311SDaniel Pieczko  * this on its VFs to ensure they are unbound first.
2981874aeea5SJeff Kirsher  */
2982874aeea5SJeff Kirsher static void efx_pci_remove(struct pci_dev *pci_dev)
2983874aeea5SJeff Kirsher {
2984874aeea5SJeff Kirsher 	struct efx_nic *efx;
2985874aeea5SJeff Kirsher 
2986874aeea5SJeff Kirsher 	efx = pci_get_drvdata(pci_dev);
2987874aeea5SJeff Kirsher 	if (!efx)
2988874aeea5SJeff Kirsher 		return;
2989874aeea5SJeff Kirsher 
2990874aeea5SJeff Kirsher 	/* Mark the NIC as fini, then stop the interface */
2991874aeea5SJeff Kirsher 	rtnl_lock();
29920bcf4a64SBen Hutchings 	efx_dissociate(efx);
2993874aeea5SJeff Kirsher 	dev_close(efx->net_dev);
2994d8291187SBen Hutchings 	efx_disable_interrupts(efx);
2995ea6bb99eSEdward Cree 	efx->state = STATE_UNINIT;
2996874aeea5SJeff Kirsher 	rtnl_unlock();
2997874aeea5SJeff Kirsher 
29987fa8d547SShradha Shah 	if (efx->type->sriov_fini)
2999d98a4ffeSShradha Shah 		efx->type->sriov_fini(efx);
30007fa8d547SShradha Shah 
3001874aeea5SJeff Kirsher 	efx_unregister_netdev(efx);
3002874aeea5SJeff Kirsher 
3003874aeea5SJeff Kirsher 	efx_mtd_remove(efx);
3004874aeea5SJeff Kirsher 
3005874aeea5SJeff Kirsher 	efx_pci_remove_main(efx);
3006874aeea5SJeff Kirsher 
3007874aeea5SJeff Kirsher 	efx_fini_io(efx);
3008874aeea5SJeff Kirsher 	netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
3009874aeea5SJeff Kirsher 
3010874aeea5SJeff Kirsher 	efx_fini_struct(efx);
3011874aeea5SJeff Kirsher 	free_netdev(efx->net_dev);
3012626950dbSAlexandre Rames 
3013626950dbSAlexandre Rames 	pci_disable_pcie_error_reporting(pci_dev);
3014874aeea5SJeff Kirsher };
3015874aeea5SJeff Kirsher 
3016460eeaa0SBen Hutchings /* NIC VPD information
3017460eeaa0SBen Hutchings  * Called during probe to display the part number of the
3018460eeaa0SBen Hutchings  * installed NIC.  VPD is potentially very large but this should
3019460eeaa0SBen Hutchings  * always appear within the first 512 bytes.
3020460eeaa0SBen Hutchings  */
3021460eeaa0SBen Hutchings #define SFC_VPD_LEN 512
3022ef215e64SBen Hutchings static void efx_probe_vpd_strings(struct efx_nic *efx)
3023460eeaa0SBen Hutchings {
3024460eeaa0SBen Hutchings 	struct pci_dev *dev = efx->pci_dev;
3025460eeaa0SBen Hutchings 	char vpd_data[SFC_VPD_LEN];
3026460eeaa0SBen Hutchings 	ssize_t vpd_size;
3027ef215e64SBen Hutchings 	int ro_start, ro_size, i, j;
3028460eeaa0SBen Hutchings 
3029460eeaa0SBen Hutchings 	/* Get the vpd data from the device */
3030460eeaa0SBen Hutchings 	vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
3031460eeaa0SBen Hutchings 	if (vpd_size <= 0) {
3032460eeaa0SBen Hutchings 		netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
3033460eeaa0SBen Hutchings 		return;
3034460eeaa0SBen Hutchings 	}
3035460eeaa0SBen Hutchings 
3036460eeaa0SBen Hutchings 	/* Get the Read only section */
3037ef215e64SBen Hutchings 	ro_start = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
3038ef215e64SBen Hutchings 	if (ro_start < 0) {
3039460eeaa0SBen Hutchings 		netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
3040460eeaa0SBen Hutchings 		return;
3041460eeaa0SBen Hutchings 	}
3042460eeaa0SBen Hutchings 
3043ef215e64SBen Hutchings 	ro_size = pci_vpd_lrdt_size(&vpd_data[ro_start]);
3044ef215e64SBen Hutchings 	j = ro_size;
3045ef215e64SBen Hutchings 	i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
3046460eeaa0SBen Hutchings 	if (i + j > vpd_size)
3047460eeaa0SBen Hutchings 		j = vpd_size - i;
3048460eeaa0SBen Hutchings 
3049460eeaa0SBen Hutchings 	/* Get the Part number */
3050460eeaa0SBen Hutchings 	i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
3051460eeaa0SBen Hutchings 	if (i < 0) {
3052460eeaa0SBen Hutchings 		netif_err(efx, drv, efx->net_dev, "Part number not found\n");
3053460eeaa0SBen Hutchings 		return;
3054460eeaa0SBen Hutchings 	}
3055460eeaa0SBen Hutchings 
3056460eeaa0SBen Hutchings 	j = pci_vpd_info_field_size(&vpd_data[i]);
3057460eeaa0SBen Hutchings 	i += PCI_VPD_INFO_FLD_HDR_SIZE;
3058460eeaa0SBen Hutchings 	if (i + j > vpd_size) {
3059460eeaa0SBen Hutchings 		netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
3060460eeaa0SBen Hutchings 		return;
3061460eeaa0SBen Hutchings 	}
3062460eeaa0SBen Hutchings 
3063460eeaa0SBen Hutchings 	netif_info(efx, drv, efx->net_dev,
3064460eeaa0SBen Hutchings 		   "Part Number : %.*s\n", j, &vpd_data[i]);
3065ef215e64SBen Hutchings 
3066ef215e64SBen Hutchings 	i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
3067ef215e64SBen Hutchings 	j = ro_size;
3068ef215e64SBen Hutchings 	i = pci_vpd_find_info_keyword(vpd_data, i, j, "SN");
3069ef215e64SBen Hutchings 	if (i < 0) {
3070ef215e64SBen Hutchings 		netif_err(efx, drv, efx->net_dev, "Serial number not found\n");
3071ef215e64SBen Hutchings 		return;
3072ef215e64SBen Hutchings 	}
3073ef215e64SBen Hutchings 
3074ef215e64SBen Hutchings 	j = pci_vpd_info_field_size(&vpd_data[i]);
3075ef215e64SBen Hutchings 	i += PCI_VPD_INFO_FLD_HDR_SIZE;
3076ef215e64SBen Hutchings 	if (i + j > vpd_size) {
3077ef215e64SBen Hutchings 		netif_err(efx, drv, efx->net_dev, "Incomplete serial number\n");
3078ef215e64SBen Hutchings 		return;
3079ef215e64SBen Hutchings 	}
3080ef215e64SBen Hutchings 
3081ef215e64SBen Hutchings 	efx->vpd_sn = kmalloc(j + 1, GFP_KERNEL);
3082ef215e64SBen Hutchings 	if (!efx->vpd_sn)
3083ef215e64SBen Hutchings 		return;
3084ef215e64SBen Hutchings 
3085ef215e64SBen Hutchings 	snprintf(efx->vpd_sn, j + 1, "%s", &vpd_data[i]);
3086460eeaa0SBen Hutchings }
3087460eeaa0SBen Hutchings 
3088460eeaa0SBen Hutchings 
3089874aeea5SJeff Kirsher /* Main body of NIC initialisation
3090874aeea5SJeff Kirsher  * This is called at module load (or hotplug insertion, theoretically).
3091874aeea5SJeff Kirsher  */
3092874aeea5SJeff Kirsher static int efx_pci_probe_main(struct efx_nic *efx)
3093874aeea5SJeff Kirsher {
3094874aeea5SJeff Kirsher 	int rc;
3095874aeea5SJeff Kirsher 
3096874aeea5SJeff Kirsher 	/* Do start-of-day initialisation */
3097874aeea5SJeff Kirsher 	rc = efx_probe_all(efx);
3098874aeea5SJeff Kirsher 	if (rc)
3099874aeea5SJeff Kirsher 		goto fail1;
3100874aeea5SJeff Kirsher 
3101874aeea5SJeff Kirsher 	efx_init_napi(efx);
3102874aeea5SJeff Kirsher 
3103874aeea5SJeff Kirsher 	rc = efx->type->init(efx);
3104874aeea5SJeff Kirsher 	if (rc) {
3105874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev,
3106874aeea5SJeff Kirsher 			  "failed to initialise NIC\n");
3107874aeea5SJeff Kirsher 		goto fail3;
3108874aeea5SJeff Kirsher 	}
3109874aeea5SJeff Kirsher 
3110874aeea5SJeff Kirsher 	rc = efx_init_port(efx);
3111874aeea5SJeff Kirsher 	if (rc) {
3112874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev,
3113874aeea5SJeff Kirsher 			  "failed to initialise port\n");
3114874aeea5SJeff Kirsher 		goto fail4;
3115874aeea5SJeff Kirsher 	}
3116874aeea5SJeff Kirsher 
3117874aeea5SJeff Kirsher 	rc = efx_nic_init_interrupt(efx);
3118874aeea5SJeff Kirsher 	if (rc)
3119874aeea5SJeff Kirsher 		goto fail5;
3120261e4d96SJon Cooper 	rc = efx_enable_interrupts(efx);
3121261e4d96SJon Cooper 	if (rc)
3122261e4d96SJon Cooper 		goto fail6;
3123874aeea5SJeff Kirsher 
3124874aeea5SJeff Kirsher 	return 0;
3125874aeea5SJeff Kirsher 
3126261e4d96SJon Cooper  fail6:
3127261e4d96SJon Cooper 	efx_nic_fini_interrupt(efx);
3128874aeea5SJeff Kirsher  fail5:
3129874aeea5SJeff Kirsher 	efx_fini_port(efx);
3130874aeea5SJeff Kirsher  fail4:
3131874aeea5SJeff Kirsher 	efx->type->fini(efx);
3132874aeea5SJeff Kirsher  fail3:
3133874aeea5SJeff Kirsher 	efx_fini_napi(efx);
3134874aeea5SJeff Kirsher 	efx_remove_all(efx);
3135874aeea5SJeff Kirsher  fail1:
3136874aeea5SJeff Kirsher 	return rc;
3137874aeea5SJeff Kirsher }
3138874aeea5SJeff Kirsher 
3139874aeea5SJeff Kirsher /* NIC initialisation
3140874aeea5SJeff Kirsher  *
3141874aeea5SJeff Kirsher  * This is called at module load (or hotplug insertion,
314273ba7b68SBen Hutchings  * theoretically).  It sets up PCI mappings, resets the NIC,
3143874aeea5SJeff Kirsher  * sets up and registers the network devices with the kernel and hooks
3144874aeea5SJeff Kirsher  * the interrupt service routine.  It does not prepare the device for
3145874aeea5SJeff Kirsher  * transmission; this is left to the first time one of the network
3146874aeea5SJeff Kirsher  * interfaces is brought up (i.e. efx_net_open).
3147874aeea5SJeff Kirsher  */
314887d1fc11SBill Pemberton static int efx_pci_probe(struct pci_dev *pci_dev,
3149874aeea5SJeff Kirsher 			 const struct pci_device_id *entry)
3150874aeea5SJeff Kirsher {
3151874aeea5SJeff Kirsher 	struct net_device *net_dev;
3152874aeea5SJeff Kirsher 	struct efx_nic *efx;
3153fadac6aaSBen Hutchings 	int rc;
3154874aeea5SJeff Kirsher 
3155874aeea5SJeff Kirsher 	/* Allocate and initialise a struct net_device and struct efx_nic */
3156874aeea5SJeff Kirsher 	net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
3157874aeea5SJeff Kirsher 				     EFX_MAX_RX_QUEUES);
3158874aeea5SJeff Kirsher 	if (!net_dev)
3159874aeea5SJeff Kirsher 		return -ENOMEM;
3160adeb15aaSBen Hutchings 	efx = netdev_priv(net_dev);
3161adeb15aaSBen Hutchings 	efx->type = (const struct efx_nic_type *) entry->driver_data;
3162ebfcd0fdSAndrew Rybchenko 	efx->fixed_features |= NETIF_F_HIGHDMA;
3163eb7cfd8cSAndrew Rybchenko 
3164874aeea5SJeff Kirsher 	pci_set_drvdata(pci_dev, efx);
3165874aeea5SJeff Kirsher 	SET_NETDEV_DEV(net_dev, &pci_dev->dev);
3166adeb15aaSBen Hutchings 	rc = efx_init_struct(efx, pci_dev, net_dev);
3167874aeea5SJeff Kirsher 	if (rc)
3168874aeea5SJeff Kirsher 		goto fail1;
3169874aeea5SJeff Kirsher 
3170874aeea5SJeff Kirsher 	netif_info(efx, probe, efx->net_dev,
3171874aeea5SJeff Kirsher 		   "Solarflare NIC detected\n");
3172874aeea5SJeff Kirsher 
31736f7f8aa6SShradha Shah 	if (!efx->type->is_vf)
3174ef215e64SBen Hutchings 		efx_probe_vpd_strings(efx);
3175460eeaa0SBen Hutchings 
3176874aeea5SJeff Kirsher 	/* Set up basic I/O (BAR mappings etc) */
3177874aeea5SJeff Kirsher 	rc = efx_init_io(efx);
3178874aeea5SJeff Kirsher 	if (rc)
3179874aeea5SJeff Kirsher 		goto fail2;
3180874aeea5SJeff Kirsher 
3181874aeea5SJeff Kirsher 	rc = efx_pci_probe_main(efx);
3182fadac6aaSBen Hutchings 	if (rc)
3183874aeea5SJeff Kirsher 		goto fail3;
3184874aeea5SJeff Kirsher 
318546d1efd8SEdward Cree 	net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
318646d1efd8SEdward Cree 			      NETIF_F_TSO | NETIF_F_RXCSUM);
318746d1efd8SEdward Cree 	if (efx->type->offload_features & (NETIF_F_IPV6_CSUM | NETIF_F_HW_CSUM))
318846d1efd8SEdward Cree 		net_dev->features |= NETIF_F_TSO6;
318946d1efd8SEdward Cree 	/* Check whether device supports TSO */
319046d1efd8SEdward Cree 	if (!efx->type->tso_versions || !efx->type->tso_versions(efx))
319146d1efd8SEdward Cree 		net_dev->features &= ~NETIF_F_ALL_TSO;
319246d1efd8SEdward Cree 	/* Mask for features that also apply to VLAN devices */
319346d1efd8SEdward Cree 	net_dev->vlan_features |= (NETIF_F_HW_CSUM | NETIF_F_SG |
319446d1efd8SEdward Cree 				   NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
319546d1efd8SEdward Cree 				   NETIF_F_RXCSUM);
319646d1efd8SEdward Cree 
319746d1efd8SEdward Cree 	net_dev->hw_features = net_dev->features & ~efx->fixed_features;
319846d1efd8SEdward Cree 
319946d1efd8SEdward Cree 	/* Disable VLAN filtering by default.  It may be enforced if
320046d1efd8SEdward Cree 	 * the feature is fixed (i.e. VLAN filters are required to
320146d1efd8SEdward Cree 	 * receive VLAN tagged packets due to vPort restrictions).
320246d1efd8SEdward Cree 	 */
320346d1efd8SEdward Cree 	net_dev->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
320446d1efd8SEdward Cree 	net_dev->features |= efx->fixed_features;
320546d1efd8SEdward Cree 
3206874aeea5SJeff Kirsher 	rc = efx_register_netdev(efx);
3207874aeea5SJeff Kirsher 	if (rc)
3208fadac6aaSBen Hutchings 		goto fail4;
3209874aeea5SJeff Kirsher 
32107fa8d547SShradha Shah 	if (efx->type->sriov_init) {
3211d98a4ffeSShradha Shah 		rc = efx->type->sriov_init(efx);
3212cd2d5b52SBen Hutchings 		if (rc)
3213cd2d5b52SBen Hutchings 			netif_err(efx, probe, efx->net_dev,
3214cd2d5b52SBen Hutchings 				  "SR-IOV can't be enabled rc %d\n", rc);
32157fa8d547SShradha Shah 	}
3216cd2d5b52SBen Hutchings 
3217874aeea5SJeff Kirsher 	netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
3218874aeea5SJeff Kirsher 
32197c43161cSBen Hutchings 	/* Try to create MTDs, but allow this to fail */
3220874aeea5SJeff Kirsher 	rtnl_lock();
32217c43161cSBen Hutchings 	rc = efx_mtd_probe(efx);
3222874aeea5SJeff Kirsher 	rtnl_unlock();
322309a04204SBert Kenward 	if (rc && rc != -EPERM)
32247c43161cSBen Hutchings 		netif_warn(efx, probe, efx->net_dev,
32257c43161cSBen Hutchings 			   "failed to create MTDs (%d)\n", rc);
32267c43161cSBen Hutchings 
3227626950dbSAlexandre Rames 	rc = pci_enable_pcie_error_reporting(pci_dev);
3228626950dbSAlexandre Rames 	if (rc && rc != -EINVAL)
322909a04204SBert Kenward 		netif_notice(efx, probe, efx->net_dev,
323009a04204SBert Kenward 			     "PCIE error reporting unavailable (%d).\n",
323109a04204SBert Kenward 			     rc);
3232626950dbSAlexandre Rames 
3233874aeea5SJeff Kirsher 	return 0;
3234874aeea5SJeff Kirsher 
3235874aeea5SJeff Kirsher  fail4:
3236fadac6aaSBen Hutchings 	efx_pci_remove_main(efx);
3237874aeea5SJeff Kirsher  fail3:
3238874aeea5SJeff Kirsher 	efx_fini_io(efx);
3239874aeea5SJeff Kirsher  fail2:
3240874aeea5SJeff Kirsher 	efx_fini_struct(efx);
3241874aeea5SJeff Kirsher  fail1:
3242874aeea5SJeff Kirsher 	WARN_ON(rc > 0);
3243874aeea5SJeff Kirsher 	netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
3244874aeea5SJeff Kirsher 	free_netdev(net_dev);
3245874aeea5SJeff Kirsher 	return rc;
3246874aeea5SJeff Kirsher }
3247874aeea5SJeff Kirsher 
3248834e23ddSShradha Shah /* efx_pci_sriov_configure returns the actual number of Virtual Functions
3249834e23ddSShradha Shah  * enabled on success
3250834e23ddSShradha Shah  */
3251834e23ddSShradha Shah #ifdef CONFIG_SFC_SRIOV
3252834e23ddSShradha Shah static int efx_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
3253834e23ddSShradha Shah {
3254834e23ddSShradha Shah 	int rc;
3255834e23ddSShradha Shah 	struct efx_nic *efx = pci_get_drvdata(dev);
3256834e23ddSShradha Shah 
3257834e23ddSShradha Shah 	if (efx->type->sriov_configure) {
3258834e23ddSShradha Shah 		rc = efx->type->sriov_configure(efx, num_vfs);
3259834e23ddSShradha Shah 		if (rc)
3260834e23ddSShradha Shah 			return rc;
3261834e23ddSShradha Shah 		else
3262834e23ddSShradha Shah 			return num_vfs;
3263834e23ddSShradha Shah 	} else
3264834e23ddSShradha Shah 		return -EOPNOTSUPP;
3265834e23ddSShradha Shah }
3266834e23ddSShradha Shah #endif
3267834e23ddSShradha Shah 
3268874aeea5SJeff Kirsher static int efx_pm_freeze(struct device *dev)
3269874aeea5SJeff Kirsher {
3270874aeea5SJeff Kirsher 	struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
3271874aeea5SJeff Kirsher 
327261da026dSBen Hutchings 	rtnl_lock();
327361da026dSBen Hutchings 
32746032fb56SBen Hutchings 	if (efx->state != STATE_DISABLED) {
3275f16aeea0SBen Hutchings 		efx->state = STATE_UNINIT;
3276874aeea5SJeff Kirsher 
3277c2f3b8e3SDaniel Pieczko 		efx_device_detach_sync(efx);
3278874aeea5SJeff Kirsher 
3279874aeea5SJeff Kirsher 		efx_stop_all(efx);
3280d8291187SBen Hutchings 		efx_disable_interrupts(efx);
32816032fb56SBen Hutchings 	}
3282874aeea5SJeff Kirsher 
328361da026dSBen Hutchings 	rtnl_unlock();
328461da026dSBen Hutchings 
3285874aeea5SJeff Kirsher 	return 0;
3286874aeea5SJeff Kirsher }
3287874aeea5SJeff Kirsher 
3288874aeea5SJeff Kirsher static int efx_pm_thaw(struct device *dev)
3289874aeea5SJeff Kirsher {
3290261e4d96SJon Cooper 	int rc;
3291874aeea5SJeff Kirsher 	struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
3292874aeea5SJeff Kirsher 
329361da026dSBen Hutchings 	rtnl_lock();
329461da026dSBen Hutchings 
32956032fb56SBen Hutchings 	if (efx->state != STATE_DISABLED) {
3296261e4d96SJon Cooper 		rc = efx_enable_interrupts(efx);
3297261e4d96SJon Cooper 		if (rc)
3298261e4d96SJon Cooper 			goto fail;
3299874aeea5SJeff Kirsher 
3300874aeea5SJeff Kirsher 		mutex_lock(&efx->mac_lock);
3301874aeea5SJeff Kirsher 		efx->phy_op->reconfigure(efx);
3302874aeea5SJeff Kirsher 		mutex_unlock(&efx->mac_lock);
3303874aeea5SJeff Kirsher 
3304874aeea5SJeff Kirsher 		efx_start_all(efx);
3305874aeea5SJeff Kirsher 
3306874aeea5SJeff Kirsher 		netif_device_attach(efx->net_dev);
3307874aeea5SJeff Kirsher 
3308f16aeea0SBen Hutchings 		efx->state = STATE_READY;
3309874aeea5SJeff Kirsher 
3310874aeea5SJeff Kirsher 		efx->type->resume_wol(efx);
33116032fb56SBen Hutchings 	}
3312874aeea5SJeff Kirsher 
331361da026dSBen Hutchings 	rtnl_unlock();
331461da026dSBen Hutchings 
3315874aeea5SJeff Kirsher 	/* Reschedule any quenched resets scheduled during efx_pm_freeze() */
3316874aeea5SJeff Kirsher 	queue_work(reset_workqueue, &efx->reset_work);
3317874aeea5SJeff Kirsher 
3318874aeea5SJeff Kirsher 	return 0;
3319261e4d96SJon Cooper 
3320261e4d96SJon Cooper fail:
3321261e4d96SJon Cooper 	rtnl_unlock();
3322261e4d96SJon Cooper 
3323261e4d96SJon Cooper 	return rc;
3324874aeea5SJeff Kirsher }
3325874aeea5SJeff Kirsher 
3326874aeea5SJeff Kirsher static int efx_pm_poweroff(struct device *dev)
3327874aeea5SJeff Kirsher {
3328874aeea5SJeff Kirsher 	struct pci_dev *pci_dev = to_pci_dev(dev);
3329874aeea5SJeff Kirsher 	struct efx_nic *efx = pci_get_drvdata(pci_dev);
3330874aeea5SJeff Kirsher 
3331874aeea5SJeff Kirsher 	efx->type->fini(efx);
3332874aeea5SJeff Kirsher 
3333874aeea5SJeff Kirsher 	efx->reset_pending = 0;
3334874aeea5SJeff Kirsher 
3335874aeea5SJeff Kirsher 	pci_save_state(pci_dev);
3336874aeea5SJeff Kirsher 	return pci_set_power_state(pci_dev, PCI_D3hot);
3337874aeea5SJeff Kirsher }
3338874aeea5SJeff Kirsher 
3339874aeea5SJeff Kirsher /* Used for both resume and restore */
3340874aeea5SJeff Kirsher static int efx_pm_resume(struct device *dev)
3341874aeea5SJeff Kirsher {
3342874aeea5SJeff Kirsher 	struct pci_dev *pci_dev = to_pci_dev(dev);
3343874aeea5SJeff Kirsher 	struct efx_nic *efx = pci_get_drvdata(pci_dev);
3344874aeea5SJeff Kirsher 	int rc;
3345874aeea5SJeff Kirsher 
3346874aeea5SJeff Kirsher 	rc = pci_set_power_state(pci_dev, PCI_D0);
3347874aeea5SJeff Kirsher 	if (rc)
3348874aeea5SJeff Kirsher 		return rc;
3349874aeea5SJeff Kirsher 	pci_restore_state(pci_dev);
3350874aeea5SJeff Kirsher 	rc = pci_enable_device(pci_dev);
3351874aeea5SJeff Kirsher 	if (rc)
3352874aeea5SJeff Kirsher 		return rc;
3353874aeea5SJeff Kirsher 	pci_set_master(efx->pci_dev);
3354874aeea5SJeff Kirsher 	rc = efx->type->reset(efx, RESET_TYPE_ALL);
3355874aeea5SJeff Kirsher 	if (rc)
3356874aeea5SJeff Kirsher 		return rc;
3357874aeea5SJeff Kirsher 	rc = efx->type->init(efx);
3358874aeea5SJeff Kirsher 	if (rc)
3359874aeea5SJeff Kirsher 		return rc;
3360261e4d96SJon Cooper 	rc = efx_pm_thaw(dev);
3361261e4d96SJon Cooper 	return rc;
3362874aeea5SJeff Kirsher }
3363874aeea5SJeff Kirsher 
3364874aeea5SJeff Kirsher static int efx_pm_suspend(struct device *dev)
3365874aeea5SJeff Kirsher {
3366874aeea5SJeff Kirsher 	int rc;
3367874aeea5SJeff Kirsher 
3368874aeea5SJeff Kirsher 	efx_pm_freeze(dev);
3369874aeea5SJeff Kirsher 	rc = efx_pm_poweroff(dev);
3370874aeea5SJeff Kirsher 	if (rc)
3371874aeea5SJeff Kirsher 		efx_pm_resume(dev);
3372874aeea5SJeff Kirsher 	return rc;
3373874aeea5SJeff Kirsher }
3374874aeea5SJeff Kirsher 
337518e83e4cSBen Hutchings static const struct dev_pm_ops efx_pm_ops = {
3376874aeea5SJeff Kirsher 	.suspend	= efx_pm_suspend,
3377874aeea5SJeff Kirsher 	.resume		= efx_pm_resume,
3378874aeea5SJeff Kirsher 	.freeze		= efx_pm_freeze,
3379874aeea5SJeff Kirsher 	.thaw		= efx_pm_thaw,
3380874aeea5SJeff Kirsher 	.poweroff	= efx_pm_poweroff,
3381874aeea5SJeff Kirsher 	.restore	= efx_pm_resume,
3382874aeea5SJeff Kirsher };
3383874aeea5SJeff Kirsher 
3384626950dbSAlexandre Rames /* A PCI error affecting this device was detected.
3385626950dbSAlexandre Rames  * At this point MMIO and DMA may be disabled.
3386626950dbSAlexandre Rames  * Stop the software path and request a slot reset.
3387626950dbSAlexandre Rames  */
3388debd0034Sstephen hemminger static pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
3389626950dbSAlexandre Rames 					      enum pci_channel_state state)
3390626950dbSAlexandre Rames {
3391626950dbSAlexandre Rames 	pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
3392626950dbSAlexandre Rames 	struct efx_nic *efx = pci_get_drvdata(pdev);
3393626950dbSAlexandre Rames 
3394626950dbSAlexandre Rames 	if (state == pci_channel_io_perm_failure)
3395626950dbSAlexandre Rames 		return PCI_ERS_RESULT_DISCONNECT;
3396626950dbSAlexandre Rames 
3397626950dbSAlexandre Rames 	rtnl_lock();
3398626950dbSAlexandre Rames 
3399626950dbSAlexandre Rames 	if (efx->state != STATE_DISABLED) {
3400626950dbSAlexandre Rames 		efx->state = STATE_RECOVERY;
3401626950dbSAlexandre Rames 		efx->reset_pending = 0;
3402626950dbSAlexandre Rames 
3403626950dbSAlexandre Rames 		efx_device_detach_sync(efx);
3404626950dbSAlexandre Rames 
3405626950dbSAlexandre Rames 		efx_stop_all(efx);
3406d8291187SBen Hutchings 		efx_disable_interrupts(efx);
3407626950dbSAlexandre Rames 
3408626950dbSAlexandre Rames 		status = PCI_ERS_RESULT_NEED_RESET;
3409626950dbSAlexandre Rames 	} else {
3410626950dbSAlexandre Rames 		/* If the interface is disabled we don't want to do anything
3411626950dbSAlexandre Rames 		 * with it.
3412626950dbSAlexandre Rames 		 */
3413626950dbSAlexandre Rames 		status = PCI_ERS_RESULT_RECOVERED;
3414626950dbSAlexandre Rames 	}
3415626950dbSAlexandre Rames 
3416626950dbSAlexandre Rames 	rtnl_unlock();
3417626950dbSAlexandre Rames 
3418626950dbSAlexandre Rames 	pci_disable_device(pdev);
3419626950dbSAlexandre Rames 
3420626950dbSAlexandre Rames 	return status;
3421626950dbSAlexandre Rames }
3422626950dbSAlexandre Rames 
3423dbedd44eSJoe Perches /* Fake a successful reset, which will be performed later in efx_io_resume. */
3424debd0034Sstephen hemminger static pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
3425626950dbSAlexandre Rames {
3426626950dbSAlexandre Rames 	struct efx_nic *efx = pci_get_drvdata(pdev);
3427626950dbSAlexandre Rames 	pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
3428626950dbSAlexandre Rames 	int rc;
3429626950dbSAlexandre Rames 
3430626950dbSAlexandre Rames 	if (pci_enable_device(pdev)) {
3431626950dbSAlexandre Rames 		netif_err(efx, hw, efx->net_dev,
3432626950dbSAlexandre Rames 			  "Cannot re-enable PCI device after reset.\n");
3433626950dbSAlexandre Rames 		status =  PCI_ERS_RESULT_DISCONNECT;
3434626950dbSAlexandre Rames 	}
3435626950dbSAlexandre Rames 
3436626950dbSAlexandre Rames 	rc = pci_cleanup_aer_uncorrect_error_status(pdev);
3437626950dbSAlexandre Rames 	if (rc) {
3438626950dbSAlexandre Rames 		netif_err(efx, hw, efx->net_dev,
3439626950dbSAlexandre Rames 		"pci_cleanup_aer_uncorrect_error_status failed (%d)\n", rc);
3440626950dbSAlexandre Rames 		/* Non-fatal error. Continue. */
3441626950dbSAlexandre Rames 	}
3442626950dbSAlexandre Rames 
3443626950dbSAlexandre Rames 	return status;
3444626950dbSAlexandre Rames }
3445626950dbSAlexandre Rames 
3446626950dbSAlexandre Rames /* Perform the actual reset and resume I/O operations. */
3447626950dbSAlexandre Rames static void efx_io_resume(struct pci_dev *pdev)
3448626950dbSAlexandre Rames {
3449626950dbSAlexandre Rames 	struct efx_nic *efx = pci_get_drvdata(pdev);
3450626950dbSAlexandre Rames 	int rc;
3451626950dbSAlexandre Rames 
3452626950dbSAlexandre Rames 	rtnl_lock();
3453626950dbSAlexandre Rames 
3454626950dbSAlexandre Rames 	if (efx->state == STATE_DISABLED)
3455626950dbSAlexandre Rames 		goto out;
3456626950dbSAlexandre Rames 
3457626950dbSAlexandre Rames 	rc = efx_reset(efx, RESET_TYPE_ALL);
3458626950dbSAlexandre Rames 	if (rc) {
3459626950dbSAlexandre Rames 		netif_err(efx, hw, efx->net_dev,
3460626950dbSAlexandre Rames 			  "efx_reset failed after PCI error (%d)\n", rc);
3461626950dbSAlexandre Rames 	} else {
3462626950dbSAlexandre Rames 		efx->state = STATE_READY;
3463626950dbSAlexandre Rames 		netif_dbg(efx, hw, efx->net_dev,
3464626950dbSAlexandre Rames 			  "Done resetting and resuming IO after PCI error.\n");
3465626950dbSAlexandre Rames 	}
3466626950dbSAlexandre Rames 
3467626950dbSAlexandre Rames out:
3468626950dbSAlexandre Rames 	rtnl_unlock();
3469626950dbSAlexandre Rames }
3470626950dbSAlexandre Rames 
3471626950dbSAlexandre Rames /* For simplicity and reliability, we always require a slot reset and try to
3472626950dbSAlexandre Rames  * reset the hardware when a pci error affecting the device is detected.
3473626950dbSAlexandre Rames  * We leave both the link_reset and mmio_enabled callback unimplemented:
3474626950dbSAlexandre Rames  * with our request for slot reset the mmio_enabled callback will never be
3475626950dbSAlexandre Rames  * called, and the link_reset callback is not used by AER or EEH mechanisms.
3476626950dbSAlexandre Rames  */
3477c300366bSJulia Lawall static const struct pci_error_handlers efx_err_handlers = {
3478626950dbSAlexandre Rames 	.error_detected = efx_io_error_detected,
3479626950dbSAlexandre Rames 	.slot_reset	= efx_io_slot_reset,
3480626950dbSAlexandre Rames 	.resume		= efx_io_resume,
3481626950dbSAlexandre Rames };
3482626950dbSAlexandre Rames 
3483874aeea5SJeff Kirsher static struct pci_driver efx_pci_driver = {
3484874aeea5SJeff Kirsher 	.name		= KBUILD_MODNAME,
3485874aeea5SJeff Kirsher 	.id_table	= efx_pci_table,
3486874aeea5SJeff Kirsher 	.probe		= efx_pci_probe,
3487874aeea5SJeff Kirsher 	.remove		= efx_pci_remove,
3488874aeea5SJeff Kirsher 	.driver.pm	= &efx_pm_ops,
3489626950dbSAlexandre Rames 	.err_handler	= &efx_err_handlers,
3490834e23ddSShradha Shah #ifdef CONFIG_SFC_SRIOV
3491834e23ddSShradha Shah 	.sriov_configure = efx_pci_sriov_configure,
3492834e23ddSShradha Shah #endif
3493874aeea5SJeff Kirsher };
3494874aeea5SJeff Kirsher 
3495874aeea5SJeff Kirsher /**************************************************************************
3496874aeea5SJeff Kirsher  *
3497874aeea5SJeff Kirsher  * Kernel module interface
3498874aeea5SJeff Kirsher  *
3499874aeea5SJeff Kirsher  *************************************************************************/
3500874aeea5SJeff Kirsher 
3501874aeea5SJeff Kirsher module_param(interrupt_mode, uint, 0444);
3502874aeea5SJeff Kirsher MODULE_PARM_DESC(interrupt_mode,
3503874aeea5SJeff Kirsher 		 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
3504874aeea5SJeff Kirsher 
3505874aeea5SJeff Kirsher static int __init efx_init_module(void)
3506874aeea5SJeff Kirsher {
3507874aeea5SJeff Kirsher 	int rc;
3508874aeea5SJeff Kirsher 
3509874aeea5SJeff Kirsher 	printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
3510874aeea5SJeff Kirsher 
3511874aeea5SJeff Kirsher 	rc = register_netdevice_notifier(&efx_netdev_notifier);
3512874aeea5SJeff Kirsher 	if (rc)
3513874aeea5SJeff Kirsher 		goto err_notifier;
3514874aeea5SJeff Kirsher 
35157fa8d547SShradha Shah #ifdef CONFIG_SFC_SRIOV
3516cd2d5b52SBen Hutchings 	rc = efx_init_sriov();
3517cd2d5b52SBen Hutchings 	if (rc)
3518cd2d5b52SBen Hutchings 		goto err_sriov;
35197fa8d547SShradha Shah #endif
3520cd2d5b52SBen Hutchings 
3521874aeea5SJeff Kirsher 	reset_workqueue = create_singlethread_workqueue("sfc_reset");
3522874aeea5SJeff Kirsher 	if (!reset_workqueue) {
3523874aeea5SJeff Kirsher 		rc = -ENOMEM;
3524874aeea5SJeff Kirsher 		goto err_reset;
3525874aeea5SJeff Kirsher 	}
3526874aeea5SJeff Kirsher 
3527874aeea5SJeff Kirsher 	rc = pci_register_driver(&efx_pci_driver);
3528874aeea5SJeff Kirsher 	if (rc < 0)
3529874aeea5SJeff Kirsher 		goto err_pci;
3530874aeea5SJeff Kirsher 
3531874aeea5SJeff Kirsher 	return 0;
3532874aeea5SJeff Kirsher 
3533874aeea5SJeff Kirsher  err_pci:
3534874aeea5SJeff Kirsher 	destroy_workqueue(reset_workqueue);
3535874aeea5SJeff Kirsher  err_reset:
35367fa8d547SShradha Shah #ifdef CONFIG_SFC_SRIOV
3537cd2d5b52SBen Hutchings 	efx_fini_sriov();
3538cd2d5b52SBen Hutchings  err_sriov:
35397fa8d547SShradha Shah #endif
3540874aeea5SJeff Kirsher 	unregister_netdevice_notifier(&efx_netdev_notifier);
3541874aeea5SJeff Kirsher  err_notifier:
3542874aeea5SJeff Kirsher 	return rc;
3543874aeea5SJeff Kirsher }
3544874aeea5SJeff Kirsher 
3545874aeea5SJeff Kirsher static void __exit efx_exit_module(void)
3546874aeea5SJeff Kirsher {
3547874aeea5SJeff Kirsher 	printk(KERN_INFO "Solarflare NET driver unloading\n");
3548874aeea5SJeff Kirsher 
3549874aeea5SJeff Kirsher 	pci_unregister_driver(&efx_pci_driver);
3550874aeea5SJeff Kirsher 	destroy_workqueue(reset_workqueue);
35517fa8d547SShradha Shah #ifdef CONFIG_SFC_SRIOV
3552cd2d5b52SBen Hutchings 	efx_fini_sriov();
35537fa8d547SShradha Shah #endif
3554874aeea5SJeff Kirsher 	unregister_netdevice_notifier(&efx_netdev_notifier);
3555874aeea5SJeff Kirsher 
3556874aeea5SJeff Kirsher }
3557874aeea5SJeff Kirsher 
3558874aeea5SJeff Kirsher module_init(efx_init_module);
3559874aeea5SJeff Kirsher module_exit(efx_exit_module);
3560874aeea5SJeff Kirsher 
3561874aeea5SJeff Kirsher MODULE_AUTHOR("Solarflare Communications and "
3562874aeea5SJeff Kirsher 	      "Michael Brown <mbrown@fensystems.co.uk>");
35636a350fdbSBen Hutchings MODULE_DESCRIPTION("Solarflare network driver");
3564874aeea5SJeff Kirsher MODULE_LICENSE("GPL");
3565874aeea5SJeff Kirsher MODULE_DEVICE_TABLE(pci, efx_pci_table);
356614077e9eSEdward Cree MODULE_VERSION(EFX_DRIVER_VERSION);
3567