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