• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2015-2018 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/bpf_trace.h>
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/fs.h>
21 #include <linux/netdevice.h>
22 #include <linux/etherdevice.h>
23 #include <linux/interrupt.h>
24 #include <linux/ip.h>
25 #include <linux/ipv6.h>
26 #include <linux/mm.h>
27 #include <linux/overflow.h>
28 #include <linux/page_ref.h>
29 #include <linux/pci.h>
30 #include <linux/pci_regs.h>
31 #include <linux/msi.h>
32 #include <linux/ethtool.h>
33 #include <linux/log2.h>
34 #include <linux/if_vlan.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 
42 #include "nfpcore/nfp_nsp.h"
43 #include "ccm.h"
44 #include "nfp_app.h"
45 #include "nfp_net_ctrl.h"
46 #include "nfp_net.h"
47 #include "nfp_net_sriov.h"
48 #include "nfp_port.h"
49 #include "crypto/crypto.h"
50 #include "crypto/fw.h"
51 
52 /**
53  * nfp_net_get_fw_version() - Read and parse the FW version
54  * @fw_ver:	Output fw_version structure to read to
55  * @ctrl_bar:	Mapped address of the control BAR
56  */
nfp_net_get_fw_version(struct nfp_net_fw_version * fw_ver,void __iomem * ctrl_bar)57 void nfp_net_get_fw_version(struct nfp_net_fw_version *fw_ver,
58 			    void __iomem *ctrl_bar)
59 {
60 	u32 reg;
61 
62 	reg = readl(ctrl_bar + NFP_NET_CFG_VERSION);
63 	put_unaligned_le32(reg, fw_ver);
64 }
65 
nfp_net_dma_map_rx(struct nfp_net_dp * dp,void * frag)66 static dma_addr_t nfp_net_dma_map_rx(struct nfp_net_dp *dp, void *frag)
67 {
68 	return dma_map_single_attrs(dp->dev, frag + NFP_NET_RX_BUF_HEADROOM,
69 				    dp->fl_bufsz - NFP_NET_RX_BUF_NON_DATA,
70 				    dp->rx_dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
71 }
72 
73 static void
nfp_net_dma_sync_dev_rx(const struct nfp_net_dp * dp,dma_addr_t dma_addr)74 nfp_net_dma_sync_dev_rx(const struct nfp_net_dp *dp, dma_addr_t dma_addr)
75 {
76 	dma_sync_single_for_device(dp->dev, dma_addr,
77 				   dp->fl_bufsz - NFP_NET_RX_BUF_NON_DATA,
78 				   dp->rx_dma_dir);
79 }
80 
nfp_net_dma_unmap_rx(struct nfp_net_dp * dp,dma_addr_t dma_addr)81 static void nfp_net_dma_unmap_rx(struct nfp_net_dp *dp, dma_addr_t dma_addr)
82 {
83 	dma_unmap_single_attrs(dp->dev, dma_addr,
84 			       dp->fl_bufsz - NFP_NET_RX_BUF_NON_DATA,
85 			       dp->rx_dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
86 }
87 
nfp_net_dma_sync_cpu_rx(struct nfp_net_dp * dp,dma_addr_t dma_addr,unsigned int len)88 static void nfp_net_dma_sync_cpu_rx(struct nfp_net_dp *dp, dma_addr_t dma_addr,
89 				    unsigned int len)
90 {
91 	dma_sync_single_for_cpu(dp->dev, dma_addr - NFP_NET_RX_BUF_HEADROOM,
92 				len, dp->rx_dma_dir);
93 }
94 
95 /* Firmware reconfig
96  *
97  * Firmware reconfig may take a while so we have two versions of it -
98  * synchronous and asynchronous (posted).  All synchronous callers are holding
99  * RTNL so we don't have to worry about serializing them.
100  */
nfp_net_reconfig_start(struct nfp_net * nn,u32 update)101 static void nfp_net_reconfig_start(struct nfp_net *nn, u32 update)
102 {
103 	nn_writel(nn, NFP_NET_CFG_UPDATE, update);
104 	/* ensure update is written before pinging HW */
105 	nn_pci_flush(nn);
106 	nfp_qcp_wr_ptr_add(nn->qcp_cfg, 1);
107 	nn->reconfig_in_progress_update = update;
108 }
109 
110 /* Pass 0 as update to run posted reconfigs. */
nfp_net_reconfig_start_async(struct nfp_net * nn,u32 update)111 static void nfp_net_reconfig_start_async(struct nfp_net *nn, u32 update)
112 {
113 	update |= nn->reconfig_posted;
114 	nn->reconfig_posted = 0;
115 
116 	nfp_net_reconfig_start(nn, update);
117 
118 	nn->reconfig_timer_active = true;
119 	mod_timer(&nn->reconfig_timer, jiffies + NFP_NET_POLL_TIMEOUT * HZ);
120 }
121 
nfp_net_reconfig_check_done(struct nfp_net * nn,bool last_check)122 static bool nfp_net_reconfig_check_done(struct nfp_net *nn, bool last_check)
123 {
124 	u32 reg;
125 
126 	reg = nn_readl(nn, NFP_NET_CFG_UPDATE);
127 	if (reg == 0)
128 		return true;
129 	if (reg & NFP_NET_CFG_UPDATE_ERR) {
130 		nn_err(nn, "Reconfig error (status: 0x%08x update: 0x%08x ctrl: 0x%08x)\n",
131 		       reg, nn->reconfig_in_progress_update,
132 		       nn_readl(nn, NFP_NET_CFG_CTRL));
133 		return true;
134 	} else if (last_check) {
135 		nn_err(nn, "Reconfig timeout (status: 0x%08x update: 0x%08x ctrl: 0x%08x)\n",
136 		       reg, nn->reconfig_in_progress_update,
137 		       nn_readl(nn, NFP_NET_CFG_CTRL));
138 		return true;
139 	}
140 
141 	return false;
142 }
143 
__nfp_net_reconfig_wait(struct nfp_net * nn,unsigned long deadline)144 static bool __nfp_net_reconfig_wait(struct nfp_net *nn, unsigned long deadline)
145 {
146 	bool timed_out = false;
147 	int i;
148 
149 	/* Poll update field, waiting for NFP to ack the config.
150 	 * Do an opportunistic wait-busy loop, afterward sleep.
151 	 */
152 	for (i = 0; i < 50; i++) {
153 		if (nfp_net_reconfig_check_done(nn, false))
154 			return false;
155 		udelay(4);
156 	}
157 
158 	while (!nfp_net_reconfig_check_done(nn, timed_out)) {
159 		usleep_range(250, 500);
160 		timed_out = time_is_before_eq_jiffies(deadline);
161 	}
162 
163 	return timed_out;
164 }
165 
nfp_net_reconfig_wait(struct nfp_net * nn,unsigned long deadline)166 static int nfp_net_reconfig_wait(struct nfp_net *nn, unsigned long deadline)
167 {
168 	if (__nfp_net_reconfig_wait(nn, deadline))
169 		return -EIO;
170 
171 	if (nn_readl(nn, NFP_NET_CFG_UPDATE) & NFP_NET_CFG_UPDATE_ERR)
172 		return -EIO;
173 
174 	return 0;
175 }
176 
nfp_net_reconfig_timer(struct timer_list * t)177 static void nfp_net_reconfig_timer(struct timer_list *t)
178 {
179 	struct nfp_net *nn = from_timer(nn, t, reconfig_timer);
180 
181 	spin_lock_bh(&nn->reconfig_lock);
182 
183 	nn->reconfig_timer_active = false;
184 
185 	/* If sync caller is present it will take over from us */
186 	if (nn->reconfig_sync_present)
187 		goto done;
188 
189 	/* Read reconfig status and report errors */
190 	nfp_net_reconfig_check_done(nn, true);
191 
192 	if (nn->reconfig_posted)
193 		nfp_net_reconfig_start_async(nn, 0);
194 done:
195 	spin_unlock_bh(&nn->reconfig_lock);
196 }
197 
198 /**
199  * nfp_net_reconfig_post() - Post async reconfig request
200  * @nn:      NFP Net device to reconfigure
201  * @update:  The value for the update field in the BAR config
202  *
203  * Record FW reconfiguration request.  Reconfiguration will be kicked off
204  * whenever reconfiguration machinery is idle.  Multiple requests can be
205  * merged together!
206  */
nfp_net_reconfig_post(struct nfp_net * nn,u32 update)207 static void nfp_net_reconfig_post(struct nfp_net *nn, u32 update)
208 {
209 	spin_lock_bh(&nn->reconfig_lock);
210 
211 	/* Sync caller will kick off async reconf when it's done, just post */
212 	if (nn->reconfig_sync_present) {
213 		nn->reconfig_posted |= update;
214 		goto done;
215 	}
216 
217 	/* Opportunistically check if the previous command is done */
218 	if (!nn->reconfig_timer_active ||
219 	    nfp_net_reconfig_check_done(nn, false))
220 		nfp_net_reconfig_start_async(nn, update);
221 	else
222 		nn->reconfig_posted |= update;
223 done:
224 	spin_unlock_bh(&nn->reconfig_lock);
225 }
226 
nfp_net_reconfig_sync_enter(struct nfp_net * nn)227 static void nfp_net_reconfig_sync_enter(struct nfp_net *nn)
228 {
229 	bool cancelled_timer = false;
230 	u32 pre_posted_requests;
231 
232 	spin_lock_bh(&nn->reconfig_lock);
233 
234 	WARN_ON(nn->reconfig_sync_present);
235 	nn->reconfig_sync_present = true;
236 
237 	if (nn->reconfig_timer_active) {
238 		nn->reconfig_timer_active = false;
239 		cancelled_timer = true;
240 	}
241 	pre_posted_requests = nn->reconfig_posted;
242 	nn->reconfig_posted = 0;
243 
244 	spin_unlock_bh(&nn->reconfig_lock);
245 
246 	if (cancelled_timer) {
247 		del_timer_sync(&nn->reconfig_timer);
248 		nfp_net_reconfig_wait(nn, nn->reconfig_timer.expires);
249 	}
250 
251 	/* Run the posted reconfigs which were issued before we started */
252 	if (pre_posted_requests) {
253 		nfp_net_reconfig_start(nn, pre_posted_requests);
254 		nfp_net_reconfig_wait(nn, jiffies + HZ * NFP_NET_POLL_TIMEOUT);
255 	}
256 }
257 
nfp_net_reconfig_wait_posted(struct nfp_net * nn)258 static void nfp_net_reconfig_wait_posted(struct nfp_net *nn)
259 {
260 	nfp_net_reconfig_sync_enter(nn);
261 
262 	spin_lock_bh(&nn->reconfig_lock);
263 	nn->reconfig_sync_present = false;
264 	spin_unlock_bh(&nn->reconfig_lock);
265 }
266 
267 /**
268  * __nfp_net_reconfig() - Reconfigure the firmware
269  * @nn:      NFP Net device to reconfigure
270  * @update:  The value for the update field in the BAR config
271  *
272  * Write the update word to the BAR and ping the reconfig queue.  The
273  * poll until the firmware has acknowledged the update by zeroing the
274  * update word.
275  *
276  * Return: Negative errno on error, 0 on success
277  */
__nfp_net_reconfig(struct nfp_net * nn,u32 update)278 int __nfp_net_reconfig(struct nfp_net *nn, u32 update)
279 {
280 	int ret;
281 
282 	nfp_net_reconfig_sync_enter(nn);
283 
284 	nfp_net_reconfig_start(nn, update);
285 	ret = nfp_net_reconfig_wait(nn, jiffies + HZ * NFP_NET_POLL_TIMEOUT);
286 
287 	spin_lock_bh(&nn->reconfig_lock);
288 
289 	if (nn->reconfig_posted)
290 		nfp_net_reconfig_start_async(nn, 0);
291 
292 	nn->reconfig_sync_present = false;
293 
294 	spin_unlock_bh(&nn->reconfig_lock);
295 
296 	return ret;
297 }
298 
nfp_net_reconfig(struct nfp_net * nn,u32 update)299 int nfp_net_reconfig(struct nfp_net *nn, u32 update)
300 {
301 	int ret;
302 
303 	nn_ctrl_bar_lock(nn);
304 	ret = __nfp_net_reconfig(nn, update);
305 	nn_ctrl_bar_unlock(nn);
306 
307 	return ret;
308 }
309 
nfp_net_mbox_lock(struct nfp_net * nn,unsigned int data_size)310 int nfp_net_mbox_lock(struct nfp_net *nn, unsigned int data_size)
311 {
312 	if (nn->tlv_caps.mbox_len < NFP_NET_CFG_MBOX_SIMPLE_VAL + data_size) {
313 		nn_err(nn, "mailbox too small for %u of data (%u)\n",
314 		       data_size, nn->tlv_caps.mbox_len);
315 		return -EIO;
316 	}
317 
318 	nn_ctrl_bar_lock(nn);
319 	return 0;
320 }
321 
322 /**
323  * nfp_net_mbox_reconfig() - Reconfigure the firmware via the mailbox
324  * @nn:        NFP Net device to reconfigure
325  * @mbox_cmd:  The value for the mailbox command
326  *
327  * Helper function for mailbox updates
328  *
329  * Return: Negative errno on error, 0 on success
330  */
nfp_net_mbox_reconfig(struct nfp_net * nn,u32 mbox_cmd)331 int nfp_net_mbox_reconfig(struct nfp_net *nn, u32 mbox_cmd)
332 {
333 	u32 mbox = nn->tlv_caps.mbox_off;
334 	int ret;
335 
336 	nn_writeq(nn, mbox + NFP_NET_CFG_MBOX_SIMPLE_CMD, mbox_cmd);
337 
338 	ret = __nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_MBOX);
339 	if (ret) {
340 		nn_err(nn, "Mailbox update error\n");
341 		return ret;
342 	}
343 
344 	return -nn_readl(nn, mbox + NFP_NET_CFG_MBOX_SIMPLE_RET);
345 }
346 
nfp_net_mbox_reconfig_post(struct nfp_net * nn,u32 mbox_cmd)347 void nfp_net_mbox_reconfig_post(struct nfp_net *nn, u32 mbox_cmd)
348 {
349 	u32 mbox = nn->tlv_caps.mbox_off;
350 
351 	nn_writeq(nn, mbox + NFP_NET_CFG_MBOX_SIMPLE_CMD, mbox_cmd);
352 
353 	nfp_net_reconfig_post(nn, NFP_NET_CFG_UPDATE_MBOX);
354 }
355 
nfp_net_mbox_reconfig_wait_posted(struct nfp_net * nn)356 int nfp_net_mbox_reconfig_wait_posted(struct nfp_net *nn)
357 {
358 	u32 mbox = nn->tlv_caps.mbox_off;
359 
360 	nfp_net_reconfig_wait_posted(nn);
361 
362 	return -nn_readl(nn, mbox + NFP_NET_CFG_MBOX_SIMPLE_RET);
363 }
364 
nfp_net_mbox_reconfig_and_unlock(struct nfp_net * nn,u32 mbox_cmd)365 int nfp_net_mbox_reconfig_and_unlock(struct nfp_net *nn, u32 mbox_cmd)
366 {
367 	int ret;
368 
369 	ret = nfp_net_mbox_reconfig(nn, mbox_cmd);
370 	nn_ctrl_bar_unlock(nn);
371 	return ret;
372 }
373 
374 /* Interrupt configuration and handling
375  */
376 
377 /**
378  * nfp_net_irq_unmask() - Unmask automasked interrupt
379  * @nn:       NFP Network structure
380  * @entry_nr: MSI-X table entry
381  *
382  * Clear the ICR for the IRQ entry.
383  */
nfp_net_irq_unmask(struct nfp_net * nn,unsigned int entry_nr)384 static void nfp_net_irq_unmask(struct nfp_net *nn, unsigned int entry_nr)
385 {
386 	nn_writeb(nn, NFP_NET_CFG_ICR(entry_nr), NFP_NET_CFG_ICR_UNMASKED);
387 	nn_pci_flush(nn);
388 }
389 
390 /**
391  * nfp_net_irqs_alloc() - allocates MSI-X irqs
392  * @pdev:        PCI device structure
393  * @irq_entries: Array to be initialized and used to hold the irq entries
394  * @min_irqs:    Minimal acceptable number of interrupts
395  * @wanted_irqs: Target number of interrupts to allocate
396  *
397  * Return: Number of irqs obtained or 0 on error.
398  */
399 unsigned int
nfp_net_irqs_alloc(struct pci_dev * pdev,struct msix_entry * irq_entries,unsigned int min_irqs,unsigned int wanted_irqs)400 nfp_net_irqs_alloc(struct pci_dev *pdev, struct msix_entry *irq_entries,
401 		   unsigned int min_irqs, unsigned int wanted_irqs)
402 {
403 	unsigned int i;
404 	int got_irqs;
405 
406 	for (i = 0; i < wanted_irqs; i++)
407 		irq_entries[i].entry = i;
408 
409 	got_irqs = pci_enable_msix_range(pdev, irq_entries,
410 					 min_irqs, wanted_irqs);
411 	if (got_irqs < 0) {
412 		dev_err(&pdev->dev, "Failed to enable %d-%d MSI-X (err=%d)\n",
413 			min_irqs, wanted_irqs, got_irqs);
414 		return 0;
415 	}
416 
417 	if (got_irqs < wanted_irqs)
418 		dev_warn(&pdev->dev, "Unable to allocate %d IRQs got only %d\n",
419 			 wanted_irqs, got_irqs);
420 
421 	return got_irqs;
422 }
423 
424 /**
425  * nfp_net_irqs_assign() - Assign interrupts allocated externally to netdev
426  * @nn:		 NFP Network structure
427  * @irq_entries: Table of allocated interrupts
428  * @n:		 Size of @irq_entries (number of entries to grab)
429  *
430  * After interrupts are allocated with nfp_net_irqs_alloc() this function
431  * should be called to assign them to a specific netdev (port).
432  */
433 void
nfp_net_irqs_assign(struct nfp_net * nn,struct msix_entry * irq_entries,unsigned int n)434 nfp_net_irqs_assign(struct nfp_net *nn, struct msix_entry *irq_entries,
435 		    unsigned int n)
436 {
437 	struct nfp_net_dp *dp = &nn->dp;
438 
439 	nn->max_r_vecs = n - NFP_NET_NON_Q_VECTORS;
440 	dp->num_r_vecs = nn->max_r_vecs;
441 
442 	memcpy(nn->irq_entries, irq_entries, sizeof(*irq_entries) * n);
443 
444 	if (dp->num_rx_rings > dp->num_r_vecs ||
445 	    dp->num_tx_rings > dp->num_r_vecs)
446 		dev_warn(nn->dp.dev, "More rings (%d,%d) than vectors (%d).\n",
447 			 dp->num_rx_rings, dp->num_tx_rings,
448 			 dp->num_r_vecs);
449 
450 	dp->num_rx_rings = min(dp->num_r_vecs, dp->num_rx_rings);
451 	dp->num_tx_rings = min(dp->num_r_vecs, dp->num_tx_rings);
452 	dp->num_stack_tx_rings = dp->num_tx_rings;
453 }
454 
455 /**
456  * nfp_net_irqs_disable() - Disable interrupts
457  * @pdev:        PCI device structure
458  *
459  * Undoes what @nfp_net_irqs_alloc() does.
460  */
nfp_net_irqs_disable(struct pci_dev * pdev)461 void nfp_net_irqs_disable(struct pci_dev *pdev)
462 {
463 	pci_disable_msix(pdev);
464 }
465 
466 /**
467  * nfp_net_irq_rxtx() - Interrupt service routine for RX/TX rings.
468  * @irq:      Interrupt
469  * @data:     Opaque data structure
470  *
471  * Return: Indicate if the interrupt has been handled.
472  */
nfp_net_irq_rxtx(int irq,void * data)473 static irqreturn_t nfp_net_irq_rxtx(int irq, void *data)
474 {
475 	struct nfp_net_r_vector *r_vec = data;
476 
477 	/* Currently we cannot tell if it's a rx or tx interrupt,
478 	 * since dim does not need accurate event_ctr to calculate,
479 	 * we just use this counter for both rx and tx dim.
480 	 */
481 	r_vec->event_ctr++;
482 
483 	napi_schedule_irqoff(&r_vec->napi);
484 
485 	/* The FW auto-masks any interrupt, either via the MASK bit in
486 	 * the MSI-X table or via the per entry ICR field.  So there
487 	 * is no need to disable interrupts here.
488 	 */
489 	return IRQ_HANDLED;
490 }
491 
nfp_ctrl_irq_rxtx(int irq,void * data)492 static irqreturn_t nfp_ctrl_irq_rxtx(int irq, void *data)
493 {
494 	struct nfp_net_r_vector *r_vec = data;
495 
496 	tasklet_schedule(&r_vec->tasklet);
497 
498 	return IRQ_HANDLED;
499 }
500 
501 /**
502  * nfp_net_read_link_status() - Reread link status from control BAR
503  * @nn:       NFP Network structure
504  */
nfp_net_read_link_status(struct nfp_net * nn)505 static void nfp_net_read_link_status(struct nfp_net *nn)
506 {
507 	unsigned long flags;
508 	bool link_up;
509 	u32 sts;
510 
511 	spin_lock_irqsave(&nn->link_status_lock, flags);
512 
513 	sts = nn_readl(nn, NFP_NET_CFG_STS);
514 	link_up = !!(sts & NFP_NET_CFG_STS_LINK);
515 
516 	if (nn->link_up == link_up)
517 		goto out;
518 
519 	nn->link_up = link_up;
520 	if (nn->port)
521 		set_bit(NFP_PORT_CHANGED, &nn->port->flags);
522 
523 	if (nn->link_up) {
524 		netif_carrier_on(nn->dp.netdev);
525 		netdev_info(nn->dp.netdev, "NIC Link is Up\n");
526 	} else {
527 		netif_carrier_off(nn->dp.netdev);
528 		netdev_info(nn->dp.netdev, "NIC Link is Down\n");
529 	}
530 out:
531 	spin_unlock_irqrestore(&nn->link_status_lock, flags);
532 }
533 
534 /**
535  * nfp_net_irq_lsc() - Interrupt service routine for link state changes
536  * @irq:      Interrupt
537  * @data:     Opaque data structure
538  *
539  * Return: Indicate if the interrupt has been handled.
540  */
nfp_net_irq_lsc(int irq,void * data)541 static irqreturn_t nfp_net_irq_lsc(int irq, void *data)
542 {
543 	struct nfp_net *nn = data;
544 	struct msix_entry *entry;
545 
546 	entry = &nn->irq_entries[NFP_NET_IRQ_LSC_IDX];
547 
548 	nfp_net_read_link_status(nn);
549 
550 	nfp_net_irq_unmask(nn, entry->entry);
551 
552 	return IRQ_HANDLED;
553 }
554 
555 /**
556  * nfp_net_irq_exn() - Interrupt service routine for exceptions
557  * @irq:      Interrupt
558  * @data:     Opaque data structure
559  *
560  * Return: Indicate if the interrupt has been handled.
561  */
nfp_net_irq_exn(int irq,void * data)562 static irqreturn_t nfp_net_irq_exn(int irq, void *data)
563 {
564 	struct nfp_net *nn = data;
565 
566 	nn_err(nn, "%s: UNIMPLEMENTED.\n", __func__);
567 	/* XXX TO BE IMPLEMENTED */
568 	return IRQ_HANDLED;
569 }
570 
571 /**
572  * nfp_net_tx_ring_init() - Fill in the boilerplate for a TX ring
573  * @tx_ring:  TX ring structure
574  * @r_vec:    IRQ vector servicing this ring
575  * @idx:      Ring index
576  * @is_xdp:   Is this an XDP TX ring?
577  */
578 static void
nfp_net_tx_ring_init(struct nfp_net_tx_ring * tx_ring,struct nfp_net_r_vector * r_vec,unsigned int idx,bool is_xdp)579 nfp_net_tx_ring_init(struct nfp_net_tx_ring *tx_ring,
580 		     struct nfp_net_r_vector *r_vec, unsigned int idx,
581 		     bool is_xdp)
582 {
583 	struct nfp_net *nn = r_vec->nfp_net;
584 
585 	tx_ring->idx = idx;
586 	tx_ring->r_vec = r_vec;
587 	tx_ring->is_xdp = is_xdp;
588 	u64_stats_init(&tx_ring->r_vec->tx_sync);
589 
590 	tx_ring->qcidx = tx_ring->idx * nn->stride_tx;
591 	tx_ring->qcp_q = nn->tx_bar + NFP_QCP_QUEUE_OFF(tx_ring->qcidx);
592 }
593 
594 /**
595  * nfp_net_rx_ring_init() - Fill in the boilerplate for a RX ring
596  * @rx_ring:  RX ring structure
597  * @r_vec:    IRQ vector servicing this ring
598  * @idx:      Ring index
599  */
600 static void
nfp_net_rx_ring_init(struct nfp_net_rx_ring * rx_ring,struct nfp_net_r_vector * r_vec,unsigned int idx)601 nfp_net_rx_ring_init(struct nfp_net_rx_ring *rx_ring,
602 		     struct nfp_net_r_vector *r_vec, unsigned int idx)
603 {
604 	struct nfp_net *nn = r_vec->nfp_net;
605 
606 	rx_ring->idx = idx;
607 	rx_ring->r_vec = r_vec;
608 	u64_stats_init(&rx_ring->r_vec->rx_sync);
609 
610 	rx_ring->fl_qcidx = rx_ring->idx * nn->stride_rx;
611 	rx_ring->qcp_fl = nn->rx_bar + NFP_QCP_QUEUE_OFF(rx_ring->fl_qcidx);
612 }
613 
614 /**
615  * nfp_net_aux_irq_request() - Request an auxiliary interrupt (LSC or EXN)
616  * @nn:		NFP Network structure
617  * @ctrl_offset: Control BAR offset where IRQ configuration should be written
618  * @format:	printf-style format to construct the interrupt name
619  * @name:	Pointer to allocated space for interrupt name
620  * @name_sz:	Size of space for interrupt name
621  * @vector_idx:	Index of MSI-X vector used for this interrupt
622  * @handler:	IRQ handler to register for this interrupt
623  */
624 static int
nfp_net_aux_irq_request(struct nfp_net * nn,u32 ctrl_offset,const char * format,char * name,size_t name_sz,unsigned int vector_idx,irq_handler_t handler)625 nfp_net_aux_irq_request(struct nfp_net *nn, u32 ctrl_offset,
626 			const char *format, char *name, size_t name_sz,
627 			unsigned int vector_idx, irq_handler_t handler)
628 {
629 	struct msix_entry *entry;
630 	int err;
631 
632 	entry = &nn->irq_entries[vector_idx];
633 
634 	snprintf(name, name_sz, format, nfp_net_name(nn));
635 	err = request_irq(entry->vector, handler, 0, name, nn);
636 	if (err) {
637 		nn_err(nn, "Failed to request IRQ %d (err=%d).\n",
638 		       entry->vector, err);
639 		return err;
640 	}
641 	nn_writeb(nn, ctrl_offset, entry->entry);
642 	nfp_net_irq_unmask(nn, entry->entry);
643 
644 	return 0;
645 }
646 
647 /**
648  * nfp_net_aux_irq_free() - Free an auxiliary interrupt (LSC or EXN)
649  * @nn:		NFP Network structure
650  * @ctrl_offset: Control BAR offset where IRQ configuration should be written
651  * @vector_idx:	Index of MSI-X vector used for this interrupt
652  */
nfp_net_aux_irq_free(struct nfp_net * nn,u32 ctrl_offset,unsigned int vector_idx)653 static void nfp_net_aux_irq_free(struct nfp_net *nn, u32 ctrl_offset,
654 				 unsigned int vector_idx)
655 {
656 	nn_writeb(nn, ctrl_offset, 0xff);
657 	nn_pci_flush(nn);
658 	free_irq(nn->irq_entries[vector_idx].vector, nn);
659 }
660 
661 /* Transmit
662  *
663  * One queue controller peripheral queue is used for transmit.  The
664  * driver en-queues packets for transmit by advancing the write
665  * pointer.  The device indicates that packets have transmitted by
666  * advancing the read pointer.  The driver maintains a local copy of
667  * the read and write pointer in @struct nfp_net_tx_ring.  The driver
668  * keeps @wr_p in sync with the queue controller write pointer and can
669  * determine how many packets have been transmitted by comparing its
670  * copy of the read pointer @rd_p with the read pointer maintained by
671  * the queue controller peripheral.
672  */
673 
674 /**
675  * nfp_net_tx_full() - Check if the TX ring is full
676  * @tx_ring: TX ring to check
677  * @dcnt:    Number of descriptors that need to be enqueued (must be >= 1)
678  *
679  * This function checks, based on the *host copy* of read/write
680  * pointer if a given TX ring is full.  The real TX queue may have
681  * some newly made available slots.
682  *
683  * Return: True if the ring is full.
684  */
nfp_net_tx_full(struct nfp_net_tx_ring * tx_ring,int dcnt)685 static int nfp_net_tx_full(struct nfp_net_tx_ring *tx_ring, int dcnt)
686 {
687 	return (tx_ring->wr_p - tx_ring->rd_p) >= (tx_ring->cnt - dcnt);
688 }
689 
690 /* Wrappers for deciding when to stop and restart TX queues */
nfp_net_tx_ring_should_wake(struct nfp_net_tx_ring * tx_ring)691 static int nfp_net_tx_ring_should_wake(struct nfp_net_tx_ring *tx_ring)
692 {
693 	return !nfp_net_tx_full(tx_ring, MAX_SKB_FRAGS * 4);
694 }
695 
nfp_net_tx_ring_should_stop(struct nfp_net_tx_ring * tx_ring)696 static int nfp_net_tx_ring_should_stop(struct nfp_net_tx_ring *tx_ring)
697 {
698 	return nfp_net_tx_full(tx_ring, MAX_SKB_FRAGS + 1);
699 }
700 
701 /**
702  * nfp_net_tx_ring_stop() - stop tx ring
703  * @nd_q:    netdev queue
704  * @tx_ring: driver tx queue structure
705  *
706  * Safely stop TX ring.  Remember that while we are running .start_xmit()
707  * someone else may be cleaning the TX ring completions so we need to be
708  * extra careful here.
709  */
nfp_net_tx_ring_stop(struct netdev_queue * nd_q,struct nfp_net_tx_ring * tx_ring)710 static void nfp_net_tx_ring_stop(struct netdev_queue *nd_q,
711 				 struct nfp_net_tx_ring *tx_ring)
712 {
713 	netif_tx_stop_queue(nd_q);
714 
715 	/* We can race with the TX completion out of NAPI so recheck */
716 	smp_mb();
717 	if (unlikely(nfp_net_tx_ring_should_wake(tx_ring)))
718 		netif_tx_start_queue(nd_q);
719 }
720 
721 /**
722  * nfp_net_tx_tso() - Set up Tx descriptor for LSO
723  * @r_vec: per-ring structure
724  * @txbuf: Pointer to driver soft TX descriptor
725  * @txd: Pointer to HW TX descriptor
726  * @skb: Pointer to SKB
727  * @md_bytes: Prepend length
728  *
729  * Set up Tx descriptor for LSO, do nothing for non-LSO skbs.
730  * Return error on packet header greater than maximum supported LSO header size.
731  */
nfp_net_tx_tso(struct nfp_net_r_vector * r_vec,struct nfp_net_tx_buf * txbuf,struct nfp_net_tx_desc * txd,struct sk_buff * skb,u32 md_bytes)732 static void nfp_net_tx_tso(struct nfp_net_r_vector *r_vec,
733 			   struct nfp_net_tx_buf *txbuf,
734 			   struct nfp_net_tx_desc *txd, struct sk_buff *skb,
735 			   u32 md_bytes)
736 {
737 	u32 l3_offset, l4_offset, hdrlen;
738 	u16 mss;
739 
740 	if (!skb_is_gso(skb))
741 		return;
742 
743 	if (!skb->encapsulation) {
744 		l3_offset = skb_network_offset(skb);
745 		l4_offset = skb_transport_offset(skb);
746 		hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
747 	} else {
748 		l3_offset = skb_inner_network_offset(skb);
749 		l4_offset = skb_inner_transport_offset(skb);
750 		hdrlen = skb_inner_transport_header(skb) - skb->data +
751 			inner_tcp_hdrlen(skb);
752 	}
753 
754 	txbuf->pkt_cnt = skb_shinfo(skb)->gso_segs;
755 	txbuf->real_len += hdrlen * (txbuf->pkt_cnt - 1);
756 
757 	mss = skb_shinfo(skb)->gso_size & PCIE_DESC_TX_MSS_MASK;
758 	txd->l3_offset = l3_offset - md_bytes;
759 	txd->l4_offset = l4_offset - md_bytes;
760 	txd->lso_hdrlen = hdrlen - md_bytes;
761 	txd->mss = cpu_to_le16(mss);
762 	txd->flags |= PCIE_DESC_TX_LSO;
763 
764 	u64_stats_update_begin(&r_vec->tx_sync);
765 	r_vec->tx_lso++;
766 	u64_stats_update_end(&r_vec->tx_sync);
767 }
768 
769 /**
770  * nfp_net_tx_csum() - Set TX CSUM offload flags in TX descriptor
771  * @dp:  NFP Net data path struct
772  * @r_vec: per-ring structure
773  * @txbuf: Pointer to driver soft TX descriptor
774  * @txd: Pointer to TX descriptor
775  * @skb: Pointer to SKB
776  *
777  * This function sets the TX checksum flags in the TX descriptor based
778  * on the configuration and the protocol of the packet to be transmitted.
779  */
nfp_net_tx_csum(struct nfp_net_dp * dp,struct nfp_net_r_vector * r_vec,struct nfp_net_tx_buf * txbuf,struct nfp_net_tx_desc * txd,struct sk_buff * skb)780 static void nfp_net_tx_csum(struct nfp_net_dp *dp,
781 			    struct nfp_net_r_vector *r_vec,
782 			    struct nfp_net_tx_buf *txbuf,
783 			    struct nfp_net_tx_desc *txd, struct sk_buff *skb)
784 {
785 	struct ipv6hdr *ipv6h;
786 	struct iphdr *iph;
787 	u8 l4_hdr;
788 
789 	if (!(dp->ctrl & NFP_NET_CFG_CTRL_TXCSUM))
790 		return;
791 
792 	if (skb->ip_summed != CHECKSUM_PARTIAL)
793 		return;
794 
795 	txd->flags |= PCIE_DESC_TX_CSUM;
796 	if (skb->encapsulation)
797 		txd->flags |= PCIE_DESC_TX_ENCAP;
798 
799 	iph = skb->encapsulation ? inner_ip_hdr(skb) : ip_hdr(skb);
800 	ipv6h = skb->encapsulation ? inner_ipv6_hdr(skb) : ipv6_hdr(skb);
801 
802 	if (iph->version == 4) {
803 		txd->flags |= PCIE_DESC_TX_IP4_CSUM;
804 		l4_hdr = iph->protocol;
805 	} else if (ipv6h->version == 6) {
806 		l4_hdr = ipv6h->nexthdr;
807 	} else {
808 		nn_dp_warn(dp, "partial checksum but ipv=%x!\n", iph->version);
809 		return;
810 	}
811 
812 	switch (l4_hdr) {
813 	case IPPROTO_TCP:
814 		txd->flags |= PCIE_DESC_TX_TCP_CSUM;
815 		break;
816 	case IPPROTO_UDP:
817 		txd->flags |= PCIE_DESC_TX_UDP_CSUM;
818 		break;
819 	default:
820 		nn_dp_warn(dp, "partial checksum but l4 proto=%x!\n", l4_hdr);
821 		return;
822 	}
823 
824 	u64_stats_update_begin(&r_vec->tx_sync);
825 	if (skb->encapsulation)
826 		r_vec->hw_csum_tx_inner += txbuf->pkt_cnt;
827 	else
828 		r_vec->hw_csum_tx += txbuf->pkt_cnt;
829 	u64_stats_update_end(&r_vec->tx_sync);
830 }
831 
832 static struct sk_buff *
nfp_net_tls_tx(struct nfp_net_dp * dp,struct nfp_net_r_vector * r_vec,struct sk_buff * skb,u64 * tls_handle,int * nr_frags)833 nfp_net_tls_tx(struct nfp_net_dp *dp, struct nfp_net_r_vector *r_vec,
834 	       struct sk_buff *skb, u64 *tls_handle, int *nr_frags)
835 {
836 #ifdef CONFIG_TLS_DEVICE
837 	struct nfp_net_tls_offload_ctx *ntls;
838 	struct sk_buff *nskb;
839 	bool resync_pending;
840 	u32 datalen, seq;
841 
842 	if (likely(!dp->ktls_tx))
843 		return skb;
844 	if (!skb->sk || !tls_is_sk_tx_device_offloaded(skb->sk))
845 		return skb;
846 
847 	datalen = skb->len - (skb_transport_offset(skb) + tcp_hdrlen(skb));
848 	seq = ntohl(tcp_hdr(skb)->seq);
849 	ntls = tls_driver_ctx(skb->sk, TLS_OFFLOAD_CTX_DIR_TX);
850 	resync_pending = tls_offload_tx_resync_pending(skb->sk);
851 	if (unlikely(resync_pending || ntls->next_seq != seq)) {
852 		/* Pure ACK out of order already */
853 		if (!datalen)
854 			return skb;
855 
856 		u64_stats_update_begin(&r_vec->tx_sync);
857 		r_vec->tls_tx_fallback++;
858 		u64_stats_update_end(&r_vec->tx_sync);
859 
860 		nskb = tls_encrypt_skb(skb);
861 		if (!nskb) {
862 			u64_stats_update_begin(&r_vec->tx_sync);
863 			r_vec->tls_tx_no_fallback++;
864 			u64_stats_update_end(&r_vec->tx_sync);
865 			return NULL;
866 		}
867 		/* encryption wasn't necessary */
868 		if (nskb == skb)
869 			return skb;
870 		/* we don't re-check ring space */
871 		if (unlikely(skb_is_nonlinear(nskb))) {
872 			nn_dp_warn(dp, "tls_encrypt_skb() produced fragmented frame\n");
873 			u64_stats_update_begin(&r_vec->tx_sync);
874 			r_vec->tx_errors++;
875 			u64_stats_update_end(&r_vec->tx_sync);
876 			dev_kfree_skb_any(nskb);
877 			return NULL;
878 		}
879 
880 		/* jump forward, a TX may have gotten lost, need to sync TX */
881 		if (!resync_pending && seq - ntls->next_seq < U32_MAX / 4)
882 			tls_offload_tx_resync_request(nskb->sk, seq,
883 						      ntls->next_seq);
884 
885 		*nr_frags = 0;
886 		return nskb;
887 	}
888 
889 	if (datalen) {
890 		u64_stats_update_begin(&r_vec->tx_sync);
891 		if (!skb_is_gso(skb))
892 			r_vec->hw_tls_tx++;
893 		else
894 			r_vec->hw_tls_tx += skb_shinfo(skb)->gso_segs;
895 		u64_stats_update_end(&r_vec->tx_sync);
896 	}
897 
898 	memcpy(tls_handle, ntls->fw_handle, sizeof(ntls->fw_handle));
899 	ntls->next_seq += datalen;
900 #endif
901 	return skb;
902 }
903 
nfp_net_tls_tx_undo(struct sk_buff * skb,u64 tls_handle)904 static void nfp_net_tls_tx_undo(struct sk_buff *skb, u64 tls_handle)
905 {
906 #ifdef CONFIG_TLS_DEVICE
907 	struct nfp_net_tls_offload_ctx *ntls;
908 	u32 datalen, seq;
909 
910 	if (!tls_handle)
911 		return;
912 	if (WARN_ON_ONCE(!skb->sk || !tls_is_sk_tx_device_offloaded(skb->sk)))
913 		return;
914 
915 	datalen = skb->len - (skb_transport_offset(skb) + tcp_hdrlen(skb));
916 	seq = ntohl(tcp_hdr(skb)->seq);
917 
918 	ntls = tls_driver_ctx(skb->sk, TLS_OFFLOAD_CTX_DIR_TX);
919 	if (ntls->next_seq == seq + datalen)
920 		ntls->next_seq = seq;
921 	else
922 		WARN_ON_ONCE(1);
923 #endif
924 }
925 
nfp_net_tx_xmit_more_flush(struct nfp_net_tx_ring * tx_ring)926 static void nfp_net_tx_xmit_more_flush(struct nfp_net_tx_ring *tx_ring)
927 {
928 	wmb();
929 	nfp_qcp_wr_ptr_add(tx_ring->qcp_q, tx_ring->wr_ptr_add);
930 	tx_ring->wr_ptr_add = 0;
931 }
932 
nfp_net_prep_tx_meta(struct sk_buff * skb,u64 tls_handle)933 static int nfp_net_prep_tx_meta(struct sk_buff *skb, u64 tls_handle)
934 {
935 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
936 	unsigned char *data;
937 	u32 meta_id = 0;
938 	int md_bytes;
939 
940 	if (likely(!md_dst && !tls_handle))
941 		return 0;
942 	if (unlikely(md_dst && md_dst->type != METADATA_HW_PORT_MUX)) {
943 		if (!tls_handle)
944 			return 0;
945 		md_dst = NULL;
946 	}
947 
948 	md_bytes = 4 + !!md_dst * 4 + !!tls_handle * 8;
949 
950 	if (unlikely(skb_cow_head(skb, md_bytes)))
951 		return -ENOMEM;
952 
953 	meta_id = 0;
954 	data = skb_push(skb, md_bytes) + md_bytes;
955 	if (md_dst) {
956 		data -= 4;
957 		put_unaligned_be32(md_dst->u.port_info.port_id, data);
958 		meta_id = NFP_NET_META_PORTID;
959 	}
960 	if (tls_handle) {
961 		/* conn handle is opaque, we just use u64 to be able to quickly
962 		 * compare it to zero
963 		 */
964 		data -= 8;
965 		memcpy(data, &tls_handle, sizeof(tls_handle));
966 		meta_id <<= NFP_NET_META_FIELD_SIZE;
967 		meta_id |= NFP_NET_META_CONN_HANDLE;
968 	}
969 
970 	data -= 4;
971 	put_unaligned_be32(meta_id, data);
972 
973 	return md_bytes;
974 }
975 
976 /**
977  * nfp_net_tx() - Main transmit entry point
978  * @skb:    SKB to transmit
979  * @netdev: netdev structure
980  *
981  * Return: NETDEV_TX_OK on success.
982  */
nfp_net_tx(struct sk_buff * skb,struct net_device * netdev)983 static netdev_tx_t nfp_net_tx(struct sk_buff *skb, struct net_device *netdev)
984 {
985 	struct nfp_net *nn = netdev_priv(netdev);
986 	const skb_frag_t *frag;
987 	int f, nr_frags, wr_idx, md_bytes;
988 	struct nfp_net_tx_ring *tx_ring;
989 	struct nfp_net_r_vector *r_vec;
990 	struct nfp_net_tx_buf *txbuf;
991 	struct nfp_net_tx_desc *txd;
992 	struct netdev_queue *nd_q;
993 	struct nfp_net_dp *dp;
994 	dma_addr_t dma_addr;
995 	unsigned int fsize;
996 	u64 tls_handle = 0;
997 	u16 qidx;
998 
999 	dp = &nn->dp;
1000 	qidx = skb_get_queue_mapping(skb);
1001 	tx_ring = &dp->tx_rings[qidx];
1002 	r_vec = tx_ring->r_vec;
1003 
1004 	nr_frags = skb_shinfo(skb)->nr_frags;
1005 
1006 	if (unlikely(nfp_net_tx_full(tx_ring, nr_frags + 1))) {
1007 		nn_dp_warn(dp, "TX ring %d busy. wrp=%u rdp=%u\n",
1008 			   qidx, tx_ring->wr_p, tx_ring->rd_p);
1009 		nd_q = netdev_get_tx_queue(dp->netdev, qidx);
1010 		netif_tx_stop_queue(nd_q);
1011 		nfp_net_tx_xmit_more_flush(tx_ring);
1012 		u64_stats_update_begin(&r_vec->tx_sync);
1013 		r_vec->tx_busy++;
1014 		u64_stats_update_end(&r_vec->tx_sync);
1015 		return NETDEV_TX_BUSY;
1016 	}
1017 
1018 	skb = nfp_net_tls_tx(dp, r_vec, skb, &tls_handle, &nr_frags);
1019 	if (unlikely(!skb)) {
1020 		nfp_net_tx_xmit_more_flush(tx_ring);
1021 		return NETDEV_TX_OK;
1022 	}
1023 
1024 	md_bytes = nfp_net_prep_tx_meta(skb, tls_handle);
1025 	if (unlikely(md_bytes < 0))
1026 		goto err_flush;
1027 
1028 	/* Start with the head skbuf */
1029 	dma_addr = dma_map_single(dp->dev, skb->data, skb_headlen(skb),
1030 				  DMA_TO_DEVICE);
1031 	if (dma_mapping_error(dp->dev, dma_addr))
1032 		goto err_dma_err;
1033 
1034 	wr_idx = D_IDX(tx_ring, tx_ring->wr_p);
1035 
1036 	/* Stash the soft descriptor of the head then initialize it */
1037 	txbuf = &tx_ring->txbufs[wr_idx];
1038 	txbuf->skb = skb;
1039 	txbuf->dma_addr = dma_addr;
1040 	txbuf->fidx = -1;
1041 	txbuf->pkt_cnt = 1;
1042 	txbuf->real_len = skb->len;
1043 
1044 	/* Build TX descriptor */
1045 	txd = &tx_ring->txds[wr_idx];
1046 	txd->offset_eop = (nr_frags ? 0 : PCIE_DESC_TX_EOP) | md_bytes;
1047 	txd->dma_len = cpu_to_le16(skb_headlen(skb));
1048 	nfp_desc_set_dma_addr(txd, dma_addr);
1049 	txd->data_len = cpu_to_le16(skb->len);
1050 
1051 	txd->flags = 0;
1052 	txd->mss = 0;
1053 	txd->lso_hdrlen = 0;
1054 
1055 	/* Do not reorder - tso may adjust pkt cnt, vlan may override fields */
1056 	nfp_net_tx_tso(r_vec, txbuf, txd, skb, md_bytes);
1057 	nfp_net_tx_csum(dp, r_vec, txbuf, txd, skb);
1058 	if (skb_vlan_tag_present(skb) && dp->ctrl & NFP_NET_CFG_CTRL_TXVLAN) {
1059 		txd->flags |= PCIE_DESC_TX_VLAN;
1060 		txd->vlan = cpu_to_le16(skb_vlan_tag_get(skb));
1061 	}
1062 
1063 	/* Gather DMA */
1064 	if (nr_frags > 0) {
1065 		__le64 second_half;
1066 
1067 		/* all descs must match except for in addr, length and eop */
1068 		second_half = txd->vals8[1];
1069 
1070 		for (f = 0; f < nr_frags; f++) {
1071 			frag = &skb_shinfo(skb)->frags[f];
1072 			fsize = skb_frag_size(frag);
1073 
1074 			dma_addr = skb_frag_dma_map(dp->dev, frag, 0,
1075 						    fsize, DMA_TO_DEVICE);
1076 			if (dma_mapping_error(dp->dev, dma_addr))
1077 				goto err_unmap;
1078 
1079 			wr_idx = D_IDX(tx_ring, wr_idx + 1);
1080 			tx_ring->txbufs[wr_idx].skb = skb;
1081 			tx_ring->txbufs[wr_idx].dma_addr = dma_addr;
1082 			tx_ring->txbufs[wr_idx].fidx = f;
1083 
1084 			txd = &tx_ring->txds[wr_idx];
1085 			txd->dma_len = cpu_to_le16(fsize);
1086 			nfp_desc_set_dma_addr(txd, dma_addr);
1087 			txd->offset_eop = md_bytes |
1088 				((f == nr_frags - 1) ? PCIE_DESC_TX_EOP : 0);
1089 			txd->vals8[1] = second_half;
1090 		}
1091 
1092 		u64_stats_update_begin(&r_vec->tx_sync);
1093 		r_vec->tx_gather++;
1094 		u64_stats_update_end(&r_vec->tx_sync);
1095 	}
1096 
1097 	skb_tx_timestamp(skb);
1098 
1099 	nd_q = netdev_get_tx_queue(dp->netdev, tx_ring->idx);
1100 
1101 	tx_ring->wr_p += nr_frags + 1;
1102 	if (nfp_net_tx_ring_should_stop(tx_ring))
1103 		nfp_net_tx_ring_stop(nd_q, tx_ring);
1104 
1105 	tx_ring->wr_ptr_add += nr_frags + 1;
1106 	if (__netdev_tx_sent_queue(nd_q, txbuf->real_len, netdev_xmit_more()))
1107 		nfp_net_tx_xmit_more_flush(tx_ring);
1108 
1109 	return NETDEV_TX_OK;
1110 
1111 err_unmap:
1112 	while (--f >= 0) {
1113 		frag = &skb_shinfo(skb)->frags[f];
1114 		dma_unmap_page(dp->dev, tx_ring->txbufs[wr_idx].dma_addr,
1115 			       skb_frag_size(frag), DMA_TO_DEVICE);
1116 		tx_ring->txbufs[wr_idx].skb = NULL;
1117 		tx_ring->txbufs[wr_idx].dma_addr = 0;
1118 		tx_ring->txbufs[wr_idx].fidx = -2;
1119 		wr_idx = wr_idx - 1;
1120 		if (wr_idx < 0)
1121 			wr_idx += tx_ring->cnt;
1122 	}
1123 	dma_unmap_single(dp->dev, tx_ring->txbufs[wr_idx].dma_addr,
1124 			 skb_headlen(skb), DMA_TO_DEVICE);
1125 	tx_ring->txbufs[wr_idx].skb = NULL;
1126 	tx_ring->txbufs[wr_idx].dma_addr = 0;
1127 	tx_ring->txbufs[wr_idx].fidx = -2;
1128 err_dma_err:
1129 	nn_dp_warn(dp, "Failed to map DMA TX buffer\n");
1130 err_flush:
1131 	nfp_net_tx_xmit_more_flush(tx_ring);
1132 	u64_stats_update_begin(&r_vec->tx_sync);
1133 	r_vec->tx_errors++;
1134 	u64_stats_update_end(&r_vec->tx_sync);
1135 	nfp_net_tls_tx_undo(skb, tls_handle);
1136 	dev_kfree_skb_any(skb);
1137 	return NETDEV_TX_OK;
1138 }
1139 
1140 /**
1141  * nfp_net_tx_complete() - Handled completed TX packets
1142  * @tx_ring:	TX ring structure
1143  * @budget:	NAPI budget (only used as bool to determine if in NAPI context)
1144  */
nfp_net_tx_complete(struct nfp_net_tx_ring * tx_ring,int budget)1145 static void nfp_net_tx_complete(struct nfp_net_tx_ring *tx_ring, int budget)
1146 {
1147 	struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
1148 	struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
1149 	struct netdev_queue *nd_q;
1150 	u32 done_pkts = 0, done_bytes = 0;
1151 	u32 qcp_rd_p;
1152 	int todo;
1153 
1154 	if (tx_ring->wr_p == tx_ring->rd_p)
1155 		return;
1156 
1157 	/* Work out how many descriptors have been transmitted */
1158 	qcp_rd_p = nfp_qcp_rd_ptr_read(tx_ring->qcp_q);
1159 
1160 	if (qcp_rd_p == tx_ring->qcp_rd_p)
1161 		return;
1162 
1163 	todo = D_IDX(tx_ring, qcp_rd_p - tx_ring->qcp_rd_p);
1164 
1165 	while (todo--) {
1166 		const skb_frag_t *frag;
1167 		struct nfp_net_tx_buf *tx_buf;
1168 		struct sk_buff *skb;
1169 		int fidx, nr_frags;
1170 		int idx;
1171 
1172 		idx = D_IDX(tx_ring, tx_ring->rd_p++);
1173 		tx_buf = &tx_ring->txbufs[idx];
1174 
1175 		skb = tx_buf->skb;
1176 		if (!skb)
1177 			continue;
1178 
1179 		nr_frags = skb_shinfo(skb)->nr_frags;
1180 		fidx = tx_buf->fidx;
1181 
1182 		if (fidx == -1) {
1183 			/* unmap head */
1184 			dma_unmap_single(dp->dev, tx_buf->dma_addr,
1185 					 skb_headlen(skb), DMA_TO_DEVICE);
1186 
1187 			done_pkts += tx_buf->pkt_cnt;
1188 			done_bytes += tx_buf->real_len;
1189 		} else {
1190 			/* unmap fragment */
1191 			frag = &skb_shinfo(skb)->frags[fidx];
1192 			dma_unmap_page(dp->dev, tx_buf->dma_addr,
1193 				       skb_frag_size(frag), DMA_TO_DEVICE);
1194 		}
1195 
1196 		/* check for last gather fragment */
1197 		if (fidx == nr_frags - 1)
1198 			napi_consume_skb(skb, budget);
1199 
1200 		tx_buf->dma_addr = 0;
1201 		tx_buf->skb = NULL;
1202 		tx_buf->fidx = -2;
1203 	}
1204 
1205 	tx_ring->qcp_rd_p = qcp_rd_p;
1206 
1207 	u64_stats_update_begin(&r_vec->tx_sync);
1208 	r_vec->tx_bytes += done_bytes;
1209 	r_vec->tx_pkts += done_pkts;
1210 	u64_stats_update_end(&r_vec->tx_sync);
1211 
1212 	if (!dp->netdev)
1213 		return;
1214 
1215 	nd_q = netdev_get_tx_queue(dp->netdev, tx_ring->idx);
1216 	netdev_tx_completed_queue(nd_q, done_pkts, done_bytes);
1217 	if (nfp_net_tx_ring_should_wake(tx_ring)) {
1218 		/* Make sure TX thread will see updated tx_ring->rd_p */
1219 		smp_mb();
1220 
1221 		if (unlikely(netif_tx_queue_stopped(nd_q)))
1222 			netif_tx_wake_queue(nd_q);
1223 	}
1224 
1225 	WARN_ONCE(tx_ring->wr_p - tx_ring->rd_p > tx_ring->cnt,
1226 		  "TX ring corruption rd_p=%u wr_p=%u cnt=%u\n",
1227 		  tx_ring->rd_p, tx_ring->wr_p, tx_ring->cnt);
1228 }
1229 
nfp_net_xdp_complete(struct nfp_net_tx_ring * tx_ring)1230 static bool nfp_net_xdp_complete(struct nfp_net_tx_ring *tx_ring)
1231 {
1232 	struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
1233 	u32 done_pkts = 0, done_bytes = 0;
1234 	bool done_all;
1235 	int idx, todo;
1236 	u32 qcp_rd_p;
1237 
1238 	/* Work out how many descriptors have been transmitted */
1239 	qcp_rd_p = nfp_qcp_rd_ptr_read(tx_ring->qcp_q);
1240 
1241 	if (qcp_rd_p == tx_ring->qcp_rd_p)
1242 		return true;
1243 
1244 	todo = D_IDX(tx_ring, qcp_rd_p - tx_ring->qcp_rd_p);
1245 
1246 	done_all = todo <= NFP_NET_XDP_MAX_COMPLETE;
1247 	todo = min(todo, NFP_NET_XDP_MAX_COMPLETE);
1248 
1249 	tx_ring->qcp_rd_p = D_IDX(tx_ring, tx_ring->qcp_rd_p + todo);
1250 
1251 	done_pkts = todo;
1252 	while (todo--) {
1253 		idx = D_IDX(tx_ring, tx_ring->rd_p);
1254 		tx_ring->rd_p++;
1255 
1256 		done_bytes += tx_ring->txbufs[idx].real_len;
1257 	}
1258 
1259 	u64_stats_update_begin(&r_vec->tx_sync);
1260 	r_vec->tx_bytes += done_bytes;
1261 	r_vec->tx_pkts += done_pkts;
1262 	u64_stats_update_end(&r_vec->tx_sync);
1263 
1264 	WARN_ONCE(tx_ring->wr_p - tx_ring->rd_p > tx_ring->cnt,
1265 		  "XDP TX ring corruption rd_p=%u wr_p=%u cnt=%u\n",
1266 		  tx_ring->rd_p, tx_ring->wr_p, tx_ring->cnt);
1267 
1268 	return done_all;
1269 }
1270 
1271 /**
1272  * nfp_net_tx_ring_reset() - Free any untransmitted buffers and reset pointers
1273  * @dp:		NFP Net data path struct
1274  * @tx_ring:	TX ring structure
1275  *
1276  * Assumes that the device is stopped, must be idempotent.
1277  */
1278 static void
nfp_net_tx_ring_reset(struct nfp_net_dp * dp,struct nfp_net_tx_ring * tx_ring)1279 nfp_net_tx_ring_reset(struct nfp_net_dp *dp, struct nfp_net_tx_ring *tx_ring)
1280 {
1281 	const skb_frag_t *frag;
1282 	struct netdev_queue *nd_q;
1283 
1284 	while (!tx_ring->is_xdp && tx_ring->rd_p != tx_ring->wr_p) {
1285 		struct nfp_net_tx_buf *tx_buf;
1286 		struct sk_buff *skb;
1287 		int idx, nr_frags;
1288 
1289 		idx = D_IDX(tx_ring, tx_ring->rd_p);
1290 		tx_buf = &tx_ring->txbufs[idx];
1291 
1292 		skb = tx_ring->txbufs[idx].skb;
1293 		nr_frags = skb_shinfo(skb)->nr_frags;
1294 
1295 		if (tx_buf->fidx == -1) {
1296 			/* unmap head */
1297 			dma_unmap_single(dp->dev, tx_buf->dma_addr,
1298 					 skb_headlen(skb), DMA_TO_DEVICE);
1299 		} else {
1300 			/* unmap fragment */
1301 			frag = &skb_shinfo(skb)->frags[tx_buf->fidx];
1302 			dma_unmap_page(dp->dev, tx_buf->dma_addr,
1303 				       skb_frag_size(frag), DMA_TO_DEVICE);
1304 		}
1305 
1306 		/* check for last gather fragment */
1307 		if (tx_buf->fidx == nr_frags - 1)
1308 			dev_kfree_skb_any(skb);
1309 
1310 		tx_buf->dma_addr = 0;
1311 		tx_buf->skb = NULL;
1312 		tx_buf->fidx = -2;
1313 
1314 		tx_ring->qcp_rd_p++;
1315 		tx_ring->rd_p++;
1316 	}
1317 
1318 	memset(tx_ring->txds, 0, tx_ring->size);
1319 	tx_ring->wr_p = 0;
1320 	tx_ring->rd_p = 0;
1321 	tx_ring->qcp_rd_p = 0;
1322 	tx_ring->wr_ptr_add = 0;
1323 
1324 	if (tx_ring->is_xdp || !dp->netdev)
1325 		return;
1326 
1327 	nd_q = netdev_get_tx_queue(dp->netdev, tx_ring->idx);
1328 	netdev_tx_reset_queue(nd_q);
1329 }
1330 
nfp_net_tx_timeout(struct net_device * netdev,unsigned int txqueue)1331 static void nfp_net_tx_timeout(struct net_device *netdev, unsigned int txqueue)
1332 {
1333 	struct nfp_net *nn = netdev_priv(netdev);
1334 
1335 	nn_warn(nn, "TX watchdog timeout on ring: %u\n", txqueue);
1336 }
1337 
1338 /* Receive processing
1339  */
1340 static unsigned int
nfp_net_calc_fl_bufsz(struct nfp_net_dp * dp)1341 nfp_net_calc_fl_bufsz(struct nfp_net_dp *dp)
1342 {
1343 	unsigned int fl_bufsz;
1344 
1345 	fl_bufsz = NFP_NET_RX_BUF_HEADROOM;
1346 	fl_bufsz += dp->rx_dma_off;
1347 	if (dp->rx_offset == NFP_NET_CFG_RX_OFFSET_DYNAMIC)
1348 		fl_bufsz += NFP_NET_MAX_PREPEND;
1349 	else
1350 		fl_bufsz += dp->rx_offset;
1351 	fl_bufsz += ETH_HLEN + VLAN_HLEN * 2 + dp->mtu;
1352 
1353 	fl_bufsz = SKB_DATA_ALIGN(fl_bufsz);
1354 	fl_bufsz += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1355 
1356 	return fl_bufsz;
1357 }
1358 
1359 static void
nfp_net_free_frag(void * frag,bool xdp)1360 nfp_net_free_frag(void *frag, bool xdp)
1361 {
1362 	if (!xdp)
1363 		skb_free_frag(frag);
1364 	else
1365 		__free_page(virt_to_page(frag));
1366 }
1367 
1368 /**
1369  * nfp_net_rx_alloc_one() - Allocate and map page frag for RX
1370  * @dp:		NFP Net data path struct
1371  * @dma_addr:	Pointer to storage for DMA address (output param)
1372  *
1373  * This function will allcate a new page frag, map it for DMA.
1374  *
1375  * Return: allocated page frag or NULL on failure.
1376  */
nfp_net_rx_alloc_one(struct nfp_net_dp * dp,dma_addr_t * dma_addr)1377 static void *nfp_net_rx_alloc_one(struct nfp_net_dp *dp, dma_addr_t *dma_addr)
1378 {
1379 	void *frag;
1380 
1381 	if (!dp->xdp_prog) {
1382 		frag = netdev_alloc_frag(dp->fl_bufsz);
1383 	} else {
1384 		struct page *page;
1385 
1386 		page = alloc_page(GFP_KERNEL);
1387 		frag = page ? page_address(page) : NULL;
1388 	}
1389 	if (!frag) {
1390 		nn_dp_warn(dp, "Failed to alloc receive page frag\n");
1391 		return NULL;
1392 	}
1393 
1394 	*dma_addr = nfp_net_dma_map_rx(dp, frag);
1395 	if (dma_mapping_error(dp->dev, *dma_addr)) {
1396 		nfp_net_free_frag(frag, dp->xdp_prog);
1397 		nn_dp_warn(dp, "Failed to map DMA RX buffer\n");
1398 		return NULL;
1399 	}
1400 
1401 	return frag;
1402 }
1403 
nfp_net_napi_alloc_one(struct nfp_net_dp * dp,dma_addr_t * dma_addr)1404 static void *nfp_net_napi_alloc_one(struct nfp_net_dp *dp, dma_addr_t *dma_addr)
1405 {
1406 	void *frag;
1407 
1408 	if (!dp->xdp_prog) {
1409 		frag = napi_alloc_frag(dp->fl_bufsz);
1410 		if (unlikely(!frag))
1411 			return NULL;
1412 	} else {
1413 		struct page *page;
1414 
1415 		page = dev_alloc_page();
1416 		if (unlikely(!page))
1417 			return NULL;
1418 		frag = page_address(page);
1419 	}
1420 
1421 	*dma_addr = nfp_net_dma_map_rx(dp, frag);
1422 	if (dma_mapping_error(dp->dev, *dma_addr)) {
1423 		nfp_net_free_frag(frag, dp->xdp_prog);
1424 		nn_dp_warn(dp, "Failed to map DMA RX buffer\n");
1425 		return NULL;
1426 	}
1427 
1428 	return frag;
1429 }
1430 
1431 /**
1432  * nfp_net_rx_give_one() - Put mapped skb on the software and hardware rings
1433  * @dp:		NFP Net data path struct
1434  * @rx_ring:	RX ring structure
1435  * @frag:	page fragment buffer
1436  * @dma_addr:	DMA address of skb mapping
1437  */
nfp_net_rx_give_one(const struct nfp_net_dp * dp,struct nfp_net_rx_ring * rx_ring,void * frag,dma_addr_t dma_addr)1438 static void nfp_net_rx_give_one(const struct nfp_net_dp *dp,
1439 				struct nfp_net_rx_ring *rx_ring,
1440 				void *frag, dma_addr_t dma_addr)
1441 {
1442 	unsigned int wr_idx;
1443 
1444 	wr_idx = D_IDX(rx_ring, rx_ring->wr_p);
1445 
1446 	nfp_net_dma_sync_dev_rx(dp, dma_addr);
1447 
1448 	/* Stash SKB and DMA address away */
1449 	rx_ring->rxbufs[wr_idx].frag = frag;
1450 	rx_ring->rxbufs[wr_idx].dma_addr = dma_addr;
1451 
1452 	/* Fill freelist descriptor */
1453 	rx_ring->rxds[wr_idx].fld.reserved = 0;
1454 	rx_ring->rxds[wr_idx].fld.meta_len_dd = 0;
1455 	nfp_desc_set_dma_addr(&rx_ring->rxds[wr_idx].fld,
1456 			      dma_addr + dp->rx_dma_off);
1457 
1458 	rx_ring->wr_p++;
1459 	if (!(rx_ring->wr_p % NFP_NET_FL_BATCH)) {
1460 		/* Update write pointer of the freelist queue. Make
1461 		 * sure all writes are flushed before telling the hardware.
1462 		 */
1463 		wmb();
1464 		nfp_qcp_wr_ptr_add(rx_ring->qcp_fl, NFP_NET_FL_BATCH);
1465 	}
1466 }
1467 
1468 /**
1469  * nfp_net_rx_ring_reset() - Reflect in SW state of freelist after disable
1470  * @rx_ring:	RX ring structure
1471  *
1472  * Assumes that the device is stopped, must be idempotent.
1473  */
nfp_net_rx_ring_reset(struct nfp_net_rx_ring * rx_ring)1474 static void nfp_net_rx_ring_reset(struct nfp_net_rx_ring *rx_ring)
1475 {
1476 	unsigned int wr_idx, last_idx;
1477 
1478 	/* wr_p == rd_p means ring was never fed FL bufs.  RX rings are always
1479 	 * kept at cnt - 1 FL bufs.
1480 	 */
1481 	if (rx_ring->wr_p == 0 && rx_ring->rd_p == 0)
1482 		return;
1483 
1484 	/* Move the empty entry to the end of the list */
1485 	wr_idx = D_IDX(rx_ring, rx_ring->wr_p);
1486 	last_idx = rx_ring->cnt - 1;
1487 	rx_ring->rxbufs[wr_idx].dma_addr = rx_ring->rxbufs[last_idx].dma_addr;
1488 	rx_ring->rxbufs[wr_idx].frag = rx_ring->rxbufs[last_idx].frag;
1489 	rx_ring->rxbufs[last_idx].dma_addr = 0;
1490 	rx_ring->rxbufs[last_idx].frag = NULL;
1491 
1492 	memset(rx_ring->rxds, 0, rx_ring->size);
1493 	rx_ring->wr_p = 0;
1494 	rx_ring->rd_p = 0;
1495 }
1496 
1497 /**
1498  * nfp_net_rx_ring_bufs_free() - Free any buffers currently on the RX ring
1499  * @dp:		NFP Net data path struct
1500  * @rx_ring:	RX ring to remove buffers from
1501  *
1502  * Assumes that the device is stopped and buffers are in [0, ring->cnt - 1)
1503  * entries.  After device is disabled nfp_net_rx_ring_reset() must be called
1504  * to restore required ring geometry.
1505  */
1506 static void
nfp_net_rx_ring_bufs_free(struct nfp_net_dp * dp,struct nfp_net_rx_ring * rx_ring)1507 nfp_net_rx_ring_bufs_free(struct nfp_net_dp *dp,
1508 			  struct nfp_net_rx_ring *rx_ring)
1509 {
1510 	unsigned int i;
1511 
1512 	for (i = 0; i < rx_ring->cnt - 1; i++) {
1513 		/* NULL skb can only happen when initial filling of the ring
1514 		 * fails to allocate enough buffers and calls here to free
1515 		 * already allocated ones.
1516 		 */
1517 		if (!rx_ring->rxbufs[i].frag)
1518 			continue;
1519 
1520 		nfp_net_dma_unmap_rx(dp, rx_ring->rxbufs[i].dma_addr);
1521 		nfp_net_free_frag(rx_ring->rxbufs[i].frag, dp->xdp_prog);
1522 		rx_ring->rxbufs[i].dma_addr = 0;
1523 		rx_ring->rxbufs[i].frag = NULL;
1524 	}
1525 }
1526 
1527 /**
1528  * nfp_net_rx_ring_bufs_alloc() - Fill RX ring with buffers (don't give to FW)
1529  * @dp:		NFP Net data path struct
1530  * @rx_ring:	RX ring to remove buffers from
1531  */
1532 static int
nfp_net_rx_ring_bufs_alloc(struct nfp_net_dp * dp,struct nfp_net_rx_ring * rx_ring)1533 nfp_net_rx_ring_bufs_alloc(struct nfp_net_dp *dp,
1534 			   struct nfp_net_rx_ring *rx_ring)
1535 {
1536 	struct nfp_net_rx_buf *rxbufs;
1537 	unsigned int i;
1538 
1539 	rxbufs = rx_ring->rxbufs;
1540 
1541 	for (i = 0; i < rx_ring->cnt - 1; i++) {
1542 		rxbufs[i].frag = nfp_net_rx_alloc_one(dp, &rxbufs[i].dma_addr);
1543 		if (!rxbufs[i].frag) {
1544 			nfp_net_rx_ring_bufs_free(dp, rx_ring);
1545 			return -ENOMEM;
1546 		}
1547 	}
1548 
1549 	return 0;
1550 }
1551 
1552 /**
1553  * nfp_net_rx_ring_fill_freelist() - Give buffers from the ring to FW
1554  * @dp:	     NFP Net data path struct
1555  * @rx_ring: RX ring to fill
1556  */
1557 static void
nfp_net_rx_ring_fill_freelist(struct nfp_net_dp * dp,struct nfp_net_rx_ring * rx_ring)1558 nfp_net_rx_ring_fill_freelist(struct nfp_net_dp *dp,
1559 			      struct nfp_net_rx_ring *rx_ring)
1560 {
1561 	unsigned int i;
1562 
1563 	for (i = 0; i < rx_ring->cnt - 1; i++)
1564 		nfp_net_rx_give_one(dp, rx_ring, rx_ring->rxbufs[i].frag,
1565 				    rx_ring->rxbufs[i].dma_addr);
1566 }
1567 
1568 /**
1569  * nfp_net_rx_csum_has_errors() - group check if rxd has any csum errors
1570  * @flags: RX descriptor flags field in CPU byte order
1571  */
nfp_net_rx_csum_has_errors(u16 flags)1572 static int nfp_net_rx_csum_has_errors(u16 flags)
1573 {
1574 	u16 csum_all_checked, csum_all_ok;
1575 
1576 	csum_all_checked = flags & __PCIE_DESC_RX_CSUM_ALL;
1577 	csum_all_ok = flags & __PCIE_DESC_RX_CSUM_ALL_OK;
1578 
1579 	return csum_all_checked != (csum_all_ok << PCIE_DESC_RX_CSUM_OK_SHIFT);
1580 }
1581 
1582 /**
1583  * nfp_net_rx_csum() - set SKB checksum field based on RX descriptor flags
1584  * @dp:  NFP Net data path struct
1585  * @r_vec: per-ring structure
1586  * @rxd: Pointer to RX descriptor
1587  * @meta: Parsed metadata prepend
1588  * @skb: Pointer to SKB
1589  */
nfp_net_rx_csum(struct nfp_net_dp * dp,struct nfp_net_r_vector * r_vec,struct nfp_net_rx_desc * rxd,struct nfp_meta_parsed * meta,struct sk_buff * skb)1590 static void nfp_net_rx_csum(struct nfp_net_dp *dp,
1591 			    struct nfp_net_r_vector *r_vec,
1592 			    struct nfp_net_rx_desc *rxd,
1593 			    struct nfp_meta_parsed *meta, struct sk_buff *skb)
1594 {
1595 	skb_checksum_none_assert(skb);
1596 
1597 	if (!(dp->netdev->features & NETIF_F_RXCSUM))
1598 		return;
1599 
1600 	if (meta->csum_type) {
1601 		skb->ip_summed = meta->csum_type;
1602 		skb->csum = meta->csum;
1603 		u64_stats_update_begin(&r_vec->rx_sync);
1604 		r_vec->hw_csum_rx_complete++;
1605 		u64_stats_update_end(&r_vec->rx_sync);
1606 		return;
1607 	}
1608 
1609 	if (nfp_net_rx_csum_has_errors(le16_to_cpu(rxd->rxd.flags))) {
1610 		u64_stats_update_begin(&r_vec->rx_sync);
1611 		r_vec->hw_csum_rx_error++;
1612 		u64_stats_update_end(&r_vec->rx_sync);
1613 		return;
1614 	}
1615 
1616 	/* Assume that the firmware will never report inner CSUM_OK unless outer
1617 	 * L4 headers were successfully parsed. FW will always report zero UDP
1618 	 * checksum as CSUM_OK.
1619 	 */
1620 	if (rxd->rxd.flags & PCIE_DESC_RX_TCP_CSUM_OK ||
1621 	    rxd->rxd.flags & PCIE_DESC_RX_UDP_CSUM_OK) {
1622 		__skb_incr_checksum_unnecessary(skb);
1623 		u64_stats_update_begin(&r_vec->rx_sync);
1624 		r_vec->hw_csum_rx_ok++;
1625 		u64_stats_update_end(&r_vec->rx_sync);
1626 	}
1627 
1628 	if (rxd->rxd.flags & PCIE_DESC_RX_I_TCP_CSUM_OK ||
1629 	    rxd->rxd.flags & PCIE_DESC_RX_I_UDP_CSUM_OK) {
1630 		__skb_incr_checksum_unnecessary(skb);
1631 		u64_stats_update_begin(&r_vec->rx_sync);
1632 		r_vec->hw_csum_rx_inner_ok++;
1633 		u64_stats_update_end(&r_vec->rx_sync);
1634 	}
1635 }
1636 
1637 static void
nfp_net_set_hash(struct net_device * netdev,struct nfp_meta_parsed * meta,unsigned int type,__be32 * hash)1638 nfp_net_set_hash(struct net_device *netdev, struct nfp_meta_parsed *meta,
1639 		 unsigned int type, __be32 *hash)
1640 {
1641 	if (!(netdev->features & NETIF_F_RXHASH))
1642 		return;
1643 
1644 	switch (type) {
1645 	case NFP_NET_RSS_IPV4:
1646 	case NFP_NET_RSS_IPV6:
1647 	case NFP_NET_RSS_IPV6_EX:
1648 		meta->hash_type = PKT_HASH_TYPE_L3;
1649 		break;
1650 	default:
1651 		meta->hash_type = PKT_HASH_TYPE_L4;
1652 		break;
1653 	}
1654 
1655 	meta->hash = get_unaligned_be32(hash);
1656 }
1657 
1658 static void
nfp_net_set_hash_desc(struct net_device * netdev,struct nfp_meta_parsed * meta,void * data,struct nfp_net_rx_desc * rxd)1659 nfp_net_set_hash_desc(struct net_device *netdev, struct nfp_meta_parsed *meta,
1660 		      void *data, struct nfp_net_rx_desc *rxd)
1661 {
1662 	struct nfp_net_rx_hash *rx_hash = data;
1663 
1664 	if (!(rxd->rxd.flags & PCIE_DESC_RX_RSS))
1665 		return;
1666 
1667 	nfp_net_set_hash(netdev, meta, get_unaligned_be32(&rx_hash->hash_type),
1668 			 &rx_hash->hash);
1669 }
1670 
1671 static bool
nfp_net_parse_meta(struct net_device * netdev,struct nfp_meta_parsed * meta,void * data,void * pkt,unsigned int pkt_len,int meta_len)1672 nfp_net_parse_meta(struct net_device *netdev, struct nfp_meta_parsed *meta,
1673 		   void *data, void *pkt, unsigned int pkt_len, int meta_len)
1674 {
1675 	u32 meta_info;
1676 
1677 	meta_info = get_unaligned_be32(data);
1678 	data += 4;
1679 
1680 	while (meta_info) {
1681 		switch (meta_info & NFP_NET_META_FIELD_MASK) {
1682 		case NFP_NET_META_HASH:
1683 			meta_info >>= NFP_NET_META_FIELD_SIZE;
1684 			nfp_net_set_hash(netdev, meta,
1685 					 meta_info & NFP_NET_META_FIELD_MASK,
1686 					 (__be32 *)data);
1687 			data += 4;
1688 			break;
1689 		case NFP_NET_META_MARK:
1690 			meta->mark = get_unaligned_be32(data);
1691 			data += 4;
1692 			break;
1693 		case NFP_NET_META_PORTID:
1694 			meta->portid = get_unaligned_be32(data);
1695 			data += 4;
1696 			break;
1697 		case NFP_NET_META_CSUM:
1698 			meta->csum_type = CHECKSUM_COMPLETE;
1699 			meta->csum =
1700 				(__force __wsum)__get_unaligned_cpu32(data);
1701 			data += 4;
1702 			break;
1703 		case NFP_NET_META_RESYNC_INFO:
1704 			if (nfp_net_tls_rx_resync_req(netdev, data, pkt,
1705 						      pkt_len))
1706 				return false;
1707 			data += sizeof(struct nfp_net_tls_resync_req);
1708 			break;
1709 		default:
1710 			return true;
1711 		}
1712 
1713 		meta_info >>= NFP_NET_META_FIELD_SIZE;
1714 	}
1715 
1716 	return data != pkt;
1717 }
1718 
1719 static void
nfp_net_rx_drop(const struct nfp_net_dp * dp,struct nfp_net_r_vector * r_vec,struct nfp_net_rx_ring * rx_ring,struct nfp_net_rx_buf * rxbuf,struct sk_buff * skb)1720 nfp_net_rx_drop(const struct nfp_net_dp *dp, struct nfp_net_r_vector *r_vec,
1721 		struct nfp_net_rx_ring *rx_ring, struct nfp_net_rx_buf *rxbuf,
1722 		struct sk_buff *skb)
1723 {
1724 	u64_stats_update_begin(&r_vec->rx_sync);
1725 	r_vec->rx_drops++;
1726 	/* If we have both skb and rxbuf the replacement buffer allocation
1727 	 * must have failed, count this as an alloc failure.
1728 	 */
1729 	if (skb && rxbuf)
1730 		r_vec->rx_replace_buf_alloc_fail++;
1731 	u64_stats_update_end(&r_vec->rx_sync);
1732 
1733 	/* skb is build based on the frag, free_skb() would free the frag
1734 	 * so to be able to reuse it we need an extra ref.
1735 	 */
1736 	if (skb && rxbuf && skb->head == rxbuf->frag)
1737 		page_ref_inc(virt_to_head_page(rxbuf->frag));
1738 	if (rxbuf)
1739 		nfp_net_rx_give_one(dp, rx_ring, rxbuf->frag, rxbuf->dma_addr);
1740 	if (skb)
1741 		dev_kfree_skb_any(skb);
1742 }
1743 
1744 static bool
nfp_net_tx_xdp_buf(struct nfp_net_dp * dp,struct nfp_net_rx_ring * rx_ring,struct nfp_net_tx_ring * tx_ring,struct nfp_net_rx_buf * rxbuf,unsigned int dma_off,unsigned int pkt_len,bool * completed)1745 nfp_net_tx_xdp_buf(struct nfp_net_dp *dp, struct nfp_net_rx_ring *rx_ring,
1746 		   struct nfp_net_tx_ring *tx_ring,
1747 		   struct nfp_net_rx_buf *rxbuf, unsigned int dma_off,
1748 		   unsigned int pkt_len, bool *completed)
1749 {
1750 	unsigned int dma_map_sz = dp->fl_bufsz - NFP_NET_RX_BUF_NON_DATA;
1751 	struct nfp_net_tx_buf *txbuf;
1752 	struct nfp_net_tx_desc *txd;
1753 	int wr_idx;
1754 
1755 	/* Reject if xdp_adjust_tail grow packet beyond DMA area */
1756 	if (pkt_len + dma_off > dma_map_sz)
1757 		return false;
1758 
1759 	if (unlikely(nfp_net_tx_full(tx_ring, 1))) {
1760 		if (!*completed) {
1761 			nfp_net_xdp_complete(tx_ring);
1762 			*completed = true;
1763 		}
1764 
1765 		if (unlikely(nfp_net_tx_full(tx_ring, 1))) {
1766 			nfp_net_rx_drop(dp, rx_ring->r_vec, rx_ring, rxbuf,
1767 					NULL);
1768 			return false;
1769 		}
1770 	}
1771 
1772 	wr_idx = D_IDX(tx_ring, tx_ring->wr_p);
1773 
1774 	/* Stash the soft descriptor of the head then initialize it */
1775 	txbuf = &tx_ring->txbufs[wr_idx];
1776 
1777 	nfp_net_rx_give_one(dp, rx_ring, txbuf->frag, txbuf->dma_addr);
1778 
1779 	txbuf->frag = rxbuf->frag;
1780 	txbuf->dma_addr = rxbuf->dma_addr;
1781 	txbuf->fidx = -1;
1782 	txbuf->pkt_cnt = 1;
1783 	txbuf->real_len = pkt_len;
1784 
1785 	dma_sync_single_for_device(dp->dev, rxbuf->dma_addr + dma_off,
1786 				   pkt_len, DMA_BIDIRECTIONAL);
1787 
1788 	/* Build TX descriptor */
1789 	txd = &tx_ring->txds[wr_idx];
1790 	txd->offset_eop = PCIE_DESC_TX_EOP;
1791 	txd->dma_len = cpu_to_le16(pkt_len);
1792 	nfp_desc_set_dma_addr(txd, rxbuf->dma_addr + dma_off);
1793 	txd->data_len = cpu_to_le16(pkt_len);
1794 
1795 	txd->flags = 0;
1796 	txd->mss = 0;
1797 	txd->lso_hdrlen = 0;
1798 
1799 	tx_ring->wr_p++;
1800 	tx_ring->wr_ptr_add++;
1801 	return true;
1802 }
1803 
1804 /**
1805  * nfp_net_rx() - receive up to @budget packets on @rx_ring
1806  * @rx_ring:   RX ring to receive from
1807  * @budget:    NAPI budget
1808  *
1809  * Note, this function is separated out from the napi poll function to
1810  * more cleanly separate packet receive code from other bookkeeping
1811  * functions performed in the napi poll function.
1812  *
1813  * Return: Number of packets received.
1814  */
nfp_net_rx(struct nfp_net_rx_ring * rx_ring,int budget)1815 static int nfp_net_rx(struct nfp_net_rx_ring *rx_ring, int budget)
1816 {
1817 	struct nfp_net_r_vector *r_vec = rx_ring->r_vec;
1818 	struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
1819 	struct nfp_net_tx_ring *tx_ring;
1820 	struct bpf_prog *xdp_prog;
1821 	bool xdp_tx_cmpl = false;
1822 	unsigned int true_bufsz;
1823 	struct sk_buff *skb;
1824 	int pkts_polled = 0;
1825 	struct xdp_buff xdp;
1826 	int idx;
1827 
1828 	xdp_prog = READ_ONCE(dp->xdp_prog);
1829 	true_bufsz = xdp_prog ? PAGE_SIZE : dp->fl_bufsz;
1830 	xdp_init_buff(&xdp, PAGE_SIZE - NFP_NET_RX_BUF_HEADROOM,
1831 		      &rx_ring->xdp_rxq);
1832 	tx_ring = r_vec->xdp_ring;
1833 
1834 	while (pkts_polled < budget) {
1835 		unsigned int meta_len, data_len, meta_off, pkt_len, pkt_off;
1836 		struct nfp_net_rx_buf *rxbuf;
1837 		struct nfp_net_rx_desc *rxd;
1838 		struct nfp_meta_parsed meta;
1839 		bool redir_egress = false;
1840 		struct net_device *netdev;
1841 		dma_addr_t new_dma_addr;
1842 		u32 meta_len_xdp = 0;
1843 		void *new_frag;
1844 
1845 		idx = D_IDX(rx_ring, rx_ring->rd_p);
1846 
1847 		rxd = &rx_ring->rxds[idx];
1848 		if (!(rxd->rxd.meta_len_dd & PCIE_DESC_RX_DD))
1849 			break;
1850 
1851 		/* Memory barrier to ensure that we won't do other reads
1852 		 * before the DD bit.
1853 		 */
1854 		dma_rmb();
1855 
1856 		memset(&meta, 0, sizeof(meta));
1857 
1858 		rx_ring->rd_p++;
1859 		pkts_polled++;
1860 
1861 		rxbuf =	&rx_ring->rxbufs[idx];
1862 		/*         < meta_len >
1863 		 *  <-- [rx_offset] -->
1864 		 *  ---------------------------------------------------------
1865 		 * | [XX] |  metadata  |             packet           | XXXX |
1866 		 *  ---------------------------------------------------------
1867 		 *         <---------------- data_len --------------->
1868 		 *
1869 		 * The rx_offset is fixed for all packets, the meta_len can vary
1870 		 * on a packet by packet basis. If rx_offset is set to zero
1871 		 * (_RX_OFFSET_DYNAMIC) metadata starts at the beginning of the
1872 		 * buffer and is immediately followed by the packet (no [XX]).
1873 		 */
1874 		meta_len = rxd->rxd.meta_len_dd & PCIE_DESC_RX_META_LEN_MASK;
1875 		data_len = le16_to_cpu(rxd->rxd.data_len);
1876 		pkt_len = data_len - meta_len;
1877 
1878 		pkt_off = NFP_NET_RX_BUF_HEADROOM + dp->rx_dma_off;
1879 		if (dp->rx_offset == NFP_NET_CFG_RX_OFFSET_DYNAMIC)
1880 			pkt_off += meta_len;
1881 		else
1882 			pkt_off += dp->rx_offset;
1883 		meta_off = pkt_off - meta_len;
1884 
1885 		/* Stats update */
1886 		u64_stats_update_begin(&r_vec->rx_sync);
1887 		r_vec->rx_pkts++;
1888 		r_vec->rx_bytes += pkt_len;
1889 		u64_stats_update_end(&r_vec->rx_sync);
1890 
1891 		if (unlikely(meta_len > NFP_NET_MAX_PREPEND ||
1892 			     (dp->rx_offset && meta_len > dp->rx_offset))) {
1893 			nn_dp_warn(dp, "oversized RX packet metadata %u\n",
1894 				   meta_len);
1895 			nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
1896 			continue;
1897 		}
1898 
1899 		nfp_net_dma_sync_cpu_rx(dp, rxbuf->dma_addr + meta_off,
1900 					data_len);
1901 
1902 		if (!dp->chained_metadata_format) {
1903 			nfp_net_set_hash_desc(dp->netdev, &meta,
1904 					      rxbuf->frag + meta_off, rxd);
1905 		} else if (meta_len) {
1906 			if (unlikely(nfp_net_parse_meta(dp->netdev, &meta,
1907 							rxbuf->frag + meta_off,
1908 							rxbuf->frag + pkt_off,
1909 							pkt_len, meta_len))) {
1910 				nn_dp_warn(dp, "invalid RX packet metadata\n");
1911 				nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf,
1912 						NULL);
1913 				continue;
1914 			}
1915 		}
1916 
1917 		if (xdp_prog && !meta.portid) {
1918 			void *orig_data = rxbuf->frag + pkt_off;
1919 			unsigned int dma_off;
1920 			int act;
1921 
1922 			xdp_prepare_buff(&xdp,
1923 					 rxbuf->frag + NFP_NET_RX_BUF_HEADROOM,
1924 					 pkt_off - NFP_NET_RX_BUF_HEADROOM,
1925 					 pkt_len, true);
1926 
1927 			act = bpf_prog_run_xdp(xdp_prog, &xdp);
1928 
1929 			pkt_len = xdp.data_end - xdp.data;
1930 			pkt_off += xdp.data - orig_data;
1931 
1932 			switch (act) {
1933 			case XDP_PASS:
1934 				meta_len_xdp = xdp.data - xdp.data_meta;
1935 				break;
1936 			case XDP_TX:
1937 				dma_off = pkt_off - NFP_NET_RX_BUF_HEADROOM;
1938 				if (unlikely(!nfp_net_tx_xdp_buf(dp, rx_ring,
1939 								 tx_ring, rxbuf,
1940 								 dma_off,
1941 								 pkt_len,
1942 								 &xdp_tx_cmpl)))
1943 					trace_xdp_exception(dp->netdev,
1944 							    xdp_prog, act);
1945 				continue;
1946 			default:
1947 				bpf_warn_invalid_xdp_action(act);
1948 				fallthrough;
1949 			case XDP_ABORTED:
1950 				trace_xdp_exception(dp->netdev, xdp_prog, act);
1951 				fallthrough;
1952 			case XDP_DROP:
1953 				nfp_net_rx_give_one(dp, rx_ring, rxbuf->frag,
1954 						    rxbuf->dma_addr);
1955 				continue;
1956 			}
1957 		}
1958 
1959 		if (likely(!meta.portid)) {
1960 			netdev = dp->netdev;
1961 		} else if (meta.portid == NFP_META_PORT_ID_CTRL) {
1962 			struct nfp_net *nn = netdev_priv(dp->netdev);
1963 
1964 			nfp_app_ctrl_rx_raw(nn->app, rxbuf->frag + pkt_off,
1965 					    pkt_len);
1966 			nfp_net_rx_give_one(dp, rx_ring, rxbuf->frag,
1967 					    rxbuf->dma_addr);
1968 			continue;
1969 		} else {
1970 			struct nfp_net *nn;
1971 
1972 			nn = netdev_priv(dp->netdev);
1973 			netdev = nfp_app_dev_get(nn->app, meta.portid,
1974 						 &redir_egress);
1975 			if (unlikely(!netdev)) {
1976 				nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf,
1977 						NULL);
1978 				continue;
1979 			}
1980 
1981 			if (nfp_netdev_is_nfp_repr(netdev))
1982 				nfp_repr_inc_rx_stats(netdev, pkt_len);
1983 		}
1984 
1985 		skb = build_skb(rxbuf->frag, true_bufsz);
1986 		if (unlikely(!skb)) {
1987 			nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
1988 			continue;
1989 		}
1990 		new_frag = nfp_net_napi_alloc_one(dp, &new_dma_addr);
1991 		if (unlikely(!new_frag)) {
1992 			nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, skb);
1993 			continue;
1994 		}
1995 
1996 		nfp_net_dma_unmap_rx(dp, rxbuf->dma_addr);
1997 
1998 		nfp_net_rx_give_one(dp, rx_ring, new_frag, new_dma_addr);
1999 
2000 		skb_reserve(skb, pkt_off);
2001 		skb_put(skb, pkt_len);
2002 
2003 		skb->mark = meta.mark;
2004 		skb_set_hash(skb, meta.hash, meta.hash_type);
2005 
2006 		skb_record_rx_queue(skb, rx_ring->idx);
2007 		skb->protocol = eth_type_trans(skb, netdev);
2008 
2009 		nfp_net_rx_csum(dp, r_vec, rxd, &meta, skb);
2010 
2011 #ifdef CONFIG_TLS_DEVICE
2012 		if (rxd->rxd.flags & PCIE_DESC_RX_DECRYPTED) {
2013 			skb->decrypted = true;
2014 			u64_stats_update_begin(&r_vec->rx_sync);
2015 			r_vec->hw_tls_rx++;
2016 			u64_stats_update_end(&r_vec->rx_sync);
2017 		}
2018 #endif
2019 
2020 		if (rxd->rxd.flags & PCIE_DESC_RX_VLAN)
2021 			__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2022 					       le16_to_cpu(rxd->rxd.vlan));
2023 		if (meta_len_xdp)
2024 			skb_metadata_set(skb, meta_len_xdp);
2025 
2026 		if (likely(!redir_egress)) {
2027 			napi_gro_receive(&rx_ring->r_vec->napi, skb);
2028 		} else {
2029 			skb->dev = netdev;
2030 			skb_reset_network_header(skb);
2031 			__skb_push(skb, ETH_HLEN);
2032 			dev_queue_xmit(skb);
2033 		}
2034 	}
2035 
2036 	if (xdp_prog) {
2037 		if (tx_ring->wr_ptr_add)
2038 			nfp_net_tx_xmit_more_flush(tx_ring);
2039 		else if (unlikely(tx_ring->wr_p != tx_ring->rd_p) &&
2040 			 !xdp_tx_cmpl)
2041 			if (!nfp_net_xdp_complete(tx_ring))
2042 				pkts_polled = budget;
2043 	}
2044 
2045 	return pkts_polled;
2046 }
2047 
2048 /**
2049  * nfp_net_poll() - napi poll function
2050  * @napi:    NAPI structure
2051  * @budget:  NAPI budget
2052  *
2053  * Return: number of packets polled.
2054  */
nfp_net_poll(struct napi_struct * napi,int budget)2055 static int nfp_net_poll(struct napi_struct *napi, int budget)
2056 {
2057 	struct nfp_net_r_vector *r_vec =
2058 		container_of(napi, struct nfp_net_r_vector, napi);
2059 	unsigned int pkts_polled = 0;
2060 
2061 	if (r_vec->tx_ring)
2062 		nfp_net_tx_complete(r_vec->tx_ring, budget);
2063 	if (r_vec->rx_ring)
2064 		pkts_polled = nfp_net_rx(r_vec->rx_ring, budget);
2065 
2066 	if (pkts_polled < budget)
2067 		if (napi_complete_done(napi, pkts_polled))
2068 			nfp_net_irq_unmask(r_vec->nfp_net, r_vec->irq_entry);
2069 
2070 	if (r_vec->nfp_net->rx_coalesce_adapt_on && r_vec->rx_ring) {
2071 		struct dim_sample dim_sample = {};
2072 		unsigned int start;
2073 		u64 pkts, bytes;
2074 
2075 		do {
2076 			start = u64_stats_fetch_begin(&r_vec->rx_sync);
2077 			pkts = r_vec->rx_pkts;
2078 			bytes = r_vec->rx_bytes;
2079 		} while (u64_stats_fetch_retry(&r_vec->rx_sync, start));
2080 
2081 		dim_update_sample(r_vec->event_ctr, pkts, bytes, &dim_sample);
2082 		net_dim(&r_vec->rx_dim, dim_sample);
2083 	}
2084 
2085 	if (r_vec->nfp_net->tx_coalesce_adapt_on && r_vec->tx_ring) {
2086 		struct dim_sample dim_sample = {};
2087 		unsigned int start;
2088 		u64 pkts, bytes;
2089 
2090 		do {
2091 			start = u64_stats_fetch_begin(&r_vec->tx_sync);
2092 			pkts = r_vec->tx_pkts;
2093 			bytes = r_vec->tx_bytes;
2094 		} while (u64_stats_fetch_retry(&r_vec->tx_sync, start));
2095 
2096 		dim_update_sample(r_vec->event_ctr, pkts, bytes, &dim_sample);
2097 		net_dim(&r_vec->tx_dim, dim_sample);
2098 	}
2099 
2100 	return pkts_polled;
2101 }
2102 
2103 /* Control device data path
2104  */
2105 
2106 static bool
nfp_ctrl_tx_one(struct nfp_net * nn,struct nfp_net_r_vector * r_vec,struct sk_buff * skb,bool old)2107 nfp_ctrl_tx_one(struct nfp_net *nn, struct nfp_net_r_vector *r_vec,
2108 		struct sk_buff *skb, bool old)
2109 {
2110 	unsigned int real_len = skb->len, meta_len = 0;
2111 	struct nfp_net_tx_ring *tx_ring;
2112 	struct nfp_net_tx_buf *txbuf;
2113 	struct nfp_net_tx_desc *txd;
2114 	struct nfp_net_dp *dp;
2115 	dma_addr_t dma_addr;
2116 	int wr_idx;
2117 
2118 	dp = &r_vec->nfp_net->dp;
2119 	tx_ring = r_vec->tx_ring;
2120 
2121 	if (WARN_ON_ONCE(skb_shinfo(skb)->nr_frags)) {
2122 		nn_dp_warn(dp, "Driver's CTRL TX does not implement gather\n");
2123 		goto err_free;
2124 	}
2125 
2126 	if (unlikely(nfp_net_tx_full(tx_ring, 1))) {
2127 		u64_stats_update_begin(&r_vec->tx_sync);
2128 		r_vec->tx_busy++;
2129 		u64_stats_update_end(&r_vec->tx_sync);
2130 		if (!old)
2131 			__skb_queue_tail(&r_vec->queue, skb);
2132 		else
2133 			__skb_queue_head(&r_vec->queue, skb);
2134 		return true;
2135 	}
2136 
2137 	if (nfp_app_ctrl_has_meta(nn->app)) {
2138 		if (unlikely(skb_headroom(skb) < 8)) {
2139 			nn_dp_warn(dp, "CTRL TX on skb without headroom\n");
2140 			goto err_free;
2141 		}
2142 		meta_len = 8;
2143 		put_unaligned_be32(NFP_META_PORT_ID_CTRL, skb_push(skb, 4));
2144 		put_unaligned_be32(NFP_NET_META_PORTID, skb_push(skb, 4));
2145 	}
2146 
2147 	/* Start with the head skbuf */
2148 	dma_addr = dma_map_single(dp->dev, skb->data, skb_headlen(skb),
2149 				  DMA_TO_DEVICE);
2150 	if (dma_mapping_error(dp->dev, dma_addr))
2151 		goto err_dma_warn;
2152 
2153 	wr_idx = D_IDX(tx_ring, tx_ring->wr_p);
2154 
2155 	/* Stash the soft descriptor of the head then initialize it */
2156 	txbuf = &tx_ring->txbufs[wr_idx];
2157 	txbuf->skb = skb;
2158 	txbuf->dma_addr = dma_addr;
2159 	txbuf->fidx = -1;
2160 	txbuf->pkt_cnt = 1;
2161 	txbuf->real_len = real_len;
2162 
2163 	/* Build TX descriptor */
2164 	txd = &tx_ring->txds[wr_idx];
2165 	txd->offset_eop = meta_len | PCIE_DESC_TX_EOP;
2166 	txd->dma_len = cpu_to_le16(skb_headlen(skb));
2167 	nfp_desc_set_dma_addr(txd, dma_addr);
2168 	txd->data_len = cpu_to_le16(skb->len);
2169 
2170 	txd->flags = 0;
2171 	txd->mss = 0;
2172 	txd->lso_hdrlen = 0;
2173 
2174 	tx_ring->wr_p++;
2175 	tx_ring->wr_ptr_add++;
2176 	nfp_net_tx_xmit_more_flush(tx_ring);
2177 
2178 	return false;
2179 
2180 err_dma_warn:
2181 	nn_dp_warn(dp, "Failed to DMA map TX CTRL buffer\n");
2182 err_free:
2183 	u64_stats_update_begin(&r_vec->tx_sync);
2184 	r_vec->tx_errors++;
2185 	u64_stats_update_end(&r_vec->tx_sync);
2186 	dev_kfree_skb_any(skb);
2187 	return false;
2188 }
2189 
__nfp_ctrl_tx(struct nfp_net * nn,struct sk_buff * skb)2190 bool __nfp_ctrl_tx(struct nfp_net *nn, struct sk_buff *skb)
2191 {
2192 	struct nfp_net_r_vector *r_vec = &nn->r_vecs[0];
2193 
2194 	return nfp_ctrl_tx_one(nn, r_vec, skb, false);
2195 }
2196 
nfp_ctrl_tx(struct nfp_net * nn,struct sk_buff * skb)2197 bool nfp_ctrl_tx(struct nfp_net *nn, struct sk_buff *skb)
2198 {
2199 	struct nfp_net_r_vector *r_vec = &nn->r_vecs[0];
2200 	bool ret;
2201 
2202 	spin_lock_bh(&r_vec->lock);
2203 	ret = nfp_ctrl_tx_one(nn, r_vec, skb, false);
2204 	spin_unlock_bh(&r_vec->lock);
2205 
2206 	return ret;
2207 }
2208 
__nfp_ctrl_tx_queued(struct nfp_net_r_vector * r_vec)2209 static void __nfp_ctrl_tx_queued(struct nfp_net_r_vector *r_vec)
2210 {
2211 	struct sk_buff *skb;
2212 
2213 	while ((skb = __skb_dequeue(&r_vec->queue)))
2214 		if (nfp_ctrl_tx_one(r_vec->nfp_net, r_vec, skb, true))
2215 			return;
2216 }
2217 
2218 static bool
nfp_ctrl_meta_ok(struct nfp_net * nn,void * data,unsigned int meta_len)2219 nfp_ctrl_meta_ok(struct nfp_net *nn, void *data, unsigned int meta_len)
2220 {
2221 	u32 meta_type, meta_tag;
2222 
2223 	if (!nfp_app_ctrl_has_meta(nn->app))
2224 		return !meta_len;
2225 
2226 	if (meta_len != 8)
2227 		return false;
2228 
2229 	meta_type = get_unaligned_be32(data);
2230 	meta_tag = get_unaligned_be32(data + 4);
2231 
2232 	return (meta_type == NFP_NET_META_PORTID &&
2233 		meta_tag == NFP_META_PORT_ID_CTRL);
2234 }
2235 
2236 static bool
nfp_ctrl_rx_one(struct nfp_net * nn,struct nfp_net_dp * dp,struct nfp_net_r_vector * r_vec,struct nfp_net_rx_ring * rx_ring)2237 nfp_ctrl_rx_one(struct nfp_net *nn, struct nfp_net_dp *dp,
2238 		struct nfp_net_r_vector *r_vec, struct nfp_net_rx_ring *rx_ring)
2239 {
2240 	unsigned int meta_len, data_len, meta_off, pkt_len, pkt_off;
2241 	struct nfp_net_rx_buf *rxbuf;
2242 	struct nfp_net_rx_desc *rxd;
2243 	dma_addr_t new_dma_addr;
2244 	struct sk_buff *skb;
2245 	void *new_frag;
2246 	int idx;
2247 
2248 	idx = D_IDX(rx_ring, rx_ring->rd_p);
2249 
2250 	rxd = &rx_ring->rxds[idx];
2251 	if (!(rxd->rxd.meta_len_dd & PCIE_DESC_RX_DD))
2252 		return false;
2253 
2254 	/* Memory barrier to ensure that we won't do other reads
2255 	 * before the DD bit.
2256 	 */
2257 	dma_rmb();
2258 
2259 	rx_ring->rd_p++;
2260 
2261 	rxbuf =	&rx_ring->rxbufs[idx];
2262 	meta_len = rxd->rxd.meta_len_dd & PCIE_DESC_RX_META_LEN_MASK;
2263 	data_len = le16_to_cpu(rxd->rxd.data_len);
2264 	pkt_len = data_len - meta_len;
2265 
2266 	pkt_off = NFP_NET_RX_BUF_HEADROOM + dp->rx_dma_off;
2267 	if (dp->rx_offset == NFP_NET_CFG_RX_OFFSET_DYNAMIC)
2268 		pkt_off += meta_len;
2269 	else
2270 		pkt_off += dp->rx_offset;
2271 	meta_off = pkt_off - meta_len;
2272 
2273 	/* Stats update */
2274 	u64_stats_update_begin(&r_vec->rx_sync);
2275 	r_vec->rx_pkts++;
2276 	r_vec->rx_bytes += pkt_len;
2277 	u64_stats_update_end(&r_vec->rx_sync);
2278 
2279 	nfp_net_dma_sync_cpu_rx(dp, rxbuf->dma_addr + meta_off,	data_len);
2280 
2281 	if (unlikely(!nfp_ctrl_meta_ok(nn, rxbuf->frag + meta_off, meta_len))) {
2282 		nn_dp_warn(dp, "incorrect metadata for ctrl packet (%d)\n",
2283 			   meta_len);
2284 		nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
2285 		return true;
2286 	}
2287 
2288 	skb = build_skb(rxbuf->frag, dp->fl_bufsz);
2289 	if (unlikely(!skb)) {
2290 		nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
2291 		return true;
2292 	}
2293 	new_frag = nfp_net_napi_alloc_one(dp, &new_dma_addr);
2294 	if (unlikely(!new_frag)) {
2295 		nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, skb);
2296 		return true;
2297 	}
2298 
2299 	nfp_net_dma_unmap_rx(dp, rxbuf->dma_addr);
2300 
2301 	nfp_net_rx_give_one(dp, rx_ring, new_frag, new_dma_addr);
2302 
2303 	skb_reserve(skb, pkt_off);
2304 	skb_put(skb, pkt_len);
2305 
2306 	nfp_app_ctrl_rx(nn->app, skb);
2307 
2308 	return true;
2309 }
2310 
nfp_ctrl_rx(struct nfp_net_r_vector * r_vec)2311 static bool nfp_ctrl_rx(struct nfp_net_r_vector *r_vec)
2312 {
2313 	struct nfp_net_rx_ring *rx_ring = r_vec->rx_ring;
2314 	struct nfp_net *nn = r_vec->nfp_net;
2315 	struct nfp_net_dp *dp = &nn->dp;
2316 	unsigned int budget = 512;
2317 
2318 	while (nfp_ctrl_rx_one(nn, dp, r_vec, rx_ring) && budget--)
2319 		continue;
2320 
2321 	return budget;
2322 }
2323 
nfp_ctrl_poll(struct tasklet_struct * t)2324 static void nfp_ctrl_poll(struct tasklet_struct *t)
2325 {
2326 	struct nfp_net_r_vector *r_vec = from_tasklet(r_vec, t, tasklet);
2327 
2328 	spin_lock(&r_vec->lock);
2329 	nfp_net_tx_complete(r_vec->tx_ring, 0);
2330 	__nfp_ctrl_tx_queued(r_vec);
2331 	spin_unlock(&r_vec->lock);
2332 
2333 	if (nfp_ctrl_rx(r_vec)) {
2334 		nfp_net_irq_unmask(r_vec->nfp_net, r_vec->irq_entry);
2335 	} else {
2336 		tasklet_schedule(&r_vec->tasklet);
2337 		nn_dp_warn(&r_vec->nfp_net->dp,
2338 			   "control message budget exceeded!\n");
2339 	}
2340 }
2341 
2342 /* Setup and Configuration
2343  */
2344 
2345 /**
2346  * nfp_net_vecs_init() - Assign IRQs and setup rvecs.
2347  * @nn:		NFP Network structure
2348  */
nfp_net_vecs_init(struct nfp_net * nn)2349 static void nfp_net_vecs_init(struct nfp_net *nn)
2350 {
2351 	struct nfp_net_r_vector *r_vec;
2352 	int r;
2353 
2354 	nn->lsc_handler = nfp_net_irq_lsc;
2355 	nn->exn_handler = nfp_net_irq_exn;
2356 
2357 	for (r = 0; r < nn->max_r_vecs; r++) {
2358 		struct msix_entry *entry;
2359 
2360 		entry = &nn->irq_entries[NFP_NET_NON_Q_VECTORS + r];
2361 
2362 		r_vec = &nn->r_vecs[r];
2363 		r_vec->nfp_net = nn;
2364 		r_vec->irq_entry = entry->entry;
2365 		r_vec->irq_vector = entry->vector;
2366 
2367 		if (nn->dp.netdev) {
2368 			r_vec->handler = nfp_net_irq_rxtx;
2369 		} else {
2370 			r_vec->handler = nfp_ctrl_irq_rxtx;
2371 
2372 			__skb_queue_head_init(&r_vec->queue);
2373 			spin_lock_init(&r_vec->lock);
2374 			tasklet_setup(&r_vec->tasklet, nfp_ctrl_poll);
2375 			tasklet_disable(&r_vec->tasklet);
2376 		}
2377 
2378 		cpumask_set_cpu(r, &r_vec->affinity_mask);
2379 	}
2380 }
2381 
2382 /**
2383  * nfp_net_tx_ring_free() - Free resources allocated to a TX ring
2384  * @tx_ring:   TX ring to free
2385  */
nfp_net_tx_ring_free(struct nfp_net_tx_ring * tx_ring)2386 static void nfp_net_tx_ring_free(struct nfp_net_tx_ring *tx_ring)
2387 {
2388 	struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
2389 	struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
2390 
2391 	kvfree(tx_ring->txbufs);
2392 
2393 	if (tx_ring->txds)
2394 		dma_free_coherent(dp->dev, tx_ring->size,
2395 				  tx_ring->txds, tx_ring->dma);
2396 
2397 	tx_ring->cnt = 0;
2398 	tx_ring->txbufs = NULL;
2399 	tx_ring->txds = NULL;
2400 	tx_ring->dma = 0;
2401 	tx_ring->size = 0;
2402 }
2403 
2404 /**
2405  * nfp_net_tx_ring_alloc() - Allocate resource for a TX ring
2406  * @dp:        NFP Net data path struct
2407  * @tx_ring:   TX Ring structure to allocate
2408  *
2409  * Return: 0 on success, negative errno otherwise.
2410  */
2411 static int
nfp_net_tx_ring_alloc(struct nfp_net_dp * dp,struct nfp_net_tx_ring * tx_ring)2412 nfp_net_tx_ring_alloc(struct nfp_net_dp *dp, struct nfp_net_tx_ring *tx_ring)
2413 {
2414 	struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
2415 
2416 	tx_ring->cnt = dp->txd_cnt;
2417 
2418 	tx_ring->size = array_size(tx_ring->cnt, sizeof(*tx_ring->txds));
2419 	tx_ring->txds = dma_alloc_coherent(dp->dev, tx_ring->size,
2420 					   &tx_ring->dma,
2421 					   GFP_KERNEL | __GFP_NOWARN);
2422 	if (!tx_ring->txds) {
2423 		netdev_warn(dp->netdev, "failed to allocate TX descriptor ring memory, requested descriptor count: %d, consider lowering descriptor count\n",
2424 			    tx_ring->cnt);
2425 		goto err_alloc;
2426 	}
2427 
2428 	tx_ring->txbufs = kvcalloc(tx_ring->cnt, sizeof(*tx_ring->txbufs),
2429 				   GFP_KERNEL);
2430 	if (!tx_ring->txbufs)
2431 		goto err_alloc;
2432 
2433 	if (!tx_ring->is_xdp && dp->netdev)
2434 		netif_set_xps_queue(dp->netdev, &r_vec->affinity_mask,
2435 				    tx_ring->idx);
2436 
2437 	return 0;
2438 
2439 err_alloc:
2440 	nfp_net_tx_ring_free(tx_ring);
2441 	return -ENOMEM;
2442 }
2443 
2444 static void
nfp_net_tx_ring_bufs_free(struct nfp_net_dp * dp,struct nfp_net_tx_ring * tx_ring)2445 nfp_net_tx_ring_bufs_free(struct nfp_net_dp *dp,
2446 			  struct nfp_net_tx_ring *tx_ring)
2447 {
2448 	unsigned int i;
2449 
2450 	if (!tx_ring->is_xdp)
2451 		return;
2452 
2453 	for (i = 0; i < tx_ring->cnt; i++) {
2454 		if (!tx_ring->txbufs[i].frag)
2455 			return;
2456 
2457 		nfp_net_dma_unmap_rx(dp, tx_ring->txbufs[i].dma_addr);
2458 		__free_page(virt_to_page(tx_ring->txbufs[i].frag));
2459 	}
2460 }
2461 
2462 static int
nfp_net_tx_ring_bufs_alloc(struct nfp_net_dp * dp,struct nfp_net_tx_ring * tx_ring)2463 nfp_net_tx_ring_bufs_alloc(struct nfp_net_dp *dp,
2464 			   struct nfp_net_tx_ring *tx_ring)
2465 {
2466 	struct nfp_net_tx_buf *txbufs = tx_ring->txbufs;
2467 	unsigned int i;
2468 
2469 	if (!tx_ring->is_xdp)
2470 		return 0;
2471 
2472 	for (i = 0; i < tx_ring->cnt; i++) {
2473 		txbufs[i].frag = nfp_net_rx_alloc_one(dp, &txbufs[i].dma_addr);
2474 		if (!txbufs[i].frag) {
2475 			nfp_net_tx_ring_bufs_free(dp, tx_ring);
2476 			return -ENOMEM;
2477 		}
2478 	}
2479 
2480 	return 0;
2481 }
2482 
nfp_net_tx_rings_prepare(struct nfp_net * nn,struct nfp_net_dp * dp)2483 static int nfp_net_tx_rings_prepare(struct nfp_net *nn, struct nfp_net_dp *dp)
2484 {
2485 	unsigned int r;
2486 
2487 	dp->tx_rings = kcalloc(dp->num_tx_rings, sizeof(*dp->tx_rings),
2488 			       GFP_KERNEL);
2489 	if (!dp->tx_rings)
2490 		return -ENOMEM;
2491 
2492 	for (r = 0; r < dp->num_tx_rings; r++) {
2493 		int bias = 0;
2494 
2495 		if (r >= dp->num_stack_tx_rings)
2496 			bias = dp->num_stack_tx_rings;
2497 
2498 		nfp_net_tx_ring_init(&dp->tx_rings[r], &nn->r_vecs[r - bias],
2499 				     r, bias);
2500 
2501 		if (nfp_net_tx_ring_alloc(dp, &dp->tx_rings[r]))
2502 			goto err_free_prev;
2503 
2504 		if (nfp_net_tx_ring_bufs_alloc(dp, &dp->tx_rings[r]))
2505 			goto err_free_ring;
2506 	}
2507 
2508 	return 0;
2509 
2510 err_free_prev:
2511 	while (r--) {
2512 		nfp_net_tx_ring_bufs_free(dp, &dp->tx_rings[r]);
2513 err_free_ring:
2514 		nfp_net_tx_ring_free(&dp->tx_rings[r]);
2515 	}
2516 	kfree(dp->tx_rings);
2517 	return -ENOMEM;
2518 }
2519 
nfp_net_tx_rings_free(struct nfp_net_dp * dp)2520 static void nfp_net_tx_rings_free(struct nfp_net_dp *dp)
2521 {
2522 	unsigned int r;
2523 
2524 	for (r = 0; r < dp->num_tx_rings; r++) {
2525 		nfp_net_tx_ring_bufs_free(dp, &dp->tx_rings[r]);
2526 		nfp_net_tx_ring_free(&dp->tx_rings[r]);
2527 	}
2528 
2529 	kfree(dp->tx_rings);
2530 }
2531 
2532 /**
2533  * nfp_net_rx_ring_free() - Free resources allocated to a RX ring
2534  * @rx_ring:  RX ring to free
2535  */
nfp_net_rx_ring_free(struct nfp_net_rx_ring * rx_ring)2536 static void nfp_net_rx_ring_free(struct nfp_net_rx_ring *rx_ring)
2537 {
2538 	struct nfp_net_r_vector *r_vec = rx_ring->r_vec;
2539 	struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
2540 
2541 	if (dp->netdev)
2542 		xdp_rxq_info_unreg(&rx_ring->xdp_rxq);
2543 	kvfree(rx_ring->rxbufs);
2544 
2545 	if (rx_ring->rxds)
2546 		dma_free_coherent(dp->dev, rx_ring->size,
2547 				  rx_ring->rxds, rx_ring->dma);
2548 
2549 	rx_ring->cnt = 0;
2550 	rx_ring->rxbufs = NULL;
2551 	rx_ring->rxds = NULL;
2552 	rx_ring->dma = 0;
2553 	rx_ring->size = 0;
2554 }
2555 
2556 /**
2557  * nfp_net_rx_ring_alloc() - Allocate resource for a RX ring
2558  * @dp:	      NFP Net data path struct
2559  * @rx_ring:  RX ring to allocate
2560  *
2561  * Return: 0 on success, negative errno otherwise.
2562  */
2563 static int
nfp_net_rx_ring_alloc(struct nfp_net_dp * dp,struct nfp_net_rx_ring * rx_ring)2564 nfp_net_rx_ring_alloc(struct nfp_net_dp *dp, struct nfp_net_rx_ring *rx_ring)
2565 {
2566 	int err;
2567 
2568 	if (dp->netdev) {
2569 		err = xdp_rxq_info_reg(&rx_ring->xdp_rxq, dp->netdev,
2570 				       rx_ring->idx, rx_ring->r_vec->napi.napi_id);
2571 		if (err < 0)
2572 			return err;
2573 	}
2574 
2575 	rx_ring->cnt = dp->rxd_cnt;
2576 	rx_ring->size = array_size(rx_ring->cnt, sizeof(*rx_ring->rxds));
2577 	rx_ring->rxds = dma_alloc_coherent(dp->dev, rx_ring->size,
2578 					   &rx_ring->dma,
2579 					   GFP_KERNEL | __GFP_NOWARN);
2580 	if (!rx_ring->rxds) {
2581 		netdev_warn(dp->netdev, "failed to allocate RX descriptor ring memory, requested descriptor count: %d, consider lowering descriptor count\n",
2582 			    rx_ring->cnt);
2583 		goto err_alloc;
2584 	}
2585 
2586 	rx_ring->rxbufs = kvcalloc(rx_ring->cnt, sizeof(*rx_ring->rxbufs),
2587 				   GFP_KERNEL);
2588 	if (!rx_ring->rxbufs)
2589 		goto err_alloc;
2590 
2591 	return 0;
2592 
2593 err_alloc:
2594 	nfp_net_rx_ring_free(rx_ring);
2595 	return -ENOMEM;
2596 }
2597 
nfp_net_rx_rings_prepare(struct nfp_net * nn,struct nfp_net_dp * dp)2598 static int nfp_net_rx_rings_prepare(struct nfp_net *nn, struct nfp_net_dp *dp)
2599 {
2600 	unsigned int r;
2601 
2602 	dp->rx_rings = kcalloc(dp->num_rx_rings, sizeof(*dp->rx_rings),
2603 			       GFP_KERNEL);
2604 	if (!dp->rx_rings)
2605 		return -ENOMEM;
2606 
2607 	for (r = 0; r < dp->num_rx_rings; r++) {
2608 		nfp_net_rx_ring_init(&dp->rx_rings[r], &nn->r_vecs[r], r);
2609 
2610 		if (nfp_net_rx_ring_alloc(dp, &dp->rx_rings[r]))
2611 			goto err_free_prev;
2612 
2613 		if (nfp_net_rx_ring_bufs_alloc(dp, &dp->rx_rings[r]))
2614 			goto err_free_ring;
2615 	}
2616 
2617 	return 0;
2618 
2619 err_free_prev:
2620 	while (r--) {
2621 		nfp_net_rx_ring_bufs_free(dp, &dp->rx_rings[r]);
2622 err_free_ring:
2623 		nfp_net_rx_ring_free(&dp->rx_rings[r]);
2624 	}
2625 	kfree(dp->rx_rings);
2626 	return -ENOMEM;
2627 }
2628 
nfp_net_rx_rings_free(struct nfp_net_dp * dp)2629 static void nfp_net_rx_rings_free(struct nfp_net_dp *dp)
2630 {
2631 	unsigned int r;
2632 
2633 	for (r = 0; r < dp->num_rx_rings; r++) {
2634 		nfp_net_rx_ring_bufs_free(dp, &dp->rx_rings[r]);
2635 		nfp_net_rx_ring_free(&dp->rx_rings[r]);
2636 	}
2637 
2638 	kfree(dp->rx_rings);
2639 }
2640 
2641 static void
nfp_net_vector_assign_rings(struct nfp_net_dp * dp,struct nfp_net_r_vector * r_vec,int idx)2642 nfp_net_vector_assign_rings(struct nfp_net_dp *dp,
2643 			    struct nfp_net_r_vector *r_vec, int idx)
2644 {
2645 	r_vec->rx_ring = idx < dp->num_rx_rings ? &dp->rx_rings[idx] : NULL;
2646 	r_vec->tx_ring =
2647 		idx < dp->num_stack_tx_rings ? &dp->tx_rings[idx] : NULL;
2648 
2649 	r_vec->xdp_ring = idx < dp->num_tx_rings - dp->num_stack_tx_rings ?
2650 		&dp->tx_rings[dp->num_stack_tx_rings + idx] : NULL;
2651 }
2652 
2653 static int
nfp_net_prepare_vector(struct nfp_net * nn,struct nfp_net_r_vector * r_vec,int idx)2654 nfp_net_prepare_vector(struct nfp_net *nn, struct nfp_net_r_vector *r_vec,
2655 		       int idx)
2656 {
2657 	int err;
2658 
2659 	/* Setup NAPI */
2660 	if (nn->dp.netdev)
2661 		netif_napi_add(nn->dp.netdev, &r_vec->napi,
2662 			       nfp_net_poll, NAPI_POLL_WEIGHT);
2663 	else
2664 		tasklet_enable(&r_vec->tasklet);
2665 
2666 	snprintf(r_vec->name, sizeof(r_vec->name),
2667 		 "%s-rxtx-%d", nfp_net_name(nn), idx);
2668 	err = request_irq(r_vec->irq_vector, r_vec->handler, 0, r_vec->name,
2669 			  r_vec);
2670 	if (err) {
2671 		if (nn->dp.netdev)
2672 			netif_napi_del(&r_vec->napi);
2673 		else
2674 			tasklet_disable(&r_vec->tasklet);
2675 
2676 		nn_err(nn, "Error requesting IRQ %d\n", r_vec->irq_vector);
2677 		return err;
2678 	}
2679 	disable_irq(r_vec->irq_vector);
2680 
2681 	irq_set_affinity_hint(r_vec->irq_vector, &r_vec->affinity_mask);
2682 
2683 	nn_dbg(nn, "RV%02d: irq=%03d/%03d\n", idx, r_vec->irq_vector,
2684 	       r_vec->irq_entry);
2685 
2686 	return 0;
2687 }
2688 
2689 static void
nfp_net_cleanup_vector(struct nfp_net * nn,struct nfp_net_r_vector * r_vec)2690 nfp_net_cleanup_vector(struct nfp_net *nn, struct nfp_net_r_vector *r_vec)
2691 {
2692 	irq_set_affinity_hint(r_vec->irq_vector, NULL);
2693 	if (nn->dp.netdev)
2694 		netif_napi_del(&r_vec->napi);
2695 	else
2696 		tasklet_disable(&r_vec->tasklet);
2697 
2698 	free_irq(r_vec->irq_vector, r_vec);
2699 }
2700 
2701 /**
2702  * nfp_net_rss_write_itbl() - Write RSS indirection table to device
2703  * @nn:      NFP Net device to reconfigure
2704  */
nfp_net_rss_write_itbl(struct nfp_net * nn)2705 void nfp_net_rss_write_itbl(struct nfp_net *nn)
2706 {
2707 	int i;
2708 
2709 	for (i = 0; i < NFP_NET_CFG_RSS_ITBL_SZ; i += 4)
2710 		nn_writel(nn, NFP_NET_CFG_RSS_ITBL + i,
2711 			  get_unaligned_le32(nn->rss_itbl + i));
2712 }
2713 
2714 /**
2715  * nfp_net_rss_write_key() - Write RSS hash key to device
2716  * @nn:      NFP Net device to reconfigure
2717  */
nfp_net_rss_write_key(struct nfp_net * nn)2718 void nfp_net_rss_write_key(struct nfp_net *nn)
2719 {
2720 	int i;
2721 
2722 	for (i = 0; i < nfp_net_rss_key_sz(nn); i += 4)
2723 		nn_writel(nn, NFP_NET_CFG_RSS_KEY + i,
2724 			  get_unaligned_le32(nn->rss_key + i));
2725 }
2726 
2727 /**
2728  * nfp_net_coalesce_write_cfg() - Write irq coalescence configuration to HW
2729  * @nn:      NFP Net device to reconfigure
2730  */
nfp_net_coalesce_write_cfg(struct nfp_net * nn)2731 void nfp_net_coalesce_write_cfg(struct nfp_net *nn)
2732 {
2733 	u8 i;
2734 	u32 factor;
2735 	u32 value;
2736 
2737 	/* Compute factor used to convert coalesce '_usecs' parameters to
2738 	 * ME timestamp ticks.  There are 16 ME clock cycles for each timestamp
2739 	 * count.
2740 	 */
2741 	factor = nn->tlv_caps.me_freq_mhz / 16;
2742 
2743 	/* copy RX interrupt coalesce parameters */
2744 	value = (nn->rx_coalesce_max_frames << 16) |
2745 		(factor * nn->rx_coalesce_usecs);
2746 	for (i = 0; i < nn->dp.num_rx_rings; i++)
2747 		nn_writel(nn, NFP_NET_CFG_RXR_IRQ_MOD(i), value);
2748 
2749 	/* copy TX interrupt coalesce parameters */
2750 	value = (nn->tx_coalesce_max_frames << 16) |
2751 		(factor * nn->tx_coalesce_usecs);
2752 	for (i = 0; i < nn->dp.num_tx_rings; i++)
2753 		nn_writel(nn, NFP_NET_CFG_TXR_IRQ_MOD(i), value);
2754 }
2755 
2756 /**
2757  * nfp_net_write_mac_addr() - Write mac address to the device control BAR
2758  * @nn:      NFP Net device to reconfigure
2759  * @addr:    MAC address to write
2760  *
2761  * Writes the MAC address from the netdev to the device control BAR.  Does not
2762  * perform the required reconfig.  We do a bit of byte swapping dance because
2763  * firmware is LE.
2764  */
nfp_net_write_mac_addr(struct nfp_net * nn,const u8 * addr)2765 static void nfp_net_write_mac_addr(struct nfp_net *nn, const u8 *addr)
2766 {
2767 	nn_writel(nn, NFP_NET_CFG_MACADDR + 0, get_unaligned_be32(addr));
2768 	nn_writew(nn, NFP_NET_CFG_MACADDR + 6, get_unaligned_be16(addr + 4));
2769 }
2770 
nfp_net_vec_clear_ring_data(struct nfp_net * nn,unsigned int idx)2771 static void nfp_net_vec_clear_ring_data(struct nfp_net *nn, unsigned int idx)
2772 {
2773 	nn_writeq(nn, NFP_NET_CFG_RXR_ADDR(idx), 0);
2774 	nn_writeb(nn, NFP_NET_CFG_RXR_SZ(idx), 0);
2775 	nn_writeb(nn, NFP_NET_CFG_RXR_VEC(idx), 0);
2776 
2777 	nn_writeq(nn, NFP_NET_CFG_TXR_ADDR(idx), 0);
2778 	nn_writeb(nn, NFP_NET_CFG_TXR_SZ(idx), 0);
2779 	nn_writeb(nn, NFP_NET_CFG_TXR_VEC(idx), 0);
2780 }
2781 
2782 /**
2783  * nfp_net_clear_config_and_disable() - Clear control BAR and disable NFP
2784  * @nn:      NFP Net device to reconfigure
2785  *
2786  * Warning: must be fully idempotent.
2787  */
nfp_net_clear_config_and_disable(struct nfp_net * nn)2788 static void nfp_net_clear_config_and_disable(struct nfp_net *nn)
2789 {
2790 	u32 new_ctrl, update;
2791 	unsigned int r;
2792 	int err;
2793 
2794 	new_ctrl = nn->dp.ctrl;
2795 	new_ctrl &= ~NFP_NET_CFG_CTRL_ENABLE;
2796 	update = NFP_NET_CFG_UPDATE_GEN;
2797 	update |= NFP_NET_CFG_UPDATE_MSIX;
2798 	update |= NFP_NET_CFG_UPDATE_RING;
2799 
2800 	if (nn->cap & NFP_NET_CFG_CTRL_RINGCFG)
2801 		new_ctrl &= ~NFP_NET_CFG_CTRL_RINGCFG;
2802 
2803 	nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, 0);
2804 	nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, 0);
2805 
2806 	nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
2807 	err = nfp_net_reconfig(nn, update);
2808 	if (err)
2809 		nn_err(nn, "Could not disable device: %d\n", err);
2810 
2811 	for (r = 0; r < nn->dp.num_rx_rings; r++)
2812 		nfp_net_rx_ring_reset(&nn->dp.rx_rings[r]);
2813 	for (r = 0; r < nn->dp.num_tx_rings; r++)
2814 		nfp_net_tx_ring_reset(&nn->dp, &nn->dp.tx_rings[r]);
2815 	for (r = 0; r < nn->dp.num_r_vecs; r++)
2816 		nfp_net_vec_clear_ring_data(nn, r);
2817 
2818 	nn->dp.ctrl = new_ctrl;
2819 }
2820 
2821 static void
nfp_net_rx_ring_hw_cfg_write(struct nfp_net * nn,struct nfp_net_rx_ring * rx_ring,unsigned int idx)2822 nfp_net_rx_ring_hw_cfg_write(struct nfp_net *nn,
2823 			     struct nfp_net_rx_ring *rx_ring, unsigned int idx)
2824 {
2825 	/* Write the DMA address, size and MSI-X info to the device */
2826 	nn_writeq(nn, NFP_NET_CFG_RXR_ADDR(idx), rx_ring->dma);
2827 	nn_writeb(nn, NFP_NET_CFG_RXR_SZ(idx), ilog2(rx_ring->cnt));
2828 	nn_writeb(nn, NFP_NET_CFG_RXR_VEC(idx), rx_ring->r_vec->irq_entry);
2829 }
2830 
2831 static void
nfp_net_tx_ring_hw_cfg_write(struct nfp_net * nn,struct nfp_net_tx_ring * tx_ring,unsigned int idx)2832 nfp_net_tx_ring_hw_cfg_write(struct nfp_net *nn,
2833 			     struct nfp_net_tx_ring *tx_ring, unsigned int idx)
2834 {
2835 	nn_writeq(nn, NFP_NET_CFG_TXR_ADDR(idx), tx_ring->dma);
2836 	nn_writeb(nn, NFP_NET_CFG_TXR_SZ(idx), ilog2(tx_ring->cnt));
2837 	nn_writeb(nn, NFP_NET_CFG_TXR_VEC(idx), tx_ring->r_vec->irq_entry);
2838 }
2839 
2840 /**
2841  * nfp_net_set_config_and_enable() - Write control BAR and enable NFP
2842  * @nn:      NFP Net device to reconfigure
2843  */
nfp_net_set_config_and_enable(struct nfp_net * nn)2844 static int nfp_net_set_config_and_enable(struct nfp_net *nn)
2845 {
2846 	u32 bufsz, new_ctrl, update = 0;
2847 	unsigned int r;
2848 	int err;
2849 
2850 	new_ctrl = nn->dp.ctrl;
2851 
2852 	if (nn->dp.ctrl & NFP_NET_CFG_CTRL_RSS_ANY) {
2853 		nfp_net_rss_write_key(nn);
2854 		nfp_net_rss_write_itbl(nn);
2855 		nn_writel(nn, NFP_NET_CFG_RSS_CTRL, nn->rss_cfg);
2856 		update |= NFP_NET_CFG_UPDATE_RSS;
2857 	}
2858 
2859 	if (nn->dp.ctrl & NFP_NET_CFG_CTRL_IRQMOD) {
2860 		nfp_net_coalesce_write_cfg(nn);
2861 		update |= NFP_NET_CFG_UPDATE_IRQMOD;
2862 	}
2863 
2864 	for (r = 0; r < nn->dp.num_tx_rings; r++)
2865 		nfp_net_tx_ring_hw_cfg_write(nn, &nn->dp.tx_rings[r], r);
2866 	for (r = 0; r < nn->dp.num_rx_rings; r++)
2867 		nfp_net_rx_ring_hw_cfg_write(nn, &nn->dp.rx_rings[r], r);
2868 
2869 	nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, nn->dp.num_tx_rings == 64 ?
2870 		  0xffffffffffffffffULL : ((u64)1 << nn->dp.num_tx_rings) - 1);
2871 
2872 	nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, nn->dp.num_rx_rings == 64 ?
2873 		  0xffffffffffffffffULL : ((u64)1 << nn->dp.num_rx_rings) - 1);
2874 
2875 	if (nn->dp.netdev)
2876 		nfp_net_write_mac_addr(nn, nn->dp.netdev->dev_addr);
2877 
2878 	nn_writel(nn, NFP_NET_CFG_MTU, nn->dp.mtu);
2879 
2880 	bufsz = nn->dp.fl_bufsz - nn->dp.rx_dma_off - NFP_NET_RX_BUF_NON_DATA;
2881 	nn_writel(nn, NFP_NET_CFG_FLBUFSZ, bufsz);
2882 
2883 	/* Enable device */
2884 	new_ctrl |= NFP_NET_CFG_CTRL_ENABLE;
2885 	update |= NFP_NET_CFG_UPDATE_GEN;
2886 	update |= NFP_NET_CFG_UPDATE_MSIX;
2887 	update |= NFP_NET_CFG_UPDATE_RING;
2888 	if (nn->cap & NFP_NET_CFG_CTRL_RINGCFG)
2889 		new_ctrl |= NFP_NET_CFG_CTRL_RINGCFG;
2890 
2891 	nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
2892 	err = nfp_net_reconfig(nn, update);
2893 	if (err) {
2894 		nfp_net_clear_config_and_disable(nn);
2895 		return err;
2896 	}
2897 
2898 	nn->dp.ctrl = new_ctrl;
2899 
2900 	for (r = 0; r < nn->dp.num_rx_rings; r++)
2901 		nfp_net_rx_ring_fill_freelist(&nn->dp, &nn->dp.rx_rings[r]);
2902 
2903 	return 0;
2904 }
2905 
2906 /**
2907  * nfp_net_close_stack() - Quiesce the stack (part of close)
2908  * @nn:	     NFP Net device to reconfigure
2909  */
nfp_net_close_stack(struct nfp_net * nn)2910 static void nfp_net_close_stack(struct nfp_net *nn)
2911 {
2912 	struct nfp_net_r_vector *r_vec;
2913 	unsigned int r;
2914 
2915 	disable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
2916 	netif_carrier_off(nn->dp.netdev);
2917 	nn->link_up = false;
2918 
2919 	for (r = 0; r < nn->dp.num_r_vecs; r++) {
2920 		r_vec = &nn->r_vecs[r];
2921 
2922 		disable_irq(r_vec->irq_vector);
2923 		napi_disable(&r_vec->napi);
2924 
2925 		if (r_vec->rx_ring)
2926 			cancel_work_sync(&r_vec->rx_dim.work);
2927 
2928 		if (r_vec->tx_ring)
2929 			cancel_work_sync(&r_vec->tx_dim.work);
2930 	}
2931 
2932 	netif_tx_disable(nn->dp.netdev);
2933 }
2934 
2935 /**
2936  * nfp_net_close_free_all() - Free all runtime resources
2937  * @nn:      NFP Net device to reconfigure
2938  */
nfp_net_close_free_all(struct nfp_net * nn)2939 static void nfp_net_close_free_all(struct nfp_net *nn)
2940 {
2941 	unsigned int r;
2942 
2943 	nfp_net_tx_rings_free(&nn->dp);
2944 	nfp_net_rx_rings_free(&nn->dp);
2945 
2946 	for (r = 0; r < nn->dp.num_r_vecs; r++)
2947 		nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
2948 
2949 	nfp_net_aux_irq_free(nn, NFP_NET_CFG_LSC, NFP_NET_IRQ_LSC_IDX);
2950 	nfp_net_aux_irq_free(nn, NFP_NET_CFG_EXN, NFP_NET_IRQ_EXN_IDX);
2951 }
2952 
2953 /**
2954  * nfp_net_netdev_close() - Called when the device is downed
2955  * @netdev:      netdev structure
2956  */
nfp_net_netdev_close(struct net_device * netdev)2957 static int nfp_net_netdev_close(struct net_device *netdev)
2958 {
2959 	struct nfp_net *nn = netdev_priv(netdev);
2960 
2961 	/* Step 1: Disable RX and TX rings from the Linux kernel perspective
2962 	 */
2963 	nfp_net_close_stack(nn);
2964 
2965 	/* Step 2: Tell NFP
2966 	 */
2967 	nfp_net_clear_config_and_disable(nn);
2968 	nfp_port_configure(netdev, false);
2969 
2970 	/* Step 3: Free resources
2971 	 */
2972 	nfp_net_close_free_all(nn);
2973 
2974 	nn_dbg(nn, "%s down", netdev->name);
2975 	return 0;
2976 }
2977 
nfp_ctrl_close(struct nfp_net * nn)2978 void nfp_ctrl_close(struct nfp_net *nn)
2979 {
2980 	int r;
2981 
2982 	rtnl_lock();
2983 
2984 	for (r = 0; r < nn->dp.num_r_vecs; r++) {
2985 		disable_irq(nn->r_vecs[r].irq_vector);
2986 		tasklet_disable(&nn->r_vecs[r].tasklet);
2987 	}
2988 
2989 	nfp_net_clear_config_and_disable(nn);
2990 
2991 	nfp_net_close_free_all(nn);
2992 
2993 	rtnl_unlock();
2994 }
2995 
nfp_net_rx_dim_work(struct work_struct * work)2996 static void nfp_net_rx_dim_work(struct work_struct *work)
2997 {
2998 	struct nfp_net_r_vector *r_vec;
2999 	unsigned int factor, value;
3000 	struct dim_cq_moder moder;
3001 	struct nfp_net *nn;
3002 	struct dim *dim;
3003 
3004 	dim = container_of(work, struct dim, work);
3005 	moder = net_dim_get_rx_moderation(dim->mode, dim->profile_ix);
3006 	r_vec = container_of(dim, struct nfp_net_r_vector, rx_dim);
3007 	nn = r_vec->nfp_net;
3008 
3009 	/* Compute factor used to convert coalesce '_usecs' parameters to
3010 	 * ME timestamp ticks.  There are 16 ME clock cycles for each timestamp
3011 	 * count.
3012 	 */
3013 	factor = nn->tlv_caps.me_freq_mhz / 16;
3014 	if (nfp_net_coalesce_para_check(factor * moder.usec, moder.pkts))
3015 		return;
3016 
3017 	/* copy RX interrupt coalesce parameters */
3018 	value = (moder.pkts << 16) | (factor * moder.usec);
3019 	nn_writel(nn, NFP_NET_CFG_RXR_IRQ_MOD(r_vec->rx_ring->idx), value);
3020 	(void)nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_IRQMOD);
3021 
3022 	dim->state = DIM_START_MEASURE;
3023 }
3024 
nfp_net_tx_dim_work(struct work_struct * work)3025 static void nfp_net_tx_dim_work(struct work_struct *work)
3026 {
3027 	struct nfp_net_r_vector *r_vec;
3028 	unsigned int factor, value;
3029 	struct dim_cq_moder moder;
3030 	struct nfp_net *nn;
3031 	struct dim *dim;
3032 
3033 	dim = container_of(work, struct dim, work);
3034 	moder = net_dim_get_tx_moderation(dim->mode, dim->profile_ix);
3035 	r_vec = container_of(dim, struct nfp_net_r_vector, tx_dim);
3036 	nn = r_vec->nfp_net;
3037 
3038 	/* Compute factor used to convert coalesce '_usecs' parameters to
3039 	 * ME timestamp ticks.  There are 16 ME clock cycles for each timestamp
3040 	 * count.
3041 	 */
3042 	factor = nn->tlv_caps.me_freq_mhz / 16;
3043 	if (nfp_net_coalesce_para_check(factor * moder.usec, moder.pkts))
3044 		return;
3045 
3046 	/* copy TX interrupt coalesce parameters */
3047 	value = (moder.pkts << 16) | (factor * moder.usec);
3048 	nn_writel(nn, NFP_NET_CFG_TXR_IRQ_MOD(r_vec->tx_ring->idx), value);
3049 	(void)nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_IRQMOD);
3050 
3051 	dim->state = DIM_START_MEASURE;
3052 }
3053 
3054 /**
3055  * nfp_net_open_stack() - Start the device from stack's perspective
3056  * @nn:      NFP Net device to reconfigure
3057  */
nfp_net_open_stack(struct nfp_net * nn)3058 static void nfp_net_open_stack(struct nfp_net *nn)
3059 {
3060 	struct nfp_net_r_vector *r_vec;
3061 	unsigned int r;
3062 
3063 	for (r = 0; r < nn->dp.num_r_vecs; r++) {
3064 		r_vec = &nn->r_vecs[r];
3065 
3066 		if (r_vec->rx_ring) {
3067 			INIT_WORK(&r_vec->rx_dim.work, nfp_net_rx_dim_work);
3068 			r_vec->rx_dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
3069 		}
3070 
3071 		if (r_vec->tx_ring) {
3072 			INIT_WORK(&r_vec->tx_dim.work, nfp_net_tx_dim_work);
3073 			r_vec->tx_dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
3074 		}
3075 
3076 		napi_enable(&r_vec->napi);
3077 		enable_irq(r_vec->irq_vector);
3078 	}
3079 
3080 	netif_tx_wake_all_queues(nn->dp.netdev);
3081 
3082 	enable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
3083 	nfp_net_read_link_status(nn);
3084 }
3085 
nfp_net_open_alloc_all(struct nfp_net * nn)3086 static int nfp_net_open_alloc_all(struct nfp_net *nn)
3087 {
3088 	int err, r;
3089 
3090 	err = nfp_net_aux_irq_request(nn, NFP_NET_CFG_EXN, "%s-exn",
3091 				      nn->exn_name, sizeof(nn->exn_name),
3092 				      NFP_NET_IRQ_EXN_IDX, nn->exn_handler);
3093 	if (err)
3094 		return err;
3095 	err = nfp_net_aux_irq_request(nn, NFP_NET_CFG_LSC, "%s-lsc",
3096 				      nn->lsc_name, sizeof(nn->lsc_name),
3097 				      NFP_NET_IRQ_LSC_IDX, nn->lsc_handler);
3098 	if (err)
3099 		goto err_free_exn;
3100 	disable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
3101 
3102 	for (r = 0; r < nn->dp.num_r_vecs; r++) {
3103 		err = nfp_net_prepare_vector(nn, &nn->r_vecs[r], r);
3104 		if (err)
3105 			goto err_cleanup_vec_p;
3106 	}
3107 
3108 	err = nfp_net_rx_rings_prepare(nn, &nn->dp);
3109 	if (err)
3110 		goto err_cleanup_vec;
3111 
3112 	err = nfp_net_tx_rings_prepare(nn, &nn->dp);
3113 	if (err)
3114 		goto err_free_rx_rings;
3115 
3116 	for (r = 0; r < nn->max_r_vecs; r++)
3117 		nfp_net_vector_assign_rings(&nn->dp, &nn->r_vecs[r], r);
3118 
3119 	return 0;
3120 
3121 err_free_rx_rings:
3122 	nfp_net_rx_rings_free(&nn->dp);
3123 err_cleanup_vec:
3124 	r = nn->dp.num_r_vecs;
3125 err_cleanup_vec_p:
3126 	while (r--)
3127 		nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
3128 	nfp_net_aux_irq_free(nn, NFP_NET_CFG_LSC, NFP_NET_IRQ_LSC_IDX);
3129 err_free_exn:
3130 	nfp_net_aux_irq_free(nn, NFP_NET_CFG_EXN, NFP_NET_IRQ_EXN_IDX);
3131 	return err;
3132 }
3133 
nfp_net_netdev_open(struct net_device * netdev)3134 static int nfp_net_netdev_open(struct net_device *netdev)
3135 {
3136 	struct nfp_net *nn = netdev_priv(netdev);
3137 	int err;
3138 
3139 	/* Step 1: Allocate resources for rings and the like
3140 	 * - Request interrupts
3141 	 * - Allocate RX and TX ring resources
3142 	 * - Setup initial RSS table
3143 	 */
3144 	err = nfp_net_open_alloc_all(nn);
3145 	if (err)
3146 		return err;
3147 
3148 	err = netif_set_real_num_tx_queues(netdev, nn->dp.num_stack_tx_rings);
3149 	if (err)
3150 		goto err_free_all;
3151 
3152 	err = netif_set_real_num_rx_queues(netdev, nn->dp.num_rx_rings);
3153 	if (err)
3154 		goto err_free_all;
3155 
3156 	/* Step 2: Configure the NFP
3157 	 * - Ifup the physical interface if it exists
3158 	 * - Enable rings from 0 to tx_rings/rx_rings - 1.
3159 	 * - Write MAC address (in case it changed)
3160 	 * - Set the MTU
3161 	 * - Set the Freelist buffer size
3162 	 * - Enable the FW
3163 	 */
3164 	err = nfp_port_configure(netdev, true);
3165 	if (err)
3166 		goto err_free_all;
3167 
3168 	err = nfp_net_set_config_and_enable(nn);
3169 	if (err)
3170 		goto err_port_disable;
3171 
3172 	/* Step 3: Enable for kernel
3173 	 * - put some freelist descriptors on each RX ring
3174 	 * - enable NAPI on each ring
3175 	 * - enable all TX queues
3176 	 * - set link state
3177 	 */
3178 	nfp_net_open_stack(nn);
3179 
3180 	return 0;
3181 
3182 err_port_disable:
3183 	nfp_port_configure(netdev, false);
3184 err_free_all:
3185 	nfp_net_close_free_all(nn);
3186 	return err;
3187 }
3188 
nfp_ctrl_open(struct nfp_net * nn)3189 int nfp_ctrl_open(struct nfp_net *nn)
3190 {
3191 	int err, r;
3192 
3193 	/* ring dumping depends on vNICs being opened/closed under rtnl */
3194 	rtnl_lock();
3195 
3196 	err = nfp_net_open_alloc_all(nn);
3197 	if (err)
3198 		goto err_unlock;
3199 
3200 	err = nfp_net_set_config_and_enable(nn);
3201 	if (err)
3202 		goto err_free_all;
3203 
3204 	for (r = 0; r < nn->dp.num_r_vecs; r++)
3205 		enable_irq(nn->r_vecs[r].irq_vector);
3206 
3207 	rtnl_unlock();
3208 
3209 	return 0;
3210 
3211 err_free_all:
3212 	nfp_net_close_free_all(nn);
3213 err_unlock:
3214 	rtnl_unlock();
3215 	return err;
3216 }
3217 
nfp_net_set_rx_mode(struct net_device * netdev)3218 static void nfp_net_set_rx_mode(struct net_device *netdev)
3219 {
3220 	struct nfp_net *nn = netdev_priv(netdev);
3221 	u32 new_ctrl;
3222 
3223 	new_ctrl = nn->dp.ctrl;
3224 
3225 	if (!netdev_mc_empty(netdev) || netdev->flags & IFF_ALLMULTI)
3226 		new_ctrl |= nn->cap & NFP_NET_CFG_CTRL_L2MC;
3227 	else
3228 		new_ctrl &= ~NFP_NET_CFG_CTRL_L2MC;
3229 
3230 	if (netdev->flags & IFF_PROMISC) {
3231 		if (nn->cap & NFP_NET_CFG_CTRL_PROMISC)
3232 			new_ctrl |= NFP_NET_CFG_CTRL_PROMISC;
3233 		else
3234 			nn_warn(nn, "FW does not support promiscuous mode\n");
3235 	} else {
3236 		new_ctrl &= ~NFP_NET_CFG_CTRL_PROMISC;
3237 	}
3238 
3239 	if (new_ctrl == nn->dp.ctrl)
3240 		return;
3241 
3242 	nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
3243 	nfp_net_reconfig_post(nn, NFP_NET_CFG_UPDATE_GEN);
3244 
3245 	nn->dp.ctrl = new_ctrl;
3246 }
3247 
nfp_net_rss_init_itbl(struct nfp_net * nn)3248 static void nfp_net_rss_init_itbl(struct nfp_net *nn)
3249 {
3250 	int i;
3251 
3252 	for (i = 0; i < sizeof(nn->rss_itbl); i++)
3253 		nn->rss_itbl[i] =
3254 			ethtool_rxfh_indir_default(i, nn->dp.num_rx_rings);
3255 }
3256 
nfp_net_dp_swap(struct nfp_net * nn,struct nfp_net_dp * dp)3257 static void nfp_net_dp_swap(struct nfp_net *nn, struct nfp_net_dp *dp)
3258 {
3259 	struct nfp_net_dp new_dp = *dp;
3260 
3261 	*dp = nn->dp;
3262 	nn->dp = new_dp;
3263 
3264 	nn->dp.netdev->mtu = new_dp.mtu;
3265 
3266 	if (!netif_is_rxfh_configured(nn->dp.netdev))
3267 		nfp_net_rss_init_itbl(nn);
3268 }
3269 
nfp_net_dp_swap_enable(struct nfp_net * nn,struct nfp_net_dp * dp)3270 static int nfp_net_dp_swap_enable(struct nfp_net *nn, struct nfp_net_dp *dp)
3271 {
3272 	unsigned int r;
3273 	int err;
3274 
3275 	nfp_net_dp_swap(nn, dp);
3276 
3277 	for (r = 0; r <	nn->max_r_vecs; r++)
3278 		nfp_net_vector_assign_rings(&nn->dp, &nn->r_vecs[r], r);
3279 
3280 	err = netif_set_real_num_queues(nn->dp.netdev,
3281 					nn->dp.num_stack_tx_rings,
3282 					nn->dp.num_rx_rings);
3283 	if (err)
3284 		return err;
3285 
3286 	return nfp_net_set_config_and_enable(nn);
3287 }
3288 
nfp_net_clone_dp(struct nfp_net * nn)3289 struct nfp_net_dp *nfp_net_clone_dp(struct nfp_net *nn)
3290 {
3291 	struct nfp_net_dp *new;
3292 
3293 	new = kmalloc(sizeof(*new), GFP_KERNEL);
3294 	if (!new)
3295 		return NULL;
3296 
3297 	*new = nn->dp;
3298 
3299 	/* Clear things which need to be recomputed */
3300 	new->fl_bufsz = 0;
3301 	new->tx_rings = NULL;
3302 	new->rx_rings = NULL;
3303 	new->num_r_vecs = 0;
3304 	new->num_stack_tx_rings = 0;
3305 
3306 	return new;
3307 }
3308 
3309 static int
nfp_net_check_config(struct nfp_net * nn,struct nfp_net_dp * dp,struct netlink_ext_ack * extack)3310 nfp_net_check_config(struct nfp_net *nn, struct nfp_net_dp *dp,
3311 		     struct netlink_ext_ack *extack)
3312 {
3313 	/* XDP-enabled tests */
3314 	if (!dp->xdp_prog)
3315 		return 0;
3316 	if (dp->fl_bufsz > PAGE_SIZE) {
3317 		NL_SET_ERR_MSG_MOD(extack, "MTU too large w/ XDP enabled");
3318 		return -EINVAL;
3319 	}
3320 	if (dp->num_tx_rings > nn->max_tx_rings) {
3321 		NL_SET_ERR_MSG_MOD(extack, "Insufficient number of TX rings w/ XDP enabled");
3322 		return -EINVAL;
3323 	}
3324 
3325 	return 0;
3326 }
3327 
nfp_net_ring_reconfig(struct nfp_net * nn,struct nfp_net_dp * dp,struct netlink_ext_ack * extack)3328 int nfp_net_ring_reconfig(struct nfp_net *nn, struct nfp_net_dp *dp,
3329 			  struct netlink_ext_ack *extack)
3330 {
3331 	int r, err;
3332 
3333 	dp->fl_bufsz = nfp_net_calc_fl_bufsz(dp);
3334 
3335 	dp->num_stack_tx_rings = dp->num_tx_rings;
3336 	if (dp->xdp_prog)
3337 		dp->num_stack_tx_rings -= dp->num_rx_rings;
3338 
3339 	dp->num_r_vecs = max(dp->num_rx_rings, dp->num_stack_tx_rings);
3340 
3341 	err = nfp_net_check_config(nn, dp, extack);
3342 	if (err)
3343 		goto exit_free_dp;
3344 
3345 	if (!netif_running(dp->netdev)) {
3346 		nfp_net_dp_swap(nn, dp);
3347 		err = 0;
3348 		goto exit_free_dp;
3349 	}
3350 
3351 	/* Prepare new rings */
3352 	for (r = nn->dp.num_r_vecs; r < dp->num_r_vecs; r++) {
3353 		err = nfp_net_prepare_vector(nn, &nn->r_vecs[r], r);
3354 		if (err) {
3355 			dp->num_r_vecs = r;
3356 			goto err_cleanup_vecs;
3357 		}
3358 	}
3359 
3360 	err = nfp_net_rx_rings_prepare(nn, dp);
3361 	if (err)
3362 		goto err_cleanup_vecs;
3363 
3364 	err = nfp_net_tx_rings_prepare(nn, dp);
3365 	if (err)
3366 		goto err_free_rx;
3367 
3368 	/* Stop device, swap in new rings, try to start the firmware */
3369 	nfp_net_close_stack(nn);
3370 	nfp_net_clear_config_and_disable(nn);
3371 
3372 	err = nfp_net_dp_swap_enable(nn, dp);
3373 	if (err) {
3374 		int err2;
3375 
3376 		nfp_net_clear_config_and_disable(nn);
3377 
3378 		/* Try with old configuration and old rings */
3379 		err2 = nfp_net_dp_swap_enable(nn, dp);
3380 		if (err2)
3381 			nn_err(nn, "Can't restore ring config - FW communication failed (%d,%d)\n",
3382 			       err, err2);
3383 	}
3384 	for (r = dp->num_r_vecs - 1; r >= nn->dp.num_r_vecs; r--)
3385 		nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
3386 
3387 	nfp_net_rx_rings_free(dp);
3388 	nfp_net_tx_rings_free(dp);
3389 
3390 	nfp_net_open_stack(nn);
3391 exit_free_dp:
3392 	kfree(dp);
3393 
3394 	return err;
3395 
3396 err_free_rx:
3397 	nfp_net_rx_rings_free(dp);
3398 err_cleanup_vecs:
3399 	for (r = dp->num_r_vecs - 1; r >= nn->dp.num_r_vecs; r--)
3400 		nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
3401 	kfree(dp);
3402 	return err;
3403 }
3404 
nfp_net_change_mtu(struct net_device * netdev,int new_mtu)3405 static int nfp_net_change_mtu(struct net_device *netdev, int new_mtu)
3406 {
3407 	struct nfp_net *nn = netdev_priv(netdev);
3408 	struct nfp_net_dp *dp;
3409 	int err;
3410 
3411 	err = nfp_app_check_mtu(nn->app, netdev, new_mtu);
3412 	if (err)
3413 		return err;
3414 
3415 	dp = nfp_net_clone_dp(nn);
3416 	if (!dp)
3417 		return -ENOMEM;
3418 
3419 	dp->mtu = new_mtu;
3420 
3421 	return nfp_net_ring_reconfig(nn, dp, NULL);
3422 }
3423 
3424 static int
nfp_net_vlan_rx_add_vid(struct net_device * netdev,__be16 proto,u16 vid)3425 nfp_net_vlan_rx_add_vid(struct net_device *netdev, __be16 proto, u16 vid)
3426 {
3427 	const u32 cmd = NFP_NET_CFG_MBOX_CMD_CTAG_FILTER_ADD;
3428 	struct nfp_net *nn = netdev_priv(netdev);
3429 	int err;
3430 
3431 	/* Priority tagged packets with vlan id 0 are processed by the
3432 	 * NFP as untagged packets
3433 	 */
3434 	if (!vid)
3435 		return 0;
3436 
3437 	err = nfp_net_mbox_lock(nn, NFP_NET_CFG_VLAN_FILTER_SZ);
3438 	if (err)
3439 		return err;
3440 
3441 	nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_VLAN_FILTER_VID, vid);
3442 	nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_VLAN_FILTER_PROTO,
3443 		  ETH_P_8021Q);
3444 
3445 	return nfp_net_mbox_reconfig_and_unlock(nn, cmd);
3446 }
3447 
3448 static int
nfp_net_vlan_rx_kill_vid(struct net_device * netdev,__be16 proto,u16 vid)3449 nfp_net_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto, u16 vid)
3450 {
3451 	const u32 cmd = NFP_NET_CFG_MBOX_CMD_CTAG_FILTER_KILL;
3452 	struct nfp_net *nn = netdev_priv(netdev);
3453 	int err;
3454 
3455 	/* Priority tagged packets with vlan id 0 are processed by the
3456 	 * NFP as untagged packets
3457 	 */
3458 	if (!vid)
3459 		return 0;
3460 
3461 	err = nfp_net_mbox_lock(nn, NFP_NET_CFG_VLAN_FILTER_SZ);
3462 	if (err)
3463 		return err;
3464 
3465 	nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_VLAN_FILTER_VID, vid);
3466 	nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_VLAN_FILTER_PROTO,
3467 		  ETH_P_8021Q);
3468 
3469 	return nfp_net_mbox_reconfig_and_unlock(nn, cmd);
3470 }
3471 
nfp_net_stat64(struct net_device * netdev,struct rtnl_link_stats64 * stats)3472 static void nfp_net_stat64(struct net_device *netdev,
3473 			   struct rtnl_link_stats64 *stats)
3474 {
3475 	struct nfp_net *nn = netdev_priv(netdev);
3476 	int r;
3477 
3478 	/* Collect software stats */
3479 	for (r = 0; r < nn->max_r_vecs; r++) {
3480 		struct nfp_net_r_vector *r_vec = &nn->r_vecs[r];
3481 		u64 data[3];
3482 		unsigned int start;
3483 
3484 		do {
3485 			start = u64_stats_fetch_begin_irq(&r_vec->rx_sync);
3486 			data[0] = r_vec->rx_pkts;
3487 			data[1] = r_vec->rx_bytes;
3488 			data[2] = r_vec->rx_drops;
3489 		} while (u64_stats_fetch_retry_irq(&r_vec->rx_sync, start));
3490 		stats->rx_packets += data[0];
3491 		stats->rx_bytes += data[1];
3492 		stats->rx_dropped += data[2];
3493 
3494 		do {
3495 			start = u64_stats_fetch_begin_irq(&r_vec->tx_sync);
3496 			data[0] = r_vec->tx_pkts;
3497 			data[1] = r_vec->tx_bytes;
3498 			data[2] = r_vec->tx_errors;
3499 		} while (u64_stats_fetch_retry_irq(&r_vec->tx_sync, start));
3500 		stats->tx_packets += data[0];
3501 		stats->tx_bytes += data[1];
3502 		stats->tx_errors += data[2];
3503 	}
3504 
3505 	/* Add in device stats */
3506 	stats->multicast += nn_readq(nn, NFP_NET_CFG_STATS_RX_MC_FRAMES);
3507 	stats->rx_dropped += nn_readq(nn, NFP_NET_CFG_STATS_RX_DISCARDS);
3508 	stats->rx_errors += nn_readq(nn, NFP_NET_CFG_STATS_RX_ERRORS);
3509 
3510 	stats->tx_dropped += nn_readq(nn, NFP_NET_CFG_STATS_TX_DISCARDS);
3511 	stats->tx_errors += nn_readq(nn, NFP_NET_CFG_STATS_TX_ERRORS);
3512 }
3513 
nfp_net_set_features(struct net_device * netdev,netdev_features_t features)3514 static int nfp_net_set_features(struct net_device *netdev,
3515 				netdev_features_t features)
3516 {
3517 	netdev_features_t changed = netdev->features ^ features;
3518 	struct nfp_net *nn = netdev_priv(netdev);
3519 	u32 new_ctrl;
3520 	int err;
3521 
3522 	/* Assume this is not called with features we have not advertised */
3523 
3524 	new_ctrl = nn->dp.ctrl;
3525 
3526 	if (changed & NETIF_F_RXCSUM) {
3527 		if (features & NETIF_F_RXCSUM)
3528 			new_ctrl |= nn->cap & NFP_NET_CFG_CTRL_RXCSUM_ANY;
3529 		else
3530 			new_ctrl &= ~NFP_NET_CFG_CTRL_RXCSUM_ANY;
3531 	}
3532 
3533 	if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
3534 		if (features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))
3535 			new_ctrl |= NFP_NET_CFG_CTRL_TXCSUM;
3536 		else
3537 			new_ctrl &= ~NFP_NET_CFG_CTRL_TXCSUM;
3538 	}
3539 
3540 	if (changed & (NETIF_F_TSO | NETIF_F_TSO6)) {
3541 		if (features & (NETIF_F_TSO | NETIF_F_TSO6))
3542 			new_ctrl |= nn->cap & NFP_NET_CFG_CTRL_LSO2 ?:
3543 					      NFP_NET_CFG_CTRL_LSO;
3544 		else
3545 			new_ctrl &= ~NFP_NET_CFG_CTRL_LSO_ANY;
3546 	}
3547 
3548 	if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
3549 		if (features & NETIF_F_HW_VLAN_CTAG_RX)
3550 			new_ctrl |= NFP_NET_CFG_CTRL_RXVLAN;
3551 		else
3552 			new_ctrl &= ~NFP_NET_CFG_CTRL_RXVLAN;
3553 	}
3554 
3555 	if (changed & NETIF_F_HW_VLAN_CTAG_TX) {
3556 		if (features & NETIF_F_HW_VLAN_CTAG_TX)
3557 			new_ctrl |= NFP_NET_CFG_CTRL_TXVLAN;
3558 		else
3559 			new_ctrl &= ~NFP_NET_CFG_CTRL_TXVLAN;
3560 	}
3561 
3562 	if (changed & NETIF_F_HW_VLAN_CTAG_FILTER) {
3563 		if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
3564 			new_ctrl |= NFP_NET_CFG_CTRL_CTAG_FILTER;
3565 		else
3566 			new_ctrl &= ~NFP_NET_CFG_CTRL_CTAG_FILTER;
3567 	}
3568 
3569 	if (changed & NETIF_F_SG) {
3570 		if (features & NETIF_F_SG)
3571 			new_ctrl |= NFP_NET_CFG_CTRL_GATHER;
3572 		else
3573 			new_ctrl &= ~NFP_NET_CFG_CTRL_GATHER;
3574 	}
3575 
3576 	err = nfp_port_set_features(netdev, features);
3577 	if (err)
3578 		return err;
3579 
3580 	nn_dbg(nn, "Feature change 0x%llx -> 0x%llx (changed=0x%llx)\n",
3581 	       netdev->features, features, changed);
3582 
3583 	if (new_ctrl == nn->dp.ctrl)
3584 		return 0;
3585 
3586 	nn_dbg(nn, "NIC ctrl: 0x%x -> 0x%x\n", nn->dp.ctrl, new_ctrl);
3587 	nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
3588 	err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_GEN);
3589 	if (err)
3590 		return err;
3591 
3592 	nn->dp.ctrl = new_ctrl;
3593 
3594 	return 0;
3595 }
3596 
3597 static netdev_features_t
nfp_net_features_check(struct sk_buff * skb,struct net_device * dev,netdev_features_t features)3598 nfp_net_features_check(struct sk_buff *skb, struct net_device *dev,
3599 		       netdev_features_t features)
3600 {
3601 	u8 l4_hdr;
3602 
3603 	/* We can't do TSO over double tagged packets (802.1AD) */
3604 	features &= vlan_features_check(skb, features);
3605 
3606 	if (!skb->encapsulation)
3607 		return features;
3608 
3609 	/* Ensure that inner L4 header offset fits into TX descriptor field */
3610 	if (skb_is_gso(skb)) {
3611 		u32 hdrlen;
3612 
3613 		hdrlen = skb_inner_transport_header(skb) - skb->data +
3614 			inner_tcp_hdrlen(skb);
3615 
3616 		/* Assume worst case scenario of having longest possible
3617 		 * metadata prepend - 8B
3618 		 */
3619 		if (unlikely(hdrlen > NFP_NET_LSO_MAX_HDR_SZ - 8))
3620 			features &= ~NETIF_F_GSO_MASK;
3621 	}
3622 
3623 	/* VXLAN/GRE check */
3624 	switch (vlan_get_protocol(skb)) {
3625 	case htons(ETH_P_IP):
3626 		l4_hdr = ip_hdr(skb)->protocol;
3627 		break;
3628 	case htons(ETH_P_IPV6):
3629 		l4_hdr = ipv6_hdr(skb)->nexthdr;
3630 		break;
3631 	default:
3632 		return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
3633 	}
3634 
3635 	if (skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
3636 	    skb->inner_protocol != htons(ETH_P_TEB) ||
3637 	    (l4_hdr != IPPROTO_UDP && l4_hdr != IPPROTO_GRE) ||
3638 	    (l4_hdr == IPPROTO_UDP &&
3639 	     (skb_inner_mac_header(skb) - skb_transport_header(skb) !=
3640 	      sizeof(struct udphdr) + sizeof(struct vxlanhdr))))
3641 		return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
3642 
3643 	return features;
3644 }
3645 
3646 static int
nfp_net_get_phys_port_name(struct net_device * netdev,char * name,size_t len)3647 nfp_net_get_phys_port_name(struct net_device *netdev, char *name, size_t len)
3648 {
3649 	struct nfp_net *nn = netdev_priv(netdev);
3650 	int n;
3651 
3652 	/* If port is defined, devlink_port is registered and devlink core
3653 	 * is taking care of name formatting.
3654 	 */
3655 	if (nn->port)
3656 		return -EOPNOTSUPP;
3657 
3658 	if (nn->dp.is_vf || nn->vnic_no_name)
3659 		return -EOPNOTSUPP;
3660 
3661 	n = snprintf(name, len, "n%d", nn->id);
3662 	if (n >= len)
3663 		return -EINVAL;
3664 
3665 	return 0;
3666 }
3667 
nfp_net_xdp_setup_drv(struct nfp_net * nn,struct netdev_bpf * bpf)3668 static int nfp_net_xdp_setup_drv(struct nfp_net *nn, struct netdev_bpf *bpf)
3669 {
3670 	struct bpf_prog *prog = bpf->prog;
3671 	struct nfp_net_dp *dp;
3672 	int err;
3673 
3674 	if (!prog == !nn->dp.xdp_prog) {
3675 		WRITE_ONCE(nn->dp.xdp_prog, prog);
3676 		xdp_attachment_setup(&nn->xdp, bpf);
3677 		return 0;
3678 	}
3679 
3680 	dp = nfp_net_clone_dp(nn);
3681 	if (!dp)
3682 		return -ENOMEM;
3683 
3684 	dp->xdp_prog = prog;
3685 	dp->num_tx_rings += prog ? nn->dp.num_rx_rings : -nn->dp.num_rx_rings;
3686 	dp->rx_dma_dir = prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
3687 	dp->rx_dma_off = prog ? XDP_PACKET_HEADROOM - nn->dp.rx_offset : 0;
3688 
3689 	/* We need RX reconfig to remap the buffers (BIDIR vs FROM_DEV) */
3690 	err = nfp_net_ring_reconfig(nn, dp, bpf->extack);
3691 	if (err)
3692 		return err;
3693 
3694 	xdp_attachment_setup(&nn->xdp, bpf);
3695 	return 0;
3696 }
3697 
nfp_net_xdp_setup_hw(struct nfp_net * nn,struct netdev_bpf * bpf)3698 static int nfp_net_xdp_setup_hw(struct nfp_net *nn, struct netdev_bpf *bpf)
3699 {
3700 	int err;
3701 
3702 	err = nfp_app_xdp_offload(nn->app, nn, bpf->prog, bpf->extack);
3703 	if (err)
3704 		return err;
3705 
3706 	xdp_attachment_setup(&nn->xdp_hw, bpf);
3707 	return 0;
3708 }
3709 
nfp_net_xdp(struct net_device * netdev,struct netdev_bpf * xdp)3710 static int nfp_net_xdp(struct net_device *netdev, struct netdev_bpf *xdp)
3711 {
3712 	struct nfp_net *nn = netdev_priv(netdev);
3713 
3714 	switch (xdp->command) {
3715 	case XDP_SETUP_PROG:
3716 		return nfp_net_xdp_setup_drv(nn, xdp);
3717 	case XDP_SETUP_PROG_HW:
3718 		return nfp_net_xdp_setup_hw(nn, xdp);
3719 	default:
3720 		return nfp_app_bpf(nn->app, nn, xdp);
3721 	}
3722 }
3723 
nfp_net_set_mac_address(struct net_device * netdev,void * addr)3724 static int nfp_net_set_mac_address(struct net_device *netdev, void *addr)
3725 {
3726 	struct nfp_net *nn = netdev_priv(netdev);
3727 	struct sockaddr *saddr = addr;
3728 	int err;
3729 
3730 	err = eth_prepare_mac_addr_change(netdev, addr);
3731 	if (err)
3732 		return err;
3733 
3734 	nfp_net_write_mac_addr(nn, saddr->sa_data);
3735 
3736 	err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_MACADDR);
3737 	if (err)
3738 		return err;
3739 
3740 	eth_commit_mac_addr_change(netdev, addr);
3741 
3742 	return 0;
3743 }
3744 
3745 const struct net_device_ops nfp_net_netdev_ops = {
3746 	.ndo_init		= nfp_app_ndo_init,
3747 	.ndo_uninit		= nfp_app_ndo_uninit,
3748 	.ndo_open		= nfp_net_netdev_open,
3749 	.ndo_stop		= nfp_net_netdev_close,
3750 	.ndo_start_xmit		= nfp_net_tx,
3751 	.ndo_get_stats64	= nfp_net_stat64,
3752 	.ndo_vlan_rx_add_vid	= nfp_net_vlan_rx_add_vid,
3753 	.ndo_vlan_rx_kill_vid	= nfp_net_vlan_rx_kill_vid,
3754 	.ndo_set_vf_mac         = nfp_app_set_vf_mac,
3755 	.ndo_set_vf_vlan        = nfp_app_set_vf_vlan,
3756 	.ndo_set_vf_spoofchk    = nfp_app_set_vf_spoofchk,
3757 	.ndo_set_vf_trust	= nfp_app_set_vf_trust,
3758 	.ndo_get_vf_config	= nfp_app_get_vf_config,
3759 	.ndo_set_vf_link_state  = nfp_app_set_vf_link_state,
3760 	.ndo_setup_tc		= nfp_port_setup_tc,
3761 	.ndo_tx_timeout		= nfp_net_tx_timeout,
3762 	.ndo_set_rx_mode	= nfp_net_set_rx_mode,
3763 	.ndo_change_mtu		= nfp_net_change_mtu,
3764 	.ndo_set_mac_address	= nfp_net_set_mac_address,
3765 	.ndo_set_features	= nfp_net_set_features,
3766 	.ndo_features_check	= nfp_net_features_check,
3767 	.ndo_get_phys_port_name	= nfp_net_get_phys_port_name,
3768 	.ndo_bpf		= nfp_net_xdp,
3769 	.ndo_get_devlink_port	= nfp_devlink_get_devlink_port,
3770 };
3771 
nfp_udp_tunnel_sync(struct net_device * netdev,unsigned int table)3772 static int nfp_udp_tunnel_sync(struct net_device *netdev, unsigned int table)
3773 {
3774 	struct nfp_net *nn = netdev_priv(netdev);
3775 	int i;
3776 
3777 	BUILD_BUG_ON(NFP_NET_N_VXLAN_PORTS & 1);
3778 	for (i = 0; i < NFP_NET_N_VXLAN_PORTS; i += 2) {
3779 		struct udp_tunnel_info ti0, ti1;
3780 
3781 		udp_tunnel_nic_get_port(netdev, table, i, &ti0);
3782 		udp_tunnel_nic_get_port(netdev, table, i + 1, &ti1);
3783 
3784 		nn_writel(nn, NFP_NET_CFG_VXLAN_PORT + i * sizeof(ti0.port),
3785 			  be16_to_cpu(ti1.port) << 16 | be16_to_cpu(ti0.port));
3786 	}
3787 
3788 	return nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_VXLAN);
3789 }
3790 
3791 static const struct udp_tunnel_nic_info nfp_udp_tunnels = {
3792 	.sync_table     = nfp_udp_tunnel_sync,
3793 	.flags          = UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
3794 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
3795 	.tables         = {
3796 		{
3797 			.n_entries      = NFP_NET_N_VXLAN_PORTS,
3798 			.tunnel_types   = UDP_TUNNEL_TYPE_VXLAN,
3799 		},
3800 	},
3801 };
3802 
3803 /**
3804  * nfp_net_info() - Print general info about the NIC
3805  * @nn:      NFP Net device to reconfigure
3806  */
nfp_net_info(struct nfp_net * nn)3807 void nfp_net_info(struct nfp_net *nn)
3808 {
3809 	nn_info(nn, "Netronome NFP-6xxx %sNetdev: TxQs=%d/%d RxQs=%d/%d\n",
3810 		nn->dp.is_vf ? "VF " : "",
3811 		nn->dp.num_tx_rings, nn->max_tx_rings,
3812 		nn->dp.num_rx_rings, nn->max_rx_rings);
3813 	nn_info(nn, "VER: %d.%d.%d.%d, Maximum supported MTU: %d\n",
3814 		nn->fw_ver.resv, nn->fw_ver.class,
3815 		nn->fw_ver.major, nn->fw_ver.minor,
3816 		nn->max_mtu);
3817 	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\n",
3818 		nn->cap,
3819 		nn->cap & NFP_NET_CFG_CTRL_PROMISC  ? "PROMISC "  : "",
3820 		nn->cap & NFP_NET_CFG_CTRL_L2BC     ? "L2BCFILT " : "",
3821 		nn->cap & NFP_NET_CFG_CTRL_L2MC     ? "L2MCFILT " : "",
3822 		nn->cap & NFP_NET_CFG_CTRL_RXCSUM   ? "RXCSUM "   : "",
3823 		nn->cap & NFP_NET_CFG_CTRL_TXCSUM   ? "TXCSUM "   : "",
3824 		nn->cap & NFP_NET_CFG_CTRL_RXVLAN   ? "RXVLAN "   : "",
3825 		nn->cap & NFP_NET_CFG_CTRL_TXVLAN   ? "TXVLAN "   : "",
3826 		nn->cap & NFP_NET_CFG_CTRL_SCATTER  ? "SCATTER "  : "",
3827 		nn->cap & NFP_NET_CFG_CTRL_GATHER   ? "GATHER "   : "",
3828 		nn->cap & NFP_NET_CFG_CTRL_LSO      ? "TSO1 "     : "",
3829 		nn->cap & NFP_NET_CFG_CTRL_LSO2     ? "TSO2 "     : "",
3830 		nn->cap & NFP_NET_CFG_CTRL_RSS      ? "RSS1 "     : "",
3831 		nn->cap & NFP_NET_CFG_CTRL_RSS2     ? "RSS2 "     : "",
3832 		nn->cap & NFP_NET_CFG_CTRL_CTAG_FILTER ? "CTAG_FILTER " : "",
3833 		nn->cap & NFP_NET_CFG_CTRL_MSIXAUTO ? "AUTOMASK " : "",
3834 		nn->cap & NFP_NET_CFG_CTRL_IRQMOD   ? "IRQMOD "   : "",
3835 		nn->cap & NFP_NET_CFG_CTRL_VXLAN    ? "VXLAN "    : "",
3836 		nn->cap & NFP_NET_CFG_CTRL_NVGRE    ? "NVGRE "	  : "",
3837 		nn->cap & NFP_NET_CFG_CTRL_CSUM_COMPLETE ?
3838 						      "RXCSUM_COMPLETE " : "",
3839 		nn->cap & NFP_NET_CFG_CTRL_LIVE_ADDR ? "LIVE_ADDR " : "",
3840 		nfp_app_extra_cap(nn->app, nn));
3841 }
3842 
3843 /**
3844  * nfp_net_alloc() - Allocate netdev and related structure
3845  * @pdev:         PCI device
3846  * @ctrl_bar:     PCI IOMEM with vNIC config memory
3847  * @needs_netdev: Whether to allocate a netdev for this vNIC
3848  * @max_tx_rings: Maximum number of TX rings supported by device
3849  * @max_rx_rings: Maximum number of RX rings supported by device
3850  *
3851  * This function allocates a netdev device and fills in the initial
3852  * part of the @struct nfp_net structure.  In case of control device
3853  * nfp_net structure is allocated without the netdev.
3854  *
3855  * Return: NFP Net device structure, or ERR_PTR on error.
3856  */
3857 struct nfp_net *
nfp_net_alloc(struct pci_dev * pdev,void __iomem * ctrl_bar,bool needs_netdev,unsigned int max_tx_rings,unsigned int max_rx_rings)3858 nfp_net_alloc(struct pci_dev *pdev, void __iomem *ctrl_bar, bool needs_netdev,
3859 	      unsigned int max_tx_rings, unsigned int max_rx_rings)
3860 {
3861 	struct nfp_net *nn;
3862 	int err;
3863 
3864 	if (needs_netdev) {
3865 		struct net_device *netdev;
3866 
3867 		netdev = alloc_etherdev_mqs(sizeof(struct nfp_net),
3868 					    max_tx_rings, max_rx_rings);
3869 		if (!netdev)
3870 			return ERR_PTR(-ENOMEM);
3871 
3872 		SET_NETDEV_DEV(netdev, &pdev->dev);
3873 		nn = netdev_priv(netdev);
3874 		nn->dp.netdev = netdev;
3875 	} else {
3876 		nn = vzalloc(sizeof(*nn));
3877 		if (!nn)
3878 			return ERR_PTR(-ENOMEM);
3879 	}
3880 
3881 	nn->dp.dev = &pdev->dev;
3882 	nn->dp.ctrl_bar = ctrl_bar;
3883 	nn->pdev = pdev;
3884 
3885 	nn->max_tx_rings = max_tx_rings;
3886 	nn->max_rx_rings = max_rx_rings;
3887 
3888 	nn->dp.num_tx_rings = min_t(unsigned int,
3889 				    max_tx_rings, num_online_cpus());
3890 	nn->dp.num_rx_rings = min_t(unsigned int, max_rx_rings,
3891 				 netif_get_num_default_rss_queues());
3892 
3893 	nn->dp.num_r_vecs = max(nn->dp.num_tx_rings, nn->dp.num_rx_rings);
3894 	nn->dp.num_r_vecs = min_t(unsigned int,
3895 				  nn->dp.num_r_vecs, num_online_cpus());
3896 
3897 	nn->dp.txd_cnt = NFP_NET_TX_DESCS_DEFAULT;
3898 	nn->dp.rxd_cnt = NFP_NET_RX_DESCS_DEFAULT;
3899 
3900 	sema_init(&nn->bar_lock, 1);
3901 
3902 	spin_lock_init(&nn->reconfig_lock);
3903 	spin_lock_init(&nn->link_status_lock);
3904 
3905 	timer_setup(&nn->reconfig_timer, nfp_net_reconfig_timer, 0);
3906 
3907 	err = nfp_net_tlv_caps_parse(&nn->pdev->dev, nn->dp.ctrl_bar,
3908 				     &nn->tlv_caps);
3909 	if (err)
3910 		goto err_free_nn;
3911 
3912 	err = nfp_ccm_mbox_alloc(nn);
3913 	if (err)
3914 		goto err_free_nn;
3915 
3916 	return nn;
3917 
3918 err_free_nn:
3919 	if (nn->dp.netdev)
3920 		free_netdev(nn->dp.netdev);
3921 	else
3922 		vfree(nn);
3923 	return ERR_PTR(err);
3924 }
3925 
3926 /**
3927  * nfp_net_free() - Undo what @nfp_net_alloc() did
3928  * @nn:      NFP Net device to reconfigure
3929  */
nfp_net_free(struct nfp_net * nn)3930 void nfp_net_free(struct nfp_net *nn)
3931 {
3932 	WARN_ON(timer_pending(&nn->reconfig_timer) || nn->reconfig_posted);
3933 	nfp_ccm_mbox_free(nn);
3934 
3935 	if (nn->dp.netdev)
3936 		free_netdev(nn->dp.netdev);
3937 	else
3938 		vfree(nn);
3939 }
3940 
3941 /**
3942  * nfp_net_rss_key_sz() - Get current size of the RSS key
3943  * @nn:		NFP Net device instance
3944  *
3945  * Return: size of the RSS key for currently selected hash function.
3946  */
nfp_net_rss_key_sz(struct nfp_net * nn)3947 unsigned int nfp_net_rss_key_sz(struct nfp_net *nn)
3948 {
3949 	switch (nn->rss_hfunc) {
3950 	case ETH_RSS_HASH_TOP:
3951 		return NFP_NET_CFG_RSS_KEY_SZ;
3952 	case ETH_RSS_HASH_XOR:
3953 		return 0;
3954 	case ETH_RSS_HASH_CRC32:
3955 		return 4;
3956 	}
3957 
3958 	nn_warn(nn, "Unknown hash function: %u\n", nn->rss_hfunc);
3959 	return 0;
3960 }
3961 
3962 /**
3963  * nfp_net_rss_init() - Set the initial RSS parameters
3964  * @nn:	     NFP Net device to reconfigure
3965  */
nfp_net_rss_init(struct nfp_net * nn)3966 static void nfp_net_rss_init(struct nfp_net *nn)
3967 {
3968 	unsigned long func_bit, rss_cap_hfunc;
3969 	u32 reg;
3970 
3971 	/* Read the RSS function capability and select first supported func */
3972 	reg = nn_readl(nn, NFP_NET_CFG_RSS_CAP);
3973 	rss_cap_hfunc =	FIELD_GET(NFP_NET_CFG_RSS_CAP_HFUNC, reg);
3974 	if (!rss_cap_hfunc)
3975 		rss_cap_hfunc =	FIELD_GET(NFP_NET_CFG_RSS_CAP_HFUNC,
3976 					  NFP_NET_CFG_RSS_TOEPLITZ);
3977 
3978 	func_bit = find_first_bit(&rss_cap_hfunc, NFP_NET_CFG_RSS_HFUNCS);
3979 	if (func_bit == NFP_NET_CFG_RSS_HFUNCS) {
3980 		dev_warn(nn->dp.dev,
3981 			 "Bad RSS config, defaulting to Toeplitz hash\n");
3982 		func_bit = ETH_RSS_HASH_TOP_BIT;
3983 	}
3984 	nn->rss_hfunc = 1 << func_bit;
3985 
3986 	netdev_rss_key_fill(nn->rss_key, nfp_net_rss_key_sz(nn));
3987 
3988 	nfp_net_rss_init_itbl(nn);
3989 
3990 	/* Enable IPv4/IPv6 TCP by default */
3991 	nn->rss_cfg = NFP_NET_CFG_RSS_IPV4_TCP |
3992 		      NFP_NET_CFG_RSS_IPV6_TCP |
3993 		      FIELD_PREP(NFP_NET_CFG_RSS_HFUNC, nn->rss_hfunc) |
3994 		      NFP_NET_CFG_RSS_MASK;
3995 }
3996 
3997 /**
3998  * nfp_net_irqmod_init() - Set the initial IRQ moderation parameters
3999  * @nn:	     NFP Net device to reconfigure
4000  */
nfp_net_irqmod_init(struct nfp_net * nn)4001 static void nfp_net_irqmod_init(struct nfp_net *nn)
4002 {
4003 	nn->rx_coalesce_usecs      = 50;
4004 	nn->rx_coalesce_max_frames = 64;
4005 	nn->tx_coalesce_usecs      = 50;
4006 	nn->tx_coalesce_max_frames = 64;
4007 
4008 	nn->rx_coalesce_adapt_on   = true;
4009 	nn->tx_coalesce_adapt_on   = true;
4010 }
4011 
nfp_net_netdev_init(struct nfp_net * nn)4012 static void nfp_net_netdev_init(struct nfp_net *nn)
4013 {
4014 	struct net_device *netdev = nn->dp.netdev;
4015 
4016 	nfp_net_write_mac_addr(nn, nn->dp.netdev->dev_addr);
4017 
4018 	netdev->mtu = nn->dp.mtu;
4019 
4020 	/* Advertise/enable offloads based on capabilities
4021 	 *
4022 	 * Note: netdev->features show the currently enabled features
4023 	 * and netdev->hw_features advertises which features are
4024 	 * supported.  By default we enable most features.
4025 	 */
4026 	if (nn->cap & NFP_NET_CFG_CTRL_LIVE_ADDR)
4027 		netdev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
4028 
4029 	netdev->hw_features = NETIF_F_HIGHDMA;
4030 	if (nn->cap & NFP_NET_CFG_CTRL_RXCSUM_ANY) {
4031 		netdev->hw_features |= NETIF_F_RXCSUM;
4032 		nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_RXCSUM_ANY;
4033 	}
4034 	if (nn->cap & NFP_NET_CFG_CTRL_TXCSUM) {
4035 		netdev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
4036 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_TXCSUM;
4037 	}
4038 	if (nn->cap & NFP_NET_CFG_CTRL_GATHER) {
4039 		netdev->hw_features |= NETIF_F_SG;
4040 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_GATHER;
4041 	}
4042 	if ((nn->cap & NFP_NET_CFG_CTRL_LSO && nn->fw_ver.major > 2) ||
4043 	    nn->cap & NFP_NET_CFG_CTRL_LSO2) {
4044 		netdev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
4045 		nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_LSO2 ?:
4046 					 NFP_NET_CFG_CTRL_LSO;
4047 	}
4048 	if (nn->cap & NFP_NET_CFG_CTRL_RSS_ANY)
4049 		netdev->hw_features |= NETIF_F_RXHASH;
4050 	if (nn->cap & NFP_NET_CFG_CTRL_VXLAN) {
4051 		if (nn->cap & NFP_NET_CFG_CTRL_LSO)
4052 			netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL;
4053 		netdev->udp_tunnel_nic_info = &nfp_udp_tunnels;
4054 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_VXLAN;
4055 	}
4056 	if (nn->cap & NFP_NET_CFG_CTRL_NVGRE) {
4057 		if (nn->cap & NFP_NET_CFG_CTRL_LSO)
4058 			netdev->hw_features |= NETIF_F_GSO_GRE;
4059 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_NVGRE;
4060 	}
4061 	if (nn->cap & (NFP_NET_CFG_CTRL_VXLAN | NFP_NET_CFG_CTRL_NVGRE))
4062 		netdev->hw_enc_features = netdev->hw_features;
4063 
4064 	netdev->vlan_features = netdev->hw_features;
4065 
4066 	if (nn->cap & NFP_NET_CFG_CTRL_RXVLAN) {
4067 		netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
4068 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_RXVLAN;
4069 	}
4070 	if (nn->cap & NFP_NET_CFG_CTRL_TXVLAN) {
4071 		if (nn->cap & NFP_NET_CFG_CTRL_LSO2) {
4072 			nn_warn(nn, "Device advertises both TSO2 and TXVLAN. Refusing to enable TXVLAN.\n");
4073 		} else {
4074 			netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
4075 			nn->dp.ctrl |= NFP_NET_CFG_CTRL_TXVLAN;
4076 		}
4077 	}
4078 	if (nn->cap & NFP_NET_CFG_CTRL_CTAG_FILTER) {
4079 		netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
4080 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_CTAG_FILTER;
4081 	}
4082 
4083 	netdev->features = netdev->hw_features;
4084 
4085 	if (nfp_app_has_tc(nn->app) && nn->port)
4086 		netdev->hw_features |= NETIF_F_HW_TC;
4087 
4088 	/* Advertise but disable TSO by default. */
4089 	netdev->features &= ~(NETIF_F_TSO | NETIF_F_TSO6);
4090 	nn->dp.ctrl &= ~NFP_NET_CFG_CTRL_LSO_ANY;
4091 
4092 	/* Finalise the netdev setup */
4093 	netdev->netdev_ops = &nfp_net_netdev_ops;
4094 	netdev->watchdog_timeo = msecs_to_jiffies(5 * 1000);
4095 
4096 	/* MTU range: 68 - hw-specific max */
4097 	netdev->min_mtu = ETH_MIN_MTU;
4098 	netdev->max_mtu = nn->max_mtu;
4099 
4100 	netdev->gso_max_segs = NFP_NET_LSO_MAX_SEGS;
4101 
4102 	netif_carrier_off(netdev);
4103 
4104 	nfp_net_set_ethtool_ops(netdev);
4105 }
4106 
nfp_net_read_caps(struct nfp_net * nn)4107 static int nfp_net_read_caps(struct nfp_net *nn)
4108 {
4109 	/* Get some of the read-only fields from the BAR */
4110 	nn->cap = nn_readl(nn, NFP_NET_CFG_CAP);
4111 	nn->max_mtu = nn_readl(nn, NFP_NET_CFG_MAX_MTU);
4112 
4113 	/* ABI 4.x and ctrl vNIC always use chained metadata, in other cases
4114 	 * we allow use of non-chained metadata if RSS(v1) is the only
4115 	 * advertised capability requiring metadata.
4116 	 */
4117 	nn->dp.chained_metadata_format = nn->fw_ver.major == 4 ||
4118 					 !nn->dp.netdev ||
4119 					 !(nn->cap & NFP_NET_CFG_CTRL_RSS) ||
4120 					 nn->cap & NFP_NET_CFG_CTRL_CHAIN_META;
4121 	/* RSS(v1) uses non-chained metadata format, except in ABI 4.x where
4122 	 * it has the same meaning as RSSv2.
4123 	 */
4124 	if (nn->dp.chained_metadata_format && nn->fw_ver.major != 4)
4125 		nn->cap &= ~NFP_NET_CFG_CTRL_RSS;
4126 
4127 	/* Determine RX packet/metadata boundary offset */
4128 	if (nn->fw_ver.major >= 2) {
4129 		u32 reg;
4130 
4131 		reg = nn_readl(nn, NFP_NET_CFG_RX_OFFSET);
4132 		if (reg > NFP_NET_MAX_PREPEND) {
4133 			nn_err(nn, "Invalid rx offset: %d\n", reg);
4134 			return -EINVAL;
4135 		}
4136 		nn->dp.rx_offset = reg;
4137 	} else {
4138 		nn->dp.rx_offset = NFP_NET_RX_OFFSET;
4139 	}
4140 
4141 	/* For control vNICs mask out the capabilities app doesn't want. */
4142 	if (!nn->dp.netdev)
4143 		nn->cap &= nn->app->type->ctrl_cap_mask;
4144 
4145 	return 0;
4146 }
4147 
4148 /**
4149  * nfp_net_init() - Initialise/finalise the nfp_net structure
4150  * @nn:		NFP Net device structure
4151  *
4152  * Return: 0 on success or negative errno on error.
4153  */
nfp_net_init(struct nfp_net * nn)4154 int nfp_net_init(struct nfp_net *nn)
4155 {
4156 	int err;
4157 
4158 	nn->dp.rx_dma_dir = DMA_FROM_DEVICE;
4159 
4160 	err = nfp_net_read_caps(nn);
4161 	if (err)
4162 		return err;
4163 
4164 	/* Set default MTU and Freelist buffer size */
4165 	if (!nfp_net_is_data_vnic(nn) && nn->app->ctrl_mtu) {
4166 		nn->dp.mtu = min(nn->app->ctrl_mtu, nn->max_mtu);
4167 	} else if (nn->max_mtu < NFP_NET_DEFAULT_MTU) {
4168 		nn->dp.mtu = nn->max_mtu;
4169 	} else {
4170 		nn->dp.mtu = NFP_NET_DEFAULT_MTU;
4171 	}
4172 	nn->dp.fl_bufsz = nfp_net_calc_fl_bufsz(&nn->dp);
4173 
4174 	if (nfp_app_ctrl_uses_data_vnics(nn->app))
4175 		nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_CMSG_DATA;
4176 
4177 	if (nn->cap & NFP_NET_CFG_CTRL_RSS_ANY) {
4178 		nfp_net_rss_init(nn);
4179 		nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_RSS2 ?:
4180 					 NFP_NET_CFG_CTRL_RSS;
4181 	}
4182 
4183 	/* Allow L2 Broadcast and Multicast through by default, if supported */
4184 	if (nn->cap & NFP_NET_CFG_CTRL_L2BC)
4185 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_L2BC;
4186 
4187 	/* Allow IRQ moderation, if supported */
4188 	if (nn->cap & NFP_NET_CFG_CTRL_IRQMOD) {
4189 		nfp_net_irqmod_init(nn);
4190 		nn->dp.ctrl |= NFP_NET_CFG_CTRL_IRQMOD;
4191 	}
4192 
4193 	/* Stash the re-configuration queue away.  First odd queue in TX Bar */
4194 	nn->qcp_cfg = nn->tx_bar + NFP_QCP_QUEUE_ADDR_SZ;
4195 
4196 	/* Make sure the FW knows the netdev is supposed to be disabled here */
4197 	nn_writel(nn, NFP_NET_CFG_CTRL, 0);
4198 	nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, 0);
4199 	nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, 0);
4200 	err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_RING |
4201 				   NFP_NET_CFG_UPDATE_GEN);
4202 	if (err)
4203 		return err;
4204 
4205 	if (nn->dp.netdev) {
4206 		nfp_net_netdev_init(nn);
4207 
4208 		err = nfp_ccm_mbox_init(nn);
4209 		if (err)
4210 			return err;
4211 
4212 		err = nfp_net_tls_init(nn);
4213 		if (err)
4214 			goto err_clean_mbox;
4215 	}
4216 
4217 	nfp_net_vecs_init(nn);
4218 
4219 	if (!nn->dp.netdev)
4220 		return 0;
4221 	return register_netdev(nn->dp.netdev);
4222 
4223 err_clean_mbox:
4224 	nfp_ccm_mbox_clean(nn);
4225 	return err;
4226 }
4227 
4228 /**
4229  * nfp_net_clean() - Undo what nfp_net_init() did.
4230  * @nn:		NFP Net device structure
4231  */
nfp_net_clean(struct nfp_net * nn)4232 void nfp_net_clean(struct nfp_net *nn)
4233 {
4234 	if (!nn->dp.netdev)
4235 		return;
4236 
4237 	unregister_netdev(nn->dp.netdev);
4238 	nfp_ccm_mbox_clean(nn);
4239 	nfp_net_reconfig_wait_posted(nn);
4240 }
4241