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