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