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>
12 #define DRV_VERSION "0.0.1-k"
13 #define DRV_SUMMARY "Intel(R) 2.5G Ethernet Linux Driver"
15 #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
17 static int debug = -1;
19 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
20 MODULE_DESCRIPTION(DRV_SUMMARY);
21 MODULE_LICENSE("GPL v2");
22 MODULE_VERSION(DRV_VERSION);
23 module_param(debug, int, 0);
24 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
26 char igc_driver_name[] = "igc";
27 char igc_driver_version[] = DRV_VERSION;
28 static const char igc_driver_string[] = DRV_SUMMARY;
29 static const char igc_copyright[] =
30 "Copyright(c) 2018 Intel Corporation.";
32 static const struct igc_info *igc_info_tbl[] = {
33 [board_base] = &igc_base_info,
36 static const struct pci_device_id igc_pci_tbl[] = {
37 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_LM), board_base },
38 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_V), board_base },
39 /* required last entry */
43 MODULE_DEVICE_TABLE(pci, igc_pci_tbl);
45 /* forward declaration */
46 static void igc_clean_tx_ring(struct igc_ring *tx_ring);
47 static int igc_sw_init(struct igc_adapter *);
48 static void igc_configure(struct igc_adapter *adapter);
49 static void igc_power_down_link(struct igc_adapter *adapter);
50 static void igc_set_default_mac_filter(struct igc_adapter *adapter);
51 static void igc_set_rx_mode(struct net_device *netdev);
52 static void igc_write_itr(struct igc_q_vector *q_vector);
53 static void igc_assign_vector(struct igc_q_vector *q_vector, int msix_vector);
54 static void igc_free_q_vector(struct igc_adapter *adapter, int v_idx);
55 static void igc_set_interrupt_capability(struct igc_adapter *adapter,
57 static void igc_free_q_vectors(struct igc_adapter *adapter);
58 static void igc_irq_disable(struct igc_adapter *adapter);
59 static void igc_irq_enable(struct igc_adapter *adapter);
60 static void igc_configure_msix(struct igc_adapter *adapter);
61 static bool igc_alloc_mapped_page(struct igc_ring *rx_ring,
62 struct igc_rx_buffer *bi);
71 void igc_reset(struct igc_adapter *adapter)
73 struct pci_dev *pdev = adapter->pdev;
74 struct igc_hw *hw = &adapter->hw;
76 hw->mac.ops.reset_hw(hw);
78 if (hw->mac.ops.init_hw(hw))
79 dev_err(&pdev->dev, "Hardware Error\n");
81 if (!netif_running(adapter->netdev))
82 igc_power_down_link(adapter);
88 * igc_power_up_link - Power up the phy/serdes link
89 * @adapter: address of board private structure
91 static void igc_power_up_link(struct igc_adapter *adapter)
93 igc_reset_phy(&adapter->hw);
95 if (adapter->hw.phy.media_type == igc_media_type_copper)
96 igc_power_up_phy_copper(&adapter->hw);
98 igc_setup_link(&adapter->hw);
102 * igc_power_down_link - Power down the phy/serdes link
103 * @adapter: address of board private structure
105 static void igc_power_down_link(struct igc_adapter *adapter)
107 if (adapter->hw.phy.media_type == igc_media_type_copper)
108 igc_power_down_phy_copper_base(&adapter->hw);
112 * igc_release_hw_control - release control of the h/w to f/w
113 * @adapter: address of board private structure
115 * igc_release_hw_control resets CTRL_EXT:DRV_LOAD bit.
116 * For ASF and Pass Through versions of f/w this means that the
117 * driver is no longer loaded.
119 static void igc_release_hw_control(struct igc_adapter *adapter)
121 struct igc_hw *hw = &adapter->hw;
124 /* Let firmware take over control of h/w */
125 ctrl_ext = rd32(IGC_CTRL_EXT);
127 ctrl_ext & ~IGC_CTRL_EXT_DRV_LOAD);
131 * igc_get_hw_control - get control of the h/w from f/w
132 * @adapter: address of board private structure
134 * igc_get_hw_control sets CTRL_EXT:DRV_LOAD bit.
135 * For ASF and Pass Through versions of f/w this means that
136 * the driver is loaded.
138 static void igc_get_hw_control(struct igc_adapter *adapter)
140 struct igc_hw *hw = &adapter->hw;
143 /* Let firmware know the driver has taken over */
144 ctrl_ext = rd32(IGC_CTRL_EXT);
146 ctrl_ext | IGC_CTRL_EXT_DRV_LOAD);
150 * igc_free_tx_resources - Free Tx Resources per Queue
151 * @tx_ring: Tx descriptor ring for a specific queue
153 * Free all transmit software resources
155 void igc_free_tx_resources(struct igc_ring *tx_ring)
157 igc_clean_tx_ring(tx_ring);
159 vfree(tx_ring->tx_buffer_info);
160 tx_ring->tx_buffer_info = NULL;
162 /* if not set, then don't free */
166 dma_free_coherent(tx_ring->dev, tx_ring->size,
167 tx_ring->desc, tx_ring->dma);
169 tx_ring->desc = NULL;
173 * igc_free_all_tx_resources - Free Tx Resources for All Queues
174 * @adapter: board private structure
176 * Free all transmit software resources
178 static void igc_free_all_tx_resources(struct igc_adapter *adapter)
182 for (i = 0; i < adapter->num_tx_queues; i++)
183 igc_free_tx_resources(adapter->tx_ring[i]);
187 * igc_clean_tx_ring - Free Tx Buffers
188 * @tx_ring: ring to be cleaned
190 static void igc_clean_tx_ring(struct igc_ring *tx_ring)
192 u16 i = tx_ring->next_to_clean;
193 struct igc_tx_buffer *tx_buffer = &tx_ring->tx_buffer_info[i];
195 while (i != tx_ring->next_to_use) {
196 union igc_adv_tx_desc *eop_desc, *tx_desc;
198 /* Free all the Tx ring sk_buffs */
199 dev_kfree_skb_any(tx_buffer->skb);
201 /* unmap skb header data */
202 dma_unmap_single(tx_ring->dev,
203 dma_unmap_addr(tx_buffer, dma),
204 dma_unmap_len(tx_buffer, len),
207 /* check for eop_desc to determine the end of the packet */
208 eop_desc = tx_buffer->next_to_watch;
209 tx_desc = IGC_TX_DESC(tx_ring, i);
211 /* unmap remaining buffers */
212 while (tx_desc != eop_desc) {
216 if (unlikely(i == tx_ring->count)) {
218 tx_buffer = tx_ring->tx_buffer_info;
219 tx_desc = IGC_TX_DESC(tx_ring, 0);
222 /* unmap any remaining paged data */
223 if (dma_unmap_len(tx_buffer, len))
224 dma_unmap_page(tx_ring->dev,
225 dma_unmap_addr(tx_buffer, dma),
226 dma_unmap_len(tx_buffer, len),
230 /* move us one more past the eop_desc for start of next pkt */
233 if (unlikely(i == tx_ring->count)) {
235 tx_buffer = tx_ring->tx_buffer_info;
239 /* reset BQL for queue */
240 netdev_tx_reset_queue(txring_txq(tx_ring));
242 /* reset next_to_use and next_to_clean */
243 tx_ring->next_to_use = 0;
244 tx_ring->next_to_clean = 0;
248 * igc_clean_all_tx_rings - Free Tx Buffers for all queues
249 * @adapter: board private structure
251 static void igc_clean_all_tx_rings(struct igc_adapter *adapter)
255 for (i = 0; i < adapter->num_tx_queues; i++)
256 if (adapter->tx_ring[i])
257 igc_clean_tx_ring(adapter->tx_ring[i]);
261 * igc_setup_tx_resources - allocate Tx resources (Descriptors)
262 * @tx_ring: tx descriptor ring (for a specific queue) to setup
264 * Return 0 on success, negative on failure
266 int igc_setup_tx_resources(struct igc_ring *tx_ring)
268 struct device *dev = tx_ring->dev;
271 size = sizeof(struct igc_tx_buffer) * tx_ring->count;
272 tx_ring->tx_buffer_info = vzalloc(size);
273 if (!tx_ring->tx_buffer_info)
276 /* round up to nearest 4K */
277 tx_ring->size = tx_ring->count * sizeof(union igc_adv_tx_desc);
278 tx_ring->size = ALIGN(tx_ring->size, 4096);
280 tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
281 &tx_ring->dma, GFP_KERNEL);
286 tx_ring->next_to_use = 0;
287 tx_ring->next_to_clean = 0;
292 vfree(tx_ring->tx_buffer_info);
294 "Unable to allocate memory for the transmit descriptor ring\n");
299 * igc_setup_all_tx_resources - wrapper to allocate Tx resources for all queues
300 * @adapter: board private structure
302 * Return 0 on success, negative on failure
304 static int igc_setup_all_tx_resources(struct igc_adapter *adapter)
306 struct pci_dev *pdev = adapter->pdev;
309 for (i = 0; i < adapter->num_tx_queues; i++) {
310 err = igc_setup_tx_resources(adapter->tx_ring[i]);
313 "Allocation for Tx Queue %u failed\n", i);
314 for (i--; i >= 0; i--)
315 igc_free_tx_resources(adapter->tx_ring[i]);
324 * igc_clean_rx_ring - Free Rx Buffers per Queue
325 * @rx_ring: ring to free buffers from
327 static void igc_clean_rx_ring(struct igc_ring *rx_ring)
329 u16 i = rx_ring->next_to_clean;
332 dev_kfree_skb(rx_ring->skb);
335 /* Free all the Rx ring sk_buffs */
336 while (i != rx_ring->next_to_alloc) {
337 struct igc_rx_buffer *buffer_info = &rx_ring->rx_buffer_info[i];
339 /* Invalidate cache lines that may have been written to by
340 * device so that we avoid corrupting memory.
342 dma_sync_single_range_for_cpu(rx_ring->dev,
344 buffer_info->page_offset,
345 igc_rx_bufsz(rx_ring),
348 /* free resources associated with mapping */
349 dma_unmap_page_attrs(rx_ring->dev,
351 igc_rx_pg_size(rx_ring),
354 __page_frag_cache_drain(buffer_info->page,
355 buffer_info->pagecnt_bias);
358 if (i == rx_ring->count)
362 rx_ring->next_to_alloc = 0;
363 rx_ring->next_to_clean = 0;
364 rx_ring->next_to_use = 0;
368 * igc_clean_all_rx_rings - Free Rx Buffers for all queues
369 * @adapter: board private structure
371 static void igc_clean_all_rx_rings(struct igc_adapter *adapter)
375 for (i = 0; i < adapter->num_rx_queues; i++)
376 if (adapter->rx_ring[i])
377 igc_clean_rx_ring(adapter->rx_ring[i]);
381 * igc_free_rx_resources - Free Rx Resources
382 * @rx_ring: ring to clean the resources from
384 * Free all receive software resources
386 void igc_free_rx_resources(struct igc_ring *rx_ring)
388 igc_clean_rx_ring(rx_ring);
390 vfree(rx_ring->rx_buffer_info);
391 rx_ring->rx_buffer_info = NULL;
393 /* if not set, then don't free */
397 dma_free_coherent(rx_ring->dev, rx_ring->size,
398 rx_ring->desc, rx_ring->dma);
400 rx_ring->desc = NULL;
404 * igc_free_all_rx_resources - Free Rx Resources for All Queues
405 * @adapter: board private structure
407 * Free all receive software resources
409 static void igc_free_all_rx_resources(struct igc_adapter *adapter)
413 for (i = 0; i < adapter->num_rx_queues; i++)
414 igc_free_rx_resources(adapter->rx_ring[i]);
418 * igc_setup_rx_resources - allocate Rx resources (Descriptors)
419 * @rx_ring: rx descriptor ring (for a specific queue) to setup
421 * Returns 0 on success, negative on failure
423 int igc_setup_rx_resources(struct igc_ring *rx_ring)
425 struct device *dev = rx_ring->dev;
428 size = sizeof(struct igc_rx_buffer) * rx_ring->count;
429 rx_ring->rx_buffer_info = vzalloc(size);
430 if (!rx_ring->rx_buffer_info)
433 desc_len = sizeof(union igc_adv_rx_desc);
435 /* Round up to nearest 4K */
436 rx_ring->size = rx_ring->count * desc_len;
437 rx_ring->size = ALIGN(rx_ring->size, 4096);
439 rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
440 &rx_ring->dma, GFP_KERNEL);
445 rx_ring->next_to_alloc = 0;
446 rx_ring->next_to_clean = 0;
447 rx_ring->next_to_use = 0;
452 vfree(rx_ring->rx_buffer_info);
453 rx_ring->rx_buffer_info = NULL;
455 "Unable to allocate memory for the receive descriptor ring\n");
460 * igc_setup_all_rx_resources - wrapper to allocate Rx resources
461 * (Descriptors) for all queues
462 * @adapter: board private structure
464 * Return 0 on success, negative on failure
466 static int igc_setup_all_rx_resources(struct igc_adapter *adapter)
468 struct pci_dev *pdev = adapter->pdev;
471 for (i = 0; i < adapter->num_rx_queues; i++) {
472 err = igc_setup_rx_resources(adapter->rx_ring[i]);
475 "Allocation for Rx Queue %u failed\n", i);
476 for (i--; i >= 0; i--)
477 igc_free_rx_resources(adapter->rx_ring[i]);
486 * igc_configure_rx_ring - Configure a receive ring after Reset
487 * @adapter: board private structure
488 * @ring: receive ring to be configured
490 * Configure the Rx unit of the MAC after a reset.
492 static void igc_configure_rx_ring(struct igc_adapter *adapter,
493 struct igc_ring *ring)
495 struct igc_hw *hw = &adapter->hw;
496 union igc_adv_rx_desc *rx_desc;
497 int reg_idx = ring->reg_idx;
498 u32 srrctl = 0, rxdctl = 0;
499 u64 rdba = ring->dma;
501 /* disable the queue */
502 wr32(IGC_RXDCTL(reg_idx), 0);
504 /* Set DMA base address registers */
505 wr32(IGC_RDBAL(reg_idx),
506 rdba & 0x00000000ffffffffULL);
507 wr32(IGC_RDBAH(reg_idx), rdba >> 32);
508 wr32(IGC_RDLEN(reg_idx),
509 ring->count * sizeof(union igc_adv_rx_desc));
511 /* initialize head and tail */
512 ring->tail = adapter->io_addr + IGC_RDT(reg_idx);
513 wr32(IGC_RDH(reg_idx), 0);
514 writel(0, ring->tail);
516 /* reset next-to- use/clean to place SW in sync with hardware */
517 ring->next_to_clean = 0;
518 ring->next_to_use = 0;
520 /* set descriptor configuration */
521 srrctl = IGC_RX_HDR_LEN << IGC_SRRCTL_BSIZEHDRSIZE_SHIFT;
522 if (ring_uses_large_buffer(ring))
523 srrctl |= IGC_RXBUFFER_3072 >> IGC_SRRCTL_BSIZEPKT_SHIFT;
525 srrctl |= IGC_RXBUFFER_2048 >> IGC_SRRCTL_BSIZEPKT_SHIFT;
526 srrctl |= IGC_SRRCTL_DESCTYPE_ADV_ONEBUF;
528 wr32(IGC_SRRCTL(reg_idx), srrctl);
530 rxdctl |= IGC_RX_PTHRESH;
531 rxdctl |= IGC_RX_HTHRESH << 8;
532 rxdctl |= IGC_RX_WTHRESH << 16;
534 /* initialize rx_buffer_info */
535 memset(ring->rx_buffer_info, 0,
536 sizeof(struct igc_rx_buffer) * ring->count);
538 /* initialize Rx descriptor 0 */
539 rx_desc = IGC_RX_DESC(ring, 0);
540 rx_desc->wb.upper.length = 0;
542 /* enable receive descriptor fetching */
543 rxdctl |= IGC_RXDCTL_QUEUE_ENABLE;
545 wr32(IGC_RXDCTL(reg_idx), rxdctl);
549 * igc_configure_rx - Configure receive Unit after Reset
550 * @adapter: board private structure
552 * Configure the Rx unit of the MAC after a reset.
554 static void igc_configure_rx(struct igc_adapter *adapter)
558 /* Setup the HW Rx Head and Tail Descriptor Pointers and
559 * the Base and Length of the Rx Descriptor Ring
561 for (i = 0; i < adapter->num_rx_queues; i++)
562 igc_configure_rx_ring(adapter, adapter->rx_ring[i]);
566 * igc_configure_tx_ring - Configure transmit ring after Reset
567 * @adapter: board private structure
568 * @ring: tx ring to configure
570 * Configure a transmit ring after a reset.
572 static void igc_configure_tx_ring(struct igc_adapter *adapter,
573 struct igc_ring *ring)
575 struct igc_hw *hw = &adapter->hw;
576 int reg_idx = ring->reg_idx;
577 u64 tdba = ring->dma;
580 /* disable the queue */
581 wr32(IGC_TXDCTL(reg_idx), 0);
585 wr32(IGC_TDLEN(reg_idx),
586 ring->count * sizeof(union igc_adv_tx_desc));
587 wr32(IGC_TDBAL(reg_idx),
588 tdba & 0x00000000ffffffffULL);
589 wr32(IGC_TDBAH(reg_idx), tdba >> 32);
591 ring->tail = adapter->io_addr + IGC_TDT(reg_idx);
592 wr32(IGC_TDH(reg_idx), 0);
593 writel(0, ring->tail);
595 txdctl |= IGC_TX_PTHRESH;
596 txdctl |= IGC_TX_HTHRESH << 8;
597 txdctl |= IGC_TX_WTHRESH << 16;
599 txdctl |= IGC_TXDCTL_QUEUE_ENABLE;
600 wr32(IGC_TXDCTL(reg_idx), txdctl);
604 * igc_configure_tx - Configure transmit Unit after Reset
605 * @adapter: board private structure
607 * Configure the Tx unit of the MAC after a reset.
609 static void igc_configure_tx(struct igc_adapter *adapter)
613 for (i = 0; i < adapter->num_tx_queues; i++)
614 igc_configure_tx_ring(adapter, adapter->tx_ring[i]);
618 * igc_setup_mrqc - configure the multiple receive queue control registers
619 * @adapter: Board private structure
621 static void igc_setup_mrqc(struct igc_adapter *adapter)
623 struct igc_hw *hw = &adapter->hw;
624 u32 j, num_rx_queues;
628 netdev_rss_key_fill(rss_key, sizeof(rss_key));
629 for (j = 0; j < 10; j++)
630 wr32(IGC_RSSRK(j), rss_key[j]);
632 num_rx_queues = adapter->rss_queues;
634 if (adapter->rss_indir_tbl_init != num_rx_queues) {
635 for (j = 0; j < IGC_RETA_SIZE; j++)
636 adapter->rss_indir_tbl[j] =
637 (j * num_rx_queues) / IGC_RETA_SIZE;
638 adapter->rss_indir_tbl_init = num_rx_queues;
640 igc_write_rss_indir_tbl(adapter);
642 /* Disable raw packet checksumming so that RSS hash is placed in
643 * descriptor on writeback. No need to enable TCP/UDP/IP checksum
644 * offloads as they are enabled by default
646 rxcsum = rd32(IGC_RXCSUM);
647 rxcsum |= IGC_RXCSUM_PCSD;
649 /* Enable Receive Checksum Offload for SCTP */
650 rxcsum |= IGC_RXCSUM_CRCOFL;
652 /* Don't need to set TUOFL or IPOFL, they default to 1 */
653 wr32(IGC_RXCSUM, rxcsum);
655 /* Generate RSS hash based on packet types, TCP/UDP
656 * port numbers and/or IPv4/v6 src and dst addresses
658 mrqc = IGC_MRQC_RSS_FIELD_IPV4 |
659 IGC_MRQC_RSS_FIELD_IPV4_TCP |
660 IGC_MRQC_RSS_FIELD_IPV6 |
661 IGC_MRQC_RSS_FIELD_IPV6_TCP |
662 IGC_MRQC_RSS_FIELD_IPV6_TCP_EX;
664 if (adapter->flags & IGC_FLAG_RSS_FIELD_IPV4_UDP)
665 mrqc |= IGC_MRQC_RSS_FIELD_IPV4_UDP;
666 if (adapter->flags & IGC_FLAG_RSS_FIELD_IPV6_UDP)
667 mrqc |= IGC_MRQC_RSS_FIELD_IPV6_UDP;
669 mrqc |= IGC_MRQC_ENABLE_RSS_MQ;
671 wr32(IGC_MRQC, mrqc);
675 * igc_setup_rctl - configure the receive control registers
676 * @adapter: Board private structure
678 static void igc_setup_rctl(struct igc_adapter *adapter)
680 struct igc_hw *hw = &adapter->hw;
683 rctl = rd32(IGC_RCTL);
685 rctl &= ~(3 << IGC_RCTL_MO_SHIFT);
686 rctl &= ~(IGC_RCTL_LBM_TCVR | IGC_RCTL_LBM_MAC);
688 rctl |= IGC_RCTL_EN | IGC_RCTL_BAM | IGC_RCTL_RDMTS_HALF |
689 (hw->mac.mc_filter_type << IGC_RCTL_MO_SHIFT);
691 /* enable stripping of CRC. Newer features require
692 * that the HW strips the CRC.
694 rctl |= IGC_RCTL_SECRC;
696 /* disable store bad packets and clear size bits. */
697 rctl &= ~(IGC_RCTL_SBP | IGC_RCTL_SZ_256);
699 /* enable LPE to allow for reception of jumbo frames */
700 rctl |= IGC_RCTL_LPE;
702 /* disable queue 0 to prevent tail write w/o re-config */
703 wr32(IGC_RXDCTL(0), 0);
705 /* This is useful for sniffing bad packets. */
706 if (adapter->netdev->features & NETIF_F_RXALL) {
707 /* UPE and MPE will be handled by normal PROMISC logic
710 rctl |= (IGC_RCTL_SBP | /* Receive bad packets */
711 IGC_RCTL_BAM | /* RX All Bcast Pkts */
712 IGC_RCTL_PMCF); /* RX All MAC Ctrl Pkts */
714 rctl &= ~(IGC_RCTL_DPF | /* Allow filtered pause */
715 IGC_RCTL_CFIEN); /* Disable VLAN CFIEN Filter */
718 wr32(IGC_RCTL, rctl);
722 * igc_setup_tctl - configure the transmit control registers
723 * @adapter: Board private structure
725 static void igc_setup_tctl(struct igc_adapter *adapter)
727 struct igc_hw *hw = &adapter->hw;
730 /* disable queue 0 which icould be enabled by default */
731 wr32(IGC_TXDCTL(0), 0);
733 /* Program the Transmit Control Register */
734 tctl = rd32(IGC_TCTL);
735 tctl &= ~IGC_TCTL_CT;
736 tctl |= IGC_TCTL_PSP | IGC_TCTL_RTLC |
737 (IGC_COLLISION_THRESHOLD << IGC_CT_SHIFT);
739 /* Enable transmits */
742 wr32(IGC_TCTL, tctl);
746 * igc_set_mac - Change the Ethernet Address of the NIC
747 * @netdev: network interface device structure
748 * @p: pointer to an address structure
750 * Returns 0 on success, negative on failure
752 static int igc_set_mac(struct net_device *netdev, void *p)
754 struct igc_adapter *adapter = netdev_priv(netdev);
755 struct igc_hw *hw = &adapter->hw;
756 struct sockaddr *addr = p;
758 if (!is_valid_ether_addr(addr->sa_data))
759 return -EADDRNOTAVAIL;
761 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
762 memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
764 /* set the correct pool for the new PF MAC address in entry 0 */
765 igc_set_default_mac_filter(adapter);
770 static void igc_tx_csum(struct igc_ring *tx_ring, struct igc_tx_buffer *first)
774 static int __igc_maybe_stop_tx(struct igc_ring *tx_ring, const u16 size)
776 struct net_device *netdev = tx_ring->netdev;
778 netif_stop_subqueue(netdev, tx_ring->queue_index);
780 /* memory barriier comment */
783 /* We need to check again in a case another CPU has just
784 * made room available.
786 if (igc_desc_unused(tx_ring) < size)
790 netif_wake_subqueue(netdev, tx_ring->queue_index);
792 u64_stats_update_begin(&tx_ring->tx_syncp2);
793 tx_ring->tx_stats.restart_queue2++;
794 u64_stats_update_end(&tx_ring->tx_syncp2);
799 static inline int igc_maybe_stop_tx(struct igc_ring *tx_ring, const u16 size)
801 if (igc_desc_unused(tx_ring) >= size)
803 return __igc_maybe_stop_tx(tx_ring, size);
806 static u32 igc_tx_cmd_type(struct sk_buff *skb, u32 tx_flags)
808 /* set type for advanced descriptor with frame checksum insertion */
809 u32 cmd_type = IGC_ADVTXD_DTYP_DATA |
810 IGC_ADVTXD_DCMD_DEXT |
811 IGC_ADVTXD_DCMD_IFCS;
816 static void igc_tx_olinfo_status(struct igc_ring *tx_ring,
817 union igc_adv_tx_desc *tx_desc,
818 u32 tx_flags, unsigned int paylen)
820 u32 olinfo_status = paylen << IGC_ADVTXD_PAYLEN_SHIFT;
822 /* insert L4 checksum */
823 olinfo_status |= (tx_flags & IGC_TX_FLAGS_CSUM) *
824 ((IGC_TXD_POPTS_TXSM << 8) /
827 /* insert IPv4 checksum */
828 olinfo_status |= (tx_flags & IGC_TX_FLAGS_IPV4) *
829 (((IGC_TXD_POPTS_IXSM << 8)) /
832 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
835 static int igc_tx_map(struct igc_ring *tx_ring,
836 struct igc_tx_buffer *first,
839 struct sk_buff *skb = first->skb;
840 struct igc_tx_buffer *tx_buffer;
841 union igc_adv_tx_desc *tx_desc;
842 u32 tx_flags = first->tx_flags;
843 struct skb_frag_struct *frag;
844 u16 i = tx_ring->next_to_use;
845 unsigned int data_len, size;
847 u32 cmd_type = igc_tx_cmd_type(skb, tx_flags);
849 tx_desc = IGC_TX_DESC(tx_ring, i);
851 igc_tx_olinfo_status(tx_ring, tx_desc, tx_flags, skb->len - hdr_len);
853 size = skb_headlen(skb);
854 data_len = skb->data_len;
856 dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
860 for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
861 if (dma_mapping_error(tx_ring->dev, dma))
864 /* record length, and DMA address */
865 dma_unmap_len_set(tx_buffer, len, size);
866 dma_unmap_addr_set(tx_buffer, dma, dma);
868 tx_desc->read.buffer_addr = cpu_to_le64(dma);
870 while (unlikely(size > IGC_MAX_DATA_PER_TXD)) {
871 tx_desc->read.cmd_type_len =
872 cpu_to_le32(cmd_type ^ IGC_MAX_DATA_PER_TXD);
876 if (i == tx_ring->count) {
877 tx_desc = IGC_TX_DESC(tx_ring, 0);
880 tx_desc->read.olinfo_status = 0;
882 dma += IGC_MAX_DATA_PER_TXD;
883 size -= IGC_MAX_DATA_PER_TXD;
885 tx_desc->read.buffer_addr = cpu_to_le64(dma);
888 if (likely(!data_len))
891 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type ^ size);
895 if (i == tx_ring->count) {
896 tx_desc = IGC_TX_DESC(tx_ring, 0);
899 tx_desc->read.olinfo_status = 0;
901 size = skb_frag_size(frag);
904 dma = skb_frag_dma_map(tx_ring->dev, frag, 0,
905 size, DMA_TO_DEVICE);
907 tx_buffer = &tx_ring->tx_buffer_info[i];
910 /* write last descriptor with RS and EOP bits */
911 cmd_type |= size | IGC_TXD_DCMD;
912 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type);
914 netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount);
916 /* set the timestamp */
917 first->time_stamp = jiffies;
919 skb_tx_timestamp(skb);
921 /* Force memory writes to complete before letting h/w know there
922 * are new descriptors to fetch. (Only applicable for weak-ordered
923 * memory model archs, such as IA-64).
925 * We also need this memory barrier to make certain all of the
926 * status bits have been updated before next_to_watch is written.
930 /* set next_to_watch value indicating a packet is present */
931 first->next_to_watch = tx_desc;
934 if (i == tx_ring->count)
937 tx_ring->next_to_use = i;
939 /* Make sure there is space in the ring for the next send. */
940 igc_maybe_stop_tx(tx_ring, DESC_NEEDED);
942 if (netif_xmit_stopped(txring_txq(tx_ring)) || !skb->xmit_more) {
943 writel(i, tx_ring->tail);
945 /* we need this if more than one processor can write to our tail
946 * at a time, it synchronizes IO on IA64/Altix systems
953 dev_err(tx_ring->dev, "TX DMA map failed\n");
954 tx_buffer = &tx_ring->tx_buffer_info[i];
956 /* clear dma mappings for failed tx_buffer_info map */
957 while (tx_buffer != first) {
958 if (dma_unmap_len(tx_buffer, len))
959 dma_unmap_page(tx_ring->dev,
960 dma_unmap_addr(tx_buffer, dma),
961 dma_unmap_len(tx_buffer, len),
963 dma_unmap_len_set(tx_buffer, len, 0);
967 tx_buffer = &tx_ring->tx_buffer_info[i];
970 if (dma_unmap_len(tx_buffer, len))
971 dma_unmap_single(tx_ring->dev,
972 dma_unmap_addr(tx_buffer, dma),
973 dma_unmap_len(tx_buffer, len),
975 dma_unmap_len_set(tx_buffer, len, 0);
977 dev_kfree_skb_any(tx_buffer->skb);
978 tx_buffer->skb = NULL;
980 tx_ring->next_to_use = i;
985 static netdev_tx_t igc_xmit_frame_ring(struct sk_buff *skb,
986 struct igc_ring *tx_ring)
988 u16 count = TXD_USE_COUNT(skb_headlen(skb));
989 __be16 protocol = vlan_get_protocol(skb);
990 struct igc_tx_buffer *first;
995 /* need: 1 descriptor per page * PAGE_SIZE/IGC_MAX_DATA_PER_TXD,
996 * + 1 desc for skb_headlen/IGC_MAX_DATA_PER_TXD,
997 * + 2 desc gap to keep tail from touching head,
998 * + 1 desc for context descriptor,
999 * otherwise try next time
1001 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
1002 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
1004 if (igc_maybe_stop_tx(tx_ring, count + 3)) {
1005 /* this is a hard error */
1006 return NETDEV_TX_BUSY;
1009 /* record the location of the first descriptor for this packet */
1010 first = &tx_ring->tx_buffer_info[tx_ring->next_to_use];
1012 first->bytecount = skb->len;
1013 first->gso_segs = 1;
1015 /* record initial flags and protocol */
1016 first->tx_flags = tx_flags;
1017 first->protocol = protocol;
1019 igc_tx_csum(tx_ring, first);
1021 igc_tx_map(tx_ring, first, hdr_len);
1023 return NETDEV_TX_OK;
1026 static inline struct igc_ring *igc_tx_queue_mapping(struct igc_adapter *adapter,
1027 struct sk_buff *skb)
1029 unsigned int r_idx = skb->queue_mapping;
1031 if (r_idx >= adapter->num_tx_queues)
1032 r_idx = r_idx % adapter->num_tx_queues;
1034 return adapter->tx_ring[r_idx];
1037 static netdev_tx_t igc_xmit_frame(struct sk_buff *skb,
1038 struct net_device *netdev)
1040 struct igc_adapter *adapter = netdev_priv(netdev);
1042 /* The minimum packet size with TCTL.PSP set is 17 so pad the skb
1043 * in order to meet this minimum size requirement.
1045 if (skb->len < 17) {
1046 if (skb_padto(skb, 17))
1047 return NETDEV_TX_OK;
1051 return igc_xmit_frame_ring(skb, igc_tx_queue_mapping(adapter, skb));
1054 static inline void igc_rx_hash(struct igc_ring *ring,
1055 union igc_adv_rx_desc *rx_desc,
1056 struct sk_buff *skb)
1058 if (ring->netdev->features & NETIF_F_RXHASH)
1060 le32_to_cpu(rx_desc->wb.lower.hi_dword.rss),
1065 * igc_process_skb_fields - Populate skb header fields from Rx descriptor
1066 * @rx_ring: rx descriptor ring packet is being transacted on
1067 * @rx_desc: pointer to the EOP Rx descriptor
1068 * @skb: pointer to current skb being populated
1070 * This function checks the ring, descriptor, and packet information in
1071 * order to populate the hash, checksum, VLAN, timestamp, protocol, and
1072 * other fields within the skb.
1074 static void igc_process_skb_fields(struct igc_ring *rx_ring,
1075 union igc_adv_rx_desc *rx_desc,
1076 struct sk_buff *skb)
1078 igc_rx_hash(rx_ring, rx_desc, skb);
1080 skb_record_rx_queue(skb, rx_ring->queue_index);
1082 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
1085 static struct igc_rx_buffer *igc_get_rx_buffer(struct igc_ring *rx_ring,
1086 const unsigned int size)
1088 struct igc_rx_buffer *rx_buffer;
1090 rx_buffer = &rx_ring->rx_buffer_info[rx_ring->next_to_clean];
1091 prefetchw(rx_buffer->page);
1093 /* we are reusing so sync this buffer for CPU use */
1094 dma_sync_single_range_for_cpu(rx_ring->dev,
1096 rx_buffer->page_offset,
1100 rx_buffer->pagecnt_bias--;
1106 * igc_add_rx_frag - Add contents of Rx buffer to sk_buff
1107 * @rx_ring: rx descriptor ring to transact packets on
1108 * @rx_buffer: buffer containing page to add
1109 * @skb: sk_buff to place the data into
1110 * @size: size of buffer to be added
1112 * This function will add the data contained in rx_buffer->page to the skb.
1114 static void igc_add_rx_frag(struct igc_ring *rx_ring,
1115 struct igc_rx_buffer *rx_buffer,
1116 struct sk_buff *skb,
1119 #if (PAGE_SIZE < 8192)
1120 unsigned int truesize = igc_rx_pg_size(rx_ring) / 2;
1122 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_buffer->page,
1123 rx_buffer->page_offset, size, truesize);
1124 rx_buffer->page_offset ^= truesize;
1126 unsigned int truesize = ring_uses_build_skb(rx_ring) ?
1127 SKB_DATA_ALIGN(IGC_SKB_PAD + size) :
1128 SKB_DATA_ALIGN(size);
1129 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_buffer->page,
1130 rx_buffer->page_offset, size, truesize);
1131 rx_buffer->page_offset += truesize;
1135 static struct sk_buff *igc_build_skb(struct igc_ring *rx_ring,
1136 struct igc_rx_buffer *rx_buffer,
1137 union igc_adv_rx_desc *rx_desc,
1140 void *va = page_address(rx_buffer->page) + rx_buffer->page_offset;
1141 #if (PAGE_SIZE < 8192)
1142 unsigned int truesize = igc_rx_pg_size(rx_ring) / 2;
1144 unsigned int truesize = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
1145 SKB_DATA_ALIGN(IGC_SKB_PAD + size);
1147 struct sk_buff *skb;
1149 /* prefetch first cache line of first page */
1151 #if L1_CACHE_BYTES < 128
1152 prefetch(va + L1_CACHE_BYTES);
1155 /* build an skb around the page buffer */
1156 skb = build_skb(va - IGC_SKB_PAD, truesize);
1160 /* update pointers within the skb to store the data */
1161 skb_reserve(skb, IGC_SKB_PAD);
1162 __skb_put(skb, size);
1164 /* update buffer offset */
1165 #if (PAGE_SIZE < 8192)
1166 rx_buffer->page_offset ^= truesize;
1168 rx_buffer->page_offset += truesize;
1174 static struct sk_buff *igc_construct_skb(struct igc_ring *rx_ring,
1175 struct igc_rx_buffer *rx_buffer,
1176 union igc_adv_rx_desc *rx_desc,
1179 void *va = page_address(rx_buffer->page) + rx_buffer->page_offset;
1180 #if (PAGE_SIZE < 8192)
1181 unsigned int truesize = igc_rx_pg_size(rx_ring) / 2;
1183 unsigned int truesize = SKB_DATA_ALIGN(size);
1185 unsigned int headlen;
1186 struct sk_buff *skb;
1188 /* prefetch first cache line of first page */
1190 #if L1_CACHE_BYTES < 128
1191 prefetch(va + L1_CACHE_BYTES);
1194 /* allocate a skb to store the frags */
1195 skb = napi_alloc_skb(&rx_ring->q_vector->napi, IGC_RX_HDR_LEN);
1199 /* Determine available headroom for copy */
1201 if (headlen > IGC_RX_HDR_LEN)
1202 headlen = eth_get_headlen(va, IGC_RX_HDR_LEN);
1204 /* align pull length to size of long to optimize memcpy performance */
1205 memcpy(__skb_put(skb, headlen), va, ALIGN(headlen, sizeof(long)));
1207 /* update all of the pointers */
1210 skb_add_rx_frag(skb, 0, rx_buffer->page,
1211 (va + headlen) - page_address(rx_buffer->page),
1213 #if (PAGE_SIZE < 8192)
1214 rx_buffer->page_offset ^= truesize;
1216 rx_buffer->page_offset += truesize;
1219 rx_buffer->pagecnt_bias++;
1226 * igc_reuse_rx_page - page flip buffer and store it back on the ring
1227 * @rx_ring: rx descriptor ring to store buffers on
1228 * @old_buff: donor buffer to have page reused
1230 * Synchronizes page for reuse by the adapter
1232 static void igc_reuse_rx_page(struct igc_ring *rx_ring,
1233 struct igc_rx_buffer *old_buff)
1235 u16 nta = rx_ring->next_to_alloc;
1236 struct igc_rx_buffer *new_buff;
1238 new_buff = &rx_ring->rx_buffer_info[nta];
1240 /* update, and store next to alloc */
1242 rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
1244 /* Transfer page from old buffer to new buffer.
1245 * Move each member individually to avoid possible store
1246 * forwarding stalls.
1248 new_buff->dma = old_buff->dma;
1249 new_buff->page = old_buff->page;
1250 new_buff->page_offset = old_buff->page_offset;
1251 new_buff->pagecnt_bias = old_buff->pagecnt_bias;
1254 static inline bool igc_page_is_reserved(struct page *page)
1256 return (page_to_nid(page) != numa_mem_id()) || page_is_pfmemalloc(page);
1259 static bool igc_can_reuse_rx_page(struct igc_rx_buffer *rx_buffer)
1261 unsigned int pagecnt_bias = rx_buffer->pagecnt_bias;
1262 struct page *page = rx_buffer->page;
1264 /* avoid re-using remote pages */
1265 if (unlikely(igc_page_is_reserved(page)))
1268 #if (PAGE_SIZE < 8192)
1269 /* if we are only owner of page we can reuse it */
1270 if (unlikely((page_ref_count(page) - pagecnt_bias) > 1))
1273 #define IGC_LAST_OFFSET \
1274 (SKB_WITH_OVERHEAD(PAGE_SIZE) - IGC_RXBUFFER_2048)
1276 if (rx_buffer->page_offset > IGC_LAST_OFFSET)
1280 /* If we have drained the page fragment pool we need to update
1281 * the pagecnt_bias and page count so that we fully restock the
1282 * number of references the driver holds.
1284 if (unlikely(!pagecnt_bias)) {
1285 page_ref_add(page, USHRT_MAX);
1286 rx_buffer->pagecnt_bias = USHRT_MAX;
1293 * igc_is_non_eop - process handling of non-EOP buffers
1294 * @rx_ring: Rx ring being processed
1295 * @rx_desc: Rx descriptor for current buffer
1296 * @skb: current socket buffer containing buffer in progress
1298 * This function updates next to clean. If the buffer is an EOP buffer
1299 * this function exits returning false, otherwise it will place the
1300 * sk_buff in the next buffer to be chained and return true indicating
1301 * that this is in fact a non-EOP buffer.
1303 static bool igc_is_non_eop(struct igc_ring *rx_ring,
1304 union igc_adv_rx_desc *rx_desc)
1306 u32 ntc = rx_ring->next_to_clean + 1;
1308 /* fetch, update, and store next to clean */
1309 ntc = (ntc < rx_ring->count) ? ntc : 0;
1310 rx_ring->next_to_clean = ntc;
1312 prefetch(IGC_RX_DESC(rx_ring, ntc));
1314 if (likely(igc_test_staterr(rx_desc, IGC_RXD_STAT_EOP)))
1321 * igc_cleanup_headers - Correct corrupted or empty headers
1322 * @rx_ring: rx descriptor ring packet is being transacted on
1323 * @rx_desc: pointer to the EOP Rx descriptor
1324 * @skb: pointer to current skb being fixed
1326 * Address the case where we are pulling data in on pages only
1327 * and as such no data is present in the skb header.
1329 * In addition if skb is not at least 60 bytes we need to pad it so that
1330 * it is large enough to qualify as a valid Ethernet frame.
1332 * Returns true if an error was encountered and skb was freed.
1334 static bool igc_cleanup_headers(struct igc_ring *rx_ring,
1335 union igc_adv_rx_desc *rx_desc,
1336 struct sk_buff *skb)
1338 if (unlikely((igc_test_staterr(rx_desc,
1339 IGC_RXDEXT_ERR_FRAME_ERR_MASK)))) {
1340 struct net_device *netdev = rx_ring->netdev;
1342 if (!(netdev->features & NETIF_F_RXALL)) {
1343 dev_kfree_skb_any(skb);
1348 /* if eth_skb_pad returns an error the skb was freed */
1349 if (eth_skb_pad(skb))
1355 static void igc_put_rx_buffer(struct igc_ring *rx_ring,
1356 struct igc_rx_buffer *rx_buffer)
1358 if (igc_can_reuse_rx_page(rx_buffer)) {
1359 /* hand second half of page back to the ring */
1360 igc_reuse_rx_page(rx_ring, rx_buffer);
1362 /* We are not reusing the buffer so unmap it and free
1363 * any references we are holding to it
1365 dma_unmap_page_attrs(rx_ring->dev, rx_buffer->dma,
1366 igc_rx_pg_size(rx_ring), DMA_FROM_DEVICE,
1368 __page_frag_cache_drain(rx_buffer->page,
1369 rx_buffer->pagecnt_bias);
1372 /* clear contents of rx_buffer */
1373 rx_buffer->page = NULL;
1377 * igc_alloc_rx_buffers - Replace used receive buffers; packet split
1378 * @adapter: address of board private structure
1380 static void igc_alloc_rx_buffers(struct igc_ring *rx_ring, u16 cleaned_count)
1382 union igc_adv_rx_desc *rx_desc;
1383 u16 i = rx_ring->next_to_use;
1384 struct igc_rx_buffer *bi;
1391 rx_desc = IGC_RX_DESC(rx_ring, i);
1392 bi = &rx_ring->rx_buffer_info[i];
1393 i -= rx_ring->count;
1395 bufsz = igc_rx_bufsz(rx_ring);
1398 if (!igc_alloc_mapped_page(rx_ring, bi))
1401 /* sync the buffer for use by the device */
1402 dma_sync_single_range_for_device(rx_ring->dev, bi->dma,
1403 bi->page_offset, bufsz,
1406 /* Refresh the desc even if buffer_addrs didn't change
1407 * because each write-back erases this info.
1409 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
1415 rx_desc = IGC_RX_DESC(rx_ring, 0);
1416 bi = rx_ring->rx_buffer_info;
1417 i -= rx_ring->count;
1420 /* clear the length for the next_to_use descriptor */
1421 rx_desc->wb.upper.length = 0;
1424 } while (cleaned_count);
1426 i += rx_ring->count;
1428 if (rx_ring->next_to_use != i) {
1429 /* record the next descriptor to use */
1430 rx_ring->next_to_use = i;
1432 /* update next to alloc since we have filled the ring */
1433 rx_ring->next_to_alloc = i;
1435 /* Force memory writes to complete before letting h/w
1436 * know there are new descriptors to fetch. (Only
1437 * applicable for weak-ordered memory model archs,
1441 writel(i, rx_ring->tail);
1445 static int igc_clean_rx_irq(struct igc_q_vector *q_vector, const int budget)
1447 unsigned int total_bytes = 0, total_packets = 0;
1448 struct igc_ring *rx_ring = q_vector->rx.ring;
1449 struct sk_buff *skb = rx_ring->skb;
1450 u16 cleaned_count = igc_desc_unused(rx_ring);
1452 while (likely(total_packets < budget)) {
1453 union igc_adv_rx_desc *rx_desc;
1454 struct igc_rx_buffer *rx_buffer;
1457 /* return some buffers to hardware, one at a time is too slow */
1458 if (cleaned_count >= IGC_RX_BUFFER_WRITE) {
1459 igc_alloc_rx_buffers(rx_ring, cleaned_count);
1463 rx_desc = IGC_RX_DESC(rx_ring, rx_ring->next_to_clean);
1464 size = le16_to_cpu(rx_desc->wb.upper.length);
1468 /* This memory barrier is needed to keep us from reading
1469 * any other fields out of the rx_desc until we know the
1470 * descriptor has been written back
1474 rx_buffer = igc_get_rx_buffer(rx_ring, size);
1476 /* retrieve a buffer from the ring */
1478 igc_add_rx_frag(rx_ring, rx_buffer, skb, size);
1479 else if (ring_uses_build_skb(rx_ring))
1480 skb = igc_build_skb(rx_ring, rx_buffer, rx_desc, size);
1482 skb = igc_construct_skb(rx_ring, rx_buffer,
1485 /* exit if we failed to retrieve a buffer */
1487 rx_ring->rx_stats.alloc_failed++;
1488 rx_buffer->pagecnt_bias++;
1492 igc_put_rx_buffer(rx_ring, rx_buffer);
1495 /* fetch next buffer in frame if non-eop */
1496 if (igc_is_non_eop(rx_ring, rx_desc))
1499 /* verify the packet layout is correct */
1500 if (igc_cleanup_headers(rx_ring, rx_desc, skb)) {
1505 /* probably a little skewed due to removing CRC */
1506 total_bytes += skb->len;
1508 /* populate checksum, timestamp, VLAN, and protocol */
1509 igc_process_skb_fields(rx_ring, rx_desc, skb);
1511 napi_gro_receive(&q_vector->napi, skb);
1513 /* reset skb pointer */
1516 /* update budget accounting */
1520 /* place incomplete frames back on ring for completion */
1523 u64_stats_update_begin(&rx_ring->rx_syncp);
1524 rx_ring->rx_stats.packets += total_packets;
1525 rx_ring->rx_stats.bytes += total_bytes;
1526 u64_stats_update_end(&rx_ring->rx_syncp);
1527 q_vector->rx.total_packets += total_packets;
1528 q_vector->rx.total_bytes += total_bytes;
1531 igc_alloc_rx_buffers(rx_ring, cleaned_count);
1533 return total_packets;
1536 static inline unsigned int igc_rx_offset(struct igc_ring *rx_ring)
1538 return ring_uses_build_skb(rx_ring) ? IGC_SKB_PAD : 0;
1541 static bool igc_alloc_mapped_page(struct igc_ring *rx_ring,
1542 struct igc_rx_buffer *bi)
1544 struct page *page = bi->page;
1547 /* since we are recycling buffers we should seldom need to alloc */
1551 /* alloc new page for storage */
1552 page = dev_alloc_pages(igc_rx_pg_order(rx_ring));
1553 if (unlikely(!page)) {
1554 rx_ring->rx_stats.alloc_failed++;
1558 /* map page for use */
1559 dma = dma_map_page_attrs(rx_ring->dev, page, 0,
1560 igc_rx_pg_size(rx_ring),
1564 /* if mapping failed free memory back to system since
1565 * there isn't much point in holding memory we can't use
1567 if (dma_mapping_error(rx_ring->dev, dma)) {
1570 rx_ring->rx_stats.alloc_failed++;
1576 bi->page_offset = igc_rx_offset(rx_ring);
1577 bi->pagecnt_bias = 1;
1583 * igc_clean_tx_irq - Reclaim resources after transmit completes
1584 * @q_vector: pointer to q_vector containing needed info
1585 * @napi_budget: Used to determine if we are in netpoll
1587 * returns true if ring is completely cleaned
1589 static bool igc_clean_tx_irq(struct igc_q_vector *q_vector, int napi_budget)
1591 struct igc_adapter *adapter = q_vector->adapter;
1592 unsigned int total_bytes = 0, total_packets = 0;
1593 unsigned int budget = q_vector->tx.work_limit;
1594 struct igc_ring *tx_ring = q_vector->tx.ring;
1595 unsigned int i = tx_ring->next_to_clean;
1596 struct igc_tx_buffer *tx_buffer;
1597 union igc_adv_tx_desc *tx_desc;
1599 if (test_bit(__IGC_DOWN, &adapter->state))
1602 tx_buffer = &tx_ring->tx_buffer_info[i];
1603 tx_desc = IGC_TX_DESC(tx_ring, i);
1604 i -= tx_ring->count;
1607 union igc_adv_tx_desc *eop_desc = tx_buffer->next_to_watch;
1609 /* if next_to_watch is not set then there is no work pending */
1613 /* prevent any other reads prior to eop_desc */
1616 /* if DD is not set pending work has not been completed */
1617 if (!(eop_desc->wb.status & cpu_to_le32(IGC_TXD_STAT_DD)))
1620 /* clear next_to_watch to prevent false hangs */
1621 tx_buffer->next_to_watch = NULL;
1623 /* update the statistics for this packet */
1624 total_bytes += tx_buffer->bytecount;
1625 total_packets += tx_buffer->gso_segs;
1628 napi_consume_skb(tx_buffer->skb, napi_budget);
1630 /* unmap skb header data */
1631 dma_unmap_single(tx_ring->dev,
1632 dma_unmap_addr(tx_buffer, dma),
1633 dma_unmap_len(tx_buffer, len),
1636 /* clear tx_buffer data */
1637 dma_unmap_len_set(tx_buffer, len, 0);
1639 /* clear last DMA location and unmap remaining buffers */
1640 while (tx_desc != eop_desc) {
1645 i -= tx_ring->count;
1646 tx_buffer = tx_ring->tx_buffer_info;
1647 tx_desc = IGC_TX_DESC(tx_ring, 0);
1650 /* unmap any remaining paged data */
1651 if (dma_unmap_len(tx_buffer, len)) {
1652 dma_unmap_page(tx_ring->dev,
1653 dma_unmap_addr(tx_buffer, dma),
1654 dma_unmap_len(tx_buffer, len),
1656 dma_unmap_len_set(tx_buffer, len, 0);
1660 /* move us one more past the eop_desc for start of next pkt */
1665 i -= tx_ring->count;
1666 tx_buffer = tx_ring->tx_buffer_info;
1667 tx_desc = IGC_TX_DESC(tx_ring, 0);
1670 /* issue prefetch for next Tx descriptor */
1673 /* update budget accounting */
1675 } while (likely(budget));
1677 netdev_tx_completed_queue(txring_txq(tx_ring),
1678 total_packets, total_bytes);
1680 i += tx_ring->count;
1681 tx_ring->next_to_clean = i;
1682 u64_stats_update_begin(&tx_ring->tx_syncp);
1683 tx_ring->tx_stats.bytes += total_bytes;
1684 tx_ring->tx_stats.packets += total_packets;
1685 u64_stats_update_end(&tx_ring->tx_syncp);
1686 q_vector->tx.total_bytes += total_bytes;
1687 q_vector->tx.total_packets += total_packets;
1689 if (test_bit(IGC_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags)) {
1690 struct igc_hw *hw = &adapter->hw;
1692 /* Detect a transmit hang in hardware, this serializes the
1693 * check with the clearing of time_stamp and movement of i
1695 clear_bit(IGC_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags);
1696 if (tx_buffer->next_to_watch &&
1697 time_after(jiffies, tx_buffer->time_stamp +
1698 (adapter->tx_timeout_factor * HZ)) &&
1699 !(rd32(IGC_STATUS) & IGC_STATUS_TXOFF)) {
1700 /* detected Tx unit hang */
1701 dev_err(tx_ring->dev,
1702 "Detected Tx Unit Hang\n"
1706 " next_to_use <%x>\n"
1707 " next_to_clean <%x>\n"
1708 "buffer_info[next_to_clean]\n"
1709 " time_stamp <%lx>\n"
1710 " next_to_watch <%p>\n"
1712 " desc.status <%x>\n",
1713 tx_ring->queue_index,
1714 rd32(IGC_TDH(tx_ring->reg_idx)),
1715 readl(tx_ring->tail),
1716 tx_ring->next_to_use,
1717 tx_ring->next_to_clean,
1718 tx_buffer->time_stamp,
1719 tx_buffer->next_to_watch,
1721 tx_buffer->next_to_watch->wb.status);
1722 netif_stop_subqueue(tx_ring->netdev,
1723 tx_ring->queue_index);
1725 /* we are about to reset, no point in enabling stuff */
1730 #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
1731 if (unlikely(total_packets &&
1732 netif_carrier_ok(tx_ring->netdev) &&
1733 igc_desc_unused(tx_ring) >= TX_WAKE_THRESHOLD)) {
1734 /* Make sure that anybody stopping the queue after this
1735 * sees the new next_to_clean.
1738 if (__netif_subqueue_stopped(tx_ring->netdev,
1739 tx_ring->queue_index) &&
1740 !(test_bit(__IGC_DOWN, &adapter->state))) {
1741 netif_wake_subqueue(tx_ring->netdev,
1742 tx_ring->queue_index);
1744 u64_stats_update_begin(&tx_ring->tx_syncp);
1745 tx_ring->tx_stats.restart_queue++;
1746 u64_stats_update_end(&tx_ring->tx_syncp);
1754 * igc_up - Open the interface and prepare it to handle traffic
1755 * @adapter: board private structure
1757 void igc_up(struct igc_adapter *adapter)
1759 struct igc_hw *hw = &adapter->hw;
1762 /* hardware has been reset, we need to reload some things */
1763 igc_configure(adapter);
1765 clear_bit(__IGC_DOWN, &adapter->state);
1767 for (i = 0; i < adapter->num_q_vectors; i++)
1768 napi_enable(&adapter->q_vector[i]->napi);
1770 if (adapter->msix_entries)
1771 igc_configure_msix(adapter);
1773 igc_assign_vector(adapter->q_vector[0], 0);
1775 /* Clear any pending interrupts. */
1777 igc_irq_enable(adapter);
1779 netif_tx_start_all_queues(adapter->netdev);
1781 /* start the watchdog. */
1782 hw->mac.get_link_status = 1;
1783 schedule_work(&adapter->watchdog_task);
1787 * igc_update_stats - Update the board statistics counters
1788 * @adapter: board private structure
1790 void igc_update_stats(struct igc_adapter *adapter)
1792 struct rtnl_link_stats64 *net_stats = &adapter->stats64;
1793 struct pci_dev *pdev = adapter->pdev;
1794 struct igc_hw *hw = &adapter->hw;
1795 u64 _bytes, _packets;
1801 /* Prevent stats update while adapter is being reset, or if the pci
1802 * connection is down.
1804 if (adapter->link_speed == 0)
1806 if (pci_channel_offline(pdev))
1813 for (i = 0; i < adapter->num_rx_queues; i++) {
1814 struct igc_ring *ring = adapter->rx_ring[i];
1815 u32 rqdpc = rd32(IGC_RQDPC(i));
1817 if (hw->mac.type >= igc_i225)
1818 wr32(IGC_RQDPC(i), 0);
1821 ring->rx_stats.drops += rqdpc;
1822 net_stats->rx_fifo_errors += rqdpc;
1826 start = u64_stats_fetch_begin_irq(&ring->rx_syncp);
1827 _bytes = ring->rx_stats.bytes;
1828 _packets = ring->rx_stats.packets;
1829 } while (u64_stats_fetch_retry_irq(&ring->rx_syncp, start));
1831 packets += _packets;
1834 net_stats->rx_bytes = bytes;
1835 net_stats->rx_packets = packets;
1839 for (i = 0; i < adapter->num_tx_queues; i++) {
1840 struct igc_ring *ring = adapter->tx_ring[i];
1843 start = u64_stats_fetch_begin_irq(&ring->tx_syncp);
1844 _bytes = ring->tx_stats.bytes;
1845 _packets = ring->tx_stats.packets;
1846 } while (u64_stats_fetch_retry_irq(&ring->tx_syncp, start));
1848 packets += _packets;
1850 net_stats->tx_bytes = bytes;
1851 net_stats->tx_packets = packets;
1854 /* read stats registers */
1855 adapter->stats.crcerrs += rd32(IGC_CRCERRS);
1856 adapter->stats.gprc += rd32(IGC_GPRC);
1857 adapter->stats.gorc += rd32(IGC_GORCL);
1858 rd32(IGC_GORCH); /* clear GORCL */
1859 adapter->stats.bprc += rd32(IGC_BPRC);
1860 adapter->stats.mprc += rd32(IGC_MPRC);
1861 adapter->stats.roc += rd32(IGC_ROC);
1863 adapter->stats.prc64 += rd32(IGC_PRC64);
1864 adapter->stats.prc127 += rd32(IGC_PRC127);
1865 adapter->stats.prc255 += rd32(IGC_PRC255);
1866 adapter->stats.prc511 += rd32(IGC_PRC511);
1867 adapter->stats.prc1023 += rd32(IGC_PRC1023);
1868 adapter->stats.prc1522 += rd32(IGC_PRC1522);
1869 adapter->stats.symerrs += rd32(IGC_SYMERRS);
1870 adapter->stats.sec += rd32(IGC_SEC);
1872 mpc = rd32(IGC_MPC);
1873 adapter->stats.mpc += mpc;
1874 net_stats->rx_fifo_errors += mpc;
1875 adapter->stats.scc += rd32(IGC_SCC);
1876 adapter->stats.ecol += rd32(IGC_ECOL);
1877 adapter->stats.mcc += rd32(IGC_MCC);
1878 adapter->stats.latecol += rd32(IGC_LATECOL);
1879 adapter->stats.dc += rd32(IGC_DC);
1880 adapter->stats.rlec += rd32(IGC_RLEC);
1881 adapter->stats.xonrxc += rd32(IGC_XONRXC);
1882 adapter->stats.xontxc += rd32(IGC_XONTXC);
1883 adapter->stats.xoffrxc += rd32(IGC_XOFFRXC);
1884 adapter->stats.xofftxc += rd32(IGC_XOFFTXC);
1885 adapter->stats.fcruc += rd32(IGC_FCRUC);
1886 adapter->stats.gptc += rd32(IGC_GPTC);
1887 adapter->stats.gotc += rd32(IGC_GOTCL);
1888 rd32(IGC_GOTCH); /* clear GOTCL */
1889 adapter->stats.rnbc += rd32(IGC_RNBC);
1890 adapter->stats.ruc += rd32(IGC_RUC);
1891 adapter->stats.rfc += rd32(IGC_RFC);
1892 adapter->stats.rjc += rd32(IGC_RJC);
1893 adapter->stats.tor += rd32(IGC_TORH);
1894 adapter->stats.tot += rd32(IGC_TOTH);
1895 adapter->stats.tpr += rd32(IGC_TPR);
1897 adapter->stats.ptc64 += rd32(IGC_PTC64);
1898 adapter->stats.ptc127 += rd32(IGC_PTC127);
1899 adapter->stats.ptc255 += rd32(IGC_PTC255);
1900 adapter->stats.ptc511 += rd32(IGC_PTC511);
1901 adapter->stats.ptc1023 += rd32(IGC_PTC1023);
1902 adapter->stats.ptc1522 += rd32(IGC_PTC1522);
1904 adapter->stats.mptc += rd32(IGC_MPTC);
1905 adapter->stats.bptc += rd32(IGC_BPTC);
1907 adapter->stats.tpt += rd32(IGC_TPT);
1908 adapter->stats.colc += rd32(IGC_COLC);
1910 adapter->stats.algnerrc += rd32(IGC_ALGNERRC);
1912 adapter->stats.tsctc += rd32(IGC_TSCTC);
1913 adapter->stats.tsctfc += rd32(IGC_TSCTFC);
1915 adapter->stats.iac += rd32(IGC_IAC);
1916 adapter->stats.icrxoc += rd32(IGC_ICRXOC);
1917 adapter->stats.icrxptc += rd32(IGC_ICRXPTC);
1918 adapter->stats.icrxatc += rd32(IGC_ICRXATC);
1919 adapter->stats.ictxptc += rd32(IGC_ICTXPTC);
1920 adapter->stats.ictxatc += rd32(IGC_ICTXATC);
1921 adapter->stats.ictxqec += rd32(IGC_ICTXQEC);
1922 adapter->stats.ictxqmtc += rd32(IGC_ICTXQMTC);
1923 adapter->stats.icrxdmtc += rd32(IGC_ICRXDMTC);
1925 /* Fill out the OS statistics structure */
1926 net_stats->multicast = adapter->stats.mprc;
1927 net_stats->collisions = adapter->stats.colc;
1931 /* RLEC on some newer hardware can be incorrect so build
1932 * our own version based on RUC and ROC
1934 net_stats->rx_errors = adapter->stats.rxerrc +
1935 adapter->stats.crcerrs + adapter->stats.algnerrc +
1936 adapter->stats.ruc + adapter->stats.roc +
1937 adapter->stats.cexterr;
1938 net_stats->rx_length_errors = adapter->stats.ruc +
1940 net_stats->rx_crc_errors = adapter->stats.crcerrs;
1941 net_stats->rx_frame_errors = adapter->stats.algnerrc;
1942 net_stats->rx_missed_errors = adapter->stats.mpc;
1945 net_stats->tx_errors = adapter->stats.ecol +
1946 adapter->stats.latecol;
1947 net_stats->tx_aborted_errors = adapter->stats.ecol;
1948 net_stats->tx_window_errors = adapter->stats.latecol;
1949 net_stats->tx_carrier_errors = adapter->stats.tncrs;
1951 /* Tx Dropped needs to be maintained elsewhere */
1953 /* Management Stats */
1954 adapter->stats.mgptc += rd32(IGC_MGTPTC);
1955 adapter->stats.mgprc += rd32(IGC_MGTPRC);
1956 adapter->stats.mgpdc += rd32(IGC_MGTPDC);
1959 static void igc_nfc_filter_exit(struct igc_adapter *adapter)
1961 struct igc_nfc_filter *rule;
1963 spin_lock(&adapter->nfc_lock);
1965 hlist_for_each_entry(rule, &adapter->nfc_filter_list, nfc_node)
1966 igc_erase_filter(adapter, rule);
1968 hlist_for_each_entry(rule, &adapter->cls_flower_list, nfc_node)
1969 igc_erase_filter(adapter, rule);
1971 spin_unlock(&adapter->nfc_lock);
1974 static void igc_nfc_filter_restore(struct igc_adapter *adapter)
1976 struct igc_nfc_filter *rule;
1978 spin_lock(&adapter->nfc_lock);
1980 hlist_for_each_entry(rule, &adapter->nfc_filter_list, nfc_node)
1981 igc_add_filter(adapter, rule);
1983 spin_unlock(&adapter->nfc_lock);
1987 * igc_down - Close the interface
1988 * @adapter: board private structure
1990 void igc_down(struct igc_adapter *adapter)
1992 struct net_device *netdev = adapter->netdev;
1993 struct igc_hw *hw = &adapter->hw;
1997 set_bit(__IGC_DOWN, &adapter->state);
1999 /* disable receives in the hardware */
2000 rctl = rd32(IGC_RCTL);
2001 wr32(IGC_RCTL, rctl & ~IGC_RCTL_EN);
2002 /* flush and sleep below */
2004 igc_nfc_filter_exit(adapter);
2006 /* set trans_start so we don't get spurious watchdogs during reset */
2007 netif_trans_update(netdev);
2009 netif_carrier_off(netdev);
2010 netif_tx_stop_all_queues(netdev);
2012 /* disable transmits in the hardware */
2013 tctl = rd32(IGC_TCTL);
2014 tctl &= ~IGC_TCTL_EN;
2015 wr32(IGC_TCTL, tctl);
2016 /* flush both disables and wait for them to finish */
2018 usleep_range(10000, 20000);
2020 igc_irq_disable(adapter);
2022 adapter->flags &= ~IGC_FLAG_NEED_LINK_UPDATE;
2024 for (i = 0; i < adapter->num_q_vectors; i++) {
2025 if (adapter->q_vector[i]) {
2026 napi_synchronize(&adapter->q_vector[i]->napi);
2027 napi_disable(&adapter->q_vector[i]->napi);
2031 del_timer_sync(&adapter->watchdog_timer);
2032 del_timer_sync(&adapter->phy_info_timer);
2034 /* record the stats before reset*/
2035 spin_lock(&adapter->stats64_lock);
2036 igc_update_stats(adapter);
2037 spin_unlock(&adapter->stats64_lock);
2039 adapter->link_speed = 0;
2040 adapter->link_duplex = 0;
2042 if (!pci_channel_offline(adapter->pdev))
2045 /* clear VLAN promisc flag so VFTA will be updated if necessary */
2046 adapter->flags &= ~IGC_FLAG_VLAN_PROMISC;
2048 igc_clean_all_tx_rings(adapter);
2049 igc_clean_all_rx_rings(adapter);
2052 void igc_reinit_locked(struct igc_adapter *adapter)
2054 WARN_ON(in_interrupt());
2055 while (test_and_set_bit(__IGC_RESETTING, &adapter->state))
2056 usleep_range(1000, 2000);
2059 clear_bit(__IGC_RESETTING, &adapter->state);
2062 static void igc_reset_task(struct work_struct *work)
2064 struct igc_adapter *adapter;
2066 adapter = container_of(work, struct igc_adapter, reset_task);
2068 netdev_err(adapter->netdev, "Reset adapter\n");
2069 igc_reinit_locked(adapter);
2073 * igc_change_mtu - Change the Maximum Transfer Unit
2074 * @netdev: network interface device structure
2075 * @new_mtu: new value for maximum frame size
2077 * Returns 0 on success, negative on failure
2079 static int igc_change_mtu(struct net_device *netdev, int new_mtu)
2081 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2082 struct igc_adapter *adapter = netdev_priv(netdev);
2083 struct pci_dev *pdev = adapter->pdev;
2085 /* adjust max frame to be at least the size of a standard frame */
2086 if (max_frame < (ETH_FRAME_LEN + ETH_FCS_LEN))
2087 max_frame = ETH_FRAME_LEN + ETH_FCS_LEN;
2089 while (test_and_set_bit(__IGC_RESETTING, &adapter->state))
2090 usleep_range(1000, 2000);
2092 /* igc_down has a dependency on max_frame_size */
2093 adapter->max_frame_size = max_frame;
2095 if (netif_running(netdev))
2098 dev_info(&pdev->dev, "changing MTU from %d to %d\n",
2099 netdev->mtu, new_mtu);
2100 netdev->mtu = new_mtu;
2102 if (netif_running(netdev))
2107 clear_bit(__IGC_RESETTING, &adapter->state);
2113 * igc_get_stats - Get System Network Statistics
2114 * @netdev: network interface device structure
2116 * Returns the address of the device statistics structure.
2117 * The statistics are updated here and also from the timer callback.
2119 static struct net_device_stats *igc_get_stats(struct net_device *netdev)
2121 struct igc_adapter *adapter = netdev_priv(netdev);
2123 if (!test_bit(__IGC_RESETTING, &adapter->state))
2124 igc_update_stats(adapter);
2126 /* only return the current stats */
2127 return &netdev->stats;
2130 static netdev_features_t igc_fix_features(struct net_device *netdev,
2131 netdev_features_t features)
2133 /* Since there is no support for separate Rx/Tx vlan accel
2134 * enable/disable make sure Tx flag is always in same state as Rx.
2136 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2137 features |= NETIF_F_HW_VLAN_CTAG_TX;
2139 features &= ~NETIF_F_HW_VLAN_CTAG_TX;
2144 static int igc_set_features(struct net_device *netdev,
2145 netdev_features_t features)
2147 netdev_features_t changed = netdev->features ^ features;
2148 struct igc_adapter *adapter = netdev_priv(netdev);
2150 /* Add VLAN support */
2151 if (!(changed & (NETIF_F_RXALL | NETIF_F_NTUPLE)))
2154 if (!(features & NETIF_F_NTUPLE)) {
2155 struct hlist_node *node2;
2156 struct igc_nfc_filter *rule;
2158 spin_lock(&adapter->nfc_lock);
2159 hlist_for_each_entry_safe(rule, node2,
2160 &adapter->nfc_filter_list, nfc_node) {
2161 igc_erase_filter(adapter, rule);
2162 hlist_del(&rule->nfc_node);
2165 spin_unlock(&adapter->nfc_lock);
2166 adapter->nfc_filter_count = 0;
2169 netdev->features = features;
2171 if (netif_running(netdev))
2172 igc_reinit_locked(adapter);
2179 static netdev_features_t
2180 igc_features_check(struct sk_buff *skb, struct net_device *dev,
2181 netdev_features_t features)
2183 unsigned int network_hdr_len, mac_hdr_len;
2185 /* Make certain the headers can be described by a context descriptor */
2186 mac_hdr_len = skb_network_header(skb) - skb->data;
2187 if (unlikely(mac_hdr_len > IGC_MAX_MAC_HDR_LEN))
2188 return features & ~(NETIF_F_HW_CSUM |
2190 NETIF_F_HW_VLAN_CTAG_TX |
2194 network_hdr_len = skb_checksum_start(skb) - skb_network_header(skb);
2195 if (unlikely(network_hdr_len > IGC_MAX_NETWORK_HDR_LEN))
2196 return features & ~(NETIF_F_HW_CSUM |
2201 /* We can only support IPv4 TSO in tunnels if we can mangle the
2202 * inner IP ID field, so strip TSO if MANGLEID is not supported.
2204 if (skb->encapsulation && !(features & NETIF_F_TSO_MANGLEID))
2205 features &= ~NETIF_F_TSO;
2211 * igc_configure - configure the hardware for RX and TX
2212 * @adapter: private board structure
2214 static void igc_configure(struct igc_adapter *adapter)
2216 struct net_device *netdev = adapter->netdev;
2219 igc_get_hw_control(adapter);
2220 igc_set_rx_mode(netdev);
2222 igc_setup_tctl(adapter);
2223 igc_setup_mrqc(adapter);
2224 igc_setup_rctl(adapter);
2226 igc_nfc_filter_restore(adapter);
2227 igc_configure_tx(adapter);
2228 igc_configure_rx(adapter);
2230 igc_rx_fifo_flush_base(&adapter->hw);
2232 /* call igc_desc_unused which always leaves
2233 * at least 1 descriptor unused to make sure
2234 * next_to_use != next_to_clean
2236 for (i = 0; i < adapter->num_rx_queues; i++) {
2237 struct igc_ring *ring = adapter->rx_ring[i];
2239 igc_alloc_rx_buffers(ring, igc_desc_unused(ring));
2244 * igc_rar_set_index - Sync RAL[index] and RAH[index] registers with MAC table
2245 * @adapter: address of board private structure
2246 * @index: Index of the RAR entry which need to be synced with MAC table
2248 static void igc_rar_set_index(struct igc_adapter *adapter, u32 index)
2250 u8 *addr = adapter->mac_table[index].addr;
2251 struct igc_hw *hw = &adapter->hw;
2252 u32 rar_low, rar_high;
2254 /* HW expects these to be in network order when they are plugged
2255 * into the registers which are little endian. In order to guarantee
2256 * that ordering we need to do an leXX_to_cpup here in order to be
2257 * ready for the byteswap that occurs with writel
2259 rar_low = le32_to_cpup((__le32 *)(addr));
2260 rar_high = le16_to_cpup((__le16 *)(addr + 4));
2262 /* Indicate to hardware the Address is Valid. */
2263 if (adapter->mac_table[index].state & IGC_MAC_STATE_IN_USE) {
2264 if (is_valid_ether_addr(addr))
2265 rar_high |= IGC_RAH_AV;
2267 rar_high |= IGC_RAH_POOL_1 <<
2268 adapter->mac_table[index].queue;
2271 wr32(IGC_RAL(index), rar_low);
2273 wr32(IGC_RAH(index), rar_high);
2277 /* Set default MAC address for the PF in the first RAR entry */
2278 static void igc_set_default_mac_filter(struct igc_adapter *adapter)
2280 struct igc_mac_addr *mac_table = &adapter->mac_table[0];
2282 ether_addr_copy(mac_table->addr, adapter->hw.mac.addr);
2283 mac_table->state = IGC_MAC_STATE_DEFAULT | IGC_MAC_STATE_IN_USE;
2285 igc_rar_set_index(adapter, 0);
2288 /* If the filter to be added and an already existing filter express
2289 * the same address and address type, it should be possible to only
2290 * override the other configurations, for example the queue to steer
2293 static bool igc_mac_entry_can_be_used(const struct igc_mac_addr *entry,
2294 const u8 *addr, const u8 flags)
2296 if (!(entry->state & IGC_MAC_STATE_IN_USE))
2299 if ((entry->state & IGC_MAC_STATE_SRC_ADDR) !=
2300 (flags & IGC_MAC_STATE_SRC_ADDR))
2303 if (!ether_addr_equal(addr, entry->addr))
2309 /* Add a MAC filter for 'addr' directing matching traffic to 'queue',
2310 * 'flags' is used to indicate what kind of match is made, match is by
2311 * default for the destination address, if matching by source address
2312 * is desired the flag IGC_MAC_STATE_SRC_ADDR can be used.
2314 static int igc_add_mac_filter_flags(struct igc_adapter *adapter,
2315 const u8 *addr, const u8 queue,
2318 struct igc_hw *hw = &adapter->hw;
2319 int rar_entries = hw->mac.rar_entry_count;
2322 if (is_zero_ether_addr(addr))
2325 /* Search for the first empty entry in the MAC table.
2326 * Do not touch entries at the end of the table reserved for the VF MAC
2329 for (i = 0; i < rar_entries; i++) {
2330 if (!igc_mac_entry_can_be_used(&adapter->mac_table[i],
2334 ether_addr_copy(adapter->mac_table[i].addr, addr);
2335 adapter->mac_table[i].queue = queue;
2336 adapter->mac_table[i].state |= IGC_MAC_STATE_IN_USE | flags;
2338 igc_rar_set_index(adapter, i);
2345 int igc_add_mac_steering_filter(struct igc_adapter *adapter,
2346 const u8 *addr, u8 queue, u8 flags)
2348 return igc_add_mac_filter_flags(adapter, addr, queue,
2349 IGC_MAC_STATE_QUEUE_STEERING | flags);
2352 /* Remove a MAC filter for 'addr' directing matching traffic to
2353 * 'queue', 'flags' is used to indicate what kind of match need to be
2354 * removed, match is by default for the destination address, if
2355 * matching by source address is to be removed the flag
2356 * IGC_MAC_STATE_SRC_ADDR can be used.
2358 static int igc_del_mac_filter_flags(struct igc_adapter *adapter,
2359 const u8 *addr, const u8 queue,
2362 struct igc_hw *hw = &adapter->hw;
2363 int rar_entries = hw->mac.rar_entry_count;
2366 if (is_zero_ether_addr(addr))
2369 /* Search for matching entry in the MAC table based on given address
2370 * and queue. Do not touch entries at the end of the table reserved
2371 * for the VF MAC addresses.
2373 for (i = 0; i < rar_entries; i++) {
2374 if (!(adapter->mac_table[i].state & IGC_MAC_STATE_IN_USE))
2376 if ((adapter->mac_table[i].state & flags) != flags)
2378 if (adapter->mac_table[i].queue != queue)
2380 if (!ether_addr_equal(adapter->mac_table[i].addr, addr))
2383 /* When a filter for the default address is "deleted",
2384 * we return it to its initial configuration
2386 if (adapter->mac_table[i].state & IGC_MAC_STATE_DEFAULT) {
2387 adapter->mac_table[i].state =
2388 IGC_MAC_STATE_DEFAULT | IGC_MAC_STATE_IN_USE;
2390 adapter->mac_table[i].state = 0;
2391 adapter->mac_table[i].queue = 0;
2392 memset(adapter->mac_table[i].addr, 0, ETH_ALEN);
2395 igc_rar_set_index(adapter, i);
2402 int igc_del_mac_steering_filter(struct igc_adapter *adapter,
2403 const u8 *addr, u8 queue, u8 flags)
2405 return igc_del_mac_filter_flags(adapter, addr, queue,
2406 IGC_MAC_STATE_QUEUE_STEERING | flags);
2410 * igc_set_rx_mode - Secondary Unicast, Multicast and Promiscuous mode set
2411 * @netdev: network interface device structure
2413 * The set_rx_mode entry point is called whenever the unicast or multicast
2414 * address lists or the network interface flags are updated. This routine is
2415 * responsible for configuring the hardware for proper unicast, multicast,
2416 * promiscuous mode, and all-multi behavior.
2418 static void igc_set_rx_mode(struct net_device *netdev)
2423 * igc_msix_other - msix other interrupt handler
2424 * @irq: interrupt number
2425 * @data: pointer to a q_vector
2427 static irqreturn_t igc_msix_other(int irq, void *data)
2429 struct igc_adapter *adapter = data;
2430 struct igc_hw *hw = &adapter->hw;
2431 u32 icr = rd32(IGC_ICR);
2433 /* reading ICR causes bit 31 of EICR to be cleared */
2434 if (icr & IGC_ICR_DRSTA)
2435 schedule_work(&adapter->reset_task);
2437 if (icr & IGC_ICR_DOUTSYNC) {
2438 /* HW is reporting DMA is out of sync */
2439 adapter->stats.doosync++;
2442 if (icr & IGC_ICR_LSC) {
2443 hw->mac.get_link_status = 1;
2444 /* guard against interrupt when we're going down */
2445 if (!test_bit(__IGC_DOWN, &adapter->state))
2446 mod_timer(&adapter->watchdog_timer, jiffies + 1);
2449 wr32(IGC_EIMS, adapter->eims_other);
2455 * igc_write_ivar - configure ivar for given MSI-X vector
2456 * @hw: pointer to the HW structure
2457 * @msix_vector: vector number we are allocating to a given ring
2458 * @index: row index of IVAR register to write within IVAR table
2459 * @offset: column offset of in IVAR, should be multiple of 8
2461 * The IVAR table consists of 2 columns,
2462 * each containing an cause allocation for an Rx and Tx ring, and a
2463 * variable number of rows depending on the number of queues supported.
2465 static void igc_write_ivar(struct igc_hw *hw, int msix_vector,
2466 int index, int offset)
2468 u32 ivar = array_rd32(IGC_IVAR0, index);
2470 /* clear any bits that are currently set */
2471 ivar &= ~((u32)0xFF << offset);
2473 /* write vector and valid bit */
2474 ivar |= (msix_vector | IGC_IVAR_VALID) << offset;
2476 array_wr32(IGC_IVAR0, index, ivar);
2479 static void igc_assign_vector(struct igc_q_vector *q_vector, int msix_vector)
2481 struct igc_adapter *adapter = q_vector->adapter;
2482 struct igc_hw *hw = &adapter->hw;
2483 int rx_queue = IGC_N0_QUEUE;
2484 int tx_queue = IGC_N0_QUEUE;
2486 if (q_vector->rx.ring)
2487 rx_queue = q_vector->rx.ring->reg_idx;
2488 if (q_vector->tx.ring)
2489 tx_queue = q_vector->tx.ring->reg_idx;
2491 switch (hw->mac.type) {
2493 if (rx_queue > IGC_N0_QUEUE)
2494 igc_write_ivar(hw, msix_vector,
2496 (rx_queue & 0x1) << 4);
2497 if (tx_queue > IGC_N0_QUEUE)
2498 igc_write_ivar(hw, msix_vector,
2500 ((tx_queue & 0x1) << 4) + 8);
2501 q_vector->eims_value = BIT(msix_vector);
2504 WARN_ONCE(hw->mac.type != igc_i225, "Wrong MAC type\n");
2508 /* add q_vector eims value to global eims_enable_mask */
2509 adapter->eims_enable_mask |= q_vector->eims_value;
2511 /* configure q_vector to set itr on first interrupt */
2512 q_vector->set_itr = 1;
2516 * igc_configure_msix - Configure MSI-X hardware
2517 * @adapter: Pointer to adapter structure
2519 * igc_configure_msix sets up the hardware to properly
2520 * generate MSI-X interrupts.
2522 static void igc_configure_msix(struct igc_adapter *adapter)
2524 struct igc_hw *hw = &adapter->hw;
2528 adapter->eims_enable_mask = 0;
2530 /* set vector for other causes, i.e. link changes */
2531 switch (hw->mac.type) {
2533 /* Turn on MSI-X capability first, or our settings
2534 * won't stick. And it will take days to debug.
2536 wr32(IGC_GPIE, IGC_GPIE_MSIX_MODE |
2537 IGC_GPIE_PBA | IGC_GPIE_EIAME |
2540 /* enable msix_other interrupt */
2541 adapter->eims_other = BIT(vector);
2542 tmp = (vector++ | IGC_IVAR_VALID) << 8;
2544 wr32(IGC_IVAR_MISC, tmp);
2547 /* do nothing, since nothing else supports MSI-X */
2549 } /* switch (hw->mac.type) */
2551 adapter->eims_enable_mask |= adapter->eims_other;
2553 for (i = 0; i < adapter->num_q_vectors; i++)
2554 igc_assign_vector(adapter->q_vector[i], vector++);
2559 static irqreturn_t igc_msix_ring(int irq, void *data)
2561 struct igc_q_vector *q_vector = data;
2563 /* Write the ITR value calculated from the previous interrupt. */
2564 igc_write_itr(q_vector);
2566 napi_schedule(&q_vector->napi);
2572 * igc_request_msix - Initialize MSI-X interrupts
2573 * @adapter: Pointer to adapter structure
2575 * igc_request_msix allocates MSI-X vectors and requests interrupts from the
2578 static int igc_request_msix(struct igc_adapter *adapter)
2580 int i = 0, err = 0, vector = 0, free_vector = 0;
2581 struct net_device *netdev = adapter->netdev;
2583 err = request_irq(adapter->msix_entries[vector].vector,
2584 &igc_msix_other, 0, netdev->name, adapter);
2588 for (i = 0; i < adapter->num_q_vectors; i++) {
2589 struct igc_q_vector *q_vector = adapter->q_vector[i];
2593 q_vector->itr_register = adapter->io_addr + IGC_EITR(vector);
2595 if (q_vector->rx.ring && q_vector->tx.ring)
2596 sprintf(q_vector->name, "%s-TxRx-%u", netdev->name,
2597 q_vector->rx.ring->queue_index);
2598 else if (q_vector->tx.ring)
2599 sprintf(q_vector->name, "%s-tx-%u", netdev->name,
2600 q_vector->tx.ring->queue_index);
2601 else if (q_vector->rx.ring)
2602 sprintf(q_vector->name, "%s-rx-%u", netdev->name,
2603 q_vector->rx.ring->queue_index);
2605 sprintf(q_vector->name, "%s-unused", netdev->name);
2607 err = request_irq(adapter->msix_entries[vector].vector,
2608 igc_msix_ring, 0, q_vector->name,
2614 igc_configure_msix(adapter);
2618 /* free already assigned IRQs */
2619 free_irq(adapter->msix_entries[free_vector++].vector, adapter);
2622 for (i = 0; i < vector; i++) {
2623 free_irq(adapter->msix_entries[free_vector++].vector,
2624 adapter->q_vector[i]);
2631 * igc_reset_q_vector - Reset config for interrupt vector
2632 * @adapter: board private structure to initialize
2633 * @v_idx: Index of vector to be reset
2635 * If NAPI is enabled it will delete any references to the
2636 * NAPI struct. This is preparation for igc_free_q_vector.
2638 static void igc_reset_q_vector(struct igc_adapter *adapter, int v_idx)
2640 struct igc_q_vector *q_vector = adapter->q_vector[v_idx];
2642 /* if we're coming from igc_set_interrupt_capability, the vectors are
2648 if (q_vector->tx.ring)
2649 adapter->tx_ring[q_vector->tx.ring->queue_index] = NULL;
2651 if (q_vector->rx.ring)
2652 adapter->rx_ring[q_vector->rx.ring->queue_index] = NULL;
2654 netif_napi_del(&q_vector->napi);
2657 static void igc_reset_interrupt_capability(struct igc_adapter *adapter)
2659 int v_idx = adapter->num_q_vectors;
2661 if (adapter->msix_entries) {
2662 pci_disable_msix(adapter->pdev);
2663 kfree(adapter->msix_entries);
2664 adapter->msix_entries = NULL;
2665 } else if (adapter->flags & IGC_FLAG_HAS_MSI) {
2666 pci_disable_msi(adapter->pdev);
2670 igc_reset_q_vector(adapter, v_idx);
2674 * igc_clear_interrupt_scheme - reset the device to a state of no interrupts
2675 * @adapter: Pointer to adapter structure
2677 * This function resets the device so that it has 0 rx queues, tx queues, and
2678 * MSI-X interrupts allocated.
2680 static void igc_clear_interrupt_scheme(struct igc_adapter *adapter)
2682 igc_free_q_vectors(adapter);
2683 igc_reset_interrupt_capability(adapter);
2687 * igc_free_q_vectors - Free memory allocated for interrupt vectors
2688 * @adapter: board private structure to initialize
2690 * This function frees the memory allocated to the q_vectors. In addition if
2691 * NAPI is enabled it will delete any references to the NAPI struct prior
2692 * to freeing the q_vector.
2694 static void igc_free_q_vectors(struct igc_adapter *adapter)
2696 int v_idx = adapter->num_q_vectors;
2698 adapter->num_tx_queues = 0;
2699 adapter->num_rx_queues = 0;
2700 adapter->num_q_vectors = 0;
2703 igc_reset_q_vector(adapter, v_idx);
2704 igc_free_q_vector(adapter, v_idx);
2709 * igc_free_q_vector - Free memory allocated for specific interrupt vector
2710 * @adapter: board private structure to initialize
2711 * @v_idx: Index of vector to be freed
2713 * This function frees the memory allocated to the q_vector.
2715 static void igc_free_q_vector(struct igc_adapter *adapter, int v_idx)
2717 struct igc_q_vector *q_vector = adapter->q_vector[v_idx];
2719 adapter->q_vector[v_idx] = NULL;
2721 /* igc_get_stats64() might access the rings on this vector,
2722 * we must wait a grace period before freeing it.
2725 kfree_rcu(q_vector, rcu);
2728 /* Need to wait a few seconds after link up to get diagnostic information from
2731 static void igc_update_phy_info(struct timer_list *t)
2733 struct igc_adapter *adapter = from_timer(adapter, t, phy_info_timer);
2735 igc_get_phy_info(&adapter->hw);
2739 * igc_has_link - check shared code for link and determine up/down
2740 * @adapter: pointer to driver private info
2742 bool igc_has_link(struct igc_adapter *adapter)
2744 struct igc_hw *hw = &adapter->hw;
2745 bool link_active = false;
2747 /* get_link_status is set on LSC (link status) interrupt or
2748 * rx sequence error interrupt. get_link_status will stay
2749 * false until the igc_check_for_link establishes link
2750 * for copper adapters ONLY
2752 switch (hw->phy.media_type) {
2753 case igc_media_type_copper:
2754 if (!hw->mac.get_link_status)
2756 hw->mac.ops.check_for_link(hw);
2757 link_active = !hw->mac.get_link_status;
2760 case igc_media_type_unknown:
2764 if (hw->mac.type == igc_i225 &&
2765 hw->phy.id == I225_I_PHY_ID) {
2766 if (!netif_carrier_ok(adapter->netdev)) {
2767 adapter->flags &= ~IGC_FLAG_NEED_LINK_UPDATE;
2768 } else if (!(adapter->flags & IGC_FLAG_NEED_LINK_UPDATE)) {
2769 adapter->flags |= IGC_FLAG_NEED_LINK_UPDATE;
2770 adapter->link_check_timeout = jiffies;
2778 * igc_watchdog - Timer Call-back
2779 * @data: pointer to adapter cast into an unsigned long
2781 static void igc_watchdog(struct timer_list *t)
2783 struct igc_adapter *adapter = from_timer(adapter, t, watchdog_timer);
2784 /* Do the rest outside of interrupt context */
2785 schedule_work(&adapter->watchdog_task);
2788 static void igc_watchdog_task(struct work_struct *work)
2790 struct igc_adapter *adapter = container_of(work,
2793 struct net_device *netdev = adapter->netdev;
2794 struct igc_hw *hw = &adapter->hw;
2795 struct igc_phy_info *phy = &hw->phy;
2796 u16 phy_data, retry_count = 20;
2801 link = igc_has_link(adapter);
2803 if (adapter->flags & IGC_FLAG_NEED_LINK_UPDATE) {
2804 if (time_after(jiffies, (adapter->link_check_timeout + HZ)))
2805 adapter->flags &= ~IGC_FLAG_NEED_LINK_UPDATE;
2810 /* Force link down if we have fiber to swap to */
2811 if (adapter->flags & IGC_FLAG_MAS_ENABLE) {
2812 if (hw->phy.media_type == igc_media_type_copper) {
2813 connsw = rd32(IGC_CONNSW);
2814 if (!(connsw & IGC_CONNSW_AUTOSENSE_EN))
2819 if (!netif_carrier_ok(netdev)) {
2822 hw->mac.ops.get_speed_and_duplex(hw,
2823 &adapter->link_speed,
2824 &adapter->link_duplex);
2826 ctrl = rd32(IGC_CTRL);
2827 /* Link status message must follow this format */
2829 "igc: %s NIC Link is Up %d Mbps %s Duplex, Flow Control: %s\n",
2831 adapter->link_speed,
2832 adapter->link_duplex == FULL_DUPLEX ?
2834 (ctrl & IGC_CTRL_TFCE) &&
2835 (ctrl & IGC_CTRL_RFCE) ? "RX/TX" :
2836 (ctrl & IGC_CTRL_RFCE) ? "RX" :
2837 (ctrl & IGC_CTRL_TFCE) ? "TX" : "None");
2839 /* check if SmartSpeed worked */
2840 igc_check_downshift(hw);
2841 if (phy->speed_downgraded)
2842 netdev_warn(netdev, "Link Speed was downgraded by SmartSpeed\n");
2844 /* adjust timeout factor according to speed/duplex */
2845 adapter->tx_timeout_factor = 1;
2846 switch (adapter->link_speed) {
2848 adapter->tx_timeout_factor = 14;
2851 /* maybe add some timeout factor ? */
2855 if (adapter->link_speed != SPEED_1000)
2858 /* wait for Remote receiver status OK */
2860 if (!igc_read_phy_reg(hw, PHY_1000T_STATUS,
2862 if (!(phy_data & SR_1000T_REMOTE_RX_STATUS) &&
2866 goto retry_read_status;
2867 } else if (!retry_count) {
2868 dev_err(&adapter->pdev->dev, "exceed max 2 second\n");
2871 dev_err(&adapter->pdev->dev, "read 1000Base-T Status Reg\n");
2874 netif_carrier_on(netdev);
2876 /* link state has changed, schedule phy info update */
2877 if (!test_bit(__IGC_DOWN, &adapter->state))
2878 mod_timer(&adapter->phy_info_timer,
2879 round_jiffies(jiffies + 2 * HZ));
2882 if (netif_carrier_ok(netdev)) {
2883 adapter->link_speed = 0;
2884 adapter->link_duplex = 0;
2886 /* Links status message must follow this format */
2887 netdev_info(netdev, "igc: %s NIC Link is Down\n",
2889 netif_carrier_off(netdev);
2891 /* link state has changed, schedule phy info update */
2892 if (!test_bit(__IGC_DOWN, &adapter->state))
2893 mod_timer(&adapter->phy_info_timer,
2894 round_jiffies(jiffies + 2 * HZ));
2896 /* link is down, time to check for alternate media */
2897 if (adapter->flags & IGC_FLAG_MAS_ENABLE) {
2898 if (adapter->flags & IGC_FLAG_MEDIA_RESET) {
2899 schedule_work(&adapter->reset_task);
2900 /* return immediately */
2905 /* also check for alternate media here */
2906 } else if (!netif_carrier_ok(netdev) &&
2907 (adapter->flags & IGC_FLAG_MAS_ENABLE)) {
2908 if (adapter->flags & IGC_FLAG_MEDIA_RESET) {
2909 schedule_work(&adapter->reset_task);
2910 /* return immediately */
2916 spin_lock(&adapter->stats64_lock);
2917 igc_update_stats(adapter);
2918 spin_unlock(&adapter->stats64_lock);
2920 for (i = 0; i < adapter->num_tx_queues; i++) {
2921 struct igc_ring *tx_ring = adapter->tx_ring[i];
2923 if (!netif_carrier_ok(netdev)) {
2924 /* We've lost link, so the controller stops DMA,
2925 * but we've got queued Tx work that's never going
2926 * to get done, so reset controller to flush Tx.
2927 * (Do the reset outside of interrupt context).
2929 if (igc_desc_unused(tx_ring) + 1 < tx_ring->count) {
2930 adapter->tx_timeout_count++;
2931 schedule_work(&adapter->reset_task);
2932 /* return immediately since reset is imminent */
2937 /* Force detection of hung controller every watchdog period */
2938 set_bit(IGC_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags);
2941 /* Cause software interrupt to ensure Rx ring is cleaned */
2942 if (adapter->flags & IGC_FLAG_HAS_MSIX) {
2945 for (i = 0; i < adapter->num_q_vectors; i++)
2946 eics |= adapter->q_vector[i]->eims_value;
2947 wr32(IGC_EICS, eics);
2949 wr32(IGC_ICS, IGC_ICS_RXDMT0);
2952 /* Reset the timer */
2953 if (!test_bit(__IGC_DOWN, &adapter->state)) {
2954 if (adapter->flags & IGC_FLAG_NEED_LINK_UPDATE)
2955 mod_timer(&adapter->watchdog_timer,
2956 round_jiffies(jiffies + HZ));
2958 mod_timer(&adapter->watchdog_timer,
2959 round_jiffies(jiffies + 2 * HZ));
2964 * igc_update_ring_itr - update the dynamic ITR value based on packet size
2965 * @q_vector: pointer to q_vector
2967 * Stores a new ITR value based on strictly on packet size. This
2968 * algorithm is less sophisticated than that used in igc_update_itr,
2969 * due to the difficulty of synchronizing statistics across multiple
2970 * receive rings. The divisors and thresholds used by this function
2971 * were determined based on theoretical maximum wire speed and testing
2972 * data, in order to minimize response time while increasing bulk
2974 * NOTE: This function is called only when operating in a multiqueue
2975 * receive environment.
2977 static void igc_update_ring_itr(struct igc_q_vector *q_vector)
2979 struct igc_adapter *adapter = q_vector->adapter;
2980 int new_val = q_vector->itr_val;
2981 int avg_wire_size = 0;
2982 unsigned int packets;
2984 /* For non-gigabit speeds, just fix the interrupt rate at 4000
2985 * ints/sec - ITR timer value of 120 ticks.
2987 switch (adapter->link_speed) {
2990 new_val = IGC_4K_ITR;
2996 packets = q_vector->rx.total_packets;
2998 avg_wire_size = q_vector->rx.total_bytes / packets;
3000 packets = q_vector->tx.total_packets;
3002 avg_wire_size = max_t(u32, avg_wire_size,
3003 q_vector->tx.total_bytes / packets);
3005 /* if avg_wire_size isn't set no work was done */
3009 /* Add 24 bytes to size to account for CRC, preamble, and gap */
3010 avg_wire_size += 24;
3012 /* Don't starve jumbo frames */
3013 avg_wire_size = min(avg_wire_size, 3000);
3015 /* Give a little boost to mid-size frames */
3016 if (avg_wire_size > 300 && avg_wire_size < 1200)
3017 new_val = avg_wire_size / 3;
3019 new_val = avg_wire_size / 2;
3021 /* conservative mode (itr 3) eliminates the lowest_latency setting */
3022 if (new_val < IGC_20K_ITR &&
3023 ((q_vector->rx.ring && adapter->rx_itr_setting == 3) ||
3024 (!q_vector->rx.ring && adapter->tx_itr_setting == 3)))
3025 new_val = IGC_20K_ITR;
3028 if (new_val != q_vector->itr_val) {
3029 q_vector->itr_val = new_val;
3030 q_vector->set_itr = 1;
3033 q_vector->rx.total_bytes = 0;
3034 q_vector->rx.total_packets = 0;
3035 q_vector->tx.total_bytes = 0;
3036 q_vector->tx.total_packets = 0;
3040 * igc_update_itr - update the dynamic ITR value based on statistics
3041 * @q_vector: pointer to q_vector
3042 * @ring_container: ring info to update the itr for
3044 * Stores a new ITR value based on packets and byte
3045 * counts during the last interrupt. The advantage of per interrupt
3046 * computation is faster updates and more accurate ITR for the current
3047 * traffic pattern. Constants in this function were computed
3048 * based on theoretical maximum wire speed and thresholds were set based
3049 * on testing data as well as attempting to minimize response time
3050 * while increasing bulk throughput.
3051 * NOTE: These calculations are only valid when operating in a single-
3052 * queue environment.
3054 static void igc_update_itr(struct igc_q_vector *q_vector,
3055 struct igc_ring_container *ring_container)
3057 unsigned int packets = ring_container->total_packets;
3058 unsigned int bytes = ring_container->total_bytes;
3059 u8 itrval = ring_container->itr;
3061 /* no packets, exit with status unchanged */
3066 case lowest_latency:
3067 /* handle TSO and jumbo frames */
3068 if (bytes / packets > 8000)
3069 itrval = bulk_latency;
3070 else if ((packets < 5) && (bytes > 512))
3071 itrval = low_latency;
3073 case low_latency: /* 50 usec aka 20000 ints/s */
3074 if (bytes > 10000) {
3075 /* this if handles the TSO accounting */
3076 if (bytes / packets > 8000)
3077 itrval = bulk_latency;
3078 else if ((packets < 10) || ((bytes / packets) > 1200))
3079 itrval = bulk_latency;
3080 else if ((packets > 35))
3081 itrval = lowest_latency;
3082 } else if (bytes / packets > 2000) {
3083 itrval = bulk_latency;
3084 } else if (packets <= 2 && bytes < 512) {
3085 itrval = lowest_latency;
3088 case bulk_latency: /* 250 usec aka 4000 ints/s */
3089 if (bytes > 25000) {
3091 itrval = low_latency;
3092 } else if (bytes < 1500) {
3093 itrval = low_latency;
3098 /* clear work counters since we have the values we need */
3099 ring_container->total_bytes = 0;
3100 ring_container->total_packets = 0;
3102 /* write updated itr to ring container */
3103 ring_container->itr = itrval;
3107 * igc_intr_msi - Interrupt Handler
3108 * @irq: interrupt number
3109 * @data: pointer to a network interface device structure
3111 static irqreturn_t igc_intr_msi(int irq, void *data)
3113 struct igc_adapter *adapter = data;
3114 struct igc_q_vector *q_vector = adapter->q_vector[0];
3115 struct igc_hw *hw = &adapter->hw;
3116 /* read ICR disables interrupts using IAM */
3117 u32 icr = rd32(IGC_ICR);
3119 igc_write_itr(q_vector);
3121 if (icr & IGC_ICR_DRSTA)
3122 schedule_work(&adapter->reset_task);
3124 if (icr & IGC_ICR_DOUTSYNC) {
3125 /* HW is reporting DMA is out of sync */
3126 adapter->stats.doosync++;
3129 if (icr & (IGC_ICR_RXSEQ | IGC_ICR_LSC)) {
3130 hw->mac.get_link_status = 1;
3131 if (!test_bit(__IGC_DOWN, &adapter->state))
3132 mod_timer(&adapter->watchdog_timer, jiffies + 1);
3135 napi_schedule(&q_vector->napi);
3141 * igc_intr - Legacy Interrupt Handler
3142 * @irq: interrupt number
3143 * @data: pointer to a network interface device structure
3145 static irqreturn_t igc_intr(int irq, void *data)
3147 struct igc_adapter *adapter = data;
3148 struct igc_q_vector *q_vector = adapter->q_vector[0];
3149 struct igc_hw *hw = &adapter->hw;
3150 /* Interrupt Auto-Mask...upon reading ICR, interrupts are masked. No
3151 * need for the IMC write
3153 u32 icr = rd32(IGC_ICR);
3155 /* IMS will not auto-mask if INT_ASSERTED is not set, and if it is
3156 * not set, then the adapter didn't send an interrupt
3158 if (!(icr & IGC_ICR_INT_ASSERTED))
3161 igc_write_itr(q_vector);
3163 if (icr & IGC_ICR_DRSTA)
3164 schedule_work(&adapter->reset_task);
3166 if (icr & IGC_ICR_DOUTSYNC) {
3167 /* HW is reporting DMA is out of sync */
3168 adapter->stats.doosync++;
3171 if (icr & (IGC_ICR_RXSEQ | IGC_ICR_LSC)) {
3172 hw->mac.get_link_status = 1;
3173 /* guard against interrupt when we're going down */
3174 if (!test_bit(__IGC_DOWN, &adapter->state))
3175 mod_timer(&adapter->watchdog_timer, jiffies + 1);
3178 napi_schedule(&q_vector->napi);
3183 static void igc_set_itr(struct igc_q_vector *q_vector)
3185 struct igc_adapter *adapter = q_vector->adapter;
3186 u32 new_itr = q_vector->itr_val;
3189 /* for non-gigabit speeds, just fix the interrupt rate at 4000 */
3190 switch (adapter->link_speed) {
3194 new_itr = IGC_4K_ITR;
3200 igc_update_itr(q_vector, &q_vector->tx);
3201 igc_update_itr(q_vector, &q_vector->rx);
3203 current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
3205 /* conservative mode (itr 3) eliminates the lowest_latency setting */
3206 if (current_itr == lowest_latency &&
3207 ((q_vector->rx.ring && adapter->rx_itr_setting == 3) ||
3208 (!q_vector->rx.ring && adapter->tx_itr_setting == 3)))
3209 current_itr = low_latency;
3211 switch (current_itr) {
3212 /* counts and packets in update_itr are dependent on these numbers */
3213 case lowest_latency:
3214 new_itr = IGC_70K_ITR; /* 70,000 ints/sec */
3217 new_itr = IGC_20K_ITR; /* 20,000 ints/sec */
3220 new_itr = IGC_4K_ITR; /* 4,000 ints/sec */
3227 if (new_itr != q_vector->itr_val) {
3228 /* this attempts to bias the interrupt rate towards Bulk
3229 * by adding intermediate steps when interrupt rate is
3232 new_itr = new_itr > q_vector->itr_val ?
3233 max((new_itr * q_vector->itr_val) /
3234 (new_itr + (q_vector->itr_val >> 2)),
3236 /* Don't write the value here; it resets the adapter's
3237 * internal timer, and causes us to delay far longer than
3238 * we should between interrupts. Instead, we write the ITR
3239 * value at the beginning of the next interrupt so the timing
3240 * ends up being correct.
3242 q_vector->itr_val = new_itr;
3243 q_vector->set_itr = 1;
3247 static void igc_ring_irq_enable(struct igc_q_vector *q_vector)
3249 struct igc_adapter *adapter = q_vector->adapter;
3250 struct igc_hw *hw = &adapter->hw;
3252 if ((q_vector->rx.ring && (adapter->rx_itr_setting & 3)) ||
3253 (!q_vector->rx.ring && (adapter->tx_itr_setting & 3))) {
3254 if (adapter->num_q_vectors == 1)
3255 igc_set_itr(q_vector);
3257 igc_update_ring_itr(q_vector);
3260 if (!test_bit(__IGC_DOWN, &adapter->state)) {
3261 if (adapter->msix_entries)
3262 wr32(IGC_EIMS, q_vector->eims_value);
3264 igc_irq_enable(adapter);
3269 * igc_poll - NAPI Rx polling callback
3270 * @napi: napi polling structure
3271 * @budget: count of how many packets we should handle
3273 static int igc_poll(struct napi_struct *napi, int budget)
3275 struct igc_q_vector *q_vector = container_of(napi,
3276 struct igc_q_vector,
3278 bool clean_complete = true;
3281 if (q_vector->tx.ring)
3282 clean_complete = igc_clean_tx_irq(q_vector, budget);
3284 if (q_vector->rx.ring) {
3285 int cleaned = igc_clean_rx_irq(q_vector, budget);
3287 work_done += cleaned;
3288 if (cleaned >= budget)
3289 clean_complete = false;
3292 /* If all work not completed, return budget and keep polling */
3293 if (!clean_complete)
3296 /* Exit the polling mode, but don't re-enable interrupts if stack might
3297 * poll us due to busy-polling
3299 if (likely(napi_complete_done(napi, work_done)))
3300 igc_ring_irq_enable(q_vector);
3302 return min(work_done, budget - 1);
3306 * igc_set_interrupt_capability - set MSI or MSI-X if supported
3307 * @adapter: Pointer to adapter structure
3309 * Attempt to configure interrupts using the best available
3310 * capabilities of the hardware and kernel.
3312 static void igc_set_interrupt_capability(struct igc_adapter *adapter,
3320 adapter->flags |= IGC_FLAG_HAS_MSIX;
3322 /* Number of supported queues. */
3323 adapter->num_rx_queues = adapter->rss_queues;
3325 adapter->num_tx_queues = adapter->rss_queues;
3327 /* start with one vector for every Rx queue */
3328 numvecs = adapter->num_rx_queues;
3330 /* if Tx handler is separate add 1 for every Tx queue */
3331 if (!(adapter->flags & IGC_FLAG_QUEUE_PAIRS))
3332 numvecs += adapter->num_tx_queues;
3334 /* store the number of vectors reserved for queues */
3335 adapter->num_q_vectors = numvecs;
3337 /* add 1 vector for link status interrupts */
3340 adapter->msix_entries = kcalloc(numvecs, sizeof(struct msix_entry),
3343 if (!adapter->msix_entries)
3346 /* populate entry values */
3347 for (i = 0; i < numvecs; i++)
3348 adapter->msix_entries[i].entry = i;
3350 err = pci_enable_msix_range(adapter->pdev,
3351 adapter->msix_entries,
3357 kfree(adapter->msix_entries);
3358 adapter->msix_entries = NULL;
3360 igc_reset_interrupt_capability(adapter);
3363 adapter->flags &= ~IGC_FLAG_HAS_MSIX;
3365 adapter->rss_queues = 1;
3366 adapter->flags |= IGC_FLAG_QUEUE_PAIRS;
3367 adapter->num_rx_queues = 1;
3368 adapter->num_tx_queues = 1;
3369 adapter->num_q_vectors = 1;
3370 if (!pci_enable_msi(adapter->pdev))
3371 adapter->flags |= IGC_FLAG_HAS_MSI;
3374 static void igc_add_ring(struct igc_ring *ring,
3375 struct igc_ring_container *head)
3382 * igc_alloc_q_vector - Allocate memory for a single interrupt vector
3383 * @adapter: board private structure to initialize
3384 * @v_count: q_vectors allocated on adapter, used for ring interleaving
3385 * @v_idx: index of vector in adapter struct
3386 * @txr_count: total number of Tx rings to allocate
3387 * @txr_idx: index of first Tx ring to allocate
3388 * @rxr_count: total number of Rx rings to allocate
3389 * @rxr_idx: index of first Rx ring to allocate
3391 * We allocate one q_vector. If allocation fails we return -ENOMEM.
3393 static int igc_alloc_q_vector(struct igc_adapter *adapter,
3394 unsigned int v_count, unsigned int v_idx,
3395 unsigned int txr_count, unsigned int txr_idx,
3396 unsigned int rxr_count, unsigned int rxr_idx)
3398 struct igc_q_vector *q_vector;
3399 struct igc_ring *ring;
3402 /* igc only supports 1 Tx and/or 1 Rx queue per vector */
3403 if (txr_count > 1 || rxr_count > 1)
3406 ring_count = txr_count + rxr_count;
3408 /* allocate q_vector and rings */
3409 q_vector = adapter->q_vector[v_idx];
3411 q_vector = kzalloc(struct_size(q_vector, ring, ring_count),
3414 memset(q_vector, 0, struct_size(q_vector, ring, ring_count));
3418 /* initialize NAPI */
3419 netif_napi_add(adapter->netdev, &q_vector->napi,
3422 /* tie q_vector and adapter together */
3423 adapter->q_vector[v_idx] = q_vector;
3424 q_vector->adapter = adapter;
3426 /* initialize work limits */
3427 q_vector->tx.work_limit = adapter->tx_work_limit;
3429 /* initialize ITR configuration */
3430 q_vector->itr_register = adapter->io_addr + IGC_EITR(0);
3431 q_vector->itr_val = IGC_START_ITR;
3433 /* initialize pointer to rings */
3434 ring = q_vector->ring;
3436 /* initialize ITR */
3438 /* rx or rx/tx vector */
3439 if (!adapter->rx_itr_setting || adapter->rx_itr_setting > 3)
3440 q_vector->itr_val = adapter->rx_itr_setting;
3442 /* tx only vector */
3443 if (!adapter->tx_itr_setting || adapter->tx_itr_setting > 3)
3444 q_vector->itr_val = adapter->tx_itr_setting;
3448 /* assign generic ring traits */
3449 ring->dev = &adapter->pdev->dev;
3450 ring->netdev = adapter->netdev;
3452 /* configure backlink on ring */
3453 ring->q_vector = q_vector;
3455 /* update q_vector Tx values */
3456 igc_add_ring(ring, &q_vector->tx);
3458 /* apply Tx specific ring traits */
3459 ring->count = adapter->tx_ring_count;
3460 ring->queue_index = txr_idx;
3462 /* assign ring to adapter */
3463 adapter->tx_ring[txr_idx] = ring;
3465 /* push pointer to next ring */
3470 /* assign generic ring traits */
3471 ring->dev = &adapter->pdev->dev;
3472 ring->netdev = adapter->netdev;
3474 /* configure backlink on ring */
3475 ring->q_vector = q_vector;
3477 /* update q_vector Rx values */
3478 igc_add_ring(ring, &q_vector->rx);
3480 /* apply Rx specific ring traits */
3481 ring->count = adapter->rx_ring_count;
3482 ring->queue_index = rxr_idx;
3484 /* assign ring to adapter */
3485 adapter->rx_ring[rxr_idx] = ring;
3492 * igc_alloc_q_vectors - Allocate memory for interrupt vectors
3493 * @adapter: board private structure to initialize
3495 * We allocate one q_vector per queue interrupt. If allocation fails we
3498 static int igc_alloc_q_vectors(struct igc_adapter *adapter)
3500 int rxr_remaining = adapter->num_rx_queues;
3501 int txr_remaining = adapter->num_tx_queues;
3502 int rxr_idx = 0, txr_idx = 0, v_idx = 0;
3503 int q_vectors = adapter->num_q_vectors;
3506 if (q_vectors >= (rxr_remaining + txr_remaining)) {
3507 for (; rxr_remaining; v_idx++) {
3508 err = igc_alloc_q_vector(adapter, q_vectors, v_idx,
3514 /* update counts and index */
3520 for (; v_idx < q_vectors; v_idx++) {
3521 int rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - v_idx);
3522 int tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - v_idx);
3524 err = igc_alloc_q_vector(adapter, q_vectors, v_idx,
3525 tqpv, txr_idx, rqpv, rxr_idx);
3530 /* update counts and index */
3531 rxr_remaining -= rqpv;
3532 txr_remaining -= tqpv;
3540 adapter->num_tx_queues = 0;
3541 adapter->num_rx_queues = 0;
3542 adapter->num_q_vectors = 0;
3545 igc_free_q_vector(adapter, v_idx);
3551 * igc_cache_ring_register - Descriptor ring to register mapping
3552 * @adapter: board private structure to initialize
3554 * Once we know the feature-set enabled for the device, we'll cache
3555 * the register offset the descriptor ring is assigned to.
3557 static void igc_cache_ring_register(struct igc_adapter *adapter)
3561 switch (adapter->hw.mac.type) {
3565 for (; i < adapter->num_rx_queues; i++)
3566 adapter->rx_ring[i]->reg_idx = i;
3567 for (; j < adapter->num_tx_queues; j++)
3568 adapter->tx_ring[j]->reg_idx = j;
3574 * igc_init_interrupt_scheme - initialize interrupts, allocate queues/vectors
3575 * @adapter: Pointer to adapter structure
3577 * This function initializes the interrupts and allocates all of the queues.
3579 static int igc_init_interrupt_scheme(struct igc_adapter *adapter, bool msix)
3581 struct pci_dev *pdev = adapter->pdev;
3584 igc_set_interrupt_capability(adapter, msix);
3586 err = igc_alloc_q_vectors(adapter);
3588 dev_err(&pdev->dev, "Unable to allocate memory for vectors\n");
3589 goto err_alloc_q_vectors;
3592 igc_cache_ring_register(adapter);
3596 err_alloc_q_vectors:
3597 igc_reset_interrupt_capability(adapter);
3601 static void igc_free_irq(struct igc_adapter *adapter)
3603 if (adapter->msix_entries) {
3606 free_irq(adapter->msix_entries[vector++].vector, adapter);
3608 for (i = 0; i < adapter->num_q_vectors; i++)
3609 free_irq(adapter->msix_entries[vector++].vector,
3610 adapter->q_vector[i]);
3612 free_irq(adapter->pdev->irq, adapter);
3617 * igc_irq_disable - Mask off interrupt generation on the NIC
3618 * @adapter: board private structure
3620 static void igc_irq_disable(struct igc_adapter *adapter)
3622 struct igc_hw *hw = &adapter->hw;
3624 if (adapter->msix_entries) {
3625 u32 regval = rd32(IGC_EIAM);
3627 wr32(IGC_EIAM, regval & ~adapter->eims_enable_mask);
3628 wr32(IGC_EIMC, adapter->eims_enable_mask);
3629 regval = rd32(IGC_EIAC);
3630 wr32(IGC_EIAC, regval & ~adapter->eims_enable_mask);
3637 if (adapter->msix_entries) {
3640 synchronize_irq(adapter->msix_entries[vector++].vector);
3642 for (i = 0; i < adapter->num_q_vectors; i++)
3643 synchronize_irq(adapter->msix_entries[vector++].vector);
3645 synchronize_irq(adapter->pdev->irq);
3650 * igc_irq_enable - Enable default interrupt generation settings
3651 * @adapter: board private structure
3653 static void igc_irq_enable(struct igc_adapter *adapter)
3655 struct igc_hw *hw = &adapter->hw;
3657 if (adapter->msix_entries) {
3658 u32 ims = IGC_IMS_LSC | IGC_IMS_DOUTSYNC | IGC_IMS_DRSTA;
3659 u32 regval = rd32(IGC_EIAC);
3661 wr32(IGC_EIAC, regval | adapter->eims_enable_mask);
3662 regval = rd32(IGC_EIAM);
3663 wr32(IGC_EIAM, regval | adapter->eims_enable_mask);
3664 wr32(IGC_EIMS, adapter->eims_enable_mask);
3667 wr32(IGC_IMS, IMS_ENABLE_MASK | IGC_IMS_DRSTA);
3668 wr32(IGC_IAM, IMS_ENABLE_MASK | IGC_IMS_DRSTA);
3673 * igc_request_irq - initialize interrupts
3674 * @adapter: Pointer to adapter structure
3676 * Attempts to configure interrupts using the best available
3677 * capabilities of the hardware and kernel.
3679 static int igc_request_irq(struct igc_adapter *adapter)
3681 struct net_device *netdev = adapter->netdev;
3682 struct pci_dev *pdev = adapter->pdev;
3685 if (adapter->flags & IGC_FLAG_HAS_MSIX) {
3686 err = igc_request_msix(adapter);
3689 /* fall back to MSI */
3690 igc_free_all_tx_resources(adapter);
3691 igc_free_all_rx_resources(adapter);
3693 igc_clear_interrupt_scheme(adapter);
3694 err = igc_init_interrupt_scheme(adapter, false);
3697 igc_setup_all_tx_resources(adapter);
3698 igc_setup_all_rx_resources(adapter);
3699 igc_configure(adapter);
3702 igc_assign_vector(adapter->q_vector[0], 0);
3704 if (adapter->flags & IGC_FLAG_HAS_MSI) {
3705 err = request_irq(pdev->irq, &igc_intr_msi, 0,
3706 netdev->name, adapter);
3710 /* fall back to legacy interrupts */
3711 igc_reset_interrupt_capability(adapter);
3712 adapter->flags &= ~IGC_FLAG_HAS_MSI;
3715 err = request_irq(pdev->irq, &igc_intr, IRQF_SHARED,
3716 netdev->name, adapter);
3719 dev_err(&pdev->dev, "Error %d getting interrupt\n",
3726 static void igc_write_itr(struct igc_q_vector *q_vector)
3728 u32 itr_val = q_vector->itr_val & IGC_QVECTOR_MASK;
3730 if (!q_vector->set_itr)
3734 itr_val = IGC_ITR_VAL_MASK;
3736 itr_val |= IGC_EITR_CNT_IGNR;
3738 writel(itr_val, q_vector->itr_register);
3739 q_vector->set_itr = 0;
3743 * igc_open - Called when a network interface is made active
3744 * @netdev: network interface device structure
3746 * Returns 0 on success, negative value on failure
3748 * The open entry point is called when a network interface is made
3749 * active by the system (IFF_UP). At this point all resources needed
3750 * for transmit and receive operations are allocated, the interrupt
3751 * handler is registered with the OS, the watchdog timer is started,
3752 * and the stack is notified that the interface is ready.
3754 static int __igc_open(struct net_device *netdev, bool resuming)
3756 struct igc_adapter *adapter = netdev_priv(netdev);
3757 struct igc_hw *hw = &adapter->hw;
3761 /* disallow open during test */
3763 if (test_bit(__IGC_TESTING, &adapter->state)) {
3768 netif_carrier_off(netdev);
3770 /* allocate transmit descriptors */
3771 err = igc_setup_all_tx_resources(adapter);
3775 /* allocate receive descriptors */
3776 err = igc_setup_all_rx_resources(adapter);
3780 igc_power_up_link(adapter);
3782 igc_configure(adapter);
3784 err = igc_request_irq(adapter);
3788 /* Notify the stack of the actual queue counts. */
3789 err = netif_set_real_num_tx_queues(netdev, adapter->num_tx_queues);
3791 goto err_set_queues;
3793 err = netif_set_real_num_rx_queues(netdev, adapter->num_rx_queues);
3795 goto err_set_queues;
3797 clear_bit(__IGC_DOWN, &adapter->state);
3799 for (i = 0; i < adapter->num_q_vectors; i++)
3800 napi_enable(&adapter->q_vector[i]->napi);
3802 /* Clear any pending interrupts. */
3804 igc_irq_enable(adapter);
3806 netif_tx_start_all_queues(netdev);
3808 /* start the watchdog. */
3809 hw->mac.get_link_status = 1;
3810 schedule_work(&adapter->watchdog_task);
3815 igc_free_irq(adapter);
3817 igc_release_hw_control(adapter);
3818 igc_power_down_link(adapter);
3819 igc_free_all_rx_resources(adapter);
3821 igc_free_all_tx_resources(adapter);
3828 static int igc_open(struct net_device *netdev)
3830 return __igc_open(netdev, false);
3834 * igc_close - Disables a network interface
3835 * @netdev: network interface device structure
3837 * Returns 0, this is not allowed to fail
3839 * The close entry point is called when an interface is de-activated
3840 * by the OS. The hardware is still under the driver's control, but
3841 * needs to be disabled. A global MAC reset is issued to stop the
3842 * hardware, and all transmit and receive resources are freed.
3844 static int __igc_close(struct net_device *netdev, bool suspending)
3846 struct igc_adapter *adapter = netdev_priv(netdev);
3848 WARN_ON(test_bit(__IGC_RESETTING, &adapter->state));
3852 igc_release_hw_control(adapter);
3854 igc_free_irq(adapter);
3856 igc_free_all_tx_resources(adapter);
3857 igc_free_all_rx_resources(adapter);
3862 static int igc_close(struct net_device *netdev)
3864 if (netif_device_present(netdev) || netdev->dismantle)
3865 return __igc_close(netdev, false);
3869 static const struct net_device_ops igc_netdev_ops = {
3870 .ndo_open = igc_open,
3871 .ndo_stop = igc_close,
3872 .ndo_start_xmit = igc_xmit_frame,
3873 .ndo_set_mac_address = igc_set_mac,
3874 .ndo_change_mtu = igc_change_mtu,
3875 .ndo_get_stats = igc_get_stats,
3876 .ndo_fix_features = igc_fix_features,
3877 .ndo_set_features = igc_set_features,
3878 .ndo_features_check = igc_features_check,
3881 /* PCIe configuration access */
3882 void igc_read_pci_cfg(struct igc_hw *hw, u32 reg, u16 *value)
3884 struct igc_adapter *adapter = hw->back;
3886 pci_read_config_word(adapter->pdev, reg, value);
3889 void igc_write_pci_cfg(struct igc_hw *hw, u32 reg, u16 *value)
3891 struct igc_adapter *adapter = hw->back;
3893 pci_write_config_word(adapter->pdev, reg, *value);
3896 s32 igc_read_pcie_cap_reg(struct igc_hw *hw, u32 reg, u16 *value)
3898 struct igc_adapter *adapter = hw->back;
3901 cap_offset = pci_find_capability(adapter->pdev, PCI_CAP_ID_EXP);
3903 return -IGC_ERR_CONFIG;
3905 pci_read_config_word(adapter->pdev, cap_offset + reg, value);
3910 s32 igc_write_pcie_cap_reg(struct igc_hw *hw, u32 reg, u16 *value)
3912 struct igc_adapter *adapter = hw->back;
3915 cap_offset = pci_find_capability(adapter->pdev, PCI_CAP_ID_EXP);
3917 return -IGC_ERR_CONFIG;
3919 pci_write_config_word(adapter->pdev, cap_offset + reg, *value);
3924 u32 igc_rd32(struct igc_hw *hw, u32 reg)
3926 struct igc_adapter *igc = container_of(hw, struct igc_adapter, hw);
3927 u8 __iomem *hw_addr = READ_ONCE(hw->hw_addr);
3930 if (IGC_REMOVED(hw_addr))
3933 value = readl(&hw_addr[reg]);
3935 /* reads should not return all F's */
3936 if (!(~value) && (!reg || !(~readl(hw_addr)))) {
3937 struct net_device *netdev = igc->netdev;
3940 netif_device_detach(netdev);
3941 netdev_err(netdev, "PCIe link lost, device now detached\n");
3947 int igc_set_spd_dplx(struct igc_adapter *adapter, u32 spd, u8 dplx)
3949 struct pci_dev *pdev = adapter->pdev;
3950 struct igc_mac_info *mac = &adapter->hw.mac;
3954 /* Make sure dplx is at most 1 bit and lsb of speed is not set
3955 * for the switch() below to work
3957 if ((spd & 1) || (dplx & ~1))
3960 switch (spd + dplx) {
3961 case SPEED_10 + DUPLEX_HALF:
3962 mac->forced_speed_duplex = ADVERTISE_10_HALF;
3964 case SPEED_10 + DUPLEX_FULL:
3965 mac->forced_speed_duplex = ADVERTISE_10_FULL;
3967 case SPEED_100 + DUPLEX_HALF:
3968 mac->forced_speed_duplex = ADVERTISE_100_HALF;
3970 case SPEED_100 + DUPLEX_FULL:
3971 mac->forced_speed_duplex = ADVERTISE_100_FULL;
3973 case SPEED_1000 + DUPLEX_FULL:
3975 adapter->hw.phy.autoneg_advertised = ADVERTISE_1000_FULL;
3977 case SPEED_1000 + DUPLEX_HALF: /* not supported */
3979 case SPEED_2500 + DUPLEX_FULL:
3981 adapter->hw.phy.autoneg_advertised = ADVERTISE_2500_FULL;
3983 case SPEED_2500 + DUPLEX_HALF: /* not supported */
3988 /* clear MDI, MDI(-X) override is only allowed when autoneg enabled */
3989 adapter->hw.phy.mdix = AUTO_ALL_MODES;
3994 dev_err(&pdev->dev, "Unsupported Speed/Duplex configuration\n");
3999 * igc_probe - Device Initialization Routine
4000 * @pdev: PCI device information struct
4001 * @ent: entry in igc_pci_tbl
4003 * Returns 0 on success, negative on failure
4005 * igc_probe initializes an adapter identified by a pci_dev structure.
4006 * The OS initialization, configuring the adapter private structure,
4007 * and a hardware reset occur.
4009 static int igc_probe(struct pci_dev *pdev,
4010 const struct pci_device_id *ent)
4012 struct igc_adapter *adapter;
4013 struct net_device *netdev;
4015 const struct igc_info *ei = igc_info_tbl[ent->driver_data];
4018 err = pci_enable_device_mem(pdev);
4022 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
4024 err = dma_set_coherent_mask(&pdev->dev,
4027 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
4029 err = dma_set_coherent_mask(&pdev->dev,
4032 dev_err(&pdev->dev, "igc: Wrong DMA config\n");
4038 err = pci_request_selected_regions(pdev,
4039 pci_select_bars(pdev,
4045 pci_enable_pcie_error_reporting(pdev);
4047 pci_set_master(pdev);
4050 netdev = alloc_etherdev_mq(sizeof(struct igc_adapter),
4054 goto err_alloc_etherdev;
4056 SET_NETDEV_DEV(netdev, &pdev->dev);
4058 pci_set_drvdata(pdev, netdev);
4059 adapter = netdev_priv(netdev);
4060 adapter->netdev = netdev;
4061 adapter->pdev = pdev;
4064 adapter->port_num = hw->bus.func;
4065 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
4067 err = pci_save_state(pdev);
4072 adapter->io_addr = ioremap(pci_resource_start(pdev, 0),
4073 pci_resource_len(pdev, 0));
4074 if (!adapter->io_addr)
4077 /* hw->hw_addr can be zeroed, so use adapter->io_addr for unmap */
4078 hw->hw_addr = adapter->io_addr;
4080 netdev->netdev_ops = &igc_netdev_ops;
4081 igc_set_ethtool_ops(netdev);
4082 netdev->watchdog_timeo = 5 * HZ;
4084 netdev->mem_start = pci_resource_start(pdev, 0);
4085 netdev->mem_end = pci_resource_end(pdev, 0);
4087 /* PCI config space info */
4088 hw->vendor_id = pdev->vendor;
4089 hw->device_id = pdev->device;
4090 hw->revision_id = pdev->revision;
4091 hw->subsystem_vendor_id = pdev->subsystem_vendor;
4092 hw->subsystem_device_id = pdev->subsystem_device;
4094 /* Copy the default MAC and PHY function pointers */
4095 memcpy(&hw->mac.ops, ei->mac_ops, sizeof(hw->mac.ops));
4096 memcpy(&hw->phy.ops, ei->phy_ops, sizeof(hw->phy.ops));
4098 /* Initialize skew-specific constants */
4099 err = ei->get_invariants(hw);
4103 /* setup the private structure */
4104 err = igc_sw_init(adapter);
4108 /* copy netdev features into list of user selectable features */
4109 netdev->hw_features |= NETIF_F_NTUPLE;
4111 /* MTU range: 68 - 9216 */
4112 netdev->min_mtu = ETH_MIN_MTU;
4113 netdev->max_mtu = MAX_STD_JUMBO_FRAME_SIZE;
4115 /* before reading the NVM, reset the controller to put the device in a
4116 * known good starting state
4118 hw->mac.ops.reset_hw(hw);
4120 if (eth_platform_get_mac_address(&pdev->dev, hw->mac.addr)) {
4121 /* copy the MAC address out of the NVM */
4122 if (hw->mac.ops.read_mac_addr(hw))
4123 dev_err(&pdev->dev, "NVM Read Error\n");
4126 memcpy(netdev->dev_addr, hw->mac.addr, netdev->addr_len);
4128 if (!is_valid_ether_addr(netdev->dev_addr)) {
4129 dev_err(&pdev->dev, "Invalid MAC Address\n");
4134 /* configure RXPBSIZE and TXPBSIZE */
4135 wr32(IGC_RXPBS, I225_RXPBSIZE_DEFAULT);
4136 wr32(IGC_TXPBS, I225_TXPBSIZE_DEFAULT);
4138 timer_setup(&adapter->watchdog_timer, igc_watchdog, 0);
4139 timer_setup(&adapter->phy_info_timer, igc_update_phy_info, 0);
4141 INIT_WORK(&adapter->reset_task, igc_reset_task);
4142 INIT_WORK(&adapter->watchdog_task, igc_watchdog_task);
4144 /* Initialize link properties that are user-changeable */
4145 adapter->fc_autoneg = true;
4146 hw->mac.autoneg = true;
4147 hw->phy.autoneg_advertised = 0xaf;
4149 hw->fc.requested_mode = igc_fc_default;
4150 hw->fc.current_mode = igc_fc_default;
4152 /* reset the hardware with the new settings */
4155 /* let the f/w know that the h/w is now under the control of the
4158 igc_get_hw_control(adapter);
4160 strncpy(netdev->name, "eth%d", IFNAMSIZ);
4161 err = register_netdev(netdev);
4165 /* carrier off reporting is important to ethtool even BEFORE open */
4166 netif_carrier_off(netdev);
4168 /* Check if Media Autosense is enabled */
4171 /* print pcie link status and MAC address */
4172 pcie_print_link_status(pdev);
4173 netdev_info(netdev, "MAC: %pM\n", netdev->dev_addr);
4178 igc_release_hw_control(adapter);
4180 if (!igc_check_reset_block(hw))
4183 igc_clear_interrupt_scheme(adapter);
4184 iounmap(adapter->io_addr);
4186 free_netdev(netdev);
4188 pci_release_selected_regions(pdev,
4189 pci_select_bars(pdev, IORESOURCE_MEM));
4192 pci_disable_device(pdev);
4197 * igc_remove - Device Removal Routine
4198 * @pdev: PCI device information struct
4200 * igc_remove is called by the PCI subsystem to alert the driver
4201 * that it should release a PCI device. This could be caused by a
4202 * Hot-Plug event, or because the driver is going to be removed from
4205 static void igc_remove(struct pci_dev *pdev)
4207 struct net_device *netdev = pci_get_drvdata(pdev);
4208 struct igc_adapter *adapter = netdev_priv(netdev);
4210 set_bit(__IGC_DOWN, &adapter->state);
4212 del_timer_sync(&adapter->watchdog_timer);
4213 del_timer_sync(&adapter->phy_info_timer);
4215 cancel_work_sync(&adapter->reset_task);
4216 cancel_work_sync(&adapter->watchdog_task);
4218 /* Release control of h/w to f/w. If f/w is AMT enabled, this
4219 * would have already happened in close and is redundant.
4221 igc_release_hw_control(adapter);
4222 unregister_netdev(netdev);
4224 igc_clear_interrupt_scheme(adapter);
4225 pci_iounmap(pdev, adapter->io_addr);
4226 pci_release_mem_regions(pdev);
4228 kfree(adapter->mac_table);
4229 kfree(adapter->shadow_vfta);
4230 free_netdev(netdev);
4232 pci_disable_pcie_error_reporting(pdev);
4234 pci_disable_device(pdev);
4237 static struct pci_driver igc_driver = {
4238 .name = igc_driver_name,
4239 .id_table = igc_pci_tbl,
4241 .remove = igc_remove,
4244 void igc_set_flag_queue_pairs(struct igc_adapter *adapter,
4245 const u32 max_rss_queues)
4247 /* Determine if we need to pair queues. */
4248 /* If rss_queues > half of max_rss_queues, pair the queues in
4249 * order to conserve interrupts due to limited supply.
4251 if (adapter->rss_queues > (max_rss_queues / 2))
4252 adapter->flags |= IGC_FLAG_QUEUE_PAIRS;
4254 adapter->flags &= ~IGC_FLAG_QUEUE_PAIRS;
4257 unsigned int igc_get_max_rss_queues(struct igc_adapter *adapter)
4259 unsigned int max_rss_queues;
4261 /* Determine the maximum number of RSS queues supported. */
4262 max_rss_queues = IGC_MAX_RX_QUEUES;
4264 return max_rss_queues;
4267 static void igc_init_queue_configuration(struct igc_adapter *adapter)
4271 max_rss_queues = igc_get_max_rss_queues(adapter);
4272 adapter->rss_queues = min_t(u32, max_rss_queues, num_online_cpus());
4274 igc_set_flag_queue_pairs(adapter, max_rss_queues);
4278 * igc_sw_init - Initialize general software structures (struct igc_adapter)
4279 * @adapter: board private structure to initialize
4281 * igc_sw_init initializes the Adapter private data structure.
4282 * Fields are initialized based on PCI device information and
4283 * OS network device settings (MTU size).
4285 static int igc_sw_init(struct igc_adapter *adapter)
4287 struct net_device *netdev = adapter->netdev;
4288 struct pci_dev *pdev = adapter->pdev;
4289 struct igc_hw *hw = &adapter->hw;
4291 int size = sizeof(struct igc_mac_addr) * hw->mac.rar_entry_count;
4293 pci_read_config_word(pdev, PCI_COMMAND, &hw->bus.pci_cmd_word);
4295 /* set default ring sizes */
4296 adapter->tx_ring_count = IGC_DEFAULT_TXD;
4297 adapter->rx_ring_count = IGC_DEFAULT_RXD;
4299 /* set default ITR values */
4300 adapter->rx_itr_setting = IGC_DEFAULT_ITR;
4301 adapter->tx_itr_setting = IGC_DEFAULT_ITR;
4303 /* set default work limits */
4304 adapter->tx_work_limit = IGC_DEFAULT_TX_WORK;
4306 /* adjust max frame to be at least the size of a standard frame */
4307 adapter->max_frame_size = netdev->mtu + ETH_HLEN + ETH_FCS_LEN +
4309 adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
4311 spin_lock_init(&adapter->nfc_lock);
4312 spin_lock_init(&adapter->stats64_lock);
4313 /* Assume MSI-X interrupts, will be checked during IRQ allocation */
4314 adapter->flags |= IGC_FLAG_HAS_MSIX;
4316 adapter->mac_table = kzalloc(size, GFP_ATOMIC);
4317 if (!adapter->mac_table)
4320 igc_init_queue_configuration(adapter);
4322 /* This call may decrease the number of queues */
4323 if (igc_init_interrupt_scheme(adapter, true)) {
4324 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
4328 /* Explicitly disable IRQ since the NIC can be in any state. */
4329 igc_irq_disable(adapter);
4331 set_bit(__IGC_DOWN, &adapter->state);
4337 * igc_reinit_queues - return error
4338 * @adapter: pointer to adapter structure
4340 int igc_reinit_queues(struct igc_adapter *adapter)
4342 struct net_device *netdev = adapter->netdev;
4343 struct pci_dev *pdev = adapter->pdev;
4346 if (netif_running(netdev))
4349 igc_reset_interrupt_capability(adapter);
4351 if (igc_init_interrupt_scheme(adapter, true)) {
4352 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
4356 if (netif_running(netdev))
4357 err = igc_open(netdev);
4363 * igc_get_hw_dev - return device
4364 * @hw: pointer to hardware structure
4366 * used by hardware layer to print debugging information
4368 struct net_device *igc_get_hw_dev(struct igc_hw *hw)
4370 struct igc_adapter *adapter = hw->back;
4372 return adapter->netdev;
4376 * igc_init_module - Driver Registration Routine
4378 * igc_init_module is the first routine called when the driver is
4379 * loaded. All it does is register with the PCI subsystem.
4381 static int __init igc_init_module(void)
4385 pr_info("%s - version %s\n",
4386 igc_driver_string, igc_driver_version);
4388 pr_info("%s\n", igc_copyright);
4390 ret = pci_register_driver(&igc_driver);
4394 module_init(igc_init_module);
4397 * igc_exit_module - Driver Exit Cleanup Routine
4399 * igc_exit_module is called just before the driver is removed
4402 static void __exit igc_exit_module(void)
4404 pci_unregister_driver(&igc_driver);
4407 module_exit(igc_exit_module);