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