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