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