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