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