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