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