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