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