1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2018, Intel Corporation. */ 3 4 /* The driver transmit and receive code */ 5 6 #include <linux/mm.h> 7 #include <linux/netdevice.h> 8 #include <linux/prefetch.h> 9 #include <linux/bpf_trace.h> 10 #include <linux/net/intel/libie/rx.h> 11 #include <net/libeth/xdp.h> 12 #include <net/dsfield.h> 13 #include <net/mpls.h> 14 #include <net/xdp.h> 15 #include "ice_txrx_lib.h" 16 #include "ice_lib.h" 17 #include "ice.h" 18 #include "ice_trace.h" 19 #include "ice_dcb_lib.h" 20 #include "ice_xsk.h" 21 #include "ice_eswitch.h" 22 23 #define ICE_RX_HDR_SIZE 256 24 25 #define ICE_FDIR_CLEAN_DELAY 10 26 27 /** 28 * ice_prgm_fdir_fltr - Program a Flow Director filter 29 * @vsi: VSI to send dummy packet 30 * @fdir_desc: flow director descriptor 31 * @raw_packet: allocated buffer for flow director 32 */ 33 int 34 ice_prgm_fdir_fltr(struct ice_vsi *vsi, struct ice_fltr_desc *fdir_desc, 35 u8 *raw_packet) 36 { 37 struct ice_tx_buf *tx_buf, *first; 38 struct ice_fltr_desc *f_desc; 39 struct ice_tx_desc *tx_desc; 40 struct ice_tx_ring *tx_ring; 41 struct device *dev; 42 dma_addr_t dma; 43 u32 td_cmd; 44 u16 i; 45 46 /* VSI and Tx ring */ 47 if (!vsi) 48 return -ENOENT; 49 tx_ring = vsi->tx_rings[0]; 50 if (!tx_ring || !tx_ring->desc) 51 return -ENOENT; 52 dev = tx_ring->dev; 53 54 /* we are using two descriptors to add/del a filter and we can wait */ 55 for (i = ICE_FDIR_CLEAN_DELAY; ICE_DESC_UNUSED(tx_ring) < 2; i--) { 56 if (!i) 57 return -EAGAIN; 58 msleep_interruptible(1); 59 } 60 61 dma = dma_map_single(dev, raw_packet, ICE_FDIR_MAX_RAW_PKT_SIZE, 62 DMA_TO_DEVICE); 63 64 if (dma_mapping_error(dev, dma)) 65 return -EINVAL; 66 67 /* grab the next descriptor */ 68 i = tx_ring->next_to_use; 69 first = &tx_ring->tx_buf[i]; 70 f_desc = ICE_TX_FDIRDESC(tx_ring, i); 71 memcpy(f_desc, fdir_desc, sizeof(*f_desc)); 72 73 i++; 74 i = (i < tx_ring->count) ? i : 0; 75 tx_desc = ICE_TX_DESC(tx_ring, i); 76 tx_buf = &tx_ring->tx_buf[i]; 77 78 i++; 79 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0; 80 81 memset(tx_buf, 0, sizeof(*tx_buf)); 82 dma_unmap_len_set(tx_buf, len, ICE_FDIR_MAX_RAW_PKT_SIZE); 83 dma_unmap_addr_set(tx_buf, dma, dma); 84 85 tx_desc->buf_addr = cpu_to_le64(dma); 86 td_cmd = ICE_TXD_LAST_DESC_CMD | ICE_TX_DESC_CMD_DUMMY | 87 ICE_TX_DESC_CMD_RE; 88 89 tx_buf->type = ICE_TX_BUF_DUMMY; 90 tx_buf->raw_buf = raw_packet; 91 92 tx_desc->cmd_type_offset_bsz = 93 ice_build_ctob(td_cmd, 0, ICE_FDIR_MAX_RAW_PKT_SIZE, 0); 94 95 /* Force memory write to complete before letting h/w know 96 * there are new descriptors to fetch. 97 */ 98 wmb(); 99 100 /* mark the data descriptor to be watched */ 101 first->next_to_watch = tx_desc; 102 103 writel(tx_ring->next_to_use, tx_ring->tail); 104 105 return 0; 106 } 107 108 /** 109 * ice_unmap_and_free_tx_buf - Release a Tx buffer 110 * @ring: the ring that owns the buffer 111 * @tx_buf: the buffer to free 112 */ 113 static void 114 ice_unmap_and_free_tx_buf(struct ice_tx_ring *ring, struct ice_tx_buf *tx_buf) 115 { 116 if (tx_buf->type != ICE_TX_BUF_XDP_TX && dma_unmap_len(tx_buf, len)) 117 dma_unmap_page(ring->dev, 118 dma_unmap_addr(tx_buf, dma), 119 dma_unmap_len(tx_buf, len), 120 DMA_TO_DEVICE); 121 122 switch (tx_buf->type) { 123 case ICE_TX_BUF_DUMMY: 124 devm_kfree(ring->dev, tx_buf->raw_buf); 125 break; 126 case ICE_TX_BUF_SKB: 127 dev_kfree_skb_any(tx_buf->skb); 128 break; 129 case ICE_TX_BUF_XDP_TX: 130 libeth_xdp_return_va(tx_buf->raw_buf, false); 131 break; 132 case ICE_TX_BUF_XDP_XMIT: 133 xdp_return_frame(tx_buf->xdpf); 134 break; 135 } 136 137 tx_buf->next_to_watch = NULL; 138 tx_buf->type = ICE_TX_BUF_EMPTY; 139 dma_unmap_len_set(tx_buf, len, 0); 140 /* tx_buf must be completely set up in the transmit path */ 141 } 142 143 static struct netdev_queue *txring_txq(const struct ice_tx_ring *ring) 144 { 145 return netdev_get_tx_queue(ring->netdev, ring->q_index); 146 } 147 148 /** 149 * ice_clean_tstamp_ring - clean time stamp ring 150 * @tx_ring: Tx ring to clean the Time Stamp ring for 151 */ 152 static void ice_clean_tstamp_ring(struct ice_tx_ring *tx_ring) 153 { 154 struct ice_tstamp_ring *tstamp_ring = tx_ring->tstamp_ring; 155 u32 size; 156 157 if (!tstamp_ring->desc) 158 return; 159 160 size = ALIGN(tstamp_ring->count * sizeof(struct ice_ts_desc), 161 PAGE_SIZE); 162 memset(tstamp_ring->desc, 0, size); 163 tstamp_ring->next_to_use = 0; 164 } 165 166 /** 167 * ice_free_tstamp_ring - free time stamp resources per queue 168 * @tx_ring: Tx ring to free the Time Stamp ring for 169 */ 170 void ice_free_tstamp_ring(struct ice_tx_ring *tx_ring) 171 { 172 struct ice_tstamp_ring *tstamp_ring = tx_ring->tstamp_ring; 173 u32 size; 174 175 if (!tstamp_ring->desc) 176 return; 177 178 ice_clean_tstamp_ring(tx_ring); 179 size = ALIGN(tstamp_ring->count * sizeof(struct ice_ts_desc), 180 PAGE_SIZE); 181 dmam_free_coherent(tx_ring->dev, size, tstamp_ring->desc, 182 tstamp_ring->dma); 183 tstamp_ring->desc = NULL; 184 } 185 186 /** 187 * ice_free_tx_tstamp_ring - free time stamp resources per Tx ring 188 * @tx_ring: Tx ring to free the Time Stamp ring for 189 */ 190 void ice_free_tx_tstamp_ring(struct ice_tx_ring *tx_ring) 191 { 192 ice_free_tstamp_ring(tx_ring); 193 kfree_rcu(tx_ring->tstamp_ring, rcu); 194 tx_ring->tstamp_ring = NULL; 195 tx_ring->flags &= ~ICE_TX_FLAGS_TXTIME; 196 } 197 198 /** 199 * ice_clean_tx_ring - Free any empty Tx buffers 200 * @tx_ring: ring to be cleaned 201 */ 202 void ice_clean_tx_ring(struct ice_tx_ring *tx_ring) 203 { 204 u32 size; 205 u16 i; 206 207 if (ice_ring_is_xdp(tx_ring) && tx_ring->xsk_pool) { 208 ice_xsk_clean_xdp_ring(tx_ring); 209 goto tx_skip_free; 210 } 211 212 /* ring already cleared, nothing to do */ 213 if (!tx_ring->tx_buf) 214 return; 215 216 /* Free all the Tx ring sk_buffs */ 217 for (i = 0; i < tx_ring->count; i++) 218 ice_unmap_and_free_tx_buf(tx_ring, &tx_ring->tx_buf[i]); 219 220 tx_skip_free: 221 memset(tx_ring->tx_buf, 0, sizeof(*tx_ring->tx_buf) * tx_ring->count); 222 223 size = ALIGN(tx_ring->count * sizeof(struct ice_tx_desc), 224 PAGE_SIZE); 225 /* Zero out the descriptor ring */ 226 memset(tx_ring->desc, 0, size); 227 228 tx_ring->next_to_use = 0; 229 tx_ring->next_to_clean = 0; 230 231 if (!tx_ring->netdev) 232 return; 233 234 /* cleanup Tx queue statistics */ 235 netdev_tx_reset_queue(txring_txq(tx_ring)); 236 237 if (ice_is_txtime_cfg(tx_ring)) 238 ice_free_tx_tstamp_ring(tx_ring); 239 } 240 241 /** 242 * ice_free_tx_ring - Free Tx resources per queue 243 * @tx_ring: Tx descriptor ring for a specific queue 244 * 245 * Free all transmit software resources 246 */ 247 void ice_free_tx_ring(struct ice_tx_ring *tx_ring) 248 { 249 u32 size; 250 251 ice_clean_tx_ring(tx_ring); 252 devm_kfree(tx_ring->dev, tx_ring->tx_buf); 253 tx_ring->tx_buf = NULL; 254 255 if (tx_ring->desc) { 256 size = ALIGN(tx_ring->count * sizeof(struct ice_tx_desc), 257 PAGE_SIZE); 258 dmam_free_coherent(tx_ring->dev, size, 259 tx_ring->desc, tx_ring->dma); 260 tx_ring->desc = NULL; 261 } 262 } 263 264 /** 265 * ice_clean_tx_irq - Reclaim resources after transmit completes 266 * @tx_ring: Tx ring to clean 267 * @napi_budget: Used to determine if we are in netpoll 268 * 269 * Returns true if there's any budget left (e.g. the clean is finished) 270 */ 271 static bool ice_clean_tx_irq(struct ice_tx_ring *tx_ring, int napi_budget) 272 { 273 unsigned int total_bytes = 0, total_pkts = 0; 274 unsigned int budget = ICE_DFLT_IRQ_WORK; 275 struct ice_vsi *vsi = tx_ring->vsi; 276 s16 i = tx_ring->next_to_clean; 277 struct ice_tx_desc *tx_desc; 278 struct ice_tx_buf *tx_buf; 279 280 /* get the bql data ready */ 281 netdev_txq_bql_complete_prefetchw(txring_txq(tx_ring)); 282 283 tx_buf = &tx_ring->tx_buf[i]; 284 tx_desc = ICE_TX_DESC(tx_ring, i); 285 i -= tx_ring->count; 286 287 prefetch(&vsi->state); 288 289 do { 290 struct ice_tx_desc *eop_desc = tx_buf->next_to_watch; 291 292 /* if next_to_watch is not set then there is no work pending */ 293 if (!eop_desc) 294 break; 295 296 /* follow the guidelines of other drivers */ 297 prefetchw(&tx_buf->skb->users); 298 299 smp_rmb(); /* prevent any other reads prior to eop_desc */ 300 301 ice_trace(clean_tx_irq, tx_ring, tx_desc, tx_buf); 302 /* if the descriptor isn't done, no work yet to do */ 303 if (!(eop_desc->cmd_type_offset_bsz & 304 cpu_to_le64(ICE_TX_DESC_DTYPE_DESC_DONE))) 305 break; 306 307 /* clear next_to_watch to prevent false hangs */ 308 tx_buf->next_to_watch = NULL; 309 310 /* update the statistics for this packet */ 311 total_bytes += tx_buf->bytecount; 312 total_pkts += tx_buf->gso_segs; 313 314 /* free the skb */ 315 napi_consume_skb(tx_buf->skb, napi_budget); 316 317 /* unmap skb header data */ 318 dma_unmap_single(tx_ring->dev, 319 dma_unmap_addr(tx_buf, dma), 320 dma_unmap_len(tx_buf, len), 321 DMA_TO_DEVICE); 322 323 /* clear tx_buf data */ 324 tx_buf->type = ICE_TX_BUF_EMPTY; 325 dma_unmap_len_set(tx_buf, len, 0); 326 327 /* unmap remaining buffers */ 328 while (tx_desc != eop_desc) { 329 ice_trace(clean_tx_irq_unmap, tx_ring, tx_desc, tx_buf); 330 tx_buf++; 331 tx_desc++; 332 i++; 333 if (unlikely(!i)) { 334 i -= tx_ring->count; 335 tx_buf = tx_ring->tx_buf; 336 tx_desc = ICE_TX_DESC(tx_ring, 0); 337 } 338 339 /* unmap any remaining paged data */ 340 if (dma_unmap_len(tx_buf, len)) { 341 dma_unmap_page(tx_ring->dev, 342 dma_unmap_addr(tx_buf, dma), 343 dma_unmap_len(tx_buf, len), 344 DMA_TO_DEVICE); 345 dma_unmap_len_set(tx_buf, len, 0); 346 } 347 } 348 ice_trace(clean_tx_irq_unmap_eop, tx_ring, tx_desc, tx_buf); 349 350 /* move us one more past the eop_desc for start of next pkt */ 351 tx_buf++; 352 tx_desc++; 353 i++; 354 if (unlikely(!i)) { 355 i -= tx_ring->count; 356 tx_buf = tx_ring->tx_buf; 357 tx_desc = ICE_TX_DESC(tx_ring, 0); 358 } 359 360 prefetch(tx_desc); 361 362 /* update budget accounting */ 363 budget--; 364 } while (likely(budget)); 365 366 i += tx_ring->count; 367 tx_ring->next_to_clean = i; 368 369 ice_update_tx_ring_stats(tx_ring, total_pkts, total_bytes); 370 netdev_tx_completed_queue(txring_txq(tx_ring), total_pkts, total_bytes); 371 372 #define TX_WAKE_THRESHOLD ((s16)(DESC_NEEDED * 2)) 373 if (unlikely(total_pkts && netif_carrier_ok(tx_ring->netdev) && 374 (ICE_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) { 375 /* Make sure that anybody stopping the queue after this 376 * sees the new next_to_clean. 377 */ 378 smp_mb(); 379 if (netif_tx_queue_stopped(txring_txq(tx_ring)) && 380 !test_bit(ICE_VSI_DOWN, vsi->state)) { 381 netif_tx_wake_queue(txring_txq(tx_ring)); 382 ++tx_ring->ring_stats->tx_stats.restart_q; 383 } 384 } 385 386 return !!budget; 387 } 388 389 /** 390 * ice_alloc_tstamp_ring - allocate the Time Stamp ring 391 * @tx_ring: Tx ring to allocate the Time Stamp ring for 392 * 393 * Return: 0 on success, negative on error 394 */ 395 static int ice_alloc_tstamp_ring(struct ice_tx_ring *tx_ring) 396 { 397 struct ice_tstamp_ring *tstamp_ring; 398 399 /* allocate with kzalloc(), free with kfree_rcu() */ 400 tstamp_ring = kzalloc(sizeof(*tstamp_ring), GFP_KERNEL); 401 if (!tstamp_ring) 402 return -ENOMEM; 403 404 tstamp_ring->tx_ring = tx_ring; 405 tx_ring->tstamp_ring = tstamp_ring; 406 tstamp_ring->desc = NULL; 407 tstamp_ring->count = ice_calc_ts_ring_count(tx_ring); 408 tx_ring->flags |= ICE_TX_FLAGS_TXTIME; 409 return 0; 410 } 411 412 /** 413 * ice_setup_tstamp_ring - allocate the Time Stamp ring 414 * @tx_ring: Tx ring to set up the Time Stamp ring for 415 * 416 * Return: 0 on success, negative on error 417 */ 418 static int ice_setup_tstamp_ring(struct ice_tx_ring *tx_ring) 419 { 420 struct ice_tstamp_ring *tstamp_ring = tx_ring->tstamp_ring; 421 struct device *dev = tx_ring->dev; 422 u32 size; 423 424 /* round up to nearest page */ 425 size = ALIGN(tstamp_ring->count * sizeof(struct ice_ts_desc), 426 PAGE_SIZE); 427 tstamp_ring->desc = dmam_alloc_coherent(dev, size, &tstamp_ring->dma, 428 GFP_KERNEL); 429 if (!tstamp_ring->desc) { 430 dev_err(dev, "Unable to allocate memory for Time stamp Ring, size=%d\n", 431 size); 432 return -ENOMEM; 433 } 434 435 tstamp_ring->next_to_use = 0; 436 return 0; 437 } 438 439 /** 440 * ice_alloc_setup_tstamp_ring - Allocate and setup the Time Stamp ring 441 * @tx_ring: Tx ring to allocate and setup the Time Stamp ring for 442 * 443 * Return: 0 on success, negative on error 444 */ 445 int ice_alloc_setup_tstamp_ring(struct ice_tx_ring *tx_ring) 446 { 447 struct device *dev = tx_ring->dev; 448 int err; 449 450 err = ice_alloc_tstamp_ring(tx_ring); 451 if (err) { 452 dev_err(dev, "Unable to allocate Time stamp ring for Tx ring %d\n", 453 tx_ring->q_index); 454 return err; 455 } 456 457 err = ice_setup_tstamp_ring(tx_ring); 458 if (err) { 459 dev_err(dev, "Unable to setup Time stamp ring for Tx ring %d\n", 460 tx_ring->q_index); 461 ice_free_tx_tstamp_ring(tx_ring); 462 return err; 463 } 464 return 0; 465 } 466 467 /** 468 * ice_setup_tx_ring - Allocate the Tx descriptors 469 * @tx_ring: the Tx ring to set up 470 * 471 * Return 0 on success, negative on error 472 */ 473 int ice_setup_tx_ring(struct ice_tx_ring *tx_ring) 474 { 475 struct device *dev = tx_ring->dev; 476 u32 size; 477 478 if (!dev) 479 return -ENOMEM; 480 481 /* warn if we are about to overwrite the pointer */ 482 WARN_ON(tx_ring->tx_buf); 483 tx_ring->tx_buf = 484 devm_kcalloc(dev, sizeof(*tx_ring->tx_buf), tx_ring->count, 485 GFP_KERNEL); 486 if (!tx_ring->tx_buf) 487 return -ENOMEM; 488 489 /* round up to nearest page */ 490 size = ALIGN(tx_ring->count * sizeof(struct ice_tx_desc), 491 PAGE_SIZE); 492 tx_ring->desc = dmam_alloc_coherent(dev, size, &tx_ring->dma, 493 GFP_KERNEL); 494 if (!tx_ring->desc) { 495 dev_err(dev, "Unable to allocate memory for the Tx descriptor ring, size=%d\n", 496 size); 497 goto err; 498 } 499 500 tx_ring->next_to_use = 0; 501 tx_ring->next_to_clean = 0; 502 tx_ring->ring_stats->tx_stats.prev_pkt = -1; 503 return 0; 504 505 err: 506 devm_kfree(dev, tx_ring->tx_buf); 507 tx_ring->tx_buf = NULL; 508 return -ENOMEM; 509 } 510 511 void ice_rxq_pp_destroy(struct ice_rx_ring *rq) 512 { 513 struct libeth_fq fq = { 514 .fqes = rq->rx_fqes, 515 .pp = rq->pp, 516 }; 517 518 libeth_rx_fq_destroy(&fq); 519 rq->rx_fqes = NULL; 520 rq->pp = NULL; 521 522 if (!rq->hdr_pp) 523 return; 524 525 fq.fqes = rq->hdr_fqes; 526 fq.pp = rq->hdr_pp; 527 528 libeth_rx_fq_destroy(&fq); 529 rq->hdr_fqes = NULL; 530 rq->hdr_pp = NULL; 531 } 532 533 /** 534 * ice_clean_rx_ring - Free Rx buffers 535 * @rx_ring: ring to be cleaned 536 */ 537 void ice_clean_rx_ring(struct ice_rx_ring *rx_ring) 538 { 539 u32 size; 540 541 if (rx_ring->xsk_pool) { 542 ice_xsk_clean_rx_ring(rx_ring); 543 goto rx_skip_free; 544 } 545 546 /* ring already cleared, nothing to do */ 547 if (!rx_ring->rx_fqes) 548 return; 549 550 libeth_xdp_return_stash(&rx_ring->xdp); 551 552 /* Free all the Rx ring sk_buffs */ 553 for (u32 i = rx_ring->next_to_clean; i != rx_ring->next_to_use; ) { 554 libeth_rx_recycle_slow(rx_ring->rx_fqes[i].netmem); 555 556 if (rx_ring->hdr_pp) 557 libeth_rx_recycle_slow(rx_ring->hdr_fqes[i].netmem); 558 559 if (unlikely(++i == rx_ring->count)) 560 i = 0; 561 } 562 563 if (rx_ring->vsi->type == ICE_VSI_PF && 564 xdp_rxq_info_is_reg(&rx_ring->xdp_rxq)) { 565 xdp_rxq_info_detach_mem_model(&rx_ring->xdp_rxq); 566 xdp_rxq_info_unreg(&rx_ring->xdp_rxq); 567 } 568 569 ice_rxq_pp_destroy(rx_ring); 570 571 rx_skip_free: 572 /* Zero out the descriptor ring */ 573 size = ALIGN(rx_ring->count * sizeof(union ice_32byte_rx_desc), 574 PAGE_SIZE); 575 memset(rx_ring->desc, 0, size); 576 577 rx_ring->next_to_alloc = 0; 578 rx_ring->next_to_clean = 0; 579 rx_ring->next_to_use = 0; 580 } 581 582 /** 583 * ice_free_rx_ring - Free Rx resources 584 * @rx_ring: ring to clean the resources from 585 * 586 * Free all receive software resources 587 */ 588 void ice_free_rx_ring(struct ice_rx_ring *rx_ring) 589 { 590 struct device *dev = ice_pf_to_dev(rx_ring->vsi->back); 591 u32 size; 592 593 ice_clean_rx_ring(rx_ring); 594 WRITE_ONCE(rx_ring->xdp_prog, NULL); 595 if (rx_ring->xsk_pool) { 596 kfree(rx_ring->xdp_buf); 597 rx_ring->xdp_buf = NULL; 598 } 599 600 if (rx_ring->desc) { 601 size = ALIGN(rx_ring->count * sizeof(union ice_32byte_rx_desc), 602 PAGE_SIZE); 603 dmam_free_coherent(dev, size, rx_ring->desc, rx_ring->dma); 604 rx_ring->desc = NULL; 605 } 606 } 607 608 /** 609 * ice_setup_rx_ring - Allocate the Rx descriptors 610 * @rx_ring: the Rx ring to set up 611 * 612 * Return 0 on success, negative on error 613 */ 614 int ice_setup_rx_ring(struct ice_rx_ring *rx_ring) 615 { 616 struct device *dev = ice_pf_to_dev(rx_ring->vsi->back); 617 u32 size; 618 619 /* round up to nearest page */ 620 size = ALIGN(rx_ring->count * sizeof(union ice_32byte_rx_desc), 621 PAGE_SIZE); 622 rx_ring->desc = dmam_alloc_coherent(dev, size, &rx_ring->dma, 623 GFP_KERNEL); 624 if (!rx_ring->desc) { 625 dev_err(dev, "Unable to allocate memory for the Rx descriptor ring, size=%d\n", 626 size); 627 return -ENOMEM; 628 } 629 630 rx_ring->next_to_use = 0; 631 rx_ring->next_to_clean = 0; 632 633 if (ice_is_xdp_ena_vsi(rx_ring->vsi)) 634 WRITE_ONCE(rx_ring->xdp_prog, rx_ring->vsi->xdp_prog); 635 636 return 0; 637 } 638 639 /** 640 * ice_run_xdp - Executes an XDP program on initialized xdp_buff 641 * @rx_ring: Rx ring 642 * @xdp: xdp_buff used as input to the XDP program 643 * @xdp_prog: XDP program to run 644 * @xdp_ring: ring to be used for XDP_TX action 645 * @eop_desc: Last descriptor in packet to read metadata from 646 * 647 * Returns any of ICE_XDP_{PASS, CONSUMED, TX, REDIR} 648 */ 649 static u32 650 ice_run_xdp(struct ice_rx_ring *rx_ring, struct libeth_xdp_buff *xdp, 651 struct bpf_prog *xdp_prog, struct ice_tx_ring *xdp_ring, 652 union ice_32b_rx_flex_desc *eop_desc) 653 { 654 unsigned int ret = ICE_XDP_PASS; 655 u32 act; 656 657 if (!xdp_prog) 658 goto exit; 659 660 xdp->desc = eop_desc; 661 662 act = bpf_prog_run_xdp(xdp_prog, &xdp->base); 663 switch (act) { 664 case XDP_PASS: 665 break; 666 case XDP_TX: 667 if (static_branch_unlikely(&ice_xdp_locking_key)) 668 spin_lock(&xdp_ring->tx_lock); 669 ret = __ice_xmit_xdp_ring(&xdp->base, xdp_ring, false); 670 if (static_branch_unlikely(&ice_xdp_locking_key)) 671 spin_unlock(&xdp_ring->tx_lock); 672 if (ret == ICE_XDP_CONSUMED) 673 goto out_failure; 674 break; 675 case XDP_REDIRECT: 676 if (xdp_do_redirect(rx_ring->netdev, &xdp->base, xdp_prog)) 677 goto out_failure; 678 ret = ICE_XDP_REDIR; 679 break; 680 default: 681 bpf_warn_invalid_xdp_action(rx_ring->netdev, xdp_prog, act); 682 fallthrough; 683 case XDP_ABORTED: 684 out_failure: 685 trace_xdp_exception(rx_ring->netdev, xdp_prog, act); 686 fallthrough; 687 case XDP_DROP: 688 libeth_xdp_return_buff(xdp); 689 ret = ICE_XDP_CONSUMED; 690 } 691 692 exit: 693 return ret; 694 } 695 696 /** 697 * ice_xmit_xdp_ring - submit frame to XDP ring for transmission 698 * @xdpf: XDP frame that will be converted to XDP buff 699 * @xdp_ring: XDP ring for transmission 700 */ 701 static int ice_xmit_xdp_ring(const struct xdp_frame *xdpf, 702 struct ice_tx_ring *xdp_ring) 703 { 704 struct xdp_buff xdp; 705 706 xdp.data_hard_start = (void *)xdpf; 707 xdp.data = xdpf->data; 708 xdp.data_end = xdp.data + xdpf->len; 709 xdp.frame_sz = xdpf->frame_sz; 710 xdp.flags = xdpf->flags; 711 712 return __ice_xmit_xdp_ring(&xdp, xdp_ring, true); 713 } 714 715 /** 716 * ice_xdp_xmit - submit packets to XDP ring for transmission 717 * @dev: netdev 718 * @n: number of XDP frames to be transmitted 719 * @frames: XDP frames to be transmitted 720 * @flags: transmit flags 721 * 722 * Returns number of frames successfully sent. Failed frames 723 * will be free'ed by XDP core. 724 * For error cases, a negative errno code is returned and no-frames 725 * are transmitted (caller must handle freeing frames). 726 */ 727 int 728 ice_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **frames, 729 u32 flags) 730 { 731 struct ice_netdev_priv *np = netdev_priv(dev); 732 unsigned int queue_index = smp_processor_id(); 733 struct ice_vsi *vsi = np->vsi; 734 struct ice_tx_ring *xdp_ring; 735 struct ice_tx_buf *tx_buf; 736 int nxmit = 0, i; 737 738 if (test_bit(ICE_VSI_DOWN, vsi->state)) 739 return -ENETDOWN; 740 741 if (!ice_is_xdp_ena_vsi(vsi)) 742 return -ENXIO; 743 744 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) 745 return -EINVAL; 746 747 if (static_branch_unlikely(&ice_xdp_locking_key)) { 748 queue_index %= vsi->num_xdp_txq; 749 xdp_ring = vsi->xdp_rings[queue_index]; 750 spin_lock(&xdp_ring->tx_lock); 751 } else { 752 /* Generally, should not happen */ 753 if (unlikely(queue_index >= vsi->num_xdp_txq)) 754 return -ENXIO; 755 xdp_ring = vsi->xdp_rings[queue_index]; 756 } 757 758 tx_buf = &xdp_ring->tx_buf[xdp_ring->next_to_use]; 759 for (i = 0; i < n; i++) { 760 const struct xdp_frame *xdpf = frames[i]; 761 int err; 762 763 err = ice_xmit_xdp_ring(xdpf, xdp_ring); 764 if (err != ICE_XDP_TX) 765 break; 766 nxmit++; 767 } 768 769 tx_buf->rs_idx = ice_set_rs_bit(xdp_ring); 770 if (unlikely(flags & XDP_XMIT_FLUSH)) 771 ice_xdp_ring_update_tail(xdp_ring); 772 773 if (static_branch_unlikely(&ice_xdp_locking_key)) 774 spin_unlock(&xdp_ring->tx_lock); 775 776 return nxmit; 777 } 778 779 /** 780 * ice_init_ctrl_rx_descs - Initialize Rx descriptors for control vsi. 781 * @rx_ring: ring to init descriptors on 782 * @count: number of descriptors to initialize 783 */ 784 void ice_init_ctrl_rx_descs(struct ice_rx_ring *rx_ring, u32 count) 785 { 786 union ice_32b_rx_flex_desc *rx_desc; 787 u32 ntu = rx_ring->next_to_use; 788 789 if (!count) 790 return; 791 792 rx_desc = ICE_RX_DESC(rx_ring, ntu); 793 794 do { 795 rx_desc++; 796 ntu++; 797 if (unlikely(ntu == rx_ring->count)) { 798 rx_desc = ICE_RX_DESC(rx_ring, 0); 799 ntu = 0; 800 } 801 802 rx_desc->wb.status_error0 = 0; 803 count--; 804 } while (count); 805 806 if (rx_ring->next_to_use != ntu) 807 ice_release_rx_desc(rx_ring, ntu); 808 } 809 810 /** 811 * ice_alloc_rx_bufs - Replace used receive buffers 812 * @rx_ring: ring to place buffers on 813 * @cleaned_count: number of buffers to replace 814 * 815 * Returns false if all allocations were successful, true if any fail. Returning 816 * true signals to the caller that we didn't replace cleaned_count buffers and 817 * there is more work to do. 818 * 819 * First, try to clean "cleaned_count" Rx buffers. Then refill the cleaned Rx 820 * buffers. Then bump tail at most one time. Grouping like this lets us avoid 821 * multiple tail writes per call. 822 */ 823 bool ice_alloc_rx_bufs(struct ice_rx_ring *rx_ring, unsigned int cleaned_count) 824 { 825 const struct libeth_fq_fp hdr_fq = { 826 .pp = rx_ring->hdr_pp, 827 .fqes = rx_ring->hdr_fqes, 828 .truesize = rx_ring->hdr_truesize, 829 .count = rx_ring->count, 830 }; 831 const struct libeth_fq_fp fq = { 832 .pp = rx_ring->pp, 833 .fqes = rx_ring->rx_fqes, 834 .truesize = rx_ring->truesize, 835 .count = rx_ring->count, 836 }; 837 union ice_32b_rx_flex_desc *rx_desc; 838 u16 ntu = rx_ring->next_to_use; 839 840 /* do nothing if no valid netdev defined */ 841 if (!rx_ring->netdev || !cleaned_count) 842 return false; 843 844 /* get the Rx descriptor and buffer based on next_to_use */ 845 rx_desc = ICE_RX_DESC(rx_ring, ntu); 846 847 do { 848 dma_addr_t addr; 849 850 addr = libeth_rx_alloc(&fq, ntu); 851 if (addr == DMA_MAPPING_ERROR) { 852 rx_ring->ring_stats->rx_stats.alloc_page_failed++; 853 break; 854 } 855 856 /* Refresh the desc even if buffer_addrs didn't change 857 * because each write-back erases this info. 858 */ 859 rx_desc->read.pkt_addr = cpu_to_le64(addr); 860 861 if (!hdr_fq.pp) 862 goto next; 863 864 addr = libeth_rx_alloc(&hdr_fq, ntu); 865 if (addr == DMA_MAPPING_ERROR) { 866 rx_ring->ring_stats->rx_stats.alloc_page_failed++; 867 868 libeth_rx_recycle_slow(fq.fqes[ntu].netmem); 869 break; 870 } 871 872 rx_desc->read.hdr_addr = cpu_to_le64(addr); 873 874 next: 875 rx_desc++; 876 ntu++; 877 if (unlikely(ntu == rx_ring->count)) { 878 rx_desc = ICE_RX_DESC(rx_ring, 0); 879 ntu = 0; 880 } 881 882 /* clear the status bits for the next_to_use descriptor */ 883 rx_desc->wb.status_error0 = 0; 884 885 cleaned_count--; 886 } while (cleaned_count); 887 888 if (rx_ring->next_to_use != ntu) 889 ice_release_rx_desc(rx_ring, ntu); 890 891 return !!cleaned_count; 892 } 893 894 /** 895 * ice_clean_ctrl_rx_irq - Clean descriptors from flow director Rx ring 896 * @rx_ring: Rx descriptor ring for ctrl_vsi to transact packets on 897 * 898 * This function cleans Rx descriptors from the ctrl_vsi Rx ring used 899 * to set flow director rules on VFs. 900 */ 901 void ice_clean_ctrl_rx_irq(struct ice_rx_ring *rx_ring) 902 { 903 u32 ntc = rx_ring->next_to_clean; 904 unsigned int total_rx_pkts = 0; 905 u32 cnt = rx_ring->count; 906 907 while (likely(total_rx_pkts < ICE_DFLT_IRQ_WORK)) { 908 struct ice_vsi *ctrl_vsi = rx_ring->vsi; 909 union ice_32b_rx_flex_desc *rx_desc; 910 u16 stat_err_bits; 911 912 rx_desc = ICE_RX_DESC(rx_ring, ntc); 913 914 stat_err_bits = BIT(ICE_RX_FLEX_DESC_STATUS0_DD_S); 915 if (!ice_test_staterr(rx_desc->wb.status_error0, stat_err_bits)) 916 break; 917 918 dma_rmb(); 919 920 if (ctrl_vsi->vf) 921 ice_vc_fdir_irq_handler(ctrl_vsi, rx_desc); 922 923 if (++ntc == cnt) 924 ntc = 0; 925 total_rx_pkts++; 926 } 927 928 rx_ring->next_to_clean = ntc; 929 ice_init_ctrl_rx_descs(rx_ring, ICE_DESC_UNUSED(rx_ring)); 930 } 931 932 /** 933 * ice_clean_rx_irq - Clean completed descriptors from Rx ring - bounce buf 934 * @rx_ring: Rx descriptor ring to transact packets on 935 * @budget: Total limit on number of packets to process 936 * 937 * This function provides a "bounce buffer" approach to Rx interrupt 938 * processing. The advantage to this is that on systems that have 939 * expensive overhead for IOMMU access this provides a means of avoiding 940 * it by maintaining the mapping of the page to the system. 941 * 942 * Returns amount of work completed 943 */ 944 static int ice_clean_rx_irq(struct ice_rx_ring *rx_ring, int budget) 945 { 946 unsigned int total_rx_bytes = 0, total_rx_pkts = 0; 947 struct ice_tx_ring *xdp_ring = NULL; 948 struct bpf_prog *xdp_prog = NULL; 949 u32 ntc = rx_ring->next_to_clean; 950 LIBETH_XDP_ONSTACK_BUFF(xdp); 951 u32 cached_ntu, xdp_verdict; 952 u32 cnt = rx_ring->count; 953 u32 xdp_xmit = 0; 954 bool failure; 955 956 libeth_xdp_init_buff(xdp, &rx_ring->xdp, &rx_ring->xdp_rxq); 957 958 xdp_prog = READ_ONCE(rx_ring->xdp_prog); 959 if (xdp_prog) { 960 xdp_ring = rx_ring->xdp_ring; 961 cached_ntu = xdp_ring->next_to_use; 962 } 963 964 /* start the loop to process Rx packets bounded by 'budget' */ 965 while (likely(total_rx_pkts < (unsigned int)budget)) { 966 union ice_32b_rx_flex_desc *rx_desc; 967 struct libeth_fqe *rx_buf; 968 struct sk_buff *skb; 969 unsigned int size; 970 u16 stat_err_bits; 971 u16 vlan_tci; 972 bool rxe; 973 974 /* get the Rx desc from Rx ring based on 'next_to_clean' */ 975 rx_desc = ICE_RX_DESC(rx_ring, ntc); 976 977 /* 978 * The DD bit will always be zero for unused descriptors 979 * because it's cleared in cleanup or when setting the DMA 980 * address of the header buffer, which never uses the DD bit. 981 * If the hardware wrote the descriptor, it will be non-zero. 982 */ 983 stat_err_bits = BIT(ICE_RX_FLEX_DESC_STATUS0_DD_S); 984 if (!ice_test_staterr(rx_desc->wb.status_error0, stat_err_bits)) 985 break; 986 987 /* This memory barrier is needed to keep us from reading 988 * any other fields out of the rx_desc until we know the 989 * DD bit is set. 990 */ 991 dma_rmb(); 992 993 ice_trace(clean_rx_irq, rx_ring, rx_desc); 994 995 stat_err_bits = BIT(ICE_RX_FLEX_DESC_STATUS0_HBO_S) | 996 BIT(ICE_RX_FLEX_DESC_STATUS0_RXE_S); 997 rxe = ice_test_staterr(rx_desc->wb.status_error0, 998 stat_err_bits); 999 1000 if (!rx_ring->hdr_pp) 1001 goto payload; 1002 1003 size = le16_get_bits(rx_desc->wb.hdr_len_sph_flex_flags1, 1004 ICE_RX_FLEX_DESC_HDR_LEN_M); 1005 if (unlikely(rxe)) 1006 size = 0; 1007 1008 rx_buf = &rx_ring->hdr_fqes[ntc]; 1009 libeth_xdp_process_buff(xdp, rx_buf, size); 1010 rx_buf->netmem = 0; 1011 1012 payload: 1013 size = le16_to_cpu(rx_desc->wb.pkt_len) & 1014 ICE_RX_FLX_DESC_PKT_LEN_M; 1015 if (unlikely(rxe)) 1016 size = 0; 1017 1018 /* retrieve a buffer from the ring */ 1019 rx_buf = &rx_ring->rx_fqes[ntc]; 1020 libeth_xdp_process_buff(xdp, rx_buf, size); 1021 1022 if (++ntc == cnt) 1023 ntc = 0; 1024 1025 /* skip if it is NOP desc */ 1026 if (ice_is_non_eop(rx_ring, rx_desc) || unlikely(!xdp->data)) 1027 continue; 1028 1029 xdp_verdict = ice_run_xdp(rx_ring, xdp, xdp_prog, xdp_ring, rx_desc); 1030 if (xdp_verdict == ICE_XDP_PASS) 1031 goto construct_skb; 1032 1033 if (xdp_verdict & (ICE_XDP_TX | ICE_XDP_REDIR)) 1034 xdp_xmit |= xdp_verdict; 1035 total_rx_bytes += xdp_get_buff_len(&xdp->base); 1036 total_rx_pkts++; 1037 1038 xdp->data = NULL; 1039 continue; 1040 1041 construct_skb: 1042 skb = xdp_build_skb_from_buff(&xdp->base); 1043 xdp->data = NULL; 1044 1045 /* exit if we failed to retrieve a buffer */ 1046 if (!skb) { 1047 libeth_xdp_return_buff_slow(xdp); 1048 rx_ring->ring_stats->rx_stats.alloc_buf_failed++; 1049 continue; 1050 } 1051 1052 vlan_tci = ice_get_vlan_tci(rx_desc); 1053 1054 /* probably a little skewed due to removing CRC */ 1055 total_rx_bytes += skb->len; 1056 1057 /* populate checksum, VLAN, and protocol */ 1058 ice_process_skb_fields(rx_ring, rx_desc, skb); 1059 1060 ice_trace(clean_rx_irq_indicate, rx_ring, rx_desc, skb); 1061 /* send completed skb up the stack */ 1062 ice_receive_skb(rx_ring, skb, vlan_tci); 1063 1064 /* update budget accounting */ 1065 total_rx_pkts++; 1066 } 1067 1068 rx_ring->next_to_clean = ntc; 1069 /* return up to cleaned_count buffers to hardware */ 1070 failure = ice_alloc_rx_bufs(rx_ring, ICE_DESC_UNUSED(rx_ring)); 1071 1072 if (xdp_xmit) 1073 ice_finalize_xdp_rx(xdp_ring, xdp_xmit, cached_ntu); 1074 1075 libeth_xdp_save_buff(&rx_ring->xdp, xdp); 1076 1077 if (rx_ring->ring_stats) 1078 ice_update_rx_ring_stats(rx_ring, total_rx_pkts, 1079 total_rx_bytes); 1080 1081 /* guarantee a trip back through this routine if there was a failure */ 1082 return failure ? budget : (int)total_rx_pkts; 1083 } 1084 1085 static void __ice_update_sample(struct ice_q_vector *q_vector, 1086 struct ice_ring_container *rc, 1087 struct dim_sample *sample, 1088 bool is_tx) 1089 { 1090 u64 packets = 0, bytes = 0; 1091 1092 if (is_tx) { 1093 struct ice_tx_ring *tx_ring; 1094 1095 ice_for_each_tx_ring(tx_ring, *rc) { 1096 struct ice_ring_stats *ring_stats; 1097 1098 ring_stats = tx_ring->ring_stats; 1099 if (!ring_stats) 1100 continue; 1101 packets += ring_stats->stats.pkts; 1102 bytes += ring_stats->stats.bytes; 1103 } 1104 } else { 1105 struct ice_rx_ring *rx_ring; 1106 1107 ice_for_each_rx_ring(rx_ring, *rc) { 1108 struct ice_ring_stats *ring_stats; 1109 1110 ring_stats = rx_ring->ring_stats; 1111 if (!ring_stats) 1112 continue; 1113 packets += ring_stats->stats.pkts; 1114 bytes += ring_stats->stats.bytes; 1115 } 1116 } 1117 1118 dim_update_sample(q_vector->total_events, packets, bytes, sample); 1119 sample->comp_ctr = 0; 1120 1121 /* if dim settings get stale, like when not updated for 1 1122 * second or longer, force it to start again. This addresses the 1123 * frequent case of an idle queue being switched to by the 1124 * scheduler. The 1,000 here means 1,000 milliseconds. 1125 */ 1126 if (ktime_ms_delta(sample->time, rc->dim.start_sample.time) >= 1000) 1127 rc->dim.state = DIM_START_MEASURE; 1128 } 1129 1130 /** 1131 * ice_net_dim - Update net DIM algorithm 1132 * @q_vector: the vector associated with the interrupt 1133 * 1134 * Create a DIM sample and notify net_dim() so that it can possibly decide 1135 * a new ITR value based on incoming packets, bytes, and interrupts. 1136 * 1137 * This function is a no-op if the ring is not configured to dynamic ITR. 1138 */ 1139 static void ice_net_dim(struct ice_q_vector *q_vector) 1140 { 1141 struct ice_ring_container *tx = &q_vector->tx; 1142 struct ice_ring_container *rx = &q_vector->rx; 1143 1144 if (ITR_IS_DYNAMIC(tx)) { 1145 struct dim_sample dim_sample; 1146 1147 __ice_update_sample(q_vector, tx, &dim_sample, true); 1148 net_dim(&tx->dim, &dim_sample); 1149 } 1150 1151 if (ITR_IS_DYNAMIC(rx)) { 1152 struct dim_sample dim_sample; 1153 1154 __ice_update_sample(q_vector, rx, &dim_sample, false); 1155 net_dim(&rx->dim, &dim_sample); 1156 } 1157 } 1158 1159 /** 1160 * ice_buildreg_itr - build value for writing to the GLINT_DYN_CTL register 1161 * @itr_idx: interrupt throttling index 1162 * @itr: interrupt throttling value in usecs 1163 */ 1164 static u32 ice_buildreg_itr(u16 itr_idx, u16 itr) 1165 { 1166 /* The ITR value is reported in microseconds, and the register value is 1167 * recorded in 2 microsecond units. For this reason we only need to 1168 * shift by the GLINT_DYN_CTL_INTERVAL_S - ICE_ITR_GRAN_S to apply this 1169 * granularity as a shift instead of division. The mask makes sure the 1170 * ITR value is never odd so we don't accidentally write into the field 1171 * prior to the ITR field. 1172 */ 1173 itr &= ICE_ITR_MASK; 1174 1175 return GLINT_DYN_CTL_INTENA_M | GLINT_DYN_CTL_CLEARPBA_M | 1176 (itr_idx << GLINT_DYN_CTL_ITR_INDX_S) | 1177 (itr << (GLINT_DYN_CTL_INTERVAL_S - ICE_ITR_GRAN_S)); 1178 } 1179 1180 /** 1181 * ice_enable_interrupt - re-enable MSI-X interrupt 1182 * @q_vector: the vector associated with the interrupt to enable 1183 * 1184 * If the VSI is down, the interrupt will not be re-enabled. Also, 1185 * when enabling the interrupt always reset the wb_on_itr to false 1186 * and trigger a software interrupt to clean out internal state. 1187 */ 1188 static void ice_enable_interrupt(struct ice_q_vector *q_vector) 1189 { 1190 struct ice_vsi *vsi = q_vector->vsi; 1191 bool wb_en = q_vector->wb_on_itr; 1192 u32 itr_val; 1193 1194 if (test_bit(ICE_DOWN, vsi->state)) 1195 return; 1196 1197 /* trigger an ITR delayed software interrupt when exiting busy poll, to 1198 * make sure to catch any pending cleanups that might have been missed 1199 * due to interrupt state transition. If busy poll or poll isn't 1200 * enabled, then don't update ITR, and just enable the interrupt. 1201 */ 1202 if (!wb_en) { 1203 itr_val = ice_buildreg_itr(ICE_ITR_NONE, 0); 1204 } else { 1205 q_vector->wb_on_itr = false; 1206 1207 /* do two things here with a single write. Set up the third ITR 1208 * index to be used for software interrupt moderation, and then 1209 * trigger a software interrupt with a rate limit of 20K on 1210 * software interrupts, this will help avoid high interrupt 1211 * loads due to frequently polling and exiting polling. 1212 */ 1213 itr_val = ice_buildreg_itr(ICE_IDX_ITR2, ICE_ITR_20K); 1214 itr_val |= GLINT_DYN_CTL_SWINT_TRIG_M | 1215 ICE_IDX_ITR2 << GLINT_DYN_CTL_SW_ITR_INDX_S | 1216 GLINT_DYN_CTL_SW_ITR_INDX_ENA_M; 1217 } 1218 wr32(&vsi->back->hw, GLINT_DYN_CTL(q_vector->reg_idx), itr_val); 1219 } 1220 1221 /** 1222 * ice_set_wb_on_itr - set WB_ON_ITR for this q_vector 1223 * @q_vector: q_vector to set WB_ON_ITR on 1224 * 1225 * We need to tell hardware to write-back completed descriptors even when 1226 * interrupts are disabled. Descriptors will be written back on cache line 1227 * boundaries without WB_ON_ITR enabled, but if we don't enable WB_ON_ITR 1228 * descriptors may not be written back if they don't fill a cache line until 1229 * the next interrupt. 1230 * 1231 * This sets the write-back frequency to whatever was set previously for the 1232 * ITR indices. Also, set the INTENA_MSK bit to make sure hardware knows we 1233 * aren't meddling with the INTENA_M bit. 1234 */ 1235 static void ice_set_wb_on_itr(struct ice_q_vector *q_vector) 1236 { 1237 struct ice_vsi *vsi = q_vector->vsi; 1238 1239 /* already in wb_on_itr mode no need to change it */ 1240 if (q_vector->wb_on_itr) 1241 return; 1242 1243 /* use previously set ITR values for all of the ITR indices by 1244 * specifying ICE_ITR_NONE, which will vary in adaptive (AIM) mode and 1245 * be static in non-adaptive mode (user configured) 1246 */ 1247 wr32(&vsi->back->hw, GLINT_DYN_CTL(q_vector->reg_idx), 1248 FIELD_PREP(GLINT_DYN_CTL_ITR_INDX_M, ICE_ITR_NONE) | 1249 FIELD_PREP(GLINT_DYN_CTL_INTENA_MSK_M, 1) | 1250 FIELD_PREP(GLINT_DYN_CTL_WB_ON_ITR_M, 1)); 1251 1252 q_vector->wb_on_itr = true; 1253 } 1254 1255 /** 1256 * ice_napi_poll - NAPI polling Rx/Tx cleanup routine 1257 * @napi: napi struct with our devices info in it 1258 * @budget: amount of work driver is allowed to do this pass, in packets 1259 * 1260 * This function will clean all queues associated with a q_vector. 1261 * 1262 * Returns the amount of work done 1263 */ 1264 int ice_napi_poll(struct napi_struct *napi, int budget) 1265 { 1266 struct ice_q_vector *q_vector = 1267 container_of(napi, struct ice_q_vector, napi); 1268 struct ice_tx_ring *tx_ring; 1269 struct ice_rx_ring *rx_ring; 1270 bool clean_complete = true; 1271 int budget_per_ring; 1272 int work_done = 0; 1273 1274 /* Since the actual Tx work is minimal, we can give the Tx a larger 1275 * budget and be more aggressive about cleaning up the Tx descriptors. 1276 */ 1277 ice_for_each_tx_ring(tx_ring, q_vector->tx) { 1278 struct xsk_buff_pool *xsk_pool = READ_ONCE(tx_ring->xsk_pool); 1279 bool wd; 1280 1281 if (xsk_pool) 1282 wd = ice_xmit_zc(tx_ring, xsk_pool); 1283 else if (ice_ring_is_xdp(tx_ring)) 1284 wd = true; 1285 else 1286 wd = ice_clean_tx_irq(tx_ring, budget); 1287 1288 if (!wd) 1289 clean_complete = false; 1290 } 1291 1292 /* Handle case where we are called by netpoll with a budget of 0 */ 1293 if (unlikely(budget <= 0)) 1294 return budget; 1295 1296 /* normally we have 1 Rx ring per q_vector */ 1297 if (unlikely(q_vector->num_ring_rx > 1)) 1298 /* We attempt to distribute budget to each Rx queue fairly, but 1299 * don't allow the budget to go below 1 because that would exit 1300 * polling early. 1301 */ 1302 budget_per_ring = max_t(int, budget / q_vector->num_ring_rx, 1); 1303 else 1304 /* Max of 1 Rx ring in this q_vector so give it the budget */ 1305 budget_per_ring = budget; 1306 1307 ice_for_each_rx_ring(rx_ring, q_vector->rx) { 1308 struct xsk_buff_pool *xsk_pool = READ_ONCE(rx_ring->xsk_pool); 1309 int cleaned; 1310 1311 /* A dedicated path for zero-copy allows making a single 1312 * comparison in the irq context instead of many inside the 1313 * ice_clean_rx_irq function and makes the codebase cleaner. 1314 */ 1315 cleaned = rx_ring->xsk_pool ? 1316 ice_clean_rx_irq_zc(rx_ring, xsk_pool, budget_per_ring) : 1317 ice_clean_rx_irq(rx_ring, budget_per_ring); 1318 work_done += cleaned; 1319 /* if we clean as many as budgeted, we must not be done */ 1320 if (cleaned >= budget_per_ring) 1321 clean_complete = false; 1322 } 1323 1324 /* If work not completed, return budget and polling will return */ 1325 if (!clean_complete) { 1326 /* Set the writeback on ITR so partial completions of 1327 * cache-lines will still continue even if we're polling. 1328 */ 1329 ice_set_wb_on_itr(q_vector); 1330 return budget; 1331 } 1332 1333 /* Exit the polling mode, but don't re-enable interrupts if stack might 1334 * poll us due to busy-polling 1335 */ 1336 if (napi_complete_done(napi, work_done)) { 1337 ice_net_dim(q_vector); 1338 ice_enable_interrupt(q_vector); 1339 } else { 1340 ice_set_wb_on_itr(q_vector); 1341 } 1342 1343 return min_t(int, work_done, budget - 1); 1344 } 1345 1346 /** 1347 * __ice_maybe_stop_tx - 2nd level check for Tx stop conditions 1348 * @tx_ring: the ring to be checked 1349 * @size: the size buffer we want to assure is available 1350 * 1351 * Returns -EBUSY if a stop is needed, else 0 1352 */ 1353 static int __ice_maybe_stop_tx(struct ice_tx_ring *tx_ring, unsigned int size) 1354 { 1355 netif_tx_stop_queue(txring_txq(tx_ring)); 1356 /* Memory barrier before checking head and tail */ 1357 smp_mb(); 1358 1359 /* Check again in a case another CPU has just made room available. */ 1360 if (likely(ICE_DESC_UNUSED(tx_ring) < size)) 1361 return -EBUSY; 1362 1363 /* A reprieve! - use start_queue because it doesn't call schedule */ 1364 netif_tx_start_queue(txring_txq(tx_ring)); 1365 ++tx_ring->ring_stats->tx_stats.restart_q; 1366 return 0; 1367 } 1368 1369 /** 1370 * ice_maybe_stop_tx - 1st level check for Tx stop conditions 1371 * @tx_ring: the ring to be checked 1372 * @size: the size buffer we want to assure is available 1373 * 1374 * Returns 0 if stop is not needed 1375 */ 1376 static int ice_maybe_stop_tx(struct ice_tx_ring *tx_ring, unsigned int size) 1377 { 1378 if (likely(ICE_DESC_UNUSED(tx_ring) >= size)) 1379 return 0; 1380 1381 return __ice_maybe_stop_tx(tx_ring, size); 1382 } 1383 1384 /** 1385 * ice_tx_map - Build the Tx descriptor 1386 * @tx_ring: ring to send buffer on 1387 * @first: first buffer info buffer to use 1388 * @off: pointer to struct that holds offload parameters 1389 * 1390 * This function loops over the skb data pointed to by *first 1391 * and gets a physical address for each memory location and programs 1392 * it and the length into the transmit descriptor. 1393 */ 1394 static void 1395 ice_tx_map(struct ice_tx_ring *tx_ring, struct ice_tx_buf *first, 1396 struct ice_tx_offload_params *off) 1397 { 1398 u64 td_offset, td_tag, td_cmd; 1399 u16 i = tx_ring->next_to_use; 1400 unsigned int data_len, size; 1401 struct ice_tx_desc *tx_desc; 1402 struct ice_tx_buf *tx_buf; 1403 struct sk_buff *skb; 1404 skb_frag_t *frag; 1405 dma_addr_t dma; 1406 bool kick; 1407 1408 td_tag = off->td_l2tag1; 1409 td_cmd = off->td_cmd; 1410 td_offset = off->td_offset; 1411 skb = first->skb; 1412 1413 data_len = skb->data_len; 1414 size = skb_headlen(skb); 1415 1416 tx_desc = ICE_TX_DESC(tx_ring, i); 1417 1418 if (first->tx_flags & ICE_TX_FLAGS_HW_VLAN) { 1419 td_cmd |= (u64)ICE_TX_DESC_CMD_IL2TAG1; 1420 td_tag = first->vid; 1421 } 1422 1423 dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE); 1424 1425 tx_buf = first; 1426 1427 for (frag = &skb_shinfo(skb)->frags[0];; frag++) { 1428 unsigned int max_data = ICE_MAX_DATA_PER_TXD_ALIGNED; 1429 1430 if (dma_mapping_error(tx_ring->dev, dma)) 1431 goto dma_error; 1432 1433 /* record length, and DMA address */ 1434 dma_unmap_len_set(tx_buf, len, size); 1435 dma_unmap_addr_set(tx_buf, dma, dma); 1436 1437 /* align size to end of page */ 1438 max_data += -dma & (ICE_MAX_READ_REQ_SIZE - 1); 1439 tx_desc->buf_addr = cpu_to_le64(dma); 1440 1441 /* account for data chunks larger than the hardware 1442 * can handle 1443 */ 1444 while (unlikely(size > ICE_MAX_DATA_PER_TXD)) { 1445 tx_desc->cmd_type_offset_bsz = 1446 ice_build_ctob(td_cmd, td_offset, max_data, 1447 td_tag); 1448 1449 tx_desc++; 1450 i++; 1451 1452 if (i == tx_ring->count) { 1453 tx_desc = ICE_TX_DESC(tx_ring, 0); 1454 i = 0; 1455 } 1456 1457 dma += max_data; 1458 size -= max_data; 1459 1460 max_data = ICE_MAX_DATA_PER_TXD_ALIGNED; 1461 tx_desc->buf_addr = cpu_to_le64(dma); 1462 } 1463 1464 if (likely(!data_len)) 1465 break; 1466 1467 tx_desc->cmd_type_offset_bsz = ice_build_ctob(td_cmd, td_offset, 1468 size, td_tag); 1469 1470 tx_desc++; 1471 i++; 1472 1473 if (i == tx_ring->count) { 1474 tx_desc = ICE_TX_DESC(tx_ring, 0); 1475 i = 0; 1476 } 1477 1478 size = skb_frag_size(frag); 1479 data_len -= size; 1480 1481 dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size, 1482 DMA_TO_DEVICE); 1483 1484 tx_buf = &tx_ring->tx_buf[i]; 1485 tx_buf->type = ICE_TX_BUF_FRAG; 1486 } 1487 1488 /* record SW timestamp if HW timestamp is not available */ 1489 skb_tx_timestamp(first->skb); 1490 1491 i++; 1492 if (i == tx_ring->count) 1493 i = 0; 1494 1495 /* write last descriptor with RS and EOP bits */ 1496 td_cmd |= (u64)ICE_TXD_LAST_DESC_CMD; 1497 tx_desc->cmd_type_offset_bsz = 1498 ice_build_ctob(td_cmd, td_offset, size, td_tag); 1499 1500 /* Force memory writes to complete before letting h/w know there 1501 * are new descriptors to fetch. 1502 * 1503 * We also use this memory barrier to make certain all of the 1504 * status bits have been updated before next_to_watch is written. 1505 */ 1506 wmb(); 1507 1508 /* set next_to_watch value indicating a packet is present */ 1509 first->next_to_watch = tx_desc; 1510 1511 tx_ring->next_to_use = i; 1512 1513 ice_maybe_stop_tx(tx_ring, DESC_NEEDED); 1514 1515 /* notify HW of packet */ 1516 kick = __netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount, 1517 netdev_xmit_more()); 1518 if (!kick) 1519 return; 1520 1521 if (ice_is_txtime_cfg(tx_ring)) { 1522 struct ice_tstamp_ring *tstamp_ring = tx_ring->tstamp_ring; 1523 u32 tstamp_count = tstamp_ring->count; 1524 u32 j = tstamp_ring->next_to_use; 1525 struct ice_ts_desc *ts_desc; 1526 struct timespec64 ts; 1527 u32 tstamp; 1528 1529 ts = ktime_to_timespec64(first->skb->tstamp); 1530 tstamp = ts.tv_nsec >> ICE_TXTIME_CTX_RESOLUTION_128NS; 1531 1532 ts_desc = ICE_TS_DESC(tstamp_ring, j); 1533 ts_desc->tx_desc_idx_tstamp = ice_build_tstamp_desc(i, tstamp); 1534 1535 j++; 1536 if (j == tstamp_count) { 1537 u32 fetch = tstamp_count - tx_ring->count; 1538 1539 j = 0; 1540 1541 /* To prevent an MDD, when wrapping the tstamp ring 1542 * create additional TS descriptors equal to the number 1543 * of the fetch TS descriptors value. HW will merge the 1544 * TS descriptors with the same timestamp value into a 1545 * single descriptor. 1546 */ 1547 for (; j < fetch; j++) { 1548 ts_desc = ICE_TS_DESC(tstamp_ring, j); 1549 ts_desc->tx_desc_idx_tstamp = 1550 ice_build_tstamp_desc(i, tstamp); 1551 } 1552 } 1553 tstamp_ring->next_to_use = j; 1554 writel_relaxed(j, tstamp_ring->tail); 1555 } else { 1556 writel_relaxed(i, tx_ring->tail); 1557 } 1558 return; 1559 1560 dma_error: 1561 /* clear DMA mappings for failed tx_buf map */ 1562 for (;;) { 1563 tx_buf = &tx_ring->tx_buf[i]; 1564 ice_unmap_and_free_tx_buf(tx_ring, tx_buf); 1565 if (tx_buf == first) 1566 break; 1567 if (i == 0) 1568 i = tx_ring->count; 1569 i--; 1570 } 1571 1572 tx_ring->next_to_use = i; 1573 } 1574 1575 /** 1576 * ice_tx_csum - Enable Tx checksum offloads 1577 * @first: pointer to the first descriptor 1578 * @off: pointer to struct that holds offload parameters 1579 * 1580 * Returns 0 or error (negative) if checksum offload can't happen, 1 otherwise. 1581 */ 1582 static 1583 int ice_tx_csum(struct ice_tx_buf *first, struct ice_tx_offload_params *off) 1584 { 1585 const struct ice_tx_ring *tx_ring = off->tx_ring; 1586 u32 l4_len = 0, l3_len = 0, l2_len = 0; 1587 struct sk_buff *skb = first->skb; 1588 union { 1589 struct iphdr *v4; 1590 struct ipv6hdr *v6; 1591 unsigned char *hdr; 1592 } ip; 1593 union { 1594 struct tcphdr *tcp; 1595 unsigned char *hdr; 1596 } l4; 1597 __be16 frag_off, protocol; 1598 unsigned char *exthdr; 1599 u32 offset, cmd = 0; 1600 u8 l4_proto = 0; 1601 1602 if (skb->ip_summed != CHECKSUM_PARTIAL) 1603 return 0; 1604 1605 protocol = vlan_get_protocol(skb); 1606 1607 if (eth_p_mpls(protocol)) { 1608 ip.hdr = skb_inner_network_header(skb); 1609 l4.hdr = skb_checksum_start(skb); 1610 } else { 1611 ip.hdr = skb_network_header(skb); 1612 l4.hdr = skb_transport_header(skb); 1613 } 1614 1615 /* compute outer L2 header size */ 1616 l2_len = ip.hdr - skb->data; 1617 offset = (l2_len / 2) << ICE_TX_DESC_LEN_MACLEN_S; 1618 1619 /* set the tx_flags to indicate the IP protocol type. this is 1620 * required so that checksum header computation below is accurate. 1621 */ 1622 if (ip.v4->version == 4) 1623 first->tx_flags |= ICE_TX_FLAGS_IPV4; 1624 else if (ip.v6->version == 6) 1625 first->tx_flags |= ICE_TX_FLAGS_IPV6; 1626 1627 if (skb->encapsulation) { 1628 bool gso_ena = false; 1629 u32 tunnel = 0; 1630 1631 /* define outer network header type */ 1632 if (first->tx_flags & ICE_TX_FLAGS_IPV4) { 1633 tunnel |= (first->tx_flags & ICE_TX_FLAGS_TSO) ? 1634 ICE_TX_CTX_EIPT_IPV4 : 1635 ICE_TX_CTX_EIPT_IPV4_NO_CSUM; 1636 l4_proto = ip.v4->protocol; 1637 } else if (first->tx_flags & ICE_TX_FLAGS_IPV6) { 1638 int ret; 1639 1640 tunnel |= ICE_TX_CTX_EIPT_IPV6; 1641 exthdr = ip.hdr + sizeof(*ip.v6); 1642 l4_proto = ip.v6->nexthdr; 1643 ret = ipv6_skip_exthdr(skb, exthdr - skb->data, 1644 &l4_proto, &frag_off); 1645 if (ret < 0) 1646 return -1; 1647 } 1648 1649 /* define outer transport */ 1650 switch (l4_proto) { 1651 case IPPROTO_UDP: 1652 tunnel |= ICE_TXD_CTX_UDP_TUNNELING; 1653 first->tx_flags |= ICE_TX_FLAGS_TUNNEL; 1654 break; 1655 case IPPROTO_GRE: 1656 tunnel |= ICE_TXD_CTX_GRE_TUNNELING; 1657 first->tx_flags |= ICE_TX_FLAGS_TUNNEL; 1658 break; 1659 case IPPROTO_IPIP: 1660 case IPPROTO_IPV6: 1661 first->tx_flags |= ICE_TX_FLAGS_TUNNEL; 1662 l4.hdr = skb_inner_network_header(skb); 1663 break; 1664 default: 1665 if (first->tx_flags & ICE_TX_FLAGS_TSO) 1666 return -1; 1667 1668 skb_checksum_help(skb); 1669 return 0; 1670 } 1671 1672 /* compute outer L3 header size */ 1673 tunnel |= ((l4.hdr - ip.hdr) / 4) << 1674 ICE_TXD_CTX_QW0_EIPLEN_S; 1675 1676 /* switch IP header pointer from outer to inner header */ 1677 ip.hdr = skb_inner_network_header(skb); 1678 1679 /* compute tunnel header size */ 1680 tunnel |= ((ip.hdr - l4.hdr) / 2) << 1681 ICE_TXD_CTX_QW0_NATLEN_S; 1682 1683 gso_ena = skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL; 1684 /* indicate if we need to offload outer UDP header */ 1685 if ((first->tx_flags & ICE_TX_FLAGS_TSO) && !gso_ena && 1686 (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM)) 1687 tunnel |= ICE_TXD_CTX_QW0_L4T_CS_M; 1688 1689 /* record tunnel offload values */ 1690 off->cd_tunnel_params |= tunnel; 1691 1692 /* set DTYP=1 to indicate that it's an Tx context descriptor 1693 * in IPsec tunnel mode with Tx offloads in Quad word 1 1694 */ 1695 off->cd_qw1 |= (u64)ICE_TX_DESC_DTYPE_CTX; 1696 1697 /* switch L4 header pointer from outer to inner */ 1698 l4.hdr = skb_inner_transport_header(skb); 1699 l4_proto = 0; 1700 1701 /* reset type as we transition from outer to inner headers */ 1702 first->tx_flags &= ~(ICE_TX_FLAGS_IPV4 | ICE_TX_FLAGS_IPV6); 1703 if (ip.v4->version == 4) 1704 first->tx_flags |= ICE_TX_FLAGS_IPV4; 1705 if (ip.v6->version == 6) 1706 first->tx_flags |= ICE_TX_FLAGS_IPV6; 1707 } 1708 1709 /* Enable IP checksum offloads */ 1710 if (first->tx_flags & ICE_TX_FLAGS_IPV4) { 1711 l4_proto = ip.v4->protocol; 1712 /* the stack computes the IP header already, the only time we 1713 * need the hardware to recompute it is in the case of TSO. 1714 */ 1715 if (first->tx_flags & ICE_TX_FLAGS_TSO) 1716 cmd |= ICE_TX_DESC_CMD_IIPT_IPV4_CSUM; 1717 else 1718 cmd |= ICE_TX_DESC_CMD_IIPT_IPV4; 1719 1720 } else if (first->tx_flags & ICE_TX_FLAGS_IPV6) { 1721 cmd |= ICE_TX_DESC_CMD_IIPT_IPV6; 1722 exthdr = ip.hdr + sizeof(*ip.v6); 1723 l4_proto = ip.v6->nexthdr; 1724 if (l4.hdr != exthdr) 1725 ipv6_skip_exthdr(skb, exthdr - skb->data, &l4_proto, 1726 &frag_off); 1727 } else { 1728 return -1; 1729 } 1730 1731 /* compute inner L3 header size */ 1732 l3_len = l4.hdr - ip.hdr; 1733 offset |= (l3_len / 4) << ICE_TX_DESC_LEN_IPLEN_S; 1734 1735 if ((tx_ring->netdev->features & NETIF_F_HW_CSUM) && 1736 !(first->tx_flags & ICE_TX_FLAGS_TSO) && 1737 !skb_csum_is_sctp(skb)) { 1738 /* Set GCS */ 1739 u16 csum_start = (skb->csum_start - skb->mac_header) / 2; 1740 u16 csum_offset = skb->csum_offset / 2; 1741 u16 gcs_params; 1742 1743 gcs_params = FIELD_PREP(ICE_TX_GCS_DESC_START_M, csum_start) | 1744 FIELD_PREP(ICE_TX_GCS_DESC_OFFSET_M, csum_offset) | 1745 FIELD_PREP(ICE_TX_GCS_DESC_TYPE_M, 1746 ICE_TX_GCS_DESC_CSUM_PSH); 1747 1748 /* Unlike legacy HW checksums, GCS requires a context 1749 * descriptor. 1750 */ 1751 off->cd_qw1 |= ICE_TX_DESC_DTYPE_CTX; 1752 off->cd_gcs_params = gcs_params; 1753 /* Fill out CSO info in data descriptors */ 1754 off->td_offset |= offset; 1755 off->td_cmd |= cmd; 1756 return 1; 1757 } 1758 1759 /* Enable L4 checksum offloads */ 1760 switch (l4_proto) { 1761 case IPPROTO_TCP: 1762 /* enable checksum offloads */ 1763 cmd |= ICE_TX_DESC_CMD_L4T_EOFT_TCP; 1764 l4_len = l4.tcp->doff; 1765 offset |= l4_len << ICE_TX_DESC_LEN_L4_LEN_S; 1766 break; 1767 case IPPROTO_UDP: 1768 /* enable UDP checksum offload */ 1769 cmd |= ICE_TX_DESC_CMD_L4T_EOFT_UDP; 1770 l4_len = (sizeof(struct udphdr) >> 2); 1771 offset |= l4_len << ICE_TX_DESC_LEN_L4_LEN_S; 1772 break; 1773 case IPPROTO_SCTP: 1774 /* enable SCTP checksum offload */ 1775 cmd |= ICE_TX_DESC_CMD_L4T_EOFT_SCTP; 1776 l4_len = sizeof(struct sctphdr) >> 2; 1777 offset |= l4_len << ICE_TX_DESC_LEN_L4_LEN_S; 1778 break; 1779 1780 default: 1781 if (first->tx_flags & ICE_TX_FLAGS_TSO) 1782 return -1; 1783 skb_checksum_help(skb); 1784 return 0; 1785 } 1786 1787 off->td_cmd |= cmd; 1788 off->td_offset |= offset; 1789 return 1; 1790 } 1791 1792 /** 1793 * ice_tx_prepare_vlan_flags - prepare generic Tx VLAN tagging flags for HW 1794 * @tx_ring: ring to send buffer on 1795 * @first: pointer to struct ice_tx_buf 1796 * 1797 * Checks the skb and set up correspondingly several generic transmit flags 1798 * related to VLAN tagging for the HW, such as VLAN, DCB, etc. 1799 */ 1800 static void 1801 ice_tx_prepare_vlan_flags(struct ice_tx_ring *tx_ring, struct ice_tx_buf *first) 1802 { 1803 struct sk_buff *skb = first->skb; 1804 1805 /* nothing left to do, software offloaded VLAN */ 1806 if (!skb_vlan_tag_present(skb) && eth_type_vlan(skb->protocol)) 1807 return; 1808 1809 /* the VLAN ethertype/tpid is determined by VSI configuration and netdev 1810 * feature flags, which the driver only allows either 802.1Q or 802.1ad 1811 * VLAN offloads exclusively so we only care about the VLAN ID here 1812 */ 1813 if (skb_vlan_tag_present(skb)) { 1814 first->vid = skb_vlan_tag_get(skb); 1815 if (tx_ring->flags & ICE_TX_FLAGS_RING_VLAN_L2TAG2) 1816 first->tx_flags |= ICE_TX_FLAGS_HW_OUTER_SINGLE_VLAN; 1817 else 1818 first->tx_flags |= ICE_TX_FLAGS_HW_VLAN; 1819 } 1820 1821 ice_tx_prepare_vlan_flags_dcb(tx_ring, first); 1822 } 1823 1824 /** 1825 * ice_tso - computes mss and TSO length to prepare for TSO 1826 * @first: pointer to struct ice_tx_buf 1827 * @off: pointer to struct that holds offload parameters 1828 * 1829 * Returns 0 or error (negative) if TSO can't happen, 1 otherwise. 1830 */ 1831 static 1832 int ice_tso(struct ice_tx_buf *first, struct ice_tx_offload_params *off) 1833 { 1834 struct sk_buff *skb = first->skb; 1835 union { 1836 struct iphdr *v4; 1837 struct ipv6hdr *v6; 1838 unsigned char *hdr; 1839 } ip; 1840 union { 1841 struct tcphdr *tcp; 1842 struct udphdr *udp; 1843 unsigned char *hdr; 1844 } l4; 1845 u64 cd_mss, cd_tso_len; 1846 __be16 protocol; 1847 u32 paylen; 1848 u8 l4_start; 1849 int err; 1850 1851 if (skb->ip_summed != CHECKSUM_PARTIAL) 1852 return 0; 1853 1854 if (!skb_is_gso(skb)) 1855 return 0; 1856 1857 err = skb_cow_head(skb, 0); 1858 if (err < 0) 1859 return err; 1860 1861 protocol = vlan_get_protocol(skb); 1862 1863 if (eth_p_mpls(protocol)) 1864 ip.hdr = skb_inner_network_header(skb); 1865 else 1866 ip.hdr = skb_network_header(skb); 1867 l4.hdr = skb_checksum_start(skb); 1868 1869 /* initialize outer IP header fields */ 1870 if (ip.v4->version == 4) { 1871 ip.v4->tot_len = 0; 1872 ip.v4->check = 0; 1873 } else { 1874 ip.v6->payload_len = 0; 1875 } 1876 1877 if (skb_shinfo(skb)->gso_type & (SKB_GSO_GRE | 1878 SKB_GSO_GRE_CSUM | 1879 SKB_GSO_IPXIP4 | 1880 SKB_GSO_IPXIP6 | 1881 SKB_GSO_UDP_TUNNEL | 1882 SKB_GSO_UDP_TUNNEL_CSUM)) { 1883 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL) && 1884 (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM)) { 1885 l4.udp->len = 0; 1886 1887 /* determine offset of outer transport header */ 1888 l4_start = (u8)(l4.hdr - skb->data); 1889 1890 /* remove payload length from outer checksum */ 1891 paylen = skb->len - l4_start; 1892 csum_replace_by_diff(&l4.udp->check, 1893 (__force __wsum)htonl(paylen)); 1894 } 1895 1896 /* reset pointers to inner headers */ 1897 ip.hdr = skb_inner_network_header(skb); 1898 l4.hdr = skb_inner_transport_header(skb); 1899 1900 /* initialize inner IP header fields */ 1901 if (ip.v4->version == 4) { 1902 ip.v4->tot_len = 0; 1903 ip.v4->check = 0; 1904 } else { 1905 ip.v6->payload_len = 0; 1906 } 1907 } 1908 1909 /* determine offset of transport header */ 1910 l4_start = (u8)(l4.hdr - skb->data); 1911 1912 /* remove payload length from checksum */ 1913 paylen = skb->len - l4_start; 1914 1915 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) { 1916 csum_replace_by_diff(&l4.udp->check, 1917 (__force __wsum)htonl(paylen)); 1918 /* compute length of UDP segmentation header */ 1919 off->header_len = (u8)sizeof(l4.udp) + l4_start; 1920 } else { 1921 csum_replace_by_diff(&l4.tcp->check, 1922 (__force __wsum)htonl(paylen)); 1923 /* compute length of TCP segmentation header */ 1924 off->header_len = (u8)((l4.tcp->doff * 4) + l4_start); 1925 } 1926 1927 /* update gso_segs and bytecount */ 1928 first->gso_segs = skb_shinfo(skb)->gso_segs; 1929 first->bytecount += (first->gso_segs - 1) * off->header_len; 1930 1931 cd_tso_len = skb->len - off->header_len; 1932 cd_mss = skb_shinfo(skb)->gso_size; 1933 1934 /* record cdesc_qw1 with TSO parameters */ 1935 off->cd_qw1 |= (u64)(ICE_TX_DESC_DTYPE_CTX | 1936 (ICE_TX_CTX_DESC_TSO << ICE_TXD_CTX_QW1_CMD_S) | 1937 (cd_tso_len << ICE_TXD_CTX_QW1_TSO_LEN_S) | 1938 (cd_mss << ICE_TXD_CTX_QW1_MSS_S)); 1939 first->tx_flags |= ICE_TX_FLAGS_TSO; 1940 return 1; 1941 } 1942 1943 /** 1944 * ice_txd_use_count - estimate the number of descriptors needed for Tx 1945 * @size: transmit request size in bytes 1946 * 1947 * Due to hardware alignment restrictions (4K alignment), we need to 1948 * assume that we can have no more than 12K of data per descriptor, even 1949 * though each descriptor can take up to 16K - 1 bytes of aligned memory. 1950 * Thus, we need to divide by 12K. But division is slow! Instead, 1951 * we decompose the operation into shifts and one relatively cheap 1952 * multiply operation. 1953 * 1954 * To divide by 12K, we first divide by 4K, then divide by 3: 1955 * To divide by 4K, shift right by 12 bits 1956 * To divide by 3, multiply by 85, then divide by 256 1957 * (Divide by 256 is done by shifting right by 8 bits) 1958 * Finally, we add one to round up. Because 256 isn't an exact multiple of 1959 * 3, we'll underestimate near each multiple of 12K. This is actually more 1960 * accurate as we have 4K - 1 of wiggle room that we can fit into the last 1961 * segment. For our purposes this is accurate out to 1M which is orders of 1962 * magnitude greater than our largest possible GSO size. 1963 * 1964 * This would then be implemented as: 1965 * return (((size >> 12) * 85) >> 8) + ICE_DESCS_FOR_SKB_DATA_PTR; 1966 * 1967 * Since multiplication and division are commutative, we can reorder 1968 * operations into: 1969 * return ((size * 85) >> 20) + ICE_DESCS_FOR_SKB_DATA_PTR; 1970 */ 1971 static unsigned int ice_txd_use_count(unsigned int size) 1972 { 1973 return ((size * 85) >> 20) + ICE_DESCS_FOR_SKB_DATA_PTR; 1974 } 1975 1976 /** 1977 * ice_xmit_desc_count - calculate number of Tx descriptors needed 1978 * @skb: send buffer 1979 * 1980 * Returns number of data descriptors needed for this skb. 1981 */ 1982 static unsigned int ice_xmit_desc_count(struct sk_buff *skb) 1983 { 1984 const skb_frag_t *frag = &skb_shinfo(skb)->frags[0]; 1985 unsigned int nr_frags = skb_shinfo(skb)->nr_frags; 1986 unsigned int count = 0, size = skb_headlen(skb); 1987 1988 for (;;) { 1989 count += ice_txd_use_count(size); 1990 1991 if (!nr_frags--) 1992 break; 1993 1994 size = skb_frag_size(frag++); 1995 } 1996 1997 return count; 1998 } 1999 2000 /** 2001 * __ice_chk_linearize - Check if there are more than 8 buffers per packet 2002 * @skb: send buffer 2003 * 2004 * Note: This HW can't DMA more than 8 buffers to build a packet on the wire 2005 * and so we need to figure out the cases where we need to linearize the skb. 2006 * 2007 * For TSO we need to count the TSO header and segment payload separately. 2008 * As such we need to check cases where we have 7 fragments or more as we 2009 * can potentially require 9 DMA transactions, 1 for the TSO header, 1 for 2010 * the segment payload in the first descriptor, and another 7 for the 2011 * fragments. 2012 */ 2013 static bool __ice_chk_linearize(struct sk_buff *skb) 2014 { 2015 const skb_frag_t *frag, *stale; 2016 int nr_frags, sum; 2017 2018 /* no need to check if number of frags is less than 7 */ 2019 nr_frags = skb_shinfo(skb)->nr_frags; 2020 if (nr_frags < (ICE_MAX_BUF_TXD - 1)) 2021 return false; 2022 2023 /* We need to walk through the list and validate that each group 2024 * of 6 fragments totals at least gso_size. 2025 */ 2026 nr_frags -= ICE_MAX_BUF_TXD - 2; 2027 frag = &skb_shinfo(skb)->frags[0]; 2028 2029 /* Initialize size to the negative value of gso_size minus 1. We 2030 * use this as the worst case scenario in which the frag ahead 2031 * of us only provides one byte which is why we are limited to 6 2032 * descriptors for a single transmit as the header and previous 2033 * fragment are already consuming 2 descriptors. 2034 */ 2035 sum = 1 - skb_shinfo(skb)->gso_size; 2036 2037 /* Add size of frags 0 through 4 to create our initial sum */ 2038 sum += skb_frag_size(frag++); 2039 sum += skb_frag_size(frag++); 2040 sum += skb_frag_size(frag++); 2041 sum += skb_frag_size(frag++); 2042 sum += skb_frag_size(frag++); 2043 2044 /* Walk through fragments adding latest fragment, testing it, and 2045 * then removing stale fragments from the sum. 2046 */ 2047 for (stale = &skb_shinfo(skb)->frags[0];; stale++) { 2048 int stale_size = skb_frag_size(stale); 2049 2050 sum += skb_frag_size(frag++); 2051 2052 /* The stale fragment may present us with a smaller 2053 * descriptor than the actual fragment size. To account 2054 * for that we need to remove all the data on the front and 2055 * figure out what the remainder would be in the last 2056 * descriptor associated with the fragment. 2057 */ 2058 if (stale_size > ICE_MAX_DATA_PER_TXD) { 2059 int align_pad = -(skb_frag_off(stale)) & 2060 (ICE_MAX_READ_REQ_SIZE - 1); 2061 2062 sum -= align_pad; 2063 stale_size -= align_pad; 2064 2065 do { 2066 sum -= ICE_MAX_DATA_PER_TXD_ALIGNED; 2067 stale_size -= ICE_MAX_DATA_PER_TXD_ALIGNED; 2068 } while (stale_size > ICE_MAX_DATA_PER_TXD); 2069 } 2070 2071 /* if sum is negative we failed to make sufficient progress */ 2072 if (sum < 0) 2073 return true; 2074 2075 if (!nr_frags--) 2076 break; 2077 2078 sum -= stale_size; 2079 } 2080 2081 return false; 2082 } 2083 2084 /** 2085 * ice_chk_linearize - Check if there are more than 8 fragments per packet 2086 * @skb: send buffer 2087 * @count: number of buffers used 2088 * 2089 * Note: Our HW can't scatter-gather more than 8 fragments to build 2090 * a packet on the wire and so we need to figure out the cases where we 2091 * need to linearize the skb. 2092 */ 2093 static bool ice_chk_linearize(struct sk_buff *skb, unsigned int count) 2094 { 2095 /* Both TSO and single send will work if count is less than 8 */ 2096 if (likely(count < ICE_MAX_BUF_TXD)) 2097 return false; 2098 2099 if (skb_is_gso(skb)) 2100 return __ice_chk_linearize(skb); 2101 2102 /* we can support up to 8 data buffers for a single send */ 2103 return count != ICE_MAX_BUF_TXD; 2104 } 2105 2106 /** 2107 * ice_tstamp - set up context descriptor for hardware timestamp 2108 * @tx_ring: pointer to the Tx ring to send buffer on 2109 * @skb: pointer to the SKB we're sending 2110 * @first: Tx buffer 2111 * @off: Tx offload parameters 2112 */ 2113 static void 2114 ice_tstamp(struct ice_tx_ring *tx_ring, struct sk_buff *skb, 2115 struct ice_tx_buf *first, struct ice_tx_offload_params *off) 2116 { 2117 s8 idx; 2118 2119 /* only timestamp the outbound packet if the user has requested it */ 2120 if (likely(!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))) 2121 return; 2122 2123 /* Tx timestamps cannot be sampled when doing TSO */ 2124 if (first->tx_flags & ICE_TX_FLAGS_TSO) 2125 return; 2126 2127 /* Grab an open timestamp slot */ 2128 idx = ice_ptp_request_ts(tx_ring->tx_tstamps, skb); 2129 if (idx < 0) { 2130 tx_ring->vsi->back->ptp.tx_hwtstamp_skipped++; 2131 return; 2132 } 2133 2134 off->cd_qw1 |= (u64)(ICE_TX_DESC_DTYPE_CTX | 2135 (ICE_TX_CTX_DESC_TSYN << ICE_TXD_CTX_QW1_CMD_S) | 2136 ((u64)idx << ICE_TXD_CTX_QW1_TSO_LEN_S)); 2137 first->tx_flags |= ICE_TX_FLAGS_TSYN; 2138 } 2139 2140 /** 2141 * ice_xmit_frame_ring - Sends buffer on Tx ring 2142 * @skb: send buffer 2143 * @tx_ring: ring to send buffer on 2144 * 2145 * Returns NETDEV_TX_OK if sent, else an error code 2146 */ 2147 static netdev_tx_t 2148 ice_xmit_frame_ring(struct sk_buff *skb, struct ice_tx_ring *tx_ring) 2149 { 2150 struct ice_tx_offload_params offload = { 0 }; 2151 struct ice_vsi *vsi = tx_ring->vsi; 2152 struct ice_tx_buf *first; 2153 struct ethhdr *eth; 2154 unsigned int count; 2155 int tso, csum; 2156 2157 ice_trace(xmit_frame_ring, tx_ring, skb); 2158 2159 if (unlikely(ipv6_hopopt_jumbo_remove(skb))) 2160 goto out_drop; 2161 2162 count = ice_xmit_desc_count(skb); 2163 if (ice_chk_linearize(skb, count)) { 2164 if (__skb_linearize(skb)) 2165 goto out_drop; 2166 count = ice_txd_use_count(skb->len); 2167 tx_ring->ring_stats->tx_stats.tx_linearize++; 2168 } 2169 2170 /* need: 1 descriptor per page * PAGE_SIZE/ICE_MAX_DATA_PER_TXD, 2171 * + 1 desc for skb_head_len/ICE_MAX_DATA_PER_TXD, 2172 * + 4 desc gap to avoid the cache line where head is, 2173 * + 1 desc for context descriptor, 2174 * otherwise try next time 2175 */ 2176 if (ice_maybe_stop_tx(tx_ring, count + ICE_DESCS_PER_CACHE_LINE + 2177 ICE_DESCS_FOR_CTX_DESC)) { 2178 tx_ring->ring_stats->tx_stats.tx_busy++; 2179 return NETDEV_TX_BUSY; 2180 } 2181 2182 /* prefetch for bql data which is infrequently used */ 2183 netdev_txq_bql_enqueue_prefetchw(txring_txq(tx_ring)); 2184 2185 offload.tx_ring = tx_ring; 2186 2187 /* record the location of the first descriptor for this packet */ 2188 first = &tx_ring->tx_buf[tx_ring->next_to_use]; 2189 first->skb = skb; 2190 first->type = ICE_TX_BUF_SKB; 2191 first->bytecount = max_t(unsigned int, skb->len, ETH_ZLEN); 2192 first->gso_segs = 1; 2193 first->tx_flags = 0; 2194 2195 /* prepare the VLAN tagging flags for Tx */ 2196 ice_tx_prepare_vlan_flags(tx_ring, first); 2197 if (first->tx_flags & ICE_TX_FLAGS_HW_OUTER_SINGLE_VLAN) { 2198 offload.cd_qw1 |= (u64)(ICE_TX_DESC_DTYPE_CTX | 2199 (ICE_TX_CTX_DESC_IL2TAG2 << 2200 ICE_TXD_CTX_QW1_CMD_S)); 2201 offload.cd_l2tag2 = first->vid; 2202 } 2203 2204 /* set up TSO offload */ 2205 tso = ice_tso(first, &offload); 2206 if (tso < 0) 2207 goto out_drop; 2208 2209 /* always set up Tx checksum offload */ 2210 csum = ice_tx_csum(first, &offload); 2211 if (csum < 0) 2212 goto out_drop; 2213 2214 /* allow CONTROL frames egress from main VSI if FW LLDP disabled */ 2215 eth = (struct ethhdr *)skb_mac_header(skb); 2216 2217 if ((ice_is_switchdev_running(vsi->back) || 2218 ice_lag_is_switchdev_running(vsi->back)) && 2219 vsi->type != ICE_VSI_SF) 2220 ice_eswitch_set_target_vsi(skb, &offload); 2221 else if (unlikely((skb->priority == TC_PRIO_CONTROL || 2222 eth->h_proto == htons(ETH_P_LLDP)) && 2223 vsi->type == ICE_VSI_PF && 2224 vsi->port_info->qos_cfg.is_sw_lldp)) 2225 offload.cd_qw1 |= (u64)(ICE_TX_DESC_DTYPE_CTX | 2226 ICE_TX_CTX_DESC_SWTCH_UPLINK << 2227 ICE_TXD_CTX_QW1_CMD_S); 2228 2229 ice_tstamp(tx_ring, skb, first, &offload); 2230 2231 if (offload.cd_qw1 & ICE_TX_DESC_DTYPE_CTX) { 2232 struct ice_tx_ctx_desc *cdesc; 2233 u16 i = tx_ring->next_to_use; 2234 2235 /* grab the next descriptor */ 2236 cdesc = ICE_TX_CTX_DESC(tx_ring, i); 2237 i++; 2238 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0; 2239 2240 /* setup context descriptor */ 2241 cdesc->tunneling_params = cpu_to_le32(offload.cd_tunnel_params); 2242 cdesc->l2tag2 = cpu_to_le16(offload.cd_l2tag2); 2243 cdesc->gcs = cpu_to_le16(offload.cd_gcs_params); 2244 cdesc->qw1 = cpu_to_le64(offload.cd_qw1); 2245 } 2246 2247 ice_tx_map(tx_ring, first, &offload); 2248 return NETDEV_TX_OK; 2249 2250 out_drop: 2251 ice_trace(xmit_frame_ring_drop, tx_ring, skb); 2252 dev_kfree_skb_any(skb); 2253 return NETDEV_TX_OK; 2254 } 2255 2256 /** 2257 * ice_start_xmit - Selects the correct VSI and Tx queue to send buffer 2258 * @skb: send buffer 2259 * @netdev: network interface device structure 2260 * 2261 * Returns NETDEV_TX_OK if sent, else an error code 2262 */ 2263 netdev_tx_t ice_start_xmit(struct sk_buff *skb, struct net_device *netdev) 2264 { 2265 struct ice_netdev_priv *np = netdev_priv(netdev); 2266 struct ice_vsi *vsi = np->vsi; 2267 struct ice_tx_ring *tx_ring; 2268 2269 tx_ring = vsi->tx_rings[skb->queue_mapping]; 2270 2271 /* hardware can't handle really short frames, hardware padding works 2272 * beyond this point 2273 */ 2274 if (skb_put_padto(skb, ICE_MIN_TX_LEN)) 2275 return NETDEV_TX_OK; 2276 2277 return ice_xmit_frame_ring(skb, tx_ring); 2278 } 2279 2280 /** 2281 * ice_get_dscp_up - return the UP/TC value for a SKB 2282 * @dcbcfg: DCB config that contains DSCP to UP/TC mapping 2283 * @skb: SKB to query for info to determine UP/TC 2284 * 2285 * This function is to only be called when the PF is in L3 DSCP PFC mode 2286 */ 2287 static u8 ice_get_dscp_up(struct ice_dcbx_cfg *dcbcfg, struct sk_buff *skb) 2288 { 2289 u8 dscp = 0; 2290 2291 if (skb->protocol == htons(ETH_P_IP)) 2292 dscp = ipv4_get_dsfield(ip_hdr(skb)) >> 2; 2293 else if (skb->protocol == htons(ETH_P_IPV6)) 2294 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) >> 2; 2295 2296 return dcbcfg->dscp_map[dscp]; 2297 } 2298 2299 u16 2300 ice_select_queue(struct net_device *netdev, struct sk_buff *skb, 2301 struct net_device *sb_dev) 2302 { 2303 struct ice_pf *pf = ice_netdev_to_pf(netdev); 2304 struct ice_dcbx_cfg *dcbcfg; 2305 2306 dcbcfg = &pf->hw.port_info->qos_cfg.local_dcbx_cfg; 2307 if (dcbcfg->pfc_mode == ICE_QOS_MODE_DSCP) 2308 skb->priority = ice_get_dscp_up(dcbcfg, skb); 2309 2310 return netdev_pick_tx(netdev, skb, sb_dev); 2311 } 2312 2313 /** 2314 * ice_clean_ctrl_tx_irq - interrupt handler for flow director Tx queue 2315 * @tx_ring: tx_ring to clean 2316 */ 2317 void ice_clean_ctrl_tx_irq(struct ice_tx_ring *tx_ring) 2318 { 2319 struct ice_vsi *vsi = tx_ring->vsi; 2320 s16 i = tx_ring->next_to_clean; 2321 int budget = ICE_DFLT_IRQ_WORK; 2322 struct ice_tx_desc *tx_desc; 2323 struct ice_tx_buf *tx_buf; 2324 2325 tx_buf = &tx_ring->tx_buf[i]; 2326 tx_desc = ICE_TX_DESC(tx_ring, i); 2327 i -= tx_ring->count; 2328 2329 do { 2330 struct ice_tx_desc *eop_desc = tx_buf->next_to_watch; 2331 2332 /* if next_to_watch is not set then there is no pending work */ 2333 if (!eop_desc) 2334 break; 2335 2336 /* prevent any other reads prior to eop_desc */ 2337 smp_rmb(); 2338 2339 /* if the descriptor isn't done, no work to do */ 2340 if (!(eop_desc->cmd_type_offset_bsz & 2341 cpu_to_le64(ICE_TX_DESC_DTYPE_DESC_DONE))) 2342 break; 2343 2344 /* clear next_to_watch to prevent false hangs */ 2345 tx_buf->next_to_watch = NULL; 2346 tx_desc->buf_addr = 0; 2347 tx_desc->cmd_type_offset_bsz = 0; 2348 2349 /* move past filter desc */ 2350 tx_buf++; 2351 tx_desc++; 2352 i++; 2353 if (unlikely(!i)) { 2354 i -= tx_ring->count; 2355 tx_buf = tx_ring->tx_buf; 2356 tx_desc = ICE_TX_DESC(tx_ring, 0); 2357 } 2358 2359 /* unmap the data header */ 2360 if (dma_unmap_len(tx_buf, len)) 2361 dma_unmap_single(tx_ring->dev, 2362 dma_unmap_addr(tx_buf, dma), 2363 dma_unmap_len(tx_buf, len), 2364 DMA_TO_DEVICE); 2365 if (tx_buf->type == ICE_TX_BUF_DUMMY) 2366 devm_kfree(tx_ring->dev, tx_buf->raw_buf); 2367 2368 /* clear next_to_watch to prevent false hangs */ 2369 tx_buf->type = ICE_TX_BUF_EMPTY; 2370 tx_buf->tx_flags = 0; 2371 tx_buf->next_to_watch = NULL; 2372 dma_unmap_len_set(tx_buf, len, 0); 2373 tx_desc->buf_addr = 0; 2374 tx_desc->cmd_type_offset_bsz = 0; 2375 2376 /* move past eop_desc for start of next FD desc */ 2377 tx_buf++; 2378 tx_desc++; 2379 i++; 2380 if (unlikely(!i)) { 2381 i -= tx_ring->count; 2382 tx_buf = tx_ring->tx_buf; 2383 tx_desc = ICE_TX_DESC(tx_ring, 0); 2384 } 2385 2386 budget--; 2387 } while (likely(budget)); 2388 2389 i += tx_ring->count; 2390 tx_ring->next_to_clean = i; 2391 2392 /* re-enable interrupt if needed */ 2393 ice_irq_dynamic_ena(&vsi->back->hw, vsi, vsi->q_vectors[0]); 2394 } 2395