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