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