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)
626 * igc_setup_rctl - configure the receive control registers
627 * @adapter: Board private structure
629 static void igc_setup_rctl(struct igc_adapter *adapter)
631 struct igc_hw *hw = &adapter->hw;
634 rctl = rd32(IGC_RCTL);
636 rctl &= ~(3 << IGC_RCTL_MO_SHIFT);
637 rctl &= ~(IGC_RCTL_LBM_TCVR | IGC_RCTL_LBM_MAC);
639 rctl |= IGC_RCTL_EN | IGC_RCTL_BAM | IGC_RCTL_RDMTS_HALF |
640 (hw->mac.mc_filter_type << IGC_RCTL_MO_SHIFT);
642 /* enable stripping of CRC. Newer features require
643 * that the HW strips the CRC.
645 rctl |= IGC_RCTL_SECRC;
647 /* disable store bad packets and clear size bits. */
648 rctl &= ~(IGC_RCTL_SBP | IGC_RCTL_SZ_256);
650 /* enable LPE to allow for reception of jumbo frames */
651 rctl |= IGC_RCTL_LPE;
653 /* disable queue 0 to prevent tail write w/o re-config */
654 wr32(IGC_RXDCTL(0), 0);
656 /* This is useful for sniffing bad packets. */
657 if (adapter->netdev->features & NETIF_F_RXALL) {
658 /* UPE and MPE will be handled by normal PROMISC logic
661 rctl |= (IGC_RCTL_SBP | /* Receive bad packets */
662 IGC_RCTL_BAM | /* RX All Bcast Pkts */
663 IGC_RCTL_PMCF); /* RX All MAC Ctrl Pkts */
665 rctl &= ~(IGC_RCTL_DPF | /* Allow filtered pause */
666 IGC_RCTL_CFIEN); /* Disable VLAN CFIEN Filter */
669 wr32(IGC_RCTL, rctl);
673 * igc_setup_tctl - configure the transmit control registers
674 * @adapter: Board private structure
676 static void igc_setup_tctl(struct igc_adapter *adapter)
678 struct igc_hw *hw = &adapter->hw;
681 /* disable queue 0 which icould be enabled by default */
682 wr32(IGC_TXDCTL(0), 0);
684 /* Program the Transmit Control Register */
685 tctl = rd32(IGC_TCTL);
686 tctl &= ~IGC_TCTL_CT;
687 tctl |= IGC_TCTL_PSP | IGC_TCTL_RTLC |
688 (IGC_COLLISION_THRESHOLD << IGC_CT_SHIFT);
690 /* Enable transmits */
693 wr32(IGC_TCTL, tctl);
697 * igc_set_mac - Change the Ethernet Address of the NIC
698 * @netdev: network interface device structure
699 * @p: pointer to an address structure
701 * Returns 0 on success, negative on failure
703 static int igc_set_mac(struct net_device *netdev, void *p)
705 struct igc_adapter *adapter = netdev_priv(netdev);
706 struct igc_hw *hw = &adapter->hw;
707 struct sockaddr *addr = p;
709 if (!is_valid_ether_addr(addr->sa_data))
710 return -EADDRNOTAVAIL;
712 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
713 memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
715 /* set the correct pool for the new PF MAC address in entry 0 */
716 igc_set_default_mac_filter(adapter);
721 static void igc_tx_csum(struct igc_ring *tx_ring, struct igc_tx_buffer *first)
725 static int __igc_maybe_stop_tx(struct igc_ring *tx_ring, const u16 size)
727 struct net_device *netdev = tx_ring->netdev;
729 netif_stop_subqueue(netdev, tx_ring->queue_index);
731 /* memory barriier comment */
734 /* We need to check again in a case another CPU has just
735 * made room available.
737 if (igc_desc_unused(tx_ring) < size)
741 netif_wake_subqueue(netdev, tx_ring->queue_index);
743 u64_stats_update_begin(&tx_ring->tx_syncp2);
744 tx_ring->tx_stats.restart_queue2++;
745 u64_stats_update_end(&tx_ring->tx_syncp2);
750 static inline int igc_maybe_stop_tx(struct igc_ring *tx_ring, const u16 size)
752 if (igc_desc_unused(tx_ring) >= size)
754 return __igc_maybe_stop_tx(tx_ring, size);
757 static u32 igc_tx_cmd_type(struct sk_buff *skb, u32 tx_flags)
759 /* set type for advanced descriptor with frame checksum insertion */
760 u32 cmd_type = IGC_ADVTXD_DTYP_DATA |
761 IGC_ADVTXD_DCMD_DEXT |
762 IGC_ADVTXD_DCMD_IFCS;
767 static void igc_tx_olinfo_status(struct igc_ring *tx_ring,
768 union igc_adv_tx_desc *tx_desc,
769 u32 tx_flags, unsigned int paylen)
771 u32 olinfo_status = paylen << IGC_ADVTXD_PAYLEN_SHIFT;
773 /* insert L4 checksum */
774 olinfo_status |= (tx_flags & IGC_TX_FLAGS_CSUM) *
775 ((IGC_TXD_POPTS_TXSM << 8) /
778 /* insert IPv4 checksum */
779 olinfo_status |= (tx_flags & IGC_TX_FLAGS_IPV4) *
780 (((IGC_TXD_POPTS_IXSM << 8)) /
783 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
786 static int igc_tx_map(struct igc_ring *tx_ring,
787 struct igc_tx_buffer *first,
790 struct sk_buff *skb = first->skb;
791 struct igc_tx_buffer *tx_buffer;
792 union igc_adv_tx_desc *tx_desc;
793 u32 tx_flags = first->tx_flags;
794 struct skb_frag_struct *frag;
795 u16 i = tx_ring->next_to_use;
796 unsigned int data_len, size;
798 u32 cmd_type = igc_tx_cmd_type(skb, tx_flags);
800 tx_desc = IGC_TX_DESC(tx_ring, i);
802 igc_tx_olinfo_status(tx_ring, tx_desc, tx_flags, skb->len - hdr_len);
804 size = skb_headlen(skb);
805 data_len = skb->data_len;
807 dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
811 for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
812 if (dma_mapping_error(tx_ring->dev, dma))
815 /* record length, and DMA address */
816 dma_unmap_len_set(tx_buffer, len, size);
817 dma_unmap_addr_set(tx_buffer, dma, dma);
819 tx_desc->read.buffer_addr = cpu_to_le64(dma);
821 while (unlikely(size > IGC_MAX_DATA_PER_TXD)) {
822 tx_desc->read.cmd_type_len =
823 cpu_to_le32(cmd_type ^ IGC_MAX_DATA_PER_TXD);
827 if (i == tx_ring->count) {
828 tx_desc = IGC_TX_DESC(tx_ring, 0);
831 tx_desc->read.olinfo_status = 0;
833 dma += IGC_MAX_DATA_PER_TXD;
834 size -= IGC_MAX_DATA_PER_TXD;
836 tx_desc->read.buffer_addr = cpu_to_le64(dma);
839 if (likely(!data_len))
842 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type ^ size);
846 if (i == tx_ring->count) {
847 tx_desc = IGC_TX_DESC(tx_ring, 0);
850 tx_desc->read.olinfo_status = 0;
852 size = skb_frag_size(frag);
855 dma = skb_frag_dma_map(tx_ring->dev, frag, 0,
856 size, DMA_TO_DEVICE);
858 tx_buffer = &tx_ring->tx_buffer_info[i];
861 /* write last descriptor with RS and EOP bits */
862 cmd_type |= size | IGC_TXD_DCMD;
863 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type);
865 netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount);
867 /* set the timestamp */
868 first->time_stamp = jiffies;
870 skb_tx_timestamp(skb);
872 /* Force memory writes to complete before letting h/w know there
873 * are new descriptors to fetch. (Only applicable for weak-ordered
874 * memory model archs, such as IA-64).
876 * We also need this memory barrier to make certain all of the
877 * status bits have been updated before next_to_watch is written.
881 /* set next_to_watch value indicating a packet is present */
882 first->next_to_watch = tx_desc;
885 if (i == tx_ring->count)
888 tx_ring->next_to_use = i;
890 /* Make sure there is space in the ring for the next send. */
891 igc_maybe_stop_tx(tx_ring, DESC_NEEDED);
893 if (netif_xmit_stopped(txring_txq(tx_ring)) || !skb->xmit_more) {
894 writel(i, tx_ring->tail);
899 dev_err(tx_ring->dev, "TX DMA map failed\n");
900 tx_buffer = &tx_ring->tx_buffer_info[i];
902 /* clear dma mappings for failed tx_buffer_info map */
903 while (tx_buffer != first) {
904 if (dma_unmap_len(tx_buffer, len))
905 dma_unmap_page(tx_ring->dev,
906 dma_unmap_addr(tx_buffer, dma),
907 dma_unmap_len(tx_buffer, len),
909 dma_unmap_len_set(tx_buffer, len, 0);
913 tx_buffer = &tx_ring->tx_buffer_info[i];
916 if (dma_unmap_len(tx_buffer, len))
917 dma_unmap_single(tx_ring->dev,
918 dma_unmap_addr(tx_buffer, dma),
919 dma_unmap_len(tx_buffer, len),
921 dma_unmap_len_set(tx_buffer, len, 0);
923 dev_kfree_skb_any(tx_buffer->skb);
924 tx_buffer->skb = NULL;
926 tx_ring->next_to_use = i;
931 static netdev_tx_t igc_xmit_frame_ring(struct sk_buff *skb,
932 struct igc_ring *tx_ring)
934 u16 count = TXD_USE_COUNT(skb_headlen(skb));
935 __be16 protocol = vlan_get_protocol(skb);
936 struct igc_tx_buffer *first;
941 /* need: 1 descriptor per page * PAGE_SIZE/IGC_MAX_DATA_PER_TXD,
942 * + 1 desc for skb_headlen/IGC_MAX_DATA_PER_TXD,
943 * + 2 desc gap to keep tail from touching head,
944 * + 1 desc for context descriptor,
945 * otherwise try next time
947 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
948 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
950 if (igc_maybe_stop_tx(tx_ring, count + 3)) {
951 /* this is a hard error */
952 return NETDEV_TX_BUSY;
955 /* record the location of the first descriptor for this packet */
956 first = &tx_ring->tx_buffer_info[tx_ring->next_to_use];
958 first->bytecount = skb->len;
961 /* record initial flags and protocol */
962 first->tx_flags = tx_flags;
963 first->protocol = protocol;
965 igc_tx_csum(tx_ring, first);
967 igc_tx_map(tx_ring, first, hdr_len);
972 static inline struct igc_ring *igc_tx_queue_mapping(struct igc_adapter *adapter,
975 unsigned int r_idx = skb->queue_mapping;
977 if (r_idx >= adapter->num_tx_queues)
978 r_idx = r_idx % adapter->num_tx_queues;
980 return adapter->tx_ring[r_idx];
983 static netdev_tx_t igc_xmit_frame(struct sk_buff *skb,
984 struct net_device *netdev)
986 struct igc_adapter *adapter = netdev_priv(netdev);
988 /* The minimum packet size with TCTL.PSP set is 17 so pad the skb
989 * in order to meet this minimum size requirement.
992 if (skb_padto(skb, 17))
997 return igc_xmit_frame_ring(skb, igc_tx_queue_mapping(adapter, skb));
1000 static inline void igc_rx_hash(struct igc_ring *ring,
1001 union igc_adv_rx_desc *rx_desc,
1002 struct sk_buff *skb)
1004 if (ring->netdev->features & NETIF_F_RXHASH)
1006 le32_to_cpu(rx_desc->wb.lower.hi_dword.rss),
1011 * igc_process_skb_fields - Populate skb header fields from Rx descriptor
1012 * @rx_ring: rx descriptor ring packet is being transacted on
1013 * @rx_desc: pointer to the EOP Rx descriptor
1014 * @skb: pointer to current skb being populated
1016 * This function checks the ring, descriptor, and packet information in
1017 * order to populate the hash, checksum, VLAN, timestamp, protocol, and
1018 * other fields within the skb.
1020 static void igc_process_skb_fields(struct igc_ring *rx_ring,
1021 union igc_adv_rx_desc *rx_desc,
1022 struct sk_buff *skb)
1024 igc_rx_hash(rx_ring, rx_desc, skb);
1026 skb_record_rx_queue(skb, rx_ring->queue_index);
1028 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
1031 static struct igc_rx_buffer *igc_get_rx_buffer(struct igc_ring *rx_ring,
1032 const unsigned int size)
1034 struct igc_rx_buffer *rx_buffer;
1036 rx_buffer = &rx_ring->rx_buffer_info[rx_ring->next_to_clean];
1037 prefetchw(rx_buffer->page);
1039 /* we are reusing so sync this buffer for CPU use */
1040 dma_sync_single_range_for_cpu(rx_ring->dev,
1042 rx_buffer->page_offset,
1046 rx_buffer->pagecnt_bias--;
1052 * igc_add_rx_frag - Add contents of Rx buffer to sk_buff
1053 * @rx_ring: rx descriptor ring to transact packets on
1054 * @rx_buffer: buffer containing page to add
1055 * @skb: sk_buff to place the data into
1056 * @size: size of buffer to be added
1058 * This function will add the data contained in rx_buffer->page to the skb.
1060 static void igc_add_rx_frag(struct igc_ring *rx_ring,
1061 struct igc_rx_buffer *rx_buffer,
1062 struct sk_buff *skb,
1065 #if (PAGE_SIZE < 8192)
1066 unsigned int truesize = igc_rx_pg_size(rx_ring) / 2;
1068 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_buffer->page,
1069 rx_buffer->page_offset, size, truesize);
1070 rx_buffer->page_offset ^= truesize;
1072 unsigned int truesize = ring_uses_build_skb(rx_ring) ?
1073 SKB_DATA_ALIGN(IGC_SKB_PAD + size) :
1074 SKB_DATA_ALIGN(size);
1075 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_buffer->page,
1076 rx_buffer->page_offset, size, truesize);
1077 rx_buffer->page_offset += truesize;
1081 static struct sk_buff *igc_build_skb(struct igc_ring *rx_ring,
1082 struct igc_rx_buffer *rx_buffer,
1083 union igc_adv_rx_desc *rx_desc,
1086 void *va = page_address(rx_buffer->page) + rx_buffer->page_offset;
1087 #if (PAGE_SIZE < 8192)
1088 unsigned int truesize = igc_rx_pg_size(rx_ring) / 2;
1090 unsigned int truesize = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
1091 SKB_DATA_ALIGN(IGC_SKB_PAD + size);
1093 struct sk_buff *skb;
1095 /* prefetch first cache line of first page */
1097 #if L1_CACHE_BYTES < 128
1098 prefetch(va + L1_CACHE_BYTES);
1101 /* build an skb around the page buffer */
1102 skb = build_skb(va - IGC_SKB_PAD, truesize);
1106 /* update pointers within the skb to store the data */
1107 skb_reserve(skb, IGC_SKB_PAD);
1108 __skb_put(skb, size);
1110 /* update buffer offset */
1111 #if (PAGE_SIZE < 8192)
1112 rx_buffer->page_offset ^= truesize;
1114 rx_buffer->page_offset += truesize;
1120 static struct sk_buff *igc_construct_skb(struct igc_ring *rx_ring,
1121 struct igc_rx_buffer *rx_buffer,
1122 union igc_adv_rx_desc *rx_desc,
1125 void *va = page_address(rx_buffer->page) + rx_buffer->page_offset;
1126 #if (PAGE_SIZE < 8192)
1127 unsigned int truesize = igc_rx_pg_size(rx_ring) / 2;
1129 unsigned int truesize = SKB_DATA_ALIGN(size);
1131 unsigned int headlen;
1132 struct sk_buff *skb;
1134 /* prefetch first cache line of first page */
1136 #if L1_CACHE_BYTES < 128
1137 prefetch(va + L1_CACHE_BYTES);
1140 /* allocate a skb to store the frags */
1141 skb = napi_alloc_skb(&rx_ring->q_vector->napi, IGC_RX_HDR_LEN);
1145 /* Determine available headroom for copy */
1147 if (headlen > IGC_RX_HDR_LEN)
1148 headlen = eth_get_headlen(va, IGC_RX_HDR_LEN);
1150 /* align pull length to size of long to optimize memcpy performance */
1151 memcpy(__skb_put(skb, headlen), va, ALIGN(headlen, sizeof(long)));
1153 /* update all of the pointers */
1156 skb_add_rx_frag(skb, 0, rx_buffer->page,
1157 (va + headlen) - page_address(rx_buffer->page),
1159 #if (PAGE_SIZE < 8192)
1160 rx_buffer->page_offset ^= truesize;
1162 rx_buffer->page_offset += truesize;
1165 rx_buffer->pagecnt_bias++;
1172 * igc_reuse_rx_page - page flip buffer and store it back on the ring
1173 * @rx_ring: rx descriptor ring to store buffers on
1174 * @old_buff: donor buffer to have page reused
1176 * Synchronizes page for reuse by the adapter
1178 static void igc_reuse_rx_page(struct igc_ring *rx_ring,
1179 struct igc_rx_buffer *old_buff)
1181 u16 nta = rx_ring->next_to_alloc;
1182 struct igc_rx_buffer *new_buff;
1184 new_buff = &rx_ring->rx_buffer_info[nta];
1186 /* update, and store next to alloc */
1188 rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
1190 /* Transfer page from old buffer to new buffer.
1191 * Move each member individually to avoid possible store
1192 * forwarding stalls.
1194 new_buff->dma = old_buff->dma;
1195 new_buff->page = old_buff->page;
1196 new_buff->page_offset = old_buff->page_offset;
1197 new_buff->pagecnt_bias = old_buff->pagecnt_bias;
1200 static inline bool igc_page_is_reserved(struct page *page)
1202 return (page_to_nid(page) != numa_mem_id()) || page_is_pfmemalloc(page);
1205 static bool igc_can_reuse_rx_page(struct igc_rx_buffer *rx_buffer)
1207 unsigned int pagecnt_bias = rx_buffer->pagecnt_bias;
1208 struct page *page = rx_buffer->page;
1210 /* avoid re-using remote pages */
1211 if (unlikely(igc_page_is_reserved(page)))
1214 #if (PAGE_SIZE < 8192)
1215 /* if we are only owner of page we can reuse it */
1216 if (unlikely((page_ref_count(page) - pagecnt_bias) > 1))
1219 #define IGC_LAST_OFFSET \
1220 (SKB_WITH_OVERHEAD(PAGE_SIZE) - IGC_RXBUFFER_2048)
1222 if (rx_buffer->page_offset > IGC_LAST_OFFSET)
1226 /* If we have drained the page fragment pool we need to update
1227 * the pagecnt_bias and page count so that we fully restock the
1228 * number of references the driver holds.
1230 if (unlikely(!pagecnt_bias)) {
1231 page_ref_add(page, USHRT_MAX);
1232 rx_buffer->pagecnt_bias = USHRT_MAX;
1239 * igc_is_non_eop - process handling of non-EOP buffers
1240 * @rx_ring: Rx ring being processed
1241 * @rx_desc: Rx descriptor for current buffer
1242 * @skb: current socket buffer containing buffer in progress
1244 * This function updates next to clean. If the buffer is an EOP buffer
1245 * this function exits returning false, otherwise it will place the
1246 * sk_buff in the next buffer to be chained and return true indicating
1247 * that this is in fact a non-EOP buffer.
1249 static bool igc_is_non_eop(struct igc_ring *rx_ring,
1250 union igc_adv_rx_desc *rx_desc)
1252 u32 ntc = rx_ring->next_to_clean + 1;
1254 /* fetch, update, and store next to clean */
1255 ntc = (ntc < rx_ring->count) ? ntc : 0;
1256 rx_ring->next_to_clean = ntc;
1258 prefetch(IGC_RX_DESC(rx_ring, ntc));
1260 if (likely(igc_test_staterr(rx_desc, IGC_RXD_STAT_EOP)))
1267 * igc_cleanup_headers - Correct corrupted or empty headers
1268 * @rx_ring: rx descriptor ring packet is being transacted on
1269 * @rx_desc: pointer to the EOP Rx descriptor
1270 * @skb: pointer to current skb being fixed
1272 * Address the case where we are pulling data in on pages only
1273 * and as such no data is present in the skb header.
1275 * In addition if skb is not at least 60 bytes we need to pad it so that
1276 * it is large enough to qualify as a valid Ethernet frame.
1278 * Returns true if an error was encountered and skb was freed.
1280 static bool igc_cleanup_headers(struct igc_ring *rx_ring,
1281 union igc_adv_rx_desc *rx_desc,
1282 struct sk_buff *skb)
1284 if (unlikely((igc_test_staterr(rx_desc,
1285 IGC_RXDEXT_ERR_FRAME_ERR_MASK)))) {
1286 struct net_device *netdev = rx_ring->netdev;
1288 if (!(netdev->features & NETIF_F_RXALL)) {
1289 dev_kfree_skb_any(skb);
1294 /* if eth_skb_pad returns an error the skb was freed */
1295 if (eth_skb_pad(skb))
1301 static void igc_put_rx_buffer(struct igc_ring *rx_ring,
1302 struct igc_rx_buffer *rx_buffer)
1304 if (igc_can_reuse_rx_page(rx_buffer)) {
1305 /* hand second half of page back to the ring */
1306 igc_reuse_rx_page(rx_ring, rx_buffer);
1308 /* We are not reusing the buffer so unmap it and free
1309 * any references we are holding to it
1311 dma_unmap_page_attrs(rx_ring->dev, rx_buffer->dma,
1312 igc_rx_pg_size(rx_ring), DMA_FROM_DEVICE,
1314 __page_frag_cache_drain(rx_buffer->page,
1315 rx_buffer->pagecnt_bias);
1318 /* clear contents of rx_buffer */
1319 rx_buffer->page = NULL;
1323 * igc_alloc_rx_buffers - Replace used receive buffers; packet split
1324 * @adapter: address of board private structure
1326 static void igc_alloc_rx_buffers(struct igc_ring *rx_ring, u16 cleaned_count)
1328 union igc_adv_rx_desc *rx_desc;
1329 u16 i = rx_ring->next_to_use;
1330 struct igc_rx_buffer *bi;
1337 rx_desc = IGC_RX_DESC(rx_ring, i);
1338 bi = &rx_ring->rx_buffer_info[i];
1339 i -= rx_ring->count;
1341 bufsz = igc_rx_bufsz(rx_ring);
1344 if (!igc_alloc_mapped_page(rx_ring, bi))
1347 /* sync the buffer for use by the device */
1348 dma_sync_single_range_for_device(rx_ring->dev, bi->dma,
1349 bi->page_offset, bufsz,
1352 /* Refresh the desc even if buffer_addrs didn't change
1353 * because each write-back erases this info.
1355 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
1361 rx_desc = IGC_RX_DESC(rx_ring, 0);
1362 bi = rx_ring->rx_buffer_info;
1363 i -= rx_ring->count;
1366 /* clear the length for the next_to_use descriptor */
1367 rx_desc->wb.upper.length = 0;
1370 } while (cleaned_count);
1372 i += rx_ring->count;
1374 if (rx_ring->next_to_use != i) {
1375 /* record the next descriptor to use */
1376 rx_ring->next_to_use = i;
1378 /* update next to alloc since we have filled the ring */
1379 rx_ring->next_to_alloc = i;
1381 /* Force memory writes to complete before letting h/w
1382 * know there are new descriptors to fetch. (Only
1383 * applicable for weak-ordered memory model archs,
1387 writel(i, rx_ring->tail);
1391 static int igc_clean_rx_irq(struct igc_q_vector *q_vector, const int budget)
1393 unsigned int total_bytes = 0, total_packets = 0;
1394 struct igc_ring *rx_ring = q_vector->rx.ring;
1395 struct sk_buff *skb = rx_ring->skb;
1396 u16 cleaned_count = igc_desc_unused(rx_ring);
1398 while (likely(total_packets < budget)) {
1399 union igc_adv_rx_desc *rx_desc;
1400 struct igc_rx_buffer *rx_buffer;
1403 /* return some buffers to hardware, one at a time is too slow */
1404 if (cleaned_count >= IGC_RX_BUFFER_WRITE) {
1405 igc_alloc_rx_buffers(rx_ring, cleaned_count);
1409 rx_desc = IGC_RX_DESC(rx_ring, rx_ring->next_to_clean);
1410 size = le16_to_cpu(rx_desc->wb.upper.length);
1414 /* This memory barrier is needed to keep us from reading
1415 * any other fields out of the rx_desc until we know the
1416 * descriptor has been written back
1420 rx_buffer = igc_get_rx_buffer(rx_ring, size);
1422 /* retrieve a buffer from the ring */
1424 igc_add_rx_frag(rx_ring, rx_buffer, skb, size);
1425 else if (ring_uses_build_skb(rx_ring))
1426 skb = igc_build_skb(rx_ring, rx_buffer, rx_desc, size);
1428 skb = igc_construct_skb(rx_ring, rx_buffer,
1431 /* exit if we failed to retrieve a buffer */
1433 rx_ring->rx_stats.alloc_failed++;
1434 rx_buffer->pagecnt_bias++;
1438 igc_put_rx_buffer(rx_ring, rx_buffer);
1441 /* fetch next buffer in frame if non-eop */
1442 if (igc_is_non_eop(rx_ring, rx_desc))
1445 /* verify the packet layout is correct */
1446 if (igc_cleanup_headers(rx_ring, rx_desc, skb)) {
1451 /* probably a little skewed due to removing CRC */
1452 total_bytes += skb->len;
1454 /* populate checksum, timestamp, VLAN, and protocol */
1455 igc_process_skb_fields(rx_ring, rx_desc, skb);
1457 napi_gro_receive(&q_vector->napi, skb);
1459 /* reset skb pointer */
1462 /* update budget accounting */
1466 /* place incomplete frames back on ring for completion */
1469 u64_stats_update_begin(&rx_ring->rx_syncp);
1470 rx_ring->rx_stats.packets += total_packets;
1471 rx_ring->rx_stats.bytes += total_bytes;
1472 u64_stats_update_end(&rx_ring->rx_syncp);
1473 q_vector->rx.total_packets += total_packets;
1474 q_vector->rx.total_bytes += total_bytes;
1477 igc_alloc_rx_buffers(rx_ring, cleaned_count);
1479 return total_packets;
1482 static inline unsigned int igc_rx_offset(struct igc_ring *rx_ring)
1484 return ring_uses_build_skb(rx_ring) ? IGC_SKB_PAD : 0;
1487 static bool igc_alloc_mapped_page(struct igc_ring *rx_ring,
1488 struct igc_rx_buffer *bi)
1490 struct page *page = bi->page;
1493 /* since we are recycling buffers we should seldom need to alloc */
1497 /* alloc new page for storage */
1498 page = dev_alloc_pages(igc_rx_pg_order(rx_ring));
1499 if (unlikely(!page)) {
1500 rx_ring->rx_stats.alloc_failed++;
1504 /* map page for use */
1505 dma = dma_map_page_attrs(rx_ring->dev, page, 0,
1506 igc_rx_pg_size(rx_ring),
1510 /* if mapping failed free memory back to system since
1511 * there isn't much point in holding memory we can't use
1513 if (dma_mapping_error(rx_ring->dev, dma)) {
1516 rx_ring->rx_stats.alloc_failed++;
1522 bi->page_offset = igc_rx_offset(rx_ring);
1523 bi->pagecnt_bias = 1;
1529 * igc_clean_tx_irq - Reclaim resources after transmit completes
1530 * @q_vector: pointer to q_vector containing needed info
1531 * @napi_budget: Used to determine if we are in netpoll
1533 * returns true if ring is completely cleaned
1535 static bool igc_clean_tx_irq(struct igc_q_vector *q_vector, int napi_budget)
1537 struct igc_adapter *adapter = q_vector->adapter;
1538 unsigned int total_bytes = 0, total_packets = 0;
1539 unsigned int budget = q_vector->tx.work_limit;
1540 struct igc_ring *tx_ring = q_vector->tx.ring;
1541 unsigned int i = tx_ring->next_to_clean;
1542 struct igc_tx_buffer *tx_buffer;
1543 union igc_adv_tx_desc *tx_desc;
1545 if (test_bit(__IGC_DOWN, &adapter->state))
1548 tx_buffer = &tx_ring->tx_buffer_info[i];
1549 tx_desc = IGC_TX_DESC(tx_ring, i);
1550 i -= tx_ring->count;
1553 union igc_adv_tx_desc *eop_desc = tx_buffer->next_to_watch;
1555 /* if next_to_watch is not set then there is no work pending */
1559 /* prevent any other reads prior to eop_desc */
1562 /* if DD is not set pending work has not been completed */
1563 if (!(eop_desc->wb.status & cpu_to_le32(IGC_TXD_STAT_DD)))
1566 /* clear next_to_watch to prevent false hangs */
1567 tx_buffer->next_to_watch = NULL;
1569 /* update the statistics for this packet */
1570 total_bytes += tx_buffer->bytecount;
1571 total_packets += tx_buffer->gso_segs;
1574 napi_consume_skb(tx_buffer->skb, napi_budget);
1576 /* unmap skb header data */
1577 dma_unmap_single(tx_ring->dev,
1578 dma_unmap_addr(tx_buffer, dma),
1579 dma_unmap_len(tx_buffer, len),
1582 /* clear tx_buffer data */
1583 dma_unmap_len_set(tx_buffer, len, 0);
1585 /* clear last DMA location and unmap remaining buffers */
1586 while (tx_desc != eop_desc) {
1591 i -= tx_ring->count;
1592 tx_buffer = tx_ring->tx_buffer_info;
1593 tx_desc = IGC_TX_DESC(tx_ring, 0);
1596 /* unmap any remaining paged data */
1597 if (dma_unmap_len(tx_buffer, len)) {
1598 dma_unmap_page(tx_ring->dev,
1599 dma_unmap_addr(tx_buffer, dma),
1600 dma_unmap_len(tx_buffer, len),
1602 dma_unmap_len_set(tx_buffer, len, 0);
1606 /* move us one more past the eop_desc for start of next pkt */
1611 i -= tx_ring->count;
1612 tx_buffer = tx_ring->tx_buffer_info;
1613 tx_desc = IGC_TX_DESC(tx_ring, 0);
1616 /* issue prefetch for next Tx descriptor */
1619 /* update budget accounting */
1621 } while (likely(budget));
1623 netdev_tx_completed_queue(txring_txq(tx_ring),
1624 total_packets, total_bytes);
1626 i += tx_ring->count;
1627 tx_ring->next_to_clean = i;
1628 u64_stats_update_begin(&tx_ring->tx_syncp);
1629 tx_ring->tx_stats.bytes += total_bytes;
1630 tx_ring->tx_stats.packets += total_packets;
1631 u64_stats_update_end(&tx_ring->tx_syncp);
1632 q_vector->tx.total_bytes += total_bytes;
1633 q_vector->tx.total_packets += total_packets;
1635 if (test_bit(IGC_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags)) {
1636 struct igc_hw *hw = &adapter->hw;
1638 /* Detect a transmit hang in hardware, this serializes the
1639 * check with the clearing of time_stamp and movement of i
1641 clear_bit(IGC_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags);
1642 if (tx_buffer->next_to_watch &&
1643 time_after(jiffies, tx_buffer->time_stamp +
1644 (adapter->tx_timeout_factor * HZ)) &&
1645 !(rd32(IGC_STATUS) & IGC_STATUS_TXOFF)) {
1646 /* detected Tx unit hang */
1647 dev_err(tx_ring->dev,
1648 "Detected Tx Unit Hang\n"
1652 " next_to_use <%x>\n"
1653 " next_to_clean <%x>\n"
1654 "buffer_info[next_to_clean]\n"
1655 " time_stamp <%lx>\n"
1656 " next_to_watch <%p>\n"
1658 " desc.status <%x>\n",
1659 tx_ring->queue_index,
1660 rd32(IGC_TDH(tx_ring->reg_idx)),
1661 readl(tx_ring->tail),
1662 tx_ring->next_to_use,
1663 tx_ring->next_to_clean,
1664 tx_buffer->time_stamp,
1665 tx_buffer->next_to_watch,
1667 tx_buffer->next_to_watch->wb.status);
1668 netif_stop_subqueue(tx_ring->netdev,
1669 tx_ring->queue_index);
1671 /* we are about to reset, no point in enabling stuff */
1676 #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
1677 if (unlikely(total_packets &&
1678 netif_carrier_ok(tx_ring->netdev) &&
1679 igc_desc_unused(tx_ring) >= TX_WAKE_THRESHOLD)) {
1680 /* Make sure that anybody stopping the queue after this
1681 * sees the new next_to_clean.
1684 if (__netif_subqueue_stopped(tx_ring->netdev,
1685 tx_ring->queue_index) &&
1686 !(test_bit(__IGC_DOWN, &adapter->state))) {
1687 netif_wake_subqueue(tx_ring->netdev,
1688 tx_ring->queue_index);
1690 u64_stats_update_begin(&tx_ring->tx_syncp);
1691 tx_ring->tx_stats.restart_queue++;
1692 u64_stats_update_end(&tx_ring->tx_syncp);
1700 * igc_up - Open the interface and prepare it to handle traffic
1701 * @adapter: board private structure
1703 void igc_up(struct igc_adapter *adapter)
1705 struct igc_hw *hw = &adapter->hw;
1708 /* hardware has been reset, we need to reload some things */
1709 igc_configure(adapter);
1711 clear_bit(__IGC_DOWN, &adapter->state);
1713 for (i = 0; i < adapter->num_q_vectors; i++)
1714 napi_enable(&adapter->q_vector[i]->napi);
1716 if (adapter->msix_entries)
1717 igc_configure_msix(adapter);
1719 igc_assign_vector(adapter->q_vector[0], 0);
1721 /* Clear any pending interrupts. */
1723 igc_irq_enable(adapter);
1725 netif_tx_start_all_queues(adapter->netdev);
1727 /* start the watchdog. */
1728 hw->mac.get_link_status = 1;
1729 schedule_work(&adapter->watchdog_task);
1733 * igc_update_stats - Update the board statistics counters
1734 * @adapter: board private structure
1736 static void igc_update_stats(struct igc_adapter *adapter)
1740 static void igc_nfc_filter_exit(struct igc_adapter *adapter)
1745 * igc_down - Close the interface
1746 * @adapter: board private structure
1748 void igc_down(struct igc_adapter *adapter)
1750 struct net_device *netdev = adapter->netdev;
1751 struct igc_hw *hw = &adapter->hw;
1755 set_bit(__IGC_DOWN, &adapter->state);
1757 /* disable receives in the hardware */
1758 rctl = rd32(IGC_RCTL);
1759 wr32(IGC_RCTL, rctl & ~IGC_RCTL_EN);
1760 /* flush and sleep below */
1762 igc_nfc_filter_exit(adapter);
1764 /* set trans_start so we don't get spurious watchdogs during reset */
1765 netif_trans_update(netdev);
1767 netif_carrier_off(netdev);
1768 netif_tx_stop_all_queues(netdev);
1770 /* disable transmits in the hardware */
1771 tctl = rd32(IGC_TCTL);
1772 tctl &= ~IGC_TCTL_EN;
1773 wr32(IGC_TCTL, tctl);
1774 /* flush both disables and wait for them to finish */
1776 usleep_range(10000, 20000);
1778 igc_irq_disable(adapter);
1780 adapter->flags &= ~IGC_FLAG_NEED_LINK_UPDATE;
1782 for (i = 0; i < adapter->num_q_vectors; i++) {
1783 if (adapter->q_vector[i]) {
1784 napi_synchronize(&adapter->q_vector[i]->napi);
1785 napi_disable(&adapter->q_vector[i]->napi);
1789 del_timer_sync(&adapter->watchdog_timer);
1790 del_timer_sync(&adapter->phy_info_timer);
1792 /* record the stats before reset*/
1793 spin_lock(&adapter->stats64_lock);
1794 igc_update_stats(adapter);
1795 spin_unlock(&adapter->stats64_lock);
1797 adapter->link_speed = 0;
1798 adapter->link_duplex = 0;
1800 if (!pci_channel_offline(adapter->pdev))
1803 /* clear VLAN promisc flag so VFTA will be updated if necessary */
1804 adapter->flags &= ~IGC_FLAG_VLAN_PROMISC;
1806 igc_clean_all_tx_rings(adapter);
1807 igc_clean_all_rx_rings(adapter);
1810 void igc_reinit_locked(struct igc_adapter *adapter)
1812 WARN_ON(in_interrupt());
1813 while (test_and_set_bit(__IGC_RESETTING, &adapter->state))
1814 usleep_range(1000, 2000);
1817 clear_bit(__IGC_RESETTING, &adapter->state);
1820 static void igc_reset_task(struct work_struct *work)
1822 struct igc_adapter *adapter;
1824 adapter = container_of(work, struct igc_adapter, reset_task);
1826 netdev_err(adapter->netdev, "Reset adapter\n");
1827 igc_reinit_locked(adapter);
1831 * igc_change_mtu - Change the Maximum Transfer Unit
1832 * @netdev: network interface device structure
1833 * @new_mtu: new value for maximum frame size
1835 * Returns 0 on success, negative on failure
1837 static int igc_change_mtu(struct net_device *netdev, int new_mtu)
1839 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
1840 struct igc_adapter *adapter = netdev_priv(netdev);
1841 struct pci_dev *pdev = adapter->pdev;
1843 /* adjust max frame to be at least the size of a standard frame */
1844 if (max_frame < (ETH_FRAME_LEN + ETH_FCS_LEN))
1845 max_frame = ETH_FRAME_LEN + ETH_FCS_LEN;
1847 while (test_and_set_bit(__IGC_RESETTING, &adapter->state))
1848 usleep_range(1000, 2000);
1850 /* igc_down has a dependency on max_frame_size */
1851 adapter->max_frame_size = max_frame;
1853 if (netif_running(netdev))
1856 dev_info(&pdev->dev, "changing MTU from %d to %d\n",
1857 netdev->mtu, new_mtu);
1858 netdev->mtu = new_mtu;
1860 if (netif_running(netdev))
1865 clear_bit(__IGC_RESETTING, &adapter->state);
1871 * igc_get_stats - Get System Network Statistics
1872 * @netdev: network interface device structure
1874 * Returns the address of the device statistics structure.
1875 * The statistics are updated here and also from the timer callback.
1877 static struct net_device_stats *igc_get_stats(struct net_device *netdev)
1879 struct igc_adapter *adapter = netdev_priv(netdev);
1881 if (!test_bit(__IGC_RESETTING, &adapter->state))
1882 igc_update_stats(adapter);
1884 /* only return the current stats */
1885 return &netdev->stats;
1889 * igc_configure - configure the hardware for RX and TX
1890 * @adapter: private board structure
1892 static void igc_configure(struct igc_adapter *adapter)
1894 struct net_device *netdev = adapter->netdev;
1897 igc_get_hw_control(adapter);
1898 igc_set_rx_mode(netdev);
1900 igc_setup_tctl(adapter);
1901 igc_setup_mrqc(adapter);
1902 igc_setup_rctl(adapter);
1904 igc_configure_tx(adapter);
1905 igc_configure_rx(adapter);
1907 igc_rx_fifo_flush_base(&adapter->hw);
1909 /* call igc_desc_unused which always leaves
1910 * at least 1 descriptor unused to make sure
1911 * next_to_use != next_to_clean
1913 for (i = 0; i < adapter->num_rx_queues; i++) {
1914 struct igc_ring *ring = adapter->rx_ring[i];
1916 igc_alloc_rx_buffers(ring, igc_desc_unused(ring));
1921 * igc_rar_set_index - Sync RAL[index] and RAH[index] registers with MAC table
1922 * @adapter: address of board private structure
1923 * @index: Index of the RAR entry which need to be synced with MAC table
1925 static void igc_rar_set_index(struct igc_adapter *adapter, u32 index)
1927 u8 *addr = adapter->mac_table[index].addr;
1928 struct igc_hw *hw = &adapter->hw;
1929 u32 rar_low, rar_high;
1931 /* HW expects these to be in network order when they are plugged
1932 * into the registers which are little endian. In order to guarantee
1933 * that ordering we need to do an leXX_to_cpup here in order to be
1934 * ready for the byteswap that occurs with writel
1936 rar_low = le32_to_cpup((__le32 *)(addr));
1937 rar_high = le16_to_cpup((__le16 *)(addr + 4));
1939 /* Indicate to hardware the Address is Valid. */
1940 if (adapter->mac_table[index].state & IGC_MAC_STATE_IN_USE) {
1941 if (is_valid_ether_addr(addr))
1942 rar_high |= IGC_RAH_AV;
1944 rar_high |= IGC_RAH_POOL_1 <<
1945 adapter->mac_table[index].queue;
1948 wr32(IGC_RAL(index), rar_low);
1950 wr32(IGC_RAH(index), rar_high);
1954 /* Set default MAC address for the PF in the first RAR entry */
1955 static void igc_set_default_mac_filter(struct igc_adapter *adapter)
1957 struct igc_mac_addr *mac_table = &adapter->mac_table[0];
1959 ether_addr_copy(mac_table->addr, adapter->hw.mac.addr);
1960 mac_table->state = IGC_MAC_STATE_DEFAULT | IGC_MAC_STATE_IN_USE;
1962 igc_rar_set_index(adapter, 0);
1966 * igc_set_rx_mode - Secondary Unicast, Multicast and Promiscuous mode set
1967 * @netdev: network interface device structure
1969 * The set_rx_mode entry point is called whenever the unicast or multicast
1970 * address lists or the network interface flags are updated. This routine is
1971 * responsible for configuring the hardware for proper unicast, multicast,
1972 * promiscuous mode, and all-multi behavior.
1974 static void igc_set_rx_mode(struct net_device *netdev)
1979 * igc_msix_other - msix other interrupt handler
1980 * @irq: interrupt number
1981 * @data: pointer to a q_vector
1983 static irqreturn_t igc_msix_other(int irq, void *data)
1985 struct igc_adapter *adapter = data;
1986 struct igc_hw *hw = &adapter->hw;
1987 u32 icr = rd32(IGC_ICR);
1989 /* reading ICR causes bit 31 of EICR to be cleared */
1990 if (icr & IGC_ICR_DRSTA)
1991 schedule_work(&adapter->reset_task);
1993 if (icr & IGC_ICR_DOUTSYNC) {
1994 /* HW is reporting DMA is out of sync */
1995 adapter->stats.doosync++;
1998 if (icr & IGC_ICR_LSC) {
1999 hw->mac.get_link_status = 1;
2000 /* guard against interrupt when we're going down */
2001 if (!test_bit(__IGC_DOWN, &adapter->state))
2002 mod_timer(&adapter->watchdog_timer, jiffies + 1);
2005 wr32(IGC_EIMS, adapter->eims_other);
2011 * igc_write_ivar - configure ivar for given MSI-X vector
2012 * @hw: pointer to the HW structure
2013 * @msix_vector: vector number we are allocating to a given ring
2014 * @index: row index of IVAR register to write within IVAR table
2015 * @offset: column offset of in IVAR, should be multiple of 8
2017 * The IVAR table consists of 2 columns,
2018 * each containing an cause allocation for an Rx and Tx ring, and a
2019 * variable number of rows depending on the number of queues supported.
2021 static void igc_write_ivar(struct igc_hw *hw, int msix_vector,
2022 int index, int offset)
2024 u32 ivar = array_rd32(IGC_IVAR0, index);
2026 /* clear any bits that are currently set */
2027 ivar &= ~((u32)0xFF << offset);
2029 /* write vector and valid bit */
2030 ivar |= (msix_vector | IGC_IVAR_VALID) << offset;
2032 array_wr32(IGC_IVAR0, index, ivar);
2035 static void igc_assign_vector(struct igc_q_vector *q_vector, int msix_vector)
2037 struct igc_adapter *adapter = q_vector->adapter;
2038 struct igc_hw *hw = &adapter->hw;
2039 int rx_queue = IGC_N0_QUEUE;
2040 int tx_queue = IGC_N0_QUEUE;
2042 if (q_vector->rx.ring)
2043 rx_queue = q_vector->rx.ring->reg_idx;
2044 if (q_vector->tx.ring)
2045 tx_queue = q_vector->tx.ring->reg_idx;
2047 switch (hw->mac.type) {
2049 if (rx_queue > IGC_N0_QUEUE)
2050 igc_write_ivar(hw, msix_vector,
2052 (rx_queue & 0x1) << 4);
2053 if (tx_queue > IGC_N0_QUEUE)
2054 igc_write_ivar(hw, msix_vector,
2056 ((tx_queue & 0x1) << 4) + 8);
2057 q_vector->eims_value = BIT(msix_vector);
2060 WARN_ONCE(hw->mac.type != igc_i225, "Wrong MAC type\n");
2064 /* add q_vector eims value to global eims_enable_mask */
2065 adapter->eims_enable_mask |= q_vector->eims_value;
2067 /* configure q_vector to set itr on first interrupt */
2068 q_vector->set_itr = 1;
2072 * igc_configure_msix - Configure MSI-X hardware
2073 * @adapter: Pointer to adapter structure
2075 * igc_configure_msix sets up the hardware to properly
2076 * generate MSI-X interrupts.
2078 static void igc_configure_msix(struct igc_adapter *adapter)
2080 struct igc_hw *hw = &adapter->hw;
2084 adapter->eims_enable_mask = 0;
2086 /* set vector for other causes, i.e. link changes */
2087 switch (hw->mac.type) {
2089 /* Turn on MSI-X capability first, or our settings
2090 * won't stick. And it will take days to debug.
2092 wr32(IGC_GPIE, IGC_GPIE_MSIX_MODE |
2093 IGC_GPIE_PBA | IGC_GPIE_EIAME |
2096 /* enable msix_other interrupt */
2097 adapter->eims_other = BIT(vector);
2098 tmp = (vector++ | IGC_IVAR_VALID) << 8;
2100 wr32(IGC_IVAR_MISC, tmp);
2103 /* do nothing, since nothing else supports MSI-X */
2105 } /* switch (hw->mac.type) */
2107 adapter->eims_enable_mask |= adapter->eims_other;
2109 for (i = 0; i < adapter->num_q_vectors; i++)
2110 igc_assign_vector(adapter->q_vector[i], vector++);
2115 static irqreturn_t igc_msix_ring(int irq, void *data)
2117 struct igc_q_vector *q_vector = data;
2119 /* Write the ITR value calculated from the previous interrupt. */
2120 igc_write_itr(q_vector);
2122 napi_schedule(&q_vector->napi);
2128 * igc_request_msix - Initialize MSI-X interrupts
2129 * @adapter: Pointer to adapter structure
2131 * igc_request_msix allocates MSI-X vectors and requests interrupts from the
2134 static int igc_request_msix(struct igc_adapter *adapter)
2136 int i = 0, err = 0, vector = 0, free_vector = 0;
2137 struct net_device *netdev = adapter->netdev;
2139 err = request_irq(adapter->msix_entries[vector].vector,
2140 &igc_msix_other, 0, netdev->name, adapter);
2144 for (i = 0; i < adapter->num_q_vectors; i++) {
2145 struct igc_q_vector *q_vector = adapter->q_vector[i];
2149 q_vector->itr_register = adapter->io_addr + IGC_EITR(vector);
2151 if (q_vector->rx.ring && q_vector->tx.ring)
2152 sprintf(q_vector->name, "%s-TxRx-%u", netdev->name,
2153 q_vector->rx.ring->queue_index);
2154 else if (q_vector->tx.ring)
2155 sprintf(q_vector->name, "%s-tx-%u", netdev->name,
2156 q_vector->tx.ring->queue_index);
2157 else if (q_vector->rx.ring)
2158 sprintf(q_vector->name, "%s-rx-%u", netdev->name,
2159 q_vector->rx.ring->queue_index);
2161 sprintf(q_vector->name, "%s-unused", netdev->name);
2163 err = request_irq(adapter->msix_entries[vector].vector,
2164 igc_msix_ring, 0, q_vector->name,
2170 igc_configure_msix(adapter);
2174 /* free already assigned IRQs */
2175 free_irq(adapter->msix_entries[free_vector++].vector, adapter);
2178 for (i = 0; i < vector; i++) {
2179 free_irq(adapter->msix_entries[free_vector++].vector,
2180 adapter->q_vector[i]);
2187 * igc_reset_q_vector - Reset config for interrupt vector
2188 * @adapter: board private structure to initialize
2189 * @v_idx: Index of vector to be reset
2191 * If NAPI is enabled it will delete any references to the
2192 * NAPI struct. This is preparation for igc_free_q_vector.
2194 static void igc_reset_q_vector(struct igc_adapter *adapter, int v_idx)
2196 struct igc_q_vector *q_vector = adapter->q_vector[v_idx];
2198 /* if we're coming from igc_set_interrupt_capability, the vectors are
2204 if (q_vector->tx.ring)
2205 adapter->tx_ring[q_vector->tx.ring->queue_index] = NULL;
2207 if (q_vector->rx.ring)
2208 adapter->rx_ring[q_vector->rx.ring->queue_index] = NULL;
2210 netif_napi_del(&q_vector->napi);
2213 static void igc_reset_interrupt_capability(struct igc_adapter *adapter)
2215 int v_idx = adapter->num_q_vectors;
2217 if (adapter->msix_entries) {
2218 pci_disable_msix(adapter->pdev);
2219 kfree(adapter->msix_entries);
2220 adapter->msix_entries = NULL;
2221 } else if (adapter->flags & IGC_FLAG_HAS_MSI) {
2222 pci_disable_msi(adapter->pdev);
2226 igc_reset_q_vector(adapter, v_idx);
2230 * igc_clear_interrupt_scheme - reset the device to a state of no interrupts
2231 * @adapter: Pointer to adapter structure
2233 * This function resets the device so that it has 0 rx queues, tx queues, and
2234 * MSI-X interrupts allocated.
2236 static void igc_clear_interrupt_scheme(struct igc_adapter *adapter)
2238 igc_free_q_vectors(adapter);
2239 igc_reset_interrupt_capability(adapter);
2243 * igc_free_q_vectors - Free memory allocated for interrupt vectors
2244 * @adapter: board private structure to initialize
2246 * This function frees the memory allocated to the q_vectors. In addition if
2247 * NAPI is enabled it will delete any references to the NAPI struct prior
2248 * to freeing the q_vector.
2250 static void igc_free_q_vectors(struct igc_adapter *adapter)
2252 int v_idx = adapter->num_q_vectors;
2254 adapter->num_tx_queues = 0;
2255 adapter->num_rx_queues = 0;
2256 adapter->num_q_vectors = 0;
2259 igc_reset_q_vector(adapter, v_idx);
2260 igc_free_q_vector(adapter, v_idx);
2265 * igc_free_q_vector - Free memory allocated for specific interrupt vector
2266 * @adapter: board private structure to initialize
2267 * @v_idx: Index of vector to be freed
2269 * This function frees the memory allocated to the q_vector.
2271 static void igc_free_q_vector(struct igc_adapter *adapter, int v_idx)
2273 struct igc_q_vector *q_vector = adapter->q_vector[v_idx];
2275 adapter->q_vector[v_idx] = NULL;
2277 /* igc_get_stats64() might access the rings on this vector,
2278 * we must wait a grace period before freeing it.
2281 kfree_rcu(q_vector, rcu);
2284 /* Need to wait a few seconds after link up to get diagnostic information from
2287 static void igc_update_phy_info(struct timer_list *t)
2289 struct igc_adapter *adapter = from_timer(adapter, t, phy_info_timer);
2291 igc_get_phy_info(&adapter->hw);
2295 * igc_has_link - check shared code for link and determine up/down
2296 * @adapter: pointer to driver private info
2298 bool igc_has_link(struct igc_adapter *adapter)
2300 struct igc_hw *hw = &adapter->hw;
2301 bool link_active = false;
2303 /* get_link_status is set on LSC (link status) interrupt or
2304 * rx sequence error interrupt. get_link_status will stay
2305 * false until the igc_check_for_link establishes link
2306 * for copper adapters ONLY
2308 switch (hw->phy.media_type) {
2309 case igc_media_type_copper:
2310 if (!hw->mac.get_link_status)
2312 hw->mac.ops.check_for_link(hw);
2313 link_active = !hw->mac.get_link_status;
2316 case igc_media_type_unknown:
2320 if (hw->mac.type == igc_i225 &&
2321 hw->phy.id == I225_I_PHY_ID) {
2322 if (!netif_carrier_ok(adapter->netdev)) {
2323 adapter->flags &= ~IGC_FLAG_NEED_LINK_UPDATE;
2324 } else if (!(adapter->flags & IGC_FLAG_NEED_LINK_UPDATE)) {
2325 adapter->flags |= IGC_FLAG_NEED_LINK_UPDATE;
2326 adapter->link_check_timeout = jiffies;
2334 * igc_watchdog - Timer Call-back
2335 * @data: pointer to adapter cast into an unsigned long
2337 static void igc_watchdog(struct timer_list *t)
2339 struct igc_adapter *adapter = from_timer(adapter, t, watchdog_timer);
2340 /* Do the rest outside of interrupt context */
2341 schedule_work(&adapter->watchdog_task);
2344 static void igc_watchdog_task(struct work_struct *work)
2346 struct igc_adapter *adapter = container_of(work,
2349 struct net_device *netdev = adapter->netdev;
2350 struct igc_hw *hw = &adapter->hw;
2351 struct igc_phy_info *phy = &hw->phy;
2352 u16 phy_data, retry_count = 20;
2357 link = igc_has_link(adapter);
2359 if (adapter->flags & IGC_FLAG_NEED_LINK_UPDATE) {
2360 if (time_after(jiffies, (adapter->link_check_timeout + HZ)))
2361 adapter->flags &= ~IGC_FLAG_NEED_LINK_UPDATE;
2366 /* Force link down if we have fiber to swap to */
2367 if (adapter->flags & IGC_FLAG_MAS_ENABLE) {
2368 if (hw->phy.media_type == igc_media_type_copper) {
2369 connsw = rd32(IGC_CONNSW);
2370 if (!(connsw & IGC_CONNSW_AUTOSENSE_EN))
2375 if (!netif_carrier_ok(netdev)) {
2378 hw->mac.ops.get_speed_and_duplex(hw,
2379 &adapter->link_speed,
2380 &adapter->link_duplex);
2382 ctrl = rd32(IGC_CTRL);
2383 /* Link status message must follow this format */
2385 "igc: %s NIC Link is Up %d Mbps %s Duplex, Flow Control: %s\n",
2387 adapter->link_speed,
2388 adapter->link_duplex == FULL_DUPLEX ?
2390 (ctrl & IGC_CTRL_TFCE) &&
2391 (ctrl & IGC_CTRL_RFCE) ? "RX/TX" :
2392 (ctrl & IGC_CTRL_RFCE) ? "RX" :
2393 (ctrl & IGC_CTRL_TFCE) ? "TX" : "None");
2395 /* check if SmartSpeed worked */
2396 igc_check_downshift(hw);
2397 if (phy->speed_downgraded)
2398 netdev_warn(netdev, "Link Speed was downgraded by SmartSpeed\n");
2400 /* adjust timeout factor according to speed/duplex */
2401 adapter->tx_timeout_factor = 1;
2402 switch (adapter->link_speed) {
2404 adapter->tx_timeout_factor = 14;
2407 /* maybe add some timeout factor ? */
2411 if (adapter->link_speed != SPEED_1000)
2414 /* wait for Remote receiver status OK */
2416 if (!igc_read_phy_reg(hw, PHY_1000T_STATUS,
2418 if (!(phy_data & SR_1000T_REMOTE_RX_STATUS) &&
2422 goto retry_read_status;
2423 } else if (!retry_count) {
2424 dev_err(&adapter->pdev->dev, "exceed max 2 second\n");
2427 dev_err(&adapter->pdev->dev, "read 1000Base-T Status Reg\n");
2430 netif_carrier_on(netdev);
2432 /* link state has changed, schedule phy info update */
2433 if (!test_bit(__IGC_DOWN, &adapter->state))
2434 mod_timer(&adapter->phy_info_timer,
2435 round_jiffies(jiffies + 2 * HZ));
2438 if (netif_carrier_ok(netdev)) {
2439 adapter->link_speed = 0;
2440 adapter->link_duplex = 0;
2442 /* Links status message must follow this format */
2443 netdev_info(netdev, "igc: %s NIC Link is Down\n",
2445 netif_carrier_off(netdev);
2447 /* link state has changed, schedule phy info update */
2448 if (!test_bit(__IGC_DOWN, &adapter->state))
2449 mod_timer(&adapter->phy_info_timer,
2450 round_jiffies(jiffies + 2 * HZ));
2452 /* link is down, time to check for alternate media */
2453 if (adapter->flags & IGC_FLAG_MAS_ENABLE) {
2454 if (adapter->flags & IGC_FLAG_MEDIA_RESET) {
2455 schedule_work(&adapter->reset_task);
2456 /* return immediately */
2461 /* also check for alternate media here */
2462 } else if (!netif_carrier_ok(netdev) &&
2463 (adapter->flags & IGC_FLAG_MAS_ENABLE)) {
2464 if (adapter->flags & IGC_FLAG_MEDIA_RESET) {
2465 schedule_work(&adapter->reset_task);
2466 /* return immediately */
2472 spin_lock(&adapter->stats64_lock);
2473 igc_update_stats(adapter);
2474 spin_unlock(&adapter->stats64_lock);
2476 for (i = 0; i < adapter->num_tx_queues; i++) {
2477 struct igc_ring *tx_ring = adapter->tx_ring[i];
2479 if (!netif_carrier_ok(netdev)) {
2480 /* We've lost link, so the controller stops DMA,
2481 * but we've got queued Tx work that's never going
2482 * to get done, so reset controller to flush Tx.
2483 * (Do the reset outside of interrupt context).
2485 if (igc_desc_unused(tx_ring) + 1 < tx_ring->count) {
2486 adapter->tx_timeout_count++;
2487 schedule_work(&adapter->reset_task);
2488 /* return immediately since reset is imminent */
2493 /* Force detection of hung controller every watchdog period */
2494 set_bit(IGC_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags);
2497 /* Cause software interrupt to ensure Rx ring is cleaned */
2498 if (adapter->flags & IGC_FLAG_HAS_MSIX) {
2501 for (i = 0; i < adapter->num_q_vectors; i++)
2502 eics |= adapter->q_vector[i]->eims_value;
2503 wr32(IGC_EICS, eics);
2505 wr32(IGC_ICS, IGC_ICS_RXDMT0);
2508 /* Reset the timer */
2509 if (!test_bit(__IGC_DOWN, &adapter->state)) {
2510 if (adapter->flags & IGC_FLAG_NEED_LINK_UPDATE)
2511 mod_timer(&adapter->watchdog_timer,
2512 round_jiffies(jiffies + HZ));
2514 mod_timer(&adapter->watchdog_timer,
2515 round_jiffies(jiffies + 2 * HZ));
2520 * igc_update_ring_itr - update the dynamic ITR value based on packet size
2521 * @q_vector: pointer to q_vector
2523 * Stores a new ITR value based on strictly on packet size. This
2524 * algorithm is less sophisticated than that used in igc_update_itr,
2525 * due to the difficulty of synchronizing statistics across multiple
2526 * receive rings. The divisors and thresholds used by this function
2527 * were determined based on theoretical maximum wire speed and testing
2528 * data, in order to minimize response time while increasing bulk
2530 * NOTE: This function is called only when operating in a multiqueue
2531 * receive environment.
2533 static void igc_update_ring_itr(struct igc_q_vector *q_vector)
2535 struct igc_adapter *adapter = q_vector->adapter;
2536 int new_val = q_vector->itr_val;
2537 int avg_wire_size = 0;
2538 unsigned int packets;
2540 /* For non-gigabit speeds, just fix the interrupt rate at 4000
2541 * ints/sec - ITR timer value of 120 ticks.
2543 switch (adapter->link_speed) {
2546 new_val = IGC_4K_ITR;
2552 packets = q_vector->rx.total_packets;
2554 avg_wire_size = q_vector->rx.total_bytes / packets;
2556 packets = q_vector->tx.total_packets;
2558 avg_wire_size = max_t(u32, avg_wire_size,
2559 q_vector->tx.total_bytes / packets);
2561 /* if avg_wire_size isn't set no work was done */
2565 /* Add 24 bytes to size to account for CRC, preamble, and gap */
2566 avg_wire_size += 24;
2568 /* Don't starve jumbo frames */
2569 avg_wire_size = min(avg_wire_size, 3000);
2571 /* Give a little boost to mid-size frames */
2572 if (avg_wire_size > 300 && avg_wire_size < 1200)
2573 new_val = avg_wire_size / 3;
2575 new_val = avg_wire_size / 2;
2577 /* conservative mode (itr 3) eliminates the lowest_latency setting */
2578 if (new_val < IGC_20K_ITR &&
2579 ((q_vector->rx.ring && adapter->rx_itr_setting == 3) ||
2580 (!q_vector->rx.ring && adapter->tx_itr_setting == 3)))
2581 new_val = IGC_20K_ITR;
2584 if (new_val != q_vector->itr_val) {
2585 q_vector->itr_val = new_val;
2586 q_vector->set_itr = 1;
2589 q_vector->rx.total_bytes = 0;
2590 q_vector->rx.total_packets = 0;
2591 q_vector->tx.total_bytes = 0;
2592 q_vector->tx.total_packets = 0;
2596 * igc_update_itr - update the dynamic ITR value based on statistics
2597 * @q_vector: pointer to q_vector
2598 * @ring_container: ring info to update the itr for
2600 * Stores a new ITR value based on packets and byte
2601 * counts during the last interrupt. The advantage of per interrupt
2602 * computation is faster updates and more accurate ITR for the current
2603 * traffic pattern. Constants in this function were computed
2604 * based on theoretical maximum wire speed and thresholds were set based
2605 * on testing data as well as attempting to minimize response time
2606 * while increasing bulk throughput.
2607 * NOTE: These calculations are only valid when operating in a single-
2608 * queue environment.
2610 static void igc_update_itr(struct igc_q_vector *q_vector,
2611 struct igc_ring_container *ring_container)
2613 unsigned int packets = ring_container->total_packets;
2614 unsigned int bytes = ring_container->total_bytes;
2615 u8 itrval = ring_container->itr;
2617 /* no packets, exit with status unchanged */
2622 case lowest_latency:
2623 /* handle TSO and jumbo frames */
2624 if (bytes / packets > 8000)
2625 itrval = bulk_latency;
2626 else if ((packets < 5) && (bytes > 512))
2627 itrval = low_latency;
2629 case low_latency: /* 50 usec aka 20000 ints/s */
2630 if (bytes > 10000) {
2631 /* this if handles the TSO accounting */
2632 if (bytes / packets > 8000)
2633 itrval = bulk_latency;
2634 else if ((packets < 10) || ((bytes / packets) > 1200))
2635 itrval = bulk_latency;
2636 else if ((packets > 35))
2637 itrval = lowest_latency;
2638 } else if (bytes / packets > 2000) {
2639 itrval = bulk_latency;
2640 } else if (packets <= 2 && bytes < 512) {
2641 itrval = lowest_latency;
2644 case bulk_latency: /* 250 usec aka 4000 ints/s */
2645 if (bytes > 25000) {
2647 itrval = low_latency;
2648 } else if (bytes < 1500) {
2649 itrval = low_latency;
2654 /* clear work counters since we have the values we need */
2655 ring_container->total_bytes = 0;
2656 ring_container->total_packets = 0;
2658 /* write updated itr to ring container */
2659 ring_container->itr = itrval;
2663 * igc_intr_msi - Interrupt Handler
2664 * @irq: interrupt number
2665 * @data: pointer to a network interface device structure
2667 static irqreturn_t igc_intr_msi(int irq, void *data)
2669 struct igc_adapter *adapter = data;
2670 struct igc_q_vector *q_vector = adapter->q_vector[0];
2671 struct igc_hw *hw = &adapter->hw;
2672 /* read ICR disables interrupts using IAM */
2673 u32 icr = rd32(IGC_ICR);
2675 igc_write_itr(q_vector);
2677 if (icr & IGC_ICR_DRSTA)
2678 schedule_work(&adapter->reset_task);
2680 if (icr & IGC_ICR_DOUTSYNC) {
2681 /* HW is reporting DMA is out of sync */
2682 adapter->stats.doosync++;
2685 if (icr & (IGC_ICR_RXSEQ | IGC_ICR_LSC)) {
2686 hw->mac.get_link_status = 1;
2687 if (!test_bit(__IGC_DOWN, &adapter->state))
2688 mod_timer(&adapter->watchdog_timer, jiffies + 1);
2691 napi_schedule(&q_vector->napi);
2697 * igc_intr - Legacy Interrupt Handler
2698 * @irq: interrupt number
2699 * @data: pointer to a network interface device structure
2701 static irqreturn_t igc_intr(int irq, void *data)
2703 struct igc_adapter *adapter = data;
2704 struct igc_q_vector *q_vector = adapter->q_vector[0];
2705 struct igc_hw *hw = &adapter->hw;
2706 /* Interrupt Auto-Mask...upon reading ICR, interrupts are masked. No
2707 * need for the IMC write
2709 u32 icr = rd32(IGC_ICR);
2711 /* IMS will not auto-mask if INT_ASSERTED is not set, and if it is
2712 * not set, then the adapter didn't send an interrupt
2714 if (!(icr & IGC_ICR_INT_ASSERTED))
2717 igc_write_itr(q_vector);
2719 if (icr & IGC_ICR_DRSTA)
2720 schedule_work(&adapter->reset_task);
2722 if (icr & IGC_ICR_DOUTSYNC) {
2723 /* HW is reporting DMA is out of sync */
2724 adapter->stats.doosync++;
2727 if (icr & (IGC_ICR_RXSEQ | IGC_ICR_LSC)) {
2728 hw->mac.get_link_status = 1;
2729 /* guard against interrupt when we're going down */
2730 if (!test_bit(__IGC_DOWN, &adapter->state))
2731 mod_timer(&adapter->watchdog_timer, jiffies + 1);
2734 napi_schedule(&q_vector->napi);
2739 static void igc_set_itr(struct igc_q_vector *q_vector)
2741 struct igc_adapter *adapter = q_vector->adapter;
2742 u32 new_itr = q_vector->itr_val;
2745 /* for non-gigabit speeds, just fix the interrupt rate at 4000 */
2746 switch (adapter->link_speed) {
2750 new_itr = IGC_4K_ITR;
2756 igc_update_itr(q_vector, &q_vector->tx);
2757 igc_update_itr(q_vector, &q_vector->rx);
2759 current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
2761 /* conservative mode (itr 3) eliminates the lowest_latency setting */
2762 if (current_itr == lowest_latency &&
2763 ((q_vector->rx.ring && adapter->rx_itr_setting == 3) ||
2764 (!q_vector->rx.ring && adapter->tx_itr_setting == 3)))
2765 current_itr = low_latency;
2767 switch (current_itr) {
2768 /* counts and packets in update_itr are dependent on these numbers */
2769 case lowest_latency:
2770 new_itr = IGC_70K_ITR; /* 70,000 ints/sec */
2773 new_itr = IGC_20K_ITR; /* 20,000 ints/sec */
2776 new_itr = IGC_4K_ITR; /* 4,000 ints/sec */
2783 if (new_itr != q_vector->itr_val) {
2784 /* this attempts to bias the interrupt rate towards Bulk
2785 * by adding intermediate steps when interrupt rate is
2788 new_itr = new_itr > q_vector->itr_val ?
2789 max((new_itr * q_vector->itr_val) /
2790 (new_itr + (q_vector->itr_val >> 2)),
2792 /* Don't write the value here; it resets the adapter's
2793 * internal timer, and causes us to delay far longer than
2794 * we should between interrupts. Instead, we write the ITR
2795 * value at the beginning of the next interrupt so the timing
2796 * ends up being correct.
2798 q_vector->itr_val = new_itr;
2799 q_vector->set_itr = 1;
2803 static void igc_ring_irq_enable(struct igc_q_vector *q_vector)
2805 struct igc_adapter *adapter = q_vector->adapter;
2806 struct igc_hw *hw = &adapter->hw;
2808 if ((q_vector->rx.ring && (adapter->rx_itr_setting & 3)) ||
2809 (!q_vector->rx.ring && (adapter->tx_itr_setting & 3))) {
2810 if (adapter->num_q_vectors == 1)
2811 igc_set_itr(q_vector);
2813 igc_update_ring_itr(q_vector);
2816 if (!test_bit(__IGC_DOWN, &adapter->state)) {
2817 if (adapter->msix_entries)
2818 wr32(IGC_EIMS, q_vector->eims_value);
2820 igc_irq_enable(adapter);
2825 * igc_poll - NAPI Rx polling callback
2826 * @napi: napi polling structure
2827 * @budget: count of how many packets we should handle
2829 static int igc_poll(struct napi_struct *napi, int budget)
2831 struct igc_q_vector *q_vector = container_of(napi,
2832 struct igc_q_vector,
2834 bool clean_complete = true;
2837 if (q_vector->tx.ring)
2838 clean_complete = igc_clean_tx_irq(q_vector, budget);
2840 if (q_vector->rx.ring) {
2841 int cleaned = igc_clean_rx_irq(q_vector, budget);
2843 work_done += cleaned;
2844 if (cleaned >= budget)
2845 clean_complete = false;
2848 /* If all work not completed, return budget and keep polling */
2849 if (!clean_complete)
2852 /* Exit the polling mode, but don't re-enable interrupts if stack might
2853 * poll us due to busy-polling
2855 if (likely(napi_complete_done(napi, work_done)))
2856 igc_ring_irq_enable(q_vector);
2858 return min(work_done, budget - 1);
2862 * igc_set_interrupt_capability - set MSI or MSI-X if supported
2863 * @adapter: Pointer to adapter structure
2865 * Attempt to configure interrupts using the best available
2866 * capabilities of the hardware and kernel.
2868 static void igc_set_interrupt_capability(struct igc_adapter *adapter,
2876 adapter->flags |= IGC_FLAG_HAS_MSIX;
2878 /* Number of supported queues. */
2879 adapter->num_rx_queues = adapter->rss_queues;
2881 adapter->num_tx_queues = adapter->rss_queues;
2883 /* start with one vector for every Rx queue */
2884 numvecs = adapter->num_rx_queues;
2886 /* if Tx handler is separate add 1 for every Tx queue */
2887 if (!(adapter->flags & IGC_FLAG_QUEUE_PAIRS))
2888 numvecs += adapter->num_tx_queues;
2890 /* store the number of vectors reserved for queues */
2891 adapter->num_q_vectors = numvecs;
2893 /* add 1 vector for link status interrupts */
2896 adapter->msix_entries = kcalloc(numvecs, sizeof(struct msix_entry),
2899 if (!adapter->msix_entries)
2902 /* populate entry values */
2903 for (i = 0; i < numvecs; i++)
2904 adapter->msix_entries[i].entry = i;
2906 err = pci_enable_msix_range(adapter->pdev,
2907 adapter->msix_entries,
2913 kfree(adapter->msix_entries);
2914 adapter->msix_entries = NULL;
2916 igc_reset_interrupt_capability(adapter);
2919 adapter->flags &= ~IGC_FLAG_HAS_MSIX;
2921 adapter->rss_queues = 1;
2922 adapter->flags |= IGC_FLAG_QUEUE_PAIRS;
2923 adapter->num_rx_queues = 1;
2924 adapter->num_tx_queues = 1;
2925 adapter->num_q_vectors = 1;
2926 if (!pci_enable_msi(adapter->pdev))
2927 adapter->flags |= IGC_FLAG_HAS_MSI;
2930 static void igc_add_ring(struct igc_ring *ring,
2931 struct igc_ring_container *head)
2938 * igc_alloc_q_vector - Allocate memory for a single interrupt vector
2939 * @adapter: board private structure to initialize
2940 * @v_count: q_vectors allocated on adapter, used for ring interleaving
2941 * @v_idx: index of vector in adapter struct
2942 * @txr_count: total number of Tx rings to allocate
2943 * @txr_idx: index of first Tx ring to allocate
2944 * @rxr_count: total number of Rx rings to allocate
2945 * @rxr_idx: index of first Rx ring to allocate
2947 * We allocate one q_vector. If allocation fails we return -ENOMEM.
2949 static int igc_alloc_q_vector(struct igc_adapter *adapter,
2950 unsigned int v_count, unsigned int v_idx,
2951 unsigned int txr_count, unsigned int txr_idx,
2952 unsigned int rxr_count, unsigned int rxr_idx)
2954 struct igc_q_vector *q_vector;
2955 struct igc_ring *ring;
2958 /* igc only supports 1 Tx and/or 1 Rx queue per vector */
2959 if (txr_count > 1 || rxr_count > 1)
2962 ring_count = txr_count + rxr_count;
2964 /* allocate q_vector and rings */
2965 q_vector = adapter->q_vector[v_idx];
2967 q_vector = kzalloc(struct_size(q_vector, ring, ring_count),
2970 memset(q_vector, 0, struct_size(q_vector, ring, ring_count));
2974 /* initialize NAPI */
2975 netif_napi_add(adapter->netdev, &q_vector->napi,
2978 /* tie q_vector and adapter together */
2979 adapter->q_vector[v_idx] = q_vector;
2980 q_vector->adapter = adapter;
2982 /* initialize work limits */
2983 q_vector->tx.work_limit = adapter->tx_work_limit;
2985 /* initialize ITR configuration */
2986 q_vector->itr_register = adapter->io_addr + IGC_EITR(0);
2987 q_vector->itr_val = IGC_START_ITR;
2989 /* initialize pointer to rings */
2990 ring = q_vector->ring;
2992 /* initialize ITR */
2994 /* rx or rx/tx vector */
2995 if (!adapter->rx_itr_setting || adapter->rx_itr_setting > 3)
2996 q_vector->itr_val = adapter->rx_itr_setting;
2998 /* tx only vector */
2999 if (!adapter->tx_itr_setting || adapter->tx_itr_setting > 3)
3000 q_vector->itr_val = adapter->tx_itr_setting;
3004 /* assign generic ring traits */
3005 ring->dev = &adapter->pdev->dev;
3006 ring->netdev = adapter->netdev;
3008 /* configure backlink on ring */
3009 ring->q_vector = q_vector;
3011 /* update q_vector Tx values */
3012 igc_add_ring(ring, &q_vector->tx);
3014 /* apply Tx specific ring traits */
3015 ring->count = adapter->tx_ring_count;
3016 ring->queue_index = txr_idx;
3018 /* assign ring to adapter */
3019 adapter->tx_ring[txr_idx] = ring;
3021 /* push pointer to next ring */
3026 /* assign generic ring traits */
3027 ring->dev = &adapter->pdev->dev;
3028 ring->netdev = adapter->netdev;
3030 /* configure backlink on ring */
3031 ring->q_vector = q_vector;
3033 /* update q_vector Rx values */
3034 igc_add_ring(ring, &q_vector->rx);
3036 /* apply Rx specific ring traits */
3037 ring->count = adapter->rx_ring_count;
3038 ring->queue_index = rxr_idx;
3040 /* assign ring to adapter */
3041 adapter->rx_ring[rxr_idx] = ring;
3048 * igc_alloc_q_vectors - Allocate memory for interrupt vectors
3049 * @adapter: board private structure to initialize
3051 * We allocate one q_vector per queue interrupt. If allocation fails we
3054 static int igc_alloc_q_vectors(struct igc_adapter *adapter)
3056 int rxr_remaining = adapter->num_rx_queues;
3057 int txr_remaining = adapter->num_tx_queues;
3058 int rxr_idx = 0, txr_idx = 0, v_idx = 0;
3059 int q_vectors = adapter->num_q_vectors;
3062 if (q_vectors >= (rxr_remaining + txr_remaining)) {
3063 for (; rxr_remaining; v_idx++) {
3064 err = igc_alloc_q_vector(adapter, q_vectors, v_idx,
3070 /* update counts and index */
3076 for (; v_idx < q_vectors; v_idx++) {
3077 int rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - v_idx);
3078 int tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - v_idx);
3080 err = igc_alloc_q_vector(adapter, q_vectors, v_idx,
3081 tqpv, txr_idx, rqpv, rxr_idx);
3086 /* update counts and index */
3087 rxr_remaining -= rqpv;
3088 txr_remaining -= tqpv;
3096 adapter->num_tx_queues = 0;
3097 adapter->num_rx_queues = 0;
3098 adapter->num_q_vectors = 0;
3101 igc_free_q_vector(adapter, v_idx);
3107 * igc_cache_ring_register - Descriptor ring to register mapping
3108 * @adapter: board private structure to initialize
3110 * Once we know the feature-set enabled for the device, we'll cache
3111 * the register offset the descriptor ring is assigned to.
3113 static void igc_cache_ring_register(struct igc_adapter *adapter)
3117 switch (adapter->hw.mac.type) {
3121 for (; i < adapter->num_rx_queues; i++)
3122 adapter->rx_ring[i]->reg_idx = i;
3123 for (; j < adapter->num_tx_queues; j++)
3124 adapter->tx_ring[j]->reg_idx = j;
3130 * igc_init_interrupt_scheme - initialize interrupts, allocate queues/vectors
3131 * @adapter: Pointer to adapter structure
3133 * This function initializes the interrupts and allocates all of the queues.
3135 static int igc_init_interrupt_scheme(struct igc_adapter *adapter, bool msix)
3137 struct pci_dev *pdev = adapter->pdev;
3140 igc_set_interrupt_capability(adapter, msix);
3142 err = igc_alloc_q_vectors(adapter);
3144 dev_err(&pdev->dev, "Unable to allocate memory for vectors\n");
3145 goto err_alloc_q_vectors;
3148 igc_cache_ring_register(adapter);
3152 err_alloc_q_vectors:
3153 igc_reset_interrupt_capability(adapter);
3157 static void igc_free_irq(struct igc_adapter *adapter)
3159 if (adapter->msix_entries) {
3162 free_irq(adapter->msix_entries[vector++].vector, adapter);
3164 for (i = 0; i < adapter->num_q_vectors; i++)
3165 free_irq(adapter->msix_entries[vector++].vector,
3166 adapter->q_vector[i]);
3168 free_irq(adapter->pdev->irq, adapter);
3173 * igc_irq_disable - Mask off interrupt generation on the NIC
3174 * @adapter: board private structure
3176 static void igc_irq_disable(struct igc_adapter *adapter)
3178 struct igc_hw *hw = &adapter->hw;
3180 if (adapter->msix_entries) {
3181 u32 regval = rd32(IGC_EIAM);
3183 wr32(IGC_EIAM, regval & ~adapter->eims_enable_mask);
3184 wr32(IGC_EIMC, adapter->eims_enable_mask);
3185 regval = rd32(IGC_EIAC);
3186 wr32(IGC_EIAC, regval & ~adapter->eims_enable_mask);
3193 if (adapter->msix_entries) {
3196 synchronize_irq(adapter->msix_entries[vector++].vector);
3198 for (i = 0; i < adapter->num_q_vectors; i++)
3199 synchronize_irq(adapter->msix_entries[vector++].vector);
3201 synchronize_irq(adapter->pdev->irq);
3206 * igc_irq_enable - Enable default interrupt generation settings
3207 * @adapter: board private structure
3209 static void igc_irq_enable(struct igc_adapter *adapter)
3211 struct igc_hw *hw = &adapter->hw;
3213 if (adapter->msix_entries) {
3214 u32 ims = IGC_IMS_LSC | IGC_IMS_DOUTSYNC | IGC_IMS_DRSTA;
3215 u32 regval = rd32(IGC_EIAC);
3217 wr32(IGC_EIAC, regval | adapter->eims_enable_mask);
3218 regval = rd32(IGC_EIAM);
3219 wr32(IGC_EIAM, regval | adapter->eims_enable_mask);
3220 wr32(IGC_EIMS, adapter->eims_enable_mask);
3223 wr32(IGC_IMS, IMS_ENABLE_MASK | IGC_IMS_DRSTA);
3224 wr32(IGC_IAM, IMS_ENABLE_MASK | IGC_IMS_DRSTA);
3229 * igc_request_irq - initialize interrupts
3230 * @adapter: Pointer to adapter structure
3232 * Attempts to configure interrupts using the best available
3233 * capabilities of the hardware and kernel.
3235 static int igc_request_irq(struct igc_adapter *adapter)
3237 struct net_device *netdev = adapter->netdev;
3238 struct pci_dev *pdev = adapter->pdev;
3241 if (adapter->flags & IGC_FLAG_HAS_MSIX) {
3242 err = igc_request_msix(adapter);
3245 /* fall back to MSI */
3246 igc_free_all_tx_resources(adapter);
3247 igc_free_all_rx_resources(adapter);
3249 igc_clear_interrupt_scheme(adapter);
3250 err = igc_init_interrupt_scheme(adapter, false);
3253 igc_setup_all_tx_resources(adapter);
3254 igc_setup_all_rx_resources(adapter);
3255 igc_configure(adapter);
3258 igc_assign_vector(adapter->q_vector[0], 0);
3260 if (adapter->flags & IGC_FLAG_HAS_MSI) {
3261 err = request_irq(pdev->irq, &igc_intr_msi, 0,
3262 netdev->name, adapter);
3266 /* fall back to legacy interrupts */
3267 igc_reset_interrupt_capability(adapter);
3268 adapter->flags &= ~IGC_FLAG_HAS_MSI;
3271 err = request_irq(pdev->irq, &igc_intr, IRQF_SHARED,
3272 netdev->name, adapter);
3275 dev_err(&pdev->dev, "Error %d getting interrupt\n",
3282 static void igc_write_itr(struct igc_q_vector *q_vector)
3284 u32 itr_val = q_vector->itr_val & IGC_QVECTOR_MASK;
3286 if (!q_vector->set_itr)
3290 itr_val = IGC_ITR_VAL_MASK;
3292 itr_val |= IGC_EITR_CNT_IGNR;
3294 writel(itr_val, q_vector->itr_register);
3295 q_vector->set_itr = 0;
3299 * igc_open - Called when a network interface is made active
3300 * @netdev: network interface device structure
3302 * Returns 0 on success, negative value on failure
3304 * The open entry point is called when a network interface is made
3305 * active by the system (IFF_UP). At this point all resources needed
3306 * for transmit and receive operations are allocated, the interrupt
3307 * handler is registered with the OS, the watchdog timer is started,
3308 * and the stack is notified that the interface is ready.
3310 static int __igc_open(struct net_device *netdev, bool resuming)
3312 struct igc_adapter *adapter = netdev_priv(netdev);
3313 struct igc_hw *hw = &adapter->hw;
3317 /* disallow open during test */
3319 if (test_bit(__IGC_TESTING, &adapter->state)) {
3324 netif_carrier_off(netdev);
3326 /* allocate transmit descriptors */
3327 err = igc_setup_all_tx_resources(adapter);
3331 /* allocate receive descriptors */
3332 err = igc_setup_all_rx_resources(adapter);
3336 igc_power_up_link(adapter);
3338 igc_configure(adapter);
3340 err = igc_request_irq(adapter);
3344 /* Notify the stack of the actual queue counts. */
3345 err = netif_set_real_num_tx_queues(netdev, adapter->num_tx_queues);
3347 goto err_set_queues;
3349 err = netif_set_real_num_rx_queues(netdev, adapter->num_rx_queues);
3351 goto err_set_queues;
3353 clear_bit(__IGC_DOWN, &adapter->state);
3355 for (i = 0; i < adapter->num_q_vectors; i++)
3356 napi_enable(&adapter->q_vector[i]->napi);
3358 /* Clear any pending interrupts. */
3360 igc_irq_enable(adapter);
3362 netif_tx_start_all_queues(netdev);
3364 /* start the watchdog. */
3365 hw->mac.get_link_status = 1;
3366 schedule_work(&adapter->watchdog_task);
3371 igc_free_irq(adapter);
3373 igc_release_hw_control(adapter);
3374 igc_power_down_link(adapter);
3375 igc_free_all_rx_resources(adapter);
3377 igc_free_all_tx_resources(adapter);
3384 static int igc_open(struct net_device *netdev)
3386 return __igc_open(netdev, false);
3390 * igc_close - Disables a network interface
3391 * @netdev: network interface device structure
3393 * Returns 0, this is not allowed to fail
3395 * The close entry point is called when an interface is de-activated
3396 * by the OS. The hardware is still under the driver's control, but
3397 * needs to be disabled. A global MAC reset is issued to stop the
3398 * hardware, and all transmit and receive resources are freed.
3400 static int __igc_close(struct net_device *netdev, bool suspending)
3402 struct igc_adapter *adapter = netdev_priv(netdev);
3404 WARN_ON(test_bit(__IGC_RESETTING, &adapter->state));
3408 igc_release_hw_control(adapter);
3410 igc_free_irq(adapter);
3412 igc_free_all_tx_resources(adapter);
3413 igc_free_all_rx_resources(adapter);
3418 static int igc_close(struct net_device *netdev)
3420 if (netif_device_present(netdev) || netdev->dismantle)
3421 return __igc_close(netdev, false);
3425 static const struct net_device_ops igc_netdev_ops = {
3426 .ndo_open = igc_open,
3427 .ndo_stop = igc_close,
3428 .ndo_start_xmit = igc_xmit_frame,
3429 .ndo_set_mac_address = igc_set_mac,
3430 .ndo_change_mtu = igc_change_mtu,
3431 .ndo_get_stats = igc_get_stats,
3434 /* PCIe configuration access */
3435 void igc_read_pci_cfg(struct igc_hw *hw, u32 reg, u16 *value)
3437 struct igc_adapter *adapter = hw->back;
3439 pci_read_config_word(adapter->pdev, reg, value);
3442 void igc_write_pci_cfg(struct igc_hw *hw, u32 reg, u16 *value)
3444 struct igc_adapter *adapter = hw->back;
3446 pci_write_config_word(adapter->pdev, reg, *value);
3449 s32 igc_read_pcie_cap_reg(struct igc_hw *hw, u32 reg, u16 *value)
3451 struct igc_adapter *adapter = hw->back;
3454 cap_offset = pci_find_capability(adapter->pdev, PCI_CAP_ID_EXP);
3456 return -IGC_ERR_CONFIG;
3458 pci_read_config_word(adapter->pdev, cap_offset + reg, value);
3463 s32 igc_write_pcie_cap_reg(struct igc_hw *hw, u32 reg, u16 *value)
3465 struct igc_adapter *adapter = hw->back;
3468 cap_offset = pci_find_capability(adapter->pdev, PCI_CAP_ID_EXP);
3470 return -IGC_ERR_CONFIG;
3472 pci_write_config_word(adapter->pdev, cap_offset + reg, *value);
3477 u32 igc_rd32(struct igc_hw *hw, u32 reg)
3479 struct igc_adapter *igc = container_of(hw, struct igc_adapter, hw);
3480 u8 __iomem *hw_addr = READ_ONCE(hw->hw_addr);
3483 if (IGC_REMOVED(hw_addr))
3486 value = readl(&hw_addr[reg]);
3488 /* reads should not return all F's */
3489 if (!(~value) && (!reg || !(~readl(hw_addr)))) {
3490 struct net_device *netdev = igc->netdev;
3493 netif_device_detach(netdev);
3494 netdev_err(netdev, "PCIe link lost, device now detached\n");
3500 int igc_set_spd_dplx(struct igc_adapter *adapter, u32 spd, u8 dplx)
3502 struct pci_dev *pdev = adapter->pdev;
3503 struct igc_mac_info *mac = &adapter->hw.mac;
3507 /* Make sure dplx is at most 1 bit and lsb of speed is not set
3508 * for the switch() below to work
3510 if ((spd & 1) || (dplx & ~1))
3513 switch (spd + dplx) {
3514 case SPEED_10 + DUPLEX_HALF:
3515 mac->forced_speed_duplex = ADVERTISE_10_HALF;
3517 case SPEED_10 + DUPLEX_FULL:
3518 mac->forced_speed_duplex = ADVERTISE_10_FULL;
3520 case SPEED_100 + DUPLEX_HALF:
3521 mac->forced_speed_duplex = ADVERTISE_100_HALF;
3523 case SPEED_100 + DUPLEX_FULL:
3524 mac->forced_speed_duplex = ADVERTISE_100_FULL;
3526 case SPEED_1000 + DUPLEX_FULL:
3528 adapter->hw.phy.autoneg_advertised = ADVERTISE_1000_FULL;
3530 case SPEED_1000 + DUPLEX_HALF: /* not supported */
3532 case SPEED_2500 + DUPLEX_FULL:
3534 adapter->hw.phy.autoneg_advertised = ADVERTISE_2500_FULL;
3536 case SPEED_2500 + DUPLEX_HALF: /* not supported */
3541 /* clear MDI, MDI(-X) override is only allowed when autoneg enabled */
3542 adapter->hw.phy.mdix = AUTO_ALL_MODES;
3547 dev_err(&pdev->dev, "Unsupported Speed/Duplex configuration\n");
3552 * igc_probe - Device Initialization Routine
3553 * @pdev: PCI device information struct
3554 * @ent: entry in igc_pci_tbl
3556 * Returns 0 on success, negative on failure
3558 * igc_probe initializes an adapter identified by a pci_dev structure.
3559 * The OS initialization, configuring the adapter private structure,
3560 * and a hardware reset occur.
3562 static int igc_probe(struct pci_dev *pdev,
3563 const struct pci_device_id *ent)
3565 struct igc_adapter *adapter;
3566 struct net_device *netdev;
3568 const struct igc_info *ei = igc_info_tbl[ent->driver_data];
3571 err = pci_enable_device_mem(pdev);
3575 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
3577 err = dma_set_coherent_mask(&pdev->dev,
3580 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
3582 err = dma_set_coherent_mask(&pdev->dev,
3585 dev_err(&pdev->dev, "igc: Wrong DMA config\n");
3591 err = pci_request_selected_regions(pdev,
3592 pci_select_bars(pdev,
3598 pci_enable_pcie_error_reporting(pdev);
3600 pci_set_master(pdev);
3603 netdev = alloc_etherdev_mq(sizeof(struct igc_adapter),
3607 goto err_alloc_etherdev;
3609 SET_NETDEV_DEV(netdev, &pdev->dev);
3611 pci_set_drvdata(pdev, netdev);
3612 adapter = netdev_priv(netdev);
3613 adapter->netdev = netdev;
3614 adapter->pdev = pdev;
3617 adapter->port_num = hw->bus.func;
3618 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
3620 err = pci_save_state(pdev);
3625 adapter->io_addr = ioremap(pci_resource_start(pdev, 0),
3626 pci_resource_len(pdev, 0));
3627 if (!adapter->io_addr)
3630 /* hw->hw_addr can be zeroed, so use adapter->io_addr for unmap */
3631 hw->hw_addr = adapter->io_addr;
3633 netdev->netdev_ops = &igc_netdev_ops;
3634 igc_set_ethtool_ops(netdev);
3635 netdev->watchdog_timeo = 5 * HZ;
3637 netdev->mem_start = pci_resource_start(pdev, 0);
3638 netdev->mem_end = pci_resource_end(pdev, 0);
3640 /* PCI config space info */
3641 hw->vendor_id = pdev->vendor;
3642 hw->device_id = pdev->device;
3643 hw->revision_id = pdev->revision;
3644 hw->subsystem_vendor_id = pdev->subsystem_vendor;
3645 hw->subsystem_device_id = pdev->subsystem_device;
3647 /* Copy the default MAC and PHY function pointers */
3648 memcpy(&hw->mac.ops, ei->mac_ops, sizeof(hw->mac.ops));
3649 memcpy(&hw->phy.ops, ei->phy_ops, sizeof(hw->phy.ops));
3651 /* Initialize skew-specific constants */
3652 err = ei->get_invariants(hw);
3656 /* setup the private structure */
3657 err = igc_sw_init(adapter);
3661 /* MTU range: 68 - 9216 */
3662 netdev->min_mtu = ETH_MIN_MTU;
3663 netdev->max_mtu = MAX_STD_JUMBO_FRAME_SIZE;
3665 /* before reading the NVM, reset the controller to put the device in a
3666 * known good starting state
3668 hw->mac.ops.reset_hw(hw);
3670 if (eth_platform_get_mac_address(&pdev->dev, hw->mac.addr)) {
3671 /* copy the MAC address out of the NVM */
3672 if (hw->mac.ops.read_mac_addr(hw))
3673 dev_err(&pdev->dev, "NVM Read Error\n");
3676 memcpy(netdev->dev_addr, hw->mac.addr, netdev->addr_len);
3678 if (!is_valid_ether_addr(netdev->dev_addr)) {
3679 dev_err(&pdev->dev, "Invalid MAC Address\n");
3684 /* configure RXPBSIZE and TXPBSIZE */
3685 wr32(IGC_RXPBS, I225_RXPBSIZE_DEFAULT);
3686 wr32(IGC_TXPBS, I225_TXPBSIZE_DEFAULT);
3688 timer_setup(&adapter->watchdog_timer, igc_watchdog, 0);
3689 timer_setup(&adapter->phy_info_timer, igc_update_phy_info, 0);
3691 INIT_WORK(&adapter->reset_task, igc_reset_task);
3692 INIT_WORK(&adapter->watchdog_task, igc_watchdog_task);
3694 /* Initialize link properties that are user-changeable */
3695 adapter->fc_autoneg = true;
3696 hw->mac.autoneg = true;
3697 hw->phy.autoneg_advertised = 0xaf;
3699 hw->fc.requested_mode = igc_fc_default;
3700 hw->fc.current_mode = igc_fc_default;
3702 /* reset the hardware with the new settings */
3705 /* let the f/w know that the h/w is now under the control of the
3708 igc_get_hw_control(adapter);
3710 strncpy(netdev->name, "eth%d", IFNAMSIZ);
3711 err = register_netdev(netdev);
3715 /* carrier off reporting is important to ethtool even BEFORE open */
3716 netif_carrier_off(netdev);
3718 /* Check if Media Autosense is enabled */
3721 /* print pcie link status and MAC address */
3722 pcie_print_link_status(pdev);
3723 netdev_info(netdev, "MAC: %pM\n", netdev->dev_addr);
3728 igc_release_hw_control(adapter);
3730 if (!igc_check_reset_block(hw))
3733 igc_clear_interrupt_scheme(adapter);
3734 iounmap(adapter->io_addr);
3736 free_netdev(netdev);
3738 pci_release_selected_regions(pdev,
3739 pci_select_bars(pdev, IORESOURCE_MEM));
3742 pci_disable_device(pdev);
3747 * igc_remove - Device Removal Routine
3748 * @pdev: PCI device information struct
3750 * igc_remove is called by the PCI subsystem to alert the driver
3751 * that it should release a PCI device. This could be caused by a
3752 * Hot-Plug event, or because the driver is going to be removed from
3755 static void igc_remove(struct pci_dev *pdev)
3757 struct net_device *netdev = pci_get_drvdata(pdev);
3758 struct igc_adapter *adapter = netdev_priv(netdev);
3760 set_bit(__IGC_DOWN, &adapter->state);
3762 del_timer_sync(&adapter->watchdog_timer);
3763 del_timer_sync(&adapter->phy_info_timer);
3765 cancel_work_sync(&adapter->reset_task);
3766 cancel_work_sync(&adapter->watchdog_task);
3768 /* Release control of h/w to f/w. If f/w is AMT enabled, this
3769 * would have already happened in close and is redundant.
3771 igc_release_hw_control(adapter);
3772 unregister_netdev(netdev);
3774 igc_clear_interrupt_scheme(adapter);
3775 pci_iounmap(pdev, adapter->io_addr);
3776 pci_release_mem_regions(pdev);
3778 kfree(adapter->mac_table);
3779 kfree(adapter->shadow_vfta);
3780 free_netdev(netdev);
3782 pci_disable_pcie_error_reporting(pdev);
3784 pci_disable_device(pdev);
3787 static struct pci_driver igc_driver = {
3788 .name = igc_driver_name,
3789 .id_table = igc_pci_tbl,
3791 .remove = igc_remove,
3794 void igc_set_flag_queue_pairs(struct igc_adapter *adapter,
3795 const u32 max_rss_queues)
3797 /* Determine if we need to pair queues. */
3798 /* If rss_queues > half of max_rss_queues, pair the queues in
3799 * order to conserve interrupts due to limited supply.
3801 if (adapter->rss_queues > (max_rss_queues / 2))
3802 adapter->flags |= IGC_FLAG_QUEUE_PAIRS;
3804 adapter->flags &= ~IGC_FLAG_QUEUE_PAIRS;
3807 unsigned int igc_get_max_rss_queues(struct igc_adapter *adapter)
3809 unsigned int max_rss_queues;
3811 /* Determine the maximum number of RSS queues supported. */
3812 max_rss_queues = IGC_MAX_RX_QUEUES;
3814 return max_rss_queues;
3817 static void igc_init_queue_configuration(struct igc_adapter *adapter)
3821 max_rss_queues = igc_get_max_rss_queues(adapter);
3822 adapter->rss_queues = min_t(u32, max_rss_queues, num_online_cpus());
3824 igc_set_flag_queue_pairs(adapter, max_rss_queues);
3828 * igc_sw_init - Initialize general software structures (struct igc_adapter)
3829 * @adapter: board private structure to initialize
3831 * igc_sw_init initializes the Adapter private data structure.
3832 * Fields are initialized based on PCI device information and
3833 * OS network device settings (MTU size).
3835 static int igc_sw_init(struct igc_adapter *adapter)
3837 struct net_device *netdev = adapter->netdev;
3838 struct pci_dev *pdev = adapter->pdev;
3839 struct igc_hw *hw = &adapter->hw;
3841 int size = sizeof(struct igc_mac_addr) * hw->mac.rar_entry_count;
3843 pci_read_config_word(pdev, PCI_COMMAND, &hw->bus.pci_cmd_word);
3845 /* set default ring sizes */
3846 adapter->tx_ring_count = IGC_DEFAULT_TXD;
3847 adapter->rx_ring_count = IGC_DEFAULT_RXD;
3849 /* set default ITR values */
3850 adapter->rx_itr_setting = IGC_DEFAULT_ITR;
3851 adapter->tx_itr_setting = IGC_DEFAULT_ITR;
3853 /* set default work limits */
3854 adapter->tx_work_limit = IGC_DEFAULT_TX_WORK;
3856 /* adjust max frame to be at least the size of a standard frame */
3857 adapter->max_frame_size = netdev->mtu + ETH_HLEN + ETH_FCS_LEN +
3859 adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
3861 spin_lock_init(&adapter->nfc_lock);
3862 spin_lock_init(&adapter->stats64_lock);
3863 /* Assume MSI-X interrupts, will be checked during IRQ allocation */
3864 adapter->flags |= IGC_FLAG_HAS_MSIX;
3866 adapter->mac_table = kzalloc(size, GFP_ATOMIC);
3867 if (!adapter->mac_table)
3870 igc_init_queue_configuration(adapter);
3872 /* This call may decrease the number of queues */
3873 if (igc_init_interrupt_scheme(adapter, true)) {
3874 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
3878 /* Explicitly disable IRQ since the NIC can be in any state. */
3879 igc_irq_disable(adapter);
3881 set_bit(__IGC_DOWN, &adapter->state);
3887 * igc_reinit_queues - return error
3888 * @adapter: pointer to adapter structure
3890 int igc_reinit_queues(struct igc_adapter *adapter)
3892 struct net_device *netdev = adapter->netdev;
3893 struct pci_dev *pdev = adapter->pdev;
3896 if (netif_running(netdev))
3899 igc_reset_interrupt_capability(adapter);
3901 if (igc_init_interrupt_scheme(adapter, true)) {
3902 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
3906 if (netif_running(netdev))
3907 err = igc_open(netdev);
3913 * igc_get_hw_dev - return device
3914 * @hw: pointer to hardware structure
3916 * used by hardware layer to print debugging information
3918 struct net_device *igc_get_hw_dev(struct igc_hw *hw)
3920 struct igc_adapter *adapter = hw->back;
3922 return adapter->netdev;
3926 * igc_init_module - Driver Registration Routine
3928 * igc_init_module is the first routine called when the driver is
3929 * loaded. All it does is register with the PCI subsystem.
3931 static int __init igc_init_module(void)
3935 pr_info("%s - version %s\n",
3936 igc_driver_string, igc_driver_version);
3938 pr_info("%s\n", igc_copyright);
3940 ret = pci_register_driver(&igc_driver);
3944 module_init(igc_init_module);
3947 * igc_exit_module - Driver Exit Cleanup Routine
3949 * igc_exit_module is called just before the driver is removed
3952 static void __exit igc_exit_module(void)
3954 pci_unregister_driver(&igc_driver);
3957 module_exit(igc_exit_module);