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