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