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