xref: /openbmc/linux/drivers/net/ethernet/sfc/efx.c (revision a16e5b24)
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 
1899f2cb71cSBen Hutchings static void efx_start_interrupts(struct efx_nic *efx);
1909f2cb71cSBen Hutchings static void efx_stop_interrupts(struct efx_nic *efx);
191874aeea5SJeff Kirsher static void efx_remove_channels(struct efx_nic *efx);
192874aeea5SJeff Kirsher static void efx_remove_port(struct efx_nic *efx);
193874aeea5SJeff Kirsher static void efx_init_napi(struct efx_nic *efx);
194874aeea5SJeff Kirsher static void efx_fini_napi(struct efx_nic *efx);
195874aeea5SJeff Kirsher static void efx_fini_napi_channel(struct efx_channel *channel);
196874aeea5SJeff Kirsher static void efx_fini_struct(struct efx_nic *efx);
197874aeea5SJeff Kirsher static void efx_start_all(struct efx_nic *efx);
198874aeea5SJeff Kirsher static void efx_stop_all(struct efx_nic *efx);
199874aeea5SJeff Kirsher 
200874aeea5SJeff Kirsher #define EFX_ASSERT_RESET_SERIALISED(efx)		\
201874aeea5SJeff Kirsher 	do {						\
202874aeea5SJeff Kirsher 		if ((efx->state == STATE_RUNNING) ||	\
203874aeea5SJeff Kirsher 		    (efx->state == STATE_DISABLED))	\
204874aeea5SJeff Kirsher 			ASSERT_RTNL();			\
205874aeea5SJeff Kirsher 	} while (0)
206874aeea5SJeff Kirsher 
207874aeea5SJeff Kirsher /**************************************************************************
208874aeea5SJeff Kirsher  *
209874aeea5SJeff Kirsher  * Event queue processing
210874aeea5SJeff Kirsher  *
211874aeea5SJeff Kirsher  *************************************************************************/
212874aeea5SJeff Kirsher 
213874aeea5SJeff Kirsher /* Process channel's event queue
214874aeea5SJeff Kirsher  *
215874aeea5SJeff Kirsher  * This function is responsible for processing the event queue of a
216874aeea5SJeff Kirsher  * single channel.  The caller must guarantee that this function will
217874aeea5SJeff Kirsher  * never be concurrently called more than once on the same channel,
218874aeea5SJeff Kirsher  * though different channels may be being processed concurrently.
219874aeea5SJeff Kirsher  */
220874aeea5SJeff Kirsher static int efx_process_channel(struct efx_channel *channel, int budget)
221874aeea5SJeff Kirsher {
222874aeea5SJeff Kirsher 	int spent;
223874aeea5SJeff Kirsher 
2249f2cb71cSBen Hutchings 	if (unlikely(!channel->enabled))
225874aeea5SJeff Kirsher 		return 0;
226874aeea5SJeff Kirsher 
227874aeea5SJeff Kirsher 	spent = efx_nic_process_eventq(channel, budget);
228d9ab7007SBen Hutchings 	if (spent && efx_channel_has_rx_queue(channel)) {
229d9ab7007SBen Hutchings 		struct efx_rx_queue *rx_queue =
230d9ab7007SBen Hutchings 			efx_channel_get_rx_queue(channel);
231874aeea5SJeff Kirsher 
232874aeea5SJeff Kirsher 		/* Deliver last RX packet. */
233874aeea5SJeff Kirsher 		if (channel->rx_pkt) {
234db339569SBen Hutchings 			__efx_rx_packet(channel, channel->rx_pkt);
235874aeea5SJeff Kirsher 			channel->rx_pkt = NULL;
236874aeea5SJeff Kirsher 		}
2379f2cb71cSBen Hutchings 		if (rx_queue->enabled) {
238874aeea5SJeff Kirsher 			efx_rx_strategy(channel);
239d9ab7007SBen Hutchings 			efx_fast_push_rx_descriptors(rx_queue);
240d9ab7007SBen Hutchings 		}
2419f2cb71cSBen Hutchings 	}
242874aeea5SJeff Kirsher 
243874aeea5SJeff Kirsher 	return spent;
244874aeea5SJeff Kirsher }
245874aeea5SJeff Kirsher 
246874aeea5SJeff Kirsher /* Mark channel as finished processing
247874aeea5SJeff Kirsher  *
248874aeea5SJeff Kirsher  * Note that since we will not receive further interrupts for this
249874aeea5SJeff Kirsher  * channel before we finish processing and call the eventq_read_ack()
250874aeea5SJeff Kirsher  * method, there is no need to use the interrupt hold-off timers.
251874aeea5SJeff Kirsher  */
252874aeea5SJeff Kirsher static inline void efx_channel_processed(struct efx_channel *channel)
253874aeea5SJeff Kirsher {
254874aeea5SJeff Kirsher 	/* The interrupt handler for this channel may set work_pending
255874aeea5SJeff Kirsher 	 * as soon as we acknowledge the events we've seen.  Make sure
256874aeea5SJeff Kirsher 	 * it's cleared before then. */
257874aeea5SJeff Kirsher 	channel->work_pending = false;
258874aeea5SJeff Kirsher 	smp_wmb();
259874aeea5SJeff Kirsher 
260874aeea5SJeff Kirsher 	efx_nic_eventq_read_ack(channel);
261874aeea5SJeff Kirsher }
262874aeea5SJeff Kirsher 
263874aeea5SJeff Kirsher /* NAPI poll handler
264874aeea5SJeff Kirsher  *
265874aeea5SJeff Kirsher  * NAPI guarantees serialisation of polls of the same device, which
266874aeea5SJeff Kirsher  * provides the guarantee required by efx_process_channel().
267874aeea5SJeff Kirsher  */
268874aeea5SJeff Kirsher static int efx_poll(struct napi_struct *napi, int budget)
269874aeea5SJeff Kirsher {
270874aeea5SJeff Kirsher 	struct efx_channel *channel =
271874aeea5SJeff Kirsher 		container_of(napi, struct efx_channel, napi_str);
272874aeea5SJeff Kirsher 	struct efx_nic *efx = channel->efx;
273874aeea5SJeff Kirsher 	int spent;
274874aeea5SJeff Kirsher 
275874aeea5SJeff Kirsher 	netif_vdbg(efx, intr, efx->net_dev,
276874aeea5SJeff Kirsher 		   "channel %d NAPI poll executing on CPU %d\n",
277874aeea5SJeff Kirsher 		   channel->channel, raw_smp_processor_id());
278874aeea5SJeff Kirsher 
279874aeea5SJeff Kirsher 	spent = efx_process_channel(channel, budget);
280874aeea5SJeff Kirsher 
281874aeea5SJeff Kirsher 	if (spent < budget) {
2829d9a6973SBen Hutchings 		if (efx_channel_has_rx_queue(channel) &&
283874aeea5SJeff Kirsher 		    efx->irq_rx_adaptive &&
284874aeea5SJeff Kirsher 		    unlikely(++channel->irq_count == 1000)) {
285874aeea5SJeff Kirsher 			if (unlikely(channel->irq_mod_score <
286874aeea5SJeff Kirsher 				     irq_adapt_low_thresh)) {
287874aeea5SJeff Kirsher 				if (channel->irq_moderation > 1) {
288874aeea5SJeff Kirsher 					channel->irq_moderation -= 1;
289874aeea5SJeff Kirsher 					efx->type->push_irq_moderation(channel);
290874aeea5SJeff Kirsher 				}
291874aeea5SJeff Kirsher 			} else if (unlikely(channel->irq_mod_score >
292874aeea5SJeff Kirsher 					    irq_adapt_high_thresh)) {
293874aeea5SJeff Kirsher 				if (channel->irq_moderation <
294874aeea5SJeff Kirsher 				    efx->irq_rx_moderation) {
295874aeea5SJeff Kirsher 					channel->irq_moderation += 1;
296874aeea5SJeff Kirsher 					efx->type->push_irq_moderation(channel);
297874aeea5SJeff Kirsher 				}
298874aeea5SJeff Kirsher 			}
299874aeea5SJeff Kirsher 			channel->irq_count = 0;
300874aeea5SJeff Kirsher 			channel->irq_mod_score = 0;
301874aeea5SJeff Kirsher 		}
302874aeea5SJeff Kirsher 
303874aeea5SJeff Kirsher 		efx_filter_rfs_expire(channel);
304874aeea5SJeff Kirsher 
305874aeea5SJeff Kirsher 		/* There is no race here; although napi_disable() will
306874aeea5SJeff Kirsher 		 * only wait for napi_complete(), this isn't a problem
307874aeea5SJeff Kirsher 		 * since efx_channel_processed() will have no effect if
308874aeea5SJeff Kirsher 		 * interrupts have already been disabled.
309874aeea5SJeff Kirsher 		 */
310874aeea5SJeff Kirsher 		napi_complete(napi);
311874aeea5SJeff Kirsher 		efx_channel_processed(channel);
312874aeea5SJeff Kirsher 	}
313874aeea5SJeff Kirsher 
314874aeea5SJeff Kirsher 	return spent;
315874aeea5SJeff Kirsher }
316874aeea5SJeff Kirsher 
317874aeea5SJeff Kirsher /* Process the eventq of the specified channel immediately on this CPU
318874aeea5SJeff Kirsher  *
319874aeea5SJeff Kirsher  * Disable hardware generated interrupts, wait for any existing
320874aeea5SJeff Kirsher  * processing to finish, then directly poll (and ack ) the eventq.
321874aeea5SJeff Kirsher  * Finally reenable NAPI and interrupts.
322874aeea5SJeff Kirsher  *
323874aeea5SJeff Kirsher  * This is for use only during a loopback self-test.  It must not
324874aeea5SJeff Kirsher  * deliver any packets up the stack as this can result in deadlock.
325874aeea5SJeff Kirsher  */
326874aeea5SJeff Kirsher void efx_process_channel_now(struct efx_channel *channel)
327874aeea5SJeff Kirsher {
328874aeea5SJeff Kirsher 	struct efx_nic *efx = channel->efx;
329874aeea5SJeff Kirsher 
330874aeea5SJeff Kirsher 	BUG_ON(channel->channel >= efx->n_channels);
331874aeea5SJeff Kirsher 	BUG_ON(!channel->enabled);
332874aeea5SJeff Kirsher 	BUG_ON(!efx->loopback_selftest);
333874aeea5SJeff Kirsher 
334874aeea5SJeff Kirsher 	/* Disable interrupts and wait for ISRs to complete */
335874aeea5SJeff Kirsher 	efx_nic_disable_interrupts(efx);
336874aeea5SJeff Kirsher 	if (efx->legacy_irq) {
337874aeea5SJeff Kirsher 		synchronize_irq(efx->legacy_irq);
338874aeea5SJeff Kirsher 		efx->legacy_irq_enabled = false;
339874aeea5SJeff Kirsher 	}
340874aeea5SJeff Kirsher 	if (channel->irq)
341874aeea5SJeff Kirsher 		synchronize_irq(channel->irq);
342874aeea5SJeff Kirsher 
343874aeea5SJeff Kirsher 	/* Wait for any NAPI processing to complete */
344874aeea5SJeff Kirsher 	napi_disable(&channel->napi_str);
345874aeea5SJeff Kirsher 
346874aeea5SJeff Kirsher 	/* Poll the channel */
347874aeea5SJeff Kirsher 	efx_process_channel(channel, channel->eventq_mask + 1);
348874aeea5SJeff Kirsher 
349874aeea5SJeff Kirsher 	/* Ack the eventq. This may cause an interrupt to be generated
350874aeea5SJeff Kirsher 	 * when they are reenabled */
351874aeea5SJeff Kirsher 	efx_channel_processed(channel);
352874aeea5SJeff Kirsher 
353874aeea5SJeff Kirsher 	napi_enable(&channel->napi_str);
354874aeea5SJeff Kirsher 	if (efx->legacy_irq)
355874aeea5SJeff Kirsher 		efx->legacy_irq_enabled = true;
356874aeea5SJeff Kirsher 	efx_nic_enable_interrupts(efx);
357874aeea5SJeff Kirsher }
358874aeea5SJeff Kirsher 
359874aeea5SJeff Kirsher /* Create event queue
360874aeea5SJeff Kirsher  * Event queue memory allocations are done only once.  If the channel
361874aeea5SJeff Kirsher  * is reset, the memory buffer will be reused; this guards against
362874aeea5SJeff Kirsher  * errors during channel reset and also simplifies interrupt handling.
363874aeea5SJeff Kirsher  */
364874aeea5SJeff Kirsher static int efx_probe_eventq(struct efx_channel *channel)
365874aeea5SJeff Kirsher {
366874aeea5SJeff Kirsher 	struct efx_nic *efx = channel->efx;
367874aeea5SJeff Kirsher 	unsigned long entries;
368874aeea5SJeff Kirsher 
36986ee5302SBen Hutchings 	netif_dbg(efx, probe, efx->net_dev,
370874aeea5SJeff Kirsher 		  "chan %d create event queue\n", channel->channel);
371874aeea5SJeff Kirsher 
372874aeea5SJeff Kirsher 	/* Build an event queue with room for one event per tx and rx buffer,
373874aeea5SJeff Kirsher 	 * plus some extra for link state events and MCDI completions. */
374874aeea5SJeff Kirsher 	entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
375874aeea5SJeff Kirsher 	EFX_BUG_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
376874aeea5SJeff Kirsher 	channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
377874aeea5SJeff Kirsher 
378874aeea5SJeff Kirsher 	return efx_nic_probe_eventq(channel);
379874aeea5SJeff Kirsher }
380874aeea5SJeff Kirsher 
381874aeea5SJeff Kirsher /* Prepare channel's event queue */
382874aeea5SJeff Kirsher static void efx_init_eventq(struct efx_channel *channel)
383874aeea5SJeff Kirsher {
384874aeea5SJeff Kirsher 	netif_dbg(channel->efx, drv, channel->efx->net_dev,
385874aeea5SJeff Kirsher 		  "chan %d init event queue\n", channel->channel);
386874aeea5SJeff Kirsher 
387874aeea5SJeff Kirsher 	channel->eventq_read_ptr = 0;
388874aeea5SJeff Kirsher 
389874aeea5SJeff Kirsher 	efx_nic_init_eventq(channel);
390874aeea5SJeff Kirsher }
391874aeea5SJeff Kirsher 
3929f2cb71cSBen Hutchings /* Enable event queue processing and NAPI */
3939f2cb71cSBen Hutchings static void efx_start_eventq(struct efx_channel *channel)
3949f2cb71cSBen Hutchings {
3959f2cb71cSBen Hutchings 	netif_dbg(channel->efx, ifup, channel->efx->net_dev,
3969f2cb71cSBen Hutchings 		  "chan %d start event queue\n", channel->channel);
3979f2cb71cSBen Hutchings 
3989f2cb71cSBen Hutchings 	/* The interrupt handler for this channel may set work_pending
3999f2cb71cSBen Hutchings 	 * as soon as we enable it.  Make sure it's cleared before
4009f2cb71cSBen Hutchings 	 * then.  Similarly, make sure it sees the enabled flag set.
4019f2cb71cSBen Hutchings 	 */
4029f2cb71cSBen Hutchings 	channel->work_pending = false;
4039f2cb71cSBen Hutchings 	channel->enabled = true;
4049f2cb71cSBen Hutchings 	smp_wmb();
4059f2cb71cSBen Hutchings 
4069f2cb71cSBen Hutchings 	napi_enable(&channel->napi_str);
4079f2cb71cSBen Hutchings 	efx_nic_eventq_read_ack(channel);
4089f2cb71cSBen Hutchings }
4099f2cb71cSBen Hutchings 
4109f2cb71cSBen Hutchings /* Disable event queue processing and NAPI */
4119f2cb71cSBen Hutchings static void efx_stop_eventq(struct efx_channel *channel)
4129f2cb71cSBen Hutchings {
4139f2cb71cSBen Hutchings 	if (!channel->enabled)
4149f2cb71cSBen Hutchings 		return;
4159f2cb71cSBen Hutchings 
4169f2cb71cSBen Hutchings 	napi_disable(&channel->napi_str);
4179f2cb71cSBen Hutchings 	channel->enabled = false;
4189f2cb71cSBen Hutchings }
4199f2cb71cSBen Hutchings 
420874aeea5SJeff Kirsher static void efx_fini_eventq(struct efx_channel *channel)
421874aeea5SJeff Kirsher {
422874aeea5SJeff Kirsher 	netif_dbg(channel->efx, drv, channel->efx->net_dev,
423874aeea5SJeff Kirsher 		  "chan %d fini event queue\n", channel->channel);
424874aeea5SJeff Kirsher 
425874aeea5SJeff Kirsher 	efx_nic_fini_eventq(channel);
426874aeea5SJeff Kirsher }
427874aeea5SJeff Kirsher 
428874aeea5SJeff Kirsher static void efx_remove_eventq(struct efx_channel *channel)
429874aeea5SJeff Kirsher {
430874aeea5SJeff Kirsher 	netif_dbg(channel->efx, drv, channel->efx->net_dev,
431874aeea5SJeff Kirsher 		  "chan %d remove event queue\n", channel->channel);
432874aeea5SJeff Kirsher 
433874aeea5SJeff Kirsher 	efx_nic_remove_eventq(channel);
434874aeea5SJeff Kirsher }
435874aeea5SJeff Kirsher 
436874aeea5SJeff Kirsher /**************************************************************************
437874aeea5SJeff Kirsher  *
438874aeea5SJeff Kirsher  * Channel handling
439874aeea5SJeff Kirsher  *
440874aeea5SJeff Kirsher  *************************************************************************/
441874aeea5SJeff Kirsher 
442874aeea5SJeff Kirsher /* Allocate and initialise a channel structure, optionally copying
443874aeea5SJeff Kirsher  * parameters (but not resources) from an old channel structure. */
444874aeea5SJeff Kirsher static struct efx_channel *
445874aeea5SJeff Kirsher efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
446874aeea5SJeff Kirsher {
447874aeea5SJeff Kirsher 	struct efx_channel *channel;
448874aeea5SJeff Kirsher 	struct efx_rx_queue *rx_queue;
449874aeea5SJeff Kirsher 	struct efx_tx_queue *tx_queue;
450874aeea5SJeff Kirsher 	int j;
451874aeea5SJeff Kirsher 
452874aeea5SJeff Kirsher 	if (old_channel) {
453874aeea5SJeff Kirsher 		channel = kmalloc(sizeof(*channel), GFP_KERNEL);
454874aeea5SJeff Kirsher 		if (!channel)
455874aeea5SJeff Kirsher 			return NULL;
456874aeea5SJeff Kirsher 
457874aeea5SJeff Kirsher 		*channel = *old_channel;
458874aeea5SJeff Kirsher 
459874aeea5SJeff Kirsher 		channel->napi_dev = NULL;
460874aeea5SJeff Kirsher 		memset(&channel->eventq, 0, sizeof(channel->eventq));
461874aeea5SJeff Kirsher 
462874aeea5SJeff Kirsher 		rx_queue = &channel->rx_queue;
463874aeea5SJeff Kirsher 		rx_queue->buffer = NULL;
464874aeea5SJeff Kirsher 		memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
465874aeea5SJeff Kirsher 
466874aeea5SJeff Kirsher 		for (j = 0; j < EFX_TXQ_TYPES; j++) {
467874aeea5SJeff Kirsher 			tx_queue = &channel->tx_queue[j];
468874aeea5SJeff Kirsher 			if (tx_queue->channel)
469874aeea5SJeff Kirsher 				tx_queue->channel = channel;
470874aeea5SJeff Kirsher 			tx_queue->buffer = NULL;
471874aeea5SJeff Kirsher 			memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
472874aeea5SJeff Kirsher 		}
473874aeea5SJeff Kirsher 	} else {
474874aeea5SJeff Kirsher 		channel = kzalloc(sizeof(*channel), GFP_KERNEL);
475874aeea5SJeff Kirsher 		if (!channel)
476874aeea5SJeff Kirsher 			return NULL;
477874aeea5SJeff Kirsher 
478874aeea5SJeff Kirsher 		channel->efx = efx;
479874aeea5SJeff Kirsher 		channel->channel = i;
480874aeea5SJeff Kirsher 
481874aeea5SJeff Kirsher 		for (j = 0; j < EFX_TXQ_TYPES; j++) {
482874aeea5SJeff Kirsher 			tx_queue = &channel->tx_queue[j];
483874aeea5SJeff Kirsher 			tx_queue->efx = efx;
484874aeea5SJeff Kirsher 			tx_queue->queue = i * EFX_TXQ_TYPES + j;
485874aeea5SJeff Kirsher 			tx_queue->channel = channel;
486874aeea5SJeff Kirsher 		}
487874aeea5SJeff Kirsher 	}
488874aeea5SJeff Kirsher 
489874aeea5SJeff Kirsher 	rx_queue = &channel->rx_queue;
490874aeea5SJeff Kirsher 	rx_queue->efx = efx;
491874aeea5SJeff Kirsher 	setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
492874aeea5SJeff Kirsher 		    (unsigned long)rx_queue);
493874aeea5SJeff Kirsher 
494874aeea5SJeff Kirsher 	return channel;
495874aeea5SJeff Kirsher }
496874aeea5SJeff Kirsher 
497874aeea5SJeff Kirsher static int efx_probe_channel(struct efx_channel *channel)
498874aeea5SJeff Kirsher {
499874aeea5SJeff Kirsher 	struct efx_tx_queue *tx_queue;
500874aeea5SJeff Kirsher 	struct efx_rx_queue *rx_queue;
501874aeea5SJeff Kirsher 	int rc;
502874aeea5SJeff Kirsher 
503874aeea5SJeff Kirsher 	netif_dbg(channel->efx, probe, channel->efx->net_dev,
504874aeea5SJeff Kirsher 		  "creating channel %d\n", channel->channel);
505874aeea5SJeff Kirsher 
506874aeea5SJeff Kirsher 	rc = efx_probe_eventq(channel);
507874aeea5SJeff Kirsher 	if (rc)
508874aeea5SJeff Kirsher 		goto fail1;
509874aeea5SJeff Kirsher 
510874aeea5SJeff Kirsher 	efx_for_each_channel_tx_queue(tx_queue, channel) {
511874aeea5SJeff Kirsher 		rc = efx_probe_tx_queue(tx_queue);
512874aeea5SJeff Kirsher 		if (rc)
513874aeea5SJeff Kirsher 			goto fail2;
514874aeea5SJeff Kirsher 	}
515874aeea5SJeff Kirsher 
516874aeea5SJeff Kirsher 	efx_for_each_channel_rx_queue(rx_queue, channel) {
517874aeea5SJeff Kirsher 		rc = efx_probe_rx_queue(rx_queue);
518874aeea5SJeff Kirsher 		if (rc)
519874aeea5SJeff Kirsher 			goto fail3;
520874aeea5SJeff Kirsher 	}
521874aeea5SJeff Kirsher 
522874aeea5SJeff Kirsher 	channel->n_rx_frm_trunc = 0;
523874aeea5SJeff Kirsher 
524874aeea5SJeff Kirsher 	return 0;
525874aeea5SJeff Kirsher 
526874aeea5SJeff Kirsher  fail3:
527874aeea5SJeff Kirsher 	efx_for_each_channel_rx_queue(rx_queue, channel)
528874aeea5SJeff Kirsher 		efx_remove_rx_queue(rx_queue);
529874aeea5SJeff Kirsher  fail2:
530874aeea5SJeff Kirsher 	efx_for_each_channel_tx_queue(tx_queue, channel)
531874aeea5SJeff Kirsher 		efx_remove_tx_queue(tx_queue);
532874aeea5SJeff Kirsher  fail1:
533874aeea5SJeff Kirsher 	return rc;
534874aeea5SJeff Kirsher }
535874aeea5SJeff Kirsher 
536874aeea5SJeff Kirsher 
537874aeea5SJeff Kirsher static void efx_set_channel_names(struct efx_nic *efx)
538874aeea5SJeff Kirsher {
539874aeea5SJeff Kirsher 	struct efx_channel *channel;
540874aeea5SJeff Kirsher 	const char *type = "";
541874aeea5SJeff Kirsher 	int number;
542874aeea5SJeff Kirsher 
543874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx) {
544874aeea5SJeff Kirsher 		number = channel->channel;
545874aeea5SJeff Kirsher 		if (efx->n_channels > efx->n_rx_channels) {
546874aeea5SJeff Kirsher 			if (channel->channel < efx->n_rx_channels) {
547874aeea5SJeff Kirsher 				type = "-rx";
548874aeea5SJeff Kirsher 			} else {
549874aeea5SJeff Kirsher 				type = "-tx";
550874aeea5SJeff Kirsher 				number -= efx->n_rx_channels;
551874aeea5SJeff Kirsher 			}
552874aeea5SJeff Kirsher 		}
553874aeea5SJeff Kirsher 		snprintf(efx->channel_name[channel->channel],
554874aeea5SJeff Kirsher 			 sizeof(efx->channel_name[0]),
555874aeea5SJeff Kirsher 			 "%s%s-%d", efx->name, type, number);
556874aeea5SJeff Kirsher 	}
557874aeea5SJeff Kirsher }
558874aeea5SJeff Kirsher 
559874aeea5SJeff Kirsher static int efx_probe_channels(struct efx_nic *efx)
560874aeea5SJeff Kirsher {
561874aeea5SJeff Kirsher 	struct efx_channel *channel;
562874aeea5SJeff Kirsher 	int rc;
563874aeea5SJeff Kirsher 
564874aeea5SJeff Kirsher 	/* Restart special buffer allocation */
565874aeea5SJeff Kirsher 	efx->next_buffer_table = 0;
566874aeea5SJeff Kirsher 
567874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx) {
568874aeea5SJeff Kirsher 		rc = efx_probe_channel(channel);
569874aeea5SJeff Kirsher 		if (rc) {
570874aeea5SJeff Kirsher 			netif_err(efx, probe, efx->net_dev,
571874aeea5SJeff Kirsher 				  "failed to create channel %d\n",
572874aeea5SJeff Kirsher 				  channel->channel);
573874aeea5SJeff Kirsher 			goto fail;
574874aeea5SJeff Kirsher 		}
575874aeea5SJeff Kirsher 	}
576874aeea5SJeff Kirsher 	efx_set_channel_names(efx);
577874aeea5SJeff Kirsher 
578874aeea5SJeff Kirsher 	return 0;
579874aeea5SJeff Kirsher 
580874aeea5SJeff Kirsher fail:
581874aeea5SJeff Kirsher 	efx_remove_channels(efx);
582874aeea5SJeff Kirsher 	return rc;
583874aeea5SJeff Kirsher }
584874aeea5SJeff Kirsher 
585874aeea5SJeff Kirsher /* Channels are shutdown and reinitialised whilst the NIC is running
586874aeea5SJeff Kirsher  * to propagate configuration changes (mtu, checksum offload), or
587874aeea5SJeff Kirsher  * to clear hardware error conditions
588874aeea5SJeff Kirsher  */
5899f2cb71cSBen Hutchings static void efx_start_datapath(struct efx_nic *efx)
590874aeea5SJeff Kirsher {
591874aeea5SJeff Kirsher 	struct efx_tx_queue *tx_queue;
592874aeea5SJeff Kirsher 	struct efx_rx_queue *rx_queue;
593874aeea5SJeff Kirsher 	struct efx_channel *channel;
594874aeea5SJeff Kirsher 
595874aeea5SJeff Kirsher 	/* Calculate the rx buffer allocation parameters required to
596874aeea5SJeff Kirsher 	 * support the current MTU, including padding for header
597874aeea5SJeff Kirsher 	 * alignment and overruns.
598874aeea5SJeff Kirsher 	 */
599874aeea5SJeff Kirsher 	efx->rx_buffer_len = (max(EFX_PAGE_IP_ALIGN, NET_IP_ALIGN) +
600874aeea5SJeff Kirsher 			      EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
601874aeea5SJeff Kirsher 			      efx->type->rx_buffer_hash_size +
602874aeea5SJeff Kirsher 			      efx->type->rx_buffer_padding);
603874aeea5SJeff Kirsher 	efx->rx_buffer_order = get_order(efx->rx_buffer_len +
604874aeea5SJeff Kirsher 					 sizeof(struct efx_rx_page_state));
605874aeea5SJeff Kirsher 
606874aeea5SJeff Kirsher 	/* Initialise the channels */
607874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx) {
608874aeea5SJeff Kirsher 		efx_for_each_channel_tx_queue(tx_queue, channel)
609874aeea5SJeff Kirsher 			efx_init_tx_queue(tx_queue);
610874aeea5SJeff Kirsher 
611874aeea5SJeff Kirsher 		/* The rx buffer allocation strategy is MTU dependent */
612874aeea5SJeff Kirsher 		efx_rx_strategy(channel);
613874aeea5SJeff Kirsher 
6149f2cb71cSBen Hutchings 		efx_for_each_channel_rx_queue(rx_queue, channel) {
615874aeea5SJeff Kirsher 			efx_init_rx_queue(rx_queue);
6169f2cb71cSBen Hutchings 			efx_nic_generate_fill_event(rx_queue);
6179f2cb71cSBen Hutchings 		}
618874aeea5SJeff Kirsher 
619874aeea5SJeff Kirsher 		WARN_ON(channel->rx_pkt != NULL);
620874aeea5SJeff Kirsher 		efx_rx_strategy(channel);
621874aeea5SJeff Kirsher 	}
6229f2cb71cSBen Hutchings 
6239f2cb71cSBen Hutchings 	if (netif_device_present(efx->net_dev))
6249f2cb71cSBen Hutchings 		netif_tx_wake_all_queues(efx->net_dev);
625874aeea5SJeff Kirsher }
626874aeea5SJeff Kirsher 
6279f2cb71cSBen Hutchings static void efx_stop_datapath(struct efx_nic *efx)
628874aeea5SJeff Kirsher {
629874aeea5SJeff Kirsher 	struct efx_channel *channel;
630874aeea5SJeff Kirsher 	struct efx_tx_queue *tx_queue;
631874aeea5SJeff Kirsher 	struct efx_rx_queue *rx_queue;
632874aeea5SJeff Kirsher 	int rc;
633874aeea5SJeff Kirsher 
634874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
635874aeea5SJeff Kirsher 	BUG_ON(efx->port_enabled);
636874aeea5SJeff Kirsher 
637874aeea5SJeff Kirsher 	rc = efx_nic_flush_queues(efx);
638874aeea5SJeff Kirsher 	if (rc && EFX_WORKAROUND_7803(efx)) {
639874aeea5SJeff Kirsher 		/* Schedule a reset to recover from the flush failure. The
640874aeea5SJeff Kirsher 		 * descriptor caches reference memory we're about to free,
641874aeea5SJeff Kirsher 		 * but falcon_reconfigure_mac_wrapper() won't reconnect
642874aeea5SJeff Kirsher 		 * the MACs because of the pending reset. */
643874aeea5SJeff Kirsher 		netif_err(efx, drv, efx->net_dev,
644874aeea5SJeff Kirsher 			  "Resetting to recover from flush failure\n");
645874aeea5SJeff Kirsher 		efx_schedule_reset(efx, RESET_TYPE_ALL);
646874aeea5SJeff Kirsher 	} else if (rc) {
647874aeea5SJeff Kirsher 		netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
648874aeea5SJeff Kirsher 	} else {
649874aeea5SJeff Kirsher 		netif_dbg(efx, drv, efx->net_dev,
650874aeea5SJeff Kirsher 			  "successfully flushed all queues\n");
651874aeea5SJeff Kirsher 	}
652874aeea5SJeff Kirsher 
653874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx) {
6549f2cb71cSBen Hutchings 		/* RX packet processing is pipelined, so wait for the
6559f2cb71cSBen Hutchings 		 * NAPI handler to complete.  At least event queue 0
6569f2cb71cSBen Hutchings 		 * might be kept active by non-data events, so don't
6579f2cb71cSBen Hutchings 		 * use napi_synchronize() but actually disable NAPI
6589f2cb71cSBen Hutchings 		 * temporarily.
6599f2cb71cSBen Hutchings 		 */
6609f2cb71cSBen Hutchings 		if (efx_channel_has_rx_queue(channel)) {
6619f2cb71cSBen Hutchings 			efx_stop_eventq(channel);
6629f2cb71cSBen Hutchings 			efx_start_eventq(channel);
6639f2cb71cSBen Hutchings 		}
664874aeea5SJeff Kirsher 
665874aeea5SJeff Kirsher 		efx_for_each_channel_rx_queue(rx_queue, channel)
666874aeea5SJeff Kirsher 			efx_fini_rx_queue(rx_queue);
667874aeea5SJeff Kirsher 		efx_for_each_possible_channel_tx_queue(tx_queue, channel)
668874aeea5SJeff Kirsher 			efx_fini_tx_queue(tx_queue);
669874aeea5SJeff Kirsher 	}
670874aeea5SJeff Kirsher }
671874aeea5SJeff Kirsher 
672874aeea5SJeff Kirsher static void efx_remove_channel(struct efx_channel *channel)
673874aeea5SJeff Kirsher {
674874aeea5SJeff Kirsher 	struct efx_tx_queue *tx_queue;
675874aeea5SJeff Kirsher 	struct efx_rx_queue *rx_queue;
676874aeea5SJeff Kirsher 
677874aeea5SJeff Kirsher 	netif_dbg(channel->efx, drv, channel->efx->net_dev,
678874aeea5SJeff Kirsher 		  "destroy chan %d\n", channel->channel);
679874aeea5SJeff Kirsher 
680874aeea5SJeff Kirsher 	efx_for_each_channel_rx_queue(rx_queue, channel)
681874aeea5SJeff Kirsher 		efx_remove_rx_queue(rx_queue);
682874aeea5SJeff Kirsher 	efx_for_each_possible_channel_tx_queue(tx_queue, channel)
683874aeea5SJeff Kirsher 		efx_remove_tx_queue(tx_queue);
684874aeea5SJeff Kirsher 	efx_remove_eventq(channel);
685874aeea5SJeff Kirsher }
686874aeea5SJeff Kirsher 
687874aeea5SJeff Kirsher static void efx_remove_channels(struct efx_nic *efx)
688874aeea5SJeff Kirsher {
689874aeea5SJeff Kirsher 	struct efx_channel *channel;
690874aeea5SJeff Kirsher 
691874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx)
692874aeea5SJeff Kirsher 		efx_remove_channel(channel);
693874aeea5SJeff Kirsher }
694874aeea5SJeff Kirsher 
695874aeea5SJeff Kirsher int
696874aeea5SJeff Kirsher efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
697874aeea5SJeff Kirsher {
698874aeea5SJeff Kirsher 	struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
699874aeea5SJeff Kirsher 	u32 old_rxq_entries, old_txq_entries;
700874aeea5SJeff Kirsher 	unsigned i;
701874aeea5SJeff Kirsher 	int rc;
702874aeea5SJeff Kirsher 
703874aeea5SJeff Kirsher 	efx_stop_all(efx);
7049f2cb71cSBen Hutchings 	efx_stop_interrupts(efx);
705874aeea5SJeff Kirsher 
706874aeea5SJeff Kirsher 	/* Clone channels */
707874aeea5SJeff Kirsher 	memset(other_channel, 0, sizeof(other_channel));
708874aeea5SJeff Kirsher 	for (i = 0; i < efx->n_channels; i++) {
709874aeea5SJeff Kirsher 		channel = efx_alloc_channel(efx, i, efx->channel[i]);
710874aeea5SJeff Kirsher 		if (!channel) {
711874aeea5SJeff Kirsher 			rc = -ENOMEM;
712874aeea5SJeff Kirsher 			goto out;
713874aeea5SJeff Kirsher 		}
714874aeea5SJeff Kirsher 		other_channel[i] = channel;
715874aeea5SJeff Kirsher 	}
716874aeea5SJeff Kirsher 
717874aeea5SJeff Kirsher 	/* Swap entry counts and channel pointers */
718874aeea5SJeff Kirsher 	old_rxq_entries = efx->rxq_entries;
719874aeea5SJeff Kirsher 	old_txq_entries = efx->txq_entries;
720874aeea5SJeff Kirsher 	efx->rxq_entries = rxq_entries;
721874aeea5SJeff Kirsher 	efx->txq_entries = txq_entries;
722874aeea5SJeff Kirsher 	for (i = 0; i < efx->n_channels; i++) {
723874aeea5SJeff Kirsher 		channel = efx->channel[i];
724874aeea5SJeff Kirsher 		efx->channel[i] = other_channel[i];
725874aeea5SJeff Kirsher 		other_channel[i] = channel;
726874aeea5SJeff Kirsher 	}
727874aeea5SJeff Kirsher 
728874aeea5SJeff Kirsher 	rc = efx_probe_channels(efx);
729874aeea5SJeff Kirsher 	if (rc)
730874aeea5SJeff Kirsher 		goto rollback;
731874aeea5SJeff Kirsher 
732874aeea5SJeff Kirsher 	efx_init_napi(efx);
733874aeea5SJeff Kirsher 
734874aeea5SJeff Kirsher 	/* Destroy old channels */
735874aeea5SJeff Kirsher 	for (i = 0; i < efx->n_channels; i++) {
736874aeea5SJeff Kirsher 		efx_fini_napi_channel(other_channel[i]);
737874aeea5SJeff Kirsher 		efx_remove_channel(other_channel[i]);
738874aeea5SJeff Kirsher 	}
739874aeea5SJeff Kirsher out:
740874aeea5SJeff Kirsher 	/* Free unused channel structures */
741874aeea5SJeff Kirsher 	for (i = 0; i < efx->n_channels; i++)
742874aeea5SJeff Kirsher 		kfree(other_channel[i]);
743874aeea5SJeff Kirsher 
7449f2cb71cSBen Hutchings 	efx_start_interrupts(efx);
745874aeea5SJeff Kirsher 	efx_start_all(efx);
746874aeea5SJeff Kirsher 	return rc;
747874aeea5SJeff Kirsher 
748874aeea5SJeff Kirsher rollback:
749874aeea5SJeff Kirsher 	/* Swap back */
750874aeea5SJeff Kirsher 	efx->rxq_entries = old_rxq_entries;
751874aeea5SJeff Kirsher 	efx->txq_entries = old_txq_entries;
752874aeea5SJeff Kirsher 	for (i = 0; i < efx->n_channels; i++) {
753874aeea5SJeff Kirsher 		channel = efx->channel[i];
754874aeea5SJeff Kirsher 		efx->channel[i] = other_channel[i];
755874aeea5SJeff Kirsher 		other_channel[i] = channel;
756874aeea5SJeff Kirsher 	}
757874aeea5SJeff Kirsher 	goto out;
758874aeea5SJeff Kirsher }
759874aeea5SJeff Kirsher 
760874aeea5SJeff Kirsher void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
761874aeea5SJeff Kirsher {
762874aeea5SJeff Kirsher 	mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
763874aeea5SJeff Kirsher }
764874aeea5SJeff Kirsher 
765874aeea5SJeff Kirsher /**************************************************************************
766874aeea5SJeff Kirsher  *
767874aeea5SJeff Kirsher  * Port handling
768874aeea5SJeff Kirsher  *
769874aeea5SJeff Kirsher  **************************************************************************/
770874aeea5SJeff Kirsher 
771874aeea5SJeff Kirsher /* This ensures that the kernel is kept informed (via
772874aeea5SJeff Kirsher  * netif_carrier_on/off) of the link status, and also maintains the
773874aeea5SJeff Kirsher  * link status's stop on the port's TX queue.
774874aeea5SJeff Kirsher  */
775874aeea5SJeff Kirsher void efx_link_status_changed(struct efx_nic *efx)
776874aeea5SJeff Kirsher {
777874aeea5SJeff Kirsher 	struct efx_link_state *link_state = &efx->link_state;
778874aeea5SJeff Kirsher 
779874aeea5SJeff Kirsher 	/* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
780874aeea5SJeff Kirsher 	 * that no events are triggered between unregister_netdev() and the
781874aeea5SJeff Kirsher 	 * driver unloading. A more general condition is that NETDEV_CHANGE
782874aeea5SJeff Kirsher 	 * can only be generated between NETDEV_UP and NETDEV_DOWN */
783874aeea5SJeff Kirsher 	if (!netif_running(efx->net_dev))
784874aeea5SJeff Kirsher 		return;
785874aeea5SJeff Kirsher 
786874aeea5SJeff Kirsher 	if (link_state->up != netif_carrier_ok(efx->net_dev)) {
787874aeea5SJeff Kirsher 		efx->n_link_state_changes++;
788874aeea5SJeff Kirsher 
789874aeea5SJeff Kirsher 		if (link_state->up)
790874aeea5SJeff Kirsher 			netif_carrier_on(efx->net_dev);
791874aeea5SJeff Kirsher 		else
792874aeea5SJeff Kirsher 			netif_carrier_off(efx->net_dev);
793874aeea5SJeff Kirsher 	}
794874aeea5SJeff Kirsher 
795874aeea5SJeff Kirsher 	/* Status message for kernel log */
7962aa9ef11SBen Hutchings 	if (link_state->up)
797874aeea5SJeff Kirsher 		netif_info(efx, link, efx->net_dev,
798874aeea5SJeff Kirsher 			   "link up at %uMbps %s-duplex (MTU %d)%s\n",
799874aeea5SJeff Kirsher 			   link_state->speed, link_state->fd ? "full" : "half",
800874aeea5SJeff Kirsher 			   efx->net_dev->mtu,
801874aeea5SJeff Kirsher 			   (efx->promiscuous ? " [PROMISC]" : ""));
8022aa9ef11SBen Hutchings 	else
803874aeea5SJeff Kirsher 		netif_info(efx, link, efx->net_dev, "link down\n");
804874aeea5SJeff Kirsher }
805874aeea5SJeff Kirsher 
806874aeea5SJeff Kirsher void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
807874aeea5SJeff Kirsher {
808874aeea5SJeff Kirsher 	efx->link_advertising = advertising;
809874aeea5SJeff Kirsher 	if (advertising) {
810874aeea5SJeff Kirsher 		if (advertising & ADVERTISED_Pause)
811874aeea5SJeff Kirsher 			efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
812874aeea5SJeff Kirsher 		else
813874aeea5SJeff Kirsher 			efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
814874aeea5SJeff Kirsher 		if (advertising & ADVERTISED_Asym_Pause)
815874aeea5SJeff Kirsher 			efx->wanted_fc ^= EFX_FC_TX;
816874aeea5SJeff Kirsher 	}
817874aeea5SJeff Kirsher }
818874aeea5SJeff Kirsher 
819874aeea5SJeff Kirsher void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
820874aeea5SJeff Kirsher {
821874aeea5SJeff Kirsher 	efx->wanted_fc = wanted_fc;
822874aeea5SJeff Kirsher 	if (efx->link_advertising) {
823874aeea5SJeff Kirsher 		if (wanted_fc & EFX_FC_RX)
824874aeea5SJeff Kirsher 			efx->link_advertising |= (ADVERTISED_Pause |
825874aeea5SJeff Kirsher 						  ADVERTISED_Asym_Pause);
826874aeea5SJeff Kirsher 		else
827874aeea5SJeff Kirsher 			efx->link_advertising &= ~(ADVERTISED_Pause |
828874aeea5SJeff Kirsher 						   ADVERTISED_Asym_Pause);
829874aeea5SJeff Kirsher 		if (wanted_fc & EFX_FC_TX)
830874aeea5SJeff Kirsher 			efx->link_advertising ^= ADVERTISED_Asym_Pause;
831874aeea5SJeff Kirsher 	}
832874aeea5SJeff Kirsher }
833874aeea5SJeff Kirsher 
834874aeea5SJeff Kirsher static void efx_fini_port(struct efx_nic *efx);
835874aeea5SJeff Kirsher 
836874aeea5SJeff Kirsher /* Push loopback/power/transmit disable settings to the PHY, and reconfigure
837874aeea5SJeff Kirsher  * the MAC appropriately. All other PHY configuration changes are pushed
838874aeea5SJeff Kirsher  * through phy_op->set_settings(), and pushed asynchronously to the MAC
839874aeea5SJeff Kirsher  * through efx_monitor().
840874aeea5SJeff Kirsher  *
841874aeea5SJeff Kirsher  * Callers must hold the mac_lock
842874aeea5SJeff Kirsher  */
843874aeea5SJeff Kirsher int __efx_reconfigure_port(struct efx_nic *efx)
844874aeea5SJeff Kirsher {
845874aeea5SJeff Kirsher 	enum efx_phy_mode phy_mode;
846874aeea5SJeff Kirsher 	int rc;
847874aeea5SJeff Kirsher 
848874aeea5SJeff Kirsher 	WARN_ON(!mutex_is_locked(&efx->mac_lock));
849874aeea5SJeff Kirsher 
8500fca8c97SBen Hutchings 	/* Serialise the promiscuous flag with efx_set_rx_mode. */
851874aeea5SJeff Kirsher 	netif_addr_lock_bh(efx->net_dev);
852874aeea5SJeff Kirsher 	netif_addr_unlock_bh(efx->net_dev);
853874aeea5SJeff Kirsher 
854874aeea5SJeff Kirsher 	/* Disable PHY transmit in mac level loopbacks */
855874aeea5SJeff Kirsher 	phy_mode = efx->phy_mode;
856874aeea5SJeff Kirsher 	if (LOOPBACK_INTERNAL(efx))
857874aeea5SJeff Kirsher 		efx->phy_mode |= PHY_MODE_TX_DISABLED;
858874aeea5SJeff Kirsher 	else
859874aeea5SJeff Kirsher 		efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
860874aeea5SJeff Kirsher 
861874aeea5SJeff Kirsher 	rc = efx->type->reconfigure_port(efx);
862874aeea5SJeff Kirsher 
863874aeea5SJeff Kirsher 	if (rc)
864874aeea5SJeff Kirsher 		efx->phy_mode = phy_mode;
865874aeea5SJeff Kirsher 
866874aeea5SJeff Kirsher 	return rc;
867874aeea5SJeff Kirsher }
868874aeea5SJeff Kirsher 
869874aeea5SJeff Kirsher /* Reinitialise the MAC to pick up new PHY settings, even if the port is
870874aeea5SJeff Kirsher  * disabled. */
871874aeea5SJeff Kirsher int efx_reconfigure_port(struct efx_nic *efx)
872874aeea5SJeff Kirsher {
873874aeea5SJeff Kirsher 	int rc;
874874aeea5SJeff Kirsher 
875874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
876874aeea5SJeff Kirsher 
877874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
878874aeea5SJeff Kirsher 	rc = __efx_reconfigure_port(efx);
879874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
880874aeea5SJeff Kirsher 
881874aeea5SJeff Kirsher 	return rc;
882874aeea5SJeff Kirsher }
883874aeea5SJeff Kirsher 
884874aeea5SJeff Kirsher /* Asynchronous work item for changing MAC promiscuity and multicast
885874aeea5SJeff Kirsher  * hash.  Avoid a drain/rx_ingress enable by reconfiguring the current
886874aeea5SJeff Kirsher  * MAC directly. */
887874aeea5SJeff Kirsher static void efx_mac_work(struct work_struct *data)
888874aeea5SJeff Kirsher {
889874aeea5SJeff Kirsher 	struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
890874aeea5SJeff Kirsher 
891874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
89230b81cdaSBen Hutchings 	if (efx->port_enabled)
893710b208dSBen Hutchings 		efx->type->reconfigure_mac(efx);
894874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
895874aeea5SJeff Kirsher }
896874aeea5SJeff Kirsher 
897874aeea5SJeff Kirsher static int efx_probe_port(struct efx_nic *efx)
898874aeea5SJeff Kirsher {
899874aeea5SJeff Kirsher 	int rc;
900874aeea5SJeff Kirsher 
901874aeea5SJeff Kirsher 	netif_dbg(efx, probe, efx->net_dev, "create port\n");
902874aeea5SJeff Kirsher 
903874aeea5SJeff Kirsher 	if (phy_flash_cfg)
904874aeea5SJeff Kirsher 		efx->phy_mode = PHY_MODE_SPECIAL;
905874aeea5SJeff Kirsher 
906874aeea5SJeff Kirsher 	/* Connect up MAC/PHY operations table */
907874aeea5SJeff Kirsher 	rc = efx->type->probe_port(efx);
908874aeea5SJeff Kirsher 	if (rc)
909874aeea5SJeff Kirsher 		return rc;
910874aeea5SJeff Kirsher 
911e332bcb3SBen Hutchings 	/* Initialise MAC address to permanent address */
912e332bcb3SBen Hutchings 	memcpy(efx->net_dev->dev_addr, efx->net_dev->perm_addr, ETH_ALEN);
913874aeea5SJeff Kirsher 
914874aeea5SJeff Kirsher 	return 0;
915874aeea5SJeff Kirsher }
916874aeea5SJeff Kirsher 
917874aeea5SJeff Kirsher static int efx_init_port(struct efx_nic *efx)
918874aeea5SJeff Kirsher {
919874aeea5SJeff Kirsher 	int rc;
920874aeea5SJeff Kirsher 
921874aeea5SJeff Kirsher 	netif_dbg(efx, drv, efx->net_dev, "init port\n");
922874aeea5SJeff Kirsher 
923874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
924874aeea5SJeff Kirsher 
925874aeea5SJeff Kirsher 	rc = efx->phy_op->init(efx);
926874aeea5SJeff Kirsher 	if (rc)
927874aeea5SJeff Kirsher 		goto fail1;
928874aeea5SJeff Kirsher 
929874aeea5SJeff Kirsher 	efx->port_initialized = true;
930874aeea5SJeff Kirsher 
931874aeea5SJeff Kirsher 	/* Reconfigure the MAC before creating dma queues (required for
932874aeea5SJeff Kirsher 	 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
933710b208dSBen Hutchings 	efx->type->reconfigure_mac(efx);
934874aeea5SJeff Kirsher 
935874aeea5SJeff Kirsher 	/* Ensure the PHY advertises the correct flow control settings */
936874aeea5SJeff Kirsher 	rc = efx->phy_op->reconfigure(efx);
937874aeea5SJeff Kirsher 	if (rc)
938874aeea5SJeff Kirsher 		goto fail2;
939874aeea5SJeff Kirsher 
940874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
941874aeea5SJeff Kirsher 	return 0;
942874aeea5SJeff Kirsher 
943874aeea5SJeff Kirsher fail2:
944874aeea5SJeff Kirsher 	efx->phy_op->fini(efx);
945874aeea5SJeff Kirsher fail1:
946874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
947874aeea5SJeff Kirsher 	return rc;
948874aeea5SJeff Kirsher }
949874aeea5SJeff Kirsher 
950874aeea5SJeff Kirsher static void efx_start_port(struct efx_nic *efx)
951874aeea5SJeff Kirsher {
952874aeea5SJeff Kirsher 	netif_dbg(efx, ifup, efx->net_dev, "start port\n");
953874aeea5SJeff Kirsher 	BUG_ON(efx->port_enabled);
954874aeea5SJeff Kirsher 
955874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
956874aeea5SJeff Kirsher 	efx->port_enabled = true;
957874aeea5SJeff Kirsher 
958874aeea5SJeff Kirsher 	/* efx_mac_work() might have been scheduled after efx_stop_port(),
959874aeea5SJeff Kirsher 	 * and then cancelled by efx_flush_all() */
960710b208dSBen Hutchings 	efx->type->reconfigure_mac(efx);
961874aeea5SJeff Kirsher 
962874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
963874aeea5SJeff Kirsher }
964874aeea5SJeff Kirsher 
965874aeea5SJeff Kirsher /* Prevent efx_mac_work() and efx_monitor() from working */
966874aeea5SJeff Kirsher static void efx_stop_port(struct efx_nic *efx)
967874aeea5SJeff Kirsher {
968874aeea5SJeff Kirsher 	netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
969874aeea5SJeff Kirsher 
970874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
971874aeea5SJeff Kirsher 	efx->port_enabled = false;
972874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
973874aeea5SJeff Kirsher 
974874aeea5SJeff Kirsher 	/* Serialise against efx_set_multicast_list() */
975874aeea5SJeff Kirsher 	netif_addr_lock_bh(efx->net_dev);
976874aeea5SJeff Kirsher 	netif_addr_unlock_bh(efx->net_dev);
977874aeea5SJeff Kirsher }
978874aeea5SJeff Kirsher 
979874aeea5SJeff Kirsher static void efx_fini_port(struct efx_nic *efx)
980874aeea5SJeff Kirsher {
981874aeea5SJeff Kirsher 	netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
982874aeea5SJeff Kirsher 
983874aeea5SJeff Kirsher 	if (!efx->port_initialized)
984874aeea5SJeff Kirsher 		return;
985874aeea5SJeff Kirsher 
986874aeea5SJeff Kirsher 	efx->phy_op->fini(efx);
987874aeea5SJeff Kirsher 	efx->port_initialized = false;
988874aeea5SJeff Kirsher 
989874aeea5SJeff Kirsher 	efx->link_state.up = false;
990874aeea5SJeff Kirsher 	efx_link_status_changed(efx);
991874aeea5SJeff Kirsher }
992874aeea5SJeff Kirsher 
993874aeea5SJeff Kirsher static void efx_remove_port(struct efx_nic *efx)
994874aeea5SJeff Kirsher {
995874aeea5SJeff Kirsher 	netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
996874aeea5SJeff Kirsher 
997874aeea5SJeff Kirsher 	efx->type->remove_port(efx);
998874aeea5SJeff Kirsher }
999874aeea5SJeff Kirsher 
1000874aeea5SJeff Kirsher /**************************************************************************
1001874aeea5SJeff Kirsher  *
1002874aeea5SJeff Kirsher  * NIC handling
1003874aeea5SJeff Kirsher  *
1004874aeea5SJeff Kirsher  **************************************************************************/
1005874aeea5SJeff Kirsher 
1006874aeea5SJeff Kirsher /* This configures the PCI device to enable I/O and DMA. */
1007874aeea5SJeff Kirsher static int efx_init_io(struct efx_nic *efx)
1008874aeea5SJeff Kirsher {
1009874aeea5SJeff Kirsher 	struct pci_dev *pci_dev = efx->pci_dev;
1010874aeea5SJeff Kirsher 	dma_addr_t dma_mask = efx->type->max_dma_mask;
1011874aeea5SJeff Kirsher 	int rc;
1012874aeea5SJeff Kirsher 
1013874aeea5SJeff Kirsher 	netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
1014874aeea5SJeff Kirsher 
1015874aeea5SJeff Kirsher 	rc = pci_enable_device(pci_dev);
1016874aeea5SJeff Kirsher 	if (rc) {
1017874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev,
1018874aeea5SJeff Kirsher 			  "failed to enable PCI device\n");
1019874aeea5SJeff Kirsher 		goto fail1;
1020874aeea5SJeff Kirsher 	}
1021874aeea5SJeff Kirsher 
1022874aeea5SJeff Kirsher 	pci_set_master(pci_dev);
1023874aeea5SJeff Kirsher 
1024874aeea5SJeff Kirsher 	/* Set the PCI DMA mask.  Try all possibilities from our
1025874aeea5SJeff Kirsher 	 * genuine mask down to 32 bits, because some architectures
1026874aeea5SJeff Kirsher 	 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
1027874aeea5SJeff Kirsher 	 * masks event though they reject 46 bit masks.
1028874aeea5SJeff Kirsher 	 */
1029874aeea5SJeff Kirsher 	while (dma_mask > 0x7fffffffUL) {
1030e9e01846SBen Hutchings 		if (pci_dma_supported(pci_dev, dma_mask)) {
1031e9e01846SBen Hutchings 			rc = pci_set_dma_mask(pci_dev, dma_mask);
1032e9e01846SBen Hutchings 			if (rc == 0)
1033874aeea5SJeff Kirsher 				break;
1034e9e01846SBen Hutchings 		}
1035874aeea5SJeff Kirsher 		dma_mask >>= 1;
1036874aeea5SJeff Kirsher 	}
1037874aeea5SJeff Kirsher 	if (rc) {
1038874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev,
1039874aeea5SJeff Kirsher 			  "could not find a suitable DMA mask\n");
1040874aeea5SJeff Kirsher 		goto fail2;
1041874aeea5SJeff Kirsher 	}
1042874aeea5SJeff Kirsher 	netif_dbg(efx, probe, efx->net_dev,
1043874aeea5SJeff Kirsher 		  "using DMA mask %llx\n", (unsigned long long) dma_mask);
1044874aeea5SJeff Kirsher 	rc = pci_set_consistent_dma_mask(pci_dev, dma_mask);
1045874aeea5SJeff Kirsher 	if (rc) {
1046874aeea5SJeff Kirsher 		/* pci_set_consistent_dma_mask() is not *allowed* to
1047874aeea5SJeff Kirsher 		 * fail with a mask that pci_set_dma_mask() accepted,
1048874aeea5SJeff Kirsher 		 * but just in case...
1049874aeea5SJeff Kirsher 		 */
1050874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev,
1051874aeea5SJeff Kirsher 			  "failed to set consistent DMA mask\n");
1052874aeea5SJeff Kirsher 		goto fail2;
1053874aeea5SJeff Kirsher 	}
1054874aeea5SJeff Kirsher 
1055874aeea5SJeff Kirsher 	efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
1056874aeea5SJeff Kirsher 	rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
1057874aeea5SJeff Kirsher 	if (rc) {
1058874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev,
1059874aeea5SJeff Kirsher 			  "request for memory BAR failed\n");
1060874aeea5SJeff Kirsher 		rc = -EIO;
1061874aeea5SJeff Kirsher 		goto fail3;
1062874aeea5SJeff Kirsher 	}
1063874aeea5SJeff Kirsher 	efx->membase = ioremap_nocache(efx->membase_phys,
1064874aeea5SJeff Kirsher 				       efx->type->mem_map_size);
1065874aeea5SJeff Kirsher 	if (!efx->membase) {
1066874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev,
1067874aeea5SJeff Kirsher 			  "could not map memory BAR at %llx+%x\n",
1068874aeea5SJeff Kirsher 			  (unsigned long long)efx->membase_phys,
1069874aeea5SJeff Kirsher 			  efx->type->mem_map_size);
1070874aeea5SJeff Kirsher 		rc = -ENOMEM;
1071874aeea5SJeff Kirsher 		goto fail4;
1072874aeea5SJeff Kirsher 	}
1073874aeea5SJeff Kirsher 	netif_dbg(efx, probe, efx->net_dev,
1074874aeea5SJeff Kirsher 		  "memory BAR at %llx+%x (virtual %p)\n",
1075874aeea5SJeff Kirsher 		  (unsigned long long)efx->membase_phys,
1076874aeea5SJeff Kirsher 		  efx->type->mem_map_size, efx->membase);
1077874aeea5SJeff Kirsher 
1078874aeea5SJeff Kirsher 	return 0;
1079874aeea5SJeff Kirsher 
1080874aeea5SJeff Kirsher  fail4:
1081874aeea5SJeff Kirsher 	pci_release_region(efx->pci_dev, EFX_MEM_BAR);
1082874aeea5SJeff Kirsher  fail3:
1083874aeea5SJeff Kirsher 	efx->membase_phys = 0;
1084874aeea5SJeff Kirsher  fail2:
1085874aeea5SJeff Kirsher 	pci_disable_device(efx->pci_dev);
1086874aeea5SJeff Kirsher  fail1:
1087874aeea5SJeff Kirsher 	return rc;
1088874aeea5SJeff Kirsher }
1089874aeea5SJeff Kirsher 
1090874aeea5SJeff Kirsher static void efx_fini_io(struct efx_nic *efx)
1091874aeea5SJeff Kirsher {
1092874aeea5SJeff Kirsher 	netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
1093874aeea5SJeff Kirsher 
1094874aeea5SJeff Kirsher 	if (efx->membase) {
1095874aeea5SJeff Kirsher 		iounmap(efx->membase);
1096874aeea5SJeff Kirsher 		efx->membase = NULL;
1097874aeea5SJeff Kirsher 	}
1098874aeea5SJeff Kirsher 
1099874aeea5SJeff Kirsher 	if (efx->membase_phys) {
1100874aeea5SJeff Kirsher 		pci_release_region(efx->pci_dev, EFX_MEM_BAR);
1101874aeea5SJeff Kirsher 		efx->membase_phys = 0;
1102874aeea5SJeff Kirsher 	}
1103874aeea5SJeff Kirsher 
1104874aeea5SJeff Kirsher 	pci_disable_device(efx->pci_dev);
1105874aeea5SJeff Kirsher }
1106874aeea5SJeff Kirsher 
1107a16e5b24SBen Hutchings static unsigned int efx_wanted_parallelism(void)
1108874aeea5SJeff Kirsher {
1109cdb08f8fSBen Hutchings 	cpumask_var_t thread_mask;
1110a16e5b24SBen Hutchings 	unsigned int count;
1111874aeea5SJeff Kirsher 	int cpu;
1112874aeea5SJeff Kirsher 
1113874aeea5SJeff Kirsher 	if (rss_cpus)
1114874aeea5SJeff Kirsher 		return rss_cpus;
1115874aeea5SJeff Kirsher 
1116cdb08f8fSBen Hutchings 	if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
1117874aeea5SJeff Kirsher 		printk(KERN_WARNING
1118874aeea5SJeff Kirsher 		       "sfc: RSS disabled due to allocation failure\n");
1119874aeea5SJeff Kirsher 		return 1;
1120874aeea5SJeff Kirsher 	}
1121874aeea5SJeff Kirsher 
1122874aeea5SJeff Kirsher 	count = 0;
1123874aeea5SJeff Kirsher 	for_each_online_cpu(cpu) {
1124cdb08f8fSBen Hutchings 		if (!cpumask_test_cpu(cpu, thread_mask)) {
1125874aeea5SJeff Kirsher 			++count;
1126cdb08f8fSBen Hutchings 			cpumask_or(thread_mask, thread_mask,
1127cdb08f8fSBen Hutchings 				   topology_thread_cpumask(cpu));
1128874aeea5SJeff Kirsher 		}
1129874aeea5SJeff Kirsher 	}
1130874aeea5SJeff Kirsher 
1131cdb08f8fSBen Hutchings 	free_cpumask_var(thread_mask);
1132874aeea5SJeff Kirsher 	return count;
1133874aeea5SJeff Kirsher }
1134874aeea5SJeff Kirsher 
1135874aeea5SJeff Kirsher static int
1136874aeea5SJeff Kirsher efx_init_rx_cpu_rmap(struct efx_nic *efx, struct msix_entry *xentries)
1137874aeea5SJeff Kirsher {
1138874aeea5SJeff Kirsher #ifdef CONFIG_RFS_ACCEL
1139a16e5b24SBen Hutchings 	unsigned int i;
1140a16e5b24SBen Hutchings 	int rc;
1141874aeea5SJeff Kirsher 
1142874aeea5SJeff Kirsher 	efx->net_dev->rx_cpu_rmap = alloc_irq_cpu_rmap(efx->n_rx_channels);
1143874aeea5SJeff Kirsher 	if (!efx->net_dev->rx_cpu_rmap)
1144874aeea5SJeff Kirsher 		return -ENOMEM;
1145874aeea5SJeff Kirsher 	for (i = 0; i < efx->n_rx_channels; i++) {
1146874aeea5SJeff Kirsher 		rc = irq_cpu_rmap_add(efx->net_dev->rx_cpu_rmap,
1147874aeea5SJeff Kirsher 				      xentries[i].vector);
1148874aeea5SJeff Kirsher 		if (rc) {
1149874aeea5SJeff Kirsher 			free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
1150874aeea5SJeff Kirsher 			efx->net_dev->rx_cpu_rmap = NULL;
1151874aeea5SJeff Kirsher 			return rc;
1152874aeea5SJeff Kirsher 		}
1153874aeea5SJeff Kirsher 	}
1154874aeea5SJeff Kirsher #endif
1155874aeea5SJeff Kirsher 	return 0;
1156874aeea5SJeff Kirsher }
1157874aeea5SJeff Kirsher 
1158874aeea5SJeff Kirsher /* Probe the number and type of interrupts we are able to obtain, and
1159874aeea5SJeff Kirsher  * the resulting numbers of channels and RX queues.
1160874aeea5SJeff Kirsher  */
1161874aeea5SJeff Kirsher static int efx_probe_interrupts(struct efx_nic *efx)
1162874aeea5SJeff Kirsher {
1163a16e5b24SBen Hutchings 	unsigned int max_channels =
1164a16e5b24SBen Hutchings 		min(efx->type->phys_addr_channels, EFX_MAX_CHANNELS);
1165a16e5b24SBen Hutchings 	unsigned int i;
1166a16e5b24SBen Hutchings 	int rc;
1167874aeea5SJeff Kirsher 
1168874aeea5SJeff Kirsher 	if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
1169874aeea5SJeff Kirsher 		struct msix_entry xentries[EFX_MAX_CHANNELS];
1170a16e5b24SBen Hutchings 		unsigned int n_channels;
1171874aeea5SJeff Kirsher 
1172fa142b9dSBen Hutchings 		n_channels = efx_wanted_parallelism();
1173874aeea5SJeff Kirsher 		if (separate_tx_channels)
1174874aeea5SJeff Kirsher 			n_channels *= 2;
1175874aeea5SJeff Kirsher 		n_channels = min(n_channels, max_channels);
1176874aeea5SJeff Kirsher 
1177874aeea5SJeff Kirsher 		for (i = 0; i < n_channels; i++)
1178874aeea5SJeff Kirsher 			xentries[i].entry = i;
1179874aeea5SJeff Kirsher 		rc = pci_enable_msix(efx->pci_dev, xentries, n_channels);
1180874aeea5SJeff Kirsher 		if (rc > 0) {
1181874aeea5SJeff Kirsher 			netif_err(efx, drv, efx->net_dev,
1182874aeea5SJeff Kirsher 				  "WARNING: Insufficient MSI-X vectors"
1183a16e5b24SBen Hutchings 				  " available (%d < %u).\n", rc, n_channels);
1184874aeea5SJeff Kirsher 			netif_err(efx, drv, efx->net_dev,
1185874aeea5SJeff Kirsher 				  "WARNING: Performance may be reduced.\n");
1186874aeea5SJeff Kirsher 			EFX_BUG_ON_PARANOID(rc >= n_channels);
1187874aeea5SJeff Kirsher 			n_channels = rc;
1188874aeea5SJeff Kirsher 			rc = pci_enable_msix(efx->pci_dev, xentries,
1189874aeea5SJeff Kirsher 					     n_channels);
1190874aeea5SJeff Kirsher 		}
1191874aeea5SJeff Kirsher 
1192874aeea5SJeff Kirsher 		if (rc == 0) {
1193874aeea5SJeff Kirsher 			efx->n_channels = n_channels;
1194874aeea5SJeff Kirsher 			if (separate_tx_channels) {
1195874aeea5SJeff Kirsher 				efx->n_tx_channels =
1196874aeea5SJeff Kirsher 					max(efx->n_channels / 2, 1U);
1197874aeea5SJeff Kirsher 				efx->n_rx_channels =
1198874aeea5SJeff Kirsher 					max(efx->n_channels -
1199874aeea5SJeff Kirsher 					    efx->n_tx_channels, 1U);
1200874aeea5SJeff Kirsher 			} else {
1201874aeea5SJeff Kirsher 				efx->n_tx_channels = efx->n_channels;
1202874aeea5SJeff Kirsher 				efx->n_rx_channels = efx->n_channels;
1203874aeea5SJeff Kirsher 			}
1204874aeea5SJeff Kirsher 			rc = efx_init_rx_cpu_rmap(efx, xentries);
1205874aeea5SJeff Kirsher 			if (rc) {
1206874aeea5SJeff Kirsher 				pci_disable_msix(efx->pci_dev);
1207874aeea5SJeff Kirsher 				return rc;
1208874aeea5SJeff Kirsher 			}
1209874aeea5SJeff Kirsher 			for (i = 0; i < n_channels; i++)
1210874aeea5SJeff Kirsher 				efx_get_channel(efx, i)->irq =
1211874aeea5SJeff Kirsher 					xentries[i].vector;
1212874aeea5SJeff Kirsher 		} else {
1213874aeea5SJeff Kirsher 			/* Fall back to single channel MSI */
1214874aeea5SJeff Kirsher 			efx->interrupt_mode = EFX_INT_MODE_MSI;
1215874aeea5SJeff Kirsher 			netif_err(efx, drv, efx->net_dev,
1216874aeea5SJeff Kirsher 				  "could not enable MSI-X\n");
1217874aeea5SJeff Kirsher 		}
1218874aeea5SJeff Kirsher 	}
1219874aeea5SJeff Kirsher 
1220874aeea5SJeff Kirsher 	/* Try single interrupt MSI */
1221874aeea5SJeff Kirsher 	if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
1222874aeea5SJeff Kirsher 		efx->n_channels = 1;
1223874aeea5SJeff Kirsher 		efx->n_rx_channels = 1;
1224874aeea5SJeff Kirsher 		efx->n_tx_channels = 1;
1225874aeea5SJeff Kirsher 		rc = pci_enable_msi(efx->pci_dev);
1226874aeea5SJeff Kirsher 		if (rc == 0) {
1227874aeea5SJeff Kirsher 			efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
1228874aeea5SJeff Kirsher 		} else {
1229874aeea5SJeff Kirsher 			netif_err(efx, drv, efx->net_dev,
1230874aeea5SJeff Kirsher 				  "could not enable MSI\n");
1231874aeea5SJeff Kirsher 			efx->interrupt_mode = EFX_INT_MODE_LEGACY;
1232874aeea5SJeff Kirsher 		}
1233874aeea5SJeff Kirsher 	}
1234874aeea5SJeff Kirsher 
1235874aeea5SJeff Kirsher 	/* Assume legacy interrupts */
1236874aeea5SJeff Kirsher 	if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
1237874aeea5SJeff Kirsher 		efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
1238874aeea5SJeff Kirsher 		efx->n_rx_channels = 1;
1239874aeea5SJeff Kirsher 		efx->n_tx_channels = 1;
1240874aeea5SJeff Kirsher 		efx->legacy_irq = efx->pci_dev->irq;
1241874aeea5SJeff Kirsher 	}
1242874aeea5SJeff Kirsher 
1243874aeea5SJeff Kirsher 	return 0;
1244874aeea5SJeff Kirsher }
1245874aeea5SJeff Kirsher 
12469f2cb71cSBen Hutchings /* Enable interrupts, then probe and start the event queues */
12479f2cb71cSBen Hutchings static void efx_start_interrupts(struct efx_nic *efx)
12489f2cb71cSBen Hutchings {
12499f2cb71cSBen Hutchings 	struct efx_channel *channel;
12509f2cb71cSBen Hutchings 
12519f2cb71cSBen Hutchings 	if (efx->legacy_irq)
12529f2cb71cSBen Hutchings 		efx->legacy_irq_enabled = true;
12539f2cb71cSBen Hutchings 	efx_nic_enable_interrupts(efx);
12549f2cb71cSBen Hutchings 
12559f2cb71cSBen Hutchings 	efx_for_each_channel(channel, efx) {
12569f2cb71cSBen Hutchings 		efx_init_eventq(channel);
12579f2cb71cSBen Hutchings 		efx_start_eventq(channel);
12589f2cb71cSBen Hutchings 	}
12599f2cb71cSBen Hutchings 
12609f2cb71cSBen Hutchings 	efx_mcdi_mode_event(efx);
12619f2cb71cSBen Hutchings }
12629f2cb71cSBen Hutchings 
12639f2cb71cSBen Hutchings static void efx_stop_interrupts(struct efx_nic *efx)
12649f2cb71cSBen Hutchings {
12659f2cb71cSBen Hutchings 	struct efx_channel *channel;
12669f2cb71cSBen Hutchings 
12679f2cb71cSBen Hutchings 	efx_mcdi_mode_poll(efx);
12689f2cb71cSBen Hutchings 
12699f2cb71cSBen Hutchings 	efx_nic_disable_interrupts(efx);
12709f2cb71cSBen Hutchings 	if (efx->legacy_irq) {
12719f2cb71cSBen Hutchings 		synchronize_irq(efx->legacy_irq);
12729f2cb71cSBen Hutchings 		efx->legacy_irq_enabled = false;
12739f2cb71cSBen Hutchings 	}
12749f2cb71cSBen Hutchings 
12759f2cb71cSBen Hutchings 	efx_for_each_channel(channel, efx) {
12769f2cb71cSBen Hutchings 		if (channel->irq)
12779f2cb71cSBen Hutchings 			synchronize_irq(channel->irq);
12789f2cb71cSBen Hutchings 
12799f2cb71cSBen Hutchings 		efx_stop_eventq(channel);
12809f2cb71cSBen Hutchings 		efx_fini_eventq(channel);
12819f2cb71cSBen Hutchings 	}
12829f2cb71cSBen Hutchings }
12839f2cb71cSBen Hutchings 
1284874aeea5SJeff Kirsher static void efx_remove_interrupts(struct efx_nic *efx)
1285874aeea5SJeff Kirsher {
1286874aeea5SJeff Kirsher 	struct efx_channel *channel;
1287874aeea5SJeff Kirsher 
1288874aeea5SJeff Kirsher 	/* Remove MSI/MSI-X interrupts */
1289874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx)
1290874aeea5SJeff Kirsher 		channel->irq = 0;
1291874aeea5SJeff Kirsher 	pci_disable_msi(efx->pci_dev);
1292874aeea5SJeff Kirsher 	pci_disable_msix(efx->pci_dev);
1293874aeea5SJeff Kirsher 
1294874aeea5SJeff Kirsher 	/* Remove legacy interrupt */
1295874aeea5SJeff Kirsher 	efx->legacy_irq = 0;
1296874aeea5SJeff Kirsher }
1297874aeea5SJeff Kirsher 
1298874aeea5SJeff Kirsher static void efx_set_channels(struct efx_nic *efx)
1299874aeea5SJeff Kirsher {
1300874aeea5SJeff Kirsher 	struct efx_channel *channel;
1301874aeea5SJeff Kirsher 	struct efx_tx_queue *tx_queue;
1302874aeea5SJeff Kirsher 
1303874aeea5SJeff Kirsher 	efx->tx_channel_offset =
1304874aeea5SJeff Kirsher 		separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0;
1305874aeea5SJeff Kirsher 
1306874aeea5SJeff Kirsher 	/* We need to adjust the TX queue numbers if we have separate
1307874aeea5SJeff Kirsher 	 * RX-only and TX-only channels.
1308874aeea5SJeff Kirsher 	 */
1309874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx) {
1310874aeea5SJeff Kirsher 		efx_for_each_channel_tx_queue(tx_queue, channel)
1311874aeea5SJeff Kirsher 			tx_queue->queue -= (efx->tx_channel_offset *
1312874aeea5SJeff Kirsher 					    EFX_TXQ_TYPES);
1313874aeea5SJeff Kirsher 	}
1314874aeea5SJeff Kirsher }
1315874aeea5SJeff Kirsher 
1316874aeea5SJeff Kirsher static int efx_probe_nic(struct efx_nic *efx)
1317874aeea5SJeff Kirsher {
1318874aeea5SJeff Kirsher 	size_t i;
1319874aeea5SJeff Kirsher 	int rc;
1320874aeea5SJeff Kirsher 
1321874aeea5SJeff Kirsher 	netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
1322874aeea5SJeff Kirsher 
1323874aeea5SJeff Kirsher 	/* Carry out hardware-type specific initialisation */
1324874aeea5SJeff Kirsher 	rc = efx->type->probe(efx);
1325874aeea5SJeff Kirsher 	if (rc)
1326874aeea5SJeff Kirsher 		return rc;
1327874aeea5SJeff Kirsher 
1328874aeea5SJeff Kirsher 	/* Determine the number of channels and queues by trying to hook
1329874aeea5SJeff Kirsher 	 * in MSI-X interrupts. */
1330874aeea5SJeff Kirsher 	rc = efx_probe_interrupts(efx);
1331874aeea5SJeff Kirsher 	if (rc)
1332874aeea5SJeff Kirsher 		goto fail;
1333874aeea5SJeff Kirsher 
1334874aeea5SJeff Kirsher 	if (efx->n_channels > 1)
1335874aeea5SJeff Kirsher 		get_random_bytes(&efx->rx_hash_key, sizeof(efx->rx_hash_key));
1336874aeea5SJeff Kirsher 	for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
1337278bc429SBen Hutchings 		efx->rx_indir_table[i] =
1338278bc429SBen Hutchings 			ethtool_rxfh_indir_default(i, efx->n_rx_channels);
1339874aeea5SJeff Kirsher 
1340874aeea5SJeff Kirsher 	efx_set_channels(efx);
1341874aeea5SJeff Kirsher 	netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1342874aeea5SJeff Kirsher 	netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
1343874aeea5SJeff Kirsher 
1344874aeea5SJeff Kirsher 	/* Initialise the interrupt moderation settings */
13459e393b30SBen Hutchings 	efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
13469e393b30SBen Hutchings 				true);
1347874aeea5SJeff Kirsher 
1348874aeea5SJeff Kirsher 	return 0;
1349874aeea5SJeff Kirsher 
1350874aeea5SJeff Kirsher fail:
1351874aeea5SJeff Kirsher 	efx->type->remove(efx);
1352874aeea5SJeff Kirsher 	return rc;
1353874aeea5SJeff Kirsher }
1354874aeea5SJeff Kirsher 
1355874aeea5SJeff Kirsher static void efx_remove_nic(struct efx_nic *efx)
1356874aeea5SJeff Kirsher {
1357874aeea5SJeff Kirsher 	netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
1358874aeea5SJeff Kirsher 
1359874aeea5SJeff Kirsher 	efx_remove_interrupts(efx);
1360874aeea5SJeff Kirsher 	efx->type->remove(efx);
1361874aeea5SJeff Kirsher }
1362874aeea5SJeff Kirsher 
1363874aeea5SJeff Kirsher /**************************************************************************
1364874aeea5SJeff Kirsher  *
1365874aeea5SJeff Kirsher  * NIC startup/shutdown
1366874aeea5SJeff Kirsher  *
1367874aeea5SJeff Kirsher  *************************************************************************/
1368874aeea5SJeff Kirsher 
1369874aeea5SJeff Kirsher static int efx_probe_all(struct efx_nic *efx)
1370874aeea5SJeff Kirsher {
1371874aeea5SJeff Kirsher 	int rc;
1372874aeea5SJeff Kirsher 
1373874aeea5SJeff Kirsher 	rc = efx_probe_nic(efx);
1374874aeea5SJeff Kirsher 	if (rc) {
1375874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
1376874aeea5SJeff Kirsher 		goto fail1;
1377874aeea5SJeff Kirsher 	}
1378874aeea5SJeff Kirsher 
1379874aeea5SJeff Kirsher 	rc = efx_probe_port(efx);
1380874aeea5SJeff Kirsher 	if (rc) {
1381874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev, "failed to create port\n");
1382874aeea5SJeff Kirsher 		goto fail2;
1383874aeea5SJeff Kirsher 	}
1384874aeea5SJeff Kirsher 
1385874aeea5SJeff Kirsher 	efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
1386874aeea5SJeff Kirsher 	rc = efx_probe_channels(efx);
1387874aeea5SJeff Kirsher 	if (rc)
1388874aeea5SJeff Kirsher 		goto fail3;
1389874aeea5SJeff Kirsher 
1390874aeea5SJeff Kirsher 	rc = efx_probe_filters(efx);
1391874aeea5SJeff Kirsher 	if (rc) {
1392874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev,
1393874aeea5SJeff Kirsher 			  "failed to create filter tables\n");
1394874aeea5SJeff Kirsher 		goto fail4;
1395874aeea5SJeff Kirsher 	}
1396874aeea5SJeff Kirsher 
1397874aeea5SJeff Kirsher 	return 0;
1398874aeea5SJeff Kirsher 
1399874aeea5SJeff Kirsher  fail4:
1400874aeea5SJeff Kirsher 	efx_remove_channels(efx);
1401874aeea5SJeff Kirsher  fail3:
1402874aeea5SJeff Kirsher 	efx_remove_port(efx);
1403874aeea5SJeff Kirsher  fail2:
1404874aeea5SJeff Kirsher 	efx_remove_nic(efx);
1405874aeea5SJeff Kirsher  fail1:
1406874aeea5SJeff Kirsher 	return rc;
1407874aeea5SJeff Kirsher }
1408874aeea5SJeff Kirsher 
14099f2cb71cSBen Hutchings /* Called after previous invocation(s) of efx_stop_all, restarts the port,
14109f2cb71cSBen Hutchings  * kernel transmit queues and NAPI processing, and ensures that the port is
14119f2cb71cSBen Hutchings  * scheduled to be reconfigured. This function is safe to call multiple
14129f2cb71cSBen Hutchings  * times when the NIC is in any state.
14139f2cb71cSBen Hutchings  */
1414874aeea5SJeff Kirsher static void efx_start_all(struct efx_nic *efx)
1415874aeea5SJeff Kirsher {
1416874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
1417874aeea5SJeff Kirsher 
1418874aeea5SJeff Kirsher 	/* Check that it is appropriate to restart the interface. All
1419874aeea5SJeff Kirsher 	 * of these flags are safe to read under just the rtnl lock */
1420874aeea5SJeff Kirsher 	if (efx->port_enabled)
1421874aeea5SJeff Kirsher 		return;
1422874aeea5SJeff Kirsher 	if ((efx->state != STATE_RUNNING) && (efx->state != STATE_INIT))
1423874aeea5SJeff Kirsher 		return;
142473ba7b68SBen Hutchings 	if (!netif_running(efx->net_dev))
1425874aeea5SJeff Kirsher 		return;
1426874aeea5SJeff Kirsher 
1427874aeea5SJeff Kirsher 	efx_start_port(efx);
14289f2cb71cSBen Hutchings 	efx_start_datapath(efx);
1429874aeea5SJeff Kirsher 
1430874aeea5SJeff Kirsher 	/* Start the hardware monitor if there is one. Otherwise (we're link
1431874aeea5SJeff Kirsher 	 * event driven), we have to poll the PHY because after an event queue
1432874aeea5SJeff Kirsher 	 * flush, we could have a missed a link state change */
1433874aeea5SJeff Kirsher 	if (efx->type->monitor != NULL) {
1434874aeea5SJeff Kirsher 		queue_delayed_work(efx->workqueue, &efx->monitor_work,
1435874aeea5SJeff Kirsher 				   efx_monitor_interval);
1436874aeea5SJeff Kirsher 	} else {
1437874aeea5SJeff Kirsher 		mutex_lock(&efx->mac_lock);
1438874aeea5SJeff Kirsher 		if (efx->phy_op->poll(efx))
1439874aeea5SJeff Kirsher 			efx_link_status_changed(efx);
1440874aeea5SJeff Kirsher 		mutex_unlock(&efx->mac_lock);
1441874aeea5SJeff Kirsher 	}
1442874aeea5SJeff Kirsher 
1443874aeea5SJeff Kirsher 	efx->type->start_stats(efx);
1444874aeea5SJeff Kirsher }
1445874aeea5SJeff Kirsher 
1446874aeea5SJeff Kirsher /* Flush all delayed work. Should only be called when no more delayed work
1447874aeea5SJeff Kirsher  * will be scheduled. This doesn't flush pending online resets (efx_reset),
1448874aeea5SJeff Kirsher  * since we're holding the rtnl_lock at this point. */
1449874aeea5SJeff Kirsher static void efx_flush_all(struct efx_nic *efx)
1450874aeea5SJeff Kirsher {
1451874aeea5SJeff Kirsher 	/* Make sure the hardware monitor is stopped */
1452874aeea5SJeff Kirsher 	cancel_delayed_work_sync(&efx->monitor_work);
1453874aeea5SJeff Kirsher 	/* Stop scheduled port reconfigurations */
1454874aeea5SJeff Kirsher 	cancel_work_sync(&efx->mac_work);
1455874aeea5SJeff Kirsher }
1456874aeea5SJeff Kirsher 
1457874aeea5SJeff Kirsher /* Quiesce hardware and software without bringing the link down.
1458874aeea5SJeff Kirsher  * Safe to call multiple times, when the nic and interface is in any
1459874aeea5SJeff Kirsher  * state. The caller is guaranteed to subsequently be in a position
1460874aeea5SJeff Kirsher  * to modify any hardware and software state they see fit without
1461874aeea5SJeff Kirsher  * taking locks. */
1462874aeea5SJeff Kirsher static void efx_stop_all(struct efx_nic *efx)
1463874aeea5SJeff Kirsher {
1464874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
1465874aeea5SJeff Kirsher 
1466874aeea5SJeff Kirsher 	/* port_enabled can be read safely under the rtnl lock */
1467874aeea5SJeff Kirsher 	if (!efx->port_enabled)
1468874aeea5SJeff Kirsher 		return;
1469874aeea5SJeff Kirsher 
1470874aeea5SJeff Kirsher 	efx->type->stop_stats(efx);
1471874aeea5SJeff Kirsher 	efx_stop_port(efx);
1472874aeea5SJeff Kirsher 
1473874aeea5SJeff Kirsher 	/* Flush efx_mac_work(), refill_workqueue, monitor_work */
1474874aeea5SJeff Kirsher 	efx_flush_all(efx);
1475874aeea5SJeff Kirsher 
1476874aeea5SJeff Kirsher 	/* Stop the kernel transmit interface late, so the watchdog
1477874aeea5SJeff Kirsher 	 * timer isn't ticking over the flush */
14789f2cb71cSBen Hutchings 	netif_tx_disable(efx->net_dev);
14799f2cb71cSBen Hutchings 
14809f2cb71cSBen Hutchings 	efx_stop_datapath(efx);
1481874aeea5SJeff Kirsher }
1482874aeea5SJeff Kirsher 
1483874aeea5SJeff Kirsher static void efx_remove_all(struct efx_nic *efx)
1484874aeea5SJeff Kirsher {
1485874aeea5SJeff Kirsher 	efx_remove_filters(efx);
1486874aeea5SJeff Kirsher 	efx_remove_channels(efx);
1487874aeea5SJeff Kirsher 	efx_remove_port(efx);
1488874aeea5SJeff Kirsher 	efx_remove_nic(efx);
1489874aeea5SJeff Kirsher }
1490874aeea5SJeff Kirsher 
1491874aeea5SJeff Kirsher /**************************************************************************
1492874aeea5SJeff Kirsher  *
1493874aeea5SJeff Kirsher  * Interrupt moderation
1494874aeea5SJeff Kirsher  *
1495874aeea5SJeff Kirsher  **************************************************************************/
1496874aeea5SJeff Kirsher 
1497cc180b69SBen Hutchings static unsigned int irq_mod_ticks(unsigned int usecs, unsigned int quantum_ns)
1498874aeea5SJeff Kirsher {
1499b548f976SBen Hutchings 	if (usecs == 0)
1500b548f976SBen Hutchings 		return 0;
1501cc180b69SBen Hutchings 	if (usecs * 1000 < quantum_ns)
1502874aeea5SJeff Kirsher 		return 1; /* never round down to 0 */
1503cc180b69SBen Hutchings 	return usecs * 1000 / quantum_ns;
1504874aeea5SJeff Kirsher }
1505874aeea5SJeff Kirsher 
1506874aeea5SJeff Kirsher /* Set interrupt moderation parameters */
15079e393b30SBen Hutchings int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
15089e393b30SBen Hutchings 			    unsigned int rx_usecs, bool rx_adaptive,
15099e393b30SBen Hutchings 			    bool rx_may_override_tx)
1510874aeea5SJeff Kirsher {
1511874aeea5SJeff Kirsher 	struct efx_channel *channel;
1512cc180b69SBen Hutchings 	unsigned int irq_mod_max = DIV_ROUND_UP(efx->type->timer_period_max *
1513cc180b69SBen Hutchings 						efx->timer_quantum_ns,
1514cc180b69SBen Hutchings 						1000);
1515cc180b69SBen Hutchings 	unsigned int tx_ticks;
1516cc180b69SBen Hutchings 	unsigned int rx_ticks;
1517874aeea5SJeff Kirsher 
1518874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
1519874aeea5SJeff Kirsher 
1520cc180b69SBen Hutchings 	if (tx_usecs > irq_mod_max || rx_usecs > irq_mod_max)
15219e393b30SBen Hutchings 		return -EINVAL;
15229e393b30SBen Hutchings 
1523cc180b69SBen Hutchings 	tx_ticks = irq_mod_ticks(tx_usecs, efx->timer_quantum_ns);
1524cc180b69SBen Hutchings 	rx_ticks = irq_mod_ticks(rx_usecs, efx->timer_quantum_ns);
1525cc180b69SBen Hutchings 
15269e393b30SBen Hutchings 	if (tx_ticks != rx_ticks && efx->tx_channel_offset == 0 &&
15279e393b30SBen Hutchings 	    !rx_may_override_tx) {
15289e393b30SBen Hutchings 		netif_err(efx, drv, efx->net_dev, "Channels are shared. "
15299e393b30SBen Hutchings 			  "RX and TX IRQ moderation must be equal\n");
15309e393b30SBen Hutchings 		return -EINVAL;
15319e393b30SBen Hutchings 	}
15329e393b30SBen Hutchings 
1533874aeea5SJeff Kirsher 	efx->irq_rx_adaptive = rx_adaptive;
1534874aeea5SJeff Kirsher 	efx->irq_rx_moderation = rx_ticks;
1535874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx) {
1536874aeea5SJeff Kirsher 		if (efx_channel_has_rx_queue(channel))
1537874aeea5SJeff Kirsher 			channel->irq_moderation = rx_ticks;
1538874aeea5SJeff Kirsher 		else if (efx_channel_has_tx_queues(channel))
1539874aeea5SJeff Kirsher 			channel->irq_moderation = tx_ticks;
1540874aeea5SJeff Kirsher 	}
15419e393b30SBen Hutchings 
15429e393b30SBen Hutchings 	return 0;
1543874aeea5SJeff Kirsher }
1544874aeea5SJeff Kirsher 
1545a0c4faf5SBen Hutchings void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
1546a0c4faf5SBen Hutchings 			    unsigned int *rx_usecs, bool *rx_adaptive)
1547a0c4faf5SBen Hutchings {
1548cc180b69SBen Hutchings 	/* We must round up when converting ticks to microseconds
1549cc180b69SBen Hutchings 	 * because we round down when converting the other way.
1550cc180b69SBen Hutchings 	 */
1551cc180b69SBen Hutchings 
1552a0c4faf5SBen Hutchings 	*rx_adaptive = efx->irq_rx_adaptive;
1553cc180b69SBen Hutchings 	*rx_usecs = DIV_ROUND_UP(efx->irq_rx_moderation *
1554cc180b69SBen Hutchings 				 efx->timer_quantum_ns,
1555cc180b69SBen Hutchings 				 1000);
1556a0c4faf5SBen Hutchings 
1557a0c4faf5SBen Hutchings 	/* If channels are shared between RX and TX, so is IRQ
1558a0c4faf5SBen Hutchings 	 * moderation.  Otherwise, IRQ moderation is the same for all
1559a0c4faf5SBen Hutchings 	 * TX channels and is not adaptive.
1560a0c4faf5SBen Hutchings 	 */
1561a0c4faf5SBen Hutchings 	if (efx->tx_channel_offset == 0)
1562a0c4faf5SBen Hutchings 		*tx_usecs = *rx_usecs;
1563a0c4faf5SBen Hutchings 	else
1564cc180b69SBen Hutchings 		*tx_usecs = DIV_ROUND_UP(
1565a0c4faf5SBen Hutchings 			efx->channel[efx->tx_channel_offset]->irq_moderation *
1566cc180b69SBen Hutchings 			efx->timer_quantum_ns,
1567cc180b69SBen Hutchings 			1000);
1568a0c4faf5SBen Hutchings }
1569a0c4faf5SBen Hutchings 
1570874aeea5SJeff Kirsher /**************************************************************************
1571874aeea5SJeff Kirsher  *
1572874aeea5SJeff Kirsher  * Hardware monitor
1573874aeea5SJeff Kirsher  *
1574874aeea5SJeff Kirsher  **************************************************************************/
1575874aeea5SJeff Kirsher 
1576874aeea5SJeff Kirsher /* Run periodically off the general workqueue */
1577874aeea5SJeff Kirsher static void efx_monitor(struct work_struct *data)
1578874aeea5SJeff Kirsher {
1579874aeea5SJeff Kirsher 	struct efx_nic *efx = container_of(data, struct efx_nic,
1580874aeea5SJeff Kirsher 					   monitor_work.work);
1581874aeea5SJeff Kirsher 
1582874aeea5SJeff Kirsher 	netif_vdbg(efx, timer, efx->net_dev,
1583874aeea5SJeff Kirsher 		   "hardware monitor executing on CPU %d\n",
1584874aeea5SJeff Kirsher 		   raw_smp_processor_id());
1585874aeea5SJeff Kirsher 	BUG_ON(efx->type->monitor == NULL);
1586874aeea5SJeff Kirsher 
1587874aeea5SJeff Kirsher 	/* If the mac_lock is already held then it is likely a port
1588874aeea5SJeff Kirsher 	 * reconfiguration is already in place, which will likely do
1589874aeea5SJeff Kirsher 	 * most of the work of monitor() anyway. */
1590874aeea5SJeff Kirsher 	if (mutex_trylock(&efx->mac_lock)) {
1591874aeea5SJeff Kirsher 		if (efx->port_enabled)
1592874aeea5SJeff Kirsher 			efx->type->monitor(efx);
1593874aeea5SJeff Kirsher 		mutex_unlock(&efx->mac_lock);
1594874aeea5SJeff Kirsher 	}
1595874aeea5SJeff Kirsher 
1596874aeea5SJeff Kirsher 	queue_delayed_work(efx->workqueue, &efx->monitor_work,
1597874aeea5SJeff Kirsher 			   efx_monitor_interval);
1598874aeea5SJeff Kirsher }
1599874aeea5SJeff Kirsher 
1600874aeea5SJeff Kirsher /**************************************************************************
1601874aeea5SJeff Kirsher  *
1602874aeea5SJeff Kirsher  * ioctls
1603874aeea5SJeff Kirsher  *
1604874aeea5SJeff Kirsher  *************************************************************************/
1605874aeea5SJeff Kirsher 
1606874aeea5SJeff Kirsher /* Net device ioctl
1607874aeea5SJeff Kirsher  * Context: process, rtnl_lock() held.
1608874aeea5SJeff Kirsher  */
1609874aeea5SJeff Kirsher static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1610874aeea5SJeff Kirsher {
1611874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
1612874aeea5SJeff Kirsher 	struct mii_ioctl_data *data = if_mii(ifr);
1613874aeea5SJeff Kirsher 
1614874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
1615874aeea5SJeff Kirsher 
1616874aeea5SJeff Kirsher 	/* Convert phy_id from older PRTAD/DEVAD format */
1617874aeea5SJeff Kirsher 	if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
1618874aeea5SJeff Kirsher 	    (data->phy_id & 0xfc00) == 0x0400)
1619874aeea5SJeff Kirsher 		data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
1620874aeea5SJeff Kirsher 
1621874aeea5SJeff Kirsher 	return mdio_mii_ioctl(&efx->mdio, data, cmd);
1622874aeea5SJeff Kirsher }
1623874aeea5SJeff Kirsher 
1624874aeea5SJeff Kirsher /**************************************************************************
1625874aeea5SJeff Kirsher  *
1626874aeea5SJeff Kirsher  * NAPI interface
1627874aeea5SJeff Kirsher  *
1628874aeea5SJeff Kirsher  **************************************************************************/
1629874aeea5SJeff Kirsher 
1630874aeea5SJeff Kirsher static void efx_init_napi(struct efx_nic *efx)
1631874aeea5SJeff Kirsher {
1632874aeea5SJeff Kirsher 	struct efx_channel *channel;
1633874aeea5SJeff Kirsher 
1634874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx) {
1635874aeea5SJeff Kirsher 		channel->napi_dev = efx->net_dev;
1636874aeea5SJeff Kirsher 		netif_napi_add(channel->napi_dev, &channel->napi_str,
1637874aeea5SJeff Kirsher 			       efx_poll, napi_weight);
1638874aeea5SJeff Kirsher 	}
1639874aeea5SJeff Kirsher }
1640874aeea5SJeff Kirsher 
1641874aeea5SJeff Kirsher static void efx_fini_napi_channel(struct efx_channel *channel)
1642874aeea5SJeff Kirsher {
1643874aeea5SJeff Kirsher 	if (channel->napi_dev)
1644874aeea5SJeff Kirsher 		netif_napi_del(&channel->napi_str);
1645874aeea5SJeff Kirsher 	channel->napi_dev = NULL;
1646874aeea5SJeff Kirsher }
1647874aeea5SJeff Kirsher 
1648874aeea5SJeff Kirsher static void efx_fini_napi(struct efx_nic *efx)
1649874aeea5SJeff Kirsher {
1650874aeea5SJeff Kirsher 	struct efx_channel *channel;
1651874aeea5SJeff Kirsher 
1652874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx)
1653874aeea5SJeff Kirsher 		efx_fini_napi_channel(channel);
1654874aeea5SJeff Kirsher }
1655874aeea5SJeff Kirsher 
1656874aeea5SJeff Kirsher /**************************************************************************
1657874aeea5SJeff Kirsher  *
1658874aeea5SJeff Kirsher  * Kernel netpoll interface
1659874aeea5SJeff Kirsher  *
1660874aeea5SJeff Kirsher  *************************************************************************/
1661874aeea5SJeff Kirsher 
1662874aeea5SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
1663874aeea5SJeff Kirsher 
1664874aeea5SJeff Kirsher /* Although in the common case interrupts will be disabled, this is not
1665874aeea5SJeff Kirsher  * guaranteed. However, all our work happens inside the NAPI callback,
1666874aeea5SJeff Kirsher  * so no locking is required.
1667874aeea5SJeff Kirsher  */
1668874aeea5SJeff Kirsher static void efx_netpoll(struct net_device *net_dev)
1669874aeea5SJeff Kirsher {
1670874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
1671874aeea5SJeff Kirsher 	struct efx_channel *channel;
1672874aeea5SJeff Kirsher 
1673874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx)
1674874aeea5SJeff Kirsher 		efx_schedule_channel(channel);
1675874aeea5SJeff Kirsher }
1676874aeea5SJeff Kirsher 
1677874aeea5SJeff Kirsher #endif
1678874aeea5SJeff Kirsher 
1679874aeea5SJeff Kirsher /**************************************************************************
1680874aeea5SJeff Kirsher  *
1681874aeea5SJeff Kirsher  * Kernel net device interface
1682874aeea5SJeff Kirsher  *
1683874aeea5SJeff Kirsher  *************************************************************************/
1684874aeea5SJeff Kirsher 
1685874aeea5SJeff Kirsher /* Context: process, rtnl_lock() held. */
1686874aeea5SJeff Kirsher static int efx_net_open(struct net_device *net_dev)
1687874aeea5SJeff Kirsher {
1688874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
1689874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
1690874aeea5SJeff Kirsher 
1691874aeea5SJeff Kirsher 	netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
1692874aeea5SJeff Kirsher 		  raw_smp_processor_id());
1693874aeea5SJeff Kirsher 
1694874aeea5SJeff Kirsher 	if (efx->state == STATE_DISABLED)
1695874aeea5SJeff Kirsher 		return -EIO;
1696874aeea5SJeff Kirsher 	if (efx->phy_mode & PHY_MODE_SPECIAL)
1697874aeea5SJeff Kirsher 		return -EBUSY;
1698874aeea5SJeff Kirsher 	if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
1699874aeea5SJeff Kirsher 		return -EIO;
1700874aeea5SJeff Kirsher 
1701874aeea5SJeff Kirsher 	/* Notify the kernel of the link state polled during driver load,
1702874aeea5SJeff Kirsher 	 * before the monitor starts running */
1703874aeea5SJeff Kirsher 	efx_link_status_changed(efx);
1704874aeea5SJeff Kirsher 
1705874aeea5SJeff Kirsher 	efx_start_all(efx);
1706874aeea5SJeff Kirsher 	return 0;
1707874aeea5SJeff Kirsher }
1708874aeea5SJeff Kirsher 
1709874aeea5SJeff Kirsher /* Context: process, rtnl_lock() held.
1710874aeea5SJeff Kirsher  * Note that the kernel will ignore our return code; this method
1711874aeea5SJeff Kirsher  * should really be a void.
1712874aeea5SJeff Kirsher  */
1713874aeea5SJeff Kirsher static int efx_net_stop(struct net_device *net_dev)
1714874aeea5SJeff Kirsher {
1715874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
1716874aeea5SJeff Kirsher 
1717874aeea5SJeff Kirsher 	netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
1718874aeea5SJeff Kirsher 		  raw_smp_processor_id());
1719874aeea5SJeff Kirsher 
1720874aeea5SJeff Kirsher 	if (efx->state != STATE_DISABLED) {
1721874aeea5SJeff Kirsher 		/* Stop the device and flush all the channels */
1722874aeea5SJeff Kirsher 		efx_stop_all(efx);
1723874aeea5SJeff Kirsher 	}
1724874aeea5SJeff Kirsher 
1725874aeea5SJeff Kirsher 	return 0;
1726874aeea5SJeff Kirsher }
1727874aeea5SJeff Kirsher 
1728874aeea5SJeff Kirsher /* Context: process, dev_base_lock or RTNL held, non-blocking. */
17292aa9ef11SBen Hutchings static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev,
17302aa9ef11SBen Hutchings 					       struct rtnl_link_stats64 *stats)
1731874aeea5SJeff Kirsher {
1732874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
1733874aeea5SJeff Kirsher 	struct efx_mac_stats *mac_stats = &efx->mac_stats;
1734874aeea5SJeff Kirsher 
1735874aeea5SJeff Kirsher 	spin_lock_bh(&efx->stats_lock);
17361cb34522SBen Hutchings 
1737874aeea5SJeff Kirsher 	efx->type->update_stats(efx);
1738874aeea5SJeff Kirsher 
1739874aeea5SJeff Kirsher 	stats->rx_packets = mac_stats->rx_packets;
1740874aeea5SJeff Kirsher 	stats->tx_packets = mac_stats->tx_packets;
1741874aeea5SJeff Kirsher 	stats->rx_bytes = mac_stats->rx_bytes;
1742874aeea5SJeff Kirsher 	stats->tx_bytes = mac_stats->tx_bytes;
1743874aeea5SJeff Kirsher 	stats->rx_dropped = efx->n_rx_nodesc_drop_cnt;
1744874aeea5SJeff Kirsher 	stats->multicast = mac_stats->rx_multicast;
1745874aeea5SJeff Kirsher 	stats->collisions = mac_stats->tx_collision;
1746874aeea5SJeff Kirsher 	stats->rx_length_errors = (mac_stats->rx_gtjumbo +
1747874aeea5SJeff Kirsher 				   mac_stats->rx_length_error);
1748874aeea5SJeff Kirsher 	stats->rx_crc_errors = mac_stats->rx_bad;
1749874aeea5SJeff Kirsher 	stats->rx_frame_errors = mac_stats->rx_align_error;
1750874aeea5SJeff Kirsher 	stats->rx_fifo_errors = mac_stats->rx_overflow;
1751874aeea5SJeff Kirsher 	stats->rx_missed_errors = mac_stats->rx_missed;
1752874aeea5SJeff Kirsher 	stats->tx_window_errors = mac_stats->tx_late_collision;
1753874aeea5SJeff Kirsher 
1754874aeea5SJeff Kirsher 	stats->rx_errors = (stats->rx_length_errors +
1755874aeea5SJeff Kirsher 			    stats->rx_crc_errors +
1756874aeea5SJeff Kirsher 			    stats->rx_frame_errors +
1757874aeea5SJeff Kirsher 			    mac_stats->rx_symbol_error);
1758874aeea5SJeff Kirsher 	stats->tx_errors = (stats->tx_window_errors +
1759874aeea5SJeff Kirsher 			    mac_stats->tx_bad);
1760874aeea5SJeff Kirsher 
17611cb34522SBen Hutchings 	spin_unlock_bh(&efx->stats_lock);
17621cb34522SBen Hutchings 
1763874aeea5SJeff Kirsher 	return stats;
1764874aeea5SJeff Kirsher }
1765874aeea5SJeff Kirsher 
1766874aeea5SJeff Kirsher /* Context: netif_tx_lock held, BHs disabled. */
1767874aeea5SJeff Kirsher static void efx_watchdog(struct net_device *net_dev)
1768874aeea5SJeff Kirsher {
1769874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
1770874aeea5SJeff Kirsher 
1771874aeea5SJeff Kirsher 	netif_err(efx, tx_err, efx->net_dev,
1772874aeea5SJeff Kirsher 		  "TX stuck with port_enabled=%d: resetting channels\n",
1773874aeea5SJeff Kirsher 		  efx->port_enabled);
1774874aeea5SJeff Kirsher 
1775874aeea5SJeff Kirsher 	efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
1776874aeea5SJeff Kirsher }
1777874aeea5SJeff Kirsher 
1778874aeea5SJeff Kirsher 
1779874aeea5SJeff Kirsher /* Context: process, rtnl_lock() held. */
1780874aeea5SJeff Kirsher static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
1781874aeea5SJeff Kirsher {
1782874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
1783874aeea5SJeff Kirsher 
1784874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
1785874aeea5SJeff Kirsher 
1786874aeea5SJeff Kirsher 	if (new_mtu > EFX_MAX_MTU)
1787874aeea5SJeff Kirsher 		return -EINVAL;
1788874aeea5SJeff Kirsher 
1789874aeea5SJeff Kirsher 	efx_stop_all(efx);
1790874aeea5SJeff Kirsher 
1791874aeea5SJeff Kirsher 	netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
1792874aeea5SJeff Kirsher 
1793874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
1794874aeea5SJeff Kirsher 	/* Reconfigure the MAC before enabling the dma queues so that
1795874aeea5SJeff Kirsher 	 * the RX buffers don't overflow */
1796874aeea5SJeff Kirsher 	net_dev->mtu = new_mtu;
1797710b208dSBen Hutchings 	efx->type->reconfigure_mac(efx);
1798874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
1799874aeea5SJeff Kirsher 
1800874aeea5SJeff Kirsher 	efx_start_all(efx);
18016c8eef4aSBen Hutchings 	return 0;
1802874aeea5SJeff Kirsher }
1803874aeea5SJeff Kirsher 
1804874aeea5SJeff Kirsher static int efx_set_mac_address(struct net_device *net_dev, void *data)
1805874aeea5SJeff Kirsher {
1806874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
1807874aeea5SJeff Kirsher 	struct sockaddr *addr = data;
1808874aeea5SJeff Kirsher 	char *new_addr = addr->sa_data;
1809874aeea5SJeff Kirsher 
1810874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
1811874aeea5SJeff Kirsher 
1812874aeea5SJeff Kirsher 	if (!is_valid_ether_addr(new_addr)) {
1813874aeea5SJeff Kirsher 		netif_err(efx, drv, efx->net_dev,
1814874aeea5SJeff Kirsher 			  "invalid ethernet MAC address requested: %pM\n",
1815874aeea5SJeff Kirsher 			  new_addr);
1816874aeea5SJeff Kirsher 		return -EINVAL;
1817874aeea5SJeff Kirsher 	}
1818874aeea5SJeff Kirsher 
1819874aeea5SJeff Kirsher 	memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
1820874aeea5SJeff Kirsher 
1821874aeea5SJeff Kirsher 	/* Reconfigure the MAC */
1822874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
1823710b208dSBen Hutchings 	efx->type->reconfigure_mac(efx);
1824874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
1825874aeea5SJeff Kirsher 
1826874aeea5SJeff Kirsher 	return 0;
1827874aeea5SJeff Kirsher }
1828874aeea5SJeff Kirsher 
1829874aeea5SJeff Kirsher /* Context: netif_addr_lock held, BHs disabled. */
18300fca8c97SBen Hutchings static void efx_set_rx_mode(struct net_device *net_dev)
1831874aeea5SJeff Kirsher {
1832874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
1833874aeea5SJeff Kirsher 	struct netdev_hw_addr *ha;
1834874aeea5SJeff Kirsher 	union efx_multicast_hash *mc_hash = &efx->multicast_hash;
1835874aeea5SJeff Kirsher 	u32 crc;
1836874aeea5SJeff Kirsher 	int bit;
1837874aeea5SJeff Kirsher 
1838874aeea5SJeff Kirsher 	efx->promiscuous = !!(net_dev->flags & IFF_PROMISC);
1839874aeea5SJeff Kirsher 
1840874aeea5SJeff Kirsher 	/* Build multicast hash table */
1841874aeea5SJeff Kirsher 	if (efx->promiscuous || (net_dev->flags & IFF_ALLMULTI)) {
1842874aeea5SJeff Kirsher 		memset(mc_hash, 0xff, sizeof(*mc_hash));
1843874aeea5SJeff Kirsher 	} else {
1844874aeea5SJeff Kirsher 		memset(mc_hash, 0x00, sizeof(*mc_hash));
1845874aeea5SJeff Kirsher 		netdev_for_each_mc_addr(ha, net_dev) {
1846874aeea5SJeff Kirsher 			crc = ether_crc_le(ETH_ALEN, ha->addr);
1847874aeea5SJeff Kirsher 			bit = crc & (EFX_MCAST_HASH_ENTRIES - 1);
1848874aeea5SJeff Kirsher 			set_bit_le(bit, mc_hash->byte);
1849874aeea5SJeff Kirsher 		}
1850874aeea5SJeff Kirsher 
1851874aeea5SJeff Kirsher 		/* Broadcast packets go through the multicast hash filter.
1852874aeea5SJeff Kirsher 		 * ether_crc_le() of the broadcast address is 0xbe2612ff
1853874aeea5SJeff Kirsher 		 * so we always add bit 0xff to the mask.
1854874aeea5SJeff Kirsher 		 */
1855874aeea5SJeff Kirsher 		set_bit_le(0xff, mc_hash->byte);
1856874aeea5SJeff Kirsher 	}
1857874aeea5SJeff Kirsher 
1858874aeea5SJeff Kirsher 	if (efx->port_enabled)
1859874aeea5SJeff Kirsher 		queue_work(efx->workqueue, &efx->mac_work);
1860874aeea5SJeff Kirsher 	/* Otherwise efx_start_port() will do this */
1861874aeea5SJeff Kirsher }
1862874aeea5SJeff Kirsher 
1863c8f44affSMichał Mirosław static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
1864874aeea5SJeff Kirsher {
1865874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
1866874aeea5SJeff Kirsher 
1867874aeea5SJeff Kirsher 	/* If disabling RX n-tuple filtering, clear existing filters */
1868874aeea5SJeff Kirsher 	if (net_dev->features & ~data & NETIF_F_NTUPLE)
1869874aeea5SJeff Kirsher 		efx_filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
1870874aeea5SJeff Kirsher 
1871874aeea5SJeff Kirsher 	return 0;
1872874aeea5SJeff Kirsher }
1873874aeea5SJeff Kirsher 
1874874aeea5SJeff Kirsher static const struct net_device_ops efx_netdev_ops = {
1875874aeea5SJeff Kirsher 	.ndo_open		= efx_net_open,
1876874aeea5SJeff Kirsher 	.ndo_stop		= efx_net_stop,
1877874aeea5SJeff Kirsher 	.ndo_get_stats64	= efx_net_stats,
1878874aeea5SJeff Kirsher 	.ndo_tx_timeout		= efx_watchdog,
1879874aeea5SJeff Kirsher 	.ndo_start_xmit		= efx_hard_start_xmit,
1880874aeea5SJeff Kirsher 	.ndo_validate_addr	= eth_validate_addr,
1881874aeea5SJeff Kirsher 	.ndo_do_ioctl		= efx_ioctl,
1882874aeea5SJeff Kirsher 	.ndo_change_mtu		= efx_change_mtu,
1883874aeea5SJeff Kirsher 	.ndo_set_mac_address	= efx_set_mac_address,
18840fca8c97SBen Hutchings 	.ndo_set_rx_mode	= efx_set_rx_mode,
1885874aeea5SJeff Kirsher 	.ndo_set_features	= efx_set_features,
1886874aeea5SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
1887874aeea5SJeff Kirsher 	.ndo_poll_controller = efx_netpoll,
1888874aeea5SJeff Kirsher #endif
1889874aeea5SJeff Kirsher 	.ndo_setup_tc		= efx_setup_tc,
1890874aeea5SJeff Kirsher #ifdef CONFIG_RFS_ACCEL
1891874aeea5SJeff Kirsher 	.ndo_rx_flow_steer	= efx_filter_rfs,
1892874aeea5SJeff Kirsher #endif
1893874aeea5SJeff Kirsher };
1894874aeea5SJeff Kirsher 
1895874aeea5SJeff Kirsher static void efx_update_name(struct efx_nic *efx)
1896874aeea5SJeff Kirsher {
1897874aeea5SJeff Kirsher 	strcpy(efx->name, efx->net_dev->name);
1898874aeea5SJeff Kirsher 	efx_mtd_rename(efx);
1899874aeea5SJeff Kirsher 	efx_set_channel_names(efx);
1900874aeea5SJeff Kirsher }
1901874aeea5SJeff Kirsher 
1902874aeea5SJeff Kirsher static int efx_netdev_event(struct notifier_block *this,
1903874aeea5SJeff Kirsher 			    unsigned long event, void *ptr)
1904874aeea5SJeff Kirsher {
1905874aeea5SJeff Kirsher 	struct net_device *net_dev = ptr;
1906874aeea5SJeff Kirsher 
1907874aeea5SJeff Kirsher 	if (net_dev->netdev_ops == &efx_netdev_ops &&
1908874aeea5SJeff Kirsher 	    event == NETDEV_CHANGENAME)
1909874aeea5SJeff Kirsher 		efx_update_name(netdev_priv(net_dev));
1910874aeea5SJeff Kirsher 
1911874aeea5SJeff Kirsher 	return NOTIFY_DONE;
1912874aeea5SJeff Kirsher }
1913874aeea5SJeff Kirsher 
1914874aeea5SJeff Kirsher static struct notifier_block efx_netdev_notifier = {
1915874aeea5SJeff Kirsher 	.notifier_call = efx_netdev_event,
1916874aeea5SJeff Kirsher };
1917874aeea5SJeff Kirsher 
1918874aeea5SJeff Kirsher static ssize_t
1919874aeea5SJeff Kirsher show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
1920874aeea5SJeff Kirsher {
1921874aeea5SJeff Kirsher 	struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
1922874aeea5SJeff Kirsher 	return sprintf(buf, "%d\n", efx->phy_type);
1923874aeea5SJeff Kirsher }
1924874aeea5SJeff Kirsher static DEVICE_ATTR(phy_type, 0644, show_phy_type, NULL);
1925874aeea5SJeff Kirsher 
1926874aeea5SJeff Kirsher static int efx_register_netdev(struct efx_nic *efx)
1927874aeea5SJeff Kirsher {
1928874aeea5SJeff Kirsher 	struct net_device *net_dev = efx->net_dev;
1929874aeea5SJeff Kirsher 	struct efx_channel *channel;
1930874aeea5SJeff Kirsher 	int rc;
1931874aeea5SJeff Kirsher 
1932874aeea5SJeff Kirsher 	net_dev->watchdog_timeo = 5 * HZ;
1933874aeea5SJeff Kirsher 	net_dev->irq = efx->pci_dev->irq;
1934874aeea5SJeff Kirsher 	net_dev->netdev_ops = &efx_netdev_ops;
1935874aeea5SJeff Kirsher 	SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
1936874aeea5SJeff Kirsher 
1937874aeea5SJeff Kirsher 	rtnl_lock();
1938874aeea5SJeff Kirsher 
1939874aeea5SJeff Kirsher 	rc = dev_alloc_name(net_dev, net_dev->name);
1940874aeea5SJeff Kirsher 	if (rc < 0)
1941874aeea5SJeff Kirsher 		goto fail_locked;
1942874aeea5SJeff Kirsher 	efx_update_name(efx);
1943874aeea5SJeff Kirsher 
1944874aeea5SJeff Kirsher 	rc = register_netdevice(net_dev);
1945874aeea5SJeff Kirsher 	if (rc)
1946874aeea5SJeff Kirsher 		goto fail_locked;
1947874aeea5SJeff Kirsher 
1948874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx) {
1949874aeea5SJeff Kirsher 		struct efx_tx_queue *tx_queue;
1950874aeea5SJeff Kirsher 		efx_for_each_channel_tx_queue(tx_queue, channel)
1951874aeea5SJeff Kirsher 			efx_init_tx_queue_core_txq(tx_queue);
1952874aeea5SJeff Kirsher 	}
1953874aeea5SJeff Kirsher 
1954874aeea5SJeff Kirsher 	/* Always start with carrier off; PHY events will detect the link */
195586ee5302SBen Hutchings 	netif_carrier_off(net_dev);
1956874aeea5SJeff Kirsher 
1957874aeea5SJeff Kirsher 	rtnl_unlock();
1958874aeea5SJeff Kirsher 
1959874aeea5SJeff Kirsher 	rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
1960874aeea5SJeff Kirsher 	if (rc) {
1961874aeea5SJeff Kirsher 		netif_err(efx, drv, efx->net_dev,
1962874aeea5SJeff Kirsher 			  "failed to init net dev attributes\n");
1963874aeea5SJeff Kirsher 		goto fail_registered;
1964874aeea5SJeff Kirsher 	}
1965874aeea5SJeff Kirsher 
1966874aeea5SJeff Kirsher 	return 0;
1967874aeea5SJeff Kirsher 
1968874aeea5SJeff Kirsher fail_locked:
1969874aeea5SJeff Kirsher 	rtnl_unlock();
1970874aeea5SJeff Kirsher 	netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
1971874aeea5SJeff Kirsher 	return rc;
1972874aeea5SJeff Kirsher 
1973874aeea5SJeff Kirsher fail_registered:
1974874aeea5SJeff Kirsher 	unregister_netdev(net_dev);
1975874aeea5SJeff Kirsher 	return rc;
1976874aeea5SJeff Kirsher }
1977874aeea5SJeff Kirsher 
1978874aeea5SJeff Kirsher static void efx_unregister_netdev(struct efx_nic *efx)
1979874aeea5SJeff Kirsher {
1980874aeea5SJeff Kirsher 	struct efx_channel *channel;
1981874aeea5SJeff Kirsher 	struct efx_tx_queue *tx_queue;
1982874aeea5SJeff Kirsher 
1983874aeea5SJeff Kirsher 	if (!efx->net_dev)
1984874aeea5SJeff Kirsher 		return;
1985874aeea5SJeff Kirsher 
1986874aeea5SJeff Kirsher 	BUG_ON(netdev_priv(efx->net_dev) != efx);
1987874aeea5SJeff Kirsher 
1988874aeea5SJeff Kirsher 	/* Free up any skbs still remaining. This has to happen before
1989874aeea5SJeff Kirsher 	 * we try to unregister the netdev as running their destructors
1990874aeea5SJeff Kirsher 	 * may be needed to get the device ref. count to 0. */
1991874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx) {
1992874aeea5SJeff Kirsher 		efx_for_each_channel_tx_queue(tx_queue, channel)
1993874aeea5SJeff Kirsher 			efx_release_tx_buffers(tx_queue);
1994874aeea5SJeff Kirsher 	}
1995874aeea5SJeff Kirsher 
1996874aeea5SJeff Kirsher 	strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
1997874aeea5SJeff Kirsher 	device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
1998874aeea5SJeff Kirsher 	unregister_netdev(efx->net_dev);
1999874aeea5SJeff Kirsher }
2000874aeea5SJeff Kirsher 
2001874aeea5SJeff Kirsher /**************************************************************************
2002874aeea5SJeff Kirsher  *
2003874aeea5SJeff Kirsher  * Device reset and suspend
2004874aeea5SJeff Kirsher  *
2005874aeea5SJeff Kirsher  **************************************************************************/
2006874aeea5SJeff Kirsher 
2007874aeea5SJeff Kirsher /* Tears down the entire software state and most of the hardware state
2008874aeea5SJeff Kirsher  * before reset.  */
2009874aeea5SJeff Kirsher void efx_reset_down(struct efx_nic *efx, enum reset_type method)
2010874aeea5SJeff Kirsher {
2011874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
2012874aeea5SJeff Kirsher 
2013874aeea5SJeff Kirsher 	efx_stop_all(efx);
2014874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
2015874aeea5SJeff Kirsher 
20169f2cb71cSBen Hutchings 	efx_stop_interrupts(efx);
2017874aeea5SJeff Kirsher 	if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
2018874aeea5SJeff Kirsher 		efx->phy_op->fini(efx);
2019874aeea5SJeff Kirsher 	efx->type->fini(efx);
2020874aeea5SJeff Kirsher }
2021874aeea5SJeff Kirsher 
2022874aeea5SJeff Kirsher /* This function will always ensure that the locks acquired in
2023874aeea5SJeff Kirsher  * efx_reset_down() are released. A failure return code indicates
2024874aeea5SJeff Kirsher  * that we were unable to reinitialise the hardware, and the
2025874aeea5SJeff Kirsher  * driver should be disabled. If ok is false, then the rx and tx
2026874aeea5SJeff Kirsher  * engines are not restarted, pending a RESET_DISABLE. */
2027874aeea5SJeff Kirsher int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
2028874aeea5SJeff Kirsher {
2029874aeea5SJeff Kirsher 	int rc;
2030874aeea5SJeff Kirsher 
2031874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
2032874aeea5SJeff Kirsher 
2033874aeea5SJeff Kirsher 	rc = efx->type->init(efx);
2034874aeea5SJeff Kirsher 	if (rc) {
2035874aeea5SJeff Kirsher 		netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
2036874aeea5SJeff Kirsher 		goto fail;
2037874aeea5SJeff Kirsher 	}
2038874aeea5SJeff Kirsher 
2039874aeea5SJeff Kirsher 	if (!ok)
2040874aeea5SJeff Kirsher 		goto fail;
2041874aeea5SJeff Kirsher 
2042874aeea5SJeff Kirsher 	if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
2043874aeea5SJeff Kirsher 		rc = efx->phy_op->init(efx);
2044874aeea5SJeff Kirsher 		if (rc)
2045874aeea5SJeff Kirsher 			goto fail;
2046874aeea5SJeff Kirsher 		if (efx->phy_op->reconfigure(efx))
2047874aeea5SJeff Kirsher 			netif_err(efx, drv, efx->net_dev,
2048874aeea5SJeff Kirsher 				  "could not restore PHY settings\n");
2049874aeea5SJeff Kirsher 	}
2050874aeea5SJeff Kirsher 
2051710b208dSBen Hutchings 	efx->type->reconfigure_mac(efx);
2052874aeea5SJeff Kirsher 
20539f2cb71cSBen Hutchings 	efx_start_interrupts(efx);
2054874aeea5SJeff Kirsher 	efx_restore_filters(efx);
2055874aeea5SJeff Kirsher 
2056874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
2057874aeea5SJeff Kirsher 
2058874aeea5SJeff Kirsher 	efx_start_all(efx);
2059874aeea5SJeff Kirsher 
2060874aeea5SJeff Kirsher 	return 0;
2061874aeea5SJeff Kirsher 
2062874aeea5SJeff Kirsher fail:
2063874aeea5SJeff Kirsher 	efx->port_initialized = false;
2064874aeea5SJeff Kirsher 
2065874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
2066874aeea5SJeff Kirsher 
2067874aeea5SJeff Kirsher 	return rc;
2068874aeea5SJeff Kirsher }
2069874aeea5SJeff Kirsher 
2070874aeea5SJeff Kirsher /* Reset the NIC using the specified method.  Note that the reset may
2071874aeea5SJeff Kirsher  * fail, in which case the card will be left in an unusable state.
2072874aeea5SJeff Kirsher  *
2073874aeea5SJeff Kirsher  * Caller must hold the rtnl_lock.
2074874aeea5SJeff Kirsher  */
2075874aeea5SJeff Kirsher int efx_reset(struct efx_nic *efx, enum reset_type method)
2076874aeea5SJeff Kirsher {
2077874aeea5SJeff Kirsher 	int rc, rc2;
2078874aeea5SJeff Kirsher 	bool disabled;
2079874aeea5SJeff Kirsher 
2080874aeea5SJeff Kirsher 	netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2081874aeea5SJeff Kirsher 		   RESET_TYPE(method));
2082874aeea5SJeff Kirsher 
2083874aeea5SJeff Kirsher 	netif_device_detach(efx->net_dev);
2084874aeea5SJeff Kirsher 	efx_reset_down(efx, method);
2085874aeea5SJeff Kirsher 
2086874aeea5SJeff Kirsher 	rc = efx->type->reset(efx, method);
2087874aeea5SJeff Kirsher 	if (rc) {
2088874aeea5SJeff Kirsher 		netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
2089874aeea5SJeff Kirsher 		goto out;
2090874aeea5SJeff Kirsher 	}
2091874aeea5SJeff Kirsher 
2092874aeea5SJeff Kirsher 	/* Clear flags for the scopes we covered.  We assume the NIC and
2093874aeea5SJeff Kirsher 	 * driver are now quiescent so that there is no race here.
2094874aeea5SJeff Kirsher 	 */
2095874aeea5SJeff Kirsher 	efx->reset_pending &= -(1 << (method + 1));
2096874aeea5SJeff Kirsher 
2097874aeea5SJeff Kirsher 	/* Reinitialise bus-mastering, which may have been turned off before
2098874aeea5SJeff Kirsher 	 * the reset was scheduled. This is still appropriate, even in the
2099874aeea5SJeff Kirsher 	 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2100874aeea5SJeff Kirsher 	 * can respond to requests. */
2101874aeea5SJeff Kirsher 	pci_set_master(efx->pci_dev);
2102874aeea5SJeff Kirsher 
2103874aeea5SJeff Kirsher out:
2104874aeea5SJeff Kirsher 	/* Leave device stopped if necessary */
2105874aeea5SJeff Kirsher 	disabled = rc || method == RESET_TYPE_DISABLE;
2106874aeea5SJeff Kirsher 	rc2 = efx_reset_up(efx, method, !disabled);
2107874aeea5SJeff Kirsher 	if (rc2) {
2108874aeea5SJeff Kirsher 		disabled = true;
2109874aeea5SJeff Kirsher 		if (!rc)
2110874aeea5SJeff Kirsher 			rc = rc2;
2111874aeea5SJeff Kirsher 	}
2112874aeea5SJeff Kirsher 
2113874aeea5SJeff Kirsher 	if (disabled) {
2114874aeea5SJeff Kirsher 		dev_close(efx->net_dev);
2115874aeea5SJeff Kirsher 		netif_err(efx, drv, efx->net_dev, "has been disabled\n");
2116874aeea5SJeff Kirsher 		efx->state = STATE_DISABLED;
2117874aeea5SJeff Kirsher 	} else {
2118874aeea5SJeff Kirsher 		netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
2119874aeea5SJeff Kirsher 		netif_device_attach(efx->net_dev);
2120874aeea5SJeff Kirsher 	}
2121874aeea5SJeff Kirsher 	return rc;
2122874aeea5SJeff Kirsher }
2123874aeea5SJeff Kirsher 
2124874aeea5SJeff Kirsher /* The worker thread exists so that code that cannot sleep can
2125874aeea5SJeff Kirsher  * schedule a reset for later.
2126874aeea5SJeff Kirsher  */
2127874aeea5SJeff Kirsher static void efx_reset_work(struct work_struct *data)
2128874aeea5SJeff Kirsher {
2129874aeea5SJeff Kirsher 	struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
2130874aeea5SJeff Kirsher 	unsigned long pending = ACCESS_ONCE(efx->reset_pending);
2131874aeea5SJeff Kirsher 
2132874aeea5SJeff Kirsher 	if (!pending)
2133874aeea5SJeff Kirsher 		return;
2134874aeea5SJeff Kirsher 
2135874aeea5SJeff Kirsher 	/* If we're not RUNNING then don't reset. Leave the reset_pending
2136874aeea5SJeff Kirsher 	 * flags set so that efx_pci_probe_main will be retried */
2137874aeea5SJeff Kirsher 	if (efx->state != STATE_RUNNING) {
2138874aeea5SJeff Kirsher 		netif_info(efx, drv, efx->net_dev,
2139874aeea5SJeff Kirsher 			   "scheduled reset quenched. NIC not RUNNING\n");
2140874aeea5SJeff Kirsher 		return;
2141874aeea5SJeff Kirsher 	}
2142874aeea5SJeff Kirsher 
2143874aeea5SJeff Kirsher 	rtnl_lock();
2144874aeea5SJeff Kirsher 	(void)efx_reset(efx, fls(pending) - 1);
2145874aeea5SJeff Kirsher 	rtnl_unlock();
2146874aeea5SJeff Kirsher }
2147874aeea5SJeff Kirsher 
2148874aeea5SJeff Kirsher void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2149874aeea5SJeff Kirsher {
2150874aeea5SJeff Kirsher 	enum reset_type method;
2151874aeea5SJeff Kirsher 
2152874aeea5SJeff Kirsher 	switch (type) {
2153874aeea5SJeff Kirsher 	case RESET_TYPE_INVISIBLE:
2154874aeea5SJeff Kirsher 	case RESET_TYPE_ALL:
2155874aeea5SJeff Kirsher 	case RESET_TYPE_WORLD:
2156874aeea5SJeff Kirsher 	case RESET_TYPE_DISABLE:
2157874aeea5SJeff Kirsher 		method = type;
2158874aeea5SJeff Kirsher 		netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2159874aeea5SJeff Kirsher 			  RESET_TYPE(method));
2160874aeea5SJeff Kirsher 		break;
2161874aeea5SJeff Kirsher 	default:
2162874aeea5SJeff Kirsher 		method = efx->type->map_reset_reason(type);
2163874aeea5SJeff Kirsher 		netif_dbg(efx, drv, efx->net_dev,
2164874aeea5SJeff Kirsher 			  "scheduling %s reset for %s\n",
2165874aeea5SJeff Kirsher 			  RESET_TYPE(method), RESET_TYPE(type));
2166874aeea5SJeff Kirsher 		break;
2167874aeea5SJeff Kirsher 	}
2168874aeea5SJeff Kirsher 
2169874aeea5SJeff Kirsher 	set_bit(method, &efx->reset_pending);
2170874aeea5SJeff Kirsher 
2171874aeea5SJeff Kirsher 	/* efx_process_channel() will no longer read events once a
2172874aeea5SJeff Kirsher 	 * reset is scheduled. So switch back to poll'd MCDI completions. */
2173874aeea5SJeff Kirsher 	efx_mcdi_mode_poll(efx);
2174874aeea5SJeff Kirsher 
2175874aeea5SJeff Kirsher 	queue_work(reset_workqueue, &efx->reset_work);
2176874aeea5SJeff Kirsher }
2177874aeea5SJeff Kirsher 
2178874aeea5SJeff Kirsher /**************************************************************************
2179874aeea5SJeff Kirsher  *
2180874aeea5SJeff Kirsher  * List of NICs we support
2181874aeea5SJeff Kirsher  *
2182874aeea5SJeff Kirsher  **************************************************************************/
2183874aeea5SJeff Kirsher 
2184874aeea5SJeff Kirsher /* PCI device ID table */
2185874aeea5SJeff Kirsher static DEFINE_PCI_DEVICE_TABLE(efx_pci_table) = {
21860e59e7e7SLinus Torvalds 	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
21870e59e7e7SLinus Torvalds 		    PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
2188874aeea5SJeff Kirsher 	 .driver_data = (unsigned long) &falcon_a1_nic_type},
21890e59e7e7SLinus Torvalds 	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
21900e59e7e7SLinus Torvalds 		    PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
2191874aeea5SJeff Kirsher 	 .driver_data = (unsigned long) &falcon_b0_nic_type},
2192547c474fSBen Hutchings 	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803),	/* SFC9020 */
2193874aeea5SJeff Kirsher 	 .driver_data = (unsigned long) &siena_a0_nic_type},
2194547c474fSBen Hutchings 	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813),	/* SFL9021 */
2195874aeea5SJeff Kirsher 	 .driver_data = (unsigned long) &siena_a0_nic_type},
2196874aeea5SJeff Kirsher 	{0}			/* end of list */
2197874aeea5SJeff Kirsher };
2198874aeea5SJeff Kirsher 
2199874aeea5SJeff Kirsher /**************************************************************************
2200874aeea5SJeff Kirsher  *
2201874aeea5SJeff Kirsher  * Dummy PHY/MAC operations
2202874aeea5SJeff Kirsher  *
2203874aeea5SJeff Kirsher  * Can be used for some unimplemented operations
2204874aeea5SJeff Kirsher  * Needed so all function pointers are valid and do not have to be tested
2205874aeea5SJeff Kirsher  * before use
2206874aeea5SJeff Kirsher  *
2207874aeea5SJeff Kirsher  **************************************************************************/
2208874aeea5SJeff Kirsher int efx_port_dummy_op_int(struct efx_nic *efx)
2209874aeea5SJeff Kirsher {
2210874aeea5SJeff Kirsher 	return 0;
2211874aeea5SJeff Kirsher }
2212874aeea5SJeff Kirsher void efx_port_dummy_op_void(struct efx_nic *efx) {}
2213874aeea5SJeff Kirsher 
2214874aeea5SJeff Kirsher static bool efx_port_dummy_op_poll(struct efx_nic *efx)
2215874aeea5SJeff Kirsher {
2216874aeea5SJeff Kirsher 	return false;
2217874aeea5SJeff Kirsher }
2218874aeea5SJeff Kirsher 
2219874aeea5SJeff Kirsher static const struct efx_phy_operations efx_dummy_phy_operations = {
2220874aeea5SJeff Kirsher 	.init		 = efx_port_dummy_op_int,
2221874aeea5SJeff Kirsher 	.reconfigure	 = efx_port_dummy_op_int,
2222874aeea5SJeff Kirsher 	.poll		 = efx_port_dummy_op_poll,
2223874aeea5SJeff Kirsher 	.fini		 = efx_port_dummy_op_void,
2224874aeea5SJeff Kirsher };
2225874aeea5SJeff Kirsher 
2226874aeea5SJeff Kirsher /**************************************************************************
2227874aeea5SJeff Kirsher  *
2228874aeea5SJeff Kirsher  * Data housekeeping
2229874aeea5SJeff Kirsher  *
2230874aeea5SJeff Kirsher  **************************************************************************/
2231874aeea5SJeff Kirsher 
2232874aeea5SJeff Kirsher /* This zeroes out and then fills in the invariants in a struct
2233874aeea5SJeff Kirsher  * efx_nic (including all sub-structures).
2234874aeea5SJeff Kirsher  */
2235874aeea5SJeff Kirsher static int efx_init_struct(struct efx_nic *efx, const struct efx_nic_type *type,
2236874aeea5SJeff Kirsher 			   struct pci_dev *pci_dev, struct net_device *net_dev)
2237874aeea5SJeff Kirsher {
2238874aeea5SJeff Kirsher 	int i;
2239874aeea5SJeff Kirsher 
2240874aeea5SJeff Kirsher 	/* Initialise common structures */
2241874aeea5SJeff Kirsher 	memset(efx, 0, sizeof(*efx));
2242874aeea5SJeff Kirsher 	spin_lock_init(&efx->biu_lock);
2243874aeea5SJeff Kirsher #ifdef CONFIG_SFC_MTD
2244874aeea5SJeff Kirsher 	INIT_LIST_HEAD(&efx->mtd_list);
2245874aeea5SJeff Kirsher #endif
2246874aeea5SJeff Kirsher 	INIT_WORK(&efx->reset_work, efx_reset_work);
2247874aeea5SJeff Kirsher 	INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
2248874aeea5SJeff Kirsher 	efx->pci_dev = pci_dev;
2249874aeea5SJeff Kirsher 	efx->msg_enable = debug;
2250874aeea5SJeff Kirsher 	efx->state = STATE_INIT;
2251874aeea5SJeff Kirsher 	strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
2252874aeea5SJeff Kirsher 
2253874aeea5SJeff Kirsher 	efx->net_dev = net_dev;
2254874aeea5SJeff Kirsher 	spin_lock_init(&efx->stats_lock);
2255874aeea5SJeff Kirsher 	mutex_init(&efx->mac_lock);
2256874aeea5SJeff Kirsher 	efx->phy_op = &efx_dummy_phy_operations;
2257874aeea5SJeff Kirsher 	efx->mdio.dev = net_dev;
2258874aeea5SJeff Kirsher 	INIT_WORK(&efx->mac_work, efx_mac_work);
22599f2cb71cSBen Hutchings 	init_waitqueue_head(&efx->flush_wq);
2260874aeea5SJeff Kirsher 
2261874aeea5SJeff Kirsher 	for (i = 0; i < EFX_MAX_CHANNELS; i++) {
2262874aeea5SJeff Kirsher 		efx->channel[i] = efx_alloc_channel(efx, i, NULL);
2263874aeea5SJeff Kirsher 		if (!efx->channel[i])
2264874aeea5SJeff Kirsher 			goto fail;
2265874aeea5SJeff Kirsher 	}
2266874aeea5SJeff Kirsher 
2267874aeea5SJeff Kirsher 	efx->type = type;
2268874aeea5SJeff Kirsher 
2269874aeea5SJeff Kirsher 	EFX_BUG_ON_PARANOID(efx->type->phys_addr_channels > EFX_MAX_CHANNELS);
2270874aeea5SJeff Kirsher 
2271874aeea5SJeff Kirsher 	/* Higher numbered interrupt modes are less capable! */
2272874aeea5SJeff Kirsher 	efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2273874aeea5SJeff Kirsher 				  interrupt_mode);
2274874aeea5SJeff Kirsher 
2275874aeea5SJeff Kirsher 	/* Would be good to use the net_dev name, but we're too early */
2276874aeea5SJeff Kirsher 	snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2277874aeea5SJeff Kirsher 		 pci_name(pci_dev));
2278874aeea5SJeff Kirsher 	efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
2279874aeea5SJeff Kirsher 	if (!efx->workqueue)
2280874aeea5SJeff Kirsher 		goto fail;
2281874aeea5SJeff Kirsher 
2282874aeea5SJeff Kirsher 	return 0;
2283874aeea5SJeff Kirsher 
2284874aeea5SJeff Kirsher fail:
2285874aeea5SJeff Kirsher 	efx_fini_struct(efx);
2286874aeea5SJeff Kirsher 	return -ENOMEM;
2287874aeea5SJeff Kirsher }
2288874aeea5SJeff Kirsher 
2289874aeea5SJeff Kirsher static void efx_fini_struct(struct efx_nic *efx)
2290874aeea5SJeff Kirsher {
2291874aeea5SJeff Kirsher 	int i;
2292874aeea5SJeff Kirsher 
2293874aeea5SJeff Kirsher 	for (i = 0; i < EFX_MAX_CHANNELS; i++)
2294874aeea5SJeff Kirsher 		kfree(efx->channel[i]);
2295874aeea5SJeff Kirsher 
2296874aeea5SJeff Kirsher 	if (efx->workqueue) {
2297874aeea5SJeff Kirsher 		destroy_workqueue(efx->workqueue);
2298874aeea5SJeff Kirsher 		efx->workqueue = NULL;
2299874aeea5SJeff Kirsher 	}
2300874aeea5SJeff Kirsher }
2301874aeea5SJeff Kirsher 
2302874aeea5SJeff Kirsher /**************************************************************************
2303874aeea5SJeff Kirsher  *
2304874aeea5SJeff Kirsher  * PCI interface
2305874aeea5SJeff Kirsher  *
2306874aeea5SJeff Kirsher  **************************************************************************/
2307874aeea5SJeff Kirsher 
2308874aeea5SJeff Kirsher /* Main body of final NIC shutdown code
2309874aeea5SJeff Kirsher  * This is called only at module unload (or hotplug removal).
2310874aeea5SJeff Kirsher  */
2311874aeea5SJeff Kirsher static void efx_pci_remove_main(struct efx_nic *efx)
2312874aeea5SJeff Kirsher {
2313874aeea5SJeff Kirsher #ifdef CONFIG_RFS_ACCEL
2314874aeea5SJeff Kirsher 	free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
2315874aeea5SJeff Kirsher 	efx->net_dev->rx_cpu_rmap = NULL;
2316874aeea5SJeff Kirsher #endif
23179f2cb71cSBen Hutchings 	efx_stop_interrupts(efx);
2318874aeea5SJeff Kirsher 	efx_nic_fini_interrupt(efx);
2319874aeea5SJeff Kirsher 	efx_fini_port(efx);
2320874aeea5SJeff Kirsher 	efx->type->fini(efx);
2321874aeea5SJeff Kirsher 	efx_fini_napi(efx);
2322874aeea5SJeff Kirsher 	efx_remove_all(efx);
2323874aeea5SJeff Kirsher }
2324874aeea5SJeff Kirsher 
2325874aeea5SJeff Kirsher /* Final NIC shutdown
2326874aeea5SJeff Kirsher  * This is called only at module unload (or hotplug removal).
2327874aeea5SJeff Kirsher  */
2328874aeea5SJeff Kirsher static void efx_pci_remove(struct pci_dev *pci_dev)
2329874aeea5SJeff Kirsher {
2330874aeea5SJeff Kirsher 	struct efx_nic *efx;
2331874aeea5SJeff Kirsher 
2332874aeea5SJeff Kirsher 	efx = pci_get_drvdata(pci_dev);
2333874aeea5SJeff Kirsher 	if (!efx)
2334874aeea5SJeff Kirsher 		return;
2335874aeea5SJeff Kirsher 
2336874aeea5SJeff Kirsher 	/* Mark the NIC as fini, then stop the interface */
2337874aeea5SJeff Kirsher 	rtnl_lock();
2338874aeea5SJeff Kirsher 	efx->state = STATE_FINI;
2339874aeea5SJeff Kirsher 	dev_close(efx->net_dev);
2340874aeea5SJeff Kirsher 
2341874aeea5SJeff Kirsher 	/* Allow any queued efx_resets() to complete */
2342874aeea5SJeff Kirsher 	rtnl_unlock();
2343874aeea5SJeff Kirsher 
23449f2cb71cSBen Hutchings 	efx_stop_interrupts(efx);
2345874aeea5SJeff Kirsher 	efx_unregister_netdev(efx);
2346874aeea5SJeff Kirsher 
2347874aeea5SJeff Kirsher 	efx_mtd_remove(efx);
2348874aeea5SJeff Kirsher 
2349874aeea5SJeff Kirsher 	/* Wait for any scheduled resets to complete. No more will be
2350874aeea5SJeff Kirsher 	 * scheduled from this point because efx_stop_all() has been
2351874aeea5SJeff Kirsher 	 * called, we are no longer registered with driverlink, and
2352874aeea5SJeff Kirsher 	 * the net_device's have been removed. */
2353874aeea5SJeff Kirsher 	cancel_work_sync(&efx->reset_work);
2354874aeea5SJeff Kirsher 
2355874aeea5SJeff Kirsher 	efx_pci_remove_main(efx);
2356874aeea5SJeff Kirsher 
2357874aeea5SJeff Kirsher 	efx_fini_io(efx);
2358874aeea5SJeff Kirsher 	netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
2359874aeea5SJeff Kirsher 
2360874aeea5SJeff Kirsher 	pci_set_drvdata(pci_dev, NULL);
2361874aeea5SJeff Kirsher 	efx_fini_struct(efx);
2362874aeea5SJeff Kirsher 	free_netdev(efx->net_dev);
2363874aeea5SJeff Kirsher };
2364874aeea5SJeff Kirsher 
2365874aeea5SJeff Kirsher /* Main body of NIC initialisation
2366874aeea5SJeff Kirsher  * This is called at module load (or hotplug insertion, theoretically).
2367874aeea5SJeff Kirsher  */
2368874aeea5SJeff Kirsher static int efx_pci_probe_main(struct efx_nic *efx)
2369874aeea5SJeff Kirsher {
2370874aeea5SJeff Kirsher 	int rc;
2371874aeea5SJeff Kirsher 
2372874aeea5SJeff Kirsher 	/* Do start-of-day initialisation */
2373874aeea5SJeff Kirsher 	rc = efx_probe_all(efx);
2374874aeea5SJeff Kirsher 	if (rc)
2375874aeea5SJeff Kirsher 		goto fail1;
2376874aeea5SJeff Kirsher 
2377874aeea5SJeff Kirsher 	efx_init_napi(efx);
2378874aeea5SJeff Kirsher 
2379874aeea5SJeff Kirsher 	rc = efx->type->init(efx);
2380874aeea5SJeff Kirsher 	if (rc) {
2381874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev,
2382874aeea5SJeff Kirsher 			  "failed to initialise NIC\n");
2383874aeea5SJeff Kirsher 		goto fail3;
2384874aeea5SJeff Kirsher 	}
2385874aeea5SJeff Kirsher 
2386874aeea5SJeff Kirsher 	rc = efx_init_port(efx);
2387874aeea5SJeff Kirsher 	if (rc) {
2388874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev,
2389874aeea5SJeff Kirsher 			  "failed to initialise port\n");
2390874aeea5SJeff Kirsher 		goto fail4;
2391874aeea5SJeff Kirsher 	}
2392874aeea5SJeff Kirsher 
2393874aeea5SJeff Kirsher 	rc = efx_nic_init_interrupt(efx);
2394874aeea5SJeff Kirsher 	if (rc)
2395874aeea5SJeff Kirsher 		goto fail5;
23969f2cb71cSBen Hutchings 	efx_start_interrupts(efx);
2397874aeea5SJeff Kirsher 
2398874aeea5SJeff Kirsher 	return 0;
2399874aeea5SJeff Kirsher 
2400874aeea5SJeff Kirsher  fail5:
2401874aeea5SJeff Kirsher 	efx_fini_port(efx);
2402874aeea5SJeff Kirsher  fail4:
2403874aeea5SJeff Kirsher 	efx->type->fini(efx);
2404874aeea5SJeff Kirsher  fail3:
2405874aeea5SJeff Kirsher 	efx_fini_napi(efx);
2406874aeea5SJeff Kirsher 	efx_remove_all(efx);
2407874aeea5SJeff Kirsher  fail1:
2408874aeea5SJeff Kirsher 	return rc;
2409874aeea5SJeff Kirsher }
2410874aeea5SJeff Kirsher 
2411874aeea5SJeff Kirsher /* NIC initialisation
2412874aeea5SJeff Kirsher  *
2413874aeea5SJeff Kirsher  * This is called at module load (or hotplug insertion,
241473ba7b68SBen Hutchings  * theoretically).  It sets up PCI mappings, resets the NIC,
2415874aeea5SJeff Kirsher  * sets up and registers the network devices with the kernel and hooks
2416874aeea5SJeff Kirsher  * the interrupt service routine.  It does not prepare the device for
2417874aeea5SJeff Kirsher  * transmission; this is left to the first time one of the network
2418874aeea5SJeff Kirsher  * interfaces is brought up (i.e. efx_net_open).
2419874aeea5SJeff Kirsher  */
2420874aeea5SJeff Kirsher static int __devinit efx_pci_probe(struct pci_dev *pci_dev,
2421874aeea5SJeff Kirsher 				   const struct pci_device_id *entry)
2422874aeea5SJeff Kirsher {
2423874aeea5SJeff Kirsher 	const struct efx_nic_type *type = (const struct efx_nic_type *) entry->driver_data;
2424874aeea5SJeff Kirsher 	struct net_device *net_dev;
2425874aeea5SJeff Kirsher 	struct efx_nic *efx;
2426fadac6aaSBen Hutchings 	int rc;
2427874aeea5SJeff Kirsher 
2428874aeea5SJeff Kirsher 	/* Allocate and initialise a struct net_device and struct efx_nic */
2429874aeea5SJeff Kirsher 	net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
2430874aeea5SJeff Kirsher 				     EFX_MAX_RX_QUEUES);
2431874aeea5SJeff Kirsher 	if (!net_dev)
2432874aeea5SJeff Kirsher 		return -ENOMEM;
2433874aeea5SJeff Kirsher 	net_dev->features |= (type->offload_features | NETIF_F_SG |
2434874aeea5SJeff Kirsher 			      NETIF_F_HIGHDMA | NETIF_F_TSO |
2435874aeea5SJeff Kirsher 			      NETIF_F_RXCSUM);
2436874aeea5SJeff Kirsher 	if (type->offload_features & NETIF_F_V6_CSUM)
2437874aeea5SJeff Kirsher 		net_dev->features |= NETIF_F_TSO6;
2438874aeea5SJeff Kirsher 	/* Mask for features that also apply to VLAN devices */
2439874aeea5SJeff Kirsher 	net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
2440874aeea5SJeff Kirsher 				   NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
2441874aeea5SJeff Kirsher 				   NETIF_F_RXCSUM);
2442874aeea5SJeff Kirsher 	/* All offloads can be toggled */
2443874aeea5SJeff Kirsher 	net_dev->hw_features = net_dev->features & ~NETIF_F_HIGHDMA;
2444874aeea5SJeff Kirsher 	efx = netdev_priv(net_dev);
2445874aeea5SJeff Kirsher 	pci_set_drvdata(pci_dev, efx);
2446874aeea5SJeff Kirsher 	SET_NETDEV_DEV(net_dev, &pci_dev->dev);
2447874aeea5SJeff Kirsher 	rc = efx_init_struct(efx, type, pci_dev, net_dev);
2448874aeea5SJeff Kirsher 	if (rc)
2449874aeea5SJeff Kirsher 		goto fail1;
2450874aeea5SJeff Kirsher 
2451874aeea5SJeff Kirsher 	netif_info(efx, probe, efx->net_dev,
2452874aeea5SJeff Kirsher 		   "Solarflare NIC detected\n");
2453874aeea5SJeff Kirsher 
2454874aeea5SJeff Kirsher 	/* Set up basic I/O (BAR mappings etc) */
2455874aeea5SJeff Kirsher 	rc = efx_init_io(efx);
2456874aeea5SJeff Kirsher 	if (rc)
2457874aeea5SJeff Kirsher 		goto fail2;
2458874aeea5SJeff Kirsher 
2459874aeea5SJeff Kirsher 	rc = efx_pci_probe_main(efx);
2460874aeea5SJeff Kirsher 
2461874aeea5SJeff Kirsher 	/* Serialise against efx_reset(). No more resets will be
2462fadac6aaSBen Hutchings 	 * scheduled since efx_stop_all() has been called, and we have
2463fadac6aaSBen Hutchings 	 * not and never have been registered.
2464fadac6aaSBen Hutchings 	 */
2465874aeea5SJeff Kirsher 	cancel_work_sync(&efx->reset_work);
2466874aeea5SJeff Kirsher 
2467fadac6aaSBen Hutchings 	if (rc)
2468874aeea5SJeff Kirsher 		goto fail3;
2469874aeea5SJeff Kirsher 
2470fadac6aaSBen Hutchings 	/* If there was a scheduled reset during probe, the NIC is
2471fadac6aaSBen Hutchings 	 * probably hosed anyway.
2472fadac6aaSBen Hutchings 	 */
2473fadac6aaSBen Hutchings 	if (efx->reset_pending) {
2474fadac6aaSBen Hutchings 		rc = -EIO;
2475874aeea5SJeff Kirsher 		goto fail4;
2476874aeea5SJeff Kirsher 	}
2477874aeea5SJeff Kirsher 
2478874aeea5SJeff Kirsher 	/* Switch to the running state before we expose the device to the OS,
2479874aeea5SJeff Kirsher 	 * so that dev_open()|efx_start_all() will actually start the device */
2480874aeea5SJeff Kirsher 	efx->state = STATE_RUNNING;
2481874aeea5SJeff Kirsher 
2482874aeea5SJeff Kirsher 	rc = efx_register_netdev(efx);
2483874aeea5SJeff Kirsher 	if (rc)
2484fadac6aaSBen Hutchings 		goto fail4;
2485874aeea5SJeff Kirsher 
2486874aeea5SJeff Kirsher 	netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
2487874aeea5SJeff Kirsher 
24887c43161cSBen Hutchings 	/* Try to create MTDs, but allow this to fail */
2489874aeea5SJeff Kirsher 	rtnl_lock();
24907c43161cSBen Hutchings 	rc = efx_mtd_probe(efx);
2491874aeea5SJeff Kirsher 	rtnl_unlock();
24927c43161cSBen Hutchings 	if (rc)
24937c43161cSBen Hutchings 		netif_warn(efx, probe, efx->net_dev,
24947c43161cSBen Hutchings 			   "failed to create MTDs (%d)\n", rc);
24957c43161cSBen Hutchings 
2496874aeea5SJeff Kirsher 	return 0;
2497874aeea5SJeff Kirsher 
2498874aeea5SJeff Kirsher  fail4:
2499fadac6aaSBen Hutchings 	efx_pci_remove_main(efx);
2500874aeea5SJeff Kirsher  fail3:
2501874aeea5SJeff Kirsher 	efx_fini_io(efx);
2502874aeea5SJeff Kirsher  fail2:
2503874aeea5SJeff Kirsher 	efx_fini_struct(efx);
2504874aeea5SJeff Kirsher  fail1:
2505874aeea5SJeff Kirsher 	WARN_ON(rc > 0);
2506874aeea5SJeff Kirsher 	netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
2507874aeea5SJeff Kirsher 	free_netdev(net_dev);
2508874aeea5SJeff Kirsher 	return rc;
2509874aeea5SJeff Kirsher }
2510874aeea5SJeff Kirsher 
2511874aeea5SJeff Kirsher static int efx_pm_freeze(struct device *dev)
2512874aeea5SJeff Kirsher {
2513874aeea5SJeff Kirsher 	struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2514874aeea5SJeff Kirsher 
2515874aeea5SJeff Kirsher 	efx->state = STATE_FINI;
2516874aeea5SJeff Kirsher 
2517874aeea5SJeff Kirsher 	netif_device_detach(efx->net_dev);
2518874aeea5SJeff Kirsher 
2519874aeea5SJeff Kirsher 	efx_stop_all(efx);
25209f2cb71cSBen Hutchings 	efx_stop_interrupts(efx);
2521874aeea5SJeff Kirsher 
2522874aeea5SJeff Kirsher 	return 0;
2523874aeea5SJeff Kirsher }
2524874aeea5SJeff Kirsher 
2525874aeea5SJeff Kirsher static int efx_pm_thaw(struct device *dev)
2526874aeea5SJeff Kirsher {
2527874aeea5SJeff Kirsher 	struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2528874aeea5SJeff Kirsher 
2529874aeea5SJeff Kirsher 	efx->state = STATE_INIT;
2530874aeea5SJeff Kirsher 
25319f2cb71cSBen Hutchings 	efx_start_interrupts(efx);
2532874aeea5SJeff Kirsher 
2533874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
2534874aeea5SJeff Kirsher 	efx->phy_op->reconfigure(efx);
2535874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
2536874aeea5SJeff Kirsher 
2537874aeea5SJeff Kirsher 	efx_start_all(efx);
2538874aeea5SJeff Kirsher 
2539874aeea5SJeff Kirsher 	netif_device_attach(efx->net_dev);
2540874aeea5SJeff Kirsher 
2541874aeea5SJeff Kirsher 	efx->state = STATE_RUNNING;
2542874aeea5SJeff Kirsher 
2543874aeea5SJeff Kirsher 	efx->type->resume_wol(efx);
2544874aeea5SJeff Kirsher 
2545874aeea5SJeff Kirsher 	/* Reschedule any quenched resets scheduled during efx_pm_freeze() */
2546874aeea5SJeff Kirsher 	queue_work(reset_workqueue, &efx->reset_work);
2547874aeea5SJeff Kirsher 
2548874aeea5SJeff Kirsher 	return 0;
2549874aeea5SJeff Kirsher }
2550874aeea5SJeff Kirsher 
2551874aeea5SJeff Kirsher static int efx_pm_poweroff(struct device *dev)
2552874aeea5SJeff Kirsher {
2553874aeea5SJeff Kirsher 	struct pci_dev *pci_dev = to_pci_dev(dev);
2554874aeea5SJeff Kirsher 	struct efx_nic *efx = pci_get_drvdata(pci_dev);
2555874aeea5SJeff Kirsher 
2556874aeea5SJeff Kirsher 	efx->type->fini(efx);
2557874aeea5SJeff Kirsher 
2558874aeea5SJeff Kirsher 	efx->reset_pending = 0;
2559874aeea5SJeff Kirsher 
2560874aeea5SJeff Kirsher 	pci_save_state(pci_dev);
2561874aeea5SJeff Kirsher 	return pci_set_power_state(pci_dev, PCI_D3hot);
2562874aeea5SJeff Kirsher }
2563874aeea5SJeff Kirsher 
2564874aeea5SJeff Kirsher /* Used for both resume and restore */
2565874aeea5SJeff Kirsher static int efx_pm_resume(struct device *dev)
2566874aeea5SJeff Kirsher {
2567874aeea5SJeff Kirsher 	struct pci_dev *pci_dev = to_pci_dev(dev);
2568874aeea5SJeff Kirsher 	struct efx_nic *efx = pci_get_drvdata(pci_dev);
2569874aeea5SJeff Kirsher 	int rc;
2570874aeea5SJeff Kirsher 
2571874aeea5SJeff Kirsher 	rc = pci_set_power_state(pci_dev, PCI_D0);
2572874aeea5SJeff Kirsher 	if (rc)
2573874aeea5SJeff Kirsher 		return rc;
2574874aeea5SJeff Kirsher 	pci_restore_state(pci_dev);
2575874aeea5SJeff Kirsher 	rc = pci_enable_device(pci_dev);
2576874aeea5SJeff Kirsher 	if (rc)
2577874aeea5SJeff Kirsher 		return rc;
2578874aeea5SJeff Kirsher 	pci_set_master(efx->pci_dev);
2579874aeea5SJeff Kirsher 	rc = efx->type->reset(efx, RESET_TYPE_ALL);
2580874aeea5SJeff Kirsher 	if (rc)
2581874aeea5SJeff Kirsher 		return rc;
2582874aeea5SJeff Kirsher 	rc = efx->type->init(efx);
2583874aeea5SJeff Kirsher 	if (rc)
2584874aeea5SJeff Kirsher 		return rc;
2585874aeea5SJeff Kirsher 	efx_pm_thaw(dev);
2586874aeea5SJeff Kirsher 	return 0;
2587874aeea5SJeff Kirsher }
2588874aeea5SJeff Kirsher 
2589874aeea5SJeff Kirsher static int efx_pm_suspend(struct device *dev)
2590874aeea5SJeff Kirsher {
2591874aeea5SJeff Kirsher 	int rc;
2592874aeea5SJeff Kirsher 
2593874aeea5SJeff Kirsher 	efx_pm_freeze(dev);
2594874aeea5SJeff Kirsher 	rc = efx_pm_poweroff(dev);
2595874aeea5SJeff Kirsher 	if (rc)
2596874aeea5SJeff Kirsher 		efx_pm_resume(dev);
2597874aeea5SJeff Kirsher 	return rc;
2598874aeea5SJeff Kirsher }
2599874aeea5SJeff Kirsher 
260018e83e4cSBen Hutchings static const struct dev_pm_ops efx_pm_ops = {
2601874aeea5SJeff Kirsher 	.suspend	= efx_pm_suspend,
2602874aeea5SJeff Kirsher 	.resume		= efx_pm_resume,
2603874aeea5SJeff Kirsher 	.freeze		= efx_pm_freeze,
2604874aeea5SJeff Kirsher 	.thaw		= efx_pm_thaw,
2605874aeea5SJeff Kirsher 	.poweroff	= efx_pm_poweroff,
2606874aeea5SJeff Kirsher 	.restore	= efx_pm_resume,
2607874aeea5SJeff Kirsher };
2608874aeea5SJeff Kirsher 
2609874aeea5SJeff Kirsher static struct pci_driver efx_pci_driver = {
2610874aeea5SJeff Kirsher 	.name		= KBUILD_MODNAME,
2611874aeea5SJeff Kirsher 	.id_table	= efx_pci_table,
2612874aeea5SJeff Kirsher 	.probe		= efx_pci_probe,
2613874aeea5SJeff Kirsher 	.remove		= efx_pci_remove,
2614874aeea5SJeff Kirsher 	.driver.pm	= &efx_pm_ops,
2615874aeea5SJeff Kirsher };
2616874aeea5SJeff Kirsher 
2617874aeea5SJeff Kirsher /**************************************************************************
2618874aeea5SJeff Kirsher  *
2619874aeea5SJeff Kirsher  * Kernel module interface
2620874aeea5SJeff Kirsher  *
2621874aeea5SJeff Kirsher  *************************************************************************/
2622874aeea5SJeff Kirsher 
2623874aeea5SJeff Kirsher module_param(interrupt_mode, uint, 0444);
2624874aeea5SJeff Kirsher MODULE_PARM_DESC(interrupt_mode,
2625874aeea5SJeff Kirsher 		 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
2626874aeea5SJeff Kirsher 
2627874aeea5SJeff Kirsher static int __init efx_init_module(void)
2628874aeea5SJeff Kirsher {
2629874aeea5SJeff Kirsher 	int rc;
2630874aeea5SJeff Kirsher 
2631874aeea5SJeff Kirsher 	printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
2632874aeea5SJeff Kirsher 
2633874aeea5SJeff Kirsher 	rc = register_netdevice_notifier(&efx_netdev_notifier);
2634874aeea5SJeff Kirsher 	if (rc)
2635874aeea5SJeff Kirsher 		goto err_notifier;
2636874aeea5SJeff Kirsher 
2637874aeea5SJeff Kirsher 	reset_workqueue = create_singlethread_workqueue("sfc_reset");
2638874aeea5SJeff Kirsher 	if (!reset_workqueue) {
2639874aeea5SJeff Kirsher 		rc = -ENOMEM;
2640874aeea5SJeff Kirsher 		goto err_reset;
2641874aeea5SJeff Kirsher 	}
2642874aeea5SJeff Kirsher 
2643874aeea5SJeff Kirsher 	rc = pci_register_driver(&efx_pci_driver);
2644874aeea5SJeff Kirsher 	if (rc < 0)
2645874aeea5SJeff Kirsher 		goto err_pci;
2646874aeea5SJeff Kirsher 
2647874aeea5SJeff Kirsher 	return 0;
2648874aeea5SJeff Kirsher 
2649874aeea5SJeff Kirsher  err_pci:
2650874aeea5SJeff Kirsher 	destroy_workqueue(reset_workqueue);
2651874aeea5SJeff Kirsher  err_reset:
2652874aeea5SJeff Kirsher 	unregister_netdevice_notifier(&efx_netdev_notifier);
2653874aeea5SJeff Kirsher  err_notifier:
2654874aeea5SJeff Kirsher 	return rc;
2655874aeea5SJeff Kirsher }
2656874aeea5SJeff Kirsher 
2657874aeea5SJeff Kirsher static void __exit efx_exit_module(void)
2658874aeea5SJeff Kirsher {
2659874aeea5SJeff Kirsher 	printk(KERN_INFO "Solarflare NET driver unloading\n");
2660874aeea5SJeff Kirsher 
2661874aeea5SJeff Kirsher 	pci_unregister_driver(&efx_pci_driver);
2662874aeea5SJeff Kirsher 	destroy_workqueue(reset_workqueue);
2663874aeea5SJeff Kirsher 	unregister_netdevice_notifier(&efx_netdev_notifier);
2664874aeea5SJeff Kirsher 
2665874aeea5SJeff Kirsher }
2666874aeea5SJeff Kirsher 
2667874aeea5SJeff Kirsher module_init(efx_init_module);
2668874aeea5SJeff Kirsher module_exit(efx_exit_module);
2669874aeea5SJeff Kirsher 
2670874aeea5SJeff Kirsher MODULE_AUTHOR("Solarflare Communications and "
2671874aeea5SJeff Kirsher 	      "Michael Brown <mbrown@fensystems.co.uk>");
2672874aeea5SJeff Kirsher MODULE_DESCRIPTION("Solarflare Communications network driver");
2673874aeea5SJeff Kirsher MODULE_LICENSE("GPL");
2674874aeea5SJeff Kirsher MODULE_DEVICE_TABLE(pci, efx_pci_table);
2675