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