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/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 	u32 sts;
477 
478 	spin_lock_irqsave(&nn->link_status_lock, flags);
479 
480 	sts = nn_readl(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 
490 	if (nn->link_up) {
491 		netif_carrier_on(nn->dp.netdev);
492 		netdev_info(nn->dp.netdev, "NIC Link is Up\n");
493 	} else {
494 		netif_carrier_off(nn->dp.netdev);
495 		netdev_info(nn->dp.netdev, "NIC Link is Down\n");
496 	}
497 out:
498 	spin_unlock_irqrestore(&nn->link_status_lock, flags);
499 }
500 
501 /**
502  * nfp_net_irq_lsc() - Interrupt service routine for link state changes
503  * @irq:      Interrupt
504  * @data:     Opaque data structure
505  *
506  * Return: Indicate if the interrupt has been handled.
507  */
508 static irqreturn_t nfp_net_irq_lsc(int irq, void *data)
509 {
510 	struct nfp_net *nn = data;
511 	struct msix_entry *entry;
512 
513 	entry = &nn->irq_entries[NFP_NET_IRQ_LSC_IDX];
514 
515 	nfp_net_read_link_status(nn);
516 
517 	nfp_net_irq_unmask(nn, entry->entry);
518 
519 	return IRQ_HANDLED;
520 }
521 
522 /**
523  * nfp_net_irq_exn() - Interrupt service routine for exceptions
524  * @irq:      Interrupt
525  * @data:     Opaque data structure
526  *
527  * Return: Indicate if the interrupt has been handled.
528  */
529 static irqreturn_t nfp_net_irq_exn(int irq, void *data)
530 {
531 	struct nfp_net *nn = data;
532 
533 	nn_err(nn, "%s: UNIMPLEMENTED.\n", __func__);
534 	/* XXX TO BE IMPLEMENTED */
535 	return IRQ_HANDLED;
536 }
537 
538 /**
539  * nfp_net_aux_irq_request() - Request an auxiliary interrupt (LSC or EXN)
540  * @nn:		NFP Network structure
541  * @ctrl_offset: Control BAR offset where IRQ configuration should be written
542  * @format:	printf-style format to construct the interrupt name
543  * @name:	Pointer to allocated space for interrupt name
544  * @name_sz:	Size of space for interrupt name
545  * @vector_idx:	Index of MSI-X vector used for this interrupt
546  * @handler:	IRQ handler to register for this interrupt
547  */
548 static int
549 nfp_net_aux_irq_request(struct nfp_net *nn, u32 ctrl_offset,
550 			const char *format, char *name, size_t name_sz,
551 			unsigned int vector_idx, irq_handler_t handler)
552 {
553 	struct msix_entry *entry;
554 	int err;
555 
556 	entry = &nn->irq_entries[vector_idx];
557 
558 	snprintf(name, name_sz, format, nfp_net_name(nn));
559 	err = request_irq(entry->vector, handler, 0, name, nn);
560 	if (err) {
561 		nn_err(nn, "Failed to request IRQ %d (err=%d).\n",
562 		       entry->vector, err);
563 		return err;
564 	}
565 	nn_writeb(nn, ctrl_offset, entry->entry);
566 	nfp_net_irq_unmask(nn, entry->entry);
567 
568 	return 0;
569 }
570 
571 /**
572  * nfp_net_aux_irq_free() - Free an auxiliary interrupt (LSC or EXN)
573  * @nn:		NFP Network structure
574  * @ctrl_offset: Control BAR offset where IRQ configuration should be written
575  * @vector_idx:	Index of MSI-X vector used for this interrupt
576  */
577 static void nfp_net_aux_irq_free(struct nfp_net *nn, u32 ctrl_offset,
578 				 unsigned int vector_idx)
579 {
580 	nn_writeb(nn, ctrl_offset, 0xff);
581 	nn_pci_flush(nn);
582 	free_irq(nn->irq_entries[vector_idx].vector, nn);
583 }
584 
585 struct sk_buff *
586 nfp_net_tls_tx(struct nfp_net_dp *dp, struct nfp_net_r_vector *r_vec,
587 	       struct sk_buff *skb, u64 *tls_handle, int *nr_frags)
588 {
589 #ifdef CONFIG_TLS_DEVICE
590 	struct nfp_net_tls_offload_ctx *ntls;
591 	struct sk_buff *nskb;
592 	bool resync_pending;
593 	u32 datalen, seq;
594 
595 	if (likely(!dp->ktls_tx))
596 		return skb;
597 	if (!skb->sk || !tls_is_sk_tx_device_offloaded(skb->sk))
598 		return skb;
599 
600 	datalen = skb->len - (skb_transport_offset(skb) + tcp_hdrlen(skb));
601 	seq = ntohl(tcp_hdr(skb)->seq);
602 	ntls = tls_driver_ctx(skb->sk, TLS_OFFLOAD_CTX_DIR_TX);
603 	resync_pending = tls_offload_tx_resync_pending(skb->sk);
604 	if (unlikely(resync_pending || ntls->next_seq != seq)) {
605 		/* Pure ACK out of order already */
606 		if (!datalen)
607 			return skb;
608 
609 		u64_stats_update_begin(&r_vec->tx_sync);
610 		r_vec->tls_tx_fallback++;
611 		u64_stats_update_end(&r_vec->tx_sync);
612 
613 		nskb = tls_encrypt_skb(skb);
614 		if (!nskb) {
615 			u64_stats_update_begin(&r_vec->tx_sync);
616 			r_vec->tls_tx_no_fallback++;
617 			u64_stats_update_end(&r_vec->tx_sync);
618 			return NULL;
619 		}
620 		/* encryption wasn't necessary */
621 		if (nskb == skb)
622 			return skb;
623 		/* we don't re-check ring space */
624 		if (unlikely(skb_is_nonlinear(nskb))) {
625 			nn_dp_warn(dp, "tls_encrypt_skb() produced fragmented frame\n");
626 			u64_stats_update_begin(&r_vec->tx_sync);
627 			r_vec->tx_errors++;
628 			u64_stats_update_end(&r_vec->tx_sync);
629 			dev_kfree_skb_any(nskb);
630 			return NULL;
631 		}
632 
633 		/* jump forward, a TX may have gotten lost, need to sync TX */
634 		if (!resync_pending && seq - ntls->next_seq < U32_MAX / 4)
635 			tls_offload_tx_resync_request(nskb->sk, seq,
636 						      ntls->next_seq);
637 
638 		*nr_frags = 0;
639 		return nskb;
640 	}
641 
642 	if (datalen) {
643 		u64_stats_update_begin(&r_vec->tx_sync);
644 		if (!skb_is_gso(skb))
645 			r_vec->hw_tls_tx++;
646 		else
647 			r_vec->hw_tls_tx += skb_shinfo(skb)->gso_segs;
648 		u64_stats_update_end(&r_vec->tx_sync);
649 	}
650 
651 	memcpy(tls_handle, ntls->fw_handle, sizeof(ntls->fw_handle));
652 	ntls->next_seq += datalen;
653 #endif
654 	return skb;
655 }
656 
657 void nfp_net_tls_tx_undo(struct sk_buff *skb, u64 tls_handle)
658 {
659 #ifdef CONFIG_TLS_DEVICE
660 	struct nfp_net_tls_offload_ctx *ntls;
661 	u32 datalen, seq;
662 
663 	if (!tls_handle)
664 		return;
665 	if (WARN_ON_ONCE(!skb->sk || !tls_is_sk_tx_device_offloaded(skb->sk)))
666 		return;
667 
668 	datalen = skb->len - (skb_transport_offset(skb) + tcp_hdrlen(skb));
669 	seq = ntohl(tcp_hdr(skb)->seq);
670 
671 	ntls = tls_driver_ctx(skb->sk, TLS_OFFLOAD_CTX_DIR_TX);
672 	if (ntls->next_seq == seq + datalen)
673 		ntls->next_seq = seq;
674 	else
675 		WARN_ON_ONCE(1);
676 #endif
677 }
678 
679 static void nfp_net_tx_timeout(struct net_device *netdev, unsigned int txqueue)
680 {
681 	struct nfp_net *nn = netdev_priv(netdev);
682 
683 	nn_warn(nn, "TX watchdog timeout on ring: %u\n", txqueue);
684 }
685 
686 /* Receive processing */
687 static unsigned int
688 nfp_net_calc_fl_bufsz_data(struct nfp_net_dp *dp)
689 {
690 	unsigned int fl_bufsz = 0;
691 
692 	if (dp->rx_offset == NFP_NET_CFG_RX_OFFSET_DYNAMIC)
693 		fl_bufsz += NFP_NET_MAX_PREPEND;
694 	else
695 		fl_bufsz += dp->rx_offset;
696 	fl_bufsz += ETH_HLEN + VLAN_HLEN * 2 + dp->mtu;
697 
698 	return fl_bufsz;
699 }
700 
701 static unsigned int nfp_net_calc_fl_bufsz(struct nfp_net_dp *dp)
702 {
703 	unsigned int fl_bufsz;
704 
705 	fl_bufsz = NFP_NET_RX_BUF_HEADROOM;
706 	fl_bufsz += dp->rx_dma_off;
707 	fl_bufsz += nfp_net_calc_fl_bufsz_data(dp);
708 
709 	fl_bufsz = SKB_DATA_ALIGN(fl_bufsz);
710 	fl_bufsz += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
711 
712 	return fl_bufsz;
713 }
714 
715 static unsigned int nfp_net_calc_fl_bufsz_xsk(struct nfp_net_dp *dp)
716 {
717 	unsigned int fl_bufsz;
718 
719 	fl_bufsz = XDP_PACKET_HEADROOM;
720 	fl_bufsz += nfp_net_calc_fl_bufsz_data(dp);
721 
722 	return fl_bufsz;
723 }
724 
725 /* Setup and Configuration
726  */
727 
728 /**
729  * nfp_net_vecs_init() - Assign IRQs and setup rvecs.
730  * @nn:		NFP Network structure
731  */
732 static void nfp_net_vecs_init(struct nfp_net *nn)
733 {
734 	struct nfp_net_r_vector *r_vec;
735 	int r;
736 
737 	nn->lsc_handler = nfp_net_irq_lsc;
738 	nn->exn_handler = nfp_net_irq_exn;
739 
740 	for (r = 0; r < nn->max_r_vecs; r++) {
741 		struct msix_entry *entry;
742 
743 		entry = &nn->irq_entries[NFP_NET_NON_Q_VECTORS + r];
744 
745 		r_vec = &nn->r_vecs[r];
746 		r_vec->nfp_net = nn;
747 		r_vec->irq_entry = entry->entry;
748 		r_vec->irq_vector = entry->vector;
749 
750 		if (nn->dp.netdev) {
751 			r_vec->handler = nfp_net_irq_rxtx;
752 		} else {
753 			r_vec->handler = nfp_ctrl_irq_rxtx;
754 
755 			__skb_queue_head_init(&r_vec->queue);
756 			spin_lock_init(&r_vec->lock);
757 			tasklet_setup(&r_vec->tasklet, nn->dp.ops->ctrl_poll);
758 			tasklet_disable(&r_vec->tasklet);
759 		}
760 
761 		cpumask_set_cpu(r, &r_vec->affinity_mask);
762 	}
763 }
764 
765 static void
766 nfp_net_napi_add(struct nfp_net_dp *dp, struct nfp_net_r_vector *r_vec, int idx)
767 {
768 	if (dp->netdev)
769 		netif_napi_add(dp->netdev, &r_vec->napi,
770 			       nfp_net_has_xsk_pool_slow(dp, idx) ?
771 			       dp->ops->xsk_poll : dp->ops->poll,
772 			       NAPI_POLL_WEIGHT);
773 	else
774 		tasklet_enable(&r_vec->tasklet);
775 }
776 
777 static void
778 nfp_net_napi_del(struct nfp_net_dp *dp, struct nfp_net_r_vector *r_vec)
779 {
780 	if (dp->netdev)
781 		netif_napi_del(&r_vec->napi);
782 	else
783 		tasklet_disable(&r_vec->tasklet);
784 }
785 
786 static void
787 nfp_net_vector_assign_rings(struct nfp_net_dp *dp,
788 			    struct nfp_net_r_vector *r_vec, int idx)
789 {
790 	r_vec->rx_ring = idx < dp->num_rx_rings ? &dp->rx_rings[idx] : NULL;
791 	r_vec->tx_ring =
792 		idx < dp->num_stack_tx_rings ? &dp->tx_rings[idx] : NULL;
793 
794 	r_vec->xdp_ring = idx < dp->num_tx_rings - dp->num_stack_tx_rings ?
795 		&dp->tx_rings[dp->num_stack_tx_rings + idx] : NULL;
796 
797 	if (nfp_net_has_xsk_pool_slow(dp, idx) || r_vec->xsk_pool) {
798 		r_vec->xsk_pool = dp->xdp_prog ? dp->xsk_pools[idx] : NULL;
799 
800 		if (r_vec->xsk_pool)
801 			xsk_pool_set_rxq_info(r_vec->xsk_pool,
802 					      &r_vec->rx_ring->xdp_rxq);
803 
804 		nfp_net_napi_del(dp, r_vec);
805 		nfp_net_napi_add(dp, r_vec, idx);
806 	}
807 }
808 
809 static int
810 nfp_net_prepare_vector(struct nfp_net *nn, struct nfp_net_r_vector *r_vec,
811 		       int idx)
812 {
813 	int err;
814 
815 	nfp_net_napi_add(&nn->dp, r_vec, idx);
816 
817 	snprintf(r_vec->name, sizeof(r_vec->name),
818 		 "%s-rxtx-%d", nfp_net_name(nn), idx);
819 	err = request_irq(r_vec->irq_vector, r_vec->handler, 0, r_vec->name,
820 			  r_vec);
821 	if (err) {
822 		nfp_net_napi_del(&nn->dp, r_vec);
823 		nn_err(nn, "Error requesting IRQ %d\n", r_vec->irq_vector);
824 		return err;
825 	}
826 	disable_irq(r_vec->irq_vector);
827 
828 	irq_set_affinity_hint(r_vec->irq_vector, &r_vec->affinity_mask);
829 
830 	nn_dbg(nn, "RV%02d: irq=%03d/%03d\n", idx, r_vec->irq_vector,
831 	       r_vec->irq_entry);
832 
833 	return 0;
834 }
835 
836 static void
837 nfp_net_cleanup_vector(struct nfp_net *nn, struct nfp_net_r_vector *r_vec)
838 {
839 	irq_set_affinity_hint(r_vec->irq_vector, NULL);
840 	nfp_net_napi_del(&nn->dp, r_vec);
841 	free_irq(r_vec->irq_vector, r_vec);
842 }
843 
844 /**
845  * nfp_net_rss_write_itbl() - Write RSS indirection table to device
846  * @nn:      NFP Net device to reconfigure
847  */
848 void nfp_net_rss_write_itbl(struct nfp_net *nn)
849 {
850 	int i;
851 
852 	for (i = 0; i < NFP_NET_CFG_RSS_ITBL_SZ; i += 4)
853 		nn_writel(nn, NFP_NET_CFG_RSS_ITBL + i,
854 			  get_unaligned_le32(nn->rss_itbl + i));
855 }
856 
857 /**
858  * nfp_net_rss_write_key() - Write RSS hash key to device
859  * @nn:      NFP Net device to reconfigure
860  */
861 void nfp_net_rss_write_key(struct nfp_net *nn)
862 {
863 	int i;
864 
865 	for (i = 0; i < nfp_net_rss_key_sz(nn); i += 4)
866 		nn_writel(nn, NFP_NET_CFG_RSS_KEY + i,
867 			  get_unaligned_le32(nn->rss_key + i));
868 }
869 
870 /**
871  * nfp_net_coalesce_write_cfg() - Write irq coalescence configuration to HW
872  * @nn:      NFP Net device to reconfigure
873  */
874 void nfp_net_coalesce_write_cfg(struct nfp_net *nn)
875 {
876 	u8 i;
877 	u32 factor;
878 	u32 value;
879 
880 	/* Compute factor used to convert coalesce '_usecs' parameters to
881 	 * ME timestamp ticks.  There are 16 ME clock cycles for each timestamp
882 	 * count.
883 	 */
884 	factor = nn->tlv_caps.me_freq_mhz / 16;
885 
886 	/* copy RX interrupt coalesce parameters */
887 	value = (nn->rx_coalesce_max_frames << 16) |
888 		(factor * nn->rx_coalesce_usecs);
889 	for (i = 0; i < nn->dp.num_rx_rings; i++)
890 		nn_writel(nn, NFP_NET_CFG_RXR_IRQ_MOD(i), value);
891 
892 	/* copy TX interrupt coalesce parameters */
893 	value = (nn->tx_coalesce_max_frames << 16) |
894 		(factor * nn->tx_coalesce_usecs);
895 	for (i = 0; i < nn->dp.num_tx_rings; i++)
896 		nn_writel(nn, NFP_NET_CFG_TXR_IRQ_MOD(i), value);
897 }
898 
899 /**
900  * nfp_net_write_mac_addr() - Write mac address to the device control BAR
901  * @nn:      NFP Net device to reconfigure
902  * @addr:    MAC address to write
903  *
904  * Writes the MAC address from the netdev to the device control BAR.  Does not
905  * perform the required reconfig.  We do a bit of byte swapping dance because
906  * firmware is LE.
907  */
908 static void nfp_net_write_mac_addr(struct nfp_net *nn, const u8 *addr)
909 {
910 	nn_writel(nn, NFP_NET_CFG_MACADDR + 0, get_unaligned_be32(addr));
911 	nn_writew(nn, NFP_NET_CFG_MACADDR + 6, get_unaligned_be16(addr + 4));
912 }
913 
914 /**
915  * nfp_net_clear_config_and_disable() - Clear control BAR and disable NFP
916  * @nn:      NFP Net device to reconfigure
917  *
918  * Warning: must be fully idempotent.
919  */
920 static void nfp_net_clear_config_and_disable(struct nfp_net *nn)
921 {
922 	u32 new_ctrl, update;
923 	unsigned int r;
924 	int err;
925 
926 	new_ctrl = nn->dp.ctrl;
927 	new_ctrl &= ~NFP_NET_CFG_CTRL_ENABLE;
928 	update = NFP_NET_CFG_UPDATE_GEN;
929 	update |= NFP_NET_CFG_UPDATE_MSIX;
930 	update |= NFP_NET_CFG_UPDATE_RING;
931 
932 	if (nn->cap & NFP_NET_CFG_CTRL_RINGCFG)
933 		new_ctrl &= ~NFP_NET_CFG_CTRL_RINGCFG;
934 
935 	nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, 0);
936 	nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, 0);
937 
938 	nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
939 	err = nfp_net_reconfig(nn, update);
940 	if (err)
941 		nn_err(nn, "Could not disable device: %d\n", err);
942 
943 	for (r = 0; r < nn->dp.num_rx_rings; r++) {
944 		nfp_net_rx_ring_reset(&nn->dp.rx_rings[r]);
945 		if (nfp_net_has_xsk_pool_slow(&nn->dp, nn->dp.rx_rings[r].idx))
946 			nfp_net_xsk_rx_bufs_free(&nn->dp.rx_rings[r]);
947 	}
948 	for (r = 0; r < nn->dp.num_tx_rings; r++)
949 		nfp_net_tx_ring_reset(&nn->dp, &nn->dp.tx_rings[r]);
950 	for (r = 0; r < nn->dp.num_r_vecs; r++)
951 		nfp_net_vec_clear_ring_data(nn, r);
952 
953 	nn->dp.ctrl = new_ctrl;
954 }
955 
956 /**
957  * nfp_net_set_config_and_enable() - Write control BAR and enable NFP
958  * @nn:      NFP Net device to reconfigure
959  */
960 static int nfp_net_set_config_and_enable(struct nfp_net *nn)
961 {
962 	u32 bufsz, new_ctrl, update = 0;
963 	unsigned int r;
964 	int err;
965 
966 	new_ctrl = nn->dp.ctrl;
967 
968 	if (nn->dp.ctrl & NFP_NET_CFG_CTRL_RSS_ANY) {
969 		nfp_net_rss_write_key(nn);
970 		nfp_net_rss_write_itbl(nn);
971 		nn_writel(nn, NFP_NET_CFG_RSS_CTRL, nn->rss_cfg);
972 		update |= NFP_NET_CFG_UPDATE_RSS;
973 	}
974 
975 	if (nn->dp.ctrl & NFP_NET_CFG_CTRL_IRQMOD) {
976 		nfp_net_coalesce_write_cfg(nn);
977 		update |= NFP_NET_CFG_UPDATE_IRQMOD;
978 	}
979 
980 	for (r = 0; r < nn->dp.num_tx_rings; r++)
981 		nfp_net_tx_ring_hw_cfg_write(nn, &nn->dp.tx_rings[r], r);
982 	for (r = 0; r < nn->dp.num_rx_rings; r++)
983 		nfp_net_rx_ring_hw_cfg_write(nn, &nn->dp.rx_rings[r], r);
984 
985 	nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE,
986 		  U64_MAX >> (64 - nn->dp.num_tx_rings));
987 
988 	nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE,
989 		  U64_MAX >> (64 - nn->dp.num_rx_rings));
990 
991 	if (nn->dp.netdev)
992 		nfp_net_write_mac_addr(nn, nn->dp.netdev->dev_addr);
993 
994 	nn_writel(nn, NFP_NET_CFG_MTU, nn->dp.mtu);
995 
996 	bufsz = nn->dp.fl_bufsz - nn->dp.rx_dma_off - NFP_NET_RX_BUF_NON_DATA;
997 	nn_writel(nn, NFP_NET_CFG_FLBUFSZ, bufsz);
998 
999 	/* Enable device */
1000 	new_ctrl |= NFP_NET_CFG_CTRL_ENABLE;
1001 	update |= NFP_NET_CFG_UPDATE_GEN;
1002 	update |= NFP_NET_CFG_UPDATE_MSIX;
1003 	update |= NFP_NET_CFG_UPDATE_RING;
1004 	if (nn->cap & NFP_NET_CFG_CTRL_RINGCFG)
1005 		new_ctrl |= NFP_NET_CFG_CTRL_RINGCFG;
1006 
1007 	nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
1008 	err = nfp_net_reconfig(nn, update);
1009 	if (err) {
1010 		nfp_net_clear_config_and_disable(nn);
1011 		return err;
1012 	}
1013 
1014 	nn->dp.ctrl = new_ctrl;
1015 
1016 	for (r = 0; r < nn->dp.num_rx_rings; r++)
1017 		nfp_net_rx_ring_fill_freelist(&nn->dp, &nn->dp.rx_rings[r]);
1018 
1019 	return 0;
1020 }
1021 
1022 /**
1023  * nfp_net_close_stack() - Quiesce the stack (part of close)
1024  * @nn:	     NFP Net device to reconfigure
1025  */
1026 static void nfp_net_close_stack(struct nfp_net *nn)
1027 {
1028 	struct nfp_net_r_vector *r_vec;
1029 	unsigned int r;
1030 
1031 	disable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
1032 	netif_carrier_off(nn->dp.netdev);
1033 	nn->link_up = false;
1034 
1035 	for (r = 0; r < nn->dp.num_r_vecs; r++) {
1036 		r_vec = &nn->r_vecs[r];
1037 
1038 		disable_irq(r_vec->irq_vector);
1039 		napi_disable(&r_vec->napi);
1040 
1041 		if (r_vec->rx_ring)
1042 			cancel_work_sync(&r_vec->rx_dim.work);
1043 
1044 		if (r_vec->tx_ring)
1045 			cancel_work_sync(&r_vec->tx_dim.work);
1046 	}
1047 
1048 	netif_tx_disable(nn->dp.netdev);
1049 }
1050 
1051 /**
1052  * nfp_net_close_free_all() - Free all runtime resources
1053  * @nn:      NFP Net device to reconfigure
1054  */
1055 static void nfp_net_close_free_all(struct nfp_net *nn)
1056 {
1057 	unsigned int r;
1058 
1059 	nfp_net_tx_rings_free(&nn->dp);
1060 	nfp_net_rx_rings_free(&nn->dp);
1061 
1062 	for (r = 0; r < nn->dp.num_r_vecs; r++)
1063 		nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
1064 
1065 	nfp_net_aux_irq_free(nn, NFP_NET_CFG_LSC, NFP_NET_IRQ_LSC_IDX);
1066 	nfp_net_aux_irq_free(nn, NFP_NET_CFG_EXN, NFP_NET_IRQ_EXN_IDX);
1067 }
1068 
1069 /**
1070  * nfp_net_netdev_close() - Called when the device is downed
1071  * @netdev:      netdev structure
1072  */
1073 static int nfp_net_netdev_close(struct net_device *netdev)
1074 {
1075 	struct nfp_net *nn = netdev_priv(netdev);
1076 
1077 	/* Step 1: Disable RX and TX rings from the Linux kernel perspective
1078 	 */
1079 	nfp_net_close_stack(nn);
1080 
1081 	/* Step 2: Tell NFP
1082 	 */
1083 	nfp_net_clear_config_and_disable(nn);
1084 	nfp_port_configure(netdev, false);
1085 
1086 	/* Step 3: Free resources
1087 	 */
1088 	nfp_net_close_free_all(nn);
1089 
1090 	nn_dbg(nn, "%s down", netdev->name);
1091 	return 0;
1092 }
1093 
1094 void nfp_ctrl_close(struct nfp_net *nn)
1095 {
1096 	int r;
1097 
1098 	rtnl_lock();
1099 
1100 	for (r = 0; r < nn->dp.num_r_vecs; r++) {
1101 		disable_irq(nn->r_vecs[r].irq_vector);
1102 		tasklet_disable(&nn->r_vecs[r].tasklet);
1103 	}
1104 
1105 	nfp_net_clear_config_and_disable(nn);
1106 
1107 	nfp_net_close_free_all(nn);
1108 
1109 	rtnl_unlock();
1110 }
1111 
1112 static void nfp_net_rx_dim_work(struct work_struct *work)
1113 {
1114 	struct nfp_net_r_vector *r_vec;
1115 	unsigned int factor, value;
1116 	struct dim_cq_moder moder;
1117 	struct nfp_net *nn;
1118 	struct dim *dim;
1119 
1120 	dim = container_of(work, struct dim, work);
1121 	moder = net_dim_get_rx_moderation(dim->mode, dim->profile_ix);
1122 	r_vec = container_of(dim, struct nfp_net_r_vector, rx_dim);
1123 	nn = r_vec->nfp_net;
1124 
1125 	/* Compute factor used to convert coalesce '_usecs' parameters to
1126 	 * ME timestamp ticks.  There are 16 ME clock cycles for each timestamp
1127 	 * count.
1128 	 */
1129 	factor = nn->tlv_caps.me_freq_mhz / 16;
1130 	if (nfp_net_coalesce_para_check(factor * moder.usec, moder.pkts))
1131 		return;
1132 
1133 	/* copy RX interrupt coalesce parameters */
1134 	value = (moder.pkts << 16) | (factor * moder.usec);
1135 	nn_writel(nn, NFP_NET_CFG_RXR_IRQ_MOD(r_vec->rx_ring->idx), value);
1136 	(void)nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_IRQMOD);
1137 
1138 	dim->state = DIM_START_MEASURE;
1139 }
1140 
1141 static void nfp_net_tx_dim_work(struct work_struct *work)
1142 {
1143 	struct nfp_net_r_vector *r_vec;
1144 	unsigned int factor, value;
1145 	struct dim_cq_moder moder;
1146 	struct nfp_net *nn;
1147 	struct dim *dim;
1148 
1149 	dim = container_of(work, struct dim, work);
1150 	moder = net_dim_get_tx_moderation(dim->mode, dim->profile_ix);
1151 	r_vec = container_of(dim, struct nfp_net_r_vector, tx_dim);
1152 	nn = r_vec->nfp_net;
1153 
1154 	/* Compute factor used to convert coalesce '_usecs' parameters to
1155 	 * ME timestamp ticks.  There are 16 ME clock cycles for each timestamp
1156 	 * count.
1157 	 */
1158 	factor = nn->tlv_caps.me_freq_mhz / 16;
1159 	if (nfp_net_coalesce_para_check(factor * moder.usec, moder.pkts))
1160 		return;
1161 
1162 	/* copy TX interrupt coalesce parameters */
1163 	value = (moder.pkts << 16) | (factor * moder.usec);
1164 	nn_writel(nn, NFP_NET_CFG_TXR_IRQ_MOD(r_vec->tx_ring->idx), value);
1165 	(void)nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_IRQMOD);
1166 
1167 	dim->state = DIM_START_MEASURE;
1168 }
1169 
1170 /**
1171  * nfp_net_open_stack() - Start the device from stack's perspective
1172  * @nn:      NFP Net device to reconfigure
1173  */
1174 static void nfp_net_open_stack(struct nfp_net *nn)
1175 {
1176 	struct nfp_net_r_vector *r_vec;
1177 	unsigned int r;
1178 
1179 	for (r = 0; r < nn->dp.num_r_vecs; r++) {
1180 		r_vec = &nn->r_vecs[r];
1181 
1182 		if (r_vec->rx_ring) {
1183 			INIT_WORK(&r_vec->rx_dim.work, nfp_net_rx_dim_work);
1184 			r_vec->rx_dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
1185 		}
1186 
1187 		if (r_vec->tx_ring) {
1188 			INIT_WORK(&r_vec->tx_dim.work, nfp_net_tx_dim_work);
1189 			r_vec->tx_dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
1190 		}
1191 
1192 		napi_enable(&r_vec->napi);
1193 		enable_irq(r_vec->irq_vector);
1194 	}
1195 
1196 	netif_tx_wake_all_queues(nn->dp.netdev);
1197 
1198 	enable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
1199 	nfp_net_read_link_status(nn);
1200 }
1201 
1202 static int nfp_net_open_alloc_all(struct nfp_net *nn)
1203 {
1204 	int err, r;
1205 
1206 	err = nfp_net_aux_irq_request(nn, NFP_NET_CFG_EXN, "%s-exn",
1207 				      nn->exn_name, sizeof(nn->exn_name),
1208 				      NFP_NET_IRQ_EXN_IDX, nn->exn_handler);
1209 	if (err)
1210 		return err;
1211 	err = nfp_net_aux_irq_request(nn, NFP_NET_CFG_LSC, "%s-lsc",
1212 				      nn->lsc_name, sizeof(nn->lsc_name),
1213 				      NFP_NET_IRQ_LSC_IDX, nn->lsc_handler);
1214 	if (err)
1215 		goto err_free_exn;
1216 	disable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
1217 
1218 	for (r = 0; r < nn->dp.num_r_vecs; r++) {
1219 		err = nfp_net_prepare_vector(nn, &nn->r_vecs[r], r);
1220 		if (err)
1221 			goto err_cleanup_vec_p;
1222 	}
1223 
1224 	err = nfp_net_rx_rings_prepare(nn, &nn->dp);
1225 	if (err)
1226 		goto err_cleanup_vec;
1227 
1228 	err = nfp_net_tx_rings_prepare(nn, &nn->dp);
1229 	if (err)
1230 		goto err_free_rx_rings;
1231 
1232 	for (r = 0; r < nn->max_r_vecs; r++)
1233 		nfp_net_vector_assign_rings(&nn->dp, &nn->r_vecs[r], r);
1234 
1235 	return 0;
1236 
1237 err_free_rx_rings:
1238 	nfp_net_rx_rings_free(&nn->dp);
1239 err_cleanup_vec:
1240 	r = nn->dp.num_r_vecs;
1241 err_cleanup_vec_p:
1242 	while (r--)
1243 		nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
1244 	nfp_net_aux_irq_free(nn, NFP_NET_CFG_LSC, NFP_NET_IRQ_LSC_IDX);
1245 err_free_exn:
1246 	nfp_net_aux_irq_free(nn, NFP_NET_CFG_EXN, NFP_NET_IRQ_EXN_IDX);
1247 	return err;
1248 }
1249 
1250 static int nfp_net_netdev_open(struct net_device *netdev)
1251 {
1252 	struct nfp_net *nn = netdev_priv(netdev);
1253 	int err;
1254 
1255 	/* Step 1: Allocate resources for rings and the like
1256 	 * - Request interrupts
1257 	 * - Allocate RX and TX ring resources
1258 	 * - Setup initial RSS table
1259 	 */
1260 	err = nfp_net_open_alloc_all(nn);
1261 	if (err)
1262 		return err;
1263 
1264 	err = netif_set_real_num_tx_queues(netdev, nn->dp.num_stack_tx_rings);
1265 	if (err)
1266 		goto err_free_all;
1267 
1268 	err = netif_set_real_num_rx_queues(netdev, nn->dp.num_rx_rings);
1269 	if (err)
1270 		goto err_free_all;
1271 
1272 	/* Step 2: Configure the NFP
1273 	 * - Ifup the physical interface if it exists
1274 	 * - Enable rings from 0 to tx_rings/rx_rings - 1.
1275 	 * - Write MAC address (in case it changed)
1276 	 * - Set the MTU
1277 	 * - Set the Freelist buffer size
1278 	 * - Enable the FW
1279 	 */
1280 	err = nfp_port_configure(netdev, true);
1281 	if (err)
1282 		goto err_free_all;
1283 
1284 	err = nfp_net_set_config_and_enable(nn);
1285 	if (err)
1286 		goto err_port_disable;
1287 
1288 	/* Step 3: Enable for kernel
1289 	 * - put some freelist descriptors on each RX ring
1290 	 * - enable NAPI on each ring
1291 	 * - enable all TX queues
1292 	 * - set link state
1293 	 */
1294 	nfp_net_open_stack(nn);
1295 
1296 	return 0;
1297 
1298 err_port_disable:
1299 	nfp_port_configure(netdev, false);
1300 err_free_all:
1301 	nfp_net_close_free_all(nn);
1302 	return err;
1303 }
1304 
1305 int nfp_ctrl_open(struct nfp_net *nn)
1306 {
1307 	int err, r;
1308 
1309 	/* ring dumping depends on vNICs being opened/closed under rtnl */
1310 	rtnl_lock();
1311 
1312 	err = nfp_net_open_alloc_all(nn);
1313 	if (err)
1314 		goto err_unlock;
1315 
1316 	err = nfp_net_set_config_and_enable(nn);
1317 	if (err)
1318 		goto err_free_all;
1319 
1320 	for (r = 0; r < nn->dp.num_r_vecs; r++)
1321 		enable_irq(nn->r_vecs[r].irq_vector);
1322 
1323 	rtnl_unlock();
1324 
1325 	return 0;
1326 
1327 err_free_all:
1328 	nfp_net_close_free_all(nn);
1329 err_unlock:
1330 	rtnl_unlock();
1331 	return err;
1332 }
1333 
1334 static void nfp_net_set_rx_mode(struct net_device *netdev)
1335 {
1336 	struct nfp_net *nn = netdev_priv(netdev);
1337 	u32 new_ctrl;
1338 
1339 	new_ctrl = nn->dp.ctrl;
1340 
1341 	if (!netdev_mc_empty(netdev) || netdev->flags & IFF_ALLMULTI)
1342 		new_ctrl |= nn->cap & NFP_NET_CFG_CTRL_L2MC;
1343 	else
1344 		new_ctrl &= ~NFP_NET_CFG_CTRL_L2MC;
1345 
1346 	if (netdev->flags & IFF_PROMISC) {
1347 		if (nn->cap & NFP_NET_CFG_CTRL_PROMISC)
1348 			new_ctrl |= NFP_NET_CFG_CTRL_PROMISC;
1349 		else
1350 			nn_warn(nn, "FW does not support promiscuous mode\n");
1351 	} else {
1352 		new_ctrl &= ~NFP_NET_CFG_CTRL_PROMISC;
1353 	}
1354 
1355 	if (new_ctrl == nn->dp.ctrl)
1356 		return;
1357 
1358 	nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
1359 	nfp_net_reconfig_post(nn, NFP_NET_CFG_UPDATE_GEN);
1360 
1361 	nn->dp.ctrl = new_ctrl;
1362 }
1363 
1364 static void nfp_net_rss_init_itbl(struct nfp_net *nn)
1365 {
1366 	int i;
1367 
1368 	for (i = 0; i < sizeof(nn->rss_itbl); i++)
1369 		nn->rss_itbl[i] =
1370 			ethtool_rxfh_indir_default(i, nn->dp.num_rx_rings);
1371 }
1372 
1373 static void nfp_net_dp_swap(struct nfp_net *nn, struct nfp_net_dp *dp)
1374 {
1375 	struct nfp_net_dp new_dp = *dp;
1376 
1377 	*dp = nn->dp;
1378 	nn->dp = new_dp;
1379 
1380 	nn->dp.netdev->mtu = new_dp.mtu;
1381 
1382 	if (!netif_is_rxfh_configured(nn->dp.netdev))
1383 		nfp_net_rss_init_itbl(nn);
1384 }
1385 
1386 static int nfp_net_dp_swap_enable(struct nfp_net *nn, struct nfp_net_dp *dp)
1387 {
1388 	unsigned int r;
1389 	int err;
1390 
1391 	nfp_net_dp_swap(nn, dp);
1392 
1393 	for (r = 0; r <	nn->max_r_vecs; r++)
1394 		nfp_net_vector_assign_rings(&nn->dp, &nn->r_vecs[r], r);
1395 
1396 	err = netif_set_real_num_queues(nn->dp.netdev,
1397 					nn->dp.num_stack_tx_rings,
1398 					nn->dp.num_rx_rings);
1399 	if (err)
1400 		return err;
1401 
1402 	return nfp_net_set_config_and_enable(nn);
1403 }
1404 
1405 struct nfp_net_dp *nfp_net_clone_dp(struct nfp_net *nn)
1406 {
1407 	struct nfp_net_dp *new;
1408 
1409 	new = kmalloc(sizeof(*new), GFP_KERNEL);
1410 	if (!new)
1411 		return NULL;
1412 
1413 	*new = nn->dp;
1414 
1415 	new->xsk_pools = kmemdup(new->xsk_pools,
1416 				 array_size(nn->max_r_vecs,
1417 					    sizeof(new->xsk_pools)),
1418 				 GFP_KERNEL);
1419 	if (!new->xsk_pools) {
1420 		kfree(new);
1421 		return NULL;
1422 	}
1423 
1424 	/* Clear things which need to be recomputed */
1425 	new->fl_bufsz = 0;
1426 	new->tx_rings = NULL;
1427 	new->rx_rings = NULL;
1428 	new->num_r_vecs = 0;
1429 	new->num_stack_tx_rings = 0;
1430 	new->txrwb = NULL;
1431 	new->txrwb_dma = 0;
1432 
1433 	return new;
1434 }
1435 
1436 static void nfp_net_free_dp(struct nfp_net_dp *dp)
1437 {
1438 	kfree(dp->xsk_pools);
1439 	kfree(dp);
1440 }
1441 
1442 static int
1443 nfp_net_check_config(struct nfp_net *nn, struct nfp_net_dp *dp,
1444 		     struct netlink_ext_ack *extack)
1445 {
1446 	unsigned int r, xsk_min_fl_bufsz;
1447 
1448 	/* XDP-enabled tests */
1449 	if (!dp->xdp_prog)
1450 		return 0;
1451 	if (dp->fl_bufsz > PAGE_SIZE) {
1452 		NL_SET_ERR_MSG_MOD(extack, "MTU too large w/ XDP enabled");
1453 		return -EINVAL;
1454 	}
1455 	if (dp->num_tx_rings > nn->max_tx_rings) {
1456 		NL_SET_ERR_MSG_MOD(extack, "Insufficient number of TX rings w/ XDP enabled");
1457 		return -EINVAL;
1458 	}
1459 
1460 	xsk_min_fl_bufsz = nfp_net_calc_fl_bufsz_xsk(dp);
1461 	for (r = 0; r < nn->max_r_vecs; r++) {
1462 		if (!dp->xsk_pools[r])
1463 			continue;
1464 
1465 		if (xsk_pool_get_rx_frame_size(dp->xsk_pools[r]) < xsk_min_fl_bufsz) {
1466 			NL_SET_ERR_MSG_MOD(extack,
1467 					   "XSK buffer pool chunk size too small");
1468 			return -EINVAL;
1469 		}
1470 	}
1471 
1472 	return 0;
1473 }
1474 
1475 int nfp_net_ring_reconfig(struct nfp_net *nn, struct nfp_net_dp *dp,
1476 			  struct netlink_ext_ack *extack)
1477 {
1478 	int r, err;
1479 
1480 	dp->fl_bufsz = nfp_net_calc_fl_bufsz(dp);
1481 
1482 	dp->num_stack_tx_rings = dp->num_tx_rings;
1483 	if (dp->xdp_prog)
1484 		dp->num_stack_tx_rings -= dp->num_rx_rings;
1485 
1486 	dp->num_r_vecs = max(dp->num_rx_rings, dp->num_stack_tx_rings);
1487 
1488 	err = nfp_net_check_config(nn, dp, extack);
1489 	if (err)
1490 		goto exit_free_dp;
1491 
1492 	if (!netif_running(dp->netdev)) {
1493 		nfp_net_dp_swap(nn, dp);
1494 		err = 0;
1495 		goto exit_free_dp;
1496 	}
1497 
1498 	/* Prepare new rings */
1499 	for (r = nn->dp.num_r_vecs; r < dp->num_r_vecs; r++) {
1500 		err = nfp_net_prepare_vector(nn, &nn->r_vecs[r], r);
1501 		if (err) {
1502 			dp->num_r_vecs = r;
1503 			goto err_cleanup_vecs;
1504 		}
1505 	}
1506 
1507 	err = nfp_net_rx_rings_prepare(nn, dp);
1508 	if (err)
1509 		goto err_cleanup_vecs;
1510 
1511 	err = nfp_net_tx_rings_prepare(nn, dp);
1512 	if (err)
1513 		goto err_free_rx;
1514 
1515 	/* Stop device, swap in new rings, try to start the firmware */
1516 	nfp_net_close_stack(nn);
1517 	nfp_net_clear_config_and_disable(nn);
1518 
1519 	err = nfp_net_dp_swap_enable(nn, dp);
1520 	if (err) {
1521 		int err2;
1522 
1523 		nfp_net_clear_config_and_disable(nn);
1524 
1525 		/* Try with old configuration and old rings */
1526 		err2 = nfp_net_dp_swap_enable(nn, dp);
1527 		if (err2)
1528 			nn_err(nn, "Can't restore ring config - FW communication failed (%d,%d)\n",
1529 			       err, err2);
1530 	}
1531 	for (r = dp->num_r_vecs - 1; r >= nn->dp.num_r_vecs; r--)
1532 		nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
1533 
1534 	nfp_net_rx_rings_free(dp);
1535 	nfp_net_tx_rings_free(dp);
1536 
1537 	nfp_net_open_stack(nn);
1538 exit_free_dp:
1539 	nfp_net_free_dp(dp);
1540 
1541 	return err;
1542 
1543 err_free_rx:
1544 	nfp_net_rx_rings_free(dp);
1545 err_cleanup_vecs:
1546 	for (r = dp->num_r_vecs - 1; r >= nn->dp.num_r_vecs; r--)
1547 		nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
1548 	nfp_net_free_dp(dp);
1549 	return err;
1550 }
1551 
1552 static int nfp_net_change_mtu(struct net_device *netdev, int new_mtu)
1553 {
1554 	struct nfp_net *nn = netdev_priv(netdev);
1555 	struct nfp_net_dp *dp;
1556 	int err;
1557 
1558 	err = nfp_app_check_mtu(nn->app, netdev, new_mtu);
1559 	if (err)
1560 		return err;
1561 
1562 	dp = nfp_net_clone_dp(nn);
1563 	if (!dp)
1564 		return -ENOMEM;
1565 
1566 	dp->mtu = new_mtu;
1567 
1568 	return nfp_net_ring_reconfig(nn, dp, NULL);
1569 }
1570 
1571 static int
1572 nfp_net_vlan_rx_add_vid(struct net_device *netdev, __be16 proto, u16 vid)
1573 {
1574 	const u32 cmd = NFP_NET_CFG_MBOX_CMD_CTAG_FILTER_ADD;
1575 	struct nfp_net *nn = netdev_priv(netdev);
1576 	int err;
1577 
1578 	/* Priority tagged packets with vlan id 0 are processed by the
1579 	 * NFP as untagged packets
1580 	 */
1581 	if (!vid)
1582 		return 0;
1583 
1584 	err = nfp_net_mbox_lock(nn, NFP_NET_CFG_VLAN_FILTER_SZ);
1585 	if (err)
1586 		return err;
1587 
1588 	nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_VLAN_FILTER_VID, vid);
1589 	nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_VLAN_FILTER_PROTO,
1590 		  ETH_P_8021Q);
1591 
1592 	return nfp_net_mbox_reconfig_and_unlock(nn, cmd);
1593 }
1594 
1595 static int
1596 nfp_net_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto, u16 vid)
1597 {
1598 	const u32 cmd = NFP_NET_CFG_MBOX_CMD_CTAG_FILTER_KILL;
1599 	struct nfp_net *nn = netdev_priv(netdev);
1600 	int err;
1601 
1602 	/* Priority tagged packets with vlan id 0 are processed by the
1603 	 * NFP as untagged packets
1604 	 */
1605 	if (!vid)
1606 		return 0;
1607 
1608 	err = nfp_net_mbox_lock(nn, NFP_NET_CFG_VLAN_FILTER_SZ);
1609 	if (err)
1610 		return err;
1611 
1612 	nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_VLAN_FILTER_VID, vid);
1613 	nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_VLAN_FILTER_PROTO,
1614 		  ETH_P_8021Q);
1615 
1616 	return nfp_net_mbox_reconfig_and_unlock(nn, cmd);
1617 }
1618 
1619 static void nfp_net_stat64(struct net_device *netdev,
1620 			   struct rtnl_link_stats64 *stats)
1621 {
1622 	struct nfp_net *nn = netdev_priv(netdev);
1623 	int r;
1624 
1625 	/* Collect software stats */
1626 	for (r = 0; r < nn->max_r_vecs; r++) {
1627 		struct nfp_net_r_vector *r_vec = &nn->r_vecs[r];
1628 		u64 data[3];
1629 		unsigned int start;
1630 
1631 		do {
1632 			start = u64_stats_fetch_begin(&r_vec->rx_sync);
1633 			data[0] = r_vec->rx_pkts;
1634 			data[1] = r_vec->rx_bytes;
1635 			data[2] = r_vec->rx_drops;
1636 		} while (u64_stats_fetch_retry(&r_vec->rx_sync, start));
1637 		stats->rx_packets += data[0];
1638 		stats->rx_bytes += data[1];
1639 		stats->rx_dropped += data[2];
1640 
1641 		do {
1642 			start = u64_stats_fetch_begin(&r_vec->tx_sync);
1643 			data[0] = r_vec->tx_pkts;
1644 			data[1] = r_vec->tx_bytes;
1645 			data[2] = r_vec->tx_errors;
1646 		} while (u64_stats_fetch_retry(&r_vec->tx_sync, start));
1647 		stats->tx_packets += data[0];
1648 		stats->tx_bytes += data[1];
1649 		stats->tx_errors += data[2];
1650 	}
1651 
1652 	/* Add in device stats */
1653 	stats->multicast += nn_readq(nn, NFP_NET_CFG_STATS_RX_MC_FRAMES);
1654 	stats->rx_dropped += nn_readq(nn, NFP_NET_CFG_STATS_RX_DISCARDS);
1655 	stats->rx_errors += nn_readq(nn, NFP_NET_CFG_STATS_RX_ERRORS);
1656 
1657 	stats->tx_dropped += nn_readq(nn, NFP_NET_CFG_STATS_TX_DISCARDS);
1658 	stats->tx_errors += nn_readq(nn, NFP_NET_CFG_STATS_TX_ERRORS);
1659 }
1660 
1661 static int nfp_net_set_features(struct net_device *netdev,
1662 				netdev_features_t features)
1663 {
1664 	netdev_features_t changed = netdev->features ^ features;
1665 	struct nfp_net *nn = netdev_priv(netdev);
1666 	u32 new_ctrl;
1667 	int err;
1668 
1669 	/* Assume this is not called with features we have not advertised */
1670 
1671 	new_ctrl = nn->dp.ctrl;
1672 
1673 	if (changed & NETIF_F_RXCSUM) {
1674 		if (features & NETIF_F_RXCSUM)
1675 			new_ctrl |= nn->cap & NFP_NET_CFG_CTRL_RXCSUM_ANY;
1676 		else
1677 			new_ctrl &= ~NFP_NET_CFG_CTRL_RXCSUM_ANY;
1678 	}
1679 
1680 	if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
1681 		if (features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))
1682 			new_ctrl |= NFP_NET_CFG_CTRL_TXCSUM;
1683 		else
1684 			new_ctrl &= ~NFP_NET_CFG_CTRL_TXCSUM;
1685 	}
1686 
1687 	if (changed & (NETIF_F_TSO | NETIF_F_TSO6)) {
1688 		if (features & (NETIF_F_TSO | NETIF_F_TSO6))
1689 			new_ctrl |= nn->cap & NFP_NET_CFG_CTRL_LSO2 ?:
1690 					      NFP_NET_CFG_CTRL_LSO;
1691 		else
1692 			new_ctrl &= ~NFP_NET_CFG_CTRL_LSO_ANY;
1693 	}
1694 
1695 	if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
1696 		if (features & NETIF_F_HW_VLAN_CTAG_RX)
1697 			new_ctrl |= NFP_NET_CFG_CTRL_RXVLAN;
1698 		else
1699 			new_ctrl &= ~NFP_NET_CFG_CTRL_RXVLAN;
1700 	}
1701 
1702 	if (changed & NETIF_F_HW_VLAN_CTAG_TX) {
1703 		if (features & NETIF_F_HW_VLAN_CTAG_TX)
1704 			new_ctrl |= NFP_NET_CFG_CTRL_TXVLAN;
1705 		else
1706 			new_ctrl &= ~NFP_NET_CFG_CTRL_TXVLAN;
1707 	}
1708 
1709 	if (changed & NETIF_F_HW_VLAN_CTAG_FILTER) {
1710 		if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
1711 			new_ctrl |= NFP_NET_CFG_CTRL_CTAG_FILTER;
1712 		else
1713 			new_ctrl &= ~NFP_NET_CFG_CTRL_CTAG_FILTER;
1714 	}
1715 
1716 	if (changed & NETIF_F_SG) {
1717 		if (features & NETIF_F_SG)
1718 			new_ctrl |= NFP_NET_CFG_CTRL_GATHER;
1719 		else
1720 			new_ctrl &= ~NFP_NET_CFG_CTRL_GATHER;
1721 	}
1722 
1723 	err = nfp_port_set_features(netdev, features);
1724 	if (err)
1725 		return err;
1726 
1727 	nn_dbg(nn, "Feature change 0x%llx -> 0x%llx (changed=0x%llx)\n",
1728 	       netdev->features, features, changed);
1729 
1730 	if (new_ctrl == nn->dp.ctrl)
1731 		return 0;
1732 
1733 	nn_dbg(nn, "NIC ctrl: 0x%x -> 0x%x\n", nn->dp.ctrl, new_ctrl);
1734 	nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
1735 	err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_GEN);
1736 	if (err)
1737 		return err;
1738 
1739 	nn->dp.ctrl = new_ctrl;
1740 
1741 	return 0;
1742 }
1743 
1744 static netdev_features_t
1745 nfp_net_features_check(struct sk_buff *skb, struct net_device *dev,
1746 		       netdev_features_t features)
1747 {
1748 	u8 l4_hdr;
1749 
1750 	/* We can't do TSO over double tagged packets (802.1AD) */
1751 	features &= vlan_features_check(skb, features);
1752 
1753 	if (!skb->encapsulation)
1754 		return features;
1755 
1756 	/* Ensure that inner L4 header offset fits into TX descriptor field */
1757 	if (skb_is_gso(skb)) {
1758 		u32 hdrlen;
1759 
1760 		hdrlen = skb_inner_transport_header(skb) - skb->data +
1761 			inner_tcp_hdrlen(skb);
1762 
1763 		/* Assume worst case scenario of having longest possible
1764 		 * metadata prepend - 8B
1765 		 */
1766 		if (unlikely(hdrlen > NFP_NET_LSO_MAX_HDR_SZ - 8))
1767 			features &= ~NETIF_F_GSO_MASK;
1768 	}
1769 
1770 	/* VXLAN/GRE check */
1771 	switch (vlan_get_protocol(skb)) {
1772 	case htons(ETH_P_IP):
1773 		l4_hdr = ip_hdr(skb)->protocol;
1774 		break;
1775 	case htons(ETH_P_IPV6):
1776 		l4_hdr = ipv6_hdr(skb)->nexthdr;
1777 		break;
1778 	default:
1779 		return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
1780 	}
1781 
1782 	if (skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
1783 	    skb->inner_protocol != htons(ETH_P_TEB) ||
1784 	    (l4_hdr != IPPROTO_UDP && l4_hdr != IPPROTO_GRE) ||
1785 	    (l4_hdr == IPPROTO_UDP &&
1786 	     (skb_inner_mac_header(skb) - skb_transport_header(skb) !=
1787 	      sizeof(struct udphdr) + sizeof(struct vxlanhdr))))
1788 		return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
1789 
1790 	return features;
1791 }
1792 
1793 static int
1794 nfp_net_get_phys_port_name(struct net_device *netdev, char *name, size_t len)
1795 {
1796 	struct nfp_net *nn = netdev_priv(netdev);
1797 	int n;
1798 
1799 	/* If port is defined, devlink_port is registered and devlink core
1800 	 * is taking care of name formatting.
1801 	 */
1802 	if (nn->port)
1803 		return -EOPNOTSUPP;
1804 
1805 	if (nn->dp.is_vf || nn->vnic_no_name)
1806 		return -EOPNOTSUPP;
1807 
1808 	n = snprintf(name, len, "n%d", nn->id);
1809 	if (n >= len)
1810 		return -EINVAL;
1811 
1812 	return 0;
1813 }
1814 
1815 static int nfp_net_xdp_setup_drv(struct nfp_net *nn, struct netdev_bpf *bpf)
1816 {
1817 	struct bpf_prog *prog = bpf->prog;
1818 	struct nfp_net_dp *dp;
1819 	int err;
1820 
1821 	if (!prog == !nn->dp.xdp_prog) {
1822 		WRITE_ONCE(nn->dp.xdp_prog, prog);
1823 		xdp_attachment_setup(&nn->xdp, bpf);
1824 		return 0;
1825 	}
1826 
1827 	dp = nfp_net_clone_dp(nn);
1828 	if (!dp)
1829 		return -ENOMEM;
1830 
1831 	dp->xdp_prog = prog;
1832 	dp->num_tx_rings += prog ? nn->dp.num_rx_rings : -nn->dp.num_rx_rings;
1833 	dp->rx_dma_dir = prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
1834 	dp->rx_dma_off = prog ? XDP_PACKET_HEADROOM - nn->dp.rx_offset : 0;
1835 
1836 	/* We need RX reconfig to remap the buffers (BIDIR vs FROM_DEV) */
1837 	err = nfp_net_ring_reconfig(nn, dp, bpf->extack);
1838 	if (err)
1839 		return err;
1840 
1841 	xdp_attachment_setup(&nn->xdp, bpf);
1842 	return 0;
1843 }
1844 
1845 static int nfp_net_xdp_setup_hw(struct nfp_net *nn, struct netdev_bpf *bpf)
1846 {
1847 	int err;
1848 
1849 	err = nfp_app_xdp_offload(nn->app, nn, bpf->prog, bpf->extack);
1850 	if (err)
1851 		return err;
1852 
1853 	xdp_attachment_setup(&nn->xdp_hw, bpf);
1854 	return 0;
1855 }
1856 
1857 static int nfp_net_xdp(struct net_device *netdev, struct netdev_bpf *xdp)
1858 {
1859 	struct nfp_net *nn = netdev_priv(netdev);
1860 
1861 	switch (xdp->command) {
1862 	case XDP_SETUP_PROG:
1863 		return nfp_net_xdp_setup_drv(nn, xdp);
1864 	case XDP_SETUP_PROG_HW:
1865 		return nfp_net_xdp_setup_hw(nn, xdp);
1866 	case XDP_SETUP_XSK_POOL:
1867 		return nfp_net_xsk_setup_pool(netdev, xdp->xsk.pool,
1868 					      xdp->xsk.queue_id);
1869 	default:
1870 		return nfp_app_bpf(nn->app, nn, xdp);
1871 	}
1872 }
1873 
1874 static int nfp_net_set_mac_address(struct net_device *netdev, void *addr)
1875 {
1876 	struct nfp_net *nn = netdev_priv(netdev);
1877 	struct sockaddr *saddr = addr;
1878 	int err;
1879 
1880 	err = eth_prepare_mac_addr_change(netdev, addr);
1881 	if (err)
1882 		return err;
1883 
1884 	nfp_net_write_mac_addr(nn, saddr->sa_data);
1885 
1886 	err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_MACADDR);
1887 	if (err)
1888 		return err;
1889 
1890 	eth_commit_mac_addr_change(netdev, addr);
1891 
1892 	return 0;
1893 }
1894 
1895 const struct net_device_ops nfp_nfd3_netdev_ops = {
1896 	.ndo_init		= nfp_app_ndo_init,
1897 	.ndo_uninit		= nfp_app_ndo_uninit,
1898 	.ndo_open		= nfp_net_netdev_open,
1899 	.ndo_stop		= nfp_net_netdev_close,
1900 	.ndo_start_xmit		= nfp_net_tx,
1901 	.ndo_get_stats64	= nfp_net_stat64,
1902 	.ndo_vlan_rx_add_vid	= nfp_net_vlan_rx_add_vid,
1903 	.ndo_vlan_rx_kill_vid	= nfp_net_vlan_rx_kill_vid,
1904 	.ndo_set_vf_mac         = nfp_app_set_vf_mac,
1905 	.ndo_set_vf_vlan        = nfp_app_set_vf_vlan,
1906 	.ndo_set_vf_spoofchk    = nfp_app_set_vf_spoofchk,
1907 	.ndo_set_vf_trust	= nfp_app_set_vf_trust,
1908 	.ndo_get_vf_config	= nfp_app_get_vf_config,
1909 	.ndo_set_vf_link_state  = nfp_app_set_vf_link_state,
1910 	.ndo_setup_tc		= nfp_port_setup_tc,
1911 	.ndo_tx_timeout		= nfp_net_tx_timeout,
1912 	.ndo_set_rx_mode	= nfp_net_set_rx_mode,
1913 	.ndo_change_mtu		= nfp_net_change_mtu,
1914 	.ndo_set_mac_address	= nfp_net_set_mac_address,
1915 	.ndo_set_features	= nfp_net_set_features,
1916 	.ndo_features_check	= nfp_net_features_check,
1917 	.ndo_get_phys_port_name	= nfp_net_get_phys_port_name,
1918 	.ndo_bpf		= nfp_net_xdp,
1919 	.ndo_xsk_wakeup		= nfp_net_xsk_wakeup,
1920 	.ndo_get_devlink_port	= nfp_devlink_get_devlink_port,
1921 };
1922 
1923 const struct net_device_ops nfp_nfdk_netdev_ops = {
1924 	.ndo_init		= nfp_app_ndo_init,
1925 	.ndo_uninit		= nfp_app_ndo_uninit,
1926 	.ndo_open		= nfp_net_netdev_open,
1927 	.ndo_stop		= nfp_net_netdev_close,
1928 	.ndo_start_xmit		= nfp_net_tx,
1929 	.ndo_get_stats64	= nfp_net_stat64,
1930 	.ndo_vlan_rx_add_vid	= nfp_net_vlan_rx_add_vid,
1931 	.ndo_vlan_rx_kill_vid	= nfp_net_vlan_rx_kill_vid,
1932 	.ndo_set_vf_mac         = nfp_app_set_vf_mac,
1933 	.ndo_set_vf_vlan        = nfp_app_set_vf_vlan,
1934 	.ndo_set_vf_spoofchk    = nfp_app_set_vf_spoofchk,
1935 	.ndo_set_vf_trust	= nfp_app_set_vf_trust,
1936 	.ndo_get_vf_config	= nfp_app_get_vf_config,
1937 	.ndo_set_vf_link_state  = nfp_app_set_vf_link_state,
1938 	.ndo_setup_tc		= nfp_port_setup_tc,
1939 	.ndo_tx_timeout		= nfp_net_tx_timeout,
1940 	.ndo_set_rx_mode	= nfp_net_set_rx_mode,
1941 	.ndo_change_mtu		= nfp_net_change_mtu,
1942 	.ndo_set_mac_address	= nfp_net_set_mac_address,
1943 	.ndo_set_features	= nfp_net_set_features,
1944 	.ndo_features_check	= nfp_net_features_check,
1945 	.ndo_get_phys_port_name	= nfp_net_get_phys_port_name,
1946 	.ndo_bpf		= nfp_net_xdp,
1947 	.ndo_get_devlink_port	= nfp_devlink_get_devlink_port,
1948 };
1949 
1950 static int nfp_udp_tunnel_sync(struct net_device *netdev, unsigned int table)
1951 {
1952 	struct nfp_net *nn = netdev_priv(netdev);
1953 	int i;
1954 
1955 	BUILD_BUG_ON(NFP_NET_N_VXLAN_PORTS & 1);
1956 	for (i = 0; i < NFP_NET_N_VXLAN_PORTS; i += 2) {
1957 		struct udp_tunnel_info ti0, ti1;
1958 
1959 		udp_tunnel_nic_get_port(netdev, table, i, &ti0);
1960 		udp_tunnel_nic_get_port(netdev, table, i + 1, &ti1);
1961 
1962 		nn_writel(nn, NFP_NET_CFG_VXLAN_PORT + i * sizeof(ti0.port),
1963 			  be16_to_cpu(ti1.port) << 16 | be16_to_cpu(ti0.port));
1964 	}
1965 
1966 	return nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_VXLAN);
1967 }
1968 
1969 static const struct udp_tunnel_nic_info nfp_udp_tunnels = {
1970 	.sync_table     = nfp_udp_tunnel_sync,
1971 	.flags          = UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
1972 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
1973 	.tables         = {
1974 		{
1975 			.n_entries      = NFP_NET_N_VXLAN_PORTS,
1976 			.tunnel_types   = UDP_TUNNEL_TYPE_VXLAN,
1977 		},
1978 	},
1979 };
1980 
1981 /**
1982  * nfp_net_info() - Print general info about the NIC
1983  * @nn:      NFP Net device to reconfigure
1984  */
1985 void nfp_net_info(struct nfp_net *nn)
1986 {
1987 	nn_info(nn, "Netronome NFP-6xxx %sNetdev: TxQs=%d/%d RxQs=%d/%d\n",
1988 		nn->dp.is_vf ? "VF " : "",
1989 		nn->dp.num_tx_rings, nn->max_tx_rings,
1990 		nn->dp.num_rx_rings, nn->max_rx_rings);
1991 	nn_info(nn, "VER: %d.%d.%d.%d, Maximum supported MTU: %d\n",
1992 		nn->fw_ver.extend, nn->fw_ver.class,
1993 		nn->fw_ver.major, nn->fw_ver.minor,
1994 		nn->max_mtu);
1995 	nn_info(nn, "CAP: %#x %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1996 		nn->cap,
1997 		nn->cap & NFP_NET_CFG_CTRL_PROMISC  ? "PROMISC "  : "",
1998 		nn->cap & NFP_NET_CFG_CTRL_L2BC     ? "L2BCFILT " : "",
1999 		nn->cap & NFP_NET_CFG_CTRL_L2MC     ? "L2MCFILT " : "",
2000 		nn->cap & NFP_NET_CFG_CTRL_RXCSUM   ? "RXCSUM "   : "",
2001 		nn->cap & NFP_NET_CFG_CTRL_TXCSUM   ? "TXCSUM "   : "",
2002 		nn->cap & NFP_NET_CFG_CTRL_RXVLAN   ? "RXVLAN "   : "",
2003 		nn->cap & NFP_NET_CFG_CTRL_TXVLAN   ? "TXVLAN "   : "",
2004 		nn->cap & NFP_NET_CFG_CTRL_SCATTER  ? "SCATTER "  : "",
2005 		nn->cap & NFP_NET_CFG_CTRL_GATHER   ? "GATHER "   : "",
2006 		nn->cap & NFP_NET_CFG_CTRL_LSO      ? "TSO1 "     : "",
2007 		nn->cap & NFP_NET_CFG_CTRL_LSO2     ? "TSO2 "     : "",
2008 		nn->cap & NFP_NET_CFG_CTRL_RSS      ? "RSS1 "     : "",
2009 		nn->cap & NFP_NET_CFG_CTRL_RSS2     ? "RSS2 "     : "",
2010 		nn->cap & NFP_NET_CFG_CTRL_CTAG_FILTER ? "CTAG_FILTER " : "",
2011 		nn->cap & NFP_NET_CFG_CTRL_MSIXAUTO ? "AUTOMASK " : "",
2012 		nn->cap & NFP_NET_CFG_CTRL_IRQMOD   ? "IRQMOD "   : "",
2013 		nn->cap & NFP_NET_CFG_CTRL_TXRWB    ? "TXRWB "    : "",
2014 		nn->cap & NFP_NET_CFG_CTRL_VXLAN    ? "VXLAN "    : "",
2015 		nn->cap & NFP_NET_CFG_CTRL_NVGRE    ? "NVGRE "	  : "",
2016 		nn->cap & NFP_NET_CFG_CTRL_CSUM_COMPLETE ?
2017 						      "RXCSUM_COMPLETE " : "",
2018 		nn->cap & NFP_NET_CFG_CTRL_LIVE_ADDR ? "LIVE_ADDR " : "",
2019 		nfp_app_extra_cap(nn->app, nn));
2020 }
2021 
2022 /**
2023  * nfp_net_alloc() - Allocate netdev and related structure
2024  * @pdev:         PCI device
2025  * @dev_info:     NFP ASIC params
2026  * @ctrl_bar:     PCI IOMEM with vNIC config memory
2027  * @needs_netdev: Whether to allocate a netdev for this vNIC
2028  * @max_tx_rings: Maximum number of TX rings supported by device
2029  * @max_rx_rings: Maximum number of RX rings supported by device
2030  *
2031  * This function allocates a netdev device and fills in the initial
2032  * part of the @struct nfp_net structure.  In case of control device
2033  * nfp_net structure is allocated without the netdev.
2034  *
2035  * Return: NFP Net device structure, or ERR_PTR on error.
2036  */
2037 struct nfp_net *
2038 nfp_net_alloc(struct pci_dev *pdev, const struct nfp_dev_info *dev_info,
2039 	      void __iomem *ctrl_bar, bool needs_netdev,
2040 	      unsigned int max_tx_rings, unsigned int max_rx_rings)
2041 {
2042 	struct nfp_net *nn;
2043 	int err;
2044 
2045 	if (needs_netdev) {
2046 		struct net_device *netdev;
2047 
2048 		netdev = alloc_etherdev_mqs(sizeof(struct nfp_net),
2049 					    max_tx_rings, max_rx_rings);
2050 		if (!netdev)
2051 			return ERR_PTR(-ENOMEM);
2052 
2053 		SET_NETDEV_DEV(netdev, &pdev->dev);
2054 		nn = netdev_priv(netdev);
2055 		nn->dp.netdev = netdev;
2056 	} else {
2057 		nn = vzalloc(sizeof(*nn));
2058 		if (!nn)
2059 			return ERR_PTR(-ENOMEM);
2060 	}
2061 
2062 	nn->dp.dev = &pdev->dev;
2063 	nn->dp.ctrl_bar = ctrl_bar;
2064 	nn->dev_info = dev_info;
2065 	nn->pdev = pdev;
2066 	nfp_net_get_fw_version(&nn->fw_ver, ctrl_bar);
2067 
2068 	switch (FIELD_GET(NFP_NET_CFG_VERSION_DP_MASK, nn->fw_ver.extend)) {
2069 	case NFP_NET_CFG_VERSION_DP_NFD3:
2070 		nn->dp.ops = &nfp_nfd3_ops;
2071 		break;
2072 	case NFP_NET_CFG_VERSION_DP_NFDK:
2073 		if (nn->fw_ver.major < 5) {
2074 			dev_err(&pdev->dev,
2075 				"NFDK must use ABI 5 or newer, found: %d\n",
2076 				nn->fw_ver.major);
2077 			err = -EINVAL;
2078 			goto err_free_nn;
2079 		}
2080 		nn->dp.ops = &nfp_nfdk_ops;
2081 		break;
2082 	default:
2083 		err = -EINVAL;
2084 		goto err_free_nn;
2085 	}
2086 
2087 	nn->max_tx_rings = max_tx_rings;
2088 	nn->max_rx_rings = max_rx_rings;
2089 
2090 	nn->dp.num_tx_rings = min_t(unsigned int,
2091 				    max_tx_rings, num_online_cpus());
2092 	nn->dp.num_rx_rings = min_t(unsigned int, max_rx_rings,
2093 				 netif_get_num_default_rss_queues());
2094 
2095 	nn->dp.num_r_vecs = max(nn->dp.num_tx_rings, nn->dp.num_rx_rings);
2096 	nn->dp.num_r_vecs = min_t(unsigned int,
2097 				  nn->dp.num_r_vecs, num_online_cpus());
2098 	nn->max_r_vecs = nn->dp.num_r_vecs;
2099 
2100 	nn->dp.xsk_pools = kcalloc(nn->max_r_vecs, sizeof(nn->dp.xsk_pools),
2101 				   GFP_KERNEL);
2102 	if (!nn->dp.xsk_pools) {
2103 		err = -ENOMEM;
2104 		goto err_free_nn;
2105 	}
2106 
2107 	nn->dp.txd_cnt = NFP_NET_TX_DESCS_DEFAULT;
2108 	nn->dp.rxd_cnt = NFP_NET_RX_DESCS_DEFAULT;
2109 
2110 	sema_init(&nn->bar_lock, 1);
2111 
2112 	spin_lock_init(&nn->reconfig_lock);
2113 	spin_lock_init(&nn->link_status_lock);
2114 
2115 	timer_setup(&nn->reconfig_timer, nfp_net_reconfig_timer, 0);
2116 
2117 	err = nfp_net_tlv_caps_parse(&nn->pdev->dev, nn->dp.ctrl_bar,
2118 				     &nn->tlv_caps);
2119 	if (err)
2120 		goto err_free_nn;
2121 
2122 	err = nfp_ccm_mbox_alloc(nn);
2123 	if (err)
2124 		goto err_free_nn;
2125 
2126 	return nn;
2127 
2128 err_free_nn:
2129 	if (nn->dp.netdev)
2130 		free_netdev(nn->dp.netdev);
2131 	else
2132 		vfree(nn);
2133 	return ERR_PTR(err);
2134 }
2135 
2136 /**
2137  * nfp_net_free() - Undo what @nfp_net_alloc() did
2138  * @nn:      NFP Net device to reconfigure
2139  */
2140 void nfp_net_free(struct nfp_net *nn)
2141 {
2142 	WARN_ON(timer_pending(&nn->reconfig_timer) || nn->reconfig_posted);
2143 	nfp_ccm_mbox_free(nn);
2144 
2145 	kfree(nn->dp.xsk_pools);
2146 	if (nn->dp.netdev)
2147 		free_netdev(nn->dp.netdev);
2148 	else
2149 		vfree(nn);
2150 }
2151 
2152 /**
2153  * nfp_net_rss_key_sz() - Get current size of the RSS key
2154  * @nn:		NFP Net device instance
2155  *
2156  * Return: size of the RSS key for currently selected hash function.
2157  */
2158 unsigned int nfp_net_rss_key_sz(struct nfp_net *nn)
2159 {
2160 	switch (nn->rss_hfunc) {
2161 	case ETH_RSS_HASH_TOP:
2162 		return NFP_NET_CFG_RSS_KEY_SZ;
2163 	case ETH_RSS_HASH_XOR:
2164 		return 0;
2165 	case ETH_RSS_HASH_CRC32:
2166 		return 4;
2167 	}
2168 
2169 	nn_warn(nn, "Unknown hash function: %u\n", nn->rss_hfunc);
2170 	return 0;
2171 }
2172 
2173 /**
2174  * nfp_net_rss_init() - Set the initial RSS parameters
2175  * @nn:	     NFP Net device to reconfigure
2176  */
2177 static void nfp_net_rss_init(struct nfp_net *nn)
2178 {
2179 	unsigned long func_bit, rss_cap_hfunc;
2180 	u32 reg;
2181 
2182 	/* Read the RSS function capability and select first supported func */
2183 	reg = nn_readl(nn, NFP_NET_CFG_RSS_CAP);
2184 	rss_cap_hfunc =	FIELD_GET(NFP_NET_CFG_RSS_CAP_HFUNC, reg);
2185 	if (!rss_cap_hfunc)
2186 		rss_cap_hfunc =	FIELD_GET(NFP_NET_CFG_RSS_CAP_HFUNC,
2187 					  NFP_NET_CFG_RSS_TOEPLITZ);
2188 
2189 	func_bit = find_first_bit(&rss_cap_hfunc, NFP_NET_CFG_RSS_HFUNCS);
2190 	if (func_bit == NFP_NET_CFG_RSS_HFUNCS) {
2191 		dev_warn(nn->dp.dev,
2192 			 "Bad RSS config, defaulting to Toeplitz hash\n");
2193 		func_bit = ETH_RSS_HASH_TOP_BIT;
2194 	}
2195 	nn->rss_hfunc = 1 << func_bit;
2196 
2197 	netdev_rss_key_fill(nn->rss_key, nfp_net_rss_key_sz(nn));
2198 
2199 	nfp_net_rss_init_itbl(nn);
2200 
2201 	/* Enable IPv4/IPv6 TCP by default */
2202 	nn->rss_cfg = NFP_NET_CFG_RSS_IPV4_TCP |
2203 		      NFP_NET_CFG_RSS_IPV6_TCP |
2204 		      FIELD_PREP(NFP_NET_CFG_RSS_HFUNC, nn->rss_hfunc) |
2205 		      NFP_NET_CFG_RSS_MASK;
2206 }
2207 
2208 /**
2209  * nfp_net_irqmod_init() - Set the initial IRQ moderation parameters
2210  * @nn:	     NFP Net device to reconfigure
2211  */
2212 static void nfp_net_irqmod_init(struct nfp_net *nn)
2213 {
2214 	nn->rx_coalesce_usecs      = 50;
2215 	nn->rx_coalesce_max_frames = 64;
2216 	nn->tx_coalesce_usecs      = 50;
2217 	nn->tx_coalesce_max_frames = 64;
2218 
2219 	nn->rx_coalesce_adapt_on   = true;
2220 	nn->tx_coalesce_adapt_on   = true;
2221 }
2222 
2223 static void nfp_net_netdev_init(struct nfp_net *nn)
2224 {
2225 	struct net_device *netdev = nn->dp.netdev;
2226 
2227 	nfp_net_write_mac_addr(nn, nn->dp.netdev->dev_addr);
2228 
2229 	netdev->mtu = nn->dp.mtu;
2230 
2231 	/* Advertise/enable offloads based on capabilities
2232 	 *
2233 	 * Note: netdev->features show the currently enabled features
2234 	 * and netdev->hw_features advertises which features are
2235 	 * supported.  By default we enable most features.
2236 	 */
2237 	if (nn->cap & NFP_NET_CFG_CTRL_LIVE_ADDR)
2238 		netdev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2239 
2240 	netdev->hw_features = NETIF_F_HIGHDMA;
2241 	if (nn->cap & NFP_NET_CFG_CTRL_RXCSUM_ANY) {
2242 		netdev->hw_features |= NETIF_F_RXCSUM;
2243 		nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_RXCSUM_ANY;
2244 	}
2245 	if (nn->cap & NFP_NET_CFG_CTRL_TXCSUM) {
2246 		netdev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
2247 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_TXCSUM;
2248 	}
2249 	if (nn->cap & NFP_NET_CFG_CTRL_GATHER) {
2250 		netdev->hw_features |= NETIF_F_SG;
2251 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_GATHER;
2252 	}
2253 	if ((nn->cap & NFP_NET_CFG_CTRL_LSO && nn->fw_ver.major > 2) ||
2254 	    nn->cap & NFP_NET_CFG_CTRL_LSO2) {
2255 		netdev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
2256 		nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_LSO2 ?:
2257 					 NFP_NET_CFG_CTRL_LSO;
2258 	}
2259 	if (nn->cap & NFP_NET_CFG_CTRL_RSS_ANY)
2260 		netdev->hw_features |= NETIF_F_RXHASH;
2261 	if (nn->cap & NFP_NET_CFG_CTRL_VXLAN) {
2262 		if (nn->cap & NFP_NET_CFG_CTRL_LSO)
2263 			netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL;
2264 		netdev->udp_tunnel_nic_info = &nfp_udp_tunnels;
2265 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_VXLAN;
2266 	}
2267 	if (nn->cap & NFP_NET_CFG_CTRL_NVGRE) {
2268 		if (nn->cap & NFP_NET_CFG_CTRL_LSO)
2269 			netdev->hw_features |= NETIF_F_GSO_GRE;
2270 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_NVGRE;
2271 	}
2272 	if (nn->cap & (NFP_NET_CFG_CTRL_VXLAN | NFP_NET_CFG_CTRL_NVGRE))
2273 		netdev->hw_enc_features = netdev->hw_features;
2274 
2275 	netdev->vlan_features = netdev->hw_features;
2276 
2277 	if (nn->cap & NFP_NET_CFG_CTRL_RXVLAN) {
2278 		netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
2279 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_RXVLAN;
2280 	}
2281 	if (nn->cap & NFP_NET_CFG_CTRL_TXVLAN) {
2282 		if (nn->cap & NFP_NET_CFG_CTRL_LSO2) {
2283 			nn_warn(nn, "Device advertises both TSO2 and TXVLAN. Refusing to enable TXVLAN.\n");
2284 		} else {
2285 			netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
2286 			nn->dp.ctrl |= NFP_NET_CFG_CTRL_TXVLAN;
2287 		}
2288 	}
2289 	if (nn->cap & NFP_NET_CFG_CTRL_CTAG_FILTER) {
2290 		netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
2291 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_CTAG_FILTER;
2292 	}
2293 
2294 	netdev->features = netdev->hw_features;
2295 
2296 	if (nfp_app_has_tc(nn->app) && nn->port)
2297 		netdev->hw_features |= NETIF_F_HW_TC;
2298 
2299 	/* Advertise but disable TSO by default. */
2300 	netdev->features &= ~(NETIF_F_TSO | NETIF_F_TSO6);
2301 	nn->dp.ctrl &= ~NFP_NET_CFG_CTRL_LSO_ANY;
2302 
2303 	/* Finalise the netdev setup */
2304 	switch (nn->dp.ops->version) {
2305 	case NFP_NFD_VER_NFD3:
2306 		netdev->netdev_ops = &nfp_nfd3_netdev_ops;
2307 		break;
2308 	case NFP_NFD_VER_NFDK:
2309 		netdev->netdev_ops = &nfp_nfdk_netdev_ops;
2310 		break;
2311 	}
2312 
2313 	netdev->watchdog_timeo = msecs_to_jiffies(5 * 1000);
2314 
2315 	/* MTU range: 68 - hw-specific max */
2316 	netdev->min_mtu = ETH_MIN_MTU;
2317 	netdev->max_mtu = nn->max_mtu;
2318 
2319 	netif_set_gso_max_segs(netdev, NFP_NET_LSO_MAX_SEGS);
2320 
2321 	netif_carrier_off(netdev);
2322 
2323 	nfp_net_set_ethtool_ops(netdev);
2324 }
2325 
2326 static int nfp_net_read_caps(struct nfp_net *nn)
2327 {
2328 	/* Get some of the read-only fields from the BAR */
2329 	nn->cap = nn_readl(nn, NFP_NET_CFG_CAP);
2330 	nn->max_mtu = nn_readl(nn, NFP_NET_CFG_MAX_MTU);
2331 
2332 	/* ABI 4.x and ctrl vNIC always use chained metadata, in other cases
2333 	 * we allow use of non-chained metadata if RSS(v1) is the only
2334 	 * advertised capability requiring metadata.
2335 	 */
2336 	nn->dp.chained_metadata_format = nn->fw_ver.major == 4 ||
2337 					 !nn->dp.netdev ||
2338 					 !(nn->cap & NFP_NET_CFG_CTRL_RSS) ||
2339 					 nn->cap & NFP_NET_CFG_CTRL_CHAIN_META;
2340 	/* RSS(v1) uses non-chained metadata format, except in ABI 4.x where
2341 	 * it has the same meaning as RSSv2.
2342 	 */
2343 	if (nn->dp.chained_metadata_format && nn->fw_ver.major != 4)
2344 		nn->cap &= ~NFP_NET_CFG_CTRL_RSS;
2345 
2346 	/* Determine RX packet/metadata boundary offset */
2347 	if (nn->fw_ver.major >= 2) {
2348 		u32 reg;
2349 
2350 		reg = nn_readl(nn, NFP_NET_CFG_RX_OFFSET);
2351 		if (reg > NFP_NET_MAX_PREPEND) {
2352 			nn_err(nn, "Invalid rx offset: %d\n", reg);
2353 			return -EINVAL;
2354 		}
2355 		nn->dp.rx_offset = reg;
2356 	} else {
2357 		nn->dp.rx_offset = NFP_NET_RX_OFFSET;
2358 	}
2359 
2360 	/* Mask out NFD-version-specific features */
2361 	nn->cap &= nn->dp.ops->cap_mask;
2362 
2363 	/* For control vNICs mask out the capabilities app doesn't want. */
2364 	if (!nn->dp.netdev)
2365 		nn->cap &= nn->app->type->ctrl_cap_mask;
2366 
2367 	return 0;
2368 }
2369 
2370 /**
2371  * nfp_net_init() - Initialise/finalise the nfp_net structure
2372  * @nn:		NFP Net device structure
2373  *
2374  * Return: 0 on success or negative errno on error.
2375  */
2376 int nfp_net_init(struct nfp_net *nn)
2377 {
2378 	int err;
2379 
2380 	nn->dp.rx_dma_dir = DMA_FROM_DEVICE;
2381 
2382 	err = nfp_net_read_caps(nn);
2383 	if (err)
2384 		return err;
2385 
2386 	/* Set default MTU and Freelist buffer size */
2387 	if (!nfp_net_is_data_vnic(nn) && nn->app->ctrl_mtu) {
2388 		nn->dp.mtu = min(nn->app->ctrl_mtu, nn->max_mtu);
2389 	} else if (nn->max_mtu < NFP_NET_DEFAULT_MTU) {
2390 		nn->dp.mtu = nn->max_mtu;
2391 	} else {
2392 		nn->dp.mtu = NFP_NET_DEFAULT_MTU;
2393 	}
2394 	nn->dp.fl_bufsz = nfp_net_calc_fl_bufsz(&nn->dp);
2395 
2396 	if (nfp_app_ctrl_uses_data_vnics(nn->app))
2397 		nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_CMSG_DATA;
2398 
2399 	if (nn->cap & NFP_NET_CFG_CTRL_RSS_ANY) {
2400 		nfp_net_rss_init(nn);
2401 		nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_RSS2 ?:
2402 					 NFP_NET_CFG_CTRL_RSS;
2403 	}
2404 
2405 	/* Allow L2 Broadcast and Multicast through by default, if supported */
2406 	if (nn->cap & NFP_NET_CFG_CTRL_L2BC)
2407 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_L2BC;
2408 
2409 	/* Allow IRQ moderation, if supported */
2410 	if (nn->cap & NFP_NET_CFG_CTRL_IRQMOD) {
2411 		nfp_net_irqmod_init(nn);
2412 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_IRQMOD;
2413 	}
2414 
2415 	/* Enable TX pointer writeback, if supported */
2416 	if (nn->cap & NFP_NET_CFG_CTRL_TXRWB)
2417 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_TXRWB;
2418 
2419 	/* Stash the re-configuration queue away.  First odd queue in TX Bar */
2420 	nn->qcp_cfg = nn->tx_bar + NFP_QCP_QUEUE_ADDR_SZ;
2421 
2422 	/* Make sure the FW knows the netdev is supposed to be disabled here */
2423 	nn_writel(nn, NFP_NET_CFG_CTRL, 0);
2424 	nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, 0);
2425 	nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, 0);
2426 	err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_RING |
2427 				   NFP_NET_CFG_UPDATE_GEN);
2428 	if (err)
2429 		return err;
2430 
2431 	if (nn->dp.netdev) {
2432 		nfp_net_netdev_init(nn);
2433 
2434 		err = nfp_ccm_mbox_init(nn);
2435 		if (err)
2436 			return err;
2437 
2438 		err = nfp_net_tls_init(nn);
2439 		if (err)
2440 			goto err_clean_mbox;
2441 	}
2442 
2443 	nfp_net_vecs_init(nn);
2444 
2445 	if (!nn->dp.netdev)
2446 		return 0;
2447 	return register_netdev(nn->dp.netdev);
2448 
2449 err_clean_mbox:
2450 	nfp_ccm_mbox_clean(nn);
2451 	return err;
2452 }
2453 
2454 /**
2455  * nfp_net_clean() - Undo what nfp_net_init() did.
2456  * @nn:		NFP Net device structure
2457  */
2458 void nfp_net_clean(struct nfp_net *nn)
2459 {
2460 	if (!nn->dp.netdev)
2461 		return;
2462 
2463 	unregister_netdev(nn->dp.netdev);
2464 	nfp_ccm_mbox_clean(nn);
2465 	nfp_net_reconfig_wait_posted(nn);
2466 }
2467