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