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