xref: /openbmc/linux/drivers/net/ethernet/sfc/efx.c (revision a9a52506)
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 
1897f967c01SBen Hutchings static void efx_start_interrupts(struct efx_nic *efx, bool may_keep_eventq);
1907f967c01SBen Hutchings static void efx_stop_interrupts(struct efx_nic *efx, bool may_keep_eventq);
1917f967c01SBen Hutchings static void efx_remove_channel(struct efx_channel *channel);
192874aeea5SJeff Kirsher static void efx_remove_channels(struct efx_nic *efx);
1937f967c01SBen Hutchings static const struct efx_channel_type efx_default_channel_type;
194874aeea5SJeff Kirsher static void efx_remove_port(struct efx_nic *efx);
1957f967c01SBen Hutchings static void efx_init_napi_channel(struct efx_channel *channel);
196874aeea5SJeff Kirsher static void efx_fini_napi(struct efx_nic *efx);
197874aeea5SJeff Kirsher static void efx_fini_napi_channel(struct efx_channel *channel);
198874aeea5SJeff Kirsher static void efx_fini_struct(struct efx_nic *efx);
199874aeea5SJeff Kirsher static void efx_start_all(struct efx_nic *efx);
200874aeea5SJeff Kirsher static void efx_stop_all(struct efx_nic *efx);
201874aeea5SJeff Kirsher 
202874aeea5SJeff Kirsher #define EFX_ASSERT_RESET_SERIALISED(efx)		\
203874aeea5SJeff Kirsher 	do {						\
204874aeea5SJeff Kirsher 		if ((efx->state == STATE_RUNNING) ||	\
205874aeea5SJeff Kirsher 		    (efx->state == STATE_DISABLED))	\
206874aeea5SJeff Kirsher 			ASSERT_RTNL();			\
207874aeea5SJeff Kirsher 	} while (0)
208874aeea5SJeff Kirsher 
209874aeea5SJeff Kirsher /**************************************************************************
210874aeea5SJeff Kirsher  *
211874aeea5SJeff Kirsher  * Event queue processing
212874aeea5SJeff Kirsher  *
213874aeea5SJeff Kirsher  *************************************************************************/
214874aeea5SJeff Kirsher 
215874aeea5SJeff Kirsher /* Process channel's event queue
216874aeea5SJeff Kirsher  *
217874aeea5SJeff Kirsher  * This function is responsible for processing the event queue of a
218874aeea5SJeff Kirsher  * single channel.  The caller must guarantee that this function will
219874aeea5SJeff Kirsher  * never be concurrently called more than once on the same channel,
220874aeea5SJeff Kirsher  * though different channels may be being processed concurrently.
221874aeea5SJeff Kirsher  */
222874aeea5SJeff Kirsher static int efx_process_channel(struct efx_channel *channel, int budget)
223874aeea5SJeff Kirsher {
224874aeea5SJeff Kirsher 	int spent;
225874aeea5SJeff Kirsher 
2269f2cb71cSBen Hutchings 	if (unlikely(!channel->enabled))
227874aeea5SJeff Kirsher 		return 0;
228874aeea5SJeff Kirsher 
229874aeea5SJeff Kirsher 	spent = efx_nic_process_eventq(channel, budget);
230d9ab7007SBen Hutchings 	if (spent && efx_channel_has_rx_queue(channel)) {
231d9ab7007SBen Hutchings 		struct efx_rx_queue *rx_queue =
232d9ab7007SBen Hutchings 			efx_channel_get_rx_queue(channel);
233874aeea5SJeff Kirsher 
234874aeea5SJeff Kirsher 		/* Deliver last RX packet. */
235874aeea5SJeff Kirsher 		if (channel->rx_pkt) {
236db339569SBen Hutchings 			__efx_rx_packet(channel, channel->rx_pkt);
237874aeea5SJeff Kirsher 			channel->rx_pkt = NULL;
238874aeea5SJeff Kirsher 		}
2399f2cb71cSBen Hutchings 		if (rx_queue->enabled) {
240874aeea5SJeff Kirsher 			efx_rx_strategy(channel);
241d9ab7007SBen Hutchings 			efx_fast_push_rx_descriptors(rx_queue);
242d9ab7007SBen Hutchings 		}
2439f2cb71cSBen Hutchings 	}
244874aeea5SJeff Kirsher 
245874aeea5SJeff Kirsher 	return spent;
246874aeea5SJeff Kirsher }
247874aeea5SJeff Kirsher 
248874aeea5SJeff Kirsher /* Mark channel as finished processing
249874aeea5SJeff Kirsher  *
250874aeea5SJeff Kirsher  * Note that since we will not receive further interrupts for this
251874aeea5SJeff Kirsher  * channel before we finish processing and call the eventq_read_ack()
252874aeea5SJeff Kirsher  * method, there is no need to use the interrupt hold-off timers.
253874aeea5SJeff Kirsher  */
254874aeea5SJeff Kirsher static inline void efx_channel_processed(struct efx_channel *channel)
255874aeea5SJeff Kirsher {
256874aeea5SJeff Kirsher 	/* The interrupt handler for this channel may set work_pending
257874aeea5SJeff Kirsher 	 * as soon as we acknowledge the events we've seen.  Make sure
258874aeea5SJeff Kirsher 	 * it's cleared before then. */
259874aeea5SJeff Kirsher 	channel->work_pending = false;
260874aeea5SJeff Kirsher 	smp_wmb();
261874aeea5SJeff Kirsher 
262874aeea5SJeff Kirsher 	efx_nic_eventq_read_ack(channel);
263874aeea5SJeff Kirsher }
264874aeea5SJeff Kirsher 
265874aeea5SJeff Kirsher /* NAPI poll handler
266874aeea5SJeff Kirsher  *
267874aeea5SJeff Kirsher  * NAPI guarantees serialisation of polls of the same device, which
268874aeea5SJeff Kirsher  * provides the guarantee required by efx_process_channel().
269874aeea5SJeff Kirsher  */
270874aeea5SJeff Kirsher static int efx_poll(struct napi_struct *napi, int budget)
271874aeea5SJeff Kirsher {
272874aeea5SJeff Kirsher 	struct efx_channel *channel =
273874aeea5SJeff Kirsher 		container_of(napi, struct efx_channel, napi_str);
274874aeea5SJeff Kirsher 	struct efx_nic *efx = channel->efx;
275874aeea5SJeff Kirsher 	int spent;
276874aeea5SJeff Kirsher 
277874aeea5SJeff Kirsher 	netif_vdbg(efx, intr, efx->net_dev,
278874aeea5SJeff Kirsher 		   "channel %d NAPI poll executing on CPU %d\n",
279874aeea5SJeff Kirsher 		   channel->channel, raw_smp_processor_id());
280874aeea5SJeff Kirsher 
281874aeea5SJeff Kirsher 	spent = efx_process_channel(channel, budget);
282874aeea5SJeff Kirsher 
283874aeea5SJeff Kirsher 	if (spent < budget) {
2849d9a6973SBen Hutchings 		if (efx_channel_has_rx_queue(channel) &&
285874aeea5SJeff Kirsher 		    efx->irq_rx_adaptive &&
286874aeea5SJeff Kirsher 		    unlikely(++channel->irq_count == 1000)) {
287874aeea5SJeff Kirsher 			if (unlikely(channel->irq_mod_score <
288874aeea5SJeff Kirsher 				     irq_adapt_low_thresh)) {
289874aeea5SJeff Kirsher 				if (channel->irq_moderation > 1) {
290874aeea5SJeff Kirsher 					channel->irq_moderation -= 1;
291874aeea5SJeff Kirsher 					efx->type->push_irq_moderation(channel);
292874aeea5SJeff Kirsher 				}
293874aeea5SJeff Kirsher 			} else if (unlikely(channel->irq_mod_score >
294874aeea5SJeff Kirsher 					    irq_adapt_high_thresh)) {
295874aeea5SJeff Kirsher 				if (channel->irq_moderation <
296874aeea5SJeff Kirsher 				    efx->irq_rx_moderation) {
297874aeea5SJeff Kirsher 					channel->irq_moderation += 1;
298874aeea5SJeff Kirsher 					efx->type->push_irq_moderation(channel);
299874aeea5SJeff Kirsher 				}
300874aeea5SJeff Kirsher 			}
301874aeea5SJeff Kirsher 			channel->irq_count = 0;
302874aeea5SJeff Kirsher 			channel->irq_mod_score = 0;
303874aeea5SJeff Kirsher 		}
304874aeea5SJeff Kirsher 
305874aeea5SJeff Kirsher 		efx_filter_rfs_expire(channel);
306874aeea5SJeff Kirsher 
307874aeea5SJeff Kirsher 		/* There is no race here; although napi_disable() will
308874aeea5SJeff Kirsher 		 * only wait for napi_complete(), this isn't a problem
309874aeea5SJeff Kirsher 		 * since efx_channel_processed() will have no effect if
310874aeea5SJeff Kirsher 		 * interrupts have already been disabled.
311874aeea5SJeff Kirsher 		 */
312874aeea5SJeff Kirsher 		napi_complete(napi);
313874aeea5SJeff Kirsher 		efx_channel_processed(channel);
314874aeea5SJeff Kirsher 	}
315874aeea5SJeff Kirsher 
316874aeea5SJeff Kirsher 	return spent;
317874aeea5SJeff Kirsher }
318874aeea5SJeff Kirsher 
319874aeea5SJeff Kirsher /* Process the eventq of the specified channel immediately on this CPU
320874aeea5SJeff Kirsher  *
321874aeea5SJeff Kirsher  * Disable hardware generated interrupts, wait for any existing
322874aeea5SJeff Kirsher  * processing to finish, then directly poll (and ack ) the eventq.
323874aeea5SJeff Kirsher  * Finally reenable NAPI and interrupts.
324874aeea5SJeff Kirsher  *
325874aeea5SJeff Kirsher  * This is for use only during a loopback self-test.  It must not
326874aeea5SJeff Kirsher  * deliver any packets up the stack as this can result in deadlock.
327874aeea5SJeff Kirsher  */
328874aeea5SJeff Kirsher void efx_process_channel_now(struct efx_channel *channel)
329874aeea5SJeff Kirsher {
330874aeea5SJeff Kirsher 	struct efx_nic *efx = channel->efx;
331874aeea5SJeff Kirsher 
332874aeea5SJeff Kirsher 	BUG_ON(channel->channel >= efx->n_channels);
333874aeea5SJeff Kirsher 	BUG_ON(!channel->enabled);
334874aeea5SJeff Kirsher 	BUG_ON(!efx->loopback_selftest);
335874aeea5SJeff Kirsher 
336874aeea5SJeff Kirsher 	/* Disable interrupts and wait for ISRs to complete */
337874aeea5SJeff Kirsher 	efx_nic_disable_interrupts(efx);
338874aeea5SJeff Kirsher 	if (efx->legacy_irq) {
339874aeea5SJeff Kirsher 		synchronize_irq(efx->legacy_irq);
340874aeea5SJeff Kirsher 		efx->legacy_irq_enabled = false;
341874aeea5SJeff Kirsher 	}
342874aeea5SJeff Kirsher 	if (channel->irq)
343874aeea5SJeff Kirsher 		synchronize_irq(channel->irq);
344874aeea5SJeff Kirsher 
345874aeea5SJeff Kirsher 	/* Wait for any NAPI processing to complete */
346874aeea5SJeff Kirsher 	napi_disable(&channel->napi_str);
347874aeea5SJeff Kirsher 
348874aeea5SJeff Kirsher 	/* Poll the channel */
349874aeea5SJeff Kirsher 	efx_process_channel(channel, channel->eventq_mask + 1);
350874aeea5SJeff Kirsher 
351874aeea5SJeff Kirsher 	/* Ack the eventq. This may cause an interrupt to be generated
352874aeea5SJeff Kirsher 	 * when they are reenabled */
353874aeea5SJeff Kirsher 	efx_channel_processed(channel);
354874aeea5SJeff Kirsher 
355874aeea5SJeff Kirsher 	napi_enable(&channel->napi_str);
356874aeea5SJeff Kirsher 	if (efx->legacy_irq)
357874aeea5SJeff Kirsher 		efx->legacy_irq_enabled = true;
358874aeea5SJeff Kirsher 	efx_nic_enable_interrupts(efx);
359874aeea5SJeff Kirsher }
360874aeea5SJeff Kirsher 
361874aeea5SJeff Kirsher /* Create event queue
362874aeea5SJeff Kirsher  * Event queue memory allocations are done only once.  If the channel
363874aeea5SJeff Kirsher  * is reset, the memory buffer will be reused; this guards against
364874aeea5SJeff Kirsher  * errors during channel reset and also simplifies interrupt handling.
365874aeea5SJeff Kirsher  */
366874aeea5SJeff Kirsher static int efx_probe_eventq(struct efx_channel *channel)
367874aeea5SJeff Kirsher {
368874aeea5SJeff Kirsher 	struct efx_nic *efx = channel->efx;
369874aeea5SJeff Kirsher 	unsigned long entries;
370874aeea5SJeff Kirsher 
37186ee5302SBen Hutchings 	netif_dbg(efx, probe, efx->net_dev,
372874aeea5SJeff Kirsher 		  "chan %d create event queue\n", channel->channel);
373874aeea5SJeff Kirsher 
374874aeea5SJeff Kirsher 	/* Build an event queue with room for one event per tx and rx buffer,
375874aeea5SJeff Kirsher 	 * plus some extra for link state events and MCDI completions. */
376874aeea5SJeff Kirsher 	entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
377874aeea5SJeff Kirsher 	EFX_BUG_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
378874aeea5SJeff Kirsher 	channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
379874aeea5SJeff Kirsher 
380874aeea5SJeff Kirsher 	return efx_nic_probe_eventq(channel);
381874aeea5SJeff Kirsher }
382874aeea5SJeff Kirsher 
383874aeea5SJeff Kirsher /* Prepare channel's event queue */
384874aeea5SJeff Kirsher static void efx_init_eventq(struct efx_channel *channel)
385874aeea5SJeff Kirsher {
386874aeea5SJeff Kirsher 	netif_dbg(channel->efx, drv, channel->efx->net_dev,
387874aeea5SJeff Kirsher 		  "chan %d init event queue\n", channel->channel);
388874aeea5SJeff Kirsher 
389874aeea5SJeff Kirsher 	channel->eventq_read_ptr = 0;
390874aeea5SJeff Kirsher 
391874aeea5SJeff Kirsher 	efx_nic_init_eventq(channel);
392874aeea5SJeff Kirsher }
393874aeea5SJeff Kirsher 
3949f2cb71cSBen Hutchings /* Enable event queue processing and NAPI */
3959f2cb71cSBen Hutchings static void efx_start_eventq(struct efx_channel *channel)
3969f2cb71cSBen Hutchings {
3979f2cb71cSBen Hutchings 	netif_dbg(channel->efx, ifup, channel->efx->net_dev,
3989f2cb71cSBen Hutchings 		  "chan %d start event queue\n", channel->channel);
3999f2cb71cSBen Hutchings 
4009f2cb71cSBen Hutchings 	/* The interrupt handler for this channel may set work_pending
4019f2cb71cSBen Hutchings 	 * as soon as we enable it.  Make sure it's cleared before
4029f2cb71cSBen Hutchings 	 * then.  Similarly, make sure it sees the enabled flag set.
4039f2cb71cSBen Hutchings 	 */
4049f2cb71cSBen Hutchings 	channel->work_pending = false;
4059f2cb71cSBen Hutchings 	channel->enabled = true;
4069f2cb71cSBen Hutchings 	smp_wmb();
4079f2cb71cSBen Hutchings 
4089f2cb71cSBen Hutchings 	napi_enable(&channel->napi_str);
4099f2cb71cSBen Hutchings 	efx_nic_eventq_read_ack(channel);
4109f2cb71cSBen Hutchings }
4119f2cb71cSBen Hutchings 
4129f2cb71cSBen Hutchings /* Disable event queue processing and NAPI */
4139f2cb71cSBen Hutchings static void efx_stop_eventq(struct efx_channel *channel)
4149f2cb71cSBen Hutchings {
4159f2cb71cSBen Hutchings 	if (!channel->enabled)
4169f2cb71cSBen Hutchings 		return;
4179f2cb71cSBen Hutchings 
4189f2cb71cSBen Hutchings 	napi_disable(&channel->napi_str);
4199f2cb71cSBen Hutchings 	channel->enabled = false;
4209f2cb71cSBen Hutchings }
4219f2cb71cSBen Hutchings 
422874aeea5SJeff Kirsher static void efx_fini_eventq(struct efx_channel *channel)
423874aeea5SJeff Kirsher {
424874aeea5SJeff Kirsher 	netif_dbg(channel->efx, drv, channel->efx->net_dev,
425874aeea5SJeff Kirsher 		  "chan %d fini event queue\n", channel->channel);
426874aeea5SJeff Kirsher 
427874aeea5SJeff Kirsher 	efx_nic_fini_eventq(channel);
428874aeea5SJeff Kirsher }
429874aeea5SJeff Kirsher 
430874aeea5SJeff Kirsher static void efx_remove_eventq(struct efx_channel *channel)
431874aeea5SJeff Kirsher {
432874aeea5SJeff Kirsher 	netif_dbg(channel->efx, drv, channel->efx->net_dev,
433874aeea5SJeff Kirsher 		  "chan %d remove event queue\n", channel->channel);
434874aeea5SJeff Kirsher 
435874aeea5SJeff Kirsher 	efx_nic_remove_eventq(channel);
436874aeea5SJeff Kirsher }
437874aeea5SJeff Kirsher 
438874aeea5SJeff Kirsher /**************************************************************************
439874aeea5SJeff Kirsher  *
440874aeea5SJeff Kirsher  * Channel handling
441874aeea5SJeff Kirsher  *
442874aeea5SJeff Kirsher  *************************************************************************/
443874aeea5SJeff Kirsher 
4447f967c01SBen Hutchings /* Allocate and initialise a channel structure. */
445874aeea5SJeff Kirsher static struct efx_channel *
446874aeea5SJeff Kirsher efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
447874aeea5SJeff Kirsher {
448874aeea5SJeff Kirsher 	struct efx_channel *channel;
449874aeea5SJeff Kirsher 	struct efx_rx_queue *rx_queue;
450874aeea5SJeff Kirsher 	struct efx_tx_queue *tx_queue;
451874aeea5SJeff Kirsher 	int j;
452874aeea5SJeff Kirsher 
4537f967c01SBen Hutchings 	channel = kzalloc(sizeof(*channel), GFP_KERNEL);
4547f967c01SBen Hutchings 	if (!channel)
4557f967c01SBen Hutchings 		return NULL;
4567f967c01SBen Hutchings 
4577f967c01SBen Hutchings 	channel->efx = efx;
4587f967c01SBen Hutchings 	channel->channel = i;
4597f967c01SBen Hutchings 	channel->type = &efx_default_channel_type;
4607f967c01SBen Hutchings 
4617f967c01SBen Hutchings 	for (j = 0; j < EFX_TXQ_TYPES; j++) {
4627f967c01SBen Hutchings 		tx_queue = &channel->tx_queue[j];
4637f967c01SBen Hutchings 		tx_queue->efx = efx;
4647f967c01SBen Hutchings 		tx_queue->queue = i * EFX_TXQ_TYPES + j;
4657f967c01SBen Hutchings 		tx_queue->channel = channel;
4667f967c01SBen Hutchings 	}
4677f967c01SBen Hutchings 
4687f967c01SBen Hutchings 	rx_queue = &channel->rx_queue;
4697f967c01SBen Hutchings 	rx_queue->efx = efx;
4707f967c01SBen Hutchings 	setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
4717f967c01SBen Hutchings 		    (unsigned long)rx_queue);
4727f967c01SBen Hutchings 
4737f967c01SBen Hutchings 	return channel;
4747f967c01SBen Hutchings }
4757f967c01SBen Hutchings 
4767f967c01SBen Hutchings /* Allocate and initialise a channel structure, copying parameters
4777f967c01SBen Hutchings  * (but not resources) from an old channel structure.
4787f967c01SBen Hutchings  */
4797f967c01SBen Hutchings static struct efx_channel *
4807f967c01SBen Hutchings efx_copy_channel(const struct efx_channel *old_channel)
4817f967c01SBen Hutchings {
4827f967c01SBen Hutchings 	struct efx_channel *channel;
4837f967c01SBen Hutchings 	struct efx_rx_queue *rx_queue;
4847f967c01SBen Hutchings 	struct efx_tx_queue *tx_queue;
4857f967c01SBen Hutchings 	int j;
4867f967c01SBen Hutchings 
487874aeea5SJeff Kirsher 	channel = kmalloc(sizeof(*channel), GFP_KERNEL);
488874aeea5SJeff Kirsher 	if (!channel)
489874aeea5SJeff Kirsher 		return NULL;
490874aeea5SJeff Kirsher 
491874aeea5SJeff Kirsher 	*channel = *old_channel;
492874aeea5SJeff Kirsher 
493874aeea5SJeff Kirsher 	channel->napi_dev = NULL;
494874aeea5SJeff Kirsher 	memset(&channel->eventq, 0, sizeof(channel->eventq));
495874aeea5SJeff Kirsher 
496874aeea5SJeff Kirsher 	for (j = 0; j < EFX_TXQ_TYPES; j++) {
497874aeea5SJeff Kirsher 		tx_queue = &channel->tx_queue[j];
498874aeea5SJeff Kirsher 		if (tx_queue->channel)
499874aeea5SJeff Kirsher 			tx_queue->channel = channel;
500874aeea5SJeff Kirsher 		tx_queue->buffer = NULL;
501874aeea5SJeff Kirsher 		memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
502874aeea5SJeff Kirsher 	}
503874aeea5SJeff Kirsher 
504874aeea5SJeff Kirsher 	rx_queue = &channel->rx_queue;
5057f967c01SBen Hutchings 	rx_queue->buffer = NULL;
5067f967c01SBen Hutchings 	memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
507874aeea5SJeff Kirsher 	setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
508874aeea5SJeff Kirsher 		    (unsigned long)rx_queue);
509874aeea5SJeff Kirsher 
510874aeea5SJeff Kirsher 	return channel;
511874aeea5SJeff Kirsher }
512874aeea5SJeff Kirsher 
513874aeea5SJeff Kirsher static int efx_probe_channel(struct efx_channel *channel)
514874aeea5SJeff Kirsher {
515874aeea5SJeff Kirsher 	struct efx_tx_queue *tx_queue;
516874aeea5SJeff Kirsher 	struct efx_rx_queue *rx_queue;
517874aeea5SJeff Kirsher 	int rc;
518874aeea5SJeff Kirsher 
519874aeea5SJeff Kirsher 	netif_dbg(channel->efx, probe, channel->efx->net_dev,
520874aeea5SJeff Kirsher 		  "creating channel %d\n", channel->channel);
521874aeea5SJeff Kirsher 
5227f967c01SBen Hutchings 	rc = channel->type->pre_probe(channel);
5237f967c01SBen Hutchings 	if (rc)
5247f967c01SBen Hutchings 		goto fail;
5257f967c01SBen Hutchings 
526874aeea5SJeff Kirsher 	rc = efx_probe_eventq(channel);
527874aeea5SJeff Kirsher 	if (rc)
5287f967c01SBen Hutchings 		goto fail;
529874aeea5SJeff Kirsher 
530874aeea5SJeff Kirsher 	efx_for_each_channel_tx_queue(tx_queue, channel) {
531874aeea5SJeff Kirsher 		rc = efx_probe_tx_queue(tx_queue);
532874aeea5SJeff Kirsher 		if (rc)
5337f967c01SBen Hutchings 			goto fail;
534874aeea5SJeff Kirsher 	}
535874aeea5SJeff Kirsher 
536874aeea5SJeff Kirsher 	efx_for_each_channel_rx_queue(rx_queue, channel) {
537874aeea5SJeff Kirsher 		rc = efx_probe_rx_queue(rx_queue);
538874aeea5SJeff Kirsher 		if (rc)
5397f967c01SBen Hutchings 			goto fail;
540874aeea5SJeff Kirsher 	}
541874aeea5SJeff Kirsher 
542874aeea5SJeff Kirsher 	channel->n_rx_frm_trunc = 0;
543874aeea5SJeff Kirsher 
544874aeea5SJeff Kirsher 	return 0;
545874aeea5SJeff Kirsher 
5467f967c01SBen Hutchings fail:
5477f967c01SBen Hutchings 	efx_remove_channel(channel);
548874aeea5SJeff Kirsher 	return rc;
549874aeea5SJeff Kirsher }
550874aeea5SJeff Kirsher 
5517f967c01SBen Hutchings static void
5527f967c01SBen Hutchings efx_get_channel_name(struct efx_channel *channel, char *buf, size_t len)
5537f967c01SBen Hutchings {
5547f967c01SBen Hutchings 	struct efx_nic *efx = channel->efx;
5557f967c01SBen Hutchings 	const char *type;
5567f967c01SBen Hutchings 	int number;
5577f967c01SBen Hutchings 
5587f967c01SBen Hutchings 	number = channel->channel;
5597f967c01SBen Hutchings 	if (efx->tx_channel_offset == 0) {
5607f967c01SBen Hutchings 		type = "";
5617f967c01SBen Hutchings 	} else if (channel->channel < efx->tx_channel_offset) {
5627f967c01SBen Hutchings 		type = "-rx";
5637f967c01SBen Hutchings 	} else {
5647f967c01SBen Hutchings 		type = "-tx";
5657f967c01SBen Hutchings 		number -= efx->tx_channel_offset;
5667f967c01SBen Hutchings 	}
5677f967c01SBen Hutchings 	snprintf(buf, len, "%s%s-%d", efx->name, type, number);
5687f967c01SBen Hutchings }
569874aeea5SJeff Kirsher 
570874aeea5SJeff Kirsher static void efx_set_channel_names(struct efx_nic *efx)
571874aeea5SJeff Kirsher {
572874aeea5SJeff Kirsher 	struct efx_channel *channel;
573874aeea5SJeff Kirsher 
5747f967c01SBen Hutchings 	efx_for_each_channel(channel, efx)
5757f967c01SBen Hutchings 		channel->type->get_name(channel,
5767f967c01SBen Hutchings 					efx->channel_name[channel->channel],
5777f967c01SBen Hutchings 					sizeof(efx->channel_name[0]));
578874aeea5SJeff Kirsher }
579874aeea5SJeff Kirsher 
580874aeea5SJeff Kirsher static int efx_probe_channels(struct efx_nic *efx)
581874aeea5SJeff Kirsher {
582874aeea5SJeff Kirsher 	struct efx_channel *channel;
583874aeea5SJeff Kirsher 	int rc;
584874aeea5SJeff Kirsher 
585874aeea5SJeff Kirsher 	/* Restart special buffer allocation */
586874aeea5SJeff Kirsher 	efx->next_buffer_table = 0;
587874aeea5SJeff Kirsher 
588874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx) {
589874aeea5SJeff Kirsher 		rc = efx_probe_channel(channel);
590874aeea5SJeff Kirsher 		if (rc) {
591874aeea5SJeff Kirsher 			netif_err(efx, probe, efx->net_dev,
592874aeea5SJeff Kirsher 				  "failed to create channel %d\n",
593874aeea5SJeff Kirsher 				  channel->channel);
594874aeea5SJeff Kirsher 			goto fail;
595874aeea5SJeff Kirsher 		}
596874aeea5SJeff Kirsher 	}
597874aeea5SJeff Kirsher 	efx_set_channel_names(efx);
598874aeea5SJeff Kirsher 
599874aeea5SJeff Kirsher 	return 0;
600874aeea5SJeff Kirsher 
601874aeea5SJeff Kirsher fail:
602874aeea5SJeff Kirsher 	efx_remove_channels(efx);
603874aeea5SJeff Kirsher 	return rc;
604874aeea5SJeff Kirsher }
605874aeea5SJeff Kirsher 
606874aeea5SJeff Kirsher /* Channels are shutdown and reinitialised whilst the NIC is running
607874aeea5SJeff Kirsher  * to propagate configuration changes (mtu, checksum offload), or
608874aeea5SJeff Kirsher  * to clear hardware error conditions
609874aeea5SJeff Kirsher  */
6109f2cb71cSBen Hutchings static void efx_start_datapath(struct efx_nic *efx)
611874aeea5SJeff Kirsher {
612874aeea5SJeff Kirsher 	struct efx_tx_queue *tx_queue;
613874aeea5SJeff Kirsher 	struct efx_rx_queue *rx_queue;
614874aeea5SJeff Kirsher 	struct efx_channel *channel;
615874aeea5SJeff Kirsher 
616874aeea5SJeff Kirsher 	/* Calculate the rx buffer allocation parameters required to
617874aeea5SJeff Kirsher 	 * support the current MTU, including padding for header
618874aeea5SJeff Kirsher 	 * alignment and overruns.
619874aeea5SJeff Kirsher 	 */
620874aeea5SJeff Kirsher 	efx->rx_buffer_len = (max(EFX_PAGE_IP_ALIGN, NET_IP_ALIGN) +
621874aeea5SJeff Kirsher 			      EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
622874aeea5SJeff Kirsher 			      efx->type->rx_buffer_hash_size +
623874aeea5SJeff Kirsher 			      efx->type->rx_buffer_padding);
624874aeea5SJeff Kirsher 	efx->rx_buffer_order = get_order(efx->rx_buffer_len +
625874aeea5SJeff Kirsher 					 sizeof(struct efx_rx_page_state));
626874aeea5SJeff Kirsher 
627874aeea5SJeff Kirsher 	/* Initialise the channels */
628874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx) {
629874aeea5SJeff Kirsher 		efx_for_each_channel_tx_queue(tx_queue, channel)
630874aeea5SJeff Kirsher 			efx_init_tx_queue(tx_queue);
631874aeea5SJeff Kirsher 
632874aeea5SJeff Kirsher 		/* The rx buffer allocation strategy is MTU dependent */
633874aeea5SJeff Kirsher 		efx_rx_strategy(channel);
634874aeea5SJeff Kirsher 
6359f2cb71cSBen Hutchings 		efx_for_each_channel_rx_queue(rx_queue, channel) {
636874aeea5SJeff Kirsher 			efx_init_rx_queue(rx_queue);
6379f2cb71cSBen Hutchings 			efx_nic_generate_fill_event(rx_queue);
6389f2cb71cSBen Hutchings 		}
639874aeea5SJeff Kirsher 
640874aeea5SJeff Kirsher 		WARN_ON(channel->rx_pkt != NULL);
641874aeea5SJeff Kirsher 		efx_rx_strategy(channel);
642874aeea5SJeff Kirsher 	}
6439f2cb71cSBen Hutchings 
6449f2cb71cSBen Hutchings 	if (netif_device_present(efx->net_dev))
6459f2cb71cSBen Hutchings 		netif_tx_wake_all_queues(efx->net_dev);
646874aeea5SJeff Kirsher }
647874aeea5SJeff Kirsher 
6489f2cb71cSBen Hutchings static void efx_stop_datapath(struct efx_nic *efx)
649874aeea5SJeff Kirsher {
650874aeea5SJeff Kirsher 	struct efx_channel *channel;
651874aeea5SJeff Kirsher 	struct efx_tx_queue *tx_queue;
652874aeea5SJeff Kirsher 	struct efx_rx_queue *rx_queue;
653874aeea5SJeff Kirsher 	int rc;
654874aeea5SJeff Kirsher 
655874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
656874aeea5SJeff Kirsher 	BUG_ON(efx->port_enabled);
657874aeea5SJeff Kirsher 
658874aeea5SJeff Kirsher 	rc = efx_nic_flush_queues(efx);
659874aeea5SJeff Kirsher 	if (rc && EFX_WORKAROUND_7803(efx)) {
660874aeea5SJeff Kirsher 		/* Schedule a reset to recover from the flush failure. The
661874aeea5SJeff Kirsher 		 * descriptor caches reference memory we're about to free,
662874aeea5SJeff Kirsher 		 * but falcon_reconfigure_mac_wrapper() won't reconnect
663874aeea5SJeff Kirsher 		 * the MACs because of the pending reset. */
664874aeea5SJeff Kirsher 		netif_err(efx, drv, efx->net_dev,
665874aeea5SJeff Kirsher 			  "Resetting to recover from flush failure\n");
666874aeea5SJeff Kirsher 		efx_schedule_reset(efx, RESET_TYPE_ALL);
667874aeea5SJeff Kirsher 	} else if (rc) {
668874aeea5SJeff Kirsher 		netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
669874aeea5SJeff Kirsher 	} else {
670874aeea5SJeff Kirsher 		netif_dbg(efx, drv, efx->net_dev,
671874aeea5SJeff Kirsher 			  "successfully flushed all queues\n");
672874aeea5SJeff Kirsher 	}
673874aeea5SJeff Kirsher 
674874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx) {
6759f2cb71cSBen Hutchings 		/* RX packet processing is pipelined, so wait for the
6769f2cb71cSBen Hutchings 		 * NAPI handler to complete.  At least event queue 0
6779f2cb71cSBen Hutchings 		 * might be kept active by non-data events, so don't
6789f2cb71cSBen Hutchings 		 * use napi_synchronize() but actually disable NAPI
6799f2cb71cSBen Hutchings 		 * temporarily.
6809f2cb71cSBen Hutchings 		 */
6819f2cb71cSBen Hutchings 		if (efx_channel_has_rx_queue(channel)) {
6829f2cb71cSBen Hutchings 			efx_stop_eventq(channel);
6839f2cb71cSBen Hutchings 			efx_start_eventq(channel);
6849f2cb71cSBen Hutchings 		}
685874aeea5SJeff Kirsher 
686874aeea5SJeff Kirsher 		efx_for_each_channel_rx_queue(rx_queue, channel)
687874aeea5SJeff Kirsher 			efx_fini_rx_queue(rx_queue);
688874aeea5SJeff Kirsher 		efx_for_each_possible_channel_tx_queue(tx_queue, channel)
689874aeea5SJeff Kirsher 			efx_fini_tx_queue(tx_queue);
690874aeea5SJeff Kirsher 	}
691874aeea5SJeff Kirsher }
692874aeea5SJeff Kirsher 
693874aeea5SJeff Kirsher static void efx_remove_channel(struct efx_channel *channel)
694874aeea5SJeff Kirsher {
695874aeea5SJeff Kirsher 	struct efx_tx_queue *tx_queue;
696874aeea5SJeff Kirsher 	struct efx_rx_queue *rx_queue;
697874aeea5SJeff Kirsher 
698874aeea5SJeff Kirsher 	netif_dbg(channel->efx, drv, channel->efx->net_dev,
699874aeea5SJeff Kirsher 		  "destroy chan %d\n", channel->channel);
700874aeea5SJeff Kirsher 
701874aeea5SJeff Kirsher 	efx_for_each_channel_rx_queue(rx_queue, channel)
702874aeea5SJeff Kirsher 		efx_remove_rx_queue(rx_queue);
703874aeea5SJeff Kirsher 	efx_for_each_possible_channel_tx_queue(tx_queue, channel)
704874aeea5SJeff Kirsher 		efx_remove_tx_queue(tx_queue);
705874aeea5SJeff Kirsher 	efx_remove_eventq(channel);
706874aeea5SJeff Kirsher }
707874aeea5SJeff Kirsher 
708874aeea5SJeff Kirsher static void efx_remove_channels(struct efx_nic *efx)
709874aeea5SJeff Kirsher {
710874aeea5SJeff Kirsher 	struct efx_channel *channel;
711874aeea5SJeff Kirsher 
712874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx)
713874aeea5SJeff Kirsher 		efx_remove_channel(channel);
714874aeea5SJeff Kirsher }
715874aeea5SJeff Kirsher 
716874aeea5SJeff Kirsher int
717874aeea5SJeff Kirsher efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
718874aeea5SJeff Kirsher {
719874aeea5SJeff Kirsher 	struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
720874aeea5SJeff Kirsher 	u32 old_rxq_entries, old_txq_entries;
7217f967c01SBen Hutchings 	unsigned i, next_buffer_table = 0;
7227f967c01SBen Hutchings 	int rc = 0;
7237f967c01SBen Hutchings 
7247f967c01SBen Hutchings 	/* Not all channels should be reallocated. We must avoid
7257f967c01SBen Hutchings 	 * reallocating their buffer table entries.
7267f967c01SBen Hutchings 	 */
7277f967c01SBen Hutchings 	efx_for_each_channel(channel, efx) {
7287f967c01SBen Hutchings 		struct efx_rx_queue *rx_queue;
7297f967c01SBen Hutchings 		struct efx_tx_queue *tx_queue;
7307f967c01SBen Hutchings 
7317f967c01SBen Hutchings 		if (channel->type->copy)
7327f967c01SBen Hutchings 			continue;
7337f967c01SBen Hutchings 		next_buffer_table = max(next_buffer_table,
7347f967c01SBen Hutchings 					channel->eventq.index +
7357f967c01SBen Hutchings 					channel->eventq.entries);
7367f967c01SBen Hutchings 		efx_for_each_channel_rx_queue(rx_queue, channel)
7377f967c01SBen Hutchings 			next_buffer_table = max(next_buffer_table,
7387f967c01SBen Hutchings 						rx_queue->rxd.index +
7397f967c01SBen Hutchings 						rx_queue->rxd.entries);
7407f967c01SBen Hutchings 		efx_for_each_channel_tx_queue(tx_queue, channel)
7417f967c01SBen Hutchings 			next_buffer_table = max(next_buffer_table,
7427f967c01SBen Hutchings 						tx_queue->txd.index +
7437f967c01SBen Hutchings 						tx_queue->txd.entries);
7447f967c01SBen Hutchings 	}
745874aeea5SJeff Kirsher 
746874aeea5SJeff Kirsher 	efx_stop_all(efx);
7477f967c01SBen Hutchings 	efx_stop_interrupts(efx, true);
748874aeea5SJeff Kirsher 
7497f967c01SBen Hutchings 	/* Clone channels (where possible) */
750874aeea5SJeff Kirsher 	memset(other_channel, 0, sizeof(other_channel));
751874aeea5SJeff Kirsher 	for (i = 0; i < efx->n_channels; i++) {
7527f967c01SBen Hutchings 		channel = efx->channel[i];
7537f967c01SBen Hutchings 		if (channel->type->copy)
7547f967c01SBen Hutchings 			channel = channel->type->copy(channel);
755874aeea5SJeff Kirsher 		if (!channel) {
756874aeea5SJeff Kirsher 			rc = -ENOMEM;
757874aeea5SJeff Kirsher 			goto out;
758874aeea5SJeff Kirsher 		}
759874aeea5SJeff Kirsher 		other_channel[i] = channel;
760874aeea5SJeff Kirsher 	}
761874aeea5SJeff Kirsher 
762874aeea5SJeff Kirsher 	/* Swap entry counts and channel pointers */
763874aeea5SJeff Kirsher 	old_rxq_entries = efx->rxq_entries;
764874aeea5SJeff Kirsher 	old_txq_entries = efx->txq_entries;
765874aeea5SJeff Kirsher 	efx->rxq_entries = rxq_entries;
766874aeea5SJeff Kirsher 	efx->txq_entries = txq_entries;
767874aeea5SJeff Kirsher 	for (i = 0; i < efx->n_channels; i++) {
768874aeea5SJeff Kirsher 		channel = efx->channel[i];
769874aeea5SJeff Kirsher 		efx->channel[i] = other_channel[i];
770874aeea5SJeff Kirsher 		other_channel[i] = channel;
771874aeea5SJeff Kirsher 	}
772874aeea5SJeff Kirsher 
7737f967c01SBen Hutchings 	/* Restart buffer table allocation */
7747f967c01SBen Hutchings 	efx->next_buffer_table = next_buffer_table;
7757f967c01SBen Hutchings 
7767f967c01SBen Hutchings 	for (i = 0; i < efx->n_channels; i++) {
7777f967c01SBen Hutchings 		channel = efx->channel[i];
7787f967c01SBen Hutchings 		if (!channel->type->copy)
7797f967c01SBen Hutchings 			continue;
7807f967c01SBen Hutchings 		rc = efx_probe_channel(channel);
781874aeea5SJeff Kirsher 		if (rc)
782874aeea5SJeff Kirsher 			goto rollback;
7837f967c01SBen Hutchings 		efx_init_napi_channel(efx->channel[i]);
784874aeea5SJeff Kirsher 	}
785874aeea5SJeff Kirsher 
7867f967c01SBen Hutchings out:
7877f967c01SBen Hutchings 	/* Destroy unused channel structures */
7887f967c01SBen Hutchings 	for (i = 0; i < efx->n_channels; i++) {
7897f967c01SBen Hutchings 		channel = other_channel[i];
7907f967c01SBen Hutchings 		if (channel && channel->type->copy) {
7917f967c01SBen Hutchings 			efx_fini_napi_channel(channel);
7927f967c01SBen Hutchings 			efx_remove_channel(channel);
7937f967c01SBen Hutchings 			kfree(channel);
7947f967c01SBen Hutchings 		}
7957f967c01SBen Hutchings 	}
7967f967c01SBen Hutchings 
7977f967c01SBen Hutchings 	efx_start_interrupts(efx, true);
798874aeea5SJeff Kirsher 	efx_start_all(efx);
799874aeea5SJeff Kirsher 	return rc;
800874aeea5SJeff Kirsher 
801874aeea5SJeff Kirsher rollback:
802874aeea5SJeff Kirsher 	/* Swap back */
803874aeea5SJeff Kirsher 	efx->rxq_entries = old_rxq_entries;
804874aeea5SJeff Kirsher 	efx->txq_entries = old_txq_entries;
805874aeea5SJeff Kirsher 	for (i = 0; i < efx->n_channels; i++) {
806874aeea5SJeff Kirsher 		channel = efx->channel[i];
807874aeea5SJeff Kirsher 		efx->channel[i] = other_channel[i];
808874aeea5SJeff Kirsher 		other_channel[i] = channel;
809874aeea5SJeff Kirsher 	}
810874aeea5SJeff Kirsher 	goto out;
811874aeea5SJeff Kirsher }
812874aeea5SJeff Kirsher 
813874aeea5SJeff Kirsher void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
814874aeea5SJeff Kirsher {
815874aeea5SJeff Kirsher 	mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
816874aeea5SJeff Kirsher }
817874aeea5SJeff Kirsher 
8187f967c01SBen Hutchings static const struct efx_channel_type efx_default_channel_type = {
8197f967c01SBen Hutchings 	.pre_probe		= efx_channel_dummy_op_int,
8207f967c01SBen Hutchings 	.get_name		= efx_get_channel_name,
8217f967c01SBen Hutchings 	.copy			= efx_copy_channel,
8227f967c01SBen Hutchings 	.keep_eventq		= false,
8237f967c01SBen Hutchings };
8247f967c01SBen Hutchings 
8257f967c01SBen Hutchings int efx_channel_dummy_op_int(struct efx_channel *channel)
8267f967c01SBen Hutchings {
8277f967c01SBen Hutchings 	return 0;
8287f967c01SBen Hutchings }
8297f967c01SBen Hutchings 
830874aeea5SJeff Kirsher /**************************************************************************
831874aeea5SJeff Kirsher  *
832874aeea5SJeff Kirsher  * Port handling
833874aeea5SJeff Kirsher  *
834874aeea5SJeff Kirsher  **************************************************************************/
835874aeea5SJeff Kirsher 
836874aeea5SJeff Kirsher /* This ensures that the kernel is kept informed (via
837874aeea5SJeff Kirsher  * netif_carrier_on/off) of the link status, and also maintains the
838874aeea5SJeff Kirsher  * link status's stop on the port's TX queue.
839874aeea5SJeff Kirsher  */
840874aeea5SJeff Kirsher void efx_link_status_changed(struct efx_nic *efx)
841874aeea5SJeff Kirsher {
842874aeea5SJeff Kirsher 	struct efx_link_state *link_state = &efx->link_state;
843874aeea5SJeff Kirsher 
844874aeea5SJeff Kirsher 	/* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
845874aeea5SJeff Kirsher 	 * that no events are triggered between unregister_netdev() and the
846874aeea5SJeff Kirsher 	 * driver unloading. A more general condition is that NETDEV_CHANGE
847874aeea5SJeff Kirsher 	 * can only be generated between NETDEV_UP and NETDEV_DOWN */
848874aeea5SJeff Kirsher 	if (!netif_running(efx->net_dev))
849874aeea5SJeff Kirsher 		return;
850874aeea5SJeff Kirsher 
851874aeea5SJeff Kirsher 	if (link_state->up != netif_carrier_ok(efx->net_dev)) {
852874aeea5SJeff Kirsher 		efx->n_link_state_changes++;
853874aeea5SJeff Kirsher 
854874aeea5SJeff Kirsher 		if (link_state->up)
855874aeea5SJeff Kirsher 			netif_carrier_on(efx->net_dev);
856874aeea5SJeff Kirsher 		else
857874aeea5SJeff Kirsher 			netif_carrier_off(efx->net_dev);
858874aeea5SJeff Kirsher 	}
859874aeea5SJeff Kirsher 
860874aeea5SJeff Kirsher 	/* Status message for kernel log */
8612aa9ef11SBen Hutchings 	if (link_state->up)
862874aeea5SJeff Kirsher 		netif_info(efx, link, efx->net_dev,
863874aeea5SJeff Kirsher 			   "link up at %uMbps %s-duplex (MTU %d)%s\n",
864874aeea5SJeff Kirsher 			   link_state->speed, link_state->fd ? "full" : "half",
865874aeea5SJeff Kirsher 			   efx->net_dev->mtu,
866874aeea5SJeff Kirsher 			   (efx->promiscuous ? " [PROMISC]" : ""));
8672aa9ef11SBen Hutchings 	else
868874aeea5SJeff Kirsher 		netif_info(efx, link, efx->net_dev, "link down\n");
869874aeea5SJeff Kirsher }
870874aeea5SJeff Kirsher 
871874aeea5SJeff Kirsher void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
872874aeea5SJeff Kirsher {
873874aeea5SJeff Kirsher 	efx->link_advertising = advertising;
874874aeea5SJeff Kirsher 	if (advertising) {
875874aeea5SJeff Kirsher 		if (advertising & ADVERTISED_Pause)
876874aeea5SJeff Kirsher 			efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
877874aeea5SJeff Kirsher 		else
878874aeea5SJeff Kirsher 			efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
879874aeea5SJeff Kirsher 		if (advertising & ADVERTISED_Asym_Pause)
880874aeea5SJeff Kirsher 			efx->wanted_fc ^= EFX_FC_TX;
881874aeea5SJeff Kirsher 	}
882874aeea5SJeff Kirsher }
883874aeea5SJeff Kirsher 
884874aeea5SJeff Kirsher void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
885874aeea5SJeff Kirsher {
886874aeea5SJeff Kirsher 	efx->wanted_fc = wanted_fc;
887874aeea5SJeff Kirsher 	if (efx->link_advertising) {
888874aeea5SJeff Kirsher 		if (wanted_fc & EFX_FC_RX)
889874aeea5SJeff Kirsher 			efx->link_advertising |= (ADVERTISED_Pause |
890874aeea5SJeff Kirsher 						  ADVERTISED_Asym_Pause);
891874aeea5SJeff Kirsher 		else
892874aeea5SJeff Kirsher 			efx->link_advertising &= ~(ADVERTISED_Pause |
893874aeea5SJeff Kirsher 						   ADVERTISED_Asym_Pause);
894874aeea5SJeff Kirsher 		if (wanted_fc & EFX_FC_TX)
895874aeea5SJeff Kirsher 			efx->link_advertising ^= ADVERTISED_Asym_Pause;
896874aeea5SJeff Kirsher 	}
897874aeea5SJeff Kirsher }
898874aeea5SJeff Kirsher 
899874aeea5SJeff Kirsher static void efx_fini_port(struct efx_nic *efx);
900874aeea5SJeff Kirsher 
901874aeea5SJeff Kirsher /* Push loopback/power/transmit disable settings to the PHY, and reconfigure
902874aeea5SJeff Kirsher  * the MAC appropriately. All other PHY configuration changes are pushed
903874aeea5SJeff Kirsher  * through phy_op->set_settings(), and pushed asynchronously to the MAC
904874aeea5SJeff Kirsher  * through efx_monitor().
905874aeea5SJeff Kirsher  *
906874aeea5SJeff Kirsher  * Callers must hold the mac_lock
907874aeea5SJeff Kirsher  */
908874aeea5SJeff Kirsher int __efx_reconfigure_port(struct efx_nic *efx)
909874aeea5SJeff Kirsher {
910874aeea5SJeff Kirsher 	enum efx_phy_mode phy_mode;
911874aeea5SJeff Kirsher 	int rc;
912874aeea5SJeff Kirsher 
913874aeea5SJeff Kirsher 	WARN_ON(!mutex_is_locked(&efx->mac_lock));
914874aeea5SJeff Kirsher 
9150fca8c97SBen Hutchings 	/* Serialise the promiscuous flag with efx_set_rx_mode. */
916874aeea5SJeff Kirsher 	netif_addr_lock_bh(efx->net_dev);
917874aeea5SJeff Kirsher 	netif_addr_unlock_bh(efx->net_dev);
918874aeea5SJeff Kirsher 
919874aeea5SJeff Kirsher 	/* Disable PHY transmit in mac level loopbacks */
920874aeea5SJeff Kirsher 	phy_mode = efx->phy_mode;
921874aeea5SJeff Kirsher 	if (LOOPBACK_INTERNAL(efx))
922874aeea5SJeff Kirsher 		efx->phy_mode |= PHY_MODE_TX_DISABLED;
923874aeea5SJeff Kirsher 	else
924874aeea5SJeff Kirsher 		efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
925874aeea5SJeff Kirsher 
926874aeea5SJeff Kirsher 	rc = efx->type->reconfigure_port(efx);
927874aeea5SJeff Kirsher 
928874aeea5SJeff Kirsher 	if (rc)
929874aeea5SJeff Kirsher 		efx->phy_mode = phy_mode;
930874aeea5SJeff Kirsher 
931874aeea5SJeff Kirsher 	return rc;
932874aeea5SJeff Kirsher }
933874aeea5SJeff Kirsher 
934874aeea5SJeff Kirsher /* Reinitialise the MAC to pick up new PHY settings, even if the port is
935874aeea5SJeff Kirsher  * disabled. */
936874aeea5SJeff Kirsher int efx_reconfigure_port(struct efx_nic *efx)
937874aeea5SJeff Kirsher {
938874aeea5SJeff Kirsher 	int rc;
939874aeea5SJeff Kirsher 
940874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
941874aeea5SJeff Kirsher 
942874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
943874aeea5SJeff Kirsher 	rc = __efx_reconfigure_port(efx);
944874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
945874aeea5SJeff Kirsher 
946874aeea5SJeff Kirsher 	return rc;
947874aeea5SJeff Kirsher }
948874aeea5SJeff Kirsher 
949874aeea5SJeff Kirsher /* Asynchronous work item for changing MAC promiscuity and multicast
950874aeea5SJeff Kirsher  * hash.  Avoid a drain/rx_ingress enable by reconfiguring the current
951874aeea5SJeff Kirsher  * MAC directly. */
952874aeea5SJeff Kirsher static void efx_mac_work(struct work_struct *data)
953874aeea5SJeff Kirsher {
954874aeea5SJeff Kirsher 	struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
955874aeea5SJeff Kirsher 
956874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
95730b81cdaSBen Hutchings 	if (efx->port_enabled)
958710b208dSBen Hutchings 		efx->type->reconfigure_mac(efx);
959874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
960874aeea5SJeff Kirsher }
961874aeea5SJeff Kirsher 
962874aeea5SJeff Kirsher static int efx_probe_port(struct efx_nic *efx)
963874aeea5SJeff Kirsher {
964874aeea5SJeff Kirsher 	int rc;
965874aeea5SJeff Kirsher 
966874aeea5SJeff Kirsher 	netif_dbg(efx, probe, efx->net_dev, "create port\n");
967874aeea5SJeff Kirsher 
968874aeea5SJeff Kirsher 	if (phy_flash_cfg)
969874aeea5SJeff Kirsher 		efx->phy_mode = PHY_MODE_SPECIAL;
970874aeea5SJeff Kirsher 
971874aeea5SJeff Kirsher 	/* Connect up MAC/PHY operations table */
972874aeea5SJeff Kirsher 	rc = efx->type->probe_port(efx);
973874aeea5SJeff Kirsher 	if (rc)
974874aeea5SJeff Kirsher 		return rc;
975874aeea5SJeff Kirsher 
976e332bcb3SBen Hutchings 	/* Initialise MAC address to permanent address */
977e332bcb3SBen Hutchings 	memcpy(efx->net_dev->dev_addr, efx->net_dev->perm_addr, ETH_ALEN);
978874aeea5SJeff Kirsher 
979874aeea5SJeff Kirsher 	return 0;
980874aeea5SJeff Kirsher }
981874aeea5SJeff Kirsher 
982874aeea5SJeff Kirsher static int efx_init_port(struct efx_nic *efx)
983874aeea5SJeff Kirsher {
984874aeea5SJeff Kirsher 	int rc;
985874aeea5SJeff Kirsher 
986874aeea5SJeff Kirsher 	netif_dbg(efx, drv, efx->net_dev, "init port\n");
987874aeea5SJeff Kirsher 
988874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
989874aeea5SJeff Kirsher 
990874aeea5SJeff Kirsher 	rc = efx->phy_op->init(efx);
991874aeea5SJeff Kirsher 	if (rc)
992874aeea5SJeff Kirsher 		goto fail1;
993874aeea5SJeff Kirsher 
994874aeea5SJeff Kirsher 	efx->port_initialized = true;
995874aeea5SJeff Kirsher 
996874aeea5SJeff Kirsher 	/* Reconfigure the MAC before creating dma queues (required for
997874aeea5SJeff Kirsher 	 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
998710b208dSBen Hutchings 	efx->type->reconfigure_mac(efx);
999874aeea5SJeff Kirsher 
1000874aeea5SJeff Kirsher 	/* Ensure the PHY advertises the correct flow control settings */
1001874aeea5SJeff Kirsher 	rc = efx->phy_op->reconfigure(efx);
1002874aeea5SJeff Kirsher 	if (rc)
1003874aeea5SJeff Kirsher 		goto fail2;
1004874aeea5SJeff Kirsher 
1005874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
1006874aeea5SJeff Kirsher 	return 0;
1007874aeea5SJeff Kirsher 
1008874aeea5SJeff Kirsher fail2:
1009874aeea5SJeff Kirsher 	efx->phy_op->fini(efx);
1010874aeea5SJeff Kirsher fail1:
1011874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
1012874aeea5SJeff Kirsher 	return rc;
1013874aeea5SJeff Kirsher }
1014874aeea5SJeff Kirsher 
1015874aeea5SJeff Kirsher static void efx_start_port(struct efx_nic *efx)
1016874aeea5SJeff Kirsher {
1017874aeea5SJeff Kirsher 	netif_dbg(efx, ifup, efx->net_dev, "start port\n");
1018874aeea5SJeff Kirsher 	BUG_ON(efx->port_enabled);
1019874aeea5SJeff Kirsher 
1020874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
1021874aeea5SJeff Kirsher 	efx->port_enabled = true;
1022874aeea5SJeff Kirsher 
1023874aeea5SJeff Kirsher 	/* efx_mac_work() might have been scheduled after efx_stop_port(),
1024874aeea5SJeff Kirsher 	 * and then cancelled by efx_flush_all() */
1025710b208dSBen Hutchings 	efx->type->reconfigure_mac(efx);
1026874aeea5SJeff Kirsher 
1027874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
1028874aeea5SJeff Kirsher }
1029874aeea5SJeff Kirsher 
1030874aeea5SJeff Kirsher /* Prevent efx_mac_work() and efx_monitor() from working */
1031874aeea5SJeff Kirsher static void efx_stop_port(struct efx_nic *efx)
1032874aeea5SJeff Kirsher {
1033874aeea5SJeff Kirsher 	netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
1034874aeea5SJeff Kirsher 
1035874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
1036874aeea5SJeff Kirsher 	efx->port_enabled = false;
1037874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
1038874aeea5SJeff Kirsher 
1039874aeea5SJeff Kirsher 	/* Serialise against efx_set_multicast_list() */
1040874aeea5SJeff Kirsher 	netif_addr_lock_bh(efx->net_dev);
1041874aeea5SJeff Kirsher 	netif_addr_unlock_bh(efx->net_dev);
1042874aeea5SJeff Kirsher }
1043874aeea5SJeff Kirsher 
1044874aeea5SJeff Kirsher static void efx_fini_port(struct efx_nic *efx)
1045874aeea5SJeff Kirsher {
1046874aeea5SJeff Kirsher 	netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
1047874aeea5SJeff Kirsher 
1048874aeea5SJeff Kirsher 	if (!efx->port_initialized)
1049874aeea5SJeff Kirsher 		return;
1050874aeea5SJeff Kirsher 
1051874aeea5SJeff Kirsher 	efx->phy_op->fini(efx);
1052874aeea5SJeff Kirsher 	efx->port_initialized = false;
1053874aeea5SJeff Kirsher 
1054874aeea5SJeff Kirsher 	efx->link_state.up = false;
1055874aeea5SJeff Kirsher 	efx_link_status_changed(efx);
1056874aeea5SJeff Kirsher }
1057874aeea5SJeff Kirsher 
1058874aeea5SJeff Kirsher static void efx_remove_port(struct efx_nic *efx)
1059874aeea5SJeff Kirsher {
1060874aeea5SJeff Kirsher 	netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
1061874aeea5SJeff Kirsher 
1062874aeea5SJeff Kirsher 	efx->type->remove_port(efx);
1063874aeea5SJeff Kirsher }
1064874aeea5SJeff Kirsher 
1065874aeea5SJeff Kirsher /**************************************************************************
1066874aeea5SJeff Kirsher  *
1067874aeea5SJeff Kirsher  * NIC handling
1068874aeea5SJeff Kirsher  *
1069874aeea5SJeff Kirsher  **************************************************************************/
1070874aeea5SJeff Kirsher 
1071874aeea5SJeff Kirsher /* This configures the PCI device to enable I/O and DMA. */
1072874aeea5SJeff Kirsher static int efx_init_io(struct efx_nic *efx)
1073874aeea5SJeff Kirsher {
1074874aeea5SJeff Kirsher 	struct pci_dev *pci_dev = efx->pci_dev;
1075874aeea5SJeff Kirsher 	dma_addr_t dma_mask = efx->type->max_dma_mask;
1076874aeea5SJeff Kirsher 	int rc;
1077874aeea5SJeff Kirsher 
1078874aeea5SJeff Kirsher 	netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
1079874aeea5SJeff Kirsher 
1080874aeea5SJeff Kirsher 	rc = pci_enable_device(pci_dev);
1081874aeea5SJeff Kirsher 	if (rc) {
1082874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev,
1083874aeea5SJeff Kirsher 			  "failed to enable PCI device\n");
1084874aeea5SJeff Kirsher 		goto fail1;
1085874aeea5SJeff Kirsher 	}
1086874aeea5SJeff Kirsher 
1087874aeea5SJeff Kirsher 	pci_set_master(pci_dev);
1088874aeea5SJeff Kirsher 
1089874aeea5SJeff Kirsher 	/* Set the PCI DMA mask.  Try all possibilities from our
1090874aeea5SJeff Kirsher 	 * genuine mask down to 32 bits, because some architectures
1091874aeea5SJeff Kirsher 	 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
1092874aeea5SJeff Kirsher 	 * masks event though they reject 46 bit masks.
1093874aeea5SJeff Kirsher 	 */
1094874aeea5SJeff Kirsher 	while (dma_mask > 0x7fffffffUL) {
1095e9e01846SBen Hutchings 		if (pci_dma_supported(pci_dev, dma_mask)) {
1096e9e01846SBen Hutchings 			rc = pci_set_dma_mask(pci_dev, dma_mask);
1097e9e01846SBen Hutchings 			if (rc == 0)
1098874aeea5SJeff Kirsher 				break;
1099e9e01846SBen Hutchings 		}
1100874aeea5SJeff Kirsher 		dma_mask >>= 1;
1101874aeea5SJeff Kirsher 	}
1102874aeea5SJeff Kirsher 	if (rc) {
1103874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev,
1104874aeea5SJeff Kirsher 			  "could not find a suitable DMA mask\n");
1105874aeea5SJeff Kirsher 		goto fail2;
1106874aeea5SJeff Kirsher 	}
1107874aeea5SJeff Kirsher 	netif_dbg(efx, probe, efx->net_dev,
1108874aeea5SJeff Kirsher 		  "using DMA mask %llx\n", (unsigned long long) dma_mask);
1109874aeea5SJeff Kirsher 	rc = pci_set_consistent_dma_mask(pci_dev, dma_mask);
1110874aeea5SJeff Kirsher 	if (rc) {
1111874aeea5SJeff Kirsher 		/* pci_set_consistent_dma_mask() is not *allowed* to
1112874aeea5SJeff Kirsher 		 * fail with a mask that pci_set_dma_mask() accepted,
1113874aeea5SJeff Kirsher 		 * but just in case...
1114874aeea5SJeff Kirsher 		 */
1115874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev,
1116874aeea5SJeff Kirsher 			  "failed to set consistent DMA mask\n");
1117874aeea5SJeff Kirsher 		goto fail2;
1118874aeea5SJeff Kirsher 	}
1119874aeea5SJeff Kirsher 
1120874aeea5SJeff Kirsher 	efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
1121874aeea5SJeff Kirsher 	rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
1122874aeea5SJeff Kirsher 	if (rc) {
1123874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev,
1124874aeea5SJeff Kirsher 			  "request for memory BAR failed\n");
1125874aeea5SJeff Kirsher 		rc = -EIO;
1126874aeea5SJeff Kirsher 		goto fail3;
1127874aeea5SJeff Kirsher 	}
1128874aeea5SJeff Kirsher 	efx->membase = ioremap_nocache(efx->membase_phys,
1129874aeea5SJeff Kirsher 				       efx->type->mem_map_size);
1130874aeea5SJeff Kirsher 	if (!efx->membase) {
1131874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev,
1132874aeea5SJeff Kirsher 			  "could not map memory BAR at %llx+%x\n",
1133874aeea5SJeff Kirsher 			  (unsigned long long)efx->membase_phys,
1134874aeea5SJeff Kirsher 			  efx->type->mem_map_size);
1135874aeea5SJeff Kirsher 		rc = -ENOMEM;
1136874aeea5SJeff Kirsher 		goto fail4;
1137874aeea5SJeff Kirsher 	}
1138874aeea5SJeff Kirsher 	netif_dbg(efx, probe, efx->net_dev,
1139874aeea5SJeff Kirsher 		  "memory BAR at %llx+%x (virtual %p)\n",
1140874aeea5SJeff Kirsher 		  (unsigned long long)efx->membase_phys,
1141874aeea5SJeff Kirsher 		  efx->type->mem_map_size, efx->membase);
1142874aeea5SJeff Kirsher 
1143874aeea5SJeff Kirsher 	return 0;
1144874aeea5SJeff Kirsher 
1145874aeea5SJeff Kirsher  fail4:
1146874aeea5SJeff Kirsher 	pci_release_region(efx->pci_dev, EFX_MEM_BAR);
1147874aeea5SJeff Kirsher  fail3:
1148874aeea5SJeff Kirsher 	efx->membase_phys = 0;
1149874aeea5SJeff Kirsher  fail2:
1150874aeea5SJeff Kirsher 	pci_disable_device(efx->pci_dev);
1151874aeea5SJeff Kirsher  fail1:
1152874aeea5SJeff Kirsher 	return rc;
1153874aeea5SJeff Kirsher }
1154874aeea5SJeff Kirsher 
1155874aeea5SJeff Kirsher static void efx_fini_io(struct efx_nic *efx)
1156874aeea5SJeff Kirsher {
1157874aeea5SJeff Kirsher 	netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
1158874aeea5SJeff Kirsher 
1159874aeea5SJeff Kirsher 	if (efx->membase) {
1160874aeea5SJeff Kirsher 		iounmap(efx->membase);
1161874aeea5SJeff Kirsher 		efx->membase = NULL;
1162874aeea5SJeff Kirsher 	}
1163874aeea5SJeff Kirsher 
1164874aeea5SJeff Kirsher 	if (efx->membase_phys) {
1165874aeea5SJeff Kirsher 		pci_release_region(efx->pci_dev, EFX_MEM_BAR);
1166874aeea5SJeff Kirsher 		efx->membase_phys = 0;
1167874aeea5SJeff Kirsher 	}
1168874aeea5SJeff Kirsher 
1169874aeea5SJeff Kirsher 	pci_disable_device(efx->pci_dev);
1170874aeea5SJeff Kirsher }
1171874aeea5SJeff Kirsher 
1172a9a52506SBen Hutchings static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
1173874aeea5SJeff Kirsher {
1174cdb08f8fSBen Hutchings 	cpumask_var_t thread_mask;
1175a16e5b24SBen Hutchings 	unsigned int count;
1176874aeea5SJeff Kirsher 	int cpu;
1177874aeea5SJeff Kirsher 
1178874aeea5SJeff Kirsher 	if (rss_cpus)
1179874aeea5SJeff Kirsher 		return rss_cpus;
1180874aeea5SJeff Kirsher 
1181cdb08f8fSBen Hutchings 	if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
1182a9a52506SBen Hutchings 		netif_warn(efx, probe, efx->net_dev,
1183a9a52506SBen Hutchings 			   "RSS disabled due to allocation failure\n");
1184874aeea5SJeff Kirsher 		return 1;
1185874aeea5SJeff Kirsher 	}
1186874aeea5SJeff Kirsher 
1187874aeea5SJeff Kirsher 	count = 0;
1188874aeea5SJeff Kirsher 	for_each_online_cpu(cpu) {
1189cdb08f8fSBen Hutchings 		if (!cpumask_test_cpu(cpu, thread_mask)) {
1190874aeea5SJeff Kirsher 			++count;
1191cdb08f8fSBen Hutchings 			cpumask_or(thread_mask, thread_mask,
1192cdb08f8fSBen Hutchings 				   topology_thread_cpumask(cpu));
1193874aeea5SJeff Kirsher 		}
1194874aeea5SJeff Kirsher 	}
1195874aeea5SJeff Kirsher 
1196cdb08f8fSBen Hutchings 	free_cpumask_var(thread_mask);
1197874aeea5SJeff Kirsher 	return count;
1198874aeea5SJeff Kirsher }
1199874aeea5SJeff Kirsher 
1200874aeea5SJeff Kirsher static int
1201874aeea5SJeff Kirsher efx_init_rx_cpu_rmap(struct efx_nic *efx, struct msix_entry *xentries)
1202874aeea5SJeff Kirsher {
1203874aeea5SJeff Kirsher #ifdef CONFIG_RFS_ACCEL
1204a16e5b24SBen Hutchings 	unsigned int i;
1205a16e5b24SBen Hutchings 	int rc;
1206874aeea5SJeff Kirsher 
1207874aeea5SJeff Kirsher 	efx->net_dev->rx_cpu_rmap = alloc_irq_cpu_rmap(efx->n_rx_channels);
1208874aeea5SJeff Kirsher 	if (!efx->net_dev->rx_cpu_rmap)
1209874aeea5SJeff Kirsher 		return -ENOMEM;
1210874aeea5SJeff Kirsher 	for (i = 0; i < efx->n_rx_channels; i++) {
1211874aeea5SJeff Kirsher 		rc = irq_cpu_rmap_add(efx->net_dev->rx_cpu_rmap,
1212874aeea5SJeff Kirsher 				      xentries[i].vector);
1213874aeea5SJeff Kirsher 		if (rc) {
1214874aeea5SJeff Kirsher 			free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
1215874aeea5SJeff Kirsher 			efx->net_dev->rx_cpu_rmap = NULL;
1216874aeea5SJeff Kirsher 			return rc;
1217874aeea5SJeff Kirsher 		}
1218874aeea5SJeff Kirsher 	}
1219874aeea5SJeff Kirsher #endif
1220874aeea5SJeff Kirsher 	return 0;
1221874aeea5SJeff Kirsher }
1222874aeea5SJeff Kirsher 
1223874aeea5SJeff Kirsher /* Probe the number and type of interrupts we are able to obtain, and
1224874aeea5SJeff Kirsher  * the resulting numbers of channels and RX queues.
1225874aeea5SJeff Kirsher  */
1226874aeea5SJeff Kirsher static int efx_probe_interrupts(struct efx_nic *efx)
1227874aeea5SJeff Kirsher {
1228a16e5b24SBen Hutchings 	unsigned int max_channels =
1229a16e5b24SBen Hutchings 		min(efx->type->phys_addr_channels, EFX_MAX_CHANNELS);
12307f967c01SBen Hutchings 	unsigned int extra_channels = 0;
12317f967c01SBen Hutchings 	unsigned int i, j;
1232a16e5b24SBen Hutchings 	int rc;
1233874aeea5SJeff Kirsher 
12347f967c01SBen Hutchings 	for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
12357f967c01SBen Hutchings 		if (efx->extra_channel_type[i])
12367f967c01SBen Hutchings 			++extra_channels;
12377f967c01SBen Hutchings 
1238874aeea5SJeff Kirsher 	if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
1239874aeea5SJeff Kirsher 		struct msix_entry xentries[EFX_MAX_CHANNELS];
1240a16e5b24SBen Hutchings 		unsigned int n_channels;
1241874aeea5SJeff Kirsher 
1242a9a52506SBen Hutchings 		n_channels = efx_wanted_parallelism(efx);
1243874aeea5SJeff Kirsher 		if (separate_tx_channels)
1244874aeea5SJeff Kirsher 			n_channels *= 2;
12457f967c01SBen Hutchings 		n_channels += extra_channels;
1246874aeea5SJeff Kirsher 		n_channels = min(n_channels, max_channels);
1247874aeea5SJeff Kirsher 
1248874aeea5SJeff Kirsher 		for (i = 0; i < n_channels; i++)
1249874aeea5SJeff Kirsher 			xentries[i].entry = i;
1250874aeea5SJeff Kirsher 		rc = pci_enable_msix(efx->pci_dev, xentries, n_channels);
1251874aeea5SJeff Kirsher 		if (rc > 0) {
1252874aeea5SJeff Kirsher 			netif_err(efx, drv, efx->net_dev,
1253874aeea5SJeff Kirsher 				  "WARNING: Insufficient MSI-X vectors"
1254a16e5b24SBen Hutchings 				  " available (%d < %u).\n", rc, n_channels);
1255874aeea5SJeff Kirsher 			netif_err(efx, drv, efx->net_dev,
1256874aeea5SJeff Kirsher 				  "WARNING: Performance may be reduced.\n");
1257874aeea5SJeff Kirsher 			EFX_BUG_ON_PARANOID(rc >= n_channels);
1258874aeea5SJeff Kirsher 			n_channels = rc;
1259874aeea5SJeff Kirsher 			rc = pci_enable_msix(efx->pci_dev, xentries,
1260874aeea5SJeff Kirsher 					     n_channels);
1261874aeea5SJeff Kirsher 		}
1262874aeea5SJeff Kirsher 
1263874aeea5SJeff Kirsher 		if (rc == 0) {
1264874aeea5SJeff Kirsher 			efx->n_channels = n_channels;
12657f967c01SBen Hutchings 			if (n_channels > extra_channels)
12667f967c01SBen Hutchings 				n_channels -= extra_channels;
1267874aeea5SJeff Kirsher 			if (separate_tx_channels) {
12687f967c01SBen Hutchings 				efx->n_tx_channels = max(n_channels / 2, 1U);
12697f967c01SBen Hutchings 				efx->n_rx_channels = max(n_channels -
12707f967c01SBen Hutchings 							 efx->n_tx_channels,
12717f967c01SBen Hutchings 							 1U);
1272874aeea5SJeff Kirsher 			} else {
12737f967c01SBen Hutchings 				efx->n_tx_channels = n_channels;
12747f967c01SBen Hutchings 				efx->n_rx_channels = n_channels;
1275874aeea5SJeff Kirsher 			}
1276874aeea5SJeff Kirsher 			rc = efx_init_rx_cpu_rmap(efx, xentries);
1277874aeea5SJeff Kirsher 			if (rc) {
1278874aeea5SJeff Kirsher 				pci_disable_msix(efx->pci_dev);
1279874aeea5SJeff Kirsher 				return rc;
1280874aeea5SJeff Kirsher 			}
12817f967c01SBen Hutchings 			for (i = 0; i < efx->n_channels; i++)
1282874aeea5SJeff Kirsher 				efx_get_channel(efx, i)->irq =
1283874aeea5SJeff Kirsher 					xentries[i].vector;
1284874aeea5SJeff Kirsher 		} else {
1285874aeea5SJeff Kirsher 			/* Fall back to single channel MSI */
1286874aeea5SJeff Kirsher 			efx->interrupt_mode = EFX_INT_MODE_MSI;
1287874aeea5SJeff Kirsher 			netif_err(efx, drv, efx->net_dev,
1288874aeea5SJeff Kirsher 				  "could not enable MSI-X\n");
1289874aeea5SJeff Kirsher 		}
1290874aeea5SJeff Kirsher 	}
1291874aeea5SJeff Kirsher 
1292874aeea5SJeff Kirsher 	/* Try single interrupt MSI */
1293874aeea5SJeff Kirsher 	if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
1294874aeea5SJeff Kirsher 		efx->n_channels = 1;
1295874aeea5SJeff Kirsher 		efx->n_rx_channels = 1;
1296874aeea5SJeff Kirsher 		efx->n_tx_channels = 1;
1297874aeea5SJeff Kirsher 		rc = pci_enable_msi(efx->pci_dev);
1298874aeea5SJeff Kirsher 		if (rc == 0) {
1299874aeea5SJeff Kirsher 			efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
1300874aeea5SJeff Kirsher 		} else {
1301874aeea5SJeff Kirsher 			netif_err(efx, drv, efx->net_dev,
1302874aeea5SJeff Kirsher 				  "could not enable MSI\n");
1303874aeea5SJeff Kirsher 			efx->interrupt_mode = EFX_INT_MODE_LEGACY;
1304874aeea5SJeff Kirsher 		}
1305874aeea5SJeff Kirsher 	}
1306874aeea5SJeff Kirsher 
1307874aeea5SJeff Kirsher 	/* Assume legacy interrupts */
1308874aeea5SJeff Kirsher 	if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
1309874aeea5SJeff Kirsher 		efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
1310874aeea5SJeff Kirsher 		efx->n_rx_channels = 1;
1311874aeea5SJeff Kirsher 		efx->n_tx_channels = 1;
1312874aeea5SJeff Kirsher 		efx->legacy_irq = efx->pci_dev->irq;
1313874aeea5SJeff Kirsher 	}
1314874aeea5SJeff Kirsher 
13157f967c01SBen Hutchings 	/* Assign extra channels if possible */
13167f967c01SBen Hutchings 	j = efx->n_channels;
13177f967c01SBen Hutchings 	for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
13187f967c01SBen Hutchings 		if (!efx->extra_channel_type[i])
13197f967c01SBen Hutchings 			continue;
13207f967c01SBen Hutchings 		if (efx->interrupt_mode != EFX_INT_MODE_MSIX ||
13217f967c01SBen Hutchings 		    efx->n_channels <= extra_channels) {
13227f967c01SBen Hutchings 			efx->extra_channel_type[i]->handle_no_channel(efx);
13237f967c01SBen Hutchings 		} else {
13247f967c01SBen Hutchings 			--j;
13257f967c01SBen Hutchings 			efx_get_channel(efx, j)->type =
13267f967c01SBen Hutchings 				efx->extra_channel_type[i];
13277f967c01SBen Hutchings 		}
13287f967c01SBen Hutchings 	}
13297f967c01SBen Hutchings 
1330874aeea5SJeff Kirsher 	return 0;
1331874aeea5SJeff Kirsher }
1332874aeea5SJeff Kirsher 
13339f2cb71cSBen Hutchings /* Enable interrupts, then probe and start the event queues */
13347f967c01SBen Hutchings static void efx_start_interrupts(struct efx_nic *efx, bool may_keep_eventq)
13359f2cb71cSBen Hutchings {
13369f2cb71cSBen Hutchings 	struct efx_channel *channel;
13379f2cb71cSBen Hutchings 
13389f2cb71cSBen Hutchings 	if (efx->legacy_irq)
13399f2cb71cSBen Hutchings 		efx->legacy_irq_enabled = true;
13409f2cb71cSBen Hutchings 	efx_nic_enable_interrupts(efx);
13419f2cb71cSBen Hutchings 
13429f2cb71cSBen Hutchings 	efx_for_each_channel(channel, efx) {
13437f967c01SBen Hutchings 		if (!channel->type->keep_eventq || !may_keep_eventq)
13449f2cb71cSBen Hutchings 			efx_init_eventq(channel);
13459f2cb71cSBen Hutchings 		efx_start_eventq(channel);
13469f2cb71cSBen Hutchings 	}
13479f2cb71cSBen Hutchings 
13489f2cb71cSBen Hutchings 	efx_mcdi_mode_event(efx);
13499f2cb71cSBen Hutchings }
13509f2cb71cSBen Hutchings 
13517f967c01SBen Hutchings static void efx_stop_interrupts(struct efx_nic *efx, bool may_keep_eventq)
13529f2cb71cSBen Hutchings {
13539f2cb71cSBen Hutchings 	struct efx_channel *channel;
13549f2cb71cSBen Hutchings 
13559f2cb71cSBen Hutchings 	efx_mcdi_mode_poll(efx);
13569f2cb71cSBen Hutchings 
13579f2cb71cSBen Hutchings 	efx_nic_disable_interrupts(efx);
13589f2cb71cSBen Hutchings 	if (efx->legacy_irq) {
13599f2cb71cSBen Hutchings 		synchronize_irq(efx->legacy_irq);
13609f2cb71cSBen Hutchings 		efx->legacy_irq_enabled = false;
13619f2cb71cSBen Hutchings 	}
13629f2cb71cSBen Hutchings 
13639f2cb71cSBen Hutchings 	efx_for_each_channel(channel, efx) {
13649f2cb71cSBen Hutchings 		if (channel->irq)
13659f2cb71cSBen Hutchings 			synchronize_irq(channel->irq);
13669f2cb71cSBen Hutchings 
13679f2cb71cSBen Hutchings 		efx_stop_eventq(channel);
13687f967c01SBen Hutchings 		if (!channel->type->keep_eventq || !may_keep_eventq)
13699f2cb71cSBen Hutchings 			efx_fini_eventq(channel);
13709f2cb71cSBen Hutchings 	}
13719f2cb71cSBen Hutchings }
13729f2cb71cSBen Hutchings 
1373874aeea5SJeff Kirsher static void efx_remove_interrupts(struct efx_nic *efx)
1374874aeea5SJeff Kirsher {
1375874aeea5SJeff Kirsher 	struct efx_channel *channel;
1376874aeea5SJeff Kirsher 
1377874aeea5SJeff Kirsher 	/* Remove MSI/MSI-X interrupts */
1378874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx)
1379874aeea5SJeff Kirsher 		channel->irq = 0;
1380874aeea5SJeff Kirsher 	pci_disable_msi(efx->pci_dev);
1381874aeea5SJeff Kirsher 	pci_disable_msix(efx->pci_dev);
1382874aeea5SJeff Kirsher 
1383874aeea5SJeff Kirsher 	/* Remove legacy interrupt */
1384874aeea5SJeff Kirsher 	efx->legacy_irq = 0;
1385874aeea5SJeff Kirsher }
1386874aeea5SJeff Kirsher 
1387874aeea5SJeff Kirsher static void efx_set_channels(struct efx_nic *efx)
1388874aeea5SJeff Kirsher {
1389874aeea5SJeff Kirsher 	struct efx_channel *channel;
1390874aeea5SJeff Kirsher 	struct efx_tx_queue *tx_queue;
1391874aeea5SJeff Kirsher 
1392874aeea5SJeff Kirsher 	efx->tx_channel_offset =
1393874aeea5SJeff Kirsher 		separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0;
1394874aeea5SJeff Kirsher 
1395874aeea5SJeff Kirsher 	/* We need to adjust the TX queue numbers if we have separate
1396874aeea5SJeff Kirsher 	 * RX-only and TX-only channels.
1397874aeea5SJeff Kirsher 	 */
1398874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx) {
1399874aeea5SJeff Kirsher 		efx_for_each_channel_tx_queue(tx_queue, channel)
1400874aeea5SJeff Kirsher 			tx_queue->queue -= (efx->tx_channel_offset *
1401874aeea5SJeff Kirsher 					    EFX_TXQ_TYPES);
1402874aeea5SJeff Kirsher 	}
1403874aeea5SJeff Kirsher }
1404874aeea5SJeff Kirsher 
1405874aeea5SJeff Kirsher static int efx_probe_nic(struct efx_nic *efx)
1406874aeea5SJeff Kirsher {
1407874aeea5SJeff Kirsher 	size_t i;
1408874aeea5SJeff Kirsher 	int rc;
1409874aeea5SJeff Kirsher 
1410874aeea5SJeff Kirsher 	netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
1411874aeea5SJeff Kirsher 
1412874aeea5SJeff Kirsher 	/* Carry out hardware-type specific initialisation */
1413874aeea5SJeff Kirsher 	rc = efx->type->probe(efx);
1414874aeea5SJeff Kirsher 	if (rc)
1415874aeea5SJeff Kirsher 		return rc;
1416874aeea5SJeff Kirsher 
1417874aeea5SJeff Kirsher 	/* Determine the number of channels and queues by trying to hook
1418874aeea5SJeff Kirsher 	 * in MSI-X interrupts. */
1419874aeea5SJeff Kirsher 	rc = efx_probe_interrupts(efx);
1420874aeea5SJeff Kirsher 	if (rc)
1421874aeea5SJeff Kirsher 		goto fail;
1422874aeea5SJeff Kirsher 
1423874aeea5SJeff Kirsher 	if (efx->n_channels > 1)
1424874aeea5SJeff Kirsher 		get_random_bytes(&efx->rx_hash_key, sizeof(efx->rx_hash_key));
1425874aeea5SJeff Kirsher 	for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
1426278bc429SBen Hutchings 		efx->rx_indir_table[i] =
1427278bc429SBen Hutchings 			ethtool_rxfh_indir_default(i, efx->n_rx_channels);
1428874aeea5SJeff Kirsher 
1429874aeea5SJeff Kirsher 	efx_set_channels(efx);
1430874aeea5SJeff Kirsher 	netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1431874aeea5SJeff Kirsher 	netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
1432874aeea5SJeff Kirsher 
1433874aeea5SJeff Kirsher 	/* Initialise the interrupt moderation settings */
14349e393b30SBen Hutchings 	efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
14359e393b30SBen Hutchings 				true);
1436874aeea5SJeff Kirsher 
1437874aeea5SJeff Kirsher 	return 0;
1438874aeea5SJeff Kirsher 
1439874aeea5SJeff Kirsher fail:
1440874aeea5SJeff Kirsher 	efx->type->remove(efx);
1441874aeea5SJeff Kirsher 	return rc;
1442874aeea5SJeff Kirsher }
1443874aeea5SJeff Kirsher 
1444874aeea5SJeff Kirsher static void efx_remove_nic(struct efx_nic *efx)
1445874aeea5SJeff Kirsher {
1446874aeea5SJeff Kirsher 	netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
1447874aeea5SJeff Kirsher 
1448874aeea5SJeff Kirsher 	efx_remove_interrupts(efx);
1449874aeea5SJeff Kirsher 	efx->type->remove(efx);
1450874aeea5SJeff Kirsher }
1451874aeea5SJeff Kirsher 
1452874aeea5SJeff Kirsher /**************************************************************************
1453874aeea5SJeff Kirsher  *
1454874aeea5SJeff Kirsher  * NIC startup/shutdown
1455874aeea5SJeff Kirsher  *
1456874aeea5SJeff Kirsher  *************************************************************************/
1457874aeea5SJeff Kirsher 
1458874aeea5SJeff Kirsher static int efx_probe_all(struct efx_nic *efx)
1459874aeea5SJeff Kirsher {
1460874aeea5SJeff Kirsher 	int rc;
1461874aeea5SJeff Kirsher 
1462874aeea5SJeff Kirsher 	rc = efx_probe_nic(efx);
1463874aeea5SJeff Kirsher 	if (rc) {
1464874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
1465874aeea5SJeff Kirsher 		goto fail1;
1466874aeea5SJeff Kirsher 	}
1467874aeea5SJeff Kirsher 
1468874aeea5SJeff Kirsher 	rc = efx_probe_port(efx);
1469874aeea5SJeff Kirsher 	if (rc) {
1470874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev, "failed to create port\n");
1471874aeea5SJeff Kirsher 		goto fail2;
1472874aeea5SJeff Kirsher 	}
1473874aeea5SJeff Kirsher 
1474874aeea5SJeff Kirsher 	efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
1475874aeea5SJeff Kirsher 
1476874aeea5SJeff Kirsher 	rc = efx_probe_filters(efx);
1477874aeea5SJeff Kirsher 	if (rc) {
1478874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev,
1479874aeea5SJeff Kirsher 			  "failed to create filter tables\n");
14807f967c01SBen Hutchings 		goto fail3;
1481874aeea5SJeff Kirsher 	}
1482874aeea5SJeff Kirsher 
14837f967c01SBen Hutchings 	rc = efx_probe_channels(efx);
14847f967c01SBen Hutchings 	if (rc)
14857f967c01SBen Hutchings 		goto fail4;
14867f967c01SBen Hutchings 
1487874aeea5SJeff Kirsher 	return 0;
1488874aeea5SJeff Kirsher 
1489874aeea5SJeff Kirsher  fail4:
14907f967c01SBen Hutchings 	efx_remove_filters(efx);
1491874aeea5SJeff Kirsher  fail3:
1492874aeea5SJeff Kirsher 	efx_remove_port(efx);
1493874aeea5SJeff Kirsher  fail2:
1494874aeea5SJeff Kirsher 	efx_remove_nic(efx);
1495874aeea5SJeff Kirsher  fail1:
1496874aeea5SJeff Kirsher 	return rc;
1497874aeea5SJeff Kirsher }
1498874aeea5SJeff Kirsher 
14999f2cb71cSBen Hutchings /* Called after previous invocation(s) of efx_stop_all, restarts the port,
15009f2cb71cSBen Hutchings  * kernel transmit queues and NAPI processing, and ensures that the port is
15019f2cb71cSBen Hutchings  * scheduled to be reconfigured. This function is safe to call multiple
15029f2cb71cSBen Hutchings  * times when the NIC is in any state.
15039f2cb71cSBen Hutchings  */
1504874aeea5SJeff Kirsher static void efx_start_all(struct efx_nic *efx)
1505874aeea5SJeff Kirsher {
1506874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
1507874aeea5SJeff Kirsher 
1508874aeea5SJeff Kirsher 	/* Check that it is appropriate to restart the interface. All
1509874aeea5SJeff Kirsher 	 * of these flags are safe to read under just the rtnl lock */
1510874aeea5SJeff Kirsher 	if (efx->port_enabled)
1511874aeea5SJeff Kirsher 		return;
1512874aeea5SJeff Kirsher 	if ((efx->state != STATE_RUNNING) && (efx->state != STATE_INIT))
1513874aeea5SJeff Kirsher 		return;
151473ba7b68SBen Hutchings 	if (!netif_running(efx->net_dev))
1515874aeea5SJeff Kirsher 		return;
1516874aeea5SJeff Kirsher 
1517874aeea5SJeff Kirsher 	efx_start_port(efx);
15189f2cb71cSBen Hutchings 	efx_start_datapath(efx);
1519874aeea5SJeff Kirsher 
1520874aeea5SJeff Kirsher 	/* Start the hardware monitor if there is one. Otherwise (we're link
1521874aeea5SJeff Kirsher 	 * event driven), we have to poll the PHY because after an event queue
1522874aeea5SJeff Kirsher 	 * flush, we could have a missed a link state change */
1523874aeea5SJeff Kirsher 	if (efx->type->monitor != NULL) {
1524874aeea5SJeff Kirsher 		queue_delayed_work(efx->workqueue, &efx->monitor_work,
1525874aeea5SJeff Kirsher 				   efx_monitor_interval);
1526874aeea5SJeff Kirsher 	} else {
1527874aeea5SJeff Kirsher 		mutex_lock(&efx->mac_lock);
1528874aeea5SJeff Kirsher 		if (efx->phy_op->poll(efx))
1529874aeea5SJeff Kirsher 			efx_link_status_changed(efx);
1530874aeea5SJeff Kirsher 		mutex_unlock(&efx->mac_lock);
1531874aeea5SJeff Kirsher 	}
1532874aeea5SJeff Kirsher 
1533874aeea5SJeff Kirsher 	efx->type->start_stats(efx);
1534874aeea5SJeff Kirsher }
1535874aeea5SJeff Kirsher 
1536874aeea5SJeff Kirsher /* Flush all delayed work. Should only be called when no more delayed work
1537874aeea5SJeff Kirsher  * will be scheduled. This doesn't flush pending online resets (efx_reset),
1538874aeea5SJeff Kirsher  * since we're holding the rtnl_lock at this point. */
1539874aeea5SJeff Kirsher static void efx_flush_all(struct efx_nic *efx)
1540874aeea5SJeff Kirsher {
1541874aeea5SJeff Kirsher 	/* Make sure the hardware monitor is stopped */
1542874aeea5SJeff Kirsher 	cancel_delayed_work_sync(&efx->monitor_work);
1543874aeea5SJeff Kirsher 	/* Stop scheduled port reconfigurations */
1544874aeea5SJeff Kirsher 	cancel_work_sync(&efx->mac_work);
1545874aeea5SJeff Kirsher }
1546874aeea5SJeff Kirsher 
1547874aeea5SJeff Kirsher /* Quiesce hardware and software without bringing the link down.
1548874aeea5SJeff Kirsher  * Safe to call multiple times, when the nic and interface is in any
1549874aeea5SJeff Kirsher  * state. The caller is guaranteed to subsequently be in a position
1550874aeea5SJeff Kirsher  * to modify any hardware and software state they see fit without
1551874aeea5SJeff Kirsher  * taking locks. */
1552874aeea5SJeff Kirsher static void efx_stop_all(struct efx_nic *efx)
1553874aeea5SJeff Kirsher {
1554874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
1555874aeea5SJeff Kirsher 
1556874aeea5SJeff Kirsher 	/* port_enabled can be read safely under the rtnl lock */
1557874aeea5SJeff Kirsher 	if (!efx->port_enabled)
1558874aeea5SJeff Kirsher 		return;
1559874aeea5SJeff Kirsher 
1560874aeea5SJeff Kirsher 	efx->type->stop_stats(efx);
1561874aeea5SJeff Kirsher 	efx_stop_port(efx);
1562874aeea5SJeff Kirsher 
1563874aeea5SJeff Kirsher 	/* Flush efx_mac_work(), refill_workqueue, monitor_work */
1564874aeea5SJeff Kirsher 	efx_flush_all(efx);
1565874aeea5SJeff Kirsher 
1566874aeea5SJeff Kirsher 	/* Stop the kernel transmit interface late, so the watchdog
1567874aeea5SJeff Kirsher 	 * timer isn't ticking over the flush */
15689f2cb71cSBen Hutchings 	netif_tx_disable(efx->net_dev);
15699f2cb71cSBen Hutchings 
15709f2cb71cSBen Hutchings 	efx_stop_datapath(efx);
1571874aeea5SJeff Kirsher }
1572874aeea5SJeff Kirsher 
1573874aeea5SJeff Kirsher static void efx_remove_all(struct efx_nic *efx)
1574874aeea5SJeff Kirsher {
1575874aeea5SJeff Kirsher 	efx_remove_channels(efx);
15767f967c01SBen Hutchings 	efx_remove_filters(efx);
1577874aeea5SJeff Kirsher 	efx_remove_port(efx);
1578874aeea5SJeff Kirsher 	efx_remove_nic(efx);
1579874aeea5SJeff Kirsher }
1580874aeea5SJeff Kirsher 
1581874aeea5SJeff Kirsher /**************************************************************************
1582874aeea5SJeff Kirsher  *
1583874aeea5SJeff Kirsher  * Interrupt moderation
1584874aeea5SJeff Kirsher  *
1585874aeea5SJeff Kirsher  **************************************************************************/
1586874aeea5SJeff Kirsher 
1587cc180b69SBen Hutchings static unsigned int irq_mod_ticks(unsigned int usecs, unsigned int quantum_ns)
1588874aeea5SJeff Kirsher {
1589b548f976SBen Hutchings 	if (usecs == 0)
1590b548f976SBen Hutchings 		return 0;
1591cc180b69SBen Hutchings 	if (usecs * 1000 < quantum_ns)
1592874aeea5SJeff Kirsher 		return 1; /* never round down to 0 */
1593cc180b69SBen Hutchings 	return usecs * 1000 / quantum_ns;
1594874aeea5SJeff Kirsher }
1595874aeea5SJeff Kirsher 
1596874aeea5SJeff Kirsher /* Set interrupt moderation parameters */
15979e393b30SBen Hutchings int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
15989e393b30SBen Hutchings 			    unsigned int rx_usecs, bool rx_adaptive,
15999e393b30SBen Hutchings 			    bool rx_may_override_tx)
1600874aeea5SJeff Kirsher {
1601874aeea5SJeff Kirsher 	struct efx_channel *channel;
1602cc180b69SBen Hutchings 	unsigned int irq_mod_max = DIV_ROUND_UP(efx->type->timer_period_max *
1603cc180b69SBen Hutchings 						efx->timer_quantum_ns,
1604cc180b69SBen Hutchings 						1000);
1605cc180b69SBen Hutchings 	unsigned int tx_ticks;
1606cc180b69SBen Hutchings 	unsigned int rx_ticks;
1607874aeea5SJeff Kirsher 
1608874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
1609874aeea5SJeff Kirsher 
1610cc180b69SBen Hutchings 	if (tx_usecs > irq_mod_max || rx_usecs > irq_mod_max)
16119e393b30SBen Hutchings 		return -EINVAL;
16129e393b30SBen Hutchings 
1613cc180b69SBen Hutchings 	tx_ticks = irq_mod_ticks(tx_usecs, efx->timer_quantum_ns);
1614cc180b69SBen Hutchings 	rx_ticks = irq_mod_ticks(rx_usecs, efx->timer_quantum_ns);
1615cc180b69SBen Hutchings 
16169e393b30SBen Hutchings 	if (tx_ticks != rx_ticks && efx->tx_channel_offset == 0 &&
16179e393b30SBen Hutchings 	    !rx_may_override_tx) {
16189e393b30SBen Hutchings 		netif_err(efx, drv, efx->net_dev, "Channels are shared. "
16199e393b30SBen Hutchings 			  "RX and TX IRQ moderation must be equal\n");
16209e393b30SBen Hutchings 		return -EINVAL;
16219e393b30SBen Hutchings 	}
16229e393b30SBen Hutchings 
1623874aeea5SJeff Kirsher 	efx->irq_rx_adaptive = rx_adaptive;
1624874aeea5SJeff Kirsher 	efx->irq_rx_moderation = rx_ticks;
1625874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx) {
1626874aeea5SJeff Kirsher 		if (efx_channel_has_rx_queue(channel))
1627874aeea5SJeff Kirsher 			channel->irq_moderation = rx_ticks;
1628874aeea5SJeff Kirsher 		else if (efx_channel_has_tx_queues(channel))
1629874aeea5SJeff Kirsher 			channel->irq_moderation = tx_ticks;
1630874aeea5SJeff Kirsher 	}
16319e393b30SBen Hutchings 
16329e393b30SBen Hutchings 	return 0;
1633874aeea5SJeff Kirsher }
1634874aeea5SJeff Kirsher 
1635a0c4faf5SBen Hutchings void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
1636a0c4faf5SBen Hutchings 			    unsigned int *rx_usecs, bool *rx_adaptive)
1637a0c4faf5SBen Hutchings {
1638cc180b69SBen Hutchings 	/* We must round up when converting ticks to microseconds
1639cc180b69SBen Hutchings 	 * because we round down when converting the other way.
1640cc180b69SBen Hutchings 	 */
1641cc180b69SBen Hutchings 
1642a0c4faf5SBen Hutchings 	*rx_adaptive = efx->irq_rx_adaptive;
1643cc180b69SBen Hutchings 	*rx_usecs = DIV_ROUND_UP(efx->irq_rx_moderation *
1644cc180b69SBen Hutchings 				 efx->timer_quantum_ns,
1645cc180b69SBen Hutchings 				 1000);
1646a0c4faf5SBen Hutchings 
1647a0c4faf5SBen Hutchings 	/* If channels are shared between RX and TX, so is IRQ
1648a0c4faf5SBen Hutchings 	 * moderation.  Otherwise, IRQ moderation is the same for all
1649a0c4faf5SBen Hutchings 	 * TX channels and is not adaptive.
1650a0c4faf5SBen Hutchings 	 */
1651a0c4faf5SBen Hutchings 	if (efx->tx_channel_offset == 0)
1652a0c4faf5SBen Hutchings 		*tx_usecs = *rx_usecs;
1653a0c4faf5SBen Hutchings 	else
1654cc180b69SBen Hutchings 		*tx_usecs = DIV_ROUND_UP(
1655a0c4faf5SBen Hutchings 			efx->channel[efx->tx_channel_offset]->irq_moderation *
1656cc180b69SBen Hutchings 			efx->timer_quantum_ns,
1657cc180b69SBen Hutchings 			1000);
1658a0c4faf5SBen Hutchings }
1659a0c4faf5SBen Hutchings 
1660874aeea5SJeff Kirsher /**************************************************************************
1661874aeea5SJeff Kirsher  *
1662874aeea5SJeff Kirsher  * Hardware monitor
1663874aeea5SJeff Kirsher  *
1664874aeea5SJeff Kirsher  **************************************************************************/
1665874aeea5SJeff Kirsher 
1666874aeea5SJeff Kirsher /* Run periodically off the general workqueue */
1667874aeea5SJeff Kirsher static void efx_monitor(struct work_struct *data)
1668874aeea5SJeff Kirsher {
1669874aeea5SJeff Kirsher 	struct efx_nic *efx = container_of(data, struct efx_nic,
1670874aeea5SJeff Kirsher 					   monitor_work.work);
1671874aeea5SJeff Kirsher 
1672874aeea5SJeff Kirsher 	netif_vdbg(efx, timer, efx->net_dev,
1673874aeea5SJeff Kirsher 		   "hardware monitor executing on CPU %d\n",
1674874aeea5SJeff Kirsher 		   raw_smp_processor_id());
1675874aeea5SJeff Kirsher 	BUG_ON(efx->type->monitor == NULL);
1676874aeea5SJeff Kirsher 
1677874aeea5SJeff Kirsher 	/* If the mac_lock is already held then it is likely a port
1678874aeea5SJeff Kirsher 	 * reconfiguration is already in place, which will likely do
1679874aeea5SJeff Kirsher 	 * most of the work of monitor() anyway. */
1680874aeea5SJeff Kirsher 	if (mutex_trylock(&efx->mac_lock)) {
1681874aeea5SJeff Kirsher 		if (efx->port_enabled)
1682874aeea5SJeff Kirsher 			efx->type->monitor(efx);
1683874aeea5SJeff Kirsher 		mutex_unlock(&efx->mac_lock);
1684874aeea5SJeff Kirsher 	}
1685874aeea5SJeff Kirsher 
1686874aeea5SJeff Kirsher 	queue_delayed_work(efx->workqueue, &efx->monitor_work,
1687874aeea5SJeff Kirsher 			   efx_monitor_interval);
1688874aeea5SJeff Kirsher }
1689874aeea5SJeff Kirsher 
1690874aeea5SJeff Kirsher /**************************************************************************
1691874aeea5SJeff Kirsher  *
1692874aeea5SJeff Kirsher  * ioctls
1693874aeea5SJeff Kirsher  *
1694874aeea5SJeff Kirsher  *************************************************************************/
1695874aeea5SJeff Kirsher 
1696874aeea5SJeff Kirsher /* Net device ioctl
1697874aeea5SJeff Kirsher  * Context: process, rtnl_lock() held.
1698874aeea5SJeff Kirsher  */
1699874aeea5SJeff Kirsher static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1700874aeea5SJeff Kirsher {
1701874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
1702874aeea5SJeff Kirsher 	struct mii_ioctl_data *data = if_mii(ifr);
1703874aeea5SJeff Kirsher 
1704874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
1705874aeea5SJeff Kirsher 
1706874aeea5SJeff Kirsher 	/* Convert phy_id from older PRTAD/DEVAD format */
1707874aeea5SJeff Kirsher 	if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
1708874aeea5SJeff Kirsher 	    (data->phy_id & 0xfc00) == 0x0400)
1709874aeea5SJeff Kirsher 		data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
1710874aeea5SJeff Kirsher 
1711874aeea5SJeff Kirsher 	return mdio_mii_ioctl(&efx->mdio, data, cmd);
1712874aeea5SJeff Kirsher }
1713874aeea5SJeff Kirsher 
1714874aeea5SJeff Kirsher /**************************************************************************
1715874aeea5SJeff Kirsher  *
1716874aeea5SJeff Kirsher  * NAPI interface
1717874aeea5SJeff Kirsher  *
1718874aeea5SJeff Kirsher  **************************************************************************/
1719874aeea5SJeff Kirsher 
17207f967c01SBen Hutchings static void efx_init_napi_channel(struct efx_channel *channel)
1721874aeea5SJeff Kirsher {
17227f967c01SBen Hutchings 	struct efx_nic *efx = channel->efx;
1723874aeea5SJeff Kirsher 
1724874aeea5SJeff Kirsher 	channel->napi_dev = efx->net_dev;
1725874aeea5SJeff Kirsher 	netif_napi_add(channel->napi_dev, &channel->napi_str,
1726874aeea5SJeff Kirsher 		       efx_poll, napi_weight);
1727874aeea5SJeff Kirsher }
17287f967c01SBen Hutchings 
17297f967c01SBen Hutchings static void efx_init_napi(struct efx_nic *efx)
17307f967c01SBen Hutchings {
17317f967c01SBen Hutchings 	struct efx_channel *channel;
17327f967c01SBen Hutchings 
17337f967c01SBen Hutchings 	efx_for_each_channel(channel, efx)
17347f967c01SBen Hutchings 		efx_init_napi_channel(channel);
1735874aeea5SJeff Kirsher }
1736874aeea5SJeff Kirsher 
1737874aeea5SJeff Kirsher static void efx_fini_napi_channel(struct efx_channel *channel)
1738874aeea5SJeff Kirsher {
1739874aeea5SJeff Kirsher 	if (channel->napi_dev)
1740874aeea5SJeff Kirsher 		netif_napi_del(&channel->napi_str);
1741874aeea5SJeff Kirsher 	channel->napi_dev = NULL;
1742874aeea5SJeff Kirsher }
1743874aeea5SJeff Kirsher 
1744874aeea5SJeff Kirsher static void efx_fini_napi(struct efx_nic *efx)
1745874aeea5SJeff Kirsher {
1746874aeea5SJeff Kirsher 	struct efx_channel *channel;
1747874aeea5SJeff Kirsher 
1748874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx)
1749874aeea5SJeff Kirsher 		efx_fini_napi_channel(channel);
1750874aeea5SJeff Kirsher }
1751874aeea5SJeff Kirsher 
1752874aeea5SJeff Kirsher /**************************************************************************
1753874aeea5SJeff Kirsher  *
1754874aeea5SJeff Kirsher  * Kernel netpoll interface
1755874aeea5SJeff Kirsher  *
1756874aeea5SJeff Kirsher  *************************************************************************/
1757874aeea5SJeff Kirsher 
1758874aeea5SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
1759874aeea5SJeff Kirsher 
1760874aeea5SJeff Kirsher /* Although in the common case interrupts will be disabled, this is not
1761874aeea5SJeff Kirsher  * guaranteed. However, all our work happens inside the NAPI callback,
1762874aeea5SJeff Kirsher  * so no locking is required.
1763874aeea5SJeff Kirsher  */
1764874aeea5SJeff Kirsher static void efx_netpoll(struct net_device *net_dev)
1765874aeea5SJeff Kirsher {
1766874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
1767874aeea5SJeff Kirsher 	struct efx_channel *channel;
1768874aeea5SJeff Kirsher 
1769874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx)
1770874aeea5SJeff Kirsher 		efx_schedule_channel(channel);
1771874aeea5SJeff Kirsher }
1772874aeea5SJeff Kirsher 
1773874aeea5SJeff Kirsher #endif
1774874aeea5SJeff Kirsher 
1775874aeea5SJeff Kirsher /**************************************************************************
1776874aeea5SJeff Kirsher  *
1777874aeea5SJeff Kirsher  * Kernel net device interface
1778874aeea5SJeff Kirsher  *
1779874aeea5SJeff Kirsher  *************************************************************************/
1780874aeea5SJeff Kirsher 
1781874aeea5SJeff Kirsher /* Context: process, rtnl_lock() held. */
1782874aeea5SJeff Kirsher static int efx_net_open(struct net_device *net_dev)
1783874aeea5SJeff Kirsher {
1784874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
1785874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
1786874aeea5SJeff Kirsher 
1787874aeea5SJeff Kirsher 	netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
1788874aeea5SJeff Kirsher 		  raw_smp_processor_id());
1789874aeea5SJeff Kirsher 
1790874aeea5SJeff Kirsher 	if (efx->state == STATE_DISABLED)
1791874aeea5SJeff Kirsher 		return -EIO;
1792874aeea5SJeff Kirsher 	if (efx->phy_mode & PHY_MODE_SPECIAL)
1793874aeea5SJeff Kirsher 		return -EBUSY;
1794874aeea5SJeff Kirsher 	if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
1795874aeea5SJeff Kirsher 		return -EIO;
1796874aeea5SJeff Kirsher 
1797874aeea5SJeff Kirsher 	/* Notify the kernel of the link state polled during driver load,
1798874aeea5SJeff Kirsher 	 * before the monitor starts running */
1799874aeea5SJeff Kirsher 	efx_link_status_changed(efx);
1800874aeea5SJeff Kirsher 
1801874aeea5SJeff Kirsher 	efx_start_all(efx);
1802874aeea5SJeff Kirsher 	return 0;
1803874aeea5SJeff Kirsher }
1804874aeea5SJeff Kirsher 
1805874aeea5SJeff Kirsher /* Context: process, rtnl_lock() held.
1806874aeea5SJeff Kirsher  * Note that the kernel will ignore our return code; this method
1807874aeea5SJeff Kirsher  * should really be a void.
1808874aeea5SJeff Kirsher  */
1809874aeea5SJeff Kirsher static int efx_net_stop(struct net_device *net_dev)
1810874aeea5SJeff Kirsher {
1811874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
1812874aeea5SJeff Kirsher 
1813874aeea5SJeff Kirsher 	netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
1814874aeea5SJeff Kirsher 		  raw_smp_processor_id());
1815874aeea5SJeff Kirsher 
1816874aeea5SJeff Kirsher 	if (efx->state != STATE_DISABLED) {
1817874aeea5SJeff Kirsher 		/* Stop the device and flush all the channels */
1818874aeea5SJeff Kirsher 		efx_stop_all(efx);
1819874aeea5SJeff Kirsher 	}
1820874aeea5SJeff Kirsher 
1821874aeea5SJeff Kirsher 	return 0;
1822874aeea5SJeff Kirsher }
1823874aeea5SJeff Kirsher 
1824874aeea5SJeff Kirsher /* Context: process, dev_base_lock or RTNL held, non-blocking. */
18252aa9ef11SBen Hutchings static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev,
18262aa9ef11SBen Hutchings 					       struct rtnl_link_stats64 *stats)
1827874aeea5SJeff Kirsher {
1828874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
1829874aeea5SJeff Kirsher 	struct efx_mac_stats *mac_stats = &efx->mac_stats;
1830874aeea5SJeff Kirsher 
1831874aeea5SJeff Kirsher 	spin_lock_bh(&efx->stats_lock);
18321cb34522SBen Hutchings 
1833874aeea5SJeff Kirsher 	efx->type->update_stats(efx);
1834874aeea5SJeff Kirsher 
1835874aeea5SJeff Kirsher 	stats->rx_packets = mac_stats->rx_packets;
1836874aeea5SJeff Kirsher 	stats->tx_packets = mac_stats->tx_packets;
1837874aeea5SJeff Kirsher 	stats->rx_bytes = mac_stats->rx_bytes;
1838874aeea5SJeff Kirsher 	stats->tx_bytes = mac_stats->tx_bytes;
1839874aeea5SJeff Kirsher 	stats->rx_dropped = efx->n_rx_nodesc_drop_cnt;
1840874aeea5SJeff Kirsher 	stats->multicast = mac_stats->rx_multicast;
1841874aeea5SJeff Kirsher 	stats->collisions = mac_stats->tx_collision;
1842874aeea5SJeff Kirsher 	stats->rx_length_errors = (mac_stats->rx_gtjumbo +
1843874aeea5SJeff Kirsher 				   mac_stats->rx_length_error);
1844874aeea5SJeff Kirsher 	stats->rx_crc_errors = mac_stats->rx_bad;
1845874aeea5SJeff Kirsher 	stats->rx_frame_errors = mac_stats->rx_align_error;
1846874aeea5SJeff Kirsher 	stats->rx_fifo_errors = mac_stats->rx_overflow;
1847874aeea5SJeff Kirsher 	stats->rx_missed_errors = mac_stats->rx_missed;
1848874aeea5SJeff Kirsher 	stats->tx_window_errors = mac_stats->tx_late_collision;
1849874aeea5SJeff Kirsher 
1850874aeea5SJeff Kirsher 	stats->rx_errors = (stats->rx_length_errors +
1851874aeea5SJeff Kirsher 			    stats->rx_crc_errors +
1852874aeea5SJeff Kirsher 			    stats->rx_frame_errors +
1853874aeea5SJeff Kirsher 			    mac_stats->rx_symbol_error);
1854874aeea5SJeff Kirsher 	stats->tx_errors = (stats->tx_window_errors +
1855874aeea5SJeff Kirsher 			    mac_stats->tx_bad);
1856874aeea5SJeff Kirsher 
18571cb34522SBen Hutchings 	spin_unlock_bh(&efx->stats_lock);
18581cb34522SBen Hutchings 
1859874aeea5SJeff Kirsher 	return stats;
1860874aeea5SJeff Kirsher }
1861874aeea5SJeff Kirsher 
1862874aeea5SJeff Kirsher /* Context: netif_tx_lock held, BHs disabled. */
1863874aeea5SJeff Kirsher static void efx_watchdog(struct net_device *net_dev)
1864874aeea5SJeff Kirsher {
1865874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
1866874aeea5SJeff Kirsher 
1867874aeea5SJeff Kirsher 	netif_err(efx, tx_err, efx->net_dev,
1868874aeea5SJeff Kirsher 		  "TX stuck with port_enabled=%d: resetting channels\n",
1869874aeea5SJeff Kirsher 		  efx->port_enabled);
1870874aeea5SJeff Kirsher 
1871874aeea5SJeff Kirsher 	efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
1872874aeea5SJeff Kirsher }
1873874aeea5SJeff Kirsher 
1874874aeea5SJeff Kirsher 
1875874aeea5SJeff Kirsher /* Context: process, rtnl_lock() held. */
1876874aeea5SJeff Kirsher static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
1877874aeea5SJeff Kirsher {
1878874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
1879874aeea5SJeff Kirsher 
1880874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
1881874aeea5SJeff Kirsher 
1882874aeea5SJeff Kirsher 	if (new_mtu > EFX_MAX_MTU)
1883874aeea5SJeff Kirsher 		return -EINVAL;
1884874aeea5SJeff Kirsher 
1885874aeea5SJeff Kirsher 	efx_stop_all(efx);
1886874aeea5SJeff Kirsher 
1887874aeea5SJeff Kirsher 	netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
1888874aeea5SJeff Kirsher 
1889874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
1890874aeea5SJeff Kirsher 	/* Reconfigure the MAC before enabling the dma queues so that
1891874aeea5SJeff Kirsher 	 * the RX buffers don't overflow */
1892874aeea5SJeff Kirsher 	net_dev->mtu = new_mtu;
1893710b208dSBen Hutchings 	efx->type->reconfigure_mac(efx);
1894874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
1895874aeea5SJeff Kirsher 
1896874aeea5SJeff Kirsher 	efx_start_all(efx);
18976c8eef4aSBen Hutchings 	return 0;
1898874aeea5SJeff Kirsher }
1899874aeea5SJeff Kirsher 
1900874aeea5SJeff Kirsher static int efx_set_mac_address(struct net_device *net_dev, void *data)
1901874aeea5SJeff Kirsher {
1902874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
1903874aeea5SJeff Kirsher 	struct sockaddr *addr = data;
1904874aeea5SJeff Kirsher 	char *new_addr = addr->sa_data;
1905874aeea5SJeff Kirsher 
1906874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
1907874aeea5SJeff Kirsher 
1908874aeea5SJeff Kirsher 	if (!is_valid_ether_addr(new_addr)) {
1909874aeea5SJeff Kirsher 		netif_err(efx, drv, efx->net_dev,
1910874aeea5SJeff Kirsher 			  "invalid ethernet MAC address requested: %pM\n",
1911874aeea5SJeff Kirsher 			  new_addr);
1912874aeea5SJeff Kirsher 		return -EINVAL;
1913874aeea5SJeff Kirsher 	}
1914874aeea5SJeff Kirsher 
1915874aeea5SJeff Kirsher 	memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
1916874aeea5SJeff Kirsher 
1917874aeea5SJeff Kirsher 	/* Reconfigure the MAC */
1918874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
1919710b208dSBen Hutchings 	efx->type->reconfigure_mac(efx);
1920874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
1921874aeea5SJeff Kirsher 
1922874aeea5SJeff Kirsher 	return 0;
1923874aeea5SJeff Kirsher }
1924874aeea5SJeff Kirsher 
1925874aeea5SJeff Kirsher /* Context: netif_addr_lock held, BHs disabled. */
19260fca8c97SBen Hutchings static void efx_set_rx_mode(struct net_device *net_dev)
1927874aeea5SJeff Kirsher {
1928874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
1929874aeea5SJeff Kirsher 	struct netdev_hw_addr *ha;
1930874aeea5SJeff Kirsher 	union efx_multicast_hash *mc_hash = &efx->multicast_hash;
1931874aeea5SJeff Kirsher 	u32 crc;
1932874aeea5SJeff Kirsher 	int bit;
1933874aeea5SJeff Kirsher 
1934874aeea5SJeff Kirsher 	efx->promiscuous = !!(net_dev->flags & IFF_PROMISC);
1935874aeea5SJeff Kirsher 
1936874aeea5SJeff Kirsher 	/* Build multicast hash table */
1937874aeea5SJeff Kirsher 	if (efx->promiscuous || (net_dev->flags & IFF_ALLMULTI)) {
1938874aeea5SJeff Kirsher 		memset(mc_hash, 0xff, sizeof(*mc_hash));
1939874aeea5SJeff Kirsher 	} else {
1940874aeea5SJeff Kirsher 		memset(mc_hash, 0x00, sizeof(*mc_hash));
1941874aeea5SJeff Kirsher 		netdev_for_each_mc_addr(ha, net_dev) {
1942874aeea5SJeff Kirsher 			crc = ether_crc_le(ETH_ALEN, ha->addr);
1943874aeea5SJeff Kirsher 			bit = crc & (EFX_MCAST_HASH_ENTRIES - 1);
1944874aeea5SJeff Kirsher 			set_bit_le(bit, mc_hash->byte);
1945874aeea5SJeff Kirsher 		}
1946874aeea5SJeff Kirsher 
1947874aeea5SJeff Kirsher 		/* Broadcast packets go through the multicast hash filter.
1948874aeea5SJeff Kirsher 		 * ether_crc_le() of the broadcast address is 0xbe2612ff
1949874aeea5SJeff Kirsher 		 * so we always add bit 0xff to the mask.
1950874aeea5SJeff Kirsher 		 */
1951874aeea5SJeff Kirsher 		set_bit_le(0xff, mc_hash->byte);
1952874aeea5SJeff Kirsher 	}
1953874aeea5SJeff Kirsher 
1954874aeea5SJeff Kirsher 	if (efx->port_enabled)
1955874aeea5SJeff Kirsher 		queue_work(efx->workqueue, &efx->mac_work);
1956874aeea5SJeff Kirsher 	/* Otherwise efx_start_port() will do this */
1957874aeea5SJeff Kirsher }
1958874aeea5SJeff Kirsher 
1959c8f44affSMichał Mirosław static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
1960874aeea5SJeff Kirsher {
1961874aeea5SJeff Kirsher 	struct efx_nic *efx = netdev_priv(net_dev);
1962874aeea5SJeff Kirsher 
1963874aeea5SJeff Kirsher 	/* If disabling RX n-tuple filtering, clear existing filters */
1964874aeea5SJeff Kirsher 	if (net_dev->features & ~data & NETIF_F_NTUPLE)
1965874aeea5SJeff Kirsher 		efx_filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
1966874aeea5SJeff Kirsher 
1967874aeea5SJeff Kirsher 	return 0;
1968874aeea5SJeff Kirsher }
1969874aeea5SJeff Kirsher 
1970874aeea5SJeff Kirsher static const struct net_device_ops efx_netdev_ops = {
1971874aeea5SJeff Kirsher 	.ndo_open		= efx_net_open,
1972874aeea5SJeff Kirsher 	.ndo_stop		= efx_net_stop,
1973874aeea5SJeff Kirsher 	.ndo_get_stats64	= efx_net_stats,
1974874aeea5SJeff Kirsher 	.ndo_tx_timeout		= efx_watchdog,
1975874aeea5SJeff Kirsher 	.ndo_start_xmit		= efx_hard_start_xmit,
1976874aeea5SJeff Kirsher 	.ndo_validate_addr	= eth_validate_addr,
1977874aeea5SJeff Kirsher 	.ndo_do_ioctl		= efx_ioctl,
1978874aeea5SJeff Kirsher 	.ndo_change_mtu		= efx_change_mtu,
1979874aeea5SJeff Kirsher 	.ndo_set_mac_address	= efx_set_mac_address,
19800fca8c97SBen Hutchings 	.ndo_set_rx_mode	= efx_set_rx_mode,
1981874aeea5SJeff Kirsher 	.ndo_set_features	= efx_set_features,
1982874aeea5SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
1983874aeea5SJeff Kirsher 	.ndo_poll_controller = efx_netpoll,
1984874aeea5SJeff Kirsher #endif
1985874aeea5SJeff Kirsher 	.ndo_setup_tc		= efx_setup_tc,
1986874aeea5SJeff Kirsher #ifdef CONFIG_RFS_ACCEL
1987874aeea5SJeff Kirsher 	.ndo_rx_flow_steer	= efx_filter_rfs,
1988874aeea5SJeff Kirsher #endif
1989874aeea5SJeff Kirsher };
1990874aeea5SJeff Kirsher 
1991874aeea5SJeff Kirsher static void efx_update_name(struct efx_nic *efx)
1992874aeea5SJeff Kirsher {
1993874aeea5SJeff Kirsher 	strcpy(efx->name, efx->net_dev->name);
1994874aeea5SJeff Kirsher 	efx_mtd_rename(efx);
1995874aeea5SJeff Kirsher 	efx_set_channel_names(efx);
1996874aeea5SJeff Kirsher }
1997874aeea5SJeff Kirsher 
1998874aeea5SJeff Kirsher static int efx_netdev_event(struct notifier_block *this,
1999874aeea5SJeff Kirsher 			    unsigned long event, void *ptr)
2000874aeea5SJeff Kirsher {
2001874aeea5SJeff Kirsher 	struct net_device *net_dev = ptr;
2002874aeea5SJeff Kirsher 
2003874aeea5SJeff Kirsher 	if (net_dev->netdev_ops == &efx_netdev_ops &&
2004874aeea5SJeff Kirsher 	    event == NETDEV_CHANGENAME)
2005874aeea5SJeff Kirsher 		efx_update_name(netdev_priv(net_dev));
2006874aeea5SJeff Kirsher 
2007874aeea5SJeff Kirsher 	return NOTIFY_DONE;
2008874aeea5SJeff Kirsher }
2009874aeea5SJeff Kirsher 
2010874aeea5SJeff Kirsher static struct notifier_block efx_netdev_notifier = {
2011874aeea5SJeff Kirsher 	.notifier_call = efx_netdev_event,
2012874aeea5SJeff Kirsher };
2013874aeea5SJeff Kirsher 
2014874aeea5SJeff Kirsher static ssize_t
2015874aeea5SJeff Kirsher show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
2016874aeea5SJeff Kirsher {
2017874aeea5SJeff Kirsher 	struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2018874aeea5SJeff Kirsher 	return sprintf(buf, "%d\n", efx->phy_type);
2019874aeea5SJeff Kirsher }
2020874aeea5SJeff Kirsher static DEVICE_ATTR(phy_type, 0644, show_phy_type, NULL);
2021874aeea5SJeff Kirsher 
2022874aeea5SJeff Kirsher static int efx_register_netdev(struct efx_nic *efx)
2023874aeea5SJeff Kirsher {
2024874aeea5SJeff Kirsher 	struct net_device *net_dev = efx->net_dev;
2025874aeea5SJeff Kirsher 	struct efx_channel *channel;
2026874aeea5SJeff Kirsher 	int rc;
2027874aeea5SJeff Kirsher 
2028874aeea5SJeff Kirsher 	net_dev->watchdog_timeo = 5 * HZ;
2029874aeea5SJeff Kirsher 	net_dev->irq = efx->pci_dev->irq;
2030874aeea5SJeff Kirsher 	net_dev->netdev_ops = &efx_netdev_ops;
2031874aeea5SJeff Kirsher 	SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
2032874aeea5SJeff Kirsher 
2033874aeea5SJeff Kirsher 	rtnl_lock();
2034874aeea5SJeff Kirsher 
2035874aeea5SJeff Kirsher 	rc = dev_alloc_name(net_dev, net_dev->name);
2036874aeea5SJeff Kirsher 	if (rc < 0)
2037874aeea5SJeff Kirsher 		goto fail_locked;
2038874aeea5SJeff Kirsher 	efx_update_name(efx);
2039874aeea5SJeff Kirsher 
2040874aeea5SJeff Kirsher 	rc = register_netdevice(net_dev);
2041874aeea5SJeff Kirsher 	if (rc)
2042874aeea5SJeff Kirsher 		goto fail_locked;
2043874aeea5SJeff Kirsher 
2044874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx) {
2045874aeea5SJeff Kirsher 		struct efx_tx_queue *tx_queue;
2046874aeea5SJeff Kirsher 		efx_for_each_channel_tx_queue(tx_queue, channel)
2047874aeea5SJeff Kirsher 			efx_init_tx_queue_core_txq(tx_queue);
2048874aeea5SJeff Kirsher 	}
2049874aeea5SJeff Kirsher 
2050874aeea5SJeff Kirsher 	/* Always start with carrier off; PHY events will detect the link */
205186ee5302SBen Hutchings 	netif_carrier_off(net_dev);
2052874aeea5SJeff Kirsher 
2053874aeea5SJeff Kirsher 	rtnl_unlock();
2054874aeea5SJeff Kirsher 
2055874aeea5SJeff Kirsher 	rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2056874aeea5SJeff Kirsher 	if (rc) {
2057874aeea5SJeff Kirsher 		netif_err(efx, drv, efx->net_dev,
2058874aeea5SJeff Kirsher 			  "failed to init net dev attributes\n");
2059874aeea5SJeff Kirsher 		goto fail_registered;
2060874aeea5SJeff Kirsher 	}
2061874aeea5SJeff Kirsher 
2062874aeea5SJeff Kirsher 	return 0;
2063874aeea5SJeff Kirsher 
2064874aeea5SJeff Kirsher fail_locked:
2065874aeea5SJeff Kirsher 	rtnl_unlock();
2066874aeea5SJeff Kirsher 	netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
2067874aeea5SJeff Kirsher 	return rc;
2068874aeea5SJeff Kirsher 
2069874aeea5SJeff Kirsher fail_registered:
2070874aeea5SJeff Kirsher 	unregister_netdev(net_dev);
2071874aeea5SJeff Kirsher 	return rc;
2072874aeea5SJeff Kirsher }
2073874aeea5SJeff Kirsher 
2074874aeea5SJeff Kirsher static void efx_unregister_netdev(struct efx_nic *efx)
2075874aeea5SJeff Kirsher {
2076874aeea5SJeff Kirsher 	struct efx_channel *channel;
2077874aeea5SJeff Kirsher 	struct efx_tx_queue *tx_queue;
2078874aeea5SJeff Kirsher 
2079874aeea5SJeff Kirsher 	if (!efx->net_dev)
2080874aeea5SJeff Kirsher 		return;
2081874aeea5SJeff Kirsher 
2082874aeea5SJeff Kirsher 	BUG_ON(netdev_priv(efx->net_dev) != efx);
2083874aeea5SJeff Kirsher 
2084874aeea5SJeff Kirsher 	/* Free up any skbs still remaining. This has to happen before
2085874aeea5SJeff Kirsher 	 * we try to unregister the netdev as running their destructors
2086874aeea5SJeff Kirsher 	 * may be needed to get the device ref. count to 0. */
2087874aeea5SJeff Kirsher 	efx_for_each_channel(channel, efx) {
2088874aeea5SJeff Kirsher 		efx_for_each_channel_tx_queue(tx_queue, channel)
2089874aeea5SJeff Kirsher 			efx_release_tx_buffers(tx_queue);
2090874aeea5SJeff Kirsher 	}
2091874aeea5SJeff Kirsher 
2092874aeea5SJeff Kirsher 	strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2093874aeea5SJeff Kirsher 	device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2094874aeea5SJeff Kirsher 	unregister_netdev(efx->net_dev);
2095874aeea5SJeff Kirsher }
2096874aeea5SJeff Kirsher 
2097874aeea5SJeff Kirsher /**************************************************************************
2098874aeea5SJeff Kirsher  *
2099874aeea5SJeff Kirsher  * Device reset and suspend
2100874aeea5SJeff Kirsher  *
2101874aeea5SJeff Kirsher  **************************************************************************/
2102874aeea5SJeff Kirsher 
2103874aeea5SJeff Kirsher /* Tears down the entire software state and most of the hardware state
2104874aeea5SJeff Kirsher  * before reset.  */
2105874aeea5SJeff Kirsher void efx_reset_down(struct efx_nic *efx, enum reset_type method)
2106874aeea5SJeff Kirsher {
2107874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
2108874aeea5SJeff Kirsher 
2109874aeea5SJeff Kirsher 	efx_stop_all(efx);
2110874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
2111874aeea5SJeff Kirsher 
21127f967c01SBen Hutchings 	efx_stop_interrupts(efx, false);
2113874aeea5SJeff Kirsher 	if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
2114874aeea5SJeff Kirsher 		efx->phy_op->fini(efx);
2115874aeea5SJeff Kirsher 	efx->type->fini(efx);
2116874aeea5SJeff Kirsher }
2117874aeea5SJeff Kirsher 
2118874aeea5SJeff Kirsher /* This function will always ensure that the locks acquired in
2119874aeea5SJeff Kirsher  * efx_reset_down() are released. A failure return code indicates
2120874aeea5SJeff Kirsher  * that we were unable to reinitialise the hardware, and the
2121874aeea5SJeff Kirsher  * driver should be disabled. If ok is false, then the rx and tx
2122874aeea5SJeff Kirsher  * engines are not restarted, pending a RESET_DISABLE. */
2123874aeea5SJeff Kirsher int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
2124874aeea5SJeff Kirsher {
2125874aeea5SJeff Kirsher 	int rc;
2126874aeea5SJeff Kirsher 
2127874aeea5SJeff Kirsher 	EFX_ASSERT_RESET_SERIALISED(efx);
2128874aeea5SJeff Kirsher 
2129874aeea5SJeff Kirsher 	rc = efx->type->init(efx);
2130874aeea5SJeff Kirsher 	if (rc) {
2131874aeea5SJeff Kirsher 		netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
2132874aeea5SJeff Kirsher 		goto fail;
2133874aeea5SJeff Kirsher 	}
2134874aeea5SJeff Kirsher 
2135874aeea5SJeff Kirsher 	if (!ok)
2136874aeea5SJeff Kirsher 		goto fail;
2137874aeea5SJeff Kirsher 
2138874aeea5SJeff Kirsher 	if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
2139874aeea5SJeff Kirsher 		rc = efx->phy_op->init(efx);
2140874aeea5SJeff Kirsher 		if (rc)
2141874aeea5SJeff Kirsher 			goto fail;
2142874aeea5SJeff Kirsher 		if (efx->phy_op->reconfigure(efx))
2143874aeea5SJeff Kirsher 			netif_err(efx, drv, efx->net_dev,
2144874aeea5SJeff Kirsher 				  "could not restore PHY settings\n");
2145874aeea5SJeff Kirsher 	}
2146874aeea5SJeff Kirsher 
2147710b208dSBen Hutchings 	efx->type->reconfigure_mac(efx);
2148874aeea5SJeff Kirsher 
21497f967c01SBen Hutchings 	efx_start_interrupts(efx, false);
2150874aeea5SJeff Kirsher 	efx_restore_filters(efx);
2151874aeea5SJeff Kirsher 
2152874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
2153874aeea5SJeff Kirsher 
2154874aeea5SJeff Kirsher 	efx_start_all(efx);
2155874aeea5SJeff Kirsher 
2156874aeea5SJeff Kirsher 	return 0;
2157874aeea5SJeff Kirsher 
2158874aeea5SJeff Kirsher fail:
2159874aeea5SJeff Kirsher 	efx->port_initialized = false;
2160874aeea5SJeff Kirsher 
2161874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
2162874aeea5SJeff Kirsher 
2163874aeea5SJeff Kirsher 	return rc;
2164874aeea5SJeff Kirsher }
2165874aeea5SJeff Kirsher 
2166874aeea5SJeff Kirsher /* Reset the NIC using the specified method.  Note that the reset may
2167874aeea5SJeff Kirsher  * fail, in which case the card will be left in an unusable state.
2168874aeea5SJeff Kirsher  *
2169874aeea5SJeff Kirsher  * Caller must hold the rtnl_lock.
2170874aeea5SJeff Kirsher  */
2171874aeea5SJeff Kirsher int efx_reset(struct efx_nic *efx, enum reset_type method)
2172874aeea5SJeff Kirsher {
2173874aeea5SJeff Kirsher 	int rc, rc2;
2174874aeea5SJeff Kirsher 	bool disabled;
2175874aeea5SJeff Kirsher 
2176874aeea5SJeff Kirsher 	netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2177874aeea5SJeff Kirsher 		   RESET_TYPE(method));
2178874aeea5SJeff Kirsher 
2179874aeea5SJeff Kirsher 	netif_device_detach(efx->net_dev);
2180874aeea5SJeff Kirsher 	efx_reset_down(efx, method);
2181874aeea5SJeff Kirsher 
2182874aeea5SJeff Kirsher 	rc = efx->type->reset(efx, method);
2183874aeea5SJeff Kirsher 	if (rc) {
2184874aeea5SJeff Kirsher 		netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
2185874aeea5SJeff Kirsher 		goto out;
2186874aeea5SJeff Kirsher 	}
2187874aeea5SJeff Kirsher 
2188874aeea5SJeff Kirsher 	/* Clear flags for the scopes we covered.  We assume the NIC and
2189874aeea5SJeff Kirsher 	 * driver are now quiescent so that there is no race here.
2190874aeea5SJeff Kirsher 	 */
2191874aeea5SJeff Kirsher 	efx->reset_pending &= -(1 << (method + 1));
2192874aeea5SJeff Kirsher 
2193874aeea5SJeff Kirsher 	/* Reinitialise bus-mastering, which may have been turned off before
2194874aeea5SJeff Kirsher 	 * the reset was scheduled. This is still appropriate, even in the
2195874aeea5SJeff Kirsher 	 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2196874aeea5SJeff Kirsher 	 * can respond to requests. */
2197874aeea5SJeff Kirsher 	pci_set_master(efx->pci_dev);
2198874aeea5SJeff Kirsher 
2199874aeea5SJeff Kirsher out:
2200874aeea5SJeff Kirsher 	/* Leave device stopped if necessary */
2201874aeea5SJeff Kirsher 	disabled = rc || method == RESET_TYPE_DISABLE;
2202874aeea5SJeff Kirsher 	rc2 = efx_reset_up(efx, method, !disabled);
2203874aeea5SJeff Kirsher 	if (rc2) {
2204874aeea5SJeff Kirsher 		disabled = true;
2205874aeea5SJeff Kirsher 		if (!rc)
2206874aeea5SJeff Kirsher 			rc = rc2;
2207874aeea5SJeff Kirsher 	}
2208874aeea5SJeff Kirsher 
2209874aeea5SJeff Kirsher 	if (disabled) {
2210874aeea5SJeff Kirsher 		dev_close(efx->net_dev);
2211874aeea5SJeff Kirsher 		netif_err(efx, drv, efx->net_dev, "has been disabled\n");
2212874aeea5SJeff Kirsher 		efx->state = STATE_DISABLED;
2213874aeea5SJeff Kirsher 	} else {
2214874aeea5SJeff Kirsher 		netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
2215874aeea5SJeff Kirsher 		netif_device_attach(efx->net_dev);
2216874aeea5SJeff Kirsher 	}
2217874aeea5SJeff Kirsher 	return rc;
2218874aeea5SJeff Kirsher }
2219874aeea5SJeff Kirsher 
2220874aeea5SJeff Kirsher /* The worker thread exists so that code that cannot sleep can
2221874aeea5SJeff Kirsher  * schedule a reset for later.
2222874aeea5SJeff Kirsher  */
2223874aeea5SJeff Kirsher static void efx_reset_work(struct work_struct *data)
2224874aeea5SJeff Kirsher {
2225874aeea5SJeff Kirsher 	struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
2226874aeea5SJeff Kirsher 	unsigned long pending = ACCESS_ONCE(efx->reset_pending);
2227874aeea5SJeff Kirsher 
2228874aeea5SJeff Kirsher 	if (!pending)
2229874aeea5SJeff Kirsher 		return;
2230874aeea5SJeff Kirsher 
2231874aeea5SJeff Kirsher 	/* If we're not RUNNING then don't reset. Leave the reset_pending
2232874aeea5SJeff Kirsher 	 * flags set so that efx_pci_probe_main will be retried */
2233874aeea5SJeff Kirsher 	if (efx->state != STATE_RUNNING) {
2234874aeea5SJeff Kirsher 		netif_info(efx, drv, efx->net_dev,
2235874aeea5SJeff Kirsher 			   "scheduled reset quenched. NIC not RUNNING\n");
2236874aeea5SJeff Kirsher 		return;
2237874aeea5SJeff Kirsher 	}
2238874aeea5SJeff Kirsher 
2239874aeea5SJeff Kirsher 	rtnl_lock();
2240874aeea5SJeff Kirsher 	(void)efx_reset(efx, fls(pending) - 1);
2241874aeea5SJeff Kirsher 	rtnl_unlock();
2242874aeea5SJeff Kirsher }
2243874aeea5SJeff Kirsher 
2244874aeea5SJeff Kirsher void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2245874aeea5SJeff Kirsher {
2246874aeea5SJeff Kirsher 	enum reset_type method;
2247874aeea5SJeff Kirsher 
2248874aeea5SJeff Kirsher 	switch (type) {
2249874aeea5SJeff Kirsher 	case RESET_TYPE_INVISIBLE:
2250874aeea5SJeff Kirsher 	case RESET_TYPE_ALL:
2251874aeea5SJeff Kirsher 	case RESET_TYPE_WORLD:
2252874aeea5SJeff Kirsher 	case RESET_TYPE_DISABLE:
2253874aeea5SJeff Kirsher 		method = type;
2254874aeea5SJeff Kirsher 		netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2255874aeea5SJeff Kirsher 			  RESET_TYPE(method));
2256874aeea5SJeff Kirsher 		break;
2257874aeea5SJeff Kirsher 	default:
2258874aeea5SJeff Kirsher 		method = efx->type->map_reset_reason(type);
2259874aeea5SJeff Kirsher 		netif_dbg(efx, drv, efx->net_dev,
2260874aeea5SJeff Kirsher 			  "scheduling %s reset for %s\n",
2261874aeea5SJeff Kirsher 			  RESET_TYPE(method), RESET_TYPE(type));
2262874aeea5SJeff Kirsher 		break;
2263874aeea5SJeff Kirsher 	}
2264874aeea5SJeff Kirsher 
2265874aeea5SJeff Kirsher 	set_bit(method, &efx->reset_pending);
2266874aeea5SJeff Kirsher 
2267874aeea5SJeff Kirsher 	/* efx_process_channel() will no longer read events once a
2268874aeea5SJeff Kirsher 	 * reset is scheduled. So switch back to poll'd MCDI completions. */
2269874aeea5SJeff Kirsher 	efx_mcdi_mode_poll(efx);
2270874aeea5SJeff Kirsher 
2271874aeea5SJeff Kirsher 	queue_work(reset_workqueue, &efx->reset_work);
2272874aeea5SJeff Kirsher }
2273874aeea5SJeff Kirsher 
2274874aeea5SJeff Kirsher /**************************************************************************
2275874aeea5SJeff Kirsher  *
2276874aeea5SJeff Kirsher  * List of NICs we support
2277874aeea5SJeff Kirsher  *
2278874aeea5SJeff Kirsher  **************************************************************************/
2279874aeea5SJeff Kirsher 
2280874aeea5SJeff Kirsher /* PCI device ID table */
2281874aeea5SJeff Kirsher static DEFINE_PCI_DEVICE_TABLE(efx_pci_table) = {
22820e59e7e7SLinus Torvalds 	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
22830e59e7e7SLinus Torvalds 		    PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
2284874aeea5SJeff Kirsher 	 .driver_data = (unsigned long) &falcon_a1_nic_type},
22850e59e7e7SLinus Torvalds 	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
22860e59e7e7SLinus Torvalds 		    PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
2287874aeea5SJeff Kirsher 	 .driver_data = (unsigned long) &falcon_b0_nic_type},
2288547c474fSBen Hutchings 	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803),	/* SFC9020 */
2289874aeea5SJeff Kirsher 	 .driver_data = (unsigned long) &siena_a0_nic_type},
2290547c474fSBen Hutchings 	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813),	/* SFL9021 */
2291874aeea5SJeff Kirsher 	 .driver_data = (unsigned long) &siena_a0_nic_type},
2292874aeea5SJeff Kirsher 	{0}			/* end of list */
2293874aeea5SJeff Kirsher };
2294874aeea5SJeff Kirsher 
2295874aeea5SJeff Kirsher /**************************************************************************
2296874aeea5SJeff Kirsher  *
2297874aeea5SJeff Kirsher  * Dummy PHY/MAC operations
2298874aeea5SJeff Kirsher  *
2299874aeea5SJeff Kirsher  * Can be used for some unimplemented operations
2300874aeea5SJeff Kirsher  * Needed so all function pointers are valid and do not have to be tested
2301874aeea5SJeff Kirsher  * before use
2302874aeea5SJeff Kirsher  *
2303874aeea5SJeff Kirsher  **************************************************************************/
2304874aeea5SJeff Kirsher int efx_port_dummy_op_int(struct efx_nic *efx)
2305874aeea5SJeff Kirsher {
2306874aeea5SJeff Kirsher 	return 0;
2307874aeea5SJeff Kirsher }
2308874aeea5SJeff Kirsher void efx_port_dummy_op_void(struct efx_nic *efx) {}
2309874aeea5SJeff Kirsher 
2310874aeea5SJeff Kirsher static bool efx_port_dummy_op_poll(struct efx_nic *efx)
2311874aeea5SJeff Kirsher {
2312874aeea5SJeff Kirsher 	return false;
2313874aeea5SJeff Kirsher }
2314874aeea5SJeff Kirsher 
2315874aeea5SJeff Kirsher static const struct efx_phy_operations efx_dummy_phy_operations = {
2316874aeea5SJeff Kirsher 	.init		 = efx_port_dummy_op_int,
2317874aeea5SJeff Kirsher 	.reconfigure	 = efx_port_dummy_op_int,
2318874aeea5SJeff Kirsher 	.poll		 = efx_port_dummy_op_poll,
2319874aeea5SJeff Kirsher 	.fini		 = efx_port_dummy_op_void,
2320874aeea5SJeff Kirsher };
2321874aeea5SJeff Kirsher 
2322874aeea5SJeff Kirsher /**************************************************************************
2323874aeea5SJeff Kirsher  *
2324874aeea5SJeff Kirsher  * Data housekeeping
2325874aeea5SJeff Kirsher  *
2326874aeea5SJeff Kirsher  **************************************************************************/
2327874aeea5SJeff Kirsher 
2328874aeea5SJeff Kirsher /* This zeroes out and then fills in the invariants in a struct
2329874aeea5SJeff Kirsher  * efx_nic (including all sub-structures).
2330874aeea5SJeff Kirsher  */
2331874aeea5SJeff Kirsher static int efx_init_struct(struct efx_nic *efx, const struct efx_nic_type *type,
2332874aeea5SJeff Kirsher 			   struct pci_dev *pci_dev, struct net_device *net_dev)
2333874aeea5SJeff Kirsher {
2334874aeea5SJeff Kirsher 	int i;
2335874aeea5SJeff Kirsher 
2336874aeea5SJeff Kirsher 	/* Initialise common structures */
2337874aeea5SJeff Kirsher 	memset(efx, 0, sizeof(*efx));
2338874aeea5SJeff Kirsher 	spin_lock_init(&efx->biu_lock);
2339874aeea5SJeff Kirsher #ifdef CONFIG_SFC_MTD
2340874aeea5SJeff Kirsher 	INIT_LIST_HEAD(&efx->mtd_list);
2341874aeea5SJeff Kirsher #endif
2342874aeea5SJeff Kirsher 	INIT_WORK(&efx->reset_work, efx_reset_work);
2343874aeea5SJeff Kirsher 	INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
2344874aeea5SJeff Kirsher 	efx->pci_dev = pci_dev;
2345874aeea5SJeff Kirsher 	efx->msg_enable = debug;
2346874aeea5SJeff Kirsher 	efx->state = STATE_INIT;
2347874aeea5SJeff Kirsher 	strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
2348874aeea5SJeff Kirsher 
2349874aeea5SJeff Kirsher 	efx->net_dev = net_dev;
2350874aeea5SJeff Kirsher 	spin_lock_init(&efx->stats_lock);
2351874aeea5SJeff Kirsher 	mutex_init(&efx->mac_lock);
2352874aeea5SJeff Kirsher 	efx->phy_op = &efx_dummy_phy_operations;
2353874aeea5SJeff Kirsher 	efx->mdio.dev = net_dev;
2354874aeea5SJeff Kirsher 	INIT_WORK(&efx->mac_work, efx_mac_work);
23559f2cb71cSBen Hutchings 	init_waitqueue_head(&efx->flush_wq);
2356874aeea5SJeff Kirsher 
2357874aeea5SJeff Kirsher 	for (i = 0; i < EFX_MAX_CHANNELS; i++) {
2358874aeea5SJeff Kirsher 		efx->channel[i] = efx_alloc_channel(efx, i, NULL);
2359874aeea5SJeff Kirsher 		if (!efx->channel[i])
2360874aeea5SJeff Kirsher 			goto fail;
2361874aeea5SJeff Kirsher 	}
2362874aeea5SJeff Kirsher 
2363874aeea5SJeff Kirsher 	efx->type = type;
2364874aeea5SJeff Kirsher 
2365874aeea5SJeff Kirsher 	EFX_BUG_ON_PARANOID(efx->type->phys_addr_channels > EFX_MAX_CHANNELS);
2366874aeea5SJeff Kirsher 
2367874aeea5SJeff Kirsher 	/* Higher numbered interrupt modes are less capable! */
2368874aeea5SJeff Kirsher 	efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2369874aeea5SJeff Kirsher 				  interrupt_mode);
2370874aeea5SJeff Kirsher 
2371874aeea5SJeff Kirsher 	/* Would be good to use the net_dev name, but we're too early */
2372874aeea5SJeff Kirsher 	snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2373874aeea5SJeff Kirsher 		 pci_name(pci_dev));
2374874aeea5SJeff Kirsher 	efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
2375874aeea5SJeff Kirsher 	if (!efx->workqueue)
2376874aeea5SJeff Kirsher 		goto fail;
2377874aeea5SJeff Kirsher 
2378874aeea5SJeff Kirsher 	return 0;
2379874aeea5SJeff Kirsher 
2380874aeea5SJeff Kirsher fail:
2381874aeea5SJeff Kirsher 	efx_fini_struct(efx);
2382874aeea5SJeff Kirsher 	return -ENOMEM;
2383874aeea5SJeff Kirsher }
2384874aeea5SJeff Kirsher 
2385874aeea5SJeff Kirsher static void efx_fini_struct(struct efx_nic *efx)
2386874aeea5SJeff Kirsher {
2387874aeea5SJeff Kirsher 	int i;
2388874aeea5SJeff Kirsher 
2389874aeea5SJeff Kirsher 	for (i = 0; i < EFX_MAX_CHANNELS; i++)
2390874aeea5SJeff Kirsher 		kfree(efx->channel[i]);
2391874aeea5SJeff Kirsher 
2392874aeea5SJeff Kirsher 	if (efx->workqueue) {
2393874aeea5SJeff Kirsher 		destroy_workqueue(efx->workqueue);
2394874aeea5SJeff Kirsher 		efx->workqueue = NULL;
2395874aeea5SJeff Kirsher 	}
2396874aeea5SJeff Kirsher }
2397874aeea5SJeff Kirsher 
2398874aeea5SJeff Kirsher /**************************************************************************
2399874aeea5SJeff Kirsher  *
2400874aeea5SJeff Kirsher  * PCI interface
2401874aeea5SJeff Kirsher  *
2402874aeea5SJeff Kirsher  **************************************************************************/
2403874aeea5SJeff Kirsher 
2404874aeea5SJeff Kirsher /* Main body of final NIC shutdown code
2405874aeea5SJeff Kirsher  * This is called only at module unload (or hotplug removal).
2406874aeea5SJeff Kirsher  */
2407874aeea5SJeff Kirsher static void efx_pci_remove_main(struct efx_nic *efx)
2408874aeea5SJeff Kirsher {
2409874aeea5SJeff Kirsher #ifdef CONFIG_RFS_ACCEL
2410874aeea5SJeff Kirsher 	free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
2411874aeea5SJeff Kirsher 	efx->net_dev->rx_cpu_rmap = NULL;
2412874aeea5SJeff Kirsher #endif
24137f967c01SBen Hutchings 	efx_stop_interrupts(efx, false);
2414874aeea5SJeff Kirsher 	efx_nic_fini_interrupt(efx);
2415874aeea5SJeff Kirsher 	efx_fini_port(efx);
2416874aeea5SJeff Kirsher 	efx->type->fini(efx);
2417874aeea5SJeff Kirsher 	efx_fini_napi(efx);
2418874aeea5SJeff Kirsher 	efx_remove_all(efx);
2419874aeea5SJeff Kirsher }
2420874aeea5SJeff Kirsher 
2421874aeea5SJeff Kirsher /* Final NIC shutdown
2422874aeea5SJeff Kirsher  * This is called only at module unload (or hotplug removal).
2423874aeea5SJeff Kirsher  */
2424874aeea5SJeff Kirsher static void efx_pci_remove(struct pci_dev *pci_dev)
2425874aeea5SJeff Kirsher {
2426874aeea5SJeff Kirsher 	struct efx_nic *efx;
2427874aeea5SJeff Kirsher 
2428874aeea5SJeff Kirsher 	efx = pci_get_drvdata(pci_dev);
2429874aeea5SJeff Kirsher 	if (!efx)
2430874aeea5SJeff Kirsher 		return;
2431874aeea5SJeff Kirsher 
2432874aeea5SJeff Kirsher 	/* Mark the NIC as fini, then stop the interface */
2433874aeea5SJeff Kirsher 	rtnl_lock();
2434874aeea5SJeff Kirsher 	efx->state = STATE_FINI;
2435874aeea5SJeff Kirsher 	dev_close(efx->net_dev);
2436874aeea5SJeff Kirsher 
2437874aeea5SJeff Kirsher 	/* Allow any queued efx_resets() to complete */
2438874aeea5SJeff Kirsher 	rtnl_unlock();
2439874aeea5SJeff Kirsher 
24407f967c01SBen Hutchings 	efx_stop_interrupts(efx, false);
2441874aeea5SJeff Kirsher 	efx_unregister_netdev(efx);
2442874aeea5SJeff Kirsher 
2443874aeea5SJeff Kirsher 	efx_mtd_remove(efx);
2444874aeea5SJeff Kirsher 
2445874aeea5SJeff Kirsher 	/* Wait for any scheduled resets to complete. No more will be
2446874aeea5SJeff Kirsher 	 * scheduled from this point because efx_stop_all() has been
2447874aeea5SJeff Kirsher 	 * called, we are no longer registered with driverlink, and
2448874aeea5SJeff Kirsher 	 * the net_device's have been removed. */
2449874aeea5SJeff Kirsher 	cancel_work_sync(&efx->reset_work);
2450874aeea5SJeff Kirsher 
2451874aeea5SJeff Kirsher 	efx_pci_remove_main(efx);
2452874aeea5SJeff Kirsher 
2453874aeea5SJeff Kirsher 	efx_fini_io(efx);
2454874aeea5SJeff Kirsher 	netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
2455874aeea5SJeff Kirsher 
2456874aeea5SJeff Kirsher 	pci_set_drvdata(pci_dev, NULL);
2457874aeea5SJeff Kirsher 	efx_fini_struct(efx);
2458874aeea5SJeff Kirsher 	free_netdev(efx->net_dev);
2459874aeea5SJeff Kirsher };
2460874aeea5SJeff Kirsher 
2461874aeea5SJeff Kirsher /* Main body of NIC initialisation
2462874aeea5SJeff Kirsher  * This is called at module load (or hotplug insertion, theoretically).
2463874aeea5SJeff Kirsher  */
2464874aeea5SJeff Kirsher static int efx_pci_probe_main(struct efx_nic *efx)
2465874aeea5SJeff Kirsher {
2466874aeea5SJeff Kirsher 	int rc;
2467874aeea5SJeff Kirsher 
2468874aeea5SJeff Kirsher 	/* Do start-of-day initialisation */
2469874aeea5SJeff Kirsher 	rc = efx_probe_all(efx);
2470874aeea5SJeff Kirsher 	if (rc)
2471874aeea5SJeff Kirsher 		goto fail1;
2472874aeea5SJeff Kirsher 
2473874aeea5SJeff Kirsher 	efx_init_napi(efx);
2474874aeea5SJeff Kirsher 
2475874aeea5SJeff Kirsher 	rc = efx->type->init(efx);
2476874aeea5SJeff Kirsher 	if (rc) {
2477874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev,
2478874aeea5SJeff Kirsher 			  "failed to initialise NIC\n");
2479874aeea5SJeff Kirsher 		goto fail3;
2480874aeea5SJeff Kirsher 	}
2481874aeea5SJeff Kirsher 
2482874aeea5SJeff Kirsher 	rc = efx_init_port(efx);
2483874aeea5SJeff Kirsher 	if (rc) {
2484874aeea5SJeff Kirsher 		netif_err(efx, probe, efx->net_dev,
2485874aeea5SJeff Kirsher 			  "failed to initialise port\n");
2486874aeea5SJeff Kirsher 		goto fail4;
2487874aeea5SJeff Kirsher 	}
2488874aeea5SJeff Kirsher 
2489874aeea5SJeff Kirsher 	rc = efx_nic_init_interrupt(efx);
2490874aeea5SJeff Kirsher 	if (rc)
2491874aeea5SJeff Kirsher 		goto fail5;
24927f967c01SBen Hutchings 	efx_start_interrupts(efx, false);
2493874aeea5SJeff Kirsher 
2494874aeea5SJeff Kirsher 	return 0;
2495874aeea5SJeff Kirsher 
2496874aeea5SJeff Kirsher  fail5:
2497874aeea5SJeff Kirsher 	efx_fini_port(efx);
2498874aeea5SJeff Kirsher  fail4:
2499874aeea5SJeff Kirsher 	efx->type->fini(efx);
2500874aeea5SJeff Kirsher  fail3:
2501874aeea5SJeff Kirsher 	efx_fini_napi(efx);
2502874aeea5SJeff Kirsher 	efx_remove_all(efx);
2503874aeea5SJeff Kirsher  fail1:
2504874aeea5SJeff Kirsher 	return rc;
2505874aeea5SJeff Kirsher }
2506874aeea5SJeff Kirsher 
2507874aeea5SJeff Kirsher /* NIC initialisation
2508874aeea5SJeff Kirsher  *
2509874aeea5SJeff Kirsher  * This is called at module load (or hotplug insertion,
251073ba7b68SBen Hutchings  * theoretically).  It sets up PCI mappings, resets the NIC,
2511874aeea5SJeff Kirsher  * sets up and registers the network devices with the kernel and hooks
2512874aeea5SJeff Kirsher  * the interrupt service routine.  It does not prepare the device for
2513874aeea5SJeff Kirsher  * transmission; this is left to the first time one of the network
2514874aeea5SJeff Kirsher  * interfaces is brought up (i.e. efx_net_open).
2515874aeea5SJeff Kirsher  */
2516874aeea5SJeff Kirsher static int __devinit efx_pci_probe(struct pci_dev *pci_dev,
2517874aeea5SJeff Kirsher 				   const struct pci_device_id *entry)
2518874aeea5SJeff Kirsher {
2519874aeea5SJeff Kirsher 	const struct efx_nic_type *type = (const struct efx_nic_type *) entry->driver_data;
2520874aeea5SJeff Kirsher 	struct net_device *net_dev;
2521874aeea5SJeff Kirsher 	struct efx_nic *efx;
2522fadac6aaSBen Hutchings 	int rc;
2523874aeea5SJeff Kirsher 
2524874aeea5SJeff Kirsher 	/* Allocate and initialise a struct net_device and struct efx_nic */
2525874aeea5SJeff Kirsher 	net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
2526874aeea5SJeff Kirsher 				     EFX_MAX_RX_QUEUES);
2527874aeea5SJeff Kirsher 	if (!net_dev)
2528874aeea5SJeff Kirsher 		return -ENOMEM;
2529874aeea5SJeff Kirsher 	net_dev->features |= (type->offload_features | NETIF_F_SG |
2530874aeea5SJeff Kirsher 			      NETIF_F_HIGHDMA | NETIF_F_TSO |
2531874aeea5SJeff Kirsher 			      NETIF_F_RXCSUM);
2532874aeea5SJeff Kirsher 	if (type->offload_features & NETIF_F_V6_CSUM)
2533874aeea5SJeff Kirsher 		net_dev->features |= NETIF_F_TSO6;
2534874aeea5SJeff Kirsher 	/* Mask for features that also apply to VLAN devices */
2535874aeea5SJeff Kirsher 	net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
2536874aeea5SJeff Kirsher 				   NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
2537874aeea5SJeff Kirsher 				   NETIF_F_RXCSUM);
2538874aeea5SJeff Kirsher 	/* All offloads can be toggled */
2539874aeea5SJeff Kirsher 	net_dev->hw_features = net_dev->features & ~NETIF_F_HIGHDMA;
2540874aeea5SJeff Kirsher 	efx = netdev_priv(net_dev);
2541874aeea5SJeff Kirsher 	pci_set_drvdata(pci_dev, efx);
2542874aeea5SJeff Kirsher 	SET_NETDEV_DEV(net_dev, &pci_dev->dev);
2543874aeea5SJeff Kirsher 	rc = efx_init_struct(efx, type, pci_dev, net_dev);
2544874aeea5SJeff Kirsher 	if (rc)
2545874aeea5SJeff Kirsher 		goto fail1;
2546874aeea5SJeff Kirsher 
2547874aeea5SJeff Kirsher 	netif_info(efx, probe, efx->net_dev,
2548874aeea5SJeff Kirsher 		   "Solarflare NIC detected\n");
2549874aeea5SJeff Kirsher 
2550874aeea5SJeff Kirsher 	/* Set up basic I/O (BAR mappings etc) */
2551874aeea5SJeff Kirsher 	rc = efx_init_io(efx);
2552874aeea5SJeff Kirsher 	if (rc)
2553874aeea5SJeff Kirsher 		goto fail2;
2554874aeea5SJeff Kirsher 
2555874aeea5SJeff Kirsher 	rc = efx_pci_probe_main(efx);
2556874aeea5SJeff Kirsher 
2557874aeea5SJeff Kirsher 	/* Serialise against efx_reset(). No more resets will be
2558fadac6aaSBen Hutchings 	 * scheduled since efx_stop_all() has been called, and we have
2559fadac6aaSBen Hutchings 	 * not and never have been registered.
2560fadac6aaSBen Hutchings 	 */
2561874aeea5SJeff Kirsher 	cancel_work_sync(&efx->reset_work);
2562874aeea5SJeff Kirsher 
2563fadac6aaSBen Hutchings 	if (rc)
2564874aeea5SJeff Kirsher 		goto fail3;
2565874aeea5SJeff Kirsher 
2566fadac6aaSBen Hutchings 	/* If there was a scheduled reset during probe, the NIC is
2567fadac6aaSBen Hutchings 	 * probably hosed anyway.
2568fadac6aaSBen Hutchings 	 */
2569fadac6aaSBen Hutchings 	if (efx->reset_pending) {
2570fadac6aaSBen Hutchings 		rc = -EIO;
2571874aeea5SJeff Kirsher 		goto fail4;
2572874aeea5SJeff Kirsher 	}
2573874aeea5SJeff Kirsher 
2574874aeea5SJeff Kirsher 	/* Switch to the running state before we expose the device to the OS,
2575874aeea5SJeff Kirsher 	 * so that dev_open()|efx_start_all() will actually start the device */
2576874aeea5SJeff Kirsher 	efx->state = STATE_RUNNING;
2577874aeea5SJeff Kirsher 
2578874aeea5SJeff Kirsher 	rc = efx_register_netdev(efx);
2579874aeea5SJeff Kirsher 	if (rc)
2580fadac6aaSBen Hutchings 		goto fail4;
2581874aeea5SJeff Kirsher 
2582874aeea5SJeff Kirsher 	netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
2583874aeea5SJeff Kirsher 
25847c43161cSBen Hutchings 	/* Try to create MTDs, but allow this to fail */
2585874aeea5SJeff Kirsher 	rtnl_lock();
25867c43161cSBen Hutchings 	rc = efx_mtd_probe(efx);
2587874aeea5SJeff Kirsher 	rtnl_unlock();
25887c43161cSBen Hutchings 	if (rc)
25897c43161cSBen Hutchings 		netif_warn(efx, probe, efx->net_dev,
25907c43161cSBen Hutchings 			   "failed to create MTDs (%d)\n", rc);
25917c43161cSBen Hutchings 
2592874aeea5SJeff Kirsher 	return 0;
2593874aeea5SJeff Kirsher 
2594874aeea5SJeff Kirsher  fail4:
2595fadac6aaSBen Hutchings 	efx_pci_remove_main(efx);
2596874aeea5SJeff Kirsher  fail3:
2597874aeea5SJeff Kirsher 	efx_fini_io(efx);
2598874aeea5SJeff Kirsher  fail2:
2599874aeea5SJeff Kirsher 	efx_fini_struct(efx);
2600874aeea5SJeff Kirsher  fail1:
2601874aeea5SJeff Kirsher 	WARN_ON(rc > 0);
2602874aeea5SJeff Kirsher 	netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
2603874aeea5SJeff Kirsher 	free_netdev(net_dev);
2604874aeea5SJeff Kirsher 	return rc;
2605874aeea5SJeff Kirsher }
2606874aeea5SJeff Kirsher 
2607874aeea5SJeff Kirsher static int efx_pm_freeze(struct device *dev)
2608874aeea5SJeff Kirsher {
2609874aeea5SJeff Kirsher 	struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2610874aeea5SJeff Kirsher 
2611874aeea5SJeff Kirsher 	efx->state = STATE_FINI;
2612874aeea5SJeff Kirsher 
2613874aeea5SJeff Kirsher 	netif_device_detach(efx->net_dev);
2614874aeea5SJeff Kirsher 
2615874aeea5SJeff Kirsher 	efx_stop_all(efx);
26167f967c01SBen Hutchings 	efx_stop_interrupts(efx, false);
2617874aeea5SJeff Kirsher 
2618874aeea5SJeff Kirsher 	return 0;
2619874aeea5SJeff Kirsher }
2620874aeea5SJeff Kirsher 
2621874aeea5SJeff Kirsher static int efx_pm_thaw(struct device *dev)
2622874aeea5SJeff Kirsher {
2623874aeea5SJeff Kirsher 	struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2624874aeea5SJeff Kirsher 
2625874aeea5SJeff Kirsher 	efx->state = STATE_INIT;
2626874aeea5SJeff Kirsher 
26277f967c01SBen Hutchings 	efx_start_interrupts(efx, false);
2628874aeea5SJeff Kirsher 
2629874aeea5SJeff Kirsher 	mutex_lock(&efx->mac_lock);
2630874aeea5SJeff Kirsher 	efx->phy_op->reconfigure(efx);
2631874aeea5SJeff Kirsher 	mutex_unlock(&efx->mac_lock);
2632874aeea5SJeff Kirsher 
2633874aeea5SJeff Kirsher 	efx_start_all(efx);
2634874aeea5SJeff Kirsher 
2635874aeea5SJeff Kirsher 	netif_device_attach(efx->net_dev);
2636874aeea5SJeff Kirsher 
2637874aeea5SJeff Kirsher 	efx->state = STATE_RUNNING;
2638874aeea5SJeff Kirsher 
2639874aeea5SJeff Kirsher 	efx->type->resume_wol(efx);
2640874aeea5SJeff Kirsher 
2641874aeea5SJeff Kirsher 	/* Reschedule any quenched resets scheduled during efx_pm_freeze() */
2642874aeea5SJeff Kirsher 	queue_work(reset_workqueue, &efx->reset_work);
2643874aeea5SJeff Kirsher 
2644874aeea5SJeff Kirsher 	return 0;
2645874aeea5SJeff Kirsher }
2646874aeea5SJeff Kirsher 
2647874aeea5SJeff Kirsher static int efx_pm_poweroff(struct device *dev)
2648874aeea5SJeff Kirsher {
2649874aeea5SJeff Kirsher 	struct pci_dev *pci_dev = to_pci_dev(dev);
2650874aeea5SJeff Kirsher 	struct efx_nic *efx = pci_get_drvdata(pci_dev);
2651874aeea5SJeff Kirsher 
2652874aeea5SJeff Kirsher 	efx->type->fini(efx);
2653874aeea5SJeff Kirsher 
2654874aeea5SJeff Kirsher 	efx->reset_pending = 0;
2655874aeea5SJeff Kirsher 
2656874aeea5SJeff Kirsher 	pci_save_state(pci_dev);
2657874aeea5SJeff Kirsher 	return pci_set_power_state(pci_dev, PCI_D3hot);
2658874aeea5SJeff Kirsher }
2659874aeea5SJeff Kirsher 
2660874aeea5SJeff Kirsher /* Used for both resume and restore */
2661874aeea5SJeff Kirsher static int efx_pm_resume(struct device *dev)
2662874aeea5SJeff Kirsher {
2663874aeea5SJeff Kirsher 	struct pci_dev *pci_dev = to_pci_dev(dev);
2664874aeea5SJeff Kirsher 	struct efx_nic *efx = pci_get_drvdata(pci_dev);
2665874aeea5SJeff Kirsher 	int rc;
2666874aeea5SJeff Kirsher 
2667874aeea5SJeff Kirsher 	rc = pci_set_power_state(pci_dev, PCI_D0);
2668874aeea5SJeff Kirsher 	if (rc)
2669874aeea5SJeff Kirsher 		return rc;
2670874aeea5SJeff Kirsher 	pci_restore_state(pci_dev);
2671874aeea5SJeff Kirsher 	rc = pci_enable_device(pci_dev);
2672874aeea5SJeff Kirsher 	if (rc)
2673874aeea5SJeff Kirsher 		return rc;
2674874aeea5SJeff Kirsher 	pci_set_master(efx->pci_dev);
2675874aeea5SJeff Kirsher 	rc = efx->type->reset(efx, RESET_TYPE_ALL);
2676874aeea5SJeff Kirsher 	if (rc)
2677874aeea5SJeff Kirsher 		return rc;
2678874aeea5SJeff Kirsher 	rc = efx->type->init(efx);
2679874aeea5SJeff Kirsher 	if (rc)
2680874aeea5SJeff Kirsher 		return rc;
2681874aeea5SJeff Kirsher 	efx_pm_thaw(dev);
2682874aeea5SJeff Kirsher 	return 0;
2683874aeea5SJeff Kirsher }
2684874aeea5SJeff Kirsher 
2685874aeea5SJeff Kirsher static int efx_pm_suspend(struct device *dev)
2686874aeea5SJeff Kirsher {
2687874aeea5SJeff Kirsher 	int rc;
2688874aeea5SJeff Kirsher 
2689874aeea5SJeff Kirsher 	efx_pm_freeze(dev);
2690874aeea5SJeff Kirsher 	rc = efx_pm_poweroff(dev);
2691874aeea5SJeff Kirsher 	if (rc)
2692874aeea5SJeff Kirsher 		efx_pm_resume(dev);
2693874aeea5SJeff Kirsher 	return rc;
2694874aeea5SJeff Kirsher }
2695874aeea5SJeff Kirsher 
269618e83e4cSBen Hutchings static const struct dev_pm_ops efx_pm_ops = {
2697874aeea5SJeff Kirsher 	.suspend	= efx_pm_suspend,
2698874aeea5SJeff Kirsher 	.resume		= efx_pm_resume,
2699874aeea5SJeff Kirsher 	.freeze		= efx_pm_freeze,
2700874aeea5SJeff Kirsher 	.thaw		= efx_pm_thaw,
2701874aeea5SJeff Kirsher 	.poweroff	= efx_pm_poweroff,
2702874aeea5SJeff Kirsher 	.restore	= efx_pm_resume,
2703874aeea5SJeff Kirsher };
2704874aeea5SJeff Kirsher 
2705874aeea5SJeff Kirsher static struct pci_driver efx_pci_driver = {
2706874aeea5SJeff Kirsher 	.name		= KBUILD_MODNAME,
2707874aeea5SJeff Kirsher 	.id_table	= efx_pci_table,
2708874aeea5SJeff Kirsher 	.probe		= efx_pci_probe,
2709874aeea5SJeff Kirsher 	.remove		= efx_pci_remove,
2710874aeea5SJeff Kirsher 	.driver.pm	= &efx_pm_ops,
2711874aeea5SJeff Kirsher };
2712874aeea5SJeff Kirsher 
2713874aeea5SJeff Kirsher /**************************************************************************
2714874aeea5SJeff Kirsher  *
2715874aeea5SJeff Kirsher  * Kernel module interface
2716874aeea5SJeff Kirsher  *
2717874aeea5SJeff Kirsher  *************************************************************************/
2718874aeea5SJeff Kirsher 
2719874aeea5SJeff Kirsher module_param(interrupt_mode, uint, 0444);
2720874aeea5SJeff Kirsher MODULE_PARM_DESC(interrupt_mode,
2721874aeea5SJeff Kirsher 		 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
2722874aeea5SJeff Kirsher 
2723874aeea5SJeff Kirsher static int __init efx_init_module(void)
2724874aeea5SJeff Kirsher {
2725874aeea5SJeff Kirsher 	int rc;
2726874aeea5SJeff Kirsher 
2727874aeea5SJeff Kirsher 	printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
2728874aeea5SJeff Kirsher 
2729874aeea5SJeff Kirsher 	rc = register_netdevice_notifier(&efx_netdev_notifier);
2730874aeea5SJeff Kirsher 	if (rc)
2731874aeea5SJeff Kirsher 		goto err_notifier;
2732874aeea5SJeff Kirsher 
2733874aeea5SJeff Kirsher 	reset_workqueue = create_singlethread_workqueue("sfc_reset");
2734874aeea5SJeff Kirsher 	if (!reset_workqueue) {
2735874aeea5SJeff Kirsher 		rc = -ENOMEM;
2736874aeea5SJeff Kirsher 		goto err_reset;
2737874aeea5SJeff Kirsher 	}
2738874aeea5SJeff Kirsher 
2739874aeea5SJeff Kirsher 	rc = pci_register_driver(&efx_pci_driver);
2740874aeea5SJeff Kirsher 	if (rc < 0)
2741874aeea5SJeff Kirsher 		goto err_pci;
2742874aeea5SJeff Kirsher 
2743874aeea5SJeff Kirsher 	return 0;
2744874aeea5SJeff Kirsher 
2745874aeea5SJeff Kirsher  err_pci:
2746874aeea5SJeff Kirsher 	destroy_workqueue(reset_workqueue);
2747874aeea5SJeff Kirsher  err_reset:
2748874aeea5SJeff Kirsher 	unregister_netdevice_notifier(&efx_netdev_notifier);
2749874aeea5SJeff Kirsher  err_notifier:
2750874aeea5SJeff Kirsher 	return rc;
2751874aeea5SJeff Kirsher }
2752874aeea5SJeff Kirsher 
2753874aeea5SJeff Kirsher static void __exit efx_exit_module(void)
2754874aeea5SJeff Kirsher {
2755874aeea5SJeff Kirsher 	printk(KERN_INFO "Solarflare NET driver unloading\n");
2756874aeea5SJeff Kirsher 
2757874aeea5SJeff Kirsher 	pci_unregister_driver(&efx_pci_driver);
2758874aeea5SJeff Kirsher 	destroy_workqueue(reset_workqueue);
2759874aeea5SJeff Kirsher 	unregister_netdevice_notifier(&efx_netdev_notifier);
2760874aeea5SJeff Kirsher 
2761874aeea5SJeff Kirsher }
2762874aeea5SJeff Kirsher 
2763874aeea5SJeff Kirsher module_init(efx_init_module);
2764874aeea5SJeff Kirsher module_exit(efx_exit_module);
2765874aeea5SJeff Kirsher 
2766874aeea5SJeff Kirsher MODULE_AUTHOR("Solarflare Communications and "
2767874aeea5SJeff Kirsher 	      "Michael Brown <mbrown@fensystems.co.uk>");
2768874aeea5SJeff Kirsher MODULE_DESCRIPTION("Solarflare Communications network driver");
2769874aeea5SJeff Kirsher MODULE_LICENSE("GPL");
2770874aeea5SJeff Kirsher MODULE_DEVICE_TABLE(pci, efx_pci_table);
2771