1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright(c) 2018 Intel Corporation. */ 3 4 #include <linux/bpf_trace.h> 5 #include <linux/unroll.h> 6 #include <net/xdp_sock_drv.h> 7 #include "i40e_txrx_common.h" 8 #include "i40e_xsk.h" 9 10 void i40e_clear_rx_bi_zc(struct i40e_ring *rx_ring) 11 { 12 memset(rx_ring->rx_bi_zc, 0, 13 sizeof(*rx_ring->rx_bi_zc) * rx_ring->count); 14 } 15 16 static struct xdp_buff **i40e_rx_bi(struct i40e_ring *rx_ring, u32 idx) 17 { 18 return &rx_ring->rx_bi_zc[idx]; 19 } 20 21 /** 22 * i40e_realloc_rx_xdp_bi - reallocate SW ring for either XSK or normal buffer 23 * @rx_ring: Current rx ring 24 * @pool_present: is pool for XSK present 25 * 26 * Try allocating memory and return ENOMEM, if failed to allocate. 27 * If allocation was successful, substitute buffer with allocated one. 28 * Returns 0 on success, negative on failure 29 */ 30 static int i40e_realloc_rx_xdp_bi(struct i40e_ring *rx_ring, bool pool_present) 31 { 32 size_t elem_size = pool_present ? sizeof(*rx_ring->rx_bi_zc) : 33 sizeof(*rx_ring->rx_bi); 34 void *sw_ring = kcalloc(rx_ring->count, elem_size, GFP_KERNEL); 35 36 if (!sw_ring) 37 return -ENOMEM; 38 39 if (pool_present) { 40 kfree(rx_ring->rx_bi); 41 rx_ring->rx_bi = NULL; 42 rx_ring->rx_bi_zc = sw_ring; 43 } else { 44 kfree(rx_ring->rx_bi_zc); 45 rx_ring->rx_bi_zc = NULL; 46 rx_ring->rx_bi = sw_ring; 47 } 48 return 0; 49 } 50 51 /** 52 * i40e_realloc_rx_bi_zc - reallocate rx SW rings 53 * @vsi: Current VSI 54 * @zc: is zero copy set 55 * 56 * Reallocate buffer for rx_rings that might be used by XSK. 57 * XDP requires more memory, than rx_buf provides. 58 * Returns 0 on success, negative on failure 59 */ 60 int i40e_realloc_rx_bi_zc(struct i40e_vsi *vsi, bool zc) 61 { 62 struct i40e_ring *rx_ring; 63 unsigned long q; 64 65 for_each_set_bit(q, vsi->af_xdp_zc_qps, vsi->alloc_queue_pairs) { 66 rx_ring = vsi->rx_rings[q]; 67 if (i40e_realloc_rx_xdp_bi(rx_ring, zc)) 68 return -ENOMEM; 69 } 70 return 0; 71 } 72 73 /** 74 * i40e_xsk_pool_enable - Enable/associate an AF_XDP buffer pool to a 75 * certain ring/qid 76 * @vsi: Current VSI 77 * @pool: buffer pool 78 * @qid: Rx ring to associate buffer pool with 79 * 80 * Returns 0 on success, <0 on failure 81 **/ 82 static int i40e_xsk_pool_enable(struct i40e_vsi *vsi, 83 struct xsk_buff_pool *pool, 84 u16 qid) 85 { 86 struct net_device *netdev = vsi->netdev; 87 bool if_running; 88 int err; 89 90 if (vsi->type != I40E_VSI_MAIN) 91 return -EINVAL; 92 93 if (qid >= vsi->num_queue_pairs) 94 return -EINVAL; 95 96 if (qid >= netdev->real_num_rx_queues || 97 qid >= netdev->real_num_tx_queues) 98 return -EINVAL; 99 100 err = xsk_pool_dma_map(pool, &vsi->back->pdev->dev, I40E_RX_DMA_ATTR); 101 if (err) 102 return err; 103 104 set_bit(qid, vsi->af_xdp_zc_qps); 105 106 if_running = netif_running(vsi->netdev) && i40e_enabled_xdp_vsi(vsi); 107 108 if (if_running) { 109 err = i40e_queue_pair_disable(vsi, qid); 110 if (err) 111 return err; 112 113 err = i40e_realloc_rx_xdp_bi(vsi->rx_rings[qid], true); 114 if (err) 115 return err; 116 117 err = i40e_queue_pair_enable(vsi, qid); 118 if (err) 119 return err; 120 121 /* Kick start the NAPI context so that receiving will start */ 122 err = i40e_xsk_wakeup(vsi->netdev, qid, XDP_WAKEUP_RX); 123 if (err) 124 return err; 125 } 126 127 return 0; 128 } 129 130 /** 131 * i40e_xsk_pool_disable - Disassociate an AF_XDP buffer pool from a 132 * certain ring/qid 133 * @vsi: Current VSI 134 * @qid: Rx ring to associate buffer pool with 135 * 136 * Returns 0 on success, <0 on failure 137 **/ 138 static int i40e_xsk_pool_disable(struct i40e_vsi *vsi, u16 qid) 139 { 140 struct net_device *netdev = vsi->netdev; 141 struct xsk_buff_pool *pool; 142 bool if_running; 143 int err; 144 145 pool = xsk_get_pool_from_qid(netdev, qid); 146 if (!pool) 147 return -EINVAL; 148 149 if_running = netif_running(vsi->netdev) && i40e_enabled_xdp_vsi(vsi); 150 151 if (if_running) { 152 err = i40e_queue_pair_disable(vsi, qid); 153 if (err) 154 return err; 155 } 156 157 clear_bit(qid, vsi->af_xdp_zc_qps); 158 xsk_pool_dma_unmap(pool, I40E_RX_DMA_ATTR); 159 160 if (if_running) { 161 err = i40e_realloc_rx_xdp_bi(vsi->rx_rings[qid], false); 162 if (err) 163 return err; 164 err = i40e_queue_pair_enable(vsi, qid); 165 if (err) 166 return err; 167 } 168 169 return 0; 170 } 171 172 /** 173 * i40e_xsk_pool_setup - Enable/disassociate an AF_XDP buffer pool to/from 174 * a ring/qid 175 * @vsi: Current VSI 176 * @pool: Buffer pool to enable/associate to a ring, or NULL to disable 177 * @qid: Rx ring to (dis)associate buffer pool (from)to 178 * 179 * This function enables or disables a buffer pool to a certain ring. 180 * 181 * Returns 0 on success, <0 on failure 182 **/ 183 int i40e_xsk_pool_setup(struct i40e_vsi *vsi, struct xsk_buff_pool *pool, 184 u16 qid) 185 { 186 return pool ? i40e_xsk_pool_enable(vsi, pool, qid) : 187 i40e_xsk_pool_disable(vsi, qid); 188 } 189 190 /** 191 * i40e_run_xdp_zc - Executes an XDP program on an xdp_buff 192 * @rx_ring: Rx ring 193 * @xdp: xdp_buff used as input to the XDP program 194 * @xdp_prog: XDP program to run 195 * 196 * Returns any of I40E_XDP_{PASS, CONSUMED, TX, REDIR} 197 **/ 198 static int i40e_run_xdp_zc(struct i40e_ring *rx_ring, struct xdp_buff *xdp, 199 struct bpf_prog *xdp_prog) 200 { 201 int err, result = I40E_XDP_PASS; 202 struct i40e_ring *xdp_ring; 203 u32 act; 204 205 act = bpf_prog_run_xdp(xdp_prog, xdp); 206 207 if (likely(act == XDP_REDIRECT)) { 208 err = xdp_do_redirect(rx_ring->netdev, xdp, xdp_prog); 209 if (!err) 210 return I40E_XDP_REDIR; 211 if (xsk_uses_need_wakeup(rx_ring->xsk_pool) && err == -ENOBUFS) 212 result = I40E_XDP_EXIT; 213 else 214 result = I40E_XDP_CONSUMED; 215 goto out_failure; 216 } 217 218 switch (act) { 219 case XDP_PASS: 220 break; 221 case XDP_TX: 222 xdp_ring = rx_ring->vsi->xdp_rings[rx_ring->queue_index]; 223 result = i40e_xmit_xdp_tx_ring(xdp, xdp_ring); 224 if (result == I40E_XDP_CONSUMED) 225 goto out_failure; 226 break; 227 case XDP_DROP: 228 result = I40E_XDP_CONSUMED; 229 break; 230 default: 231 bpf_warn_invalid_xdp_action(rx_ring->netdev, xdp_prog, act); 232 fallthrough; 233 case XDP_ABORTED: 234 result = I40E_XDP_CONSUMED; 235 out_failure: 236 trace_xdp_exception(rx_ring->netdev, xdp_prog, act); 237 } 238 return result; 239 } 240 241 bool i40e_alloc_rx_buffers_zc(struct i40e_ring *rx_ring, u16 count) 242 { 243 u16 ntu = rx_ring->next_to_use; 244 union i40e_rx_desc *rx_desc; 245 struct xdp_buff **xdp; 246 u32 nb_buffs, i; 247 dma_addr_t dma; 248 249 XSK_CHECK_PRIV_TYPE(struct i40e_xdp_buff); 250 251 rx_desc = I40E_RX_DESC(rx_ring, ntu); 252 xdp = i40e_rx_bi(rx_ring, ntu); 253 254 nb_buffs = min_t(u16, count, rx_ring->count - ntu); 255 nb_buffs = xsk_buff_alloc_batch(rx_ring->xsk_pool, xdp, nb_buffs); 256 if (!nb_buffs) 257 return false; 258 259 i = nb_buffs; 260 while (i--) { 261 dma = xsk_buff_xdp_get_dma(*xdp); 262 rx_desc->read.pkt_addr = cpu_to_le64(dma); 263 rx_desc->read.hdr_addr = 0; 264 265 rx_desc++; 266 xdp++; 267 } 268 269 ntu += nb_buffs; 270 if (ntu == rx_ring->count) { 271 rx_desc = I40E_RX_DESC(rx_ring, 0); 272 ntu = 0; 273 } 274 275 /* clear the status bits for the next_to_use descriptor */ 276 rx_desc->wb.qword1.status_error_len = 0; 277 i40e_release_rx_desc(rx_ring, ntu); 278 279 return count == nb_buffs; 280 } 281 282 /** 283 * i40e_construct_skb_zc - Create skbuff from zero-copy Rx buffer 284 * @rx_ring: Rx ring 285 * @xdp: xdp_buff 286 * 287 * This functions allocates a new skb from a zero-copy Rx buffer. 288 * 289 * Returns the skb, or NULL on failure. 290 **/ 291 static struct sk_buff *i40e_construct_skb_zc(struct i40e_ring *rx_ring, 292 struct xdp_buff *xdp) 293 { 294 unsigned int totalsize = xdp->data_end - xdp->data_meta; 295 unsigned int metasize = xdp->data - xdp->data_meta; 296 struct skb_shared_info *sinfo = NULL; 297 struct sk_buff *skb; 298 u32 nr_frags = 0; 299 300 if (unlikely(xdp_buff_has_frags(xdp))) { 301 sinfo = xdp_get_shared_info_from_buff(xdp); 302 nr_frags = sinfo->nr_frags; 303 } 304 net_prefetch(xdp->data_meta); 305 306 /* allocate a skb to store the frags */ 307 skb = napi_alloc_skb(&rx_ring->q_vector->napi, totalsize); 308 if (unlikely(!skb)) 309 goto out; 310 311 memcpy(__skb_put(skb, totalsize), xdp->data_meta, 312 ALIGN(totalsize, sizeof(long))); 313 314 if (metasize) { 315 skb_metadata_set(skb, metasize); 316 __skb_pull(skb, metasize); 317 } 318 319 if (likely(!xdp_buff_has_frags(xdp))) 320 goto out; 321 322 for (int i = 0; i < nr_frags; i++) { 323 struct skb_shared_info *skinfo = skb_shinfo(skb); 324 skb_frag_t *frag = &sinfo->frags[i]; 325 struct page *page; 326 void *addr; 327 328 page = dev_alloc_page(); 329 if (!page) { 330 dev_kfree_skb(skb); 331 return NULL; 332 } 333 addr = page_to_virt(page); 334 335 memcpy(addr, skb_frag_page(frag), skb_frag_size(frag)); 336 337 __skb_fill_page_desc_noacc(skinfo, skinfo->nr_frags++, 338 addr, 0, skb_frag_size(frag)); 339 } 340 341 out: 342 xsk_buff_free(xdp); 343 return skb; 344 } 345 346 static void i40e_handle_xdp_result_zc(struct i40e_ring *rx_ring, 347 struct xdp_buff *xdp_buff, 348 union i40e_rx_desc *rx_desc, 349 unsigned int *rx_packets, 350 unsigned int *rx_bytes, 351 unsigned int xdp_res, 352 bool *failure) 353 { 354 struct sk_buff *skb; 355 356 *rx_packets = 1; 357 *rx_bytes = xdp_get_buff_len(xdp_buff); 358 359 if (likely(xdp_res == I40E_XDP_REDIR) || xdp_res == I40E_XDP_TX) 360 return; 361 362 if (xdp_res == I40E_XDP_EXIT) { 363 *failure = true; 364 return; 365 } 366 367 if (xdp_res == I40E_XDP_CONSUMED) { 368 xsk_buff_free(xdp_buff); 369 return; 370 } 371 if (xdp_res == I40E_XDP_PASS) { 372 /* NB! We are not checking for errors using 373 * i40e_test_staterr with 374 * BIT(I40E_RXD_QW1_ERROR_SHIFT). This is due to that 375 * SBP is *not* set in PRT_SBPVSI (default not set). 376 */ 377 skb = i40e_construct_skb_zc(rx_ring, xdp_buff); 378 if (!skb) { 379 rx_ring->rx_stats.alloc_buff_failed++; 380 *rx_packets = 0; 381 *rx_bytes = 0; 382 return; 383 } 384 385 if (eth_skb_pad(skb)) { 386 *rx_packets = 0; 387 *rx_bytes = 0; 388 return; 389 } 390 391 i40e_process_skb_fields(rx_ring, rx_desc, skb); 392 napi_gro_receive(&rx_ring->q_vector->napi, skb); 393 return; 394 } 395 396 /* Should never get here, as all valid cases have been handled already. 397 */ 398 WARN_ON_ONCE(1); 399 } 400 401 static struct i40e_xdp_buff *xsk_buff_to_i40e_ctx(struct xdp_buff *xdp) 402 { 403 /* xdp_buff pointer used by ZC code path is allocated as xdp_buff_xsk. 404 * i40e_xdp_buff private fields overlap with xdp_buff_xsk->cb. 405 */ 406 return (struct i40e_xdp_buff *)xdp; 407 } 408 409 /** 410 * i40e_clean_rx_irq_zc - Consumes Rx packets from the hardware ring 411 * @rx_ring: Rx ring 412 * @budget: NAPI budget 413 * 414 * Returns amount of work completed 415 **/ 416 int i40e_clean_rx_irq_zc(struct i40e_ring *rx_ring, int budget) 417 { 418 unsigned int total_rx_bytes = 0, total_rx_packets = 0; 419 u16 next_to_process = rx_ring->next_to_process; 420 u16 next_to_clean = rx_ring->next_to_clean; 421 unsigned int xdp_res, xdp_xmit = 0; 422 struct xdp_buff *first = NULL; 423 u32 count = rx_ring->count; 424 struct bpf_prog *xdp_prog; 425 u32 entries_to_alloc; 426 bool failure = false; 427 428 if (next_to_process != next_to_clean) 429 first = *i40e_rx_bi(rx_ring, next_to_clean); 430 431 /* NB! xdp_prog will always be !NULL, due to the fact that 432 * this path is enabled by setting an XDP program. 433 */ 434 xdp_prog = READ_ONCE(rx_ring->xdp_prog); 435 436 while (likely(total_rx_packets < (unsigned int)budget)) { 437 union i40e_rx_desc *rx_desc; 438 unsigned int rx_packets; 439 unsigned int rx_bytes; 440 struct xdp_buff *bi; 441 unsigned int size; 442 u64 qword; 443 444 rx_desc = I40E_RX_DESC(rx_ring, next_to_process); 445 qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len); 446 447 /* This memory barrier is needed to keep us from reading 448 * any other fields out of the rx_desc until we have 449 * verified the descriptor has been written back. 450 */ 451 dma_rmb(); 452 453 if (i40e_rx_is_programming_status(qword)) { 454 i40e_clean_programming_status(rx_ring, 455 rx_desc->raw.qword[0], 456 qword); 457 bi = *i40e_rx_bi(rx_ring, next_to_process); 458 xsk_buff_free(bi); 459 if (++next_to_process == count) 460 next_to_process = 0; 461 continue; 462 } 463 464 size = FIELD_GET(I40E_RXD_QW1_LENGTH_PBUF_MASK, qword); 465 if (!size) 466 break; 467 468 bi = *i40e_rx_bi(rx_ring, next_to_process); 469 xsk_buff_set_size(bi, size); 470 xsk_buff_dma_sync_for_cpu(bi); 471 472 if (!first) 473 first = bi; 474 else if (!xsk_buff_add_frag(first, bi)) { 475 xsk_buff_free(first); 476 break; 477 } 478 479 if (++next_to_process == count) 480 next_to_process = 0; 481 482 if (i40e_is_non_eop(rx_ring, rx_desc)) 483 continue; 484 485 xsk_buff_to_i40e_ctx(first)->desc = rx_desc; 486 487 xdp_res = i40e_run_xdp_zc(rx_ring, first, xdp_prog); 488 i40e_handle_xdp_result_zc(rx_ring, first, rx_desc, &rx_packets, 489 &rx_bytes, xdp_res, &failure); 490 next_to_clean = next_to_process; 491 if (failure) 492 break; 493 total_rx_packets += rx_packets; 494 total_rx_bytes += rx_bytes; 495 xdp_xmit |= xdp_res & (I40E_XDP_TX | I40E_XDP_REDIR); 496 first = NULL; 497 } 498 499 rx_ring->next_to_clean = next_to_clean; 500 rx_ring->next_to_process = next_to_process; 501 502 entries_to_alloc = I40E_DESC_UNUSED(rx_ring); 503 if (entries_to_alloc >= I40E_RX_BUFFER_WRITE) 504 failure |= !i40e_alloc_rx_buffers_zc(rx_ring, entries_to_alloc); 505 506 i40e_finalize_xdp_rx(rx_ring, xdp_xmit); 507 i40e_update_rx_stats(rx_ring, total_rx_bytes, total_rx_packets); 508 509 if (xsk_uses_need_wakeup(rx_ring->xsk_pool)) { 510 if (failure || next_to_clean == rx_ring->next_to_use) 511 xsk_set_rx_need_wakeup(rx_ring->xsk_pool); 512 else 513 xsk_clear_rx_need_wakeup(rx_ring->xsk_pool); 514 515 return (int)total_rx_packets; 516 } 517 return failure ? budget : (int)total_rx_packets; 518 } 519 520 static void i40e_xmit_pkt(struct i40e_ring *xdp_ring, struct xdp_desc *desc, 521 unsigned int *total_bytes) 522 { 523 u32 cmd = I40E_TX_DESC_CMD_ICRC | xsk_is_eop_desc(desc); 524 struct i40e_tx_desc *tx_desc; 525 dma_addr_t dma; 526 527 dma = xsk_buff_raw_get_dma(xdp_ring->xsk_pool, desc->addr); 528 xsk_buff_raw_dma_sync_for_device(xdp_ring->xsk_pool, dma, desc->len); 529 530 tx_desc = I40E_TX_DESC(xdp_ring, xdp_ring->next_to_use++); 531 tx_desc->buffer_addr = cpu_to_le64(dma); 532 tx_desc->cmd_type_offset_bsz = build_ctob(cmd, 0, desc->len, 0); 533 534 *total_bytes += desc->len; 535 } 536 537 static void i40e_xmit_pkt_batch(struct i40e_ring *xdp_ring, struct xdp_desc *desc, 538 unsigned int *total_bytes) 539 { 540 u16 ntu = xdp_ring->next_to_use; 541 struct i40e_tx_desc *tx_desc; 542 dma_addr_t dma; 543 u32 i; 544 545 unrolled_count(PKTS_PER_BATCH) 546 for (i = 0; i < PKTS_PER_BATCH; i++) { 547 u32 cmd = I40E_TX_DESC_CMD_ICRC | xsk_is_eop_desc(&desc[i]); 548 549 dma = xsk_buff_raw_get_dma(xdp_ring->xsk_pool, desc[i].addr); 550 xsk_buff_raw_dma_sync_for_device(xdp_ring->xsk_pool, dma, desc[i].len); 551 552 tx_desc = I40E_TX_DESC(xdp_ring, ntu++); 553 tx_desc->buffer_addr = cpu_to_le64(dma); 554 tx_desc->cmd_type_offset_bsz = build_ctob(cmd, 0, desc[i].len, 0); 555 556 *total_bytes += desc[i].len; 557 } 558 559 xdp_ring->next_to_use = ntu; 560 } 561 562 static void i40e_fill_tx_hw_ring(struct i40e_ring *xdp_ring, struct xdp_desc *descs, u32 nb_pkts, 563 unsigned int *total_bytes) 564 { 565 u32 batched, leftover, i; 566 567 batched = nb_pkts & ~(PKTS_PER_BATCH - 1); 568 leftover = nb_pkts & (PKTS_PER_BATCH - 1); 569 for (i = 0; i < batched; i += PKTS_PER_BATCH) 570 i40e_xmit_pkt_batch(xdp_ring, &descs[i], total_bytes); 571 for (i = batched; i < batched + leftover; i++) 572 i40e_xmit_pkt(xdp_ring, &descs[i], total_bytes); 573 } 574 575 static void i40e_set_rs_bit(struct i40e_ring *xdp_ring) 576 { 577 u16 ntu = xdp_ring->next_to_use ? xdp_ring->next_to_use - 1 : xdp_ring->count - 1; 578 struct i40e_tx_desc *tx_desc; 579 580 tx_desc = I40E_TX_DESC(xdp_ring, ntu); 581 tx_desc->cmd_type_offset_bsz |= cpu_to_le64(I40E_TX_DESC_CMD_RS << I40E_TXD_QW1_CMD_SHIFT); 582 } 583 584 /** 585 * i40e_xmit_zc - Performs zero-copy Tx AF_XDP 586 * @xdp_ring: XDP Tx ring 587 * @budget: NAPI budget 588 * 589 * Returns true if the work is finished. 590 **/ 591 static bool i40e_xmit_zc(struct i40e_ring *xdp_ring, unsigned int budget) 592 { 593 struct xdp_desc *descs = xdp_ring->xsk_pool->tx_descs; 594 u32 nb_pkts, nb_processed = 0; 595 unsigned int total_bytes = 0; 596 597 nb_pkts = xsk_tx_peek_release_desc_batch(xdp_ring->xsk_pool, budget); 598 if (!nb_pkts) 599 return true; 600 601 if (xdp_ring->next_to_use + nb_pkts >= xdp_ring->count) { 602 nb_processed = xdp_ring->count - xdp_ring->next_to_use; 603 i40e_fill_tx_hw_ring(xdp_ring, descs, nb_processed, &total_bytes); 604 xdp_ring->next_to_use = 0; 605 } 606 607 i40e_fill_tx_hw_ring(xdp_ring, &descs[nb_processed], nb_pkts - nb_processed, 608 &total_bytes); 609 610 /* Request an interrupt for the last frame and bump tail ptr. */ 611 i40e_set_rs_bit(xdp_ring); 612 i40e_xdp_ring_update_tail(xdp_ring); 613 614 i40e_update_tx_stats(xdp_ring, nb_pkts, total_bytes); 615 616 return nb_pkts < budget; 617 } 618 619 /** 620 * i40e_clean_xdp_tx_buffer - Frees and unmaps an XDP Tx entry 621 * @tx_ring: XDP Tx ring 622 * @tx_bi: Tx buffer info to clean 623 **/ 624 static void i40e_clean_xdp_tx_buffer(struct i40e_ring *tx_ring, 625 struct i40e_tx_buffer *tx_bi) 626 { 627 xdp_return_frame(tx_bi->xdpf); 628 tx_ring->xdp_tx_active--; 629 dma_unmap_single(tx_ring->dev, 630 dma_unmap_addr(tx_bi, dma), 631 dma_unmap_len(tx_bi, len), DMA_TO_DEVICE); 632 dma_unmap_len_set(tx_bi, len, 0); 633 } 634 635 /** 636 * i40e_clean_xdp_tx_irq - Completes AF_XDP entries, and cleans XDP entries 637 * @vsi: Current VSI 638 * @tx_ring: XDP Tx ring 639 * 640 * Returns true if cleanup/transmission is done. 641 **/ 642 bool i40e_clean_xdp_tx_irq(struct i40e_vsi *vsi, struct i40e_ring *tx_ring) 643 { 644 struct xsk_buff_pool *bp = tx_ring->xsk_pool; 645 u32 i, completed_frames, xsk_frames = 0; 646 u32 head_idx = i40e_get_head(tx_ring); 647 struct i40e_tx_buffer *tx_bi; 648 unsigned int ntc; 649 650 if (head_idx < tx_ring->next_to_clean) 651 head_idx += tx_ring->count; 652 completed_frames = head_idx - tx_ring->next_to_clean; 653 654 if (completed_frames == 0) 655 goto out_xmit; 656 657 if (likely(!tx_ring->xdp_tx_active)) { 658 xsk_frames = completed_frames; 659 goto skip; 660 } 661 662 ntc = tx_ring->next_to_clean; 663 664 for (i = 0; i < completed_frames; i++) { 665 tx_bi = &tx_ring->tx_bi[ntc]; 666 667 if (tx_bi->xdpf) { 668 i40e_clean_xdp_tx_buffer(tx_ring, tx_bi); 669 tx_bi->xdpf = NULL; 670 } else { 671 xsk_frames++; 672 } 673 674 if (++ntc >= tx_ring->count) 675 ntc = 0; 676 } 677 678 skip: 679 tx_ring->next_to_clean += completed_frames; 680 if (unlikely(tx_ring->next_to_clean >= tx_ring->count)) 681 tx_ring->next_to_clean -= tx_ring->count; 682 683 if (xsk_frames) 684 xsk_tx_completed(bp, xsk_frames); 685 686 i40e_arm_wb(tx_ring, vsi, completed_frames); 687 688 out_xmit: 689 if (xsk_uses_need_wakeup(tx_ring->xsk_pool)) 690 xsk_set_tx_need_wakeup(tx_ring->xsk_pool); 691 692 return i40e_xmit_zc(tx_ring, I40E_DESC_UNUSED(tx_ring)); 693 } 694 695 /** 696 * i40e_xsk_wakeup - Implements the ndo_xsk_wakeup 697 * @dev: the netdevice 698 * @queue_id: queue id to wake up 699 * @flags: ignored in our case since we have Rx and Tx in the same NAPI. 700 * 701 * Returns <0 for errors, 0 otherwise. 702 **/ 703 int i40e_xsk_wakeup(struct net_device *dev, u32 queue_id, u32 flags) 704 { 705 struct i40e_netdev_priv *np = netdev_priv(dev); 706 struct i40e_vsi *vsi = np->vsi; 707 struct i40e_pf *pf = vsi->back; 708 struct i40e_ring *ring; 709 710 if (test_bit(__I40E_CONFIG_BUSY, pf->state)) 711 return -EAGAIN; 712 713 if (test_bit(__I40E_VSI_DOWN, vsi->state)) 714 return -ENETDOWN; 715 716 if (!i40e_enabled_xdp_vsi(vsi)) 717 return -EINVAL; 718 719 if (queue_id >= vsi->num_queue_pairs) 720 return -EINVAL; 721 722 if (!vsi->xdp_rings[queue_id]->xsk_pool) 723 return -EINVAL; 724 725 ring = vsi->xdp_rings[queue_id]; 726 727 /* The idea here is that if NAPI is running, mark a miss, so 728 * it will run again. If not, trigger an interrupt and 729 * schedule the NAPI from interrupt context. If NAPI would be 730 * scheduled here, the interrupt affinity would not be 731 * honored. 732 */ 733 if (!napi_if_scheduled_mark_missed(&ring->q_vector->napi)) 734 i40e_force_wb(vsi, ring->q_vector); 735 736 return 0; 737 } 738 739 void i40e_xsk_clean_rx_ring(struct i40e_ring *rx_ring) 740 { 741 u16 ntc = rx_ring->next_to_clean; 742 u16 ntu = rx_ring->next_to_use; 743 744 while (ntc != ntu) { 745 struct xdp_buff *rx_bi = *i40e_rx_bi(rx_ring, ntc); 746 747 xsk_buff_free(rx_bi); 748 ntc++; 749 if (ntc >= rx_ring->count) 750 ntc = 0; 751 } 752 } 753 754 /** 755 * i40e_xsk_clean_tx_ring - Clean the XDP Tx ring on shutdown 756 * @tx_ring: XDP Tx ring 757 **/ 758 void i40e_xsk_clean_tx_ring(struct i40e_ring *tx_ring) 759 { 760 u16 ntc = tx_ring->next_to_clean, ntu = tx_ring->next_to_use; 761 struct xsk_buff_pool *bp = tx_ring->xsk_pool; 762 struct i40e_tx_buffer *tx_bi; 763 u32 xsk_frames = 0; 764 765 while (ntc != ntu) { 766 tx_bi = &tx_ring->tx_bi[ntc]; 767 768 if (tx_bi->xdpf) 769 i40e_clean_xdp_tx_buffer(tx_ring, tx_bi); 770 else 771 xsk_frames++; 772 773 tx_bi->xdpf = NULL; 774 775 ntc++; 776 if (ntc >= tx_ring->count) 777 ntc = 0; 778 } 779 780 if (xsk_frames) 781 xsk_tx_completed(bp, xsk_frames); 782 } 783 784 /** 785 * i40e_xsk_any_rx_ring_enabled - Checks if Rx rings have an AF_XDP 786 * buffer pool attached 787 * @vsi: vsi 788 * 789 * Returns true if any of the Rx rings has an AF_XDP buffer pool attached 790 **/ 791 bool i40e_xsk_any_rx_ring_enabled(struct i40e_vsi *vsi) 792 { 793 struct net_device *netdev = vsi->netdev; 794 int i; 795 796 for (i = 0; i < vsi->num_queue_pairs; i++) { 797 if (xsk_get_pool_from_qid(netdev, i)) 798 return true; 799 } 800 801 return false; 802 } 803