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