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/cpu_rmap.h>
25 #include "net_driver.h"
31 #include "workarounds.h"
33 /**************************************************************************
37 **************************************************************************
40 /* Loopback mode names (see LOOPBACK_MODE()) */
41 const unsigned int efx_loopback_mode_max
= LOOPBACK_MAX
;
42 const char *const efx_loopback_mode_names
[] = {
43 [LOOPBACK_NONE
] = "NONE",
44 [LOOPBACK_DATA
] = "DATAPATH",
45 [LOOPBACK_GMAC
] = "GMAC",
46 [LOOPBACK_XGMII
] = "XGMII",
47 [LOOPBACK_XGXS
] = "XGXS",
48 [LOOPBACK_XAUI
] = "XAUI",
49 [LOOPBACK_GMII
] = "GMII",
50 [LOOPBACK_SGMII
] = "SGMII",
51 [LOOPBACK_XGBR
] = "XGBR",
52 [LOOPBACK_XFI
] = "XFI",
53 [LOOPBACK_XAUI_FAR
] = "XAUI_FAR",
54 [LOOPBACK_GMII_FAR
] = "GMII_FAR",
55 [LOOPBACK_SGMII_FAR
] = "SGMII_FAR",
56 [LOOPBACK_XFI_FAR
] = "XFI_FAR",
57 [LOOPBACK_GPHY
] = "GPHY",
58 [LOOPBACK_PHYXS
] = "PHYXS",
59 [LOOPBACK_PCS
] = "PCS",
60 [LOOPBACK_PMAPMD
] = "PMA/PMD",
61 [LOOPBACK_XPORT
] = "XPORT",
62 [LOOPBACK_XGMII_WS
] = "XGMII_WS",
63 [LOOPBACK_XAUI_WS
] = "XAUI_WS",
64 [LOOPBACK_XAUI_WS_FAR
] = "XAUI_WS_FAR",
65 [LOOPBACK_XAUI_WS_NEAR
] = "XAUI_WS_NEAR",
66 [LOOPBACK_GMII_WS
] = "GMII_WS",
67 [LOOPBACK_XFI_WS
] = "XFI_WS",
68 [LOOPBACK_XFI_WS_FAR
] = "XFI_WS_FAR",
69 [LOOPBACK_PHYXS_WS
] = "PHYXS_WS",
72 const unsigned int efx_reset_type_max
= RESET_TYPE_MAX
;
73 const char *const efx_reset_type_names
[] = {
74 [RESET_TYPE_INVISIBLE
] = "INVISIBLE",
75 [RESET_TYPE_ALL
] = "ALL",
76 [RESET_TYPE_WORLD
] = "WORLD",
77 [RESET_TYPE_DISABLE
] = "DISABLE",
78 [RESET_TYPE_TX_WATCHDOG
] = "TX_WATCHDOG",
79 [RESET_TYPE_INT_ERROR
] = "INT_ERROR",
80 [RESET_TYPE_RX_RECOVERY
] = "RX_RECOVERY",
81 [RESET_TYPE_RX_DESC_FETCH
] = "RX_DESC_FETCH",
82 [RESET_TYPE_TX_DESC_FETCH
] = "TX_DESC_FETCH",
83 [RESET_TYPE_TX_SKIP
] = "TX_SKIP",
84 [RESET_TYPE_MC_FAILURE
] = "MC_FAILURE",
87 #define EFX_MAX_MTU (9 * 1024)
89 /* Reset workqueue. If any NIC has a hardware failure then a reset will be
90 * queued onto this work queue. This is not a per-nic work queue, because
91 * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
93 static struct workqueue_struct
*reset_workqueue
;
95 /**************************************************************************
99 *************************************************************************/
102 * Use separate channels for TX and RX events
104 * Set this to 1 to use separate channels for TX and RX. It allows us
105 * to control interrupt affinity separately for TX and RX.
107 * This is only used in MSI-X interrupt mode
109 static unsigned int separate_tx_channels
;
110 module_param(separate_tx_channels
, uint
, 0444);
111 MODULE_PARM_DESC(separate_tx_channels
,
112 "Use separate channels for TX and RX");
114 /* This is the weight assigned to each of the (per-channel) virtual
117 static int napi_weight
= 64;
119 /* This is the time (in jiffies) between invocations of the hardware
120 * monitor. On Falcon-based NICs, this will:
121 * - Check the on-board hardware monitor;
122 * - Poll the link state and reconfigure the hardware as necessary.
124 static unsigned int efx_monitor_interval
= 1 * HZ
;
126 /* Initial interrupt moderation settings. They can be modified after
127 * module load with ethtool.
129 * The default for RX should strike a balance between increasing the
130 * round-trip latency and reducing overhead.
132 static unsigned int rx_irq_mod_usec
= 60;
134 /* Initial interrupt moderation settings. They can be modified after
135 * module load with ethtool.
137 * This default is chosen to ensure that a 10G link does not go idle
138 * while a TX queue is stopped after it has become full. A queue is
139 * restarted when it drops below half full. The time this takes (assuming
140 * worst case 3 descriptors per packet and 1024 descriptors) is
141 * 512 / 3 * 1.2 = 205 usec.
143 static unsigned int tx_irq_mod_usec
= 150;
145 /* This is the first interrupt mode to try out of:
150 static unsigned int interrupt_mode
;
152 /* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
153 * i.e. the number of CPUs among which we may distribute simultaneous
154 * interrupt handling.
156 * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
157 * The default (0) means to assign an interrupt to each core.
159 static unsigned int rss_cpus
;
160 module_param(rss_cpus
, uint
, 0444);
161 MODULE_PARM_DESC(rss_cpus
, "Number of CPUs to use for Receive-Side Scaling");
163 static int phy_flash_cfg
;
164 module_param(phy_flash_cfg
, int, 0644);
165 MODULE_PARM_DESC(phy_flash_cfg
, "Set PHYs into reflash mode initially");
167 static unsigned irq_adapt_low_thresh
= 8000;
168 module_param(irq_adapt_low_thresh
, uint
, 0644);
169 MODULE_PARM_DESC(irq_adapt_low_thresh
,
170 "Threshold score for reducing IRQ moderation");
172 static unsigned irq_adapt_high_thresh
= 16000;
173 module_param(irq_adapt_high_thresh
, uint
, 0644);
174 MODULE_PARM_DESC(irq_adapt_high_thresh
,
175 "Threshold score for increasing IRQ moderation");
177 static unsigned debug
= (NETIF_MSG_DRV
| NETIF_MSG_PROBE
|
178 NETIF_MSG_LINK
| NETIF_MSG_IFDOWN
|
179 NETIF_MSG_IFUP
| NETIF_MSG_RX_ERR
|
180 NETIF_MSG_TX_ERR
| NETIF_MSG_HW
);
181 module_param(debug
, uint
, 0);
182 MODULE_PARM_DESC(debug
, "Bitmapped debugging message enable value");
184 /**************************************************************************
186 * Utility functions and prototypes
188 *************************************************************************/
190 static void efx_start_interrupts(struct efx_nic
*efx
, bool may_keep_eventq
);
191 static void efx_stop_interrupts(struct efx_nic
*efx
, bool may_keep_eventq
);
192 static void efx_remove_channel(struct efx_channel
*channel
);
193 static void efx_remove_channels(struct efx_nic
*efx
);
194 static const struct efx_channel_type efx_default_channel_type
;
195 static void efx_remove_port(struct efx_nic
*efx
);
196 static void efx_init_napi_channel(struct efx_channel
*channel
);
197 static void efx_fini_napi(struct efx_nic
*efx
);
198 static void efx_fini_napi_channel(struct efx_channel
*channel
);
199 static void efx_fini_struct(struct efx_nic
*efx
);
200 static void efx_start_all(struct efx_nic
*efx
);
201 static void efx_stop_all(struct efx_nic
*efx
);
203 #define EFX_ASSERT_RESET_SERIALISED(efx) \
205 if ((efx->state == STATE_READY) || \
206 (efx->state == STATE_DISABLED)) \
210 static int efx_check_disabled(struct efx_nic
*efx
)
212 if (efx
->state
== STATE_DISABLED
) {
213 netif_err(efx
, drv
, efx
->net_dev
,
214 "device is disabled due to earlier errors\n");
220 /**************************************************************************
222 * Event queue processing
224 *************************************************************************/
226 /* Process channel's event queue
228 * This function is responsible for processing the event queue of a
229 * single channel. The caller must guarantee that this function will
230 * never be concurrently called more than once on the same channel,
231 * though different channels may be being processed concurrently.
233 static int efx_process_channel(struct efx_channel
*channel
, int budget
)
237 if (unlikely(!channel
->enabled
))
240 spent
= efx_nic_process_eventq(channel
, budget
);
241 if (spent
&& efx_channel_has_rx_queue(channel
)) {
242 struct efx_rx_queue
*rx_queue
=
243 efx_channel_get_rx_queue(channel
);
245 /* Deliver last RX packet. */
246 if (channel
->rx_pkt
) {
247 __efx_rx_packet(channel
, channel
->rx_pkt
);
248 channel
->rx_pkt
= NULL
;
250 if (rx_queue
->enabled
) {
251 efx_rx_strategy(channel
);
252 efx_fast_push_rx_descriptors(rx_queue
);
259 /* Mark channel as finished processing
261 * Note that since we will not receive further interrupts for this
262 * channel before we finish processing and call the eventq_read_ack()
263 * method, there is no need to use the interrupt hold-off timers.
265 static inline void efx_channel_processed(struct efx_channel
*channel
)
267 /* The interrupt handler for this channel may set work_pending
268 * as soon as we acknowledge the events we've seen. Make sure
269 * it's cleared before then. */
270 channel
->work_pending
= false;
273 efx_nic_eventq_read_ack(channel
);
278 * NAPI guarantees serialisation of polls of the same device, which
279 * provides the guarantee required by efx_process_channel().
281 static int efx_poll(struct napi_struct
*napi
, int budget
)
283 struct efx_channel
*channel
=
284 container_of(napi
, struct efx_channel
, napi_str
);
285 struct efx_nic
*efx
= channel
->efx
;
288 netif_vdbg(efx
, intr
, efx
->net_dev
,
289 "channel %d NAPI poll executing on CPU %d\n",
290 channel
->channel
, raw_smp_processor_id());
292 spent
= efx_process_channel(channel
, budget
);
294 if (spent
< budget
) {
295 if (efx_channel_has_rx_queue(channel
) &&
296 efx
->irq_rx_adaptive
&&
297 unlikely(++channel
->irq_count
== 1000)) {
298 if (unlikely(channel
->irq_mod_score
<
299 irq_adapt_low_thresh
)) {
300 if (channel
->irq_moderation
> 1) {
301 channel
->irq_moderation
-= 1;
302 efx
->type
->push_irq_moderation(channel
);
304 } else if (unlikely(channel
->irq_mod_score
>
305 irq_adapt_high_thresh
)) {
306 if (channel
->irq_moderation
<
307 efx
->irq_rx_moderation
) {
308 channel
->irq_moderation
+= 1;
309 efx
->type
->push_irq_moderation(channel
);
312 channel
->irq_count
= 0;
313 channel
->irq_mod_score
= 0;
316 efx_filter_rfs_expire(channel
);
318 /* There is no race here; although napi_disable() will
319 * only wait for napi_complete(), this isn't a problem
320 * since efx_channel_processed() will have no effect if
321 * interrupts have already been disabled.
324 efx_channel_processed(channel
);
330 /* Process the eventq of the specified channel immediately on this CPU
332 * Disable hardware generated interrupts, wait for any existing
333 * processing to finish, then directly poll (and ack ) the eventq.
334 * Finally reenable NAPI and interrupts.
336 * This is for use only during a loopback self-test. It must not
337 * deliver any packets up the stack as this can result in deadlock.
339 void efx_process_channel_now(struct efx_channel
*channel
)
341 struct efx_nic
*efx
= channel
->efx
;
343 BUG_ON(channel
->channel
>= efx
->n_channels
);
344 BUG_ON(!channel
->enabled
);
345 BUG_ON(!efx
->loopback_selftest
);
347 /* Disable interrupts and wait for ISRs to complete */
348 efx_nic_disable_interrupts(efx
);
349 if (efx
->legacy_irq
) {
350 synchronize_irq(efx
->legacy_irq
);
351 efx
->legacy_irq_enabled
= false;
354 synchronize_irq(channel
->irq
);
356 /* Wait for any NAPI processing to complete */
357 napi_disable(&channel
->napi_str
);
359 /* Poll the channel */
360 efx_process_channel(channel
, channel
->eventq_mask
+ 1);
362 /* Ack the eventq. This may cause an interrupt to be generated
363 * when they are reenabled */
364 efx_channel_processed(channel
);
366 napi_enable(&channel
->napi_str
);
368 efx
->legacy_irq_enabled
= true;
369 efx_nic_enable_interrupts(efx
);
372 /* Create event queue
373 * Event queue memory allocations are done only once. If the channel
374 * is reset, the memory buffer will be reused; this guards against
375 * errors during channel reset and also simplifies interrupt handling.
377 static int efx_probe_eventq(struct efx_channel
*channel
)
379 struct efx_nic
*efx
= channel
->efx
;
380 unsigned long entries
;
382 netif_dbg(efx
, probe
, efx
->net_dev
,
383 "chan %d create event queue\n", channel
->channel
);
385 /* Build an event queue with room for one event per tx and rx buffer,
386 * plus some extra for link state events and MCDI completions. */
387 entries
= roundup_pow_of_two(efx
->rxq_entries
+ efx
->txq_entries
+ 128);
388 EFX_BUG_ON_PARANOID(entries
> EFX_MAX_EVQ_SIZE
);
389 channel
->eventq_mask
= max(entries
, EFX_MIN_EVQ_SIZE
) - 1;
391 return efx_nic_probe_eventq(channel
);
394 /* Prepare channel's event queue */
395 static void efx_init_eventq(struct efx_channel
*channel
)
397 netif_dbg(channel
->efx
, drv
, channel
->efx
->net_dev
,
398 "chan %d init event queue\n", channel
->channel
);
400 channel
->eventq_read_ptr
= 0;
402 efx_nic_init_eventq(channel
);
405 /* Enable event queue processing and NAPI */
406 static void efx_start_eventq(struct efx_channel
*channel
)
408 netif_dbg(channel
->efx
, ifup
, channel
->efx
->net_dev
,
409 "chan %d start event queue\n", channel
->channel
);
411 /* The interrupt handler for this channel may set work_pending
412 * as soon as we enable it. Make sure it's cleared before
413 * then. Similarly, make sure it sees the enabled flag set.
415 channel
->work_pending
= false;
416 channel
->enabled
= true;
419 napi_enable(&channel
->napi_str
);
420 efx_nic_eventq_read_ack(channel
);
423 /* Disable event queue processing and NAPI */
424 static void efx_stop_eventq(struct efx_channel
*channel
)
426 if (!channel
->enabled
)
429 napi_disable(&channel
->napi_str
);
430 channel
->enabled
= false;
433 static void efx_fini_eventq(struct efx_channel
*channel
)
435 netif_dbg(channel
->efx
, drv
, channel
->efx
->net_dev
,
436 "chan %d fini event queue\n", channel
->channel
);
438 efx_nic_fini_eventq(channel
);
441 static void efx_remove_eventq(struct efx_channel
*channel
)
443 netif_dbg(channel
->efx
, drv
, channel
->efx
->net_dev
,
444 "chan %d remove event queue\n", channel
->channel
);
446 efx_nic_remove_eventq(channel
);
449 /**************************************************************************
453 *************************************************************************/
455 /* Allocate and initialise a channel structure. */
456 static struct efx_channel
*
457 efx_alloc_channel(struct efx_nic
*efx
, int i
, struct efx_channel
*old_channel
)
459 struct efx_channel
*channel
;
460 struct efx_rx_queue
*rx_queue
;
461 struct efx_tx_queue
*tx_queue
;
464 channel
= kzalloc(sizeof(*channel
), GFP_KERNEL
);
469 channel
->channel
= i
;
470 channel
->type
= &efx_default_channel_type
;
472 for (j
= 0; j
< EFX_TXQ_TYPES
; j
++) {
473 tx_queue
= &channel
->tx_queue
[j
];
475 tx_queue
->queue
= i
* EFX_TXQ_TYPES
+ j
;
476 tx_queue
->channel
= channel
;
479 rx_queue
= &channel
->rx_queue
;
481 setup_timer(&rx_queue
->slow_fill
, efx_rx_slow_fill
,
482 (unsigned long)rx_queue
);
487 /* Allocate and initialise a channel structure, copying parameters
488 * (but not resources) from an old channel structure.
490 static struct efx_channel
*
491 efx_copy_channel(const struct efx_channel
*old_channel
)
493 struct efx_channel
*channel
;
494 struct efx_rx_queue
*rx_queue
;
495 struct efx_tx_queue
*tx_queue
;
498 channel
= kmalloc(sizeof(*channel
), GFP_KERNEL
);
502 *channel
= *old_channel
;
504 channel
->napi_dev
= NULL
;
505 memset(&channel
->eventq
, 0, sizeof(channel
->eventq
));
507 for (j
= 0; j
< EFX_TXQ_TYPES
; j
++) {
508 tx_queue
= &channel
->tx_queue
[j
];
509 if (tx_queue
->channel
)
510 tx_queue
->channel
= channel
;
511 tx_queue
->buffer
= NULL
;
512 memset(&tx_queue
->txd
, 0, sizeof(tx_queue
->txd
));
515 rx_queue
= &channel
->rx_queue
;
516 rx_queue
->buffer
= NULL
;
517 memset(&rx_queue
->rxd
, 0, sizeof(rx_queue
->rxd
));
518 setup_timer(&rx_queue
->slow_fill
, efx_rx_slow_fill
,
519 (unsigned long)rx_queue
);
524 static int efx_probe_channel(struct efx_channel
*channel
)
526 struct efx_tx_queue
*tx_queue
;
527 struct efx_rx_queue
*rx_queue
;
530 netif_dbg(channel
->efx
, probe
, channel
->efx
->net_dev
,
531 "creating channel %d\n", channel
->channel
);
533 rc
= channel
->type
->pre_probe(channel
);
537 rc
= efx_probe_eventq(channel
);
541 efx_for_each_channel_tx_queue(tx_queue
, channel
) {
542 rc
= efx_probe_tx_queue(tx_queue
);
547 efx_for_each_channel_rx_queue(rx_queue
, channel
) {
548 rc
= efx_probe_rx_queue(rx_queue
);
553 channel
->n_rx_frm_trunc
= 0;
558 efx_remove_channel(channel
);
563 efx_get_channel_name(struct efx_channel
*channel
, char *buf
, size_t len
)
565 struct efx_nic
*efx
= channel
->efx
;
569 number
= channel
->channel
;
570 if (efx
->tx_channel_offset
== 0) {
572 } else if (channel
->channel
< efx
->tx_channel_offset
) {
576 number
-= efx
->tx_channel_offset
;
578 snprintf(buf
, len
, "%s%s-%d", efx
->name
, type
, number
);
581 static void efx_set_channel_names(struct efx_nic
*efx
)
583 struct efx_channel
*channel
;
585 efx_for_each_channel(channel
, efx
)
586 channel
->type
->get_name(channel
,
587 efx
->channel_name
[channel
->channel
],
588 sizeof(efx
->channel_name
[0]));
591 static int efx_probe_channels(struct efx_nic
*efx
)
593 struct efx_channel
*channel
;
596 /* Restart special buffer allocation */
597 efx
->next_buffer_table
= 0;
599 /* Probe channels in reverse, so that any 'extra' channels
600 * use the start of the buffer table. This allows the traffic
601 * channels to be resized without moving them or wasting the
602 * entries before them.
604 efx_for_each_channel_rev(channel
, efx
) {
605 rc
= efx_probe_channel(channel
);
607 netif_err(efx
, probe
, efx
->net_dev
,
608 "failed to create channel %d\n",
613 efx_set_channel_names(efx
);
618 efx_remove_channels(efx
);
622 /* Channels are shutdown and reinitialised whilst the NIC is running
623 * to propagate configuration changes (mtu, checksum offload), or
624 * to clear hardware error conditions
626 static void efx_start_datapath(struct efx_nic
*efx
)
628 struct efx_tx_queue
*tx_queue
;
629 struct efx_rx_queue
*rx_queue
;
630 struct efx_channel
*channel
;
632 /* Calculate the rx buffer allocation parameters required to
633 * support the current MTU, including padding for header
634 * alignment and overruns.
636 efx
->rx_buffer_len
= (max(EFX_PAGE_IP_ALIGN
, NET_IP_ALIGN
) +
637 EFX_MAX_FRAME_LEN(efx
->net_dev
->mtu
) +
638 efx
->type
->rx_buffer_hash_size
+
639 efx
->type
->rx_buffer_padding
);
640 efx
->rx_buffer_order
= get_order(efx
->rx_buffer_len
+
641 sizeof(struct efx_rx_page_state
));
643 /* We must keep at least one descriptor in a TX ring empty.
644 * We could avoid this when the queue size does not exactly
645 * match the hardware ring size, but it's not that important.
646 * Therefore we stop the queue when one more skb might fill
647 * the ring completely. We wake it when half way back to
650 efx
->txq_stop_thresh
= efx
->txq_entries
- efx_tx_max_skb_descs(efx
);
651 efx
->txq_wake_thresh
= efx
->txq_stop_thresh
/ 2;
653 /* Initialise the channels */
654 efx_for_each_channel(channel
, efx
) {
655 efx_for_each_channel_tx_queue(tx_queue
, channel
)
656 efx_init_tx_queue(tx_queue
);
658 /* The rx buffer allocation strategy is MTU dependent */
659 efx_rx_strategy(channel
);
661 efx_for_each_channel_rx_queue(rx_queue
, channel
) {
662 efx_init_rx_queue(rx_queue
);
663 efx_nic_generate_fill_event(rx_queue
);
666 WARN_ON(channel
->rx_pkt
!= NULL
);
667 efx_rx_strategy(channel
);
670 if (netif_device_present(efx
->net_dev
))
671 netif_tx_wake_all_queues(efx
->net_dev
);
674 static void efx_stop_datapath(struct efx_nic
*efx
)
676 struct efx_channel
*channel
;
677 struct efx_tx_queue
*tx_queue
;
678 struct efx_rx_queue
*rx_queue
;
679 struct pci_dev
*dev
= efx
->pci_dev
;
682 EFX_ASSERT_RESET_SERIALISED(efx
);
683 BUG_ON(efx
->port_enabled
);
685 /* Only perform flush if dma is enabled */
686 if (dev
->is_busmaster
) {
687 rc
= efx_nic_flush_queues(efx
);
689 if (rc
&& EFX_WORKAROUND_7803(efx
)) {
690 /* Schedule a reset to recover from the flush failure. The
691 * descriptor caches reference memory we're about to free,
692 * but falcon_reconfigure_mac_wrapper() won't reconnect
693 * the MACs because of the pending reset. */
694 netif_err(efx
, drv
, efx
->net_dev
,
695 "Resetting to recover from flush failure\n");
696 efx_schedule_reset(efx
, RESET_TYPE_ALL
);
698 netif_err(efx
, drv
, efx
->net_dev
, "failed to flush queues\n");
700 netif_dbg(efx
, drv
, efx
->net_dev
,
701 "successfully flushed all queues\n");
705 efx_for_each_channel(channel
, efx
) {
706 /* RX packet processing is pipelined, so wait for the
707 * NAPI handler to complete. At least event queue 0
708 * might be kept active by non-data events, so don't
709 * use napi_synchronize() but actually disable NAPI
712 if (efx_channel_has_rx_queue(channel
)) {
713 efx_stop_eventq(channel
);
714 efx_start_eventq(channel
);
717 efx_for_each_channel_rx_queue(rx_queue
, channel
)
718 efx_fini_rx_queue(rx_queue
);
719 efx_for_each_possible_channel_tx_queue(tx_queue
, channel
)
720 efx_fini_tx_queue(tx_queue
);
724 static void efx_remove_channel(struct efx_channel
*channel
)
726 struct efx_tx_queue
*tx_queue
;
727 struct efx_rx_queue
*rx_queue
;
729 netif_dbg(channel
->efx
, drv
, channel
->efx
->net_dev
,
730 "destroy chan %d\n", channel
->channel
);
732 efx_for_each_channel_rx_queue(rx_queue
, channel
)
733 efx_remove_rx_queue(rx_queue
);
734 efx_for_each_possible_channel_tx_queue(tx_queue
, channel
)
735 efx_remove_tx_queue(tx_queue
);
736 efx_remove_eventq(channel
);
737 channel
->type
->post_remove(channel
);
740 static void efx_remove_channels(struct efx_nic
*efx
)
742 struct efx_channel
*channel
;
744 efx_for_each_channel(channel
, efx
)
745 efx_remove_channel(channel
);
749 efx_realloc_channels(struct efx_nic
*efx
, u32 rxq_entries
, u32 txq_entries
)
751 struct efx_channel
*other_channel
[EFX_MAX_CHANNELS
], *channel
;
752 u32 old_rxq_entries
, old_txq_entries
;
753 unsigned i
, next_buffer_table
= 0;
756 rc
= efx_check_disabled(efx
);
760 /* Not all channels should be reallocated. We must avoid
761 * reallocating their buffer table entries.
763 efx_for_each_channel(channel
, efx
) {
764 struct efx_rx_queue
*rx_queue
;
765 struct efx_tx_queue
*tx_queue
;
767 if (channel
->type
->copy
)
769 next_buffer_table
= max(next_buffer_table
,
770 channel
->eventq
.index
+
771 channel
->eventq
.entries
);
772 efx_for_each_channel_rx_queue(rx_queue
, channel
)
773 next_buffer_table
= max(next_buffer_table
,
774 rx_queue
->rxd
.index
+
775 rx_queue
->rxd
.entries
);
776 efx_for_each_channel_tx_queue(tx_queue
, channel
)
777 next_buffer_table
= max(next_buffer_table
,
778 tx_queue
->txd
.index
+
779 tx_queue
->txd
.entries
);
783 efx_stop_interrupts(efx
, true);
785 /* Clone channels (where possible) */
786 memset(other_channel
, 0, sizeof(other_channel
));
787 for (i
= 0; i
< efx
->n_channels
; i
++) {
788 channel
= efx
->channel
[i
];
789 if (channel
->type
->copy
)
790 channel
= channel
->type
->copy(channel
);
795 other_channel
[i
] = channel
;
798 /* Swap entry counts and channel pointers */
799 old_rxq_entries
= efx
->rxq_entries
;
800 old_txq_entries
= efx
->txq_entries
;
801 efx
->rxq_entries
= rxq_entries
;
802 efx
->txq_entries
= txq_entries
;
803 for (i
= 0; i
< efx
->n_channels
; i
++) {
804 channel
= efx
->channel
[i
];
805 efx
->channel
[i
] = other_channel
[i
];
806 other_channel
[i
] = channel
;
809 /* Restart buffer table allocation */
810 efx
->next_buffer_table
= next_buffer_table
;
812 for (i
= 0; i
< efx
->n_channels
; i
++) {
813 channel
= efx
->channel
[i
];
814 if (!channel
->type
->copy
)
816 rc
= efx_probe_channel(channel
);
819 efx_init_napi_channel(efx
->channel
[i
]);
823 /* Destroy unused channel structures */
824 for (i
= 0; i
< efx
->n_channels
; i
++) {
825 channel
= other_channel
[i
];
826 if (channel
&& channel
->type
->copy
) {
827 efx_fini_napi_channel(channel
);
828 efx_remove_channel(channel
);
833 efx_start_interrupts(efx
, true);
839 efx
->rxq_entries
= old_rxq_entries
;
840 efx
->txq_entries
= old_txq_entries
;
841 for (i
= 0; i
< efx
->n_channels
; i
++) {
842 channel
= efx
->channel
[i
];
843 efx
->channel
[i
] = other_channel
[i
];
844 other_channel
[i
] = channel
;
849 void efx_schedule_slow_fill(struct efx_rx_queue
*rx_queue
)
851 mod_timer(&rx_queue
->slow_fill
, jiffies
+ msecs_to_jiffies(100));
854 static const struct efx_channel_type efx_default_channel_type
= {
855 .pre_probe
= efx_channel_dummy_op_int
,
856 .post_remove
= efx_channel_dummy_op_void
,
857 .get_name
= efx_get_channel_name
,
858 .copy
= efx_copy_channel
,
859 .keep_eventq
= false,
862 int efx_channel_dummy_op_int(struct efx_channel
*channel
)
867 void efx_channel_dummy_op_void(struct efx_channel
*channel
)
871 /**************************************************************************
875 **************************************************************************/
877 /* This ensures that the kernel is kept informed (via
878 * netif_carrier_on/off) of the link status, and also maintains the
879 * link status's stop on the port's TX queue.
881 void efx_link_status_changed(struct efx_nic
*efx
)
883 struct efx_link_state
*link_state
= &efx
->link_state
;
885 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
886 * that no events are triggered between unregister_netdev() and the
887 * driver unloading. A more general condition is that NETDEV_CHANGE
888 * can only be generated between NETDEV_UP and NETDEV_DOWN */
889 if (!netif_running(efx
->net_dev
))
892 if (link_state
->up
!= netif_carrier_ok(efx
->net_dev
)) {
893 efx
->n_link_state_changes
++;
896 netif_carrier_on(efx
->net_dev
);
898 netif_carrier_off(efx
->net_dev
);
901 /* Status message for kernel log */
903 netif_info(efx
, link
, efx
->net_dev
,
904 "link up at %uMbps %s-duplex (MTU %d)%s\n",
905 link_state
->speed
, link_state
->fd
? "full" : "half",
907 (efx
->promiscuous
? " [PROMISC]" : ""));
909 netif_info(efx
, link
, efx
->net_dev
, "link down\n");
912 void efx_link_set_advertising(struct efx_nic
*efx
, u32 advertising
)
914 efx
->link_advertising
= advertising
;
916 if (advertising
& ADVERTISED_Pause
)
917 efx
->wanted_fc
|= (EFX_FC_TX
| EFX_FC_RX
);
919 efx
->wanted_fc
&= ~(EFX_FC_TX
| EFX_FC_RX
);
920 if (advertising
& ADVERTISED_Asym_Pause
)
921 efx
->wanted_fc
^= EFX_FC_TX
;
925 void efx_link_set_wanted_fc(struct efx_nic
*efx
, u8 wanted_fc
)
927 efx
->wanted_fc
= wanted_fc
;
928 if (efx
->link_advertising
) {
929 if (wanted_fc
& EFX_FC_RX
)
930 efx
->link_advertising
|= (ADVERTISED_Pause
|
931 ADVERTISED_Asym_Pause
);
933 efx
->link_advertising
&= ~(ADVERTISED_Pause
|
934 ADVERTISED_Asym_Pause
);
935 if (wanted_fc
& EFX_FC_TX
)
936 efx
->link_advertising
^= ADVERTISED_Asym_Pause
;
940 static void efx_fini_port(struct efx_nic
*efx
);
942 /* Push loopback/power/transmit disable settings to the PHY, and reconfigure
943 * the MAC appropriately. All other PHY configuration changes are pushed
944 * through phy_op->set_settings(), and pushed asynchronously to the MAC
945 * through efx_monitor().
947 * Callers must hold the mac_lock
949 int __efx_reconfigure_port(struct efx_nic
*efx
)
951 enum efx_phy_mode phy_mode
;
954 WARN_ON(!mutex_is_locked(&efx
->mac_lock
));
956 /* Serialise the promiscuous flag with efx_set_rx_mode. */
957 netif_addr_lock_bh(efx
->net_dev
);
958 netif_addr_unlock_bh(efx
->net_dev
);
960 /* Disable PHY transmit in mac level loopbacks */
961 phy_mode
= efx
->phy_mode
;
962 if (LOOPBACK_INTERNAL(efx
))
963 efx
->phy_mode
|= PHY_MODE_TX_DISABLED
;
965 efx
->phy_mode
&= ~PHY_MODE_TX_DISABLED
;
967 rc
= efx
->type
->reconfigure_port(efx
);
970 efx
->phy_mode
= phy_mode
;
975 /* Reinitialise the MAC to pick up new PHY settings, even if the port is
977 int efx_reconfigure_port(struct efx_nic
*efx
)
981 EFX_ASSERT_RESET_SERIALISED(efx
);
983 mutex_lock(&efx
->mac_lock
);
984 rc
= __efx_reconfigure_port(efx
);
985 mutex_unlock(&efx
->mac_lock
);
990 /* Asynchronous work item for changing MAC promiscuity and multicast
991 * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
993 static void efx_mac_work(struct work_struct
*data
)
995 struct efx_nic
*efx
= container_of(data
, struct efx_nic
, mac_work
);
997 mutex_lock(&efx
->mac_lock
);
998 if (efx
->port_enabled
)
999 efx
->type
->reconfigure_mac(efx
);
1000 mutex_unlock(&efx
->mac_lock
);
1003 static int efx_probe_port(struct efx_nic
*efx
)
1007 netif_dbg(efx
, probe
, efx
->net_dev
, "create port\n");
1010 efx
->phy_mode
= PHY_MODE_SPECIAL
;
1012 /* Connect up MAC/PHY operations table */
1013 rc
= efx
->type
->probe_port(efx
);
1017 /* Initialise MAC address to permanent address */
1018 memcpy(efx
->net_dev
->dev_addr
, efx
->net_dev
->perm_addr
, ETH_ALEN
);
1023 static int efx_init_port(struct efx_nic
*efx
)
1027 netif_dbg(efx
, drv
, efx
->net_dev
, "init port\n");
1029 mutex_lock(&efx
->mac_lock
);
1031 rc
= efx
->phy_op
->init(efx
);
1035 efx
->port_initialized
= true;
1037 /* Reconfigure the MAC before creating dma queues (required for
1038 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
1039 efx
->type
->reconfigure_mac(efx
);
1041 /* Ensure the PHY advertises the correct flow control settings */
1042 rc
= efx
->phy_op
->reconfigure(efx
);
1046 mutex_unlock(&efx
->mac_lock
);
1050 efx
->phy_op
->fini(efx
);
1052 mutex_unlock(&efx
->mac_lock
);
1056 static void efx_start_port(struct efx_nic
*efx
)
1058 netif_dbg(efx
, ifup
, efx
->net_dev
, "start port\n");
1059 BUG_ON(efx
->port_enabled
);
1061 mutex_lock(&efx
->mac_lock
);
1062 efx
->port_enabled
= true;
1064 /* efx_mac_work() might have been scheduled after efx_stop_port(),
1065 * and then cancelled by efx_flush_all() */
1066 efx
->type
->reconfigure_mac(efx
);
1068 mutex_unlock(&efx
->mac_lock
);
1071 /* Prevent efx_mac_work() and efx_monitor() from working */
1072 static void efx_stop_port(struct efx_nic
*efx
)
1074 netif_dbg(efx
, ifdown
, efx
->net_dev
, "stop port\n");
1076 mutex_lock(&efx
->mac_lock
);
1077 efx
->port_enabled
= false;
1078 mutex_unlock(&efx
->mac_lock
);
1080 /* Serialise against efx_set_multicast_list() */
1081 netif_addr_lock_bh(efx
->net_dev
);
1082 netif_addr_unlock_bh(efx
->net_dev
);
1085 static void efx_fini_port(struct efx_nic
*efx
)
1087 netif_dbg(efx
, drv
, efx
->net_dev
, "shut down port\n");
1089 if (!efx
->port_initialized
)
1092 efx
->phy_op
->fini(efx
);
1093 efx
->port_initialized
= false;
1095 efx
->link_state
.up
= false;
1096 efx_link_status_changed(efx
);
1099 static void efx_remove_port(struct efx_nic
*efx
)
1101 netif_dbg(efx
, drv
, efx
->net_dev
, "destroying port\n");
1103 efx
->type
->remove_port(efx
);
1106 /**************************************************************************
1110 **************************************************************************/
1112 /* This configures the PCI device to enable I/O and DMA. */
1113 static int efx_init_io(struct efx_nic
*efx
)
1115 struct pci_dev
*pci_dev
= efx
->pci_dev
;
1116 dma_addr_t dma_mask
= efx
->type
->max_dma_mask
;
1119 netif_dbg(efx
, probe
, efx
->net_dev
, "initialising I/O\n");
1121 rc
= pci_enable_device(pci_dev
);
1123 netif_err(efx
, probe
, efx
->net_dev
,
1124 "failed to enable PCI device\n");
1128 pci_set_master(pci_dev
);
1130 /* Set the PCI DMA mask. Try all possibilities from our
1131 * genuine mask down to 32 bits, because some architectures
1132 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
1133 * masks event though they reject 46 bit masks.
1135 while (dma_mask
> 0x7fffffffUL
) {
1136 if (dma_supported(&pci_dev
->dev
, dma_mask
)) {
1137 rc
= dma_set_mask(&pci_dev
->dev
, dma_mask
);
1144 netif_err(efx
, probe
, efx
->net_dev
,
1145 "could not find a suitable DMA mask\n");
1148 netif_dbg(efx
, probe
, efx
->net_dev
,
1149 "using DMA mask %llx\n", (unsigned long long) dma_mask
);
1150 rc
= dma_set_coherent_mask(&pci_dev
->dev
, dma_mask
);
1152 /* dma_set_coherent_mask() is not *allowed* to
1153 * fail with a mask that dma_set_mask() accepted,
1154 * but just in case...
1156 netif_err(efx
, probe
, efx
->net_dev
,
1157 "failed to set consistent DMA mask\n");
1161 efx
->membase_phys
= pci_resource_start(efx
->pci_dev
, EFX_MEM_BAR
);
1162 rc
= pci_request_region(pci_dev
, EFX_MEM_BAR
, "sfc");
1164 netif_err(efx
, probe
, efx
->net_dev
,
1165 "request for memory BAR failed\n");
1169 efx
->membase
= ioremap_nocache(efx
->membase_phys
,
1170 efx
->type
->mem_map_size
);
1171 if (!efx
->membase
) {
1172 netif_err(efx
, probe
, efx
->net_dev
,
1173 "could not map memory BAR at %llx+%x\n",
1174 (unsigned long long)efx
->membase_phys
,
1175 efx
->type
->mem_map_size
);
1179 netif_dbg(efx
, probe
, efx
->net_dev
,
1180 "memory BAR at %llx+%x (virtual %p)\n",
1181 (unsigned long long)efx
->membase_phys
,
1182 efx
->type
->mem_map_size
, efx
->membase
);
1187 pci_release_region(efx
->pci_dev
, EFX_MEM_BAR
);
1189 efx
->membase_phys
= 0;
1191 pci_disable_device(efx
->pci_dev
);
1196 static void efx_fini_io(struct efx_nic
*efx
)
1198 netif_dbg(efx
, drv
, efx
->net_dev
, "shutting down I/O\n");
1201 iounmap(efx
->membase
);
1202 efx
->membase
= NULL
;
1205 if (efx
->membase_phys
) {
1206 pci_release_region(efx
->pci_dev
, EFX_MEM_BAR
);
1207 efx
->membase_phys
= 0;
1210 pci_disable_device(efx
->pci_dev
);
1213 static unsigned int efx_wanted_parallelism(struct efx_nic
*efx
)
1215 cpumask_var_t thread_mask
;
1222 if (unlikely(!zalloc_cpumask_var(&thread_mask
, GFP_KERNEL
))) {
1223 netif_warn(efx
, probe
, efx
->net_dev
,
1224 "RSS disabled due to allocation failure\n");
1229 for_each_online_cpu(cpu
) {
1230 if (!cpumask_test_cpu(cpu
, thread_mask
)) {
1232 cpumask_or(thread_mask
, thread_mask
,
1233 topology_thread_cpumask(cpu
));
1237 free_cpumask_var(thread_mask
);
1240 /* If RSS is requested for the PF *and* VFs then we can't write RSS
1241 * table entries that are inaccessible to VFs
1243 if (efx_sriov_wanted(efx
) && efx_vf_size(efx
) > 1 &&
1244 count
> efx_vf_size(efx
)) {
1245 netif_warn(efx
, probe
, efx
->net_dev
,
1246 "Reducing number of RSS channels from %u to %u for "
1247 "VF support. Increase vf-msix-limit to use more "
1248 "channels on the PF.\n",
1249 count
, efx_vf_size(efx
));
1250 count
= efx_vf_size(efx
);
1257 efx_init_rx_cpu_rmap(struct efx_nic
*efx
, struct msix_entry
*xentries
)
1259 #ifdef CONFIG_RFS_ACCEL
1263 efx
->net_dev
->rx_cpu_rmap
= alloc_irq_cpu_rmap(efx
->n_rx_channels
);
1264 if (!efx
->net_dev
->rx_cpu_rmap
)
1266 for (i
= 0; i
< efx
->n_rx_channels
; i
++) {
1267 rc
= irq_cpu_rmap_add(efx
->net_dev
->rx_cpu_rmap
,
1268 xentries
[i
].vector
);
1270 free_irq_cpu_rmap(efx
->net_dev
->rx_cpu_rmap
);
1271 efx
->net_dev
->rx_cpu_rmap
= NULL
;
1279 /* Probe the number and type of interrupts we are able to obtain, and
1280 * the resulting numbers of channels and RX queues.
1282 static int efx_probe_interrupts(struct efx_nic
*efx
)
1284 unsigned int max_channels
=
1285 min(efx
->type
->phys_addr_channels
, EFX_MAX_CHANNELS
);
1286 unsigned int extra_channels
= 0;
1290 for (i
= 0; i
< EFX_MAX_EXTRA_CHANNELS
; i
++)
1291 if (efx
->extra_channel_type
[i
])
1294 if (efx
->interrupt_mode
== EFX_INT_MODE_MSIX
) {
1295 struct msix_entry xentries
[EFX_MAX_CHANNELS
];
1296 unsigned int n_channels
;
1298 n_channels
= efx_wanted_parallelism(efx
);
1299 if (separate_tx_channels
)
1301 n_channels
+= extra_channels
;
1302 n_channels
= min(n_channels
, max_channels
);
1304 for (i
= 0; i
< n_channels
; i
++)
1305 xentries
[i
].entry
= i
;
1306 rc
= pci_enable_msix(efx
->pci_dev
, xentries
, n_channels
);
1308 netif_err(efx
, drv
, efx
->net_dev
,
1309 "WARNING: Insufficient MSI-X vectors"
1310 " available (%d < %u).\n", rc
, n_channels
);
1311 netif_err(efx
, drv
, efx
->net_dev
,
1312 "WARNING: Performance may be reduced.\n");
1313 EFX_BUG_ON_PARANOID(rc
>= n_channels
);
1315 rc
= pci_enable_msix(efx
->pci_dev
, xentries
,
1320 efx
->n_channels
= n_channels
;
1321 if (n_channels
> extra_channels
)
1322 n_channels
-= extra_channels
;
1323 if (separate_tx_channels
) {
1324 efx
->n_tx_channels
= max(n_channels
/ 2, 1U);
1325 efx
->n_rx_channels
= max(n_channels
-
1329 efx
->n_tx_channels
= n_channels
;
1330 efx
->n_rx_channels
= n_channels
;
1332 rc
= efx_init_rx_cpu_rmap(efx
, xentries
);
1334 pci_disable_msix(efx
->pci_dev
);
1337 for (i
= 0; i
< efx
->n_channels
; i
++)
1338 efx_get_channel(efx
, i
)->irq
=
1341 /* Fall back to single channel MSI */
1342 efx
->interrupt_mode
= EFX_INT_MODE_MSI
;
1343 netif_err(efx
, drv
, efx
->net_dev
,
1344 "could not enable MSI-X\n");
1348 /* Try single interrupt MSI */
1349 if (efx
->interrupt_mode
== EFX_INT_MODE_MSI
) {
1350 efx
->n_channels
= 1;
1351 efx
->n_rx_channels
= 1;
1352 efx
->n_tx_channels
= 1;
1353 rc
= pci_enable_msi(efx
->pci_dev
);
1355 efx_get_channel(efx
, 0)->irq
= efx
->pci_dev
->irq
;
1357 netif_err(efx
, drv
, efx
->net_dev
,
1358 "could not enable MSI\n");
1359 efx
->interrupt_mode
= EFX_INT_MODE_LEGACY
;
1363 /* Assume legacy interrupts */
1364 if (efx
->interrupt_mode
== EFX_INT_MODE_LEGACY
) {
1365 efx
->n_channels
= 1 + (separate_tx_channels
? 1 : 0);
1366 efx
->n_rx_channels
= 1;
1367 efx
->n_tx_channels
= 1;
1368 efx
->legacy_irq
= efx
->pci_dev
->irq
;
1371 /* Assign extra channels if possible */
1372 j
= efx
->n_channels
;
1373 for (i
= 0; i
< EFX_MAX_EXTRA_CHANNELS
; i
++) {
1374 if (!efx
->extra_channel_type
[i
])
1376 if (efx
->interrupt_mode
!= EFX_INT_MODE_MSIX
||
1377 efx
->n_channels
<= extra_channels
) {
1378 efx
->extra_channel_type
[i
]->handle_no_channel(efx
);
1381 efx_get_channel(efx
, j
)->type
=
1382 efx
->extra_channel_type
[i
];
1386 /* RSS might be usable on VFs even if it is disabled on the PF */
1387 efx
->rss_spread
= ((efx
->n_rx_channels
> 1 || !efx_sriov_wanted(efx
)) ?
1388 efx
->n_rx_channels
: efx_vf_size(efx
));
1393 /* Enable interrupts, then probe and start the event queues */
1394 static void efx_start_interrupts(struct efx_nic
*efx
, bool may_keep_eventq
)
1396 struct efx_channel
*channel
;
1398 BUG_ON(efx
->state
== STATE_DISABLED
);
1400 if (efx
->legacy_irq
)
1401 efx
->legacy_irq_enabled
= true;
1402 efx_nic_enable_interrupts(efx
);
1404 efx_for_each_channel(channel
, efx
) {
1405 if (!channel
->type
->keep_eventq
|| !may_keep_eventq
)
1406 efx_init_eventq(channel
);
1407 efx_start_eventq(channel
);
1410 efx_mcdi_mode_event(efx
);
1413 static void efx_stop_interrupts(struct efx_nic
*efx
, bool may_keep_eventq
)
1415 struct efx_channel
*channel
;
1417 if (efx
->state
== STATE_DISABLED
)
1420 efx_mcdi_mode_poll(efx
);
1422 efx_nic_disable_interrupts(efx
);
1423 if (efx
->legacy_irq
) {
1424 synchronize_irq(efx
->legacy_irq
);
1425 efx
->legacy_irq_enabled
= false;
1428 efx_for_each_channel(channel
, efx
) {
1430 synchronize_irq(channel
->irq
);
1432 efx_stop_eventq(channel
);
1433 if (!channel
->type
->keep_eventq
|| !may_keep_eventq
)
1434 efx_fini_eventq(channel
);
1438 static void efx_remove_interrupts(struct efx_nic
*efx
)
1440 struct efx_channel
*channel
;
1442 /* Remove MSI/MSI-X interrupts */
1443 efx_for_each_channel(channel
, efx
)
1445 pci_disable_msi(efx
->pci_dev
);
1446 pci_disable_msix(efx
->pci_dev
);
1448 /* Remove legacy interrupt */
1449 efx
->legacy_irq
= 0;
1452 static void efx_set_channels(struct efx_nic
*efx
)
1454 struct efx_channel
*channel
;
1455 struct efx_tx_queue
*tx_queue
;
1457 efx
->tx_channel_offset
=
1458 separate_tx_channels
? efx
->n_channels
- efx
->n_tx_channels
: 0;
1460 /* We need to mark which channels really have RX and TX
1461 * queues, and adjust the TX queue numbers if we have separate
1462 * RX-only and TX-only channels.
1464 efx_for_each_channel(channel
, efx
) {
1465 if (channel
->channel
< efx
->n_rx_channels
)
1466 channel
->rx_queue
.core_index
= channel
->channel
;
1468 channel
->rx_queue
.core_index
= -1;
1470 efx_for_each_channel_tx_queue(tx_queue
, channel
)
1471 tx_queue
->queue
-= (efx
->tx_channel_offset
*
1476 static int efx_probe_nic(struct efx_nic
*efx
)
1481 netif_dbg(efx
, probe
, efx
->net_dev
, "creating NIC\n");
1483 /* Carry out hardware-type specific initialisation */
1484 rc
= efx
->type
->probe(efx
);
1488 /* Determine the number of channels and queues by trying to hook
1489 * in MSI-X interrupts. */
1490 rc
= efx_probe_interrupts(efx
);
1494 efx
->type
->dimension_resources(efx
);
1496 if (efx
->n_channels
> 1)
1497 get_random_bytes(&efx
->rx_hash_key
, sizeof(efx
->rx_hash_key
));
1498 for (i
= 0; i
< ARRAY_SIZE(efx
->rx_indir_table
); i
++)
1499 efx
->rx_indir_table
[i
] =
1500 ethtool_rxfh_indir_default(i
, efx
->rss_spread
);
1502 efx_set_channels(efx
);
1503 netif_set_real_num_tx_queues(efx
->net_dev
, efx
->n_tx_channels
);
1504 netif_set_real_num_rx_queues(efx
->net_dev
, efx
->n_rx_channels
);
1506 /* Initialise the interrupt moderation settings */
1507 efx_init_irq_moderation(efx
, tx_irq_mod_usec
, rx_irq_mod_usec
, true,
1513 efx
->type
->remove(efx
);
1517 static void efx_remove_nic(struct efx_nic
*efx
)
1519 netif_dbg(efx
, drv
, efx
->net_dev
, "destroying NIC\n");
1521 efx_remove_interrupts(efx
);
1522 efx
->type
->remove(efx
);
1525 /**************************************************************************
1527 * NIC startup/shutdown
1529 *************************************************************************/
1531 static int efx_probe_all(struct efx_nic
*efx
)
1535 rc
= efx_probe_nic(efx
);
1537 netif_err(efx
, probe
, efx
->net_dev
, "failed to create NIC\n");
1541 rc
= efx_probe_port(efx
);
1543 netif_err(efx
, probe
, efx
->net_dev
, "failed to create port\n");
1547 BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE
< EFX_RXQ_MIN_ENT
);
1548 if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE
< EFX_TXQ_MIN_ENT(efx
))) {
1552 efx
->rxq_entries
= efx
->txq_entries
= EFX_DEFAULT_DMAQ_SIZE
;
1554 rc
= efx_probe_filters(efx
);
1556 netif_err(efx
, probe
, efx
->net_dev
,
1557 "failed to create filter tables\n");
1561 rc
= efx_probe_channels(efx
);
1568 efx_remove_filters(efx
);
1570 efx_remove_port(efx
);
1572 efx_remove_nic(efx
);
1577 /* If the interface is supposed to be running but is not, start
1578 * the hardware and software data path, regular activity for the port
1579 * (MAC statistics, link polling, etc.) and schedule the port to be
1580 * reconfigured. Interrupts must already be enabled. This function
1581 * is safe to call multiple times, so long as the NIC is not disabled.
1582 * Requires the RTNL lock.
1584 static void efx_start_all(struct efx_nic
*efx
)
1586 EFX_ASSERT_RESET_SERIALISED(efx
);
1587 BUG_ON(efx
->state
== STATE_DISABLED
);
1589 /* Check that it is appropriate to restart the interface. All
1590 * of these flags are safe to read under just the rtnl lock */
1591 if (efx
->port_enabled
|| !netif_running(efx
->net_dev
))
1594 efx_start_port(efx
);
1595 efx_start_datapath(efx
);
1597 /* Start the hardware monitor if there is one. Otherwise (we're link
1598 * event driven), we have to poll the PHY because after an event queue
1599 * flush, we could have a missed a link state change */
1600 if (efx
->type
->monitor
!= NULL
) {
1601 queue_delayed_work(efx
->workqueue
, &efx
->monitor_work
,
1602 efx_monitor_interval
);
1604 mutex_lock(&efx
->mac_lock
);
1605 if (efx
->phy_op
->poll(efx
))
1606 efx_link_status_changed(efx
);
1607 mutex_unlock(&efx
->mac_lock
);
1610 efx
->type
->start_stats(efx
);
1613 /* Flush all delayed work. Should only be called when no more delayed work
1614 * will be scheduled. This doesn't flush pending online resets (efx_reset),
1615 * since we're holding the rtnl_lock at this point. */
1616 static void efx_flush_all(struct efx_nic
*efx
)
1618 /* Make sure the hardware monitor and event self-test are stopped */
1619 cancel_delayed_work_sync(&efx
->monitor_work
);
1620 efx_selftest_async_cancel(efx
);
1621 /* Stop scheduled port reconfigurations */
1622 cancel_work_sync(&efx
->mac_work
);
1625 /* Quiesce the hardware and software data path, and regular activity
1626 * for the port without bringing the link down. Safe to call multiple
1627 * times with the NIC in almost any state, but interrupts should be
1628 * enabled. Requires the RTNL lock.
1630 static void efx_stop_all(struct efx_nic
*efx
)
1632 EFX_ASSERT_RESET_SERIALISED(efx
);
1634 /* port_enabled can be read safely under the rtnl lock */
1635 if (!efx
->port_enabled
)
1638 efx
->type
->stop_stats(efx
);
1641 /* Flush efx_mac_work(), refill_workqueue, monitor_work */
1644 /* Stop the kernel transmit interface late, so the watchdog
1645 * timer isn't ticking over the flush */
1646 netif_tx_disable(efx
->net_dev
);
1648 efx_stop_datapath(efx
);
1651 static void efx_remove_all(struct efx_nic
*efx
)
1653 efx_remove_channels(efx
);
1654 efx_remove_filters(efx
);
1655 efx_remove_port(efx
);
1656 efx_remove_nic(efx
);
1659 /**************************************************************************
1661 * Interrupt moderation
1663 **************************************************************************/
1665 static unsigned int irq_mod_ticks(unsigned int usecs
, unsigned int quantum_ns
)
1669 if (usecs
* 1000 < quantum_ns
)
1670 return 1; /* never round down to 0 */
1671 return usecs
* 1000 / quantum_ns
;
1674 /* Set interrupt moderation parameters */
1675 int efx_init_irq_moderation(struct efx_nic
*efx
, unsigned int tx_usecs
,
1676 unsigned int rx_usecs
, bool rx_adaptive
,
1677 bool rx_may_override_tx
)
1679 struct efx_channel
*channel
;
1680 unsigned int irq_mod_max
= DIV_ROUND_UP(efx
->type
->timer_period_max
*
1681 efx
->timer_quantum_ns
,
1683 unsigned int tx_ticks
;
1684 unsigned int rx_ticks
;
1686 EFX_ASSERT_RESET_SERIALISED(efx
);
1688 if (tx_usecs
> irq_mod_max
|| rx_usecs
> irq_mod_max
)
1691 tx_ticks
= irq_mod_ticks(tx_usecs
, efx
->timer_quantum_ns
);
1692 rx_ticks
= irq_mod_ticks(rx_usecs
, efx
->timer_quantum_ns
);
1694 if (tx_ticks
!= rx_ticks
&& efx
->tx_channel_offset
== 0 &&
1695 !rx_may_override_tx
) {
1696 netif_err(efx
, drv
, efx
->net_dev
, "Channels are shared. "
1697 "RX and TX IRQ moderation must be equal\n");
1701 efx
->irq_rx_adaptive
= rx_adaptive
;
1702 efx
->irq_rx_moderation
= rx_ticks
;
1703 efx_for_each_channel(channel
, efx
) {
1704 if (efx_channel_has_rx_queue(channel
))
1705 channel
->irq_moderation
= rx_ticks
;
1706 else if (efx_channel_has_tx_queues(channel
))
1707 channel
->irq_moderation
= tx_ticks
;
1713 void efx_get_irq_moderation(struct efx_nic
*efx
, unsigned int *tx_usecs
,
1714 unsigned int *rx_usecs
, bool *rx_adaptive
)
1716 /* We must round up when converting ticks to microseconds
1717 * because we round down when converting the other way.
1720 *rx_adaptive
= efx
->irq_rx_adaptive
;
1721 *rx_usecs
= DIV_ROUND_UP(efx
->irq_rx_moderation
*
1722 efx
->timer_quantum_ns
,
1725 /* If channels are shared between RX and TX, so is IRQ
1726 * moderation. Otherwise, IRQ moderation is the same for all
1727 * TX channels and is not adaptive.
1729 if (efx
->tx_channel_offset
== 0)
1730 *tx_usecs
= *rx_usecs
;
1732 *tx_usecs
= DIV_ROUND_UP(
1733 efx
->channel
[efx
->tx_channel_offset
]->irq_moderation
*
1734 efx
->timer_quantum_ns
,
1738 /**************************************************************************
1742 **************************************************************************/
1744 /* Run periodically off the general workqueue */
1745 static void efx_monitor(struct work_struct
*data
)
1747 struct efx_nic
*efx
= container_of(data
, struct efx_nic
,
1750 netif_vdbg(efx
, timer
, efx
->net_dev
,
1751 "hardware monitor executing on CPU %d\n",
1752 raw_smp_processor_id());
1753 BUG_ON(efx
->type
->monitor
== NULL
);
1755 /* If the mac_lock is already held then it is likely a port
1756 * reconfiguration is already in place, which will likely do
1757 * most of the work of monitor() anyway. */
1758 if (mutex_trylock(&efx
->mac_lock
)) {
1759 if (efx
->port_enabled
)
1760 efx
->type
->monitor(efx
);
1761 mutex_unlock(&efx
->mac_lock
);
1764 queue_delayed_work(efx
->workqueue
, &efx
->monitor_work
,
1765 efx_monitor_interval
);
1768 /**************************************************************************
1772 *************************************************************************/
1775 * Context: process, rtnl_lock() held.
1777 static int efx_ioctl(struct net_device
*net_dev
, struct ifreq
*ifr
, int cmd
)
1779 struct efx_nic
*efx
= netdev_priv(net_dev
);
1780 struct mii_ioctl_data
*data
= if_mii(ifr
);
1782 if (cmd
== SIOCSHWTSTAMP
)
1783 return efx_ptp_ioctl(efx
, ifr
, cmd
);
1785 /* Convert phy_id from older PRTAD/DEVAD format */
1786 if ((cmd
== SIOCGMIIREG
|| cmd
== SIOCSMIIREG
) &&
1787 (data
->phy_id
& 0xfc00) == 0x0400)
1788 data
->phy_id
^= MDIO_PHY_ID_C45
| 0x0400;
1790 return mdio_mii_ioctl(&efx
->mdio
, data
, cmd
);
1793 /**************************************************************************
1797 **************************************************************************/
1799 static void efx_init_napi_channel(struct efx_channel
*channel
)
1801 struct efx_nic
*efx
= channel
->efx
;
1803 channel
->napi_dev
= efx
->net_dev
;
1804 netif_napi_add(channel
->napi_dev
, &channel
->napi_str
,
1805 efx_poll
, napi_weight
);
1808 static void efx_init_napi(struct efx_nic
*efx
)
1810 struct efx_channel
*channel
;
1812 efx_for_each_channel(channel
, efx
)
1813 efx_init_napi_channel(channel
);
1816 static void efx_fini_napi_channel(struct efx_channel
*channel
)
1818 if (channel
->napi_dev
)
1819 netif_napi_del(&channel
->napi_str
);
1820 channel
->napi_dev
= NULL
;
1823 static void efx_fini_napi(struct efx_nic
*efx
)
1825 struct efx_channel
*channel
;
1827 efx_for_each_channel(channel
, efx
)
1828 efx_fini_napi_channel(channel
);
1831 /**************************************************************************
1833 * Kernel netpoll interface
1835 *************************************************************************/
1837 #ifdef CONFIG_NET_POLL_CONTROLLER
1839 /* Although in the common case interrupts will be disabled, this is not
1840 * guaranteed. However, all our work happens inside the NAPI callback,
1841 * so no locking is required.
1843 static void efx_netpoll(struct net_device
*net_dev
)
1845 struct efx_nic
*efx
= netdev_priv(net_dev
);
1846 struct efx_channel
*channel
;
1848 efx_for_each_channel(channel
, efx
)
1849 efx_schedule_channel(channel
);
1854 /**************************************************************************
1856 * Kernel net device interface
1858 *************************************************************************/
1860 /* Context: process, rtnl_lock() held. */
1861 static int efx_net_open(struct net_device
*net_dev
)
1863 struct efx_nic
*efx
= netdev_priv(net_dev
);
1866 netif_dbg(efx
, ifup
, efx
->net_dev
, "opening device on CPU %d\n",
1867 raw_smp_processor_id());
1869 rc
= efx_check_disabled(efx
);
1872 if (efx
->phy_mode
& PHY_MODE_SPECIAL
)
1874 if (efx_mcdi_poll_reboot(efx
) && efx_reset(efx
, RESET_TYPE_ALL
))
1877 /* Notify the kernel of the link state polled during driver load,
1878 * before the monitor starts running */
1879 efx_link_status_changed(efx
);
1882 efx_selftest_async_start(efx
);
1886 /* Context: process, rtnl_lock() held.
1887 * Note that the kernel will ignore our return code; this method
1888 * should really be a void.
1890 static int efx_net_stop(struct net_device
*net_dev
)
1892 struct efx_nic
*efx
= netdev_priv(net_dev
);
1894 netif_dbg(efx
, ifdown
, efx
->net_dev
, "closing on CPU %d\n",
1895 raw_smp_processor_id());
1897 /* Stop the device and flush all the channels */
1903 /* Context: process, dev_base_lock or RTNL held, non-blocking. */
1904 static struct rtnl_link_stats64
*efx_net_stats(struct net_device
*net_dev
,
1905 struct rtnl_link_stats64
*stats
)
1907 struct efx_nic
*efx
= netdev_priv(net_dev
);
1908 struct efx_mac_stats
*mac_stats
= &efx
->mac_stats
;
1910 spin_lock_bh(&efx
->stats_lock
);
1912 efx
->type
->update_stats(efx
);
1914 stats
->rx_packets
= mac_stats
->rx_packets
;
1915 stats
->tx_packets
= mac_stats
->tx_packets
;
1916 stats
->rx_bytes
= mac_stats
->rx_bytes
;
1917 stats
->tx_bytes
= mac_stats
->tx_bytes
;
1918 stats
->rx_dropped
= efx
->n_rx_nodesc_drop_cnt
;
1919 stats
->multicast
= mac_stats
->rx_multicast
;
1920 stats
->collisions
= mac_stats
->tx_collision
;
1921 stats
->rx_length_errors
= (mac_stats
->rx_gtjumbo
+
1922 mac_stats
->rx_length_error
);
1923 stats
->rx_crc_errors
= mac_stats
->rx_bad
;
1924 stats
->rx_frame_errors
= mac_stats
->rx_align_error
;
1925 stats
->rx_fifo_errors
= mac_stats
->rx_overflow
;
1926 stats
->rx_missed_errors
= mac_stats
->rx_missed
;
1927 stats
->tx_window_errors
= mac_stats
->tx_late_collision
;
1929 stats
->rx_errors
= (stats
->rx_length_errors
+
1930 stats
->rx_crc_errors
+
1931 stats
->rx_frame_errors
+
1932 mac_stats
->rx_symbol_error
);
1933 stats
->tx_errors
= (stats
->tx_window_errors
+
1936 spin_unlock_bh(&efx
->stats_lock
);
1941 /* Context: netif_tx_lock held, BHs disabled. */
1942 static void efx_watchdog(struct net_device
*net_dev
)
1944 struct efx_nic
*efx
= netdev_priv(net_dev
);
1946 netif_err(efx
, tx_err
, efx
->net_dev
,
1947 "TX stuck with port_enabled=%d: resetting channels\n",
1950 efx_schedule_reset(efx
, RESET_TYPE_TX_WATCHDOG
);
1954 /* Context: process, rtnl_lock() held. */
1955 static int efx_change_mtu(struct net_device
*net_dev
, int new_mtu
)
1957 struct efx_nic
*efx
= netdev_priv(net_dev
);
1960 rc
= efx_check_disabled(efx
);
1963 if (new_mtu
> EFX_MAX_MTU
)
1968 netif_dbg(efx
, drv
, efx
->net_dev
, "changing MTU to %d\n", new_mtu
);
1970 mutex_lock(&efx
->mac_lock
);
1971 net_dev
->mtu
= new_mtu
;
1972 efx
->type
->reconfigure_mac(efx
);
1973 mutex_unlock(&efx
->mac_lock
);
1979 static int efx_set_mac_address(struct net_device
*net_dev
, void *data
)
1981 struct efx_nic
*efx
= netdev_priv(net_dev
);
1982 struct sockaddr
*addr
= data
;
1983 char *new_addr
= addr
->sa_data
;
1985 if (!is_valid_ether_addr(new_addr
)) {
1986 netif_err(efx
, drv
, efx
->net_dev
,
1987 "invalid ethernet MAC address requested: %pM\n",
1989 return -EADDRNOTAVAIL
;
1992 memcpy(net_dev
->dev_addr
, new_addr
, net_dev
->addr_len
);
1993 efx_sriov_mac_address_changed(efx
);
1995 /* Reconfigure the MAC */
1996 mutex_lock(&efx
->mac_lock
);
1997 efx
->type
->reconfigure_mac(efx
);
1998 mutex_unlock(&efx
->mac_lock
);
2003 /* Context: netif_addr_lock held, BHs disabled. */
2004 static void efx_set_rx_mode(struct net_device
*net_dev
)
2006 struct efx_nic
*efx
= netdev_priv(net_dev
);
2007 struct netdev_hw_addr
*ha
;
2008 union efx_multicast_hash
*mc_hash
= &efx
->multicast_hash
;
2012 efx
->promiscuous
= !!(net_dev
->flags
& IFF_PROMISC
);
2014 /* Build multicast hash table */
2015 if (efx
->promiscuous
|| (net_dev
->flags
& IFF_ALLMULTI
)) {
2016 memset(mc_hash
, 0xff, sizeof(*mc_hash
));
2018 memset(mc_hash
, 0x00, sizeof(*mc_hash
));
2019 netdev_for_each_mc_addr(ha
, net_dev
) {
2020 crc
= ether_crc_le(ETH_ALEN
, ha
->addr
);
2021 bit
= crc
& (EFX_MCAST_HASH_ENTRIES
- 1);
2022 __set_bit_le(bit
, mc_hash
);
2025 /* Broadcast packets go through the multicast hash filter.
2026 * ether_crc_le() of the broadcast address is 0xbe2612ff
2027 * so we always add bit 0xff to the mask.
2029 __set_bit_le(0xff, mc_hash
);
2032 if (efx
->port_enabled
)
2033 queue_work(efx
->workqueue
, &efx
->mac_work
);
2034 /* Otherwise efx_start_port() will do this */
2037 static int efx_set_features(struct net_device
*net_dev
, netdev_features_t data
)
2039 struct efx_nic
*efx
= netdev_priv(net_dev
);
2041 /* If disabling RX n-tuple filtering, clear existing filters */
2042 if (net_dev
->features
& ~data
& NETIF_F_NTUPLE
)
2043 efx_filter_clear_rx(efx
, EFX_FILTER_PRI_MANUAL
);
2048 static const struct net_device_ops efx_netdev_ops
= {
2049 .ndo_open
= efx_net_open
,
2050 .ndo_stop
= efx_net_stop
,
2051 .ndo_get_stats64
= efx_net_stats
,
2052 .ndo_tx_timeout
= efx_watchdog
,
2053 .ndo_start_xmit
= efx_hard_start_xmit
,
2054 .ndo_validate_addr
= eth_validate_addr
,
2055 .ndo_do_ioctl
= efx_ioctl
,
2056 .ndo_change_mtu
= efx_change_mtu
,
2057 .ndo_set_mac_address
= efx_set_mac_address
,
2058 .ndo_set_rx_mode
= efx_set_rx_mode
,
2059 .ndo_set_features
= efx_set_features
,
2060 #ifdef CONFIG_SFC_SRIOV
2061 .ndo_set_vf_mac
= efx_sriov_set_vf_mac
,
2062 .ndo_set_vf_vlan
= efx_sriov_set_vf_vlan
,
2063 .ndo_set_vf_spoofchk
= efx_sriov_set_vf_spoofchk
,
2064 .ndo_get_vf_config
= efx_sriov_get_vf_config
,
2066 #ifdef CONFIG_NET_POLL_CONTROLLER
2067 .ndo_poll_controller
= efx_netpoll
,
2069 .ndo_setup_tc
= efx_setup_tc
,
2070 #ifdef CONFIG_RFS_ACCEL
2071 .ndo_rx_flow_steer
= efx_filter_rfs
,
2075 static void efx_update_name(struct efx_nic
*efx
)
2077 strcpy(efx
->name
, efx
->net_dev
->name
);
2078 efx_mtd_rename(efx
);
2079 efx_set_channel_names(efx
);
2082 static int efx_netdev_event(struct notifier_block
*this,
2083 unsigned long event
, void *ptr
)
2085 struct net_device
*net_dev
= ptr
;
2087 if (net_dev
->netdev_ops
== &efx_netdev_ops
&&
2088 event
== NETDEV_CHANGENAME
)
2089 efx_update_name(netdev_priv(net_dev
));
2094 static struct notifier_block efx_netdev_notifier
= {
2095 .notifier_call
= efx_netdev_event
,
2099 show_phy_type(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
2101 struct efx_nic
*efx
= pci_get_drvdata(to_pci_dev(dev
));
2102 return sprintf(buf
, "%d\n", efx
->phy_type
);
2104 static DEVICE_ATTR(phy_type
, 0644, show_phy_type
, NULL
);
2106 static int efx_register_netdev(struct efx_nic
*efx
)
2108 struct net_device
*net_dev
= efx
->net_dev
;
2109 struct efx_channel
*channel
;
2112 net_dev
->watchdog_timeo
= 5 * HZ
;
2113 net_dev
->irq
= efx
->pci_dev
->irq
;
2114 net_dev
->netdev_ops
= &efx_netdev_ops
;
2115 SET_ETHTOOL_OPS(net_dev
, &efx_ethtool_ops
);
2116 net_dev
->gso_max_segs
= EFX_TSO_MAX_SEGS
;
2120 /* Enable resets to be scheduled and check whether any were
2121 * already requested. If so, the NIC is probably hosed so we
2124 efx
->state
= STATE_READY
;
2125 smp_mb(); /* ensure we change state before checking reset_pending */
2126 if (efx
->reset_pending
) {
2127 netif_err(efx
, probe
, efx
->net_dev
,
2128 "aborting probe due to scheduled reset\n");
2133 rc
= dev_alloc_name(net_dev
, net_dev
->name
);
2136 efx_update_name(efx
);
2138 /* Always start with carrier off; PHY events will detect the link */
2139 netif_carrier_off(net_dev
);
2141 rc
= register_netdevice(net_dev
);
2145 efx_for_each_channel(channel
, efx
) {
2146 struct efx_tx_queue
*tx_queue
;
2147 efx_for_each_channel_tx_queue(tx_queue
, channel
)
2148 efx_init_tx_queue_core_txq(tx_queue
);
2153 rc
= device_create_file(&efx
->pci_dev
->dev
, &dev_attr_phy_type
);
2155 netif_err(efx
, drv
, efx
->net_dev
,
2156 "failed to init net dev attributes\n");
2157 goto fail_registered
;
2164 unregister_netdevice(net_dev
);
2166 efx
->state
= STATE_UNINIT
;
2168 netif_err(efx
, drv
, efx
->net_dev
, "could not register net dev\n");
2172 static void efx_unregister_netdev(struct efx_nic
*efx
)
2174 struct efx_channel
*channel
;
2175 struct efx_tx_queue
*tx_queue
;
2180 BUG_ON(netdev_priv(efx
->net_dev
) != efx
);
2182 /* Free up any skbs still remaining. This has to happen before
2183 * we try to unregister the netdev as running their destructors
2184 * may be needed to get the device ref. count to 0. */
2185 efx_for_each_channel(channel
, efx
) {
2186 efx_for_each_channel_tx_queue(tx_queue
, channel
)
2187 efx_release_tx_buffers(tx_queue
);
2190 strlcpy(efx
->name
, pci_name(efx
->pci_dev
), sizeof(efx
->name
));
2191 device_remove_file(&efx
->pci_dev
->dev
, &dev_attr_phy_type
);
2194 unregister_netdevice(efx
->net_dev
);
2195 efx
->state
= STATE_UNINIT
;
2199 /**************************************************************************
2201 * Device reset and suspend
2203 **************************************************************************/
2205 /* Tears down the entire software state and most of the hardware state
2207 void efx_reset_down(struct efx_nic
*efx
, enum reset_type method
)
2209 EFX_ASSERT_RESET_SERIALISED(efx
);
2212 efx_stop_interrupts(efx
, false);
2214 mutex_lock(&efx
->mac_lock
);
2215 if (efx
->port_initialized
&& method
!= RESET_TYPE_INVISIBLE
)
2216 efx
->phy_op
->fini(efx
);
2217 efx
->type
->fini(efx
);
2220 /* This function will always ensure that the locks acquired in
2221 * efx_reset_down() are released. A failure return code indicates
2222 * that we were unable to reinitialise the hardware, and the
2223 * driver should be disabled. If ok is false, then the rx and tx
2224 * engines are not restarted, pending a RESET_DISABLE. */
2225 int efx_reset_up(struct efx_nic
*efx
, enum reset_type method
, bool ok
)
2229 EFX_ASSERT_RESET_SERIALISED(efx
);
2231 rc
= efx
->type
->init(efx
);
2233 netif_err(efx
, drv
, efx
->net_dev
, "failed to initialise NIC\n");
2240 if (efx
->port_initialized
&& method
!= RESET_TYPE_INVISIBLE
) {
2241 rc
= efx
->phy_op
->init(efx
);
2244 if (efx
->phy_op
->reconfigure(efx
))
2245 netif_err(efx
, drv
, efx
->net_dev
,
2246 "could not restore PHY settings\n");
2249 efx
->type
->reconfigure_mac(efx
);
2251 efx_start_interrupts(efx
, false);
2252 efx_restore_filters(efx
);
2253 efx_sriov_reset(efx
);
2255 mutex_unlock(&efx
->mac_lock
);
2262 efx
->port_initialized
= false;
2264 mutex_unlock(&efx
->mac_lock
);
2269 /* Reset the NIC using the specified method. Note that the reset may
2270 * fail, in which case the card will be left in an unusable state.
2272 * Caller must hold the rtnl_lock.
2274 int efx_reset(struct efx_nic
*efx
, enum reset_type method
)
2279 netif_info(efx
, drv
, efx
->net_dev
, "resetting (%s)\n",
2280 RESET_TYPE(method
));
2282 netif_device_detach(efx
->net_dev
);
2283 efx_reset_down(efx
, method
);
2285 rc
= efx
->type
->reset(efx
, method
);
2287 netif_err(efx
, drv
, efx
->net_dev
, "failed to reset hardware\n");
2291 /* Clear flags for the scopes we covered. We assume the NIC and
2292 * driver are now quiescent so that there is no race here.
2294 efx
->reset_pending
&= -(1 << (method
+ 1));
2296 /* Reinitialise bus-mastering, which may have been turned off before
2297 * the reset was scheduled. This is still appropriate, even in the
2298 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2299 * can respond to requests. */
2300 pci_set_master(efx
->pci_dev
);
2303 /* Leave device stopped if necessary */
2304 disabled
= rc
|| method
== RESET_TYPE_DISABLE
;
2305 rc2
= efx_reset_up(efx
, method
, !disabled
);
2313 dev_close(efx
->net_dev
);
2314 netif_err(efx
, drv
, efx
->net_dev
, "has been disabled\n");
2315 efx
->state
= STATE_DISABLED
;
2317 netif_dbg(efx
, drv
, efx
->net_dev
, "reset complete\n");
2318 netif_device_attach(efx
->net_dev
);
2323 /* The worker thread exists so that code that cannot sleep can
2324 * schedule a reset for later.
2326 static void efx_reset_work(struct work_struct
*data
)
2328 struct efx_nic
*efx
= container_of(data
, struct efx_nic
, reset_work
);
2329 unsigned long pending
= ACCESS_ONCE(efx
->reset_pending
);
2336 /* We checked the state in efx_schedule_reset() but it may
2337 * have changed by now. Now that we have the RTNL lock,
2338 * it cannot change again.
2340 if (efx
->state
== STATE_READY
)
2341 (void)efx_reset(efx
, fls(pending
) - 1);
2346 void efx_schedule_reset(struct efx_nic
*efx
, enum reset_type type
)
2348 enum reset_type method
;
2351 case RESET_TYPE_INVISIBLE
:
2352 case RESET_TYPE_ALL
:
2353 case RESET_TYPE_WORLD
:
2354 case RESET_TYPE_DISABLE
:
2356 netif_dbg(efx
, drv
, efx
->net_dev
, "scheduling %s reset\n",
2357 RESET_TYPE(method
));
2360 method
= efx
->type
->map_reset_reason(type
);
2361 netif_dbg(efx
, drv
, efx
->net_dev
,
2362 "scheduling %s reset for %s\n",
2363 RESET_TYPE(method
), RESET_TYPE(type
));
2367 set_bit(method
, &efx
->reset_pending
);
2368 smp_mb(); /* ensure we change reset_pending before checking state */
2370 /* If we're not READY then just leave the flags set as the cue
2371 * to abort probing or reschedule the reset later.
2373 if (ACCESS_ONCE(efx
->state
) != STATE_READY
)
2376 /* efx_process_channel() will no longer read events once a
2377 * reset is scheduled. So switch back to poll'd MCDI completions. */
2378 efx_mcdi_mode_poll(efx
);
2380 queue_work(reset_workqueue
, &efx
->reset_work
);
2383 /**************************************************************************
2385 * List of NICs we support
2387 **************************************************************************/
2389 /* PCI device ID table */
2390 static DEFINE_PCI_DEVICE_TABLE(efx_pci_table
) = {
2391 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE
,
2392 PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0
),
2393 .driver_data
= (unsigned long) &falcon_a1_nic_type
},
2394 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE
,
2395 PCI_DEVICE_ID_SOLARFLARE_SFC4000B
),
2396 .driver_data
= (unsigned long) &falcon_b0_nic_type
},
2397 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE
, 0x0803), /* SFC9020 */
2398 .driver_data
= (unsigned long) &siena_a0_nic_type
},
2399 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE
, 0x0813), /* SFL9021 */
2400 .driver_data
= (unsigned long) &siena_a0_nic_type
},
2401 {0} /* end of list */
2404 /**************************************************************************
2406 * Dummy PHY/MAC operations
2408 * Can be used for some unimplemented operations
2409 * Needed so all function pointers are valid and do not have to be tested
2412 **************************************************************************/
2413 int efx_port_dummy_op_int(struct efx_nic
*efx
)
2417 void efx_port_dummy_op_void(struct efx_nic
*efx
) {}
2419 static bool efx_port_dummy_op_poll(struct efx_nic
*efx
)
2424 static const struct efx_phy_operations efx_dummy_phy_operations
= {
2425 .init
= efx_port_dummy_op_int
,
2426 .reconfigure
= efx_port_dummy_op_int
,
2427 .poll
= efx_port_dummy_op_poll
,
2428 .fini
= efx_port_dummy_op_void
,
2431 /**************************************************************************
2435 **************************************************************************/
2437 /* This zeroes out and then fills in the invariants in a struct
2438 * efx_nic (including all sub-structures).
2440 static int efx_init_struct(struct efx_nic
*efx
,
2441 struct pci_dev
*pci_dev
, struct net_device
*net_dev
)
2445 /* Initialise common structures */
2446 spin_lock_init(&efx
->biu_lock
);
2447 #ifdef CONFIG_SFC_MTD
2448 INIT_LIST_HEAD(&efx
->mtd_list
);
2450 INIT_WORK(&efx
->reset_work
, efx_reset_work
);
2451 INIT_DELAYED_WORK(&efx
->monitor_work
, efx_monitor
);
2452 INIT_DELAYED_WORK(&efx
->selftest_work
, efx_selftest_async_work
);
2453 efx
->pci_dev
= pci_dev
;
2454 efx
->msg_enable
= debug
;
2455 efx
->state
= STATE_UNINIT
;
2456 strlcpy(efx
->name
, pci_name(pci_dev
), sizeof(efx
->name
));
2458 efx
->net_dev
= net_dev
;
2459 spin_lock_init(&efx
->stats_lock
);
2460 mutex_init(&efx
->mac_lock
);
2461 efx
->phy_op
= &efx_dummy_phy_operations
;
2462 efx
->mdio
.dev
= net_dev
;
2463 INIT_WORK(&efx
->mac_work
, efx_mac_work
);
2464 init_waitqueue_head(&efx
->flush_wq
);
2466 for (i
= 0; i
< EFX_MAX_CHANNELS
; i
++) {
2467 efx
->channel
[i
] = efx_alloc_channel(efx
, i
, NULL
);
2468 if (!efx
->channel
[i
])
2472 EFX_BUG_ON_PARANOID(efx
->type
->phys_addr_channels
> EFX_MAX_CHANNELS
);
2474 /* Higher numbered interrupt modes are less capable! */
2475 efx
->interrupt_mode
= max(efx
->type
->max_interrupt_mode
,
2478 /* Would be good to use the net_dev name, but we're too early */
2479 snprintf(efx
->workqueue_name
, sizeof(efx
->workqueue_name
), "sfc%s",
2481 efx
->workqueue
= create_singlethread_workqueue(efx
->workqueue_name
);
2482 if (!efx
->workqueue
)
2488 efx_fini_struct(efx
);
2492 static void efx_fini_struct(struct efx_nic
*efx
)
2496 for (i
= 0; i
< EFX_MAX_CHANNELS
; i
++)
2497 kfree(efx
->channel
[i
]);
2499 if (efx
->workqueue
) {
2500 destroy_workqueue(efx
->workqueue
);
2501 efx
->workqueue
= NULL
;
2505 /**************************************************************************
2509 **************************************************************************/
2511 /* Main body of final NIC shutdown code
2512 * This is called only at module unload (or hotplug removal).
2514 static void efx_pci_remove_main(struct efx_nic
*efx
)
2516 /* Flush reset_work. It can no longer be scheduled since we
2519 BUG_ON(efx
->state
== STATE_READY
);
2520 cancel_work_sync(&efx
->reset_work
);
2522 #ifdef CONFIG_RFS_ACCEL
2523 free_irq_cpu_rmap(efx
->net_dev
->rx_cpu_rmap
);
2524 efx
->net_dev
->rx_cpu_rmap
= NULL
;
2526 efx_stop_interrupts(efx
, false);
2527 efx_nic_fini_interrupt(efx
);
2529 efx
->type
->fini(efx
);
2531 efx_remove_all(efx
);
2534 /* Final NIC shutdown
2535 * This is called only at module unload (or hotplug removal).
2537 static void efx_pci_remove(struct pci_dev
*pci_dev
)
2539 struct efx_nic
*efx
;
2541 efx
= pci_get_drvdata(pci_dev
);
2545 /* Mark the NIC as fini, then stop the interface */
2547 dev_close(efx
->net_dev
);
2548 efx_stop_interrupts(efx
, false);
2551 efx_sriov_fini(efx
);
2552 efx_unregister_netdev(efx
);
2554 efx_mtd_remove(efx
);
2556 efx_pci_remove_main(efx
);
2559 netif_dbg(efx
, drv
, efx
->net_dev
, "shutdown successful\n");
2561 efx_fini_struct(efx
);
2562 pci_set_drvdata(pci_dev
, NULL
);
2563 free_netdev(efx
->net_dev
);
2566 /* NIC VPD information
2567 * Called during probe to display the part number of the
2568 * installed NIC. VPD is potentially very large but this should
2569 * always appear within the first 512 bytes.
2571 #define SFC_VPD_LEN 512
2572 static void efx_print_product_vpd(struct efx_nic
*efx
)
2574 struct pci_dev
*dev
= efx
->pci_dev
;
2575 char vpd_data
[SFC_VPD_LEN
];
2579 /* Get the vpd data from the device */
2580 vpd_size
= pci_read_vpd(dev
, 0, sizeof(vpd_data
), vpd_data
);
2581 if (vpd_size
<= 0) {
2582 netif_err(efx
, drv
, efx
->net_dev
, "Unable to read VPD\n");
2586 /* Get the Read only section */
2587 i
= pci_vpd_find_tag(vpd_data
, 0, vpd_size
, PCI_VPD_LRDT_RO_DATA
);
2589 netif_err(efx
, drv
, efx
->net_dev
, "VPD Read-only not found\n");
2593 j
= pci_vpd_lrdt_size(&vpd_data
[i
]);
2594 i
+= PCI_VPD_LRDT_TAG_SIZE
;
2595 if (i
+ j
> vpd_size
)
2598 /* Get the Part number */
2599 i
= pci_vpd_find_info_keyword(vpd_data
, i
, j
, "PN");
2601 netif_err(efx
, drv
, efx
->net_dev
, "Part number not found\n");
2605 j
= pci_vpd_info_field_size(&vpd_data
[i
]);
2606 i
+= PCI_VPD_INFO_FLD_HDR_SIZE
;
2607 if (i
+ j
> vpd_size
) {
2608 netif_err(efx
, drv
, efx
->net_dev
, "Incomplete part number\n");
2612 netif_info(efx
, drv
, efx
->net_dev
,
2613 "Part Number : %.*s\n", j
, &vpd_data
[i
]);
2617 /* Main body of NIC initialisation
2618 * This is called at module load (or hotplug insertion, theoretically).
2620 static int efx_pci_probe_main(struct efx_nic
*efx
)
2624 /* Do start-of-day initialisation */
2625 rc
= efx_probe_all(efx
);
2631 rc
= efx
->type
->init(efx
);
2633 netif_err(efx
, probe
, efx
->net_dev
,
2634 "failed to initialise NIC\n");
2638 rc
= efx_init_port(efx
);
2640 netif_err(efx
, probe
, efx
->net_dev
,
2641 "failed to initialise port\n");
2645 rc
= efx_nic_init_interrupt(efx
);
2648 efx_start_interrupts(efx
, false);
2655 efx
->type
->fini(efx
);
2658 efx_remove_all(efx
);
2663 /* NIC initialisation
2665 * This is called at module load (or hotplug insertion,
2666 * theoretically). It sets up PCI mappings, resets the NIC,
2667 * sets up and registers the network devices with the kernel and hooks
2668 * the interrupt service routine. It does not prepare the device for
2669 * transmission; this is left to the first time one of the network
2670 * interfaces is brought up (i.e. efx_net_open).
2672 static int __devinit
efx_pci_probe(struct pci_dev
*pci_dev
,
2673 const struct pci_device_id
*entry
)
2675 struct net_device
*net_dev
;
2676 struct efx_nic
*efx
;
2679 /* Allocate and initialise a struct net_device and struct efx_nic */
2680 net_dev
= alloc_etherdev_mqs(sizeof(*efx
), EFX_MAX_CORE_TX_QUEUES
,
2684 efx
= netdev_priv(net_dev
);
2685 efx
->type
= (const struct efx_nic_type
*) entry
->driver_data
;
2686 net_dev
->features
|= (efx
->type
->offload_features
| NETIF_F_SG
|
2687 NETIF_F_HIGHDMA
| NETIF_F_TSO
|
2689 if (efx
->type
->offload_features
& NETIF_F_V6_CSUM
)
2690 net_dev
->features
|= NETIF_F_TSO6
;
2691 /* Mask for features that also apply to VLAN devices */
2692 net_dev
->vlan_features
|= (NETIF_F_ALL_CSUM
| NETIF_F_SG
|
2693 NETIF_F_HIGHDMA
| NETIF_F_ALL_TSO
|
2695 /* All offloads can be toggled */
2696 net_dev
->hw_features
= net_dev
->features
& ~NETIF_F_HIGHDMA
;
2697 pci_set_drvdata(pci_dev
, efx
);
2698 SET_NETDEV_DEV(net_dev
, &pci_dev
->dev
);
2699 rc
= efx_init_struct(efx
, pci_dev
, net_dev
);
2703 netif_info(efx
, probe
, efx
->net_dev
,
2704 "Solarflare NIC detected\n");
2706 efx_print_product_vpd(efx
);
2708 /* Set up basic I/O (BAR mappings etc) */
2709 rc
= efx_init_io(efx
);
2713 rc
= efx_pci_probe_main(efx
);
2717 rc
= efx_register_netdev(efx
);
2721 rc
= efx_sriov_init(efx
);
2723 netif_err(efx
, probe
, efx
->net_dev
,
2724 "SR-IOV can't be enabled rc %d\n", rc
);
2726 netif_dbg(efx
, probe
, efx
->net_dev
, "initialisation successful\n");
2728 /* Try to create MTDs, but allow this to fail */
2730 rc
= efx_mtd_probe(efx
);
2733 netif_warn(efx
, probe
, efx
->net_dev
,
2734 "failed to create MTDs (%d)\n", rc
);
2739 efx_pci_remove_main(efx
);
2743 efx_fini_struct(efx
);
2745 pci_set_drvdata(pci_dev
, NULL
);
2747 netif_dbg(efx
, drv
, efx
->net_dev
, "initialisation failed. rc=%d\n", rc
);
2748 free_netdev(net_dev
);
2752 static int efx_pm_freeze(struct device
*dev
)
2754 struct efx_nic
*efx
= pci_get_drvdata(to_pci_dev(dev
));
2758 if (efx
->state
!= STATE_DISABLED
) {
2759 efx
->state
= STATE_UNINIT
;
2761 netif_device_detach(efx
->net_dev
);
2764 efx_stop_interrupts(efx
, false);
2772 static int efx_pm_thaw(struct device
*dev
)
2774 struct efx_nic
*efx
= pci_get_drvdata(to_pci_dev(dev
));
2778 if (efx
->state
!= STATE_DISABLED
) {
2779 efx_start_interrupts(efx
, false);
2781 mutex_lock(&efx
->mac_lock
);
2782 efx
->phy_op
->reconfigure(efx
);
2783 mutex_unlock(&efx
->mac_lock
);
2787 netif_device_attach(efx
->net_dev
);
2789 efx
->state
= STATE_READY
;
2791 efx
->type
->resume_wol(efx
);
2796 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
2797 queue_work(reset_workqueue
, &efx
->reset_work
);
2802 static int efx_pm_poweroff(struct device
*dev
)
2804 struct pci_dev
*pci_dev
= to_pci_dev(dev
);
2805 struct efx_nic
*efx
= pci_get_drvdata(pci_dev
);
2807 efx
->type
->fini(efx
);
2809 efx
->reset_pending
= 0;
2811 pci_save_state(pci_dev
);
2812 return pci_set_power_state(pci_dev
, PCI_D3hot
);
2815 /* Used for both resume and restore */
2816 static int efx_pm_resume(struct device
*dev
)
2818 struct pci_dev
*pci_dev
= to_pci_dev(dev
);
2819 struct efx_nic
*efx
= pci_get_drvdata(pci_dev
);
2822 rc
= pci_set_power_state(pci_dev
, PCI_D0
);
2825 pci_restore_state(pci_dev
);
2826 rc
= pci_enable_device(pci_dev
);
2829 pci_set_master(efx
->pci_dev
);
2830 rc
= efx
->type
->reset(efx
, RESET_TYPE_ALL
);
2833 rc
= efx
->type
->init(efx
);
2840 static int efx_pm_suspend(struct device
*dev
)
2845 rc
= efx_pm_poweroff(dev
);
2851 static const struct dev_pm_ops efx_pm_ops
= {
2852 .suspend
= efx_pm_suspend
,
2853 .resume
= efx_pm_resume
,
2854 .freeze
= efx_pm_freeze
,
2855 .thaw
= efx_pm_thaw
,
2856 .poweroff
= efx_pm_poweroff
,
2857 .restore
= efx_pm_resume
,
2860 static struct pci_driver efx_pci_driver
= {
2861 .name
= KBUILD_MODNAME
,
2862 .id_table
= efx_pci_table
,
2863 .probe
= efx_pci_probe
,
2864 .remove
= efx_pci_remove
,
2865 .driver
.pm
= &efx_pm_ops
,
2868 /**************************************************************************
2870 * Kernel module interface
2872 *************************************************************************/
2874 module_param(interrupt_mode
, uint
, 0444);
2875 MODULE_PARM_DESC(interrupt_mode
,
2876 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
2878 static int __init
efx_init_module(void)
2882 printk(KERN_INFO
"Solarflare NET driver v" EFX_DRIVER_VERSION
"\n");
2884 rc
= register_netdevice_notifier(&efx_netdev_notifier
);
2888 rc
= efx_init_sriov();
2892 reset_workqueue
= create_singlethread_workqueue("sfc_reset");
2893 if (!reset_workqueue
) {
2898 rc
= pci_register_driver(&efx_pci_driver
);
2905 destroy_workqueue(reset_workqueue
);
2909 unregister_netdevice_notifier(&efx_netdev_notifier
);
2914 static void __exit
efx_exit_module(void)
2916 printk(KERN_INFO
"Solarflare NET driver unloading\n");
2918 pci_unregister_driver(&efx_pci_driver
);
2919 destroy_workqueue(reset_workqueue
);
2921 unregister_netdevice_notifier(&efx_netdev_notifier
);
2925 module_init(efx_init_module
);
2926 module_exit(efx_exit_module
);
2928 MODULE_AUTHOR("Solarflare Communications and "
2929 "Michael Brown <mbrown@fensystems.co.uk>");
2930 MODULE_DESCRIPTION("Solarflare Communications network driver");
2931 MODULE_LICENSE("GPL");
2932 MODULE_DEVICE_TABLE(pci
, efx_pci_table
);