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 	err = nfp_net_reconfig(nn, update);
1011 	if (err) {
1012 		nfp_net_clear_config_and_disable(nn);
1013 		return err;
1014 	}
1015 
1016 	nn->dp.ctrl = new_ctrl;
1017 
1018 	for (r = 0; r < nn->dp.num_rx_rings; r++)
1019 		nfp_net_rx_ring_fill_freelist(&nn->dp, &nn->dp.rx_rings[r]);
1020 
1021 	return 0;
1022 }
1023 
1024 /**
1025  * nfp_net_close_stack() - Quiesce the stack (part of close)
1026  * @nn:	     NFP Net device to reconfigure
1027  */
1028 static void nfp_net_close_stack(struct nfp_net *nn)
1029 {
1030 	struct nfp_net_r_vector *r_vec;
1031 	unsigned int r;
1032 
1033 	disable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
1034 	netif_carrier_off(nn->dp.netdev);
1035 	nn->link_up = false;
1036 
1037 	for (r = 0; r < nn->dp.num_r_vecs; r++) {
1038 		r_vec = &nn->r_vecs[r];
1039 
1040 		disable_irq(r_vec->irq_vector);
1041 		napi_disable(&r_vec->napi);
1042 
1043 		if (r_vec->rx_ring)
1044 			cancel_work_sync(&r_vec->rx_dim.work);
1045 
1046 		if (r_vec->tx_ring)
1047 			cancel_work_sync(&r_vec->tx_dim.work);
1048 	}
1049 
1050 	netif_tx_disable(nn->dp.netdev);
1051 }
1052 
1053 /**
1054  * nfp_net_close_free_all() - Free all runtime resources
1055  * @nn:      NFP Net device to reconfigure
1056  */
1057 static void nfp_net_close_free_all(struct nfp_net *nn)
1058 {
1059 	unsigned int r;
1060 
1061 	nfp_net_tx_rings_free(&nn->dp);
1062 	nfp_net_rx_rings_free(&nn->dp);
1063 
1064 	for (r = 0; r < nn->dp.num_r_vecs; r++)
1065 		nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
1066 
1067 	nfp_net_aux_irq_free(nn, NFP_NET_CFG_LSC, NFP_NET_IRQ_LSC_IDX);
1068 	nfp_net_aux_irq_free(nn, NFP_NET_CFG_EXN, NFP_NET_IRQ_EXN_IDX);
1069 }
1070 
1071 /**
1072  * nfp_net_netdev_close() - Called when the device is downed
1073  * @netdev:      netdev structure
1074  */
1075 static int nfp_net_netdev_close(struct net_device *netdev)
1076 {
1077 	struct nfp_net *nn = netdev_priv(netdev);
1078 
1079 	/* Step 1: Disable RX and TX rings from the Linux kernel perspective
1080 	 */
1081 	nfp_net_close_stack(nn);
1082 
1083 	/* Step 2: Tell NFP
1084 	 */
1085 	nfp_net_clear_config_and_disable(nn);
1086 	nfp_port_configure(netdev, false);
1087 
1088 	/* Step 3: Free resources
1089 	 */
1090 	nfp_net_close_free_all(nn);
1091 
1092 	nn_dbg(nn, "%s down", netdev->name);
1093 	return 0;
1094 }
1095 
1096 void nfp_ctrl_close(struct nfp_net *nn)
1097 {
1098 	int r;
1099 
1100 	rtnl_lock();
1101 
1102 	for (r = 0; r < nn->dp.num_r_vecs; r++) {
1103 		disable_irq(nn->r_vecs[r].irq_vector);
1104 		tasklet_disable(&nn->r_vecs[r].tasklet);
1105 	}
1106 
1107 	nfp_net_clear_config_and_disable(nn);
1108 
1109 	nfp_net_close_free_all(nn);
1110 
1111 	rtnl_unlock();
1112 }
1113 
1114 static void nfp_net_rx_dim_work(struct work_struct *work)
1115 {
1116 	struct nfp_net_r_vector *r_vec;
1117 	unsigned int factor, value;
1118 	struct dim_cq_moder moder;
1119 	struct nfp_net *nn;
1120 	struct dim *dim;
1121 
1122 	dim = container_of(work, struct dim, work);
1123 	moder = net_dim_get_rx_moderation(dim->mode, dim->profile_ix);
1124 	r_vec = container_of(dim, struct nfp_net_r_vector, rx_dim);
1125 	nn = r_vec->nfp_net;
1126 
1127 	/* Compute factor used to convert coalesce '_usecs' parameters to
1128 	 * ME timestamp ticks.  There are 16 ME clock cycles for each timestamp
1129 	 * count.
1130 	 */
1131 	factor = nn->tlv_caps.me_freq_mhz / 16;
1132 	if (nfp_net_coalesce_para_check(factor * moder.usec, moder.pkts))
1133 		return;
1134 
1135 	/* copy RX interrupt coalesce parameters */
1136 	value = (moder.pkts << 16) | (factor * moder.usec);
1137 	nn_writel(nn, NFP_NET_CFG_RXR_IRQ_MOD(r_vec->rx_ring->idx), value);
1138 	(void)nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_IRQMOD);
1139 
1140 	dim->state = DIM_START_MEASURE;
1141 }
1142 
1143 static void nfp_net_tx_dim_work(struct work_struct *work)
1144 {
1145 	struct nfp_net_r_vector *r_vec;
1146 	unsigned int factor, value;
1147 	struct dim_cq_moder moder;
1148 	struct nfp_net *nn;
1149 	struct dim *dim;
1150 
1151 	dim = container_of(work, struct dim, work);
1152 	moder = net_dim_get_tx_moderation(dim->mode, dim->profile_ix);
1153 	r_vec = container_of(dim, struct nfp_net_r_vector, tx_dim);
1154 	nn = r_vec->nfp_net;
1155 
1156 	/* Compute factor used to convert coalesce '_usecs' parameters to
1157 	 * ME timestamp ticks.  There are 16 ME clock cycles for each timestamp
1158 	 * count.
1159 	 */
1160 	factor = nn->tlv_caps.me_freq_mhz / 16;
1161 	if (nfp_net_coalesce_para_check(factor * moder.usec, moder.pkts))
1162 		return;
1163 
1164 	/* copy TX interrupt coalesce parameters */
1165 	value = (moder.pkts << 16) | (factor * moder.usec);
1166 	nn_writel(nn, NFP_NET_CFG_TXR_IRQ_MOD(r_vec->tx_ring->idx), value);
1167 	(void)nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_IRQMOD);
1168 
1169 	dim->state = DIM_START_MEASURE;
1170 }
1171 
1172 /**
1173  * nfp_net_open_stack() - Start the device from stack's perspective
1174  * @nn:      NFP Net device to reconfigure
1175  */
1176 static void nfp_net_open_stack(struct nfp_net *nn)
1177 {
1178 	struct nfp_net_r_vector *r_vec;
1179 	unsigned int r;
1180 
1181 	for (r = 0; r < nn->dp.num_r_vecs; r++) {
1182 		r_vec = &nn->r_vecs[r];
1183 
1184 		if (r_vec->rx_ring) {
1185 			INIT_WORK(&r_vec->rx_dim.work, nfp_net_rx_dim_work);
1186 			r_vec->rx_dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
1187 		}
1188 
1189 		if (r_vec->tx_ring) {
1190 			INIT_WORK(&r_vec->tx_dim.work, nfp_net_tx_dim_work);
1191 			r_vec->tx_dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
1192 		}
1193 
1194 		napi_enable(&r_vec->napi);
1195 		enable_irq(r_vec->irq_vector);
1196 	}
1197 
1198 	netif_tx_wake_all_queues(nn->dp.netdev);
1199 
1200 	enable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
1201 	nfp_net_read_link_status(nn);
1202 }
1203 
1204 static int nfp_net_open_alloc_all(struct nfp_net *nn)
1205 {
1206 	int err, r;
1207 
1208 	err = nfp_net_aux_irq_request(nn, NFP_NET_CFG_EXN, "%s-exn",
1209 				      nn->exn_name, sizeof(nn->exn_name),
1210 				      NFP_NET_IRQ_EXN_IDX, nn->exn_handler);
1211 	if (err)
1212 		return err;
1213 	err = nfp_net_aux_irq_request(nn, NFP_NET_CFG_LSC, "%s-lsc",
1214 				      nn->lsc_name, sizeof(nn->lsc_name),
1215 				      NFP_NET_IRQ_LSC_IDX, nn->lsc_handler);
1216 	if (err)
1217 		goto err_free_exn;
1218 	disable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
1219 
1220 	for (r = 0; r < nn->dp.num_r_vecs; r++) {
1221 		err = nfp_net_prepare_vector(nn, &nn->r_vecs[r], r);
1222 		if (err)
1223 			goto err_cleanup_vec_p;
1224 	}
1225 
1226 	err = nfp_net_rx_rings_prepare(nn, &nn->dp);
1227 	if (err)
1228 		goto err_cleanup_vec;
1229 
1230 	err = nfp_net_tx_rings_prepare(nn, &nn->dp);
1231 	if (err)
1232 		goto err_free_rx_rings;
1233 
1234 	for (r = 0; r < nn->max_r_vecs; r++)
1235 		nfp_net_vector_assign_rings(&nn->dp, &nn->r_vecs[r], r);
1236 
1237 	return 0;
1238 
1239 err_free_rx_rings:
1240 	nfp_net_rx_rings_free(&nn->dp);
1241 err_cleanup_vec:
1242 	r = nn->dp.num_r_vecs;
1243 err_cleanup_vec_p:
1244 	while (r--)
1245 		nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
1246 	nfp_net_aux_irq_free(nn, NFP_NET_CFG_LSC, NFP_NET_IRQ_LSC_IDX);
1247 err_free_exn:
1248 	nfp_net_aux_irq_free(nn, NFP_NET_CFG_EXN, NFP_NET_IRQ_EXN_IDX);
1249 	return err;
1250 }
1251 
1252 static int nfp_net_netdev_open(struct net_device *netdev)
1253 {
1254 	struct nfp_net *nn = netdev_priv(netdev);
1255 	int err;
1256 
1257 	/* Step 1: Allocate resources for rings and the like
1258 	 * - Request interrupts
1259 	 * - Allocate RX and TX ring resources
1260 	 * - Setup initial RSS table
1261 	 */
1262 	err = nfp_net_open_alloc_all(nn);
1263 	if (err)
1264 		return err;
1265 
1266 	err = netif_set_real_num_tx_queues(netdev, nn->dp.num_stack_tx_rings);
1267 	if (err)
1268 		goto err_free_all;
1269 
1270 	err = netif_set_real_num_rx_queues(netdev, nn->dp.num_rx_rings);
1271 	if (err)
1272 		goto err_free_all;
1273 
1274 	/* Step 2: Configure the NFP
1275 	 * - Ifup the physical interface if it exists
1276 	 * - Enable rings from 0 to tx_rings/rx_rings - 1.
1277 	 * - Write MAC address (in case it changed)
1278 	 * - Set the MTU
1279 	 * - Set the Freelist buffer size
1280 	 * - Enable the FW
1281 	 */
1282 	err = nfp_port_configure(netdev, true);
1283 	if (err)
1284 		goto err_free_all;
1285 
1286 	err = nfp_net_set_config_and_enable(nn);
1287 	if (err)
1288 		goto err_port_disable;
1289 
1290 	/* Step 3: Enable for kernel
1291 	 * - put some freelist descriptors on each RX ring
1292 	 * - enable NAPI on each ring
1293 	 * - enable all TX queues
1294 	 * - set link state
1295 	 */
1296 	nfp_net_open_stack(nn);
1297 
1298 	return 0;
1299 
1300 err_port_disable:
1301 	nfp_port_configure(netdev, false);
1302 err_free_all:
1303 	nfp_net_close_free_all(nn);
1304 	return err;
1305 }
1306 
1307 int nfp_ctrl_open(struct nfp_net *nn)
1308 {
1309 	int err, r;
1310 
1311 	/* ring dumping depends on vNICs being opened/closed under rtnl */
1312 	rtnl_lock();
1313 
1314 	err = nfp_net_open_alloc_all(nn);
1315 	if (err)
1316 		goto err_unlock;
1317 
1318 	err = nfp_net_set_config_and_enable(nn);
1319 	if (err)
1320 		goto err_free_all;
1321 
1322 	for (r = 0; r < nn->dp.num_r_vecs; r++)
1323 		enable_irq(nn->r_vecs[r].irq_vector);
1324 
1325 	rtnl_unlock();
1326 
1327 	return 0;
1328 
1329 err_free_all:
1330 	nfp_net_close_free_all(nn);
1331 err_unlock:
1332 	rtnl_unlock();
1333 	return err;
1334 }
1335 
1336 static void nfp_net_set_rx_mode(struct net_device *netdev)
1337 {
1338 	struct nfp_net *nn = netdev_priv(netdev);
1339 	u32 new_ctrl;
1340 
1341 	new_ctrl = nn->dp.ctrl;
1342 
1343 	if (!netdev_mc_empty(netdev) || netdev->flags & IFF_ALLMULTI)
1344 		new_ctrl |= nn->cap & NFP_NET_CFG_CTRL_L2MC;
1345 	else
1346 		new_ctrl &= ~NFP_NET_CFG_CTRL_L2MC;
1347 
1348 	if (netdev->flags & IFF_PROMISC) {
1349 		if (nn->cap & NFP_NET_CFG_CTRL_PROMISC)
1350 			new_ctrl |= NFP_NET_CFG_CTRL_PROMISC;
1351 		else
1352 			nn_warn(nn, "FW does not support promiscuous mode\n");
1353 	} else {
1354 		new_ctrl &= ~NFP_NET_CFG_CTRL_PROMISC;
1355 	}
1356 
1357 	if (new_ctrl == nn->dp.ctrl)
1358 		return;
1359 
1360 	nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
1361 	nfp_net_reconfig_post(nn, NFP_NET_CFG_UPDATE_GEN);
1362 
1363 	nn->dp.ctrl = new_ctrl;
1364 }
1365 
1366 static void nfp_net_rss_init_itbl(struct nfp_net *nn)
1367 {
1368 	int i;
1369 
1370 	for (i = 0; i < sizeof(nn->rss_itbl); i++)
1371 		nn->rss_itbl[i] =
1372 			ethtool_rxfh_indir_default(i, nn->dp.num_rx_rings);
1373 }
1374 
1375 static void nfp_net_dp_swap(struct nfp_net *nn, struct nfp_net_dp *dp)
1376 {
1377 	struct nfp_net_dp new_dp = *dp;
1378 
1379 	*dp = nn->dp;
1380 	nn->dp = new_dp;
1381 
1382 	nn->dp.netdev->mtu = new_dp.mtu;
1383 
1384 	if (!netif_is_rxfh_configured(nn->dp.netdev))
1385 		nfp_net_rss_init_itbl(nn);
1386 }
1387 
1388 static int nfp_net_dp_swap_enable(struct nfp_net *nn, struct nfp_net_dp *dp)
1389 {
1390 	unsigned int r;
1391 	int err;
1392 
1393 	nfp_net_dp_swap(nn, dp);
1394 
1395 	for (r = 0; r <	nn->max_r_vecs; r++)
1396 		nfp_net_vector_assign_rings(&nn->dp, &nn->r_vecs[r], r);
1397 
1398 	err = netif_set_real_num_queues(nn->dp.netdev,
1399 					nn->dp.num_stack_tx_rings,
1400 					nn->dp.num_rx_rings);
1401 	if (err)
1402 		return err;
1403 
1404 	return nfp_net_set_config_and_enable(nn);
1405 }
1406 
1407 struct nfp_net_dp *nfp_net_clone_dp(struct nfp_net *nn)
1408 {
1409 	struct nfp_net_dp *new;
1410 
1411 	new = kmalloc(sizeof(*new), GFP_KERNEL);
1412 	if (!new)
1413 		return NULL;
1414 
1415 	*new = nn->dp;
1416 
1417 	new->xsk_pools = kmemdup(new->xsk_pools,
1418 				 array_size(nn->max_r_vecs,
1419 					    sizeof(new->xsk_pools)),
1420 				 GFP_KERNEL);
1421 	if (!new->xsk_pools) {
1422 		kfree(new);
1423 		return NULL;
1424 	}
1425 
1426 	/* Clear things which need to be recomputed */
1427 	new->fl_bufsz = 0;
1428 	new->tx_rings = NULL;
1429 	new->rx_rings = NULL;
1430 	new->num_r_vecs = 0;
1431 	new->num_stack_tx_rings = 0;
1432 	new->txrwb = NULL;
1433 	new->txrwb_dma = 0;
1434 
1435 	return new;
1436 }
1437 
1438 static void nfp_net_free_dp(struct nfp_net_dp *dp)
1439 {
1440 	kfree(dp->xsk_pools);
1441 	kfree(dp);
1442 }
1443 
1444 static int
1445 nfp_net_check_config(struct nfp_net *nn, struct nfp_net_dp *dp,
1446 		     struct netlink_ext_ack *extack)
1447 {
1448 	unsigned int r, xsk_min_fl_bufsz;
1449 
1450 	/* XDP-enabled tests */
1451 	if (!dp->xdp_prog)
1452 		return 0;
1453 	if (dp->fl_bufsz > PAGE_SIZE) {
1454 		NL_SET_ERR_MSG_MOD(extack, "MTU too large w/ XDP enabled");
1455 		return -EINVAL;
1456 	}
1457 	if (dp->num_tx_rings > nn->max_tx_rings) {
1458 		NL_SET_ERR_MSG_MOD(extack, "Insufficient number of TX rings w/ XDP enabled");
1459 		return -EINVAL;
1460 	}
1461 
1462 	xsk_min_fl_bufsz = nfp_net_calc_fl_bufsz_xsk(dp);
1463 	for (r = 0; r < nn->max_r_vecs; r++) {
1464 		if (!dp->xsk_pools[r])
1465 			continue;
1466 
1467 		if (xsk_pool_get_rx_frame_size(dp->xsk_pools[r]) < xsk_min_fl_bufsz) {
1468 			NL_SET_ERR_MSG_MOD(extack,
1469 					   "XSK buffer pool chunk size too small");
1470 			return -EINVAL;
1471 		}
1472 	}
1473 
1474 	return 0;
1475 }
1476 
1477 int nfp_net_ring_reconfig(struct nfp_net *nn, struct nfp_net_dp *dp,
1478 			  struct netlink_ext_ack *extack)
1479 {
1480 	int r, err;
1481 
1482 	dp->fl_bufsz = nfp_net_calc_fl_bufsz(dp);
1483 
1484 	dp->num_stack_tx_rings = dp->num_tx_rings;
1485 	if (dp->xdp_prog)
1486 		dp->num_stack_tx_rings -= dp->num_rx_rings;
1487 
1488 	dp->num_r_vecs = max(dp->num_rx_rings, dp->num_stack_tx_rings);
1489 
1490 	err = nfp_net_check_config(nn, dp, extack);
1491 	if (err)
1492 		goto exit_free_dp;
1493 
1494 	if (!netif_running(dp->netdev)) {
1495 		nfp_net_dp_swap(nn, dp);
1496 		err = 0;
1497 		goto exit_free_dp;
1498 	}
1499 
1500 	/* Prepare new rings */
1501 	for (r = nn->dp.num_r_vecs; r < dp->num_r_vecs; r++) {
1502 		err = nfp_net_prepare_vector(nn, &nn->r_vecs[r], r);
1503 		if (err) {
1504 			dp->num_r_vecs = r;
1505 			goto err_cleanup_vecs;
1506 		}
1507 	}
1508 
1509 	err = nfp_net_rx_rings_prepare(nn, dp);
1510 	if (err)
1511 		goto err_cleanup_vecs;
1512 
1513 	err = nfp_net_tx_rings_prepare(nn, dp);
1514 	if (err)
1515 		goto err_free_rx;
1516 
1517 	/* Stop device, swap in new rings, try to start the firmware */
1518 	nfp_net_close_stack(nn);
1519 	nfp_net_clear_config_and_disable(nn);
1520 
1521 	err = nfp_net_dp_swap_enable(nn, dp);
1522 	if (err) {
1523 		int err2;
1524 
1525 		nfp_net_clear_config_and_disable(nn);
1526 
1527 		/* Try with old configuration and old rings */
1528 		err2 = nfp_net_dp_swap_enable(nn, dp);
1529 		if (err2)
1530 			nn_err(nn, "Can't restore ring config - FW communication failed (%d,%d)\n",
1531 			       err, err2);
1532 	}
1533 	for (r = dp->num_r_vecs - 1; r >= nn->dp.num_r_vecs; r--)
1534 		nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
1535 
1536 	nfp_net_rx_rings_free(dp);
1537 	nfp_net_tx_rings_free(dp);
1538 
1539 	nfp_net_open_stack(nn);
1540 exit_free_dp:
1541 	nfp_net_free_dp(dp);
1542 
1543 	return err;
1544 
1545 err_free_rx:
1546 	nfp_net_rx_rings_free(dp);
1547 err_cleanup_vecs:
1548 	for (r = dp->num_r_vecs - 1; r >= nn->dp.num_r_vecs; r--)
1549 		nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
1550 	nfp_net_free_dp(dp);
1551 	return err;
1552 }
1553 
1554 static int nfp_net_change_mtu(struct net_device *netdev, int new_mtu)
1555 {
1556 	struct nfp_net *nn = netdev_priv(netdev);
1557 	struct nfp_net_dp *dp;
1558 	int err;
1559 
1560 	err = nfp_app_check_mtu(nn->app, netdev, new_mtu);
1561 	if (err)
1562 		return err;
1563 
1564 	dp = nfp_net_clone_dp(nn);
1565 	if (!dp)
1566 		return -ENOMEM;
1567 
1568 	dp->mtu = new_mtu;
1569 
1570 	return nfp_net_ring_reconfig(nn, dp, NULL);
1571 }
1572 
1573 static int
1574 nfp_net_vlan_rx_add_vid(struct net_device *netdev, __be16 proto, u16 vid)
1575 {
1576 	const u32 cmd = NFP_NET_CFG_MBOX_CMD_CTAG_FILTER_ADD;
1577 	struct nfp_net *nn = netdev_priv(netdev);
1578 	int err;
1579 
1580 	/* Priority tagged packets with vlan id 0 are processed by the
1581 	 * NFP as untagged packets
1582 	 */
1583 	if (!vid)
1584 		return 0;
1585 
1586 	err = nfp_net_mbox_lock(nn, NFP_NET_CFG_VLAN_FILTER_SZ);
1587 	if (err)
1588 		return err;
1589 
1590 	nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_VLAN_FILTER_VID, vid);
1591 	nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_VLAN_FILTER_PROTO,
1592 		  ETH_P_8021Q);
1593 
1594 	return nfp_net_mbox_reconfig_and_unlock(nn, cmd);
1595 }
1596 
1597 static int
1598 nfp_net_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto, u16 vid)
1599 {
1600 	const u32 cmd = NFP_NET_CFG_MBOX_CMD_CTAG_FILTER_KILL;
1601 	struct nfp_net *nn = netdev_priv(netdev);
1602 	int err;
1603 
1604 	/* Priority tagged packets with vlan id 0 are processed by the
1605 	 * NFP as untagged packets
1606 	 */
1607 	if (!vid)
1608 		return 0;
1609 
1610 	err = nfp_net_mbox_lock(nn, NFP_NET_CFG_VLAN_FILTER_SZ);
1611 	if (err)
1612 		return err;
1613 
1614 	nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_VLAN_FILTER_VID, vid);
1615 	nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_VLAN_FILTER_PROTO,
1616 		  ETH_P_8021Q);
1617 
1618 	return nfp_net_mbox_reconfig_and_unlock(nn, cmd);
1619 }
1620 
1621 static void nfp_net_stat64(struct net_device *netdev,
1622 			   struct rtnl_link_stats64 *stats)
1623 {
1624 	struct nfp_net *nn = netdev_priv(netdev);
1625 	int r;
1626 
1627 	/* Collect software stats */
1628 	for (r = 0; r < nn->max_r_vecs; r++) {
1629 		struct nfp_net_r_vector *r_vec = &nn->r_vecs[r];
1630 		u64 data[3];
1631 		unsigned int start;
1632 
1633 		do {
1634 			start = u64_stats_fetch_begin(&r_vec->rx_sync);
1635 			data[0] = r_vec->rx_pkts;
1636 			data[1] = r_vec->rx_bytes;
1637 			data[2] = r_vec->rx_drops;
1638 		} while (u64_stats_fetch_retry(&r_vec->rx_sync, start));
1639 		stats->rx_packets += data[0];
1640 		stats->rx_bytes += data[1];
1641 		stats->rx_dropped += data[2];
1642 
1643 		do {
1644 			start = u64_stats_fetch_begin(&r_vec->tx_sync);
1645 			data[0] = r_vec->tx_pkts;
1646 			data[1] = r_vec->tx_bytes;
1647 			data[2] = r_vec->tx_errors;
1648 		} while (u64_stats_fetch_retry(&r_vec->tx_sync, start));
1649 		stats->tx_packets += data[0];
1650 		stats->tx_bytes += data[1];
1651 		stats->tx_errors += data[2];
1652 	}
1653 
1654 	/* Add in device stats */
1655 	stats->multicast += nn_readq(nn, NFP_NET_CFG_STATS_RX_MC_FRAMES);
1656 	stats->rx_dropped += nn_readq(nn, NFP_NET_CFG_STATS_RX_DISCARDS);
1657 	stats->rx_errors += nn_readq(nn, NFP_NET_CFG_STATS_RX_ERRORS);
1658 
1659 	stats->tx_dropped += nn_readq(nn, NFP_NET_CFG_STATS_TX_DISCARDS);
1660 	stats->tx_errors += nn_readq(nn, NFP_NET_CFG_STATS_TX_ERRORS);
1661 }
1662 
1663 static int nfp_net_set_features(struct net_device *netdev,
1664 				netdev_features_t features)
1665 {
1666 	netdev_features_t changed = netdev->features ^ features;
1667 	struct nfp_net *nn = netdev_priv(netdev);
1668 	u32 new_ctrl;
1669 	int err;
1670 
1671 	/* Assume this is not called with features we have not advertised */
1672 
1673 	new_ctrl = nn->dp.ctrl;
1674 
1675 	if (changed & NETIF_F_RXCSUM) {
1676 		if (features & NETIF_F_RXCSUM)
1677 			new_ctrl |= nn->cap & NFP_NET_CFG_CTRL_RXCSUM_ANY;
1678 		else
1679 			new_ctrl &= ~NFP_NET_CFG_CTRL_RXCSUM_ANY;
1680 	}
1681 
1682 	if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
1683 		if (features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))
1684 			new_ctrl |= NFP_NET_CFG_CTRL_TXCSUM;
1685 		else
1686 			new_ctrl &= ~NFP_NET_CFG_CTRL_TXCSUM;
1687 	}
1688 
1689 	if (changed & (NETIF_F_TSO | NETIF_F_TSO6)) {
1690 		if (features & (NETIF_F_TSO | NETIF_F_TSO6))
1691 			new_ctrl |= nn->cap & NFP_NET_CFG_CTRL_LSO2 ?:
1692 					      NFP_NET_CFG_CTRL_LSO;
1693 		else
1694 			new_ctrl &= ~NFP_NET_CFG_CTRL_LSO_ANY;
1695 	}
1696 
1697 	if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
1698 		if (features & NETIF_F_HW_VLAN_CTAG_RX)
1699 			new_ctrl |= nn->cap & NFP_NET_CFG_CTRL_RXVLAN_V2 ?:
1700 				    NFP_NET_CFG_CTRL_RXVLAN;
1701 		else
1702 			new_ctrl &= ~NFP_NET_CFG_CTRL_RXVLAN_ANY;
1703 	}
1704 
1705 	if (changed & NETIF_F_HW_VLAN_CTAG_TX) {
1706 		if (features & NETIF_F_HW_VLAN_CTAG_TX)
1707 			new_ctrl |= nn->cap & NFP_NET_CFG_CTRL_TXVLAN_V2 ?:
1708 				    NFP_NET_CFG_CTRL_TXVLAN;
1709 		else
1710 			new_ctrl &= ~NFP_NET_CFG_CTRL_TXVLAN_ANY;
1711 	}
1712 
1713 	if (changed & NETIF_F_HW_VLAN_CTAG_FILTER) {
1714 		if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
1715 			new_ctrl |= NFP_NET_CFG_CTRL_CTAG_FILTER;
1716 		else
1717 			new_ctrl &= ~NFP_NET_CFG_CTRL_CTAG_FILTER;
1718 	}
1719 
1720 	if (changed & NETIF_F_HW_VLAN_STAG_RX) {
1721 		if (features & NETIF_F_HW_VLAN_STAG_RX)
1722 			new_ctrl |= NFP_NET_CFG_CTRL_RXQINQ;
1723 		else
1724 			new_ctrl &= ~NFP_NET_CFG_CTRL_RXQINQ;
1725 	}
1726 
1727 	if (changed & NETIF_F_SG) {
1728 		if (features & NETIF_F_SG)
1729 			new_ctrl |= NFP_NET_CFG_CTRL_GATHER;
1730 		else
1731 			new_ctrl &= ~NFP_NET_CFG_CTRL_GATHER;
1732 	}
1733 
1734 	err = nfp_port_set_features(netdev, features);
1735 	if (err)
1736 		return err;
1737 
1738 	nn_dbg(nn, "Feature change 0x%llx -> 0x%llx (changed=0x%llx)\n",
1739 	       netdev->features, features, changed);
1740 
1741 	if (new_ctrl == nn->dp.ctrl)
1742 		return 0;
1743 
1744 	nn_dbg(nn, "NIC ctrl: 0x%x -> 0x%x\n", nn->dp.ctrl, new_ctrl);
1745 	nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
1746 	err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_GEN);
1747 	if (err)
1748 		return err;
1749 
1750 	nn->dp.ctrl = new_ctrl;
1751 
1752 	return 0;
1753 }
1754 
1755 static netdev_features_t
1756 nfp_net_fix_features(struct net_device *netdev,
1757 		     netdev_features_t features)
1758 {
1759 	if ((features & NETIF_F_HW_VLAN_CTAG_RX) &&
1760 	    (features & NETIF_F_HW_VLAN_STAG_RX)) {
1761 		if (netdev->features & NETIF_F_HW_VLAN_CTAG_RX) {
1762 			features &= ~NETIF_F_HW_VLAN_CTAG_RX;
1763 			netdev->wanted_features &= ~NETIF_F_HW_VLAN_CTAG_RX;
1764 			netdev_warn(netdev,
1765 				    "S-tag and C-tag stripping can't be enabled at the same time. Enabling S-tag stripping and disabling C-tag stripping\n");
1766 		} else if (netdev->features & NETIF_F_HW_VLAN_STAG_RX) {
1767 			features &= ~NETIF_F_HW_VLAN_STAG_RX;
1768 			netdev->wanted_features &= ~NETIF_F_HW_VLAN_STAG_RX;
1769 			netdev_warn(netdev,
1770 				    "S-tag and C-tag stripping can't be enabled at the same time. Enabling C-tag stripping and disabling S-tag stripping\n");
1771 		}
1772 	}
1773 	return features;
1774 }
1775 
1776 static netdev_features_t
1777 nfp_net_features_check(struct sk_buff *skb, struct net_device *dev,
1778 		       netdev_features_t features)
1779 {
1780 	u8 l4_hdr;
1781 
1782 	/* We can't do TSO over double tagged packets (802.1AD) */
1783 	features &= vlan_features_check(skb, features);
1784 
1785 	if (!skb->encapsulation)
1786 		return features;
1787 
1788 	/* Ensure that inner L4 header offset fits into TX descriptor field */
1789 	if (skb_is_gso(skb)) {
1790 		u32 hdrlen;
1791 
1792 		hdrlen = skb_inner_tcp_all_headers(skb);
1793 
1794 		/* Assume worst case scenario of having longest possible
1795 		 * metadata prepend - 8B
1796 		 */
1797 		if (unlikely(hdrlen > NFP_NET_LSO_MAX_HDR_SZ - 8))
1798 			features &= ~NETIF_F_GSO_MASK;
1799 	}
1800 
1801 	/* VXLAN/GRE check */
1802 	switch (vlan_get_protocol(skb)) {
1803 	case htons(ETH_P_IP):
1804 		l4_hdr = ip_hdr(skb)->protocol;
1805 		break;
1806 	case htons(ETH_P_IPV6):
1807 		l4_hdr = ipv6_hdr(skb)->nexthdr;
1808 		break;
1809 	default:
1810 		return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
1811 	}
1812 
1813 	if (skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
1814 	    skb->inner_protocol != htons(ETH_P_TEB) ||
1815 	    (l4_hdr != IPPROTO_UDP && l4_hdr != IPPROTO_GRE) ||
1816 	    (l4_hdr == IPPROTO_UDP &&
1817 	     (skb_inner_mac_header(skb) - skb_transport_header(skb) !=
1818 	      sizeof(struct udphdr) + sizeof(struct vxlanhdr))))
1819 		return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
1820 
1821 	return features;
1822 }
1823 
1824 static int
1825 nfp_net_get_phys_port_name(struct net_device *netdev, char *name, size_t len)
1826 {
1827 	struct nfp_net *nn = netdev_priv(netdev);
1828 	int n;
1829 
1830 	/* If port is defined, devlink_port is registered and devlink core
1831 	 * is taking care of name formatting.
1832 	 */
1833 	if (nn->port)
1834 		return -EOPNOTSUPP;
1835 
1836 	if (nn->dp.is_vf || nn->vnic_no_name)
1837 		return -EOPNOTSUPP;
1838 
1839 	n = snprintf(name, len, "n%d", nn->id);
1840 	if (n >= len)
1841 		return -EINVAL;
1842 
1843 	return 0;
1844 }
1845 
1846 static int nfp_net_xdp_setup_drv(struct nfp_net *nn, struct netdev_bpf *bpf)
1847 {
1848 	struct bpf_prog *prog = bpf->prog;
1849 	struct nfp_net_dp *dp;
1850 	int err;
1851 
1852 	if (!prog == !nn->dp.xdp_prog) {
1853 		WRITE_ONCE(nn->dp.xdp_prog, prog);
1854 		xdp_attachment_setup(&nn->xdp, bpf);
1855 		return 0;
1856 	}
1857 
1858 	dp = nfp_net_clone_dp(nn);
1859 	if (!dp)
1860 		return -ENOMEM;
1861 
1862 	dp->xdp_prog = prog;
1863 	dp->num_tx_rings += prog ? nn->dp.num_rx_rings : -nn->dp.num_rx_rings;
1864 	dp->rx_dma_dir = prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
1865 	dp->rx_dma_off = prog ? XDP_PACKET_HEADROOM - nn->dp.rx_offset : 0;
1866 
1867 	/* We need RX reconfig to remap the buffers (BIDIR vs FROM_DEV) */
1868 	err = nfp_net_ring_reconfig(nn, dp, bpf->extack);
1869 	if (err)
1870 		return err;
1871 
1872 	xdp_attachment_setup(&nn->xdp, bpf);
1873 	return 0;
1874 }
1875 
1876 static int nfp_net_xdp_setup_hw(struct nfp_net *nn, struct netdev_bpf *bpf)
1877 {
1878 	int err;
1879 
1880 	err = nfp_app_xdp_offload(nn->app, nn, bpf->prog, bpf->extack);
1881 	if (err)
1882 		return err;
1883 
1884 	xdp_attachment_setup(&nn->xdp_hw, bpf);
1885 	return 0;
1886 }
1887 
1888 static int nfp_net_xdp(struct net_device *netdev, struct netdev_bpf *xdp)
1889 {
1890 	struct nfp_net *nn = netdev_priv(netdev);
1891 
1892 	switch (xdp->command) {
1893 	case XDP_SETUP_PROG:
1894 		return nfp_net_xdp_setup_drv(nn, xdp);
1895 	case XDP_SETUP_PROG_HW:
1896 		return nfp_net_xdp_setup_hw(nn, xdp);
1897 	case XDP_SETUP_XSK_POOL:
1898 		return nfp_net_xsk_setup_pool(netdev, xdp->xsk.pool,
1899 					      xdp->xsk.queue_id);
1900 	default:
1901 		return nfp_app_bpf(nn->app, nn, xdp);
1902 	}
1903 }
1904 
1905 static int nfp_net_set_mac_address(struct net_device *netdev, void *addr)
1906 {
1907 	struct nfp_net *nn = netdev_priv(netdev);
1908 	struct sockaddr *saddr = addr;
1909 	int err;
1910 
1911 	err = eth_prepare_mac_addr_change(netdev, addr);
1912 	if (err)
1913 		return err;
1914 
1915 	nfp_net_write_mac_addr(nn, saddr->sa_data);
1916 
1917 	err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_MACADDR);
1918 	if (err)
1919 		return err;
1920 
1921 	eth_commit_mac_addr_change(netdev, addr);
1922 
1923 	return 0;
1924 }
1925 
1926 static int nfp_net_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
1927 				  struct net_device *dev, u32 filter_mask,
1928 				  int nlflags)
1929 {
1930 	struct nfp_net *nn = netdev_priv(dev);
1931 	u16 mode;
1932 
1933 	if (!(nn->cap & NFP_NET_CFG_CTRL_VEPA))
1934 		return -EOPNOTSUPP;
1935 
1936 	mode = (nn->dp.ctrl & NFP_NET_CFG_CTRL_VEPA) ?
1937 	       BRIDGE_MODE_VEPA : BRIDGE_MODE_VEB;
1938 
1939 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, mode, 0, 0,
1940 				       nlflags, filter_mask, NULL);
1941 }
1942 
1943 static int nfp_net_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
1944 				  u16 flags, struct netlink_ext_ack *extack)
1945 {
1946 	struct nfp_net *nn = netdev_priv(dev);
1947 	struct nlattr *attr, *br_spec;
1948 	int rem, err;
1949 	u32 new_ctrl;
1950 	u16 mode;
1951 
1952 	if (!(nn->cap & NFP_NET_CFG_CTRL_VEPA))
1953 		return -EOPNOTSUPP;
1954 
1955 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
1956 	if (!br_spec)
1957 		return -EINVAL;
1958 
1959 	nla_for_each_nested(attr, br_spec, rem) {
1960 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
1961 			continue;
1962 
1963 		if (nla_len(attr) < sizeof(mode))
1964 			return -EINVAL;
1965 
1966 		new_ctrl = nn->dp.ctrl;
1967 		mode = nla_get_u16(attr);
1968 		if (mode == BRIDGE_MODE_VEPA)
1969 			new_ctrl |= NFP_NET_CFG_CTRL_VEPA;
1970 		else if (mode == BRIDGE_MODE_VEB)
1971 			new_ctrl &= ~NFP_NET_CFG_CTRL_VEPA;
1972 		else
1973 			return -EOPNOTSUPP;
1974 
1975 		if (new_ctrl == nn->dp.ctrl)
1976 			return 0;
1977 
1978 		nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
1979 		err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_GEN);
1980 		if (!err)
1981 			nn->dp.ctrl = new_ctrl;
1982 
1983 		return err;
1984 	}
1985 
1986 	return -EINVAL;
1987 }
1988 
1989 const struct net_device_ops nfp_nfd3_netdev_ops = {
1990 	.ndo_init		= nfp_app_ndo_init,
1991 	.ndo_uninit		= nfp_app_ndo_uninit,
1992 	.ndo_open		= nfp_net_netdev_open,
1993 	.ndo_stop		= nfp_net_netdev_close,
1994 	.ndo_start_xmit		= nfp_net_tx,
1995 	.ndo_get_stats64	= nfp_net_stat64,
1996 	.ndo_vlan_rx_add_vid	= nfp_net_vlan_rx_add_vid,
1997 	.ndo_vlan_rx_kill_vid	= nfp_net_vlan_rx_kill_vid,
1998 	.ndo_set_vf_mac         = nfp_app_set_vf_mac,
1999 	.ndo_set_vf_vlan        = nfp_app_set_vf_vlan,
2000 	.ndo_set_vf_rate	= nfp_app_set_vf_rate,
2001 	.ndo_set_vf_spoofchk    = nfp_app_set_vf_spoofchk,
2002 	.ndo_set_vf_trust	= nfp_app_set_vf_trust,
2003 	.ndo_get_vf_config	= nfp_app_get_vf_config,
2004 	.ndo_set_vf_link_state  = nfp_app_set_vf_link_state,
2005 	.ndo_setup_tc		= nfp_port_setup_tc,
2006 	.ndo_tx_timeout		= nfp_net_tx_timeout,
2007 	.ndo_set_rx_mode	= nfp_net_set_rx_mode,
2008 	.ndo_change_mtu		= nfp_net_change_mtu,
2009 	.ndo_set_mac_address	= nfp_net_set_mac_address,
2010 	.ndo_set_features	= nfp_net_set_features,
2011 	.ndo_fix_features	= nfp_net_fix_features,
2012 	.ndo_features_check	= nfp_net_features_check,
2013 	.ndo_get_phys_port_name	= nfp_net_get_phys_port_name,
2014 	.ndo_bpf		= nfp_net_xdp,
2015 	.ndo_xsk_wakeup		= nfp_net_xsk_wakeup,
2016 	.ndo_bridge_getlink     = nfp_net_bridge_getlink,
2017 	.ndo_bridge_setlink     = nfp_net_bridge_setlink,
2018 };
2019 
2020 const struct net_device_ops nfp_nfdk_netdev_ops = {
2021 	.ndo_init		= nfp_app_ndo_init,
2022 	.ndo_uninit		= nfp_app_ndo_uninit,
2023 	.ndo_open		= nfp_net_netdev_open,
2024 	.ndo_stop		= nfp_net_netdev_close,
2025 	.ndo_start_xmit		= nfp_net_tx,
2026 	.ndo_get_stats64	= nfp_net_stat64,
2027 	.ndo_vlan_rx_add_vid	= nfp_net_vlan_rx_add_vid,
2028 	.ndo_vlan_rx_kill_vid	= nfp_net_vlan_rx_kill_vid,
2029 	.ndo_set_vf_mac         = nfp_app_set_vf_mac,
2030 	.ndo_set_vf_vlan        = nfp_app_set_vf_vlan,
2031 	.ndo_set_vf_rate	= nfp_app_set_vf_rate,
2032 	.ndo_set_vf_spoofchk    = nfp_app_set_vf_spoofchk,
2033 	.ndo_set_vf_trust	= nfp_app_set_vf_trust,
2034 	.ndo_get_vf_config	= nfp_app_get_vf_config,
2035 	.ndo_set_vf_link_state  = nfp_app_set_vf_link_state,
2036 	.ndo_setup_tc		= nfp_port_setup_tc,
2037 	.ndo_tx_timeout		= nfp_net_tx_timeout,
2038 	.ndo_set_rx_mode	= nfp_net_set_rx_mode,
2039 	.ndo_change_mtu		= nfp_net_change_mtu,
2040 	.ndo_set_mac_address	= nfp_net_set_mac_address,
2041 	.ndo_set_features	= nfp_net_set_features,
2042 	.ndo_fix_features	= nfp_net_fix_features,
2043 	.ndo_features_check	= nfp_net_features_check,
2044 	.ndo_get_phys_port_name	= nfp_net_get_phys_port_name,
2045 	.ndo_bpf		= nfp_net_xdp,
2046 	.ndo_bridge_getlink     = nfp_net_bridge_getlink,
2047 	.ndo_bridge_setlink     = nfp_net_bridge_setlink,
2048 };
2049 
2050 static int nfp_udp_tunnel_sync(struct net_device *netdev, unsigned int table)
2051 {
2052 	struct nfp_net *nn = netdev_priv(netdev);
2053 	int i;
2054 
2055 	BUILD_BUG_ON(NFP_NET_N_VXLAN_PORTS & 1);
2056 	for (i = 0; i < NFP_NET_N_VXLAN_PORTS; i += 2) {
2057 		struct udp_tunnel_info ti0, ti1;
2058 
2059 		udp_tunnel_nic_get_port(netdev, table, i, &ti0);
2060 		udp_tunnel_nic_get_port(netdev, table, i + 1, &ti1);
2061 
2062 		nn_writel(nn, NFP_NET_CFG_VXLAN_PORT + i * sizeof(ti0.port),
2063 			  be16_to_cpu(ti1.port) << 16 | be16_to_cpu(ti0.port));
2064 	}
2065 
2066 	return nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_VXLAN);
2067 }
2068 
2069 static const struct udp_tunnel_nic_info nfp_udp_tunnels = {
2070 	.sync_table     = nfp_udp_tunnel_sync,
2071 	.flags          = UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
2072 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
2073 	.tables         = {
2074 		{
2075 			.n_entries      = NFP_NET_N_VXLAN_PORTS,
2076 			.tunnel_types   = UDP_TUNNEL_TYPE_VXLAN,
2077 		},
2078 	},
2079 };
2080 
2081 /**
2082  * nfp_net_info() - Print general info about the NIC
2083  * @nn:      NFP Net device to reconfigure
2084  */
2085 void nfp_net_info(struct nfp_net *nn)
2086 {
2087 	nn_info(nn, "NFP-6xxx %sNetdev: TxQs=%d/%d RxQs=%d/%d\n",
2088 		nn->dp.is_vf ? "VF " : "",
2089 		nn->dp.num_tx_rings, nn->max_tx_rings,
2090 		nn->dp.num_rx_rings, nn->max_rx_rings);
2091 	nn_info(nn, "VER: %d.%d.%d.%d, Maximum supported MTU: %d\n",
2092 		nn->fw_ver.extend, nn->fw_ver.class,
2093 		nn->fw_ver.major, nn->fw_ver.minor,
2094 		nn->max_mtu);
2095 	nn_info(nn, "CAP: %#x %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
2096 		nn->cap,
2097 		nn->cap & NFP_NET_CFG_CTRL_PROMISC  ? "PROMISC "  : "",
2098 		nn->cap & NFP_NET_CFG_CTRL_L2BC     ? "L2BCFILT " : "",
2099 		nn->cap & NFP_NET_CFG_CTRL_L2MC     ? "L2MCFILT " : "",
2100 		nn->cap & NFP_NET_CFG_CTRL_RXCSUM   ? "RXCSUM "   : "",
2101 		nn->cap & NFP_NET_CFG_CTRL_TXCSUM   ? "TXCSUM "   : "",
2102 		nn->cap & NFP_NET_CFG_CTRL_RXVLAN   ? "RXVLAN "   : "",
2103 		nn->cap & NFP_NET_CFG_CTRL_TXVLAN   ? "TXVLAN "   : "",
2104 		nn->cap & NFP_NET_CFG_CTRL_RXQINQ   ? "RXQINQ "   : "",
2105 		nn->cap & NFP_NET_CFG_CTRL_RXVLAN_V2 ? "RXVLANv2 "   : "",
2106 		nn->cap & NFP_NET_CFG_CTRL_TXVLAN_V2   ? "TXVLANv2 "   : "",
2107 		nn->cap & NFP_NET_CFG_CTRL_SCATTER  ? "SCATTER "  : "",
2108 		nn->cap & NFP_NET_CFG_CTRL_GATHER   ? "GATHER "   : "",
2109 		nn->cap & NFP_NET_CFG_CTRL_LSO      ? "TSO1 "     : "",
2110 		nn->cap & NFP_NET_CFG_CTRL_LSO2     ? "TSO2 "     : "",
2111 		nn->cap & NFP_NET_CFG_CTRL_RSS      ? "RSS1 "     : "",
2112 		nn->cap & NFP_NET_CFG_CTRL_RSS2     ? "RSS2 "     : "",
2113 		nn->cap & NFP_NET_CFG_CTRL_CTAG_FILTER ? "CTAG_FILTER " : "",
2114 		nn->cap & NFP_NET_CFG_CTRL_MSIXAUTO ? "AUTOMASK " : "",
2115 		nn->cap & NFP_NET_CFG_CTRL_IRQMOD   ? "IRQMOD "   : "",
2116 		nn->cap & NFP_NET_CFG_CTRL_TXRWB    ? "TXRWB "    : "",
2117 		nn->cap & NFP_NET_CFG_CTRL_VEPA     ? "VEPA "     : "",
2118 		nn->cap & NFP_NET_CFG_CTRL_VXLAN    ? "VXLAN "    : "",
2119 		nn->cap & NFP_NET_CFG_CTRL_NVGRE    ? "NVGRE "	  : "",
2120 		nn->cap & NFP_NET_CFG_CTRL_CSUM_COMPLETE ?
2121 						      "RXCSUM_COMPLETE " : "",
2122 		nn->cap & NFP_NET_CFG_CTRL_LIVE_ADDR ? "LIVE_ADDR " : "",
2123 		nfp_app_extra_cap(nn->app, nn));
2124 }
2125 
2126 /**
2127  * nfp_net_alloc() - Allocate netdev and related structure
2128  * @pdev:         PCI device
2129  * @dev_info:     NFP ASIC params
2130  * @ctrl_bar:     PCI IOMEM with vNIC config memory
2131  * @needs_netdev: Whether to allocate a netdev for this vNIC
2132  * @max_tx_rings: Maximum number of TX rings supported by device
2133  * @max_rx_rings: Maximum number of RX rings supported by device
2134  *
2135  * This function allocates a netdev device and fills in the initial
2136  * part of the @struct nfp_net structure.  In case of control device
2137  * nfp_net structure is allocated without the netdev.
2138  *
2139  * Return: NFP Net device structure, or ERR_PTR on error.
2140  */
2141 struct nfp_net *
2142 nfp_net_alloc(struct pci_dev *pdev, const struct nfp_dev_info *dev_info,
2143 	      void __iomem *ctrl_bar, bool needs_netdev,
2144 	      unsigned int max_tx_rings, unsigned int max_rx_rings)
2145 {
2146 	u64 dma_mask = dma_get_mask(&pdev->dev);
2147 	struct nfp_net *nn;
2148 	int err;
2149 
2150 	if (needs_netdev) {
2151 		struct net_device *netdev;
2152 
2153 		netdev = alloc_etherdev_mqs(sizeof(struct nfp_net),
2154 					    max_tx_rings, max_rx_rings);
2155 		if (!netdev)
2156 			return ERR_PTR(-ENOMEM);
2157 
2158 		SET_NETDEV_DEV(netdev, &pdev->dev);
2159 		nn = netdev_priv(netdev);
2160 		nn->dp.netdev = netdev;
2161 	} else {
2162 		nn = vzalloc(sizeof(*nn));
2163 		if (!nn)
2164 			return ERR_PTR(-ENOMEM);
2165 	}
2166 
2167 	nn->dp.dev = &pdev->dev;
2168 	nn->dp.ctrl_bar = ctrl_bar;
2169 	nn->dev_info = dev_info;
2170 	nn->pdev = pdev;
2171 	nfp_net_get_fw_version(&nn->fw_ver, ctrl_bar);
2172 
2173 	switch (FIELD_GET(NFP_NET_CFG_VERSION_DP_MASK, nn->fw_ver.extend)) {
2174 	case NFP_NET_CFG_VERSION_DP_NFD3:
2175 		nn->dp.ops = &nfp_nfd3_ops;
2176 		break;
2177 	case NFP_NET_CFG_VERSION_DP_NFDK:
2178 		if (nn->fw_ver.major < 5) {
2179 			dev_err(&pdev->dev,
2180 				"NFDK must use ABI 5 or newer, found: %d\n",
2181 				nn->fw_ver.major);
2182 			err = -EINVAL;
2183 			goto err_free_nn;
2184 		}
2185 		nn->dp.ops = &nfp_nfdk_ops;
2186 		break;
2187 	default:
2188 		err = -EINVAL;
2189 		goto err_free_nn;
2190 	}
2191 
2192 	if ((dma_mask & nn->dp.ops->dma_mask) != dma_mask) {
2193 		dev_err(&pdev->dev,
2194 			"DMA mask of loaded firmware: %llx, required DMA mask: %llx\n",
2195 			nn->dp.ops->dma_mask, dma_mask);
2196 		err = -EINVAL;
2197 		goto err_free_nn;
2198 	}
2199 
2200 	nn->max_tx_rings = max_tx_rings;
2201 	nn->max_rx_rings = max_rx_rings;
2202 
2203 	nn->dp.num_tx_rings = min_t(unsigned int,
2204 				    max_tx_rings, num_online_cpus());
2205 	nn->dp.num_rx_rings = min_t(unsigned int, max_rx_rings,
2206 				 netif_get_num_default_rss_queues());
2207 
2208 	nn->dp.num_r_vecs = max(nn->dp.num_tx_rings, nn->dp.num_rx_rings);
2209 	nn->dp.num_r_vecs = min_t(unsigned int,
2210 				  nn->dp.num_r_vecs, num_online_cpus());
2211 	nn->max_r_vecs = nn->dp.num_r_vecs;
2212 
2213 	nn->dp.xsk_pools = kcalloc(nn->max_r_vecs, sizeof(nn->dp.xsk_pools),
2214 				   GFP_KERNEL);
2215 	if (!nn->dp.xsk_pools) {
2216 		err = -ENOMEM;
2217 		goto err_free_nn;
2218 	}
2219 
2220 	nn->dp.txd_cnt = NFP_NET_TX_DESCS_DEFAULT;
2221 	nn->dp.rxd_cnt = NFP_NET_RX_DESCS_DEFAULT;
2222 
2223 	sema_init(&nn->bar_lock, 1);
2224 
2225 	spin_lock_init(&nn->reconfig_lock);
2226 	spin_lock_init(&nn->link_status_lock);
2227 
2228 	timer_setup(&nn->reconfig_timer, nfp_net_reconfig_timer, 0);
2229 
2230 	err = nfp_net_tlv_caps_parse(&nn->pdev->dev, nn->dp.ctrl_bar,
2231 				     &nn->tlv_caps);
2232 	if (err)
2233 		goto err_free_nn;
2234 
2235 	err = nfp_ccm_mbox_alloc(nn);
2236 	if (err)
2237 		goto err_free_nn;
2238 
2239 	return nn;
2240 
2241 err_free_nn:
2242 	if (nn->dp.netdev)
2243 		free_netdev(nn->dp.netdev);
2244 	else
2245 		vfree(nn);
2246 	return ERR_PTR(err);
2247 }
2248 
2249 /**
2250  * nfp_net_free() - Undo what @nfp_net_alloc() did
2251  * @nn:      NFP Net device to reconfigure
2252  */
2253 void nfp_net_free(struct nfp_net *nn)
2254 {
2255 	WARN_ON(timer_pending(&nn->reconfig_timer) || nn->reconfig_posted);
2256 	nfp_ccm_mbox_free(nn);
2257 
2258 	kfree(nn->dp.xsk_pools);
2259 	if (nn->dp.netdev)
2260 		free_netdev(nn->dp.netdev);
2261 	else
2262 		vfree(nn);
2263 }
2264 
2265 /**
2266  * nfp_net_rss_key_sz() - Get current size of the RSS key
2267  * @nn:		NFP Net device instance
2268  *
2269  * Return: size of the RSS key for currently selected hash function.
2270  */
2271 unsigned int nfp_net_rss_key_sz(struct nfp_net *nn)
2272 {
2273 	switch (nn->rss_hfunc) {
2274 	case ETH_RSS_HASH_TOP:
2275 		return NFP_NET_CFG_RSS_KEY_SZ;
2276 	case ETH_RSS_HASH_XOR:
2277 		return 0;
2278 	case ETH_RSS_HASH_CRC32:
2279 		return 4;
2280 	}
2281 
2282 	nn_warn(nn, "Unknown hash function: %u\n", nn->rss_hfunc);
2283 	return 0;
2284 }
2285 
2286 /**
2287  * nfp_net_rss_init() - Set the initial RSS parameters
2288  * @nn:	     NFP Net device to reconfigure
2289  */
2290 static void nfp_net_rss_init(struct nfp_net *nn)
2291 {
2292 	unsigned long func_bit, rss_cap_hfunc;
2293 	u32 reg;
2294 
2295 	/* Read the RSS function capability and select first supported func */
2296 	reg = nn_readl(nn, NFP_NET_CFG_RSS_CAP);
2297 	rss_cap_hfunc =	FIELD_GET(NFP_NET_CFG_RSS_CAP_HFUNC, reg);
2298 	if (!rss_cap_hfunc)
2299 		rss_cap_hfunc =	FIELD_GET(NFP_NET_CFG_RSS_CAP_HFUNC,
2300 					  NFP_NET_CFG_RSS_TOEPLITZ);
2301 
2302 	func_bit = find_first_bit(&rss_cap_hfunc, NFP_NET_CFG_RSS_HFUNCS);
2303 	if (func_bit == NFP_NET_CFG_RSS_HFUNCS) {
2304 		dev_warn(nn->dp.dev,
2305 			 "Bad RSS config, defaulting to Toeplitz hash\n");
2306 		func_bit = ETH_RSS_HASH_TOP_BIT;
2307 	}
2308 	nn->rss_hfunc = 1 << func_bit;
2309 
2310 	netdev_rss_key_fill(nn->rss_key, nfp_net_rss_key_sz(nn));
2311 
2312 	nfp_net_rss_init_itbl(nn);
2313 
2314 	/* Enable IPv4/IPv6 TCP by default */
2315 	nn->rss_cfg = NFP_NET_CFG_RSS_IPV4_TCP |
2316 		      NFP_NET_CFG_RSS_IPV6_TCP |
2317 		      FIELD_PREP(NFP_NET_CFG_RSS_HFUNC, nn->rss_hfunc) |
2318 		      NFP_NET_CFG_RSS_MASK;
2319 }
2320 
2321 /**
2322  * nfp_net_irqmod_init() - Set the initial IRQ moderation parameters
2323  * @nn:	     NFP Net device to reconfigure
2324  */
2325 static void nfp_net_irqmod_init(struct nfp_net *nn)
2326 {
2327 	nn->rx_coalesce_usecs      = 50;
2328 	nn->rx_coalesce_max_frames = 64;
2329 	nn->tx_coalesce_usecs      = 50;
2330 	nn->tx_coalesce_max_frames = 64;
2331 
2332 	nn->rx_coalesce_adapt_on   = true;
2333 	nn->tx_coalesce_adapt_on   = true;
2334 }
2335 
2336 static void nfp_net_netdev_init(struct nfp_net *nn)
2337 {
2338 	struct net_device *netdev = nn->dp.netdev;
2339 
2340 	nfp_net_write_mac_addr(nn, nn->dp.netdev->dev_addr);
2341 
2342 	netdev->mtu = nn->dp.mtu;
2343 
2344 	/* Advertise/enable offloads based on capabilities
2345 	 *
2346 	 * Note: netdev->features show the currently enabled features
2347 	 * and netdev->hw_features advertises which features are
2348 	 * supported.  By default we enable most features.
2349 	 */
2350 	if (nn->cap & NFP_NET_CFG_CTRL_LIVE_ADDR)
2351 		netdev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2352 
2353 	netdev->hw_features = NETIF_F_HIGHDMA;
2354 	if (nn->cap & NFP_NET_CFG_CTRL_RXCSUM_ANY) {
2355 		netdev->hw_features |= NETIF_F_RXCSUM;
2356 		nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_RXCSUM_ANY;
2357 	}
2358 	if (nn->cap & NFP_NET_CFG_CTRL_TXCSUM) {
2359 		netdev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
2360 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_TXCSUM;
2361 	}
2362 	if (nn->cap & NFP_NET_CFG_CTRL_GATHER) {
2363 		netdev->hw_features |= NETIF_F_SG;
2364 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_GATHER;
2365 	}
2366 	if ((nn->cap & NFP_NET_CFG_CTRL_LSO && nn->fw_ver.major > 2) ||
2367 	    nn->cap & NFP_NET_CFG_CTRL_LSO2) {
2368 		netdev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
2369 		nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_LSO2 ?:
2370 					 NFP_NET_CFG_CTRL_LSO;
2371 	}
2372 	if (nn->cap & NFP_NET_CFG_CTRL_RSS_ANY)
2373 		netdev->hw_features |= NETIF_F_RXHASH;
2374 
2375 #ifdef CONFIG_NFP_NET_IPSEC
2376 	if (nn->cap_w1 & NFP_NET_CFG_CTRL_IPSEC)
2377 		netdev->hw_features |= NETIF_F_HW_ESP | NETIF_F_HW_ESP_TX_CSUM;
2378 #endif
2379 
2380 	if (nn->cap & NFP_NET_CFG_CTRL_VXLAN) {
2381 		if (nn->cap & NFP_NET_CFG_CTRL_LSO) {
2382 			netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL |
2383 					       NETIF_F_GSO_UDP_TUNNEL_CSUM |
2384 					       NETIF_F_GSO_PARTIAL;
2385 			netdev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM;
2386 		}
2387 		netdev->udp_tunnel_nic_info = &nfp_udp_tunnels;
2388 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_VXLAN;
2389 	}
2390 	if (nn->cap & NFP_NET_CFG_CTRL_NVGRE) {
2391 		if (nn->cap & NFP_NET_CFG_CTRL_LSO)
2392 			netdev->hw_features |= NETIF_F_GSO_GRE;
2393 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_NVGRE;
2394 	}
2395 	if (nn->cap & (NFP_NET_CFG_CTRL_VXLAN | NFP_NET_CFG_CTRL_NVGRE))
2396 		netdev->hw_enc_features = netdev->hw_features;
2397 
2398 	netdev->vlan_features = netdev->hw_features;
2399 
2400 	if (nn->cap & NFP_NET_CFG_CTRL_RXVLAN_ANY) {
2401 		netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
2402 		nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_RXVLAN_V2 ?:
2403 			       NFP_NET_CFG_CTRL_RXVLAN;
2404 	}
2405 	if (nn->cap & NFP_NET_CFG_CTRL_TXVLAN_ANY) {
2406 		if (nn->cap & NFP_NET_CFG_CTRL_LSO2) {
2407 			nn_warn(nn, "Device advertises both TSO2 and TXVLAN. Refusing to enable TXVLAN.\n");
2408 		} else {
2409 			netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
2410 			nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_TXVLAN_V2 ?:
2411 				       NFP_NET_CFG_CTRL_TXVLAN;
2412 		}
2413 	}
2414 	if (nn->cap & NFP_NET_CFG_CTRL_CTAG_FILTER) {
2415 		netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
2416 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_CTAG_FILTER;
2417 	}
2418 	if (nn->cap & NFP_NET_CFG_CTRL_RXQINQ) {
2419 		netdev->hw_features |= NETIF_F_HW_VLAN_STAG_RX;
2420 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_RXQINQ;
2421 	}
2422 
2423 	netdev->features = netdev->hw_features;
2424 
2425 	if (nfp_app_has_tc(nn->app) && nn->port)
2426 		netdev->hw_features |= NETIF_F_HW_TC;
2427 
2428 	/* C-Tag strip and S-Tag strip can't be supported simultaneously,
2429 	 * so enable C-Tag strip and disable S-Tag strip by default.
2430 	 */
2431 	netdev->features &= ~NETIF_F_HW_VLAN_STAG_RX;
2432 	nn->dp.ctrl &= ~NFP_NET_CFG_CTRL_RXQINQ;
2433 
2434 	/* Finalise the netdev setup */
2435 	switch (nn->dp.ops->version) {
2436 	case NFP_NFD_VER_NFD3:
2437 		netdev->netdev_ops = &nfp_nfd3_netdev_ops;
2438 		break;
2439 	case NFP_NFD_VER_NFDK:
2440 		netdev->netdev_ops = &nfp_nfdk_netdev_ops;
2441 		break;
2442 	}
2443 
2444 	netdev->watchdog_timeo = msecs_to_jiffies(5 * 1000);
2445 
2446 	/* MTU range: 68 - hw-specific max */
2447 	netdev->min_mtu = ETH_MIN_MTU;
2448 	netdev->max_mtu = nn->max_mtu;
2449 
2450 	netif_set_tso_max_segs(netdev, NFP_NET_LSO_MAX_SEGS);
2451 
2452 	netif_carrier_off(netdev);
2453 
2454 	nfp_net_set_ethtool_ops(netdev);
2455 }
2456 
2457 static int nfp_net_read_caps(struct nfp_net *nn)
2458 {
2459 	/* Get some of the read-only fields from the BAR */
2460 	nn->cap = nn_readl(nn, NFP_NET_CFG_CAP);
2461 	nn->cap_w1 = nn_readq(nn, NFP_NET_CFG_CAP_WORD1);
2462 	nn->max_mtu = nn_readl(nn, NFP_NET_CFG_MAX_MTU);
2463 
2464 	/* ABI 4.x and ctrl vNIC always use chained metadata, in other cases
2465 	 * we allow use of non-chained metadata if RSS(v1) is the only
2466 	 * advertised capability requiring metadata.
2467 	 */
2468 	nn->dp.chained_metadata_format = nn->fw_ver.major == 4 ||
2469 					 !nn->dp.netdev ||
2470 					 !(nn->cap & NFP_NET_CFG_CTRL_RSS) ||
2471 					 nn->cap & NFP_NET_CFG_CTRL_CHAIN_META;
2472 	/* RSS(v1) uses non-chained metadata format, except in ABI 4.x where
2473 	 * it has the same meaning as RSSv2.
2474 	 */
2475 	if (nn->dp.chained_metadata_format && nn->fw_ver.major != 4)
2476 		nn->cap &= ~NFP_NET_CFG_CTRL_RSS;
2477 
2478 	/* Determine RX packet/metadata boundary offset */
2479 	if (nn->fw_ver.major >= 2) {
2480 		u32 reg;
2481 
2482 		reg = nn_readl(nn, NFP_NET_CFG_RX_OFFSET);
2483 		if (reg > NFP_NET_MAX_PREPEND) {
2484 			nn_err(nn, "Invalid rx offset: %d\n", reg);
2485 			return -EINVAL;
2486 		}
2487 		nn->dp.rx_offset = reg;
2488 	} else {
2489 		nn->dp.rx_offset = NFP_NET_RX_OFFSET;
2490 	}
2491 
2492 	/* Mask out NFD-version-specific features */
2493 	nn->cap &= nn->dp.ops->cap_mask;
2494 
2495 	/* For control vNICs mask out the capabilities app doesn't want. */
2496 	if (!nn->dp.netdev)
2497 		nn->cap &= nn->app->type->ctrl_cap_mask;
2498 
2499 	return 0;
2500 }
2501 
2502 /**
2503  * nfp_net_init() - Initialise/finalise the nfp_net structure
2504  * @nn:		NFP Net device structure
2505  *
2506  * Return: 0 on success or negative errno on error.
2507  */
2508 int nfp_net_init(struct nfp_net *nn)
2509 {
2510 	int err;
2511 
2512 	nn->dp.rx_dma_dir = DMA_FROM_DEVICE;
2513 
2514 	err = nfp_net_read_caps(nn);
2515 	if (err)
2516 		return err;
2517 
2518 	/* Set default MTU and Freelist buffer size */
2519 	if (!nfp_net_is_data_vnic(nn) && nn->app->ctrl_mtu) {
2520 		nn->dp.mtu = min(nn->app->ctrl_mtu, nn->max_mtu);
2521 	} else if (nn->max_mtu < NFP_NET_DEFAULT_MTU) {
2522 		nn->dp.mtu = nn->max_mtu;
2523 	} else {
2524 		nn->dp.mtu = NFP_NET_DEFAULT_MTU;
2525 	}
2526 	nn->dp.fl_bufsz = nfp_net_calc_fl_bufsz(&nn->dp);
2527 
2528 	if (nfp_app_ctrl_uses_data_vnics(nn->app))
2529 		nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_CMSG_DATA;
2530 
2531 	if (nn->cap & NFP_NET_CFG_CTRL_RSS_ANY) {
2532 		nfp_net_rss_init(nn);
2533 		nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_RSS2 ?:
2534 					 NFP_NET_CFG_CTRL_RSS;
2535 	}
2536 
2537 	/* Allow L2 Broadcast and Multicast through by default, if supported */
2538 	if (nn->cap & NFP_NET_CFG_CTRL_L2BC)
2539 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_L2BC;
2540 
2541 	/* Allow IRQ moderation, if supported */
2542 	if (nn->cap & NFP_NET_CFG_CTRL_IRQMOD) {
2543 		nfp_net_irqmod_init(nn);
2544 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_IRQMOD;
2545 	}
2546 
2547 	/* Enable TX pointer writeback, if supported */
2548 	if (nn->cap & NFP_NET_CFG_CTRL_TXRWB)
2549 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_TXRWB;
2550 
2551 	/* Stash the re-configuration queue away.  First odd queue in TX Bar */
2552 	nn->qcp_cfg = nn->tx_bar + NFP_QCP_QUEUE_ADDR_SZ;
2553 
2554 	/* Make sure the FW knows the netdev is supposed to be disabled here */
2555 	nn_writel(nn, NFP_NET_CFG_CTRL, 0);
2556 	nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, 0);
2557 	nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, 0);
2558 	err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_RING |
2559 				   NFP_NET_CFG_UPDATE_GEN);
2560 	if (err)
2561 		return err;
2562 
2563 	if (nn->dp.netdev) {
2564 		nfp_net_netdev_init(nn);
2565 
2566 		err = nfp_ccm_mbox_init(nn);
2567 		if (err)
2568 			return err;
2569 
2570 		err = nfp_net_tls_init(nn);
2571 		if (err)
2572 			goto err_clean_mbox;
2573 
2574 		nfp_net_ipsec_init(nn);
2575 	}
2576 
2577 	nfp_net_vecs_init(nn);
2578 
2579 	if (!nn->dp.netdev)
2580 		return 0;
2581 	return register_netdev(nn->dp.netdev);
2582 
2583 err_clean_mbox:
2584 	nfp_ccm_mbox_clean(nn);
2585 	return err;
2586 }
2587 
2588 /**
2589  * nfp_net_clean() - Undo what nfp_net_init() did.
2590  * @nn:		NFP Net device structure
2591  */
2592 void nfp_net_clean(struct nfp_net *nn)
2593 {
2594 	if (!nn->dp.netdev)
2595 		return;
2596 
2597 	unregister_netdev(nn->dp.netdev);
2598 	nfp_net_ipsec_clean(nn);
2599 	nfp_ccm_mbox_clean(nn);
2600 	nfp_net_reconfig_wait_posted(nn);
2601 }
2602