xref: /linux/drivers/net/ethernet/sfc/efx.c (revision 995231c820e3bd3633cb38bf4ea6f2541e1da331)
1 /****************************************************************************
2  * Driver for Solarflare network controllers and boards
3  * Copyright 2005-2006 Fen Systems Ltd.
4  * Copyright 2005-2013 Solarflare Communications Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 as published
8  * by the Free Software Foundation, incorporated herein by reference.
9  */
10 
11 #include <linux/module.h>
12 #include <linux/pci.h>
13 #include <linux/netdevice.h>
14 #include <linux/etherdevice.h>
15 #include <linux/delay.h>
16 #include <linux/notifier.h>
17 #include <linux/ip.h>
18 #include <linux/tcp.h>
19 #include <linux/in.h>
20 #include <linux/ethtool.h>
21 #include <linux/topology.h>
22 #include <linux/gfp.h>
23 #include <linux/aer.h>
24 #include <linux/interrupt.h>
25 #include "net_driver.h"
26 #include <net/gre.h>
27 #include <net/udp_tunnel.h>
28 #include "efx.h"
29 #include "nic.h"
30 #include "selftest.h"
31 #include "sriov.h"
32 
33 #include "mcdi.h"
34 #include "mcdi_pcol.h"
35 #include "workarounds.h"
36 
37 /**************************************************************************
38  *
39  * Type name strings
40  *
41  **************************************************************************
42  */
43 
44 /* Loopback mode names (see LOOPBACK_MODE()) */
45 const unsigned int efx_loopback_mode_max = LOOPBACK_MAX;
46 const char *const efx_loopback_mode_names[] = {
47 	[LOOPBACK_NONE]		= "NONE",
48 	[LOOPBACK_DATA]		= "DATAPATH",
49 	[LOOPBACK_GMAC]		= "GMAC",
50 	[LOOPBACK_XGMII]	= "XGMII",
51 	[LOOPBACK_XGXS]		= "XGXS",
52 	[LOOPBACK_XAUI]		= "XAUI",
53 	[LOOPBACK_GMII]		= "GMII",
54 	[LOOPBACK_SGMII]	= "SGMII",
55 	[LOOPBACK_XGBR]		= "XGBR",
56 	[LOOPBACK_XFI]		= "XFI",
57 	[LOOPBACK_XAUI_FAR]	= "XAUI_FAR",
58 	[LOOPBACK_GMII_FAR]	= "GMII_FAR",
59 	[LOOPBACK_SGMII_FAR]	= "SGMII_FAR",
60 	[LOOPBACK_XFI_FAR]	= "XFI_FAR",
61 	[LOOPBACK_GPHY]		= "GPHY",
62 	[LOOPBACK_PHYXS]	= "PHYXS",
63 	[LOOPBACK_PCS]		= "PCS",
64 	[LOOPBACK_PMAPMD]	= "PMA/PMD",
65 	[LOOPBACK_XPORT]	= "XPORT",
66 	[LOOPBACK_XGMII_WS]	= "XGMII_WS",
67 	[LOOPBACK_XAUI_WS]	= "XAUI_WS",
68 	[LOOPBACK_XAUI_WS_FAR]  = "XAUI_WS_FAR",
69 	[LOOPBACK_XAUI_WS_NEAR] = "XAUI_WS_NEAR",
70 	[LOOPBACK_GMII_WS]	= "GMII_WS",
71 	[LOOPBACK_XFI_WS]	= "XFI_WS",
72 	[LOOPBACK_XFI_WS_FAR]	= "XFI_WS_FAR",
73 	[LOOPBACK_PHYXS_WS]	= "PHYXS_WS",
74 };
75 
76 const unsigned int efx_reset_type_max = RESET_TYPE_MAX;
77 const char *const efx_reset_type_names[] = {
78 	[RESET_TYPE_INVISIBLE]          = "INVISIBLE",
79 	[RESET_TYPE_ALL]                = "ALL",
80 	[RESET_TYPE_RECOVER_OR_ALL]     = "RECOVER_OR_ALL",
81 	[RESET_TYPE_WORLD]              = "WORLD",
82 	[RESET_TYPE_RECOVER_OR_DISABLE] = "RECOVER_OR_DISABLE",
83 	[RESET_TYPE_DATAPATH]           = "DATAPATH",
84 	[RESET_TYPE_MC_BIST]		= "MC_BIST",
85 	[RESET_TYPE_DISABLE]            = "DISABLE",
86 	[RESET_TYPE_TX_WATCHDOG]        = "TX_WATCHDOG",
87 	[RESET_TYPE_INT_ERROR]          = "INT_ERROR",
88 	[RESET_TYPE_DMA_ERROR]          = "DMA_ERROR",
89 	[RESET_TYPE_TX_SKIP]            = "TX_SKIP",
90 	[RESET_TYPE_MC_FAILURE]         = "MC_FAILURE",
91 	[RESET_TYPE_MCDI_TIMEOUT]	= "MCDI_TIMEOUT (FLR)",
92 };
93 
94 /* UDP tunnel type names */
95 static const char *const efx_udp_tunnel_type_names[] = {
96 	[TUNNEL_ENCAP_UDP_PORT_ENTRY_VXLAN] = "vxlan",
97 	[TUNNEL_ENCAP_UDP_PORT_ENTRY_GENEVE] = "geneve",
98 };
99 
100 void efx_get_udp_tunnel_type_name(u16 type, char *buf, size_t buflen)
101 {
102 	if (type < ARRAY_SIZE(efx_udp_tunnel_type_names) &&
103 	    efx_udp_tunnel_type_names[type] != NULL)
104 		snprintf(buf, buflen, "%s", efx_udp_tunnel_type_names[type]);
105 	else
106 		snprintf(buf, buflen, "type %d", type);
107 }
108 
109 /* Reset workqueue. If any NIC has a hardware failure then a reset will be
110  * queued onto this work queue. This is not a per-nic work queue, because
111  * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
112  */
113 static struct workqueue_struct *reset_workqueue;
114 
115 /* How often and how many times to poll for a reset while waiting for a
116  * BIST that another function started to complete.
117  */
118 #define BIST_WAIT_DELAY_MS	100
119 #define BIST_WAIT_DELAY_COUNT	100
120 
121 /**************************************************************************
122  *
123  * Configurable values
124  *
125  *************************************************************************/
126 
127 /*
128  * Use separate channels for TX and RX events
129  *
130  * Set this to 1 to use separate channels for TX and RX. It allows us
131  * to control interrupt affinity separately for TX and RX.
132  *
133  * This is only used in MSI-X interrupt mode
134  */
135 bool efx_separate_tx_channels;
136 module_param(efx_separate_tx_channels, bool, 0444);
137 MODULE_PARM_DESC(efx_separate_tx_channels,
138 		 "Use separate channels for TX and RX");
139 
140 /* This is the weight assigned to each of the (per-channel) virtual
141  * NAPI devices.
142  */
143 static int napi_weight = 64;
144 
145 /* This is the time (in jiffies) between invocations of the hardware
146  * monitor.
147  * On Falcon-based NICs, this will:
148  * - Check the on-board hardware monitor;
149  * - Poll the link state and reconfigure the hardware as necessary.
150  * On Siena-based NICs for power systems with EEH support, this will give EEH a
151  * chance to start.
152  */
153 static unsigned int efx_monitor_interval = 1 * HZ;
154 
155 /* Initial interrupt moderation settings.  They can be modified after
156  * module load with ethtool.
157  *
158  * The default for RX should strike a balance between increasing the
159  * round-trip latency and reducing overhead.
160  */
161 static unsigned int rx_irq_mod_usec = 60;
162 
163 /* Initial interrupt moderation settings.  They can be modified after
164  * module load with ethtool.
165  *
166  * This default is chosen to ensure that a 10G link does not go idle
167  * while a TX queue is stopped after it has become full.  A queue is
168  * restarted when it drops below half full.  The time this takes (assuming
169  * worst case 3 descriptors per packet and 1024 descriptors) is
170  *   512 / 3 * 1.2 = 205 usec.
171  */
172 static unsigned int tx_irq_mod_usec = 150;
173 
174 /* This is the first interrupt mode to try out of:
175  * 0 => MSI-X
176  * 1 => MSI
177  * 2 => legacy
178  */
179 static unsigned int interrupt_mode;
180 
181 /* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
182  * i.e. the number of CPUs among which we may distribute simultaneous
183  * interrupt handling.
184  *
185  * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
186  * The default (0) means to assign an interrupt to each core.
187  */
188 static unsigned int rss_cpus;
189 module_param(rss_cpus, uint, 0444);
190 MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
191 
192 static bool phy_flash_cfg;
193 module_param(phy_flash_cfg, bool, 0644);
194 MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
195 
196 static unsigned irq_adapt_low_thresh = 8000;
197 module_param(irq_adapt_low_thresh, uint, 0644);
198 MODULE_PARM_DESC(irq_adapt_low_thresh,
199 		 "Threshold score for reducing IRQ moderation");
200 
201 static unsigned irq_adapt_high_thresh = 16000;
202 module_param(irq_adapt_high_thresh, uint, 0644);
203 MODULE_PARM_DESC(irq_adapt_high_thresh,
204 		 "Threshold score for increasing IRQ moderation");
205 
206 static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
207 			 NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
208 			 NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
209 			 NETIF_MSG_TX_ERR | NETIF_MSG_HW);
210 module_param(debug, uint, 0);
211 MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
212 
213 /**************************************************************************
214  *
215  * Utility functions and prototypes
216  *
217  *************************************************************************/
218 
219 static int efx_soft_enable_interrupts(struct efx_nic *efx);
220 static void efx_soft_disable_interrupts(struct efx_nic *efx);
221 static void efx_remove_channel(struct efx_channel *channel);
222 static void efx_remove_channels(struct efx_nic *efx);
223 static const struct efx_channel_type efx_default_channel_type;
224 static void efx_remove_port(struct efx_nic *efx);
225 static void efx_init_napi_channel(struct efx_channel *channel);
226 static void efx_fini_napi(struct efx_nic *efx);
227 static void efx_fini_napi_channel(struct efx_channel *channel);
228 static void efx_fini_struct(struct efx_nic *efx);
229 static void efx_start_all(struct efx_nic *efx);
230 static void efx_stop_all(struct efx_nic *efx);
231 
232 #define EFX_ASSERT_RESET_SERIALISED(efx)		\
233 	do {						\
234 		if ((efx->state == STATE_READY) ||	\
235 		    (efx->state == STATE_RECOVERY) ||	\
236 		    (efx->state == STATE_DISABLED))	\
237 			ASSERT_RTNL();			\
238 	} while (0)
239 
240 static int efx_check_disabled(struct efx_nic *efx)
241 {
242 	if (efx->state == STATE_DISABLED || efx->state == STATE_RECOVERY) {
243 		netif_err(efx, drv, efx->net_dev,
244 			  "device is disabled due to earlier errors\n");
245 		return -EIO;
246 	}
247 	return 0;
248 }
249 
250 /**************************************************************************
251  *
252  * Event queue processing
253  *
254  *************************************************************************/
255 
256 /* Process channel's event queue
257  *
258  * This function is responsible for processing the event queue of a
259  * single channel.  The caller must guarantee that this function will
260  * never be concurrently called more than once on the same channel,
261  * though different channels may be being processed concurrently.
262  */
263 static int efx_process_channel(struct efx_channel *channel, int budget)
264 {
265 	struct efx_tx_queue *tx_queue;
266 	int spent;
267 
268 	if (unlikely(!channel->enabled))
269 		return 0;
270 
271 	efx_for_each_channel_tx_queue(tx_queue, channel) {
272 		tx_queue->pkts_compl = 0;
273 		tx_queue->bytes_compl = 0;
274 	}
275 
276 	spent = efx_nic_process_eventq(channel, budget);
277 	if (spent && efx_channel_has_rx_queue(channel)) {
278 		struct efx_rx_queue *rx_queue =
279 			efx_channel_get_rx_queue(channel);
280 
281 		efx_rx_flush_packet(channel);
282 		efx_fast_push_rx_descriptors(rx_queue, true);
283 	}
284 
285 	/* Update BQL */
286 	efx_for_each_channel_tx_queue(tx_queue, channel) {
287 		if (tx_queue->bytes_compl) {
288 			netdev_tx_completed_queue(tx_queue->core_txq,
289 				tx_queue->pkts_compl, tx_queue->bytes_compl);
290 		}
291 	}
292 
293 	return spent;
294 }
295 
296 /* NAPI poll handler
297  *
298  * NAPI guarantees serialisation of polls of the same device, which
299  * provides the guarantee required by efx_process_channel().
300  */
301 static void efx_update_irq_mod(struct efx_nic *efx, struct efx_channel *channel)
302 {
303 	int step = efx->irq_mod_step_us;
304 
305 	if (channel->irq_mod_score < irq_adapt_low_thresh) {
306 		if (channel->irq_moderation_us > step) {
307 			channel->irq_moderation_us -= step;
308 			efx->type->push_irq_moderation(channel);
309 		}
310 	} else if (channel->irq_mod_score > irq_adapt_high_thresh) {
311 		if (channel->irq_moderation_us <
312 		    efx->irq_rx_moderation_us) {
313 			channel->irq_moderation_us += step;
314 			efx->type->push_irq_moderation(channel);
315 		}
316 	}
317 
318 	channel->irq_count = 0;
319 	channel->irq_mod_score = 0;
320 }
321 
322 static int efx_poll(struct napi_struct *napi, int budget)
323 {
324 	struct efx_channel *channel =
325 		container_of(napi, struct efx_channel, napi_str);
326 	struct efx_nic *efx = channel->efx;
327 	int spent;
328 
329 	netif_vdbg(efx, intr, efx->net_dev,
330 		   "channel %d NAPI poll executing on CPU %d\n",
331 		   channel->channel, raw_smp_processor_id());
332 
333 	spent = efx_process_channel(channel, budget);
334 
335 	if (spent < budget) {
336 		if (efx_channel_has_rx_queue(channel) &&
337 		    efx->irq_rx_adaptive &&
338 		    unlikely(++channel->irq_count == 1000)) {
339 			efx_update_irq_mod(efx, channel);
340 		}
341 
342 		efx_filter_rfs_expire(channel);
343 
344 		/* There is no race here; although napi_disable() will
345 		 * only wait for napi_complete(), this isn't a problem
346 		 * since efx_nic_eventq_read_ack() will have no effect if
347 		 * interrupts have already been disabled.
348 		 */
349 		if (napi_complete_done(napi, spent))
350 			efx_nic_eventq_read_ack(channel);
351 	}
352 
353 	return spent;
354 }
355 
356 /* Create event queue
357  * Event queue memory allocations are done only once.  If the channel
358  * is reset, the memory buffer will be reused; this guards against
359  * errors during channel reset and also simplifies interrupt handling.
360  */
361 static int efx_probe_eventq(struct efx_channel *channel)
362 {
363 	struct efx_nic *efx = channel->efx;
364 	unsigned long entries;
365 
366 	netif_dbg(efx, probe, efx->net_dev,
367 		  "chan %d create event queue\n", channel->channel);
368 
369 	/* Build an event queue with room for one event per tx and rx buffer,
370 	 * plus some extra for link state events and MCDI completions. */
371 	entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
372 	EFX_WARN_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
373 	channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
374 
375 	return efx_nic_probe_eventq(channel);
376 }
377 
378 /* Prepare channel's event queue */
379 static int efx_init_eventq(struct efx_channel *channel)
380 {
381 	struct efx_nic *efx = channel->efx;
382 	int rc;
383 
384 	EFX_WARN_ON_PARANOID(channel->eventq_init);
385 
386 	netif_dbg(efx, drv, efx->net_dev,
387 		  "chan %d init event queue\n", channel->channel);
388 
389 	rc = efx_nic_init_eventq(channel);
390 	if (rc == 0) {
391 		efx->type->push_irq_moderation(channel);
392 		channel->eventq_read_ptr = 0;
393 		channel->eventq_init = true;
394 	}
395 	return rc;
396 }
397 
398 /* Enable event queue processing and NAPI */
399 void efx_start_eventq(struct efx_channel *channel)
400 {
401 	netif_dbg(channel->efx, ifup, channel->efx->net_dev,
402 		  "chan %d start event queue\n", channel->channel);
403 
404 	/* Make sure the NAPI handler sees the enabled flag set */
405 	channel->enabled = true;
406 	smp_wmb();
407 
408 	napi_enable(&channel->napi_str);
409 	efx_nic_eventq_read_ack(channel);
410 }
411 
412 /* Disable event queue processing and NAPI */
413 void efx_stop_eventq(struct efx_channel *channel)
414 {
415 	if (!channel->enabled)
416 		return;
417 
418 	napi_disable(&channel->napi_str);
419 	channel->enabled = false;
420 }
421 
422 static void efx_fini_eventq(struct efx_channel *channel)
423 {
424 	if (!channel->eventq_init)
425 		return;
426 
427 	netif_dbg(channel->efx, drv, channel->efx->net_dev,
428 		  "chan %d fini event queue\n", channel->channel);
429 
430 	efx_nic_fini_eventq(channel);
431 	channel->eventq_init = false;
432 }
433 
434 static void efx_remove_eventq(struct efx_channel *channel)
435 {
436 	netif_dbg(channel->efx, drv, channel->efx->net_dev,
437 		  "chan %d remove event queue\n", channel->channel);
438 
439 	efx_nic_remove_eventq(channel);
440 }
441 
442 /**************************************************************************
443  *
444  * Channel handling
445  *
446  *************************************************************************/
447 
448 /* Allocate and initialise a channel structure. */
449 static struct efx_channel *
450 efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
451 {
452 	struct efx_channel *channel;
453 	struct efx_rx_queue *rx_queue;
454 	struct efx_tx_queue *tx_queue;
455 	int j;
456 
457 	channel = kzalloc(sizeof(*channel), GFP_KERNEL);
458 	if (!channel)
459 		return NULL;
460 
461 	channel->efx = efx;
462 	channel->channel = i;
463 	channel->type = &efx_default_channel_type;
464 
465 	for (j = 0; j < EFX_TXQ_TYPES; j++) {
466 		tx_queue = &channel->tx_queue[j];
467 		tx_queue->efx = efx;
468 		tx_queue->queue = i * EFX_TXQ_TYPES + j;
469 		tx_queue->channel = channel;
470 	}
471 
472 	rx_queue = &channel->rx_queue;
473 	rx_queue->efx = efx;
474 	timer_setup(&rx_queue->slow_fill, efx_rx_slow_fill, 0);
475 
476 	return channel;
477 }
478 
479 /* Allocate and initialise a channel structure, copying parameters
480  * (but not resources) from an old channel structure.
481  */
482 static struct efx_channel *
483 efx_copy_channel(const struct efx_channel *old_channel)
484 {
485 	struct efx_channel *channel;
486 	struct efx_rx_queue *rx_queue;
487 	struct efx_tx_queue *tx_queue;
488 	int j;
489 
490 	channel = kmalloc(sizeof(*channel), GFP_KERNEL);
491 	if (!channel)
492 		return NULL;
493 
494 	*channel = *old_channel;
495 
496 	channel->napi_dev = NULL;
497 	INIT_HLIST_NODE(&channel->napi_str.napi_hash_node);
498 	channel->napi_str.napi_id = 0;
499 	channel->napi_str.state = 0;
500 	memset(&channel->eventq, 0, sizeof(channel->eventq));
501 
502 	for (j = 0; j < EFX_TXQ_TYPES; j++) {
503 		tx_queue = &channel->tx_queue[j];
504 		if (tx_queue->channel)
505 			tx_queue->channel = channel;
506 		tx_queue->buffer = NULL;
507 		memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
508 	}
509 
510 	rx_queue = &channel->rx_queue;
511 	rx_queue->buffer = NULL;
512 	memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
513 	timer_setup(&rx_queue->slow_fill, efx_rx_slow_fill, 0);
514 
515 	return channel;
516 }
517 
518 static int efx_probe_channel(struct efx_channel *channel)
519 {
520 	struct efx_tx_queue *tx_queue;
521 	struct efx_rx_queue *rx_queue;
522 	int rc;
523 
524 	netif_dbg(channel->efx, probe, channel->efx->net_dev,
525 		  "creating channel %d\n", channel->channel);
526 
527 	rc = channel->type->pre_probe(channel);
528 	if (rc)
529 		goto fail;
530 
531 	rc = efx_probe_eventq(channel);
532 	if (rc)
533 		goto fail;
534 
535 	efx_for_each_channel_tx_queue(tx_queue, channel) {
536 		rc = efx_probe_tx_queue(tx_queue);
537 		if (rc)
538 			goto fail;
539 	}
540 
541 	efx_for_each_channel_rx_queue(rx_queue, channel) {
542 		rc = efx_probe_rx_queue(rx_queue);
543 		if (rc)
544 			goto fail;
545 	}
546 
547 	return 0;
548 
549 fail:
550 	efx_remove_channel(channel);
551 	return rc;
552 }
553 
554 static void
555 efx_get_channel_name(struct efx_channel *channel, char *buf, size_t len)
556 {
557 	struct efx_nic *efx = channel->efx;
558 	const char *type;
559 	int number;
560 
561 	number = channel->channel;
562 	if (efx->tx_channel_offset == 0) {
563 		type = "";
564 	} else if (channel->channel < efx->tx_channel_offset) {
565 		type = "-rx";
566 	} else {
567 		type = "-tx";
568 		number -= efx->tx_channel_offset;
569 	}
570 	snprintf(buf, len, "%s%s-%d", efx->name, type, number);
571 }
572 
573 static void efx_set_channel_names(struct efx_nic *efx)
574 {
575 	struct efx_channel *channel;
576 
577 	efx_for_each_channel(channel, efx)
578 		channel->type->get_name(channel,
579 					efx->msi_context[channel->channel].name,
580 					sizeof(efx->msi_context[0].name));
581 }
582 
583 static int efx_probe_channels(struct efx_nic *efx)
584 {
585 	struct efx_channel *channel;
586 	int rc;
587 
588 	/* Restart special buffer allocation */
589 	efx->next_buffer_table = 0;
590 
591 	/* Probe channels in reverse, so that any 'extra' channels
592 	 * use the start of the buffer table. This allows the traffic
593 	 * channels to be resized without moving them or wasting the
594 	 * entries before them.
595 	 */
596 	efx_for_each_channel_rev(channel, efx) {
597 		rc = efx_probe_channel(channel);
598 		if (rc) {
599 			netif_err(efx, probe, efx->net_dev,
600 				  "failed to create channel %d\n",
601 				  channel->channel);
602 			goto fail;
603 		}
604 	}
605 	efx_set_channel_names(efx);
606 
607 	return 0;
608 
609 fail:
610 	efx_remove_channels(efx);
611 	return rc;
612 }
613 
614 /* Channels are shutdown and reinitialised whilst the NIC is running
615  * to propagate configuration changes (mtu, checksum offload), or
616  * to clear hardware error conditions
617  */
618 static void efx_start_datapath(struct efx_nic *efx)
619 {
620 	netdev_features_t old_features = efx->net_dev->features;
621 	bool old_rx_scatter = efx->rx_scatter;
622 	struct efx_tx_queue *tx_queue;
623 	struct efx_rx_queue *rx_queue;
624 	struct efx_channel *channel;
625 	size_t rx_buf_len;
626 
627 	/* Calculate the rx buffer allocation parameters required to
628 	 * support the current MTU, including padding for header
629 	 * alignment and overruns.
630 	 */
631 	efx->rx_dma_len = (efx->rx_prefix_size +
632 			   EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
633 			   efx->type->rx_buffer_padding);
634 	rx_buf_len = (sizeof(struct efx_rx_page_state) +
635 		      efx->rx_ip_align + efx->rx_dma_len);
636 	if (rx_buf_len <= PAGE_SIZE) {
637 		efx->rx_scatter = efx->type->always_rx_scatter;
638 		efx->rx_buffer_order = 0;
639 	} else if (efx->type->can_rx_scatter) {
640 		BUILD_BUG_ON(EFX_RX_USR_BUF_SIZE % L1_CACHE_BYTES);
641 		BUILD_BUG_ON(sizeof(struct efx_rx_page_state) +
642 			     2 * ALIGN(NET_IP_ALIGN + EFX_RX_USR_BUF_SIZE,
643 				       EFX_RX_BUF_ALIGNMENT) >
644 			     PAGE_SIZE);
645 		efx->rx_scatter = true;
646 		efx->rx_dma_len = EFX_RX_USR_BUF_SIZE;
647 		efx->rx_buffer_order = 0;
648 	} else {
649 		efx->rx_scatter = false;
650 		efx->rx_buffer_order = get_order(rx_buf_len);
651 	}
652 
653 	efx_rx_config_page_split(efx);
654 	if (efx->rx_buffer_order)
655 		netif_dbg(efx, drv, efx->net_dev,
656 			  "RX buf len=%u; page order=%u batch=%u\n",
657 			  efx->rx_dma_len, efx->rx_buffer_order,
658 			  efx->rx_pages_per_batch);
659 	else
660 		netif_dbg(efx, drv, efx->net_dev,
661 			  "RX buf len=%u step=%u bpp=%u; page batch=%u\n",
662 			  efx->rx_dma_len, efx->rx_page_buf_step,
663 			  efx->rx_bufs_per_page, efx->rx_pages_per_batch);
664 
665 	/* Restore previously fixed features in hw_features and remove
666 	 * features which are fixed now
667 	 */
668 	efx->net_dev->hw_features |= efx->net_dev->features;
669 	efx->net_dev->hw_features &= ~efx->fixed_features;
670 	efx->net_dev->features |= efx->fixed_features;
671 	if (efx->net_dev->features != old_features)
672 		netdev_features_change(efx->net_dev);
673 
674 	/* RX filters may also have scatter-enabled flags */
675 	if (efx->rx_scatter != old_rx_scatter)
676 		efx->type->filter_update_rx_scatter(efx);
677 
678 	/* We must keep at least one descriptor in a TX ring empty.
679 	 * We could avoid this when the queue size does not exactly
680 	 * match the hardware ring size, but it's not that important.
681 	 * Therefore we stop the queue when one more skb might fill
682 	 * the ring completely.  We wake it when half way back to
683 	 * empty.
684 	 */
685 	efx->txq_stop_thresh = efx->txq_entries - efx_tx_max_skb_descs(efx);
686 	efx->txq_wake_thresh = efx->txq_stop_thresh / 2;
687 
688 	/* Initialise the channels */
689 	efx_for_each_channel(channel, efx) {
690 		efx_for_each_channel_tx_queue(tx_queue, channel) {
691 			efx_init_tx_queue(tx_queue);
692 			atomic_inc(&efx->active_queues);
693 		}
694 
695 		efx_for_each_channel_rx_queue(rx_queue, channel) {
696 			efx_init_rx_queue(rx_queue);
697 			atomic_inc(&efx->active_queues);
698 			efx_stop_eventq(channel);
699 			efx_fast_push_rx_descriptors(rx_queue, false);
700 			efx_start_eventq(channel);
701 		}
702 
703 		WARN_ON(channel->rx_pkt_n_frags);
704 	}
705 
706 	efx_ptp_start_datapath(efx);
707 
708 	if (netif_device_present(efx->net_dev))
709 		netif_tx_wake_all_queues(efx->net_dev);
710 }
711 
712 static void efx_stop_datapath(struct efx_nic *efx)
713 {
714 	struct efx_channel *channel;
715 	struct efx_tx_queue *tx_queue;
716 	struct efx_rx_queue *rx_queue;
717 	int rc;
718 
719 	EFX_ASSERT_RESET_SERIALISED(efx);
720 	BUG_ON(efx->port_enabled);
721 
722 	efx_ptp_stop_datapath(efx);
723 
724 	/* Stop RX refill */
725 	efx_for_each_channel(channel, efx) {
726 		efx_for_each_channel_rx_queue(rx_queue, channel)
727 			rx_queue->refill_enabled = false;
728 	}
729 
730 	efx_for_each_channel(channel, efx) {
731 		/* RX packet processing is pipelined, so wait for the
732 		 * NAPI handler to complete.  At least event queue 0
733 		 * might be kept active by non-data events, so don't
734 		 * use napi_synchronize() but actually disable NAPI
735 		 * temporarily.
736 		 */
737 		if (efx_channel_has_rx_queue(channel)) {
738 			efx_stop_eventq(channel);
739 			efx_start_eventq(channel);
740 		}
741 	}
742 
743 	rc = efx->type->fini_dmaq(efx);
744 	if (rc) {
745 		netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
746 	} else {
747 		netif_dbg(efx, drv, efx->net_dev,
748 			  "successfully flushed all queues\n");
749 	}
750 
751 	efx_for_each_channel(channel, efx) {
752 		efx_for_each_channel_rx_queue(rx_queue, channel)
753 			efx_fini_rx_queue(rx_queue);
754 		efx_for_each_possible_channel_tx_queue(tx_queue, channel)
755 			efx_fini_tx_queue(tx_queue);
756 	}
757 }
758 
759 static void efx_remove_channel(struct efx_channel *channel)
760 {
761 	struct efx_tx_queue *tx_queue;
762 	struct efx_rx_queue *rx_queue;
763 
764 	netif_dbg(channel->efx, drv, channel->efx->net_dev,
765 		  "destroy chan %d\n", channel->channel);
766 
767 	efx_for_each_channel_rx_queue(rx_queue, channel)
768 		efx_remove_rx_queue(rx_queue);
769 	efx_for_each_possible_channel_tx_queue(tx_queue, channel)
770 		efx_remove_tx_queue(tx_queue);
771 	efx_remove_eventq(channel);
772 	channel->type->post_remove(channel);
773 }
774 
775 static void efx_remove_channels(struct efx_nic *efx)
776 {
777 	struct efx_channel *channel;
778 
779 	efx_for_each_channel(channel, efx)
780 		efx_remove_channel(channel);
781 }
782 
783 int
784 efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
785 {
786 	struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
787 	u32 old_rxq_entries, old_txq_entries;
788 	unsigned i, next_buffer_table = 0;
789 	int rc, rc2;
790 
791 	rc = efx_check_disabled(efx);
792 	if (rc)
793 		return rc;
794 
795 	/* Not all channels should be reallocated. We must avoid
796 	 * reallocating their buffer table entries.
797 	 */
798 	efx_for_each_channel(channel, efx) {
799 		struct efx_rx_queue *rx_queue;
800 		struct efx_tx_queue *tx_queue;
801 
802 		if (channel->type->copy)
803 			continue;
804 		next_buffer_table = max(next_buffer_table,
805 					channel->eventq.index +
806 					channel->eventq.entries);
807 		efx_for_each_channel_rx_queue(rx_queue, channel)
808 			next_buffer_table = max(next_buffer_table,
809 						rx_queue->rxd.index +
810 						rx_queue->rxd.entries);
811 		efx_for_each_channel_tx_queue(tx_queue, channel)
812 			next_buffer_table = max(next_buffer_table,
813 						tx_queue->txd.index +
814 						tx_queue->txd.entries);
815 	}
816 
817 	efx_device_detach_sync(efx);
818 	efx_stop_all(efx);
819 	efx_soft_disable_interrupts(efx);
820 
821 	/* Clone channels (where possible) */
822 	memset(other_channel, 0, sizeof(other_channel));
823 	for (i = 0; i < efx->n_channels; i++) {
824 		channel = efx->channel[i];
825 		if (channel->type->copy)
826 			channel = channel->type->copy(channel);
827 		if (!channel) {
828 			rc = -ENOMEM;
829 			goto out;
830 		}
831 		other_channel[i] = channel;
832 	}
833 
834 	/* Swap entry counts and channel pointers */
835 	old_rxq_entries = efx->rxq_entries;
836 	old_txq_entries = efx->txq_entries;
837 	efx->rxq_entries = rxq_entries;
838 	efx->txq_entries = txq_entries;
839 	for (i = 0; i < efx->n_channels; i++) {
840 		channel = efx->channel[i];
841 		efx->channel[i] = other_channel[i];
842 		other_channel[i] = channel;
843 	}
844 
845 	/* Restart buffer table allocation */
846 	efx->next_buffer_table = next_buffer_table;
847 
848 	for (i = 0; i < efx->n_channels; i++) {
849 		channel = efx->channel[i];
850 		if (!channel->type->copy)
851 			continue;
852 		rc = efx_probe_channel(channel);
853 		if (rc)
854 			goto rollback;
855 		efx_init_napi_channel(efx->channel[i]);
856 	}
857 
858 out:
859 	/* Destroy unused channel structures */
860 	for (i = 0; i < efx->n_channels; i++) {
861 		channel = other_channel[i];
862 		if (channel && channel->type->copy) {
863 			efx_fini_napi_channel(channel);
864 			efx_remove_channel(channel);
865 			kfree(channel);
866 		}
867 	}
868 
869 	rc2 = efx_soft_enable_interrupts(efx);
870 	if (rc2) {
871 		rc = rc ? rc : rc2;
872 		netif_err(efx, drv, efx->net_dev,
873 			  "unable to restart interrupts on channel reallocation\n");
874 		efx_schedule_reset(efx, RESET_TYPE_DISABLE);
875 	} else {
876 		efx_start_all(efx);
877 		efx_device_attach_if_not_resetting(efx);
878 	}
879 	return rc;
880 
881 rollback:
882 	/* Swap back */
883 	efx->rxq_entries = old_rxq_entries;
884 	efx->txq_entries = old_txq_entries;
885 	for (i = 0; i < efx->n_channels; i++) {
886 		channel = efx->channel[i];
887 		efx->channel[i] = other_channel[i];
888 		other_channel[i] = channel;
889 	}
890 	goto out;
891 }
892 
893 void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
894 {
895 	mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
896 }
897 
898 static const struct efx_channel_type efx_default_channel_type = {
899 	.pre_probe		= efx_channel_dummy_op_int,
900 	.post_remove		= efx_channel_dummy_op_void,
901 	.get_name		= efx_get_channel_name,
902 	.copy			= efx_copy_channel,
903 	.keep_eventq		= false,
904 };
905 
906 int efx_channel_dummy_op_int(struct efx_channel *channel)
907 {
908 	return 0;
909 }
910 
911 void efx_channel_dummy_op_void(struct efx_channel *channel)
912 {
913 }
914 
915 /**************************************************************************
916  *
917  * Port handling
918  *
919  **************************************************************************/
920 
921 /* This ensures that the kernel is kept informed (via
922  * netif_carrier_on/off) of the link status, and also maintains the
923  * link status's stop on the port's TX queue.
924  */
925 void efx_link_status_changed(struct efx_nic *efx)
926 {
927 	struct efx_link_state *link_state = &efx->link_state;
928 
929 	/* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
930 	 * that no events are triggered between unregister_netdev() and the
931 	 * driver unloading. A more general condition is that NETDEV_CHANGE
932 	 * can only be generated between NETDEV_UP and NETDEV_DOWN */
933 	if (!netif_running(efx->net_dev))
934 		return;
935 
936 	if (link_state->up != netif_carrier_ok(efx->net_dev)) {
937 		efx->n_link_state_changes++;
938 
939 		if (link_state->up)
940 			netif_carrier_on(efx->net_dev);
941 		else
942 			netif_carrier_off(efx->net_dev);
943 	}
944 
945 	/* Status message for kernel log */
946 	if (link_state->up)
947 		netif_info(efx, link, efx->net_dev,
948 			   "link up at %uMbps %s-duplex (MTU %d)\n",
949 			   link_state->speed, link_state->fd ? "full" : "half",
950 			   efx->net_dev->mtu);
951 	else
952 		netif_info(efx, link, efx->net_dev, "link down\n");
953 }
954 
955 void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
956 {
957 	efx->link_advertising = advertising;
958 	if (advertising) {
959 		if (advertising & ADVERTISED_Pause)
960 			efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
961 		else
962 			efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
963 		if (advertising & ADVERTISED_Asym_Pause)
964 			efx->wanted_fc ^= EFX_FC_TX;
965 	}
966 }
967 
968 void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
969 {
970 	efx->wanted_fc = wanted_fc;
971 	if (efx->link_advertising) {
972 		if (wanted_fc & EFX_FC_RX)
973 			efx->link_advertising |= (ADVERTISED_Pause |
974 						  ADVERTISED_Asym_Pause);
975 		else
976 			efx->link_advertising &= ~(ADVERTISED_Pause |
977 						   ADVERTISED_Asym_Pause);
978 		if (wanted_fc & EFX_FC_TX)
979 			efx->link_advertising ^= ADVERTISED_Asym_Pause;
980 	}
981 }
982 
983 static void efx_fini_port(struct efx_nic *efx);
984 
985 /* We assume that efx->type->reconfigure_mac will always try to sync RX
986  * filters and therefore needs to read-lock the filter table against freeing
987  */
988 void efx_mac_reconfigure(struct efx_nic *efx)
989 {
990 	down_read(&efx->filter_sem);
991 	efx->type->reconfigure_mac(efx);
992 	up_read(&efx->filter_sem);
993 }
994 
995 /* Push loopback/power/transmit disable settings to the PHY, and reconfigure
996  * the MAC appropriately. All other PHY configuration changes are pushed
997  * through phy_op->set_settings(), and pushed asynchronously to the MAC
998  * through efx_monitor().
999  *
1000  * Callers must hold the mac_lock
1001  */
1002 int __efx_reconfigure_port(struct efx_nic *efx)
1003 {
1004 	enum efx_phy_mode phy_mode;
1005 	int rc;
1006 
1007 	WARN_ON(!mutex_is_locked(&efx->mac_lock));
1008 
1009 	/* Disable PHY transmit in mac level loopbacks */
1010 	phy_mode = efx->phy_mode;
1011 	if (LOOPBACK_INTERNAL(efx))
1012 		efx->phy_mode |= PHY_MODE_TX_DISABLED;
1013 	else
1014 		efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
1015 
1016 	rc = efx->type->reconfigure_port(efx);
1017 
1018 	if (rc)
1019 		efx->phy_mode = phy_mode;
1020 
1021 	return rc;
1022 }
1023 
1024 /* Reinitialise the MAC to pick up new PHY settings, even if the port is
1025  * disabled. */
1026 int efx_reconfigure_port(struct efx_nic *efx)
1027 {
1028 	int rc;
1029 
1030 	EFX_ASSERT_RESET_SERIALISED(efx);
1031 
1032 	mutex_lock(&efx->mac_lock);
1033 	rc = __efx_reconfigure_port(efx);
1034 	mutex_unlock(&efx->mac_lock);
1035 
1036 	return rc;
1037 }
1038 
1039 /* Asynchronous work item for changing MAC promiscuity and multicast
1040  * hash.  Avoid a drain/rx_ingress enable by reconfiguring the current
1041  * MAC directly. */
1042 static void efx_mac_work(struct work_struct *data)
1043 {
1044 	struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
1045 
1046 	mutex_lock(&efx->mac_lock);
1047 	if (efx->port_enabled)
1048 		efx_mac_reconfigure(efx);
1049 	mutex_unlock(&efx->mac_lock);
1050 }
1051 
1052 static int efx_probe_port(struct efx_nic *efx)
1053 {
1054 	int rc;
1055 
1056 	netif_dbg(efx, probe, efx->net_dev, "create port\n");
1057 
1058 	if (phy_flash_cfg)
1059 		efx->phy_mode = PHY_MODE_SPECIAL;
1060 
1061 	/* Connect up MAC/PHY operations table */
1062 	rc = efx->type->probe_port(efx);
1063 	if (rc)
1064 		return rc;
1065 
1066 	/* Initialise MAC address to permanent address */
1067 	ether_addr_copy(efx->net_dev->dev_addr, efx->net_dev->perm_addr);
1068 
1069 	return 0;
1070 }
1071 
1072 static int efx_init_port(struct efx_nic *efx)
1073 {
1074 	int rc;
1075 
1076 	netif_dbg(efx, drv, efx->net_dev, "init port\n");
1077 
1078 	mutex_lock(&efx->mac_lock);
1079 
1080 	rc = efx->phy_op->init(efx);
1081 	if (rc)
1082 		goto fail1;
1083 
1084 	efx->port_initialized = true;
1085 
1086 	/* Reconfigure the MAC before creating dma queues (required for
1087 	 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
1088 	efx_mac_reconfigure(efx);
1089 
1090 	/* Ensure the PHY advertises the correct flow control settings */
1091 	rc = efx->phy_op->reconfigure(efx);
1092 	if (rc && rc != -EPERM)
1093 		goto fail2;
1094 
1095 	mutex_unlock(&efx->mac_lock);
1096 	return 0;
1097 
1098 fail2:
1099 	efx->phy_op->fini(efx);
1100 fail1:
1101 	mutex_unlock(&efx->mac_lock);
1102 	return rc;
1103 }
1104 
1105 static void efx_start_port(struct efx_nic *efx)
1106 {
1107 	netif_dbg(efx, ifup, efx->net_dev, "start port\n");
1108 	BUG_ON(efx->port_enabled);
1109 
1110 	mutex_lock(&efx->mac_lock);
1111 	efx->port_enabled = true;
1112 
1113 	/* Ensure MAC ingress/egress is enabled */
1114 	efx_mac_reconfigure(efx);
1115 
1116 	mutex_unlock(&efx->mac_lock);
1117 }
1118 
1119 /* Cancel work for MAC reconfiguration, periodic hardware monitoring
1120  * and the async self-test, wait for them to finish and prevent them
1121  * being scheduled again.  This doesn't cover online resets, which
1122  * should only be cancelled when removing the device.
1123  */
1124 static void efx_stop_port(struct efx_nic *efx)
1125 {
1126 	netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
1127 
1128 	EFX_ASSERT_RESET_SERIALISED(efx);
1129 
1130 	mutex_lock(&efx->mac_lock);
1131 	efx->port_enabled = false;
1132 	mutex_unlock(&efx->mac_lock);
1133 
1134 	/* Serialise against efx_set_multicast_list() */
1135 	netif_addr_lock_bh(efx->net_dev);
1136 	netif_addr_unlock_bh(efx->net_dev);
1137 
1138 	cancel_delayed_work_sync(&efx->monitor_work);
1139 	efx_selftest_async_cancel(efx);
1140 	cancel_work_sync(&efx->mac_work);
1141 }
1142 
1143 static void efx_fini_port(struct efx_nic *efx)
1144 {
1145 	netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
1146 
1147 	if (!efx->port_initialized)
1148 		return;
1149 
1150 	efx->phy_op->fini(efx);
1151 	efx->port_initialized = false;
1152 
1153 	efx->link_state.up = false;
1154 	efx_link_status_changed(efx);
1155 }
1156 
1157 static void efx_remove_port(struct efx_nic *efx)
1158 {
1159 	netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
1160 
1161 	efx->type->remove_port(efx);
1162 }
1163 
1164 /**************************************************************************
1165  *
1166  * NIC handling
1167  *
1168  **************************************************************************/
1169 
1170 static LIST_HEAD(efx_primary_list);
1171 static LIST_HEAD(efx_unassociated_list);
1172 
1173 static bool efx_same_controller(struct efx_nic *left, struct efx_nic *right)
1174 {
1175 	return left->type == right->type &&
1176 		left->vpd_sn && right->vpd_sn &&
1177 		!strcmp(left->vpd_sn, right->vpd_sn);
1178 }
1179 
1180 static void efx_associate(struct efx_nic *efx)
1181 {
1182 	struct efx_nic *other, *next;
1183 
1184 	if (efx->primary == efx) {
1185 		/* Adding primary function; look for secondaries */
1186 
1187 		netif_dbg(efx, probe, efx->net_dev, "adding to primary list\n");
1188 		list_add_tail(&efx->node, &efx_primary_list);
1189 
1190 		list_for_each_entry_safe(other, next, &efx_unassociated_list,
1191 					 node) {
1192 			if (efx_same_controller(efx, other)) {
1193 				list_del(&other->node);
1194 				netif_dbg(other, probe, other->net_dev,
1195 					  "moving to secondary list of %s %s\n",
1196 					  pci_name(efx->pci_dev),
1197 					  efx->net_dev->name);
1198 				list_add_tail(&other->node,
1199 					      &efx->secondary_list);
1200 				other->primary = efx;
1201 			}
1202 		}
1203 	} else {
1204 		/* Adding secondary function; look for primary */
1205 
1206 		list_for_each_entry(other, &efx_primary_list, node) {
1207 			if (efx_same_controller(efx, other)) {
1208 				netif_dbg(efx, probe, efx->net_dev,
1209 					  "adding to secondary list of %s %s\n",
1210 					  pci_name(other->pci_dev),
1211 					  other->net_dev->name);
1212 				list_add_tail(&efx->node,
1213 					      &other->secondary_list);
1214 				efx->primary = other;
1215 				return;
1216 			}
1217 		}
1218 
1219 		netif_dbg(efx, probe, efx->net_dev,
1220 			  "adding to unassociated list\n");
1221 		list_add_tail(&efx->node, &efx_unassociated_list);
1222 	}
1223 }
1224 
1225 static void efx_dissociate(struct efx_nic *efx)
1226 {
1227 	struct efx_nic *other, *next;
1228 
1229 	list_del(&efx->node);
1230 	efx->primary = NULL;
1231 
1232 	list_for_each_entry_safe(other, next, &efx->secondary_list, node) {
1233 		list_del(&other->node);
1234 		netif_dbg(other, probe, other->net_dev,
1235 			  "moving to unassociated list\n");
1236 		list_add_tail(&other->node, &efx_unassociated_list);
1237 		other->primary = NULL;
1238 	}
1239 }
1240 
1241 /* This configures the PCI device to enable I/O and DMA. */
1242 static int efx_init_io(struct efx_nic *efx)
1243 {
1244 	struct pci_dev *pci_dev = efx->pci_dev;
1245 	dma_addr_t dma_mask = efx->type->max_dma_mask;
1246 	unsigned int mem_map_size = efx->type->mem_map_size(efx);
1247 	int rc, bar;
1248 
1249 	netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
1250 
1251 	bar = efx->type->mem_bar;
1252 
1253 	rc = pci_enable_device(pci_dev);
1254 	if (rc) {
1255 		netif_err(efx, probe, efx->net_dev,
1256 			  "failed to enable PCI device\n");
1257 		goto fail1;
1258 	}
1259 
1260 	pci_set_master(pci_dev);
1261 
1262 	/* Set the PCI DMA mask.  Try all possibilities from our
1263 	 * genuine mask down to 32 bits, because some architectures
1264 	 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
1265 	 * masks event though they reject 46 bit masks.
1266 	 */
1267 	while (dma_mask > 0x7fffffffUL) {
1268 		rc = dma_set_mask_and_coherent(&pci_dev->dev, dma_mask);
1269 		if (rc == 0)
1270 			break;
1271 		dma_mask >>= 1;
1272 	}
1273 	if (rc) {
1274 		netif_err(efx, probe, efx->net_dev,
1275 			  "could not find a suitable DMA mask\n");
1276 		goto fail2;
1277 	}
1278 	netif_dbg(efx, probe, efx->net_dev,
1279 		  "using DMA mask %llx\n", (unsigned long long) dma_mask);
1280 
1281 	efx->membase_phys = pci_resource_start(efx->pci_dev, bar);
1282 	rc = pci_request_region(pci_dev, bar, "sfc");
1283 	if (rc) {
1284 		netif_err(efx, probe, efx->net_dev,
1285 			  "request for memory BAR failed\n");
1286 		rc = -EIO;
1287 		goto fail3;
1288 	}
1289 	efx->membase = ioremap_nocache(efx->membase_phys, mem_map_size);
1290 	if (!efx->membase) {
1291 		netif_err(efx, probe, efx->net_dev,
1292 			  "could not map memory BAR at %llx+%x\n",
1293 			  (unsigned long long)efx->membase_phys, mem_map_size);
1294 		rc = -ENOMEM;
1295 		goto fail4;
1296 	}
1297 	netif_dbg(efx, probe, efx->net_dev,
1298 		  "memory BAR at %llx+%x (virtual %p)\n",
1299 		  (unsigned long long)efx->membase_phys, mem_map_size,
1300 		  efx->membase);
1301 
1302 	return 0;
1303 
1304  fail4:
1305 	pci_release_region(efx->pci_dev, bar);
1306  fail3:
1307 	efx->membase_phys = 0;
1308  fail2:
1309 	pci_disable_device(efx->pci_dev);
1310  fail1:
1311 	return rc;
1312 }
1313 
1314 static void efx_fini_io(struct efx_nic *efx)
1315 {
1316 	int bar;
1317 
1318 	netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
1319 
1320 	if (efx->membase) {
1321 		iounmap(efx->membase);
1322 		efx->membase = NULL;
1323 	}
1324 
1325 	if (efx->membase_phys) {
1326 		bar = efx->type->mem_bar;
1327 		pci_release_region(efx->pci_dev, bar);
1328 		efx->membase_phys = 0;
1329 	}
1330 
1331 	/* Don't disable bus-mastering if VFs are assigned */
1332 	if (!pci_vfs_assigned(efx->pci_dev))
1333 		pci_disable_device(efx->pci_dev);
1334 }
1335 
1336 void efx_set_default_rx_indir_table(struct efx_nic *efx)
1337 {
1338 	size_t i;
1339 
1340 	for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
1341 		efx->rx_indir_table[i] =
1342 			ethtool_rxfh_indir_default(i, efx->rss_spread);
1343 }
1344 
1345 static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
1346 {
1347 	cpumask_var_t thread_mask;
1348 	unsigned int count;
1349 	int cpu;
1350 
1351 	if (rss_cpus) {
1352 		count = rss_cpus;
1353 	} else {
1354 		if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
1355 			netif_warn(efx, probe, efx->net_dev,
1356 				   "RSS disabled due to allocation failure\n");
1357 			return 1;
1358 		}
1359 
1360 		count = 0;
1361 		for_each_online_cpu(cpu) {
1362 			if (!cpumask_test_cpu(cpu, thread_mask)) {
1363 				++count;
1364 				cpumask_or(thread_mask, thread_mask,
1365 					   topology_sibling_cpumask(cpu));
1366 			}
1367 		}
1368 
1369 		free_cpumask_var(thread_mask);
1370 	}
1371 
1372 	if (count > EFX_MAX_RX_QUEUES) {
1373 		netif_cond_dbg(efx, probe, efx->net_dev, !rss_cpus, warn,
1374 			       "Reducing number of rx queues from %u to %u.\n",
1375 			       count, EFX_MAX_RX_QUEUES);
1376 		count = EFX_MAX_RX_QUEUES;
1377 	}
1378 
1379 	/* If RSS is requested for the PF *and* VFs then we can't write RSS
1380 	 * table entries that are inaccessible to VFs
1381 	 */
1382 #ifdef CONFIG_SFC_SRIOV
1383 	if (efx->type->sriov_wanted) {
1384 		if (efx->type->sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
1385 		    count > efx_vf_size(efx)) {
1386 			netif_warn(efx, probe, efx->net_dev,
1387 				   "Reducing number of RSS channels from %u to %u for "
1388 				   "VF support. Increase vf-msix-limit to use more "
1389 				   "channels on the PF.\n",
1390 				   count, efx_vf_size(efx));
1391 			count = efx_vf_size(efx);
1392 		}
1393 	}
1394 #endif
1395 
1396 	return count;
1397 }
1398 
1399 /* Probe the number and type of interrupts we are able to obtain, and
1400  * the resulting numbers of channels and RX queues.
1401  */
1402 static int efx_probe_interrupts(struct efx_nic *efx)
1403 {
1404 	unsigned int extra_channels = 0;
1405 	unsigned int i, j;
1406 	int rc;
1407 
1408 	for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
1409 		if (efx->extra_channel_type[i])
1410 			++extra_channels;
1411 
1412 	if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
1413 		struct msix_entry xentries[EFX_MAX_CHANNELS];
1414 		unsigned int n_channels;
1415 
1416 		n_channels = efx_wanted_parallelism(efx);
1417 		if (efx_separate_tx_channels)
1418 			n_channels *= 2;
1419 		n_channels += extra_channels;
1420 		n_channels = min(n_channels, efx->max_channels);
1421 
1422 		for (i = 0; i < n_channels; i++)
1423 			xentries[i].entry = i;
1424 		rc = pci_enable_msix_range(efx->pci_dev,
1425 					   xentries, 1, n_channels);
1426 		if (rc < 0) {
1427 			/* Fall back to single channel MSI */
1428 			netif_err(efx, drv, efx->net_dev,
1429 				  "could not enable MSI-X\n");
1430 			if (efx->type->min_interrupt_mode >= EFX_INT_MODE_MSI)
1431 				efx->interrupt_mode = EFX_INT_MODE_MSI;
1432 			else
1433 				return rc;
1434 		} else if (rc < n_channels) {
1435 			netif_err(efx, drv, efx->net_dev,
1436 				  "WARNING: Insufficient MSI-X vectors"
1437 				  " available (%d < %u).\n", rc, n_channels);
1438 			netif_err(efx, drv, efx->net_dev,
1439 				  "WARNING: Performance may be reduced.\n");
1440 			n_channels = rc;
1441 		}
1442 
1443 		if (rc > 0) {
1444 			efx->n_channels = n_channels;
1445 			if (n_channels > extra_channels)
1446 				n_channels -= extra_channels;
1447 			if (efx_separate_tx_channels) {
1448 				efx->n_tx_channels = min(max(n_channels / 2,
1449 							     1U),
1450 							 efx->max_tx_channels);
1451 				efx->n_rx_channels = max(n_channels -
1452 							 efx->n_tx_channels,
1453 							 1U);
1454 			} else {
1455 				efx->n_tx_channels = min(n_channels,
1456 							 efx->max_tx_channels);
1457 				efx->n_rx_channels = n_channels;
1458 			}
1459 			for (i = 0; i < efx->n_channels; i++)
1460 				efx_get_channel(efx, i)->irq =
1461 					xentries[i].vector;
1462 		}
1463 	}
1464 
1465 	/* Try single interrupt MSI */
1466 	if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
1467 		efx->n_channels = 1;
1468 		efx->n_rx_channels = 1;
1469 		efx->n_tx_channels = 1;
1470 		rc = pci_enable_msi(efx->pci_dev);
1471 		if (rc == 0) {
1472 			efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
1473 		} else {
1474 			netif_err(efx, drv, efx->net_dev,
1475 				  "could not enable MSI\n");
1476 			if (efx->type->min_interrupt_mode >= EFX_INT_MODE_LEGACY)
1477 				efx->interrupt_mode = EFX_INT_MODE_LEGACY;
1478 			else
1479 				return rc;
1480 		}
1481 	}
1482 
1483 	/* Assume legacy interrupts */
1484 	if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
1485 		efx->n_channels = 1 + (efx_separate_tx_channels ? 1 : 0);
1486 		efx->n_rx_channels = 1;
1487 		efx->n_tx_channels = 1;
1488 		efx->legacy_irq = efx->pci_dev->irq;
1489 	}
1490 
1491 	/* Assign extra channels if possible */
1492 	j = efx->n_channels;
1493 	for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
1494 		if (!efx->extra_channel_type[i])
1495 			continue;
1496 		if (efx->interrupt_mode != EFX_INT_MODE_MSIX ||
1497 		    efx->n_channels <= extra_channels) {
1498 			efx->extra_channel_type[i]->handle_no_channel(efx);
1499 		} else {
1500 			--j;
1501 			efx_get_channel(efx, j)->type =
1502 				efx->extra_channel_type[i];
1503 		}
1504 	}
1505 
1506 	/* RSS might be usable on VFs even if it is disabled on the PF */
1507 #ifdef CONFIG_SFC_SRIOV
1508 	if (efx->type->sriov_wanted) {
1509 		efx->rss_spread = ((efx->n_rx_channels > 1 ||
1510 				    !efx->type->sriov_wanted(efx)) ?
1511 				   efx->n_rx_channels : efx_vf_size(efx));
1512 		return 0;
1513 	}
1514 #endif
1515 	efx->rss_spread = efx->n_rx_channels;
1516 
1517 	return 0;
1518 }
1519 
1520 static int efx_soft_enable_interrupts(struct efx_nic *efx)
1521 {
1522 	struct efx_channel *channel, *end_channel;
1523 	int rc;
1524 
1525 	BUG_ON(efx->state == STATE_DISABLED);
1526 
1527 	efx->irq_soft_enabled = true;
1528 	smp_wmb();
1529 
1530 	efx_for_each_channel(channel, efx) {
1531 		if (!channel->type->keep_eventq) {
1532 			rc = efx_init_eventq(channel);
1533 			if (rc)
1534 				goto fail;
1535 		}
1536 		efx_start_eventq(channel);
1537 	}
1538 
1539 	efx_mcdi_mode_event(efx);
1540 
1541 	return 0;
1542 fail:
1543 	end_channel = channel;
1544 	efx_for_each_channel(channel, efx) {
1545 		if (channel == end_channel)
1546 			break;
1547 		efx_stop_eventq(channel);
1548 		if (!channel->type->keep_eventq)
1549 			efx_fini_eventq(channel);
1550 	}
1551 
1552 	return rc;
1553 }
1554 
1555 static void efx_soft_disable_interrupts(struct efx_nic *efx)
1556 {
1557 	struct efx_channel *channel;
1558 
1559 	if (efx->state == STATE_DISABLED)
1560 		return;
1561 
1562 	efx_mcdi_mode_poll(efx);
1563 
1564 	efx->irq_soft_enabled = false;
1565 	smp_wmb();
1566 
1567 	if (efx->legacy_irq)
1568 		synchronize_irq(efx->legacy_irq);
1569 
1570 	efx_for_each_channel(channel, efx) {
1571 		if (channel->irq)
1572 			synchronize_irq(channel->irq);
1573 
1574 		efx_stop_eventq(channel);
1575 		if (!channel->type->keep_eventq)
1576 			efx_fini_eventq(channel);
1577 	}
1578 
1579 	/* Flush the asynchronous MCDI request queue */
1580 	efx_mcdi_flush_async(efx);
1581 }
1582 
1583 static int efx_enable_interrupts(struct efx_nic *efx)
1584 {
1585 	struct efx_channel *channel, *end_channel;
1586 	int rc;
1587 
1588 	BUG_ON(efx->state == STATE_DISABLED);
1589 
1590 	if (efx->eeh_disabled_legacy_irq) {
1591 		enable_irq(efx->legacy_irq);
1592 		efx->eeh_disabled_legacy_irq = false;
1593 	}
1594 
1595 	efx->type->irq_enable_master(efx);
1596 
1597 	efx_for_each_channel(channel, efx) {
1598 		if (channel->type->keep_eventq) {
1599 			rc = efx_init_eventq(channel);
1600 			if (rc)
1601 				goto fail;
1602 		}
1603 	}
1604 
1605 	rc = efx_soft_enable_interrupts(efx);
1606 	if (rc)
1607 		goto fail;
1608 
1609 	return 0;
1610 
1611 fail:
1612 	end_channel = channel;
1613 	efx_for_each_channel(channel, efx) {
1614 		if (channel == end_channel)
1615 			break;
1616 		if (channel->type->keep_eventq)
1617 			efx_fini_eventq(channel);
1618 	}
1619 
1620 	efx->type->irq_disable_non_ev(efx);
1621 
1622 	return rc;
1623 }
1624 
1625 static void efx_disable_interrupts(struct efx_nic *efx)
1626 {
1627 	struct efx_channel *channel;
1628 
1629 	efx_soft_disable_interrupts(efx);
1630 
1631 	efx_for_each_channel(channel, efx) {
1632 		if (channel->type->keep_eventq)
1633 			efx_fini_eventq(channel);
1634 	}
1635 
1636 	efx->type->irq_disable_non_ev(efx);
1637 }
1638 
1639 static void efx_remove_interrupts(struct efx_nic *efx)
1640 {
1641 	struct efx_channel *channel;
1642 
1643 	/* Remove MSI/MSI-X interrupts */
1644 	efx_for_each_channel(channel, efx)
1645 		channel->irq = 0;
1646 	pci_disable_msi(efx->pci_dev);
1647 	pci_disable_msix(efx->pci_dev);
1648 
1649 	/* Remove legacy interrupt */
1650 	efx->legacy_irq = 0;
1651 }
1652 
1653 static void efx_set_channels(struct efx_nic *efx)
1654 {
1655 	struct efx_channel *channel;
1656 	struct efx_tx_queue *tx_queue;
1657 
1658 	efx->tx_channel_offset =
1659 		efx_separate_tx_channels ?
1660 		efx->n_channels - efx->n_tx_channels : 0;
1661 
1662 	/* We need to mark which channels really have RX and TX
1663 	 * queues, and adjust the TX queue numbers if we have separate
1664 	 * RX-only and TX-only channels.
1665 	 */
1666 	efx_for_each_channel(channel, efx) {
1667 		if (channel->channel < efx->n_rx_channels)
1668 			channel->rx_queue.core_index = channel->channel;
1669 		else
1670 			channel->rx_queue.core_index = -1;
1671 
1672 		efx_for_each_channel_tx_queue(tx_queue, channel)
1673 			tx_queue->queue -= (efx->tx_channel_offset *
1674 					    EFX_TXQ_TYPES);
1675 	}
1676 }
1677 
1678 static int efx_probe_nic(struct efx_nic *efx)
1679 {
1680 	int rc;
1681 
1682 	netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
1683 
1684 	/* Carry out hardware-type specific initialisation */
1685 	rc = efx->type->probe(efx);
1686 	if (rc)
1687 		return rc;
1688 
1689 	do {
1690 		if (!efx->max_channels || !efx->max_tx_channels) {
1691 			netif_err(efx, drv, efx->net_dev,
1692 				  "Insufficient resources to allocate"
1693 				  " any channels\n");
1694 			rc = -ENOSPC;
1695 			goto fail1;
1696 		}
1697 
1698 		/* Determine the number of channels and queues by trying
1699 		 * to hook in MSI-X interrupts.
1700 		 */
1701 		rc = efx_probe_interrupts(efx);
1702 		if (rc)
1703 			goto fail1;
1704 
1705 		efx_set_channels(efx);
1706 
1707 		/* dimension_resources can fail with EAGAIN */
1708 		rc = efx->type->dimension_resources(efx);
1709 		if (rc != 0 && rc != -EAGAIN)
1710 			goto fail2;
1711 
1712 		if (rc == -EAGAIN)
1713 			/* try again with new max_channels */
1714 			efx_remove_interrupts(efx);
1715 
1716 	} while (rc == -EAGAIN);
1717 
1718 	if (efx->n_channels > 1)
1719 		netdev_rss_key_fill(&efx->rx_hash_key,
1720 				    sizeof(efx->rx_hash_key));
1721 	efx_set_default_rx_indir_table(efx);
1722 
1723 	netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1724 	netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
1725 
1726 	/* Initialise the interrupt moderation settings */
1727 	efx->irq_mod_step_us = DIV_ROUND_UP(efx->timer_quantum_ns, 1000);
1728 	efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
1729 				true);
1730 
1731 	return 0;
1732 
1733 fail2:
1734 	efx_remove_interrupts(efx);
1735 fail1:
1736 	efx->type->remove(efx);
1737 	return rc;
1738 }
1739 
1740 static void efx_remove_nic(struct efx_nic *efx)
1741 {
1742 	netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
1743 
1744 	efx_remove_interrupts(efx);
1745 	efx->type->remove(efx);
1746 }
1747 
1748 static int efx_probe_filters(struct efx_nic *efx)
1749 {
1750 	int rc;
1751 
1752 	spin_lock_init(&efx->filter_lock);
1753 	init_rwsem(&efx->filter_sem);
1754 	mutex_lock(&efx->mac_lock);
1755 	down_write(&efx->filter_sem);
1756 	rc = efx->type->filter_table_probe(efx);
1757 	if (rc)
1758 		goto out_unlock;
1759 
1760 #ifdef CONFIG_RFS_ACCEL
1761 	if (efx->type->offload_features & NETIF_F_NTUPLE) {
1762 		struct efx_channel *channel;
1763 		int i, success = 1;
1764 
1765 		efx_for_each_channel(channel, efx) {
1766 			channel->rps_flow_id =
1767 				kcalloc(efx->type->max_rx_ip_filters,
1768 					sizeof(*channel->rps_flow_id),
1769 					GFP_KERNEL);
1770 			if (!channel->rps_flow_id)
1771 				success = 0;
1772 			else
1773 				for (i = 0;
1774 				     i < efx->type->max_rx_ip_filters;
1775 				     ++i)
1776 					channel->rps_flow_id[i] =
1777 						RPS_FLOW_ID_INVALID;
1778 		}
1779 
1780 		if (!success) {
1781 			efx_for_each_channel(channel, efx)
1782 				kfree(channel->rps_flow_id);
1783 			efx->type->filter_table_remove(efx);
1784 			rc = -ENOMEM;
1785 			goto out_unlock;
1786 		}
1787 
1788 		efx->rps_expire_index = efx->rps_expire_channel = 0;
1789 	}
1790 #endif
1791 out_unlock:
1792 	up_write(&efx->filter_sem);
1793 	mutex_unlock(&efx->mac_lock);
1794 	return rc;
1795 }
1796 
1797 static void efx_remove_filters(struct efx_nic *efx)
1798 {
1799 #ifdef CONFIG_RFS_ACCEL
1800 	struct efx_channel *channel;
1801 
1802 	efx_for_each_channel(channel, efx)
1803 		kfree(channel->rps_flow_id);
1804 #endif
1805 	down_write(&efx->filter_sem);
1806 	efx->type->filter_table_remove(efx);
1807 	up_write(&efx->filter_sem);
1808 }
1809 
1810 static void efx_restore_filters(struct efx_nic *efx)
1811 {
1812 	down_read(&efx->filter_sem);
1813 	efx->type->filter_table_restore(efx);
1814 	up_read(&efx->filter_sem);
1815 }
1816 
1817 /**************************************************************************
1818  *
1819  * NIC startup/shutdown
1820  *
1821  *************************************************************************/
1822 
1823 static int efx_probe_all(struct efx_nic *efx)
1824 {
1825 	int rc;
1826 
1827 	rc = efx_probe_nic(efx);
1828 	if (rc) {
1829 		netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
1830 		goto fail1;
1831 	}
1832 
1833 	rc = efx_probe_port(efx);
1834 	if (rc) {
1835 		netif_err(efx, probe, efx->net_dev, "failed to create port\n");
1836 		goto fail2;
1837 	}
1838 
1839 	BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
1840 	if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
1841 		rc = -EINVAL;
1842 		goto fail3;
1843 	}
1844 	efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
1845 
1846 #ifdef CONFIG_SFC_SRIOV
1847 	rc = efx->type->vswitching_probe(efx);
1848 	if (rc) /* not fatal; the PF will still work fine */
1849 		netif_warn(efx, probe, efx->net_dev,
1850 			   "failed to setup vswitching rc=%d;"
1851 			   " VFs may not function\n", rc);
1852 #endif
1853 
1854 	rc = efx_probe_filters(efx);
1855 	if (rc) {
1856 		netif_err(efx, probe, efx->net_dev,
1857 			  "failed to create filter tables\n");
1858 		goto fail4;
1859 	}
1860 
1861 	rc = efx_probe_channels(efx);
1862 	if (rc)
1863 		goto fail5;
1864 
1865 	return 0;
1866 
1867  fail5:
1868 	efx_remove_filters(efx);
1869  fail4:
1870 #ifdef CONFIG_SFC_SRIOV
1871 	efx->type->vswitching_remove(efx);
1872 #endif
1873  fail3:
1874 	efx_remove_port(efx);
1875  fail2:
1876 	efx_remove_nic(efx);
1877  fail1:
1878 	return rc;
1879 }
1880 
1881 /* If the interface is supposed to be running but is not, start
1882  * the hardware and software data path, regular activity for the port
1883  * (MAC statistics, link polling, etc.) and schedule the port to be
1884  * reconfigured.  Interrupts must already be enabled.  This function
1885  * is safe to call multiple times, so long as the NIC is not disabled.
1886  * Requires the RTNL lock.
1887  */
1888 static void efx_start_all(struct efx_nic *efx)
1889 {
1890 	EFX_ASSERT_RESET_SERIALISED(efx);
1891 	BUG_ON(efx->state == STATE_DISABLED);
1892 
1893 	/* Check that it is appropriate to restart the interface. All
1894 	 * of these flags are safe to read under just the rtnl lock */
1895 	if (efx->port_enabled || !netif_running(efx->net_dev) ||
1896 	    efx->reset_pending)
1897 		return;
1898 
1899 	efx_start_port(efx);
1900 	efx_start_datapath(efx);
1901 
1902 	/* Start the hardware monitor if there is one */
1903 	if (efx->type->monitor != NULL)
1904 		queue_delayed_work(efx->workqueue, &efx->monitor_work,
1905 				   efx_monitor_interval);
1906 
1907 	/* Link state detection is normally event-driven; we have
1908 	 * to poll now because we could have missed a change
1909 	 */
1910 	mutex_lock(&efx->mac_lock);
1911 	if (efx->phy_op->poll(efx))
1912 		efx_link_status_changed(efx);
1913 	mutex_unlock(&efx->mac_lock);
1914 
1915 	efx->type->start_stats(efx);
1916 	efx->type->pull_stats(efx);
1917 	spin_lock_bh(&efx->stats_lock);
1918 	efx->type->update_stats(efx, NULL, NULL);
1919 	spin_unlock_bh(&efx->stats_lock);
1920 }
1921 
1922 /* Quiesce the hardware and software data path, and regular activity
1923  * for the port without bringing the link down.  Safe to call multiple
1924  * times with the NIC in almost any state, but interrupts should be
1925  * enabled.  Requires the RTNL lock.
1926  */
1927 static void efx_stop_all(struct efx_nic *efx)
1928 {
1929 	EFX_ASSERT_RESET_SERIALISED(efx);
1930 
1931 	/* port_enabled can be read safely under the rtnl lock */
1932 	if (!efx->port_enabled)
1933 		return;
1934 
1935 	/* update stats before we go down so we can accurately count
1936 	 * rx_nodesc_drops
1937 	 */
1938 	efx->type->pull_stats(efx);
1939 	spin_lock_bh(&efx->stats_lock);
1940 	efx->type->update_stats(efx, NULL, NULL);
1941 	spin_unlock_bh(&efx->stats_lock);
1942 	efx->type->stop_stats(efx);
1943 	efx_stop_port(efx);
1944 
1945 	/* Stop the kernel transmit interface.  This is only valid if
1946 	 * the device is stopped or detached; otherwise the watchdog
1947 	 * may fire immediately.
1948 	 */
1949 	WARN_ON(netif_running(efx->net_dev) &&
1950 		netif_device_present(efx->net_dev));
1951 	netif_tx_disable(efx->net_dev);
1952 
1953 	efx_stop_datapath(efx);
1954 }
1955 
1956 static void efx_remove_all(struct efx_nic *efx)
1957 {
1958 	efx_remove_channels(efx);
1959 	efx_remove_filters(efx);
1960 #ifdef CONFIG_SFC_SRIOV
1961 	efx->type->vswitching_remove(efx);
1962 #endif
1963 	efx_remove_port(efx);
1964 	efx_remove_nic(efx);
1965 }
1966 
1967 /**************************************************************************
1968  *
1969  * Interrupt moderation
1970  *
1971  **************************************************************************/
1972 unsigned int efx_usecs_to_ticks(struct efx_nic *efx, unsigned int usecs)
1973 {
1974 	if (usecs == 0)
1975 		return 0;
1976 	if (usecs * 1000 < efx->timer_quantum_ns)
1977 		return 1; /* never round down to 0 */
1978 	return usecs * 1000 / efx->timer_quantum_ns;
1979 }
1980 
1981 unsigned int efx_ticks_to_usecs(struct efx_nic *efx, unsigned int ticks)
1982 {
1983 	/* We must round up when converting ticks to microseconds
1984 	 * because we round down when converting the other way.
1985 	 */
1986 	return DIV_ROUND_UP(ticks * efx->timer_quantum_ns, 1000);
1987 }
1988 
1989 /* Set interrupt moderation parameters */
1990 int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
1991 			    unsigned int rx_usecs, bool rx_adaptive,
1992 			    bool rx_may_override_tx)
1993 {
1994 	struct efx_channel *channel;
1995 	unsigned int timer_max_us;
1996 
1997 	EFX_ASSERT_RESET_SERIALISED(efx);
1998 
1999 	timer_max_us = efx->timer_max_ns / 1000;
2000 
2001 	if (tx_usecs > timer_max_us || rx_usecs > timer_max_us)
2002 		return -EINVAL;
2003 
2004 	if (tx_usecs != rx_usecs && efx->tx_channel_offset == 0 &&
2005 	    !rx_may_override_tx) {
2006 		netif_err(efx, drv, efx->net_dev, "Channels are shared. "
2007 			  "RX and TX IRQ moderation must be equal\n");
2008 		return -EINVAL;
2009 	}
2010 
2011 	efx->irq_rx_adaptive = rx_adaptive;
2012 	efx->irq_rx_moderation_us = rx_usecs;
2013 	efx_for_each_channel(channel, efx) {
2014 		if (efx_channel_has_rx_queue(channel))
2015 			channel->irq_moderation_us = rx_usecs;
2016 		else if (efx_channel_has_tx_queues(channel))
2017 			channel->irq_moderation_us = tx_usecs;
2018 	}
2019 
2020 	return 0;
2021 }
2022 
2023 void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
2024 			    unsigned int *rx_usecs, bool *rx_adaptive)
2025 {
2026 	*rx_adaptive = efx->irq_rx_adaptive;
2027 	*rx_usecs = efx->irq_rx_moderation_us;
2028 
2029 	/* If channels are shared between RX and TX, so is IRQ
2030 	 * moderation.  Otherwise, IRQ moderation is the same for all
2031 	 * TX channels and is not adaptive.
2032 	 */
2033 	if (efx->tx_channel_offset == 0) {
2034 		*tx_usecs = *rx_usecs;
2035 	} else {
2036 		struct efx_channel *tx_channel;
2037 
2038 		tx_channel = efx->channel[efx->tx_channel_offset];
2039 		*tx_usecs = tx_channel->irq_moderation_us;
2040 	}
2041 }
2042 
2043 /**************************************************************************
2044  *
2045  * Hardware monitor
2046  *
2047  **************************************************************************/
2048 
2049 /* Run periodically off the general workqueue */
2050 static void efx_monitor(struct work_struct *data)
2051 {
2052 	struct efx_nic *efx = container_of(data, struct efx_nic,
2053 					   monitor_work.work);
2054 
2055 	netif_vdbg(efx, timer, efx->net_dev,
2056 		   "hardware monitor executing on CPU %d\n",
2057 		   raw_smp_processor_id());
2058 	BUG_ON(efx->type->monitor == NULL);
2059 
2060 	/* If the mac_lock is already held then it is likely a port
2061 	 * reconfiguration is already in place, which will likely do
2062 	 * most of the work of monitor() anyway. */
2063 	if (mutex_trylock(&efx->mac_lock)) {
2064 		if (efx->port_enabled)
2065 			efx->type->monitor(efx);
2066 		mutex_unlock(&efx->mac_lock);
2067 	}
2068 
2069 	queue_delayed_work(efx->workqueue, &efx->monitor_work,
2070 			   efx_monitor_interval);
2071 }
2072 
2073 /**************************************************************************
2074  *
2075  * ioctls
2076  *
2077  *************************************************************************/
2078 
2079 /* Net device ioctl
2080  * Context: process, rtnl_lock() held.
2081  */
2082 static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
2083 {
2084 	struct efx_nic *efx = netdev_priv(net_dev);
2085 	struct mii_ioctl_data *data = if_mii(ifr);
2086 
2087 	if (cmd == SIOCSHWTSTAMP)
2088 		return efx_ptp_set_ts_config(efx, ifr);
2089 	if (cmd == SIOCGHWTSTAMP)
2090 		return efx_ptp_get_ts_config(efx, ifr);
2091 
2092 	/* Convert phy_id from older PRTAD/DEVAD format */
2093 	if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
2094 	    (data->phy_id & 0xfc00) == 0x0400)
2095 		data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
2096 
2097 	return mdio_mii_ioctl(&efx->mdio, data, cmd);
2098 }
2099 
2100 /**************************************************************************
2101  *
2102  * NAPI interface
2103  *
2104  **************************************************************************/
2105 
2106 static void efx_init_napi_channel(struct efx_channel *channel)
2107 {
2108 	struct efx_nic *efx = channel->efx;
2109 
2110 	channel->napi_dev = efx->net_dev;
2111 	netif_napi_add(channel->napi_dev, &channel->napi_str,
2112 		       efx_poll, napi_weight);
2113 }
2114 
2115 static void efx_init_napi(struct efx_nic *efx)
2116 {
2117 	struct efx_channel *channel;
2118 
2119 	efx_for_each_channel(channel, efx)
2120 		efx_init_napi_channel(channel);
2121 }
2122 
2123 static void efx_fini_napi_channel(struct efx_channel *channel)
2124 {
2125 	if (channel->napi_dev)
2126 		netif_napi_del(&channel->napi_str);
2127 
2128 	channel->napi_dev = NULL;
2129 }
2130 
2131 static void efx_fini_napi(struct efx_nic *efx)
2132 {
2133 	struct efx_channel *channel;
2134 
2135 	efx_for_each_channel(channel, efx)
2136 		efx_fini_napi_channel(channel);
2137 }
2138 
2139 /**************************************************************************
2140  *
2141  * Kernel netpoll interface
2142  *
2143  *************************************************************************/
2144 
2145 #ifdef CONFIG_NET_POLL_CONTROLLER
2146 
2147 /* Although in the common case interrupts will be disabled, this is not
2148  * guaranteed. However, all our work happens inside the NAPI callback,
2149  * so no locking is required.
2150  */
2151 static void efx_netpoll(struct net_device *net_dev)
2152 {
2153 	struct efx_nic *efx = netdev_priv(net_dev);
2154 	struct efx_channel *channel;
2155 
2156 	efx_for_each_channel(channel, efx)
2157 		efx_schedule_channel(channel);
2158 }
2159 
2160 #endif
2161 
2162 /**************************************************************************
2163  *
2164  * Kernel net device interface
2165  *
2166  *************************************************************************/
2167 
2168 /* Context: process, rtnl_lock() held. */
2169 int efx_net_open(struct net_device *net_dev)
2170 {
2171 	struct efx_nic *efx = netdev_priv(net_dev);
2172 	int rc;
2173 
2174 	netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
2175 		  raw_smp_processor_id());
2176 
2177 	rc = efx_check_disabled(efx);
2178 	if (rc)
2179 		return rc;
2180 	if (efx->phy_mode & PHY_MODE_SPECIAL)
2181 		return -EBUSY;
2182 	if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
2183 		return -EIO;
2184 
2185 	/* Notify the kernel of the link state polled during driver load,
2186 	 * before the monitor starts running */
2187 	efx_link_status_changed(efx);
2188 
2189 	efx_start_all(efx);
2190 	if (efx->state == STATE_DISABLED || efx->reset_pending)
2191 		netif_device_detach(efx->net_dev);
2192 	efx_selftest_async_start(efx);
2193 	return 0;
2194 }
2195 
2196 /* Context: process, rtnl_lock() held.
2197  * Note that the kernel will ignore our return code; this method
2198  * should really be a void.
2199  */
2200 int efx_net_stop(struct net_device *net_dev)
2201 {
2202 	struct efx_nic *efx = netdev_priv(net_dev);
2203 
2204 	netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
2205 		  raw_smp_processor_id());
2206 
2207 	/* Stop the device and flush all the channels */
2208 	efx_stop_all(efx);
2209 
2210 	return 0;
2211 }
2212 
2213 /* Context: process, dev_base_lock or RTNL held, non-blocking. */
2214 static void efx_net_stats(struct net_device *net_dev,
2215 			  struct rtnl_link_stats64 *stats)
2216 {
2217 	struct efx_nic *efx = netdev_priv(net_dev);
2218 
2219 	spin_lock_bh(&efx->stats_lock);
2220 	efx->type->update_stats(efx, NULL, stats);
2221 	spin_unlock_bh(&efx->stats_lock);
2222 }
2223 
2224 /* Context: netif_tx_lock held, BHs disabled. */
2225 static void efx_watchdog(struct net_device *net_dev)
2226 {
2227 	struct efx_nic *efx = netdev_priv(net_dev);
2228 
2229 	netif_err(efx, tx_err, efx->net_dev,
2230 		  "TX stuck with port_enabled=%d: resetting channels\n",
2231 		  efx->port_enabled);
2232 
2233 	efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
2234 }
2235 
2236 
2237 /* Context: process, rtnl_lock() held. */
2238 static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
2239 {
2240 	struct efx_nic *efx = netdev_priv(net_dev);
2241 	int rc;
2242 
2243 	rc = efx_check_disabled(efx);
2244 	if (rc)
2245 		return rc;
2246 
2247 	netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
2248 
2249 	efx_device_detach_sync(efx);
2250 	efx_stop_all(efx);
2251 
2252 	mutex_lock(&efx->mac_lock);
2253 	net_dev->mtu = new_mtu;
2254 	efx_mac_reconfigure(efx);
2255 	mutex_unlock(&efx->mac_lock);
2256 
2257 	efx_start_all(efx);
2258 	efx_device_attach_if_not_resetting(efx);
2259 	return 0;
2260 }
2261 
2262 static int efx_set_mac_address(struct net_device *net_dev, void *data)
2263 {
2264 	struct efx_nic *efx = netdev_priv(net_dev);
2265 	struct sockaddr *addr = data;
2266 	u8 *new_addr = addr->sa_data;
2267 	u8 old_addr[6];
2268 	int rc;
2269 
2270 	if (!is_valid_ether_addr(new_addr)) {
2271 		netif_err(efx, drv, efx->net_dev,
2272 			  "invalid ethernet MAC address requested: %pM\n",
2273 			  new_addr);
2274 		return -EADDRNOTAVAIL;
2275 	}
2276 
2277 	/* save old address */
2278 	ether_addr_copy(old_addr, net_dev->dev_addr);
2279 	ether_addr_copy(net_dev->dev_addr, new_addr);
2280 	if (efx->type->set_mac_address) {
2281 		rc = efx->type->set_mac_address(efx);
2282 		if (rc) {
2283 			ether_addr_copy(net_dev->dev_addr, old_addr);
2284 			return rc;
2285 		}
2286 	}
2287 
2288 	/* Reconfigure the MAC */
2289 	mutex_lock(&efx->mac_lock);
2290 	efx_mac_reconfigure(efx);
2291 	mutex_unlock(&efx->mac_lock);
2292 
2293 	return 0;
2294 }
2295 
2296 /* Context: netif_addr_lock held, BHs disabled. */
2297 static void efx_set_rx_mode(struct net_device *net_dev)
2298 {
2299 	struct efx_nic *efx = netdev_priv(net_dev);
2300 
2301 	if (efx->port_enabled)
2302 		queue_work(efx->workqueue, &efx->mac_work);
2303 	/* Otherwise efx_start_port() will do this */
2304 }
2305 
2306 static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
2307 {
2308 	struct efx_nic *efx = netdev_priv(net_dev);
2309 	int rc;
2310 
2311 	/* If disabling RX n-tuple filtering, clear existing filters */
2312 	if (net_dev->features & ~data & NETIF_F_NTUPLE) {
2313 		rc = efx->type->filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
2314 		if (rc)
2315 			return rc;
2316 	}
2317 
2318 	/* If Rx VLAN filter is changed, update filters via mac_reconfigure */
2319 	if ((net_dev->features ^ data) & NETIF_F_HW_VLAN_CTAG_FILTER) {
2320 		/* efx_set_rx_mode() will schedule MAC work to update filters
2321 		 * when a new features are finally set in net_dev.
2322 		 */
2323 		efx_set_rx_mode(net_dev);
2324 	}
2325 
2326 	return 0;
2327 }
2328 
2329 static int efx_get_phys_port_id(struct net_device *net_dev,
2330 				struct netdev_phys_item_id *ppid)
2331 {
2332 	struct efx_nic *efx = netdev_priv(net_dev);
2333 
2334 	if (efx->type->get_phys_port_id)
2335 		return efx->type->get_phys_port_id(efx, ppid);
2336 	else
2337 		return -EOPNOTSUPP;
2338 }
2339 
2340 static int efx_get_phys_port_name(struct net_device *net_dev,
2341 				  char *name, size_t len)
2342 {
2343 	struct efx_nic *efx = netdev_priv(net_dev);
2344 
2345 	if (snprintf(name, len, "p%u", efx->port_num) >= len)
2346 		return -EINVAL;
2347 	return 0;
2348 }
2349 
2350 static int efx_vlan_rx_add_vid(struct net_device *net_dev, __be16 proto, u16 vid)
2351 {
2352 	struct efx_nic *efx = netdev_priv(net_dev);
2353 
2354 	if (efx->type->vlan_rx_add_vid)
2355 		return efx->type->vlan_rx_add_vid(efx, proto, vid);
2356 	else
2357 		return -EOPNOTSUPP;
2358 }
2359 
2360 static int efx_vlan_rx_kill_vid(struct net_device *net_dev, __be16 proto, u16 vid)
2361 {
2362 	struct efx_nic *efx = netdev_priv(net_dev);
2363 
2364 	if (efx->type->vlan_rx_kill_vid)
2365 		return efx->type->vlan_rx_kill_vid(efx, proto, vid);
2366 	else
2367 		return -EOPNOTSUPP;
2368 }
2369 
2370 static int efx_udp_tunnel_type_map(enum udp_parsable_tunnel_type in)
2371 {
2372 	switch (in) {
2373 	case UDP_TUNNEL_TYPE_VXLAN:
2374 		return TUNNEL_ENCAP_UDP_PORT_ENTRY_VXLAN;
2375 	case UDP_TUNNEL_TYPE_GENEVE:
2376 		return TUNNEL_ENCAP_UDP_PORT_ENTRY_GENEVE;
2377 	default:
2378 		return -1;
2379 	}
2380 }
2381 
2382 static void efx_udp_tunnel_add(struct net_device *dev, struct udp_tunnel_info *ti)
2383 {
2384 	struct efx_nic *efx = netdev_priv(dev);
2385 	struct efx_udp_tunnel tnl;
2386 	int efx_tunnel_type;
2387 
2388 	efx_tunnel_type = efx_udp_tunnel_type_map(ti->type);
2389 	if (efx_tunnel_type < 0)
2390 		return;
2391 
2392 	tnl.type = (u16)efx_tunnel_type;
2393 	tnl.port = ti->port;
2394 
2395 	if (efx->type->udp_tnl_add_port)
2396 		(void)efx->type->udp_tnl_add_port(efx, tnl);
2397 }
2398 
2399 static void efx_udp_tunnel_del(struct net_device *dev, struct udp_tunnel_info *ti)
2400 {
2401 	struct efx_nic *efx = netdev_priv(dev);
2402 	struct efx_udp_tunnel tnl;
2403 	int efx_tunnel_type;
2404 
2405 	efx_tunnel_type = efx_udp_tunnel_type_map(ti->type);
2406 	if (efx_tunnel_type < 0)
2407 		return;
2408 
2409 	tnl.type = (u16)efx_tunnel_type;
2410 	tnl.port = ti->port;
2411 
2412 	if (efx->type->udp_tnl_del_port)
2413 		(void)efx->type->udp_tnl_del_port(efx, tnl);
2414 }
2415 
2416 static const struct net_device_ops efx_netdev_ops = {
2417 	.ndo_open		= efx_net_open,
2418 	.ndo_stop		= efx_net_stop,
2419 	.ndo_get_stats64	= efx_net_stats,
2420 	.ndo_tx_timeout		= efx_watchdog,
2421 	.ndo_start_xmit		= efx_hard_start_xmit,
2422 	.ndo_validate_addr	= eth_validate_addr,
2423 	.ndo_do_ioctl		= efx_ioctl,
2424 	.ndo_change_mtu		= efx_change_mtu,
2425 	.ndo_set_mac_address	= efx_set_mac_address,
2426 	.ndo_set_rx_mode	= efx_set_rx_mode,
2427 	.ndo_set_features	= efx_set_features,
2428 	.ndo_vlan_rx_add_vid	= efx_vlan_rx_add_vid,
2429 	.ndo_vlan_rx_kill_vid	= efx_vlan_rx_kill_vid,
2430 #ifdef CONFIG_SFC_SRIOV
2431 	.ndo_set_vf_mac		= efx_sriov_set_vf_mac,
2432 	.ndo_set_vf_vlan	= efx_sriov_set_vf_vlan,
2433 	.ndo_set_vf_spoofchk	= efx_sriov_set_vf_spoofchk,
2434 	.ndo_get_vf_config	= efx_sriov_get_vf_config,
2435 	.ndo_set_vf_link_state  = efx_sriov_set_vf_link_state,
2436 #endif
2437 	.ndo_get_phys_port_id   = efx_get_phys_port_id,
2438 	.ndo_get_phys_port_name	= efx_get_phys_port_name,
2439 #ifdef CONFIG_NET_POLL_CONTROLLER
2440 	.ndo_poll_controller = efx_netpoll,
2441 #endif
2442 	.ndo_setup_tc		= efx_setup_tc,
2443 #ifdef CONFIG_RFS_ACCEL
2444 	.ndo_rx_flow_steer	= efx_filter_rfs,
2445 #endif
2446 	.ndo_udp_tunnel_add	= efx_udp_tunnel_add,
2447 	.ndo_udp_tunnel_del	= efx_udp_tunnel_del,
2448 };
2449 
2450 static void efx_update_name(struct efx_nic *efx)
2451 {
2452 	strcpy(efx->name, efx->net_dev->name);
2453 	efx_mtd_rename(efx);
2454 	efx_set_channel_names(efx);
2455 }
2456 
2457 static int efx_netdev_event(struct notifier_block *this,
2458 			    unsigned long event, void *ptr)
2459 {
2460 	struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
2461 
2462 	if ((net_dev->netdev_ops == &efx_netdev_ops) &&
2463 	    event == NETDEV_CHANGENAME)
2464 		efx_update_name(netdev_priv(net_dev));
2465 
2466 	return NOTIFY_DONE;
2467 }
2468 
2469 static struct notifier_block efx_netdev_notifier = {
2470 	.notifier_call = efx_netdev_event,
2471 };
2472 
2473 static ssize_t
2474 show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
2475 {
2476 	struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2477 	return sprintf(buf, "%d\n", efx->phy_type);
2478 }
2479 static DEVICE_ATTR(phy_type, 0444, show_phy_type, NULL);
2480 
2481 #ifdef CONFIG_SFC_MCDI_LOGGING
2482 static ssize_t show_mcdi_log(struct device *dev, struct device_attribute *attr,
2483 			     char *buf)
2484 {
2485 	struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2486 	struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
2487 
2488 	return scnprintf(buf, PAGE_SIZE, "%d\n", mcdi->logging_enabled);
2489 }
2490 static ssize_t set_mcdi_log(struct device *dev, struct device_attribute *attr,
2491 			    const char *buf, size_t count)
2492 {
2493 	struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2494 	struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
2495 	bool enable = count > 0 && *buf != '0';
2496 
2497 	mcdi->logging_enabled = enable;
2498 	return count;
2499 }
2500 static DEVICE_ATTR(mcdi_logging, 0644, show_mcdi_log, set_mcdi_log);
2501 #endif
2502 
2503 static int efx_register_netdev(struct efx_nic *efx)
2504 {
2505 	struct net_device *net_dev = efx->net_dev;
2506 	struct efx_channel *channel;
2507 	int rc;
2508 
2509 	net_dev->watchdog_timeo = 5 * HZ;
2510 	net_dev->irq = efx->pci_dev->irq;
2511 	net_dev->netdev_ops = &efx_netdev_ops;
2512 	if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0)
2513 		net_dev->priv_flags |= IFF_UNICAST_FLT;
2514 	net_dev->ethtool_ops = &efx_ethtool_ops;
2515 	net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
2516 	net_dev->min_mtu = EFX_MIN_MTU;
2517 	net_dev->max_mtu = EFX_MAX_MTU;
2518 
2519 	rtnl_lock();
2520 
2521 	/* Enable resets to be scheduled and check whether any were
2522 	 * already requested.  If so, the NIC is probably hosed so we
2523 	 * abort.
2524 	 */
2525 	efx->state = STATE_READY;
2526 	smp_mb(); /* ensure we change state before checking reset_pending */
2527 	if (efx->reset_pending) {
2528 		netif_err(efx, probe, efx->net_dev,
2529 			  "aborting probe due to scheduled reset\n");
2530 		rc = -EIO;
2531 		goto fail_locked;
2532 	}
2533 
2534 	rc = dev_alloc_name(net_dev, net_dev->name);
2535 	if (rc < 0)
2536 		goto fail_locked;
2537 	efx_update_name(efx);
2538 
2539 	/* Always start with carrier off; PHY events will detect the link */
2540 	netif_carrier_off(net_dev);
2541 
2542 	rc = register_netdevice(net_dev);
2543 	if (rc)
2544 		goto fail_locked;
2545 
2546 	efx_for_each_channel(channel, efx) {
2547 		struct efx_tx_queue *tx_queue;
2548 		efx_for_each_channel_tx_queue(tx_queue, channel)
2549 			efx_init_tx_queue_core_txq(tx_queue);
2550 	}
2551 
2552 	efx_associate(efx);
2553 
2554 	rtnl_unlock();
2555 
2556 	rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2557 	if (rc) {
2558 		netif_err(efx, drv, efx->net_dev,
2559 			  "failed to init net dev attributes\n");
2560 		goto fail_registered;
2561 	}
2562 #ifdef CONFIG_SFC_MCDI_LOGGING
2563 	rc = device_create_file(&efx->pci_dev->dev, &dev_attr_mcdi_logging);
2564 	if (rc) {
2565 		netif_err(efx, drv, efx->net_dev,
2566 			  "failed to init net dev attributes\n");
2567 		goto fail_attr_mcdi_logging;
2568 	}
2569 #endif
2570 
2571 	return 0;
2572 
2573 #ifdef CONFIG_SFC_MCDI_LOGGING
2574 fail_attr_mcdi_logging:
2575 	device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2576 #endif
2577 fail_registered:
2578 	rtnl_lock();
2579 	efx_dissociate(efx);
2580 	unregister_netdevice(net_dev);
2581 fail_locked:
2582 	efx->state = STATE_UNINIT;
2583 	rtnl_unlock();
2584 	netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
2585 	return rc;
2586 }
2587 
2588 static void efx_unregister_netdev(struct efx_nic *efx)
2589 {
2590 	if (!efx->net_dev)
2591 		return;
2592 
2593 	BUG_ON(netdev_priv(efx->net_dev) != efx);
2594 
2595 	if (efx_dev_registered(efx)) {
2596 		strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2597 #ifdef CONFIG_SFC_MCDI_LOGGING
2598 		device_remove_file(&efx->pci_dev->dev, &dev_attr_mcdi_logging);
2599 #endif
2600 		device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2601 		unregister_netdev(efx->net_dev);
2602 	}
2603 }
2604 
2605 /**************************************************************************
2606  *
2607  * Device reset and suspend
2608  *
2609  **************************************************************************/
2610 
2611 /* Tears down the entire software state and most of the hardware state
2612  * before reset.  */
2613 void efx_reset_down(struct efx_nic *efx, enum reset_type method)
2614 {
2615 	EFX_ASSERT_RESET_SERIALISED(efx);
2616 
2617 	if (method == RESET_TYPE_MCDI_TIMEOUT)
2618 		efx->type->prepare_flr(efx);
2619 
2620 	efx_stop_all(efx);
2621 	efx_disable_interrupts(efx);
2622 
2623 	mutex_lock(&efx->mac_lock);
2624 	if (efx->port_initialized && method != RESET_TYPE_INVISIBLE &&
2625 	    method != RESET_TYPE_DATAPATH)
2626 		efx->phy_op->fini(efx);
2627 	efx->type->fini(efx);
2628 }
2629 
2630 /* This function will always ensure that the locks acquired in
2631  * efx_reset_down() are released. A failure return code indicates
2632  * that we were unable to reinitialise the hardware, and the
2633  * driver should be disabled. If ok is false, then the rx and tx
2634  * engines are not restarted, pending a RESET_DISABLE. */
2635 int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
2636 {
2637 	int rc;
2638 
2639 	EFX_ASSERT_RESET_SERIALISED(efx);
2640 
2641 	if (method == RESET_TYPE_MCDI_TIMEOUT)
2642 		efx->type->finish_flr(efx);
2643 
2644 	/* Ensure that SRAM is initialised even if we're disabling the device */
2645 	rc = efx->type->init(efx);
2646 	if (rc) {
2647 		netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
2648 		goto fail;
2649 	}
2650 
2651 	if (!ok)
2652 		goto fail;
2653 
2654 	if (efx->port_initialized && method != RESET_TYPE_INVISIBLE &&
2655 	    method != RESET_TYPE_DATAPATH) {
2656 		rc = efx->phy_op->init(efx);
2657 		if (rc)
2658 			goto fail;
2659 		rc = efx->phy_op->reconfigure(efx);
2660 		if (rc && rc != -EPERM)
2661 			netif_err(efx, drv, efx->net_dev,
2662 				  "could not restore PHY settings\n");
2663 	}
2664 
2665 	rc = efx_enable_interrupts(efx);
2666 	if (rc)
2667 		goto fail;
2668 
2669 #ifdef CONFIG_SFC_SRIOV
2670 	rc = efx->type->vswitching_restore(efx);
2671 	if (rc) /* not fatal; the PF will still work fine */
2672 		netif_warn(efx, probe, efx->net_dev,
2673 			   "failed to restore vswitching rc=%d;"
2674 			   " VFs may not function\n", rc);
2675 #endif
2676 
2677 	down_read(&efx->filter_sem);
2678 	efx_restore_filters(efx);
2679 	up_read(&efx->filter_sem);
2680 	if (efx->type->sriov_reset)
2681 		efx->type->sriov_reset(efx);
2682 
2683 	mutex_unlock(&efx->mac_lock);
2684 
2685 	efx_start_all(efx);
2686 
2687 	if (efx->type->udp_tnl_push_ports)
2688 		efx->type->udp_tnl_push_ports(efx);
2689 
2690 	return 0;
2691 
2692 fail:
2693 	efx->port_initialized = false;
2694 
2695 	mutex_unlock(&efx->mac_lock);
2696 
2697 	return rc;
2698 }
2699 
2700 /* Reset the NIC using the specified method.  Note that the reset may
2701  * fail, in which case the card will be left in an unusable state.
2702  *
2703  * Caller must hold the rtnl_lock.
2704  */
2705 int efx_reset(struct efx_nic *efx, enum reset_type method)
2706 {
2707 	int rc, rc2;
2708 	bool disabled;
2709 
2710 	netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2711 		   RESET_TYPE(method));
2712 
2713 	efx_device_detach_sync(efx);
2714 	efx_reset_down(efx, method);
2715 
2716 	rc = efx->type->reset(efx, method);
2717 	if (rc) {
2718 		netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
2719 		goto out;
2720 	}
2721 
2722 	/* Clear flags for the scopes we covered.  We assume the NIC and
2723 	 * driver are now quiescent so that there is no race here.
2724 	 */
2725 	if (method < RESET_TYPE_MAX_METHOD)
2726 		efx->reset_pending &= -(1 << (method + 1));
2727 	else /* it doesn't fit into the well-ordered scope hierarchy */
2728 		__clear_bit(method, &efx->reset_pending);
2729 
2730 	/* Reinitialise bus-mastering, which may have been turned off before
2731 	 * the reset was scheduled. This is still appropriate, even in the
2732 	 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2733 	 * can respond to requests. */
2734 	pci_set_master(efx->pci_dev);
2735 
2736 out:
2737 	/* Leave device stopped if necessary */
2738 	disabled = rc ||
2739 		method == RESET_TYPE_DISABLE ||
2740 		method == RESET_TYPE_RECOVER_OR_DISABLE;
2741 	rc2 = efx_reset_up(efx, method, !disabled);
2742 	if (rc2) {
2743 		disabled = true;
2744 		if (!rc)
2745 			rc = rc2;
2746 	}
2747 
2748 	if (disabled) {
2749 		dev_close(efx->net_dev);
2750 		netif_err(efx, drv, efx->net_dev, "has been disabled\n");
2751 		efx->state = STATE_DISABLED;
2752 	} else {
2753 		netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
2754 		efx_device_attach_if_not_resetting(efx);
2755 	}
2756 	return rc;
2757 }
2758 
2759 /* Try recovery mechanisms.
2760  * For now only EEH is supported.
2761  * Returns 0 if the recovery mechanisms are unsuccessful.
2762  * Returns a non-zero value otherwise.
2763  */
2764 int efx_try_recovery(struct efx_nic *efx)
2765 {
2766 #ifdef CONFIG_EEH
2767 	/* A PCI error can occur and not be seen by EEH because nothing
2768 	 * happens on the PCI bus. In this case the driver may fail and
2769 	 * schedule a 'recover or reset', leading to this recovery handler.
2770 	 * Manually call the eeh failure check function.
2771 	 */
2772 	struct eeh_dev *eehdev = pci_dev_to_eeh_dev(efx->pci_dev);
2773 	if (eeh_dev_check_failure(eehdev)) {
2774 		/* The EEH mechanisms will handle the error and reset the
2775 		 * device if necessary.
2776 		 */
2777 		return 1;
2778 	}
2779 #endif
2780 	return 0;
2781 }
2782 
2783 static void efx_wait_for_bist_end(struct efx_nic *efx)
2784 {
2785 	int i;
2786 
2787 	for (i = 0; i < BIST_WAIT_DELAY_COUNT; ++i) {
2788 		if (efx_mcdi_poll_reboot(efx))
2789 			goto out;
2790 		msleep(BIST_WAIT_DELAY_MS);
2791 	}
2792 
2793 	netif_err(efx, drv, efx->net_dev, "Warning: No MC reboot after BIST mode\n");
2794 out:
2795 	/* Either way unset the BIST flag. If we found no reboot we probably
2796 	 * won't recover, but we should try.
2797 	 */
2798 	efx->mc_bist_for_other_fn = false;
2799 }
2800 
2801 /* The worker thread exists so that code that cannot sleep can
2802  * schedule a reset for later.
2803  */
2804 static void efx_reset_work(struct work_struct *data)
2805 {
2806 	struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
2807 	unsigned long pending;
2808 	enum reset_type method;
2809 
2810 	pending = ACCESS_ONCE(efx->reset_pending);
2811 	method = fls(pending) - 1;
2812 
2813 	if (method == RESET_TYPE_MC_BIST)
2814 		efx_wait_for_bist_end(efx);
2815 
2816 	if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
2817 	     method == RESET_TYPE_RECOVER_OR_ALL) &&
2818 	    efx_try_recovery(efx))
2819 		return;
2820 
2821 	if (!pending)
2822 		return;
2823 
2824 	rtnl_lock();
2825 
2826 	/* We checked the state in efx_schedule_reset() but it may
2827 	 * have changed by now.  Now that we have the RTNL lock,
2828 	 * it cannot change again.
2829 	 */
2830 	if (efx->state == STATE_READY)
2831 		(void)efx_reset(efx, method);
2832 
2833 	rtnl_unlock();
2834 }
2835 
2836 void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2837 {
2838 	enum reset_type method;
2839 
2840 	if (efx->state == STATE_RECOVERY) {
2841 		netif_dbg(efx, drv, efx->net_dev,
2842 			  "recovering: skip scheduling %s reset\n",
2843 			  RESET_TYPE(type));
2844 		return;
2845 	}
2846 
2847 	switch (type) {
2848 	case RESET_TYPE_INVISIBLE:
2849 	case RESET_TYPE_ALL:
2850 	case RESET_TYPE_RECOVER_OR_ALL:
2851 	case RESET_TYPE_WORLD:
2852 	case RESET_TYPE_DISABLE:
2853 	case RESET_TYPE_RECOVER_OR_DISABLE:
2854 	case RESET_TYPE_DATAPATH:
2855 	case RESET_TYPE_MC_BIST:
2856 	case RESET_TYPE_MCDI_TIMEOUT:
2857 		method = type;
2858 		netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2859 			  RESET_TYPE(method));
2860 		break;
2861 	default:
2862 		method = efx->type->map_reset_reason(type);
2863 		netif_dbg(efx, drv, efx->net_dev,
2864 			  "scheduling %s reset for %s\n",
2865 			  RESET_TYPE(method), RESET_TYPE(type));
2866 		break;
2867 	}
2868 
2869 	set_bit(method, &efx->reset_pending);
2870 	smp_mb(); /* ensure we change reset_pending before checking state */
2871 
2872 	/* If we're not READY then just leave the flags set as the cue
2873 	 * to abort probing or reschedule the reset later.
2874 	 */
2875 	if (ACCESS_ONCE(efx->state) != STATE_READY)
2876 		return;
2877 
2878 	/* efx_process_channel() will no longer read events once a
2879 	 * reset is scheduled. So switch back to poll'd MCDI completions. */
2880 	efx_mcdi_mode_poll(efx);
2881 
2882 	queue_work(reset_workqueue, &efx->reset_work);
2883 }
2884 
2885 /**************************************************************************
2886  *
2887  * List of NICs we support
2888  *
2889  **************************************************************************/
2890 
2891 /* PCI device ID table */
2892 static const struct pci_device_id efx_pci_table[] = {
2893 	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803),	/* SFC9020 */
2894 	 .driver_data = (unsigned long) &siena_a0_nic_type},
2895 	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813),	/* SFL9021 */
2896 	 .driver_data = (unsigned long) &siena_a0_nic_type},
2897 	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0903),  /* SFC9120 PF */
2898 	 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
2899 	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1903),  /* SFC9120 VF */
2900 	 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
2901 	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0923),  /* SFC9140 PF */
2902 	 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
2903 	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1923),  /* SFC9140 VF */
2904 	 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
2905 	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0a03),  /* SFC9220 PF */
2906 	 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
2907 	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1a03),  /* SFC9220 VF */
2908 	 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
2909 	{0}			/* end of list */
2910 };
2911 
2912 /**************************************************************************
2913  *
2914  * Dummy PHY/MAC operations
2915  *
2916  * Can be used for some unimplemented operations
2917  * Needed so all function pointers are valid and do not have to be tested
2918  * before use
2919  *
2920  **************************************************************************/
2921 int efx_port_dummy_op_int(struct efx_nic *efx)
2922 {
2923 	return 0;
2924 }
2925 void efx_port_dummy_op_void(struct efx_nic *efx) {}
2926 
2927 static bool efx_port_dummy_op_poll(struct efx_nic *efx)
2928 {
2929 	return false;
2930 }
2931 
2932 static const struct efx_phy_operations efx_dummy_phy_operations = {
2933 	.init		 = efx_port_dummy_op_int,
2934 	.reconfigure	 = efx_port_dummy_op_int,
2935 	.poll		 = efx_port_dummy_op_poll,
2936 	.fini		 = efx_port_dummy_op_void,
2937 };
2938 
2939 /**************************************************************************
2940  *
2941  * Data housekeeping
2942  *
2943  **************************************************************************/
2944 
2945 /* This zeroes out and then fills in the invariants in a struct
2946  * efx_nic (including all sub-structures).
2947  */
2948 static int efx_init_struct(struct efx_nic *efx,
2949 			   struct pci_dev *pci_dev, struct net_device *net_dev)
2950 {
2951 	int rc = -ENOMEM, i;
2952 
2953 	/* Initialise common structures */
2954 	INIT_LIST_HEAD(&efx->node);
2955 	INIT_LIST_HEAD(&efx->secondary_list);
2956 	spin_lock_init(&efx->biu_lock);
2957 #ifdef CONFIG_SFC_MTD
2958 	INIT_LIST_HEAD(&efx->mtd_list);
2959 #endif
2960 	INIT_WORK(&efx->reset_work, efx_reset_work);
2961 	INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
2962 	INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
2963 	efx->pci_dev = pci_dev;
2964 	efx->msg_enable = debug;
2965 	efx->state = STATE_UNINIT;
2966 	strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
2967 
2968 	efx->net_dev = net_dev;
2969 	efx->rx_prefix_size = efx->type->rx_prefix_size;
2970 	efx->rx_ip_align =
2971 		NET_IP_ALIGN ? (efx->rx_prefix_size + NET_IP_ALIGN) % 4 : 0;
2972 	efx->rx_packet_hash_offset =
2973 		efx->type->rx_hash_offset - efx->type->rx_prefix_size;
2974 	efx->rx_packet_ts_offset =
2975 		efx->type->rx_ts_offset - efx->type->rx_prefix_size;
2976 	spin_lock_init(&efx->stats_lock);
2977 	mutex_init(&efx->mac_lock);
2978 	efx->phy_op = &efx_dummy_phy_operations;
2979 	efx->mdio.dev = net_dev;
2980 	INIT_WORK(&efx->mac_work, efx_mac_work);
2981 	init_waitqueue_head(&efx->flush_wq);
2982 
2983 	for (i = 0; i < EFX_MAX_CHANNELS; i++) {
2984 		efx->channel[i] = efx_alloc_channel(efx, i, NULL);
2985 		if (!efx->channel[i])
2986 			goto fail;
2987 		efx->msi_context[i].efx = efx;
2988 		efx->msi_context[i].index = i;
2989 	}
2990 
2991 	/* Higher numbered interrupt modes are less capable! */
2992 	if (WARN_ON_ONCE(efx->type->max_interrupt_mode >
2993 			 efx->type->min_interrupt_mode)) {
2994 		rc = -EIO;
2995 		goto fail;
2996 	}
2997 	efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2998 				  interrupt_mode);
2999 	efx->interrupt_mode = min(efx->type->min_interrupt_mode,
3000 				  interrupt_mode);
3001 
3002 	/* Would be good to use the net_dev name, but we're too early */
3003 	snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
3004 		 pci_name(pci_dev));
3005 	efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
3006 	if (!efx->workqueue)
3007 		goto fail;
3008 
3009 	return 0;
3010 
3011 fail:
3012 	efx_fini_struct(efx);
3013 	return rc;
3014 }
3015 
3016 static void efx_fini_struct(struct efx_nic *efx)
3017 {
3018 	int i;
3019 
3020 	for (i = 0; i < EFX_MAX_CHANNELS; i++)
3021 		kfree(efx->channel[i]);
3022 
3023 	kfree(efx->vpd_sn);
3024 
3025 	if (efx->workqueue) {
3026 		destroy_workqueue(efx->workqueue);
3027 		efx->workqueue = NULL;
3028 	}
3029 }
3030 
3031 void efx_update_sw_stats(struct efx_nic *efx, u64 *stats)
3032 {
3033 	u64 n_rx_nodesc_trunc = 0;
3034 	struct efx_channel *channel;
3035 
3036 	efx_for_each_channel(channel, efx)
3037 		n_rx_nodesc_trunc += channel->n_rx_nodesc_trunc;
3038 	stats[GENERIC_STAT_rx_nodesc_trunc] = n_rx_nodesc_trunc;
3039 	stats[GENERIC_STAT_rx_noskb_drops] = atomic_read(&efx->n_rx_noskb_drops);
3040 }
3041 
3042 /**************************************************************************
3043  *
3044  * PCI interface
3045  *
3046  **************************************************************************/
3047 
3048 /* Main body of final NIC shutdown code
3049  * This is called only at module unload (or hotplug removal).
3050  */
3051 static void efx_pci_remove_main(struct efx_nic *efx)
3052 {
3053 	/* Flush reset_work. It can no longer be scheduled since we
3054 	 * are not READY.
3055 	 */
3056 	BUG_ON(efx->state == STATE_READY);
3057 	cancel_work_sync(&efx->reset_work);
3058 
3059 	efx_disable_interrupts(efx);
3060 	efx_nic_fini_interrupt(efx);
3061 	efx_fini_port(efx);
3062 	efx->type->fini(efx);
3063 	efx_fini_napi(efx);
3064 	efx_remove_all(efx);
3065 }
3066 
3067 /* Final NIC shutdown
3068  * This is called only at module unload (or hotplug removal).  A PF can call
3069  * this on its VFs to ensure they are unbound first.
3070  */
3071 static void efx_pci_remove(struct pci_dev *pci_dev)
3072 {
3073 	struct efx_nic *efx;
3074 
3075 	efx = pci_get_drvdata(pci_dev);
3076 	if (!efx)
3077 		return;
3078 
3079 	/* Mark the NIC as fini, then stop the interface */
3080 	rtnl_lock();
3081 	efx_dissociate(efx);
3082 	dev_close(efx->net_dev);
3083 	efx_disable_interrupts(efx);
3084 	efx->state = STATE_UNINIT;
3085 	rtnl_unlock();
3086 
3087 	if (efx->type->sriov_fini)
3088 		efx->type->sriov_fini(efx);
3089 
3090 	efx_unregister_netdev(efx);
3091 
3092 	efx_mtd_remove(efx);
3093 
3094 	efx_pci_remove_main(efx);
3095 
3096 	efx_fini_io(efx);
3097 	netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
3098 
3099 	efx_fini_struct(efx);
3100 	free_netdev(efx->net_dev);
3101 
3102 	pci_disable_pcie_error_reporting(pci_dev);
3103 };
3104 
3105 /* NIC VPD information
3106  * Called during probe to display the part number of the
3107  * installed NIC.  VPD is potentially very large but this should
3108  * always appear within the first 512 bytes.
3109  */
3110 #define SFC_VPD_LEN 512
3111 static void efx_probe_vpd_strings(struct efx_nic *efx)
3112 {
3113 	struct pci_dev *dev = efx->pci_dev;
3114 	char vpd_data[SFC_VPD_LEN];
3115 	ssize_t vpd_size;
3116 	int ro_start, ro_size, i, j;
3117 
3118 	/* Get the vpd data from the device */
3119 	vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
3120 	if (vpd_size <= 0) {
3121 		netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
3122 		return;
3123 	}
3124 
3125 	/* Get the Read only section */
3126 	ro_start = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
3127 	if (ro_start < 0) {
3128 		netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
3129 		return;
3130 	}
3131 
3132 	ro_size = pci_vpd_lrdt_size(&vpd_data[ro_start]);
3133 	j = ro_size;
3134 	i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
3135 	if (i + j > vpd_size)
3136 		j = vpd_size - i;
3137 
3138 	/* Get the Part number */
3139 	i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
3140 	if (i < 0) {
3141 		netif_err(efx, drv, efx->net_dev, "Part number not found\n");
3142 		return;
3143 	}
3144 
3145 	j = pci_vpd_info_field_size(&vpd_data[i]);
3146 	i += PCI_VPD_INFO_FLD_HDR_SIZE;
3147 	if (i + j > vpd_size) {
3148 		netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
3149 		return;
3150 	}
3151 
3152 	netif_info(efx, drv, efx->net_dev,
3153 		   "Part Number : %.*s\n", j, &vpd_data[i]);
3154 
3155 	i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
3156 	j = ro_size;
3157 	i = pci_vpd_find_info_keyword(vpd_data, i, j, "SN");
3158 	if (i < 0) {
3159 		netif_err(efx, drv, efx->net_dev, "Serial number not found\n");
3160 		return;
3161 	}
3162 
3163 	j = pci_vpd_info_field_size(&vpd_data[i]);
3164 	i += PCI_VPD_INFO_FLD_HDR_SIZE;
3165 	if (i + j > vpd_size) {
3166 		netif_err(efx, drv, efx->net_dev, "Incomplete serial number\n");
3167 		return;
3168 	}
3169 
3170 	efx->vpd_sn = kmalloc(j + 1, GFP_KERNEL);
3171 	if (!efx->vpd_sn)
3172 		return;
3173 
3174 	snprintf(efx->vpd_sn, j + 1, "%s", &vpd_data[i]);
3175 }
3176 
3177 
3178 /* Main body of NIC initialisation
3179  * This is called at module load (or hotplug insertion, theoretically).
3180  */
3181 static int efx_pci_probe_main(struct efx_nic *efx)
3182 {
3183 	int rc;
3184 
3185 	/* Do start-of-day initialisation */
3186 	rc = efx_probe_all(efx);
3187 	if (rc)
3188 		goto fail1;
3189 
3190 	efx_init_napi(efx);
3191 
3192 	rc = efx->type->init(efx);
3193 	if (rc) {
3194 		netif_err(efx, probe, efx->net_dev,
3195 			  "failed to initialise NIC\n");
3196 		goto fail3;
3197 	}
3198 
3199 	rc = efx_init_port(efx);
3200 	if (rc) {
3201 		netif_err(efx, probe, efx->net_dev,
3202 			  "failed to initialise port\n");
3203 		goto fail4;
3204 	}
3205 
3206 	rc = efx_nic_init_interrupt(efx);
3207 	if (rc)
3208 		goto fail5;
3209 	rc = efx_enable_interrupts(efx);
3210 	if (rc)
3211 		goto fail6;
3212 
3213 	return 0;
3214 
3215  fail6:
3216 	efx_nic_fini_interrupt(efx);
3217  fail5:
3218 	efx_fini_port(efx);
3219  fail4:
3220 	efx->type->fini(efx);
3221  fail3:
3222 	efx_fini_napi(efx);
3223 	efx_remove_all(efx);
3224  fail1:
3225 	return rc;
3226 }
3227 
3228 static int efx_pci_probe_post_io(struct efx_nic *efx)
3229 {
3230 	struct net_device *net_dev = efx->net_dev;
3231 	int rc = efx_pci_probe_main(efx);
3232 
3233 	if (rc)
3234 		return rc;
3235 
3236 	if (efx->type->sriov_init) {
3237 		rc = efx->type->sriov_init(efx);
3238 		if (rc)
3239 			netif_err(efx, probe, efx->net_dev,
3240 				  "SR-IOV can't be enabled rc %d\n", rc);
3241 	}
3242 
3243 	/* Determine netdevice features */
3244 	net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
3245 			      NETIF_F_TSO | NETIF_F_RXCSUM);
3246 	if (efx->type->offload_features & (NETIF_F_IPV6_CSUM | NETIF_F_HW_CSUM))
3247 		net_dev->features |= NETIF_F_TSO6;
3248 	/* Check whether device supports TSO */
3249 	if (!efx->type->tso_versions || !efx->type->tso_versions(efx))
3250 		net_dev->features &= ~NETIF_F_ALL_TSO;
3251 	/* Mask for features that also apply to VLAN devices */
3252 	net_dev->vlan_features |= (NETIF_F_HW_CSUM | NETIF_F_SG |
3253 				   NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
3254 				   NETIF_F_RXCSUM);
3255 
3256 	net_dev->hw_features = net_dev->features & ~efx->fixed_features;
3257 
3258 	/* Disable VLAN filtering by default.  It may be enforced if
3259 	 * the feature is fixed (i.e. VLAN filters are required to
3260 	 * receive VLAN tagged packets due to vPort restrictions).
3261 	 */
3262 	net_dev->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
3263 	net_dev->features |= efx->fixed_features;
3264 
3265 	rc = efx_register_netdev(efx);
3266 	if (!rc)
3267 		return 0;
3268 
3269 	efx_pci_remove_main(efx);
3270 	return rc;
3271 }
3272 
3273 /* NIC initialisation
3274  *
3275  * This is called at module load (or hotplug insertion,
3276  * theoretically).  It sets up PCI mappings, resets the NIC,
3277  * sets up and registers the network devices with the kernel and hooks
3278  * the interrupt service routine.  It does not prepare the device for
3279  * transmission; this is left to the first time one of the network
3280  * interfaces is brought up (i.e. efx_net_open).
3281  */
3282 static int efx_pci_probe(struct pci_dev *pci_dev,
3283 			 const struct pci_device_id *entry)
3284 {
3285 	struct net_device *net_dev;
3286 	struct efx_nic *efx;
3287 	int rc;
3288 
3289 	/* Allocate and initialise a struct net_device and struct efx_nic */
3290 	net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
3291 				     EFX_MAX_RX_QUEUES);
3292 	if (!net_dev)
3293 		return -ENOMEM;
3294 	efx = netdev_priv(net_dev);
3295 	efx->type = (const struct efx_nic_type *) entry->driver_data;
3296 	efx->fixed_features |= NETIF_F_HIGHDMA;
3297 
3298 	pci_set_drvdata(pci_dev, efx);
3299 	SET_NETDEV_DEV(net_dev, &pci_dev->dev);
3300 	rc = efx_init_struct(efx, pci_dev, net_dev);
3301 	if (rc)
3302 		goto fail1;
3303 
3304 	netif_info(efx, probe, efx->net_dev,
3305 		   "Solarflare NIC detected\n");
3306 
3307 	if (!efx->type->is_vf)
3308 		efx_probe_vpd_strings(efx);
3309 
3310 	/* Set up basic I/O (BAR mappings etc) */
3311 	rc = efx_init_io(efx);
3312 	if (rc)
3313 		goto fail2;
3314 
3315 	rc = efx_pci_probe_post_io(efx);
3316 	if (rc) {
3317 		/* On failure, retry once immediately.
3318 		 * If we aborted probe due to a scheduled reset, dismiss it.
3319 		 */
3320 		efx->reset_pending = 0;
3321 		rc = efx_pci_probe_post_io(efx);
3322 		if (rc) {
3323 			/* On another failure, retry once more
3324 			 * after a 50-305ms delay.
3325 			 */
3326 			unsigned char r;
3327 
3328 			get_random_bytes(&r, 1);
3329 			msleep((unsigned int)r + 50);
3330 			efx->reset_pending = 0;
3331 			rc = efx_pci_probe_post_io(efx);
3332 		}
3333 	}
3334 	if (rc)
3335 		goto fail3;
3336 
3337 	netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
3338 
3339 	/* Try to create MTDs, but allow this to fail */
3340 	rtnl_lock();
3341 	rc = efx_mtd_probe(efx);
3342 	rtnl_unlock();
3343 	if (rc && rc != -EPERM)
3344 		netif_warn(efx, probe, efx->net_dev,
3345 			   "failed to create MTDs (%d)\n", rc);
3346 
3347 	rc = pci_enable_pcie_error_reporting(pci_dev);
3348 	if (rc && rc != -EINVAL)
3349 		netif_notice(efx, probe, efx->net_dev,
3350 			     "PCIE error reporting unavailable (%d).\n",
3351 			     rc);
3352 
3353 	if (efx->type->udp_tnl_push_ports)
3354 		efx->type->udp_tnl_push_ports(efx);
3355 
3356 	return 0;
3357 
3358  fail3:
3359 	efx_fini_io(efx);
3360  fail2:
3361 	efx_fini_struct(efx);
3362  fail1:
3363 	WARN_ON(rc > 0);
3364 	netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
3365 	free_netdev(net_dev);
3366 	return rc;
3367 }
3368 
3369 /* efx_pci_sriov_configure returns the actual number of Virtual Functions
3370  * enabled on success
3371  */
3372 #ifdef CONFIG_SFC_SRIOV
3373 static int efx_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
3374 {
3375 	int rc;
3376 	struct efx_nic *efx = pci_get_drvdata(dev);
3377 
3378 	if (efx->type->sriov_configure) {
3379 		rc = efx->type->sriov_configure(efx, num_vfs);
3380 		if (rc)
3381 			return rc;
3382 		else
3383 			return num_vfs;
3384 	} else
3385 		return -EOPNOTSUPP;
3386 }
3387 #endif
3388 
3389 static int efx_pm_freeze(struct device *dev)
3390 {
3391 	struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
3392 
3393 	rtnl_lock();
3394 
3395 	if (efx->state != STATE_DISABLED) {
3396 		efx->state = STATE_UNINIT;
3397 
3398 		efx_device_detach_sync(efx);
3399 
3400 		efx_stop_all(efx);
3401 		efx_disable_interrupts(efx);
3402 	}
3403 
3404 	rtnl_unlock();
3405 
3406 	return 0;
3407 }
3408 
3409 static int efx_pm_thaw(struct device *dev)
3410 {
3411 	int rc;
3412 	struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
3413 
3414 	rtnl_lock();
3415 
3416 	if (efx->state != STATE_DISABLED) {
3417 		rc = efx_enable_interrupts(efx);
3418 		if (rc)
3419 			goto fail;
3420 
3421 		mutex_lock(&efx->mac_lock);
3422 		efx->phy_op->reconfigure(efx);
3423 		mutex_unlock(&efx->mac_lock);
3424 
3425 		efx_start_all(efx);
3426 
3427 		efx_device_attach_if_not_resetting(efx);
3428 
3429 		efx->state = STATE_READY;
3430 
3431 		efx->type->resume_wol(efx);
3432 	}
3433 
3434 	rtnl_unlock();
3435 
3436 	/* Reschedule any quenched resets scheduled during efx_pm_freeze() */
3437 	queue_work(reset_workqueue, &efx->reset_work);
3438 
3439 	return 0;
3440 
3441 fail:
3442 	rtnl_unlock();
3443 
3444 	return rc;
3445 }
3446 
3447 static int efx_pm_poweroff(struct device *dev)
3448 {
3449 	struct pci_dev *pci_dev = to_pci_dev(dev);
3450 	struct efx_nic *efx = pci_get_drvdata(pci_dev);
3451 
3452 	efx->type->fini(efx);
3453 
3454 	efx->reset_pending = 0;
3455 
3456 	pci_save_state(pci_dev);
3457 	return pci_set_power_state(pci_dev, PCI_D3hot);
3458 }
3459 
3460 /* Used for both resume and restore */
3461 static int efx_pm_resume(struct device *dev)
3462 {
3463 	struct pci_dev *pci_dev = to_pci_dev(dev);
3464 	struct efx_nic *efx = pci_get_drvdata(pci_dev);
3465 	int rc;
3466 
3467 	rc = pci_set_power_state(pci_dev, PCI_D0);
3468 	if (rc)
3469 		return rc;
3470 	pci_restore_state(pci_dev);
3471 	rc = pci_enable_device(pci_dev);
3472 	if (rc)
3473 		return rc;
3474 	pci_set_master(efx->pci_dev);
3475 	rc = efx->type->reset(efx, RESET_TYPE_ALL);
3476 	if (rc)
3477 		return rc;
3478 	rc = efx->type->init(efx);
3479 	if (rc)
3480 		return rc;
3481 	rc = efx_pm_thaw(dev);
3482 	return rc;
3483 }
3484 
3485 static int efx_pm_suspend(struct device *dev)
3486 {
3487 	int rc;
3488 
3489 	efx_pm_freeze(dev);
3490 	rc = efx_pm_poweroff(dev);
3491 	if (rc)
3492 		efx_pm_resume(dev);
3493 	return rc;
3494 }
3495 
3496 static const struct dev_pm_ops efx_pm_ops = {
3497 	.suspend	= efx_pm_suspend,
3498 	.resume		= efx_pm_resume,
3499 	.freeze		= efx_pm_freeze,
3500 	.thaw		= efx_pm_thaw,
3501 	.poweroff	= efx_pm_poweroff,
3502 	.restore	= efx_pm_resume,
3503 };
3504 
3505 /* A PCI error affecting this device was detected.
3506  * At this point MMIO and DMA may be disabled.
3507  * Stop the software path and request a slot reset.
3508  */
3509 static pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
3510 					      enum pci_channel_state state)
3511 {
3512 	pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
3513 	struct efx_nic *efx = pci_get_drvdata(pdev);
3514 
3515 	if (state == pci_channel_io_perm_failure)
3516 		return PCI_ERS_RESULT_DISCONNECT;
3517 
3518 	rtnl_lock();
3519 
3520 	if (efx->state != STATE_DISABLED) {
3521 		efx->state = STATE_RECOVERY;
3522 		efx->reset_pending = 0;
3523 
3524 		efx_device_detach_sync(efx);
3525 
3526 		efx_stop_all(efx);
3527 		efx_disable_interrupts(efx);
3528 
3529 		status = PCI_ERS_RESULT_NEED_RESET;
3530 	} else {
3531 		/* If the interface is disabled we don't want to do anything
3532 		 * with it.
3533 		 */
3534 		status = PCI_ERS_RESULT_RECOVERED;
3535 	}
3536 
3537 	rtnl_unlock();
3538 
3539 	pci_disable_device(pdev);
3540 
3541 	return status;
3542 }
3543 
3544 /* Fake a successful reset, which will be performed later in efx_io_resume. */
3545 static pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
3546 {
3547 	struct efx_nic *efx = pci_get_drvdata(pdev);
3548 	pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
3549 	int rc;
3550 
3551 	if (pci_enable_device(pdev)) {
3552 		netif_err(efx, hw, efx->net_dev,
3553 			  "Cannot re-enable PCI device after reset.\n");
3554 		status =  PCI_ERS_RESULT_DISCONNECT;
3555 	}
3556 
3557 	rc = pci_cleanup_aer_uncorrect_error_status(pdev);
3558 	if (rc) {
3559 		netif_err(efx, hw, efx->net_dev,
3560 		"pci_cleanup_aer_uncorrect_error_status failed (%d)\n", rc);
3561 		/* Non-fatal error. Continue. */
3562 	}
3563 
3564 	return status;
3565 }
3566 
3567 /* Perform the actual reset and resume I/O operations. */
3568 static void efx_io_resume(struct pci_dev *pdev)
3569 {
3570 	struct efx_nic *efx = pci_get_drvdata(pdev);
3571 	int rc;
3572 
3573 	rtnl_lock();
3574 
3575 	if (efx->state == STATE_DISABLED)
3576 		goto out;
3577 
3578 	rc = efx_reset(efx, RESET_TYPE_ALL);
3579 	if (rc) {
3580 		netif_err(efx, hw, efx->net_dev,
3581 			  "efx_reset failed after PCI error (%d)\n", rc);
3582 	} else {
3583 		efx->state = STATE_READY;
3584 		netif_dbg(efx, hw, efx->net_dev,
3585 			  "Done resetting and resuming IO after PCI error.\n");
3586 	}
3587 
3588 out:
3589 	rtnl_unlock();
3590 }
3591 
3592 /* For simplicity and reliability, we always require a slot reset and try to
3593  * reset the hardware when a pci error affecting the device is detected.
3594  * We leave both the link_reset and mmio_enabled callback unimplemented:
3595  * with our request for slot reset the mmio_enabled callback will never be
3596  * called, and the link_reset callback is not used by AER or EEH mechanisms.
3597  */
3598 static const struct pci_error_handlers efx_err_handlers = {
3599 	.error_detected = efx_io_error_detected,
3600 	.slot_reset	= efx_io_slot_reset,
3601 	.resume		= efx_io_resume,
3602 };
3603 
3604 static struct pci_driver efx_pci_driver = {
3605 	.name		= KBUILD_MODNAME,
3606 	.id_table	= efx_pci_table,
3607 	.probe		= efx_pci_probe,
3608 	.remove		= efx_pci_remove,
3609 	.driver.pm	= &efx_pm_ops,
3610 	.err_handler	= &efx_err_handlers,
3611 #ifdef CONFIG_SFC_SRIOV
3612 	.sriov_configure = efx_pci_sriov_configure,
3613 #endif
3614 };
3615 
3616 /**************************************************************************
3617  *
3618  * Kernel module interface
3619  *
3620  *************************************************************************/
3621 
3622 module_param(interrupt_mode, uint, 0444);
3623 MODULE_PARM_DESC(interrupt_mode,
3624 		 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
3625 
3626 static int __init efx_init_module(void)
3627 {
3628 	int rc;
3629 
3630 	printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
3631 
3632 	rc = register_netdevice_notifier(&efx_netdev_notifier);
3633 	if (rc)
3634 		goto err_notifier;
3635 
3636 #ifdef CONFIG_SFC_SRIOV
3637 	rc = efx_init_sriov();
3638 	if (rc)
3639 		goto err_sriov;
3640 #endif
3641 
3642 	reset_workqueue = create_singlethread_workqueue("sfc_reset");
3643 	if (!reset_workqueue) {
3644 		rc = -ENOMEM;
3645 		goto err_reset;
3646 	}
3647 
3648 	rc = pci_register_driver(&efx_pci_driver);
3649 	if (rc < 0)
3650 		goto err_pci;
3651 
3652 	return 0;
3653 
3654  err_pci:
3655 	destroy_workqueue(reset_workqueue);
3656  err_reset:
3657 #ifdef CONFIG_SFC_SRIOV
3658 	efx_fini_sriov();
3659  err_sriov:
3660 #endif
3661 	unregister_netdevice_notifier(&efx_netdev_notifier);
3662  err_notifier:
3663 	return rc;
3664 }
3665 
3666 static void __exit efx_exit_module(void)
3667 {
3668 	printk(KERN_INFO "Solarflare NET driver unloading\n");
3669 
3670 	pci_unregister_driver(&efx_pci_driver);
3671 	destroy_workqueue(reset_workqueue);
3672 #ifdef CONFIG_SFC_SRIOV
3673 	efx_fini_sriov();
3674 #endif
3675 	unregister_netdevice_notifier(&efx_netdev_notifier);
3676 
3677 }
3678 
3679 module_init(efx_init_module);
3680 module_exit(efx_exit_module);
3681 
3682 MODULE_AUTHOR("Solarflare Communications and "
3683 	      "Michael Brown <mbrown@fensystems.co.uk>");
3684 MODULE_DESCRIPTION("Solarflare network driver");
3685 MODULE_LICENSE("GPL");
3686 MODULE_DEVICE_TABLE(pci, efx_pci_table);
3687 MODULE_VERSION(EFX_DRIVER_VERSION);
3688