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