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