GNU Linux-libre 4.9.333-gnu1
[releases.git] / drivers / net / ethernet / netronome / nfp / nfp_net_common.c
1 /*
2  * Copyright (C) 2015 Netronome Systems, Inc.
3  *
4  * This software is dual licensed under the GNU General License Version 2,
5  * June 1991 as shown in the file COPYING in the top-level directory of this
6  * source tree or the BSD 2-Clause License provided below.  You have the
7  * option to license this software under the complete terms of either license.
8  *
9  * The BSD 2-Clause License:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      1. Redistributions of source code must retain the above
16  *         copyright notice, this list of conditions and the following
17  *         disclaimer.
18  *
19  *      2. Redistributions in binary form must reproduce the above
20  *         copyright notice, this list of conditions and the following
21  *         disclaimer in the documentation and/or other materials
22  *         provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33
34 /*
35  * nfp_net_common.c
36  * Netronome network device driver: Common functions between PF and VF
37  * Authors: Jakub Kicinski <jakub.kicinski@netronome.com>
38  *          Jason McMullan <jason.mcmullan@netronome.com>
39  *          Rolf Neugebauer <rolf.neugebauer@netronome.com>
40  *          Brad Petrus <brad.petrus@netronome.com>
41  *          Chris Telfer <chris.telfer@netronome.com>
42  */
43
44 #include <linux/module.h>
45 #include <linux/kernel.h>
46 #include <linux/init.h>
47 #include <linux/fs.h>
48 #include <linux/netdevice.h>
49 #include <linux/etherdevice.h>
50 #include <linux/interrupt.h>
51 #include <linux/ip.h>
52 #include <linux/ipv6.h>
53 #include <linux/pci.h>
54 #include <linux/pci_regs.h>
55 #include <linux/msi.h>
56 #include <linux/ethtool.h>
57 #include <linux/log2.h>
58 #include <linux/if_vlan.h>
59 #include <linux/random.h>
60
61 #include <linux/ktime.h>
62
63 #include <net/pkt_cls.h>
64 #include <net/vxlan.h>
65
66 #include "nfp_net_ctrl.h"
67 #include "nfp_net.h"
68
69 /**
70  * nfp_net_get_fw_version() - Read and parse the FW version
71  * @fw_ver:     Output fw_version structure to read to
72  * @ctrl_bar:   Mapped address of the control BAR
73  */
74 void nfp_net_get_fw_version(struct nfp_net_fw_version *fw_ver,
75                             void __iomem *ctrl_bar)
76 {
77         u32 reg;
78
79         reg = readl(ctrl_bar + NFP_NET_CFG_VERSION);
80         put_unaligned_le32(reg, fw_ver);
81 }
82
83 /* Firmware reconfig
84  *
85  * Firmware reconfig may take a while so we have two versions of it -
86  * synchronous and asynchronous (posted).  All synchronous callers are holding
87  * RTNL so we don't have to worry about serializing them.
88  */
89 static void nfp_net_reconfig_start(struct nfp_net *nn, u32 update)
90 {
91         nn_writel(nn, NFP_NET_CFG_UPDATE, update);
92         /* ensure update is written before pinging HW */
93         nn_pci_flush(nn);
94         nfp_qcp_wr_ptr_add(nn->qcp_cfg, 1);
95 }
96
97 /* Pass 0 as update to run posted reconfigs. */
98 static void nfp_net_reconfig_start_async(struct nfp_net *nn, u32 update)
99 {
100         update |= nn->reconfig_posted;
101         nn->reconfig_posted = 0;
102
103         nfp_net_reconfig_start(nn, update);
104
105         nn->reconfig_timer_active = true;
106         mod_timer(&nn->reconfig_timer, jiffies + NFP_NET_POLL_TIMEOUT * HZ);
107 }
108
109 static bool nfp_net_reconfig_check_done(struct nfp_net *nn, bool last_check)
110 {
111         u32 reg;
112
113         reg = nn_readl(nn, NFP_NET_CFG_UPDATE);
114         if (reg == 0)
115                 return true;
116         if (reg & NFP_NET_CFG_UPDATE_ERR) {
117                 nn_err(nn, "Reconfig error: 0x%08x\n", reg);
118                 return true;
119         } else if (last_check) {
120                 nn_err(nn, "Reconfig timeout: 0x%08x\n", reg);
121                 return true;
122         }
123
124         return false;
125 }
126
127 static int nfp_net_reconfig_wait(struct nfp_net *nn, unsigned long deadline)
128 {
129         bool timed_out = false;
130
131         /* Poll update field, waiting for NFP to ack the config */
132         while (!nfp_net_reconfig_check_done(nn, timed_out)) {
133                 msleep(1);
134                 timed_out = time_is_before_eq_jiffies(deadline);
135         }
136
137         if (nn_readl(nn, NFP_NET_CFG_UPDATE) & NFP_NET_CFG_UPDATE_ERR)
138                 return -EIO;
139
140         return timed_out ? -EIO : 0;
141 }
142
143 static void nfp_net_reconfig_timer(unsigned long data)
144 {
145         struct nfp_net *nn = (void *)data;
146
147         spin_lock_bh(&nn->reconfig_lock);
148
149         nn->reconfig_timer_active = false;
150
151         /* If sync caller is present it will take over from us */
152         if (nn->reconfig_sync_present)
153                 goto done;
154
155         /* Read reconfig status and report errors */
156         nfp_net_reconfig_check_done(nn, true);
157
158         if (nn->reconfig_posted)
159                 nfp_net_reconfig_start_async(nn, 0);
160 done:
161         spin_unlock_bh(&nn->reconfig_lock);
162 }
163
164 /**
165  * nfp_net_reconfig_post() - Post async reconfig request
166  * @nn:      NFP Net device to reconfigure
167  * @update:  The value for the update field in the BAR config
168  *
169  * Record FW reconfiguration request.  Reconfiguration will be kicked off
170  * whenever reconfiguration machinery is idle.  Multiple requests can be
171  * merged together!
172  */
173 static void nfp_net_reconfig_post(struct nfp_net *nn, u32 update)
174 {
175         spin_lock_bh(&nn->reconfig_lock);
176
177         /* Sync caller will kick off async reconf when it's done, just post */
178         if (nn->reconfig_sync_present) {
179                 nn->reconfig_posted |= update;
180                 goto done;
181         }
182
183         /* Opportunistically check if the previous command is done */
184         if (!nn->reconfig_timer_active ||
185             nfp_net_reconfig_check_done(nn, false))
186                 nfp_net_reconfig_start_async(nn, update);
187         else
188                 nn->reconfig_posted |= update;
189 done:
190         spin_unlock_bh(&nn->reconfig_lock);
191 }
192
193 /**
194  * nfp_net_reconfig() - Reconfigure the firmware
195  * @nn:      NFP Net device to reconfigure
196  * @update:  The value for the update field in the BAR config
197  *
198  * Write the update word to the BAR and ping the reconfig queue.  The
199  * poll until the firmware has acknowledged the update by zeroing the
200  * update word.
201  *
202  * Return: Negative errno on error, 0 on success
203  */
204 int nfp_net_reconfig(struct nfp_net *nn, u32 update)
205 {
206         bool cancelled_timer = false;
207         u32 pre_posted_requests;
208         int ret;
209
210         spin_lock_bh(&nn->reconfig_lock);
211
212         nn->reconfig_sync_present = true;
213
214         if (nn->reconfig_timer_active) {
215                 del_timer(&nn->reconfig_timer);
216                 nn->reconfig_timer_active = false;
217                 cancelled_timer = true;
218         }
219         pre_posted_requests = nn->reconfig_posted;
220         nn->reconfig_posted = 0;
221
222         spin_unlock_bh(&nn->reconfig_lock);
223
224         if (cancelled_timer)
225                 nfp_net_reconfig_wait(nn, nn->reconfig_timer.expires);
226
227         /* Run the posted reconfigs which were issued before we started */
228         if (pre_posted_requests) {
229                 nfp_net_reconfig_start(nn, pre_posted_requests);
230                 nfp_net_reconfig_wait(nn, jiffies + HZ * NFP_NET_POLL_TIMEOUT);
231         }
232
233         nfp_net_reconfig_start(nn, update);
234         ret = nfp_net_reconfig_wait(nn, jiffies + HZ * NFP_NET_POLL_TIMEOUT);
235
236         spin_lock_bh(&nn->reconfig_lock);
237
238         if (nn->reconfig_posted)
239                 nfp_net_reconfig_start_async(nn, 0);
240
241         nn->reconfig_sync_present = false;
242
243         spin_unlock_bh(&nn->reconfig_lock);
244
245         return ret;
246 }
247
248 /* Interrupt configuration and handling
249  */
250
251 /**
252  * nfp_net_irq_unmask_msix() - Unmask MSI-X after automasking
253  * @nn:       NFP Network structure
254  * @entry_nr: MSI-X table entry
255  *
256  * Clear the MSI-X table mask bit for the given entry bypassing Linux irq
257  * handling subsystem.  Use *only* to reenable automasked vectors.
258  */
259 static void nfp_net_irq_unmask_msix(struct nfp_net *nn, unsigned int entry_nr)
260 {
261         struct list_head *msi_head = &nn->pdev->dev.msi_list;
262         struct msi_desc *entry;
263         u32 off;
264
265         /* All MSI-Xs have the same mask_base */
266         entry = list_first_entry(msi_head, struct msi_desc, list);
267
268         off = (PCI_MSIX_ENTRY_SIZE * entry_nr) +
269                 PCI_MSIX_ENTRY_VECTOR_CTRL;
270         writel(0, entry->mask_base + off);
271         readl(entry->mask_base);
272 }
273
274 /**
275  * nfp_net_irq_unmask() - Unmask automasked interrupt
276  * @nn:       NFP Network structure
277  * @entry_nr: MSI-X table entry
278  *
279  * If MSI-X auto-masking is enabled clear the mask bit, otherwise
280  * clear the ICR for the entry.
281  */
282 static void nfp_net_irq_unmask(struct nfp_net *nn, unsigned int entry_nr)
283 {
284         if (nn->ctrl & NFP_NET_CFG_CTRL_MSIXAUTO) {
285                 nfp_net_irq_unmask_msix(nn, entry_nr);
286                 return;
287         }
288
289         nn_writeb(nn, NFP_NET_CFG_ICR(entry_nr), NFP_NET_CFG_ICR_UNMASKED);
290         nn_pci_flush(nn);
291 }
292
293 /**
294  * nfp_net_msix_alloc() - Try to allocate MSI-X irqs
295  * @nn:       NFP Network structure
296  * @nr_vecs:  Number of MSI-X vectors to allocate
297  *
298  * For MSI-X we want at least NFP_NET_NON_Q_VECTORS + 1 vectors.
299  *
300  * Return: Number of MSI-X vectors obtained or 0 on error.
301  */
302 static int nfp_net_msix_alloc(struct nfp_net *nn, int nr_vecs)
303 {
304         struct pci_dev *pdev = nn->pdev;
305         int nvecs;
306         int i;
307
308         for (i = 0; i < nr_vecs; i++)
309                 nn->irq_entries[i].entry = i;
310
311         nvecs = pci_enable_msix_range(pdev, nn->irq_entries,
312                                       NFP_NET_NON_Q_VECTORS + 1, nr_vecs);
313         if (nvecs < 0) {
314                 nn_warn(nn, "Failed to enable MSI-X. Wanted %d-%d (err=%d)\n",
315                         NFP_NET_NON_Q_VECTORS + 1, nr_vecs, nvecs);
316                 return 0;
317         }
318
319         return nvecs;
320 }
321
322 /**
323  * nfp_net_irqs_wanted() - Work out how many interrupt vectors we want
324  * @nn:       NFP Network structure
325  *
326  * We want a vector per CPU (or ring), whatever is smaller plus
327  * NFP_NET_NON_Q_VECTORS for LSC etc.
328  *
329  * Return: Number of interrupts wanted
330  */
331 static int nfp_net_irqs_wanted(struct nfp_net *nn)
332 {
333         int ncpus;
334         int vecs;
335
336         ncpus = num_online_cpus();
337
338         vecs = max_t(int, nn->num_tx_rings, nn->num_rx_rings);
339         vecs = min_t(int, vecs, ncpus);
340
341         return vecs + NFP_NET_NON_Q_VECTORS;
342 }
343
344 /**
345  * nfp_net_irqs_alloc() - allocates MSI-X irqs
346  * @nn:       NFP Network structure
347  *
348  * Return: Number of irqs obtained or 0 on error.
349  */
350 int nfp_net_irqs_alloc(struct nfp_net *nn)
351 {
352         int wanted_irqs;
353
354         wanted_irqs = nfp_net_irqs_wanted(nn);
355
356         nn->num_irqs = nfp_net_msix_alloc(nn, wanted_irqs);
357         if (nn->num_irqs == 0) {
358                 nn_err(nn, "Failed to allocate MSI-X IRQs\n");
359                 return 0;
360         }
361
362         nn->num_r_vecs = nn->num_irqs - NFP_NET_NON_Q_VECTORS;
363
364         if (nn->num_irqs < wanted_irqs)
365                 nn_warn(nn, "Unable to allocate %d vectors. Got %d instead\n",
366                         wanted_irqs, nn->num_irqs);
367
368         return nn->num_irqs;
369 }
370
371 /**
372  * nfp_net_irqs_disable() - Disable interrupts
373  * @nn:       NFP Network structure
374  *
375  * Undoes what @nfp_net_irqs_alloc() does.
376  */
377 void nfp_net_irqs_disable(struct nfp_net *nn)
378 {
379         pci_disable_msix(nn->pdev);
380 }
381
382 /**
383  * nfp_net_irq_rxtx() - Interrupt service routine for RX/TX rings.
384  * @irq:      Interrupt
385  * @data:     Opaque data structure
386  *
387  * Return: Indicate if the interrupt has been handled.
388  */
389 static irqreturn_t nfp_net_irq_rxtx(int irq, void *data)
390 {
391         struct nfp_net_r_vector *r_vec = data;
392
393         napi_schedule_irqoff(&r_vec->napi);
394
395         /* The FW auto-masks any interrupt, either via the MASK bit in
396          * the MSI-X table or via the per entry ICR field.  So there
397          * is no need to disable interrupts here.
398          */
399         return IRQ_HANDLED;
400 }
401
402 /**
403  * nfp_net_read_link_status() - Reread link status from control BAR
404  * @nn:       NFP Network structure
405  */
406 static void nfp_net_read_link_status(struct nfp_net *nn)
407 {
408         unsigned long flags;
409         bool link_up;
410         u32 sts;
411
412         spin_lock_irqsave(&nn->link_status_lock, flags);
413
414         sts = nn_readl(nn, NFP_NET_CFG_STS);
415         link_up = !!(sts & NFP_NET_CFG_STS_LINK);
416
417         if (nn->link_up == link_up)
418                 goto out;
419
420         nn->link_up = link_up;
421
422         if (nn->link_up) {
423                 netif_carrier_on(nn->netdev);
424                 netdev_info(nn->netdev, "NIC Link is Up\n");
425         } else {
426                 netif_carrier_off(nn->netdev);
427                 netdev_info(nn->netdev, "NIC Link is Down\n");
428         }
429 out:
430         spin_unlock_irqrestore(&nn->link_status_lock, flags);
431 }
432
433 /**
434  * nfp_net_irq_lsc() - Interrupt service routine for link state changes
435  * @irq:      Interrupt
436  * @data:     Opaque data structure
437  *
438  * Return: Indicate if the interrupt has been handled.
439  */
440 static irqreturn_t nfp_net_irq_lsc(int irq, void *data)
441 {
442         struct nfp_net *nn = data;
443
444         nfp_net_read_link_status(nn);
445
446         nfp_net_irq_unmask(nn, NFP_NET_IRQ_LSC_IDX);
447
448         return IRQ_HANDLED;
449 }
450
451 /**
452  * nfp_net_irq_exn() - Interrupt service routine for exceptions
453  * @irq:      Interrupt
454  * @data:     Opaque data structure
455  *
456  * Return: Indicate if the interrupt has been handled.
457  */
458 static irqreturn_t nfp_net_irq_exn(int irq, void *data)
459 {
460         struct nfp_net *nn = data;
461
462         nn_err(nn, "%s: UNIMPLEMENTED.\n", __func__);
463         /* XXX TO BE IMPLEMENTED */
464         return IRQ_HANDLED;
465 }
466
467 /**
468  * nfp_net_tx_ring_init() - Fill in the boilerplate for a TX ring
469  * @tx_ring:  TX ring structure
470  * @r_vec:    IRQ vector servicing this ring
471  * @idx:      Ring index
472  */
473 static void
474 nfp_net_tx_ring_init(struct nfp_net_tx_ring *tx_ring,
475                      struct nfp_net_r_vector *r_vec, unsigned int idx)
476 {
477         struct nfp_net *nn = r_vec->nfp_net;
478
479         tx_ring->idx = idx;
480         tx_ring->r_vec = r_vec;
481
482         tx_ring->qcidx = tx_ring->idx * nn->stride_tx;
483         tx_ring->qcp_q = nn->tx_bar + NFP_QCP_QUEUE_OFF(tx_ring->qcidx);
484 }
485
486 /**
487  * nfp_net_rx_ring_init() - Fill in the boilerplate for a RX ring
488  * @rx_ring:  RX ring structure
489  * @r_vec:    IRQ vector servicing this ring
490  * @idx:      Ring index
491  */
492 static void
493 nfp_net_rx_ring_init(struct nfp_net_rx_ring *rx_ring,
494                      struct nfp_net_r_vector *r_vec, unsigned int idx)
495 {
496         struct nfp_net *nn = r_vec->nfp_net;
497
498         rx_ring->idx = idx;
499         rx_ring->r_vec = r_vec;
500
501         rx_ring->fl_qcidx = rx_ring->idx * nn->stride_rx;
502         rx_ring->rx_qcidx = rx_ring->fl_qcidx + (nn->stride_rx - 1);
503
504         rx_ring->qcp_fl = nn->rx_bar + NFP_QCP_QUEUE_OFF(rx_ring->fl_qcidx);
505         rx_ring->qcp_rx = nn->rx_bar + NFP_QCP_QUEUE_OFF(rx_ring->rx_qcidx);
506 }
507
508 /**
509  * nfp_net_irqs_assign() - Assign IRQs and setup rvecs.
510  * @netdev:   netdev structure
511  */
512 static void nfp_net_irqs_assign(struct net_device *netdev)
513 {
514         struct nfp_net *nn = netdev_priv(netdev);
515         struct nfp_net_r_vector *r_vec;
516         int r;
517
518         /* Assumes nn->num_tx_rings == nn->num_rx_rings */
519         if (nn->num_tx_rings > nn->num_r_vecs) {
520                 nn_warn(nn, "More rings (%d) than vectors (%d).\n",
521                         nn->num_tx_rings, nn->num_r_vecs);
522                 nn->num_tx_rings = nn->num_r_vecs;
523                 nn->num_rx_rings = nn->num_r_vecs;
524         }
525
526         nn->lsc_handler = nfp_net_irq_lsc;
527         nn->exn_handler = nfp_net_irq_exn;
528
529         for (r = 0; r < nn->num_r_vecs; r++) {
530                 r_vec = &nn->r_vecs[r];
531                 r_vec->nfp_net = nn;
532                 r_vec->handler = nfp_net_irq_rxtx;
533                 r_vec->irq_idx = NFP_NET_NON_Q_VECTORS + r;
534
535                 cpumask_set_cpu(r, &r_vec->affinity_mask);
536         }
537 }
538
539 /**
540  * nfp_net_aux_irq_request() - Request an auxiliary interrupt (LSC or EXN)
541  * @nn:         NFP Network structure
542  * @ctrl_offset: Control BAR offset where IRQ configuration should be written
543  * @format:     printf-style format to construct the interrupt name
544  * @name:       Pointer to allocated space for interrupt name
545  * @name_sz:    Size of space for interrupt name
546  * @vector_idx: Index of MSI-X vector used for this interrupt
547  * @handler:    IRQ handler to register for this interrupt
548  */
549 static int
550 nfp_net_aux_irq_request(struct nfp_net *nn, u32 ctrl_offset,
551                         const char *format, char *name, size_t name_sz,
552                         unsigned int vector_idx, irq_handler_t handler)
553 {
554         struct msix_entry *entry;
555         int err;
556
557         entry = &nn->irq_entries[vector_idx];
558
559         snprintf(name, name_sz, format, netdev_name(nn->netdev));
560         err = request_irq(entry->vector, handler, 0, name, nn);
561         if (err) {
562                 nn_err(nn, "Failed to request IRQ %d (err=%d).\n",
563                        entry->vector, err);
564                 return err;
565         }
566         nn_writeb(nn, ctrl_offset, vector_idx);
567
568         return 0;
569 }
570
571 /**
572  * nfp_net_aux_irq_free() - Free an auxiliary interrupt (LSC or EXN)
573  * @nn:         NFP Network structure
574  * @ctrl_offset: Control BAR offset where IRQ configuration should be written
575  * @vector_idx: Index of MSI-X vector used for this interrupt
576  */
577 static void nfp_net_aux_irq_free(struct nfp_net *nn, u32 ctrl_offset,
578                                  unsigned int vector_idx)
579 {
580         nn_writeb(nn, ctrl_offset, 0xff);
581         free_irq(nn->irq_entries[vector_idx].vector, nn);
582 }
583
584 /* Transmit
585  *
586  * One queue controller peripheral queue is used for transmit.  The
587  * driver en-queues packets for transmit by advancing the write
588  * pointer.  The device indicates that packets have transmitted by
589  * advancing the read pointer.  The driver maintains a local copy of
590  * the read and write pointer in @struct nfp_net_tx_ring.  The driver
591  * keeps @wr_p in sync with the queue controller write pointer and can
592  * determine how many packets have been transmitted by comparing its
593  * copy of the read pointer @rd_p with the read pointer maintained by
594  * the queue controller peripheral.
595  */
596
597 /**
598  * nfp_net_tx_full() - Check if the TX ring is full
599  * @tx_ring: TX ring to check
600  * @dcnt:    Number of descriptors that need to be enqueued (must be >= 1)
601  *
602  * This function checks, based on the *host copy* of read/write
603  * pointer if a given TX ring is full.  The real TX queue may have
604  * some newly made available slots.
605  *
606  * Return: True if the ring is full.
607  */
608 static inline int nfp_net_tx_full(struct nfp_net_tx_ring *tx_ring, int dcnt)
609 {
610         return (tx_ring->wr_p - tx_ring->rd_p) >= (tx_ring->cnt - dcnt);
611 }
612
613 /* Wrappers for deciding when to stop and restart TX queues */
614 static int nfp_net_tx_ring_should_wake(struct nfp_net_tx_ring *tx_ring)
615 {
616         return !nfp_net_tx_full(tx_ring, MAX_SKB_FRAGS * 4);
617 }
618
619 static int nfp_net_tx_ring_should_stop(struct nfp_net_tx_ring *tx_ring)
620 {
621         return nfp_net_tx_full(tx_ring, MAX_SKB_FRAGS + 1);
622 }
623
624 /**
625  * nfp_net_tx_ring_stop() - stop tx ring
626  * @nd_q:    netdev queue
627  * @tx_ring: driver tx queue structure
628  *
629  * Safely stop TX ring.  Remember that while we are running .start_xmit()
630  * someone else may be cleaning the TX ring completions so we need to be
631  * extra careful here.
632  */
633 static void nfp_net_tx_ring_stop(struct netdev_queue *nd_q,
634                                  struct nfp_net_tx_ring *tx_ring)
635 {
636         netif_tx_stop_queue(nd_q);
637
638         /* We can race with the TX completion out of NAPI so recheck */
639         smp_mb();
640         if (unlikely(nfp_net_tx_ring_should_wake(tx_ring)))
641                 netif_tx_start_queue(nd_q);
642 }
643
644 /**
645  * nfp_net_tx_tso() - Set up Tx descriptor for LSO
646  * @nn:  NFP Net device
647  * @r_vec: per-ring structure
648  * @txbuf: Pointer to driver soft TX descriptor
649  * @txd: Pointer to HW TX descriptor
650  * @skb: Pointer to SKB
651  *
652  * Set up Tx descriptor for LSO, do nothing for non-LSO skbs.
653  * Return error on packet header greater than maximum supported LSO header size.
654  */
655 static void nfp_net_tx_tso(struct nfp_net *nn, struct nfp_net_r_vector *r_vec,
656                            struct nfp_net_tx_buf *txbuf,
657                            struct nfp_net_tx_desc *txd, struct sk_buff *skb)
658 {
659         u32 hdrlen;
660         u16 mss;
661
662         if (!skb_is_gso(skb))
663                 return;
664
665         if (!skb->encapsulation)
666                 hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
667         else
668                 hdrlen = skb_inner_transport_header(skb) - skb->data +
669                         inner_tcp_hdrlen(skb);
670
671         txbuf->pkt_cnt = skb_shinfo(skb)->gso_segs;
672         txbuf->real_len += hdrlen * (txbuf->pkt_cnt - 1);
673
674         mss = skb_shinfo(skb)->gso_size & PCIE_DESC_TX_MSS_MASK;
675         txd->l4_offset = hdrlen;
676         txd->mss = cpu_to_le16(mss);
677         txd->flags |= PCIE_DESC_TX_LSO;
678
679         u64_stats_update_begin(&r_vec->tx_sync);
680         r_vec->tx_lso++;
681         u64_stats_update_end(&r_vec->tx_sync);
682 }
683
684 /**
685  * nfp_net_tx_csum() - Set TX CSUM offload flags in TX descriptor
686  * @nn:  NFP Net device
687  * @r_vec: per-ring structure
688  * @txbuf: Pointer to driver soft TX descriptor
689  * @txd: Pointer to TX descriptor
690  * @skb: Pointer to SKB
691  *
692  * This function sets the TX checksum flags in the TX descriptor based
693  * on the configuration and the protocol of the packet to be transmitted.
694  */
695 static void nfp_net_tx_csum(struct nfp_net *nn, struct nfp_net_r_vector *r_vec,
696                             struct nfp_net_tx_buf *txbuf,
697                             struct nfp_net_tx_desc *txd, struct sk_buff *skb)
698 {
699         struct ipv6hdr *ipv6h;
700         struct iphdr *iph;
701         u8 l4_hdr;
702
703         if (!(nn->ctrl & NFP_NET_CFG_CTRL_TXCSUM))
704                 return;
705
706         if (skb->ip_summed != CHECKSUM_PARTIAL)
707                 return;
708
709         txd->flags |= PCIE_DESC_TX_CSUM;
710         if (skb->encapsulation)
711                 txd->flags |= PCIE_DESC_TX_ENCAP;
712
713         iph = skb->encapsulation ? inner_ip_hdr(skb) : ip_hdr(skb);
714         ipv6h = skb->encapsulation ? inner_ipv6_hdr(skb) : ipv6_hdr(skb);
715
716         if (iph->version == 4) {
717                 txd->flags |= PCIE_DESC_TX_IP4_CSUM;
718                 l4_hdr = iph->protocol;
719         } else if (ipv6h->version == 6) {
720                 l4_hdr = ipv6h->nexthdr;
721         } else {
722                 nn_warn_ratelimit(nn, "partial checksum but ipv=%x!\n",
723                                   iph->version);
724                 return;
725         }
726
727         switch (l4_hdr) {
728         case IPPROTO_TCP:
729                 txd->flags |= PCIE_DESC_TX_TCP_CSUM;
730                 break;
731         case IPPROTO_UDP:
732                 txd->flags |= PCIE_DESC_TX_UDP_CSUM;
733                 break;
734         default:
735                 nn_warn_ratelimit(nn, "partial checksum but l4 proto=%x!\n",
736                                   l4_hdr);
737                 return;
738         }
739
740         u64_stats_update_begin(&r_vec->tx_sync);
741         if (skb->encapsulation)
742                 r_vec->hw_csum_tx_inner += txbuf->pkt_cnt;
743         else
744                 r_vec->hw_csum_tx += txbuf->pkt_cnt;
745         u64_stats_update_end(&r_vec->tx_sync);
746 }
747
748 /**
749  * nfp_net_tx() - Main transmit entry point
750  * @skb:    SKB to transmit
751  * @netdev: netdev structure
752  *
753  * Return: NETDEV_TX_OK on success.
754  */
755 static int nfp_net_tx(struct sk_buff *skb, struct net_device *netdev)
756 {
757         struct nfp_net *nn = netdev_priv(netdev);
758         const struct skb_frag_struct *frag;
759         struct nfp_net_r_vector *r_vec;
760         struct nfp_net_tx_desc *txd, txdg;
761         struct nfp_net_tx_buf *txbuf;
762         struct nfp_net_tx_ring *tx_ring;
763         struct netdev_queue *nd_q;
764         dma_addr_t dma_addr;
765         unsigned int fsize;
766         int f, nr_frags;
767         int wr_idx;
768         u16 qidx;
769
770         qidx = skb_get_queue_mapping(skb);
771         tx_ring = &nn->tx_rings[qidx];
772         r_vec = tx_ring->r_vec;
773         nd_q = netdev_get_tx_queue(nn->netdev, qidx);
774
775         nr_frags = skb_shinfo(skb)->nr_frags;
776
777         if (unlikely(nfp_net_tx_full(tx_ring, nr_frags + 1))) {
778                 nn_warn_ratelimit(nn, "TX ring %d busy. wrp=%u rdp=%u\n",
779                                   qidx, tx_ring->wr_p, tx_ring->rd_p);
780                 netif_tx_stop_queue(nd_q);
781                 u64_stats_update_begin(&r_vec->tx_sync);
782                 r_vec->tx_busy++;
783                 u64_stats_update_end(&r_vec->tx_sync);
784                 return NETDEV_TX_BUSY;
785         }
786
787         /* Start with the head skbuf */
788         dma_addr = dma_map_single(&nn->pdev->dev, skb->data, skb_headlen(skb),
789                                   DMA_TO_DEVICE);
790         if (dma_mapping_error(&nn->pdev->dev, dma_addr))
791                 goto err_free;
792
793         wr_idx = tx_ring->wr_p % tx_ring->cnt;
794
795         /* Stash the soft descriptor of the head then initialize it */
796         txbuf = &tx_ring->txbufs[wr_idx];
797         txbuf->skb = skb;
798         txbuf->dma_addr = dma_addr;
799         txbuf->fidx = -1;
800         txbuf->pkt_cnt = 1;
801         txbuf->real_len = skb->len;
802
803         /* Build TX descriptor */
804         txd = &tx_ring->txds[wr_idx];
805         txd->offset_eop = (nr_frags == 0) ? PCIE_DESC_TX_EOP : 0;
806         txd->dma_len = cpu_to_le16(skb_headlen(skb));
807         nfp_desc_set_dma_addr(txd, dma_addr);
808         txd->data_len = cpu_to_le16(skb->len);
809
810         txd->flags = 0;
811         txd->mss = 0;
812         txd->l4_offset = 0;
813
814         nfp_net_tx_tso(nn, r_vec, txbuf, txd, skb);
815
816         nfp_net_tx_csum(nn, r_vec, txbuf, txd, skb);
817
818         if (skb_vlan_tag_present(skb) && nn->ctrl & NFP_NET_CFG_CTRL_TXVLAN) {
819                 txd->flags |= PCIE_DESC_TX_VLAN;
820                 txd->vlan = cpu_to_le16(skb_vlan_tag_get(skb));
821         }
822
823         /* Gather DMA */
824         if (nr_frags > 0) {
825                 /* all descs must match except for in addr, length and eop */
826                 txdg = *txd;
827
828                 for (f = 0; f < nr_frags; f++) {
829                         frag = &skb_shinfo(skb)->frags[f];
830                         fsize = skb_frag_size(frag);
831
832                         dma_addr = skb_frag_dma_map(&nn->pdev->dev, frag, 0,
833                                                     fsize, DMA_TO_DEVICE);
834                         if (dma_mapping_error(&nn->pdev->dev, dma_addr))
835                                 goto err_unmap;
836
837                         wr_idx = (wr_idx + 1) % tx_ring->cnt;
838                         tx_ring->txbufs[wr_idx].skb = skb;
839                         tx_ring->txbufs[wr_idx].dma_addr = dma_addr;
840                         tx_ring->txbufs[wr_idx].fidx = f;
841
842                         txd = &tx_ring->txds[wr_idx];
843                         *txd = txdg;
844                         txd->dma_len = cpu_to_le16(fsize);
845                         nfp_desc_set_dma_addr(txd, dma_addr);
846                         txd->offset_eop =
847                                 (f == nr_frags - 1) ? PCIE_DESC_TX_EOP : 0;
848                 }
849
850                 u64_stats_update_begin(&r_vec->tx_sync);
851                 r_vec->tx_gather++;
852                 u64_stats_update_end(&r_vec->tx_sync);
853         }
854
855         netdev_tx_sent_queue(nd_q, txbuf->real_len);
856
857         skb_tx_timestamp(skb);
858
859         tx_ring->wr_p += nr_frags + 1;
860         if (nfp_net_tx_ring_should_stop(tx_ring))
861                 nfp_net_tx_ring_stop(nd_q, tx_ring);
862
863         tx_ring->wr_ptr_add += nr_frags + 1;
864         if (!skb->xmit_more || netif_xmit_stopped(nd_q)) {
865                 /* force memory write before we let HW know */
866                 wmb();
867                 nfp_qcp_wr_ptr_add(tx_ring->qcp_q, tx_ring->wr_ptr_add);
868                 tx_ring->wr_ptr_add = 0;
869         }
870
871         return NETDEV_TX_OK;
872
873 err_unmap:
874         while (--f >= 0) {
875                 frag = &skb_shinfo(skb)->frags[f];
876                 dma_unmap_page(&nn->pdev->dev,
877                                tx_ring->txbufs[wr_idx].dma_addr,
878                                skb_frag_size(frag), DMA_TO_DEVICE);
879                 tx_ring->txbufs[wr_idx].skb = NULL;
880                 tx_ring->txbufs[wr_idx].dma_addr = 0;
881                 tx_ring->txbufs[wr_idx].fidx = -2;
882                 wr_idx = wr_idx - 1;
883                 if (wr_idx < 0)
884                         wr_idx += tx_ring->cnt;
885         }
886         dma_unmap_single(&nn->pdev->dev, tx_ring->txbufs[wr_idx].dma_addr,
887                          skb_headlen(skb), DMA_TO_DEVICE);
888         tx_ring->txbufs[wr_idx].skb = NULL;
889         tx_ring->txbufs[wr_idx].dma_addr = 0;
890         tx_ring->txbufs[wr_idx].fidx = -2;
891 err_free:
892         nn_warn_ratelimit(nn, "Failed to map DMA TX buffer\n");
893         u64_stats_update_begin(&r_vec->tx_sync);
894         r_vec->tx_errors++;
895         u64_stats_update_end(&r_vec->tx_sync);
896         dev_kfree_skb_any(skb);
897         return NETDEV_TX_OK;
898 }
899
900 /**
901  * nfp_net_tx_complete() - Handled completed TX packets
902  * @tx_ring:   TX ring structure
903  *
904  * Return: Number of completed TX descriptors
905  */
906 static void nfp_net_tx_complete(struct nfp_net_tx_ring *tx_ring)
907 {
908         struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
909         struct nfp_net *nn = r_vec->nfp_net;
910         const struct skb_frag_struct *frag;
911         struct netdev_queue *nd_q;
912         u32 done_pkts = 0, done_bytes = 0;
913         struct sk_buff *skb;
914         int todo, nr_frags;
915         u32 qcp_rd_p;
916         int fidx;
917         int idx;
918
919         /* Work out how many descriptors have been transmitted */
920         qcp_rd_p = nfp_qcp_rd_ptr_read(tx_ring->qcp_q);
921
922         if (qcp_rd_p == tx_ring->qcp_rd_p)
923                 return;
924
925         if (qcp_rd_p > tx_ring->qcp_rd_p)
926                 todo = qcp_rd_p - tx_ring->qcp_rd_p;
927         else
928                 todo = qcp_rd_p + tx_ring->cnt - tx_ring->qcp_rd_p;
929
930         while (todo--) {
931                 idx = tx_ring->rd_p % tx_ring->cnt;
932                 tx_ring->rd_p++;
933
934                 skb = tx_ring->txbufs[idx].skb;
935                 if (!skb)
936                         continue;
937
938                 nr_frags = skb_shinfo(skb)->nr_frags;
939                 fidx = tx_ring->txbufs[idx].fidx;
940
941                 if (fidx == -1) {
942                         /* unmap head */
943                         dma_unmap_single(&nn->pdev->dev,
944                                          tx_ring->txbufs[idx].dma_addr,
945                                          skb_headlen(skb), DMA_TO_DEVICE);
946
947                         done_pkts += tx_ring->txbufs[idx].pkt_cnt;
948                         done_bytes += tx_ring->txbufs[idx].real_len;
949                 } else {
950                         /* unmap fragment */
951                         frag = &skb_shinfo(skb)->frags[fidx];
952                         dma_unmap_page(&nn->pdev->dev,
953                                        tx_ring->txbufs[idx].dma_addr,
954                                        skb_frag_size(frag), DMA_TO_DEVICE);
955                 }
956
957                 /* check for last gather fragment */
958                 if (fidx == nr_frags - 1)
959                         dev_kfree_skb_any(skb);
960
961                 tx_ring->txbufs[idx].dma_addr = 0;
962                 tx_ring->txbufs[idx].skb = NULL;
963                 tx_ring->txbufs[idx].fidx = -2;
964         }
965
966         tx_ring->qcp_rd_p = qcp_rd_p;
967
968         u64_stats_update_begin(&r_vec->tx_sync);
969         r_vec->tx_bytes += done_bytes;
970         r_vec->tx_pkts += done_pkts;
971         u64_stats_update_end(&r_vec->tx_sync);
972
973         nd_q = netdev_get_tx_queue(nn->netdev, tx_ring->idx);
974         netdev_tx_completed_queue(nd_q, done_pkts, done_bytes);
975         if (nfp_net_tx_ring_should_wake(tx_ring)) {
976                 /* Make sure TX thread will see updated tx_ring->rd_p */
977                 smp_mb();
978
979                 if (unlikely(netif_tx_queue_stopped(nd_q)))
980                         netif_tx_wake_queue(nd_q);
981         }
982
983         WARN_ONCE(tx_ring->wr_p - tx_ring->rd_p > tx_ring->cnt,
984                   "TX ring corruption rd_p=%u wr_p=%u cnt=%u\n",
985                   tx_ring->rd_p, tx_ring->wr_p, tx_ring->cnt);
986 }
987
988 /**
989  * nfp_net_tx_ring_reset() - Free any untransmitted buffers and reset pointers
990  * @nn:         NFP Net device
991  * @tx_ring:    TX ring structure
992  *
993  * Assumes that the device is stopped, must be idempotent.
994  */
995 static void
996 nfp_net_tx_ring_reset(struct nfp_net *nn, struct nfp_net_tx_ring *tx_ring)
997 {
998         const struct skb_frag_struct *frag;
999         struct netdev_queue *nd_q;
1000         struct pci_dev *pdev = nn->pdev;
1001
1002         while (tx_ring->rd_p != tx_ring->wr_p) {
1003                 int nr_frags, fidx, idx;
1004                 struct sk_buff *skb;
1005
1006                 idx = tx_ring->rd_p % tx_ring->cnt;
1007                 skb = tx_ring->txbufs[idx].skb;
1008                 nr_frags = skb_shinfo(skb)->nr_frags;
1009                 fidx = tx_ring->txbufs[idx].fidx;
1010
1011                 if (fidx == -1) {
1012                         /* unmap head */
1013                         dma_unmap_single(&pdev->dev,
1014                                          tx_ring->txbufs[idx].dma_addr,
1015                                          skb_headlen(skb), DMA_TO_DEVICE);
1016                 } else {
1017                         /* unmap fragment */
1018                         frag = &skb_shinfo(skb)->frags[fidx];
1019                         dma_unmap_page(&pdev->dev,
1020                                        tx_ring->txbufs[idx].dma_addr,
1021                                        skb_frag_size(frag), DMA_TO_DEVICE);
1022                 }
1023
1024                 /* check for last gather fragment */
1025                 if (fidx == nr_frags - 1)
1026                         dev_kfree_skb_any(skb);
1027
1028                 tx_ring->txbufs[idx].dma_addr = 0;
1029                 tx_ring->txbufs[idx].skb = NULL;
1030                 tx_ring->txbufs[idx].fidx = -2;
1031
1032                 tx_ring->qcp_rd_p++;
1033                 tx_ring->rd_p++;
1034         }
1035
1036         memset(tx_ring->txds, 0, sizeof(*tx_ring->txds) * tx_ring->cnt);
1037         tx_ring->wr_p = 0;
1038         tx_ring->rd_p = 0;
1039         tx_ring->qcp_rd_p = 0;
1040         tx_ring->wr_ptr_add = 0;
1041
1042         nd_q = netdev_get_tx_queue(nn->netdev, tx_ring->idx);
1043         netdev_tx_reset_queue(nd_q);
1044 }
1045
1046 static void nfp_net_tx_timeout(struct net_device *netdev)
1047 {
1048         struct nfp_net *nn = netdev_priv(netdev);
1049         int i;
1050
1051         for (i = 0; i < nn->num_tx_rings; i++) {
1052                 if (!netif_tx_queue_stopped(netdev_get_tx_queue(netdev, i)))
1053                         continue;
1054                 nn_warn(nn, "TX timeout on ring: %d\n", i);
1055         }
1056         nn_warn(nn, "TX watchdog timeout\n");
1057 }
1058
1059 /* Receive processing
1060  */
1061
1062 /**
1063  * nfp_net_rx_space() - return the number of free slots on the RX ring
1064  * @rx_ring:   RX ring structure
1065  *
1066  * Make sure we leave at least one slot free.
1067  *
1068  * Return: True if there is space on the RX ring
1069  */
1070 static inline int nfp_net_rx_space(struct nfp_net_rx_ring *rx_ring)
1071 {
1072         return (rx_ring->cnt - 1) - (rx_ring->wr_p - rx_ring->rd_p);
1073 }
1074
1075 /**
1076  * nfp_net_rx_alloc_one() - Allocate and map skb for RX
1077  * @rx_ring:    RX ring structure of the skb
1078  * @dma_addr:   Pointer to storage for DMA address (output param)
1079  * @fl_bufsz:   size of freelist buffers
1080  *
1081  * This function will allcate a new skb, map it for DMA.
1082  *
1083  * Return: allocated skb or NULL on failure.
1084  */
1085 static struct sk_buff *
1086 nfp_net_rx_alloc_one(struct nfp_net_rx_ring *rx_ring, dma_addr_t *dma_addr,
1087                      unsigned int fl_bufsz)
1088 {
1089         struct nfp_net *nn = rx_ring->r_vec->nfp_net;
1090         struct sk_buff *skb;
1091
1092         skb = netdev_alloc_skb(nn->netdev, fl_bufsz);
1093         if (!skb) {
1094                 nn_warn_ratelimit(nn, "Failed to alloc receive SKB\n");
1095                 return NULL;
1096         }
1097
1098         *dma_addr = dma_map_single(&nn->pdev->dev, skb->data,
1099                                    fl_bufsz, DMA_FROM_DEVICE);
1100         if (dma_mapping_error(&nn->pdev->dev, *dma_addr)) {
1101                 dev_kfree_skb_any(skb);
1102                 nn_warn_ratelimit(nn, "Failed to map DMA RX buffer\n");
1103                 return NULL;
1104         }
1105
1106         return skb;
1107 }
1108
1109 /**
1110  * nfp_net_rx_give_one() - Put mapped skb on the software and hardware rings
1111  * @rx_ring:    RX ring structure
1112  * @skb:        Skb to put on rings
1113  * @dma_addr:   DMA address of skb mapping
1114  */
1115 static void nfp_net_rx_give_one(struct nfp_net_rx_ring *rx_ring,
1116                                 struct sk_buff *skb, dma_addr_t dma_addr)
1117 {
1118         unsigned int wr_idx;
1119
1120         wr_idx = rx_ring->wr_p % rx_ring->cnt;
1121
1122         /* Stash SKB and DMA address away */
1123         rx_ring->rxbufs[wr_idx].skb = skb;
1124         rx_ring->rxbufs[wr_idx].dma_addr = dma_addr;
1125
1126         /* Fill freelist descriptor */
1127         rx_ring->rxds[wr_idx].fld.reserved = 0;
1128         rx_ring->rxds[wr_idx].fld.meta_len_dd = 0;
1129         nfp_desc_set_dma_addr(&rx_ring->rxds[wr_idx].fld, dma_addr);
1130
1131         rx_ring->wr_p++;
1132         rx_ring->wr_ptr_add++;
1133         if (rx_ring->wr_ptr_add >= NFP_NET_FL_BATCH) {
1134                 /* Update write pointer of the freelist queue. Make
1135                  * sure all writes are flushed before telling the hardware.
1136                  */
1137                 wmb();
1138                 nfp_qcp_wr_ptr_add(rx_ring->qcp_fl, rx_ring->wr_ptr_add);
1139                 rx_ring->wr_ptr_add = 0;
1140         }
1141 }
1142
1143 /**
1144  * nfp_net_rx_ring_reset() - Reflect in SW state of freelist after disable
1145  * @rx_ring:    RX ring structure
1146  *
1147  * Assumes that the device is stopped, must be idempotent.
1148  */
1149 static void nfp_net_rx_ring_reset(struct nfp_net_rx_ring *rx_ring)
1150 {
1151         unsigned int wr_idx, last_idx;
1152
1153         /* wr_p == rd_p means ring was never fed FL bufs.  RX rings are always
1154          * kept at cnt - 1 FL bufs.
1155          */
1156         if (rx_ring->wr_p == 0 && rx_ring->rd_p == 0)
1157                 return;
1158
1159         /* Move the empty entry to the end of the list */
1160         wr_idx = rx_ring->wr_p % rx_ring->cnt;
1161         last_idx = rx_ring->cnt - 1;
1162         rx_ring->rxbufs[wr_idx].dma_addr = rx_ring->rxbufs[last_idx].dma_addr;
1163         rx_ring->rxbufs[wr_idx].skb = rx_ring->rxbufs[last_idx].skb;
1164         rx_ring->rxbufs[last_idx].dma_addr = 0;
1165         rx_ring->rxbufs[last_idx].skb = NULL;
1166
1167         memset(rx_ring->rxds, 0, sizeof(*rx_ring->rxds) * rx_ring->cnt);
1168         rx_ring->wr_p = 0;
1169         rx_ring->rd_p = 0;
1170         rx_ring->wr_ptr_add = 0;
1171 }
1172
1173 /**
1174  * nfp_net_rx_ring_bufs_free() - Free any buffers currently on the RX ring
1175  * @nn:         NFP Net device
1176  * @rx_ring:    RX ring to remove buffers from
1177  *
1178  * Assumes that the device is stopped and buffers are in [0, ring->cnt - 1)
1179  * entries.  After device is disabled nfp_net_rx_ring_reset() must be called
1180  * to restore required ring geometry.
1181  */
1182 static void
1183 nfp_net_rx_ring_bufs_free(struct nfp_net *nn, struct nfp_net_rx_ring *rx_ring)
1184 {
1185         struct pci_dev *pdev = nn->pdev;
1186         unsigned int i;
1187
1188         for (i = 0; i < rx_ring->cnt - 1; i++) {
1189                 /* NULL skb can only happen when initial filling of the ring
1190                  * fails to allocate enough buffers and calls here to free
1191                  * already allocated ones.
1192                  */
1193                 if (!rx_ring->rxbufs[i].skb)
1194                         continue;
1195
1196                 dma_unmap_single(&pdev->dev, rx_ring->rxbufs[i].dma_addr,
1197                                  rx_ring->bufsz, DMA_FROM_DEVICE);
1198                 dev_kfree_skb_any(rx_ring->rxbufs[i].skb);
1199                 rx_ring->rxbufs[i].dma_addr = 0;
1200                 rx_ring->rxbufs[i].skb = NULL;
1201         }
1202 }
1203
1204 /**
1205  * nfp_net_rx_ring_bufs_alloc() - Fill RX ring with buffers (don't give to FW)
1206  * @nn:         NFP Net device
1207  * @rx_ring:    RX ring to remove buffers from
1208  */
1209 static int
1210 nfp_net_rx_ring_bufs_alloc(struct nfp_net *nn, struct nfp_net_rx_ring *rx_ring)
1211 {
1212         struct nfp_net_rx_buf *rxbufs;
1213         unsigned int i;
1214
1215         rxbufs = rx_ring->rxbufs;
1216
1217         for (i = 0; i < rx_ring->cnt - 1; i++) {
1218                 rxbufs[i].skb =
1219                         nfp_net_rx_alloc_one(rx_ring, &rxbufs[i].dma_addr,
1220                                              rx_ring->bufsz);
1221                 if (!rxbufs[i].skb) {
1222                         nfp_net_rx_ring_bufs_free(nn, rx_ring);
1223                         return -ENOMEM;
1224                 }
1225         }
1226
1227         return 0;
1228 }
1229
1230 /**
1231  * nfp_net_rx_ring_fill_freelist() - Give buffers from the ring to FW
1232  * @rx_ring: RX ring to fill
1233  */
1234 static void nfp_net_rx_ring_fill_freelist(struct nfp_net_rx_ring *rx_ring)
1235 {
1236         unsigned int i;
1237
1238         for (i = 0; i < rx_ring->cnt - 1; i++)
1239                 nfp_net_rx_give_one(rx_ring, rx_ring->rxbufs[i].skb,
1240                                     rx_ring->rxbufs[i].dma_addr);
1241 }
1242
1243 /**
1244  * nfp_net_rx_csum_has_errors() - group check if rxd has any csum errors
1245  * @flags: RX descriptor flags field in CPU byte order
1246  */
1247 static int nfp_net_rx_csum_has_errors(u16 flags)
1248 {
1249         u16 csum_all_checked, csum_all_ok;
1250
1251         csum_all_checked = flags & __PCIE_DESC_RX_CSUM_ALL;
1252         csum_all_ok = flags & __PCIE_DESC_RX_CSUM_ALL_OK;
1253
1254         return csum_all_checked != (csum_all_ok << PCIE_DESC_RX_CSUM_OK_SHIFT);
1255 }
1256
1257 /**
1258  * nfp_net_rx_csum() - set SKB checksum field based on RX descriptor flags
1259  * @nn:  NFP Net device
1260  * @r_vec: per-ring structure
1261  * @rxd: Pointer to RX descriptor
1262  * @skb: Pointer to SKB
1263  */
1264 static void nfp_net_rx_csum(struct nfp_net *nn, struct nfp_net_r_vector *r_vec,
1265                             struct nfp_net_rx_desc *rxd, struct sk_buff *skb)
1266 {
1267         skb_checksum_none_assert(skb);
1268
1269         if (!(nn->netdev->features & NETIF_F_RXCSUM))
1270                 return;
1271
1272         if (nfp_net_rx_csum_has_errors(le16_to_cpu(rxd->rxd.flags))) {
1273                 u64_stats_update_begin(&r_vec->rx_sync);
1274                 r_vec->hw_csum_rx_error++;
1275                 u64_stats_update_end(&r_vec->rx_sync);
1276                 return;
1277         }
1278
1279         /* Assume that the firmware will never report inner CSUM_OK unless outer
1280          * L4 headers were successfully parsed. FW will always report zero UDP
1281          * checksum as CSUM_OK.
1282          */
1283         if (rxd->rxd.flags & PCIE_DESC_RX_TCP_CSUM_OK ||
1284             rxd->rxd.flags & PCIE_DESC_RX_UDP_CSUM_OK) {
1285                 __skb_incr_checksum_unnecessary(skb);
1286                 u64_stats_update_begin(&r_vec->rx_sync);
1287                 r_vec->hw_csum_rx_ok++;
1288                 u64_stats_update_end(&r_vec->rx_sync);
1289         }
1290
1291         if (rxd->rxd.flags & PCIE_DESC_RX_I_TCP_CSUM_OK ||
1292             rxd->rxd.flags & PCIE_DESC_RX_I_UDP_CSUM_OK) {
1293                 __skb_incr_checksum_unnecessary(skb);
1294                 u64_stats_update_begin(&r_vec->rx_sync);
1295                 r_vec->hw_csum_rx_inner_ok++;
1296                 u64_stats_update_end(&r_vec->rx_sync);
1297         }
1298 }
1299
1300 static void nfp_net_set_hash(struct net_device *netdev, struct sk_buff *skb,
1301                              unsigned int type, __be32 *hash)
1302 {
1303         if (!(netdev->features & NETIF_F_RXHASH))
1304                 return;
1305
1306         switch (type) {
1307         case NFP_NET_RSS_IPV4:
1308         case NFP_NET_RSS_IPV6:
1309         case NFP_NET_RSS_IPV6_EX:
1310                 skb_set_hash(skb, get_unaligned_be32(hash), PKT_HASH_TYPE_L3);
1311                 break;
1312         default:
1313                 skb_set_hash(skb, get_unaligned_be32(hash), PKT_HASH_TYPE_L4);
1314                 break;
1315         }
1316 }
1317
1318 static void
1319 nfp_net_set_hash_desc(struct net_device *netdev, struct sk_buff *skb,
1320                       struct nfp_net_rx_desc *rxd)
1321 {
1322         struct nfp_net_rx_hash *rx_hash;
1323
1324         if (!(rxd->rxd.flags & PCIE_DESC_RX_RSS))
1325                 return;
1326
1327         rx_hash = (struct nfp_net_rx_hash *)(skb->data - sizeof(*rx_hash));
1328
1329         nfp_net_set_hash(netdev, skb, get_unaligned_be32(&rx_hash->hash_type),
1330                          &rx_hash->hash);
1331 }
1332
1333 static void *
1334 nfp_net_parse_meta(struct net_device *netdev, struct sk_buff *skb,
1335                    int meta_len)
1336 {
1337         u8 *data = skb->data - meta_len;
1338         u32 meta_info;
1339
1340         meta_info = get_unaligned_be32(data);
1341         data += 4;
1342
1343         while (meta_info) {
1344                 switch (meta_info & NFP_NET_META_FIELD_MASK) {
1345                 case NFP_NET_META_HASH:
1346                         meta_info >>= NFP_NET_META_FIELD_SIZE;
1347                         nfp_net_set_hash(netdev, skb,
1348                                          meta_info & NFP_NET_META_FIELD_MASK,
1349                                          (__be32 *)data);
1350                         data += 4;
1351                         break;
1352                 case NFP_NET_META_MARK:
1353                         skb->mark = get_unaligned_be32(data);
1354                         data += 4;
1355                         break;
1356                 default:
1357                         return NULL;
1358                 }
1359
1360                 meta_info >>= NFP_NET_META_FIELD_SIZE;
1361         }
1362
1363         return data;
1364 }
1365
1366 /**
1367  * nfp_net_rx() - receive up to @budget packets on @rx_ring
1368  * @rx_ring:   RX ring to receive from
1369  * @budget:    NAPI budget
1370  *
1371  * Note, this function is separated out from the napi poll function to
1372  * more cleanly separate packet receive code from other bookkeeping
1373  * functions performed in the napi poll function.
1374  *
1375  * There are differences between the NFP-3200 firmware and the
1376  * NFP-6000 firmware.  The NFP-3200 firmware uses a dedicated RX queue
1377  * to indicate that new packets have arrived.  The NFP-6000 does not
1378  * have this queue and uses the DD bit in the RX descriptor. This
1379  * method cannot be used on the NFP-3200 as it causes a race
1380  * condition: The RX ring write pointer on the NFP-3200 is updated
1381  * after packets (and descriptors) have been DMAed.  If the DD bit is
1382  * used and subsequently the read pointer is updated this may lead to
1383  * the RX queue to underflow (if the firmware has not yet update the
1384  * write pointer).  Therefore we use slightly ugly conditional code
1385  * below to handle the differences.  We may, in the future update the
1386  * NFP-3200 firmware to behave the same as the firmware on the
1387  * NFP-6000.
1388  *
1389  * Return: Number of packets received.
1390  */
1391 static int nfp_net_rx(struct nfp_net_rx_ring *rx_ring, int budget)
1392 {
1393         struct nfp_net_r_vector *r_vec = rx_ring->r_vec;
1394         struct nfp_net *nn = r_vec->nfp_net;
1395         unsigned int data_len, meta_len;
1396         int avail = 0, pkts_polled = 0;
1397         struct sk_buff *skb, *new_skb;
1398         struct nfp_net_rx_desc *rxd;
1399         dma_addr_t new_dma_addr;
1400         u32 qcp_wr_p;
1401         int idx;
1402
1403         if (nn->is_nfp3200) {
1404                 /* Work out how many packets arrived */
1405                 qcp_wr_p = nfp_qcp_wr_ptr_read(rx_ring->qcp_rx);
1406                 idx = rx_ring->rd_p % rx_ring->cnt;
1407
1408                 if (qcp_wr_p == idx)
1409                         /* No new packets */
1410                         return 0;
1411
1412                 if (qcp_wr_p > idx)
1413                         avail = qcp_wr_p - idx;
1414                 else
1415                         avail = qcp_wr_p + rx_ring->cnt - idx;
1416         } else {
1417                 avail = budget + 1;
1418         }
1419
1420         while (avail > 0 && pkts_polled < budget) {
1421                 idx = rx_ring->rd_p % rx_ring->cnt;
1422
1423                 rxd = &rx_ring->rxds[idx];
1424                 if (!(rxd->rxd.meta_len_dd & PCIE_DESC_RX_DD)) {
1425                         if (nn->is_nfp3200)
1426                                 nn_dbg(nn, "RX descriptor not valid (DD)%d:%u rxd[0]=%#x rxd[1]=%#x\n",
1427                                        rx_ring->idx, idx,
1428                                        rxd->vals[0], rxd->vals[1]);
1429                         break;
1430                 }
1431                 /* Memory barrier to ensure that we won't do other reads
1432                  * before the DD bit.
1433                  */
1434                 dma_rmb();
1435
1436                 rx_ring->rd_p++;
1437                 pkts_polled++;
1438                 avail--;
1439
1440                 skb = rx_ring->rxbufs[idx].skb;
1441
1442                 new_skb = nfp_net_rx_alloc_one(rx_ring, &new_dma_addr,
1443                                                nn->fl_bufsz);
1444                 if (!new_skb) {
1445                         nfp_net_rx_give_one(rx_ring, rx_ring->rxbufs[idx].skb,
1446                                             rx_ring->rxbufs[idx].dma_addr);
1447                         u64_stats_update_begin(&r_vec->rx_sync);
1448                         r_vec->rx_drops++;
1449                         u64_stats_update_end(&r_vec->rx_sync);
1450                         continue;
1451                 }
1452
1453                 dma_unmap_single(&nn->pdev->dev,
1454                                  rx_ring->rxbufs[idx].dma_addr,
1455                                  nn->fl_bufsz, DMA_FROM_DEVICE);
1456
1457                 nfp_net_rx_give_one(rx_ring, new_skb, new_dma_addr);
1458
1459                 /*         < meta_len >
1460                  *  <-- [rx_offset] -->
1461                  *  ---------------------------------------------------------
1462                  * | [XX] |  metadata  |             packet           | XXXX |
1463                  *  ---------------------------------------------------------
1464                  *         <---------------- data_len --------------->
1465                  *
1466                  * The rx_offset is fixed for all packets, the meta_len can vary
1467                  * on a packet by packet basis. If rx_offset is set to zero
1468                  * (_RX_OFFSET_DYNAMIC) metadata starts at the beginning of the
1469                  * buffer and is immediately followed by the packet (no [XX]).
1470                  */
1471                 meta_len = rxd->rxd.meta_len_dd & PCIE_DESC_RX_META_LEN_MASK;
1472                 data_len = le16_to_cpu(rxd->rxd.data_len);
1473
1474                 if (nn->rx_offset == NFP_NET_CFG_RX_OFFSET_DYNAMIC)
1475                         skb_reserve(skb, meta_len);
1476                 else
1477                         skb_reserve(skb, nn->rx_offset);
1478                 skb_put(skb, data_len - meta_len);
1479
1480                 /* Stats update */
1481                 u64_stats_update_begin(&r_vec->rx_sync);
1482                 r_vec->rx_pkts++;
1483                 r_vec->rx_bytes += skb->len;
1484                 u64_stats_update_end(&r_vec->rx_sync);
1485
1486                 if (nn->fw_ver.major <= 3) {
1487                         nfp_net_set_hash_desc(nn->netdev, skb, rxd);
1488                 } else if (meta_len) {
1489                         void *end;
1490
1491                         end = nfp_net_parse_meta(nn->netdev, skb, meta_len);
1492                         if (unlikely(end != skb->data)) {
1493                                 u64_stats_update_begin(&r_vec->rx_sync);
1494                                 r_vec->rx_drops++;
1495                                 u64_stats_update_end(&r_vec->rx_sync);
1496
1497                                 dev_kfree_skb_any(skb);
1498                                 nn_warn_ratelimit(nn, "invalid RX packet metadata\n");
1499                                 continue;
1500                         }
1501                 }
1502
1503                 skb_record_rx_queue(skb, rx_ring->idx);
1504                 skb->protocol = eth_type_trans(skb, nn->netdev);
1505
1506                 nfp_net_rx_csum(nn, r_vec, rxd, skb);
1507
1508                 if (rxd->rxd.flags & PCIE_DESC_RX_VLAN)
1509                         __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1510                                                le16_to_cpu(rxd->rxd.vlan));
1511
1512                 napi_gro_receive(&rx_ring->r_vec->napi, skb);
1513         }
1514
1515         if (nn->is_nfp3200)
1516                 nfp_qcp_rd_ptr_add(rx_ring->qcp_rx, pkts_polled);
1517
1518         return pkts_polled;
1519 }
1520
1521 /**
1522  * nfp_net_poll() - napi poll function
1523  * @napi:    NAPI structure
1524  * @budget:  NAPI budget
1525  *
1526  * Return: number of packets polled.
1527  */
1528 static int nfp_net_poll(struct napi_struct *napi, int budget)
1529 {
1530         struct nfp_net_r_vector *r_vec =
1531                 container_of(napi, struct nfp_net_r_vector, napi);
1532         struct nfp_net_rx_ring *rx_ring = r_vec->rx_ring;
1533         struct nfp_net_tx_ring *tx_ring = r_vec->tx_ring;
1534         struct nfp_net *nn = r_vec->nfp_net;
1535         struct netdev_queue *txq;
1536         unsigned int pkts_polled;
1537
1538         tx_ring = &nn->tx_rings[rx_ring->idx];
1539         txq = netdev_get_tx_queue(nn->netdev, tx_ring->idx);
1540         nfp_net_tx_complete(tx_ring);
1541
1542         pkts_polled = nfp_net_rx(rx_ring, budget);
1543
1544         if (pkts_polled < budget) {
1545                 napi_complete_done(napi, pkts_polled);
1546                 nfp_net_irq_unmask(nn, r_vec->irq_idx);
1547         }
1548
1549         return pkts_polled;
1550 }
1551
1552 /* Setup and Configuration
1553  */
1554
1555 /**
1556  * nfp_net_tx_ring_free() - Free resources allocated to a TX ring
1557  * @tx_ring:   TX ring to free
1558  */
1559 static void nfp_net_tx_ring_free(struct nfp_net_tx_ring *tx_ring)
1560 {
1561         struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
1562         struct nfp_net *nn = r_vec->nfp_net;
1563         struct pci_dev *pdev = nn->pdev;
1564
1565         kfree(tx_ring->txbufs);
1566
1567         if (tx_ring->txds)
1568                 dma_free_coherent(&pdev->dev, tx_ring->size,
1569                                   tx_ring->txds, tx_ring->dma);
1570
1571         tx_ring->cnt = 0;
1572         tx_ring->txbufs = NULL;
1573         tx_ring->txds = NULL;
1574         tx_ring->dma = 0;
1575         tx_ring->size = 0;
1576 }
1577
1578 /**
1579  * nfp_net_tx_ring_alloc() - Allocate resource for a TX ring
1580  * @tx_ring:   TX Ring structure to allocate
1581  * @cnt:       Ring buffer count
1582  *
1583  * Return: 0 on success, negative errno otherwise.
1584  */
1585 static int nfp_net_tx_ring_alloc(struct nfp_net_tx_ring *tx_ring, u32 cnt)
1586 {
1587         struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
1588         struct nfp_net *nn = r_vec->nfp_net;
1589         struct pci_dev *pdev = nn->pdev;
1590         int sz;
1591
1592         tx_ring->cnt = cnt;
1593
1594         tx_ring->size = sizeof(*tx_ring->txds) * tx_ring->cnt;
1595         tx_ring->txds = dma_zalloc_coherent(&pdev->dev, tx_ring->size,
1596                                             &tx_ring->dma, GFP_KERNEL);
1597         if (!tx_ring->txds)
1598                 goto err_alloc;
1599
1600         sz = sizeof(*tx_ring->txbufs) * tx_ring->cnt;
1601         tx_ring->txbufs = kzalloc(sz, GFP_KERNEL);
1602         if (!tx_ring->txbufs)
1603                 goto err_alloc;
1604
1605         netif_set_xps_queue(nn->netdev, &r_vec->affinity_mask, tx_ring->idx);
1606
1607         nn_dbg(nn, "TxQ%02d: QCidx=%02d cnt=%d dma=%#llx host=%p\n",
1608                tx_ring->idx, tx_ring->qcidx,
1609                tx_ring->cnt, (unsigned long long)tx_ring->dma, tx_ring->txds);
1610
1611         return 0;
1612
1613 err_alloc:
1614         nfp_net_tx_ring_free(tx_ring);
1615         return -ENOMEM;
1616 }
1617
1618 static struct nfp_net_tx_ring *
1619 nfp_net_shadow_tx_rings_prepare(struct nfp_net *nn, u32 buf_cnt)
1620 {
1621         struct nfp_net_tx_ring *rings;
1622         unsigned int r;
1623
1624         rings = kcalloc(nn->num_tx_rings, sizeof(*rings), GFP_KERNEL);
1625         if (!rings)
1626                 return NULL;
1627
1628         for (r = 0; r < nn->num_tx_rings; r++) {
1629                 nfp_net_tx_ring_init(&rings[r], nn->tx_rings[r].r_vec, r);
1630
1631                 if (nfp_net_tx_ring_alloc(&rings[r], buf_cnt))
1632                         goto err_free_prev;
1633         }
1634
1635         return rings;
1636
1637 err_free_prev:
1638         while (r--)
1639                 nfp_net_tx_ring_free(&rings[r]);
1640         kfree(rings);
1641         return NULL;
1642 }
1643
1644 static struct nfp_net_tx_ring *
1645 nfp_net_shadow_tx_rings_swap(struct nfp_net *nn, struct nfp_net_tx_ring *rings)
1646 {
1647         struct nfp_net_tx_ring *old = nn->tx_rings;
1648         unsigned int r;
1649
1650         for (r = 0; r < nn->num_tx_rings; r++)
1651                 old[r].r_vec->tx_ring = &rings[r];
1652
1653         nn->tx_rings = rings;
1654         return old;
1655 }
1656
1657 static void
1658 nfp_net_shadow_tx_rings_free(struct nfp_net *nn, struct nfp_net_tx_ring *rings)
1659 {
1660         unsigned int r;
1661
1662         if (!rings)
1663                 return;
1664
1665         for (r = 0; r < nn->num_tx_rings; r++)
1666                 nfp_net_tx_ring_free(&rings[r]);
1667
1668         kfree(rings);
1669 }
1670
1671 /**
1672  * nfp_net_rx_ring_free() - Free resources allocated to a RX ring
1673  * @rx_ring:  RX ring to free
1674  */
1675 static void nfp_net_rx_ring_free(struct nfp_net_rx_ring *rx_ring)
1676 {
1677         struct nfp_net_r_vector *r_vec = rx_ring->r_vec;
1678         struct nfp_net *nn = r_vec->nfp_net;
1679         struct pci_dev *pdev = nn->pdev;
1680
1681         kfree(rx_ring->rxbufs);
1682
1683         if (rx_ring->rxds)
1684                 dma_free_coherent(&pdev->dev, rx_ring->size,
1685                                   rx_ring->rxds, rx_ring->dma);
1686
1687         rx_ring->cnt = 0;
1688         rx_ring->rxbufs = NULL;
1689         rx_ring->rxds = NULL;
1690         rx_ring->dma = 0;
1691         rx_ring->size = 0;
1692 }
1693
1694 /**
1695  * nfp_net_rx_ring_alloc() - Allocate resource for a RX ring
1696  * @rx_ring:  RX ring to allocate
1697  * @fl_bufsz: Size of buffers to allocate
1698  * @cnt:      Ring buffer count
1699  *
1700  * Return: 0 on success, negative errno otherwise.
1701  */
1702 static int
1703 nfp_net_rx_ring_alloc(struct nfp_net_rx_ring *rx_ring, unsigned int fl_bufsz,
1704                       u32 cnt)
1705 {
1706         struct nfp_net_r_vector *r_vec = rx_ring->r_vec;
1707         struct nfp_net *nn = r_vec->nfp_net;
1708         struct pci_dev *pdev = nn->pdev;
1709         int sz;
1710
1711         rx_ring->cnt = cnt;
1712         rx_ring->bufsz = fl_bufsz;
1713
1714         rx_ring->size = sizeof(*rx_ring->rxds) * rx_ring->cnt;
1715         rx_ring->rxds = dma_zalloc_coherent(&pdev->dev, rx_ring->size,
1716                                             &rx_ring->dma, GFP_KERNEL);
1717         if (!rx_ring->rxds)
1718                 goto err_alloc;
1719
1720         sz = sizeof(*rx_ring->rxbufs) * rx_ring->cnt;
1721         rx_ring->rxbufs = kzalloc(sz, GFP_KERNEL);
1722         if (!rx_ring->rxbufs)
1723                 goto err_alloc;
1724
1725         nn_dbg(nn, "RxQ%02d: FlQCidx=%02d RxQCidx=%02d cnt=%d dma=%#llx host=%p\n",
1726                rx_ring->idx, rx_ring->fl_qcidx, rx_ring->rx_qcidx,
1727                rx_ring->cnt, (unsigned long long)rx_ring->dma, rx_ring->rxds);
1728
1729         return 0;
1730
1731 err_alloc:
1732         nfp_net_rx_ring_free(rx_ring);
1733         return -ENOMEM;
1734 }
1735
1736 static struct nfp_net_rx_ring *
1737 nfp_net_shadow_rx_rings_prepare(struct nfp_net *nn, unsigned int fl_bufsz,
1738                                 u32 buf_cnt)
1739 {
1740         struct nfp_net_rx_ring *rings;
1741         unsigned int r;
1742
1743         rings = kcalloc(nn->num_rx_rings, sizeof(*rings), GFP_KERNEL);
1744         if (!rings)
1745                 return NULL;
1746
1747         for (r = 0; r < nn->num_rx_rings; r++) {
1748                 nfp_net_rx_ring_init(&rings[r], nn->rx_rings[r].r_vec, r);
1749
1750                 if (nfp_net_rx_ring_alloc(&rings[r], fl_bufsz, buf_cnt))
1751                         goto err_free_prev;
1752
1753                 if (nfp_net_rx_ring_bufs_alloc(nn, &rings[r]))
1754                         goto err_free_ring;
1755         }
1756
1757         return rings;
1758
1759 err_free_prev:
1760         while (r--) {
1761                 nfp_net_rx_ring_bufs_free(nn, &rings[r]);
1762 err_free_ring:
1763                 nfp_net_rx_ring_free(&rings[r]);
1764         }
1765         kfree(rings);
1766         return NULL;
1767 }
1768
1769 static struct nfp_net_rx_ring *
1770 nfp_net_shadow_rx_rings_swap(struct nfp_net *nn, struct nfp_net_rx_ring *rings)
1771 {
1772         struct nfp_net_rx_ring *old = nn->rx_rings;
1773         unsigned int r;
1774
1775         for (r = 0; r < nn->num_rx_rings; r++)
1776                 old[r].r_vec->rx_ring = &rings[r];
1777
1778         nn->rx_rings = rings;
1779         return old;
1780 }
1781
1782 static void
1783 nfp_net_shadow_rx_rings_free(struct nfp_net *nn, struct nfp_net_rx_ring *rings)
1784 {
1785         unsigned int r;
1786
1787         if (!rings)
1788                 return;
1789
1790         for (r = 0; r < nn->num_r_vecs; r++) {
1791                 nfp_net_rx_ring_bufs_free(nn, &rings[r]);
1792                 nfp_net_rx_ring_free(&rings[r]);
1793         }
1794
1795         kfree(rings);
1796 }
1797
1798 static int
1799 nfp_net_prepare_vector(struct nfp_net *nn, struct nfp_net_r_vector *r_vec,
1800                        int idx)
1801 {
1802         struct msix_entry *entry = &nn->irq_entries[r_vec->irq_idx];
1803         int err;
1804
1805         r_vec->tx_ring = &nn->tx_rings[idx];
1806         nfp_net_tx_ring_init(r_vec->tx_ring, r_vec, idx);
1807
1808         r_vec->rx_ring = &nn->rx_rings[idx];
1809         nfp_net_rx_ring_init(r_vec->rx_ring, r_vec, idx);
1810
1811         snprintf(r_vec->name, sizeof(r_vec->name),
1812                  "%s-rxtx-%d", nn->netdev->name, idx);
1813         err = request_irq(entry->vector, r_vec->handler, 0, r_vec->name, r_vec);
1814         if (err) {
1815                 nn_err(nn, "Error requesting IRQ %d\n", entry->vector);
1816                 return err;
1817         }
1818         disable_irq(entry->vector);
1819
1820         /* Setup NAPI */
1821         netif_napi_add(nn->netdev, &r_vec->napi,
1822                        nfp_net_poll, NAPI_POLL_WEIGHT);
1823
1824         irq_set_affinity_hint(entry->vector, &r_vec->affinity_mask);
1825
1826         nn_dbg(nn, "RV%02d: irq=%03d/%03d\n", idx, entry->vector, entry->entry);
1827
1828         return 0;
1829 }
1830
1831 static void
1832 nfp_net_cleanup_vector(struct nfp_net *nn, struct nfp_net_r_vector *r_vec)
1833 {
1834         struct msix_entry *entry = &nn->irq_entries[r_vec->irq_idx];
1835
1836         irq_set_affinity_hint(entry->vector, NULL);
1837         netif_napi_del(&r_vec->napi);
1838         free_irq(entry->vector, r_vec);
1839 }
1840
1841 /**
1842  * nfp_net_rss_write_itbl() - Write RSS indirection table to device
1843  * @nn:      NFP Net device to reconfigure
1844  */
1845 void nfp_net_rss_write_itbl(struct nfp_net *nn)
1846 {
1847         int i;
1848
1849         for (i = 0; i < NFP_NET_CFG_RSS_ITBL_SZ; i += 4)
1850                 nn_writel(nn, NFP_NET_CFG_RSS_ITBL + i,
1851                           get_unaligned_le32(nn->rss_itbl + i));
1852 }
1853
1854 /**
1855  * nfp_net_rss_write_key() - Write RSS hash key to device
1856  * @nn:      NFP Net device to reconfigure
1857  */
1858 void nfp_net_rss_write_key(struct nfp_net *nn)
1859 {
1860         int i;
1861
1862         for (i = 0; i < NFP_NET_CFG_RSS_KEY_SZ; i += 4)
1863                 nn_writel(nn, NFP_NET_CFG_RSS_KEY + i,
1864                           get_unaligned_le32(nn->rss_key + i));
1865 }
1866
1867 /**
1868  * nfp_net_coalesce_write_cfg() - Write irq coalescence configuration to HW
1869  * @nn:      NFP Net device to reconfigure
1870  */
1871 void nfp_net_coalesce_write_cfg(struct nfp_net *nn)
1872 {
1873         u8 i;
1874         u32 factor;
1875         u32 value;
1876
1877         /* Compute factor used to convert coalesce '_usecs' parameters to
1878          * ME timestamp ticks.  There are 16 ME clock cycles for each timestamp
1879          * count.
1880          */
1881         factor = nn->me_freq_mhz / 16;
1882
1883         /* copy RX interrupt coalesce parameters */
1884         value = (nn->rx_coalesce_max_frames << 16) |
1885                 (factor * nn->rx_coalesce_usecs);
1886         for (i = 0; i < nn->num_r_vecs; i++)
1887                 nn_writel(nn, NFP_NET_CFG_RXR_IRQ_MOD(i), value);
1888
1889         /* copy TX interrupt coalesce parameters */
1890         value = (nn->tx_coalesce_max_frames << 16) |
1891                 (factor * nn->tx_coalesce_usecs);
1892         for (i = 0; i < nn->num_r_vecs; i++)
1893                 nn_writel(nn, NFP_NET_CFG_TXR_IRQ_MOD(i), value);
1894 }
1895
1896 /**
1897  * nfp_net_write_mac_addr() - Write mac address to the device control BAR
1898  * @nn:      NFP Net device to reconfigure
1899  *
1900  * Writes the MAC address from the netdev to the device control BAR.  Does not
1901  * perform the required reconfig.  We do a bit of byte swapping dance because
1902  * firmware is LE.
1903  */
1904 static void nfp_net_write_mac_addr(struct nfp_net *nn)
1905 {
1906         nn_writel(nn, NFP_NET_CFG_MACADDR + 0,
1907                   get_unaligned_be32(nn->netdev->dev_addr));
1908         /* We can't do writew for NFP-3200 compatibility */
1909         nn_writel(nn, NFP_NET_CFG_MACADDR + 4,
1910                   get_unaligned_be16(nn->netdev->dev_addr + 4) << 16);
1911 }
1912
1913 static void nfp_net_vec_clear_ring_data(struct nfp_net *nn, unsigned int idx)
1914 {
1915         nn_writeq(nn, NFP_NET_CFG_RXR_ADDR(idx), 0);
1916         nn_writeb(nn, NFP_NET_CFG_RXR_SZ(idx), 0);
1917         nn_writeb(nn, NFP_NET_CFG_RXR_VEC(idx), 0);
1918
1919         nn_writeq(nn, NFP_NET_CFG_TXR_ADDR(idx), 0);
1920         nn_writeb(nn, NFP_NET_CFG_TXR_SZ(idx), 0);
1921         nn_writeb(nn, NFP_NET_CFG_TXR_VEC(idx), 0);
1922 }
1923
1924 /**
1925  * nfp_net_clear_config_and_disable() - Clear control BAR and disable NFP
1926  * @nn:      NFP Net device to reconfigure
1927  *
1928  * Warning: must be fully idempotent.
1929  */
1930 static void nfp_net_clear_config_and_disable(struct nfp_net *nn)
1931 {
1932         u32 new_ctrl, update;
1933         unsigned int r;
1934         int err;
1935
1936         new_ctrl = nn->ctrl;
1937         new_ctrl &= ~NFP_NET_CFG_CTRL_ENABLE;
1938         update = NFP_NET_CFG_UPDATE_GEN;
1939         update |= NFP_NET_CFG_UPDATE_MSIX;
1940         update |= NFP_NET_CFG_UPDATE_RING;
1941
1942         if (nn->cap & NFP_NET_CFG_CTRL_RINGCFG)
1943                 new_ctrl &= ~NFP_NET_CFG_CTRL_RINGCFG;
1944
1945         nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, 0);
1946         nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, 0);
1947
1948         nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
1949         err = nfp_net_reconfig(nn, update);
1950         if (err)
1951                 nn_err(nn, "Could not disable device: %d\n", err);
1952
1953         for (r = 0; r < nn->num_r_vecs; r++) {
1954                 nfp_net_rx_ring_reset(nn->r_vecs[r].rx_ring);
1955                 nfp_net_tx_ring_reset(nn, nn->r_vecs[r].tx_ring);
1956                 nfp_net_vec_clear_ring_data(nn, r);
1957         }
1958
1959         nn->ctrl = new_ctrl;
1960 }
1961
1962 static void
1963 nfp_net_vec_write_ring_data(struct nfp_net *nn, struct nfp_net_r_vector *r_vec,
1964                             unsigned int idx)
1965 {
1966         /* Write the DMA address, size and MSI-X info to the device */
1967         nn_writeq(nn, NFP_NET_CFG_RXR_ADDR(idx), r_vec->rx_ring->dma);
1968         nn_writeb(nn, NFP_NET_CFG_RXR_SZ(idx), ilog2(r_vec->rx_ring->cnt));
1969         nn_writeb(nn, NFP_NET_CFG_RXR_VEC(idx), r_vec->irq_idx);
1970
1971         nn_writeq(nn, NFP_NET_CFG_TXR_ADDR(idx), r_vec->tx_ring->dma);
1972         nn_writeb(nn, NFP_NET_CFG_TXR_SZ(idx), ilog2(r_vec->tx_ring->cnt));
1973         nn_writeb(nn, NFP_NET_CFG_TXR_VEC(idx), r_vec->irq_idx);
1974 }
1975
1976 static int __nfp_net_set_config_and_enable(struct nfp_net *nn)
1977 {
1978         u32 new_ctrl, update = 0;
1979         unsigned int r;
1980         int err;
1981
1982         new_ctrl = nn->ctrl;
1983
1984         if (nn->cap & NFP_NET_CFG_CTRL_RSS) {
1985                 nfp_net_rss_write_key(nn);
1986                 nfp_net_rss_write_itbl(nn);
1987                 nn_writel(nn, NFP_NET_CFG_RSS_CTRL, nn->rss_cfg);
1988                 update |= NFP_NET_CFG_UPDATE_RSS;
1989         }
1990
1991         if (nn->cap & NFP_NET_CFG_CTRL_IRQMOD) {
1992                 nfp_net_coalesce_write_cfg(nn);
1993
1994                 new_ctrl |= NFP_NET_CFG_CTRL_IRQMOD;
1995                 update |= NFP_NET_CFG_UPDATE_IRQMOD;
1996         }
1997
1998         for (r = 0; r < nn->num_r_vecs; r++)
1999                 nfp_net_vec_write_ring_data(nn, &nn->r_vecs[r], r);
2000
2001         nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, nn->num_tx_rings == 64 ?
2002                   0xffffffffffffffffULL : ((u64)1 << nn->num_tx_rings) - 1);
2003
2004         nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, nn->num_rx_rings == 64 ?
2005                   0xffffffffffffffffULL : ((u64)1 << nn->num_rx_rings) - 1);
2006
2007         nfp_net_write_mac_addr(nn);
2008
2009         nn_writel(nn, NFP_NET_CFG_MTU, nn->netdev->mtu);
2010         nn_writel(nn, NFP_NET_CFG_FLBUFSZ, nn->fl_bufsz);
2011
2012         /* Enable device */
2013         new_ctrl |= NFP_NET_CFG_CTRL_ENABLE;
2014         update |= NFP_NET_CFG_UPDATE_GEN;
2015         update |= NFP_NET_CFG_UPDATE_MSIX;
2016         update |= NFP_NET_CFG_UPDATE_RING;
2017         if (nn->cap & NFP_NET_CFG_CTRL_RINGCFG)
2018                 new_ctrl |= NFP_NET_CFG_CTRL_RINGCFG;
2019
2020         nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
2021         err = nfp_net_reconfig(nn, update);
2022
2023         nn->ctrl = new_ctrl;
2024
2025         for (r = 0; r < nn->num_r_vecs; r++)
2026                 nfp_net_rx_ring_fill_freelist(nn->r_vecs[r].rx_ring);
2027
2028         /* Since reconfiguration requests while NFP is down are ignored we
2029          * have to wipe the entire VXLAN configuration and reinitialize it.
2030          */
2031         if (nn->ctrl & NFP_NET_CFG_CTRL_VXLAN) {
2032                 memset(&nn->vxlan_ports, 0, sizeof(nn->vxlan_ports));
2033                 memset(&nn->vxlan_usecnt, 0, sizeof(nn->vxlan_usecnt));
2034                 udp_tunnel_get_rx_info(nn->netdev);
2035         }
2036
2037         return err;
2038 }
2039
2040 /**
2041  * nfp_net_set_config_and_enable() - Write control BAR and enable NFP
2042  * @nn:      NFP Net device to reconfigure
2043  */
2044 static int nfp_net_set_config_and_enable(struct nfp_net *nn)
2045 {
2046         int err;
2047
2048         err = __nfp_net_set_config_and_enable(nn);
2049         if (err)
2050                 nfp_net_clear_config_and_disable(nn);
2051
2052         return err;
2053 }
2054
2055 /**
2056  * nfp_net_open_stack() - Start the device from stack's perspective
2057  * @nn:      NFP Net device to reconfigure
2058  */
2059 static void nfp_net_open_stack(struct nfp_net *nn)
2060 {
2061         unsigned int r;
2062
2063         for (r = 0; r < nn->num_r_vecs; r++) {
2064                 napi_enable(&nn->r_vecs[r].napi);
2065                 enable_irq(nn->irq_entries[nn->r_vecs[r].irq_idx].vector);
2066         }
2067
2068         netif_tx_wake_all_queues(nn->netdev);
2069
2070         enable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
2071         nfp_net_read_link_status(nn);
2072 }
2073
2074 static int nfp_net_netdev_open(struct net_device *netdev)
2075 {
2076         struct nfp_net *nn = netdev_priv(netdev);
2077         int err, r;
2078
2079         if (nn->ctrl & NFP_NET_CFG_CTRL_ENABLE) {
2080                 nn_err(nn, "Dev is already enabled: 0x%08x\n", nn->ctrl);
2081                 return -EBUSY;
2082         }
2083
2084         /* Step 1: Allocate resources for rings and the like
2085          * - Request interrupts
2086          * - Allocate RX and TX ring resources
2087          * - Setup initial RSS table
2088          */
2089         err = nfp_net_aux_irq_request(nn, NFP_NET_CFG_EXN, "%s-exn",
2090                                       nn->exn_name, sizeof(nn->exn_name),
2091                                       NFP_NET_IRQ_EXN_IDX, nn->exn_handler);
2092         if (err)
2093                 return err;
2094         err = nfp_net_aux_irq_request(nn, NFP_NET_CFG_LSC, "%s-lsc",
2095                                       nn->lsc_name, sizeof(nn->lsc_name),
2096                                       NFP_NET_IRQ_LSC_IDX, nn->lsc_handler);
2097         if (err)
2098                 goto err_free_exn;
2099         disable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
2100
2101         nn->rx_rings = kcalloc(nn->num_rx_rings, sizeof(*nn->rx_rings),
2102                                GFP_KERNEL);
2103         if (!nn->rx_rings) {
2104                 err = -ENOMEM;
2105                 goto err_free_lsc;
2106         }
2107         nn->tx_rings = kcalloc(nn->num_tx_rings, sizeof(*nn->tx_rings),
2108                                GFP_KERNEL);
2109         if (!nn->tx_rings) {
2110                 err = -ENOMEM;
2111                 goto err_free_rx_rings;
2112         }
2113
2114         for (r = 0; r < nn->num_r_vecs; r++) {
2115                 err = nfp_net_prepare_vector(nn, &nn->r_vecs[r], r);
2116                 if (err)
2117                         goto err_free_prev_vecs;
2118
2119                 err = nfp_net_tx_ring_alloc(nn->r_vecs[r].tx_ring, nn->txd_cnt);
2120                 if (err)
2121                         goto err_cleanup_vec_p;
2122
2123                 err = nfp_net_rx_ring_alloc(nn->r_vecs[r].rx_ring,
2124                                             nn->fl_bufsz, nn->rxd_cnt);
2125                 if (err)
2126                         goto err_free_tx_ring_p;
2127
2128                 err = nfp_net_rx_ring_bufs_alloc(nn, nn->r_vecs[r].rx_ring);
2129                 if (err)
2130                         goto err_flush_rx_ring_p;
2131         }
2132
2133         err = netif_set_real_num_tx_queues(netdev, nn->num_tx_rings);
2134         if (err)
2135                 goto err_free_rings;
2136
2137         err = netif_set_real_num_rx_queues(netdev, nn->num_rx_rings);
2138         if (err)
2139                 goto err_free_rings;
2140
2141         /* Step 2: Configure the NFP
2142          * - Enable rings from 0 to tx_rings/rx_rings - 1.
2143          * - Write MAC address (in case it changed)
2144          * - Set the MTU
2145          * - Set the Freelist buffer size
2146          * - Enable the FW
2147          */
2148         err = nfp_net_set_config_and_enable(nn);
2149         if (err)
2150                 goto err_free_rings;
2151
2152         /* Step 3: Enable for kernel
2153          * - put some freelist descriptors on each RX ring
2154          * - enable NAPI on each ring
2155          * - enable all TX queues
2156          * - set link state
2157          */
2158         nfp_net_open_stack(nn);
2159
2160         return 0;
2161
2162 err_free_rings:
2163         r = nn->num_r_vecs;
2164 err_free_prev_vecs:
2165         while (r--) {
2166                 nfp_net_rx_ring_bufs_free(nn, nn->r_vecs[r].rx_ring);
2167 err_flush_rx_ring_p:
2168                 nfp_net_rx_ring_free(nn->r_vecs[r].rx_ring);
2169 err_free_tx_ring_p:
2170                 nfp_net_tx_ring_free(nn->r_vecs[r].tx_ring);
2171 err_cleanup_vec_p:
2172                 nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
2173         }
2174         kfree(nn->tx_rings);
2175 err_free_rx_rings:
2176         kfree(nn->rx_rings);
2177 err_free_lsc:
2178         nfp_net_aux_irq_free(nn, NFP_NET_CFG_LSC, NFP_NET_IRQ_LSC_IDX);
2179 err_free_exn:
2180         nfp_net_aux_irq_free(nn, NFP_NET_CFG_EXN, NFP_NET_IRQ_EXN_IDX);
2181         return err;
2182 }
2183
2184 /**
2185  * nfp_net_close_stack() - Quiescent the stack (part of close)
2186  * @nn:      NFP Net device to reconfigure
2187  */
2188 static void nfp_net_close_stack(struct nfp_net *nn)
2189 {
2190         unsigned int r;
2191
2192         disable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
2193         netif_carrier_off(nn->netdev);
2194         nn->link_up = false;
2195
2196         for (r = 0; r < nn->num_r_vecs; r++) {
2197                 disable_irq(nn->irq_entries[nn->r_vecs[r].irq_idx].vector);
2198                 napi_disable(&nn->r_vecs[r].napi);
2199         }
2200
2201         netif_tx_disable(nn->netdev);
2202 }
2203
2204 /**
2205  * nfp_net_close_free_all() - Free all runtime resources
2206  * @nn:      NFP Net device to reconfigure
2207  */
2208 static void nfp_net_close_free_all(struct nfp_net *nn)
2209 {
2210         unsigned int r;
2211
2212         for (r = 0; r < nn->num_r_vecs; r++) {
2213                 nfp_net_rx_ring_bufs_free(nn, nn->r_vecs[r].rx_ring);
2214                 nfp_net_rx_ring_free(nn->r_vecs[r].rx_ring);
2215                 nfp_net_tx_ring_free(nn->r_vecs[r].tx_ring);
2216                 nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
2217         }
2218
2219         kfree(nn->rx_rings);
2220         kfree(nn->tx_rings);
2221
2222         nfp_net_aux_irq_free(nn, NFP_NET_CFG_LSC, NFP_NET_IRQ_LSC_IDX);
2223         nfp_net_aux_irq_free(nn, NFP_NET_CFG_EXN, NFP_NET_IRQ_EXN_IDX);
2224 }
2225
2226 /**
2227  * nfp_net_netdev_close() - Called when the device is downed
2228  * @netdev:      netdev structure
2229  */
2230 static int nfp_net_netdev_close(struct net_device *netdev)
2231 {
2232         struct nfp_net *nn = netdev_priv(netdev);
2233
2234         if (!(nn->ctrl & NFP_NET_CFG_CTRL_ENABLE)) {
2235                 nn_err(nn, "Dev is not up: 0x%08x\n", nn->ctrl);
2236                 return 0;
2237         }
2238
2239         /* Step 1: Disable RX and TX rings from the Linux kernel perspective
2240          */
2241         nfp_net_close_stack(nn);
2242
2243         /* Step 2: Tell NFP
2244          */
2245         nfp_net_clear_config_and_disable(nn);
2246
2247         /* Step 3: Free resources
2248          */
2249         nfp_net_close_free_all(nn);
2250
2251         nn_dbg(nn, "%s down", netdev->name);
2252         return 0;
2253 }
2254
2255 static void nfp_net_set_rx_mode(struct net_device *netdev)
2256 {
2257         struct nfp_net *nn = netdev_priv(netdev);
2258         u32 new_ctrl;
2259
2260         new_ctrl = nn->ctrl;
2261
2262         if (netdev->flags & IFF_PROMISC) {
2263                 if (nn->cap & NFP_NET_CFG_CTRL_PROMISC)
2264                         new_ctrl |= NFP_NET_CFG_CTRL_PROMISC;
2265                 else
2266                         nn_warn(nn, "FW does not support promiscuous mode\n");
2267         } else {
2268                 new_ctrl &= ~NFP_NET_CFG_CTRL_PROMISC;
2269         }
2270
2271         if (new_ctrl == nn->ctrl)
2272                 return;
2273
2274         nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
2275         nfp_net_reconfig_post(nn, NFP_NET_CFG_UPDATE_GEN);
2276
2277         nn->ctrl = new_ctrl;
2278 }
2279
2280 static int nfp_net_change_mtu(struct net_device *netdev, int new_mtu)
2281 {
2282         unsigned int old_mtu, old_fl_bufsz, new_fl_bufsz;
2283         struct nfp_net *nn = netdev_priv(netdev);
2284         struct nfp_net_rx_ring *tmp_rings;
2285         int err;
2286
2287         if (new_mtu < 68 || new_mtu > nn->max_mtu) {
2288                 nn_err(nn, "New MTU (%d) is not valid\n", new_mtu);
2289                 return -EINVAL;
2290         }
2291
2292         old_mtu = netdev->mtu;
2293         old_fl_bufsz = nn->fl_bufsz;
2294         new_fl_bufsz = NFP_NET_MAX_PREPEND + ETH_HLEN + VLAN_HLEN * 2 + new_mtu;
2295
2296         if (!netif_running(netdev)) {
2297                 netdev->mtu = new_mtu;
2298                 nn->fl_bufsz = new_fl_bufsz;
2299                 return 0;
2300         }
2301
2302         /* Prepare new rings */
2303         tmp_rings = nfp_net_shadow_rx_rings_prepare(nn, new_fl_bufsz,
2304                                                     nn->rxd_cnt);
2305         if (!tmp_rings)
2306                 return -ENOMEM;
2307
2308         /* Stop device, swap in new rings, try to start the firmware */
2309         nfp_net_close_stack(nn);
2310         nfp_net_clear_config_and_disable(nn);
2311
2312         tmp_rings = nfp_net_shadow_rx_rings_swap(nn, tmp_rings);
2313
2314         netdev->mtu = new_mtu;
2315         nn->fl_bufsz = new_fl_bufsz;
2316
2317         err = nfp_net_set_config_and_enable(nn);
2318         if (err) {
2319                 const int err_new = err;
2320
2321                 /* Try with old configuration and old rings */
2322                 tmp_rings = nfp_net_shadow_rx_rings_swap(nn, tmp_rings);
2323
2324                 netdev->mtu = old_mtu;
2325                 nn->fl_bufsz = old_fl_bufsz;
2326
2327                 err = __nfp_net_set_config_and_enable(nn);
2328                 if (err)
2329                         nn_err(nn, "Can't restore MTU - FW communication failed (%d,%d)\n",
2330                                err_new, err);
2331         }
2332
2333         nfp_net_shadow_rx_rings_free(nn, tmp_rings);
2334
2335         nfp_net_open_stack(nn);
2336
2337         return err;
2338 }
2339
2340 int nfp_net_set_ring_size(struct nfp_net *nn, u32 rxd_cnt, u32 txd_cnt)
2341 {
2342         struct nfp_net_tx_ring *tx_rings = NULL;
2343         struct nfp_net_rx_ring *rx_rings = NULL;
2344         u32 old_rxd_cnt, old_txd_cnt;
2345         int err;
2346
2347         if (!netif_running(nn->netdev)) {
2348                 nn->rxd_cnt = rxd_cnt;
2349                 nn->txd_cnt = txd_cnt;
2350                 return 0;
2351         }
2352
2353         old_rxd_cnt = nn->rxd_cnt;
2354         old_txd_cnt = nn->txd_cnt;
2355
2356         /* Prepare new rings */
2357         if (nn->rxd_cnt != rxd_cnt) {
2358                 rx_rings = nfp_net_shadow_rx_rings_prepare(nn, nn->fl_bufsz,
2359                                                            rxd_cnt);
2360                 if (!rx_rings)
2361                         return -ENOMEM;
2362         }
2363         if (nn->txd_cnt != txd_cnt) {
2364                 tx_rings = nfp_net_shadow_tx_rings_prepare(nn, txd_cnt);
2365                 if (!tx_rings) {
2366                         nfp_net_shadow_rx_rings_free(nn, rx_rings);
2367                         return -ENOMEM;
2368                 }
2369         }
2370
2371         /* Stop device, swap in new rings, try to start the firmware */
2372         nfp_net_close_stack(nn);
2373         nfp_net_clear_config_and_disable(nn);
2374
2375         if (rx_rings)
2376                 rx_rings = nfp_net_shadow_rx_rings_swap(nn, rx_rings);
2377         if (tx_rings)
2378                 tx_rings = nfp_net_shadow_tx_rings_swap(nn, tx_rings);
2379
2380         nn->rxd_cnt = rxd_cnt;
2381         nn->txd_cnt = txd_cnt;
2382
2383         err = nfp_net_set_config_and_enable(nn);
2384         if (err) {
2385                 const int err_new = err;
2386
2387                 /* Try with old configuration and old rings */
2388                 if (rx_rings)
2389                         rx_rings = nfp_net_shadow_rx_rings_swap(nn, rx_rings);
2390                 if (tx_rings)
2391                         tx_rings = nfp_net_shadow_tx_rings_swap(nn, tx_rings);
2392
2393                 nn->rxd_cnt = old_rxd_cnt;
2394                 nn->txd_cnt = old_txd_cnt;
2395
2396                 err = __nfp_net_set_config_and_enable(nn);
2397                 if (err)
2398                         nn_err(nn, "Can't restore ring config - FW communication failed (%d,%d)\n",
2399                                err_new, err);
2400         }
2401
2402         nfp_net_shadow_rx_rings_free(nn, rx_rings);
2403         nfp_net_shadow_tx_rings_free(nn, tx_rings);
2404
2405         nfp_net_open_stack(nn);
2406
2407         return err;
2408 }
2409
2410 static struct rtnl_link_stats64 *nfp_net_stat64(struct net_device *netdev,
2411                                                 struct rtnl_link_stats64 *stats)
2412 {
2413         struct nfp_net *nn = netdev_priv(netdev);
2414         int r;
2415
2416         for (r = 0; r < nn->num_r_vecs; r++) {
2417                 struct nfp_net_r_vector *r_vec = &nn->r_vecs[r];
2418                 u64 data[3];
2419                 unsigned int start;
2420
2421                 do {
2422                         start = u64_stats_fetch_begin(&r_vec->rx_sync);
2423                         data[0] = r_vec->rx_pkts;
2424                         data[1] = r_vec->rx_bytes;
2425                         data[2] = r_vec->rx_drops;
2426                 } while (u64_stats_fetch_retry(&r_vec->rx_sync, start));
2427                 stats->rx_packets += data[0];
2428                 stats->rx_bytes += data[1];
2429                 stats->rx_dropped += data[2];
2430
2431                 do {
2432                         start = u64_stats_fetch_begin(&r_vec->tx_sync);
2433                         data[0] = r_vec->tx_pkts;
2434                         data[1] = r_vec->tx_bytes;
2435                         data[2] = r_vec->tx_errors;
2436                 } while (u64_stats_fetch_retry(&r_vec->tx_sync, start));
2437                 stats->tx_packets += data[0];
2438                 stats->tx_bytes += data[1];
2439                 stats->tx_errors += data[2];
2440         }
2441
2442         return stats;
2443 }
2444
2445 static bool nfp_net_ebpf_capable(struct nfp_net *nn)
2446 {
2447         if (nn->cap & NFP_NET_CFG_CTRL_BPF &&
2448             nn_readb(nn, NFP_NET_CFG_BPF_ABI) == NFP_NET_BPF_ABI)
2449                 return true;
2450         return false;
2451 }
2452
2453 static int
2454 nfp_net_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
2455                  struct tc_to_netdev *tc)
2456 {
2457         struct nfp_net *nn = netdev_priv(netdev);
2458
2459         if (TC_H_MAJ(handle) != TC_H_MAJ(TC_H_INGRESS))
2460                 return -ENOTSUPP;
2461         if (proto != htons(ETH_P_ALL))
2462                 return -ENOTSUPP;
2463
2464         if (tc->type == TC_SETUP_CLSBPF && nfp_net_ebpf_capable(nn))
2465                 return nfp_net_bpf_offload(nn, handle, proto, tc->cls_bpf);
2466
2467         return -EINVAL;
2468 }
2469
2470 static int nfp_net_set_features(struct net_device *netdev,
2471                                 netdev_features_t features)
2472 {
2473         netdev_features_t changed = netdev->features ^ features;
2474         struct nfp_net *nn = netdev_priv(netdev);
2475         u32 new_ctrl;
2476         int err;
2477
2478         /* Assume this is not called with features we have not advertised */
2479
2480         new_ctrl = nn->ctrl;
2481
2482         if (changed & NETIF_F_RXCSUM) {
2483                 if (features & NETIF_F_RXCSUM)
2484                         new_ctrl |= NFP_NET_CFG_CTRL_RXCSUM;
2485                 else
2486                         new_ctrl &= ~NFP_NET_CFG_CTRL_RXCSUM;
2487         }
2488
2489         if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
2490                 if (features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))
2491                         new_ctrl |= NFP_NET_CFG_CTRL_TXCSUM;
2492                 else
2493                         new_ctrl &= ~NFP_NET_CFG_CTRL_TXCSUM;
2494         }
2495
2496         if (changed & (NETIF_F_TSO | NETIF_F_TSO6)) {
2497                 if (features & (NETIF_F_TSO | NETIF_F_TSO6))
2498                         new_ctrl |= NFP_NET_CFG_CTRL_LSO;
2499                 else
2500                         new_ctrl &= ~NFP_NET_CFG_CTRL_LSO;
2501         }
2502
2503         if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
2504                 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2505                         new_ctrl |= NFP_NET_CFG_CTRL_RXVLAN;
2506                 else
2507                         new_ctrl &= ~NFP_NET_CFG_CTRL_RXVLAN;
2508         }
2509
2510         if (changed & NETIF_F_HW_VLAN_CTAG_TX) {
2511                 if (features & NETIF_F_HW_VLAN_CTAG_TX)
2512                         new_ctrl |= NFP_NET_CFG_CTRL_TXVLAN;
2513                 else
2514                         new_ctrl &= ~NFP_NET_CFG_CTRL_TXVLAN;
2515         }
2516
2517         if (changed & NETIF_F_SG) {
2518                 if (features & NETIF_F_SG)
2519                         new_ctrl |= NFP_NET_CFG_CTRL_GATHER;
2520                 else
2521                         new_ctrl &= ~NFP_NET_CFG_CTRL_GATHER;
2522         }
2523
2524         if (changed & NETIF_F_HW_TC && nn->ctrl & NFP_NET_CFG_CTRL_BPF) {
2525                 nn_err(nn, "Cannot disable HW TC offload while in use\n");
2526                 return -EBUSY;
2527         }
2528
2529         nn_dbg(nn, "Feature change 0x%llx -> 0x%llx (changed=0x%llx)\n",
2530                netdev->features, features, changed);
2531
2532         if (new_ctrl == nn->ctrl)
2533                 return 0;
2534
2535         nn_dbg(nn, "NIC ctrl: 0x%x -> 0x%x\n", nn->ctrl, new_ctrl);
2536         nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
2537         err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_GEN);
2538         if (err)
2539                 return err;
2540
2541         nn->ctrl = new_ctrl;
2542
2543         return 0;
2544 }
2545
2546 static netdev_features_t
2547 nfp_net_features_check(struct sk_buff *skb, struct net_device *dev,
2548                        netdev_features_t features)
2549 {
2550         u8 l4_hdr;
2551
2552         /* We can't do TSO over double tagged packets (802.1AD) */
2553         features &= vlan_features_check(skb, features);
2554
2555         if (!skb->encapsulation)
2556                 return features;
2557
2558         /* Ensure that inner L4 header offset fits into TX descriptor field */
2559         if (skb_is_gso(skb)) {
2560                 u32 hdrlen;
2561
2562                 hdrlen = skb_inner_transport_header(skb) - skb->data +
2563                         inner_tcp_hdrlen(skb);
2564
2565                 if (unlikely(hdrlen > NFP_NET_LSO_MAX_HDR_SZ))
2566                         features &= ~NETIF_F_GSO_MASK;
2567         }
2568
2569         /* VXLAN/GRE check */
2570         switch (vlan_get_protocol(skb)) {
2571         case htons(ETH_P_IP):
2572                 l4_hdr = ip_hdr(skb)->protocol;
2573                 break;
2574         case htons(ETH_P_IPV6):
2575                 l4_hdr = ipv6_hdr(skb)->nexthdr;
2576                 break;
2577         default:
2578                 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2579         }
2580
2581         if (skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
2582             skb->inner_protocol != htons(ETH_P_TEB) ||
2583             (l4_hdr != IPPROTO_UDP && l4_hdr != IPPROTO_GRE) ||
2584             (l4_hdr == IPPROTO_UDP &&
2585              (skb_inner_mac_header(skb) - skb_transport_header(skb) !=
2586               sizeof(struct udphdr) + sizeof(struct vxlanhdr))))
2587                 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2588
2589         return features;
2590 }
2591
2592 /**
2593  * nfp_net_set_vxlan_port() - set vxlan port in SW and reconfigure HW
2594  * @nn:   NFP Net device to reconfigure
2595  * @idx:  Index into the port table where new port should be written
2596  * @port: UDP port to configure (pass zero to remove VXLAN port)
2597  */
2598 static void nfp_net_set_vxlan_port(struct nfp_net *nn, int idx, __be16 port)
2599 {
2600         int i;
2601
2602         nn->vxlan_ports[idx] = port;
2603
2604         if (!(nn->ctrl & NFP_NET_CFG_CTRL_VXLAN))
2605                 return;
2606
2607         BUILD_BUG_ON(NFP_NET_N_VXLAN_PORTS & 1);
2608         for (i = 0; i < NFP_NET_N_VXLAN_PORTS; i += 2)
2609                 nn_writel(nn, NFP_NET_CFG_VXLAN_PORT + i * sizeof(port),
2610                           be16_to_cpu(nn->vxlan_ports[i + 1]) << 16 |
2611                           be16_to_cpu(nn->vxlan_ports[i]));
2612
2613         nfp_net_reconfig_post(nn, NFP_NET_CFG_UPDATE_VXLAN);
2614 }
2615
2616 /**
2617  * nfp_net_find_vxlan_idx() - find table entry of the port or a free one
2618  * @nn:   NFP Network structure
2619  * @port: UDP port to look for
2620  *
2621  * Return: if the port is already in the table -- it's position;
2622  *         if the port is not in the table -- free position to use;
2623  *         if the table is full -- -ENOSPC.
2624  */
2625 static int nfp_net_find_vxlan_idx(struct nfp_net *nn, __be16 port)
2626 {
2627         int i, free_idx = -ENOSPC;
2628
2629         for (i = 0; i < NFP_NET_N_VXLAN_PORTS; i++) {
2630                 if (nn->vxlan_ports[i] == port)
2631                         return i;
2632                 if (!nn->vxlan_usecnt[i])
2633                         free_idx = i;
2634         }
2635
2636         return free_idx;
2637 }
2638
2639 static void nfp_net_add_vxlan_port(struct net_device *netdev,
2640                                    struct udp_tunnel_info *ti)
2641 {
2642         struct nfp_net *nn = netdev_priv(netdev);
2643         int idx;
2644
2645         if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
2646                 return;
2647
2648         idx = nfp_net_find_vxlan_idx(nn, ti->port);
2649         if (idx == -ENOSPC)
2650                 return;
2651
2652         if (!nn->vxlan_usecnt[idx]++)
2653                 nfp_net_set_vxlan_port(nn, idx, ti->port);
2654 }
2655
2656 static void nfp_net_del_vxlan_port(struct net_device *netdev,
2657                                    struct udp_tunnel_info *ti)
2658 {
2659         struct nfp_net *nn = netdev_priv(netdev);
2660         int idx;
2661
2662         if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
2663                 return;
2664
2665         idx = nfp_net_find_vxlan_idx(nn, ti->port);
2666         if (idx == -ENOSPC || !nn->vxlan_usecnt[idx])
2667                 return;
2668
2669         if (!--nn->vxlan_usecnt[idx])
2670                 nfp_net_set_vxlan_port(nn, idx, 0);
2671 }
2672
2673 static const struct net_device_ops nfp_net_netdev_ops = {
2674         .ndo_open               = nfp_net_netdev_open,
2675         .ndo_stop               = nfp_net_netdev_close,
2676         .ndo_start_xmit         = nfp_net_tx,
2677         .ndo_get_stats64        = nfp_net_stat64,
2678         .ndo_setup_tc           = nfp_net_setup_tc,
2679         .ndo_tx_timeout         = nfp_net_tx_timeout,
2680         .ndo_set_rx_mode        = nfp_net_set_rx_mode,
2681         .ndo_change_mtu         = nfp_net_change_mtu,
2682         .ndo_set_mac_address    = eth_mac_addr,
2683         .ndo_set_features       = nfp_net_set_features,
2684         .ndo_features_check     = nfp_net_features_check,
2685         .ndo_udp_tunnel_add     = nfp_net_add_vxlan_port,
2686         .ndo_udp_tunnel_del     = nfp_net_del_vxlan_port,
2687 };
2688
2689 /**
2690  * nfp_net_info() - Print general info about the NIC
2691  * @nn:      NFP Net device to reconfigure
2692  */
2693 void nfp_net_info(struct nfp_net *nn)
2694 {
2695         nn_info(nn, "Netronome %s %sNetdev: TxQs=%d/%d RxQs=%d/%d\n",
2696                 nn->is_nfp3200 ? "NFP-32xx" : "NFP-6xxx",
2697                 nn->is_vf ? "VF " : "",
2698                 nn->num_tx_rings, nn->max_tx_rings,
2699                 nn->num_rx_rings, nn->max_rx_rings);
2700         nn_info(nn, "VER: %d.%d.%d.%d, Maximum supported MTU: %d\n",
2701                 nn->fw_ver.resv, nn->fw_ver.class,
2702                 nn->fw_ver.major, nn->fw_ver.minor,
2703                 nn->max_mtu);
2704         nn_info(nn, "CAP: %#x %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2705                 nn->cap,
2706                 nn->cap & NFP_NET_CFG_CTRL_PROMISC  ? "PROMISC "  : "",
2707                 nn->cap & NFP_NET_CFG_CTRL_L2BC     ? "L2BCFILT " : "",
2708                 nn->cap & NFP_NET_CFG_CTRL_L2MC     ? "L2MCFILT " : "",
2709                 nn->cap & NFP_NET_CFG_CTRL_RXCSUM   ? "RXCSUM "   : "",
2710                 nn->cap & NFP_NET_CFG_CTRL_TXCSUM   ? "TXCSUM "   : "",
2711                 nn->cap & NFP_NET_CFG_CTRL_RXVLAN   ? "RXVLAN "   : "",
2712                 nn->cap & NFP_NET_CFG_CTRL_TXVLAN   ? "TXVLAN "   : "",
2713                 nn->cap & NFP_NET_CFG_CTRL_SCATTER  ? "SCATTER "  : "",
2714                 nn->cap & NFP_NET_CFG_CTRL_GATHER   ? "GATHER "   : "",
2715                 nn->cap & NFP_NET_CFG_CTRL_LSO      ? "TSO "      : "",
2716                 nn->cap & NFP_NET_CFG_CTRL_RSS      ? "RSS "      : "",
2717                 nn->cap & NFP_NET_CFG_CTRL_L2SWITCH ? "L2SWITCH " : "",
2718                 nn->cap & NFP_NET_CFG_CTRL_MSIXAUTO ? "AUTOMASK " : "",
2719                 nn->cap & NFP_NET_CFG_CTRL_IRQMOD   ? "IRQMOD "   : "",
2720                 nn->cap & NFP_NET_CFG_CTRL_VXLAN    ? "VXLAN "    : "",
2721                 nn->cap & NFP_NET_CFG_CTRL_NVGRE    ? "NVGRE "    : "",
2722                 nfp_net_ebpf_capable(nn)            ? "BPF "      : "");
2723 }
2724
2725 /**
2726  * nfp_net_netdev_alloc() - Allocate netdev and related structure
2727  * @pdev:         PCI device
2728  * @max_tx_rings: Maximum number of TX rings supported by device
2729  * @max_rx_rings: Maximum number of RX rings supported by device
2730  *
2731  * This function allocates a netdev device and fills in the initial
2732  * part of the @struct nfp_net structure.
2733  *
2734  * Return: NFP Net device structure, or ERR_PTR on error.
2735  */
2736 struct nfp_net *nfp_net_netdev_alloc(struct pci_dev *pdev,
2737                                      int max_tx_rings, int max_rx_rings)
2738 {
2739         struct net_device *netdev;
2740         struct nfp_net *nn;
2741         int nqs;
2742
2743         netdev = alloc_etherdev_mqs(sizeof(struct nfp_net),
2744                                     max_tx_rings, max_rx_rings);
2745         if (!netdev)
2746                 return ERR_PTR(-ENOMEM);
2747
2748         SET_NETDEV_DEV(netdev, &pdev->dev);
2749         nn = netdev_priv(netdev);
2750
2751         nn->netdev = netdev;
2752         nn->pdev = pdev;
2753
2754         nn->max_tx_rings = max_tx_rings;
2755         nn->max_rx_rings = max_rx_rings;
2756
2757         nqs = netif_get_num_default_rss_queues();
2758         nn->num_tx_rings = min_t(int, nqs, max_tx_rings);
2759         nn->num_rx_rings = min_t(int, nqs, max_rx_rings);
2760
2761         nn->txd_cnt = NFP_NET_TX_DESCS_DEFAULT;
2762         nn->rxd_cnt = NFP_NET_RX_DESCS_DEFAULT;
2763
2764         spin_lock_init(&nn->reconfig_lock);
2765         spin_lock_init(&nn->rx_filter_lock);
2766         spin_lock_init(&nn->link_status_lock);
2767
2768         setup_timer(&nn->reconfig_timer,
2769                     nfp_net_reconfig_timer, (unsigned long)nn);
2770         setup_timer(&nn->rx_filter_stats_timer,
2771                     nfp_net_filter_stats_timer, (unsigned long)nn);
2772
2773         return nn;
2774 }
2775
2776 /**
2777  * nfp_net_netdev_free() - Undo what @nfp_net_netdev_alloc() did
2778  * @nn:      NFP Net device to reconfigure
2779  */
2780 void nfp_net_netdev_free(struct nfp_net *nn)
2781 {
2782         free_netdev(nn->netdev);
2783 }
2784
2785 /**
2786  * nfp_net_rss_init() - Set the initial RSS parameters
2787  * @nn:      NFP Net device to reconfigure
2788  */
2789 static void nfp_net_rss_init(struct nfp_net *nn)
2790 {
2791         int i;
2792
2793         netdev_rss_key_fill(nn->rss_key, NFP_NET_CFG_RSS_KEY_SZ);
2794
2795         for (i = 0; i < sizeof(nn->rss_itbl); i++)
2796                 nn->rss_itbl[i] =
2797                         ethtool_rxfh_indir_default(i, nn->num_rx_rings);
2798
2799         /* Enable IPv4/IPv6 TCP by default */
2800         nn->rss_cfg = NFP_NET_CFG_RSS_IPV4_TCP |
2801                       NFP_NET_CFG_RSS_IPV6_TCP |
2802                       NFP_NET_CFG_RSS_TOEPLITZ |
2803                       NFP_NET_CFG_RSS_MASK;
2804 }
2805
2806 /**
2807  * nfp_net_irqmod_init() - Set the initial IRQ moderation parameters
2808  * @nn:      NFP Net device to reconfigure
2809  */
2810 static void nfp_net_irqmod_init(struct nfp_net *nn)
2811 {
2812         nn->rx_coalesce_usecs      = 50;
2813         nn->rx_coalesce_max_frames = 64;
2814         nn->tx_coalesce_usecs      = 50;
2815         nn->tx_coalesce_max_frames = 64;
2816 }
2817
2818 /**
2819  * nfp_net_netdev_init() - Initialise/finalise the netdev structure
2820  * @netdev:      netdev structure
2821  *
2822  * Return: 0 on success or negative errno on error.
2823  */
2824 int nfp_net_netdev_init(struct net_device *netdev)
2825 {
2826         struct nfp_net *nn = netdev_priv(netdev);
2827         int err;
2828
2829         /* Get some of the read-only fields from the BAR */
2830         nn->cap = nn_readl(nn, NFP_NET_CFG_CAP);
2831         nn->max_mtu = nn_readl(nn, NFP_NET_CFG_MAX_MTU);
2832
2833         nfp_net_write_mac_addr(nn);
2834
2835         /* Set default MTU and Freelist buffer size */
2836         if (nn->max_mtu < NFP_NET_DEFAULT_MTU)
2837                 netdev->mtu = nn->max_mtu;
2838         else
2839                 netdev->mtu = NFP_NET_DEFAULT_MTU;
2840         nn->fl_bufsz = NFP_NET_DEFAULT_RX_BUFSZ;
2841
2842         /* Advertise/enable offloads based on capabilities
2843          *
2844          * Note: netdev->features show the currently enabled features
2845          * and netdev->hw_features advertises which features are
2846          * supported.  By default we enable most features.
2847          */
2848         netdev->hw_features = NETIF_F_HIGHDMA;
2849         if (nn->cap & NFP_NET_CFG_CTRL_RXCSUM) {
2850                 netdev->hw_features |= NETIF_F_RXCSUM;
2851                 nn->ctrl |= NFP_NET_CFG_CTRL_RXCSUM;
2852         }
2853         if (nn->cap & NFP_NET_CFG_CTRL_TXCSUM) {
2854                 netdev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
2855                 nn->ctrl |= NFP_NET_CFG_CTRL_TXCSUM;
2856         }
2857         if (nn->cap & NFP_NET_CFG_CTRL_GATHER) {
2858                 netdev->hw_features |= NETIF_F_SG;
2859                 nn->ctrl |= NFP_NET_CFG_CTRL_GATHER;
2860         }
2861         if ((nn->cap & NFP_NET_CFG_CTRL_LSO) && nn->fw_ver.major > 2) {
2862                 netdev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
2863                 nn->ctrl |= NFP_NET_CFG_CTRL_LSO;
2864         }
2865         if (nn->cap & NFP_NET_CFG_CTRL_RSS) {
2866                 netdev->hw_features |= NETIF_F_RXHASH;
2867                 nfp_net_rss_init(nn);
2868                 nn->ctrl |= NFP_NET_CFG_CTRL_RSS;
2869         }
2870         if (nn->cap & NFP_NET_CFG_CTRL_VXLAN &&
2871             nn->cap & NFP_NET_CFG_CTRL_NVGRE) {
2872                 if (nn->cap & NFP_NET_CFG_CTRL_LSO)
2873                         netdev->hw_features |= NETIF_F_GSO_GRE |
2874                                                NETIF_F_GSO_UDP_TUNNEL;
2875                 nn->ctrl |= NFP_NET_CFG_CTRL_VXLAN | NFP_NET_CFG_CTRL_NVGRE;
2876
2877                 netdev->hw_enc_features = netdev->hw_features;
2878         }
2879
2880         netdev->vlan_features = netdev->hw_features;
2881
2882         if (nn->cap & NFP_NET_CFG_CTRL_RXVLAN) {
2883                 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
2884                 nn->ctrl |= NFP_NET_CFG_CTRL_RXVLAN;
2885         }
2886         if (nn->cap & NFP_NET_CFG_CTRL_TXVLAN) {
2887                 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
2888                 nn->ctrl |= NFP_NET_CFG_CTRL_TXVLAN;
2889         }
2890
2891         netdev->features = netdev->hw_features;
2892
2893         if (nfp_net_ebpf_capable(nn))
2894                 netdev->hw_features |= NETIF_F_HW_TC;
2895
2896         /* Advertise but disable TSO by default. */
2897         netdev->features &= ~(NETIF_F_TSO | NETIF_F_TSO6);
2898
2899         /* Allow L2 Broadcast and Multicast through by default, if supported */
2900         if (nn->cap & NFP_NET_CFG_CTRL_L2BC)
2901                 nn->ctrl |= NFP_NET_CFG_CTRL_L2BC;
2902         if (nn->cap & NFP_NET_CFG_CTRL_L2MC)
2903                 nn->ctrl |= NFP_NET_CFG_CTRL_L2MC;
2904
2905         /* Allow IRQ moderation, if supported */
2906         if (nn->cap & NFP_NET_CFG_CTRL_IRQMOD) {
2907                 nfp_net_irqmod_init(nn);
2908                 nn->ctrl |= NFP_NET_CFG_CTRL_IRQMOD;
2909         }
2910
2911         /* On NFP-3200 enable MSI-X auto-masking, if supported and the
2912          * interrupts are not shared.
2913          */
2914         if (nn->is_nfp3200 && nn->cap & NFP_NET_CFG_CTRL_MSIXAUTO)
2915                 nn->ctrl |= NFP_NET_CFG_CTRL_MSIXAUTO;
2916
2917         /* On NFP4000/NFP6000, determine RX packet/metadata boundary offset */
2918         if (nn->fw_ver.major >= 2)
2919                 nn->rx_offset = nn_readl(nn, NFP_NET_CFG_RX_OFFSET);
2920         else
2921                 nn->rx_offset = NFP_NET_RX_OFFSET;
2922
2923         /* Stash the re-configuration queue away.  First odd queue in TX Bar */
2924         nn->qcp_cfg = nn->tx_bar + NFP_QCP_QUEUE_ADDR_SZ;
2925
2926         /* Make sure the FW knows the netdev is supposed to be disabled here */
2927         nn_writel(nn, NFP_NET_CFG_CTRL, 0);
2928         nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, 0);
2929         nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, 0);
2930         err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_RING |
2931                                    NFP_NET_CFG_UPDATE_GEN);
2932         if (err)
2933                 return err;
2934
2935         /* Finalise the netdev setup */
2936         ether_setup(netdev);
2937         netdev->netdev_ops = &nfp_net_netdev_ops;
2938         netdev->watchdog_timeo = msecs_to_jiffies(5 * 1000);
2939         netif_carrier_off(netdev);
2940
2941         nfp_net_set_ethtool_ops(netdev);
2942         nfp_net_irqs_assign(netdev);
2943
2944         return register_netdev(netdev);
2945 }
2946
2947 /**
2948  * nfp_net_netdev_clean() - Undo what nfp_net_netdev_init() did.
2949  * @netdev:      netdev structure
2950  */
2951 void nfp_net_netdev_clean(struct net_device *netdev)
2952 {
2953         unregister_netdev(netdev);
2954 }