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