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