xref: /linux/drivers/net/ethernet/netronome/nfp/nfp_net_common.c (revision 4b660dbd9ee2059850fd30e0df420ca7a38a1856)
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2015-2019 Netronome Systems, Inc. */
3 
4 /*
5  * nfp_net_common.c
6  * Netronome network device driver: Common functions between PF and VF
7  * Authors: Jakub Kicinski <jakub.kicinski@netronome.com>
8  *          Jason McMullan <jason.mcmullan@netronome.com>
9  *          Rolf Neugebauer <rolf.neugebauer@netronome.com>
10  *          Brad Petrus <brad.petrus@netronome.com>
11  *          Chris Telfer <chris.telfer@netronome.com>
12  */
13 
14 #include <linux/bitfield.h>
15 #include <linux/bpf.h>
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/fs.h>
20 #include <linux/netdevice.h>
21 #include <linux/etherdevice.h>
22 #include <linux/interrupt.h>
23 #include <linux/ip.h>
24 #include <linux/ipv6.h>
25 #include <linux/mm.h>
26 #include <linux/overflow.h>
27 #include <linux/page_ref.h>
28 #include <linux/pci.h>
29 #include <linux/pci_regs.h>
30 #include <linux/ethtool.h>
31 #include <linux/log2.h>
32 #include <linux/if_vlan.h>
33 #include <linux/if_bridge.h>
34 #include <linux/random.h>
35 #include <linux/vmalloc.h>
36 #include <linux/ktime.h>
37 
38 #include <net/tls.h>
39 #include <net/vxlan.h>
40 #include <net/xdp_sock_drv.h>
41 #include <net/xfrm.h>
42 
43 #include "nfpcore/nfp_dev.h"
44 #include "nfpcore/nfp_nsp.h"
45 #include "ccm.h"
46 #include "nfp_app.h"
47 #include "nfp_net_ctrl.h"
48 #include "nfp_net.h"
49 #include "nfp_net_dp.h"
50 #include "nfp_net_sriov.h"
51 #include "nfp_net_xsk.h"
52 #include "nfp_port.h"
53 #include "crypto/crypto.h"
54 #include "crypto/fw.h"
55 
56 static int nfp_net_mc_unsync(struct net_device *netdev, const unsigned char *addr);
57 
58 /**
59  * nfp_net_get_fw_version() - Read and parse the FW version
60  * @fw_ver:	Output fw_version structure to read to
61  * @ctrl_bar:	Mapped address of the control BAR
62  */
63 void nfp_net_get_fw_version(struct nfp_net_fw_version *fw_ver,
64 			    void __iomem *ctrl_bar)
65 {
66 	u32 reg;
67 
68 	reg = readl(ctrl_bar + NFP_NET_CFG_VERSION);
69 	put_unaligned_le32(reg, fw_ver);
70 }
71 
72 u32 nfp_qcp_queue_offset(const struct nfp_dev_info *dev_info, u16 queue)
73 {
74 	queue &= dev_info->qc_idx_mask;
75 	return dev_info->qc_addr_offset + NFP_QCP_QUEUE_ADDR_SZ * queue;
76 }
77 
78 /* Firmware reconfig
79  *
80  * Firmware reconfig may take a while so we have two versions of it -
81  * synchronous and asynchronous (posted).  All synchronous callers are holding
82  * RTNL so we don't have to worry about serializing them.
83  */
84 static void nfp_net_reconfig_start(struct nfp_net *nn, u32 update)
85 {
86 	nn_writel(nn, NFP_NET_CFG_UPDATE, update);
87 	/* ensure update is written before pinging HW */
88 	nn_pci_flush(nn);
89 	nfp_qcp_wr_ptr_add(nn->qcp_cfg, 1);
90 	nn->reconfig_in_progress_update = update;
91 }
92 
93 /* Pass 0 as update to run posted reconfigs. */
94 static void nfp_net_reconfig_start_async(struct nfp_net *nn, u32 update)
95 {
96 	update |= nn->reconfig_posted;
97 	nn->reconfig_posted = 0;
98 
99 	nfp_net_reconfig_start(nn, update);
100 
101 	nn->reconfig_timer_active = true;
102 	mod_timer(&nn->reconfig_timer, jiffies + NFP_NET_POLL_TIMEOUT * HZ);
103 }
104 
105 static bool nfp_net_reconfig_check_done(struct nfp_net *nn, bool last_check)
106 {
107 	u32 reg;
108 
109 	reg = nn_readl(nn, NFP_NET_CFG_UPDATE);
110 	if (reg == 0)
111 		return true;
112 	if (reg & NFP_NET_CFG_UPDATE_ERR) {
113 		nn_err(nn, "Reconfig error (status: 0x%08x update: 0x%08x ctrl: 0x%08x)\n",
114 		       reg, nn->reconfig_in_progress_update,
115 		       nn_readl(nn, NFP_NET_CFG_CTRL));
116 		return true;
117 	} else if (last_check) {
118 		nn_err(nn, "Reconfig timeout (status: 0x%08x update: 0x%08x ctrl: 0x%08x)\n",
119 		       reg, nn->reconfig_in_progress_update,
120 		       nn_readl(nn, NFP_NET_CFG_CTRL));
121 		return true;
122 	}
123 
124 	return false;
125 }
126 
127 static bool __nfp_net_reconfig_wait(struct nfp_net *nn, unsigned long deadline)
128 {
129 	bool timed_out = false;
130 	int i;
131 
132 	/* Poll update field, waiting for NFP to ack the config.
133 	 * Do an opportunistic wait-busy loop, afterward sleep.
134 	 */
135 	for (i = 0; i < 50; i++) {
136 		if (nfp_net_reconfig_check_done(nn, false))
137 			return false;
138 		udelay(4);
139 	}
140 
141 	while (!nfp_net_reconfig_check_done(nn, timed_out)) {
142 		usleep_range(250, 500);
143 		timed_out = time_is_before_eq_jiffies(deadline);
144 	}
145 
146 	return timed_out;
147 }
148 
149 static int nfp_net_reconfig_wait(struct nfp_net *nn, unsigned long deadline)
150 {
151 	if (__nfp_net_reconfig_wait(nn, deadline))
152 		return -EIO;
153 
154 	if (nn_readl(nn, NFP_NET_CFG_UPDATE) & NFP_NET_CFG_UPDATE_ERR)
155 		return -EIO;
156 
157 	return 0;
158 }
159 
160 static void nfp_net_reconfig_timer(struct timer_list *t)
161 {
162 	struct nfp_net *nn = from_timer(nn, t, reconfig_timer);
163 
164 	spin_lock_bh(&nn->reconfig_lock);
165 
166 	nn->reconfig_timer_active = false;
167 
168 	/* If sync caller is present it will take over from us */
169 	if (nn->reconfig_sync_present)
170 		goto done;
171 
172 	/* Read reconfig status and report errors */
173 	nfp_net_reconfig_check_done(nn, true);
174 
175 	if (nn->reconfig_posted)
176 		nfp_net_reconfig_start_async(nn, 0);
177 done:
178 	spin_unlock_bh(&nn->reconfig_lock);
179 }
180 
181 /**
182  * nfp_net_reconfig_post() - Post async reconfig request
183  * @nn:      NFP Net device to reconfigure
184  * @update:  The value for the update field in the BAR config
185  *
186  * Record FW reconfiguration request.  Reconfiguration will be kicked off
187  * whenever reconfiguration machinery is idle.  Multiple requests can be
188  * merged together!
189  */
190 static void nfp_net_reconfig_post(struct nfp_net *nn, u32 update)
191 {
192 	spin_lock_bh(&nn->reconfig_lock);
193 
194 	/* Sync caller will kick off async reconf when it's done, just post */
195 	if (nn->reconfig_sync_present) {
196 		nn->reconfig_posted |= update;
197 		goto done;
198 	}
199 
200 	/* Opportunistically check if the previous command is done */
201 	if (!nn->reconfig_timer_active ||
202 	    nfp_net_reconfig_check_done(nn, false))
203 		nfp_net_reconfig_start_async(nn, update);
204 	else
205 		nn->reconfig_posted |= update;
206 done:
207 	spin_unlock_bh(&nn->reconfig_lock);
208 }
209 
210 static void nfp_net_reconfig_sync_enter(struct nfp_net *nn)
211 {
212 	bool cancelled_timer = false;
213 	u32 pre_posted_requests;
214 
215 	spin_lock_bh(&nn->reconfig_lock);
216 
217 	WARN_ON(nn->reconfig_sync_present);
218 	nn->reconfig_sync_present = true;
219 
220 	if (nn->reconfig_timer_active) {
221 		nn->reconfig_timer_active = false;
222 		cancelled_timer = true;
223 	}
224 	pre_posted_requests = nn->reconfig_posted;
225 	nn->reconfig_posted = 0;
226 
227 	spin_unlock_bh(&nn->reconfig_lock);
228 
229 	if (cancelled_timer) {
230 		del_timer_sync(&nn->reconfig_timer);
231 		nfp_net_reconfig_wait(nn, nn->reconfig_timer.expires);
232 	}
233 
234 	/* Run the posted reconfigs which were issued before we started */
235 	if (pre_posted_requests) {
236 		nfp_net_reconfig_start(nn, pre_posted_requests);
237 		nfp_net_reconfig_wait(nn, jiffies + HZ * NFP_NET_POLL_TIMEOUT);
238 	}
239 }
240 
241 static void nfp_net_reconfig_wait_posted(struct nfp_net *nn)
242 {
243 	nfp_net_reconfig_sync_enter(nn);
244 
245 	spin_lock_bh(&nn->reconfig_lock);
246 	nn->reconfig_sync_present = false;
247 	spin_unlock_bh(&nn->reconfig_lock);
248 }
249 
250 /**
251  * __nfp_net_reconfig() - Reconfigure the firmware
252  * @nn:      NFP Net device to reconfigure
253  * @update:  The value for the update field in the BAR config
254  *
255  * Write the update word to the BAR and ping the reconfig queue.  The
256  * poll until the firmware has acknowledged the update by zeroing the
257  * update word.
258  *
259  * Return: Negative errno on error, 0 on success
260  */
261 int __nfp_net_reconfig(struct nfp_net *nn, u32 update)
262 {
263 	int ret;
264 
265 	nfp_net_reconfig_sync_enter(nn);
266 
267 	nfp_net_reconfig_start(nn, update);
268 	ret = nfp_net_reconfig_wait(nn, jiffies + HZ * NFP_NET_POLL_TIMEOUT);
269 
270 	spin_lock_bh(&nn->reconfig_lock);
271 
272 	if (nn->reconfig_posted)
273 		nfp_net_reconfig_start_async(nn, 0);
274 
275 	nn->reconfig_sync_present = false;
276 
277 	spin_unlock_bh(&nn->reconfig_lock);
278 
279 	return ret;
280 }
281 
282 int nfp_net_reconfig(struct nfp_net *nn, u32 update)
283 {
284 	int ret;
285 
286 	nn_ctrl_bar_lock(nn);
287 	ret = __nfp_net_reconfig(nn, update);
288 	nn_ctrl_bar_unlock(nn);
289 
290 	return ret;
291 }
292 
293 int nfp_net_mbox_lock(struct nfp_net *nn, unsigned int data_size)
294 {
295 	if (nn->tlv_caps.mbox_len < NFP_NET_CFG_MBOX_SIMPLE_VAL + data_size) {
296 		nn_err(nn, "mailbox too small for %u of data (%u)\n",
297 		       data_size, nn->tlv_caps.mbox_len);
298 		return -EIO;
299 	}
300 
301 	nn_ctrl_bar_lock(nn);
302 	return 0;
303 }
304 
305 /**
306  * nfp_net_mbox_reconfig() - Reconfigure the firmware via the mailbox
307  * @nn:        NFP Net device to reconfigure
308  * @mbox_cmd:  The value for the mailbox command
309  *
310  * Helper function for mailbox updates
311  *
312  * Return: Negative errno on error, 0 on success
313  */
314 int nfp_net_mbox_reconfig(struct nfp_net *nn, u32 mbox_cmd)
315 {
316 	u32 mbox = nn->tlv_caps.mbox_off;
317 	int ret;
318 
319 	nn_writeq(nn, mbox + NFP_NET_CFG_MBOX_SIMPLE_CMD, mbox_cmd);
320 
321 	ret = __nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_MBOX);
322 	if (ret) {
323 		nn_err(nn, "Mailbox update error\n");
324 		return ret;
325 	}
326 
327 	return -nn_readl(nn, mbox + NFP_NET_CFG_MBOX_SIMPLE_RET);
328 }
329 
330 void nfp_net_mbox_reconfig_post(struct nfp_net *nn, u32 mbox_cmd)
331 {
332 	u32 mbox = nn->tlv_caps.mbox_off;
333 
334 	nn_writeq(nn, mbox + NFP_NET_CFG_MBOX_SIMPLE_CMD, mbox_cmd);
335 
336 	nfp_net_reconfig_post(nn, NFP_NET_CFG_UPDATE_MBOX);
337 }
338 
339 int nfp_net_mbox_reconfig_wait_posted(struct nfp_net *nn)
340 {
341 	u32 mbox = nn->tlv_caps.mbox_off;
342 
343 	nfp_net_reconfig_wait_posted(nn);
344 
345 	return -nn_readl(nn, mbox + NFP_NET_CFG_MBOX_SIMPLE_RET);
346 }
347 
348 int nfp_net_mbox_reconfig_and_unlock(struct nfp_net *nn, u32 mbox_cmd)
349 {
350 	int ret;
351 
352 	ret = nfp_net_mbox_reconfig(nn, mbox_cmd);
353 	nn_ctrl_bar_unlock(nn);
354 	return ret;
355 }
356 
357 /* Interrupt configuration and handling
358  */
359 
360 /**
361  * nfp_net_irqs_alloc() - allocates MSI-X irqs
362  * @pdev:        PCI device structure
363  * @irq_entries: Array to be initialized and used to hold the irq entries
364  * @min_irqs:    Minimal acceptable number of interrupts
365  * @wanted_irqs: Target number of interrupts to allocate
366  *
367  * Return: Number of irqs obtained or 0 on error.
368  */
369 unsigned int
370 nfp_net_irqs_alloc(struct pci_dev *pdev, struct msix_entry *irq_entries,
371 		   unsigned int min_irqs, unsigned int wanted_irqs)
372 {
373 	unsigned int i;
374 	int got_irqs;
375 
376 	for (i = 0; i < wanted_irqs; i++)
377 		irq_entries[i].entry = i;
378 
379 	got_irqs = pci_enable_msix_range(pdev, irq_entries,
380 					 min_irqs, wanted_irqs);
381 	if (got_irqs < 0) {
382 		dev_err(&pdev->dev, "Failed to enable %d-%d MSI-X (err=%d)\n",
383 			min_irqs, wanted_irqs, got_irqs);
384 		return 0;
385 	}
386 
387 	if (got_irqs < wanted_irqs)
388 		dev_warn(&pdev->dev, "Unable to allocate %d IRQs got only %d\n",
389 			 wanted_irqs, got_irqs);
390 
391 	return got_irqs;
392 }
393 
394 /**
395  * nfp_net_irqs_assign() - Assign interrupts allocated externally to netdev
396  * @nn:		 NFP Network structure
397  * @irq_entries: Table of allocated interrupts
398  * @n:		 Size of @irq_entries (number of entries to grab)
399  *
400  * After interrupts are allocated with nfp_net_irqs_alloc() this function
401  * should be called to assign them to a specific netdev (port).
402  */
403 void
404 nfp_net_irqs_assign(struct nfp_net *nn, struct msix_entry *irq_entries,
405 		    unsigned int n)
406 {
407 	struct nfp_net_dp *dp = &nn->dp;
408 
409 	nn->max_r_vecs = n - NFP_NET_NON_Q_VECTORS;
410 	dp->num_r_vecs = nn->max_r_vecs;
411 
412 	memcpy(nn->irq_entries, irq_entries, sizeof(*irq_entries) * n);
413 
414 	if (dp->num_rx_rings > dp->num_r_vecs ||
415 	    dp->num_tx_rings > dp->num_r_vecs)
416 		dev_warn(nn->dp.dev, "More rings (%d,%d) than vectors (%d).\n",
417 			 dp->num_rx_rings, dp->num_tx_rings,
418 			 dp->num_r_vecs);
419 
420 	dp->num_rx_rings = min(dp->num_r_vecs, dp->num_rx_rings);
421 	dp->num_tx_rings = min(dp->num_r_vecs, dp->num_tx_rings);
422 	dp->num_stack_tx_rings = dp->num_tx_rings;
423 }
424 
425 /**
426  * nfp_net_irqs_disable() - Disable interrupts
427  * @pdev:        PCI device structure
428  *
429  * Undoes what @nfp_net_irqs_alloc() does.
430  */
431 void nfp_net_irqs_disable(struct pci_dev *pdev)
432 {
433 	pci_disable_msix(pdev);
434 }
435 
436 /**
437  * nfp_net_irq_rxtx() - Interrupt service routine for RX/TX rings.
438  * @irq:      Interrupt
439  * @data:     Opaque data structure
440  *
441  * Return: Indicate if the interrupt has been handled.
442  */
443 static irqreturn_t nfp_net_irq_rxtx(int irq, void *data)
444 {
445 	struct nfp_net_r_vector *r_vec = data;
446 
447 	/* Currently we cannot tell if it's a rx or tx interrupt,
448 	 * since dim does not need accurate event_ctr to calculate,
449 	 * we just use this counter for both rx and tx dim.
450 	 */
451 	r_vec->event_ctr++;
452 
453 	napi_schedule_irqoff(&r_vec->napi);
454 
455 	/* The FW auto-masks any interrupt, either via the MASK bit in
456 	 * the MSI-X table or via the per entry ICR field.  So there
457 	 * is no need to disable interrupts here.
458 	 */
459 	return IRQ_HANDLED;
460 }
461 
462 static irqreturn_t nfp_ctrl_irq_rxtx(int irq, void *data)
463 {
464 	struct nfp_net_r_vector *r_vec = data;
465 
466 	tasklet_schedule(&r_vec->tasklet);
467 
468 	return IRQ_HANDLED;
469 }
470 
471 /**
472  * nfp_net_read_link_status() - Reread link status from control BAR
473  * @nn:       NFP Network structure
474  */
475 static void nfp_net_read_link_status(struct nfp_net *nn)
476 {
477 	unsigned long flags;
478 	bool link_up;
479 	u16 sts;
480 
481 	spin_lock_irqsave(&nn->link_status_lock, flags);
482 
483 	sts = nn_readw(nn, NFP_NET_CFG_STS);
484 	link_up = !!(sts & NFP_NET_CFG_STS_LINK);
485 
486 	if (nn->link_up == link_up)
487 		goto out;
488 
489 	nn->link_up = link_up;
490 	if (nn->port) {
491 		set_bit(NFP_PORT_CHANGED, &nn->port->flags);
492 		if (nn->port->link_cb)
493 			nn->port->link_cb(nn->port);
494 	}
495 
496 	if (nn->link_up) {
497 		netif_carrier_on(nn->dp.netdev);
498 		netdev_info(nn->dp.netdev, "NIC Link is Up\n");
499 	} else {
500 		netif_carrier_off(nn->dp.netdev);
501 		netdev_info(nn->dp.netdev, "NIC Link is Down\n");
502 	}
503 out:
504 	spin_unlock_irqrestore(&nn->link_status_lock, flags);
505 }
506 
507 /**
508  * nfp_net_irq_lsc() - Interrupt service routine for link state changes
509  * @irq:      Interrupt
510  * @data:     Opaque data structure
511  *
512  * Return: Indicate if the interrupt has been handled.
513  */
514 static irqreturn_t nfp_net_irq_lsc(int irq, void *data)
515 {
516 	struct nfp_net *nn = data;
517 	struct msix_entry *entry;
518 
519 	entry = &nn->irq_entries[NFP_NET_IRQ_LSC_IDX];
520 
521 	nfp_net_read_link_status(nn);
522 
523 	nfp_net_irq_unmask(nn, entry->entry);
524 
525 	return IRQ_HANDLED;
526 }
527 
528 /**
529  * nfp_net_irq_exn() - Interrupt service routine for exceptions
530  * @irq:      Interrupt
531  * @data:     Opaque data structure
532  *
533  * Return: Indicate if the interrupt has been handled.
534  */
535 static irqreturn_t nfp_net_irq_exn(int irq, void *data)
536 {
537 	struct nfp_net *nn = data;
538 
539 	nn_err(nn, "%s: UNIMPLEMENTED.\n", __func__);
540 	/* XXX TO BE IMPLEMENTED */
541 	return IRQ_HANDLED;
542 }
543 
544 /**
545  * nfp_net_aux_irq_request() - Request an auxiliary interrupt (LSC or EXN)
546  * @nn:		NFP Network structure
547  * @ctrl_offset: Control BAR offset where IRQ configuration should be written
548  * @format:	printf-style format to construct the interrupt name
549  * @name:	Pointer to allocated space for interrupt name
550  * @name_sz:	Size of space for interrupt name
551  * @vector_idx:	Index of MSI-X vector used for this interrupt
552  * @handler:	IRQ handler to register for this interrupt
553  */
554 static int
555 nfp_net_aux_irq_request(struct nfp_net *nn, u32 ctrl_offset,
556 			const char *format, char *name, size_t name_sz,
557 			unsigned int vector_idx, irq_handler_t handler)
558 {
559 	struct msix_entry *entry;
560 	int err;
561 
562 	entry = &nn->irq_entries[vector_idx];
563 
564 	snprintf(name, name_sz, format, nfp_net_name(nn));
565 	err = request_irq(entry->vector, handler, 0, name, nn);
566 	if (err) {
567 		nn_err(nn, "Failed to request IRQ %d (err=%d).\n",
568 		       entry->vector, err);
569 		return err;
570 	}
571 	nn_writeb(nn, ctrl_offset, entry->entry);
572 	nfp_net_irq_unmask(nn, entry->entry);
573 
574 	return 0;
575 }
576 
577 /**
578  * nfp_net_aux_irq_free() - Free an auxiliary interrupt (LSC or EXN)
579  * @nn:		NFP Network structure
580  * @ctrl_offset: Control BAR offset where IRQ configuration should be written
581  * @vector_idx:	Index of MSI-X vector used for this interrupt
582  */
583 static void nfp_net_aux_irq_free(struct nfp_net *nn, u32 ctrl_offset,
584 				 unsigned int vector_idx)
585 {
586 	nn_writeb(nn, ctrl_offset, 0xff);
587 	nn_pci_flush(nn);
588 	free_irq(nn->irq_entries[vector_idx].vector, nn);
589 }
590 
591 struct sk_buff *
592 nfp_net_tls_tx(struct nfp_net_dp *dp, struct nfp_net_r_vector *r_vec,
593 	       struct sk_buff *skb, u64 *tls_handle, int *nr_frags)
594 {
595 #ifdef CONFIG_TLS_DEVICE
596 	struct nfp_net_tls_offload_ctx *ntls;
597 	struct sk_buff *nskb;
598 	bool resync_pending;
599 	u32 datalen, seq;
600 
601 	if (likely(!dp->ktls_tx))
602 		return skb;
603 	if (!tls_is_skb_tx_device_offloaded(skb))
604 		return skb;
605 
606 	datalen = skb->len - skb_tcp_all_headers(skb);
607 	seq = ntohl(tcp_hdr(skb)->seq);
608 	ntls = tls_driver_ctx(skb->sk, TLS_OFFLOAD_CTX_DIR_TX);
609 	resync_pending = tls_offload_tx_resync_pending(skb->sk);
610 	if (unlikely(resync_pending || ntls->next_seq != seq)) {
611 		/* Pure ACK out of order already */
612 		if (!datalen)
613 			return skb;
614 
615 		u64_stats_update_begin(&r_vec->tx_sync);
616 		r_vec->tls_tx_fallback++;
617 		u64_stats_update_end(&r_vec->tx_sync);
618 
619 		nskb = tls_encrypt_skb(skb);
620 		if (!nskb) {
621 			u64_stats_update_begin(&r_vec->tx_sync);
622 			r_vec->tls_tx_no_fallback++;
623 			u64_stats_update_end(&r_vec->tx_sync);
624 			return NULL;
625 		}
626 		/* encryption wasn't necessary */
627 		if (nskb == skb)
628 			return skb;
629 		/* we don't re-check ring space */
630 		if (unlikely(skb_is_nonlinear(nskb))) {
631 			nn_dp_warn(dp, "tls_encrypt_skb() produced fragmented frame\n");
632 			u64_stats_update_begin(&r_vec->tx_sync);
633 			r_vec->tx_errors++;
634 			u64_stats_update_end(&r_vec->tx_sync);
635 			dev_kfree_skb_any(nskb);
636 			return NULL;
637 		}
638 
639 		/* jump forward, a TX may have gotten lost, need to sync TX */
640 		if (!resync_pending && seq - ntls->next_seq < U32_MAX / 4)
641 			tls_offload_tx_resync_request(nskb->sk, seq,
642 						      ntls->next_seq);
643 
644 		*nr_frags = 0;
645 		return nskb;
646 	}
647 
648 	if (datalen) {
649 		u64_stats_update_begin(&r_vec->tx_sync);
650 		if (!skb_is_gso(skb))
651 			r_vec->hw_tls_tx++;
652 		else
653 			r_vec->hw_tls_tx += skb_shinfo(skb)->gso_segs;
654 		u64_stats_update_end(&r_vec->tx_sync);
655 	}
656 
657 	memcpy(tls_handle, ntls->fw_handle, sizeof(ntls->fw_handle));
658 	ntls->next_seq += datalen;
659 #endif
660 	return skb;
661 }
662 
663 void nfp_net_tls_tx_undo(struct sk_buff *skb, u64 tls_handle)
664 {
665 #ifdef CONFIG_TLS_DEVICE
666 	struct nfp_net_tls_offload_ctx *ntls;
667 	u32 datalen, seq;
668 
669 	if (!tls_handle)
670 		return;
671 	if (WARN_ON_ONCE(!tls_is_skb_tx_device_offloaded(skb)))
672 		return;
673 
674 	datalen = skb->len - skb_tcp_all_headers(skb);
675 	seq = ntohl(tcp_hdr(skb)->seq);
676 
677 	ntls = tls_driver_ctx(skb->sk, TLS_OFFLOAD_CTX_DIR_TX);
678 	if (ntls->next_seq == seq + datalen)
679 		ntls->next_seq = seq;
680 	else
681 		WARN_ON_ONCE(1);
682 #endif
683 }
684 
685 static void nfp_net_tx_timeout(struct net_device *netdev, unsigned int txqueue)
686 {
687 	struct nfp_net *nn = netdev_priv(netdev);
688 
689 	nn_warn(nn, "TX watchdog timeout on ring: %u\n", txqueue);
690 }
691 
692 /* Receive processing */
693 static unsigned int
694 nfp_net_calc_fl_bufsz_data(struct nfp_net_dp *dp)
695 {
696 	unsigned int fl_bufsz = 0;
697 
698 	if (dp->rx_offset == NFP_NET_CFG_RX_OFFSET_DYNAMIC)
699 		fl_bufsz += NFP_NET_MAX_PREPEND;
700 	else
701 		fl_bufsz += dp->rx_offset;
702 	fl_bufsz += ETH_HLEN + VLAN_HLEN * 2 + dp->mtu;
703 
704 	return fl_bufsz;
705 }
706 
707 static unsigned int nfp_net_calc_fl_bufsz(struct nfp_net_dp *dp)
708 {
709 	unsigned int fl_bufsz;
710 
711 	fl_bufsz = NFP_NET_RX_BUF_HEADROOM;
712 	fl_bufsz += dp->rx_dma_off;
713 	fl_bufsz += nfp_net_calc_fl_bufsz_data(dp);
714 
715 	fl_bufsz = SKB_DATA_ALIGN(fl_bufsz);
716 	fl_bufsz += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
717 
718 	return fl_bufsz;
719 }
720 
721 static unsigned int nfp_net_calc_fl_bufsz_xsk(struct nfp_net_dp *dp)
722 {
723 	unsigned int fl_bufsz;
724 
725 	fl_bufsz = XDP_PACKET_HEADROOM;
726 	fl_bufsz += nfp_net_calc_fl_bufsz_data(dp);
727 
728 	return fl_bufsz;
729 }
730 
731 /* Setup and Configuration
732  */
733 
734 /**
735  * nfp_net_vecs_init() - Assign IRQs and setup rvecs.
736  * @nn:		NFP Network structure
737  */
738 static void nfp_net_vecs_init(struct nfp_net *nn)
739 {
740 	int numa_node = dev_to_node(&nn->pdev->dev);
741 	struct nfp_net_r_vector *r_vec;
742 	unsigned int r;
743 
744 	nn->lsc_handler = nfp_net_irq_lsc;
745 	nn->exn_handler = nfp_net_irq_exn;
746 
747 	for (r = 0; r < nn->max_r_vecs; r++) {
748 		struct msix_entry *entry;
749 
750 		entry = &nn->irq_entries[NFP_NET_NON_Q_VECTORS + r];
751 
752 		r_vec = &nn->r_vecs[r];
753 		r_vec->nfp_net = nn;
754 		r_vec->irq_entry = entry->entry;
755 		r_vec->irq_vector = entry->vector;
756 
757 		if (nn->dp.netdev) {
758 			r_vec->handler = nfp_net_irq_rxtx;
759 		} else {
760 			r_vec->handler = nfp_ctrl_irq_rxtx;
761 
762 			__skb_queue_head_init(&r_vec->queue);
763 			spin_lock_init(&r_vec->lock);
764 			tasklet_setup(&r_vec->tasklet, nn->dp.ops->ctrl_poll);
765 			tasklet_disable(&r_vec->tasklet);
766 		}
767 
768 		cpumask_set_cpu(cpumask_local_spread(r, numa_node), &r_vec->affinity_mask);
769 	}
770 }
771 
772 static void
773 nfp_net_napi_add(struct nfp_net_dp *dp, struct nfp_net_r_vector *r_vec, int idx)
774 {
775 	if (dp->netdev)
776 		netif_napi_add(dp->netdev, &r_vec->napi,
777 			       nfp_net_has_xsk_pool_slow(dp, idx) ? dp->ops->xsk_poll : dp->ops->poll);
778 	else
779 		tasklet_enable(&r_vec->tasklet);
780 }
781 
782 static void
783 nfp_net_napi_del(struct nfp_net_dp *dp, struct nfp_net_r_vector *r_vec)
784 {
785 	if (dp->netdev)
786 		netif_napi_del(&r_vec->napi);
787 	else
788 		tasklet_disable(&r_vec->tasklet);
789 }
790 
791 static void
792 nfp_net_vector_assign_rings(struct nfp_net_dp *dp,
793 			    struct nfp_net_r_vector *r_vec, int idx)
794 {
795 	r_vec->rx_ring = idx < dp->num_rx_rings ? &dp->rx_rings[idx] : NULL;
796 	r_vec->tx_ring =
797 		idx < dp->num_stack_tx_rings ? &dp->tx_rings[idx] : NULL;
798 
799 	r_vec->xdp_ring = idx < dp->num_tx_rings - dp->num_stack_tx_rings ?
800 		&dp->tx_rings[dp->num_stack_tx_rings + idx] : NULL;
801 
802 	if (nfp_net_has_xsk_pool_slow(dp, idx) || r_vec->xsk_pool) {
803 		r_vec->xsk_pool = dp->xdp_prog ? dp->xsk_pools[idx] : NULL;
804 
805 		if (r_vec->xsk_pool)
806 			xsk_pool_set_rxq_info(r_vec->xsk_pool,
807 					      &r_vec->rx_ring->xdp_rxq);
808 
809 		nfp_net_napi_del(dp, r_vec);
810 		nfp_net_napi_add(dp, r_vec, idx);
811 	}
812 }
813 
814 static int
815 nfp_net_prepare_vector(struct nfp_net *nn, struct nfp_net_r_vector *r_vec,
816 		       int idx)
817 {
818 	int err;
819 
820 	nfp_net_napi_add(&nn->dp, r_vec, idx);
821 
822 	snprintf(r_vec->name, sizeof(r_vec->name),
823 		 "%s-rxtx-%d", nfp_net_name(nn), idx);
824 	err = request_irq(r_vec->irq_vector, r_vec->handler, 0, r_vec->name,
825 			  r_vec);
826 	if (err) {
827 		nfp_net_napi_del(&nn->dp, r_vec);
828 		nn_err(nn, "Error requesting IRQ %d\n", r_vec->irq_vector);
829 		return err;
830 	}
831 	disable_irq(r_vec->irq_vector);
832 
833 	irq_set_affinity_hint(r_vec->irq_vector, &r_vec->affinity_mask);
834 
835 	nn_dbg(nn, "RV%02d: irq=%03d/%03d\n", idx, r_vec->irq_vector,
836 	       r_vec->irq_entry);
837 
838 	return 0;
839 }
840 
841 static void
842 nfp_net_cleanup_vector(struct nfp_net *nn, struct nfp_net_r_vector *r_vec)
843 {
844 	irq_set_affinity_hint(r_vec->irq_vector, NULL);
845 	nfp_net_napi_del(&nn->dp, r_vec);
846 	free_irq(r_vec->irq_vector, r_vec);
847 }
848 
849 /**
850  * nfp_net_rss_write_itbl() - Write RSS indirection table to device
851  * @nn:      NFP Net device to reconfigure
852  */
853 void nfp_net_rss_write_itbl(struct nfp_net *nn)
854 {
855 	int i;
856 
857 	for (i = 0; i < NFP_NET_CFG_RSS_ITBL_SZ; i += 4)
858 		nn_writel(nn, NFP_NET_CFG_RSS_ITBL + i,
859 			  get_unaligned_le32(nn->rss_itbl + i));
860 }
861 
862 /**
863  * nfp_net_rss_write_key() - Write RSS hash key to device
864  * @nn:      NFP Net device to reconfigure
865  */
866 void nfp_net_rss_write_key(struct nfp_net *nn)
867 {
868 	int i;
869 
870 	for (i = 0; i < nfp_net_rss_key_sz(nn); i += 4)
871 		nn_writel(nn, NFP_NET_CFG_RSS_KEY + i,
872 			  get_unaligned_le32(nn->rss_key + i));
873 }
874 
875 /**
876  * nfp_net_coalesce_write_cfg() - Write irq coalescence configuration to HW
877  * @nn:      NFP Net device to reconfigure
878  */
879 void nfp_net_coalesce_write_cfg(struct nfp_net *nn)
880 {
881 	u8 i;
882 	u32 factor;
883 	u32 value;
884 
885 	/* Compute factor used to convert coalesce '_usecs' parameters to
886 	 * ME timestamp ticks.  There are 16 ME clock cycles for each timestamp
887 	 * count.
888 	 */
889 	factor = nn->tlv_caps.me_freq_mhz / 16;
890 
891 	/* copy RX interrupt coalesce parameters */
892 	value = (nn->rx_coalesce_max_frames << 16) |
893 		(factor * nn->rx_coalesce_usecs);
894 	for (i = 0; i < nn->dp.num_rx_rings; i++)
895 		nn_writel(nn, NFP_NET_CFG_RXR_IRQ_MOD(i), value);
896 
897 	/* copy TX interrupt coalesce parameters */
898 	value = (nn->tx_coalesce_max_frames << 16) |
899 		(factor * nn->tx_coalesce_usecs);
900 	for (i = 0; i < nn->dp.num_tx_rings; i++)
901 		nn_writel(nn, NFP_NET_CFG_TXR_IRQ_MOD(i), value);
902 }
903 
904 /**
905  * nfp_net_write_mac_addr() - Write mac address to the device control BAR
906  * @nn:      NFP Net device to reconfigure
907  * @addr:    MAC address to write
908  *
909  * Writes the MAC address from the netdev to the device control BAR.  Does not
910  * perform the required reconfig.  We do a bit of byte swapping dance because
911  * firmware is LE.
912  */
913 static void nfp_net_write_mac_addr(struct nfp_net *nn, const u8 *addr)
914 {
915 	nn_writel(nn, NFP_NET_CFG_MACADDR + 0, get_unaligned_be32(addr));
916 	nn_writew(nn, NFP_NET_CFG_MACADDR + 6, get_unaligned_be16(addr + 4));
917 }
918 
919 /**
920  * nfp_net_clear_config_and_disable() - Clear control BAR and disable NFP
921  * @nn:      NFP Net device to reconfigure
922  *
923  * Warning: must be fully idempotent.
924  */
925 static void nfp_net_clear_config_and_disable(struct nfp_net *nn)
926 {
927 	u32 new_ctrl, new_ctrl_w1, update;
928 	unsigned int r;
929 	int err;
930 
931 	new_ctrl = nn->dp.ctrl;
932 	new_ctrl &= ~NFP_NET_CFG_CTRL_ENABLE;
933 	update = NFP_NET_CFG_UPDATE_GEN;
934 	update |= NFP_NET_CFG_UPDATE_MSIX;
935 	update |= NFP_NET_CFG_UPDATE_RING;
936 
937 	if (nn->cap & NFP_NET_CFG_CTRL_RINGCFG)
938 		new_ctrl &= ~NFP_NET_CFG_CTRL_RINGCFG;
939 
940 	if (!(nn->cap_w1 & NFP_NET_CFG_CTRL_FREELIST_EN)) {
941 		nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, 0);
942 		nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, 0);
943 	}
944 
945 	nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
946 	err = nfp_net_reconfig(nn, update);
947 	if (err)
948 		nn_err(nn, "Could not disable device: %d\n", err);
949 
950 	if (nn->cap_w1 & NFP_NET_CFG_CTRL_FREELIST_EN) {
951 		new_ctrl_w1 = nn->dp.ctrl_w1;
952 		new_ctrl_w1 &= ~NFP_NET_CFG_CTRL_FREELIST_EN;
953 		nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, 0);
954 		nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, 0);
955 
956 		nn_writel(nn, NFP_NET_CFG_CTRL_WORD1, new_ctrl_w1);
957 		err = nfp_net_reconfig(nn, update);
958 		if (err)
959 			nn_err(nn, "Could not disable FREELIST_EN: %d\n", err);
960 		nn->dp.ctrl_w1 = new_ctrl_w1;
961 	}
962 
963 	for (r = 0; r < nn->dp.num_rx_rings; r++) {
964 		nfp_net_rx_ring_reset(&nn->dp.rx_rings[r]);
965 		if (nfp_net_has_xsk_pool_slow(&nn->dp, nn->dp.rx_rings[r].idx))
966 			nfp_net_xsk_rx_bufs_free(&nn->dp.rx_rings[r]);
967 	}
968 	for (r = 0; r < nn->dp.num_tx_rings; r++)
969 		nfp_net_tx_ring_reset(&nn->dp, &nn->dp.tx_rings[r]);
970 	for (r = 0; r < nn->dp.num_r_vecs; r++)
971 		nfp_net_vec_clear_ring_data(nn, r);
972 
973 	nn->dp.ctrl = new_ctrl;
974 }
975 
976 /**
977  * nfp_net_set_config_and_enable() - Write control BAR and enable NFP
978  * @nn:      NFP Net device to reconfigure
979  */
980 static int nfp_net_set_config_and_enable(struct nfp_net *nn)
981 {
982 	u32 bufsz, new_ctrl, new_ctrl_w1, update = 0;
983 	unsigned int r;
984 	int err;
985 
986 	new_ctrl = nn->dp.ctrl;
987 	new_ctrl_w1 = nn->dp.ctrl_w1;
988 
989 	if (nn->dp.ctrl & NFP_NET_CFG_CTRL_RSS_ANY) {
990 		nfp_net_rss_write_key(nn);
991 		nfp_net_rss_write_itbl(nn);
992 		nn_writel(nn, NFP_NET_CFG_RSS_CTRL, nn->rss_cfg);
993 		update |= NFP_NET_CFG_UPDATE_RSS;
994 	}
995 
996 	if (nn->dp.ctrl & NFP_NET_CFG_CTRL_IRQMOD) {
997 		nfp_net_coalesce_write_cfg(nn);
998 		update |= NFP_NET_CFG_UPDATE_IRQMOD;
999 	}
1000 
1001 	for (r = 0; r < nn->dp.num_tx_rings; r++)
1002 		nfp_net_tx_ring_hw_cfg_write(nn, &nn->dp.tx_rings[r], r);
1003 	for (r = 0; r < nn->dp.num_rx_rings; r++)
1004 		nfp_net_rx_ring_hw_cfg_write(nn, &nn->dp.rx_rings[r], r);
1005 
1006 	nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE,
1007 		  U64_MAX >> (64 - nn->dp.num_tx_rings));
1008 
1009 	nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE,
1010 		  U64_MAX >> (64 - nn->dp.num_rx_rings));
1011 
1012 	if (nn->dp.netdev)
1013 		nfp_net_write_mac_addr(nn, nn->dp.netdev->dev_addr);
1014 
1015 	nn_writel(nn, NFP_NET_CFG_MTU, nn->dp.mtu);
1016 
1017 	bufsz = nn->dp.fl_bufsz - nn->dp.rx_dma_off - NFP_NET_RX_BUF_NON_DATA;
1018 	nn_writel(nn, NFP_NET_CFG_FLBUFSZ, bufsz);
1019 
1020 	/* Enable device
1021 	 * Step 1: Replace the CTRL_ENABLE by NFP_NET_CFG_CTRL_FREELIST_EN if
1022 	 * FREELIST_EN exits.
1023 	 */
1024 	if (nn->cap_w1 & NFP_NET_CFG_CTRL_FREELIST_EN)
1025 		new_ctrl_w1 |= NFP_NET_CFG_CTRL_FREELIST_EN;
1026 	else
1027 		new_ctrl |= NFP_NET_CFG_CTRL_ENABLE;
1028 	update |= NFP_NET_CFG_UPDATE_GEN;
1029 	update |= NFP_NET_CFG_UPDATE_MSIX;
1030 	update |= NFP_NET_CFG_UPDATE_RING;
1031 	if (nn->cap & NFP_NET_CFG_CTRL_RINGCFG)
1032 		new_ctrl |= NFP_NET_CFG_CTRL_RINGCFG;
1033 
1034 	/* Step 2: Send the configuration and write the freelist.
1035 	 * - The freelist only need to be written once.
1036 	 */
1037 	nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
1038 	nn_writel(nn, NFP_NET_CFG_CTRL_WORD1, new_ctrl_w1);
1039 	err = nfp_net_reconfig(nn, update);
1040 	if (err) {
1041 		nfp_net_clear_config_and_disable(nn);
1042 		return err;
1043 	}
1044 
1045 	nn->dp.ctrl = new_ctrl;
1046 	nn->dp.ctrl_w1 = new_ctrl_w1;
1047 
1048 	for (r = 0; r < nn->dp.num_rx_rings; r++)
1049 		nfp_net_rx_ring_fill_freelist(&nn->dp, &nn->dp.rx_rings[r]);
1050 
1051 	/* Step 3: Do the NFP_NET_CFG_CTRL_ENABLE. Send the configuration.
1052 	 */
1053 	if (nn->cap_w1 & NFP_NET_CFG_CTRL_FREELIST_EN) {
1054 		new_ctrl |= NFP_NET_CFG_CTRL_ENABLE;
1055 		nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
1056 
1057 		err = nfp_net_reconfig(nn, update);
1058 		if (err) {
1059 			nfp_net_clear_config_and_disable(nn);
1060 			return err;
1061 		}
1062 		nn->dp.ctrl = new_ctrl;
1063 	}
1064 
1065 	return 0;
1066 }
1067 
1068 /**
1069  * nfp_net_close_stack() - Quiesce the stack (part of close)
1070  * @nn:	     NFP Net device to reconfigure
1071  */
1072 static void nfp_net_close_stack(struct nfp_net *nn)
1073 {
1074 	struct nfp_net_r_vector *r_vec;
1075 	unsigned int r;
1076 
1077 	disable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
1078 	netif_carrier_off(nn->dp.netdev);
1079 	nn->link_up = false;
1080 
1081 	for (r = 0; r < nn->dp.num_r_vecs; r++) {
1082 		r_vec = &nn->r_vecs[r];
1083 
1084 		disable_irq(r_vec->irq_vector);
1085 		napi_disable(&r_vec->napi);
1086 
1087 		if (r_vec->rx_ring)
1088 			cancel_work_sync(&r_vec->rx_dim.work);
1089 
1090 		if (r_vec->tx_ring)
1091 			cancel_work_sync(&r_vec->tx_dim.work);
1092 	}
1093 
1094 	netif_tx_disable(nn->dp.netdev);
1095 }
1096 
1097 /**
1098  * nfp_net_close_free_all() - Free all runtime resources
1099  * @nn:      NFP Net device to reconfigure
1100  */
1101 static void nfp_net_close_free_all(struct nfp_net *nn)
1102 {
1103 	unsigned int r;
1104 
1105 	nfp_net_tx_rings_free(&nn->dp);
1106 	nfp_net_rx_rings_free(&nn->dp);
1107 
1108 	for (r = 0; r < nn->dp.num_r_vecs; r++)
1109 		nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
1110 
1111 	nfp_net_aux_irq_free(nn, NFP_NET_CFG_LSC, NFP_NET_IRQ_LSC_IDX);
1112 	nfp_net_aux_irq_free(nn, NFP_NET_CFG_EXN, NFP_NET_IRQ_EXN_IDX);
1113 }
1114 
1115 /**
1116  * nfp_net_netdev_close() - Called when the device is downed
1117  * @netdev:      netdev structure
1118  */
1119 static int nfp_net_netdev_close(struct net_device *netdev)
1120 {
1121 	struct nfp_net *nn = netdev_priv(netdev);
1122 
1123 	/* Step 1: Disable RX and TX rings from the Linux kernel perspective
1124 	 */
1125 	nfp_net_close_stack(nn);
1126 
1127 	/* Step 2: Tell NFP
1128 	 */
1129 	if (nn->cap_w1 & NFP_NET_CFG_CTRL_MCAST_FILTER)
1130 		__dev_mc_unsync(netdev, nfp_net_mc_unsync);
1131 
1132 	nfp_net_clear_config_and_disable(nn);
1133 	nfp_port_configure(netdev, false);
1134 
1135 	/* Step 3: Free resources
1136 	 */
1137 	nfp_net_close_free_all(nn);
1138 
1139 	nn_dbg(nn, "%s down", netdev->name);
1140 	return 0;
1141 }
1142 
1143 void nfp_ctrl_close(struct nfp_net *nn)
1144 {
1145 	int r;
1146 
1147 	rtnl_lock();
1148 
1149 	for (r = 0; r < nn->dp.num_r_vecs; r++) {
1150 		disable_irq(nn->r_vecs[r].irq_vector);
1151 		tasklet_disable(&nn->r_vecs[r].tasklet);
1152 	}
1153 
1154 	nfp_net_clear_config_and_disable(nn);
1155 
1156 	nfp_net_close_free_all(nn);
1157 
1158 	rtnl_unlock();
1159 }
1160 
1161 static void nfp_net_rx_dim_work(struct work_struct *work)
1162 {
1163 	struct nfp_net_r_vector *r_vec;
1164 	unsigned int factor, value;
1165 	struct dim_cq_moder moder;
1166 	struct nfp_net *nn;
1167 	struct dim *dim;
1168 
1169 	dim = container_of(work, struct dim, work);
1170 	moder = net_dim_get_rx_moderation(dim->mode, dim->profile_ix);
1171 	r_vec = container_of(dim, struct nfp_net_r_vector, rx_dim);
1172 	nn = r_vec->nfp_net;
1173 
1174 	/* Compute factor used to convert coalesce '_usecs' parameters to
1175 	 * ME timestamp ticks.  There are 16 ME clock cycles for each timestamp
1176 	 * count.
1177 	 */
1178 	factor = nn->tlv_caps.me_freq_mhz / 16;
1179 	if (nfp_net_coalesce_para_check(factor * moder.usec) ||
1180 	    nfp_net_coalesce_para_check(moder.pkts))
1181 		return;
1182 
1183 	/* copy RX interrupt coalesce parameters */
1184 	value = (moder.pkts << 16) | (factor * moder.usec);
1185 	nn_writel(nn, NFP_NET_CFG_RXR_IRQ_MOD(r_vec->rx_ring->idx), value);
1186 	(void)nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_IRQMOD);
1187 
1188 	dim->state = DIM_START_MEASURE;
1189 }
1190 
1191 static void nfp_net_tx_dim_work(struct work_struct *work)
1192 {
1193 	struct nfp_net_r_vector *r_vec;
1194 	unsigned int factor, value;
1195 	struct dim_cq_moder moder;
1196 	struct nfp_net *nn;
1197 	struct dim *dim;
1198 
1199 	dim = container_of(work, struct dim, work);
1200 	moder = net_dim_get_tx_moderation(dim->mode, dim->profile_ix);
1201 	r_vec = container_of(dim, struct nfp_net_r_vector, tx_dim);
1202 	nn = r_vec->nfp_net;
1203 
1204 	/* Compute factor used to convert coalesce '_usecs' parameters to
1205 	 * ME timestamp ticks.  There are 16 ME clock cycles for each timestamp
1206 	 * count.
1207 	 */
1208 	factor = nn->tlv_caps.me_freq_mhz / 16;
1209 	if (nfp_net_coalesce_para_check(factor * moder.usec) ||
1210 	    nfp_net_coalesce_para_check(moder.pkts))
1211 		return;
1212 
1213 	/* copy TX interrupt coalesce parameters */
1214 	value = (moder.pkts << 16) | (factor * moder.usec);
1215 	nn_writel(nn, NFP_NET_CFG_TXR_IRQ_MOD(r_vec->tx_ring->idx), value);
1216 	(void)nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_IRQMOD);
1217 
1218 	dim->state = DIM_START_MEASURE;
1219 }
1220 
1221 /**
1222  * nfp_net_open_stack() - Start the device from stack's perspective
1223  * @nn:      NFP Net device to reconfigure
1224  */
1225 static void nfp_net_open_stack(struct nfp_net *nn)
1226 {
1227 	struct nfp_net_r_vector *r_vec;
1228 	unsigned int r;
1229 
1230 	for (r = 0; r < nn->dp.num_r_vecs; r++) {
1231 		r_vec = &nn->r_vecs[r];
1232 
1233 		if (r_vec->rx_ring) {
1234 			INIT_WORK(&r_vec->rx_dim.work, nfp_net_rx_dim_work);
1235 			r_vec->rx_dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
1236 		}
1237 
1238 		if (r_vec->tx_ring) {
1239 			INIT_WORK(&r_vec->tx_dim.work, nfp_net_tx_dim_work);
1240 			r_vec->tx_dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
1241 		}
1242 
1243 		napi_enable(&r_vec->napi);
1244 		enable_irq(r_vec->irq_vector);
1245 	}
1246 
1247 	netif_tx_wake_all_queues(nn->dp.netdev);
1248 
1249 	enable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
1250 	nfp_net_read_link_status(nn);
1251 }
1252 
1253 static int nfp_net_open_alloc_all(struct nfp_net *nn)
1254 {
1255 	int err, r;
1256 
1257 	err = nfp_net_aux_irq_request(nn, NFP_NET_CFG_EXN, "%s-exn",
1258 				      nn->exn_name, sizeof(nn->exn_name),
1259 				      NFP_NET_IRQ_EXN_IDX, nn->exn_handler);
1260 	if (err)
1261 		return err;
1262 	err = nfp_net_aux_irq_request(nn, NFP_NET_CFG_LSC, "%s-lsc",
1263 				      nn->lsc_name, sizeof(nn->lsc_name),
1264 				      NFP_NET_IRQ_LSC_IDX, nn->lsc_handler);
1265 	if (err)
1266 		goto err_free_exn;
1267 	disable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
1268 
1269 	for (r = 0; r < nn->dp.num_r_vecs; r++) {
1270 		err = nfp_net_prepare_vector(nn, &nn->r_vecs[r], r);
1271 		if (err)
1272 			goto err_cleanup_vec_p;
1273 	}
1274 
1275 	err = nfp_net_rx_rings_prepare(nn, &nn->dp);
1276 	if (err)
1277 		goto err_cleanup_vec;
1278 
1279 	err = nfp_net_tx_rings_prepare(nn, &nn->dp);
1280 	if (err)
1281 		goto err_free_rx_rings;
1282 
1283 	for (r = 0; r < nn->max_r_vecs; r++)
1284 		nfp_net_vector_assign_rings(&nn->dp, &nn->r_vecs[r], r);
1285 
1286 	return 0;
1287 
1288 err_free_rx_rings:
1289 	nfp_net_rx_rings_free(&nn->dp);
1290 err_cleanup_vec:
1291 	r = nn->dp.num_r_vecs;
1292 err_cleanup_vec_p:
1293 	while (r--)
1294 		nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
1295 	nfp_net_aux_irq_free(nn, NFP_NET_CFG_LSC, NFP_NET_IRQ_LSC_IDX);
1296 err_free_exn:
1297 	nfp_net_aux_irq_free(nn, NFP_NET_CFG_EXN, NFP_NET_IRQ_EXN_IDX);
1298 	return err;
1299 }
1300 
1301 static int nfp_net_netdev_open(struct net_device *netdev)
1302 {
1303 	struct nfp_net *nn = netdev_priv(netdev);
1304 	int err;
1305 
1306 	/* Step 1: Allocate resources for rings and the like
1307 	 * - Request interrupts
1308 	 * - Allocate RX and TX ring resources
1309 	 * - Setup initial RSS table
1310 	 */
1311 	err = nfp_net_open_alloc_all(nn);
1312 	if (err)
1313 		return err;
1314 
1315 	err = netif_set_real_num_tx_queues(netdev, nn->dp.num_stack_tx_rings);
1316 	if (err)
1317 		goto err_free_all;
1318 
1319 	err = netif_set_real_num_rx_queues(netdev, nn->dp.num_rx_rings);
1320 	if (err)
1321 		goto err_free_all;
1322 
1323 	/* Step 2: Configure the NFP
1324 	 * - Ifup the physical interface if it exists
1325 	 * - Enable rings from 0 to tx_rings/rx_rings - 1.
1326 	 * - Write MAC address (in case it changed)
1327 	 * - Set the MTU
1328 	 * - Set the Freelist buffer size
1329 	 * - Enable the FW
1330 	 */
1331 	err = nfp_port_configure(netdev, true);
1332 	if (err)
1333 		goto err_free_all;
1334 
1335 	err = nfp_net_set_config_and_enable(nn);
1336 	if (err)
1337 		goto err_port_disable;
1338 
1339 	/* Step 3: Enable for kernel
1340 	 * - put some freelist descriptors on each RX ring
1341 	 * - enable NAPI on each ring
1342 	 * - enable all TX queues
1343 	 * - set link state
1344 	 */
1345 	nfp_net_open_stack(nn);
1346 
1347 	return 0;
1348 
1349 err_port_disable:
1350 	nfp_port_configure(netdev, false);
1351 err_free_all:
1352 	nfp_net_close_free_all(nn);
1353 	return err;
1354 }
1355 
1356 int nfp_ctrl_open(struct nfp_net *nn)
1357 {
1358 	int err, r;
1359 
1360 	/* ring dumping depends on vNICs being opened/closed under rtnl */
1361 	rtnl_lock();
1362 
1363 	err = nfp_net_open_alloc_all(nn);
1364 	if (err)
1365 		goto err_unlock;
1366 
1367 	err = nfp_net_set_config_and_enable(nn);
1368 	if (err)
1369 		goto err_free_all;
1370 
1371 	for (r = 0; r < nn->dp.num_r_vecs; r++)
1372 		enable_irq(nn->r_vecs[r].irq_vector);
1373 
1374 	rtnl_unlock();
1375 
1376 	return 0;
1377 
1378 err_free_all:
1379 	nfp_net_close_free_all(nn);
1380 err_unlock:
1381 	rtnl_unlock();
1382 	return err;
1383 }
1384 
1385 int nfp_net_sched_mbox_amsg_work(struct nfp_net *nn, u32 cmd, const void *data, size_t len,
1386 				 int (*cb)(struct nfp_net *, struct nfp_mbox_amsg_entry *))
1387 {
1388 	struct nfp_mbox_amsg_entry *entry;
1389 
1390 	entry = kmalloc(sizeof(*entry) + len, GFP_ATOMIC);
1391 	if (!entry)
1392 		return -ENOMEM;
1393 
1394 	memcpy(entry->msg, data, len);
1395 	entry->cmd = cmd;
1396 	entry->cfg = cb;
1397 
1398 	spin_lock_bh(&nn->mbox_amsg.lock);
1399 	list_add_tail(&entry->list, &nn->mbox_amsg.list);
1400 	spin_unlock_bh(&nn->mbox_amsg.lock);
1401 
1402 	schedule_work(&nn->mbox_amsg.work);
1403 
1404 	return 0;
1405 }
1406 
1407 static void nfp_net_mbox_amsg_work(struct work_struct *work)
1408 {
1409 	struct nfp_net *nn = container_of(work, struct nfp_net, mbox_amsg.work);
1410 	struct nfp_mbox_amsg_entry *entry, *tmp;
1411 	struct list_head tmp_list;
1412 
1413 	INIT_LIST_HEAD(&tmp_list);
1414 
1415 	spin_lock_bh(&nn->mbox_amsg.lock);
1416 	list_splice_init(&nn->mbox_amsg.list, &tmp_list);
1417 	spin_unlock_bh(&nn->mbox_amsg.lock);
1418 
1419 	list_for_each_entry_safe(entry, tmp, &tmp_list, list) {
1420 		int err = entry->cfg(nn, entry);
1421 
1422 		if (err)
1423 			nn_err(nn, "Config cmd %d to HW failed %d.\n", entry->cmd, err);
1424 
1425 		list_del(&entry->list);
1426 		kfree(entry);
1427 	}
1428 }
1429 
1430 static int nfp_net_mc_cfg(struct nfp_net *nn, struct nfp_mbox_amsg_entry *entry)
1431 {
1432 	unsigned char *addr = entry->msg;
1433 	int ret;
1434 
1435 	ret = nfp_net_mbox_lock(nn, NFP_NET_CFG_MULTICAST_SZ);
1436 	if (ret)
1437 		return ret;
1438 
1439 	nn_writel(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_MULTICAST_MAC_HI,
1440 		  get_unaligned_be32(addr));
1441 	nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_MULTICAST_MAC_LO,
1442 		  get_unaligned_be16(addr + 4));
1443 
1444 	return nfp_net_mbox_reconfig_and_unlock(nn, entry->cmd);
1445 }
1446 
1447 static int nfp_net_mc_sync(struct net_device *netdev, const unsigned char *addr)
1448 {
1449 	struct nfp_net *nn = netdev_priv(netdev);
1450 
1451 	if (netdev_mc_count(netdev) > NFP_NET_CFG_MAC_MC_MAX) {
1452 		nn_err(nn, "Requested number of MC addresses (%d) exceeds maximum (%d).\n",
1453 		       netdev_mc_count(netdev), NFP_NET_CFG_MAC_MC_MAX);
1454 		return -EINVAL;
1455 	}
1456 
1457 	return nfp_net_sched_mbox_amsg_work(nn, NFP_NET_CFG_MBOX_CMD_MULTICAST_ADD, addr,
1458 					    NFP_NET_CFG_MULTICAST_SZ, nfp_net_mc_cfg);
1459 }
1460 
1461 static int nfp_net_mc_unsync(struct net_device *netdev, const unsigned char *addr)
1462 {
1463 	struct nfp_net *nn = netdev_priv(netdev);
1464 
1465 	return nfp_net_sched_mbox_amsg_work(nn, NFP_NET_CFG_MBOX_CMD_MULTICAST_DEL, addr,
1466 					    NFP_NET_CFG_MULTICAST_SZ, nfp_net_mc_cfg);
1467 }
1468 
1469 static void nfp_net_set_rx_mode(struct net_device *netdev)
1470 {
1471 	struct nfp_net *nn = netdev_priv(netdev);
1472 	u32 new_ctrl, new_ctrl_w1;
1473 
1474 	new_ctrl = nn->dp.ctrl;
1475 	new_ctrl_w1 = nn->dp.ctrl_w1;
1476 
1477 	if (!netdev_mc_empty(netdev) || netdev->flags & IFF_ALLMULTI)
1478 		new_ctrl |= nn->cap & NFP_NET_CFG_CTRL_L2MC;
1479 	else
1480 		new_ctrl &= ~NFP_NET_CFG_CTRL_L2MC;
1481 
1482 	if (netdev->flags & IFF_ALLMULTI)
1483 		new_ctrl_w1 &= ~NFP_NET_CFG_CTRL_MCAST_FILTER;
1484 	else
1485 		new_ctrl_w1 |= nn->cap_w1 & NFP_NET_CFG_CTRL_MCAST_FILTER;
1486 
1487 	if (netdev->flags & IFF_PROMISC) {
1488 		if (nn->cap & NFP_NET_CFG_CTRL_PROMISC)
1489 			new_ctrl |= NFP_NET_CFG_CTRL_PROMISC;
1490 		else
1491 			nn_warn(nn, "FW does not support promiscuous mode\n");
1492 	} else {
1493 		new_ctrl &= ~NFP_NET_CFG_CTRL_PROMISC;
1494 	}
1495 
1496 	if ((nn->cap_w1 & NFP_NET_CFG_CTRL_MCAST_FILTER) &&
1497 	    __dev_mc_sync(netdev, nfp_net_mc_sync, nfp_net_mc_unsync))
1498 		netdev_err(netdev, "Sync mc address failed\n");
1499 
1500 	if (new_ctrl == nn->dp.ctrl && new_ctrl_w1 == nn->dp.ctrl_w1)
1501 		return;
1502 
1503 	if (new_ctrl != nn->dp.ctrl)
1504 		nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
1505 	if (new_ctrl_w1 != nn->dp.ctrl_w1)
1506 		nn_writel(nn, NFP_NET_CFG_CTRL_WORD1, new_ctrl_w1);
1507 	nfp_net_reconfig_post(nn, NFP_NET_CFG_UPDATE_GEN);
1508 
1509 	nn->dp.ctrl = new_ctrl;
1510 	nn->dp.ctrl_w1 = new_ctrl_w1;
1511 }
1512 
1513 static void nfp_net_rss_init_itbl(struct nfp_net *nn)
1514 {
1515 	int i;
1516 
1517 	for (i = 0; i < sizeof(nn->rss_itbl); i++)
1518 		nn->rss_itbl[i] =
1519 			ethtool_rxfh_indir_default(i, nn->dp.num_rx_rings);
1520 }
1521 
1522 static void nfp_net_dp_swap(struct nfp_net *nn, struct nfp_net_dp *dp)
1523 {
1524 	struct nfp_net_dp new_dp = *dp;
1525 
1526 	*dp = nn->dp;
1527 	nn->dp = new_dp;
1528 
1529 	nn->dp.netdev->mtu = new_dp.mtu;
1530 
1531 	if (!netif_is_rxfh_configured(nn->dp.netdev))
1532 		nfp_net_rss_init_itbl(nn);
1533 }
1534 
1535 static int nfp_net_dp_swap_enable(struct nfp_net *nn, struct nfp_net_dp *dp)
1536 {
1537 	unsigned int r;
1538 	int err;
1539 
1540 	nfp_net_dp_swap(nn, dp);
1541 
1542 	for (r = 0; r <	nn->max_r_vecs; r++)
1543 		nfp_net_vector_assign_rings(&nn->dp, &nn->r_vecs[r], r);
1544 
1545 	err = netif_set_real_num_queues(nn->dp.netdev,
1546 					nn->dp.num_stack_tx_rings,
1547 					nn->dp.num_rx_rings);
1548 	if (err)
1549 		return err;
1550 
1551 	return nfp_net_set_config_and_enable(nn);
1552 }
1553 
1554 struct nfp_net_dp *nfp_net_clone_dp(struct nfp_net *nn)
1555 {
1556 	struct nfp_net_dp *new;
1557 
1558 	new = kmalloc(sizeof(*new), GFP_KERNEL);
1559 	if (!new)
1560 		return NULL;
1561 
1562 	*new = nn->dp;
1563 
1564 	new->xsk_pools = kmemdup(new->xsk_pools,
1565 				 array_size(nn->max_r_vecs,
1566 					    sizeof(new->xsk_pools)),
1567 				 GFP_KERNEL);
1568 	if (!new->xsk_pools) {
1569 		kfree(new);
1570 		return NULL;
1571 	}
1572 
1573 	/* Clear things which need to be recomputed */
1574 	new->fl_bufsz = 0;
1575 	new->tx_rings = NULL;
1576 	new->rx_rings = NULL;
1577 	new->num_r_vecs = 0;
1578 	new->num_stack_tx_rings = 0;
1579 	new->txrwb = NULL;
1580 	new->txrwb_dma = 0;
1581 
1582 	return new;
1583 }
1584 
1585 static void nfp_net_free_dp(struct nfp_net_dp *dp)
1586 {
1587 	kfree(dp->xsk_pools);
1588 	kfree(dp);
1589 }
1590 
1591 static int
1592 nfp_net_check_config(struct nfp_net *nn, struct nfp_net_dp *dp,
1593 		     struct netlink_ext_ack *extack)
1594 {
1595 	unsigned int r, xsk_min_fl_bufsz;
1596 
1597 	/* XDP-enabled tests */
1598 	if (!dp->xdp_prog)
1599 		return 0;
1600 	if (dp->fl_bufsz > PAGE_SIZE) {
1601 		NL_SET_ERR_MSG_MOD(extack, "MTU too large w/ XDP enabled");
1602 		return -EINVAL;
1603 	}
1604 	if (dp->num_tx_rings > nn->max_tx_rings) {
1605 		NL_SET_ERR_MSG_MOD(extack, "Insufficient number of TX rings w/ XDP enabled");
1606 		return -EINVAL;
1607 	}
1608 
1609 	xsk_min_fl_bufsz = nfp_net_calc_fl_bufsz_xsk(dp);
1610 	for (r = 0; r < nn->max_r_vecs; r++) {
1611 		if (!dp->xsk_pools[r])
1612 			continue;
1613 
1614 		if (xsk_pool_get_rx_frame_size(dp->xsk_pools[r]) < xsk_min_fl_bufsz) {
1615 			NL_SET_ERR_MSG_MOD(extack,
1616 					   "XSK buffer pool chunk size too small");
1617 			return -EINVAL;
1618 		}
1619 	}
1620 
1621 	return 0;
1622 }
1623 
1624 int nfp_net_ring_reconfig(struct nfp_net *nn, struct nfp_net_dp *dp,
1625 			  struct netlink_ext_ack *extack)
1626 {
1627 	int r, err;
1628 
1629 	dp->fl_bufsz = nfp_net_calc_fl_bufsz(dp);
1630 
1631 	dp->num_stack_tx_rings = dp->num_tx_rings;
1632 	if (dp->xdp_prog)
1633 		dp->num_stack_tx_rings -= dp->num_rx_rings;
1634 
1635 	dp->num_r_vecs = max(dp->num_rx_rings, dp->num_stack_tx_rings);
1636 
1637 	err = nfp_net_check_config(nn, dp, extack);
1638 	if (err)
1639 		goto exit_free_dp;
1640 
1641 	if (!netif_running(dp->netdev)) {
1642 		nfp_net_dp_swap(nn, dp);
1643 		err = 0;
1644 		goto exit_free_dp;
1645 	}
1646 
1647 	/* Prepare new rings */
1648 	for (r = nn->dp.num_r_vecs; r < dp->num_r_vecs; r++) {
1649 		err = nfp_net_prepare_vector(nn, &nn->r_vecs[r], r);
1650 		if (err) {
1651 			dp->num_r_vecs = r;
1652 			goto err_cleanup_vecs;
1653 		}
1654 	}
1655 
1656 	err = nfp_net_rx_rings_prepare(nn, dp);
1657 	if (err)
1658 		goto err_cleanup_vecs;
1659 
1660 	err = nfp_net_tx_rings_prepare(nn, dp);
1661 	if (err)
1662 		goto err_free_rx;
1663 
1664 	/* Stop device, swap in new rings, try to start the firmware */
1665 	nfp_net_close_stack(nn);
1666 	nfp_net_clear_config_and_disable(nn);
1667 
1668 	err = nfp_net_dp_swap_enable(nn, dp);
1669 	if (err) {
1670 		int err2;
1671 
1672 		nfp_net_clear_config_and_disable(nn);
1673 
1674 		/* Try with old configuration and old rings */
1675 		err2 = nfp_net_dp_swap_enable(nn, dp);
1676 		if (err2)
1677 			nn_err(nn, "Can't restore ring config - FW communication failed (%d,%d)\n",
1678 			       err, err2);
1679 	}
1680 	for (r = dp->num_r_vecs - 1; r >= nn->dp.num_r_vecs; r--)
1681 		nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
1682 
1683 	nfp_net_rx_rings_free(dp);
1684 	nfp_net_tx_rings_free(dp);
1685 
1686 	nfp_net_open_stack(nn);
1687 exit_free_dp:
1688 	nfp_net_free_dp(dp);
1689 
1690 	return err;
1691 
1692 err_free_rx:
1693 	nfp_net_rx_rings_free(dp);
1694 err_cleanup_vecs:
1695 	for (r = dp->num_r_vecs - 1; r >= nn->dp.num_r_vecs; r--)
1696 		nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
1697 	nfp_net_free_dp(dp);
1698 	return err;
1699 }
1700 
1701 static int nfp_net_change_mtu(struct net_device *netdev, int new_mtu)
1702 {
1703 	struct nfp_net *nn = netdev_priv(netdev);
1704 	struct nfp_net_dp *dp;
1705 	int err;
1706 
1707 	err = nfp_app_check_mtu(nn->app, netdev, new_mtu);
1708 	if (err)
1709 		return err;
1710 
1711 	dp = nfp_net_clone_dp(nn);
1712 	if (!dp)
1713 		return -ENOMEM;
1714 
1715 	dp->mtu = new_mtu;
1716 
1717 	return nfp_net_ring_reconfig(nn, dp, NULL);
1718 }
1719 
1720 static int
1721 nfp_net_vlan_rx_add_vid(struct net_device *netdev, __be16 proto, u16 vid)
1722 {
1723 	const u32 cmd = NFP_NET_CFG_MBOX_CMD_CTAG_FILTER_ADD;
1724 	struct nfp_net *nn = netdev_priv(netdev);
1725 	int err;
1726 
1727 	/* Priority tagged packets with vlan id 0 are processed by the
1728 	 * NFP as untagged packets
1729 	 */
1730 	if (!vid)
1731 		return 0;
1732 
1733 	err = nfp_net_mbox_lock(nn, NFP_NET_CFG_VLAN_FILTER_SZ);
1734 	if (err)
1735 		return err;
1736 
1737 	nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_VLAN_FILTER_VID, vid);
1738 	nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_VLAN_FILTER_PROTO,
1739 		  ETH_P_8021Q);
1740 
1741 	return nfp_net_mbox_reconfig_and_unlock(nn, cmd);
1742 }
1743 
1744 static int
1745 nfp_net_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto, u16 vid)
1746 {
1747 	const u32 cmd = NFP_NET_CFG_MBOX_CMD_CTAG_FILTER_KILL;
1748 	struct nfp_net *nn = netdev_priv(netdev);
1749 	int err;
1750 
1751 	/* Priority tagged packets with vlan id 0 are processed by the
1752 	 * NFP as untagged packets
1753 	 */
1754 	if (!vid)
1755 		return 0;
1756 
1757 	err = nfp_net_mbox_lock(nn, NFP_NET_CFG_VLAN_FILTER_SZ);
1758 	if (err)
1759 		return err;
1760 
1761 	nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_VLAN_FILTER_VID, vid);
1762 	nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_VLAN_FILTER_PROTO,
1763 		  ETH_P_8021Q);
1764 
1765 	return nfp_net_mbox_reconfig_and_unlock(nn, cmd);
1766 }
1767 
1768 static void
1769 nfp_net_fs_fill_v4(struct nfp_net *nn, struct nfp_fs_entry *entry, u32 op, u32 *addr)
1770 {
1771 	unsigned int i;
1772 
1773 	union {
1774 		struct {
1775 			__be16 loc;
1776 			u8 k_proto, m_proto;
1777 			__be32 k_sip, m_sip, k_dip, m_dip;
1778 			__be16 k_sport, m_sport, k_dport, m_dport;
1779 		};
1780 		__be32 val[7];
1781 	} v4_rule;
1782 
1783 	nn_writel(nn, *addr, op);
1784 	*addr += sizeof(u32);
1785 
1786 	v4_rule.loc     = cpu_to_be16(entry->loc);
1787 	v4_rule.k_proto = entry->key.l4_proto;
1788 	v4_rule.m_proto = entry->msk.l4_proto;
1789 	v4_rule.k_sip   = entry->key.sip4;
1790 	v4_rule.m_sip   = entry->msk.sip4;
1791 	v4_rule.k_dip   = entry->key.dip4;
1792 	v4_rule.m_dip   = entry->msk.dip4;
1793 	v4_rule.k_sport = entry->key.sport;
1794 	v4_rule.m_sport = entry->msk.sport;
1795 	v4_rule.k_dport = entry->key.dport;
1796 	v4_rule.m_dport = entry->msk.dport;
1797 
1798 	for (i = 0; i < ARRAY_SIZE(v4_rule.val); i++, *addr += sizeof(__be32))
1799 		nn_writel(nn, *addr, be32_to_cpu(v4_rule.val[i]));
1800 }
1801 
1802 static void
1803 nfp_net_fs_fill_v6(struct nfp_net *nn, struct nfp_fs_entry *entry, u32 op, u32 *addr)
1804 {
1805 	unsigned int i;
1806 
1807 	union {
1808 		struct {
1809 			__be16 loc;
1810 			u8 k_proto, m_proto;
1811 			__be32 k_sip[4], m_sip[4], k_dip[4], m_dip[4];
1812 			__be16 k_sport, m_sport, k_dport, m_dport;
1813 		};
1814 		__be32 val[19];
1815 	} v6_rule;
1816 
1817 	nn_writel(nn, *addr, op);
1818 	*addr += sizeof(u32);
1819 
1820 	v6_rule.loc     = cpu_to_be16(entry->loc);
1821 	v6_rule.k_proto = entry->key.l4_proto;
1822 	v6_rule.m_proto = entry->msk.l4_proto;
1823 	for (i = 0; i < 4; i++) {
1824 		v6_rule.k_sip[i] = entry->key.sip6[i];
1825 		v6_rule.m_sip[i] = entry->msk.sip6[i];
1826 		v6_rule.k_dip[i] = entry->key.dip6[i];
1827 		v6_rule.m_dip[i] = entry->msk.dip6[i];
1828 	}
1829 	v6_rule.k_sport = entry->key.sport;
1830 	v6_rule.m_sport = entry->msk.sport;
1831 	v6_rule.k_dport = entry->key.dport;
1832 	v6_rule.m_dport = entry->msk.dport;
1833 
1834 	for (i = 0; i < ARRAY_SIZE(v6_rule.val); i++, *addr += sizeof(__be32))
1835 		nn_writel(nn, *addr, be32_to_cpu(v6_rule.val[i]));
1836 }
1837 
1838 #define NFP_FS_QUEUE_ID	GENMASK(22, 16)
1839 #define NFP_FS_ACT	GENMASK(15, 0)
1840 #define NFP_FS_ACT_DROP	BIT(0)
1841 #define NFP_FS_ACT_Q	BIT(1)
1842 static void
1843 nfp_net_fs_fill_act(struct nfp_net *nn, struct nfp_fs_entry *entry, u32 addr)
1844 {
1845 	u32 action = 0; /* 0 means default passthrough */
1846 
1847 	if (entry->action == RX_CLS_FLOW_DISC)
1848 		action = NFP_FS_ACT_DROP;
1849 	else if (!(entry->flow_type & FLOW_RSS))
1850 		action = FIELD_PREP(NFP_FS_QUEUE_ID, entry->action) | NFP_FS_ACT_Q;
1851 
1852 	nn_writel(nn, addr, action);
1853 }
1854 
1855 int nfp_net_fs_add_hw(struct nfp_net *nn, struct nfp_fs_entry *entry)
1856 {
1857 	u32 addr = nn->tlv_caps.mbox_off + NFP_NET_CFG_MBOX_SIMPLE_VAL;
1858 	int err;
1859 
1860 	err = nfp_net_mbox_lock(nn, NFP_NET_CFG_FS_SZ);
1861 	if (err)
1862 		return err;
1863 
1864 	switch (entry->flow_type & ~FLOW_RSS) {
1865 	case TCP_V4_FLOW:
1866 	case UDP_V4_FLOW:
1867 	case SCTP_V4_FLOW:
1868 	case IPV4_USER_FLOW:
1869 		nfp_net_fs_fill_v4(nn, entry, NFP_NET_CFG_MBOX_CMD_FS_ADD_V4, &addr);
1870 		break;
1871 	case TCP_V6_FLOW:
1872 	case UDP_V6_FLOW:
1873 	case SCTP_V6_FLOW:
1874 	case IPV6_USER_FLOW:
1875 		nfp_net_fs_fill_v6(nn, entry, NFP_NET_CFG_MBOX_CMD_FS_ADD_V6, &addr);
1876 		break;
1877 	case ETHER_FLOW:
1878 		nn_writel(nn, addr, NFP_NET_CFG_MBOX_CMD_FS_ADD_ETHTYPE);
1879 		addr += sizeof(u32);
1880 		nn_writew(nn, addr, be16_to_cpu(entry->key.l3_proto));
1881 		addr += sizeof(u32);
1882 		break;
1883 	}
1884 
1885 	nfp_net_fs_fill_act(nn, entry, addr);
1886 
1887 	err = nfp_net_mbox_reconfig_and_unlock(nn, NFP_NET_CFG_MBOX_CMD_FLOW_STEER);
1888 	if (err) {
1889 		nn_err(nn, "Add new fs rule failed with %d\n", err);
1890 		return -EIO;
1891 	}
1892 
1893 	return 0;
1894 }
1895 
1896 int nfp_net_fs_del_hw(struct nfp_net *nn, struct nfp_fs_entry *entry)
1897 {
1898 	u32 addr = nn->tlv_caps.mbox_off + NFP_NET_CFG_MBOX_SIMPLE_VAL;
1899 	int err;
1900 
1901 	err = nfp_net_mbox_lock(nn, NFP_NET_CFG_FS_SZ);
1902 	if (err)
1903 		return err;
1904 
1905 	switch (entry->flow_type & ~FLOW_RSS) {
1906 	case TCP_V4_FLOW:
1907 	case UDP_V4_FLOW:
1908 	case SCTP_V4_FLOW:
1909 	case IPV4_USER_FLOW:
1910 		nfp_net_fs_fill_v4(nn, entry, NFP_NET_CFG_MBOX_CMD_FS_DEL_V4, &addr);
1911 		break;
1912 	case TCP_V6_FLOW:
1913 	case UDP_V6_FLOW:
1914 	case SCTP_V6_FLOW:
1915 	case IPV6_USER_FLOW:
1916 		nfp_net_fs_fill_v6(nn, entry, NFP_NET_CFG_MBOX_CMD_FS_DEL_V6, &addr);
1917 		break;
1918 	case ETHER_FLOW:
1919 		nn_writel(nn, addr, NFP_NET_CFG_MBOX_CMD_FS_DEL_ETHTYPE);
1920 		addr += sizeof(u32);
1921 		nn_writew(nn, addr, be16_to_cpu(entry->key.l3_proto));
1922 		addr += sizeof(u32);
1923 		break;
1924 	}
1925 
1926 	nfp_net_fs_fill_act(nn, entry, addr);
1927 
1928 	err = nfp_net_mbox_reconfig_and_unlock(nn, NFP_NET_CFG_MBOX_CMD_FLOW_STEER);
1929 	if (err) {
1930 		nn_err(nn, "Delete fs rule failed with %d\n", err);
1931 		return -EIO;
1932 	}
1933 
1934 	return 0;
1935 }
1936 
1937 static void nfp_net_fs_clean(struct nfp_net *nn)
1938 {
1939 	struct nfp_fs_entry *entry, *tmp;
1940 
1941 	list_for_each_entry_safe(entry, tmp, &nn->fs.list, node) {
1942 		nfp_net_fs_del_hw(nn, entry);
1943 		list_del(&entry->node);
1944 		kfree(entry);
1945 	}
1946 }
1947 
1948 static void nfp_net_stat64(struct net_device *netdev,
1949 			   struct rtnl_link_stats64 *stats)
1950 {
1951 	struct nfp_net *nn = netdev_priv(netdev);
1952 	int r;
1953 
1954 	/* Collect software stats */
1955 	for (r = 0; r < nn->max_r_vecs; r++) {
1956 		struct nfp_net_r_vector *r_vec = &nn->r_vecs[r];
1957 		u64 data[3];
1958 		unsigned int start;
1959 
1960 		do {
1961 			start = u64_stats_fetch_begin(&r_vec->rx_sync);
1962 			data[0] = r_vec->rx_pkts;
1963 			data[1] = r_vec->rx_bytes;
1964 			data[2] = r_vec->rx_drops;
1965 		} while (u64_stats_fetch_retry(&r_vec->rx_sync, start));
1966 		stats->rx_packets += data[0];
1967 		stats->rx_bytes += data[1];
1968 		stats->rx_dropped += data[2];
1969 
1970 		do {
1971 			start = u64_stats_fetch_begin(&r_vec->tx_sync);
1972 			data[0] = r_vec->tx_pkts;
1973 			data[1] = r_vec->tx_bytes;
1974 			data[2] = r_vec->tx_errors;
1975 		} while (u64_stats_fetch_retry(&r_vec->tx_sync, start));
1976 		stats->tx_packets += data[0];
1977 		stats->tx_bytes += data[1];
1978 		stats->tx_errors += data[2];
1979 	}
1980 
1981 	/* Add in device stats */
1982 	stats->multicast += nn_readq(nn, NFP_NET_CFG_STATS_RX_MC_FRAMES);
1983 	stats->rx_dropped += nn_readq(nn, NFP_NET_CFG_STATS_RX_DISCARDS);
1984 	stats->rx_errors += nn_readq(nn, NFP_NET_CFG_STATS_RX_ERRORS);
1985 
1986 	stats->tx_dropped += nn_readq(nn, NFP_NET_CFG_STATS_TX_DISCARDS);
1987 	stats->tx_errors += nn_readq(nn, NFP_NET_CFG_STATS_TX_ERRORS);
1988 }
1989 
1990 static int nfp_net_set_features(struct net_device *netdev,
1991 				netdev_features_t features)
1992 {
1993 	netdev_features_t changed = netdev->features ^ features;
1994 	struct nfp_net *nn = netdev_priv(netdev);
1995 	u32 new_ctrl;
1996 	int err;
1997 
1998 	/* Assume this is not called with features we have not advertised */
1999 
2000 	new_ctrl = nn->dp.ctrl;
2001 
2002 	if (changed & NETIF_F_RXCSUM) {
2003 		if (features & NETIF_F_RXCSUM)
2004 			new_ctrl |= nn->cap & NFP_NET_CFG_CTRL_RXCSUM_ANY;
2005 		else
2006 			new_ctrl &= ~NFP_NET_CFG_CTRL_RXCSUM_ANY;
2007 	}
2008 
2009 	if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
2010 		if (features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))
2011 			new_ctrl |= NFP_NET_CFG_CTRL_TXCSUM;
2012 		else
2013 			new_ctrl &= ~NFP_NET_CFG_CTRL_TXCSUM;
2014 	}
2015 
2016 	if (changed & (NETIF_F_TSO | NETIF_F_TSO6)) {
2017 		if (features & (NETIF_F_TSO | NETIF_F_TSO6))
2018 			new_ctrl |= nn->cap & NFP_NET_CFG_CTRL_LSO2 ?:
2019 					      NFP_NET_CFG_CTRL_LSO;
2020 		else
2021 			new_ctrl &= ~NFP_NET_CFG_CTRL_LSO_ANY;
2022 	}
2023 
2024 	if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
2025 		if (features & NETIF_F_HW_VLAN_CTAG_RX)
2026 			new_ctrl |= nn->cap & NFP_NET_CFG_CTRL_RXVLAN_V2 ?:
2027 				    NFP_NET_CFG_CTRL_RXVLAN;
2028 		else
2029 			new_ctrl &= ~NFP_NET_CFG_CTRL_RXVLAN_ANY;
2030 	}
2031 
2032 	if (changed & NETIF_F_HW_VLAN_CTAG_TX) {
2033 		if (features & NETIF_F_HW_VLAN_CTAG_TX)
2034 			new_ctrl |= nn->cap & NFP_NET_CFG_CTRL_TXVLAN_V2 ?:
2035 				    NFP_NET_CFG_CTRL_TXVLAN;
2036 		else
2037 			new_ctrl &= ~NFP_NET_CFG_CTRL_TXVLAN_ANY;
2038 	}
2039 
2040 	if (changed & NETIF_F_HW_VLAN_CTAG_FILTER) {
2041 		if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
2042 			new_ctrl |= NFP_NET_CFG_CTRL_CTAG_FILTER;
2043 		else
2044 			new_ctrl &= ~NFP_NET_CFG_CTRL_CTAG_FILTER;
2045 	}
2046 
2047 	if (changed & NETIF_F_HW_VLAN_STAG_RX) {
2048 		if (features & NETIF_F_HW_VLAN_STAG_RX)
2049 			new_ctrl |= NFP_NET_CFG_CTRL_RXQINQ;
2050 		else
2051 			new_ctrl &= ~NFP_NET_CFG_CTRL_RXQINQ;
2052 	}
2053 
2054 	if (changed & NETIF_F_SG) {
2055 		if (features & NETIF_F_SG)
2056 			new_ctrl |= NFP_NET_CFG_CTRL_GATHER;
2057 		else
2058 			new_ctrl &= ~NFP_NET_CFG_CTRL_GATHER;
2059 	}
2060 
2061 	err = nfp_port_set_features(netdev, features);
2062 	if (err)
2063 		return err;
2064 
2065 	nn_dbg(nn, "Feature change 0x%llx -> 0x%llx (changed=0x%llx)\n",
2066 	       netdev->features, features, changed);
2067 
2068 	if (new_ctrl == nn->dp.ctrl)
2069 		return 0;
2070 
2071 	nn_dbg(nn, "NIC ctrl: 0x%x -> 0x%x\n", nn->dp.ctrl, new_ctrl);
2072 	nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
2073 	err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_GEN);
2074 	if (err)
2075 		return err;
2076 
2077 	nn->dp.ctrl = new_ctrl;
2078 
2079 	return 0;
2080 }
2081 
2082 static netdev_features_t
2083 nfp_net_fix_features(struct net_device *netdev,
2084 		     netdev_features_t features)
2085 {
2086 	if ((features & NETIF_F_HW_VLAN_CTAG_RX) &&
2087 	    (features & NETIF_F_HW_VLAN_STAG_RX)) {
2088 		if (netdev->features & NETIF_F_HW_VLAN_CTAG_RX) {
2089 			features &= ~NETIF_F_HW_VLAN_CTAG_RX;
2090 			netdev->wanted_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
2091 			netdev_warn(netdev,
2092 				    "S-tag and C-tag stripping can't be enabled at the same time. Enabling S-tag stripping and disabling C-tag stripping\n");
2093 		} else if (netdev->features & NETIF_F_HW_VLAN_STAG_RX) {
2094 			features &= ~NETIF_F_HW_VLAN_STAG_RX;
2095 			netdev->wanted_features &= ~NETIF_F_HW_VLAN_STAG_RX;
2096 			netdev_warn(netdev,
2097 				    "S-tag and C-tag stripping can't be enabled at the same time. Enabling C-tag stripping and disabling S-tag stripping\n");
2098 		}
2099 	}
2100 	return features;
2101 }
2102 
2103 static netdev_features_t
2104 nfp_net_features_check(struct sk_buff *skb, struct net_device *dev,
2105 		       netdev_features_t features)
2106 {
2107 	u8 l4_hdr;
2108 
2109 	/* We can't do TSO over double tagged packets (802.1AD) */
2110 	features &= vlan_features_check(skb, features);
2111 
2112 	if (!skb->encapsulation)
2113 		return features;
2114 
2115 	/* Ensure that inner L4 header offset fits into TX descriptor field */
2116 	if (skb_is_gso(skb)) {
2117 		u32 hdrlen;
2118 
2119 		if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4)
2120 			hdrlen = skb_inner_transport_offset(skb) + sizeof(struct udphdr);
2121 		else
2122 			hdrlen = skb_inner_tcp_all_headers(skb);
2123 
2124 		/* Assume worst case scenario of having longest possible
2125 		 * metadata prepend - 8B
2126 		 */
2127 		if (unlikely(hdrlen > NFP_NET_LSO_MAX_HDR_SZ - 8))
2128 			features &= ~NETIF_F_GSO_MASK;
2129 	}
2130 
2131 	if (xfrm_offload(skb))
2132 		return features;
2133 
2134 	/* VXLAN/GRE check */
2135 	switch (vlan_get_protocol(skb)) {
2136 	case htons(ETH_P_IP):
2137 		l4_hdr = ip_hdr(skb)->protocol;
2138 		break;
2139 	case htons(ETH_P_IPV6):
2140 		l4_hdr = ipv6_hdr(skb)->nexthdr;
2141 		break;
2142 	default:
2143 		return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2144 	}
2145 
2146 	if (skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
2147 	    skb->inner_protocol != htons(ETH_P_TEB) ||
2148 	    (l4_hdr != IPPROTO_UDP && l4_hdr != IPPROTO_GRE) ||
2149 	    (l4_hdr == IPPROTO_UDP &&
2150 	     (skb_inner_mac_header(skb) - skb_transport_header(skb) !=
2151 	      sizeof(struct udphdr) + sizeof(struct vxlanhdr))))
2152 		return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2153 
2154 	return features;
2155 }
2156 
2157 static int
2158 nfp_net_get_phys_port_name(struct net_device *netdev, char *name, size_t len)
2159 {
2160 	struct nfp_net *nn = netdev_priv(netdev);
2161 	int n;
2162 
2163 	/* If port is defined, devlink_port is registered and devlink core
2164 	 * is taking care of name formatting.
2165 	 */
2166 	if (nn->port)
2167 		return -EOPNOTSUPP;
2168 
2169 	if (nn->dp.is_vf || nn->vnic_no_name)
2170 		return -EOPNOTSUPP;
2171 
2172 	n = snprintf(name, len, "n%d", nn->id);
2173 	if (n >= len)
2174 		return -EINVAL;
2175 
2176 	return 0;
2177 }
2178 
2179 static int nfp_net_xdp_setup_drv(struct nfp_net *nn, struct netdev_bpf *bpf)
2180 {
2181 	struct bpf_prog *prog = bpf->prog;
2182 	struct nfp_net_dp *dp;
2183 	int err;
2184 
2185 	if (!prog == !nn->dp.xdp_prog) {
2186 		WRITE_ONCE(nn->dp.xdp_prog, prog);
2187 		xdp_attachment_setup(&nn->xdp, bpf);
2188 		return 0;
2189 	}
2190 
2191 	dp = nfp_net_clone_dp(nn);
2192 	if (!dp)
2193 		return -ENOMEM;
2194 
2195 	dp->xdp_prog = prog;
2196 	dp->num_tx_rings += prog ? nn->dp.num_rx_rings : -nn->dp.num_rx_rings;
2197 	dp->rx_dma_dir = prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
2198 	dp->rx_dma_off = prog ? XDP_PACKET_HEADROOM - nn->dp.rx_offset : 0;
2199 
2200 	/* We need RX reconfig to remap the buffers (BIDIR vs FROM_DEV) */
2201 	err = nfp_net_ring_reconfig(nn, dp, bpf->extack);
2202 	if (err)
2203 		return err;
2204 
2205 	xdp_attachment_setup(&nn->xdp, bpf);
2206 	return 0;
2207 }
2208 
2209 static int nfp_net_xdp_setup_hw(struct nfp_net *nn, struct netdev_bpf *bpf)
2210 {
2211 	int err;
2212 
2213 	err = nfp_app_xdp_offload(nn->app, nn, bpf->prog, bpf->extack);
2214 	if (err)
2215 		return err;
2216 
2217 	xdp_attachment_setup(&nn->xdp_hw, bpf);
2218 	return 0;
2219 }
2220 
2221 static int nfp_net_xdp(struct net_device *netdev, struct netdev_bpf *xdp)
2222 {
2223 	struct nfp_net *nn = netdev_priv(netdev);
2224 
2225 	switch (xdp->command) {
2226 	case XDP_SETUP_PROG:
2227 		return nfp_net_xdp_setup_drv(nn, xdp);
2228 	case XDP_SETUP_PROG_HW:
2229 		return nfp_net_xdp_setup_hw(nn, xdp);
2230 	case XDP_SETUP_XSK_POOL:
2231 		return nfp_net_xsk_setup_pool(netdev, xdp->xsk.pool,
2232 					      xdp->xsk.queue_id);
2233 	default:
2234 		return nfp_app_bpf(nn->app, nn, xdp);
2235 	}
2236 }
2237 
2238 static int nfp_net_set_mac_address(struct net_device *netdev, void *addr)
2239 {
2240 	struct nfp_net *nn = netdev_priv(netdev);
2241 	struct sockaddr *saddr = addr;
2242 	int err;
2243 
2244 	err = eth_prepare_mac_addr_change(netdev, addr);
2245 	if (err)
2246 		return err;
2247 
2248 	nfp_net_write_mac_addr(nn, saddr->sa_data);
2249 
2250 	err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_MACADDR);
2251 	if (err)
2252 		return err;
2253 
2254 	eth_commit_mac_addr_change(netdev, addr);
2255 
2256 	return 0;
2257 }
2258 
2259 static int nfp_net_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
2260 				  struct net_device *dev, u32 filter_mask,
2261 				  int nlflags)
2262 {
2263 	struct nfp_net *nn = netdev_priv(dev);
2264 	u16 mode;
2265 
2266 	if (!(nn->cap & NFP_NET_CFG_CTRL_VEPA))
2267 		return -EOPNOTSUPP;
2268 
2269 	mode = (nn->dp.ctrl & NFP_NET_CFG_CTRL_VEPA) ?
2270 	       BRIDGE_MODE_VEPA : BRIDGE_MODE_VEB;
2271 
2272 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, mode, 0, 0,
2273 				       nlflags, filter_mask, NULL);
2274 }
2275 
2276 static int nfp_net_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
2277 				  u16 flags, struct netlink_ext_ack *extack)
2278 {
2279 	struct nfp_net *nn = netdev_priv(dev);
2280 	struct nlattr *attr, *br_spec;
2281 	int rem, err;
2282 	u32 new_ctrl;
2283 	u16 mode;
2284 
2285 	if (!(nn->cap & NFP_NET_CFG_CTRL_VEPA))
2286 		return -EOPNOTSUPP;
2287 
2288 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
2289 	if (!br_spec)
2290 		return -EINVAL;
2291 
2292 	nla_for_each_nested(attr, br_spec, rem) {
2293 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
2294 			continue;
2295 
2296 		new_ctrl = nn->dp.ctrl;
2297 		mode = nla_get_u16(attr);
2298 		if (mode == BRIDGE_MODE_VEPA)
2299 			new_ctrl |= NFP_NET_CFG_CTRL_VEPA;
2300 		else if (mode == BRIDGE_MODE_VEB)
2301 			new_ctrl &= ~NFP_NET_CFG_CTRL_VEPA;
2302 		else
2303 			return -EOPNOTSUPP;
2304 
2305 		if (new_ctrl == nn->dp.ctrl)
2306 			return 0;
2307 
2308 		nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
2309 		err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_GEN);
2310 		if (!err)
2311 			nn->dp.ctrl = new_ctrl;
2312 
2313 		return err;
2314 	}
2315 
2316 	return -EINVAL;
2317 }
2318 
2319 const struct net_device_ops nfp_nfd3_netdev_ops = {
2320 	.ndo_init		= nfp_app_ndo_init,
2321 	.ndo_uninit		= nfp_app_ndo_uninit,
2322 	.ndo_open		= nfp_net_netdev_open,
2323 	.ndo_stop		= nfp_net_netdev_close,
2324 	.ndo_start_xmit		= nfp_net_tx,
2325 	.ndo_get_stats64	= nfp_net_stat64,
2326 	.ndo_vlan_rx_add_vid	= nfp_net_vlan_rx_add_vid,
2327 	.ndo_vlan_rx_kill_vid	= nfp_net_vlan_rx_kill_vid,
2328 	.ndo_set_vf_mac         = nfp_app_set_vf_mac,
2329 	.ndo_set_vf_vlan        = nfp_app_set_vf_vlan,
2330 	.ndo_set_vf_rate	= nfp_app_set_vf_rate,
2331 	.ndo_set_vf_spoofchk    = nfp_app_set_vf_spoofchk,
2332 	.ndo_set_vf_trust	= nfp_app_set_vf_trust,
2333 	.ndo_get_vf_config	= nfp_app_get_vf_config,
2334 	.ndo_set_vf_link_state  = nfp_app_set_vf_link_state,
2335 	.ndo_setup_tc		= nfp_port_setup_tc,
2336 	.ndo_tx_timeout		= nfp_net_tx_timeout,
2337 	.ndo_set_rx_mode	= nfp_net_set_rx_mode,
2338 	.ndo_change_mtu		= nfp_net_change_mtu,
2339 	.ndo_set_mac_address	= nfp_net_set_mac_address,
2340 	.ndo_set_features	= nfp_net_set_features,
2341 	.ndo_fix_features	= nfp_net_fix_features,
2342 	.ndo_features_check	= nfp_net_features_check,
2343 	.ndo_get_phys_port_name	= nfp_net_get_phys_port_name,
2344 	.ndo_bpf		= nfp_net_xdp,
2345 	.ndo_xsk_wakeup		= nfp_net_xsk_wakeup,
2346 	.ndo_bridge_getlink     = nfp_net_bridge_getlink,
2347 	.ndo_bridge_setlink     = nfp_net_bridge_setlink,
2348 };
2349 
2350 const struct net_device_ops nfp_nfdk_netdev_ops = {
2351 	.ndo_init		= nfp_app_ndo_init,
2352 	.ndo_uninit		= nfp_app_ndo_uninit,
2353 	.ndo_open		= nfp_net_netdev_open,
2354 	.ndo_stop		= nfp_net_netdev_close,
2355 	.ndo_start_xmit		= nfp_net_tx,
2356 	.ndo_get_stats64	= nfp_net_stat64,
2357 	.ndo_vlan_rx_add_vid	= nfp_net_vlan_rx_add_vid,
2358 	.ndo_vlan_rx_kill_vid	= nfp_net_vlan_rx_kill_vid,
2359 	.ndo_set_vf_mac         = nfp_app_set_vf_mac,
2360 	.ndo_set_vf_vlan        = nfp_app_set_vf_vlan,
2361 	.ndo_set_vf_rate	= nfp_app_set_vf_rate,
2362 	.ndo_set_vf_spoofchk    = nfp_app_set_vf_spoofchk,
2363 	.ndo_set_vf_trust	= nfp_app_set_vf_trust,
2364 	.ndo_get_vf_config	= nfp_app_get_vf_config,
2365 	.ndo_set_vf_link_state  = nfp_app_set_vf_link_state,
2366 	.ndo_setup_tc		= nfp_port_setup_tc,
2367 	.ndo_tx_timeout		= nfp_net_tx_timeout,
2368 	.ndo_set_rx_mode	= nfp_net_set_rx_mode,
2369 	.ndo_change_mtu		= nfp_net_change_mtu,
2370 	.ndo_set_mac_address	= nfp_net_set_mac_address,
2371 	.ndo_set_features	= nfp_net_set_features,
2372 	.ndo_fix_features	= nfp_net_fix_features,
2373 	.ndo_features_check	= nfp_net_features_check,
2374 	.ndo_get_phys_port_name	= nfp_net_get_phys_port_name,
2375 	.ndo_bpf		= nfp_net_xdp,
2376 	.ndo_bridge_getlink     = nfp_net_bridge_getlink,
2377 	.ndo_bridge_setlink     = nfp_net_bridge_setlink,
2378 };
2379 
2380 static int nfp_udp_tunnel_sync(struct net_device *netdev, unsigned int table)
2381 {
2382 	struct nfp_net *nn = netdev_priv(netdev);
2383 	int i;
2384 
2385 	BUILD_BUG_ON(NFP_NET_N_VXLAN_PORTS & 1);
2386 	for (i = 0; i < NFP_NET_N_VXLAN_PORTS; i += 2) {
2387 		struct udp_tunnel_info ti0, ti1;
2388 
2389 		udp_tunnel_nic_get_port(netdev, table, i, &ti0);
2390 		udp_tunnel_nic_get_port(netdev, table, i + 1, &ti1);
2391 
2392 		nn_writel(nn, NFP_NET_CFG_VXLAN_PORT + i * sizeof(ti0.port),
2393 			  be16_to_cpu(ti1.port) << 16 | be16_to_cpu(ti0.port));
2394 	}
2395 
2396 	return nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_VXLAN);
2397 }
2398 
2399 static const struct udp_tunnel_nic_info nfp_udp_tunnels = {
2400 	.sync_table     = nfp_udp_tunnel_sync,
2401 	.flags          = UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
2402 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
2403 	.tables         = {
2404 		{
2405 			.n_entries      = NFP_NET_N_VXLAN_PORTS,
2406 			.tunnel_types   = UDP_TUNNEL_TYPE_VXLAN,
2407 		},
2408 	},
2409 };
2410 
2411 /**
2412  * nfp_net_info() - Print general info about the NIC
2413  * @nn:      NFP Net device to reconfigure
2414  */
2415 void nfp_net_info(struct nfp_net *nn)
2416 {
2417 	nn_info(nn, "NFP-6xxx %sNetdev: TxQs=%d/%d RxQs=%d/%d\n",
2418 		nn->dp.is_vf ? "VF " : "",
2419 		nn->dp.num_tx_rings, nn->max_tx_rings,
2420 		nn->dp.num_rx_rings, nn->max_rx_rings);
2421 	nn_info(nn, "VER: %d.%d.%d.%d, Maximum supported MTU: %d\n",
2422 		nn->fw_ver.extend, nn->fw_ver.class,
2423 		nn->fw_ver.major, nn->fw_ver.minor,
2424 		nn->max_mtu);
2425 	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%s%s%s%s%s%s\n",
2426 		nn->cap,
2427 		nn->cap & NFP_NET_CFG_CTRL_PROMISC  ? "PROMISC "  : "",
2428 		nn->cap & NFP_NET_CFG_CTRL_L2BC     ? "L2BCFILT " : "",
2429 		nn->cap & NFP_NET_CFG_CTRL_L2MC     ? "L2MCFILT " : "",
2430 		nn->cap & NFP_NET_CFG_CTRL_RXCSUM   ? "RXCSUM "   : "",
2431 		nn->cap & NFP_NET_CFG_CTRL_TXCSUM   ? "TXCSUM "   : "",
2432 		nn->cap & NFP_NET_CFG_CTRL_RXVLAN   ? "RXVLAN "   : "",
2433 		nn->cap & NFP_NET_CFG_CTRL_TXVLAN   ? "TXVLAN "   : "",
2434 		nn->cap & NFP_NET_CFG_CTRL_RXQINQ   ? "RXQINQ "   : "",
2435 		nn->cap & NFP_NET_CFG_CTRL_RXVLAN_V2 ? "RXVLANv2 "   : "",
2436 		nn->cap & NFP_NET_CFG_CTRL_TXVLAN_V2   ? "TXVLANv2 "   : "",
2437 		nn->cap & NFP_NET_CFG_CTRL_SCATTER  ? "SCATTER "  : "",
2438 		nn->cap & NFP_NET_CFG_CTRL_GATHER   ? "GATHER "   : "",
2439 		nn->cap & NFP_NET_CFG_CTRL_LSO      ? "TSO1 "     : "",
2440 		nn->cap & NFP_NET_CFG_CTRL_LSO2     ? "TSO2 "     : "",
2441 		nn->cap & NFP_NET_CFG_CTRL_RSS      ? "RSS1 "     : "",
2442 		nn->cap & NFP_NET_CFG_CTRL_RSS2     ? "RSS2 "     : "",
2443 		nn->cap & NFP_NET_CFG_CTRL_CTAG_FILTER ? "CTAG_FILTER " : "",
2444 		nn->cap & NFP_NET_CFG_CTRL_MSIXAUTO ? "AUTOMASK " : "",
2445 		nn->cap & NFP_NET_CFG_CTRL_IRQMOD   ? "IRQMOD "   : "",
2446 		nn->cap & NFP_NET_CFG_CTRL_TXRWB    ? "TXRWB "    : "",
2447 		nn->cap & NFP_NET_CFG_CTRL_VEPA     ? "VEPA "     : "",
2448 		nn->cap & NFP_NET_CFG_CTRL_VXLAN    ? "VXLAN "    : "",
2449 		nn->cap & NFP_NET_CFG_CTRL_NVGRE    ? "NVGRE "	  : "",
2450 		nn->cap & NFP_NET_CFG_CTRL_CSUM_COMPLETE ?
2451 						      "RXCSUM_COMPLETE " : "",
2452 		nn->cap & NFP_NET_CFG_CTRL_LIVE_ADDR ? "LIVE_ADDR " : "",
2453 		nn->cap_w1 & NFP_NET_CFG_CTRL_MCAST_FILTER ? "MULTICAST_FILTER " : "",
2454 		nn->cap_w1 & NFP_NET_CFG_CTRL_USO ? "USO " : "",
2455 		nfp_app_extra_cap(nn->app, nn));
2456 }
2457 
2458 /**
2459  * nfp_net_alloc() - Allocate netdev and related structure
2460  * @pdev:         PCI device
2461  * @dev_info:     NFP ASIC params
2462  * @ctrl_bar:     PCI IOMEM with vNIC config memory
2463  * @needs_netdev: Whether to allocate a netdev for this vNIC
2464  * @max_tx_rings: Maximum number of TX rings supported by device
2465  * @max_rx_rings: Maximum number of RX rings supported by device
2466  *
2467  * This function allocates a netdev device and fills in the initial
2468  * part of the @struct nfp_net structure.  In case of control device
2469  * nfp_net structure is allocated without the netdev.
2470  *
2471  * Return: NFP Net device structure, or ERR_PTR on error.
2472  */
2473 struct nfp_net *
2474 nfp_net_alloc(struct pci_dev *pdev, const struct nfp_dev_info *dev_info,
2475 	      void __iomem *ctrl_bar, bool needs_netdev,
2476 	      unsigned int max_tx_rings, unsigned int max_rx_rings)
2477 {
2478 	u64 dma_mask = dma_get_mask(&pdev->dev);
2479 	struct nfp_net *nn;
2480 	int err;
2481 
2482 	if (needs_netdev) {
2483 		struct net_device *netdev;
2484 
2485 		netdev = alloc_etherdev_mqs(sizeof(struct nfp_net),
2486 					    max_tx_rings, max_rx_rings);
2487 		if (!netdev)
2488 			return ERR_PTR(-ENOMEM);
2489 
2490 		SET_NETDEV_DEV(netdev, &pdev->dev);
2491 		nn = netdev_priv(netdev);
2492 		nn->dp.netdev = netdev;
2493 	} else {
2494 		nn = vzalloc(sizeof(*nn));
2495 		if (!nn)
2496 			return ERR_PTR(-ENOMEM);
2497 	}
2498 
2499 	nn->dp.dev = &pdev->dev;
2500 	nn->dp.ctrl_bar = ctrl_bar;
2501 	nn->dev_info = dev_info;
2502 	nn->pdev = pdev;
2503 	nfp_net_get_fw_version(&nn->fw_ver, ctrl_bar);
2504 
2505 	switch (FIELD_GET(NFP_NET_CFG_VERSION_DP_MASK, nn->fw_ver.extend)) {
2506 	case NFP_NET_CFG_VERSION_DP_NFD3:
2507 		nn->dp.ops = &nfp_nfd3_ops;
2508 		break;
2509 	case NFP_NET_CFG_VERSION_DP_NFDK:
2510 		if (nn->fw_ver.major < 5) {
2511 			dev_err(&pdev->dev,
2512 				"NFDK must use ABI 5 or newer, found: %d\n",
2513 				nn->fw_ver.major);
2514 			err = -EINVAL;
2515 			goto err_free_nn;
2516 		}
2517 		nn->dp.ops = &nfp_nfdk_ops;
2518 		break;
2519 	default:
2520 		err = -EINVAL;
2521 		goto err_free_nn;
2522 	}
2523 
2524 	if ((dma_mask & nn->dp.ops->dma_mask) != dma_mask) {
2525 		dev_err(&pdev->dev,
2526 			"DMA mask of loaded firmware: %llx, required DMA mask: %llx\n",
2527 			nn->dp.ops->dma_mask, dma_mask);
2528 		err = -EINVAL;
2529 		goto err_free_nn;
2530 	}
2531 
2532 	nn->max_tx_rings = max_tx_rings;
2533 	nn->max_rx_rings = max_rx_rings;
2534 
2535 	nn->dp.num_tx_rings = min_t(unsigned int,
2536 				    max_tx_rings, num_online_cpus());
2537 	nn->dp.num_rx_rings = min_t(unsigned int, max_rx_rings,
2538 				 netif_get_num_default_rss_queues());
2539 
2540 	nn->dp.num_r_vecs = max(nn->dp.num_tx_rings, nn->dp.num_rx_rings);
2541 	nn->dp.num_r_vecs = min_t(unsigned int,
2542 				  nn->dp.num_r_vecs, num_online_cpus());
2543 	nn->max_r_vecs = nn->dp.num_r_vecs;
2544 
2545 	nn->dp.xsk_pools = kcalloc(nn->max_r_vecs, sizeof(nn->dp.xsk_pools),
2546 				   GFP_KERNEL);
2547 	if (!nn->dp.xsk_pools) {
2548 		err = -ENOMEM;
2549 		goto err_free_nn;
2550 	}
2551 
2552 	nn->dp.txd_cnt = NFP_NET_TX_DESCS_DEFAULT;
2553 	nn->dp.rxd_cnt = NFP_NET_RX_DESCS_DEFAULT;
2554 
2555 	sema_init(&nn->bar_lock, 1);
2556 
2557 	spin_lock_init(&nn->reconfig_lock);
2558 	spin_lock_init(&nn->link_status_lock);
2559 
2560 	timer_setup(&nn->reconfig_timer, nfp_net_reconfig_timer, 0);
2561 
2562 	err = nfp_net_tlv_caps_parse(&nn->pdev->dev, nn->dp.ctrl_bar,
2563 				     &nn->tlv_caps);
2564 	if (err)
2565 		goto err_free_nn;
2566 
2567 	err = nfp_ccm_mbox_alloc(nn);
2568 	if (err)
2569 		goto err_free_nn;
2570 
2571 	return nn;
2572 
2573 err_free_nn:
2574 	if (nn->dp.netdev)
2575 		free_netdev(nn->dp.netdev);
2576 	else
2577 		vfree(nn);
2578 	return ERR_PTR(err);
2579 }
2580 
2581 /**
2582  * nfp_net_free() - Undo what @nfp_net_alloc() did
2583  * @nn:      NFP Net device to reconfigure
2584  */
2585 void nfp_net_free(struct nfp_net *nn)
2586 {
2587 	WARN_ON(timer_pending(&nn->reconfig_timer) || nn->reconfig_posted);
2588 	nfp_ccm_mbox_free(nn);
2589 
2590 	kfree(nn->dp.xsk_pools);
2591 	if (nn->dp.netdev)
2592 		free_netdev(nn->dp.netdev);
2593 	else
2594 		vfree(nn);
2595 }
2596 
2597 /**
2598  * nfp_net_rss_key_sz() - Get current size of the RSS key
2599  * @nn:		NFP Net device instance
2600  *
2601  * Return: size of the RSS key for currently selected hash function.
2602  */
2603 unsigned int nfp_net_rss_key_sz(struct nfp_net *nn)
2604 {
2605 	switch (nn->rss_hfunc) {
2606 	case ETH_RSS_HASH_TOP:
2607 		return NFP_NET_CFG_RSS_KEY_SZ;
2608 	case ETH_RSS_HASH_XOR:
2609 		return 0;
2610 	case ETH_RSS_HASH_CRC32:
2611 		return 4;
2612 	}
2613 
2614 	nn_warn(nn, "Unknown hash function: %u\n", nn->rss_hfunc);
2615 	return 0;
2616 }
2617 
2618 /**
2619  * nfp_net_rss_init() - Set the initial RSS parameters
2620  * @nn:	     NFP Net device to reconfigure
2621  */
2622 static void nfp_net_rss_init(struct nfp_net *nn)
2623 {
2624 	unsigned long func_bit, rss_cap_hfunc;
2625 	u32 reg;
2626 
2627 	/* Read the RSS function capability and select first supported func */
2628 	reg = nn_readl(nn, NFP_NET_CFG_RSS_CAP);
2629 	rss_cap_hfunc =	FIELD_GET(NFP_NET_CFG_RSS_CAP_HFUNC, reg);
2630 	if (!rss_cap_hfunc)
2631 		rss_cap_hfunc =	FIELD_GET(NFP_NET_CFG_RSS_CAP_HFUNC,
2632 					  NFP_NET_CFG_RSS_TOEPLITZ);
2633 
2634 	func_bit = find_first_bit(&rss_cap_hfunc, NFP_NET_CFG_RSS_HFUNCS);
2635 	if (func_bit == NFP_NET_CFG_RSS_HFUNCS) {
2636 		dev_warn(nn->dp.dev,
2637 			 "Bad RSS config, defaulting to Toeplitz hash\n");
2638 		func_bit = ETH_RSS_HASH_TOP_BIT;
2639 	}
2640 	nn->rss_hfunc = 1 << func_bit;
2641 
2642 	netdev_rss_key_fill(nn->rss_key, nfp_net_rss_key_sz(nn));
2643 
2644 	nfp_net_rss_init_itbl(nn);
2645 
2646 	/* Enable IPv4/IPv6 TCP by default */
2647 	nn->rss_cfg = NFP_NET_CFG_RSS_IPV4_TCP |
2648 		      NFP_NET_CFG_RSS_IPV6_TCP |
2649 		      NFP_NET_CFG_RSS_IPV4_UDP |
2650 		      NFP_NET_CFG_RSS_IPV6_UDP |
2651 		      FIELD_PREP(NFP_NET_CFG_RSS_HFUNC, nn->rss_hfunc) |
2652 		      NFP_NET_CFG_RSS_MASK;
2653 }
2654 
2655 /**
2656  * nfp_net_irqmod_init() - Set the initial IRQ moderation parameters
2657  * @nn:	     NFP Net device to reconfigure
2658  */
2659 static void nfp_net_irqmod_init(struct nfp_net *nn)
2660 {
2661 	nn->rx_coalesce_usecs      = 50;
2662 	nn->rx_coalesce_max_frames = 64;
2663 	nn->tx_coalesce_usecs      = 50;
2664 	nn->tx_coalesce_max_frames = 64;
2665 
2666 	nn->rx_coalesce_adapt_on   = true;
2667 	nn->tx_coalesce_adapt_on   = true;
2668 }
2669 
2670 static void nfp_net_netdev_init(struct nfp_net *nn)
2671 {
2672 	struct net_device *netdev = nn->dp.netdev;
2673 
2674 	nfp_net_write_mac_addr(nn, nn->dp.netdev->dev_addr);
2675 
2676 	netdev->mtu = nn->dp.mtu;
2677 
2678 	/* Advertise/enable offloads based on capabilities
2679 	 *
2680 	 * Note: netdev->features show the currently enabled features
2681 	 * and netdev->hw_features advertises which features are
2682 	 * supported.  By default we enable most features.
2683 	 */
2684 	if (nn->cap & NFP_NET_CFG_CTRL_LIVE_ADDR)
2685 		netdev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2686 
2687 	netdev->hw_features = NETIF_F_HIGHDMA;
2688 	if (nn->cap & NFP_NET_CFG_CTRL_RXCSUM_ANY) {
2689 		netdev->hw_features |= NETIF_F_RXCSUM;
2690 		nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_RXCSUM_ANY;
2691 	}
2692 	if (nn->cap & NFP_NET_CFG_CTRL_TXCSUM) {
2693 		netdev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
2694 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_TXCSUM;
2695 	}
2696 	if (nn->cap & NFP_NET_CFG_CTRL_GATHER) {
2697 		netdev->hw_features |= NETIF_F_SG;
2698 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_GATHER;
2699 	}
2700 	if ((nn->cap & NFP_NET_CFG_CTRL_LSO && nn->fw_ver.major > 2) ||
2701 	    nn->cap & NFP_NET_CFG_CTRL_LSO2) {
2702 		netdev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
2703 		if (nn->cap_w1 & NFP_NET_CFG_CTRL_USO)
2704 			netdev->hw_features |= NETIF_F_GSO_UDP_L4;
2705 		nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_LSO2 ?:
2706 					 NFP_NET_CFG_CTRL_LSO;
2707 	}
2708 	if (nn->cap & NFP_NET_CFG_CTRL_RSS_ANY)
2709 		netdev->hw_features |= NETIF_F_RXHASH;
2710 
2711 #ifdef CONFIG_NFP_NET_IPSEC
2712 	if (nn->cap_w1 & NFP_NET_CFG_CTRL_IPSEC)
2713 		netdev->hw_features |= NETIF_F_HW_ESP | NETIF_F_HW_ESP_TX_CSUM;
2714 #endif
2715 
2716 	if (nn->cap & NFP_NET_CFG_CTRL_VXLAN) {
2717 		if (nn->cap & NFP_NET_CFG_CTRL_LSO) {
2718 			netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL |
2719 					       NETIF_F_GSO_UDP_TUNNEL_CSUM |
2720 					       NETIF_F_GSO_PARTIAL;
2721 			netdev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM;
2722 		}
2723 		netdev->udp_tunnel_nic_info = &nfp_udp_tunnels;
2724 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_VXLAN;
2725 	}
2726 	if (nn->cap & NFP_NET_CFG_CTRL_NVGRE) {
2727 		if (nn->cap & NFP_NET_CFG_CTRL_LSO)
2728 			netdev->hw_features |= NETIF_F_GSO_GRE;
2729 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_NVGRE;
2730 	}
2731 	if (nn->cap & (NFP_NET_CFG_CTRL_VXLAN | NFP_NET_CFG_CTRL_NVGRE))
2732 		netdev->hw_enc_features = netdev->hw_features;
2733 
2734 	netdev->vlan_features = netdev->hw_features;
2735 
2736 	if (nn->cap & NFP_NET_CFG_CTRL_RXVLAN_ANY) {
2737 		netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
2738 		nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_RXVLAN_V2 ?:
2739 			       NFP_NET_CFG_CTRL_RXVLAN;
2740 	}
2741 	if (nn->cap & NFP_NET_CFG_CTRL_TXVLAN_ANY) {
2742 		if (nn->cap & NFP_NET_CFG_CTRL_LSO2) {
2743 			nn_warn(nn, "Device advertises both TSO2 and TXVLAN. Refusing to enable TXVLAN.\n");
2744 		} else {
2745 			netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
2746 			nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_TXVLAN_V2 ?:
2747 				       NFP_NET_CFG_CTRL_TXVLAN;
2748 		}
2749 	}
2750 	if (nn->cap & NFP_NET_CFG_CTRL_CTAG_FILTER) {
2751 		netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
2752 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_CTAG_FILTER;
2753 	}
2754 	if (nn->cap & NFP_NET_CFG_CTRL_RXQINQ) {
2755 		netdev->hw_features |= NETIF_F_HW_VLAN_STAG_RX;
2756 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_RXQINQ;
2757 	}
2758 
2759 	netdev->features = netdev->hw_features;
2760 
2761 	if (nfp_app_has_tc(nn->app) && nn->port)
2762 		netdev->hw_features |= NETIF_F_HW_TC;
2763 
2764 	/* C-Tag strip and S-Tag strip can't be supported simultaneously,
2765 	 * so enable C-Tag strip and disable S-Tag strip by default.
2766 	 */
2767 	netdev->features &= ~NETIF_F_HW_VLAN_STAG_RX;
2768 	nn->dp.ctrl &= ~NFP_NET_CFG_CTRL_RXQINQ;
2769 
2770 	netdev->xdp_features = NETDEV_XDP_ACT_BASIC;
2771 	if (nn->app && nn->app->type->id == NFP_APP_BPF_NIC)
2772 		netdev->xdp_features |= NETDEV_XDP_ACT_HW_OFFLOAD;
2773 
2774 	/* Finalise the netdev setup */
2775 	switch (nn->dp.ops->version) {
2776 	case NFP_NFD_VER_NFD3:
2777 		netdev->netdev_ops = &nfp_nfd3_netdev_ops;
2778 		netdev->xdp_features |= NETDEV_XDP_ACT_XSK_ZEROCOPY;
2779 		netdev->xdp_features |= NETDEV_XDP_ACT_REDIRECT;
2780 		break;
2781 	case NFP_NFD_VER_NFDK:
2782 		netdev->netdev_ops = &nfp_nfdk_netdev_ops;
2783 		break;
2784 	}
2785 
2786 	netdev->watchdog_timeo = msecs_to_jiffies(5 * 1000);
2787 
2788 	/* MTU range: 68 - hw-specific max */
2789 	netdev->min_mtu = ETH_MIN_MTU;
2790 	netdev->max_mtu = nn->max_mtu;
2791 
2792 	netif_set_tso_max_segs(netdev, NFP_NET_LSO_MAX_SEGS);
2793 
2794 	netif_carrier_off(netdev);
2795 
2796 	nfp_net_set_ethtool_ops(netdev);
2797 }
2798 
2799 static int nfp_net_read_caps(struct nfp_net *nn)
2800 {
2801 	/* Get some of the read-only fields from the BAR */
2802 	nn->cap = nn_readl(nn, NFP_NET_CFG_CAP);
2803 	nn->cap_w1 = nn_readl(nn, NFP_NET_CFG_CAP_WORD1);
2804 	nn->max_mtu = nn_readl(nn, NFP_NET_CFG_MAX_MTU);
2805 
2806 	/* ABI 4.x and ctrl vNIC always use chained metadata, in other cases
2807 	 * we allow use of non-chained metadata if RSS(v1) is the only
2808 	 * advertised capability requiring metadata.
2809 	 */
2810 	nn->dp.chained_metadata_format = nn->fw_ver.major == 4 ||
2811 					 !nn->dp.netdev ||
2812 					 !(nn->cap & NFP_NET_CFG_CTRL_RSS) ||
2813 					 nn->cap & NFP_NET_CFG_CTRL_CHAIN_META;
2814 	/* RSS(v1) uses non-chained metadata format, except in ABI 4.x where
2815 	 * it has the same meaning as RSSv2.
2816 	 */
2817 	if (nn->dp.chained_metadata_format && nn->fw_ver.major != 4)
2818 		nn->cap &= ~NFP_NET_CFG_CTRL_RSS;
2819 
2820 	/* Determine RX packet/metadata boundary offset */
2821 	if (nn->fw_ver.major >= 2) {
2822 		u32 reg;
2823 
2824 		reg = nn_readl(nn, NFP_NET_CFG_RX_OFFSET);
2825 		if (reg > NFP_NET_MAX_PREPEND) {
2826 			nn_err(nn, "Invalid rx offset: %d\n", reg);
2827 			return -EINVAL;
2828 		}
2829 		nn->dp.rx_offset = reg;
2830 	} else {
2831 		nn->dp.rx_offset = NFP_NET_RX_OFFSET;
2832 	}
2833 
2834 	/* Mask out NFD-version-specific features */
2835 	nn->cap &= nn->dp.ops->cap_mask;
2836 
2837 	/* For control vNICs mask out the capabilities app doesn't want. */
2838 	if (!nn->dp.netdev)
2839 		nn->cap &= nn->app->type->ctrl_cap_mask;
2840 
2841 	return 0;
2842 }
2843 
2844 /**
2845  * nfp_net_init() - Initialise/finalise the nfp_net structure
2846  * @nn:		NFP Net device structure
2847  *
2848  * Return: 0 on success or negative errno on error.
2849  */
2850 int nfp_net_init(struct nfp_net *nn)
2851 {
2852 	int err;
2853 
2854 	nn->dp.rx_dma_dir = DMA_FROM_DEVICE;
2855 
2856 	err = nfp_net_read_caps(nn);
2857 	if (err)
2858 		return err;
2859 
2860 	/* Set default MTU and Freelist buffer size */
2861 	if (!nfp_net_is_data_vnic(nn) && nn->app->ctrl_mtu) {
2862 		nn->dp.mtu = min(nn->app->ctrl_mtu, nn->max_mtu);
2863 	} else if (nn->max_mtu < NFP_NET_DEFAULT_MTU) {
2864 		nn->dp.mtu = nn->max_mtu;
2865 	} else {
2866 		nn->dp.mtu = NFP_NET_DEFAULT_MTU;
2867 	}
2868 	nn->dp.fl_bufsz = nfp_net_calc_fl_bufsz(&nn->dp);
2869 
2870 	if (nfp_app_ctrl_uses_data_vnics(nn->app))
2871 		nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_CMSG_DATA;
2872 
2873 	if (nn->cap & NFP_NET_CFG_CTRL_RSS_ANY) {
2874 		nfp_net_rss_init(nn);
2875 		nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_RSS2 ?:
2876 					 NFP_NET_CFG_CTRL_RSS;
2877 	}
2878 
2879 	/* Allow L2 Broadcast and Multicast through by default, if supported */
2880 	if (nn->cap & NFP_NET_CFG_CTRL_L2BC)
2881 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_L2BC;
2882 
2883 	/* Allow IRQ moderation, if supported */
2884 	if (nn->cap & NFP_NET_CFG_CTRL_IRQMOD) {
2885 		nfp_net_irqmod_init(nn);
2886 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_IRQMOD;
2887 	}
2888 
2889 	/* Enable TX pointer writeback, if supported */
2890 	if (nn->cap & NFP_NET_CFG_CTRL_TXRWB)
2891 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_TXRWB;
2892 
2893 	if (nn->cap_w1 & NFP_NET_CFG_CTRL_MCAST_FILTER)
2894 		nn->dp.ctrl_w1 |= NFP_NET_CFG_CTRL_MCAST_FILTER;
2895 
2896 	/* Stash the re-configuration queue away.  First odd queue in TX Bar */
2897 	nn->qcp_cfg = nn->tx_bar + NFP_QCP_QUEUE_ADDR_SZ;
2898 
2899 	/* Make sure the FW knows the netdev is supposed to be disabled here */
2900 	nn_writel(nn, NFP_NET_CFG_CTRL, 0);
2901 	nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, 0);
2902 	nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, 0);
2903 	nn_writel(nn, NFP_NET_CFG_CTRL_WORD1, 0);
2904 	err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_RING |
2905 				   NFP_NET_CFG_UPDATE_GEN);
2906 	if (err)
2907 		return err;
2908 
2909 	if (nn->dp.netdev) {
2910 		nfp_net_netdev_init(nn);
2911 
2912 		err = nfp_ccm_mbox_init(nn);
2913 		if (err)
2914 			return err;
2915 
2916 		err = nfp_net_tls_init(nn);
2917 		if (err)
2918 			goto err_clean_mbox;
2919 
2920 		nfp_net_ipsec_init(nn);
2921 	}
2922 
2923 	nfp_net_vecs_init(nn);
2924 
2925 	if (!nn->dp.netdev)
2926 		return 0;
2927 
2928 	spin_lock_init(&nn->mbox_amsg.lock);
2929 	INIT_LIST_HEAD(&nn->mbox_amsg.list);
2930 	INIT_WORK(&nn->mbox_amsg.work, nfp_net_mbox_amsg_work);
2931 
2932 	INIT_LIST_HEAD(&nn->fs.list);
2933 
2934 	return register_netdev(nn->dp.netdev);
2935 
2936 err_clean_mbox:
2937 	nfp_ccm_mbox_clean(nn);
2938 	return err;
2939 }
2940 
2941 /**
2942  * nfp_net_clean() - Undo what nfp_net_init() did.
2943  * @nn:		NFP Net device structure
2944  */
2945 void nfp_net_clean(struct nfp_net *nn)
2946 {
2947 	if (!nn->dp.netdev)
2948 		return;
2949 
2950 	unregister_netdev(nn->dp.netdev);
2951 	nfp_net_ipsec_clean(nn);
2952 	nfp_ccm_mbox_clean(nn);
2953 	nfp_net_fs_clean(nn);
2954 	flush_work(&nn->mbox_amsg.work);
2955 	nfp_net_reconfig_wait_posted(nn);
2956 }
2957