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