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 int nfp_net_sched_mbox_amsg_work(struct nfp_net *nn, u32 cmd, const void *data, size_t len,
1338 				 int (*cb)(struct nfp_net *, struct nfp_mbox_amsg_entry *))
1339 {
1340 	struct nfp_mbox_amsg_entry *entry;
1341 
1342 	entry = kmalloc(sizeof(*entry) + len, GFP_ATOMIC);
1343 	if (!entry)
1344 		return -ENOMEM;
1345 
1346 	memcpy(entry->msg, data, len);
1347 	entry->cmd = cmd;
1348 	entry->cfg = cb;
1349 
1350 	spin_lock_bh(&nn->mbox_amsg.lock);
1351 	list_add_tail(&entry->list, &nn->mbox_amsg.list);
1352 	spin_unlock_bh(&nn->mbox_amsg.lock);
1353 
1354 	schedule_work(&nn->mbox_amsg.work);
1355 
1356 	return 0;
1357 }
1358 
1359 static void nfp_net_mbox_amsg_work(struct work_struct *work)
1360 {
1361 	struct nfp_net *nn = container_of(work, struct nfp_net, mbox_amsg.work);
1362 	struct nfp_mbox_amsg_entry *entry, *tmp;
1363 	struct list_head tmp_list;
1364 
1365 	INIT_LIST_HEAD(&tmp_list);
1366 
1367 	spin_lock_bh(&nn->mbox_amsg.lock);
1368 	list_splice_init(&nn->mbox_amsg.list, &tmp_list);
1369 	spin_unlock_bh(&nn->mbox_amsg.lock);
1370 
1371 	list_for_each_entry_safe(entry, tmp, &tmp_list, list) {
1372 		int err = entry->cfg(nn, entry);
1373 
1374 		if (err)
1375 			nn_err(nn, "Config cmd %d to HW failed %d.\n", entry->cmd, err);
1376 
1377 		list_del(&entry->list);
1378 		kfree(entry);
1379 	}
1380 }
1381 
1382 static int nfp_net_mc_cfg(struct nfp_net *nn, struct nfp_mbox_amsg_entry *entry)
1383 {
1384 	unsigned char *addr = entry->msg;
1385 	int ret;
1386 
1387 	ret = nfp_net_mbox_lock(nn, NFP_NET_CFG_MULTICAST_SZ);
1388 	if (ret)
1389 		return ret;
1390 
1391 	nn_writel(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_MULTICAST_MAC_HI,
1392 		  get_unaligned_be32(addr));
1393 	nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_MULTICAST_MAC_LO,
1394 		  get_unaligned_be16(addr + 4));
1395 
1396 	return nfp_net_mbox_reconfig_and_unlock(nn, entry->cmd);
1397 }
1398 
1399 static int nfp_net_mc_sync(struct net_device *netdev, const unsigned char *addr)
1400 {
1401 	struct nfp_net *nn = netdev_priv(netdev);
1402 
1403 	if (netdev_mc_count(netdev) > NFP_NET_CFG_MAC_MC_MAX) {
1404 		nn_err(nn, "Requested number of MC addresses (%d) exceeds maximum (%d).\n",
1405 		       netdev_mc_count(netdev), NFP_NET_CFG_MAC_MC_MAX);
1406 		return -EINVAL;
1407 	}
1408 
1409 	return nfp_net_sched_mbox_amsg_work(nn, NFP_NET_CFG_MBOX_CMD_MULTICAST_ADD, addr,
1410 					    NFP_NET_CFG_MULTICAST_SZ, nfp_net_mc_cfg);
1411 }
1412 
1413 static int nfp_net_mc_unsync(struct net_device *netdev, const unsigned char *addr)
1414 {
1415 	struct nfp_net *nn = netdev_priv(netdev);
1416 
1417 	return nfp_net_sched_mbox_amsg_work(nn, NFP_NET_CFG_MBOX_CMD_MULTICAST_DEL, addr,
1418 					    NFP_NET_CFG_MULTICAST_SZ, nfp_net_mc_cfg);
1419 }
1420 
1421 static void nfp_net_set_rx_mode(struct net_device *netdev)
1422 {
1423 	struct nfp_net *nn = netdev_priv(netdev);
1424 	u32 new_ctrl, new_ctrl_w1;
1425 
1426 	new_ctrl = nn->dp.ctrl;
1427 	new_ctrl_w1 = nn->dp.ctrl_w1;
1428 
1429 	if (!netdev_mc_empty(netdev) || netdev->flags & IFF_ALLMULTI)
1430 		new_ctrl |= nn->cap & NFP_NET_CFG_CTRL_L2MC;
1431 	else
1432 		new_ctrl &= ~NFP_NET_CFG_CTRL_L2MC;
1433 
1434 	if (netdev->flags & IFF_ALLMULTI)
1435 		new_ctrl_w1 &= ~NFP_NET_CFG_CTRL_MCAST_FILTER;
1436 	else
1437 		new_ctrl_w1 |= nn->cap_w1 & NFP_NET_CFG_CTRL_MCAST_FILTER;
1438 
1439 	if (netdev->flags & IFF_PROMISC) {
1440 		if (nn->cap & NFP_NET_CFG_CTRL_PROMISC)
1441 			new_ctrl |= NFP_NET_CFG_CTRL_PROMISC;
1442 		else
1443 			nn_warn(nn, "FW does not support promiscuous mode\n");
1444 	} else {
1445 		new_ctrl &= ~NFP_NET_CFG_CTRL_PROMISC;
1446 	}
1447 
1448 	if ((nn->cap_w1 & NFP_NET_CFG_CTRL_MCAST_FILTER) &&
1449 	    __dev_mc_sync(netdev, nfp_net_mc_sync, nfp_net_mc_unsync))
1450 		netdev_err(netdev, "Sync mc address failed\n");
1451 
1452 	if (new_ctrl == nn->dp.ctrl && new_ctrl_w1 == nn->dp.ctrl_w1)
1453 		return;
1454 
1455 	if (new_ctrl != nn->dp.ctrl)
1456 		nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
1457 	if (new_ctrl_w1 != nn->dp.ctrl_w1)
1458 		nn_writel(nn, NFP_NET_CFG_CTRL_WORD1, new_ctrl_w1);
1459 	nfp_net_reconfig_post(nn, NFP_NET_CFG_UPDATE_GEN);
1460 
1461 	nn->dp.ctrl = new_ctrl;
1462 	nn->dp.ctrl_w1 = new_ctrl_w1;
1463 }
1464 
1465 static void nfp_net_rss_init_itbl(struct nfp_net *nn)
1466 {
1467 	int i;
1468 
1469 	for (i = 0; i < sizeof(nn->rss_itbl); i++)
1470 		nn->rss_itbl[i] =
1471 			ethtool_rxfh_indir_default(i, nn->dp.num_rx_rings);
1472 }
1473 
1474 static void nfp_net_dp_swap(struct nfp_net *nn, struct nfp_net_dp *dp)
1475 {
1476 	struct nfp_net_dp new_dp = *dp;
1477 
1478 	*dp = nn->dp;
1479 	nn->dp = new_dp;
1480 
1481 	nn->dp.netdev->mtu = new_dp.mtu;
1482 
1483 	if (!netif_is_rxfh_configured(nn->dp.netdev))
1484 		nfp_net_rss_init_itbl(nn);
1485 }
1486 
1487 static int nfp_net_dp_swap_enable(struct nfp_net *nn, struct nfp_net_dp *dp)
1488 {
1489 	unsigned int r;
1490 	int err;
1491 
1492 	nfp_net_dp_swap(nn, dp);
1493 
1494 	for (r = 0; r <	nn->max_r_vecs; r++)
1495 		nfp_net_vector_assign_rings(&nn->dp, &nn->r_vecs[r], r);
1496 
1497 	err = netif_set_real_num_queues(nn->dp.netdev,
1498 					nn->dp.num_stack_tx_rings,
1499 					nn->dp.num_rx_rings);
1500 	if (err)
1501 		return err;
1502 
1503 	return nfp_net_set_config_and_enable(nn);
1504 }
1505 
1506 struct nfp_net_dp *nfp_net_clone_dp(struct nfp_net *nn)
1507 {
1508 	struct nfp_net_dp *new;
1509 
1510 	new = kmalloc(sizeof(*new), GFP_KERNEL);
1511 	if (!new)
1512 		return NULL;
1513 
1514 	*new = nn->dp;
1515 
1516 	new->xsk_pools = kmemdup(new->xsk_pools,
1517 				 array_size(nn->max_r_vecs,
1518 					    sizeof(new->xsk_pools)),
1519 				 GFP_KERNEL);
1520 	if (!new->xsk_pools) {
1521 		kfree(new);
1522 		return NULL;
1523 	}
1524 
1525 	/* Clear things which need to be recomputed */
1526 	new->fl_bufsz = 0;
1527 	new->tx_rings = NULL;
1528 	new->rx_rings = NULL;
1529 	new->num_r_vecs = 0;
1530 	new->num_stack_tx_rings = 0;
1531 	new->txrwb = NULL;
1532 	new->txrwb_dma = 0;
1533 
1534 	return new;
1535 }
1536 
1537 static void nfp_net_free_dp(struct nfp_net_dp *dp)
1538 {
1539 	kfree(dp->xsk_pools);
1540 	kfree(dp);
1541 }
1542 
1543 static int
1544 nfp_net_check_config(struct nfp_net *nn, struct nfp_net_dp *dp,
1545 		     struct netlink_ext_ack *extack)
1546 {
1547 	unsigned int r, xsk_min_fl_bufsz;
1548 
1549 	/* XDP-enabled tests */
1550 	if (!dp->xdp_prog)
1551 		return 0;
1552 	if (dp->fl_bufsz > PAGE_SIZE) {
1553 		NL_SET_ERR_MSG_MOD(extack, "MTU too large w/ XDP enabled");
1554 		return -EINVAL;
1555 	}
1556 	if (dp->num_tx_rings > nn->max_tx_rings) {
1557 		NL_SET_ERR_MSG_MOD(extack, "Insufficient number of TX rings w/ XDP enabled");
1558 		return -EINVAL;
1559 	}
1560 
1561 	xsk_min_fl_bufsz = nfp_net_calc_fl_bufsz_xsk(dp);
1562 	for (r = 0; r < nn->max_r_vecs; r++) {
1563 		if (!dp->xsk_pools[r])
1564 			continue;
1565 
1566 		if (xsk_pool_get_rx_frame_size(dp->xsk_pools[r]) < xsk_min_fl_bufsz) {
1567 			NL_SET_ERR_MSG_MOD(extack,
1568 					   "XSK buffer pool chunk size too small");
1569 			return -EINVAL;
1570 		}
1571 	}
1572 
1573 	return 0;
1574 }
1575 
1576 int nfp_net_ring_reconfig(struct nfp_net *nn, struct nfp_net_dp *dp,
1577 			  struct netlink_ext_ack *extack)
1578 {
1579 	int r, err;
1580 
1581 	dp->fl_bufsz = nfp_net_calc_fl_bufsz(dp);
1582 
1583 	dp->num_stack_tx_rings = dp->num_tx_rings;
1584 	if (dp->xdp_prog)
1585 		dp->num_stack_tx_rings -= dp->num_rx_rings;
1586 
1587 	dp->num_r_vecs = max(dp->num_rx_rings, dp->num_stack_tx_rings);
1588 
1589 	err = nfp_net_check_config(nn, dp, extack);
1590 	if (err)
1591 		goto exit_free_dp;
1592 
1593 	if (!netif_running(dp->netdev)) {
1594 		nfp_net_dp_swap(nn, dp);
1595 		err = 0;
1596 		goto exit_free_dp;
1597 	}
1598 
1599 	/* Prepare new rings */
1600 	for (r = nn->dp.num_r_vecs; r < dp->num_r_vecs; r++) {
1601 		err = nfp_net_prepare_vector(nn, &nn->r_vecs[r], r);
1602 		if (err) {
1603 			dp->num_r_vecs = r;
1604 			goto err_cleanup_vecs;
1605 		}
1606 	}
1607 
1608 	err = nfp_net_rx_rings_prepare(nn, dp);
1609 	if (err)
1610 		goto err_cleanup_vecs;
1611 
1612 	err = nfp_net_tx_rings_prepare(nn, dp);
1613 	if (err)
1614 		goto err_free_rx;
1615 
1616 	/* Stop device, swap in new rings, try to start the firmware */
1617 	nfp_net_close_stack(nn);
1618 	nfp_net_clear_config_and_disable(nn);
1619 
1620 	err = nfp_net_dp_swap_enable(nn, dp);
1621 	if (err) {
1622 		int err2;
1623 
1624 		nfp_net_clear_config_and_disable(nn);
1625 
1626 		/* Try with old configuration and old rings */
1627 		err2 = nfp_net_dp_swap_enable(nn, dp);
1628 		if (err2)
1629 			nn_err(nn, "Can't restore ring config - FW communication failed (%d,%d)\n",
1630 			       err, err2);
1631 	}
1632 	for (r = dp->num_r_vecs - 1; r >= nn->dp.num_r_vecs; r--)
1633 		nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
1634 
1635 	nfp_net_rx_rings_free(dp);
1636 	nfp_net_tx_rings_free(dp);
1637 
1638 	nfp_net_open_stack(nn);
1639 exit_free_dp:
1640 	nfp_net_free_dp(dp);
1641 
1642 	return err;
1643 
1644 err_free_rx:
1645 	nfp_net_rx_rings_free(dp);
1646 err_cleanup_vecs:
1647 	for (r = dp->num_r_vecs - 1; r >= nn->dp.num_r_vecs; r--)
1648 		nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
1649 	nfp_net_free_dp(dp);
1650 	return err;
1651 }
1652 
1653 static int nfp_net_change_mtu(struct net_device *netdev, int new_mtu)
1654 {
1655 	struct nfp_net *nn = netdev_priv(netdev);
1656 	struct nfp_net_dp *dp;
1657 	int err;
1658 
1659 	err = nfp_app_check_mtu(nn->app, netdev, new_mtu);
1660 	if (err)
1661 		return err;
1662 
1663 	dp = nfp_net_clone_dp(nn);
1664 	if (!dp)
1665 		return -ENOMEM;
1666 
1667 	dp->mtu = new_mtu;
1668 
1669 	return nfp_net_ring_reconfig(nn, dp, NULL);
1670 }
1671 
1672 static int
1673 nfp_net_vlan_rx_add_vid(struct net_device *netdev, __be16 proto, u16 vid)
1674 {
1675 	const u32 cmd = NFP_NET_CFG_MBOX_CMD_CTAG_FILTER_ADD;
1676 	struct nfp_net *nn = netdev_priv(netdev);
1677 	int err;
1678 
1679 	/* Priority tagged packets with vlan id 0 are processed by the
1680 	 * NFP as untagged packets
1681 	 */
1682 	if (!vid)
1683 		return 0;
1684 
1685 	err = nfp_net_mbox_lock(nn, NFP_NET_CFG_VLAN_FILTER_SZ);
1686 	if (err)
1687 		return err;
1688 
1689 	nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_VLAN_FILTER_VID, vid);
1690 	nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_VLAN_FILTER_PROTO,
1691 		  ETH_P_8021Q);
1692 
1693 	return nfp_net_mbox_reconfig_and_unlock(nn, cmd);
1694 }
1695 
1696 static int
1697 nfp_net_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto, u16 vid)
1698 {
1699 	const u32 cmd = NFP_NET_CFG_MBOX_CMD_CTAG_FILTER_KILL;
1700 	struct nfp_net *nn = netdev_priv(netdev);
1701 	int err;
1702 
1703 	/* Priority tagged packets with vlan id 0 are processed by the
1704 	 * NFP as untagged packets
1705 	 */
1706 	if (!vid)
1707 		return 0;
1708 
1709 	err = nfp_net_mbox_lock(nn, NFP_NET_CFG_VLAN_FILTER_SZ);
1710 	if (err)
1711 		return err;
1712 
1713 	nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_VLAN_FILTER_VID, vid);
1714 	nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_VLAN_FILTER_PROTO,
1715 		  ETH_P_8021Q);
1716 
1717 	return nfp_net_mbox_reconfig_and_unlock(nn, cmd);
1718 }
1719 
1720 static void nfp_net_stat64(struct net_device *netdev,
1721 			   struct rtnl_link_stats64 *stats)
1722 {
1723 	struct nfp_net *nn = netdev_priv(netdev);
1724 	int r;
1725 
1726 	/* Collect software stats */
1727 	for (r = 0; r < nn->max_r_vecs; r++) {
1728 		struct nfp_net_r_vector *r_vec = &nn->r_vecs[r];
1729 		u64 data[3];
1730 		unsigned int start;
1731 
1732 		do {
1733 			start = u64_stats_fetch_begin(&r_vec->rx_sync);
1734 			data[0] = r_vec->rx_pkts;
1735 			data[1] = r_vec->rx_bytes;
1736 			data[2] = r_vec->rx_drops;
1737 		} while (u64_stats_fetch_retry(&r_vec->rx_sync, start));
1738 		stats->rx_packets += data[0];
1739 		stats->rx_bytes += data[1];
1740 		stats->rx_dropped += data[2];
1741 
1742 		do {
1743 			start = u64_stats_fetch_begin(&r_vec->tx_sync);
1744 			data[0] = r_vec->tx_pkts;
1745 			data[1] = r_vec->tx_bytes;
1746 			data[2] = r_vec->tx_errors;
1747 		} while (u64_stats_fetch_retry(&r_vec->tx_sync, start));
1748 		stats->tx_packets += data[0];
1749 		stats->tx_bytes += data[1];
1750 		stats->tx_errors += data[2];
1751 	}
1752 
1753 	/* Add in device stats */
1754 	stats->multicast += nn_readq(nn, NFP_NET_CFG_STATS_RX_MC_FRAMES);
1755 	stats->rx_dropped += nn_readq(nn, NFP_NET_CFG_STATS_RX_DISCARDS);
1756 	stats->rx_errors += nn_readq(nn, NFP_NET_CFG_STATS_RX_ERRORS);
1757 
1758 	stats->tx_dropped += nn_readq(nn, NFP_NET_CFG_STATS_TX_DISCARDS);
1759 	stats->tx_errors += nn_readq(nn, NFP_NET_CFG_STATS_TX_ERRORS);
1760 }
1761 
1762 static int nfp_net_set_features(struct net_device *netdev,
1763 				netdev_features_t features)
1764 {
1765 	netdev_features_t changed = netdev->features ^ features;
1766 	struct nfp_net *nn = netdev_priv(netdev);
1767 	u32 new_ctrl;
1768 	int err;
1769 
1770 	/* Assume this is not called with features we have not advertised */
1771 
1772 	new_ctrl = nn->dp.ctrl;
1773 
1774 	if (changed & NETIF_F_RXCSUM) {
1775 		if (features & NETIF_F_RXCSUM)
1776 			new_ctrl |= nn->cap & NFP_NET_CFG_CTRL_RXCSUM_ANY;
1777 		else
1778 			new_ctrl &= ~NFP_NET_CFG_CTRL_RXCSUM_ANY;
1779 	}
1780 
1781 	if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
1782 		if (features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))
1783 			new_ctrl |= NFP_NET_CFG_CTRL_TXCSUM;
1784 		else
1785 			new_ctrl &= ~NFP_NET_CFG_CTRL_TXCSUM;
1786 	}
1787 
1788 	if (changed & (NETIF_F_TSO | NETIF_F_TSO6)) {
1789 		if (features & (NETIF_F_TSO | NETIF_F_TSO6))
1790 			new_ctrl |= nn->cap & NFP_NET_CFG_CTRL_LSO2 ?:
1791 					      NFP_NET_CFG_CTRL_LSO;
1792 		else
1793 			new_ctrl &= ~NFP_NET_CFG_CTRL_LSO_ANY;
1794 	}
1795 
1796 	if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
1797 		if (features & NETIF_F_HW_VLAN_CTAG_RX)
1798 			new_ctrl |= nn->cap & NFP_NET_CFG_CTRL_RXVLAN_V2 ?:
1799 				    NFP_NET_CFG_CTRL_RXVLAN;
1800 		else
1801 			new_ctrl &= ~NFP_NET_CFG_CTRL_RXVLAN_ANY;
1802 	}
1803 
1804 	if (changed & NETIF_F_HW_VLAN_CTAG_TX) {
1805 		if (features & NETIF_F_HW_VLAN_CTAG_TX)
1806 			new_ctrl |= nn->cap & NFP_NET_CFG_CTRL_TXVLAN_V2 ?:
1807 				    NFP_NET_CFG_CTRL_TXVLAN;
1808 		else
1809 			new_ctrl &= ~NFP_NET_CFG_CTRL_TXVLAN_ANY;
1810 	}
1811 
1812 	if (changed & NETIF_F_HW_VLAN_CTAG_FILTER) {
1813 		if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
1814 			new_ctrl |= NFP_NET_CFG_CTRL_CTAG_FILTER;
1815 		else
1816 			new_ctrl &= ~NFP_NET_CFG_CTRL_CTAG_FILTER;
1817 	}
1818 
1819 	if (changed & NETIF_F_HW_VLAN_STAG_RX) {
1820 		if (features & NETIF_F_HW_VLAN_STAG_RX)
1821 			new_ctrl |= NFP_NET_CFG_CTRL_RXQINQ;
1822 		else
1823 			new_ctrl &= ~NFP_NET_CFG_CTRL_RXQINQ;
1824 	}
1825 
1826 	if (changed & NETIF_F_SG) {
1827 		if (features & NETIF_F_SG)
1828 			new_ctrl |= NFP_NET_CFG_CTRL_GATHER;
1829 		else
1830 			new_ctrl &= ~NFP_NET_CFG_CTRL_GATHER;
1831 	}
1832 
1833 	err = nfp_port_set_features(netdev, features);
1834 	if (err)
1835 		return err;
1836 
1837 	nn_dbg(nn, "Feature change 0x%llx -> 0x%llx (changed=0x%llx)\n",
1838 	       netdev->features, features, changed);
1839 
1840 	if (new_ctrl == nn->dp.ctrl)
1841 		return 0;
1842 
1843 	nn_dbg(nn, "NIC ctrl: 0x%x -> 0x%x\n", nn->dp.ctrl, new_ctrl);
1844 	nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
1845 	err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_GEN);
1846 	if (err)
1847 		return err;
1848 
1849 	nn->dp.ctrl = new_ctrl;
1850 
1851 	return 0;
1852 }
1853 
1854 static netdev_features_t
1855 nfp_net_fix_features(struct net_device *netdev,
1856 		     netdev_features_t features)
1857 {
1858 	if ((features & NETIF_F_HW_VLAN_CTAG_RX) &&
1859 	    (features & NETIF_F_HW_VLAN_STAG_RX)) {
1860 		if (netdev->features & NETIF_F_HW_VLAN_CTAG_RX) {
1861 			features &= ~NETIF_F_HW_VLAN_CTAG_RX;
1862 			netdev->wanted_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
1863 			netdev_warn(netdev,
1864 				    "S-tag and C-tag stripping can't be enabled at the same time. Enabling S-tag stripping and disabling C-tag stripping\n");
1865 		} else if (netdev->features & NETIF_F_HW_VLAN_STAG_RX) {
1866 			features &= ~NETIF_F_HW_VLAN_STAG_RX;
1867 			netdev->wanted_features &= ~NETIF_F_HW_VLAN_STAG_RX;
1868 			netdev_warn(netdev,
1869 				    "S-tag and C-tag stripping can't be enabled at the same time. Enabling C-tag stripping and disabling S-tag stripping\n");
1870 		}
1871 	}
1872 	return features;
1873 }
1874 
1875 static netdev_features_t
1876 nfp_net_features_check(struct sk_buff *skb, struct net_device *dev,
1877 		       netdev_features_t features)
1878 {
1879 	u8 l4_hdr;
1880 
1881 	/* We can't do TSO over double tagged packets (802.1AD) */
1882 	features &= vlan_features_check(skb, features);
1883 
1884 	if (!skb->encapsulation)
1885 		return features;
1886 
1887 	/* Ensure that inner L4 header offset fits into TX descriptor field */
1888 	if (skb_is_gso(skb)) {
1889 		u32 hdrlen;
1890 
1891 		hdrlen = skb_inner_tcp_all_headers(skb);
1892 
1893 		/* Assume worst case scenario of having longest possible
1894 		 * metadata prepend - 8B
1895 		 */
1896 		if (unlikely(hdrlen > NFP_NET_LSO_MAX_HDR_SZ - 8))
1897 			features &= ~NETIF_F_GSO_MASK;
1898 	}
1899 
1900 	/* VXLAN/GRE check */
1901 	switch (vlan_get_protocol(skb)) {
1902 	case htons(ETH_P_IP):
1903 		l4_hdr = ip_hdr(skb)->protocol;
1904 		break;
1905 	case htons(ETH_P_IPV6):
1906 		l4_hdr = ipv6_hdr(skb)->nexthdr;
1907 		break;
1908 	default:
1909 		return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
1910 	}
1911 
1912 	if (skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
1913 	    skb->inner_protocol != htons(ETH_P_TEB) ||
1914 	    (l4_hdr != IPPROTO_UDP && l4_hdr != IPPROTO_GRE) ||
1915 	    (l4_hdr == IPPROTO_UDP &&
1916 	     (skb_inner_mac_header(skb) - skb_transport_header(skb) !=
1917 	      sizeof(struct udphdr) + sizeof(struct vxlanhdr))))
1918 		return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
1919 
1920 	return features;
1921 }
1922 
1923 static int
1924 nfp_net_get_phys_port_name(struct net_device *netdev, char *name, size_t len)
1925 {
1926 	struct nfp_net *nn = netdev_priv(netdev);
1927 	int n;
1928 
1929 	/* If port is defined, devlink_port is registered and devlink core
1930 	 * is taking care of name formatting.
1931 	 */
1932 	if (nn->port)
1933 		return -EOPNOTSUPP;
1934 
1935 	if (nn->dp.is_vf || nn->vnic_no_name)
1936 		return -EOPNOTSUPP;
1937 
1938 	n = snprintf(name, len, "n%d", nn->id);
1939 	if (n >= len)
1940 		return -EINVAL;
1941 
1942 	return 0;
1943 }
1944 
1945 static int nfp_net_xdp_setup_drv(struct nfp_net *nn, struct netdev_bpf *bpf)
1946 {
1947 	struct bpf_prog *prog = bpf->prog;
1948 	struct nfp_net_dp *dp;
1949 	int err;
1950 
1951 	if (!prog == !nn->dp.xdp_prog) {
1952 		WRITE_ONCE(nn->dp.xdp_prog, prog);
1953 		xdp_attachment_setup(&nn->xdp, bpf);
1954 		return 0;
1955 	}
1956 
1957 	dp = nfp_net_clone_dp(nn);
1958 	if (!dp)
1959 		return -ENOMEM;
1960 
1961 	dp->xdp_prog = prog;
1962 	dp->num_tx_rings += prog ? nn->dp.num_rx_rings : -nn->dp.num_rx_rings;
1963 	dp->rx_dma_dir = prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
1964 	dp->rx_dma_off = prog ? XDP_PACKET_HEADROOM - nn->dp.rx_offset : 0;
1965 
1966 	/* We need RX reconfig to remap the buffers (BIDIR vs FROM_DEV) */
1967 	err = nfp_net_ring_reconfig(nn, dp, bpf->extack);
1968 	if (err)
1969 		return err;
1970 
1971 	xdp_attachment_setup(&nn->xdp, bpf);
1972 	return 0;
1973 }
1974 
1975 static int nfp_net_xdp_setup_hw(struct nfp_net *nn, struct netdev_bpf *bpf)
1976 {
1977 	int err;
1978 
1979 	err = nfp_app_xdp_offload(nn->app, nn, bpf->prog, bpf->extack);
1980 	if (err)
1981 		return err;
1982 
1983 	xdp_attachment_setup(&nn->xdp_hw, bpf);
1984 	return 0;
1985 }
1986 
1987 static int nfp_net_xdp(struct net_device *netdev, struct netdev_bpf *xdp)
1988 {
1989 	struct nfp_net *nn = netdev_priv(netdev);
1990 
1991 	switch (xdp->command) {
1992 	case XDP_SETUP_PROG:
1993 		return nfp_net_xdp_setup_drv(nn, xdp);
1994 	case XDP_SETUP_PROG_HW:
1995 		return nfp_net_xdp_setup_hw(nn, xdp);
1996 	case XDP_SETUP_XSK_POOL:
1997 		return nfp_net_xsk_setup_pool(netdev, xdp->xsk.pool,
1998 					      xdp->xsk.queue_id);
1999 	default:
2000 		return nfp_app_bpf(nn->app, nn, xdp);
2001 	}
2002 }
2003 
2004 static int nfp_net_set_mac_address(struct net_device *netdev, void *addr)
2005 {
2006 	struct nfp_net *nn = netdev_priv(netdev);
2007 	struct sockaddr *saddr = addr;
2008 	int err;
2009 
2010 	err = eth_prepare_mac_addr_change(netdev, addr);
2011 	if (err)
2012 		return err;
2013 
2014 	nfp_net_write_mac_addr(nn, saddr->sa_data);
2015 
2016 	err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_MACADDR);
2017 	if (err)
2018 		return err;
2019 
2020 	eth_commit_mac_addr_change(netdev, addr);
2021 
2022 	return 0;
2023 }
2024 
2025 static int nfp_net_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
2026 				  struct net_device *dev, u32 filter_mask,
2027 				  int nlflags)
2028 {
2029 	struct nfp_net *nn = netdev_priv(dev);
2030 	u16 mode;
2031 
2032 	if (!(nn->cap & NFP_NET_CFG_CTRL_VEPA))
2033 		return -EOPNOTSUPP;
2034 
2035 	mode = (nn->dp.ctrl & NFP_NET_CFG_CTRL_VEPA) ?
2036 	       BRIDGE_MODE_VEPA : BRIDGE_MODE_VEB;
2037 
2038 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, mode, 0, 0,
2039 				       nlflags, filter_mask, NULL);
2040 }
2041 
2042 static int nfp_net_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
2043 				  u16 flags, struct netlink_ext_ack *extack)
2044 {
2045 	struct nfp_net *nn = netdev_priv(dev);
2046 	struct nlattr *attr, *br_spec;
2047 	int rem, err;
2048 	u32 new_ctrl;
2049 	u16 mode;
2050 
2051 	if (!(nn->cap & NFP_NET_CFG_CTRL_VEPA))
2052 		return -EOPNOTSUPP;
2053 
2054 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
2055 	if (!br_spec)
2056 		return -EINVAL;
2057 
2058 	nla_for_each_nested(attr, br_spec, rem) {
2059 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
2060 			continue;
2061 
2062 		if (nla_len(attr) < sizeof(mode))
2063 			return -EINVAL;
2064 
2065 		new_ctrl = nn->dp.ctrl;
2066 		mode = nla_get_u16(attr);
2067 		if (mode == BRIDGE_MODE_VEPA)
2068 			new_ctrl |= NFP_NET_CFG_CTRL_VEPA;
2069 		else if (mode == BRIDGE_MODE_VEB)
2070 			new_ctrl &= ~NFP_NET_CFG_CTRL_VEPA;
2071 		else
2072 			return -EOPNOTSUPP;
2073 
2074 		if (new_ctrl == nn->dp.ctrl)
2075 			return 0;
2076 
2077 		nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
2078 		err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_GEN);
2079 		if (!err)
2080 			nn->dp.ctrl = new_ctrl;
2081 
2082 		return err;
2083 	}
2084 
2085 	return -EINVAL;
2086 }
2087 
2088 const struct net_device_ops nfp_nfd3_netdev_ops = {
2089 	.ndo_init		= nfp_app_ndo_init,
2090 	.ndo_uninit		= nfp_app_ndo_uninit,
2091 	.ndo_open		= nfp_net_netdev_open,
2092 	.ndo_stop		= nfp_net_netdev_close,
2093 	.ndo_start_xmit		= nfp_net_tx,
2094 	.ndo_get_stats64	= nfp_net_stat64,
2095 	.ndo_vlan_rx_add_vid	= nfp_net_vlan_rx_add_vid,
2096 	.ndo_vlan_rx_kill_vid	= nfp_net_vlan_rx_kill_vid,
2097 	.ndo_set_vf_mac         = nfp_app_set_vf_mac,
2098 	.ndo_set_vf_vlan        = nfp_app_set_vf_vlan,
2099 	.ndo_set_vf_rate	= nfp_app_set_vf_rate,
2100 	.ndo_set_vf_spoofchk    = nfp_app_set_vf_spoofchk,
2101 	.ndo_set_vf_trust	= nfp_app_set_vf_trust,
2102 	.ndo_get_vf_config	= nfp_app_get_vf_config,
2103 	.ndo_set_vf_link_state  = nfp_app_set_vf_link_state,
2104 	.ndo_setup_tc		= nfp_port_setup_tc,
2105 	.ndo_tx_timeout		= nfp_net_tx_timeout,
2106 	.ndo_set_rx_mode	= nfp_net_set_rx_mode,
2107 	.ndo_change_mtu		= nfp_net_change_mtu,
2108 	.ndo_set_mac_address	= nfp_net_set_mac_address,
2109 	.ndo_set_features	= nfp_net_set_features,
2110 	.ndo_fix_features	= nfp_net_fix_features,
2111 	.ndo_features_check	= nfp_net_features_check,
2112 	.ndo_get_phys_port_name	= nfp_net_get_phys_port_name,
2113 	.ndo_bpf		= nfp_net_xdp,
2114 	.ndo_xsk_wakeup		= nfp_net_xsk_wakeup,
2115 	.ndo_bridge_getlink     = nfp_net_bridge_getlink,
2116 	.ndo_bridge_setlink     = nfp_net_bridge_setlink,
2117 };
2118 
2119 const struct net_device_ops nfp_nfdk_netdev_ops = {
2120 	.ndo_init		= nfp_app_ndo_init,
2121 	.ndo_uninit		= nfp_app_ndo_uninit,
2122 	.ndo_open		= nfp_net_netdev_open,
2123 	.ndo_stop		= nfp_net_netdev_close,
2124 	.ndo_start_xmit		= nfp_net_tx,
2125 	.ndo_get_stats64	= nfp_net_stat64,
2126 	.ndo_vlan_rx_add_vid	= nfp_net_vlan_rx_add_vid,
2127 	.ndo_vlan_rx_kill_vid	= nfp_net_vlan_rx_kill_vid,
2128 	.ndo_set_vf_mac         = nfp_app_set_vf_mac,
2129 	.ndo_set_vf_vlan        = nfp_app_set_vf_vlan,
2130 	.ndo_set_vf_rate	= nfp_app_set_vf_rate,
2131 	.ndo_set_vf_spoofchk    = nfp_app_set_vf_spoofchk,
2132 	.ndo_set_vf_trust	= nfp_app_set_vf_trust,
2133 	.ndo_get_vf_config	= nfp_app_get_vf_config,
2134 	.ndo_set_vf_link_state  = nfp_app_set_vf_link_state,
2135 	.ndo_setup_tc		= nfp_port_setup_tc,
2136 	.ndo_tx_timeout		= nfp_net_tx_timeout,
2137 	.ndo_set_rx_mode	= nfp_net_set_rx_mode,
2138 	.ndo_change_mtu		= nfp_net_change_mtu,
2139 	.ndo_set_mac_address	= nfp_net_set_mac_address,
2140 	.ndo_set_features	= nfp_net_set_features,
2141 	.ndo_fix_features	= nfp_net_fix_features,
2142 	.ndo_features_check	= nfp_net_features_check,
2143 	.ndo_get_phys_port_name	= nfp_net_get_phys_port_name,
2144 	.ndo_bpf		= nfp_net_xdp,
2145 	.ndo_bridge_getlink     = nfp_net_bridge_getlink,
2146 	.ndo_bridge_setlink     = nfp_net_bridge_setlink,
2147 };
2148 
2149 static int nfp_udp_tunnel_sync(struct net_device *netdev, unsigned int table)
2150 {
2151 	struct nfp_net *nn = netdev_priv(netdev);
2152 	int i;
2153 
2154 	BUILD_BUG_ON(NFP_NET_N_VXLAN_PORTS & 1);
2155 	for (i = 0; i < NFP_NET_N_VXLAN_PORTS; i += 2) {
2156 		struct udp_tunnel_info ti0, ti1;
2157 
2158 		udp_tunnel_nic_get_port(netdev, table, i, &ti0);
2159 		udp_tunnel_nic_get_port(netdev, table, i + 1, &ti1);
2160 
2161 		nn_writel(nn, NFP_NET_CFG_VXLAN_PORT + i * sizeof(ti0.port),
2162 			  be16_to_cpu(ti1.port) << 16 | be16_to_cpu(ti0.port));
2163 	}
2164 
2165 	return nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_VXLAN);
2166 }
2167 
2168 static const struct udp_tunnel_nic_info nfp_udp_tunnels = {
2169 	.sync_table     = nfp_udp_tunnel_sync,
2170 	.flags          = UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
2171 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
2172 	.tables         = {
2173 		{
2174 			.n_entries      = NFP_NET_N_VXLAN_PORTS,
2175 			.tunnel_types   = UDP_TUNNEL_TYPE_VXLAN,
2176 		},
2177 	},
2178 };
2179 
2180 /**
2181  * nfp_net_info() - Print general info about the NIC
2182  * @nn:      NFP Net device to reconfigure
2183  */
2184 void nfp_net_info(struct nfp_net *nn)
2185 {
2186 	nn_info(nn, "NFP-6xxx %sNetdev: TxQs=%d/%d RxQs=%d/%d\n",
2187 		nn->dp.is_vf ? "VF " : "",
2188 		nn->dp.num_tx_rings, nn->max_tx_rings,
2189 		nn->dp.num_rx_rings, nn->max_rx_rings);
2190 	nn_info(nn, "VER: %d.%d.%d.%d, Maximum supported MTU: %d\n",
2191 		nn->fw_ver.extend, nn->fw_ver.class,
2192 		nn->fw_ver.major, nn->fw_ver.minor,
2193 		nn->max_mtu);
2194 	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",
2195 		nn->cap,
2196 		nn->cap & NFP_NET_CFG_CTRL_PROMISC  ? "PROMISC "  : "",
2197 		nn->cap & NFP_NET_CFG_CTRL_L2BC     ? "L2BCFILT " : "",
2198 		nn->cap & NFP_NET_CFG_CTRL_L2MC     ? "L2MCFILT " : "",
2199 		nn->cap & NFP_NET_CFG_CTRL_RXCSUM   ? "RXCSUM "   : "",
2200 		nn->cap & NFP_NET_CFG_CTRL_TXCSUM   ? "TXCSUM "   : "",
2201 		nn->cap & NFP_NET_CFG_CTRL_RXVLAN   ? "RXVLAN "   : "",
2202 		nn->cap & NFP_NET_CFG_CTRL_TXVLAN   ? "TXVLAN "   : "",
2203 		nn->cap & NFP_NET_CFG_CTRL_RXQINQ   ? "RXQINQ "   : "",
2204 		nn->cap & NFP_NET_CFG_CTRL_RXVLAN_V2 ? "RXVLANv2 "   : "",
2205 		nn->cap & NFP_NET_CFG_CTRL_TXVLAN_V2   ? "TXVLANv2 "   : "",
2206 		nn->cap & NFP_NET_CFG_CTRL_SCATTER  ? "SCATTER "  : "",
2207 		nn->cap & NFP_NET_CFG_CTRL_GATHER   ? "GATHER "   : "",
2208 		nn->cap & NFP_NET_CFG_CTRL_LSO      ? "TSO1 "     : "",
2209 		nn->cap & NFP_NET_CFG_CTRL_LSO2     ? "TSO2 "     : "",
2210 		nn->cap & NFP_NET_CFG_CTRL_RSS      ? "RSS1 "     : "",
2211 		nn->cap & NFP_NET_CFG_CTRL_RSS2     ? "RSS2 "     : "",
2212 		nn->cap & NFP_NET_CFG_CTRL_CTAG_FILTER ? "CTAG_FILTER " : "",
2213 		nn->cap & NFP_NET_CFG_CTRL_MSIXAUTO ? "AUTOMASK " : "",
2214 		nn->cap & NFP_NET_CFG_CTRL_IRQMOD   ? "IRQMOD "   : "",
2215 		nn->cap & NFP_NET_CFG_CTRL_TXRWB    ? "TXRWB "    : "",
2216 		nn->cap & NFP_NET_CFG_CTRL_VEPA     ? "VEPA "     : "",
2217 		nn->cap & NFP_NET_CFG_CTRL_VXLAN    ? "VXLAN "    : "",
2218 		nn->cap & NFP_NET_CFG_CTRL_NVGRE    ? "NVGRE "	  : "",
2219 		nn->cap & NFP_NET_CFG_CTRL_CSUM_COMPLETE ?
2220 						      "RXCSUM_COMPLETE " : "",
2221 		nn->cap & NFP_NET_CFG_CTRL_LIVE_ADDR ? "LIVE_ADDR " : "",
2222 		nn->cap_w1 & NFP_NET_CFG_CTRL_MCAST_FILTER ? "MULTICAST_FILTER " : "",
2223 		nfp_app_extra_cap(nn->app, nn));
2224 }
2225 
2226 /**
2227  * nfp_net_alloc() - Allocate netdev and related structure
2228  * @pdev:         PCI device
2229  * @dev_info:     NFP ASIC params
2230  * @ctrl_bar:     PCI IOMEM with vNIC config memory
2231  * @needs_netdev: Whether to allocate a netdev for this vNIC
2232  * @max_tx_rings: Maximum number of TX rings supported by device
2233  * @max_rx_rings: Maximum number of RX rings supported by device
2234  *
2235  * This function allocates a netdev device and fills in the initial
2236  * part of the @struct nfp_net structure.  In case of control device
2237  * nfp_net structure is allocated without the netdev.
2238  *
2239  * Return: NFP Net device structure, or ERR_PTR on error.
2240  */
2241 struct nfp_net *
2242 nfp_net_alloc(struct pci_dev *pdev, const struct nfp_dev_info *dev_info,
2243 	      void __iomem *ctrl_bar, bool needs_netdev,
2244 	      unsigned int max_tx_rings, unsigned int max_rx_rings)
2245 {
2246 	u64 dma_mask = dma_get_mask(&pdev->dev);
2247 	struct nfp_net *nn;
2248 	int err;
2249 
2250 	if (needs_netdev) {
2251 		struct net_device *netdev;
2252 
2253 		netdev = alloc_etherdev_mqs(sizeof(struct nfp_net),
2254 					    max_tx_rings, max_rx_rings);
2255 		if (!netdev)
2256 			return ERR_PTR(-ENOMEM);
2257 
2258 		SET_NETDEV_DEV(netdev, &pdev->dev);
2259 		nn = netdev_priv(netdev);
2260 		nn->dp.netdev = netdev;
2261 	} else {
2262 		nn = vzalloc(sizeof(*nn));
2263 		if (!nn)
2264 			return ERR_PTR(-ENOMEM);
2265 	}
2266 
2267 	nn->dp.dev = &pdev->dev;
2268 	nn->dp.ctrl_bar = ctrl_bar;
2269 	nn->dev_info = dev_info;
2270 	nn->pdev = pdev;
2271 	nfp_net_get_fw_version(&nn->fw_ver, ctrl_bar);
2272 
2273 	switch (FIELD_GET(NFP_NET_CFG_VERSION_DP_MASK, nn->fw_ver.extend)) {
2274 	case NFP_NET_CFG_VERSION_DP_NFD3:
2275 		nn->dp.ops = &nfp_nfd3_ops;
2276 		break;
2277 	case NFP_NET_CFG_VERSION_DP_NFDK:
2278 		if (nn->fw_ver.major < 5) {
2279 			dev_err(&pdev->dev,
2280 				"NFDK must use ABI 5 or newer, found: %d\n",
2281 				nn->fw_ver.major);
2282 			err = -EINVAL;
2283 			goto err_free_nn;
2284 		}
2285 		nn->dp.ops = &nfp_nfdk_ops;
2286 		break;
2287 	default:
2288 		err = -EINVAL;
2289 		goto err_free_nn;
2290 	}
2291 
2292 	if ((dma_mask & nn->dp.ops->dma_mask) != dma_mask) {
2293 		dev_err(&pdev->dev,
2294 			"DMA mask of loaded firmware: %llx, required DMA mask: %llx\n",
2295 			nn->dp.ops->dma_mask, dma_mask);
2296 		err = -EINVAL;
2297 		goto err_free_nn;
2298 	}
2299 
2300 	nn->max_tx_rings = max_tx_rings;
2301 	nn->max_rx_rings = max_rx_rings;
2302 
2303 	nn->dp.num_tx_rings = min_t(unsigned int,
2304 				    max_tx_rings, num_online_cpus());
2305 	nn->dp.num_rx_rings = min_t(unsigned int, max_rx_rings,
2306 				 netif_get_num_default_rss_queues());
2307 
2308 	nn->dp.num_r_vecs = max(nn->dp.num_tx_rings, nn->dp.num_rx_rings);
2309 	nn->dp.num_r_vecs = min_t(unsigned int,
2310 				  nn->dp.num_r_vecs, num_online_cpus());
2311 	nn->max_r_vecs = nn->dp.num_r_vecs;
2312 
2313 	nn->dp.xsk_pools = kcalloc(nn->max_r_vecs, sizeof(nn->dp.xsk_pools),
2314 				   GFP_KERNEL);
2315 	if (!nn->dp.xsk_pools) {
2316 		err = -ENOMEM;
2317 		goto err_free_nn;
2318 	}
2319 
2320 	nn->dp.txd_cnt = NFP_NET_TX_DESCS_DEFAULT;
2321 	nn->dp.rxd_cnt = NFP_NET_RX_DESCS_DEFAULT;
2322 
2323 	sema_init(&nn->bar_lock, 1);
2324 
2325 	spin_lock_init(&nn->reconfig_lock);
2326 	spin_lock_init(&nn->link_status_lock);
2327 
2328 	timer_setup(&nn->reconfig_timer, nfp_net_reconfig_timer, 0);
2329 
2330 	err = nfp_net_tlv_caps_parse(&nn->pdev->dev, nn->dp.ctrl_bar,
2331 				     &nn->tlv_caps);
2332 	if (err)
2333 		goto err_free_nn;
2334 
2335 	err = nfp_ccm_mbox_alloc(nn);
2336 	if (err)
2337 		goto err_free_nn;
2338 
2339 	return nn;
2340 
2341 err_free_nn:
2342 	if (nn->dp.netdev)
2343 		free_netdev(nn->dp.netdev);
2344 	else
2345 		vfree(nn);
2346 	return ERR_PTR(err);
2347 }
2348 
2349 /**
2350  * nfp_net_free() - Undo what @nfp_net_alloc() did
2351  * @nn:      NFP Net device to reconfigure
2352  */
2353 void nfp_net_free(struct nfp_net *nn)
2354 {
2355 	WARN_ON(timer_pending(&nn->reconfig_timer) || nn->reconfig_posted);
2356 	nfp_ccm_mbox_free(nn);
2357 
2358 	kfree(nn->dp.xsk_pools);
2359 	if (nn->dp.netdev)
2360 		free_netdev(nn->dp.netdev);
2361 	else
2362 		vfree(nn);
2363 }
2364 
2365 /**
2366  * nfp_net_rss_key_sz() - Get current size of the RSS key
2367  * @nn:		NFP Net device instance
2368  *
2369  * Return: size of the RSS key for currently selected hash function.
2370  */
2371 unsigned int nfp_net_rss_key_sz(struct nfp_net *nn)
2372 {
2373 	switch (nn->rss_hfunc) {
2374 	case ETH_RSS_HASH_TOP:
2375 		return NFP_NET_CFG_RSS_KEY_SZ;
2376 	case ETH_RSS_HASH_XOR:
2377 		return 0;
2378 	case ETH_RSS_HASH_CRC32:
2379 		return 4;
2380 	}
2381 
2382 	nn_warn(nn, "Unknown hash function: %u\n", nn->rss_hfunc);
2383 	return 0;
2384 }
2385 
2386 /**
2387  * nfp_net_rss_init() - Set the initial RSS parameters
2388  * @nn:	     NFP Net device to reconfigure
2389  */
2390 static void nfp_net_rss_init(struct nfp_net *nn)
2391 {
2392 	unsigned long func_bit, rss_cap_hfunc;
2393 	u32 reg;
2394 
2395 	/* Read the RSS function capability and select first supported func */
2396 	reg = nn_readl(nn, NFP_NET_CFG_RSS_CAP);
2397 	rss_cap_hfunc =	FIELD_GET(NFP_NET_CFG_RSS_CAP_HFUNC, reg);
2398 	if (!rss_cap_hfunc)
2399 		rss_cap_hfunc =	FIELD_GET(NFP_NET_CFG_RSS_CAP_HFUNC,
2400 					  NFP_NET_CFG_RSS_TOEPLITZ);
2401 
2402 	func_bit = find_first_bit(&rss_cap_hfunc, NFP_NET_CFG_RSS_HFUNCS);
2403 	if (func_bit == NFP_NET_CFG_RSS_HFUNCS) {
2404 		dev_warn(nn->dp.dev,
2405 			 "Bad RSS config, defaulting to Toeplitz hash\n");
2406 		func_bit = ETH_RSS_HASH_TOP_BIT;
2407 	}
2408 	nn->rss_hfunc = 1 << func_bit;
2409 
2410 	netdev_rss_key_fill(nn->rss_key, nfp_net_rss_key_sz(nn));
2411 
2412 	nfp_net_rss_init_itbl(nn);
2413 
2414 	/* Enable IPv4/IPv6 TCP by default */
2415 	nn->rss_cfg = NFP_NET_CFG_RSS_IPV4_TCP |
2416 		      NFP_NET_CFG_RSS_IPV6_TCP |
2417 		      FIELD_PREP(NFP_NET_CFG_RSS_HFUNC, nn->rss_hfunc) |
2418 		      NFP_NET_CFG_RSS_MASK;
2419 }
2420 
2421 /**
2422  * nfp_net_irqmod_init() - Set the initial IRQ moderation parameters
2423  * @nn:	     NFP Net device to reconfigure
2424  */
2425 static void nfp_net_irqmod_init(struct nfp_net *nn)
2426 {
2427 	nn->rx_coalesce_usecs      = 50;
2428 	nn->rx_coalesce_max_frames = 64;
2429 	nn->tx_coalesce_usecs      = 50;
2430 	nn->tx_coalesce_max_frames = 64;
2431 
2432 	nn->rx_coalesce_adapt_on   = true;
2433 	nn->tx_coalesce_adapt_on   = true;
2434 }
2435 
2436 static void nfp_net_netdev_init(struct nfp_net *nn)
2437 {
2438 	struct net_device *netdev = nn->dp.netdev;
2439 
2440 	nfp_net_write_mac_addr(nn, nn->dp.netdev->dev_addr);
2441 
2442 	netdev->mtu = nn->dp.mtu;
2443 
2444 	/* Advertise/enable offloads based on capabilities
2445 	 *
2446 	 * Note: netdev->features show the currently enabled features
2447 	 * and netdev->hw_features advertises which features are
2448 	 * supported.  By default we enable most features.
2449 	 */
2450 	if (nn->cap & NFP_NET_CFG_CTRL_LIVE_ADDR)
2451 		netdev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2452 
2453 	netdev->hw_features = NETIF_F_HIGHDMA;
2454 	if (nn->cap & NFP_NET_CFG_CTRL_RXCSUM_ANY) {
2455 		netdev->hw_features |= NETIF_F_RXCSUM;
2456 		nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_RXCSUM_ANY;
2457 	}
2458 	if (nn->cap & NFP_NET_CFG_CTRL_TXCSUM) {
2459 		netdev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
2460 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_TXCSUM;
2461 	}
2462 	if (nn->cap & NFP_NET_CFG_CTRL_GATHER) {
2463 		netdev->hw_features |= NETIF_F_SG;
2464 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_GATHER;
2465 	}
2466 	if ((nn->cap & NFP_NET_CFG_CTRL_LSO && nn->fw_ver.major > 2) ||
2467 	    nn->cap & NFP_NET_CFG_CTRL_LSO2) {
2468 		netdev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
2469 		nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_LSO2 ?:
2470 					 NFP_NET_CFG_CTRL_LSO;
2471 	}
2472 	if (nn->cap & NFP_NET_CFG_CTRL_RSS_ANY)
2473 		netdev->hw_features |= NETIF_F_RXHASH;
2474 
2475 #ifdef CONFIG_NFP_NET_IPSEC
2476 	if (nn->cap_w1 & NFP_NET_CFG_CTRL_IPSEC)
2477 		netdev->hw_features |= NETIF_F_HW_ESP | NETIF_F_HW_ESP_TX_CSUM;
2478 #endif
2479 
2480 	if (nn->cap & NFP_NET_CFG_CTRL_VXLAN) {
2481 		if (nn->cap & NFP_NET_CFG_CTRL_LSO) {
2482 			netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL |
2483 					       NETIF_F_GSO_UDP_TUNNEL_CSUM |
2484 					       NETIF_F_GSO_PARTIAL;
2485 			netdev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM;
2486 		}
2487 		netdev->udp_tunnel_nic_info = &nfp_udp_tunnels;
2488 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_VXLAN;
2489 	}
2490 	if (nn->cap & NFP_NET_CFG_CTRL_NVGRE) {
2491 		if (nn->cap & NFP_NET_CFG_CTRL_LSO)
2492 			netdev->hw_features |= NETIF_F_GSO_GRE;
2493 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_NVGRE;
2494 	}
2495 	if (nn->cap & (NFP_NET_CFG_CTRL_VXLAN | NFP_NET_CFG_CTRL_NVGRE))
2496 		netdev->hw_enc_features = netdev->hw_features;
2497 
2498 	netdev->vlan_features = netdev->hw_features;
2499 
2500 	if (nn->cap & NFP_NET_CFG_CTRL_RXVLAN_ANY) {
2501 		netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
2502 		nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_RXVLAN_V2 ?:
2503 			       NFP_NET_CFG_CTRL_RXVLAN;
2504 	}
2505 	if (nn->cap & NFP_NET_CFG_CTRL_TXVLAN_ANY) {
2506 		if (nn->cap & NFP_NET_CFG_CTRL_LSO2) {
2507 			nn_warn(nn, "Device advertises both TSO2 and TXVLAN. Refusing to enable TXVLAN.\n");
2508 		} else {
2509 			netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
2510 			nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_TXVLAN_V2 ?:
2511 				       NFP_NET_CFG_CTRL_TXVLAN;
2512 		}
2513 	}
2514 	if (nn->cap & NFP_NET_CFG_CTRL_CTAG_FILTER) {
2515 		netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
2516 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_CTAG_FILTER;
2517 	}
2518 	if (nn->cap & NFP_NET_CFG_CTRL_RXQINQ) {
2519 		netdev->hw_features |= NETIF_F_HW_VLAN_STAG_RX;
2520 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_RXQINQ;
2521 	}
2522 
2523 	netdev->features = netdev->hw_features;
2524 
2525 	if (nfp_app_has_tc(nn->app) && nn->port)
2526 		netdev->hw_features |= NETIF_F_HW_TC;
2527 
2528 	/* C-Tag strip and S-Tag strip can't be supported simultaneously,
2529 	 * so enable C-Tag strip and disable S-Tag strip by default.
2530 	 */
2531 	netdev->features &= ~NETIF_F_HW_VLAN_STAG_RX;
2532 	nn->dp.ctrl &= ~NFP_NET_CFG_CTRL_RXQINQ;
2533 
2534 	netdev->xdp_features = NETDEV_XDP_ACT_BASIC;
2535 	if (nn->app && nn->app->type->id == NFP_APP_BPF_NIC)
2536 		netdev->xdp_features |= NETDEV_XDP_ACT_HW_OFFLOAD;
2537 
2538 	/* Finalise the netdev setup */
2539 	switch (nn->dp.ops->version) {
2540 	case NFP_NFD_VER_NFD3:
2541 		netdev->netdev_ops = &nfp_nfd3_netdev_ops;
2542 		netdev->xdp_features |= NETDEV_XDP_ACT_XSK_ZEROCOPY;
2543 		break;
2544 	case NFP_NFD_VER_NFDK:
2545 		netdev->netdev_ops = &nfp_nfdk_netdev_ops;
2546 		break;
2547 	}
2548 
2549 	netdev->watchdog_timeo = msecs_to_jiffies(5 * 1000);
2550 
2551 	/* MTU range: 68 - hw-specific max */
2552 	netdev->min_mtu = ETH_MIN_MTU;
2553 	netdev->max_mtu = nn->max_mtu;
2554 
2555 	netif_set_tso_max_segs(netdev, NFP_NET_LSO_MAX_SEGS);
2556 
2557 	netif_carrier_off(netdev);
2558 
2559 	nfp_net_set_ethtool_ops(netdev);
2560 }
2561 
2562 static int nfp_net_read_caps(struct nfp_net *nn)
2563 {
2564 	/* Get some of the read-only fields from the BAR */
2565 	nn->cap = nn_readl(nn, NFP_NET_CFG_CAP);
2566 	nn->cap_w1 = nn_readl(nn, NFP_NET_CFG_CAP_WORD1);
2567 	nn->max_mtu = nn_readl(nn, NFP_NET_CFG_MAX_MTU);
2568 
2569 	/* ABI 4.x and ctrl vNIC always use chained metadata, in other cases
2570 	 * we allow use of non-chained metadata if RSS(v1) is the only
2571 	 * advertised capability requiring metadata.
2572 	 */
2573 	nn->dp.chained_metadata_format = nn->fw_ver.major == 4 ||
2574 					 !nn->dp.netdev ||
2575 					 !(nn->cap & NFP_NET_CFG_CTRL_RSS) ||
2576 					 nn->cap & NFP_NET_CFG_CTRL_CHAIN_META;
2577 	/* RSS(v1) uses non-chained metadata format, except in ABI 4.x where
2578 	 * it has the same meaning as RSSv2.
2579 	 */
2580 	if (nn->dp.chained_metadata_format && nn->fw_ver.major != 4)
2581 		nn->cap &= ~NFP_NET_CFG_CTRL_RSS;
2582 
2583 	/* Determine RX packet/metadata boundary offset */
2584 	if (nn->fw_ver.major >= 2) {
2585 		u32 reg;
2586 
2587 		reg = nn_readl(nn, NFP_NET_CFG_RX_OFFSET);
2588 		if (reg > NFP_NET_MAX_PREPEND) {
2589 			nn_err(nn, "Invalid rx offset: %d\n", reg);
2590 			return -EINVAL;
2591 		}
2592 		nn->dp.rx_offset = reg;
2593 	} else {
2594 		nn->dp.rx_offset = NFP_NET_RX_OFFSET;
2595 	}
2596 
2597 	/* Mask out NFD-version-specific features */
2598 	nn->cap &= nn->dp.ops->cap_mask;
2599 
2600 	/* For control vNICs mask out the capabilities app doesn't want. */
2601 	if (!nn->dp.netdev)
2602 		nn->cap &= nn->app->type->ctrl_cap_mask;
2603 
2604 	return 0;
2605 }
2606 
2607 /**
2608  * nfp_net_init() - Initialise/finalise the nfp_net structure
2609  * @nn:		NFP Net device structure
2610  *
2611  * Return: 0 on success or negative errno on error.
2612  */
2613 int nfp_net_init(struct nfp_net *nn)
2614 {
2615 	int err;
2616 
2617 	nn->dp.rx_dma_dir = DMA_FROM_DEVICE;
2618 
2619 	err = nfp_net_read_caps(nn);
2620 	if (err)
2621 		return err;
2622 
2623 	/* Set default MTU and Freelist buffer size */
2624 	if (!nfp_net_is_data_vnic(nn) && nn->app->ctrl_mtu) {
2625 		nn->dp.mtu = min(nn->app->ctrl_mtu, nn->max_mtu);
2626 	} else if (nn->max_mtu < NFP_NET_DEFAULT_MTU) {
2627 		nn->dp.mtu = nn->max_mtu;
2628 	} else {
2629 		nn->dp.mtu = NFP_NET_DEFAULT_MTU;
2630 	}
2631 	nn->dp.fl_bufsz = nfp_net_calc_fl_bufsz(&nn->dp);
2632 
2633 	if (nfp_app_ctrl_uses_data_vnics(nn->app))
2634 		nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_CMSG_DATA;
2635 
2636 	if (nn->cap & NFP_NET_CFG_CTRL_RSS_ANY) {
2637 		nfp_net_rss_init(nn);
2638 		nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_RSS2 ?:
2639 					 NFP_NET_CFG_CTRL_RSS;
2640 	}
2641 
2642 	/* Allow L2 Broadcast and Multicast through by default, if supported */
2643 	if (nn->cap & NFP_NET_CFG_CTRL_L2BC)
2644 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_L2BC;
2645 
2646 	/* Allow IRQ moderation, if supported */
2647 	if (nn->cap & NFP_NET_CFG_CTRL_IRQMOD) {
2648 		nfp_net_irqmod_init(nn);
2649 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_IRQMOD;
2650 	}
2651 
2652 	/* Enable TX pointer writeback, if supported */
2653 	if (nn->cap & NFP_NET_CFG_CTRL_TXRWB)
2654 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_TXRWB;
2655 
2656 	if (nn->cap_w1 & NFP_NET_CFG_CTRL_MCAST_FILTER)
2657 		nn->dp.ctrl_w1 |= NFP_NET_CFG_CTRL_MCAST_FILTER;
2658 
2659 	/* Stash the re-configuration queue away.  First odd queue in TX Bar */
2660 	nn->qcp_cfg = nn->tx_bar + NFP_QCP_QUEUE_ADDR_SZ;
2661 
2662 	/* Make sure the FW knows the netdev is supposed to be disabled here */
2663 	nn_writel(nn, NFP_NET_CFG_CTRL, 0);
2664 	nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, 0);
2665 	nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, 0);
2666 	nn_writel(nn, NFP_NET_CFG_CTRL_WORD1, 0);
2667 	err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_RING |
2668 				   NFP_NET_CFG_UPDATE_GEN);
2669 	if (err)
2670 		return err;
2671 
2672 	if (nn->dp.netdev) {
2673 		nfp_net_netdev_init(nn);
2674 
2675 		err = nfp_ccm_mbox_init(nn);
2676 		if (err)
2677 			return err;
2678 
2679 		err = nfp_net_tls_init(nn);
2680 		if (err)
2681 			goto err_clean_mbox;
2682 
2683 		nfp_net_ipsec_init(nn);
2684 	}
2685 
2686 	nfp_net_vecs_init(nn);
2687 
2688 	if (!nn->dp.netdev)
2689 		return 0;
2690 
2691 	spin_lock_init(&nn->mbox_amsg.lock);
2692 	INIT_LIST_HEAD(&nn->mbox_amsg.list);
2693 	INIT_WORK(&nn->mbox_amsg.work, nfp_net_mbox_amsg_work);
2694 
2695 	return register_netdev(nn->dp.netdev);
2696 
2697 err_clean_mbox:
2698 	nfp_ccm_mbox_clean(nn);
2699 	return err;
2700 }
2701 
2702 /**
2703  * nfp_net_clean() - Undo what nfp_net_init() did.
2704  * @nn:		NFP Net device structure
2705  */
2706 void nfp_net_clean(struct nfp_net *nn)
2707 {
2708 	if (!nn->dp.netdev)
2709 		return;
2710 
2711 	unregister_netdev(nn->dp.netdev);
2712 	nfp_net_ipsec_clean(nn);
2713 	nfp_ccm_mbox_clean(nn);
2714 	flush_work(&nn->mbox_amsg.work);
2715 	nfp_net_reconfig_wait_posted(nn);
2716 }
2717