nfp: move vNIC reset before netdev init
[linux-2.6-microblaze.git] / drivers / net / ethernet / netronome / nfp / nfp_net_common.c
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2015-2018 Netronome Systems, Inc. */
3
4 /*
5  * nfp_net_common.c
6  * Netronome network device driver: Common functions between PF and VF
7  * Authors: Jakub Kicinski <jakub.kicinski@netronome.com>
8  *          Jason McMullan <jason.mcmullan@netronome.com>
9  *          Rolf Neugebauer <rolf.neugebauer@netronome.com>
10  *          Brad Petrus <brad.petrus@netronome.com>
11  *          Chris Telfer <chris.telfer@netronome.com>
12  */
13
14 #include <linux/bitfield.h>
15 #include <linux/bpf.h>
16 #include <linux/bpf_trace.h>
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/fs.h>
21 #include <linux/netdevice.h>
22 #include <linux/etherdevice.h>
23 #include <linux/interrupt.h>
24 #include <linux/ip.h>
25 #include <linux/ipv6.h>
26 #include <linux/lockdep.h>
27 #include <linux/mm.h>
28 #include <linux/overflow.h>
29 #include <linux/page_ref.h>
30 #include <linux/pci.h>
31 #include <linux/pci_regs.h>
32 #include <linux/msi.h>
33 #include <linux/ethtool.h>
34 #include <linux/log2.h>
35 #include <linux/if_vlan.h>
36 #include <linux/random.h>
37 #include <linux/vmalloc.h>
38 #include <linux/ktime.h>
39
40 #include <net/vxlan.h>
41
42 #include "nfpcore/nfp_nsp.h"
43 #include "nfp_app.h"
44 #include "nfp_net_ctrl.h"
45 #include "nfp_net.h"
46 #include "nfp_net_sriov.h"
47 #include "nfp_port.h"
48
49 /**
50  * nfp_net_get_fw_version() - Read and parse the FW version
51  * @fw_ver:     Output fw_version structure to read to
52  * @ctrl_bar:   Mapped address of the control BAR
53  */
54 void nfp_net_get_fw_version(struct nfp_net_fw_version *fw_ver,
55                             void __iomem *ctrl_bar)
56 {
57         u32 reg;
58
59         reg = readl(ctrl_bar + NFP_NET_CFG_VERSION);
60         put_unaligned_le32(reg, fw_ver);
61 }
62
63 static dma_addr_t nfp_net_dma_map_rx(struct nfp_net_dp *dp, void *frag)
64 {
65         return dma_map_single_attrs(dp->dev, frag + NFP_NET_RX_BUF_HEADROOM,
66                                     dp->fl_bufsz - NFP_NET_RX_BUF_NON_DATA,
67                                     dp->rx_dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
68 }
69
70 static void
71 nfp_net_dma_sync_dev_rx(const struct nfp_net_dp *dp, dma_addr_t dma_addr)
72 {
73         dma_sync_single_for_device(dp->dev, dma_addr,
74                                    dp->fl_bufsz - NFP_NET_RX_BUF_NON_DATA,
75                                    dp->rx_dma_dir);
76 }
77
78 static void nfp_net_dma_unmap_rx(struct nfp_net_dp *dp, dma_addr_t dma_addr)
79 {
80         dma_unmap_single_attrs(dp->dev, dma_addr,
81                                dp->fl_bufsz - NFP_NET_RX_BUF_NON_DATA,
82                                dp->rx_dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
83 }
84
85 static void nfp_net_dma_sync_cpu_rx(struct nfp_net_dp *dp, dma_addr_t dma_addr,
86                                     unsigned int len)
87 {
88         dma_sync_single_for_cpu(dp->dev, dma_addr - NFP_NET_RX_BUF_HEADROOM,
89                                 len, dp->rx_dma_dir);
90 }
91
92 /* Firmware reconfig
93  *
94  * Firmware reconfig may take a while so we have two versions of it -
95  * synchronous and asynchronous (posted).  All synchronous callers are holding
96  * RTNL so we don't have to worry about serializing them.
97  */
98 static void nfp_net_reconfig_start(struct nfp_net *nn, u32 update)
99 {
100         nn_writel(nn, NFP_NET_CFG_UPDATE, update);
101         /* ensure update is written before pinging HW */
102         nn_pci_flush(nn);
103         nfp_qcp_wr_ptr_add(nn->qcp_cfg, 1);
104         nn->reconfig_in_progress_update = update;
105 }
106
107 /* Pass 0 as update to run posted reconfigs. */
108 static void nfp_net_reconfig_start_async(struct nfp_net *nn, u32 update)
109 {
110         update |= nn->reconfig_posted;
111         nn->reconfig_posted = 0;
112
113         nfp_net_reconfig_start(nn, update);
114
115         nn->reconfig_timer_active = true;
116         mod_timer(&nn->reconfig_timer, jiffies + NFP_NET_POLL_TIMEOUT * HZ);
117 }
118
119 static bool nfp_net_reconfig_check_done(struct nfp_net *nn, bool last_check)
120 {
121         u32 reg;
122
123         reg = nn_readl(nn, NFP_NET_CFG_UPDATE);
124         if (reg == 0)
125                 return true;
126         if (reg & NFP_NET_CFG_UPDATE_ERR) {
127                 nn_err(nn, "Reconfig error (status: 0x%08x update: 0x%08x ctrl: 0x%08x)\n",
128                        reg, nn->reconfig_in_progress_update,
129                        nn_readl(nn, NFP_NET_CFG_CTRL));
130                 return true;
131         } else if (last_check) {
132                 nn_err(nn, "Reconfig timeout (status: 0x%08x update: 0x%08x ctrl: 0x%08x)\n",
133                        reg, nn->reconfig_in_progress_update,
134                        nn_readl(nn, NFP_NET_CFG_CTRL));
135                 return true;
136         }
137
138         return false;
139 }
140
141 static bool __nfp_net_reconfig_wait(struct nfp_net *nn, unsigned long deadline)
142 {
143         bool timed_out = false;
144         int i;
145
146         /* Poll update field, waiting for NFP to ack the config.
147          * Do an opportunistic wait-busy loop, afterward sleep.
148          */
149         for (i = 0; i < 50; i++) {
150                 if (nfp_net_reconfig_check_done(nn, false))
151                         return false;
152                 udelay(4);
153         }
154
155         while (!nfp_net_reconfig_check_done(nn, timed_out)) {
156                 usleep_range(250, 500);
157                 timed_out = time_is_before_eq_jiffies(deadline);
158         }
159
160         return timed_out;
161 }
162
163 static int nfp_net_reconfig_wait(struct nfp_net *nn, unsigned long deadline)
164 {
165         if (__nfp_net_reconfig_wait(nn, deadline))
166                 return -EIO;
167
168         if (nn_readl(nn, NFP_NET_CFG_UPDATE) & NFP_NET_CFG_UPDATE_ERR)
169                 return -EIO;
170
171         return 0;
172 }
173
174 static void nfp_net_reconfig_timer(struct timer_list *t)
175 {
176         struct nfp_net *nn = from_timer(nn, t, reconfig_timer);
177
178         spin_lock_bh(&nn->reconfig_lock);
179
180         nn->reconfig_timer_active = false;
181
182         /* If sync caller is present it will take over from us */
183         if (nn->reconfig_sync_present)
184                 goto done;
185
186         /* Read reconfig status and report errors */
187         nfp_net_reconfig_check_done(nn, true);
188
189         if (nn->reconfig_posted)
190                 nfp_net_reconfig_start_async(nn, 0);
191 done:
192         spin_unlock_bh(&nn->reconfig_lock);
193 }
194
195 /**
196  * nfp_net_reconfig_post() - Post async reconfig request
197  * @nn:      NFP Net device to reconfigure
198  * @update:  The value for the update field in the BAR config
199  *
200  * Record FW reconfiguration request.  Reconfiguration will be kicked off
201  * whenever reconfiguration machinery is idle.  Multiple requests can be
202  * merged together!
203  */
204 static void nfp_net_reconfig_post(struct nfp_net *nn, u32 update)
205 {
206         spin_lock_bh(&nn->reconfig_lock);
207
208         /* Sync caller will kick off async reconf when it's done, just post */
209         if (nn->reconfig_sync_present) {
210                 nn->reconfig_posted |= update;
211                 goto done;
212         }
213
214         /* Opportunistically check if the previous command is done */
215         if (!nn->reconfig_timer_active ||
216             nfp_net_reconfig_check_done(nn, false))
217                 nfp_net_reconfig_start_async(nn, update);
218         else
219                 nn->reconfig_posted |= update;
220 done:
221         spin_unlock_bh(&nn->reconfig_lock);
222 }
223
224 static void nfp_net_reconfig_sync_enter(struct nfp_net *nn)
225 {
226         bool cancelled_timer = false;
227         u32 pre_posted_requests;
228
229         spin_lock_bh(&nn->reconfig_lock);
230
231         nn->reconfig_sync_present = true;
232
233         if (nn->reconfig_timer_active) {
234                 nn->reconfig_timer_active = false;
235                 cancelled_timer = true;
236         }
237         pre_posted_requests = nn->reconfig_posted;
238         nn->reconfig_posted = 0;
239
240         spin_unlock_bh(&nn->reconfig_lock);
241
242         if (cancelled_timer) {
243                 del_timer_sync(&nn->reconfig_timer);
244                 nfp_net_reconfig_wait(nn, nn->reconfig_timer.expires);
245         }
246
247         /* Run the posted reconfigs which were issued before we started */
248         if (pre_posted_requests) {
249                 nfp_net_reconfig_start(nn, pre_posted_requests);
250                 nfp_net_reconfig_wait(nn, jiffies + HZ * NFP_NET_POLL_TIMEOUT);
251         }
252 }
253
254 static void nfp_net_reconfig_wait_posted(struct nfp_net *nn)
255 {
256         nfp_net_reconfig_sync_enter(nn);
257
258         spin_lock_bh(&nn->reconfig_lock);
259         nn->reconfig_sync_present = false;
260         spin_unlock_bh(&nn->reconfig_lock);
261 }
262
263 /**
264  * __nfp_net_reconfig() - Reconfigure the firmware
265  * @nn:      NFP Net device to reconfigure
266  * @update:  The value for the update field in the BAR config
267  *
268  * Write the update word to the BAR and ping the reconfig queue.  The
269  * poll until the firmware has acknowledged the update by zeroing the
270  * update word.
271  *
272  * Return: Negative errno on error, 0 on success
273  */
274 static int __nfp_net_reconfig(struct nfp_net *nn, u32 update)
275 {
276         int ret;
277
278         lockdep_assert_held(&nn->bar_lock);
279
280         nfp_net_reconfig_sync_enter(nn);
281
282         nfp_net_reconfig_start(nn, update);
283         ret = nfp_net_reconfig_wait(nn, jiffies + HZ * NFP_NET_POLL_TIMEOUT);
284
285         spin_lock_bh(&nn->reconfig_lock);
286
287         if (nn->reconfig_posted)
288                 nfp_net_reconfig_start_async(nn, 0);
289
290         nn->reconfig_sync_present = false;
291
292         spin_unlock_bh(&nn->reconfig_lock);
293
294         return ret;
295 }
296
297 int nfp_net_reconfig(struct nfp_net *nn, u32 update)
298 {
299         int ret;
300
301         nn_ctrl_bar_lock(nn);
302         ret = __nfp_net_reconfig(nn, update);
303         nn_ctrl_bar_unlock(nn);
304
305         return ret;
306 }
307
308 int nfp_net_mbox_lock(struct nfp_net *nn, unsigned int data_size)
309 {
310         if (nn->tlv_caps.mbox_len < NFP_NET_CFG_MBOX_SIMPLE_VAL + data_size) {
311                 nn_err(nn, "mailbox too small for %u of data (%u)\n",
312                        data_size, nn->tlv_caps.mbox_len);
313                 return -EIO;
314         }
315
316         nn_ctrl_bar_lock(nn);
317         return 0;
318 }
319
320 /**
321  * nfp_net_mbox_reconfig() - Reconfigure the firmware via the mailbox
322  * @nn:        NFP Net device to reconfigure
323  * @mbox_cmd:  The value for the mailbox command
324  *
325  * Helper function for mailbox updates
326  *
327  * Return: Negative errno on error, 0 on success
328  */
329 int nfp_net_mbox_reconfig(struct nfp_net *nn, u32 mbox_cmd)
330 {
331         u32 mbox = nn->tlv_caps.mbox_off;
332         int ret;
333
334         lockdep_assert_held(&nn->bar_lock);
335         nn_writeq(nn, mbox + NFP_NET_CFG_MBOX_SIMPLE_CMD, mbox_cmd);
336
337         ret = __nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_MBOX);
338         if (ret) {
339                 nn_err(nn, "Mailbox update error\n");
340                 return ret;
341         }
342
343         return -nn_readl(nn, mbox + NFP_NET_CFG_MBOX_SIMPLE_RET);
344 }
345
346 int nfp_net_mbox_reconfig_and_unlock(struct nfp_net *nn, u32 mbox_cmd)
347 {
348         int ret;
349
350         ret = nfp_net_mbox_reconfig(nn, mbox_cmd);
351         nn_ctrl_bar_unlock(nn);
352         return ret;
353 }
354
355 /* Interrupt configuration and handling
356  */
357
358 /**
359  * nfp_net_irq_unmask() - Unmask automasked interrupt
360  * @nn:       NFP Network structure
361  * @entry_nr: MSI-X table entry
362  *
363  * Clear the ICR for the IRQ entry.
364  */
365 static void nfp_net_irq_unmask(struct nfp_net *nn, unsigned int entry_nr)
366 {
367         nn_writeb(nn, NFP_NET_CFG_ICR(entry_nr), NFP_NET_CFG_ICR_UNMASKED);
368         nn_pci_flush(nn);
369 }
370
371 /**
372  * nfp_net_irqs_alloc() - allocates MSI-X irqs
373  * @pdev:        PCI device structure
374  * @irq_entries: Array to be initialized and used to hold the irq entries
375  * @min_irqs:    Minimal acceptable number of interrupts
376  * @wanted_irqs: Target number of interrupts to allocate
377  *
378  * Return: Number of irqs obtained or 0 on error.
379  */
380 unsigned int
381 nfp_net_irqs_alloc(struct pci_dev *pdev, struct msix_entry *irq_entries,
382                    unsigned int min_irqs, unsigned int wanted_irqs)
383 {
384         unsigned int i;
385         int got_irqs;
386
387         for (i = 0; i < wanted_irqs; i++)
388                 irq_entries[i].entry = i;
389
390         got_irqs = pci_enable_msix_range(pdev, irq_entries,
391                                          min_irqs, wanted_irqs);
392         if (got_irqs < 0) {
393                 dev_err(&pdev->dev, "Failed to enable %d-%d MSI-X (err=%d)\n",
394                         min_irqs, wanted_irqs, got_irqs);
395                 return 0;
396         }
397
398         if (got_irqs < wanted_irqs)
399                 dev_warn(&pdev->dev, "Unable to allocate %d IRQs got only %d\n",
400                          wanted_irqs, got_irqs);
401
402         return got_irqs;
403 }
404
405 /**
406  * nfp_net_irqs_assign() - Assign interrupts allocated externally to netdev
407  * @nn:          NFP Network structure
408  * @irq_entries: Table of allocated interrupts
409  * @n:           Size of @irq_entries (number of entries to grab)
410  *
411  * After interrupts are allocated with nfp_net_irqs_alloc() this function
412  * should be called to assign them to a specific netdev (port).
413  */
414 void
415 nfp_net_irqs_assign(struct nfp_net *nn, struct msix_entry *irq_entries,
416                     unsigned int n)
417 {
418         struct nfp_net_dp *dp = &nn->dp;
419
420         nn->max_r_vecs = n - NFP_NET_NON_Q_VECTORS;
421         dp->num_r_vecs = nn->max_r_vecs;
422
423         memcpy(nn->irq_entries, irq_entries, sizeof(*irq_entries) * n);
424
425         if (dp->num_rx_rings > dp->num_r_vecs ||
426             dp->num_tx_rings > dp->num_r_vecs)
427                 dev_warn(nn->dp.dev, "More rings (%d,%d) than vectors (%d).\n",
428                          dp->num_rx_rings, dp->num_tx_rings,
429                          dp->num_r_vecs);
430
431         dp->num_rx_rings = min(dp->num_r_vecs, dp->num_rx_rings);
432         dp->num_tx_rings = min(dp->num_r_vecs, dp->num_tx_rings);
433         dp->num_stack_tx_rings = dp->num_tx_rings;
434 }
435
436 /**
437  * nfp_net_irqs_disable() - Disable interrupts
438  * @pdev:        PCI device structure
439  *
440  * Undoes what @nfp_net_irqs_alloc() does.
441  */
442 void nfp_net_irqs_disable(struct pci_dev *pdev)
443 {
444         pci_disable_msix(pdev);
445 }
446
447 /**
448  * nfp_net_irq_rxtx() - Interrupt service routine for RX/TX rings.
449  * @irq:      Interrupt
450  * @data:     Opaque data structure
451  *
452  * Return: Indicate if the interrupt has been handled.
453  */
454 static irqreturn_t nfp_net_irq_rxtx(int irq, void *data)
455 {
456         struct nfp_net_r_vector *r_vec = data;
457
458         napi_schedule_irqoff(&r_vec->napi);
459
460         /* The FW auto-masks any interrupt, either via the MASK bit in
461          * the MSI-X table or via the per entry ICR field.  So there
462          * is no need to disable interrupts here.
463          */
464         return IRQ_HANDLED;
465 }
466
467 static irqreturn_t nfp_ctrl_irq_rxtx(int irq, void *data)
468 {
469         struct nfp_net_r_vector *r_vec = data;
470
471         tasklet_schedule(&r_vec->tasklet);
472
473         return IRQ_HANDLED;
474 }
475
476 /**
477  * nfp_net_read_link_status() - Reread link status from control BAR
478  * @nn:       NFP Network structure
479  */
480 static void nfp_net_read_link_status(struct nfp_net *nn)
481 {
482         unsigned long flags;
483         bool link_up;
484         u32 sts;
485
486         spin_lock_irqsave(&nn->link_status_lock, flags);
487
488         sts = nn_readl(nn, NFP_NET_CFG_STS);
489         link_up = !!(sts & NFP_NET_CFG_STS_LINK);
490
491         if (nn->link_up == link_up)
492                 goto out;
493
494         nn->link_up = link_up;
495         if (nn->port)
496                 set_bit(NFP_PORT_CHANGED, &nn->port->flags);
497
498         if (nn->link_up) {
499                 netif_carrier_on(nn->dp.netdev);
500                 netdev_info(nn->dp.netdev, "NIC Link is Up\n");
501         } else {
502                 netif_carrier_off(nn->dp.netdev);
503                 netdev_info(nn->dp.netdev, "NIC Link is Down\n");
504         }
505 out:
506         spin_unlock_irqrestore(&nn->link_status_lock, flags);
507 }
508
509 /**
510  * nfp_net_irq_lsc() - Interrupt service routine for link state changes
511  * @irq:      Interrupt
512  * @data:     Opaque data structure
513  *
514  * Return: Indicate if the interrupt has been handled.
515  */
516 static irqreturn_t nfp_net_irq_lsc(int irq, void *data)
517 {
518         struct nfp_net *nn = data;
519         struct msix_entry *entry;
520
521         entry = &nn->irq_entries[NFP_NET_IRQ_LSC_IDX];
522
523         nfp_net_read_link_status(nn);
524
525         nfp_net_irq_unmask(nn, entry->entry);
526
527         return IRQ_HANDLED;
528 }
529
530 /**
531  * nfp_net_irq_exn() - Interrupt service routine for exceptions
532  * @irq:      Interrupt
533  * @data:     Opaque data structure
534  *
535  * Return: Indicate if the interrupt has been handled.
536  */
537 static irqreturn_t nfp_net_irq_exn(int irq, void *data)
538 {
539         struct nfp_net *nn = data;
540
541         nn_err(nn, "%s: UNIMPLEMENTED.\n", __func__);
542         /* XXX TO BE IMPLEMENTED */
543         return IRQ_HANDLED;
544 }
545
546 /**
547  * nfp_net_tx_ring_init() - Fill in the boilerplate for a TX ring
548  * @tx_ring:  TX ring structure
549  * @r_vec:    IRQ vector servicing this ring
550  * @idx:      Ring index
551  * @is_xdp:   Is this an XDP TX ring?
552  */
553 static void
554 nfp_net_tx_ring_init(struct nfp_net_tx_ring *tx_ring,
555                      struct nfp_net_r_vector *r_vec, unsigned int idx,
556                      bool is_xdp)
557 {
558         struct nfp_net *nn = r_vec->nfp_net;
559
560         tx_ring->idx = idx;
561         tx_ring->r_vec = r_vec;
562         tx_ring->is_xdp = is_xdp;
563         u64_stats_init(&tx_ring->r_vec->tx_sync);
564
565         tx_ring->qcidx = tx_ring->idx * nn->stride_tx;
566         tx_ring->qcp_q = nn->tx_bar + NFP_QCP_QUEUE_OFF(tx_ring->qcidx);
567 }
568
569 /**
570  * nfp_net_rx_ring_init() - Fill in the boilerplate for a RX ring
571  * @rx_ring:  RX ring structure
572  * @r_vec:    IRQ vector servicing this ring
573  * @idx:      Ring index
574  */
575 static void
576 nfp_net_rx_ring_init(struct nfp_net_rx_ring *rx_ring,
577                      struct nfp_net_r_vector *r_vec, unsigned int idx)
578 {
579         struct nfp_net *nn = r_vec->nfp_net;
580
581         rx_ring->idx = idx;
582         rx_ring->r_vec = r_vec;
583         u64_stats_init(&rx_ring->r_vec->rx_sync);
584
585         rx_ring->fl_qcidx = rx_ring->idx * nn->stride_rx;
586         rx_ring->qcp_fl = nn->rx_bar + NFP_QCP_QUEUE_OFF(rx_ring->fl_qcidx);
587 }
588
589 /**
590  * nfp_net_aux_irq_request() - Request an auxiliary interrupt (LSC or EXN)
591  * @nn:         NFP Network structure
592  * @ctrl_offset: Control BAR offset where IRQ configuration should be written
593  * @format:     printf-style format to construct the interrupt name
594  * @name:       Pointer to allocated space for interrupt name
595  * @name_sz:    Size of space for interrupt name
596  * @vector_idx: Index of MSI-X vector used for this interrupt
597  * @handler:    IRQ handler to register for this interrupt
598  */
599 static int
600 nfp_net_aux_irq_request(struct nfp_net *nn, u32 ctrl_offset,
601                         const char *format, char *name, size_t name_sz,
602                         unsigned int vector_idx, irq_handler_t handler)
603 {
604         struct msix_entry *entry;
605         int err;
606
607         entry = &nn->irq_entries[vector_idx];
608
609         snprintf(name, name_sz, format, nfp_net_name(nn));
610         err = request_irq(entry->vector, handler, 0, name, nn);
611         if (err) {
612                 nn_err(nn, "Failed to request IRQ %d (err=%d).\n",
613                        entry->vector, err);
614                 return err;
615         }
616         nn_writeb(nn, ctrl_offset, entry->entry);
617         nfp_net_irq_unmask(nn, entry->entry);
618
619         return 0;
620 }
621
622 /**
623  * nfp_net_aux_irq_free() - Free an auxiliary interrupt (LSC or EXN)
624  * @nn:         NFP Network structure
625  * @ctrl_offset: Control BAR offset where IRQ configuration should be written
626  * @vector_idx: Index of MSI-X vector used for this interrupt
627  */
628 static void nfp_net_aux_irq_free(struct nfp_net *nn, u32 ctrl_offset,
629                                  unsigned int vector_idx)
630 {
631         nn_writeb(nn, ctrl_offset, 0xff);
632         nn_pci_flush(nn);
633         free_irq(nn->irq_entries[vector_idx].vector, nn);
634 }
635
636 /* Transmit
637  *
638  * One queue controller peripheral queue is used for transmit.  The
639  * driver en-queues packets for transmit by advancing the write
640  * pointer.  The device indicates that packets have transmitted by
641  * advancing the read pointer.  The driver maintains a local copy of
642  * the read and write pointer in @struct nfp_net_tx_ring.  The driver
643  * keeps @wr_p in sync with the queue controller write pointer and can
644  * determine how many packets have been transmitted by comparing its
645  * copy of the read pointer @rd_p with the read pointer maintained by
646  * the queue controller peripheral.
647  */
648
649 /**
650  * nfp_net_tx_full() - Check if the TX ring is full
651  * @tx_ring: TX ring to check
652  * @dcnt:    Number of descriptors that need to be enqueued (must be >= 1)
653  *
654  * This function checks, based on the *host copy* of read/write
655  * pointer if a given TX ring is full.  The real TX queue may have
656  * some newly made available slots.
657  *
658  * Return: True if the ring is full.
659  */
660 static int nfp_net_tx_full(struct nfp_net_tx_ring *tx_ring, int dcnt)
661 {
662         return (tx_ring->wr_p - tx_ring->rd_p) >= (tx_ring->cnt - dcnt);
663 }
664
665 /* Wrappers for deciding when to stop and restart TX queues */
666 static int nfp_net_tx_ring_should_wake(struct nfp_net_tx_ring *tx_ring)
667 {
668         return !nfp_net_tx_full(tx_ring, MAX_SKB_FRAGS * 4);
669 }
670
671 static int nfp_net_tx_ring_should_stop(struct nfp_net_tx_ring *tx_ring)
672 {
673         return nfp_net_tx_full(tx_ring, MAX_SKB_FRAGS + 1);
674 }
675
676 /**
677  * nfp_net_tx_ring_stop() - stop tx ring
678  * @nd_q:    netdev queue
679  * @tx_ring: driver tx queue structure
680  *
681  * Safely stop TX ring.  Remember that while we are running .start_xmit()
682  * someone else may be cleaning the TX ring completions so we need to be
683  * extra careful here.
684  */
685 static void nfp_net_tx_ring_stop(struct netdev_queue *nd_q,
686                                  struct nfp_net_tx_ring *tx_ring)
687 {
688         netif_tx_stop_queue(nd_q);
689
690         /* We can race with the TX completion out of NAPI so recheck */
691         smp_mb();
692         if (unlikely(nfp_net_tx_ring_should_wake(tx_ring)))
693                 netif_tx_start_queue(nd_q);
694 }
695
696 /**
697  * nfp_net_tx_tso() - Set up Tx descriptor for LSO
698  * @r_vec: per-ring structure
699  * @txbuf: Pointer to driver soft TX descriptor
700  * @txd: Pointer to HW TX descriptor
701  * @skb: Pointer to SKB
702  * @md_bytes: Prepend length
703  *
704  * Set up Tx descriptor for LSO, do nothing for non-LSO skbs.
705  * Return error on packet header greater than maximum supported LSO header size.
706  */
707 static void nfp_net_tx_tso(struct nfp_net_r_vector *r_vec,
708                            struct nfp_net_tx_buf *txbuf,
709                            struct nfp_net_tx_desc *txd, struct sk_buff *skb,
710                            u32 md_bytes)
711 {
712         u32 l3_offset, l4_offset, hdrlen;
713         u16 mss;
714
715         if (!skb_is_gso(skb))
716                 return;
717
718         if (!skb->encapsulation) {
719                 l3_offset = skb_network_offset(skb);
720                 l4_offset = skb_transport_offset(skb);
721                 hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
722         } else {
723                 l3_offset = skb_inner_network_offset(skb);
724                 l4_offset = skb_inner_transport_offset(skb);
725                 hdrlen = skb_inner_transport_header(skb) - skb->data +
726                         inner_tcp_hdrlen(skb);
727         }
728
729         txbuf->pkt_cnt = skb_shinfo(skb)->gso_segs;
730         txbuf->real_len += hdrlen * (txbuf->pkt_cnt - 1);
731
732         mss = skb_shinfo(skb)->gso_size & PCIE_DESC_TX_MSS_MASK;
733         txd->l3_offset = l3_offset - md_bytes;
734         txd->l4_offset = l4_offset - md_bytes;
735         txd->lso_hdrlen = hdrlen - md_bytes;
736         txd->mss = cpu_to_le16(mss);
737         txd->flags |= PCIE_DESC_TX_LSO;
738
739         u64_stats_update_begin(&r_vec->tx_sync);
740         r_vec->tx_lso++;
741         u64_stats_update_end(&r_vec->tx_sync);
742 }
743
744 /**
745  * nfp_net_tx_csum() - Set TX CSUM offload flags in TX descriptor
746  * @dp:  NFP Net data path struct
747  * @r_vec: per-ring structure
748  * @txbuf: Pointer to driver soft TX descriptor
749  * @txd: Pointer to TX descriptor
750  * @skb: Pointer to SKB
751  *
752  * This function sets the TX checksum flags in the TX descriptor based
753  * on the configuration and the protocol of the packet to be transmitted.
754  */
755 static void nfp_net_tx_csum(struct nfp_net_dp *dp,
756                             struct nfp_net_r_vector *r_vec,
757                             struct nfp_net_tx_buf *txbuf,
758                             struct nfp_net_tx_desc *txd, struct sk_buff *skb)
759 {
760         struct ipv6hdr *ipv6h;
761         struct iphdr *iph;
762         u8 l4_hdr;
763
764         if (!(dp->ctrl & NFP_NET_CFG_CTRL_TXCSUM))
765                 return;
766
767         if (skb->ip_summed != CHECKSUM_PARTIAL)
768                 return;
769
770         txd->flags |= PCIE_DESC_TX_CSUM;
771         if (skb->encapsulation)
772                 txd->flags |= PCIE_DESC_TX_ENCAP;
773
774         iph = skb->encapsulation ? inner_ip_hdr(skb) : ip_hdr(skb);
775         ipv6h = skb->encapsulation ? inner_ipv6_hdr(skb) : ipv6_hdr(skb);
776
777         if (iph->version == 4) {
778                 txd->flags |= PCIE_DESC_TX_IP4_CSUM;
779                 l4_hdr = iph->protocol;
780         } else if (ipv6h->version == 6) {
781                 l4_hdr = ipv6h->nexthdr;
782         } else {
783                 nn_dp_warn(dp, "partial checksum but ipv=%x!\n", iph->version);
784                 return;
785         }
786
787         switch (l4_hdr) {
788         case IPPROTO_TCP:
789                 txd->flags |= PCIE_DESC_TX_TCP_CSUM;
790                 break;
791         case IPPROTO_UDP:
792                 txd->flags |= PCIE_DESC_TX_UDP_CSUM;
793                 break;
794         default:
795                 nn_dp_warn(dp, "partial checksum but l4 proto=%x!\n", l4_hdr);
796                 return;
797         }
798
799         u64_stats_update_begin(&r_vec->tx_sync);
800         if (skb->encapsulation)
801                 r_vec->hw_csum_tx_inner += txbuf->pkt_cnt;
802         else
803                 r_vec->hw_csum_tx += txbuf->pkt_cnt;
804         u64_stats_update_end(&r_vec->tx_sync);
805 }
806
807 static void nfp_net_tx_xmit_more_flush(struct nfp_net_tx_ring *tx_ring)
808 {
809         wmb();
810         nfp_qcp_wr_ptr_add(tx_ring->qcp_q, tx_ring->wr_ptr_add);
811         tx_ring->wr_ptr_add = 0;
812 }
813
814 static int nfp_net_prep_port_id(struct sk_buff *skb)
815 {
816         struct metadata_dst *md_dst = skb_metadata_dst(skb);
817         unsigned char *data;
818
819         if (likely(!md_dst))
820                 return 0;
821         if (unlikely(md_dst->type != METADATA_HW_PORT_MUX))
822                 return 0;
823
824         if (unlikely(skb_cow_head(skb, 8)))
825                 return -ENOMEM;
826
827         data = skb_push(skb, 8);
828         put_unaligned_be32(NFP_NET_META_PORTID, data);
829         put_unaligned_be32(md_dst->u.port_info.port_id, data + 4);
830
831         return 8;
832 }
833
834 /**
835  * nfp_net_tx() - Main transmit entry point
836  * @skb:    SKB to transmit
837  * @netdev: netdev structure
838  *
839  * Return: NETDEV_TX_OK on success.
840  */
841 static int nfp_net_tx(struct sk_buff *skb, struct net_device *netdev)
842 {
843         struct nfp_net *nn = netdev_priv(netdev);
844         const struct skb_frag_struct *frag;
845         int f, nr_frags, wr_idx, md_bytes;
846         struct nfp_net_tx_ring *tx_ring;
847         struct nfp_net_r_vector *r_vec;
848         struct nfp_net_tx_buf *txbuf;
849         struct nfp_net_tx_desc *txd;
850         struct netdev_queue *nd_q;
851         struct nfp_net_dp *dp;
852         dma_addr_t dma_addr;
853         unsigned int fsize;
854         u16 qidx;
855
856         dp = &nn->dp;
857         qidx = skb_get_queue_mapping(skb);
858         tx_ring = &dp->tx_rings[qidx];
859         r_vec = tx_ring->r_vec;
860
861         nr_frags = skb_shinfo(skb)->nr_frags;
862
863         if (unlikely(nfp_net_tx_full(tx_ring, nr_frags + 1))) {
864                 nn_dp_warn(dp, "TX ring %d busy. wrp=%u rdp=%u\n",
865                            qidx, tx_ring->wr_p, tx_ring->rd_p);
866                 nd_q = netdev_get_tx_queue(dp->netdev, qidx);
867                 netif_tx_stop_queue(nd_q);
868                 nfp_net_tx_xmit_more_flush(tx_ring);
869                 u64_stats_update_begin(&r_vec->tx_sync);
870                 r_vec->tx_busy++;
871                 u64_stats_update_end(&r_vec->tx_sync);
872                 return NETDEV_TX_BUSY;
873         }
874
875         md_bytes = nfp_net_prep_port_id(skb);
876         if (unlikely(md_bytes < 0)) {
877                 nfp_net_tx_xmit_more_flush(tx_ring);
878                 dev_kfree_skb_any(skb);
879                 return NETDEV_TX_OK;
880         }
881
882         /* Start with the head skbuf */
883         dma_addr = dma_map_single(dp->dev, skb->data, skb_headlen(skb),
884                                   DMA_TO_DEVICE);
885         if (dma_mapping_error(dp->dev, dma_addr))
886                 goto err_free;
887
888         wr_idx = D_IDX(tx_ring, tx_ring->wr_p);
889
890         /* Stash the soft descriptor of the head then initialize it */
891         txbuf = &tx_ring->txbufs[wr_idx];
892         txbuf->skb = skb;
893         txbuf->dma_addr = dma_addr;
894         txbuf->fidx = -1;
895         txbuf->pkt_cnt = 1;
896         txbuf->real_len = skb->len;
897
898         /* Build TX descriptor */
899         txd = &tx_ring->txds[wr_idx];
900         txd->offset_eop = (nr_frags ? 0 : PCIE_DESC_TX_EOP) | md_bytes;
901         txd->dma_len = cpu_to_le16(skb_headlen(skb));
902         nfp_desc_set_dma_addr(txd, dma_addr);
903         txd->data_len = cpu_to_le16(skb->len);
904
905         txd->flags = 0;
906         txd->mss = 0;
907         txd->lso_hdrlen = 0;
908
909         /* Do not reorder - tso may adjust pkt cnt, vlan may override fields */
910         nfp_net_tx_tso(r_vec, txbuf, txd, skb, md_bytes);
911         nfp_net_tx_csum(dp, r_vec, txbuf, txd, skb);
912         if (skb_vlan_tag_present(skb) && dp->ctrl & NFP_NET_CFG_CTRL_TXVLAN) {
913                 txd->flags |= PCIE_DESC_TX_VLAN;
914                 txd->vlan = cpu_to_le16(skb_vlan_tag_get(skb));
915         }
916
917         /* Gather DMA */
918         if (nr_frags > 0) {
919                 __le64 second_half;
920
921                 /* all descs must match except for in addr, length and eop */
922                 second_half = txd->vals8[1];
923
924                 for (f = 0; f < nr_frags; f++) {
925                         frag = &skb_shinfo(skb)->frags[f];
926                         fsize = skb_frag_size(frag);
927
928                         dma_addr = skb_frag_dma_map(dp->dev, frag, 0,
929                                                     fsize, DMA_TO_DEVICE);
930                         if (dma_mapping_error(dp->dev, dma_addr))
931                                 goto err_unmap;
932
933                         wr_idx = D_IDX(tx_ring, wr_idx + 1);
934                         tx_ring->txbufs[wr_idx].skb = skb;
935                         tx_ring->txbufs[wr_idx].dma_addr = dma_addr;
936                         tx_ring->txbufs[wr_idx].fidx = f;
937
938                         txd = &tx_ring->txds[wr_idx];
939                         txd->dma_len = cpu_to_le16(fsize);
940                         nfp_desc_set_dma_addr(txd, dma_addr);
941                         txd->offset_eop = md_bytes |
942                                 ((f == nr_frags - 1) ? PCIE_DESC_TX_EOP : 0);
943                         txd->vals8[1] = second_half;
944                 }
945
946                 u64_stats_update_begin(&r_vec->tx_sync);
947                 r_vec->tx_gather++;
948                 u64_stats_update_end(&r_vec->tx_sync);
949         }
950
951         skb_tx_timestamp(skb);
952
953         nd_q = netdev_get_tx_queue(dp->netdev, tx_ring->idx);
954
955         tx_ring->wr_p += nr_frags + 1;
956         if (nfp_net_tx_ring_should_stop(tx_ring))
957                 nfp_net_tx_ring_stop(nd_q, tx_ring);
958
959         tx_ring->wr_ptr_add += nr_frags + 1;
960         if (__netdev_tx_sent_queue(nd_q, txbuf->real_len, netdev_xmit_more()))
961                 nfp_net_tx_xmit_more_flush(tx_ring);
962
963         return NETDEV_TX_OK;
964
965 err_unmap:
966         while (--f >= 0) {
967                 frag = &skb_shinfo(skb)->frags[f];
968                 dma_unmap_page(dp->dev, tx_ring->txbufs[wr_idx].dma_addr,
969                                skb_frag_size(frag), DMA_TO_DEVICE);
970                 tx_ring->txbufs[wr_idx].skb = NULL;
971                 tx_ring->txbufs[wr_idx].dma_addr = 0;
972                 tx_ring->txbufs[wr_idx].fidx = -2;
973                 wr_idx = wr_idx - 1;
974                 if (wr_idx < 0)
975                         wr_idx += tx_ring->cnt;
976         }
977         dma_unmap_single(dp->dev, tx_ring->txbufs[wr_idx].dma_addr,
978                          skb_headlen(skb), DMA_TO_DEVICE);
979         tx_ring->txbufs[wr_idx].skb = NULL;
980         tx_ring->txbufs[wr_idx].dma_addr = 0;
981         tx_ring->txbufs[wr_idx].fidx = -2;
982 err_free:
983         nn_dp_warn(dp, "Failed to map DMA TX buffer\n");
984         nfp_net_tx_xmit_more_flush(tx_ring);
985         u64_stats_update_begin(&r_vec->tx_sync);
986         r_vec->tx_errors++;
987         u64_stats_update_end(&r_vec->tx_sync);
988         dev_kfree_skb_any(skb);
989         return NETDEV_TX_OK;
990 }
991
992 /**
993  * nfp_net_tx_complete() - Handled completed TX packets
994  * @tx_ring:    TX ring structure
995  * @budget:     NAPI budget (only used as bool to determine if in NAPI context)
996  */
997 static void nfp_net_tx_complete(struct nfp_net_tx_ring *tx_ring, int budget)
998 {
999         struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
1000         struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
1001         struct netdev_queue *nd_q;
1002         u32 done_pkts = 0, done_bytes = 0;
1003         u32 qcp_rd_p;
1004         int todo;
1005
1006         if (tx_ring->wr_p == tx_ring->rd_p)
1007                 return;
1008
1009         /* Work out how many descriptors have been transmitted */
1010         qcp_rd_p = nfp_qcp_rd_ptr_read(tx_ring->qcp_q);
1011
1012         if (qcp_rd_p == tx_ring->qcp_rd_p)
1013                 return;
1014
1015         todo = D_IDX(tx_ring, qcp_rd_p - tx_ring->qcp_rd_p);
1016
1017         while (todo--) {
1018                 const struct skb_frag_struct *frag;
1019                 struct nfp_net_tx_buf *tx_buf;
1020                 struct sk_buff *skb;
1021                 int fidx, nr_frags;
1022                 int idx;
1023
1024                 idx = D_IDX(tx_ring, tx_ring->rd_p++);
1025                 tx_buf = &tx_ring->txbufs[idx];
1026
1027                 skb = tx_buf->skb;
1028                 if (!skb)
1029                         continue;
1030
1031                 nr_frags = skb_shinfo(skb)->nr_frags;
1032                 fidx = tx_buf->fidx;
1033
1034                 if (fidx == -1) {
1035                         /* unmap head */
1036                         dma_unmap_single(dp->dev, tx_buf->dma_addr,
1037                                          skb_headlen(skb), DMA_TO_DEVICE);
1038
1039                         done_pkts += tx_buf->pkt_cnt;
1040                         done_bytes += tx_buf->real_len;
1041                 } else {
1042                         /* unmap fragment */
1043                         frag = &skb_shinfo(skb)->frags[fidx];
1044                         dma_unmap_page(dp->dev, tx_buf->dma_addr,
1045                                        skb_frag_size(frag), DMA_TO_DEVICE);
1046                 }
1047
1048                 /* check for last gather fragment */
1049                 if (fidx == nr_frags - 1)
1050                         napi_consume_skb(skb, budget);
1051
1052                 tx_buf->dma_addr = 0;
1053                 tx_buf->skb = NULL;
1054                 tx_buf->fidx = -2;
1055         }
1056
1057         tx_ring->qcp_rd_p = qcp_rd_p;
1058
1059         u64_stats_update_begin(&r_vec->tx_sync);
1060         r_vec->tx_bytes += done_bytes;
1061         r_vec->tx_pkts += done_pkts;
1062         u64_stats_update_end(&r_vec->tx_sync);
1063
1064         if (!dp->netdev)
1065                 return;
1066
1067         nd_q = netdev_get_tx_queue(dp->netdev, tx_ring->idx);
1068         netdev_tx_completed_queue(nd_q, done_pkts, done_bytes);
1069         if (nfp_net_tx_ring_should_wake(tx_ring)) {
1070                 /* Make sure TX thread will see updated tx_ring->rd_p */
1071                 smp_mb();
1072
1073                 if (unlikely(netif_tx_queue_stopped(nd_q)))
1074                         netif_tx_wake_queue(nd_q);
1075         }
1076
1077         WARN_ONCE(tx_ring->wr_p - tx_ring->rd_p > tx_ring->cnt,
1078                   "TX ring corruption rd_p=%u wr_p=%u cnt=%u\n",
1079                   tx_ring->rd_p, tx_ring->wr_p, tx_ring->cnt);
1080 }
1081
1082 static bool nfp_net_xdp_complete(struct nfp_net_tx_ring *tx_ring)
1083 {
1084         struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
1085         u32 done_pkts = 0, done_bytes = 0;
1086         bool done_all;
1087         int idx, todo;
1088         u32 qcp_rd_p;
1089
1090         /* Work out how many descriptors have been transmitted */
1091         qcp_rd_p = nfp_qcp_rd_ptr_read(tx_ring->qcp_q);
1092
1093         if (qcp_rd_p == tx_ring->qcp_rd_p)
1094                 return true;
1095
1096         todo = D_IDX(tx_ring, qcp_rd_p - tx_ring->qcp_rd_p);
1097
1098         done_all = todo <= NFP_NET_XDP_MAX_COMPLETE;
1099         todo = min(todo, NFP_NET_XDP_MAX_COMPLETE);
1100
1101         tx_ring->qcp_rd_p = D_IDX(tx_ring, tx_ring->qcp_rd_p + todo);
1102
1103         done_pkts = todo;
1104         while (todo--) {
1105                 idx = D_IDX(tx_ring, tx_ring->rd_p);
1106                 tx_ring->rd_p++;
1107
1108                 done_bytes += tx_ring->txbufs[idx].real_len;
1109         }
1110
1111         u64_stats_update_begin(&r_vec->tx_sync);
1112         r_vec->tx_bytes += done_bytes;
1113         r_vec->tx_pkts += done_pkts;
1114         u64_stats_update_end(&r_vec->tx_sync);
1115
1116         WARN_ONCE(tx_ring->wr_p - tx_ring->rd_p > tx_ring->cnt,
1117                   "XDP TX ring corruption rd_p=%u wr_p=%u cnt=%u\n",
1118                   tx_ring->rd_p, tx_ring->wr_p, tx_ring->cnt);
1119
1120         return done_all;
1121 }
1122
1123 /**
1124  * nfp_net_tx_ring_reset() - Free any untransmitted buffers and reset pointers
1125  * @dp:         NFP Net data path struct
1126  * @tx_ring:    TX ring structure
1127  *
1128  * Assumes that the device is stopped, must be idempotent.
1129  */
1130 static void
1131 nfp_net_tx_ring_reset(struct nfp_net_dp *dp, struct nfp_net_tx_ring *tx_ring)
1132 {
1133         const struct skb_frag_struct *frag;
1134         struct netdev_queue *nd_q;
1135
1136         while (!tx_ring->is_xdp && tx_ring->rd_p != tx_ring->wr_p) {
1137                 struct nfp_net_tx_buf *tx_buf;
1138                 struct sk_buff *skb;
1139                 int idx, nr_frags;
1140
1141                 idx = D_IDX(tx_ring, tx_ring->rd_p);
1142                 tx_buf = &tx_ring->txbufs[idx];
1143
1144                 skb = tx_ring->txbufs[idx].skb;
1145                 nr_frags = skb_shinfo(skb)->nr_frags;
1146
1147                 if (tx_buf->fidx == -1) {
1148                         /* unmap head */
1149                         dma_unmap_single(dp->dev, tx_buf->dma_addr,
1150                                          skb_headlen(skb), DMA_TO_DEVICE);
1151                 } else {
1152                         /* unmap fragment */
1153                         frag = &skb_shinfo(skb)->frags[tx_buf->fidx];
1154                         dma_unmap_page(dp->dev, tx_buf->dma_addr,
1155                                        skb_frag_size(frag), DMA_TO_DEVICE);
1156                 }
1157
1158                 /* check for last gather fragment */
1159                 if (tx_buf->fidx == nr_frags - 1)
1160                         dev_kfree_skb_any(skb);
1161
1162                 tx_buf->dma_addr = 0;
1163                 tx_buf->skb = NULL;
1164                 tx_buf->fidx = -2;
1165
1166                 tx_ring->qcp_rd_p++;
1167                 tx_ring->rd_p++;
1168         }
1169
1170         memset(tx_ring->txds, 0, tx_ring->size);
1171         tx_ring->wr_p = 0;
1172         tx_ring->rd_p = 0;
1173         tx_ring->qcp_rd_p = 0;
1174         tx_ring->wr_ptr_add = 0;
1175
1176         if (tx_ring->is_xdp || !dp->netdev)
1177                 return;
1178
1179         nd_q = netdev_get_tx_queue(dp->netdev, tx_ring->idx);
1180         netdev_tx_reset_queue(nd_q);
1181 }
1182
1183 static void nfp_net_tx_timeout(struct net_device *netdev)
1184 {
1185         struct nfp_net *nn = netdev_priv(netdev);
1186         int i;
1187
1188         for (i = 0; i < nn->dp.netdev->real_num_tx_queues; i++) {
1189                 if (!netif_tx_queue_stopped(netdev_get_tx_queue(netdev, i)))
1190                         continue;
1191                 nn_warn(nn, "TX timeout on ring: %d\n", i);
1192         }
1193         nn_warn(nn, "TX watchdog timeout\n");
1194 }
1195
1196 /* Receive processing
1197  */
1198 static unsigned int
1199 nfp_net_calc_fl_bufsz(struct nfp_net_dp *dp)
1200 {
1201         unsigned int fl_bufsz;
1202
1203         fl_bufsz = NFP_NET_RX_BUF_HEADROOM;
1204         fl_bufsz += dp->rx_dma_off;
1205         if (dp->rx_offset == NFP_NET_CFG_RX_OFFSET_DYNAMIC)
1206                 fl_bufsz += NFP_NET_MAX_PREPEND;
1207         else
1208                 fl_bufsz += dp->rx_offset;
1209         fl_bufsz += ETH_HLEN + VLAN_HLEN * 2 + dp->mtu;
1210
1211         fl_bufsz = SKB_DATA_ALIGN(fl_bufsz);
1212         fl_bufsz += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1213
1214         return fl_bufsz;
1215 }
1216
1217 static void
1218 nfp_net_free_frag(void *frag, bool xdp)
1219 {
1220         if (!xdp)
1221                 skb_free_frag(frag);
1222         else
1223                 __free_page(virt_to_page(frag));
1224 }
1225
1226 /**
1227  * nfp_net_rx_alloc_one() - Allocate and map page frag for RX
1228  * @dp:         NFP Net data path struct
1229  * @dma_addr:   Pointer to storage for DMA address (output param)
1230  *
1231  * This function will allcate a new page frag, map it for DMA.
1232  *
1233  * Return: allocated page frag or NULL on failure.
1234  */
1235 static void *nfp_net_rx_alloc_one(struct nfp_net_dp *dp, dma_addr_t *dma_addr)
1236 {
1237         void *frag;
1238
1239         if (!dp->xdp_prog) {
1240                 frag = netdev_alloc_frag(dp->fl_bufsz);
1241         } else {
1242                 struct page *page;
1243
1244                 page = alloc_page(GFP_KERNEL);
1245                 frag = page ? page_address(page) : NULL;
1246         }
1247         if (!frag) {
1248                 nn_dp_warn(dp, "Failed to alloc receive page frag\n");
1249                 return NULL;
1250         }
1251
1252         *dma_addr = nfp_net_dma_map_rx(dp, frag);
1253         if (dma_mapping_error(dp->dev, *dma_addr)) {
1254                 nfp_net_free_frag(frag, dp->xdp_prog);
1255                 nn_dp_warn(dp, "Failed to map DMA RX buffer\n");
1256                 return NULL;
1257         }
1258
1259         return frag;
1260 }
1261
1262 static void *nfp_net_napi_alloc_one(struct nfp_net_dp *dp, dma_addr_t *dma_addr)
1263 {
1264         void *frag;
1265
1266         if (!dp->xdp_prog) {
1267                 frag = napi_alloc_frag(dp->fl_bufsz);
1268                 if (unlikely(!frag))
1269                         return NULL;
1270         } else {
1271                 struct page *page;
1272
1273                 page = dev_alloc_page();
1274                 if (unlikely(!page))
1275                         return NULL;
1276                 frag = page_address(page);
1277         }
1278
1279         *dma_addr = nfp_net_dma_map_rx(dp, frag);
1280         if (dma_mapping_error(dp->dev, *dma_addr)) {
1281                 nfp_net_free_frag(frag, dp->xdp_prog);
1282                 nn_dp_warn(dp, "Failed to map DMA RX buffer\n");
1283                 return NULL;
1284         }
1285
1286         return frag;
1287 }
1288
1289 /**
1290  * nfp_net_rx_give_one() - Put mapped skb on the software and hardware rings
1291  * @dp:         NFP Net data path struct
1292  * @rx_ring:    RX ring structure
1293  * @frag:       page fragment buffer
1294  * @dma_addr:   DMA address of skb mapping
1295  */
1296 static void nfp_net_rx_give_one(const struct nfp_net_dp *dp,
1297                                 struct nfp_net_rx_ring *rx_ring,
1298                                 void *frag, dma_addr_t dma_addr)
1299 {
1300         unsigned int wr_idx;
1301
1302         wr_idx = D_IDX(rx_ring, rx_ring->wr_p);
1303
1304         nfp_net_dma_sync_dev_rx(dp, dma_addr);
1305
1306         /* Stash SKB and DMA address away */
1307         rx_ring->rxbufs[wr_idx].frag = frag;
1308         rx_ring->rxbufs[wr_idx].dma_addr = dma_addr;
1309
1310         /* Fill freelist descriptor */
1311         rx_ring->rxds[wr_idx].fld.reserved = 0;
1312         rx_ring->rxds[wr_idx].fld.meta_len_dd = 0;
1313         nfp_desc_set_dma_addr(&rx_ring->rxds[wr_idx].fld,
1314                               dma_addr + dp->rx_dma_off);
1315
1316         rx_ring->wr_p++;
1317         if (!(rx_ring->wr_p % NFP_NET_FL_BATCH)) {
1318                 /* Update write pointer of the freelist queue. Make
1319                  * sure all writes are flushed before telling the hardware.
1320                  */
1321                 wmb();
1322                 nfp_qcp_wr_ptr_add(rx_ring->qcp_fl, NFP_NET_FL_BATCH);
1323         }
1324 }
1325
1326 /**
1327  * nfp_net_rx_ring_reset() - Reflect in SW state of freelist after disable
1328  * @rx_ring:    RX ring structure
1329  *
1330  * Assumes that the device is stopped, must be idempotent.
1331  */
1332 static void nfp_net_rx_ring_reset(struct nfp_net_rx_ring *rx_ring)
1333 {
1334         unsigned int wr_idx, last_idx;
1335
1336         /* wr_p == rd_p means ring was never fed FL bufs.  RX rings are always
1337          * kept at cnt - 1 FL bufs.
1338          */
1339         if (rx_ring->wr_p == 0 && rx_ring->rd_p == 0)
1340                 return;
1341
1342         /* Move the empty entry to the end of the list */
1343         wr_idx = D_IDX(rx_ring, rx_ring->wr_p);
1344         last_idx = rx_ring->cnt - 1;
1345         rx_ring->rxbufs[wr_idx].dma_addr = rx_ring->rxbufs[last_idx].dma_addr;
1346         rx_ring->rxbufs[wr_idx].frag = rx_ring->rxbufs[last_idx].frag;
1347         rx_ring->rxbufs[last_idx].dma_addr = 0;
1348         rx_ring->rxbufs[last_idx].frag = NULL;
1349
1350         memset(rx_ring->rxds, 0, rx_ring->size);
1351         rx_ring->wr_p = 0;
1352         rx_ring->rd_p = 0;
1353 }
1354
1355 /**
1356  * nfp_net_rx_ring_bufs_free() - Free any buffers currently on the RX ring
1357  * @dp:         NFP Net data path struct
1358  * @rx_ring:    RX ring to remove buffers from
1359  *
1360  * Assumes that the device is stopped and buffers are in [0, ring->cnt - 1)
1361  * entries.  After device is disabled nfp_net_rx_ring_reset() must be called
1362  * to restore required ring geometry.
1363  */
1364 static void
1365 nfp_net_rx_ring_bufs_free(struct nfp_net_dp *dp,
1366                           struct nfp_net_rx_ring *rx_ring)
1367 {
1368         unsigned int i;
1369
1370         for (i = 0; i < rx_ring->cnt - 1; i++) {
1371                 /* NULL skb can only happen when initial filling of the ring
1372                  * fails to allocate enough buffers and calls here to free
1373                  * already allocated ones.
1374                  */
1375                 if (!rx_ring->rxbufs[i].frag)
1376                         continue;
1377
1378                 nfp_net_dma_unmap_rx(dp, rx_ring->rxbufs[i].dma_addr);
1379                 nfp_net_free_frag(rx_ring->rxbufs[i].frag, dp->xdp_prog);
1380                 rx_ring->rxbufs[i].dma_addr = 0;
1381                 rx_ring->rxbufs[i].frag = NULL;
1382         }
1383 }
1384
1385 /**
1386  * nfp_net_rx_ring_bufs_alloc() - Fill RX ring with buffers (don't give to FW)
1387  * @dp:         NFP Net data path struct
1388  * @rx_ring:    RX ring to remove buffers from
1389  */
1390 static int
1391 nfp_net_rx_ring_bufs_alloc(struct nfp_net_dp *dp,
1392                            struct nfp_net_rx_ring *rx_ring)
1393 {
1394         struct nfp_net_rx_buf *rxbufs;
1395         unsigned int i;
1396
1397         rxbufs = rx_ring->rxbufs;
1398
1399         for (i = 0; i < rx_ring->cnt - 1; i++) {
1400                 rxbufs[i].frag = nfp_net_rx_alloc_one(dp, &rxbufs[i].dma_addr);
1401                 if (!rxbufs[i].frag) {
1402                         nfp_net_rx_ring_bufs_free(dp, rx_ring);
1403                         return -ENOMEM;
1404                 }
1405         }
1406
1407         return 0;
1408 }
1409
1410 /**
1411  * nfp_net_rx_ring_fill_freelist() - Give buffers from the ring to FW
1412  * @dp:      NFP Net data path struct
1413  * @rx_ring: RX ring to fill
1414  */
1415 static void
1416 nfp_net_rx_ring_fill_freelist(struct nfp_net_dp *dp,
1417                               struct nfp_net_rx_ring *rx_ring)
1418 {
1419         unsigned int i;
1420
1421         for (i = 0; i < rx_ring->cnt - 1; i++)
1422                 nfp_net_rx_give_one(dp, rx_ring, rx_ring->rxbufs[i].frag,
1423                                     rx_ring->rxbufs[i].dma_addr);
1424 }
1425
1426 /**
1427  * nfp_net_rx_csum_has_errors() - group check if rxd has any csum errors
1428  * @flags: RX descriptor flags field in CPU byte order
1429  */
1430 static int nfp_net_rx_csum_has_errors(u16 flags)
1431 {
1432         u16 csum_all_checked, csum_all_ok;
1433
1434         csum_all_checked = flags & __PCIE_DESC_RX_CSUM_ALL;
1435         csum_all_ok = flags & __PCIE_DESC_RX_CSUM_ALL_OK;
1436
1437         return csum_all_checked != (csum_all_ok << PCIE_DESC_RX_CSUM_OK_SHIFT);
1438 }
1439
1440 /**
1441  * nfp_net_rx_csum() - set SKB checksum field based on RX descriptor flags
1442  * @dp:  NFP Net data path struct
1443  * @r_vec: per-ring structure
1444  * @rxd: Pointer to RX descriptor
1445  * @meta: Parsed metadata prepend
1446  * @skb: Pointer to SKB
1447  */
1448 static void nfp_net_rx_csum(struct nfp_net_dp *dp,
1449                             struct nfp_net_r_vector *r_vec,
1450                             struct nfp_net_rx_desc *rxd,
1451                             struct nfp_meta_parsed *meta, struct sk_buff *skb)
1452 {
1453         skb_checksum_none_assert(skb);
1454
1455         if (!(dp->netdev->features & NETIF_F_RXCSUM))
1456                 return;
1457
1458         if (meta->csum_type) {
1459                 skb->ip_summed = meta->csum_type;
1460                 skb->csum = meta->csum;
1461                 u64_stats_update_begin(&r_vec->rx_sync);
1462                 r_vec->hw_csum_rx_complete++;
1463                 u64_stats_update_end(&r_vec->rx_sync);
1464                 return;
1465         }
1466
1467         if (nfp_net_rx_csum_has_errors(le16_to_cpu(rxd->rxd.flags))) {
1468                 u64_stats_update_begin(&r_vec->rx_sync);
1469                 r_vec->hw_csum_rx_error++;
1470                 u64_stats_update_end(&r_vec->rx_sync);
1471                 return;
1472         }
1473
1474         /* Assume that the firmware will never report inner CSUM_OK unless outer
1475          * L4 headers were successfully parsed. FW will always report zero UDP
1476          * checksum as CSUM_OK.
1477          */
1478         if (rxd->rxd.flags & PCIE_DESC_RX_TCP_CSUM_OK ||
1479             rxd->rxd.flags & PCIE_DESC_RX_UDP_CSUM_OK) {
1480                 __skb_incr_checksum_unnecessary(skb);
1481                 u64_stats_update_begin(&r_vec->rx_sync);
1482                 r_vec->hw_csum_rx_ok++;
1483                 u64_stats_update_end(&r_vec->rx_sync);
1484         }
1485
1486         if (rxd->rxd.flags & PCIE_DESC_RX_I_TCP_CSUM_OK ||
1487             rxd->rxd.flags & PCIE_DESC_RX_I_UDP_CSUM_OK) {
1488                 __skb_incr_checksum_unnecessary(skb);
1489                 u64_stats_update_begin(&r_vec->rx_sync);
1490                 r_vec->hw_csum_rx_inner_ok++;
1491                 u64_stats_update_end(&r_vec->rx_sync);
1492         }
1493 }
1494
1495 static void
1496 nfp_net_set_hash(struct net_device *netdev, struct nfp_meta_parsed *meta,
1497                  unsigned int type, __be32 *hash)
1498 {
1499         if (!(netdev->features & NETIF_F_RXHASH))
1500                 return;
1501
1502         switch (type) {
1503         case NFP_NET_RSS_IPV4:
1504         case NFP_NET_RSS_IPV6:
1505         case NFP_NET_RSS_IPV6_EX:
1506                 meta->hash_type = PKT_HASH_TYPE_L3;
1507                 break;
1508         default:
1509                 meta->hash_type = PKT_HASH_TYPE_L4;
1510                 break;
1511         }
1512
1513         meta->hash = get_unaligned_be32(hash);
1514 }
1515
1516 static void
1517 nfp_net_set_hash_desc(struct net_device *netdev, struct nfp_meta_parsed *meta,
1518                       void *data, struct nfp_net_rx_desc *rxd)
1519 {
1520         struct nfp_net_rx_hash *rx_hash = data;
1521
1522         if (!(rxd->rxd.flags & PCIE_DESC_RX_RSS))
1523                 return;
1524
1525         nfp_net_set_hash(netdev, meta, get_unaligned_be32(&rx_hash->hash_type),
1526                          &rx_hash->hash);
1527 }
1528
1529 static void *
1530 nfp_net_parse_meta(struct net_device *netdev, struct nfp_meta_parsed *meta,
1531                    void *data, int meta_len)
1532 {
1533         u32 meta_info;
1534
1535         meta_info = get_unaligned_be32(data);
1536         data += 4;
1537
1538         while (meta_info) {
1539                 switch (meta_info & NFP_NET_META_FIELD_MASK) {
1540                 case NFP_NET_META_HASH:
1541                         meta_info >>= NFP_NET_META_FIELD_SIZE;
1542                         nfp_net_set_hash(netdev, meta,
1543                                          meta_info & NFP_NET_META_FIELD_MASK,
1544                                          (__be32 *)data);
1545                         data += 4;
1546                         break;
1547                 case NFP_NET_META_MARK:
1548                         meta->mark = get_unaligned_be32(data);
1549                         data += 4;
1550                         break;
1551                 case NFP_NET_META_PORTID:
1552                         meta->portid = get_unaligned_be32(data);
1553                         data += 4;
1554                         break;
1555                 case NFP_NET_META_CSUM:
1556                         meta->csum_type = CHECKSUM_COMPLETE;
1557                         meta->csum =
1558                                 (__force __wsum)__get_unaligned_cpu32(data);
1559                         data += 4;
1560                         break;
1561                 default:
1562                         return NULL;
1563                 }
1564
1565                 meta_info >>= NFP_NET_META_FIELD_SIZE;
1566         }
1567
1568         return data;
1569 }
1570
1571 static void
1572 nfp_net_rx_drop(const struct nfp_net_dp *dp, struct nfp_net_r_vector *r_vec,
1573                 struct nfp_net_rx_ring *rx_ring, struct nfp_net_rx_buf *rxbuf,
1574                 struct sk_buff *skb)
1575 {
1576         u64_stats_update_begin(&r_vec->rx_sync);
1577         r_vec->rx_drops++;
1578         /* If we have both skb and rxbuf the replacement buffer allocation
1579          * must have failed, count this as an alloc failure.
1580          */
1581         if (skb && rxbuf)
1582                 r_vec->rx_replace_buf_alloc_fail++;
1583         u64_stats_update_end(&r_vec->rx_sync);
1584
1585         /* skb is build based on the frag, free_skb() would free the frag
1586          * so to be able to reuse it we need an extra ref.
1587          */
1588         if (skb && rxbuf && skb->head == rxbuf->frag)
1589                 page_ref_inc(virt_to_head_page(rxbuf->frag));
1590         if (rxbuf)
1591                 nfp_net_rx_give_one(dp, rx_ring, rxbuf->frag, rxbuf->dma_addr);
1592         if (skb)
1593                 dev_kfree_skb_any(skb);
1594 }
1595
1596 static bool
1597 nfp_net_tx_xdp_buf(struct nfp_net_dp *dp, struct nfp_net_rx_ring *rx_ring,
1598                    struct nfp_net_tx_ring *tx_ring,
1599                    struct nfp_net_rx_buf *rxbuf, unsigned int dma_off,
1600                    unsigned int pkt_len, bool *completed)
1601 {
1602         struct nfp_net_tx_buf *txbuf;
1603         struct nfp_net_tx_desc *txd;
1604         int wr_idx;
1605
1606         if (unlikely(nfp_net_tx_full(tx_ring, 1))) {
1607                 if (!*completed) {
1608                         nfp_net_xdp_complete(tx_ring);
1609                         *completed = true;
1610                 }
1611
1612                 if (unlikely(nfp_net_tx_full(tx_ring, 1))) {
1613                         nfp_net_rx_drop(dp, rx_ring->r_vec, rx_ring, rxbuf,
1614                                         NULL);
1615                         return false;
1616                 }
1617         }
1618
1619         wr_idx = D_IDX(tx_ring, tx_ring->wr_p);
1620
1621         /* Stash the soft descriptor of the head then initialize it */
1622         txbuf = &tx_ring->txbufs[wr_idx];
1623
1624         nfp_net_rx_give_one(dp, rx_ring, txbuf->frag, txbuf->dma_addr);
1625
1626         txbuf->frag = rxbuf->frag;
1627         txbuf->dma_addr = rxbuf->dma_addr;
1628         txbuf->fidx = -1;
1629         txbuf->pkt_cnt = 1;
1630         txbuf->real_len = pkt_len;
1631
1632         dma_sync_single_for_device(dp->dev, rxbuf->dma_addr + dma_off,
1633                                    pkt_len, DMA_BIDIRECTIONAL);
1634
1635         /* Build TX descriptor */
1636         txd = &tx_ring->txds[wr_idx];
1637         txd->offset_eop = PCIE_DESC_TX_EOP;
1638         txd->dma_len = cpu_to_le16(pkt_len);
1639         nfp_desc_set_dma_addr(txd, rxbuf->dma_addr + dma_off);
1640         txd->data_len = cpu_to_le16(pkt_len);
1641
1642         txd->flags = 0;
1643         txd->mss = 0;
1644         txd->lso_hdrlen = 0;
1645
1646         tx_ring->wr_p++;
1647         tx_ring->wr_ptr_add++;
1648         return true;
1649 }
1650
1651 /**
1652  * nfp_net_rx() - receive up to @budget packets on @rx_ring
1653  * @rx_ring:   RX ring to receive from
1654  * @budget:    NAPI budget
1655  *
1656  * Note, this function is separated out from the napi poll function to
1657  * more cleanly separate packet receive code from other bookkeeping
1658  * functions performed in the napi poll function.
1659  *
1660  * Return: Number of packets received.
1661  */
1662 static int nfp_net_rx(struct nfp_net_rx_ring *rx_ring, int budget)
1663 {
1664         struct nfp_net_r_vector *r_vec = rx_ring->r_vec;
1665         struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
1666         struct nfp_net_tx_ring *tx_ring;
1667         struct bpf_prog *xdp_prog;
1668         bool xdp_tx_cmpl = false;
1669         unsigned int true_bufsz;
1670         struct sk_buff *skb;
1671         int pkts_polled = 0;
1672         struct xdp_buff xdp;
1673         int idx;
1674
1675         rcu_read_lock();
1676         xdp_prog = READ_ONCE(dp->xdp_prog);
1677         true_bufsz = xdp_prog ? PAGE_SIZE : dp->fl_bufsz;
1678         xdp.rxq = &rx_ring->xdp_rxq;
1679         tx_ring = r_vec->xdp_ring;
1680
1681         while (pkts_polled < budget) {
1682                 unsigned int meta_len, data_len, meta_off, pkt_len, pkt_off;
1683                 struct nfp_net_rx_buf *rxbuf;
1684                 struct nfp_net_rx_desc *rxd;
1685                 struct nfp_meta_parsed meta;
1686                 struct net_device *netdev;
1687                 dma_addr_t new_dma_addr;
1688                 u32 meta_len_xdp = 0;
1689                 void *new_frag;
1690
1691                 idx = D_IDX(rx_ring, rx_ring->rd_p);
1692
1693                 rxd = &rx_ring->rxds[idx];
1694                 if (!(rxd->rxd.meta_len_dd & PCIE_DESC_RX_DD))
1695                         break;
1696
1697                 /* Memory barrier to ensure that we won't do other reads
1698                  * before the DD bit.
1699                  */
1700                 dma_rmb();
1701
1702                 memset(&meta, 0, sizeof(meta));
1703
1704                 rx_ring->rd_p++;
1705                 pkts_polled++;
1706
1707                 rxbuf = &rx_ring->rxbufs[idx];
1708                 /*         < meta_len >
1709                  *  <-- [rx_offset] -->
1710                  *  ---------------------------------------------------------
1711                  * | [XX] |  metadata  |             packet           | XXXX |
1712                  *  ---------------------------------------------------------
1713                  *         <---------------- data_len --------------->
1714                  *
1715                  * The rx_offset is fixed for all packets, the meta_len can vary
1716                  * on a packet by packet basis. If rx_offset is set to zero
1717                  * (_RX_OFFSET_DYNAMIC) metadata starts at the beginning of the
1718                  * buffer and is immediately followed by the packet (no [XX]).
1719                  */
1720                 meta_len = rxd->rxd.meta_len_dd & PCIE_DESC_RX_META_LEN_MASK;
1721                 data_len = le16_to_cpu(rxd->rxd.data_len);
1722                 pkt_len = data_len - meta_len;
1723
1724                 pkt_off = NFP_NET_RX_BUF_HEADROOM + dp->rx_dma_off;
1725                 if (dp->rx_offset == NFP_NET_CFG_RX_OFFSET_DYNAMIC)
1726                         pkt_off += meta_len;
1727                 else
1728                         pkt_off += dp->rx_offset;
1729                 meta_off = pkt_off - meta_len;
1730
1731                 /* Stats update */
1732                 u64_stats_update_begin(&r_vec->rx_sync);
1733                 r_vec->rx_pkts++;
1734                 r_vec->rx_bytes += pkt_len;
1735                 u64_stats_update_end(&r_vec->rx_sync);
1736
1737                 if (unlikely(meta_len > NFP_NET_MAX_PREPEND ||
1738                              (dp->rx_offset && meta_len > dp->rx_offset))) {
1739                         nn_dp_warn(dp, "oversized RX packet metadata %u\n",
1740                                    meta_len);
1741                         nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
1742                         continue;
1743                 }
1744
1745                 nfp_net_dma_sync_cpu_rx(dp, rxbuf->dma_addr + meta_off,
1746                                         data_len);
1747
1748                 if (!dp->chained_metadata_format) {
1749                         nfp_net_set_hash_desc(dp->netdev, &meta,
1750                                               rxbuf->frag + meta_off, rxd);
1751                 } else if (meta_len) {
1752                         void *end;
1753
1754                         end = nfp_net_parse_meta(dp->netdev, &meta,
1755                                                  rxbuf->frag + meta_off,
1756                                                  meta_len);
1757                         if (unlikely(end != rxbuf->frag + pkt_off)) {
1758                                 nn_dp_warn(dp, "invalid RX packet metadata\n");
1759                                 nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf,
1760                                                 NULL);
1761                                 continue;
1762                         }
1763                 }
1764
1765                 if (xdp_prog && !meta.portid) {
1766                         void *orig_data = rxbuf->frag + pkt_off;
1767                         unsigned int dma_off;
1768                         int act;
1769
1770                         xdp.data_hard_start = rxbuf->frag + NFP_NET_RX_BUF_HEADROOM;
1771                         xdp.data = orig_data;
1772                         xdp.data_meta = orig_data;
1773                         xdp.data_end = orig_data + pkt_len;
1774
1775                         act = bpf_prog_run_xdp(xdp_prog, &xdp);
1776
1777                         pkt_len = xdp.data_end - xdp.data;
1778                         pkt_off += xdp.data - orig_data;
1779
1780                         switch (act) {
1781                         case XDP_PASS:
1782                                 meta_len_xdp = xdp.data - xdp.data_meta;
1783                                 break;
1784                         case XDP_TX:
1785                                 dma_off = pkt_off - NFP_NET_RX_BUF_HEADROOM;
1786                                 if (unlikely(!nfp_net_tx_xdp_buf(dp, rx_ring,
1787                                                                  tx_ring, rxbuf,
1788                                                                  dma_off,
1789                                                                  pkt_len,
1790                                                                  &xdp_tx_cmpl)))
1791                                         trace_xdp_exception(dp->netdev,
1792                                                             xdp_prog, act);
1793                                 continue;
1794                         default:
1795                                 bpf_warn_invalid_xdp_action(act);
1796                                 /* fall through */
1797                         case XDP_ABORTED:
1798                                 trace_xdp_exception(dp->netdev, xdp_prog, act);
1799                                 /* fall through */
1800                         case XDP_DROP:
1801                                 nfp_net_rx_give_one(dp, rx_ring, rxbuf->frag,
1802                                                     rxbuf->dma_addr);
1803                                 continue;
1804                         }
1805                 }
1806
1807                 if (likely(!meta.portid)) {
1808                         netdev = dp->netdev;
1809                 } else if (meta.portid == NFP_META_PORT_ID_CTRL) {
1810                         struct nfp_net *nn = netdev_priv(dp->netdev);
1811
1812                         nfp_app_ctrl_rx_raw(nn->app, rxbuf->frag + pkt_off,
1813                                             pkt_len);
1814                         nfp_net_rx_give_one(dp, rx_ring, rxbuf->frag,
1815                                             rxbuf->dma_addr);
1816                         continue;
1817                 } else {
1818                         struct nfp_net *nn;
1819
1820                         nn = netdev_priv(dp->netdev);
1821                         netdev = nfp_app_repr_get(nn->app, meta.portid);
1822                         if (unlikely(!netdev)) {
1823                                 nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf,
1824                                                 NULL);
1825                                 continue;
1826                         }
1827                         nfp_repr_inc_rx_stats(netdev, pkt_len);
1828                 }
1829
1830                 skb = build_skb(rxbuf->frag, true_bufsz);
1831                 if (unlikely(!skb)) {
1832                         nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
1833                         continue;
1834                 }
1835                 new_frag = nfp_net_napi_alloc_one(dp, &new_dma_addr);
1836                 if (unlikely(!new_frag)) {
1837                         nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, skb);
1838                         continue;
1839                 }
1840
1841                 nfp_net_dma_unmap_rx(dp, rxbuf->dma_addr);
1842
1843                 nfp_net_rx_give_one(dp, rx_ring, new_frag, new_dma_addr);
1844
1845                 skb_reserve(skb, pkt_off);
1846                 skb_put(skb, pkt_len);
1847
1848                 skb->mark = meta.mark;
1849                 skb_set_hash(skb, meta.hash, meta.hash_type);
1850
1851                 skb_record_rx_queue(skb, rx_ring->idx);
1852                 skb->protocol = eth_type_trans(skb, netdev);
1853
1854                 nfp_net_rx_csum(dp, r_vec, rxd, &meta, skb);
1855
1856                 if (rxd->rxd.flags & PCIE_DESC_RX_VLAN)
1857                         __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1858                                                le16_to_cpu(rxd->rxd.vlan));
1859                 if (meta_len_xdp)
1860                         skb_metadata_set(skb, meta_len_xdp);
1861
1862                 napi_gro_receive(&rx_ring->r_vec->napi, skb);
1863         }
1864
1865         if (xdp_prog) {
1866                 if (tx_ring->wr_ptr_add)
1867                         nfp_net_tx_xmit_more_flush(tx_ring);
1868                 else if (unlikely(tx_ring->wr_p != tx_ring->rd_p) &&
1869                          !xdp_tx_cmpl)
1870                         if (!nfp_net_xdp_complete(tx_ring))
1871                                 pkts_polled = budget;
1872         }
1873         rcu_read_unlock();
1874
1875         return pkts_polled;
1876 }
1877
1878 /**
1879  * nfp_net_poll() - napi poll function
1880  * @napi:    NAPI structure
1881  * @budget:  NAPI budget
1882  *
1883  * Return: number of packets polled.
1884  */
1885 static int nfp_net_poll(struct napi_struct *napi, int budget)
1886 {
1887         struct nfp_net_r_vector *r_vec =
1888                 container_of(napi, struct nfp_net_r_vector, napi);
1889         unsigned int pkts_polled = 0;
1890
1891         if (r_vec->tx_ring)
1892                 nfp_net_tx_complete(r_vec->tx_ring, budget);
1893         if (r_vec->rx_ring)
1894                 pkts_polled = nfp_net_rx(r_vec->rx_ring, budget);
1895
1896         if (pkts_polled < budget)
1897                 if (napi_complete_done(napi, pkts_polled))
1898                         nfp_net_irq_unmask(r_vec->nfp_net, r_vec->irq_entry);
1899
1900         return pkts_polled;
1901 }
1902
1903 /* Control device data path
1904  */
1905
1906 static bool
1907 nfp_ctrl_tx_one(struct nfp_net *nn, struct nfp_net_r_vector *r_vec,
1908                 struct sk_buff *skb, bool old)
1909 {
1910         unsigned int real_len = skb->len, meta_len = 0;
1911         struct nfp_net_tx_ring *tx_ring;
1912         struct nfp_net_tx_buf *txbuf;
1913         struct nfp_net_tx_desc *txd;
1914         struct nfp_net_dp *dp;
1915         dma_addr_t dma_addr;
1916         int wr_idx;
1917
1918         dp = &r_vec->nfp_net->dp;
1919         tx_ring = r_vec->tx_ring;
1920
1921         if (WARN_ON_ONCE(skb_shinfo(skb)->nr_frags)) {
1922                 nn_dp_warn(dp, "Driver's CTRL TX does not implement gather\n");
1923                 goto err_free;
1924         }
1925
1926         if (unlikely(nfp_net_tx_full(tx_ring, 1))) {
1927                 u64_stats_update_begin(&r_vec->tx_sync);
1928                 r_vec->tx_busy++;
1929                 u64_stats_update_end(&r_vec->tx_sync);
1930                 if (!old)
1931                         __skb_queue_tail(&r_vec->queue, skb);
1932                 else
1933                         __skb_queue_head(&r_vec->queue, skb);
1934                 return true;
1935         }
1936
1937         if (nfp_app_ctrl_has_meta(nn->app)) {
1938                 if (unlikely(skb_headroom(skb) < 8)) {
1939                         nn_dp_warn(dp, "CTRL TX on skb without headroom\n");
1940                         goto err_free;
1941                 }
1942                 meta_len = 8;
1943                 put_unaligned_be32(NFP_META_PORT_ID_CTRL, skb_push(skb, 4));
1944                 put_unaligned_be32(NFP_NET_META_PORTID, skb_push(skb, 4));
1945         }
1946
1947         /* Start with the head skbuf */
1948         dma_addr = dma_map_single(dp->dev, skb->data, skb_headlen(skb),
1949                                   DMA_TO_DEVICE);
1950         if (dma_mapping_error(dp->dev, dma_addr))
1951                 goto err_dma_warn;
1952
1953         wr_idx = D_IDX(tx_ring, tx_ring->wr_p);
1954
1955         /* Stash the soft descriptor of the head then initialize it */
1956         txbuf = &tx_ring->txbufs[wr_idx];
1957         txbuf->skb = skb;
1958         txbuf->dma_addr = dma_addr;
1959         txbuf->fidx = -1;
1960         txbuf->pkt_cnt = 1;
1961         txbuf->real_len = real_len;
1962
1963         /* Build TX descriptor */
1964         txd = &tx_ring->txds[wr_idx];
1965         txd->offset_eop = meta_len | PCIE_DESC_TX_EOP;
1966         txd->dma_len = cpu_to_le16(skb_headlen(skb));
1967         nfp_desc_set_dma_addr(txd, dma_addr);
1968         txd->data_len = cpu_to_le16(skb->len);
1969
1970         txd->flags = 0;
1971         txd->mss = 0;
1972         txd->lso_hdrlen = 0;
1973
1974         tx_ring->wr_p++;
1975         tx_ring->wr_ptr_add++;
1976         nfp_net_tx_xmit_more_flush(tx_ring);
1977
1978         return false;
1979
1980 err_dma_warn:
1981         nn_dp_warn(dp, "Failed to DMA map TX CTRL buffer\n");
1982 err_free:
1983         u64_stats_update_begin(&r_vec->tx_sync);
1984         r_vec->tx_errors++;
1985         u64_stats_update_end(&r_vec->tx_sync);
1986         dev_kfree_skb_any(skb);
1987         return false;
1988 }
1989
1990 bool __nfp_ctrl_tx(struct nfp_net *nn, struct sk_buff *skb)
1991 {
1992         struct nfp_net_r_vector *r_vec = &nn->r_vecs[0];
1993
1994         return nfp_ctrl_tx_one(nn, r_vec, skb, false);
1995 }
1996
1997 bool nfp_ctrl_tx(struct nfp_net *nn, struct sk_buff *skb)
1998 {
1999         struct nfp_net_r_vector *r_vec = &nn->r_vecs[0];
2000         bool ret;
2001
2002         spin_lock_bh(&r_vec->lock);
2003         ret = nfp_ctrl_tx_one(nn, r_vec, skb, false);
2004         spin_unlock_bh(&r_vec->lock);
2005
2006         return ret;
2007 }
2008
2009 static void __nfp_ctrl_tx_queued(struct nfp_net_r_vector *r_vec)
2010 {
2011         struct sk_buff *skb;
2012
2013         while ((skb = __skb_dequeue(&r_vec->queue)))
2014                 if (nfp_ctrl_tx_one(r_vec->nfp_net, r_vec, skb, true))
2015                         return;
2016 }
2017
2018 static bool
2019 nfp_ctrl_meta_ok(struct nfp_net *nn, void *data, unsigned int meta_len)
2020 {
2021         u32 meta_type, meta_tag;
2022
2023         if (!nfp_app_ctrl_has_meta(nn->app))
2024                 return !meta_len;
2025
2026         if (meta_len != 8)
2027                 return false;
2028
2029         meta_type = get_unaligned_be32(data);
2030         meta_tag = get_unaligned_be32(data + 4);
2031
2032         return (meta_type == NFP_NET_META_PORTID &&
2033                 meta_tag == NFP_META_PORT_ID_CTRL);
2034 }
2035
2036 static bool
2037 nfp_ctrl_rx_one(struct nfp_net *nn, struct nfp_net_dp *dp,
2038                 struct nfp_net_r_vector *r_vec, struct nfp_net_rx_ring *rx_ring)
2039 {
2040         unsigned int meta_len, data_len, meta_off, pkt_len, pkt_off;
2041         struct nfp_net_rx_buf *rxbuf;
2042         struct nfp_net_rx_desc *rxd;
2043         dma_addr_t new_dma_addr;
2044         struct sk_buff *skb;
2045         void *new_frag;
2046         int idx;
2047
2048         idx = D_IDX(rx_ring, rx_ring->rd_p);
2049
2050         rxd = &rx_ring->rxds[idx];
2051         if (!(rxd->rxd.meta_len_dd & PCIE_DESC_RX_DD))
2052                 return false;
2053
2054         /* Memory barrier to ensure that we won't do other reads
2055          * before the DD bit.
2056          */
2057         dma_rmb();
2058
2059         rx_ring->rd_p++;
2060
2061         rxbuf = &rx_ring->rxbufs[idx];
2062         meta_len = rxd->rxd.meta_len_dd & PCIE_DESC_RX_META_LEN_MASK;
2063         data_len = le16_to_cpu(rxd->rxd.data_len);
2064         pkt_len = data_len - meta_len;
2065
2066         pkt_off = NFP_NET_RX_BUF_HEADROOM + dp->rx_dma_off;
2067         if (dp->rx_offset == NFP_NET_CFG_RX_OFFSET_DYNAMIC)
2068                 pkt_off += meta_len;
2069         else
2070                 pkt_off += dp->rx_offset;
2071         meta_off = pkt_off - meta_len;
2072
2073         /* Stats update */
2074         u64_stats_update_begin(&r_vec->rx_sync);
2075         r_vec->rx_pkts++;
2076         r_vec->rx_bytes += pkt_len;
2077         u64_stats_update_end(&r_vec->rx_sync);
2078
2079         nfp_net_dma_sync_cpu_rx(dp, rxbuf->dma_addr + meta_off, data_len);
2080
2081         if (unlikely(!nfp_ctrl_meta_ok(nn, rxbuf->frag + meta_off, meta_len))) {
2082                 nn_dp_warn(dp, "incorrect metadata for ctrl packet (%d)\n",
2083                            meta_len);
2084                 nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
2085                 return true;
2086         }
2087
2088         skb = build_skb(rxbuf->frag, dp->fl_bufsz);
2089         if (unlikely(!skb)) {
2090                 nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
2091                 return true;
2092         }
2093         new_frag = nfp_net_napi_alloc_one(dp, &new_dma_addr);
2094         if (unlikely(!new_frag)) {
2095                 nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, skb);
2096                 return true;
2097         }
2098
2099         nfp_net_dma_unmap_rx(dp, rxbuf->dma_addr);
2100
2101         nfp_net_rx_give_one(dp, rx_ring, new_frag, new_dma_addr);
2102
2103         skb_reserve(skb, pkt_off);
2104         skb_put(skb, pkt_len);
2105
2106         nfp_app_ctrl_rx(nn->app, skb);
2107
2108         return true;
2109 }
2110
2111 static bool nfp_ctrl_rx(struct nfp_net_r_vector *r_vec)
2112 {
2113         struct nfp_net_rx_ring *rx_ring = r_vec->rx_ring;
2114         struct nfp_net *nn = r_vec->nfp_net;
2115         struct nfp_net_dp *dp = &nn->dp;
2116         unsigned int budget = 512;
2117
2118         while (nfp_ctrl_rx_one(nn, dp, r_vec, rx_ring) && budget--)
2119                 continue;
2120
2121         return budget;
2122 }
2123
2124 static void nfp_ctrl_poll(unsigned long arg)
2125 {
2126         struct nfp_net_r_vector *r_vec = (void *)arg;
2127
2128         spin_lock(&r_vec->lock);
2129         nfp_net_tx_complete(r_vec->tx_ring, 0);
2130         __nfp_ctrl_tx_queued(r_vec);
2131         spin_unlock(&r_vec->lock);
2132
2133         if (nfp_ctrl_rx(r_vec)) {
2134                 nfp_net_irq_unmask(r_vec->nfp_net, r_vec->irq_entry);
2135         } else {
2136                 tasklet_schedule(&r_vec->tasklet);
2137                 nn_dp_warn(&r_vec->nfp_net->dp,
2138                            "control message budget exceeded!\n");
2139         }
2140 }
2141
2142 /* Setup and Configuration
2143  */
2144
2145 /**
2146  * nfp_net_vecs_init() - Assign IRQs and setup rvecs.
2147  * @nn:         NFP Network structure
2148  */
2149 static void nfp_net_vecs_init(struct nfp_net *nn)
2150 {
2151         struct nfp_net_r_vector *r_vec;
2152         int r;
2153
2154         nn->lsc_handler = nfp_net_irq_lsc;
2155         nn->exn_handler = nfp_net_irq_exn;
2156
2157         for (r = 0; r < nn->max_r_vecs; r++) {
2158                 struct msix_entry *entry;
2159
2160                 entry = &nn->irq_entries[NFP_NET_NON_Q_VECTORS + r];
2161
2162                 r_vec = &nn->r_vecs[r];
2163                 r_vec->nfp_net = nn;
2164                 r_vec->irq_entry = entry->entry;
2165                 r_vec->irq_vector = entry->vector;
2166
2167                 if (nn->dp.netdev) {
2168                         r_vec->handler = nfp_net_irq_rxtx;
2169                 } else {
2170                         r_vec->handler = nfp_ctrl_irq_rxtx;
2171
2172                         __skb_queue_head_init(&r_vec->queue);
2173                         spin_lock_init(&r_vec->lock);
2174                         tasklet_init(&r_vec->tasklet, nfp_ctrl_poll,
2175                                      (unsigned long)r_vec);
2176                         tasklet_disable(&r_vec->tasklet);
2177                 }
2178
2179                 cpumask_set_cpu(r, &r_vec->affinity_mask);
2180         }
2181 }
2182
2183 /**
2184  * nfp_net_tx_ring_free() - Free resources allocated to a TX ring
2185  * @tx_ring:   TX ring to free
2186  */
2187 static void nfp_net_tx_ring_free(struct nfp_net_tx_ring *tx_ring)
2188 {
2189         struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
2190         struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
2191
2192         kvfree(tx_ring->txbufs);
2193
2194         if (tx_ring->txds)
2195                 dma_free_coherent(dp->dev, tx_ring->size,
2196                                   tx_ring->txds, tx_ring->dma);
2197
2198         tx_ring->cnt = 0;
2199         tx_ring->txbufs = NULL;
2200         tx_ring->txds = NULL;
2201         tx_ring->dma = 0;
2202         tx_ring->size = 0;
2203 }
2204
2205 /**
2206  * nfp_net_tx_ring_alloc() - Allocate resource for a TX ring
2207  * @dp:        NFP Net data path struct
2208  * @tx_ring:   TX Ring structure to allocate
2209  *
2210  * Return: 0 on success, negative errno otherwise.
2211  */
2212 static int
2213 nfp_net_tx_ring_alloc(struct nfp_net_dp *dp, struct nfp_net_tx_ring *tx_ring)
2214 {
2215         struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
2216
2217         tx_ring->cnt = dp->txd_cnt;
2218
2219         tx_ring->size = array_size(tx_ring->cnt, sizeof(*tx_ring->txds));
2220         tx_ring->txds = dma_alloc_coherent(dp->dev, tx_ring->size,
2221                                            &tx_ring->dma,
2222                                            GFP_KERNEL | __GFP_NOWARN);
2223         if (!tx_ring->txds) {
2224                 netdev_warn(dp->netdev, "failed to allocate TX descriptor ring memory, requested descriptor count: %d, consider lowering descriptor count\n",
2225                             tx_ring->cnt);
2226                 goto err_alloc;
2227         }
2228
2229         tx_ring->txbufs = kvcalloc(tx_ring->cnt, sizeof(*tx_ring->txbufs),
2230                                    GFP_KERNEL);
2231         if (!tx_ring->txbufs)
2232                 goto err_alloc;
2233
2234         if (!tx_ring->is_xdp && dp->netdev)
2235                 netif_set_xps_queue(dp->netdev, &r_vec->affinity_mask,
2236                                     tx_ring->idx);
2237
2238         return 0;
2239
2240 err_alloc:
2241         nfp_net_tx_ring_free(tx_ring);
2242         return -ENOMEM;
2243 }
2244
2245 static void
2246 nfp_net_tx_ring_bufs_free(struct nfp_net_dp *dp,
2247                           struct nfp_net_tx_ring *tx_ring)
2248 {
2249         unsigned int i;
2250
2251         if (!tx_ring->is_xdp)
2252                 return;
2253
2254         for (i = 0; i < tx_ring->cnt; i++) {
2255                 if (!tx_ring->txbufs[i].frag)
2256                         return;
2257
2258                 nfp_net_dma_unmap_rx(dp, tx_ring->txbufs[i].dma_addr);
2259                 __free_page(virt_to_page(tx_ring->txbufs[i].frag));
2260         }
2261 }
2262
2263 static int
2264 nfp_net_tx_ring_bufs_alloc(struct nfp_net_dp *dp,
2265                            struct nfp_net_tx_ring *tx_ring)
2266 {
2267         struct nfp_net_tx_buf *txbufs = tx_ring->txbufs;
2268         unsigned int i;
2269
2270         if (!tx_ring->is_xdp)
2271                 return 0;
2272
2273         for (i = 0; i < tx_ring->cnt; i++) {
2274                 txbufs[i].frag = nfp_net_rx_alloc_one(dp, &txbufs[i].dma_addr);
2275                 if (!txbufs[i].frag) {
2276                         nfp_net_tx_ring_bufs_free(dp, tx_ring);
2277                         return -ENOMEM;
2278                 }
2279         }
2280
2281         return 0;
2282 }
2283
2284 static int nfp_net_tx_rings_prepare(struct nfp_net *nn, struct nfp_net_dp *dp)
2285 {
2286         unsigned int r;
2287
2288         dp->tx_rings = kcalloc(dp->num_tx_rings, sizeof(*dp->tx_rings),
2289                                GFP_KERNEL);
2290         if (!dp->tx_rings)
2291                 return -ENOMEM;
2292
2293         for (r = 0; r < dp->num_tx_rings; r++) {
2294                 int bias = 0;
2295
2296                 if (r >= dp->num_stack_tx_rings)
2297                         bias = dp->num_stack_tx_rings;
2298
2299                 nfp_net_tx_ring_init(&dp->tx_rings[r], &nn->r_vecs[r - bias],
2300                                      r, bias);
2301
2302                 if (nfp_net_tx_ring_alloc(dp, &dp->tx_rings[r]))
2303                         goto err_free_prev;
2304
2305                 if (nfp_net_tx_ring_bufs_alloc(dp, &dp->tx_rings[r]))
2306                         goto err_free_ring;
2307         }
2308
2309         return 0;
2310
2311 err_free_prev:
2312         while (r--) {
2313                 nfp_net_tx_ring_bufs_free(dp, &dp->tx_rings[r]);
2314 err_free_ring:
2315                 nfp_net_tx_ring_free(&dp->tx_rings[r]);
2316         }
2317         kfree(dp->tx_rings);
2318         return -ENOMEM;
2319 }
2320
2321 static void nfp_net_tx_rings_free(struct nfp_net_dp *dp)
2322 {
2323         unsigned int r;
2324
2325         for (r = 0; r < dp->num_tx_rings; r++) {
2326                 nfp_net_tx_ring_bufs_free(dp, &dp->tx_rings[r]);
2327                 nfp_net_tx_ring_free(&dp->tx_rings[r]);
2328         }
2329
2330         kfree(dp->tx_rings);
2331 }
2332
2333 /**
2334  * nfp_net_rx_ring_free() - Free resources allocated to a RX ring
2335  * @rx_ring:  RX ring to free
2336  */
2337 static void nfp_net_rx_ring_free(struct nfp_net_rx_ring *rx_ring)
2338 {
2339         struct nfp_net_r_vector *r_vec = rx_ring->r_vec;
2340         struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
2341
2342         if (dp->netdev)
2343                 xdp_rxq_info_unreg(&rx_ring->xdp_rxq);
2344         kvfree(rx_ring->rxbufs);
2345
2346         if (rx_ring->rxds)
2347                 dma_free_coherent(dp->dev, rx_ring->size,
2348                                   rx_ring->rxds, rx_ring->dma);
2349
2350         rx_ring->cnt = 0;
2351         rx_ring->rxbufs = NULL;
2352         rx_ring->rxds = NULL;
2353         rx_ring->dma = 0;
2354         rx_ring->size = 0;
2355 }
2356
2357 /**
2358  * nfp_net_rx_ring_alloc() - Allocate resource for a RX ring
2359  * @dp:       NFP Net data path struct
2360  * @rx_ring:  RX ring to allocate
2361  *
2362  * Return: 0 on success, negative errno otherwise.
2363  */
2364 static int
2365 nfp_net_rx_ring_alloc(struct nfp_net_dp *dp, struct nfp_net_rx_ring *rx_ring)
2366 {
2367         int err;
2368
2369         if (dp->netdev) {
2370                 err = xdp_rxq_info_reg(&rx_ring->xdp_rxq, dp->netdev,
2371                                        rx_ring->idx);
2372                 if (err < 0)
2373                         return err;
2374         }
2375
2376         rx_ring->cnt = dp->rxd_cnt;
2377         rx_ring->size = array_size(rx_ring->cnt, sizeof(*rx_ring->rxds));
2378         rx_ring->rxds = dma_alloc_coherent(dp->dev, rx_ring->size,
2379                                            &rx_ring->dma,
2380                                            GFP_KERNEL | __GFP_NOWARN);
2381         if (!rx_ring->rxds) {
2382                 netdev_warn(dp->netdev, "failed to allocate RX descriptor ring memory, requested descriptor count: %d, consider lowering descriptor count\n",
2383                             rx_ring->cnt);
2384                 goto err_alloc;
2385         }
2386
2387         rx_ring->rxbufs = kvcalloc(rx_ring->cnt, sizeof(*rx_ring->rxbufs),
2388                                    GFP_KERNEL);
2389         if (!rx_ring->rxbufs)
2390                 goto err_alloc;
2391
2392         return 0;
2393
2394 err_alloc:
2395         nfp_net_rx_ring_free(rx_ring);
2396         return -ENOMEM;
2397 }
2398
2399 static int nfp_net_rx_rings_prepare(struct nfp_net *nn, struct nfp_net_dp *dp)
2400 {
2401         unsigned int r;
2402
2403         dp->rx_rings = kcalloc(dp->num_rx_rings, sizeof(*dp->rx_rings),
2404                                GFP_KERNEL);
2405         if (!dp->rx_rings)
2406                 return -ENOMEM;
2407
2408         for (r = 0; r < dp->num_rx_rings; r++) {
2409                 nfp_net_rx_ring_init(&dp->rx_rings[r], &nn->r_vecs[r], r);
2410
2411                 if (nfp_net_rx_ring_alloc(dp, &dp->rx_rings[r]))
2412                         goto err_free_prev;
2413
2414                 if (nfp_net_rx_ring_bufs_alloc(dp, &dp->rx_rings[r]))
2415                         goto err_free_ring;
2416         }
2417
2418         return 0;
2419
2420 err_free_prev:
2421         while (r--) {
2422                 nfp_net_rx_ring_bufs_free(dp, &dp->rx_rings[r]);
2423 err_free_ring:
2424                 nfp_net_rx_ring_free(&dp->rx_rings[r]);
2425         }
2426         kfree(dp->rx_rings);
2427         return -ENOMEM;
2428 }
2429
2430 static void nfp_net_rx_rings_free(struct nfp_net_dp *dp)
2431 {
2432         unsigned int r;
2433
2434         for (r = 0; r < dp->num_rx_rings; r++) {
2435                 nfp_net_rx_ring_bufs_free(dp, &dp->rx_rings[r]);
2436                 nfp_net_rx_ring_free(&dp->rx_rings[r]);
2437         }
2438
2439         kfree(dp->rx_rings);
2440 }
2441
2442 static void
2443 nfp_net_vector_assign_rings(struct nfp_net_dp *dp,
2444                             struct nfp_net_r_vector *r_vec, int idx)
2445 {
2446         r_vec->rx_ring = idx < dp->num_rx_rings ? &dp->rx_rings[idx] : NULL;
2447         r_vec->tx_ring =
2448                 idx < dp->num_stack_tx_rings ? &dp->tx_rings[idx] : NULL;
2449
2450         r_vec->xdp_ring = idx < dp->num_tx_rings - dp->num_stack_tx_rings ?
2451                 &dp->tx_rings[dp->num_stack_tx_rings + idx] : NULL;
2452 }
2453
2454 static int
2455 nfp_net_prepare_vector(struct nfp_net *nn, struct nfp_net_r_vector *r_vec,
2456                        int idx)
2457 {
2458         int err;
2459
2460         /* Setup NAPI */
2461         if (nn->dp.netdev)
2462                 netif_napi_add(nn->dp.netdev, &r_vec->napi,
2463                                nfp_net_poll, NAPI_POLL_WEIGHT);
2464         else
2465                 tasklet_enable(&r_vec->tasklet);
2466
2467         snprintf(r_vec->name, sizeof(r_vec->name),
2468                  "%s-rxtx-%d", nfp_net_name(nn), idx);
2469         err = request_irq(r_vec->irq_vector, r_vec->handler, 0, r_vec->name,
2470                           r_vec);
2471         if (err) {
2472                 if (nn->dp.netdev)
2473                         netif_napi_del(&r_vec->napi);
2474                 else
2475                         tasklet_disable(&r_vec->tasklet);
2476
2477                 nn_err(nn, "Error requesting IRQ %d\n", r_vec->irq_vector);
2478                 return err;
2479         }
2480         disable_irq(r_vec->irq_vector);
2481
2482         irq_set_affinity_hint(r_vec->irq_vector, &r_vec->affinity_mask);
2483
2484         nn_dbg(nn, "RV%02d: irq=%03d/%03d\n", idx, r_vec->irq_vector,
2485                r_vec->irq_entry);
2486
2487         return 0;
2488 }
2489
2490 static void
2491 nfp_net_cleanup_vector(struct nfp_net *nn, struct nfp_net_r_vector *r_vec)
2492 {
2493         irq_set_affinity_hint(r_vec->irq_vector, NULL);
2494         if (nn->dp.netdev)
2495                 netif_napi_del(&r_vec->napi);
2496         else
2497                 tasklet_disable(&r_vec->tasklet);
2498
2499         free_irq(r_vec->irq_vector, r_vec);
2500 }
2501
2502 /**
2503  * nfp_net_rss_write_itbl() - Write RSS indirection table to device
2504  * @nn:      NFP Net device to reconfigure
2505  */
2506 void nfp_net_rss_write_itbl(struct nfp_net *nn)
2507 {
2508         int i;
2509
2510         for (i = 0; i < NFP_NET_CFG_RSS_ITBL_SZ; i += 4)
2511                 nn_writel(nn, NFP_NET_CFG_RSS_ITBL + i,
2512                           get_unaligned_le32(nn->rss_itbl + i));
2513 }
2514
2515 /**
2516  * nfp_net_rss_write_key() - Write RSS hash key to device
2517  * @nn:      NFP Net device to reconfigure
2518  */
2519 void nfp_net_rss_write_key(struct nfp_net *nn)
2520 {
2521         int i;
2522
2523         for (i = 0; i < nfp_net_rss_key_sz(nn); i += 4)
2524                 nn_writel(nn, NFP_NET_CFG_RSS_KEY + i,
2525                           get_unaligned_le32(nn->rss_key + i));
2526 }
2527
2528 /**
2529  * nfp_net_coalesce_write_cfg() - Write irq coalescence configuration to HW
2530  * @nn:      NFP Net device to reconfigure
2531  */
2532 void nfp_net_coalesce_write_cfg(struct nfp_net *nn)
2533 {
2534         u8 i;
2535         u32 factor;
2536         u32 value;
2537
2538         /* Compute factor used to convert coalesce '_usecs' parameters to
2539          * ME timestamp ticks.  There are 16 ME clock cycles for each timestamp
2540          * count.
2541          */
2542         factor = nn->tlv_caps.me_freq_mhz / 16;
2543
2544         /* copy RX interrupt coalesce parameters */
2545         value = (nn->rx_coalesce_max_frames << 16) |
2546                 (factor * nn->rx_coalesce_usecs);
2547         for (i = 0; i < nn->dp.num_rx_rings; i++)
2548                 nn_writel(nn, NFP_NET_CFG_RXR_IRQ_MOD(i), value);
2549
2550         /* copy TX interrupt coalesce parameters */
2551         value = (nn->tx_coalesce_max_frames << 16) |
2552                 (factor * nn->tx_coalesce_usecs);
2553         for (i = 0; i < nn->dp.num_tx_rings; i++)
2554                 nn_writel(nn, NFP_NET_CFG_TXR_IRQ_MOD(i), value);
2555 }
2556
2557 /**
2558  * nfp_net_write_mac_addr() - Write mac address to the device control BAR
2559  * @nn:      NFP Net device to reconfigure
2560  * @addr:    MAC address to write
2561  *
2562  * Writes the MAC address from the netdev to the device control BAR.  Does not
2563  * perform the required reconfig.  We do a bit of byte swapping dance because
2564  * firmware is LE.
2565  */
2566 static void nfp_net_write_mac_addr(struct nfp_net *nn, const u8 *addr)
2567 {
2568         nn_writel(nn, NFP_NET_CFG_MACADDR + 0, get_unaligned_be32(addr));
2569         nn_writew(nn, NFP_NET_CFG_MACADDR + 6, get_unaligned_be16(addr + 4));
2570 }
2571
2572 static void nfp_net_vec_clear_ring_data(struct nfp_net *nn, unsigned int idx)
2573 {
2574         nn_writeq(nn, NFP_NET_CFG_RXR_ADDR(idx), 0);
2575         nn_writeb(nn, NFP_NET_CFG_RXR_SZ(idx), 0);
2576         nn_writeb(nn, NFP_NET_CFG_RXR_VEC(idx), 0);
2577
2578         nn_writeq(nn, NFP_NET_CFG_TXR_ADDR(idx), 0);
2579         nn_writeb(nn, NFP_NET_CFG_TXR_SZ(idx), 0);
2580         nn_writeb(nn, NFP_NET_CFG_TXR_VEC(idx), 0);
2581 }
2582
2583 /**
2584  * nfp_net_clear_config_and_disable() - Clear control BAR and disable NFP
2585  * @nn:      NFP Net device to reconfigure
2586  *
2587  * Warning: must be fully idempotent.
2588  */
2589 static void nfp_net_clear_config_and_disable(struct nfp_net *nn)
2590 {
2591         u32 new_ctrl, update;
2592         unsigned int r;
2593         int err;
2594
2595         new_ctrl = nn->dp.ctrl;
2596         new_ctrl &= ~NFP_NET_CFG_CTRL_ENABLE;
2597         update = NFP_NET_CFG_UPDATE_GEN;
2598         update |= NFP_NET_CFG_UPDATE_MSIX;
2599         update |= NFP_NET_CFG_UPDATE_RING;
2600
2601         if (nn->cap & NFP_NET_CFG_CTRL_RINGCFG)
2602                 new_ctrl &= ~NFP_NET_CFG_CTRL_RINGCFG;
2603
2604         nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, 0);
2605         nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, 0);
2606
2607         nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
2608         err = nfp_net_reconfig(nn, update);
2609         if (err)
2610                 nn_err(nn, "Could not disable device: %d\n", err);
2611
2612         for (r = 0; r < nn->dp.num_rx_rings; r++)
2613                 nfp_net_rx_ring_reset(&nn->dp.rx_rings[r]);
2614         for (r = 0; r < nn->dp.num_tx_rings; r++)
2615                 nfp_net_tx_ring_reset(&nn->dp, &nn->dp.tx_rings[r]);
2616         for (r = 0; r < nn->dp.num_r_vecs; r++)
2617                 nfp_net_vec_clear_ring_data(nn, r);
2618
2619         nn->dp.ctrl = new_ctrl;
2620 }
2621
2622 static void
2623 nfp_net_rx_ring_hw_cfg_write(struct nfp_net *nn,
2624                              struct nfp_net_rx_ring *rx_ring, unsigned int idx)
2625 {
2626         /* Write the DMA address, size and MSI-X info to the device */
2627         nn_writeq(nn, NFP_NET_CFG_RXR_ADDR(idx), rx_ring->dma);
2628         nn_writeb(nn, NFP_NET_CFG_RXR_SZ(idx), ilog2(rx_ring->cnt));
2629         nn_writeb(nn, NFP_NET_CFG_RXR_VEC(idx), rx_ring->r_vec->irq_entry);
2630 }
2631
2632 static void
2633 nfp_net_tx_ring_hw_cfg_write(struct nfp_net *nn,
2634                              struct nfp_net_tx_ring *tx_ring, unsigned int idx)
2635 {
2636         nn_writeq(nn, NFP_NET_CFG_TXR_ADDR(idx), tx_ring->dma);
2637         nn_writeb(nn, NFP_NET_CFG_TXR_SZ(idx), ilog2(tx_ring->cnt));
2638         nn_writeb(nn, NFP_NET_CFG_TXR_VEC(idx), tx_ring->r_vec->irq_entry);
2639 }
2640
2641 /**
2642  * nfp_net_set_config_and_enable() - Write control BAR and enable NFP
2643  * @nn:      NFP Net device to reconfigure
2644  */
2645 static int nfp_net_set_config_and_enable(struct nfp_net *nn)
2646 {
2647         u32 bufsz, new_ctrl, update = 0;
2648         unsigned int r;
2649         int err;
2650
2651         new_ctrl = nn->dp.ctrl;
2652
2653         if (nn->dp.ctrl & NFP_NET_CFG_CTRL_RSS_ANY) {
2654                 nfp_net_rss_write_key(nn);
2655                 nfp_net_rss_write_itbl(nn);
2656                 nn_writel(nn, NFP_NET_CFG_RSS_CTRL, nn->rss_cfg);
2657                 update |= NFP_NET_CFG_UPDATE_RSS;
2658         }
2659
2660         if (nn->dp.ctrl & NFP_NET_CFG_CTRL_IRQMOD) {
2661                 nfp_net_coalesce_write_cfg(nn);
2662                 update |= NFP_NET_CFG_UPDATE_IRQMOD;
2663         }
2664
2665         for (r = 0; r < nn->dp.num_tx_rings; r++)
2666                 nfp_net_tx_ring_hw_cfg_write(nn, &nn->dp.tx_rings[r], r);
2667         for (r = 0; r < nn->dp.num_rx_rings; r++)
2668                 nfp_net_rx_ring_hw_cfg_write(nn, &nn->dp.rx_rings[r], r);
2669
2670         nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, nn->dp.num_tx_rings == 64 ?
2671                   0xffffffffffffffffULL : ((u64)1 << nn->dp.num_tx_rings) - 1);
2672
2673         nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, nn->dp.num_rx_rings == 64 ?
2674                   0xffffffffffffffffULL : ((u64)1 << nn->dp.num_rx_rings) - 1);
2675
2676         if (nn->dp.netdev)
2677                 nfp_net_write_mac_addr(nn, nn->dp.netdev->dev_addr);
2678
2679         nn_writel(nn, NFP_NET_CFG_MTU, nn->dp.mtu);
2680
2681         bufsz = nn->dp.fl_bufsz - nn->dp.rx_dma_off - NFP_NET_RX_BUF_NON_DATA;
2682         nn_writel(nn, NFP_NET_CFG_FLBUFSZ, bufsz);
2683
2684         /* Enable device */
2685         new_ctrl |= NFP_NET_CFG_CTRL_ENABLE;
2686         update |= NFP_NET_CFG_UPDATE_GEN;
2687         update |= NFP_NET_CFG_UPDATE_MSIX;
2688         update |= NFP_NET_CFG_UPDATE_RING;
2689         if (nn->cap & NFP_NET_CFG_CTRL_RINGCFG)
2690                 new_ctrl |= NFP_NET_CFG_CTRL_RINGCFG;
2691
2692         nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
2693         err = nfp_net_reconfig(nn, update);
2694         if (err) {
2695                 nfp_net_clear_config_and_disable(nn);
2696                 return err;
2697         }
2698
2699         nn->dp.ctrl = new_ctrl;
2700
2701         for (r = 0; r < nn->dp.num_rx_rings; r++)
2702                 nfp_net_rx_ring_fill_freelist(&nn->dp, &nn->dp.rx_rings[r]);
2703
2704         /* Since reconfiguration requests while NFP is down are ignored we
2705          * have to wipe the entire VXLAN configuration and reinitialize it.
2706          */
2707         if (nn->dp.ctrl & NFP_NET_CFG_CTRL_VXLAN) {
2708                 memset(&nn->vxlan_ports, 0, sizeof(nn->vxlan_ports));
2709                 memset(&nn->vxlan_usecnt, 0, sizeof(nn->vxlan_usecnt));
2710                 udp_tunnel_get_rx_info(nn->dp.netdev);
2711         }
2712
2713         return 0;
2714 }
2715
2716 /**
2717  * nfp_net_close_stack() - Quiesce the stack (part of close)
2718  * @nn:      NFP Net device to reconfigure
2719  */
2720 static void nfp_net_close_stack(struct nfp_net *nn)
2721 {
2722         unsigned int r;
2723
2724         disable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
2725         netif_carrier_off(nn->dp.netdev);
2726         nn->link_up = false;
2727
2728         for (r = 0; r < nn->dp.num_r_vecs; r++) {
2729                 disable_irq(nn->r_vecs[r].irq_vector);
2730                 napi_disable(&nn->r_vecs[r].napi);
2731         }
2732
2733         netif_tx_disable(nn->dp.netdev);
2734 }
2735
2736 /**
2737  * nfp_net_close_free_all() - Free all runtime resources
2738  * @nn:      NFP Net device to reconfigure
2739  */
2740 static void nfp_net_close_free_all(struct nfp_net *nn)
2741 {
2742         unsigned int r;
2743
2744         nfp_net_tx_rings_free(&nn->dp);
2745         nfp_net_rx_rings_free(&nn->dp);
2746
2747         for (r = 0; r < nn->dp.num_r_vecs; r++)
2748                 nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
2749
2750         nfp_net_aux_irq_free(nn, NFP_NET_CFG_LSC, NFP_NET_IRQ_LSC_IDX);
2751         nfp_net_aux_irq_free(nn, NFP_NET_CFG_EXN, NFP_NET_IRQ_EXN_IDX);
2752 }
2753
2754 /**
2755  * nfp_net_netdev_close() - Called when the device is downed
2756  * @netdev:      netdev structure
2757  */
2758 static int nfp_net_netdev_close(struct net_device *netdev)
2759 {
2760         struct nfp_net *nn = netdev_priv(netdev);
2761
2762         /* Step 1: Disable RX and TX rings from the Linux kernel perspective
2763          */
2764         nfp_net_close_stack(nn);
2765
2766         /* Step 2: Tell NFP
2767          */
2768         nfp_net_clear_config_and_disable(nn);
2769         nfp_port_configure(netdev, false);
2770
2771         /* Step 3: Free resources
2772          */
2773         nfp_net_close_free_all(nn);
2774
2775         nn_dbg(nn, "%s down", netdev->name);
2776         return 0;
2777 }
2778
2779 void nfp_ctrl_close(struct nfp_net *nn)
2780 {
2781         int r;
2782
2783         rtnl_lock();
2784
2785         for (r = 0; r < nn->dp.num_r_vecs; r++) {
2786                 disable_irq(nn->r_vecs[r].irq_vector);
2787                 tasklet_disable(&nn->r_vecs[r].tasklet);
2788         }
2789
2790         nfp_net_clear_config_and_disable(nn);
2791
2792         nfp_net_close_free_all(nn);
2793
2794         rtnl_unlock();
2795 }
2796
2797 /**
2798  * nfp_net_open_stack() - Start the device from stack's perspective
2799  * @nn:      NFP Net device to reconfigure
2800  */
2801 static void nfp_net_open_stack(struct nfp_net *nn)
2802 {
2803         unsigned int r;
2804
2805         for (r = 0; r < nn->dp.num_r_vecs; r++) {
2806                 napi_enable(&nn->r_vecs[r].napi);
2807                 enable_irq(nn->r_vecs[r].irq_vector);
2808         }
2809
2810         netif_tx_wake_all_queues(nn->dp.netdev);
2811
2812         enable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
2813         nfp_net_read_link_status(nn);
2814 }
2815
2816 static int nfp_net_open_alloc_all(struct nfp_net *nn)
2817 {
2818         int err, r;
2819
2820         err = nfp_net_aux_irq_request(nn, NFP_NET_CFG_EXN, "%s-exn",
2821                                       nn->exn_name, sizeof(nn->exn_name),
2822                                       NFP_NET_IRQ_EXN_IDX, nn->exn_handler);
2823         if (err)
2824                 return err;
2825         err = nfp_net_aux_irq_request(nn, NFP_NET_CFG_LSC, "%s-lsc",
2826                                       nn->lsc_name, sizeof(nn->lsc_name),
2827                                       NFP_NET_IRQ_LSC_IDX, nn->lsc_handler);
2828         if (err)
2829                 goto err_free_exn;
2830         disable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
2831
2832         for (r = 0; r < nn->dp.num_r_vecs; r++) {
2833                 err = nfp_net_prepare_vector(nn, &nn->r_vecs[r], r);
2834                 if (err)
2835                         goto err_cleanup_vec_p;
2836         }
2837
2838         err = nfp_net_rx_rings_prepare(nn, &nn->dp);
2839         if (err)
2840                 goto err_cleanup_vec;
2841
2842         err = nfp_net_tx_rings_prepare(nn, &nn->dp);
2843         if (err)
2844                 goto err_free_rx_rings;
2845
2846         for (r = 0; r < nn->max_r_vecs; r++)
2847                 nfp_net_vector_assign_rings(&nn->dp, &nn->r_vecs[r], r);
2848
2849         return 0;
2850
2851 err_free_rx_rings:
2852         nfp_net_rx_rings_free(&nn->dp);
2853 err_cleanup_vec:
2854         r = nn->dp.num_r_vecs;
2855 err_cleanup_vec_p:
2856         while (r--)
2857                 nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
2858         nfp_net_aux_irq_free(nn, NFP_NET_CFG_LSC, NFP_NET_IRQ_LSC_IDX);
2859 err_free_exn:
2860         nfp_net_aux_irq_free(nn, NFP_NET_CFG_EXN, NFP_NET_IRQ_EXN_IDX);
2861         return err;
2862 }
2863
2864 static int nfp_net_netdev_open(struct net_device *netdev)
2865 {
2866         struct nfp_net *nn = netdev_priv(netdev);
2867         int err;
2868
2869         /* Step 1: Allocate resources for rings and the like
2870          * - Request interrupts
2871          * - Allocate RX and TX ring resources
2872          * - Setup initial RSS table
2873          */
2874         err = nfp_net_open_alloc_all(nn);
2875         if (err)
2876                 return err;
2877
2878         err = netif_set_real_num_tx_queues(netdev, nn->dp.num_stack_tx_rings);
2879         if (err)
2880                 goto err_free_all;
2881
2882         err = netif_set_real_num_rx_queues(netdev, nn->dp.num_rx_rings);
2883         if (err)
2884                 goto err_free_all;
2885
2886         /* Step 2: Configure the NFP
2887          * - Ifup the physical interface if it exists
2888          * - Enable rings from 0 to tx_rings/rx_rings - 1.
2889          * - Write MAC address (in case it changed)
2890          * - Set the MTU
2891          * - Set the Freelist buffer size
2892          * - Enable the FW
2893          */
2894         err = nfp_port_configure(netdev, true);
2895         if (err)
2896                 goto err_free_all;
2897
2898         err = nfp_net_set_config_and_enable(nn);
2899         if (err)
2900                 goto err_port_disable;
2901
2902         /* Step 3: Enable for kernel
2903          * - put some freelist descriptors on each RX ring
2904          * - enable NAPI on each ring
2905          * - enable all TX queues
2906          * - set link state
2907          */
2908         nfp_net_open_stack(nn);
2909
2910         return 0;
2911
2912 err_port_disable:
2913         nfp_port_configure(netdev, false);
2914 err_free_all:
2915         nfp_net_close_free_all(nn);
2916         return err;
2917 }
2918
2919 int nfp_ctrl_open(struct nfp_net *nn)
2920 {
2921         int err, r;
2922
2923         /* ring dumping depends on vNICs being opened/closed under rtnl */
2924         rtnl_lock();
2925
2926         err = nfp_net_open_alloc_all(nn);
2927         if (err)
2928                 goto err_unlock;
2929
2930         err = nfp_net_set_config_and_enable(nn);
2931         if (err)
2932                 goto err_free_all;
2933
2934         for (r = 0; r < nn->dp.num_r_vecs; r++)
2935                 enable_irq(nn->r_vecs[r].irq_vector);
2936
2937         rtnl_unlock();
2938
2939         return 0;
2940
2941 err_free_all:
2942         nfp_net_close_free_all(nn);
2943 err_unlock:
2944         rtnl_unlock();
2945         return err;
2946 }
2947
2948 static void nfp_net_set_rx_mode(struct net_device *netdev)
2949 {
2950         struct nfp_net *nn = netdev_priv(netdev);
2951         u32 new_ctrl;
2952
2953         new_ctrl = nn->dp.ctrl;
2954
2955         if (!netdev_mc_empty(netdev) || netdev->flags & IFF_ALLMULTI)
2956                 new_ctrl |= nn->cap & NFP_NET_CFG_CTRL_L2MC;
2957         else
2958                 new_ctrl &= ~NFP_NET_CFG_CTRL_L2MC;
2959
2960         if (netdev->flags & IFF_PROMISC) {
2961                 if (nn->cap & NFP_NET_CFG_CTRL_PROMISC)
2962                         new_ctrl |= NFP_NET_CFG_CTRL_PROMISC;
2963                 else
2964                         nn_warn(nn, "FW does not support promiscuous mode\n");
2965         } else {
2966                 new_ctrl &= ~NFP_NET_CFG_CTRL_PROMISC;
2967         }
2968
2969         if (new_ctrl == nn->dp.ctrl)
2970                 return;
2971
2972         nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
2973         nfp_net_reconfig_post(nn, NFP_NET_CFG_UPDATE_GEN);
2974
2975         nn->dp.ctrl = new_ctrl;
2976 }
2977
2978 static void nfp_net_rss_init_itbl(struct nfp_net *nn)
2979 {
2980         int i;
2981
2982         for (i = 0; i < sizeof(nn->rss_itbl); i++)
2983                 nn->rss_itbl[i] =
2984                         ethtool_rxfh_indir_default(i, nn->dp.num_rx_rings);
2985 }
2986
2987 static void nfp_net_dp_swap(struct nfp_net *nn, struct nfp_net_dp *dp)
2988 {
2989         struct nfp_net_dp new_dp = *dp;
2990
2991         *dp = nn->dp;
2992         nn->dp = new_dp;
2993
2994         nn->dp.netdev->mtu = new_dp.mtu;
2995
2996         if (!netif_is_rxfh_configured(nn->dp.netdev))
2997                 nfp_net_rss_init_itbl(nn);
2998 }
2999
3000 static int nfp_net_dp_swap_enable(struct nfp_net *nn, struct nfp_net_dp *dp)
3001 {
3002         unsigned int r;
3003         int err;
3004
3005         nfp_net_dp_swap(nn, dp);
3006
3007         for (r = 0; r < nn->max_r_vecs; r++)
3008                 nfp_net_vector_assign_rings(&nn->dp, &nn->r_vecs[r], r);
3009
3010         err = netif_set_real_num_rx_queues(nn->dp.netdev, nn->dp.num_rx_rings);
3011         if (err)
3012                 return err;
3013
3014         if (nn->dp.netdev->real_num_tx_queues != nn->dp.num_stack_tx_rings) {
3015                 err = netif_set_real_num_tx_queues(nn->dp.netdev,
3016                                                    nn->dp.num_stack_tx_rings);
3017                 if (err)
3018                         return err;
3019         }
3020
3021         return nfp_net_set_config_and_enable(nn);
3022 }
3023
3024 struct nfp_net_dp *nfp_net_clone_dp(struct nfp_net *nn)
3025 {
3026         struct nfp_net_dp *new;
3027
3028         new = kmalloc(sizeof(*new), GFP_KERNEL);
3029         if (!new)
3030                 return NULL;
3031
3032         *new = nn->dp;
3033
3034         /* Clear things which need to be recomputed */
3035         new->fl_bufsz = 0;
3036         new->tx_rings = NULL;
3037         new->rx_rings = NULL;
3038         new->num_r_vecs = 0;
3039         new->num_stack_tx_rings = 0;
3040
3041         return new;
3042 }
3043
3044 static int
3045 nfp_net_check_config(struct nfp_net *nn, struct nfp_net_dp *dp,
3046                      struct netlink_ext_ack *extack)
3047 {
3048         /* XDP-enabled tests */
3049         if (!dp->xdp_prog)
3050                 return 0;
3051         if (dp->fl_bufsz > PAGE_SIZE) {
3052                 NL_SET_ERR_MSG_MOD(extack, "MTU too large w/ XDP enabled");
3053                 return -EINVAL;
3054         }
3055         if (dp->num_tx_rings > nn->max_tx_rings) {
3056                 NL_SET_ERR_MSG_MOD(extack, "Insufficient number of TX rings w/ XDP enabled");
3057                 return -EINVAL;
3058         }
3059
3060         return 0;
3061 }
3062
3063 int nfp_net_ring_reconfig(struct nfp_net *nn, struct nfp_net_dp *dp,
3064                           struct netlink_ext_ack *extack)
3065 {
3066         int r, err;
3067
3068         dp->fl_bufsz = nfp_net_calc_fl_bufsz(dp);
3069
3070         dp->num_stack_tx_rings = dp->num_tx_rings;
3071         if (dp->xdp_prog)
3072                 dp->num_stack_tx_rings -= dp->num_rx_rings;
3073
3074         dp->num_r_vecs = max(dp->num_rx_rings, dp->num_stack_tx_rings);
3075
3076         err = nfp_net_check_config(nn, dp, extack);
3077         if (err)
3078                 goto exit_free_dp;
3079
3080         if (!netif_running(dp->netdev)) {
3081                 nfp_net_dp_swap(nn, dp);
3082                 err = 0;
3083                 goto exit_free_dp;
3084         }
3085
3086         /* Prepare new rings */
3087         for (r = nn->dp.num_r_vecs; r < dp->num_r_vecs; r++) {
3088                 err = nfp_net_prepare_vector(nn, &nn->r_vecs[r], r);
3089                 if (err) {
3090                         dp->num_r_vecs = r;
3091                         goto err_cleanup_vecs;
3092                 }
3093         }
3094
3095         err = nfp_net_rx_rings_prepare(nn, dp);
3096         if (err)
3097                 goto err_cleanup_vecs;
3098
3099         err = nfp_net_tx_rings_prepare(nn, dp);
3100         if (err)
3101                 goto err_free_rx;
3102
3103         /* Stop device, swap in new rings, try to start the firmware */
3104         nfp_net_close_stack(nn);
3105         nfp_net_clear_config_and_disable(nn);
3106
3107         err = nfp_net_dp_swap_enable(nn, dp);
3108         if (err) {
3109                 int err2;
3110
3111                 nfp_net_clear_config_and_disable(nn);
3112
3113                 /* Try with old configuration and old rings */
3114                 err2 = nfp_net_dp_swap_enable(nn, dp);
3115                 if (err2)
3116                         nn_err(nn, "Can't restore ring config - FW communication failed (%d,%d)\n",
3117                                err, err2);
3118         }
3119         for (r = dp->num_r_vecs - 1; r >= nn->dp.num_r_vecs; r--)
3120                 nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
3121
3122         nfp_net_rx_rings_free(dp);
3123         nfp_net_tx_rings_free(dp);
3124
3125         nfp_net_open_stack(nn);
3126 exit_free_dp:
3127         kfree(dp);
3128
3129         return err;
3130
3131 err_free_rx:
3132         nfp_net_rx_rings_free(dp);
3133 err_cleanup_vecs:
3134         for (r = dp->num_r_vecs - 1; r >= nn->dp.num_r_vecs; r--)
3135                 nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
3136         kfree(dp);
3137         return err;
3138 }
3139
3140 static int nfp_net_change_mtu(struct net_device *netdev, int new_mtu)
3141 {
3142         struct nfp_net *nn = netdev_priv(netdev);
3143         struct nfp_net_dp *dp;
3144         int err;
3145
3146         err = nfp_app_check_mtu(nn->app, netdev, new_mtu);
3147         if (err)
3148                 return err;
3149
3150         dp = nfp_net_clone_dp(nn);
3151         if (!dp)
3152                 return -ENOMEM;
3153
3154         dp->mtu = new_mtu;
3155
3156         return nfp_net_ring_reconfig(nn, dp, NULL);
3157 }
3158
3159 static int
3160 nfp_net_vlan_rx_add_vid(struct net_device *netdev, __be16 proto, u16 vid)
3161 {
3162         const u32 cmd = NFP_NET_CFG_MBOX_CMD_CTAG_FILTER_ADD;
3163         struct nfp_net *nn = netdev_priv(netdev);
3164         int err;
3165
3166         /* Priority tagged packets with vlan id 0 are processed by the
3167          * NFP as untagged packets
3168          */
3169         if (!vid)
3170                 return 0;
3171
3172         err = nfp_net_mbox_lock(nn, NFP_NET_CFG_VLAN_FILTER_SZ);
3173         if (err)
3174                 return err;
3175
3176         nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_VLAN_FILTER_VID, vid);
3177         nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_VLAN_FILTER_PROTO,
3178                   ETH_P_8021Q);
3179
3180         return nfp_net_mbox_reconfig_and_unlock(nn, cmd);
3181 }
3182
3183 static int
3184 nfp_net_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto, u16 vid)
3185 {
3186         const u32 cmd = NFP_NET_CFG_MBOX_CMD_CTAG_FILTER_KILL;
3187         struct nfp_net *nn = netdev_priv(netdev);
3188         int err;
3189
3190         /* Priority tagged packets with vlan id 0 are processed by the
3191          * NFP as untagged packets
3192          */
3193         if (!vid)
3194                 return 0;
3195
3196         err = nfp_net_mbox_lock(nn, NFP_NET_CFG_VLAN_FILTER_SZ);
3197         if (err)
3198                 return err;
3199
3200         nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_VLAN_FILTER_VID, vid);
3201         nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_VLAN_FILTER_PROTO,
3202                   ETH_P_8021Q);
3203
3204         return nfp_net_mbox_reconfig_and_unlock(nn, cmd);
3205 }
3206
3207 static void nfp_net_stat64(struct net_device *netdev,
3208                            struct rtnl_link_stats64 *stats)
3209 {
3210         struct nfp_net *nn = netdev_priv(netdev);
3211         int r;
3212
3213         /* Collect software stats */
3214         for (r = 0; r < nn->max_r_vecs; r++) {
3215                 struct nfp_net_r_vector *r_vec = &nn->r_vecs[r];
3216                 u64 data[3];
3217                 unsigned int start;
3218
3219                 do {
3220                         start = u64_stats_fetch_begin(&r_vec->rx_sync);
3221                         data[0] = r_vec->rx_pkts;
3222                         data[1] = r_vec->rx_bytes;
3223                         data[2] = r_vec->rx_drops;
3224                 } while (u64_stats_fetch_retry(&r_vec->rx_sync, start));
3225                 stats->rx_packets += data[0];
3226                 stats->rx_bytes += data[1];
3227                 stats->rx_dropped += data[2];
3228
3229                 do {
3230                         start = u64_stats_fetch_begin(&r_vec->tx_sync);
3231                         data[0] = r_vec->tx_pkts;
3232                         data[1] = r_vec->tx_bytes;
3233                         data[2] = r_vec->tx_errors;
3234                 } while (u64_stats_fetch_retry(&r_vec->tx_sync, start));
3235                 stats->tx_packets += data[0];
3236                 stats->tx_bytes += data[1];
3237                 stats->tx_errors += data[2];
3238         }
3239
3240         /* Add in device stats */
3241         stats->multicast += nn_readq(nn, NFP_NET_CFG_STATS_RX_MC_FRAMES);
3242         stats->rx_dropped += nn_readq(nn, NFP_NET_CFG_STATS_RX_DISCARDS);
3243         stats->rx_errors += nn_readq(nn, NFP_NET_CFG_STATS_RX_ERRORS);
3244
3245         stats->tx_dropped += nn_readq(nn, NFP_NET_CFG_STATS_TX_DISCARDS);
3246         stats->tx_errors += nn_readq(nn, NFP_NET_CFG_STATS_TX_ERRORS);
3247 }
3248
3249 static int nfp_net_set_features(struct net_device *netdev,
3250                                 netdev_features_t features)
3251 {
3252         netdev_features_t changed = netdev->features ^ features;
3253         struct nfp_net *nn = netdev_priv(netdev);
3254         u32 new_ctrl;
3255         int err;
3256
3257         /* Assume this is not called with features we have not advertised */
3258
3259         new_ctrl = nn->dp.ctrl;
3260
3261         if (changed & NETIF_F_RXCSUM) {
3262                 if (features & NETIF_F_RXCSUM)
3263                         new_ctrl |= nn->cap & NFP_NET_CFG_CTRL_RXCSUM_ANY;
3264                 else
3265                         new_ctrl &= ~NFP_NET_CFG_CTRL_RXCSUM_ANY;
3266         }
3267
3268         if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
3269                 if (features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))
3270                         new_ctrl |= NFP_NET_CFG_CTRL_TXCSUM;
3271                 else
3272                         new_ctrl &= ~NFP_NET_CFG_CTRL_TXCSUM;
3273         }
3274
3275         if (changed & (NETIF_F_TSO | NETIF_F_TSO6)) {
3276                 if (features & (NETIF_F_TSO | NETIF_F_TSO6))
3277                         new_ctrl |= nn->cap & NFP_NET_CFG_CTRL_LSO2 ?:
3278                                               NFP_NET_CFG_CTRL_LSO;
3279                 else
3280                         new_ctrl &= ~NFP_NET_CFG_CTRL_LSO_ANY;
3281         }
3282
3283         if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
3284                 if (features & NETIF_F_HW_VLAN_CTAG_RX)
3285                         new_ctrl |= NFP_NET_CFG_CTRL_RXVLAN;
3286                 else
3287                         new_ctrl &= ~NFP_NET_CFG_CTRL_RXVLAN;
3288         }
3289
3290         if (changed & NETIF_F_HW_VLAN_CTAG_TX) {
3291                 if (features & NETIF_F_HW_VLAN_CTAG_TX)
3292                         new_ctrl |= NFP_NET_CFG_CTRL_TXVLAN;
3293                 else
3294                         new_ctrl &= ~NFP_NET_CFG_CTRL_TXVLAN;
3295         }
3296
3297         if (changed & NETIF_F_HW_VLAN_CTAG_FILTER) {
3298                 if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
3299                         new_ctrl |= NFP_NET_CFG_CTRL_CTAG_FILTER;
3300                 else
3301                         new_ctrl &= ~NFP_NET_CFG_CTRL_CTAG_FILTER;
3302         }
3303
3304         if (changed & NETIF_F_SG) {
3305                 if (features & NETIF_F_SG)
3306                         new_ctrl |= NFP_NET_CFG_CTRL_GATHER;
3307                 else
3308                         new_ctrl &= ~NFP_NET_CFG_CTRL_GATHER;
3309         }
3310
3311         err = nfp_port_set_features(netdev, features);
3312         if (err)
3313                 return err;
3314
3315         nn_dbg(nn, "Feature change 0x%llx -> 0x%llx (changed=0x%llx)\n",
3316                netdev->features, features, changed);
3317
3318         if (new_ctrl == nn->dp.ctrl)
3319                 return 0;
3320
3321         nn_dbg(nn, "NIC ctrl: 0x%x -> 0x%x\n", nn->dp.ctrl, new_ctrl);
3322         nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
3323         err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_GEN);
3324         if (err)
3325                 return err;
3326
3327         nn->dp.ctrl = new_ctrl;
3328
3329         return 0;
3330 }
3331
3332 static netdev_features_t
3333 nfp_net_features_check(struct sk_buff *skb, struct net_device *dev,
3334                        netdev_features_t features)
3335 {
3336         u8 l4_hdr;
3337
3338         /* We can't do TSO over double tagged packets (802.1AD) */
3339         features &= vlan_features_check(skb, features);
3340
3341         if (!skb->encapsulation)
3342                 return features;
3343
3344         /* Ensure that inner L4 header offset fits into TX descriptor field */
3345         if (skb_is_gso(skb)) {
3346                 u32 hdrlen;
3347
3348                 hdrlen = skb_inner_transport_header(skb) - skb->data +
3349                         inner_tcp_hdrlen(skb);
3350
3351                 /* Assume worst case scenario of having longest possible
3352                  * metadata prepend - 8B
3353                  */
3354                 if (unlikely(hdrlen > NFP_NET_LSO_MAX_HDR_SZ - 8))
3355                         features &= ~NETIF_F_GSO_MASK;
3356         }
3357
3358         /* VXLAN/GRE check */
3359         switch (vlan_get_protocol(skb)) {
3360         case htons(ETH_P_IP):
3361                 l4_hdr = ip_hdr(skb)->protocol;
3362                 break;
3363         case htons(ETH_P_IPV6):
3364                 l4_hdr = ipv6_hdr(skb)->nexthdr;
3365                 break;
3366         default:
3367                 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
3368         }
3369
3370         if (skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
3371             skb->inner_protocol != htons(ETH_P_TEB) ||
3372             (l4_hdr != IPPROTO_UDP && l4_hdr != IPPROTO_GRE) ||
3373             (l4_hdr == IPPROTO_UDP &&
3374              (skb_inner_mac_header(skb) - skb_transport_header(skb) !=
3375               sizeof(struct udphdr) + sizeof(struct vxlanhdr))))
3376                 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
3377
3378         return features;
3379 }
3380
3381 static int
3382 nfp_net_get_phys_port_name(struct net_device *netdev, char *name, size_t len)
3383 {
3384         struct nfp_net *nn = netdev_priv(netdev);
3385         int n;
3386
3387         /* If port is defined, devlink_port is registered and devlink core
3388          * is taking care of name formatting.
3389          */
3390         if (nn->port)
3391                 return -EOPNOTSUPP;
3392
3393         if (nn->dp.is_vf || nn->vnic_no_name)
3394                 return -EOPNOTSUPP;
3395
3396         n = snprintf(name, len, "n%d", nn->id);
3397         if (n >= len)
3398                 return -EINVAL;
3399
3400         return 0;
3401 }
3402
3403 /**
3404  * nfp_net_set_vxlan_port() - set vxlan port in SW and reconfigure HW
3405  * @nn:   NFP Net device to reconfigure
3406  * @idx:  Index into the port table where new port should be written
3407  * @port: UDP port to configure (pass zero to remove VXLAN port)
3408  */
3409 static void nfp_net_set_vxlan_port(struct nfp_net *nn, int idx, __be16 port)
3410 {
3411         int i;
3412
3413         nn->vxlan_ports[idx] = port;
3414
3415         if (!(nn->dp.ctrl & NFP_NET_CFG_CTRL_VXLAN))
3416                 return;
3417
3418         BUILD_BUG_ON(NFP_NET_N_VXLAN_PORTS & 1);
3419         for (i = 0; i < NFP_NET_N_VXLAN_PORTS; i += 2)
3420                 nn_writel(nn, NFP_NET_CFG_VXLAN_PORT + i * sizeof(port),
3421                           be16_to_cpu(nn->vxlan_ports[i + 1]) << 16 |
3422                           be16_to_cpu(nn->vxlan_ports[i]));
3423
3424         nfp_net_reconfig_post(nn, NFP_NET_CFG_UPDATE_VXLAN);
3425 }
3426
3427 /**
3428  * nfp_net_find_vxlan_idx() - find table entry of the port or a free one
3429  * @nn:   NFP Network structure
3430  * @port: UDP port to look for
3431  *
3432  * Return: if the port is already in the table -- it's position;
3433  *         if the port is not in the table -- free position to use;
3434  *         if the table is full -- -ENOSPC.
3435  */
3436 static int nfp_net_find_vxlan_idx(struct nfp_net *nn, __be16 port)
3437 {
3438         int i, free_idx = -ENOSPC;
3439
3440         for (i = 0; i < NFP_NET_N_VXLAN_PORTS; i++) {
3441                 if (nn->vxlan_ports[i] == port)
3442                         return i;
3443                 if (!nn->vxlan_usecnt[i])
3444                         free_idx = i;
3445         }
3446
3447         return free_idx;
3448 }
3449
3450 static void nfp_net_add_vxlan_port(struct net_device *netdev,
3451                                    struct udp_tunnel_info *ti)
3452 {
3453         struct nfp_net *nn = netdev_priv(netdev);
3454         int idx;
3455
3456         if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3457                 return;
3458
3459         idx = nfp_net_find_vxlan_idx(nn, ti->port);
3460         if (idx == -ENOSPC)
3461                 return;
3462
3463         if (!nn->vxlan_usecnt[idx]++)
3464                 nfp_net_set_vxlan_port(nn, idx, ti->port);
3465 }
3466
3467 static void nfp_net_del_vxlan_port(struct net_device *netdev,
3468                                    struct udp_tunnel_info *ti)
3469 {
3470         struct nfp_net *nn = netdev_priv(netdev);
3471         int idx;
3472
3473         if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3474                 return;
3475
3476         idx = nfp_net_find_vxlan_idx(nn, ti->port);
3477         if (idx == -ENOSPC || !nn->vxlan_usecnt[idx])
3478                 return;
3479
3480         if (!--nn->vxlan_usecnt[idx])
3481                 nfp_net_set_vxlan_port(nn, idx, 0);
3482 }
3483
3484 static int nfp_net_xdp_setup_drv(struct nfp_net *nn, struct netdev_bpf *bpf)
3485 {
3486         struct bpf_prog *prog = bpf->prog;
3487         struct nfp_net_dp *dp;
3488         int err;
3489
3490         if (!xdp_attachment_flags_ok(&nn->xdp, bpf))
3491                 return -EBUSY;
3492
3493         if (!prog == !nn->dp.xdp_prog) {
3494                 WRITE_ONCE(nn->dp.xdp_prog, prog);
3495                 xdp_attachment_setup(&nn->xdp, bpf);
3496                 return 0;
3497         }
3498
3499         dp = nfp_net_clone_dp(nn);
3500         if (!dp)
3501                 return -ENOMEM;
3502
3503         dp->xdp_prog = prog;
3504         dp->num_tx_rings += prog ? nn->dp.num_rx_rings : -nn->dp.num_rx_rings;
3505         dp->rx_dma_dir = prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
3506         dp->rx_dma_off = prog ? XDP_PACKET_HEADROOM - nn->dp.rx_offset : 0;
3507
3508         /* We need RX reconfig to remap the buffers (BIDIR vs FROM_DEV) */
3509         err = nfp_net_ring_reconfig(nn, dp, bpf->extack);
3510         if (err)
3511                 return err;
3512
3513         xdp_attachment_setup(&nn->xdp, bpf);
3514         return 0;
3515 }
3516
3517 static int nfp_net_xdp_setup_hw(struct nfp_net *nn, struct netdev_bpf *bpf)
3518 {
3519         int err;
3520
3521         if (!xdp_attachment_flags_ok(&nn->xdp_hw, bpf))
3522                 return -EBUSY;
3523
3524         err = nfp_app_xdp_offload(nn->app, nn, bpf->prog, bpf->extack);
3525         if (err)
3526                 return err;
3527
3528         xdp_attachment_setup(&nn->xdp_hw, bpf);
3529         return 0;
3530 }
3531
3532 static int nfp_net_xdp(struct net_device *netdev, struct netdev_bpf *xdp)
3533 {
3534         struct nfp_net *nn = netdev_priv(netdev);
3535
3536         switch (xdp->command) {
3537         case XDP_SETUP_PROG:
3538                 return nfp_net_xdp_setup_drv(nn, xdp);
3539         case XDP_SETUP_PROG_HW:
3540                 return nfp_net_xdp_setup_hw(nn, xdp);
3541         case XDP_QUERY_PROG:
3542                 return xdp_attachment_query(&nn->xdp, xdp);
3543         case XDP_QUERY_PROG_HW:
3544                 return xdp_attachment_query(&nn->xdp_hw, xdp);
3545         default:
3546                 return nfp_app_bpf(nn->app, nn, xdp);
3547         }
3548 }
3549
3550 static int nfp_net_set_mac_address(struct net_device *netdev, void *addr)
3551 {
3552         struct nfp_net *nn = netdev_priv(netdev);
3553         struct sockaddr *saddr = addr;
3554         int err;
3555
3556         err = eth_prepare_mac_addr_change(netdev, addr);
3557         if (err)
3558                 return err;
3559
3560         nfp_net_write_mac_addr(nn, saddr->sa_data);
3561
3562         err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_MACADDR);
3563         if (err)
3564                 return err;
3565
3566         eth_commit_mac_addr_change(netdev, addr);
3567
3568         return 0;
3569 }
3570
3571 const struct net_device_ops nfp_net_netdev_ops = {
3572         .ndo_init               = nfp_app_ndo_init,
3573         .ndo_uninit             = nfp_app_ndo_uninit,
3574         .ndo_open               = nfp_net_netdev_open,
3575         .ndo_stop               = nfp_net_netdev_close,
3576         .ndo_start_xmit         = nfp_net_tx,
3577         .ndo_get_stats64        = nfp_net_stat64,
3578         .ndo_vlan_rx_add_vid    = nfp_net_vlan_rx_add_vid,
3579         .ndo_vlan_rx_kill_vid   = nfp_net_vlan_rx_kill_vid,
3580         .ndo_set_vf_mac         = nfp_app_set_vf_mac,
3581         .ndo_set_vf_vlan        = nfp_app_set_vf_vlan,
3582         .ndo_set_vf_spoofchk    = nfp_app_set_vf_spoofchk,
3583         .ndo_get_vf_config      = nfp_app_get_vf_config,
3584         .ndo_set_vf_link_state  = nfp_app_set_vf_link_state,
3585         .ndo_setup_tc           = nfp_port_setup_tc,
3586         .ndo_tx_timeout         = nfp_net_tx_timeout,
3587         .ndo_set_rx_mode        = nfp_net_set_rx_mode,
3588         .ndo_change_mtu         = nfp_net_change_mtu,
3589         .ndo_set_mac_address    = nfp_net_set_mac_address,
3590         .ndo_set_features       = nfp_net_set_features,
3591         .ndo_features_check     = nfp_net_features_check,
3592         .ndo_get_phys_port_name = nfp_net_get_phys_port_name,
3593         .ndo_udp_tunnel_add     = nfp_net_add_vxlan_port,
3594         .ndo_udp_tunnel_del     = nfp_net_del_vxlan_port,
3595         .ndo_bpf                = nfp_net_xdp,
3596         .ndo_get_devlink_port   = nfp_devlink_get_devlink_port,
3597 };
3598
3599 /**
3600  * nfp_net_info() - Print general info about the NIC
3601  * @nn:      NFP Net device to reconfigure
3602  */
3603 void nfp_net_info(struct nfp_net *nn)
3604 {
3605         nn_info(nn, "Netronome NFP-6xxx %sNetdev: TxQs=%d/%d RxQs=%d/%d\n",
3606                 nn->dp.is_vf ? "VF " : "",
3607                 nn->dp.num_tx_rings, nn->max_tx_rings,
3608                 nn->dp.num_rx_rings, nn->max_rx_rings);
3609         nn_info(nn, "VER: %d.%d.%d.%d, Maximum supported MTU: %d\n",
3610                 nn->fw_ver.resv, nn->fw_ver.class,
3611                 nn->fw_ver.major, nn->fw_ver.minor,
3612                 nn->max_mtu);
3613         nn_info(nn, "CAP: %#x %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
3614                 nn->cap,
3615                 nn->cap & NFP_NET_CFG_CTRL_PROMISC  ? "PROMISC "  : "",
3616                 nn->cap & NFP_NET_CFG_CTRL_L2BC     ? "L2BCFILT " : "",
3617                 nn->cap & NFP_NET_CFG_CTRL_L2MC     ? "L2MCFILT " : "",
3618                 nn->cap & NFP_NET_CFG_CTRL_RXCSUM   ? "RXCSUM "   : "",
3619                 nn->cap & NFP_NET_CFG_CTRL_TXCSUM   ? "TXCSUM "   : "",
3620                 nn->cap & NFP_NET_CFG_CTRL_RXVLAN   ? "RXVLAN "   : "",
3621                 nn->cap & NFP_NET_CFG_CTRL_TXVLAN   ? "TXVLAN "   : "",
3622                 nn->cap & NFP_NET_CFG_CTRL_SCATTER  ? "SCATTER "  : "",
3623                 nn->cap & NFP_NET_CFG_CTRL_GATHER   ? "GATHER "   : "",
3624                 nn->cap & NFP_NET_CFG_CTRL_LSO      ? "TSO1 "     : "",
3625                 nn->cap & NFP_NET_CFG_CTRL_LSO2     ? "TSO2 "     : "",
3626                 nn->cap & NFP_NET_CFG_CTRL_RSS      ? "RSS1 "     : "",
3627                 nn->cap & NFP_NET_CFG_CTRL_RSS2     ? "RSS2 "     : "",
3628                 nn->cap & NFP_NET_CFG_CTRL_CTAG_FILTER ? "CTAG_FILTER " : "",
3629                 nn->cap & NFP_NET_CFG_CTRL_MSIXAUTO ? "AUTOMASK " : "",
3630                 nn->cap & NFP_NET_CFG_CTRL_IRQMOD   ? "IRQMOD "   : "",
3631                 nn->cap & NFP_NET_CFG_CTRL_VXLAN    ? "VXLAN "    : "",
3632                 nn->cap & NFP_NET_CFG_CTRL_NVGRE    ? "NVGRE "    : "",
3633                 nn->cap & NFP_NET_CFG_CTRL_CSUM_COMPLETE ?
3634                                                       "RXCSUM_COMPLETE " : "",
3635                 nn->cap & NFP_NET_CFG_CTRL_LIVE_ADDR ? "LIVE_ADDR " : "",
3636                 nfp_app_extra_cap(nn->app, nn));
3637 }
3638
3639 /**
3640  * nfp_net_alloc() - Allocate netdev and related structure
3641  * @pdev:         PCI device
3642  * @ctrl_bar:     PCI IOMEM with vNIC config memory
3643  * @needs_netdev: Whether to allocate a netdev for this vNIC
3644  * @max_tx_rings: Maximum number of TX rings supported by device
3645  * @max_rx_rings: Maximum number of RX rings supported by device
3646  *
3647  * This function allocates a netdev device and fills in the initial
3648  * part of the @struct nfp_net structure.  In case of control device
3649  * nfp_net structure is allocated without the netdev.
3650  *
3651  * Return: NFP Net device structure, or ERR_PTR on error.
3652  */
3653 struct nfp_net *
3654 nfp_net_alloc(struct pci_dev *pdev, void __iomem *ctrl_bar, bool needs_netdev,
3655               unsigned int max_tx_rings, unsigned int max_rx_rings)
3656 {
3657         struct nfp_net *nn;
3658         int err;
3659
3660         if (needs_netdev) {
3661                 struct net_device *netdev;
3662
3663                 netdev = alloc_etherdev_mqs(sizeof(struct nfp_net),
3664                                             max_tx_rings, max_rx_rings);
3665                 if (!netdev)
3666                         return ERR_PTR(-ENOMEM);
3667
3668                 SET_NETDEV_DEV(netdev, &pdev->dev);
3669                 nn = netdev_priv(netdev);
3670                 nn->dp.netdev = netdev;
3671         } else {
3672                 nn = vzalloc(sizeof(*nn));
3673                 if (!nn)
3674                         return ERR_PTR(-ENOMEM);
3675         }
3676
3677         nn->dp.dev = &pdev->dev;
3678         nn->dp.ctrl_bar = ctrl_bar;
3679         nn->pdev = pdev;
3680
3681         nn->max_tx_rings = max_tx_rings;
3682         nn->max_rx_rings = max_rx_rings;
3683
3684         nn->dp.num_tx_rings = min_t(unsigned int,
3685                                     max_tx_rings, num_online_cpus());
3686         nn->dp.num_rx_rings = min_t(unsigned int, max_rx_rings,
3687                                  netif_get_num_default_rss_queues());
3688
3689         nn->dp.num_r_vecs = max(nn->dp.num_tx_rings, nn->dp.num_rx_rings);
3690         nn->dp.num_r_vecs = min_t(unsigned int,
3691                                   nn->dp.num_r_vecs, num_online_cpus());
3692
3693         nn->dp.txd_cnt = NFP_NET_TX_DESCS_DEFAULT;
3694         nn->dp.rxd_cnt = NFP_NET_RX_DESCS_DEFAULT;
3695
3696         mutex_init(&nn->bar_lock);
3697
3698         spin_lock_init(&nn->reconfig_lock);
3699         spin_lock_init(&nn->link_status_lock);
3700
3701         timer_setup(&nn->reconfig_timer, nfp_net_reconfig_timer, 0);
3702
3703         err = nfp_net_tlv_caps_parse(&nn->pdev->dev, nn->dp.ctrl_bar,
3704                                      &nn->tlv_caps);
3705         if (err)
3706                 goto err_free_nn;
3707
3708         return nn;
3709
3710 err_free_nn:
3711         if (nn->dp.netdev)
3712                 free_netdev(nn->dp.netdev);
3713         else
3714                 vfree(nn);
3715         return ERR_PTR(err);
3716 }
3717
3718 /**
3719  * nfp_net_free() - Undo what @nfp_net_alloc() did
3720  * @nn:      NFP Net device to reconfigure
3721  */
3722 void nfp_net_free(struct nfp_net *nn)
3723 {
3724         WARN_ON(timer_pending(&nn->reconfig_timer) || nn->reconfig_posted);
3725
3726         mutex_destroy(&nn->bar_lock);
3727
3728         if (nn->dp.netdev)
3729                 free_netdev(nn->dp.netdev);
3730         else
3731                 vfree(nn);
3732 }
3733
3734 /**
3735  * nfp_net_rss_key_sz() - Get current size of the RSS key
3736  * @nn:         NFP Net device instance
3737  *
3738  * Return: size of the RSS key for currently selected hash function.
3739  */
3740 unsigned int nfp_net_rss_key_sz(struct nfp_net *nn)
3741 {
3742         switch (nn->rss_hfunc) {
3743         case ETH_RSS_HASH_TOP:
3744                 return NFP_NET_CFG_RSS_KEY_SZ;
3745         case ETH_RSS_HASH_XOR:
3746                 return 0;
3747         case ETH_RSS_HASH_CRC32:
3748                 return 4;
3749         }
3750
3751         nn_warn(nn, "Unknown hash function: %u\n", nn->rss_hfunc);
3752         return 0;
3753 }
3754
3755 /**
3756  * nfp_net_rss_init() - Set the initial RSS parameters
3757  * @nn:      NFP Net device to reconfigure
3758  */
3759 static void nfp_net_rss_init(struct nfp_net *nn)
3760 {
3761         unsigned long func_bit, rss_cap_hfunc;
3762         u32 reg;
3763
3764         /* Read the RSS function capability and select first supported func */
3765         reg = nn_readl(nn, NFP_NET_CFG_RSS_CAP);
3766         rss_cap_hfunc = FIELD_GET(NFP_NET_CFG_RSS_CAP_HFUNC, reg);
3767         if (!rss_cap_hfunc)
3768                 rss_cap_hfunc = FIELD_GET(NFP_NET_CFG_RSS_CAP_HFUNC,
3769                                           NFP_NET_CFG_RSS_TOEPLITZ);
3770
3771         func_bit = find_first_bit(&rss_cap_hfunc, NFP_NET_CFG_RSS_HFUNCS);
3772         if (func_bit == NFP_NET_CFG_RSS_HFUNCS) {
3773                 dev_warn(nn->dp.dev,
3774                          "Bad RSS config, defaulting to Toeplitz hash\n");
3775                 func_bit = ETH_RSS_HASH_TOP_BIT;
3776         }
3777         nn->rss_hfunc = 1 << func_bit;
3778
3779         netdev_rss_key_fill(nn->rss_key, nfp_net_rss_key_sz(nn));
3780
3781         nfp_net_rss_init_itbl(nn);
3782
3783         /* Enable IPv4/IPv6 TCP by default */
3784         nn->rss_cfg = NFP_NET_CFG_RSS_IPV4_TCP |
3785                       NFP_NET_CFG_RSS_IPV6_TCP |
3786                       FIELD_PREP(NFP_NET_CFG_RSS_HFUNC, nn->rss_hfunc) |
3787                       NFP_NET_CFG_RSS_MASK;
3788 }
3789
3790 /**
3791  * nfp_net_irqmod_init() - Set the initial IRQ moderation parameters
3792  * @nn:      NFP Net device to reconfigure
3793  */
3794 static void nfp_net_irqmod_init(struct nfp_net *nn)
3795 {
3796         nn->rx_coalesce_usecs      = 50;
3797         nn->rx_coalesce_max_frames = 64;
3798         nn->tx_coalesce_usecs      = 50;
3799         nn->tx_coalesce_max_frames = 64;
3800 }
3801
3802 static void nfp_net_netdev_init(struct nfp_net *nn)
3803 {
3804         struct net_device *netdev = nn->dp.netdev;
3805
3806         nfp_net_write_mac_addr(nn, nn->dp.netdev->dev_addr);
3807
3808         netdev->mtu = nn->dp.mtu;
3809
3810         /* Advertise/enable offloads based on capabilities
3811          *
3812          * Note: netdev->features show the currently enabled features
3813          * and netdev->hw_features advertises which features are
3814          * supported.  By default we enable most features.
3815          */
3816         if (nn->cap & NFP_NET_CFG_CTRL_LIVE_ADDR)
3817                 netdev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
3818
3819         netdev->hw_features = NETIF_F_HIGHDMA;
3820         if (nn->cap & NFP_NET_CFG_CTRL_RXCSUM_ANY) {
3821                 netdev->hw_features |= NETIF_F_RXCSUM;
3822                 nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_RXCSUM_ANY;
3823         }
3824         if (nn->cap & NFP_NET_CFG_CTRL_TXCSUM) {
3825                 netdev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
3826                 nn->dp.ctrl |= NFP_NET_CFG_CTRL_TXCSUM;
3827         }
3828         if (nn->cap & NFP_NET_CFG_CTRL_GATHER) {
3829                 netdev->hw_features |= NETIF_F_SG;
3830                 nn->dp.ctrl |= NFP_NET_CFG_CTRL_GATHER;
3831         }
3832         if ((nn->cap & NFP_NET_CFG_CTRL_LSO && nn->fw_ver.major > 2) ||
3833             nn->cap & NFP_NET_CFG_CTRL_LSO2) {
3834                 netdev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
3835                 nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_LSO2 ?:
3836                                          NFP_NET_CFG_CTRL_LSO;
3837         }
3838         if (nn->cap & NFP_NET_CFG_CTRL_RSS_ANY)
3839                 netdev->hw_features |= NETIF_F_RXHASH;
3840         if (nn->cap & NFP_NET_CFG_CTRL_VXLAN) {
3841                 if (nn->cap & NFP_NET_CFG_CTRL_LSO)
3842                         netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL;
3843                 nn->dp.ctrl |= NFP_NET_CFG_CTRL_VXLAN;
3844         }
3845         if (nn->cap & NFP_NET_CFG_CTRL_NVGRE) {
3846                 if (nn->cap & NFP_NET_CFG_CTRL_LSO)
3847                         netdev->hw_features |= NETIF_F_GSO_GRE;
3848                 nn->dp.ctrl |= NFP_NET_CFG_CTRL_NVGRE;
3849         }
3850         if (nn->cap & (NFP_NET_CFG_CTRL_VXLAN | NFP_NET_CFG_CTRL_NVGRE))
3851                 netdev->hw_enc_features = netdev->hw_features;
3852
3853         netdev->vlan_features = netdev->hw_features;
3854
3855         if (nn->cap & NFP_NET_CFG_CTRL_RXVLAN) {
3856                 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
3857                 nn->dp.ctrl |= NFP_NET_CFG_CTRL_RXVLAN;
3858         }
3859         if (nn->cap & NFP_NET_CFG_CTRL_TXVLAN) {
3860                 if (nn->cap & NFP_NET_CFG_CTRL_LSO2) {
3861                         nn_warn(nn, "Device advertises both TSO2 and TXVLAN. Refusing to enable TXVLAN.\n");
3862                 } else {
3863                         netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
3864                         nn->dp.ctrl |= NFP_NET_CFG_CTRL_TXVLAN;
3865                 }
3866         }
3867         if (nn->cap & NFP_NET_CFG_CTRL_CTAG_FILTER) {
3868                 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
3869                 nn->dp.ctrl |= NFP_NET_CFG_CTRL_CTAG_FILTER;
3870         }
3871
3872         netdev->features = netdev->hw_features;
3873
3874         if (nfp_app_has_tc(nn->app) && nn->port)
3875                 netdev->hw_features |= NETIF_F_HW_TC;
3876
3877         /* Advertise but disable TSO by default. */
3878         netdev->features &= ~(NETIF_F_TSO | NETIF_F_TSO6);
3879         nn->dp.ctrl &= ~NFP_NET_CFG_CTRL_LSO_ANY;
3880
3881         /* Finalise the netdev setup */
3882         netdev->netdev_ops = &nfp_net_netdev_ops;
3883         netdev->watchdog_timeo = msecs_to_jiffies(5 * 1000);
3884
3885         /* MTU range: 68 - hw-specific max */
3886         netdev->min_mtu = ETH_MIN_MTU;
3887         netdev->max_mtu = nn->max_mtu;
3888
3889         netdev->gso_max_segs = NFP_NET_LSO_MAX_SEGS;
3890
3891         netif_carrier_off(netdev);
3892
3893         nfp_net_set_ethtool_ops(netdev);
3894 }
3895
3896 static int nfp_net_read_caps(struct nfp_net *nn)
3897 {
3898         /* Get some of the read-only fields from the BAR */
3899         nn->cap = nn_readl(nn, NFP_NET_CFG_CAP);
3900         nn->max_mtu = nn_readl(nn, NFP_NET_CFG_MAX_MTU);
3901
3902         /* ABI 4.x and ctrl vNIC always use chained metadata, in other cases
3903          * we allow use of non-chained metadata if RSS(v1) is the only
3904          * advertised capability requiring metadata.
3905          */
3906         nn->dp.chained_metadata_format = nn->fw_ver.major == 4 ||
3907                                          !nn->dp.netdev ||
3908                                          !(nn->cap & NFP_NET_CFG_CTRL_RSS) ||
3909                                          nn->cap & NFP_NET_CFG_CTRL_CHAIN_META;
3910         /* RSS(v1) uses non-chained metadata format, except in ABI 4.x where
3911          * it has the same meaning as RSSv2.
3912          */
3913         if (nn->dp.chained_metadata_format && nn->fw_ver.major != 4)
3914                 nn->cap &= ~NFP_NET_CFG_CTRL_RSS;
3915
3916         /* Determine RX packet/metadata boundary offset */
3917         if (nn->fw_ver.major >= 2) {
3918                 u32 reg;
3919
3920                 reg = nn_readl(nn, NFP_NET_CFG_RX_OFFSET);
3921                 if (reg > NFP_NET_MAX_PREPEND) {
3922                         nn_err(nn, "Invalid rx offset: %d\n", reg);
3923                         return -EINVAL;
3924                 }
3925                 nn->dp.rx_offset = reg;
3926         } else {
3927                 nn->dp.rx_offset = NFP_NET_RX_OFFSET;
3928         }
3929
3930         /* For control vNICs mask out the capabilities app doesn't want. */
3931         if (!nn->dp.netdev)
3932                 nn->cap &= nn->app->type->ctrl_cap_mask;
3933
3934         return 0;
3935 }
3936
3937 /**
3938  * nfp_net_init() - Initialise/finalise the nfp_net structure
3939  * @nn:         NFP Net device structure
3940  *
3941  * Return: 0 on success or negative errno on error.
3942  */
3943 int nfp_net_init(struct nfp_net *nn)
3944 {
3945         int err;
3946
3947         nn->dp.rx_dma_dir = DMA_FROM_DEVICE;
3948
3949         err = nfp_net_read_caps(nn);
3950         if (err)
3951                 return err;
3952
3953         /* Set default MTU and Freelist buffer size */
3954         if (!nfp_net_is_data_vnic(nn) && nn->app->ctrl_mtu) {
3955                 if (nn->app->ctrl_mtu <= nn->max_mtu) {
3956                         nn->dp.mtu = nn->app->ctrl_mtu;
3957                 } else {
3958                         if (nn->app->ctrl_mtu != NFP_APP_CTRL_MTU_MAX)
3959                                 nn_warn(nn, "app requested MTU above max supported %u > %u\n",
3960                                         nn->app->ctrl_mtu, nn->max_mtu);
3961                         nn->dp.mtu = nn->max_mtu;
3962                 }
3963         } else if (nn->max_mtu < NFP_NET_DEFAULT_MTU) {
3964                 nn->dp.mtu = nn->max_mtu;
3965         } else {
3966                 nn->dp.mtu = NFP_NET_DEFAULT_MTU;
3967         }
3968         nn->dp.fl_bufsz = nfp_net_calc_fl_bufsz(&nn->dp);
3969
3970         if (nfp_app_ctrl_uses_data_vnics(nn->app))
3971                 nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_CMSG_DATA;
3972
3973         if (nn->cap & NFP_NET_CFG_CTRL_RSS_ANY) {
3974                 nfp_net_rss_init(nn);
3975                 nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_RSS2 ?:
3976                                          NFP_NET_CFG_CTRL_RSS;
3977         }
3978
3979         /* Allow L2 Broadcast and Multicast through by default, if supported */
3980         if (nn->cap & NFP_NET_CFG_CTRL_L2BC)
3981                 nn->dp.ctrl |= NFP_NET_CFG_CTRL_L2BC;
3982
3983         /* Allow IRQ moderation, if supported */
3984         if (nn->cap & NFP_NET_CFG_CTRL_IRQMOD) {
3985                 nfp_net_irqmod_init(nn);
3986                 nn->dp.ctrl |= NFP_NET_CFG_CTRL_IRQMOD;
3987         }
3988
3989         /* Stash the re-configuration queue away.  First odd queue in TX Bar */
3990         nn->qcp_cfg = nn->tx_bar + NFP_QCP_QUEUE_ADDR_SZ;
3991
3992         /* Make sure the FW knows the netdev is supposed to be disabled here */
3993         nn_writel(nn, NFP_NET_CFG_CTRL, 0);
3994         nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, 0);
3995         nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, 0);
3996         err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_RING |
3997                                    NFP_NET_CFG_UPDATE_GEN);
3998         if (err)
3999                 return err;
4000
4001         if (nn->dp.netdev)
4002                 nfp_net_netdev_init(nn);
4003
4004         nfp_net_vecs_init(nn);
4005
4006         if (!nn->dp.netdev)
4007                 return 0;
4008         return register_netdev(nn->dp.netdev);
4009 }
4010
4011 /**
4012  * nfp_net_clean() - Undo what nfp_net_init() did.
4013  * @nn:         NFP Net device structure
4014  */
4015 void nfp_net_clean(struct nfp_net *nn)
4016 {
4017         if (!nn->dp.netdev)
4018                 return;
4019
4020         unregister_netdev(nn->dp.netdev);
4021         nfp_net_reconfig_wait_posted(nn);
4022 }