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