1 /****************************************************************************
2 * Driver for Solarflare Solarstorm network controllers and boards
3 * Copyright 2005-2006 Fen Systems Ltd.
4 * Copyright 2005-2011 Solarflare Communications Inc.
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.
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>
18 #include <linux/tcp.h>
20 #include <linux/crc32.h>
21 #include <linux/ethtool.h>
22 #include <linux/topology.h>
23 #include <linux/gfp.h>
24 #include <linux/aer.h>
25 #include <linux/interrupt.h>
26 #include "net_driver.h"
32 #include "workarounds.h"
34 /**************************************************************************
38 **************************************************************************
41 /* Loopback mode names (see LOOPBACK_MODE()) */
42 const unsigned int efx_loopback_mode_max = LOOPBACK_MAX;
43 const char *const efx_loopback_mode_names[] = {
44 [LOOPBACK_NONE] = "NONE",
45 [LOOPBACK_DATA] = "DATAPATH",
46 [LOOPBACK_GMAC] = "GMAC",
47 [LOOPBACK_XGMII] = "XGMII",
48 [LOOPBACK_XGXS] = "XGXS",
49 [LOOPBACK_XAUI] = "XAUI",
50 [LOOPBACK_GMII] = "GMII",
51 [LOOPBACK_SGMII] = "SGMII",
52 [LOOPBACK_XGBR] = "XGBR",
53 [LOOPBACK_XFI] = "XFI",
54 [LOOPBACK_XAUI_FAR] = "XAUI_FAR",
55 [LOOPBACK_GMII_FAR] = "GMII_FAR",
56 [LOOPBACK_SGMII_FAR] = "SGMII_FAR",
57 [LOOPBACK_XFI_FAR] = "XFI_FAR",
58 [LOOPBACK_GPHY] = "GPHY",
59 [LOOPBACK_PHYXS] = "PHYXS",
60 [LOOPBACK_PCS] = "PCS",
61 [LOOPBACK_PMAPMD] = "PMA/PMD",
62 [LOOPBACK_XPORT] = "XPORT",
63 [LOOPBACK_XGMII_WS] = "XGMII_WS",
64 [LOOPBACK_XAUI_WS] = "XAUI_WS",
65 [LOOPBACK_XAUI_WS_FAR] = "XAUI_WS_FAR",
66 [LOOPBACK_XAUI_WS_NEAR] = "XAUI_WS_NEAR",
67 [LOOPBACK_GMII_WS] = "GMII_WS",
68 [LOOPBACK_XFI_WS] = "XFI_WS",
69 [LOOPBACK_XFI_WS_FAR] = "XFI_WS_FAR",
70 [LOOPBACK_PHYXS_WS] = "PHYXS_WS",
73 const unsigned int efx_reset_type_max = RESET_TYPE_MAX;
74 const char *const efx_reset_type_names[] = {
75 [RESET_TYPE_INVISIBLE] = "INVISIBLE",
76 [RESET_TYPE_ALL] = "ALL",
77 [RESET_TYPE_RECOVER_OR_ALL] = "RECOVER_OR_ALL",
78 [RESET_TYPE_WORLD] = "WORLD",
79 [RESET_TYPE_RECOVER_OR_DISABLE] = "RECOVER_OR_DISABLE",
80 [RESET_TYPE_DISABLE] = "DISABLE",
81 [RESET_TYPE_TX_WATCHDOG] = "TX_WATCHDOG",
82 [RESET_TYPE_INT_ERROR] = "INT_ERROR",
83 [RESET_TYPE_RX_RECOVERY] = "RX_RECOVERY",
84 [RESET_TYPE_RX_DESC_FETCH] = "RX_DESC_FETCH",
85 [RESET_TYPE_TX_DESC_FETCH] = "TX_DESC_FETCH",
86 [RESET_TYPE_TX_SKIP] = "TX_SKIP",
87 [RESET_TYPE_MC_FAILURE] = "MC_FAILURE",
90 /* Reset workqueue. If any NIC has a hardware failure then a reset will be
91 * queued onto this work queue. This is not a per-nic work queue, because
92 * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
94 static struct workqueue_struct *reset_workqueue;
96 /**************************************************************************
100 *************************************************************************/
103 * Use separate channels for TX and RX events
105 * Set this to 1 to use separate channels for TX and RX. It allows us
106 * to control interrupt affinity separately for TX and RX.
108 * This is only used in MSI-X interrupt mode
110 static bool separate_tx_channels;
111 module_param(separate_tx_channels, bool, 0444);
112 MODULE_PARM_DESC(separate_tx_channels,
113 "Use separate channels for TX and RX");
115 /* This is the weight assigned to each of the (per-channel) virtual
118 static int napi_weight = 64;
120 /* This is the time (in jiffies) between invocations of the hardware
122 * On Falcon-based NICs, this will:
123 * - Check the on-board hardware monitor;
124 * - Poll the link state and reconfigure the hardware as necessary.
125 * On Siena-based NICs for power systems with EEH support, this will give EEH a
128 static unsigned int efx_monitor_interval = 1 * HZ;
130 /* Initial interrupt moderation settings. They can be modified after
131 * module load with ethtool.
133 * The default for RX should strike a balance between increasing the
134 * round-trip latency and reducing overhead.
136 static unsigned int rx_irq_mod_usec = 60;
138 /* Initial interrupt moderation settings. They can be modified after
139 * module load with ethtool.
141 * This default is chosen to ensure that a 10G link does not go idle
142 * while a TX queue is stopped after it has become full. A queue is
143 * restarted when it drops below half full. The time this takes (assuming
144 * worst case 3 descriptors per packet and 1024 descriptors) is
145 * 512 / 3 * 1.2 = 205 usec.
147 static unsigned int tx_irq_mod_usec = 150;
149 /* This is the first interrupt mode to try out of:
154 static unsigned int interrupt_mode;
156 /* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
157 * i.e. the number of CPUs among which we may distribute simultaneous
158 * interrupt handling.
160 * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
161 * The default (0) means to assign an interrupt to each core.
163 static unsigned int rss_cpus;
164 module_param(rss_cpus, uint, 0444);
165 MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
167 static bool phy_flash_cfg;
168 module_param(phy_flash_cfg, bool, 0644);
169 MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
171 static unsigned irq_adapt_low_thresh = 8000;
172 module_param(irq_adapt_low_thresh, uint, 0644);
173 MODULE_PARM_DESC(irq_adapt_low_thresh,
174 "Threshold score for reducing IRQ moderation");
176 static unsigned irq_adapt_high_thresh = 16000;
177 module_param(irq_adapt_high_thresh, uint, 0644);
178 MODULE_PARM_DESC(irq_adapt_high_thresh,
179 "Threshold score for increasing IRQ moderation");
181 static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
182 NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
183 NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
184 NETIF_MSG_TX_ERR | NETIF_MSG_HW);
185 module_param(debug, uint, 0);
186 MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
188 /**************************************************************************
190 * Utility functions and prototypes
192 *************************************************************************/
194 static void efx_soft_enable_interrupts(struct efx_nic *efx);
195 static void efx_soft_disable_interrupts(struct efx_nic *efx);
196 static void efx_remove_channel(struct efx_channel *channel);
197 static void efx_remove_channels(struct efx_nic *efx);
198 static const struct efx_channel_type efx_default_channel_type;
199 static void efx_remove_port(struct efx_nic *efx);
200 static void efx_init_napi_channel(struct efx_channel *channel);
201 static void efx_fini_napi(struct efx_nic *efx);
202 static void efx_fini_napi_channel(struct efx_channel *channel);
203 static void efx_fini_struct(struct efx_nic *efx);
204 static void efx_start_all(struct efx_nic *efx);
205 static void efx_stop_all(struct efx_nic *efx);
207 #define EFX_ASSERT_RESET_SERIALISED(efx) \
209 if ((efx->state == STATE_READY) || \
210 (efx->state == STATE_RECOVERY) || \
211 (efx->state == STATE_DISABLED)) \
215 static int efx_check_disabled(struct efx_nic *efx)
217 if (efx->state == STATE_DISABLED || efx->state == STATE_RECOVERY) {
218 netif_err(efx, drv, efx->net_dev,
219 "device is disabled due to earlier errors\n");
225 /**************************************************************************
227 * Event queue processing
229 *************************************************************************/
231 /* Process channel's event queue
233 * This function is responsible for processing the event queue of a
234 * single channel. The caller must guarantee that this function will
235 * never be concurrently called more than once on the same channel,
236 * though different channels may be being processed concurrently.
238 static int efx_process_channel(struct efx_channel *channel, int budget)
242 if (unlikely(!channel->enabled))
245 spent = efx_nic_process_eventq(channel, budget);
246 if (spent && efx_channel_has_rx_queue(channel)) {
247 struct efx_rx_queue *rx_queue =
248 efx_channel_get_rx_queue(channel);
250 efx_rx_flush_packet(channel);
251 if (rx_queue->enabled)
252 efx_fast_push_rx_descriptors(rx_queue);
260 * NAPI guarantees serialisation of polls of the same device, which
261 * provides the guarantee required by efx_process_channel().
263 static int efx_poll(struct napi_struct *napi, int budget)
265 struct efx_channel *channel =
266 container_of(napi, struct efx_channel, napi_str);
267 struct efx_nic *efx = channel->efx;
270 netif_vdbg(efx, intr, efx->net_dev,
271 "channel %d NAPI poll executing on CPU %d\n",
272 channel->channel, raw_smp_processor_id());
274 spent = efx_process_channel(channel, budget);
276 if (spent < budget) {
277 if (efx_channel_has_rx_queue(channel) &&
278 efx->irq_rx_adaptive &&
279 unlikely(++channel->irq_count == 1000)) {
280 if (unlikely(channel->irq_mod_score <
281 irq_adapt_low_thresh)) {
282 if (channel->irq_moderation > 1) {
283 channel->irq_moderation -= 1;
284 efx->type->push_irq_moderation(channel);
286 } else if (unlikely(channel->irq_mod_score >
287 irq_adapt_high_thresh)) {
288 if (channel->irq_moderation <
289 efx->irq_rx_moderation) {
290 channel->irq_moderation += 1;
291 efx->type->push_irq_moderation(channel);
294 channel->irq_count = 0;
295 channel->irq_mod_score = 0;
298 efx_filter_rfs_expire(channel);
300 /* There is no race here; although napi_disable() will
301 * only wait for napi_complete(), this isn't a problem
302 * since efx_nic_eventq_read_ack() will have no effect if
303 * interrupts have already been disabled.
306 efx_nic_eventq_read_ack(channel);
312 /* Create event queue
313 * Event queue memory allocations are done only once. If the channel
314 * is reset, the memory buffer will be reused; this guards against
315 * errors during channel reset and also simplifies interrupt handling.
317 static int efx_probe_eventq(struct efx_channel *channel)
319 struct efx_nic *efx = channel->efx;
320 unsigned long entries;
322 netif_dbg(efx, probe, efx->net_dev,
323 "chan %d create event queue\n", channel->channel);
325 /* Build an event queue with room for one event per tx and rx buffer,
326 * plus some extra for link state events and MCDI completions. */
327 entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
328 EFX_BUG_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
329 channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
331 return efx_nic_probe_eventq(channel);
334 /* Prepare channel's event queue */
335 static void efx_init_eventq(struct efx_channel *channel)
337 netif_dbg(channel->efx, drv, channel->efx->net_dev,
338 "chan %d init event queue\n", channel->channel);
340 channel->eventq_read_ptr = 0;
342 efx_nic_init_eventq(channel);
345 /* Enable event queue processing and NAPI */
346 static void efx_start_eventq(struct efx_channel *channel)
348 netif_dbg(channel->efx, ifup, channel->efx->net_dev,
349 "chan %d start event queue\n", channel->channel);
351 /* Make sure the NAPI handler sees the enabled flag set */
352 channel->enabled = true;
355 napi_enable(&channel->napi_str);
356 efx_nic_eventq_read_ack(channel);
359 /* Disable event queue processing and NAPI */
360 static void efx_stop_eventq(struct efx_channel *channel)
362 if (!channel->enabled)
365 napi_disable(&channel->napi_str);
366 channel->enabled = false;
369 static void efx_fini_eventq(struct efx_channel *channel)
371 netif_dbg(channel->efx, drv, channel->efx->net_dev,
372 "chan %d fini event queue\n", channel->channel);
374 efx_nic_fini_eventq(channel);
377 static void efx_remove_eventq(struct efx_channel *channel)
379 netif_dbg(channel->efx, drv, channel->efx->net_dev,
380 "chan %d remove event queue\n", channel->channel);
382 efx_nic_remove_eventq(channel);
385 /**************************************************************************
389 *************************************************************************/
391 /* Allocate and initialise a channel structure. */
392 static struct efx_channel *
393 efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
395 struct efx_channel *channel;
396 struct efx_rx_queue *rx_queue;
397 struct efx_tx_queue *tx_queue;
400 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
405 channel->channel = i;
406 channel->type = &efx_default_channel_type;
408 for (j = 0; j < EFX_TXQ_TYPES; j++) {
409 tx_queue = &channel->tx_queue[j];
411 tx_queue->queue = i * EFX_TXQ_TYPES + j;
412 tx_queue->channel = channel;
415 rx_queue = &channel->rx_queue;
417 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
418 (unsigned long)rx_queue);
423 /* Allocate and initialise a channel structure, copying parameters
424 * (but not resources) from an old channel structure.
426 static struct efx_channel *
427 efx_copy_channel(const struct efx_channel *old_channel)
429 struct efx_channel *channel;
430 struct efx_rx_queue *rx_queue;
431 struct efx_tx_queue *tx_queue;
434 channel = kmalloc(sizeof(*channel), GFP_KERNEL);
438 *channel = *old_channel;
440 channel->napi_dev = NULL;
441 memset(&channel->eventq, 0, sizeof(channel->eventq));
443 for (j = 0; j < EFX_TXQ_TYPES; j++) {
444 tx_queue = &channel->tx_queue[j];
445 if (tx_queue->channel)
446 tx_queue->channel = channel;
447 tx_queue->buffer = NULL;
448 memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
451 rx_queue = &channel->rx_queue;
452 rx_queue->buffer = NULL;
453 memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
454 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
455 (unsigned long)rx_queue);
460 static int efx_probe_channel(struct efx_channel *channel)
462 struct efx_tx_queue *tx_queue;
463 struct efx_rx_queue *rx_queue;
466 netif_dbg(channel->efx, probe, channel->efx->net_dev,
467 "creating channel %d\n", channel->channel);
469 rc = channel->type->pre_probe(channel);
473 rc = efx_probe_eventq(channel);
477 efx_for_each_channel_tx_queue(tx_queue, channel) {
478 rc = efx_probe_tx_queue(tx_queue);
483 efx_for_each_channel_rx_queue(rx_queue, channel) {
484 rc = efx_probe_rx_queue(rx_queue);
489 channel->n_rx_frm_trunc = 0;
494 efx_remove_channel(channel);
499 efx_get_channel_name(struct efx_channel *channel, char *buf, size_t len)
501 struct efx_nic *efx = channel->efx;
505 number = channel->channel;
506 if (efx->tx_channel_offset == 0) {
508 } else if (channel->channel < efx->tx_channel_offset) {
512 number -= efx->tx_channel_offset;
514 snprintf(buf, len, "%s%s-%d", efx->name, type, number);
517 static void efx_set_channel_names(struct efx_nic *efx)
519 struct efx_channel *channel;
521 efx_for_each_channel(channel, efx)
522 channel->type->get_name(channel,
523 efx->msi_context[channel->channel].name,
524 sizeof(efx->msi_context[0].name));
527 static int efx_probe_channels(struct efx_nic *efx)
529 struct efx_channel *channel;
532 /* Restart special buffer allocation */
533 efx->next_buffer_table = 0;
535 /* Probe channels in reverse, so that any 'extra' channels
536 * use the start of the buffer table. This allows the traffic
537 * channels to be resized without moving them or wasting the
538 * entries before them.
540 efx_for_each_channel_rev(channel, efx) {
541 rc = efx_probe_channel(channel);
543 netif_err(efx, probe, efx->net_dev,
544 "failed to create channel %d\n",
549 efx_set_channel_names(efx);
554 efx_remove_channels(efx);
558 /* Channels are shutdown and reinitialised whilst the NIC is running
559 * to propagate configuration changes (mtu, checksum offload), or
560 * to clear hardware error conditions
562 static void efx_start_datapath(struct efx_nic *efx)
564 bool old_rx_scatter = efx->rx_scatter;
565 struct efx_tx_queue *tx_queue;
566 struct efx_rx_queue *rx_queue;
567 struct efx_channel *channel;
570 /* Calculate the rx buffer allocation parameters required to
571 * support the current MTU, including padding for header
572 * alignment and overruns.
574 efx->rx_dma_len = (efx->type->rx_buffer_hash_size +
575 EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
576 efx->type->rx_buffer_padding);
577 rx_buf_len = (sizeof(struct efx_rx_page_state) +
578 NET_IP_ALIGN + efx->rx_dma_len);
579 if (rx_buf_len <= PAGE_SIZE) {
580 efx->rx_scatter = false;
581 efx->rx_buffer_order = 0;
582 } else if (efx->type->can_rx_scatter) {
583 BUILD_BUG_ON(EFX_RX_USR_BUF_SIZE % L1_CACHE_BYTES);
584 BUILD_BUG_ON(sizeof(struct efx_rx_page_state) +
585 2 * ALIGN(NET_IP_ALIGN + EFX_RX_USR_BUF_SIZE,
586 EFX_RX_BUF_ALIGNMENT) >
588 efx->rx_scatter = true;
589 efx->rx_dma_len = EFX_RX_USR_BUF_SIZE;
590 efx->rx_buffer_order = 0;
592 efx->rx_scatter = false;
593 efx->rx_buffer_order = get_order(rx_buf_len);
596 efx_rx_config_page_split(efx);
597 if (efx->rx_buffer_order)
598 netif_dbg(efx, drv, efx->net_dev,
599 "RX buf len=%u; page order=%u batch=%u\n",
600 efx->rx_dma_len, efx->rx_buffer_order,
601 efx->rx_pages_per_batch);
603 netif_dbg(efx, drv, efx->net_dev,
604 "RX buf len=%u step=%u bpp=%u; page batch=%u\n",
605 efx->rx_dma_len, efx->rx_page_buf_step,
606 efx->rx_bufs_per_page, efx->rx_pages_per_batch);
608 /* RX filters also have scatter-enabled flags */
609 if (efx->rx_scatter != old_rx_scatter)
610 efx_filter_update_rx_scatter(efx);
612 /* We must keep at least one descriptor in a TX ring empty.
613 * We could avoid this when the queue size does not exactly
614 * match the hardware ring size, but it's not that important.
615 * Therefore we stop the queue when one more skb might fill
616 * the ring completely. We wake it when half way back to
619 efx->txq_stop_thresh = efx->txq_entries - efx_tx_max_skb_descs(efx);
620 efx->txq_wake_thresh = efx->txq_stop_thresh / 2;
622 /* Initialise the channels */
623 efx_for_each_channel(channel, efx) {
624 efx_for_each_channel_tx_queue(tx_queue, channel)
625 efx_init_tx_queue(tx_queue);
627 efx_for_each_channel_rx_queue(rx_queue, channel) {
628 efx_init_rx_queue(rx_queue);
629 efx_nic_generate_fill_event(rx_queue);
632 WARN_ON(channel->rx_pkt_n_frags);
635 if (netif_device_present(efx->net_dev))
636 netif_tx_wake_all_queues(efx->net_dev);
639 static void efx_stop_datapath(struct efx_nic *efx)
641 struct efx_channel *channel;
642 struct efx_tx_queue *tx_queue;
643 struct efx_rx_queue *rx_queue;
644 struct pci_dev *dev = efx->pci_dev;
647 EFX_ASSERT_RESET_SERIALISED(efx);
648 BUG_ON(efx->port_enabled);
650 /* Only perform flush if dma is enabled */
651 if (dev->is_busmaster && efx->state != STATE_RECOVERY) {
652 rc = efx_nic_flush_queues(efx);
654 if (rc && EFX_WORKAROUND_7803(efx)) {
655 /* Schedule a reset to recover from the flush failure. The
656 * descriptor caches reference memory we're about to free,
657 * but falcon_reconfigure_mac_wrapper() won't reconnect
658 * the MACs because of the pending reset. */
659 netif_err(efx, drv, efx->net_dev,
660 "Resetting to recover from flush failure\n");
661 efx_schedule_reset(efx, RESET_TYPE_ALL);
663 netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
665 netif_dbg(efx, drv, efx->net_dev,
666 "successfully flushed all queues\n");
670 efx_for_each_channel(channel, efx) {
671 /* RX packet processing is pipelined, so wait for the
672 * NAPI handler to complete. At least event queue 0
673 * might be kept active by non-data events, so don't
674 * use napi_synchronize() but actually disable NAPI
677 if (efx_channel_has_rx_queue(channel)) {
678 efx_stop_eventq(channel);
679 efx_start_eventq(channel);
682 efx_for_each_channel_rx_queue(rx_queue, channel)
683 efx_fini_rx_queue(rx_queue);
684 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
685 efx_fini_tx_queue(tx_queue);
689 static void efx_remove_channel(struct efx_channel *channel)
691 struct efx_tx_queue *tx_queue;
692 struct efx_rx_queue *rx_queue;
694 netif_dbg(channel->efx, drv, channel->efx->net_dev,
695 "destroy chan %d\n", channel->channel);
697 efx_for_each_channel_rx_queue(rx_queue, channel)
698 efx_remove_rx_queue(rx_queue);
699 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
700 efx_remove_tx_queue(tx_queue);
701 efx_remove_eventq(channel);
702 channel->type->post_remove(channel);
705 static void efx_remove_channels(struct efx_nic *efx)
707 struct efx_channel *channel;
709 efx_for_each_channel(channel, efx)
710 efx_remove_channel(channel);
714 efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
716 struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
717 u32 old_rxq_entries, old_txq_entries;
718 unsigned i, next_buffer_table = 0;
721 rc = efx_check_disabled(efx);
725 /* Not all channels should be reallocated. We must avoid
726 * reallocating their buffer table entries.
728 efx_for_each_channel(channel, efx) {
729 struct efx_rx_queue *rx_queue;
730 struct efx_tx_queue *tx_queue;
732 if (channel->type->copy)
734 next_buffer_table = max(next_buffer_table,
735 channel->eventq.index +
736 channel->eventq.entries);
737 efx_for_each_channel_rx_queue(rx_queue, channel)
738 next_buffer_table = max(next_buffer_table,
739 rx_queue->rxd.index +
740 rx_queue->rxd.entries);
741 efx_for_each_channel_tx_queue(tx_queue, channel)
742 next_buffer_table = max(next_buffer_table,
743 tx_queue->txd.index +
744 tx_queue->txd.entries);
747 efx_device_detach_sync(efx);
749 efx_soft_disable_interrupts(efx);
751 /* Clone channels (where possible) */
752 memset(other_channel, 0, sizeof(other_channel));
753 for (i = 0; i < efx->n_channels; i++) {
754 channel = efx->channel[i];
755 if (channel->type->copy)
756 channel = channel->type->copy(channel);
761 other_channel[i] = channel;
764 /* Swap entry counts and channel pointers */
765 old_rxq_entries = efx->rxq_entries;
766 old_txq_entries = efx->txq_entries;
767 efx->rxq_entries = rxq_entries;
768 efx->txq_entries = txq_entries;
769 for (i = 0; i < efx->n_channels; i++) {
770 channel = efx->channel[i];
771 efx->channel[i] = other_channel[i];
772 other_channel[i] = channel;
775 /* Restart buffer table allocation */
776 efx->next_buffer_table = next_buffer_table;
778 for (i = 0; i < efx->n_channels; i++) {
779 channel = efx->channel[i];
780 if (!channel->type->copy)
782 rc = efx_probe_channel(channel);
785 efx_init_napi_channel(efx->channel[i]);
789 /* Destroy unused channel structures */
790 for (i = 0; i < efx->n_channels; i++) {
791 channel = other_channel[i];
792 if (channel && channel->type->copy) {
793 efx_fini_napi_channel(channel);
794 efx_remove_channel(channel);
799 efx_soft_enable_interrupts(efx);
801 netif_device_attach(efx->net_dev);
806 efx->rxq_entries = old_rxq_entries;
807 efx->txq_entries = old_txq_entries;
808 for (i = 0; i < efx->n_channels; i++) {
809 channel = efx->channel[i];
810 efx->channel[i] = other_channel[i];
811 other_channel[i] = channel;
816 void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
818 mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
821 static const struct efx_channel_type efx_default_channel_type = {
822 .pre_probe = efx_channel_dummy_op_int,
823 .post_remove = efx_channel_dummy_op_void,
824 .get_name = efx_get_channel_name,
825 .copy = efx_copy_channel,
826 .keep_eventq = false,
829 int efx_channel_dummy_op_int(struct efx_channel *channel)
834 void efx_channel_dummy_op_void(struct efx_channel *channel)
838 /**************************************************************************
842 **************************************************************************/
844 /* This ensures that the kernel is kept informed (via
845 * netif_carrier_on/off) of the link status, and also maintains the
846 * link status's stop on the port's TX queue.
848 void efx_link_status_changed(struct efx_nic *efx)
850 struct efx_link_state *link_state = &efx->link_state;
852 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
853 * that no events are triggered between unregister_netdev() and the
854 * driver unloading. A more general condition is that NETDEV_CHANGE
855 * can only be generated between NETDEV_UP and NETDEV_DOWN */
856 if (!netif_running(efx->net_dev))
859 if (link_state->up != netif_carrier_ok(efx->net_dev)) {
860 efx->n_link_state_changes++;
863 netif_carrier_on(efx->net_dev);
865 netif_carrier_off(efx->net_dev);
868 /* Status message for kernel log */
870 netif_info(efx, link, efx->net_dev,
871 "link up at %uMbps %s-duplex (MTU %d)%s\n",
872 link_state->speed, link_state->fd ? "full" : "half",
874 (efx->promiscuous ? " [PROMISC]" : ""));
876 netif_info(efx, link, efx->net_dev, "link down\n");
879 void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
881 efx->link_advertising = advertising;
883 if (advertising & ADVERTISED_Pause)
884 efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
886 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
887 if (advertising & ADVERTISED_Asym_Pause)
888 efx->wanted_fc ^= EFX_FC_TX;
892 void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
894 efx->wanted_fc = wanted_fc;
895 if (efx->link_advertising) {
896 if (wanted_fc & EFX_FC_RX)
897 efx->link_advertising |= (ADVERTISED_Pause |
898 ADVERTISED_Asym_Pause);
900 efx->link_advertising &= ~(ADVERTISED_Pause |
901 ADVERTISED_Asym_Pause);
902 if (wanted_fc & EFX_FC_TX)
903 efx->link_advertising ^= ADVERTISED_Asym_Pause;
907 static void efx_fini_port(struct efx_nic *efx);
909 /* Push loopback/power/transmit disable settings to the PHY, and reconfigure
910 * the MAC appropriately. All other PHY configuration changes are pushed
911 * through phy_op->set_settings(), and pushed asynchronously to the MAC
912 * through efx_monitor().
914 * Callers must hold the mac_lock
916 int __efx_reconfigure_port(struct efx_nic *efx)
918 enum efx_phy_mode phy_mode;
921 WARN_ON(!mutex_is_locked(&efx->mac_lock));
923 /* Serialise the promiscuous flag with efx_set_rx_mode. */
924 netif_addr_lock_bh(efx->net_dev);
925 netif_addr_unlock_bh(efx->net_dev);
927 /* Disable PHY transmit in mac level loopbacks */
928 phy_mode = efx->phy_mode;
929 if (LOOPBACK_INTERNAL(efx))
930 efx->phy_mode |= PHY_MODE_TX_DISABLED;
932 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
934 rc = efx->type->reconfigure_port(efx);
937 efx->phy_mode = phy_mode;
942 /* Reinitialise the MAC to pick up new PHY settings, even if the port is
944 int efx_reconfigure_port(struct efx_nic *efx)
948 EFX_ASSERT_RESET_SERIALISED(efx);
950 mutex_lock(&efx->mac_lock);
951 rc = __efx_reconfigure_port(efx);
952 mutex_unlock(&efx->mac_lock);
957 /* Asynchronous work item for changing MAC promiscuity and multicast
958 * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
960 static void efx_mac_work(struct work_struct *data)
962 struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
964 mutex_lock(&efx->mac_lock);
965 if (efx->port_enabled)
966 efx->type->reconfigure_mac(efx);
967 mutex_unlock(&efx->mac_lock);
970 static int efx_probe_port(struct efx_nic *efx)
974 netif_dbg(efx, probe, efx->net_dev, "create port\n");
977 efx->phy_mode = PHY_MODE_SPECIAL;
979 /* Connect up MAC/PHY operations table */
980 rc = efx->type->probe_port(efx);
984 /* Initialise MAC address to permanent address */
985 memcpy(efx->net_dev->dev_addr, efx->net_dev->perm_addr, ETH_ALEN);
990 static int efx_init_port(struct efx_nic *efx)
994 netif_dbg(efx, drv, efx->net_dev, "init port\n");
996 mutex_lock(&efx->mac_lock);
998 rc = efx->phy_op->init(efx);
1002 efx->port_initialized = true;
1004 /* Reconfigure the MAC before creating dma queues (required for
1005 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
1006 efx->type->reconfigure_mac(efx);
1008 /* Ensure the PHY advertises the correct flow control settings */
1009 rc = efx->phy_op->reconfigure(efx);
1013 mutex_unlock(&efx->mac_lock);
1017 efx->phy_op->fini(efx);
1019 mutex_unlock(&efx->mac_lock);
1023 static void efx_start_port(struct efx_nic *efx)
1025 netif_dbg(efx, ifup, efx->net_dev, "start port\n");
1026 BUG_ON(efx->port_enabled);
1028 mutex_lock(&efx->mac_lock);
1029 efx->port_enabled = true;
1031 /* efx_mac_work() might have been scheduled after efx_stop_port(),
1032 * and then cancelled by efx_flush_all() */
1033 efx->type->reconfigure_mac(efx);
1035 mutex_unlock(&efx->mac_lock);
1038 /* Prevent efx_mac_work() and efx_monitor() from working */
1039 static void efx_stop_port(struct efx_nic *efx)
1041 netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
1043 mutex_lock(&efx->mac_lock);
1044 efx->port_enabled = false;
1045 mutex_unlock(&efx->mac_lock);
1047 /* Serialise against efx_set_multicast_list() */
1048 netif_addr_lock_bh(efx->net_dev);
1049 netif_addr_unlock_bh(efx->net_dev);
1052 static void efx_fini_port(struct efx_nic *efx)
1054 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
1056 if (!efx->port_initialized)
1059 efx->phy_op->fini(efx);
1060 efx->port_initialized = false;
1062 efx->link_state.up = false;
1063 efx_link_status_changed(efx);
1066 static void efx_remove_port(struct efx_nic *efx)
1068 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
1070 efx->type->remove_port(efx);
1073 /**************************************************************************
1077 **************************************************************************/
1079 /* This configures the PCI device to enable I/O and DMA. */
1080 static int efx_init_io(struct efx_nic *efx)
1082 struct pci_dev *pci_dev = efx->pci_dev;
1083 dma_addr_t dma_mask = efx->type->max_dma_mask;
1086 netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
1088 rc = pci_enable_device(pci_dev);
1090 netif_err(efx, probe, efx->net_dev,
1091 "failed to enable PCI device\n");
1095 pci_set_master(pci_dev);
1097 /* Set the PCI DMA mask. Try all possibilities from our
1098 * genuine mask down to 32 bits, because some architectures
1099 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
1100 * masks event though they reject 46 bit masks.
1102 while (dma_mask > 0x7fffffffUL) {
1103 if (dma_supported(&pci_dev->dev, dma_mask)) {
1104 rc = dma_set_mask(&pci_dev->dev, dma_mask);
1111 netif_err(efx, probe, efx->net_dev,
1112 "could not find a suitable DMA mask\n");
1115 netif_dbg(efx, probe, efx->net_dev,
1116 "using DMA mask %llx\n", (unsigned long long) dma_mask);
1117 rc = dma_set_coherent_mask(&pci_dev->dev, dma_mask);
1119 /* dma_set_coherent_mask() is not *allowed* to
1120 * fail with a mask that dma_set_mask() accepted,
1121 * but just in case...
1123 netif_err(efx, probe, efx->net_dev,
1124 "failed to set consistent DMA mask\n");
1128 efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
1129 rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
1131 netif_err(efx, probe, efx->net_dev,
1132 "request for memory BAR failed\n");
1136 efx->membase = ioremap_nocache(efx->membase_phys,
1137 efx->type->mem_map_size);
1138 if (!efx->membase) {
1139 netif_err(efx, probe, efx->net_dev,
1140 "could not map memory BAR at %llx+%x\n",
1141 (unsigned long long)efx->membase_phys,
1142 efx->type->mem_map_size);
1146 netif_dbg(efx, probe, efx->net_dev,
1147 "memory BAR at %llx+%x (virtual %p)\n",
1148 (unsigned long long)efx->membase_phys,
1149 efx->type->mem_map_size, efx->membase);
1154 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
1156 efx->membase_phys = 0;
1158 pci_disable_device(efx->pci_dev);
1163 static void efx_fini_io(struct efx_nic *efx)
1165 netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
1168 iounmap(efx->membase);
1169 efx->membase = NULL;
1172 if (efx->membase_phys) {
1173 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
1174 efx->membase_phys = 0;
1177 pci_disable_device(efx->pci_dev);
1180 static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
1182 cpumask_var_t thread_mask;
1189 if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
1190 netif_warn(efx, probe, efx->net_dev,
1191 "RSS disabled due to allocation failure\n");
1196 for_each_online_cpu(cpu) {
1197 if (!cpumask_test_cpu(cpu, thread_mask)) {
1199 cpumask_or(thread_mask, thread_mask,
1200 topology_thread_cpumask(cpu));
1204 free_cpumask_var(thread_mask);
1207 /* If RSS is requested for the PF *and* VFs then we can't write RSS
1208 * table entries that are inaccessible to VFs
1210 if (efx_sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
1211 count > efx_vf_size(efx)) {
1212 netif_warn(efx, probe, efx->net_dev,
1213 "Reducing number of RSS channels from %u to %u for "
1214 "VF support. Increase vf-msix-limit to use more "
1215 "channels on the PF.\n",
1216 count, efx_vf_size(efx));
1217 count = efx_vf_size(efx);
1223 /* Probe the number and type of interrupts we are able to obtain, and
1224 * the resulting numbers of channels and RX queues.
1226 static int efx_probe_interrupts(struct efx_nic *efx)
1228 unsigned int max_channels =
1229 min(efx->type->phys_addr_channels, EFX_MAX_CHANNELS);
1230 unsigned int extra_channels = 0;
1234 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
1235 if (efx->extra_channel_type[i])
1238 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
1239 struct msix_entry xentries[EFX_MAX_CHANNELS];
1240 unsigned int n_channels;
1242 n_channels = efx_wanted_parallelism(efx);
1243 if (separate_tx_channels)
1245 n_channels += extra_channels;
1246 n_channels = min(n_channels, max_channels);
1248 for (i = 0; i < n_channels; i++)
1249 xentries[i].entry = i;
1250 rc = pci_enable_msix(efx->pci_dev, xentries, n_channels);
1252 netif_err(efx, drv, efx->net_dev,
1253 "WARNING: Insufficient MSI-X vectors"
1254 " available (%d < %u).\n", rc, n_channels);
1255 netif_err(efx, drv, efx->net_dev,
1256 "WARNING: Performance may be reduced.\n");
1257 EFX_BUG_ON_PARANOID(rc >= n_channels);
1259 rc = pci_enable_msix(efx->pci_dev, xentries,
1264 efx->n_channels = n_channels;
1265 if (n_channels > extra_channels)
1266 n_channels -= extra_channels;
1267 if (separate_tx_channels) {
1268 efx->n_tx_channels = max(n_channels / 2, 1U);
1269 efx->n_rx_channels = max(n_channels -
1273 efx->n_tx_channels = n_channels;
1274 efx->n_rx_channels = n_channels;
1276 for (i = 0; i < efx->n_channels; i++)
1277 efx_get_channel(efx, i)->irq =
1280 /* Fall back to single channel MSI */
1281 efx->interrupt_mode = EFX_INT_MODE_MSI;
1282 netif_err(efx, drv, efx->net_dev,
1283 "could not enable MSI-X\n");
1287 /* Try single interrupt MSI */
1288 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
1289 efx->n_channels = 1;
1290 efx->n_rx_channels = 1;
1291 efx->n_tx_channels = 1;
1292 rc = pci_enable_msi(efx->pci_dev);
1294 efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
1296 netif_err(efx, drv, efx->net_dev,
1297 "could not enable MSI\n");
1298 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
1302 /* Assume legacy interrupts */
1303 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
1304 efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
1305 efx->n_rx_channels = 1;
1306 efx->n_tx_channels = 1;
1307 efx->legacy_irq = efx->pci_dev->irq;
1310 /* Assign extra channels if possible */
1311 j = efx->n_channels;
1312 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
1313 if (!efx->extra_channel_type[i])
1315 if (efx->interrupt_mode != EFX_INT_MODE_MSIX ||
1316 efx->n_channels <= extra_channels) {
1317 efx->extra_channel_type[i]->handle_no_channel(efx);
1320 efx_get_channel(efx, j)->type =
1321 efx->extra_channel_type[i];
1325 /* RSS might be usable on VFs even if it is disabled on the PF */
1326 efx->rss_spread = ((efx->n_rx_channels > 1 || !efx_sriov_wanted(efx)) ?
1327 efx->n_rx_channels : efx_vf_size(efx));
1332 static void efx_soft_enable_interrupts(struct efx_nic *efx)
1334 struct efx_channel *channel;
1336 BUG_ON(efx->state == STATE_DISABLED);
1338 efx->irq_soft_enabled = true;
1341 efx_for_each_channel(channel, efx) {
1342 if (!channel->type->keep_eventq)
1343 efx_init_eventq(channel);
1344 efx_start_eventq(channel);
1347 efx_mcdi_mode_event(efx);
1350 static void efx_soft_disable_interrupts(struct efx_nic *efx)
1352 struct efx_channel *channel;
1354 if (efx->state == STATE_DISABLED)
1357 efx_mcdi_mode_poll(efx);
1359 efx->irq_soft_enabled = false;
1362 if (efx->legacy_irq)
1363 synchronize_irq(efx->legacy_irq);
1365 efx_for_each_channel(channel, efx) {
1367 synchronize_irq(channel->irq);
1369 efx_stop_eventq(channel);
1370 if (!channel->type->keep_eventq)
1371 efx_fini_eventq(channel);
1375 static void efx_enable_interrupts(struct efx_nic *efx)
1377 struct efx_channel *channel;
1379 BUG_ON(efx->state == STATE_DISABLED);
1381 if (efx->eeh_disabled_legacy_irq) {
1382 enable_irq(efx->legacy_irq);
1383 efx->eeh_disabled_legacy_irq = false;
1386 efx_nic_enable_interrupts(efx);
1388 efx_for_each_channel(channel, efx) {
1389 if (channel->type->keep_eventq)
1390 efx_init_eventq(channel);
1393 efx_soft_enable_interrupts(efx);
1396 static void efx_disable_interrupts(struct efx_nic *efx)
1398 struct efx_channel *channel;
1400 efx_soft_disable_interrupts(efx);
1402 efx_for_each_channel(channel, efx) {
1403 if (channel->type->keep_eventq)
1404 efx_fini_eventq(channel);
1407 efx_nic_disable_interrupts(efx);
1410 static void efx_remove_interrupts(struct efx_nic *efx)
1412 struct efx_channel *channel;
1414 /* Remove MSI/MSI-X interrupts */
1415 efx_for_each_channel(channel, efx)
1417 pci_disable_msi(efx->pci_dev);
1418 pci_disable_msix(efx->pci_dev);
1420 /* Remove legacy interrupt */
1421 efx->legacy_irq = 0;
1424 static void efx_set_channels(struct efx_nic *efx)
1426 struct efx_channel *channel;
1427 struct efx_tx_queue *tx_queue;
1429 efx->tx_channel_offset =
1430 separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0;
1432 /* We need to mark which channels really have RX and TX
1433 * queues, and adjust the TX queue numbers if we have separate
1434 * RX-only and TX-only channels.
1436 efx_for_each_channel(channel, efx) {
1437 if (channel->channel < efx->n_rx_channels)
1438 channel->rx_queue.core_index = channel->channel;
1440 channel->rx_queue.core_index = -1;
1442 efx_for_each_channel_tx_queue(tx_queue, channel)
1443 tx_queue->queue -= (efx->tx_channel_offset *
1448 static int efx_probe_nic(struct efx_nic *efx)
1453 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
1455 /* Carry out hardware-type specific initialisation */
1456 rc = efx->type->probe(efx);
1460 /* Determine the number of channels and queues by trying to hook
1461 * in MSI-X interrupts. */
1462 rc = efx_probe_interrupts(efx);
1466 efx->type->dimension_resources(efx);
1468 if (efx->n_channels > 1)
1469 get_random_bytes(&efx->rx_hash_key, sizeof(efx->rx_hash_key));
1470 for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
1471 efx->rx_indir_table[i] =
1472 ethtool_rxfh_indir_default(i, efx->rss_spread);
1474 efx_set_channels(efx);
1475 netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1476 netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
1478 /* Initialise the interrupt moderation settings */
1479 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
1485 efx->type->remove(efx);
1489 static void efx_remove_nic(struct efx_nic *efx)
1491 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
1493 efx_remove_interrupts(efx);
1494 efx->type->remove(efx);
1497 /**************************************************************************
1499 * NIC startup/shutdown
1501 *************************************************************************/
1503 static int efx_probe_all(struct efx_nic *efx)
1507 rc = efx_probe_nic(efx);
1509 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
1513 rc = efx_probe_port(efx);
1515 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
1519 BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
1520 if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
1524 efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
1526 rc = efx_probe_filters(efx);
1528 netif_err(efx, probe, efx->net_dev,
1529 "failed to create filter tables\n");
1533 rc = efx_probe_channels(efx);
1540 efx_remove_filters(efx);
1542 efx_remove_port(efx);
1544 efx_remove_nic(efx);
1549 /* If the interface is supposed to be running but is not, start
1550 * the hardware and software data path, regular activity for the port
1551 * (MAC statistics, link polling, etc.) and schedule the port to be
1552 * reconfigured. Interrupts must already be enabled. This function
1553 * is safe to call multiple times, so long as the NIC is not disabled.
1554 * Requires the RTNL lock.
1556 static void efx_start_all(struct efx_nic *efx)
1558 EFX_ASSERT_RESET_SERIALISED(efx);
1559 BUG_ON(efx->state == STATE_DISABLED);
1561 /* Check that it is appropriate to restart the interface. All
1562 * of these flags are safe to read under just the rtnl lock */
1563 if (efx->port_enabled || !netif_running(efx->net_dev))
1566 efx_start_port(efx);
1567 efx_start_datapath(efx);
1569 /* Start the hardware monitor if there is one */
1570 if (efx->type->monitor != NULL)
1571 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1572 efx_monitor_interval);
1574 /* If link state detection is normally event-driven, we have
1575 * to poll now because we could have missed a change
1577 if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0) {
1578 mutex_lock(&efx->mac_lock);
1579 if (efx->phy_op->poll(efx))
1580 efx_link_status_changed(efx);
1581 mutex_unlock(&efx->mac_lock);
1584 efx->type->start_stats(efx);
1587 /* Flush all delayed work. Should only be called when no more delayed work
1588 * will be scheduled. This doesn't flush pending online resets (efx_reset),
1589 * since we're holding the rtnl_lock at this point. */
1590 static void efx_flush_all(struct efx_nic *efx)
1592 /* Make sure the hardware monitor and event self-test are stopped */
1593 cancel_delayed_work_sync(&efx->monitor_work);
1594 efx_selftest_async_cancel(efx);
1595 /* Stop scheduled port reconfigurations */
1596 cancel_work_sync(&efx->mac_work);
1599 /* Quiesce the hardware and software data path, and regular activity
1600 * for the port without bringing the link down. Safe to call multiple
1601 * times with the NIC in almost any state, but interrupts should be
1602 * enabled. Requires the RTNL lock.
1604 static void efx_stop_all(struct efx_nic *efx)
1606 EFX_ASSERT_RESET_SERIALISED(efx);
1608 /* port_enabled can be read safely under the rtnl lock */
1609 if (!efx->port_enabled)
1612 efx->type->stop_stats(efx);
1615 /* Flush efx_mac_work(), refill_workqueue, monitor_work */
1618 /* Stop the kernel transmit interface. This is only valid if
1619 * the device is stopped or detached; otherwise the watchdog
1620 * may fire immediately.
1622 WARN_ON(netif_running(efx->net_dev) &&
1623 netif_device_present(efx->net_dev));
1624 netif_tx_disable(efx->net_dev);
1626 efx_stop_datapath(efx);
1629 static void efx_remove_all(struct efx_nic *efx)
1631 efx_remove_channels(efx);
1632 efx_remove_filters(efx);
1633 efx_remove_port(efx);
1634 efx_remove_nic(efx);
1637 /**************************************************************************
1639 * Interrupt moderation
1641 **************************************************************************/
1643 static unsigned int irq_mod_ticks(unsigned int usecs, unsigned int quantum_ns)
1647 if (usecs * 1000 < quantum_ns)
1648 return 1; /* never round down to 0 */
1649 return usecs * 1000 / quantum_ns;
1652 /* Set interrupt moderation parameters */
1653 int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
1654 unsigned int rx_usecs, bool rx_adaptive,
1655 bool rx_may_override_tx)
1657 struct efx_channel *channel;
1658 unsigned int irq_mod_max = DIV_ROUND_UP(efx->type->timer_period_max *
1659 efx->timer_quantum_ns,
1661 unsigned int tx_ticks;
1662 unsigned int rx_ticks;
1664 EFX_ASSERT_RESET_SERIALISED(efx);
1666 if (tx_usecs > irq_mod_max || rx_usecs > irq_mod_max)
1669 tx_ticks = irq_mod_ticks(tx_usecs, efx->timer_quantum_ns);
1670 rx_ticks = irq_mod_ticks(rx_usecs, efx->timer_quantum_ns);
1672 if (tx_ticks != rx_ticks && efx->tx_channel_offset == 0 &&
1673 !rx_may_override_tx) {
1674 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
1675 "RX and TX IRQ moderation must be equal\n");
1679 efx->irq_rx_adaptive = rx_adaptive;
1680 efx->irq_rx_moderation = rx_ticks;
1681 efx_for_each_channel(channel, efx) {
1682 if (efx_channel_has_rx_queue(channel))
1683 channel->irq_moderation = rx_ticks;
1684 else if (efx_channel_has_tx_queues(channel))
1685 channel->irq_moderation = tx_ticks;
1691 void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
1692 unsigned int *rx_usecs, bool *rx_adaptive)
1694 /* We must round up when converting ticks to microseconds
1695 * because we round down when converting the other way.
1698 *rx_adaptive = efx->irq_rx_adaptive;
1699 *rx_usecs = DIV_ROUND_UP(efx->irq_rx_moderation *
1700 efx->timer_quantum_ns,
1703 /* If channels are shared between RX and TX, so is IRQ
1704 * moderation. Otherwise, IRQ moderation is the same for all
1705 * TX channels and is not adaptive.
1707 if (efx->tx_channel_offset == 0)
1708 *tx_usecs = *rx_usecs;
1710 *tx_usecs = DIV_ROUND_UP(
1711 efx->channel[efx->tx_channel_offset]->irq_moderation *
1712 efx->timer_quantum_ns,
1716 /**************************************************************************
1720 **************************************************************************/
1722 /* Run periodically off the general workqueue */
1723 static void efx_monitor(struct work_struct *data)
1725 struct efx_nic *efx = container_of(data, struct efx_nic,
1728 netif_vdbg(efx, timer, efx->net_dev,
1729 "hardware monitor executing on CPU %d\n",
1730 raw_smp_processor_id());
1731 BUG_ON(efx->type->monitor == NULL);
1733 /* If the mac_lock is already held then it is likely a port
1734 * reconfiguration is already in place, which will likely do
1735 * most of the work of monitor() anyway. */
1736 if (mutex_trylock(&efx->mac_lock)) {
1737 if (efx->port_enabled)
1738 efx->type->monitor(efx);
1739 mutex_unlock(&efx->mac_lock);
1742 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1743 efx_monitor_interval);
1746 /**************************************************************************
1750 *************************************************************************/
1753 * Context: process, rtnl_lock() held.
1755 static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1757 struct efx_nic *efx = netdev_priv(net_dev);
1758 struct mii_ioctl_data *data = if_mii(ifr);
1760 if (cmd == SIOCSHWTSTAMP)
1761 return efx_ptp_ioctl(efx, ifr, cmd);
1763 /* Convert phy_id from older PRTAD/DEVAD format */
1764 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
1765 (data->phy_id & 0xfc00) == 0x0400)
1766 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
1768 return mdio_mii_ioctl(&efx->mdio, data, cmd);
1771 /**************************************************************************
1775 **************************************************************************/
1777 static void efx_init_napi_channel(struct efx_channel *channel)
1779 struct efx_nic *efx = channel->efx;
1781 channel->napi_dev = efx->net_dev;
1782 netif_napi_add(channel->napi_dev, &channel->napi_str,
1783 efx_poll, napi_weight);
1786 static void efx_init_napi(struct efx_nic *efx)
1788 struct efx_channel *channel;
1790 efx_for_each_channel(channel, efx)
1791 efx_init_napi_channel(channel);
1794 static void efx_fini_napi_channel(struct efx_channel *channel)
1796 if (channel->napi_dev)
1797 netif_napi_del(&channel->napi_str);
1798 channel->napi_dev = NULL;
1801 static void efx_fini_napi(struct efx_nic *efx)
1803 struct efx_channel *channel;
1805 efx_for_each_channel(channel, efx)
1806 efx_fini_napi_channel(channel);
1809 /**************************************************************************
1811 * Kernel netpoll interface
1813 *************************************************************************/
1815 #ifdef CONFIG_NET_POLL_CONTROLLER
1817 /* Although in the common case interrupts will be disabled, this is not
1818 * guaranteed. However, all our work happens inside the NAPI callback,
1819 * so no locking is required.
1821 static void efx_netpoll(struct net_device *net_dev)
1823 struct efx_nic *efx = netdev_priv(net_dev);
1824 struct efx_channel *channel;
1826 efx_for_each_channel(channel, efx)
1827 efx_schedule_channel(channel);
1832 /**************************************************************************
1834 * Kernel net device interface
1836 *************************************************************************/
1838 /* Context: process, rtnl_lock() held. */
1839 static int efx_net_open(struct net_device *net_dev)
1841 struct efx_nic *efx = netdev_priv(net_dev);
1844 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
1845 raw_smp_processor_id());
1847 rc = efx_check_disabled(efx);
1850 if (efx->phy_mode & PHY_MODE_SPECIAL)
1852 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
1855 /* Notify the kernel of the link state polled during driver load,
1856 * before the monitor starts running */
1857 efx_link_status_changed(efx);
1860 efx_selftest_async_start(efx);
1864 /* Context: process, rtnl_lock() held.
1865 * Note that the kernel will ignore our return code; this method
1866 * should really be a void.
1868 static int efx_net_stop(struct net_device *net_dev)
1870 struct efx_nic *efx = netdev_priv(net_dev);
1872 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
1873 raw_smp_processor_id());
1875 /* Stop the device and flush all the channels */
1881 /* Context: process, dev_base_lock or RTNL held, non-blocking. */
1882 static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev,
1883 struct rtnl_link_stats64 *stats)
1885 struct efx_nic *efx = netdev_priv(net_dev);
1886 struct efx_mac_stats *mac_stats = &efx->mac_stats;
1888 spin_lock_bh(&efx->stats_lock);
1890 efx->type->update_stats(efx);
1892 stats->rx_packets = mac_stats->rx_packets;
1893 stats->tx_packets = mac_stats->tx_packets;
1894 stats->rx_bytes = mac_stats->rx_bytes;
1895 stats->tx_bytes = mac_stats->tx_bytes;
1896 stats->rx_dropped = efx->n_rx_nodesc_drop_cnt;
1897 stats->multicast = mac_stats->rx_multicast;
1898 stats->collisions = mac_stats->tx_collision;
1899 stats->rx_length_errors = (mac_stats->rx_gtjumbo +
1900 mac_stats->rx_length_error);
1901 stats->rx_crc_errors = mac_stats->rx_bad;
1902 stats->rx_frame_errors = mac_stats->rx_align_error;
1903 stats->rx_fifo_errors = mac_stats->rx_overflow;
1904 stats->rx_missed_errors = mac_stats->rx_missed;
1905 stats->tx_window_errors = mac_stats->tx_late_collision;
1907 stats->rx_errors = (stats->rx_length_errors +
1908 stats->rx_crc_errors +
1909 stats->rx_frame_errors +
1910 mac_stats->rx_symbol_error);
1911 stats->tx_errors = (stats->tx_window_errors +
1914 spin_unlock_bh(&efx->stats_lock);
1919 /* Context: netif_tx_lock held, BHs disabled. */
1920 static void efx_watchdog(struct net_device *net_dev)
1922 struct efx_nic *efx = netdev_priv(net_dev);
1924 netif_err(efx, tx_err, efx->net_dev,
1925 "TX stuck with port_enabled=%d: resetting channels\n",
1928 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
1932 /* Context: process, rtnl_lock() held. */
1933 static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
1935 struct efx_nic *efx = netdev_priv(net_dev);
1938 rc = efx_check_disabled(efx);
1941 if (new_mtu > EFX_MAX_MTU)
1944 netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
1946 efx_device_detach_sync(efx);
1949 mutex_lock(&efx->mac_lock);
1950 net_dev->mtu = new_mtu;
1951 efx->type->reconfigure_mac(efx);
1952 mutex_unlock(&efx->mac_lock);
1955 netif_device_attach(efx->net_dev);
1959 static int efx_set_mac_address(struct net_device *net_dev, void *data)
1961 struct efx_nic *efx = netdev_priv(net_dev);
1962 struct sockaddr *addr = data;
1963 char *new_addr = addr->sa_data;
1965 if (!is_valid_ether_addr(new_addr)) {
1966 netif_err(efx, drv, efx->net_dev,
1967 "invalid ethernet MAC address requested: %pM\n",
1969 return -EADDRNOTAVAIL;
1972 memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
1973 efx_sriov_mac_address_changed(efx);
1975 /* Reconfigure the MAC */
1976 mutex_lock(&efx->mac_lock);
1977 efx->type->reconfigure_mac(efx);
1978 mutex_unlock(&efx->mac_lock);
1983 /* Context: netif_addr_lock held, BHs disabled. */
1984 static void efx_set_rx_mode(struct net_device *net_dev)
1986 struct efx_nic *efx = netdev_priv(net_dev);
1987 struct netdev_hw_addr *ha;
1988 union efx_multicast_hash *mc_hash = &efx->multicast_hash;
1992 efx->promiscuous = !!(net_dev->flags & IFF_PROMISC);
1994 /* Build multicast hash table */
1995 if (efx->promiscuous || (net_dev->flags & IFF_ALLMULTI)) {
1996 memset(mc_hash, 0xff, sizeof(*mc_hash));
1998 memset(mc_hash, 0x00, sizeof(*mc_hash));
1999 netdev_for_each_mc_addr(ha, net_dev) {
2000 crc = ether_crc_le(ETH_ALEN, ha->addr);
2001 bit = crc & (EFX_MCAST_HASH_ENTRIES - 1);
2002 __set_bit_le(bit, mc_hash);
2005 /* Broadcast packets go through the multicast hash filter.
2006 * ether_crc_le() of the broadcast address is 0xbe2612ff
2007 * so we always add bit 0xff to the mask.
2009 __set_bit_le(0xff, mc_hash);
2012 if (efx->port_enabled)
2013 queue_work(efx->workqueue, &efx->mac_work);
2014 /* Otherwise efx_start_port() will do this */
2017 static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
2019 struct efx_nic *efx = netdev_priv(net_dev);
2021 /* If disabling RX n-tuple filtering, clear existing filters */
2022 if (net_dev->features & ~data & NETIF_F_NTUPLE)
2023 efx_filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
2028 static const struct net_device_ops efx_netdev_ops = {
2029 .ndo_open = efx_net_open,
2030 .ndo_stop = efx_net_stop,
2031 .ndo_get_stats64 = efx_net_stats,
2032 .ndo_tx_timeout = efx_watchdog,
2033 .ndo_start_xmit = efx_hard_start_xmit,
2034 .ndo_validate_addr = eth_validate_addr,
2035 .ndo_do_ioctl = efx_ioctl,
2036 .ndo_change_mtu = efx_change_mtu,
2037 .ndo_set_mac_address = efx_set_mac_address,
2038 .ndo_set_rx_mode = efx_set_rx_mode,
2039 .ndo_set_features = efx_set_features,
2040 #ifdef CONFIG_SFC_SRIOV
2041 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
2042 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
2043 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
2044 .ndo_get_vf_config = efx_sriov_get_vf_config,
2046 #ifdef CONFIG_NET_POLL_CONTROLLER
2047 .ndo_poll_controller = efx_netpoll,
2049 .ndo_setup_tc = efx_setup_tc,
2050 #ifdef CONFIG_RFS_ACCEL
2051 .ndo_rx_flow_steer = efx_filter_rfs,
2055 static void efx_update_name(struct efx_nic *efx)
2057 strcpy(efx->name, efx->net_dev->name);
2058 efx_mtd_rename(efx);
2059 efx_set_channel_names(efx);
2062 static int efx_netdev_event(struct notifier_block *this,
2063 unsigned long event, void *ptr)
2065 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
2067 if (net_dev->netdev_ops == &efx_netdev_ops &&
2068 event == NETDEV_CHANGENAME)
2069 efx_update_name(netdev_priv(net_dev));
2074 static struct notifier_block efx_netdev_notifier = {
2075 .notifier_call = efx_netdev_event,
2079 show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
2081 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2082 return sprintf(buf, "%d\n", efx->phy_type);
2084 static DEVICE_ATTR(phy_type, 0444, show_phy_type, NULL);
2086 static int efx_register_netdev(struct efx_nic *efx)
2088 struct net_device *net_dev = efx->net_dev;
2089 struct efx_channel *channel;
2092 net_dev->watchdog_timeo = 5 * HZ;
2093 net_dev->irq = efx->pci_dev->irq;
2094 net_dev->netdev_ops = &efx_netdev_ops;
2095 SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
2096 net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
2100 /* Enable resets to be scheduled and check whether any were
2101 * already requested. If so, the NIC is probably hosed so we
2104 efx->state = STATE_READY;
2105 smp_mb(); /* ensure we change state before checking reset_pending */
2106 if (efx->reset_pending) {
2107 netif_err(efx, probe, efx->net_dev,
2108 "aborting probe due to scheduled reset\n");
2113 rc = dev_alloc_name(net_dev, net_dev->name);
2116 efx_update_name(efx);
2118 /* Always start with carrier off; PHY events will detect the link */
2119 netif_carrier_off(net_dev);
2121 rc = register_netdevice(net_dev);
2125 efx_for_each_channel(channel, efx) {
2126 struct efx_tx_queue *tx_queue;
2127 efx_for_each_channel_tx_queue(tx_queue, channel)
2128 efx_init_tx_queue_core_txq(tx_queue);
2133 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2135 netif_err(efx, drv, efx->net_dev,
2136 "failed to init net dev attributes\n");
2137 goto fail_registered;
2144 unregister_netdevice(net_dev);
2146 efx->state = STATE_UNINIT;
2148 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
2152 static void efx_unregister_netdev(struct efx_nic *efx)
2154 struct efx_channel *channel;
2155 struct efx_tx_queue *tx_queue;
2160 BUG_ON(netdev_priv(efx->net_dev) != efx);
2162 /* Free up any skbs still remaining. This has to happen before
2163 * we try to unregister the netdev as running their destructors
2164 * may be needed to get the device ref. count to 0. */
2165 efx_for_each_channel(channel, efx) {
2166 efx_for_each_channel_tx_queue(tx_queue, channel)
2167 efx_release_tx_buffers(tx_queue);
2170 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2171 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2174 unregister_netdevice(efx->net_dev);
2175 efx->state = STATE_UNINIT;
2179 /**************************************************************************
2181 * Device reset and suspend
2183 **************************************************************************/
2185 /* Tears down the entire software state and most of the hardware state
2187 void efx_reset_down(struct efx_nic *efx, enum reset_type method)
2189 EFX_ASSERT_RESET_SERIALISED(efx);
2192 efx_disable_interrupts(efx);
2194 mutex_lock(&efx->mac_lock);
2195 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
2196 efx->phy_op->fini(efx);
2197 efx->type->fini(efx);
2200 /* This function will always ensure that the locks acquired in
2201 * efx_reset_down() are released. A failure return code indicates
2202 * that we were unable to reinitialise the hardware, and the
2203 * driver should be disabled. If ok is false, then the rx and tx
2204 * engines are not restarted, pending a RESET_DISABLE. */
2205 int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
2209 EFX_ASSERT_RESET_SERIALISED(efx);
2211 rc = efx->type->init(efx);
2213 netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
2220 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
2221 rc = efx->phy_op->init(efx);
2224 if (efx->phy_op->reconfigure(efx))
2225 netif_err(efx, drv, efx->net_dev,
2226 "could not restore PHY settings\n");
2229 efx->type->reconfigure_mac(efx);
2231 efx_enable_interrupts(efx);
2232 efx_restore_filters(efx);
2233 efx_sriov_reset(efx);
2235 mutex_unlock(&efx->mac_lock);
2242 efx->port_initialized = false;
2244 mutex_unlock(&efx->mac_lock);
2249 /* Reset the NIC using the specified method. Note that the reset may
2250 * fail, in which case the card will be left in an unusable state.
2252 * Caller must hold the rtnl_lock.
2254 int efx_reset(struct efx_nic *efx, enum reset_type method)
2259 netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2260 RESET_TYPE(method));
2262 efx_device_detach_sync(efx);
2263 efx_reset_down(efx, method);
2265 rc = efx->type->reset(efx, method);
2267 netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
2271 /* Clear flags for the scopes we covered. We assume the NIC and
2272 * driver are now quiescent so that there is no race here.
2274 efx->reset_pending &= -(1 << (method + 1));
2276 /* Reinitialise bus-mastering, which may have been turned off before
2277 * the reset was scheduled. This is still appropriate, even in the
2278 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2279 * can respond to requests. */
2280 pci_set_master(efx->pci_dev);
2283 /* Leave device stopped if necessary */
2285 method == RESET_TYPE_DISABLE ||
2286 method == RESET_TYPE_RECOVER_OR_DISABLE;
2287 rc2 = efx_reset_up(efx, method, !disabled);
2295 dev_close(efx->net_dev);
2296 netif_err(efx, drv, efx->net_dev, "has been disabled\n");
2297 efx->state = STATE_DISABLED;
2299 netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
2300 netif_device_attach(efx->net_dev);
2305 /* Try recovery mechanisms.
2306 * For now only EEH is supported.
2307 * Returns 0 if the recovery mechanisms are unsuccessful.
2308 * Returns a non-zero value otherwise.
2310 int efx_try_recovery(struct efx_nic *efx)
2313 /* A PCI error can occur and not be seen by EEH because nothing
2314 * happens on the PCI bus. In this case the driver may fail and
2315 * schedule a 'recover or reset', leading to this recovery handler.
2316 * Manually call the eeh failure check function.
2318 struct eeh_dev *eehdev =
2319 of_node_to_eeh_dev(pci_device_to_OF_node(efx->pci_dev));
2321 if (eeh_dev_check_failure(eehdev)) {
2322 /* The EEH mechanisms will handle the error and reset the
2323 * device if necessary.
2331 /* The worker thread exists so that code that cannot sleep can
2332 * schedule a reset for later.
2334 static void efx_reset_work(struct work_struct *data)
2336 struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
2337 unsigned long pending;
2338 enum reset_type method;
2340 pending = ACCESS_ONCE(efx->reset_pending);
2341 method = fls(pending) - 1;
2343 if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
2344 method == RESET_TYPE_RECOVER_OR_ALL) &&
2345 efx_try_recovery(efx))
2353 /* We checked the state in efx_schedule_reset() but it may
2354 * have changed by now. Now that we have the RTNL lock,
2355 * it cannot change again.
2357 if (efx->state == STATE_READY)
2358 (void)efx_reset(efx, method);
2363 void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2365 enum reset_type method;
2367 if (efx->state == STATE_RECOVERY) {
2368 netif_dbg(efx, drv, efx->net_dev,
2369 "recovering: skip scheduling %s reset\n",
2375 case RESET_TYPE_INVISIBLE:
2376 case RESET_TYPE_ALL:
2377 case RESET_TYPE_RECOVER_OR_ALL:
2378 case RESET_TYPE_WORLD:
2379 case RESET_TYPE_DISABLE:
2380 case RESET_TYPE_RECOVER_OR_DISABLE:
2382 netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2383 RESET_TYPE(method));
2386 method = efx->type->map_reset_reason(type);
2387 netif_dbg(efx, drv, efx->net_dev,
2388 "scheduling %s reset for %s\n",
2389 RESET_TYPE(method), RESET_TYPE(type));
2393 set_bit(method, &efx->reset_pending);
2394 smp_mb(); /* ensure we change reset_pending before checking state */
2396 /* If we're not READY then just leave the flags set as the cue
2397 * to abort probing or reschedule the reset later.
2399 if (ACCESS_ONCE(efx->state) != STATE_READY)
2402 /* efx_process_channel() will no longer read events once a
2403 * reset is scheduled. So switch back to poll'd MCDI completions. */
2404 efx_mcdi_mode_poll(efx);
2406 queue_work(reset_workqueue, &efx->reset_work);
2409 /**************************************************************************
2411 * List of NICs we support
2413 **************************************************************************/
2415 /* PCI device ID table */
2416 static DEFINE_PCI_DEVICE_TABLE(efx_pci_table) = {
2417 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2418 PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
2419 .driver_data = (unsigned long) &falcon_a1_nic_type},
2420 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2421 PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
2422 .driver_data = (unsigned long) &falcon_b0_nic_type},
2423 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
2424 .driver_data = (unsigned long) &siena_a0_nic_type},
2425 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
2426 .driver_data = (unsigned long) &siena_a0_nic_type},
2427 {0} /* end of list */
2430 /**************************************************************************
2432 * Dummy PHY/MAC operations
2434 * Can be used for some unimplemented operations
2435 * Needed so all function pointers are valid and do not have to be tested
2438 **************************************************************************/
2439 int efx_port_dummy_op_int(struct efx_nic *efx)
2443 void efx_port_dummy_op_void(struct efx_nic *efx) {}
2445 static bool efx_port_dummy_op_poll(struct efx_nic *efx)
2450 static const struct efx_phy_operations efx_dummy_phy_operations = {
2451 .init = efx_port_dummy_op_int,
2452 .reconfigure = efx_port_dummy_op_int,
2453 .poll = efx_port_dummy_op_poll,
2454 .fini = efx_port_dummy_op_void,
2457 /**************************************************************************
2461 **************************************************************************/
2463 /* This zeroes out and then fills in the invariants in a struct
2464 * efx_nic (including all sub-structures).
2466 static int efx_init_struct(struct efx_nic *efx,
2467 struct pci_dev *pci_dev, struct net_device *net_dev)
2471 /* Initialise common structures */
2472 spin_lock_init(&efx->biu_lock);
2473 #ifdef CONFIG_SFC_MTD
2474 INIT_LIST_HEAD(&efx->mtd_list);
2476 INIT_WORK(&efx->reset_work, efx_reset_work);
2477 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
2478 INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
2479 efx->pci_dev = pci_dev;
2480 efx->msg_enable = debug;
2481 efx->state = STATE_UNINIT;
2482 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
2484 efx->net_dev = net_dev;
2485 spin_lock_init(&efx->stats_lock);
2486 mutex_init(&efx->mac_lock);
2487 efx->phy_op = &efx_dummy_phy_operations;
2488 efx->mdio.dev = net_dev;
2489 INIT_WORK(&efx->mac_work, efx_mac_work);
2490 init_waitqueue_head(&efx->flush_wq);
2492 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
2493 efx->channel[i] = efx_alloc_channel(efx, i, NULL);
2494 if (!efx->channel[i])
2496 efx->msi_context[i].efx = efx;
2497 efx->msi_context[i].index = i;
2500 EFX_BUG_ON_PARANOID(efx->type->phys_addr_channels > EFX_MAX_CHANNELS);
2502 /* Higher numbered interrupt modes are less capable! */
2503 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2506 /* Would be good to use the net_dev name, but we're too early */
2507 snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2509 efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
2510 if (!efx->workqueue)
2516 efx_fini_struct(efx);
2520 static void efx_fini_struct(struct efx_nic *efx)
2524 for (i = 0; i < EFX_MAX_CHANNELS; i++)
2525 kfree(efx->channel[i]);
2527 if (efx->workqueue) {
2528 destroy_workqueue(efx->workqueue);
2529 efx->workqueue = NULL;
2533 /**************************************************************************
2537 **************************************************************************/
2539 /* Main body of final NIC shutdown code
2540 * This is called only at module unload (or hotplug removal).
2542 static void efx_pci_remove_main(struct efx_nic *efx)
2544 /* Flush reset_work. It can no longer be scheduled since we
2547 BUG_ON(efx->state == STATE_READY);
2548 cancel_work_sync(&efx->reset_work);
2550 efx_disable_interrupts(efx);
2551 efx_nic_fini_interrupt(efx);
2553 efx->type->fini(efx);
2555 efx_remove_all(efx);
2558 /* Final NIC shutdown
2559 * This is called only at module unload (or hotplug removal).
2561 static void efx_pci_remove(struct pci_dev *pci_dev)
2563 struct efx_nic *efx;
2565 efx = pci_get_drvdata(pci_dev);
2569 /* Mark the NIC as fini, then stop the interface */
2571 dev_close(efx->net_dev);
2572 efx_disable_interrupts(efx);
2575 efx_sriov_fini(efx);
2576 efx_unregister_netdev(efx);
2578 efx_mtd_remove(efx);
2580 efx_pci_remove_main(efx);
2583 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
2585 efx_fini_struct(efx);
2586 pci_set_drvdata(pci_dev, NULL);
2587 free_netdev(efx->net_dev);
2589 pci_disable_pcie_error_reporting(pci_dev);
2592 /* NIC VPD information
2593 * Called during probe to display the part number of the
2594 * installed NIC. VPD is potentially very large but this should
2595 * always appear within the first 512 bytes.
2597 #define SFC_VPD_LEN 512
2598 static void efx_print_product_vpd(struct efx_nic *efx)
2600 struct pci_dev *dev = efx->pci_dev;
2601 char vpd_data[SFC_VPD_LEN];
2605 /* Get the vpd data from the device */
2606 vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
2607 if (vpd_size <= 0) {
2608 netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
2612 /* Get the Read only section */
2613 i = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
2615 netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
2619 j = pci_vpd_lrdt_size(&vpd_data[i]);
2620 i += PCI_VPD_LRDT_TAG_SIZE;
2621 if (i + j > vpd_size)
2624 /* Get the Part number */
2625 i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
2627 netif_err(efx, drv, efx->net_dev, "Part number not found\n");
2631 j = pci_vpd_info_field_size(&vpd_data[i]);
2632 i += PCI_VPD_INFO_FLD_HDR_SIZE;
2633 if (i + j > vpd_size) {
2634 netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
2638 netif_info(efx, drv, efx->net_dev,
2639 "Part Number : %.*s\n", j, &vpd_data[i]);
2643 /* Main body of NIC initialisation
2644 * This is called at module load (or hotplug insertion, theoretically).
2646 static int efx_pci_probe_main(struct efx_nic *efx)
2650 /* Do start-of-day initialisation */
2651 rc = efx_probe_all(efx);
2657 rc = efx->type->init(efx);
2659 netif_err(efx, probe, efx->net_dev,
2660 "failed to initialise NIC\n");
2664 rc = efx_init_port(efx);
2666 netif_err(efx, probe, efx->net_dev,
2667 "failed to initialise port\n");
2671 rc = efx_nic_init_interrupt(efx);
2674 efx_enable_interrupts(efx);
2681 efx->type->fini(efx);
2684 efx_remove_all(efx);
2689 /* NIC initialisation
2691 * This is called at module load (or hotplug insertion,
2692 * theoretically). It sets up PCI mappings, resets the NIC,
2693 * sets up and registers the network devices with the kernel and hooks
2694 * the interrupt service routine. It does not prepare the device for
2695 * transmission; this is left to the first time one of the network
2696 * interfaces is brought up (i.e. efx_net_open).
2698 static int efx_pci_probe(struct pci_dev *pci_dev,
2699 const struct pci_device_id *entry)
2701 struct net_device *net_dev;
2702 struct efx_nic *efx;
2705 /* Allocate and initialise a struct net_device and struct efx_nic */
2706 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
2710 efx = netdev_priv(net_dev);
2711 efx->type = (const struct efx_nic_type *) entry->driver_data;
2712 net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
2713 NETIF_F_HIGHDMA | NETIF_F_TSO |
2715 if (efx->type->offload_features & NETIF_F_V6_CSUM)
2716 net_dev->features |= NETIF_F_TSO6;
2717 /* Mask for features that also apply to VLAN devices */
2718 net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
2719 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
2721 /* All offloads can be toggled */
2722 net_dev->hw_features = net_dev->features & ~NETIF_F_HIGHDMA;
2723 pci_set_drvdata(pci_dev, efx);
2724 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
2725 rc = efx_init_struct(efx, pci_dev, net_dev);
2729 netif_info(efx, probe, efx->net_dev,
2730 "Solarflare NIC detected\n");
2732 efx_print_product_vpd(efx);
2734 /* Set up basic I/O (BAR mappings etc) */
2735 rc = efx_init_io(efx);
2739 rc = efx_pci_probe_main(efx);
2743 rc = efx_register_netdev(efx);
2747 rc = efx_sriov_init(efx);
2749 netif_err(efx, probe, efx->net_dev,
2750 "SR-IOV can't be enabled rc %d\n", rc);
2752 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
2754 /* Try to create MTDs, but allow this to fail */
2756 rc = efx_mtd_probe(efx);
2759 netif_warn(efx, probe, efx->net_dev,
2760 "failed to create MTDs (%d)\n", rc);
2762 rc = pci_enable_pcie_error_reporting(pci_dev);
2763 if (rc && rc != -EINVAL)
2764 netif_warn(efx, probe, efx->net_dev,
2765 "pci_enable_pcie_error_reporting failed (%d)\n", rc);
2770 efx_pci_remove_main(efx);
2774 efx_fini_struct(efx);
2776 pci_set_drvdata(pci_dev, NULL);
2778 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
2779 free_netdev(net_dev);
2783 static int efx_pm_freeze(struct device *dev)
2785 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2789 if (efx->state != STATE_DISABLED) {
2790 efx->state = STATE_UNINIT;
2792 efx_device_detach_sync(efx);
2795 efx_disable_interrupts(efx);
2803 static int efx_pm_thaw(struct device *dev)
2805 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2809 if (efx->state != STATE_DISABLED) {
2810 efx_enable_interrupts(efx);
2812 mutex_lock(&efx->mac_lock);
2813 efx->phy_op->reconfigure(efx);
2814 mutex_unlock(&efx->mac_lock);
2818 netif_device_attach(efx->net_dev);
2820 efx->state = STATE_READY;
2822 efx->type->resume_wol(efx);
2827 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
2828 queue_work(reset_workqueue, &efx->reset_work);
2833 static int efx_pm_poweroff(struct device *dev)
2835 struct pci_dev *pci_dev = to_pci_dev(dev);
2836 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2838 efx->type->fini(efx);
2840 efx->reset_pending = 0;
2842 pci_save_state(pci_dev);
2843 return pci_set_power_state(pci_dev, PCI_D3hot);
2846 /* Used for both resume and restore */
2847 static int efx_pm_resume(struct device *dev)
2849 struct pci_dev *pci_dev = to_pci_dev(dev);
2850 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2853 rc = pci_set_power_state(pci_dev, PCI_D0);
2856 pci_restore_state(pci_dev);
2857 rc = pci_enable_device(pci_dev);
2860 pci_set_master(efx->pci_dev);
2861 rc = efx->type->reset(efx, RESET_TYPE_ALL);
2864 rc = efx->type->init(efx);
2871 static int efx_pm_suspend(struct device *dev)
2876 rc = efx_pm_poweroff(dev);
2882 static const struct dev_pm_ops efx_pm_ops = {
2883 .suspend = efx_pm_suspend,
2884 .resume = efx_pm_resume,
2885 .freeze = efx_pm_freeze,
2886 .thaw = efx_pm_thaw,
2887 .poweroff = efx_pm_poweroff,
2888 .restore = efx_pm_resume,
2891 /* A PCI error affecting this device was detected.
2892 * At this point MMIO and DMA may be disabled.
2893 * Stop the software path and request a slot reset.
2895 static pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
2896 enum pci_channel_state state)
2898 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
2899 struct efx_nic *efx = pci_get_drvdata(pdev);
2901 if (state == pci_channel_io_perm_failure)
2902 return PCI_ERS_RESULT_DISCONNECT;
2906 if (efx->state != STATE_DISABLED) {
2907 efx->state = STATE_RECOVERY;
2908 efx->reset_pending = 0;
2910 efx_device_detach_sync(efx);
2913 efx_disable_interrupts(efx);
2915 status = PCI_ERS_RESULT_NEED_RESET;
2917 /* If the interface is disabled we don't want to do anything
2920 status = PCI_ERS_RESULT_RECOVERED;
2925 pci_disable_device(pdev);
2930 /* Fake a successfull reset, which will be performed later in efx_io_resume. */
2931 static pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
2933 struct efx_nic *efx = pci_get_drvdata(pdev);
2934 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
2937 if (pci_enable_device(pdev)) {
2938 netif_err(efx, hw, efx->net_dev,
2939 "Cannot re-enable PCI device after reset.\n");
2940 status = PCI_ERS_RESULT_DISCONNECT;
2943 rc = pci_cleanup_aer_uncorrect_error_status(pdev);
2945 netif_err(efx, hw, efx->net_dev,
2946 "pci_cleanup_aer_uncorrect_error_status failed (%d)\n", rc);
2947 /* Non-fatal error. Continue. */
2953 /* Perform the actual reset and resume I/O operations. */
2954 static void efx_io_resume(struct pci_dev *pdev)
2956 struct efx_nic *efx = pci_get_drvdata(pdev);
2961 if (efx->state == STATE_DISABLED)
2964 rc = efx_reset(efx, RESET_TYPE_ALL);
2966 netif_err(efx, hw, efx->net_dev,
2967 "efx_reset failed after PCI error (%d)\n", rc);
2969 efx->state = STATE_READY;
2970 netif_dbg(efx, hw, efx->net_dev,
2971 "Done resetting and resuming IO after PCI error.\n");
2978 /* For simplicity and reliability, we always require a slot reset and try to
2979 * reset the hardware when a pci error affecting the device is detected.
2980 * We leave both the link_reset and mmio_enabled callback unimplemented:
2981 * with our request for slot reset the mmio_enabled callback will never be
2982 * called, and the link_reset callback is not used by AER or EEH mechanisms.
2984 static struct pci_error_handlers efx_err_handlers = {
2985 .error_detected = efx_io_error_detected,
2986 .slot_reset = efx_io_slot_reset,
2987 .resume = efx_io_resume,
2990 static struct pci_driver efx_pci_driver = {
2991 .name = KBUILD_MODNAME,
2992 .id_table = efx_pci_table,
2993 .probe = efx_pci_probe,
2994 .remove = efx_pci_remove,
2995 .driver.pm = &efx_pm_ops,
2996 .err_handler = &efx_err_handlers,
2999 /**************************************************************************
3001 * Kernel module interface
3003 *************************************************************************/
3005 module_param(interrupt_mode, uint, 0444);
3006 MODULE_PARM_DESC(interrupt_mode,
3007 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
3009 static int __init efx_init_module(void)
3013 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
3015 rc = register_netdevice_notifier(&efx_netdev_notifier);
3019 rc = efx_init_sriov();
3023 reset_workqueue = create_singlethread_workqueue("sfc_reset");
3024 if (!reset_workqueue) {
3029 rc = pci_register_driver(&efx_pci_driver);
3036 destroy_workqueue(reset_workqueue);
3040 unregister_netdevice_notifier(&efx_netdev_notifier);
3045 static void __exit efx_exit_module(void)
3047 printk(KERN_INFO "Solarflare NET driver unloading\n");
3049 pci_unregister_driver(&efx_pci_driver);
3050 destroy_workqueue(reset_workqueue);
3052 unregister_netdevice_notifier(&efx_netdev_notifier);
3056 module_init(efx_init_module);
3057 module_exit(efx_exit_module);
3059 MODULE_AUTHOR("Solarflare Communications and "
3060 "Michael Brown <mbrown@fensystems.co.uk>");
3061 MODULE_DESCRIPTION("Solarflare Communications network driver");
3062 MODULE_LICENSE("GPL");
3063 MODULE_DEVICE_TABLE(pci, efx_pci_table);