1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018 Intel Corporation */
4 #include <linux/module.h>
5 #include <linux/types.h>
6 #include <linux/if_vlan.h>
11 #include <linux/pm_runtime.h>
12 #include <net/pkt_sched.h>
13 #include <linux/bpf_trace.h>
22 #define DRV_SUMMARY "Intel(R) 2.5G Ethernet Linux Driver"
24 #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
26 #define IGC_XDP_PASS 0
27 #define IGC_XDP_CONSUMED BIT(0)
28 #define IGC_XDP_TX BIT(1)
29 #define IGC_XDP_REDIRECT BIT(2)
31 static int debug = -1;
33 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
34 MODULE_DESCRIPTION(DRV_SUMMARY);
35 MODULE_LICENSE("GPL v2");
36 module_param(debug, int, 0);
37 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
39 char igc_driver_name[] = "igc";
40 static const char igc_driver_string[] = DRV_SUMMARY;
41 static const char igc_copyright[] =
42 "Copyright(c) 2018 Intel Corporation.";
44 static const struct igc_info *igc_info_tbl[] = {
45 [board_base] = &igc_base_info,
48 static const struct pci_device_id igc_pci_tbl[] = {
49 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_LM), board_base },
50 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_V), board_base },
51 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_I), board_base },
52 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I220_V), board_base },
53 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_K), board_base },
54 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_K2), board_base },
55 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I226_K), board_base },
56 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_LMVP), board_base },
57 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_IT), board_base },
58 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I226_LM), board_base },
59 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I226_V), board_base },
60 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I226_IT), board_base },
61 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I221_V), board_base },
62 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I226_BLANK_NVM), board_base },
63 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_BLANK_NVM), board_base },
64 /* required last entry */
68 MODULE_DEVICE_TABLE(pci, igc_pci_tbl);
77 void igc_reset(struct igc_adapter *adapter)
79 struct net_device *dev = adapter->netdev;
80 struct igc_hw *hw = &adapter->hw;
81 struct igc_fc_info *fc = &hw->fc;
84 /* Repartition PBA for greater than 9k MTU if required */
87 /* flow control settings
88 * The high water mark must be low enough to fit one full frame
89 * after transmitting the pause frame. As such we must have enough
90 * space to allow for us to complete our current transmit and then
91 * receive the frame that is in progress from the link partner.
93 * - the full Rx FIFO size minus one full Tx plus one full Rx frame
95 hwm = (pba << 10) - (adapter->max_frame_size + MAX_JUMBO_FRAME_SIZE);
97 fc->high_water = hwm & 0xFFFFFFF0; /* 16-byte granularity */
98 fc->low_water = fc->high_water - 16;
99 fc->pause_time = 0xFFFF;
101 fc->current_mode = fc->requested_mode;
103 hw->mac.ops.reset_hw(hw);
105 if (hw->mac.ops.init_hw(hw))
106 netdev_err(dev, "Error on hardware initialization\n");
108 /* Re-establish EEE setting */
109 igc_set_eee_i225(hw, true, true, true);
111 if (!netif_running(adapter->netdev))
112 igc_power_down_phy_copper_base(&adapter->hw);
114 /* Re-enable PTP, where applicable. */
115 igc_ptp_reset(adapter);
117 /* Re-enable TSN offloading, where applicable. */
118 igc_tsn_offload_apply(adapter);
120 igc_get_phy_info(hw);
124 * igc_power_up_link - Power up the phy link
125 * @adapter: address of board private structure
127 static void igc_power_up_link(struct igc_adapter *adapter)
129 igc_reset_phy(&adapter->hw);
131 igc_power_up_phy_copper(&adapter->hw);
133 igc_setup_link(&adapter->hw);
137 * igc_release_hw_control - release control of the h/w to f/w
138 * @adapter: address of board private structure
140 * igc_release_hw_control resets CTRL_EXT:DRV_LOAD bit.
141 * For ASF and Pass Through versions of f/w this means that the
142 * driver is no longer loaded.
144 static void igc_release_hw_control(struct igc_adapter *adapter)
146 struct igc_hw *hw = &adapter->hw;
149 /* Let firmware take over control of h/w */
150 ctrl_ext = rd32(IGC_CTRL_EXT);
152 ctrl_ext & ~IGC_CTRL_EXT_DRV_LOAD);
156 * igc_get_hw_control - get control of the h/w from f/w
157 * @adapter: address of board private structure
159 * igc_get_hw_control sets CTRL_EXT:DRV_LOAD bit.
160 * For ASF and Pass Through versions of f/w this means that
161 * the driver is loaded.
163 static void igc_get_hw_control(struct igc_adapter *adapter)
165 struct igc_hw *hw = &adapter->hw;
168 /* Let firmware know the driver has taken over */
169 ctrl_ext = rd32(IGC_CTRL_EXT);
171 ctrl_ext | IGC_CTRL_EXT_DRV_LOAD);
175 * igc_clean_tx_ring - Free Tx Buffers
176 * @tx_ring: ring to be cleaned
178 static void igc_clean_tx_ring(struct igc_ring *tx_ring)
180 u16 i = tx_ring->next_to_clean;
181 struct igc_tx_buffer *tx_buffer = &tx_ring->tx_buffer_info[i];
183 while (i != tx_ring->next_to_use) {
184 union igc_adv_tx_desc *eop_desc, *tx_desc;
186 if (tx_buffer->tx_flags & IGC_TX_FLAGS_XDP)
187 xdp_return_frame(tx_buffer->xdpf);
189 dev_kfree_skb_any(tx_buffer->skb);
191 /* unmap skb header data */
192 dma_unmap_single(tx_ring->dev,
193 dma_unmap_addr(tx_buffer, dma),
194 dma_unmap_len(tx_buffer, len),
197 /* check for eop_desc to determine the end of the packet */
198 eop_desc = tx_buffer->next_to_watch;
199 tx_desc = IGC_TX_DESC(tx_ring, i);
201 /* unmap remaining buffers */
202 while (tx_desc != eop_desc) {
206 if (unlikely(i == tx_ring->count)) {
208 tx_buffer = tx_ring->tx_buffer_info;
209 tx_desc = IGC_TX_DESC(tx_ring, 0);
212 /* unmap any remaining paged data */
213 if (dma_unmap_len(tx_buffer, len))
214 dma_unmap_page(tx_ring->dev,
215 dma_unmap_addr(tx_buffer, dma),
216 dma_unmap_len(tx_buffer, len),
220 /* move us one more past the eop_desc for start of next pkt */
223 if (unlikely(i == tx_ring->count)) {
225 tx_buffer = tx_ring->tx_buffer_info;
229 /* reset BQL for queue */
230 netdev_tx_reset_queue(txring_txq(tx_ring));
232 /* reset next_to_use and next_to_clean */
233 tx_ring->next_to_use = 0;
234 tx_ring->next_to_clean = 0;
238 * igc_free_tx_resources - Free Tx Resources per Queue
239 * @tx_ring: Tx descriptor ring for a specific queue
241 * Free all transmit software resources
243 void igc_free_tx_resources(struct igc_ring *tx_ring)
245 igc_clean_tx_ring(tx_ring);
247 vfree(tx_ring->tx_buffer_info);
248 tx_ring->tx_buffer_info = NULL;
250 /* if not set, then don't free */
254 dma_free_coherent(tx_ring->dev, tx_ring->size,
255 tx_ring->desc, tx_ring->dma);
257 tx_ring->desc = NULL;
261 * igc_free_all_tx_resources - Free Tx Resources for All Queues
262 * @adapter: board private structure
264 * Free all transmit software resources
266 static void igc_free_all_tx_resources(struct igc_adapter *adapter)
270 for (i = 0; i < adapter->num_tx_queues; i++)
271 igc_free_tx_resources(adapter->tx_ring[i]);
275 * igc_clean_all_tx_rings - Free Tx Buffers for all queues
276 * @adapter: board private structure
278 static void igc_clean_all_tx_rings(struct igc_adapter *adapter)
282 for (i = 0; i < adapter->num_tx_queues; i++)
283 if (adapter->tx_ring[i])
284 igc_clean_tx_ring(adapter->tx_ring[i]);
288 * igc_setup_tx_resources - allocate Tx resources (Descriptors)
289 * @tx_ring: tx descriptor ring (for a specific queue) to setup
291 * Return 0 on success, negative on failure
293 int igc_setup_tx_resources(struct igc_ring *tx_ring)
295 struct net_device *ndev = tx_ring->netdev;
296 struct device *dev = tx_ring->dev;
299 size = sizeof(struct igc_tx_buffer) * tx_ring->count;
300 tx_ring->tx_buffer_info = vzalloc(size);
301 if (!tx_ring->tx_buffer_info)
304 /* round up to nearest 4K */
305 tx_ring->size = tx_ring->count * sizeof(union igc_adv_tx_desc);
306 tx_ring->size = ALIGN(tx_ring->size, 4096);
308 tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
309 &tx_ring->dma, GFP_KERNEL);
314 tx_ring->next_to_use = 0;
315 tx_ring->next_to_clean = 0;
320 vfree(tx_ring->tx_buffer_info);
321 netdev_err(ndev, "Unable to allocate memory for Tx descriptor ring\n");
326 * igc_setup_all_tx_resources - wrapper to allocate Tx resources for all queues
327 * @adapter: board private structure
329 * Return 0 on success, negative on failure
331 static int igc_setup_all_tx_resources(struct igc_adapter *adapter)
333 struct net_device *dev = adapter->netdev;
336 for (i = 0; i < adapter->num_tx_queues; i++) {
337 err = igc_setup_tx_resources(adapter->tx_ring[i]);
339 netdev_err(dev, "Error on Tx queue %u setup\n", i);
340 for (i--; i >= 0; i--)
341 igc_free_tx_resources(adapter->tx_ring[i]);
350 * igc_clean_rx_ring - Free Rx Buffers per Queue
351 * @rx_ring: ring to free buffers from
353 static void igc_clean_rx_ring(struct igc_ring *rx_ring)
355 u16 i = rx_ring->next_to_clean;
357 dev_kfree_skb(rx_ring->skb);
360 /* Free all the Rx ring sk_buffs */
361 while (i != rx_ring->next_to_alloc) {
362 struct igc_rx_buffer *buffer_info = &rx_ring->rx_buffer_info[i];
364 /* Invalidate cache lines that may have been written to by
365 * device so that we avoid corrupting memory.
367 dma_sync_single_range_for_cpu(rx_ring->dev,
369 buffer_info->page_offset,
370 igc_rx_bufsz(rx_ring),
373 /* free resources associated with mapping */
374 dma_unmap_page_attrs(rx_ring->dev,
376 igc_rx_pg_size(rx_ring),
379 __page_frag_cache_drain(buffer_info->page,
380 buffer_info->pagecnt_bias);
383 if (i == rx_ring->count)
387 clear_ring_uses_large_buffer(rx_ring);
389 rx_ring->next_to_alloc = 0;
390 rx_ring->next_to_clean = 0;
391 rx_ring->next_to_use = 0;
395 * igc_clean_all_rx_rings - Free Rx Buffers for all queues
396 * @adapter: board private structure
398 static void igc_clean_all_rx_rings(struct igc_adapter *adapter)
402 for (i = 0; i < adapter->num_rx_queues; i++)
403 if (adapter->rx_ring[i])
404 igc_clean_rx_ring(adapter->rx_ring[i]);
408 * igc_free_rx_resources - Free Rx Resources
409 * @rx_ring: ring to clean the resources from
411 * Free all receive software resources
413 void igc_free_rx_resources(struct igc_ring *rx_ring)
415 igc_clean_rx_ring(rx_ring);
417 igc_xdp_unregister_rxq_info(rx_ring);
419 vfree(rx_ring->rx_buffer_info);
420 rx_ring->rx_buffer_info = NULL;
422 /* if not set, then don't free */
426 dma_free_coherent(rx_ring->dev, rx_ring->size,
427 rx_ring->desc, rx_ring->dma);
429 rx_ring->desc = NULL;
433 * igc_free_all_rx_resources - Free Rx Resources for All Queues
434 * @adapter: board private structure
436 * Free all receive software resources
438 static void igc_free_all_rx_resources(struct igc_adapter *adapter)
442 for (i = 0; i < adapter->num_rx_queues; i++)
443 igc_free_rx_resources(adapter->rx_ring[i]);
447 * igc_setup_rx_resources - allocate Rx resources (Descriptors)
448 * @rx_ring: rx descriptor ring (for a specific queue) to setup
450 * Returns 0 on success, negative on failure
452 int igc_setup_rx_resources(struct igc_ring *rx_ring)
454 struct net_device *ndev = rx_ring->netdev;
455 struct device *dev = rx_ring->dev;
456 int size, desc_len, res;
458 res = igc_xdp_register_rxq_info(rx_ring);
462 size = sizeof(struct igc_rx_buffer) * rx_ring->count;
463 rx_ring->rx_buffer_info = vzalloc(size);
464 if (!rx_ring->rx_buffer_info)
467 desc_len = sizeof(union igc_adv_rx_desc);
469 /* Round up to nearest 4K */
470 rx_ring->size = rx_ring->count * desc_len;
471 rx_ring->size = ALIGN(rx_ring->size, 4096);
473 rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
474 &rx_ring->dma, GFP_KERNEL);
479 rx_ring->next_to_alloc = 0;
480 rx_ring->next_to_clean = 0;
481 rx_ring->next_to_use = 0;
486 igc_xdp_unregister_rxq_info(rx_ring);
487 vfree(rx_ring->rx_buffer_info);
488 rx_ring->rx_buffer_info = NULL;
489 netdev_err(ndev, "Unable to allocate memory for Rx descriptor ring\n");
494 * igc_setup_all_rx_resources - wrapper to allocate Rx resources
495 * (Descriptors) for all queues
496 * @adapter: board private structure
498 * Return 0 on success, negative on failure
500 static int igc_setup_all_rx_resources(struct igc_adapter *adapter)
502 struct net_device *dev = adapter->netdev;
505 for (i = 0; i < adapter->num_rx_queues; i++) {
506 err = igc_setup_rx_resources(adapter->rx_ring[i]);
508 netdev_err(dev, "Error on Rx queue %u setup\n", i);
509 for (i--; i >= 0; i--)
510 igc_free_rx_resources(adapter->rx_ring[i]);
518 static bool igc_xdp_is_enabled(struct igc_adapter *adapter)
520 return !!adapter->xdp_prog;
524 * igc_configure_rx_ring - Configure a receive ring after Reset
525 * @adapter: board private structure
526 * @ring: receive ring to be configured
528 * Configure the Rx unit of the MAC after a reset.
530 static void igc_configure_rx_ring(struct igc_adapter *adapter,
531 struct igc_ring *ring)
533 struct igc_hw *hw = &adapter->hw;
534 union igc_adv_rx_desc *rx_desc;
535 int reg_idx = ring->reg_idx;
536 u32 srrctl = 0, rxdctl = 0;
537 u64 rdba = ring->dma;
539 if (igc_xdp_is_enabled(adapter))
540 set_ring_uses_large_buffer(ring);
542 /* disable the queue */
543 wr32(IGC_RXDCTL(reg_idx), 0);
545 /* Set DMA base address registers */
546 wr32(IGC_RDBAL(reg_idx),
547 rdba & 0x00000000ffffffffULL);
548 wr32(IGC_RDBAH(reg_idx), rdba >> 32);
549 wr32(IGC_RDLEN(reg_idx),
550 ring->count * sizeof(union igc_adv_rx_desc));
552 /* initialize head and tail */
553 ring->tail = adapter->io_addr + IGC_RDT(reg_idx);
554 wr32(IGC_RDH(reg_idx), 0);
555 writel(0, ring->tail);
557 /* reset next-to- use/clean to place SW in sync with hardware */
558 ring->next_to_clean = 0;
559 ring->next_to_use = 0;
561 /* set descriptor configuration */
562 srrctl = IGC_RX_HDR_LEN << IGC_SRRCTL_BSIZEHDRSIZE_SHIFT;
563 if (ring_uses_large_buffer(ring))
564 srrctl |= IGC_RXBUFFER_3072 >> IGC_SRRCTL_BSIZEPKT_SHIFT;
566 srrctl |= IGC_RXBUFFER_2048 >> IGC_SRRCTL_BSIZEPKT_SHIFT;
567 srrctl |= IGC_SRRCTL_DESCTYPE_ADV_ONEBUF;
569 wr32(IGC_SRRCTL(reg_idx), srrctl);
571 rxdctl |= IGC_RX_PTHRESH;
572 rxdctl |= IGC_RX_HTHRESH << 8;
573 rxdctl |= IGC_RX_WTHRESH << 16;
575 /* initialize rx_buffer_info */
576 memset(ring->rx_buffer_info, 0,
577 sizeof(struct igc_rx_buffer) * ring->count);
579 /* initialize Rx descriptor 0 */
580 rx_desc = IGC_RX_DESC(ring, 0);
581 rx_desc->wb.upper.length = 0;
583 /* enable receive descriptor fetching */
584 rxdctl |= IGC_RXDCTL_QUEUE_ENABLE;
586 wr32(IGC_RXDCTL(reg_idx), rxdctl);
590 * igc_configure_rx - Configure receive Unit after Reset
591 * @adapter: board private structure
593 * Configure the Rx unit of the MAC after a reset.
595 static void igc_configure_rx(struct igc_adapter *adapter)
599 /* Setup the HW Rx Head and Tail Descriptor Pointers and
600 * the Base and Length of the Rx Descriptor Ring
602 for (i = 0; i < adapter->num_rx_queues; i++)
603 igc_configure_rx_ring(adapter, adapter->rx_ring[i]);
607 * igc_configure_tx_ring - Configure transmit ring after Reset
608 * @adapter: board private structure
609 * @ring: tx ring to configure
611 * Configure a transmit ring after a reset.
613 static void igc_configure_tx_ring(struct igc_adapter *adapter,
614 struct igc_ring *ring)
616 struct igc_hw *hw = &adapter->hw;
617 int reg_idx = ring->reg_idx;
618 u64 tdba = ring->dma;
621 /* disable the queue */
622 wr32(IGC_TXDCTL(reg_idx), 0);
626 wr32(IGC_TDLEN(reg_idx),
627 ring->count * sizeof(union igc_adv_tx_desc));
628 wr32(IGC_TDBAL(reg_idx),
629 tdba & 0x00000000ffffffffULL);
630 wr32(IGC_TDBAH(reg_idx), tdba >> 32);
632 ring->tail = adapter->io_addr + IGC_TDT(reg_idx);
633 wr32(IGC_TDH(reg_idx), 0);
634 writel(0, ring->tail);
636 txdctl |= IGC_TX_PTHRESH;
637 txdctl |= IGC_TX_HTHRESH << 8;
638 txdctl |= IGC_TX_WTHRESH << 16;
640 txdctl |= IGC_TXDCTL_QUEUE_ENABLE;
641 wr32(IGC_TXDCTL(reg_idx), txdctl);
645 * igc_configure_tx - Configure transmit Unit after Reset
646 * @adapter: board private structure
648 * Configure the Tx unit of the MAC after a reset.
650 static void igc_configure_tx(struct igc_adapter *adapter)
654 for (i = 0; i < adapter->num_tx_queues; i++)
655 igc_configure_tx_ring(adapter, adapter->tx_ring[i]);
659 * igc_setup_mrqc - configure the multiple receive queue control registers
660 * @adapter: Board private structure
662 static void igc_setup_mrqc(struct igc_adapter *adapter)
664 struct igc_hw *hw = &adapter->hw;
665 u32 j, num_rx_queues;
669 netdev_rss_key_fill(rss_key, sizeof(rss_key));
670 for (j = 0; j < 10; j++)
671 wr32(IGC_RSSRK(j), rss_key[j]);
673 num_rx_queues = adapter->rss_queues;
675 if (adapter->rss_indir_tbl_init != num_rx_queues) {
676 for (j = 0; j < IGC_RETA_SIZE; j++)
677 adapter->rss_indir_tbl[j] =
678 (j * num_rx_queues) / IGC_RETA_SIZE;
679 adapter->rss_indir_tbl_init = num_rx_queues;
681 igc_write_rss_indir_tbl(adapter);
683 /* Disable raw packet checksumming so that RSS hash is placed in
684 * descriptor on writeback. No need to enable TCP/UDP/IP checksum
685 * offloads as they are enabled by default
687 rxcsum = rd32(IGC_RXCSUM);
688 rxcsum |= IGC_RXCSUM_PCSD;
690 /* Enable Receive Checksum Offload for SCTP */
691 rxcsum |= IGC_RXCSUM_CRCOFL;
693 /* Don't need to set TUOFL or IPOFL, they default to 1 */
694 wr32(IGC_RXCSUM, rxcsum);
696 /* Generate RSS hash based on packet types, TCP/UDP
697 * port numbers and/or IPv4/v6 src and dst addresses
699 mrqc = IGC_MRQC_RSS_FIELD_IPV4 |
700 IGC_MRQC_RSS_FIELD_IPV4_TCP |
701 IGC_MRQC_RSS_FIELD_IPV6 |
702 IGC_MRQC_RSS_FIELD_IPV6_TCP |
703 IGC_MRQC_RSS_FIELD_IPV6_TCP_EX;
705 if (adapter->flags & IGC_FLAG_RSS_FIELD_IPV4_UDP)
706 mrqc |= IGC_MRQC_RSS_FIELD_IPV4_UDP;
707 if (adapter->flags & IGC_FLAG_RSS_FIELD_IPV6_UDP)
708 mrqc |= IGC_MRQC_RSS_FIELD_IPV6_UDP;
710 mrqc |= IGC_MRQC_ENABLE_RSS_MQ;
712 wr32(IGC_MRQC, mrqc);
716 * igc_setup_rctl - configure the receive control registers
717 * @adapter: Board private structure
719 static void igc_setup_rctl(struct igc_adapter *adapter)
721 struct igc_hw *hw = &adapter->hw;
724 rctl = rd32(IGC_RCTL);
726 rctl &= ~(3 << IGC_RCTL_MO_SHIFT);
727 rctl &= ~(IGC_RCTL_LBM_TCVR | IGC_RCTL_LBM_MAC);
729 rctl |= IGC_RCTL_EN | IGC_RCTL_BAM | IGC_RCTL_RDMTS_HALF |
730 (hw->mac.mc_filter_type << IGC_RCTL_MO_SHIFT);
732 /* enable stripping of CRC. Newer features require
733 * that the HW strips the CRC.
735 rctl |= IGC_RCTL_SECRC;
737 /* disable store bad packets and clear size bits. */
738 rctl &= ~(IGC_RCTL_SBP | IGC_RCTL_SZ_256);
740 /* enable LPE to allow for reception of jumbo frames */
741 rctl |= IGC_RCTL_LPE;
743 /* disable queue 0 to prevent tail write w/o re-config */
744 wr32(IGC_RXDCTL(0), 0);
746 /* This is useful for sniffing bad packets. */
747 if (adapter->netdev->features & NETIF_F_RXALL) {
748 /* UPE and MPE will be handled by normal PROMISC logic
751 rctl |= (IGC_RCTL_SBP | /* Receive bad packets */
752 IGC_RCTL_BAM | /* RX All Bcast Pkts */
753 IGC_RCTL_PMCF); /* RX All MAC Ctrl Pkts */
755 rctl &= ~(IGC_RCTL_DPF | /* Allow filtered pause */
756 IGC_RCTL_CFIEN); /* Disable VLAN CFIEN Filter */
759 wr32(IGC_RCTL, rctl);
763 * igc_setup_tctl - configure the transmit control registers
764 * @adapter: Board private structure
766 static void igc_setup_tctl(struct igc_adapter *adapter)
768 struct igc_hw *hw = &adapter->hw;
771 /* disable queue 0 which icould be enabled by default */
772 wr32(IGC_TXDCTL(0), 0);
774 /* Program the Transmit Control Register */
775 tctl = rd32(IGC_TCTL);
776 tctl &= ~IGC_TCTL_CT;
777 tctl |= IGC_TCTL_PSP | IGC_TCTL_RTLC |
778 (IGC_COLLISION_THRESHOLD << IGC_CT_SHIFT);
780 /* Enable transmits */
783 wr32(IGC_TCTL, tctl);
787 * igc_set_mac_filter_hw() - Set MAC address filter in hardware
788 * @adapter: Pointer to adapter where the filter should be set
789 * @index: Filter index
790 * @type: MAC address filter type (source or destination)
792 * @queue: If non-negative, queue assignment feature is enabled and frames
793 * matching the filter are enqueued onto 'queue'. Otherwise, queue
794 * assignment is disabled.
796 static void igc_set_mac_filter_hw(struct igc_adapter *adapter, int index,
797 enum igc_mac_filter_type type,
798 const u8 *addr, int queue)
800 struct net_device *dev = adapter->netdev;
801 struct igc_hw *hw = &adapter->hw;
804 if (WARN_ON(index >= hw->mac.rar_entry_count))
807 ral = le32_to_cpup((__le32 *)(addr));
808 rah = le16_to_cpup((__le16 *)(addr + 4));
810 if (type == IGC_MAC_FILTER_TYPE_SRC) {
811 rah &= ~IGC_RAH_ASEL_MASK;
812 rah |= IGC_RAH_ASEL_SRC_ADDR;
816 rah &= ~IGC_RAH_QSEL_MASK;
817 rah |= (queue << IGC_RAH_QSEL_SHIFT);
818 rah |= IGC_RAH_QSEL_ENABLE;
823 wr32(IGC_RAL(index), ral);
824 wr32(IGC_RAH(index), rah);
826 netdev_dbg(dev, "MAC address filter set in HW: index %d", index);
830 * igc_clear_mac_filter_hw() - Clear MAC address filter in hardware
831 * @adapter: Pointer to adapter where the filter should be cleared
832 * @index: Filter index
834 static void igc_clear_mac_filter_hw(struct igc_adapter *adapter, int index)
836 struct net_device *dev = adapter->netdev;
837 struct igc_hw *hw = &adapter->hw;
839 if (WARN_ON(index >= hw->mac.rar_entry_count))
842 wr32(IGC_RAL(index), 0);
843 wr32(IGC_RAH(index), 0);
845 netdev_dbg(dev, "MAC address filter cleared in HW: index %d", index);
848 /* Set default MAC address for the PF in the first RAR entry */
849 static void igc_set_default_mac_filter(struct igc_adapter *adapter)
851 struct net_device *dev = adapter->netdev;
852 u8 *addr = adapter->hw.mac.addr;
854 netdev_dbg(dev, "Set default MAC address filter: address %pM", addr);
856 igc_set_mac_filter_hw(adapter, 0, IGC_MAC_FILTER_TYPE_DST, addr, -1);
860 * igc_set_mac - Change the Ethernet Address of the NIC
861 * @netdev: network interface device structure
862 * @p: pointer to an address structure
864 * Returns 0 on success, negative on failure
866 static int igc_set_mac(struct net_device *netdev, void *p)
868 struct igc_adapter *adapter = netdev_priv(netdev);
869 struct igc_hw *hw = &adapter->hw;
870 struct sockaddr *addr = p;
872 if (!is_valid_ether_addr(addr->sa_data))
873 return -EADDRNOTAVAIL;
875 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
876 memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
878 /* set the correct pool for the new PF MAC address in entry 0 */
879 igc_set_default_mac_filter(adapter);
885 * igc_write_mc_addr_list - write multicast addresses to MTA
886 * @netdev: network interface device structure
888 * Writes multicast address list to the MTA hash table.
889 * Returns: -ENOMEM on failure
890 * 0 on no addresses written
891 * X on writing X addresses to MTA
893 static int igc_write_mc_addr_list(struct net_device *netdev)
895 struct igc_adapter *adapter = netdev_priv(netdev);
896 struct igc_hw *hw = &adapter->hw;
897 struct netdev_hw_addr *ha;
901 if (netdev_mc_empty(netdev)) {
902 /* nothing to program, so clear mc list */
903 igc_update_mc_addr_list(hw, NULL, 0);
907 mta_list = kcalloc(netdev_mc_count(netdev), 6, GFP_ATOMIC);
911 /* The shared function expects a packed array of only addresses. */
913 netdev_for_each_mc_addr(ha, netdev)
914 memcpy(mta_list + (i++ * ETH_ALEN), ha->addr, ETH_ALEN);
916 igc_update_mc_addr_list(hw, mta_list, i);
919 return netdev_mc_count(netdev);
922 static __le32 igc_tx_launchtime(struct igc_adapter *adapter, ktime_t txtime)
924 ktime_t cycle_time = adapter->cycle_time;
925 ktime_t base_time = adapter->base_time;
928 /* FIXME: when using ETF together with taprio, we may have a
929 * case where 'delta' is larger than the cycle_time, this may
930 * cause problems if we don't read the current value of
931 * IGC_BASET, as the value writen into the launchtime
932 * descriptor field may be misinterpreted.
934 div_s64_rem(ktime_sub_ns(txtime, base_time), cycle_time, &launchtime);
936 return cpu_to_le32(launchtime);
939 static void igc_tx_ctxtdesc(struct igc_ring *tx_ring,
940 struct igc_tx_buffer *first,
941 u32 vlan_macip_lens, u32 type_tucmd,
944 struct igc_adv_tx_context_desc *context_desc;
945 u16 i = tx_ring->next_to_use;
947 context_desc = IGC_TX_CTXTDESC(tx_ring, i);
950 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
952 /* set bits to identify this as an advanced context descriptor */
953 type_tucmd |= IGC_TXD_CMD_DEXT | IGC_ADVTXD_DTYP_CTXT;
955 /* For i225, context index must be unique per ring. */
956 if (test_bit(IGC_RING_FLAG_TX_CTX_IDX, &tx_ring->flags))
957 mss_l4len_idx |= tx_ring->reg_idx << 4;
959 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
960 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd);
961 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
963 /* We assume there is always a valid Tx time available. Invalid times
964 * should have been handled by the upper layers.
966 if (tx_ring->launchtime_enable) {
967 struct igc_adapter *adapter = netdev_priv(tx_ring->netdev);
968 ktime_t txtime = first->skb->tstamp;
970 skb_txtime_consumed(first->skb);
971 context_desc->launch_time = igc_tx_launchtime(adapter,
974 context_desc->launch_time = 0;
978 static void igc_tx_csum(struct igc_ring *tx_ring, struct igc_tx_buffer *first)
980 struct sk_buff *skb = first->skb;
981 u32 vlan_macip_lens = 0;
984 if (skb->ip_summed != CHECKSUM_PARTIAL) {
986 if (!(first->tx_flags & IGC_TX_FLAGS_VLAN) &&
987 !tx_ring->launchtime_enable)
992 switch (skb->csum_offset) {
993 case offsetof(struct tcphdr, check):
994 type_tucmd = IGC_ADVTXD_TUCMD_L4T_TCP;
996 case offsetof(struct udphdr, check):
998 case offsetof(struct sctphdr, checksum):
999 /* validate that this is actually an SCTP request */
1000 if (skb_csum_is_sctp(skb)) {
1001 type_tucmd = IGC_ADVTXD_TUCMD_L4T_SCTP;
1006 skb_checksum_help(skb);
1010 /* update TX checksum flag */
1011 first->tx_flags |= IGC_TX_FLAGS_CSUM;
1012 vlan_macip_lens = skb_checksum_start_offset(skb) -
1013 skb_network_offset(skb);
1015 vlan_macip_lens |= skb_network_offset(skb) << IGC_ADVTXD_MACLEN_SHIFT;
1016 vlan_macip_lens |= first->tx_flags & IGC_TX_FLAGS_VLAN_MASK;
1018 igc_tx_ctxtdesc(tx_ring, first, vlan_macip_lens, type_tucmd, 0);
1021 static int __igc_maybe_stop_tx(struct igc_ring *tx_ring, const u16 size)
1023 struct net_device *netdev = tx_ring->netdev;
1025 netif_stop_subqueue(netdev, tx_ring->queue_index);
1027 /* memory barriier comment */
1030 /* We need to check again in a case another CPU has just
1031 * made room available.
1033 if (igc_desc_unused(tx_ring) < size)
1037 netif_wake_subqueue(netdev, tx_ring->queue_index);
1039 u64_stats_update_begin(&tx_ring->tx_syncp2);
1040 tx_ring->tx_stats.restart_queue2++;
1041 u64_stats_update_end(&tx_ring->tx_syncp2);
1046 static inline int igc_maybe_stop_tx(struct igc_ring *tx_ring, const u16 size)
1048 if (igc_desc_unused(tx_ring) >= size)
1050 return __igc_maybe_stop_tx(tx_ring, size);
1053 #define IGC_SET_FLAG(_input, _flag, _result) \
1054 (((_flag) <= (_result)) ? \
1055 ((u32)((_input) & (_flag)) * ((_result) / (_flag))) : \
1056 ((u32)((_input) & (_flag)) / ((_flag) / (_result))))
1058 static u32 igc_tx_cmd_type(u32 tx_flags)
1060 /* set type for advanced descriptor with frame checksum insertion */
1061 u32 cmd_type = IGC_ADVTXD_DTYP_DATA |
1062 IGC_ADVTXD_DCMD_DEXT |
1063 IGC_ADVTXD_DCMD_IFCS;
1065 /* set segmentation bits for TSO */
1066 cmd_type |= IGC_SET_FLAG(tx_flags, IGC_TX_FLAGS_TSO,
1067 (IGC_ADVTXD_DCMD_TSE));
1069 /* set timestamp bit if present */
1070 cmd_type |= IGC_SET_FLAG(tx_flags, IGC_TX_FLAGS_TSTAMP,
1071 (IGC_ADVTXD_MAC_TSTAMP));
1076 static void igc_tx_olinfo_status(struct igc_ring *tx_ring,
1077 union igc_adv_tx_desc *tx_desc,
1078 u32 tx_flags, unsigned int paylen)
1080 u32 olinfo_status = paylen << IGC_ADVTXD_PAYLEN_SHIFT;
1082 /* insert L4 checksum */
1083 olinfo_status |= (tx_flags & IGC_TX_FLAGS_CSUM) *
1084 ((IGC_TXD_POPTS_TXSM << 8) /
1087 /* insert IPv4 checksum */
1088 olinfo_status |= (tx_flags & IGC_TX_FLAGS_IPV4) *
1089 (((IGC_TXD_POPTS_IXSM << 8)) /
1092 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
1095 static int igc_tx_map(struct igc_ring *tx_ring,
1096 struct igc_tx_buffer *first,
1099 struct sk_buff *skb = first->skb;
1100 struct igc_tx_buffer *tx_buffer;
1101 union igc_adv_tx_desc *tx_desc;
1102 u32 tx_flags = first->tx_flags;
1104 u16 i = tx_ring->next_to_use;
1105 unsigned int data_len, size;
1107 u32 cmd_type = igc_tx_cmd_type(tx_flags);
1109 tx_desc = IGC_TX_DESC(tx_ring, i);
1111 igc_tx_olinfo_status(tx_ring, tx_desc, tx_flags, skb->len - hdr_len);
1113 size = skb_headlen(skb);
1114 data_len = skb->data_len;
1116 dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
1120 for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
1121 if (dma_mapping_error(tx_ring->dev, dma))
1124 /* record length, and DMA address */
1125 dma_unmap_len_set(tx_buffer, len, size);
1126 dma_unmap_addr_set(tx_buffer, dma, dma);
1128 tx_desc->read.buffer_addr = cpu_to_le64(dma);
1130 while (unlikely(size > IGC_MAX_DATA_PER_TXD)) {
1131 tx_desc->read.cmd_type_len =
1132 cpu_to_le32(cmd_type ^ IGC_MAX_DATA_PER_TXD);
1136 if (i == tx_ring->count) {
1137 tx_desc = IGC_TX_DESC(tx_ring, 0);
1140 tx_desc->read.olinfo_status = 0;
1142 dma += IGC_MAX_DATA_PER_TXD;
1143 size -= IGC_MAX_DATA_PER_TXD;
1145 tx_desc->read.buffer_addr = cpu_to_le64(dma);
1148 if (likely(!data_len))
1151 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type ^ size);
1155 if (i == tx_ring->count) {
1156 tx_desc = IGC_TX_DESC(tx_ring, 0);
1159 tx_desc->read.olinfo_status = 0;
1161 size = skb_frag_size(frag);
1164 dma = skb_frag_dma_map(tx_ring->dev, frag, 0,
1165 size, DMA_TO_DEVICE);
1167 tx_buffer = &tx_ring->tx_buffer_info[i];
1170 /* write last descriptor with RS and EOP bits */
1171 cmd_type |= size | IGC_TXD_DCMD;
1172 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type);
1174 netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount);
1176 /* set the timestamp */
1177 first->time_stamp = jiffies;
1179 skb_tx_timestamp(skb);
1181 /* Force memory writes to complete before letting h/w know there
1182 * are new descriptors to fetch. (Only applicable for weak-ordered
1183 * memory model archs, such as IA-64).
1185 * We also need this memory barrier to make certain all of the
1186 * status bits have been updated before next_to_watch is written.
1190 /* set next_to_watch value indicating a packet is present */
1191 first->next_to_watch = tx_desc;
1194 if (i == tx_ring->count)
1197 tx_ring->next_to_use = i;
1199 /* Make sure there is space in the ring for the next send. */
1200 igc_maybe_stop_tx(tx_ring, DESC_NEEDED);
1202 if (netif_xmit_stopped(txring_txq(tx_ring)) || !netdev_xmit_more()) {
1203 writel(i, tx_ring->tail);
1208 netdev_err(tx_ring->netdev, "TX DMA map failed\n");
1209 tx_buffer = &tx_ring->tx_buffer_info[i];
1211 /* clear dma mappings for failed tx_buffer_info map */
1212 while (tx_buffer != first) {
1213 if (dma_unmap_len(tx_buffer, len))
1214 dma_unmap_page(tx_ring->dev,
1215 dma_unmap_addr(tx_buffer, dma),
1216 dma_unmap_len(tx_buffer, len),
1218 dma_unmap_len_set(tx_buffer, len, 0);
1221 i += tx_ring->count;
1222 tx_buffer = &tx_ring->tx_buffer_info[i];
1225 if (dma_unmap_len(tx_buffer, len))
1226 dma_unmap_single(tx_ring->dev,
1227 dma_unmap_addr(tx_buffer, dma),
1228 dma_unmap_len(tx_buffer, len),
1230 dma_unmap_len_set(tx_buffer, len, 0);
1232 dev_kfree_skb_any(tx_buffer->skb);
1233 tx_buffer->skb = NULL;
1235 tx_ring->next_to_use = i;
1240 static int igc_tso(struct igc_ring *tx_ring,
1241 struct igc_tx_buffer *first,
1244 u32 vlan_macip_lens, type_tucmd, mss_l4len_idx;
1245 struct sk_buff *skb = first->skb;
1256 u32 paylen, l4_offset;
1259 if (skb->ip_summed != CHECKSUM_PARTIAL)
1262 if (!skb_is_gso(skb))
1265 err = skb_cow_head(skb, 0);
1269 ip.hdr = skb_network_header(skb);
1270 l4.hdr = skb_checksum_start(skb);
1272 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
1273 type_tucmd = IGC_ADVTXD_TUCMD_L4T_TCP;
1275 /* initialize outer IP header fields */
1276 if (ip.v4->version == 4) {
1277 unsigned char *csum_start = skb_checksum_start(skb);
1278 unsigned char *trans_start = ip.hdr + (ip.v4->ihl * 4);
1280 /* IP header will have to cancel out any data that
1281 * is not a part of the outer IP header
1283 ip.v4->check = csum_fold(csum_partial(trans_start,
1284 csum_start - trans_start,
1286 type_tucmd |= IGC_ADVTXD_TUCMD_IPV4;
1289 first->tx_flags |= IGC_TX_FLAGS_TSO |
1293 ip.v6->payload_len = 0;
1294 first->tx_flags |= IGC_TX_FLAGS_TSO |
1298 /* determine offset of inner transport header */
1299 l4_offset = l4.hdr - skb->data;
1301 /* remove payload length from inner checksum */
1302 paylen = skb->len - l4_offset;
1303 if (type_tucmd & IGC_ADVTXD_TUCMD_L4T_TCP) {
1304 /* compute length of segmentation header */
1305 *hdr_len = (l4.tcp->doff * 4) + l4_offset;
1306 csum_replace_by_diff(&l4.tcp->check,
1307 (__force __wsum)htonl(paylen));
1309 /* compute length of segmentation header */
1310 *hdr_len = sizeof(*l4.udp) + l4_offset;
1311 csum_replace_by_diff(&l4.udp->check,
1312 (__force __wsum)htonl(paylen));
1315 /* update gso size and bytecount with header size */
1316 first->gso_segs = skb_shinfo(skb)->gso_segs;
1317 first->bytecount += (first->gso_segs - 1) * *hdr_len;
1320 mss_l4len_idx = (*hdr_len - l4_offset) << IGC_ADVTXD_L4LEN_SHIFT;
1321 mss_l4len_idx |= skb_shinfo(skb)->gso_size << IGC_ADVTXD_MSS_SHIFT;
1323 /* VLAN MACLEN IPLEN */
1324 vlan_macip_lens = l4.hdr - ip.hdr;
1325 vlan_macip_lens |= (ip.hdr - skb->data) << IGC_ADVTXD_MACLEN_SHIFT;
1326 vlan_macip_lens |= first->tx_flags & IGC_TX_FLAGS_VLAN_MASK;
1328 igc_tx_ctxtdesc(tx_ring, first, vlan_macip_lens,
1329 type_tucmd, mss_l4len_idx);
1334 static netdev_tx_t igc_xmit_frame_ring(struct sk_buff *skb,
1335 struct igc_ring *tx_ring)
1337 u16 count = TXD_USE_COUNT(skb_headlen(skb));
1338 __be16 protocol = vlan_get_protocol(skb);
1339 struct igc_tx_buffer *first;
1345 /* need: 1 descriptor per page * PAGE_SIZE/IGC_MAX_DATA_PER_TXD,
1346 * + 1 desc for skb_headlen/IGC_MAX_DATA_PER_TXD,
1347 * + 2 desc gap to keep tail from touching head,
1348 * + 1 desc for context descriptor,
1349 * otherwise try next time
1351 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
1352 count += TXD_USE_COUNT(skb_frag_size(
1353 &skb_shinfo(skb)->frags[f]));
1355 if (igc_maybe_stop_tx(tx_ring, count + 3)) {
1356 /* this is a hard error */
1357 return NETDEV_TX_BUSY;
1360 /* record the location of the first descriptor for this packet */
1361 first = &tx_ring->tx_buffer_info[tx_ring->next_to_use];
1363 first->bytecount = skb->len;
1364 first->gso_segs = 1;
1366 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
1367 struct igc_adapter *adapter = netdev_priv(tx_ring->netdev);
1369 /* FIXME: add support for retrieving timestamps from
1370 * the other timer registers before skipping the
1371 * timestamping request.
1373 if (adapter->tstamp_config.tx_type == HWTSTAMP_TX_ON &&
1374 !test_and_set_bit_lock(__IGC_PTP_TX_IN_PROGRESS,
1376 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1377 tx_flags |= IGC_TX_FLAGS_TSTAMP;
1379 adapter->ptp_tx_skb = skb_get(skb);
1380 adapter->ptp_tx_start = jiffies;
1382 adapter->tx_hwtstamp_skipped++;
1386 /* record initial flags and protocol */
1387 first->tx_flags = tx_flags;
1388 first->protocol = protocol;
1390 tso = igc_tso(tx_ring, first, &hdr_len);
1394 igc_tx_csum(tx_ring, first);
1396 igc_tx_map(tx_ring, first, hdr_len);
1398 return NETDEV_TX_OK;
1401 dev_kfree_skb_any(first->skb);
1404 return NETDEV_TX_OK;
1407 static inline struct igc_ring *igc_tx_queue_mapping(struct igc_adapter *adapter,
1408 struct sk_buff *skb)
1410 unsigned int r_idx = skb->queue_mapping;
1412 if (r_idx >= adapter->num_tx_queues)
1413 r_idx = r_idx % adapter->num_tx_queues;
1415 return adapter->tx_ring[r_idx];
1418 static netdev_tx_t igc_xmit_frame(struct sk_buff *skb,
1419 struct net_device *netdev)
1421 struct igc_adapter *adapter = netdev_priv(netdev);
1423 /* The minimum packet size with TCTL.PSP set is 17 so pad the skb
1424 * in order to meet this minimum size requirement.
1426 if (skb->len < 17) {
1427 if (skb_padto(skb, 17))
1428 return NETDEV_TX_OK;
1432 return igc_xmit_frame_ring(skb, igc_tx_queue_mapping(adapter, skb));
1435 static void igc_rx_checksum(struct igc_ring *ring,
1436 union igc_adv_rx_desc *rx_desc,
1437 struct sk_buff *skb)
1439 skb_checksum_none_assert(skb);
1441 /* Ignore Checksum bit is set */
1442 if (igc_test_staterr(rx_desc, IGC_RXD_STAT_IXSM))
1445 /* Rx checksum disabled via ethtool */
1446 if (!(ring->netdev->features & NETIF_F_RXCSUM))
1449 /* TCP/UDP checksum error bit is set */
1450 if (igc_test_staterr(rx_desc,
1451 IGC_RXDEXT_STATERR_L4E |
1452 IGC_RXDEXT_STATERR_IPE)) {
1453 /* work around errata with sctp packets where the TCPE aka
1454 * L4E bit is set incorrectly on 64 byte (60 byte w/o crc)
1455 * packets (aka let the stack check the crc32c)
1457 if (!(skb->len == 60 &&
1458 test_bit(IGC_RING_FLAG_RX_SCTP_CSUM, &ring->flags))) {
1459 u64_stats_update_begin(&ring->rx_syncp);
1460 ring->rx_stats.csum_err++;
1461 u64_stats_update_end(&ring->rx_syncp);
1463 /* let the stack verify checksum errors */
1466 /* It must be a TCP or UDP packet with a valid checksum */
1467 if (igc_test_staterr(rx_desc, IGC_RXD_STAT_TCPCS |
1468 IGC_RXD_STAT_UDPCS))
1469 skb->ip_summed = CHECKSUM_UNNECESSARY;
1471 netdev_dbg(ring->netdev, "cksum success: bits %08X\n",
1472 le32_to_cpu(rx_desc->wb.upper.status_error));
1475 static inline void igc_rx_hash(struct igc_ring *ring,
1476 union igc_adv_rx_desc *rx_desc,
1477 struct sk_buff *skb)
1479 if (ring->netdev->features & NETIF_F_RXHASH)
1481 le32_to_cpu(rx_desc->wb.lower.hi_dword.rss),
1486 * igc_process_skb_fields - Populate skb header fields from Rx descriptor
1487 * @rx_ring: rx descriptor ring packet is being transacted on
1488 * @rx_desc: pointer to the EOP Rx descriptor
1489 * @skb: pointer to current skb being populated
1491 * This function checks the ring, descriptor, and packet information in order
1492 * to populate the hash, checksum, VLAN, protocol, and other fields within the
1495 static void igc_process_skb_fields(struct igc_ring *rx_ring,
1496 union igc_adv_rx_desc *rx_desc,
1497 struct sk_buff *skb)
1499 igc_rx_hash(rx_ring, rx_desc, skb);
1501 igc_rx_checksum(rx_ring, rx_desc, skb);
1503 skb_record_rx_queue(skb, rx_ring->queue_index);
1505 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
1508 static struct igc_rx_buffer *igc_get_rx_buffer(struct igc_ring *rx_ring,
1509 const unsigned int size,
1510 int *rx_buffer_pgcnt)
1512 struct igc_rx_buffer *rx_buffer;
1514 rx_buffer = &rx_ring->rx_buffer_info[rx_ring->next_to_clean];
1516 #if (PAGE_SIZE < 8192)
1517 page_count(rx_buffer->page);
1521 prefetchw(rx_buffer->page);
1523 /* we are reusing so sync this buffer for CPU use */
1524 dma_sync_single_range_for_cpu(rx_ring->dev,
1526 rx_buffer->page_offset,
1530 rx_buffer->pagecnt_bias--;
1535 static void igc_rx_buffer_flip(struct igc_rx_buffer *buffer,
1536 unsigned int truesize)
1538 #if (PAGE_SIZE < 8192)
1539 buffer->page_offset ^= truesize;
1541 buffer->page_offset += truesize;
1545 static unsigned int igc_get_rx_frame_truesize(struct igc_ring *ring,
1548 unsigned int truesize;
1550 #if (PAGE_SIZE < 8192)
1551 truesize = igc_rx_pg_size(ring) / 2;
1553 truesize = ring_uses_build_skb(ring) ?
1554 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
1555 SKB_DATA_ALIGN(IGC_SKB_PAD + size) :
1556 SKB_DATA_ALIGN(size);
1562 * igc_add_rx_frag - Add contents of Rx buffer to sk_buff
1563 * @rx_ring: rx descriptor ring to transact packets on
1564 * @rx_buffer: buffer containing page to add
1565 * @skb: sk_buff to place the data into
1566 * @size: size of buffer to be added
1568 * This function will add the data contained in rx_buffer->page to the skb.
1570 static void igc_add_rx_frag(struct igc_ring *rx_ring,
1571 struct igc_rx_buffer *rx_buffer,
1572 struct sk_buff *skb,
1575 unsigned int truesize;
1577 #if (PAGE_SIZE < 8192)
1578 truesize = igc_rx_pg_size(rx_ring) / 2;
1580 truesize = ring_uses_build_skb(rx_ring) ?
1581 SKB_DATA_ALIGN(IGC_SKB_PAD + size) :
1582 SKB_DATA_ALIGN(size);
1584 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_buffer->page,
1585 rx_buffer->page_offset, size, truesize);
1587 igc_rx_buffer_flip(rx_buffer, truesize);
1590 static struct sk_buff *igc_build_skb(struct igc_ring *rx_ring,
1591 struct igc_rx_buffer *rx_buffer,
1592 union igc_adv_rx_desc *rx_desc,
1595 void *va = page_address(rx_buffer->page) + rx_buffer->page_offset;
1596 unsigned int truesize = igc_get_rx_frame_truesize(rx_ring, size);
1597 struct sk_buff *skb;
1599 /* prefetch first cache line of first page */
1602 /* build an skb around the page buffer */
1603 skb = build_skb(va - IGC_SKB_PAD, truesize);
1607 /* update pointers within the skb to store the data */
1608 skb_reserve(skb, IGC_SKB_PAD);
1609 __skb_put(skb, size);
1611 igc_rx_buffer_flip(rx_buffer, truesize);
1615 static struct sk_buff *igc_construct_skb(struct igc_ring *rx_ring,
1616 struct igc_rx_buffer *rx_buffer,
1617 struct xdp_buff *xdp,
1620 unsigned int size = xdp->data_end - xdp->data;
1621 unsigned int truesize = igc_get_rx_frame_truesize(rx_ring, size);
1622 void *va = xdp->data;
1623 unsigned int headlen;
1624 struct sk_buff *skb;
1626 /* prefetch first cache line of first page */
1629 /* allocate a skb to store the frags */
1630 skb = napi_alloc_skb(&rx_ring->q_vector->napi, IGC_RX_HDR_LEN);
1635 skb_hwtstamps(skb)->hwtstamp = timestamp;
1637 /* Determine available headroom for copy */
1639 if (headlen > IGC_RX_HDR_LEN)
1640 headlen = eth_get_headlen(skb->dev, va, IGC_RX_HDR_LEN);
1642 /* align pull length to size of long to optimize memcpy performance */
1643 memcpy(__skb_put(skb, headlen), va, ALIGN(headlen, sizeof(long)));
1645 /* update all of the pointers */
1648 skb_add_rx_frag(skb, 0, rx_buffer->page,
1649 (va + headlen) - page_address(rx_buffer->page),
1651 igc_rx_buffer_flip(rx_buffer, truesize);
1653 rx_buffer->pagecnt_bias++;
1660 * igc_reuse_rx_page - page flip buffer and store it back on the ring
1661 * @rx_ring: rx descriptor ring to store buffers on
1662 * @old_buff: donor buffer to have page reused
1664 * Synchronizes page for reuse by the adapter
1666 static void igc_reuse_rx_page(struct igc_ring *rx_ring,
1667 struct igc_rx_buffer *old_buff)
1669 u16 nta = rx_ring->next_to_alloc;
1670 struct igc_rx_buffer *new_buff;
1672 new_buff = &rx_ring->rx_buffer_info[nta];
1674 /* update, and store next to alloc */
1676 rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
1678 /* Transfer page from old buffer to new buffer.
1679 * Move each member individually to avoid possible store
1680 * forwarding stalls.
1682 new_buff->dma = old_buff->dma;
1683 new_buff->page = old_buff->page;
1684 new_buff->page_offset = old_buff->page_offset;
1685 new_buff->pagecnt_bias = old_buff->pagecnt_bias;
1688 static bool igc_can_reuse_rx_page(struct igc_rx_buffer *rx_buffer,
1689 int rx_buffer_pgcnt)
1691 unsigned int pagecnt_bias = rx_buffer->pagecnt_bias;
1692 struct page *page = rx_buffer->page;
1694 /* avoid re-using remote and pfmemalloc pages */
1695 if (!dev_page_is_reusable(page))
1698 #if (PAGE_SIZE < 8192)
1699 /* if we are only owner of page we can reuse it */
1700 if (unlikely((rx_buffer_pgcnt - pagecnt_bias) > 1))
1703 #define IGC_LAST_OFFSET \
1704 (SKB_WITH_OVERHEAD(PAGE_SIZE) - IGC_RXBUFFER_2048)
1706 if (rx_buffer->page_offset > IGC_LAST_OFFSET)
1710 /* If we have drained the page fragment pool we need to update
1711 * the pagecnt_bias and page count so that we fully restock the
1712 * number of references the driver holds.
1714 if (unlikely(pagecnt_bias == 1)) {
1715 page_ref_add(page, USHRT_MAX - 1);
1716 rx_buffer->pagecnt_bias = USHRT_MAX;
1723 * igc_is_non_eop - process handling of non-EOP buffers
1724 * @rx_ring: Rx ring being processed
1725 * @rx_desc: Rx descriptor for current buffer
1727 * This function updates next to clean. If the buffer is an EOP buffer
1728 * this function exits returning false, otherwise it will place the
1729 * sk_buff in the next buffer to be chained and return true indicating
1730 * that this is in fact a non-EOP buffer.
1732 static bool igc_is_non_eop(struct igc_ring *rx_ring,
1733 union igc_adv_rx_desc *rx_desc)
1735 u32 ntc = rx_ring->next_to_clean + 1;
1737 /* fetch, update, and store next to clean */
1738 ntc = (ntc < rx_ring->count) ? ntc : 0;
1739 rx_ring->next_to_clean = ntc;
1741 prefetch(IGC_RX_DESC(rx_ring, ntc));
1743 if (likely(igc_test_staterr(rx_desc, IGC_RXD_STAT_EOP)))
1750 * igc_cleanup_headers - Correct corrupted or empty headers
1751 * @rx_ring: rx descriptor ring packet is being transacted on
1752 * @rx_desc: pointer to the EOP Rx descriptor
1753 * @skb: pointer to current skb being fixed
1755 * Address the case where we are pulling data in on pages only
1756 * and as such no data is present in the skb header.
1758 * In addition if skb is not at least 60 bytes we need to pad it so that
1759 * it is large enough to qualify as a valid Ethernet frame.
1761 * Returns true if an error was encountered and skb was freed.
1763 static bool igc_cleanup_headers(struct igc_ring *rx_ring,
1764 union igc_adv_rx_desc *rx_desc,
1765 struct sk_buff *skb)
1767 /* XDP packets use error pointer so abort at this point */
1771 if (unlikely(igc_test_staterr(rx_desc, IGC_RXDEXT_STATERR_RXE))) {
1772 struct net_device *netdev = rx_ring->netdev;
1774 if (!(netdev->features & NETIF_F_RXALL)) {
1775 dev_kfree_skb_any(skb);
1780 /* if eth_skb_pad returns an error the skb was freed */
1781 if (eth_skb_pad(skb))
1787 static void igc_put_rx_buffer(struct igc_ring *rx_ring,
1788 struct igc_rx_buffer *rx_buffer,
1789 int rx_buffer_pgcnt)
1791 if (igc_can_reuse_rx_page(rx_buffer, rx_buffer_pgcnt)) {
1792 /* hand second half of page back to the ring */
1793 igc_reuse_rx_page(rx_ring, rx_buffer);
1795 /* We are not reusing the buffer so unmap it and free
1796 * any references we are holding to it
1798 dma_unmap_page_attrs(rx_ring->dev, rx_buffer->dma,
1799 igc_rx_pg_size(rx_ring), DMA_FROM_DEVICE,
1801 __page_frag_cache_drain(rx_buffer->page,
1802 rx_buffer->pagecnt_bias);
1805 /* clear contents of rx_buffer */
1806 rx_buffer->page = NULL;
1809 static inline unsigned int igc_rx_offset(struct igc_ring *rx_ring)
1811 struct igc_adapter *adapter = rx_ring->q_vector->adapter;
1813 if (ring_uses_build_skb(rx_ring))
1815 if (igc_xdp_is_enabled(adapter))
1816 return XDP_PACKET_HEADROOM;
1821 static bool igc_alloc_mapped_page(struct igc_ring *rx_ring,
1822 struct igc_rx_buffer *bi)
1824 struct page *page = bi->page;
1827 /* since we are recycling buffers we should seldom need to alloc */
1831 /* alloc new page for storage */
1832 page = dev_alloc_pages(igc_rx_pg_order(rx_ring));
1833 if (unlikely(!page)) {
1834 rx_ring->rx_stats.alloc_failed++;
1838 /* map page for use */
1839 dma = dma_map_page_attrs(rx_ring->dev, page, 0,
1840 igc_rx_pg_size(rx_ring),
1844 /* if mapping failed free memory back to system since
1845 * there isn't much point in holding memory we can't use
1847 if (dma_mapping_error(rx_ring->dev, dma)) {
1850 rx_ring->rx_stats.alloc_failed++;
1856 bi->page_offset = igc_rx_offset(rx_ring);
1857 page_ref_add(page, USHRT_MAX - 1);
1858 bi->pagecnt_bias = USHRT_MAX;
1864 * igc_alloc_rx_buffers - Replace used receive buffers; packet split
1865 * @rx_ring: rx descriptor ring
1866 * @cleaned_count: number of buffers to clean
1868 static void igc_alloc_rx_buffers(struct igc_ring *rx_ring, u16 cleaned_count)
1870 union igc_adv_rx_desc *rx_desc;
1871 u16 i = rx_ring->next_to_use;
1872 struct igc_rx_buffer *bi;
1879 rx_desc = IGC_RX_DESC(rx_ring, i);
1880 bi = &rx_ring->rx_buffer_info[i];
1881 i -= rx_ring->count;
1883 bufsz = igc_rx_bufsz(rx_ring);
1886 if (!igc_alloc_mapped_page(rx_ring, bi))
1889 /* sync the buffer for use by the device */
1890 dma_sync_single_range_for_device(rx_ring->dev, bi->dma,
1891 bi->page_offset, bufsz,
1894 /* Refresh the desc even if buffer_addrs didn't change
1895 * because each write-back erases this info.
1897 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
1903 rx_desc = IGC_RX_DESC(rx_ring, 0);
1904 bi = rx_ring->rx_buffer_info;
1905 i -= rx_ring->count;
1908 /* clear the length for the next_to_use descriptor */
1909 rx_desc->wb.upper.length = 0;
1912 } while (cleaned_count);
1914 i += rx_ring->count;
1916 if (rx_ring->next_to_use != i) {
1917 /* record the next descriptor to use */
1918 rx_ring->next_to_use = i;
1920 /* update next to alloc since we have filled the ring */
1921 rx_ring->next_to_alloc = i;
1923 /* Force memory writes to complete before letting h/w
1924 * know there are new descriptors to fetch. (Only
1925 * applicable for weak-ordered memory model archs,
1929 writel(i, rx_ring->tail);
1933 static int igc_xdp_init_tx_buffer(struct igc_tx_buffer *buffer,
1934 struct xdp_frame *xdpf,
1935 struct igc_ring *ring)
1939 dma = dma_map_single(ring->dev, xdpf->data, xdpf->len, DMA_TO_DEVICE);
1940 if (dma_mapping_error(ring->dev, dma)) {
1941 netdev_err_once(ring->netdev, "Failed to map DMA for TX\n");
1945 buffer->xdpf = xdpf;
1946 buffer->tx_flags = IGC_TX_FLAGS_XDP;
1947 buffer->protocol = 0;
1948 buffer->bytecount = xdpf->len;
1949 buffer->gso_segs = 1;
1950 buffer->time_stamp = jiffies;
1951 dma_unmap_len_set(buffer, len, xdpf->len);
1952 dma_unmap_addr_set(buffer, dma, dma);
1956 /* This function requires __netif_tx_lock is held by the caller. */
1957 static int igc_xdp_init_tx_descriptor(struct igc_ring *ring,
1958 struct xdp_frame *xdpf)
1960 struct igc_tx_buffer *buffer;
1961 union igc_adv_tx_desc *desc;
1962 u32 cmd_type, olinfo_status;
1965 if (!igc_desc_unused(ring))
1968 buffer = &ring->tx_buffer_info[ring->next_to_use];
1969 err = igc_xdp_init_tx_buffer(buffer, xdpf, ring);
1973 cmd_type = IGC_ADVTXD_DTYP_DATA | IGC_ADVTXD_DCMD_DEXT |
1974 IGC_ADVTXD_DCMD_IFCS | IGC_TXD_DCMD |
1976 olinfo_status = buffer->bytecount << IGC_ADVTXD_PAYLEN_SHIFT;
1978 desc = IGC_TX_DESC(ring, ring->next_to_use);
1979 desc->read.cmd_type_len = cpu_to_le32(cmd_type);
1980 desc->read.olinfo_status = cpu_to_le32(olinfo_status);
1981 desc->read.buffer_addr = cpu_to_le64(dma_unmap_addr(buffer, dma));
1983 netdev_tx_sent_queue(txring_txq(ring), buffer->bytecount);
1985 buffer->next_to_watch = desc;
1987 ring->next_to_use++;
1988 if (ring->next_to_use == ring->count)
1989 ring->next_to_use = 0;
1994 static struct igc_ring *igc_xdp_get_tx_ring(struct igc_adapter *adapter,
1999 if (unlikely(index < 0))
2002 while (index >= adapter->num_tx_queues)
2003 index -= adapter->num_tx_queues;
2005 return adapter->tx_ring[index];
2008 static int igc_xdp_xmit_back(struct igc_adapter *adapter, struct xdp_buff *xdp)
2010 struct xdp_frame *xdpf = xdp_convert_buff_to_frame(xdp);
2011 int cpu = smp_processor_id();
2012 struct netdev_queue *nq;
2013 struct igc_ring *ring;
2016 if (unlikely(!xdpf))
2019 ring = igc_xdp_get_tx_ring(adapter, cpu);
2020 nq = txring_txq(ring);
2022 __netif_tx_lock(nq, cpu);
2023 res = igc_xdp_init_tx_descriptor(ring, xdpf);
2024 __netif_tx_unlock(nq);
2028 static struct sk_buff *igc_xdp_run_prog(struct igc_adapter *adapter,
2029 struct xdp_buff *xdp)
2031 struct bpf_prog *prog;
2037 prog = READ_ONCE(adapter->xdp_prog);
2043 act = bpf_prog_run_xdp(prog, xdp);
2049 if (igc_xdp_xmit_back(adapter, xdp) < 0)
2054 if (xdp_do_redirect(adapter->netdev, xdp, prog) < 0)
2056 res = IGC_XDP_REDIRECT;
2059 bpf_warn_invalid_xdp_action(act);
2063 trace_xdp_exception(adapter->netdev, prog, act);
2066 res = IGC_XDP_CONSUMED;
2072 return ERR_PTR(-res);
2075 /* This function assumes __netif_tx_lock is held by the caller. */
2076 static void igc_flush_tx_descriptors(struct igc_ring *ring)
2078 /* Once tail pointer is updated, hardware can fetch the descriptors
2079 * any time so we issue a write membar here to ensure all memory
2080 * writes are complete before the tail pointer is updated.
2083 writel(ring->next_to_use, ring->tail);
2086 static void igc_finalize_xdp(struct igc_adapter *adapter, int status)
2088 int cpu = smp_processor_id();
2089 struct netdev_queue *nq;
2090 struct igc_ring *ring;
2092 if (status & IGC_XDP_TX) {
2093 ring = igc_xdp_get_tx_ring(adapter, cpu);
2094 nq = txring_txq(ring);
2096 __netif_tx_lock(nq, cpu);
2097 igc_flush_tx_descriptors(ring);
2098 __netif_tx_unlock(nq);
2101 if (status & IGC_XDP_REDIRECT)
2105 static int igc_clean_rx_irq(struct igc_q_vector *q_vector, const int budget)
2107 unsigned int total_bytes = 0, total_packets = 0;
2108 struct igc_adapter *adapter = q_vector->adapter;
2109 struct igc_ring *rx_ring = q_vector->rx.ring;
2110 struct sk_buff *skb = rx_ring->skb;
2111 u16 cleaned_count = igc_desc_unused(rx_ring);
2112 int xdp_status = 0, rx_buffer_pgcnt;
2114 while (likely(total_packets < budget)) {
2115 union igc_adv_rx_desc *rx_desc;
2116 struct igc_rx_buffer *rx_buffer;
2117 unsigned int size, truesize;
2118 ktime_t timestamp = 0;
2119 struct xdp_buff xdp;
2123 /* return some buffers to hardware, one at a time is too slow */
2124 if (cleaned_count >= IGC_RX_BUFFER_WRITE) {
2125 igc_alloc_rx_buffers(rx_ring, cleaned_count);
2129 rx_desc = IGC_RX_DESC(rx_ring, rx_ring->next_to_clean);
2130 size = le16_to_cpu(rx_desc->wb.upper.length);
2134 /* This memory barrier is needed to keep us from reading
2135 * any other fields out of the rx_desc until we know the
2136 * descriptor has been written back
2140 rx_buffer = igc_get_rx_buffer(rx_ring, size, &rx_buffer_pgcnt);
2141 truesize = igc_get_rx_frame_truesize(rx_ring, size);
2143 pktbuf = page_address(rx_buffer->page) + rx_buffer->page_offset;
2145 if (igc_test_staterr(rx_desc, IGC_RXDADV_STAT_TSIP)) {
2146 timestamp = igc_ptp_rx_pktstamp(q_vector->adapter,
2148 pkt_offset = IGC_TS_HDR_LEN;
2149 size -= IGC_TS_HDR_LEN;
2153 xdp.data = pktbuf + pkt_offset;
2154 xdp.data_end = xdp.data + size;
2155 xdp.data_hard_start = pktbuf - igc_rx_offset(rx_ring);
2156 xdp_set_data_meta_invalid(&xdp);
2157 xdp.frame_sz = truesize;
2158 xdp.rxq = &rx_ring->xdp_rxq;
2160 skb = igc_xdp_run_prog(adapter, &xdp);
2164 unsigned int xdp_res = -PTR_ERR(skb);
2167 case IGC_XDP_CONSUMED:
2168 rx_buffer->pagecnt_bias++;
2171 case IGC_XDP_REDIRECT:
2172 igc_rx_buffer_flip(rx_buffer, truesize);
2173 xdp_status |= xdp_res;
2178 total_bytes += size;
2180 igc_add_rx_frag(rx_ring, rx_buffer, skb, size);
2181 else if (ring_uses_build_skb(rx_ring))
2182 skb = igc_build_skb(rx_ring, rx_buffer, rx_desc, size);
2184 skb = igc_construct_skb(rx_ring, rx_buffer, &xdp,
2187 /* exit if we failed to retrieve a buffer */
2189 rx_ring->rx_stats.alloc_failed++;
2190 rx_buffer->pagecnt_bias++;
2194 igc_put_rx_buffer(rx_ring, rx_buffer, rx_buffer_pgcnt);
2197 /* fetch next buffer in frame if non-eop */
2198 if (igc_is_non_eop(rx_ring, rx_desc))
2201 /* verify the packet layout is correct */
2202 if (igc_cleanup_headers(rx_ring, rx_desc, skb)) {
2207 /* probably a little skewed due to removing CRC */
2208 total_bytes += skb->len;
2210 /* populate checksum, VLAN, and protocol */
2211 igc_process_skb_fields(rx_ring, rx_desc, skb);
2213 napi_gro_receive(&q_vector->napi, skb);
2215 /* reset skb pointer */
2218 /* update budget accounting */
2223 igc_finalize_xdp(adapter, xdp_status);
2225 /* place incomplete frames back on ring for completion */
2228 u64_stats_update_begin(&rx_ring->rx_syncp);
2229 rx_ring->rx_stats.packets += total_packets;
2230 rx_ring->rx_stats.bytes += total_bytes;
2231 u64_stats_update_end(&rx_ring->rx_syncp);
2232 q_vector->rx.total_packets += total_packets;
2233 q_vector->rx.total_bytes += total_bytes;
2236 igc_alloc_rx_buffers(rx_ring, cleaned_count);
2238 return total_packets;
2242 * igc_clean_tx_irq - Reclaim resources after transmit completes
2243 * @q_vector: pointer to q_vector containing needed info
2244 * @napi_budget: Used to determine if we are in netpoll
2246 * returns true if ring is completely cleaned
2248 static bool igc_clean_tx_irq(struct igc_q_vector *q_vector, int napi_budget)
2250 struct igc_adapter *adapter = q_vector->adapter;
2251 unsigned int total_bytes = 0, total_packets = 0;
2252 unsigned int budget = q_vector->tx.work_limit;
2253 struct igc_ring *tx_ring = q_vector->tx.ring;
2254 unsigned int i = tx_ring->next_to_clean;
2255 struct igc_tx_buffer *tx_buffer;
2256 union igc_adv_tx_desc *tx_desc;
2258 if (test_bit(__IGC_DOWN, &adapter->state))
2261 tx_buffer = &tx_ring->tx_buffer_info[i];
2262 tx_desc = IGC_TX_DESC(tx_ring, i);
2263 i -= tx_ring->count;
2266 union igc_adv_tx_desc *eop_desc = tx_buffer->next_to_watch;
2268 /* if next_to_watch is not set then there is no work pending */
2272 /* prevent any other reads prior to eop_desc */
2275 /* if DD is not set pending work has not been completed */
2276 if (!(eop_desc->wb.status & cpu_to_le32(IGC_TXD_STAT_DD)))
2279 /* clear next_to_watch to prevent false hangs */
2280 tx_buffer->next_to_watch = NULL;
2282 /* update the statistics for this packet */
2283 total_bytes += tx_buffer->bytecount;
2284 total_packets += tx_buffer->gso_segs;
2286 if (tx_buffer->tx_flags & IGC_TX_FLAGS_XDP)
2287 xdp_return_frame(tx_buffer->xdpf);
2289 napi_consume_skb(tx_buffer->skb, napi_budget);
2291 /* unmap skb header data */
2292 dma_unmap_single(tx_ring->dev,
2293 dma_unmap_addr(tx_buffer, dma),
2294 dma_unmap_len(tx_buffer, len),
2297 /* clear tx_buffer data */
2298 dma_unmap_len_set(tx_buffer, len, 0);
2300 /* clear last DMA location and unmap remaining buffers */
2301 while (tx_desc != eop_desc) {
2306 i -= tx_ring->count;
2307 tx_buffer = tx_ring->tx_buffer_info;
2308 tx_desc = IGC_TX_DESC(tx_ring, 0);
2311 /* unmap any remaining paged data */
2312 if (dma_unmap_len(tx_buffer, len)) {
2313 dma_unmap_page(tx_ring->dev,
2314 dma_unmap_addr(tx_buffer, dma),
2315 dma_unmap_len(tx_buffer, len),
2317 dma_unmap_len_set(tx_buffer, len, 0);
2321 /* move us one more past the eop_desc for start of next pkt */
2326 i -= tx_ring->count;
2327 tx_buffer = tx_ring->tx_buffer_info;
2328 tx_desc = IGC_TX_DESC(tx_ring, 0);
2331 /* issue prefetch for next Tx descriptor */
2334 /* update budget accounting */
2336 } while (likely(budget));
2338 netdev_tx_completed_queue(txring_txq(tx_ring),
2339 total_packets, total_bytes);
2341 i += tx_ring->count;
2342 tx_ring->next_to_clean = i;
2343 u64_stats_update_begin(&tx_ring->tx_syncp);
2344 tx_ring->tx_stats.bytes += total_bytes;
2345 tx_ring->tx_stats.packets += total_packets;
2346 u64_stats_update_end(&tx_ring->tx_syncp);
2347 q_vector->tx.total_bytes += total_bytes;
2348 q_vector->tx.total_packets += total_packets;
2350 if (test_bit(IGC_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags)) {
2351 struct igc_hw *hw = &adapter->hw;
2353 /* Detect a transmit hang in hardware, this serializes the
2354 * check with the clearing of time_stamp and movement of i
2356 clear_bit(IGC_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags);
2357 if (tx_buffer->next_to_watch &&
2358 time_after(jiffies, tx_buffer->time_stamp +
2359 (adapter->tx_timeout_factor * HZ)) &&
2360 !(rd32(IGC_STATUS) & IGC_STATUS_TXOFF)) {
2361 /* detected Tx unit hang */
2362 netdev_err(tx_ring->netdev,
2363 "Detected Tx Unit Hang\n"
2367 " next_to_use <%x>\n"
2368 " next_to_clean <%x>\n"
2369 "buffer_info[next_to_clean]\n"
2370 " time_stamp <%lx>\n"
2371 " next_to_watch <%p>\n"
2373 " desc.status <%x>\n",
2374 tx_ring->queue_index,
2375 rd32(IGC_TDH(tx_ring->reg_idx)),
2376 readl(tx_ring->tail),
2377 tx_ring->next_to_use,
2378 tx_ring->next_to_clean,
2379 tx_buffer->time_stamp,
2380 tx_buffer->next_to_watch,
2382 tx_buffer->next_to_watch->wb.status);
2383 netif_stop_subqueue(tx_ring->netdev,
2384 tx_ring->queue_index);
2386 /* we are about to reset, no point in enabling stuff */
2391 #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
2392 if (unlikely(total_packets &&
2393 netif_carrier_ok(tx_ring->netdev) &&
2394 igc_desc_unused(tx_ring) >= TX_WAKE_THRESHOLD)) {
2395 /* Make sure that anybody stopping the queue after this
2396 * sees the new next_to_clean.
2399 if (__netif_subqueue_stopped(tx_ring->netdev,
2400 tx_ring->queue_index) &&
2401 !(test_bit(__IGC_DOWN, &adapter->state))) {
2402 netif_wake_subqueue(tx_ring->netdev,
2403 tx_ring->queue_index);
2405 u64_stats_update_begin(&tx_ring->tx_syncp);
2406 tx_ring->tx_stats.restart_queue++;
2407 u64_stats_update_end(&tx_ring->tx_syncp);
2414 static int igc_find_mac_filter(struct igc_adapter *adapter,
2415 enum igc_mac_filter_type type, const u8 *addr)
2417 struct igc_hw *hw = &adapter->hw;
2418 int max_entries = hw->mac.rar_entry_count;
2422 for (i = 0; i < max_entries; i++) {
2423 ral = rd32(IGC_RAL(i));
2424 rah = rd32(IGC_RAH(i));
2426 if (!(rah & IGC_RAH_AV))
2428 if (!!(rah & IGC_RAH_ASEL_SRC_ADDR) != type)
2430 if ((rah & IGC_RAH_RAH_MASK) !=
2431 le16_to_cpup((__le16 *)(addr + 4)))
2433 if (ral != le32_to_cpup((__le32 *)(addr)))
2442 static int igc_get_avail_mac_filter_slot(struct igc_adapter *adapter)
2444 struct igc_hw *hw = &adapter->hw;
2445 int max_entries = hw->mac.rar_entry_count;
2449 for (i = 0; i < max_entries; i++) {
2450 rah = rd32(IGC_RAH(i));
2452 if (!(rah & IGC_RAH_AV))
2460 * igc_add_mac_filter() - Add MAC address filter
2461 * @adapter: Pointer to adapter where the filter should be added
2462 * @type: MAC address filter type (source or destination)
2463 * @addr: MAC address
2464 * @queue: If non-negative, queue assignment feature is enabled and frames
2465 * matching the filter are enqueued onto 'queue'. Otherwise, queue
2466 * assignment is disabled.
2468 * Return: 0 in case of success, negative errno code otherwise.
2470 static int igc_add_mac_filter(struct igc_adapter *adapter,
2471 enum igc_mac_filter_type type, const u8 *addr,
2474 struct net_device *dev = adapter->netdev;
2477 index = igc_find_mac_filter(adapter, type, addr);
2481 index = igc_get_avail_mac_filter_slot(adapter);
2485 netdev_dbg(dev, "Add MAC address filter: index %d type %s address %pM queue %d\n",
2486 index, type == IGC_MAC_FILTER_TYPE_DST ? "dst" : "src",
2490 igc_set_mac_filter_hw(adapter, index, type, addr, queue);
2495 * igc_del_mac_filter() - Delete MAC address filter
2496 * @adapter: Pointer to adapter where the filter should be deleted from
2497 * @type: MAC address filter type (source or destination)
2498 * @addr: MAC address
2500 static void igc_del_mac_filter(struct igc_adapter *adapter,
2501 enum igc_mac_filter_type type, const u8 *addr)
2503 struct net_device *dev = adapter->netdev;
2506 index = igc_find_mac_filter(adapter, type, addr);
2511 /* If this is the default filter, we don't actually delete it.
2512 * We just reset to its default value i.e. disable queue
2515 netdev_dbg(dev, "Disable default MAC filter queue assignment");
2517 igc_set_mac_filter_hw(adapter, 0, type, addr, -1);
2519 netdev_dbg(dev, "Delete MAC address filter: index %d type %s address %pM\n",
2521 type == IGC_MAC_FILTER_TYPE_DST ? "dst" : "src",
2524 igc_clear_mac_filter_hw(adapter, index);
2529 * igc_add_vlan_prio_filter() - Add VLAN priority filter
2530 * @adapter: Pointer to adapter where the filter should be added
2531 * @prio: VLAN priority value
2532 * @queue: Queue number which matching frames are assigned to
2534 * Return: 0 in case of success, negative errno code otherwise.
2536 static int igc_add_vlan_prio_filter(struct igc_adapter *adapter, int prio,
2539 struct net_device *dev = adapter->netdev;
2540 struct igc_hw *hw = &adapter->hw;
2543 vlanpqf = rd32(IGC_VLANPQF);
2545 if (vlanpqf & IGC_VLANPQF_VALID(prio)) {
2546 netdev_dbg(dev, "VLAN priority filter already in use\n");
2550 vlanpqf |= IGC_VLANPQF_QSEL(prio, queue);
2551 vlanpqf |= IGC_VLANPQF_VALID(prio);
2553 wr32(IGC_VLANPQF, vlanpqf);
2555 netdev_dbg(dev, "Add VLAN priority filter: prio %d queue %d\n",
2561 * igc_del_vlan_prio_filter() - Delete VLAN priority filter
2562 * @adapter: Pointer to adapter where the filter should be deleted from
2563 * @prio: VLAN priority value
2565 static void igc_del_vlan_prio_filter(struct igc_adapter *adapter, int prio)
2567 struct igc_hw *hw = &adapter->hw;
2570 vlanpqf = rd32(IGC_VLANPQF);
2572 vlanpqf &= ~IGC_VLANPQF_VALID(prio);
2573 vlanpqf &= ~IGC_VLANPQF_QSEL(prio, IGC_VLANPQF_QUEUE_MASK);
2575 wr32(IGC_VLANPQF, vlanpqf);
2577 netdev_dbg(adapter->netdev, "Delete VLAN priority filter: prio %d\n",
2581 static int igc_get_avail_etype_filter_slot(struct igc_adapter *adapter)
2583 struct igc_hw *hw = &adapter->hw;
2586 for (i = 0; i < MAX_ETYPE_FILTER; i++) {
2587 u32 etqf = rd32(IGC_ETQF(i));
2589 if (!(etqf & IGC_ETQF_FILTER_ENABLE))
2597 * igc_add_etype_filter() - Add ethertype filter
2598 * @adapter: Pointer to adapter where the filter should be added
2599 * @etype: Ethertype value
2600 * @queue: If non-negative, queue assignment feature is enabled and frames
2601 * matching the filter are enqueued onto 'queue'. Otherwise, queue
2602 * assignment is disabled.
2604 * Return: 0 in case of success, negative errno code otherwise.
2606 static int igc_add_etype_filter(struct igc_adapter *adapter, u16 etype,
2609 struct igc_hw *hw = &adapter->hw;
2613 index = igc_get_avail_etype_filter_slot(adapter);
2617 etqf = rd32(IGC_ETQF(index));
2619 etqf &= ~IGC_ETQF_ETYPE_MASK;
2623 etqf &= ~IGC_ETQF_QUEUE_MASK;
2624 etqf |= (queue << IGC_ETQF_QUEUE_SHIFT);
2625 etqf |= IGC_ETQF_QUEUE_ENABLE;
2628 etqf |= IGC_ETQF_FILTER_ENABLE;
2630 wr32(IGC_ETQF(index), etqf);
2632 netdev_dbg(adapter->netdev, "Add ethertype filter: etype %04x queue %d\n",
2637 static int igc_find_etype_filter(struct igc_adapter *adapter, u16 etype)
2639 struct igc_hw *hw = &adapter->hw;
2642 for (i = 0; i < MAX_ETYPE_FILTER; i++) {
2643 u32 etqf = rd32(IGC_ETQF(i));
2645 if ((etqf & IGC_ETQF_ETYPE_MASK) == etype)
2653 * igc_del_etype_filter() - Delete ethertype filter
2654 * @adapter: Pointer to adapter where the filter should be deleted from
2655 * @etype: Ethertype value
2657 static void igc_del_etype_filter(struct igc_adapter *adapter, u16 etype)
2659 struct igc_hw *hw = &adapter->hw;
2662 index = igc_find_etype_filter(adapter, etype);
2666 wr32(IGC_ETQF(index), 0);
2668 netdev_dbg(adapter->netdev, "Delete ethertype filter: etype %04x\n",
2672 static int igc_enable_nfc_rule(struct igc_adapter *adapter,
2673 const struct igc_nfc_rule *rule)
2677 if (rule->filter.match_flags & IGC_FILTER_FLAG_ETHER_TYPE) {
2678 err = igc_add_etype_filter(adapter, rule->filter.etype,
2684 if (rule->filter.match_flags & IGC_FILTER_FLAG_SRC_MAC_ADDR) {
2685 err = igc_add_mac_filter(adapter, IGC_MAC_FILTER_TYPE_SRC,
2686 rule->filter.src_addr, rule->action);
2691 if (rule->filter.match_flags & IGC_FILTER_FLAG_DST_MAC_ADDR) {
2692 err = igc_add_mac_filter(adapter, IGC_MAC_FILTER_TYPE_DST,
2693 rule->filter.dst_addr, rule->action);
2698 if (rule->filter.match_flags & IGC_FILTER_FLAG_VLAN_TCI) {
2699 int prio = (rule->filter.vlan_tci & VLAN_PRIO_MASK) >>
2702 err = igc_add_vlan_prio_filter(adapter, prio, rule->action);
2710 static void igc_disable_nfc_rule(struct igc_adapter *adapter,
2711 const struct igc_nfc_rule *rule)
2713 if (rule->filter.match_flags & IGC_FILTER_FLAG_ETHER_TYPE)
2714 igc_del_etype_filter(adapter, rule->filter.etype);
2716 if (rule->filter.match_flags & IGC_FILTER_FLAG_VLAN_TCI) {
2717 int prio = (rule->filter.vlan_tci & VLAN_PRIO_MASK) >>
2720 igc_del_vlan_prio_filter(adapter, prio);
2723 if (rule->filter.match_flags & IGC_FILTER_FLAG_SRC_MAC_ADDR)
2724 igc_del_mac_filter(adapter, IGC_MAC_FILTER_TYPE_SRC,
2725 rule->filter.src_addr);
2727 if (rule->filter.match_flags & IGC_FILTER_FLAG_DST_MAC_ADDR)
2728 igc_del_mac_filter(adapter, IGC_MAC_FILTER_TYPE_DST,
2729 rule->filter.dst_addr);
2733 * igc_get_nfc_rule() - Get NFC rule
2734 * @adapter: Pointer to adapter
2735 * @location: Rule location
2737 * Context: Expects adapter->nfc_rule_lock to be held by caller.
2739 * Return: Pointer to NFC rule at @location. If not found, NULL.
2741 struct igc_nfc_rule *igc_get_nfc_rule(struct igc_adapter *adapter,
2744 struct igc_nfc_rule *rule;
2746 list_for_each_entry(rule, &adapter->nfc_rule_list, list) {
2747 if (rule->location == location)
2749 if (rule->location > location)
2757 * igc_del_nfc_rule() - Delete NFC rule
2758 * @adapter: Pointer to adapter
2759 * @rule: Pointer to rule to be deleted
2761 * Disable NFC rule in hardware and delete it from adapter.
2763 * Context: Expects adapter->nfc_rule_lock to be held by caller.
2765 void igc_del_nfc_rule(struct igc_adapter *adapter, struct igc_nfc_rule *rule)
2767 igc_disable_nfc_rule(adapter, rule);
2769 list_del(&rule->list);
2770 adapter->nfc_rule_count--;
2775 static void igc_flush_nfc_rules(struct igc_adapter *adapter)
2777 struct igc_nfc_rule *rule, *tmp;
2779 mutex_lock(&adapter->nfc_rule_lock);
2781 list_for_each_entry_safe(rule, tmp, &adapter->nfc_rule_list, list)
2782 igc_del_nfc_rule(adapter, rule);
2784 mutex_unlock(&adapter->nfc_rule_lock);
2788 * igc_add_nfc_rule() - Add NFC rule
2789 * @adapter: Pointer to adapter
2790 * @rule: Pointer to rule to be added
2792 * Enable NFC rule in hardware and add it to adapter.
2794 * Context: Expects adapter->nfc_rule_lock to be held by caller.
2796 * Return: 0 on success, negative errno on failure.
2798 int igc_add_nfc_rule(struct igc_adapter *adapter, struct igc_nfc_rule *rule)
2800 struct igc_nfc_rule *pred, *cur;
2803 err = igc_enable_nfc_rule(adapter, rule);
2808 list_for_each_entry(cur, &adapter->nfc_rule_list, list) {
2809 if (cur->location >= rule->location)
2814 list_add(&rule->list, pred ? &pred->list : &adapter->nfc_rule_list);
2815 adapter->nfc_rule_count++;
2819 static void igc_restore_nfc_rules(struct igc_adapter *adapter)
2821 struct igc_nfc_rule *rule;
2823 mutex_lock(&adapter->nfc_rule_lock);
2825 list_for_each_entry_reverse(rule, &adapter->nfc_rule_list, list)
2826 igc_enable_nfc_rule(adapter, rule);
2828 mutex_unlock(&adapter->nfc_rule_lock);
2831 static int igc_uc_sync(struct net_device *netdev, const unsigned char *addr)
2833 struct igc_adapter *adapter = netdev_priv(netdev);
2835 return igc_add_mac_filter(adapter, IGC_MAC_FILTER_TYPE_DST, addr, -1);
2838 static int igc_uc_unsync(struct net_device *netdev, const unsigned char *addr)
2840 struct igc_adapter *adapter = netdev_priv(netdev);
2842 igc_del_mac_filter(adapter, IGC_MAC_FILTER_TYPE_DST, addr);
2847 * igc_set_rx_mode - Secondary Unicast, Multicast and Promiscuous mode set
2848 * @netdev: network interface device structure
2850 * The set_rx_mode entry point is called whenever the unicast or multicast
2851 * address lists or the network interface flags are updated. This routine is
2852 * responsible for configuring the hardware for proper unicast, multicast,
2853 * promiscuous mode, and all-multi behavior.
2855 static void igc_set_rx_mode(struct net_device *netdev)
2857 struct igc_adapter *adapter = netdev_priv(netdev);
2858 struct igc_hw *hw = &adapter->hw;
2859 u32 rctl = 0, rlpml = MAX_JUMBO_FRAME_SIZE;
2862 /* Check for Promiscuous and All Multicast modes */
2863 if (netdev->flags & IFF_PROMISC) {
2864 rctl |= IGC_RCTL_UPE | IGC_RCTL_MPE;
2866 if (netdev->flags & IFF_ALLMULTI) {
2867 rctl |= IGC_RCTL_MPE;
2869 /* Write addresses to the MTA, if the attempt fails
2870 * then we should just turn on promiscuous mode so
2871 * that we can at least receive multicast traffic
2873 count = igc_write_mc_addr_list(netdev);
2875 rctl |= IGC_RCTL_MPE;
2879 /* Write addresses to available RAR registers, if there is not
2880 * sufficient space to store all the addresses then enable
2881 * unicast promiscuous mode
2883 if (__dev_uc_sync(netdev, igc_uc_sync, igc_uc_unsync))
2884 rctl |= IGC_RCTL_UPE;
2886 /* update state of unicast and multicast */
2887 rctl |= rd32(IGC_RCTL) & ~(IGC_RCTL_UPE | IGC_RCTL_MPE);
2888 wr32(IGC_RCTL, rctl);
2890 #if (PAGE_SIZE < 8192)
2891 if (adapter->max_frame_size <= IGC_MAX_FRAME_BUILD_SKB)
2892 rlpml = IGC_MAX_FRAME_BUILD_SKB;
2894 wr32(IGC_RLPML, rlpml);
2898 * igc_configure - configure the hardware for RX and TX
2899 * @adapter: private board structure
2901 static void igc_configure(struct igc_adapter *adapter)
2903 struct net_device *netdev = adapter->netdev;
2906 igc_get_hw_control(adapter);
2907 igc_set_rx_mode(netdev);
2909 igc_setup_tctl(adapter);
2910 igc_setup_mrqc(adapter);
2911 igc_setup_rctl(adapter);
2913 igc_set_default_mac_filter(adapter);
2914 igc_restore_nfc_rules(adapter);
2916 igc_configure_tx(adapter);
2917 igc_configure_rx(adapter);
2919 igc_rx_fifo_flush_base(&adapter->hw);
2921 /* call igc_desc_unused which always leaves
2922 * at least 1 descriptor unused to make sure
2923 * next_to_use != next_to_clean
2925 for (i = 0; i < adapter->num_rx_queues; i++) {
2926 struct igc_ring *ring = adapter->rx_ring[i];
2928 igc_alloc_rx_buffers(ring, igc_desc_unused(ring));
2933 * igc_write_ivar - configure ivar for given MSI-X vector
2934 * @hw: pointer to the HW structure
2935 * @msix_vector: vector number we are allocating to a given ring
2936 * @index: row index of IVAR register to write within IVAR table
2937 * @offset: column offset of in IVAR, should be multiple of 8
2939 * The IVAR table consists of 2 columns,
2940 * each containing an cause allocation for an Rx and Tx ring, and a
2941 * variable number of rows depending on the number of queues supported.
2943 static void igc_write_ivar(struct igc_hw *hw, int msix_vector,
2944 int index, int offset)
2946 u32 ivar = array_rd32(IGC_IVAR0, index);
2948 /* clear any bits that are currently set */
2949 ivar &= ~((u32)0xFF << offset);
2951 /* write vector and valid bit */
2952 ivar |= (msix_vector | IGC_IVAR_VALID) << offset;
2954 array_wr32(IGC_IVAR0, index, ivar);
2957 static void igc_assign_vector(struct igc_q_vector *q_vector, int msix_vector)
2959 struct igc_adapter *adapter = q_vector->adapter;
2960 struct igc_hw *hw = &adapter->hw;
2961 int rx_queue = IGC_N0_QUEUE;
2962 int tx_queue = IGC_N0_QUEUE;
2964 if (q_vector->rx.ring)
2965 rx_queue = q_vector->rx.ring->reg_idx;
2966 if (q_vector->tx.ring)
2967 tx_queue = q_vector->tx.ring->reg_idx;
2969 switch (hw->mac.type) {
2971 if (rx_queue > IGC_N0_QUEUE)
2972 igc_write_ivar(hw, msix_vector,
2974 (rx_queue & 0x1) << 4);
2975 if (tx_queue > IGC_N0_QUEUE)
2976 igc_write_ivar(hw, msix_vector,
2978 ((tx_queue & 0x1) << 4) + 8);
2979 q_vector->eims_value = BIT(msix_vector);
2982 WARN_ONCE(hw->mac.type != igc_i225, "Wrong MAC type\n");
2986 /* add q_vector eims value to global eims_enable_mask */
2987 adapter->eims_enable_mask |= q_vector->eims_value;
2989 /* configure q_vector to set itr on first interrupt */
2990 q_vector->set_itr = 1;
2994 * igc_configure_msix - Configure MSI-X hardware
2995 * @adapter: Pointer to adapter structure
2997 * igc_configure_msix sets up the hardware to properly
2998 * generate MSI-X interrupts.
3000 static void igc_configure_msix(struct igc_adapter *adapter)
3002 struct igc_hw *hw = &adapter->hw;
3006 adapter->eims_enable_mask = 0;
3008 /* set vector for other causes, i.e. link changes */
3009 switch (hw->mac.type) {
3011 /* Turn on MSI-X capability first, or our settings
3012 * won't stick. And it will take days to debug.
3014 wr32(IGC_GPIE, IGC_GPIE_MSIX_MODE |
3015 IGC_GPIE_PBA | IGC_GPIE_EIAME |
3018 /* enable msix_other interrupt */
3019 adapter->eims_other = BIT(vector);
3020 tmp = (vector++ | IGC_IVAR_VALID) << 8;
3022 wr32(IGC_IVAR_MISC, tmp);
3025 /* do nothing, since nothing else supports MSI-X */
3027 } /* switch (hw->mac.type) */
3029 adapter->eims_enable_mask |= adapter->eims_other;
3031 for (i = 0; i < adapter->num_q_vectors; i++)
3032 igc_assign_vector(adapter->q_vector[i], vector++);
3038 * igc_irq_enable - Enable default interrupt generation settings
3039 * @adapter: board private structure
3041 static void igc_irq_enable(struct igc_adapter *adapter)
3043 struct igc_hw *hw = &adapter->hw;
3045 if (adapter->msix_entries) {
3046 u32 ims = IGC_IMS_LSC | IGC_IMS_DOUTSYNC | IGC_IMS_DRSTA;
3047 u32 regval = rd32(IGC_EIAC);
3049 wr32(IGC_EIAC, regval | adapter->eims_enable_mask);
3050 regval = rd32(IGC_EIAM);
3051 wr32(IGC_EIAM, regval | adapter->eims_enable_mask);
3052 wr32(IGC_EIMS, adapter->eims_enable_mask);
3055 wr32(IGC_IMS, IMS_ENABLE_MASK | IGC_IMS_DRSTA);
3056 wr32(IGC_IAM, IMS_ENABLE_MASK | IGC_IMS_DRSTA);
3061 * igc_irq_disable - Mask off interrupt generation on the NIC
3062 * @adapter: board private structure
3064 static void igc_irq_disable(struct igc_adapter *adapter)
3066 struct igc_hw *hw = &adapter->hw;
3068 if (adapter->msix_entries) {
3069 u32 regval = rd32(IGC_EIAM);
3071 wr32(IGC_EIAM, regval & ~adapter->eims_enable_mask);
3072 wr32(IGC_EIMC, adapter->eims_enable_mask);
3073 regval = rd32(IGC_EIAC);
3074 wr32(IGC_EIAC, regval & ~adapter->eims_enable_mask);
3081 if (adapter->msix_entries) {
3084 synchronize_irq(adapter->msix_entries[vector++].vector);
3086 for (i = 0; i < adapter->num_q_vectors; i++)
3087 synchronize_irq(adapter->msix_entries[vector++].vector);
3089 synchronize_irq(adapter->pdev->irq);
3093 void igc_set_flag_queue_pairs(struct igc_adapter *adapter,
3094 const u32 max_rss_queues)
3096 /* Determine if we need to pair queues. */
3097 /* If rss_queues > half of max_rss_queues, pair the queues in
3098 * order to conserve interrupts due to limited supply.
3100 if (adapter->rss_queues > (max_rss_queues / 2))
3101 adapter->flags |= IGC_FLAG_QUEUE_PAIRS;
3103 adapter->flags &= ~IGC_FLAG_QUEUE_PAIRS;
3106 unsigned int igc_get_max_rss_queues(struct igc_adapter *adapter)
3108 return IGC_MAX_RX_QUEUES;
3111 static void igc_init_queue_configuration(struct igc_adapter *adapter)
3115 max_rss_queues = igc_get_max_rss_queues(adapter);
3116 adapter->rss_queues = min_t(u32, max_rss_queues, num_online_cpus());
3118 igc_set_flag_queue_pairs(adapter, max_rss_queues);
3122 * igc_reset_q_vector - Reset config for interrupt vector
3123 * @adapter: board private structure to initialize
3124 * @v_idx: Index of vector to be reset
3126 * If NAPI is enabled it will delete any references to the
3127 * NAPI struct. This is preparation for igc_free_q_vector.
3129 static void igc_reset_q_vector(struct igc_adapter *adapter, int v_idx)
3131 struct igc_q_vector *q_vector = adapter->q_vector[v_idx];
3133 /* if we're coming from igc_set_interrupt_capability, the vectors are
3139 if (q_vector->tx.ring)
3140 adapter->tx_ring[q_vector->tx.ring->queue_index] = NULL;
3142 if (q_vector->rx.ring)
3143 adapter->rx_ring[q_vector->rx.ring->queue_index] = NULL;
3145 netif_napi_del(&q_vector->napi);
3149 * igc_free_q_vector - Free memory allocated for specific interrupt vector
3150 * @adapter: board private structure to initialize
3151 * @v_idx: Index of vector to be freed
3153 * This function frees the memory allocated to the q_vector.
3155 static void igc_free_q_vector(struct igc_adapter *adapter, int v_idx)
3157 struct igc_q_vector *q_vector = adapter->q_vector[v_idx];
3159 adapter->q_vector[v_idx] = NULL;
3161 /* igc_get_stats64() might access the rings on this vector,
3162 * we must wait a grace period before freeing it.
3165 kfree_rcu(q_vector, rcu);
3169 * igc_free_q_vectors - Free memory allocated for interrupt vectors
3170 * @adapter: board private structure to initialize
3172 * This function frees the memory allocated to the q_vectors. In addition if
3173 * NAPI is enabled it will delete any references to the NAPI struct prior
3174 * to freeing the q_vector.
3176 static void igc_free_q_vectors(struct igc_adapter *adapter)
3178 int v_idx = adapter->num_q_vectors;
3180 adapter->num_tx_queues = 0;
3181 adapter->num_rx_queues = 0;
3182 adapter->num_q_vectors = 0;
3185 igc_reset_q_vector(adapter, v_idx);
3186 igc_free_q_vector(adapter, v_idx);
3191 * igc_update_itr - update the dynamic ITR value based on statistics
3192 * @q_vector: pointer to q_vector
3193 * @ring_container: ring info to update the itr for
3195 * Stores a new ITR value based on packets and byte
3196 * counts during the last interrupt. The advantage of per interrupt
3197 * computation is faster updates and more accurate ITR for the current
3198 * traffic pattern. Constants in this function were computed
3199 * based on theoretical maximum wire speed and thresholds were set based
3200 * on testing data as well as attempting to minimize response time
3201 * while increasing bulk throughput.
3202 * NOTE: These calculations are only valid when operating in a single-
3203 * queue environment.
3205 static void igc_update_itr(struct igc_q_vector *q_vector,
3206 struct igc_ring_container *ring_container)
3208 unsigned int packets = ring_container->total_packets;
3209 unsigned int bytes = ring_container->total_bytes;
3210 u8 itrval = ring_container->itr;
3212 /* no packets, exit with status unchanged */
3217 case lowest_latency:
3218 /* handle TSO and jumbo frames */
3219 if (bytes / packets > 8000)
3220 itrval = bulk_latency;
3221 else if ((packets < 5) && (bytes > 512))
3222 itrval = low_latency;
3224 case low_latency: /* 50 usec aka 20000 ints/s */
3225 if (bytes > 10000) {
3226 /* this if handles the TSO accounting */
3227 if (bytes / packets > 8000)
3228 itrval = bulk_latency;
3229 else if ((packets < 10) || ((bytes / packets) > 1200))
3230 itrval = bulk_latency;
3231 else if ((packets > 35))
3232 itrval = lowest_latency;
3233 } else if (bytes / packets > 2000) {
3234 itrval = bulk_latency;
3235 } else if (packets <= 2 && bytes < 512) {
3236 itrval = lowest_latency;
3239 case bulk_latency: /* 250 usec aka 4000 ints/s */
3240 if (bytes > 25000) {
3242 itrval = low_latency;
3243 } else if (bytes < 1500) {
3244 itrval = low_latency;
3249 /* clear work counters since we have the values we need */
3250 ring_container->total_bytes = 0;
3251 ring_container->total_packets = 0;
3253 /* write updated itr to ring container */
3254 ring_container->itr = itrval;
3257 static void igc_set_itr(struct igc_q_vector *q_vector)
3259 struct igc_adapter *adapter = q_vector->adapter;
3260 u32 new_itr = q_vector->itr_val;
3263 /* for non-gigabit speeds, just fix the interrupt rate at 4000 */
3264 switch (adapter->link_speed) {
3268 new_itr = IGC_4K_ITR;
3274 igc_update_itr(q_vector, &q_vector->tx);
3275 igc_update_itr(q_vector, &q_vector->rx);
3277 current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
3279 /* conservative mode (itr 3) eliminates the lowest_latency setting */
3280 if (current_itr == lowest_latency &&
3281 ((q_vector->rx.ring && adapter->rx_itr_setting == 3) ||
3282 (!q_vector->rx.ring && adapter->tx_itr_setting == 3)))
3283 current_itr = low_latency;
3285 switch (current_itr) {
3286 /* counts and packets in update_itr are dependent on these numbers */
3287 case lowest_latency:
3288 new_itr = IGC_70K_ITR; /* 70,000 ints/sec */
3291 new_itr = IGC_20K_ITR; /* 20,000 ints/sec */
3294 new_itr = IGC_4K_ITR; /* 4,000 ints/sec */
3301 if (new_itr != q_vector->itr_val) {
3302 /* this attempts to bias the interrupt rate towards Bulk
3303 * by adding intermediate steps when interrupt rate is
3306 new_itr = new_itr > q_vector->itr_val ?
3307 max((new_itr * q_vector->itr_val) /
3308 (new_itr + (q_vector->itr_val >> 2)),
3310 /* Don't write the value here; it resets the adapter's
3311 * internal timer, and causes us to delay far longer than
3312 * we should between interrupts. Instead, we write the ITR
3313 * value at the beginning of the next interrupt so the timing
3314 * ends up being correct.
3316 q_vector->itr_val = new_itr;
3317 q_vector->set_itr = 1;
3321 static void igc_reset_interrupt_capability(struct igc_adapter *adapter)
3323 int v_idx = adapter->num_q_vectors;
3325 if (adapter->msix_entries) {
3326 pci_disable_msix(adapter->pdev);
3327 kfree(adapter->msix_entries);
3328 adapter->msix_entries = NULL;
3329 } else if (adapter->flags & IGC_FLAG_HAS_MSI) {
3330 pci_disable_msi(adapter->pdev);
3334 igc_reset_q_vector(adapter, v_idx);
3338 * igc_set_interrupt_capability - set MSI or MSI-X if supported
3339 * @adapter: Pointer to adapter structure
3340 * @msix: boolean value for MSI-X capability
3342 * Attempt to configure interrupts using the best available
3343 * capabilities of the hardware and kernel.
3345 static void igc_set_interrupt_capability(struct igc_adapter *adapter,
3353 adapter->flags |= IGC_FLAG_HAS_MSIX;
3355 /* Number of supported queues. */
3356 adapter->num_rx_queues = adapter->rss_queues;
3358 adapter->num_tx_queues = adapter->rss_queues;
3360 /* start with one vector for every Rx queue */
3361 numvecs = adapter->num_rx_queues;
3363 /* if Tx handler is separate add 1 for every Tx queue */
3364 if (!(adapter->flags & IGC_FLAG_QUEUE_PAIRS))
3365 numvecs += adapter->num_tx_queues;
3367 /* store the number of vectors reserved for queues */
3368 adapter->num_q_vectors = numvecs;
3370 /* add 1 vector for link status interrupts */
3373 adapter->msix_entries = kcalloc(numvecs, sizeof(struct msix_entry),
3376 if (!adapter->msix_entries)
3379 /* populate entry values */
3380 for (i = 0; i < numvecs; i++)
3381 adapter->msix_entries[i].entry = i;
3383 err = pci_enable_msix_range(adapter->pdev,
3384 adapter->msix_entries,
3390 kfree(adapter->msix_entries);
3391 adapter->msix_entries = NULL;
3393 igc_reset_interrupt_capability(adapter);
3396 adapter->flags &= ~IGC_FLAG_HAS_MSIX;
3398 adapter->rss_queues = 1;
3399 adapter->flags |= IGC_FLAG_QUEUE_PAIRS;
3400 adapter->num_rx_queues = 1;
3401 adapter->num_tx_queues = 1;
3402 adapter->num_q_vectors = 1;
3403 if (!pci_enable_msi(adapter->pdev))
3404 adapter->flags |= IGC_FLAG_HAS_MSI;
3408 * igc_update_ring_itr - update the dynamic ITR value based on packet size
3409 * @q_vector: pointer to q_vector
3411 * Stores a new ITR value based on strictly on packet size. This
3412 * algorithm is less sophisticated than that used in igc_update_itr,
3413 * due to the difficulty of synchronizing statistics across multiple
3414 * receive rings. The divisors and thresholds used by this function
3415 * were determined based on theoretical maximum wire speed and testing
3416 * data, in order to minimize response time while increasing bulk
3418 * NOTE: This function is called only when operating in a multiqueue
3419 * receive environment.
3421 static void igc_update_ring_itr(struct igc_q_vector *q_vector)
3423 struct igc_adapter *adapter = q_vector->adapter;
3424 int new_val = q_vector->itr_val;
3425 int avg_wire_size = 0;
3426 unsigned int packets;
3428 /* For non-gigabit speeds, just fix the interrupt rate at 4000
3429 * ints/sec - ITR timer value of 120 ticks.
3431 switch (adapter->link_speed) {
3434 new_val = IGC_4K_ITR;
3440 packets = q_vector->rx.total_packets;
3442 avg_wire_size = q_vector->rx.total_bytes / packets;
3444 packets = q_vector->tx.total_packets;
3446 avg_wire_size = max_t(u32, avg_wire_size,
3447 q_vector->tx.total_bytes / packets);
3449 /* if avg_wire_size isn't set no work was done */
3453 /* Add 24 bytes to size to account for CRC, preamble, and gap */
3454 avg_wire_size += 24;
3456 /* Don't starve jumbo frames */
3457 avg_wire_size = min(avg_wire_size, 3000);
3459 /* Give a little boost to mid-size frames */
3460 if (avg_wire_size > 300 && avg_wire_size < 1200)
3461 new_val = avg_wire_size / 3;
3463 new_val = avg_wire_size / 2;
3465 /* conservative mode (itr 3) eliminates the lowest_latency setting */
3466 if (new_val < IGC_20K_ITR &&
3467 ((q_vector->rx.ring && adapter->rx_itr_setting == 3) ||
3468 (!q_vector->rx.ring && adapter->tx_itr_setting == 3)))
3469 new_val = IGC_20K_ITR;
3472 if (new_val != q_vector->itr_val) {
3473 q_vector->itr_val = new_val;
3474 q_vector->set_itr = 1;
3477 q_vector->rx.total_bytes = 0;
3478 q_vector->rx.total_packets = 0;
3479 q_vector->tx.total_bytes = 0;
3480 q_vector->tx.total_packets = 0;
3483 static void igc_ring_irq_enable(struct igc_q_vector *q_vector)
3485 struct igc_adapter *adapter = q_vector->adapter;
3486 struct igc_hw *hw = &adapter->hw;
3488 if ((q_vector->rx.ring && (adapter->rx_itr_setting & 3)) ||
3489 (!q_vector->rx.ring && (adapter->tx_itr_setting & 3))) {
3490 if (adapter->num_q_vectors == 1)
3491 igc_set_itr(q_vector);
3493 igc_update_ring_itr(q_vector);
3496 if (!test_bit(__IGC_DOWN, &adapter->state)) {
3497 if (adapter->msix_entries)
3498 wr32(IGC_EIMS, q_vector->eims_value);
3500 igc_irq_enable(adapter);
3504 static void igc_add_ring(struct igc_ring *ring,
3505 struct igc_ring_container *head)
3512 * igc_cache_ring_register - Descriptor ring to register mapping
3513 * @adapter: board private structure to initialize
3515 * Once we know the feature-set enabled for the device, we'll cache
3516 * the register offset the descriptor ring is assigned to.
3518 static void igc_cache_ring_register(struct igc_adapter *adapter)
3522 switch (adapter->hw.mac.type) {
3525 for (; i < adapter->num_rx_queues; i++)
3526 adapter->rx_ring[i]->reg_idx = i;
3527 for (; j < adapter->num_tx_queues; j++)
3528 adapter->tx_ring[j]->reg_idx = j;
3534 * igc_poll - NAPI Rx polling callback
3535 * @napi: napi polling structure
3536 * @budget: count of how many packets we should handle
3538 static int igc_poll(struct napi_struct *napi, int budget)
3540 struct igc_q_vector *q_vector = container_of(napi,
3541 struct igc_q_vector,
3543 bool clean_complete = true;
3546 if (q_vector->tx.ring)
3547 clean_complete = igc_clean_tx_irq(q_vector, budget);
3549 if (q_vector->rx.ring) {
3550 int cleaned = igc_clean_rx_irq(q_vector, budget);
3552 work_done += cleaned;
3553 if (cleaned >= budget)
3554 clean_complete = false;
3557 /* If all work not completed, return budget and keep polling */
3558 if (!clean_complete)
3561 /* Exit the polling mode, but don't re-enable interrupts if stack might
3562 * poll us due to busy-polling
3564 if (likely(napi_complete_done(napi, work_done)))
3565 igc_ring_irq_enable(q_vector);
3567 return min(work_done, budget - 1);
3571 * igc_alloc_q_vector - Allocate memory for a single interrupt vector
3572 * @adapter: board private structure to initialize
3573 * @v_count: q_vectors allocated on adapter, used for ring interleaving
3574 * @v_idx: index of vector in adapter struct
3575 * @txr_count: total number of Tx rings to allocate
3576 * @txr_idx: index of first Tx ring to allocate
3577 * @rxr_count: total number of Rx rings to allocate
3578 * @rxr_idx: index of first Rx ring to allocate
3580 * We allocate one q_vector. If allocation fails we return -ENOMEM.
3582 static int igc_alloc_q_vector(struct igc_adapter *adapter,
3583 unsigned int v_count, unsigned int v_idx,
3584 unsigned int txr_count, unsigned int txr_idx,
3585 unsigned int rxr_count, unsigned int rxr_idx)
3587 struct igc_q_vector *q_vector;
3588 struct igc_ring *ring;
3591 /* igc only supports 1 Tx and/or 1 Rx queue per vector */
3592 if (txr_count > 1 || rxr_count > 1)
3595 ring_count = txr_count + rxr_count;
3597 /* allocate q_vector and rings */
3598 q_vector = adapter->q_vector[v_idx];
3600 q_vector = kzalloc(struct_size(q_vector, ring, ring_count),
3603 memset(q_vector, 0, struct_size(q_vector, ring, ring_count));
3607 /* initialize NAPI */
3608 netif_napi_add(adapter->netdev, &q_vector->napi,
3611 /* tie q_vector and adapter together */
3612 adapter->q_vector[v_idx] = q_vector;
3613 q_vector->adapter = adapter;
3615 /* initialize work limits */
3616 q_vector->tx.work_limit = adapter->tx_work_limit;
3618 /* initialize ITR configuration */
3619 q_vector->itr_register = adapter->io_addr + IGC_EITR(0);
3620 q_vector->itr_val = IGC_START_ITR;
3622 /* initialize pointer to rings */
3623 ring = q_vector->ring;
3625 /* initialize ITR */
3627 /* rx or rx/tx vector */
3628 if (!adapter->rx_itr_setting || adapter->rx_itr_setting > 3)
3629 q_vector->itr_val = adapter->rx_itr_setting;
3631 /* tx only vector */
3632 if (!adapter->tx_itr_setting || adapter->tx_itr_setting > 3)
3633 q_vector->itr_val = adapter->tx_itr_setting;
3637 /* assign generic ring traits */
3638 ring->dev = &adapter->pdev->dev;
3639 ring->netdev = adapter->netdev;
3641 /* configure backlink on ring */
3642 ring->q_vector = q_vector;
3644 /* update q_vector Tx values */
3645 igc_add_ring(ring, &q_vector->tx);
3647 /* apply Tx specific ring traits */
3648 ring->count = adapter->tx_ring_count;
3649 ring->queue_index = txr_idx;
3651 /* assign ring to adapter */
3652 adapter->tx_ring[txr_idx] = ring;
3654 /* push pointer to next ring */
3659 /* assign generic ring traits */
3660 ring->dev = &adapter->pdev->dev;
3661 ring->netdev = adapter->netdev;
3663 /* configure backlink on ring */
3664 ring->q_vector = q_vector;
3666 /* update q_vector Rx values */
3667 igc_add_ring(ring, &q_vector->rx);
3669 /* apply Rx specific ring traits */
3670 ring->count = adapter->rx_ring_count;
3671 ring->queue_index = rxr_idx;
3673 /* assign ring to adapter */
3674 adapter->rx_ring[rxr_idx] = ring;
3681 * igc_alloc_q_vectors - Allocate memory for interrupt vectors
3682 * @adapter: board private structure to initialize
3684 * We allocate one q_vector per queue interrupt. If allocation fails we
3687 static int igc_alloc_q_vectors(struct igc_adapter *adapter)
3689 int rxr_remaining = adapter->num_rx_queues;
3690 int txr_remaining = adapter->num_tx_queues;
3691 int rxr_idx = 0, txr_idx = 0, v_idx = 0;
3692 int q_vectors = adapter->num_q_vectors;
3695 if (q_vectors >= (rxr_remaining + txr_remaining)) {
3696 for (; rxr_remaining; v_idx++) {
3697 err = igc_alloc_q_vector(adapter, q_vectors, v_idx,
3703 /* update counts and index */
3709 for (; v_idx < q_vectors; v_idx++) {
3710 int rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - v_idx);
3711 int tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - v_idx);
3713 err = igc_alloc_q_vector(adapter, q_vectors, v_idx,
3714 tqpv, txr_idx, rqpv, rxr_idx);
3719 /* update counts and index */
3720 rxr_remaining -= rqpv;
3721 txr_remaining -= tqpv;
3729 adapter->num_tx_queues = 0;
3730 adapter->num_rx_queues = 0;
3731 adapter->num_q_vectors = 0;
3734 igc_free_q_vector(adapter, v_idx);
3740 * igc_init_interrupt_scheme - initialize interrupts, allocate queues/vectors
3741 * @adapter: Pointer to adapter structure
3742 * @msix: boolean for MSI-X capability
3744 * This function initializes the interrupts and allocates all of the queues.
3746 static int igc_init_interrupt_scheme(struct igc_adapter *adapter, bool msix)
3748 struct net_device *dev = adapter->netdev;
3751 igc_set_interrupt_capability(adapter, msix);
3753 err = igc_alloc_q_vectors(adapter);
3755 netdev_err(dev, "Unable to allocate memory for vectors\n");
3756 goto err_alloc_q_vectors;
3759 igc_cache_ring_register(adapter);
3763 err_alloc_q_vectors:
3764 igc_reset_interrupt_capability(adapter);
3769 * igc_sw_init - Initialize general software structures (struct igc_adapter)
3770 * @adapter: board private structure to initialize
3772 * igc_sw_init initializes the Adapter private data structure.
3773 * Fields are initialized based on PCI device information and
3774 * OS network device settings (MTU size).
3776 static int igc_sw_init(struct igc_adapter *adapter)
3778 struct net_device *netdev = adapter->netdev;
3779 struct pci_dev *pdev = adapter->pdev;
3780 struct igc_hw *hw = &adapter->hw;
3782 pci_read_config_word(pdev, PCI_COMMAND, &hw->bus.pci_cmd_word);
3784 /* set default ring sizes */
3785 adapter->tx_ring_count = IGC_DEFAULT_TXD;
3786 adapter->rx_ring_count = IGC_DEFAULT_RXD;
3788 /* set default ITR values */
3789 adapter->rx_itr_setting = IGC_DEFAULT_ITR;
3790 adapter->tx_itr_setting = IGC_DEFAULT_ITR;
3792 /* set default work limits */
3793 adapter->tx_work_limit = IGC_DEFAULT_TX_WORK;
3795 /* adjust max frame to be at least the size of a standard frame */
3796 adapter->max_frame_size = netdev->mtu + ETH_HLEN + ETH_FCS_LEN +
3798 adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
3800 mutex_init(&adapter->nfc_rule_lock);
3801 INIT_LIST_HEAD(&adapter->nfc_rule_list);
3802 adapter->nfc_rule_count = 0;
3804 spin_lock_init(&adapter->stats64_lock);
3805 /* Assume MSI-X interrupts, will be checked during IRQ allocation */
3806 adapter->flags |= IGC_FLAG_HAS_MSIX;
3808 igc_init_queue_configuration(adapter);
3810 /* This call may decrease the number of queues */
3811 if (igc_init_interrupt_scheme(adapter, true)) {
3812 netdev_err(netdev, "Unable to allocate memory for queues\n");
3816 /* Explicitly disable IRQ since the NIC can be in any state. */
3817 igc_irq_disable(adapter);
3819 set_bit(__IGC_DOWN, &adapter->state);
3825 * igc_up - Open the interface and prepare it to handle traffic
3826 * @adapter: board private structure
3828 void igc_up(struct igc_adapter *adapter)
3830 struct igc_hw *hw = &adapter->hw;
3833 /* hardware has been reset, we need to reload some things */
3834 igc_configure(adapter);
3836 clear_bit(__IGC_DOWN, &adapter->state);
3838 for (i = 0; i < adapter->num_q_vectors; i++)
3839 napi_enable(&adapter->q_vector[i]->napi);
3841 if (adapter->msix_entries)
3842 igc_configure_msix(adapter);
3844 igc_assign_vector(adapter->q_vector[0], 0);
3846 /* Clear any pending interrupts. */
3848 igc_irq_enable(adapter);
3850 netif_tx_start_all_queues(adapter->netdev);
3852 /* start the watchdog. */
3853 hw->mac.get_link_status = true;
3854 schedule_work(&adapter->watchdog_task);
3858 * igc_update_stats - Update the board statistics counters
3859 * @adapter: board private structure
3861 void igc_update_stats(struct igc_adapter *adapter)
3863 struct rtnl_link_stats64 *net_stats = &adapter->stats64;
3864 struct pci_dev *pdev = adapter->pdev;
3865 struct igc_hw *hw = &adapter->hw;
3866 u64 _bytes, _packets;
3872 /* Prevent stats update while adapter is being reset, or if the pci
3873 * connection is down.
3875 if (adapter->link_speed == 0)
3877 if (pci_channel_offline(pdev))
3884 for (i = 0; i < adapter->num_rx_queues; i++) {
3885 struct igc_ring *ring = adapter->rx_ring[i];
3886 u32 rqdpc = rd32(IGC_RQDPC(i));
3888 if (hw->mac.type >= igc_i225)
3889 wr32(IGC_RQDPC(i), 0);
3892 ring->rx_stats.drops += rqdpc;
3893 net_stats->rx_fifo_errors += rqdpc;
3897 start = u64_stats_fetch_begin_irq(&ring->rx_syncp);
3898 _bytes = ring->rx_stats.bytes;
3899 _packets = ring->rx_stats.packets;
3900 } while (u64_stats_fetch_retry_irq(&ring->rx_syncp, start));
3902 packets += _packets;
3905 net_stats->rx_bytes = bytes;
3906 net_stats->rx_packets = packets;
3910 for (i = 0; i < adapter->num_tx_queues; i++) {
3911 struct igc_ring *ring = adapter->tx_ring[i];
3914 start = u64_stats_fetch_begin_irq(&ring->tx_syncp);
3915 _bytes = ring->tx_stats.bytes;
3916 _packets = ring->tx_stats.packets;
3917 } while (u64_stats_fetch_retry_irq(&ring->tx_syncp, start));
3919 packets += _packets;
3921 net_stats->tx_bytes = bytes;
3922 net_stats->tx_packets = packets;
3925 /* read stats registers */
3926 adapter->stats.crcerrs += rd32(IGC_CRCERRS);
3927 adapter->stats.gprc += rd32(IGC_GPRC);
3928 adapter->stats.gorc += rd32(IGC_GORCL);
3929 rd32(IGC_GORCH); /* clear GORCL */
3930 adapter->stats.bprc += rd32(IGC_BPRC);
3931 adapter->stats.mprc += rd32(IGC_MPRC);
3932 adapter->stats.roc += rd32(IGC_ROC);
3934 adapter->stats.prc64 += rd32(IGC_PRC64);
3935 adapter->stats.prc127 += rd32(IGC_PRC127);
3936 adapter->stats.prc255 += rd32(IGC_PRC255);
3937 adapter->stats.prc511 += rd32(IGC_PRC511);
3938 adapter->stats.prc1023 += rd32(IGC_PRC1023);
3939 adapter->stats.prc1522 += rd32(IGC_PRC1522);
3940 adapter->stats.tlpic += rd32(IGC_TLPIC);
3941 adapter->stats.rlpic += rd32(IGC_RLPIC);
3942 adapter->stats.hgptc += rd32(IGC_HGPTC);
3944 mpc = rd32(IGC_MPC);
3945 adapter->stats.mpc += mpc;
3946 net_stats->rx_fifo_errors += mpc;
3947 adapter->stats.scc += rd32(IGC_SCC);
3948 adapter->stats.ecol += rd32(IGC_ECOL);
3949 adapter->stats.mcc += rd32(IGC_MCC);
3950 adapter->stats.latecol += rd32(IGC_LATECOL);
3951 adapter->stats.dc += rd32(IGC_DC);
3952 adapter->stats.rlec += rd32(IGC_RLEC);
3953 adapter->stats.xonrxc += rd32(IGC_XONRXC);
3954 adapter->stats.xontxc += rd32(IGC_XONTXC);
3955 adapter->stats.xoffrxc += rd32(IGC_XOFFRXC);
3956 adapter->stats.xofftxc += rd32(IGC_XOFFTXC);
3957 adapter->stats.fcruc += rd32(IGC_FCRUC);
3958 adapter->stats.gptc += rd32(IGC_GPTC);
3959 adapter->stats.gotc += rd32(IGC_GOTCL);
3960 rd32(IGC_GOTCH); /* clear GOTCL */
3961 adapter->stats.rnbc += rd32(IGC_RNBC);
3962 adapter->stats.ruc += rd32(IGC_RUC);
3963 adapter->stats.rfc += rd32(IGC_RFC);
3964 adapter->stats.rjc += rd32(IGC_RJC);
3965 adapter->stats.tor += rd32(IGC_TORH);
3966 adapter->stats.tot += rd32(IGC_TOTH);
3967 adapter->stats.tpr += rd32(IGC_TPR);
3969 adapter->stats.ptc64 += rd32(IGC_PTC64);
3970 adapter->stats.ptc127 += rd32(IGC_PTC127);
3971 adapter->stats.ptc255 += rd32(IGC_PTC255);
3972 adapter->stats.ptc511 += rd32(IGC_PTC511);
3973 adapter->stats.ptc1023 += rd32(IGC_PTC1023);
3974 adapter->stats.ptc1522 += rd32(IGC_PTC1522);
3976 adapter->stats.mptc += rd32(IGC_MPTC);
3977 adapter->stats.bptc += rd32(IGC_BPTC);
3979 adapter->stats.tpt += rd32(IGC_TPT);
3980 adapter->stats.colc += rd32(IGC_COLC);
3981 adapter->stats.colc += rd32(IGC_RERC);
3983 adapter->stats.algnerrc += rd32(IGC_ALGNERRC);
3985 adapter->stats.tsctc += rd32(IGC_TSCTC);
3987 adapter->stats.iac += rd32(IGC_IAC);
3989 /* Fill out the OS statistics structure */
3990 net_stats->multicast = adapter->stats.mprc;
3991 net_stats->collisions = adapter->stats.colc;
3995 /* RLEC on some newer hardware can be incorrect so build
3996 * our own version based on RUC and ROC
3998 net_stats->rx_errors = adapter->stats.rxerrc +
3999 adapter->stats.crcerrs + adapter->stats.algnerrc +
4000 adapter->stats.ruc + adapter->stats.roc +
4001 adapter->stats.cexterr;
4002 net_stats->rx_length_errors = adapter->stats.ruc +
4004 net_stats->rx_crc_errors = adapter->stats.crcerrs;
4005 net_stats->rx_frame_errors = adapter->stats.algnerrc;
4006 net_stats->rx_missed_errors = adapter->stats.mpc;
4009 net_stats->tx_errors = adapter->stats.ecol +
4010 adapter->stats.latecol;
4011 net_stats->tx_aborted_errors = adapter->stats.ecol;
4012 net_stats->tx_window_errors = adapter->stats.latecol;
4013 net_stats->tx_carrier_errors = adapter->stats.tncrs;
4015 /* Tx Dropped needs to be maintained elsewhere */
4017 /* Management Stats */
4018 adapter->stats.mgptc += rd32(IGC_MGTPTC);
4019 adapter->stats.mgprc += rd32(IGC_MGTPRC);
4020 adapter->stats.mgpdc += rd32(IGC_MGTPDC);
4024 * igc_down - Close the interface
4025 * @adapter: board private structure
4027 void igc_down(struct igc_adapter *adapter)
4029 struct net_device *netdev = adapter->netdev;
4030 struct igc_hw *hw = &adapter->hw;
4034 set_bit(__IGC_DOWN, &adapter->state);
4036 igc_ptp_suspend(adapter);
4038 /* disable receives in the hardware */
4039 rctl = rd32(IGC_RCTL);
4040 wr32(IGC_RCTL, rctl & ~IGC_RCTL_EN);
4041 /* flush and sleep below */
4043 /* set trans_start so we don't get spurious watchdogs during reset */
4044 netif_trans_update(netdev);
4046 netif_carrier_off(netdev);
4047 netif_tx_stop_all_queues(netdev);
4049 /* disable transmits in the hardware */
4050 tctl = rd32(IGC_TCTL);
4051 tctl &= ~IGC_TCTL_EN;
4052 wr32(IGC_TCTL, tctl);
4053 /* flush both disables and wait for them to finish */
4055 usleep_range(10000, 20000);
4057 igc_irq_disable(adapter);
4059 adapter->flags &= ~IGC_FLAG_NEED_LINK_UPDATE;
4061 for (i = 0; i < adapter->num_q_vectors; i++) {
4062 if (adapter->q_vector[i]) {
4063 napi_synchronize(&adapter->q_vector[i]->napi);
4064 napi_disable(&adapter->q_vector[i]->napi);
4068 del_timer_sync(&adapter->watchdog_timer);
4069 del_timer_sync(&adapter->phy_info_timer);
4071 /* record the stats before reset*/
4072 spin_lock(&adapter->stats64_lock);
4073 igc_update_stats(adapter);
4074 spin_unlock(&adapter->stats64_lock);
4076 adapter->link_speed = 0;
4077 adapter->link_duplex = 0;
4079 if (!pci_channel_offline(adapter->pdev))
4082 /* clear VLAN promisc flag so VFTA will be updated if necessary */
4083 adapter->flags &= ~IGC_FLAG_VLAN_PROMISC;
4085 igc_clean_all_tx_rings(adapter);
4086 igc_clean_all_rx_rings(adapter);
4089 void igc_reinit_locked(struct igc_adapter *adapter)
4091 while (test_and_set_bit(__IGC_RESETTING, &adapter->state))
4092 usleep_range(1000, 2000);
4095 clear_bit(__IGC_RESETTING, &adapter->state);
4098 static void igc_reset_task(struct work_struct *work)
4100 struct igc_adapter *adapter;
4102 adapter = container_of(work, struct igc_adapter, reset_task);
4105 /* If we're already down or resetting, just bail */
4106 if (test_bit(__IGC_DOWN, &adapter->state) ||
4107 test_bit(__IGC_RESETTING, &adapter->state)) {
4112 igc_rings_dump(adapter);
4113 igc_regs_dump(adapter);
4114 netdev_err(adapter->netdev, "Reset adapter\n");
4115 igc_reinit_locked(adapter);
4120 * igc_change_mtu - Change the Maximum Transfer Unit
4121 * @netdev: network interface device structure
4122 * @new_mtu: new value for maximum frame size
4124 * Returns 0 on success, negative on failure
4126 static int igc_change_mtu(struct net_device *netdev, int new_mtu)
4128 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
4129 struct igc_adapter *adapter = netdev_priv(netdev);
4131 if (igc_xdp_is_enabled(adapter) && new_mtu > ETH_DATA_LEN) {
4132 netdev_dbg(netdev, "Jumbo frames not supported with XDP");
4136 /* adjust max frame to be at least the size of a standard frame */
4137 if (max_frame < (ETH_FRAME_LEN + ETH_FCS_LEN))
4138 max_frame = ETH_FRAME_LEN + ETH_FCS_LEN;
4140 while (test_and_set_bit(__IGC_RESETTING, &adapter->state))
4141 usleep_range(1000, 2000);
4143 /* igc_down has a dependency on max_frame_size */
4144 adapter->max_frame_size = max_frame;
4146 if (netif_running(netdev))
4149 netdev_dbg(netdev, "changing MTU from %d to %d\n", netdev->mtu, new_mtu);
4150 netdev->mtu = new_mtu;
4152 if (netif_running(netdev))
4157 clear_bit(__IGC_RESETTING, &adapter->state);
4163 * igc_get_stats64 - Get System Network Statistics
4164 * @netdev: network interface device structure
4165 * @stats: rtnl_link_stats64 pointer
4167 * Returns the address of the device statistics structure.
4168 * The statistics are updated here and also from the timer callback.
4170 static void igc_get_stats64(struct net_device *netdev,
4171 struct rtnl_link_stats64 *stats)
4173 struct igc_adapter *adapter = netdev_priv(netdev);
4175 spin_lock(&adapter->stats64_lock);
4176 if (!test_bit(__IGC_RESETTING, &adapter->state))
4177 igc_update_stats(adapter);
4178 memcpy(stats, &adapter->stats64, sizeof(*stats));
4179 spin_unlock(&adapter->stats64_lock);
4182 static netdev_features_t igc_fix_features(struct net_device *netdev,
4183 netdev_features_t features)
4185 /* Since there is no support for separate Rx/Tx vlan accel
4186 * enable/disable make sure Tx flag is always in same state as Rx.
4188 if (features & NETIF_F_HW_VLAN_CTAG_RX)
4189 features |= NETIF_F_HW_VLAN_CTAG_TX;
4191 features &= ~NETIF_F_HW_VLAN_CTAG_TX;
4196 static int igc_set_features(struct net_device *netdev,
4197 netdev_features_t features)
4199 netdev_features_t changed = netdev->features ^ features;
4200 struct igc_adapter *adapter = netdev_priv(netdev);
4202 /* Add VLAN support */
4203 if (!(changed & (NETIF_F_RXALL | NETIF_F_NTUPLE)))
4206 if (!(features & NETIF_F_NTUPLE))
4207 igc_flush_nfc_rules(adapter);
4209 netdev->features = features;
4211 if (netif_running(netdev))
4212 igc_reinit_locked(adapter);
4219 static netdev_features_t
4220 igc_features_check(struct sk_buff *skb, struct net_device *dev,
4221 netdev_features_t features)
4223 unsigned int network_hdr_len, mac_hdr_len;
4225 /* Make certain the headers can be described by a context descriptor */
4226 mac_hdr_len = skb_network_header(skb) - skb->data;
4227 if (unlikely(mac_hdr_len > IGC_MAX_MAC_HDR_LEN))
4228 return features & ~(NETIF_F_HW_CSUM |
4230 NETIF_F_HW_VLAN_CTAG_TX |
4234 network_hdr_len = skb_checksum_start(skb) - skb_network_header(skb);
4235 if (unlikely(network_hdr_len > IGC_MAX_NETWORK_HDR_LEN))
4236 return features & ~(NETIF_F_HW_CSUM |
4241 /* We can only support IPv4 TSO in tunnels if we can mangle the
4242 * inner IP ID field, so strip TSO if MANGLEID is not supported.
4244 if (skb->encapsulation && !(features & NETIF_F_TSO_MANGLEID))
4245 features &= ~NETIF_F_TSO;
4250 static void igc_tsync_interrupt(struct igc_adapter *adapter)
4252 u32 ack, tsauxc, sec, nsec, tsicr;
4253 struct igc_hw *hw = &adapter->hw;
4254 struct ptp_clock_event event;
4255 struct timespec64 ts;
4257 tsicr = rd32(IGC_TSICR);
4260 if (tsicr & IGC_TSICR_SYS_WRAP) {
4261 event.type = PTP_CLOCK_PPS;
4262 if (adapter->ptp_caps.pps)
4263 ptp_clock_event(adapter->ptp_clock, &event);
4264 ack |= IGC_TSICR_SYS_WRAP;
4267 if (tsicr & IGC_TSICR_TXTS) {
4268 /* retrieve hardware timestamp */
4269 schedule_work(&adapter->ptp_tx_work);
4270 ack |= IGC_TSICR_TXTS;
4273 if (tsicr & IGC_TSICR_TT0) {
4274 spin_lock(&adapter->tmreg_lock);
4275 ts = timespec64_add(adapter->perout[0].start,
4276 adapter->perout[0].period);
4277 wr32(IGC_TRGTTIML0, ts.tv_nsec | IGC_TT_IO_TIMER_SEL_SYSTIM0);
4278 wr32(IGC_TRGTTIMH0, (u32)ts.tv_sec);
4279 tsauxc = rd32(IGC_TSAUXC);
4280 tsauxc |= IGC_TSAUXC_EN_TT0;
4281 wr32(IGC_TSAUXC, tsauxc);
4282 adapter->perout[0].start = ts;
4283 spin_unlock(&adapter->tmreg_lock);
4284 ack |= IGC_TSICR_TT0;
4287 if (tsicr & IGC_TSICR_TT1) {
4288 spin_lock(&adapter->tmreg_lock);
4289 ts = timespec64_add(adapter->perout[1].start,
4290 adapter->perout[1].period);
4291 wr32(IGC_TRGTTIML1, ts.tv_nsec | IGC_TT_IO_TIMER_SEL_SYSTIM0);
4292 wr32(IGC_TRGTTIMH1, (u32)ts.tv_sec);
4293 tsauxc = rd32(IGC_TSAUXC);
4294 tsauxc |= IGC_TSAUXC_EN_TT1;
4295 wr32(IGC_TSAUXC, tsauxc);
4296 adapter->perout[1].start = ts;
4297 spin_unlock(&adapter->tmreg_lock);
4298 ack |= IGC_TSICR_TT1;
4301 if (tsicr & IGC_TSICR_AUTT0) {
4302 nsec = rd32(IGC_AUXSTMPL0);
4303 sec = rd32(IGC_AUXSTMPH0);
4304 event.type = PTP_CLOCK_EXTTS;
4306 event.timestamp = sec * NSEC_PER_SEC + nsec;
4307 ptp_clock_event(adapter->ptp_clock, &event);
4308 ack |= IGC_TSICR_AUTT0;
4311 if (tsicr & IGC_TSICR_AUTT1) {
4312 nsec = rd32(IGC_AUXSTMPL1);
4313 sec = rd32(IGC_AUXSTMPH1);
4314 event.type = PTP_CLOCK_EXTTS;
4316 event.timestamp = sec * NSEC_PER_SEC + nsec;
4317 ptp_clock_event(adapter->ptp_clock, &event);
4318 ack |= IGC_TSICR_AUTT1;
4321 /* acknowledge the interrupts */
4322 wr32(IGC_TSICR, ack);
4326 * igc_msix_other - msix other interrupt handler
4327 * @irq: interrupt number
4328 * @data: pointer to a q_vector
4330 static irqreturn_t igc_msix_other(int irq, void *data)
4332 struct igc_adapter *adapter = data;
4333 struct igc_hw *hw = &adapter->hw;
4334 u32 icr = rd32(IGC_ICR);
4336 /* reading ICR causes bit 31 of EICR to be cleared */
4337 if (icr & IGC_ICR_DRSTA)
4338 schedule_work(&adapter->reset_task);
4340 if (icr & IGC_ICR_DOUTSYNC) {
4341 /* HW is reporting DMA is out of sync */
4342 adapter->stats.doosync++;
4345 if (icr & IGC_ICR_LSC) {
4346 hw->mac.get_link_status = true;
4347 /* guard against interrupt when we're going down */
4348 if (!test_bit(__IGC_DOWN, &adapter->state))
4349 mod_timer(&adapter->watchdog_timer, jiffies + 1);
4352 if (icr & IGC_ICR_TS)
4353 igc_tsync_interrupt(adapter);
4355 wr32(IGC_EIMS, adapter->eims_other);
4360 static void igc_write_itr(struct igc_q_vector *q_vector)
4362 u32 itr_val = q_vector->itr_val & IGC_QVECTOR_MASK;
4364 if (!q_vector->set_itr)
4368 itr_val = IGC_ITR_VAL_MASK;
4370 itr_val |= IGC_EITR_CNT_IGNR;
4372 writel(itr_val, q_vector->itr_register);
4373 q_vector->set_itr = 0;
4376 static irqreturn_t igc_msix_ring(int irq, void *data)
4378 struct igc_q_vector *q_vector = data;
4380 /* Write the ITR value calculated from the previous interrupt. */
4381 igc_write_itr(q_vector);
4383 napi_schedule(&q_vector->napi);
4389 * igc_request_msix - Initialize MSI-X interrupts
4390 * @adapter: Pointer to adapter structure
4392 * igc_request_msix allocates MSI-X vectors and requests interrupts from the
4395 static int igc_request_msix(struct igc_adapter *adapter)
4397 int i = 0, err = 0, vector = 0, free_vector = 0;
4398 struct net_device *netdev = adapter->netdev;
4400 err = request_irq(adapter->msix_entries[vector].vector,
4401 &igc_msix_other, 0, netdev->name, adapter);
4405 for (i = 0; i < adapter->num_q_vectors; i++) {
4406 struct igc_q_vector *q_vector = adapter->q_vector[i];
4410 q_vector->itr_register = adapter->io_addr + IGC_EITR(vector);
4412 if (q_vector->rx.ring && q_vector->tx.ring)
4413 sprintf(q_vector->name, "%s-TxRx-%u", netdev->name,
4414 q_vector->rx.ring->queue_index);
4415 else if (q_vector->tx.ring)
4416 sprintf(q_vector->name, "%s-tx-%u", netdev->name,
4417 q_vector->tx.ring->queue_index);
4418 else if (q_vector->rx.ring)
4419 sprintf(q_vector->name, "%s-rx-%u", netdev->name,
4420 q_vector->rx.ring->queue_index);
4422 sprintf(q_vector->name, "%s-unused", netdev->name);
4424 err = request_irq(adapter->msix_entries[vector].vector,
4425 igc_msix_ring, 0, q_vector->name,
4431 igc_configure_msix(adapter);
4435 /* free already assigned IRQs */
4436 free_irq(adapter->msix_entries[free_vector++].vector, adapter);
4439 for (i = 0; i < vector; i++) {
4440 free_irq(adapter->msix_entries[free_vector++].vector,
4441 adapter->q_vector[i]);
4448 * igc_clear_interrupt_scheme - reset the device to a state of no interrupts
4449 * @adapter: Pointer to adapter structure
4451 * This function resets the device so that it has 0 rx queues, tx queues, and
4452 * MSI-X interrupts allocated.
4454 static void igc_clear_interrupt_scheme(struct igc_adapter *adapter)
4456 igc_free_q_vectors(adapter);
4457 igc_reset_interrupt_capability(adapter);
4460 /* Need to wait a few seconds after link up to get diagnostic information from
4463 static void igc_update_phy_info(struct timer_list *t)
4465 struct igc_adapter *adapter = from_timer(adapter, t, phy_info_timer);
4467 igc_get_phy_info(&adapter->hw);
4471 * igc_has_link - check shared code for link and determine up/down
4472 * @adapter: pointer to driver private info
4474 bool igc_has_link(struct igc_adapter *adapter)
4476 struct igc_hw *hw = &adapter->hw;
4477 bool link_active = false;
4479 /* get_link_status is set on LSC (link status) interrupt or
4480 * rx sequence error interrupt. get_link_status will stay
4481 * false until the igc_check_for_link establishes link
4482 * for copper adapters ONLY
4484 switch (hw->phy.media_type) {
4485 case igc_media_type_copper:
4486 if (!hw->mac.get_link_status)
4488 hw->mac.ops.check_for_link(hw);
4489 link_active = !hw->mac.get_link_status;
4492 case igc_media_type_unknown:
4496 if (hw->mac.type == igc_i225 &&
4497 hw->phy.id == I225_I_PHY_ID) {
4498 if (!netif_carrier_ok(adapter->netdev)) {
4499 adapter->flags &= ~IGC_FLAG_NEED_LINK_UPDATE;
4500 } else if (!(adapter->flags & IGC_FLAG_NEED_LINK_UPDATE)) {
4501 adapter->flags |= IGC_FLAG_NEED_LINK_UPDATE;
4502 adapter->link_check_timeout = jiffies;
4510 * igc_watchdog - Timer Call-back
4511 * @t: timer for the watchdog
4513 static void igc_watchdog(struct timer_list *t)
4515 struct igc_adapter *adapter = from_timer(adapter, t, watchdog_timer);
4516 /* Do the rest outside of interrupt context */
4517 schedule_work(&adapter->watchdog_task);
4520 static void igc_watchdog_task(struct work_struct *work)
4522 struct igc_adapter *adapter = container_of(work,
4525 struct net_device *netdev = adapter->netdev;
4526 struct igc_hw *hw = &adapter->hw;
4527 struct igc_phy_info *phy = &hw->phy;
4528 u16 phy_data, retry_count = 20;
4532 link = igc_has_link(adapter);
4534 if (adapter->flags & IGC_FLAG_NEED_LINK_UPDATE) {
4535 if (time_after(jiffies, (adapter->link_check_timeout + HZ)))
4536 adapter->flags &= ~IGC_FLAG_NEED_LINK_UPDATE;
4542 /* Cancel scheduled suspend requests. */
4543 pm_runtime_resume(netdev->dev.parent);
4545 if (!netif_carrier_ok(netdev)) {
4548 hw->mac.ops.get_speed_and_duplex(hw,
4549 &adapter->link_speed,
4550 &adapter->link_duplex);
4552 ctrl = rd32(IGC_CTRL);
4553 /* Link status message must follow this format */
4555 "NIC Link is Up %d Mbps %s Duplex, Flow Control: %s\n",
4556 adapter->link_speed,
4557 adapter->link_duplex == FULL_DUPLEX ?
4559 (ctrl & IGC_CTRL_TFCE) &&
4560 (ctrl & IGC_CTRL_RFCE) ? "RX/TX" :
4561 (ctrl & IGC_CTRL_RFCE) ? "RX" :
4562 (ctrl & IGC_CTRL_TFCE) ? "TX" : "None");
4564 /* disable EEE if enabled */
4565 if ((adapter->flags & IGC_FLAG_EEE) &&
4566 adapter->link_duplex == HALF_DUPLEX) {
4568 "EEE Disabled: unsupported at half duplex. Re-enable using ethtool when at full duplex\n");
4569 adapter->hw.dev_spec._base.eee_enable = false;
4570 adapter->flags &= ~IGC_FLAG_EEE;
4573 /* check if SmartSpeed worked */
4574 igc_check_downshift(hw);
4575 if (phy->speed_downgraded)
4576 netdev_warn(netdev, "Link Speed was downgraded by SmartSpeed\n");
4578 /* adjust timeout factor according to speed/duplex */
4579 adapter->tx_timeout_factor = 1;
4580 switch (adapter->link_speed) {
4582 adapter->tx_timeout_factor = 14;
4585 /* maybe add some timeout factor ? */
4589 if (adapter->link_speed != SPEED_1000)
4592 /* wait for Remote receiver status OK */
4594 if (!igc_read_phy_reg(hw, PHY_1000T_STATUS,
4596 if (!(phy_data & SR_1000T_REMOTE_RX_STATUS) &&
4600 goto retry_read_status;
4601 } else if (!retry_count) {
4602 netdev_err(netdev, "exceed max 2 second\n");
4605 netdev_err(netdev, "read 1000Base-T Status Reg\n");
4608 netif_carrier_on(netdev);
4610 /* link state has changed, schedule phy info update */
4611 if (!test_bit(__IGC_DOWN, &adapter->state))
4612 mod_timer(&adapter->phy_info_timer,
4613 round_jiffies(jiffies + 2 * HZ));
4616 if (netif_carrier_ok(netdev)) {
4617 adapter->link_speed = 0;
4618 adapter->link_duplex = 0;
4620 /* Links status message must follow this format */
4621 netdev_info(netdev, "NIC Link is Down\n");
4622 netif_carrier_off(netdev);
4624 /* link state has changed, schedule phy info update */
4625 if (!test_bit(__IGC_DOWN, &adapter->state))
4626 mod_timer(&adapter->phy_info_timer,
4627 round_jiffies(jiffies + 2 * HZ));
4629 /* link is down, time to check for alternate media */
4630 if (adapter->flags & IGC_FLAG_MAS_ENABLE) {
4631 if (adapter->flags & IGC_FLAG_MEDIA_RESET) {
4632 schedule_work(&adapter->reset_task);
4633 /* return immediately */
4637 pm_schedule_suspend(netdev->dev.parent,
4640 /* also check for alternate media here */
4641 } else if (!netif_carrier_ok(netdev) &&
4642 (adapter->flags & IGC_FLAG_MAS_ENABLE)) {
4643 if (adapter->flags & IGC_FLAG_MEDIA_RESET) {
4644 schedule_work(&adapter->reset_task);
4645 /* return immediately */
4651 spin_lock(&adapter->stats64_lock);
4652 igc_update_stats(adapter);
4653 spin_unlock(&adapter->stats64_lock);
4655 for (i = 0; i < adapter->num_tx_queues; i++) {
4656 struct igc_ring *tx_ring = adapter->tx_ring[i];
4658 if (!netif_carrier_ok(netdev)) {
4659 /* We've lost link, so the controller stops DMA,
4660 * but we've got queued Tx work that's never going
4661 * to get done, so reset controller to flush Tx.
4662 * (Do the reset outside of interrupt context).
4664 if (igc_desc_unused(tx_ring) + 1 < tx_ring->count) {
4665 adapter->tx_timeout_count++;
4666 schedule_work(&adapter->reset_task);
4667 /* return immediately since reset is imminent */
4672 /* Force detection of hung controller every watchdog period */
4673 set_bit(IGC_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags);
4676 /* Cause software interrupt to ensure Rx ring is cleaned */
4677 if (adapter->flags & IGC_FLAG_HAS_MSIX) {
4680 for (i = 0; i < adapter->num_q_vectors; i++)
4681 eics |= adapter->q_vector[i]->eims_value;
4682 wr32(IGC_EICS, eics);
4684 wr32(IGC_ICS, IGC_ICS_RXDMT0);
4687 igc_ptp_tx_hang(adapter);
4689 /* Reset the timer */
4690 if (!test_bit(__IGC_DOWN, &adapter->state)) {
4691 if (adapter->flags & IGC_FLAG_NEED_LINK_UPDATE)
4692 mod_timer(&adapter->watchdog_timer,
4693 round_jiffies(jiffies + HZ));
4695 mod_timer(&adapter->watchdog_timer,
4696 round_jiffies(jiffies + 2 * HZ));
4701 * igc_intr_msi - Interrupt Handler
4702 * @irq: interrupt number
4703 * @data: pointer to a network interface device structure
4705 static irqreturn_t igc_intr_msi(int irq, void *data)
4707 struct igc_adapter *adapter = data;
4708 struct igc_q_vector *q_vector = adapter->q_vector[0];
4709 struct igc_hw *hw = &adapter->hw;
4710 /* read ICR disables interrupts using IAM */
4711 u32 icr = rd32(IGC_ICR);
4713 igc_write_itr(q_vector);
4715 if (icr & IGC_ICR_DRSTA)
4716 schedule_work(&adapter->reset_task);
4718 if (icr & IGC_ICR_DOUTSYNC) {
4719 /* HW is reporting DMA is out of sync */
4720 adapter->stats.doosync++;
4723 if (icr & (IGC_ICR_RXSEQ | IGC_ICR_LSC)) {
4724 hw->mac.get_link_status = true;
4725 if (!test_bit(__IGC_DOWN, &adapter->state))
4726 mod_timer(&adapter->watchdog_timer, jiffies + 1);
4729 napi_schedule(&q_vector->napi);
4735 * igc_intr - Legacy Interrupt Handler
4736 * @irq: interrupt number
4737 * @data: pointer to a network interface device structure
4739 static irqreturn_t igc_intr(int irq, void *data)
4741 struct igc_adapter *adapter = data;
4742 struct igc_q_vector *q_vector = adapter->q_vector[0];
4743 struct igc_hw *hw = &adapter->hw;
4744 /* Interrupt Auto-Mask...upon reading ICR, interrupts are masked. No
4745 * need for the IMC write
4747 u32 icr = rd32(IGC_ICR);
4749 /* IMS will not auto-mask if INT_ASSERTED is not set, and if it is
4750 * not set, then the adapter didn't send an interrupt
4752 if (!(icr & IGC_ICR_INT_ASSERTED))
4755 igc_write_itr(q_vector);
4757 if (icr & IGC_ICR_DRSTA)
4758 schedule_work(&adapter->reset_task);
4760 if (icr & IGC_ICR_DOUTSYNC) {
4761 /* HW is reporting DMA is out of sync */
4762 adapter->stats.doosync++;
4765 if (icr & (IGC_ICR_RXSEQ | IGC_ICR_LSC)) {
4766 hw->mac.get_link_status = true;
4767 /* guard against interrupt when we're going down */
4768 if (!test_bit(__IGC_DOWN, &adapter->state))
4769 mod_timer(&adapter->watchdog_timer, jiffies + 1);
4772 napi_schedule(&q_vector->napi);
4777 static void igc_free_irq(struct igc_adapter *adapter)
4779 if (adapter->msix_entries) {
4782 free_irq(adapter->msix_entries[vector++].vector, adapter);
4784 for (i = 0; i < adapter->num_q_vectors; i++)
4785 free_irq(adapter->msix_entries[vector++].vector,
4786 adapter->q_vector[i]);
4788 free_irq(adapter->pdev->irq, adapter);
4793 * igc_request_irq - initialize interrupts
4794 * @adapter: Pointer to adapter structure
4796 * Attempts to configure interrupts using the best available
4797 * capabilities of the hardware and kernel.
4799 static int igc_request_irq(struct igc_adapter *adapter)
4801 struct net_device *netdev = adapter->netdev;
4802 struct pci_dev *pdev = adapter->pdev;
4805 if (adapter->flags & IGC_FLAG_HAS_MSIX) {
4806 err = igc_request_msix(adapter);
4809 /* fall back to MSI */
4810 igc_free_all_tx_resources(adapter);
4811 igc_free_all_rx_resources(adapter);
4813 igc_clear_interrupt_scheme(adapter);
4814 err = igc_init_interrupt_scheme(adapter, false);
4817 igc_setup_all_tx_resources(adapter);
4818 igc_setup_all_rx_resources(adapter);
4819 igc_configure(adapter);
4822 igc_assign_vector(adapter->q_vector[0], 0);
4824 if (adapter->flags & IGC_FLAG_HAS_MSI) {
4825 err = request_irq(pdev->irq, &igc_intr_msi, 0,
4826 netdev->name, adapter);
4830 /* fall back to legacy interrupts */
4831 igc_reset_interrupt_capability(adapter);
4832 adapter->flags &= ~IGC_FLAG_HAS_MSI;
4835 err = request_irq(pdev->irq, &igc_intr, IRQF_SHARED,
4836 netdev->name, adapter);
4839 netdev_err(netdev, "Error %d getting interrupt\n", err);
4846 * __igc_open - Called when a network interface is made active
4847 * @netdev: network interface device structure
4848 * @resuming: boolean indicating if the device is resuming
4850 * Returns 0 on success, negative value on failure
4852 * The open entry point is called when a network interface is made
4853 * active by the system (IFF_UP). At this point all resources needed
4854 * for transmit and receive operations are allocated, the interrupt
4855 * handler is registered with the OS, the watchdog timer is started,
4856 * and the stack is notified that the interface is ready.
4858 static int __igc_open(struct net_device *netdev, bool resuming)
4860 struct igc_adapter *adapter = netdev_priv(netdev);
4861 struct pci_dev *pdev = adapter->pdev;
4862 struct igc_hw *hw = &adapter->hw;
4866 /* disallow open during test */
4868 if (test_bit(__IGC_TESTING, &adapter->state)) {
4874 pm_runtime_get_sync(&pdev->dev);
4876 netif_carrier_off(netdev);
4878 /* allocate transmit descriptors */
4879 err = igc_setup_all_tx_resources(adapter);
4883 /* allocate receive descriptors */
4884 err = igc_setup_all_rx_resources(adapter);
4888 igc_power_up_link(adapter);
4890 igc_configure(adapter);
4892 err = igc_request_irq(adapter);
4896 /* Notify the stack of the actual queue counts. */
4897 err = netif_set_real_num_tx_queues(netdev, adapter->num_tx_queues);
4899 goto err_set_queues;
4901 err = netif_set_real_num_rx_queues(netdev, adapter->num_rx_queues);
4903 goto err_set_queues;
4905 clear_bit(__IGC_DOWN, &adapter->state);
4907 for (i = 0; i < adapter->num_q_vectors; i++)
4908 napi_enable(&adapter->q_vector[i]->napi);
4910 /* Clear any pending interrupts. */
4912 igc_irq_enable(adapter);
4915 pm_runtime_put(&pdev->dev);
4917 netif_tx_start_all_queues(netdev);
4919 /* start the watchdog. */
4920 hw->mac.get_link_status = true;
4921 schedule_work(&adapter->watchdog_task);
4926 igc_free_irq(adapter);
4928 igc_release_hw_control(adapter);
4929 igc_power_down_phy_copper_base(&adapter->hw);
4930 igc_free_all_rx_resources(adapter);
4932 igc_free_all_tx_resources(adapter);
4936 pm_runtime_put(&pdev->dev);
4941 int igc_open(struct net_device *netdev)
4943 return __igc_open(netdev, false);
4947 * __igc_close - Disables a network interface
4948 * @netdev: network interface device structure
4949 * @suspending: boolean indicating the device is suspending
4951 * Returns 0, this is not allowed to fail
4953 * The close entry point is called when an interface is de-activated
4954 * by the OS. The hardware is still under the driver's control, but
4955 * needs to be disabled. A global MAC reset is issued to stop the
4956 * hardware, and all transmit and receive resources are freed.
4958 static int __igc_close(struct net_device *netdev, bool suspending)
4960 struct igc_adapter *adapter = netdev_priv(netdev);
4961 struct pci_dev *pdev = adapter->pdev;
4963 WARN_ON(test_bit(__IGC_RESETTING, &adapter->state));
4966 pm_runtime_get_sync(&pdev->dev);
4970 igc_release_hw_control(adapter);
4972 igc_free_irq(adapter);
4974 igc_free_all_tx_resources(adapter);
4975 igc_free_all_rx_resources(adapter);
4978 pm_runtime_put_sync(&pdev->dev);
4983 int igc_close(struct net_device *netdev)
4985 if (netif_device_present(netdev) || netdev->dismantle)
4986 return __igc_close(netdev, false);
4991 * igc_ioctl - Access the hwtstamp interface
4992 * @netdev: network interface device structure
4993 * @ifr: interface request data
4994 * @cmd: ioctl command
4996 static int igc_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
5000 return igc_ptp_get_ts_config(netdev, ifr);
5002 return igc_ptp_set_ts_config(netdev, ifr);
5008 static int igc_save_launchtime_params(struct igc_adapter *adapter, int queue,
5011 struct igc_ring *ring;
5014 if (queue < 0 || queue >= adapter->num_tx_queues)
5017 ring = adapter->tx_ring[queue];
5018 ring->launchtime_enable = enable;
5020 if (adapter->base_time)
5023 adapter->cycle_time = NSEC_PER_SEC;
5025 for (i = 0; i < adapter->num_tx_queues; i++) {
5026 ring = adapter->tx_ring[i];
5027 ring->start_time = 0;
5028 ring->end_time = NSEC_PER_SEC;
5034 static bool is_base_time_past(ktime_t base_time, const struct timespec64 *now)
5036 struct timespec64 b;
5038 b = ktime_to_timespec64(base_time);
5040 return timespec64_compare(now, &b) > 0;
5043 static bool validate_schedule(struct igc_adapter *adapter,
5044 const struct tc_taprio_qopt_offload *qopt)
5046 int queue_uses[IGC_MAX_TX_QUEUES] = { };
5047 struct timespec64 now;
5050 if (qopt->cycle_time_extension)
5053 igc_ptp_read(adapter, &now);
5055 /* If we program the controller's BASET registers with a time
5056 * in the future, it will hold all the packets until that
5057 * time, causing a lot of TX Hangs, so to avoid that, we
5058 * reject schedules that would start in the future.
5060 if (!is_base_time_past(qopt->base_time, &now))
5063 for (n = 0; n < qopt->num_entries; n++) {
5064 const struct tc_taprio_sched_entry *e;
5067 e = &qopt->entries[n];
5069 /* i225 only supports "global" frame preemption
5072 if (e->command != TC_TAPRIO_CMD_SET_GATES)
5075 for (i = 0; i < IGC_MAX_TX_QUEUES; i++) {
5076 if (e->gate_mask & BIT(i))
5079 if (queue_uses[i] > 1)
5087 static int igc_tsn_enable_launchtime(struct igc_adapter *adapter,
5088 struct tc_etf_qopt_offload *qopt)
5090 struct igc_hw *hw = &adapter->hw;
5093 if (hw->mac.type != igc_i225)
5096 err = igc_save_launchtime_params(adapter, qopt->queue, qopt->enable);
5100 return igc_tsn_offload_apply(adapter);
5103 static int igc_save_qbv_schedule(struct igc_adapter *adapter,
5104 struct tc_taprio_qopt_offload *qopt)
5106 u32 start_time = 0, end_time = 0;
5109 if (!qopt->enable) {
5110 adapter->base_time = 0;
5114 if (adapter->base_time)
5117 if (!validate_schedule(adapter, qopt))
5120 adapter->cycle_time = qopt->cycle_time;
5121 adapter->base_time = qopt->base_time;
5123 /* FIXME: be a little smarter about cases when the gate for a
5124 * queue stays open for more than one entry.
5126 for (n = 0; n < qopt->num_entries; n++) {
5127 struct tc_taprio_sched_entry *e = &qopt->entries[n];
5130 end_time += e->interval;
5132 for (i = 0; i < IGC_MAX_TX_QUEUES; i++) {
5133 struct igc_ring *ring = adapter->tx_ring[i];
5135 if (!(e->gate_mask & BIT(i)))
5138 ring->start_time = start_time;
5139 ring->end_time = end_time;
5142 start_time += e->interval;
5148 static int igc_tsn_enable_qbv_scheduling(struct igc_adapter *adapter,
5149 struct tc_taprio_qopt_offload *qopt)
5151 struct igc_hw *hw = &adapter->hw;
5154 if (hw->mac.type != igc_i225)
5157 err = igc_save_qbv_schedule(adapter, qopt);
5161 return igc_tsn_offload_apply(adapter);
5164 static int igc_setup_tc(struct net_device *dev, enum tc_setup_type type,
5167 struct igc_adapter *adapter = netdev_priv(dev);
5170 case TC_SETUP_QDISC_TAPRIO:
5171 return igc_tsn_enable_qbv_scheduling(adapter, type_data);
5173 case TC_SETUP_QDISC_ETF:
5174 return igc_tsn_enable_launchtime(adapter, type_data);
5181 static int igc_bpf(struct net_device *dev, struct netdev_bpf *bpf)
5183 struct igc_adapter *adapter = netdev_priv(dev);
5185 switch (bpf->command) {
5186 case XDP_SETUP_PROG:
5187 return igc_xdp_set_prog(adapter, bpf->prog, bpf->extack);
5193 static int igc_xdp_xmit(struct net_device *dev, int num_frames,
5194 struct xdp_frame **frames, u32 flags)
5196 struct igc_adapter *adapter = netdev_priv(dev);
5197 int cpu = smp_processor_id();
5198 struct netdev_queue *nq;
5199 struct igc_ring *ring;
5202 if (unlikely(test_bit(__IGC_DOWN, &adapter->state)))
5205 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
5208 ring = igc_xdp_get_tx_ring(adapter, cpu);
5209 nq = txring_txq(ring);
5211 __netif_tx_lock(nq, cpu);
5214 for (i = 0; i < num_frames; i++) {
5216 struct xdp_frame *xdpf = frames[i];
5218 err = igc_xdp_init_tx_descriptor(ring, xdpf);
5220 xdp_return_frame_rx_napi(xdpf);
5225 if (flags & XDP_XMIT_FLUSH)
5226 igc_flush_tx_descriptors(ring);
5228 __netif_tx_unlock(nq);
5230 return num_frames - drops;
5233 static const struct net_device_ops igc_netdev_ops = {
5234 .ndo_open = igc_open,
5235 .ndo_stop = igc_close,
5236 .ndo_start_xmit = igc_xmit_frame,
5237 .ndo_set_rx_mode = igc_set_rx_mode,
5238 .ndo_set_mac_address = igc_set_mac,
5239 .ndo_change_mtu = igc_change_mtu,
5240 .ndo_get_stats64 = igc_get_stats64,
5241 .ndo_fix_features = igc_fix_features,
5242 .ndo_set_features = igc_set_features,
5243 .ndo_features_check = igc_features_check,
5244 .ndo_do_ioctl = igc_ioctl,
5245 .ndo_setup_tc = igc_setup_tc,
5247 .ndo_xdp_xmit = igc_xdp_xmit,
5250 /* PCIe configuration access */
5251 void igc_read_pci_cfg(struct igc_hw *hw, u32 reg, u16 *value)
5253 struct igc_adapter *adapter = hw->back;
5255 pci_read_config_word(adapter->pdev, reg, value);
5258 void igc_write_pci_cfg(struct igc_hw *hw, u32 reg, u16 *value)
5260 struct igc_adapter *adapter = hw->back;
5262 pci_write_config_word(adapter->pdev, reg, *value);
5265 s32 igc_read_pcie_cap_reg(struct igc_hw *hw, u32 reg, u16 *value)
5267 struct igc_adapter *adapter = hw->back;
5269 if (!pci_is_pcie(adapter->pdev))
5270 return -IGC_ERR_CONFIG;
5272 pcie_capability_read_word(adapter->pdev, reg, value);
5277 s32 igc_write_pcie_cap_reg(struct igc_hw *hw, u32 reg, u16 *value)
5279 struct igc_adapter *adapter = hw->back;
5281 if (!pci_is_pcie(adapter->pdev))
5282 return -IGC_ERR_CONFIG;
5284 pcie_capability_write_word(adapter->pdev, reg, *value);
5289 u32 igc_rd32(struct igc_hw *hw, u32 reg)
5291 struct igc_adapter *igc = container_of(hw, struct igc_adapter, hw);
5292 u8 __iomem *hw_addr = READ_ONCE(hw->hw_addr);
5295 value = readl(&hw_addr[reg]);
5297 /* reads should not return all F's */
5298 if (!(~value) && (!reg || !(~readl(hw_addr)))) {
5299 struct net_device *netdev = igc->netdev;
5302 netif_device_detach(netdev);
5303 netdev_err(netdev, "PCIe link lost, device now detached\n");
5304 WARN(pci_device_is_present(igc->pdev),
5305 "igc: Failed to read reg 0x%x!\n", reg);
5311 int igc_set_spd_dplx(struct igc_adapter *adapter, u32 spd, u8 dplx)
5313 struct igc_mac_info *mac = &adapter->hw.mac;
5315 mac->autoneg = false;
5317 /* Make sure dplx is at most 1 bit and lsb of speed is not set
5318 * for the switch() below to work
5320 if ((spd & 1) || (dplx & ~1))
5323 switch (spd + dplx) {
5324 case SPEED_10 + DUPLEX_HALF:
5325 mac->forced_speed_duplex = ADVERTISE_10_HALF;
5327 case SPEED_10 + DUPLEX_FULL:
5328 mac->forced_speed_duplex = ADVERTISE_10_FULL;
5330 case SPEED_100 + DUPLEX_HALF:
5331 mac->forced_speed_duplex = ADVERTISE_100_HALF;
5333 case SPEED_100 + DUPLEX_FULL:
5334 mac->forced_speed_duplex = ADVERTISE_100_FULL;
5336 case SPEED_1000 + DUPLEX_FULL:
5337 mac->autoneg = true;
5338 adapter->hw.phy.autoneg_advertised = ADVERTISE_1000_FULL;
5340 case SPEED_1000 + DUPLEX_HALF: /* not supported */
5342 case SPEED_2500 + DUPLEX_FULL:
5343 mac->autoneg = true;
5344 adapter->hw.phy.autoneg_advertised = ADVERTISE_2500_FULL;
5346 case SPEED_2500 + DUPLEX_HALF: /* not supported */
5351 /* clear MDI, MDI(-X) override is only allowed when autoneg enabled */
5352 adapter->hw.phy.mdix = AUTO_ALL_MODES;
5357 netdev_err(adapter->netdev, "Unsupported Speed/Duplex configuration\n");
5362 * igc_probe - Device Initialization Routine
5363 * @pdev: PCI device information struct
5364 * @ent: entry in igc_pci_tbl
5366 * Returns 0 on success, negative on failure
5368 * igc_probe initializes an adapter identified by a pci_dev structure.
5369 * The OS initialization, configuring the adapter private structure,
5370 * and a hardware reset occur.
5372 static int igc_probe(struct pci_dev *pdev,
5373 const struct pci_device_id *ent)
5375 struct igc_adapter *adapter;
5376 struct net_device *netdev;
5378 const struct igc_info *ei = igc_info_tbl[ent->driver_data];
5379 int err, pci_using_dac;
5381 err = pci_enable_device_mem(pdev);
5386 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
5390 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
5393 "No usable DMA configuration, aborting\n");
5398 err = pci_request_mem_regions(pdev, igc_driver_name);
5402 pci_enable_pcie_error_reporting(pdev);
5404 pci_set_master(pdev);
5407 netdev = alloc_etherdev_mq(sizeof(struct igc_adapter),
5411 goto err_alloc_etherdev;
5413 SET_NETDEV_DEV(netdev, &pdev->dev);
5415 pci_set_drvdata(pdev, netdev);
5416 adapter = netdev_priv(netdev);
5417 adapter->netdev = netdev;
5418 adapter->pdev = pdev;
5421 adapter->port_num = hw->bus.func;
5422 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
5424 err = pci_save_state(pdev);
5429 adapter->io_addr = ioremap(pci_resource_start(pdev, 0),
5430 pci_resource_len(pdev, 0));
5431 if (!adapter->io_addr)
5434 /* hw->hw_addr can be zeroed, so use adapter->io_addr for unmap */
5435 hw->hw_addr = adapter->io_addr;
5437 netdev->netdev_ops = &igc_netdev_ops;
5438 igc_ethtool_set_ops(netdev);
5439 netdev->watchdog_timeo = 5 * HZ;
5441 netdev->mem_start = pci_resource_start(pdev, 0);
5442 netdev->mem_end = pci_resource_end(pdev, 0);
5444 /* PCI config space info */
5445 hw->vendor_id = pdev->vendor;
5446 hw->device_id = pdev->device;
5447 hw->revision_id = pdev->revision;
5448 hw->subsystem_vendor_id = pdev->subsystem_vendor;
5449 hw->subsystem_device_id = pdev->subsystem_device;
5451 /* Copy the default MAC and PHY function pointers */
5452 memcpy(&hw->mac.ops, ei->mac_ops, sizeof(hw->mac.ops));
5453 memcpy(&hw->phy.ops, ei->phy_ops, sizeof(hw->phy.ops));
5455 /* Initialize skew-specific constants */
5456 err = ei->get_invariants(hw);
5460 /* Add supported features to the features list*/
5461 netdev->features |= NETIF_F_SG;
5462 netdev->features |= NETIF_F_TSO;
5463 netdev->features |= NETIF_F_TSO6;
5464 netdev->features |= NETIF_F_TSO_ECN;
5465 netdev->features |= NETIF_F_RXCSUM;
5466 netdev->features |= NETIF_F_HW_CSUM;
5467 netdev->features |= NETIF_F_SCTP_CRC;
5468 netdev->features |= NETIF_F_HW_TC;
5470 #define IGC_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE | \
5471 NETIF_F_GSO_GRE_CSUM | \
5472 NETIF_F_GSO_IPXIP4 | \
5473 NETIF_F_GSO_IPXIP6 | \
5474 NETIF_F_GSO_UDP_TUNNEL | \
5475 NETIF_F_GSO_UDP_TUNNEL_CSUM)
5477 netdev->gso_partial_features = IGC_GSO_PARTIAL_FEATURES;
5478 netdev->features |= NETIF_F_GSO_PARTIAL | IGC_GSO_PARTIAL_FEATURES;
5480 /* setup the private structure */
5481 err = igc_sw_init(adapter);
5485 /* copy netdev features into list of user selectable features */
5486 netdev->hw_features |= NETIF_F_NTUPLE;
5487 netdev->hw_features |= netdev->features;
5490 netdev->features |= NETIF_F_HIGHDMA;
5492 /* MTU range: 68 - 9216 */
5493 netdev->min_mtu = ETH_MIN_MTU;
5494 netdev->max_mtu = MAX_STD_JUMBO_FRAME_SIZE;
5496 /* before reading the NVM, reset the controller to put the device in a
5497 * known good starting state
5499 hw->mac.ops.reset_hw(hw);
5501 if (igc_get_flash_presence_i225(hw)) {
5502 if (hw->nvm.ops.validate(hw) < 0) {
5503 dev_err(&pdev->dev, "The NVM Checksum Is Not Valid\n");
5509 if (eth_platform_get_mac_address(&pdev->dev, hw->mac.addr)) {
5510 /* copy the MAC address out of the NVM */
5511 if (hw->mac.ops.read_mac_addr(hw))
5512 dev_err(&pdev->dev, "NVM Read Error\n");
5515 memcpy(netdev->dev_addr, hw->mac.addr, netdev->addr_len);
5517 if (!is_valid_ether_addr(netdev->dev_addr)) {
5518 dev_err(&pdev->dev, "Invalid MAC Address\n");
5523 /* configure RXPBSIZE and TXPBSIZE */
5524 wr32(IGC_RXPBS, I225_RXPBSIZE_DEFAULT);
5525 wr32(IGC_TXPBS, I225_TXPBSIZE_DEFAULT);
5527 timer_setup(&adapter->watchdog_timer, igc_watchdog, 0);
5528 timer_setup(&adapter->phy_info_timer, igc_update_phy_info, 0);
5530 INIT_WORK(&adapter->reset_task, igc_reset_task);
5531 INIT_WORK(&adapter->watchdog_task, igc_watchdog_task);
5533 /* Initialize link properties that are user-changeable */
5534 adapter->fc_autoneg = true;
5535 hw->mac.autoneg = true;
5536 hw->phy.autoneg_advertised = 0xaf;
5538 hw->fc.requested_mode = igc_fc_default;
5539 hw->fc.current_mode = igc_fc_default;
5541 /* By default, support wake on port A */
5542 adapter->flags |= IGC_FLAG_WOL_SUPPORTED;
5544 /* initialize the wol settings based on the eeprom settings */
5545 if (adapter->flags & IGC_FLAG_WOL_SUPPORTED)
5546 adapter->wol |= IGC_WUFC_MAG;
5548 device_set_wakeup_enable(&adapter->pdev->dev,
5549 adapter->flags & IGC_FLAG_WOL_SUPPORTED);
5551 igc_ptp_init(adapter);
5553 /* reset the hardware with the new settings */
5556 /* let the f/w know that the h/w is now under the control of the
5559 igc_get_hw_control(adapter);
5561 strncpy(netdev->name, "eth%d", IFNAMSIZ);
5562 err = register_netdev(netdev);
5566 /* carrier off reporting is important to ethtool even BEFORE open */
5567 netif_carrier_off(netdev);
5569 /* Check if Media Autosense is enabled */
5572 /* print pcie link status and MAC address */
5573 pcie_print_link_status(pdev);
5574 netdev_info(netdev, "MAC: %pM\n", netdev->dev_addr);
5576 dev_pm_set_driver_flags(&pdev->dev, DPM_FLAG_NO_DIRECT_COMPLETE);
5577 /* Disable EEE for internal PHY devices */
5578 hw->dev_spec._base.eee_enable = false;
5579 adapter->flags &= ~IGC_FLAG_EEE;
5580 igc_set_eee_i225(hw, false, false, false);
5582 pm_runtime_put_noidle(&pdev->dev);
5587 igc_release_hw_control(adapter);
5589 if (!igc_check_reset_block(hw))
5592 igc_clear_interrupt_scheme(adapter);
5593 iounmap(adapter->io_addr);
5595 free_netdev(netdev);
5597 pci_release_mem_regions(pdev);
5600 pci_disable_device(pdev);
5605 * igc_remove - Device Removal Routine
5606 * @pdev: PCI device information struct
5608 * igc_remove is called by the PCI subsystem to alert the driver
5609 * that it should release a PCI device. This could be caused by a
5610 * Hot-Plug event, or because the driver is going to be removed from
5613 static void igc_remove(struct pci_dev *pdev)
5615 struct net_device *netdev = pci_get_drvdata(pdev);
5616 struct igc_adapter *adapter = netdev_priv(netdev);
5618 pm_runtime_get_noresume(&pdev->dev);
5620 igc_flush_nfc_rules(adapter);
5622 igc_ptp_stop(adapter);
5624 set_bit(__IGC_DOWN, &adapter->state);
5626 del_timer_sync(&adapter->watchdog_timer);
5627 del_timer_sync(&adapter->phy_info_timer);
5629 cancel_work_sync(&adapter->reset_task);
5630 cancel_work_sync(&adapter->watchdog_task);
5632 /* Release control of h/w to f/w. If f/w is AMT enabled, this
5633 * would have already happened in close and is redundant.
5635 igc_release_hw_control(adapter);
5636 unregister_netdev(netdev);
5638 igc_clear_interrupt_scheme(adapter);
5639 pci_iounmap(pdev, adapter->io_addr);
5640 pci_release_mem_regions(pdev);
5642 free_netdev(netdev);
5644 pci_disable_pcie_error_reporting(pdev);
5646 pci_disable_device(pdev);
5649 static int __igc_shutdown(struct pci_dev *pdev, bool *enable_wake,
5652 struct net_device *netdev = pci_get_drvdata(pdev);
5653 struct igc_adapter *adapter = netdev_priv(netdev);
5654 u32 wufc = runtime ? IGC_WUFC_LNKC : adapter->wol;
5655 struct igc_hw *hw = &adapter->hw;
5656 u32 ctrl, rctl, status;
5660 netif_device_detach(netdev);
5662 if (netif_running(netdev))
5663 __igc_close(netdev, true);
5665 igc_ptp_suspend(adapter);
5667 igc_clear_interrupt_scheme(adapter);
5670 status = rd32(IGC_STATUS);
5671 if (status & IGC_STATUS_LU)
5672 wufc &= ~IGC_WUFC_LNKC;
5675 igc_setup_rctl(adapter);
5676 igc_set_rx_mode(netdev);
5678 /* turn on all-multi mode if wake on multicast is enabled */
5679 if (wufc & IGC_WUFC_MC) {
5680 rctl = rd32(IGC_RCTL);
5681 rctl |= IGC_RCTL_MPE;
5682 wr32(IGC_RCTL, rctl);
5685 ctrl = rd32(IGC_CTRL);
5686 ctrl |= IGC_CTRL_ADVD3WUC;
5687 wr32(IGC_CTRL, ctrl);
5689 /* Allow time for pending master requests to run */
5690 igc_disable_pcie_master(hw);
5692 wr32(IGC_WUC, IGC_WUC_PME_EN);
5693 wr32(IGC_WUFC, wufc);
5699 wake = wufc || adapter->en_mng_pt;
5701 igc_power_down_phy_copper_base(&adapter->hw);
5703 igc_power_up_link(adapter);
5706 *enable_wake = wake;
5708 /* Release control of h/w to f/w. If f/w is AMT enabled, this
5709 * would have already happened in close and is redundant.
5711 igc_release_hw_control(adapter);
5713 pci_disable_device(pdev);
5719 static int __maybe_unused igc_runtime_suspend(struct device *dev)
5721 return __igc_shutdown(to_pci_dev(dev), NULL, 1);
5724 static void igc_deliver_wake_packet(struct net_device *netdev)
5726 struct igc_adapter *adapter = netdev_priv(netdev);
5727 struct igc_hw *hw = &adapter->hw;
5728 struct sk_buff *skb;
5731 wupl = rd32(IGC_WUPL) & IGC_WUPL_MASK;
5733 /* WUPM stores only the first 128 bytes of the wake packet.
5734 * Read the packet only if we have the whole thing.
5736 if (wupl == 0 || wupl > IGC_WUPM_BYTES)
5739 skb = netdev_alloc_skb_ip_align(netdev, IGC_WUPM_BYTES);
5745 /* Ensure reads are 32-bit aligned */
5746 wupl = roundup(wupl, 4);
5748 memcpy_fromio(skb->data, hw->hw_addr + IGC_WUPM_REG(0), wupl);
5750 skb->protocol = eth_type_trans(skb, netdev);
5754 static int __maybe_unused igc_resume(struct device *dev)
5756 struct pci_dev *pdev = to_pci_dev(dev);
5757 struct net_device *netdev = pci_get_drvdata(pdev);
5758 struct igc_adapter *adapter = netdev_priv(netdev);
5759 struct igc_hw *hw = &adapter->hw;
5762 pci_set_power_state(pdev, PCI_D0);
5763 pci_restore_state(pdev);
5764 pci_save_state(pdev);
5766 if (!pci_device_is_present(pdev))
5768 err = pci_enable_device_mem(pdev);
5770 netdev_err(netdev, "Cannot enable PCI device from suspend\n");
5773 pci_set_master(pdev);
5775 pci_enable_wake(pdev, PCI_D3hot, 0);
5776 pci_enable_wake(pdev, PCI_D3cold, 0);
5778 if (igc_init_interrupt_scheme(adapter, true)) {
5779 netdev_err(netdev, "Unable to allocate memory for queues\n");
5785 /* let the f/w know that the h/w is now under the control of the
5788 igc_get_hw_control(adapter);
5790 val = rd32(IGC_WUS);
5791 if (val & WAKE_PKT_WUS)
5792 igc_deliver_wake_packet(netdev);
5797 if (!err && netif_running(netdev))
5798 err = __igc_open(netdev, true);
5801 netif_device_attach(netdev);
5807 static int __maybe_unused igc_runtime_resume(struct device *dev)
5809 return igc_resume(dev);
5812 static int __maybe_unused igc_suspend(struct device *dev)
5814 return __igc_shutdown(to_pci_dev(dev), NULL, 0);
5817 static int __maybe_unused igc_runtime_idle(struct device *dev)
5819 struct net_device *netdev = dev_get_drvdata(dev);
5820 struct igc_adapter *adapter = netdev_priv(netdev);
5822 if (!igc_has_link(adapter))
5823 pm_schedule_suspend(dev, MSEC_PER_SEC * 5);
5827 #endif /* CONFIG_PM */
5829 static void igc_shutdown(struct pci_dev *pdev)
5833 __igc_shutdown(pdev, &wake, 0);
5835 if (system_state == SYSTEM_POWER_OFF) {
5836 pci_wake_from_d3(pdev, wake);
5837 pci_set_power_state(pdev, PCI_D3hot);
5842 * igc_io_error_detected - called when PCI error is detected
5843 * @pdev: Pointer to PCI device
5844 * @state: The current PCI connection state
5846 * This function is called after a PCI bus error affecting
5847 * this device has been detected.
5849 static pci_ers_result_t igc_io_error_detected(struct pci_dev *pdev,
5850 pci_channel_state_t state)
5852 struct net_device *netdev = pci_get_drvdata(pdev);
5853 struct igc_adapter *adapter = netdev_priv(netdev);
5855 netif_device_detach(netdev);
5857 if (state == pci_channel_io_perm_failure)
5858 return PCI_ERS_RESULT_DISCONNECT;
5860 if (netif_running(netdev))
5862 pci_disable_device(pdev);
5864 /* Request a slot reset. */
5865 return PCI_ERS_RESULT_NEED_RESET;
5869 * igc_io_slot_reset - called after the PCI bus has been reset.
5870 * @pdev: Pointer to PCI device
5872 * Restart the card from scratch, as if from a cold-boot. Implementation
5873 * resembles the first-half of the igc_resume routine.
5875 static pci_ers_result_t igc_io_slot_reset(struct pci_dev *pdev)
5877 struct net_device *netdev = pci_get_drvdata(pdev);
5878 struct igc_adapter *adapter = netdev_priv(netdev);
5879 struct igc_hw *hw = &adapter->hw;
5880 pci_ers_result_t result;
5882 if (pci_enable_device_mem(pdev)) {
5883 netdev_err(netdev, "Could not re-enable PCI device after reset\n");
5884 result = PCI_ERS_RESULT_DISCONNECT;
5886 pci_set_master(pdev);
5887 pci_restore_state(pdev);
5888 pci_save_state(pdev);
5890 pci_enable_wake(pdev, PCI_D3hot, 0);
5891 pci_enable_wake(pdev, PCI_D3cold, 0);
5893 /* In case of PCI error, adapter loses its HW address
5894 * so we should re-assign it here.
5896 hw->hw_addr = adapter->io_addr;
5900 result = PCI_ERS_RESULT_RECOVERED;
5907 * igc_io_resume - called when traffic can start to flow again.
5908 * @pdev: Pointer to PCI device
5910 * This callback is called when the error recovery driver tells us that
5911 * its OK to resume normal operation. Implementation resembles the
5912 * second-half of the igc_resume routine.
5914 static void igc_io_resume(struct pci_dev *pdev)
5916 struct net_device *netdev = pci_get_drvdata(pdev);
5917 struct igc_adapter *adapter = netdev_priv(netdev);
5920 if (netif_running(netdev)) {
5921 if (igc_open(netdev)) {
5922 netdev_err(netdev, "igc_open failed after reset\n");
5927 netif_device_attach(netdev);
5929 /* let the f/w know that the h/w is now under the control of the
5932 igc_get_hw_control(adapter);
5936 static const struct pci_error_handlers igc_err_handler = {
5937 .error_detected = igc_io_error_detected,
5938 .slot_reset = igc_io_slot_reset,
5939 .resume = igc_io_resume,
5943 static const struct dev_pm_ops igc_pm_ops = {
5944 SET_SYSTEM_SLEEP_PM_OPS(igc_suspend, igc_resume)
5945 SET_RUNTIME_PM_OPS(igc_runtime_suspend, igc_runtime_resume,
5950 static struct pci_driver igc_driver = {
5951 .name = igc_driver_name,
5952 .id_table = igc_pci_tbl,
5954 .remove = igc_remove,
5956 .driver.pm = &igc_pm_ops,
5958 .shutdown = igc_shutdown,
5959 .err_handler = &igc_err_handler,
5963 * igc_reinit_queues - return error
5964 * @adapter: pointer to adapter structure
5966 int igc_reinit_queues(struct igc_adapter *adapter)
5968 struct net_device *netdev = adapter->netdev;
5971 if (netif_running(netdev))
5974 igc_reset_interrupt_capability(adapter);
5976 if (igc_init_interrupt_scheme(adapter, true)) {
5977 netdev_err(netdev, "Unable to allocate memory for queues\n");
5981 if (netif_running(netdev))
5982 err = igc_open(netdev);
5988 * igc_get_hw_dev - return device
5989 * @hw: pointer to hardware structure
5991 * used by hardware layer to print debugging information
5993 struct net_device *igc_get_hw_dev(struct igc_hw *hw)
5995 struct igc_adapter *adapter = hw->back;
5997 return adapter->netdev;
6001 * igc_init_module - Driver Registration Routine
6003 * igc_init_module is the first routine called when the driver is
6004 * loaded. All it does is register with the PCI subsystem.
6006 static int __init igc_init_module(void)
6010 pr_info("%s\n", igc_driver_string);
6011 pr_info("%s\n", igc_copyright);
6013 ret = pci_register_driver(&igc_driver);
6017 module_init(igc_init_module);
6020 * igc_exit_module - Driver Exit Cleanup Routine
6022 * igc_exit_module is called just before the driver is removed
6025 static void __exit igc_exit_module(void)
6027 pci_unregister_driver(&igc_driver);
6030 module_exit(igc_exit_module);