Merge tag 'nfs-for-4.18-2' of git://git.linux-nfs.org/projects/trondmy/linux-nfs
[platform/kernel/linux-rpi.git] / drivers / net / ethernet / sfc / falcon / efx.c
1 /****************************************************************************
2  * Driver for Solarflare network controllers and boards
3  * Copyright 2005-2006 Fen Systems Ltd.
4  * Copyright 2005-2013 Solarflare Communications Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 as published
8  * by the Free Software Foundation, incorporated herein by reference.
9  */
10
11 #include <linux/module.h>
12 #include <linux/pci.h>
13 #include <linux/netdevice.h>
14 #include <linux/etherdevice.h>
15 #include <linux/delay.h>
16 #include <linux/notifier.h>
17 #include <linux/ip.h>
18 #include <linux/tcp.h>
19 #include <linux/in.h>
20 #include <linux/ethtool.h>
21 #include <linux/topology.h>
22 #include <linux/gfp.h>
23 #include <linux/aer.h>
24 #include <linux/interrupt.h>
25 #include "net_driver.h"
26 #include "efx.h"
27 #include "nic.h"
28 #include "selftest.h"
29
30 #include "workarounds.h"
31
32 /**************************************************************************
33  *
34  * Type name strings
35  *
36  **************************************************************************
37  */
38
39 /* Loopback mode names (see LOOPBACK_MODE()) */
40 const unsigned int ef4_loopback_mode_max = LOOPBACK_MAX;
41 const char *const ef4_loopback_mode_names[] = {
42         [LOOPBACK_NONE]         = "NONE",
43         [LOOPBACK_DATA]         = "DATAPATH",
44         [LOOPBACK_GMAC]         = "GMAC",
45         [LOOPBACK_XGMII]        = "XGMII",
46         [LOOPBACK_XGXS]         = "XGXS",
47         [LOOPBACK_XAUI]         = "XAUI",
48         [LOOPBACK_GMII]         = "GMII",
49         [LOOPBACK_SGMII]        = "SGMII",
50         [LOOPBACK_XGBR]         = "XGBR",
51         [LOOPBACK_XFI]          = "XFI",
52         [LOOPBACK_XAUI_FAR]     = "XAUI_FAR",
53         [LOOPBACK_GMII_FAR]     = "GMII_FAR",
54         [LOOPBACK_SGMII_FAR]    = "SGMII_FAR",
55         [LOOPBACK_XFI_FAR]      = "XFI_FAR",
56         [LOOPBACK_GPHY]         = "GPHY",
57         [LOOPBACK_PHYXS]        = "PHYXS",
58         [LOOPBACK_PCS]          = "PCS",
59         [LOOPBACK_PMAPMD]       = "PMA/PMD",
60         [LOOPBACK_XPORT]        = "XPORT",
61         [LOOPBACK_XGMII_WS]     = "XGMII_WS",
62         [LOOPBACK_XAUI_WS]      = "XAUI_WS",
63         [LOOPBACK_XAUI_WS_FAR]  = "XAUI_WS_FAR",
64         [LOOPBACK_XAUI_WS_NEAR] = "XAUI_WS_NEAR",
65         [LOOPBACK_GMII_WS]      = "GMII_WS",
66         [LOOPBACK_XFI_WS]       = "XFI_WS",
67         [LOOPBACK_XFI_WS_FAR]   = "XFI_WS_FAR",
68         [LOOPBACK_PHYXS_WS]     = "PHYXS_WS",
69 };
70
71 const unsigned int ef4_reset_type_max = RESET_TYPE_MAX;
72 const char *const ef4_reset_type_names[] = {
73         [RESET_TYPE_INVISIBLE]          = "INVISIBLE",
74         [RESET_TYPE_ALL]                = "ALL",
75         [RESET_TYPE_RECOVER_OR_ALL]     = "RECOVER_OR_ALL",
76         [RESET_TYPE_WORLD]              = "WORLD",
77         [RESET_TYPE_RECOVER_OR_DISABLE] = "RECOVER_OR_DISABLE",
78         [RESET_TYPE_DATAPATH]           = "DATAPATH",
79         [RESET_TYPE_DISABLE]            = "DISABLE",
80         [RESET_TYPE_TX_WATCHDOG]        = "TX_WATCHDOG",
81         [RESET_TYPE_INT_ERROR]          = "INT_ERROR",
82         [RESET_TYPE_RX_RECOVERY]        = "RX_RECOVERY",
83         [RESET_TYPE_DMA_ERROR]          = "DMA_ERROR",
84         [RESET_TYPE_TX_SKIP]            = "TX_SKIP",
85 };
86
87 /* Reset workqueue. If any NIC has a hardware failure then a reset will be
88  * queued onto this work queue. This is not a per-nic work queue, because
89  * ef4_reset_work() acquires the rtnl lock, so resets are naturally serialised.
90  */
91 static struct workqueue_struct *reset_workqueue;
92
93 /* How often and how many times to poll for a reset while waiting for a
94  * BIST that another function started to complete.
95  */
96 #define BIST_WAIT_DELAY_MS      100
97 #define BIST_WAIT_DELAY_COUNT   100
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 bool ef4_separate_tx_channels;
114 module_param(ef4_separate_tx_channels, bool, 0444);
115 MODULE_PARM_DESC(ef4_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 ef4_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 int ef4_soft_enable_interrupts(struct ef4_nic *efx);
198 static void ef4_soft_disable_interrupts(struct ef4_nic *efx);
199 static void ef4_remove_channel(struct ef4_channel *channel);
200 static void ef4_remove_channels(struct ef4_nic *efx);
201 static const struct ef4_channel_type ef4_default_channel_type;
202 static void ef4_remove_port(struct ef4_nic *efx);
203 static void ef4_init_napi_channel(struct ef4_channel *channel);
204 static void ef4_fini_napi(struct ef4_nic *efx);
205 static void ef4_fini_napi_channel(struct ef4_channel *channel);
206 static void ef4_fini_struct(struct ef4_nic *efx);
207 static void ef4_start_all(struct ef4_nic *efx);
208 static void ef4_stop_all(struct ef4_nic *efx);
209
210 #define EF4_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 ef4_check_disabled(struct ef4_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 ef4_process_channel(struct ef4_channel *channel, int budget)
242 {
243         struct ef4_tx_queue *tx_queue;
244         int spent;
245
246         if (unlikely(!channel->enabled))
247                 return 0;
248
249         ef4_for_each_channel_tx_queue(tx_queue, channel) {
250                 tx_queue->pkts_compl = 0;
251                 tx_queue->bytes_compl = 0;
252         }
253
254         spent = ef4_nic_process_eventq(channel, budget);
255         if (spent && ef4_channel_has_rx_queue(channel)) {
256                 struct ef4_rx_queue *rx_queue =
257                         ef4_channel_get_rx_queue(channel);
258
259                 ef4_rx_flush_packet(channel);
260                 ef4_fast_push_rx_descriptors(rx_queue, true);
261         }
262
263         /* Update BQL */
264         ef4_for_each_channel_tx_queue(tx_queue, channel) {
265                 if (tx_queue->bytes_compl) {
266                         netdev_tx_completed_queue(tx_queue->core_txq,
267                                 tx_queue->pkts_compl, tx_queue->bytes_compl);
268                 }
269         }
270
271         return spent;
272 }
273
274 /* NAPI poll handler
275  *
276  * NAPI guarantees serialisation of polls of the same device, which
277  * provides the guarantee required by ef4_process_channel().
278  */
279 static void ef4_update_irq_mod(struct ef4_nic *efx, struct ef4_channel *channel)
280 {
281         int step = efx->irq_mod_step_us;
282
283         if (channel->irq_mod_score < irq_adapt_low_thresh) {
284                 if (channel->irq_moderation_us > step) {
285                         channel->irq_moderation_us -= step;
286                         efx->type->push_irq_moderation(channel);
287                 }
288         } else if (channel->irq_mod_score > irq_adapt_high_thresh) {
289                 if (channel->irq_moderation_us <
290                     efx->irq_rx_moderation_us) {
291                         channel->irq_moderation_us += step;
292                         efx->type->push_irq_moderation(channel);
293                 }
294         }
295
296         channel->irq_count = 0;
297         channel->irq_mod_score = 0;
298 }
299
300 static int ef4_poll(struct napi_struct *napi, int budget)
301 {
302         struct ef4_channel *channel =
303                 container_of(napi, struct ef4_channel, napi_str);
304         struct ef4_nic *efx = channel->efx;
305         int spent;
306
307         netif_vdbg(efx, intr, efx->net_dev,
308                    "channel %d NAPI poll executing on CPU %d\n",
309                    channel->channel, raw_smp_processor_id());
310
311         spent = ef4_process_channel(channel, budget);
312
313         if (spent < budget) {
314                 if (ef4_channel_has_rx_queue(channel) &&
315                     efx->irq_rx_adaptive &&
316                     unlikely(++channel->irq_count == 1000)) {
317                         ef4_update_irq_mod(efx, channel);
318                 }
319
320                 ef4_filter_rfs_expire(channel);
321
322                 /* There is no race here; although napi_disable() will
323                  * only wait for napi_complete(), this isn't a problem
324                  * since ef4_nic_eventq_read_ack() will have no effect if
325                  * interrupts have already been disabled.
326                  */
327                 napi_complete_done(napi, spent);
328                 ef4_nic_eventq_read_ack(channel);
329         }
330
331         return spent;
332 }
333
334 /* Create event queue
335  * Event queue memory allocations are done only once.  If the channel
336  * is reset, the memory buffer will be reused; this guards against
337  * errors during channel reset and also simplifies interrupt handling.
338  */
339 static int ef4_probe_eventq(struct ef4_channel *channel)
340 {
341         struct ef4_nic *efx = channel->efx;
342         unsigned long entries;
343
344         netif_dbg(efx, probe, efx->net_dev,
345                   "chan %d create event queue\n", channel->channel);
346
347         /* Build an event queue with room for one event per tx and rx buffer,
348          * plus some extra for link state events and MCDI completions. */
349         entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
350         EF4_BUG_ON_PARANOID(entries > EF4_MAX_EVQ_SIZE);
351         channel->eventq_mask = max(entries, EF4_MIN_EVQ_SIZE) - 1;
352
353         return ef4_nic_probe_eventq(channel);
354 }
355
356 /* Prepare channel's event queue */
357 static int ef4_init_eventq(struct ef4_channel *channel)
358 {
359         struct ef4_nic *efx = channel->efx;
360         int rc;
361
362         EF4_WARN_ON_PARANOID(channel->eventq_init);
363
364         netif_dbg(efx, drv, efx->net_dev,
365                   "chan %d init event queue\n", channel->channel);
366
367         rc = ef4_nic_init_eventq(channel);
368         if (rc == 0) {
369                 efx->type->push_irq_moderation(channel);
370                 channel->eventq_read_ptr = 0;
371                 channel->eventq_init = true;
372         }
373         return rc;
374 }
375
376 /* Enable event queue processing and NAPI */
377 void ef4_start_eventq(struct ef4_channel *channel)
378 {
379         netif_dbg(channel->efx, ifup, channel->efx->net_dev,
380                   "chan %d start event queue\n", channel->channel);
381
382         /* Make sure the NAPI handler sees the enabled flag set */
383         channel->enabled = true;
384         smp_wmb();
385
386         napi_enable(&channel->napi_str);
387         ef4_nic_eventq_read_ack(channel);
388 }
389
390 /* Disable event queue processing and NAPI */
391 void ef4_stop_eventq(struct ef4_channel *channel)
392 {
393         if (!channel->enabled)
394                 return;
395
396         napi_disable(&channel->napi_str);
397         channel->enabled = false;
398 }
399
400 static void ef4_fini_eventq(struct ef4_channel *channel)
401 {
402         if (!channel->eventq_init)
403                 return;
404
405         netif_dbg(channel->efx, drv, channel->efx->net_dev,
406                   "chan %d fini event queue\n", channel->channel);
407
408         ef4_nic_fini_eventq(channel);
409         channel->eventq_init = false;
410 }
411
412 static void ef4_remove_eventq(struct ef4_channel *channel)
413 {
414         netif_dbg(channel->efx, drv, channel->efx->net_dev,
415                   "chan %d remove event queue\n", channel->channel);
416
417         ef4_nic_remove_eventq(channel);
418 }
419
420 /**************************************************************************
421  *
422  * Channel handling
423  *
424  *************************************************************************/
425
426 /* Allocate and initialise a channel structure. */
427 static struct ef4_channel *
428 ef4_alloc_channel(struct ef4_nic *efx, int i, struct ef4_channel *old_channel)
429 {
430         struct ef4_channel *channel;
431         struct ef4_rx_queue *rx_queue;
432         struct ef4_tx_queue *tx_queue;
433         int j;
434
435         channel = kzalloc(sizeof(*channel), GFP_KERNEL);
436         if (!channel)
437                 return NULL;
438
439         channel->efx = efx;
440         channel->channel = i;
441         channel->type = &ef4_default_channel_type;
442
443         for (j = 0; j < EF4_TXQ_TYPES; j++) {
444                 tx_queue = &channel->tx_queue[j];
445                 tx_queue->efx = efx;
446                 tx_queue->queue = i * EF4_TXQ_TYPES + j;
447                 tx_queue->channel = channel;
448         }
449
450         rx_queue = &channel->rx_queue;
451         rx_queue->efx = efx;
452         timer_setup(&rx_queue->slow_fill, ef4_rx_slow_fill, 0);
453
454         return channel;
455 }
456
457 /* Allocate and initialise a channel structure, copying parameters
458  * (but not resources) from an old channel structure.
459  */
460 static struct ef4_channel *
461 ef4_copy_channel(const struct ef4_channel *old_channel)
462 {
463         struct ef4_channel *channel;
464         struct ef4_rx_queue *rx_queue;
465         struct ef4_tx_queue *tx_queue;
466         int j;
467
468         channel = kmalloc(sizeof(*channel), GFP_KERNEL);
469         if (!channel)
470                 return NULL;
471
472         *channel = *old_channel;
473
474         channel->napi_dev = NULL;
475         INIT_HLIST_NODE(&channel->napi_str.napi_hash_node);
476         channel->napi_str.napi_id = 0;
477         channel->napi_str.state = 0;
478         memset(&channel->eventq, 0, sizeof(channel->eventq));
479
480         for (j = 0; j < EF4_TXQ_TYPES; j++) {
481                 tx_queue = &channel->tx_queue[j];
482                 if (tx_queue->channel)
483                         tx_queue->channel = channel;
484                 tx_queue->buffer = NULL;
485                 memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
486         }
487
488         rx_queue = &channel->rx_queue;
489         rx_queue->buffer = NULL;
490         memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
491         timer_setup(&rx_queue->slow_fill, ef4_rx_slow_fill, 0);
492
493         return channel;
494 }
495
496 static int ef4_probe_channel(struct ef4_channel *channel)
497 {
498         struct ef4_tx_queue *tx_queue;
499         struct ef4_rx_queue *rx_queue;
500         int rc;
501
502         netif_dbg(channel->efx, probe, channel->efx->net_dev,
503                   "creating channel %d\n", channel->channel);
504
505         rc = channel->type->pre_probe(channel);
506         if (rc)
507                 goto fail;
508
509         rc = ef4_probe_eventq(channel);
510         if (rc)
511                 goto fail;
512
513         ef4_for_each_channel_tx_queue(tx_queue, channel) {
514                 rc = ef4_probe_tx_queue(tx_queue);
515                 if (rc)
516                         goto fail;
517         }
518
519         ef4_for_each_channel_rx_queue(rx_queue, channel) {
520                 rc = ef4_probe_rx_queue(rx_queue);
521                 if (rc)
522                         goto fail;
523         }
524
525         return 0;
526
527 fail:
528         ef4_remove_channel(channel);
529         return rc;
530 }
531
532 static void
533 ef4_get_channel_name(struct ef4_channel *channel, char *buf, size_t len)
534 {
535         struct ef4_nic *efx = channel->efx;
536         const char *type;
537         int number;
538
539         number = channel->channel;
540         if (efx->tx_channel_offset == 0) {
541                 type = "";
542         } else if (channel->channel < efx->tx_channel_offset) {
543                 type = "-rx";
544         } else {
545                 type = "-tx";
546                 number -= efx->tx_channel_offset;
547         }
548         snprintf(buf, len, "%s%s-%d", efx->name, type, number);
549 }
550
551 static void ef4_set_channel_names(struct ef4_nic *efx)
552 {
553         struct ef4_channel *channel;
554
555         ef4_for_each_channel(channel, efx)
556                 channel->type->get_name(channel,
557                                         efx->msi_context[channel->channel].name,
558                                         sizeof(efx->msi_context[0].name));
559 }
560
561 static int ef4_probe_channels(struct ef4_nic *efx)
562 {
563         struct ef4_channel *channel;
564         int rc;
565
566         /* Restart special buffer allocation */
567         efx->next_buffer_table = 0;
568
569         /* Probe channels in reverse, so that any 'extra' channels
570          * use the start of the buffer table. This allows the traffic
571          * channels to be resized without moving them or wasting the
572          * entries before them.
573          */
574         ef4_for_each_channel_rev(channel, efx) {
575                 rc = ef4_probe_channel(channel);
576                 if (rc) {
577                         netif_err(efx, probe, efx->net_dev,
578                                   "failed to create channel %d\n",
579                                   channel->channel);
580                         goto fail;
581                 }
582         }
583         ef4_set_channel_names(efx);
584
585         return 0;
586
587 fail:
588         ef4_remove_channels(efx);
589         return rc;
590 }
591
592 /* Channels are shutdown and reinitialised whilst the NIC is running
593  * to propagate configuration changes (mtu, checksum offload), or
594  * to clear hardware error conditions
595  */
596 static void ef4_start_datapath(struct ef4_nic *efx)
597 {
598         netdev_features_t old_features = efx->net_dev->features;
599         bool old_rx_scatter = efx->rx_scatter;
600         struct ef4_tx_queue *tx_queue;
601         struct ef4_rx_queue *rx_queue;
602         struct ef4_channel *channel;
603         size_t rx_buf_len;
604
605         /* Calculate the rx buffer allocation parameters required to
606          * support the current MTU, including padding for header
607          * alignment and overruns.
608          */
609         efx->rx_dma_len = (efx->rx_prefix_size +
610                            EF4_MAX_FRAME_LEN(efx->net_dev->mtu) +
611                            efx->type->rx_buffer_padding);
612         rx_buf_len = (sizeof(struct ef4_rx_page_state) +
613                       efx->rx_ip_align + efx->rx_dma_len);
614         if (rx_buf_len <= PAGE_SIZE) {
615                 efx->rx_scatter = efx->type->always_rx_scatter;
616                 efx->rx_buffer_order = 0;
617         } else if (efx->type->can_rx_scatter) {
618                 BUILD_BUG_ON(EF4_RX_USR_BUF_SIZE % L1_CACHE_BYTES);
619                 BUILD_BUG_ON(sizeof(struct ef4_rx_page_state) +
620                              2 * ALIGN(NET_IP_ALIGN + EF4_RX_USR_BUF_SIZE,
621                                        EF4_RX_BUF_ALIGNMENT) >
622                              PAGE_SIZE);
623                 efx->rx_scatter = true;
624                 efx->rx_dma_len = EF4_RX_USR_BUF_SIZE;
625                 efx->rx_buffer_order = 0;
626         } else {
627                 efx->rx_scatter = false;
628                 efx->rx_buffer_order = get_order(rx_buf_len);
629         }
630
631         ef4_rx_config_page_split(efx);
632         if (efx->rx_buffer_order)
633                 netif_dbg(efx, drv, efx->net_dev,
634                           "RX buf len=%u; page order=%u batch=%u\n",
635                           efx->rx_dma_len, efx->rx_buffer_order,
636                           efx->rx_pages_per_batch);
637         else
638                 netif_dbg(efx, drv, efx->net_dev,
639                           "RX buf len=%u step=%u bpp=%u; page batch=%u\n",
640                           efx->rx_dma_len, efx->rx_page_buf_step,
641                           efx->rx_bufs_per_page, efx->rx_pages_per_batch);
642
643         /* Restore previously fixed features in hw_features and remove
644          * features which are fixed now
645          */
646         efx->net_dev->hw_features |= efx->net_dev->features;
647         efx->net_dev->hw_features &= ~efx->fixed_features;
648         efx->net_dev->features |= efx->fixed_features;
649         if (efx->net_dev->features != old_features)
650                 netdev_features_change(efx->net_dev);
651
652         /* RX filters may also have scatter-enabled flags */
653         if (efx->rx_scatter != old_rx_scatter)
654                 efx->type->filter_update_rx_scatter(efx);
655
656         /* We must keep at least one descriptor in a TX ring empty.
657          * We could avoid this when the queue size does not exactly
658          * match the hardware ring size, but it's not that important.
659          * Therefore we stop the queue when one more skb might fill
660          * the ring completely.  We wake it when half way back to
661          * empty.
662          */
663         efx->txq_stop_thresh = efx->txq_entries - ef4_tx_max_skb_descs(efx);
664         efx->txq_wake_thresh = efx->txq_stop_thresh / 2;
665
666         /* Initialise the channels */
667         ef4_for_each_channel(channel, efx) {
668                 ef4_for_each_channel_tx_queue(tx_queue, channel) {
669                         ef4_init_tx_queue(tx_queue);
670                         atomic_inc(&efx->active_queues);
671                 }
672
673                 ef4_for_each_channel_rx_queue(rx_queue, channel) {
674                         ef4_init_rx_queue(rx_queue);
675                         atomic_inc(&efx->active_queues);
676                         ef4_stop_eventq(channel);
677                         ef4_fast_push_rx_descriptors(rx_queue, false);
678                         ef4_start_eventq(channel);
679                 }
680
681                 WARN_ON(channel->rx_pkt_n_frags);
682         }
683
684         if (netif_device_present(efx->net_dev))
685                 netif_tx_wake_all_queues(efx->net_dev);
686 }
687
688 static void ef4_stop_datapath(struct ef4_nic *efx)
689 {
690         struct ef4_channel *channel;
691         struct ef4_tx_queue *tx_queue;
692         struct ef4_rx_queue *rx_queue;
693         int rc;
694
695         EF4_ASSERT_RESET_SERIALISED(efx);
696         BUG_ON(efx->port_enabled);
697
698         /* Stop RX refill */
699         ef4_for_each_channel(channel, efx) {
700                 ef4_for_each_channel_rx_queue(rx_queue, channel)
701                         rx_queue->refill_enabled = false;
702         }
703
704         ef4_for_each_channel(channel, efx) {
705                 /* RX packet processing is pipelined, so wait for the
706                  * NAPI handler to complete.  At least event queue 0
707                  * might be kept active by non-data events, so don't
708                  * use napi_synchronize() but actually disable NAPI
709                  * temporarily.
710                  */
711                 if (ef4_channel_has_rx_queue(channel)) {
712                         ef4_stop_eventq(channel);
713                         ef4_start_eventq(channel);
714                 }
715         }
716
717         rc = efx->type->fini_dmaq(efx);
718         if (rc && EF4_WORKAROUND_7803(efx)) {
719                 /* Schedule a reset to recover from the flush failure. The
720                  * descriptor caches reference memory we're about to free,
721                  * but falcon_reconfigure_mac_wrapper() won't reconnect
722                  * the MACs because of the pending reset.
723                  */
724                 netif_err(efx, drv, efx->net_dev,
725                           "Resetting to recover from flush failure\n");
726                 ef4_schedule_reset(efx, RESET_TYPE_ALL);
727         } else if (rc) {
728                 netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
729         } else {
730                 netif_dbg(efx, drv, efx->net_dev,
731                           "successfully flushed all queues\n");
732         }
733
734         ef4_for_each_channel(channel, efx) {
735                 ef4_for_each_channel_rx_queue(rx_queue, channel)
736                         ef4_fini_rx_queue(rx_queue);
737                 ef4_for_each_possible_channel_tx_queue(tx_queue, channel)
738                         ef4_fini_tx_queue(tx_queue);
739         }
740 }
741
742 static void ef4_remove_channel(struct ef4_channel *channel)
743 {
744         struct ef4_tx_queue *tx_queue;
745         struct ef4_rx_queue *rx_queue;
746
747         netif_dbg(channel->efx, drv, channel->efx->net_dev,
748                   "destroy chan %d\n", channel->channel);
749
750         ef4_for_each_channel_rx_queue(rx_queue, channel)
751                 ef4_remove_rx_queue(rx_queue);
752         ef4_for_each_possible_channel_tx_queue(tx_queue, channel)
753                 ef4_remove_tx_queue(tx_queue);
754         ef4_remove_eventq(channel);
755         channel->type->post_remove(channel);
756 }
757
758 static void ef4_remove_channels(struct ef4_nic *efx)
759 {
760         struct ef4_channel *channel;
761
762         ef4_for_each_channel(channel, efx)
763                 ef4_remove_channel(channel);
764 }
765
766 int
767 ef4_realloc_channels(struct ef4_nic *efx, u32 rxq_entries, u32 txq_entries)
768 {
769         struct ef4_channel *other_channel[EF4_MAX_CHANNELS], *channel;
770         u32 old_rxq_entries, old_txq_entries;
771         unsigned i, next_buffer_table = 0;
772         int rc, rc2;
773
774         rc = ef4_check_disabled(efx);
775         if (rc)
776                 return rc;
777
778         /* Not all channels should be reallocated. We must avoid
779          * reallocating their buffer table entries.
780          */
781         ef4_for_each_channel(channel, efx) {
782                 struct ef4_rx_queue *rx_queue;
783                 struct ef4_tx_queue *tx_queue;
784
785                 if (channel->type->copy)
786                         continue;
787                 next_buffer_table = max(next_buffer_table,
788                                         channel->eventq.index +
789                                         channel->eventq.entries);
790                 ef4_for_each_channel_rx_queue(rx_queue, channel)
791                         next_buffer_table = max(next_buffer_table,
792                                                 rx_queue->rxd.index +
793                                                 rx_queue->rxd.entries);
794                 ef4_for_each_channel_tx_queue(tx_queue, channel)
795                         next_buffer_table = max(next_buffer_table,
796                                                 tx_queue->txd.index +
797                                                 tx_queue->txd.entries);
798         }
799
800         ef4_device_detach_sync(efx);
801         ef4_stop_all(efx);
802         ef4_soft_disable_interrupts(efx);
803
804         /* Clone channels (where possible) */
805         memset(other_channel, 0, sizeof(other_channel));
806         for (i = 0; i < efx->n_channels; i++) {
807                 channel = efx->channel[i];
808                 if (channel->type->copy)
809                         channel = channel->type->copy(channel);
810                 if (!channel) {
811                         rc = -ENOMEM;
812                         goto out;
813                 }
814                 other_channel[i] = channel;
815         }
816
817         /* Swap entry counts and channel pointers */
818         old_rxq_entries = efx->rxq_entries;
819         old_txq_entries = efx->txq_entries;
820         efx->rxq_entries = rxq_entries;
821         efx->txq_entries = txq_entries;
822         for (i = 0; i < efx->n_channels; i++) {
823                 channel = efx->channel[i];
824                 efx->channel[i] = other_channel[i];
825                 other_channel[i] = channel;
826         }
827
828         /* Restart buffer table allocation */
829         efx->next_buffer_table = next_buffer_table;
830
831         for (i = 0; i < efx->n_channels; i++) {
832                 channel = efx->channel[i];
833                 if (!channel->type->copy)
834                         continue;
835                 rc = ef4_probe_channel(channel);
836                 if (rc)
837                         goto rollback;
838                 ef4_init_napi_channel(efx->channel[i]);
839         }
840
841 out:
842         /* Destroy unused channel structures */
843         for (i = 0; i < efx->n_channels; i++) {
844                 channel = other_channel[i];
845                 if (channel && channel->type->copy) {
846                         ef4_fini_napi_channel(channel);
847                         ef4_remove_channel(channel);
848                         kfree(channel);
849                 }
850         }
851
852         rc2 = ef4_soft_enable_interrupts(efx);
853         if (rc2) {
854                 rc = rc ? rc : rc2;
855                 netif_err(efx, drv, efx->net_dev,
856                           "unable to restart interrupts on channel reallocation\n");
857                 ef4_schedule_reset(efx, RESET_TYPE_DISABLE);
858         } else {
859                 ef4_start_all(efx);
860                 netif_device_attach(efx->net_dev);
861         }
862         return rc;
863
864 rollback:
865         /* Swap back */
866         efx->rxq_entries = old_rxq_entries;
867         efx->txq_entries = old_txq_entries;
868         for (i = 0; i < efx->n_channels; i++) {
869                 channel = efx->channel[i];
870                 efx->channel[i] = other_channel[i];
871                 other_channel[i] = channel;
872         }
873         goto out;
874 }
875
876 void ef4_schedule_slow_fill(struct ef4_rx_queue *rx_queue)
877 {
878         mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
879 }
880
881 static const struct ef4_channel_type ef4_default_channel_type = {
882         .pre_probe              = ef4_channel_dummy_op_int,
883         .post_remove            = ef4_channel_dummy_op_void,
884         .get_name               = ef4_get_channel_name,
885         .copy                   = ef4_copy_channel,
886         .keep_eventq            = false,
887 };
888
889 int ef4_channel_dummy_op_int(struct ef4_channel *channel)
890 {
891         return 0;
892 }
893
894 void ef4_channel_dummy_op_void(struct ef4_channel *channel)
895 {
896 }
897
898 /**************************************************************************
899  *
900  * Port handling
901  *
902  **************************************************************************/
903
904 /* This ensures that the kernel is kept informed (via
905  * netif_carrier_on/off) of the link status, and also maintains the
906  * link status's stop on the port's TX queue.
907  */
908 void ef4_link_status_changed(struct ef4_nic *efx)
909 {
910         struct ef4_link_state *link_state = &efx->link_state;
911
912         /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
913          * that no events are triggered between unregister_netdev() and the
914          * driver unloading. A more general condition is that NETDEV_CHANGE
915          * can only be generated between NETDEV_UP and NETDEV_DOWN */
916         if (!netif_running(efx->net_dev))
917                 return;
918
919         if (link_state->up != netif_carrier_ok(efx->net_dev)) {
920                 efx->n_link_state_changes++;
921
922                 if (link_state->up)
923                         netif_carrier_on(efx->net_dev);
924                 else
925                         netif_carrier_off(efx->net_dev);
926         }
927
928         /* Status message for kernel log */
929         if (link_state->up)
930                 netif_info(efx, link, efx->net_dev,
931                            "link up at %uMbps %s-duplex (MTU %d)\n",
932                            link_state->speed, link_state->fd ? "full" : "half",
933                            efx->net_dev->mtu);
934         else
935                 netif_info(efx, link, efx->net_dev, "link down\n");
936 }
937
938 void ef4_link_set_advertising(struct ef4_nic *efx, u32 advertising)
939 {
940         efx->link_advertising = advertising;
941         if (advertising) {
942                 if (advertising & ADVERTISED_Pause)
943                         efx->wanted_fc |= (EF4_FC_TX | EF4_FC_RX);
944                 else
945                         efx->wanted_fc &= ~(EF4_FC_TX | EF4_FC_RX);
946                 if (advertising & ADVERTISED_Asym_Pause)
947                         efx->wanted_fc ^= EF4_FC_TX;
948         }
949 }
950
951 void ef4_link_set_wanted_fc(struct ef4_nic *efx, u8 wanted_fc)
952 {
953         efx->wanted_fc = wanted_fc;
954         if (efx->link_advertising) {
955                 if (wanted_fc & EF4_FC_RX)
956                         efx->link_advertising |= (ADVERTISED_Pause |
957                                                   ADVERTISED_Asym_Pause);
958                 else
959                         efx->link_advertising &= ~(ADVERTISED_Pause |
960                                                    ADVERTISED_Asym_Pause);
961                 if (wanted_fc & EF4_FC_TX)
962                         efx->link_advertising ^= ADVERTISED_Asym_Pause;
963         }
964 }
965
966 static void ef4_fini_port(struct ef4_nic *efx);
967
968 /* We assume that efx->type->reconfigure_mac will always try to sync RX
969  * filters and therefore needs to read-lock the filter table against freeing
970  */
971 void ef4_mac_reconfigure(struct ef4_nic *efx)
972 {
973         down_read(&efx->filter_sem);
974         efx->type->reconfigure_mac(efx);
975         up_read(&efx->filter_sem);
976 }
977
978 /* Push loopback/power/transmit disable settings to the PHY, and reconfigure
979  * the MAC appropriately. All other PHY configuration changes are pushed
980  * through phy_op->set_link_ksettings(), and pushed asynchronously to the MAC
981  * through ef4_monitor().
982  *
983  * Callers must hold the mac_lock
984  */
985 int __ef4_reconfigure_port(struct ef4_nic *efx)
986 {
987         enum ef4_phy_mode phy_mode;
988         int rc;
989
990         WARN_ON(!mutex_is_locked(&efx->mac_lock));
991
992         /* Disable PHY transmit in mac level loopbacks */
993         phy_mode = efx->phy_mode;
994         if (LOOPBACK_INTERNAL(efx))
995                 efx->phy_mode |= PHY_MODE_TX_DISABLED;
996         else
997                 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
998
999         rc = efx->type->reconfigure_port(efx);
1000
1001         if (rc)
1002                 efx->phy_mode = phy_mode;
1003
1004         return rc;
1005 }
1006
1007 /* Reinitialise the MAC to pick up new PHY settings, even if the port is
1008  * disabled. */
1009 int ef4_reconfigure_port(struct ef4_nic *efx)
1010 {
1011         int rc;
1012
1013         EF4_ASSERT_RESET_SERIALISED(efx);
1014
1015         mutex_lock(&efx->mac_lock);
1016         rc = __ef4_reconfigure_port(efx);
1017         mutex_unlock(&efx->mac_lock);
1018
1019         return rc;
1020 }
1021
1022 /* Asynchronous work item for changing MAC promiscuity and multicast
1023  * hash.  Avoid a drain/rx_ingress enable by reconfiguring the current
1024  * MAC directly. */
1025 static void ef4_mac_work(struct work_struct *data)
1026 {
1027         struct ef4_nic *efx = container_of(data, struct ef4_nic, mac_work);
1028
1029         mutex_lock(&efx->mac_lock);
1030         if (efx->port_enabled)
1031                 ef4_mac_reconfigure(efx);
1032         mutex_unlock(&efx->mac_lock);
1033 }
1034
1035 static int ef4_probe_port(struct ef4_nic *efx)
1036 {
1037         int rc;
1038
1039         netif_dbg(efx, probe, efx->net_dev, "create port\n");
1040
1041         if (phy_flash_cfg)
1042                 efx->phy_mode = PHY_MODE_SPECIAL;
1043
1044         /* Connect up MAC/PHY operations table */
1045         rc = efx->type->probe_port(efx);
1046         if (rc)
1047                 return rc;
1048
1049         /* Initialise MAC address to permanent address */
1050         ether_addr_copy(efx->net_dev->dev_addr, efx->net_dev->perm_addr);
1051
1052         return 0;
1053 }
1054
1055 static int ef4_init_port(struct ef4_nic *efx)
1056 {
1057         int rc;
1058
1059         netif_dbg(efx, drv, efx->net_dev, "init port\n");
1060
1061         mutex_lock(&efx->mac_lock);
1062
1063         rc = efx->phy_op->init(efx);
1064         if (rc)
1065                 goto fail1;
1066
1067         efx->port_initialized = true;
1068
1069         /* Reconfigure the MAC before creating dma queues (required for
1070          * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
1071         ef4_mac_reconfigure(efx);
1072
1073         /* Ensure the PHY advertises the correct flow control settings */
1074         rc = efx->phy_op->reconfigure(efx);
1075         if (rc && rc != -EPERM)
1076                 goto fail2;
1077
1078         mutex_unlock(&efx->mac_lock);
1079         return 0;
1080
1081 fail2:
1082         efx->phy_op->fini(efx);
1083 fail1:
1084         mutex_unlock(&efx->mac_lock);
1085         return rc;
1086 }
1087
1088 static void ef4_start_port(struct ef4_nic *efx)
1089 {
1090         netif_dbg(efx, ifup, efx->net_dev, "start port\n");
1091         BUG_ON(efx->port_enabled);
1092
1093         mutex_lock(&efx->mac_lock);
1094         efx->port_enabled = true;
1095
1096         /* Ensure MAC ingress/egress is enabled */
1097         ef4_mac_reconfigure(efx);
1098
1099         mutex_unlock(&efx->mac_lock);
1100 }
1101
1102 /* Cancel work for MAC reconfiguration, periodic hardware monitoring
1103  * and the async self-test, wait for them to finish and prevent them
1104  * being scheduled again.  This doesn't cover online resets, which
1105  * should only be cancelled when removing the device.
1106  */
1107 static void ef4_stop_port(struct ef4_nic *efx)
1108 {
1109         netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
1110
1111         EF4_ASSERT_RESET_SERIALISED(efx);
1112
1113         mutex_lock(&efx->mac_lock);
1114         efx->port_enabled = false;
1115         mutex_unlock(&efx->mac_lock);
1116
1117         /* Serialise against ef4_set_multicast_list() */
1118         netif_addr_lock_bh(efx->net_dev);
1119         netif_addr_unlock_bh(efx->net_dev);
1120
1121         cancel_delayed_work_sync(&efx->monitor_work);
1122         ef4_selftest_async_cancel(efx);
1123         cancel_work_sync(&efx->mac_work);
1124 }
1125
1126 static void ef4_fini_port(struct ef4_nic *efx)
1127 {
1128         netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
1129
1130         if (!efx->port_initialized)
1131                 return;
1132
1133         efx->phy_op->fini(efx);
1134         efx->port_initialized = false;
1135
1136         efx->link_state.up = false;
1137         ef4_link_status_changed(efx);
1138 }
1139
1140 static void ef4_remove_port(struct ef4_nic *efx)
1141 {
1142         netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
1143
1144         efx->type->remove_port(efx);
1145 }
1146
1147 /**************************************************************************
1148  *
1149  * NIC handling
1150  *
1151  **************************************************************************/
1152
1153 static LIST_HEAD(ef4_primary_list);
1154 static LIST_HEAD(ef4_unassociated_list);
1155
1156 static bool ef4_same_controller(struct ef4_nic *left, struct ef4_nic *right)
1157 {
1158         return left->type == right->type &&
1159                 left->vpd_sn && right->vpd_sn &&
1160                 !strcmp(left->vpd_sn, right->vpd_sn);
1161 }
1162
1163 static void ef4_associate(struct ef4_nic *efx)
1164 {
1165         struct ef4_nic *other, *next;
1166
1167         if (efx->primary == efx) {
1168                 /* Adding primary function; look for secondaries */
1169
1170                 netif_dbg(efx, probe, efx->net_dev, "adding to primary list\n");
1171                 list_add_tail(&efx->node, &ef4_primary_list);
1172
1173                 list_for_each_entry_safe(other, next, &ef4_unassociated_list,
1174                                          node) {
1175                         if (ef4_same_controller(efx, other)) {
1176                                 list_del(&other->node);
1177                                 netif_dbg(other, probe, other->net_dev,
1178                                           "moving to secondary list of %s %s\n",
1179                                           pci_name(efx->pci_dev),
1180                                           efx->net_dev->name);
1181                                 list_add_tail(&other->node,
1182                                               &efx->secondary_list);
1183                                 other->primary = efx;
1184                         }
1185                 }
1186         } else {
1187                 /* Adding secondary function; look for primary */
1188
1189                 list_for_each_entry(other, &ef4_primary_list, node) {
1190                         if (ef4_same_controller(efx, other)) {
1191                                 netif_dbg(efx, probe, efx->net_dev,
1192                                           "adding to secondary list of %s %s\n",
1193                                           pci_name(other->pci_dev),
1194                                           other->net_dev->name);
1195                                 list_add_tail(&efx->node,
1196                                               &other->secondary_list);
1197                                 efx->primary = other;
1198                                 return;
1199                         }
1200                 }
1201
1202                 netif_dbg(efx, probe, efx->net_dev,
1203                           "adding to unassociated list\n");
1204                 list_add_tail(&efx->node, &ef4_unassociated_list);
1205         }
1206 }
1207
1208 static void ef4_dissociate(struct ef4_nic *efx)
1209 {
1210         struct ef4_nic *other, *next;
1211
1212         list_del(&efx->node);
1213         efx->primary = NULL;
1214
1215         list_for_each_entry_safe(other, next, &efx->secondary_list, node) {
1216                 list_del(&other->node);
1217                 netif_dbg(other, probe, other->net_dev,
1218                           "moving to unassociated list\n");
1219                 list_add_tail(&other->node, &ef4_unassociated_list);
1220                 other->primary = NULL;
1221         }
1222 }
1223
1224 /* This configures the PCI device to enable I/O and DMA. */
1225 static int ef4_init_io(struct ef4_nic *efx)
1226 {
1227         struct pci_dev *pci_dev = efx->pci_dev;
1228         dma_addr_t dma_mask = efx->type->max_dma_mask;
1229         unsigned int mem_map_size = efx->type->mem_map_size(efx);
1230         int rc, bar;
1231
1232         netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
1233
1234         bar = efx->type->mem_bar;
1235
1236         rc = pci_enable_device(pci_dev);
1237         if (rc) {
1238                 netif_err(efx, probe, efx->net_dev,
1239                           "failed to enable PCI device\n");
1240                 goto fail1;
1241         }
1242
1243         pci_set_master(pci_dev);
1244
1245         /* Set the PCI DMA mask.  Try all possibilities from our genuine mask
1246          * down to 32 bits, because some architectures will allow 40 bit
1247          * masks event though they reject 46 bit masks.
1248          */
1249         while (dma_mask > 0x7fffffffUL) {
1250                 rc = dma_set_mask_and_coherent(&pci_dev->dev, dma_mask);
1251                 if (rc == 0)
1252                         break;
1253                 dma_mask >>= 1;
1254         }
1255         if (rc) {
1256                 netif_err(efx, probe, efx->net_dev,
1257                           "could not find a suitable DMA mask\n");
1258                 goto fail2;
1259         }
1260         netif_dbg(efx, probe, efx->net_dev,
1261                   "using DMA mask %llx\n", (unsigned long long) dma_mask);
1262
1263         efx->membase_phys = pci_resource_start(efx->pci_dev, bar);
1264         rc = pci_request_region(pci_dev, bar, "sfc");
1265         if (rc) {
1266                 netif_err(efx, probe, efx->net_dev,
1267                           "request for memory BAR failed\n");
1268                 rc = -EIO;
1269                 goto fail3;
1270         }
1271         efx->membase = ioremap_nocache(efx->membase_phys, mem_map_size);
1272         if (!efx->membase) {
1273                 netif_err(efx, probe, efx->net_dev,
1274                           "could not map memory BAR at %llx+%x\n",
1275                           (unsigned long long)efx->membase_phys, mem_map_size);
1276                 rc = -ENOMEM;
1277                 goto fail4;
1278         }
1279         netif_dbg(efx, probe, efx->net_dev,
1280                   "memory BAR at %llx+%x (virtual %p)\n",
1281                   (unsigned long long)efx->membase_phys, mem_map_size,
1282                   efx->membase);
1283
1284         return 0;
1285
1286  fail4:
1287         pci_release_region(efx->pci_dev, bar);
1288  fail3:
1289         efx->membase_phys = 0;
1290  fail2:
1291         pci_disable_device(efx->pci_dev);
1292  fail1:
1293         return rc;
1294 }
1295
1296 static void ef4_fini_io(struct ef4_nic *efx)
1297 {
1298         int bar;
1299
1300         netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
1301
1302         if (efx->membase) {
1303                 iounmap(efx->membase);
1304                 efx->membase = NULL;
1305         }
1306
1307         if (efx->membase_phys) {
1308                 bar = efx->type->mem_bar;
1309                 pci_release_region(efx->pci_dev, bar);
1310                 efx->membase_phys = 0;
1311         }
1312
1313         /* Don't disable bus-mastering if VFs are assigned */
1314         if (!pci_vfs_assigned(efx->pci_dev))
1315                 pci_disable_device(efx->pci_dev);
1316 }
1317
1318 void ef4_set_default_rx_indir_table(struct ef4_nic *efx)
1319 {
1320         size_t i;
1321
1322         for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
1323                 efx->rx_indir_table[i] =
1324                         ethtool_rxfh_indir_default(i, efx->rss_spread);
1325 }
1326
1327 static unsigned int ef4_wanted_parallelism(struct ef4_nic *efx)
1328 {
1329         cpumask_var_t thread_mask;
1330         unsigned int count;
1331         int cpu;
1332
1333         if (rss_cpus) {
1334                 count = rss_cpus;
1335         } else {
1336                 if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
1337                         netif_warn(efx, probe, efx->net_dev,
1338                                    "RSS disabled due to allocation failure\n");
1339                         return 1;
1340                 }
1341
1342                 count = 0;
1343                 for_each_online_cpu(cpu) {
1344                         if (!cpumask_test_cpu(cpu, thread_mask)) {
1345                                 ++count;
1346                                 cpumask_or(thread_mask, thread_mask,
1347                                            topology_sibling_cpumask(cpu));
1348                         }
1349                 }
1350
1351                 free_cpumask_var(thread_mask);
1352         }
1353
1354         if (count > EF4_MAX_RX_QUEUES) {
1355                 netif_cond_dbg(efx, probe, efx->net_dev, !rss_cpus, warn,
1356                                "Reducing number of rx queues from %u to %u.\n",
1357                                count, EF4_MAX_RX_QUEUES);
1358                 count = EF4_MAX_RX_QUEUES;
1359         }
1360
1361         return count;
1362 }
1363
1364 /* Probe the number and type of interrupts we are able to obtain, and
1365  * the resulting numbers of channels and RX queues.
1366  */
1367 static int ef4_probe_interrupts(struct ef4_nic *efx)
1368 {
1369         unsigned int extra_channels = 0;
1370         unsigned int i, j;
1371         int rc;
1372
1373         for (i = 0; i < EF4_MAX_EXTRA_CHANNELS; i++)
1374                 if (efx->extra_channel_type[i])
1375                         ++extra_channels;
1376
1377         if (efx->interrupt_mode == EF4_INT_MODE_MSIX) {
1378                 struct msix_entry xentries[EF4_MAX_CHANNELS];
1379                 unsigned int n_channels;
1380
1381                 n_channels = ef4_wanted_parallelism(efx);
1382                 if (ef4_separate_tx_channels)
1383                         n_channels *= 2;
1384                 n_channels += extra_channels;
1385                 n_channels = min(n_channels, efx->max_channels);
1386
1387                 for (i = 0; i < n_channels; i++)
1388                         xentries[i].entry = i;
1389                 rc = pci_enable_msix_range(efx->pci_dev,
1390                                            xentries, 1, n_channels);
1391                 if (rc < 0) {
1392                         /* Fall back to single channel MSI */
1393                         efx->interrupt_mode = EF4_INT_MODE_MSI;
1394                         netif_err(efx, drv, efx->net_dev,
1395                                   "could not enable MSI-X\n");
1396                 } else if (rc < n_channels) {
1397                         netif_err(efx, drv, efx->net_dev,
1398                                   "WARNING: Insufficient MSI-X vectors"
1399                                   " available (%d < %u).\n", rc, n_channels);
1400                         netif_err(efx, drv, efx->net_dev,
1401                                   "WARNING: Performance may be reduced.\n");
1402                         n_channels = rc;
1403                 }
1404
1405                 if (rc > 0) {
1406                         efx->n_channels = n_channels;
1407                         if (n_channels > extra_channels)
1408                                 n_channels -= extra_channels;
1409                         if (ef4_separate_tx_channels) {
1410                                 efx->n_tx_channels = min(max(n_channels / 2,
1411                                                              1U),
1412                                                          efx->max_tx_channels);
1413                                 efx->n_rx_channels = max(n_channels -
1414                                                          efx->n_tx_channels,
1415                                                          1U);
1416                         } else {
1417                                 efx->n_tx_channels = min(n_channels,
1418                                                          efx->max_tx_channels);
1419                                 efx->n_rx_channels = n_channels;
1420                         }
1421                         for (i = 0; i < efx->n_channels; i++)
1422                                 ef4_get_channel(efx, i)->irq =
1423                                         xentries[i].vector;
1424                 }
1425         }
1426
1427         /* Try single interrupt MSI */
1428         if (efx->interrupt_mode == EF4_INT_MODE_MSI) {
1429                 efx->n_channels = 1;
1430                 efx->n_rx_channels = 1;
1431                 efx->n_tx_channels = 1;
1432                 rc = pci_enable_msi(efx->pci_dev);
1433                 if (rc == 0) {
1434                         ef4_get_channel(efx, 0)->irq = efx->pci_dev->irq;
1435                 } else {
1436                         netif_err(efx, drv, efx->net_dev,
1437                                   "could not enable MSI\n");
1438                         efx->interrupt_mode = EF4_INT_MODE_LEGACY;
1439                 }
1440         }
1441
1442         /* Assume legacy interrupts */
1443         if (efx->interrupt_mode == EF4_INT_MODE_LEGACY) {
1444                 efx->n_channels = 1 + (ef4_separate_tx_channels ? 1 : 0);
1445                 efx->n_rx_channels = 1;
1446                 efx->n_tx_channels = 1;
1447                 efx->legacy_irq = efx->pci_dev->irq;
1448         }
1449
1450         /* Assign extra channels if possible */
1451         j = efx->n_channels;
1452         for (i = 0; i < EF4_MAX_EXTRA_CHANNELS; i++) {
1453                 if (!efx->extra_channel_type[i])
1454                         continue;
1455                 if (efx->interrupt_mode != EF4_INT_MODE_MSIX ||
1456                     efx->n_channels <= extra_channels) {
1457                         efx->extra_channel_type[i]->handle_no_channel(efx);
1458                 } else {
1459                         --j;
1460                         ef4_get_channel(efx, j)->type =
1461                                 efx->extra_channel_type[i];
1462                 }
1463         }
1464
1465         efx->rss_spread = efx->n_rx_channels;
1466
1467         return 0;
1468 }
1469
1470 static int ef4_soft_enable_interrupts(struct ef4_nic *efx)
1471 {
1472         struct ef4_channel *channel, *end_channel;
1473         int rc;
1474
1475         BUG_ON(efx->state == STATE_DISABLED);
1476
1477         efx->irq_soft_enabled = true;
1478         smp_wmb();
1479
1480         ef4_for_each_channel(channel, efx) {
1481                 if (!channel->type->keep_eventq) {
1482                         rc = ef4_init_eventq(channel);
1483                         if (rc)
1484                                 goto fail;
1485                 }
1486                 ef4_start_eventq(channel);
1487         }
1488
1489         return 0;
1490 fail:
1491         end_channel = channel;
1492         ef4_for_each_channel(channel, efx) {
1493                 if (channel == end_channel)
1494                         break;
1495                 ef4_stop_eventq(channel);
1496                 if (!channel->type->keep_eventq)
1497                         ef4_fini_eventq(channel);
1498         }
1499
1500         return rc;
1501 }
1502
1503 static void ef4_soft_disable_interrupts(struct ef4_nic *efx)
1504 {
1505         struct ef4_channel *channel;
1506
1507         if (efx->state == STATE_DISABLED)
1508                 return;
1509
1510         efx->irq_soft_enabled = false;
1511         smp_wmb();
1512
1513         if (efx->legacy_irq)
1514                 synchronize_irq(efx->legacy_irq);
1515
1516         ef4_for_each_channel(channel, efx) {
1517                 if (channel->irq)
1518                         synchronize_irq(channel->irq);
1519
1520                 ef4_stop_eventq(channel);
1521                 if (!channel->type->keep_eventq)
1522                         ef4_fini_eventq(channel);
1523         }
1524 }
1525
1526 static int ef4_enable_interrupts(struct ef4_nic *efx)
1527 {
1528         struct ef4_channel *channel, *end_channel;
1529         int rc;
1530
1531         BUG_ON(efx->state == STATE_DISABLED);
1532
1533         if (efx->eeh_disabled_legacy_irq) {
1534                 enable_irq(efx->legacy_irq);
1535                 efx->eeh_disabled_legacy_irq = false;
1536         }
1537
1538         efx->type->irq_enable_master(efx);
1539
1540         ef4_for_each_channel(channel, efx) {
1541                 if (channel->type->keep_eventq) {
1542                         rc = ef4_init_eventq(channel);
1543                         if (rc)
1544                                 goto fail;
1545                 }
1546         }
1547
1548         rc = ef4_soft_enable_interrupts(efx);
1549         if (rc)
1550                 goto fail;
1551
1552         return 0;
1553
1554 fail:
1555         end_channel = channel;
1556         ef4_for_each_channel(channel, efx) {
1557                 if (channel == end_channel)
1558                         break;
1559                 if (channel->type->keep_eventq)
1560                         ef4_fini_eventq(channel);
1561         }
1562
1563         efx->type->irq_disable_non_ev(efx);
1564
1565         return rc;
1566 }
1567
1568 static void ef4_disable_interrupts(struct ef4_nic *efx)
1569 {
1570         struct ef4_channel *channel;
1571
1572         ef4_soft_disable_interrupts(efx);
1573
1574         ef4_for_each_channel(channel, efx) {
1575                 if (channel->type->keep_eventq)
1576                         ef4_fini_eventq(channel);
1577         }
1578
1579         efx->type->irq_disable_non_ev(efx);
1580 }
1581
1582 static void ef4_remove_interrupts(struct ef4_nic *efx)
1583 {
1584         struct ef4_channel *channel;
1585
1586         /* Remove MSI/MSI-X interrupts */
1587         ef4_for_each_channel(channel, efx)
1588                 channel->irq = 0;
1589         pci_disable_msi(efx->pci_dev);
1590         pci_disable_msix(efx->pci_dev);
1591
1592         /* Remove legacy interrupt */
1593         efx->legacy_irq = 0;
1594 }
1595
1596 static void ef4_set_channels(struct ef4_nic *efx)
1597 {
1598         struct ef4_channel *channel;
1599         struct ef4_tx_queue *tx_queue;
1600
1601         efx->tx_channel_offset =
1602                 ef4_separate_tx_channels ?
1603                 efx->n_channels - efx->n_tx_channels : 0;
1604
1605         /* We need to mark which channels really have RX and TX
1606          * queues, and adjust the TX queue numbers if we have separate
1607          * RX-only and TX-only channels.
1608          */
1609         ef4_for_each_channel(channel, efx) {
1610                 if (channel->channel < efx->n_rx_channels)
1611                         channel->rx_queue.core_index = channel->channel;
1612                 else
1613                         channel->rx_queue.core_index = -1;
1614
1615                 ef4_for_each_channel_tx_queue(tx_queue, channel)
1616                         tx_queue->queue -= (efx->tx_channel_offset *
1617                                             EF4_TXQ_TYPES);
1618         }
1619 }
1620
1621 static int ef4_probe_nic(struct ef4_nic *efx)
1622 {
1623         int rc;
1624
1625         netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
1626
1627         /* Carry out hardware-type specific initialisation */
1628         rc = efx->type->probe(efx);
1629         if (rc)
1630                 return rc;
1631
1632         do {
1633                 if (!efx->max_channels || !efx->max_tx_channels) {
1634                         netif_err(efx, drv, efx->net_dev,
1635                                   "Insufficient resources to allocate"
1636                                   " any channels\n");
1637                         rc = -ENOSPC;
1638                         goto fail1;
1639                 }
1640
1641                 /* Determine the number of channels and queues by trying
1642                  * to hook in MSI-X interrupts.
1643                  */
1644                 rc = ef4_probe_interrupts(efx);
1645                 if (rc)
1646                         goto fail1;
1647
1648                 ef4_set_channels(efx);
1649
1650                 /* dimension_resources can fail with EAGAIN */
1651                 rc = efx->type->dimension_resources(efx);
1652                 if (rc != 0 && rc != -EAGAIN)
1653                         goto fail2;
1654
1655                 if (rc == -EAGAIN)
1656                         /* try again with new max_channels */
1657                         ef4_remove_interrupts(efx);
1658
1659         } while (rc == -EAGAIN);
1660
1661         if (efx->n_channels > 1)
1662                 netdev_rss_key_fill(&efx->rx_hash_key,
1663                                     sizeof(efx->rx_hash_key));
1664         ef4_set_default_rx_indir_table(efx);
1665
1666         netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1667         netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
1668
1669         /* Initialise the interrupt moderation settings */
1670         efx->irq_mod_step_us = DIV_ROUND_UP(efx->timer_quantum_ns, 1000);
1671         ef4_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
1672                                 true);
1673
1674         return 0;
1675
1676 fail2:
1677         ef4_remove_interrupts(efx);
1678 fail1:
1679         efx->type->remove(efx);
1680         return rc;
1681 }
1682
1683 static void ef4_remove_nic(struct ef4_nic *efx)
1684 {
1685         netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
1686
1687         ef4_remove_interrupts(efx);
1688         efx->type->remove(efx);
1689 }
1690
1691 static int ef4_probe_filters(struct ef4_nic *efx)
1692 {
1693         int rc;
1694
1695         spin_lock_init(&efx->filter_lock);
1696         init_rwsem(&efx->filter_sem);
1697         mutex_lock(&efx->mac_lock);
1698         down_write(&efx->filter_sem);
1699         rc = efx->type->filter_table_probe(efx);
1700         if (rc)
1701                 goto out_unlock;
1702
1703 #ifdef CONFIG_RFS_ACCEL
1704         if (efx->type->offload_features & NETIF_F_NTUPLE) {
1705                 struct ef4_channel *channel;
1706                 int i, success = 1;
1707
1708                 ef4_for_each_channel(channel, efx) {
1709                         channel->rps_flow_id =
1710                                 kcalloc(efx->type->max_rx_ip_filters,
1711                                         sizeof(*channel->rps_flow_id),
1712                                         GFP_KERNEL);
1713                         if (!channel->rps_flow_id)
1714                                 success = 0;
1715                         else
1716                                 for (i = 0;
1717                                      i < efx->type->max_rx_ip_filters;
1718                                      ++i)
1719                                         channel->rps_flow_id[i] =
1720                                                 RPS_FLOW_ID_INVALID;
1721                 }
1722
1723                 if (!success) {
1724                         ef4_for_each_channel(channel, efx)
1725                                 kfree(channel->rps_flow_id);
1726                         efx->type->filter_table_remove(efx);
1727                         rc = -ENOMEM;
1728                         goto out_unlock;
1729                 }
1730
1731                 efx->rps_expire_index = efx->rps_expire_channel = 0;
1732         }
1733 #endif
1734 out_unlock:
1735         up_write(&efx->filter_sem);
1736         mutex_unlock(&efx->mac_lock);
1737         return rc;
1738 }
1739
1740 static void ef4_remove_filters(struct ef4_nic *efx)
1741 {
1742 #ifdef CONFIG_RFS_ACCEL
1743         struct ef4_channel *channel;
1744
1745         ef4_for_each_channel(channel, efx)
1746                 kfree(channel->rps_flow_id);
1747 #endif
1748         down_write(&efx->filter_sem);
1749         efx->type->filter_table_remove(efx);
1750         up_write(&efx->filter_sem);
1751 }
1752
1753 static void ef4_restore_filters(struct ef4_nic *efx)
1754 {
1755         down_read(&efx->filter_sem);
1756         efx->type->filter_table_restore(efx);
1757         up_read(&efx->filter_sem);
1758 }
1759
1760 /**************************************************************************
1761  *
1762  * NIC startup/shutdown
1763  *
1764  *************************************************************************/
1765
1766 static int ef4_probe_all(struct ef4_nic *efx)
1767 {
1768         int rc;
1769
1770         rc = ef4_probe_nic(efx);
1771         if (rc) {
1772                 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
1773                 goto fail1;
1774         }
1775
1776         rc = ef4_probe_port(efx);
1777         if (rc) {
1778                 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
1779                 goto fail2;
1780         }
1781
1782         BUILD_BUG_ON(EF4_DEFAULT_DMAQ_SIZE < EF4_RXQ_MIN_ENT);
1783         if (WARN_ON(EF4_DEFAULT_DMAQ_SIZE < EF4_TXQ_MIN_ENT(efx))) {
1784                 rc = -EINVAL;
1785                 goto fail3;
1786         }
1787         efx->rxq_entries = efx->txq_entries = EF4_DEFAULT_DMAQ_SIZE;
1788
1789         rc = ef4_probe_filters(efx);
1790         if (rc) {
1791                 netif_err(efx, probe, efx->net_dev,
1792                           "failed to create filter tables\n");
1793                 goto fail4;
1794         }
1795
1796         rc = ef4_probe_channels(efx);
1797         if (rc)
1798                 goto fail5;
1799
1800         return 0;
1801
1802  fail5:
1803         ef4_remove_filters(efx);
1804  fail4:
1805  fail3:
1806         ef4_remove_port(efx);
1807  fail2:
1808         ef4_remove_nic(efx);
1809  fail1:
1810         return rc;
1811 }
1812
1813 /* If the interface is supposed to be running but is not, start
1814  * the hardware and software data path, regular activity for the port
1815  * (MAC statistics, link polling, etc.) and schedule the port to be
1816  * reconfigured.  Interrupts must already be enabled.  This function
1817  * is safe to call multiple times, so long as the NIC is not disabled.
1818  * Requires the RTNL lock.
1819  */
1820 static void ef4_start_all(struct ef4_nic *efx)
1821 {
1822         EF4_ASSERT_RESET_SERIALISED(efx);
1823         BUG_ON(efx->state == STATE_DISABLED);
1824
1825         /* Check that it is appropriate to restart the interface. All
1826          * of these flags are safe to read under just the rtnl lock */
1827         if (efx->port_enabled || !netif_running(efx->net_dev) ||
1828             efx->reset_pending)
1829                 return;
1830
1831         ef4_start_port(efx);
1832         ef4_start_datapath(efx);
1833
1834         /* Start the hardware monitor if there is one */
1835         if (efx->type->monitor != NULL)
1836                 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1837                                    ef4_monitor_interval);
1838
1839         efx->type->start_stats(efx);
1840         efx->type->pull_stats(efx);
1841         spin_lock_bh(&efx->stats_lock);
1842         efx->type->update_stats(efx, NULL, NULL);
1843         spin_unlock_bh(&efx->stats_lock);
1844 }
1845
1846 /* Quiesce the hardware and software data path, and regular activity
1847  * for the port without bringing the link down.  Safe to call multiple
1848  * times with the NIC in almost any state, but interrupts should be
1849  * enabled.  Requires the RTNL lock.
1850  */
1851 static void ef4_stop_all(struct ef4_nic *efx)
1852 {
1853         EF4_ASSERT_RESET_SERIALISED(efx);
1854
1855         /* port_enabled can be read safely under the rtnl lock */
1856         if (!efx->port_enabled)
1857                 return;
1858
1859         /* update stats before we go down so we can accurately count
1860          * rx_nodesc_drops
1861          */
1862         efx->type->pull_stats(efx);
1863         spin_lock_bh(&efx->stats_lock);
1864         efx->type->update_stats(efx, NULL, NULL);
1865         spin_unlock_bh(&efx->stats_lock);
1866         efx->type->stop_stats(efx);
1867         ef4_stop_port(efx);
1868
1869         /* Stop the kernel transmit interface.  This is only valid if
1870          * the device is stopped or detached; otherwise the watchdog
1871          * may fire immediately.
1872          */
1873         WARN_ON(netif_running(efx->net_dev) &&
1874                 netif_device_present(efx->net_dev));
1875         netif_tx_disable(efx->net_dev);
1876
1877         ef4_stop_datapath(efx);
1878 }
1879
1880 static void ef4_remove_all(struct ef4_nic *efx)
1881 {
1882         ef4_remove_channels(efx);
1883         ef4_remove_filters(efx);
1884         ef4_remove_port(efx);
1885         ef4_remove_nic(efx);
1886 }
1887
1888 /**************************************************************************
1889  *
1890  * Interrupt moderation
1891  *
1892  **************************************************************************/
1893 unsigned int ef4_usecs_to_ticks(struct ef4_nic *efx, unsigned int usecs)
1894 {
1895         if (usecs == 0)
1896                 return 0;
1897         if (usecs * 1000 < efx->timer_quantum_ns)
1898                 return 1; /* never round down to 0 */
1899         return usecs * 1000 / efx->timer_quantum_ns;
1900 }
1901
1902 unsigned int ef4_ticks_to_usecs(struct ef4_nic *efx, unsigned int ticks)
1903 {
1904         /* We must round up when converting ticks to microseconds
1905          * because we round down when converting the other way.
1906          */
1907         return DIV_ROUND_UP(ticks * efx->timer_quantum_ns, 1000);
1908 }
1909
1910 /* Set interrupt moderation parameters */
1911 int ef4_init_irq_moderation(struct ef4_nic *efx, unsigned int tx_usecs,
1912                             unsigned int rx_usecs, bool rx_adaptive,
1913                             bool rx_may_override_tx)
1914 {
1915         struct ef4_channel *channel;
1916         unsigned int timer_max_us;
1917
1918         EF4_ASSERT_RESET_SERIALISED(efx);
1919
1920         timer_max_us = efx->timer_max_ns / 1000;
1921
1922         if (tx_usecs > timer_max_us || rx_usecs > timer_max_us)
1923                 return -EINVAL;
1924
1925         if (tx_usecs != rx_usecs && efx->tx_channel_offset == 0 &&
1926             !rx_may_override_tx) {
1927                 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
1928                           "RX and TX IRQ moderation must be equal\n");
1929                 return -EINVAL;
1930         }
1931
1932         efx->irq_rx_adaptive = rx_adaptive;
1933         efx->irq_rx_moderation_us = rx_usecs;
1934         ef4_for_each_channel(channel, efx) {
1935                 if (ef4_channel_has_rx_queue(channel))
1936                         channel->irq_moderation_us = rx_usecs;
1937                 else if (ef4_channel_has_tx_queues(channel))
1938                         channel->irq_moderation_us = tx_usecs;
1939         }
1940
1941         return 0;
1942 }
1943
1944 void ef4_get_irq_moderation(struct ef4_nic *efx, unsigned int *tx_usecs,
1945                             unsigned int *rx_usecs, bool *rx_adaptive)
1946 {
1947         *rx_adaptive = efx->irq_rx_adaptive;
1948         *rx_usecs = efx->irq_rx_moderation_us;
1949
1950         /* If channels are shared between RX and TX, so is IRQ
1951          * moderation.  Otherwise, IRQ moderation is the same for all
1952          * TX channels and is not adaptive.
1953          */
1954         if (efx->tx_channel_offset == 0) {
1955                 *tx_usecs = *rx_usecs;
1956         } else {
1957                 struct ef4_channel *tx_channel;
1958
1959                 tx_channel = efx->channel[efx->tx_channel_offset];
1960                 *tx_usecs = tx_channel->irq_moderation_us;
1961         }
1962 }
1963
1964 /**************************************************************************
1965  *
1966  * Hardware monitor
1967  *
1968  **************************************************************************/
1969
1970 /* Run periodically off the general workqueue */
1971 static void ef4_monitor(struct work_struct *data)
1972 {
1973         struct ef4_nic *efx = container_of(data, struct ef4_nic,
1974                                            monitor_work.work);
1975
1976         netif_vdbg(efx, timer, efx->net_dev,
1977                    "hardware monitor executing on CPU %d\n",
1978                    raw_smp_processor_id());
1979         BUG_ON(efx->type->monitor == NULL);
1980
1981         /* If the mac_lock is already held then it is likely a port
1982          * reconfiguration is already in place, which will likely do
1983          * most of the work of monitor() anyway. */
1984         if (mutex_trylock(&efx->mac_lock)) {
1985                 if (efx->port_enabled)
1986                         efx->type->monitor(efx);
1987                 mutex_unlock(&efx->mac_lock);
1988         }
1989
1990         queue_delayed_work(efx->workqueue, &efx->monitor_work,
1991                            ef4_monitor_interval);
1992 }
1993
1994 /**************************************************************************
1995  *
1996  * ioctls
1997  *
1998  *************************************************************************/
1999
2000 /* Net device ioctl
2001  * Context: process, rtnl_lock() held.
2002  */
2003 static int ef4_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
2004 {
2005         struct ef4_nic *efx = netdev_priv(net_dev);
2006         struct mii_ioctl_data *data = if_mii(ifr);
2007
2008         /* Convert phy_id from older PRTAD/DEVAD format */
2009         if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
2010             (data->phy_id & 0xfc00) == 0x0400)
2011                 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
2012
2013         return mdio_mii_ioctl(&efx->mdio, data, cmd);
2014 }
2015
2016 /**************************************************************************
2017  *
2018  * NAPI interface
2019  *
2020  **************************************************************************/
2021
2022 static void ef4_init_napi_channel(struct ef4_channel *channel)
2023 {
2024         struct ef4_nic *efx = channel->efx;
2025
2026         channel->napi_dev = efx->net_dev;
2027         netif_napi_add(channel->napi_dev, &channel->napi_str,
2028                        ef4_poll, napi_weight);
2029 }
2030
2031 static void ef4_init_napi(struct ef4_nic *efx)
2032 {
2033         struct ef4_channel *channel;
2034
2035         ef4_for_each_channel(channel, efx)
2036                 ef4_init_napi_channel(channel);
2037 }
2038
2039 static void ef4_fini_napi_channel(struct ef4_channel *channel)
2040 {
2041         if (channel->napi_dev)
2042                 netif_napi_del(&channel->napi_str);
2043
2044         channel->napi_dev = NULL;
2045 }
2046
2047 static void ef4_fini_napi(struct ef4_nic *efx)
2048 {
2049         struct ef4_channel *channel;
2050
2051         ef4_for_each_channel(channel, efx)
2052                 ef4_fini_napi_channel(channel);
2053 }
2054
2055 /**************************************************************************
2056  *
2057  * Kernel netpoll interface
2058  *
2059  *************************************************************************/
2060
2061 #ifdef CONFIG_NET_POLL_CONTROLLER
2062
2063 /* Although in the common case interrupts will be disabled, this is not
2064  * guaranteed. However, all our work happens inside the NAPI callback,
2065  * so no locking is required.
2066  */
2067 static void ef4_netpoll(struct net_device *net_dev)
2068 {
2069         struct ef4_nic *efx = netdev_priv(net_dev);
2070         struct ef4_channel *channel;
2071
2072         ef4_for_each_channel(channel, efx)
2073                 ef4_schedule_channel(channel);
2074 }
2075
2076 #endif
2077
2078 /**************************************************************************
2079  *
2080  * Kernel net device interface
2081  *
2082  *************************************************************************/
2083
2084 /* Context: process, rtnl_lock() held. */
2085 int ef4_net_open(struct net_device *net_dev)
2086 {
2087         struct ef4_nic *efx = netdev_priv(net_dev);
2088         int rc;
2089
2090         netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
2091                   raw_smp_processor_id());
2092
2093         rc = ef4_check_disabled(efx);
2094         if (rc)
2095                 return rc;
2096         if (efx->phy_mode & PHY_MODE_SPECIAL)
2097                 return -EBUSY;
2098
2099         /* Notify the kernel of the link state polled during driver load,
2100          * before the monitor starts running */
2101         ef4_link_status_changed(efx);
2102
2103         ef4_start_all(efx);
2104         ef4_selftest_async_start(efx);
2105         return 0;
2106 }
2107
2108 /* Context: process, rtnl_lock() held.
2109  * Note that the kernel will ignore our return code; this method
2110  * should really be a void.
2111  */
2112 int ef4_net_stop(struct net_device *net_dev)
2113 {
2114         struct ef4_nic *efx = netdev_priv(net_dev);
2115
2116         netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
2117                   raw_smp_processor_id());
2118
2119         /* Stop the device and flush all the channels */
2120         ef4_stop_all(efx);
2121
2122         return 0;
2123 }
2124
2125 /* Context: process, dev_base_lock or RTNL held, non-blocking. */
2126 static void ef4_net_stats(struct net_device *net_dev,
2127                           struct rtnl_link_stats64 *stats)
2128 {
2129         struct ef4_nic *efx = netdev_priv(net_dev);
2130
2131         spin_lock_bh(&efx->stats_lock);
2132         efx->type->update_stats(efx, NULL, stats);
2133         spin_unlock_bh(&efx->stats_lock);
2134 }
2135
2136 /* Context: netif_tx_lock held, BHs disabled. */
2137 static void ef4_watchdog(struct net_device *net_dev)
2138 {
2139         struct ef4_nic *efx = netdev_priv(net_dev);
2140
2141         netif_err(efx, tx_err, efx->net_dev,
2142                   "TX stuck with port_enabled=%d: resetting channels\n",
2143                   efx->port_enabled);
2144
2145         ef4_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
2146 }
2147
2148
2149 /* Context: process, rtnl_lock() held. */
2150 static int ef4_change_mtu(struct net_device *net_dev, int new_mtu)
2151 {
2152         struct ef4_nic *efx = netdev_priv(net_dev);
2153         int rc;
2154
2155         rc = ef4_check_disabled(efx);
2156         if (rc)
2157                 return rc;
2158
2159         netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
2160
2161         ef4_device_detach_sync(efx);
2162         ef4_stop_all(efx);
2163
2164         mutex_lock(&efx->mac_lock);
2165         net_dev->mtu = new_mtu;
2166         ef4_mac_reconfigure(efx);
2167         mutex_unlock(&efx->mac_lock);
2168
2169         ef4_start_all(efx);
2170         netif_device_attach(efx->net_dev);
2171         return 0;
2172 }
2173
2174 static int ef4_set_mac_address(struct net_device *net_dev, void *data)
2175 {
2176         struct ef4_nic *efx = netdev_priv(net_dev);
2177         struct sockaddr *addr = data;
2178         u8 *new_addr = addr->sa_data;
2179         u8 old_addr[6];
2180         int rc;
2181
2182         if (!is_valid_ether_addr(new_addr)) {
2183                 netif_err(efx, drv, efx->net_dev,
2184                           "invalid ethernet MAC address requested: %pM\n",
2185                           new_addr);
2186                 return -EADDRNOTAVAIL;
2187         }
2188
2189         /* save old address */
2190         ether_addr_copy(old_addr, net_dev->dev_addr);
2191         ether_addr_copy(net_dev->dev_addr, new_addr);
2192         if (efx->type->set_mac_address) {
2193                 rc = efx->type->set_mac_address(efx);
2194                 if (rc) {
2195                         ether_addr_copy(net_dev->dev_addr, old_addr);
2196                         return rc;
2197                 }
2198         }
2199
2200         /* Reconfigure the MAC */
2201         mutex_lock(&efx->mac_lock);
2202         ef4_mac_reconfigure(efx);
2203         mutex_unlock(&efx->mac_lock);
2204
2205         return 0;
2206 }
2207
2208 /* Context: netif_addr_lock held, BHs disabled. */
2209 static void ef4_set_rx_mode(struct net_device *net_dev)
2210 {
2211         struct ef4_nic *efx = netdev_priv(net_dev);
2212
2213         if (efx->port_enabled)
2214                 queue_work(efx->workqueue, &efx->mac_work);
2215         /* Otherwise ef4_start_port() will do this */
2216 }
2217
2218 static int ef4_set_features(struct net_device *net_dev, netdev_features_t data)
2219 {
2220         struct ef4_nic *efx = netdev_priv(net_dev);
2221         int rc;
2222
2223         /* If disabling RX n-tuple filtering, clear existing filters */
2224         if (net_dev->features & ~data & NETIF_F_NTUPLE) {
2225                 rc = efx->type->filter_clear_rx(efx, EF4_FILTER_PRI_MANUAL);
2226                 if (rc)
2227                         return rc;
2228         }
2229
2230         /* If Rx VLAN filter is changed, update filters via mac_reconfigure */
2231         if ((net_dev->features ^ data) & NETIF_F_HW_VLAN_CTAG_FILTER) {
2232                 /* ef4_set_rx_mode() will schedule MAC work to update filters
2233                  * when a new features are finally set in net_dev.
2234                  */
2235                 ef4_set_rx_mode(net_dev);
2236         }
2237
2238         return 0;
2239 }
2240
2241 static const struct net_device_ops ef4_netdev_ops = {
2242         .ndo_open               = ef4_net_open,
2243         .ndo_stop               = ef4_net_stop,
2244         .ndo_get_stats64        = ef4_net_stats,
2245         .ndo_tx_timeout         = ef4_watchdog,
2246         .ndo_start_xmit         = ef4_hard_start_xmit,
2247         .ndo_validate_addr      = eth_validate_addr,
2248         .ndo_do_ioctl           = ef4_ioctl,
2249         .ndo_change_mtu         = ef4_change_mtu,
2250         .ndo_set_mac_address    = ef4_set_mac_address,
2251         .ndo_set_rx_mode        = ef4_set_rx_mode,
2252         .ndo_set_features       = ef4_set_features,
2253 #ifdef CONFIG_NET_POLL_CONTROLLER
2254         .ndo_poll_controller = ef4_netpoll,
2255 #endif
2256         .ndo_setup_tc           = ef4_setup_tc,
2257 #ifdef CONFIG_RFS_ACCEL
2258         .ndo_rx_flow_steer      = ef4_filter_rfs,
2259 #endif
2260 };
2261
2262 static void ef4_update_name(struct ef4_nic *efx)
2263 {
2264         strcpy(efx->name, efx->net_dev->name);
2265         ef4_mtd_rename(efx);
2266         ef4_set_channel_names(efx);
2267 }
2268
2269 static int ef4_netdev_event(struct notifier_block *this,
2270                             unsigned long event, void *ptr)
2271 {
2272         struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
2273
2274         if ((net_dev->netdev_ops == &ef4_netdev_ops) &&
2275             event == NETDEV_CHANGENAME)
2276                 ef4_update_name(netdev_priv(net_dev));
2277
2278         return NOTIFY_DONE;
2279 }
2280
2281 static struct notifier_block ef4_netdev_notifier = {
2282         .notifier_call = ef4_netdev_event,
2283 };
2284
2285 static ssize_t
2286 show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
2287 {
2288         struct ef4_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2289         return sprintf(buf, "%d\n", efx->phy_type);
2290 }
2291 static DEVICE_ATTR(phy_type, 0444, show_phy_type, NULL);
2292
2293 static int ef4_register_netdev(struct ef4_nic *efx)
2294 {
2295         struct net_device *net_dev = efx->net_dev;
2296         struct ef4_channel *channel;
2297         int rc;
2298
2299         net_dev->watchdog_timeo = 5 * HZ;
2300         net_dev->irq = efx->pci_dev->irq;
2301         net_dev->netdev_ops = &ef4_netdev_ops;
2302         net_dev->ethtool_ops = &ef4_ethtool_ops;
2303         net_dev->gso_max_segs = EF4_TSO_MAX_SEGS;
2304         net_dev->min_mtu = EF4_MIN_MTU;
2305         net_dev->max_mtu = EF4_MAX_MTU;
2306
2307         rtnl_lock();
2308
2309         /* Enable resets to be scheduled and check whether any were
2310          * already requested.  If so, the NIC is probably hosed so we
2311          * abort.
2312          */
2313         efx->state = STATE_READY;
2314         smp_mb(); /* ensure we change state before checking reset_pending */
2315         if (efx->reset_pending) {
2316                 netif_err(efx, probe, efx->net_dev,
2317                           "aborting probe due to scheduled reset\n");
2318                 rc = -EIO;
2319                 goto fail_locked;
2320         }
2321
2322         rc = dev_alloc_name(net_dev, net_dev->name);
2323         if (rc < 0)
2324                 goto fail_locked;
2325         ef4_update_name(efx);
2326
2327         /* Always start with carrier off; PHY events will detect the link */
2328         netif_carrier_off(net_dev);
2329
2330         rc = register_netdevice(net_dev);
2331         if (rc)
2332                 goto fail_locked;
2333
2334         ef4_for_each_channel(channel, efx) {
2335                 struct ef4_tx_queue *tx_queue;
2336                 ef4_for_each_channel_tx_queue(tx_queue, channel)
2337                         ef4_init_tx_queue_core_txq(tx_queue);
2338         }
2339
2340         ef4_associate(efx);
2341
2342         rtnl_unlock();
2343
2344         rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2345         if (rc) {
2346                 netif_err(efx, drv, efx->net_dev,
2347                           "failed to init net dev attributes\n");
2348                 goto fail_registered;
2349         }
2350         return 0;
2351
2352 fail_registered:
2353         rtnl_lock();
2354         ef4_dissociate(efx);
2355         unregister_netdevice(net_dev);
2356 fail_locked:
2357         efx->state = STATE_UNINIT;
2358         rtnl_unlock();
2359         netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
2360         return rc;
2361 }
2362
2363 static void ef4_unregister_netdev(struct ef4_nic *efx)
2364 {
2365         if (!efx->net_dev)
2366                 return;
2367
2368         BUG_ON(netdev_priv(efx->net_dev) != efx);
2369
2370         if (ef4_dev_registered(efx)) {
2371                 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2372                 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2373                 unregister_netdev(efx->net_dev);
2374         }
2375 }
2376
2377 /**************************************************************************
2378  *
2379  * Device reset and suspend
2380  *
2381  **************************************************************************/
2382
2383 /* Tears down the entire software state and most of the hardware state
2384  * before reset.  */
2385 void ef4_reset_down(struct ef4_nic *efx, enum reset_type method)
2386 {
2387         EF4_ASSERT_RESET_SERIALISED(efx);
2388
2389         ef4_stop_all(efx);
2390         ef4_disable_interrupts(efx);
2391
2392         mutex_lock(&efx->mac_lock);
2393         if (efx->port_initialized && method != RESET_TYPE_INVISIBLE &&
2394             method != RESET_TYPE_DATAPATH)
2395                 efx->phy_op->fini(efx);
2396         efx->type->fini(efx);
2397 }
2398
2399 /* This function will always ensure that the locks acquired in
2400  * ef4_reset_down() are released. A failure return code indicates
2401  * that we were unable to reinitialise the hardware, and the
2402  * driver should be disabled. If ok is false, then the rx and tx
2403  * engines are not restarted, pending a RESET_DISABLE. */
2404 int ef4_reset_up(struct ef4_nic *efx, enum reset_type method, bool ok)
2405 {
2406         int rc;
2407
2408         EF4_ASSERT_RESET_SERIALISED(efx);
2409
2410         /* Ensure that SRAM is initialised even if we're disabling the device */
2411         rc = efx->type->init(efx);
2412         if (rc) {
2413                 netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
2414                 goto fail;
2415         }
2416
2417         if (!ok)
2418                 goto fail;
2419
2420         if (efx->port_initialized && method != RESET_TYPE_INVISIBLE &&
2421             method != RESET_TYPE_DATAPATH) {
2422                 rc = efx->phy_op->init(efx);
2423                 if (rc)
2424                         goto fail;
2425                 rc = efx->phy_op->reconfigure(efx);
2426                 if (rc && rc != -EPERM)
2427                         netif_err(efx, drv, efx->net_dev,
2428                                   "could not restore PHY settings\n");
2429         }
2430
2431         rc = ef4_enable_interrupts(efx);
2432         if (rc)
2433                 goto fail;
2434
2435         down_read(&efx->filter_sem);
2436         ef4_restore_filters(efx);
2437         up_read(&efx->filter_sem);
2438
2439         mutex_unlock(&efx->mac_lock);
2440
2441         ef4_start_all(efx);
2442
2443         return 0;
2444
2445 fail:
2446         efx->port_initialized = false;
2447
2448         mutex_unlock(&efx->mac_lock);
2449
2450         return rc;
2451 }
2452
2453 /* Reset the NIC using the specified method.  Note that the reset may
2454  * fail, in which case the card will be left in an unusable state.
2455  *
2456  * Caller must hold the rtnl_lock.
2457  */
2458 int ef4_reset(struct ef4_nic *efx, enum reset_type method)
2459 {
2460         int rc, rc2;
2461         bool disabled;
2462
2463         netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2464                    RESET_TYPE(method));
2465
2466         ef4_device_detach_sync(efx);
2467         ef4_reset_down(efx, method);
2468
2469         rc = efx->type->reset(efx, method);
2470         if (rc) {
2471                 netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
2472                 goto out;
2473         }
2474
2475         /* Clear flags for the scopes we covered.  We assume the NIC and
2476          * driver are now quiescent so that there is no race here.
2477          */
2478         if (method < RESET_TYPE_MAX_METHOD)
2479                 efx->reset_pending &= -(1 << (method + 1));
2480         else /* it doesn't fit into the well-ordered scope hierarchy */
2481                 __clear_bit(method, &efx->reset_pending);
2482
2483         /* Reinitialise bus-mastering, which may have been turned off before
2484          * the reset was scheduled. This is still appropriate, even in the
2485          * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2486          * can respond to requests. */
2487         pci_set_master(efx->pci_dev);
2488
2489 out:
2490         /* Leave device stopped if necessary */
2491         disabled = rc ||
2492                 method == RESET_TYPE_DISABLE ||
2493                 method == RESET_TYPE_RECOVER_OR_DISABLE;
2494         rc2 = ef4_reset_up(efx, method, !disabled);
2495         if (rc2) {
2496                 disabled = true;
2497                 if (!rc)
2498                         rc = rc2;
2499         }
2500
2501         if (disabled) {
2502                 dev_close(efx->net_dev);
2503                 netif_err(efx, drv, efx->net_dev, "has been disabled\n");
2504                 efx->state = STATE_DISABLED;
2505         } else {
2506                 netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
2507                 netif_device_attach(efx->net_dev);
2508         }
2509         return rc;
2510 }
2511
2512 /* Try recovery mechanisms.
2513  * For now only EEH is supported.
2514  * Returns 0 if the recovery mechanisms are unsuccessful.
2515  * Returns a non-zero value otherwise.
2516  */
2517 int ef4_try_recovery(struct ef4_nic *efx)
2518 {
2519 #ifdef CONFIG_EEH
2520         /* A PCI error can occur and not be seen by EEH because nothing
2521          * happens on the PCI bus. In this case the driver may fail and
2522          * schedule a 'recover or reset', leading to this recovery handler.
2523          * Manually call the eeh failure check function.
2524          */
2525         struct eeh_dev *eehdev = pci_dev_to_eeh_dev(efx->pci_dev);
2526         if (eeh_dev_check_failure(eehdev)) {
2527                 /* The EEH mechanisms will handle the error and reset the
2528                  * device if necessary.
2529                  */
2530                 return 1;
2531         }
2532 #endif
2533         return 0;
2534 }
2535
2536 /* The worker thread exists so that code that cannot sleep can
2537  * schedule a reset for later.
2538  */
2539 static void ef4_reset_work(struct work_struct *data)
2540 {
2541         struct ef4_nic *efx = container_of(data, struct ef4_nic, reset_work);
2542         unsigned long pending;
2543         enum reset_type method;
2544
2545         pending = READ_ONCE(efx->reset_pending);
2546         method = fls(pending) - 1;
2547
2548         if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
2549              method == RESET_TYPE_RECOVER_OR_ALL) &&
2550             ef4_try_recovery(efx))
2551                 return;
2552
2553         if (!pending)
2554                 return;
2555
2556         rtnl_lock();
2557
2558         /* We checked the state in ef4_schedule_reset() but it may
2559          * have changed by now.  Now that we have the RTNL lock,
2560          * it cannot change again.
2561          */
2562         if (efx->state == STATE_READY)
2563                 (void)ef4_reset(efx, method);
2564
2565         rtnl_unlock();
2566 }
2567
2568 void ef4_schedule_reset(struct ef4_nic *efx, enum reset_type type)
2569 {
2570         enum reset_type method;
2571
2572         if (efx->state == STATE_RECOVERY) {
2573                 netif_dbg(efx, drv, efx->net_dev,
2574                           "recovering: skip scheduling %s reset\n",
2575                           RESET_TYPE(type));
2576                 return;
2577         }
2578
2579         switch (type) {
2580         case RESET_TYPE_INVISIBLE:
2581         case RESET_TYPE_ALL:
2582         case RESET_TYPE_RECOVER_OR_ALL:
2583         case RESET_TYPE_WORLD:
2584         case RESET_TYPE_DISABLE:
2585         case RESET_TYPE_RECOVER_OR_DISABLE:
2586         case RESET_TYPE_DATAPATH:
2587                 method = type;
2588                 netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2589                           RESET_TYPE(method));
2590                 break;
2591         default:
2592                 method = efx->type->map_reset_reason(type);
2593                 netif_dbg(efx, drv, efx->net_dev,
2594                           "scheduling %s reset for %s\n",
2595                           RESET_TYPE(method), RESET_TYPE(type));
2596                 break;
2597         }
2598
2599         set_bit(method, &efx->reset_pending);
2600         smp_mb(); /* ensure we change reset_pending before checking state */
2601
2602         /* If we're not READY then just leave the flags set as the cue
2603          * to abort probing or reschedule the reset later.
2604          */
2605         if (READ_ONCE(efx->state) != STATE_READY)
2606                 return;
2607
2608         queue_work(reset_workqueue, &efx->reset_work);
2609 }
2610
2611 /**************************************************************************
2612  *
2613  * List of NICs we support
2614  *
2615  **************************************************************************/
2616
2617 /* PCI device ID table */
2618 static const struct pci_device_id ef4_pci_table[] = {
2619         {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2620                     PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
2621          .driver_data = (unsigned long) &falcon_a1_nic_type},
2622         {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2623                     PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
2624          .driver_data = (unsigned long) &falcon_b0_nic_type},
2625         {0}                     /* end of list */
2626 };
2627
2628 /**************************************************************************
2629  *
2630  * Dummy PHY/MAC operations
2631  *
2632  * Can be used for some unimplemented operations
2633  * Needed so all function pointers are valid and do not have to be tested
2634  * before use
2635  *
2636  **************************************************************************/
2637 int ef4_port_dummy_op_int(struct ef4_nic *efx)
2638 {
2639         return 0;
2640 }
2641 void ef4_port_dummy_op_void(struct ef4_nic *efx) {}
2642
2643 static bool ef4_port_dummy_op_poll(struct ef4_nic *efx)
2644 {
2645         return false;
2646 }
2647
2648 static const struct ef4_phy_operations ef4_dummy_phy_operations = {
2649         .init            = ef4_port_dummy_op_int,
2650         .reconfigure     = ef4_port_dummy_op_int,
2651         .poll            = ef4_port_dummy_op_poll,
2652         .fini            = ef4_port_dummy_op_void,
2653 };
2654
2655 /**************************************************************************
2656  *
2657  * Data housekeeping
2658  *
2659  **************************************************************************/
2660
2661 /* This zeroes out and then fills in the invariants in a struct
2662  * ef4_nic (including all sub-structures).
2663  */
2664 static int ef4_init_struct(struct ef4_nic *efx,
2665                            struct pci_dev *pci_dev, struct net_device *net_dev)
2666 {
2667         int i;
2668
2669         /* Initialise common structures */
2670         INIT_LIST_HEAD(&efx->node);
2671         INIT_LIST_HEAD(&efx->secondary_list);
2672         spin_lock_init(&efx->biu_lock);
2673 #ifdef CONFIG_SFC_FALCON_MTD
2674         INIT_LIST_HEAD(&efx->mtd_list);
2675 #endif
2676         INIT_WORK(&efx->reset_work, ef4_reset_work);
2677         INIT_DELAYED_WORK(&efx->monitor_work, ef4_monitor);
2678         INIT_DELAYED_WORK(&efx->selftest_work, ef4_selftest_async_work);
2679         efx->pci_dev = pci_dev;
2680         efx->msg_enable = debug;
2681         efx->state = STATE_UNINIT;
2682         strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
2683
2684         efx->net_dev = net_dev;
2685         efx->rx_prefix_size = efx->type->rx_prefix_size;
2686         efx->rx_ip_align =
2687                 NET_IP_ALIGN ? (efx->rx_prefix_size + NET_IP_ALIGN) % 4 : 0;
2688         efx->rx_packet_hash_offset =
2689                 efx->type->rx_hash_offset - efx->type->rx_prefix_size;
2690         efx->rx_packet_ts_offset =
2691                 efx->type->rx_ts_offset - efx->type->rx_prefix_size;
2692         spin_lock_init(&efx->stats_lock);
2693         mutex_init(&efx->mac_lock);
2694         efx->phy_op = &ef4_dummy_phy_operations;
2695         efx->mdio.dev = net_dev;
2696         INIT_WORK(&efx->mac_work, ef4_mac_work);
2697         init_waitqueue_head(&efx->flush_wq);
2698
2699         for (i = 0; i < EF4_MAX_CHANNELS; i++) {
2700                 efx->channel[i] = ef4_alloc_channel(efx, i, NULL);
2701                 if (!efx->channel[i])
2702                         goto fail;
2703                 efx->msi_context[i].efx = efx;
2704                 efx->msi_context[i].index = i;
2705         }
2706
2707         /* Higher numbered interrupt modes are less capable! */
2708         efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2709                                   interrupt_mode);
2710
2711         /* Would be good to use the net_dev name, but we're too early */
2712         snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2713                  pci_name(pci_dev));
2714         efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
2715         if (!efx->workqueue)
2716                 goto fail;
2717
2718         return 0;
2719
2720 fail:
2721         ef4_fini_struct(efx);
2722         return -ENOMEM;
2723 }
2724
2725 static void ef4_fini_struct(struct ef4_nic *efx)
2726 {
2727         int i;
2728
2729         for (i = 0; i < EF4_MAX_CHANNELS; i++)
2730                 kfree(efx->channel[i]);
2731
2732         kfree(efx->vpd_sn);
2733
2734         if (efx->workqueue) {
2735                 destroy_workqueue(efx->workqueue);
2736                 efx->workqueue = NULL;
2737         }
2738 }
2739
2740 void ef4_update_sw_stats(struct ef4_nic *efx, u64 *stats)
2741 {
2742         u64 n_rx_nodesc_trunc = 0;
2743         struct ef4_channel *channel;
2744
2745         ef4_for_each_channel(channel, efx)
2746                 n_rx_nodesc_trunc += channel->n_rx_nodesc_trunc;
2747         stats[GENERIC_STAT_rx_nodesc_trunc] = n_rx_nodesc_trunc;
2748         stats[GENERIC_STAT_rx_noskb_drops] = atomic_read(&efx->n_rx_noskb_drops);
2749 }
2750
2751 /**************************************************************************
2752  *
2753  * PCI interface
2754  *
2755  **************************************************************************/
2756
2757 /* Main body of final NIC shutdown code
2758  * This is called only at module unload (or hotplug removal).
2759  */
2760 static void ef4_pci_remove_main(struct ef4_nic *efx)
2761 {
2762         /* Flush reset_work. It can no longer be scheduled since we
2763          * are not READY.
2764          */
2765         BUG_ON(efx->state == STATE_READY);
2766         cancel_work_sync(&efx->reset_work);
2767
2768         ef4_disable_interrupts(efx);
2769         ef4_nic_fini_interrupt(efx);
2770         ef4_fini_port(efx);
2771         efx->type->fini(efx);
2772         ef4_fini_napi(efx);
2773         ef4_remove_all(efx);
2774 }
2775
2776 /* Final NIC shutdown
2777  * This is called only at module unload (or hotplug removal).  A PF can call
2778  * this on its VFs to ensure they are unbound first.
2779  */
2780 static void ef4_pci_remove(struct pci_dev *pci_dev)
2781 {
2782         struct ef4_nic *efx;
2783
2784         efx = pci_get_drvdata(pci_dev);
2785         if (!efx)
2786                 return;
2787
2788         /* Mark the NIC as fini, then stop the interface */
2789         rtnl_lock();
2790         ef4_dissociate(efx);
2791         dev_close(efx->net_dev);
2792         ef4_disable_interrupts(efx);
2793         efx->state = STATE_UNINIT;
2794         rtnl_unlock();
2795
2796         ef4_unregister_netdev(efx);
2797
2798         ef4_mtd_remove(efx);
2799
2800         ef4_pci_remove_main(efx);
2801
2802         ef4_fini_io(efx);
2803         netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
2804
2805         ef4_fini_struct(efx);
2806         free_netdev(efx->net_dev);
2807
2808         pci_disable_pcie_error_reporting(pci_dev);
2809 };
2810
2811 /* NIC VPD information
2812  * Called during probe to display the part number of the
2813  * installed NIC.  VPD is potentially very large but this should
2814  * always appear within the first 512 bytes.
2815  */
2816 #define SFC_VPD_LEN 512
2817 static void ef4_probe_vpd_strings(struct ef4_nic *efx)
2818 {
2819         struct pci_dev *dev = efx->pci_dev;
2820         char vpd_data[SFC_VPD_LEN];
2821         ssize_t vpd_size;
2822         int ro_start, ro_size, i, j;
2823
2824         /* Get the vpd data from the device */
2825         vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
2826         if (vpd_size <= 0) {
2827                 netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
2828                 return;
2829         }
2830
2831         /* Get the Read only section */
2832         ro_start = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
2833         if (ro_start < 0) {
2834                 netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
2835                 return;
2836         }
2837
2838         ro_size = pci_vpd_lrdt_size(&vpd_data[ro_start]);
2839         j = ro_size;
2840         i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
2841         if (i + j > vpd_size)
2842                 j = vpd_size - i;
2843
2844         /* Get the Part number */
2845         i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
2846         if (i < 0) {
2847                 netif_err(efx, drv, efx->net_dev, "Part number not found\n");
2848                 return;
2849         }
2850
2851         j = pci_vpd_info_field_size(&vpd_data[i]);
2852         i += PCI_VPD_INFO_FLD_HDR_SIZE;
2853         if (i + j > vpd_size) {
2854                 netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
2855                 return;
2856         }
2857
2858         netif_info(efx, drv, efx->net_dev,
2859                    "Part Number : %.*s\n", j, &vpd_data[i]);
2860
2861         i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
2862         j = ro_size;
2863         i = pci_vpd_find_info_keyword(vpd_data, i, j, "SN");
2864         if (i < 0) {
2865                 netif_err(efx, drv, efx->net_dev, "Serial number not found\n");
2866                 return;
2867         }
2868
2869         j = pci_vpd_info_field_size(&vpd_data[i]);
2870         i += PCI_VPD_INFO_FLD_HDR_SIZE;
2871         if (i + j > vpd_size) {
2872                 netif_err(efx, drv, efx->net_dev, "Incomplete serial number\n");
2873                 return;
2874         }
2875
2876         efx->vpd_sn = kmalloc(j + 1, GFP_KERNEL);
2877         if (!efx->vpd_sn)
2878                 return;
2879
2880         snprintf(efx->vpd_sn, j + 1, "%s", &vpd_data[i]);
2881 }
2882
2883
2884 /* Main body of NIC initialisation
2885  * This is called at module load (or hotplug insertion, theoretically).
2886  */
2887 static int ef4_pci_probe_main(struct ef4_nic *efx)
2888 {
2889         int rc;
2890
2891         /* Do start-of-day initialisation */
2892         rc = ef4_probe_all(efx);
2893         if (rc)
2894                 goto fail1;
2895
2896         ef4_init_napi(efx);
2897
2898         rc = efx->type->init(efx);
2899         if (rc) {
2900                 netif_err(efx, probe, efx->net_dev,
2901                           "failed to initialise NIC\n");
2902                 goto fail3;
2903         }
2904
2905         rc = ef4_init_port(efx);
2906         if (rc) {
2907                 netif_err(efx, probe, efx->net_dev,
2908                           "failed to initialise port\n");
2909                 goto fail4;
2910         }
2911
2912         rc = ef4_nic_init_interrupt(efx);
2913         if (rc)
2914                 goto fail5;
2915         rc = ef4_enable_interrupts(efx);
2916         if (rc)
2917                 goto fail6;
2918
2919         return 0;
2920
2921  fail6:
2922         ef4_nic_fini_interrupt(efx);
2923  fail5:
2924         ef4_fini_port(efx);
2925  fail4:
2926         efx->type->fini(efx);
2927  fail3:
2928         ef4_fini_napi(efx);
2929         ef4_remove_all(efx);
2930  fail1:
2931         return rc;
2932 }
2933
2934 /* NIC initialisation
2935  *
2936  * This is called at module load (or hotplug insertion,
2937  * theoretically).  It sets up PCI mappings, resets the NIC,
2938  * sets up and registers the network devices with the kernel and hooks
2939  * the interrupt service routine.  It does not prepare the device for
2940  * transmission; this is left to the first time one of the network
2941  * interfaces is brought up (i.e. ef4_net_open).
2942  */
2943 static int ef4_pci_probe(struct pci_dev *pci_dev,
2944                          const struct pci_device_id *entry)
2945 {
2946         struct net_device *net_dev;
2947         struct ef4_nic *efx;
2948         int rc;
2949
2950         /* Allocate and initialise a struct net_device and struct ef4_nic */
2951         net_dev = alloc_etherdev_mqs(sizeof(*efx), EF4_MAX_CORE_TX_QUEUES,
2952                                      EF4_MAX_RX_QUEUES);
2953         if (!net_dev)
2954                 return -ENOMEM;
2955         efx = netdev_priv(net_dev);
2956         efx->type = (const struct ef4_nic_type *) entry->driver_data;
2957         efx->fixed_features |= NETIF_F_HIGHDMA;
2958
2959         pci_set_drvdata(pci_dev, efx);
2960         SET_NETDEV_DEV(net_dev, &pci_dev->dev);
2961         rc = ef4_init_struct(efx, pci_dev, net_dev);
2962         if (rc)
2963                 goto fail1;
2964
2965         netif_info(efx, probe, efx->net_dev,
2966                    "Solarflare NIC detected\n");
2967
2968         ef4_probe_vpd_strings(efx);
2969
2970         /* Set up basic I/O (BAR mappings etc) */
2971         rc = ef4_init_io(efx);
2972         if (rc)
2973                 goto fail2;
2974
2975         rc = ef4_pci_probe_main(efx);
2976         if (rc)
2977                 goto fail3;
2978
2979         net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
2980                               NETIF_F_RXCSUM);
2981         /* Mask for features that also apply to VLAN devices */
2982         net_dev->vlan_features |= (NETIF_F_HW_CSUM | NETIF_F_SG |
2983                                    NETIF_F_HIGHDMA | NETIF_F_RXCSUM);
2984
2985         net_dev->hw_features = net_dev->features & ~efx->fixed_features;
2986
2987         /* Disable VLAN filtering by default.  It may be enforced if
2988          * the feature is fixed (i.e. VLAN filters are required to
2989          * receive VLAN tagged packets due to vPort restrictions).
2990          */
2991         net_dev->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
2992         net_dev->features |= efx->fixed_features;
2993
2994         rc = ef4_register_netdev(efx);
2995         if (rc)
2996                 goto fail4;
2997
2998         netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
2999
3000         /* Try to create MTDs, but allow this to fail */
3001         rtnl_lock();
3002         rc = ef4_mtd_probe(efx);
3003         rtnl_unlock();
3004         if (rc && rc != -EPERM)
3005                 netif_warn(efx, probe, efx->net_dev,
3006                            "failed to create MTDs (%d)\n", rc);
3007
3008         rc = pci_enable_pcie_error_reporting(pci_dev);
3009         if (rc && rc != -EINVAL)
3010                 netif_notice(efx, probe, efx->net_dev,
3011                              "PCIE error reporting unavailable (%d).\n",
3012                              rc);
3013
3014         return 0;
3015
3016  fail4:
3017         ef4_pci_remove_main(efx);
3018  fail3:
3019         ef4_fini_io(efx);
3020  fail2:
3021         ef4_fini_struct(efx);
3022  fail1:
3023         WARN_ON(rc > 0);
3024         netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
3025         free_netdev(net_dev);
3026         return rc;
3027 }
3028
3029 static int ef4_pm_freeze(struct device *dev)
3030 {
3031         struct ef4_nic *efx = pci_get_drvdata(to_pci_dev(dev));
3032
3033         rtnl_lock();
3034
3035         if (efx->state != STATE_DISABLED) {
3036                 efx->state = STATE_UNINIT;
3037
3038                 ef4_device_detach_sync(efx);
3039
3040                 ef4_stop_all(efx);
3041                 ef4_disable_interrupts(efx);
3042         }
3043
3044         rtnl_unlock();
3045
3046         return 0;
3047 }
3048
3049 static int ef4_pm_thaw(struct device *dev)
3050 {
3051         int rc;
3052         struct ef4_nic *efx = pci_get_drvdata(to_pci_dev(dev));
3053
3054         rtnl_lock();
3055
3056         if (efx->state != STATE_DISABLED) {
3057                 rc = ef4_enable_interrupts(efx);
3058                 if (rc)
3059                         goto fail;
3060
3061                 mutex_lock(&efx->mac_lock);
3062                 efx->phy_op->reconfigure(efx);
3063                 mutex_unlock(&efx->mac_lock);
3064
3065                 ef4_start_all(efx);
3066
3067                 netif_device_attach(efx->net_dev);
3068
3069                 efx->state = STATE_READY;
3070
3071                 efx->type->resume_wol(efx);
3072         }
3073
3074         rtnl_unlock();
3075
3076         /* Reschedule any quenched resets scheduled during ef4_pm_freeze() */
3077         queue_work(reset_workqueue, &efx->reset_work);
3078
3079         return 0;
3080
3081 fail:
3082         rtnl_unlock();
3083
3084         return rc;
3085 }
3086
3087 static int ef4_pm_poweroff(struct device *dev)
3088 {
3089         struct pci_dev *pci_dev = to_pci_dev(dev);
3090         struct ef4_nic *efx = pci_get_drvdata(pci_dev);
3091
3092         efx->type->fini(efx);
3093
3094         efx->reset_pending = 0;
3095
3096         pci_save_state(pci_dev);
3097         return pci_set_power_state(pci_dev, PCI_D3hot);
3098 }
3099
3100 /* Used for both resume and restore */
3101 static int ef4_pm_resume(struct device *dev)
3102 {
3103         struct pci_dev *pci_dev = to_pci_dev(dev);
3104         struct ef4_nic *efx = pci_get_drvdata(pci_dev);
3105         int rc;
3106
3107         rc = pci_set_power_state(pci_dev, PCI_D0);
3108         if (rc)
3109                 return rc;
3110         pci_restore_state(pci_dev);
3111         rc = pci_enable_device(pci_dev);
3112         if (rc)
3113                 return rc;
3114         pci_set_master(efx->pci_dev);
3115         rc = efx->type->reset(efx, RESET_TYPE_ALL);
3116         if (rc)
3117                 return rc;
3118         rc = efx->type->init(efx);
3119         if (rc)
3120                 return rc;
3121         rc = ef4_pm_thaw(dev);
3122         return rc;
3123 }
3124
3125 static int ef4_pm_suspend(struct device *dev)
3126 {
3127         int rc;
3128
3129         ef4_pm_freeze(dev);
3130         rc = ef4_pm_poweroff(dev);
3131         if (rc)
3132                 ef4_pm_resume(dev);
3133         return rc;
3134 }
3135
3136 static const struct dev_pm_ops ef4_pm_ops = {
3137         .suspend        = ef4_pm_suspend,
3138         .resume         = ef4_pm_resume,
3139         .freeze         = ef4_pm_freeze,
3140         .thaw           = ef4_pm_thaw,
3141         .poweroff       = ef4_pm_poweroff,
3142         .restore        = ef4_pm_resume,
3143 };
3144
3145 /* A PCI error affecting this device was detected.
3146  * At this point MMIO and DMA may be disabled.
3147  * Stop the software path and request a slot reset.
3148  */
3149 static pci_ers_result_t ef4_io_error_detected(struct pci_dev *pdev,
3150                                               enum pci_channel_state state)
3151 {
3152         pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
3153         struct ef4_nic *efx = pci_get_drvdata(pdev);
3154
3155         if (state == pci_channel_io_perm_failure)
3156                 return PCI_ERS_RESULT_DISCONNECT;
3157
3158         rtnl_lock();
3159
3160         if (efx->state != STATE_DISABLED) {
3161                 efx->state = STATE_RECOVERY;
3162                 efx->reset_pending = 0;
3163
3164                 ef4_device_detach_sync(efx);
3165
3166                 ef4_stop_all(efx);
3167                 ef4_disable_interrupts(efx);
3168
3169                 status = PCI_ERS_RESULT_NEED_RESET;
3170         } else {
3171                 /* If the interface is disabled we don't want to do anything
3172                  * with it.
3173                  */
3174                 status = PCI_ERS_RESULT_RECOVERED;
3175         }
3176
3177         rtnl_unlock();
3178
3179         pci_disable_device(pdev);
3180
3181         return status;
3182 }
3183
3184 /* Fake a successful reset, which will be performed later in ef4_io_resume. */
3185 static pci_ers_result_t ef4_io_slot_reset(struct pci_dev *pdev)
3186 {
3187         struct ef4_nic *efx = pci_get_drvdata(pdev);
3188         pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
3189         int rc;
3190
3191         if (pci_enable_device(pdev)) {
3192                 netif_err(efx, hw, efx->net_dev,
3193                           "Cannot re-enable PCI device after reset.\n");
3194                 status =  PCI_ERS_RESULT_DISCONNECT;
3195         }
3196
3197         rc = pci_cleanup_aer_uncorrect_error_status(pdev);
3198         if (rc) {
3199                 netif_err(efx, hw, efx->net_dev,
3200                 "pci_cleanup_aer_uncorrect_error_status failed (%d)\n", rc);
3201                 /* Non-fatal error. Continue. */
3202         }
3203
3204         return status;
3205 }
3206
3207 /* Perform the actual reset and resume I/O operations. */
3208 static void ef4_io_resume(struct pci_dev *pdev)
3209 {
3210         struct ef4_nic *efx = pci_get_drvdata(pdev);
3211         int rc;
3212
3213         rtnl_lock();
3214
3215         if (efx->state == STATE_DISABLED)
3216                 goto out;
3217
3218         rc = ef4_reset(efx, RESET_TYPE_ALL);
3219         if (rc) {
3220                 netif_err(efx, hw, efx->net_dev,
3221                           "ef4_reset failed after PCI error (%d)\n", rc);
3222         } else {
3223                 efx->state = STATE_READY;
3224                 netif_dbg(efx, hw, efx->net_dev,
3225                           "Done resetting and resuming IO after PCI error.\n");
3226         }
3227
3228 out:
3229         rtnl_unlock();
3230 }
3231
3232 /* For simplicity and reliability, we always require a slot reset and try to
3233  * reset the hardware when a pci error affecting the device is detected.
3234  * We leave both the link_reset and mmio_enabled callback unimplemented:
3235  * with our request for slot reset the mmio_enabled callback will never be
3236  * called, and the link_reset callback is not used by AER or EEH mechanisms.
3237  */
3238 static const struct pci_error_handlers ef4_err_handlers = {
3239         .error_detected = ef4_io_error_detected,
3240         .slot_reset     = ef4_io_slot_reset,
3241         .resume         = ef4_io_resume,
3242 };
3243
3244 static struct pci_driver ef4_pci_driver = {
3245         .name           = KBUILD_MODNAME,
3246         .id_table       = ef4_pci_table,
3247         .probe          = ef4_pci_probe,
3248         .remove         = ef4_pci_remove,
3249         .driver.pm      = &ef4_pm_ops,
3250         .err_handler    = &ef4_err_handlers,
3251 };
3252
3253 /**************************************************************************
3254  *
3255  * Kernel module interface
3256  *
3257  *************************************************************************/
3258
3259 module_param(interrupt_mode, uint, 0444);
3260 MODULE_PARM_DESC(interrupt_mode,
3261                  "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
3262
3263 static int __init ef4_init_module(void)
3264 {
3265         int rc;
3266
3267         printk(KERN_INFO "Solarflare Falcon driver v" EF4_DRIVER_VERSION "\n");
3268
3269         rc = register_netdevice_notifier(&ef4_netdev_notifier);
3270         if (rc)
3271                 goto err_notifier;
3272
3273         reset_workqueue = create_singlethread_workqueue("sfc_reset");
3274         if (!reset_workqueue) {
3275                 rc = -ENOMEM;
3276                 goto err_reset;
3277         }
3278
3279         rc = pci_register_driver(&ef4_pci_driver);
3280         if (rc < 0)
3281                 goto err_pci;
3282
3283         return 0;
3284
3285  err_pci:
3286         destroy_workqueue(reset_workqueue);
3287  err_reset:
3288         unregister_netdevice_notifier(&ef4_netdev_notifier);
3289  err_notifier:
3290         return rc;
3291 }
3292
3293 static void __exit ef4_exit_module(void)
3294 {
3295         printk(KERN_INFO "Solarflare Falcon driver unloading\n");
3296
3297         pci_unregister_driver(&ef4_pci_driver);
3298         destroy_workqueue(reset_workqueue);
3299         unregister_netdevice_notifier(&ef4_netdev_notifier);
3300
3301 }
3302
3303 module_init(ef4_init_module);
3304 module_exit(ef4_exit_module);
3305
3306 MODULE_AUTHOR("Solarflare Communications and "
3307               "Michael Brown <mbrown@fensystems.co.uk>");
3308 MODULE_DESCRIPTION("Solarflare Falcon network driver");
3309 MODULE_LICENSE("GPL");
3310 MODULE_DEVICE_TABLE(pci, ef4_pci_table);
3311 MODULE_VERSION(EF4_DRIVER_VERSION);