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 ice_stats_inc(tx_ring->ring_stats, tx_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_obj(*tstamp_ring); 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.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_clean = 0; 578 rx_ring->next_to_use = 0; 579 } 580 581 /** 582 * ice_free_rx_ring - Free Rx resources 583 * @rx_ring: ring to clean the resources from 584 * 585 * Free all receive software resources 586 */ 587 void ice_free_rx_ring(struct ice_rx_ring *rx_ring) 588 { 589 struct device *dev = ice_pf_to_dev(rx_ring->vsi->back); 590 u32 size; 591 592 ice_clean_rx_ring(rx_ring); 593 WRITE_ONCE(rx_ring->xdp_prog, NULL); 594 if (rx_ring->xsk_pool) { 595 kfree(rx_ring->xdp_buf); 596 rx_ring->xdp_buf = NULL; 597 } 598 599 if (rx_ring->desc) { 600 size = ALIGN(rx_ring->count * sizeof(union ice_32byte_rx_desc), 601 PAGE_SIZE); 602 dmam_free_coherent(dev, size, rx_ring->desc, rx_ring->dma); 603 rx_ring->desc = NULL; 604 } 605 } 606 607 /** 608 * ice_setup_rx_ring - Allocate the Rx descriptors 609 * @rx_ring: the Rx ring to set up 610 * 611 * Return 0 on success, negative on error 612 */ 613 int ice_setup_rx_ring(struct ice_rx_ring *rx_ring) 614 { 615 struct device *dev = ice_pf_to_dev(rx_ring->vsi->back); 616 u32 size; 617 618 /* round up to nearest page */ 619 size = ALIGN(rx_ring->count * sizeof(union ice_32byte_rx_desc), 620 PAGE_SIZE); 621 rx_ring->desc = dmam_alloc_coherent(dev, size, &rx_ring->dma, 622 GFP_KERNEL); 623 if (!rx_ring->desc) { 624 dev_err(dev, "Unable to allocate memory for the Rx descriptor ring, size=%d\n", 625 size); 626 return -ENOMEM; 627 } 628 629 rx_ring->next_to_use = 0; 630 rx_ring->next_to_clean = 0; 631 632 if (ice_is_xdp_ena_vsi(rx_ring->vsi)) 633 WRITE_ONCE(rx_ring->xdp_prog, rx_ring->vsi->xdp_prog); 634 635 return 0; 636 } 637 638 /** 639 * ice_run_xdp - Executes an XDP program on initialized xdp_buff 640 * @rx_ring: Rx ring 641 * @xdp: xdp_buff used as input to the XDP program 642 * @xdp_prog: XDP program to run 643 * @xdp_ring: ring to be used for XDP_TX action 644 * @eop_desc: Last descriptor in packet to read metadata from 645 * 646 * Returns any of ICE_XDP_{PASS, CONSUMED, TX, REDIR} 647 */ 648 static u32 649 ice_run_xdp(struct ice_rx_ring *rx_ring, struct libeth_xdp_buff *xdp, 650 struct bpf_prog *xdp_prog, struct ice_tx_ring *xdp_ring, 651 union ice_32b_rx_flex_desc *eop_desc) 652 { 653 unsigned int ret = ICE_XDP_PASS; 654 u32 act; 655 656 if (!xdp_prog) 657 goto exit; 658 659 xdp->desc = eop_desc; 660 661 act = bpf_prog_run_xdp(xdp_prog, &xdp->base); 662 switch (act) { 663 case XDP_PASS: 664 break; 665 case XDP_TX: 666 if (static_branch_unlikely(&ice_xdp_locking_key)) 667 spin_lock(&xdp_ring->tx_lock); 668 ret = __ice_xmit_xdp_ring(&xdp->base, xdp_ring, false); 669 if (static_branch_unlikely(&ice_xdp_locking_key)) 670 spin_unlock(&xdp_ring->tx_lock); 671 if (ret == ICE_XDP_CONSUMED) 672 goto out_failure; 673 break; 674 case XDP_REDIRECT: 675 if (xdp_do_redirect(rx_ring->netdev, &xdp->base, xdp_prog)) 676 goto out_failure; 677 ret = ICE_XDP_REDIR; 678 break; 679 default: 680 bpf_warn_invalid_xdp_action(rx_ring->netdev, xdp_prog, act); 681 fallthrough; 682 case XDP_ABORTED: 683 out_failure: 684 trace_xdp_exception(rx_ring->netdev, xdp_prog, act); 685 fallthrough; 686 case XDP_DROP: 687 libeth_xdp_return_buff(xdp); 688 ret = ICE_XDP_CONSUMED; 689 } 690 691 exit: 692 return ret; 693 } 694 695 /** 696 * ice_xmit_xdp_ring - submit frame to XDP ring for transmission 697 * @xdpf: XDP frame that will be converted to XDP buff 698 * @xdp_ring: XDP ring for transmission 699 */ 700 static int ice_xmit_xdp_ring(const struct xdp_frame *xdpf, 701 struct ice_tx_ring *xdp_ring) 702 { 703 struct xdp_buff xdp; 704 705 xdp.data_hard_start = (void *)xdpf; 706 xdp.data = xdpf->data; 707 xdp.data_end = xdp.data + xdpf->len; 708 xdp.frame_sz = xdpf->frame_sz; 709 xdp.flags = xdpf->flags; 710 711 return __ice_xmit_xdp_ring(&xdp, xdp_ring, true); 712 } 713 714 /** 715 * ice_xdp_xmit - submit packets to XDP ring for transmission 716 * @dev: netdev 717 * @n: number of XDP frames to be transmitted 718 * @frames: XDP frames to be transmitted 719 * @flags: transmit flags 720 * 721 * Returns number of frames successfully sent. Failed frames 722 * will be free'ed by XDP core. 723 * For error cases, a negative errno code is returned and no-frames 724 * are transmitted (caller must handle freeing frames). 725 */ 726 int 727 ice_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **frames, 728 u32 flags) 729 { 730 struct ice_netdev_priv *np = netdev_priv(dev); 731 unsigned int queue_index = smp_processor_id(); 732 struct ice_vsi *vsi = np->vsi; 733 struct ice_tx_ring *xdp_ring; 734 struct ice_tx_buf *tx_buf; 735 int nxmit = 0, i; 736 737 if (test_bit(ICE_VSI_DOWN, vsi->state)) 738 return -ENETDOWN; 739 740 if (!ice_is_xdp_ena_vsi(vsi)) 741 return -ENXIO; 742 743 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) 744 return -EINVAL; 745 746 if (static_branch_unlikely(&ice_xdp_locking_key)) { 747 queue_index %= vsi->num_xdp_txq; 748 xdp_ring = vsi->xdp_rings[queue_index]; 749 spin_lock(&xdp_ring->tx_lock); 750 } else { 751 /* Generally, should not happen */ 752 if (unlikely(queue_index >= vsi->num_xdp_txq)) 753 return -ENXIO; 754 xdp_ring = vsi->xdp_rings[queue_index]; 755 } 756 757 tx_buf = &xdp_ring->tx_buf[xdp_ring->next_to_use]; 758 for (i = 0; i < n; i++) { 759 const struct xdp_frame *xdpf = frames[i]; 760 int err; 761 762 err = ice_xmit_xdp_ring(xdpf, xdp_ring); 763 if (err != ICE_XDP_TX) 764 break; 765 nxmit++; 766 } 767 768 tx_buf->rs_idx = ice_set_rs_bit(xdp_ring); 769 if (unlikely(flags & XDP_XMIT_FLUSH)) 770 ice_xdp_ring_update_tail(xdp_ring); 771 772 if (static_branch_unlikely(&ice_xdp_locking_key)) 773 spin_unlock(&xdp_ring->tx_lock); 774 775 return nxmit; 776 } 777 778 /** 779 * ice_init_ctrl_rx_descs - Initialize Rx descriptors for control vsi. 780 * @rx_ring: ring to init descriptors on 781 * @count: number of descriptors to initialize 782 */ 783 void ice_init_ctrl_rx_descs(struct ice_rx_ring *rx_ring, u32 count) 784 { 785 union ice_32b_rx_flex_desc *rx_desc; 786 u32 ntu = rx_ring->next_to_use; 787 788 if (!count) 789 return; 790 791 rx_desc = ICE_RX_DESC(rx_ring, ntu); 792 793 do { 794 rx_desc++; 795 ntu++; 796 if (unlikely(ntu == rx_ring->count)) { 797 rx_desc = ICE_RX_DESC(rx_ring, 0); 798 ntu = 0; 799 } 800 801 rx_desc->wb.status_error0 = 0; 802 count--; 803 } while (count); 804 805 if (rx_ring->next_to_use != ntu) 806 ice_release_rx_desc(rx_ring, ntu); 807 } 808 809 /** 810 * ice_alloc_rx_bufs - Replace used receive buffers 811 * @rx_ring: ring to place buffers on 812 * @cleaned_count: number of buffers to replace 813 * 814 * Returns false if all allocations were successful, true if any fail. Returning 815 * true signals to the caller that we didn't replace cleaned_count buffers and 816 * there is more work to do. 817 * 818 * First, try to clean "cleaned_count" Rx buffers. Then refill the cleaned Rx 819 * buffers. Then bump tail at most one time. Grouping like this lets us avoid 820 * multiple tail writes per call. 821 */ 822 bool ice_alloc_rx_bufs(struct ice_rx_ring *rx_ring, unsigned int cleaned_count) 823 { 824 const struct libeth_fq_fp hdr_fq = { 825 .pp = rx_ring->hdr_pp, 826 .fqes = rx_ring->hdr_fqes, 827 .truesize = rx_ring->hdr_truesize, 828 .count = rx_ring->count, 829 }; 830 const struct libeth_fq_fp fq = { 831 .pp = rx_ring->pp, 832 .fqes = rx_ring->rx_fqes, 833 .truesize = rx_ring->truesize, 834 .count = rx_ring->count, 835 }; 836 union ice_32b_rx_flex_desc *rx_desc; 837 u16 ntu = rx_ring->next_to_use; 838 839 /* do nothing if no valid netdev defined */ 840 if (!rx_ring->netdev || !cleaned_count) 841 return false; 842 843 /* get the Rx descriptor and buffer based on next_to_use */ 844 rx_desc = ICE_RX_DESC(rx_ring, ntu); 845 846 do { 847 dma_addr_t addr; 848 849 addr = libeth_rx_alloc(&fq, ntu); 850 if (addr == DMA_MAPPING_ERROR) { 851 ice_stats_inc(rx_ring->ring_stats, rx_page_failed); 852 break; 853 } 854 855 /* Refresh the desc even if buffer_addrs didn't change 856 * because each write-back erases this info. 857 */ 858 rx_desc->read.pkt_addr = cpu_to_le64(addr); 859 860 if (!hdr_fq.pp) 861 goto next; 862 863 addr = libeth_rx_alloc(&hdr_fq, ntu); 864 if (addr == DMA_MAPPING_ERROR) { 865 ice_stats_inc(rx_ring->ring_stats, rx_page_failed); 866 867 libeth_rx_recycle_slow(fq.fqes[ntu].netmem); 868 break; 869 } 870 871 rx_desc->read.hdr_addr = cpu_to_le64(addr); 872 873 next: 874 rx_desc++; 875 ntu++; 876 if (unlikely(ntu == rx_ring->count)) { 877 rx_desc = ICE_RX_DESC(rx_ring, 0); 878 ntu = 0; 879 } 880 881 /* clear the status bits for the next_to_use descriptor */ 882 rx_desc->wb.status_error0 = 0; 883 884 cleaned_count--; 885 } while (cleaned_count); 886 887 if (rx_ring->next_to_use != ntu) 888 ice_release_rx_desc(rx_ring, ntu); 889 890 return !!cleaned_count; 891 } 892 893 /** 894 * ice_clean_ctrl_rx_irq - Clean descriptors from flow director Rx ring 895 * @rx_ring: Rx descriptor ring for ctrl_vsi to transact packets on 896 * 897 * This function cleans Rx descriptors from the ctrl_vsi Rx ring used 898 * to set flow director rules on VFs. 899 */ 900 void ice_clean_ctrl_rx_irq(struct ice_rx_ring *rx_ring) 901 { 902 u32 ntc = rx_ring->next_to_clean; 903 unsigned int total_rx_pkts = 0; 904 u32 cnt = rx_ring->count; 905 906 while (likely(total_rx_pkts < ICE_DFLT_IRQ_WORK)) { 907 struct ice_vsi *ctrl_vsi = rx_ring->vsi; 908 union ice_32b_rx_flex_desc *rx_desc; 909 u16 stat_err_bits; 910 911 rx_desc = ICE_RX_DESC(rx_ring, ntc); 912 913 stat_err_bits = BIT(ICE_RX_FLEX_DESC_STATUS0_DD_S); 914 if (!ice_test_staterr(rx_desc->wb.status_error0, stat_err_bits)) 915 break; 916 917 dma_rmb(); 918 919 if (ctrl_vsi->vf) 920 ice_vc_fdir_irq_handler(ctrl_vsi, rx_desc); 921 922 if (++ntc == cnt) 923 ntc = 0; 924 total_rx_pkts++; 925 } 926 927 rx_ring->next_to_clean = ntc; 928 ice_init_ctrl_rx_descs(rx_ring, ICE_DESC_UNUSED(rx_ring)); 929 } 930 931 /** 932 * ice_clean_rx_irq - Clean completed descriptors from Rx ring - bounce buf 933 * @rx_ring: Rx descriptor ring to transact packets on 934 * @budget: Total limit on number of packets to process 935 * 936 * This function provides a "bounce buffer" approach to Rx interrupt 937 * processing. The advantage to this is that on systems that have 938 * expensive overhead for IOMMU access this provides a means of avoiding 939 * it by maintaining the mapping of the page to the system. 940 * 941 * Returns amount of work completed 942 */ 943 static int ice_clean_rx_irq(struct ice_rx_ring *rx_ring, int budget) 944 { 945 unsigned int total_rx_bytes = 0, total_rx_pkts = 0; 946 struct ice_tx_ring *xdp_ring = NULL; 947 struct bpf_prog *xdp_prog = NULL; 948 u32 ntc = rx_ring->next_to_clean; 949 LIBETH_XDP_ONSTACK_BUFF(xdp); 950 u32 cached_ntu, xdp_verdict; 951 u32 cnt = rx_ring->count; 952 u32 xdp_xmit = 0; 953 bool failure; 954 955 libeth_xdp_init_buff(xdp, &rx_ring->xdp, &rx_ring->xdp_rxq); 956 957 xdp_prog = READ_ONCE(rx_ring->xdp_prog); 958 if (xdp_prog) { 959 xdp_ring = rx_ring->xdp_ring; 960 cached_ntu = xdp_ring->next_to_use; 961 } 962 963 /* start the loop to process Rx packets bounded by 'budget' */ 964 while (likely(total_rx_pkts < (unsigned int)budget)) { 965 union ice_32b_rx_flex_desc *rx_desc; 966 struct libeth_fqe *rx_buf; 967 struct sk_buff *skb; 968 unsigned int size; 969 u16 stat_err_bits; 970 u16 vlan_tci; 971 bool rxe; 972 973 /* get the Rx desc from Rx ring based on 'next_to_clean' */ 974 rx_desc = ICE_RX_DESC(rx_ring, ntc); 975 976 /* 977 * The DD bit will always be zero for unused descriptors 978 * because it's cleared in cleanup or when setting the DMA 979 * address of the header buffer, which never uses the DD bit. 980 * If the hardware wrote the descriptor, it will be non-zero. 981 */ 982 stat_err_bits = BIT(ICE_RX_FLEX_DESC_STATUS0_DD_S); 983 if (!ice_test_staterr(rx_desc->wb.status_error0, stat_err_bits)) 984 break; 985 986 /* This memory barrier is needed to keep us from reading 987 * any other fields out of the rx_desc until we know the 988 * DD bit is set. 989 */ 990 dma_rmb(); 991 992 ice_trace(clean_rx_irq, rx_ring, rx_desc); 993 994 stat_err_bits = BIT(ICE_RX_FLEX_DESC_STATUS0_HBO_S) | 995 BIT(ICE_RX_FLEX_DESC_STATUS0_RXE_S); 996 rxe = ice_test_staterr(rx_desc->wb.status_error0, 997 stat_err_bits); 998 999 if (!rx_ring->hdr_pp) 1000 goto payload; 1001 1002 size = le16_get_bits(rx_desc->wb.hdr_len_sph_flex_flags1, 1003 ICE_RX_FLEX_DESC_HDR_LEN_M); 1004 if (unlikely(rxe)) 1005 size = 0; 1006 1007 rx_buf = &rx_ring->hdr_fqes[ntc]; 1008 libeth_xdp_process_buff(xdp, rx_buf, size); 1009 rx_buf->netmem = 0; 1010 1011 payload: 1012 size = le16_to_cpu(rx_desc->wb.pkt_len) & 1013 ICE_RX_FLX_DESC_PKT_LEN_M; 1014 if (unlikely(rxe)) 1015 size = 0; 1016 1017 /* retrieve a buffer from the ring */ 1018 rx_buf = &rx_ring->rx_fqes[ntc]; 1019 libeth_xdp_process_buff(xdp, rx_buf, size); 1020 1021 if (++ntc == cnt) 1022 ntc = 0; 1023 1024 /* skip if it is NOP desc */ 1025 if (ice_is_non_eop(rx_ring, rx_desc) || unlikely(!xdp->data)) 1026 continue; 1027 1028 xdp_verdict = ice_run_xdp(rx_ring, xdp, xdp_prog, xdp_ring, rx_desc); 1029 if (xdp_verdict == ICE_XDP_PASS) 1030 goto construct_skb; 1031 1032 if (xdp_verdict & (ICE_XDP_TX | ICE_XDP_REDIR)) 1033 xdp_xmit |= xdp_verdict; 1034 total_rx_bytes += xdp_get_buff_len(&xdp->base); 1035 total_rx_pkts++; 1036 1037 xdp->data = NULL; 1038 continue; 1039 1040 construct_skb: 1041 skb = xdp_build_skb_from_buff(&xdp->base); 1042 xdp->data = NULL; 1043 1044 /* exit if we failed to retrieve a buffer */ 1045 if (!skb) { 1046 libeth_xdp_return_buff_slow(xdp); 1047 ice_stats_inc(rx_ring->ring_stats, rx_buf_failed); 1048 continue; 1049 } 1050 1051 vlan_tci = ice_get_vlan_tci(rx_desc); 1052 1053 /* probably a little skewed due to removing CRC */ 1054 total_rx_bytes += skb->len; 1055 1056 /* populate checksum, VLAN, and protocol */ 1057 ice_process_skb_fields(rx_ring, rx_desc, skb); 1058 1059 ice_trace(clean_rx_irq_indicate, rx_ring, rx_desc, skb); 1060 /* send completed skb up the stack */ 1061 ice_receive_skb(rx_ring, skb, vlan_tci); 1062 1063 /* update budget accounting */ 1064 total_rx_pkts++; 1065 } 1066 1067 rx_ring->next_to_clean = ntc; 1068 /* return up to cleaned_count buffers to hardware */ 1069 failure = ice_alloc_rx_bufs(rx_ring, ICE_DESC_UNUSED(rx_ring)); 1070 1071 if (xdp_xmit) 1072 ice_finalize_xdp_rx(xdp_ring, xdp_xmit, cached_ntu); 1073 1074 libeth_xdp_save_buff(&rx_ring->xdp, xdp); 1075 1076 if (rx_ring->ring_stats) 1077 ice_update_rx_ring_stats(rx_ring, total_rx_pkts, 1078 total_rx_bytes); 1079 1080 /* guarantee a trip back through this routine if there was a failure */ 1081 return failure ? budget : (int)total_rx_pkts; 1082 } 1083 1084 static void __ice_update_sample(struct ice_q_vector *q_vector, 1085 struct ice_ring_container *rc, 1086 struct dim_sample *sample, 1087 bool is_tx) 1088 { 1089 u64 total_packets = 0, total_bytes = 0, pkts, bytes; 1090 1091 if (is_tx) { 1092 struct ice_tx_ring *tx_ring; 1093 1094 ice_for_each_tx_ring(tx_ring, *rc) { 1095 if (!tx_ring->ring_stats) 1096 continue; 1097 1098 ice_fetch_tx_ring_stats(tx_ring, &pkts, &bytes); 1099 1100 total_packets += pkts; 1101 total_bytes += bytes; 1102 } 1103 } else { 1104 struct ice_rx_ring *rx_ring; 1105 1106 ice_for_each_rx_ring(rx_ring, *rc) { 1107 if (!rx_ring->ring_stats) 1108 continue; 1109 1110 ice_fetch_rx_ring_stats(rx_ring, &pkts, &bytes); 1111 1112 total_packets += pkts; 1113 total_bytes += bytes; 1114 } 1115 } 1116 1117 dim_update_sample(q_vector->total_events, 1118 total_packets, total_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 ice_stats_inc(tx_ring->ring_stats, tx_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 count = ice_xmit_desc_count(skb); 2160 if (ice_chk_linearize(skb, count)) { 2161 if (__skb_linearize(skb)) 2162 goto out_drop; 2163 count = ice_txd_use_count(skb->len); 2164 ice_stats_inc(tx_ring->ring_stats, tx_linearize); 2165 } 2166 2167 /* need: 1 descriptor per page * PAGE_SIZE/ICE_MAX_DATA_PER_TXD, 2168 * + 1 desc for skb_head_len/ICE_MAX_DATA_PER_TXD, 2169 * + 4 desc gap to avoid the cache line where head is, 2170 * + 1 desc for context descriptor, 2171 * otherwise try next time 2172 */ 2173 if (ice_maybe_stop_tx(tx_ring, count + ICE_DESCS_PER_CACHE_LINE + 2174 ICE_DESCS_FOR_CTX_DESC)) { 2175 ice_stats_inc(tx_ring->ring_stats, tx_busy); 2176 return NETDEV_TX_BUSY; 2177 } 2178 2179 /* prefetch for bql data which is infrequently used */ 2180 netdev_txq_bql_enqueue_prefetchw(txring_txq(tx_ring)); 2181 2182 offload.tx_ring = tx_ring; 2183 2184 /* record the location of the first descriptor for this packet */ 2185 first = &tx_ring->tx_buf[tx_ring->next_to_use]; 2186 first->skb = skb; 2187 first->type = ICE_TX_BUF_SKB; 2188 first->bytecount = max_t(unsigned int, skb->len, ETH_ZLEN); 2189 first->gso_segs = 1; 2190 first->tx_flags = 0; 2191 2192 /* prepare the VLAN tagging flags for Tx */ 2193 ice_tx_prepare_vlan_flags(tx_ring, first); 2194 if (first->tx_flags & ICE_TX_FLAGS_HW_OUTER_SINGLE_VLAN) { 2195 offload.cd_qw1 |= (u64)(ICE_TX_DESC_DTYPE_CTX | 2196 (ICE_TX_CTX_DESC_IL2TAG2 << 2197 ICE_TXD_CTX_QW1_CMD_S)); 2198 offload.cd_l2tag2 = first->vid; 2199 } 2200 2201 /* set up TSO offload */ 2202 tso = ice_tso(first, &offload); 2203 if (tso < 0) 2204 goto out_drop; 2205 2206 /* always set up Tx checksum offload */ 2207 csum = ice_tx_csum(first, &offload); 2208 if (csum < 0) 2209 goto out_drop; 2210 2211 /* allow CONTROL frames egress from main VSI if FW LLDP disabled */ 2212 eth = (struct ethhdr *)skb_mac_header(skb); 2213 2214 if ((ice_is_switchdev_running(vsi->back) || 2215 ice_lag_is_switchdev_running(vsi->back)) && 2216 vsi->type != ICE_VSI_SF) 2217 ice_eswitch_set_target_vsi(skb, &offload); 2218 else if (unlikely((skb->priority == TC_PRIO_CONTROL || 2219 eth->h_proto == htons(ETH_P_LLDP)) && 2220 vsi->type == ICE_VSI_PF && 2221 vsi->port_info->qos_cfg.is_sw_lldp)) 2222 offload.cd_qw1 |= (u64)(ICE_TX_DESC_DTYPE_CTX | 2223 ICE_TX_CTX_DESC_SWTCH_UPLINK << 2224 ICE_TXD_CTX_QW1_CMD_S); 2225 2226 ice_tstamp(tx_ring, skb, first, &offload); 2227 2228 if (offload.cd_qw1 & ICE_TX_DESC_DTYPE_CTX) { 2229 struct ice_tx_ctx_desc *cdesc; 2230 u16 i = tx_ring->next_to_use; 2231 2232 /* grab the next descriptor */ 2233 cdesc = ICE_TX_CTX_DESC(tx_ring, i); 2234 i++; 2235 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0; 2236 2237 /* setup context descriptor */ 2238 cdesc->tunneling_params = cpu_to_le32(offload.cd_tunnel_params); 2239 cdesc->l2tag2 = cpu_to_le16(offload.cd_l2tag2); 2240 cdesc->gcs = cpu_to_le16(offload.cd_gcs_params); 2241 cdesc->qw1 = cpu_to_le64(offload.cd_qw1); 2242 } 2243 2244 ice_tx_map(tx_ring, first, &offload); 2245 return NETDEV_TX_OK; 2246 2247 out_drop: 2248 ice_trace(xmit_frame_ring_drop, tx_ring, skb); 2249 dev_kfree_skb_any(skb); 2250 return NETDEV_TX_OK; 2251 } 2252 2253 /** 2254 * ice_start_xmit - Selects the correct VSI and Tx queue to send buffer 2255 * @skb: send buffer 2256 * @netdev: network interface device structure 2257 * 2258 * Returns NETDEV_TX_OK if sent, else an error code 2259 */ 2260 netdev_tx_t ice_start_xmit(struct sk_buff *skb, struct net_device *netdev) 2261 { 2262 struct ice_netdev_priv *np = netdev_priv(netdev); 2263 struct ice_vsi *vsi = np->vsi; 2264 struct ice_tx_ring *tx_ring; 2265 2266 tx_ring = vsi->tx_rings[skb->queue_mapping]; 2267 2268 /* hardware can't handle really short frames, hardware padding works 2269 * beyond this point 2270 */ 2271 if (skb_put_padto(skb, ICE_MIN_TX_LEN)) 2272 return NETDEV_TX_OK; 2273 2274 return ice_xmit_frame_ring(skb, tx_ring); 2275 } 2276 2277 /** 2278 * ice_get_dscp_up - return the UP/TC value for a SKB 2279 * @dcbcfg: DCB config that contains DSCP to UP/TC mapping 2280 * @skb: SKB to query for info to determine UP/TC 2281 * 2282 * This function is to only be called when the PF is in L3 DSCP PFC mode 2283 */ 2284 static u8 ice_get_dscp_up(struct ice_dcbx_cfg *dcbcfg, struct sk_buff *skb) 2285 { 2286 u8 dscp = 0; 2287 2288 if (skb->protocol == htons(ETH_P_IP)) 2289 dscp = ipv4_get_dsfield(ip_hdr(skb)) >> 2; 2290 else if (skb->protocol == htons(ETH_P_IPV6)) 2291 dscp = ipv6_get_dsfield(ipv6_hdr(skb)) >> 2; 2292 2293 return dcbcfg->dscp_map[dscp]; 2294 } 2295 2296 u16 2297 ice_select_queue(struct net_device *netdev, struct sk_buff *skb, 2298 struct net_device *sb_dev) 2299 { 2300 struct ice_pf *pf = ice_netdev_to_pf(netdev); 2301 struct ice_dcbx_cfg *dcbcfg; 2302 2303 dcbcfg = &pf->hw.port_info->qos_cfg.local_dcbx_cfg; 2304 if (dcbcfg->pfc_mode == ICE_QOS_MODE_DSCP) 2305 skb->priority = ice_get_dscp_up(dcbcfg, skb); 2306 2307 return netdev_pick_tx(netdev, skb, sb_dev); 2308 } 2309 2310 /** 2311 * ice_clean_ctrl_tx_irq - interrupt handler for flow director Tx queue 2312 * @tx_ring: tx_ring to clean 2313 */ 2314 void ice_clean_ctrl_tx_irq(struct ice_tx_ring *tx_ring) 2315 { 2316 struct ice_vsi *vsi = tx_ring->vsi; 2317 s16 i = tx_ring->next_to_clean; 2318 int budget = ICE_DFLT_IRQ_WORK; 2319 struct ice_tx_desc *tx_desc; 2320 struct ice_tx_buf *tx_buf; 2321 2322 tx_buf = &tx_ring->tx_buf[i]; 2323 tx_desc = ICE_TX_DESC(tx_ring, i); 2324 i -= tx_ring->count; 2325 2326 do { 2327 struct ice_tx_desc *eop_desc = tx_buf->next_to_watch; 2328 2329 /* if next_to_watch is not set then there is no pending work */ 2330 if (!eop_desc) 2331 break; 2332 2333 /* prevent any other reads prior to eop_desc */ 2334 smp_rmb(); 2335 2336 /* if the descriptor isn't done, no work to do */ 2337 if (!(eop_desc->cmd_type_offset_bsz & 2338 cpu_to_le64(ICE_TX_DESC_DTYPE_DESC_DONE))) 2339 break; 2340 2341 /* clear next_to_watch to prevent false hangs */ 2342 tx_buf->next_to_watch = NULL; 2343 tx_desc->buf_addr = 0; 2344 tx_desc->cmd_type_offset_bsz = 0; 2345 2346 /* move past filter desc */ 2347 tx_buf++; 2348 tx_desc++; 2349 i++; 2350 if (unlikely(!i)) { 2351 i -= tx_ring->count; 2352 tx_buf = tx_ring->tx_buf; 2353 tx_desc = ICE_TX_DESC(tx_ring, 0); 2354 } 2355 2356 /* unmap the data header */ 2357 if (dma_unmap_len(tx_buf, len)) 2358 dma_unmap_single(tx_ring->dev, 2359 dma_unmap_addr(tx_buf, dma), 2360 dma_unmap_len(tx_buf, len), 2361 DMA_TO_DEVICE); 2362 if (tx_buf->type == ICE_TX_BUF_DUMMY) 2363 devm_kfree(tx_ring->dev, tx_buf->raw_buf); 2364 2365 /* clear next_to_watch to prevent false hangs */ 2366 tx_buf->type = ICE_TX_BUF_EMPTY; 2367 tx_buf->tx_flags = 0; 2368 tx_buf->next_to_watch = NULL; 2369 dma_unmap_len_set(tx_buf, len, 0); 2370 tx_desc->buf_addr = 0; 2371 tx_desc->cmd_type_offset_bsz = 0; 2372 2373 /* move past eop_desc for start of next FD desc */ 2374 tx_buf++; 2375 tx_desc++; 2376 i++; 2377 if (unlikely(!i)) { 2378 i -= tx_ring->count; 2379 tx_buf = tx_ring->tx_buf; 2380 tx_desc = ICE_TX_DESC(tx_ring, 0); 2381 } 2382 2383 budget--; 2384 } while (likely(budget)); 2385 2386 i += tx_ring->count; 2387 tx_ring->next_to_clean = i; 2388 2389 /* re-enable interrupt if needed */ 2390 ice_irq_dynamic_ena(&vsi->back->hw, vsi, vsi->q_vectors[0]); 2391 } 2392