1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2018 Intel Corporation */ 3 4 #include <linux/module.h> 5 #include <linux/types.h> 6 #include <linux/if_vlan.h> 7 #include <linux/tcp.h> 8 #include <linux/udp.h> 9 #include <linux/ip.h> 10 #include <linux/pm_runtime.h> 11 #include <net/pkt_sched.h> 12 #include <linux/bpf_trace.h> 13 #include <net/xdp_sock_drv.h> 14 #include <linux/pci.h> 15 #include <linux/mdio.h> 16 17 #include <net/ipv6.h> 18 19 #include "igc.h" 20 #include "igc_hw.h" 21 #include "igc_tsn.h" 22 #include "igc_xdp.h" 23 24 #define DRV_SUMMARY "Intel(R) 2.5G Ethernet Linux Driver" 25 26 #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK) 27 28 #define IGC_XDP_PASS 0 29 #define IGC_XDP_CONSUMED BIT(0) 30 #define IGC_XDP_TX BIT(1) 31 #define IGC_XDP_REDIRECT BIT(2) 32 33 static int debug = -1; 34 35 MODULE_DESCRIPTION(DRV_SUMMARY); 36 MODULE_LICENSE("GPL v2"); 37 module_param(debug, int, 0); 38 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)"); 39 40 char igc_driver_name[] = "igc"; 41 static const char igc_driver_string[] = DRV_SUMMARY; 42 static const char igc_copyright[] = 43 "Copyright(c) 2018 Intel Corporation."; 44 45 static const struct igc_info *igc_info_tbl[] = { 46 [board_base] = &igc_base_info, 47 }; 48 49 static const struct pci_device_id igc_pci_tbl[] = { 50 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_LM), board_base }, 51 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_V), board_base }, 52 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_I), board_base }, 53 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I220_V), board_base }, 54 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_K), board_base }, 55 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_K2), board_base }, 56 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I226_K), board_base }, 57 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_LMVP), board_base }, 58 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I226_LMVP), board_base }, 59 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_IT), board_base }, 60 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I226_LM), board_base }, 61 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I226_V), board_base }, 62 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I226_IT), board_base }, 63 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I221_V), board_base }, 64 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I226_BLANK_NVM), board_base }, 65 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_BLANK_NVM), board_base }, 66 /* required last entry */ 67 {0, } 68 }; 69 70 MODULE_DEVICE_TABLE(pci, igc_pci_tbl); 71 72 enum latency_range { 73 lowest_latency = 0, 74 low_latency = 1, 75 bulk_latency = 2, 76 latency_invalid = 255 77 }; 78 79 void igc_reset(struct igc_adapter *adapter) 80 { 81 struct net_device *dev = adapter->netdev; 82 struct igc_hw *hw = &adapter->hw; 83 struct igc_fc_info *fc = &hw->fc; 84 u32 pba, hwm; 85 86 /* Repartition PBA for greater than 9k MTU if required */ 87 pba = IGC_PBA_34K; 88 89 /* flow control settings 90 * The high water mark must be low enough to fit one full frame 91 * after transmitting the pause frame. As such we must have enough 92 * space to allow for us to complete our current transmit and then 93 * receive the frame that is in progress from the link partner. 94 * Set it to: 95 * - the full Rx FIFO size minus one full Tx plus one full Rx frame 96 */ 97 hwm = (pba << 10) - (adapter->max_frame_size + MAX_JUMBO_FRAME_SIZE); 98 99 fc->high_water = hwm & 0xFFFFFFF0; /* 16-byte granularity */ 100 fc->low_water = fc->high_water - 16; 101 fc->pause_time = 0xFFFF; 102 fc->send_xon = 1; 103 fc->current_mode = fc->requested_mode; 104 105 hw->mac.ops.reset_hw(hw); 106 107 if (hw->mac.ops.init_hw(hw)) 108 netdev_err(dev, "Error on hardware initialization\n"); 109 110 /* Re-establish EEE setting */ 111 igc_set_eee_i225(hw, true, true, true); 112 113 if (!netif_running(adapter->netdev)) 114 igc_power_down_phy_copper_base(&adapter->hw); 115 116 /* Enable HW to recognize an 802.1Q VLAN Ethernet packet */ 117 wr32(IGC_VET, ETH_P_8021Q); 118 119 /* Re-enable PTP, where applicable. */ 120 igc_ptp_reset(adapter); 121 122 /* Re-enable TSN offloading, where applicable. */ 123 igc_tsn_reset(adapter); 124 125 igc_get_phy_info(hw); 126 } 127 128 /** 129 * igc_power_up_link - Power up the phy link 130 * @adapter: address of board private structure 131 */ 132 static void igc_power_up_link(struct igc_adapter *adapter) 133 { 134 igc_reset_phy(&adapter->hw); 135 136 igc_power_up_phy_copper(&adapter->hw); 137 138 igc_setup_link(&adapter->hw); 139 } 140 141 /** 142 * igc_release_hw_control - release control of the h/w to f/w 143 * @adapter: address of board private structure 144 * 145 * igc_release_hw_control resets CTRL_EXT:DRV_LOAD bit. 146 * For ASF and Pass Through versions of f/w this means that the 147 * driver is no longer loaded. 148 */ 149 static void igc_release_hw_control(struct igc_adapter *adapter) 150 { 151 struct igc_hw *hw = &adapter->hw; 152 u32 ctrl_ext; 153 154 if (!pci_device_is_present(adapter->pdev)) 155 return; 156 157 /* Let firmware take over control of h/w */ 158 ctrl_ext = rd32(IGC_CTRL_EXT); 159 wr32(IGC_CTRL_EXT, 160 ctrl_ext & ~IGC_CTRL_EXT_DRV_LOAD); 161 } 162 163 /** 164 * igc_get_hw_control - get control of the h/w from f/w 165 * @adapter: address of board private structure 166 * 167 * igc_get_hw_control sets CTRL_EXT:DRV_LOAD bit. 168 * For ASF and Pass Through versions of f/w this means that 169 * the driver is loaded. 170 */ 171 static void igc_get_hw_control(struct igc_adapter *adapter) 172 { 173 struct igc_hw *hw = &adapter->hw; 174 u32 ctrl_ext; 175 176 /* Let firmware know the driver has taken over */ 177 ctrl_ext = rd32(IGC_CTRL_EXT); 178 wr32(IGC_CTRL_EXT, 179 ctrl_ext | IGC_CTRL_EXT_DRV_LOAD); 180 } 181 182 static void igc_unmap_tx_buffer(struct device *dev, struct igc_tx_buffer *buf) 183 { 184 dma_unmap_single(dev, dma_unmap_addr(buf, dma), 185 dma_unmap_len(buf, len), DMA_TO_DEVICE); 186 187 dma_unmap_len_set(buf, len, 0); 188 } 189 190 /** 191 * igc_clean_tx_ring - Free Tx Buffers 192 * @tx_ring: ring to be cleaned 193 */ 194 static void igc_clean_tx_ring(struct igc_ring *tx_ring) 195 { 196 u16 i = tx_ring->next_to_clean; 197 struct igc_tx_buffer *tx_buffer = &tx_ring->tx_buffer_info[i]; 198 u32 xsk_frames = 0; 199 200 while (i != tx_ring->next_to_use) { 201 union igc_adv_tx_desc *eop_desc, *tx_desc; 202 203 switch (tx_buffer->type) { 204 case IGC_TX_BUFFER_TYPE_XSK: 205 xsk_frames++; 206 break; 207 case IGC_TX_BUFFER_TYPE_XDP: 208 xdp_return_frame(tx_buffer->xdpf); 209 igc_unmap_tx_buffer(tx_ring->dev, tx_buffer); 210 break; 211 case IGC_TX_BUFFER_TYPE_SKB: 212 dev_kfree_skb_any(tx_buffer->skb); 213 igc_unmap_tx_buffer(tx_ring->dev, tx_buffer); 214 break; 215 default: 216 netdev_warn_once(tx_ring->netdev, "Unknown Tx buffer type\n"); 217 break; 218 } 219 220 /* check for eop_desc to determine the end of the packet */ 221 eop_desc = tx_buffer->next_to_watch; 222 tx_desc = IGC_TX_DESC(tx_ring, i); 223 224 /* unmap remaining buffers */ 225 while (tx_desc != eop_desc) { 226 tx_buffer++; 227 tx_desc++; 228 i++; 229 if (unlikely(i == tx_ring->count)) { 230 i = 0; 231 tx_buffer = tx_ring->tx_buffer_info; 232 tx_desc = IGC_TX_DESC(tx_ring, 0); 233 } 234 235 /* unmap any remaining paged data */ 236 if (dma_unmap_len(tx_buffer, len)) 237 igc_unmap_tx_buffer(tx_ring->dev, tx_buffer); 238 } 239 240 tx_buffer->next_to_watch = NULL; 241 242 /* move us one more past the eop_desc for start of next pkt */ 243 tx_buffer++; 244 i++; 245 if (unlikely(i == tx_ring->count)) { 246 i = 0; 247 tx_buffer = tx_ring->tx_buffer_info; 248 } 249 } 250 251 if (tx_ring->xsk_pool && xsk_frames) 252 xsk_tx_completed(tx_ring->xsk_pool, xsk_frames); 253 254 /* reset BQL for queue */ 255 netdev_tx_reset_queue(txring_txq(tx_ring)); 256 257 /* Zero out the buffer ring */ 258 memset(tx_ring->tx_buffer_info, 0, 259 sizeof(*tx_ring->tx_buffer_info) * tx_ring->count); 260 261 /* Zero out the descriptor ring */ 262 memset(tx_ring->desc, 0, tx_ring->size); 263 264 /* reset next_to_use and next_to_clean */ 265 tx_ring->next_to_use = 0; 266 tx_ring->next_to_clean = 0; 267 } 268 269 /** 270 * igc_free_tx_resources - Free Tx Resources per Queue 271 * @tx_ring: Tx descriptor ring for a specific queue 272 * 273 * Free all transmit software resources 274 */ 275 void igc_free_tx_resources(struct igc_ring *tx_ring) 276 { 277 igc_disable_tx_ring(tx_ring); 278 279 vfree(tx_ring->tx_buffer_info); 280 tx_ring->tx_buffer_info = NULL; 281 282 /* if not set, then don't free */ 283 if (!tx_ring->desc) 284 return; 285 286 dma_free_coherent(tx_ring->dev, tx_ring->size, 287 tx_ring->desc, tx_ring->dma); 288 289 tx_ring->desc = NULL; 290 } 291 292 /** 293 * igc_free_all_tx_resources - Free Tx Resources for All Queues 294 * @adapter: board private structure 295 * 296 * Free all transmit software resources 297 */ 298 static void igc_free_all_tx_resources(struct igc_adapter *adapter) 299 { 300 int i; 301 302 for (i = 0; i < adapter->num_tx_queues; i++) 303 igc_free_tx_resources(adapter->tx_ring[i]); 304 } 305 306 /** 307 * igc_clean_all_tx_rings - Free Tx Buffers for all queues 308 * @adapter: board private structure 309 */ 310 static void igc_clean_all_tx_rings(struct igc_adapter *adapter) 311 { 312 int i; 313 314 for (i = 0; i < adapter->num_tx_queues; i++) 315 if (adapter->tx_ring[i]) 316 igc_clean_tx_ring(adapter->tx_ring[i]); 317 } 318 319 static void igc_disable_tx_ring_hw(struct igc_ring *ring) 320 { 321 struct igc_hw *hw = &ring->q_vector->adapter->hw; 322 u8 idx = ring->reg_idx; 323 u32 txdctl; 324 325 txdctl = rd32(IGC_TXDCTL(idx)); 326 txdctl &= ~IGC_TXDCTL_QUEUE_ENABLE; 327 txdctl |= IGC_TXDCTL_SWFLUSH; 328 wr32(IGC_TXDCTL(idx), txdctl); 329 } 330 331 /** 332 * igc_disable_all_tx_rings_hw - Disable all transmit queue operation 333 * @adapter: board private structure 334 */ 335 static void igc_disable_all_tx_rings_hw(struct igc_adapter *adapter) 336 { 337 int i; 338 339 for (i = 0; i < adapter->num_tx_queues; i++) { 340 struct igc_ring *tx_ring = adapter->tx_ring[i]; 341 342 igc_disable_tx_ring_hw(tx_ring); 343 } 344 } 345 346 /** 347 * igc_setup_tx_resources - allocate Tx resources (Descriptors) 348 * @tx_ring: tx descriptor ring (for a specific queue) to setup 349 * 350 * Return 0 on success, negative on failure 351 */ 352 int igc_setup_tx_resources(struct igc_ring *tx_ring) 353 { 354 struct net_device *ndev = tx_ring->netdev; 355 struct device *dev = tx_ring->dev; 356 int size = 0; 357 358 size = sizeof(struct igc_tx_buffer) * tx_ring->count; 359 tx_ring->tx_buffer_info = vzalloc(size); 360 if (!tx_ring->tx_buffer_info) 361 goto err; 362 363 /* round up to nearest 4K */ 364 tx_ring->size = tx_ring->count * sizeof(union igc_adv_tx_desc); 365 tx_ring->size = ALIGN(tx_ring->size, 4096); 366 367 tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size, 368 &tx_ring->dma, GFP_KERNEL); 369 370 if (!tx_ring->desc) 371 goto err; 372 373 tx_ring->next_to_use = 0; 374 tx_ring->next_to_clean = 0; 375 376 return 0; 377 378 err: 379 vfree(tx_ring->tx_buffer_info); 380 netdev_err(ndev, "Unable to allocate memory for Tx descriptor ring\n"); 381 return -ENOMEM; 382 } 383 384 /** 385 * igc_setup_all_tx_resources - wrapper to allocate Tx resources for all queues 386 * @adapter: board private structure 387 * 388 * Return 0 on success, negative on failure 389 */ 390 static int igc_setup_all_tx_resources(struct igc_adapter *adapter) 391 { 392 struct net_device *dev = adapter->netdev; 393 int i, err = 0; 394 395 for (i = 0; i < adapter->num_tx_queues; i++) { 396 err = igc_setup_tx_resources(adapter->tx_ring[i]); 397 if (err) { 398 netdev_err(dev, "Error on Tx queue %u setup\n", i); 399 for (i--; i >= 0; i--) 400 igc_free_tx_resources(adapter->tx_ring[i]); 401 break; 402 } 403 } 404 405 return err; 406 } 407 408 static void igc_clean_rx_ring_page_shared(struct igc_ring *rx_ring) 409 { 410 u16 i = rx_ring->next_to_clean; 411 412 dev_kfree_skb(rx_ring->skb); 413 rx_ring->skb = NULL; 414 415 /* Free all the Rx ring sk_buffs */ 416 while (i != rx_ring->next_to_alloc) { 417 struct igc_rx_buffer *buffer_info = &rx_ring->rx_buffer_info[i]; 418 419 /* Invalidate cache lines that may have been written to by 420 * device so that we avoid corrupting memory. 421 */ 422 dma_sync_single_range_for_cpu(rx_ring->dev, 423 buffer_info->dma, 424 buffer_info->page_offset, 425 igc_rx_bufsz(rx_ring), 426 DMA_FROM_DEVICE); 427 428 /* free resources associated with mapping */ 429 dma_unmap_page_attrs(rx_ring->dev, 430 buffer_info->dma, 431 igc_rx_pg_size(rx_ring), 432 DMA_FROM_DEVICE, 433 IGC_RX_DMA_ATTR); 434 __page_frag_cache_drain(buffer_info->page, 435 buffer_info->pagecnt_bias); 436 437 i++; 438 if (i == rx_ring->count) 439 i = 0; 440 } 441 } 442 443 static void igc_clean_rx_ring_xsk_pool(struct igc_ring *ring) 444 { 445 struct igc_rx_buffer *bi; 446 u16 i; 447 448 for (i = 0; i < ring->count; i++) { 449 bi = &ring->rx_buffer_info[i]; 450 if (!bi->xdp) 451 continue; 452 453 xsk_buff_free(bi->xdp); 454 bi->xdp = NULL; 455 } 456 } 457 458 /** 459 * igc_clean_rx_ring - Free Rx Buffers per Queue 460 * @ring: ring to free buffers from 461 */ 462 static void igc_clean_rx_ring(struct igc_ring *ring) 463 { 464 if (ring->xsk_pool) 465 igc_clean_rx_ring_xsk_pool(ring); 466 else 467 igc_clean_rx_ring_page_shared(ring); 468 469 clear_ring_uses_large_buffer(ring); 470 471 ring->next_to_alloc = 0; 472 ring->next_to_clean = 0; 473 ring->next_to_use = 0; 474 } 475 476 /** 477 * igc_clean_all_rx_rings - Free Rx Buffers for all queues 478 * @adapter: board private structure 479 */ 480 static void igc_clean_all_rx_rings(struct igc_adapter *adapter) 481 { 482 int i; 483 484 for (i = 0; i < adapter->num_rx_queues; i++) 485 if (adapter->rx_ring[i]) 486 igc_clean_rx_ring(adapter->rx_ring[i]); 487 } 488 489 /** 490 * igc_free_rx_resources - Free Rx Resources 491 * @rx_ring: ring to clean the resources from 492 * 493 * Free all receive software resources 494 */ 495 void igc_free_rx_resources(struct igc_ring *rx_ring) 496 { 497 igc_clean_rx_ring(rx_ring); 498 499 xdp_rxq_info_unreg(&rx_ring->xdp_rxq); 500 501 vfree(rx_ring->rx_buffer_info); 502 rx_ring->rx_buffer_info = NULL; 503 504 /* if not set, then don't free */ 505 if (!rx_ring->desc) 506 return; 507 508 dma_free_coherent(rx_ring->dev, rx_ring->size, 509 rx_ring->desc, rx_ring->dma); 510 511 rx_ring->desc = NULL; 512 } 513 514 /** 515 * igc_free_all_rx_resources - Free Rx Resources for All Queues 516 * @adapter: board private structure 517 * 518 * Free all receive software resources 519 */ 520 static void igc_free_all_rx_resources(struct igc_adapter *adapter) 521 { 522 int i; 523 524 for (i = 0; i < adapter->num_rx_queues; i++) 525 igc_free_rx_resources(adapter->rx_ring[i]); 526 } 527 528 /** 529 * igc_setup_rx_resources - allocate Rx resources (Descriptors) 530 * @rx_ring: rx descriptor ring (for a specific queue) to setup 531 * 532 * Returns 0 on success, negative on failure 533 */ 534 int igc_setup_rx_resources(struct igc_ring *rx_ring) 535 { 536 struct net_device *ndev = rx_ring->netdev; 537 struct device *dev = rx_ring->dev; 538 u8 index = rx_ring->queue_index; 539 int size, desc_len, res; 540 541 /* XDP RX-queue info */ 542 if (xdp_rxq_info_is_reg(&rx_ring->xdp_rxq)) 543 xdp_rxq_info_unreg(&rx_ring->xdp_rxq); 544 res = xdp_rxq_info_reg(&rx_ring->xdp_rxq, ndev, index, 545 rx_ring->q_vector->napi.napi_id); 546 if (res < 0) { 547 netdev_err(ndev, "Failed to register xdp_rxq index %u\n", 548 index); 549 return res; 550 } 551 552 size = sizeof(struct igc_rx_buffer) * rx_ring->count; 553 rx_ring->rx_buffer_info = vzalloc(size); 554 if (!rx_ring->rx_buffer_info) 555 goto err; 556 557 desc_len = sizeof(union igc_adv_rx_desc); 558 559 /* Round up to nearest 4K */ 560 rx_ring->size = rx_ring->count * desc_len; 561 rx_ring->size = ALIGN(rx_ring->size, 4096); 562 563 rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size, 564 &rx_ring->dma, GFP_KERNEL); 565 566 if (!rx_ring->desc) 567 goto err; 568 569 rx_ring->next_to_alloc = 0; 570 rx_ring->next_to_clean = 0; 571 rx_ring->next_to_use = 0; 572 573 return 0; 574 575 err: 576 xdp_rxq_info_unreg(&rx_ring->xdp_rxq); 577 vfree(rx_ring->rx_buffer_info); 578 rx_ring->rx_buffer_info = NULL; 579 netdev_err(ndev, "Unable to allocate memory for Rx descriptor ring\n"); 580 return -ENOMEM; 581 } 582 583 /** 584 * igc_setup_all_rx_resources - wrapper to allocate Rx resources 585 * (Descriptors) for all queues 586 * @adapter: board private structure 587 * 588 * Return 0 on success, negative on failure 589 */ 590 static int igc_setup_all_rx_resources(struct igc_adapter *adapter) 591 { 592 struct net_device *dev = adapter->netdev; 593 int i, err = 0; 594 595 for (i = 0; i < adapter->num_rx_queues; i++) { 596 err = igc_setup_rx_resources(adapter->rx_ring[i]); 597 if (err) { 598 netdev_err(dev, "Error on Rx queue %u setup\n", i); 599 for (i--; i >= 0; i--) 600 igc_free_rx_resources(adapter->rx_ring[i]); 601 break; 602 } 603 } 604 605 return err; 606 } 607 608 static struct xsk_buff_pool *igc_get_xsk_pool(struct igc_adapter *adapter, 609 struct igc_ring *ring) 610 { 611 if (!igc_xdp_is_enabled(adapter) || 612 !test_bit(IGC_RING_FLAG_AF_XDP_ZC, &ring->flags)) 613 return NULL; 614 615 return xsk_get_pool_from_qid(ring->netdev, ring->queue_index); 616 } 617 618 /** 619 * igc_configure_rx_ring - Configure a receive ring after Reset 620 * @adapter: board private structure 621 * @ring: receive ring to be configured 622 * 623 * Configure the Rx unit of the MAC after a reset. 624 */ 625 static void igc_configure_rx_ring(struct igc_adapter *adapter, 626 struct igc_ring *ring) 627 { 628 struct igc_hw *hw = &adapter->hw; 629 union igc_adv_rx_desc *rx_desc; 630 int reg_idx = ring->reg_idx; 631 u32 srrctl = 0, rxdctl = 0; 632 u64 rdba = ring->dma; 633 u32 buf_size; 634 635 xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq); 636 ring->xsk_pool = igc_get_xsk_pool(adapter, ring); 637 if (ring->xsk_pool) { 638 WARN_ON(xdp_rxq_info_reg_mem_model(&ring->xdp_rxq, 639 MEM_TYPE_XSK_BUFF_POOL, 640 NULL)); 641 xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq); 642 } else { 643 WARN_ON(xdp_rxq_info_reg_mem_model(&ring->xdp_rxq, 644 MEM_TYPE_PAGE_SHARED, 645 NULL)); 646 } 647 648 if (igc_xdp_is_enabled(adapter)) 649 set_ring_uses_large_buffer(ring); 650 651 /* disable the queue */ 652 wr32(IGC_RXDCTL(reg_idx), 0); 653 654 /* Set DMA base address registers */ 655 wr32(IGC_RDBAL(reg_idx), 656 rdba & 0x00000000ffffffffULL); 657 wr32(IGC_RDBAH(reg_idx), rdba >> 32); 658 wr32(IGC_RDLEN(reg_idx), 659 ring->count * sizeof(union igc_adv_rx_desc)); 660 661 /* initialize head and tail */ 662 ring->tail = adapter->io_addr + IGC_RDT(reg_idx); 663 wr32(IGC_RDH(reg_idx), 0); 664 writel(0, ring->tail); 665 666 /* reset next-to- use/clean to place SW in sync with hardware */ 667 ring->next_to_clean = 0; 668 ring->next_to_use = 0; 669 670 if (ring->xsk_pool) 671 buf_size = xsk_pool_get_rx_frame_size(ring->xsk_pool); 672 else if (ring_uses_large_buffer(ring)) 673 buf_size = IGC_RXBUFFER_3072; 674 else 675 buf_size = IGC_RXBUFFER_2048; 676 677 srrctl = rd32(IGC_SRRCTL(reg_idx)); 678 srrctl &= ~(IGC_SRRCTL_BSIZEPKT_MASK | IGC_SRRCTL_BSIZEHDR_MASK | 679 IGC_SRRCTL_DESCTYPE_MASK); 680 srrctl |= IGC_SRRCTL_BSIZEHDR(IGC_RX_HDR_LEN); 681 srrctl |= IGC_SRRCTL_BSIZEPKT(buf_size); 682 srrctl |= IGC_SRRCTL_DESCTYPE_ADV_ONEBUF; 683 684 wr32(IGC_SRRCTL(reg_idx), srrctl); 685 686 rxdctl |= IGC_RX_PTHRESH; 687 rxdctl |= IGC_RX_HTHRESH << 8; 688 rxdctl |= IGC_RX_WTHRESH << 16; 689 690 /* initialize rx_buffer_info */ 691 memset(ring->rx_buffer_info, 0, 692 sizeof(struct igc_rx_buffer) * ring->count); 693 694 /* initialize Rx descriptor 0 */ 695 rx_desc = IGC_RX_DESC(ring, 0); 696 rx_desc->wb.upper.length = 0; 697 698 /* enable receive descriptor fetching */ 699 rxdctl |= IGC_RXDCTL_QUEUE_ENABLE; 700 701 wr32(IGC_RXDCTL(reg_idx), rxdctl); 702 } 703 704 /** 705 * igc_configure_rx - Configure receive Unit after Reset 706 * @adapter: board private structure 707 * 708 * Configure the Rx unit of the MAC after a reset. 709 */ 710 static void igc_configure_rx(struct igc_adapter *adapter) 711 { 712 int i; 713 714 /* Setup the HW Rx Head and Tail Descriptor Pointers and 715 * the Base and Length of the Rx Descriptor Ring 716 */ 717 for (i = 0; i < adapter->num_rx_queues; i++) 718 igc_configure_rx_ring(adapter, adapter->rx_ring[i]); 719 } 720 721 /** 722 * igc_configure_tx_ring - Configure transmit ring after Reset 723 * @adapter: board private structure 724 * @ring: tx ring to configure 725 * 726 * Configure a transmit ring after a reset. 727 */ 728 static void igc_configure_tx_ring(struct igc_adapter *adapter, 729 struct igc_ring *ring) 730 { 731 struct igc_hw *hw = &adapter->hw; 732 int reg_idx = ring->reg_idx; 733 u64 tdba = ring->dma; 734 u32 txdctl = 0; 735 736 ring->xsk_pool = igc_get_xsk_pool(adapter, ring); 737 738 /* disable the queue */ 739 wr32(IGC_TXDCTL(reg_idx), 0); 740 wrfl(); 741 742 wr32(IGC_TDLEN(reg_idx), 743 ring->count * sizeof(union igc_adv_tx_desc)); 744 wr32(IGC_TDBAL(reg_idx), 745 tdba & 0x00000000ffffffffULL); 746 wr32(IGC_TDBAH(reg_idx), tdba >> 32); 747 748 ring->tail = adapter->io_addr + IGC_TDT(reg_idx); 749 wr32(IGC_TDH(reg_idx), 0); 750 writel(0, ring->tail); 751 752 txdctl |= IGC_TX_PTHRESH; 753 txdctl |= IGC_TX_HTHRESH << 8; 754 txdctl |= IGC_TX_WTHRESH << 16; 755 756 txdctl |= IGC_TXDCTL_QUEUE_ENABLE; 757 wr32(IGC_TXDCTL(reg_idx), txdctl); 758 } 759 760 /** 761 * igc_configure_tx - Configure transmit Unit after Reset 762 * @adapter: board private structure 763 * 764 * Configure the Tx unit of the MAC after a reset. 765 */ 766 static void igc_configure_tx(struct igc_adapter *adapter) 767 { 768 int i; 769 770 for (i = 0; i < adapter->num_tx_queues; i++) 771 igc_configure_tx_ring(adapter, adapter->tx_ring[i]); 772 } 773 774 /** 775 * igc_setup_mrqc - configure the multiple receive queue control registers 776 * @adapter: Board private structure 777 */ 778 static void igc_setup_mrqc(struct igc_adapter *adapter) 779 { 780 struct igc_hw *hw = &adapter->hw; 781 u32 j, num_rx_queues; 782 u32 mrqc, rxcsum; 783 u32 rss_key[10]; 784 785 netdev_rss_key_fill(rss_key, sizeof(rss_key)); 786 for (j = 0; j < 10; j++) 787 wr32(IGC_RSSRK(j), rss_key[j]); 788 789 num_rx_queues = adapter->rss_queues; 790 791 if (adapter->rss_indir_tbl_init != num_rx_queues) { 792 for (j = 0; j < IGC_RETA_SIZE; j++) 793 adapter->rss_indir_tbl[j] = 794 (j * num_rx_queues) / IGC_RETA_SIZE; 795 adapter->rss_indir_tbl_init = num_rx_queues; 796 } 797 igc_write_rss_indir_tbl(adapter); 798 799 /* Disable raw packet checksumming so that RSS hash is placed in 800 * descriptor on writeback. No need to enable TCP/UDP/IP checksum 801 * offloads as they are enabled by default 802 */ 803 rxcsum = rd32(IGC_RXCSUM); 804 rxcsum |= IGC_RXCSUM_PCSD; 805 806 /* Enable Receive Checksum Offload for SCTP */ 807 rxcsum |= IGC_RXCSUM_CRCOFL; 808 809 /* Don't need to set TUOFL or IPOFL, they default to 1 */ 810 wr32(IGC_RXCSUM, rxcsum); 811 812 /* Generate RSS hash based on packet types, TCP/UDP 813 * port numbers and/or IPv4/v6 src and dst addresses 814 */ 815 mrqc = IGC_MRQC_RSS_FIELD_IPV4 | 816 IGC_MRQC_RSS_FIELD_IPV4_TCP | 817 IGC_MRQC_RSS_FIELD_IPV6 | 818 IGC_MRQC_RSS_FIELD_IPV6_TCP | 819 IGC_MRQC_RSS_FIELD_IPV6_TCP_EX; 820 821 if (adapter->flags & IGC_FLAG_RSS_FIELD_IPV4_UDP) 822 mrqc |= IGC_MRQC_RSS_FIELD_IPV4_UDP; 823 if (adapter->flags & IGC_FLAG_RSS_FIELD_IPV6_UDP) 824 mrqc |= IGC_MRQC_RSS_FIELD_IPV6_UDP; 825 826 mrqc |= IGC_MRQC_ENABLE_RSS_MQ; 827 828 wr32(IGC_MRQC, mrqc); 829 } 830 831 /** 832 * igc_setup_rctl - configure the receive control registers 833 * @adapter: Board private structure 834 */ 835 static void igc_setup_rctl(struct igc_adapter *adapter) 836 { 837 struct igc_hw *hw = &adapter->hw; 838 u32 rctl; 839 840 rctl = rd32(IGC_RCTL); 841 842 rctl &= ~(3 << IGC_RCTL_MO_SHIFT); 843 rctl &= ~(IGC_RCTL_LBM_TCVR | IGC_RCTL_LBM_MAC); 844 845 rctl |= IGC_RCTL_EN | IGC_RCTL_BAM | IGC_RCTL_RDMTS_HALF | 846 (hw->mac.mc_filter_type << IGC_RCTL_MO_SHIFT); 847 848 /* enable stripping of CRC. Newer features require 849 * that the HW strips the CRC. 850 */ 851 rctl |= IGC_RCTL_SECRC; 852 853 /* disable store bad packets and clear size bits. */ 854 rctl &= ~(IGC_RCTL_SBP | IGC_RCTL_SZ_256); 855 856 /* enable LPE to allow for reception of jumbo frames */ 857 rctl |= IGC_RCTL_LPE; 858 859 /* disable queue 0 to prevent tail write w/o re-config */ 860 wr32(IGC_RXDCTL(0), 0); 861 862 /* This is useful for sniffing bad packets. */ 863 if (adapter->netdev->features & NETIF_F_RXALL) { 864 /* UPE and MPE will be handled by normal PROMISC logic 865 * in set_rx_mode 866 */ 867 rctl |= (IGC_RCTL_SBP | /* Receive bad packets */ 868 IGC_RCTL_BAM | /* RX All Bcast Pkts */ 869 IGC_RCTL_PMCF); /* RX All MAC Ctrl Pkts */ 870 871 rctl &= ~(IGC_RCTL_DPF | /* Allow filtered pause */ 872 IGC_RCTL_CFIEN); /* Disable VLAN CFIEN Filter */ 873 } 874 875 wr32(IGC_RCTL, rctl); 876 } 877 878 /** 879 * igc_setup_tctl - configure the transmit control registers 880 * @adapter: Board private structure 881 */ 882 static void igc_setup_tctl(struct igc_adapter *adapter) 883 { 884 struct igc_hw *hw = &adapter->hw; 885 u32 tctl; 886 887 /* disable queue 0 which icould be enabled by default */ 888 wr32(IGC_TXDCTL(0), 0); 889 890 /* Program the Transmit Control Register */ 891 tctl = rd32(IGC_TCTL); 892 tctl &= ~IGC_TCTL_CT; 893 tctl |= IGC_TCTL_PSP | IGC_TCTL_RTLC | 894 (IGC_COLLISION_THRESHOLD << IGC_CT_SHIFT); 895 896 /* Enable transmits */ 897 tctl |= IGC_TCTL_EN; 898 899 wr32(IGC_TCTL, tctl); 900 } 901 902 /** 903 * igc_set_mac_filter_hw() - Set MAC address filter in hardware 904 * @adapter: Pointer to adapter where the filter should be set 905 * @index: Filter index 906 * @type: MAC address filter type (source or destination) 907 * @addr: MAC address 908 * @queue: If non-negative, queue assignment feature is enabled and frames 909 * matching the filter are enqueued onto 'queue'. Otherwise, queue 910 * assignment is disabled. 911 */ 912 static void igc_set_mac_filter_hw(struct igc_adapter *adapter, int index, 913 enum igc_mac_filter_type type, 914 const u8 *addr, int queue) 915 { 916 struct net_device *dev = adapter->netdev; 917 struct igc_hw *hw = &adapter->hw; 918 u32 ral, rah; 919 920 if (WARN_ON(index >= hw->mac.rar_entry_count)) 921 return; 922 923 ral = le32_to_cpup((__le32 *)(addr)); 924 rah = le16_to_cpup((__le16 *)(addr + 4)); 925 926 if (type == IGC_MAC_FILTER_TYPE_SRC) { 927 rah &= ~IGC_RAH_ASEL_MASK; 928 rah |= IGC_RAH_ASEL_SRC_ADDR; 929 } 930 931 if (queue >= 0) { 932 rah &= ~IGC_RAH_QSEL_MASK; 933 rah |= (queue << IGC_RAH_QSEL_SHIFT); 934 rah |= IGC_RAH_QSEL_ENABLE; 935 } 936 937 rah |= IGC_RAH_AV; 938 939 wr32(IGC_RAL(index), ral); 940 wr32(IGC_RAH(index), rah); 941 942 netdev_dbg(dev, "MAC address filter set in HW: index %d", index); 943 } 944 945 /** 946 * igc_clear_mac_filter_hw() - Clear MAC address filter in hardware 947 * @adapter: Pointer to adapter where the filter should be cleared 948 * @index: Filter index 949 */ 950 static void igc_clear_mac_filter_hw(struct igc_adapter *adapter, int index) 951 { 952 struct net_device *dev = adapter->netdev; 953 struct igc_hw *hw = &adapter->hw; 954 955 if (WARN_ON(index >= hw->mac.rar_entry_count)) 956 return; 957 958 wr32(IGC_RAL(index), 0); 959 wr32(IGC_RAH(index), 0); 960 961 netdev_dbg(dev, "MAC address filter cleared in HW: index %d", index); 962 } 963 964 /* Set default MAC address for the PF in the first RAR entry */ 965 static void igc_set_default_mac_filter(struct igc_adapter *adapter) 966 { 967 struct net_device *dev = adapter->netdev; 968 u8 *addr = adapter->hw.mac.addr; 969 970 netdev_dbg(dev, "Set default MAC address filter: address %pM", addr); 971 972 igc_set_mac_filter_hw(adapter, 0, IGC_MAC_FILTER_TYPE_DST, addr, -1); 973 } 974 975 /** 976 * igc_set_mac - Change the Ethernet Address of the NIC 977 * @netdev: network interface device structure 978 * @p: pointer to an address structure 979 * 980 * Returns 0 on success, negative on failure 981 */ 982 static int igc_set_mac(struct net_device *netdev, void *p) 983 { 984 struct igc_adapter *adapter = netdev_priv(netdev); 985 struct igc_hw *hw = &adapter->hw; 986 struct sockaddr *addr = p; 987 988 if (!is_valid_ether_addr(addr->sa_data)) 989 return -EADDRNOTAVAIL; 990 991 eth_hw_addr_set(netdev, addr->sa_data); 992 memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len); 993 994 /* set the correct pool for the new PF MAC address in entry 0 */ 995 igc_set_default_mac_filter(adapter); 996 997 return 0; 998 } 999 1000 /** 1001 * igc_write_mc_addr_list - write multicast addresses to MTA 1002 * @netdev: network interface device structure 1003 * 1004 * Writes multicast address list to the MTA hash table. 1005 * Returns: -ENOMEM on failure 1006 * 0 on no addresses written 1007 * X on writing X addresses to MTA 1008 **/ 1009 static int igc_write_mc_addr_list(struct net_device *netdev) 1010 { 1011 struct igc_adapter *adapter = netdev_priv(netdev); 1012 struct igc_hw *hw = &adapter->hw; 1013 struct netdev_hw_addr *ha; 1014 u8 *mta_list; 1015 int i; 1016 1017 if (netdev_mc_empty(netdev)) { 1018 /* nothing to program, so clear mc list */ 1019 igc_update_mc_addr_list(hw, NULL, 0); 1020 return 0; 1021 } 1022 1023 mta_list = kcalloc(netdev_mc_count(netdev), 6, GFP_ATOMIC); 1024 if (!mta_list) 1025 return -ENOMEM; 1026 1027 /* The shared function expects a packed array of only addresses. */ 1028 i = 0; 1029 netdev_for_each_mc_addr(ha, netdev) 1030 memcpy(mta_list + (i++ * ETH_ALEN), ha->addr, ETH_ALEN); 1031 1032 igc_update_mc_addr_list(hw, mta_list, i); 1033 kfree(mta_list); 1034 1035 return netdev_mc_count(netdev); 1036 } 1037 1038 static __le32 igc_tx_launchtime(struct igc_ring *ring, ktime_t txtime, 1039 bool *first_flag, bool *insert_empty) 1040 { 1041 struct igc_adapter *adapter = netdev_priv(ring->netdev); 1042 ktime_t cycle_time = adapter->cycle_time; 1043 ktime_t base_time = adapter->base_time; 1044 ktime_t now = ktime_get_clocktai(); 1045 ktime_t baset_est, end_of_cycle; 1046 s32 launchtime; 1047 s64 n; 1048 1049 n = div64_s64(ktime_sub_ns(now, base_time), cycle_time); 1050 1051 baset_est = ktime_add_ns(base_time, cycle_time * (n)); 1052 end_of_cycle = ktime_add_ns(baset_est, cycle_time); 1053 1054 if (ktime_compare(txtime, end_of_cycle) >= 0) { 1055 if (baset_est != ring->last_ff_cycle) { 1056 *first_flag = true; 1057 ring->last_ff_cycle = baset_est; 1058 1059 if (ktime_compare(end_of_cycle, ring->last_tx_cycle) > 0) 1060 *insert_empty = true; 1061 } 1062 } 1063 1064 /* Introducing a window at end of cycle on which packets 1065 * potentially not honor launchtime. Window of 5us chosen 1066 * considering software update the tail pointer and packets 1067 * are dma'ed to packet buffer. 1068 */ 1069 if ((ktime_sub_ns(end_of_cycle, now) < 5 * NSEC_PER_USEC)) 1070 netdev_warn(ring->netdev, "Packet with txtime=%llu may not be honoured\n", 1071 txtime); 1072 1073 ring->last_tx_cycle = end_of_cycle; 1074 1075 launchtime = ktime_sub_ns(txtime, baset_est); 1076 if (launchtime > 0) 1077 div_s64_rem(launchtime, cycle_time, &launchtime); 1078 else 1079 launchtime = 0; 1080 1081 return cpu_to_le32(launchtime); 1082 } 1083 1084 static int igc_init_empty_frame(struct igc_ring *ring, 1085 struct igc_tx_buffer *buffer, 1086 struct sk_buff *skb) 1087 { 1088 unsigned int size; 1089 dma_addr_t dma; 1090 1091 size = skb_headlen(skb); 1092 1093 dma = dma_map_single(ring->dev, skb->data, size, DMA_TO_DEVICE); 1094 if (dma_mapping_error(ring->dev, dma)) { 1095 netdev_err_once(ring->netdev, "Failed to map DMA for TX\n"); 1096 return -ENOMEM; 1097 } 1098 1099 buffer->type = IGC_TX_BUFFER_TYPE_SKB; 1100 buffer->skb = skb; 1101 buffer->protocol = 0; 1102 buffer->bytecount = skb->len; 1103 buffer->gso_segs = 1; 1104 buffer->time_stamp = jiffies; 1105 dma_unmap_len_set(buffer, len, skb->len); 1106 dma_unmap_addr_set(buffer, dma, dma); 1107 1108 return 0; 1109 } 1110 1111 static int igc_init_tx_empty_descriptor(struct igc_ring *ring, 1112 struct sk_buff *skb, 1113 struct igc_tx_buffer *first) 1114 { 1115 union igc_adv_tx_desc *desc; 1116 u32 cmd_type, olinfo_status; 1117 int err; 1118 1119 if (!igc_desc_unused(ring)) 1120 return -EBUSY; 1121 1122 err = igc_init_empty_frame(ring, first, skb); 1123 if (err) 1124 return err; 1125 1126 cmd_type = IGC_ADVTXD_DTYP_DATA | IGC_ADVTXD_DCMD_DEXT | 1127 IGC_ADVTXD_DCMD_IFCS | IGC_TXD_DCMD | 1128 first->bytecount; 1129 olinfo_status = first->bytecount << IGC_ADVTXD_PAYLEN_SHIFT; 1130 1131 desc = IGC_TX_DESC(ring, ring->next_to_use); 1132 desc->read.cmd_type_len = cpu_to_le32(cmd_type); 1133 desc->read.olinfo_status = cpu_to_le32(olinfo_status); 1134 desc->read.buffer_addr = cpu_to_le64(dma_unmap_addr(first, dma)); 1135 1136 netdev_tx_sent_queue(txring_txq(ring), skb->len); 1137 1138 first->next_to_watch = desc; 1139 1140 ring->next_to_use++; 1141 if (ring->next_to_use == ring->count) 1142 ring->next_to_use = 0; 1143 1144 return 0; 1145 } 1146 1147 #define IGC_EMPTY_FRAME_SIZE 60 1148 1149 static void igc_tx_ctxtdesc(struct igc_ring *tx_ring, 1150 __le32 launch_time, bool first_flag, 1151 u32 vlan_macip_lens, u32 type_tucmd, 1152 u32 mss_l4len_idx) 1153 { 1154 struct igc_adv_tx_context_desc *context_desc; 1155 u16 i = tx_ring->next_to_use; 1156 1157 context_desc = IGC_TX_CTXTDESC(tx_ring, i); 1158 1159 i++; 1160 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0; 1161 1162 /* set bits to identify this as an advanced context descriptor */ 1163 type_tucmd |= IGC_TXD_CMD_DEXT | IGC_ADVTXD_DTYP_CTXT; 1164 1165 /* For i225, context index must be unique per ring. */ 1166 if (test_bit(IGC_RING_FLAG_TX_CTX_IDX, &tx_ring->flags)) 1167 mss_l4len_idx |= tx_ring->reg_idx << 4; 1168 1169 if (first_flag) 1170 mss_l4len_idx |= IGC_ADVTXD_TSN_CNTX_FIRST; 1171 1172 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens); 1173 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd); 1174 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx); 1175 context_desc->launch_time = launch_time; 1176 } 1177 1178 static void igc_tx_csum(struct igc_ring *tx_ring, struct igc_tx_buffer *first, 1179 __le32 launch_time, bool first_flag) 1180 { 1181 struct sk_buff *skb = first->skb; 1182 u32 vlan_macip_lens = 0; 1183 u32 type_tucmd = 0; 1184 1185 if (skb->ip_summed != CHECKSUM_PARTIAL) { 1186 csum_failed: 1187 if (!(first->tx_flags & IGC_TX_FLAGS_VLAN) && 1188 !tx_ring->launchtime_enable) 1189 return; 1190 goto no_csum; 1191 } 1192 1193 switch (skb->csum_offset) { 1194 case offsetof(struct tcphdr, check): 1195 type_tucmd = IGC_ADVTXD_TUCMD_L4T_TCP; 1196 fallthrough; 1197 case offsetof(struct udphdr, check): 1198 break; 1199 case offsetof(struct sctphdr, checksum): 1200 /* validate that this is actually an SCTP request */ 1201 if (skb_csum_is_sctp(skb)) { 1202 type_tucmd = IGC_ADVTXD_TUCMD_L4T_SCTP; 1203 break; 1204 } 1205 fallthrough; 1206 default: 1207 skb_checksum_help(skb); 1208 goto csum_failed; 1209 } 1210 1211 /* update TX checksum flag */ 1212 first->tx_flags |= IGC_TX_FLAGS_CSUM; 1213 vlan_macip_lens = skb_checksum_start_offset(skb) - 1214 skb_network_offset(skb); 1215 no_csum: 1216 vlan_macip_lens |= skb_network_offset(skb) << IGC_ADVTXD_MACLEN_SHIFT; 1217 vlan_macip_lens |= first->tx_flags & IGC_TX_FLAGS_VLAN_MASK; 1218 1219 igc_tx_ctxtdesc(tx_ring, launch_time, first_flag, 1220 vlan_macip_lens, type_tucmd, 0); 1221 } 1222 1223 static int __igc_maybe_stop_tx(struct igc_ring *tx_ring, const u16 size) 1224 { 1225 struct net_device *netdev = tx_ring->netdev; 1226 1227 netif_stop_subqueue(netdev, tx_ring->queue_index); 1228 1229 /* memory barriier comment */ 1230 smp_mb(); 1231 1232 /* We need to check again in a case another CPU has just 1233 * made room available. 1234 */ 1235 if (igc_desc_unused(tx_ring) < size) 1236 return -EBUSY; 1237 1238 /* A reprieve! */ 1239 netif_wake_subqueue(netdev, tx_ring->queue_index); 1240 1241 u64_stats_update_begin(&tx_ring->tx_syncp2); 1242 tx_ring->tx_stats.restart_queue2++; 1243 u64_stats_update_end(&tx_ring->tx_syncp2); 1244 1245 return 0; 1246 } 1247 1248 static inline int igc_maybe_stop_tx(struct igc_ring *tx_ring, const u16 size) 1249 { 1250 if (igc_desc_unused(tx_ring) >= size) 1251 return 0; 1252 return __igc_maybe_stop_tx(tx_ring, size); 1253 } 1254 1255 #define IGC_SET_FLAG(_input, _flag, _result) \ 1256 (((_flag) <= (_result)) ? \ 1257 ((u32)((_input) & (_flag)) * ((_result) / (_flag))) : \ 1258 ((u32)((_input) & (_flag)) / ((_flag) / (_result)))) 1259 1260 static u32 igc_tx_cmd_type(struct sk_buff *skb, u32 tx_flags) 1261 { 1262 /* set type for advanced descriptor with frame checksum insertion */ 1263 u32 cmd_type = IGC_ADVTXD_DTYP_DATA | 1264 IGC_ADVTXD_DCMD_DEXT | 1265 IGC_ADVTXD_DCMD_IFCS; 1266 1267 /* set HW vlan bit if vlan is present */ 1268 cmd_type |= IGC_SET_FLAG(tx_flags, IGC_TX_FLAGS_VLAN, 1269 IGC_ADVTXD_DCMD_VLE); 1270 1271 /* set segmentation bits for TSO */ 1272 cmd_type |= IGC_SET_FLAG(tx_flags, IGC_TX_FLAGS_TSO, 1273 (IGC_ADVTXD_DCMD_TSE)); 1274 1275 /* set timestamp bit if present, will select the register set 1276 * based on the _TSTAMP(_X) bit. 1277 */ 1278 cmd_type |= IGC_SET_FLAG(tx_flags, IGC_TX_FLAGS_TSTAMP, 1279 (IGC_ADVTXD_MAC_TSTAMP)); 1280 1281 cmd_type |= IGC_SET_FLAG(tx_flags, IGC_TX_FLAGS_TSTAMP_1, 1282 (IGC_ADVTXD_TSTAMP_REG_1)); 1283 1284 cmd_type |= IGC_SET_FLAG(tx_flags, IGC_TX_FLAGS_TSTAMP_2, 1285 (IGC_ADVTXD_TSTAMP_REG_2)); 1286 1287 cmd_type |= IGC_SET_FLAG(tx_flags, IGC_TX_FLAGS_TSTAMP_3, 1288 (IGC_ADVTXD_TSTAMP_REG_3)); 1289 1290 /* insert frame checksum */ 1291 cmd_type ^= IGC_SET_FLAG(skb->no_fcs, 1, IGC_ADVTXD_DCMD_IFCS); 1292 1293 return cmd_type; 1294 } 1295 1296 static void igc_tx_olinfo_status(struct igc_ring *tx_ring, 1297 union igc_adv_tx_desc *tx_desc, 1298 u32 tx_flags, unsigned int paylen) 1299 { 1300 u32 olinfo_status = paylen << IGC_ADVTXD_PAYLEN_SHIFT; 1301 1302 /* insert L4 checksum */ 1303 olinfo_status |= IGC_SET_FLAG(tx_flags, IGC_TX_FLAGS_CSUM, 1304 (IGC_TXD_POPTS_TXSM << 8)); 1305 1306 /* insert IPv4 checksum */ 1307 olinfo_status |= IGC_SET_FLAG(tx_flags, IGC_TX_FLAGS_IPV4, 1308 (IGC_TXD_POPTS_IXSM << 8)); 1309 1310 /* Use the second timer (free running, in general) for the timestamp */ 1311 olinfo_status |= IGC_SET_FLAG(tx_flags, IGC_TX_FLAGS_TSTAMP_TIMER_1, 1312 IGC_TXD_PTP2_TIMER_1); 1313 1314 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status); 1315 } 1316 1317 static int igc_tx_map(struct igc_ring *tx_ring, 1318 struct igc_tx_buffer *first, 1319 const u8 hdr_len) 1320 { 1321 struct sk_buff *skb = first->skb; 1322 struct igc_tx_buffer *tx_buffer; 1323 union igc_adv_tx_desc *tx_desc; 1324 u32 tx_flags = first->tx_flags; 1325 skb_frag_t *frag; 1326 u16 i = tx_ring->next_to_use; 1327 unsigned int data_len, size; 1328 dma_addr_t dma; 1329 u32 cmd_type; 1330 1331 cmd_type = igc_tx_cmd_type(skb, tx_flags); 1332 tx_desc = IGC_TX_DESC(tx_ring, i); 1333 1334 igc_tx_olinfo_status(tx_ring, tx_desc, tx_flags, skb->len - hdr_len); 1335 1336 size = skb_headlen(skb); 1337 data_len = skb->data_len; 1338 1339 dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE); 1340 1341 tx_buffer = first; 1342 1343 for (frag = &skb_shinfo(skb)->frags[0];; frag++) { 1344 if (dma_mapping_error(tx_ring->dev, dma)) 1345 goto dma_error; 1346 1347 /* record length, and DMA address */ 1348 dma_unmap_len_set(tx_buffer, len, size); 1349 dma_unmap_addr_set(tx_buffer, dma, dma); 1350 1351 tx_desc->read.buffer_addr = cpu_to_le64(dma); 1352 1353 while (unlikely(size > IGC_MAX_DATA_PER_TXD)) { 1354 tx_desc->read.cmd_type_len = 1355 cpu_to_le32(cmd_type ^ IGC_MAX_DATA_PER_TXD); 1356 1357 i++; 1358 tx_desc++; 1359 if (i == tx_ring->count) { 1360 tx_desc = IGC_TX_DESC(tx_ring, 0); 1361 i = 0; 1362 } 1363 tx_desc->read.olinfo_status = 0; 1364 1365 dma += IGC_MAX_DATA_PER_TXD; 1366 size -= IGC_MAX_DATA_PER_TXD; 1367 1368 tx_desc->read.buffer_addr = cpu_to_le64(dma); 1369 } 1370 1371 if (likely(!data_len)) 1372 break; 1373 1374 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type ^ size); 1375 1376 i++; 1377 tx_desc++; 1378 if (i == tx_ring->count) { 1379 tx_desc = IGC_TX_DESC(tx_ring, 0); 1380 i = 0; 1381 } 1382 tx_desc->read.olinfo_status = 0; 1383 1384 size = skb_frag_size(frag); 1385 data_len -= size; 1386 1387 dma = skb_frag_dma_map(tx_ring->dev, frag, 0, 1388 size, DMA_TO_DEVICE); 1389 1390 tx_buffer = &tx_ring->tx_buffer_info[i]; 1391 } 1392 1393 /* write last descriptor with RS and EOP bits */ 1394 cmd_type |= size | IGC_TXD_DCMD; 1395 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type); 1396 1397 netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount); 1398 1399 /* set the timestamp */ 1400 first->time_stamp = jiffies; 1401 1402 skb_tx_timestamp(skb); 1403 1404 /* Force memory writes to complete before letting h/w know there 1405 * are new descriptors to fetch. (Only applicable for weak-ordered 1406 * memory model archs, such as IA-64). 1407 * 1408 * We also need this memory barrier to make certain all of the 1409 * status bits have been updated before next_to_watch is written. 1410 */ 1411 wmb(); 1412 1413 /* set next_to_watch value indicating a packet is present */ 1414 first->next_to_watch = tx_desc; 1415 1416 i++; 1417 if (i == tx_ring->count) 1418 i = 0; 1419 1420 tx_ring->next_to_use = i; 1421 1422 /* Make sure there is space in the ring for the next send. */ 1423 igc_maybe_stop_tx(tx_ring, DESC_NEEDED); 1424 1425 if (netif_xmit_stopped(txring_txq(tx_ring)) || !netdev_xmit_more()) { 1426 writel(i, tx_ring->tail); 1427 } 1428 1429 return 0; 1430 dma_error: 1431 netdev_err(tx_ring->netdev, "TX DMA map failed\n"); 1432 tx_buffer = &tx_ring->tx_buffer_info[i]; 1433 1434 /* clear dma mappings for failed tx_buffer_info map */ 1435 while (tx_buffer != first) { 1436 if (dma_unmap_len(tx_buffer, len)) 1437 igc_unmap_tx_buffer(tx_ring->dev, tx_buffer); 1438 1439 if (i-- == 0) 1440 i += tx_ring->count; 1441 tx_buffer = &tx_ring->tx_buffer_info[i]; 1442 } 1443 1444 if (dma_unmap_len(tx_buffer, len)) 1445 igc_unmap_tx_buffer(tx_ring->dev, tx_buffer); 1446 1447 dev_kfree_skb_any(tx_buffer->skb); 1448 tx_buffer->skb = NULL; 1449 1450 tx_ring->next_to_use = i; 1451 1452 return -1; 1453 } 1454 1455 static int igc_tso(struct igc_ring *tx_ring, 1456 struct igc_tx_buffer *first, 1457 __le32 launch_time, bool first_flag, 1458 u8 *hdr_len) 1459 { 1460 u32 vlan_macip_lens, type_tucmd, mss_l4len_idx; 1461 struct sk_buff *skb = first->skb; 1462 union { 1463 struct iphdr *v4; 1464 struct ipv6hdr *v6; 1465 unsigned char *hdr; 1466 } ip; 1467 union { 1468 struct tcphdr *tcp; 1469 struct udphdr *udp; 1470 unsigned char *hdr; 1471 } l4; 1472 u32 paylen, l4_offset; 1473 int err; 1474 1475 if (skb->ip_summed != CHECKSUM_PARTIAL) 1476 return 0; 1477 1478 if (!skb_is_gso(skb)) 1479 return 0; 1480 1481 err = skb_cow_head(skb, 0); 1482 if (err < 0) 1483 return err; 1484 1485 ip.hdr = skb_network_header(skb); 1486 l4.hdr = skb_checksum_start(skb); 1487 1488 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */ 1489 type_tucmd = IGC_ADVTXD_TUCMD_L4T_TCP; 1490 1491 /* initialize outer IP header fields */ 1492 if (ip.v4->version == 4) { 1493 unsigned char *csum_start = skb_checksum_start(skb); 1494 unsigned char *trans_start = ip.hdr + (ip.v4->ihl * 4); 1495 1496 /* IP header will have to cancel out any data that 1497 * is not a part of the outer IP header 1498 */ 1499 ip.v4->check = csum_fold(csum_partial(trans_start, 1500 csum_start - trans_start, 1501 0)); 1502 type_tucmd |= IGC_ADVTXD_TUCMD_IPV4; 1503 1504 ip.v4->tot_len = 0; 1505 first->tx_flags |= IGC_TX_FLAGS_TSO | 1506 IGC_TX_FLAGS_CSUM | 1507 IGC_TX_FLAGS_IPV4; 1508 } else { 1509 ip.v6->payload_len = 0; 1510 first->tx_flags |= IGC_TX_FLAGS_TSO | 1511 IGC_TX_FLAGS_CSUM; 1512 } 1513 1514 /* determine offset of inner transport header */ 1515 l4_offset = l4.hdr - skb->data; 1516 1517 /* remove payload length from inner checksum */ 1518 paylen = skb->len - l4_offset; 1519 if (type_tucmd & IGC_ADVTXD_TUCMD_L4T_TCP) { 1520 /* compute length of segmentation header */ 1521 *hdr_len = (l4.tcp->doff * 4) + l4_offset; 1522 csum_replace_by_diff(&l4.tcp->check, 1523 (__force __wsum)htonl(paylen)); 1524 } else { 1525 /* compute length of segmentation header */ 1526 *hdr_len = sizeof(*l4.udp) + l4_offset; 1527 csum_replace_by_diff(&l4.udp->check, 1528 (__force __wsum)htonl(paylen)); 1529 } 1530 1531 /* update gso size and bytecount with header size */ 1532 first->gso_segs = skb_shinfo(skb)->gso_segs; 1533 first->bytecount += (first->gso_segs - 1) * *hdr_len; 1534 1535 /* MSS L4LEN IDX */ 1536 mss_l4len_idx = (*hdr_len - l4_offset) << IGC_ADVTXD_L4LEN_SHIFT; 1537 mss_l4len_idx |= skb_shinfo(skb)->gso_size << IGC_ADVTXD_MSS_SHIFT; 1538 1539 /* VLAN MACLEN IPLEN */ 1540 vlan_macip_lens = l4.hdr - ip.hdr; 1541 vlan_macip_lens |= (ip.hdr - skb->data) << IGC_ADVTXD_MACLEN_SHIFT; 1542 vlan_macip_lens |= first->tx_flags & IGC_TX_FLAGS_VLAN_MASK; 1543 1544 igc_tx_ctxtdesc(tx_ring, launch_time, first_flag, 1545 vlan_macip_lens, type_tucmd, mss_l4len_idx); 1546 1547 return 1; 1548 } 1549 1550 static bool igc_request_tx_tstamp(struct igc_adapter *adapter, struct sk_buff *skb, u32 *flags) 1551 { 1552 int i; 1553 1554 for (i = 0; i < IGC_MAX_TX_TSTAMP_REGS; i++) { 1555 struct igc_tx_timestamp_request *tstamp = &adapter->tx_tstamp[i]; 1556 1557 if (tstamp->skb) 1558 continue; 1559 1560 tstamp->skb = skb_get(skb); 1561 tstamp->start = jiffies; 1562 *flags = tstamp->flags; 1563 1564 return true; 1565 } 1566 1567 return false; 1568 } 1569 1570 static netdev_tx_t igc_xmit_frame_ring(struct sk_buff *skb, 1571 struct igc_ring *tx_ring) 1572 { 1573 struct igc_adapter *adapter = netdev_priv(tx_ring->netdev); 1574 bool first_flag = false, insert_empty = false; 1575 u16 count = TXD_USE_COUNT(skb_headlen(skb)); 1576 __be16 protocol = vlan_get_protocol(skb); 1577 struct igc_tx_buffer *first; 1578 __le32 launch_time = 0; 1579 u32 tx_flags = 0; 1580 unsigned short f; 1581 ktime_t txtime; 1582 u8 hdr_len = 0; 1583 int tso = 0; 1584 1585 /* need: 1 descriptor per page * PAGE_SIZE/IGC_MAX_DATA_PER_TXD, 1586 * + 1 desc for skb_headlen/IGC_MAX_DATA_PER_TXD, 1587 * + 2 desc gap to keep tail from touching head, 1588 * + 1 desc for context descriptor, 1589 * otherwise try next time 1590 */ 1591 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++) 1592 count += TXD_USE_COUNT(skb_frag_size( 1593 &skb_shinfo(skb)->frags[f])); 1594 1595 if (igc_maybe_stop_tx(tx_ring, count + 5)) { 1596 /* this is a hard error */ 1597 return NETDEV_TX_BUSY; 1598 } 1599 1600 if (!tx_ring->launchtime_enable) 1601 goto done; 1602 1603 txtime = skb->tstamp; 1604 skb->tstamp = ktime_set(0, 0); 1605 launch_time = igc_tx_launchtime(tx_ring, txtime, &first_flag, &insert_empty); 1606 1607 if (insert_empty) { 1608 struct igc_tx_buffer *empty_info; 1609 struct sk_buff *empty; 1610 void *data; 1611 1612 empty_info = &tx_ring->tx_buffer_info[tx_ring->next_to_use]; 1613 empty = alloc_skb(IGC_EMPTY_FRAME_SIZE, GFP_ATOMIC); 1614 if (!empty) 1615 goto done; 1616 1617 data = skb_put(empty, IGC_EMPTY_FRAME_SIZE); 1618 memset(data, 0, IGC_EMPTY_FRAME_SIZE); 1619 1620 igc_tx_ctxtdesc(tx_ring, 0, false, 0, 0, 0); 1621 1622 if (igc_init_tx_empty_descriptor(tx_ring, 1623 empty, 1624 empty_info) < 0) 1625 dev_kfree_skb_any(empty); 1626 } 1627 1628 done: 1629 /* record the location of the first descriptor for this packet */ 1630 first = &tx_ring->tx_buffer_info[tx_ring->next_to_use]; 1631 first->type = IGC_TX_BUFFER_TYPE_SKB; 1632 first->skb = skb; 1633 first->bytecount = skb->len; 1634 first->gso_segs = 1; 1635 1636 if (adapter->qbv_transition || tx_ring->oper_gate_closed) 1637 goto out_drop; 1638 1639 if (tx_ring->max_sdu > 0 && first->bytecount > tx_ring->max_sdu) { 1640 adapter->stats.txdrop++; 1641 goto out_drop; 1642 } 1643 1644 if (unlikely(test_bit(IGC_RING_FLAG_TX_HWTSTAMP, &tx_ring->flags) && 1645 skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) { 1646 unsigned long flags; 1647 u32 tstamp_flags; 1648 1649 spin_lock_irqsave(&adapter->ptp_tx_lock, flags); 1650 if (igc_request_tx_tstamp(adapter, skb, &tstamp_flags)) { 1651 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 1652 tx_flags |= IGC_TX_FLAGS_TSTAMP | tstamp_flags; 1653 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP_USE_CYCLES) 1654 tx_flags |= IGC_TX_FLAGS_TSTAMP_TIMER_1; 1655 } else { 1656 adapter->tx_hwtstamp_skipped++; 1657 } 1658 1659 spin_unlock_irqrestore(&adapter->ptp_tx_lock, flags); 1660 } 1661 1662 if (skb_vlan_tag_present(skb)) { 1663 tx_flags |= IGC_TX_FLAGS_VLAN; 1664 tx_flags |= (skb_vlan_tag_get(skb) << IGC_TX_FLAGS_VLAN_SHIFT); 1665 } 1666 1667 /* record initial flags and protocol */ 1668 first->tx_flags = tx_flags; 1669 first->protocol = protocol; 1670 1671 tso = igc_tso(tx_ring, first, launch_time, first_flag, &hdr_len); 1672 if (tso < 0) 1673 goto out_drop; 1674 else if (!tso) 1675 igc_tx_csum(tx_ring, first, launch_time, first_flag); 1676 1677 igc_tx_map(tx_ring, first, hdr_len); 1678 1679 return NETDEV_TX_OK; 1680 1681 out_drop: 1682 dev_kfree_skb_any(first->skb); 1683 first->skb = NULL; 1684 1685 return NETDEV_TX_OK; 1686 } 1687 1688 static inline struct igc_ring *igc_tx_queue_mapping(struct igc_adapter *adapter, 1689 struct sk_buff *skb) 1690 { 1691 unsigned int r_idx = skb->queue_mapping; 1692 1693 if (r_idx >= adapter->num_tx_queues) 1694 r_idx = r_idx % adapter->num_tx_queues; 1695 1696 return adapter->tx_ring[r_idx]; 1697 } 1698 1699 static netdev_tx_t igc_xmit_frame(struct sk_buff *skb, 1700 struct net_device *netdev) 1701 { 1702 struct igc_adapter *adapter = netdev_priv(netdev); 1703 1704 /* The minimum packet size with TCTL.PSP set is 17 so pad the skb 1705 * in order to meet this minimum size requirement. 1706 */ 1707 if (skb->len < 17) { 1708 if (skb_padto(skb, 17)) 1709 return NETDEV_TX_OK; 1710 skb->len = 17; 1711 } 1712 1713 return igc_xmit_frame_ring(skb, igc_tx_queue_mapping(adapter, skb)); 1714 } 1715 1716 static void igc_rx_checksum(struct igc_ring *ring, 1717 union igc_adv_rx_desc *rx_desc, 1718 struct sk_buff *skb) 1719 { 1720 skb_checksum_none_assert(skb); 1721 1722 /* Ignore Checksum bit is set */ 1723 if (igc_test_staterr(rx_desc, IGC_RXD_STAT_IXSM)) 1724 return; 1725 1726 /* Rx checksum disabled via ethtool */ 1727 if (!(ring->netdev->features & NETIF_F_RXCSUM)) 1728 return; 1729 1730 /* TCP/UDP checksum error bit is set */ 1731 if (igc_test_staterr(rx_desc, 1732 IGC_RXDEXT_STATERR_L4E | 1733 IGC_RXDEXT_STATERR_IPE)) { 1734 /* work around errata with sctp packets where the TCPE aka 1735 * L4E bit is set incorrectly on 64 byte (60 byte w/o crc) 1736 * packets (aka let the stack check the crc32c) 1737 */ 1738 if (!(skb->len == 60 && 1739 test_bit(IGC_RING_FLAG_RX_SCTP_CSUM, &ring->flags))) { 1740 u64_stats_update_begin(&ring->rx_syncp); 1741 ring->rx_stats.csum_err++; 1742 u64_stats_update_end(&ring->rx_syncp); 1743 } 1744 /* let the stack verify checksum errors */ 1745 return; 1746 } 1747 /* It must be a TCP or UDP packet with a valid checksum */ 1748 if (igc_test_staterr(rx_desc, IGC_RXD_STAT_TCPCS | 1749 IGC_RXD_STAT_UDPCS)) 1750 skb->ip_summed = CHECKSUM_UNNECESSARY; 1751 1752 netdev_dbg(ring->netdev, "cksum success: bits %08X\n", 1753 le32_to_cpu(rx_desc->wb.upper.status_error)); 1754 } 1755 1756 /* Mapping HW RSS Type to enum pkt_hash_types */ 1757 static const enum pkt_hash_types igc_rss_type_table[IGC_RSS_TYPE_MAX_TABLE] = { 1758 [IGC_RSS_TYPE_NO_HASH] = PKT_HASH_TYPE_L2, 1759 [IGC_RSS_TYPE_HASH_TCP_IPV4] = PKT_HASH_TYPE_L4, 1760 [IGC_RSS_TYPE_HASH_IPV4] = PKT_HASH_TYPE_L3, 1761 [IGC_RSS_TYPE_HASH_TCP_IPV6] = PKT_HASH_TYPE_L4, 1762 [IGC_RSS_TYPE_HASH_IPV6_EX] = PKT_HASH_TYPE_L3, 1763 [IGC_RSS_TYPE_HASH_IPV6] = PKT_HASH_TYPE_L3, 1764 [IGC_RSS_TYPE_HASH_TCP_IPV6_EX] = PKT_HASH_TYPE_L4, 1765 [IGC_RSS_TYPE_HASH_UDP_IPV4] = PKT_HASH_TYPE_L4, 1766 [IGC_RSS_TYPE_HASH_UDP_IPV6] = PKT_HASH_TYPE_L4, 1767 [IGC_RSS_TYPE_HASH_UDP_IPV6_EX] = PKT_HASH_TYPE_L4, 1768 [10] = PKT_HASH_TYPE_NONE, /* RSS Type above 9 "Reserved" by HW */ 1769 [11] = PKT_HASH_TYPE_NONE, /* keep array sized for SW bit-mask */ 1770 [12] = PKT_HASH_TYPE_NONE, /* to handle future HW revisons */ 1771 [13] = PKT_HASH_TYPE_NONE, 1772 [14] = PKT_HASH_TYPE_NONE, 1773 [15] = PKT_HASH_TYPE_NONE, 1774 }; 1775 1776 static inline void igc_rx_hash(struct igc_ring *ring, 1777 union igc_adv_rx_desc *rx_desc, 1778 struct sk_buff *skb) 1779 { 1780 if (ring->netdev->features & NETIF_F_RXHASH) { 1781 u32 rss_hash = le32_to_cpu(rx_desc->wb.lower.hi_dword.rss); 1782 u32 rss_type = igc_rss_type(rx_desc); 1783 1784 skb_set_hash(skb, rss_hash, igc_rss_type_table[rss_type]); 1785 } 1786 } 1787 1788 static void igc_rx_vlan(struct igc_ring *rx_ring, 1789 union igc_adv_rx_desc *rx_desc, 1790 struct sk_buff *skb) 1791 { 1792 struct net_device *dev = rx_ring->netdev; 1793 u16 vid; 1794 1795 if ((dev->features & NETIF_F_HW_VLAN_CTAG_RX) && 1796 igc_test_staterr(rx_desc, IGC_RXD_STAT_VP)) { 1797 if (igc_test_staterr(rx_desc, IGC_RXDEXT_STATERR_LB) && 1798 test_bit(IGC_RING_FLAG_RX_LB_VLAN_BSWAP, &rx_ring->flags)) 1799 vid = be16_to_cpu((__force __be16)rx_desc->wb.upper.vlan); 1800 else 1801 vid = le16_to_cpu(rx_desc->wb.upper.vlan); 1802 1803 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid); 1804 } 1805 } 1806 1807 /** 1808 * igc_process_skb_fields - Populate skb header fields from Rx descriptor 1809 * @rx_ring: rx descriptor ring packet is being transacted on 1810 * @rx_desc: pointer to the EOP Rx descriptor 1811 * @skb: pointer to current skb being populated 1812 * 1813 * This function checks the ring, descriptor, and packet information in order 1814 * to populate the hash, checksum, VLAN, protocol, and other fields within the 1815 * skb. 1816 */ 1817 static void igc_process_skb_fields(struct igc_ring *rx_ring, 1818 union igc_adv_rx_desc *rx_desc, 1819 struct sk_buff *skb) 1820 { 1821 igc_rx_hash(rx_ring, rx_desc, skb); 1822 1823 igc_rx_checksum(rx_ring, rx_desc, skb); 1824 1825 igc_rx_vlan(rx_ring, rx_desc, skb); 1826 1827 skb_record_rx_queue(skb, rx_ring->queue_index); 1828 1829 skb->protocol = eth_type_trans(skb, rx_ring->netdev); 1830 } 1831 1832 static void igc_vlan_mode(struct net_device *netdev, netdev_features_t features) 1833 { 1834 bool enable = !!(features & NETIF_F_HW_VLAN_CTAG_RX); 1835 struct igc_adapter *adapter = netdev_priv(netdev); 1836 struct igc_hw *hw = &adapter->hw; 1837 u32 ctrl; 1838 1839 ctrl = rd32(IGC_CTRL); 1840 1841 if (enable) { 1842 /* enable VLAN tag insert/strip */ 1843 ctrl |= IGC_CTRL_VME; 1844 } else { 1845 /* disable VLAN tag insert/strip */ 1846 ctrl &= ~IGC_CTRL_VME; 1847 } 1848 wr32(IGC_CTRL, ctrl); 1849 } 1850 1851 static void igc_restore_vlan(struct igc_adapter *adapter) 1852 { 1853 igc_vlan_mode(adapter->netdev, adapter->netdev->features); 1854 } 1855 1856 static struct igc_rx_buffer *igc_get_rx_buffer(struct igc_ring *rx_ring, 1857 const unsigned int size, 1858 int *rx_buffer_pgcnt) 1859 { 1860 struct igc_rx_buffer *rx_buffer; 1861 1862 rx_buffer = &rx_ring->rx_buffer_info[rx_ring->next_to_clean]; 1863 *rx_buffer_pgcnt = 1864 #if (PAGE_SIZE < 8192) 1865 page_count(rx_buffer->page); 1866 #else 1867 0; 1868 #endif 1869 prefetchw(rx_buffer->page); 1870 1871 /* we are reusing so sync this buffer for CPU use */ 1872 dma_sync_single_range_for_cpu(rx_ring->dev, 1873 rx_buffer->dma, 1874 rx_buffer->page_offset, 1875 size, 1876 DMA_FROM_DEVICE); 1877 1878 rx_buffer->pagecnt_bias--; 1879 1880 return rx_buffer; 1881 } 1882 1883 static void igc_rx_buffer_flip(struct igc_rx_buffer *buffer, 1884 unsigned int truesize) 1885 { 1886 #if (PAGE_SIZE < 8192) 1887 buffer->page_offset ^= truesize; 1888 #else 1889 buffer->page_offset += truesize; 1890 #endif 1891 } 1892 1893 static unsigned int igc_get_rx_frame_truesize(struct igc_ring *ring, 1894 unsigned int size) 1895 { 1896 unsigned int truesize; 1897 1898 #if (PAGE_SIZE < 8192) 1899 truesize = igc_rx_pg_size(ring) / 2; 1900 #else 1901 truesize = ring_uses_build_skb(ring) ? 1902 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) + 1903 SKB_DATA_ALIGN(IGC_SKB_PAD + size) : 1904 SKB_DATA_ALIGN(size); 1905 #endif 1906 return truesize; 1907 } 1908 1909 /** 1910 * igc_add_rx_frag - Add contents of Rx buffer to sk_buff 1911 * @rx_ring: rx descriptor ring to transact packets on 1912 * @rx_buffer: buffer containing page to add 1913 * @skb: sk_buff to place the data into 1914 * @size: size of buffer to be added 1915 * 1916 * This function will add the data contained in rx_buffer->page to the skb. 1917 */ 1918 static void igc_add_rx_frag(struct igc_ring *rx_ring, 1919 struct igc_rx_buffer *rx_buffer, 1920 struct sk_buff *skb, 1921 unsigned int size) 1922 { 1923 unsigned int truesize; 1924 1925 #if (PAGE_SIZE < 8192) 1926 truesize = igc_rx_pg_size(rx_ring) / 2; 1927 #else 1928 truesize = ring_uses_build_skb(rx_ring) ? 1929 SKB_DATA_ALIGN(IGC_SKB_PAD + size) : 1930 SKB_DATA_ALIGN(size); 1931 #endif 1932 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_buffer->page, 1933 rx_buffer->page_offset, size, truesize); 1934 1935 igc_rx_buffer_flip(rx_buffer, truesize); 1936 } 1937 1938 static struct sk_buff *igc_build_skb(struct igc_ring *rx_ring, 1939 struct igc_rx_buffer *rx_buffer, 1940 struct xdp_buff *xdp) 1941 { 1942 unsigned int size = xdp->data_end - xdp->data; 1943 unsigned int truesize = igc_get_rx_frame_truesize(rx_ring, size); 1944 unsigned int metasize = xdp->data - xdp->data_meta; 1945 struct sk_buff *skb; 1946 1947 /* prefetch first cache line of first page */ 1948 net_prefetch(xdp->data_meta); 1949 1950 /* build an skb around the page buffer */ 1951 skb = napi_build_skb(xdp->data_hard_start, truesize); 1952 if (unlikely(!skb)) 1953 return NULL; 1954 1955 /* update pointers within the skb to store the data */ 1956 skb_reserve(skb, xdp->data - xdp->data_hard_start); 1957 __skb_put(skb, size); 1958 if (metasize) 1959 skb_metadata_set(skb, metasize); 1960 1961 igc_rx_buffer_flip(rx_buffer, truesize); 1962 return skb; 1963 } 1964 1965 static struct sk_buff *igc_construct_skb(struct igc_ring *rx_ring, 1966 struct igc_rx_buffer *rx_buffer, 1967 struct igc_xdp_buff *ctx) 1968 { 1969 struct xdp_buff *xdp = &ctx->xdp; 1970 unsigned int metasize = xdp->data - xdp->data_meta; 1971 unsigned int size = xdp->data_end - xdp->data; 1972 unsigned int truesize = igc_get_rx_frame_truesize(rx_ring, size); 1973 void *va = xdp->data; 1974 unsigned int headlen; 1975 struct sk_buff *skb; 1976 1977 /* prefetch first cache line of first page */ 1978 net_prefetch(xdp->data_meta); 1979 1980 /* allocate a skb to store the frags */ 1981 skb = napi_alloc_skb(&rx_ring->q_vector->napi, 1982 IGC_RX_HDR_LEN + metasize); 1983 if (unlikely(!skb)) 1984 return NULL; 1985 1986 if (ctx->rx_ts) { 1987 skb_shinfo(skb)->tx_flags |= SKBTX_HW_TSTAMP_NETDEV; 1988 skb_hwtstamps(skb)->netdev_data = ctx->rx_ts; 1989 } 1990 1991 /* Determine available headroom for copy */ 1992 headlen = size; 1993 if (headlen > IGC_RX_HDR_LEN) 1994 headlen = eth_get_headlen(skb->dev, va, IGC_RX_HDR_LEN); 1995 1996 /* align pull length to size of long to optimize memcpy performance */ 1997 memcpy(__skb_put(skb, headlen + metasize), xdp->data_meta, 1998 ALIGN(headlen + metasize, sizeof(long))); 1999 2000 if (metasize) { 2001 skb_metadata_set(skb, metasize); 2002 __skb_pull(skb, metasize); 2003 } 2004 2005 /* update all of the pointers */ 2006 size -= headlen; 2007 if (size) { 2008 skb_add_rx_frag(skb, 0, rx_buffer->page, 2009 (va + headlen) - page_address(rx_buffer->page), 2010 size, truesize); 2011 igc_rx_buffer_flip(rx_buffer, truesize); 2012 } else { 2013 rx_buffer->pagecnt_bias++; 2014 } 2015 2016 return skb; 2017 } 2018 2019 /** 2020 * igc_reuse_rx_page - page flip buffer and store it back on the ring 2021 * @rx_ring: rx descriptor ring to store buffers on 2022 * @old_buff: donor buffer to have page reused 2023 * 2024 * Synchronizes page for reuse by the adapter 2025 */ 2026 static void igc_reuse_rx_page(struct igc_ring *rx_ring, 2027 struct igc_rx_buffer *old_buff) 2028 { 2029 u16 nta = rx_ring->next_to_alloc; 2030 struct igc_rx_buffer *new_buff; 2031 2032 new_buff = &rx_ring->rx_buffer_info[nta]; 2033 2034 /* update, and store next to alloc */ 2035 nta++; 2036 rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0; 2037 2038 /* Transfer page from old buffer to new buffer. 2039 * Move each member individually to avoid possible store 2040 * forwarding stalls. 2041 */ 2042 new_buff->dma = old_buff->dma; 2043 new_buff->page = old_buff->page; 2044 new_buff->page_offset = old_buff->page_offset; 2045 new_buff->pagecnt_bias = old_buff->pagecnt_bias; 2046 } 2047 2048 static bool igc_can_reuse_rx_page(struct igc_rx_buffer *rx_buffer, 2049 int rx_buffer_pgcnt) 2050 { 2051 unsigned int pagecnt_bias = rx_buffer->pagecnt_bias; 2052 struct page *page = rx_buffer->page; 2053 2054 /* avoid re-using remote and pfmemalloc pages */ 2055 if (!dev_page_is_reusable(page)) 2056 return false; 2057 2058 #if (PAGE_SIZE < 8192) 2059 /* if we are only owner of page we can reuse it */ 2060 if (unlikely((rx_buffer_pgcnt - pagecnt_bias) > 1)) 2061 return false; 2062 #else 2063 #define IGC_LAST_OFFSET \ 2064 (SKB_WITH_OVERHEAD(PAGE_SIZE) - IGC_RXBUFFER_2048) 2065 2066 if (rx_buffer->page_offset > IGC_LAST_OFFSET) 2067 return false; 2068 #endif 2069 2070 /* If we have drained the page fragment pool we need to update 2071 * the pagecnt_bias and page count so that we fully restock the 2072 * number of references the driver holds. 2073 */ 2074 if (unlikely(pagecnt_bias == 1)) { 2075 page_ref_add(page, USHRT_MAX - 1); 2076 rx_buffer->pagecnt_bias = USHRT_MAX; 2077 } 2078 2079 return true; 2080 } 2081 2082 /** 2083 * igc_is_non_eop - process handling of non-EOP buffers 2084 * @rx_ring: Rx ring being processed 2085 * @rx_desc: Rx descriptor for current buffer 2086 * 2087 * This function updates next to clean. If the buffer is an EOP buffer 2088 * this function exits returning false, otherwise it will place the 2089 * sk_buff in the next buffer to be chained and return true indicating 2090 * that this is in fact a non-EOP buffer. 2091 */ 2092 static bool igc_is_non_eop(struct igc_ring *rx_ring, 2093 union igc_adv_rx_desc *rx_desc) 2094 { 2095 u32 ntc = rx_ring->next_to_clean + 1; 2096 2097 /* fetch, update, and store next to clean */ 2098 ntc = (ntc < rx_ring->count) ? ntc : 0; 2099 rx_ring->next_to_clean = ntc; 2100 2101 prefetch(IGC_RX_DESC(rx_ring, ntc)); 2102 2103 if (likely(igc_test_staterr(rx_desc, IGC_RXD_STAT_EOP))) 2104 return false; 2105 2106 return true; 2107 } 2108 2109 /** 2110 * igc_cleanup_headers - Correct corrupted or empty headers 2111 * @rx_ring: rx descriptor ring packet is being transacted on 2112 * @rx_desc: pointer to the EOP Rx descriptor 2113 * @skb: pointer to current skb being fixed 2114 * 2115 * Address the case where we are pulling data in on pages only 2116 * and as such no data is present in the skb header. 2117 * 2118 * In addition if skb is not at least 60 bytes we need to pad it so that 2119 * it is large enough to qualify as a valid Ethernet frame. 2120 * 2121 * Returns true if an error was encountered and skb was freed. 2122 */ 2123 static bool igc_cleanup_headers(struct igc_ring *rx_ring, 2124 union igc_adv_rx_desc *rx_desc, 2125 struct sk_buff *skb) 2126 { 2127 if (unlikely(igc_test_staterr(rx_desc, IGC_RXDEXT_STATERR_RXE))) { 2128 struct net_device *netdev = rx_ring->netdev; 2129 2130 if (!(netdev->features & NETIF_F_RXALL)) { 2131 dev_kfree_skb_any(skb); 2132 return true; 2133 } 2134 } 2135 2136 /* if eth_skb_pad returns an error the skb was freed */ 2137 if (eth_skb_pad(skb)) 2138 return true; 2139 2140 return false; 2141 } 2142 2143 static void igc_put_rx_buffer(struct igc_ring *rx_ring, 2144 struct igc_rx_buffer *rx_buffer, 2145 int rx_buffer_pgcnt) 2146 { 2147 if (igc_can_reuse_rx_page(rx_buffer, rx_buffer_pgcnt)) { 2148 /* hand second half of page back to the ring */ 2149 igc_reuse_rx_page(rx_ring, rx_buffer); 2150 } else { 2151 /* We are not reusing the buffer so unmap it and free 2152 * any references we are holding to it 2153 */ 2154 dma_unmap_page_attrs(rx_ring->dev, rx_buffer->dma, 2155 igc_rx_pg_size(rx_ring), DMA_FROM_DEVICE, 2156 IGC_RX_DMA_ATTR); 2157 __page_frag_cache_drain(rx_buffer->page, 2158 rx_buffer->pagecnt_bias); 2159 } 2160 2161 /* clear contents of rx_buffer */ 2162 rx_buffer->page = NULL; 2163 } 2164 2165 static inline unsigned int igc_rx_offset(struct igc_ring *rx_ring) 2166 { 2167 struct igc_adapter *adapter = rx_ring->q_vector->adapter; 2168 2169 if (ring_uses_build_skb(rx_ring)) 2170 return IGC_SKB_PAD; 2171 if (igc_xdp_is_enabled(adapter)) 2172 return XDP_PACKET_HEADROOM; 2173 2174 return 0; 2175 } 2176 2177 static bool igc_alloc_mapped_page(struct igc_ring *rx_ring, 2178 struct igc_rx_buffer *bi) 2179 { 2180 struct page *page = bi->page; 2181 dma_addr_t dma; 2182 2183 /* since we are recycling buffers we should seldom need to alloc */ 2184 if (likely(page)) 2185 return true; 2186 2187 /* alloc new page for storage */ 2188 page = dev_alloc_pages(igc_rx_pg_order(rx_ring)); 2189 if (unlikely(!page)) { 2190 rx_ring->rx_stats.alloc_failed++; 2191 set_bit(IGC_RING_FLAG_RX_ALLOC_FAILED, &rx_ring->flags); 2192 return false; 2193 } 2194 2195 /* map page for use */ 2196 dma = dma_map_page_attrs(rx_ring->dev, page, 0, 2197 igc_rx_pg_size(rx_ring), 2198 DMA_FROM_DEVICE, 2199 IGC_RX_DMA_ATTR); 2200 2201 /* if mapping failed free memory back to system since 2202 * there isn't much point in holding memory we can't use 2203 */ 2204 if (dma_mapping_error(rx_ring->dev, dma)) { 2205 __free_page(page); 2206 2207 rx_ring->rx_stats.alloc_failed++; 2208 set_bit(IGC_RING_FLAG_RX_ALLOC_FAILED, &rx_ring->flags); 2209 return false; 2210 } 2211 2212 bi->dma = dma; 2213 bi->page = page; 2214 bi->page_offset = igc_rx_offset(rx_ring); 2215 page_ref_add(page, USHRT_MAX - 1); 2216 bi->pagecnt_bias = USHRT_MAX; 2217 2218 return true; 2219 } 2220 2221 /** 2222 * igc_alloc_rx_buffers - Replace used receive buffers; packet split 2223 * @rx_ring: rx descriptor ring 2224 * @cleaned_count: number of buffers to clean 2225 */ 2226 static void igc_alloc_rx_buffers(struct igc_ring *rx_ring, u16 cleaned_count) 2227 { 2228 union igc_adv_rx_desc *rx_desc; 2229 u16 i = rx_ring->next_to_use; 2230 struct igc_rx_buffer *bi; 2231 u16 bufsz; 2232 2233 /* nothing to do */ 2234 if (!cleaned_count) 2235 return; 2236 2237 rx_desc = IGC_RX_DESC(rx_ring, i); 2238 bi = &rx_ring->rx_buffer_info[i]; 2239 i -= rx_ring->count; 2240 2241 bufsz = igc_rx_bufsz(rx_ring); 2242 2243 do { 2244 if (!igc_alloc_mapped_page(rx_ring, bi)) 2245 break; 2246 2247 /* sync the buffer for use by the device */ 2248 dma_sync_single_range_for_device(rx_ring->dev, bi->dma, 2249 bi->page_offset, bufsz, 2250 DMA_FROM_DEVICE); 2251 2252 /* Refresh the desc even if buffer_addrs didn't change 2253 * because each write-back erases this info. 2254 */ 2255 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset); 2256 2257 rx_desc++; 2258 bi++; 2259 i++; 2260 if (unlikely(!i)) { 2261 rx_desc = IGC_RX_DESC(rx_ring, 0); 2262 bi = rx_ring->rx_buffer_info; 2263 i -= rx_ring->count; 2264 } 2265 2266 /* clear the length for the next_to_use descriptor */ 2267 rx_desc->wb.upper.length = 0; 2268 2269 cleaned_count--; 2270 } while (cleaned_count); 2271 2272 i += rx_ring->count; 2273 2274 if (rx_ring->next_to_use != i) { 2275 /* record the next descriptor to use */ 2276 rx_ring->next_to_use = i; 2277 2278 /* update next to alloc since we have filled the ring */ 2279 rx_ring->next_to_alloc = i; 2280 2281 /* Force memory writes to complete before letting h/w 2282 * know there are new descriptors to fetch. (Only 2283 * applicable for weak-ordered memory model archs, 2284 * such as IA-64). 2285 */ 2286 wmb(); 2287 writel(i, rx_ring->tail); 2288 } 2289 } 2290 2291 static bool igc_alloc_rx_buffers_zc(struct igc_ring *ring, u16 count) 2292 { 2293 union igc_adv_rx_desc *desc; 2294 u16 i = ring->next_to_use; 2295 struct igc_rx_buffer *bi; 2296 dma_addr_t dma; 2297 bool ok = true; 2298 2299 if (!count) 2300 return ok; 2301 2302 XSK_CHECK_PRIV_TYPE(struct igc_xdp_buff); 2303 2304 desc = IGC_RX_DESC(ring, i); 2305 bi = &ring->rx_buffer_info[i]; 2306 i -= ring->count; 2307 2308 do { 2309 bi->xdp = xsk_buff_alloc(ring->xsk_pool); 2310 if (!bi->xdp) { 2311 ok = false; 2312 break; 2313 } 2314 2315 dma = xsk_buff_xdp_get_dma(bi->xdp); 2316 desc->read.pkt_addr = cpu_to_le64(dma); 2317 2318 desc++; 2319 bi++; 2320 i++; 2321 if (unlikely(!i)) { 2322 desc = IGC_RX_DESC(ring, 0); 2323 bi = ring->rx_buffer_info; 2324 i -= ring->count; 2325 } 2326 2327 /* Clear the length for the next_to_use descriptor. */ 2328 desc->wb.upper.length = 0; 2329 2330 count--; 2331 } while (count); 2332 2333 i += ring->count; 2334 2335 if (ring->next_to_use != i) { 2336 ring->next_to_use = i; 2337 2338 /* Force memory writes to complete before letting h/w 2339 * know there are new descriptors to fetch. (Only 2340 * applicable for weak-ordered memory model archs, 2341 * such as IA-64). 2342 */ 2343 wmb(); 2344 writel(i, ring->tail); 2345 } 2346 2347 return ok; 2348 } 2349 2350 /* This function requires __netif_tx_lock is held by the caller. */ 2351 static int igc_xdp_init_tx_descriptor(struct igc_ring *ring, 2352 struct xdp_frame *xdpf) 2353 { 2354 struct skb_shared_info *sinfo = xdp_get_shared_info_from_frame(xdpf); 2355 u8 nr_frags = unlikely(xdp_frame_has_frags(xdpf)) ? sinfo->nr_frags : 0; 2356 u16 count, index = ring->next_to_use; 2357 struct igc_tx_buffer *head = &ring->tx_buffer_info[index]; 2358 struct igc_tx_buffer *buffer = head; 2359 union igc_adv_tx_desc *desc = IGC_TX_DESC(ring, index); 2360 u32 olinfo_status, len = xdpf->len, cmd_type; 2361 void *data = xdpf->data; 2362 u16 i; 2363 2364 count = TXD_USE_COUNT(len); 2365 for (i = 0; i < nr_frags; i++) 2366 count += TXD_USE_COUNT(skb_frag_size(&sinfo->frags[i])); 2367 2368 if (igc_maybe_stop_tx(ring, count + 3)) { 2369 /* this is a hard error */ 2370 return -EBUSY; 2371 } 2372 2373 i = 0; 2374 head->bytecount = xdp_get_frame_len(xdpf); 2375 head->type = IGC_TX_BUFFER_TYPE_XDP; 2376 head->gso_segs = 1; 2377 head->xdpf = xdpf; 2378 2379 olinfo_status = head->bytecount << IGC_ADVTXD_PAYLEN_SHIFT; 2380 desc->read.olinfo_status = cpu_to_le32(olinfo_status); 2381 2382 for (;;) { 2383 dma_addr_t dma; 2384 2385 dma = dma_map_single(ring->dev, data, len, DMA_TO_DEVICE); 2386 if (dma_mapping_error(ring->dev, dma)) { 2387 netdev_err_once(ring->netdev, 2388 "Failed to map DMA for TX\n"); 2389 goto unmap; 2390 } 2391 2392 dma_unmap_len_set(buffer, len, len); 2393 dma_unmap_addr_set(buffer, dma, dma); 2394 2395 cmd_type = IGC_ADVTXD_DTYP_DATA | IGC_ADVTXD_DCMD_DEXT | 2396 IGC_ADVTXD_DCMD_IFCS | len; 2397 2398 desc->read.cmd_type_len = cpu_to_le32(cmd_type); 2399 desc->read.buffer_addr = cpu_to_le64(dma); 2400 2401 buffer->protocol = 0; 2402 2403 if (++index == ring->count) 2404 index = 0; 2405 2406 if (i == nr_frags) 2407 break; 2408 2409 buffer = &ring->tx_buffer_info[index]; 2410 desc = IGC_TX_DESC(ring, index); 2411 desc->read.olinfo_status = 0; 2412 2413 data = skb_frag_address(&sinfo->frags[i]); 2414 len = skb_frag_size(&sinfo->frags[i]); 2415 i++; 2416 } 2417 desc->read.cmd_type_len |= cpu_to_le32(IGC_TXD_DCMD); 2418 2419 netdev_tx_sent_queue(txring_txq(ring), head->bytecount); 2420 /* set the timestamp */ 2421 head->time_stamp = jiffies; 2422 /* set next_to_watch value indicating a packet is present */ 2423 head->next_to_watch = desc; 2424 ring->next_to_use = index; 2425 2426 return 0; 2427 2428 unmap: 2429 for (;;) { 2430 buffer = &ring->tx_buffer_info[index]; 2431 if (dma_unmap_len(buffer, len)) 2432 dma_unmap_page(ring->dev, 2433 dma_unmap_addr(buffer, dma), 2434 dma_unmap_len(buffer, len), 2435 DMA_TO_DEVICE); 2436 dma_unmap_len_set(buffer, len, 0); 2437 if (buffer == head) 2438 break; 2439 2440 if (!index) 2441 index += ring->count; 2442 index--; 2443 } 2444 2445 return -ENOMEM; 2446 } 2447 2448 static struct igc_ring *igc_xdp_get_tx_ring(struct igc_adapter *adapter, 2449 int cpu) 2450 { 2451 int index = cpu; 2452 2453 if (unlikely(index < 0)) 2454 index = 0; 2455 2456 while (index >= adapter->num_tx_queues) 2457 index -= adapter->num_tx_queues; 2458 2459 return adapter->tx_ring[index]; 2460 } 2461 2462 static int igc_xdp_xmit_back(struct igc_adapter *adapter, struct xdp_buff *xdp) 2463 { 2464 struct xdp_frame *xdpf = xdp_convert_buff_to_frame(xdp); 2465 int cpu = smp_processor_id(); 2466 struct netdev_queue *nq; 2467 struct igc_ring *ring; 2468 int res; 2469 2470 if (unlikely(!xdpf)) 2471 return -EFAULT; 2472 2473 ring = igc_xdp_get_tx_ring(adapter, cpu); 2474 nq = txring_txq(ring); 2475 2476 __netif_tx_lock(nq, cpu); 2477 /* Avoid transmit queue timeout since we share it with the slow path */ 2478 txq_trans_cond_update(nq); 2479 res = igc_xdp_init_tx_descriptor(ring, xdpf); 2480 __netif_tx_unlock(nq); 2481 return res; 2482 } 2483 2484 /* This function assumes rcu_read_lock() is held by the caller. */ 2485 static int __igc_xdp_run_prog(struct igc_adapter *adapter, 2486 struct bpf_prog *prog, 2487 struct xdp_buff *xdp) 2488 { 2489 u32 act = bpf_prog_run_xdp(prog, xdp); 2490 2491 switch (act) { 2492 case XDP_PASS: 2493 return IGC_XDP_PASS; 2494 case XDP_TX: 2495 if (igc_xdp_xmit_back(adapter, xdp) < 0) 2496 goto out_failure; 2497 return IGC_XDP_TX; 2498 case XDP_REDIRECT: 2499 if (xdp_do_redirect(adapter->netdev, xdp, prog) < 0) 2500 goto out_failure; 2501 return IGC_XDP_REDIRECT; 2502 break; 2503 default: 2504 bpf_warn_invalid_xdp_action(adapter->netdev, prog, act); 2505 fallthrough; 2506 case XDP_ABORTED: 2507 out_failure: 2508 trace_xdp_exception(adapter->netdev, prog, act); 2509 fallthrough; 2510 case XDP_DROP: 2511 return IGC_XDP_CONSUMED; 2512 } 2513 } 2514 2515 static int igc_xdp_run_prog(struct igc_adapter *adapter, struct xdp_buff *xdp) 2516 { 2517 struct bpf_prog *prog; 2518 int res; 2519 2520 prog = READ_ONCE(adapter->xdp_prog); 2521 if (!prog) { 2522 res = IGC_XDP_PASS; 2523 goto out; 2524 } 2525 2526 res = __igc_xdp_run_prog(adapter, prog, xdp); 2527 2528 out: 2529 return res; 2530 } 2531 2532 /* This function assumes __netif_tx_lock is held by the caller. */ 2533 static void igc_flush_tx_descriptors(struct igc_ring *ring) 2534 { 2535 /* Once tail pointer is updated, hardware can fetch the descriptors 2536 * any time so we issue a write membar here to ensure all memory 2537 * writes are complete before the tail pointer is updated. 2538 */ 2539 wmb(); 2540 writel(ring->next_to_use, ring->tail); 2541 } 2542 2543 static void igc_finalize_xdp(struct igc_adapter *adapter, int status) 2544 { 2545 int cpu = smp_processor_id(); 2546 struct netdev_queue *nq; 2547 struct igc_ring *ring; 2548 2549 if (status & IGC_XDP_TX) { 2550 ring = igc_xdp_get_tx_ring(adapter, cpu); 2551 nq = txring_txq(ring); 2552 2553 __netif_tx_lock(nq, cpu); 2554 igc_flush_tx_descriptors(ring); 2555 __netif_tx_unlock(nq); 2556 } 2557 2558 if (status & IGC_XDP_REDIRECT) 2559 xdp_do_flush(); 2560 } 2561 2562 static void igc_update_rx_stats(struct igc_q_vector *q_vector, 2563 unsigned int packets, unsigned int bytes) 2564 { 2565 struct igc_ring *ring = q_vector->rx.ring; 2566 2567 u64_stats_update_begin(&ring->rx_syncp); 2568 ring->rx_stats.packets += packets; 2569 ring->rx_stats.bytes += bytes; 2570 u64_stats_update_end(&ring->rx_syncp); 2571 2572 q_vector->rx.total_packets += packets; 2573 q_vector->rx.total_bytes += bytes; 2574 } 2575 2576 static int igc_clean_rx_irq(struct igc_q_vector *q_vector, const int budget) 2577 { 2578 unsigned int total_bytes = 0, total_packets = 0; 2579 struct igc_adapter *adapter = q_vector->adapter; 2580 struct igc_ring *rx_ring = q_vector->rx.ring; 2581 struct sk_buff *skb = rx_ring->skb; 2582 u16 cleaned_count = igc_desc_unused(rx_ring); 2583 int xdp_status = 0, rx_buffer_pgcnt; 2584 int xdp_res = 0; 2585 2586 while (likely(total_packets < budget)) { 2587 struct igc_xdp_buff ctx = { .rx_ts = NULL }; 2588 struct igc_rx_buffer *rx_buffer; 2589 union igc_adv_rx_desc *rx_desc; 2590 unsigned int size, truesize; 2591 int pkt_offset = 0; 2592 void *pktbuf; 2593 2594 /* return some buffers to hardware, one at a time is too slow */ 2595 if (cleaned_count >= IGC_RX_BUFFER_WRITE) { 2596 igc_alloc_rx_buffers(rx_ring, cleaned_count); 2597 cleaned_count = 0; 2598 } 2599 2600 rx_desc = IGC_RX_DESC(rx_ring, rx_ring->next_to_clean); 2601 size = le16_to_cpu(rx_desc->wb.upper.length); 2602 if (!size) 2603 break; 2604 2605 /* This memory barrier is needed to keep us from reading 2606 * any other fields out of the rx_desc until we know the 2607 * descriptor has been written back 2608 */ 2609 dma_rmb(); 2610 2611 rx_buffer = igc_get_rx_buffer(rx_ring, size, &rx_buffer_pgcnt); 2612 truesize = igc_get_rx_frame_truesize(rx_ring, size); 2613 2614 pktbuf = page_address(rx_buffer->page) + rx_buffer->page_offset; 2615 2616 if (igc_test_staterr(rx_desc, IGC_RXDADV_STAT_TSIP)) { 2617 ctx.rx_ts = pktbuf; 2618 pkt_offset = IGC_TS_HDR_LEN; 2619 size -= IGC_TS_HDR_LEN; 2620 } 2621 2622 if (!skb) { 2623 xdp_init_buff(&ctx.xdp, truesize, &rx_ring->xdp_rxq); 2624 xdp_prepare_buff(&ctx.xdp, pktbuf - igc_rx_offset(rx_ring), 2625 igc_rx_offset(rx_ring) + pkt_offset, 2626 size, true); 2627 xdp_buff_clear_frags_flag(&ctx.xdp); 2628 ctx.rx_desc = rx_desc; 2629 2630 xdp_res = igc_xdp_run_prog(adapter, &ctx.xdp); 2631 } 2632 2633 if (xdp_res) { 2634 switch (xdp_res) { 2635 case IGC_XDP_CONSUMED: 2636 rx_buffer->pagecnt_bias++; 2637 break; 2638 case IGC_XDP_TX: 2639 case IGC_XDP_REDIRECT: 2640 igc_rx_buffer_flip(rx_buffer, truesize); 2641 xdp_status |= xdp_res; 2642 break; 2643 } 2644 2645 total_packets++; 2646 total_bytes += size; 2647 } else if (skb) 2648 igc_add_rx_frag(rx_ring, rx_buffer, skb, size); 2649 else if (ring_uses_build_skb(rx_ring)) 2650 skb = igc_build_skb(rx_ring, rx_buffer, &ctx.xdp); 2651 else 2652 skb = igc_construct_skb(rx_ring, rx_buffer, &ctx); 2653 2654 /* exit if we failed to retrieve a buffer */ 2655 if (!xdp_res && !skb) { 2656 rx_ring->rx_stats.alloc_failed++; 2657 rx_buffer->pagecnt_bias++; 2658 set_bit(IGC_RING_FLAG_RX_ALLOC_FAILED, &rx_ring->flags); 2659 break; 2660 } 2661 2662 igc_put_rx_buffer(rx_ring, rx_buffer, rx_buffer_pgcnt); 2663 cleaned_count++; 2664 2665 /* fetch next buffer in frame if non-eop */ 2666 if (igc_is_non_eop(rx_ring, rx_desc)) 2667 continue; 2668 2669 /* verify the packet layout is correct */ 2670 if (xdp_res || igc_cleanup_headers(rx_ring, rx_desc, skb)) { 2671 skb = NULL; 2672 continue; 2673 } 2674 2675 /* probably a little skewed due to removing CRC */ 2676 total_bytes += skb->len; 2677 2678 /* populate checksum, VLAN, and protocol */ 2679 igc_process_skb_fields(rx_ring, rx_desc, skb); 2680 2681 napi_gro_receive(&q_vector->napi, skb); 2682 2683 /* reset skb pointer */ 2684 skb = NULL; 2685 2686 /* update budget accounting */ 2687 total_packets++; 2688 } 2689 2690 if (xdp_status) 2691 igc_finalize_xdp(adapter, xdp_status); 2692 2693 /* place incomplete frames back on ring for completion */ 2694 rx_ring->skb = skb; 2695 2696 igc_update_rx_stats(q_vector, total_packets, total_bytes); 2697 2698 if (cleaned_count) 2699 igc_alloc_rx_buffers(rx_ring, cleaned_count); 2700 2701 return total_packets; 2702 } 2703 2704 static struct sk_buff *igc_construct_skb_zc(struct igc_ring *ring, 2705 struct igc_xdp_buff *ctx) 2706 { 2707 struct xdp_buff *xdp = &ctx->xdp; 2708 unsigned int totalsize = xdp->data_end - xdp->data_meta; 2709 unsigned int metasize = xdp->data - xdp->data_meta; 2710 struct sk_buff *skb; 2711 2712 net_prefetch(xdp->data_meta); 2713 2714 skb = napi_alloc_skb(&ring->q_vector->napi, totalsize); 2715 if (unlikely(!skb)) 2716 return NULL; 2717 2718 memcpy(__skb_put(skb, totalsize), xdp->data_meta, 2719 ALIGN(totalsize, sizeof(long))); 2720 2721 if (metasize) { 2722 skb_metadata_set(skb, metasize); 2723 __skb_pull(skb, metasize); 2724 } 2725 2726 if (ctx->rx_ts) { 2727 skb_shinfo(skb)->tx_flags |= SKBTX_HW_TSTAMP_NETDEV; 2728 skb_hwtstamps(skb)->netdev_data = ctx->rx_ts; 2729 } 2730 2731 return skb; 2732 } 2733 2734 static void igc_dispatch_skb_zc(struct igc_q_vector *q_vector, 2735 union igc_adv_rx_desc *desc, 2736 struct igc_xdp_buff *ctx) 2737 { 2738 struct igc_ring *ring = q_vector->rx.ring; 2739 struct sk_buff *skb; 2740 2741 skb = igc_construct_skb_zc(ring, ctx); 2742 if (!skb) { 2743 ring->rx_stats.alloc_failed++; 2744 set_bit(IGC_RING_FLAG_RX_ALLOC_FAILED, &ring->flags); 2745 return; 2746 } 2747 2748 if (igc_cleanup_headers(ring, desc, skb)) 2749 return; 2750 2751 igc_process_skb_fields(ring, desc, skb); 2752 napi_gro_receive(&q_vector->napi, skb); 2753 } 2754 2755 static struct igc_xdp_buff *xsk_buff_to_igc_ctx(struct xdp_buff *xdp) 2756 { 2757 /* xdp_buff pointer used by ZC code path is alloc as xdp_buff_xsk. The 2758 * igc_xdp_buff shares its layout with xdp_buff_xsk and private 2759 * igc_xdp_buff fields fall into xdp_buff_xsk->cb 2760 */ 2761 return (struct igc_xdp_buff *)xdp; 2762 } 2763 2764 static int igc_clean_rx_irq_zc(struct igc_q_vector *q_vector, const int budget) 2765 { 2766 struct igc_adapter *adapter = q_vector->adapter; 2767 struct igc_ring *ring = q_vector->rx.ring; 2768 u16 cleaned_count = igc_desc_unused(ring); 2769 int total_bytes = 0, total_packets = 0; 2770 u16 ntc = ring->next_to_clean; 2771 struct bpf_prog *prog; 2772 bool failure = false; 2773 int xdp_status = 0; 2774 2775 rcu_read_lock(); 2776 2777 prog = READ_ONCE(adapter->xdp_prog); 2778 2779 while (likely(total_packets < budget)) { 2780 union igc_adv_rx_desc *desc; 2781 struct igc_rx_buffer *bi; 2782 struct igc_xdp_buff *ctx; 2783 unsigned int size; 2784 int res; 2785 2786 desc = IGC_RX_DESC(ring, ntc); 2787 size = le16_to_cpu(desc->wb.upper.length); 2788 if (!size) 2789 break; 2790 2791 /* This memory barrier is needed to keep us from reading 2792 * any other fields out of the rx_desc until we know the 2793 * descriptor has been written back 2794 */ 2795 dma_rmb(); 2796 2797 bi = &ring->rx_buffer_info[ntc]; 2798 2799 ctx = xsk_buff_to_igc_ctx(bi->xdp); 2800 ctx->rx_desc = desc; 2801 2802 if (igc_test_staterr(desc, IGC_RXDADV_STAT_TSIP)) { 2803 ctx->rx_ts = bi->xdp->data; 2804 2805 bi->xdp->data += IGC_TS_HDR_LEN; 2806 2807 /* HW timestamp has been copied into local variable. Metadata 2808 * length when XDP program is called should be 0. 2809 */ 2810 bi->xdp->data_meta += IGC_TS_HDR_LEN; 2811 size -= IGC_TS_HDR_LEN; 2812 } else { 2813 ctx->rx_ts = NULL; 2814 } 2815 2816 bi->xdp->data_end = bi->xdp->data + size; 2817 xsk_buff_dma_sync_for_cpu(bi->xdp); 2818 2819 res = __igc_xdp_run_prog(adapter, prog, bi->xdp); 2820 switch (res) { 2821 case IGC_XDP_PASS: 2822 igc_dispatch_skb_zc(q_vector, desc, ctx); 2823 fallthrough; 2824 case IGC_XDP_CONSUMED: 2825 xsk_buff_free(bi->xdp); 2826 break; 2827 case IGC_XDP_TX: 2828 case IGC_XDP_REDIRECT: 2829 xdp_status |= res; 2830 break; 2831 } 2832 2833 bi->xdp = NULL; 2834 total_bytes += size; 2835 total_packets++; 2836 cleaned_count++; 2837 ntc++; 2838 if (ntc == ring->count) 2839 ntc = 0; 2840 } 2841 2842 ring->next_to_clean = ntc; 2843 rcu_read_unlock(); 2844 2845 if (cleaned_count >= IGC_RX_BUFFER_WRITE) 2846 failure = !igc_alloc_rx_buffers_zc(ring, cleaned_count); 2847 2848 if (xdp_status) 2849 igc_finalize_xdp(adapter, xdp_status); 2850 2851 igc_update_rx_stats(q_vector, total_packets, total_bytes); 2852 2853 if (xsk_uses_need_wakeup(ring->xsk_pool)) { 2854 if (failure || ring->next_to_clean == ring->next_to_use) 2855 xsk_set_rx_need_wakeup(ring->xsk_pool); 2856 else 2857 xsk_clear_rx_need_wakeup(ring->xsk_pool); 2858 return total_packets; 2859 } 2860 2861 return failure ? budget : total_packets; 2862 } 2863 2864 static void igc_update_tx_stats(struct igc_q_vector *q_vector, 2865 unsigned int packets, unsigned int bytes) 2866 { 2867 struct igc_ring *ring = q_vector->tx.ring; 2868 2869 u64_stats_update_begin(&ring->tx_syncp); 2870 ring->tx_stats.bytes += bytes; 2871 ring->tx_stats.packets += packets; 2872 u64_stats_update_end(&ring->tx_syncp); 2873 2874 q_vector->tx.total_bytes += bytes; 2875 q_vector->tx.total_packets += packets; 2876 } 2877 2878 static void igc_xsk_request_timestamp(void *_priv) 2879 { 2880 struct igc_metadata_request *meta_req = _priv; 2881 struct igc_ring *tx_ring = meta_req->tx_ring; 2882 struct igc_tx_timestamp_request *tstamp; 2883 u32 tx_flags = IGC_TX_FLAGS_TSTAMP; 2884 struct igc_adapter *adapter; 2885 unsigned long lock_flags; 2886 bool found = false; 2887 int i; 2888 2889 if (test_bit(IGC_RING_FLAG_TX_HWTSTAMP, &tx_ring->flags)) { 2890 adapter = netdev_priv(tx_ring->netdev); 2891 2892 spin_lock_irqsave(&adapter->ptp_tx_lock, lock_flags); 2893 2894 /* Search for available tstamp regs */ 2895 for (i = 0; i < IGC_MAX_TX_TSTAMP_REGS; i++) { 2896 tstamp = &adapter->tx_tstamp[i]; 2897 2898 /* tstamp->skb and tstamp->xsk_tx_buffer are in union. 2899 * When tstamp->skb is equal to NULL, 2900 * tstamp->xsk_tx_buffer is equal to NULL as well. 2901 * This condition means that the particular tstamp reg 2902 * is not occupied by other packet. 2903 */ 2904 if (!tstamp->skb) { 2905 found = true; 2906 break; 2907 } 2908 } 2909 2910 /* Return if no available tstamp regs */ 2911 if (!found) { 2912 adapter->tx_hwtstamp_skipped++; 2913 spin_unlock_irqrestore(&adapter->ptp_tx_lock, 2914 lock_flags); 2915 return; 2916 } 2917 2918 tstamp->start = jiffies; 2919 tstamp->xsk_queue_index = tx_ring->queue_index; 2920 tstamp->xsk_tx_buffer = meta_req->tx_buffer; 2921 tstamp->buffer_type = IGC_TX_BUFFER_TYPE_XSK; 2922 2923 /* Hold the transmit completion until timestamp is ready */ 2924 meta_req->tx_buffer->xsk_pending_ts = true; 2925 2926 /* Keep the pointer to tx_timestamp, which is located in XDP 2927 * metadata area. It is the location to store the value of 2928 * tx hardware timestamp. 2929 */ 2930 xsk_tx_metadata_to_compl(meta_req->meta, &tstamp->xsk_meta); 2931 2932 /* Set timestamp bit based on the _TSTAMP(_X) bit. */ 2933 tx_flags |= tstamp->flags; 2934 meta_req->cmd_type |= IGC_SET_FLAG(tx_flags, 2935 IGC_TX_FLAGS_TSTAMP, 2936 (IGC_ADVTXD_MAC_TSTAMP)); 2937 meta_req->cmd_type |= IGC_SET_FLAG(tx_flags, 2938 IGC_TX_FLAGS_TSTAMP_1, 2939 (IGC_ADVTXD_TSTAMP_REG_1)); 2940 meta_req->cmd_type |= IGC_SET_FLAG(tx_flags, 2941 IGC_TX_FLAGS_TSTAMP_2, 2942 (IGC_ADVTXD_TSTAMP_REG_2)); 2943 meta_req->cmd_type |= IGC_SET_FLAG(tx_flags, 2944 IGC_TX_FLAGS_TSTAMP_3, 2945 (IGC_ADVTXD_TSTAMP_REG_3)); 2946 2947 spin_unlock_irqrestore(&adapter->ptp_tx_lock, lock_flags); 2948 } 2949 } 2950 2951 static u64 igc_xsk_fill_timestamp(void *_priv) 2952 { 2953 return *(u64 *)_priv; 2954 } 2955 2956 const struct xsk_tx_metadata_ops igc_xsk_tx_metadata_ops = { 2957 .tmo_request_timestamp = igc_xsk_request_timestamp, 2958 .tmo_fill_timestamp = igc_xsk_fill_timestamp, 2959 }; 2960 2961 static void igc_xdp_xmit_zc(struct igc_ring *ring) 2962 { 2963 struct xsk_buff_pool *pool = ring->xsk_pool; 2964 struct netdev_queue *nq = txring_txq(ring); 2965 union igc_adv_tx_desc *tx_desc = NULL; 2966 int cpu = smp_processor_id(); 2967 struct xdp_desc xdp_desc; 2968 u16 budget, ntu; 2969 2970 if (!netif_carrier_ok(ring->netdev)) 2971 return; 2972 2973 __netif_tx_lock(nq, cpu); 2974 2975 /* Avoid transmit queue timeout since we share it with the slow path */ 2976 txq_trans_cond_update(nq); 2977 2978 ntu = ring->next_to_use; 2979 budget = igc_desc_unused(ring); 2980 2981 while (xsk_tx_peek_desc(pool, &xdp_desc) && budget--) { 2982 struct igc_metadata_request meta_req; 2983 struct xsk_tx_metadata *meta = NULL; 2984 struct igc_tx_buffer *bi; 2985 u32 olinfo_status; 2986 dma_addr_t dma; 2987 2988 meta_req.cmd_type = IGC_ADVTXD_DTYP_DATA | 2989 IGC_ADVTXD_DCMD_DEXT | 2990 IGC_ADVTXD_DCMD_IFCS | 2991 IGC_TXD_DCMD | xdp_desc.len; 2992 olinfo_status = xdp_desc.len << IGC_ADVTXD_PAYLEN_SHIFT; 2993 2994 dma = xsk_buff_raw_get_dma(pool, xdp_desc.addr); 2995 meta = xsk_buff_get_metadata(pool, xdp_desc.addr); 2996 xsk_buff_raw_dma_sync_for_device(pool, dma, xdp_desc.len); 2997 bi = &ring->tx_buffer_info[ntu]; 2998 2999 meta_req.tx_ring = ring; 3000 meta_req.tx_buffer = bi; 3001 meta_req.meta = meta; 3002 xsk_tx_metadata_request(meta, &igc_xsk_tx_metadata_ops, 3003 &meta_req); 3004 3005 tx_desc = IGC_TX_DESC(ring, ntu); 3006 tx_desc->read.cmd_type_len = cpu_to_le32(meta_req.cmd_type); 3007 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status); 3008 tx_desc->read.buffer_addr = cpu_to_le64(dma); 3009 3010 bi->type = IGC_TX_BUFFER_TYPE_XSK; 3011 bi->protocol = 0; 3012 bi->bytecount = xdp_desc.len; 3013 bi->gso_segs = 1; 3014 bi->time_stamp = jiffies; 3015 bi->next_to_watch = tx_desc; 3016 3017 netdev_tx_sent_queue(txring_txq(ring), xdp_desc.len); 3018 3019 ntu++; 3020 if (ntu == ring->count) 3021 ntu = 0; 3022 } 3023 3024 ring->next_to_use = ntu; 3025 if (tx_desc) { 3026 igc_flush_tx_descriptors(ring); 3027 xsk_tx_release(pool); 3028 } 3029 3030 __netif_tx_unlock(nq); 3031 } 3032 3033 /** 3034 * igc_clean_tx_irq - Reclaim resources after transmit completes 3035 * @q_vector: pointer to q_vector containing needed info 3036 * @napi_budget: Used to determine if we are in netpoll 3037 * 3038 * returns true if ring is completely cleaned 3039 */ 3040 static bool igc_clean_tx_irq(struct igc_q_vector *q_vector, int napi_budget) 3041 { 3042 struct igc_adapter *adapter = q_vector->adapter; 3043 unsigned int total_bytes = 0, total_packets = 0; 3044 unsigned int budget = q_vector->tx.work_limit; 3045 struct igc_ring *tx_ring = q_vector->tx.ring; 3046 unsigned int i = tx_ring->next_to_clean; 3047 struct igc_tx_buffer *tx_buffer; 3048 union igc_adv_tx_desc *tx_desc; 3049 u32 xsk_frames = 0; 3050 3051 if (test_bit(__IGC_DOWN, &adapter->state)) 3052 return true; 3053 3054 tx_buffer = &tx_ring->tx_buffer_info[i]; 3055 tx_desc = IGC_TX_DESC(tx_ring, i); 3056 i -= tx_ring->count; 3057 3058 do { 3059 union igc_adv_tx_desc *eop_desc = tx_buffer->next_to_watch; 3060 3061 /* if next_to_watch is not set then there is no work pending */ 3062 if (!eop_desc) 3063 break; 3064 3065 /* prevent any other reads prior to eop_desc */ 3066 smp_rmb(); 3067 3068 /* if DD is not set pending work has not been completed */ 3069 if (!(eop_desc->wb.status & cpu_to_le32(IGC_TXD_STAT_DD))) 3070 break; 3071 3072 /* Hold the completions while there's a pending tx hardware 3073 * timestamp request from XDP Tx metadata. 3074 */ 3075 if (tx_buffer->type == IGC_TX_BUFFER_TYPE_XSK && 3076 tx_buffer->xsk_pending_ts) 3077 break; 3078 3079 /* clear next_to_watch to prevent false hangs */ 3080 tx_buffer->next_to_watch = NULL; 3081 3082 /* update the statistics for this packet */ 3083 total_bytes += tx_buffer->bytecount; 3084 total_packets += tx_buffer->gso_segs; 3085 3086 switch (tx_buffer->type) { 3087 case IGC_TX_BUFFER_TYPE_XSK: 3088 xsk_frames++; 3089 break; 3090 case IGC_TX_BUFFER_TYPE_XDP: 3091 xdp_return_frame(tx_buffer->xdpf); 3092 igc_unmap_tx_buffer(tx_ring->dev, tx_buffer); 3093 break; 3094 case IGC_TX_BUFFER_TYPE_SKB: 3095 napi_consume_skb(tx_buffer->skb, napi_budget); 3096 igc_unmap_tx_buffer(tx_ring->dev, tx_buffer); 3097 break; 3098 default: 3099 netdev_warn_once(tx_ring->netdev, "Unknown Tx buffer type\n"); 3100 break; 3101 } 3102 3103 /* clear last DMA location and unmap remaining buffers */ 3104 while (tx_desc != eop_desc) { 3105 tx_buffer++; 3106 tx_desc++; 3107 i++; 3108 if (unlikely(!i)) { 3109 i -= tx_ring->count; 3110 tx_buffer = tx_ring->tx_buffer_info; 3111 tx_desc = IGC_TX_DESC(tx_ring, 0); 3112 } 3113 3114 /* unmap any remaining paged data */ 3115 if (dma_unmap_len(tx_buffer, len)) 3116 igc_unmap_tx_buffer(tx_ring->dev, tx_buffer); 3117 } 3118 3119 /* move us one more past the eop_desc for start of next pkt */ 3120 tx_buffer++; 3121 tx_desc++; 3122 i++; 3123 if (unlikely(!i)) { 3124 i -= tx_ring->count; 3125 tx_buffer = tx_ring->tx_buffer_info; 3126 tx_desc = IGC_TX_DESC(tx_ring, 0); 3127 } 3128 3129 /* issue prefetch for next Tx descriptor */ 3130 prefetch(tx_desc); 3131 3132 /* update budget accounting */ 3133 budget--; 3134 } while (likely(budget)); 3135 3136 netdev_tx_completed_queue(txring_txq(tx_ring), 3137 total_packets, total_bytes); 3138 3139 i += tx_ring->count; 3140 tx_ring->next_to_clean = i; 3141 3142 igc_update_tx_stats(q_vector, total_packets, total_bytes); 3143 3144 if (tx_ring->xsk_pool) { 3145 if (xsk_frames) 3146 xsk_tx_completed(tx_ring->xsk_pool, xsk_frames); 3147 if (xsk_uses_need_wakeup(tx_ring->xsk_pool)) 3148 xsk_set_tx_need_wakeup(tx_ring->xsk_pool); 3149 igc_xdp_xmit_zc(tx_ring); 3150 } 3151 3152 if (test_bit(IGC_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags)) { 3153 struct igc_hw *hw = &adapter->hw; 3154 3155 /* Detect a transmit hang in hardware, this serializes the 3156 * check with the clearing of time_stamp and movement of i 3157 */ 3158 clear_bit(IGC_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags); 3159 if (tx_buffer->next_to_watch && 3160 time_after(jiffies, tx_buffer->time_stamp + 3161 (adapter->tx_timeout_factor * HZ)) && 3162 !(rd32(IGC_STATUS) & IGC_STATUS_TXOFF) && 3163 (rd32(IGC_TDH(tx_ring->reg_idx)) != readl(tx_ring->tail)) && 3164 !tx_ring->oper_gate_closed) { 3165 /* detected Tx unit hang */ 3166 netdev_err(tx_ring->netdev, 3167 "Detected Tx Unit Hang\n" 3168 " Tx Queue <%d>\n" 3169 " TDH <%x>\n" 3170 " TDT <%x>\n" 3171 " next_to_use <%x>\n" 3172 " next_to_clean <%x>\n" 3173 "buffer_info[next_to_clean]\n" 3174 " time_stamp <%lx>\n" 3175 " next_to_watch <%p>\n" 3176 " jiffies <%lx>\n" 3177 " desc.status <%x>\n", 3178 tx_ring->queue_index, 3179 rd32(IGC_TDH(tx_ring->reg_idx)), 3180 readl(tx_ring->tail), 3181 tx_ring->next_to_use, 3182 tx_ring->next_to_clean, 3183 tx_buffer->time_stamp, 3184 tx_buffer->next_to_watch, 3185 jiffies, 3186 tx_buffer->next_to_watch->wb.status); 3187 netif_stop_subqueue(tx_ring->netdev, 3188 tx_ring->queue_index); 3189 3190 /* we are about to reset, no point in enabling stuff */ 3191 return true; 3192 } 3193 } 3194 3195 #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2) 3196 if (unlikely(total_packets && 3197 netif_carrier_ok(tx_ring->netdev) && 3198 igc_desc_unused(tx_ring) >= TX_WAKE_THRESHOLD)) { 3199 /* Make sure that anybody stopping the queue after this 3200 * sees the new next_to_clean. 3201 */ 3202 smp_mb(); 3203 if (__netif_subqueue_stopped(tx_ring->netdev, 3204 tx_ring->queue_index) && 3205 !(test_bit(__IGC_DOWN, &adapter->state))) { 3206 netif_wake_subqueue(tx_ring->netdev, 3207 tx_ring->queue_index); 3208 3209 u64_stats_update_begin(&tx_ring->tx_syncp); 3210 tx_ring->tx_stats.restart_queue++; 3211 u64_stats_update_end(&tx_ring->tx_syncp); 3212 } 3213 } 3214 3215 return !!budget; 3216 } 3217 3218 static int igc_find_mac_filter(struct igc_adapter *adapter, 3219 enum igc_mac_filter_type type, const u8 *addr) 3220 { 3221 struct igc_hw *hw = &adapter->hw; 3222 int max_entries = hw->mac.rar_entry_count; 3223 u32 ral, rah; 3224 int i; 3225 3226 for (i = 0; i < max_entries; i++) { 3227 ral = rd32(IGC_RAL(i)); 3228 rah = rd32(IGC_RAH(i)); 3229 3230 if (!(rah & IGC_RAH_AV)) 3231 continue; 3232 if (!!(rah & IGC_RAH_ASEL_SRC_ADDR) != type) 3233 continue; 3234 if ((rah & IGC_RAH_RAH_MASK) != 3235 le16_to_cpup((__le16 *)(addr + 4))) 3236 continue; 3237 if (ral != le32_to_cpup((__le32 *)(addr))) 3238 continue; 3239 3240 return i; 3241 } 3242 3243 return -1; 3244 } 3245 3246 static int igc_get_avail_mac_filter_slot(struct igc_adapter *adapter) 3247 { 3248 struct igc_hw *hw = &adapter->hw; 3249 int max_entries = hw->mac.rar_entry_count; 3250 u32 rah; 3251 int i; 3252 3253 for (i = 0; i < max_entries; i++) { 3254 rah = rd32(IGC_RAH(i)); 3255 3256 if (!(rah & IGC_RAH_AV)) 3257 return i; 3258 } 3259 3260 return -1; 3261 } 3262 3263 /** 3264 * igc_add_mac_filter() - Add MAC address filter 3265 * @adapter: Pointer to adapter where the filter should be added 3266 * @type: MAC address filter type (source or destination) 3267 * @addr: MAC address 3268 * @queue: If non-negative, queue assignment feature is enabled and frames 3269 * matching the filter are enqueued onto 'queue'. Otherwise, queue 3270 * assignment is disabled. 3271 * 3272 * Return: 0 in case of success, negative errno code otherwise. 3273 */ 3274 static int igc_add_mac_filter(struct igc_adapter *adapter, 3275 enum igc_mac_filter_type type, const u8 *addr, 3276 int queue) 3277 { 3278 struct net_device *dev = adapter->netdev; 3279 int index; 3280 3281 index = igc_find_mac_filter(adapter, type, addr); 3282 if (index >= 0) 3283 goto update_filter; 3284 3285 index = igc_get_avail_mac_filter_slot(adapter); 3286 if (index < 0) 3287 return -ENOSPC; 3288 3289 netdev_dbg(dev, "Add MAC address filter: index %d type %s address %pM queue %d\n", 3290 index, type == IGC_MAC_FILTER_TYPE_DST ? "dst" : "src", 3291 addr, queue); 3292 3293 update_filter: 3294 igc_set_mac_filter_hw(adapter, index, type, addr, queue); 3295 return 0; 3296 } 3297 3298 /** 3299 * igc_del_mac_filter() - Delete MAC address filter 3300 * @adapter: Pointer to adapter where the filter should be deleted from 3301 * @type: MAC address filter type (source or destination) 3302 * @addr: MAC address 3303 */ 3304 static void igc_del_mac_filter(struct igc_adapter *adapter, 3305 enum igc_mac_filter_type type, const u8 *addr) 3306 { 3307 struct net_device *dev = adapter->netdev; 3308 int index; 3309 3310 index = igc_find_mac_filter(adapter, type, addr); 3311 if (index < 0) 3312 return; 3313 3314 if (index == 0) { 3315 /* If this is the default filter, we don't actually delete it. 3316 * We just reset to its default value i.e. disable queue 3317 * assignment. 3318 */ 3319 netdev_dbg(dev, "Disable default MAC filter queue assignment"); 3320 3321 igc_set_mac_filter_hw(adapter, 0, type, addr, -1); 3322 } else { 3323 netdev_dbg(dev, "Delete MAC address filter: index %d type %s address %pM\n", 3324 index, 3325 type == IGC_MAC_FILTER_TYPE_DST ? "dst" : "src", 3326 addr); 3327 3328 igc_clear_mac_filter_hw(adapter, index); 3329 } 3330 } 3331 3332 /** 3333 * igc_add_vlan_prio_filter() - Add VLAN priority filter 3334 * @adapter: Pointer to adapter where the filter should be added 3335 * @prio: VLAN priority value 3336 * @queue: Queue number which matching frames are assigned to 3337 * 3338 * Return: 0 in case of success, negative errno code otherwise. 3339 */ 3340 static int igc_add_vlan_prio_filter(struct igc_adapter *adapter, int prio, 3341 int queue) 3342 { 3343 struct net_device *dev = adapter->netdev; 3344 struct igc_hw *hw = &adapter->hw; 3345 u32 vlanpqf; 3346 3347 vlanpqf = rd32(IGC_VLANPQF); 3348 3349 if (vlanpqf & IGC_VLANPQF_VALID(prio)) { 3350 netdev_dbg(dev, "VLAN priority filter already in use\n"); 3351 return -EEXIST; 3352 } 3353 3354 vlanpqf |= IGC_VLANPQF_QSEL(prio, queue); 3355 vlanpqf |= IGC_VLANPQF_VALID(prio); 3356 3357 wr32(IGC_VLANPQF, vlanpqf); 3358 3359 netdev_dbg(dev, "Add VLAN priority filter: prio %d queue %d\n", 3360 prio, queue); 3361 return 0; 3362 } 3363 3364 /** 3365 * igc_del_vlan_prio_filter() - Delete VLAN priority filter 3366 * @adapter: Pointer to adapter where the filter should be deleted from 3367 * @prio: VLAN priority value 3368 */ 3369 static void igc_del_vlan_prio_filter(struct igc_adapter *adapter, int prio) 3370 { 3371 struct igc_hw *hw = &adapter->hw; 3372 u32 vlanpqf; 3373 3374 vlanpqf = rd32(IGC_VLANPQF); 3375 3376 vlanpqf &= ~IGC_VLANPQF_VALID(prio); 3377 vlanpqf &= ~IGC_VLANPQF_QSEL(prio, IGC_VLANPQF_QUEUE_MASK); 3378 3379 wr32(IGC_VLANPQF, vlanpqf); 3380 3381 netdev_dbg(adapter->netdev, "Delete VLAN priority filter: prio %d\n", 3382 prio); 3383 } 3384 3385 static int igc_get_avail_etype_filter_slot(struct igc_adapter *adapter) 3386 { 3387 struct igc_hw *hw = &adapter->hw; 3388 int i; 3389 3390 for (i = 0; i < MAX_ETYPE_FILTER; i++) { 3391 u32 etqf = rd32(IGC_ETQF(i)); 3392 3393 if (!(etqf & IGC_ETQF_FILTER_ENABLE)) 3394 return i; 3395 } 3396 3397 return -1; 3398 } 3399 3400 /** 3401 * igc_add_etype_filter() - Add ethertype filter 3402 * @adapter: Pointer to adapter where the filter should be added 3403 * @etype: Ethertype value 3404 * @queue: If non-negative, queue assignment feature is enabled and frames 3405 * matching the filter are enqueued onto 'queue'. Otherwise, queue 3406 * assignment is disabled. 3407 * 3408 * Return: 0 in case of success, negative errno code otherwise. 3409 */ 3410 static int igc_add_etype_filter(struct igc_adapter *adapter, u16 etype, 3411 int queue) 3412 { 3413 struct igc_hw *hw = &adapter->hw; 3414 int index; 3415 u32 etqf; 3416 3417 index = igc_get_avail_etype_filter_slot(adapter); 3418 if (index < 0) 3419 return -ENOSPC; 3420 3421 etqf = rd32(IGC_ETQF(index)); 3422 3423 etqf &= ~IGC_ETQF_ETYPE_MASK; 3424 etqf |= etype; 3425 3426 if (queue >= 0) { 3427 etqf &= ~IGC_ETQF_QUEUE_MASK; 3428 etqf |= (queue << IGC_ETQF_QUEUE_SHIFT); 3429 etqf |= IGC_ETQF_QUEUE_ENABLE; 3430 } 3431 3432 etqf |= IGC_ETQF_FILTER_ENABLE; 3433 3434 wr32(IGC_ETQF(index), etqf); 3435 3436 netdev_dbg(adapter->netdev, "Add ethertype filter: etype %04x queue %d\n", 3437 etype, queue); 3438 return 0; 3439 } 3440 3441 static int igc_find_etype_filter(struct igc_adapter *adapter, u16 etype) 3442 { 3443 struct igc_hw *hw = &adapter->hw; 3444 int i; 3445 3446 for (i = 0; i < MAX_ETYPE_FILTER; i++) { 3447 u32 etqf = rd32(IGC_ETQF(i)); 3448 3449 if ((etqf & IGC_ETQF_ETYPE_MASK) == etype) 3450 return i; 3451 } 3452 3453 return -1; 3454 } 3455 3456 /** 3457 * igc_del_etype_filter() - Delete ethertype filter 3458 * @adapter: Pointer to adapter where the filter should be deleted from 3459 * @etype: Ethertype value 3460 */ 3461 static void igc_del_etype_filter(struct igc_adapter *adapter, u16 etype) 3462 { 3463 struct igc_hw *hw = &adapter->hw; 3464 int index; 3465 3466 index = igc_find_etype_filter(adapter, etype); 3467 if (index < 0) 3468 return; 3469 3470 wr32(IGC_ETQF(index), 0); 3471 3472 netdev_dbg(adapter->netdev, "Delete ethertype filter: etype %04x\n", 3473 etype); 3474 } 3475 3476 static int igc_flex_filter_select(struct igc_adapter *adapter, 3477 struct igc_flex_filter *input, 3478 u32 *fhft) 3479 { 3480 struct igc_hw *hw = &adapter->hw; 3481 u8 fhft_index; 3482 u32 fhftsl; 3483 3484 if (input->index >= MAX_FLEX_FILTER) { 3485 netdev_err(adapter->netdev, "Wrong Flex Filter index selected!\n"); 3486 return -EINVAL; 3487 } 3488 3489 /* Indirect table select register */ 3490 fhftsl = rd32(IGC_FHFTSL); 3491 fhftsl &= ~IGC_FHFTSL_FTSL_MASK; 3492 switch (input->index) { 3493 case 0 ... 7: 3494 fhftsl |= 0x00; 3495 break; 3496 case 8 ... 15: 3497 fhftsl |= 0x01; 3498 break; 3499 case 16 ... 23: 3500 fhftsl |= 0x02; 3501 break; 3502 case 24 ... 31: 3503 fhftsl |= 0x03; 3504 break; 3505 } 3506 wr32(IGC_FHFTSL, fhftsl); 3507 3508 /* Normalize index down to host table register */ 3509 fhft_index = input->index % 8; 3510 3511 *fhft = (fhft_index < 4) ? IGC_FHFT(fhft_index) : 3512 IGC_FHFT_EXT(fhft_index - 4); 3513 3514 return 0; 3515 } 3516 3517 static int igc_write_flex_filter_ll(struct igc_adapter *adapter, 3518 struct igc_flex_filter *input) 3519 { 3520 struct igc_hw *hw = &adapter->hw; 3521 u8 *data = input->data; 3522 u8 *mask = input->mask; 3523 u32 queuing; 3524 u32 fhft; 3525 u32 wufc; 3526 int ret; 3527 int i; 3528 3529 /* Length has to be aligned to 8. Otherwise the filter will fail. Bail 3530 * out early to avoid surprises later. 3531 */ 3532 if (input->length % 8 != 0) { 3533 netdev_err(adapter->netdev, "The length of a flex filter has to be 8 byte aligned!\n"); 3534 return -EINVAL; 3535 } 3536 3537 /* Select corresponding flex filter register and get base for host table. */ 3538 ret = igc_flex_filter_select(adapter, input, &fhft); 3539 if (ret) 3540 return ret; 3541 3542 /* When adding a filter globally disable flex filter feature. That is 3543 * recommended within the datasheet. 3544 */ 3545 wufc = rd32(IGC_WUFC); 3546 wufc &= ~IGC_WUFC_FLEX_HQ; 3547 wr32(IGC_WUFC, wufc); 3548 3549 /* Configure filter */ 3550 queuing = input->length & IGC_FHFT_LENGTH_MASK; 3551 queuing |= FIELD_PREP(IGC_FHFT_QUEUE_MASK, input->rx_queue); 3552 queuing |= FIELD_PREP(IGC_FHFT_PRIO_MASK, input->prio); 3553 3554 if (input->immediate_irq) 3555 queuing |= IGC_FHFT_IMM_INT; 3556 3557 if (input->drop) 3558 queuing |= IGC_FHFT_DROP; 3559 3560 wr32(fhft + 0xFC, queuing); 3561 3562 /* Write data (128 byte) and mask (128 bit) */ 3563 for (i = 0; i < 16; ++i) { 3564 const size_t data_idx = i * 8; 3565 const size_t row_idx = i * 16; 3566 u32 dw0 = 3567 (data[data_idx + 0] << 0) | 3568 (data[data_idx + 1] << 8) | 3569 (data[data_idx + 2] << 16) | 3570 (data[data_idx + 3] << 24); 3571 u32 dw1 = 3572 (data[data_idx + 4] << 0) | 3573 (data[data_idx + 5] << 8) | 3574 (data[data_idx + 6] << 16) | 3575 (data[data_idx + 7] << 24); 3576 u32 tmp; 3577 3578 /* Write row: dw0, dw1 and mask */ 3579 wr32(fhft + row_idx, dw0); 3580 wr32(fhft + row_idx + 4, dw1); 3581 3582 /* mask is only valid for MASK(7, 0) */ 3583 tmp = rd32(fhft + row_idx + 8); 3584 tmp &= ~GENMASK(7, 0); 3585 tmp |= mask[i]; 3586 wr32(fhft + row_idx + 8, tmp); 3587 } 3588 3589 /* Enable filter. */ 3590 wufc |= IGC_WUFC_FLEX_HQ; 3591 if (input->index > 8) { 3592 /* Filter 0-7 are enabled via WUFC. The other 24 filters are not. */ 3593 u32 wufc_ext = rd32(IGC_WUFC_EXT); 3594 3595 wufc_ext |= (IGC_WUFC_EXT_FLX8 << (input->index - 8)); 3596 3597 wr32(IGC_WUFC_EXT, wufc_ext); 3598 } else { 3599 wufc |= (IGC_WUFC_FLX0 << input->index); 3600 } 3601 wr32(IGC_WUFC, wufc); 3602 3603 netdev_dbg(adapter->netdev, "Added flex filter %u to HW.\n", 3604 input->index); 3605 3606 return 0; 3607 } 3608 3609 static void igc_flex_filter_add_field(struct igc_flex_filter *flex, 3610 const void *src, unsigned int offset, 3611 size_t len, const void *mask) 3612 { 3613 int i; 3614 3615 /* data */ 3616 memcpy(&flex->data[offset], src, len); 3617 3618 /* mask */ 3619 for (i = 0; i < len; ++i) { 3620 const unsigned int idx = i + offset; 3621 const u8 *ptr = mask; 3622 3623 if (mask) { 3624 if (ptr[i] & 0xff) 3625 flex->mask[idx / 8] |= BIT(idx % 8); 3626 3627 continue; 3628 } 3629 3630 flex->mask[idx / 8] |= BIT(idx % 8); 3631 } 3632 } 3633 3634 static int igc_find_avail_flex_filter_slot(struct igc_adapter *adapter) 3635 { 3636 struct igc_hw *hw = &adapter->hw; 3637 u32 wufc, wufc_ext; 3638 int i; 3639 3640 wufc = rd32(IGC_WUFC); 3641 wufc_ext = rd32(IGC_WUFC_EXT); 3642 3643 for (i = 0; i < MAX_FLEX_FILTER; i++) { 3644 if (i < 8) { 3645 if (!(wufc & (IGC_WUFC_FLX0 << i))) 3646 return i; 3647 } else { 3648 if (!(wufc_ext & (IGC_WUFC_EXT_FLX8 << (i - 8)))) 3649 return i; 3650 } 3651 } 3652 3653 return -ENOSPC; 3654 } 3655 3656 static bool igc_flex_filter_in_use(struct igc_adapter *adapter) 3657 { 3658 struct igc_hw *hw = &adapter->hw; 3659 u32 wufc, wufc_ext; 3660 3661 wufc = rd32(IGC_WUFC); 3662 wufc_ext = rd32(IGC_WUFC_EXT); 3663 3664 if (wufc & IGC_WUFC_FILTER_MASK) 3665 return true; 3666 3667 if (wufc_ext & IGC_WUFC_EXT_FILTER_MASK) 3668 return true; 3669 3670 return false; 3671 } 3672 3673 static int igc_add_flex_filter(struct igc_adapter *adapter, 3674 struct igc_nfc_rule *rule) 3675 { 3676 struct igc_nfc_filter *filter = &rule->filter; 3677 unsigned int eth_offset, user_offset; 3678 struct igc_flex_filter flex = { }; 3679 int ret, index; 3680 bool vlan; 3681 3682 index = igc_find_avail_flex_filter_slot(adapter); 3683 if (index < 0) 3684 return -ENOSPC; 3685 3686 /* Construct the flex filter: 3687 * -> dest_mac [6] 3688 * -> src_mac [6] 3689 * -> tpid [2] 3690 * -> vlan tci [2] 3691 * -> ether type [2] 3692 * -> user data [8] 3693 * -> = 26 bytes => 32 length 3694 */ 3695 flex.index = index; 3696 flex.length = 32; 3697 flex.rx_queue = rule->action; 3698 3699 vlan = rule->filter.vlan_tci || rule->filter.vlan_etype; 3700 eth_offset = vlan ? 16 : 12; 3701 user_offset = vlan ? 18 : 14; 3702 3703 /* Add destination MAC */ 3704 if (rule->filter.match_flags & IGC_FILTER_FLAG_DST_MAC_ADDR) 3705 igc_flex_filter_add_field(&flex, &filter->dst_addr, 0, 3706 ETH_ALEN, NULL); 3707 3708 /* Add source MAC */ 3709 if (rule->filter.match_flags & IGC_FILTER_FLAG_SRC_MAC_ADDR) 3710 igc_flex_filter_add_field(&flex, &filter->src_addr, 6, 3711 ETH_ALEN, NULL); 3712 3713 /* Add VLAN etype */ 3714 if (rule->filter.match_flags & IGC_FILTER_FLAG_VLAN_ETYPE) { 3715 __be16 vlan_etype = cpu_to_be16(filter->vlan_etype); 3716 3717 igc_flex_filter_add_field(&flex, &vlan_etype, 12, 3718 sizeof(vlan_etype), NULL); 3719 } 3720 3721 /* Add VLAN TCI */ 3722 if (rule->filter.match_flags & IGC_FILTER_FLAG_VLAN_TCI) 3723 igc_flex_filter_add_field(&flex, &filter->vlan_tci, 14, 3724 sizeof(filter->vlan_tci), NULL); 3725 3726 /* Add Ether type */ 3727 if (rule->filter.match_flags & IGC_FILTER_FLAG_ETHER_TYPE) { 3728 __be16 etype = cpu_to_be16(filter->etype); 3729 3730 igc_flex_filter_add_field(&flex, &etype, eth_offset, 3731 sizeof(etype), NULL); 3732 } 3733 3734 /* Add user data */ 3735 if (rule->filter.match_flags & IGC_FILTER_FLAG_USER_DATA) 3736 igc_flex_filter_add_field(&flex, &filter->user_data, 3737 user_offset, 3738 sizeof(filter->user_data), 3739 filter->user_mask); 3740 3741 /* Add it down to the hardware and enable it. */ 3742 ret = igc_write_flex_filter_ll(adapter, &flex); 3743 if (ret) 3744 return ret; 3745 3746 filter->flex_index = index; 3747 3748 return 0; 3749 } 3750 3751 static void igc_del_flex_filter(struct igc_adapter *adapter, 3752 u16 reg_index) 3753 { 3754 struct igc_hw *hw = &adapter->hw; 3755 u32 wufc; 3756 3757 /* Just disable the filter. The filter table itself is kept 3758 * intact. Another flex_filter_add() should override the "old" data 3759 * then. 3760 */ 3761 if (reg_index > 8) { 3762 u32 wufc_ext = rd32(IGC_WUFC_EXT); 3763 3764 wufc_ext &= ~(IGC_WUFC_EXT_FLX8 << (reg_index - 8)); 3765 wr32(IGC_WUFC_EXT, wufc_ext); 3766 } else { 3767 wufc = rd32(IGC_WUFC); 3768 3769 wufc &= ~(IGC_WUFC_FLX0 << reg_index); 3770 wr32(IGC_WUFC, wufc); 3771 } 3772 3773 if (igc_flex_filter_in_use(adapter)) 3774 return; 3775 3776 /* No filters are in use, we may disable flex filters */ 3777 wufc = rd32(IGC_WUFC); 3778 wufc &= ~IGC_WUFC_FLEX_HQ; 3779 wr32(IGC_WUFC, wufc); 3780 } 3781 3782 static int igc_enable_nfc_rule(struct igc_adapter *adapter, 3783 struct igc_nfc_rule *rule) 3784 { 3785 int err; 3786 3787 if (rule->flex) { 3788 return igc_add_flex_filter(adapter, rule); 3789 } 3790 3791 if (rule->filter.match_flags & IGC_FILTER_FLAG_ETHER_TYPE) { 3792 err = igc_add_etype_filter(adapter, rule->filter.etype, 3793 rule->action); 3794 if (err) 3795 return err; 3796 } 3797 3798 if (rule->filter.match_flags & IGC_FILTER_FLAG_SRC_MAC_ADDR) { 3799 err = igc_add_mac_filter(adapter, IGC_MAC_FILTER_TYPE_SRC, 3800 rule->filter.src_addr, rule->action); 3801 if (err) 3802 return err; 3803 } 3804 3805 if (rule->filter.match_flags & IGC_FILTER_FLAG_DST_MAC_ADDR) { 3806 err = igc_add_mac_filter(adapter, IGC_MAC_FILTER_TYPE_DST, 3807 rule->filter.dst_addr, rule->action); 3808 if (err) 3809 return err; 3810 } 3811 3812 if (rule->filter.match_flags & IGC_FILTER_FLAG_VLAN_TCI) { 3813 int prio = FIELD_GET(VLAN_PRIO_MASK, rule->filter.vlan_tci); 3814 3815 err = igc_add_vlan_prio_filter(adapter, prio, rule->action); 3816 if (err) 3817 return err; 3818 } 3819 3820 return 0; 3821 } 3822 3823 static void igc_disable_nfc_rule(struct igc_adapter *adapter, 3824 const struct igc_nfc_rule *rule) 3825 { 3826 if (rule->flex) { 3827 igc_del_flex_filter(adapter, rule->filter.flex_index); 3828 return; 3829 } 3830 3831 if (rule->filter.match_flags & IGC_FILTER_FLAG_ETHER_TYPE) 3832 igc_del_etype_filter(adapter, rule->filter.etype); 3833 3834 if (rule->filter.match_flags & IGC_FILTER_FLAG_VLAN_TCI) { 3835 int prio = FIELD_GET(VLAN_PRIO_MASK, rule->filter.vlan_tci); 3836 3837 igc_del_vlan_prio_filter(adapter, prio); 3838 } 3839 3840 if (rule->filter.match_flags & IGC_FILTER_FLAG_SRC_MAC_ADDR) 3841 igc_del_mac_filter(adapter, IGC_MAC_FILTER_TYPE_SRC, 3842 rule->filter.src_addr); 3843 3844 if (rule->filter.match_flags & IGC_FILTER_FLAG_DST_MAC_ADDR) 3845 igc_del_mac_filter(adapter, IGC_MAC_FILTER_TYPE_DST, 3846 rule->filter.dst_addr); 3847 } 3848 3849 /** 3850 * igc_get_nfc_rule() - Get NFC rule 3851 * @adapter: Pointer to adapter 3852 * @location: Rule location 3853 * 3854 * Context: Expects adapter->nfc_rule_lock to be held by caller. 3855 * 3856 * Return: Pointer to NFC rule at @location. If not found, NULL. 3857 */ 3858 struct igc_nfc_rule *igc_get_nfc_rule(struct igc_adapter *adapter, 3859 u32 location) 3860 { 3861 struct igc_nfc_rule *rule; 3862 3863 list_for_each_entry(rule, &adapter->nfc_rule_list, list) { 3864 if (rule->location == location) 3865 return rule; 3866 if (rule->location > location) 3867 break; 3868 } 3869 3870 return NULL; 3871 } 3872 3873 /** 3874 * igc_del_nfc_rule() - Delete NFC rule 3875 * @adapter: Pointer to adapter 3876 * @rule: Pointer to rule to be deleted 3877 * 3878 * Disable NFC rule in hardware and delete it from adapter. 3879 * 3880 * Context: Expects adapter->nfc_rule_lock to be held by caller. 3881 */ 3882 void igc_del_nfc_rule(struct igc_adapter *adapter, struct igc_nfc_rule *rule) 3883 { 3884 igc_disable_nfc_rule(adapter, rule); 3885 3886 list_del(&rule->list); 3887 adapter->nfc_rule_count--; 3888 3889 kfree(rule); 3890 } 3891 3892 static void igc_flush_nfc_rules(struct igc_adapter *adapter) 3893 { 3894 struct igc_nfc_rule *rule, *tmp; 3895 3896 mutex_lock(&adapter->nfc_rule_lock); 3897 3898 list_for_each_entry_safe(rule, tmp, &adapter->nfc_rule_list, list) 3899 igc_del_nfc_rule(adapter, rule); 3900 3901 mutex_unlock(&adapter->nfc_rule_lock); 3902 } 3903 3904 /** 3905 * igc_add_nfc_rule() - Add NFC rule 3906 * @adapter: Pointer to adapter 3907 * @rule: Pointer to rule to be added 3908 * 3909 * Enable NFC rule in hardware and add it to adapter. 3910 * 3911 * Context: Expects adapter->nfc_rule_lock to be held by caller. 3912 * 3913 * Return: 0 on success, negative errno on failure. 3914 */ 3915 int igc_add_nfc_rule(struct igc_adapter *adapter, struct igc_nfc_rule *rule) 3916 { 3917 struct igc_nfc_rule *pred, *cur; 3918 int err; 3919 3920 err = igc_enable_nfc_rule(adapter, rule); 3921 if (err) 3922 return err; 3923 3924 pred = NULL; 3925 list_for_each_entry(cur, &adapter->nfc_rule_list, list) { 3926 if (cur->location >= rule->location) 3927 break; 3928 pred = cur; 3929 } 3930 3931 list_add(&rule->list, pred ? &pred->list : &adapter->nfc_rule_list); 3932 adapter->nfc_rule_count++; 3933 return 0; 3934 } 3935 3936 static void igc_restore_nfc_rules(struct igc_adapter *adapter) 3937 { 3938 struct igc_nfc_rule *rule; 3939 3940 mutex_lock(&adapter->nfc_rule_lock); 3941 3942 list_for_each_entry_reverse(rule, &adapter->nfc_rule_list, list) 3943 igc_enable_nfc_rule(adapter, rule); 3944 3945 mutex_unlock(&adapter->nfc_rule_lock); 3946 } 3947 3948 static int igc_uc_sync(struct net_device *netdev, const unsigned char *addr) 3949 { 3950 struct igc_adapter *adapter = netdev_priv(netdev); 3951 3952 return igc_add_mac_filter(adapter, IGC_MAC_FILTER_TYPE_DST, addr, -1); 3953 } 3954 3955 static int igc_uc_unsync(struct net_device *netdev, const unsigned char *addr) 3956 { 3957 struct igc_adapter *adapter = netdev_priv(netdev); 3958 3959 igc_del_mac_filter(adapter, IGC_MAC_FILTER_TYPE_DST, addr); 3960 return 0; 3961 } 3962 3963 /** 3964 * igc_set_rx_mode - Secondary Unicast, Multicast and Promiscuous mode set 3965 * @netdev: network interface device structure 3966 * 3967 * The set_rx_mode entry point is called whenever the unicast or multicast 3968 * address lists or the network interface flags are updated. This routine is 3969 * responsible for configuring the hardware for proper unicast, multicast, 3970 * promiscuous mode, and all-multi behavior. 3971 */ 3972 static void igc_set_rx_mode(struct net_device *netdev) 3973 { 3974 struct igc_adapter *adapter = netdev_priv(netdev); 3975 struct igc_hw *hw = &adapter->hw; 3976 u32 rctl = 0, rlpml = MAX_JUMBO_FRAME_SIZE; 3977 int count; 3978 3979 /* Check for Promiscuous and All Multicast modes */ 3980 if (netdev->flags & IFF_PROMISC) { 3981 rctl |= IGC_RCTL_UPE | IGC_RCTL_MPE; 3982 } else { 3983 if (netdev->flags & IFF_ALLMULTI) { 3984 rctl |= IGC_RCTL_MPE; 3985 } else { 3986 /* Write addresses to the MTA, if the attempt fails 3987 * then we should just turn on promiscuous mode so 3988 * that we can at least receive multicast traffic 3989 */ 3990 count = igc_write_mc_addr_list(netdev); 3991 if (count < 0) 3992 rctl |= IGC_RCTL_MPE; 3993 } 3994 } 3995 3996 /* Write addresses to available RAR registers, if there is not 3997 * sufficient space to store all the addresses then enable 3998 * unicast promiscuous mode 3999 */ 4000 if (__dev_uc_sync(netdev, igc_uc_sync, igc_uc_unsync)) 4001 rctl |= IGC_RCTL_UPE; 4002 4003 /* update state of unicast and multicast */ 4004 rctl |= rd32(IGC_RCTL) & ~(IGC_RCTL_UPE | IGC_RCTL_MPE); 4005 wr32(IGC_RCTL, rctl); 4006 4007 #if (PAGE_SIZE < 8192) 4008 if (adapter->max_frame_size <= IGC_MAX_FRAME_BUILD_SKB) 4009 rlpml = IGC_MAX_FRAME_BUILD_SKB; 4010 #endif 4011 wr32(IGC_RLPML, rlpml); 4012 } 4013 4014 /** 4015 * igc_configure - configure the hardware for RX and TX 4016 * @adapter: private board structure 4017 */ 4018 static void igc_configure(struct igc_adapter *adapter) 4019 { 4020 struct net_device *netdev = adapter->netdev; 4021 int i = 0; 4022 4023 igc_get_hw_control(adapter); 4024 igc_set_rx_mode(netdev); 4025 4026 igc_restore_vlan(adapter); 4027 4028 igc_setup_tctl(adapter); 4029 igc_setup_mrqc(adapter); 4030 igc_setup_rctl(adapter); 4031 4032 igc_set_default_mac_filter(adapter); 4033 igc_restore_nfc_rules(adapter); 4034 4035 igc_configure_tx(adapter); 4036 igc_configure_rx(adapter); 4037 4038 igc_rx_fifo_flush_base(&adapter->hw); 4039 4040 /* call igc_desc_unused which always leaves 4041 * at least 1 descriptor unused to make sure 4042 * next_to_use != next_to_clean 4043 */ 4044 for (i = 0; i < adapter->num_rx_queues; i++) { 4045 struct igc_ring *ring = adapter->rx_ring[i]; 4046 4047 if (ring->xsk_pool) 4048 igc_alloc_rx_buffers_zc(ring, igc_desc_unused(ring)); 4049 else 4050 igc_alloc_rx_buffers(ring, igc_desc_unused(ring)); 4051 } 4052 } 4053 4054 /** 4055 * igc_write_ivar - configure ivar for given MSI-X vector 4056 * @hw: pointer to the HW structure 4057 * @msix_vector: vector number we are allocating to a given ring 4058 * @index: row index of IVAR register to write within IVAR table 4059 * @offset: column offset of in IVAR, should be multiple of 8 4060 * 4061 * The IVAR table consists of 2 columns, 4062 * each containing an cause allocation for an Rx and Tx ring, and a 4063 * variable number of rows depending on the number of queues supported. 4064 */ 4065 static void igc_write_ivar(struct igc_hw *hw, int msix_vector, 4066 int index, int offset) 4067 { 4068 u32 ivar = array_rd32(IGC_IVAR0, index); 4069 4070 /* clear any bits that are currently set */ 4071 ivar &= ~((u32)0xFF << offset); 4072 4073 /* write vector and valid bit */ 4074 ivar |= (msix_vector | IGC_IVAR_VALID) << offset; 4075 4076 array_wr32(IGC_IVAR0, index, ivar); 4077 } 4078 4079 static void igc_assign_vector(struct igc_q_vector *q_vector, int msix_vector) 4080 { 4081 struct igc_adapter *adapter = q_vector->adapter; 4082 struct igc_hw *hw = &adapter->hw; 4083 int rx_queue = IGC_N0_QUEUE; 4084 int tx_queue = IGC_N0_QUEUE; 4085 4086 if (q_vector->rx.ring) 4087 rx_queue = q_vector->rx.ring->reg_idx; 4088 if (q_vector->tx.ring) 4089 tx_queue = q_vector->tx.ring->reg_idx; 4090 4091 switch (hw->mac.type) { 4092 case igc_i225: 4093 if (rx_queue > IGC_N0_QUEUE) 4094 igc_write_ivar(hw, msix_vector, 4095 rx_queue >> 1, 4096 (rx_queue & 0x1) << 4); 4097 if (tx_queue > IGC_N0_QUEUE) 4098 igc_write_ivar(hw, msix_vector, 4099 tx_queue >> 1, 4100 ((tx_queue & 0x1) << 4) + 8); 4101 q_vector->eims_value = BIT(msix_vector); 4102 break; 4103 default: 4104 WARN_ONCE(hw->mac.type != igc_i225, "Wrong MAC type\n"); 4105 break; 4106 } 4107 4108 /* add q_vector eims value to global eims_enable_mask */ 4109 adapter->eims_enable_mask |= q_vector->eims_value; 4110 4111 /* configure q_vector to set itr on first interrupt */ 4112 q_vector->set_itr = 1; 4113 } 4114 4115 /** 4116 * igc_configure_msix - Configure MSI-X hardware 4117 * @adapter: Pointer to adapter structure 4118 * 4119 * igc_configure_msix sets up the hardware to properly 4120 * generate MSI-X interrupts. 4121 */ 4122 static void igc_configure_msix(struct igc_adapter *adapter) 4123 { 4124 struct igc_hw *hw = &adapter->hw; 4125 int i, vector = 0; 4126 u32 tmp; 4127 4128 adapter->eims_enable_mask = 0; 4129 4130 /* set vector for other causes, i.e. link changes */ 4131 switch (hw->mac.type) { 4132 case igc_i225: 4133 /* Turn on MSI-X capability first, or our settings 4134 * won't stick. And it will take days to debug. 4135 */ 4136 wr32(IGC_GPIE, IGC_GPIE_MSIX_MODE | 4137 IGC_GPIE_PBA | IGC_GPIE_EIAME | 4138 IGC_GPIE_NSICR); 4139 4140 /* enable msix_other interrupt */ 4141 adapter->eims_other = BIT(vector); 4142 tmp = (vector++ | IGC_IVAR_VALID) << 8; 4143 4144 wr32(IGC_IVAR_MISC, tmp); 4145 break; 4146 default: 4147 /* do nothing, since nothing else supports MSI-X */ 4148 break; 4149 } /* switch (hw->mac.type) */ 4150 4151 adapter->eims_enable_mask |= adapter->eims_other; 4152 4153 for (i = 0; i < adapter->num_q_vectors; i++) 4154 igc_assign_vector(adapter->q_vector[i], vector++); 4155 4156 wrfl(); 4157 } 4158 4159 /** 4160 * igc_irq_enable - Enable default interrupt generation settings 4161 * @adapter: board private structure 4162 */ 4163 static void igc_irq_enable(struct igc_adapter *adapter) 4164 { 4165 struct igc_hw *hw = &adapter->hw; 4166 4167 if (adapter->msix_entries) { 4168 u32 ims = IGC_IMS_LSC | IGC_IMS_DOUTSYNC | IGC_IMS_DRSTA; 4169 u32 regval = rd32(IGC_EIAC); 4170 4171 wr32(IGC_EIAC, regval | adapter->eims_enable_mask); 4172 regval = rd32(IGC_EIAM); 4173 wr32(IGC_EIAM, regval | adapter->eims_enable_mask); 4174 wr32(IGC_EIMS, adapter->eims_enable_mask); 4175 wr32(IGC_IMS, ims); 4176 } else { 4177 wr32(IGC_IMS, IMS_ENABLE_MASK | IGC_IMS_DRSTA); 4178 wr32(IGC_IAM, IMS_ENABLE_MASK | IGC_IMS_DRSTA); 4179 } 4180 } 4181 4182 /** 4183 * igc_irq_disable - Mask off interrupt generation on the NIC 4184 * @adapter: board private structure 4185 */ 4186 static void igc_irq_disable(struct igc_adapter *adapter) 4187 { 4188 struct igc_hw *hw = &adapter->hw; 4189 4190 if (adapter->msix_entries) { 4191 u32 regval = rd32(IGC_EIAM); 4192 4193 wr32(IGC_EIAM, regval & ~adapter->eims_enable_mask); 4194 wr32(IGC_EIMC, adapter->eims_enable_mask); 4195 regval = rd32(IGC_EIAC); 4196 wr32(IGC_EIAC, regval & ~adapter->eims_enable_mask); 4197 } 4198 4199 wr32(IGC_IAM, 0); 4200 wr32(IGC_IMC, ~0); 4201 wrfl(); 4202 4203 if (adapter->msix_entries) { 4204 int vector = 0, i; 4205 4206 synchronize_irq(adapter->msix_entries[vector++].vector); 4207 4208 for (i = 0; i < adapter->num_q_vectors; i++) 4209 synchronize_irq(adapter->msix_entries[vector++].vector); 4210 } else { 4211 synchronize_irq(adapter->pdev->irq); 4212 } 4213 } 4214 4215 void igc_set_flag_queue_pairs(struct igc_adapter *adapter, 4216 const u32 max_rss_queues) 4217 { 4218 /* Determine if we need to pair queues. */ 4219 /* If rss_queues > half of max_rss_queues, pair the queues in 4220 * order to conserve interrupts due to limited supply. 4221 */ 4222 if (adapter->rss_queues > (max_rss_queues / 2)) 4223 adapter->flags |= IGC_FLAG_QUEUE_PAIRS; 4224 else 4225 adapter->flags &= ~IGC_FLAG_QUEUE_PAIRS; 4226 } 4227 4228 unsigned int igc_get_max_rss_queues(struct igc_adapter *adapter) 4229 { 4230 return IGC_MAX_RX_QUEUES; 4231 } 4232 4233 static void igc_init_queue_configuration(struct igc_adapter *adapter) 4234 { 4235 u32 max_rss_queues; 4236 4237 max_rss_queues = igc_get_max_rss_queues(adapter); 4238 adapter->rss_queues = min_t(u32, max_rss_queues, num_online_cpus()); 4239 4240 igc_set_flag_queue_pairs(adapter, max_rss_queues); 4241 } 4242 4243 /** 4244 * igc_reset_q_vector - Reset config for interrupt vector 4245 * @adapter: board private structure to initialize 4246 * @v_idx: Index of vector to be reset 4247 * 4248 * If NAPI is enabled it will delete any references to the 4249 * NAPI struct. This is preparation for igc_free_q_vector. 4250 */ 4251 static void igc_reset_q_vector(struct igc_adapter *adapter, int v_idx) 4252 { 4253 struct igc_q_vector *q_vector = adapter->q_vector[v_idx]; 4254 4255 /* if we're coming from igc_set_interrupt_capability, the vectors are 4256 * not yet allocated 4257 */ 4258 if (!q_vector) 4259 return; 4260 4261 if (q_vector->tx.ring) 4262 adapter->tx_ring[q_vector->tx.ring->queue_index] = NULL; 4263 4264 if (q_vector->rx.ring) 4265 adapter->rx_ring[q_vector->rx.ring->queue_index] = NULL; 4266 4267 netif_napi_del(&q_vector->napi); 4268 } 4269 4270 /** 4271 * igc_free_q_vector - Free memory allocated for specific interrupt vector 4272 * @adapter: board private structure to initialize 4273 * @v_idx: Index of vector to be freed 4274 * 4275 * This function frees the memory allocated to the q_vector. 4276 */ 4277 static void igc_free_q_vector(struct igc_adapter *adapter, int v_idx) 4278 { 4279 struct igc_q_vector *q_vector = adapter->q_vector[v_idx]; 4280 4281 adapter->q_vector[v_idx] = NULL; 4282 4283 /* igc_get_stats64() might access the rings on this vector, 4284 * we must wait a grace period before freeing it. 4285 */ 4286 if (q_vector) 4287 kfree_rcu(q_vector, rcu); 4288 } 4289 4290 /** 4291 * igc_free_q_vectors - Free memory allocated for interrupt vectors 4292 * @adapter: board private structure to initialize 4293 * 4294 * This function frees the memory allocated to the q_vectors. In addition if 4295 * NAPI is enabled it will delete any references to the NAPI struct prior 4296 * to freeing the q_vector. 4297 */ 4298 static void igc_free_q_vectors(struct igc_adapter *adapter) 4299 { 4300 int v_idx = adapter->num_q_vectors; 4301 4302 adapter->num_tx_queues = 0; 4303 adapter->num_rx_queues = 0; 4304 adapter->num_q_vectors = 0; 4305 4306 while (v_idx--) { 4307 igc_reset_q_vector(adapter, v_idx); 4308 igc_free_q_vector(adapter, v_idx); 4309 } 4310 } 4311 4312 /** 4313 * igc_update_itr - update the dynamic ITR value based on statistics 4314 * @q_vector: pointer to q_vector 4315 * @ring_container: ring info to update the itr for 4316 * 4317 * Stores a new ITR value based on packets and byte 4318 * counts during the last interrupt. The advantage of per interrupt 4319 * computation is faster updates and more accurate ITR for the current 4320 * traffic pattern. Constants in this function were computed 4321 * based on theoretical maximum wire speed and thresholds were set based 4322 * on testing data as well as attempting to minimize response time 4323 * while increasing bulk throughput. 4324 * NOTE: These calculations are only valid when operating in a single- 4325 * queue environment. 4326 */ 4327 static void igc_update_itr(struct igc_q_vector *q_vector, 4328 struct igc_ring_container *ring_container) 4329 { 4330 unsigned int packets = ring_container->total_packets; 4331 unsigned int bytes = ring_container->total_bytes; 4332 u8 itrval = ring_container->itr; 4333 4334 /* no packets, exit with status unchanged */ 4335 if (packets == 0) 4336 return; 4337 4338 switch (itrval) { 4339 case lowest_latency: 4340 /* handle TSO and jumbo frames */ 4341 if (bytes / packets > 8000) 4342 itrval = bulk_latency; 4343 else if ((packets < 5) && (bytes > 512)) 4344 itrval = low_latency; 4345 break; 4346 case low_latency: /* 50 usec aka 20000 ints/s */ 4347 if (bytes > 10000) { 4348 /* this if handles the TSO accounting */ 4349 if (bytes / packets > 8000) 4350 itrval = bulk_latency; 4351 else if ((packets < 10) || ((bytes / packets) > 1200)) 4352 itrval = bulk_latency; 4353 else if ((packets > 35)) 4354 itrval = lowest_latency; 4355 } else if (bytes / packets > 2000) { 4356 itrval = bulk_latency; 4357 } else if (packets <= 2 && bytes < 512) { 4358 itrval = lowest_latency; 4359 } 4360 break; 4361 case bulk_latency: /* 250 usec aka 4000 ints/s */ 4362 if (bytes > 25000) { 4363 if (packets > 35) 4364 itrval = low_latency; 4365 } else if (bytes < 1500) { 4366 itrval = low_latency; 4367 } 4368 break; 4369 } 4370 4371 /* clear work counters since we have the values we need */ 4372 ring_container->total_bytes = 0; 4373 ring_container->total_packets = 0; 4374 4375 /* write updated itr to ring container */ 4376 ring_container->itr = itrval; 4377 } 4378 4379 static void igc_set_itr(struct igc_q_vector *q_vector) 4380 { 4381 struct igc_adapter *adapter = q_vector->adapter; 4382 u32 new_itr = q_vector->itr_val; 4383 u8 current_itr = 0; 4384 4385 /* for non-gigabit speeds, just fix the interrupt rate at 4000 */ 4386 switch (adapter->link_speed) { 4387 case SPEED_10: 4388 case SPEED_100: 4389 current_itr = 0; 4390 new_itr = IGC_4K_ITR; 4391 goto set_itr_now; 4392 default: 4393 break; 4394 } 4395 4396 igc_update_itr(q_vector, &q_vector->tx); 4397 igc_update_itr(q_vector, &q_vector->rx); 4398 4399 current_itr = max(q_vector->rx.itr, q_vector->tx.itr); 4400 4401 /* conservative mode (itr 3) eliminates the lowest_latency setting */ 4402 if (current_itr == lowest_latency && 4403 ((q_vector->rx.ring && adapter->rx_itr_setting == 3) || 4404 (!q_vector->rx.ring && adapter->tx_itr_setting == 3))) 4405 current_itr = low_latency; 4406 4407 switch (current_itr) { 4408 /* counts and packets in update_itr are dependent on these numbers */ 4409 case lowest_latency: 4410 new_itr = IGC_70K_ITR; /* 70,000 ints/sec */ 4411 break; 4412 case low_latency: 4413 new_itr = IGC_20K_ITR; /* 20,000 ints/sec */ 4414 break; 4415 case bulk_latency: 4416 new_itr = IGC_4K_ITR; /* 4,000 ints/sec */ 4417 break; 4418 default: 4419 break; 4420 } 4421 4422 set_itr_now: 4423 if (new_itr != q_vector->itr_val) { 4424 /* this attempts to bias the interrupt rate towards Bulk 4425 * by adding intermediate steps when interrupt rate is 4426 * increasing 4427 */ 4428 new_itr = new_itr > q_vector->itr_val ? 4429 max((new_itr * q_vector->itr_val) / 4430 (new_itr + (q_vector->itr_val >> 2)), 4431 new_itr) : new_itr; 4432 /* Don't write the value here; it resets the adapter's 4433 * internal timer, and causes us to delay far longer than 4434 * we should between interrupts. Instead, we write the ITR 4435 * value at the beginning of the next interrupt so the timing 4436 * ends up being correct. 4437 */ 4438 q_vector->itr_val = new_itr; 4439 q_vector->set_itr = 1; 4440 } 4441 } 4442 4443 static void igc_reset_interrupt_capability(struct igc_adapter *adapter) 4444 { 4445 int v_idx = adapter->num_q_vectors; 4446 4447 if (adapter->msix_entries) { 4448 pci_disable_msix(adapter->pdev); 4449 kfree(adapter->msix_entries); 4450 adapter->msix_entries = NULL; 4451 } else if (adapter->flags & IGC_FLAG_HAS_MSI) { 4452 pci_disable_msi(adapter->pdev); 4453 } 4454 4455 while (v_idx--) 4456 igc_reset_q_vector(adapter, v_idx); 4457 } 4458 4459 /** 4460 * igc_set_interrupt_capability - set MSI or MSI-X if supported 4461 * @adapter: Pointer to adapter structure 4462 * @msix: boolean value for MSI-X capability 4463 * 4464 * Attempt to configure interrupts using the best available 4465 * capabilities of the hardware and kernel. 4466 */ 4467 static void igc_set_interrupt_capability(struct igc_adapter *adapter, 4468 bool msix) 4469 { 4470 int numvecs, i; 4471 int err; 4472 4473 if (!msix) 4474 goto msi_only; 4475 adapter->flags |= IGC_FLAG_HAS_MSIX; 4476 4477 /* Number of supported queues. */ 4478 adapter->num_rx_queues = adapter->rss_queues; 4479 4480 adapter->num_tx_queues = adapter->rss_queues; 4481 4482 /* start with one vector for every Rx queue */ 4483 numvecs = adapter->num_rx_queues; 4484 4485 /* if Tx handler is separate add 1 for every Tx queue */ 4486 if (!(adapter->flags & IGC_FLAG_QUEUE_PAIRS)) 4487 numvecs += adapter->num_tx_queues; 4488 4489 /* store the number of vectors reserved for queues */ 4490 adapter->num_q_vectors = numvecs; 4491 4492 /* add 1 vector for link status interrupts */ 4493 numvecs++; 4494 4495 adapter->msix_entries = kcalloc(numvecs, sizeof(struct msix_entry), 4496 GFP_KERNEL); 4497 4498 if (!adapter->msix_entries) 4499 return; 4500 4501 /* populate entry values */ 4502 for (i = 0; i < numvecs; i++) 4503 adapter->msix_entries[i].entry = i; 4504 4505 err = pci_enable_msix_range(adapter->pdev, 4506 adapter->msix_entries, 4507 numvecs, 4508 numvecs); 4509 if (err > 0) 4510 return; 4511 4512 kfree(adapter->msix_entries); 4513 adapter->msix_entries = NULL; 4514 4515 igc_reset_interrupt_capability(adapter); 4516 4517 msi_only: 4518 adapter->flags &= ~IGC_FLAG_HAS_MSIX; 4519 4520 adapter->rss_queues = 1; 4521 adapter->flags |= IGC_FLAG_QUEUE_PAIRS; 4522 adapter->num_rx_queues = 1; 4523 adapter->num_tx_queues = 1; 4524 adapter->num_q_vectors = 1; 4525 if (!pci_enable_msi(adapter->pdev)) 4526 adapter->flags |= IGC_FLAG_HAS_MSI; 4527 } 4528 4529 /** 4530 * igc_update_ring_itr - update the dynamic ITR value based on packet size 4531 * @q_vector: pointer to q_vector 4532 * 4533 * Stores a new ITR value based on strictly on packet size. This 4534 * algorithm is less sophisticated than that used in igc_update_itr, 4535 * due to the difficulty of synchronizing statistics across multiple 4536 * receive rings. The divisors and thresholds used by this function 4537 * were determined based on theoretical maximum wire speed and testing 4538 * data, in order to minimize response time while increasing bulk 4539 * throughput. 4540 * NOTE: This function is called only when operating in a multiqueue 4541 * receive environment. 4542 */ 4543 static void igc_update_ring_itr(struct igc_q_vector *q_vector) 4544 { 4545 struct igc_adapter *adapter = q_vector->adapter; 4546 int new_val = q_vector->itr_val; 4547 int avg_wire_size = 0; 4548 unsigned int packets; 4549 4550 /* For non-gigabit speeds, just fix the interrupt rate at 4000 4551 * ints/sec - ITR timer value of 120 ticks. 4552 */ 4553 switch (adapter->link_speed) { 4554 case SPEED_10: 4555 case SPEED_100: 4556 new_val = IGC_4K_ITR; 4557 goto set_itr_val; 4558 default: 4559 break; 4560 } 4561 4562 packets = q_vector->rx.total_packets; 4563 if (packets) 4564 avg_wire_size = q_vector->rx.total_bytes / packets; 4565 4566 packets = q_vector->tx.total_packets; 4567 if (packets) 4568 avg_wire_size = max_t(u32, avg_wire_size, 4569 q_vector->tx.total_bytes / packets); 4570 4571 /* if avg_wire_size isn't set no work was done */ 4572 if (!avg_wire_size) 4573 goto clear_counts; 4574 4575 /* Add 24 bytes to size to account for CRC, preamble, and gap */ 4576 avg_wire_size += 24; 4577 4578 /* Don't starve jumbo frames */ 4579 avg_wire_size = min(avg_wire_size, 3000); 4580 4581 /* Give a little boost to mid-size frames */ 4582 if (avg_wire_size > 300 && avg_wire_size < 1200) 4583 new_val = avg_wire_size / 3; 4584 else 4585 new_val = avg_wire_size / 2; 4586 4587 /* conservative mode (itr 3) eliminates the lowest_latency setting */ 4588 if (new_val < IGC_20K_ITR && 4589 ((q_vector->rx.ring && adapter->rx_itr_setting == 3) || 4590 (!q_vector->rx.ring && adapter->tx_itr_setting == 3))) 4591 new_val = IGC_20K_ITR; 4592 4593 set_itr_val: 4594 if (new_val != q_vector->itr_val) { 4595 q_vector->itr_val = new_val; 4596 q_vector->set_itr = 1; 4597 } 4598 clear_counts: 4599 q_vector->rx.total_bytes = 0; 4600 q_vector->rx.total_packets = 0; 4601 q_vector->tx.total_bytes = 0; 4602 q_vector->tx.total_packets = 0; 4603 } 4604 4605 static void igc_ring_irq_enable(struct igc_q_vector *q_vector) 4606 { 4607 struct igc_adapter *adapter = q_vector->adapter; 4608 struct igc_hw *hw = &adapter->hw; 4609 4610 if ((q_vector->rx.ring && (adapter->rx_itr_setting & 3)) || 4611 (!q_vector->rx.ring && (adapter->tx_itr_setting & 3))) { 4612 if (adapter->num_q_vectors == 1) 4613 igc_set_itr(q_vector); 4614 else 4615 igc_update_ring_itr(q_vector); 4616 } 4617 4618 if (!test_bit(__IGC_DOWN, &adapter->state)) { 4619 if (adapter->msix_entries) 4620 wr32(IGC_EIMS, q_vector->eims_value); 4621 else 4622 igc_irq_enable(adapter); 4623 } 4624 } 4625 4626 static void igc_add_ring(struct igc_ring *ring, 4627 struct igc_ring_container *head) 4628 { 4629 head->ring = ring; 4630 head->count++; 4631 } 4632 4633 /** 4634 * igc_cache_ring_register - Descriptor ring to register mapping 4635 * @adapter: board private structure to initialize 4636 * 4637 * Once we know the feature-set enabled for the device, we'll cache 4638 * the register offset the descriptor ring is assigned to. 4639 */ 4640 static void igc_cache_ring_register(struct igc_adapter *adapter) 4641 { 4642 int i = 0, j = 0; 4643 4644 switch (adapter->hw.mac.type) { 4645 case igc_i225: 4646 default: 4647 for (; i < adapter->num_rx_queues; i++) 4648 adapter->rx_ring[i]->reg_idx = i; 4649 for (; j < adapter->num_tx_queues; j++) 4650 adapter->tx_ring[j]->reg_idx = j; 4651 break; 4652 } 4653 } 4654 4655 /** 4656 * igc_poll - NAPI Rx polling callback 4657 * @napi: napi polling structure 4658 * @budget: count of how many packets we should handle 4659 */ 4660 static int igc_poll(struct napi_struct *napi, int budget) 4661 { 4662 struct igc_q_vector *q_vector = container_of(napi, 4663 struct igc_q_vector, 4664 napi); 4665 struct igc_ring *rx_ring = q_vector->rx.ring; 4666 bool clean_complete = true; 4667 int work_done = 0; 4668 4669 if (q_vector->tx.ring) 4670 clean_complete = igc_clean_tx_irq(q_vector, budget); 4671 4672 if (rx_ring) { 4673 int cleaned = rx_ring->xsk_pool ? 4674 igc_clean_rx_irq_zc(q_vector, budget) : 4675 igc_clean_rx_irq(q_vector, budget); 4676 4677 work_done += cleaned; 4678 if (cleaned >= budget) 4679 clean_complete = false; 4680 } 4681 4682 /* If all work not completed, return budget and keep polling */ 4683 if (!clean_complete) 4684 return budget; 4685 4686 /* Exit the polling mode, but don't re-enable interrupts if stack might 4687 * poll us due to busy-polling 4688 */ 4689 if (likely(napi_complete_done(napi, work_done))) 4690 igc_ring_irq_enable(q_vector); 4691 4692 return min(work_done, budget - 1); 4693 } 4694 4695 /** 4696 * igc_alloc_q_vector - Allocate memory for a single interrupt vector 4697 * @adapter: board private structure to initialize 4698 * @v_count: q_vectors allocated on adapter, used for ring interleaving 4699 * @v_idx: index of vector in adapter struct 4700 * @txr_count: total number of Tx rings to allocate 4701 * @txr_idx: index of first Tx ring to allocate 4702 * @rxr_count: total number of Rx rings to allocate 4703 * @rxr_idx: index of first Rx ring to allocate 4704 * 4705 * We allocate one q_vector. If allocation fails we return -ENOMEM. 4706 */ 4707 static int igc_alloc_q_vector(struct igc_adapter *adapter, 4708 unsigned int v_count, unsigned int v_idx, 4709 unsigned int txr_count, unsigned int txr_idx, 4710 unsigned int rxr_count, unsigned int rxr_idx) 4711 { 4712 struct igc_q_vector *q_vector; 4713 struct igc_ring *ring; 4714 int ring_count; 4715 4716 /* igc only supports 1 Tx and/or 1 Rx queue per vector */ 4717 if (txr_count > 1 || rxr_count > 1) 4718 return -ENOMEM; 4719 4720 ring_count = txr_count + rxr_count; 4721 4722 /* allocate q_vector and rings */ 4723 q_vector = adapter->q_vector[v_idx]; 4724 if (!q_vector) 4725 q_vector = kzalloc(struct_size(q_vector, ring, ring_count), 4726 GFP_KERNEL); 4727 else 4728 memset(q_vector, 0, struct_size(q_vector, ring, ring_count)); 4729 if (!q_vector) 4730 return -ENOMEM; 4731 4732 /* initialize NAPI */ 4733 netif_napi_add(adapter->netdev, &q_vector->napi, igc_poll); 4734 4735 /* tie q_vector and adapter together */ 4736 adapter->q_vector[v_idx] = q_vector; 4737 q_vector->adapter = adapter; 4738 4739 /* initialize work limits */ 4740 q_vector->tx.work_limit = adapter->tx_work_limit; 4741 4742 /* initialize ITR configuration */ 4743 q_vector->itr_register = adapter->io_addr + IGC_EITR(0); 4744 q_vector->itr_val = IGC_START_ITR; 4745 4746 /* initialize pointer to rings */ 4747 ring = q_vector->ring; 4748 4749 /* initialize ITR */ 4750 if (rxr_count) { 4751 /* rx or rx/tx vector */ 4752 if (!adapter->rx_itr_setting || adapter->rx_itr_setting > 3) 4753 q_vector->itr_val = adapter->rx_itr_setting; 4754 } else { 4755 /* tx only vector */ 4756 if (!adapter->tx_itr_setting || adapter->tx_itr_setting > 3) 4757 q_vector->itr_val = adapter->tx_itr_setting; 4758 } 4759 4760 if (txr_count) { 4761 /* assign generic ring traits */ 4762 ring->dev = &adapter->pdev->dev; 4763 ring->netdev = adapter->netdev; 4764 4765 /* configure backlink on ring */ 4766 ring->q_vector = q_vector; 4767 4768 /* update q_vector Tx values */ 4769 igc_add_ring(ring, &q_vector->tx); 4770 4771 /* apply Tx specific ring traits */ 4772 ring->count = adapter->tx_ring_count; 4773 ring->queue_index = txr_idx; 4774 4775 /* assign ring to adapter */ 4776 adapter->tx_ring[txr_idx] = ring; 4777 4778 /* push pointer to next ring */ 4779 ring++; 4780 } 4781 4782 if (rxr_count) { 4783 /* assign generic ring traits */ 4784 ring->dev = &adapter->pdev->dev; 4785 ring->netdev = adapter->netdev; 4786 4787 /* configure backlink on ring */ 4788 ring->q_vector = q_vector; 4789 4790 /* update q_vector Rx values */ 4791 igc_add_ring(ring, &q_vector->rx); 4792 4793 /* apply Rx specific ring traits */ 4794 ring->count = adapter->rx_ring_count; 4795 ring->queue_index = rxr_idx; 4796 4797 /* assign ring to adapter */ 4798 adapter->rx_ring[rxr_idx] = ring; 4799 } 4800 4801 return 0; 4802 } 4803 4804 /** 4805 * igc_alloc_q_vectors - Allocate memory for interrupt vectors 4806 * @adapter: board private structure to initialize 4807 * 4808 * We allocate one q_vector per queue interrupt. If allocation fails we 4809 * return -ENOMEM. 4810 */ 4811 static int igc_alloc_q_vectors(struct igc_adapter *adapter) 4812 { 4813 int rxr_remaining = adapter->num_rx_queues; 4814 int txr_remaining = adapter->num_tx_queues; 4815 int rxr_idx = 0, txr_idx = 0, v_idx = 0; 4816 int q_vectors = adapter->num_q_vectors; 4817 int err; 4818 4819 if (q_vectors >= (rxr_remaining + txr_remaining)) { 4820 for (; rxr_remaining; v_idx++) { 4821 err = igc_alloc_q_vector(adapter, q_vectors, v_idx, 4822 0, 0, 1, rxr_idx); 4823 4824 if (err) 4825 goto err_out; 4826 4827 /* update counts and index */ 4828 rxr_remaining--; 4829 rxr_idx++; 4830 } 4831 } 4832 4833 for (; v_idx < q_vectors; v_idx++) { 4834 int rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - v_idx); 4835 int tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - v_idx); 4836 4837 err = igc_alloc_q_vector(adapter, q_vectors, v_idx, 4838 tqpv, txr_idx, rqpv, rxr_idx); 4839 4840 if (err) 4841 goto err_out; 4842 4843 /* update counts and index */ 4844 rxr_remaining -= rqpv; 4845 txr_remaining -= tqpv; 4846 rxr_idx++; 4847 txr_idx++; 4848 } 4849 4850 return 0; 4851 4852 err_out: 4853 adapter->num_tx_queues = 0; 4854 adapter->num_rx_queues = 0; 4855 adapter->num_q_vectors = 0; 4856 4857 while (v_idx--) 4858 igc_free_q_vector(adapter, v_idx); 4859 4860 return -ENOMEM; 4861 } 4862 4863 /** 4864 * igc_init_interrupt_scheme - initialize interrupts, allocate queues/vectors 4865 * @adapter: Pointer to adapter structure 4866 * @msix: boolean for MSI-X capability 4867 * 4868 * This function initializes the interrupts and allocates all of the queues. 4869 */ 4870 static int igc_init_interrupt_scheme(struct igc_adapter *adapter, bool msix) 4871 { 4872 struct net_device *dev = adapter->netdev; 4873 int err = 0; 4874 4875 igc_set_interrupt_capability(adapter, msix); 4876 4877 err = igc_alloc_q_vectors(adapter); 4878 if (err) { 4879 netdev_err(dev, "Unable to allocate memory for vectors\n"); 4880 goto err_alloc_q_vectors; 4881 } 4882 4883 igc_cache_ring_register(adapter); 4884 4885 return 0; 4886 4887 err_alloc_q_vectors: 4888 igc_reset_interrupt_capability(adapter); 4889 return err; 4890 } 4891 4892 /** 4893 * igc_sw_init - Initialize general software structures (struct igc_adapter) 4894 * @adapter: board private structure to initialize 4895 * 4896 * igc_sw_init initializes the Adapter private data structure. 4897 * Fields are initialized based on PCI device information and 4898 * OS network device settings (MTU size). 4899 */ 4900 static int igc_sw_init(struct igc_adapter *adapter) 4901 { 4902 struct net_device *netdev = adapter->netdev; 4903 struct pci_dev *pdev = adapter->pdev; 4904 struct igc_hw *hw = &adapter->hw; 4905 4906 pci_read_config_word(pdev, PCI_COMMAND, &hw->bus.pci_cmd_word); 4907 4908 /* set default ring sizes */ 4909 adapter->tx_ring_count = IGC_DEFAULT_TXD; 4910 adapter->rx_ring_count = IGC_DEFAULT_RXD; 4911 4912 /* set default ITR values */ 4913 adapter->rx_itr_setting = IGC_DEFAULT_ITR; 4914 adapter->tx_itr_setting = IGC_DEFAULT_ITR; 4915 4916 /* set default work limits */ 4917 adapter->tx_work_limit = IGC_DEFAULT_TX_WORK; 4918 4919 /* adjust max frame to be at least the size of a standard frame */ 4920 adapter->max_frame_size = netdev->mtu + ETH_HLEN + ETH_FCS_LEN + 4921 VLAN_HLEN; 4922 adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN; 4923 4924 mutex_init(&adapter->nfc_rule_lock); 4925 INIT_LIST_HEAD(&adapter->nfc_rule_list); 4926 adapter->nfc_rule_count = 0; 4927 4928 spin_lock_init(&adapter->stats64_lock); 4929 spin_lock_init(&adapter->qbv_tx_lock); 4930 /* Assume MSI-X interrupts, will be checked during IRQ allocation */ 4931 adapter->flags |= IGC_FLAG_HAS_MSIX; 4932 4933 igc_init_queue_configuration(adapter); 4934 4935 /* This call may decrease the number of queues */ 4936 if (igc_init_interrupt_scheme(adapter, true)) { 4937 netdev_err(netdev, "Unable to allocate memory for queues\n"); 4938 return -ENOMEM; 4939 } 4940 4941 /* Explicitly disable IRQ since the NIC can be in any state. */ 4942 igc_irq_disable(adapter); 4943 4944 set_bit(__IGC_DOWN, &adapter->state); 4945 4946 return 0; 4947 } 4948 4949 void igc_set_queue_napi(struct igc_adapter *adapter, int vector, 4950 struct napi_struct *napi) 4951 { 4952 struct igc_q_vector *q_vector = adapter->q_vector[vector]; 4953 4954 if (q_vector->rx.ring) 4955 netif_queue_set_napi(adapter->netdev, 4956 q_vector->rx.ring->queue_index, 4957 NETDEV_QUEUE_TYPE_RX, napi); 4958 4959 if (q_vector->tx.ring) 4960 netif_queue_set_napi(adapter->netdev, 4961 q_vector->tx.ring->queue_index, 4962 NETDEV_QUEUE_TYPE_TX, napi); 4963 } 4964 4965 /** 4966 * igc_up - Open the interface and prepare it to handle traffic 4967 * @adapter: board private structure 4968 */ 4969 void igc_up(struct igc_adapter *adapter) 4970 { 4971 struct igc_hw *hw = &adapter->hw; 4972 struct napi_struct *napi; 4973 int i = 0; 4974 4975 /* hardware has been reset, we need to reload some things */ 4976 igc_configure(adapter); 4977 4978 clear_bit(__IGC_DOWN, &adapter->state); 4979 4980 for (i = 0; i < adapter->num_q_vectors; i++) { 4981 napi = &adapter->q_vector[i]->napi; 4982 napi_enable(napi); 4983 igc_set_queue_napi(adapter, i, napi); 4984 } 4985 4986 if (adapter->msix_entries) 4987 igc_configure_msix(adapter); 4988 else 4989 igc_assign_vector(adapter->q_vector[0], 0); 4990 4991 /* Clear any pending interrupts. */ 4992 rd32(IGC_ICR); 4993 igc_irq_enable(adapter); 4994 4995 netif_tx_start_all_queues(adapter->netdev); 4996 4997 /* start the watchdog. */ 4998 hw->mac.get_link_status = true; 4999 schedule_work(&adapter->watchdog_task); 5000 } 5001 5002 /** 5003 * igc_update_stats - Update the board statistics counters 5004 * @adapter: board private structure 5005 */ 5006 void igc_update_stats(struct igc_adapter *adapter) 5007 { 5008 struct rtnl_link_stats64 *net_stats = &adapter->stats64; 5009 struct pci_dev *pdev = adapter->pdev; 5010 struct igc_hw *hw = &adapter->hw; 5011 u64 _bytes, _packets; 5012 u64 bytes, packets; 5013 unsigned int start; 5014 u32 mpc; 5015 int i; 5016 5017 /* Prevent stats update while adapter is being reset, or if the pci 5018 * connection is down. 5019 */ 5020 if (adapter->link_speed == 0) 5021 return; 5022 if (pci_channel_offline(pdev)) 5023 return; 5024 5025 packets = 0; 5026 bytes = 0; 5027 5028 rcu_read_lock(); 5029 for (i = 0; i < adapter->num_rx_queues; i++) { 5030 struct igc_ring *ring = adapter->rx_ring[i]; 5031 u32 rqdpc = rd32(IGC_RQDPC(i)); 5032 5033 if (hw->mac.type >= igc_i225) 5034 wr32(IGC_RQDPC(i), 0); 5035 5036 if (rqdpc) { 5037 ring->rx_stats.drops += rqdpc; 5038 net_stats->rx_fifo_errors += rqdpc; 5039 } 5040 5041 do { 5042 start = u64_stats_fetch_begin(&ring->rx_syncp); 5043 _bytes = ring->rx_stats.bytes; 5044 _packets = ring->rx_stats.packets; 5045 } while (u64_stats_fetch_retry(&ring->rx_syncp, start)); 5046 bytes += _bytes; 5047 packets += _packets; 5048 } 5049 5050 net_stats->rx_bytes = bytes; 5051 net_stats->rx_packets = packets; 5052 5053 packets = 0; 5054 bytes = 0; 5055 for (i = 0; i < adapter->num_tx_queues; i++) { 5056 struct igc_ring *ring = adapter->tx_ring[i]; 5057 5058 do { 5059 start = u64_stats_fetch_begin(&ring->tx_syncp); 5060 _bytes = ring->tx_stats.bytes; 5061 _packets = ring->tx_stats.packets; 5062 } while (u64_stats_fetch_retry(&ring->tx_syncp, start)); 5063 bytes += _bytes; 5064 packets += _packets; 5065 } 5066 net_stats->tx_bytes = bytes; 5067 net_stats->tx_packets = packets; 5068 rcu_read_unlock(); 5069 5070 /* read stats registers */ 5071 adapter->stats.crcerrs += rd32(IGC_CRCERRS); 5072 adapter->stats.gprc += rd32(IGC_GPRC); 5073 adapter->stats.gorc += rd32(IGC_GORCL); 5074 rd32(IGC_GORCH); /* clear GORCL */ 5075 adapter->stats.bprc += rd32(IGC_BPRC); 5076 adapter->stats.mprc += rd32(IGC_MPRC); 5077 adapter->stats.roc += rd32(IGC_ROC); 5078 5079 adapter->stats.prc64 += rd32(IGC_PRC64); 5080 adapter->stats.prc127 += rd32(IGC_PRC127); 5081 adapter->stats.prc255 += rd32(IGC_PRC255); 5082 adapter->stats.prc511 += rd32(IGC_PRC511); 5083 adapter->stats.prc1023 += rd32(IGC_PRC1023); 5084 adapter->stats.prc1522 += rd32(IGC_PRC1522); 5085 adapter->stats.tlpic += rd32(IGC_TLPIC); 5086 adapter->stats.rlpic += rd32(IGC_RLPIC); 5087 adapter->stats.hgptc += rd32(IGC_HGPTC); 5088 5089 mpc = rd32(IGC_MPC); 5090 adapter->stats.mpc += mpc; 5091 net_stats->rx_fifo_errors += mpc; 5092 adapter->stats.scc += rd32(IGC_SCC); 5093 adapter->stats.ecol += rd32(IGC_ECOL); 5094 adapter->stats.mcc += rd32(IGC_MCC); 5095 adapter->stats.latecol += rd32(IGC_LATECOL); 5096 adapter->stats.dc += rd32(IGC_DC); 5097 adapter->stats.rlec += rd32(IGC_RLEC); 5098 adapter->stats.xonrxc += rd32(IGC_XONRXC); 5099 adapter->stats.xontxc += rd32(IGC_XONTXC); 5100 adapter->stats.xoffrxc += rd32(IGC_XOFFRXC); 5101 adapter->stats.xofftxc += rd32(IGC_XOFFTXC); 5102 adapter->stats.fcruc += rd32(IGC_FCRUC); 5103 adapter->stats.gptc += rd32(IGC_GPTC); 5104 adapter->stats.gotc += rd32(IGC_GOTCL); 5105 rd32(IGC_GOTCH); /* clear GOTCL */ 5106 adapter->stats.rnbc += rd32(IGC_RNBC); 5107 adapter->stats.ruc += rd32(IGC_RUC); 5108 adapter->stats.rfc += rd32(IGC_RFC); 5109 adapter->stats.rjc += rd32(IGC_RJC); 5110 adapter->stats.tor += rd32(IGC_TORH); 5111 adapter->stats.tot += rd32(IGC_TOTH); 5112 adapter->stats.tpr += rd32(IGC_TPR); 5113 5114 adapter->stats.ptc64 += rd32(IGC_PTC64); 5115 adapter->stats.ptc127 += rd32(IGC_PTC127); 5116 adapter->stats.ptc255 += rd32(IGC_PTC255); 5117 adapter->stats.ptc511 += rd32(IGC_PTC511); 5118 adapter->stats.ptc1023 += rd32(IGC_PTC1023); 5119 adapter->stats.ptc1522 += rd32(IGC_PTC1522); 5120 5121 adapter->stats.mptc += rd32(IGC_MPTC); 5122 adapter->stats.bptc += rd32(IGC_BPTC); 5123 5124 adapter->stats.tpt += rd32(IGC_TPT); 5125 adapter->stats.colc += rd32(IGC_COLC); 5126 adapter->stats.colc += rd32(IGC_RERC); 5127 5128 adapter->stats.algnerrc += rd32(IGC_ALGNERRC); 5129 5130 adapter->stats.tsctc += rd32(IGC_TSCTC); 5131 5132 adapter->stats.iac += rd32(IGC_IAC); 5133 5134 /* Fill out the OS statistics structure */ 5135 net_stats->multicast = adapter->stats.mprc; 5136 net_stats->collisions = adapter->stats.colc; 5137 5138 /* Rx Errors */ 5139 5140 /* RLEC on some newer hardware can be incorrect so build 5141 * our own version based on RUC and ROC 5142 */ 5143 net_stats->rx_errors = adapter->stats.rxerrc + 5144 adapter->stats.crcerrs + adapter->stats.algnerrc + 5145 adapter->stats.ruc + adapter->stats.roc + 5146 adapter->stats.cexterr; 5147 net_stats->rx_length_errors = adapter->stats.ruc + 5148 adapter->stats.roc; 5149 net_stats->rx_crc_errors = adapter->stats.crcerrs; 5150 net_stats->rx_frame_errors = adapter->stats.algnerrc; 5151 net_stats->rx_missed_errors = adapter->stats.mpc; 5152 5153 /* Tx Errors */ 5154 net_stats->tx_errors = adapter->stats.ecol + 5155 adapter->stats.latecol; 5156 net_stats->tx_aborted_errors = adapter->stats.ecol; 5157 net_stats->tx_window_errors = adapter->stats.latecol; 5158 net_stats->tx_carrier_errors = adapter->stats.tncrs; 5159 5160 /* Tx Dropped */ 5161 net_stats->tx_dropped = adapter->stats.txdrop; 5162 5163 /* Management Stats */ 5164 adapter->stats.mgptc += rd32(IGC_MGTPTC); 5165 adapter->stats.mgprc += rd32(IGC_MGTPRC); 5166 adapter->stats.mgpdc += rd32(IGC_MGTPDC); 5167 } 5168 5169 /** 5170 * igc_down - Close the interface 5171 * @adapter: board private structure 5172 */ 5173 void igc_down(struct igc_adapter *adapter) 5174 { 5175 struct net_device *netdev = adapter->netdev; 5176 struct igc_hw *hw = &adapter->hw; 5177 u32 tctl, rctl; 5178 int i = 0; 5179 5180 set_bit(__IGC_DOWN, &adapter->state); 5181 5182 igc_ptp_suspend(adapter); 5183 5184 if (pci_device_is_present(adapter->pdev)) { 5185 /* disable receives in the hardware */ 5186 rctl = rd32(IGC_RCTL); 5187 wr32(IGC_RCTL, rctl & ~IGC_RCTL_EN); 5188 /* flush and sleep below */ 5189 } 5190 /* set trans_start so we don't get spurious watchdogs during reset */ 5191 netif_trans_update(netdev); 5192 5193 netif_carrier_off(netdev); 5194 netif_tx_stop_all_queues(netdev); 5195 5196 if (pci_device_is_present(adapter->pdev)) { 5197 /* disable transmits in the hardware */ 5198 tctl = rd32(IGC_TCTL); 5199 tctl &= ~IGC_TCTL_EN; 5200 wr32(IGC_TCTL, tctl); 5201 /* flush both disables and wait for them to finish */ 5202 wrfl(); 5203 usleep_range(10000, 20000); 5204 5205 igc_irq_disable(adapter); 5206 } 5207 5208 adapter->flags &= ~IGC_FLAG_NEED_LINK_UPDATE; 5209 5210 for (i = 0; i < adapter->num_q_vectors; i++) { 5211 if (adapter->q_vector[i]) { 5212 napi_synchronize(&adapter->q_vector[i]->napi); 5213 igc_set_queue_napi(adapter, i, NULL); 5214 napi_disable(&adapter->q_vector[i]->napi); 5215 } 5216 } 5217 5218 del_timer_sync(&adapter->watchdog_timer); 5219 del_timer_sync(&adapter->phy_info_timer); 5220 5221 /* record the stats before reset*/ 5222 spin_lock(&adapter->stats64_lock); 5223 igc_update_stats(adapter); 5224 spin_unlock(&adapter->stats64_lock); 5225 5226 adapter->link_speed = 0; 5227 adapter->link_duplex = 0; 5228 5229 if (!pci_channel_offline(adapter->pdev)) 5230 igc_reset(adapter); 5231 5232 /* clear VLAN promisc flag so VFTA will be updated if necessary */ 5233 adapter->flags &= ~IGC_FLAG_VLAN_PROMISC; 5234 5235 igc_disable_all_tx_rings_hw(adapter); 5236 igc_clean_all_tx_rings(adapter); 5237 igc_clean_all_rx_rings(adapter); 5238 } 5239 5240 void igc_reinit_locked(struct igc_adapter *adapter) 5241 { 5242 while (test_and_set_bit(__IGC_RESETTING, &adapter->state)) 5243 usleep_range(1000, 2000); 5244 igc_down(adapter); 5245 igc_up(adapter); 5246 clear_bit(__IGC_RESETTING, &adapter->state); 5247 } 5248 5249 static void igc_reset_task(struct work_struct *work) 5250 { 5251 struct igc_adapter *adapter; 5252 5253 adapter = container_of(work, struct igc_adapter, reset_task); 5254 5255 rtnl_lock(); 5256 /* If we're already down or resetting, just bail */ 5257 if (test_bit(__IGC_DOWN, &adapter->state) || 5258 test_bit(__IGC_RESETTING, &adapter->state)) { 5259 rtnl_unlock(); 5260 return; 5261 } 5262 5263 igc_rings_dump(adapter); 5264 igc_regs_dump(adapter); 5265 netdev_err(adapter->netdev, "Reset adapter\n"); 5266 igc_reinit_locked(adapter); 5267 rtnl_unlock(); 5268 } 5269 5270 /** 5271 * igc_change_mtu - Change the Maximum Transfer Unit 5272 * @netdev: network interface device structure 5273 * @new_mtu: new value for maximum frame size 5274 * 5275 * Returns 0 on success, negative on failure 5276 */ 5277 static int igc_change_mtu(struct net_device *netdev, int new_mtu) 5278 { 5279 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN; 5280 struct igc_adapter *adapter = netdev_priv(netdev); 5281 5282 if (igc_xdp_is_enabled(adapter) && new_mtu > ETH_DATA_LEN) { 5283 netdev_dbg(netdev, "Jumbo frames not supported with XDP"); 5284 return -EINVAL; 5285 } 5286 5287 /* adjust max frame to be at least the size of a standard frame */ 5288 if (max_frame < (ETH_FRAME_LEN + ETH_FCS_LEN)) 5289 max_frame = ETH_FRAME_LEN + ETH_FCS_LEN; 5290 5291 while (test_and_set_bit(__IGC_RESETTING, &adapter->state)) 5292 usleep_range(1000, 2000); 5293 5294 /* igc_down has a dependency on max_frame_size */ 5295 adapter->max_frame_size = max_frame; 5296 5297 if (netif_running(netdev)) 5298 igc_down(adapter); 5299 5300 netdev_dbg(netdev, "changing MTU from %d to %d\n", netdev->mtu, new_mtu); 5301 WRITE_ONCE(netdev->mtu, new_mtu); 5302 5303 if (netif_running(netdev)) 5304 igc_up(adapter); 5305 else 5306 igc_reset(adapter); 5307 5308 clear_bit(__IGC_RESETTING, &adapter->state); 5309 5310 return 0; 5311 } 5312 5313 /** 5314 * igc_tx_timeout - Respond to a Tx Hang 5315 * @netdev: network interface device structure 5316 * @txqueue: queue number that timed out 5317 **/ 5318 static void igc_tx_timeout(struct net_device *netdev, 5319 unsigned int __always_unused txqueue) 5320 { 5321 struct igc_adapter *adapter = netdev_priv(netdev); 5322 struct igc_hw *hw = &adapter->hw; 5323 5324 /* Do the reset outside of interrupt context */ 5325 adapter->tx_timeout_count++; 5326 schedule_work(&adapter->reset_task); 5327 wr32(IGC_EICS, 5328 (adapter->eims_enable_mask & ~adapter->eims_other)); 5329 } 5330 5331 /** 5332 * igc_get_stats64 - Get System Network Statistics 5333 * @netdev: network interface device structure 5334 * @stats: rtnl_link_stats64 pointer 5335 * 5336 * Returns the address of the device statistics structure. 5337 * The statistics are updated here and also from the timer callback. 5338 */ 5339 static void igc_get_stats64(struct net_device *netdev, 5340 struct rtnl_link_stats64 *stats) 5341 { 5342 struct igc_adapter *adapter = netdev_priv(netdev); 5343 5344 spin_lock(&adapter->stats64_lock); 5345 if (!test_bit(__IGC_RESETTING, &adapter->state)) 5346 igc_update_stats(adapter); 5347 memcpy(stats, &adapter->stats64, sizeof(*stats)); 5348 spin_unlock(&adapter->stats64_lock); 5349 } 5350 5351 static netdev_features_t igc_fix_features(struct net_device *netdev, 5352 netdev_features_t features) 5353 { 5354 /* Since there is no support for separate Rx/Tx vlan accel 5355 * enable/disable make sure Tx flag is always in same state as Rx. 5356 */ 5357 if (features & NETIF_F_HW_VLAN_CTAG_RX) 5358 features |= NETIF_F_HW_VLAN_CTAG_TX; 5359 else 5360 features &= ~NETIF_F_HW_VLAN_CTAG_TX; 5361 5362 return features; 5363 } 5364 5365 static int igc_set_features(struct net_device *netdev, 5366 netdev_features_t features) 5367 { 5368 netdev_features_t changed = netdev->features ^ features; 5369 struct igc_adapter *adapter = netdev_priv(netdev); 5370 5371 if (changed & NETIF_F_HW_VLAN_CTAG_RX) 5372 igc_vlan_mode(netdev, features); 5373 5374 /* Add VLAN support */ 5375 if (!(changed & (NETIF_F_RXALL | NETIF_F_NTUPLE))) 5376 return 0; 5377 5378 if (!(features & NETIF_F_NTUPLE)) 5379 igc_flush_nfc_rules(adapter); 5380 5381 netdev->features = features; 5382 5383 if (netif_running(netdev)) 5384 igc_reinit_locked(adapter); 5385 else 5386 igc_reset(adapter); 5387 5388 return 1; 5389 } 5390 5391 static netdev_features_t 5392 igc_features_check(struct sk_buff *skb, struct net_device *dev, 5393 netdev_features_t features) 5394 { 5395 unsigned int network_hdr_len, mac_hdr_len; 5396 5397 /* Make certain the headers can be described by a context descriptor */ 5398 mac_hdr_len = skb_network_offset(skb); 5399 if (unlikely(mac_hdr_len > IGC_MAX_MAC_HDR_LEN)) 5400 return features & ~(NETIF_F_HW_CSUM | 5401 NETIF_F_SCTP_CRC | 5402 NETIF_F_HW_VLAN_CTAG_TX | 5403 NETIF_F_TSO | 5404 NETIF_F_TSO6); 5405 5406 network_hdr_len = skb_checksum_start(skb) - skb_network_header(skb); 5407 if (unlikely(network_hdr_len > IGC_MAX_NETWORK_HDR_LEN)) 5408 return features & ~(NETIF_F_HW_CSUM | 5409 NETIF_F_SCTP_CRC | 5410 NETIF_F_TSO | 5411 NETIF_F_TSO6); 5412 5413 /* We can only support IPv4 TSO in tunnels if we can mangle the 5414 * inner IP ID field, so strip TSO if MANGLEID is not supported. 5415 */ 5416 if (skb->encapsulation && !(features & NETIF_F_TSO_MANGLEID)) 5417 features &= ~NETIF_F_TSO; 5418 5419 return features; 5420 } 5421 5422 static void igc_tsync_interrupt(struct igc_adapter *adapter) 5423 { 5424 struct igc_hw *hw = &adapter->hw; 5425 u32 tsauxc, sec, nsec, tsicr; 5426 struct ptp_clock_event event; 5427 struct timespec64 ts; 5428 5429 tsicr = rd32(IGC_TSICR); 5430 5431 if (tsicr & IGC_TSICR_SYS_WRAP) { 5432 event.type = PTP_CLOCK_PPS; 5433 if (adapter->ptp_caps.pps) 5434 ptp_clock_event(adapter->ptp_clock, &event); 5435 } 5436 5437 if (tsicr & IGC_TSICR_TXTS) { 5438 /* retrieve hardware timestamp */ 5439 igc_ptp_tx_tstamp_event(adapter); 5440 } 5441 5442 if (tsicr & IGC_TSICR_TT0) { 5443 spin_lock(&adapter->tmreg_lock); 5444 ts = timespec64_add(adapter->perout[0].start, 5445 adapter->perout[0].period); 5446 wr32(IGC_TRGTTIML0, ts.tv_nsec | IGC_TT_IO_TIMER_SEL_SYSTIM0); 5447 wr32(IGC_TRGTTIMH0, (u32)ts.tv_sec); 5448 tsauxc = rd32(IGC_TSAUXC); 5449 tsauxc |= IGC_TSAUXC_EN_TT0; 5450 wr32(IGC_TSAUXC, tsauxc); 5451 adapter->perout[0].start = ts; 5452 spin_unlock(&adapter->tmreg_lock); 5453 } 5454 5455 if (tsicr & IGC_TSICR_TT1) { 5456 spin_lock(&adapter->tmreg_lock); 5457 ts = timespec64_add(adapter->perout[1].start, 5458 adapter->perout[1].period); 5459 wr32(IGC_TRGTTIML1, ts.tv_nsec | IGC_TT_IO_TIMER_SEL_SYSTIM0); 5460 wr32(IGC_TRGTTIMH1, (u32)ts.tv_sec); 5461 tsauxc = rd32(IGC_TSAUXC); 5462 tsauxc |= IGC_TSAUXC_EN_TT1; 5463 wr32(IGC_TSAUXC, tsauxc); 5464 adapter->perout[1].start = ts; 5465 spin_unlock(&adapter->tmreg_lock); 5466 } 5467 5468 if (tsicr & IGC_TSICR_AUTT0) { 5469 nsec = rd32(IGC_AUXSTMPL0); 5470 sec = rd32(IGC_AUXSTMPH0); 5471 event.type = PTP_CLOCK_EXTTS; 5472 event.index = 0; 5473 event.timestamp = sec * NSEC_PER_SEC + nsec; 5474 ptp_clock_event(adapter->ptp_clock, &event); 5475 } 5476 5477 if (tsicr & IGC_TSICR_AUTT1) { 5478 nsec = rd32(IGC_AUXSTMPL1); 5479 sec = rd32(IGC_AUXSTMPH1); 5480 event.type = PTP_CLOCK_EXTTS; 5481 event.index = 1; 5482 event.timestamp = sec * NSEC_PER_SEC + nsec; 5483 ptp_clock_event(adapter->ptp_clock, &event); 5484 } 5485 } 5486 5487 /** 5488 * igc_msix_other - msix other interrupt handler 5489 * @irq: interrupt number 5490 * @data: pointer to a q_vector 5491 */ 5492 static irqreturn_t igc_msix_other(int irq, void *data) 5493 { 5494 struct igc_adapter *adapter = data; 5495 struct igc_hw *hw = &adapter->hw; 5496 u32 icr = rd32(IGC_ICR); 5497 5498 /* reading ICR causes bit 31 of EICR to be cleared */ 5499 if (icr & IGC_ICR_DRSTA) 5500 schedule_work(&adapter->reset_task); 5501 5502 if (icr & IGC_ICR_DOUTSYNC) { 5503 /* HW is reporting DMA is out of sync */ 5504 adapter->stats.doosync++; 5505 } 5506 5507 if (icr & IGC_ICR_LSC) { 5508 hw->mac.get_link_status = true; 5509 /* guard against interrupt when we're going down */ 5510 if (!test_bit(__IGC_DOWN, &adapter->state)) 5511 mod_timer(&adapter->watchdog_timer, jiffies + 1); 5512 } 5513 5514 if (icr & IGC_ICR_TS) 5515 igc_tsync_interrupt(adapter); 5516 5517 wr32(IGC_EIMS, adapter->eims_other); 5518 5519 return IRQ_HANDLED; 5520 } 5521 5522 static void igc_write_itr(struct igc_q_vector *q_vector) 5523 { 5524 u32 itr_val = q_vector->itr_val & IGC_QVECTOR_MASK; 5525 5526 if (!q_vector->set_itr) 5527 return; 5528 5529 if (!itr_val) 5530 itr_val = IGC_ITR_VAL_MASK; 5531 5532 itr_val |= IGC_EITR_CNT_IGNR; 5533 5534 writel(itr_val, q_vector->itr_register); 5535 q_vector->set_itr = 0; 5536 } 5537 5538 static irqreturn_t igc_msix_ring(int irq, void *data) 5539 { 5540 struct igc_q_vector *q_vector = data; 5541 5542 /* Write the ITR value calculated from the previous interrupt. */ 5543 igc_write_itr(q_vector); 5544 5545 napi_schedule(&q_vector->napi); 5546 5547 return IRQ_HANDLED; 5548 } 5549 5550 /** 5551 * igc_request_msix - Initialize MSI-X interrupts 5552 * @adapter: Pointer to adapter structure 5553 * 5554 * igc_request_msix allocates MSI-X vectors and requests interrupts from the 5555 * kernel. 5556 */ 5557 static int igc_request_msix(struct igc_adapter *adapter) 5558 { 5559 unsigned int num_q_vectors = adapter->num_q_vectors; 5560 int i = 0, err = 0, vector = 0, free_vector = 0; 5561 struct net_device *netdev = adapter->netdev; 5562 5563 err = request_irq(adapter->msix_entries[vector].vector, 5564 &igc_msix_other, 0, netdev->name, adapter); 5565 if (err) 5566 goto err_out; 5567 5568 if (num_q_vectors > MAX_Q_VECTORS) { 5569 num_q_vectors = MAX_Q_VECTORS; 5570 dev_warn(&adapter->pdev->dev, 5571 "The number of queue vectors (%d) is higher than max allowed (%d)\n", 5572 adapter->num_q_vectors, MAX_Q_VECTORS); 5573 } 5574 for (i = 0; i < num_q_vectors; i++) { 5575 struct igc_q_vector *q_vector = adapter->q_vector[i]; 5576 5577 vector++; 5578 5579 q_vector->itr_register = adapter->io_addr + IGC_EITR(vector); 5580 5581 if (q_vector->rx.ring && q_vector->tx.ring) 5582 sprintf(q_vector->name, "%s-TxRx-%u", netdev->name, 5583 q_vector->rx.ring->queue_index); 5584 else if (q_vector->tx.ring) 5585 sprintf(q_vector->name, "%s-tx-%u", netdev->name, 5586 q_vector->tx.ring->queue_index); 5587 else if (q_vector->rx.ring) 5588 sprintf(q_vector->name, "%s-rx-%u", netdev->name, 5589 q_vector->rx.ring->queue_index); 5590 else 5591 sprintf(q_vector->name, "%s-unused", netdev->name); 5592 5593 err = request_irq(adapter->msix_entries[vector].vector, 5594 igc_msix_ring, 0, q_vector->name, 5595 q_vector); 5596 if (err) 5597 goto err_free; 5598 5599 netif_napi_set_irq(&q_vector->napi, 5600 adapter->msix_entries[vector].vector); 5601 } 5602 5603 igc_configure_msix(adapter); 5604 return 0; 5605 5606 err_free: 5607 /* free already assigned IRQs */ 5608 free_irq(adapter->msix_entries[free_vector++].vector, adapter); 5609 5610 vector--; 5611 for (i = 0; i < vector; i++) { 5612 free_irq(adapter->msix_entries[free_vector++].vector, 5613 adapter->q_vector[i]); 5614 } 5615 err_out: 5616 return err; 5617 } 5618 5619 /** 5620 * igc_clear_interrupt_scheme - reset the device to a state of no interrupts 5621 * @adapter: Pointer to adapter structure 5622 * 5623 * This function resets the device so that it has 0 rx queues, tx queues, and 5624 * MSI-X interrupts allocated. 5625 */ 5626 static void igc_clear_interrupt_scheme(struct igc_adapter *adapter) 5627 { 5628 igc_free_q_vectors(adapter); 5629 igc_reset_interrupt_capability(adapter); 5630 } 5631 5632 /* Need to wait a few seconds after link up to get diagnostic information from 5633 * the phy 5634 */ 5635 static void igc_update_phy_info(struct timer_list *t) 5636 { 5637 struct igc_adapter *adapter = from_timer(adapter, t, phy_info_timer); 5638 5639 igc_get_phy_info(&adapter->hw); 5640 } 5641 5642 /** 5643 * igc_has_link - check shared code for link and determine up/down 5644 * @adapter: pointer to driver private info 5645 */ 5646 bool igc_has_link(struct igc_adapter *adapter) 5647 { 5648 struct igc_hw *hw = &adapter->hw; 5649 bool link_active = false; 5650 5651 /* get_link_status is set on LSC (link status) interrupt or 5652 * rx sequence error interrupt. get_link_status will stay 5653 * false until the igc_check_for_link establishes link 5654 * for copper adapters ONLY 5655 */ 5656 if (!hw->mac.get_link_status) 5657 return true; 5658 hw->mac.ops.check_for_link(hw); 5659 link_active = !hw->mac.get_link_status; 5660 5661 if (hw->mac.type == igc_i225) { 5662 if (!netif_carrier_ok(adapter->netdev)) { 5663 adapter->flags &= ~IGC_FLAG_NEED_LINK_UPDATE; 5664 } else if (!(adapter->flags & IGC_FLAG_NEED_LINK_UPDATE)) { 5665 adapter->flags |= IGC_FLAG_NEED_LINK_UPDATE; 5666 adapter->link_check_timeout = jiffies; 5667 } 5668 } 5669 5670 return link_active; 5671 } 5672 5673 /** 5674 * igc_watchdog - Timer Call-back 5675 * @t: timer for the watchdog 5676 */ 5677 static void igc_watchdog(struct timer_list *t) 5678 { 5679 struct igc_adapter *adapter = from_timer(adapter, t, watchdog_timer); 5680 /* Do the rest outside of interrupt context */ 5681 schedule_work(&adapter->watchdog_task); 5682 } 5683 5684 static void igc_watchdog_task(struct work_struct *work) 5685 { 5686 struct igc_adapter *adapter = container_of(work, 5687 struct igc_adapter, 5688 watchdog_task); 5689 struct net_device *netdev = adapter->netdev; 5690 struct igc_hw *hw = &adapter->hw; 5691 struct igc_phy_info *phy = &hw->phy; 5692 u16 phy_data, retry_count = 20; 5693 u32 link; 5694 int i; 5695 5696 link = igc_has_link(adapter); 5697 5698 if (adapter->flags & IGC_FLAG_NEED_LINK_UPDATE) { 5699 if (time_after(jiffies, (adapter->link_check_timeout + HZ))) 5700 adapter->flags &= ~IGC_FLAG_NEED_LINK_UPDATE; 5701 else 5702 link = false; 5703 } 5704 5705 if (link) { 5706 /* Cancel scheduled suspend requests. */ 5707 pm_runtime_resume(netdev->dev.parent); 5708 5709 if (!netif_carrier_ok(netdev)) { 5710 u32 ctrl; 5711 5712 hw->mac.ops.get_speed_and_duplex(hw, 5713 &adapter->link_speed, 5714 &adapter->link_duplex); 5715 5716 ctrl = rd32(IGC_CTRL); 5717 /* Link status message must follow this format */ 5718 netdev_info(netdev, 5719 "NIC Link is Up %d Mbps %s Duplex, Flow Control: %s\n", 5720 adapter->link_speed, 5721 adapter->link_duplex == FULL_DUPLEX ? 5722 "Full" : "Half", 5723 (ctrl & IGC_CTRL_TFCE) && 5724 (ctrl & IGC_CTRL_RFCE) ? "RX/TX" : 5725 (ctrl & IGC_CTRL_RFCE) ? "RX" : 5726 (ctrl & IGC_CTRL_TFCE) ? "TX" : "None"); 5727 5728 /* disable EEE if enabled */ 5729 if ((adapter->flags & IGC_FLAG_EEE) && 5730 adapter->link_duplex == HALF_DUPLEX) { 5731 netdev_info(netdev, 5732 "EEE Disabled: unsupported at half duplex. Re-enable using ethtool when at full duplex\n"); 5733 adapter->hw.dev_spec._base.eee_enable = false; 5734 adapter->flags &= ~IGC_FLAG_EEE; 5735 } 5736 5737 /* check if SmartSpeed worked */ 5738 igc_check_downshift(hw); 5739 if (phy->speed_downgraded) 5740 netdev_warn(netdev, "Link Speed was downgraded by SmartSpeed\n"); 5741 5742 /* adjust timeout factor according to speed/duplex */ 5743 adapter->tx_timeout_factor = 1; 5744 switch (adapter->link_speed) { 5745 case SPEED_10: 5746 adapter->tx_timeout_factor = 14; 5747 break; 5748 case SPEED_100: 5749 case SPEED_1000: 5750 case SPEED_2500: 5751 adapter->tx_timeout_factor = 1; 5752 break; 5753 } 5754 5755 /* Once the launch time has been set on the wire, there 5756 * is a delay before the link speed can be determined 5757 * based on link-up activity. Write into the register 5758 * as soon as we know the correct link speed. 5759 */ 5760 igc_tsn_adjust_txtime_offset(adapter); 5761 5762 if (adapter->link_speed != SPEED_1000) 5763 goto no_wait; 5764 5765 /* wait for Remote receiver status OK */ 5766 retry_read_status: 5767 if (!igc_read_phy_reg(hw, PHY_1000T_STATUS, 5768 &phy_data)) { 5769 if (!(phy_data & SR_1000T_REMOTE_RX_STATUS) && 5770 retry_count) { 5771 msleep(100); 5772 retry_count--; 5773 goto retry_read_status; 5774 } else if (!retry_count) { 5775 netdev_err(netdev, "exceed max 2 second\n"); 5776 } 5777 } else { 5778 netdev_err(netdev, "read 1000Base-T Status Reg\n"); 5779 } 5780 no_wait: 5781 netif_carrier_on(netdev); 5782 5783 /* link state has changed, schedule phy info update */ 5784 if (!test_bit(__IGC_DOWN, &adapter->state)) 5785 mod_timer(&adapter->phy_info_timer, 5786 round_jiffies(jiffies + 2 * HZ)); 5787 } 5788 } else { 5789 if (netif_carrier_ok(netdev)) { 5790 adapter->link_speed = 0; 5791 adapter->link_duplex = 0; 5792 5793 /* Links status message must follow this format */ 5794 netdev_info(netdev, "NIC Link is Down\n"); 5795 netif_carrier_off(netdev); 5796 5797 /* link state has changed, schedule phy info update */ 5798 if (!test_bit(__IGC_DOWN, &adapter->state)) 5799 mod_timer(&adapter->phy_info_timer, 5800 round_jiffies(jiffies + 2 * HZ)); 5801 5802 pm_schedule_suspend(netdev->dev.parent, 5803 MSEC_PER_SEC * 5); 5804 } 5805 } 5806 5807 spin_lock(&adapter->stats64_lock); 5808 igc_update_stats(adapter); 5809 spin_unlock(&adapter->stats64_lock); 5810 5811 for (i = 0; i < adapter->num_tx_queues; i++) { 5812 struct igc_ring *tx_ring = adapter->tx_ring[i]; 5813 5814 if (!netif_carrier_ok(netdev)) { 5815 /* We've lost link, so the controller stops DMA, 5816 * but we've got queued Tx work that's never going 5817 * to get done, so reset controller to flush Tx. 5818 * (Do the reset outside of interrupt context). 5819 */ 5820 if (igc_desc_unused(tx_ring) + 1 < tx_ring->count) { 5821 adapter->tx_timeout_count++; 5822 schedule_work(&adapter->reset_task); 5823 /* return immediately since reset is imminent */ 5824 return; 5825 } 5826 } 5827 5828 /* Force detection of hung controller every watchdog period */ 5829 set_bit(IGC_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags); 5830 } 5831 5832 /* Cause software interrupt to ensure Rx ring is cleaned */ 5833 if (adapter->flags & IGC_FLAG_HAS_MSIX) { 5834 u32 eics = 0; 5835 5836 for (i = 0; i < adapter->num_q_vectors; i++) { 5837 struct igc_q_vector *q_vector = adapter->q_vector[i]; 5838 struct igc_ring *rx_ring; 5839 5840 if (!q_vector->rx.ring) 5841 continue; 5842 5843 rx_ring = adapter->rx_ring[q_vector->rx.ring->queue_index]; 5844 5845 if (test_bit(IGC_RING_FLAG_RX_ALLOC_FAILED, &rx_ring->flags)) { 5846 eics |= q_vector->eims_value; 5847 clear_bit(IGC_RING_FLAG_RX_ALLOC_FAILED, &rx_ring->flags); 5848 } 5849 } 5850 if (eics) 5851 wr32(IGC_EICS, eics); 5852 } else { 5853 struct igc_ring *rx_ring = adapter->rx_ring[0]; 5854 5855 if (test_bit(IGC_RING_FLAG_RX_ALLOC_FAILED, &rx_ring->flags)) { 5856 clear_bit(IGC_RING_FLAG_RX_ALLOC_FAILED, &rx_ring->flags); 5857 wr32(IGC_ICS, IGC_ICS_RXDMT0); 5858 } 5859 } 5860 5861 igc_ptp_tx_hang(adapter); 5862 5863 /* Reset the timer */ 5864 if (!test_bit(__IGC_DOWN, &adapter->state)) { 5865 if (adapter->flags & IGC_FLAG_NEED_LINK_UPDATE) 5866 mod_timer(&adapter->watchdog_timer, 5867 round_jiffies(jiffies + HZ)); 5868 else 5869 mod_timer(&adapter->watchdog_timer, 5870 round_jiffies(jiffies + 2 * HZ)); 5871 } 5872 } 5873 5874 /** 5875 * igc_intr_msi - Interrupt Handler 5876 * @irq: interrupt number 5877 * @data: pointer to a network interface device structure 5878 */ 5879 static irqreturn_t igc_intr_msi(int irq, void *data) 5880 { 5881 struct igc_adapter *adapter = data; 5882 struct igc_q_vector *q_vector = adapter->q_vector[0]; 5883 struct igc_hw *hw = &adapter->hw; 5884 /* read ICR disables interrupts using IAM */ 5885 u32 icr = rd32(IGC_ICR); 5886 5887 igc_write_itr(q_vector); 5888 5889 if (icr & IGC_ICR_DRSTA) 5890 schedule_work(&adapter->reset_task); 5891 5892 if (icr & IGC_ICR_DOUTSYNC) { 5893 /* HW is reporting DMA is out of sync */ 5894 adapter->stats.doosync++; 5895 } 5896 5897 if (icr & (IGC_ICR_RXSEQ | IGC_ICR_LSC)) { 5898 hw->mac.get_link_status = true; 5899 if (!test_bit(__IGC_DOWN, &adapter->state)) 5900 mod_timer(&adapter->watchdog_timer, jiffies + 1); 5901 } 5902 5903 if (icr & IGC_ICR_TS) 5904 igc_tsync_interrupt(adapter); 5905 5906 napi_schedule(&q_vector->napi); 5907 5908 return IRQ_HANDLED; 5909 } 5910 5911 /** 5912 * igc_intr - Legacy Interrupt Handler 5913 * @irq: interrupt number 5914 * @data: pointer to a network interface device structure 5915 */ 5916 static irqreturn_t igc_intr(int irq, void *data) 5917 { 5918 struct igc_adapter *adapter = data; 5919 struct igc_q_vector *q_vector = adapter->q_vector[0]; 5920 struct igc_hw *hw = &adapter->hw; 5921 /* Interrupt Auto-Mask...upon reading ICR, interrupts are masked. No 5922 * need for the IMC write 5923 */ 5924 u32 icr = rd32(IGC_ICR); 5925 5926 /* IMS will not auto-mask if INT_ASSERTED is not set, and if it is 5927 * not set, then the adapter didn't send an interrupt 5928 */ 5929 if (!(icr & IGC_ICR_INT_ASSERTED)) 5930 return IRQ_NONE; 5931 5932 igc_write_itr(q_vector); 5933 5934 if (icr & IGC_ICR_DRSTA) 5935 schedule_work(&adapter->reset_task); 5936 5937 if (icr & IGC_ICR_DOUTSYNC) { 5938 /* HW is reporting DMA is out of sync */ 5939 adapter->stats.doosync++; 5940 } 5941 5942 if (icr & (IGC_ICR_RXSEQ | IGC_ICR_LSC)) { 5943 hw->mac.get_link_status = true; 5944 /* guard against interrupt when we're going down */ 5945 if (!test_bit(__IGC_DOWN, &adapter->state)) 5946 mod_timer(&adapter->watchdog_timer, jiffies + 1); 5947 } 5948 5949 if (icr & IGC_ICR_TS) 5950 igc_tsync_interrupt(adapter); 5951 5952 napi_schedule(&q_vector->napi); 5953 5954 return IRQ_HANDLED; 5955 } 5956 5957 static void igc_free_irq(struct igc_adapter *adapter) 5958 { 5959 if (adapter->msix_entries) { 5960 int vector = 0, i; 5961 5962 free_irq(adapter->msix_entries[vector++].vector, adapter); 5963 5964 for (i = 0; i < adapter->num_q_vectors; i++) 5965 free_irq(adapter->msix_entries[vector++].vector, 5966 adapter->q_vector[i]); 5967 } else { 5968 free_irq(adapter->pdev->irq, adapter); 5969 } 5970 } 5971 5972 /** 5973 * igc_request_irq - initialize interrupts 5974 * @adapter: Pointer to adapter structure 5975 * 5976 * Attempts to configure interrupts using the best available 5977 * capabilities of the hardware and kernel. 5978 */ 5979 static int igc_request_irq(struct igc_adapter *adapter) 5980 { 5981 struct net_device *netdev = adapter->netdev; 5982 struct pci_dev *pdev = adapter->pdev; 5983 int err = 0; 5984 5985 if (adapter->flags & IGC_FLAG_HAS_MSIX) { 5986 err = igc_request_msix(adapter); 5987 if (!err) 5988 goto request_done; 5989 /* fall back to MSI */ 5990 igc_free_all_tx_resources(adapter); 5991 igc_free_all_rx_resources(adapter); 5992 5993 igc_clear_interrupt_scheme(adapter); 5994 err = igc_init_interrupt_scheme(adapter, false); 5995 if (err) 5996 goto request_done; 5997 igc_setup_all_tx_resources(adapter); 5998 igc_setup_all_rx_resources(adapter); 5999 igc_configure(adapter); 6000 } 6001 6002 igc_assign_vector(adapter->q_vector[0], 0); 6003 6004 if (adapter->flags & IGC_FLAG_HAS_MSI) { 6005 err = request_irq(pdev->irq, &igc_intr_msi, 0, 6006 netdev->name, adapter); 6007 if (!err) 6008 goto request_done; 6009 6010 /* fall back to legacy interrupts */ 6011 igc_reset_interrupt_capability(adapter); 6012 adapter->flags &= ~IGC_FLAG_HAS_MSI; 6013 } 6014 6015 err = request_irq(pdev->irq, &igc_intr, IRQF_SHARED, 6016 netdev->name, adapter); 6017 6018 if (err) 6019 netdev_err(netdev, "Error %d getting interrupt\n", err); 6020 6021 request_done: 6022 return err; 6023 } 6024 6025 /** 6026 * __igc_open - Called when a network interface is made active 6027 * @netdev: network interface device structure 6028 * @resuming: boolean indicating if the device is resuming 6029 * 6030 * Returns 0 on success, negative value on failure 6031 * 6032 * The open entry point is called when a network interface is made 6033 * active by the system (IFF_UP). At this point all resources needed 6034 * for transmit and receive operations are allocated, the interrupt 6035 * handler is registered with the OS, the watchdog timer is started, 6036 * and the stack is notified that the interface is ready. 6037 */ 6038 static int __igc_open(struct net_device *netdev, bool resuming) 6039 { 6040 struct igc_adapter *adapter = netdev_priv(netdev); 6041 struct pci_dev *pdev = adapter->pdev; 6042 struct igc_hw *hw = &adapter->hw; 6043 struct napi_struct *napi; 6044 int err = 0; 6045 int i = 0; 6046 6047 /* disallow open during test */ 6048 6049 if (test_bit(__IGC_TESTING, &adapter->state)) { 6050 WARN_ON(resuming); 6051 return -EBUSY; 6052 } 6053 6054 if (!resuming) 6055 pm_runtime_get_sync(&pdev->dev); 6056 6057 netif_carrier_off(netdev); 6058 6059 /* allocate transmit descriptors */ 6060 err = igc_setup_all_tx_resources(adapter); 6061 if (err) 6062 goto err_setup_tx; 6063 6064 /* allocate receive descriptors */ 6065 err = igc_setup_all_rx_resources(adapter); 6066 if (err) 6067 goto err_setup_rx; 6068 6069 igc_power_up_link(adapter); 6070 6071 igc_configure(adapter); 6072 6073 err = igc_request_irq(adapter); 6074 if (err) 6075 goto err_req_irq; 6076 6077 clear_bit(__IGC_DOWN, &adapter->state); 6078 6079 for (i = 0; i < adapter->num_q_vectors; i++) { 6080 napi = &adapter->q_vector[i]->napi; 6081 napi_enable(napi); 6082 igc_set_queue_napi(adapter, i, napi); 6083 } 6084 6085 /* Clear any pending interrupts. */ 6086 rd32(IGC_ICR); 6087 igc_irq_enable(adapter); 6088 6089 if (!resuming) 6090 pm_runtime_put(&pdev->dev); 6091 6092 netif_tx_start_all_queues(netdev); 6093 6094 /* start the watchdog. */ 6095 hw->mac.get_link_status = true; 6096 schedule_work(&adapter->watchdog_task); 6097 6098 return IGC_SUCCESS; 6099 6100 err_req_irq: 6101 igc_release_hw_control(adapter); 6102 igc_power_down_phy_copper_base(&adapter->hw); 6103 igc_free_all_rx_resources(adapter); 6104 err_setup_rx: 6105 igc_free_all_tx_resources(adapter); 6106 err_setup_tx: 6107 igc_reset(adapter); 6108 if (!resuming) 6109 pm_runtime_put(&pdev->dev); 6110 6111 return err; 6112 } 6113 6114 int igc_open(struct net_device *netdev) 6115 { 6116 struct igc_adapter *adapter = netdev_priv(netdev); 6117 int err; 6118 6119 /* Notify the stack of the actual queue counts. */ 6120 err = netif_set_real_num_queues(netdev, adapter->num_tx_queues, 6121 adapter->num_rx_queues); 6122 if (err) { 6123 netdev_err(netdev, "error setting real queue count\n"); 6124 return err; 6125 } 6126 6127 return __igc_open(netdev, false); 6128 } 6129 6130 /** 6131 * __igc_close - Disables a network interface 6132 * @netdev: network interface device structure 6133 * @suspending: boolean indicating the device is suspending 6134 * 6135 * Returns 0, this is not allowed to fail 6136 * 6137 * The close entry point is called when an interface is de-activated 6138 * by the OS. The hardware is still under the driver's control, but 6139 * needs to be disabled. A global MAC reset is issued to stop the 6140 * hardware, and all transmit and receive resources are freed. 6141 */ 6142 static int __igc_close(struct net_device *netdev, bool suspending) 6143 { 6144 struct igc_adapter *adapter = netdev_priv(netdev); 6145 struct pci_dev *pdev = adapter->pdev; 6146 6147 WARN_ON(test_bit(__IGC_RESETTING, &adapter->state)); 6148 6149 if (!suspending) 6150 pm_runtime_get_sync(&pdev->dev); 6151 6152 igc_down(adapter); 6153 6154 igc_release_hw_control(adapter); 6155 6156 igc_free_irq(adapter); 6157 6158 igc_free_all_tx_resources(adapter); 6159 igc_free_all_rx_resources(adapter); 6160 6161 if (!suspending) 6162 pm_runtime_put_sync(&pdev->dev); 6163 6164 return 0; 6165 } 6166 6167 int igc_close(struct net_device *netdev) 6168 { 6169 if (netif_device_present(netdev) || netdev->dismantle) 6170 return __igc_close(netdev, false); 6171 return 0; 6172 } 6173 6174 /** 6175 * igc_ioctl - Access the hwtstamp interface 6176 * @netdev: network interface device structure 6177 * @ifr: interface request data 6178 * @cmd: ioctl command 6179 **/ 6180 static int igc_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd) 6181 { 6182 switch (cmd) { 6183 case SIOCGHWTSTAMP: 6184 return igc_ptp_get_ts_config(netdev, ifr); 6185 case SIOCSHWTSTAMP: 6186 return igc_ptp_set_ts_config(netdev, ifr); 6187 default: 6188 return -EOPNOTSUPP; 6189 } 6190 } 6191 6192 static int igc_save_launchtime_params(struct igc_adapter *adapter, int queue, 6193 bool enable) 6194 { 6195 struct igc_ring *ring; 6196 6197 if (queue < 0 || queue >= adapter->num_tx_queues) 6198 return -EINVAL; 6199 6200 ring = adapter->tx_ring[queue]; 6201 ring->launchtime_enable = enable; 6202 6203 return 0; 6204 } 6205 6206 static bool is_base_time_past(ktime_t base_time, const struct timespec64 *now) 6207 { 6208 struct timespec64 b; 6209 6210 b = ktime_to_timespec64(base_time); 6211 6212 return timespec64_compare(now, &b) > 0; 6213 } 6214 6215 static bool validate_schedule(struct igc_adapter *adapter, 6216 const struct tc_taprio_qopt_offload *qopt) 6217 { 6218 int queue_uses[IGC_MAX_TX_QUEUES] = { }; 6219 struct igc_hw *hw = &adapter->hw; 6220 struct timespec64 now; 6221 size_t n; 6222 6223 if (qopt->cycle_time_extension) 6224 return false; 6225 6226 igc_ptp_read(adapter, &now); 6227 6228 /* If we program the controller's BASET registers with a time 6229 * in the future, it will hold all the packets until that 6230 * time, causing a lot of TX Hangs, so to avoid that, we 6231 * reject schedules that would start in the future. 6232 * Note: Limitation above is no longer in i226. 6233 */ 6234 if (!is_base_time_past(qopt->base_time, &now) && 6235 igc_is_device_id_i225(hw)) 6236 return false; 6237 6238 for (n = 0; n < qopt->num_entries; n++) { 6239 const struct tc_taprio_sched_entry *e, *prev; 6240 int i; 6241 6242 prev = n ? &qopt->entries[n - 1] : NULL; 6243 e = &qopt->entries[n]; 6244 6245 /* i225 only supports "global" frame preemption 6246 * settings. 6247 */ 6248 if (e->command != TC_TAPRIO_CMD_SET_GATES) 6249 return false; 6250 6251 for (i = 0; i < adapter->num_tx_queues; i++) 6252 if (e->gate_mask & BIT(i)) { 6253 queue_uses[i]++; 6254 6255 /* There are limitations: A single queue cannot 6256 * be opened and closed multiple times per cycle 6257 * unless the gate stays open. Check for it. 6258 */ 6259 if (queue_uses[i] > 1 && 6260 !(prev->gate_mask & BIT(i))) 6261 return false; 6262 } 6263 } 6264 6265 return true; 6266 } 6267 6268 static int igc_tsn_enable_launchtime(struct igc_adapter *adapter, 6269 struct tc_etf_qopt_offload *qopt) 6270 { 6271 struct igc_hw *hw = &adapter->hw; 6272 int err; 6273 6274 if (hw->mac.type != igc_i225) 6275 return -EOPNOTSUPP; 6276 6277 err = igc_save_launchtime_params(adapter, qopt->queue, qopt->enable); 6278 if (err) 6279 return err; 6280 6281 return igc_tsn_offload_apply(adapter); 6282 } 6283 6284 static int igc_qbv_clear_schedule(struct igc_adapter *adapter) 6285 { 6286 unsigned long flags; 6287 int i; 6288 6289 adapter->base_time = 0; 6290 adapter->cycle_time = NSEC_PER_SEC; 6291 adapter->taprio_offload_enable = false; 6292 adapter->qbv_config_change_errors = 0; 6293 adapter->qbv_count = 0; 6294 6295 for (i = 0; i < adapter->num_tx_queues; i++) { 6296 struct igc_ring *ring = adapter->tx_ring[i]; 6297 6298 ring->start_time = 0; 6299 ring->end_time = NSEC_PER_SEC; 6300 ring->max_sdu = 0; 6301 } 6302 6303 spin_lock_irqsave(&adapter->qbv_tx_lock, flags); 6304 6305 adapter->qbv_transition = false; 6306 6307 for (i = 0; i < adapter->num_tx_queues; i++) { 6308 struct igc_ring *ring = adapter->tx_ring[i]; 6309 6310 ring->oper_gate_closed = false; 6311 ring->admin_gate_closed = false; 6312 } 6313 6314 spin_unlock_irqrestore(&adapter->qbv_tx_lock, flags); 6315 6316 return 0; 6317 } 6318 6319 static int igc_tsn_clear_schedule(struct igc_adapter *adapter) 6320 { 6321 igc_qbv_clear_schedule(adapter); 6322 6323 return 0; 6324 } 6325 6326 static void igc_taprio_stats(struct net_device *dev, 6327 struct tc_taprio_qopt_stats *stats) 6328 { 6329 /* When Strict_End is enabled, the tx_overruns counter 6330 * will always be zero. 6331 */ 6332 stats->tx_overruns = 0; 6333 } 6334 6335 static void igc_taprio_queue_stats(struct net_device *dev, 6336 struct tc_taprio_qopt_queue_stats *queue_stats) 6337 { 6338 struct tc_taprio_qopt_stats *stats = &queue_stats->stats; 6339 6340 /* When Strict_End is enabled, the tx_overruns counter 6341 * will always be zero. 6342 */ 6343 stats->tx_overruns = 0; 6344 } 6345 6346 static int igc_save_qbv_schedule(struct igc_adapter *adapter, 6347 struct tc_taprio_qopt_offload *qopt) 6348 { 6349 bool queue_configured[IGC_MAX_TX_QUEUES] = { }; 6350 struct igc_hw *hw = &adapter->hw; 6351 u32 start_time = 0, end_time = 0; 6352 struct timespec64 now; 6353 unsigned long flags; 6354 size_t n; 6355 int i; 6356 6357 if (qopt->base_time < 0) 6358 return -ERANGE; 6359 6360 if (igc_is_device_id_i225(hw) && adapter->taprio_offload_enable) 6361 return -EALREADY; 6362 6363 if (!validate_schedule(adapter, qopt)) 6364 return -EINVAL; 6365 6366 igc_ptp_read(adapter, &now); 6367 6368 if (igc_tsn_is_taprio_activated_by_user(adapter) && 6369 is_base_time_past(qopt->base_time, &now)) 6370 adapter->qbv_config_change_errors++; 6371 6372 adapter->cycle_time = qopt->cycle_time; 6373 adapter->base_time = qopt->base_time; 6374 adapter->taprio_offload_enable = true; 6375 6376 for (n = 0; n < qopt->num_entries; n++) { 6377 struct tc_taprio_sched_entry *e = &qopt->entries[n]; 6378 6379 end_time += e->interval; 6380 6381 /* If any of the conditions below are true, we need to manually 6382 * control the end time of the cycle. 6383 * 1. Qbv users can specify a cycle time that is not equal 6384 * to the total GCL intervals. Hence, recalculation is 6385 * necessary here to exclude the time interval that 6386 * exceeds the cycle time. 6387 * 2. According to IEEE Std. 802.1Q-2018 section 8.6.9.2, 6388 * once the end of the list is reached, it will switch 6389 * to the END_OF_CYCLE state and leave the gates in the 6390 * same state until the next cycle is started. 6391 */ 6392 if (end_time > adapter->cycle_time || 6393 n + 1 == qopt->num_entries) 6394 end_time = adapter->cycle_time; 6395 6396 for (i = 0; i < adapter->num_tx_queues; i++) { 6397 struct igc_ring *ring = adapter->tx_ring[i]; 6398 6399 if (!(e->gate_mask & BIT(i))) 6400 continue; 6401 6402 /* Check whether a queue stays open for more than one 6403 * entry. If so, keep the start and advance the end 6404 * time. 6405 */ 6406 if (!queue_configured[i]) 6407 ring->start_time = start_time; 6408 ring->end_time = end_time; 6409 6410 if (ring->start_time >= adapter->cycle_time) 6411 queue_configured[i] = false; 6412 else 6413 queue_configured[i] = true; 6414 } 6415 6416 start_time += e->interval; 6417 } 6418 6419 spin_lock_irqsave(&adapter->qbv_tx_lock, flags); 6420 6421 /* Check whether a queue gets configured. 6422 * If not, set the start and end time to be end time. 6423 */ 6424 for (i = 0; i < adapter->num_tx_queues; i++) { 6425 struct igc_ring *ring = adapter->tx_ring[i]; 6426 6427 if (!is_base_time_past(qopt->base_time, &now)) { 6428 ring->admin_gate_closed = false; 6429 } else { 6430 ring->oper_gate_closed = false; 6431 ring->admin_gate_closed = false; 6432 } 6433 6434 if (!queue_configured[i]) { 6435 if (!is_base_time_past(qopt->base_time, &now)) 6436 ring->admin_gate_closed = true; 6437 else 6438 ring->oper_gate_closed = true; 6439 6440 ring->start_time = end_time; 6441 ring->end_time = end_time; 6442 } 6443 } 6444 6445 spin_unlock_irqrestore(&adapter->qbv_tx_lock, flags); 6446 6447 for (i = 0; i < adapter->num_tx_queues; i++) { 6448 struct igc_ring *ring = adapter->tx_ring[i]; 6449 struct net_device *dev = adapter->netdev; 6450 6451 if (qopt->max_sdu[i]) 6452 ring->max_sdu = qopt->max_sdu[i] + dev->hard_header_len - ETH_TLEN; 6453 else 6454 ring->max_sdu = 0; 6455 } 6456 6457 return 0; 6458 } 6459 6460 static int igc_tsn_enable_qbv_scheduling(struct igc_adapter *adapter, 6461 struct tc_taprio_qopt_offload *qopt) 6462 { 6463 struct igc_hw *hw = &adapter->hw; 6464 int err; 6465 6466 if (hw->mac.type != igc_i225) 6467 return -EOPNOTSUPP; 6468 6469 switch (qopt->cmd) { 6470 case TAPRIO_CMD_REPLACE: 6471 err = igc_save_qbv_schedule(adapter, qopt); 6472 break; 6473 case TAPRIO_CMD_DESTROY: 6474 err = igc_tsn_clear_schedule(adapter); 6475 break; 6476 case TAPRIO_CMD_STATS: 6477 igc_taprio_stats(adapter->netdev, &qopt->stats); 6478 return 0; 6479 case TAPRIO_CMD_QUEUE_STATS: 6480 igc_taprio_queue_stats(adapter->netdev, &qopt->queue_stats); 6481 return 0; 6482 default: 6483 return -EOPNOTSUPP; 6484 } 6485 6486 if (err) 6487 return err; 6488 6489 return igc_tsn_offload_apply(adapter); 6490 } 6491 6492 static int igc_save_cbs_params(struct igc_adapter *adapter, int queue, 6493 bool enable, int idleslope, int sendslope, 6494 int hicredit, int locredit) 6495 { 6496 bool cbs_status[IGC_MAX_SR_QUEUES] = { false }; 6497 struct net_device *netdev = adapter->netdev; 6498 struct igc_ring *ring; 6499 int i; 6500 6501 /* i225 has two sets of credit-based shaper logic. 6502 * Supporting it only on the top two priority queues 6503 */ 6504 if (queue < 0 || queue > 1) 6505 return -EINVAL; 6506 6507 ring = adapter->tx_ring[queue]; 6508 6509 for (i = 0; i < IGC_MAX_SR_QUEUES; i++) 6510 if (adapter->tx_ring[i]) 6511 cbs_status[i] = adapter->tx_ring[i]->cbs_enable; 6512 6513 /* CBS should be enabled on the highest priority queue first in order 6514 * for the CBS algorithm to operate as intended. 6515 */ 6516 if (enable) { 6517 if (queue == 1 && !cbs_status[0]) { 6518 netdev_err(netdev, 6519 "Enabling CBS on queue1 before queue0\n"); 6520 return -EINVAL; 6521 } 6522 } else { 6523 if (queue == 0 && cbs_status[1]) { 6524 netdev_err(netdev, 6525 "Disabling CBS on queue0 before queue1\n"); 6526 return -EINVAL; 6527 } 6528 } 6529 6530 ring->cbs_enable = enable; 6531 ring->idleslope = idleslope; 6532 ring->sendslope = sendslope; 6533 ring->hicredit = hicredit; 6534 ring->locredit = locredit; 6535 6536 return 0; 6537 } 6538 6539 static int igc_tsn_enable_cbs(struct igc_adapter *adapter, 6540 struct tc_cbs_qopt_offload *qopt) 6541 { 6542 struct igc_hw *hw = &adapter->hw; 6543 int err; 6544 6545 if (hw->mac.type != igc_i225) 6546 return -EOPNOTSUPP; 6547 6548 if (qopt->queue < 0 || qopt->queue > 1) 6549 return -EINVAL; 6550 6551 err = igc_save_cbs_params(adapter, qopt->queue, qopt->enable, 6552 qopt->idleslope, qopt->sendslope, 6553 qopt->hicredit, qopt->locredit); 6554 if (err) 6555 return err; 6556 6557 return igc_tsn_offload_apply(adapter); 6558 } 6559 6560 static int igc_tc_query_caps(struct igc_adapter *adapter, 6561 struct tc_query_caps_base *base) 6562 { 6563 struct igc_hw *hw = &adapter->hw; 6564 6565 switch (base->type) { 6566 case TC_SETUP_QDISC_MQPRIO: { 6567 struct tc_mqprio_caps *caps = base->caps; 6568 6569 caps->validate_queue_counts = true; 6570 6571 return 0; 6572 } 6573 case TC_SETUP_QDISC_TAPRIO: { 6574 struct tc_taprio_caps *caps = base->caps; 6575 6576 caps->broken_mqprio = true; 6577 6578 if (hw->mac.type == igc_i225) { 6579 caps->supports_queue_max_sdu = true; 6580 caps->gate_mask_per_txq = true; 6581 } 6582 6583 return 0; 6584 } 6585 default: 6586 return -EOPNOTSUPP; 6587 } 6588 } 6589 6590 static void igc_save_mqprio_params(struct igc_adapter *adapter, u8 num_tc, 6591 u16 *offset) 6592 { 6593 int i; 6594 6595 adapter->strict_priority_enable = true; 6596 adapter->num_tc = num_tc; 6597 6598 for (i = 0; i < num_tc; i++) 6599 adapter->queue_per_tc[i] = offset[i]; 6600 } 6601 6602 static int igc_tsn_enable_mqprio(struct igc_adapter *adapter, 6603 struct tc_mqprio_qopt_offload *mqprio) 6604 { 6605 struct igc_hw *hw = &adapter->hw; 6606 int i; 6607 6608 if (hw->mac.type != igc_i225) 6609 return -EOPNOTSUPP; 6610 6611 if (!mqprio->qopt.num_tc) { 6612 adapter->strict_priority_enable = false; 6613 goto apply; 6614 } 6615 6616 /* There are as many TCs as Tx queues. */ 6617 if (mqprio->qopt.num_tc != adapter->num_tx_queues) { 6618 NL_SET_ERR_MSG_FMT_MOD(mqprio->extack, 6619 "Only %d traffic classes supported", 6620 adapter->num_tx_queues); 6621 return -EOPNOTSUPP; 6622 } 6623 6624 /* Only one queue per TC is supported. */ 6625 for (i = 0; i < mqprio->qopt.num_tc; i++) { 6626 if (mqprio->qopt.count[i] != 1) { 6627 NL_SET_ERR_MSG_MOD(mqprio->extack, 6628 "Only one queue per TC supported"); 6629 return -EOPNOTSUPP; 6630 } 6631 } 6632 6633 /* Preemption is not supported yet. */ 6634 if (mqprio->preemptible_tcs) { 6635 NL_SET_ERR_MSG_MOD(mqprio->extack, 6636 "Preemption is not supported yet"); 6637 return -EOPNOTSUPP; 6638 } 6639 6640 igc_save_mqprio_params(adapter, mqprio->qopt.num_tc, 6641 mqprio->qopt.offset); 6642 6643 mqprio->qopt.hw = TC_MQPRIO_HW_OFFLOAD_TCS; 6644 6645 apply: 6646 return igc_tsn_offload_apply(adapter); 6647 } 6648 6649 static int igc_setup_tc(struct net_device *dev, enum tc_setup_type type, 6650 void *type_data) 6651 { 6652 struct igc_adapter *adapter = netdev_priv(dev); 6653 6654 adapter->tc_setup_type = type; 6655 6656 switch (type) { 6657 case TC_QUERY_CAPS: 6658 return igc_tc_query_caps(adapter, type_data); 6659 case TC_SETUP_QDISC_TAPRIO: 6660 return igc_tsn_enable_qbv_scheduling(adapter, type_data); 6661 6662 case TC_SETUP_QDISC_ETF: 6663 return igc_tsn_enable_launchtime(adapter, type_data); 6664 6665 case TC_SETUP_QDISC_CBS: 6666 return igc_tsn_enable_cbs(adapter, type_data); 6667 6668 case TC_SETUP_QDISC_MQPRIO: 6669 return igc_tsn_enable_mqprio(adapter, type_data); 6670 6671 default: 6672 return -EOPNOTSUPP; 6673 } 6674 } 6675 6676 static int igc_bpf(struct net_device *dev, struct netdev_bpf *bpf) 6677 { 6678 struct igc_adapter *adapter = netdev_priv(dev); 6679 6680 switch (bpf->command) { 6681 case XDP_SETUP_PROG: 6682 return igc_xdp_set_prog(adapter, bpf->prog, bpf->extack); 6683 case XDP_SETUP_XSK_POOL: 6684 return igc_xdp_setup_pool(adapter, bpf->xsk.pool, 6685 bpf->xsk.queue_id); 6686 default: 6687 return -EOPNOTSUPP; 6688 } 6689 } 6690 6691 static int igc_xdp_xmit(struct net_device *dev, int num_frames, 6692 struct xdp_frame **frames, u32 flags) 6693 { 6694 struct igc_adapter *adapter = netdev_priv(dev); 6695 int cpu = smp_processor_id(); 6696 struct netdev_queue *nq; 6697 struct igc_ring *ring; 6698 int i, nxmit; 6699 6700 if (unlikely(!netif_carrier_ok(dev))) 6701 return -ENETDOWN; 6702 6703 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) 6704 return -EINVAL; 6705 6706 ring = igc_xdp_get_tx_ring(adapter, cpu); 6707 nq = txring_txq(ring); 6708 6709 __netif_tx_lock(nq, cpu); 6710 6711 /* Avoid transmit queue timeout since we share it with the slow path */ 6712 txq_trans_cond_update(nq); 6713 6714 nxmit = 0; 6715 for (i = 0; i < num_frames; i++) { 6716 int err; 6717 struct xdp_frame *xdpf = frames[i]; 6718 6719 err = igc_xdp_init_tx_descriptor(ring, xdpf); 6720 if (err) 6721 break; 6722 nxmit++; 6723 } 6724 6725 if (flags & XDP_XMIT_FLUSH) 6726 igc_flush_tx_descriptors(ring); 6727 6728 __netif_tx_unlock(nq); 6729 6730 return nxmit; 6731 } 6732 6733 static void igc_trigger_rxtxq_interrupt(struct igc_adapter *adapter, 6734 struct igc_q_vector *q_vector) 6735 { 6736 struct igc_hw *hw = &adapter->hw; 6737 u32 eics = 0; 6738 6739 eics |= q_vector->eims_value; 6740 wr32(IGC_EICS, eics); 6741 } 6742 6743 int igc_xsk_wakeup(struct net_device *dev, u32 queue_id, u32 flags) 6744 { 6745 struct igc_adapter *adapter = netdev_priv(dev); 6746 struct igc_q_vector *q_vector; 6747 struct igc_ring *ring; 6748 6749 if (test_bit(__IGC_DOWN, &adapter->state)) 6750 return -ENETDOWN; 6751 6752 if (!igc_xdp_is_enabled(adapter)) 6753 return -ENXIO; 6754 6755 if (queue_id >= adapter->num_rx_queues) 6756 return -EINVAL; 6757 6758 ring = adapter->rx_ring[queue_id]; 6759 6760 if (!ring->xsk_pool) 6761 return -ENXIO; 6762 6763 q_vector = adapter->q_vector[queue_id]; 6764 if (!napi_if_scheduled_mark_missed(&q_vector->napi)) 6765 igc_trigger_rxtxq_interrupt(adapter, q_vector); 6766 6767 return 0; 6768 } 6769 6770 static ktime_t igc_get_tstamp(struct net_device *dev, 6771 const struct skb_shared_hwtstamps *hwtstamps, 6772 bool cycles) 6773 { 6774 struct igc_adapter *adapter = netdev_priv(dev); 6775 struct igc_inline_rx_tstamps *tstamp; 6776 ktime_t timestamp; 6777 6778 tstamp = hwtstamps->netdev_data; 6779 6780 if (cycles) 6781 timestamp = igc_ptp_rx_pktstamp(adapter, tstamp->timer1); 6782 else 6783 timestamp = igc_ptp_rx_pktstamp(adapter, tstamp->timer0); 6784 6785 return timestamp; 6786 } 6787 6788 static const struct net_device_ops igc_netdev_ops = { 6789 .ndo_open = igc_open, 6790 .ndo_stop = igc_close, 6791 .ndo_start_xmit = igc_xmit_frame, 6792 .ndo_set_rx_mode = igc_set_rx_mode, 6793 .ndo_set_mac_address = igc_set_mac, 6794 .ndo_change_mtu = igc_change_mtu, 6795 .ndo_tx_timeout = igc_tx_timeout, 6796 .ndo_get_stats64 = igc_get_stats64, 6797 .ndo_fix_features = igc_fix_features, 6798 .ndo_set_features = igc_set_features, 6799 .ndo_features_check = igc_features_check, 6800 .ndo_eth_ioctl = igc_ioctl, 6801 .ndo_setup_tc = igc_setup_tc, 6802 .ndo_bpf = igc_bpf, 6803 .ndo_xdp_xmit = igc_xdp_xmit, 6804 .ndo_xsk_wakeup = igc_xsk_wakeup, 6805 .ndo_get_tstamp = igc_get_tstamp, 6806 }; 6807 6808 u32 igc_rd32(struct igc_hw *hw, u32 reg) 6809 { 6810 struct igc_adapter *igc = container_of(hw, struct igc_adapter, hw); 6811 u8 __iomem *hw_addr = READ_ONCE(hw->hw_addr); 6812 u32 value = 0; 6813 6814 if (IGC_REMOVED(hw_addr)) 6815 return ~value; 6816 6817 value = readl(&hw_addr[reg]); 6818 6819 /* reads should not return all F's */ 6820 if (!(~value) && (!reg || !(~readl(hw_addr)))) { 6821 struct net_device *netdev = igc->netdev; 6822 6823 hw->hw_addr = NULL; 6824 netif_device_detach(netdev); 6825 netdev_err(netdev, "PCIe link lost, device now detached\n"); 6826 WARN(pci_device_is_present(igc->pdev), 6827 "igc: Failed to read reg 0x%x!\n", reg); 6828 } 6829 6830 return value; 6831 } 6832 6833 /* Mapping HW RSS Type to enum xdp_rss_hash_type */ 6834 static enum xdp_rss_hash_type igc_xdp_rss_type[IGC_RSS_TYPE_MAX_TABLE] = { 6835 [IGC_RSS_TYPE_NO_HASH] = XDP_RSS_TYPE_L2, 6836 [IGC_RSS_TYPE_HASH_TCP_IPV4] = XDP_RSS_TYPE_L4_IPV4_TCP, 6837 [IGC_RSS_TYPE_HASH_IPV4] = XDP_RSS_TYPE_L3_IPV4, 6838 [IGC_RSS_TYPE_HASH_TCP_IPV6] = XDP_RSS_TYPE_L4_IPV6_TCP, 6839 [IGC_RSS_TYPE_HASH_IPV6_EX] = XDP_RSS_TYPE_L3_IPV6_EX, 6840 [IGC_RSS_TYPE_HASH_IPV6] = XDP_RSS_TYPE_L3_IPV6, 6841 [IGC_RSS_TYPE_HASH_TCP_IPV6_EX] = XDP_RSS_TYPE_L4_IPV6_TCP_EX, 6842 [IGC_RSS_TYPE_HASH_UDP_IPV4] = XDP_RSS_TYPE_L4_IPV4_UDP, 6843 [IGC_RSS_TYPE_HASH_UDP_IPV6] = XDP_RSS_TYPE_L4_IPV6_UDP, 6844 [IGC_RSS_TYPE_HASH_UDP_IPV6_EX] = XDP_RSS_TYPE_L4_IPV6_UDP_EX, 6845 [10] = XDP_RSS_TYPE_NONE, /* RSS Type above 9 "Reserved" by HW */ 6846 [11] = XDP_RSS_TYPE_NONE, /* keep array sized for SW bit-mask */ 6847 [12] = XDP_RSS_TYPE_NONE, /* to handle future HW revisons */ 6848 [13] = XDP_RSS_TYPE_NONE, 6849 [14] = XDP_RSS_TYPE_NONE, 6850 [15] = XDP_RSS_TYPE_NONE, 6851 }; 6852 6853 static int igc_xdp_rx_hash(const struct xdp_md *_ctx, u32 *hash, 6854 enum xdp_rss_hash_type *rss_type) 6855 { 6856 const struct igc_xdp_buff *ctx = (void *)_ctx; 6857 6858 if (!(ctx->xdp.rxq->dev->features & NETIF_F_RXHASH)) 6859 return -ENODATA; 6860 6861 *hash = le32_to_cpu(ctx->rx_desc->wb.lower.hi_dword.rss); 6862 *rss_type = igc_xdp_rss_type[igc_rss_type(ctx->rx_desc)]; 6863 6864 return 0; 6865 } 6866 6867 static int igc_xdp_rx_timestamp(const struct xdp_md *_ctx, u64 *timestamp) 6868 { 6869 const struct igc_xdp_buff *ctx = (void *)_ctx; 6870 struct igc_adapter *adapter = netdev_priv(ctx->xdp.rxq->dev); 6871 struct igc_inline_rx_tstamps *tstamp = ctx->rx_ts; 6872 6873 if (igc_test_staterr(ctx->rx_desc, IGC_RXDADV_STAT_TSIP)) { 6874 *timestamp = igc_ptp_rx_pktstamp(adapter, tstamp->timer0); 6875 6876 return 0; 6877 } 6878 6879 return -ENODATA; 6880 } 6881 6882 static const struct xdp_metadata_ops igc_xdp_metadata_ops = { 6883 .xmo_rx_hash = igc_xdp_rx_hash, 6884 .xmo_rx_timestamp = igc_xdp_rx_timestamp, 6885 }; 6886 6887 static enum hrtimer_restart igc_qbv_scheduling_timer(struct hrtimer *timer) 6888 { 6889 struct igc_adapter *adapter = container_of(timer, struct igc_adapter, 6890 hrtimer); 6891 unsigned long flags; 6892 unsigned int i; 6893 6894 spin_lock_irqsave(&adapter->qbv_tx_lock, flags); 6895 6896 adapter->qbv_transition = true; 6897 for (i = 0; i < adapter->num_tx_queues; i++) { 6898 struct igc_ring *tx_ring = adapter->tx_ring[i]; 6899 6900 if (tx_ring->admin_gate_closed) { 6901 tx_ring->admin_gate_closed = false; 6902 tx_ring->oper_gate_closed = true; 6903 } else { 6904 tx_ring->oper_gate_closed = false; 6905 } 6906 } 6907 adapter->qbv_transition = false; 6908 6909 spin_unlock_irqrestore(&adapter->qbv_tx_lock, flags); 6910 6911 return HRTIMER_NORESTART; 6912 } 6913 6914 /** 6915 * igc_probe - Device Initialization Routine 6916 * @pdev: PCI device information struct 6917 * @ent: entry in igc_pci_tbl 6918 * 6919 * Returns 0 on success, negative on failure 6920 * 6921 * igc_probe initializes an adapter identified by a pci_dev structure. 6922 * The OS initialization, configuring the adapter private structure, 6923 * and a hardware reset occur. 6924 */ 6925 static int igc_probe(struct pci_dev *pdev, 6926 const struct pci_device_id *ent) 6927 { 6928 struct igc_adapter *adapter; 6929 struct net_device *netdev; 6930 struct igc_hw *hw; 6931 const struct igc_info *ei = igc_info_tbl[ent->driver_data]; 6932 int err; 6933 6934 err = pci_enable_device_mem(pdev); 6935 if (err) 6936 return err; 6937 6938 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); 6939 if (err) { 6940 dev_err(&pdev->dev, 6941 "No usable DMA configuration, aborting\n"); 6942 goto err_dma; 6943 } 6944 6945 err = pci_request_mem_regions(pdev, igc_driver_name); 6946 if (err) 6947 goto err_pci_reg; 6948 6949 err = pci_enable_ptm(pdev, NULL); 6950 if (err < 0) 6951 dev_info(&pdev->dev, "PCIe PTM not supported by PCIe bus/controller\n"); 6952 6953 pci_set_master(pdev); 6954 6955 err = -ENOMEM; 6956 netdev = alloc_etherdev_mq(sizeof(struct igc_adapter), 6957 IGC_MAX_TX_QUEUES); 6958 6959 if (!netdev) 6960 goto err_alloc_etherdev; 6961 6962 SET_NETDEV_DEV(netdev, &pdev->dev); 6963 6964 pci_set_drvdata(pdev, netdev); 6965 adapter = netdev_priv(netdev); 6966 adapter->netdev = netdev; 6967 adapter->pdev = pdev; 6968 hw = &adapter->hw; 6969 hw->back = adapter; 6970 adapter->port_num = hw->bus.func; 6971 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE); 6972 6973 err = pci_save_state(pdev); 6974 if (err) 6975 goto err_ioremap; 6976 6977 err = -EIO; 6978 adapter->io_addr = ioremap(pci_resource_start(pdev, 0), 6979 pci_resource_len(pdev, 0)); 6980 if (!adapter->io_addr) 6981 goto err_ioremap; 6982 6983 /* hw->hw_addr can be zeroed, so use adapter->io_addr for unmap */ 6984 hw->hw_addr = adapter->io_addr; 6985 6986 netdev->netdev_ops = &igc_netdev_ops; 6987 netdev->xdp_metadata_ops = &igc_xdp_metadata_ops; 6988 netdev->xsk_tx_metadata_ops = &igc_xsk_tx_metadata_ops; 6989 igc_ethtool_set_ops(netdev); 6990 netdev->watchdog_timeo = 5 * HZ; 6991 6992 netdev->mem_start = pci_resource_start(pdev, 0); 6993 netdev->mem_end = pci_resource_end(pdev, 0); 6994 6995 /* PCI config space info */ 6996 hw->vendor_id = pdev->vendor; 6997 hw->device_id = pdev->device; 6998 hw->revision_id = pdev->revision; 6999 hw->subsystem_vendor_id = pdev->subsystem_vendor; 7000 hw->subsystem_device_id = pdev->subsystem_device; 7001 7002 /* Copy the default MAC and PHY function pointers */ 7003 memcpy(&hw->mac.ops, ei->mac_ops, sizeof(hw->mac.ops)); 7004 memcpy(&hw->phy.ops, ei->phy_ops, sizeof(hw->phy.ops)); 7005 7006 /* Initialize skew-specific constants */ 7007 err = ei->get_invariants(hw); 7008 if (err) 7009 goto err_sw_init; 7010 7011 /* Add supported features to the features list*/ 7012 netdev->features |= NETIF_F_SG; 7013 netdev->features |= NETIF_F_TSO; 7014 netdev->features |= NETIF_F_TSO6; 7015 netdev->features |= NETIF_F_TSO_ECN; 7016 netdev->features |= NETIF_F_RXHASH; 7017 netdev->features |= NETIF_F_RXCSUM; 7018 netdev->features |= NETIF_F_HW_CSUM; 7019 netdev->features |= NETIF_F_SCTP_CRC; 7020 netdev->features |= NETIF_F_HW_TC; 7021 7022 #define IGC_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE | \ 7023 NETIF_F_GSO_GRE_CSUM | \ 7024 NETIF_F_GSO_IPXIP4 | \ 7025 NETIF_F_GSO_IPXIP6 | \ 7026 NETIF_F_GSO_UDP_TUNNEL | \ 7027 NETIF_F_GSO_UDP_TUNNEL_CSUM) 7028 7029 netdev->gso_partial_features = IGC_GSO_PARTIAL_FEATURES; 7030 netdev->features |= NETIF_F_GSO_PARTIAL | IGC_GSO_PARTIAL_FEATURES; 7031 7032 /* setup the private structure */ 7033 err = igc_sw_init(adapter); 7034 if (err) 7035 goto err_sw_init; 7036 7037 /* copy netdev features into list of user selectable features */ 7038 netdev->hw_features |= NETIF_F_NTUPLE; 7039 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX; 7040 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX; 7041 netdev->hw_features |= netdev->features; 7042 7043 netdev->features |= NETIF_F_HIGHDMA; 7044 7045 netdev->vlan_features |= netdev->features | NETIF_F_TSO_MANGLEID; 7046 netdev->mpls_features |= NETIF_F_HW_CSUM; 7047 netdev->hw_enc_features |= netdev->vlan_features; 7048 7049 netdev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT | 7050 NETDEV_XDP_ACT_XSK_ZEROCOPY; 7051 7052 /* MTU range: 68 - 9216 */ 7053 netdev->min_mtu = ETH_MIN_MTU; 7054 netdev->max_mtu = MAX_STD_JUMBO_FRAME_SIZE; 7055 7056 /* before reading the NVM, reset the controller to put the device in a 7057 * known good starting state 7058 */ 7059 hw->mac.ops.reset_hw(hw); 7060 7061 if (igc_get_flash_presence_i225(hw)) { 7062 if (hw->nvm.ops.validate(hw) < 0) { 7063 dev_err(&pdev->dev, "The NVM Checksum Is Not Valid\n"); 7064 err = -EIO; 7065 goto err_eeprom; 7066 } 7067 } 7068 7069 if (eth_platform_get_mac_address(&pdev->dev, hw->mac.addr)) { 7070 /* copy the MAC address out of the NVM */ 7071 if (hw->mac.ops.read_mac_addr(hw)) 7072 dev_err(&pdev->dev, "NVM Read Error\n"); 7073 } 7074 7075 eth_hw_addr_set(netdev, hw->mac.addr); 7076 7077 if (!is_valid_ether_addr(netdev->dev_addr)) { 7078 dev_err(&pdev->dev, "Invalid MAC Address\n"); 7079 err = -EIO; 7080 goto err_eeprom; 7081 } 7082 7083 /* configure RXPBSIZE and TXPBSIZE */ 7084 wr32(IGC_RXPBS, I225_RXPBSIZE_DEFAULT); 7085 wr32(IGC_TXPBS, I225_TXPBSIZE_DEFAULT); 7086 7087 timer_setup(&adapter->watchdog_timer, igc_watchdog, 0); 7088 timer_setup(&adapter->phy_info_timer, igc_update_phy_info, 0); 7089 7090 INIT_WORK(&adapter->reset_task, igc_reset_task); 7091 INIT_WORK(&adapter->watchdog_task, igc_watchdog_task); 7092 7093 hrtimer_init(&adapter->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 7094 adapter->hrtimer.function = &igc_qbv_scheduling_timer; 7095 7096 /* Initialize link properties that are user-changeable */ 7097 adapter->fc_autoneg = true; 7098 hw->phy.autoneg_advertised = 0xaf; 7099 7100 hw->fc.requested_mode = igc_fc_default; 7101 hw->fc.current_mode = igc_fc_default; 7102 7103 /* By default, support wake on port A */ 7104 adapter->flags |= IGC_FLAG_WOL_SUPPORTED; 7105 7106 /* initialize the wol settings based on the eeprom settings */ 7107 if (adapter->flags & IGC_FLAG_WOL_SUPPORTED) 7108 adapter->wol |= IGC_WUFC_MAG; 7109 7110 device_set_wakeup_enable(&adapter->pdev->dev, 7111 adapter->flags & IGC_FLAG_WOL_SUPPORTED); 7112 7113 igc_ptp_init(adapter); 7114 7115 igc_tsn_clear_schedule(adapter); 7116 7117 /* reset the hardware with the new settings */ 7118 igc_reset(adapter); 7119 7120 /* let the f/w know that the h/w is now under the control of the 7121 * driver. 7122 */ 7123 igc_get_hw_control(adapter); 7124 7125 strscpy(netdev->name, "eth%d", sizeof(netdev->name)); 7126 err = register_netdev(netdev); 7127 if (err) 7128 goto err_register; 7129 7130 /* carrier off reporting is important to ethtool even BEFORE open */ 7131 netif_carrier_off(netdev); 7132 7133 /* Check if Media Autosense is enabled */ 7134 adapter->ei = *ei; 7135 7136 /* print pcie link status and MAC address */ 7137 pcie_print_link_status(pdev); 7138 netdev_info(netdev, "MAC: %pM\n", netdev->dev_addr); 7139 7140 dev_pm_set_driver_flags(&pdev->dev, DPM_FLAG_NO_DIRECT_COMPLETE); 7141 /* Disable EEE for internal PHY devices */ 7142 hw->dev_spec._base.eee_enable = false; 7143 adapter->flags &= ~IGC_FLAG_EEE; 7144 igc_set_eee_i225(hw, false, false, false); 7145 7146 pm_runtime_put_noidle(&pdev->dev); 7147 7148 if (IS_ENABLED(CONFIG_IGC_LEDS)) { 7149 err = igc_led_setup(adapter); 7150 if (err) 7151 goto err_register; 7152 } 7153 7154 return 0; 7155 7156 err_register: 7157 igc_release_hw_control(adapter); 7158 err_eeprom: 7159 if (!igc_check_reset_block(hw)) 7160 igc_reset_phy(hw); 7161 err_sw_init: 7162 igc_clear_interrupt_scheme(adapter); 7163 iounmap(adapter->io_addr); 7164 err_ioremap: 7165 free_netdev(netdev); 7166 err_alloc_etherdev: 7167 pci_release_mem_regions(pdev); 7168 err_pci_reg: 7169 err_dma: 7170 pci_disable_device(pdev); 7171 return err; 7172 } 7173 7174 /** 7175 * igc_remove - Device Removal Routine 7176 * @pdev: PCI device information struct 7177 * 7178 * igc_remove is called by the PCI subsystem to alert the driver 7179 * that it should release a PCI device. This could be caused by a 7180 * Hot-Plug event, or because the driver is going to be removed from 7181 * memory. 7182 */ 7183 static void igc_remove(struct pci_dev *pdev) 7184 { 7185 struct net_device *netdev = pci_get_drvdata(pdev); 7186 struct igc_adapter *adapter = netdev_priv(netdev); 7187 7188 pm_runtime_get_noresume(&pdev->dev); 7189 7190 igc_flush_nfc_rules(adapter); 7191 7192 igc_ptp_stop(adapter); 7193 7194 pci_disable_ptm(pdev); 7195 pci_clear_master(pdev); 7196 7197 set_bit(__IGC_DOWN, &adapter->state); 7198 7199 del_timer_sync(&adapter->watchdog_timer); 7200 del_timer_sync(&adapter->phy_info_timer); 7201 7202 cancel_work_sync(&adapter->reset_task); 7203 cancel_work_sync(&adapter->watchdog_task); 7204 hrtimer_cancel(&adapter->hrtimer); 7205 7206 if (IS_ENABLED(CONFIG_IGC_LEDS)) 7207 igc_led_free(adapter); 7208 7209 /* Release control of h/w to f/w. If f/w is AMT enabled, this 7210 * would have already happened in close and is redundant. 7211 */ 7212 igc_release_hw_control(adapter); 7213 unregister_netdev(netdev); 7214 7215 igc_clear_interrupt_scheme(adapter); 7216 pci_iounmap(pdev, adapter->io_addr); 7217 pci_release_mem_regions(pdev); 7218 7219 free_netdev(netdev); 7220 7221 pci_disable_device(pdev); 7222 } 7223 7224 static int __igc_shutdown(struct pci_dev *pdev, bool *enable_wake, 7225 bool runtime) 7226 { 7227 struct net_device *netdev = pci_get_drvdata(pdev); 7228 struct igc_adapter *adapter = netdev_priv(netdev); 7229 u32 wufc = runtime ? IGC_WUFC_LNKC : adapter->wol; 7230 struct igc_hw *hw = &adapter->hw; 7231 u32 ctrl, rctl, status; 7232 bool wake; 7233 7234 rtnl_lock(); 7235 netif_device_detach(netdev); 7236 7237 if (netif_running(netdev)) 7238 __igc_close(netdev, true); 7239 7240 igc_ptp_suspend(adapter); 7241 7242 igc_clear_interrupt_scheme(adapter); 7243 rtnl_unlock(); 7244 7245 status = rd32(IGC_STATUS); 7246 if (status & IGC_STATUS_LU) 7247 wufc &= ~IGC_WUFC_LNKC; 7248 7249 if (wufc) { 7250 igc_setup_rctl(adapter); 7251 igc_set_rx_mode(netdev); 7252 7253 /* turn on all-multi mode if wake on multicast is enabled */ 7254 if (wufc & IGC_WUFC_MC) { 7255 rctl = rd32(IGC_RCTL); 7256 rctl |= IGC_RCTL_MPE; 7257 wr32(IGC_RCTL, rctl); 7258 } 7259 7260 ctrl = rd32(IGC_CTRL); 7261 ctrl |= IGC_CTRL_ADVD3WUC; 7262 wr32(IGC_CTRL, ctrl); 7263 7264 /* Allow time for pending master requests to run */ 7265 igc_disable_pcie_master(hw); 7266 7267 wr32(IGC_WUC, IGC_WUC_PME_EN); 7268 wr32(IGC_WUFC, wufc); 7269 } else { 7270 wr32(IGC_WUC, 0); 7271 wr32(IGC_WUFC, 0); 7272 } 7273 7274 wake = wufc || adapter->en_mng_pt; 7275 if (!wake) 7276 igc_power_down_phy_copper_base(&adapter->hw); 7277 else 7278 igc_power_up_link(adapter); 7279 7280 if (enable_wake) 7281 *enable_wake = wake; 7282 7283 /* Release control of h/w to f/w. If f/w is AMT enabled, this 7284 * would have already happened in close and is redundant. 7285 */ 7286 igc_release_hw_control(adapter); 7287 7288 pci_disable_device(pdev); 7289 7290 return 0; 7291 } 7292 7293 static int igc_runtime_suspend(struct device *dev) 7294 { 7295 return __igc_shutdown(to_pci_dev(dev), NULL, 1); 7296 } 7297 7298 static void igc_deliver_wake_packet(struct net_device *netdev) 7299 { 7300 struct igc_adapter *adapter = netdev_priv(netdev); 7301 struct igc_hw *hw = &adapter->hw; 7302 struct sk_buff *skb; 7303 u32 wupl; 7304 7305 wupl = rd32(IGC_WUPL) & IGC_WUPL_MASK; 7306 7307 /* WUPM stores only the first 128 bytes of the wake packet. 7308 * Read the packet only if we have the whole thing. 7309 */ 7310 if (wupl == 0 || wupl > IGC_WUPM_BYTES) 7311 return; 7312 7313 skb = netdev_alloc_skb_ip_align(netdev, IGC_WUPM_BYTES); 7314 if (!skb) 7315 return; 7316 7317 skb_put(skb, wupl); 7318 7319 /* Ensure reads are 32-bit aligned */ 7320 wupl = roundup(wupl, 4); 7321 7322 memcpy_fromio(skb->data, hw->hw_addr + IGC_WUPM_REG(0), wupl); 7323 7324 skb->protocol = eth_type_trans(skb, netdev); 7325 netif_rx(skb); 7326 } 7327 7328 static int __igc_resume(struct device *dev, bool rpm) 7329 { 7330 struct pci_dev *pdev = to_pci_dev(dev); 7331 struct net_device *netdev = pci_get_drvdata(pdev); 7332 struct igc_adapter *adapter = netdev_priv(netdev); 7333 struct igc_hw *hw = &adapter->hw; 7334 u32 err, val; 7335 7336 pci_set_power_state(pdev, PCI_D0); 7337 pci_restore_state(pdev); 7338 pci_save_state(pdev); 7339 7340 if (!pci_device_is_present(pdev)) 7341 return -ENODEV; 7342 err = pci_enable_device_mem(pdev); 7343 if (err) { 7344 netdev_err(netdev, "Cannot enable PCI device from suspend\n"); 7345 return err; 7346 } 7347 pci_set_master(pdev); 7348 7349 pci_enable_wake(pdev, PCI_D3hot, 0); 7350 pci_enable_wake(pdev, PCI_D3cold, 0); 7351 7352 if (igc_init_interrupt_scheme(adapter, true)) { 7353 netdev_err(netdev, "Unable to allocate memory for queues\n"); 7354 return -ENOMEM; 7355 } 7356 7357 igc_reset(adapter); 7358 7359 /* let the f/w know that the h/w is now under the control of the 7360 * driver. 7361 */ 7362 igc_get_hw_control(adapter); 7363 7364 val = rd32(IGC_WUS); 7365 if (val & WAKE_PKT_WUS) 7366 igc_deliver_wake_packet(netdev); 7367 7368 wr32(IGC_WUS, ~0); 7369 7370 if (netif_running(netdev)) { 7371 if (!rpm) 7372 rtnl_lock(); 7373 err = __igc_open(netdev, true); 7374 if (!rpm) 7375 rtnl_unlock(); 7376 if (!err) 7377 netif_device_attach(netdev); 7378 } 7379 7380 return err; 7381 } 7382 7383 static int igc_resume(struct device *dev) 7384 { 7385 return __igc_resume(dev, false); 7386 } 7387 7388 static int igc_runtime_resume(struct device *dev) 7389 { 7390 return __igc_resume(dev, true); 7391 } 7392 7393 static int igc_suspend(struct device *dev) 7394 { 7395 return __igc_shutdown(to_pci_dev(dev), NULL, 0); 7396 } 7397 7398 static int __maybe_unused igc_runtime_idle(struct device *dev) 7399 { 7400 struct net_device *netdev = dev_get_drvdata(dev); 7401 struct igc_adapter *adapter = netdev_priv(netdev); 7402 7403 if (!igc_has_link(adapter)) 7404 pm_schedule_suspend(dev, MSEC_PER_SEC * 5); 7405 7406 return -EBUSY; 7407 } 7408 7409 static void igc_shutdown(struct pci_dev *pdev) 7410 { 7411 bool wake; 7412 7413 __igc_shutdown(pdev, &wake, 0); 7414 7415 if (system_state == SYSTEM_POWER_OFF) { 7416 pci_wake_from_d3(pdev, wake); 7417 pci_set_power_state(pdev, PCI_D3hot); 7418 } 7419 } 7420 7421 /** 7422 * igc_io_error_detected - called when PCI error is detected 7423 * @pdev: Pointer to PCI device 7424 * @state: The current PCI connection state 7425 * 7426 * This function is called after a PCI bus error affecting 7427 * this device has been detected. 7428 **/ 7429 static pci_ers_result_t igc_io_error_detected(struct pci_dev *pdev, 7430 pci_channel_state_t state) 7431 { 7432 struct net_device *netdev = pci_get_drvdata(pdev); 7433 struct igc_adapter *adapter = netdev_priv(netdev); 7434 7435 rtnl_lock(); 7436 netif_device_detach(netdev); 7437 7438 if (state == pci_channel_io_perm_failure) { 7439 rtnl_unlock(); 7440 return PCI_ERS_RESULT_DISCONNECT; 7441 } 7442 7443 if (netif_running(netdev)) 7444 igc_down(adapter); 7445 pci_disable_device(pdev); 7446 rtnl_unlock(); 7447 7448 /* Request a slot reset. */ 7449 return PCI_ERS_RESULT_NEED_RESET; 7450 } 7451 7452 /** 7453 * igc_io_slot_reset - called after the PCI bus has been reset. 7454 * @pdev: Pointer to PCI device 7455 * 7456 * Restart the card from scratch, as if from a cold-boot. Implementation 7457 * resembles the first-half of the __igc_resume routine. 7458 **/ 7459 static pci_ers_result_t igc_io_slot_reset(struct pci_dev *pdev) 7460 { 7461 struct net_device *netdev = pci_get_drvdata(pdev); 7462 struct igc_adapter *adapter = netdev_priv(netdev); 7463 struct igc_hw *hw = &adapter->hw; 7464 pci_ers_result_t result; 7465 7466 if (pci_enable_device_mem(pdev)) { 7467 netdev_err(netdev, "Could not re-enable PCI device after reset\n"); 7468 result = PCI_ERS_RESULT_DISCONNECT; 7469 } else { 7470 pci_set_master(pdev); 7471 pci_restore_state(pdev); 7472 pci_save_state(pdev); 7473 7474 pci_enable_wake(pdev, PCI_D3hot, 0); 7475 pci_enable_wake(pdev, PCI_D3cold, 0); 7476 7477 /* In case of PCI error, adapter loses its HW address 7478 * so we should re-assign it here. 7479 */ 7480 hw->hw_addr = adapter->io_addr; 7481 7482 igc_reset(adapter); 7483 wr32(IGC_WUS, ~0); 7484 result = PCI_ERS_RESULT_RECOVERED; 7485 } 7486 7487 return result; 7488 } 7489 7490 /** 7491 * igc_io_resume - called when traffic can start to flow again. 7492 * @pdev: Pointer to PCI device 7493 * 7494 * This callback is called when the error recovery driver tells us that 7495 * its OK to resume normal operation. Implementation resembles the 7496 * second-half of the __igc_resume routine. 7497 */ 7498 static void igc_io_resume(struct pci_dev *pdev) 7499 { 7500 struct net_device *netdev = pci_get_drvdata(pdev); 7501 struct igc_adapter *adapter = netdev_priv(netdev); 7502 7503 rtnl_lock(); 7504 if (netif_running(netdev)) { 7505 if (igc_open(netdev)) { 7506 rtnl_unlock(); 7507 netdev_err(netdev, "igc_open failed after reset\n"); 7508 return; 7509 } 7510 } 7511 7512 netif_device_attach(netdev); 7513 7514 /* let the f/w know that the h/w is now under the control of the 7515 * driver. 7516 */ 7517 igc_get_hw_control(adapter); 7518 rtnl_unlock(); 7519 } 7520 7521 static const struct pci_error_handlers igc_err_handler = { 7522 .error_detected = igc_io_error_detected, 7523 .slot_reset = igc_io_slot_reset, 7524 .resume = igc_io_resume, 7525 }; 7526 7527 static _DEFINE_DEV_PM_OPS(igc_pm_ops, igc_suspend, igc_resume, 7528 igc_runtime_suspend, igc_runtime_resume, 7529 igc_runtime_idle); 7530 7531 static struct pci_driver igc_driver = { 7532 .name = igc_driver_name, 7533 .id_table = igc_pci_tbl, 7534 .probe = igc_probe, 7535 .remove = igc_remove, 7536 .driver.pm = pm_ptr(&igc_pm_ops), 7537 .shutdown = igc_shutdown, 7538 .err_handler = &igc_err_handler, 7539 }; 7540 7541 /** 7542 * igc_reinit_queues - return error 7543 * @adapter: pointer to adapter structure 7544 */ 7545 int igc_reinit_queues(struct igc_adapter *adapter) 7546 { 7547 struct net_device *netdev = adapter->netdev; 7548 int err = 0; 7549 7550 if (netif_running(netdev)) 7551 igc_close(netdev); 7552 7553 igc_reset_interrupt_capability(adapter); 7554 7555 if (igc_init_interrupt_scheme(adapter, true)) { 7556 netdev_err(netdev, "Unable to allocate memory for queues\n"); 7557 return -ENOMEM; 7558 } 7559 7560 if (netif_running(netdev)) 7561 err = igc_open(netdev); 7562 7563 return err; 7564 } 7565 7566 /** 7567 * igc_get_hw_dev - return device 7568 * @hw: pointer to hardware structure 7569 * 7570 * used by hardware layer to print debugging information 7571 */ 7572 struct net_device *igc_get_hw_dev(struct igc_hw *hw) 7573 { 7574 struct igc_adapter *adapter = hw->back; 7575 7576 return adapter->netdev; 7577 } 7578 7579 static void igc_disable_rx_ring_hw(struct igc_ring *ring) 7580 { 7581 struct igc_hw *hw = &ring->q_vector->adapter->hw; 7582 u8 idx = ring->reg_idx; 7583 u32 rxdctl; 7584 7585 rxdctl = rd32(IGC_RXDCTL(idx)); 7586 rxdctl &= ~IGC_RXDCTL_QUEUE_ENABLE; 7587 rxdctl |= IGC_RXDCTL_SWFLUSH; 7588 wr32(IGC_RXDCTL(idx), rxdctl); 7589 } 7590 7591 void igc_disable_rx_ring(struct igc_ring *ring) 7592 { 7593 igc_disable_rx_ring_hw(ring); 7594 igc_clean_rx_ring(ring); 7595 } 7596 7597 void igc_enable_rx_ring(struct igc_ring *ring) 7598 { 7599 struct igc_adapter *adapter = ring->q_vector->adapter; 7600 7601 igc_configure_rx_ring(adapter, ring); 7602 7603 if (ring->xsk_pool) 7604 igc_alloc_rx_buffers_zc(ring, igc_desc_unused(ring)); 7605 else 7606 igc_alloc_rx_buffers(ring, igc_desc_unused(ring)); 7607 } 7608 7609 void igc_disable_tx_ring(struct igc_ring *ring) 7610 { 7611 igc_disable_tx_ring_hw(ring); 7612 igc_clean_tx_ring(ring); 7613 } 7614 7615 void igc_enable_tx_ring(struct igc_ring *ring) 7616 { 7617 struct igc_adapter *adapter = ring->q_vector->adapter; 7618 7619 igc_configure_tx_ring(adapter, ring); 7620 } 7621 7622 /** 7623 * igc_init_module - Driver Registration Routine 7624 * 7625 * igc_init_module is the first routine called when the driver is 7626 * loaded. All it does is register with the PCI subsystem. 7627 */ 7628 static int __init igc_init_module(void) 7629 { 7630 int ret; 7631 7632 pr_info("%s\n", igc_driver_string); 7633 pr_info("%s\n", igc_copyright); 7634 7635 ret = pci_register_driver(&igc_driver); 7636 return ret; 7637 } 7638 7639 module_init(igc_init_module); 7640 7641 /** 7642 * igc_exit_module - Driver Exit Cleanup Routine 7643 * 7644 * igc_exit_module is called just before the driver is removed 7645 * from memory. 7646 */ 7647 static void __exit igc_exit_module(void) 7648 { 7649 pci_unregister_driver(&igc_driver); 7650 } 7651 7652 module_exit(igc_exit_module); 7653 /* igc_main.c */ 7654