1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright(c) 2018 Intel Corporation. */ 3 4 #include <linux/bpf_trace.h> 5 #include <net/xdp_sock.h> 6 #include <net/xdp.h> 7 8 #include "i40e.h" 9 #include "i40e_txrx_common.h" 10 #include "i40e_xsk.h" 11 12 /** 13 * i40e_xsk_umem_dma_map - DMA maps all UMEM memory for the netdev 14 * @vsi: Current VSI 15 * @umem: UMEM to DMA map 16 * 17 * Returns 0 on success, <0 on failure 18 **/ 19 static int i40e_xsk_umem_dma_map(struct i40e_vsi *vsi, struct xdp_umem *umem) 20 { 21 struct i40e_pf *pf = vsi->back; 22 struct device *dev; 23 unsigned int i, j; 24 dma_addr_t dma; 25 26 dev = &pf->pdev->dev; 27 for (i = 0; i < umem->npgs; i++) { 28 dma = dma_map_page_attrs(dev, umem->pgs[i], 0, PAGE_SIZE, 29 DMA_BIDIRECTIONAL, I40E_RX_DMA_ATTR); 30 if (dma_mapping_error(dev, dma)) 31 goto out_unmap; 32 33 umem->pages[i].dma = dma; 34 } 35 36 return 0; 37 38 out_unmap: 39 for (j = 0; j < i; j++) { 40 dma_unmap_page_attrs(dev, umem->pages[i].dma, PAGE_SIZE, 41 DMA_BIDIRECTIONAL, I40E_RX_DMA_ATTR); 42 umem->pages[i].dma = 0; 43 } 44 45 return -1; 46 } 47 48 /** 49 * i40e_xsk_umem_dma_unmap - DMA unmaps all UMEM memory for the netdev 50 * @vsi: Current VSI 51 * @umem: UMEM to DMA map 52 **/ 53 static void i40e_xsk_umem_dma_unmap(struct i40e_vsi *vsi, struct xdp_umem *umem) 54 { 55 struct i40e_pf *pf = vsi->back; 56 struct device *dev; 57 unsigned int i; 58 59 dev = &pf->pdev->dev; 60 61 for (i = 0; i < umem->npgs; i++) { 62 dma_unmap_page_attrs(dev, umem->pages[i].dma, PAGE_SIZE, 63 DMA_BIDIRECTIONAL, I40E_RX_DMA_ATTR); 64 65 umem->pages[i].dma = 0; 66 } 67 } 68 69 /** 70 * i40e_xsk_umem_enable - Enable/associate a UMEM to a certain ring/qid 71 * @vsi: Current VSI 72 * @umem: UMEM 73 * @qid: Rx ring to associate UMEM to 74 * 75 * Returns 0 on success, <0 on failure 76 **/ 77 static int i40e_xsk_umem_enable(struct i40e_vsi *vsi, struct xdp_umem *umem, 78 u16 qid) 79 { 80 struct net_device *netdev = vsi->netdev; 81 struct xdp_umem_fq_reuse *reuseq; 82 bool if_running; 83 int err; 84 85 if (vsi->type != I40E_VSI_MAIN) 86 return -EINVAL; 87 88 if (qid >= vsi->num_queue_pairs) 89 return -EINVAL; 90 91 if (qid >= netdev->real_num_rx_queues || 92 qid >= netdev->real_num_tx_queues) 93 return -EINVAL; 94 95 reuseq = xsk_reuseq_prepare(vsi->rx_rings[0]->count); 96 if (!reuseq) 97 return -ENOMEM; 98 99 xsk_reuseq_free(xsk_reuseq_swap(umem, reuseq)); 100 101 err = i40e_xsk_umem_dma_map(vsi, umem); 102 if (err) 103 return err; 104 105 if_running = netif_running(vsi->netdev) && i40e_enabled_xdp_vsi(vsi); 106 107 if (if_running) { 108 err = i40e_queue_pair_disable(vsi, qid); 109 if (err) 110 return err; 111 112 err = i40e_queue_pair_enable(vsi, qid); 113 if (err) 114 return err; 115 } 116 117 return 0; 118 } 119 120 /** 121 * i40e_xsk_umem_disable - Disassociate a UMEM from a certain ring/qid 122 * @vsi: Current VSI 123 * @qid: Rx ring to associate UMEM to 124 * 125 * Returns 0 on success, <0 on failure 126 **/ 127 static int i40e_xsk_umem_disable(struct i40e_vsi *vsi, u16 qid) 128 { 129 struct net_device *netdev = vsi->netdev; 130 struct xdp_umem *umem; 131 bool if_running; 132 int err; 133 134 umem = xdp_get_umem_from_qid(netdev, qid); 135 if (!umem) 136 return -EINVAL; 137 138 if_running = netif_running(vsi->netdev) && i40e_enabled_xdp_vsi(vsi); 139 140 if (if_running) { 141 err = i40e_queue_pair_disable(vsi, qid); 142 if (err) 143 return err; 144 } 145 146 i40e_xsk_umem_dma_unmap(vsi, umem); 147 148 if (if_running) { 149 err = i40e_queue_pair_enable(vsi, qid); 150 if (err) 151 return err; 152 } 153 154 return 0; 155 } 156 157 /** 158 * i40e_xsk_umem_setup - Enable/disassociate a UMEM to/from a ring/qid 159 * @vsi: Current VSI 160 * @umem: UMEM to enable/associate to a ring, or NULL to disable 161 * @qid: Rx ring to (dis)associate UMEM (from)to 162 * 163 * This function enables or disables a UMEM to a certain ring. 164 * 165 * Returns 0 on success, <0 on failure 166 **/ 167 int i40e_xsk_umem_setup(struct i40e_vsi *vsi, struct xdp_umem *umem, 168 u16 qid) 169 { 170 return umem ? i40e_xsk_umem_enable(vsi, umem, qid) : 171 i40e_xsk_umem_disable(vsi, qid); 172 } 173 174 /** 175 * i40e_run_xdp_zc - Executes an XDP program on an xdp_buff 176 * @rx_ring: Rx ring 177 * @xdp: xdp_buff used as input to the XDP program 178 * 179 * This function enables or disables a UMEM to a certain ring. 180 * 181 * Returns any of I40E_XDP_{PASS, CONSUMED, TX, REDIR} 182 **/ 183 static int i40e_run_xdp_zc(struct i40e_ring *rx_ring, struct xdp_buff *xdp) 184 { 185 int err, result = I40E_XDP_PASS; 186 struct i40e_ring *xdp_ring; 187 struct bpf_prog *xdp_prog; 188 u32 act; 189 190 rcu_read_lock(); 191 /* NB! xdp_prog will always be !NULL, due to the fact that 192 * this path is enabled by setting an XDP program. 193 */ 194 xdp_prog = READ_ONCE(rx_ring->xdp_prog); 195 act = bpf_prog_run_xdp(xdp_prog, xdp); 196 xdp->handle += xdp->data - xdp->data_hard_start; 197 switch (act) { 198 case XDP_PASS: 199 break; 200 case XDP_TX: 201 xdp_ring = rx_ring->vsi->xdp_rings[rx_ring->queue_index]; 202 result = i40e_xmit_xdp_tx_ring(xdp, xdp_ring); 203 break; 204 case XDP_REDIRECT: 205 err = xdp_do_redirect(rx_ring->netdev, xdp, xdp_prog); 206 result = !err ? I40E_XDP_REDIR : I40E_XDP_CONSUMED; 207 break; 208 default: 209 bpf_warn_invalid_xdp_action(act); 210 case XDP_ABORTED: 211 trace_xdp_exception(rx_ring->netdev, xdp_prog, act); 212 /* fallthrough -- handle aborts by dropping packet */ 213 case XDP_DROP: 214 result = I40E_XDP_CONSUMED; 215 break; 216 } 217 rcu_read_unlock(); 218 return result; 219 } 220 221 /** 222 * i40e_alloc_buffer_zc - Allocates an i40e_rx_buffer 223 * @rx_ring: Rx ring 224 * @bi: Rx buffer to populate 225 * 226 * This function allocates an Rx buffer. The buffer can come from fill 227 * queue, or via the recycle queue (next_to_alloc). 228 * 229 * Returns true for a successful allocation, false otherwise 230 **/ 231 static bool i40e_alloc_buffer_zc(struct i40e_ring *rx_ring, 232 struct i40e_rx_buffer *bi) 233 { 234 struct xdp_umem *umem = rx_ring->xsk_umem; 235 void *addr = bi->addr; 236 u64 handle, hr; 237 238 if (addr) { 239 rx_ring->rx_stats.page_reuse_count++; 240 return true; 241 } 242 243 if (!xsk_umem_peek_addr(umem, &handle)) { 244 rx_ring->rx_stats.alloc_page_failed++; 245 return false; 246 } 247 248 hr = umem->headroom + XDP_PACKET_HEADROOM; 249 250 bi->dma = xdp_umem_get_dma(umem, handle); 251 bi->dma += hr; 252 253 bi->addr = xdp_umem_get_data(umem, handle); 254 bi->addr += hr; 255 256 bi->handle = handle + umem->headroom; 257 258 xsk_umem_discard_addr(umem); 259 return true; 260 } 261 262 /** 263 * i40e_alloc_buffer_slow_zc - Allocates an i40e_rx_buffer 264 * @rx_ring: Rx ring 265 * @bi: Rx buffer to populate 266 * 267 * This function allocates an Rx buffer. The buffer can come from fill 268 * queue, or via the reuse queue. 269 * 270 * Returns true for a successful allocation, false otherwise 271 **/ 272 static bool i40e_alloc_buffer_slow_zc(struct i40e_ring *rx_ring, 273 struct i40e_rx_buffer *bi) 274 { 275 struct xdp_umem *umem = rx_ring->xsk_umem; 276 u64 handle, hr; 277 278 if (!xsk_umem_peek_addr_rq(umem, &handle)) { 279 rx_ring->rx_stats.alloc_page_failed++; 280 return false; 281 } 282 283 handle &= rx_ring->xsk_umem->chunk_mask; 284 285 hr = umem->headroom + XDP_PACKET_HEADROOM; 286 287 bi->dma = xdp_umem_get_dma(umem, handle); 288 bi->dma += hr; 289 290 bi->addr = xdp_umem_get_data(umem, handle); 291 bi->addr += hr; 292 293 bi->handle = handle + umem->headroom; 294 295 xsk_umem_discard_addr_rq(umem); 296 return true; 297 } 298 299 static __always_inline bool 300 __i40e_alloc_rx_buffers_zc(struct i40e_ring *rx_ring, u16 count, 301 bool alloc(struct i40e_ring *rx_ring, 302 struct i40e_rx_buffer *bi)) 303 { 304 u16 ntu = rx_ring->next_to_use; 305 union i40e_rx_desc *rx_desc; 306 struct i40e_rx_buffer *bi; 307 bool ok = true; 308 309 rx_desc = I40E_RX_DESC(rx_ring, ntu); 310 bi = &rx_ring->rx_bi[ntu]; 311 do { 312 if (!alloc(rx_ring, bi)) { 313 ok = false; 314 goto no_buffers; 315 } 316 317 dma_sync_single_range_for_device(rx_ring->dev, bi->dma, 0, 318 rx_ring->rx_buf_len, 319 DMA_BIDIRECTIONAL); 320 321 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma); 322 323 rx_desc++; 324 bi++; 325 ntu++; 326 327 if (unlikely(ntu == rx_ring->count)) { 328 rx_desc = I40E_RX_DESC(rx_ring, 0); 329 bi = rx_ring->rx_bi; 330 ntu = 0; 331 } 332 333 rx_desc->wb.qword1.status_error_len = 0; 334 count--; 335 } while (count); 336 337 no_buffers: 338 if (rx_ring->next_to_use != ntu) 339 i40e_release_rx_desc(rx_ring, ntu); 340 341 return ok; 342 } 343 344 /** 345 * i40e_alloc_rx_buffers_zc - Allocates a number of Rx buffers 346 * @rx_ring: Rx ring 347 * @count: The number of buffers to allocate 348 * 349 * This function allocates a number of Rx buffers from the reuse queue 350 * or fill ring and places them on the Rx ring. 351 * 352 * Returns true for a successful allocation, false otherwise 353 **/ 354 bool i40e_alloc_rx_buffers_zc(struct i40e_ring *rx_ring, u16 count) 355 { 356 return __i40e_alloc_rx_buffers_zc(rx_ring, count, 357 i40e_alloc_buffer_slow_zc); 358 } 359 360 /** 361 * i40e_alloc_rx_buffers_fast_zc - Allocates a number of Rx buffers 362 * @rx_ring: Rx ring 363 * @count: The number of buffers to allocate 364 * 365 * This function allocates a number of Rx buffers from the fill ring 366 * or the internal recycle mechanism and places them on the Rx ring. 367 * 368 * Returns true for a successful allocation, false otherwise 369 **/ 370 static bool i40e_alloc_rx_buffers_fast_zc(struct i40e_ring *rx_ring, u16 count) 371 { 372 return __i40e_alloc_rx_buffers_zc(rx_ring, count, 373 i40e_alloc_buffer_zc); 374 } 375 376 /** 377 * i40e_get_rx_buffer_zc - Return the current Rx buffer 378 * @rx_ring: Rx ring 379 * @size: The size of the rx buffer (read from descriptor) 380 * 381 * This function returns the current, received Rx buffer, and also 382 * does DMA synchronization. the Rx ring. 383 * 384 * Returns the received Rx buffer 385 **/ 386 static struct i40e_rx_buffer *i40e_get_rx_buffer_zc(struct i40e_ring *rx_ring, 387 const unsigned int size) 388 { 389 struct i40e_rx_buffer *bi; 390 391 bi = &rx_ring->rx_bi[rx_ring->next_to_clean]; 392 393 /* we are reusing so sync this buffer for CPU use */ 394 dma_sync_single_range_for_cpu(rx_ring->dev, 395 bi->dma, 0, 396 size, 397 DMA_BIDIRECTIONAL); 398 399 return bi; 400 } 401 402 /** 403 * i40e_reuse_rx_buffer_zc - Recycle an Rx buffer 404 * @rx_ring: Rx ring 405 * @old_bi: The Rx buffer to recycle 406 * 407 * This function recycles a finished Rx buffer, and places it on the 408 * recycle queue (next_to_alloc). 409 **/ 410 static void i40e_reuse_rx_buffer_zc(struct i40e_ring *rx_ring, 411 struct i40e_rx_buffer *old_bi) 412 { 413 struct i40e_rx_buffer *new_bi = &rx_ring->rx_bi[rx_ring->next_to_alloc]; 414 unsigned long mask = (unsigned long)rx_ring->xsk_umem->chunk_mask; 415 u64 hr = rx_ring->xsk_umem->headroom + XDP_PACKET_HEADROOM; 416 u16 nta = rx_ring->next_to_alloc; 417 418 /* update, and store next to alloc */ 419 nta++; 420 rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0; 421 422 /* transfer page from old buffer to new buffer */ 423 new_bi->dma = old_bi->dma & mask; 424 new_bi->dma += hr; 425 426 new_bi->addr = (void *)((unsigned long)old_bi->addr & mask); 427 new_bi->addr += hr; 428 429 new_bi->handle = old_bi->handle & mask; 430 new_bi->handle += rx_ring->xsk_umem->headroom; 431 432 old_bi->addr = NULL; 433 } 434 435 /** 436 * i40e_zca_free - Free callback for MEM_TYPE_ZERO_COPY allocations 437 * @alloc: Zero-copy allocator 438 * @handle: Buffer handle 439 **/ 440 void i40e_zca_free(struct zero_copy_allocator *alloc, unsigned long handle) 441 { 442 struct i40e_rx_buffer *bi; 443 struct i40e_ring *rx_ring; 444 u64 hr, mask; 445 u16 nta; 446 447 rx_ring = container_of(alloc, struct i40e_ring, zca); 448 hr = rx_ring->xsk_umem->headroom + XDP_PACKET_HEADROOM; 449 mask = rx_ring->xsk_umem->chunk_mask; 450 451 nta = rx_ring->next_to_alloc; 452 bi = &rx_ring->rx_bi[nta]; 453 454 nta++; 455 rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0; 456 457 handle &= mask; 458 459 bi->dma = xdp_umem_get_dma(rx_ring->xsk_umem, handle); 460 bi->dma += hr; 461 462 bi->addr = xdp_umem_get_data(rx_ring->xsk_umem, handle); 463 bi->addr += hr; 464 465 bi->handle = (u64)handle + rx_ring->xsk_umem->headroom; 466 } 467 468 /** 469 * i40e_construct_skb_zc - Create skbufff from zero-copy Rx buffer 470 * @rx_ring: Rx ring 471 * @bi: Rx buffer 472 * @xdp: xdp_buff 473 * 474 * This functions allocates a new skb from a zero-copy Rx buffer. 475 * 476 * Returns the skb, or NULL on failure. 477 **/ 478 static struct sk_buff *i40e_construct_skb_zc(struct i40e_ring *rx_ring, 479 struct i40e_rx_buffer *bi, 480 struct xdp_buff *xdp) 481 { 482 unsigned int metasize = xdp->data - xdp->data_meta; 483 unsigned int datasize = xdp->data_end - xdp->data; 484 struct sk_buff *skb; 485 486 /* allocate a skb to store the frags */ 487 skb = __napi_alloc_skb(&rx_ring->q_vector->napi, 488 xdp->data_end - xdp->data_hard_start, 489 GFP_ATOMIC | __GFP_NOWARN); 490 if (unlikely(!skb)) 491 return NULL; 492 493 skb_reserve(skb, xdp->data - xdp->data_hard_start); 494 memcpy(__skb_put(skb, datasize), xdp->data, datasize); 495 if (metasize) 496 skb_metadata_set(skb, metasize); 497 498 i40e_reuse_rx_buffer_zc(rx_ring, bi); 499 return skb; 500 } 501 502 /** 503 * i40e_inc_ntc: Advance the next_to_clean index 504 * @rx_ring: Rx ring 505 **/ 506 static void i40e_inc_ntc(struct i40e_ring *rx_ring) 507 { 508 u32 ntc = rx_ring->next_to_clean + 1; 509 510 ntc = (ntc < rx_ring->count) ? ntc : 0; 511 rx_ring->next_to_clean = ntc; 512 prefetch(I40E_RX_DESC(rx_ring, ntc)); 513 } 514 515 /** 516 * i40e_clean_rx_irq_zc - Consumes Rx packets from the hardware ring 517 * @rx_ring: Rx ring 518 * @budget: NAPI budget 519 * 520 * Returns amount of work completed 521 **/ 522 int i40e_clean_rx_irq_zc(struct i40e_ring *rx_ring, int budget) 523 { 524 unsigned int total_rx_bytes = 0, total_rx_packets = 0; 525 u16 cleaned_count = I40E_DESC_UNUSED(rx_ring); 526 unsigned int xdp_res, xdp_xmit = 0; 527 bool failure = false; 528 struct sk_buff *skb; 529 struct xdp_buff xdp; 530 531 xdp.rxq = &rx_ring->xdp_rxq; 532 533 while (likely(total_rx_packets < (unsigned int)budget)) { 534 struct i40e_rx_buffer *bi; 535 union i40e_rx_desc *rx_desc; 536 unsigned int size; 537 u64 qword; 538 539 if (cleaned_count >= I40E_RX_BUFFER_WRITE) { 540 failure = failure || 541 !i40e_alloc_rx_buffers_fast_zc(rx_ring, 542 cleaned_count); 543 cleaned_count = 0; 544 } 545 546 rx_desc = I40E_RX_DESC(rx_ring, rx_ring->next_to_clean); 547 qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len); 548 549 /* This memory barrier is needed to keep us from reading 550 * any other fields out of the rx_desc until we have 551 * verified the descriptor has been written back. 552 */ 553 dma_rmb(); 554 555 bi = i40e_clean_programming_status(rx_ring, rx_desc, 556 qword); 557 if (unlikely(bi)) { 558 i40e_reuse_rx_buffer_zc(rx_ring, bi); 559 cleaned_count++; 560 continue; 561 } 562 563 size = (qword & I40E_RXD_QW1_LENGTH_PBUF_MASK) >> 564 I40E_RXD_QW1_LENGTH_PBUF_SHIFT; 565 if (!size) 566 break; 567 568 bi = i40e_get_rx_buffer_zc(rx_ring, size); 569 xdp.data = bi->addr; 570 xdp.data_meta = xdp.data; 571 xdp.data_hard_start = xdp.data - XDP_PACKET_HEADROOM; 572 xdp.data_end = xdp.data + size; 573 xdp.handle = bi->handle; 574 575 xdp_res = i40e_run_xdp_zc(rx_ring, &xdp); 576 if (xdp_res) { 577 if (xdp_res & (I40E_XDP_TX | I40E_XDP_REDIR)) { 578 xdp_xmit |= xdp_res; 579 bi->addr = NULL; 580 } else { 581 i40e_reuse_rx_buffer_zc(rx_ring, bi); 582 } 583 584 total_rx_bytes += size; 585 total_rx_packets++; 586 587 cleaned_count++; 588 i40e_inc_ntc(rx_ring); 589 continue; 590 } 591 592 /* XDP_PASS path */ 593 594 /* NB! We are not checking for errors using 595 * i40e_test_staterr with 596 * BIT(I40E_RXD_QW1_ERROR_SHIFT). This is due to that 597 * SBP is *not* set in PRT_SBPVSI (default not set). 598 */ 599 skb = i40e_construct_skb_zc(rx_ring, bi, &xdp); 600 if (!skb) { 601 rx_ring->rx_stats.alloc_buff_failed++; 602 break; 603 } 604 605 cleaned_count++; 606 i40e_inc_ntc(rx_ring); 607 608 if (eth_skb_pad(skb)) 609 continue; 610 611 total_rx_bytes += skb->len; 612 total_rx_packets++; 613 614 i40e_process_skb_fields(rx_ring, rx_desc, skb); 615 napi_gro_receive(&rx_ring->q_vector->napi, skb); 616 } 617 618 i40e_finalize_xdp_rx(rx_ring, xdp_xmit); 619 i40e_update_rx_stats(rx_ring, total_rx_bytes, total_rx_packets); 620 return failure ? budget : (int)total_rx_packets; 621 } 622 623 /** 624 * i40e_xmit_zc - Performs zero-copy Tx AF_XDP 625 * @xdp_ring: XDP Tx ring 626 * @budget: NAPI budget 627 * 628 * Returns true if the work is finished. 629 **/ 630 static bool i40e_xmit_zc(struct i40e_ring *xdp_ring, unsigned int budget) 631 { 632 struct i40e_tx_desc *tx_desc = NULL; 633 struct i40e_tx_buffer *tx_bi; 634 bool work_done = true; 635 dma_addr_t dma; 636 u32 len; 637 638 while (budget-- > 0) { 639 if (!unlikely(I40E_DESC_UNUSED(xdp_ring))) { 640 xdp_ring->tx_stats.tx_busy++; 641 work_done = false; 642 break; 643 } 644 645 if (!xsk_umem_consume_tx(xdp_ring->xsk_umem, &dma, &len)) 646 break; 647 648 dma_sync_single_for_device(xdp_ring->dev, dma, len, 649 DMA_BIDIRECTIONAL); 650 651 tx_bi = &xdp_ring->tx_bi[xdp_ring->next_to_use]; 652 tx_bi->bytecount = len; 653 654 tx_desc = I40E_TX_DESC(xdp_ring, xdp_ring->next_to_use); 655 tx_desc->buffer_addr = cpu_to_le64(dma); 656 tx_desc->cmd_type_offset_bsz = 657 build_ctob(I40E_TX_DESC_CMD_ICRC 658 | I40E_TX_DESC_CMD_EOP, 659 0, len, 0); 660 661 xdp_ring->next_to_use++; 662 if (xdp_ring->next_to_use == xdp_ring->count) 663 xdp_ring->next_to_use = 0; 664 } 665 666 if (tx_desc) { 667 /* Request an interrupt for the last frame and bump tail ptr. */ 668 tx_desc->cmd_type_offset_bsz |= (I40E_TX_DESC_CMD_RS << 669 I40E_TXD_QW1_CMD_SHIFT); 670 i40e_xdp_ring_update_tail(xdp_ring); 671 672 xsk_umem_consume_tx_done(xdp_ring->xsk_umem); 673 } 674 675 return !!budget && work_done; 676 } 677 678 /** 679 * i40e_clean_xdp_tx_buffer - Frees and unmaps an XDP Tx entry 680 * @tx_ring: XDP Tx ring 681 * @tx_bi: Tx buffer info to clean 682 **/ 683 static void i40e_clean_xdp_tx_buffer(struct i40e_ring *tx_ring, 684 struct i40e_tx_buffer *tx_bi) 685 { 686 xdp_return_frame(tx_bi->xdpf); 687 dma_unmap_single(tx_ring->dev, 688 dma_unmap_addr(tx_bi, dma), 689 dma_unmap_len(tx_bi, len), DMA_TO_DEVICE); 690 dma_unmap_len_set(tx_bi, len, 0); 691 } 692 693 /** 694 * i40e_clean_xdp_tx_irq - Completes AF_XDP entries, and cleans XDP entries 695 * @tx_ring: XDP Tx ring 696 * @tx_bi: Tx buffer info to clean 697 * 698 * Returns true if cleanup/tranmission is done. 699 **/ 700 bool i40e_clean_xdp_tx_irq(struct i40e_vsi *vsi, 701 struct i40e_ring *tx_ring, int napi_budget) 702 { 703 unsigned int ntc, total_bytes = 0, budget = vsi->work_limit; 704 u32 i, completed_frames, frames_ready, xsk_frames = 0; 705 struct xdp_umem *umem = tx_ring->xsk_umem; 706 u32 head_idx = i40e_get_head(tx_ring); 707 bool work_done = true, xmit_done; 708 struct i40e_tx_buffer *tx_bi; 709 710 if (head_idx < tx_ring->next_to_clean) 711 head_idx += tx_ring->count; 712 frames_ready = head_idx - tx_ring->next_to_clean; 713 714 if (frames_ready == 0) { 715 goto out_xmit; 716 } else if (frames_ready > budget) { 717 completed_frames = budget; 718 work_done = false; 719 } else { 720 completed_frames = frames_ready; 721 } 722 723 ntc = tx_ring->next_to_clean; 724 725 for (i = 0; i < completed_frames; i++) { 726 tx_bi = &tx_ring->tx_bi[ntc]; 727 728 if (tx_bi->xdpf) 729 i40e_clean_xdp_tx_buffer(tx_ring, tx_bi); 730 else 731 xsk_frames++; 732 733 tx_bi->xdpf = NULL; 734 total_bytes += tx_bi->bytecount; 735 736 if (++ntc >= tx_ring->count) 737 ntc = 0; 738 } 739 740 tx_ring->next_to_clean += completed_frames; 741 if (unlikely(tx_ring->next_to_clean >= tx_ring->count)) 742 tx_ring->next_to_clean -= tx_ring->count; 743 744 if (xsk_frames) 745 xsk_umem_complete_tx(umem, xsk_frames); 746 747 i40e_arm_wb(tx_ring, vsi, budget); 748 i40e_update_tx_stats(tx_ring, completed_frames, total_bytes); 749 750 out_xmit: 751 xmit_done = i40e_xmit_zc(tx_ring, budget); 752 753 return work_done && xmit_done; 754 } 755 756 /** 757 * i40e_xsk_async_xmit - Implements the ndo_xsk_async_xmit 758 * @dev: the netdevice 759 * @queue_id: queue id to wake up 760 * 761 * Returns <0 for errors, 0 otherwise. 762 **/ 763 int i40e_xsk_async_xmit(struct net_device *dev, u32 queue_id) 764 { 765 struct i40e_netdev_priv *np = netdev_priv(dev); 766 struct i40e_vsi *vsi = np->vsi; 767 struct i40e_ring *ring; 768 769 if (test_bit(__I40E_VSI_DOWN, vsi->state)) 770 return -ENETDOWN; 771 772 if (!i40e_enabled_xdp_vsi(vsi)) 773 return -ENXIO; 774 775 if (queue_id >= vsi->num_queue_pairs) 776 return -ENXIO; 777 778 if (!vsi->xdp_rings[queue_id]->xsk_umem) 779 return -ENXIO; 780 781 ring = vsi->xdp_rings[queue_id]; 782 783 /* The idea here is that if NAPI is running, mark a miss, so 784 * it will run again. If not, trigger an interrupt and 785 * schedule the NAPI from interrupt context. If NAPI would be 786 * scheduled here, the interrupt affinity would not be 787 * honored. 788 */ 789 if (!napi_if_scheduled_mark_missed(&ring->q_vector->napi)) 790 i40e_force_wb(vsi, ring->q_vector); 791 792 return 0; 793 } 794 795 void i40e_xsk_clean_rx_ring(struct i40e_ring *rx_ring) 796 { 797 u16 i; 798 799 for (i = 0; i < rx_ring->count; i++) { 800 struct i40e_rx_buffer *rx_bi = &rx_ring->rx_bi[i]; 801 802 if (!rx_bi->addr) 803 continue; 804 805 xsk_umem_fq_reuse(rx_ring->xsk_umem, rx_bi->handle); 806 rx_bi->addr = NULL; 807 } 808 } 809 810 /** 811 * i40e_xsk_clean_xdp_ring - Clean the XDP Tx ring on shutdown 812 * @xdp_ring: XDP Tx ring 813 **/ 814 void i40e_xsk_clean_tx_ring(struct i40e_ring *tx_ring) 815 { 816 u16 ntc = tx_ring->next_to_clean, ntu = tx_ring->next_to_use; 817 struct xdp_umem *umem = tx_ring->xsk_umem; 818 struct i40e_tx_buffer *tx_bi; 819 u32 xsk_frames = 0; 820 821 while (ntc != ntu) { 822 tx_bi = &tx_ring->tx_bi[ntc]; 823 824 if (tx_bi->xdpf) 825 i40e_clean_xdp_tx_buffer(tx_ring, tx_bi); 826 else 827 xsk_frames++; 828 829 tx_bi->xdpf = NULL; 830 831 ntc++; 832 if (ntc >= tx_ring->count) 833 ntc = 0; 834 } 835 836 if (xsk_frames) 837 xsk_umem_complete_tx(umem, xsk_frames); 838 } 839 840 /** 841 * i40e_xsk_any_rx_ring_enabled - Checks if Rx rings have AF_XDP UMEM attached 842 * @vsi: vsi 843 * 844 * Returns true if any of the Rx rings has an AF_XDP UMEM attached 845 **/ 846 bool i40e_xsk_any_rx_ring_enabled(struct i40e_vsi *vsi) 847 { 848 struct net_device *netdev = vsi->netdev; 849 int i; 850 851 for (i = 0; i < vsi->num_queue_pairs; i++) { 852 if (xdp_get_umem_from_qid(netdev, i)) 853 return true; 854 } 855 856 return false; 857 } 858