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