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