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