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