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