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