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 /** 16 * ice_qp_reset_stats - Resets all stats for rings of given index 17 * @vsi: VSI that contains rings of interest 18 * @q_idx: ring index in array 19 */ 20 static void ice_qp_reset_stats(struct ice_vsi *vsi, u16 q_idx) 21 { 22 memset(&vsi->rx_rings[q_idx]->rx_stats, 0, 23 sizeof(vsi->rx_rings[q_idx]->rx_stats)); 24 memset(&vsi->tx_rings[q_idx]->stats, 0, 25 sizeof(vsi->tx_rings[q_idx]->stats)); 26 if (ice_is_xdp_ena_vsi(vsi)) 27 memset(&vsi->xdp_rings[q_idx]->stats, 0, 28 sizeof(vsi->xdp_rings[q_idx]->stats)); 29 } 30 31 /** 32 * ice_qp_clean_rings - Cleans all the rings of a given index 33 * @vsi: VSI that contains rings of interest 34 * @q_idx: ring index in array 35 */ 36 static void ice_qp_clean_rings(struct ice_vsi *vsi, u16 q_idx) 37 { 38 ice_clean_tx_ring(vsi->tx_rings[q_idx]); 39 if (ice_is_xdp_ena_vsi(vsi)) 40 ice_clean_tx_ring(vsi->xdp_rings[q_idx]); 41 ice_clean_rx_ring(vsi->rx_rings[q_idx]); 42 } 43 44 /** 45 * ice_qvec_toggle_napi - Enables/disables NAPI for a given q_vector 46 * @vsi: VSI that has netdev 47 * @q_vector: q_vector that has NAPI context 48 * @enable: true for enable, false for disable 49 */ 50 static void 51 ice_qvec_toggle_napi(struct ice_vsi *vsi, struct ice_q_vector *q_vector, 52 bool enable) 53 { 54 if (!vsi->netdev || !q_vector) 55 return; 56 57 if (enable) 58 napi_enable(&q_vector->napi); 59 else 60 napi_disable(&q_vector->napi); 61 } 62 63 /** 64 * ice_qvec_dis_irq - Mask off queue interrupt generation on given ring 65 * @vsi: the VSI that contains queue vector being un-configured 66 * @rx_ring: Rx ring that will have its IRQ disabled 67 * @q_vector: queue vector 68 */ 69 static void 70 ice_qvec_dis_irq(struct ice_vsi *vsi, struct ice_ring *rx_ring, 71 struct ice_q_vector *q_vector) 72 { 73 struct ice_pf *pf = vsi->back; 74 struct ice_hw *hw = &pf->hw; 75 int base = vsi->base_vector; 76 u16 reg; 77 u32 val; 78 79 /* QINT_TQCTL is being cleared in ice_vsi_stop_tx_ring, so handle 80 * here only QINT_RQCTL 81 */ 82 reg = rx_ring->reg_idx; 83 val = rd32(hw, QINT_RQCTL(reg)); 84 val &= ~QINT_RQCTL_CAUSE_ENA_M; 85 wr32(hw, QINT_RQCTL(reg), val); 86 87 if (q_vector) { 88 u16 v_idx = q_vector->v_idx; 89 90 wr32(hw, GLINT_DYN_CTL(q_vector->reg_idx), 0); 91 ice_flush(hw); 92 synchronize_irq(pf->msix_entries[v_idx + base].vector); 93 } 94 } 95 96 /** 97 * ice_qvec_cfg_msix - Enable IRQ for given queue vector 98 * @vsi: the VSI that contains queue vector 99 * @q_vector: queue vector 100 */ 101 static void 102 ice_qvec_cfg_msix(struct ice_vsi *vsi, struct ice_q_vector *q_vector) 103 { 104 u16 reg_idx = q_vector->reg_idx; 105 struct ice_pf *pf = vsi->back; 106 struct ice_hw *hw = &pf->hw; 107 struct ice_ring *ring; 108 109 ice_cfg_itr(hw, q_vector); 110 111 wr32(hw, GLINT_RATE(reg_idx), 112 ice_intrl_usec_to_reg(q_vector->intrl, hw->intrl_gran)); 113 114 ice_for_each_ring(ring, q_vector->tx) 115 ice_cfg_txq_interrupt(vsi, ring->reg_idx, reg_idx, 116 q_vector->tx.itr_idx); 117 118 ice_for_each_ring(ring, q_vector->rx) 119 ice_cfg_rxq_interrupt(vsi, ring->reg_idx, reg_idx, 120 q_vector->rx.itr_idx); 121 122 ice_flush(hw); 123 } 124 125 /** 126 * ice_qvec_ena_irq - Enable IRQ for given queue vector 127 * @vsi: the VSI that contains queue vector 128 * @q_vector: queue vector 129 */ 130 static void ice_qvec_ena_irq(struct ice_vsi *vsi, struct ice_q_vector *q_vector) 131 { 132 struct ice_pf *pf = vsi->back; 133 struct ice_hw *hw = &pf->hw; 134 135 ice_irq_dynamic_ena(hw, vsi, q_vector); 136 137 ice_flush(hw); 138 } 139 140 /** 141 * ice_qp_dis - Disables a queue pair 142 * @vsi: VSI of interest 143 * @q_idx: ring index in array 144 * 145 * Returns 0 on success, negative on failure. 146 */ 147 static int ice_qp_dis(struct ice_vsi *vsi, u16 q_idx) 148 { 149 struct ice_txq_meta txq_meta = { }; 150 struct ice_ring *tx_ring, *rx_ring; 151 struct ice_q_vector *q_vector; 152 int timeout = 50; 153 int err; 154 155 if (q_idx >= vsi->num_rxq || q_idx >= vsi->num_txq) 156 return -EINVAL; 157 158 tx_ring = vsi->tx_rings[q_idx]; 159 rx_ring = vsi->rx_rings[q_idx]; 160 q_vector = rx_ring->q_vector; 161 162 while (test_and_set_bit(__ICE_CFG_BUSY, vsi->state)) { 163 timeout--; 164 if (!timeout) 165 return -EBUSY; 166 usleep_range(1000, 2000); 167 } 168 netif_tx_stop_queue(netdev_get_tx_queue(vsi->netdev, q_idx)); 169 170 ice_qvec_dis_irq(vsi, rx_ring, q_vector); 171 172 ice_fill_txq_meta(vsi, tx_ring, &txq_meta); 173 err = ice_vsi_stop_tx_ring(vsi, ICE_NO_RESET, 0, tx_ring, &txq_meta); 174 if (err) 175 return err; 176 if (ice_is_xdp_ena_vsi(vsi)) { 177 struct ice_ring *xdp_ring = vsi->xdp_rings[q_idx]; 178 179 memset(&txq_meta, 0, sizeof(txq_meta)); 180 ice_fill_txq_meta(vsi, xdp_ring, &txq_meta); 181 err = ice_vsi_stop_tx_ring(vsi, ICE_NO_RESET, 0, xdp_ring, 182 &txq_meta); 183 if (err) 184 return err; 185 } 186 err = ice_vsi_ctrl_one_rx_ring(vsi, false, q_idx, true); 187 if (err) 188 return err; 189 190 ice_qvec_toggle_napi(vsi, q_vector, false); 191 ice_qp_clean_rings(vsi, q_idx); 192 ice_qp_reset_stats(vsi, q_idx); 193 194 return 0; 195 } 196 197 /** 198 * ice_qp_ena - Enables a queue pair 199 * @vsi: VSI of interest 200 * @q_idx: ring index in array 201 * 202 * Returns 0 on success, negative on failure. 203 */ 204 static int ice_qp_ena(struct ice_vsi *vsi, u16 q_idx) 205 { 206 struct ice_aqc_add_tx_qgrp *qg_buf; 207 struct ice_ring *tx_ring, *rx_ring; 208 struct ice_q_vector *q_vector; 209 int err; 210 211 if (q_idx >= vsi->num_rxq || q_idx >= vsi->num_txq) 212 return -EINVAL; 213 214 qg_buf = kzalloc(sizeof(*qg_buf), GFP_KERNEL); 215 if (!qg_buf) 216 return -ENOMEM; 217 218 qg_buf->num_txqs = 1; 219 220 tx_ring = vsi->tx_rings[q_idx]; 221 rx_ring = vsi->rx_rings[q_idx]; 222 q_vector = rx_ring->q_vector; 223 224 err = ice_vsi_cfg_txq(vsi, tx_ring, qg_buf); 225 if (err) 226 goto free_buf; 227 228 if (ice_is_xdp_ena_vsi(vsi)) { 229 struct ice_ring *xdp_ring = vsi->xdp_rings[q_idx]; 230 231 memset(qg_buf, 0, sizeof(*qg_buf)); 232 qg_buf->num_txqs = 1; 233 err = ice_vsi_cfg_txq(vsi, xdp_ring, qg_buf); 234 if (err) 235 goto free_buf; 236 ice_set_ring_xdp(xdp_ring); 237 xdp_ring->xsk_umem = ice_xsk_umem(xdp_ring); 238 } 239 240 err = ice_setup_rx_ctx(rx_ring); 241 if (err) 242 goto free_buf; 243 244 ice_qvec_cfg_msix(vsi, q_vector); 245 246 err = ice_vsi_ctrl_one_rx_ring(vsi, true, q_idx, true); 247 if (err) 248 goto free_buf; 249 250 clear_bit(__ICE_CFG_BUSY, vsi->state); 251 ice_qvec_toggle_napi(vsi, q_vector, true); 252 ice_qvec_ena_irq(vsi, q_vector); 253 254 netif_tx_start_queue(netdev_get_tx_queue(vsi->netdev, q_idx)); 255 free_buf: 256 kfree(qg_buf); 257 return err; 258 } 259 260 /** 261 * ice_xsk_alloc_umems - allocate a UMEM region for an XDP socket 262 * @vsi: VSI to allocate the UMEM on 263 * 264 * Returns 0 on success, negative on error 265 */ 266 static int ice_xsk_alloc_umems(struct ice_vsi *vsi) 267 { 268 if (vsi->xsk_umems) 269 return 0; 270 271 vsi->xsk_umems = kcalloc(vsi->num_xsk_umems, sizeof(*vsi->xsk_umems), 272 GFP_KERNEL); 273 274 if (!vsi->xsk_umems) { 275 vsi->num_xsk_umems = 0; 276 return -ENOMEM; 277 } 278 279 return 0; 280 } 281 282 /** 283 * ice_xsk_remove_umem - Remove an UMEM for a certain ring/qid 284 * @vsi: VSI from which the VSI will be removed 285 * @qid: Ring/qid associated with the UMEM 286 */ 287 static void ice_xsk_remove_umem(struct ice_vsi *vsi, u16 qid) 288 { 289 vsi->xsk_umems[qid] = NULL; 290 vsi->num_xsk_umems_used--; 291 292 if (vsi->num_xsk_umems_used == 0) { 293 kfree(vsi->xsk_umems); 294 vsi->xsk_umems = NULL; 295 vsi->num_xsk_umems = 0; 296 } 297 } 298 299 300 /** 301 * ice_xsk_umem_disable - disable a UMEM region 302 * @vsi: Current VSI 303 * @qid: queue ID 304 * 305 * Returns 0 on success, negative on failure 306 */ 307 static int ice_xsk_umem_disable(struct ice_vsi *vsi, u16 qid) 308 { 309 if (!vsi->xsk_umems || qid >= vsi->num_xsk_umems || 310 !vsi->xsk_umems[qid]) 311 return -EINVAL; 312 313 xsk_buff_dma_unmap(vsi->xsk_umems[qid], ICE_RX_DMA_ATTR); 314 ice_xsk_remove_umem(vsi, qid); 315 316 return 0; 317 } 318 319 /** 320 * ice_xsk_umem_enable - enable a UMEM region 321 * @vsi: Current VSI 322 * @umem: pointer to a requested UMEM region 323 * @qid: queue ID 324 * 325 * Returns 0 on success, negative on failure 326 */ 327 static int 328 ice_xsk_umem_enable(struct ice_vsi *vsi, struct xdp_umem *umem, u16 qid) 329 { 330 int err; 331 332 if (vsi->type != ICE_VSI_PF) 333 return -EINVAL; 334 335 if (!vsi->num_xsk_umems) 336 vsi->num_xsk_umems = min_t(u16, vsi->num_rxq, vsi->num_txq); 337 if (qid >= vsi->num_xsk_umems) 338 return -EINVAL; 339 340 err = ice_xsk_alloc_umems(vsi); 341 if (err) 342 return err; 343 344 if (vsi->xsk_umems && vsi->xsk_umems[qid]) 345 return -EBUSY; 346 347 vsi->xsk_umems[qid] = umem; 348 vsi->num_xsk_umems_used++; 349 350 err = xsk_buff_dma_map(vsi->xsk_umems[qid], ice_pf_to_dev(vsi->back), 351 ICE_RX_DMA_ATTR); 352 if (err) 353 return err; 354 355 return 0; 356 } 357 358 /** 359 * ice_xsk_umem_setup - enable/disable a UMEM region depending on its state 360 * @vsi: Current VSI 361 * @umem: UMEM to enable/associate to a ring, NULL to disable 362 * @qid: queue ID 363 * 364 * Returns 0 on success, negative on failure 365 */ 366 int ice_xsk_umem_setup(struct ice_vsi *vsi, struct xdp_umem *umem, u16 qid) 367 { 368 bool if_running, umem_present = !!umem; 369 int ret = 0, umem_failure = 0; 370 371 if_running = netif_running(vsi->netdev) && ice_is_xdp_ena_vsi(vsi); 372 373 if (if_running) { 374 ret = ice_qp_dis(vsi, qid); 375 if (ret) { 376 netdev_err(vsi->netdev, "ice_qp_dis error = %d\n", ret); 377 goto xsk_umem_if_up; 378 } 379 } 380 381 umem_failure = umem_present ? ice_xsk_umem_enable(vsi, umem, qid) : 382 ice_xsk_umem_disable(vsi, qid); 383 384 xsk_umem_if_up: 385 if (if_running) { 386 ret = ice_qp_ena(vsi, qid); 387 if (!ret && umem_present) 388 napi_schedule(&vsi->xdp_rings[qid]->q_vector->napi); 389 else if (ret) 390 netdev_err(vsi->netdev, "ice_qp_ena error = %d\n", ret); 391 } 392 393 if (umem_failure) { 394 netdev_err(vsi->netdev, "Could not %sable UMEM, error = %d\n", 395 umem_present ? "en" : "dis", umem_failure); 396 return umem_failure; 397 } 398 399 return ret; 400 } 401 402 /** 403 * ice_alloc_rx_bufs_zc - allocate a number of Rx buffers 404 * @rx_ring: Rx ring 405 * @count: The number of buffers to allocate 406 * 407 * This function allocates a number of Rx buffers from the fill ring 408 * or the internal recycle mechanism and places them on the Rx ring. 409 * 410 * Returns false if all allocations were successful, true if any fail. 411 */ 412 bool ice_alloc_rx_bufs_zc(struct ice_ring *rx_ring, u16 count) 413 { 414 union ice_32b_rx_flex_desc *rx_desc; 415 u16 ntu = rx_ring->next_to_use; 416 struct ice_rx_buf *rx_buf; 417 bool ret = false; 418 dma_addr_t dma; 419 420 if (!count) 421 return false; 422 423 rx_desc = ICE_RX_DESC(rx_ring, ntu); 424 rx_buf = &rx_ring->rx_buf[ntu]; 425 426 do { 427 rx_buf->xdp = xsk_buff_alloc(rx_ring->xsk_umem); 428 if (!rx_buf->xdp) { 429 ret = true; 430 break; 431 } 432 433 dma = xsk_buff_xdp_get_dma(rx_buf->xdp); 434 rx_desc->read.pkt_addr = cpu_to_le64(dma); 435 rx_desc->wb.status_error0 = 0; 436 437 rx_desc++; 438 rx_buf++; 439 ntu++; 440 441 if (unlikely(ntu == rx_ring->count)) { 442 rx_desc = ICE_RX_DESC(rx_ring, 0); 443 rx_buf = rx_ring->rx_buf; 444 ntu = 0; 445 } 446 } while (--count); 447 448 if (rx_ring->next_to_use != ntu) 449 ice_release_rx_desc(rx_ring, ntu); 450 451 return ret; 452 } 453 454 /** 455 * ice_bump_ntc - Bump the next_to_clean counter of an Rx ring 456 * @rx_ring: Rx ring 457 */ 458 static void ice_bump_ntc(struct ice_ring *rx_ring) 459 { 460 int ntc = rx_ring->next_to_clean + 1; 461 462 ntc = (ntc < rx_ring->count) ? ntc : 0; 463 rx_ring->next_to_clean = ntc; 464 prefetch(ICE_RX_DESC(rx_ring, ntc)); 465 } 466 467 /** 468 * ice_construct_skb_zc - Create an sk_buff from zero-copy buffer 469 * @rx_ring: Rx ring 470 * @rx_buf: zero-copy Rx buffer 471 * 472 * This function allocates a new skb from a zero-copy Rx buffer. 473 * 474 * Returns the skb on success, NULL on failure. 475 */ 476 static struct sk_buff * 477 ice_construct_skb_zc(struct ice_ring *rx_ring, struct ice_rx_buf *rx_buf) 478 { 479 unsigned int metasize = rx_buf->xdp->data - rx_buf->xdp->data_meta; 480 unsigned int datasize = rx_buf->xdp->data_end - rx_buf->xdp->data; 481 unsigned int datasize_hard = rx_buf->xdp->data_end - 482 rx_buf->xdp->data_hard_start; 483 struct sk_buff *skb; 484 485 skb = __napi_alloc_skb(&rx_ring->q_vector->napi, datasize_hard, 486 GFP_ATOMIC | __GFP_NOWARN); 487 if (unlikely(!skb)) 488 return NULL; 489 490 skb_reserve(skb, rx_buf->xdp->data - rx_buf->xdp->data_hard_start); 491 memcpy(__skb_put(skb, datasize), rx_buf->xdp->data, datasize); 492 if (metasize) 493 skb_metadata_set(skb, metasize); 494 495 xsk_buff_free(rx_buf->xdp); 496 rx_buf->xdp = NULL; 497 return skb; 498 } 499 500 /** 501 * ice_run_xdp_zc - Executes an XDP program in zero-copy path 502 * @rx_ring: Rx ring 503 * @xdp: xdp_buff used as input to the XDP program 504 * 505 * Returns any of ICE_XDP_{PASS, CONSUMED, TX, REDIR} 506 */ 507 static int 508 ice_run_xdp_zc(struct ice_ring *rx_ring, struct xdp_buff *xdp) 509 { 510 int err, result = ICE_XDP_PASS; 511 struct bpf_prog *xdp_prog; 512 struct ice_ring *xdp_ring; 513 u32 act; 514 515 rcu_read_lock(); 516 xdp_prog = READ_ONCE(rx_ring->xdp_prog); 517 if (!xdp_prog) { 518 rcu_read_unlock(); 519 return ICE_XDP_PASS; 520 } 521 522 act = bpf_prog_run_xdp(xdp_prog, xdp); 523 switch (act) { 524 case XDP_PASS: 525 break; 526 case XDP_TX: 527 xdp_ring = rx_ring->vsi->xdp_rings[rx_ring->q_index]; 528 result = ice_xmit_xdp_buff(xdp, xdp_ring); 529 break; 530 case XDP_REDIRECT: 531 err = xdp_do_redirect(rx_ring->netdev, xdp, xdp_prog); 532 result = !err ? ICE_XDP_REDIR : ICE_XDP_CONSUMED; 533 break; 534 default: 535 bpf_warn_invalid_xdp_action(act); 536 fallthrough; 537 case XDP_ABORTED: 538 trace_xdp_exception(rx_ring->netdev, xdp_prog, act); 539 fallthrough; 540 case XDP_DROP: 541 result = ICE_XDP_CONSUMED; 542 break; 543 } 544 545 rcu_read_unlock(); 546 return result; 547 } 548 549 /** 550 * ice_clean_rx_irq_zc - consumes packets from the hardware ring 551 * @rx_ring: AF_XDP Rx ring 552 * @budget: NAPI budget 553 * 554 * Returns number of processed packets on success, remaining budget on failure. 555 */ 556 int ice_clean_rx_irq_zc(struct ice_ring *rx_ring, int budget) 557 { 558 unsigned int total_rx_bytes = 0, total_rx_packets = 0; 559 u16 cleaned_count = ICE_DESC_UNUSED(rx_ring); 560 unsigned int xdp_xmit = 0; 561 bool failure = false; 562 563 while (likely(total_rx_packets < (unsigned int)budget)) { 564 union ice_32b_rx_flex_desc *rx_desc; 565 unsigned int size, xdp_res = 0; 566 struct ice_rx_buf *rx_buf; 567 struct sk_buff *skb; 568 u16 stat_err_bits; 569 u16 vlan_tag = 0; 570 u8 rx_ptype; 571 572 if (cleaned_count >= ICE_RX_BUF_WRITE) { 573 failure |= ice_alloc_rx_bufs_zc(rx_ring, 574 cleaned_count); 575 cleaned_count = 0; 576 } 577 578 rx_desc = ICE_RX_DESC(rx_ring, rx_ring->next_to_clean); 579 580 stat_err_bits = BIT(ICE_RX_FLEX_DESC_STATUS0_DD_S); 581 if (!ice_test_staterr(rx_desc, stat_err_bits)) 582 break; 583 584 /* This memory barrier is needed to keep us from reading 585 * any other fields out of the rx_desc until we have 586 * verified the descriptor has been written back. 587 */ 588 dma_rmb(); 589 590 size = le16_to_cpu(rx_desc->wb.pkt_len) & 591 ICE_RX_FLX_DESC_PKT_LEN_M; 592 if (!size) 593 break; 594 595 596 rx_buf = &rx_ring->rx_buf[rx_ring->next_to_clean]; 597 rx_buf->xdp->data_end = rx_buf->xdp->data + size; 598 xsk_buff_dma_sync_for_cpu(rx_buf->xdp); 599 600 xdp_res = ice_run_xdp_zc(rx_ring, rx_buf->xdp); 601 if (xdp_res) { 602 if (xdp_res & (ICE_XDP_TX | ICE_XDP_REDIR)) 603 xdp_xmit |= xdp_res; 604 else 605 xsk_buff_free(rx_buf->xdp); 606 607 rx_buf->xdp = NULL; 608 total_rx_bytes += size; 609 total_rx_packets++; 610 cleaned_count++; 611 612 ice_bump_ntc(rx_ring); 613 continue; 614 } 615 616 /* XDP_PASS path */ 617 skb = ice_construct_skb_zc(rx_ring, rx_buf); 618 if (!skb) { 619 rx_ring->rx_stats.alloc_buf_failed++; 620 break; 621 } 622 623 cleaned_count++; 624 ice_bump_ntc(rx_ring); 625 626 if (eth_skb_pad(skb)) { 627 skb = NULL; 628 continue; 629 } 630 631 total_rx_bytes += skb->len; 632 total_rx_packets++; 633 634 stat_err_bits = BIT(ICE_RX_FLEX_DESC_STATUS0_L2TAG1P_S); 635 if (ice_test_staterr(rx_desc, stat_err_bits)) 636 vlan_tag = le16_to_cpu(rx_desc->wb.l2tag1); 637 638 rx_ptype = le16_to_cpu(rx_desc->wb.ptype_flex_flags0) & 639 ICE_RX_FLEX_DESC_PTYPE_M; 640 641 ice_process_skb_fields(rx_ring, rx_desc, skb, rx_ptype); 642 ice_receive_skb(rx_ring, skb, vlan_tag); 643 } 644 645 ice_finalize_xdp_rx(rx_ring, xdp_xmit); 646 ice_update_rx_ring_stats(rx_ring, total_rx_packets, total_rx_bytes); 647 648 if (xsk_umem_uses_need_wakeup(rx_ring->xsk_umem)) { 649 if (failure || rx_ring->next_to_clean == rx_ring->next_to_use) 650 xsk_set_rx_need_wakeup(rx_ring->xsk_umem); 651 else 652 xsk_clear_rx_need_wakeup(rx_ring->xsk_umem); 653 654 return (int)total_rx_packets; 655 } 656 657 return failure ? budget : (int)total_rx_packets; 658 } 659 660 /** 661 * ice_xmit_zc - Completes AF_XDP entries, and cleans XDP entries 662 * @xdp_ring: XDP Tx ring 663 * @budget: max number of frames to xmit 664 * 665 * Returns true if cleanup/transmission is done. 666 */ 667 static bool ice_xmit_zc(struct ice_ring *xdp_ring, int budget) 668 { 669 struct ice_tx_desc *tx_desc = NULL; 670 bool work_done = true; 671 struct xdp_desc desc; 672 dma_addr_t dma; 673 674 while (likely(budget-- > 0)) { 675 struct ice_tx_buf *tx_buf; 676 677 if (unlikely(!ICE_DESC_UNUSED(xdp_ring))) { 678 xdp_ring->tx_stats.tx_busy++; 679 work_done = false; 680 break; 681 } 682 683 tx_buf = &xdp_ring->tx_buf[xdp_ring->next_to_use]; 684 685 if (!xsk_umem_consume_tx(xdp_ring->xsk_umem, &desc)) 686 break; 687 688 dma = xsk_buff_raw_get_dma(xdp_ring->xsk_umem, desc.addr); 689 xsk_buff_raw_dma_sync_for_device(xdp_ring->xsk_umem, dma, 690 desc.len); 691 692 tx_buf->bytecount = desc.len; 693 694 tx_desc = ICE_TX_DESC(xdp_ring, xdp_ring->next_to_use); 695 tx_desc->buf_addr = cpu_to_le64(dma); 696 tx_desc->cmd_type_offset_bsz = 697 ice_build_ctob(ICE_TXD_LAST_DESC_CMD, 0, desc.len, 0); 698 699 xdp_ring->next_to_use++; 700 if (xdp_ring->next_to_use == xdp_ring->count) 701 xdp_ring->next_to_use = 0; 702 } 703 704 if (tx_desc) { 705 ice_xdp_ring_update_tail(xdp_ring); 706 xsk_umem_consume_tx_done(xdp_ring->xsk_umem); 707 if (xsk_umem_uses_need_wakeup(xdp_ring->xsk_umem)) 708 xsk_clear_tx_need_wakeup(xdp_ring->xsk_umem); 709 } 710 711 return budget > 0 && work_done; 712 } 713 714 /** 715 * ice_clean_xdp_tx_buf - Free and unmap XDP Tx buffer 716 * @xdp_ring: XDP Tx ring 717 * @tx_buf: Tx buffer to clean 718 */ 719 static void 720 ice_clean_xdp_tx_buf(struct ice_ring *xdp_ring, struct ice_tx_buf *tx_buf) 721 { 722 xdp_return_frame((struct xdp_frame *)tx_buf->raw_buf); 723 dma_unmap_single(xdp_ring->dev, dma_unmap_addr(tx_buf, dma), 724 dma_unmap_len(tx_buf, len), DMA_TO_DEVICE); 725 dma_unmap_len_set(tx_buf, len, 0); 726 } 727 728 /** 729 * ice_clean_tx_irq_zc - Completes AF_XDP entries, and cleans XDP entries 730 * @xdp_ring: XDP Tx ring 731 * @budget: NAPI budget 732 * 733 * Returns true if cleanup/tranmission is done. 734 */ 735 bool ice_clean_tx_irq_zc(struct ice_ring *xdp_ring, int budget) 736 { 737 int total_packets = 0, total_bytes = 0; 738 s16 ntc = xdp_ring->next_to_clean; 739 struct ice_tx_desc *tx_desc; 740 struct ice_tx_buf *tx_buf; 741 u32 xsk_frames = 0; 742 bool xmit_done; 743 744 tx_desc = ICE_TX_DESC(xdp_ring, ntc); 745 tx_buf = &xdp_ring->tx_buf[ntc]; 746 ntc -= xdp_ring->count; 747 748 do { 749 if (!(tx_desc->cmd_type_offset_bsz & 750 cpu_to_le64(ICE_TX_DESC_DTYPE_DESC_DONE))) 751 break; 752 753 total_bytes += tx_buf->bytecount; 754 total_packets++; 755 756 if (tx_buf->raw_buf) { 757 ice_clean_xdp_tx_buf(xdp_ring, tx_buf); 758 tx_buf->raw_buf = NULL; 759 } else { 760 xsk_frames++; 761 } 762 763 tx_desc->cmd_type_offset_bsz = 0; 764 tx_buf++; 765 tx_desc++; 766 ntc++; 767 768 if (unlikely(!ntc)) { 769 ntc -= xdp_ring->count; 770 tx_buf = xdp_ring->tx_buf; 771 tx_desc = ICE_TX_DESC(xdp_ring, 0); 772 } 773 774 prefetch(tx_desc); 775 776 } while (likely(--budget)); 777 778 ntc += xdp_ring->count; 779 xdp_ring->next_to_clean = ntc; 780 781 if (xsk_frames) 782 xsk_umem_complete_tx(xdp_ring->xsk_umem, xsk_frames); 783 784 if (xsk_umem_uses_need_wakeup(xdp_ring->xsk_umem)) { 785 if (xdp_ring->next_to_clean == xdp_ring->next_to_use) 786 xsk_set_tx_need_wakeup(xdp_ring->xsk_umem); 787 else 788 xsk_clear_tx_need_wakeup(xdp_ring->xsk_umem); 789 } 790 791 ice_update_tx_ring_stats(xdp_ring, total_packets, total_bytes); 792 xmit_done = ice_xmit_zc(xdp_ring, ICE_DFLT_IRQ_WORK); 793 794 return budget > 0 && xmit_done; 795 } 796 797 /** 798 * ice_xsk_wakeup - Implements ndo_xsk_wakeup 799 * @netdev: net_device 800 * @queue_id: queue to wake up 801 * @flags: ignored in our case, since we have Rx and Tx in the same NAPI 802 * 803 * Returns negative on error, zero otherwise. 804 */ 805 int 806 ice_xsk_wakeup(struct net_device *netdev, u32 queue_id, 807 u32 __always_unused flags) 808 { 809 struct ice_netdev_priv *np = netdev_priv(netdev); 810 struct ice_q_vector *q_vector; 811 struct ice_vsi *vsi = np->vsi; 812 struct ice_ring *ring; 813 814 if (test_bit(__ICE_DOWN, vsi->state)) 815 return -ENETDOWN; 816 817 if (!ice_is_xdp_ena_vsi(vsi)) 818 return -ENXIO; 819 820 if (queue_id >= vsi->num_txq) 821 return -ENXIO; 822 823 if (!vsi->xdp_rings[queue_id]->xsk_umem) 824 return -ENXIO; 825 826 ring = vsi->xdp_rings[queue_id]; 827 828 /* The idea here is that if NAPI is running, mark a miss, so 829 * it will run again. If not, trigger an interrupt and 830 * schedule the NAPI from interrupt context. If NAPI would be 831 * scheduled here, the interrupt affinity would not be 832 * honored. 833 */ 834 q_vector = ring->q_vector; 835 if (!napi_if_scheduled_mark_missed(&q_vector->napi)) 836 ice_trigger_sw_intr(&vsi->back->hw, q_vector); 837 838 return 0; 839 } 840 841 /** 842 * ice_xsk_any_rx_ring_ena - Checks if Rx rings have AF_XDP UMEM attached 843 * @vsi: VSI to be checked 844 * 845 * Returns true if any of the Rx rings has an AF_XDP UMEM attached 846 */ 847 bool ice_xsk_any_rx_ring_ena(struct ice_vsi *vsi) 848 { 849 int i; 850 851 if (!vsi->xsk_umems) 852 return false; 853 854 for (i = 0; i < vsi->num_xsk_umems; i++) { 855 if (vsi->xsk_umems[i]) 856 return true; 857 } 858 859 return false; 860 } 861 862 /** 863 * ice_xsk_clean_rx_ring - clean UMEM queues connected to a given Rx ring 864 * @rx_ring: ring to be cleaned 865 */ 866 void ice_xsk_clean_rx_ring(struct ice_ring *rx_ring) 867 { 868 u16 i; 869 870 for (i = 0; i < rx_ring->count; i++) { 871 struct ice_rx_buf *rx_buf = &rx_ring->rx_buf[i]; 872 873 if (!rx_buf->xdp) 874 continue; 875 876 rx_buf->xdp = NULL; 877 } 878 } 879 880 /** 881 * ice_xsk_clean_xdp_ring - Clean the XDP Tx ring and its UMEM queues 882 * @xdp_ring: XDP_Tx ring 883 */ 884 void ice_xsk_clean_xdp_ring(struct ice_ring *xdp_ring) 885 { 886 u16 ntc = xdp_ring->next_to_clean, ntu = xdp_ring->next_to_use; 887 u32 xsk_frames = 0; 888 889 while (ntc != ntu) { 890 struct ice_tx_buf *tx_buf = &xdp_ring->tx_buf[ntc]; 891 892 if (tx_buf->raw_buf) 893 ice_clean_xdp_tx_buf(xdp_ring, tx_buf); 894 else 895 xsk_frames++; 896 897 tx_buf->raw_buf = NULL; 898 899 ntc++; 900 if (ntc >= xdp_ring->count) 901 ntc = 0; 902 } 903 904 if (xsk_frames) 905 xsk_umem_complete_tx(xdp_ring->xsk_umem, xsk_frames); 906 } 907