1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright(c) 2013 - 2018 Intel Corporation. */ 3 4 #include <linux/prefetch.h> 5 6 #include "iavf.h" 7 #include "iavf_trace.h" 8 #include "iavf_prototype.h" 9 10 static inline __le64 build_ctob(u32 td_cmd, u32 td_offset, unsigned int size, 11 u32 td_tag) 12 { 13 return cpu_to_le64(IAVF_TX_DESC_DTYPE_DATA | 14 ((u64)td_cmd << IAVF_TXD_QW1_CMD_SHIFT) | 15 ((u64)td_offset << IAVF_TXD_QW1_OFFSET_SHIFT) | 16 ((u64)size << IAVF_TXD_QW1_TX_BUF_SZ_SHIFT) | 17 ((u64)td_tag << IAVF_TXD_QW1_L2TAG1_SHIFT)); 18 } 19 20 #define IAVF_TXD_CMD (IAVF_TX_DESC_CMD_EOP | IAVF_TX_DESC_CMD_RS) 21 22 /** 23 * iavf_unmap_and_free_tx_resource - Release a Tx buffer 24 * @ring: the ring that owns the buffer 25 * @tx_buffer: the buffer to free 26 **/ 27 static void iavf_unmap_and_free_tx_resource(struct iavf_ring *ring, 28 struct iavf_tx_buffer *tx_buffer) 29 { 30 if (tx_buffer->skb) { 31 if (tx_buffer->tx_flags & IAVF_TX_FLAGS_FD_SB) 32 kfree(tx_buffer->raw_buf); 33 else 34 dev_kfree_skb_any(tx_buffer->skb); 35 if (dma_unmap_len(tx_buffer, len)) 36 dma_unmap_single(ring->dev, 37 dma_unmap_addr(tx_buffer, dma), 38 dma_unmap_len(tx_buffer, len), 39 DMA_TO_DEVICE); 40 } else if (dma_unmap_len(tx_buffer, len)) { 41 dma_unmap_page(ring->dev, 42 dma_unmap_addr(tx_buffer, dma), 43 dma_unmap_len(tx_buffer, len), 44 DMA_TO_DEVICE); 45 } 46 47 tx_buffer->next_to_watch = NULL; 48 tx_buffer->skb = NULL; 49 dma_unmap_len_set(tx_buffer, len, 0); 50 /* tx_buffer must be completely set up in the transmit path */ 51 } 52 53 /** 54 * iavf_clean_tx_ring - Free any empty Tx buffers 55 * @tx_ring: ring to be cleaned 56 **/ 57 void iavf_clean_tx_ring(struct iavf_ring *tx_ring) 58 { 59 unsigned long bi_size; 60 u16 i; 61 62 /* ring already cleared, nothing to do */ 63 if (!tx_ring->tx_bi) 64 return; 65 66 /* Free all the Tx ring sk_buffs */ 67 for (i = 0; i < tx_ring->count; i++) 68 iavf_unmap_and_free_tx_resource(tx_ring, &tx_ring->tx_bi[i]); 69 70 bi_size = sizeof(struct iavf_tx_buffer) * tx_ring->count; 71 memset(tx_ring->tx_bi, 0, bi_size); 72 73 /* Zero out the descriptor ring */ 74 memset(tx_ring->desc, 0, tx_ring->size); 75 76 tx_ring->next_to_use = 0; 77 tx_ring->next_to_clean = 0; 78 79 if (!tx_ring->netdev) 80 return; 81 82 /* cleanup Tx queue statistics */ 83 netdev_tx_reset_queue(txring_txq(tx_ring)); 84 } 85 86 /** 87 * iavf_free_tx_resources - Free Tx resources per queue 88 * @tx_ring: Tx descriptor ring for a specific queue 89 * 90 * Free all transmit software resources 91 **/ 92 void iavf_free_tx_resources(struct iavf_ring *tx_ring) 93 { 94 iavf_clean_tx_ring(tx_ring); 95 kfree(tx_ring->tx_bi); 96 tx_ring->tx_bi = NULL; 97 98 if (tx_ring->desc) { 99 dma_free_coherent(tx_ring->dev, tx_ring->size, 100 tx_ring->desc, tx_ring->dma); 101 tx_ring->desc = NULL; 102 } 103 } 104 105 /** 106 * iavf_get_tx_pending - how many Tx descriptors not processed 107 * @ring: the ring of descriptors 108 * @in_sw: is tx_pending being checked in SW or HW 109 * 110 * Since there is no access to the ring head register 111 * in XL710, we need to use our local copies 112 **/ 113 u32 iavf_get_tx_pending(struct iavf_ring *ring, bool in_sw) 114 { 115 u32 head, tail; 116 117 head = ring->next_to_clean; 118 tail = readl(ring->tail); 119 120 if (head != tail) 121 return (head < tail) ? 122 tail - head : (tail + ring->count - head); 123 124 return 0; 125 } 126 127 /** 128 * iavf_detect_recover_hung - Function to detect and recover hung_queues 129 * @vsi: pointer to vsi struct with tx queues 130 * 131 * VSI has netdev and netdev has TX queues. This function is to check each of 132 * those TX queues if they are hung, trigger recovery by issuing SW interrupt. 133 **/ 134 void iavf_detect_recover_hung(struct iavf_vsi *vsi) 135 { 136 struct iavf_ring *tx_ring = NULL; 137 struct net_device *netdev; 138 unsigned int i; 139 int packets; 140 141 if (!vsi) 142 return; 143 144 if (test_bit(__IAVF_VSI_DOWN, vsi->state)) 145 return; 146 147 netdev = vsi->netdev; 148 if (!netdev) 149 return; 150 151 if (!netif_carrier_ok(netdev)) 152 return; 153 154 for (i = 0; i < vsi->back->num_active_queues; i++) { 155 tx_ring = &vsi->back->tx_rings[i]; 156 if (tx_ring && tx_ring->desc) { 157 /* If packet counter has not changed the queue is 158 * likely stalled, so force an interrupt for this 159 * queue. 160 * 161 * prev_pkt_ctr would be negative if there was no 162 * pending work. 163 */ 164 packets = tx_ring->stats.packets & INT_MAX; 165 if (tx_ring->tx_stats.prev_pkt_ctr == packets) { 166 iavf_force_wb(vsi, tx_ring->q_vector); 167 continue; 168 } 169 170 /* Memory barrier between read of packet count and call 171 * to iavf_get_tx_pending() 172 */ 173 smp_rmb(); 174 tx_ring->tx_stats.prev_pkt_ctr = 175 iavf_get_tx_pending(tx_ring, true) ? packets : -1; 176 } 177 } 178 } 179 180 #define WB_STRIDE 4 181 182 /** 183 * iavf_clean_tx_irq - Reclaim resources after transmit completes 184 * @vsi: the VSI we care about 185 * @tx_ring: Tx ring to clean 186 * @napi_budget: Used to determine if we are in netpoll 187 * 188 * Returns true if there's any budget left (e.g. the clean is finished) 189 **/ 190 static bool iavf_clean_tx_irq(struct iavf_vsi *vsi, 191 struct iavf_ring *tx_ring, int napi_budget) 192 { 193 int i = tx_ring->next_to_clean; 194 struct iavf_tx_buffer *tx_buf; 195 struct iavf_tx_desc *tx_desc; 196 unsigned int total_bytes = 0, total_packets = 0; 197 unsigned int budget = vsi->work_limit; 198 199 tx_buf = &tx_ring->tx_bi[i]; 200 tx_desc = IAVF_TX_DESC(tx_ring, i); 201 i -= tx_ring->count; 202 203 do { 204 struct iavf_tx_desc *eop_desc = tx_buf->next_to_watch; 205 206 /* if next_to_watch is not set then there is no work pending */ 207 if (!eop_desc) 208 break; 209 210 /* prevent any other reads prior to eop_desc */ 211 smp_rmb(); 212 213 iavf_trace(clean_tx_irq, tx_ring, tx_desc, tx_buf); 214 /* if the descriptor isn't done, no work yet to do */ 215 if (!(eop_desc->cmd_type_offset_bsz & 216 cpu_to_le64(IAVF_TX_DESC_DTYPE_DESC_DONE))) 217 break; 218 219 /* clear next_to_watch to prevent false hangs */ 220 tx_buf->next_to_watch = NULL; 221 222 /* update the statistics for this packet */ 223 total_bytes += tx_buf->bytecount; 224 total_packets += tx_buf->gso_segs; 225 226 /* free the skb */ 227 napi_consume_skb(tx_buf->skb, napi_budget); 228 229 /* unmap skb header data */ 230 dma_unmap_single(tx_ring->dev, 231 dma_unmap_addr(tx_buf, dma), 232 dma_unmap_len(tx_buf, len), 233 DMA_TO_DEVICE); 234 235 /* clear tx_buffer data */ 236 tx_buf->skb = NULL; 237 dma_unmap_len_set(tx_buf, len, 0); 238 239 /* unmap remaining buffers */ 240 while (tx_desc != eop_desc) { 241 iavf_trace(clean_tx_irq_unmap, 242 tx_ring, tx_desc, tx_buf); 243 244 tx_buf++; 245 tx_desc++; 246 i++; 247 if (unlikely(!i)) { 248 i -= tx_ring->count; 249 tx_buf = tx_ring->tx_bi; 250 tx_desc = IAVF_TX_DESC(tx_ring, 0); 251 } 252 253 /* unmap any remaining paged data */ 254 if (dma_unmap_len(tx_buf, len)) { 255 dma_unmap_page(tx_ring->dev, 256 dma_unmap_addr(tx_buf, dma), 257 dma_unmap_len(tx_buf, len), 258 DMA_TO_DEVICE); 259 dma_unmap_len_set(tx_buf, len, 0); 260 } 261 } 262 263 /* move us one more past the eop_desc for start of next pkt */ 264 tx_buf++; 265 tx_desc++; 266 i++; 267 if (unlikely(!i)) { 268 i -= tx_ring->count; 269 tx_buf = tx_ring->tx_bi; 270 tx_desc = IAVF_TX_DESC(tx_ring, 0); 271 } 272 273 prefetch(tx_desc); 274 275 /* update budget accounting */ 276 budget--; 277 } while (likely(budget)); 278 279 i += tx_ring->count; 280 tx_ring->next_to_clean = i; 281 u64_stats_update_begin(&tx_ring->syncp); 282 tx_ring->stats.bytes += total_bytes; 283 tx_ring->stats.packets += total_packets; 284 u64_stats_update_end(&tx_ring->syncp); 285 tx_ring->q_vector->tx.total_bytes += total_bytes; 286 tx_ring->q_vector->tx.total_packets += total_packets; 287 288 if (tx_ring->flags & IAVF_TXR_FLAGS_WB_ON_ITR) { 289 /* check to see if there are < 4 descriptors 290 * waiting to be written back, then kick the hardware to force 291 * them to be written back in case we stay in NAPI. 292 * In this mode on X722 we do not enable Interrupt. 293 */ 294 unsigned int j = iavf_get_tx_pending(tx_ring, false); 295 296 if (budget && 297 ((j / WB_STRIDE) == 0) && (j > 0) && 298 !test_bit(__IAVF_VSI_DOWN, vsi->state) && 299 (IAVF_DESC_UNUSED(tx_ring) != tx_ring->count)) 300 tx_ring->arm_wb = true; 301 } 302 303 /* notify netdev of completed buffers */ 304 netdev_tx_completed_queue(txring_txq(tx_ring), 305 total_packets, total_bytes); 306 307 #define TX_WAKE_THRESHOLD ((s16)(DESC_NEEDED * 2)) 308 if (unlikely(total_packets && netif_carrier_ok(tx_ring->netdev) && 309 (IAVF_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD))) { 310 /* Make sure that anybody stopping the queue after this 311 * sees the new next_to_clean. 312 */ 313 smp_mb(); 314 if (__netif_subqueue_stopped(tx_ring->netdev, 315 tx_ring->queue_index) && 316 !test_bit(__IAVF_VSI_DOWN, vsi->state)) { 317 netif_wake_subqueue(tx_ring->netdev, 318 tx_ring->queue_index); 319 ++tx_ring->tx_stats.restart_queue; 320 } 321 } 322 323 return !!budget; 324 } 325 326 /** 327 * iavf_enable_wb_on_itr - Arm hardware to do a wb, interrupts are not enabled 328 * @vsi: the VSI we care about 329 * @q_vector: the vector on which to enable writeback 330 * 331 **/ 332 static void iavf_enable_wb_on_itr(struct iavf_vsi *vsi, 333 struct iavf_q_vector *q_vector) 334 { 335 u16 flags = q_vector->tx.ring[0].flags; 336 u32 val; 337 338 if (!(flags & IAVF_TXR_FLAGS_WB_ON_ITR)) 339 return; 340 341 if (q_vector->arm_wb_state) 342 return; 343 344 val = IAVF_VFINT_DYN_CTLN1_WB_ON_ITR_MASK | 345 IAVF_VFINT_DYN_CTLN1_ITR_INDX_MASK; /* set noitr */ 346 347 wr32(&vsi->back->hw, 348 IAVF_VFINT_DYN_CTLN1(q_vector->reg_idx), val); 349 q_vector->arm_wb_state = true; 350 } 351 352 /** 353 * iavf_force_wb - Issue SW Interrupt so HW does a wb 354 * @vsi: the VSI we care about 355 * @q_vector: the vector on which to force writeback 356 * 357 **/ 358 void iavf_force_wb(struct iavf_vsi *vsi, struct iavf_q_vector *q_vector) 359 { 360 u32 val = IAVF_VFINT_DYN_CTLN1_INTENA_MASK | 361 IAVF_VFINT_DYN_CTLN1_ITR_INDX_MASK | /* set noitr */ 362 IAVF_VFINT_DYN_CTLN1_SWINT_TRIG_MASK | 363 IAVF_VFINT_DYN_CTLN1_SW_ITR_INDX_ENA_MASK 364 /* allow 00 to be written to the index */; 365 366 wr32(&vsi->back->hw, 367 IAVF_VFINT_DYN_CTLN1(q_vector->reg_idx), 368 val); 369 } 370 371 static inline bool iavf_container_is_rx(struct iavf_q_vector *q_vector, 372 struct iavf_ring_container *rc) 373 { 374 return &q_vector->rx == rc; 375 } 376 377 static inline unsigned int iavf_itr_divisor(struct iavf_q_vector *q_vector) 378 { 379 unsigned int divisor; 380 381 switch (q_vector->adapter->link_speed) { 382 case VIRTCHNL_LINK_SPEED_40GB: 383 divisor = IAVF_ITR_ADAPTIVE_MIN_INC * 1024; 384 break; 385 case VIRTCHNL_LINK_SPEED_25GB: 386 case VIRTCHNL_LINK_SPEED_20GB: 387 divisor = IAVF_ITR_ADAPTIVE_MIN_INC * 512; 388 break; 389 default: 390 case VIRTCHNL_LINK_SPEED_10GB: 391 divisor = IAVF_ITR_ADAPTIVE_MIN_INC * 256; 392 break; 393 case VIRTCHNL_LINK_SPEED_1GB: 394 case VIRTCHNL_LINK_SPEED_100MB: 395 divisor = IAVF_ITR_ADAPTIVE_MIN_INC * 32; 396 break; 397 } 398 399 return divisor; 400 } 401 402 /** 403 * iavf_update_itr - update the dynamic ITR value based on statistics 404 * @q_vector: structure containing interrupt and ring information 405 * @rc: structure containing ring performance data 406 * 407 * Stores a new ITR value based on packets and byte 408 * counts during the last interrupt. The advantage of per interrupt 409 * computation is faster updates and more accurate ITR for the current 410 * traffic pattern. Constants in this function were computed 411 * based on theoretical maximum wire speed and thresholds were set based 412 * on testing data as well as attempting to minimize response time 413 * while increasing bulk throughput. 414 **/ 415 static void iavf_update_itr(struct iavf_q_vector *q_vector, 416 struct iavf_ring_container *rc) 417 { 418 unsigned int avg_wire_size, packets, bytes, itr; 419 unsigned long next_update = jiffies; 420 421 /* If we don't have any rings just leave ourselves set for maximum 422 * possible latency so we take ourselves out of the equation. 423 */ 424 if (!rc->ring || !ITR_IS_DYNAMIC(rc->ring->itr_setting)) 425 return; 426 427 /* For Rx we want to push the delay up and default to low latency. 428 * for Tx we want to pull the delay down and default to high latency. 429 */ 430 itr = iavf_container_is_rx(q_vector, rc) ? 431 IAVF_ITR_ADAPTIVE_MIN_USECS | IAVF_ITR_ADAPTIVE_LATENCY : 432 IAVF_ITR_ADAPTIVE_MAX_USECS | IAVF_ITR_ADAPTIVE_LATENCY; 433 434 /* If we didn't update within up to 1 - 2 jiffies we can assume 435 * that either packets are coming in so slow there hasn't been 436 * any work, or that there is so much work that NAPI is dealing 437 * with interrupt moderation and we don't need to do anything. 438 */ 439 if (time_after(next_update, rc->next_update)) 440 goto clear_counts; 441 442 /* If itr_countdown is set it means we programmed an ITR within 443 * the last 4 interrupt cycles. This has a side effect of us 444 * potentially firing an early interrupt. In order to work around 445 * this we need to throw out any data received for a few 446 * interrupts following the update. 447 */ 448 if (q_vector->itr_countdown) { 449 itr = rc->target_itr; 450 goto clear_counts; 451 } 452 453 packets = rc->total_packets; 454 bytes = rc->total_bytes; 455 456 if (iavf_container_is_rx(q_vector, rc)) { 457 /* If Rx there are 1 to 4 packets and bytes are less than 458 * 9000 assume insufficient data to use bulk rate limiting 459 * approach unless Tx is already in bulk rate limiting. We 460 * are likely latency driven. 461 */ 462 if (packets && packets < 4 && bytes < 9000 && 463 (q_vector->tx.target_itr & IAVF_ITR_ADAPTIVE_LATENCY)) { 464 itr = IAVF_ITR_ADAPTIVE_LATENCY; 465 goto adjust_by_size; 466 } 467 } else if (packets < 4) { 468 /* If we have Tx and Rx ITR maxed and Tx ITR is running in 469 * bulk mode and we are receiving 4 or fewer packets just 470 * reset the ITR_ADAPTIVE_LATENCY bit for latency mode so 471 * that the Rx can relax. 472 */ 473 if (rc->target_itr == IAVF_ITR_ADAPTIVE_MAX_USECS && 474 (q_vector->rx.target_itr & IAVF_ITR_MASK) == 475 IAVF_ITR_ADAPTIVE_MAX_USECS) 476 goto clear_counts; 477 } else if (packets > 32) { 478 /* If we have processed over 32 packets in a single interrupt 479 * for Tx assume we need to switch over to "bulk" mode. 480 */ 481 rc->target_itr &= ~IAVF_ITR_ADAPTIVE_LATENCY; 482 } 483 484 /* We have no packets to actually measure against. This means 485 * either one of the other queues on this vector is active or 486 * we are a Tx queue doing TSO with too high of an interrupt rate. 487 * 488 * Between 4 and 56 we can assume that our current interrupt delay 489 * is only slightly too low. As such we should increase it by a small 490 * fixed amount. 491 */ 492 if (packets < 56) { 493 itr = rc->target_itr + IAVF_ITR_ADAPTIVE_MIN_INC; 494 if ((itr & IAVF_ITR_MASK) > IAVF_ITR_ADAPTIVE_MAX_USECS) { 495 itr &= IAVF_ITR_ADAPTIVE_LATENCY; 496 itr += IAVF_ITR_ADAPTIVE_MAX_USECS; 497 } 498 goto clear_counts; 499 } 500 501 if (packets <= 256) { 502 itr = min(q_vector->tx.current_itr, q_vector->rx.current_itr); 503 itr &= IAVF_ITR_MASK; 504 505 /* Between 56 and 112 is our "goldilocks" zone where we are 506 * working out "just right". Just report that our current 507 * ITR is good for us. 508 */ 509 if (packets <= 112) 510 goto clear_counts; 511 512 /* If packet count is 128 or greater we are likely looking 513 * at a slight overrun of the delay we want. Try halving 514 * our delay to see if that will cut the number of packets 515 * in half per interrupt. 516 */ 517 itr /= 2; 518 itr &= IAVF_ITR_MASK; 519 if (itr < IAVF_ITR_ADAPTIVE_MIN_USECS) 520 itr = IAVF_ITR_ADAPTIVE_MIN_USECS; 521 522 goto clear_counts; 523 } 524 525 /* The paths below assume we are dealing with a bulk ITR since 526 * number of packets is greater than 256. We are just going to have 527 * to compute a value and try to bring the count under control, 528 * though for smaller packet sizes there isn't much we can do as 529 * NAPI polling will likely be kicking in sooner rather than later. 530 */ 531 itr = IAVF_ITR_ADAPTIVE_BULK; 532 533 adjust_by_size: 534 /* If packet counts are 256 or greater we can assume we have a gross 535 * overestimation of what the rate should be. Instead of trying to fine 536 * tune it just use the formula below to try and dial in an exact value 537 * give the current packet size of the frame. 538 */ 539 avg_wire_size = bytes / packets; 540 541 /* The following is a crude approximation of: 542 * wmem_default / (size + overhead) = desired_pkts_per_int 543 * rate / bits_per_byte / (size + ethernet overhead) = pkt_rate 544 * (desired_pkt_rate / pkt_rate) * usecs_per_sec = ITR value 545 * 546 * Assuming wmem_default is 212992 and overhead is 640 bytes per 547 * packet, (256 skb, 64 headroom, 320 shared info), we can reduce the 548 * formula down to 549 * 550 * (170 * (size + 24)) / (size + 640) = ITR 551 * 552 * We first do some math on the packet size and then finally bitshift 553 * by 8 after rounding up. We also have to account for PCIe link speed 554 * difference as ITR scales based on this. 555 */ 556 if (avg_wire_size <= 60) { 557 /* Start at 250k ints/sec */ 558 avg_wire_size = 4096; 559 } else if (avg_wire_size <= 380) { 560 /* 250K ints/sec to 60K ints/sec */ 561 avg_wire_size *= 40; 562 avg_wire_size += 1696; 563 } else if (avg_wire_size <= 1084) { 564 /* 60K ints/sec to 36K ints/sec */ 565 avg_wire_size *= 15; 566 avg_wire_size += 11452; 567 } else if (avg_wire_size <= 1980) { 568 /* 36K ints/sec to 30K ints/sec */ 569 avg_wire_size *= 5; 570 avg_wire_size += 22420; 571 } else { 572 /* plateau at a limit of 30K ints/sec */ 573 avg_wire_size = 32256; 574 } 575 576 /* If we are in low latency mode halve our delay which doubles the 577 * rate to somewhere between 100K to 16K ints/sec 578 */ 579 if (itr & IAVF_ITR_ADAPTIVE_LATENCY) 580 avg_wire_size /= 2; 581 582 /* Resultant value is 256 times larger than it needs to be. This 583 * gives us room to adjust the value as needed to either increase 584 * or decrease the value based on link speeds of 10G, 2.5G, 1G, etc. 585 * 586 * Use addition as we have already recorded the new latency flag 587 * for the ITR value. 588 */ 589 itr += DIV_ROUND_UP(avg_wire_size, iavf_itr_divisor(q_vector)) * 590 IAVF_ITR_ADAPTIVE_MIN_INC; 591 592 if ((itr & IAVF_ITR_MASK) > IAVF_ITR_ADAPTIVE_MAX_USECS) { 593 itr &= IAVF_ITR_ADAPTIVE_LATENCY; 594 itr += IAVF_ITR_ADAPTIVE_MAX_USECS; 595 } 596 597 clear_counts: 598 /* write back value */ 599 rc->target_itr = itr; 600 601 /* next update should occur within next jiffy */ 602 rc->next_update = next_update + 1; 603 604 rc->total_bytes = 0; 605 rc->total_packets = 0; 606 } 607 608 /** 609 * iavf_setup_tx_descriptors - Allocate the Tx descriptors 610 * @tx_ring: the tx ring to set up 611 * 612 * Return 0 on success, negative on error 613 **/ 614 int iavf_setup_tx_descriptors(struct iavf_ring *tx_ring) 615 { 616 struct device *dev = tx_ring->dev; 617 int bi_size; 618 619 if (!dev) 620 return -ENOMEM; 621 622 /* warn if we are about to overwrite the pointer */ 623 WARN_ON(tx_ring->tx_bi); 624 bi_size = sizeof(struct iavf_tx_buffer) * tx_ring->count; 625 tx_ring->tx_bi = kzalloc(bi_size, GFP_KERNEL); 626 if (!tx_ring->tx_bi) 627 goto err; 628 629 /* round up to nearest 4K */ 630 tx_ring->size = tx_ring->count * sizeof(struct iavf_tx_desc); 631 tx_ring->size = ALIGN(tx_ring->size, 4096); 632 tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size, 633 &tx_ring->dma, GFP_KERNEL); 634 if (!tx_ring->desc) { 635 dev_info(dev, "Unable to allocate memory for the Tx descriptor ring, size=%d\n", 636 tx_ring->size); 637 goto err; 638 } 639 640 tx_ring->next_to_use = 0; 641 tx_ring->next_to_clean = 0; 642 tx_ring->tx_stats.prev_pkt_ctr = -1; 643 return 0; 644 645 err: 646 kfree(tx_ring->tx_bi); 647 tx_ring->tx_bi = NULL; 648 return -ENOMEM; 649 } 650 651 /** 652 * iavf_clean_rx_ring - Free Rx buffers 653 * @rx_ring: ring to be cleaned 654 **/ 655 void iavf_clean_rx_ring(struct iavf_ring *rx_ring) 656 { 657 unsigned long bi_size; 658 u16 i; 659 660 /* ring already cleared, nothing to do */ 661 if (!rx_ring->rx_bi) 662 return; 663 664 if (rx_ring->skb) { 665 dev_kfree_skb(rx_ring->skb); 666 rx_ring->skb = NULL; 667 } 668 669 /* Free all the Rx ring sk_buffs */ 670 for (i = 0; i < rx_ring->count; i++) { 671 struct iavf_rx_buffer *rx_bi = &rx_ring->rx_bi[i]; 672 673 if (!rx_bi->page) 674 continue; 675 676 /* Invalidate cache lines that may have been written to by 677 * device so that we avoid corrupting memory. 678 */ 679 dma_sync_single_range_for_cpu(rx_ring->dev, 680 rx_bi->dma, 681 rx_bi->page_offset, 682 rx_ring->rx_buf_len, 683 DMA_FROM_DEVICE); 684 685 /* free resources associated with mapping */ 686 dma_unmap_page_attrs(rx_ring->dev, rx_bi->dma, 687 iavf_rx_pg_size(rx_ring), 688 DMA_FROM_DEVICE, 689 IAVF_RX_DMA_ATTR); 690 691 __page_frag_cache_drain(rx_bi->page, rx_bi->pagecnt_bias); 692 693 rx_bi->page = NULL; 694 rx_bi->page_offset = 0; 695 } 696 697 bi_size = sizeof(struct iavf_rx_buffer) * rx_ring->count; 698 memset(rx_ring->rx_bi, 0, bi_size); 699 700 /* Zero out the descriptor ring */ 701 memset(rx_ring->desc, 0, rx_ring->size); 702 703 rx_ring->next_to_alloc = 0; 704 rx_ring->next_to_clean = 0; 705 rx_ring->next_to_use = 0; 706 } 707 708 /** 709 * iavf_free_rx_resources - Free Rx resources 710 * @rx_ring: ring to clean the resources from 711 * 712 * Free all receive software resources 713 **/ 714 void iavf_free_rx_resources(struct iavf_ring *rx_ring) 715 { 716 iavf_clean_rx_ring(rx_ring); 717 kfree(rx_ring->rx_bi); 718 rx_ring->rx_bi = NULL; 719 720 if (rx_ring->desc) { 721 dma_free_coherent(rx_ring->dev, rx_ring->size, 722 rx_ring->desc, rx_ring->dma); 723 rx_ring->desc = NULL; 724 } 725 } 726 727 /** 728 * iavf_setup_rx_descriptors - Allocate Rx descriptors 729 * @rx_ring: Rx descriptor ring (for a specific queue) to setup 730 * 731 * Returns 0 on success, negative on failure 732 **/ 733 int iavf_setup_rx_descriptors(struct iavf_ring *rx_ring) 734 { 735 struct device *dev = rx_ring->dev; 736 int bi_size; 737 738 /* warn if we are about to overwrite the pointer */ 739 WARN_ON(rx_ring->rx_bi); 740 bi_size = sizeof(struct iavf_rx_buffer) * rx_ring->count; 741 rx_ring->rx_bi = kzalloc(bi_size, GFP_KERNEL); 742 if (!rx_ring->rx_bi) 743 goto err; 744 745 u64_stats_init(&rx_ring->syncp); 746 747 /* Round up to nearest 4K */ 748 rx_ring->size = rx_ring->count * sizeof(union iavf_32byte_rx_desc); 749 rx_ring->size = ALIGN(rx_ring->size, 4096); 750 rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size, 751 &rx_ring->dma, GFP_KERNEL); 752 753 if (!rx_ring->desc) { 754 dev_info(dev, "Unable to allocate memory for the Rx descriptor ring, size=%d\n", 755 rx_ring->size); 756 goto err; 757 } 758 759 rx_ring->next_to_alloc = 0; 760 rx_ring->next_to_clean = 0; 761 rx_ring->next_to_use = 0; 762 763 return 0; 764 err: 765 kfree(rx_ring->rx_bi); 766 rx_ring->rx_bi = NULL; 767 return -ENOMEM; 768 } 769 770 /** 771 * iavf_release_rx_desc - Store the new tail and head values 772 * @rx_ring: ring to bump 773 * @val: new head index 774 **/ 775 static inline void iavf_release_rx_desc(struct iavf_ring *rx_ring, u32 val) 776 { 777 rx_ring->next_to_use = val; 778 779 /* update next to alloc since we have filled the ring */ 780 rx_ring->next_to_alloc = val; 781 782 /* Force memory writes to complete before letting h/w 783 * know there are new descriptors to fetch. (Only 784 * applicable for weak-ordered memory model archs, 785 * such as IA-64). 786 */ 787 wmb(); 788 writel(val, rx_ring->tail); 789 } 790 791 /** 792 * iavf_rx_offset - Return expected offset into page to access data 793 * @rx_ring: Ring we are requesting offset of 794 * 795 * Returns the offset value for ring into the data buffer. 796 */ 797 static inline unsigned int iavf_rx_offset(struct iavf_ring *rx_ring) 798 { 799 return ring_uses_build_skb(rx_ring) ? IAVF_SKB_PAD : 0; 800 } 801 802 /** 803 * iavf_alloc_mapped_page - recycle or make a new page 804 * @rx_ring: ring to use 805 * @bi: rx_buffer struct to modify 806 * 807 * Returns true if the page was successfully allocated or 808 * reused. 809 **/ 810 static bool iavf_alloc_mapped_page(struct iavf_ring *rx_ring, 811 struct iavf_rx_buffer *bi) 812 { 813 struct page *page = bi->page; 814 dma_addr_t dma; 815 816 /* since we are recycling buffers we should seldom need to alloc */ 817 if (likely(page)) { 818 rx_ring->rx_stats.page_reuse_count++; 819 return true; 820 } 821 822 /* alloc new page for storage */ 823 page = dev_alloc_pages(iavf_rx_pg_order(rx_ring)); 824 if (unlikely(!page)) { 825 rx_ring->rx_stats.alloc_page_failed++; 826 return false; 827 } 828 829 /* map page for use */ 830 dma = dma_map_page_attrs(rx_ring->dev, page, 0, 831 iavf_rx_pg_size(rx_ring), 832 DMA_FROM_DEVICE, 833 IAVF_RX_DMA_ATTR); 834 835 /* if mapping failed free memory back to system since 836 * there isn't much point in holding memory we can't use 837 */ 838 if (dma_mapping_error(rx_ring->dev, dma)) { 839 __free_pages(page, iavf_rx_pg_order(rx_ring)); 840 rx_ring->rx_stats.alloc_page_failed++; 841 return false; 842 } 843 844 bi->dma = dma; 845 bi->page = page; 846 bi->page_offset = iavf_rx_offset(rx_ring); 847 848 /* initialize pagecnt_bias to 1 representing we fully own page */ 849 bi->pagecnt_bias = 1; 850 851 return true; 852 } 853 854 /** 855 * iavf_receive_skb - Send a completed packet up the stack 856 * @rx_ring: rx ring in play 857 * @skb: packet to send up 858 * @vlan_tag: vlan tag for packet 859 **/ 860 static void iavf_receive_skb(struct iavf_ring *rx_ring, 861 struct sk_buff *skb, u16 vlan_tag) 862 { 863 struct iavf_q_vector *q_vector = rx_ring->q_vector; 864 865 if ((rx_ring->netdev->features & NETIF_F_HW_VLAN_CTAG_RX) && 866 (vlan_tag & VLAN_VID_MASK)) 867 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tag); 868 else if ((rx_ring->netdev->features & NETIF_F_HW_VLAN_STAG_RX) && 869 vlan_tag & VLAN_VID_MASK) 870 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021AD), vlan_tag); 871 872 napi_gro_receive(&q_vector->napi, skb); 873 } 874 875 /** 876 * iavf_alloc_rx_buffers - Replace used receive buffers 877 * @rx_ring: ring to place buffers on 878 * @cleaned_count: number of buffers to replace 879 * 880 * Returns false if all allocations were successful, true if any fail 881 **/ 882 bool iavf_alloc_rx_buffers(struct iavf_ring *rx_ring, u16 cleaned_count) 883 { 884 u16 ntu = rx_ring->next_to_use; 885 union iavf_rx_desc *rx_desc; 886 struct iavf_rx_buffer *bi; 887 888 /* do nothing if no valid netdev defined */ 889 if (!rx_ring->netdev || !cleaned_count) 890 return false; 891 892 rx_desc = IAVF_RX_DESC(rx_ring, ntu); 893 bi = &rx_ring->rx_bi[ntu]; 894 895 do { 896 if (!iavf_alloc_mapped_page(rx_ring, bi)) 897 goto no_buffers; 898 899 /* sync the buffer for use by the device */ 900 dma_sync_single_range_for_device(rx_ring->dev, bi->dma, 901 bi->page_offset, 902 rx_ring->rx_buf_len, 903 DMA_FROM_DEVICE); 904 905 /* Refresh the desc even if buffer_addrs didn't change 906 * because each write-back erases this info. 907 */ 908 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset); 909 910 rx_desc++; 911 bi++; 912 ntu++; 913 if (unlikely(ntu == rx_ring->count)) { 914 rx_desc = IAVF_RX_DESC(rx_ring, 0); 915 bi = rx_ring->rx_bi; 916 ntu = 0; 917 } 918 919 /* clear the status bits for the next_to_use descriptor */ 920 rx_desc->wb.qword1.status_error_len = 0; 921 922 cleaned_count--; 923 } while (cleaned_count); 924 925 if (rx_ring->next_to_use != ntu) 926 iavf_release_rx_desc(rx_ring, ntu); 927 928 return false; 929 930 no_buffers: 931 if (rx_ring->next_to_use != ntu) 932 iavf_release_rx_desc(rx_ring, ntu); 933 934 /* make sure to come back via polling to try again after 935 * allocation failure 936 */ 937 return true; 938 } 939 940 /** 941 * iavf_rx_checksum - Indicate in skb if hw indicated a good cksum 942 * @vsi: the VSI we care about 943 * @skb: skb currently being received and modified 944 * @rx_desc: the receive descriptor 945 **/ 946 static inline void iavf_rx_checksum(struct iavf_vsi *vsi, 947 struct sk_buff *skb, 948 union iavf_rx_desc *rx_desc) 949 { 950 struct iavf_rx_ptype_decoded decoded; 951 u32 rx_error, rx_status; 952 bool ipv4, ipv6; 953 u8 ptype; 954 u64 qword; 955 956 qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len); 957 ptype = (qword & IAVF_RXD_QW1_PTYPE_MASK) >> IAVF_RXD_QW1_PTYPE_SHIFT; 958 rx_error = (qword & IAVF_RXD_QW1_ERROR_MASK) >> 959 IAVF_RXD_QW1_ERROR_SHIFT; 960 rx_status = (qword & IAVF_RXD_QW1_STATUS_MASK) >> 961 IAVF_RXD_QW1_STATUS_SHIFT; 962 decoded = decode_rx_desc_ptype(ptype); 963 964 skb->ip_summed = CHECKSUM_NONE; 965 966 skb_checksum_none_assert(skb); 967 968 /* Rx csum enabled and ip headers found? */ 969 if (!(vsi->netdev->features & NETIF_F_RXCSUM)) 970 return; 971 972 /* did the hardware decode the packet and checksum? */ 973 if (!(rx_status & BIT(IAVF_RX_DESC_STATUS_L3L4P_SHIFT))) 974 return; 975 976 /* both known and outer_ip must be set for the below code to work */ 977 if (!(decoded.known && decoded.outer_ip)) 978 return; 979 980 ipv4 = (decoded.outer_ip == IAVF_RX_PTYPE_OUTER_IP) && 981 (decoded.outer_ip_ver == IAVF_RX_PTYPE_OUTER_IPV4); 982 ipv6 = (decoded.outer_ip == IAVF_RX_PTYPE_OUTER_IP) && 983 (decoded.outer_ip_ver == IAVF_RX_PTYPE_OUTER_IPV6); 984 985 if (ipv4 && 986 (rx_error & (BIT(IAVF_RX_DESC_ERROR_IPE_SHIFT) | 987 BIT(IAVF_RX_DESC_ERROR_EIPE_SHIFT)))) 988 goto checksum_fail; 989 990 /* likely incorrect csum if alternate IP extension headers found */ 991 if (ipv6 && 992 rx_status & BIT(IAVF_RX_DESC_STATUS_IPV6EXADD_SHIFT)) 993 /* don't increment checksum err here, non-fatal err */ 994 return; 995 996 /* there was some L4 error, count error and punt packet to the stack */ 997 if (rx_error & BIT(IAVF_RX_DESC_ERROR_L4E_SHIFT)) 998 goto checksum_fail; 999 1000 /* handle packets that were not able to be checksummed due 1001 * to arrival speed, in this case the stack can compute 1002 * the csum. 1003 */ 1004 if (rx_error & BIT(IAVF_RX_DESC_ERROR_PPRS_SHIFT)) 1005 return; 1006 1007 /* Only report checksum unnecessary for TCP, UDP, or SCTP */ 1008 switch (decoded.inner_prot) { 1009 case IAVF_RX_PTYPE_INNER_PROT_TCP: 1010 case IAVF_RX_PTYPE_INNER_PROT_UDP: 1011 case IAVF_RX_PTYPE_INNER_PROT_SCTP: 1012 skb->ip_summed = CHECKSUM_UNNECESSARY; 1013 fallthrough; 1014 default: 1015 break; 1016 } 1017 1018 return; 1019 1020 checksum_fail: 1021 vsi->back->hw_csum_rx_error++; 1022 } 1023 1024 /** 1025 * iavf_ptype_to_htype - get a hash type 1026 * @ptype: the ptype value from the descriptor 1027 * 1028 * Returns a hash type to be used by skb_set_hash 1029 **/ 1030 static inline int iavf_ptype_to_htype(u8 ptype) 1031 { 1032 struct iavf_rx_ptype_decoded decoded = decode_rx_desc_ptype(ptype); 1033 1034 if (!decoded.known) 1035 return PKT_HASH_TYPE_NONE; 1036 1037 if (decoded.outer_ip == IAVF_RX_PTYPE_OUTER_IP && 1038 decoded.payload_layer == IAVF_RX_PTYPE_PAYLOAD_LAYER_PAY4) 1039 return PKT_HASH_TYPE_L4; 1040 else if (decoded.outer_ip == IAVF_RX_PTYPE_OUTER_IP && 1041 decoded.payload_layer == IAVF_RX_PTYPE_PAYLOAD_LAYER_PAY3) 1042 return PKT_HASH_TYPE_L3; 1043 else 1044 return PKT_HASH_TYPE_L2; 1045 } 1046 1047 /** 1048 * iavf_rx_hash - set the hash value in the skb 1049 * @ring: descriptor ring 1050 * @rx_desc: specific descriptor 1051 * @skb: skb currently being received and modified 1052 * @rx_ptype: Rx packet type 1053 **/ 1054 static inline void iavf_rx_hash(struct iavf_ring *ring, 1055 union iavf_rx_desc *rx_desc, 1056 struct sk_buff *skb, 1057 u8 rx_ptype) 1058 { 1059 u32 hash; 1060 const __le64 rss_mask = 1061 cpu_to_le64((u64)IAVF_RX_DESC_FLTSTAT_RSS_HASH << 1062 IAVF_RX_DESC_STATUS_FLTSTAT_SHIFT); 1063 1064 if (ring->netdev->features & NETIF_F_RXHASH) 1065 return; 1066 1067 if ((rx_desc->wb.qword1.status_error_len & rss_mask) == rss_mask) { 1068 hash = le32_to_cpu(rx_desc->wb.qword0.hi_dword.rss); 1069 skb_set_hash(skb, hash, iavf_ptype_to_htype(rx_ptype)); 1070 } 1071 } 1072 1073 /** 1074 * iavf_process_skb_fields - Populate skb header fields from Rx descriptor 1075 * @rx_ring: rx descriptor ring packet is being transacted on 1076 * @rx_desc: pointer to the EOP Rx descriptor 1077 * @skb: pointer to current skb being populated 1078 * @rx_ptype: the packet type decoded by hardware 1079 * 1080 * This function checks the ring, descriptor, and packet information in 1081 * order to populate the hash, checksum, VLAN, protocol, and 1082 * other fields within the skb. 1083 **/ 1084 static inline 1085 void iavf_process_skb_fields(struct iavf_ring *rx_ring, 1086 union iavf_rx_desc *rx_desc, struct sk_buff *skb, 1087 u8 rx_ptype) 1088 { 1089 iavf_rx_hash(rx_ring, rx_desc, skb, rx_ptype); 1090 1091 iavf_rx_checksum(rx_ring->vsi, skb, rx_desc); 1092 1093 skb_record_rx_queue(skb, rx_ring->queue_index); 1094 1095 /* modifies the skb - consumes the enet header */ 1096 skb->protocol = eth_type_trans(skb, rx_ring->netdev); 1097 } 1098 1099 /** 1100 * iavf_cleanup_headers - Correct empty headers 1101 * @rx_ring: rx descriptor ring packet is being transacted on 1102 * @skb: pointer to current skb being fixed 1103 * 1104 * Also address the case where we are pulling data in on pages only 1105 * and as such no data is present in the skb header. 1106 * 1107 * In addition if skb is not at least 60 bytes we need to pad it so that 1108 * it is large enough to qualify as a valid Ethernet frame. 1109 * 1110 * Returns true if an error was encountered and skb was freed. 1111 **/ 1112 static bool iavf_cleanup_headers(struct iavf_ring *rx_ring, struct sk_buff *skb) 1113 { 1114 /* if eth_skb_pad returns an error the skb was freed */ 1115 if (eth_skb_pad(skb)) 1116 return true; 1117 1118 return false; 1119 } 1120 1121 /** 1122 * iavf_reuse_rx_page - page flip buffer and store it back on the ring 1123 * @rx_ring: rx descriptor ring to store buffers on 1124 * @old_buff: donor buffer to have page reused 1125 * 1126 * Synchronizes page for reuse by the adapter 1127 **/ 1128 static void iavf_reuse_rx_page(struct iavf_ring *rx_ring, 1129 struct iavf_rx_buffer *old_buff) 1130 { 1131 struct iavf_rx_buffer *new_buff; 1132 u16 nta = rx_ring->next_to_alloc; 1133 1134 new_buff = &rx_ring->rx_bi[nta]; 1135 1136 /* update, and store next to alloc */ 1137 nta++; 1138 rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0; 1139 1140 /* transfer page from old buffer to new buffer */ 1141 new_buff->dma = old_buff->dma; 1142 new_buff->page = old_buff->page; 1143 new_buff->page_offset = old_buff->page_offset; 1144 new_buff->pagecnt_bias = old_buff->pagecnt_bias; 1145 } 1146 1147 /** 1148 * iavf_can_reuse_rx_page - Determine if this page can be reused by 1149 * the adapter for another receive 1150 * 1151 * @rx_buffer: buffer containing the page 1152 * 1153 * If page is reusable, rx_buffer->page_offset is adjusted to point to 1154 * an unused region in the page. 1155 * 1156 * For small pages, @truesize will be a constant value, half the size 1157 * of the memory at page. We'll attempt to alternate between high and 1158 * low halves of the page, with one half ready for use by the hardware 1159 * and the other half being consumed by the stack. We use the page 1160 * ref count to determine whether the stack has finished consuming the 1161 * portion of this page that was passed up with a previous packet. If 1162 * the page ref count is >1, we'll assume the "other" half page is 1163 * still busy, and this page cannot be reused. 1164 * 1165 * For larger pages, @truesize will be the actual space used by the 1166 * received packet (adjusted upward to an even multiple of the cache 1167 * line size). This will advance through the page by the amount 1168 * actually consumed by the received packets while there is still 1169 * space for a buffer. Each region of larger pages will be used at 1170 * most once, after which the page will not be reused. 1171 * 1172 * In either case, if the page is reusable its refcount is increased. 1173 **/ 1174 static bool iavf_can_reuse_rx_page(struct iavf_rx_buffer *rx_buffer) 1175 { 1176 unsigned int pagecnt_bias = rx_buffer->pagecnt_bias; 1177 struct page *page = rx_buffer->page; 1178 1179 /* Is any reuse possible? */ 1180 if (!dev_page_is_reusable(page)) 1181 return false; 1182 1183 #if (PAGE_SIZE < 8192) 1184 /* if we are only owner of page we can reuse it */ 1185 if (unlikely((page_count(page) - pagecnt_bias) > 1)) 1186 return false; 1187 #else 1188 #define IAVF_LAST_OFFSET \ 1189 (SKB_WITH_OVERHEAD(PAGE_SIZE) - IAVF_RXBUFFER_2048) 1190 if (rx_buffer->page_offset > IAVF_LAST_OFFSET) 1191 return false; 1192 #endif 1193 1194 /* If we have drained the page fragment pool we need to update 1195 * the pagecnt_bias and page count so that we fully restock the 1196 * number of references the driver holds. 1197 */ 1198 if (unlikely(!pagecnt_bias)) { 1199 page_ref_add(page, USHRT_MAX); 1200 rx_buffer->pagecnt_bias = USHRT_MAX; 1201 } 1202 1203 return true; 1204 } 1205 1206 /** 1207 * iavf_add_rx_frag - Add contents of Rx buffer to sk_buff 1208 * @rx_ring: rx descriptor ring to transact packets on 1209 * @rx_buffer: buffer containing page to add 1210 * @skb: sk_buff to place the data into 1211 * @size: packet length from rx_desc 1212 * 1213 * This function will add the data contained in rx_buffer->page to the skb. 1214 * It will just attach the page as a frag to the skb. 1215 * 1216 * The function will then update the page offset. 1217 **/ 1218 static void iavf_add_rx_frag(struct iavf_ring *rx_ring, 1219 struct iavf_rx_buffer *rx_buffer, 1220 struct sk_buff *skb, 1221 unsigned int size) 1222 { 1223 #if (PAGE_SIZE < 8192) 1224 unsigned int truesize = iavf_rx_pg_size(rx_ring) / 2; 1225 #else 1226 unsigned int truesize = SKB_DATA_ALIGN(size + iavf_rx_offset(rx_ring)); 1227 #endif 1228 1229 if (!size) 1230 return; 1231 1232 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_buffer->page, 1233 rx_buffer->page_offset, size, truesize); 1234 1235 /* page is being used so we must update the page offset */ 1236 #if (PAGE_SIZE < 8192) 1237 rx_buffer->page_offset ^= truesize; 1238 #else 1239 rx_buffer->page_offset += truesize; 1240 #endif 1241 } 1242 1243 /** 1244 * iavf_get_rx_buffer - Fetch Rx buffer and synchronize data for use 1245 * @rx_ring: rx descriptor ring to transact packets on 1246 * @size: size of buffer to add to skb 1247 * 1248 * This function will pull an Rx buffer from the ring and synchronize it 1249 * for use by the CPU. 1250 */ 1251 static struct iavf_rx_buffer *iavf_get_rx_buffer(struct iavf_ring *rx_ring, 1252 const unsigned int size) 1253 { 1254 struct iavf_rx_buffer *rx_buffer; 1255 1256 if (!size) 1257 return NULL; 1258 1259 rx_buffer = &rx_ring->rx_bi[rx_ring->next_to_clean]; 1260 prefetchw(rx_buffer->page); 1261 1262 /* we are reusing so sync this buffer for CPU use */ 1263 dma_sync_single_range_for_cpu(rx_ring->dev, 1264 rx_buffer->dma, 1265 rx_buffer->page_offset, 1266 size, 1267 DMA_FROM_DEVICE); 1268 1269 /* We have pulled a buffer for use, so decrement pagecnt_bias */ 1270 rx_buffer->pagecnt_bias--; 1271 1272 return rx_buffer; 1273 } 1274 1275 /** 1276 * iavf_construct_skb - Allocate skb and populate it 1277 * @rx_ring: rx descriptor ring to transact packets on 1278 * @rx_buffer: rx buffer to pull data from 1279 * @size: size of buffer to add to skb 1280 * 1281 * This function allocates an skb. It then populates it with the page 1282 * data from the current receive descriptor, taking care to set up the 1283 * skb correctly. 1284 */ 1285 static struct sk_buff *iavf_construct_skb(struct iavf_ring *rx_ring, 1286 struct iavf_rx_buffer *rx_buffer, 1287 unsigned int size) 1288 { 1289 void *va; 1290 #if (PAGE_SIZE < 8192) 1291 unsigned int truesize = iavf_rx_pg_size(rx_ring) / 2; 1292 #else 1293 unsigned int truesize = SKB_DATA_ALIGN(size); 1294 #endif 1295 unsigned int headlen; 1296 struct sk_buff *skb; 1297 1298 if (!rx_buffer) 1299 return NULL; 1300 /* prefetch first cache line of first page */ 1301 va = page_address(rx_buffer->page) + rx_buffer->page_offset; 1302 net_prefetch(va); 1303 1304 /* allocate a skb to store the frags */ 1305 skb = __napi_alloc_skb(&rx_ring->q_vector->napi, 1306 IAVF_RX_HDR_SIZE, 1307 GFP_ATOMIC | __GFP_NOWARN); 1308 if (unlikely(!skb)) 1309 return NULL; 1310 1311 /* Determine available headroom for copy */ 1312 headlen = size; 1313 if (headlen > IAVF_RX_HDR_SIZE) 1314 headlen = eth_get_headlen(skb->dev, va, IAVF_RX_HDR_SIZE); 1315 1316 /* align pull length to size of long to optimize memcpy performance */ 1317 memcpy(__skb_put(skb, headlen), va, ALIGN(headlen, sizeof(long))); 1318 1319 /* update all of the pointers */ 1320 size -= headlen; 1321 if (size) { 1322 skb_add_rx_frag(skb, 0, rx_buffer->page, 1323 rx_buffer->page_offset + headlen, 1324 size, truesize); 1325 1326 /* buffer is used by skb, update page_offset */ 1327 #if (PAGE_SIZE < 8192) 1328 rx_buffer->page_offset ^= truesize; 1329 #else 1330 rx_buffer->page_offset += truesize; 1331 #endif 1332 } else { 1333 /* buffer is unused, reset bias back to rx_buffer */ 1334 rx_buffer->pagecnt_bias++; 1335 } 1336 1337 return skb; 1338 } 1339 1340 /** 1341 * iavf_build_skb - Build skb around an existing buffer 1342 * @rx_ring: Rx descriptor ring to transact packets on 1343 * @rx_buffer: Rx buffer to pull data from 1344 * @size: size of buffer to add to skb 1345 * 1346 * This function builds an skb around an existing Rx buffer, taking care 1347 * to set up the skb correctly and avoid any memcpy overhead. 1348 */ 1349 static struct sk_buff *iavf_build_skb(struct iavf_ring *rx_ring, 1350 struct iavf_rx_buffer *rx_buffer, 1351 unsigned int size) 1352 { 1353 void *va; 1354 #if (PAGE_SIZE < 8192) 1355 unsigned int truesize = iavf_rx_pg_size(rx_ring) / 2; 1356 #else 1357 unsigned int truesize = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) + 1358 SKB_DATA_ALIGN(IAVF_SKB_PAD + size); 1359 #endif 1360 struct sk_buff *skb; 1361 1362 if (!rx_buffer) 1363 return NULL; 1364 /* prefetch first cache line of first page */ 1365 va = page_address(rx_buffer->page) + rx_buffer->page_offset; 1366 net_prefetch(va); 1367 1368 /* build an skb around the page buffer */ 1369 skb = napi_build_skb(va - IAVF_SKB_PAD, truesize); 1370 if (unlikely(!skb)) 1371 return NULL; 1372 1373 /* update pointers within the skb to store the data */ 1374 skb_reserve(skb, IAVF_SKB_PAD); 1375 __skb_put(skb, size); 1376 1377 /* buffer is used by skb, update page_offset */ 1378 #if (PAGE_SIZE < 8192) 1379 rx_buffer->page_offset ^= truesize; 1380 #else 1381 rx_buffer->page_offset += truesize; 1382 #endif 1383 1384 return skb; 1385 } 1386 1387 /** 1388 * iavf_put_rx_buffer - Clean up used buffer and either recycle or free 1389 * @rx_ring: rx descriptor ring to transact packets on 1390 * @rx_buffer: rx buffer to pull data from 1391 * 1392 * This function will clean up the contents of the rx_buffer. It will 1393 * either recycle the buffer or unmap it and free the associated resources. 1394 */ 1395 static void iavf_put_rx_buffer(struct iavf_ring *rx_ring, 1396 struct iavf_rx_buffer *rx_buffer) 1397 { 1398 if (!rx_buffer) 1399 return; 1400 1401 if (iavf_can_reuse_rx_page(rx_buffer)) { 1402 /* hand second half of page back to the ring */ 1403 iavf_reuse_rx_page(rx_ring, rx_buffer); 1404 rx_ring->rx_stats.page_reuse_count++; 1405 } else { 1406 /* we are not reusing the buffer so unmap it */ 1407 dma_unmap_page_attrs(rx_ring->dev, rx_buffer->dma, 1408 iavf_rx_pg_size(rx_ring), 1409 DMA_FROM_DEVICE, IAVF_RX_DMA_ATTR); 1410 __page_frag_cache_drain(rx_buffer->page, 1411 rx_buffer->pagecnt_bias); 1412 } 1413 1414 /* clear contents of buffer_info */ 1415 rx_buffer->page = NULL; 1416 } 1417 1418 /** 1419 * iavf_is_non_eop - process handling of non-EOP buffers 1420 * @rx_ring: Rx ring being processed 1421 * @rx_desc: Rx descriptor for current buffer 1422 * @skb: Current socket buffer containing buffer in progress 1423 * 1424 * This function updates next to clean. If the buffer is an EOP buffer 1425 * this function exits returning false, otherwise it will place the 1426 * sk_buff in the next buffer to be chained and return true indicating 1427 * that this is in fact a non-EOP buffer. 1428 **/ 1429 static bool iavf_is_non_eop(struct iavf_ring *rx_ring, 1430 union iavf_rx_desc *rx_desc, 1431 struct sk_buff *skb) 1432 { 1433 u32 ntc = rx_ring->next_to_clean + 1; 1434 1435 /* fetch, update, and store next to clean */ 1436 ntc = (ntc < rx_ring->count) ? ntc : 0; 1437 rx_ring->next_to_clean = ntc; 1438 1439 prefetch(IAVF_RX_DESC(rx_ring, ntc)); 1440 1441 /* if we are the last buffer then there is nothing else to do */ 1442 #define IAVF_RXD_EOF BIT(IAVF_RX_DESC_STATUS_EOF_SHIFT) 1443 if (likely(iavf_test_staterr(rx_desc, IAVF_RXD_EOF))) 1444 return false; 1445 1446 rx_ring->rx_stats.non_eop_descs++; 1447 1448 return true; 1449 } 1450 1451 /** 1452 * iavf_clean_rx_irq - Clean completed descriptors from Rx ring - bounce buf 1453 * @rx_ring: rx descriptor ring to transact packets on 1454 * @budget: Total limit on number of packets to process 1455 * 1456 * This function provides a "bounce buffer" approach to Rx interrupt 1457 * processing. The advantage to this is that on systems that have 1458 * expensive overhead for IOMMU access this provides a means of avoiding 1459 * it by maintaining the mapping of the page to the system. 1460 * 1461 * Returns amount of work completed 1462 **/ 1463 static int iavf_clean_rx_irq(struct iavf_ring *rx_ring, int budget) 1464 { 1465 unsigned int total_rx_bytes = 0, total_rx_packets = 0; 1466 struct sk_buff *skb = rx_ring->skb; 1467 u16 cleaned_count = IAVF_DESC_UNUSED(rx_ring); 1468 bool failure = false; 1469 1470 while (likely(total_rx_packets < (unsigned int)budget)) { 1471 struct iavf_rx_buffer *rx_buffer; 1472 union iavf_rx_desc *rx_desc; 1473 unsigned int size; 1474 u16 vlan_tag = 0; 1475 u8 rx_ptype; 1476 u64 qword; 1477 1478 /* return some buffers to hardware, one at a time is too slow */ 1479 if (cleaned_count >= IAVF_RX_BUFFER_WRITE) { 1480 failure = failure || 1481 iavf_alloc_rx_buffers(rx_ring, cleaned_count); 1482 cleaned_count = 0; 1483 } 1484 1485 rx_desc = IAVF_RX_DESC(rx_ring, rx_ring->next_to_clean); 1486 1487 /* status_error_len will always be zero for unused descriptors 1488 * because it's cleared in cleanup, and overlaps with hdr_addr 1489 * which is always zero because packet split isn't used, if the 1490 * hardware wrote DD then the length will be non-zero 1491 */ 1492 qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len); 1493 1494 /* This memory barrier is needed to keep us from reading 1495 * any other fields out of the rx_desc until we have 1496 * verified the descriptor has been written back. 1497 */ 1498 dma_rmb(); 1499 #define IAVF_RXD_DD BIT(IAVF_RX_DESC_STATUS_DD_SHIFT) 1500 if (!iavf_test_staterr(rx_desc, IAVF_RXD_DD)) 1501 break; 1502 1503 size = (qword & IAVF_RXD_QW1_LENGTH_PBUF_MASK) >> 1504 IAVF_RXD_QW1_LENGTH_PBUF_SHIFT; 1505 1506 iavf_trace(clean_rx_irq, rx_ring, rx_desc, skb); 1507 rx_buffer = iavf_get_rx_buffer(rx_ring, size); 1508 1509 /* retrieve a buffer from the ring */ 1510 if (skb) 1511 iavf_add_rx_frag(rx_ring, rx_buffer, skb, size); 1512 else if (ring_uses_build_skb(rx_ring)) 1513 skb = iavf_build_skb(rx_ring, rx_buffer, size); 1514 else 1515 skb = iavf_construct_skb(rx_ring, rx_buffer, size); 1516 1517 /* exit if we failed to retrieve a buffer */ 1518 if (!skb) { 1519 rx_ring->rx_stats.alloc_buff_failed++; 1520 if (rx_buffer) 1521 rx_buffer->pagecnt_bias++; 1522 break; 1523 } 1524 1525 iavf_put_rx_buffer(rx_ring, rx_buffer); 1526 cleaned_count++; 1527 1528 if (iavf_is_non_eop(rx_ring, rx_desc, skb)) 1529 continue; 1530 1531 /* ERR_MASK will only have valid bits if EOP set, and 1532 * what we are doing here is actually checking 1533 * IAVF_RX_DESC_ERROR_RXE_SHIFT, since it is the zeroth bit in 1534 * the error field 1535 */ 1536 if (unlikely(iavf_test_staterr(rx_desc, BIT(IAVF_RXD_QW1_ERROR_SHIFT)))) { 1537 dev_kfree_skb_any(skb); 1538 skb = NULL; 1539 continue; 1540 } 1541 1542 if (iavf_cleanup_headers(rx_ring, skb)) { 1543 skb = NULL; 1544 continue; 1545 } 1546 1547 /* probably a little skewed due to removing CRC */ 1548 total_rx_bytes += skb->len; 1549 1550 qword = le64_to_cpu(rx_desc->wb.qword1.status_error_len); 1551 rx_ptype = (qword & IAVF_RXD_QW1_PTYPE_MASK) >> 1552 IAVF_RXD_QW1_PTYPE_SHIFT; 1553 1554 /* populate checksum, VLAN, and protocol */ 1555 iavf_process_skb_fields(rx_ring, rx_desc, skb, rx_ptype); 1556 1557 if (qword & BIT(IAVF_RX_DESC_STATUS_L2TAG1P_SHIFT) && 1558 rx_ring->flags & IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1) 1559 vlan_tag = le16_to_cpu(rx_desc->wb.qword0.lo_dword.l2tag1); 1560 if (rx_desc->wb.qword2.ext_status & 1561 cpu_to_le16(BIT(IAVF_RX_DESC_EXT_STATUS_L2TAG2P_SHIFT)) && 1562 rx_ring->flags & IAVF_RXR_FLAGS_VLAN_TAG_LOC_L2TAG2_2) 1563 vlan_tag = le16_to_cpu(rx_desc->wb.qword2.l2tag2_2); 1564 1565 iavf_trace(clean_rx_irq_rx, rx_ring, rx_desc, skb); 1566 iavf_receive_skb(rx_ring, skb, vlan_tag); 1567 skb = NULL; 1568 1569 /* update budget accounting */ 1570 total_rx_packets++; 1571 } 1572 1573 rx_ring->skb = skb; 1574 1575 u64_stats_update_begin(&rx_ring->syncp); 1576 rx_ring->stats.packets += total_rx_packets; 1577 rx_ring->stats.bytes += total_rx_bytes; 1578 u64_stats_update_end(&rx_ring->syncp); 1579 rx_ring->q_vector->rx.total_packets += total_rx_packets; 1580 rx_ring->q_vector->rx.total_bytes += total_rx_bytes; 1581 1582 /* guarantee a trip back through this routine if there was a failure */ 1583 return failure ? budget : (int)total_rx_packets; 1584 } 1585 1586 static inline u32 iavf_buildreg_itr(const int type, u16 itr) 1587 { 1588 u32 val; 1589 1590 /* We don't bother with setting the CLEARPBA bit as the data sheet 1591 * points out doing so is "meaningless since it was already 1592 * auto-cleared". The auto-clearing happens when the interrupt is 1593 * asserted. 1594 * 1595 * Hardware errata 28 for also indicates that writing to a 1596 * xxINT_DYN_CTLx CSR with INTENA_MSK (bit 31) set to 0 will clear 1597 * an event in the PBA anyway so we need to rely on the automask 1598 * to hold pending events for us until the interrupt is re-enabled 1599 * 1600 * The itr value is reported in microseconds, and the register 1601 * value is recorded in 2 microsecond units. For this reason we 1602 * only need to shift by the interval shift - 1 instead of the 1603 * full value. 1604 */ 1605 itr &= IAVF_ITR_MASK; 1606 1607 val = IAVF_VFINT_DYN_CTLN1_INTENA_MASK | 1608 (type << IAVF_VFINT_DYN_CTLN1_ITR_INDX_SHIFT) | 1609 (itr << (IAVF_VFINT_DYN_CTLN1_INTERVAL_SHIFT - 1)); 1610 1611 return val; 1612 } 1613 1614 /* a small macro to shorten up some long lines */ 1615 #define INTREG IAVF_VFINT_DYN_CTLN1 1616 1617 /* The act of updating the ITR will cause it to immediately trigger. In order 1618 * to prevent this from throwing off adaptive update statistics we defer the 1619 * update so that it can only happen so often. So after either Tx or Rx are 1620 * updated we make the adaptive scheme wait until either the ITR completely 1621 * expires via the next_update expiration or we have been through at least 1622 * 3 interrupts. 1623 */ 1624 #define ITR_COUNTDOWN_START 3 1625 1626 /** 1627 * iavf_update_enable_itr - Update itr and re-enable MSIX interrupt 1628 * @vsi: the VSI we care about 1629 * @q_vector: q_vector for which itr is being updated and interrupt enabled 1630 * 1631 **/ 1632 static inline void iavf_update_enable_itr(struct iavf_vsi *vsi, 1633 struct iavf_q_vector *q_vector) 1634 { 1635 struct iavf_hw *hw = &vsi->back->hw; 1636 u32 intval; 1637 1638 /* These will do nothing if dynamic updates are not enabled */ 1639 iavf_update_itr(q_vector, &q_vector->tx); 1640 iavf_update_itr(q_vector, &q_vector->rx); 1641 1642 /* This block of logic allows us to get away with only updating 1643 * one ITR value with each interrupt. The idea is to perform a 1644 * pseudo-lazy update with the following criteria. 1645 * 1646 * 1. Rx is given higher priority than Tx if both are in same state 1647 * 2. If we must reduce an ITR that is given highest priority. 1648 * 3. We then give priority to increasing ITR based on amount. 1649 */ 1650 if (q_vector->rx.target_itr < q_vector->rx.current_itr) { 1651 /* Rx ITR needs to be reduced, this is highest priority */ 1652 intval = iavf_buildreg_itr(IAVF_RX_ITR, 1653 q_vector->rx.target_itr); 1654 q_vector->rx.current_itr = q_vector->rx.target_itr; 1655 q_vector->itr_countdown = ITR_COUNTDOWN_START; 1656 } else if ((q_vector->tx.target_itr < q_vector->tx.current_itr) || 1657 ((q_vector->rx.target_itr - q_vector->rx.current_itr) < 1658 (q_vector->tx.target_itr - q_vector->tx.current_itr))) { 1659 /* Tx ITR needs to be reduced, this is second priority 1660 * Tx ITR needs to be increased more than Rx, fourth priority 1661 */ 1662 intval = iavf_buildreg_itr(IAVF_TX_ITR, 1663 q_vector->tx.target_itr); 1664 q_vector->tx.current_itr = q_vector->tx.target_itr; 1665 q_vector->itr_countdown = ITR_COUNTDOWN_START; 1666 } else if (q_vector->rx.current_itr != q_vector->rx.target_itr) { 1667 /* Rx ITR needs to be increased, third priority */ 1668 intval = iavf_buildreg_itr(IAVF_RX_ITR, 1669 q_vector->rx.target_itr); 1670 q_vector->rx.current_itr = q_vector->rx.target_itr; 1671 q_vector->itr_countdown = ITR_COUNTDOWN_START; 1672 } else { 1673 /* No ITR update, lowest priority */ 1674 intval = iavf_buildreg_itr(IAVF_ITR_NONE, 0); 1675 if (q_vector->itr_countdown) 1676 q_vector->itr_countdown--; 1677 } 1678 1679 if (!test_bit(__IAVF_VSI_DOWN, vsi->state)) 1680 wr32(hw, INTREG(q_vector->reg_idx), intval); 1681 } 1682 1683 /** 1684 * iavf_napi_poll - NAPI polling Rx/Tx cleanup routine 1685 * @napi: napi struct with our devices info in it 1686 * @budget: amount of work driver is allowed to do this pass, in packets 1687 * 1688 * This function will clean all queues associated with a q_vector. 1689 * 1690 * Returns the amount of work done 1691 **/ 1692 int iavf_napi_poll(struct napi_struct *napi, int budget) 1693 { 1694 struct iavf_q_vector *q_vector = 1695 container_of(napi, struct iavf_q_vector, napi); 1696 struct iavf_vsi *vsi = q_vector->vsi; 1697 struct iavf_ring *ring; 1698 bool clean_complete = true; 1699 bool arm_wb = false; 1700 int budget_per_ring; 1701 int work_done = 0; 1702 1703 if (test_bit(__IAVF_VSI_DOWN, vsi->state)) { 1704 napi_complete(napi); 1705 return 0; 1706 } 1707 1708 /* Since the actual Tx work is minimal, we can give the Tx a larger 1709 * budget and be more aggressive about cleaning up the Tx descriptors. 1710 */ 1711 iavf_for_each_ring(ring, q_vector->tx) { 1712 if (!iavf_clean_tx_irq(vsi, ring, budget)) { 1713 clean_complete = false; 1714 continue; 1715 } 1716 arm_wb |= ring->arm_wb; 1717 ring->arm_wb = false; 1718 } 1719 1720 /* Handle case where we are called by netpoll with a budget of 0 */ 1721 if (budget <= 0) 1722 goto tx_only; 1723 1724 /* We attempt to distribute budget to each Rx queue fairly, but don't 1725 * allow the budget to go below 1 because that would exit polling early. 1726 */ 1727 budget_per_ring = max(budget/q_vector->num_ringpairs, 1); 1728 1729 iavf_for_each_ring(ring, q_vector->rx) { 1730 int cleaned = iavf_clean_rx_irq(ring, budget_per_ring); 1731 1732 work_done += cleaned; 1733 /* if we clean as many as budgeted, we must not be done */ 1734 if (cleaned >= budget_per_ring) 1735 clean_complete = false; 1736 } 1737 1738 /* If work not completed, return budget and polling will return */ 1739 if (!clean_complete) { 1740 int cpu_id = smp_processor_id(); 1741 1742 /* It is possible that the interrupt affinity has changed but, 1743 * if the cpu is pegged at 100%, polling will never exit while 1744 * traffic continues and the interrupt will be stuck on this 1745 * cpu. We check to make sure affinity is correct before we 1746 * continue to poll, otherwise we must stop polling so the 1747 * interrupt can move to the correct cpu. 1748 */ 1749 if (!cpumask_test_cpu(cpu_id, &q_vector->affinity_mask)) { 1750 /* Tell napi that we are done polling */ 1751 napi_complete_done(napi, work_done); 1752 1753 /* Force an interrupt */ 1754 iavf_force_wb(vsi, q_vector); 1755 1756 /* Return budget-1 so that polling stops */ 1757 return budget - 1; 1758 } 1759 tx_only: 1760 if (arm_wb) { 1761 q_vector->tx.ring[0].tx_stats.tx_force_wb++; 1762 iavf_enable_wb_on_itr(vsi, q_vector); 1763 } 1764 return budget; 1765 } 1766 1767 if (vsi->back->flags & IAVF_TXR_FLAGS_WB_ON_ITR) 1768 q_vector->arm_wb_state = false; 1769 1770 /* Exit the polling mode, but don't re-enable interrupts if stack might 1771 * poll us due to busy-polling 1772 */ 1773 if (likely(napi_complete_done(napi, work_done))) 1774 iavf_update_enable_itr(vsi, q_vector); 1775 1776 return min_t(int, work_done, budget - 1); 1777 } 1778 1779 /** 1780 * iavf_tx_prepare_vlan_flags - prepare generic TX VLAN tagging flags for HW 1781 * @skb: send buffer 1782 * @tx_ring: ring to send buffer on 1783 * @flags: the tx flags to be set 1784 * 1785 * Checks the skb and set up correspondingly several generic transmit flags 1786 * related to VLAN tagging for the HW, such as VLAN, DCB, etc. 1787 * 1788 * Returns error code indicate the frame should be dropped upon error and the 1789 * otherwise returns 0 to indicate the flags has been set properly. 1790 **/ 1791 static void iavf_tx_prepare_vlan_flags(struct sk_buff *skb, 1792 struct iavf_ring *tx_ring, u32 *flags) 1793 { 1794 u32 tx_flags = 0; 1795 1796 1797 /* stack will only request hardware VLAN insertion offload for protocols 1798 * that the driver supports and has enabled 1799 */ 1800 if (!skb_vlan_tag_present(skb)) 1801 return; 1802 1803 tx_flags |= skb_vlan_tag_get(skb) << IAVF_TX_FLAGS_VLAN_SHIFT; 1804 if (tx_ring->flags & IAVF_TXR_FLAGS_VLAN_TAG_LOC_L2TAG2) { 1805 tx_flags |= IAVF_TX_FLAGS_HW_OUTER_SINGLE_VLAN; 1806 } else if (tx_ring->flags & IAVF_TXRX_FLAGS_VLAN_TAG_LOC_L2TAG1) { 1807 tx_flags |= IAVF_TX_FLAGS_HW_VLAN; 1808 } else { 1809 dev_dbg(tx_ring->dev, "Unsupported Tx VLAN tag location requested\n"); 1810 return; 1811 } 1812 1813 *flags = tx_flags; 1814 } 1815 1816 /** 1817 * iavf_tso - set up the tso context descriptor 1818 * @first: pointer to first Tx buffer for xmit 1819 * @hdr_len: ptr to the size of the packet header 1820 * @cd_type_cmd_tso_mss: Quad Word 1 1821 * 1822 * Returns 0 if no TSO can happen, 1 if tso is going, or error 1823 **/ 1824 static int iavf_tso(struct iavf_tx_buffer *first, u8 *hdr_len, 1825 u64 *cd_type_cmd_tso_mss) 1826 { 1827 struct sk_buff *skb = first->skb; 1828 u64 cd_cmd, cd_tso_len, cd_mss; 1829 union { 1830 struct iphdr *v4; 1831 struct ipv6hdr *v6; 1832 unsigned char *hdr; 1833 } ip; 1834 union { 1835 struct tcphdr *tcp; 1836 struct udphdr *udp; 1837 unsigned char *hdr; 1838 } l4; 1839 u32 paylen, l4_offset; 1840 u16 gso_segs, gso_size; 1841 int err; 1842 1843 if (skb->ip_summed != CHECKSUM_PARTIAL) 1844 return 0; 1845 1846 if (!skb_is_gso(skb)) 1847 return 0; 1848 1849 err = skb_cow_head(skb, 0); 1850 if (err < 0) 1851 return err; 1852 1853 ip.hdr = skb_network_header(skb); 1854 l4.hdr = skb_transport_header(skb); 1855 1856 /* initialize outer IP header fields */ 1857 if (ip.v4->version == 4) { 1858 ip.v4->tot_len = 0; 1859 ip.v4->check = 0; 1860 } else { 1861 ip.v6->payload_len = 0; 1862 } 1863 1864 if (skb_shinfo(skb)->gso_type & (SKB_GSO_GRE | 1865 SKB_GSO_GRE_CSUM | 1866 SKB_GSO_IPXIP4 | 1867 SKB_GSO_IPXIP6 | 1868 SKB_GSO_UDP_TUNNEL | 1869 SKB_GSO_UDP_TUNNEL_CSUM)) { 1870 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL) && 1871 (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM)) { 1872 l4.udp->len = 0; 1873 1874 /* determine offset of outer transport header */ 1875 l4_offset = l4.hdr - skb->data; 1876 1877 /* remove payload length from outer checksum */ 1878 paylen = skb->len - l4_offset; 1879 csum_replace_by_diff(&l4.udp->check, 1880 (__force __wsum)htonl(paylen)); 1881 } 1882 1883 /* reset pointers to inner headers */ 1884 ip.hdr = skb_inner_network_header(skb); 1885 l4.hdr = skb_inner_transport_header(skb); 1886 1887 /* initialize inner IP header fields */ 1888 if (ip.v4->version == 4) { 1889 ip.v4->tot_len = 0; 1890 ip.v4->check = 0; 1891 } else { 1892 ip.v6->payload_len = 0; 1893 } 1894 } 1895 1896 /* determine offset of inner transport header */ 1897 l4_offset = l4.hdr - skb->data; 1898 /* remove payload length from inner checksum */ 1899 paylen = skb->len - l4_offset; 1900 1901 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) { 1902 csum_replace_by_diff(&l4.udp->check, 1903 (__force __wsum)htonl(paylen)); 1904 /* compute length of UDP segmentation header */ 1905 *hdr_len = (u8)sizeof(l4.udp) + l4_offset; 1906 } else { 1907 csum_replace_by_diff(&l4.tcp->check, 1908 (__force __wsum)htonl(paylen)); 1909 /* compute length of TCP segmentation header */ 1910 *hdr_len = (u8)((l4.tcp->doff * 4) + l4_offset); 1911 } 1912 1913 /* pull values out of skb_shinfo */ 1914 gso_size = skb_shinfo(skb)->gso_size; 1915 gso_segs = skb_shinfo(skb)->gso_segs; 1916 1917 /* update GSO size and bytecount with header size */ 1918 first->gso_segs = gso_segs; 1919 first->bytecount += (first->gso_segs - 1) * *hdr_len; 1920 1921 /* find the field values */ 1922 cd_cmd = IAVF_TX_CTX_DESC_TSO; 1923 cd_tso_len = skb->len - *hdr_len; 1924 cd_mss = gso_size; 1925 *cd_type_cmd_tso_mss |= (cd_cmd << IAVF_TXD_CTX_QW1_CMD_SHIFT) | 1926 (cd_tso_len << IAVF_TXD_CTX_QW1_TSO_LEN_SHIFT) | 1927 (cd_mss << IAVF_TXD_CTX_QW1_MSS_SHIFT); 1928 return 1; 1929 } 1930 1931 /** 1932 * iavf_tx_enable_csum - Enable Tx checksum offloads 1933 * @skb: send buffer 1934 * @tx_flags: pointer to Tx flags currently set 1935 * @td_cmd: Tx descriptor command bits to set 1936 * @td_offset: Tx descriptor header offsets to set 1937 * @tx_ring: Tx descriptor ring 1938 * @cd_tunneling: ptr to context desc bits 1939 **/ 1940 static int iavf_tx_enable_csum(struct sk_buff *skb, u32 *tx_flags, 1941 u32 *td_cmd, u32 *td_offset, 1942 struct iavf_ring *tx_ring, 1943 u32 *cd_tunneling) 1944 { 1945 union { 1946 struct iphdr *v4; 1947 struct ipv6hdr *v6; 1948 unsigned char *hdr; 1949 } ip; 1950 union { 1951 struct tcphdr *tcp; 1952 struct udphdr *udp; 1953 unsigned char *hdr; 1954 } l4; 1955 unsigned char *exthdr; 1956 u32 offset, cmd = 0; 1957 __be16 frag_off; 1958 u8 l4_proto = 0; 1959 1960 if (skb->ip_summed != CHECKSUM_PARTIAL) 1961 return 0; 1962 1963 ip.hdr = skb_network_header(skb); 1964 l4.hdr = skb_transport_header(skb); 1965 1966 /* compute outer L2 header size */ 1967 offset = ((ip.hdr - skb->data) / 2) << IAVF_TX_DESC_LENGTH_MACLEN_SHIFT; 1968 1969 if (skb->encapsulation) { 1970 u32 tunnel = 0; 1971 /* define outer network header type */ 1972 if (*tx_flags & IAVF_TX_FLAGS_IPV4) { 1973 tunnel |= (*tx_flags & IAVF_TX_FLAGS_TSO) ? 1974 IAVF_TX_CTX_EXT_IP_IPV4 : 1975 IAVF_TX_CTX_EXT_IP_IPV4_NO_CSUM; 1976 1977 l4_proto = ip.v4->protocol; 1978 } else if (*tx_flags & IAVF_TX_FLAGS_IPV6) { 1979 tunnel |= IAVF_TX_CTX_EXT_IP_IPV6; 1980 1981 exthdr = ip.hdr + sizeof(*ip.v6); 1982 l4_proto = ip.v6->nexthdr; 1983 if (l4.hdr != exthdr) 1984 ipv6_skip_exthdr(skb, exthdr - skb->data, 1985 &l4_proto, &frag_off); 1986 } 1987 1988 /* define outer transport */ 1989 switch (l4_proto) { 1990 case IPPROTO_UDP: 1991 tunnel |= IAVF_TXD_CTX_UDP_TUNNELING; 1992 *tx_flags |= IAVF_TX_FLAGS_VXLAN_TUNNEL; 1993 break; 1994 case IPPROTO_GRE: 1995 tunnel |= IAVF_TXD_CTX_GRE_TUNNELING; 1996 *tx_flags |= IAVF_TX_FLAGS_VXLAN_TUNNEL; 1997 break; 1998 case IPPROTO_IPIP: 1999 case IPPROTO_IPV6: 2000 *tx_flags |= IAVF_TX_FLAGS_VXLAN_TUNNEL; 2001 l4.hdr = skb_inner_network_header(skb); 2002 break; 2003 default: 2004 if (*tx_flags & IAVF_TX_FLAGS_TSO) 2005 return -1; 2006 2007 skb_checksum_help(skb); 2008 return 0; 2009 } 2010 2011 /* compute outer L3 header size */ 2012 tunnel |= ((l4.hdr - ip.hdr) / 4) << 2013 IAVF_TXD_CTX_QW0_EXT_IPLEN_SHIFT; 2014 2015 /* switch IP header pointer from outer to inner header */ 2016 ip.hdr = skb_inner_network_header(skb); 2017 2018 /* compute tunnel header size */ 2019 tunnel |= ((ip.hdr - l4.hdr) / 2) << 2020 IAVF_TXD_CTX_QW0_NATLEN_SHIFT; 2021 2022 /* indicate if we need to offload outer UDP header */ 2023 if ((*tx_flags & IAVF_TX_FLAGS_TSO) && 2024 !(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL) && 2025 (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM)) 2026 tunnel |= IAVF_TXD_CTX_QW0_L4T_CS_MASK; 2027 2028 /* record tunnel offload values */ 2029 *cd_tunneling |= tunnel; 2030 2031 /* switch L4 header pointer from outer to inner */ 2032 l4.hdr = skb_inner_transport_header(skb); 2033 l4_proto = 0; 2034 2035 /* reset type as we transition from outer to inner headers */ 2036 *tx_flags &= ~(IAVF_TX_FLAGS_IPV4 | IAVF_TX_FLAGS_IPV6); 2037 if (ip.v4->version == 4) 2038 *tx_flags |= IAVF_TX_FLAGS_IPV4; 2039 if (ip.v6->version == 6) 2040 *tx_flags |= IAVF_TX_FLAGS_IPV6; 2041 } 2042 2043 /* Enable IP checksum offloads */ 2044 if (*tx_flags & IAVF_TX_FLAGS_IPV4) { 2045 l4_proto = ip.v4->protocol; 2046 /* the stack computes the IP header already, the only time we 2047 * need the hardware to recompute it is in the case of TSO. 2048 */ 2049 cmd |= (*tx_flags & IAVF_TX_FLAGS_TSO) ? 2050 IAVF_TX_DESC_CMD_IIPT_IPV4_CSUM : 2051 IAVF_TX_DESC_CMD_IIPT_IPV4; 2052 } else if (*tx_flags & IAVF_TX_FLAGS_IPV6) { 2053 cmd |= IAVF_TX_DESC_CMD_IIPT_IPV6; 2054 2055 exthdr = ip.hdr + sizeof(*ip.v6); 2056 l4_proto = ip.v6->nexthdr; 2057 if (l4.hdr != exthdr) 2058 ipv6_skip_exthdr(skb, exthdr - skb->data, 2059 &l4_proto, &frag_off); 2060 } 2061 2062 /* compute inner L3 header size */ 2063 offset |= ((l4.hdr - ip.hdr) / 4) << IAVF_TX_DESC_LENGTH_IPLEN_SHIFT; 2064 2065 /* Enable L4 checksum offloads */ 2066 switch (l4_proto) { 2067 case IPPROTO_TCP: 2068 /* enable checksum offloads */ 2069 cmd |= IAVF_TX_DESC_CMD_L4T_EOFT_TCP; 2070 offset |= l4.tcp->doff << IAVF_TX_DESC_LENGTH_L4_FC_LEN_SHIFT; 2071 break; 2072 case IPPROTO_SCTP: 2073 /* enable SCTP checksum offload */ 2074 cmd |= IAVF_TX_DESC_CMD_L4T_EOFT_SCTP; 2075 offset |= (sizeof(struct sctphdr) >> 2) << 2076 IAVF_TX_DESC_LENGTH_L4_FC_LEN_SHIFT; 2077 break; 2078 case IPPROTO_UDP: 2079 /* enable UDP checksum offload */ 2080 cmd |= IAVF_TX_DESC_CMD_L4T_EOFT_UDP; 2081 offset |= (sizeof(struct udphdr) >> 2) << 2082 IAVF_TX_DESC_LENGTH_L4_FC_LEN_SHIFT; 2083 break; 2084 default: 2085 if (*tx_flags & IAVF_TX_FLAGS_TSO) 2086 return -1; 2087 skb_checksum_help(skb); 2088 return 0; 2089 } 2090 2091 *td_cmd |= cmd; 2092 *td_offset |= offset; 2093 2094 return 1; 2095 } 2096 2097 /** 2098 * iavf_create_tx_ctx - Build the Tx context descriptor 2099 * @tx_ring: ring to create the descriptor on 2100 * @cd_type_cmd_tso_mss: Quad Word 1 2101 * @cd_tunneling: Quad Word 0 - bits 0-31 2102 * @cd_l2tag2: Quad Word 0 - bits 32-63 2103 **/ 2104 static void iavf_create_tx_ctx(struct iavf_ring *tx_ring, 2105 const u64 cd_type_cmd_tso_mss, 2106 const u32 cd_tunneling, const u32 cd_l2tag2) 2107 { 2108 struct iavf_tx_context_desc *context_desc; 2109 int i = tx_ring->next_to_use; 2110 2111 if ((cd_type_cmd_tso_mss == IAVF_TX_DESC_DTYPE_CONTEXT) && 2112 !cd_tunneling && !cd_l2tag2) 2113 return; 2114 2115 /* grab the next descriptor */ 2116 context_desc = IAVF_TX_CTXTDESC(tx_ring, i); 2117 2118 i++; 2119 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0; 2120 2121 /* cpu_to_le32 and assign to struct fields */ 2122 context_desc->tunneling_params = cpu_to_le32(cd_tunneling); 2123 context_desc->l2tag2 = cpu_to_le16(cd_l2tag2); 2124 context_desc->rsvd = cpu_to_le16(0); 2125 context_desc->type_cmd_tso_mss = cpu_to_le64(cd_type_cmd_tso_mss); 2126 } 2127 2128 /** 2129 * __iavf_chk_linearize - Check if there are more than 8 buffers per packet 2130 * @skb: send buffer 2131 * 2132 * Note: Our HW can't DMA more than 8 buffers to build a packet on the wire 2133 * and so we need to figure out the cases where we need to linearize the skb. 2134 * 2135 * For TSO we need to count the TSO header and segment payload separately. 2136 * As such we need to check cases where we have 7 fragments or more as we 2137 * can potentially require 9 DMA transactions, 1 for the TSO header, 1 for 2138 * the segment payload in the first descriptor, and another 7 for the 2139 * fragments. 2140 **/ 2141 bool __iavf_chk_linearize(struct sk_buff *skb) 2142 { 2143 const skb_frag_t *frag, *stale; 2144 int nr_frags, sum; 2145 2146 /* no need to check if number of frags is less than 7 */ 2147 nr_frags = skb_shinfo(skb)->nr_frags; 2148 if (nr_frags < (IAVF_MAX_BUFFER_TXD - 1)) 2149 return false; 2150 2151 /* We need to walk through the list and validate that each group 2152 * of 6 fragments totals at least gso_size. 2153 */ 2154 nr_frags -= IAVF_MAX_BUFFER_TXD - 2; 2155 frag = &skb_shinfo(skb)->frags[0]; 2156 2157 /* Initialize size to the negative value of gso_size minus 1. We 2158 * use this as the worst case scenerio in which the frag ahead 2159 * of us only provides one byte which is why we are limited to 6 2160 * descriptors for a single transmit as the header and previous 2161 * fragment are already consuming 2 descriptors. 2162 */ 2163 sum = 1 - skb_shinfo(skb)->gso_size; 2164 2165 /* Add size of frags 0 through 4 to create our initial sum */ 2166 sum += skb_frag_size(frag++); 2167 sum += skb_frag_size(frag++); 2168 sum += skb_frag_size(frag++); 2169 sum += skb_frag_size(frag++); 2170 sum += skb_frag_size(frag++); 2171 2172 /* Walk through fragments adding latest fragment, testing it, and 2173 * then removing stale fragments from the sum. 2174 */ 2175 for (stale = &skb_shinfo(skb)->frags[0];; stale++) { 2176 int stale_size = skb_frag_size(stale); 2177 2178 sum += skb_frag_size(frag++); 2179 2180 /* The stale fragment may present us with a smaller 2181 * descriptor than the actual fragment size. To account 2182 * for that we need to remove all the data on the front and 2183 * figure out what the remainder would be in the last 2184 * descriptor associated with the fragment. 2185 */ 2186 if (stale_size > IAVF_MAX_DATA_PER_TXD) { 2187 int align_pad = -(skb_frag_off(stale)) & 2188 (IAVF_MAX_READ_REQ_SIZE - 1); 2189 2190 sum -= align_pad; 2191 stale_size -= align_pad; 2192 2193 do { 2194 sum -= IAVF_MAX_DATA_PER_TXD_ALIGNED; 2195 stale_size -= IAVF_MAX_DATA_PER_TXD_ALIGNED; 2196 } while (stale_size > IAVF_MAX_DATA_PER_TXD); 2197 } 2198 2199 /* if sum is negative we failed to make sufficient progress */ 2200 if (sum < 0) 2201 return true; 2202 2203 if (!nr_frags--) 2204 break; 2205 2206 sum -= stale_size; 2207 } 2208 2209 return false; 2210 } 2211 2212 /** 2213 * __iavf_maybe_stop_tx - 2nd level check for tx stop conditions 2214 * @tx_ring: the ring to be checked 2215 * @size: the size buffer we want to assure is available 2216 * 2217 * Returns -EBUSY if a stop is needed, else 0 2218 **/ 2219 int __iavf_maybe_stop_tx(struct iavf_ring *tx_ring, int size) 2220 { 2221 netif_stop_subqueue(tx_ring->netdev, tx_ring->queue_index); 2222 /* Memory barrier before checking head and tail */ 2223 smp_mb(); 2224 2225 /* Check again in a case another CPU has just made room available. */ 2226 if (likely(IAVF_DESC_UNUSED(tx_ring) < size)) 2227 return -EBUSY; 2228 2229 /* A reprieve! - use start_queue because it doesn't call schedule */ 2230 netif_start_subqueue(tx_ring->netdev, tx_ring->queue_index); 2231 ++tx_ring->tx_stats.restart_queue; 2232 return 0; 2233 } 2234 2235 /** 2236 * iavf_tx_map - Build the Tx descriptor 2237 * @tx_ring: ring to send buffer on 2238 * @skb: send buffer 2239 * @first: first buffer info buffer to use 2240 * @tx_flags: collected send information 2241 * @hdr_len: size of the packet header 2242 * @td_cmd: the command field in the descriptor 2243 * @td_offset: offset for checksum or crc 2244 **/ 2245 static inline void iavf_tx_map(struct iavf_ring *tx_ring, struct sk_buff *skb, 2246 struct iavf_tx_buffer *first, u32 tx_flags, 2247 const u8 hdr_len, u32 td_cmd, u32 td_offset) 2248 { 2249 unsigned int data_len = skb->data_len; 2250 unsigned int size = skb_headlen(skb); 2251 skb_frag_t *frag; 2252 struct iavf_tx_buffer *tx_bi; 2253 struct iavf_tx_desc *tx_desc; 2254 u16 i = tx_ring->next_to_use; 2255 u32 td_tag = 0; 2256 dma_addr_t dma; 2257 2258 if (tx_flags & IAVF_TX_FLAGS_HW_VLAN) { 2259 td_cmd |= IAVF_TX_DESC_CMD_IL2TAG1; 2260 td_tag = (tx_flags & IAVF_TX_FLAGS_VLAN_MASK) >> 2261 IAVF_TX_FLAGS_VLAN_SHIFT; 2262 } 2263 2264 first->tx_flags = tx_flags; 2265 2266 dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE); 2267 2268 tx_desc = IAVF_TX_DESC(tx_ring, i); 2269 tx_bi = first; 2270 2271 for (frag = &skb_shinfo(skb)->frags[0];; frag++) { 2272 unsigned int max_data = IAVF_MAX_DATA_PER_TXD_ALIGNED; 2273 2274 if (dma_mapping_error(tx_ring->dev, dma)) 2275 goto dma_error; 2276 2277 /* record length, and DMA address */ 2278 dma_unmap_len_set(tx_bi, len, size); 2279 dma_unmap_addr_set(tx_bi, dma, dma); 2280 2281 /* align size to end of page */ 2282 max_data += -dma & (IAVF_MAX_READ_REQ_SIZE - 1); 2283 tx_desc->buffer_addr = cpu_to_le64(dma); 2284 2285 while (unlikely(size > IAVF_MAX_DATA_PER_TXD)) { 2286 tx_desc->cmd_type_offset_bsz = 2287 build_ctob(td_cmd, td_offset, 2288 max_data, td_tag); 2289 2290 tx_desc++; 2291 i++; 2292 2293 if (i == tx_ring->count) { 2294 tx_desc = IAVF_TX_DESC(tx_ring, 0); 2295 i = 0; 2296 } 2297 2298 dma += max_data; 2299 size -= max_data; 2300 2301 max_data = IAVF_MAX_DATA_PER_TXD_ALIGNED; 2302 tx_desc->buffer_addr = cpu_to_le64(dma); 2303 } 2304 2305 if (likely(!data_len)) 2306 break; 2307 2308 tx_desc->cmd_type_offset_bsz = build_ctob(td_cmd, td_offset, 2309 size, td_tag); 2310 2311 tx_desc++; 2312 i++; 2313 2314 if (i == tx_ring->count) { 2315 tx_desc = IAVF_TX_DESC(tx_ring, 0); 2316 i = 0; 2317 } 2318 2319 size = skb_frag_size(frag); 2320 data_len -= size; 2321 2322 dma = skb_frag_dma_map(tx_ring->dev, frag, 0, size, 2323 DMA_TO_DEVICE); 2324 2325 tx_bi = &tx_ring->tx_bi[i]; 2326 } 2327 2328 netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount); 2329 2330 i++; 2331 if (i == tx_ring->count) 2332 i = 0; 2333 2334 tx_ring->next_to_use = i; 2335 2336 iavf_maybe_stop_tx(tx_ring, DESC_NEEDED); 2337 2338 /* write last descriptor with RS and EOP bits */ 2339 td_cmd |= IAVF_TXD_CMD; 2340 tx_desc->cmd_type_offset_bsz = 2341 build_ctob(td_cmd, td_offset, size, td_tag); 2342 2343 skb_tx_timestamp(skb); 2344 2345 /* Force memory writes to complete before letting h/w know there 2346 * are new descriptors to fetch. 2347 * 2348 * We also use this memory barrier to make certain all of the 2349 * status bits have been updated before next_to_watch is written. 2350 */ 2351 wmb(); 2352 2353 /* set next_to_watch value indicating a packet is present */ 2354 first->next_to_watch = tx_desc; 2355 2356 /* notify HW of packet */ 2357 if (netif_xmit_stopped(txring_txq(tx_ring)) || !netdev_xmit_more()) { 2358 writel(i, tx_ring->tail); 2359 } 2360 2361 return; 2362 2363 dma_error: 2364 dev_info(tx_ring->dev, "TX DMA map failed\n"); 2365 2366 /* clear dma mappings for failed tx_bi map */ 2367 for (;;) { 2368 tx_bi = &tx_ring->tx_bi[i]; 2369 iavf_unmap_and_free_tx_resource(tx_ring, tx_bi); 2370 if (tx_bi == first) 2371 break; 2372 if (i == 0) 2373 i = tx_ring->count; 2374 i--; 2375 } 2376 2377 tx_ring->next_to_use = i; 2378 } 2379 2380 /** 2381 * iavf_xmit_frame_ring - Sends buffer on Tx ring 2382 * @skb: send buffer 2383 * @tx_ring: ring to send buffer on 2384 * 2385 * Returns NETDEV_TX_OK if sent, else an error code 2386 **/ 2387 static netdev_tx_t iavf_xmit_frame_ring(struct sk_buff *skb, 2388 struct iavf_ring *tx_ring) 2389 { 2390 u64 cd_type_cmd_tso_mss = IAVF_TX_DESC_DTYPE_CONTEXT; 2391 u32 cd_tunneling = 0, cd_l2tag2 = 0; 2392 struct iavf_tx_buffer *first; 2393 u32 td_offset = 0; 2394 u32 tx_flags = 0; 2395 __be16 protocol; 2396 u32 td_cmd = 0; 2397 u8 hdr_len = 0; 2398 int tso, count; 2399 2400 /* prefetch the data, we'll need it later */ 2401 prefetch(skb->data); 2402 2403 iavf_trace(xmit_frame_ring, skb, tx_ring); 2404 2405 count = iavf_xmit_descriptor_count(skb); 2406 if (iavf_chk_linearize(skb, count)) { 2407 if (__skb_linearize(skb)) { 2408 dev_kfree_skb_any(skb); 2409 return NETDEV_TX_OK; 2410 } 2411 count = iavf_txd_use_count(skb->len); 2412 tx_ring->tx_stats.tx_linearize++; 2413 } 2414 2415 /* need: 1 descriptor per page * PAGE_SIZE/IAVF_MAX_DATA_PER_TXD, 2416 * + 1 desc for skb_head_len/IAVF_MAX_DATA_PER_TXD, 2417 * + 4 desc gap to avoid the cache line where head is, 2418 * + 1 desc for context descriptor, 2419 * otherwise try next time 2420 */ 2421 if (iavf_maybe_stop_tx(tx_ring, count + 4 + 1)) { 2422 tx_ring->tx_stats.tx_busy++; 2423 return NETDEV_TX_BUSY; 2424 } 2425 2426 /* record the location of the first descriptor for this packet */ 2427 first = &tx_ring->tx_bi[tx_ring->next_to_use]; 2428 first->skb = skb; 2429 first->bytecount = skb->len; 2430 first->gso_segs = 1; 2431 2432 /* prepare the xmit flags */ 2433 iavf_tx_prepare_vlan_flags(skb, tx_ring, &tx_flags); 2434 if (tx_flags & IAVF_TX_FLAGS_HW_OUTER_SINGLE_VLAN) { 2435 cd_type_cmd_tso_mss |= IAVF_TX_CTX_DESC_IL2TAG2 << 2436 IAVF_TXD_CTX_QW1_CMD_SHIFT; 2437 cd_l2tag2 = (tx_flags & IAVF_TX_FLAGS_VLAN_MASK) >> 2438 IAVF_TX_FLAGS_VLAN_SHIFT; 2439 } 2440 2441 /* obtain protocol of skb */ 2442 protocol = vlan_get_protocol(skb); 2443 2444 /* setup IPv4/IPv6 offloads */ 2445 if (protocol == htons(ETH_P_IP)) 2446 tx_flags |= IAVF_TX_FLAGS_IPV4; 2447 else if (protocol == htons(ETH_P_IPV6)) 2448 tx_flags |= IAVF_TX_FLAGS_IPV6; 2449 2450 tso = iavf_tso(first, &hdr_len, &cd_type_cmd_tso_mss); 2451 2452 if (tso < 0) 2453 goto out_drop; 2454 else if (tso) 2455 tx_flags |= IAVF_TX_FLAGS_TSO; 2456 2457 /* Always offload the checksum, since it's in the data descriptor */ 2458 tso = iavf_tx_enable_csum(skb, &tx_flags, &td_cmd, &td_offset, 2459 tx_ring, &cd_tunneling); 2460 if (tso < 0) 2461 goto out_drop; 2462 2463 /* always enable CRC insertion offload */ 2464 td_cmd |= IAVF_TX_DESC_CMD_ICRC; 2465 2466 iavf_create_tx_ctx(tx_ring, cd_type_cmd_tso_mss, 2467 cd_tunneling, cd_l2tag2); 2468 2469 iavf_tx_map(tx_ring, skb, first, tx_flags, hdr_len, 2470 td_cmd, td_offset); 2471 2472 return NETDEV_TX_OK; 2473 2474 out_drop: 2475 iavf_trace(xmit_frame_ring_drop, first->skb, tx_ring); 2476 dev_kfree_skb_any(first->skb); 2477 first->skb = NULL; 2478 return NETDEV_TX_OK; 2479 } 2480 2481 /** 2482 * iavf_xmit_frame - Selects the correct VSI and Tx queue to send buffer 2483 * @skb: send buffer 2484 * @netdev: network interface device structure 2485 * 2486 * Returns NETDEV_TX_OK if sent, else an error code 2487 **/ 2488 netdev_tx_t iavf_xmit_frame(struct sk_buff *skb, struct net_device *netdev) 2489 { 2490 struct iavf_adapter *adapter = netdev_priv(netdev); 2491 struct iavf_ring *tx_ring = &adapter->tx_rings[skb->queue_mapping]; 2492 2493 /* hardware can't handle really short frames, hardware padding works 2494 * beyond this point 2495 */ 2496 if (unlikely(skb->len < IAVF_MIN_TX_LEN)) { 2497 if (skb_pad(skb, IAVF_MIN_TX_LEN - skb->len)) 2498 return NETDEV_TX_OK; 2499 skb->len = IAVF_MIN_TX_LEN; 2500 skb_set_tail_pointer(skb, IAVF_MIN_TX_LEN); 2501 } 2502 2503 return iavf_xmit_frame_ring(skb, tx_ring); 2504 } 2505