nfp_net_common.c 100.0 KB
Newer Older
1
/*
2
 * Copyright (C) 2015-2017 Netronome Systems, Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
 *
 * This software is dual licensed under the GNU General License Version 2,
 * June 1991 as shown in the file COPYING in the top-level directory of this
 * source tree or the BSD 2-Clause License provided below.  You have the
 * option to license this software under the complete terms of either license.
 *
 * The BSD 2-Clause License:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      1. Redistributions of source code must retain the above
 *         copyright notice, this list of conditions and the following
 *         disclaimer.
 *
 *      2. Redistributions in binary form must reproduce the above
 *         copyright notice, this list of conditions and the following
 *         disclaimer in the documentation and/or other materials
 *         provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/*
 * nfp_net_common.c
 * Netronome network device driver: Common functions between PF and VF
 * Authors: Jakub Kicinski <jakub.kicinski@netronome.com>
 *          Jason McMullan <jason.mcmullan@netronome.com>
 *          Rolf Neugebauer <rolf.neugebauer@netronome.com>
 *          Brad Petrus <brad.petrus@netronome.com>
 *          Chris Telfer <chris.telfer@netronome.com>
 */

44
#include <linux/bitfield.h>
45
#include <linux/bpf.h>
46
#include <linux/bpf_trace.h>
47 48 49 50 51 52 53 54 55
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/interrupt.h>
#include <linux/ip.h>
#include <linux/ipv6.h>
56
#include <linux/page_ref.h>
57 58 59 60 61 62 63
#include <linux/pci.h>
#include <linux/pci_regs.h>
#include <linux/msi.h>
#include <linux/ethtool.h>
#include <linux/log2.h>
#include <linux/if_vlan.h>
#include <linux/random.h>
64
#include <linux/vmalloc.h>
65 66
#include <linux/ktime.h>

S
Simon Horman 已提交
67
#include <net/switchdev.h>
68 69
#include <net/vxlan.h>

70
#include "nfpcore/nfp_nsp.h"
71
#include "nfp_app.h"
72 73
#include "nfp_net_ctrl.h"
#include "nfp_net.h"
74
#include "nfp_net_sriov.h"
J
Jakub Kicinski 已提交
75
#include "nfp_port.h"
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

/**
 * nfp_net_get_fw_version() - Read and parse the FW version
 * @fw_ver:	Output fw_version structure to read to
 * @ctrl_bar:	Mapped address of the control BAR
 */
void nfp_net_get_fw_version(struct nfp_net_fw_version *fw_ver,
			    void __iomem *ctrl_bar)
{
	u32 reg;

	reg = readl(ctrl_bar + NFP_NET_CFG_VERSION);
	put_unaligned_le32(reg, fw_ver);
}

91
static dma_addr_t nfp_net_dma_map_rx(struct nfp_net_dp *dp, void *frag)
92
{
93 94 95 96 97 98 99 100 101 102 103
	return dma_map_single_attrs(dp->dev, frag + NFP_NET_RX_BUF_HEADROOM,
				    dp->fl_bufsz - NFP_NET_RX_BUF_NON_DATA,
				    dp->rx_dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
}

static void
nfp_net_dma_sync_dev_rx(const struct nfp_net_dp *dp, dma_addr_t dma_addr)
{
	dma_sync_single_for_device(dp->dev, dma_addr,
				   dp->fl_bufsz - NFP_NET_RX_BUF_NON_DATA,
				   dp->rx_dma_dir);
104 105
}

106
static void nfp_net_dma_unmap_rx(struct nfp_net_dp *dp, dma_addr_t dma_addr)
107
{
108 109 110 111 112 113 114 115 116 117
	dma_unmap_single_attrs(dp->dev, dma_addr,
			       dp->fl_bufsz - NFP_NET_RX_BUF_NON_DATA,
			       dp->rx_dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
}

static void nfp_net_dma_sync_cpu_rx(struct nfp_net_dp *dp, dma_addr_t dma_addr,
				    unsigned int len)
{
	dma_sync_single_for_cpu(dp->dev, dma_addr - NFP_NET_RX_BUF_HEADROOM,
				len, dp->rx_dma_dir);
118 119
}

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
/* Firmware reconfig
 *
 * Firmware reconfig may take a while so we have two versions of it -
 * synchronous and asynchronous (posted).  All synchronous callers are holding
 * RTNL so we don't have to worry about serializing them.
 */
static void nfp_net_reconfig_start(struct nfp_net *nn, u32 update)
{
	nn_writel(nn, NFP_NET_CFG_UPDATE, update);
	/* ensure update is written before pinging HW */
	nn_pci_flush(nn);
	nfp_qcp_wr_ptr_add(nn->qcp_cfg, 1);
}

/* Pass 0 as update to run posted reconfigs. */
static void nfp_net_reconfig_start_async(struct nfp_net *nn, u32 update)
{
	update |= nn->reconfig_posted;
	nn->reconfig_posted = 0;

	nfp_net_reconfig_start(nn, update);

	nn->reconfig_timer_active = true;
	mod_timer(&nn->reconfig_timer, jiffies + NFP_NET_POLL_TIMEOUT * HZ);
}

static bool nfp_net_reconfig_check_done(struct nfp_net *nn, bool last_check)
{
	u32 reg;

	reg = nn_readl(nn, NFP_NET_CFG_UPDATE);
	if (reg == 0)
		return true;
	if (reg & NFP_NET_CFG_UPDATE_ERR) {
		nn_err(nn, "Reconfig error: 0x%08x\n", reg);
		return true;
	} else if (last_check) {
		nn_err(nn, "Reconfig timeout: 0x%08x\n", reg);
		return true;
	}

	return false;
}

static int nfp_net_reconfig_wait(struct nfp_net *nn, unsigned long deadline)
{
	bool timed_out = false;

	/* Poll update field, waiting for NFP to ack the config */
	while (!nfp_net_reconfig_check_done(nn, timed_out)) {
		msleep(1);
		timed_out = time_is_before_eq_jiffies(deadline);
	}

	if (nn_readl(nn, NFP_NET_CFG_UPDATE) & NFP_NET_CFG_UPDATE_ERR)
		return -EIO;

	return timed_out ? -EIO : 0;
}

180
static void nfp_net_reconfig_timer(struct timer_list *t)
181
{
182
	struct nfp_net *nn = from_timer(nn, t, reconfig_timer);
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

	spin_lock_bh(&nn->reconfig_lock);

	nn->reconfig_timer_active = false;

	/* If sync caller is present it will take over from us */
	if (nn->reconfig_sync_present)
		goto done;

	/* Read reconfig status and report errors */
	nfp_net_reconfig_check_done(nn, true);

	if (nn->reconfig_posted)
		nfp_net_reconfig_start_async(nn, 0);
done:
	spin_unlock_bh(&nn->reconfig_lock);
}

/**
 * nfp_net_reconfig_post() - Post async reconfig request
 * @nn:      NFP Net device to reconfigure
 * @update:  The value for the update field in the BAR config
 *
 * Record FW reconfiguration request.  Reconfiguration will be kicked off
 * whenever reconfiguration machinery is idle.  Multiple requests can be
 * merged together!
 */
static void nfp_net_reconfig_post(struct nfp_net *nn, u32 update)
{
	spin_lock_bh(&nn->reconfig_lock);

	/* Sync caller will kick off async reconf when it's done, just post */
	if (nn->reconfig_sync_present) {
		nn->reconfig_posted |= update;
		goto done;
	}

	/* Opportunistically check if the previous command is done */
	if (!nn->reconfig_timer_active ||
	    nfp_net_reconfig_check_done(nn, false))
		nfp_net_reconfig_start_async(nn, update);
	else
		nn->reconfig_posted |= update;
done:
	spin_unlock_bh(&nn->reconfig_lock);
}

230 231 232 233 234 235 236 237 238 239 240 241 242
/**
 * nfp_net_reconfig() - Reconfigure the firmware
 * @nn:      NFP Net device to reconfigure
 * @update:  The value for the update field in the BAR config
 *
 * Write the update word to the BAR and ping the reconfig queue.  The
 * poll until the firmware has acknowledged the update by zeroing the
 * update word.
 *
 * Return: Negative errno on error, 0 on success
 */
int nfp_net_reconfig(struct nfp_net *nn, u32 update)
{
243 244 245
	bool cancelled_timer = false;
	u32 pre_posted_requests;
	int ret;
246 247 248

	spin_lock_bh(&nn->reconfig_lock);

249
	nn->reconfig_sync_present = true;
250

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
	if (nn->reconfig_timer_active) {
		del_timer(&nn->reconfig_timer);
		nn->reconfig_timer_active = false;
		cancelled_timer = true;
	}
	pre_posted_requests = nn->reconfig_posted;
	nn->reconfig_posted = 0;

	spin_unlock_bh(&nn->reconfig_lock);

	if (cancelled_timer)
		nfp_net_reconfig_wait(nn, nn->reconfig_timer.expires);

	/* Run the posted reconfigs which were issued before we started */
	if (pre_posted_requests) {
		nfp_net_reconfig_start(nn, pre_posted_requests);
		nfp_net_reconfig_wait(nn, jiffies + HZ * NFP_NET_POLL_TIMEOUT);
268 269
	}

270 271 272 273 274 275 276 277 278 279
	nfp_net_reconfig_start(nn, update);
	ret = nfp_net_reconfig_wait(nn, jiffies + HZ * NFP_NET_POLL_TIMEOUT);

	spin_lock_bh(&nn->reconfig_lock);

	if (nn->reconfig_posted)
		nfp_net_reconfig_start_async(nn, 0);

	nn->reconfig_sync_present = false;

280
	spin_unlock_bh(&nn->reconfig_lock);
281

282 283 284
	return ret;
}

P
Pablo Cascón 已提交
285 286 287 288 289 290 291 292 293 294 295
/**
 * nfp_net_reconfig_mbox() - Reconfigure the firmware via the mailbox
 * @nn:        NFP Net device to reconfigure
 * @mbox_cmd:  The value for the mailbox command
 *
 * Helper function for mailbox updates
 *
 * Return: Negative errno on error, 0 on success
 */
static int nfp_net_reconfig_mbox(struct nfp_net *nn, u32 mbox_cmd)
{
296
	u32 mbox = nn->tlv_caps.mbox_off;
P
Pablo Cascón 已提交
297 298
	int ret;

299 300 301 302 303 304
	if (!nfp_net_has_mbox(&nn->tlv_caps)) {
		nn_err(nn, "no mailbox present, command: %u\n", mbox_cmd);
		return -EIO;
	}

	nn_writeq(nn, mbox + NFP_NET_CFG_MBOX_SIMPLE_CMD, mbox_cmd);
P
Pablo Cascón 已提交
305 306 307 308 309 310 311

	ret = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_MBOX);
	if (ret) {
		nn_err(nn, "Mailbox update error\n");
		return ret;
	}

312
	return -nn_readl(nn, mbox + NFP_NET_CFG_MBOX_SIMPLE_RET);
P
Pablo Cascón 已提交
313 314
}

315 316 317 318 319 320 321 322
/* Interrupt configuration and handling
 */

/**
 * nfp_net_irq_unmask() - Unmask automasked interrupt
 * @nn:       NFP Network structure
 * @entry_nr: MSI-X table entry
 *
J
Jakub Kicinski 已提交
323
 * Clear the ICR for the IRQ entry.
324 325 326 327 328 329 330 331
 */
static void nfp_net_irq_unmask(struct nfp_net *nn, unsigned int entry_nr)
{
	nn_writeb(nn, NFP_NET_CFG_ICR(entry_nr), NFP_NET_CFG_ICR_UNMASKED);
	nn_pci_flush(nn);
}

/**
332 333 334 335 336
 * nfp_net_irqs_alloc() - allocates MSI-X irqs
 * @pdev:        PCI device structure
 * @irq_entries: Array to be initialized and used to hold the irq entries
 * @min_irqs:    Minimal acceptable number of interrupts
 * @wanted_irqs: Target number of interrupts to allocate
337
 *
338
 * Return: Number of irqs obtained or 0 on error.
339
 */
340 341 342
unsigned int
nfp_net_irqs_alloc(struct pci_dev *pdev, struct msix_entry *irq_entries,
		   unsigned int min_irqs, unsigned int wanted_irqs)
343
{
344 345
	unsigned int i;
	int got_irqs;
346

347 348
	for (i = 0; i < wanted_irqs; i++)
		irq_entries[i].entry = i;
349

350 351 352 353 354
	got_irqs = pci_enable_msix_range(pdev, irq_entries,
					 min_irqs, wanted_irqs);
	if (got_irqs < 0) {
		dev_err(&pdev->dev, "Failed to enable %d-%d MSI-X (err=%d)\n",
			min_irqs, wanted_irqs, got_irqs);
355 356 357
		return 0;
	}

358 359 360 361 362
	if (got_irqs < wanted_irqs)
		dev_warn(&pdev->dev, "Unable to allocate %d IRQs got only %d\n",
			 wanted_irqs, got_irqs);

	return got_irqs;
363 364 365
}

/**
366 367 368 369
 * nfp_net_irqs_assign() - Assign interrupts allocated externally to netdev
 * @nn:		 NFP Network structure
 * @irq_entries: Table of allocated interrupts
 * @n:		 Size of @irq_entries (number of entries to grab)
370
 *
371 372
 * After interrupts are allocated with nfp_net_irqs_alloc() this function
 * should be called to assign them to a specific netdev (port).
373
 */
374 375 376
void
nfp_net_irqs_assign(struct nfp_net *nn, struct msix_entry *irq_entries,
		    unsigned int n)
377
{
378 379
	struct nfp_net_dp *dp = &nn->dp;

380
	nn->max_r_vecs = n - NFP_NET_NON_Q_VECTORS;
381
	dp->num_r_vecs = nn->max_r_vecs;
382

383
	memcpy(nn->irq_entries, irq_entries, sizeof(*irq_entries) * n);
384

385 386
	if (dp->num_rx_rings > dp->num_r_vecs ||
	    dp->num_tx_rings > dp->num_r_vecs)
387 388 389
		dev_warn(nn->dp.dev, "More rings (%d,%d) than vectors (%d).\n",
			 dp->num_rx_rings, dp->num_tx_rings,
			 dp->num_r_vecs);
390

391 392 393
	dp->num_rx_rings = min(dp->num_r_vecs, dp->num_rx_rings);
	dp->num_tx_rings = min(dp->num_r_vecs, dp->num_tx_rings);
	dp->num_stack_tx_rings = dp->num_tx_rings;
394 395 396 397
}

/**
 * nfp_net_irqs_disable() - Disable interrupts
398
 * @pdev:        PCI device structure
399 400 401
 *
 * Undoes what @nfp_net_irqs_alloc() does.
 */
402
void nfp_net_irqs_disable(struct pci_dev *pdev)
403
{
404
	pci_disable_msix(pdev);
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
}

/**
 * nfp_net_irq_rxtx() - Interrupt service routine for RX/TX rings.
 * @irq:      Interrupt
 * @data:     Opaque data structure
 *
 * Return: Indicate if the interrupt has been handled.
 */
static irqreturn_t nfp_net_irq_rxtx(int irq, void *data)
{
	struct nfp_net_r_vector *r_vec = data;

	napi_schedule_irqoff(&r_vec->napi);

	/* The FW auto-masks any interrupt, either via the MASK bit in
	 * the MSI-X table or via the per entry ICR field.  So there
	 * is no need to disable interrupts here.
	 */
	return IRQ_HANDLED;
}

J
Jakub Kicinski 已提交
427 428 429 430 431 432 433 434 435
static irqreturn_t nfp_ctrl_irq_rxtx(int irq, void *data)
{
	struct nfp_net_r_vector *r_vec = data;

	tasklet_schedule(&r_vec->tasklet);

	return IRQ_HANDLED;
}

436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
/**
 * nfp_net_read_link_status() - Reread link status from control BAR
 * @nn:       NFP Network structure
 */
static void nfp_net_read_link_status(struct nfp_net *nn)
{
	unsigned long flags;
	bool link_up;
	u32 sts;

	spin_lock_irqsave(&nn->link_status_lock, flags);

	sts = nn_readl(nn, NFP_NET_CFG_STS);
	link_up = !!(sts & NFP_NET_CFG_STS_LINK);

	if (nn->link_up == link_up)
		goto out;

	nn->link_up = link_up;
455 456
	if (nn->port)
		set_bit(NFP_PORT_CHANGED, &nn->port->flags);
457 458

	if (nn->link_up) {
459 460
		netif_carrier_on(nn->dp.netdev);
		netdev_info(nn->dp.netdev, "NIC Link is Up\n");
461
	} else {
462 463
		netif_carrier_off(nn->dp.netdev);
		netdev_info(nn->dp.netdev, "NIC Link is Down\n");
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
	}
out:
	spin_unlock_irqrestore(&nn->link_status_lock, flags);
}

/**
 * nfp_net_irq_lsc() - Interrupt service routine for link state changes
 * @irq:      Interrupt
 * @data:     Opaque data structure
 *
 * Return: Indicate if the interrupt has been handled.
 */
static irqreturn_t nfp_net_irq_lsc(int irq, void *data)
{
	struct nfp_net *nn = data;
479 480 481
	struct msix_entry *entry;

	entry = &nn->irq_entries[NFP_NET_IRQ_LSC_IDX];
482 483 484

	nfp_net_read_link_status(nn);

485
	nfp_net_irq_unmask(nn, entry->entry);
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508

	return IRQ_HANDLED;
}

/**
 * nfp_net_irq_exn() - Interrupt service routine for exceptions
 * @irq:      Interrupt
 * @data:     Opaque data structure
 *
 * Return: Indicate if the interrupt has been handled.
 */
static irqreturn_t nfp_net_irq_exn(int irq, void *data)
{
	struct nfp_net *nn = data;

	nn_err(nn, "%s: UNIMPLEMENTED.\n", __func__);
	/* XXX TO BE IMPLEMENTED */
	return IRQ_HANDLED;
}

/**
 * nfp_net_tx_ring_init() - Fill in the boilerplate for a TX ring
 * @tx_ring:  TX ring structure
509 510
 * @r_vec:    IRQ vector servicing this ring
 * @idx:      Ring index
511
 * @is_xdp:   Is this an XDP TX ring?
512
 */
513 514
static void
nfp_net_tx_ring_init(struct nfp_net_tx_ring *tx_ring,
515 516
		     struct nfp_net_r_vector *r_vec, unsigned int idx,
		     bool is_xdp)
517 518 519
{
	struct nfp_net *nn = r_vec->nfp_net;

520 521
	tx_ring->idx = idx;
	tx_ring->r_vec = r_vec;
522
	tx_ring->is_xdp = is_xdp;
523
	u64_stats_init(&tx_ring->r_vec->tx_sync);
524

525 526 527 528 529 530 531
	tx_ring->qcidx = tx_ring->idx * nn->stride_tx;
	tx_ring->qcp_q = nn->tx_bar + NFP_QCP_QUEUE_OFF(tx_ring->qcidx);
}

/**
 * nfp_net_rx_ring_init() - Fill in the boilerplate for a RX ring
 * @rx_ring:  RX ring structure
532 533
 * @r_vec:    IRQ vector servicing this ring
 * @idx:      Ring index
534
 */
535 536 537
static void
nfp_net_rx_ring_init(struct nfp_net_rx_ring *rx_ring,
		     struct nfp_net_r_vector *r_vec, unsigned int idx)
538 539 540
{
	struct nfp_net *nn = r_vec->nfp_net;

541 542
	rx_ring->idx = idx;
	rx_ring->r_vec = r_vec;
543
	u64_stats_init(&rx_ring->r_vec->rx_sync);
544

545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
	rx_ring->fl_qcidx = rx_ring->idx * nn->stride_rx;
	rx_ring->qcp_fl = nn->rx_bar + NFP_QCP_QUEUE_OFF(rx_ring->fl_qcidx);
}

/**
 * nfp_net_aux_irq_request() - Request an auxiliary interrupt (LSC or EXN)
 * @nn:		NFP Network structure
 * @ctrl_offset: Control BAR offset where IRQ configuration should be written
 * @format:	printf-style format to construct the interrupt name
 * @name:	Pointer to allocated space for interrupt name
 * @name_sz:	Size of space for interrupt name
 * @vector_idx:	Index of MSI-X vector used for this interrupt
 * @handler:	IRQ handler to register for this interrupt
 */
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)
{
	struct msix_entry *entry;
	int err;

	entry = &nn->irq_entries[vector_idx];

J
Jakub Kicinski 已提交
569
	snprintf(name, name_sz, format, nfp_net_name(nn));
570 571 572 573 574 575
	err = request_irq(entry->vector, handler, 0, name, nn);
	if (err) {
		nn_err(nn, "Failed to request IRQ %d (err=%d).\n",
		       entry->vector, err);
		return err;
	}
576
	nn_writeb(nn, ctrl_offset, entry->entry);
577
	nfp_net_irq_unmask(nn, entry->entry);
578 579 580 581 582 583 584 585 586 587 588 589 590 591

	return 0;
}

/**
 * nfp_net_aux_irq_free() - Free an auxiliary interrupt (LSC or EXN)
 * @nn:		NFP Network structure
 * @ctrl_offset: Control BAR offset where IRQ configuration should be written
 * @vector_idx:	Index of MSI-X vector used for this interrupt
 */
static void nfp_net_aux_irq_free(struct nfp_net *nn, u32 ctrl_offset,
				 unsigned int vector_idx)
{
	nn_writeb(nn, ctrl_offset, 0xff);
592
	nn_pci_flush(nn);
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
	free_irq(nn->irq_entries[vector_idx].vector, nn);
}

/* Transmit
 *
 * One queue controller peripheral queue is used for transmit.  The
 * driver en-queues packets for transmit by advancing the write
 * pointer.  The device indicates that packets have transmitted by
 * advancing the read pointer.  The driver maintains a local copy of
 * the read and write pointer in @struct nfp_net_tx_ring.  The driver
 * keeps @wr_p in sync with the queue controller write pointer and can
 * determine how many packets have been transmitted by comparing its
 * copy of the read pointer @rd_p with the read pointer maintained by
 * the queue controller peripheral.
 */

/**
 * nfp_net_tx_full() - Check if the TX ring is full
 * @tx_ring: TX ring to check
 * @dcnt:    Number of descriptors that need to be enqueued (must be >= 1)
 *
 * This function checks, based on the *host copy* of read/write
 * pointer if a given TX ring is full.  The real TX queue may have
 * some newly made available slots.
 *
 * Return: True if the ring is full.
 */
620
static int nfp_net_tx_full(struct nfp_net_tx_ring *tx_ring, int dcnt)
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
{
	return (tx_ring->wr_p - tx_ring->rd_p) >= (tx_ring->cnt - dcnt);
}

/* Wrappers for deciding when to stop and restart TX queues */
static int nfp_net_tx_ring_should_wake(struct nfp_net_tx_ring *tx_ring)
{
	return !nfp_net_tx_full(tx_ring, MAX_SKB_FRAGS * 4);
}

static int nfp_net_tx_ring_should_stop(struct nfp_net_tx_ring *tx_ring)
{
	return nfp_net_tx_full(tx_ring, MAX_SKB_FRAGS + 1);
}

/**
 * nfp_net_tx_ring_stop() - stop tx ring
 * @nd_q:    netdev queue
 * @tx_ring: driver tx queue structure
 *
 * Safely stop TX ring.  Remember that while we are running .start_xmit()
 * someone else may be cleaning the TX ring completions so we need to be
 * extra careful here.
 */
static void nfp_net_tx_ring_stop(struct netdev_queue *nd_q,
				 struct nfp_net_tx_ring *tx_ring)
{
	netif_tx_stop_queue(nd_q);

	/* We can race with the TX completion out of NAPI so recheck */
	smp_mb();
	if (unlikely(nfp_net_tx_ring_should_wake(tx_ring)))
		netif_tx_start_queue(nd_q);
}

/**
 * nfp_net_tx_tso() - Set up Tx descriptor for LSO
 * @r_vec: per-ring structure
 * @txbuf: Pointer to driver soft TX descriptor
 * @txd: Pointer to HW TX descriptor
 * @skb: Pointer to SKB
 *
 * Set up Tx descriptor for LSO, do nothing for non-LSO skbs.
 * Return error on packet header greater than maximum supported LSO header size.
 */
666
static void nfp_net_tx_tso(struct nfp_net_r_vector *r_vec,
667 668 669 670 671 672 673 674 675
			   struct nfp_net_tx_buf *txbuf,
			   struct nfp_net_tx_desc *txd, struct sk_buff *skb)
{
	u32 hdrlen;
	u16 mss;

	if (!skb_is_gso(skb))
		return;

E
Edwin Peer 已提交
676 677 678
	if (!skb->encapsulation) {
		txd->l3_offset = skb_network_offset(skb);
		txd->l4_offset = skb_transport_offset(skb);
679
		hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
E
Edwin Peer 已提交
680 681 682
	} else {
		txd->l3_offset = skb_inner_network_offset(skb);
		txd->l4_offset = skb_inner_transport_offset(skb);
683 684
		hdrlen = skb_inner_transport_header(skb) - skb->data +
			inner_tcp_hdrlen(skb);
E
Edwin Peer 已提交
685
	}
686 687 688 689 690

	txbuf->pkt_cnt = skb_shinfo(skb)->gso_segs;
	txbuf->real_len += hdrlen * (txbuf->pkt_cnt - 1);

	mss = skb_shinfo(skb)->gso_size & PCIE_DESC_TX_MSS_MASK;
691
	txd->lso_hdrlen = hdrlen;
692 693 694 695 696 697 698 699 700 701
	txd->mss = cpu_to_le16(mss);
	txd->flags |= PCIE_DESC_TX_LSO;

	u64_stats_update_begin(&r_vec->tx_sync);
	r_vec->tx_lso++;
	u64_stats_update_end(&r_vec->tx_sync);
}

/**
 * nfp_net_tx_csum() - Set TX CSUM offload flags in TX descriptor
702
 * @dp:  NFP Net data path struct
703 704 705 706 707 708 709 710
 * @r_vec: per-ring structure
 * @txbuf: Pointer to driver soft TX descriptor
 * @txd: Pointer to TX descriptor
 * @skb: Pointer to SKB
 *
 * This function sets the TX checksum flags in the TX descriptor based
 * on the configuration and the protocol of the packet to be transmitted.
 */
711 712
static void nfp_net_tx_csum(struct nfp_net_dp *dp,
			    struct nfp_net_r_vector *r_vec,
713 714 715 716 717 718 719
			    struct nfp_net_tx_buf *txbuf,
			    struct nfp_net_tx_desc *txd, struct sk_buff *skb)
{
	struct ipv6hdr *ipv6h;
	struct iphdr *iph;
	u8 l4_hdr;

720
	if (!(dp->ctrl & NFP_NET_CFG_CTRL_TXCSUM))
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
		return;

	if (skb->ip_summed != CHECKSUM_PARTIAL)
		return;

	txd->flags |= PCIE_DESC_TX_CSUM;
	if (skb->encapsulation)
		txd->flags |= PCIE_DESC_TX_ENCAP;

	iph = skb->encapsulation ? inner_ip_hdr(skb) : ip_hdr(skb);
	ipv6h = skb->encapsulation ? inner_ipv6_hdr(skb) : ipv6_hdr(skb);

	if (iph->version == 4) {
		txd->flags |= PCIE_DESC_TX_IP4_CSUM;
		l4_hdr = iph->protocol;
	} else if (ipv6h->version == 6) {
		l4_hdr = ipv6h->nexthdr;
	} else {
739
		nn_dp_warn(dp, "partial checksum but ipv=%x!\n", iph->version);
740 741 742 743 744 745 746 747 748 749 750
		return;
	}

	switch (l4_hdr) {
	case IPPROTO_TCP:
		txd->flags |= PCIE_DESC_TX_TCP_CSUM;
		break;
	case IPPROTO_UDP:
		txd->flags |= PCIE_DESC_TX_UDP_CSUM;
		break;
	default:
751
		nn_dp_warn(dp, "partial checksum but l4 proto=%x!\n", l4_hdr);
752 753 754 755 756 757 758 759 760 761 762
		return;
	}

	u64_stats_update_begin(&r_vec->tx_sync);
	if (skb->encapsulation)
		r_vec->hw_csum_tx_inner += txbuf->pkt_cnt;
	else
		r_vec->hw_csum_tx += txbuf->pkt_cnt;
	u64_stats_update_end(&r_vec->tx_sync);
}

763 764 765 766 767 768 769
static void nfp_net_tx_xmit_more_flush(struct nfp_net_tx_ring *tx_ring)
{
	wmb();
	nfp_qcp_wr_ptr_add(tx_ring->qcp_q, tx_ring->wr_ptr_add);
	tx_ring->wr_ptr_add = 0;
}

770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
static int nfp_net_prep_port_id(struct sk_buff *skb)
{
	struct metadata_dst *md_dst = skb_metadata_dst(skb);
	unsigned char *data;

	if (likely(!md_dst))
		return 0;
	if (unlikely(md_dst->type != METADATA_HW_PORT_MUX))
		return 0;

	if (unlikely(skb_cow_head(skb, 8)))
		return -ENOMEM;

	data = skb_push(skb, 8);
	put_unaligned_be32(NFP_NET_META_PORTID, data);
	put_unaligned_be32(md_dst->u.port_info.port_id, data + 4);

	return 8;
}

790 791 792 793 794 795 796 797 798 799 800 801
/**
 * nfp_net_tx() - Main transmit entry point
 * @skb:    SKB to transmit
 * @netdev: netdev structure
 *
 * Return: NETDEV_TX_OK on success.
 */
static int nfp_net_tx(struct sk_buff *skb, struct net_device *netdev)
{
	struct nfp_net *nn = netdev_priv(netdev);
	const struct skb_frag_struct *frag;
	struct nfp_net_tx_desc *txd, txdg;
802
	int f, nr_frags, wr_idx, md_bytes;
803
	struct nfp_net_tx_ring *tx_ring;
804 805
	struct nfp_net_r_vector *r_vec;
	struct nfp_net_tx_buf *txbuf;
806
	struct netdev_queue *nd_q;
807
	struct nfp_net_dp *dp;
808 809 810 811
	dma_addr_t dma_addr;
	unsigned int fsize;
	u16 qidx;

812
	dp = &nn->dp;
813
	qidx = skb_get_queue_mapping(skb);
814
	tx_ring = &dp->tx_rings[qidx];
815
	r_vec = tx_ring->r_vec;
816
	nd_q = netdev_get_tx_queue(dp->netdev, qidx);
817 818 819 820

	nr_frags = skb_shinfo(skb)->nr_frags;

	if (unlikely(nfp_net_tx_full(tx_ring, nr_frags + 1))) {
821 822
		nn_dp_warn(dp, "TX ring %d busy. wrp=%u rdp=%u\n",
			   qidx, tx_ring->wr_p, tx_ring->rd_p);
823
		netif_tx_stop_queue(nd_q);
824
		nfp_net_tx_xmit_more_flush(tx_ring);
825 826 827 828 829 830
		u64_stats_update_begin(&r_vec->tx_sync);
		r_vec->tx_busy++;
		u64_stats_update_end(&r_vec->tx_sync);
		return NETDEV_TX_BUSY;
	}

831 832 833 834 835 836 837
	md_bytes = nfp_net_prep_port_id(skb);
	if (unlikely(md_bytes < 0)) {
		nfp_net_tx_xmit_more_flush(tx_ring);
		dev_kfree_skb_any(skb);
		return NETDEV_TX_OK;
	}

838
	/* Start with the head skbuf */
839
	dma_addr = dma_map_single(dp->dev, skb->data, skb_headlen(skb),
840
				  DMA_TO_DEVICE);
841
	if (dma_mapping_error(dp->dev, dma_addr))
842 843
		goto err_free;

844
	wr_idx = D_IDX(tx_ring, tx_ring->wr_p);
845 846 847 848 849 850 851 852 853 854 855

	/* Stash the soft descriptor of the head then initialize it */
	txbuf = &tx_ring->txbufs[wr_idx];
	txbuf->skb = skb;
	txbuf->dma_addr = dma_addr;
	txbuf->fidx = -1;
	txbuf->pkt_cnt = 1;
	txbuf->real_len = skb->len;

	/* Build TX descriptor */
	txd = &tx_ring->txds[wr_idx];
856
	txd->offset_eop = (nr_frags ? 0 : PCIE_DESC_TX_EOP) | md_bytes;
857 858 859 860 861 862
	txd->dma_len = cpu_to_le16(skb_headlen(skb));
	nfp_desc_set_dma_addr(txd, dma_addr);
	txd->data_len = cpu_to_le16(skb->len);

	txd->flags = 0;
	txd->mss = 0;
863
	txd->lso_hdrlen = 0;
864

E
Edwin Peer 已提交
865
	/* Do not reorder - tso may adjust pkt cnt, vlan may override fields */
866 867 868
	nfp_net_tx_tso(r_vec, txbuf, txd, skb);
	nfp_net_tx_csum(dp, r_vec, txbuf, txd, skb);
	if (skb_vlan_tag_present(skb) && dp->ctrl & NFP_NET_CFG_CTRL_TXVLAN) {
869 870 871 872 873 874 875 876 877 878 879 880 881
		txd->flags |= PCIE_DESC_TX_VLAN;
		txd->vlan = cpu_to_le16(skb_vlan_tag_get(skb));
	}

	/* Gather DMA */
	if (nr_frags > 0) {
		/* all descs must match except for in addr, length and eop */
		txdg = *txd;

		for (f = 0; f < nr_frags; f++) {
			frag = &skb_shinfo(skb)->frags[f];
			fsize = skb_frag_size(frag);

882
			dma_addr = skb_frag_dma_map(dp->dev, frag, 0,
883
						    fsize, DMA_TO_DEVICE);
884
			if (dma_mapping_error(dp->dev, dma_addr))
885 886
				goto err_unmap;

887
			wr_idx = D_IDX(tx_ring, wr_idx + 1);
888 889 890 891 892 893 894 895
			tx_ring->txbufs[wr_idx].skb = skb;
			tx_ring->txbufs[wr_idx].dma_addr = dma_addr;
			tx_ring->txbufs[wr_idx].fidx = f;

			txd = &tx_ring->txds[wr_idx];
			*txd = txdg;
			txd->dma_len = cpu_to_le16(fsize);
			nfp_desc_set_dma_addr(txd, dma_addr);
896
			txd->offset_eop |=
897 898 899 900 901 902 903 904 905 906
				(f == nr_frags - 1) ? PCIE_DESC_TX_EOP : 0;
		}

		u64_stats_update_begin(&r_vec->tx_sync);
		r_vec->tx_gather++;
		u64_stats_update_end(&r_vec->tx_sync);
	}

	netdev_tx_sent_queue(nd_q, txbuf->real_len);

907 908
	skb_tx_timestamp(skb);

909 910 911 912 913
	tx_ring->wr_p += nr_frags + 1;
	if (nfp_net_tx_ring_should_stop(tx_ring))
		nfp_net_tx_ring_stop(nd_q, tx_ring);

	tx_ring->wr_ptr_add += nr_frags + 1;
914 915
	if (!skb->xmit_more || netif_xmit_stopped(nd_q))
		nfp_net_tx_xmit_more_flush(tx_ring);
916 917 918 919

	return NETDEV_TX_OK;

err_unmap:
920
	while (--f >= 0) {
921
		frag = &skb_shinfo(skb)->frags[f];
922
		dma_unmap_page(dp->dev, tx_ring->txbufs[wr_idx].dma_addr,
923 924 925 926 927 928 929 930
			       skb_frag_size(frag), DMA_TO_DEVICE);
		tx_ring->txbufs[wr_idx].skb = NULL;
		tx_ring->txbufs[wr_idx].dma_addr = 0;
		tx_ring->txbufs[wr_idx].fidx = -2;
		wr_idx = wr_idx - 1;
		if (wr_idx < 0)
			wr_idx += tx_ring->cnt;
	}
931
	dma_unmap_single(dp->dev, tx_ring->txbufs[wr_idx].dma_addr,
932 933 934 935 936
			 skb_headlen(skb), DMA_TO_DEVICE);
	tx_ring->txbufs[wr_idx].skb = NULL;
	tx_ring->txbufs[wr_idx].dma_addr = 0;
	tx_ring->txbufs[wr_idx].fidx = -2;
err_free:
937
	nn_dp_warn(dp, "Failed to map DMA TX buffer\n");
938
	nfp_net_tx_xmit_more_flush(tx_ring);
939 940 941 942 943 944 945 946 947
	u64_stats_update_begin(&r_vec->tx_sync);
	r_vec->tx_errors++;
	u64_stats_update_end(&r_vec->tx_sync);
	dev_kfree_skb_any(skb);
	return NETDEV_TX_OK;
}

/**
 * nfp_net_tx_complete() - Handled completed TX packets
948 949
 * @tx_ring:	TX ring structure
 * @budget:	NAPI budget (only used as bool to determine if in NAPI context)
950 951 952
 *
 * Return: Number of completed TX descriptors
 */
953
static void nfp_net_tx_complete(struct nfp_net_tx_ring *tx_ring, int budget)
954 955
{
	struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
956
	struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
957 958 959 960 961 962 963 964 965
	const struct skb_frag_struct *frag;
	struct netdev_queue *nd_q;
	u32 done_pkts = 0, done_bytes = 0;
	struct sk_buff *skb;
	int todo, nr_frags;
	u32 qcp_rd_p;
	int fidx;
	int idx;

966 967 968
	if (tx_ring->wr_p == tx_ring->rd_p)
		return;

969 970 971 972 973 974
	/* Work out how many descriptors have been transmitted */
	qcp_rd_p = nfp_qcp_rd_ptr_read(tx_ring->qcp_q);

	if (qcp_rd_p == tx_ring->qcp_rd_p)
		return;

975
	todo = D_IDX(tx_ring, qcp_rd_p - tx_ring->qcp_rd_p);
976 977

	while (todo--) {
978
		idx = D_IDX(tx_ring, tx_ring->rd_p++);
979 980 981 982 983 984 985 986 987 988

		skb = tx_ring->txbufs[idx].skb;
		if (!skb)
			continue;

		nr_frags = skb_shinfo(skb)->nr_frags;
		fidx = tx_ring->txbufs[idx].fidx;

		if (fidx == -1) {
			/* unmap head */
989
			dma_unmap_single(dp->dev, tx_ring->txbufs[idx].dma_addr,
990 991 992 993 994 995 996
					 skb_headlen(skb), DMA_TO_DEVICE);

			done_pkts += tx_ring->txbufs[idx].pkt_cnt;
			done_bytes += tx_ring->txbufs[idx].real_len;
		} else {
			/* unmap fragment */
			frag = &skb_shinfo(skb)->frags[fidx];
997
			dma_unmap_page(dp->dev, tx_ring->txbufs[idx].dma_addr,
998 999 1000 1001 1002
				       skb_frag_size(frag), DMA_TO_DEVICE);
		}

		/* check for last gather fragment */
		if (fidx == nr_frags - 1)
1003
			napi_consume_skb(skb, budget);
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016

		tx_ring->txbufs[idx].dma_addr = 0;
		tx_ring->txbufs[idx].skb = NULL;
		tx_ring->txbufs[idx].fidx = -2;
	}

	tx_ring->qcp_rd_p = qcp_rd_p;

	u64_stats_update_begin(&r_vec->tx_sync);
	r_vec->tx_bytes += done_bytes;
	r_vec->tx_pkts += done_pkts;
	u64_stats_update_end(&r_vec->tx_sync);

J
Jakub Kicinski 已提交
1017 1018 1019
	if (!dp->netdev)
		return;

1020
	nd_q = netdev_get_tx_queue(dp->netdev, tx_ring->idx);
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
	netdev_tx_completed_queue(nd_q, done_pkts, done_bytes);
	if (nfp_net_tx_ring_should_wake(tx_ring)) {
		/* Make sure TX thread will see updated tx_ring->rd_p */
		smp_mb();

		if (unlikely(netif_tx_queue_stopped(nd_q)))
			netif_tx_wake_queue(nd_q);
	}

	WARN_ONCE(tx_ring->wr_p - tx_ring->rd_p > tx_ring->cnt,
		  "TX ring corruption rd_p=%u wr_p=%u cnt=%u\n",
		  tx_ring->rd_p, tx_ring->wr_p, tx_ring->cnt);
}

1035
static bool nfp_net_xdp_complete(struct nfp_net_tx_ring *tx_ring)
1036 1037 1038
{
	struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
	u32 done_pkts = 0, done_bytes = 0;
1039
	bool done_all;
1040 1041 1042 1043 1044 1045 1046
	int idx, todo;
	u32 qcp_rd_p;

	/* Work out how many descriptors have been transmitted */
	qcp_rd_p = nfp_qcp_rd_ptr_read(tx_ring->qcp_q);

	if (qcp_rd_p == tx_ring->qcp_rd_p)
1047
		return true;
1048

1049
	todo = D_IDX(tx_ring, qcp_rd_p - tx_ring->qcp_rd_p);
1050

1051 1052 1053
	done_all = todo <= NFP_NET_XDP_MAX_COMPLETE;
	todo = min(todo, NFP_NET_XDP_MAX_COMPLETE);

1054
	tx_ring->qcp_rd_p = D_IDX(tx_ring, tx_ring->qcp_rd_p + todo);
1055

1056
	done_pkts = todo;
1057
	while (todo--) {
1058
		idx = D_IDX(tx_ring, tx_ring->rd_p);
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
		tx_ring->rd_p++;

		done_bytes += tx_ring->txbufs[idx].real_len;
	}

	u64_stats_update_begin(&r_vec->tx_sync);
	r_vec->tx_bytes += done_bytes;
	r_vec->tx_pkts += done_pkts;
	u64_stats_update_end(&r_vec->tx_sync);

	WARN_ONCE(tx_ring->wr_p - tx_ring->rd_p > tx_ring->cnt,
1070
		  "XDP TX ring corruption rd_p=%u wr_p=%u cnt=%u\n",
1071
		  tx_ring->rd_p, tx_ring->wr_p, tx_ring->cnt);
1072 1073

	return done_all;
1074 1075
}

1076
/**
1077
 * nfp_net_tx_ring_reset() - Free any untransmitted buffers and reset pointers
1078
 * @dp:		NFP Net data path struct
1079
 * @tx_ring:	TX ring structure
1080
 *
1081
 * Assumes that the device is stopped, must be idempotent.
1082
 */
1083
static void
1084
nfp_net_tx_ring_reset(struct nfp_net_dp *dp, struct nfp_net_tx_ring *tx_ring)
1085 1086
{
	const struct skb_frag_struct *frag;
1087
	struct netdev_queue *nd_q;
1088

1089
	while (!tx_ring->is_xdp && tx_ring->rd_p != tx_ring->wr_p) {
1090
		struct nfp_net_tx_buf *tx_buf;
1091 1092
		struct sk_buff *skb;
		int idx, nr_frags;
1093

1094
		idx = D_IDX(tx_ring, tx_ring->rd_p);
1095
		tx_buf = &tx_ring->txbufs[idx];
1096

1097 1098
		skb = tx_ring->txbufs[idx].skb;
		nr_frags = skb_shinfo(skb)->nr_frags;
1099

1100 1101 1102 1103 1104 1105 1106 1107 1108
		if (tx_buf->fidx == -1) {
			/* unmap head */
			dma_unmap_single(dp->dev, tx_buf->dma_addr,
					 skb_headlen(skb), DMA_TO_DEVICE);
		} else {
			/* unmap fragment */
			frag = &skb_shinfo(skb)->frags[tx_buf->fidx];
			dma_unmap_page(dp->dev, tx_buf->dma_addr,
				       skb_frag_size(frag), DMA_TO_DEVICE);
1109
		}
1110

1111 1112 1113 1114
		/* check for last gather fragment */
		if (tx_buf->fidx == nr_frags - 1)
			dev_kfree_skb_any(skb);

1115 1116 1117
		tx_buf->dma_addr = 0;
		tx_buf->skb = NULL;
		tx_buf->fidx = -2;
1118 1119 1120 1121 1122

		tx_ring->qcp_rd_p++;
		tx_ring->rd_p++;
	}

1123 1124 1125 1126 1127 1128
	memset(tx_ring->txds, 0, sizeof(*tx_ring->txds) * tx_ring->cnt);
	tx_ring->wr_p = 0;
	tx_ring->rd_p = 0;
	tx_ring->qcp_rd_p = 0;
	tx_ring->wr_ptr_add = 0;

J
Jakub Kicinski 已提交
1129
	if (tx_ring->is_xdp || !dp->netdev)
1130 1131
		return;

1132
	nd_q = netdev_get_tx_queue(dp->netdev, tx_ring->idx);
1133 1134 1135 1136 1137 1138 1139 1140
	netdev_tx_reset_queue(nd_q);
}

static void nfp_net_tx_timeout(struct net_device *netdev)
{
	struct nfp_net *nn = netdev_priv(netdev);
	int i;

1141
	for (i = 0; i < nn->dp.netdev->real_num_tx_queues; i++) {
1142 1143 1144 1145 1146 1147 1148 1149 1150
		if (!netif_tx_queue_stopped(netdev_get_tx_queue(netdev, i)))
			continue;
		nn_warn(nn, "TX timeout on ring: %d\n", i);
	}
	nn_warn(nn, "TX watchdog timeout\n");
}

/* Receive processing
 */
1151
static unsigned int
1152
nfp_net_calc_fl_bufsz(struct nfp_net_dp *dp)
1153 1154 1155
{
	unsigned int fl_bufsz;

1156
	fl_bufsz = NFP_NET_RX_BUF_HEADROOM;
1157
	fl_bufsz += dp->rx_dma_off;
1158
	if (dp->rx_offset == NFP_NET_CFG_RX_OFFSET_DYNAMIC)
1159
		fl_bufsz += NFP_NET_MAX_PREPEND;
1160
	else
1161
		fl_bufsz += dp->rx_offset;
1162
	fl_bufsz += ETH_HLEN + VLAN_HLEN * 2 + dp->mtu;
1163

1164 1165 1166
	fl_bufsz = SKB_DATA_ALIGN(fl_bufsz);
	fl_bufsz += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));

1167 1168
	return fl_bufsz;
}
1169

1170 1171 1172 1173 1174 1175 1176 1177 1178
static void
nfp_net_free_frag(void *frag, bool xdp)
{
	if (!xdp)
		skb_free_frag(frag);
	else
		__free_page(virt_to_page(frag));
}

1179
/**
1180
 * nfp_net_rx_alloc_one() - Allocate and map page frag for RX
1181
 * @dp:		NFP Net data path struct
1182 1183
 * @dma_addr:	Pointer to storage for DMA address (output param)
 *
1184
 * This function will allcate a new page frag, map it for DMA.
1185
 *
1186
 * Return: allocated page frag or NULL on failure.
1187
 */
1188
static void *nfp_net_rx_alloc_one(struct nfp_net_dp *dp, dma_addr_t *dma_addr)
1189
{
1190
	void *frag;
1191

1192
	if (!dp->xdp_prog) {
1193
		frag = netdev_alloc_frag(dp->fl_bufsz);
1194 1195 1196
	} else {
		struct page *page;

M
Mel Gorman 已提交
1197
		page = alloc_page(GFP_KERNEL);
1198 1199
		frag = page ? page_address(page) : NULL;
	}
1200
	if (!frag) {
1201
		nn_dp_warn(dp, "Failed to alloc receive page frag\n");
1202 1203 1204
		return NULL;
	}

1205
	*dma_addr = nfp_net_dma_map_rx(dp, frag);
1206
	if (dma_mapping_error(dp->dev, *dma_addr)) {
1207
		nfp_net_free_frag(frag, dp->xdp_prog);
1208
		nn_dp_warn(dp, "Failed to map DMA RX buffer\n");
1209 1210 1211
		return NULL;
	}

1212
	return frag;
1213 1214
}

1215
static void *nfp_net_napi_alloc_one(struct nfp_net_dp *dp, dma_addr_t *dma_addr)
1216 1217 1218
{
	void *frag;

1219
	if (!dp->xdp_prog) {
1220
		frag = napi_alloc_frag(dp->fl_bufsz);
1221 1222
		if (unlikely(!frag))
			return NULL;
1223 1224 1225
	} else {
		struct page *page;

J
Jakub Kicinski 已提交
1226
		page = dev_alloc_page();
1227 1228 1229
		if (unlikely(!page))
			return NULL;
		frag = page_address(page);
1230 1231
	}

1232
	*dma_addr = nfp_net_dma_map_rx(dp, frag);
1233 1234 1235
	if (dma_mapping_error(dp->dev, *dma_addr)) {
		nfp_net_free_frag(frag, dp->xdp_prog);
		nn_dp_warn(dp, "Failed to map DMA RX buffer\n");
1236 1237 1238 1239 1240 1241
		return NULL;
	}

	return frag;
}

1242 1243
/**
 * nfp_net_rx_give_one() - Put mapped skb on the software and hardware rings
1244
 * @dp:		NFP Net data path struct
1245
 * @rx_ring:	RX ring structure
1246
 * @frag:	page fragment buffer
1247 1248
 * @dma_addr:	DMA address of skb mapping
 */
1249 1250
static void nfp_net_rx_give_one(const struct nfp_net_dp *dp,
				struct nfp_net_rx_ring *rx_ring,
1251
				void *frag, dma_addr_t dma_addr)
1252 1253 1254
{
	unsigned int wr_idx;

1255
	wr_idx = D_IDX(rx_ring, rx_ring->wr_p);
1256

1257 1258
	nfp_net_dma_sync_dev_rx(dp, dma_addr);

1259
	/* Stash SKB and DMA address away */
1260
	rx_ring->rxbufs[wr_idx].frag = frag;
1261 1262 1263 1264 1265
	rx_ring->rxbufs[wr_idx].dma_addr = dma_addr;

	/* Fill freelist descriptor */
	rx_ring->rxds[wr_idx].fld.reserved = 0;
	rx_ring->rxds[wr_idx].fld.meta_len_dd = 0;
1266 1267
	nfp_desc_set_dma_addr(&rx_ring->rxds[wr_idx].fld,
			      dma_addr + dp->rx_dma_off);
1268 1269

	rx_ring->wr_p++;
1270
	if (!(rx_ring->wr_p % NFP_NET_FL_BATCH)) {
1271 1272 1273 1274
		/* Update write pointer of the freelist queue. Make
		 * sure all writes are flushed before telling the hardware.
		 */
		wmb();
1275
		nfp_qcp_wr_ptr_add(rx_ring->qcp_fl, NFP_NET_FL_BATCH);
1276 1277 1278 1279
	}
}

/**
1280 1281
 * nfp_net_rx_ring_reset() - Reflect in SW state of freelist after disable
 * @rx_ring:	RX ring structure
1282
 *
1283
 * Assumes that the device is stopped, must be idempotent.
1284
 */
1285
static void nfp_net_rx_ring_reset(struct nfp_net_rx_ring *rx_ring)
1286
{
1287
	unsigned int wr_idx, last_idx;
1288

1289 1290 1291 1292 1293 1294
	/* wr_p == rd_p means ring was never fed FL bufs.  RX rings are always
	 * kept at cnt - 1 FL bufs.
	 */
	if (rx_ring->wr_p == 0 && rx_ring->rd_p == 0)
		return;

1295
	/* Move the empty entry to the end of the list */
1296
	wr_idx = D_IDX(rx_ring, rx_ring->wr_p);
1297 1298
	last_idx = rx_ring->cnt - 1;
	rx_ring->rxbufs[wr_idx].dma_addr = rx_ring->rxbufs[last_idx].dma_addr;
1299
	rx_ring->rxbufs[wr_idx].frag = rx_ring->rxbufs[last_idx].frag;
1300
	rx_ring->rxbufs[last_idx].dma_addr = 0;
1301
	rx_ring->rxbufs[last_idx].frag = NULL;
1302

1303 1304 1305 1306
	memset(rx_ring->rxds, 0, sizeof(*rx_ring->rxds) * rx_ring->cnt);
	rx_ring->wr_p = 0;
	rx_ring->rd_p = 0;
}
1307

1308 1309
/**
 * nfp_net_rx_ring_bufs_free() - Free any buffers currently on the RX ring
1310
 * @dp:		NFP Net data path struct
1311 1312 1313 1314 1315 1316 1317
 * @rx_ring:	RX ring to remove buffers from
 *
 * Assumes that the device is stopped and buffers are in [0, ring->cnt - 1)
 * entries.  After device is disabled nfp_net_rx_ring_reset() must be called
 * to restore required ring geometry.
 */
static void
1318
nfp_net_rx_ring_bufs_free(struct nfp_net_dp *dp,
1319
			  struct nfp_net_rx_ring *rx_ring)
1320 1321
{
	unsigned int i;
1322

1323 1324 1325 1326 1327
	for (i = 0; i < rx_ring->cnt - 1; i++) {
		/* NULL skb can only happen when initial filling of the ring
		 * fails to allocate enough buffers and calls here to free
		 * already allocated ones.
		 */
1328
		if (!rx_ring->rxbufs[i].frag)
1329 1330
			continue;

1331
		nfp_net_dma_unmap_rx(dp, rx_ring->rxbufs[i].dma_addr);
1332
		nfp_net_free_frag(rx_ring->rxbufs[i].frag, dp->xdp_prog);
1333
		rx_ring->rxbufs[i].dma_addr = 0;
1334
		rx_ring->rxbufs[i].frag = NULL;
1335 1336 1337 1338
	}
}

/**
1339
 * nfp_net_rx_ring_bufs_alloc() - Fill RX ring with buffers (don't give to FW)
1340
 * @dp:		NFP Net data path struct
1341
 * @rx_ring:	RX ring to remove buffers from
1342
 */
1343
static int
1344
nfp_net_rx_ring_bufs_alloc(struct nfp_net_dp *dp,
1345
			   struct nfp_net_rx_ring *rx_ring)
1346
{
1347 1348 1349 1350
	struct nfp_net_rx_buf *rxbufs;
	unsigned int i;

	rxbufs = rx_ring->rxbufs;
1351

1352
	for (i = 0; i < rx_ring->cnt - 1; i++) {
1353
		rxbufs[i].frag = nfp_net_rx_alloc_one(dp, &rxbufs[i].dma_addr);
1354
		if (!rxbufs[i].frag) {
1355
			nfp_net_rx_ring_bufs_free(dp, rx_ring);
1356 1357 1358 1359 1360 1361 1362
			return -ENOMEM;
		}
	}

	return 0;
}

1363 1364
/**
 * nfp_net_rx_ring_fill_freelist() - Give buffers from the ring to FW
1365
 * @dp:	     NFP Net data path struct
1366 1367
 * @rx_ring: RX ring to fill
 */
1368 1369 1370
static void
nfp_net_rx_ring_fill_freelist(struct nfp_net_dp *dp,
			      struct nfp_net_rx_ring *rx_ring)
1371 1372 1373 1374
{
	unsigned int i;

	for (i = 0; i < rx_ring->cnt - 1; i++)
1375
		nfp_net_rx_give_one(dp, rx_ring, rx_ring->rxbufs[i].frag,
1376 1377 1378
				    rx_ring->rxbufs[i].dma_addr);
}

1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
/**
 * nfp_net_rx_csum_has_errors() - group check if rxd has any csum errors
 * @flags: RX descriptor flags field in CPU byte order
 */
static int nfp_net_rx_csum_has_errors(u16 flags)
{
	u16 csum_all_checked, csum_all_ok;

	csum_all_checked = flags & __PCIE_DESC_RX_CSUM_ALL;
	csum_all_ok = flags & __PCIE_DESC_RX_CSUM_ALL_OK;

	return csum_all_checked != (csum_all_ok << PCIE_DESC_RX_CSUM_OK_SHIFT);
}

/**
 * nfp_net_rx_csum() - set SKB checksum field based on RX descriptor flags
1395
 * @dp:  NFP Net data path struct
1396 1397
 * @r_vec: per-ring structure
 * @rxd: Pointer to RX descriptor
1398
 * @meta: Parsed metadata prepend
1399 1400
 * @skb: Pointer to SKB
 */
1401 1402
static void nfp_net_rx_csum(struct nfp_net_dp *dp,
			    struct nfp_net_r_vector *r_vec,
1403 1404
			    struct nfp_net_rx_desc *rxd,
			    struct nfp_meta_parsed *meta, struct sk_buff *skb)
1405 1406 1407
{
	skb_checksum_none_assert(skb);

1408
	if (!(dp->netdev->features & NETIF_F_RXCSUM))
1409 1410
		return;

1411 1412 1413 1414
	if (meta->csum_type) {
		skb->ip_summed = meta->csum_type;
		skb->csum = meta->csum;
		u64_stats_update_begin(&r_vec->rx_sync);
1415
		r_vec->hw_csum_rx_complete++;
1416 1417 1418 1419
		u64_stats_update_end(&r_vec->rx_sync);
		return;
	}

1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
	if (nfp_net_rx_csum_has_errors(le16_to_cpu(rxd->rxd.flags))) {
		u64_stats_update_begin(&r_vec->rx_sync);
		r_vec->hw_csum_rx_error++;
		u64_stats_update_end(&r_vec->rx_sync);
		return;
	}

	/* Assume that the firmware will never report inner CSUM_OK unless outer
	 * L4 headers were successfully parsed. FW will always report zero UDP
	 * checksum as CSUM_OK.
	 */
	if (rxd->rxd.flags & PCIE_DESC_RX_TCP_CSUM_OK ||
	    rxd->rxd.flags & PCIE_DESC_RX_UDP_CSUM_OK) {
		__skb_incr_checksum_unnecessary(skb);
		u64_stats_update_begin(&r_vec->rx_sync);
		r_vec->hw_csum_rx_ok++;
		u64_stats_update_end(&r_vec->rx_sync);
	}

	if (rxd->rxd.flags & PCIE_DESC_RX_I_TCP_CSUM_OK ||
	    rxd->rxd.flags & PCIE_DESC_RX_I_UDP_CSUM_OK) {
		__skb_incr_checksum_unnecessary(skb);
		u64_stats_update_begin(&r_vec->rx_sync);
		r_vec->hw_csum_rx_inner_ok++;
		u64_stats_update_end(&r_vec->rx_sync);
	}
}

1448 1449 1450
static void
nfp_net_set_hash(struct net_device *netdev, struct nfp_meta_parsed *meta,
		 unsigned int type, __be32 *hash)
1451
{
1452
	if (!(netdev->features & NETIF_F_RXHASH))
1453 1454
		return;

1455
	switch (type) {
1456 1457 1458
	case NFP_NET_RSS_IPV4:
	case NFP_NET_RSS_IPV6:
	case NFP_NET_RSS_IPV6_EX:
1459
		meta->hash_type = PKT_HASH_TYPE_L3;
1460 1461
		break;
	default:
1462
		meta->hash_type = PKT_HASH_TYPE_L4;
1463 1464
		break;
	}
1465 1466

	meta->hash = get_unaligned_be32(hash);
1467 1468
}

1469
static void
1470
nfp_net_set_hash_desc(struct net_device *netdev, struct nfp_meta_parsed *meta,
1471
		      void *data, struct nfp_net_rx_desc *rxd)
1472
{
1473
	struct nfp_net_rx_hash *rx_hash = data;
1474 1475 1476 1477

	if (!(rxd->rxd.flags & PCIE_DESC_RX_RSS))
		return;

1478
	nfp_net_set_hash(netdev, meta, get_unaligned_be32(&rx_hash->hash_type),
1479 1480 1481 1482
			 &rx_hash->hash);
}

static void *
1483
nfp_net_parse_meta(struct net_device *netdev, struct nfp_meta_parsed *meta,
1484
		   void *data, int meta_len)
1485 1486 1487 1488 1489 1490 1491 1492 1493 1494
{
	u32 meta_info;

	meta_info = get_unaligned_be32(data);
	data += 4;

	while (meta_info) {
		switch (meta_info & NFP_NET_META_FIELD_MASK) {
		case NFP_NET_META_HASH:
			meta_info >>= NFP_NET_META_FIELD_SIZE;
1495
			nfp_net_set_hash(netdev, meta,
1496 1497 1498 1499 1500
					 meta_info & NFP_NET_META_FIELD_MASK,
					 (__be32 *)data);
			data += 4;
			break;
		case NFP_NET_META_MARK:
1501
			meta->mark = get_unaligned_be32(data);
1502 1503
			data += 4;
			break;
1504 1505 1506 1507
		case NFP_NET_META_PORTID:
			meta->portid = get_unaligned_be32(data);
			data += 4;
			break;
1508 1509 1510 1511 1512 1513
		case NFP_NET_META_CSUM:
			meta->csum_type = CHECKSUM_COMPLETE;
			meta->csum =
				(__force __wsum)__get_unaligned_cpu32(data);
			data += 4;
			break;
1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
		default:
			return NULL;
		}

		meta_info >>= NFP_NET_META_FIELD_SIZE;
	}

	return data;
}

1524
static void
1525 1526 1527
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)
1528 1529 1530
{
	u64_stats_update_begin(&r_vec->rx_sync);
	r_vec->rx_drops++;
1531 1532 1533 1534 1535
	/* If we have both skb and rxbuf the replacement buffer allocation
	 * must have failed, count this as an alloc failure.
	 */
	if (skb && rxbuf)
		r_vec->rx_replace_buf_alloc_fail++;
1536 1537
	u64_stats_update_end(&r_vec->rx_sync);

1538 1539 1540 1541 1542
	/* skb is build based on the frag, free_skb() would free the frag
	 * so to be able to reuse it we need an extra ref.
	 */
	if (skb && rxbuf && skb->head == rxbuf->frag)
		page_ref_inc(virt_to_head_page(rxbuf->frag));
1543
	if (rxbuf)
1544
		nfp_net_rx_give_one(dp, rx_ring, rxbuf->frag, rxbuf->dma_addr);
1545 1546 1547 1548
	if (skb)
		dev_kfree_skb_any(skb);
}

1549
static bool
1550
nfp_net_tx_xdp_buf(struct nfp_net_dp *dp, struct nfp_net_rx_ring *rx_ring,
1551
		   struct nfp_net_tx_ring *tx_ring,
1552
		   struct nfp_net_rx_buf *rxbuf, unsigned int dma_off,
1553
		   unsigned int pkt_len, bool *completed)
1554 1555 1556 1557 1558 1559
{
	struct nfp_net_tx_buf *txbuf;
	struct nfp_net_tx_desc *txd;
	int wr_idx;

	if (unlikely(nfp_net_tx_full(tx_ring, 1))) {
1560 1561 1562 1563 1564 1565 1566 1567 1568 1569
		if (!*completed) {
			nfp_net_xdp_complete(tx_ring);
			*completed = true;
		}

		if (unlikely(nfp_net_tx_full(tx_ring, 1))) {
			nfp_net_rx_drop(dp, rx_ring->r_vec, rx_ring, rxbuf,
					NULL);
			return false;
		}
1570 1571
	}

1572
	wr_idx = D_IDX(tx_ring, tx_ring->wr_p);
1573 1574 1575

	/* Stash the soft descriptor of the head then initialize it */
	txbuf = &tx_ring->txbufs[wr_idx];
1576 1577 1578

	nfp_net_rx_give_one(dp, rx_ring, txbuf->frag, txbuf->dma_addr);

1579 1580 1581 1582 1583 1584
	txbuf->frag = rxbuf->frag;
	txbuf->dma_addr = rxbuf->dma_addr;
	txbuf->fidx = -1;
	txbuf->pkt_cnt = 1;
	txbuf->real_len = pkt_len;

1585
	dma_sync_single_for_device(dp->dev, rxbuf->dma_addr + dma_off,
1586
				   pkt_len, DMA_BIDIRECTIONAL);
1587 1588 1589 1590 1591

	/* Build TX descriptor */
	txd = &tx_ring->txds[wr_idx];
	txd->offset_eop = PCIE_DESC_TX_EOP;
	txd->dma_len = cpu_to_le16(pkt_len);
1592
	nfp_desc_set_dma_addr(txd, rxbuf->dma_addr + dma_off);
1593 1594 1595 1596
	txd->data_len = cpu_to_le16(pkt_len);

	txd->flags = 0;
	txd->mss = 0;
1597
	txd->lso_hdrlen = 0;
1598 1599 1600

	tx_ring->wr_p++;
	tx_ring->wr_ptr_add++;
1601
	return true;
1602 1603
}

1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
/**
 * nfp_net_rx() - receive up to @budget packets on @rx_ring
 * @rx_ring:   RX ring to receive from
 * @budget:    NAPI budget
 *
 * Note, this function is separated out from the napi poll function to
 * more cleanly separate packet receive code from other bookkeeping
 * functions performed in the napi poll function.
 *
 * Return: Number of packets received.
 */
static int nfp_net_rx(struct nfp_net_rx_ring *rx_ring, int budget)
{
	struct nfp_net_r_vector *r_vec = rx_ring->r_vec;
1618
	struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
1619 1620
	struct nfp_net_tx_ring *tx_ring;
	struct bpf_prog *xdp_prog;
1621
	bool xdp_tx_cmpl = false;
1622
	unsigned int true_bufsz;
1623
	struct sk_buff *skb;
J
Jakub Kicinski 已提交
1624
	int pkts_polled = 0;
J
Jesper Dangaard Brouer 已提交
1625
	struct xdp_buff xdp;
1626 1627
	int idx;

1628
	rcu_read_lock();
1629 1630
	xdp_prog = READ_ONCE(dp->xdp_prog);
	true_bufsz = xdp_prog ? PAGE_SIZE : dp->fl_bufsz;
J
Jesper Dangaard Brouer 已提交
1631
	xdp.rxq = &rx_ring->xdp_rxq;
1632 1633
	tx_ring = r_vec->xdp_ring;

J
Jakub Kicinski 已提交
1634
	while (pkts_polled < budget) {
1635
		unsigned int meta_len, data_len, meta_off, pkt_len, pkt_off;
1636 1637
		struct nfp_net_rx_buf *rxbuf;
		struct nfp_net_rx_desc *rxd;
1638
		struct nfp_meta_parsed meta;
1639
		struct net_device *netdev;
1640
		dma_addr_t new_dma_addr;
1641
		u32 meta_len_xdp = 0;
1642 1643
		void *new_frag;

1644
		idx = D_IDX(rx_ring, rx_ring->rd_p);
1645 1646

		rxd = &rx_ring->rxds[idx];
J
Jakub Kicinski 已提交
1647
		if (!(rxd->rxd.meta_len_dd & PCIE_DESC_RX_DD))
1648
			break;
J
Jakub Kicinski 已提交
1649

1650 1651 1652 1653 1654
		/* Memory barrier to ensure that we won't do other reads
		 * before the DD bit.
		 */
		dma_rmb();

1655 1656
		memset(&meta, 0, sizeof(meta));

1657 1658 1659
		rx_ring->rd_p++;
		pkts_polled++;

1660
		rxbuf =	&rx_ring->rxbufs[idx];
1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672
		/*         < meta_len >
		 *  <-- [rx_offset] -->
		 *  ---------------------------------------------------------
		 * | [XX] |  metadata  |             packet           | XXXX |
		 *  ---------------------------------------------------------
		 *         <---------------- data_len --------------->
		 *
		 * The rx_offset is fixed for all packets, the meta_len can vary
		 * on a packet by packet basis. If rx_offset is set to zero
		 * (_RX_OFFSET_DYNAMIC) metadata starts at the beginning of the
		 * buffer and is immediately followed by the packet (no [XX]).
		 */
1673 1674
		meta_len = rxd->rxd.meta_len_dd & PCIE_DESC_RX_META_LEN_MASK;
		data_len = le16_to_cpu(rxd->rxd.data_len);
1675
		pkt_len = data_len - meta_len;
1676

1677
		pkt_off = NFP_NET_RX_BUF_HEADROOM + dp->rx_dma_off;
1678
		if (dp->rx_offset == NFP_NET_CFG_RX_OFFSET_DYNAMIC)
1679
			pkt_off += meta_len;
1680
		else
1681 1682
			pkt_off += dp->rx_offset;
		meta_off = pkt_off - meta_len;
1683 1684 1685 1686

		/* Stats update */
		u64_stats_update_begin(&r_vec->rx_sync);
		r_vec->rx_pkts++;
1687
		r_vec->rx_bytes += pkt_len;
1688 1689
		u64_stats_update_end(&r_vec->rx_sync);

1690 1691 1692 1693
		if (unlikely(meta_len > NFP_NET_MAX_PREPEND ||
			     (dp->rx_offset && meta_len > dp->rx_offset))) {
			nn_dp_warn(dp, "oversized RX packet metadata %u\n",
				   meta_len);
1694
			nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
1695 1696 1697
			continue;
		}

1698 1699 1700
		nfp_net_dma_sync_cpu_rx(dp, rxbuf->dma_addr + meta_off,
					data_len);

1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717
		if (!dp->chained_metadata_format) {
			nfp_net_set_hash_desc(dp->netdev, &meta,
					      rxbuf->frag + meta_off, rxd);
		} else if (meta_len) {
			void *end;

			end = nfp_net_parse_meta(dp->netdev, &meta,
						 rxbuf->frag + meta_off,
						 meta_len);
			if (unlikely(end != rxbuf->frag + pkt_off)) {
				nn_dp_warn(dp, "invalid RX packet metadata\n");
				nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf,
						NULL);
				continue;
			}
		}

1718
		if (xdp_prog && !meta.portid) {
1719
			void *orig_data = rxbuf->frag + pkt_off;
1720
			unsigned int dma_off;
1721 1722
			int act;

1723 1724 1725 1726 1727 1728 1729
			xdp.data_hard_start = rxbuf->frag + NFP_NET_RX_BUF_HEADROOM;
			xdp.data = orig_data;
			xdp.data_meta = orig_data;
			xdp.data_end = orig_data + pkt_len;

			act = bpf_prog_run_xdp(xdp_prog, &xdp);

1730
			pkt_len = xdp.data_end - xdp.data;
1731
			pkt_off += xdp.data - orig_data;
1732

1733 1734
			switch (act) {
			case XDP_PASS:
1735
				meta_len_xdp = xdp.data - xdp.data_meta;
1736 1737
				break;
			case XDP_TX:
1738
				dma_off = pkt_off - NFP_NET_RX_BUF_HEADROOM;
1739
				if (unlikely(!nfp_net_tx_xdp_buf(dp, rx_ring,
1740
								 tx_ring, rxbuf,
1741
								 dma_off,
1742 1743
								 pkt_len,
								 &xdp_tx_cmpl)))
1744 1745
					trace_xdp_exception(dp->netdev,
							    xdp_prog, act);
1746 1747 1748
				continue;
			default:
				bpf_warn_invalid_xdp_action(act);
1749
				/* fall through */
1750
			case XDP_ABORTED:
1751
				trace_xdp_exception(dp->netdev, xdp_prog, act);
1752
				/* fall through */
1753
			case XDP_DROP:
1754
				nfp_net_rx_give_one(dp, rx_ring, rxbuf->frag,
1755 1756 1757 1758 1759 1760
						    rxbuf->dma_addr);
				continue;
			}
		}

		skb = build_skb(rxbuf->frag, true_bufsz);
1761
		if (unlikely(!skb)) {
1762
			nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
1763 1764
			continue;
		}
1765
		new_frag = nfp_net_napi_alloc_one(dp, &new_dma_addr);
1766
		if (unlikely(!new_frag)) {
1767
			nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, skb);
1768 1769 1770
			continue;
		}

1771 1772 1773 1774
		nfp_net_dma_unmap_rx(dp, rxbuf->dma_addr);

		nfp_net_rx_give_one(dp, rx_ring, new_frag, new_dma_addr);

1775 1776 1777 1778 1779 1780 1781 1782
		if (likely(!meta.portid)) {
			netdev = dp->netdev;
		} else {
			struct nfp_net *nn;

			nn = netdev_priv(dp->netdev);
			netdev = nfp_app_repr_get(nn->app, meta.portid);
			if (unlikely(!netdev)) {
1783
				nfp_net_rx_drop(dp, r_vec, rx_ring, NULL, skb);
1784 1785 1786 1787 1788
				continue;
			}
			nfp_repr_inc_rx_stats(netdev, pkt_len);
		}

1789
		skb_reserve(skb, pkt_off);
1790 1791
		skb_put(skb, pkt_len);

1792 1793
		skb->mark = meta.mark;
		skb_set_hash(skb, meta.hash, meta.hash_type);
1794

1795
		skb_record_rx_queue(skb, rx_ring->idx);
1796
		skb->protocol = eth_type_trans(skb, netdev);
1797

1798
		nfp_net_rx_csum(dp, r_vec, rxd, &meta, skb);
1799 1800 1801 1802

		if (rxd->rxd.flags & PCIE_DESC_RX_VLAN)
			__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
					       le16_to_cpu(rxd->rxd.vlan));
1803 1804
		if (meta_len_xdp)
			skb_metadata_set(skb, meta_len_xdp);
1805 1806 1807 1808

		napi_gro_receive(&rx_ring->r_vec->napi, skb);
	}

1809 1810 1811 1812 1813 1814 1815 1816
	if (xdp_prog) {
		if (tx_ring->wr_ptr_add)
			nfp_net_tx_xmit_more_flush(tx_ring);
		else if (unlikely(tx_ring->wr_p != tx_ring->rd_p) &&
			 !xdp_tx_cmpl)
			if (!nfp_net_xdp_complete(tx_ring))
				pkts_polled = budget;
	}
1817 1818
	rcu_read_unlock();

1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832
	return pkts_polled;
}

/**
 * nfp_net_poll() - napi poll function
 * @napi:    NAPI structure
 * @budget:  NAPI budget
 *
 * Return: number of packets polled.
 */
static int nfp_net_poll(struct napi_struct *napi, int budget)
{
	struct nfp_net_r_vector *r_vec =
		container_of(napi, struct nfp_net_r_vector, napi);
1833
	unsigned int pkts_polled = 0;
1834

1835
	if (r_vec->tx_ring)
1836
		nfp_net_tx_complete(r_vec->tx_ring, budget);
1837
	if (r_vec->rx_ring)
1838
		pkts_polled = nfp_net_rx(r_vec->rx_ring, budget);
1839

1840 1841 1842
	if (pkts_polled < budget)
		if (napi_complete_done(napi, pkts_polled))
			nfp_net_irq_unmask(r_vec->nfp_net, r_vec->irq_entry);
1843 1844 1845 1846

	return pkts_polled;
}

J
Jakub Kicinski 已提交
1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933
/* Control device data path
 */

static bool
nfp_ctrl_tx_one(struct nfp_net *nn, struct nfp_net_r_vector *r_vec,
		struct sk_buff *skb, bool old)
{
	unsigned int real_len = skb->len, meta_len = 0;
	struct nfp_net_tx_ring *tx_ring;
	struct nfp_net_tx_buf *txbuf;
	struct nfp_net_tx_desc *txd;
	struct nfp_net_dp *dp;
	dma_addr_t dma_addr;
	int wr_idx;

	dp = &r_vec->nfp_net->dp;
	tx_ring = r_vec->tx_ring;

	if (WARN_ON_ONCE(skb_shinfo(skb)->nr_frags)) {
		nn_dp_warn(dp, "Driver's CTRL TX does not implement gather\n");
		goto err_free;
	}

	if (unlikely(nfp_net_tx_full(tx_ring, 1))) {
		u64_stats_update_begin(&r_vec->tx_sync);
		r_vec->tx_busy++;
		u64_stats_update_end(&r_vec->tx_sync);
		if (!old)
			__skb_queue_tail(&r_vec->queue, skb);
		else
			__skb_queue_head(&r_vec->queue, skb);
		return true;
	}

	if (nfp_app_ctrl_has_meta(nn->app)) {
		if (unlikely(skb_headroom(skb) < 8)) {
			nn_dp_warn(dp, "CTRL TX on skb without headroom\n");
			goto err_free;
		}
		meta_len = 8;
		put_unaligned_be32(NFP_META_PORT_ID_CTRL, skb_push(skb, 4));
		put_unaligned_be32(NFP_NET_META_PORTID, skb_push(skb, 4));
	}

	/* Start with the head skbuf */
	dma_addr = dma_map_single(dp->dev, skb->data, skb_headlen(skb),
				  DMA_TO_DEVICE);
	if (dma_mapping_error(dp->dev, dma_addr))
		goto err_dma_warn;

	wr_idx = D_IDX(tx_ring, tx_ring->wr_p);

	/* Stash the soft descriptor of the head then initialize it */
	txbuf = &tx_ring->txbufs[wr_idx];
	txbuf->skb = skb;
	txbuf->dma_addr = dma_addr;
	txbuf->fidx = -1;
	txbuf->pkt_cnt = 1;
	txbuf->real_len = real_len;

	/* Build TX descriptor */
	txd = &tx_ring->txds[wr_idx];
	txd->offset_eop = meta_len | PCIE_DESC_TX_EOP;
	txd->dma_len = cpu_to_le16(skb_headlen(skb));
	nfp_desc_set_dma_addr(txd, dma_addr);
	txd->data_len = cpu_to_le16(skb->len);

	txd->flags = 0;
	txd->mss = 0;
	txd->lso_hdrlen = 0;

	tx_ring->wr_p++;
	tx_ring->wr_ptr_add++;
	nfp_net_tx_xmit_more_flush(tx_ring);

	return false;

err_dma_warn:
	nn_dp_warn(dp, "Failed to DMA map TX CTRL buffer\n");
err_free:
	u64_stats_update_begin(&r_vec->tx_sync);
	r_vec->tx_errors++;
	u64_stats_update_end(&r_vec->tx_sync);
	dev_kfree_skb_any(skb);
	return false;
}

1934 1935 1936 1937 1938 1939 1940
bool __nfp_ctrl_tx(struct nfp_net *nn, struct sk_buff *skb)
{
	struct nfp_net_r_vector *r_vec = &nn->r_vecs[0];

	return nfp_ctrl_tx_one(nn, r_vec, skb, false);
}

J
Jakub Kicinski 已提交
1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049
bool nfp_ctrl_tx(struct nfp_net *nn, struct sk_buff *skb)
{
	struct nfp_net_r_vector *r_vec = &nn->r_vecs[0];
	bool ret;

	spin_lock_bh(&r_vec->lock);
	ret = nfp_ctrl_tx_one(nn, r_vec, skb, false);
	spin_unlock_bh(&r_vec->lock);

	return ret;
}

static void __nfp_ctrl_tx_queued(struct nfp_net_r_vector *r_vec)
{
	struct sk_buff *skb;

	while ((skb = __skb_dequeue(&r_vec->queue)))
		if (nfp_ctrl_tx_one(r_vec->nfp_net, r_vec, skb, true))
			return;
}

static bool
nfp_ctrl_meta_ok(struct nfp_net *nn, void *data, unsigned int meta_len)
{
	u32 meta_type, meta_tag;

	if (!nfp_app_ctrl_has_meta(nn->app))
		return !meta_len;

	if (meta_len != 8)
		return false;

	meta_type = get_unaligned_be32(data);
	meta_tag = get_unaligned_be32(data + 4);

	return (meta_type == NFP_NET_META_PORTID &&
		meta_tag == NFP_META_PORT_ID_CTRL);
}

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)
{
	unsigned int meta_len, data_len, meta_off, pkt_len, pkt_off;
	struct nfp_net_rx_buf *rxbuf;
	struct nfp_net_rx_desc *rxd;
	dma_addr_t new_dma_addr;
	struct sk_buff *skb;
	void *new_frag;
	int idx;

	idx = D_IDX(rx_ring, rx_ring->rd_p);

	rxd = &rx_ring->rxds[idx];
	if (!(rxd->rxd.meta_len_dd & PCIE_DESC_RX_DD))
		return false;

	/* Memory barrier to ensure that we won't do other reads
	 * before the DD bit.
	 */
	dma_rmb();

	rx_ring->rd_p++;

	rxbuf =	&rx_ring->rxbufs[idx];
	meta_len = rxd->rxd.meta_len_dd & PCIE_DESC_RX_META_LEN_MASK;
	data_len = le16_to_cpu(rxd->rxd.data_len);
	pkt_len = data_len - meta_len;

	pkt_off = NFP_NET_RX_BUF_HEADROOM + dp->rx_dma_off;
	if (dp->rx_offset == NFP_NET_CFG_RX_OFFSET_DYNAMIC)
		pkt_off += meta_len;
	else
		pkt_off += dp->rx_offset;
	meta_off = pkt_off - meta_len;

	/* Stats update */
	u64_stats_update_begin(&r_vec->rx_sync);
	r_vec->rx_pkts++;
	r_vec->rx_bytes += pkt_len;
	u64_stats_update_end(&r_vec->rx_sync);

	nfp_net_dma_sync_cpu_rx(dp, rxbuf->dma_addr + meta_off,	data_len);

	if (unlikely(!nfp_ctrl_meta_ok(nn, rxbuf->frag + meta_off, meta_len))) {
		nn_dp_warn(dp, "incorrect metadata for ctrl packet (%d)\n",
			   meta_len);
		nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
		return true;
	}

	skb = build_skb(rxbuf->frag, dp->fl_bufsz);
	if (unlikely(!skb)) {
		nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
		return true;
	}
	new_frag = nfp_net_napi_alloc_one(dp, &new_dma_addr);
	if (unlikely(!new_frag)) {
		nfp_net_rx_drop(dp, r_vec, rx_ring, rxbuf, skb);
		return true;
	}

	nfp_net_dma_unmap_rx(dp, rxbuf->dma_addr);

	nfp_net_rx_give_one(dp, rx_ring, new_frag, new_dma_addr);

	skb_reserve(skb, pkt_off);
	skb_put(skb, pkt_len);

2050
	nfp_app_ctrl_rx(nn->app, skb);
J
Jakub Kicinski 已提交
2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069

	return true;
}

static void nfp_ctrl_rx(struct nfp_net_r_vector *r_vec)
{
	struct nfp_net_rx_ring *rx_ring = r_vec->rx_ring;
	struct nfp_net *nn = r_vec->nfp_net;
	struct nfp_net_dp *dp = &nn->dp;

	while (nfp_ctrl_rx_one(nn, dp, r_vec, rx_ring))
		continue;
}

static void nfp_ctrl_poll(unsigned long arg)
{
	struct nfp_net_r_vector *r_vec = (void *)arg;

	spin_lock_bh(&r_vec->lock);
2070
	nfp_net_tx_complete(r_vec->tx_ring, 0);
J
Jakub Kicinski 已提交
2071 2072 2073 2074 2075 2076 2077 2078
	__nfp_ctrl_tx_queued(r_vec);
	spin_unlock_bh(&r_vec->lock);

	nfp_ctrl_rx(r_vec);

	nfp_net_irq_unmask(r_vec->nfp_net, r_vec->irq_entry);
}

2079 2080 2081
/* Setup and Configuration
 */

J
Jakub Kicinski 已提交
2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103
/**
 * nfp_net_vecs_init() - Assign IRQs and setup rvecs.
 * @nn:		NFP Network structure
 */
static void nfp_net_vecs_init(struct nfp_net *nn)
{
	struct nfp_net_r_vector *r_vec;
	int r;

	nn->lsc_handler = nfp_net_irq_lsc;
	nn->exn_handler = nfp_net_irq_exn;

	for (r = 0; r < nn->max_r_vecs; r++) {
		struct msix_entry *entry;

		entry = &nn->irq_entries[NFP_NET_NON_Q_VECTORS + r];

		r_vec = &nn->r_vecs[r];
		r_vec->nfp_net = nn;
		r_vec->irq_entry = entry->entry;
		r_vec->irq_vector = entry->vector;

J
Jakub Kicinski 已提交
2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115
		if (nn->dp.netdev) {
			r_vec->handler = nfp_net_irq_rxtx;
		} else {
			r_vec->handler = nfp_ctrl_irq_rxtx;

			__skb_queue_head_init(&r_vec->queue);
			spin_lock_init(&r_vec->lock);
			tasklet_init(&r_vec->tasklet, nfp_ctrl_poll,
				     (unsigned long)r_vec);
			tasklet_disable(&r_vec->tasklet);
		}

J
Jakub Kicinski 已提交
2116 2117 2118 2119
		cpumask_set_cpu(r, &r_vec->affinity_mask);
	}
}

2120 2121 2122 2123 2124 2125 2126
/**
 * nfp_net_tx_ring_free() - Free resources allocated to a TX ring
 * @tx_ring:   TX ring to free
 */
static void nfp_net_tx_ring_free(struct nfp_net_tx_ring *tx_ring)
{
	struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
2127
	struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
2128 2129 2130 2131

	kfree(tx_ring->txbufs);

	if (tx_ring->txds)
2132
		dma_free_coherent(dp->dev, tx_ring->size,
2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143
				  tx_ring->txds, tx_ring->dma);

	tx_ring->cnt = 0;
	tx_ring->txbufs = NULL;
	tx_ring->txds = NULL;
	tx_ring->dma = 0;
	tx_ring->size = 0;
}

/**
 * nfp_net_tx_ring_alloc() - Allocate resource for a TX ring
2144
 * @dp:        NFP Net data path struct
2145 2146 2147 2148
 * @tx_ring:   TX Ring structure to allocate
 *
 * Return: 0 on success, negative errno otherwise.
 */
2149
static int
2150
nfp_net_tx_ring_alloc(struct nfp_net_dp *dp, struct nfp_net_tx_ring *tx_ring)
2151 2152 2153 2154
{
	struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
	int sz;

2155
	tx_ring->cnt = dp->txd_cnt;
2156 2157

	tx_ring->size = sizeof(*tx_ring->txds) * tx_ring->cnt;
2158
	tx_ring->txds = dma_zalloc_coherent(dp->dev, tx_ring->size,
2159 2160 2161 2162 2163 2164 2165 2166 2167
					    &tx_ring->dma, GFP_KERNEL);
	if (!tx_ring->txds)
		goto err_alloc;

	sz = sizeof(*tx_ring->txbufs) * tx_ring->cnt;
	tx_ring->txbufs = kzalloc(sz, GFP_KERNEL);
	if (!tx_ring->txbufs)
		goto err_alloc;

2168
	if (!tx_ring->is_xdp && dp->netdev)
2169
		netif_set_xps_queue(dp->netdev, &r_vec->affinity_mask,
2170
				    tx_ring->idx);
2171 2172 2173 2174 2175 2176 2177 2178

	return 0;

err_alloc:
	nfp_net_tx_ring_free(tx_ring);
	return -ENOMEM;
}

2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217
static void
nfp_net_tx_ring_bufs_free(struct nfp_net_dp *dp,
			  struct nfp_net_tx_ring *tx_ring)
{
	unsigned int i;

	if (!tx_ring->is_xdp)
		return;

	for (i = 0; i < tx_ring->cnt; i++) {
		if (!tx_ring->txbufs[i].frag)
			return;

		nfp_net_dma_unmap_rx(dp, tx_ring->txbufs[i].dma_addr);
		__free_page(virt_to_page(tx_ring->txbufs[i].frag));
	}
}

static int
nfp_net_tx_ring_bufs_alloc(struct nfp_net_dp *dp,
			   struct nfp_net_tx_ring *tx_ring)
{
	struct nfp_net_tx_buf *txbufs = tx_ring->txbufs;
	unsigned int i;

	if (!tx_ring->is_xdp)
		return 0;

	for (i = 0; i < tx_ring->cnt; i++) {
		txbufs[i].frag = nfp_net_rx_alloc_one(dp, &txbufs[i].dma_addr);
		if (!txbufs[i].frag) {
			nfp_net_tx_ring_bufs_free(dp, tx_ring);
			return -ENOMEM;
		}
	}

	return 0;
}

2218
static int nfp_net_tx_rings_prepare(struct nfp_net *nn, struct nfp_net_dp *dp)
2219 2220 2221
{
	unsigned int r;

2222 2223 2224 2225
	dp->tx_rings = kcalloc(dp->num_tx_rings, sizeof(*dp->tx_rings),
			       GFP_KERNEL);
	if (!dp->tx_rings)
		return -ENOMEM;
2226

2227
	for (r = 0; r < dp->num_tx_rings; r++) {
2228 2229
		int bias = 0;

2230 2231
		if (r >= dp->num_stack_tx_rings)
			bias = dp->num_stack_tx_rings;
2232

2233
		nfp_net_tx_ring_init(&dp->tx_rings[r], &nn->r_vecs[r - bias],
2234
				     r, bias);
2235

2236
		if (nfp_net_tx_ring_alloc(dp, &dp->tx_rings[r]))
2237
			goto err_free_prev;
2238 2239 2240

		if (nfp_net_tx_ring_bufs_alloc(dp, &dp->tx_rings[r]))
			goto err_free_ring;
2241 2242
	}

2243
	return 0;
2244 2245

err_free_prev:
2246 2247 2248
	while (r--) {
		nfp_net_tx_ring_bufs_free(dp, &dp->tx_rings[r]);
err_free_ring:
2249
		nfp_net_tx_ring_free(&dp->tx_rings[r]);
2250
	}
2251 2252
	kfree(dp->tx_rings);
	return -ENOMEM;
2253 2254
}

2255
static void nfp_net_tx_rings_free(struct nfp_net_dp *dp)
2256 2257 2258
{
	unsigned int r;

2259 2260
	for (r = 0; r < dp->num_tx_rings; r++) {
		nfp_net_tx_ring_bufs_free(dp, &dp->tx_rings[r]);
2261
		nfp_net_tx_ring_free(&dp->tx_rings[r]);
2262
	}
2263

2264
	kfree(dp->tx_rings);
2265 2266
}

2267 2268 2269 2270 2271 2272 2273
/**
 * nfp_net_rx_ring_free() - Free resources allocated to a RX ring
 * @rx_ring:  RX ring to free
 */
static void nfp_net_rx_ring_free(struct nfp_net_rx_ring *rx_ring)
{
	struct nfp_net_r_vector *r_vec = rx_ring->r_vec;
2274
	struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
2275

2276 2277
	if (dp->netdev)
		xdp_rxq_info_unreg(&rx_ring->xdp_rxq);
2278 2279 2280
	kfree(rx_ring->rxbufs);

	if (rx_ring->rxds)
2281
		dma_free_coherent(dp->dev, rx_ring->size,
2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292
				  rx_ring->rxds, rx_ring->dma);

	rx_ring->cnt = 0;
	rx_ring->rxbufs = NULL;
	rx_ring->rxds = NULL;
	rx_ring->dma = 0;
	rx_ring->size = 0;
}

/**
 * nfp_net_rx_ring_alloc() - Allocate resource for a RX ring
2293
 * @dp:	      NFP Net data path struct
2294 2295 2296 2297
 * @rx_ring:  RX ring to allocate
 *
 * Return: 0 on success, negative errno otherwise.
 */
2298
static int
2299
nfp_net_rx_ring_alloc(struct nfp_net_dp *dp, struct nfp_net_rx_ring *rx_ring)
2300
{
J
Jesper Dangaard Brouer 已提交
2301 2302
	int sz, err;

2303 2304 2305 2306 2307 2308
	if (dp->netdev) {
		err = xdp_rxq_info_reg(&rx_ring->xdp_rxq, dp->netdev,
				       rx_ring->idx);
		if (err < 0)
			return err;
	}
2309

2310
	rx_ring->cnt = dp->rxd_cnt;
2311
	rx_ring->size = sizeof(*rx_ring->rxds) * rx_ring->cnt;
2312
	rx_ring->rxds = dma_zalloc_coherent(dp->dev, rx_ring->size,
2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328
					    &rx_ring->dma, GFP_KERNEL);
	if (!rx_ring->rxds)
		goto err_alloc;

	sz = sizeof(*rx_ring->rxbufs) * rx_ring->cnt;
	rx_ring->rxbufs = kzalloc(sz, GFP_KERNEL);
	if (!rx_ring->rxbufs)
		goto err_alloc;

	return 0;

err_alloc:
	nfp_net_rx_ring_free(rx_ring);
	return -ENOMEM;
}

2329
static int nfp_net_rx_rings_prepare(struct nfp_net *nn, struct nfp_net_dp *dp)
2330 2331 2332
{
	unsigned int r;

2333 2334 2335 2336
	dp->rx_rings = kcalloc(dp->num_rx_rings, sizeof(*dp->rx_rings),
			       GFP_KERNEL);
	if (!dp->rx_rings)
		return -ENOMEM;
2337

2338 2339
	for (r = 0; r < dp->num_rx_rings; r++) {
		nfp_net_rx_ring_init(&dp->rx_rings[r], &nn->r_vecs[r], r);
2340

2341
		if (nfp_net_rx_ring_alloc(dp, &dp->rx_rings[r]))
2342 2343
			goto err_free_prev;

2344
		if (nfp_net_rx_ring_bufs_alloc(dp, &dp->rx_rings[r]))
2345 2346 2347
			goto err_free_ring;
	}

2348
	return 0;
2349 2350 2351

err_free_prev:
	while (r--) {
2352
		nfp_net_rx_ring_bufs_free(dp, &dp->rx_rings[r]);
2353
err_free_ring:
2354
		nfp_net_rx_ring_free(&dp->rx_rings[r]);
2355
	}
2356 2357
	kfree(dp->rx_rings);
	return -ENOMEM;
2358 2359
}

2360
static void nfp_net_rx_rings_free(struct nfp_net_dp *dp)
2361 2362 2363
{
	unsigned int r;

2364 2365 2366
	for (r = 0; r < dp->num_rx_rings; r++) {
		nfp_net_rx_ring_bufs_free(dp, &dp->rx_rings[r]);
		nfp_net_rx_ring_free(&dp->rx_rings[r]);
2367 2368
	}

2369
	kfree(dp->rx_rings);
2370 2371
}

2372
static void
2373 2374
nfp_net_vector_assign_rings(struct nfp_net_dp *dp,
			    struct nfp_net_r_vector *r_vec, int idx)
2375
{
2376
	r_vec->rx_ring = idx < dp->num_rx_rings ? &dp->rx_rings[idx] : NULL;
2377
	r_vec->tx_ring =
2378
		idx < dp->num_stack_tx_rings ? &dp->tx_rings[idx] : NULL;
2379

2380 2381
	r_vec->xdp_ring = idx < dp->num_tx_rings - dp->num_stack_tx_rings ?
		&dp->tx_rings[dp->num_stack_tx_rings + idx] : NULL;
2382 2383
}

2384 2385 2386
static int
nfp_net_prepare_vector(struct nfp_net *nn, struct nfp_net_r_vector *r_vec,
		       int idx)
2387
{
2388
	int err;
2389

2390
	/* Setup NAPI */
J
Jakub Kicinski 已提交
2391 2392 2393 2394 2395
	if (nn->dp.netdev)
		netif_napi_add(nn->dp.netdev, &r_vec->napi,
			       nfp_net_poll, NAPI_POLL_WEIGHT);
	else
		tasklet_enable(&r_vec->tasklet);
2396

2397
	snprintf(r_vec->name, sizeof(r_vec->name),
J
Jakub Kicinski 已提交
2398
		 "%s-rxtx-%d", nfp_net_name(nn), idx);
2399 2400
	err = request_irq(r_vec->irq_vector, r_vec->handler, 0, r_vec->name,
			  r_vec);
2401
	if (err) {
J
Jakub Kicinski 已提交
2402 2403 2404 2405 2406
		if (nn->dp.netdev)
			netif_napi_del(&r_vec->napi);
		else
			tasklet_disable(&r_vec->tasklet);

2407
		nn_err(nn, "Error requesting IRQ %d\n", r_vec->irq_vector);
2408 2409
		return err;
	}
2410
	disable_irq(r_vec->irq_vector);
2411

2412
	irq_set_affinity_hint(r_vec->irq_vector, &r_vec->affinity_mask);
2413

2414 2415
	nn_dbg(nn, "RV%02d: irq=%03d/%03d\n", idx, r_vec->irq_vector,
	       r_vec->irq_entry);
2416

2417
	return 0;
2418 2419
}

2420 2421
static void
nfp_net_cleanup_vector(struct nfp_net *nn, struct nfp_net_r_vector *r_vec)
2422
{
2423
	irq_set_affinity_hint(r_vec->irq_vector, NULL);
J
Jakub Kicinski 已提交
2424 2425 2426 2427 2428
	if (nn->dp.netdev)
		netif_napi_del(&r_vec->napi);
	else
		tasklet_disable(&r_vec->tasklet);

2429
	free_irq(r_vec->irq_vector, r_vec);
2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452
}

/**
 * nfp_net_rss_write_itbl() - Write RSS indirection table to device
 * @nn:      NFP Net device to reconfigure
 */
void nfp_net_rss_write_itbl(struct nfp_net *nn)
{
	int i;

	for (i = 0; i < NFP_NET_CFG_RSS_ITBL_SZ; i += 4)
		nn_writel(nn, NFP_NET_CFG_RSS_ITBL + i,
			  get_unaligned_le32(nn->rss_itbl + i));
}

/**
 * nfp_net_rss_write_key() - Write RSS hash key to device
 * @nn:      NFP Net device to reconfigure
 */
void nfp_net_rss_write_key(struct nfp_net *nn)
{
	int i;

2453
	for (i = 0; i < nfp_net_rss_key_sz(nn); i += 4)
2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471
		nn_writel(nn, NFP_NET_CFG_RSS_KEY + i,
			  get_unaligned_le32(nn->rss_key + i));
}

/**
 * nfp_net_coalesce_write_cfg() - Write irq coalescence configuration to HW
 * @nn:      NFP Net device to reconfigure
 */
void nfp_net_coalesce_write_cfg(struct nfp_net *nn)
{
	u8 i;
	u32 factor;
	u32 value;

	/* Compute factor used to convert coalesce '_usecs' parameters to
	 * ME timestamp ticks.  There are 16 ME clock cycles for each timestamp
	 * count.
	 */
2472
	factor = nn->tlv_caps.me_freq_mhz / 16;
2473 2474 2475 2476

	/* copy RX interrupt coalesce parameters */
	value = (nn->rx_coalesce_max_frames << 16) |
		(factor * nn->rx_coalesce_usecs);
2477
	for (i = 0; i < nn->dp.num_rx_rings; i++)
2478 2479 2480 2481 2482
		nn_writel(nn, NFP_NET_CFG_RXR_IRQ_MOD(i), value);

	/* copy TX interrupt coalesce parameters */
	value = (nn->tx_coalesce_max_frames << 16) |
		(factor * nn->tx_coalesce_usecs);
2483
	for (i = 0; i < nn->dp.num_tx_rings; i++)
2484 2485 2486 2487
		nn_writel(nn, NFP_NET_CFG_TXR_IRQ_MOD(i), value);
}

/**
2488
 * nfp_net_write_mac_addr() - Write mac address to the device control BAR
2489
 * @nn:      NFP Net device to reconfigure
2490
 * @addr:    MAC address to write
2491
 *
2492 2493 2494
 * Writes the MAC address from the netdev to the device control BAR.  Does not
 * perform the required reconfig.  We do a bit of byte swapping dance because
 * firmware is LE.
2495
 */
2496
static void nfp_net_write_mac_addr(struct nfp_net *nn, const u8 *addr)
2497
{
2498 2499
	nn_writel(nn, NFP_NET_CFG_MACADDR + 0, get_unaligned_be32(addr));
	nn_writew(nn, NFP_NET_CFG_MACADDR + 6, get_unaligned_be16(addr + 4));
2500 2501
}

2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512
static void nfp_net_vec_clear_ring_data(struct nfp_net *nn, unsigned int idx)
{
	nn_writeq(nn, NFP_NET_CFG_RXR_ADDR(idx), 0);
	nn_writeb(nn, NFP_NET_CFG_RXR_SZ(idx), 0);
	nn_writeb(nn, NFP_NET_CFG_RXR_VEC(idx), 0);

	nn_writeq(nn, NFP_NET_CFG_TXR_ADDR(idx), 0);
	nn_writeb(nn, NFP_NET_CFG_TXR_SZ(idx), 0);
	nn_writeb(nn, NFP_NET_CFG_TXR_VEC(idx), 0);
}

2513 2514 2515
/**
 * nfp_net_clear_config_and_disable() - Clear control BAR and disable NFP
 * @nn:      NFP Net device to reconfigure
2516 2517
 *
 * Warning: must be fully idempotent.
2518 2519 2520 2521
 */
static void nfp_net_clear_config_and_disable(struct nfp_net *nn)
{
	u32 new_ctrl, update;
2522
	unsigned int r;
2523 2524
	int err;

2525
	new_ctrl = nn->dp.ctrl;
2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538
	new_ctrl &= ~NFP_NET_CFG_CTRL_ENABLE;
	update = NFP_NET_CFG_UPDATE_GEN;
	update |= NFP_NET_CFG_UPDATE_MSIX;
	update |= NFP_NET_CFG_UPDATE_RING;

	if (nn->cap & NFP_NET_CFG_CTRL_RINGCFG)
		new_ctrl &= ~NFP_NET_CFG_CTRL_RINGCFG;

	nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, 0);
	nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, 0);

	nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
	err = nfp_net_reconfig(nn, update);
2539
	if (err)
2540 2541
		nn_err(nn, "Could not disable device: %d\n", err);

2542 2543 2544 2545 2546
	for (r = 0; r < nn->dp.num_rx_rings; r++)
		nfp_net_rx_ring_reset(&nn->dp.rx_rings[r]);
	for (r = 0; r < nn->dp.num_tx_rings; r++)
		nfp_net_tx_ring_reset(&nn->dp, &nn->dp.tx_rings[r]);
	for (r = 0; r < nn->dp.num_r_vecs; r++)
2547 2548
		nfp_net_vec_clear_ring_data(nn, r);

2549
	nn->dp.ctrl = new_ctrl;
2550 2551
}

2552
static void
2553 2554
nfp_net_rx_ring_hw_cfg_write(struct nfp_net *nn,
			     struct nfp_net_rx_ring *rx_ring, unsigned int idx)
2555 2556
{
	/* Write the DMA address, size and MSI-X info to the device */
2557 2558
	nn_writeq(nn, NFP_NET_CFG_RXR_ADDR(idx), rx_ring->dma);
	nn_writeb(nn, NFP_NET_CFG_RXR_SZ(idx), ilog2(rx_ring->cnt));
2559
	nn_writeb(nn, NFP_NET_CFG_RXR_VEC(idx), rx_ring->r_vec->irq_entry);
2560
}
2561

2562 2563 2564 2565 2566 2567
static void
nfp_net_tx_ring_hw_cfg_write(struct nfp_net *nn,
			     struct nfp_net_tx_ring *tx_ring, unsigned int idx)
{
	nn_writeq(nn, NFP_NET_CFG_TXR_ADDR(idx), tx_ring->dma);
	nn_writeb(nn, NFP_NET_CFG_TXR_SZ(idx), ilog2(tx_ring->cnt));
2568
	nn_writeb(nn, NFP_NET_CFG_TXR_VEC(idx), tx_ring->r_vec->irq_entry);
2569 2570
}

2571 2572 2573 2574 2575
/**
 * nfp_net_set_config_and_enable() - Write control BAR and enable NFP
 * @nn:      NFP Net device to reconfigure
 */
static int nfp_net_set_config_and_enable(struct nfp_net *nn)
2576
{
2577
	u32 bufsz, new_ctrl, update = 0;
2578 2579 2580
	unsigned int r;
	int err;

2581
	new_ctrl = nn->dp.ctrl;
2582

2583
	if (nn->dp.ctrl & NFP_NET_CFG_CTRL_RSS_ANY) {
2584 2585 2586 2587 2588 2589
		nfp_net_rss_write_key(nn);
		nfp_net_rss_write_itbl(nn);
		nn_writel(nn, NFP_NET_CFG_RSS_CTRL, nn->rss_cfg);
		update |= NFP_NET_CFG_UPDATE_RSS;
	}

2590
	if (nn->dp.ctrl & NFP_NET_CFG_CTRL_IRQMOD) {
2591 2592 2593 2594
		nfp_net_coalesce_write_cfg(nn);
		update |= NFP_NET_CFG_UPDATE_IRQMOD;
	}

2595 2596 2597 2598
	for (r = 0; r < nn->dp.num_tx_rings; r++)
		nfp_net_tx_ring_hw_cfg_write(nn, &nn->dp.tx_rings[r], r);
	for (r = 0; r < nn->dp.num_rx_rings; r++)
		nfp_net_rx_ring_hw_cfg_write(nn, &nn->dp.rx_rings[r], r);
2599

2600 2601
	nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, nn->dp.num_tx_rings == 64 ?
		  0xffffffffffffffffULL : ((u64)1 << nn->dp.num_tx_rings) - 1);
2602

2603 2604
	nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, nn->dp.num_rx_rings == 64 ?
		  0xffffffffffffffffULL : ((u64)1 << nn->dp.num_rx_rings) - 1);
2605

2606 2607
	if (nn->dp.netdev)
		nfp_net_write_mac_addr(nn, nn->dp.netdev->dev_addr);
2608

2609
	nn_writel(nn, NFP_NET_CFG_MTU, nn->dp.mtu);
2610 2611 2612

	bufsz = nn->dp.fl_bufsz - nn->dp.rx_dma_off - NFP_NET_RX_BUF_NON_DATA;
	nn_writel(nn, NFP_NET_CFG_FLBUFSZ, bufsz);
2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623

	/* Enable device */
	new_ctrl |= NFP_NET_CFG_CTRL_ENABLE;
	update |= NFP_NET_CFG_UPDATE_GEN;
	update |= NFP_NET_CFG_UPDATE_MSIX;
	update |= NFP_NET_CFG_UPDATE_RING;
	if (nn->cap & NFP_NET_CFG_CTRL_RINGCFG)
		new_ctrl |= NFP_NET_CFG_CTRL_RINGCFG;

	nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
	err = nfp_net_reconfig(nn, update);
2624 2625 2626 2627
	if (err) {
		nfp_net_clear_config_and_disable(nn);
		return err;
	}
2628

2629
	nn->dp.ctrl = new_ctrl;
2630

2631
	for (r = 0; r < nn->dp.num_rx_rings; r++)
2632
		nfp_net_rx_ring_fill_freelist(&nn->dp, &nn->dp.rx_rings[r]);
2633

2634 2635 2636
	/* Since reconfiguration requests while NFP is down are ignored we
	 * have to wipe the entire VXLAN configuration and reinitialize it.
	 */
2637
	if (nn->dp.ctrl & NFP_NET_CFG_CTRL_VXLAN) {
2638 2639
		memset(&nn->vxlan_ports, 0, sizeof(nn->vxlan_ports));
		memset(&nn->vxlan_usecnt, 0, sizeof(nn->vxlan_usecnt));
2640
		udp_tunnel_get_rx_info(nn->dp.netdev);
2641 2642
	}

2643
	return 0;
2644 2645
}

2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673
/**
 * nfp_net_close_stack() - Quiesce the stack (part of close)
 * @nn:	     NFP Net device to reconfigure
 */
static void nfp_net_close_stack(struct nfp_net *nn)
{
	unsigned int r;

	disable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
	netif_carrier_off(nn->dp.netdev);
	nn->link_up = false;

	for (r = 0; r < nn->dp.num_r_vecs; r++) {
		disable_irq(nn->r_vecs[r].irq_vector);
		napi_disable(&nn->r_vecs[r].napi);
	}

	netif_tx_disable(nn->dp.netdev);
}

/**
 * nfp_net_close_free_all() - Free all runtime resources
 * @nn:      NFP Net device to reconfigure
 */
static void nfp_net_close_free_all(struct nfp_net *nn)
{
	unsigned int r;

2674 2675 2676
	nfp_net_tx_rings_free(&nn->dp);
	nfp_net_rx_rings_free(&nn->dp);

2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698
	for (r = 0; r < nn->dp.num_r_vecs; r++)
		nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);

	nfp_net_aux_irq_free(nn, NFP_NET_CFG_LSC, NFP_NET_IRQ_LSC_IDX);
	nfp_net_aux_irq_free(nn, NFP_NET_CFG_EXN, NFP_NET_IRQ_EXN_IDX);
}

/**
 * nfp_net_netdev_close() - Called when the device is downed
 * @netdev:      netdev structure
 */
static int nfp_net_netdev_close(struct net_device *netdev)
{
	struct nfp_net *nn = netdev_priv(netdev);

	/* Step 1: Disable RX and TX rings from the Linux kernel perspective
	 */
	nfp_net_close_stack(nn);

	/* Step 2: Tell NFP
	 */
	nfp_net_clear_config_and_disable(nn);
2699
	nfp_port_configure(netdev, false);
2700 2701 2702 2703 2704 2705 2706 2707 2708

	/* Step 3: Free resources
	 */
	nfp_net_close_free_all(nn);

	nn_dbg(nn, "%s down", netdev->name);
	return 0;
}

J
Jakub Kicinski 已提交
2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726
void nfp_ctrl_close(struct nfp_net *nn)
{
	int r;

	rtnl_lock();

	for (r = 0; r < nn->dp.num_r_vecs; r++) {
		disable_irq(nn->r_vecs[r].irq_vector);
		tasklet_disable(&nn->r_vecs[r].tasklet);
	}

	nfp_net_clear_config_and_disable(nn);

	nfp_net_close_free_all(nn);

	rtnl_unlock();
}

2727 2728 2729 2730 2731 2732 2733 2734
/**
 * nfp_net_open_stack() - Start the device from stack's perspective
 * @nn:      NFP Net device to reconfigure
 */
static void nfp_net_open_stack(struct nfp_net *nn)
{
	unsigned int r;

2735
	for (r = 0; r < nn->dp.num_r_vecs; r++) {
2736
		napi_enable(&nn->r_vecs[r].napi);
2737
		enable_irq(nn->r_vecs[r].irq_vector);
2738
	}
2739

2740
	netif_tx_wake_all_queues(nn->dp.netdev);
2741

2742
	enable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
2743 2744 2745
	nfp_net_read_link_status(nn);
}

2746
static int nfp_net_open_alloc_all(struct nfp_net *nn)
2747 2748 2749 2750 2751 2752 2753 2754
{
	int err, r;

	err = nfp_net_aux_irq_request(nn, NFP_NET_CFG_EXN, "%s-exn",
				      nn->exn_name, sizeof(nn->exn_name),
				      NFP_NET_IRQ_EXN_IDX, nn->exn_handler);
	if (err)
		return err;
2755 2756 2757 2758 2759
	err = nfp_net_aux_irq_request(nn, NFP_NET_CFG_LSC, "%s-lsc",
				      nn->lsc_name, sizeof(nn->lsc_name),
				      NFP_NET_IRQ_LSC_IDX, nn->lsc_handler);
	if (err)
		goto err_free_exn;
2760
	disable_irq(nn->irq_entries[NFP_NET_IRQ_LSC_IDX].vector);
2761

2762
	for (r = 0; r < nn->dp.num_r_vecs; r++) {
2763 2764
		err = nfp_net_prepare_vector(nn, &nn->r_vecs[r], r);
		if (err)
2765 2766
			goto err_cleanup_vec_p;
	}
2767

2768 2769
	err = nfp_net_rx_rings_prepare(nn, &nn->dp);
	if (err)
2770
		goto err_cleanup_vec;
2771

2772 2773
	err = nfp_net_tx_rings_prepare(nn, &nn->dp);
	if (err)
2774
		goto err_free_rx_rings;
2775

2776
	for (r = 0; r < nn->max_r_vecs; r++)
2777
		nfp_net_vector_assign_rings(&nn->dp, &nn->r_vecs[r], r);
2778

2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807
	return 0;

err_free_rx_rings:
	nfp_net_rx_rings_free(&nn->dp);
err_cleanup_vec:
	r = nn->dp.num_r_vecs;
err_cleanup_vec_p:
	while (r--)
		nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
	nfp_net_aux_irq_free(nn, NFP_NET_CFG_LSC, NFP_NET_IRQ_LSC_IDX);
err_free_exn:
	nfp_net_aux_irq_free(nn, NFP_NET_CFG_EXN, NFP_NET_IRQ_EXN_IDX);
	return err;
}

static int nfp_net_netdev_open(struct net_device *netdev)
{
	struct nfp_net *nn = netdev_priv(netdev);
	int err;

	/* Step 1: Allocate resources for rings and the like
	 * - Request interrupts
	 * - Allocate RX and TX ring resources
	 * - Setup initial RSS table
	 */
	err = nfp_net_open_alloc_all(nn);
	if (err)
		return err;

2808
	err = netif_set_real_num_tx_queues(netdev, nn->dp.num_stack_tx_rings);
2809
	if (err)
2810
		goto err_free_all;
2811

2812
	err = netif_set_real_num_rx_queues(netdev, nn->dp.num_rx_rings);
2813
	if (err)
2814
		goto err_free_all;
2815 2816

	/* Step 2: Configure the NFP
2817
	 * - Ifup the physical interface if it exists
2818 2819 2820 2821 2822 2823
	 * - Enable rings from 0 to tx_rings/rx_rings - 1.
	 * - Write MAC address (in case it changed)
	 * - Set the MTU
	 * - Set the Freelist buffer size
	 * - Enable the FW
	 */
2824
	err = nfp_port_configure(netdev, true);
2825
	if (err)
2826
		goto err_free_all;
2827

2828 2829 2830 2831
	err = nfp_net_set_config_and_enable(nn);
	if (err)
		goto err_port_disable;

2832 2833 2834 2835 2836 2837
	/* Step 3: Enable for kernel
	 * - put some freelist descriptors on each RX ring
	 * - enable NAPI on each ring
	 * - enable all TX queues
	 * - set link state
	 */
2838
	nfp_net_open_stack(nn);
2839 2840 2841

	return 0;

2842 2843
err_port_disable:
	nfp_port_configure(netdev, false);
2844 2845
err_free_all:
	nfp_net_close_free_all(nn);
2846 2847 2848
	return err;
}

J
Jakub Kicinski 已提交
2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877
int nfp_ctrl_open(struct nfp_net *nn)
{
	int err, r;

	/* ring dumping depends on vNICs being opened/closed under rtnl */
	rtnl_lock();

	err = nfp_net_open_alloc_all(nn);
	if (err)
		goto err_unlock;

	err = nfp_net_set_config_and_enable(nn);
	if (err)
		goto err_free_all;

	for (r = 0; r < nn->dp.num_r_vecs; r++)
		enable_irq(nn->r_vecs[r].irq_vector);

	rtnl_unlock();

	return 0;

err_free_all:
	nfp_net_close_free_all(nn);
err_unlock:
	rtnl_unlock();
	return err;
}

2878 2879 2880 2881 2882
static void nfp_net_set_rx_mode(struct net_device *netdev)
{
	struct nfp_net *nn = netdev_priv(netdev);
	u32 new_ctrl;

2883
	new_ctrl = nn->dp.ctrl;
2884

2885 2886 2887 2888 2889
	if (!netdev_mc_empty(netdev) || netdev->flags & IFF_ALLMULTI)
		new_ctrl |= nn->cap & NFP_NET_CFG_CTRL_L2MC;
	else
		new_ctrl &= ~NFP_NET_CFG_CTRL_L2MC;

2890 2891 2892 2893 2894 2895 2896 2897 2898
	if (netdev->flags & IFF_PROMISC) {
		if (nn->cap & NFP_NET_CFG_CTRL_PROMISC)
			new_ctrl |= NFP_NET_CFG_CTRL_PROMISC;
		else
			nn_warn(nn, "FW does not support promiscuous mode\n");
	} else {
		new_ctrl &= ~NFP_NET_CFG_CTRL_PROMISC;
	}

2899
	if (new_ctrl == nn->dp.ctrl)
2900 2901 2902
		return;

	nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
2903
	nfp_net_reconfig_post(nn, NFP_NET_CFG_UPDATE_GEN);
2904

2905
	nn->dp.ctrl = new_ctrl;
2906 2907
}

2908 2909 2910 2911 2912 2913
static void nfp_net_rss_init_itbl(struct nfp_net *nn)
{
	int i;

	for (i = 0; i < sizeof(nn->rss_itbl); i++)
		nn->rss_itbl[i] =
2914
			ethtool_rxfh_indir_default(i, nn->dp.num_rx_rings);
2915 2916
}

2917 2918 2919 2920 2921 2922
static void nfp_net_dp_swap(struct nfp_net *nn, struct nfp_net_dp *dp)
{
	struct nfp_net_dp new_dp = *dp;

	*dp = nn->dp;
	nn->dp = new_dp;
2923 2924

	nn->dp.netdev->mtu = new_dp.mtu;
2925 2926 2927

	if (!netif_is_rxfh_configured(nn->dp.netdev))
		nfp_net_rss_init_itbl(nn);
2928 2929
}

2930
static int nfp_net_dp_swap_enable(struct nfp_net *nn, struct nfp_net_dp *dp)
2931
{
2932
	unsigned int r;
2933
	int err;
2934

2935
	nfp_net_dp_swap(nn, dp);
2936

2937
	for (r = 0; r <	nn->max_r_vecs; r++)
2938
		nfp_net_vector_assign_rings(&nn->dp, &nn->r_vecs[r], r);
2939

2940
	err = netif_set_real_num_rx_queues(nn->dp.netdev, nn->dp.num_rx_rings);
2941 2942
	if (err)
		return err;
2943

2944 2945 2946
	if (nn->dp.netdev->real_num_tx_queues != nn->dp.num_stack_tx_rings) {
		err = netif_set_real_num_tx_queues(nn->dp.netdev,
						   nn->dp.num_stack_tx_rings);
2947 2948 2949 2950
		if (err)
			return err;
	}

2951
	return nfp_net_set_config_and_enable(nn);
2952
}
2953

2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973
struct nfp_net_dp *nfp_net_clone_dp(struct nfp_net *nn)
{
	struct nfp_net_dp *new;

	new = kmalloc(sizeof(*new), GFP_KERNEL);
	if (!new)
		return NULL;

	*new = nn->dp;

	/* Clear things which need to be recomputed */
	new->fl_bufsz = 0;
	new->tx_rings = NULL;
	new->rx_rings = NULL;
	new->num_r_vecs = 0;
	new->num_stack_tx_rings = 0;

	return new;
}

2974 2975 2976
static int
nfp_net_check_config(struct nfp_net *nn, struct nfp_net_dp *dp,
		     struct netlink_ext_ack *extack)
2977 2978
{
	/* XDP-enabled tests */
2979
	if (!dp->xdp_prog)
2980
		return 0;
2981
	if (dp->fl_bufsz > PAGE_SIZE) {
2982
		NL_SET_ERR_MSG_MOD(extack, "MTU too large w/ XDP enabled");
2983 2984
		return -EINVAL;
	}
2985
	if (dp->num_tx_rings > nn->max_tx_rings) {
2986
		NL_SET_ERR_MSG_MOD(extack, "Insufficient number of TX rings w/ XDP enabled");
2987 2988 2989 2990 2991 2992
		return -EINVAL;
	}

	return 0;
}

2993 2994
int nfp_net_ring_reconfig(struct nfp_net *nn, struct nfp_net_dp *dp,
			  struct netlink_ext_ack *extack)
2995
{
2996
	int r, err;
2997

2998
	dp->fl_bufsz = nfp_net_calc_fl_bufsz(dp);
2999

3000
	dp->num_stack_tx_rings = dp->num_tx_rings;
3001
	if (dp->xdp_prog)
3002
		dp->num_stack_tx_rings -= dp->num_rx_rings;
3003

3004
	dp->num_r_vecs = max(dp->num_rx_rings, dp->num_stack_tx_rings);
3005

3006
	err = nfp_net_check_config(nn, dp, extack);
3007
	if (err)
3008
		goto exit_free_dp;
3009

3010
	if (!netif_running(dp->netdev)) {
3011
		nfp_net_dp_swap(nn, dp);
3012 3013
		err = 0;
		goto exit_free_dp;
3014 3015 3016
	}

	/* Prepare new rings */
3017
	for (r = nn->dp.num_r_vecs; r < dp->num_r_vecs; r++) {
3018 3019
		err = nfp_net_prepare_vector(nn, &nn->r_vecs[r], r);
		if (err) {
3020
			dp->num_r_vecs = r;
3021 3022 3023
			goto err_cleanup_vecs;
		}
	}
3024 3025 3026 3027 3028 3029 3030 3031

	err = nfp_net_rx_rings_prepare(nn, dp);
	if (err)
		goto err_cleanup_vecs;

	err = nfp_net_tx_rings_prepare(nn, dp);
	if (err)
		goto err_free_rx;
3032 3033 3034 3035 3036

	/* Stop device, swap in new rings, try to start the firmware */
	nfp_net_close_stack(nn);
	nfp_net_clear_config_and_disable(nn);

3037
	err = nfp_net_dp_swap_enable(nn, dp);
3038
	if (err) {
3039
		int err2;
3040

3041
		nfp_net_clear_config_and_disable(nn);
3042

3043
		/* Try with old configuration and old rings */
3044
		err2 = nfp_net_dp_swap_enable(nn, dp);
3045
		if (err2)
3046
			nn_err(nn, "Can't restore ring config - FW communication failed (%d,%d)\n",
3047
			       err, err2);
3048
	}
3049
	for (r = dp->num_r_vecs - 1; r >= nn->dp.num_r_vecs; r--)
3050
		nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
3051

3052 3053
	nfp_net_rx_rings_free(dp);
	nfp_net_tx_rings_free(dp);
3054 3055

	nfp_net_open_stack(nn);
3056 3057
exit_free_dp:
	kfree(dp);
3058 3059

	return err;
3060 3061

err_free_rx:
3062
	nfp_net_rx_rings_free(dp);
3063
err_cleanup_vecs:
3064
	for (r = dp->num_r_vecs - 1; r >= nn->dp.num_r_vecs; r--)
3065
		nfp_net_cleanup_vector(nn, &nn->r_vecs[r]);
3066
	kfree(dp);
3067 3068 3069 3070 3071 3072
	return err;
}

static int nfp_net_change_mtu(struct net_device *netdev, int new_mtu)
{
	struct nfp_net *nn = netdev_priv(netdev);
3073
	struct nfp_net_dp *dp;
3074 3075
	int err;

3076
	err = nfp_app_check_mtu(nn->app, netdev, new_mtu);
3077 3078
	if (err)
		return err;
3079 3080 3081 3082

	dp = nfp_net_clone_dp(nn);
	if (!dp)
		return -ENOMEM;
3083

3084 3085
	dp->mtu = new_mtu;

3086
	return nfp_net_ring_reconfig(nn, dp, NULL);
3087 3088
}

P
Pablo Cascón 已提交
3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099
static int
nfp_net_vlan_rx_add_vid(struct net_device *netdev, __be16 proto, u16 vid)
{
	struct nfp_net *nn = netdev_priv(netdev);

	/* Priority tagged packets with vlan id 0 are processed by the
	 * NFP as untagged packets
	 */
	if (!vid)
		return 0;

3100 3101 3102
	nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_VLAN_FILTER_VID, vid);
	nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_VLAN_FILTER_PROTO,
		  ETH_P_8021Q);
P
Pablo Cascón 已提交
3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117

	return nfp_net_reconfig_mbox(nn, NFP_NET_CFG_MBOX_CMD_CTAG_FILTER_ADD);
}

static int
nfp_net_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto, u16 vid)
{
	struct nfp_net *nn = netdev_priv(netdev);

	/* Priority tagged packets with vlan id 0 are processed by the
	 * NFP as untagged packets
	 */
	if (!vid)
		return 0;

3118 3119 3120
	nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_VLAN_FILTER_VID, vid);
	nn_writew(nn, nn->tlv_caps.mbox_off + NFP_NET_CFG_VLAN_FILTER_PROTO,
		  ETH_P_8021Q);
P
Pablo Cascón 已提交
3121 3122 3123 3124

	return nfp_net_reconfig_mbox(nn, NFP_NET_CFG_MBOX_CMD_CTAG_FILTER_KILL);
}

3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139
#ifdef CONFIG_NET_POLL_CONTROLLER
static void nfp_net_netpoll(struct net_device *netdev)
{
	struct nfp_net *nn = netdev_priv(netdev);
	int i;

	/* nfp_net's NAPIs are statically allocated so even if there is a race
	 * with reconfig path this will simply try to schedule some disabled
	 * NAPI instances.
	 */
	for (i = 0; i < nn->dp.num_stack_tx_rings; i++)
		napi_schedule_irqoff(&nn->r_vecs[i].napi);
}
#endif

3140 3141
static void nfp_net_stat64(struct net_device *netdev,
			   struct rtnl_link_stats64 *stats)
3142 3143 3144 3145
{
	struct nfp_net *nn = netdev_priv(netdev);
	int r;

3146
	for (r = 0; r < nn->max_r_vecs; r++) {
3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182
		struct nfp_net_r_vector *r_vec = &nn->r_vecs[r];
		u64 data[3];
		unsigned int start;

		do {
			start = u64_stats_fetch_begin(&r_vec->rx_sync);
			data[0] = r_vec->rx_pkts;
			data[1] = r_vec->rx_bytes;
			data[2] = r_vec->rx_drops;
		} while (u64_stats_fetch_retry(&r_vec->rx_sync, start));
		stats->rx_packets += data[0];
		stats->rx_bytes += data[1];
		stats->rx_dropped += data[2];

		do {
			start = u64_stats_fetch_begin(&r_vec->tx_sync);
			data[0] = r_vec->tx_pkts;
			data[1] = r_vec->tx_bytes;
			data[2] = r_vec->tx_errors;
		} while (u64_stats_fetch_retry(&r_vec->tx_sync, start));
		stats->tx_packets += data[0];
		stats->tx_bytes += data[1];
		stats->tx_errors += data[2];
	}
}

static int nfp_net_set_features(struct net_device *netdev,
				netdev_features_t features)
{
	netdev_features_t changed = netdev->features ^ features;
	struct nfp_net *nn = netdev_priv(netdev);
	u32 new_ctrl;
	int err;

	/* Assume this is not called with features we have not advertised */

3183
	new_ctrl = nn->dp.ctrl;
3184 3185 3186

	if (changed & NETIF_F_RXCSUM) {
		if (features & NETIF_F_RXCSUM)
3187
			new_ctrl |= nn->cap & NFP_NET_CFG_CTRL_RXCSUM_ANY;
3188
		else
3189
			new_ctrl &= ~NFP_NET_CFG_CTRL_RXCSUM_ANY;
3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200
	}

	if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
		if (features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))
			new_ctrl |= NFP_NET_CFG_CTRL_TXCSUM;
		else
			new_ctrl &= ~NFP_NET_CFG_CTRL_TXCSUM;
	}

	if (changed & (NETIF_F_TSO | NETIF_F_TSO6)) {
		if (features & (NETIF_F_TSO | NETIF_F_TSO6))
E
Edwin Peer 已提交
3201 3202
			new_ctrl |= nn->cap & NFP_NET_CFG_CTRL_LSO2 ?:
					      NFP_NET_CFG_CTRL_LSO;
3203
		else
E
Edwin Peer 已提交
3204
			new_ctrl &= ~NFP_NET_CFG_CTRL_LSO_ANY;
3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220
	}

	if (changed & NETIF_F_HW_VLAN_CTAG_RX) {
		if (features & NETIF_F_HW_VLAN_CTAG_RX)
			new_ctrl |= NFP_NET_CFG_CTRL_RXVLAN;
		else
			new_ctrl &= ~NFP_NET_CFG_CTRL_RXVLAN;
	}

	if (changed & NETIF_F_HW_VLAN_CTAG_TX) {
		if (features & NETIF_F_HW_VLAN_CTAG_TX)
			new_ctrl |= NFP_NET_CFG_CTRL_TXVLAN;
		else
			new_ctrl &= ~NFP_NET_CFG_CTRL_TXVLAN;
	}

P
Pablo Cascón 已提交
3221 3222 3223 3224 3225 3226 3227
	if (changed & NETIF_F_HW_VLAN_CTAG_FILTER) {
		if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
			new_ctrl |= NFP_NET_CFG_CTRL_CTAG_FILTER;
		else
			new_ctrl &= ~NFP_NET_CFG_CTRL_CTAG_FILTER;
	}

3228 3229 3230 3231 3232 3233 3234
	if (changed & NETIF_F_SG) {
		if (features & NETIF_F_SG)
			new_ctrl |= NFP_NET_CFG_CTRL_GATHER;
		else
			new_ctrl &= ~NFP_NET_CFG_CTRL_GATHER;
	}

3235 3236 3237
	err = nfp_port_set_features(netdev, features);
	if (err)
		return err;
3238

3239 3240 3241
	nn_dbg(nn, "Feature change 0x%llx -> 0x%llx (changed=0x%llx)\n",
	       netdev->features, features, changed);

3242
	if (new_ctrl == nn->dp.ctrl)
3243 3244
		return 0;

3245
	nn_dbg(nn, "NIC ctrl: 0x%x -> 0x%x\n", nn->dp.ctrl, new_ctrl);
3246 3247 3248 3249 3250
	nn_writel(nn, NFP_NET_CFG_CTRL, new_ctrl);
	err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_GEN);
	if (err)
		return err;

3251
	nn->dp.ctrl = new_ctrl;
3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287

	return 0;
}

static netdev_features_t
nfp_net_features_check(struct sk_buff *skb, struct net_device *dev,
		       netdev_features_t features)
{
	u8 l4_hdr;

	/* We can't do TSO over double tagged packets (802.1AD) */
	features &= vlan_features_check(skb, features);

	if (!skb->encapsulation)
		return features;

	/* Ensure that inner L4 header offset fits into TX descriptor field */
	if (skb_is_gso(skb)) {
		u32 hdrlen;

		hdrlen = skb_inner_transport_header(skb) - skb->data +
			inner_tcp_hdrlen(skb);

		if (unlikely(hdrlen > NFP_NET_LSO_MAX_HDR_SZ))
			features &= ~NETIF_F_GSO_MASK;
	}

	/* VXLAN/GRE check */
	switch (vlan_get_protocol(skb)) {
	case htons(ETH_P_IP):
		l4_hdr = ip_hdr(skb)->protocol;
		break;
	case htons(ETH_P_IPV6):
		l4_hdr = ipv6_hdr(skb)->nexthdr;
		break;
	default:
3288
		return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
3289 3290 3291 3292 3293 3294 3295 3296
	}

	if (skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
	    skb->inner_protocol != htons(ETH_P_TEB) ||
	    (l4_hdr != IPPROTO_UDP && l4_hdr != IPPROTO_GRE) ||
	    (l4_hdr == IPPROTO_UDP &&
	     (skb_inner_mac_header(skb) - skb_transport_header(skb) !=
	      sizeof(struct udphdr) + sizeof(struct vxlanhdr))))
3297
		return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
3298 3299 3300 3301

	return features;
}

3302 3303 3304 3305 3306 3307 3308 3309 3310
static int
nfp_net_get_phys_port_name(struct net_device *netdev, char *name, size_t len)
{
	struct nfp_net *nn = netdev_priv(netdev);
	int n;

	if (nn->port)
		return nfp_port_get_phys_port_name(netdev, name, len);

3311
	if (nn->dp.is_vf || nn->vnic_no_name)
3312 3313
		return -EOPNOTSUPP;

3314
	n = snprintf(name, len, "n%d", nn->id);
3315 3316
	if (n >= len)
		return -EINVAL;
3317 3318 3319 3320

	return 0;
}

3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332
/**
 * nfp_net_set_vxlan_port() - set vxlan port in SW and reconfigure HW
 * @nn:   NFP Net device to reconfigure
 * @idx:  Index into the port table where new port should be written
 * @port: UDP port to configure (pass zero to remove VXLAN port)
 */
static void nfp_net_set_vxlan_port(struct nfp_net *nn, int idx, __be16 port)
{
	int i;

	nn->vxlan_ports[idx] = port;

3333
	if (!(nn->dp.ctrl & NFP_NET_CFG_CTRL_VXLAN))
3334 3335 3336 3337 3338 3339 3340 3341
		return;

	BUILD_BUG_ON(NFP_NET_N_VXLAN_PORTS & 1);
	for (i = 0; i < NFP_NET_N_VXLAN_PORTS; i += 2)
		nn_writel(nn, NFP_NET_CFG_VXLAN_PORT + i * sizeof(port),
			  be16_to_cpu(nn->vxlan_ports[i + 1]) << 16 |
			  be16_to_cpu(nn->vxlan_ports[i]));

3342
	nfp_net_reconfig_post(nn, NFP_NET_CFG_UPDATE_VXLAN);
3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368
}

/**
 * nfp_net_find_vxlan_idx() - find table entry of the port or a free one
 * @nn:   NFP Network structure
 * @port: UDP port to look for
 *
 * Return: if the port is already in the table -- it's position;
 *	   if the port is not in the table -- free position to use;
 *	   if the table is full -- -ENOSPC.
 */
static int nfp_net_find_vxlan_idx(struct nfp_net *nn, __be16 port)
{
	int i, free_idx = -ENOSPC;

	for (i = 0; i < NFP_NET_N_VXLAN_PORTS; i++) {
		if (nn->vxlan_ports[i] == port)
			return i;
		if (!nn->vxlan_usecnt[i])
			free_idx = i;
	}

	return free_idx;
}

static void nfp_net_add_vxlan_port(struct net_device *netdev,
3369
				   struct udp_tunnel_info *ti)
3370 3371 3372 3373
{
	struct nfp_net *nn = netdev_priv(netdev);
	int idx;

3374 3375 3376 3377
	if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
		return;

	idx = nfp_net_find_vxlan_idx(nn, ti->port);
3378 3379 3380 3381
	if (idx == -ENOSPC)
		return;

	if (!nn->vxlan_usecnt[idx]++)
3382
		nfp_net_set_vxlan_port(nn, idx, ti->port);
3383 3384 3385
}

static void nfp_net_del_vxlan_port(struct net_device *netdev,
3386
				   struct udp_tunnel_info *ti)
3387 3388 3389 3390
{
	struct nfp_net *nn = netdev_priv(netdev);
	int idx;

3391 3392 3393 3394
	if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
		return;

	idx = nfp_net_find_vxlan_idx(nn, ti->port);
3395
	if (idx == -ENOSPC || !nn->vxlan_usecnt[idx])
3396 3397 3398 3399 3400 3401
		return;

	if (!--nn->vxlan_usecnt[idx])
		nfp_net_set_vxlan_port(nn, idx, 0);
}

3402
static int nfp_net_xdp_setup_drv(struct nfp_net *nn, struct netdev_bpf *bpf)
3403
{
3404
	struct bpf_prog *prog = bpf->prog;
3405
	struct nfp_net_dp *dp;
3406 3407 3408 3409
	int err;

	if (!xdp_attachment_flags_ok(&nn->xdp, bpf))
		return -EBUSY;
3410

3411 3412
	if (!prog == !nn->dp.xdp_prog) {
		WRITE_ONCE(nn->dp.xdp_prog, prog);
3413
		xdp_attachment_setup(&nn->xdp, bpf);
3414 3415 3416
		return 0;
	}

3417 3418 3419 3420
	dp = nfp_net_clone_dp(nn);
	if (!dp)
		return -ENOMEM;

3421
	dp->xdp_prog = prog;
3422
	dp->num_tx_rings += prog ? nn->dp.num_rx_rings : -nn->dp.num_rx_rings;
3423
	dp->rx_dma_dir = prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE;
3424
	dp->rx_dma_off = prog ? XDP_PACKET_HEADROOM - nn->dp.rx_offset : 0;
3425 3426

	/* We need RX reconfig to remap the buffers (BIDIR vs FROM_DEV) */
3427 3428 3429 3430 3431 3432
	err = nfp_net_ring_reconfig(nn, dp, bpf->extack);
	if (err)
		return err;

	xdp_attachment_setup(&nn->xdp, bpf);
	return 0;
3433 3434
}

3435
static int nfp_net_xdp_setup_hw(struct nfp_net *nn, struct netdev_bpf *bpf)
3436 3437 3438
{
	int err;

3439
	if (!xdp_attachment_flags_ok(&nn->xdp_hw, bpf))
3440 3441
		return -EBUSY;

3442
	err = nfp_app_xdp_offload(nn->app, nn, bpf->prog, bpf->extack);
3443 3444 3445
	if (err)
		return err;

3446
	xdp_attachment_setup(&nn->xdp_hw, bpf);
3447 3448 3449
	return 0;
}

3450
static int nfp_net_xdp(struct net_device *netdev, struct netdev_bpf *xdp)
3451 3452 3453 3454 3455
{
	struct nfp_net *nn = netdev_priv(netdev);

	switch (xdp->command) {
	case XDP_SETUP_PROG:
3456
		return nfp_net_xdp_setup_drv(nn, xdp);
3457
	case XDP_SETUP_PROG_HW:
3458
		return nfp_net_xdp_setup_hw(nn, xdp);
3459
	case XDP_QUERY_PROG:
3460 3461
		return xdp_attachment_query(&nn->xdp, xdp);
	case XDP_QUERY_PROG_HW:
3462
		return xdp_attachment_query(&nn->xdp_hw, xdp);
3463
	default:
3464
		return nfp_app_bpf(nn->app, nn, xdp);
3465 3466 3467
	}
}

3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488
static int nfp_net_set_mac_address(struct net_device *netdev, void *addr)
{
	struct nfp_net *nn = netdev_priv(netdev);
	struct sockaddr *saddr = addr;
	int err;

	err = eth_prepare_mac_addr_change(netdev, addr);
	if (err)
		return err;

	nfp_net_write_mac_addr(nn, saddr->sa_data);

	err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_MACADDR);
	if (err)
		return err;

	eth_commit_mac_addr_change(netdev, addr);

	return 0;
}

J
Jakub Kicinski 已提交
3489
const struct net_device_ops nfp_net_netdev_ops = {
3490 3491
	.ndo_init		= nfp_app_ndo_init,
	.ndo_uninit		= nfp_app_ndo_uninit,
3492 3493 3494 3495
	.ndo_open		= nfp_net_netdev_open,
	.ndo_stop		= nfp_net_netdev_close,
	.ndo_start_xmit		= nfp_net_tx,
	.ndo_get_stats64	= nfp_net_stat64,
P
Pablo Cascón 已提交
3496 3497
	.ndo_vlan_rx_add_vid	= nfp_net_vlan_rx_add_vid,
	.ndo_vlan_rx_kill_vid	= nfp_net_vlan_rx_kill_vid,
3498 3499 3500
#ifdef CONFIG_NET_POLL_CONTROLLER
	.ndo_poll_controller	= nfp_net_netpoll,
#endif
3501 3502 3503 3504 3505
	.ndo_set_vf_mac         = nfp_app_set_vf_mac,
	.ndo_set_vf_vlan        = nfp_app_set_vf_vlan,
	.ndo_set_vf_spoofchk    = nfp_app_set_vf_spoofchk,
	.ndo_get_vf_config	= nfp_app_get_vf_config,
	.ndo_set_vf_link_state  = nfp_app_set_vf_link_state,
3506
	.ndo_setup_tc		= nfp_port_setup_tc,
3507 3508 3509
	.ndo_tx_timeout		= nfp_net_tx_timeout,
	.ndo_set_rx_mode	= nfp_net_set_rx_mode,
	.ndo_change_mtu		= nfp_net_change_mtu,
3510
	.ndo_set_mac_address	= nfp_net_set_mac_address,
3511 3512
	.ndo_set_features	= nfp_net_set_features,
	.ndo_features_check	= nfp_net_features_check,
3513
	.ndo_get_phys_port_name	= nfp_net_get_phys_port_name,
3514 3515
	.ndo_udp_tunnel_add	= nfp_net_add_vxlan_port,
	.ndo_udp_tunnel_del	= nfp_net_del_vxlan_port,
3516
	.ndo_bpf		= nfp_net_xdp,
3517 3518 3519 3520 3521 3522 3523 3524
};

/**
 * nfp_net_info() - Print general info about the NIC
 * @nn:      NFP Net device to reconfigure
 */
void nfp_net_info(struct nfp_net *nn)
{
J
Jakub Kicinski 已提交
3525
	nn_info(nn, "Netronome NFP-6xxx %sNetdev: TxQs=%d/%d RxQs=%d/%d\n",
3526 3527 3528
		nn->dp.is_vf ? "VF " : "",
		nn->dp.num_tx_rings, nn->max_tx_rings,
		nn->dp.num_rx_rings, nn->max_rx_rings);
3529 3530 3531 3532
	nn_info(nn, "VER: %d.%d.%d.%d, Maximum supported MTU: %d\n",
		nn->fw_ver.resv, nn->fw_ver.class,
		nn->fw_ver.major, nn->fw_ver.minor,
		nn->max_mtu);
P
Pablo Cascón 已提交
3533
	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\n",
3534 3535 3536 3537 3538 3539 3540 3541 3542 3543
		nn->cap,
		nn->cap & NFP_NET_CFG_CTRL_PROMISC  ? "PROMISC "  : "",
		nn->cap & NFP_NET_CFG_CTRL_L2BC     ? "L2BCFILT " : "",
		nn->cap & NFP_NET_CFG_CTRL_L2MC     ? "L2MCFILT " : "",
		nn->cap & NFP_NET_CFG_CTRL_RXCSUM   ? "RXCSUM "   : "",
		nn->cap & NFP_NET_CFG_CTRL_TXCSUM   ? "TXCSUM "   : "",
		nn->cap & NFP_NET_CFG_CTRL_RXVLAN   ? "RXVLAN "   : "",
		nn->cap & NFP_NET_CFG_CTRL_TXVLAN   ? "TXVLAN "   : "",
		nn->cap & NFP_NET_CFG_CTRL_SCATTER  ? "SCATTER "  : "",
		nn->cap & NFP_NET_CFG_CTRL_GATHER   ? "GATHER "   : "",
E
Edwin Peer 已提交
3544 3545
		nn->cap & NFP_NET_CFG_CTRL_LSO      ? "TSO1 "     : "",
		nn->cap & NFP_NET_CFG_CTRL_LSO2     ? "TSO2 "     : "",
3546 3547
		nn->cap & NFP_NET_CFG_CTRL_RSS      ? "RSS1 "     : "",
		nn->cap & NFP_NET_CFG_CTRL_RSS2     ? "RSS2 "     : "",
P
Pablo Cascón 已提交
3548
		nn->cap & NFP_NET_CFG_CTRL_CTAG_FILTER ? "CTAG_FILTER " : "",
3549 3550 3551 3552
		nn->cap & NFP_NET_CFG_CTRL_L2SWITCH ? "L2SWITCH " : "",
		nn->cap & NFP_NET_CFG_CTRL_MSIXAUTO ? "AUTOMASK " : "",
		nn->cap & NFP_NET_CFG_CTRL_IRQMOD   ? "IRQMOD "   : "",
		nn->cap & NFP_NET_CFG_CTRL_VXLAN    ? "VXLAN "    : "",
3553
		nn->cap & NFP_NET_CFG_CTRL_NVGRE    ? "NVGRE "	  : "",
3554
		nn->cap & NFP_NET_CFG_CTRL_CSUM_COMPLETE ?
3555
						      "RXCSUM_COMPLETE " : "",
3556 3557
		nn->cap & NFP_NET_CFG_CTRL_LIVE_ADDR ? "LIVE_ADDR " : "",
		nfp_app_extra_cap(nn->app, nn));
3558 3559 3560
}

/**
3561
 * nfp_net_alloc() - Allocate netdev and related structure
3562
 * @pdev:         PCI device
3563
 * @needs_netdev: Whether to allocate a netdev for this vNIC
3564 3565 3566 3567
 * @max_tx_rings: Maximum number of TX rings supported by device
 * @max_rx_rings: Maximum number of RX rings supported by device
 *
 * This function allocates a netdev device and fills in the initial
3568 3569
 * part of the @struct nfp_net structure.  In case of control device
 * nfp_net structure is allocated without the netdev.
3570 3571 3572
 *
 * Return: NFP Net device structure, or ERR_PTR on error.
 */
3573
struct nfp_net *nfp_net_alloc(struct pci_dev *pdev, bool needs_netdev,
3574 3575
			      unsigned int max_tx_rings,
			      unsigned int max_rx_rings)
3576 3577 3578
{
	struct nfp_net *nn;

3579 3580
	if (needs_netdev) {
		struct net_device *netdev;
3581

3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594
		netdev = alloc_etherdev_mqs(sizeof(struct nfp_net),
					    max_tx_rings, max_rx_rings);
		if (!netdev)
			return ERR_PTR(-ENOMEM);

		SET_NETDEV_DEV(netdev, &pdev->dev);
		nn = netdev_priv(netdev);
		nn->dp.netdev = netdev;
	} else {
		nn = vzalloc(sizeof(*nn));
		if (!nn)
			return ERR_PTR(-ENOMEM);
	}
3595

3596
	nn->dp.dev = &pdev->dev;
3597 3598 3599 3600 3601
	nn->pdev = pdev;

	nn->max_tx_rings = max_tx_rings;
	nn->max_rx_rings = max_rx_rings;

3602 3603 3604
	nn->dp.num_tx_rings = min_t(unsigned int,
				    max_tx_rings, num_online_cpus());
	nn->dp.num_rx_rings = min_t(unsigned int, max_rx_rings,
3605
				 netif_get_num_default_rss_queues());
3606

3607 3608 3609
	nn->dp.num_r_vecs = max(nn->dp.num_tx_rings, nn->dp.num_rx_rings);
	nn->dp.num_r_vecs = min_t(unsigned int,
				  nn->dp.num_r_vecs, num_online_cpus());
J
Jakub Kicinski 已提交
3610

3611 3612
	nn->dp.txd_cnt = NFP_NET_TX_DESCS_DEFAULT;
	nn->dp.rxd_cnt = NFP_NET_RX_DESCS_DEFAULT;
3613 3614 3615 3616

	spin_lock_init(&nn->reconfig_lock);
	spin_lock_init(&nn->link_status_lock);

3617
	timer_setup(&nn->reconfig_timer, nfp_net_reconfig_timer, 0);
3618

3619 3620 3621 3622
	return nn;
}

/**
3623
 * nfp_net_free() - Undo what @nfp_net_alloc() did
3624 3625
 * @nn:      NFP Net device to reconfigure
 */
3626
void nfp_net_free(struct nfp_net *nn)
3627
{
3628 3629 3630 3631
	if (nn->dp.netdev)
		free_netdev(nn->dp.netdev);
	else
		vfree(nn);
3632 3633
}

3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654
/**
 * nfp_net_rss_key_sz() - Get current size of the RSS key
 * @nn:		NFP Net device instance
 *
 * Return: size of the RSS key for currently selected hash function.
 */
unsigned int nfp_net_rss_key_sz(struct nfp_net *nn)
{
	switch (nn->rss_hfunc) {
	case ETH_RSS_HASH_TOP:
		return NFP_NET_CFG_RSS_KEY_SZ;
	case ETH_RSS_HASH_XOR:
		return 0;
	case ETH_RSS_HASH_CRC32:
		return 4;
	}

	nn_warn(nn, "Unknown hash function: %u\n", nn->rss_hfunc);
	return 0;
}

3655 3656 3657 3658 3659 3660
/**
 * nfp_net_rss_init() - Set the initial RSS parameters
 * @nn:	     NFP Net device to reconfigure
 */
static void nfp_net_rss_init(struct nfp_net *nn)
{
3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672
	unsigned long func_bit, rss_cap_hfunc;
	u32 reg;

	/* Read the RSS function capability and select first supported func */
	reg = nn_readl(nn, NFP_NET_CFG_RSS_CAP);
	rss_cap_hfunc =	FIELD_GET(NFP_NET_CFG_RSS_CAP_HFUNC, reg);
	if (!rss_cap_hfunc)
		rss_cap_hfunc =	FIELD_GET(NFP_NET_CFG_RSS_CAP_HFUNC,
					  NFP_NET_CFG_RSS_TOEPLITZ);

	func_bit = find_first_bit(&rss_cap_hfunc, NFP_NET_CFG_RSS_HFUNCS);
	if (func_bit == NFP_NET_CFG_RSS_HFUNCS) {
3673
		dev_warn(nn->dp.dev,
3674 3675 3676 3677 3678 3679
			 "Bad RSS config, defaulting to Toeplitz hash\n");
		func_bit = ETH_RSS_HASH_TOP_BIT;
	}
	nn->rss_hfunc = 1 << func_bit;

	netdev_rss_key_fill(nn->rss_key, nfp_net_rss_key_sz(nn));
3680

3681
	nfp_net_rss_init_itbl(nn);
3682 3683 3684 3685

	/* Enable IPv4/IPv6 TCP by default */
	nn->rss_cfg = NFP_NET_CFG_RSS_IPV4_TCP |
		      NFP_NET_CFG_RSS_IPV6_TCP |
3686
		      FIELD_PREP(NFP_NET_CFG_RSS_HFUNC, nn->rss_hfunc) |
3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701
		      NFP_NET_CFG_RSS_MASK;
}

/**
 * nfp_net_irqmod_init() - Set the initial IRQ moderation parameters
 * @nn:	     NFP Net device to reconfigure
 */
static void nfp_net_irqmod_init(struct nfp_net *nn)
{
	nn->rx_coalesce_usecs      = 50;
	nn->rx_coalesce_max_frames = 64;
	nn->tx_coalesce_usecs      = 50;
	nn->tx_coalesce_max_frames = 64;
}

3702
static void nfp_net_netdev_init(struct nfp_net *nn)
3703
{
3704
	struct net_device *netdev = nn->dp.netdev;
3705

3706
	nfp_net_write_mac_addr(nn, nn->dp.netdev->dev_addr);
3707

3708
	netdev->mtu = nn->dp.mtu;
3709 3710 3711 3712 3713 3714 3715

	/* Advertise/enable offloads based on capabilities
	 *
	 * Note: netdev->features show the currently enabled features
	 * and netdev->hw_features advertises which features are
	 * supported.  By default we enable most features.
	 */
3716 3717 3718
	if (nn->cap & NFP_NET_CFG_CTRL_LIVE_ADDR)
		netdev->priv_flags |= IFF_LIVE_ADDR_CHANGE;

3719
	netdev->hw_features = NETIF_F_HIGHDMA;
3720
	if (nn->cap & NFP_NET_CFG_CTRL_RXCSUM_ANY) {
3721
		netdev->hw_features |= NETIF_F_RXCSUM;
3722
		nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_RXCSUM_ANY;
3723 3724 3725
	}
	if (nn->cap & NFP_NET_CFG_CTRL_TXCSUM) {
		netdev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
3726
		nn->dp.ctrl |= NFP_NET_CFG_CTRL_TXCSUM;
3727 3728 3729
	}
	if (nn->cap & NFP_NET_CFG_CTRL_GATHER) {
		netdev->hw_features |= NETIF_F_SG;
3730
		nn->dp.ctrl |= NFP_NET_CFG_CTRL_GATHER;
3731
	}
E
Edwin Peer 已提交
3732 3733
	if ((nn->cap & NFP_NET_CFG_CTRL_LSO && nn->fw_ver.major > 2) ||
	    nn->cap & NFP_NET_CFG_CTRL_LSO2) {
3734
		netdev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
E
Edwin Peer 已提交
3735 3736
		nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_LSO2 ?:
					 NFP_NET_CFG_CTRL_LSO;
3737
	}
3738
	if (nn->cap & NFP_NET_CFG_CTRL_RSS_ANY)
3739 3740 3741 3742 3743 3744
		netdev->hw_features |= NETIF_F_RXHASH;
	if (nn->cap & NFP_NET_CFG_CTRL_VXLAN &&
	    nn->cap & NFP_NET_CFG_CTRL_NVGRE) {
		if (nn->cap & NFP_NET_CFG_CTRL_LSO)
			netdev->hw_features |= NETIF_F_GSO_GRE |
					       NETIF_F_GSO_UDP_TUNNEL;
3745
		nn->dp.ctrl |= NFP_NET_CFG_CTRL_VXLAN | NFP_NET_CFG_CTRL_NVGRE;
3746 3747 3748 3749 3750 3751 3752 3753

		netdev->hw_enc_features = netdev->hw_features;
	}

	netdev->vlan_features = netdev->hw_features;

	if (nn->cap & NFP_NET_CFG_CTRL_RXVLAN) {
		netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
3754
		nn->dp.ctrl |= NFP_NET_CFG_CTRL_RXVLAN;
3755 3756
	}
	if (nn->cap & NFP_NET_CFG_CTRL_TXVLAN) {
E
Edwin Peer 已提交
3757 3758 3759 3760 3761 3762
		if (nn->cap & NFP_NET_CFG_CTRL_LSO2) {
			nn_warn(nn, "Device advertises both TSO2 and TXVLAN. Refusing to enable TXVLAN.\n");
		} else {
			netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
			nn->dp.ctrl |= NFP_NET_CFG_CTRL_TXVLAN;
		}
3763
	}
P
Pablo Cascón 已提交
3764 3765 3766 3767
	if (nn->cap & NFP_NET_CFG_CTRL_CTAG_FILTER) {
		netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
		nn->dp.ctrl |= NFP_NET_CFG_CTRL_CTAG_FILTER;
	}
3768 3769 3770

	netdev->features = netdev->hw_features;

3771
	if (nfp_app_has_tc(nn->app) && nn->port)
3772 3773
		netdev->hw_features |= NETIF_F_HW_TC;

3774 3775
	/* Advertise but disable TSO by default. */
	netdev->features &= ~(NETIF_F_TSO | NETIF_F_TSO6);
E
Edwin Peer 已提交
3776
	nn->dp.ctrl &= ~NFP_NET_CFG_CTRL_LSO_ANY;
3777

3778 3779 3780 3781
	/* Finalise the netdev setup */
	netdev->netdev_ops = &nfp_net_netdev_ops;
	netdev->watchdog_timeo = msecs_to_jiffies(5 * 1000);

S
Simon Horman 已提交
3782 3783
	SWITCHDEV_SET_OPS(netdev, &nfp_port_switchdev_ops);

3784 3785 3786 3787
	/* MTU range: 68 - hw-specific max */
	netdev->min_mtu = ETH_MIN_MTU;
	netdev->max_mtu = nn->max_mtu;

3788 3789
	netdev->gso_max_segs = NFP_NET_LSO_MAX_SEGS;

3790 3791 3792 3793 3794
	netif_carrier_off(netdev);

	nfp_net_set_ethtool_ops(netdev);
}

3795
static int nfp_net_read_caps(struct nfp_net *nn)
3796 3797 3798 3799 3800
{
	/* Get some of the read-only fields from the BAR */
	nn->cap = nn_readl(nn, NFP_NET_CFG_CAP);
	nn->max_mtu = nn_readl(nn, NFP_NET_CFG_MAX_MTU);

3801 3802 3803 3804
	/* ABI 4.x and ctrl vNIC always use chained metadata, in other cases
	 * we allow use of non-chained metadata if RSS(v1) is the only
	 * advertised capability requiring metadata.
	 */
3805
	nn->dp.chained_metadata_format = nn->fw_ver.major == 4 ||
J
Jakub Kicinski 已提交
3806
					 !nn->dp.netdev ||
3807
					 !(nn->cap & NFP_NET_CFG_CTRL_RSS) ||
3808
					 nn->cap & NFP_NET_CFG_CTRL_CHAIN_META;
3809 3810 3811
	/* RSS(v1) uses non-chained metadata format, except in ABI 4.x where
	 * it has the same meaning as RSSv2.
	 */
3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828
	if (nn->dp.chained_metadata_format && nn->fw_ver.major != 4)
		nn->cap &= ~NFP_NET_CFG_CTRL_RSS;

	/* Determine RX packet/metadata boundary offset */
	if (nn->fw_ver.major >= 2) {
		u32 reg;

		reg = nn_readl(nn, NFP_NET_CFG_RX_OFFSET);
		if (reg > NFP_NET_MAX_PREPEND) {
			nn_err(nn, "Invalid rx offset: %d\n", reg);
			return -EINVAL;
		}
		nn->dp.rx_offset = reg;
	} else {
		nn->dp.rx_offset = NFP_NET_RX_OFFSET;
	}

3829 3830 3831 3832
	/* For control vNICs mask out the capabilities app doesn't want. */
	if (!nn->dp.netdev)
		nn->cap &= nn->app->type->ctrl_cap_mask;

3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851
	return 0;
}

/**
 * nfp_net_init() - Initialise/finalise the nfp_net structure
 * @nn:		NFP Net device structure
 *
 * Return: 0 on success or negative errno on error.
 */
int nfp_net_init(struct nfp_net *nn)
{
	int err;

	nn->dp.rx_dma_dir = DMA_FROM_DEVICE;

	err = nfp_net_read_caps(nn);
	if (err)
		return err;

3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864
	/* Set default MTU and Freelist buffer size */
	if (nn->max_mtu < NFP_NET_DEFAULT_MTU)
		nn->dp.mtu = nn->max_mtu;
	else
		nn->dp.mtu = NFP_NET_DEFAULT_MTU;
	nn->dp.fl_bufsz = nfp_net_calc_fl_bufsz(&nn->dp);

	if (nn->cap & NFP_NET_CFG_CTRL_RSS_ANY) {
		nfp_net_rss_init(nn);
		nn->dp.ctrl |= nn->cap & NFP_NET_CFG_CTRL_RSS2 ?:
					 NFP_NET_CFG_CTRL_RSS;
	}

3865 3866
	/* Allow L2 Broadcast and Multicast through by default, if supported */
	if (nn->cap & NFP_NET_CFG_CTRL_L2BC)
3867
		nn->dp.ctrl |= NFP_NET_CFG_CTRL_L2BC;
3868 3869 3870 3871

	/* Allow IRQ moderation, if supported */
	if (nn->cap & NFP_NET_CFG_CTRL_IRQMOD) {
		nfp_net_irqmod_init(nn);
3872
		nn->dp.ctrl |= NFP_NET_CFG_CTRL_IRQMOD;
3873 3874
	}

3875 3876 3877 3878 3879
	err = nfp_net_tlv_caps_parse(&nn->pdev->dev, nn->dp.ctrl_bar,
				     &nn->tlv_caps);
	if (err)
		return err;

3880 3881 3882
	if (nn->dp.netdev)
		nfp_net_netdev_init(nn);

3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894
	/* Stash the re-configuration queue away.  First odd queue in TX Bar */
	nn->qcp_cfg = nn->tx_bar + NFP_QCP_QUEUE_ADDR_SZ;

	/* Make sure the FW knows the netdev is supposed to be disabled here */
	nn_writel(nn, NFP_NET_CFG_CTRL, 0);
	nn_writeq(nn, NFP_NET_CFG_TXRS_ENABLE, 0);
	nn_writeq(nn, NFP_NET_CFG_RXRS_ENABLE, 0);
	err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_RING |
				   NFP_NET_CFG_UPDATE_GEN);
	if (err)
		return err;

3895
	nfp_net_vecs_init(nn);
3896

3897 3898 3899
	if (!nn->dp.netdev)
		return 0;
	return register_netdev(nn->dp.netdev);
3900 3901 3902
}

/**
3903 3904
 * nfp_net_clean() - Undo what nfp_net_init() did.
 * @nn:		NFP Net device structure
3905
 */
3906
void nfp_net_clean(struct nfp_net *nn)
3907
{
3908 3909 3910
	if (!nn->dp.netdev)
		return;

3911
	unregister_netdev(nn->dp.netdev);
3912
}