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/pci.h>
25 #include <linux/cpu_rmap.h>
26 #include <linux/aer.h>
27 #include "net_driver.h"
33 #include "workarounds.h"
35 /**************************************************************************
39 **************************************************************************
42 /* Loopback mode names (see LOOPBACK_MODE()) */
43 const unsigned int efx_loopback_mode_max = LOOPBACK_MAX;
44 const char *const efx_loopback_mode_names[] = {
45 [LOOPBACK_NONE] = "NONE",
46 [LOOPBACK_DATA] = "DATAPATH",
47 [LOOPBACK_GMAC] = "GMAC",
48 [LOOPBACK_XGMII] = "XGMII",
49 [LOOPBACK_XGXS] = "XGXS",
50 [LOOPBACK_XAUI] = "XAUI",
51 [LOOPBACK_GMII] = "GMII",
52 [LOOPBACK_SGMII] = "SGMII",
53 [LOOPBACK_XGBR] = "XGBR",
54 [LOOPBACK_XFI] = "XFI",
55 [LOOPBACK_XAUI_FAR] = "XAUI_FAR",
56 [LOOPBACK_GMII_FAR] = "GMII_FAR",
57 [LOOPBACK_SGMII_FAR] = "SGMII_FAR",
58 [LOOPBACK_XFI_FAR] = "XFI_FAR",
59 [LOOPBACK_GPHY] = "GPHY",
60 [LOOPBACK_PHYXS] = "PHYXS",
61 [LOOPBACK_PCS] = "PCS",
62 [LOOPBACK_PMAPMD] = "PMA/PMD",
63 [LOOPBACK_XPORT] = "XPORT",
64 [LOOPBACK_XGMII_WS] = "XGMII_WS",
65 [LOOPBACK_XAUI_WS] = "XAUI_WS",
66 [LOOPBACK_XAUI_WS_FAR] = "XAUI_WS_FAR",
67 [LOOPBACK_XAUI_WS_NEAR] = "XAUI_WS_NEAR",
68 [LOOPBACK_GMII_WS] = "GMII_WS",
69 [LOOPBACK_XFI_WS] = "XFI_WS",
70 [LOOPBACK_XFI_WS_FAR] = "XFI_WS_FAR",
71 [LOOPBACK_PHYXS_WS] = "PHYXS_WS",
74 const unsigned int efx_reset_type_max = RESET_TYPE_MAX;
75 const char *const efx_reset_type_names[] = {
76 [RESET_TYPE_INVISIBLE] = "INVISIBLE",
77 [RESET_TYPE_ALL] = "ALL",
78 [RESET_TYPE_RECOVER_OR_ALL] = "RECOVER_OR_ALL",
79 [RESET_TYPE_WORLD] = "WORLD",
80 [RESET_TYPE_RECOVER_OR_DISABLE] = "RECOVER_OR_DISABLE",
81 [RESET_TYPE_DISABLE] = "DISABLE",
82 [RESET_TYPE_TX_WATCHDOG] = "TX_WATCHDOG",
83 [RESET_TYPE_INT_ERROR] = "INT_ERROR",
84 [RESET_TYPE_RX_RECOVERY] = "RX_RECOVERY",
85 [RESET_TYPE_RX_DESC_FETCH] = "RX_DESC_FETCH",
86 [RESET_TYPE_TX_DESC_FETCH] = "TX_DESC_FETCH",
87 [RESET_TYPE_TX_SKIP] = "TX_SKIP",
88 [RESET_TYPE_MC_FAILURE] = "MC_FAILURE",
91 #define EFX_MAX_MTU (9 * 1024)
93 /* Reset workqueue. If any NIC has a hardware failure then a reset will be
94 * queued onto this work queue. This is not a per-nic work queue, because
95 * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
97 static struct workqueue_struct *reset_workqueue;
99 /**************************************************************************
101 * Configurable values
103 *************************************************************************/
106 * Use separate channels for TX and RX events
108 * Set this to 1 to use separate channels for TX and RX. It allows us
109 * to control interrupt affinity separately for TX and RX.
111 * This is only used in MSI-X interrupt mode
113 static bool separate_tx_channels;
114 module_param(separate_tx_channels, bool, 0444);
115 MODULE_PARM_DESC(separate_tx_channels,
116 "Use separate channels for TX and RX");
118 /* This is the weight assigned to each of the (per-channel) virtual
121 static int napi_weight = 64;
123 /* This is the time (in jiffies) between invocations of the hardware
125 * On Falcon-based NICs, this will:
126 * - Check the on-board hardware monitor;
127 * - Poll the link state and reconfigure the hardware as necessary.
128 * On Siena-based NICs for power systems with EEH support, this will give EEH a
131 static unsigned int efx_monitor_interval = 1 * HZ;
133 /* Initial interrupt moderation settings. They can be modified after
134 * module load with ethtool.
136 * The default for RX should strike a balance between increasing the
137 * round-trip latency and reducing overhead.
139 static unsigned int rx_irq_mod_usec = 60;
141 /* Initial interrupt moderation settings. They can be modified after
142 * module load with ethtool.
144 * This default is chosen to ensure that a 10G link does not go idle
145 * while a TX queue is stopped after it has become full. A queue is
146 * restarted when it drops below half full. The time this takes (assuming
147 * worst case 3 descriptors per packet and 1024 descriptors) is
148 * 512 / 3 * 1.2 = 205 usec.
150 static unsigned int tx_irq_mod_usec = 150;
152 /* This is the first interrupt mode to try out of:
157 static unsigned int interrupt_mode;
159 /* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
160 * i.e. the number of CPUs among which we may distribute simultaneous
161 * interrupt handling.
163 * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
164 * The default (0) means to assign an interrupt to each core.
166 static unsigned int rss_cpus;
167 module_param(rss_cpus, uint, 0444);
168 MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
170 static bool phy_flash_cfg;
171 module_param(phy_flash_cfg, bool, 0644);
172 MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
174 static unsigned irq_adapt_low_thresh = 8000;
175 module_param(irq_adapt_low_thresh, uint, 0644);
176 MODULE_PARM_DESC(irq_adapt_low_thresh,
177 "Threshold score for reducing IRQ moderation");
179 static unsigned irq_adapt_high_thresh = 16000;
180 module_param(irq_adapt_high_thresh, uint, 0644);
181 MODULE_PARM_DESC(irq_adapt_high_thresh,
182 "Threshold score for increasing IRQ moderation");
184 static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
185 NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
186 NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
187 NETIF_MSG_TX_ERR | NETIF_MSG_HW);
188 module_param(debug, uint, 0);
189 MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
191 /**************************************************************************
193 * Utility functions and prototypes
195 *************************************************************************/
197 static void efx_start_interrupts(struct efx_nic *efx, bool may_keep_eventq);
198 static void efx_stop_interrupts(struct efx_nic *efx, bool may_keep_eventq);
199 static void efx_remove_channel(struct efx_channel *channel);
200 static void efx_remove_channels(struct efx_nic *efx);
201 static const struct efx_channel_type efx_default_channel_type;
202 static void efx_remove_port(struct efx_nic *efx);
203 static void efx_init_napi_channel(struct efx_channel *channel);
204 static void efx_fini_napi(struct efx_nic *efx);
205 static void efx_fini_napi_channel(struct efx_channel *channel);
206 static void efx_fini_struct(struct efx_nic *efx);
207 static void efx_start_all(struct efx_nic *efx);
208 static void efx_stop_all(struct efx_nic *efx);
210 #define EFX_ASSERT_RESET_SERIALISED(efx) \
212 if ((efx->state == STATE_READY) || \
213 (efx->state == STATE_RECOVERY) || \
214 (efx->state == STATE_DISABLED)) \
218 static int efx_check_disabled(struct efx_nic *efx)
220 if (efx->state == STATE_DISABLED || efx->state == STATE_RECOVERY) {
221 netif_err(efx, drv, efx->net_dev,
222 "device is disabled due to earlier errors\n");
228 /**************************************************************************
230 * Event queue processing
232 *************************************************************************/
234 /* Process channel's event queue
236 * This function is responsible for processing the event queue of a
237 * single channel. The caller must guarantee that this function will
238 * never be concurrently called more than once on the same channel,
239 * though different channels may be being processed concurrently.
241 static int efx_process_channel(struct efx_channel *channel, int budget)
245 if (unlikely(!channel->enabled))
248 spent = efx_nic_process_eventq(channel, budget);
249 if (spent && efx_channel_has_rx_queue(channel)) {
250 struct efx_rx_queue *rx_queue =
251 efx_channel_get_rx_queue(channel);
253 /* Deliver last RX packet. */
254 if (channel->rx_pkt) {
255 __efx_rx_packet(channel, channel->rx_pkt);
256 channel->rx_pkt = NULL;
258 if (rx_queue->enabled)
259 efx_fast_push_rx_descriptors(rx_queue);
265 /* Mark channel as finished processing
267 * Note that since we will not receive further interrupts for this
268 * channel before we finish processing and call the eventq_read_ack()
269 * method, there is no need to use the interrupt hold-off timers.
271 static inline void efx_channel_processed(struct efx_channel *channel)
273 /* The interrupt handler for this channel may set work_pending
274 * as soon as we acknowledge the events we've seen. Make sure
275 * it's cleared before then. */
276 channel->work_pending = false;
279 efx_nic_eventq_read_ack(channel);
284 * NAPI guarantees serialisation of polls of the same device, which
285 * provides the guarantee required by efx_process_channel().
287 static int efx_poll(struct napi_struct *napi, int budget)
289 struct efx_channel *channel =
290 container_of(napi, struct efx_channel, napi_str);
291 struct efx_nic *efx = channel->efx;
294 netif_vdbg(efx, intr, efx->net_dev,
295 "channel %d NAPI poll executing on CPU %d\n",
296 channel->channel, raw_smp_processor_id());
298 spent = efx_process_channel(channel, budget);
300 if (spent < budget) {
301 if (efx_channel_has_rx_queue(channel) &&
302 efx->irq_rx_adaptive &&
303 unlikely(++channel->irq_count == 1000)) {
304 if (unlikely(channel->irq_mod_score <
305 irq_adapt_low_thresh)) {
306 if (channel->irq_moderation > 1) {
307 channel->irq_moderation -= 1;
308 efx->type->push_irq_moderation(channel);
310 } else if (unlikely(channel->irq_mod_score >
311 irq_adapt_high_thresh)) {
312 if (channel->irq_moderation <
313 efx->irq_rx_moderation) {
314 channel->irq_moderation += 1;
315 efx->type->push_irq_moderation(channel);
318 channel->irq_count = 0;
319 channel->irq_mod_score = 0;
322 efx_filter_rfs_expire(channel);
324 /* There is no race here; although napi_disable() will
325 * only wait for napi_complete(), this isn't a problem
326 * since efx_channel_processed() will have no effect if
327 * interrupts have already been disabled.
330 efx_channel_processed(channel);
336 /* Process the eventq of the specified channel immediately on this CPU
338 * Disable hardware generated interrupts, wait for any existing
339 * processing to finish, then directly poll (and ack ) the eventq.
340 * Finally reenable NAPI and interrupts.
342 * This is for use only during a loopback self-test. It must not
343 * deliver any packets up the stack as this can result in deadlock.
345 void efx_process_channel_now(struct efx_channel *channel)
347 struct efx_nic *efx = channel->efx;
349 BUG_ON(channel->channel >= efx->n_channels);
350 BUG_ON(!channel->enabled);
351 BUG_ON(!efx->loopback_selftest);
353 /* Disable interrupts and wait for ISRs to complete */
354 efx_nic_disable_interrupts(efx);
355 if (efx->legacy_irq) {
356 synchronize_irq(efx->legacy_irq);
357 efx->legacy_irq_enabled = false;
360 synchronize_irq(channel->irq);
362 /* Wait for any NAPI processing to complete */
363 napi_disable(&channel->napi_str);
365 /* Poll the channel */
366 efx_process_channel(channel, channel->eventq_mask + 1);
368 /* Ack the eventq. This may cause an interrupt to be generated
369 * when they are reenabled */
370 efx_channel_processed(channel);
372 napi_enable(&channel->napi_str);
374 efx->legacy_irq_enabled = true;
375 efx_nic_enable_interrupts(efx);
378 /* Create event queue
379 * Event queue memory allocations are done only once. If the channel
380 * is reset, the memory buffer will be reused; this guards against
381 * errors during channel reset and also simplifies interrupt handling.
383 static int efx_probe_eventq(struct efx_channel *channel)
385 struct efx_nic *efx = channel->efx;
386 unsigned long entries;
388 netif_dbg(efx, probe, efx->net_dev,
389 "chan %d create event queue\n", channel->channel);
391 /* Build an event queue with room for one event per tx and rx buffer,
392 * plus some extra for link state events and MCDI completions. */
393 entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
394 EFX_BUG_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
395 channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
397 return efx_nic_probe_eventq(channel);
400 /* Prepare channel's event queue */
401 static void efx_init_eventq(struct efx_channel *channel)
403 netif_dbg(channel->efx, drv, channel->efx->net_dev,
404 "chan %d init event queue\n", channel->channel);
406 channel->eventq_read_ptr = 0;
408 efx_nic_init_eventq(channel);
411 /* Enable event queue processing and NAPI */
412 static void efx_start_eventq(struct efx_channel *channel)
414 netif_dbg(channel->efx, ifup, channel->efx->net_dev,
415 "chan %d start event queue\n", channel->channel);
417 /* The interrupt handler for this channel may set work_pending
418 * as soon as we enable it. Make sure it's cleared before
419 * then. Similarly, make sure it sees the enabled flag set.
421 channel->work_pending = false;
422 channel->enabled = true;
425 napi_enable(&channel->napi_str);
426 efx_nic_eventq_read_ack(channel);
429 /* Disable event queue processing and NAPI */
430 static void efx_stop_eventq(struct efx_channel *channel)
432 if (!channel->enabled)
435 napi_disable(&channel->napi_str);
436 channel->enabled = false;
439 static void efx_fini_eventq(struct efx_channel *channel)
441 netif_dbg(channel->efx, drv, channel->efx->net_dev,
442 "chan %d fini event queue\n", channel->channel);
444 efx_nic_fini_eventq(channel);
447 static void efx_remove_eventq(struct efx_channel *channel)
449 netif_dbg(channel->efx, drv, channel->efx->net_dev,
450 "chan %d remove event queue\n", channel->channel);
452 efx_nic_remove_eventq(channel);
455 /**************************************************************************
459 *************************************************************************/
461 /* Allocate and initialise a channel structure. */
462 static struct efx_channel *
463 efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
465 struct efx_channel *channel;
466 struct efx_rx_queue *rx_queue;
467 struct efx_tx_queue *tx_queue;
470 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
475 channel->channel = i;
476 channel->type = &efx_default_channel_type;
478 for (j = 0; j < EFX_TXQ_TYPES; j++) {
479 tx_queue = &channel->tx_queue[j];
481 tx_queue->queue = i * EFX_TXQ_TYPES + j;
482 tx_queue->channel = channel;
485 rx_queue = &channel->rx_queue;
487 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
488 (unsigned long)rx_queue);
493 /* Allocate and initialise a channel structure, copying parameters
494 * (but not resources) from an old channel structure.
496 static struct efx_channel *
497 efx_copy_channel(const struct efx_channel *old_channel)
499 struct efx_channel *channel;
500 struct efx_rx_queue *rx_queue;
501 struct efx_tx_queue *tx_queue;
504 channel = kmalloc(sizeof(*channel), GFP_KERNEL);
508 *channel = *old_channel;
510 channel->napi_dev = NULL;
511 memset(&channel->eventq, 0, sizeof(channel->eventq));
513 for (j = 0; j < EFX_TXQ_TYPES; j++) {
514 tx_queue = &channel->tx_queue[j];
515 if (tx_queue->channel)
516 tx_queue->channel = channel;
517 tx_queue->buffer = NULL;
518 memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
521 rx_queue = &channel->rx_queue;
522 rx_queue->buffer = NULL;
523 memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
524 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
525 (unsigned long)rx_queue);
530 static int efx_probe_channel(struct efx_channel *channel)
532 struct efx_tx_queue *tx_queue;
533 struct efx_rx_queue *rx_queue;
536 netif_dbg(channel->efx, probe, channel->efx->net_dev,
537 "creating channel %d\n", channel->channel);
539 rc = channel->type->pre_probe(channel);
543 rc = efx_probe_eventq(channel);
547 efx_for_each_channel_tx_queue(tx_queue, channel) {
548 rc = efx_probe_tx_queue(tx_queue);
553 efx_for_each_channel_rx_queue(rx_queue, channel) {
554 rc = efx_probe_rx_queue(rx_queue);
559 channel->n_rx_frm_trunc = 0;
564 efx_remove_channel(channel);
569 efx_get_channel_name(struct efx_channel *channel, char *buf, size_t len)
571 struct efx_nic *efx = channel->efx;
575 number = channel->channel;
576 if (efx->tx_channel_offset == 0) {
578 } else if (channel->channel < efx->tx_channel_offset) {
582 number -= efx->tx_channel_offset;
584 snprintf(buf, len, "%s%s-%d", efx->name, type, number);
587 static void efx_set_channel_names(struct efx_nic *efx)
589 struct efx_channel *channel;
591 efx_for_each_channel(channel, efx)
592 channel->type->get_name(channel,
593 efx->channel_name[channel->channel],
594 sizeof(efx->channel_name[0]));
597 static int efx_probe_channels(struct efx_nic *efx)
599 struct efx_channel *channel;
602 /* Restart special buffer allocation */
603 efx->next_buffer_table = 0;
605 /* Probe channels in reverse, so that any 'extra' channels
606 * use the start of the buffer table. This allows the traffic
607 * channels to be resized without moving them or wasting the
608 * entries before them.
610 efx_for_each_channel_rev(channel, efx) {
611 rc = efx_probe_channel(channel);
613 netif_err(efx, probe, efx->net_dev,
614 "failed to create channel %d\n",
619 efx_set_channel_names(efx);
624 efx_remove_channels(efx);
628 /* Channels are shutdown and reinitialised whilst the NIC is running
629 * to propagate configuration changes (mtu, checksum offload), or
630 * to clear hardware error conditions
632 static void efx_start_datapath(struct efx_nic *efx)
634 struct efx_tx_queue *tx_queue;
635 struct efx_rx_queue *rx_queue;
636 struct efx_channel *channel;
638 /* Calculate the rx buffer allocation parameters required to
639 * support the current MTU, including padding for header
640 * alignment and overruns.
642 efx->rx_dma_len = (efx->type->rx_buffer_hash_size +
643 EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
644 efx->type->rx_buffer_padding);
645 efx->rx_buffer_order = get_order(sizeof(struct efx_rx_page_state) +
646 EFX_PAGE_IP_ALIGN + efx->rx_dma_len);
648 /* We must keep at least one descriptor in a TX ring empty.
649 * We could avoid this when the queue size does not exactly
650 * match the hardware ring size, but it's not that important.
651 * Therefore we stop the queue when one more skb might fill
652 * the ring completely. We wake it when half way back to
655 efx->txq_stop_thresh = efx->txq_entries - efx_tx_max_skb_descs(efx);
656 efx->txq_wake_thresh = efx->txq_stop_thresh / 2;
658 /* Initialise the channels */
659 efx_for_each_channel(channel, efx) {
660 efx_for_each_channel_tx_queue(tx_queue, channel)
661 efx_init_tx_queue(tx_queue);
663 efx_for_each_channel_rx_queue(rx_queue, channel) {
664 efx_init_rx_queue(rx_queue);
665 efx_nic_generate_fill_event(rx_queue);
668 WARN_ON(channel->rx_pkt != NULL);
671 if (netif_device_present(efx->net_dev))
672 netif_tx_wake_all_queues(efx->net_dev);
675 static void efx_stop_datapath(struct efx_nic *efx)
677 struct efx_channel *channel;
678 struct efx_tx_queue *tx_queue;
679 struct efx_rx_queue *rx_queue;
680 struct pci_dev *dev = efx->pci_dev;
683 EFX_ASSERT_RESET_SERIALISED(efx);
684 BUG_ON(efx->port_enabled);
686 /* Only perform flush if dma is enabled */
687 if (dev->is_busmaster && efx->state != STATE_RECOVERY) {
688 rc = efx_nic_flush_queues(efx);
690 if (rc && EFX_WORKAROUND_7803(efx)) {
691 /* Schedule a reset to recover from the flush failure. The
692 * descriptor caches reference memory we're about to free,
693 * but falcon_reconfigure_mac_wrapper() won't reconnect
694 * the MACs because of the pending reset. */
695 netif_err(efx, drv, efx->net_dev,
696 "Resetting to recover from flush failure\n");
697 efx_schedule_reset(efx, RESET_TYPE_ALL);
699 netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
701 netif_dbg(efx, drv, efx->net_dev,
702 "successfully flushed all queues\n");
706 efx_for_each_channel(channel, efx) {
707 /* RX packet processing is pipelined, so wait for the
708 * NAPI handler to complete. At least event queue 0
709 * might be kept active by non-data events, so don't
710 * use napi_synchronize() but actually disable NAPI
713 if (efx_channel_has_rx_queue(channel)) {
714 efx_stop_eventq(channel);
715 efx_start_eventq(channel);
718 efx_for_each_channel_rx_queue(rx_queue, channel)
719 efx_fini_rx_queue(rx_queue);
720 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
721 efx_fini_tx_queue(tx_queue);
725 static void efx_remove_channel(struct efx_channel *channel)
727 struct efx_tx_queue *tx_queue;
728 struct efx_rx_queue *rx_queue;
730 netif_dbg(channel->efx, drv, channel->efx->net_dev,
731 "destroy chan %d\n", channel->channel);
733 efx_for_each_channel_rx_queue(rx_queue, channel)
734 efx_remove_rx_queue(rx_queue);
735 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
736 efx_remove_tx_queue(tx_queue);
737 efx_remove_eventq(channel);
738 channel->type->post_remove(channel);
741 static void efx_remove_channels(struct efx_nic *efx)
743 struct efx_channel *channel;
745 efx_for_each_channel(channel, efx)
746 efx_remove_channel(channel);
750 efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
752 struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
753 u32 old_rxq_entries, old_txq_entries;
754 unsigned i, next_buffer_table = 0;
757 rc = efx_check_disabled(efx);
761 /* Not all channels should be reallocated. We must avoid
762 * reallocating their buffer table entries.
764 efx_for_each_channel(channel, efx) {
765 struct efx_rx_queue *rx_queue;
766 struct efx_tx_queue *tx_queue;
768 if (channel->type->copy)
770 next_buffer_table = max(next_buffer_table,
771 channel->eventq.index +
772 channel->eventq.entries);
773 efx_for_each_channel_rx_queue(rx_queue, channel)
774 next_buffer_table = max(next_buffer_table,
775 rx_queue->rxd.index +
776 rx_queue->rxd.entries);
777 efx_for_each_channel_tx_queue(tx_queue, channel)
778 next_buffer_table = max(next_buffer_table,
779 tx_queue->txd.index +
780 tx_queue->txd.entries);
783 efx_device_detach_sync(efx);
785 efx_stop_interrupts(efx, true);
787 /* Clone channels (where possible) */
788 memset(other_channel, 0, sizeof(other_channel));
789 for (i = 0; i < efx->n_channels; i++) {
790 channel = efx->channel[i];
791 if (channel->type->copy)
792 channel = channel->type->copy(channel);
797 other_channel[i] = channel;
800 /* Swap entry counts and channel pointers */
801 old_rxq_entries = efx->rxq_entries;
802 old_txq_entries = efx->txq_entries;
803 efx->rxq_entries = rxq_entries;
804 efx->txq_entries = txq_entries;
805 for (i = 0; i < efx->n_channels; i++) {
806 channel = efx->channel[i];
807 efx->channel[i] = other_channel[i];
808 other_channel[i] = channel;
811 /* Restart buffer table allocation */
812 efx->next_buffer_table = next_buffer_table;
814 for (i = 0; i < efx->n_channels; i++) {
815 channel = efx->channel[i];
816 if (!channel->type->copy)
818 rc = efx_probe_channel(channel);
821 efx_init_napi_channel(efx->channel[i]);
825 /* Destroy unused channel structures */
826 for (i = 0; i < efx->n_channels; i++) {
827 channel = other_channel[i];
828 if (channel && channel->type->copy) {
829 efx_fini_napi_channel(channel);
830 efx_remove_channel(channel);
835 efx_start_interrupts(efx, true);
837 netif_device_attach(efx->net_dev);
842 efx->rxq_entries = old_rxq_entries;
843 efx->txq_entries = old_txq_entries;
844 for (i = 0; i < efx->n_channels; i++) {
845 channel = efx->channel[i];
846 efx->channel[i] = other_channel[i];
847 other_channel[i] = channel;
852 void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
854 mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
857 static const struct efx_channel_type efx_default_channel_type = {
858 .pre_probe = efx_channel_dummy_op_int,
859 .post_remove = efx_channel_dummy_op_void,
860 .get_name = efx_get_channel_name,
861 .copy = efx_copy_channel,
862 .keep_eventq = false,
865 int efx_channel_dummy_op_int(struct efx_channel *channel)
870 void efx_channel_dummy_op_void(struct efx_channel *channel)
874 /**************************************************************************
878 **************************************************************************/
880 /* This ensures that the kernel is kept informed (via
881 * netif_carrier_on/off) of the link status, and also maintains the
882 * link status's stop on the port's TX queue.
884 void efx_link_status_changed(struct efx_nic *efx)
886 struct efx_link_state *link_state = &efx->link_state;
888 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
889 * that no events are triggered between unregister_netdev() and the
890 * driver unloading. A more general condition is that NETDEV_CHANGE
891 * can only be generated between NETDEV_UP and NETDEV_DOWN */
892 if (!netif_running(efx->net_dev))
895 if (link_state->up != netif_carrier_ok(efx->net_dev)) {
896 efx->n_link_state_changes++;
899 netif_carrier_on(efx->net_dev);
901 netif_carrier_off(efx->net_dev);
904 /* Status message for kernel log */
906 netif_info(efx, link, efx->net_dev,
907 "link up at %uMbps %s-duplex (MTU %d)%s\n",
908 link_state->speed, link_state->fd ? "full" : "half",
910 (efx->promiscuous ? " [PROMISC]" : ""));
912 netif_info(efx, link, efx->net_dev, "link down\n");
915 void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
917 efx->link_advertising = advertising;
919 if (advertising & ADVERTISED_Pause)
920 efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
922 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
923 if (advertising & ADVERTISED_Asym_Pause)
924 efx->wanted_fc ^= EFX_FC_TX;
928 void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
930 efx->wanted_fc = wanted_fc;
931 if (efx->link_advertising) {
932 if (wanted_fc & EFX_FC_RX)
933 efx->link_advertising |= (ADVERTISED_Pause |
934 ADVERTISED_Asym_Pause);
936 efx->link_advertising &= ~(ADVERTISED_Pause |
937 ADVERTISED_Asym_Pause);
938 if (wanted_fc & EFX_FC_TX)
939 efx->link_advertising ^= ADVERTISED_Asym_Pause;
943 static void efx_fini_port(struct efx_nic *efx);
945 /* Push loopback/power/transmit disable settings to the PHY, and reconfigure
946 * the MAC appropriately. All other PHY configuration changes are pushed
947 * through phy_op->set_settings(), and pushed asynchronously to the MAC
948 * through efx_monitor().
950 * Callers must hold the mac_lock
952 int __efx_reconfigure_port(struct efx_nic *efx)
954 enum efx_phy_mode phy_mode;
957 WARN_ON(!mutex_is_locked(&efx->mac_lock));
959 /* Serialise the promiscuous flag with efx_set_rx_mode. */
960 netif_addr_lock_bh(efx->net_dev);
961 netif_addr_unlock_bh(efx->net_dev);
963 /* Disable PHY transmit in mac level loopbacks */
964 phy_mode = efx->phy_mode;
965 if (LOOPBACK_INTERNAL(efx))
966 efx->phy_mode |= PHY_MODE_TX_DISABLED;
968 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
970 rc = efx->type->reconfigure_port(efx);
973 efx->phy_mode = phy_mode;
978 /* Reinitialise the MAC to pick up new PHY settings, even if the port is
980 int efx_reconfigure_port(struct efx_nic *efx)
984 EFX_ASSERT_RESET_SERIALISED(efx);
986 mutex_lock(&efx->mac_lock);
987 rc = __efx_reconfigure_port(efx);
988 mutex_unlock(&efx->mac_lock);
993 /* Asynchronous work item for changing MAC promiscuity and multicast
994 * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
996 static void efx_mac_work(struct work_struct *data)
998 struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
1000 mutex_lock(&efx->mac_lock);
1001 if (efx->port_enabled)
1002 efx->type->reconfigure_mac(efx);
1003 mutex_unlock(&efx->mac_lock);
1006 static int efx_probe_port(struct efx_nic *efx)
1010 netif_dbg(efx, probe, efx->net_dev, "create port\n");
1013 efx->phy_mode = PHY_MODE_SPECIAL;
1015 /* Connect up MAC/PHY operations table */
1016 rc = efx->type->probe_port(efx);
1020 /* Initialise MAC address to permanent address */
1021 memcpy(efx->net_dev->dev_addr, efx->net_dev->perm_addr, ETH_ALEN);
1026 static int efx_init_port(struct efx_nic *efx)
1030 netif_dbg(efx, drv, efx->net_dev, "init port\n");
1032 mutex_lock(&efx->mac_lock);
1034 rc = efx->phy_op->init(efx);
1038 efx->port_initialized = true;
1040 /* Reconfigure the MAC before creating dma queues (required for
1041 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
1042 efx->type->reconfigure_mac(efx);
1044 /* Ensure the PHY advertises the correct flow control settings */
1045 rc = efx->phy_op->reconfigure(efx);
1049 mutex_unlock(&efx->mac_lock);
1053 efx->phy_op->fini(efx);
1055 mutex_unlock(&efx->mac_lock);
1059 static void efx_start_port(struct efx_nic *efx)
1061 netif_dbg(efx, ifup, efx->net_dev, "start port\n");
1062 BUG_ON(efx->port_enabled);
1064 mutex_lock(&efx->mac_lock);
1065 efx->port_enabled = true;
1067 /* efx_mac_work() might have been scheduled after efx_stop_port(),
1068 * and then cancelled by efx_flush_all() */
1069 efx->type->reconfigure_mac(efx);
1071 mutex_unlock(&efx->mac_lock);
1074 /* Prevent efx_mac_work() and efx_monitor() from working */
1075 static void efx_stop_port(struct efx_nic *efx)
1077 netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
1079 mutex_lock(&efx->mac_lock);
1080 efx->port_enabled = false;
1081 mutex_unlock(&efx->mac_lock);
1083 /* Serialise against efx_set_multicast_list() */
1084 netif_addr_lock_bh(efx->net_dev);
1085 netif_addr_unlock_bh(efx->net_dev);
1088 static void efx_fini_port(struct efx_nic *efx)
1090 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
1092 if (!efx->port_initialized)
1095 efx->phy_op->fini(efx);
1096 efx->port_initialized = false;
1098 efx->link_state.up = false;
1099 efx_link_status_changed(efx);
1102 static void efx_remove_port(struct efx_nic *efx)
1104 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
1106 efx->type->remove_port(efx);
1109 /**************************************************************************
1113 **************************************************************************/
1115 /* This configures the PCI device to enable I/O and DMA. */
1116 static int efx_init_io(struct efx_nic *efx)
1118 struct pci_dev *pci_dev = efx->pci_dev;
1119 dma_addr_t dma_mask = efx->type->max_dma_mask;
1122 netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
1124 rc = pci_enable_device(pci_dev);
1126 netif_err(efx, probe, efx->net_dev,
1127 "failed to enable PCI device\n");
1131 pci_set_master(pci_dev);
1133 /* Set the PCI DMA mask. Try all possibilities from our
1134 * genuine mask down to 32 bits, because some architectures
1135 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
1136 * masks event though they reject 46 bit masks.
1138 while (dma_mask > 0x7fffffffUL) {
1139 if (dma_supported(&pci_dev->dev, dma_mask)) {
1140 rc = dma_set_mask(&pci_dev->dev, dma_mask);
1147 netif_err(efx, probe, efx->net_dev,
1148 "could not find a suitable DMA mask\n");
1151 netif_dbg(efx, probe, efx->net_dev,
1152 "using DMA mask %llx\n", (unsigned long long) dma_mask);
1153 rc = dma_set_coherent_mask(&pci_dev->dev, dma_mask);
1155 /* dma_set_coherent_mask() is not *allowed* to
1156 * fail with a mask that dma_set_mask() accepted,
1157 * but just in case...
1159 netif_err(efx, probe, efx->net_dev,
1160 "failed to set consistent DMA mask\n");
1164 efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
1165 rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
1167 netif_err(efx, probe, efx->net_dev,
1168 "request for memory BAR failed\n");
1172 efx->membase = ioremap_nocache(efx->membase_phys,
1173 efx->type->mem_map_size);
1174 if (!efx->membase) {
1175 netif_err(efx, probe, efx->net_dev,
1176 "could not map memory BAR at %llx+%x\n",
1177 (unsigned long long)efx->membase_phys,
1178 efx->type->mem_map_size);
1182 netif_dbg(efx, probe, efx->net_dev,
1183 "memory BAR at %llx+%x (virtual %p)\n",
1184 (unsigned long long)efx->membase_phys,
1185 efx->type->mem_map_size, efx->membase);
1190 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
1192 efx->membase_phys = 0;
1194 pci_disable_device(efx->pci_dev);
1199 static void efx_fini_io(struct efx_nic *efx)
1201 netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
1204 iounmap(efx->membase);
1205 efx->membase = NULL;
1208 if (efx->membase_phys) {
1209 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
1210 efx->membase_phys = 0;
1213 pci_disable_device(efx->pci_dev);
1216 static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
1218 cpumask_var_t thread_mask;
1225 if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
1226 netif_warn(efx, probe, efx->net_dev,
1227 "RSS disabled due to allocation failure\n");
1232 for_each_online_cpu(cpu) {
1233 if (!cpumask_test_cpu(cpu, thread_mask)) {
1235 cpumask_or(thread_mask, thread_mask,
1236 topology_thread_cpumask(cpu));
1240 free_cpumask_var(thread_mask);
1243 /* If RSS is requested for the PF *and* VFs then we can't write RSS
1244 * table entries that are inaccessible to VFs
1246 if (efx_sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
1247 count > efx_vf_size(efx)) {
1248 netif_warn(efx, probe, efx->net_dev,
1249 "Reducing number of RSS channels from %u to %u for "
1250 "VF support. Increase vf-msix-limit to use more "
1251 "channels on the PF.\n",
1252 count, efx_vf_size(efx));
1253 count = efx_vf_size(efx);
1260 efx_init_rx_cpu_rmap(struct efx_nic *efx, struct msix_entry *xentries)
1262 #ifdef CONFIG_RFS_ACCEL
1266 efx->net_dev->rx_cpu_rmap = alloc_irq_cpu_rmap(efx->n_rx_channels);
1267 if (!efx->net_dev->rx_cpu_rmap)
1269 for (i = 0; i < efx->n_rx_channels; i++) {
1270 rc = irq_cpu_rmap_add(efx->net_dev->rx_cpu_rmap,
1271 xentries[i].vector);
1273 free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
1274 efx->net_dev->rx_cpu_rmap = NULL;
1282 /* Probe the number and type of interrupts we are able to obtain, and
1283 * the resulting numbers of channels and RX queues.
1285 static int efx_probe_interrupts(struct efx_nic *efx)
1287 unsigned int max_channels =
1288 min(efx->type->phys_addr_channels, EFX_MAX_CHANNELS);
1289 unsigned int extra_channels = 0;
1293 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
1294 if (efx->extra_channel_type[i])
1297 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
1298 struct msix_entry xentries[EFX_MAX_CHANNELS];
1299 unsigned int n_channels;
1301 n_channels = efx_wanted_parallelism(efx);
1302 if (separate_tx_channels)
1304 n_channels += extra_channels;
1305 n_channels = min(n_channels, max_channels);
1307 for (i = 0; i < n_channels; i++)
1308 xentries[i].entry = i;
1309 rc = pci_enable_msix(efx->pci_dev, xentries, n_channels);
1311 netif_err(efx, drv, efx->net_dev,
1312 "WARNING: Insufficient MSI-X vectors"
1313 " available (%d < %u).\n", rc, n_channels);
1314 netif_err(efx, drv, efx->net_dev,
1315 "WARNING: Performance may be reduced.\n");
1316 EFX_BUG_ON_PARANOID(rc >= n_channels);
1318 rc = pci_enable_msix(efx->pci_dev, xentries,
1323 efx->n_channels = n_channels;
1324 if (n_channels > extra_channels)
1325 n_channels -= extra_channels;
1326 if (separate_tx_channels) {
1327 efx->n_tx_channels = max(n_channels / 2, 1U);
1328 efx->n_rx_channels = max(n_channels -
1332 efx->n_tx_channels = n_channels;
1333 efx->n_rx_channels = n_channels;
1335 rc = efx_init_rx_cpu_rmap(efx, xentries);
1337 pci_disable_msix(efx->pci_dev);
1340 for (i = 0; i < efx->n_channels; i++)
1341 efx_get_channel(efx, i)->irq =
1344 /* Fall back to single channel MSI */
1345 efx->interrupt_mode = EFX_INT_MODE_MSI;
1346 netif_err(efx, drv, efx->net_dev,
1347 "could not enable MSI-X\n");
1351 /* Try single interrupt MSI */
1352 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
1353 efx->n_channels = 1;
1354 efx->n_rx_channels = 1;
1355 efx->n_tx_channels = 1;
1356 rc = pci_enable_msi(efx->pci_dev);
1358 efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
1360 netif_err(efx, drv, efx->net_dev,
1361 "could not enable MSI\n");
1362 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
1366 /* Assume legacy interrupts */
1367 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
1368 efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
1369 efx->n_rx_channels = 1;
1370 efx->n_tx_channels = 1;
1371 efx->legacy_irq = efx->pci_dev->irq;
1374 /* Assign extra channels if possible */
1375 j = efx->n_channels;
1376 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
1377 if (!efx->extra_channel_type[i])
1379 if (efx->interrupt_mode != EFX_INT_MODE_MSIX ||
1380 efx->n_channels <= extra_channels) {
1381 efx->extra_channel_type[i]->handle_no_channel(efx);
1384 efx_get_channel(efx, j)->type =
1385 efx->extra_channel_type[i];
1389 /* RSS might be usable on VFs even if it is disabled on the PF */
1390 efx->rss_spread = ((efx->n_rx_channels > 1 || !efx_sriov_wanted(efx)) ?
1391 efx->n_rx_channels : efx_vf_size(efx));
1396 /* Enable interrupts, then probe and start the event queues */
1397 static void efx_start_interrupts(struct efx_nic *efx, bool may_keep_eventq)
1399 struct efx_channel *channel;
1401 BUG_ON(efx->state == STATE_DISABLED);
1403 if (efx->legacy_irq)
1404 efx->legacy_irq_enabled = true;
1405 efx_nic_enable_interrupts(efx);
1407 efx_for_each_channel(channel, efx) {
1408 if (!channel->type->keep_eventq || !may_keep_eventq)
1409 efx_init_eventq(channel);
1410 efx_start_eventq(channel);
1413 efx_mcdi_mode_event(efx);
1416 static void efx_stop_interrupts(struct efx_nic *efx, bool may_keep_eventq)
1418 struct efx_channel *channel;
1420 if (efx->state == STATE_DISABLED)
1423 efx_mcdi_mode_poll(efx);
1425 efx_nic_disable_interrupts(efx);
1426 if (efx->legacy_irq) {
1427 synchronize_irq(efx->legacy_irq);
1428 efx->legacy_irq_enabled = false;
1431 efx_for_each_channel(channel, efx) {
1433 synchronize_irq(channel->irq);
1435 efx_stop_eventq(channel);
1436 if (!channel->type->keep_eventq || !may_keep_eventq)
1437 efx_fini_eventq(channel);
1441 static void efx_remove_interrupts(struct efx_nic *efx)
1443 struct efx_channel *channel;
1445 /* Remove MSI/MSI-X interrupts */
1446 efx_for_each_channel(channel, efx)
1448 pci_disable_msi(efx->pci_dev);
1449 pci_disable_msix(efx->pci_dev);
1451 /* Remove legacy interrupt */
1452 efx->legacy_irq = 0;
1455 static void efx_set_channels(struct efx_nic *efx)
1457 struct efx_channel *channel;
1458 struct efx_tx_queue *tx_queue;
1460 efx->tx_channel_offset =
1461 separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0;
1463 /* We need to mark which channels really have RX and TX
1464 * queues, and adjust the TX queue numbers if we have separate
1465 * RX-only and TX-only channels.
1467 efx_for_each_channel(channel, efx) {
1468 if (channel->channel < efx->n_rx_channels)
1469 channel->rx_queue.core_index = channel->channel;
1471 channel->rx_queue.core_index = -1;
1473 efx_for_each_channel_tx_queue(tx_queue, channel)
1474 tx_queue->queue -= (efx->tx_channel_offset *
1479 static int efx_probe_nic(struct efx_nic *efx)
1484 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
1486 /* Carry out hardware-type specific initialisation */
1487 rc = efx->type->probe(efx);
1491 /* Determine the number of channels and queues by trying to hook
1492 * in MSI-X interrupts. */
1493 rc = efx_probe_interrupts(efx);
1497 efx->type->dimension_resources(efx);
1499 if (efx->n_channels > 1)
1500 get_random_bytes(&efx->rx_hash_key, sizeof(efx->rx_hash_key));
1501 for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
1502 efx->rx_indir_table[i] =
1503 ethtool_rxfh_indir_default(i, efx->rss_spread);
1505 efx_set_channels(efx);
1506 netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1507 netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
1509 /* Initialise the interrupt moderation settings */
1510 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
1516 efx->type->remove(efx);
1520 static void efx_remove_nic(struct efx_nic *efx)
1522 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
1524 efx_remove_interrupts(efx);
1525 efx->type->remove(efx);
1528 /**************************************************************************
1530 * NIC startup/shutdown
1532 *************************************************************************/
1534 static int efx_probe_all(struct efx_nic *efx)
1538 rc = efx_probe_nic(efx);
1540 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
1544 rc = efx_probe_port(efx);
1546 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
1550 BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
1551 if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
1555 efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
1557 rc = efx_probe_filters(efx);
1559 netif_err(efx, probe, efx->net_dev,
1560 "failed to create filter tables\n");
1564 rc = efx_probe_channels(efx);
1571 efx_remove_filters(efx);
1573 efx_remove_port(efx);
1575 efx_remove_nic(efx);
1580 /* If the interface is supposed to be running but is not, start
1581 * the hardware and software data path, regular activity for the port
1582 * (MAC statistics, link polling, etc.) and schedule the port to be
1583 * reconfigured. Interrupts must already be enabled. This function
1584 * is safe to call multiple times, so long as the NIC is not disabled.
1585 * Requires the RTNL lock.
1587 static void efx_start_all(struct efx_nic *efx)
1589 EFX_ASSERT_RESET_SERIALISED(efx);
1590 BUG_ON(efx->state == STATE_DISABLED);
1592 /* Check that it is appropriate to restart the interface. All
1593 * of these flags are safe to read under just the rtnl lock */
1594 if (efx->port_enabled || !netif_running(efx->net_dev))
1597 efx_start_port(efx);
1598 efx_start_datapath(efx);
1600 /* Start the hardware monitor if there is one */
1601 if (efx->type->monitor != NULL)
1602 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1603 efx_monitor_interval);
1605 /* If link state detection is normally event-driven, we have
1606 * to poll now because we could have missed a change
1608 if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0) {
1609 mutex_lock(&efx->mac_lock);
1610 if (efx->phy_op->poll(efx))
1611 efx_link_status_changed(efx);
1612 mutex_unlock(&efx->mac_lock);
1615 efx->type->start_stats(efx);
1618 /* Flush all delayed work. Should only be called when no more delayed work
1619 * will be scheduled. This doesn't flush pending online resets (efx_reset),
1620 * since we're holding the rtnl_lock at this point. */
1621 static void efx_flush_all(struct efx_nic *efx)
1623 /* Make sure the hardware monitor and event self-test are stopped */
1624 cancel_delayed_work_sync(&efx->monitor_work);
1625 efx_selftest_async_cancel(efx);
1626 /* Stop scheduled port reconfigurations */
1627 cancel_work_sync(&efx->mac_work);
1630 /* Quiesce the hardware and software data path, and regular activity
1631 * for the port without bringing the link down. Safe to call multiple
1632 * times with the NIC in almost any state, but interrupts should be
1633 * enabled. Requires the RTNL lock.
1635 static void efx_stop_all(struct efx_nic *efx)
1637 EFX_ASSERT_RESET_SERIALISED(efx);
1639 /* port_enabled can be read safely under the rtnl lock */
1640 if (!efx->port_enabled)
1643 efx->type->stop_stats(efx);
1646 /* Flush efx_mac_work(), refill_workqueue, monitor_work */
1649 /* Stop the kernel transmit interface. This is only valid if
1650 * the device is stopped or detached; otherwise the watchdog
1651 * may fire immediately.
1653 WARN_ON(netif_running(efx->net_dev) &&
1654 netif_device_present(efx->net_dev));
1655 netif_tx_disable(efx->net_dev);
1657 efx_stop_datapath(efx);
1660 static void efx_remove_all(struct efx_nic *efx)
1662 efx_remove_channels(efx);
1663 efx_remove_filters(efx);
1664 efx_remove_port(efx);
1665 efx_remove_nic(efx);
1668 /**************************************************************************
1670 * Interrupt moderation
1672 **************************************************************************/
1674 static unsigned int irq_mod_ticks(unsigned int usecs, unsigned int quantum_ns)
1678 if (usecs * 1000 < quantum_ns)
1679 return 1; /* never round down to 0 */
1680 return usecs * 1000 / quantum_ns;
1683 /* Set interrupt moderation parameters */
1684 int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
1685 unsigned int rx_usecs, bool rx_adaptive,
1686 bool rx_may_override_tx)
1688 struct efx_channel *channel;
1689 unsigned int irq_mod_max = DIV_ROUND_UP(efx->type->timer_period_max *
1690 efx->timer_quantum_ns,
1692 unsigned int tx_ticks;
1693 unsigned int rx_ticks;
1695 EFX_ASSERT_RESET_SERIALISED(efx);
1697 if (tx_usecs > irq_mod_max || rx_usecs > irq_mod_max)
1700 tx_ticks = irq_mod_ticks(tx_usecs, efx->timer_quantum_ns);
1701 rx_ticks = irq_mod_ticks(rx_usecs, efx->timer_quantum_ns);
1703 if (tx_ticks != rx_ticks && efx->tx_channel_offset == 0 &&
1704 !rx_may_override_tx) {
1705 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
1706 "RX and TX IRQ moderation must be equal\n");
1710 efx->irq_rx_adaptive = rx_adaptive;
1711 efx->irq_rx_moderation = rx_ticks;
1712 efx_for_each_channel(channel, efx) {
1713 if (efx_channel_has_rx_queue(channel))
1714 channel->irq_moderation = rx_ticks;
1715 else if (efx_channel_has_tx_queues(channel))
1716 channel->irq_moderation = tx_ticks;
1722 void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
1723 unsigned int *rx_usecs, bool *rx_adaptive)
1725 /* We must round up when converting ticks to microseconds
1726 * because we round down when converting the other way.
1729 *rx_adaptive = efx->irq_rx_adaptive;
1730 *rx_usecs = DIV_ROUND_UP(efx->irq_rx_moderation *
1731 efx->timer_quantum_ns,
1734 /* If channels are shared between RX and TX, so is IRQ
1735 * moderation. Otherwise, IRQ moderation is the same for all
1736 * TX channels and is not adaptive.
1738 if (efx->tx_channel_offset == 0)
1739 *tx_usecs = *rx_usecs;
1741 *tx_usecs = DIV_ROUND_UP(
1742 efx->channel[efx->tx_channel_offset]->irq_moderation *
1743 efx->timer_quantum_ns,
1747 /**************************************************************************
1751 **************************************************************************/
1753 /* Run periodically off the general workqueue */
1754 static void efx_monitor(struct work_struct *data)
1756 struct efx_nic *efx = container_of(data, struct efx_nic,
1759 netif_vdbg(efx, timer, efx->net_dev,
1760 "hardware monitor executing on CPU %d\n",
1761 raw_smp_processor_id());
1762 BUG_ON(efx->type->monitor == NULL);
1764 /* If the mac_lock is already held then it is likely a port
1765 * reconfiguration is already in place, which will likely do
1766 * most of the work of monitor() anyway. */
1767 if (mutex_trylock(&efx->mac_lock)) {
1768 if (efx->port_enabled)
1769 efx->type->monitor(efx);
1770 mutex_unlock(&efx->mac_lock);
1773 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1774 efx_monitor_interval);
1777 /**************************************************************************
1781 *************************************************************************/
1784 * Context: process, rtnl_lock() held.
1786 static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1788 struct efx_nic *efx = netdev_priv(net_dev);
1789 struct mii_ioctl_data *data = if_mii(ifr);
1791 if (cmd == SIOCSHWTSTAMP)
1792 return efx_ptp_ioctl(efx, ifr, cmd);
1794 /* Convert phy_id from older PRTAD/DEVAD format */
1795 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
1796 (data->phy_id & 0xfc00) == 0x0400)
1797 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
1799 return mdio_mii_ioctl(&efx->mdio, data, cmd);
1802 /**************************************************************************
1806 **************************************************************************/
1808 static void efx_init_napi_channel(struct efx_channel *channel)
1810 struct efx_nic *efx = channel->efx;
1812 channel->napi_dev = efx->net_dev;
1813 netif_napi_add(channel->napi_dev, &channel->napi_str,
1814 efx_poll, napi_weight);
1817 static void efx_init_napi(struct efx_nic *efx)
1819 struct efx_channel *channel;
1821 efx_for_each_channel(channel, efx)
1822 efx_init_napi_channel(channel);
1825 static void efx_fini_napi_channel(struct efx_channel *channel)
1827 if (channel->napi_dev)
1828 netif_napi_del(&channel->napi_str);
1829 channel->napi_dev = NULL;
1832 static void efx_fini_napi(struct efx_nic *efx)
1834 struct efx_channel *channel;
1836 efx_for_each_channel(channel, efx)
1837 efx_fini_napi_channel(channel);
1840 /**************************************************************************
1842 * Kernel netpoll interface
1844 *************************************************************************/
1846 #ifdef CONFIG_NET_POLL_CONTROLLER
1848 /* Although in the common case interrupts will be disabled, this is not
1849 * guaranteed. However, all our work happens inside the NAPI callback,
1850 * so no locking is required.
1852 static void efx_netpoll(struct net_device *net_dev)
1854 struct efx_nic *efx = netdev_priv(net_dev);
1855 struct efx_channel *channel;
1857 efx_for_each_channel(channel, efx)
1858 efx_schedule_channel(channel);
1863 /**************************************************************************
1865 * Kernel net device interface
1867 *************************************************************************/
1869 /* Context: process, rtnl_lock() held. */
1870 static int efx_net_open(struct net_device *net_dev)
1872 struct efx_nic *efx = netdev_priv(net_dev);
1875 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
1876 raw_smp_processor_id());
1878 rc = efx_check_disabled(efx);
1881 if (efx->phy_mode & PHY_MODE_SPECIAL)
1883 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
1886 /* Notify the kernel of the link state polled during driver load,
1887 * before the monitor starts running */
1888 efx_link_status_changed(efx);
1891 efx_selftest_async_start(efx);
1895 /* Context: process, rtnl_lock() held.
1896 * Note that the kernel will ignore our return code; this method
1897 * should really be a void.
1899 static int efx_net_stop(struct net_device *net_dev)
1901 struct efx_nic *efx = netdev_priv(net_dev);
1903 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
1904 raw_smp_processor_id());
1906 /* Stop the device and flush all the channels */
1912 /* Context: process, dev_base_lock or RTNL held, non-blocking. */
1913 static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev,
1914 struct rtnl_link_stats64 *stats)
1916 struct efx_nic *efx = netdev_priv(net_dev);
1917 struct efx_mac_stats *mac_stats = &efx->mac_stats;
1919 spin_lock_bh(&efx->stats_lock);
1921 efx->type->update_stats(efx);
1923 stats->rx_packets = mac_stats->rx_packets;
1924 stats->tx_packets = mac_stats->tx_packets;
1925 stats->rx_bytes = mac_stats->rx_bytes;
1926 stats->tx_bytes = mac_stats->tx_bytes;
1927 stats->rx_dropped = efx->n_rx_nodesc_drop_cnt;
1928 stats->multicast = mac_stats->rx_multicast;
1929 stats->collisions = mac_stats->tx_collision;
1930 stats->rx_length_errors = (mac_stats->rx_gtjumbo +
1931 mac_stats->rx_length_error);
1932 stats->rx_crc_errors = mac_stats->rx_bad;
1933 stats->rx_frame_errors = mac_stats->rx_align_error;
1934 stats->rx_fifo_errors = mac_stats->rx_overflow;
1935 stats->rx_missed_errors = mac_stats->rx_missed;
1936 stats->tx_window_errors = mac_stats->tx_late_collision;
1938 stats->rx_errors = (stats->rx_length_errors +
1939 stats->rx_crc_errors +
1940 stats->rx_frame_errors +
1941 mac_stats->rx_symbol_error);
1942 stats->tx_errors = (stats->tx_window_errors +
1945 spin_unlock_bh(&efx->stats_lock);
1950 /* Context: netif_tx_lock held, BHs disabled. */
1951 static void efx_watchdog(struct net_device *net_dev)
1953 struct efx_nic *efx = netdev_priv(net_dev);
1955 netif_err(efx, tx_err, efx->net_dev,
1956 "TX stuck with port_enabled=%d: resetting channels\n",
1959 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
1963 /* Context: process, rtnl_lock() held. */
1964 static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
1966 struct efx_nic *efx = netdev_priv(net_dev);
1969 rc = efx_check_disabled(efx);
1972 if (new_mtu > EFX_MAX_MTU)
1975 netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
1977 efx_device_detach_sync(efx);
1980 mutex_lock(&efx->mac_lock);
1981 net_dev->mtu = new_mtu;
1982 efx->type->reconfigure_mac(efx);
1983 mutex_unlock(&efx->mac_lock);
1986 netif_device_attach(efx->net_dev);
1990 static int efx_set_mac_address(struct net_device *net_dev, void *data)
1992 struct efx_nic *efx = netdev_priv(net_dev);
1993 struct sockaddr *addr = data;
1994 char *new_addr = addr->sa_data;
1996 if (!is_valid_ether_addr(new_addr)) {
1997 netif_err(efx, drv, efx->net_dev,
1998 "invalid ethernet MAC address requested: %pM\n",
2000 return -EADDRNOTAVAIL;
2003 memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
2004 efx_sriov_mac_address_changed(efx);
2006 /* Reconfigure the MAC */
2007 mutex_lock(&efx->mac_lock);
2008 efx->type->reconfigure_mac(efx);
2009 mutex_unlock(&efx->mac_lock);
2014 /* Context: netif_addr_lock held, BHs disabled. */
2015 static void efx_set_rx_mode(struct net_device *net_dev)
2017 struct efx_nic *efx = netdev_priv(net_dev);
2018 struct netdev_hw_addr *ha;
2019 union efx_multicast_hash *mc_hash = &efx->multicast_hash;
2023 efx->promiscuous = !!(net_dev->flags & IFF_PROMISC);
2025 /* Build multicast hash table */
2026 if (efx->promiscuous || (net_dev->flags & IFF_ALLMULTI)) {
2027 memset(mc_hash, 0xff, sizeof(*mc_hash));
2029 memset(mc_hash, 0x00, sizeof(*mc_hash));
2030 netdev_for_each_mc_addr(ha, net_dev) {
2031 crc = ether_crc_le(ETH_ALEN, ha->addr);
2032 bit = crc & (EFX_MCAST_HASH_ENTRIES - 1);
2033 __set_bit_le(bit, mc_hash);
2036 /* Broadcast packets go through the multicast hash filter.
2037 * ether_crc_le() of the broadcast address is 0xbe2612ff
2038 * so we always add bit 0xff to the mask.
2040 __set_bit_le(0xff, mc_hash);
2043 if (efx->port_enabled)
2044 queue_work(efx->workqueue, &efx->mac_work);
2045 /* Otherwise efx_start_port() will do this */
2048 static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
2050 struct efx_nic *efx = netdev_priv(net_dev);
2052 /* If disabling RX n-tuple filtering, clear existing filters */
2053 if (net_dev->features & ~data & NETIF_F_NTUPLE)
2054 efx_filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
2059 static const struct net_device_ops efx_netdev_ops = {
2060 .ndo_open = efx_net_open,
2061 .ndo_stop = efx_net_stop,
2062 .ndo_get_stats64 = efx_net_stats,
2063 .ndo_tx_timeout = efx_watchdog,
2064 .ndo_start_xmit = efx_hard_start_xmit,
2065 .ndo_validate_addr = eth_validate_addr,
2066 .ndo_do_ioctl = efx_ioctl,
2067 .ndo_change_mtu = efx_change_mtu,
2068 .ndo_set_mac_address = efx_set_mac_address,
2069 .ndo_set_rx_mode = efx_set_rx_mode,
2070 .ndo_set_features = efx_set_features,
2071 #ifdef CONFIG_SFC_SRIOV
2072 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
2073 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
2074 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
2075 .ndo_get_vf_config = efx_sriov_get_vf_config,
2077 #ifdef CONFIG_NET_POLL_CONTROLLER
2078 .ndo_poll_controller = efx_netpoll,
2080 .ndo_setup_tc = efx_setup_tc,
2081 #ifdef CONFIG_RFS_ACCEL
2082 .ndo_rx_flow_steer = efx_filter_rfs,
2086 static void efx_update_name(struct efx_nic *efx)
2088 strcpy(efx->name, efx->net_dev->name);
2089 efx_mtd_rename(efx);
2090 efx_set_channel_names(efx);
2093 static int efx_netdev_event(struct notifier_block *this,
2094 unsigned long event, void *ptr)
2096 struct net_device *net_dev = ptr;
2098 if (net_dev->netdev_ops == &efx_netdev_ops &&
2099 event == NETDEV_CHANGENAME)
2100 efx_update_name(netdev_priv(net_dev));
2105 static struct notifier_block efx_netdev_notifier = {
2106 .notifier_call = efx_netdev_event,
2110 show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
2112 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2113 return sprintf(buf, "%d\n", efx->phy_type);
2115 static DEVICE_ATTR(phy_type, 0644, show_phy_type, NULL);
2117 static int efx_register_netdev(struct efx_nic *efx)
2119 struct net_device *net_dev = efx->net_dev;
2120 struct efx_channel *channel;
2123 net_dev->watchdog_timeo = 5 * HZ;
2124 net_dev->irq = efx->pci_dev->irq;
2125 net_dev->netdev_ops = &efx_netdev_ops;
2126 SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
2127 net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
2131 /* Enable resets to be scheduled and check whether any were
2132 * already requested. If so, the NIC is probably hosed so we
2135 efx->state = STATE_READY;
2136 smp_mb(); /* ensure we change state before checking reset_pending */
2137 if (efx->reset_pending) {
2138 netif_err(efx, probe, efx->net_dev,
2139 "aborting probe due to scheduled reset\n");
2144 rc = dev_alloc_name(net_dev, net_dev->name);
2147 efx_update_name(efx);
2149 /* Always start with carrier off; PHY events will detect the link */
2150 netif_carrier_off(net_dev);
2152 rc = register_netdevice(net_dev);
2156 efx_for_each_channel(channel, efx) {
2157 struct efx_tx_queue *tx_queue;
2158 efx_for_each_channel_tx_queue(tx_queue, channel)
2159 efx_init_tx_queue_core_txq(tx_queue);
2164 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2166 netif_err(efx, drv, efx->net_dev,
2167 "failed to init net dev attributes\n");
2168 goto fail_registered;
2175 unregister_netdevice(net_dev);
2177 efx->state = STATE_UNINIT;
2179 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
2183 static void efx_unregister_netdev(struct efx_nic *efx)
2185 struct efx_channel *channel;
2186 struct efx_tx_queue *tx_queue;
2191 BUG_ON(netdev_priv(efx->net_dev) != efx);
2193 /* Free up any skbs still remaining. This has to happen before
2194 * we try to unregister the netdev as running their destructors
2195 * may be needed to get the device ref. count to 0. */
2196 efx_for_each_channel(channel, efx) {
2197 efx_for_each_channel_tx_queue(tx_queue, channel)
2198 efx_release_tx_buffers(tx_queue);
2201 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2202 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2205 unregister_netdevice(efx->net_dev);
2206 efx->state = STATE_UNINIT;
2210 /**************************************************************************
2212 * Device reset and suspend
2214 **************************************************************************/
2216 /* Tears down the entire software state and most of the hardware state
2218 void efx_reset_down(struct efx_nic *efx, enum reset_type method)
2220 EFX_ASSERT_RESET_SERIALISED(efx);
2223 efx_stop_interrupts(efx, false);
2225 mutex_lock(&efx->mac_lock);
2226 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
2227 efx->phy_op->fini(efx);
2228 efx->type->fini(efx);
2231 /* This function will always ensure that the locks acquired in
2232 * efx_reset_down() are released. A failure return code indicates
2233 * that we were unable to reinitialise the hardware, and the
2234 * driver should be disabled. If ok is false, then the rx and tx
2235 * engines are not restarted, pending a RESET_DISABLE. */
2236 int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
2240 EFX_ASSERT_RESET_SERIALISED(efx);
2242 rc = efx->type->init(efx);
2244 netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
2251 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
2252 rc = efx->phy_op->init(efx);
2255 if (efx->phy_op->reconfigure(efx))
2256 netif_err(efx, drv, efx->net_dev,
2257 "could not restore PHY settings\n");
2260 efx->type->reconfigure_mac(efx);
2262 efx_start_interrupts(efx, false);
2263 efx_restore_filters(efx);
2264 efx_sriov_reset(efx);
2266 mutex_unlock(&efx->mac_lock);
2273 efx->port_initialized = false;
2275 mutex_unlock(&efx->mac_lock);
2280 /* Reset the NIC using the specified method. Note that the reset may
2281 * fail, in which case the card will be left in an unusable state.
2283 * Caller must hold the rtnl_lock.
2285 int efx_reset(struct efx_nic *efx, enum reset_type method)
2290 netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2291 RESET_TYPE(method));
2293 efx_device_detach_sync(efx);
2294 efx_reset_down(efx, method);
2296 rc = efx->type->reset(efx, method);
2298 netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
2302 /* Clear flags for the scopes we covered. We assume the NIC and
2303 * driver are now quiescent so that there is no race here.
2305 efx->reset_pending &= -(1 << (method + 1));
2307 /* Reinitialise bus-mastering, which may have been turned off before
2308 * the reset was scheduled. This is still appropriate, even in the
2309 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2310 * can respond to requests. */
2311 pci_set_master(efx->pci_dev);
2314 /* Leave device stopped if necessary */
2316 method == RESET_TYPE_DISABLE ||
2317 method == RESET_TYPE_RECOVER_OR_DISABLE;
2318 rc2 = efx_reset_up(efx, method, !disabled);
2326 dev_close(efx->net_dev);
2327 netif_err(efx, drv, efx->net_dev, "has been disabled\n");
2328 efx->state = STATE_DISABLED;
2330 netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
2331 netif_device_attach(efx->net_dev);
2336 /* Try recovery mechanisms.
2337 * For now only EEH is supported.
2338 * Returns 0 if the recovery mechanisms are unsuccessful.
2339 * Returns a non-zero value otherwise.
2341 static int efx_try_recovery(struct efx_nic *efx)
2344 /* A PCI error can occur and not be seen by EEH because nothing
2345 * happens on the PCI bus. In this case the driver may fail and
2346 * schedule a 'recover or reset', leading to this recovery handler.
2347 * Manually call the eeh failure check function.
2349 struct eeh_dev *eehdev =
2350 of_node_to_eeh_dev(pci_device_to_OF_node(efx->pci_dev));
2352 if (eeh_dev_check_failure(eehdev)) {
2353 /* The EEH mechanisms will handle the error and reset the
2354 * device if necessary.
2362 /* The worker thread exists so that code that cannot sleep can
2363 * schedule a reset for later.
2365 static void efx_reset_work(struct work_struct *data)
2367 struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
2368 unsigned long pending;
2369 enum reset_type method;
2371 pending = ACCESS_ONCE(efx->reset_pending);
2372 method = fls(pending) - 1;
2374 if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
2375 method == RESET_TYPE_RECOVER_OR_ALL) &&
2376 efx_try_recovery(efx))
2384 /* We checked the state in efx_schedule_reset() but it may
2385 * have changed by now. Now that we have the RTNL lock,
2386 * it cannot change again.
2388 if (efx->state == STATE_READY)
2389 (void)efx_reset(efx, method);
2394 void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2396 enum reset_type method;
2398 if (efx->state == STATE_RECOVERY) {
2399 netif_dbg(efx, drv, efx->net_dev,
2400 "recovering: skip scheduling %s reset\n",
2406 case RESET_TYPE_INVISIBLE:
2407 case RESET_TYPE_ALL:
2408 case RESET_TYPE_RECOVER_OR_ALL:
2409 case RESET_TYPE_WORLD:
2410 case RESET_TYPE_DISABLE:
2411 case RESET_TYPE_RECOVER_OR_DISABLE:
2413 netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2414 RESET_TYPE(method));
2417 method = efx->type->map_reset_reason(type);
2418 netif_dbg(efx, drv, efx->net_dev,
2419 "scheduling %s reset for %s\n",
2420 RESET_TYPE(method), RESET_TYPE(type));
2424 set_bit(method, &efx->reset_pending);
2425 smp_mb(); /* ensure we change reset_pending before checking state */
2427 /* If we're not READY then just leave the flags set as the cue
2428 * to abort probing or reschedule the reset later.
2430 if (ACCESS_ONCE(efx->state) != STATE_READY)
2433 /* efx_process_channel() will no longer read events once a
2434 * reset is scheduled. So switch back to poll'd MCDI completions. */
2435 efx_mcdi_mode_poll(efx);
2437 queue_work(reset_workqueue, &efx->reset_work);
2440 /**************************************************************************
2442 * List of NICs we support
2444 **************************************************************************/
2446 /* PCI device ID table */
2447 static DEFINE_PCI_DEVICE_TABLE(efx_pci_table) = {
2448 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2449 PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
2450 .driver_data = (unsigned long) &falcon_a1_nic_type},
2451 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2452 PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
2453 .driver_data = (unsigned long) &falcon_b0_nic_type},
2454 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
2455 .driver_data = (unsigned long) &siena_a0_nic_type},
2456 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
2457 .driver_data = (unsigned long) &siena_a0_nic_type},
2458 {0} /* end of list */
2461 /**************************************************************************
2463 * Dummy PHY/MAC operations
2465 * Can be used for some unimplemented operations
2466 * Needed so all function pointers are valid and do not have to be tested
2469 **************************************************************************/
2470 int efx_port_dummy_op_int(struct efx_nic *efx)
2474 void efx_port_dummy_op_void(struct efx_nic *efx) {}
2476 static bool efx_port_dummy_op_poll(struct efx_nic *efx)
2481 static const struct efx_phy_operations efx_dummy_phy_operations = {
2482 .init = efx_port_dummy_op_int,
2483 .reconfigure = efx_port_dummy_op_int,
2484 .poll = efx_port_dummy_op_poll,
2485 .fini = efx_port_dummy_op_void,
2488 /**************************************************************************
2492 **************************************************************************/
2494 /* This zeroes out and then fills in the invariants in a struct
2495 * efx_nic (including all sub-structures).
2497 static int efx_init_struct(struct efx_nic *efx,
2498 struct pci_dev *pci_dev, struct net_device *net_dev)
2502 /* Initialise common structures */
2503 spin_lock_init(&efx->biu_lock);
2504 #ifdef CONFIG_SFC_MTD
2505 INIT_LIST_HEAD(&efx->mtd_list);
2507 INIT_WORK(&efx->reset_work, efx_reset_work);
2508 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
2509 INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
2510 efx->pci_dev = pci_dev;
2511 efx->msg_enable = debug;
2512 efx->state = STATE_UNINIT;
2513 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
2515 efx->net_dev = net_dev;
2516 spin_lock_init(&efx->stats_lock);
2517 mutex_init(&efx->mac_lock);
2518 efx->phy_op = &efx_dummy_phy_operations;
2519 efx->mdio.dev = net_dev;
2520 INIT_WORK(&efx->mac_work, efx_mac_work);
2521 init_waitqueue_head(&efx->flush_wq);
2523 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
2524 efx->channel[i] = efx_alloc_channel(efx, i, NULL);
2525 if (!efx->channel[i])
2529 EFX_BUG_ON_PARANOID(efx->type->phys_addr_channels > EFX_MAX_CHANNELS);
2531 /* Higher numbered interrupt modes are less capable! */
2532 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2535 /* Would be good to use the net_dev name, but we're too early */
2536 snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2538 efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
2539 if (!efx->workqueue)
2545 efx_fini_struct(efx);
2549 static void efx_fini_struct(struct efx_nic *efx)
2553 for (i = 0; i < EFX_MAX_CHANNELS; i++)
2554 kfree(efx->channel[i]);
2556 if (efx->workqueue) {
2557 destroy_workqueue(efx->workqueue);
2558 efx->workqueue = NULL;
2562 /**************************************************************************
2566 **************************************************************************/
2568 /* Main body of final NIC shutdown code
2569 * This is called only at module unload (or hotplug removal).
2571 static void efx_pci_remove_main(struct efx_nic *efx)
2573 /* Flush reset_work. It can no longer be scheduled since we
2576 BUG_ON(efx->state == STATE_READY);
2577 cancel_work_sync(&efx->reset_work);
2579 #ifdef CONFIG_RFS_ACCEL
2580 free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
2581 efx->net_dev->rx_cpu_rmap = NULL;
2583 efx_stop_interrupts(efx, false);
2584 efx_nic_fini_interrupt(efx);
2586 efx->type->fini(efx);
2588 efx_remove_all(efx);
2591 /* Final NIC shutdown
2592 * This is called only at module unload (or hotplug removal).
2594 static void efx_pci_remove(struct pci_dev *pci_dev)
2596 struct efx_nic *efx;
2598 efx = pci_get_drvdata(pci_dev);
2602 /* Mark the NIC as fini, then stop the interface */
2604 dev_close(efx->net_dev);
2605 efx_stop_interrupts(efx, false);
2608 efx_sriov_fini(efx);
2609 efx_unregister_netdev(efx);
2611 efx_mtd_remove(efx);
2613 efx_pci_remove_main(efx);
2616 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
2618 efx_fini_struct(efx);
2619 pci_set_drvdata(pci_dev, NULL);
2620 free_netdev(efx->net_dev);
2622 pci_disable_pcie_error_reporting(pci_dev);
2625 /* NIC VPD information
2626 * Called during probe to display the part number of the
2627 * installed NIC. VPD is potentially very large but this should
2628 * always appear within the first 512 bytes.
2630 #define SFC_VPD_LEN 512
2631 static void efx_print_product_vpd(struct efx_nic *efx)
2633 struct pci_dev *dev = efx->pci_dev;
2634 char vpd_data[SFC_VPD_LEN];
2638 /* Get the vpd data from the device */
2639 vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
2640 if (vpd_size <= 0) {
2641 netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
2645 /* Get the Read only section */
2646 i = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
2648 netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
2652 j = pci_vpd_lrdt_size(&vpd_data[i]);
2653 i += PCI_VPD_LRDT_TAG_SIZE;
2654 if (i + j > vpd_size)
2657 /* Get the Part number */
2658 i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
2660 netif_err(efx, drv, efx->net_dev, "Part number not found\n");
2664 j = pci_vpd_info_field_size(&vpd_data[i]);
2665 i += PCI_VPD_INFO_FLD_HDR_SIZE;
2666 if (i + j > vpd_size) {
2667 netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
2671 netif_info(efx, drv, efx->net_dev,
2672 "Part Number : %.*s\n", j, &vpd_data[i]);
2676 /* Main body of NIC initialisation
2677 * This is called at module load (or hotplug insertion, theoretically).
2679 static int efx_pci_probe_main(struct efx_nic *efx)
2683 /* Do start-of-day initialisation */
2684 rc = efx_probe_all(efx);
2690 rc = efx->type->init(efx);
2692 netif_err(efx, probe, efx->net_dev,
2693 "failed to initialise NIC\n");
2697 rc = efx_init_port(efx);
2699 netif_err(efx, probe, efx->net_dev,
2700 "failed to initialise port\n");
2704 rc = efx_nic_init_interrupt(efx);
2707 efx_start_interrupts(efx, false);
2714 efx->type->fini(efx);
2717 efx_remove_all(efx);
2722 /* NIC initialisation
2724 * This is called at module load (or hotplug insertion,
2725 * theoretically). It sets up PCI mappings, resets the NIC,
2726 * sets up and registers the network devices with the kernel and hooks
2727 * the interrupt service routine. It does not prepare the device for
2728 * transmission; this is left to the first time one of the network
2729 * interfaces is brought up (i.e. efx_net_open).
2731 static int efx_pci_probe(struct pci_dev *pci_dev,
2732 const struct pci_device_id *entry)
2734 struct net_device *net_dev;
2735 struct efx_nic *efx;
2738 /* Allocate and initialise a struct net_device and struct efx_nic */
2739 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
2743 efx = netdev_priv(net_dev);
2744 efx->type = (const struct efx_nic_type *) entry->driver_data;
2745 net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
2746 NETIF_F_HIGHDMA | NETIF_F_TSO |
2748 if (efx->type->offload_features & NETIF_F_V6_CSUM)
2749 net_dev->features |= NETIF_F_TSO6;
2750 /* Mask for features that also apply to VLAN devices */
2751 net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
2752 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
2754 /* All offloads can be toggled */
2755 net_dev->hw_features = net_dev->features & ~NETIF_F_HIGHDMA;
2756 pci_set_drvdata(pci_dev, efx);
2757 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
2758 rc = efx_init_struct(efx, pci_dev, net_dev);
2762 netif_info(efx, probe, efx->net_dev,
2763 "Solarflare NIC detected\n");
2765 efx_print_product_vpd(efx);
2767 /* Set up basic I/O (BAR mappings etc) */
2768 rc = efx_init_io(efx);
2772 rc = efx_pci_probe_main(efx);
2776 rc = efx_register_netdev(efx);
2780 rc = efx_sriov_init(efx);
2782 netif_err(efx, probe, efx->net_dev,
2783 "SR-IOV can't be enabled rc %d\n", rc);
2785 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
2787 /* Try to create MTDs, but allow this to fail */
2789 rc = efx_mtd_probe(efx);
2792 netif_warn(efx, probe, efx->net_dev,
2793 "failed to create MTDs (%d)\n", rc);
2795 rc = pci_enable_pcie_error_reporting(pci_dev);
2796 if (rc && rc != -EINVAL)
2797 netif_warn(efx, probe, efx->net_dev,
2798 "pci_enable_pcie_error_reporting failed (%d)\n", rc);
2803 efx_pci_remove_main(efx);
2807 efx_fini_struct(efx);
2809 pci_set_drvdata(pci_dev, NULL);
2811 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
2812 free_netdev(net_dev);
2816 static int efx_pm_freeze(struct device *dev)
2818 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2822 if (efx->state != STATE_DISABLED) {
2823 efx->state = STATE_UNINIT;
2825 efx_device_detach_sync(efx);
2828 efx_stop_interrupts(efx, false);
2836 static int efx_pm_thaw(struct device *dev)
2838 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2842 if (efx->state != STATE_DISABLED) {
2843 efx_start_interrupts(efx, false);
2845 mutex_lock(&efx->mac_lock);
2846 efx->phy_op->reconfigure(efx);
2847 mutex_unlock(&efx->mac_lock);
2851 netif_device_attach(efx->net_dev);
2853 efx->state = STATE_READY;
2855 efx->type->resume_wol(efx);
2860 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
2861 queue_work(reset_workqueue, &efx->reset_work);
2866 static int efx_pm_poweroff(struct device *dev)
2868 struct pci_dev *pci_dev = to_pci_dev(dev);
2869 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2871 efx->type->fini(efx);
2873 efx->reset_pending = 0;
2875 pci_save_state(pci_dev);
2876 return pci_set_power_state(pci_dev, PCI_D3hot);
2879 /* Used for both resume and restore */
2880 static int efx_pm_resume(struct device *dev)
2882 struct pci_dev *pci_dev = to_pci_dev(dev);
2883 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2886 rc = pci_set_power_state(pci_dev, PCI_D0);
2889 pci_restore_state(pci_dev);
2890 rc = pci_enable_device(pci_dev);
2893 pci_set_master(efx->pci_dev);
2894 rc = efx->type->reset(efx, RESET_TYPE_ALL);
2897 rc = efx->type->init(efx);
2904 static int efx_pm_suspend(struct device *dev)
2909 rc = efx_pm_poweroff(dev);
2915 static const struct dev_pm_ops efx_pm_ops = {
2916 .suspend = efx_pm_suspend,
2917 .resume = efx_pm_resume,
2918 .freeze = efx_pm_freeze,
2919 .thaw = efx_pm_thaw,
2920 .poweroff = efx_pm_poweroff,
2921 .restore = efx_pm_resume,
2924 /* A PCI error affecting this device was detected.
2925 * At this point MMIO and DMA may be disabled.
2926 * Stop the software path and request a slot reset.
2928 pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
2929 enum pci_channel_state state)
2931 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
2932 struct efx_nic *efx = pci_get_drvdata(pdev);
2934 if (state == pci_channel_io_perm_failure)
2935 return PCI_ERS_RESULT_DISCONNECT;
2939 if (efx->state != STATE_DISABLED) {
2940 efx->state = STATE_RECOVERY;
2941 efx->reset_pending = 0;
2943 efx_device_detach_sync(efx);
2946 efx_stop_interrupts(efx, false);
2948 status = PCI_ERS_RESULT_NEED_RESET;
2950 /* If the interface is disabled we don't want to do anything
2953 status = PCI_ERS_RESULT_RECOVERED;
2958 pci_disable_device(pdev);
2963 /* Fake a successfull reset, which will be performed later in efx_io_resume. */
2964 pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
2966 struct efx_nic *efx = pci_get_drvdata(pdev);
2967 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
2970 if (pci_enable_device(pdev)) {
2971 netif_err(efx, hw, efx->net_dev,
2972 "Cannot re-enable PCI device after reset.\n");
2973 status = PCI_ERS_RESULT_DISCONNECT;
2976 rc = pci_cleanup_aer_uncorrect_error_status(pdev);
2978 netif_err(efx, hw, efx->net_dev,
2979 "pci_cleanup_aer_uncorrect_error_status failed (%d)\n", rc);
2980 /* Non-fatal error. Continue. */
2986 /* Perform the actual reset and resume I/O operations. */
2987 static void efx_io_resume(struct pci_dev *pdev)
2989 struct efx_nic *efx = pci_get_drvdata(pdev);
2994 if (efx->state == STATE_DISABLED)
2997 rc = efx_reset(efx, RESET_TYPE_ALL);
2999 netif_err(efx, hw, efx->net_dev,
3000 "efx_reset failed after PCI error (%d)\n", rc);
3002 efx->state = STATE_READY;
3003 netif_dbg(efx, hw, efx->net_dev,
3004 "Done resetting and resuming IO after PCI error.\n");
3011 /* For simplicity and reliability, we always require a slot reset and try to
3012 * reset the hardware when a pci error affecting the device is detected.
3013 * We leave both the link_reset and mmio_enabled callback unimplemented:
3014 * with our request for slot reset the mmio_enabled callback will never be
3015 * called, and the link_reset callback is not used by AER or EEH mechanisms.
3017 static struct pci_error_handlers efx_err_handlers = {
3018 .error_detected = efx_io_error_detected,
3019 .slot_reset = efx_io_slot_reset,
3020 .resume = efx_io_resume,
3023 static struct pci_driver efx_pci_driver = {
3024 .name = KBUILD_MODNAME,
3025 .id_table = efx_pci_table,
3026 .probe = efx_pci_probe,
3027 .remove = efx_pci_remove,
3028 .driver.pm = &efx_pm_ops,
3029 .err_handler = &efx_err_handlers,
3032 /**************************************************************************
3034 * Kernel module interface
3036 *************************************************************************/
3038 module_param(interrupt_mode, uint, 0444);
3039 MODULE_PARM_DESC(interrupt_mode,
3040 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
3042 static int __init efx_init_module(void)
3046 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
3048 rc = register_netdevice_notifier(&efx_netdev_notifier);
3052 rc = efx_init_sriov();
3056 reset_workqueue = create_singlethread_workqueue("sfc_reset");
3057 if (!reset_workqueue) {
3062 rc = pci_register_driver(&efx_pci_driver);
3069 destroy_workqueue(reset_workqueue);
3073 unregister_netdevice_notifier(&efx_netdev_notifier);
3078 static void __exit efx_exit_module(void)
3080 printk(KERN_INFO "Solarflare NET driver unloading\n");
3082 pci_unregister_driver(&efx_pci_driver);
3083 destroy_workqueue(reset_workqueue);
3085 unregister_netdevice_notifier(&efx_netdev_notifier);
3089 module_init(efx_init_module);
3090 module_exit(efx_exit_module);
3092 MODULE_AUTHOR("Solarflare Communications and "
3093 "Michael Brown <mbrown@fensystems.co.uk>");
3094 MODULE_DESCRIPTION("Solarflare Communications network driver");
3095 MODULE_LICENSE("GPL");
3096 MODULE_DEVICE_TABLE(pci, efx_pci_table);