1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2019, Intel Corporation. */ 3 4 #include <linux/bpf_trace.h> 5 #include <net/xdp_sock_drv.h> 6 #include <net/xdp.h> 7 #include "ice.h" 8 #include "ice_base.h" 9 #include "ice_type.h" 10 #include "ice_xsk.h" 11 #include "ice_txrx.h" 12 #include "ice_txrx_lib.h" 13 #include "ice_lib.h" 14 15 static struct xdp_buff **ice_xdp_buf(struct ice_rx_ring *rx_ring, u32 idx) 16 { 17 return &rx_ring->xdp_buf[idx]; 18 } 19 20 /** 21 * ice_qp_reset_stats - Resets all stats for rings of given index 22 * @vsi: VSI that contains rings of interest 23 * @q_idx: ring index in array 24 */ 25 static void ice_qp_reset_stats(struct ice_vsi *vsi, u16 q_idx) 26 { 27 struct ice_vsi_stats *vsi_stat; 28 struct ice_pf *pf; 29 30 pf = vsi->back; 31 if (!pf->vsi_stats) 32 return; 33 34 vsi_stat = pf->vsi_stats[vsi->idx]; 35 if (!vsi_stat) 36 return; 37 38 memset(&vsi_stat->rx_ring_stats[q_idx]->rx_stats, 0, 39 sizeof(vsi_stat->rx_ring_stats[q_idx]->rx_stats)); 40 memset(&vsi_stat->tx_ring_stats[q_idx]->stats, 0, 41 sizeof(vsi_stat->tx_ring_stats[q_idx]->stats)); 42 if (ice_is_xdp_ena_vsi(vsi)) 43 memset(&vsi->xdp_rings[q_idx]->ring_stats->stats, 0, 44 sizeof(vsi->xdp_rings[q_idx]->ring_stats->stats)); 45 } 46 47 /** 48 * ice_qp_clean_rings - Cleans all the rings of a given index 49 * @vsi: VSI that contains rings of interest 50 * @q_idx: ring index in array 51 */ 52 static void ice_qp_clean_rings(struct ice_vsi *vsi, u16 q_idx) 53 { 54 ice_clean_tx_ring(vsi->tx_rings[q_idx]); 55 if (ice_is_xdp_ena_vsi(vsi)) 56 ice_clean_tx_ring(vsi->xdp_rings[q_idx]); 57 ice_clean_rx_ring(vsi->rx_rings[q_idx]); 58 } 59 60 /** 61 * ice_qvec_toggle_napi - Enables/disables NAPI for a given q_vector 62 * @vsi: VSI that has netdev 63 * @q_vector: q_vector that has NAPI context 64 * @enable: true for enable, false for disable 65 */ 66 static void 67 ice_qvec_toggle_napi(struct ice_vsi *vsi, struct ice_q_vector *q_vector, 68 bool enable) 69 { 70 if (!vsi->netdev || !q_vector) 71 return; 72 73 if (enable) 74 napi_enable(&q_vector->napi); 75 else 76 napi_disable(&q_vector->napi); 77 } 78 79 /** 80 * ice_qvec_dis_irq - Mask off queue interrupt generation on given ring 81 * @vsi: the VSI that contains queue vector being un-configured 82 * @rx_ring: Rx ring that will have its IRQ disabled 83 * @q_vector: queue vector 84 */ 85 static void 86 ice_qvec_dis_irq(struct ice_vsi *vsi, struct ice_rx_ring *rx_ring, 87 struct ice_q_vector *q_vector) 88 { 89 struct ice_pf *pf = vsi->back; 90 struct ice_hw *hw = &pf->hw; 91 u16 reg; 92 u32 val; 93 94 /* QINT_TQCTL is being cleared in ice_vsi_stop_tx_ring, so handle 95 * here only QINT_RQCTL 96 */ 97 reg = rx_ring->reg_idx; 98 val = rd32(hw, QINT_RQCTL(reg)); 99 val &= ~QINT_RQCTL_CAUSE_ENA_M; 100 wr32(hw, QINT_RQCTL(reg), val); 101 102 if (q_vector) { 103 wr32(hw, GLINT_DYN_CTL(q_vector->reg_idx), 0); 104 ice_flush(hw); 105 synchronize_irq(q_vector->irq.virq); 106 } 107 } 108 109 /** 110 * ice_qvec_cfg_msix - Enable IRQ for given queue vector 111 * @vsi: the VSI that contains queue vector 112 * @q_vector: queue vector 113 * @qid: queue index 114 */ 115 static void 116 ice_qvec_cfg_msix(struct ice_vsi *vsi, struct ice_q_vector *q_vector, u16 qid) 117 { 118 u16 reg_idx = q_vector->reg_idx; 119 struct ice_pf *pf = vsi->back; 120 struct ice_hw *hw = &pf->hw; 121 int q, _qid = qid; 122 123 ice_cfg_itr(hw, q_vector); 124 125 for (q = 0; q < q_vector->num_ring_tx; q++) { 126 ice_cfg_txq_interrupt(vsi, _qid, reg_idx, q_vector->tx.itr_idx); 127 _qid++; 128 } 129 130 _qid = qid; 131 132 for (q = 0; q < q_vector->num_ring_rx; q++) { 133 ice_cfg_rxq_interrupt(vsi, _qid, reg_idx, q_vector->rx.itr_idx); 134 _qid++; 135 } 136 137 ice_flush(hw); 138 } 139 140 /** 141 * ice_qvec_ena_irq - Enable IRQ for given queue vector 142 * @vsi: the VSI that contains queue vector 143 * @q_vector: queue vector 144 */ 145 static void ice_qvec_ena_irq(struct ice_vsi *vsi, struct ice_q_vector *q_vector) 146 { 147 struct ice_pf *pf = vsi->back; 148 struct ice_hw *hw = &pf->hw; 149 150 ice_irq_dynamic_ena(hw, vsi, q_vector); 151 152 ice_flush(hw); 153 } 154 155 /** 156 * ice_qp_dis - Disables a queue pair 157 * @vsi: VSI of interest 158 * @q_idx: ring index in array 159 * 160 * Returns 0 on success, negative on failure. 161 */ 162 static int ice_qp_dis(struct ice_vsi *vsi, u16 q_idx) 163 { 164 struct ice_txq_meta txq_meta = { }; 165 struct ice_q_vector *q_vector; 166 struct ice_tx_ring *tx_ring; 167 struct ice_rx_ring *rx_ring; 168 int timeout = 50; 169 int fail = 0; 170 int err; 171 172 if (q_idx >= vsi->num_rxq || q_idx >= vsi->num_txq) 173 return -EINVAL; 174 175 tx_ring = vsi->tx_rings[q_idx]; 176 rx_ring = vsi->rx_rings[q_idx]; 177 q_vector = rx_ring->q_vector; 178 179 while (test_and_set_bit(ICE_CFG_BUSY, vsi->state)) { 180 timeout--; 181 if (!timeout) 182 return -EBUSY; 183 usleep_range(1000, 2000); 184 } 185 186 synchronize_net(); 187 netif_carrier_off(vsi->netdev); 188 netif_tx_stop_queue(netdev_get_tx_queue(vsi->netdev, q_idx)); 189 190 ice_qvec_dis_irq(vsi, rx_ring, q_vector); 191 ice_qvec_toggle_napi(vsi, q_vector, false); 192 193 ice_fill_txq_meta(vsi, tx_ring, &txq_meta); 194 err = ice_vsi_stop_tx_ring(vsi, ICE_NO_RESET, 0, tx_ring, &txq_meta); 195 if (!fail) 196 fail = err; 197 if (ice_is_xdp_ena_vsi(vsi)) { 198 struct ice_tx_ring *xdp_ring = vsi->xdp_rings[q_idx]; 199 200 memset(&txq_meta, 0, sizeof(txq_meta)); 201 ice_fill_txq_meta(vsi, xdp_ring, &txq_meta); 202 err = ice_vsi_stop_tx_ring(vsi, ICE_NO_RESET, 0, xdp_ring, 203 &txq_meta); 204 if (!fail) 205 fail = err; 206 } 207 208 ice_vsi_ctrl_one_rx_ring(vsi, false, q_idx, false); 209 ice_qp_clean_rings(vsi, q_idx); 210 ice_qp_reset_stats(vsi, q_idx); 211 212 return fail; 213 } 214 215 /** 216 * ice_qp_ena - Enables a queue pair 217 * @vsi: VSI of interest 218 * @q_idx: ring index in array 219 * 220 * Returns 0 on success, negative on failure. 221 */ 222 static int ice_qp_ena(struct ice_vsi *vsi, u16 q_idx) 223 { 224 struct ice_q_vector *q_vector; 225 int fail = 0; 226 bool link_up; 227 int err; 228 229 err = ice_vsi_cfg_single_txq(vsi, vsi->tx_rings, q_idx); 230 if (!fail) 231 fail = err; 232 233 if (ice_is_xdp_ena_vsi(vsi)) { 234 struct ice_tx_ring *xdp_ring = vsi->xdp_rings[q_idx]; 235 236 err = ice_vsi_cfg_single_txq(vsi, vsi->xdp_rings, q_idx); 237 if (!fail) 238 fail = err; 239 ice_set_ring_xdp(xdp_ring); 240 ice_tx_xsk_pool(vsi, q_idx); 241 } 242 243 err = ice_vsi_cfg_single_rxq(vsi, q_idx); 244 if (!fail) 245 fail = err; 246 247 q_vector = vsi->rx_rings[q_idx]->q_vector; 248 ice_qvec_cfg_msix(vsi, q_vector, q_idx); 249 250 err = ice_vsi_ctrl_one_rx_ring(vsi, true, q_idx, true); 251 if (!fail) 252 fail = err; 253 254 ice_qvec_toggle_napi(vsi, q_vector, true); 255 ice_qvec_ena_irq(vsi, q_vector); 256 257 /* make sure NAPI sees updated ice_{t,x}_ring::xsk_pool */ 258 synchronize_net(); 259 ice_get_link_status(vsi->port_info, &link_up); 260 if (link_up) { 261 netif_tx_start_queue(netdev_get_tx_queue(vsi->netdev, q_idx)); 262 netif_carrier_on(vsi->netdev); 263 } 264 clear_bit(ICE_CFG_BUSY, vsi->state); 265 266 return fail; 267 } 268 269 /** 270 * ice_xsk_pool_disable - disable a buffer pool region 271 * @vsi: Current VSI 272 * @qid: queue ID 273 * 274 * Returns 0 on success, negative on failure 275 */ 276 static int ice_xsk_pool_disable(struct ice_vsi *vsi, u16 qid) 277 { 278 struct xsk_buff_pool *pool = xsk_get_pool_from_qid(vsi->netdev, qid); 279 280 if (!pool) 281 return -EINVAL; 282 283 xsk_pool_dma_unmap(pool, ICE_RX_DMA_ATTR); 284 285 return 0; 286 } 287 288 /** 289 * ice_xsk_pool_enable - enable a buffer pool region 290 * @vsi: Current VSI 291 * @pool: pointer to a requested buffer pool region 292 * @qid: queue ID 293 * 294 * Returns 0 on success, negative on failure 295 */ 296 static int 297 ice_xsk_pool_enable(struct ice_vsi *vsi, struct xsk_buff_pool *pool, u16 qid) 298 { 299 int err; 300 301 if (vsi->type != ICE_VSI_PF) 302 return -EINVAL; 303 304 if (qid >= vsi->netdev->real_num_rx_queues || 305 qid >= vsi->netdev->real_num_tx_queues) 306 return -EINVAL; 307 308 err = xsk_pool_dma_map(pool, ice_pf_to_dev(vsi->back), 309 ICE_RX_DMA_ATTR); 310 if (err) 311 return err; 312 313 return 0; 314 } 315 316 /** 317 * ice_realloc_rx_xdp_bufs - reallocate for either XSK or normal buffer 318 * @rx_ring: Rx ring 319 * @pool_present: is pool for XSK present 320 * 321 * Try allocating memory and return ENOMEM, if failed to allocate. 322 * If allocation was successful, substitute buffer with allocated one. 323 * Returns 0 on success, negative on failure 324 */ 325 static int 326 ice_realloc_rx_xdp_bufs(struct ice_rx_ring *rx_ring, bool pool_present) 327 { 328 size_t elem_size = pool_present ? sizeof(*rx_ring->xdp_buf) : 329 sizeof(*rx_ring->rx_buf); 330 void *sw_ring = kcalloc(rx_ring->count, elem_size, GFP_KERNEL); 331 332 if (!sw_ring) 333 return -ENOMEM; 334 335 if (pool_present) { 336 kfree(rx_ring->rx_buf); 337 rx_ring->rx_buf = NULL; 338 rx_ring->xdp_buf = sw_ring; 339 } else { 340 kfree(rx_ring->xdp_buf); 341 rx_ring->xdp_buf = NULL; 342 rx_ring->rx_buf = sw_ring; 343 } 344 345 return 0; 346 } 347 348 /** 349 * ice_realloc_zc_buf - reallocate XDP ZC queue pairs 350 * @vsi: Current VSI 351 * @zc: is zero copy set 352 * 353 * Reallocate buffer for rx_rings that might be used by XSK. 354 * XDP requires more memory, than rx_buf provides. 355 * Returns 0 on success, negative on failure 356 */ 357 int ice_realloc_zc_buf(struct ice_vsi *vsi, bool zc) 358 { 359 struct ice_rx_ring *rx_ring; 360 uint i; 361 362 ice_for_each_rxq(vsi, i) { 363 rx_ring = vsi->rx_rings[i]; 364 if (!rx_ring->xsk_pool) 365 continue; 366 367 if (ice_realloc_rx_xdp_bufs(rx_ring, zc)) 368 return -ENOMEM; 369 } 370 371 return 0; 372 } 373 374 /** 375 * ice_xsk_pool_setup - enable/disable a buffer pool region depending on its state 376 * @vsi: Current VSI 377 * @pool: buffer pool to enable/associate to a ring, NULL to disable 378 * @qid: queue ID 379 * 380 * Returns 0 on success, negative on failure 381 */ 382 int ice_xsk_pool_setup(struct ice_vsi *vsi, struct xsk_buff_pool *pool, u16 qid) 383 { 384 bool if_running, pool_present = !!pool; 385 int ret = 0, pool_failure = 0; 386 387 if (qid >= vsi->num_rxq || qid >= vsi->num_txq) { 388 netdev_err(vsi->netdev, "Please use queue id in scope of combined queues count\n"); 389 pool_failure = -EINVAL; 390 goto failure; 391 } 392 393 if_running = netif_running(vsi->netdev) && ice_is_xdp_ena_vsi(vsi); 394 395 if (if_running) { 396 struct ice_rx_ring *rx_ring = vsi->rx_rings[qid]; 397 398 ret = ice_qp_dis(vsi, qid); 399 if (ret) { 400 netdev_err(vsi->netdev, "ice_qp_dis error = %d\n", ret); 401 goto xsk_pool_if_up; 402 } 403 404 ret = ice_realloc_rx_xdp_bufs(rx_ring, pool_present); 405 if (ret) 406 goto xsk_pool_if_up; 407 } 408 409 pool_failure = pool_present ? ice_xsk_pool_enable(vsi, pool, qid) : 410 ice_xsk_pool_disable(vsi, qid); 411 412 xsk_pool_if_up: 413 if (if_running) { 414 ret = ice_qp_ena(vsi, qid); 415 if (!ret && pool_present) 416 napi_schedule(&vsi->rx_rings[qid]->xdp_ring->q_vector->napi); 417 else if (ret) 418 netdev_err(vsi->netdev, "ice_qp_ena error = %d\n", ret); 419 } 420 421 failure: 422 if (pool_failure) { 423 netdev_err(vsi->netdev, "Could not %sable buffer pool, error = %d\n", 424 pool_present ? "en" : "dis", pool_failure); 425 return pool_failure; 426 } 427 428 return ret; 429 } 430 431 /** 432 * ice_fill_rx_descs - pick buffers from XSK buffer pool and use it 433 * @pool: XSK Buffer pool to pull the buffers from 434 * @xdp: SW ring of xdp_buff that will hold the buffers 435 * @rx_desc: Pointer to Rx descriptors that will be filled 436 * @count: The number of buffers to allocate 437 * 438 * This function allocates a number of Rx buffers from the fill ring 439 * or the internal recycle mechanism and places them on the Rx ring. 440 * 441 * Note that ring wrap should be handled by caller of this function. 442 * 443 * Returns the amount of allocated Rx descriptors 444 */ 445 static u16 ice_fill_rx_descs(struct xsk_buff_pool *pool, struct xdp_buff **xdp, 446 union ice_32b_rx_flex_desc *rx_desc, u16 count) 447 { 448 dma_addr_t dma; 449 u16 buffs; 450 int i; 451 452 buffs = xsk_buff_alloc_batch(pool, xdp, count); 453 for (i = 0; i < buffs; i++) { 454 dma = xsk_buff_xdp_get_dma(*xdp); 455 rx_desc->read.pkt_addr = cpu_to_le64(dma); 456 rx_desc->wb.status_error0 = 0; 457 458 /* Put private info that changes on a per-packet basis 459 * into xdp_buff_xsk->cb. 460 */ 461 ice_xdp_meta_set_desc(*xdp, rx_desc); 462 463 rx_desc++; 464 xdp++; 465 } 466 467 return buffs; 468 } 469 470 /** 471 * __ice_alloc_rx_bufs_zc - allocate a number of Rx buffers 472 * @rx_ring: Rx ring 473 * @xsk_pool: XSK buffer pool to pick buffers to be filled by HW 474 * @count: The number of buffers to allocate 475 * 476 * Place the @count of descriptors onto Rx ring. Handle the ring wrap 477 * for case where space from next_to_use up to the end of ring is less 478 * than @count. Finally do a tail bump. 479 * 480 * Returns true if all allocations were successful, false if any fail. 481 */ 482 static bool __ice_alloc_rx_bufs_zc(struct ice_rx_ring *rx_ring, 483 struct xsk_buff_pool *xsk_pool, u16 count) 484 { 485 u32 nb_buffs_extra = 0, nb_buffs = 0; 486 union ice_32b_rx_flex_desc *rx_desc; 487 u16 ntu = rx_ring->next_to_use; 488 u16 total_count = count; 489 struct xdp_buff **xdp; 490 491 rx_desc = ICE_RX_DESC(rx_ring, ntu); 492 xdp = ice_xdp_buf(rx_ring, ntu); 493 494 if (ntu + count >= rx_ring->count) { 495 nb_buffs_extra = ice_fill_rx_descs(xsk_pool, xdp, rx_desc, 496 rx_ring->count - ntu); 497 if (nb_buffs_extra != rx_ring->count - ntu) { 498 ntu += nb_buffs_extra; 499 goto exit; 500 } 501 rx_desc = ICE_RX_DESC(rx_ring, 0); 502 xdp = ice_xdp_buf(rx_ring, 0); 503 ntu = 0; 504 count -= nb_buffs_extra; 505 ice_release_rx_desc(rx_ring, 0); 506 } 507 508 nb_buffs = ice_fill_rx_descs(xsk_pool, xdp, rx_desc, count); 509 510 ntu += nb_buffs; 511 if (ntu == rx_ring->count) 512 ntu = 0; 513 514 exit: 515 if (rx_ring->next_to_use != ntu) 516 ice_release_rx_desc(rx_ring, ntu); 517 518 return total_count == (nb_buffs_extra + nb_buffs); 519 } 520 521 /** 522 * ice_alloc_rx_bufs_zc - allocate a number of Rx buffers 523 * @rx_ring: Rx ring 524 * @xsk_pool: XSK buffer pool to pick buffers to be filled by HW 525 * @count: The number of buffers to allocate 526 * 527 * Wrapper for internal allocation routine; figure out how many tail 528 * bumps should take place based on the given threshold 529 * 530 * Returns true if all calls to internal alloc routine succeeded 531 */ 532 bool ice_alloc_rx_bufs_zc(struct ice_rx_ring *rx_ring, 533 struct xsk_buff_pool *xsk_pool, u16 count) 534 { 535 u16 rx_thresh = ICE_RING_QUARTER(rx_ring); 536 u16 leftover, i, tail_bumps; 537 538 tail_bumps = count / rx_thresh; 539 leftover = count - (tail_bumps * rx_thresh); 540 541 for (i = 0; i < tail_bumps; i++) 542 if (!__ice_alloc_rx_bufs_zc(rx_ring, xsk_pool, rx_thresh)) 543 return false; 544 return __ice_alloc_rx_bufs_zc(rx_ring, xsk_pool, leftover); 545 } 546 547 /** 548 * ice_construct_skb_zc - Create an sk_buff from zero-copy buffer 549 * @rx_ring: Rx ring 550 * @xdp: Pointer to XDP buffer 551 * 552 * This function allocates a new skb from a zero-copy Rx buffer. 553 * 554 * Returns the skb on success, NULL on failure. 555 */ 556 static struct sk_buff * 557 ice_construct_skb_zc(struct ice_rx_ring *rx_ring, struct xdp_buff *xdp) 558 { 559 unsigned int totalsize = xdp->data_end - xdp->data_meta; 560 unsigned int metasize = xdp->data - xdp->data_meta; 561 struct skb_shared_info *sinfo = NULL; 562 struct sk_buff *skb; 563 u32 nr_frags = 0; 564 565 if (unlikely(xdp_buff_has_frags(xdp))) { 566 sinfo = xdp_get_shared_info_from_buff(xdp); 567 nr_frags = sinfo->nr_frags; 568 } 569 net_prefetch(xdp->data_meta); 570 571 skb = napi_alloc_skb(&rx_ring->q_vector->napi, totalsize); 572 if (unlikely(!skb)) 573 return NULL; 574 575 memcpy(__skb_put(skb, totalsize), xdp->data_meta, 576 ALIGN(totalsize, sizeof(long))); 577 578 if (metasize) { 579 skb_metadata_set(skb, metasize); 580 __skb_pull(skb, metasize); 581 } 582 583 if (likely(!xdp_buff_has_frags(xdp))) 584 goto out; 585 586 for (int i = 0; i < nr_frags; i++) { 587 struct skb_shared_info *skinfo = skb_shinfo(skb); 588 skb_frag_t *frag = &sinfo->frags[i]; 589 struct page *page; 590 void *addr; 591 592 page = dev_alloc_page(); 593 if (!page) { 594 dev_kfree_skb(skb); 595 return NULL; 596 } 597 addr = page_to_virt(page); 598 599 memcpy(addr, skb_frag_page(frag), skb_frag_size(frag)); 600 601 __skb_fill_page_desc_noacc(skinfo, skinfo->nr_frags++, 602 addr, 0, skb_frag_size(frag)); 603 } 604 605 out: 606 xsk_buff_free(xdp); 607 return skb; 608 } 609 610 /** 611 * ice_clean_xdp_irq_zc - produce AF_XDP descriptors to CQ 612 * @xdp_ring: XDP Tx ring 613 * @xsk_pool: AF_XDP buffer pool pointer 614 */ 615 static u32 ice_clean_xdp_irq_zc(struct ice_tx_ring *xdp_ring, 616 struct xsk_buff_pool *xsk_pool) 617 { 618 u16 ntc = xdp_ring->next_to_clean; 619 struct ice_tx_desc *tx_desc; 620 u16 cnt = xdp_ring->count; 621 struct ice_tx_buf *tx_buf; 622 u16 completed_frames = 0; 623 u16 xsk_frames = 0; 624 u16 last_rs; 625 int i; 626 627 last_rs = xdp_ring->next_to_use ? xdp_ring->next_to_use - 1 : cnt - 1; 628 tx_desc = ICE_TX_DESC(xdp_ring, last_rs); 629 if (tx_desc->cmd_type_offset_bsz & 630 cpu_to_le64(ICE_TX_DESC_DTYPE_DESC_DONE)) { 631 if (last_rs >= ntc) 632 completed_frames = last_rs - ntc + 1; 633 else 634 completed_frames = last_rs + cnt - ntc + 1; 635 } 636 637 if (!completed_frames) 638 return 0; 639 640 if (likely(!xdp_ring->xdp_tx_active)) { 641 xsk_frames = completed_frames; 642 goto skip; 643 } 644 645 ntc = xdp_ring->next_to_clean; 646 for (i = 0; i < completed_frames; i++) { 647 tx_buf = &xdp_ring->tx_buf[ntc]; 648 649 if (tx_buf->type == ICE_TX_BUF_XSK_TX) { 650 tx_buf->type = ICE_TX_BUF_EMPTY; 651 xsk_buff_free(tx_buf->xdp); 652 xdp_ring->xdp_tx_active--; 653 } else { 654 xsk_frames++; 655 } 656 657 ntc++; 658 if (ntc >= xdp_ring->count) 659 ntc = 0; 660 } 661 skip: 662 tx_desc->cmd_type_offset_bsz = 0; 663 xdp_ring->next_to_clean += completed_frames; 664 if (xdp_ring->next_to_clean >= cnt) 665 xdp_ring->next_to_clean -= cnt; 666 if (xsk_frames) 667 xsk_tx_completed(xsk_pool, xsk_frames); 668 669 return completed_frames; 670 } 671 672 /** 673 * ice_xmit_xdp_tx_zc - AF_XDP ZC handler for XDP_TX 674 * @xdp: XDP buffer to xmit 675 * @xdp_ring: XDP ring to produce descriptor onto 676 * @xsk_pool: AF_XDP buffer pool pointer 677 * 678 * note that this function works directly on xdp_buff, no need to convert 679 * it to xdp_frame. xdp_buff pointer is stored to ice_tx_buf so that cleaning 680 * side will be able to xsk_buff_free() it. 681 * 682 * Returns ICE_XDP_TX for successfully produced desc, ICE_XDP_CONSUMED if there 683 * was not enough space on XDP ring 684 */ 685 static int ice_xmit_xdp_tx_zc(struct xdp_buff *xdp, 686 struct ice_tx_ring *xdp_ring, 687 struct xsk_buff_pool *xsk_pool) 688 { 689 struct skb_shared_info *sinfo = NULL; 690 u32 size = xdp->data_end - xdp->data; 691 u32 ntu = xdp_ring->next_to_use; 692 struct ice_tx_desc *tx_desc; 693 struct ice_tx_buf *tx_buf; 694 struct xdp_buff *head; 695 u32 nr_frags = 0; 696 u32 free_space; 697 u32 frag = 0; 698 699 free_space = ICE_DESC_UNUSED(xdp_ring); 700 if (free_space < ICE_RING_QUARTER(xdp_ring)) 701 free_space += ice_clean_xdp_irq_zc(xdp_ring, xsk_pool); 702 703 if (unlikely(!free_space)) 704 goto busy; 705 706 if (unlikely(xdp_buff_has_frags(xdp))) { 707 sinfo = xdp_get_shared_info_from_buff(xdp); 708 nr_frags = sinfo->nr_frags; 709 if (free_space < nr_frags + 1) 710 goto busy; 711 } 712 713 tx_desc = ICE_TX_DESC(xdp_ring, ntu); 714 tx_buf = &xdp_ring->tx_buf[ntu]; 715 head = xdp; 716 717 for (;;) { 718 dma_addr_t dma; 719 720 dma = xsk_buff_xdp_get_dma(xdp); 721 xsk_buff_raw_dma_sync_for_device(xsk_pool, dma, size); 722 723 tx_buf->xdp = xdp; 724 tx_buf->type = ICE_TX_BUF_XSK_TX; 725 tx_desc->buf_addr = cpu_to_le64(dma); 726 tx_desc->cmd_type_offset_bsz = ice_build_ctob(0, 0, size, 0); 727 /* account for each xdp_buff from xsk_buff_pool */ 728 xdp_ring->xdp_tx_active++; 729 730 if (++ntu == xdp_ring->count) 731 ntu = 0; 732 733 if (frag == nr_frags) 734 break; 735 736 tx_desc = ICE_TX_DESC(xdp_ring, ntu); 737 tx_buf = &xdp_ring->tx_buf[ntu]; 738 739 xdp = xsk_buff_get_frag(head); 740 size = skb_frag_size(&sinfo->frags[frag]); 741 frag++; 742 } 743 744 xdp_ring->next_to_use = ntu; 745 /* update last descriptor from a frame with EOP */ 746 tx_desc->cmd_type_offset_bsz |= 747 cpu_to_le64(ICE_TX_DESC_CMD_EOP << ICE_TXD_QW1_CMD_S); 748 749 return ICE_XDP_TX; 750 751 busy: 752 xdp_ring->ring_stats->tx_stats.tx_busy++; 753 754 return ICE_XDP_CONSUMED; 755 } 756 757 /** 758 * ice_run_xdp_zc - Executes an XDP program in zero-copy path 759 * @rx_ring: Rx ring 760 * @xdp: xdp_buff used as input to the XDP program 761 * @xdp_prog: XDP program to run 762 * @xdp_ring: ring to be used for XDP_TX action 763 * @xsk_pool: AF_XDP buffer pool pointer 764 * 765 * Returns any of ICE_XDP_{PASS, CONSUMED, TX, REDIR} 766 */ 767 static int 768 ice_run_xdp_zc(struct ice_rx_ring *rx_ring, struct xdp_buff *xdp, 769 struct bpf_prog *xdp_prog, struct ice_tx_ring *xdp_ring, 770 struct xsk_buff_pool *xsk_pool) 771 { 772 int err, result = ICE_XDP_PASS; 773 u32 act; 774 775 act = bpf_prog_run_xdp(xdp_prog, xdp); 776 777 if (likely(act == XDP_REDIRECT)) { 778 err = xdp_do_redirect(rx_ring->netdev, xdp, xdp_prog); 779 if (!err) 780 return ICE_XDP_REDIR; 781 if (xsk_uses_need_wakeup(xsk_pool) && err == -ENOBUFS) 782 result = ICE_XDP_EXIT; 783 else 784 result = ICE_XDP_CONSUMED; 785 goto out_failure; 786 } 787 788 switch (act) { 789 case XDP_PASS: 790 break; 791 case XDP_TX: 792 result = ice_xmit_xdp_tx_zc(xdp, xdp_ring, xsk_pool); 793 if (result == ICE_XDP_CONSUMED) 794 goto out_failure; 795 break; 796 case XDP_DROP: 797 result = ICE_XDP_CONSUMED; 798 break; 799 default: 800 bpf_warn_invalid_xdp_action(rx_ring->netdev, xdp_prog, act); 801 fallthrough; 802 case XDP_ABORTED: 803 result = ICE_XDP_CONSUMED; 804 out_failure: 805 trace_xdp_exception(rx_ring->netdev, xdp_prog, act); 806 break; 807 } 808 809 return result; 810 } 811 812 static int 813 ice_add_xsk_frag(struct ice_rx_ring *rx_ring, struct xdp_buff *first, 814 struct xdp_buff *xdp, const unsigned int size) 815 { 816 struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(first); 817 818 if (!size) 819 return 0; 820 821 if (!xdp_buff_has_frags(first)) { 822 sinfo->nr_frags = 0; 823 sinfo->xdp_frags_size = 0; 824 xdp_buff_set_frags_flag(first); 825 } 826 827 if (unlikely(sinfo->nr_frags == MAX_SKB_FRAGS)) { 828 xsk_buff_free(first); 829 return -ENOMEM; 830 } 831 832 __skb_fill_page_desc_noacc(sinfo, sinfo->nr_frags++, 833 virt_to_page(xdp->data_hard_start), 834 XDP_PACKET_HEADROOM, size); 835 sinfo->xdp_frags_size += size; 836 xsk_buff_add_frag(xdp); 837 838 return 0; 839 } 840 841 /** 842 * ice_clean_rx_irq_zc - consumes packets from the hardware ring 843 * @rx_ring: AF_XDP Rx ring 844 * @xsk_pool: AF_XDP buffer pool pointer 845 * @budget: NAPI budget 846 * 847 * Returns number of processed packets on success, remaining budget on failure. 848 */ 849 int ice_clean_rx_irq_zc(struct ice_rx_ring *rx_ring, 850 struct xsk_buff_pool *xsk_pool, 851 int budget) 852 { 853 unsigned int total_rx_bytes = 0, total_rx_packets = 0; 854 u32 ntc = rx_ring->next_to_clean; 855 u32 ntu = rx_ring->next_to_use; 856 struct xdp_buff *first = NULL; 857 struct ice_tx_ring *xdp_ring; 858 unsigned int xdp_xmit = 0; 859 struct bpf_prog *xdp_prog; 860 u32 cnt = rx_ring->count; 861 bool failure = false; 862 int entries_to_alloc; 863 864 /* ZC patch is enabled only when XDP program is set, 865 * so here it can not be NULL 866 */ 867 xdp_prog = READ_ONCE(rx_ring->xdp_prog); 868 xdp_ring = rx_ring->xdp_ring; 869 870 if (ntc != rx_ring->first_desc) 871 first = *ice_xdp_buf(rx_ring, rx_ring->first_desc); 872 873 while (likely(total_rx_packets < (unsigned int)budget)) { 874 union ice_32b_rx_flex_desc *rx_desc; 875 unsigned int size, xdp_res = 0; 876 struct xdp_buff *xdp; 877 struct sk_buff *skb; 878 u16 stat_err_bits; 879 u16 vlan_tci; 880 881 rx_desc = ICE_RX_DESC(rx_ring, ntc); 882 883 stat_err_bits = BIT(ICE_RX_FLEX_DESC_STATUS0_DD_S); 884 if (!ice_test_staterr(rx_desc->wb.status_error0, stat_err_bits)) 885 break; 886 887 /* This memory barrier is needed to keep us from reading 888 * any other fields out of the rx_desc until we have 889 * verified the descriptor has been written back. 890 */ 891 dma_rmb(); 892 893 if (unlikely(ntc == ntu)) 894 break; 895 896 xdp = *ice_xdp_buf(rx_ring, ntc); 897 898 size = le16_to_cpu(rx_desc->wb.pkt_len) & 899 ICE_RX_FLX_DESC_PKT_LEN_M; 900 901 xsk_buff_set_size(xdp, size); 902 xsk_buff_dma_sync_for_cpu(xdp); 903 904 if (!first) { 905 first = xdp; 906 } else if (ice_add_xsk_frag(rx_ring, first, xdp, size)) { 907 break; 908 } 909 910 if (++ntc == cnt) 911 ntc = 0; 912 913 if (ice_is_non_eop(rx_ring, rx_desc)) 914 continue; 915 916 xdp_res = ice_run_xdp_zc(rx_ring, first, xdp_prog, xdp_ring, 917 xsk_pool); 918 if (likely(xdp_res & (ICE_XDP_TX | ICE_XDP_REDIR))) { 919 xdp_xmit |= xdp_res; 920 } else if (xdp_res == ICE_XDP_EXIT) { 921 failure = true; 922 first = NULL; 923 rx_ring->first_desc = ntc; 924 break; 925 } else if (xdp_res == ICE_XDP_CONSUMED) { 926 xsk_buff_free(first); 927 } else if (xdp_res == ICE_XDP_PASS) { 928 goto construct_skb; 929 } 930 931 total_rx_bytes += xdp_get_buff_len(first); 932 total_rx_packets++; 933 934 first = NULL; 935 rx_ring->first_desc = ntc; 936 continue; 937 938 construct_skb: 939 /* XDP_PASS path */ 940 skb = ice_construct_skb_zc(rx_ring, first); 941 if (!skb) { 942 rx_ring->ring_stats->rx_stats.alloc_buf_failed++; 943 break; 944 } 945 946 first = NULL; 947 rx_ring->first_desc = ntc; 948 949 if (eth_skb_pad(skb)) { 950 skb = NULL; 951 continue; 952 } 953 954 total_rx_bytes += skb->len; 955 total_rx_packets++; 956 957 vlan_tci = ice_get_vlan_tci(rx_desc); 958 959 ice_process_skb_fields(rx_ring, rx_desc, skb); 960 ice_receive_skb(rx_ring, skb, vlan_tci); 961 } 962 963 rx_ring->next_to_clean = ntc; 964 entries_to_alloc = ICE_RX_DESC_UNUSED(rx_ring); 965 if (entries_to_alloc > ICE_RING_QUARTER(rx_ring)) 966 failure |= !ice_alloc_rx_bufs_zc(rx_ring, xsk_pool, 967 entries_to_alloc); 968 969 ice_finalize_xdp_rx(xdp_ring, xdp_xmit, 0); 970 ice_update_rx_ring_stats(rx_ring, total_rx_packets, total_rx_bytes); 971 972 if (xsk_uses_need_wakeup(xsk_pool)) { 973 /* ntu could have changed when allocating entries above, so 974 * use rx_ring value instead of stack based one 975 */ 976 if (failure || ntc == rx_ring->next_to_use) 977 xsk_set_rx_need_wakeup(xsk_pool); 978 else 979 xsk_clear_rx_need_wakeup(xsk_pool); 980 981 return (int)total_rx_packets; 982 } 983 984 return failure ? budget : (int)total_rx_packets; 985 } 986 987 /** 988 * ice_xmit_pkt - produce a single HW Tx descriptor out of AF_XDP descriptor 989 * @xdp_ring: XDP ring to produce the HW Tx descriptor on 990 * @xsk_pool: XSK buffer pool to pick buffers to be consumed by HW 991 * @desc: AF_XDP descriptor to pull the DMA address and length from 992 * @total_bytes: bytes accumulator that will be used for stats update 993 */ 994 static void ice_xmit_pkt(struct ice_tx_ring *xdp_ring, 995 struct xsk_buff_pool *xsk_pool, struct xdp_desc *desc, 996 unsigned int *total_bytes) 997 { 998 struct ice_tx_desc *tx_desc; 999 dma_addr_t dma; 1000 1001 dma = xsk_buff_raw_get_dma(xsk_pool, desc->addr); 1002 xsk_buff_raw_dma_sync_for_device(xsk_pool, dma, desc->len); 1003 1004 tx_desc = ICE_TX_DESC(xdp_ring, xdp_ring->next_to_use++); 1005 tx_desc->buf_addr = cpu_to_le64(dma); 1006 tx_desc->cmd_type_offset_bsz = ice_build_ctob(xsk_is_eop_desc(desc), 1007 0, desc->len, 0); 1008 1009 *total_bytes += desc->len; 1010 } 1011 1012 /** 1013 * ice_xmit_pkt_batch - produce a batch of HW Tx descriptors out of AF_XDP descriptors 1014 * @xdp_ring: XDP ring to produce the HW Tx descriptors on 1015 * @xsk_pool: XSK buffer pool to pick buffers to be consumed by HW 1016 * @descs: AF_XDP descriptors to pull the DMA addresses and lengths from 1017 * @total_bytes: bytes accumulator that will be used for stats update 1018 */ 1019 static void ice_xmit_pkt_batch(struct ice_tx_ring *xdp_ring, 1020 struct xsk_buff_pool *xsk_pool, 1021 struct xdp_desc *descs, 1022 unsigned int *total_bytes) 1023 { 1024 u16 ntu = xdp_ring->next_to_use; 1025 struct ice_tx_desc *tx_desc; 1026 u32 i; 1027 1028 loop_unrolled_for(i = 0; i < PKTS_PER_BATCH; i++) { 1029 dma_addr_t dma; 1030 1031 dma = xsk_buff_raw_get_dma(xsk_pool, descs[i].addr); 1032 xsk_buff_raw_dma_sync_for_device(xsk_pool, dma, descs[i].len); 1033 1034 tx_desc = ICE_TX_DESC(xdp_ring, ntu++); 1035 tx_desc->buf_addr = cpu_to_le64(dma); 1036 tx_desc->cmd_type_offset_bsz = ice_build_ctob(xsk_is_eop_desc(&descs[i]), 1037 0, descs[i].len, 0); 1038 1039 *total_bytes += descs[i].len; 1040 } 1041 1042 xdp_ring->next_to_use = ntu; 1043 } 1044 1045 /** 1046 * ice_fill_tx_hw_ring - produce the number of Tx descriptors onto ring 1047 * @xdp_ring: XDP ring to produce the HW Tx descriptors on 1048 * @xsk_pool: XSK buffer pool to pick buffers to be consumed by HW 1049 * @descs: AF_XDP descriptors to pull the DMA addresses and lengths from 1050 * @nb_pkts: count of packets to be send 1051 * @total_bytes: bytes accumulator that will be used for stats update 1052 */ 1053 static void ice_fill_tx_hw_ring(struct ice_tx_ring *xdp_ring, 1054 struct xsk_buff_pool *xsk_pool, 1055 struct xdp_desc *descs, u32 nb_pkts, 1056 unsigned int *total_bytes) 1057 { 1058 u32 batched, leftover, i; 1059 1060 batched = ALIGN_DOWN(nb_pkts, PKTS_PER_BATCH); 1061 leftover = nb_pkts & (PKTS_PER_BATCH - 1); 1062 for (i = 0; i < batched; i += PKTS_PER_BATCH) 1063 ice_xmit_pkt_batch(xdp_ring, xsk_pool, &descs[i], total_bytes); 1064 for (; i < batched + leftover; i++) 1065 ice_xmit_pkt(xdp_ring, xsk_pool, &descs[i], total_bytes); 1066 } 1067 1068 /** 1069 * ice_xmit_zc - take entries from XSK Tx ring and place them onto HW Tx ring 1070 * @xdp_ring: XDP ring to produce the HW Tx descriptors on 1071 * @xsk_pool: AF_XDP buffer pool pointer 1072 * 1073 * Returns true if there is no more work that needs to be done, false otherwise 1074 */ 1075 bool ice_xmit_zc(struct ice_tx_ring *xdp_ring, struct xsk_buff_pool *xsk_pool) 1076 { 1077 struct xdp_desc *descs = xsk_pool->tx_descs; 1078 u32 nb_pkts, nb_processed = 0; 1079 unsigned int total_bytes = 0; 1080 int budget; 1081 1082 ice_clean_xdp_irq_zc(xdp_ring, xsk_pool); 1083 1084 if (!netif_carrier_ok(xdp_ring->vsi->netdev) || 1085 !netif_running(xdp_ring->vsi->netdev)) 1086 return true; 1087 1088 budget = ICE_DESC_UNUSED(xdp_ring); 1089 budget = min_t(u16, budget, ICE_RING_QUARTER(xdp_ring)); 1090 1091 nb_pkts = xsk_tx_peek_release_desc_batch(xsk_pool, budget); 1092 if (!nb_pkts) 1093 return true; 1094 1095 if (xdp_ring->next_to_use + nb_pkts >= xdp_ring->count) { 1096 nb_processed = xdp_ring->count - xdp_ring->next_to_use; 1097 ice_fill_tx_hw_ring(xdp_ring, xsk_pool, descs, nb_processed, 1098 &total_bytes); 1099 xdp_ring->next_to_use = 0; 1100 } 1101 1102 ice_fill_tx_hw_ring(xdp_ring, xsk_pool, &descs[nb_processed], 1103 nb_pkts - nb_processed, &total_bytes); 1104 1105 ice_set_rs_bit(xdp_ring); 1106 ice_xdp_ring_update_tail(xdp_ring); 1107 ice_update_tx_ring_stats(xdp_ring, nb_pkts, total_bytes); 1108 1109 if (xsk_uses_need_wakeup(xsk_pool)) 1110 xsk_set_tx_need_wakeup(xsk_pool); 1111 1112 return nb_pkts < budget; 1113 } 1114 1115 /** 1116 * ice_xsk_wakeup - Implements ndo_xsk_wakeup 1117 * @netdev: net_device 1118 * @queue_id: queue to wake up 1119 * @flags: ignored in our case, since we have Rx and Tx in the same NAPI 1120 * 1121 * Returns negative on error, zero otherwise. 1122 */ 1123 int 1124 ice_xsk_wakeup(struct net_device *netdev, u32 queue_id, 1125 u32 __always_unused flags) 1126 { 1127 struct ice_netdev_priv *np = netdev_priv(netdev); 1128 struct ice_q_vector *q_vector; 1129 struct ice_vsi *vsi = np->vsi; 1130 struct ice_tx_ring *ring; 1131 1132 if (test_bit(ICE_VSI_DOWN, vsi->state) || !netif_carrier_ok(netdev)) 1133 return -ENETDOWN; 1134 1135 if (!ice_is_xdp_ena_vsi(vsi)) 1136 return -EINVAL; 1137 1138 if (queue_id >= vsi->num_txq || queue_id >= vsi->num_rxq) 1139 return -EINVAL; 1140 1141 ring = vsi->rx_rings[queue_id]->xdp_ring; 1142 1143 if (!READ_ONCE(ring->xsk_pool)) 1144 return -EINVAL; 1145 1146 /* The idea here is that if NAPI is running, mark a miss, so 1147 * it will run again. If not, trigger an interrupt and 1148 * schedule the NAPI from interrupt context. If NAPI would be 1149 * scheduled here, the interrupt affinity would not be 1150 * honored. 1151 */ 1152 q_vector = ring->q_vector; 1153 if (!napi_if_scheduled_mark_missed(&q_vector->napi)) 1154 ice_trigger_sw_intr(&vsi->back->hw, q_vector); 1155 1156 return 0; 1157 } 1158 1159 /** 1160 * ice_xsk_any_rx_ring_ena - Checks if Rx rings have AF_XDP buff pool attached 1161 * @vsi: VSI to be checked 1162 * 1163 * Returns true if any of the Rx rings has an AF_XDP buff pool attached 1164 */ 1165 bool ice_xsk_any_rx_ring_ena(struct ice_vsi *vsi) 1166 { 1167 int i; 1168 1169 ice_for_each_rxq(vsi, i) { 1170 if (xsk_get_pool_from_qid(vsi->netdev, i)) 1171 return true; 1172 } 1173 1174 return false; 1175 } 1176 1177 /** 1178 * ice_xsk_clean_rx_ring - clean buffer pool queues connected to a given Rx ring 1179 * @rx_ring: ring to be cleaned 1180 */ 1181 void ice_xsk_clean_rx_ring(struct ice_rx_ring *rx_ring) 1182 { 1183 u16 ntc = rx_ring->next_to_clean; 1184 u16 ntu = rx_ring->next_to_use; 1185 1186 while (ntc != ntu) { 1187 struct xdp_buff *xdp = *ice_xdp_buf(rx_ring, ntc); 1188 1189 xsk_buff_free(xdp); 1190 ntc++; 1191 if (ntc >= rx_ring->count) 1192 ntc = 0; 1193 } 1194 } 1195 1196 /** 1197 * ice_xsk_clean_xdp_ring - Clean the XDP Tx ring and its buffer pool queues 1198 * @xdp_ring: XDP_Tx ring 1199 */ 1200 void ice_xsk_clean_xdp_ring(struct ice_tx_ring *xdp_ring) 1201 { 1202 u16 ntc = xdp_ring->next_to_clean, ntu = xdp_ring->next_to_use; 1203 u32 xsk_frames = 0; 1204 1205 while (ntc != ntu) { 1206 struct ice_tx_buf *tx_buf = &xdp_ring->tx_buf[ntc]; 1207 1208 if (tx_buf->type == ICE_TX_BUF_XSK_TX) { 1209 tx_buf->type = ICE_TX_BUF_EMPTY; 1210 xsk_buff_free(tx_buf->xdp); 1211 } else { 1212 xsk_frames++; 1213 } 1214 1215 ntc++; 1216 if (ntc >= xdp_ring->count) 1217 ntc = 0; 1218 } 1219 1220 if (xsk_frames) 1221 xsk_tx_completed(xdp_ring->xsk_pool, xsk_frames); 1222 } 1223