xref: /openbmc/linux/drivers/net/ethernet/sfc/efx.c (revision 86ee5302)
1874aeea5SJeff Kirsher /****************************************************************************
2874aeea5SJeff Kirsher  * Driver for Solarflare Solarstorm network controllers and boards
3874aeea5SJeff Kirsher  * Copyright 2005-2006 Fen Systems Ltd.
4874aeea5SJeff Kirsher  * Copyright 2005-2011 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/crc32.h>
21874aeea5SJeff Kirsher #include <linux/ethtool.h>
22874aeea5SJeff Kirsher #include <linux/topology.h>
23874aeea5SJeff Kirsher #include <linux/gfp.h>
24874aeea5SJeff Kirsher #include <linux/cpu_rmap.h>
25874aeea5SJeff Kirsher #include "net_driver.h"
26874aeea5SJeff Kirsher #include "efx.h"
27874aeea5SJeff Kirsher #include "nic.h"
28874aeea5SJeff Kirsher 
29874aeea5SJeff Kirsher #include "mcdi.h"
30874aeea5SJeff Kirsher #include "workarounds.h"
31874aeea5SJeff Kirsher 
32874aeea5SJeff Kirsher /**************************************************************************
33874aeea5SJeff Kirsher  *
34874aeea5SJeff Kirsher  * Type name strings
35874aeea5SJeff Kirsher  *
36874aeea5SJeff Kirsher  **************************************************************************
37874aeea5SJeff Kirsher  */
38874aeea5SJeff Kirsher 
39874aeea5SJeff Kirsher /* Loopback mode names (see LOOPBACK_MODE()) */
40874aeea5SJeff Kirsher const unsigned int efx_loopback_mode_max = LOOPBACK_MAX;
4118e83e4cSBen Hutchings const char *const efx_loopback_mode_names[] = {
42874aeea5SJeff Kirsher 	[LOOPBACK_NONE]		= "NONE",
43874aeea5SJeff Kirsher 	[LOOPBACK_DATA]		= "DATAPATH",
44874aeea5SJeff Kirsher 	[LOOPBACK_GMAC]		= "GMAC",
45874aeea5SJeff Kirsher 	[LOOPBACK_XGMII]	= "XGMII",
46874aeea5SJeff Kirsher 	[LOOPBACK_XGXS]		= "XGXS",
47874aeea5SJeff Kirsher 	[LOOPBACK_XAUI]		= "XAUI",
48874aeea5SJeff Kirsher 	[LOOPBACK_GMII]		= "GMII",
49874aeea5SJeff Kirsher 	[LOOPBACK_SGMII]	= "SGMII",
50874aeea5SJeff Kirsher 	[LOOPBACK_XGBR]		= "XGBR",
51874aeea5SJeff Kirsher 	[LOOPBACK_XFI]		= "XFI",
52874aeea5SJeff Kirsher 	[LOOPBACK_XAUI_FAR]	= "XAUI_FAR",
53874aeea5SJeff Kirsher 	[LOOPBACK_GMII_FAR]	= "GMII_FAR",
54874aeea5SJeff Kirsher 	[LOOPBACK_SGMII_FAR]	= "SGMII_FAR",
55874aeea5SJeff Kirsher 	[LOOPBACK_XFI_FAR]	= "XFI_FAR",
56874aeea5SJeff Kirsher 	[LOOPBACK_GPHY]		= "GPHY",
57874aeea5SJeff Kirsher 	[LOOPBACK_PHYXS]	= "PHYXS",
58874aeea5SJeff Kirsher 	[LOOPBACK_PCS]		= "PCS",
59874aeea5SJeff Kirsher 	[LOOPBACK_PMAPMD]	= "PMA/PMD",
60874aeea5SJeff Kirsher 	[LOOPBACK_XPORT]	= "XPORT",
61874aeea5SJeff Kirsher 	[LOOPBACK_XGMII_WS]	= "XGMII_WS",
62874aeea5SJeff Kirsher 	[LOOPBACK_XAUI_WS]	= "XAUI_WS",
63874aeea5SJeff Kirsher 	[LOOPBACK_XAUI_WS_FAR]  = "XAUI_WS_FAR",
64874aeea5SJeff Kirsher 	[LOOPBACK_XAUI_WS_NEAR] = "XAUI_WS_NEAR",
65874aeea5SJeff Kirsher 	[LOOPBACK_GMII_WS]	= "GMII_WS",
66874aeea5SJeff Kirsher 	[LOOPBACK_XFI_WS]	= "XFI_WS",
67874aeea5SJeff Kirsher 	[LOOPBACK_XFI_WS_FAR]	= "XFI_WS_FAR",
68874aeea5SJeff Kirsher 	[LOOPBACK_PHYXS_WS]	= "PHYXS_WS",
69874aeea5SJeff Kirsher };
70874aeea5SJeff Kirsher 
71874aeea5SJeff Kirsher const unsigned int efx_reset_type_max = RESET_TYPE_MAX;
7218e83e4cSBen Hutchings const char *const efx_reset_type_names[] = {
73874aeea5SJeff Kirsher 	[RESET_TYPE_INVISIBLE]     = "INVISIBLE",
74874aeea5SJeff Kirsher 	[RESET_TYPE_ALL]           = "ALL",
75874aeea5SJeff Kirsher 	[RESET_TYPE_WORLD]         = "WORLD",
76874aeea5SJeff Kirsher 	[RESET_TYPE_DISABLE]       = "DISABLE",
77874aeea5SJeff Kirsher 	[RESET_TYPE_TX_WATCHDOG]   = "TX_WATCHDOG",
78874aeea5SJeff Kirsher 	[RESET_TYPE_INT_ERROR]     = "INT_ERROR",
79874aeea5SJeff Kirsher 	[RESET_TYPE_RX_RECOVERY]   = "RX_RECOVERY",
80874aeea5SJeff Kirsher 	[RESET_TYPE_RX_DESC_FETCH] = "RX_DESC_FETCH",
81874aeea5SJeff Kirsher 	[RESET_TYPE_TX_DESC_FETCH] = "TX_DESC_FETCH",
82874aeea5SJeff Kirsher 	[RESET_TYPE_TX_SKIP]       = "TX_SKIP",
83874aeea5SJeff Kirsher 	[RESET_TYPE_MC_FAILURE]    = "MC_FAILURE",
84874aeea5SJeff Kirsher };
85874aeea5SJeff Kirsher 
86874aeea5SJeff Kirsher #define EFX_MAX_MTU (9 * 1024)
87874aeea5SJeff Kirsher 
88874aeea5SJeff Kirsher /* Reset workqueue. If any NIC has a hardware failure then a reset will be
89874aeea5SJeff Kirsher  * queued onto this work queue. This is not a per-nic work queue, because
90874aeea5SJeff Kirsher  * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
91874aeea5SJeff Kirsher  */
92874aeea5SJeff Kirsher static struct workqueue_struct *reset_workqueue;
93874aeea5SJeff Kirsher 
94874aeea5SJeff Kirsher /**************************************************************************
95874aeea5SJeff Kirsher  *
96874aeea5SJeff Kirsher  * Configurable values
97874aeea5SJeff Kirsher  *
98874aeea5SJeff Kirsher  *************************************************************************/
99874aeea5SJeff Kirsher 
100874aeea5SJeff Kirsher /*
101874aeea5SJeff Kirsher  * Use separate channels for TX and RX events
102874aeea5SJeff Kirsher  *
103874aeea5SJeff Kirsher  * Set this to 1 to use separate channels for TX and RX. It allows us
104874aeea5SJeff Kirsher  * to control interrupt affinity separately for TX and RX.
105874aeea5SJeff Kirsher  *
106874aeea5SJeff Kirsher  * This is only used in MSI-X interrupt mode
107874aeea5SJeff Kirsher  */
108874aeea5SJeff Kirsher static unsigned int separate_tx_channels;
109874aeea5SJeff Kirsher module_param(separate_tx_channels, uint, 0444);
110874aeea5SJeff Kirsher MODULE_PARM_DESC(separate_tx_channels,
111874aeea5SJeff Kirsher 		 "Use separate channels for TX and RX");
112874aeea5SJeff Kirsher 
113874aeea5SJeff Kirsher /* This is the weight assigned to each of the (per-channel) virtual
114874aeea5SJeff Kirsher  * NAPI devices.
115874aeea5SJeff Kirsher  */
116874aeea5SJeff Kirsher static int napi_weight = 64;
117874aeea5SJeff Kirsher 
118874aeea5SJeff Kirsher /* This is the time (in jiffies) between invocations of the hardware
119874aeea5SJeff Kirsher  * monitor.  On Falcon-based NICs, this will:
120874aeea5SJeff Kirsher  * - Check the on-board hardware monitor;
121874aeea5SJeff Kirsher  * - Poll the link state and reconfigure the hardware as necessary.
122874aeea5SJeff Kirsher  */
123874aeea5SJeff Kirsher static unsigned int efx_monitor_interval = 1 * HZ;
124874aeea5SJeff Kirsher 
125874aeea5SJeff Kirsher /* Initial interrupt moderation settings.  They can be modified after
126874aeea5SJeff Kirsher  * module load with ethtool.
127874aeea5SJeff Kirsher  *
128874aeea5SJeff Kirsher  * The default for RX should strike a balance between increasing the
129874aeea5SJeff Kirsher  * round-trip latency and reducing overhead.
130874aeea5SJeff Kirsher  */
131874aeea5SJeff Kirsher static unsigned int rx_irq_mod_usec = 60;
132874aeea5SJeff Kirsher 
133874aeea5SJeff Kirsher /* Initial interrupt moderation settings.  They can be modified after
134874aeea5SJeff Kirsher  * module load with ethtool.
135874aeea5SJeff Kirsher  *
136874aeea5SJeff Kirsher  * This default is chosen to ensure that a 10G link does not go idle
137874aeea5SJeff Kirsher  * while a TX queue is stopped after it has become full.  A queue is
138874aeea5SJeff Kirsher  * restarted when it drops below half full.  The time this takes (assuming
139874aeea5SJeff Kirsher  * worst case 3 descriptors per packet and 1024 descriptors) is
140874aeea5SJeff Kirsher  *   512 / 3 * 1.2 = 205 usec.
141874aeea5SJeff Kirsher  */
142874aeea5SJeff Kirsher static unsigned int tx_irq_mod_usec = 150;
143874aeea5SJeff Kirsher 
144874aeea5SJeff Kirsher /* This is the first interrupt mode to try out of:
145874aeea5SJeff Kirsher  * 0 => MSI-X
146874aeea5SJeff Kirsher  * 1 => MSI
147874aeea5SJeff Kirsher  * 2 => legacy
148874aeea5SJeff Kirsher  */
149874aeea5SJeff Kirsher static unsigned int interrupt_mode;
150874aeea5SJeff Kirsher 
151874aeea5SJeff Kirsher /* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
152874aeea5SJeff Kirsher  * i.e. the number of CPUs among which we may distribute simultaneous
153874aeea5SJeff Kirsher  * interrupt handling.
154874aeea5SJeff Kirsher  *
155874aeea5SJeff Kirsher  * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
156cdb08f8fSBen Hutchings  * The default (0) means to assign an interrupt to each core.
157874aeea5SJeff Kirsher  */
158874aeea5SJeff Kirsher static unsigned int rss_cpus;
159874aeea5SJeff Kirsher module_param(rss_cpus, uint, 0444);
160874aeea5SJeff Kirsher MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
161874aeea5SJeff Kirsher 
162874aeea5SJeff Kirsher static int phy_flash_cfg;
163874aeea5SJeff Kirsher module_param(phy_flash_cfg, int, 0644);
164874aeea5SJeff Kirsher MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
165874aeea5SJeff Kirsher 
166874aeea5SJeff Kirsher static unsigned irq_adapt_low_thresh = 10000;
167874aeea5SJeff Kirsher module_param(irq_adapt_low_thresh, uint, 0644);
168874aeea5SJeff Kirsher MODULE_PARM_DESC(irq_adapt_low_thresh,
169874aeea5SJeff Kirsher 		 "Threshold score for reducing IRQ moderation");
170874aeea5SJeff Kirsher 
171874aeea5SJeff Kirsher static unsigned irq_adapt_high_thresh = 20000;
172874aeea5SJeff Kirsher module_param(irq_adapt_high_thresh, uint, 0644);
173874aeea5SJeff Kirsher MODULE_PARM_DESC(irq_adapt_high_thresh,
174874aeea5SJeff Kirsher 		 "Threshold score for increasing IRQ moderation");
175874aeea5SJeff Kirsher 
176874aeea5SJeff Kirsher static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
177874aeea5SJeff Kirsher 			 NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
178874aeea5SJeff Kirsher 			 NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
179874aeea5SJeff Kirsher 			 NETIF_MSG_TX_ERR | NETIF_MSG_HW);
180874aeea5SJeff Kirsher module_param(debug, uint, 0);
181874aeea5SJeff Kirsher MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
182874aeea5SJeff Kirsher 
183874aeea5SJeff Kirsher /**************************************************************************
184874aeea5SJeff Kirsher  *
185874aeea5SJeff Kirsher  * Utility functions and prototypes
186874aeea5SJeff Kirsher  *
187874aeea5SJeff Kirsher  *************************************************************************/
188874aeea5SJeff Kirsher 
189874aeea5SJeff Kirsher static void efx_remove_channels(struct efx_nic *efx);
190874aeea5SJeff Kirsher static void efx_remove_port(struct efx_nic *efx);
191874aeea5SJeff Kirsher static void efx_init_napi(struct efx_nic *efx);
192874aeea5SJeff Kirsher static void efx_fini_napi(struct efx_nic *efx);
193874aeea5SJeff Kirsher static void efx_fini_napi_channel(struct efx_channel *channel);
194874aeea5SJeff Kirsher static void efx_fini_struct(struct efx_nic *efx);
195874aeea5SJeff Kirsher static void efx_start_all(struct efx_nic *efx);
196874aeea5SJeff Kirsher static void efx_stop_all(struct efx_nic *efx);
197874aeea5SJeff Kirsher 
198874aeea5SJeff Kirsher #define EFX_ASSERT_RESET_SERIALISED(efx)		\
199874aeea5SJeff Kirsher 	do {						\
200874aeea5SJeff Kirsher 		if ((efx->state == STATE_RUNNING) ||	\
201874aeea5SJeff Kirsher 		    (efx->state == STATE_DISABLED))	\
202874aeea5SJeff Kirsher 			ASSERT_RTNL();			\
203874aeea5SJeff Kirsher 	} while (0)
204874aeea5SJeff Kirsher 
205874aeea5SJeff Kirsher /**************************************************************************
206874aeea5SJeff Kirsher  *
207874aeea5SJeff Kirsher  * Event queue processing
208874aeea5SJeff Kirsher  *
209874aeea5SJeff Kirsher  *************************************************************************/
210874aeea5SJeff Kirsher 
211874aeea5SJeff Kirsher /* Process channel's event queue
212874aeea5SJeff Kirsher  *
213874aeea5SJeff Kirsher  * This function is responsible for processing the event queue of a
214874aeea5SJeff Kirsher  * single channel.  The caller must guarantee that this function will
215874aeea5SJeff Kirsher  * never be concurrently called more than once on the same channel,
216874aeea5SJeff Kirsher  * though different channels may be being processed concurrently.
217874aeea5SJeff Kirsher  */
218874aeea5SJeff Kirsher static int efx_process_channel(struct efx_channel *channel, int budget)
219874aeea5SJeff Kirsher {
220874aeea5SJeff Kirsher 	struct efx_nic *efx = channel->efx;
221874aeea5SJeff Kirsher 	int spent;
222874aeea5SJeff Kirsher 
223874aeea5SJeff Kirsher 	if (unlikely(efx->reset_pending || !channel->enabled))
224874aeea5SJeff Kirsher 		return 0;
225874aeea5SJeff Kirsher 
226874aeea5SJeff Kirsher 	spent = efx_nic_process_eventq(channel, budget);
227874aeea5SJeff Kirsher 	if (spent == 0)
228874aeea5SJeff Kirsher 		return 0;
229874aeea5SJeff Kirsher 
230874aeea5SJeff Kirsher 	/* Deliver last RX packet. */
231874aeea5SJeff Kirsher 	if (channel->rx_pkt) {
232874aeea5SJeff Kirsher 		__efx_rx_packet(channel, channel->rx_pkt,
233874aeea5SJeff Kirsher 				channel->rx_pkt_csummed);
234874aeea5SJeff Kirsher 		channel->rx_pkt = NULL;
235874aeea5SJeff Kirsher 	}
236874aeea5SJeff Kirsher 
237874aeea5SJeff Kirsher 	efx_rx_strategy(channel);
238874aeea5SJeff Kirsher 
239874aeea5SJeff Kirsher 	efx_fast_push_rx_descriptors(efx_channel_get_rx_queue(channel));
240874aeea5SJeff Kirsher 
241874aeea5SJeff Kirsher 	return spent;
242874aeea5SJeff Kirsher }
243874aeea5SJeff Kirsher 
244874aeea5SJeff Kirsher /* Mark channel as finished processing
245874aeea5SJeff Kirsher  *
246874aeea5SJeff Kirsher  * Note that since we will not receive further interrupts for this
247874aeea5SJeff Kirsher  * channel before we finish processing and call the eventq_read_ack()
248874aeea5SJeff Kirsher  * method, there is no need to use the interrupt hold-off timers.
249874aeea5SJeff Kirsher  */
250874aeea5SJeff Kirsher static inline void efx_channel_processed(struct efx_channel *channel)
251874aeea5SJeff Kirsher {
252874aeea5SJeff Kirsher 	/* The interrupt handler for this channel may set work_pending
253874aeea5SJeff Kirsher 	 * as soon as we acknowledge the events we've seen.  Make sure
254874aeea5SJeff Kirsher 	 * it's cleared before then. */
255874aeea5SJeff Kirsher 	channel->work_pending = false;
256874aeea5SJeff Kirsher 	smp_wmb();
257874aeea5SJeff Kirsher 
258874aeea5SJeff Kirsher 	efx_nic_eventq_read_ack(channel);
259874aeea5SJeff Kirsher }
260874aeea5SJeff Kirsher 
261874aeea5SJeff Kirsher /* NAPI poll handler
262874aeea5SJeff Kirsher  *
263874aeea5SJeff Kirsher  * NAPI guarantees serialisation of polls of the same device, which
264874aeea5SJeff Kirsher  * provides the guarantee required by efx_process_channel().
265874aeea5SJeff Kirsher  */
266874aeea5SJeff Kirsher static int efx_poll(struct napi_struct *napi, int budget)
267874aeea5SJeff Kirsher {
268874aeea5SJeff Kirsher 	struct efx_channel *channel =
269874aeea5SJeff Kirsher 		container_of(napi, struct efx_channel, napi_str);
270874aeea5SJeff Kirsher 	struct efx_nic *efx = channel->efx;
271874aeea5SJeff Kirsher 	int spent;
272874aeea5SJeff Kirsher 
273874aeea5SJeff Kirsher 	netif_vdbg(efx, intr, efx->net_dev,
274874aeea5SJeff Kirsher 		   "channel %d NAPI poll executing on CPU %d\n",
275874aeea5SJeff Kirsher 		   channel->channel, raw_smp_processor_id());
276874aeea5SJeff Kirsher 
277874aeea5SJeff Kirsher 	spent = efx_process_channel(channel, budget);
278874aeea5SJeff Kirsher 
279874aeea5SJeff Kirsher 	if (spent < budget) {
280874aeea5SJeff Kirsher 		if (channel->channel < efx->n_rx_channels &&
281874aeea5SJeff Kirsher 		    efx->irq_rx_adaptive &&
282874aeea5SJeff Kirsher 		    unlikely(++channel->irq_count == 1000)) {
283874aeea5SJeff Kirsher 			if (unlikely(channel->irq_mod_score <
284874aeea5SJeff Kirsher 				     irq_adapt_low_thresh)) {
285874aeea5SJeff Kirsher 				if (channel->irq_moderation > 1) {
286874aeea5SJeff Kirsher 					channel->irq_moderation -= 1;
287874aeea5SJeff Kirsher 					efx->type->push_irq_moderation(channel);
288874aeea5SJeff Kirsher 				}
289874aeea5SJeff Kirsher 			} else if (unlikely(channel->irq_mod_score >
290874aeea5SJeff Kirsher 					    irq_adapt_high_thresh)) {
291874aeea5SJeff Kirsher 				if (channel->irq_moderation <
292874aeea5SJeff Kirsher 				    efx->irq_rx_moderation) {
293874aeea5SJeff Kirsher 					channel->irq_moderation += 1;
294874aeea5SJeff Kirsher 					efx->type->push_irq_moderation(channel);
295874aeea5SJeff Kirsher 				}
296874aeea5SJeff Kirsher 			}
297874aeea5SJeff Kirsher 			channel->irq_count = 0;
298874aeea5SJeff Kirsher 			channel->irq_mod_score = 0;
299874aeea5SJeff Kirsher 		}
300874aeea5SJeff Kirsher 
301874aeea5SJeff Kirsher 		efx_filter_rfs_expire(channel);
302874aeea5SJeff Kirsher 
303874aeea5SJeff Kirsher 		/* There is no race here; although napi_disable() will
304874aeea5SJeff Kirsher 		 * only wait for napi_complete(), this isn't a problem
305874aeea5SJeff Kirsher 		 * since efx_channel_processed() will have no effect if
306874aeea5SJeff Kirsher 		 * interrupts have already been disabled.
307874aeea5SJeff Kirsher 		 */
308874aeea5SJeff Kirsher 		napi_complete(napi);
309874aeea5SJeff Kirsher 		efx_channel_processed(channel);
310874aeea5SJeff Kirsher 	}
311874aeea5SJeff Kirsher 
312874aeea5SJeff Kirsher 	return spent;
313874aeea5SJeff Kirsher }
314874aeea5SJeff Kirsher 
315874aeea5SJeff Kirsher /* Process the eventq of the specified channel immediately on this CPU
316874aeea5SJeff Kirsher  *
317874aeea5SJeff Kirsher  * Disable hardware generated interrupts, wait for any existing
318874aeea5SJeff Kirsher  * processing to finish, then directly poll (and ack ) the eventq.
319874aeea5SJeff Kirsher  * Finally reenable NAPI and interrupts.
320874aeea5SJeff Kirsher  *
321874aeea5SJeff Kirsher  * This is for use only during a loopback self-test.  It must not
322874aeea5SJeff Kirsher  * deliver any packets up the stack as this can result in deadlock.
323874aeea5SJeff Kirsher  */
324874aeea5SJeff Kirsher void efx_process_channel_now(struct efx_channel *channel)
325874aeea5SJeff Kirsher {
326874aeea5SJeff Kirsher 	struct efx_nic *efx = channel->efx;
327874aeea5SJeff Kirsher 
328874aeea5SJeff Kirsher 	BUG_ON(channel->channel >= efx->n_channels);
329874aeea5SJeff Kirsher 	BUG_ON(!channel->enabled);
330874aeea5SJeff Kirsher 	BUG_ON(!efx->loopback_selftest);
331874aeea5SJeff Kirsher 
332874aeea5SJeff Kirsher 	/* Disable interrupts and wait for ISRs to complete */
333874aeea5SJeff Kirsher 	efx_nic_disable_interrupts(efx);
334874aeea5SJeff Kirsher 	if (efx->legacy_irq) {
335874aeea5SJeff Kirsher 		synchronize_irq(efx->legacy_irq);
336874aeea5SJeff Kirsher 		efx->legacy_irq_enabled = false;
337874aeea5SJeff Kirsher 	}
338874aeea5SJeff Kirsher 	if (channel->irq)
339874aeea5SJeff Kirsher 		synchronize_irq(channel->irq);
340874aeea5SJeff Kirsher 
341874aeea5SJeff Kirsher 	/* Wait for any NAPI processing to complete */
342874aeea5SJeff Kirsher 	napi_disable(&channel->napi_str);
343874aeea5SJeff Kirsher 
344874aeea5SJeff Kirsher 	/* Poll the channel */
345874aeea5SJeff Kirsher 	efx_process_channel(channel, channel->eventq_mask + 1);
346874aeea5SJeff Kirsher 
347874aeea5SJeff Kirsher 	/* Ack the eventq. This may cause an interrupt to be generated
348874aeea5SJeff Kirsher 	 * when they are reenabled */
349874aeea5SJeff Kirsher 	efx_channel_processed(channel);
350874aeea5SJeff Kirsher 
351874aeea5SJeff Kirsher 	napi_enable(&channel->napi_str);
352874aeea5SJeff Kirsher 	if (efx->legacy_irq)
353874aeea5SJeff Kirsher 		efx->legacy_irq_enabled = true;
354874aeea5SJeff Kirsher 	efx_nic_enable_interrupts(efx);
355874aeea5SJeff Kirsher }
356874aeea5SJeff Kirsher 
357874aeea5SJeff Kirsher /* Create event queue
358874aeea5SJeff Kirsher  * Event queue memory allocations are done only once.  If the channel
359874aeea5SJeff Kirsher  * is reset, the memory buffer will be reused; this guards against
360874aeea5SJeff Kirsher  * errors during channel reset and also simplifies interrupt handling.
361874aeea5SJeff Kirsher  */
362874aeea5SJeff Kirsher static int efx_probe_eventq(struct efx_channel *channel)
363874aeea5SJeff Kirsher {
364874aeea5SJeff Kirsher 	struct efx_nic *efx = channel->efx;
365874aeea5SJeff Kirsher 	unsigned long entries;
366874aeea5SJeff Kirsher 
36786ee5302SBen Hutchings 	netif_dbg(efx, probe, efx->net_dev,
368874aeea5SJeff Kirsher 		  "chan %d create event queue\n", channel->channel);
369874aeea5SJeff Kirsher 
370874aeea5SJeff Kirsher 	/* Build an event queue with room for one event per tx and rx buffer,
371874aeea5SJeff Kirsher 	 * plus some extra for link state events and MCDI completions. */
372874aeea5SJeff Kirsher 	entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
373874aeea5SJeff Kirsher 	EFX_BUG_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
374874aeea5SJeff Kirsher 	channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
375874aeea5SJeff Kirsher 
376874aeea5SJeff Kirsher 	return efx_nic_probe_eventq(channel);
377874aeea5SJeff Kirsher }
378874aeea5SJeff Kirsher 
379874aeea5SJeff Kirsher /* Prepare channel's event queue */
380874aeea5SJeff Kirsher static void efx_init_eventq(struct efx_channel *channel)
381874aeea5SJeff Kirsher {
382874aeea5SJeff Kirsher 	netif_dbg(channel->efx, drv, channel->efx->net_dev,
383874aeea5SJeff Kirsher 		  "chan %d init event queue\n", channel->channel);
384874aeea5SJeff Kirsher 
385874aeea5SJeff Kirsher 	channel->eventq_read_ptr = 0;
386874aeea5SJeff Kirsher 
387874aeea5SJeff Kirsher 	efx_nic_init_eventq(channel);
388874aeea5SJeff Kirsher }
389874aeea5SJeff Kirsher 
390874aeea5SJeff Kirsher static void efx_fini_eventq(struct efx_channel *channel)
391874aeea5SJeff Kirsher {
392874aeea5SJeff Kirsher 	netif_dbg(channel->efx, drv, channel->efx->net_dev,
393874aeea5SJeff Kirsher 		  "chan %d fini event queue\n", channel->channel);
394874aeea5SJeff Kirsher 
395874aeea5SJeff Kirsher 	efx_nic_fini_eventq(channel);
396874aeea5SJeff Kirsher }
397874aeea5SJeff Kirsher 
398874aeea5SJeff Kirsher static void efx_remove_eventq(struct efx_channel *channel)
399874aeea5SJeff Kirsher {
400874aeea5SJeff Kirsher 	netif_dbg(channel->efx, drv, channel->efx->net_dev,
401874aeea5SJeff Kirsher 		  "chan %d remove event queue\n", channel->channel);
402874aeea5SJeff Kirsher 
403874aeea5SJeff Kirsher 	efx_nic_remove_eventq(channel);
404874aeea5SJeff Kirsher }
405874aeea5SJeff Kirsher 
406874aeea5SJeff Kirsher /**************************************************************************
407874aeea5SJeff Kirsher  *
408874aeea5SJeff Kirsher  * Channel handling
409874aeea5SJeff Kirsher  *
410874aeea5SJeff Kirsher  *************************************************************************/
411874aeea5SJeff Kirsher 
412874aeea5SJeff Kirsher /* Allocate and initialise a channel structure, optionally copying
413874aeea5SJeff Kirsher  * parameters (but not resources) from an old channel structure. */
414874aeea5SJeff Kirsher static struct efx_channel *
415874aeea5SJeff Kirsher efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
416874aeea5SJeff Kirsher {
417874aeea5SJeff Kirsher 	struct efx_channel *channel;
418874aeea5SJeff Kirsher 	struct efx_rx_queue *rx_queue;
419874aeea5SJeff Kirsher 	struct efx_tx_queue *tx_queue;
420874aeea5SJeff Kirsher 	int j;
421874aeea5SJeff Kirsher 
422874aeea5SJeff Kirsher 	if (old_channel) {
423874aeea5SJeff Kirsher 		channel = kmalloc(sizeof(*channel), GFP_KERNEL);
424874aeea5SJeff Kirsher 		if (!channel)
425874aeea5SJeff Kirsher 			return NULL;
426874aeea5SJeff Kirsher 
427874aeea5SJeff Kirsher 		*channel = *old_channel;
428874aeea5SJeff Kirsher 
429874aeea5SJeff Kirsher 		channel->napi_dev = NULL;
430874aeea5SJeff Kirsher 		memset(&channel->eventq, 0, sizeof(channel->eventq));
431874aeea5SJeff Kirsher 
432874aeea5SJeff Kirsher 		rx_queue = &channel->rx_queue;
433874aeea5SJeff Kirsher 		rx_queue->buffer = NULL;
434874aeea5SJeff Kirsher 		memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
435874aeea5SJeff Kirsher 
436874aeea5SJeff Kirsher 		for (j = 0; j < EFX_TXQ_TYPES; j++) {
437874aeea5SJeff Kirsher 			tx_queue = &channel->tx_queue[j];
438874aeea5SJeff Kirsher 			if (tx_queue->channel)
439874aeea5SJeff Kirsher 				tx_queue->channel = channel;
440874aeea5SJeff Kirsher 			tx_queue->buffer = NULL;
441874aeea5SJeff Kirsher 			memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
442874aeea5SJeff Kirsher 		}
443874aeea5SJeff Kirsher 	} else {
444874aeea5SJeff Kirsher 		channel = kzalloc(sizeof(*channel), GFP_KERNEL);
445874aeea5SJeff Kirsher 		if (!channel)
446874aeea5SJeff Kirsher 			return NULL;
447874aeea5SJeff Kirsher 
448874aeea5SJeff Kirsher 		channel->efx = efx;
449874aeea5SJeff Kirsher 		channel->channel = i;
450874aeea5SJeff Kirsher 
451874aeea5SJeff Kirsher 		for (j = 0; j < EFX_TXQ_TYPES; j++) {
452874aeea5SJeff Kirsher 			tx_queue = &channel->tx_queue[j];
453874aeea5SJeff Kirsher 			tx_queue->efx = efx;
454874aeea5SJeff Kirsher 			tx_queue->queue = i * EFX_TXQ_TYPES + j;
455874aeea5SJeff Kirsher 			tx_queue->channel = channel;
456874aeea5SJeff Kirsher 		}
457874aeea5SJeff Kirsher 	}
458874aeea5SJeff Kirsher 
459874aeea5SJeff Kirsher 	rx_queue = &channel->rx_queue;
460874aeea5SJeff Kirsher 	rx_queue->efx = efx;
461874aeea5SJeff Kirsher 	setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
462874aeea5SJeff Kirsher 		    (unsigned long)rx_queue);
463874aeea5SJeff Kirsher 
464874aeea5SJeff Kirsher 	return channel;
465874aeea5SJeff Kirsher }
466874aeea5SJeff Kirsher 
467874aeea5SJeff Kirsher static int efx_probe_channel(struct efx_channel *channel)
468874aeea5SJeff Kirsher {
469874aeea5SJeff Kirsher 	struct efx_tx_queue *tx_queue;
470874aeea5SJeff Kirsher 	struct efx_rx_queue *rx_queue;
471874aeea5SJeff Kirsher 	int rc;
472874aeea5SJeff Kirsher 
473874aeea5SJeff Kirsher 	netif_dbg(channel->efx, probe, channel->efx->net_dev,
474874aeea5SJeff Kirsher 		  "creating channel %d\n", channel->channel);
475874aeea5SJeff Kirsher 
476874aeea5SJeff Kirsher 	rc = efx_probe_eventq(channel);
477874aeea5SJeff Kirsher 	if (rc)
478874aeea5SJeff Kirsher 		goto fail1;
479874aeea5SJeff Kirsher 
480874aeea5SJeff Kirsher 	efx_for_each_channel_tx_queue(tx_queue, channel) {
481874aeea5SJeff Kirsher 		rc = efx_probe_tx_queue(tx_queue);
482874aeea5SJeff Kirsher 		if (rc)
483874aeea5SJeff Kirsher 			goto fail2;
484874aeea5SJeff Kirsher 	}
485874aeea5SJeff Kirsher 
486874aeea5SJeff Kirsher 	efx_for_each_channel_rx_queue(rx_queue, channel) {
487874aeea5SJeff Kirsher 		rc = efx_probe_rx_queue(rx_queue);
488874aeea5SJeff Kirsher 		if (rc)
489874aeea5SJeff Kirsher 			goto fail3;
490874aeea5SJeff Kirsher 	}
491874aeea5SJeff Kirsher 
492874aeea5SJeff Kirsher 	channel->n_rx_frm_trunc = 0;
493874aeea5SJeff Kirsher 
494874aeea5SJeff Kirsher 	return 0;
495874aeea5SJeff Kirsher 
496874aeea5SJeff Kirsher  fail3:
497874aeea5SJeff Kirsher 	efx_for_each_channel_rx_queue(rx_queue, channel)
498874aeea5SJeff Kirsher 		efx_remove_rx_queue(rx_queue);
499874aeea5SJeff Kirsher  fail2:
500874aeea5SJeff Kirsher 	efx_for_each_channel_tx_queue(tx_queue, channel)
501874aeea5SJeff Kirsher 		efx_remove_tx_queue(tx_queue);
502874aeea5SJeff Kirsher  fail1:
503874aeea5SJeff Kirsher 	return rc;
504874aeea5SJeff Kirsher }
505874aeea5SJeff Kirsher 
506874aeea5SJeff Kirsher 
507874aeea5SJeff Kirsher static void efx_set_channel_names(struct efx_nic *efx)
508874aeea5SJeff Kirsher {
509874aeea5SJeff Kirsher 	struct efx_channel *channel;
510874aeea5SJeff Kirsher 	const char *type = "";
511874aeea5SJeff Kirsher 	int number;
512874aeea5SJeff Kirsher 
513874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx) {
514874aeea5SJeff Kirsher 		number = channel->channel;
515874aeea5SJeff Kirsher 		if (efx->n_channels > efx->n_rx_channels) {
516874aeea5SJeff Kirsher 			if (channel->channel < efx->n_rx_channels) {
517874aeea5SJeff Kirsher 				type = "-rx";
518874aeea5SJeff Kirsher 			} else {
519874aeea5SJeff Kirsher 				type = "-tx";
520874aeea5SJeff Kirsher 				number -= efx->n_rx_channels;
521874aeea5SJeff Kirsher 			}
522874aeea5SJeff Kirsher 		}
523874aeea5SJeff Kirsher 		snprintf(efx->channel_name[channel->channel],
524874aeea5SJeff Kirsher 			 sizeof(efx->channel_name[0]),
525874aeea5SJeff Kirsher 			 "%s%s-%d", efx->name, type, number);
526874aeea5SJeff Kirsher 	}
527874aeea5SJeff Kirsher }
528874aeea5SJeff Kirsher 
529874aeea5SJeff Kirsher static int efx_probe_channels(struct efx_nic *efx)
530874aeea5SJeff Kirsher {
531874aeea5SJeff Kirsher 	struct efx_channel *channel;
532874aeea5SJeff Kirsher 	int rc;
533874aeea5SJeff Kirsher 
534874aeea5SJeff Kirsher 	/* Restart special buffer allocation */
535874aeea5SJeff Kirsher 	efx->next_buffer_table = 0;
536874aeea5SJeff Kirsher 
537874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx) {
538874aeea5SJeff Kirsher 		rc = efx_probe_channel(channel);
539874aeea5SJeff Kirsher 		if (rc) {
540874aeea5SJeff Kirsher 			netif_err(efx, probe, efx->net_dev,
541874aeea5SJeff Kirsher 				  "failed to create channel %d\n",
542874aeea5SJeff Kirsher 				  channel->channel);
543874aeea5SJeff Kirsher 			goto fail;
544874aeea5SJeff Kirsher 		}
545874aeea5SJeff Kirsher 	}
546874aeea5SJeff Kirsher 	efx_set_channel_names(efx);
547874aeea5SJeff Kirsher 
548874aeea5SJeff Kirsher 	return 0;
549874aeea5SJeff Kirsher 
550874aeea5SJeff Kirsher fail:
551874aeea5SJeff Kirsher 	efx_remove_channels(efx);
552874aeea5SJeff Kirsher 	return rc;
553874aeea5SJeff Kirsher }
554874aeea5SJeff Kirsher 
555874aeea5SJeff Kirsher /* Channels are shutdown and reinitialised whilst the NIC is running
556874aeea5SJeff Kirsher  * to propagate configuration changes (mtu, checksum offload), or
557874aeea5SJeff Kirsher  * to clear hardware error conditions
558874aeea5SJeff Kirsher  */
559874aeea5SJeff Kirsher static void efx_init_channels(struct efx_nic *efx)
560874aeea5SJeff Kirsher {
561874aeea5SJeff Kirsher 	struct efx_tx_queue *tx_queue;
562874aeea5SJeff Kirsher 	struct efx_rx_queue *rx_queue;
563874aeea5SJeff Kirsher 	struct efx_channel *channel;
564874aeea5SJeff Kirsher 
565874aeea5SJeff Kirsher 	/* Calculate the rx buffer allocation parameters required to
566874aeea5SJeff Kirsher 	 * support the current MTU, including padding for header
567874aeea5SJeff Kirsher 	 * alignment and overruns.
568874aeea5SJeff Kirsher 	 */
569874aeea5SJeff Kirsher 	efx->rx_buffer_len = (max(EFX_PAGE_IP_ALIGN, NET_IP_ALIGN) +
570874aeea5SJeff Kirsher 			      EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
571874aeea5SJeff Kirsher 			      efx->type->rx_buffer_hash_size +
572874aeea5SJeff Kirsher 			      efx->type->rx_buffer_padding);
573874aeea5SJeff Kirsher 	efx->rx_buffer_order = get_order(efx->rx_buffer_len +
574874aeea5SJeff Kirsher 					 sizeof(struct efx_rx_page_state));
575874aeea5SJeff Kirsher 
576874aeea5SJeff Kirsher 	/* Initialise the channels */
577874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx) {
578874aeea5SJeff Kirsher 		netif_dbg(channel->efx, drv, channel->efx->net_dev,
579874aeea5SJeff Kirsher 			  "init chan %d\n", channel->channel);
580874aeea5SJeff Kirsher 
581874aeea5SJeff Kirsher 		efx_init_eventq(channel);
582874aeea5SJeff Kirsher 
583874aeea5SJeff Kirsher 		efx_for_each_channel_tx_queue(tx_queue, channel)
584874aeea5SJeff Kirsher 			efx_init_tx_queue(tx_queue);
585874aeea5SJeff Kirsher 
586874aeea5SJeff Kirsher 		/* The rx buffer allocation strategy is MTU dependent */
587874aeea5SJeff Kirsher 		efx_rx_strategy(channel);
588874aeea5SJeff Kirsher 
589874aeea5SJeff Kirsher 		efx_for_each_channel_rx_queue(rx_queue, channel)
590874aeea5SJeff Kirsher 			efx_init_rx_queue(rx_queue);
591874aeea5SJeff Kirsher 
592874aeea5SJeff Kirsher 		WARN_ON(channel->rx_pkt != NULL);
593874aeea5SJeff Kirsher 		efx_rx_strategy(channel);
594874aeea5SJeff Kirsher 	}
595874aeea5SJeff Kirsher }
596874aeea5SJeff Kirsher 
597874aeea5SJeff Kirsher /* This enables event queue processing and packet transmission.
598874aeea5SJeff Kirsher  *
599874aeea5SJeff Kirsher  * Note that this function is not allowed to fail, since that would
600874aeea5SJeff Kirsher  * introduce too much complexity into the suspend/resume path.
601874aeea5SJeff Kirsher  */
602874aeea5SJeff Kirsher static void efx_start_channel(struct efx_channel *channel)
603874aeea5SJeff Kirsher {
604874aeea5SJeff Kirsher 	struct efx_rx_queue *rx_queue;
605874aeea5SJeff Kirsher 
606874aeea5SJeff Kirsher 	netif_dbg(channel->efx, ifup, channel->efx->net_dev,
607874aeea5SJeff Kirsher 		  "starting chan %d\n", channel->channel);
608874aeea5SJeff Kirsher 
609874aeea5SJeff Kirsher 	/* The interrupt handler for this channel may set work_pending
610874aeea5SJeff Kirsher 	 * as soon as we enable it.  Make sure it's cleared before
611874aeea5SJeff Kirsher 	 * then.  Similarly, make sure it sees the enabled flag set. */
612874aeea5SJeff Kirsher 	channel->work_pending = false;
613874aeea5SJeff Kirsher 	channel->enabled = true;
614874aeea5SJeff Kirsher 	smp_wmb();
615874aeea5SJeff Kirsher 
616874aeea5SJeff Kirsher 	/* Fill the queues before enabling NAPI */
617874aeea5SJeff Kirsher 	efx_for_each_channel_rx_queue(rx_queue, channel)
618874aeea5SJeff Kirsher 		efx_fast_push_rx_descriptors(rx_queue);
619874aeea5SJeff Kirsher 
620874aeea5SJeff Kirsher 	napi_enable(&channel->napi_str);
621874aeea5SJeff Kirsher }
622874aeea5SJeff Kirsher 
623874aeea5SJeff Kirsher /* This disables event queue processing and packet transmission.
624874aeea5SJeff Kirsher  * This function does not guarantee that all queue processing
625874aeea5SJeff Kirsher  * (e.g. RX refill) is complete.
626874aeea5SJeff Kirsher  */
627874aeea5SJeff Kirsher static void efx_stop_channel(struct efx_channel *channel)
628874aeea5SJeff Kirsher {
629874aeea5SJeff Kirsher 	if (!channel->enabled)
630874aeea5SJeff Kirsher 		return;
631874aeea5SJeff Kirsher 
632874aeea5SJeff Kirsher 	netif_dbg(channel->efx, ifdown, channel->efx->net_dev,
633874aeea5SJeff Kirsher 		  "stop chan %d\n", channel->channel);
634874aeea5SJeff Kirsher 
635874aeea5SJeff Kirsher 	channel->enabled = false;
636874aeea5SJeff Kirsher 	napi_disable(&channel->napi_str);
637874aeea5SJeff Kirsher }
638874aeea5SJeff Kirsher 
639874aeea5SJeff Kirsher static void efx_fini_channels(struct efx_nic *efx)
640874aeea5SJeff Kirsher {
641874aeea5SJeff Kirsher 	struct efx_channel *channel;
642874aeea5SJeff Kirsher 	struct efx_tx_queue *tx_queue;
643874aeea5SJeff Kirsher 	struct efx_rx_queue *rx_queue;
644874aeea5SJeff Kirsher 	int rc;
645874aeea5SJeff Kirsher 
646874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
647874aeea5SJeff Kirsher 	BUG_ON(efx->port_enabled);
648874aeea5SJeff Kirsher 
649874aeea5SJeff Kirsher 	rc = efx_nic_flush_queues(efx);
650874aeea5SJeff Kirsher 	if (rc && EFX_WORKAROUND_7803(efx)) {
651874aeea5SJeff Kirsher 		/* Schedule a reset to recover from the flush failure. The
652874aeea5SJeff Kirsher 		 * descriptor caches reference memory we're about to free,
653874aeea5SJeff Kirsher 		 * but falcon_reconfigure_mac_wrapper() won't reconnect
654874aeea5SJeff Kirsher 		 * the MACs because of the pending reset. */
655874aeea5SJeff Kirsher 		netif_err(efx, drv, efx->net_dev,
656874aeea5SJeff Kirsher 			  "Resetting to recover from flush failure\n");
657874aeea5SJeff Kirsher 		efx_schedule_reset(efx, RESET_TYPE_ALL);
658874aeea5SJeff Kirsher 	} else if (rc) {
659874aeea5SJeff Kirsher 		netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
660874aeea5SJeff Kirsher 	} else {
661874aeea5SJeff Kirsher 		netif_dbg(efx, drv, efx->net_dev,
662874aeea5SJeff Kirsher 			  "successfully flushed all queues\n");
663874aeea5SJeff Kirsher 	}
664874aeea5SJeff Kirsher 
665874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx) {
666874aeea5SJeff Kirsher 		netif_dbg(channel->efx, drv, channel->efx->net_dev,
667874aeea5SJeff Kirsher 			  "shut down chan %d\n", channel->channel);
668874aeea5SJeff Kirsher 
669874aeea5SJeff Kirsher 		efx_for_each_channel_rx_queue(rx_queue, channel)
670874aeea5SJeff Kirsher 			efx_fini_rx_queue(rx_queue);
671874aeea5SJeff Kirsher 		efx_for_each_possible_channel_tx_queue(tx_queue, channel)
672874aeea5SJeff Kirsher 			efx_fini_tx_queue(tx_queue);
673874aeea5SJeff Kirsher 		efx_fini_eventq(channel);
674874aeea5SJeff Kirsher 	}
675874aeea5SJeff Kirsher }
676874aeea5SJeff Kirsher 
677874aeea5SJeff Kirsher static void efx_remove_channel(struct efx_channel *channel)
678874aeea5SJeff Kirsher {
679874aeea5SJeff Kirsher 	struct efx_tx_queue *tx_queue;
680874aeea5SJeff Kirsher 	struct efx_rx_queue *rx_queue;
681874aeea5SJeff Kirsher 
682874aeea5SJeff Kirsher 	netif_dbg(channel->efx, drv, channel->efx->net_dev,
683874aeea5SJeff Kirsher 		  "destroy chan %d\n", channel->channel);
684874aeea5SJeff Kirsher 
685874aeea5SJeff Kirsher 	efx_for_each_channel_rx_queue(rx_queue, channel)
686874aeea5SJeff Kirsher 		efx_remove_rx_queue(rx_queue);
687874aeea5SJeff Kirsher 	efx_for_each_possible_channel_tx_queue(tx_queue, channel)
688874aeea5SJeff Kirsher 		efx_remove_tx_queue(tx_queue);
689874aeea5SJeff Kirsher 	efx_remove_eventq(channel);
690874aeea5SJeff Kirsher }
691874aeea5SJeff Kirsher 
692874aeea5SJeff Kirsher static void efx_remove_channels(struct efx_nic *efx)
693874aeea5SJeff Kirsher {
694874aeea5SJeff Kirsher 	struct efx_channel *channel;
695874aeea5SJeff Kirsher 
696874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx)
697874aeea5SJeff Kirsher 		efx_remove_channel(channel);
698874aeea5SJeff Kirsher }
699874aeea5SJeff Kirsher 
700874aeea5SJeff Kirsher int
701874aeea5SJeff Kirsher efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
702874aeea5SJeff Kirsher {
703874aeea5SJeff Kirsher 	struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
704874aeea5SJeff Kirsher 	u32 old_rxq_entries, old_txq_entries;
705874aeea5SJeff Kirsher 	unsigned i;
706874aeea5SJeff Kirsher 	int rc;
707874aeea5SJeff Kirsher 
708874aeea5SJeff Kirsher 	efx_stop_all(efx);
709874aeea5SJeff Kirsher 	efx_fini_channels(efx);
710874aeea5SJeff Kirsher 
711874aeea5SJeff Kirsher 	/* Clone channels */
712874aeea5SJeff Kirsher 	memset(other_channel, 0, sizeof(other_channel));
713874aeea5SJeff Kirsher 	for (i = 0; i < efx->n_channels; i++) {
714874aeea5SJeff Kirsher 		channel = efx_alloc_channel(efx, i, efx->channel[i]);
715874aeea5SJeff Kirsher 		if (!channel) {
716874aeea5SJeff Kirsher 			rc = -ENOMEM;
717874aeea5SJeff Kirsher 			goto out;
718874aeea5SJeff Kirsher 		}
719874aeea5SJeff Kirsher 		other_channel[i] = channel;
720874aeea5SJeff Kirsher 	}
721874aeea5SJeff Kirsher 
722874aeea5SJeff Kirsher 	/* Swap entry counts and channel pointers */
723874aeea5SJeff Kirsher 	old_rxq_entries = efx->rxq_entries;
724874aeea5SJeff Kirsher 	old_txq_entries = efx->txq_entries;
725874aeea5SJeff Kirsher 	efx->rxq_entries = rxq_entries;
726874aeea5SJeff Kirsher 	efx->txq_entries = txq_entries;
727874aeea5SJeff Kirsher 	for (i = 0; i < efx->n_channels; i++) {
728874aeea5SJeff Kirsher 		channel = efx->channel[i];
729874aeea5SJeff Kirsher 		efx->channel[i] = other_channel[i];
730874aeea5SJeff Kirsher 		other_channel[i] = channel;
731874aeea5SJeff Kirsher 	}
732874aeea5SJeff Kirsher 
733874aeea5SJeff Kirsher 	rc = efx_probe_channels(efx);
734874aeea5SJeff Kirsher 	if (rc)
735874aeea5SJeff Kirsher 		goto rollback;
736874aeea5SJeff Kirsher 
737874aeea5SJeff Kirsher 	efx_init_napi(efx);
738874aeea5SJeff Kirsher 
739874aeea5SJeff Kirsher 	/* Destroy old channels */
740874aeea5SJeff Kirsher 	for (i = 0; i < efx->n_channels; i++) {
741874aeea5SJeff Kirsher 		efx_fini_napi_channel(other_channel[i]);
742874aeea5SJeff Kirsher 		efx_remove_channel(other_channel[i]);
743874aeea5SJeff Kirsher 	}
744874aeea5SJeff Kirsher out:
745874aeea5SJeff Kirsher 	/* Free unused channel structures */
746874aeea5SJeff Kirsher 	for (i = 0; i < efx->n_channels; i++)
747874aeea5SJeff Kirsher 		kfree(other_channel[i]);
748874aeea5SJeff Kirsher 
749874aeea5SJeff Kirsher 	efx_init_channels(efx);
750874aeea5SJeff Kirsher 	efx_start_all(efx);
751874aeea5SJeff Kirsher 	return rc;
752874aeea5SJeff Kirsher 
753874aeea5SJeff Kirsher rollback:
754874aeea5SJeff Kirsher 	/* Swap back */
755874aeea5SJeff Kirsher 	efx->rxq_entries = old_rxq_entries;
756874aeea5SJeff Kirsher 	efx->txq_entries = old_txq_entries;
757874aeea5SJeff Kirsher 	for (i = 0; i < efx->n_channels; i++) {
758874aeea5SJeff Kirsher 		channel = efx->channel[i];
759874aeea5SJeff Kirsher 		efx->channel[i] = other_channel[i];
760874aeea5SJeff Kirsher 		other_channel[i] = channel;
761874aeea5SJeff Kirsher 	}
762874aeea5SJeff Kirsher 	goto out;
763874aeea5SJeff Kirsher }
764874aeea5SJeff Kirsher 
765874aeea5SJeff Kirsher void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
766874aeea5SJeff Kirsher {
767874aeea5SJeff Kirsher 	mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
768874aeea5SJeff Kirsher }
769874aeea5SJeff Kirsher 
770874aeea5SJeff Kirsher /**************************************************************************
771874aeea5SJeff Kirsher  *
772874aeea5SJeff Kirsher  * Port handling
773874aeea5SJeff Kirsher  *
774874aeea5SJeff Kirsher  **************************************************************************/
775874aeea5SJeff Kirsher 
776874aeea5SJeff Kirsher /* This ensures that the kernel is kept informed (via
777874aeea5SJeff Kirsher  * netif_carrier_on/off) of the link status, and also maintains the
778874aeea5SJeff Kirsher  * link status's stop on the port's TX queue.
779874aeea5SJeff Kirsher  */
780874aeea5SJeff Kirsher void efx_link_status_changed(struct efx_nic *efx)
781874aeea5SJeff Kirsher {
782874aeea5SJeff Kirsher 	struct efx_link_state *link_state = &efx->link_state;
783874aeea5SJeff Kirsher 
784874aeea5SJeff Kirsher 	/* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
785874aeea5SJeff Kirsher 	 * that no events are triggered between unregister_netdev() and the
786874aeea5SJeff Kirsher 	 * driver unloading. A more general condition is that NETDEV_CHANGE
787874aeea5SJeff Kirsher 	 * can only be generated between NETDEV_UP and NETDEV_DOWN */
788874aeea5SJeff Kirsher 	if (!netif_running(efx->net_dev))
789874aeea5SJeff Kirsher 		return;
790874aeea5SJeff Kirsher 
791874aeea5SJeff Kirsher 	if (link_state->up != netif_carrier_ok(efx->net_dev)) {
792874aeea5SJeff Kirsher 		efx->n_link_state_changes++;
793874aeea5SJeff Kirsher 
794874aeea5SJeff Kirsher 		if (link_state->up)
795874aeea5SJeff Kirsher 			netif_carrier_on(efx->net_dev);
796874aeea5SJeff Kirsher 		else
797874aeea5SJeff Kirsher 			netif_carrier_off(efx->net_dev);
798874aeea5SJeff Kirsher 	}
799874aeea5SJeff Kirsher 
800874aeea5SJeff Kirsher 	/* Status message for kernel log */
801874aeea5SJeff Kirsher 	if (link_state->up) {
802874aeea5SJeff Kirsher 		netif_info(efx, link, efx->net_dev,
803874aeea5SJeff Kirsher 			   "link up at %uMbps %s-duplex (MTU %d)%s\n",
804874aeea5SJeff Kirsher 			   link_state->speed, link_state->fd ? "full" : "half",
805874aeea5SJeff Kirsher 			   efx->net_dev->mtu,
806874aeea5SJeff Kirsher 			   (efx->promiscuous ? " [PROMISC]" : ""));
807874aeea5SJeff Kirsher 	} else {
808874aeea5SJeff Kirsher 		netif_info(efx, link, efx->net_dev, "link down\n");
809874aeea5SJeff Kirsher 	}
810874aeea5SJeff Kirsher 
811874aeea5SJeff Kirsher }
812874aeea5SJeff Kirsher 
813874aeea5SJeff Kirsher void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
814874aeea5SJeff Kirsher {
815874aeea5SJeff Kirsher 	efx->link_advertising = advertising;
816874aeea5SJeff Kirsher 	if (advertising) {
817874aeea5SJeff Kirsher 		if (advertising & ADVERTISED_Pause)
818874aeea5SJeff Kirsher 			efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
819874aeea5SJeff Kirsher 		else
820874aeea5SJeff Kirsher 			efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
821874aeea5SJeff Kirsher 		if (advertising & ADVERTISED_Asym_Pause)
822874aeea5SJeff Kirsher 			efx->wanted_fc ^= EFX_FC_TX;
823874aeea5SJeff Kirsher 	}
824874aeea5SJeff Kirsher }
825874aeea5SJeff Kirsher 
826874aeea5SJeff Kirsher void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
827874aeea5SJeff Kirsher {
828874aeea5SJeff Kirsher 	efx->wanted_fc = wanted_fc;
829874aeea5SJeff Kirsher 	if (efx->link_advertising) {
830874aeea5SJeff Kirsher 		if (wanted_fc & EFX_FC_RX)
831874aeea5SJeff Kirsher 			efx->link_advertising |= (ADVERTISED_Pause |
832874aeea5SJeff Kirsher 						  ADVERTISED_Asym_Pause);
833874aeea5SJeff Kirsher 		else
834874aeea5SJeff Kirsher 			efx->link_advertising &= ~(ADVERTISED_Pause |
835874aeea5SJeff Kirsher 						   ADVERTISED_Asym_Pause);
836874aeea5SJeff Kirsher 		if (wanted_fc & EFX_FC_TX)
837874aeea5SJeff Kirsher 			efx->link_advertising ^= ADVERTISED_Asym_Pause;
838874aeea5SJeff Kirsher 	}
839874aeea5SJeff Kirsher }
840874aeea5SJeff Kirsher 
841874aeea5SJeff Kirsher static void efx_fini_port(struct efx_nic *efx);
842874aeea5SJeff Kirsher 
843874aeea5SJeff Kirsher /* Push loopback/power/transmit disable settings to the PHY, and reconfigure
844874aeea5SJeff Kirsher  * the MAC appropriately. All other PHY configuration changes are pushed
845874aeea5SJeff Kirsher  * through phy_op->set_settings(), and pushed asynchronously to the MAC
846874aeea5SJeff Kirsher  * through efx_monitor().
847874aeea5SJeff Kirsher  *
848874aeea5SJeff Kirsher  * Callers must hold the mac_lock
849874aeea5SJeff Kirsher  */
850874aeea5SJeff Kirsher int __efx_reconfigure_port(struct efx_nic *efx)
851874aeea5SJeff Kirsher {
852874aeea5SJeff Kirsher 	enum efx_phy_mode phy_mode;
853874aeea5SJeff Kirsher 	int rc;
854874aeea5SJeff Kirsher 
855874aeea5SJeff Kirsher 	WARN_ON(!mutex_is_locked(&efx->mac_lock));
856874aeea5SJeff Kirsher 
857874aeea5SJeff Kirsher 	/* Serialise the promiscuous flag with efx_set_multicast_list. */
858874aeea5SJeff Kirsher 	netif_addr_lock_bh(efx->net_dev);
859874aeea5SJeff Kirsher 	netif_addr_unlock_bh(efx->net_dev);
860874aeea5SJeff Kirsher 
861874aeea5SJeff Kirsher 	/* Disable PHY transmit in mac level loopbacks */
862874aeea5SJeff Kirsher 	phy_mode = efx->phy_mode;
863874aeea5SJeff Kirsher 	if (LOOPBACK_INTERNAL(efx))
864874aeea5SJeff Kirsher 		efx->phy_mode |= PHY_MODE_TX_DISABLED;
865874aeea5SJeff Kirsher 	else
866874aeea5SJeff Kirsher 		efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
867874aeea5SJeff Kirsher 
868874aeea5SJeff Kirsher 	rc = efx->type->reconfigure_port(efx);
869874aeea5SJeff Kirsher 
870874aeea5SJeff Kirsher 	if (rc)
871874aeea5SJeff Kirsher 		efx->phy_mode = phy_mode;
872874aeea5SJeff Kirsher 
873874aeea5SJeff Kirsher 	return rc;
874874aeea5SJeff Kirsher }
875874aeea5SJeff Kirsher 
876874aeea5SJeff Kirsher /* Reinitialise the MAC to pick up new PHY settings, even if the port is
877874aeea5SJeff Kirsher  * disabled. */
878874aeea5SJeff Kirsher int efx_reconfigure_port(struct efx_nic *efx)
879874aeea5SJeff Kirsher {
880874aeea5SJeff Kirsher 	int rc;
881874aeea5SJeff Kirsher 
882874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
883874aeea5SJeff Kirsher 
884874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
885874aeea5SJeff Kirsher 	rc = __efx_reconfigure_port(efx);
886874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
887874aeea5SJeff Kirsher 
888874aeea5SJeff Kirsher 	return rc;
889874aeea5SJeff Kirsher }
890874aeea5SJeff Kirsher 
891874aeea5SJeff Kirsher /* Asynchronous work item for changing MAC promiscuity and multicast
892874aeea5SJeff Kirsher  * hash.  Avoid a drain/rx_ingress enable by reconfiguring the current
893874aeea5SJeff Kirsher  * MAC directly. */
894874aeea5SJeff Kirsher static void efx_mac_work(struct work_struct *data)
895874aeea5SJeff Kirsher {
896874aeea5SJeff Kirsher 	struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
897874aeea5SJeff Kirsher 
898874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
89930b81cdaSBen Hutchings 	if (efx->port_enabled)
900710b208dSBen Hutchings 		efx->type->reconfigure_mac(efx);
901874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
902874aeea5SJeff Kirsher }
903874aeea5SJeff Kirsher 
904874aeea5SJeff Kirsher static int efx_probe_port(struct efx_nic *efx)
905874aeea5SJeff Kirsher {
906874aeea5SJeff Kirsher 	int rc;
907874aeea5SJeff Kirsher 
908874aeea5SJeff Kirsher 	netif_dbg(efx, probe, efx->net_dev, "create port\n");
909874aeea5SJeff Kirsher 
910874aeea5SJeff Kirsher 	if (phy_flash_cfg)
911874aeea5SJeff Kirsher 		efx->phy_mode = PHY_MODE_SPECIAL;
912874aeea5SJeff Kirsher 
913874aeea5SJeff Kirsher 	/* Connect up MAC/PHY operations table */
914874aeea5SJeff Kirsher 	rc = efx->type->probe_port(efx);
915874aeea5SJeff Kirsher 	if (rc)
916874aeea5SJeff Kirsher 		return rc;
917874aeea5SJeff Kirsher 
918e332bcb3SBen Hutchings 	/* Initialise MAC address to permanent address */
919e332bcb3SBen Hutchings 	memcpy(efx->net_dev->dev_addr, efx->net_dev->perm_addr, ETH_ALEN);
920874aeea5SJeff Kirsher 
921874aeea5SJeff Kirsher 	return 0;
922874aeea5SJeff Kirsher }
923874aeea5SJeff Kirsher 
924874aeea5SJeff Kirsher static int efx_init_port(struct efx_nic *efx)
925874aeea5SJeff Kirsher {
926874aeea5SJeff Kirsher 	int rc;
927874aeea5SJeff Kirsher 
928874aeea5SJeff Kirsher 	netif_dbg(efx, drv, efx->net_dev, "init port\n");
929874aeea5SJeff Kirsher 
930874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
931874aeea5SJeff Kirsher 
932874aeea5SJeff Kirsher 	rc = efx->phy_op->init(efx);
933874aeea5SJeff Kirsher 	if (rc)
934874aeea5SJeff Kirsher 		goto fail1;
935874aeea5SJeff Kirsher 
936874aeea5SJeff Kirsher 	efx->port_initialized = true;
937874aeea5SJeff Kirsher 
938874aeea5SJeff Kirsher 	/* Reconfigure the MAC before creating dma queues (required for
939874aeea5SJeff Kirsher 	 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
940710b208dSBen Hutchings 	efx->type->reconfigure_mac(efx);
941874aeea5SJeff Kirsher 
942874aeea5SJeff Kirsher 	/* Ensure the PHY advertises the correct flow control settings */
943874aeea5SJeff Kirsher 	rc = efx->phy_op->reconfigure(efx);
944874aeea5SJeff Kirsher 	if (rc)
945874aeea5SJeff Kirsher 		goto fail2;
946874aeea5SJeff Kirsher 
947874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
948874aeea5SJeff Kirsher 	return 0;
949874aeea5SJeff Kirsher 
950874aeea5SJeff Kirsher fail2:
951874aeea5SJeff Kirsher 	efx->phy_op->fini(efx);
952874aeea5SJeff Kirsher fail1:
953874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
954874aeea5SJeff Kirsher 	return rc;
955874aeea5SJeff Kirsher }
956874aeea5SJeff Kirsher 
957874aeea5SJeff Kirsher static void efx_start_port(struct efx_nic *efx)
958874aeea5SJeff Kirsher {
959874aeea5SJeff Kirsher 	netif_dbg(efx, ifup, efx->net_dev, "start port\n");
960874aeea5SJeff Kirsher 	BUG_ON(efx->port_enabled);
961874aeea5SJeff Kirsher 
962874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
963874aeea5SJeff Kirsher 	efx->port_enabled = true;
964874aeea5SJeff Kirsher 
965874aeea5SJeff Kirsher 	/* efx_mac_work() might have been scheduled after efx_stop_port(),
966874aeea5SJeff Kirsher 	 * and then cancelled by efx_flush_all() */
967710b208dSBen Hutchings 	efx->type->reconfigure_mac(efx);
968874aeea5SJeff Kirsher 
969874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
970874aeea5SJeff Kirsher }
971874aeea5SJeff Kirsher 
972874aeea5SJeff Kirsher /* Prevent efx_mac_work() and efx_monitor() from working */
973874aeea5SJeff Kirsher static void efx_stop_port(struct efx_nic *efx)
974874aeea5SJeff Kirsher {
975874aeea5SJeff Kirsher 	netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
976874aeea5SJeff Kirsher 
977874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
978874aeea5SJeff Kirsher 	efx->port_enabled = false;
979874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
980874aeea5SJeff Kirsher 
981874aeea5SJeff Kirsher 	/* Serialise against efx_set_multicast_list() */
982874aeea5SJeff Kirsher 	netif_addr_lock_bh(efx->net_dev);
983874aeea5SJeff Kirsher 	netif_addr_unlock_bh(efx->net_dev);
984874aeea5SJeff Kirsher }
985874aeea5SJeff Kirsher 
986874aeea5SJeff Kirsher static void efx_fini_port(struct efx_nic *efx)
987874aeea5SJeff Kirsher {
988874aeea5SJeff Kirsher 	netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
989874aeea5SJeff Kirsher 
990874aeea5SJeff Kirsher 	if (!efx->port_initialized)
991874aeea5SJeff Kirsher 		return;
992874aeea5SJeff Kirsher 
993874aeea5SJeff Kirsher 	efx->phy_op->fini(efx);
994874aeea5SJeff Kirsher 	efx->port_initialized = false;
995874aeea5SJeff Kirsher 
996874aeea5SJeff Kirsher 	efx->link_state.up = false;
997874aeea5SJeff Kirsher 	efx_link_status_changed(efx);
998874aeea5SJeff Kirsher }
999874aeea5SJeff Kirsher 
1000874aeea5SJeff Kirsher static void efx_remove_port(struct efx_nic *efx)
1001874aeea5SJeff Kirsher {
1002874aeea5SJeff Kirsher 	netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
1003874aeea5SJeff Kirsher 
1004874aeea5SJeff Kirsher 	efx->type->remove_port(efx);
1005874aeea5SJeff Kirsher }
1006874aeea5SJeff Kirsher 
1007874aeea5SJeff Kirsher /**************************************************************************
1008874aeea5SJeff Kirsher  *
1009874aeea5SJeff Kirsher  * NIC handling
1010874aeea5SJeff Kirsher  *
1011874aeea5SJeff Kirsher  **************************************************************************/
1012874aeea5SJeff Kirsher 
1013874aeea5SJeff Kirsher /* This configures the PCI device to enable I/O and DMA. */
1014874aeea5SJeff Kirsher static int efx_init_io(struct efx_nic *efx)
1015874aeea5SJeff Kirsher {
1016874aeea5SJeff Kirsher 	struct pci_dev *pci_dev = efx->pci_dev;
1017874aeea5SJeff Kirsher 	dma_addr_t dma_mask = efx->type->max_dma_mask;
1018874aeea5SJeff Kirsher 	int rc;
1019874aeea5SJeff Kirsher 
1020874aeea5SJeff Kirsher 	netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
1021874aeea5SJeff Kirsher 
1022874aeea5SJeff Kirsher 	rc = pci_enable_device(pci_dev);
1023874aeea5SJeff Kirsher 	if (rc) {
1024874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev,
1025874aeea5SJeff Kirsher 			  "failed to enable PCI device\n");
1026874aeea5SJeff Kirsher 		goto fail1;
1027874aeea5SJeff Kirsher 	}
1028874aeea5SJeff Kirsher 
1029874aeea5SJeff Kirsher 	pci_set_master(pci_dev);
1030874aeea5SJeff Kirsher 
1031874aeea5SJeff Kirsher 	/* Set the PCI DMA mask.  Try all possibilities from our
1032874aeea5SJeff Kirsher 	 * genuine mask down to 32 bits, because some architectures
1033874aeea5SJeff Kirsher 	 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
1034874aeea5SJeff Kirsher 	 * masks event though they reject 46 bit masks.
1035874aeea5SJeff Kirsher 	 */
1036874aeea5SJeff Kirsher 	while (dma_mask > 0x7fffffffUL) {
1037e9e01846SBen Hutchings 		if (pci_dma_supported(pci_dev, dma_mask)) {
1038e9e01846SBen Hutchings 			rc = pci_set_dma_mask(pci_dev, dma_mask);
1039e9e01846SBen Hutchings 			if (rc == 0)
1040874aeea5SJeff Kirsher 				break;
1041e9e01846SBen Hutchings 		}
1042874aeea5SJeff Kirsher 		dma_mask >>= 1;
1043874aeea5SJeff Kirsher 	}
1044874aeea5SJeff Kirsher 	if (rc) {
1045874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev,
1046874aeea5SJeff Kirsher 			  "could not find a suitable DMA mask\n");
1047874aeea5SJeff Kirsher 		goto fail2;
1048874aeea5SJeff Kirsher 	}
1049874aeea5SJeff Kirsher 	netif_dbg(efx, probe, efx->net_dev,
1050874aeea5SJeff Kirsher 		  "using DMA mask %llx\n", (unsigned long long) dma_mask);
1051874aeea5SJeff Kirsher 	rc = pci_set_consistent_dma_mask(pci_dev, dma_mask);
1052874aeea5SJeff Kirsher 	if (rc) {
1053874aeea5SJeff Kirsher 		/* pci_set_consistent_dma_mask() is not *allowed* to
1054874aeea5SJeff Kirsher 		 * fail with a mask that pci_set_dma_mask() accepted,
1055874aeea5SJeff Kirsher 		 * but just in case...
1056874aeea5SJeff Kirsher 		 */
1057874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev,
1058874aeea5SJeff Kirsher 			  "failed to set consistent DMA mask\n");
1059874aeea5SJeff Kirsher 		goto fail2;
1060874aeea5SJeff Kirsher 	}
1061874aeea5SJeff Kirsher 
1062874aeea5SJeff Kirsher 	efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
1063874aeea5SJeff Kirsher 	rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
1064874aeea5SJeff Kirsher 	if (rc) {
1065874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev,
1066874aeea5SJeff Kirsher 			  "request for memory BAR failed\n");
1067874aeea5SJeff Kirsher 		rc = -EIO;
1068874aeea5SJeff Kirsher 		goto fail3;
1069874aeea5SJeff Kirsher 	}
1070874aeea5SJeff Kirsher 	efx->membase = ioremap_nocache(efx->membase_phys,
1071874aeea5SJeff Kirsher 				       efx->type->mem_map_size);
1072874aeea5SJeff Kirsher 	if (!efx->membase) {
1073874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev,
1074874aeea5SJeff Kirsher 			  "could not map memory BAR at %llx+%x\n",
1075874aeea5SJeff Kirsher 			  (unsigned long long)efx->membase_phys,
1076874aeea5SJeff Kirsher 			  efx->type->mem_map_size);
1077874aeea5SJeff Kirsher 		rc = -ENOMEM;
1078874aeea5SJeff Kirsher 		goto fail4;
1079874aeea5SJeff Kirsher 	}
1080874aeea5SJeff Kirsher 	netif_dbg(efx, probe, efx->net_dev,
1081874aeea5SJeff Kirsher 		  "memory BAR at %llx+%x (virtual %p)\n",
1082874aeea5SJeff Kirsher 		  (unsigned long long)efx->membase_phys,
1083874aeea5SJeff Kirsher 		  efx->type->mem_map_size, efx->membase);
1084874aeea5SJeff Kirsher 
1085874aeea5SJeff Kirsher 	return 0;
1086874aeea5SJeff Kirsher 
1087874aeea5SJeff Kirsher  fail4:
1088874aeea5SJeff Kirsher 	pci_release_region(efx->pci_dev, EFX_MEM_BAR);
1089874aeea5SJeff Kirsher  fail3:
1090874aeea5SJeff Kirsher 	efx->membase_phys = 0;
1091874aeea5SJeff Kirsher  fail2:
1092874aeea5SJeff Kirsher 	pci_disable_device(efx->pci_dev);
1093874aeea5SJeff Kirsher  fail1:
1094874aeea5SJeff Kirsher 	return rc;
1095874aeea5SJeff Kirsher }
1096874aeea5SJeff Kirsher 
1097874aeea5SJeff Kirsher static void efx_fini_io(struct efx_nic *efx)
1098874aeea5SJeff Kirsher {
1099874aeea5SJeff Kirsher 	netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
1100874aeea5SJeff Kirsher 
1101874aeea5SJeff Kirsher 	if (efx->membase) {
1102874aeea5SJeff Kirsher 		iounmap(efx->membase);
1103874aeea5SJeff Kirsher 		efx->membase = NULL;
1104874aeea5SJeff Kirsher 	}
1105874aeea5SJeff Kirsher 
1106874aeea5SJeff Kirsher 	if (efx->membase_phys) {
1107874aeea5SJeff Kirsher 		pci_release_region(efx->pci_dev, EFX_MEM_BAR);
1108874aeea5SJeff Kirsher 		efx->membase_phys = 0;
1109874aeea5SJeff Kirsher 	}
1110874aeea5SJeff Kirsher 
1111874aeea5SJeff Kirsher 	pci_disable_device(efx->pci_dev);
1112874aeea5SJeff Kirsher }
1113874aeea5SJeff Kirsher 
1114fa142b9dSBen Hutchings static int efx_wanted_parallelism(void)
1115874aeea5SJeff Kirsher {
1116cdb08f8fSBen Hutchings 	cpumask_var_t thread_mask;
1117874aeea5SJeff Kirsher 	int count;
1118874aeea5SJeff Kirsher 	int cpu;
1119874aeea5SJeff Kirsher 
1120874aeea5SJeff Kirsher 	if (rss_cpus)
1121874aeea5SJeff Kirsher 		return rss_cpus;
1122874aeea5SJeff Kirsher 
1123cdb08f8fSBen Hutchings 	if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
1124874aeea5SJeff Kirsher 		printk(KERN_WARNING
1125874aeea5SJeff Kirsher 		       "sfc: RSS disabled due to allocation failure\n");
1126874aeea5SJeff Kirsher 		return 1;
1127874aeea5SJeff Kirsher 	}
1128874aeea5SJeff Kirsher 
1129874aeea5SJeff Kirsher 	count = 0;
1130874aeea5SJeff Kirsher 	for_each_online_cpu(cpu) {
1131cdb08f8fSBen Hutchings 		if (!cpumask_test_cpu(cpu, thread_mask)) {
1132874aeea5SJeff Kirsher 			++count;
1133cdb08f8fSBen Hutchings 			cpumask_or(thread_mask, thread_mask,
1134cdb08f8fSBen Hutchings 				   topology_thread_cpumask(cpu));
1135874aeea5SJeff Kirsher 		}
1136874aeea5SJeff Kirsher 	}
1137874aeea5SJeff Kirsher 
1138cdb08f8fSBen Hutchings 	free_cpumask_var(thread_mask);
1139874aeea5SJeff Kirsher 	return count;
1140874aeea5SJeff Kirsher }
1141874aeea5SJeff Kirsher 
1142874aeea5SJeff Kirsher static int
1143874aeea5SJeff Kirsher efx_init_rx_cpu_rmap(struct efx_nic *efx, struct msix_entry *xentries)
1144874aeea5SJeff Kirsher {
1145874aeea5SJeff Kirsher #ifdef CONFIG_RFS_ACCEL
1146874aeea5SJeff Kirsher 	int i, rc;
1147874aeea5SJeff Kirsher 
1148874aeea5SJeff Kirsher 	efx->net_dev->rx_cpu_rmap = alloc_irq_cpu_rmap(efx->n_rx_channels);
1149874aeea5SJeff Kirsher 	if (!efx->net_dev->rx_cpu_rmap)
1150874aeea5SJeff Kirsher 		return -ENOMEM;
1151874aeea5SJeff Kirsher 	for (i = 0; i < efx->n_rx_channels; i++) {
1152874aeea5SJeff Kirsher 		rc = irq_cpu_rmap_add(efx->net_dev->rx_cpu_rmap,
1153874aeea5SJeff Kirsher 				      xentries[i].vector);
1154874aeea5SJeff Kirsher 		if (rc) {
1155874aeea5SJeff Kirsher 			free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
1156874aeea5SJeff Kirsher 			efx->net_dev->rx_cpu_rmap = NULL;
1157874aeea5SJeff Kirsher 			return rc;
1158874aeea5SJeff Kirsher 		}
1159874aeea5SJeff Kirsher 	}
1160874aeea5SJeff Kirsher #endif
1161874aeea5SJeff Kirsher 	return 0;
1162874aeea5SJeff Kirsher }
1163874aeea5SJeff Kirsher 
1164874aeea5SJeff Kirsher /* Probe the number and type of interrupts we are able to obtain, and
1165874aeea5SJeff Kirsher  * the resulting numbers of channels and RX queues.
1166874aeea5SJeff Kirsher  */
1167874aeea5SJeff Kirsher static int efx_probe_interrupts(struct efx_nic *efx)
1168874aeea5SJeff Kirsher {
1169874aeea5SJeff Kirsher 	int max_channels =
1170874aeea5SJeff Kirsher 		min_t(int, efx->type->phys_addr_channels, EFX_MAX_CHANNELS);
1171874aeea5SJeff Kirsher 	int rc, i;
1172874aeea5SJeff Kirsher 
1173874aeea5SJeff Kirsher 	if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
1174874aeea5SJeff Kirsher 		struct msix_entry xentries[EFX_MAX_CHANNELS];
1175874aeea5SJeff Kirsher 		int n_channels;
1176874aeea5SJeff Kirsher 
1177fa142b9dSBen Hutchings 		n_channels = efx_wanted_parallelism();
1178874aeea5SJeff Kirsher 		if (separate_tx_channels)
1179874aeea5SJeff Kirsher 			n_channels *= 2;
1180874aeea5SJeff Kirsher 		n_channels = min(n_channels, max_channels);
1181874aeea5SJeff Kirsher 
1182874aeea5SJeff Kirsher 		for (i = 0; i < n_channels; i++)
1183874aeea5SJeff Kirsher 			xentries[i].entry = i;
1184874aeea5SJeff Kirsher 		rc = pci_enable_msix(efx->pci_dev, xentries, n_channels);
1185874aeea5SJeff Kirsher 		if (rc > 0) {
1186874aeea5SJeff Kirsher 			netif_err(efx, drv, efx->net_dev,
1187874aeea5SJeff Kirsher 				  "WARNING: Insufficient MSI-X vectors"
1188874aeea5SJeff Kirsher 				  " available (%d < %d).\n", rc, n_channels);
1189874aeea5SJeff Kirsher 			netif_err(efx, drv, efx->net_dev,
1190874aeea5SJeff Kirsher 				  "WARNING: Performance may be reduced.\n");
1191874aeea5SJeff Kirsher 			EFX_BUG_ON_PARANOID(rc >= n_channels);
1192874aeea5SJeff Kirsher 			n_channels = rc;
1193874aeea5SJeff Kirsher 			rc = pci_enable_msix(efx->pci_dev, xentries,
1194874aeea5SJeff Kirsher 					     n_channels);
1195874aeea5SJeff Kirsher 		}
1196874aeea5SJeff Kirsher 
1197874aeea5SJeff Kirsher 		if (rc == 0) {
1198874aeea5SJeff Kirsher 			efx->n_channels = n_channels;
1199874aeea5SJeff Kirsher 			if (separate_tx_channels) {
1200874aeea5SJeff Kirsher 				efx->n_tx_channels =
1201874aeea5SJeff Kirsher 					max(efx->n_channels / 2, 1U);
1202874aeea5SJeff Kirsher 				efx->n_rx_channels =
1203874aeea5SJeff Kirsher 					max(efx->n_channels -
1204874aeea5SJeff Kirsher 					    efx->n_tx_channels, 1U);
1205874aeea5SJeff Kirsher 			} else {
1206874aeea5SJeff Kirsher 				efx->n_tx_channels = efx->n_channels;
1207874aeea5SJeff Kirsher 				efx->n_rx_channels = efx->n_channels;
1208874aeea5SJeff Kirsher 			}
1209874aeea5SJeff Kirsher 			rc = efx_init_rx_cpu_rmap(efx, xentries);
1210874aeea5SJeff Kirsher 			if (rc) {
1211874aeea5SJeff Kirsher 				pci_disable_msix(efx->pci_dev);
1212874aeea5SJeff Kirsher 				return rc;
1213874aeea5SJeff Kirsher 			}
1214874aeea5SJeff Kirsher 			for (i = 0; i < n_channels; i++)
1215874aeea5SJeff Kirsher 				efx_get_channel(efx, i)->irq =
1216874aeea5SJeff Kirsher 					xentries[i].vector;
1217874aeea5SJeff Kirsher 		} else {
1218874aeea5SJeff Kirsher 			/* Fall back to single channel MSI */
1219874aeea5SJeff Kirsher 			efx->interrupt_mode = EFX_INT_MODE_MSI;
1220874aeea5SJeff Kirsher 			netif_err(efx, drv, efx->net_dev,
1221874aeea5SJeff Kirsher 				  "could not enable MSI-X\n");
1222874aeea5SJeff Kirsher 		}
1223874aeea5SJeff Kirsher 	}
1224874aeea5SJeff Kirsher 
1225874aeea5SJeff Kirsher 	/* Try single interrupt MSI */
1226874aeea5SJeff Kirsher 	if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
1227874aeea5SJeff Kirsher 		efx->n_channels = 1;
1228874aeea5SJeff Kirsher 		efx->n_rx_channels = 1;
1229874aeea5SJeff Kirsher 		efx->n_tx_channels = 1;
1230874aeea5SJeff Kirsher 		rc = pci_enable_msi(efx->pci_dev);
1231874aeea5SJeff Kirsher 		if (rc == 0) {
1232874aeea5SJeff Kirsher 			efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
1233874aeea5SJeff Kirsher 		} else {
1234874aeea5SJeff Kirsher 			netif_err(efx, drv, efx->net_dev,
1235874aeea5SJeff Kirsher 				  "could not enable MSI\n");
1236874aeea5SJeff Kirsher 			efx->interrupt_mode = EFX_INT_MODE_LEGACY;
1237874aeea5SJeff Kirsher 		}
1238874aeea5SJeff Kirsher 	}
1239874aeea5SJeff Kirsher 
1240874aeea5SJeff Kirsher 	/* Assume legacy interrupts */
1241874aeea5SJeff Kirsher 	if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
1242874aeea5SJeff Kirsher 		efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
1243874aeea5SJeff Kirsher 		efx->n_rx_channels = 1;
1244874aeea5SJeff Kirsher 		efx->n_tx_channels = 1;
1245874aeea5SJeff Kirsher 		efx->legacy_irq = efx->pci_dev->irq;
1246874aeea5SJeff Kirsher 	}
1247874aeea5SJeff Kirsher 
1248874aeea5SJeff Kirsher 	return 0;
1249874aeea5SJeff Kirsher }
1250874aeea5SJeff Kirsher 
1251874aeea5SJeff Kirsher static void efx_remove_interrupts(struct efx_nic *efx)
1252874aeea5SJeff Kirsher {
1253874aeea5SJeff Kirsher 	struct efx_channel *channel;
1254874aeea5SJeff Kirsher 
1255874aeea5SJeff Kirsher 	/* Remove MSI/MSI-X interrupts */
1256874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx)
1257874aeea5SJeff Kirsher 		channel->irq = 0;
1258874aeea5SJeff Kirsher 	pci_disable_msi(efx->pci_dev);
1259874aeea5SJeff Kirsher 	pci_disable_msix(efx->pci_dev);
1260874aeea5SJeff Kirsher 
1261874aeea5SJeff Kirsher 	/* Remove legacy interrupt */
1262874aeea5SJeff Kirsher 	efx->legacy_irq = 0;
1263874aeea5SJeff Kirsher }
1264874aeea5SJeff Kirsher 
1265874aeea5SJeff Kirsher static void efx_set_channels(struct efx_nic *efx)
1266874aeea5SJeff Kirsher {
1267874aeea5SJeff Kirsher 	struct efx_channel *channel;
1268874aeea5SJeff Kirsher 	struct efx_tx_queue *tx_queue;
1269874aeea5SJeff Kirsher 
1270874aeea5SJeff Kirsher 	efx->tx_channel_offset =
1271874aeea5SJeff Kirsher 		separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0;
1272874aeea5SJeff Kirsher 
1273874aeea5SJeff Kirsher 	/* We need to adjust the TX queue numbers if we have separate
1274874aeea5SJeff Kirsher 	 * RX-only and TX-only channels.
1275874aeea5SJeff Kirsher 	 */
1276874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx) {
1277874aeea5SJeff Kirsher 		efx_for_each_channel_tx_queue(tx_queue, channel)
1278874aeea5SJeff Kirsher 			tx_queue->queue -= (efx->tx_channel_offset *
1279874aeea5SJeff Kirsher 					    EFX_TXQ_TYPES);
1280874aeea5SJeff Kirsher 	}
1281874aeea5SJeff Kirsher }
1282874aeea5SJeff Kirsher 
1283874aeea5SJeff Kirsher static int efx_probe_nic(struct efx_nic *efx)
1284874aeea5SJeff Kirsher {
1285874aeea5SJeff Kirsher 	size_t i;
1286874aeea5SJeff Kirsher 	int rc;
1287874aeea5SJeff Kirsher 
1288874aeea5SJeff Kirsher 	netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
1289874aeea5SJeff Kirsher 
1290874aeea5SJeff Kirsher 	/* Carry out hardware-type specific initialisation */
1291874aeea5SJeff Kirsher 	rc = efx->type->probe(efx);
1292874aeea5SJeff Kirsher 	if (rc)
1293874aeea5SJeff Kirsher 		return rc;
1294874aeea5SJeff Kirsher 
1295874aeea5SJeff Kirsher 	/* Determine the number of channels and queues by trying to hook
1296874aeea5SJeff Kirsher 	 * in MSI-X interrupts. */
1297874aeea5SJeff Kirsher 	rc = efx_probe_interrupts(efx);
1298874aeea5SJeff Kirsher 	if (rc)
1299874aeea5SJeff Kirsher 		goto fail;
1300874aeea5SJeff Kirsher 
1301874aeea5SJeff Kirsher 	if (efx->n_channels > 1)
1302874aeea5SJeff Kirsher 		get_random_bytes(&efx->rx_hash_key, sizeof(efx->rx_hash_key));
1303874aeea5SJeff Kirsher 	for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
1304278bc429SBen Hutchings 		efx->rx_indir_table[i] =
1305278bc429SBen Hutchings 			ethtool_rxfh_indir_default(i, efx->n_rx_channels);
1306874aeea5SJeff Kirsher 
1307874aeea5SJeff Kirsher 	efx_set_channels(efx);
1308874aeea5SJeff Kirsher 	netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1309874aeea5SJeff Kirsher 	netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
1310874aeea5SJeff Kirsher 
1311874aeea5SJeff Kirsher 	/* Initialise the interrupt moderation settings */
13129e393b30SBen Hutchings 	efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
13139e393b30SBen Hutchings 				true);
1314874aeea5SJeff Kirsher 
1315874aeea5SJeff Kirsher 	return 0;
1316874aeea5SJeff Kirsher 
1317874aeea5SJeff Kirsher fail:
1318874aeea5SJeff Kirsher 	efx->type->remove(efx);
1319874aeea5SJeff Kirsher 	return rc;
1320874aeea5SJeff Kirsher }
1321874aeea5SJeff Kirsher 
1322874aeea5SJeff Kirsher static void efx_remove_nic(struct efx_nic *efx)
1323874aeea5SJeff Kirsher {
1324874aeea5SJeff Kirsher 	netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
1325874aeea5SJeff Kirsher 
1326874aeea5SJeff Kirsher 	efx_remove_interrupts(efx);
1327874aeea5SJeff Kirsher 	efx->type->remove(efx);
1328874aeea5SJeff Kirsher }
1329874aeea5SJeff Kirsher 
1330874aeea5SJeff Kirsher /**************************************************************************
1331874aeea5SJeff Kirsher  *
1332874aeea5SJeff Kirsher  * NIC startup/shutdown
1333874aeea5SJeff Kirsher  *
1334874aeea5SJeff Kirsher  *************************************************************************/
1335874aeea5SJeff Kirsher 
1336874aeea5SJeff Kirsher static int efx_probe_all(struct efx_nic *efx)
1337874aeea5SJeff Kirsher {
1338874aeea5SJeff Kirsher 	int rc;
1339874aeea5SJeff Kirsher 
1340874aeea5SJeff Kirsher 	rc = efx_probe_nic(efx);
1341874aeea5SJeff Kirsher 	if (rc) {
1342874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
1343874aeea5SJeff Kirsher 		goto fail1;
1344874aeea5SJeff Kirsher 	}
1345874aeea5SJeff Kirsher 
1346874aeea5SJeff Kirsher 	rc = efx_probe_port(efx);
1347874aeea5SJeff Kirsher 	if (rc) {
1348874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev, "failed to create port\n");
1349874aeea5SJeff Kirsher 		goto fail2;
1350874aeea5SJeff Kirsher 	}
1351874aeea5SJeff Kirsher 
1352874aeea5SJeff Kirsher 	efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
1353874aeea5SJeff Kirsher 	rc = efx_probe_channels(efx);
1354874aeea5SJeff Kirsher 	if (rc)
1355874aeea5SJeff Kirsher 		goto fail3;
1356874aeea5SJeff Kirsher 
1357874aeea5SJeff Kirsher 	rc = efx_probe_filters(efx);
1358874aeea5SJeff Kirsher 	if (rc) {
1359874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev,
1360874aeea5SJeff Kirsher 			  "failed to create filter tables\n");
1361874aeea5SJeff Kirsher 		goto fail4;
1362874aeea5SJeff Kirsher 	}
1363874aeea5SJeff Kirsher 
1364874aeea5SJeff Kirsher 	return 0;
1365874aeea5SJeff Kirsher 
1366874aeea5SJeff Kirsher  fail4:
1367874aeea5SJeff Kirsher 	efx_remove_channels(efx);
1368874aeea5SJeff Kirsher  fail3:
1369874aeea5SJeff Kirsher 	efx_remove_port(efx);
1370874aeea5SJeff Kirsher  fail2:
1371874aeea5SJeff Kirsher 	efx_remove_nic(efx);
1372874aeea5SJeff Kirsher  fail1:
1373874aeea5SJeff Kirsher 	return rc;
1374874aeea5SJeff Kirsher }
1375874aeea5SJeff Kirsher 
1376874aeea5SJeff Kirsher /* Called after previous invocation(s) of efx_stop_all, restarts the
1377874aeea5SJeff Kirsher  * port, kernel transmit queue, NAPI processing and hardware interrupts,
1378874aeea5SJeff Kirsher  * and ensures that the port is scheduled to be reconfigured.
1379874aeea5SJeff Kirsher  * This function is safe to call multiple times when the NIC is in any
1380874aeea5SJeff Kirsher  * state. */
1381874aeea5SJeff Kirsher static void efx_start_all(struct efx_nic *efx)
1382874aeea5SJeff Kirsher {
1383874aeea5SJeff Kirsher 	struct efx_channel *channel;
1384874aeea5SJeff Kirsher 
1385874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
1386874aeea5SJeff Kirsher 
1387874aeea5SJeff Kirsher 	/* Check that it is appropriate to restart the interface. All
1388874aeea5SJeff Kirsher 	 * of these flags are safe to read under just the rtnl lock */
1389874aeea5SJeff Kirsher 	if (efx->port_enabled)
1390874aeea5SJeff Kirsher 		return;
1391874aeea5SJeff Kirsher 	if ((efx->state != STATE_RUNNING) && (efx->state != STATE_INIT))
1392874aeea5SJeff Kirsher 		return;
139373ba7b68SBen Hutchings 	if (!netif_running(efx->net_dev))
1394874aeea5SJeff Kirsher 		return;
1395874aeea5SJeff Kirsher 
1396874aeea5SJeff Kirsher 	/* Mark the port as enabled so port reconfigurations can start, then
1397874aeea5SJeff Kirsher 	 * restart the transmit interface early so the watchdog timer stops */
1398874aeea5SJeff Kirsher 	efx_start_port(efx);
1399874aeea5SJeff Kirsher 
140073ba7b68SBen Hutchings 	if (netif_device_present(efx->net_dev))
1401874aeea5SJeff Kirsher 		netif_tx_wake_all_queues(efx->net_dev);
1402874aeea5SJeff Kirsher 
1403874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx)
1404874aeea5SJeff Kirsher 		efx_start_channel(channel);
1405874aeea5SJeff Kirsher 
1406874aeea5SJeff Kirsher 	if (efx->legacy_irq)
1407874aeea5SJeff Kirsher 		efx->legacy_irq_enabled = true;
1408874aeea5SJeff Kirsher 	efx_nic_enable_interrupts(efx);
1409874aeea5SJeff Kirsher 
1410874aeea5SJeff Kirsher 	/* Switch to event based MCDI completions after enabling interrupts.
1411874aeea5SJeff Kirsher 	 * If a reset has been scheduled, then we need to stay in polled mode.
1412874aeea5SJeff Kirsher 	 * Rather than serialising efx_mcdi_mode_event() [which sleeps] and
1413874aeea5SJeff Kirsher 	 * reset_pending [modified from an atomic context], we instead guarantee
1414874aeea5SJeff Kirsher 	 * that efx_mcdi_mode_poll() isn't reverted erroneously */
1415874aeea5SJeff Kirsher 	efx_mcdi_mode_event(efx);
1416874aeea5SJeff Kirsher 	if (efx->reset_pending)
1417874aeea5SJeff Kirsher 		efx_mcdi_mode_poll(efx);
1418874aeea5SJeff Kirsher 
1419874aeea5SJeff Kirsher 	/* Start the hardware monitor if there is one. Otherwise (we're link
1420874aeea5SJeff Kirsher 	 * event driven), we have to poll the PHY because after an event queue
1421874aeea5SJeff Kirsher 	 * flush, we could have a missed a link state change */
1422874aeea5SJeff Kirsher 	if (efx->type->monitor != NULL) {
1423874aeea5SJeff Kirsher 		queue_delayed_work(efx->workqueue, &efx->monitor_work,
1424874aeea5SJeff Kirsher 				   efx_monitor_interval);
1425874aeea5SJeff Kirsher 	} else {
1426874aeea5SJeff Kirsher 		mutex_lock(&efx->mac_lock);
1427874aeea5SJeff Kirsher 		if (efx->phy_op->poll(efx))
1428874aeea5SJeff Kirsher 			efx_link_status_changed(efx);
1429874aeea5SJeff Kirsher 		mutex_unlock(&efx->mac_lock);
1430874aeea5SJeff Kirsher 	}
1431874aeea5SJeff Kirsher 
1432874aeea5SJeff Kirsher 	efx->type->start_stats(efx);
1433874aeea5SJeff Kirsher }
1434874aeea5SJeff Kirsher 
1435874aeea5SJeff Kirsher /* Flush all delayed work. Should only be called when no more delayed work
1436874aeea5SJeff Kirsher  * will be scheduled. This doesn't flush pending online resets (efx_reset),
1437874aeea5SJeff Kirsher  * since we're holding the rtnl_lock at this point. */
1438874aeea5SJeff Kirsher static void efx_flush_all(struct efx_nic *efx)
1439874aeea5SJeff Kirsher {
1440874aeea5SJeff Kirsher 	/* Make sure the hardware monitor is stopped */
1441874aeea5SJeff Kirsher 	cancel_delayed_work_sync(&efx->monitor_work);
1442874aeea5SJeff Kirsher 	/* Stop scheduled port reconfigurations */
1443874aeea5SJeff Kirsher 	cancel_work_sync(&efx->mac_work);
1444874aeea5SJeff Kirsher }
1445874aeea5SJeff Kirsher 
1446874aeea5SJeff Kirsher /* Quiesce hardware and software without bringing the link down.
1447874aeea5SJeff Kirsher  * Safe to call multiple times, when the nic and interface is in any
1448874aeea5SJeff Kirsher  * state. The caller is guaranteed to subsequently be in a position
1449874aeea5SJeff Kirsher  * to modify any hardware and software state they see fit without
1450874aeea5SJeff Kirsher  * taking locks. */
1451874aeea5SJeff Kirsher static void efx_stop_all(struct efx_nic *efx)
1452874aeea5SJeff Kirsher {
1453874aeea5SJeff Kirsher 	struct efx_channel *channel;
1454874aeea5SJeff Kirsher 
1455874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
1456874aeea5SJeff Kirsher 
1457874aeea5SJeff Kirsher 	/* port_enabled can be read safely under the rtnl lock */
1458874aeea5SJeff Kirsher 	if (!efx->port_enabled)
1459874aeea5SJeff Kirsher 		return;
1460874aeea5SJeff Kirsher 
1461874aeea5SJeff Kirsher 	efx->type->stop_stats(efx);
1462874aeea5SJeff Kirsher 
1463874aeea5SJeff Kirsher 	/* Switch to MCDI polling on Siena before disabling interrupts */
1464874aeea5SJeff Kirsher 	efx_mcdi_mode_poll(efx);
1465874aeea5SJeff Kirsher 
1466874aeea5SJeff Kirsher 	/* Disable interrupts and wait for ISR to complete */
1467874aeea5SJeff Kirsher 	efx_nic_disable_interrupts(efx);
1468874aeea5SJeff Kirsher 	if (efx->legacy_irq) {
1469874aeea5SJeff Kirsher 		synchronize_irq(efx->legacy_irq);
1470874aeea5SJeff Kirsher 		efx->legacy_irq_enabled = false;
1471874aeea5SJeff Kirsher 	}
1472874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx) {
1473874aeea5SJeff Kirsher 		if (channel->irq)
1474874aeea5SJeff Kirsher 			synchronize_irq(channel->irq);
1475874aeea5SJeff Kirsher 	}
1476874aeea5SJeff Kirsher 
1477874aeea5SJeff Kirsher 	/* Stop all NAPI processing and synchronous rx refills */
1478874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx)
1479874aeea5SJeff Kirsher 		efx_stop_channel(channel);
1480874aeea5SJeff Kirsher 
1481874aeea5SJeff Kirsher 	/* Stop all asynchronous port reconfigurations. Since all
1482874aeea5SJeff Kirsher 	 * event processing has already been stopped, there is no
1483874aeea5SJeff Kirsher 	 * window to loose phy events */
1484874aeea5SJeff Kirsher 	efx_stop_port(efx);
1485874aeea5SJeff Kirsher 
1486874aeea5SJeff Kirsher 	/* Flush efx_mac_work(), refill_workqueue, monitor_work */
1487874aeea5SJeff Kirsher 	efx_flush_all(efx);
1488874aeea5SJeff Kirsher 
1489874aeea5SJeff Kirsher 	/* Stop the kernel transmit interface late, so the watchdog
1490874aeea5SJeff Kirsher 	 * timer isn't ticking over the flush */
1491874aeea5SJeff Kirsher 	netif_tx_stop_all_queues(efx->net_dev);
1492874aeea5SJeff Kirsher 	netif_tx_lock_bh(efx->net_dev);
1493874aeea5SJeff Kirsher 	netif_tx_unlock_bh(efx->net_dev);
1494874aeea5SJeff Kirsher }
1495874aeea5SJeff Kirsher 
1496874aeea5SJeff Kirsher static void efx_remove_all(struct efx_nic *efx)
1497874aeea5SJeff Kirsher {
1498874aeea5SJeff Kirsher 	efx_remove_filters(efx);
1499874aeea5SJeff Kirsher 	efx_remove_channels(efx);
1500874aeea5SJeff Kirsher 	efx_remove_port(efx);
1501874aeea5SJeff Kirsher 	efx_remove_nic(efx);
1502874aeea5SJeff Kirsher }
1503874aeea5SJeff Kirsher 
1504874aeea5SJeff Kirsher /**************************************************************************
1505874aeea5SJeff Kirsher  *
1506874aeea5SJeff Kirsher  * Interrupt moderation
1507874aeea5SJeff Kirsher  *
1508874aeea5SJeff Kirsher  **************************************************************************/
1509874aeea5SJeff Kirsher 
1510cc180b69SBen Hutchings static unsigned int irq_mod_ticks(unsigned int usecs, unsigned int quantum_ns)
1511874aeea5SJeff Kirsher {
1512b548f976SBen Hutchings 	if (usecs == 0)
1513b548f976SBen Hutchings 		return 0;
1514cc180b69SBen Hutchings 	if (usecs * 1000 < quantum_ns)
1515874aeea5SJeff Kirsher 		return 1; /* never round down to 0 */
1516cc180b69SBen Hutchings 	return usecs * 1000 / quantum_ns;
1517874aeea5SJeff Kirsher }
1518874aeea5SJeff Kirsher 
1519874aeea5SJeff Kirsher /* Set interrupt moderation parameters */
15209e393b30SBen Hutchings int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
15219e393b30SBen Hutchings 			    unsigned int rx_usecs, bool rx_adaptive,
15229e393b30SBen Hutchings 			    bool rx_may_override_tx)
1523874aeea5SJeff Kirsher {
1524874aeea5SJeff Kirsher 	struct efx_channel *channel;
1525cc180b69SBen Hutchings 	unsigned int irq_mod_max = DIV_ROUND_UP(efx->type->timer_period_max *
1526cc180b69SBen Hutchings 						efx->timer_quantum_ns,
1527cc180b69SBen Hutchings 						1000);
1528cc180b69SBen Hutchings 	unsigned int tx_ticks;
1529cc180b69SBen Hutchings 	unsigned int rx_ticks;
1530874aeea5SJeff Kirsher 
1531874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
1532874aeea5SJeff Kirsher 
1533cc180b69SBen Hutchings 	if (tx_usecs > irq_mod_max || rx_usecs > irq_mod_max)
15349e393b30SBen Hutchings 		return -EINVAL;
15359e393b30SBen Hutchings 
1536cc180b69SBen Hutchings 	tx_ticks = irq_mod_ticks(tx_usecs, efx->timer_quantum_ns);
1537cc180b69SBen Hutchings 	rx_ticks = irq_mod_ticks(rx_usecs, efx->timer_quantum_ns);
1538cc180b69SBen Hutchings 
15399e393b30SBen Hutchings 	if (tx_ticks != rx_ticks && efx->tx_channel_offset == 0 &&
15409e393b30SBen Hutchings 	    !rx_may_override_tx) {
15419e393b30SBen Hutchings 		netif_err(efx, drv, efx->net_dev, "Channels are shared. "
15429e393b30SBen Hutchings 			  "RX and TX IRQ moderation must be equal\n");
15439e393b30SBen Hutchings 		return -EINVAL;
15449e393b30SBen Hutchings 	}
15459e393b30SBen Hutchings 
1546874aeea5SJeff Kirsher 	efx->irq_rx_adaptive = rx_adaptive;
1547874aeea5SJeff Kirsher 	efx->irq_rx_moderation = rx_ticks;
1548874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx) {
1549874aeea5SJeff Kirsher 		if (efx_channel_has_rx_queue(channel))
1550874aeea5SJeff Kirsher 			channel->irq_moderation = rx_ticks;
1551874aeea5SJeff Kirsher 		else if (efx_channel_has_tx_queues(channel))
1552874aeea5SJeff Kirsher 			channel->irq_moderation = tx_ticks;
1553874aeea5SJeff Kirsher 	}
15549e393b30SBen Hutchings 
15559e393b30SBen Hutchings 	return 0;
1556874aeea5SJeff Kirsher }
1557874aeea5SJeff Kirsher 
1558a0c4faf5SBen Hutchings void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
1559a0c4faf5SBen Hutchings 			    unsigned int *rx_usecs, bool *rx_adaptive)
1560a0c4faf5SBen Hutchings {
1561cc180b69SBen Hutchings 	/* We must round up when converting ticks to microseconds
1562cc180b69SBen Hutchings 	 * because we round down when converting the other way.
1563cc180b69SBen Hutchings 	 */
1564cc180b69SBen Hutchings 
1565a0c4faf5SBen Hutchings 	*rx_adaptive = efx->irq_rx_adaptive;
1566cc180b69SBen Hutchings 	*rx_usecs = DIV_ROUND_UP(efx->irq_rx_moderation *
1567cc180b69SBen Hutchings 				 efx->timer_quantum_ns,
1568cc180b69SBen Hutchings 				 1000);
1569a0c4faf5SBen Hutchings 
1570a0c4faf5SBen Hutchings 	/* If channels are shared between RX and TX, so is IRQ
1571a0c4faf5SBen Hutchings 	 * moderation.  Otherwise, IRQ moderation is the same for all
1572a0c4faf5SBen Hutchings 	 * TX channels and is not adaptive.
1573a0c4faf5SBen Hutchings 	 */
1574a0c4faf5SBen Hutchings 	if (efx->tx_channel_offset == 0)
1575a0c4faf5SBen Hutchings 		*tx_usecs = *rx_usecs;
1576a0c4faf5SBen Hutchings 	else
1577cc180b69SBen Hutchings 		*tx_usecs = DIV_ROUND_UP(
1578a0c4faf5SBen Hutchings 			efx->channel[efx->tx_channel_offset]->irq_moderation *
1579cc180b69SBen Hutchings 			efx->timer_quantum_ns,
1580cc180b69SBen Hutchings 			1000);
1581a0c4faf5SBen Hutchings }
1582a0c4faf5SBen Hutchings 
1583874aeea5SJeff Kirsher /**************************************************************************
1584874aeea5SJeff Kirsher  *
1585874aeea5SJeff Kirsher  * Hardware monitor
1586874aeea5SJeff Kirsher  *
1587874aeea5SJeff Kirsher  **************************************************************************/
1588874aeea5SJeff Kirsher 
1589874aeea5SJeff Kirsher /* Run periodically off the general workqueue */
1590874aeea5SJeff Kirsher static void efx_monitor(struct work_struct *data)
1591874aeea5SJeff Kirsher {
1592874aeea5SJeff Kirsher 	struct efx_nic *efx = container_of(data, struct efx_nic,
1593874aeea5SJeff Kirsher 					   monitor_work.work);
1594874aeea5SJeff Kirsher 
1595874aeea5SJeff Kirsher 	netif_vdbg(efx, timer, efx->net_dev,
1596874aeea5SJeff Kirsher 		   "hardware monitor executing on CPU %d\n",
1597874aeea5SJeff Kirsher 		   raw_smp_processor_id());
1598874aeea5SJeff Kirsher 	BUG_ON(efx->type->monitor == NULL);
1599874aeea5SJeff Kirsher 
1600874aeea5SJeff Kirsher 	/* If the mac_lock is already held then it is likely a port
1601874aeea5SJeff Kirsher 	 * reconfiguration is already in place, which will likely do
1602874aeea5SJeff Kirsher 	 * most of the work of monitor() anyway. */
1603874aeea5SJeff Kirsher 	if (mutex_trylock(&efx->mac_lock)) {
1604874aeea5SJeff Kirsher 		if (efx->port_enabled)
1605874aeea5SJeff Kirsher 			efx->type->monitor(efx);
1606874aeea5SJeff Kirsher 		mutex_unlock(&efx->mac_lock);
1607874aeea5SJeff Kirsher 	}
1608874aeea5SJeff Kirsher 
1609874aeea5SJeff Kirsher 	queue_delayed_work(efx->workqueue, &efx->monitor_work,
1610874aeea5SJeff Kirsher 			   efx_monitor_interval);
1611874aeea5SJeff Kirsher }
1612874aeea5SJeff Kirsher 
1613874aeea5SJeff Kirsher /**************************************************************************
1614874aeea5SJeff Kirsher  *
1615874aeea5SJeff Kirsher  * ioctls
1616874aeea5SJeff Kirsher  *
1617874aeea5SJeff Kirsher  *************************************************************************/
1618874aeea5SJeff Kirsher 
1619874aeea5SJeff Kirsher /* Net device ioctl
1620874aeea5SJeff Kirsher  * Context: process, rtnl_lock() held.
1621874aeea5SJeff Kirsher  */
1622874aeea5SJeff Kirsher static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1623874aeea5SJeff Kirsher {
1624874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
1625874aeea5SJeff Kirsher 	struct mii_ioctl_data *data = if_mii(ifr);
1626874aeea5SJeff Kirsher 
1627874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
1628874aeea5SJeff Kirsher 
1629874aeea5SJeff Kirsher 	/* Convert phy_id from older PRTAD/DEVAD format */
1630874aeea5SJeff Kirsher 	if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
1631874aeea5SJeff Kirsher 	    (data->phy_id & 0xfc00) == 0x0400)
1632874aeea5SJeff Kirsher 		data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
1633874aeea5SJeff Kirsher 
1634874aeea5SJeff Kirsher 	return mdio_mii_ioctl(&efx->mdio, data, cmd);
1635874aeea5SJeff Kirsher }
1636874aeea5SJeff Kirsher 
1637874aeea5SJeff Kirsher /**************************************************************************
1638874aeea5SJeff Kirsher  *
1639874aeea5SJeff Kirsher  * NAPI interface
1640874aeea5SJeff Kirsher  *
1641874aeea5SJeff Kirsher  **************************************************************************/
1642874aeea5SJeff Kirsher 
1643874aeea5SJeff Kirsher static void efx_init_napi(struct efx_nic *efx)
1644874aeea5SJeff Kirsher {
1645874aeea5SJeff Kirsher 	struct efx_channel *channel;
1646874aeea5SJeff Kirsher 
1647874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx) {
1648874aeea5SJeff Kirsher 		channel->napi_dev = efx->net_dev;
1649874aeea5SJeff Kirsher 		netif_napi_add(channel->napi_dev, &channel->napi_str,
1650874aeea5SJeff Kirsher 			       efx_poll, napi_weight);
1651874aeea5SJeff Kirsher 	}
1652874aeea5SJeff Kirsher }
1653874aeea5SJeff Kirsher 
1654874aeea5SJeff Kirsher static void efx_fini_napi_channel(struct efx_channel *channel)
1655874aeea5SJeff Kirsher {
1656874aeea5SJeff Kirsher 	if (channel->napi_dev)
1657874aeea5SJeff Kirsher 		netif_napi_del(&channel->napi_str);
1658874aeea5SJeff Kirsher 	channel->napi_dev = NULL;
1659874aeea5SJeff Kirsher }
1660874aeea5SJeff Kirsher 
1661874aeea5SJeff Kirsher static void efx_fini_napi(struct efx_nic *efx)
1662874aeea5SJeff Kirsher {
1663874aeea5SJeff Kirsher 	struct efx_channel *channel;
1664874aeea5SJeff Kirsher 
1665874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx)
1666874aeea5SJeff Kirsher 		efx_fini_napi_channel(channel);
1667874aeea5SJeff Kirsher }
1668874aeea5SJeff Kirsher 
1669874aeea5SJeff Kirsher /**************************************************************************
1670874aeea5SJeff Kirsher  *
1671874aeea5SJeff Kirsher  * Kernel netpoll interface
1672874aeea5SJeff Kirsher  *
1673874aeea5SJeff Kirsher  *************************************************************************/
1674874aeea5SJeff Kirsher 
1675874aeea5SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
1676874aeea5SJeff Kirsher 
1677874aeea5SJeff Kirsher /* Although in the common case interrupts will be disabled, this is not
1678874aeea5SJeff Kirsher  * guaranteed. However, all our work happens inside the NAPI callback,
1679874aeea5SJeff Kirsher  * so no locking is required.
1680874aeea5SJeff Kirsher  */
1681874aeea5SJeff Kirsher static void efx_netpoll(struct net_device *net_dev)
1682874aeea5SJeff Kirsher {
1683874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
1684874aeea5SJeff Kirsher 	struct efx_channel *channel;
1685874aeea5SJeff Kirsher 
1686874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx)
1687874aeea5SJeff Kirsher 		efx_schedule_channel(channel);
1688874aeea5SJeff Kirsher }
1689874aeea5SJeff Kirsher 
1690874aeea5SJeff Kirsher #endif
1691874aeea5SJeff Kirsher 
1692874aeea5SJeff Kirsher /**************************************************************************
1693874aeea5SJeff Kirsher  *
1694874aeea5SJeff Kirsher  * Kernel net device interface
1695874aeea5SJeff Kirsher  *
1696874aeea5SJeff Kirsher  *************************************************************************/
1697874aeea5SJeff Kirsher 
1698874aeea5SJeff Kirsher /* Context: process, rtnl_lock() held. */
1699874aeea5SJeff Kirsher static int efx_net_open(struct net_device *net_dev)
1700874aeea5SJeff Kirsher {
1701874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
1702874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
1703874aeea5SJeff Kirsher 
1704874aeea5SJeff Kirsher 	netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
1705874aeea5SJeff Kirsher 		  raw_smp_processor_id());
1706874aeea5SJeff Kirsher 
1707874aeea5SJeff Kirsher 	if (efx->state == STATE_DISABLED)
1708874aeea5SJeff Kirsher 		return -EIO;
1709874aeea5SJeff Kirsher 	if (efx->phy_mode & PHY_MODE_SPECIAL)
1710874aeea5SJeff Kirsher 		return -EBUSY;
1711874aeea5SJeff Kirsher 	if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
1712874aeea5SJeff Kirsher 		return -EIO;
1713874aeea5SJeff Kirsher 
1714874aeea5SJeff Kirsher 	/* Notify the kernel of the link state polled during driver load,
1715874aeea5SJeff Kirsher 	 * before the monitor starts running */
1716874aeea5SJeff Kirsher 	efx_link_status_changed(efx);
1717874aeea5SJeff Kirsher 
1718874aeea5SJeff Kirsher 	efx_start_all(efx);
1719874aeea5SJeff Kirsher 	return 0;
1720874aeea5SJeff Kirsher }
1721874aeea5SJeff Kirsher 
1722874aeea5SJeff Kirsher /* Context: process, rtnl_lock() held.
1723874aeea5SJeff Kirsher  * Note that the kernel will ignore our return code; this method
1724874aeea5SJeff Kirsher  * should really be a void.
1725874aeea5SJeff Kirsher  */
1726874aeea5SJeff Kirsher static int efx_net_stop(struct net_device *net_dev)
1727874aeea5SJeff Kirsher {
1728874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
1729874aeea5SJeff Kirsher 
1730874aeea5SJeff Kirsher 	netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
1731874aeea5SJeff Kirsher 		  raw_smp_processor_id());
1732874aeea5SJeff Kirsher 
1733874aeea5SJeff Kirsher 	if (efx->state != STATE_DISABLED) {
1734874aeea5SJeff Kirsher 		/* Stop the device and flush all the channels */
1735874aeea5SJeff Kirsher 		efx_stop_all(efx);
1736874aeea5SJeff Kirsher 		efx_fini_channels(efx);
1737874aeea5SJeff Kirsher 		efx_init_channels(efx);
1738874aeea5SJeff Kirsher 	}
1739874aeea5SJeff Kirsher 
1740874aeea5SJeff Kirsher 	return 0;
1741874aeea5SJeff Kirsher }
1742874aeea5SJeff Kirsher 
1743874aeea5SJeff Kirsher /* Context: process, dev_base_lock or RTNL held, non-blocking. */
1744874aeea5SJeff Kirsher static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev, struct rtnl_link_stats64 *stats)
1745874aeea5SJeff Kirsher {
1746874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
1747874aeea5SJeff Kirsher 	struct efx_mac_stats *mac_stats = &efx->mac_stats;
1748874aeea5SJeff Kirsher 
1749874aeea5SJeff Kirsher 	spin_lock_bh(&efx->stats_lock);
17501cb34522SBen Hutchings 
1751874aeea5SJeff Kirsher 	efx->type->update_stats(efx);
1752874aeea5SJeff Kirsher 
1753874aeea5SJeff Kirsher 	stats->rx_packets = mac_stats->rx_packets;
1754874aeea5SJeff Kirsher 	stats->tx_packets = mac_stats->tx_packets;
1755874aeea5SJeff Kirsher 	stats->rx_bytes = mac_stats->rx_bytes;
1756874aeea5SJeff Kirsher 	stats->tx_bytes = mac_stats->tx_bytes;
1757874aeea5SJeff Kirsher 	stats->rx_dropped = efx->n_rx_nodesc_drop_cnt;
1758874aeea5SJeff Kirsher 	stats->multicast = mac_stats->rx_multicast;
1759874aeea5SJeff Kirsher 	stats->collisions = mac_stats->tx_collision;
1760874aeea5SJeff Kirsher 	stats->rx_length_errors = (mac_stats->rx_gtjumbo +
1761874aeea5SJeff Kirsher 				   mac_stats->rx_length_error);
1762874aeea5SJeff Kirsher 	stats->rx_crc_errors = mac_stats->rx_bad;
1763874aeea5SJeff Kirsher 	stats->rx_frame_errors = mac_stats->rx_align_error;
1764874aeea5SJeff Kirsher 	stats->rx_fifo_errors = mac_stats->rx_overflow;
1765874aeea5SJeff Kirsher 	stats->rx_missed_errors = mac_stats->rx_missed;
1766874aeea5SJeff Kirsher 	stats->tx_window_errors = mac_stats->tx_late_collision;
1767874aeea5SJeff Kirsher 
1768874aeea5SJeff Kirsher 	stats->rx_errors = (stats->rx_length_errors +
1769874aeea5SJeff Kirsher 			    stats->rx_crc_errors +
1770874aeea5SJeff Kirsher 			    stats->rx_frame_errors +
1771874aeea5SJeff Kirsher 			    mac_stats->rx_symbol_error);
1772874aeea5SJeff Kirsher 	stats->tx_errors = (stats->tx_window_errors +
1773874aeea5SJeff Kirsher 			    mac_stats->tx_bad);
1774874aeea5SJeff Kirsher 
17751cb34522SBen Hutchings 	spin_unlock_bh(&efx->stats_lock);
17761cb34522SBen Hutchings 
1777874aeea5SJeff Kirsher 	return stats;
1778874aeea5SJeff Kirsher }
1779874aeea5SJeff Kirsher 
1780874aeea5SJeff Kirsher /* Context: netif_tx_lock held, BHs disabled. */
1781874aeea5SJeff Kirsher static void efx_watchdog(struct net_device *net_dev)
1782874aeea5SJeff Kirsher {
1783874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
1784874aeea5SJeff Kirsher 
1785874aeea5SJeff Kirsher 	netif_err(efx, tx_err, efx->net_dev,
1786874aeea5SJeff Kirsher 		  "TX stuck with port_enabled=%d: resetting channels\n",
1787874aeea5SJeff Kirsher 		  efx->port_enabled);
1788874aeea5SJeff Kirsher 
1789874aeea5SJeff Kirsher 	efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
1790874aeea5SJeff Kirsher }
1791874aeea5SJeff Kirsher 
1792874aeea5SJeff Kirsher 
1793874aeea5SJeff Kirsher /* Context: process, rtnl_lock() held. */
1794874aeea5SJeff Kirsher static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
1795874aeea5SJeff Kirsher {
1796874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
1797874aeea5SJeff Kirsher 	int rc = 0;
1798874aeea5SJeff Kirsher 
1799874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
1800874aeea5SJeff Kirsher 
1801874aeea5SJeff Kirsher 	if (new_mtu > EFX_MAX_MTU)
1802874aeea5SJeff Kirsher 		return -EINVAL;
1803874aeea5SJeff Kirsher 
1804874aeea5SJeff Kirsher 	efx_stop_all(efx);
1805874aeea5SJeff Kirsher 
1806874aeea5SJeff Kirsher 	netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
1807874aeea5SJeff Kirsher 
1808874aeea5SJeff Kirsher 	efx_fini_channels(efx);
1809874aeea5SJeff Kirsher 
1810874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
1811874aeea5SJeff Kirsher 	/* Reconfigure the MAC before enabling the dma queues so that
1812874aeea5SJeff Kirsher 	 * the RX buffers don't overflow */
1813874aeea5SJeff Kirsher 	net_dev->mtu = new_mtu;
1814710b208dSBen Hutchings 	efx->type->reconfigure_mac(efx);
1815874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
1816874aeea5SJeff Kirsher 
1817874aeea5SJeff Kirsher 	efx_init_channels(efx);
1818874aeea5SJeff Kirsher 
1819874aeea5SJeff Kirsher 	efx_start_all(efx);
1820874aeea5SJeff Kirsher 	return rc;
1821874aeea5SJeff Kirsher }
1822874aeea5SJeff Kirsher 
1823874aeea5SJeff Kirsher static int efx_set_mac_address(struct net_device *net_dev, void *data)
1824874aeea5SJeff Kirsher {
1825874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
1826874aeea5SJeff Kirsher 	struct sockaddr *addr = data;
1827874aeea5SJeff Kirsher 	char *new_addr = addr->sa_data;
1828874aeea5SJeff Kirsher 
1829874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
1830874aeea5SJeff Kirsher 
1831874aeea5SJeff Kirsher 	if (!is_valid_ether_addr(new_addr)) {
1832874aeea5SJeff Kirsher 		netif_err(efx, drv, efx->net_dev,
1833874aeea5SJeff Kirsher 			  "invalid ethernet MAC address requested: %pM\n",
1834874aeea5SJeff Kirsher 			  new_addr);
1835874aeea5SJeff Kirsher 		return -EINVAL;
1836874aeea5SJeff Kirsher 	}
1837874aeea5SJeff Kirsher 
1838874aeea5SJeff Kirsher 	memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
1839874aeea5SJeff Kirsher 
1840874aeea5SJeff Kirsher 	/* Reconfigure the MAC */
1841874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
1842710b208dSBen Hutchings 	efx->type->reconfigure_mac(efx);
1843874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
1844874aeea5SJeff Kirsher 
1845874aeea5SJeff Kirsher 	return 0;
1846874aeea5SJeff Kirsher }
1847874aeea5SJeff Kirsher 
1848874aeea5SJeff Kirsher /* Context: netif_addr_lock held, BHs disabled. */
1849874aeea5SJeff Kirsher static void efx_set_multicast_list(struct net_device *net_dev)
1850874aeea5SJeff Kirsher {
1851874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
1852874aeea5SJeff Kirsher 	struct netdev_hw_addr *ha;
1853874aeea5SJeff Kirsher 	union efx_multicast_hash *mc_hash = &efx->multicast_hash;
1854874aeea5SJeff Kirsher 	u32 crc;
1855874aeea5SJeff Kirsher 	int bit;
1856874aeea5SJeff Kirsher 
1857874aeea5SJeff Kirsher 	efx->promiscuous = !!(net_dev->flags & IFF_PROMISC);
1858874aeea5SJeff Kirsher 
1859874aeea5SJeff Kirsher 	/* Build multicast hash table */
1860874aeea5SJeff Kirsher 	if (efx->promiscuous || (net_dev->flags & IFF_ALLMULTI)) {
1861874aeea5SJeff Kirsher 		memset(mc_hash, 0xff, sizeof(*mc_hash));
1862874aeea5SJeff Kirsher 	} else {
1863874aeea5SJeff Kirsher 		memset(mc_hash, 0x00, sizeof(*mc_hash));
1864874aeea5SJeff Kirsher 		netdev_for_each_mc_addr(ha, net_dev) {
1865874aeea5SJeff Kirsher 			crc = ether_crc_le(ETH_ALEN, ha->addr);
1866874aeea5SJeff Kirsher 			bit = crc & (EFX_MCAST_HASH_ENTRIES - 1);
1867874aeea5SJeff Kirsher 			set_bit_le(bit, mc_hash->byte);
1868874aeea5SJeff Kirsher 		}
1869874aeea5SJeff Kirsher 
1870874aeea5SJeff Kirsher 		/* Broadcast packets go through the multicast hash filter.
1871874aeea5SJeff Kirsher 		 * ether_crc_le() of the broadcast address is 0xbe2612ff
1872874aeea5SJeff Kirsher 		 * so we always add bit 0xff to the mask.
1873874aeea5SJeff Kirsher 		 */
1874874aeea5SJeff Kirsher 		set_bit_le(0xff, mc_hash->byte);
1875874aeea5SJeff Kirsher 	}
1876874aeea5SJeff Kirsher 
1877874aeea5SJeff Kirsher 	if (efx->port_enabled)
1878874aeea5SJeff Kirsher 		queue_work(efx->workqueue, &efx->mac_work);
1879874aeea5SJeff Kirsher 	/* Otherwise efx_start_port() will do this */
1880874aeea5SJeff Kirsher }
1881874aeea5SJeff Kirsher 
1882c8f44affSMichał Mirosław static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
1883874aeea5SJeff Kirsher {
1884874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
1885874aeea5SJeff Kirsher 
1886874aeea5SJeff Kirsher 	/* If disabling RX n-tuple filtering, clear existing filters */
1887874aeea5SJeff Kirsher 	if (net_dev->features & ~data & NETIF_F_NTUPLE)
1888874aeea5SJeff Kirsher 		efx_filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
1889874aeea5SJeff Kirsher 
1890874aeea5SJeff Kirsher 	return 0;
1891874aeea5SJeff Kirsher }
1892874aeea5SJeff Kirsher 
1893874aeea5SJeff Kirsher static const struct net_device_ops efx_netdev_ops = {
1894874aeea5SJeff Kirsher 	.ndo_open		= efx_net_open,
1895874aeea5SJeff Kirsher 	.ndo_stop		= efx_net_stop,
1896874aeea5SJeff Kirsher 	.ndo_get_stats64	= efx_net_stats,
1897874aeea5SJeff Kirsher 	.ndo_tx_timeout		= efx_watchdog,
1898874aeea5SJeff Kirsher 	.ndo_start_xmit		= efx_hard_start_xmit,
1899874aeea5SJeff Kirsher 	.ndo_validate_addr	= eth_validate_addr,
1900874aeea5SJeff Kirsher 	.ndo_do_ioctl		= efx_ioctl,
1901874aeea5SJeff Kirsher 	.ndo_change_mtu		= efx_change_mtu,
1902874aeea5SJeff Kirsher 	.ndo_set_mac_address	= efx_set_mac_address,
1903afc4b13dSJiri Pirko 	.ndo_set_rx_mode	= efx_set_multicast_list,
1904874aeea5SJeff Kirsher 	.ndo_set_features	= efx_set_features,
1905874aeea5SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
1906874aeea5SJeff Kirsher 	.ndo_poll_controller = efx_netpoll,
1907874aeea5SJeff Kirsher #endif
1908874aeea5SJeff Kirsher 	.ndo_setup_tc		= efx_setup_tc,
1909874aeea5SJeff Kirsher #ifdef CONFIG_RFS_ACCEL
1910874aeea5SJeff Kirsher 	.ndo_rx_flow_steer	= efx_filter_rfs,
1911874aeea5SJeff Kirsher #endif
1912874aeea5SJeff Kirsher };
1913874aeea5SJeff Kirsher 
1914874aeea5SJeff Kirsher static void efx_update_name(struct efx_nic *efx)
1915874aeea5SJeff Kirsher {
1916874aeea5SJeff Kirsher 	strcpy(efx->name, efx->net_dev->name);
1917874aeea5SJeff Kirsher 	efx_mtd_rename(efx);
1918874aeea5SJeff Kirsher 	efx_set_channel_names(efx);
1919874aeea5SJeff Kirsher }
1920874aeea5SJeff Kirsher 
1921874aeea5SJeff Kirsher static int efx_netdev_event(struct notifier_block *this,
1922874aeea5SJeff Kirsher 			    unsigned long event, void *ptr)
1923874aeea5SJeff Kirsher {
1924874aeea5SJeff Kirsher 	struct net_device *net_dev = ptr;
1925874aeea5SJeff Kirsher 
1926874aeea5SJeff Kirsher 	if (net_dev->netdev_ops == &efx_netdev_ops &&
1927874aeea5SJeff Kirsher 	    event == NETDEV_CHANGENAME)
1928874aeea5SJeff Kirsher 		efx_update_name(netdev_priv(net_dev));
1929874aeea5SJeff Kirsher 
1930874aeea5SJeff Kirsher 	return NOTIFY_DONE;
1931874aeea5SJeff Kirsher }
1932874aeea5SJeff Kirsher 
1933874aeea5SJeff Kirsher static struct notifier_block efx_netdev_notifier = {
1934874aeea5SJeff Kirsher 	.notifier_call = efx_netdev_event,
1935874aeea5SJeff Kirsher };
1936874aeea5SJeff Kirsher 
1937874aeea5SJeff Kirsher static ssize_t
1938874aeea5SJeff Kirsher show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
1939874aeea5SJeff Kirsher {
1940874aeea5SJeff Kirsher 	struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
1941874aeea5SJeff Kirsher 	return sprintf(buf, "%d\n", efx->phy_type);
1942874aeea5SJeff Kirsher }
1943874aeea5SJeff Kirsher static DEVICE_ATTR(phy_type, 0644, show_phy_type, NULL);
1944874aeea5SJeff Kirsher 
1945874aeea5SJeff Kirsher static int efx_register_netdev(struct efx_nic *efx)
1946874aeea5SJeff Kirsher {
1947874aeea5SJeff Kirsher 	struct net_device *net_dev = efx->net_dev;
1948874aeea5SJeff Kirsher 	struct efx_channel *channel;
1949874aeea5SJeff Kirsher 	int rc;
1950874aeea5SJeff Kirsher 
1951874aeea5SJeff Kirsher 	net_dev->watchdog_timeo = 5 * HZ;
1952874aeea5SJeff Kirsher 	net_dev->irq = efx->pci_dev->irq;
1953874aeea5SJeff Kirsher 	net_dev->netdev_ops = &efx_netdev_ops;
1954874aeea5SJeff Kirsher 	SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
1955874aeea5SJeff Kirsher 
1956874aeea5SJeff Kirsher 	rtnl_lock();
1957874aeea5SJeff Kirsher 
1958874aeea5SJeff Kirsher 	rc = dev_alloc_name(net_dev, net_dev->name);
1959874aeea5SJeff Kirsher 	if (rc < 0)
1960874aeea5SJeff Kirsher 		goto fail_locked;
1961874aeea5SJeff Kirsher 	efx_update_name(efx);
1962874aeea5SJeff Kirsher 
1963874aeea5SJeff Kirsher 	rc = register_netdevice(net_dev);
1964874aeea5SJeff Kirsher 	if (rc)
1965874aeea5SJeff Kirsher 		goto fail_locked;
1966874aeea5SJeff Kirsher 
1967874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx) {
1968874aeea5SJeff Kirsher 		struct efx_tx_queue *tx_queue;
1969874aeea5SJeff Kirsher 		efx_for_each_channel_tx_queue(tx_queue, channel)
1970874aeea5SJeff Kirsher 			efx_init_tx_queue_core_txq(tx_queue);
1971874aeea5SJeff Kirsher 	}
1972874aeea5SJeff Kirsher 
1973874aeea5SJeff Kirsher 	/* Always start with carrier off; PHY events will detect the link */
197486ee5302SBen Hutchings 	netif_carrier_off(net_dev);
1975874aeea5SJeff Kirsher 
1976874aeea5SJeff Kirsher 	rtnl_unlock();
1977874aeea5SJeff Kirsher 
1978874aeea5SJeff Kirsher 	rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
1979874aeea5SJeff Kirsher 	if (rc) {
1980874aeea5SJeff Kirsher 		netif_err(efx, drv, efx->net_dev,
1981874aeea5SJeff Kirsher 			  "failed to init net dev attributes\n");
1982874aeea5SJeff Kirsher 		goto fail_registered;
1983874aeea5SJeff Kirsher 	}
1984874aeea5SJeff Kirsher 
1985874aeea5SJeff Kirsher 	return 0;
1986874aeea5SJeff Kirsher 
1987874aeea5SJeff Kirsher fail_locked:
1988874aeea5SJeff Kirsher 	rtnl_unlock();
1989874aeea5SJeff Kirsher 	netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
1990874aeea5SJeff Kirsher 	return rc;
1991874aeea5SJeff Kirsher 
1992874aeea5SJeff Kirsher fail_registered:
1993874aeea5SJeff Kirsher 	unregister_netdev(net_dev);
1994874aeea5SJeff Kirsher 	return rc;
1995874aeea5SJeff Kirsher }
1996874aeea5SJeff Kirsher 
1997874aeea5SJeff Kirsher static void efx_unregister_netdev(struct efx_nic *efx)
1998874aeea5SJeff Kirsher {
1999874aeea5SJeff Kirsher 	struct efx_channel *channel;
2000874aeea5SJeff Kirsher 	struct efx_tx_queue *tx_queue;
2001874aeea5SJeff Kirsher 
2002874aeea5SJeff Kirsher 	if (!efx->net_dev)
2003874aeea5SJeff Kirsher 		return;
2004874aeea5SJeff Kirsher 
2005874aeea5SJeff Kirsher 	BUG_ON(netdev_priv(efx->net_dev) != efx);
2006874aeea5SJeff Kirsher 
2007874aeea5SJeff Kirsher 	/* Free up any skbs still remaining. This has to happen before
2008874aeea5SJeff Kirsher 	 * we try to unregister the netdev as running their destructors
2009874aeea5SJeff Kirsher 	 * may be needed to get the device ref. count to 0. */
2010874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx) {
2011874aeea5SJeff Kirsher 		efx_for_each_channel_tx_queue(tx_queue, channel)
2012874aeea5SJeff Kirsher 			efx_release_tx_buffers(tx_queue);
2013874aeea5SJeff Kirsher 	}
2014874aeea5SJeff Kirsher 
2015874aeea5SJeff Kirsher 	strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2016874aeea5SJeff Kirsher 	device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2017874aeea5SJeff Kirsher 	unregister_netdev(efx->net_dev);
2018874aeea5SJeff Kirsher }
2019874aeea5SJeff Kirsher 
2020874aeea5SJeff Kirsher /**************************************************************************
2021874aeea5SJeff Kirsher  *
2022874aeea5SJeff Kirsher  * Device reset and suspend
2023874aeea5SJeff Kirsher  *
2024874aeea5SJeff Kirsher  **************************************************************************/
2025874aeea5SJeff Kirsher 
2026874aeea5SJeff Kirsher /* Tears down the entire software state and most of the hardware state
2027874aeea5SJeff Kirsher  * before reset.  */
2028874aeea5SJeff Kirsher void efx_reset_down(struct efx_nic *efx, enum reset_type method)
2029874aeea5SJeff Kirsher {
2030874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
2031874aeea5SJeff Kirsher 
2032874aeea5SJeff Kirsher 	efx_stop_all(efx);
2033874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
2034874aeea5SJeff Kirsher 
2035874aeea5SJeff Kirsher 	efx_fini_channels(efx);
2036874aeea5SJeff Kirsher 	if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
2037874aeea5SJeff Kirsher 		efx->phy_op->fini(efx);
2038874aeea5SJeff Kirsher 	efx->type->fini(efx);
2039874aeea5SJeff Kirsher }
2040874aeea5SJeff Kirsher 
2041874aeea5SJeff Kirsher /* This function will always ensure that the locks acquired in
2042874aeea5SJeff Kirsher  * efx_reset_down() are released. A failure return code indicates
2043874aeea5SJeff Kirsher  * that we were unable to reinitialise the hardware, and the
2044874aeea5SJeff Kirsher  * driver should be disabled. If ok is false, then the rx and tx
2045874aeea5SJeff Kirsher  * engines are not restarted, pending a RESET_DISABLE. */
2046874aeea5SJeff Kirsher int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
2047874aeea5SJeff Kirsher {
2048874aeea5SJeff Kirsher 	int rc;
2049874aeea5SJeff Kirsher 
2050874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
2051874aeea5SJeff Kirsher 
2052874aeea5SJeff Kirsher 	rc = efx->type->init(efx);
2053874aeea5SJeff Kirsher 	if (rc) {
2054874aeea5SJeff Kirsher 		netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
2055874aeea5SJeff Kirsher 		goto fail;
2056874aeea5SJeff Kirsher 	}
2057874aeea5SJeff Kirsher 
2058874aeea5SJeff Kirsher 	if (!ok)
2059874aeea5SJeff Kirsher 		goto fail;
2060874aeea5SJeff Kirsher 
2061874aeea5SJeff Kirsher 	if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
2062874aeea5SJeff Kirsher 		rc = efx->phy_op->init(efx);
2063874aeea5SJeff Kirsher 		if (rc)
2064874aeea5SJeff Kirsher 			goto fail;
2065874aeea5SJeff Kirsher 		if (efx->phy_op->reconfigure(efx))
2066874aeea5SJeff Kirsher 			netif_err(efx, drv, efx->net_dev,
2067874aeea5SJeff Kirsher 				  "could not restore PHY settings\n");
2068874aeea5SJeff Kirsher 	}
2069874aeea5SJeff Kirsher 
2070710b208dSBen Hutchings 	efx->type->reconfigure_mac(efx);
2071874aeea5SJeff Kirsher 
2072874aeea5SJeff Kirsher 	efx_init_channels(efx);
2073874aeea5SJeff Kirsher 	efx_restore_filters(efx);
2074874aeea5SJeff Kirsher 
2075874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
2076874aeea5SJeff Kirsher 
2077874aeea5SJeff Kirsher 	efx_start_all(efx);
2078874aeea5SJeff Kirsher 
2079874aeea5SJeff Kirsher 	return 0;
2080874aeea5SJeff Kirsher 
2081874aeea5SJeff Kirsher fail:
2082874aeea5SJeff Kirsher 	efx->port_initialized = false;
2083874aeea5SJeff Kirsher 
2084874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
2085874aeea5SJeff Kirsher 
2086874aeea5SJeff Kirsher 	return rc;
2087874aeea5SJeff Kirsher }
2088874aeea5SJeff Kirsher 
2089874aeea5SJeff Kirsher /* Reset the NIC using the specified method.  Note that the reset may
2090874aeea5SJeff Kirsher  * fail, in which case the card will be left in an unusable state.
2091874aeea5SJeff Kirsher  *
2092874aeea5SJeff Kirsher  * Caller must hold the rtnl_lock.
2093874aeea5SJeff Kirsher  */
2094874aeea5SJeff Kirsher int efx_reset(struct efx_nic *efx, enum reset_type method)
2095874aeea5SJeff Kirsher {
2096874aeea5SJeff Kirsher 	int rc, rc2;
2097874aeea5SJeff Kirsher 	bool disabled;
2098874aeea5SJeff Kirsher 
2099874aeea5SJeff Kirsher 	netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2100874aeea5SJeff Kirsher 		   RESET_TYPE(method));
2101874aeea5SJeff Kirsher 
2102874aeea5SJeff Kirsher 	netif_device_detach(efx->net_dev);
2103874aeea5SJeff Kirsher 	efx_reset_down(efx, method);
2104874aeea5SJeff Kirsher 
2105874aeea5SJeff Kirsher 	rc = efx->type->reset(efx, method);
2106874aeea5SJeff Kirsher 	if (rc) {
2107874aeea5SJeff Kirsher 		netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
2108874aeea5SJeff Kirsher 		goto out;
2109874aeea5SJeff Kirsher 	}
2110874aeea5SJeff Kirsher 
2111874aeea5SJeff Kirsher 	/* Clear flags for the scopes we covered.  We assume the NIC and
2112874aeea5SJeff Kirsher 	 * driver are now quiescent so that there is no race here.
2113874aeea5SJeff Kirsher 	 */
2114874aeea5SJeff Kirsher 	efx->reset_pending &= -(1 << (method + 1));
2115874aeea5SJeff Kirsher 
2116874aeea5SJeff Kirsher 	/* Reinitialise bus-mastering, which may have been turned off before
2117874aeea5SJeff Kirsher 	 * the reset was scheduled. This is still appropriate, even in the
2118874aeea5SJeff Kirsher 	 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2119874aeea5SJeff Kirsher 	 * can respond to requests. */
2120874aeea5SJeff Kirsher 	pci_set_master(efx->pci_dev);
2121874aeea5SJeff Kirsher 
2122874aeea5SJeff Kirsher out:
2123874aeea5SJeff Kirsher 	/* Leave device stopped if necessary */
2124874aeea5SJeff Kirsher 	disabled = rc || method == RESET_TYPE_DISABLE;
2125874aeea5SJeff Kirsher 	rc2 = efx_reset_up(efx, method, !disabled);
2126874aeea5SJeff Kirsher 	if (rc2) {
2127874aeea5SJeff Kirsher 		disabled = true;
2128874aeea5SJeff Kirsher 		if (!rc)
2129874aeea5SJeff Kirsher 			rc = rc2;
2130874aeea5SJeff Kirsher 	}
2131874aeea5SJeff Kirsher 
2132874aeea5SJeff Kirsher 	if (disabled) {
2133874aeea5SJeff Kirsher 		dev_close(efx->net_dev);
2134874aeea5SJeff Kirsher 		netif_err(efx, drv, efx->net_dev, "has been disabled\n");
2135874aeea5SJeff Kirsher 		efx->state = STATE_DISABLED;
2136874aeea5SJeff Kirsher 	} else {
2137874aeea5SJeff Kirsher 		netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
2138874aeea5SJeff Kirsher 		netif_device_attach(efx->net_dev);
2139874aeea5SJeff Kirsher 	}
2140874aeea5SJeff Kirsher 	return rc;
2141874aeea5SJeff Kirsher }
2142874aeea5SJeff Kirsher 
2143874aeea5SJeff Kirsher /* The worker thread exists so that code that cannot sleep can
2144874aeea5SJeff Kirsher  * schedule a reset for later.
2145874aeea5SJeff Kirsher  */
2146874aeea5SJeff Kirsher static void efx_reset_work(struct work_struct *data)
2147874aeea5SJeff Kirsher {
2148874aeea5SJeff Kirsher 	struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
2149874aeea5SJeff Kirsher 	unsigned long pending = ACCESS_ONCE(efx->reset_pending);
2150874aeea5SJeff Kirsher 
2151874aeea5SJeff Kirsher 	if (!pending)
2152874aeea5SJeff Kirsher 		return;
2153874aeea5SJeff Kirsher 
2154874aeea5SJeff Kirsher 	/* If we're not RUNNING then don't reset. Leave the reset_pending
2155874aeea5SJeff Kirsher 	 * flags set so that efx_pci_probe_main will be retried */
2156874aeea5SJeff Kirsher 	if (efx->state != STATE_RUNNING) {
2157874aeea5SJeff Kirsher 		netif_info(efx, drv, efx->net_dev,
2158874aeea5SJeff Kirsher 			   "scheduled reset quenched. NIC not RUNNING\n");
2159874aeea5SJeff Kirsher 		return;
2160874aeea5SJeff Kirsher 	}
2161874aeea5SJeff Kirsher 
2162874aeea5SJeff Kirsher 	rtnl_lock();
2163874aeea5SJeff Kirsher 	(void)efx_reset(efx, fls(pending) - 1);
2164874aeea5SJeff Kirsher 	rtnl_unlock();
2165874aeea5SJeff Kirsher }
2166874aeea5SJeff Kirsher 
2167874aeea5SJeff Kirsher void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2168874aeea5SJeff Kirsher {
2169874aeea5SJeff Kirsher 	enum reset_type method;
2170874aeea5SJeff Kirsher 
2171874aeea5SJeff Kirsher 	switch (type) {
2172874aeea5SJeff Kirsher 	case RESET_TYPE_INVISIBLE:
2173874aeea5SJeff Kirsher 	case RESET_TYPE_ALL:
2174874aeea5SJeff Kirsher 	case RESET_TYPE_WORLD:
2175874aeea5SJeff Kirsher 	case RESET_TYPE_DISABLE:
2176874aeea5SJeff Kirsher 		method = type;
2177874aeea5SJeff Kirsher 		netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2178874aeea5SJeff Kirsher 			  RESET_TYPE(method));
2179874aeea5SJeff Kirsher 		break;
2180874aeea5SJeff Kirsher 	default:
2181874aeea5SJeff Kirsher 		method = efx->type->map_reset_reason(type);
2182874aeea5SJeff Kirsher 		netif_dbg(efx, drv, efx->net_dev,
2183874aeea5SJeff Kirsher 			  "scheduling %s reset for %s\n",
2184874aeea5SJeff Kirsher 			  RESET_TYPE(method), RESET_TYPE(type));
2185874aeea5SJeff Kirsher 		break;
2186874aeea5SJeff Kirsher 	}
2187874aeea5SJeff Kirsher 
2188874aeea5SJeff Kirsher 	set_bit(method, &efx->reset_pending);
2189874aeea5SJeff Kirsher 
2190874aeea5SJeff Kirsher 	/* efx_process_channel() will no longer read events once a
2191874aeea5SJeff Kirsher 	 * reset is scheduled. So switch back to poll'd MCDI completions. */
2192874aeea5SJeff Kirsher 	efx_mcdi_mode_poll(efx);
2193874aeea5SJeff Kirsher 
2194874aeea5SJeff Kirsher 	queue_work(reset_workqueue, &efx->reset_work);
2195874aeea5SJeff Kirsher }
2196874aeea5SJeff Kirsher 
2197874aeea5SJeff Kirsher /**************************************************************************
2198874aeea5SJeff Kirsher  *
2199874aeea5SJeff Kirsher  * List of NICs we support
2200874aeea5SJeff Kirsher  *
2201874aeea5SJeff Kirsher  **************************************************************************/
2202874aeea5SJeff Kirsher 
2203874aeea5SJeff Kirsher /* PCI device ID table */
2204874aeea5SJeff Kirsher static DEFINE_PCI_DEVICE_TABLE(efx_pci_table) = {
22050e59e7e7SLinus Torvalds 	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
22060e59e7e7SLinus Torvalds 		    PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
2207874aeea5SJeff Kirsher 	 .driver_data = (unsigned long) &falcon_a1_nic_type},
22080e59e7e7SLinus Torvalds 	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
22090e59e7e7SLinus Torvalds 		    PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
2210874aeea5SJeff Kirsher 	 .driver_data = (unsigned long) &falcon_b0_nic_type},
2211547c474fSBen Hutchings 	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803),	/* SFC9020 */
2212874aeea5SJeff Kirsher 	 .driver_data = (unsigned long) &siena_a0_nic_type},
2213547c474fSBen Hutchings 	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813),	/* SFL9021 */
2214874aeea5SJeff Kirsher 	 .driver_data = (unsigned long) &siena_a0_nic_type},
2215874aeea5SJeff Kirsher 	{0}			/* end of list */
2216874aeea5SJeff Kirsher };
2217874aeea5SJeff Kirsher 
2218874aeea5SJeff Kirsher /**************************************************************************
2219874aeea5SJeff Kirsher  *
2220874aeea5SJeff Kirsher  * Dummy PHY/MAC operations
2221874aeea5SJeff Kirsher  *
2222874aeea5SJeff Kirsher  * Can be used for some unimplemented operations
2223874aeea5SJeff Kirsher  * Needed so all function pointers are valid and do not have to be tested
2224874aeea5SJeff Kirsher  * before use
2225874aeea5SJeff Kirsher  *
2226874aeea5SJeff Kirsher  **************************************************************************/
2227874aeea5SJeff Kirsher int efx_port_dummy_op_int(struct efx_nic *efx)
2228874aeea5SJeff Kirsher {
2229874aeea5SJeff Kirsher 	return 0;
2230874aeea5SJeff Kirsher }
2231874aeea5SJeff Kirsher void efx_port_dummy_op_void(struct efx_nic *efx) {}
2232874aeea5SJeff Kirsher 
2233874aeea5SJeff Kirsher static bool efx_port_dummy_op_poll(struct efx_nic *efx)
2234874aeea5SJeff Kirsher {
2235874aeea5SJeff Kirsher 	return false;
2236874aeea5SJeff Kirsher }
2237874aeea5SJeff Kirsher 
2238874aeea5SJeff Kirsher static const struct efx_phy_operations efx_dummy_phy_operations = {
2239874aeea5SJeff Kirsher 	.init		 = efx_port_dummy_op_int,
2240874aeea5SJeff Kirsher 	.reconfigure	 = efx_port_dummy_op_int,
2241874aeea5SJeff Kirsher 	.poll		 = efx_port_dummy_op_poll,
2242874aeea5SJeff Kirsher 	.fini		 = efx_port_dummy_op_void,
2243874aeea5SJeff Kirsher };
2244874aeea5SJeff Kirsher 
2245874aeea5SJeff Kirsher /**************************************************************************
2246874aeea5SJeff Kirsher  *
2247874aeea5SJeff Kirsher  * Data housekeeping
2248874aeea5SJeff Kirsher  *
2249874aeea5SJeff Kirsher  **************************************************************************/
2250874aeea5SJeff Kirsher 
2251874aeea5SJeff Kirsher /* This zeroes out and then fills in the invariants in a struct
2252874aeea5SJeff Kirsher  * efx_nic (including all sub-structures).
2253874aeea5SJeff Kirsher  */
2254874aeea5SJeff Kirsher static int efx_init_struct(struct efx_nic *efx, const struct efx_nic_type *type,
2255874aeea5SJeff Kirsher 			   struct pci_dev *pci_dev, struct net_device *net_dev)
2256874aeea5SJeff Kirsher {
2257874aeea5SJeff Kirsher 	int i;
2258874aeea5SJeff Kirsher 
2259874aeea5SJeff Kirsher 	/* Initialise common structures */
2260874aeea5SJeff Kirsher 	memset(efx, 0, sizeof(*efx));
2261874aeea5SJeff Kirsher 	spin_lock_init(&efx->biu_lock);
2262874aeea5SJeff Kirsher #ifdef CONFIG_SFC_MTD
2263874aeea5SJeff Kirsher 	INIT_LIST_HEAD(&efx->mtd_list);
2264874aeea5SJeff Kirsher #endif
2265874aeea5SJeff Kirsher 	INIT_WORK(&efx->reset_work, efx_reset_work);
2266874aeea5SJeff Kirsher 	INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
2267874aeea5SJeff Kirsher 	efx->pci_dev = pci_dev;
2268874aeea5SJeff Kirsher 	efx->msg_enable = debug;
2269874aeea5SJeff Kirsher 	efx->state = STATE_INIT;
2270874aeea5SJeff Kirsher 	strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
2271874aeea5SJeff Kirsher 
2272874aeea5SJeff Kirsher 	efx->net_dev = net_dev;
2273874aeea5SJeff Kirsher 	spin_lock_init(&efx->stats_lock);
2274874aeea5SJeff Kirsher 	mutex_init(&efx->mac_lock);
2275874aeea5SJeff Kirsher 	efx->phy_op = &efx_dummy_phy_operations;
2276874aeea5SJeff Kirsher 	efx->mdio.dev = net_dev;
2277874aeea5SJeff Kirsher 	INIT_WORK(&efx->mac_work, efx_mac_work);
2278874aeea5SJeff Kirsher 
2279874aeea5SJeff Kirsher 	for (i = 0; i < EFX_MAX_CHANNELS; i++) {
2280874aeea5SJeff Kirsher 		efx->channel[i] = efx_alloc_channel(efx, i, NULL);
2281874aeea5SJeff Kirsher 		if (!efx->channel[i])
2282874aeea5SJeff Kirsher 			goto fail;
2283874aeea5SJeff Kirsher 	}
2284874aeea5SJeff Kirsher 
2285874aeea5SJeff Kirsher 	efx->type = type;
2286874aeea5SJeff Kirsher 
2287874aeea5SJeff Kirsher 	EFX_BUG_ON_PARANOID(efx->type->phys_addr_channels > EFX_MAX_CHANNELS);
2288874aeea5SJeff Kirsher 
2289874aeea5SJeff Kirsher 	/* Higher numbered interrupt modes are less capable! */
2290874aeea5SJeff Kirsher 	efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2291874aeea5SJeff Kirsher 				  interrupt_mode);
2292874aeea5SJeff Kirsher 
2293874aeea5SJeff Kirsher 	/* Would be good to use the net_dev name, but we're too early */
2294874aeea5SJeff Kirsher 	snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2295874aeea5SJeff Kirsher 		 pci_name(pci_dev));
2296874aeea5SJeff Kirsher 	efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
2297874aeea5SJeff Kirsher 	if (!efx->workqueue)
2298874aeea5SJeff Kirsher 		goto fail;
2299874aeea5SJeff Kirsher 
2300874aeea5SJeff Kirsher 	return 0;
2301874aeea5SJeff Kirsher 
2302874aeea5SJeff Kirsher fail:
2303874aeea5SJeff Kirsher 	efx_fini_struct(efx);
2304874aeea5SJeff Kirsher 	return -ENOMEM;
2305874aeea5SJeff Kirsher }
2306874aeea5SJeff Kirsher 
2307874aeea5SJeff Kirsher static void efx_fini_struct(struct efx_nic *efx)
2308874aeea5SJeff Kirsher {
2309874aeea5SJeff Kirsher 	int i;
2310874aeea5SJeff Kirsher 
2311874aeea5SJeff Kirsher 	for (i = 0; i < EFX_MAX_CHANNELS; i++)
2312874aeea5SJeff Kirsher 		kfree(efx->channel[i]);
2313874aeea5SJeff Kirsher 
2314874aeea5SJeff Kirsher 	if (efx->workqueue) {
2315874aeea5SJeff Kirsher 		destroy_workqueue(efx->workqueue);
2316874aeea5SJeff Kirsher 		efx->workqueue = NULL;
2317874aeea5SJeff Kirsher 	}
2318874aeea5SJeff Kirsher }
2319874aeea5SJeff Kirsher 
2320874aeea5SJeff Kirsher /**************************************************************************
2321874aeea5SJeff Kirsher  *
2322874aeea5SJeff Kirsher  * PCI interface
2323874aeea5SJeff Kirsher  *
2324874aeea5SJeff Kirsher  **************************************************************************/
2325874aeea5SJeff Kirsher 
2326874aeea5SJeff Kirsher /* Main body of final NIC shutdown code
2327874aeea5SJeff Kirsher  * This is called only at module unload (or hotplug removal).
2328874aeea5SJeff Kirsher  */
2329874aeea5SJeff Kirsher static void efx_pci_remove_main(struct efx_nic *efx)
2330874aeea5SJeff Kirsher {
2331874aeea5SJeff Kirsher #ifdef CONFIG_RFS_ACCEL
2332874aeea5SJeff Kirsher 	free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
2333874aeea5SJeff Kirsher 	efx->net_dev->rx_cpu_rmap = NULL;
2334874aeea5SJeff Kirsher #endif
2335874aeea5SJeff Kirsher 	efx_nic_fini_interrupt(efx);
2336874aeea5SJeff Kirsher 	efx_fini_channels(efx);
2337874aeea5SJeff Kirsher 	efx_fini_port(efx);
2338874aeea5SJeff Kirsher 	efx->type->fini(efx);
2339874aeea5SJeff Kirsher 	efx_fini_napi(efx);
2340874aeea5SJeff Kirsher 	efx_remove_all(efx);
2341874aeea5SJeff Kirsher }
2342874aeea5SJeff Kirsher 
2343874aeea5SJeff Kirsher /* Final NIC shutdown
2344874aeea5SJeff Kirsher  * This is called only at module unload (or hotplug removal).
2345874aeea5SJeff Kirsher  */
2346874aeea5SJeff Kirsher static void efx_pci_remove(struct pci_dev *pci_dev)
2347874aeea5SJeff Kirsher {
2348874aeea5SJeff Kirsher 	struct efx_nic *efx;
2349874aeea5SJeff Kirsher 
2350874aeea5SJeff Kirsher 	efx = pci_get_drvdata(pci_dev);
2351874aeea5SJeff Kirsher 	if (!efx)
2352874aeea5SJeff Kirsher 		return;
2353874aeea5SJeff Kirsher 
2354874aeea5SJeff Kirsher 	/* Mark the NIC as fini, then stop the interface */
2355874aeea5SJeff Kirsher 	rtnl_lock();
2356874aeea5SJeff Kirsher 	efx->state = STATE_FINI;
2357874aeea5SJeff Kirsher 	dev_close(efx->net_dev);
2358874aeea5SJeff Kirsher 
2359874aeea5SJeff Kirsher 	/* Allow any queued efx_resets() to complete */
2360874aeea5SJeff Kirsher 	rtnl_unlock();
2361874aeea5SJeff Kirsher 
2362874aeea5SJeff Kirsher 	efx_unregister_netdev(efx);
2363874aeea5SJeff Kirsher 
2364874aeea5SJeff Kirsher 	efx_mtd_remove(efx);
2365874aeea5SJeff Kirsher 
2366874aeea5SJeff Kirsher 	/* Wait for any scheduled resets to complete. No more will be
2367874aeea5SJeff Kirsher 	 * scheduled from this point because efx_stop_all() has been
2368874aeea5SJeff Kirsher 	 * called, we are no longer registered with driverlink, and
2369874aeea5SJeff Kirsher 	 * the net_device's have been removed. */
2370874aeea5SJeff Kirsher 	cancel_work_sync(&efx->reset_work);
2371874aeea5SJeff Kirsher 
2372874aeea5SJeff Kirsher 	efx_pci_remove_main(efx);
2373874aeea5SJeff Kirsher 
2374874aeea5SJeff Kirsher 	efx_fini_io(efx);
2375874aeea5SJeff Kirsher 	netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
2376874aeea5SJeff Kirsher 
2377874aeea5SJeff Kirsher 	pci_set_drvdata(pci_dev, NULL);
2378874aeea5SJeff Kirsher 	efx_fini_struct(efx);
2379874aeea5SJeff Kirsher 	free_netdev(efx->net_dev);
2380874aeea5SJeff Kirsher };
2381874aeea5SJeff Kirsher 
2382874aeea5SJeff Kirsher /* Main body of NIC initialisation
2383874aeea5SJeff Kirsher  * This is called at module load (or hotplug insertion, theoretically).
2384874aeea5SJeff Kirsher  */
2385874aeea5SJeff Kirsher static int efx_pci_probe_main(struct efx_nic *efx)
2386874aeea5SJeff Kirsher {
2387874aeea5SJeff Kirsher 	int rc;
2388874aeea5SJeff Kirsher 
2389874aeea5SJeff Kirsher 	/* Do start-of-day initialisation */
2390874aeea5SJeff Kirsher 	rc = efx_probe_all(efx);
2391874aeea5SJeff Kirsher 	if (rc)
2392874aeea5SJeff Kirsher 		goto fail1;
2393874aeea5SJeff Kirsher 
2394874aeea5SJeff Kirsher 	efx_init_napi(efx);
2395874aeea5SJeff Kirsher 
2396874aeea5SJeff Kirsher 	rc = efx->type->init(efx);
2397874aeea5SJeff Kirsher 	if (rc) {
2398874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev,
2399874aeea5SJeff Kirsher 			  "failed to initialise NIC\n");
2400874aeea5SJeff Kirsher 		goto fail3;
2401874aeea5SJeff Kirsher 	}
2402874aeea5SJeff Kirsher 
2403874aeea5SJeff Kirsher 	rc = efx_init_port(efx);
2404874aeea5SJeff Kirsher 	if (rc) {
2405874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev,
2406874aeea5SJeff Kirsher 			  "failed to initialise port\n");
2407874aeea5SJeff Kirsher 		goto fail4;
2408874aeea5SJeff Kirsher 	}
2409874aeea5SJeff Kirsher 
2410874aeea5SJeff Kirsher 	efx_init_channels(efx);
2411874aeea5SJeff Kirsher 
2412874aeea5SJeff Kirsher 	rc = efx_nic_init_interrupt(efx);
2413874aeea5SJeff Kirsher 	if (rc)
2414874aeea5SJeff Kirsher 		goto fail5;
2415874aeea5SJeff Kirsher 
2416874aeea5SJeff Kirsher 	return 0;
2417874aeea5SJeff Kirsher 
2418874aeea5SJeff Kirsher  fail5:
2419874aeea5SJeff Kirsher 	efx_fini_channels(efx);
2420874aeea5SJeff Kirsher 	efx_fini_port(efx);
2421874aeea5SJeff Kirsher  fail4:
2422874aeea5SJeff Kirsher 	efx->type->fini(efx);
2423874aeea5SJeff Kirsher  fail3:
2424874aeea5SJeff Kirsher 	efx_fini_napi(efx);
2425874aeea5SJeff Kirsher 	efx_remove_all(efx);
2426874aeea5SJeff Kirsher  fail1:
2427874aeea5SJeff Kirsher 	return rc;
2428874aeea5SJeff Kirsher }
2429874aeea5SJeff Kirsher 
2430874aeea5SJeff Kirsher /* NIC initialisation
2431874aeea5SJeff Kirsher  *
2432874aeea5SJeff Kirsher  * This is called at module load (or hotplug insertion,
243373ba7b68SBen Hutchings  * theoretically).  It sets up PCI mappings, resets the NIC,
2434874aeea5SJeff Kirsher  * sets up and registers the network devices with the kernel and hooks
2435874aeea5SJeff Kirsher  * the interrupt service routine.  It does not prepare the device for
2436874aeea5SJeff Kirsher  * transmission; this is left to the first time one of the network
2437874aeea5SJeff Kirsher  * interfaces is brought up (i.e. efx_net_open).
2438874aeea5SJeff Kirsher  */
2439874aeea5SJeff Kirsher static int __devinit efx_pci_probe(struct pci_dev *pci_dev,
2440874aeea5SJeff Kirsher 				   const struct pci_device_id *entry)
2441874aeea5SJeff Kirsher {
2442874aeea5SJeff Kirsher 	const struct efx_nic_type *type = (const struct efx_nic_type *) entry->driver_data;
2443874aeea5SJeff Kirsher 	struct net_device *net_dev;
2444874aeea5SJeff Kirsher 	struct efx_nic *efx;
2445874aeea5SJeff Kirsher 	int i, rc;
2446874aeea5SJeff Kirsher 
2447874aeea5SJeff Kirsher 	/* Allocate and initialise a struct net_device and struct efx_nic */
2448874aeea5SJeff Kirsher 	net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
2449874aeea5SJeff Kirsher 				     EFX_MAX_RX_QUEUES);
2450874aeea5SJeff Kirsher 	if (!net_dev)
2451874aeea5SJeff Kirsher 		return -ENOMEM;
2452874aeea5SJeff Kirsher 	net_dev->features |= (type->offload_features | NETIF_F_SG |
2453874aeea5SJeff Kirsher 			      NETIF_F_HIGHDMA | NETIF_F_TSO |
2454874aeea5SJeff Kirsher 			      NETIF_F_RXCSUM);
2455874aeea5SJeff Kirsher 	if (type->offload_features & NETIF_F_V6_CSUM)
2456874aeea5SJeff Kirsher 		net_dev->features |= NETIF_F_TSO6;
2457874aeea5SJeff Kirsher 	/* Mask for features that also apply to VLAN devices */
2458874aeea5SJeff Kirsher 	net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
2459874aeea5SJeff Kirsher 				   NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
2460874aeea5SJeff Kirsher 				   NETIF_F_RXCSUM);
2461874aeea5SJeff Kirsher 	/* All offloads can be toggled */
2462874aeea5SJeff Kirsher 	net_dev->hw_features = net_dev->features & ~NETIF_F_HIGHDMA;
2463874aeea5SJeff Kirsher 	efx = netdev_priv(net_dev);
2464874aeea5SJeff Kirsher 	pci_set_drvdata(pci_dev, efx);
2465874aeea5SJeff Kirsher 	SET_NETDEV_DEV(net_dev, &pci_dev->dev);
2466874aeea5SJeff Kirsher 	rc = efx_init_struct(efx, type, pci_dev, net_dev);
2467874aeea5SJeff Kirsher 	if (rc)
2468874aeea5SJeff Kirsher 		goto fail1;
2469874aeea5SJeff Kirsher 
2470874aeea5SJeff Kirsher 	netif_info(efx, probe, efx->net_dev,
2471874aeea5SJeff Kirsher 		   "Solarflare NIC detected\n");
2472874aeea5SJeff Kirsher 
2473874aeea5SJeff Kirsher 	/* Set up basic I/O (BAR mappings etc) */
2474874aeea5SJeff Kirsher 	rc = efx_init_io(efx);
2475874aeea5SJeff Kirsher 	if (rc)
2476874aeea5SJeff Kirsher 		goto fail2;
2477874aeea5SJeff Kirsher 
2478874aeea5SJeff Kirsher 	/* No serialisation is required with the reset path because
2479874aeea5SJeff Kirsher 	 * we're in STATE_INIT. */
2480874aeea5SJeff Kirsher 	for (i = 0; i < 5; i++) {
2481874aeea5SJeff Kirsher 		rc = efx_pci_probe_main(efx);
2482874aeea5SJeff Kirsher 
2483874aeea5SJeff Kirsher 		/* Serialise against efx_reset(). No more resets will be
2484874aeea5SJeff Kirsher 		 * scheduled since efx_stop_all() has been called, and we
2485874aeea5SJeff Kirsher 		 * have not and never have been registered with either
2486874aeea5SJeff Kirsher 		 * the rtnetlink or driverlink layers. */
2487874aeea5SJeff Kirsher 		cancel_work_sync(&efx->reset_work);
2488874aeea5SJeff Kirsher 
2489874aeea5SJeff Kirsher 		if (rc == 0) {
2490874aeea5SJeff Kirsher 			if (efx->reset_pending) {
2491874aeea5SJeff Kirsher 				/* If there was a scheduled reset during
2492874aeea5SJeff Kirsher 				 * probe, the NIC is probably hosed anyway */
2493874aeea5SJeff Kirsher 				efx_pci_remove_main(efx);
2494874aeea5SJeff Kirsher 				rc = -EIO;
2495874aeea5SJeff Kirsher 			} else {
2496874aeea5SJeff Kirsher 				break;
2497874aeea5SJeff Kirsher 			}
2498874aeea5SJeff Kirsher 		}
2499874aeea5SJeff Kirsher 
2500874aeea5SJeff Kirsher 		/* Retry if a recoverably reset event has been scheduled */
2501874aeea5SJeff Kirsher 		if (efx->reset_pending &
2502874aeea5SJeff Kirsher 		    ~(1 << RESET_TYPE_INVISIBLE | 1 << RESET_TYPE_ALL) ||
2503874aeea5SJeff Kirsher 		    !efx->reset_pending)
2504874aeea5SJeff Kirsher 			goto fail3;
2505874aeea5SJeff Kirsher 
2506874aeea5SJeff Kirsher 		efx->reset_pending = 0;
2507874aeea5SJeff Kirsher 	}
2508874aeea5SJeff Kirsher 
2509874aeea5SJeff Kirsher 	if (rc) {
2510874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev, "Could not reset NIC\n");
2511874aeea5SJeff Kirsher 		goto fail4;
2512874aeea5SJeff Kirsher 	}
2513874aeea5SJeff Kirsher 
2514874aeea5SJeff Kirsher 	/* Switch to the running state before we expose the device to the OS,
2515874aeea5SJeff Kirsher 	 * so that dev_open()|efx_start_all() will actually start the device */
2516874aeea5SJeff Kirsher 	efx->state = STATE_RUNNING;
2517874aeea5SJeff Kirsher 
2518874aeea5SJeff Kirsher 	rc = efx_register_netdev(efx);
2519874aeea5SJeff Kirsher 	if (rc)
2520874aeea5SJeff Kirsher 		goto fail5;
2521874aeea5SJeff Kirsher 
2522874aeea5SJeff Kirsher 	netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
2523874aeea5SJeff Kirsher 
2524874aeea5SJeff Kirsher 	rtnl_lock();
2525874aeea5SJeff Kirsher 	efx_mtd_probe(efx); /* allowed to fail */
2526874aeea5SJeff Kirsher 	rtnl_unlock();
2527874aeea5SJeff Kirsher 	return 0;
2528874aeea5SJeff Kirsher 
2529874aeea5SJeff Kirsher  fail5:
2530874aeea5SJeff Kirsher 	efx_pci_remove_main(efx);
2531874aeea5SJeff Kirsher  fail4:
2532874aeea5SJeff Kirsher  fail3:
2533874aeea5SJeff Kirsher 	efx_fini_io(efx);
2534874aeea5SJeff Kirsher  fail2:
2535874aeea5SJeff Kirsher 	efx_fini_struct(efx);
2536874aeea5SJeff Kirsher  fail1:
2537874aeea5SJeff Kirsher 	WARN_ON(rc > 0);
2538874aeea5SJeff Kirsher 	netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
2539874aeea5SJeff Kirsher 	free_netdev(net_dev);
2540874aeea5SJeff Kirsher 	return rc;
2541874aeea5SJeff Kirsher }
2542874aeea5SJeff Kirsher 
2543874aeea5SJeff Kirsher static int efx_pm_freeze(struct device *dev)
2544874aeea5SJeff Kirsher {
2545874aeea5SJeff Kirsher 	struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2546874aeea5SJeff Kirsher 
2547874aeea5SJeff Kirsher 	efx->state = STATE_FINI;
2548874aeea5SJeff Kirsher 
2549874aeea5SJeff Kirsher 	netif_device_detach(efx->net_dev);
2550874aeea5SJeff Kirsher 
2551874aeea5SJeff Kirsher 	efx_stop_all(efx);
2552874aeea5SJeff Kirsher 	efx_fini_channels(efx);
2553874aeea5SJeff Kirsher 
2554874aeea5SJeff Kirsher 	return 0;
2555874aeea5SJeff Kirsher }
2556874aeea5SJeff Kirsher 
2557874aeea5SJeff Kirsher static int efx_pm_thaw(struct device *dev)
2558874aeea5SJeff Kirsher {
2559874aeea5SJeff Kirsher 	struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2560874aeea5SJeff Kirsher 
2561874aeea5SJeff Kirsher 	efx->state = STATE_INIT;
2562874aeea5SJeff Kirsher 
2563874aeea5SJeff Kirsher 	efx_init_channels(efx);
2564874aeea5SJeff Kirsher 
2565874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
2566874aeea5SJeff Kirsher 	efx->phy_op->reconfigure(efx);
2567874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
2568874aeea5SJeff Kirsher 
2569874aeea5SJeff Kirsher 	efx_start_all(efx);
2570874aeea5SJeff Kirsher 
2571874aeea5SJeff Kirsher 	netif_device_attach(efx->net_dev);
2572874aeea5SJeff Kirsher 
2573874aeea5SJeff Kirsher 	efx->state = STATE_RUNNING;
2574874aeea5SJeff Kirsher 
2575874aeea5SJeff Kirsher 	efx->type->resume_wol(efx);
2576874aeea5SJeff Kirsher 
2577874aeea5SJeff Kirsher 	/* Reschedule any quenched resets scheduled during efx_pm_freeze() */
2578874aeea5SJeff Kirsher 	queue_work(reset_workqueue, &efx->reset_work);
2579874aeea5SJeff Kirsher 
2580874aeea5SJeff Kirsher 	return 0;
2581874aeea5SJeff Kirsher }
2582874aeea5SJeff Kirsher 
2583874aeea5SJeff Kirsher static int efx_pm_poweroff(struct device *dev)
2584874aeea5SJeff Kirsher {
2585874aeea5SJeff Kirsher 	struct pci_dev *pci_dev = to_pci_dev(dev);
2586874aeea5SJeff Kirsher 	struct efx_nic *efx = pci_get_drvdata(pci_dev);
2587874aeea5SJeff Kirsher 
2588874aeea5SJeff Kirsher 	efx->type->fini(efx);
2589874aeea5SJeff Kirsher 
2590874aeea5SJeff Kirsher 	efx->reset_pending = 0;
2591874aeea5SJeff Kirsher 
2592874aeea5SJeff Kirsher 	pci_save_state(pci_dev);
2593874aeea5SJeff Kirsher 	return pci_set_power_state(pci_dev, PCI_D3hot);
2594874aeea5SJeff Kirsher }
2595874aeea5SJeff Kirsher 
2596874aeea5SJeff Kirsher /* Used for both resume and restore */
2597874aeea5SJeff Kirsher static int efx_pm_resume(struct device *dev)
2598874aeea5SJeff Kirsher {
2599874aeea5SJeff Kirsher 	struct pci_dev *pci_dev = to_pci_dev(dev);
2600874aeea5SJeff Kirsher 	struct efx_nic *efx = pci_get_drvdata(pci_dev);
2601874aeea5SJeff Kirsher 	int rc;
2602874aeea5SJeff Kirsher 
2603874aeea5SJeff Kirsher 	rc = pci_set_power_state(pci_dev, PCI_D0);
2604874aeea5SJeff Kirsher 	if (rc)
2605874aeea5SJeff Kirsher 		return rc;
2606874aeea5SJeff Kirsher 	pci_restore_state(pci_dev);
2607874aeea5SJeff Kirsher 	rc = pci_enable_device(pci_dev);
2608874aeea5SJeff Kirsher 	if (rc)
2609874aeea5SJeff Kirsher 		return rc;
2610874aeea5SJeff Kirsher 	pci_set_master(efx->pci_dev);
2611874aeea5SJeff Kirsher 	rc = efx->type->reset(efx, RESET_TYPE_ALL);
2612874aeea5SJeff Kirsher 	if (rc)
2613874aeea5SJeff Kirsher 		return rc;
2614874aeea5SJeff Kirsher 	rc = efx->type->init(efx);
2615874aeea5SJeff Kirsher 	if (rc)
2616874aeea5SJeff Kirsher 		return rc;
2617874aeea5SJeff Kirsher 	efx_pm_thaw(dev);
2618874aeea5SJeff Kirsher 	return 0;
2619874aeea5SJeff Kirsher }
2620874aeea5SJeff Kirsher 
2621874aeea5SJeff Kirsher static int efx_pm_suspend(struct device *dev)
2622874aeea5SJeff Kirsher {
2623874aeea5SJeff Kirsher 	int rc;
2624874aeea5SJeff Kirsher 
2625874aeea5SJeff Kirsher 	efx_pm_freeze(dev);
2626874aeea5SJeff Kirsher 	rc = efx_pm_poweroff(dev);
2627874aeea5SJeff Kirsher 	if (rc)
2628874aeea5SJeff Kirsher 		efx_pm_resume(dev);
2629874aeea5SJeff Kirsher 	return rc;
2630874aeea5SJeff Kirsher }
2631874aeea5SJeff Kirsher 
263218e83e4cSBen Hutchings static const struct dev_pm_ops efx_pm_ops = {
2633874aeea5SJeff Kirsher 	.suspend	= efx_pm_suspend,
2634874aeea5SJeff Kirsher 	.resume		= efx_pm_resume,
2635874aeea5SJeff Kirsher 	.freeze		= efx_pm_freeze,
2636874aeea5SJeff Kirsher 	.thaw		= efx_pm_thaw,
2637874aeea5SJeff Kirsher 	.poweroff	= efx_pm_poweroff,
2638874aeea5SJeff Kirsher 	.restore	= efx_pm_resume,
2639874aeea5SJeff Kirsher };
2640874aeea5SJeff Kirsher 
2641874aeea5SJeff Kirsher static struct pci_driver efx_pci_driver = {
2642874aeea5SJeff Kirsher 	.name		= KBUILD_MODNAME,
2643874aeea5SJeff Kirsher 	.id_table	= efx_pci_table,
2644874aeea5SJeff Kirsher 	.probe		= efx_pci_probe,
2645874aeea5SJeff Kirsher 	.remove		= efx_pci_remove,
2646874aeea5SJeff Kirsher 	.driver.pm	= &efx_pm_ops,
2647874aeea5SJeff Kirsher };
2648874aeea5SJeff Kirsher 
2649874aeea5SJeff Kirsher /**************************************************************************
2650874aeea5SJeff Kirsher  *
2651874aeea5SJeff Kirsher  * Kernel module interface
2652874aeea5SJeff Kirsher  *
2653874aeea5SJeff Kirsher  *************************************************************************/
2654874aeea5SJeff Kirsher 
2655874aeea5SJeff Kirsher module_param(interrupt_mode, uint, 0444);
2656874aeea5SJeff Kirsher MODULE_PARM_DESC(interrupt_mode,
2657874aeea5SJeff Kirsher 		 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
2658874aeea5SJeff Kirsher 
2659874aeea5SJeff Kirsher static int __init efx_init_module(void)
2660874aeea5SJeff Kirsher {
2661874aeea5SJeff Kirsher 	int rc;
2662874aeea5SJeff Kirsher 
2663874aeea5SJeff Kirsher 	printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
2664874aeea5SJeff Kirsher 
2665874aeea5SJeff Kirsher 	rc = register_netdevice_notifier(&efx_netdev_notifier);
2666874aeea5SJeff Kirsher 	if (rc)
2667874aeea5SJeff Kirsher 		goto err_notifier;
2668874aeea5SJeff Kirsher 
2669874aeea5SJeff Kirsher 	reset_workqueue = create_singlethread_workqueue("sfc_reset");
2670874aeea5SJeff Kirsher 	if (!reset_workqueue) {
2671874aeea5SJeff Kirsher 		rc = -ENOMEM;
2672874aeea5SJeff Kirsher 		goto err_reset;
2673874aeea5SJeff Kirsher 	}
2674874aeea5SJeff Kirsher 
2675874aeea5SJeff Kirsher 	rc = pci_register_driver(&efx_pci_driver);
2676874aeea5SJeff Kirsher 	if (rc < 0)
2677874aeea5SJeff Kirsher 		goto err_pci;
2678874aeea5SJeff Kirsher 
2679874aeea5SJeff Kirsher 	return 0;
2680874aeea5SJeff Kirsher 
2681874aeea5SJeff Kirsher  err_pci:
2682874aeea5SJeff Kirsher 	destroy_workqueue(reset_workqueue);
2683874aeea5SJeff Kirsher  err_reset:
2684874aeea5SJeff Kirsher 	unregister_netdevice_notifier(&efx_netdev_notifier);
2685874aeea5SJeff Kirsher  err_notifier:
2686874aeea5SJeff Kirsher 	return rc;
2687874aeea5SJeff Kirsher }
2688874aeea5SJeff Kirsher 
2689874aeea5SJeff Kirsher static void __exit efx_exit_module(void)
2690874aeea5SJeff Kirsher {
2691874aeea5SJeff Kirsher 	printk(KERN_INFO "Solarflare NET driver unloading\n");
2692874aeea5SJeff Kirsher 
2693874aeea5SJeff Kirsher 	pci_unregister_driver(&efx_pci_driver);
2694874aeea5SJeff Kirsher 	destroy_workqueue(reset_workqueue);
2695874aeea5SJeff Kirsher 	unregister_netdevice_notifier(&efx_netdev_notifier);
2696874aeea5SJeff Kirsher 
2697874aeea5SJeff Kirsher }
2698874aeea5SJeff Kirsher 
2699874aeea5SJeff Kirsher module_init(efx_init_module);
2700874aeea5SJeff Kirsher module_exit(efx_exit_module);
2701874aeea5SJeff Kirsher 
2702874aeea5SJeff Kirsher MODULE_AUTHOR("Solarflare Communications and "
2703874aeea5SJeff Kirsher 	      "Michael Brown <mbrown@fensystems.co.uk>");
2704874aeea5SJeff Kirsher MODULE_DESCRIPTION("Solarflare Communications network driver");
2705874aeea5SJeff Kirsher MODULE_LICENSE("GPL");
2706874aeea5SJeff Kirsher MODULE_DEVICE_TABLE(pci, efx_pci_table);
2707