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