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