xref: /linux/drivers/net/ethernet/netronome/nfp/nfp_net_common.c (revision cad4977344b35ea116ec5fefe91a76b1dfa113f5)
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 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 	skb_tx_timestamp(skb);
894 
895 	tx_ring->wr_p += nr_frags + 1;
896 	if (nfp_net_tx_ring_should_stop(tx_ring))
897 		nfp_net_tx_ring_stop(nd_q, tx_ring);
898 
899 	tx_ring->wr_ptr_add += nr_frags + 1;
900 	if (__netdev_tx_sent_queue(nd_q, txbuf->real_len, skb->xmit_more))
901 		nfp_net_tx_xmit_more_flush(tx_ring);
902 
903 	return NETDEV_TX_OK;
904 
905 err_unmap:
906 	while (--f >= 0) {
907 		frag = &skb_shinfo(skb)->frags[f];
908 		dma_unmap_page(dp->dev, tx_ring->txbufs[wr_idx].dma_addr,
909 			       skb_frag_size(frag), DMA_TO_DEVICE);
910 		tx_ring->txbufs[wr_idx].skb = NULL;
911 		tx_ring->txbufs[wr_idx].dma_addr = 0;
912 		tx_ring->txbufs[wr_idx].fidx = -2;
913 		wr_idx = wr_idx - 1;
914 		if (wr_idx < 0)
915 			wr_idx += tx_ring->cnt;
916 	}
917 	dma_unmap_single(dp->dev, tx_ring->txbufs[wr_idx].dma_addr,
918 			 skb_headlen(skb), DMA_TO_DEVICE);
919 	tx_ring->txbufs[wr_idx].skb = NULL;
920 	tx_ring->txbufs[wr_idx].dma_addr = 0;
921 	tx_ring->txbufs[wr_idx].fidx = -2;
922 err_free:
923 	nn_dp_warn(dp, "Failed to map DMA TX buffer\n");
924 	nfp_net_tx_xmit_more_flush(tx_ring);
925 	u64_stats_update_begin(&r_vec->tx_sync);
926 	r_vec->tx_errors++;
927 	u64_stats_update_end(&r_vec->tx_sync);
928 	dev_kfree_skb_any(skb);
929 	return NETDEV_TX_OK;
930 }
931 
932 /**
933  * nfp_net_tx_complete() - Handled completed TX packets
934  * @tx_ring:	TX ring structure
935  * @budget:	NAPI budget (only used as bool to determine if in NAPI context)
936  */
937 static void nfp_net_tx_complete(struct nfp_net_tx_ring *tx_ring, int budget)
938 {
939 	struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
940 	struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
941 	const struct skb_frag_struct *frag;
942 	struct netdev_queue *nd_q;
943 	u32 done_pkts = 0, done_bytes = 0;
944 	struct sk_buff *skb;
945 	int todo, nr_frags;
946 	u32 qcp_rd_p;
947 	int fidx;
948 	int idx;
949 
950 	if (tx_ring->wr_p == tx_ring->rd_p)
951 		return;
952 
953 	/* Work out how many descriptors have been transmitted */
954 	qcp_rd_p = nfp_qcp_rd_ptr_read(tx_ring->qcp_q);
955 
956 	if (qcp_rd_p == tx_ring->qcp_rd_p)
957 		return;
958 
959 	todo = D_IDX(tx_ring, qcp_rd_p - tx_ring->qcp_rd_p);
960 
961 	while (todo--) {
962 		idx = D_IDX(tx_ring, tx_ring->rd_p++);
963 
964 		skb = tx_ring->txbufs[idx].skb;
965 		if (!skb)
966 			continue;
967 
968 		nr_frags = skb_shinfo(skb)->nr_frags;
969 		fidx = tx_ring->txbufs[idx].fidx;
970 
971 		if (fidx == -1) {
972 			/* unmap head */
973 			dma_unmap_single(dp->dev, tx_ring->txbufs[idx].dma_addr,
974 					 skb_headlen(skb), DMA_TO_DEVICE);
975 
976 			done_pkts += tx_ring->txbufs[idx].pkt_cnt;
977 			done_bytes += tx_ring->txbufs[idx].real_len;
978 		} else {
979 			/* unmap fragment */
980 			frag = &skb_shinfo(skb)->frags[fidx];
981 			dma_unmap_page(dp->dev, tx_ring->txbufs[idx].dma_addr,
982 				       skb_frag_size(frag), DMA_TO_DEVICE);
983 		}
984 
985 		/* check for last gather fragment */
986 		if (fidx == nr_frags - 1)
987 			napi_consume_skb(skb, budget);
988 
989 		tx_ring->txbufs[idx].dma_addr = 0;
990 		tx_ring->txbufs[idx].skb = NULL;
991 		tx_ring->txbufs[idx].fidx = -2;
992 	}
993 
994 	tx_ring->qcp_rd_p = qcp_rd_p;
995 
996 	u64_stats_update_begin(&r_vec->tx_sync);
997 	r_vec->tx_bytes += done_bytes;
998 	r_vec->tx_pkts += done_pkts;
999 	u64_stats_update_end(&r_vec->tx_sync);
1000 
1001 	if (!dp->netdev)
1002 		return;
1003 
1004 	nd_q = netdev_get_tx_queue(dp->netdev, tx_ring->idx);
1005 	netdev_tx_completed_queue(nd_q, done_pkts, done_bytes);
1006 	if (nfp_net_tx_ring_should_wake(tx_ring)) {
1007 		/* Make sure TX thread will see updated tx_ring->rd_p */
1008 		smp_mb();
1009 
1010 		if (unlikely(netif_tx_queue_stopped(nd_q)))
1011 			netif_tx_wake_queue(nd_q);
1012 	}
1013 
1014 	WARN_ONCE(tx_ring->wr_p - tx_ring->rd_p > tx_ring->cnt,
1015 		  "TX ring corruption rd_p=%u wr_p=%u cnt=%u\n",
1016 		  tx_ring->rd_p, tx_ring->wr_p, tx_ring->cnt);
1017 }
1018 
1019 static bool nfp_net_xdp_complete(struct nfp_net_tx_ring *tx_ring)
1020 {
1021 	struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
1022 	u32 done_pkts = 0, done_bytes = 0;
1023 	bool done_all;
1024 	int idx, todo;
1025 	u32 qcp_rd_p;
1026 
1027 	/* Work out how many descriptors have been transmitted */
1028 	qcp_rd_p = nfp_qcp_rd_ptr_read(tx_ring->qcp_q);
1029 
1030 	if (qcp_rd_p == tx_ring->qcp_rd_p)
1031 		return true;
1032 
1033 	todo = D_IDX(tx_ring, qcp_rd_p - tx_ring->qcp_rd_p);
1034 
1035 	done_all = todo <= NFP_NET_XDP_MAX_COMPLETE;
1036 	todo = min(todo, NFP_NET_XDP_MAX_COMPLETE);
1037 
1038 	tx_ring->qcp_rd_p = D_IDX(tx_ring, tx_ring->qcp_rd_p + todo);
1039 
1040 	done_pkts = todo;
1041 	while (todo--) {
1042 		idx = D_IDX(tx_ring, tx_ring->rd_p);
1043 		tx_ring->rd_p++;
1044 
1045 		done_bytes += tx_ring->txbufs[idx].real_len;
1046 	}
1047 
1048 	u64_stats_update_begin(&r_vec->tx_sync);
1049 	r_vec->tx_bytes += done_bytes;
1050 	r_vec->tx_pkts += done_pkts;
1051 	u64_stats_update_end(&r_vec->tx_sync);
1052 
1053 	WARN_ONCE(tx_ring->wr_p - tx_ring->rd_p > tx_ring->cnt,
1054 		  "XDP TX ring corruption rd_p=%u wr_p=%u cnt=%u\n",
1055 		  tx_ring->rd_p, tx_ring->wr_p, tx_ring->cnt);
1056 
1057 	return done_all;
1058 }
1059 
1060 /**
1061  * nfp_net_tx_ring_reset() - Free any untransmitted buffers and reset pointers
1062  * @dp:		NFP Net data path struct
1063  * @tx_ring:	TX ring structure
1064  *
1065  * Assumes that the device is stopped, must be idempotent.
1066  */
1067 static void
1068 nfp_net_tx_ring_reset(struct nfp_net_dp *dp, struct nfp_net_tx_ring *tx_ring)
1069 {
1070 	const struct skb_frag_struct *frag;
1071 	struct netdev_queue *nd_q;
1072 
1073 	while (!tx_ring->is_xdp && tx_ring->rd_p != tx_ring->wr_p) {
1074 		struct nfp_net_tx_buf *tx_buf;
1075 		struct sk_buff *skb;
1076 		int idx, nr_frags;
1077 
1078 		idx = D_IDX(tx_ring, tx_ring->rd_p);
1079 		tx_buf = &tx_ring->txbufs[idx];
1080 
1081 		skb = tx_ring->txbufs[idx].skb;
1082 		nr_frags = skb_shinfo(skb)->nr_frags;
1083 
1084 		if (tx_buf->fidx == -1) {
1085 			/* unmap head */
1086 			dma_unmap_single(dp->dev, tx_buf->dma_addr,
1087 					 skb_headlen(skb), DMA_TO_DEVICE);
1088 		} else {
1089 			/* unmap fragment */
1090 			frag = &skb_shinfo(skb)->frags[tx_buf->fidx];
1091 			dma_unmap_page(dp->dev, tx_buf->dma_addr,
1092 				       skb_frag_size(frag), DMA_TO_DEVICE);
1093 		}
1094 
1095 		/* check for last gather fragment */
1096 		if (tx_buf->fidx == nr_frags - 1)
1097 			dev_kfree_skb_any(skb);
1098 
1099 		tx_buf->dma_addr = 0;
1100 		tx_buf->skb = NULL;
1101 		tx_buf->fidx = -2;
1102 
1103 		tx_ring->qcp_rd_p++;
1104 		tx_ring->rd_p++;
1105 	}
1106 
1107 	memset(tx_ring->txds, 0, tx_ring->size);
1108 	tx_ring->wr_p = 0;
1109 	tx_ring->rd_p = 0;
1110 	tx_ring->qcp_rd_p = 0;
1111 	tx_ring->wr_ptr_add = 0;
1112 
1113 	if (tx_ring->is_xdp || !dp->netdev)
1114 		return;
1115 
1116 	nd_q = netdev_get_tx_queue(dp->netdev, tx_ring->idx);
1117 	netdev_tx_reset_queue(nd_q);
1118 }
1119 
1120 static void nfp_net_tx_timeout(struct net_device *netdev)
1121 {
1122 	struct nfp_net *nn = netdev_priv(netdev);
1123 	int i;
1124 
1125 	for (i = 0; i < nn->dp.netdev->real_num_tx_queues; i++) {
1126 		if (!netif_tx_queue_stopped(netdev_get_tx_queue(netdev, i)))
1127 			continue;
1128 		nn_warn(nn, "TX timeout on ring: %d\n", i);
1129 	}
1130 	nn_warn(nn, "TX watchdog timeout\n");
1131 }
1132 
1133 /* Receive processing
1134  */
1135 static unsigned int
1136 nfp_net_calc_fl_bufsz(struct nfp_net_dp *dp)
1137 {
1138 	unsigned int fl_bufsz;
1139 
1140 	fl_bufsz = NFP_NET_RX_BUF_HEADROOM;
1141 	fl_bufsz += dp->rx_dma_off;
1142 	if (dp->rx_offset == NFP_NET_CFG_RX_OFFSET_DYNAMIC)
1143 		fl_bufsz += NFP_NET_MAX_PREPEND;
1144 	else
1145 		fl_bufsz += dp->rx_offset;
1146 	fl_bufsz += ETH_HLEN + VLAN_HLEN * 2 + dp->mtu;
1147 
1148 	fl_bufsz = SKB_DATA_ALIGN(fl_bufsz);
1149 	fl_bufsz += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1150 
1151 	return fl_bufsz;
1152 }
1153 
1154 static void
1155 nfp_net_free_frag(void *frag, bool xdp)
1156 {
1157 	if (!xdp)
1158 		skb_free_frag(frag);
1159 	else
1160 		__free_page(virt_to_page(frag));
1161 }
1162 
1163 /**
1164  * nfp_net_rx_alloc_one() - Allocate and map page frag for RX
1165  * @dp:		NFP Net data path struct
1166  * @dma_addr:	Pointer to storage for DMA address (output param)
1167  *
1168  * This function will allcate a new page frag, map it for DMA.
1169  *
1170  * Return: allocated page frag or NULL on failure.
1171  */
1172 static void *nfp_net_rx_alloc_one(struct nfp_net_dp *dp, dma_addr_t *dma_addr)
1173 {
1174 	void *frag;
1175 
1176 	if (!dp->xdp_prog) {
1177 		frag = netdev_alloc_frag(dp->fl_bufsz);
1178 	} else {
1179 		struct page *page;
1180 
1181 		page = alloc_page(GFP_KERNEL);
1182 		frag = page ? page_address(page) : NULL;
1183 	}
1184 	if (!frag) {
1185 		nn_dp_warn(dp, "Failed to alloc receive page frag\n");
1186 		return NULL;
1187 	}
1188 
1189 	*dma_addr = nfp_net_dma_map_rx(dp, frag);
1190 	if (dma_mapping_error(dp->dev, *dma_addr)) {
1191 		nfp_net_free_frag(frag, dp->xdp_prog);
1192 		nn_dp_warn(dp, "Failed to map DMA RX buffer\n");
1193 		return NULL;
1194 	}
1195 
1196 	return frag;
1197 }
1198 
1199 static void *nfp_net_napi_alloc_one(struct nfp_net_dp *dp, dma_addr_t *dma_addr)
1200 {
1201 	void *frag;
1202 
1203 	if (!dp->xdp_prog) {
1204 		frag = napi_alloc_frag(dp->fl_bufsz);
1205 		if (unlikely(!frag))
1206 			return NULL;
1207 	} else {
1208 		struct page *page;
1209 
1210 		page = dev_alloc_page();
1211 		if (unlikely(!page))
1212 			return NULL;
1213 		frag = page_address(page);
1214 	}
1215 
1216 	*dma_addr = nfp_net_dma_map_rx(dp, frag);
1217 	if (dma_mapping_error(dp->dev, *dma_addr)) {
1218 		nfp_net_free_frag(frag, dp->xdp_prog);
1219 		nn_dp_warn(dp, "Failed to map DMA RX buffer\n");
1220 		return NULL;
1221 	}
1222 
1223 	return frag;
1224 }
1225 
1226 /**
1227  * nfp_net_rx_give_one() - Put mapped skb on the software and hardware rings
1228  * @dp:		NFP Net data path struct
1229  * @rx_ring:	RX ring structure
1230  * @frag:	page fragment buffer
1231  * @dma_addr:	DMA address of skb mapping
1232  */
1233 static void nfp_net_rx_give_one(const struct nfp_net_dp *dp,
1234 				struct nfp_net_rx_ring *rx_ring,
1235 				void *frag, dma_addr_t dma_addr)
1236 {
1237 	unsigned int wr_idx;
1238 
1239 	wr_idx = D_IDX(rx_ring, rx_ring->wr_p);
1240 
1241 	nfp_net_dma_sync_dev_rx(dp, dma_addr);
1242 
1243 	/* Stash SKB and DMA address away */
1244 	rx_ring->rxbufs[wr_idx].frag = frag;
1245 	rx_ring->rxbufs[wr_idx].dma_addr = dma_addr;
1246 
1247 	/* Fill freelist descriptor */
1248 	rx_ring->rxds[wr_idx].fld.reserved = 0;
1249 	rx_ring->rxds[wr_idx].fld.meta_len_dd = 0;
1250 	nfp_desc_set_dma_addr(&rx_ring->rxds[wr_idx].fld,
1251 			      dma_addr + dp->rx_dma_off);
1252 
1253 	rx_ring->wr_p++;
1254 	if (!(rx_ring->wr_p % NFP_NET_FL_BATCH)) {
1255 		/* Update write pointer of the freelist queue. Make
1256 		 * sure all writes are flushed before telling the hardware.
1257 		 */
1258 		wmb();
1259 		nfp_qcp_wr_ptr_add(rx_ring->qcp_fl, NFP_NET_FL_BATCH);
1260 	}
1261 }
1262 
1263 /**
1264  * nfp_net_rx_ring_reset() - Reflect in SW state of freelist after disable
1265  * @rx_ring:	RX ring structure
1266  *
1267  * Assumes that the device is stopped, must be idempotent.
1268  */
1269 static void nfp_net_rx_ring_reset(struct nfp_net_rx_ring *rx_ring)
1270 {
1271 	unsigned int wr_idx, last_idx;
1272 
1273 	/* wr_p == rd_p means ring was never fed FL bufs.  RX rings are always
1274 	 * kept at cnt - 1 FL bufs.
1275 	 */
1276 	if (rx_ring->wr_p == 0 && rx_ring->rd_p == 0)
1277 		return;
1278 
1279 	/* Move the empty entry to the end of the list */
1280 	wr_idx = D_IDX(rx_ring, rx_ring->wr_p);
1281 	last_idx = rx_ring->cnt - 1;
1282 	rx_ring->rxbufs[wr_idx].dma_addr = rx_ring->rxbufs[last_idx].dma_addr;
1283 	rx_ring->rxbufs[wr_idx].frag = rx_ring->rxbufs[last_idx].frag;
1284 	rx_ring->rxbufs[last_idx].dma_addr = 0;
1285 	rx_ring->rxbufs[last_idx].frag = NULL;
1286 
1287 	memset(rx_ring->rxds, 0, rx_ring->size);
1288 	rx_ring->wr_p = 0;
1289 	rx_ring->rd_p = 0;
1290 }
1291 
1292 /**
1293  * nfp_net_rx_ring_bufs_free() - Free any buffers currently on the RX ring
1294  * @dp:		NFP Net data path struct
1295  * @rx_ring:	RX ring to remove buffers from
1296  *
1297  * Assumes that the device is stopped and buffers are in [0, ring->cnt - 1)
1298  * entries.  After device is disabled nfp_net_rx_ring_reset() must be called
1299  * to restore required ring geometry.
1300  */
1301 static void
1302 nfp_net_rx_ring_bufs_free(struct nfp_net_dp *dp,
1303 			  struct nfp_net_rx_ring *rx_ring)
1304 {
1305 	unsigned int i;
1306 
1307 	for (i = 0; i < rx_ring->cnt - 1; i++) {
1308 		/* NULL skb can only happen when initial filling of the ring
1309 		 * fails to allocate enough buffers and calls here to free
1310 		 * already allocated ones.
1311 		 */
1312 		if (!rx_ring->rxbufs[i].frag)
1313 			continue;
1314 
1315 		nfp_net_dma_unmap_rx(dp, rx_ring->rxbufs[i].dma_addr);
1316 		nfp_net_free_frag(rx_ring->rxbufs[i].frag, dp->xdp_prog);
1317 		rx_ring->rxbufs[i].dma_addr = 0;
1318 		rx_ring->rxbufs[i].frag = NULL;
1319 	}
1320 }
1321 
1322 /**
1323  * nfp_net_rx_ring_bufs_alloc() - Fill RX ring with buffers (don't give to FW)
1324  * @dp:		NFP Net data path struct
1325  * @rx_ring:	RX ring to remove buffers from
1326  */
1327 static int
1328 nfp_net_rx_ring_bufs_alloc(struct nfp_net_dp *dp,
1329 			   struct nfp_net_rx_ring *rx_ring)
1330 {
1331 	struct nfp_net_rx_buf *rxbufs;
1332 	unsigned int i;
1333 
1334 	rxbufs = rx_ring->rxbufs;
1335 
1336 	for (i = 0; i < rx_ring->cnt - 1; i++) {
1337 		rxbufs[i].frag = nfp_net_rx_alloc_one(dp, &rxbufs[i].dma_addr);
1338 		if (!rxbufs[i].frag) {
1339 			nfp_net_rx_ring_bufs_free(dp, rx_ring);
1340 			return -ENOMEM;
1341 		}
1342 	}
1343 
1344 	return 0;
1345 }
1346 
1347 /**
1348  * nfp_net_rx_ring_fill_freelist() - Give buffers from the ring to FW
1349  * @dp:	     NFP Net data path struct
1350  * @rx_ring: RX ring to fill
1351  */
1352 static void
1353 nfp_net_rx_ring_fill_freelist(struct nfp_net_dp *dp,
1354 			      struct nfp_net_rx_ring *rx_ring)
1355 {
1356 	unsigned int i;
1357 
1358 	for (i = 0; i < rx_ring->cnt - 1; i++)
1359 		nfp_net_rx_give_one(dp, rx_ring, rx_ring->rxbufs[i].frag,
1360 				    rx_ring->rxbufs[i].dma_addr);
1361 }
1362 
1363 /**
1364  * nfp_net_rx_csum_has_errors() - group check if rxd has any csum errors
1365  * @flags: RX descriptor flags field in CPU byte order
1366  */
1367 static int nfp_net_rx_csum_has_errors(u16 flags)
1368 {
1369 	u16 csum_all_checked, csum_all_ok;
1370 
1371 	csum_all_checked = flags & __PCIE_DESC_RX_CSUM_ALL;
1372 	csum_all_ok = flags & __PCIE_DESC_RX_CSUM_ALL_OK;
1373 
1374 	return csum_all_checked != (csum_all_ok << PCIE_DESC_RX_CSUM_OK_SHIFT);
1375 }
1376 
1377 /**
1378  * nfp_net_rx_csum() - set SKB checksum field based on RX descriptor flags
1379  * @dp:  NFP Net data path struct
1380  * @r_vec: per-ring structure
1381  * @rxd: Pointer to RX descriptor
1382  * @meta: Parsed metadata prepend
1383  * @skb: Pointer to SKB
1384  */
1385 static void nfp_net_rx_csum(struct nfp_net_dp *dp,
1386 			    struct nfp_net_r_vector *r_vec,
1387 			    struct nfp_net_rx_desc *rxd,
1388 			    struct nfp_meta_parsed *meta, struct sk_buff *skb)
1389 {
1390 	skb_checksum_none_assert(skb);
1391 
1392 	if (!(dp->netdev->features & NETIF_F_RXCSUM))
1393 		return;
1394 
1395 	if (meta->csum_type) {
1396 		skb->ip_summed = meta->csum_type;
1397 		skb->csum = meta->csum;
1398 		u64_stats_update_begin(&r_vec->rx_sync);
1399 		r_vec->hw_csum_rx_complete++;
1400 		u64_stats_update_end(&r_vec->rx_sync);
1401 		return;
1402 	}
1403 
1404 	if (nfp_net_rx_csum_has_errors(le16_to_cpu(rxd->rxd.flags))) {
1405 		u64_stats_update_begin(&r_vec->rx_sync);
1406 		r_vec->hw_csum_rx_error++;
1407 		u64_stats_update_end(&r_vec->rx_sync);
1408 		return;
1409 	}
1410 
1411 	/* Assume that the firmware will never report inner CSUM_OK unless outer
1412 	 * L4 headers were successfully parsed. FW will always report zero UDP
1413 	 * checksum as CSUM_OK.
1414 	 */
1415 	if (rxd->rxd.flags & PCIE_DESC_RX_TCP_CSUM_OK ||
1416 	    rxd->rxd.flags & PCIE_DESC_RX_UDP_CSUM_OK) {
1417 		__skb_incr_checksum_unnecessary(skb);
1418 		u64_stats_update_begin(&r_vec->rx_sync);
1419 		r_vec->hw_csum_rx_ok++;
1420 		u64_stats_update_end(&r_vec->rx_sync);
1421 	}
1422 
1423 	if (rxd->rxd.flags & PCIE_DESC_RX_I_TCP_CSUM_OK ||
1424 	    rxd->rxd.flags & PCIE_DESC_RX_I_UDP_CSUM_OK) {
1425 		__skb_incr_checksum_unnecessary(skb);
1426 		u64_stats_update_begin(&r_vec->rx_sync);
1427 		r_vec->hw_csum_rx_inner_ok++;
1428 		u64_stats_update_end(&r_vec->rx_sync);
1429 	}
1430 }
1431 
1432 static void
1433 nfp_net_set_hash(struct net_device *netdev, struct nfp_meta_parsed *meta,
1434 		 unsigned int type, __be32 *hash)
1435 {
1436 	if (!(netdev->features & NETIF_F_RXHASH))
1437 		return;
1438 
1439 	switch (type) {
1440 	case NFP_NET_RSS_IPV4:
1441 	case NFP_NET_RSS_IPV6:
1442 	case NFP_NET_RSS_IPV6_EX:
1443 		meta->hash_type = PKT_HASH_TYPE_L3;
1444 		break;
1445 	default:
1446 		meta->hash_type = PKT_HASH_TYPE_L4;
1447 		break;
1448 	}
1449 
1450 	meta->hash = get_unaligned_be32(hash);
1451 }
1452 
1453 static void
1454 nfp_net_set_hash_desc(struct net_device *netdev, struct nfp_meta_parsed *meta,
1455 		      void *data, struct nfp_net_rx_desc *rxd)
1456 {
1457 	struct nfp_net_rx_hash *rx_hash = data;
1458 
1459 	if (!(rxd->rxd.flags & PCIE_DESC_RX_RSS))
1460 		return;
1461 
1462 	nfp_net_set_hash(netdev, meta, get_unaligned_be32(&rx_hash->hash_type),
1463 			 &rx_hash->hash);
1464 }
1465 
1466 static void *
1467 nfp_net_parse_meta(struct net_device *netdev, struct nfp_meta_parsed *meta,
1468 		   void *data, int meta_len)
1469 {
1470 	u32 meta_info;
1471 
1472 	meta_info = get_unaligned_be32(data);
1473 	data += 4;
1474 
1475 	while (meta_info) {
1476 		switch (meta_info & NFP_NET_META_FIELD_MASK) {
1477 		case NFP_NET_META_HASH:
1478 			meta_info >>= NFP_NET_META_FIELD_SIZE;
1479 			nfp_net_set_hash(netdev, meta,
1480 					 meta_info & NFP_NET_META_FIELD_MASK,
1481 					 (__be32 *)data);
1482 			data += 4;
1483 			break;
1484 		case NFP_NET_META_MARK:
1485 			meta->mark = get_unaligned_be32(data);
1486 			data += 4;
1487 			break;
1488 		case NFP_NET_META_PORTID:
1489 			meta->portid = get_unaligned_be32(data);
1490 			data += 4;
1491 			break;
1492 		case NFP_NET_META_CSUM:
1493 			meta->csum_type = CHECKSUM_COMPLETE;
1494 			meta->csum =
1495 				(__force __wsum)__get_unaligned_cpu32(data);
1496 			data += 4;
1497 			break;
1498 		default:
1499 			return NULL;
1500 		}
1501 
1502 		meta_info >>= NFP_NET_META_FIELD_SIZE;
1503 	}
1504 
1505 	return data;
1506 }
1507 
1508 static void
1509 nfp_net_rx_drop(const struct nfp_net_dp *dp, struct nfp_net_r_vector *r_vec,
1510 		struct nfp_net_rx_ring *rx_ring, struct nfp_net_rx_buf *rxbuf,
1511 		struct sk_buff *skb)
1512 {
1513 	u64_stats_update_begin(&r_vec->rx_sync);
1514 	r_vec->rx_drops++;
1515 	/* If we have both skb and rxbuf the replacement buffer allocation
1516 	 * must have failed, count this as an alloc failure.
1517 	 */
1518 	if (skb && rxbuf)
1519 		r_vec->rx_replace_buf_alloc_fail++;
1520 	u64_stats_update_end(&r_vec->rx_sync);
1521 
1522 	/* skb is build based on the frag, free_skb() would free the frag
1523 	 * so to be able to reuse it we need an extra ref.
1524 	 */
1525 	if (skb && rxbuf && skb->head == rxbuf->frag)
1526 		page_ref_inc(virt_to_head_page(rxbuf->frag));
1527 	if (rxbuf)
1528 		nfp_net_rx_give_one(dp, rx_ring, rxbuf->frag, rxbuf->dma_addr);
1529 	if (skb)
1530 		dev_kfree_skb_any(skb);
1531 }
1532 
1533 static bool
1534 nfp_net_tx_xdp_buf(struct nfp_net_dp *dp, struct nfp_net_rx_ring *rx_ring,
1535 		   struct nfp_net_tx_ring *tx_ring,
1536 		   struct nfp_net_rx_buf *rxbuf, unsigned int dma_off,
1537 		   unsigned int pkt_len, bool *completed)
1538 {
1539 	struct nfp_net_tx_buf *txbuf;
1540 	struct nfp_net_tx_desc *txd;
1541 	int wr_idx;
1542 
1543 	if (unlikely(nfp_net_tx_full(tx_ring, 1))) {
1544 		if (!*completed) {
1545 			nfp_net_xdp_complete(tx_ring);
1546 			*completed = true;
1547 		}
1548 
1549 		if (unlikely(nfp_net_tx_full(tx_ring, 1))) {
1550 			nfp_net_rx_drop(dp, rx_ring->r_vec, rx_ring, rxbuf,
1551 					NULL);
1552 			return false;
1553 		}
1554 	}
1555 
1556 	wr_idx = D_IDX(tx_ring, tx_ring->wr_p);
1557 
1558 	/* Stash the soft descriptor of the head then initialize it */
1559 	txbuf = &tx_ring->txbufs[wr_idx];
1560 
1561 	nfp_net_rx_give_one(dp, rx_ring, txbuf->frag, txbuf->dma_addr);
1562 
1563 	txbuf->frag = rxbuf->frag;
1564 	txbuf->dma_addr = rxbuf->dma_addr;
1565 	txbuf->fidx = -1;
1566 	txbuf->pkt_cnt = 1;
1567 	txbuf->real_len = pkt_len;
1568 
1569 	dma_sync_single_for_device(dp->dev, rxbuf->dma_addr + dma_off,
1570 				   pkt_len, DMA_BIDIRECTIONAL);
1571 
1572 	/* Build TX descriptor */
1573 	txd = &tx_ring->txds[wr_idx];
1574 	txd->offset_eop = PCIE_DESC_TX_EOP;
1575 	txd->dma_len = cpu_to_le16(pkt_len);
1576 	nfp_desc_set_dma_addr(txd, rxbuf->dma_addr + dma_off);
1577 	txd->data_len = cpu_to_le16(pkt_len);
1578 
1579 	txd->flags = 0;
1580 	txd->mss = 0;
1581 	txd->lso_hdrlen = 0;
1582 
1583 	tx_ring->wr_p++;
1584 	tx_ring->wr_ptr_add++;
1585 	return true;
1586 }
1587 
1588 /**
1589  * nfp_net_rx() - receive up to @budget packets on @rx_ring
1590  * @rx_ring:   RX ring to receive from
1591  * @budget:    NAPI budget
1592  *
1593  * Note, this function is separated out from the napi poll function to
1594  * more cleanly separate packet receive code from other bookkeeping
1595  * functions performed in the napi poll function.
1596  *
1597  * Return: Number of packets received.
1598  */
1599 static int nfp_net_rx(struct nfp_net_rx_ring *rx_ring, int budget)
1600 {
1601 	struct nfp_net_r_vector *r_vec = rx_ring->r_vec;
1602 	struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
1603 	struct nfp_net_tx_ring *tx_ring;
1604 	struct bpf_prog *xdp_prog;
1605 	bool xdp_tx_cmpl = false;
1606 	unsigned int true_bufsz;
1607 	struct sk_buff *skb;
1608 	int pkts_polled = 0;
1609 	struct xdp_buff xdp;
1610 	int idx;
1611 
1612 	rcu_read_lock();
1613 	xdp_prog = READ_ONCE(dp->xdp_prog);
1614 	true_bufsz = xdp_prog ? PAGE_SIZE : dp->fl_bufsz;
1615 	xdp.rxq = &rx_ring->xdp_rxq;
1616 	tx_ring = r_vec->xdp_ring;
1617 
1618 	while (pkts_polled < budget) {
1619 		unsigned int meta_len, data_len, meta_off, pkt_len, pkt_off;
1620 		struct nfp_net_rx_buf *rxbuf;
1621 		struct nfp_net_rx_desc *rxd;
1622 		struct nfp_meta_parsed meta;
1623 		struct net_device *netdev;
1624 		dma_addr_t new_dma_addr;
1625 		u32 meta_len_xdp = 0;
1626 		void *new_frag;
1627 
1628 		idx = D_IDX(rx_ring, rx_ring->rd_p);
1629 
1630 		rxd = &rx_ring->rxds[idx];
1631 		if (!(rxd->rxd.meta_len_dd & PCIE_DESC_RX_DD))
1632 			break;
1633 
1634 		/* Memory barrier to ensure that we won't do other reads
1635 		 * before the DD bit.
1636 		 */
1637 		dma_rmb();
1638 
1639 		memset(&meta, 0, sizeof(meta));
1640 
1641 		rx_ring->rd_p++;
1642 		pkts_polled++;
1643 
1644 		rxbuf =	&rx_ring->rxbufs[idx];
1645 		/*         < meta_len >
1646 		 *  <-- [rx_offset] -->
1647 		 *  ---------------------------------------------------------
1648 		 * | [XX] |  metadata  |             packet           | XXXX |
1649 		 *  ---------------------------------------------------------
1650 		 *         <---------------- data_len --------------->
1651 		 *
1652 		 * The rx_offset is fixed for all packets, the meta_len can vary
1653 		 * on a packet by packet basis. If rx_offset is set to zero
1654 		 * (_RX_OFFSET_DYNAMIC) metadata starts at the beginning of the
1655 		 * buffer and is immediately followed by the packet (no [XX]).
1656 		 */
1657 		meta_len = rxd->rxd.meta_len_dd & PCIE_DESC_RX_META_LEN_MASK;
1658 		data_len = le16_to_cpu(rxd->rxd.data_len);
1659 		pkt_len = data_len - meta_len;
1660 
1661 		pkt_off = NFP_NET_RX_BUF_HEADROOM + dp->rx_dma_off;
1662 		if (dp->rx_offset == NFP_NET_CFG_RX_OFFSET_DYNAMIC)
1663 			pkt_off += meta_len;
1664 		else
1665 			pkt_off += dp->rx_offset;
1666 		meta_off = pkt_off - meta_len;
1667 
1668 		/* Stats update */
1669 		u64_stats_update_begin(&r_vec->rx_sync);
1670 		r_vec->rx_pkts++;
1671 		r_vec->rx_bytes += pkt_len;
1672 		u64_stats_update_end(&r_vec->rx_sync);
1673 
1674 		if (unlikely(meta_len > NFP_NET_MAX_PREPEND ||
1675 			     (dp->rx_offset && meta_len > dp->rx_offset))) {
1676 			nn_dp_warn(dp, "oversized RX packet metadata %u\n",
1677 				   meta_len);
1678 			nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
1679 			continue;
1680 		}
1681 
1682 		nfp_net_dma_sync_cpu_rx(dp, rxbuf->dma_addr + meta_off,
1683 					data_len);
1684 
1685 		if (!dp->chained_metadata_format) {
1686 			nfp_net_set_hash_desc(dp->netdev, &meta,
1687 					      rxbuf->frag + meta_off, rxd);
1688 		} else if (meta_len) {
1689 			void *end;
1690 
1691 			end = nfp_net_parse_meta(dp->netdev, &meta,
1692 						 rxbuf->frag + meta_off,
1693 						 meta_len);
1694 			if (unlikely(end != rxbuf->frag + pkt_off)) {
1695 				nn_dp_warn(dp, "invalid RX packet metadata\n");
1696 				nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf,
1697 						NULL);
1698 				continue;
1699 			}
1700 		}
1701 
1702 		if (xdp_prog && !meta.portid) {
1703 			void *orig_data = rxbuf->frag + pkt_off;
1704 			unsigned int dma_off;
1705 			int act;
1706 
1707 			xdp.data_hard_start = rxbuf->frag + NFP_NET_RX_BUF_HEADROOM;
1708 			xdp.data = orig_data;
1709 			xdp.data_meta = orig_data;
1710 			xdp.data_end = orig_data + pkt_len;
1711 
1712 			act = bpf_prog_run_xdp(xdp_prog, &xdp);
1713 
1714 			pkt_len = xdp.data_end - xdp.data;
1715 			pkt_off += xdp.data - orig_data;
1716 
1717 			switch (act) {
1718 			case XDP_PASS:
1719 				meta_len_xdp = xdp.data - xdp.data_meta;
1720 				break;
1721 			case XDP_TX:
1722 				dma_off = pkt_off - NFP_NET_RX_BUF_HEADROOM;
1723 				if (unlikely(!nfp_net_tx_xdp_buf(dp, rx_ring,
1724 								 tx_ring, rxbuf,
1725 								 dma_off,
1726 								 pkt_len,
1727 								 &xdp_tx_cmpl)))
1728 					trace_xdp_exception(dp->netdev,
1729 							    xdp_prog, act);
1730 				continue;
1731 			default:
1732 				bpf_warn_invalid_xdp_action(act);
1733 				/* fall through */
1734 			case XDP_ABORTED:
1735 				trace_xdp_exception(dp->netdev, xdp_prog, act);
1736 				/* fall through */
1737 			case XDP_DROP:
1738 				nfp_net_rx_give_one(dp, rx_ring, rxbuf->frag,
1739 						    rxbuf->dma_addr);
1740 				continue;
1741 			}
1742 		}
1743 
1744 		if (likely(!meta.portid)) {
1745 			netdev = dp->netdev;
1746 		} else if (meta.portid == NFP_META_PORT_ID_CTRL) {
1747 			struct nfp_net *nn = netdev_priv(dp->netdev);
1748 
1749 			nfp_app_ctrl_rx_raw(nn->app, rxbuf->frag + pkt_off,
1750 					    pkt_len);
1751 			nfp_net_rx_give_one(dp, rx_ring, rxbuf->frag,
1752 					    rxbuf->dma_addr);
1753 			continue;
1754 		} else {
1755 			struct nfp_net *nn;
1756 
1757 			nn = netdev_priv(dp->netdev);
1758 			netdev = nfp_app_repr_get(nn->app, meta.portid);
1759 			if (unlikely(!netdev)) {
1760 				nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf,
1761 						NULL);
1762 				continue;
1763 			}
1764 			nfp_repr_inc_rx_stats(netdev, pkt_len);
1765 		}
1766 
1767 		skb = build_skb(rxbuf->frag, true_bufsz);
1768 		if (unlikely(!skb)) {
1769 			nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
1770 			continue;
1771 		}
1772 		new_frag = nfp_net_napi_alloc_one(dp, &new_dma_addr);
1773 		if (unlikely(!new_frag)) {
1774 			nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, skb);
1775 			continue;
1776 		}
1777 
1778 		nfp_net_dma_unmap_rx(dp, rxbuf->dma_addr);
1779 
1780 		nfp_net_rx_give_one(dp, rx_ring, new_frag, new_dma_addr);
1781 
1782 		skb_reserve(skb, pkt_off);
1783 		skb_put(skb, pkt_len);
1784 
1785 		skb->mark = meta.mark;
1786 		skb_set_hash(skb, meta.hash, meta.hash_type);
1787 
1788 		skb_record_rx_queue(skb, rx_ring->idx);
1789 		skb->protocol = eth_type_trans(skb, netdev);
1790 
1791 		nfp_net_rx_csum(dp, r_vec, rxd, &meta, skb);
1792 
1793 		if (rxd->rxd.flags & PCIE_DESC_RX_VLAN)
1794 			__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1795 					       le16_to_cpu(rxd->rxd.vlan));
1796 		if (meta_len_xdp)
1797 			skb_metadata_set(skb, meta_len_xdp);
1798 
1799 		napi_gro_receive(&rx_ring->r_vec->napi, skb);
1800 	}
1801 
1802 	if (xdp_prog) {
1803 		if (tx_ring->wr_ptr_add)
1804 			nfp_net_tx_xmit_more_flush(tx_ring);
1805 		else if (unlikely(tx_ring->wr_p != tx_ring->rd_p) &&
1806 			 !xdp_tx_cmpl)
1807 			if (!nfp_net_xdp_complete(tx_ring))
1808 				pkts_polled = budget;
1809 	}
1810 	rcu_read_unlock();
1811 
1812 	return pkts_polled;
1813 }
1814 
1815 /**
1816  * nfp_net_poll() - napi poll function
1817  * @napi:    NAPI structure
1818  * @budget:  NAPI budget
1819  *
1820  * Return: number of packets polled.
1821  */
1822 static int nfp_net_poll(struct napi_struct *napi, int budget)
1823 {
1824 	struct nfp_net_r_vector *r_vec =
1825 		container_of(napi, struct nfp_net_r_vector, napi);
1826 	unsigned int pkts_polled = 0;
1827 
1828 	if (r_vec->tx_ring)
1829 		nfp_net_tx_complete(r_vec->tx_ring, budget);
1830 	if (r_vec->rx_ring)
1831 		pkts_polled = nfp_net_rx(r_vec->rx_ring, budget);
1832 
1833 	if (pkts_polled < budget)
1834 		if (napi_complete_done(napi, pkts_polled))
1835 			nfp_net_irq_unmask(r_vec->nfp_net, r_vec->irq_entry);
1836 
1837 	return pkts_polled;
1838 }
1839 
1840 /* Control device data path
1841  */
1842 
1843 static bool
1844 nfp_ctrl_tx_one(struct nfp_net *nn, struct nfp_net_r_vector *r_vec,
1845 		struct sk_buff *skb, bool old)
1846 {
1847 	unsigned int real_len = skb->len, meta_len = 0;
1848 	struct nfp_net_tx_ring *tx_ring;
1849 	struct nfp_net_tx_buf *txbuf;
1850 	struct nfp_net_tx_desc *txd;
1851 	struct nfp_net_dp *dp;
1852 	dma_addr_t dma_addr;
1853 	int wr_idx;
1854 
1855 	dp = &r_vec->nfp_net->dp;
1856 	tx_ring = r_vec->tx_ring;
1857 
1858 	if (WARN_ON_ONCE(skb_shinfo(skb)->nr_frags)) {
1859 		nn_dp_warn(dp, "Driver's CTRL TX does not implement gather\n");
1860 		goto err_free;
1861 	}
1862 
1863 	if (unlikely(nfp_net_tx_full(tx_ring, 1))) {
1864 		u64_stats_update_begin(&r_vec->tx_sync);
1865 		r_vec->tx_busy++;
1866 		u64_stats_update_end(&r_vec->tx_sync);
1867 		if (!old)
1868 			__skb_queue_tail(&r_vec->queue, skb);
1869 		else
1870 			__skb_queue_head(&r_vec->queue, skb);
1871 		return true;
1872 	}
1873 
1874 	if (nfp_app_ctrl_has_meta(nn->app)) {
1875 		if (unlikely(skb_headroom(skb) < 8)) {
1876 			nn_dp_warn(dp, "CTRL TX on skb without headroom\n");
1877 			goto err_free;
1878 		}
1879 		meta_len = 8;
1880 		put_unaligned_be32(NFP_META_PORT_ID_CTRL, skb_push(skb, 4));
1881 		put_unaligned_be32(NFP_NET_META_PORTID, skb_push(skb, 4));
1882 	}
1883 
1884 	/* Start with the head skbuf */
1885 	dma_addr = dma_map_single(dp->dev, skb->data, skb_headlen(skb),
1886 				  DMA_TO_DEVICE);
1887 	if (dma_mapping_error(dp->dev, dma_addr))
1888 		goto err_dma_warn;
1889 
1890 	wr_idx = D_IDX(tx_ring, tx_ring->wr_p);
1891 
1892 	/* Stash the soft descriptor of the head then initialize it */
1893 	txbuf = &tx_ring->txbufs[wr_idx];
1894 	txbuf->skb = skb;
1895 	txbuf->dma_addr = dma_addr;
1896 	txbuf->fidx = -1;
1897 	txbuf->pkt_cnt = 1;
1898 	txbuf->real_len = real_len;
1899 
1900 	/* Build TX descriptor */
1901 	txd = &tx_ring->txds[wr_idx];
1902 	txd->offset_eop = meta_len | PCIE_DESC_TX_EOP;
1903 	txd->dma_len = cpu_to_le16(skb_headlen(skb));
1904 	nfp_desc_set_dma_addr(txd, dma_addr);
1905 	txd->data_len = cpu_to_le16(skb->len);
1906 
1907 	txd->flags = 0;
1908 	txd->mss = 0;
1909 	txd->lso_hdrlen = 0;
1910 
1911 	tx_ring->wr_p++;
1912 	tx_ring->wr_ptr_add++;
1913 	nfp_net_tx_xmit_more_flush(tx_ring);
1914 
1915 	return false;
1916 
1917 err_dma_warn:
1918 	nn_dp_warn(dp, "Failed to DMA map TX CTRL buffer\n");
1919 err_free:
1920 	u64_stats_update_begin(&r_vec->tx_sync);
1921 	r_vec->tx_errors++;
1922 	u64_stats_update_end(&r_vec->tx_sync);
1923 	dev_kfree_skb_any(skb);
1924 	return false;
1925 }
1926 
1927 bool __nfp_ctrl_tx(struct nfp_net *nn, struct sk_buff *skb)
1928 {
1929 	struct nfp_net_r_vector *r_vec = &nn->r_vecs[0];
1930 
1931 	return nfp_ctrl_tx_one(nn, r_vec, skb, false);
1932 }
1933 
1934 bool nfp_ctrl_tx(struct nfp_net *nn, struct sk_buff *skb)
1935 {
1936 	struct nfp_net_r_vector *r_vec = &nn->r_vecs[0];
1937 	bool ret;
1938 
1939 	spin_lock_bh(&r_vec->lock);
1940 	ret = nfp_ctrl_tx_one(nn, r_vec, skb, false);
1941 	spin_unlock_bh(&r_vec->lock);
1942 
1943 	return ret;
1944 }
1945 
1946 static void __nfp_ctrl_tx_queued(struct nfp_net_r_vector *r_vec)
1947 {
1948 	struct sk_buff *skb;
1949 
1950 	while ((skb = __skb_dequeue(&r_vec->queue)))
1951 		if (nfp_ctrl_tx_one(r_vec->nfp_net, r_vec, skb, true))
1952 			return;
1953 }
1954 
1955 static bool
1956 nfp_ctrl_meta_ok(struct nfp_net *nn, void *data, unsigned int meta_len)
1957 {
1958 	u32 meta_type, meta_tag;
1959 
1960 	if (!nfp_app_ctrl_has_meta(nn->app))
1961 		return !meta_len;
1962 
1963 	if (meta_len != 8)
1964 		return false;
1965 
1966 	meta_type = get_unaligned_be32(data);
1967 	meta_tag = get_unaligned_be32(data + 4);
1968 
1969 	return (meta_type == NFP_NET_META_PORTID &&
1970 		meta_tag == NFP_META_PORT_ID_CTRL);
1971 }
1972 
1973 static bool
1974 nfp_ctrl_rx_one(struct nfp_net *nn, struct nfp_net_dp *dp,
1975 		struct nfp_net_r_vector *r_vec, struct nfp_net_rx_ring *rx_ring)
1976 {
1977 	unsigned int meta_len, data_len, meta_off, pkt_len, pkt_off;
1978 	struct nfp_net_rx_buf *rxbuf;
1979 	struct nfp_net_rx_desc *rxd;
1980 	dma_addr_t new_dma_addr;
1981 	struct sk_buff *skb;
1982 	void *new_frag;
1983 	int idx;
1984 
1985 	idx = D_IDX(rx_ring, rx_ring->rd_p);
1986 
1987 	rxd = &rx_ring->rxds[idx];
1988 	if (!(rxd->rxd.meta_len_dd & PCIE_DESC_RX_DD))
1989 		return false;
1990 
1991 	/* Memory barrier to ensure that we won't do other reads
1992 	 * before the DD bit.
1993 	 */
1994 	dma_rmb();
1995 
1996 	rx_ring->rd_p++;
1997 
1998 	rxbuf =	&rx_ring->rxbufs[idx];
1999 	meta_len = rxd->rxd.meta_len_dd & PCIE_DESC_RX_META_LEN_MASK;
2000 	data_len = le16_to_cpu(rxd->rxd.data_len);
2001 	pkt_len = data_len - meta_len;
2002 
2003 	pkt_off = NFP_NET_RX_BUF_HEADROOM + dp->rx_dma_off;
2004 	if (dp->rx_offset == NFP_NET_CFG_RX_OFFSET_DYNAMIC)
2005 		pkt_off += meta_len;
2006 	else
2007 		pkt_off += dp->rx_offset;
2008 	meta_off = pkt_off - meta_len;
2009 
2010 	/* Stats update */
2011 	u64_stats_update_begin(&r_vec->rx_sync);
2012 	r_vec->rx_pkts++;
2013 	r_vec->rx_bytes += pkt_len;
2014 	u64_stats_update_end(&r_vec->rx_sync);
2015 
2016 	nfp_net_dma_sync_cpu_rx(dp, rxbuf->dma_addr + meta_off,	data_len);
2017 
2018 	if (unlikely(!nfp_ctrl_meta_ok(nn, rxbuf->frag + meta_off, meta_len))) {
2019 		nn_dp_warn(dp, "incorrect metadata for ctrl packet (%d)\n",
2020 			   meta_len);
2021 		nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
2022 		return true;
2023 	}
2024 
2025 	skb = build_skb(rxbuf->frag, dp->fl_bufsz);
2026 	if (unlikely(!skb)) {
2027 		nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
2028 		return true;
2029 	}
2030 	new_frag = nfp_net_napi_alloc_one(dp, &new_dma_addr);
2031 	if (unlikely(!new_frag)) {
2032 		nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, skb);
2033 		return true;
2034 	}
2035 
2036 	nfp_net_dma_unmap_rx(dp, rxbuf->dma_addr);
2037 
2038 	nfp_net_rx_give_one(dp, rx_ring, new_frag, new_dma_addr);
2039 
2040 	skb_reserve(skb, pkt_off);
2041 	skb_put(skb, pkt_len);
2042 
2043 	nfp_app_ctrl_rx(nn->app, skb);
2044 
2045 	return true;
2046 }
2047 
2048 static bool nfp_ctrl_rx(struct nfp_net_r_vector *r_vec)
2049 {
2050 	struct nfp_net_rx_ring *rx_ring = r_vec->rx_ring;
2051 	struct nfp_net *nn = r_vec->nfp_net;
2052 	struct nfp_net_dp *dp = &nn->dp;
2053 	unsigned int budget = 512;
2054 
2055 	while (nfp_ctrl_rx_one(nn, dp, r_vec, rx_ring) && budget--)
2056 		continue;
2057 
2058 	return budget;
2059 }
2060 
2061 static void nfp_ctrl_poll(unsigned long arg)
2062 {
2063 	struct nfp_net_r_vector *r_vec = (void *)arg;
2064 
2065 	spin_lock(&r_vec->lock);
2066 	nfp_net_tx_complete(r_vec->tx_ring, 0);
2067 	__nfp_ctrl_tx_queued(r_vec);
2068 	spin_unlock(&r_vec->lock);
2069 
2070 	if (nfp_ctrl_rx(r_vec)) {
2071 		nfp_net_irq_unmask(r_vec->nfp_net, r_vec->irq_entry);
2072 	} else {
2073 		tasklet_schedule(&r_vec->tasklet);
2074 		nn_dp_warn(&r_vec->nfp_net->dp,
2075 			   "control message budget exceeded!\n");
2076 	}
2077 }
2078 
2079 /* Setup and Configuration
2080  */
2081 
2082 /**
2083  * nfp_net_vecs_init() - Assign IRQs and setup rvecs.
2084  * @nn:		NFP Network structure
2085  */
2086 static void nfp_net_vecs_init(struct nfp_net *nn)
2087 {
2088 	struct nfp_net_r_vector *r_vec;
2089 	int r;
2090 
2091 	nn->lsc_handler = nfp_net_irq_lsc;
2092 	nn->exn_handler = nfp_net_irq_exn;
2093 
2094 	for (r = 0; r < nn->max_r_vecs; r++) {
2095 		struct msix_entry *entry;
2096 
2097 		entry = &nn->irq_entries[NFP_NET_NON_Q_VECTORS + r];
2098 
2099 		r_vec = &nn->r_vecs[r];
2100 		r_vec->nfp_net = nn;
2101 		r_vec->irq_entry = entry->entry;
2102 		r_vec->irq_vector = entry->vector;
2103 
2104 		if (nn->dp.netdev) {
2105 			r_vec->handler = nfp_net_irq_rxtx;
2106 		} else {
2107 			r_vec->handler = nfp_ctrl_irq_rxtx;
2108 
2109 			__skb_queue_head_init(&r_vec->queue);
2110 			spin_lock_init(&r_vec->lock);
2111 			tasklet_init(&r_vec->tasklet, nfp_ctrl_poll,
2112 				     (unsigned long)r_vec);
2113 			tasklet_disable(&r_vec->tasklet);
2114 		}
2115 
2116 		cpumask_set_cpu(r, &r_vec->affinity_mask);
2117 	}
2118 }
2119 
2120 /**
2121  * nfp_net_tx_ring_free() - Free resources allocated to a TX ring
2122  * @tx_ring:   TX ring to free
2123  */
2124 static void nfp_net_tx_ring_free(struct nfp_net_tx_ring *tx_ring)
2125 {
2126 	struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
2127 	struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
2128 
2129 	kvfree(tx_ring->txbufs);
2130 
2131 	if (tx_ring->txds)
2132 		dma_free_coherent(dp->dev, tx_ring->size,
2133 				  tx_ring->txds, tx_ring->dma);
2134 
2135 	tx_ring->cnt = 0;
2136 	tx_ring->txbufs = NULL;
2137 	tx_ring->txds = NULL;
2138 	tx_ring->dma = 0;
2139 	tx_ring->size = 0;
2140 }
2141 
2142 /**
2143  * nfp_net_tx_ring_alloc() - Allocate resource for a TX ring
2144  * @dp:        NFP Net data path struct
2145  * @tx_ring:   TX Ring structure to allocate
2146  *
2147  * Return: 0 on success, negative errno otherwise.
2148  */
2149 static int
2150 nfp_net_tx_ring_alloc(struct nfp_net_dp *dp, struct nfp_net_tx_ring *tx_ring)
2151 {
2152 	struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
2153 
2154 	tx_ring->cnt = dp->txd_cnt;
2155 
2156 	tx_ring->size = array_size(tx_ring->cnt, sizeof(*tx_ring->txds));
2157 	tx_ring->txds = dma_zalloc_coherent(dp->dev, tx_ring->size,
2158 					    &tx_ring->dma,
2159 					    GFP_KERNEL | __GFP_NOWARN);
2160 	if (!tx_ring->txds) {
2161 		netdev_warn(dp->netdev, "failed to allocate TX descriptor ring memory, requested descriptor count: %d, consider lowering descriptor count\n",
2162 			    tx_ring->cnt);
2163 		goto err_alloc;
2164 	}
2165 
2166 	tx_ring->txbufs = kvcalloc(tx_ring->cnt, sizeof(*tx_ring->txbufs),
2167 				   GFP_KERNEL);
2168 	if (!tx_ring->txbufs)
2169 		goto err_alloc;
2170 
2171 	if (!tx_ring->is_xdp && dp->netdev)
2172 		netif_set_xps_queue(dp->netdev, &r_vec->affinity_mask,
2173 				    tx_ring->idx);
2174 
2175 	return 0;
2176 
2177 err_alloc:
2178 	nfp_net_tx_ring_free(tx_ring);
2179 	return -ENOMEM;
2180 }
2181 
2182 static void
2183 nfp_net_tx_ring_bufs_free(struct nfp_net_dp *dp,
2184 			  struct nfp_net_tx_ring *tx_ring)
2185 {
2186 	unsigned int i;
2187 
2188 	if (!tx_ring->is_xdp)
2189 		return;
2190 
2191 	for (i = 0; i < tx_ring->cnt; i++) {
2192 		if (!tx_ring->txbufs[i].frag)
2193 			return;
2194 
2195 		nfp_net_dma_unmap_rx(dp, tx_ring->txbufs[i].dma_addr);
2196 		__free_page(virt_to_page(tx_ring->txbufs[i].frag));
2197 	}
2198 }
2199 
2200 static int
2201 nfp_net_tx_ring_bufs_alloc(struct nfp_net_dp *dp,
2202 			   struct nfp_net_tx_ring *tx_ring)
2203 {
2204 	struct nfp_net_tx_buf *txbufs = tx_ring->txbufs;
2205 	unsigned int i;
2206 
2207 	if (!tx_ring->is_xdp)
2208 		return 0;
2209 
2210 	for (i = 0; i < tx_ring->cnt; i++) {
2211 		txbufs[i].frag = nfp_net_rx_alloc_one(dp, &txbufs[i].dma_addr);
2212 		if (!txbufs[i].frag) {
2213 			nfp_net_tx_ring_bufs_free(dp, tx_ring);
2214 			return -ENOMEM;
2215 		}
2216 	}
2217 
2218 	return 0;
2219 }
2220 
2221 static int nfp_net_tx_rings_prepare(struct nfp_net *nn, struct nfp_net_dp *dp)
2222 {
2223 	unsigned int r;
2224 
2225 	dp->tx_rings = kcalloc(dp->num_tx_rings, sizeof(*dp->tx_rings),
2226 			       GFP_KERNEL);
2227 	if (!dp->tx_rings)
2228 		return -ENOMEM;
2229 
2230 	for (r = 0; r < dp->num_tx_rings; r++) {
2231 		int bias = 0;
2232 
2233 		if (r >= dp->num_stack_tx_rings)
2234 			bias = dp->num_stack_tx_rings;
2235 
2236 		nfp_net_tx_ring_init(&dp->tx_rings[r], &nn->r_vecs[r - bias],
2237 				     r, bias);
2238 
2239 		if (nfp_net_tx_ring_alloc(dp, &dp->tx_rings[r]))
2240 			goto err_free_prev;
2241 
2242 		if (nfp_net_tx_ring_bufs_alloc(dp, &dp->tx_rings[r]))
2243 			goto err_free_ring;
2244 	}
2245 
2246 	return 0;
2247 
2248 err_free_prev:
2249 	while (r--) {
2250 		nfp_net_tx_ring_bufs_free(dp, &dp->tx_rings[r]);
2251 err_free_ring:
2252 		nfp_net_tx_ring_free(&dp->tx_rings[r]);
2253 	}
2254 	kfree(dp->tx_rings);
2255 	return -ENOMEM;
2256 }
2257 
2258 static void nfp_net_tx_rings_free(struct nfp_net_dp *dp)
2259 {
2260 	unsigned int r;
2261 
2262 	for (r = 0; r < dp->num_tx_rings; r++) {
2263 		nfp_net_tx_ring_bufs_free(dp, &dp->tx_rings[r]);
2264 		nfp_net_tx_ring_free(&dp->tx_rings[r]);
2265 	}
2266 
2267 	kfree(dp->tx_rings);
2268 }
2269 
2270 /**
2271  * nfp_net_rx_ring_free() - Free resources allocated to a RX ring
2272  * @rx_ring:  RX ring to free
2273  */
2274 static void nfp_net_rx_ring_free(struct nfp_net_rx_ring *rx_ring)
2275 {
2276 	struct nfp_net_r_vector *r_vec = rx_ring->r_vec;
2277 	struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
2278 
2279 	if (dp->netdev)
2280 		xdp_rxq_info_unreg(&rx_ring->xdp_rxq);
2281 	kvfree(rx_ring->rxbufs);
2282 
2283 	if (rx_ring->rxds)
2284 		dma_free_coherent(dp->dev, rx_ring->size,
2285 				  rx_ring->rxds, rx_ring->dma);
2286 
2287 	rx_ring->cnt = 0;
2288 	rx_ring->rxbufs = NULL;
2289 	rx_ring->rxds = NULL;
2290 	rx_ring->dma = 0;
2291 	rx_ring->size = 0;
2292 }
2293 
2294 /**
2295  * nfp_net_rx_ring_alloc() - Allocate resource for a RX ring
2296  * @dp:	      NFP Net data path struct
2297  * @rx_ring:  RX ring to allocate
2298  *
2299  * Return: 0 on success, negative errno otherwise.
2300  */
2301 static int
2302 nfp_net_rx_ring_alloc(struct nfp_net_dp *dp, struct nfp_net_rx_ring *rx_ring)
2303 {
2304 	int err;
2305 
2306 	if (dp->netdev) {
2307 		err = xdp_rxq_info_reg(&rx_ring->xdp_rxq, dp->netdev,
2308 				       rx_ring->idx);
2309 		if (err < 0)
2310 			return err;
2311 	}
2312 
2313 	rx_ring->cnt = dp->rxd_cnt;
2314 	rx_ring->size = array_size(rx_ring->cnt, sizeof(*rx_ring->rxds));
2315 	rx_ring->rxds = dma_zalloc_coherent(dp->dev, rx_ring->size,
2316 					    &rx_ring->dma,
2317 					    GFP_KERNEL | __GFP_NOWARN);
2318 	if (!rx_ring->rxds) {
2319 		netdev_warn(dp->netdev, "failed to allocate RX descriptor ring memory, requested descriptor count: %d, consider lowering descriptor count\n",
2320 			    rx_ring->cnt);
2321 		goto err_alloc;
2322 	}
2323 
2324 	rx_ring->rxbufs = kvcalloc(rx_ring->cnt, sizeof(*rx_ring->rxbufs),
2325 				   GFP_KERNEL);
2326 	if (!rx_ring->rxbufs)
2327 		goto err_alloc;
2328 
2329 	return 0;
2330 
2331 err_alloc:
2332 	nfp_net_rx_ring_free(rx_ring);
2333 	return -ENOMEM;
2334 }
2335 
2336 static int nfp_net_rx_rings_prepare(struct nfp_net *nn, struct nfp_net_dp *dp)
2337 {
2338 	unsigned int r;
2339 
2340 	dp->rx_rings = kcalloc(dp->num_rx_rings, sizeof(*dp->rx_rings),
2341 			       GFP_KERNEL);
2342 	if (!dp->rx_rings)
2343 		return -ENOMEM;
2344 
2345 	for (r = 0; r < dp->num_rx_rings; r++) {
2346 		nfp_net_rx_ring_init(&dp->rx_rings[r], &nn->r_vecs[r], r);
2347 
2348 		if (nfp_net_rx_ring_alloc(dp, &dp->rx_rings[r]))
2349 			goto err_free_prev;
2350 
2351 		if (nfp_net_rx_ring_bufs_alloc(dp, &dp->rx_rings[r]))
2352 			goto err_free_ring;
2353 	}
2354 
2355 	return 0;
2356 
2357 err_free_prev:
2358 	while (r--) {
2359 		nfp_net_rx_ring_bufs_free(dp, &dp->rx_rings[r]);
2360 err_free_ring:
2361 		nfp_net_rx_ring_free(&dp->rx_rings[r]);
2362 	}
2363 	kfree(dp->rx_rings);
2364 	return -ENOMEM;
2365 }
2366 
2367 static void nfp_net_rx_rings_free(struct nfp_net_dp *dp)
2368 {
2369 	unsigned int r;
2370 
2371 	for (r = 0; r < dp->num_rx_rings; r++) {
2372 		nfp_net_rx_ring_bufs_free(dp, &dp->rx_rings[r]);
2373 		nfp_net_rx_ring_free(&dp->rx_rings[r]);
2374 	}
2375 
2376 	kfree(dp->rx_rings);
2377 }
2378 
2379 static void
2380 nfp_net_vector_assign_rings(struct nfp_net_dp *dp,
2381 			    struct nfp_net_r_vector *r_vec, int idx)
2382 {
2383 	r_vec->rx_ring = idx < dp->num_rx_rings ? &dp->rx_rings[idx] : NULL;
2384 	r_vec->tx_ring =
2385 		idx < dp->num_stack_tx_rings ? &dp->tx_rings[idx] : NULL;
2386 
2387 	r_vec->xdp_ring = idx < dp->num_tx_rings - dp->num_stack_tx_rings ?
2388 		&dp->tx_rings[dp->num_stack_tx_rings + idx] : NULL;
2389 }
2390 
2391 static int
2392 nfp_net_prepare_vector(struct nfp_net *nn, struct nfp_net_r_vector *r_vec,
2393 		       int idx)
2394 {
2395 	int err;
2396 
2397 	/* Setup NAPI */
2398 	if (nn->dp.netdev)
2399 		netif_napi_add(nn->dp.netdev, &r_vec->napi,
2400 			       nfp_net_poll, NAPI_POLL_WEIGHT);
2401 	else
2402 		tasklet_enable(&r_vec->tasklet);
2403 
2404 	snprintf(r_vec->name, sizeof(r_vec->name),
2405 		 "%s-rxtx-%d", nfp_net_name(nn), idx);
2406 	err = request_irq(r_vec->irq_vector, r_vec->handler, 0, r_vec->name,
2407 			  r_vec);
2408 	if (err) {
2409 		if (nn->dp.netdev)
2410 			netif_napi_del(&r_vec->napi);
2411 		else
2412 			tasklet_disable(&r_vec->tasklet);
2413 
2414 		nn_err(nn, "Error requesting IRQ %d\n", r_vec->irq_vector);
2415 		return err;
2416 	}
2417 	disable_irq(r_vec->irq_vector);
2418 
2419 	irq_set_affinity_hint(r_vec->irq_vector, &r_vec->affinity_mask);
2420 
2421 	nn_dbg(nn, "RV%02d: irq=%03d/%03d\n", idx, r_vec->irq_vector,
2422 	       r_vec->irq_entry);
2423 
2424 	return 0;
2425 }
2426 
2427 static void
2428 nfp_net_cleanup_vector(struct nfp_net *nn, struct nfp_net_r_vector *r_vec)
2429 {
2430 	irq_set_affinity_hint(r_vec->irq_vector, NULL);
2431 	if (nn->dp.netdev)
2432 		netif_napi_del(&r_vec->napi);
2433 	else
2434 		tasklet_disable(&r_vec->tasklet);
2435 
2436 	free_irq(r_vec->irq_vector, r_vec);
2437 }
2438 
2439 /**
2440  * nfp_net_rss_write_itbl() - Write RSS indirection table to device
2441  * @nn:      NFP Net device to reconfigure
2442  */
2443 void nfp_net_rss_write_itbl(struct nfp_net *nn)
2444 {
2445 	int i;
2446 
2447 	for (i = 0; i < NFP_NET_CFG_RSS_ITBL_SZ; i += 4)
2448 		nn_writel(nn, NFP_NET_CFG_RSS_ITBL + i,
2449 			  get_unaligned_le32(nn->rss_itbl + i));
2450 }
2451 
2452 /**
2453  * nfp_net_rss_write_key() - Write RSS hash key to device
2454  * @nn:      NFP Net device to reconfigure
2455  */
2456 void nfp_net_rss_write_key(struct nfp_net *nn)
2457 {
2458 	int i;
2459 
2460 	for (i = 0; i < nfp_net_rss_key_sz(nn); i += 4)
2461 		nn_writel(nn, NFP_NET_CFG_RSS_KEY + i,
2462 			  get_unaligned_le32(nn->rss_key + i));
2463 }
2464 
2465 /**
2466  * nfp_net_coalesce_write_cfg() - Write irq coalescence configuration to HW
2467  * @nn:      NFP Net device to reconfigure
2468  */
2469 void nfp_net_coalesce_write_cfg(struct nfp_net *nn)
2470 {
2471 	u8 i;
2472 	u32 factor;
2473 	u32 value;
2474 
2475 	/* Compute factor used to convert coalesce '_usecs' parameters to
2476 	 * ME timestamp ticks.  There are 16 ME clock cycles for each timestamp
2477 	 * count.
2478 	 */
2479 	factor = nn->tlv_caps.me_freq_mhz / 16;
2480 
2481 	/* copy RX interrupt coalesce parameters */
2482 	value = (nn->rx_coalesce_max_frames << 16) |
2483 		(factor * nn->rx_coalesce_usecs);
2484 	for (i = 0; i < nn->dp.num_rx_rings; i++)
2485 		nn_writel(nn, NFP_NET_CFG_RXR_IRQ_MOD(i), value);
2486 
2487 	/* copy TX interrupt coalesce parameters */
2488 	value = (nn->tx_coalesce_max_frames << 16) |
2489 		(factor * nn->tx_coalesce_usecs);
2490 	for (i = 0; i < nn->dp.num_tx_rings; i++)
2491 		nn_writel(nn, NFP_NET_CFG_TXR_IRQ_MOD(i), value);
2492 }
2493 
2494 /**
2495  * nfp_net_write_mac_addr() - Write mac address to the device control BAR
2496  * @nn:      NFP Net device to reconfigure
2497  * @addr:    MAC address to write
2498  *
2499  * Writes the MAC address from the netdev to the device control BAR.  Does not
2500  * perform the required reconfig.  We do a bit of byte swapping dance because
2501  * firmware is LE.
2502  */
2503 static void nfp_net_write_mac_addr(struct nfp_net *nn, const u8 *addr)
2504 {
2505 	nn_writel(nn, NFP_NET_CFG_MACADDR + 0, get_unaligned_be32(addr));
2506 	nn_writew(nn, NFP_NET_CFG_MACADDR + 6, get_unaligned_be16(addr + 4));
2507 }
2508 
2509 static void nfp_net_vec_clear_ring_data(struct nfp_net *nn, unsigned int idx)
2510 {
2511 	nn_writeq(nn, NFP_NET_CFG_RXR_ADDR(idx), 0);
2512 	nn_writeb(nn, NFP_NET_CFG_RXR_SZ(idx), 0);
2513 	nn_writeb(nn, NFP_NET_CFG_RXR_VEC(idx), 0);
2514 
2515 	nn_writeq(nn, NFP_NET_CFG_TXR_ADDR(idx), 0);
2516 	nn_writeb(nn, NFP_NET_CFG_TXR_SZ(idx), 0);
2517 	nn_writeb(nn, NFP_NET_CFG_TXR_VEC(idx), 0);
2518 }
2519 
2520 /**
2521  * nfp_net_clear_config_and_disable() - Clear control BAR and disable NFP
2522  * @nn:      NFP Net device to reconfigure
2523  *
2524  * Warning: must be fully idempotent.
2525  */
2526 static void nfp_net_clear_config_and_disable(struct nfp_net *nn)
2527 {
2528 	u32 new_ctrl, update;
2529 	unsigned int r;
2530 	int err;
2531 
2532 	new_ctrl = nn->dp.ctrl;
2533 	new_ctrl &= ~NFP_NET_CFG_CTRL_ENABLE;
2534 	update = NFP_NET_CFG_UPDATE_GEN;
2535 	update |= NFP_NET_CFG_UPDATE_MSIX;
2536 	update |= NFP_NET_CFG_UPDATE_RING;
2537 
2538 	if (nn->cap & NFP_NET_CFG_CTRL_RINGCFG)
2539 		new_ctrl &= ~NFP_NET_CFG_CTRL_RINGCFG;
2540 
2541 	nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, 0);
2542 	nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, 0);
2543 
2544 	nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
2545 	err = nfp_net_reconfig(nn, update);
2546 	if (err)
2547 		nn_err(nn, "Could not disable device: %d\n", err);
2548 
2549 	for (r = 0; r < nn->dp.num_rx_rings; r++)
2550 		nfp_net_rx_ring_reset(&nn->dp.rx_rings[r]);
2551 	for (r = 0; r < nn->dp.num_tx_rings; r++)
2552 		nfp_net_tx_ring_reset(&nn->dp, &nn->dp.tx_rings[r]);
2553 	for (r = 0; r < nn->dp.num_r_vecs; r++)
2554 		nfp_net_vec_clear_ring_data(nn, r);
2555 
2556 	nn->dp.ctrl = new_ctrl;
2557 }
2558 
2559 static void
2560 nfp_net_rx_ring_hw_cfg_write(struct nfp_net *nn,
2561 			     struct nfp_net_rx_ring *rx_ring, unsigned int idx)
2562 {
2563 	/* Write the DMA address, size and MSI-X info to the device */
2564 	nn_writeq(nn, NFP_NET_CFG_RXR_ADDR(idx), rx_ring->dma);
2565 	nn_writeb(nn, NFP_NET_CFG_RXR_SZ(idx), ilog2(rx_ring->cnt));
2566 	nn_writeb(nn, NFP_NET_CFG_RXR_VEC(idx), rx_ring->r_vec->irq_entry);
2567 }
2568 
2569 static void
2570 nfp_net_tx_ring_hw_cfg_write(struct nfp_net *nn,
2571 			     struct nfp_net_tx_ring *tx_ring, unsigned int idx)
2572 {
2573 	nn_writeq(nn, NFP_NET_CFG_TXR_ADDR(idx), tx_ring->dma);
2574 	nn_writeb(nn, NFP_NET_CFG_TXR_SZ(idx), ilog2(tx_ring->cnt));
2575 	nn_writeb(nn, NFP_NET_CFG_TXR_VEC(idx), tx_ring->r_vec->irq_entry);
2576 }
2577 
2578 /**
2579  * nfp_net_set_config_and_enable() - Write control BAR and enable NFP
2580  * @nn:      NFP Net device to reconfigure
2581  */
2582 static int nfp_net_set_config_and_enable(struct nfp_net *nn)
2583 {
2584 	u32 bufsz, new_ctrl, update = 0;
2585 	unsigned int r;
2586 	int err;
2587 
2588 	new_ctrl = nn->dp.ctrl;
2589 
2590 	if (nn->dp.ctrl & NFP_NET_CFG_CTRL_RSS_ANY) {
2591 		nfp_net_rss_write_key(nn);
2592 		nfp_net_rss_write_itbl(nn);
2593 		nn_writel(nn, NFP_NET_CFG_RSS_CTRL, nn->rss_cfg);
2594 		update |= NFP_NET_CFG_UPDATE_RSS;
2595 	}
2596 
2597 	if (nn->dp.ctrl & NFP_NET_CFG_CTRL_IRQMOD) {
2598 		nfp_net_coalesce_write_cfg(nn);
2599 		update |= NFP_NET_CFG_UPDATE_IRQMOD;
2600 	}
2601 
2602 	for (r = 0; r < nn->dp.num_tx_rings; r++)
2603 		nfp_net_tx_ring_hw_cfg_write(nn, &nn->dp.tx_rings[r], r);
2604 	for (r = 0; r < nn->dp.num_rx_rings; r++)
2605 		nfp_net_rx_ring_hw_cfg_write(nn, &nn->dp.rx_rings[r], r);
2606 
2607 	nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, nn->dp.num_tx_rings == 64 ?
2608 		  0xffffffffffffffffULL : ((u64)1 << nn->dp.num_tx_rings) - 1);
2609 
2610 	nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, nn->dp.num_rx_rings == 64 ?
2611 		  0xffffffffffffffffULL : ((u64)1 << nn->dp.num_rx_rings) - 1);
2612 
2613 	if (nn->dp.netdev)
2614 		nfp_net_write_mac_addr(nn, nn->dp.netdev->dev_addr);
2615 
2616 	nn_writel(nn, NFP_NET_CFG_MTU, nn->dp.mtu);
2617 
2618 	bufsz = nn->dp.fl_bufsz - nn->dp.rx_dma_off - NFP_NET_RX_BUF_NON_DATA;
2619 	nn_writel(nn, NFP_NET_CFG_FLBUFSZ, bufsz);
2620 
2621 	/* Enable device */
2622 	new_ctrl |= NFP_NET_CFG_CTRL_ENABLE;
2623 	update |= NFP_NET_CFG_UPDATE_GEN;
2624 	update |= NFP_NET_CFG_UPDATE_MSIX;
2625 	update |= NFP_NET_CFG_UPDATE_RING;
2626 	if (nn->cap & NFP_NET_CFG_CTRL_RINGCFG)
2627 		new_ctrl |= NFP_NET_CFG_CTRL_RINGCFG;
2628 
2629 	nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
2630 	err = nfp_net_reconfig(nn, update);
2631 	if (err) {
2632 		nfp_net_clear_config_and_disable(nn);
2633 		return err;
2634 	}
2635 
2636 	nn->dp.ctrl = new_ctrl;
2637 
2638 	for (r = 0; r < nn->dp.num_rx_rings; r++)
2639 		nfp_net_rx_ring_fill_freelist(&nn->dp, &nn->dp.rx_rings[r]);
2640 
2641 	/* Since reconfiguration requests while NFP is down are ignored we
2642 	 * have to wipe the entire VXLAN configuration and reinitialize it.
2643 	 */
2644 	if (nn->dp.ctrl & NFP_NET_CFG_CTRL_VXLAN) {
2645 		memset(&nn->vxlan_ports, 0, sizeof(nn->vxlan_ports));
2646 		memset(&nn->vxlan_usecnt, 0, sizeof(nn->vxlan_usecnt));
2647 		udp_tunnel_get_rx_info(nn->dp.netdev);
2648 	}
2649 
2650 	return 0;
2651 }
2652 
2653 /**
2654  * nfp_net_close_stack() - Quiesce the stack (part of close)
2655  * @nn:	     NFP Net device to reconfigure
2656  */
2657 static void nfp_net_close_stack(struct nfp_net *nn)
2658 {
2659 	unsigned int r;
2660 
2661 	disable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
2662 	netif_carrier_off(nn->dp.netdev);
2663 	nn->link_up = false;
2664 
2665 	for (r = 0; r < nn->dp.num_r_vecs; r++) {
2666 		disable_irq(nn->r_vecs[r].irq_vector);
2667 		napi_disable(&nn->r_vecs[r].napi);
2668 	}
2669 
2670 	netif_tx_disable(nn->dp.netdev);
2671 }
2672 
2673 /**
2674  * nfp_net_close_free_all() - Free all runtime resources
2675  * @nn:      NFP Net device to reconfigure
2676  */
2677 static void nfp_net_close_free_all(struct nfp_net *nn)
2678 {
2679 	unsigned int r;
2680 
2681 	nfp_net_tx_rings_free(&nn->dp);
2682 	nfp_net_rx_rings_free(&nn->dp);
2683 
2684 	for (r = 0; r < nn->dp.num_r_vecs; r++)
2685 		nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
2686 
2687 	nfp_net_aux_irq_free(nn, NFP_NET_CFG_LSC, NFP_NET_IRQ_LSC_IDX);
2688 	nfp_net_aux_irq_free(nn, NFP_NET_CFG_EXN, NFP_NET_IRQ_EXN_IDX);
2689 }
2690 
2691 /**
2692  * nfp_net_netdev_close() - Called when the device is downed
2693  * @netdev:      netdev structure
2694  */
2695 static int nfp_net_netdev_close(struct net_device *netdev)
2696 {
2697 	struct nfp_net *nn = netdev_priv(netdev);
2698 
2699 	/* Step 1: Disable RX and TX rings from the Linux kernel perspective
2700 	 */
2701 	nfp_net_close_stack(nn);
2702 
2703 	/* Step 2: Tell NFP
2704 	 */
2705 	nfp_net_clear_config_and_disable(nn);
2706 	nfp_port_configure(netdev, false);
2707 
2708 	/* Step 3: Free resources
2709 	 */
2710 	nfp_net_close_free_all(nn);
2711 
2712 	nn_dbg(nn, "%s down", netdev->name);
2713 	return 0;
2714 }
2715 
2716 void nfp_ctrl_close(struct nfp_net *nn)
2717 {
2718 	int r;
2719 
2720 	rtnl_lock();
2721 
2722 	for (r = 0; r < nn->dp.num_r_vecs; r++) {
2723 		disable_irq(nn->r_vecs[r].irq_vector);
2724 		tasklet_disable(&nn->r_vecs[r].tasklet);
2725 	}
2726 
2727 	nfp_net_clear_config_and_disable(nn);
2728 
2729 	nfp_net_close_free_all(nn);
2730 
2731 	rtnl_unlock();
2732 }
2733 
2734 /**
2735  * nfp_net_open_stack() - Start the device from stack's perspective
2736  * @nn:      NFP Net device to reconfigure
2737  */
2738 static void nfp_net_open_stack(struct nfp_net *nn)
2739 {
2740 	unsigned int r;
2741 
2742 	for (r = 0; r < nn->dp.num_r_vecs; r++) {
2743 		napi_enable(&nn->r_vecs[r].napi);
2744 		enable_irq(nn->r_vecs[r].irq_vector);
2745 	}
2746 
2747 	netif_tx_wake_all_queues(nn->dp.netdev);
2748 
2749 	enable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
2750 	nfp_net_read_link_status(nn);
2751 }
2752 
2753 static int nfp_net_open_alloc_all(struct nfp_net *nn)
2754 {
2755 	int err, r;
2756 
2757 	err = nfp_net_aux_irq_request(nn, NFP_NET_CFG_EXN, "%s-exn",
2758 				      nn->exn_name, sizeof(nn->exn_name),
2759 				      NFP_NET_IRQ_EXN_IDX, nn->exn_handler);
2760 	if (err)
2761 		return err;
2762 	err = nfp_net_aux_irq_request(nn, NFP_NET_CFG_LSC, "%s-lsc",
2763 				      nn->lsc_name, sizeof(nn->lsc_name),
2764 				      NFP_NET_IRQ_LSC_IDX, nn->lsc_handler);
2765 	if (err)
2766 		goto err_free_exn;
2767 	disable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
2768 
2769 	for (r = 0; r < nn->dp.num_r_vecs; r++) {
2770 		err = nfp_net_prepare_vector(nn, &nn->r_vecs[r], r);
2771 		if (err)
2772 			goto err_cleanup_vec_p;
2773 	}
2774 
2775 	err = nfp_net_rx_rings_prepare(nn, &nn->dp);
2776 	if (err)
2777 		goto err_cleanup_vec;
2778 
2779 	err = nfp_net_tx_rings_prepare(nn, &nn->dp);
2780 	if (err)
2781 		goto err_free_rx_rings;
2782 
2783 	for (r = 0; r < nn->max_r_vecs; r++)
2784 		nfp_net_vector_assign_rings(&nn->dp, &nn->r_vecs[r], r);
2785 
2786 	return 0;
2787 
2788 err_free_rx_rings:
2789 	nfp_net_rx_rings_free(&nn->dp);
2790 err_cleanup_vec:
2791 	r = nn->dp.num_r_vecs;
2792 err_cleanup_vec_p:
2793 	while (r--)
2794 		nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
2795 	nfp_net_aux_irq_free(nn, NFP_NET_CFG_LSC, NFP_NET_IRQ_LSC_IDX);
2796 err_free_exn:
2797 	nfp_net_aux_irq_free(nn, NFP_NET_CFG_EXN, NFP_NET_IRQ_EXN_IDX);
2798 	return err;
2799 }
2800 
2801 static int nfp_net_netdev_open(struct net_device *netdev)
2802 {
2803 	struct nfp_net *nn = netdev_priv(netdev);
2804 	int err;
2805 
2806 	/* Step 1: Allocate resources for rings and the like
2807 	 * - Request interrupts
2808 	 * - Allocate RX and TX ring resources
2809 	 * - Setup initial RSS table
2810 	 */
2811 	err = nfp_net_open_alloc_all(nn);
2812 	if (err)
2813 		return err;
2814 
2815 	err = netif_set_real_num_tx_queues(netdev, nn->dp.num_stack_tx_rings);
2816 	if (err)
2817 		goto err_free_all;
2818 
2819 	err = netif_set_real_num_rx_queues(netdev, nn->dp.num_rx_rings);
2820 	if (err)
2821 		goto err_free_all;
2822 
2823 	/* Step 2: Configure the NFP
2824 	 * - Ifup the physical interface if it exists
2825 	 * - Enable rings from 0 to tx_rings/rx_rings - 1.
2826 	 * - Write MAC address (in case it changed)
2827 	 * - Set the MTU
2828 	 * - Set the Freelist buffer size
2829 	 * - Enable the FW
2830 	 */
2831 	err = nfp_port_configure(netdev, true);
2832 	if (err)
2833 		goto err_free_all;
2834 
2835 	err = nfp_net_set_config_and_enable(nn);
2836 	if (err)
2837 		goto err_port_disable;
2838 
2839 	/* Step 3: Enable for kernel
2840 	 * - put some freelist descriptors on each RX ring
2841 	 * - enable NAPI on each ring
2842 	 * - enable all TX queues
2843 	 * - set link state
2844 	 */
2845 	nfp_net_open_stack(nn);
2846 
2847 	return 0;
2848 
2849 err_port_disable:
2850 	nfp_port_configure(netdev, false);
2851 err_free_all:
2852 	nfp_net_close_free_all(nn);
2853 	return err;
2854 }
2855 
2856 int nfp_ctrl_open(struct nfp_net *nn)
2857 {
2858 	int err, r;
2859 
2860 	/* ring dumping depends on vNICs being opened/closed under rtnl */
2861 	rtnl_lock();
2862 
2863 	err = nfp_net_open_alloc_all(nn);
2864 	if (err)
2865 		goto err_unlock;
2866 
2867 	err = nfp_net_set_config_and_enable(nn);
2868 	if (err)
2869 		goto err_free_all;
2870 
2871 	for (r = 0; r < nn->dp.num_r_vecs; r++)
2872 		enable_irq(nn->r_vecs[r].irq_vector);
2873 
2874 	rtnl_unlock();
2875 
2876 	return 0;
2877 
2878 err_free_all:
2879 	nfp_net_close_free_all(nn);
2880 err_unlock:
2881 	rtnl_unlock();
2882 	return err;
2883 }
2884 
2885 static void nfp_net_set_rx_mode(struct net_device *netdev)
2886 {
2887 	struct nfp_net *nn = netdev_priv(netdev);
2888 	u32 new_ctrl;
2889 
2890 	new_ctrl = nn->dp.ctrl;
2891 
2892 	if (!netdev_mc_empty(netdev) || netdev->flags & IFF_ALLMULTI)
2893 		new_ctrl |= nn->cap & NFP_NET_CFG_CTRL_L2MC;
2894 	else
2895 		new_ctrl &= ~NFP_NET_CFG_CTRL_L2MC;
2896 
2897 	if (netdev->flags & IFF_PROMISC) {
2898 		if (nn->cap & NFP_NET_CFG_CTRL_PROMISC)
2899 			new_ctrl |= NFP_NET_CFG_CTRL_PROMISC;
2900 		else
2901 			nn_warn(nn, "FW does not support promiscuous mode\n");
2902 	} else {
2903 		new_ctrl &= ~NFP_NET_CFG_CTRL_PROMISC;
2904 	}
2905 
2906 	if (new_ctrl == nn->dp.ctrl)
2907 		return;
2908 
2909 	nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
2910 	nfp_net_reconfig_post(nn, NFP_NET_CFG_UPDATE_GEN);
2911 
2912 	nn->dp.ctrl = new_ctrl;
2913 }
2914 
2915 static void nfp_net_rss_init_itbl(struct nfp_net *nn)
2916 {
2917 	int i;
2918 
2919 	for (i = 0; i < sizeof(nn->rss_itbl); i++)
2920 		nn->rss_itbl[i] =
2921 			ethtool_rxfh_indir_default(i, nn->dp.num_rx_rings);
2922 }
2923 
2924 static void nfp_net_dp_swap(struct nfp_net *nn, struct nfp_net_dp *dp)
2925 {
2926 	struct nfp_net_dp new_dp = *dp;
2927 
2928 	*dp = nn->dp;
2929 	nn->dp = new_dp;
2930 
2931 	nn->dp.netdev->mtu = new_dp.mtu;
2932 
2933 	if (!netif_is_rxfh_configured(nn->dp.netdev))
2934 		nfp_net_rss_init_itbl(nn);
2935 }
2936 
2937 static int nfp_net_dp_swap_enable(struct nfp_net *nn, struct nfp_net_dp *dp)
2938 {
2939 	unsigned int r;
2940 	int err;
2941 
2942 	nfp_net_dp_swap(nn, dp);
2943 
2944 	for (r = 0; r <	nn->max_r_vecs; r++)
2945 		nfp_net_vector_assign_rings(&nn->dp, &nn->r_vecs[r], r);
2946 
2947 	err = netif_set_real_num_rx_queues(nn->dp.netdev, nn->dp.num_rx_rings);
2948 	if (err)
2949 		return err;
2950 
2951 	if (nn->dp.netdev->real_num_tx_queues != nn->dp.num_stack_tx_rings) {
2952 		err = netif_set_real_num_tx_queues(nn->dp.netdev,
2953 						   nn->dp.num_stack_tx_rings);
2954 		if (err)
2955 			return err;
2956 	}
2957 
2958 	return nfp_net_set_config_and_enable(nn);
2959 }
2960 
2961 struct nfp_net_dp *nfp_net_clone_dp(struct nfp_net *nn)
2962 {
2963 	struct nfp_net_dp *new;
2964 
2965 	new = kmalloc(sizeof(*new), GFP_KERNEL);
2966 	if (!new)
2967 		return NULL;
2968 
2969 	*new = nn->dp;
2970 
2971 	/* Clear things which need to be recomputed */
2972 	new->fl_bufsz = 0;
2973 	new->tx_rings = NULL;
2974 	new->rx_rings = NULL;
2975 	new->num_r_vecs = 0;
2976 	new->num_stack_tx_rings = 0;
2977 
2978 	return new;
2979 }
2980 
2981 static int
2982 nfp_net_check_config(struct nfp_net *nn, struct nfp_net_dp *dp,
2983 		     struct netlink_ext_ack *extack)
2984 {
2985 	/* XDP-enabled tests */
2986 	if (!dp->xdp_prog)
2987 		return 0;
2988 	if (dp->fl_bufsz > PAGE_SIZE) {
2989 		NL_SET_ERR_MSG_MOD(extack, "MTU too large w/ XDP enabled");
2990 		return -EINVAL;
2991 	}
2992 	if (dp->num_tx_rings > nn->max_tx_rings) {
2993 		NL_SET_ERR_MSG_MOD(extack, "Insufficient number of TX rings w/ XDP enabled");
2994 		return -EINVAL;
2995 	}
2996 
2997 	return 0;
2998 }
2999 
3000 int nfp_net_ring_reconfig(struct nfp_net *nn, struct nfp_net_dp *dp,
3001 			  struct netlink_ext_ack *extack)
3002 {
3003 	int r, err;
3004 
3005 	dp->fl_bufsz = nfp_net_calc_fl_bufsz(dp);
3006 
3007 	dp->num_stack_tx_rings = dp->num_tx_rings;
3008 	if (dp->xdp_prog)
3009 		dp->num_stack_tx_rings -= dp->num_rx_rings;
3010 
3011 	dp->num_r_vecs = max(dp->num_rx_rings, dp->num_stack_tx_rings);
3012 
3013 	err = nfp_net_check_config(nn, dp, extack);
3014 	if (err)
3015 		goto exit_free_dp;
3016 
3017 	if (!netif_running(dp->netdev)) {
3018 		nfp_net_dp_swap(nn, dp);
3019 		err = 0;
3020 		goto exit_free_dp;
3021 	}
3022 
3023 	/* Prepare new rings */
3024 	for (r = nn->dp.num_r_vecs; r < dp->num_r_vecs; r++) {
3025 		err = nfp_net_prepare_vector(nn, &nn->r_vecs[r], r);
3026 		if (err) {
3027 			dp->num_r_vecs = r;
3028 			goto err_cleanup_vecs;
3029 		}
3030 	}
3031 
3032 	err = nfp_net_rx_rings_prepare(nn, dp);
3033 	if (err)
3034 		goto err_cleanup_vecs;
3035 
3036 	err = nfp_net_tx_rings_prepare(nn, dp);
3037 	if (err)
3038 		goto err_free_rx;
3039 
3040 	/* Stop device, swap in new rings, try to start the firmware */
3041 	nfp_net_close_stack(nn);
3042 	nfp_net_clear_config_and_disable(nn);
3043 
3044 	err = nfp_net_dp_swap_enable(nn, dp);
3045 	if (err) {
3046 		int err2;
3047 
3048 		nfp_net_clear_config_and_disable(nn);
3049 
3050 		/* Try with old configuration and old rings */
3051 		err2 = nfp_net_dp_swap_enable(nn, dp);
3052 		if (err2)
3053 			nn_err(nn, "Can't restore ring config - FW communication failed (%d,%d)\n",
3054 			       err, err2);
3055 	}
3056 	for (r = dp->num_r_vecs - 1; r >= nn->dp.num_r_vecs; r--)
3057 		nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
3058 
3059 	nfp_net_rx_rings_free(dp);
3060 	nfp_net_tx_rings_free(dp);
3061 
3062 	nfp_net_open_stack(nn);
3063 exit_free_dp:
3064 	kfree(dp);
3065 
3066 	return err;
3067 
3068 err_free_rx:
3069 	nfp_net_rx_rings_free(dp);
3070 err_cleanup_vecs:
3071 	for (r = dp->num_r_vecs - 1; r >= nn->dp.num_r_vecs; r--)
3072 		nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
3073 	kfree(dp);
3074 	return err;
3075 }
3076 
3077 static int nfp_net_change_mtu(struct net_device *netdev, int new_mtu)
3078 {
3079 	struct nfp_net *nn = netdev_priv(netdev);
3080 	struct nfp_net_dp *dp;
3081 	int err;
3082 
3083 	err = nfp_app_check_mtu(nn->app, netdev, new_mtu);
3084 	if (err)
3085 		return err;
3086 
3087 	dp = nfp_net_clone_dp(nn);
3088 	if (!dp)
3089 		return -ENOMEM;
3090 
3091 	dp->mtu = new_mtu;
3092 
3093 	return nfp_net_ring_reconfig(nn, dp, NULL);
3094 }
3095 
3096 static int
3097 nfp_net_vlan_rx_add_vid(struct net_device *netdev, __be16 proto, u16 vid)
3098 {
3099 	struct nfp_net *nn = netdev_priv(netdev);
3100 
3101 	/* Priority tagged packets with vlan id 0 are processed by the
3102 	 * NFP as untagged packets
3103 	 */
3104 	if (!vid)
3105 		return 0;
3106 
3107 	nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_VLAN_FILTER_VID, vid);
3108 	nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_VLAN_FILTER_PROTO,
3109 		  ETH_P_8021Q);
3110 
3111 	return nfp_net_reconfig_mbox(nn, NFP_NET_CFG_MBOX_CMD_CTAG_FILTER_ADD);
3112 }
3113 
3114 static int
3115 nfp_net_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto, u16 vid)
3116 {
3117 	struct nfp_net *nn = netdev_priv(netdev);
3118 
3119 	/* Priority tagged packets with vlan id 0 are processed by the
3120 	 * NFP as untagged packets
3121 	 */
3122 	if (!vid)
3123 		return 0;
3124 
3125 	nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_VLAN_FILTER_VID, vid);
3126 	nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_VLAN_FILTER_PROTO,
3127 		  ETH_P_8021Q);
3128 
3129 	return nfp_net_reconfig_mbox(nn, NFP_NET_CFG_MBOX_CMD_CTAG_FILTER_KILL);
3130 }
3131 
3132 static void nfp_net_stat64(struct net_device *netdev,
3133 			   struct rtnl_link_stats64 *stats)
3134 {
3135 	struct nfp_net *nn = netdev_priv(netdev);
3136 	int r;
3137 
3138 	/* Collect software stats */
3139 	for (r = 0; r < nn->max_r_vecs; r++) {
3140 		struct nfp_net_r_vector *r_vec = &nn->r_vecs[r];
3141 		u64 data[3];
3142 		unsigned int start;
3143 
3144 		do {
3145 			start = u64_stats_fetch_begin(&r_vec->rx_sync);
3146 			data[0] = r_vec->rx_pkts;
3147 			data[1] = r_vec->rx_bytes;
3148 			data[2] = r_vec->rx_drops;
3149 		} while (u64_stats_fetch_retry(&r_vec->rx_sync, start));
3150 		stats->rx_packets += data[0];
3151 		stats->rx_bytes += data[1];
3152 		stats->rx_dropped += data[2];
3153 
3154 		do {
3155 			start = u64_stats_fetch_begin(&r_vec->tx_sync);
3156 			data[0] = r_vec->tx_pkts;
3157 			data[1] = r_vec->tx_bytes;
3158 			data[2] = r_vec->tx_errors;
3159 		} while (u64_stats_fetch_retry(&r_vec->tx_sync, start));
3160 		stats->tx_packets += data[0];
3161 		stats->tx_bytes += data[1];
3162 		stats->tx_errors += data[2];
3163 	}
3164 
3165 	/* Add in device stats */
3166 	stats->multicast += nn_readq(nn, NFP_NET_CFG_STATS_RX_MC_FRAMES);
3167 	stats->rx_dropped += nn_readq(nn, NFP_NET_CFG_STATS_RX_DISCARDS);
3168 	stats->rx_errors += nn_readq(nn, NFP_NET_CFG_STATS_RX_ERRORS);
3169 
3170 	stats->tx_dropped += nn_readq(nn, NFP_NET_CFG_STATS_TX_DISCARDS);
3171 	stats->tx_errors += nn_readq(nn, NFP_NET_CFG_STATS_TX_ERRORS);
3172 }
3173 
3174 static int nfp_net_set_features(struct net_device *netdev,
3175 				netdev_features_t features)
3176 {
3177 	netdev_features_t changed = netdev->features ^ features;
3178 	struct nfp_net *nn = netdev_priv(netdev);
3179 	u32 new_ctrl;
3180 	int err;
3181 
3182 	/* Assume this is not called with features we have not advertised */
3183 
3184 	new_ctrl = nn->dp.ctrl;
3185 
3186 	if (changed & NETIF_F_RXCSUM) {
3187 		if (features & NETIF_F_RXCSUM)
3188 			new_ctrl |= nn->cap & NFP_NET_CFG_CTRL_RXCSUM_ANY;
3189 		else
3190 			new_ctrl &= ~NFP_NET_CFG_CTRL_RXCSUM_ANY;
3191 	}
3192 
3193 	if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
3194 		if (features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))
3195 			new_ctrl |= NFP_NET_CFG_CTRL_TXCSUM;
3196 		else
3197 			new_ctrl &= ~NFP_NET_CFG_CTRL_TXCSUM;
3198 	}
3199 
3200 	if (changed & (NETIF_F_TSO | NETIF_F_TSO6)) {
3201 		if (features & (NETIF_F_TSO | NETIF_F_TSO6))
3202 			new_ctrl |= nn->cap & NFP_NET_CFG_CTRL_LSO2 ?:
3203 					      NFP_NET_CFG_CTRL_LSO;
3204 		else
3205 			new_ctrl &= ~NFP_NET_CFG_CTRL_LSO_ANY;
3206 	}
3207 
3208 	if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
3209 		if (features & NETIF_F_HW_VLAN_CTAG_RX)
3210 			new_ctrl |= NFP_NET_CFG_CTRL_RXVLAN;
3211 		else
3212 			new_ctrl &= ~NFP_NET_CFG_CTRL_RXVLAN;
3213 	}
3214 
3215 	if (changed & NETIF_F_HW_VLAN_CTAG_TX) {
3216 		if (features & NETIF_F_HW_VLAN_CTAG_TX)
3217 			new_ctrl |= NFP_NET_CFG_CTRL_TXVLAN;
3218 		else
3219 			new_ctrl &= ~NFP_NET_CFG_CTRL_TXVLAN;
3220 	}
3221 
3222 	if (changed & NETIF_F_HW_VLAN_CTAG_FILTER) {
3223 		if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
3224 			new_ctrl |= NFP_NET_CFG_CTRL_CTAG_FILTER;
3225 		else
3226 			new_ctrl &= ~NFP_NET_CFG_CTRL_CTAG_FILTER;
3227 	}
3228 
3229 	if (changed & NETIF_F_SG) {
3230 		if (features & NETIF_F_SG)
3231 			new_ctrl |= NFP_NET_CFG_CTRL_GATHER;
3232 		else
3233 			new_ctrl &= ~NFP_NET_CFG_CTRL_GATHER;
3234 	}
3235 
3236 	err = nfp_port_set_features(netdev, features);
3237 	if (err)
3238 		return err;
3239 
3240 	nn_dbg(nn, "Feature change 0x%llx -> 0x%llx (changed=0x%llx)\n",
3241 	       netdev->features, features, changed);
3242 
3243 	if (new_ctrl == nn->dp.ctrl)
3244 		return 0;
3245 
3246 	nn_dbg(nn, "NIC ctrl: 0x%x -> 0x%x\n", nn->dp.ctrl, new_ctrl);
3247 	nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
3248 	err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_GEN);
3249 	if (err)
3250 		return err;
3251 
3252 	nn->dp.ctrl = new_ctrl;
3253 
3254 	return 0;
3255 }
3256 
3257 static netdev_features_t
3258 nfp_net_features_check(struct sk_buff *skb, struct net_device *dev,
3259 		       netdev_features_t features)
3260 {
3261 	u8 l4_hdr;
3262 
3263 	/* We can't do TSO over double tagged packets (802.1AD) */
3264 	features &= vlan_features_check(skb, features);
3265 
3266 	if (!skb->encapsulation)
3267 		return features;
3268 
3269 	/* Ensure that inner L4 header offset fits into TX descriptor field */
3270 	if (skb_is_gso(skb)) {
3271 		u32 hdrlen;
3272 
3273 		hdrlen = skb_inner_transport_header(skb) - skb->data +
3274 			inner_tcp_hdrlen(skb);
3275 
3276 		if (unlikely(hdrlen > NFP_NET_LSO_MAX_HDR_SZ))
3277 			features &= ~NETIF_F_GSO_MASK;
3278 	}
3279 
3280 	/* VXLAN/GRE check */
3281 	switch (vlan_get_protocol(skb)) {
3282 	case htons(ETH_P_IP):
3283 		l4_hdr = ip_hdr(skb)->protocol;
3284 		break;
3285 	case htons(ETH_P_IPV6):
3286 		l4_hdr = ipv6_hdr(skb)->nexthdr;
3287 		break;
3288 	default:
3289 		return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
3290 	}
3291 
3292 	if (skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
3293 	    skb->inner_protocol != htons(ETH_P_TEB) ||
3294 	    (l4_hdr != IPPROTO_UDP && l4_hdr != IPPROTO_GRE) ||
3295 	    (l4_hdr == IPPROTO_UDP &&
3296 	     (skb_inner_mac_header(skb) - skb_transport_header(skb) !=
3297 	      sizeof(struct udphdr) + sizeof(struct vxlanhdr))))
3298 		return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
3299 
3300 	return features;
3301 }
3302 
3303 static int
3304 nfp_net_get_phys_port_name(struct net_device *netdev, char *name, size_t len)
3305 {
3306 	struct nfp_net *nn = netdev_priv(netdev);
3307 	int n;
3308 
3309 	if (nn->port)
3310 		return nfp_port_get_phys_port_name(netdev, name, len);
3311 
3312 	if (nn->dp.is_vf || nn->vnic_no_name)
3313 		return -EOPNOTSUPP;
3314 
3315 	n = snprintf(name, len, "n%d", nn->id);
3316 	if (n >= len)
3317 		return -EINVAL;
3318 
3319 	return 0;
3320 }
3321 
3322 /**
3323  * nfp_net_set_vxlan_port() - set vxlan port in SW and reconfigure HW
3324  * @nn:   NFP Net device to reconfigure
3325  * @idx:  Index into the port table where new port should be written
3326  * @port: UDP port to configure (pass zero to remove VXLAN port)
3327  */
3328 static void nfp_net_set_vxlan_port(struct nfp_net *nn, int idx, __be16 port)
3329 {
3330 	int i;
3331 
3332 	nn->vxlan_ports[idx] = port;
3333 
3334 	if (!(nn->dp.ctrl & NFP_NET_CFG_CTRL_VXLAN))
3335 		return;
3336 
3337 	BUILD_BUG_ON(NFP_NET_N_VXLAN_PORTS & 1);
3338 	for (i = 0; i < NFP_NET_N_VXLAN_PORTS; i += 2)
3339 		nn_writel(nn, NFP_NET_CFG_VXLAN_PORT + i * sizeof(port),
3340 			  be16_to_cpu(nn->vxlan_ports[i + 1]) << 16 |
3341 			  be16_to_cpu(nn->vxlan_ports[i]));
3342 
3343 	nfp_net_reconfig_post(nn, NFP_NET_CFG_UPDATE_VXLAN);
3344 }
3345 
3346 /**
3347  * nfp_net_find_vxlan_idx() - find table entry of the port or a free one
3348  * @nn:   NFP Network structure
3349  * @port: UDP port to look for
3350  *
3351  * Return: if the port is already in the table -- it's position;
3352  *	   if the port is not in the table -- free position to use;
3353  *	   if the table is full -- -ENOSPC.
3354  */
3355 static int nfp_net_find_vxlan_idx(struct nfp_net *nn, __be16 port)
3356 {
3357 	int i, free_idx = -ENOSPC;
3358 
3359 	for (i = 0; i < NFP_NET_N_VXLAN_PORTS; i++) {
3360 		if (nn->vxlan_ports[i] == port)
3361 			return i;
3362 		if (!nn->vxlan_usecnt[i])
3363 			free_idx = i;
3364 	}
3365 
3366 	return free_idx;
3367 }
3368 
3369 static void nfp_net_add_vxlan_port(struct net_device *netdev,
3370 				   struct udp_tunnel_info *ti)
3371 {
3372 	struct nfp_net *nn = netdev_priv(netdev);
3373 	int idx;
3374 
3375 	if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3376 		return;
3377 
3378 	idx = nfp_net_find_vxlan_idx(nn, ti->port);
3379 	if (idx == -ENOSPC)
3380 		return;
3381 
3382 	if (!nn->vxlan_usecnt[idx]++)
3383 		nfp_net_set_vxlan_port(nn, idx, ti->port);
3384 }
3385 
3386 static void nfp_net_del_vxlan_port(struct net_device *netdev,
3387 				   struct udp_tunnel_info *ti)
3388 {
3389 	struct nfp_net *nn = netdev_priv(netdev);
3390 	int idx;
3391 
3392 	if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
3393 		return;
3394 
3395 	idx = nfp_net_find_vxlan_idx(nn, ti->port);
3396 	if (idx == -ENOSPC || !nn->vxlan_usecnt[idx])
3397 		return;
3398 
3399 	if (!--nn->vxlan_usecnt[idx])
3400 		nfp_net_set_vxlan_port(nn, idx, 0);
3401 }
3402 
3403 static int nfp_net_xdp_setup_drv(struct nfp_net *nn, struct netdev_bpf *bpf)
3404 {
3405 	struct bpf_prog *prog = bpf->prog;
3406 	struct nfp_net_dp *dp;
3407 	int err;
3408 
3409 	if (!xdp_attachment_flags_ok(&nn->xdp, bpf))
3410 		return -EBUSY;
3411 
3412 	if (!prog == !nn->dp.xdp_prog) {
3413 		WRITE_ONCE(nn->dp.xdp_prog, prog);
3414 		xdp_attachment_setup(&nn->xdp, bpf);
3415 		return 0;
3416 	}
3417 
3418 	dp = nfp_net_clone_dp(nn);
3419 	if (!dp)
3420 		return -ENOMEM;
3421 
3422 	dp->xdp_prog = prog;
3423 	dp->num_tx_rings += prog ? nn->dp.num_rx_rings : -nn->dp.num_rx_rings;
3424 	dp->rx_dma_dir = prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
3425 	dp->rx_dma_off = prog ? XDP_PACKET_HEADROOM - nn->dp.rx_offset : 0;
3426 
3427 	/* We need RX reconfig to remap the buffers (BIDIR vs FROM_DEV) */
3428 	err = nfp_net_ring_reconfig(nn, dp, bpf->extack);
3429 	if (err)
3430 		return err;
3431 
3432 	xdp_attachment_setup(&nn->xdp, bpf);
3433 	return 0;
3434 }
3435 
3436 static int nfp_net_xdp_setup_hw(struct nfp_net *nn, struct netdev_bpf *bpf)
3437 {
3438 	int err;
3439 
3440 	if (!xdp_attachment_flags_ok(&nn->xdp_hw, bpf))
3441 		return -EBUSY;
3442 
3443 	err = nfp_app_xdp_offload(nn->app, nn, bpf->prog, bpf->extack);
3444 	if (err)
3445 		return err;
3446 
3447 	xdp_attachment_setup(&nn->xdp_hw, bpf);
3448 	return 0;
3449 }
3450 
3451 static int nfp_net_xdp(struct net_device *netdev, struct netdev_bpf *xdp)
3452 {
3453 	struct nfp_net *nn = netdev_priv(netdev);
3454 
3455 	switch (xdp->command) {
3456 	case XDP_SETUP_PROG:
3457 		return nfp_net_xdp_setup_drv(nn, xdp);
3458 	case XDP_SETUP_PROG_HW:
3459 		return nfp_net_xdp_setup_hw(nn, xdp);
3460 	case XDP_QUERY_PROG:
3461 		return xdp_attachment_query(&nn->xdp, xdp);
3462 	case XDP_QUERY_PROG_HW:
3463 		return xdp_attachment_query(&nn->xdp_hw, xdp);
3464 	default:
3465 		return nfp_app_bpf(nn->app, nn, xdp);
3466 	}
3467 }
3468 
3469 static int nfp_net_set_mac_address(struct net_device *netdev, void *addr)
3470 {
3471 	struct nfp_net *nn = netdev_priv(netdev);
3472 	struct sockaddr *saddr = addr;
3473 	int err;
3474 
3475 	err = eth_prepare_mac_addr_change(netdev, addr);
3476 	if (err)
3477 		return err;
3478 
3479 	nfp_net_write_mac_addr(nn, saddr->sa_data);
3480 
3481 	err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_MACADDR);
3482 	if (err)
3483 		return err;
3484 
3485 	eth_commit_mac_addr_change(netdev, addr);
3486 
3487 	return 0;
3488 }
3489 
3490 const struct net_device_ops nfp_net_netdev_ops = {
3491 	.ndo_init		= nfp_app_ndo_init,
3492 	.ndo_uninit		= nfp_app_ndo_uninit,
3493 	.ndo_open		= nfp_net_netdev_open,
3494 	.ndo_stop		= nfp_net_netdev_close,
3495 	.ndo_start_xmit		= nfp_net_tx,
3496 	.ndo_get_stats64	= nfp_net_stat64,
3497 	.ndo_vlan_rx_add_vid	= nfp_net_vlan_rx_add_vid,
3498 	.ndo_vlan_rx_kill_vid	= nfp_net_vlan_rx_kill_vid,
3499 	.ndo_set_vf_mac         = nfp_app_set_vf_mac,
3500 	.ndo_set_vf_vlan        = nfp_app_set_vf_vlan,
3501 	.ndo_set_vf_spoofchk    = nfp_app_set_vf_spoofchk,
3502 	.ndo_get_vf_config	= nfp_app_get_vf_config,
3503 	.ndo_set_vf_link_state  = nfp_app_set_vf_link_state,
3504 	.ndo_setup_tc		= nfp_port_setup_tc,
3505 	.ndo_tx_timeout		= nfp_net_tx_timeout,
3506 	.ndo_set_rx_mode	= nfp_net_set_rx_mode,
3507 	.ndo_change_mtu		= nfp_net_change_mtu,
3508 	.ndo_set_mac_address	= nfp_net_set_mac_address,
3509 	.ndo_set_features	= nfp_net_set_features,
3510 	.ndo_features_check	= nfp_net_features_check,
3511 	.ndo_get_phys_port_name	= nfp_net_get_phys_port_name,
3512 	.ndo_udp_tunnel_add	= nfp_net_add_vxlan_port,
3513 	.ndo_udp_tunnel_del	= nfp_net_del_vxlan_port,
3514 	.ndo_bpf		= nfp_net_xdp,
3515 };
3516 
3517 /**
3518  * nfp_net_info() - Print general info about the NIC
3519  * @nn:      NFP Net device to reconfigure
3520  */
3521 void nfp_net_info(struct nfp_net *nn)
3522 {
3523 	nn_info(nn, "Netronome NFP-6xxx %sNetdev: TxQs=%d/%d RxQs=%d/%d\n",
3524 		nn->dp.is_vf ? "VF " : "",
3525 		nn->dp.num_tx_rings, nn->max_tx_rings,
3526 		nn->dp.num_rx_rings, nn->max_rx_rings);
3527 	nn_info(nn, "VER: %d.%d.%d.%d, Maximum supported MTU: %d\n",
3528 		nn->fw_ver.resv, nn->fw_ver.class,
3529 		nn->fw_ver.major, nn->fw_ver.minor,
3530 		nn->max_mtu);
3531 	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",
3532 		nn->cap,
3533 		nn->cap & NFP_NET_CFG_CTRL_PROMISC  ? "PROMISC "  : "",
3534 		nn->cap & NFP_NET_CFG_CTRL_L2BC     ? "L2BCFILT " : "",
3535 		nn->cap & NFP_NET_CFG_CTRL_L2MC     ? "L2MCFILT " : "",
3536 		nn->cap & NFP_NET_CFG_CTRL_RXCSUM   ? "RXCSUM "   : "",
3537 		nn->cap & NFP_NET_CFG_CTRL_TXCSUM   ? "TXCSUM "   : "",
3538 		nn->cap & NFP_NET_CFG_CTRL_RXVLAN   ? "RXVLAN "   : "",
3539 		nn->cap & NFP_NET_CFG_CTRL_TXVLAN   ? "TXVLAN "   : "",
3540 		nn->cap & NFP_NET_CFG_CTRL_SCATTER  ? "SCATTER "  : "",
3541 		nn->cap & NFP_NET_CFG_CTRL_GATHER   ? "GATHER "   : "",
3542 		nn->cap & NFP_NET_CFG_CTRL_LSO      ? "TSO1 "     : "",
3543 		nn->cap & NFP_NET_CFG_CTRL_LSO2     ? "TSO2 "     : "",
3544 		nn->cap & NFP_NET_CFG_CTRL_RSS      ? "RSS1 "     : "",
3545 		nn->cap & NFP_NET_CFG_CTRL_RSS2     ? "RSS2 "     : "",
3546 		nn->cap & NFP_NET_CFG_CTRL_CTAG_FILTER ? "CTAG_FILTER " : "",
3547 		nn->cap & NFP_NET_CFG_CTRL_L2SWITCH ? "L2SWITCH " : "",
3548 		nn->cap & NFP_NET_CFG_CTRL_MSIXAUTO ? "AUTOMASK " : "",
3549 		nn->cap & NFP_NET_CFG_CTRL_IRQMOD   ? "IRQMOD "   : "",
3550 		nn->cap & NFP_NET_CFG_CTRL_VXLAN    ? "VXLAN "    : "",
3551 		nn->cap & NFP_NET_CFG_CTRL_NVGRE    ? "NVGRE "	  : "",
3552 		nn->cap & NFP_NET_CFG_CTRL_CSUM_COMPLETE ?
3553 						      "RXCSUM_COMPLETE " : "",
3554 		nn->cap & NFP_NET_CFG_CTRL_LIVE_ADDR ? "LIVE_ADDR " : "",
3555 		nfp_app_extra_cap(nn->app, nn));
3556 }
3557 
3558 /**
3559  * nfp_net_alloc() - Allocate netdev and related structure
3560  * @pdev:         PCI device
3561  * @ctrl_bar:     PCI IOMEM with vNIC config memory
3562  * @needs_netdev: Whether to allocate a netdev for this vNIC
3563  * @max_tx_rings: Maximum number of TX rings supported by device
3564  * @max_rx_rings: Maximum number of RX rings supported by device
3565  *
3566  * This function allocates a netdev device and fills in the initial
3567  * part of the @struct nfp_net structure.  In case of control device
3568  * nfp_net structure is allocated without the netdev.
3569  *
3570  * Return: NFP Net device structure, or ERR_PTR on error.
3571  */
3572 struct nfp_net *
3573 nfp_net_alloc(struct pci_dev *pdev, void __iomem *ctrl_bar, bool needs_netdev,
3574 	      unsigned int max_tx_rings, unsigned int max_rx_rings)
3575 {
3576 	struct nfp_net *nn;
3577 	int err;
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->dp.ctrl_bar = ctrl_bar;
3598 	nn->pdev = pdev;
3599 
3600 	nn->max_tx_rings = max_tx_rings;
3601 	nn->max_rx_rings = max_rx_rings;
3602 
3603 	nn->dp.num_tx_rings = min_t(unsigned int,
3604 				    max_tx_rings, num_online_cpus());
3605 	nn->dp.num_rx_rings = min_t(unsigned int, max_rx_rings,
3606 				 netif_get_num_default_rss_queues());
3607 
3608 	nn->dp.num_r_vecs = max(nn->dp.num_tx_rings, nn->dp.num_rx_rings);
3609 	nn->dp.num_r_vecs = min_t(unsigned int,
3610 				  nn->dp.num_r_vecs, num_online_cpus());
3611 
3612 	nn->dp.txd_cnt = NFP_NET_TX_DESCS_DEFAULT;
3613 	nn->dp.rxd_cnt = NFP_NET_RX_DESCS_DEFAULT;
3614 
3615 	spin_lock_init(&nn->reconfig_lock);
3616 	spin_lock_init(&nn->link_status_lock);
3617 
3618 	timer_setup(&nn->reconfig_timer, nfp_net_reconfig_timer, 0);
3619 
3620 	err = nfp_net_tlv_caps_parse(&nn->pdev->dev, nn->dp.ctrl_bar,
3621 				     &nn->tlv_caps);
3622 	if (err)
3623 		goto err_free_nn;
3624 
3625 	return nn;
3626 
3627 err_free_nn:
3628 	if (nn->dp.netdev)
3629 		free_netdev(nn->dp.netdev);
3630 	else
3631 		vfree(nn);
3632 	return ERR_PTR(err);
3633 }
3634 
3635 /**
3636  * nfp_net_free() - Undo what @nfp_net_alloc() did
3637  * @nn:      NFP Net device to reconfigure
3638  */
3639 void nfp_net_free(struct nfp_net *nn)
3640 {
3641 	WARN_ON(timer_pending(&nn->reconfig_timer) || nn->reconfig_posted);
3642 	if (nn->dp.netdev)
3643 		free_netdev(nn->dp.netdev);
3644 	else
3645 		vfree(nn);
3646 }
3647 
3648 /**
3649  * nfp_net_rss_key_sz() - Get current size of the RSS key
3650  * @nn:		NFP Net device instance
3651  *
3652  * Return: size of the RSS key for currently selected hash function.
3653  */
3654 unsigned int nfp_net_rss_key_sz(struct nfp_net *nn)
3655 {
3656 	switch (nn->rss_hfunc) {
3657 	case ETH_RSS_HASH_TOP:
3658 		return NFP_NET_CFG_RSS_KEY_SZ;
3659 	case ETH_RSS_HASH_XOR:
3660 		return 0;
3661 	case ETH_RSS_HASH_CRC32:
3662 		return 4;
3663 	}
3664 
3665 	nn_warn(nn, "Unknown hash function: %u\n", nn->rss_hfunc);
3666 	return 0;
3667 }
3668 
3669 /**
3670  * nfp_net_rss_init() - Set the initial RSS parameters
3671  * @nn:	     NFP Net device to reconfigure
3672  */
3673 static void nfp_net_rss_init(struct nfp_net *nn)
3674 {
3675 	unsigned long func_bit, rss_cap_hfunc;
3676 	u32 reg;
3677 
3678 	/* Read the RSS function capability and select first supported func */
3679 	reg = nn_readl(nn, NFP_NET_CFG_RSS_CAP);
3680 	rss_cap_hfunc =	FIELD_GET(NFP_NET_CFG_RSS_CAP_HFUNC, reg);
3681 	if (!rss_cap_hfunc)
3682 		rss_cap_hfunc =	FIELD_GET(NFP_NET_CFG_RSS_CAP_HFUNC,
3683 					  NFP_NET_CFG_RSS_TOEPLITZ);
3684 
3685 	func_bit = find_first_bit(&rss_cap_hfunc, NFP_NET_CFG_RSS_HFUNCS);
3686 	if (func_bit == NFP_NET_CFG_RSS_HFUNCS) {
3687 		dev_warn(nn->dp.dev,
3688 			 "Bad RSS config, defaulting to Toeplitz hash\n");
3689 		func_bit = ETH_RSS_HASH_TOP_BIT;
3690 	}
3691 	nn->rss_hfunc = 1 << func_bit;
3692 
3693 	netdev_rss_key_fill(nn->rss_key, nfp_net_rss_key_sz(nn));
3694 
3695 	nfp_net_rss_init_itbl(nn);
3696 
3697 	/* Enable IPv4/IPv6 TCP by default */
3698 	nn->rss_cfg = NFP_NET_CFG_RSS_IPV4_TCP |
3699 		      NFP_NET_CFG_RSS_IPV6_TCP |
3700 		      FIELD_PREP(NFP_NET_CFG_RSS_HFUNC, nn->rss_hfunc) |
3701 		      NFP_NET_CFG_RSS_MASK;
3702 }
3703 
3704 /**
3705  * nfp_net_irqmod_init() - Set the initial IRQ moderation parameters
3706  * @nn:	     NFP Net device to reconfigure
3707  */
3708 static void nfp_net_irqmod_init(struct nfp_net *nn)
3709 {
3710 	nn->rx_coalesce_usecs      = 50;
3711 	nn->rx_coalesce_max_frames = 64;
3712 	nn->tx_coalesce_usecs      = 50;
3713 	nn->tx_coalesce_max_frames = 64;
3714 }
3715 
3716 static void nfp_net_netdev_init(struct nfp_net *nn)
3717 {
3718 	struct net_device *netdev = nn->dp.netdev;
3719 
3720 	nfp_net_write_mac_addr(nn, nn->dp.netdev->dev_addr);
3721 
3722 	netdev->mtu = nn->dp.mtu;
3723 
3724 	/* Advertise/enable offloads based on capabilities
3725 	 *
3726 	 * Note: netdev->features show the currently enabled features
3727 	 * and netdev->hw_features advertises which features are
3728 	 * supported.  By default we enable most features.
3729 	 */
3730 	if (nn->cap & NFP_NET_CFG_CTRL_LIVE_ADDR)
3731 		netdev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
3732 
3733 	netdev->hw_features = NETIF_F_HIGHDMA;
3734 	if (nn->cap & NFP_NET_CFG_CTRL_RXCSUM_ANY) {
3735 		netdev->hw_features |= NETIF_F_RXCSUM;
3736 		nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_RXCSUM_ANY;
3737 	}
3738 	if (nn->cap & NFP_NET_CFG_CTRL_TXCSUM) {
3739 		netdev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
3740 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_TXCSUM;
3741 	}
3742 	if (nn->cap & NFP_NET_CFG_CTRL_GATHER) {
3743 		netdev->hw_features |= NETIF_F_SG;
3744 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_GATHER;
3745 	}
3746 	if ((nn->cap & NFP_NET_CFG_CTRL_LSO && nn->fw_ver.major > 2) ||
3747 	    nn->cap & NFP_NET_CFG_CTRL_LSO2) {
3748 		netdev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
3749 		nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_LSO2 ?:
3750 					 NFP_NET_CFG_CTRL_LSO;
3751 	}
3752 	if (nn->cap & NFP_NET_CFG_CTRL_RSS_ANY)
3753 		netdev->hw_features |= NETIF_F_RXHASH;
3754 	if (nn->cap & NFP_NET_CFG_CTRL_VXLAN) {
3755 		if (nn->cap & NFP_NET_CFG_CTRL_LSO)
3756 			netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL;
3757 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_VXLAN;
3758 	}
3759 	if (nn->cap & NFP_NET_CFG_CTRL_NVGRE) {
3760 		if (nn->cap & NFP_NET_CFG_CTRL_LSO)
3761 			netdev->hw_features |= NETIF_F_GSO_GRE;
3762 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_NVGRE;
3763 	}
3764 	if (nn->cap & (NFP_NET_CFG_CTRL_VXLAN | NFP_NET_CFG_CTRL_NVGRE))
3765 		netdev->hw_enc_features = netdev->hw_features;
3766 
3767 	netdev->vlan_features = netdev->hw_features;
3768 
3769 	if (nn->cap & NFP_NET_CFG_CTRL_RXVLAN) {
3770 		netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
3771 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_RXVLAN;
3772 	}
3773 	if (nn->cap & NFP_NET_CFG_CTRL_TXVLAN) {
3774 		if (nn->cap & NFP_NET_CFG_CTRL_LSO2) {
3775 			nn_warn(nn, "Device advertises both TSO2 and TXVLAN. Refusing to enable TXVLAN.\n");
3776 		} else {
3777 			netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
3778 			nn->dp.ctrl |= NFP_NET_CFG_CTRL_TXVLAN;
3779 		}
3780 	}
3781 	if (nn->cap & NFP_NET_CFG_CTRL_CTAG_FILTER) {
3782 		netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
3783 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_CTAG_FILTER;
3784 	}
3785 
3786 	netdev->features = netdev->hw_features;
3787 
3788 	if (nfp_app_has_tc(nn->app) && nn->port)
3789 		netdev->hw_features |= NETIF_F_HW_TC;
3790 
3791 	/* Advertise but disable TSO by default. */
3792 	netdev->features &= ~(NETIF_F_TSO | NETIF_F_TSO6);
3793 	nn->dp.ctrl &= ~NFP_NET_CFG_CTRL_LSO_ANY;
3794 
3795 	/* Finalise the netdev setup */
3796 	netdev->netdev_ops = &nfp_net_netdev_ops;
3797 	netdev->watchdog_timeo = msecs_to_jiffies(5 * 1000);
3798 
3799 	SWITCHDEV_SET_OPS(netdev, &nfp_port_switchdev_ops);
3800 
3801 	/* MTU range: 68 - hw-specific max */
3802 	netdev->min_mtu = ETH_MIN_MTU;
3803 	netdev->max_mtu = nn->max_mtu;
3804 
3805 	netdev->gso_max_segs = NFP_NET_LSO_MAX_SEGS;
3806 
3807 	netif_carrier_off(netdev);
3808 
3809 	nfp_net_set_ethtool_ops(netdev);
3810 }
3811 
3812 static int nfp_net_read_caps(struct nfp_net *nn)
3813 {
3814 	/* Get some of the read-only fields from the BAR */
3815 	nn->cap = nn_readl(nn, NFP_NET_CFG_CAP);
3816 	nn->max_mtu = nn_readl(nn, NFP_NET_CFG_MAX_MTU);
3817 
3818 	/* ABI 4.x and ctrl vNIC always use chained metadata, in other cases
3819 	 * we allow use of non-chained metadata if RSS(v1) is the only
3820 	 * advertised capability requiring metadata.
3821 	 */
3822 	nn->dp.chained_metadata_format = nn->fw_ver.major == 4 ||
3823 					 !nn->dp.netdev ||
3824 					 !(nn->cap & NFP_NET_CFG_CTRL_RSS) ||
3825 					 nn->cap & NFP_NET_CFG_CTRL_CHAIN_META;
3826 	/* RSS(v1) uses non-chained metadata format, except in ABI 4.x where
3827 	 * it has the same meaning as RSSv2.
3828 	 */
3829 	if (nn->dp.chained_metadata_format && nn->fw_ver.major != 4)
3830 		nn->cap &= ~NFP_NET_CFG_CTRL_RSS;
3831 
3832 	/* Determine RX packet/metadata boundary offset */
3833 	if (nn->fw_ver.major >= 2) {
3834 		u32 reg;
3835 
3836 		reg = nn_readl(nn, NFP_NET_CFG_RX_OFFSET);
3837 		if (reg > NFP_NET_MAX_PREPEND) {
3838 			nn_err(nn, "Invalid rx offset: %d\n", reg);
3839 			return -EINVAL;
3840 		}
3841 		nn->dp.rx_offset = reg;
3842 	} else {
3843 		nn->dp.rx_offset = NFP_NET_RX_OFFSET;
3844 	}
3845 
3846 	/* For control vNICs mask out the capabilities app doesn't want. */
3847 	if (!nn->dp.netdev)
3848 		nn->cap &= nn->app->type->ctrl_cap_mask;
3849 
3850 	return 0;
3851 }
3852 
3853 /**
3854  * nfp_net_init() - Initialise/finalise the nfp_net structure
3855  * @nn:		NFP Net device structure
3856  *
3857  * Return: 0 on success or negative errno on error.
3858  */
3859 int nfp_net_init(struct nfp_net *nn)
3860 {
3861 	int err;
3862 
3863 	nn->dp.rx_dma_dir = DMA_FROM_DEVICE;
3864 
3865 	err = nfp_net_read_caps(nn);
3866 	if (err)
3867 		return err;
3868 
3869 	/* Set default MTU and Freelist buffer size */
3870 	if (!nfp_net_is_data_vnic(nn) && nn->app->ctrl_mtu) {
3871 		if (nn->app->ctrl_mtu <= nn->max_mtu) {
3872 			nn->dp.mtu = nn->app->ctrl_mtu;
3873 		} else {
3874 			if (nn->app->ctrl_mtu != NFP_APP_CTRL_MTU_MAX)
3875 				nn_warn(nn, "app requested MTU above max supported %u > %u\n",
3876 					nn->app->ctrl_mtu, nn->max_mtu);
3877 			nn->dp.mtu = nn->max_mtu;
3878 		}
3879 	} else if (nn->max_mtu < NFP_NET_DEFAULT_MTU) {
3880 		nn->dp.mtu = nn->max_mtu;
3881 	} else {
3882 		nn->dp.mtu = NFP_NET_DEFAULT_MTU;
3883 	}
3884 	nn->dp.fl_bufsz = nfp_net_calc_fl_bufsz(&nn->dp);
3885 
3886 	if (nfp_app_ctrl_uses_data_vnics(nn->app))
3887 		nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_CMSG_DATA;
3888 
3889 	if (nn->cap & NFP_NET_CFG_CTRL_RSS_ANY) {
3890 		nfp_net_rss_init(nn);
3891 		nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_RSS2 ?:
3892 					 NFP_NET_CFG_CTRL_RSS;
3893 	}
3894 
3895 	/* Allow L2 Broadcast and Multicast through by default, if supported */
3896 	if (nn->cap & NFP_NET_CFG_CTRL_L2BC)
3897 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_L2BC;
3898 
3899 	/* Allow IRQ moderation, if supported */
3900 	if (nn->cap & NFP_NET_CFG_CTRL_IRQMOD) {
3901 		nfp_net_irqmod_init(nn);
3902 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_IRQMOD;
3903 	}
3904 
3905 	if (nn->dp.netdev)
3906 		nfp_net_netdev_init(nn);
3907 
3908 	/* Stash the re-configuration queue away.  First odd queue in TX Bar */
3909 	nn->qcp_cfg = nn->tx_bar + NFP_QCP_QUEUE_ADDR_SZ;
3910 
3911 	/* Make sure the FW knows the netdev is supposed to be disabled here */
3912 	nn_writel(nn, NFP_NET_CFG_CTRL, 0);
3913 	nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, 0);
3914 	nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, 0);
3915 	err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_RING |
3916 				   NFP_NET_CFG_UPDATE_GEN);
3917 	if (err)
3918 		return err;
3919 
3920 	nfp_net_vecs_init(nn);
3921 
3922 	if (!nn->dp.netdev)
3923 		return 0;
3924 	return register_netdev(nn->dp.netdev);
3925 }
3926 
3927 /**
3928  * nfp_net_clean() - Undo what nfp_net_init() did.
3929  * @nn:		NFP Net device structure
3930  */
3931 void nfp_net_clean(struct nfp_net *nn)
3932 {
3933 	if (!nn->dp.netdev)
3934 		return;
3935 
3936 	unregister_netdev(nn->dp.netdev);
3937 	nfp_net_reconfig_wait_posted(nn);
3938 }
3939