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