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