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>
17 #define DRV_VERSION "0.0.1-k"
18 #define DRV_SUMMARY "Intel(R) 2.5G Ethernet Linux Driver"
20 #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
22 static int debug = -1;
25 MODULE_DESCRIPTION(DRV_SUMMARY);
26 MODULE_LICENSE("GPL v2");
27 MODULE_VERSION(DRV_VERSION);
28 module_param(debug, int, 0);
29 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
31 char igc_driver_name[] = "igc";
32 char igc_driver_version[] = DRV_VERSION;
33 static const char igc_driver_string[] = DRV_SUMMARY;
34 static const char igc_copyright[] =
35 "Copyright(c) 2018 Intel Corporation.";
37 static const struct igc_info *igc_info_tbl[] = {
38 [board_base] = &igc_base_info,
41 static const struct pci_device_id igc_pci_tbl[] = {
42 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_LM), board_base },
43 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_V), board_base },
44 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_I), board_base },
45 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I220_V), board_base },
46 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_K), board_base },
47 /* required last entry */
51 MODULE_DEVICE_TABLE(pci, igc_pci_tbl);
53 /* forward declaration */
54 static void igc_clean_tx_ring(struct igc_ring *tx_ring);
55 static int igc_sw_init(struct igc_adapter *);
56 static void igc_configure(struct igc_adapter *adapter);
57 static void igc_power_down_link(struct igc_adapter *adapter);
58 static void igc_set_default_mac_filter(struct igc_adapter *adapter);
59 static void igc_set_rx_mode(struct net_device *netdev);
60 static void igc_write_itr(struct igc_q_vector *q_vector);
61 static void igc_assign_vector(struct igc_q_vector *q_vector, int msix_vector);
62 static void igc_free_q_vector(struct igc_adapter *adapter, int v_idx);
63 static void igc_set_interrupt_capability(struct igc_adapter *adapter,
65 static void igc_free_q_vectors(struct igc_adapter *adapter);
66 static void igc_irq_disable(struct igc_adapter *adapter);
67 static void igc_irq_enable(struct igc_adapter *adapter);
68 static void igc_configure_msix(struct igc_adapter *adapter);
69 static bool igc_alloc_mapped_page(struct igc_ring *rx_ring,
70 struct igc_rx_buffer *bi);
79 void igc_reset(struct igc_adapter *adapter)
81 struct pci_dev *pdev = adapter->pdev;
82 struct igc_hw *hw = &adapter->hw;
83 struct igc_fc_info *fc = &hw->fc;
86 /* Repartition PBA for greater than 9k MTU if required */
89 /* flow control settings
90 * The high water mark must be low enough to fit one full frame
91 * after transmitting the pause frame. As such we must have enough
92 * space to allow for us to complete our current transmit and then
93 * receive the frame that is in progress from the link partner.
95 * - the full Rx FIFO size minus one full Tx plus one full Rx frame
97 hwm = (pba << 10) - (adapter->max_frame_size + MAX_JUMBO_FRAME_SIZE);
99 fc->high_water = hwm & 0xFFFFFFF0; /* 16-byte granularity */
100 fc->low_water = fc->high_water - 16;
101 fc->pause_time = 0xFFFF;
103 fc->current_mode = fc->requested_mode;
105 hw->mac.ops.reset_hw(hw);
107 if (hw->mac.ops.init_hw(hw))
108 dev_err(&pdev->dev, "Hardware Error\n");
110 if (!netif_running(adapter->netdev))
111 igc_power_down_link(adapter);
113 igc_get_phy_info(hw);
117 * igc_power_up_link - Power up the phy/serdes link
118 * @adapter: address of board private structure
120 static void igc_power_up_link(struct igc_adapter *adapter)
122 igc_reset_phy(&adapter->hw);
124 if (adapter->hw.phy.media_type == igc_media_type_copper)
125 igc_power_up_phy_copper(&adapter->hw);
127 igc_setup_link(&adapter->hw);
131 * igc_power_down_link - Power down the phy/serdes link
132 * @adapter: address of board private structure
134 static void igc_power_down_link(struct igc_adapter *adapter)
136 if (adapter->hw.phy.media_type == igc_media_type_copper)
137 igc_power_down_phy_copper_base(&adapter->hw);
141 * igc_release_hw_control - release control of the h/w to f/w
142 * @adapter: address of board private structure
144 * igc_release_hw_control resets CTRL_EXT:DRV_LOAD bit.
145 * For ASF and Pass Through versions of f/w this means that the
146 * driver is no longer loaded.
148 static void igc_release_hw_control(struct igc_adapter *adapter)
150 struct igc_hw *hw = &adapter->hw;
153 /* Let firmware take over control of h/w */
154 ctrl_ext = rd32(IGC_CTRL_EXT);
156 ctrl_ext & ~IGC_CTRL_EXT_DRV_LOAD);
160 * igc_get_hw_control - get control of the h/w from f/w
161 * @adapter: address of board private structure
163 * igc_get_hw_control sets CTRL_EXT:DRV_LOAD bit.
164 * For ASF and Pass Through versions of f/w this means that
165 * the driver is loaded.
167 static void igc_get_hw_control(struct igc_adapter *adapter)
169 struct igc_hw *hw = &adapter->hw;
172 /* Let firmware know the driver has taken over */
173 ctrl_ext = rd32(IGC_CTRL_EXT);
175 ctrl_ext | IGC_CTRL_EXT_DRV_LOAD);
179 * igc_free_tx_resources - Free Tx Resources per Queue
180 * @tx_ring: Tx descriptor ring for a specific queue
182 * Free all transmit software resources
184 void igc_free_tx_resources(struct igc_ring *tx_ring)
186 igc_clean_tx_ring(tx_ring);
188 vfree(tx_ring->tx_buffer_info);
189 tx_ring->tx_buffer_info = NULL;
191 /* if not set, then don't free */
195 dma_free_coherent(tx_ring->dev, tx_ring->size,
196 tx_ring->desc, tx_ring->dma);
198 tx_ring->desc = NULL;
202 * igc_free_all_tx_resources - Free Tx Resources for All Queues
203 * @adapter: board private structure
205 * Free all transmit software resources
207 static void igc_free_all_tx_resources(struct igc_adapter *adapter)
211 for (i = 0; i < adapter->num_tx_queues; i++)
212 igc_free_tx_resources(adapter->tx_ring[i]);
216 * igc_clean_tx_ring - Free Tx Buffers
217 * @tx_ring: ring to be cleaned
219 static void igc_clean_tx_ring(struct igc_ring *tx_ring)
221 u16 i = tx_ring->next_to_clean;
222 struct igc_tx_buffer *tx_buffer = &tx_ring->tx_buffer_info[i];
224 while (i != tx_ring->next_to_use) {
225 union igc_adv_tx_desc *eop_desc, *tx_desc;
227 /* Free all the Tx ring sk_buffs */
228 dev_kfree_skb_any(tx_buffer->skb);
230 /* unmap skb header data */
231 dma_unmap_single(tx_ring->dev,
232 dma_unmap_addr(tx_buffer, dma),
233 dma_unmap_len(tx_buffer, len),
236 /* check for eop_desc to determine the end of the packet */
237 eop_desc = tx_buffer->next_to_watch;
238 tx_desc = IGC_TX_DESC(tx_ring, i);
240 /* unmap remaining buffers */
241 while (tx_desc != eop_desc) {
245 if (unlikely(i == tx_ring->count)) {
247 tx_buffer = tx_ring->tx_buffer_info;
248 tx_desc = IGC_TX_DESC(tx_ring, 0);
251 /* unmap any remaining paged data */
252 if (dma_unmap_len(tx_buffer, len))
253 dma_unmap_page(tx_ring->dev,
254 dma_unmap_addr(tx_buffer, dma),
255 dma_unmap_len(tx_buffer, len),
259 /* move us one more past the eop_desc for start of next pkt */
262 if (unlikely(i == tx_ring->count)) {
264 tx_buffer = tx_ring->tx_buffer_info;
268 /* reset BQL for queue */
269 netdev_tx_reset_queue(txring_txq(tx_ring));
271 /* reset next_to_use and next_to_clean */
272 tx_ring->next_to_use = 0;
273 tx_ring->next_to_clean = 0;
277 * igc_clean_all_tx_rings - Free Tx Buffers for all queues
278 * @adapter: board private structure
280 static void igc_clean_all_tx_rings(struct igc_adapter *adapter)
284 for (i = 0; i < adapter->num_tx_queues; i++)
285 if (adapter->tx_ring[i])
286 igc_clean_tx_ring(adapter->tx_ring[i]);
290 * igc_setup_tx_resources - allocate Tx resources (Descriptors)
291 * @tx_ring: tx descriptor ring (for a specific queue) to setup
293 * Return 0 on success, negative on failure
295 int igc_setup_tx_resources(struct igc_ring *tx_ring)
297 struct device *dev = tx_ring->dev;
300 size = sizeof(struct igc_tx_buffer) * tx_ring->count;
301 tx_ring->tx_buffer_info = vzalloc(size);
302 if (!tx_ring->tx_buffer_info)
305 /* round up to nearest 4K */
306 tx_ring->size = tx_ring->count * sizeof(union igc_adv_tx_desc);
307 tx_ring->size = ALIGN(tx_ring->size, 4096);
309 tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
310 &tx_ring->dma, GFP_KERNEL);
315 tx_ring->next_to_use = 0;
316 tx_ring->next_to_clean = 0;
321 vfree(tx_ring->tx_buffer_info);
323 "Unable to allocate memory for the transmit descriptor ring\n");
328 * igc_setup_all_tx_resources - wrapper to allocate Tx resources for all queues
329 * @adapter: board private structure
331 * Return 0 on success, negative on failure
333 static int igc_setup_all_tx_resources(struct igc_adapter *adapter)
335 struct pci_dev *pdev = adapter->pdev;
338 for (i = 0; i < adapter->num_tx_queues; i++) {
339 err = igc_setup_tx_resources(adapter->tx_ring[i]);
342 "Allocation for Tx Queue %u failed\n", i);
343 for (i--; i >= 0; i--)
344 igc_free_tx_resources(adapter->tx_ring[i]);
353 * igc_clean_rx_ring - Free Rx Buffers per Queue
354 * @rx_ring: ring to free buffers from
356 static void igc_clean_rx_ring(struct igc_ring *rx_ring)
358 u16 i = rx_ring->next_to_clean;
360 dev_kfree_skb(rx_ring->skb);
363 /* Free all the Rx ring sk_buffs */
364 while (i != rx_ring->next_to_alloc) {
365 struct igc_rx_buffer *buffer_info = &rx_ring->rx_buffer_info[i];
367 /* Invalidate cache lines that may have been written to by
368 * device so that we avoid corrupting memory.
370 dma_sync_single_range_for_cpu(rx_ring->dev,
372 buffer_info->page_offset,
373 igc_rx_bufsz(rx_ring),
376 /* free resources associated with mapping */
377 dma_unmap_page_attrs(rx_ring->dev,
379 igc_rx_pg_size(rx_ring),
382 __page_frag_cache_drain(buffer_info->page,
383 buffer_info->pagecnt_bias);
386 if (i == rx_ring->count)
390 rx_ring->next_to_alloc = 0;
391 rx_ring->next_to_clean = 0;
392 rx_ring->next_to_use = 0;
396 * igc_clean_all_rx_rings - Free Rx Buffers for all queues
397 * @adapter: board private structure
399 static void igc_clean_all_rx_rings(struct igc_adapter *adapter)
403 for (i = 0; i < adapter->num_rx_queues; i++)
404 if (adapter->rx_ring[i])
405 igc_clean_rx_ring(adapter->rx_ring[i]);
409 * igc_free_rx_resources - Free Rx Resources
410 * @rx_ring: ring to clean the resources from
412 * Free all receive software resources
414 void igc_free_rx_resources(struct igc_ring *rx_ring)
416 igc_clean_rx_ring(rx_ring);
418 vfree(rx_ring->rx_buffer_info);
419 rx_ring->rx_buffer_info = NULL;
421 /* if not set, then don't free */
425 dma_free_coherent(rx_ring->dev, rx_ring->size,
426 rx_ring->desc, rx_ring->dma);
428 rx_ring->desc = NULL;
432 * igc_free_all_rx_resources - Free Rx Resources for All Queues
433 * @adapter: board private structure
435 * Free all receive software resources
437 static void igc_free_all_rx_resources(struct igc_adapter *adapter)
441 for (i = 0; i < adapter->num_rx_queues; i++)
442 igc_free_rx_resources(adapter->rx_ring[i]);
446 * igc_setup_rx_resources - allocate Rx resources (Descriptors)
447 * @rx_ring: rx descriptor ring (for a specific queue) to setup
449 * Returns 0 on success, negative on failure
451 int igc_setup_rx_resources(struct igc_ring *rx_ring)
453 struct device *dev = rx_ring->dev;
456 size = sizeof(struct igc_rx_buffer) * rx_ring->count;
457 rx_ring->rx_buffer_info = vzalloc(size);
458 if (!rx_ring->rx_buffer_info)
461 desc_len = sizeof(union igc_adv_rx_desc);
463 /* Round up to nearest 4K */
464 rx_ring->size = rx_ring->count * desc_len;
465 rx_ring->size = ALIGN(rx_ring->size, 4096);
467 rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
468 &rx_ring->dma, GFP_KERNEL);
473 rx_ring->next_to_alloc = 0;
474 rx_ring->next_to_clean = 0;
475 rx_ring->next_to_use = 0;
480 vfree(rx_ring->rx_buffer_info);
481 rx_ring->rx_buffer_info = NULL;
483 "Unable to allocate memory for the receive descriptor ring\n");
488 * igc_setup_all_rx_resources - wrapper to allocate Rx resources
489 * (Descriptors) for all queues
490 * @adapter: board private structure
492 * Return 0 on success, negative on failure
494 static int igc_setup_all_rx_resources(struct igc_adapter *adapter)
496 struct pci_dev *pdev = adapter->pdev;
499 for (i = 0; i < adapter->num_rx_queues; i++) {
500 err = igc_setup_rx_resources(adapter->rx_ring[i]);
503 "Allocation for Rx Queue %u failed\n", i);
504 for (i--; i >= 0; i--)
505 igc_free_rx_resources(adapter->rx_ring[i]);
514 * igc_configure_rx_ring - Configure a receive ring after Reset
515 * @adapter: board private structure
516 * @ring: receive ring to be configured
518 * Configure the Rx unit of the MAC after a reset.
520 static void igc_configure_rx_ring(struct igc_adapter *adapter,
521 struct igc_ring *ring)
523 struct igc_hw *hw = &adapter->hw;
524 union igc_adv_rx_desc *rx_desc;
525 int reg_idx = ring->reg_idx;
526 u32 srrctl = 0, rxdctl = 0;
527 u64 rdba = ring->dma;
529 /* disable the queue */
530 wr32(IGC_RXDCTL(reg_idx), 0);
532 /* Set DMA base address registers */
533 wr32(IGC_RDBAL(reg_idx),
534 rdba & 0x00000000ffffffffULL);
535 wr32(IGC_RDBAH(reg_idx), rdba >> 32);
536 wr32(IGC_RDLEN(reg_idx),
537 ring->count * sizeof(union igc_adv_rx_desc));
539 /* initialize head and tail */
540 ring->tail = adapter->io_addr + IGC_RDT(reg_idx);
541 wr32(IGC_RDH(reg_idx), 0);
542 writel(0, ring->tail);
544 /* reset next-to- use/clean to place SW in sync with hardware */
545 ring->next_to_clean = 0;
546 ring->next_to_use = 0;
548 /* set descriptor configuration */
549 srrctl = IGC_RX_HDR_LEN << IGC_SRRCTL_BSIZEHDRSIZE_SHIFT;
550 if (ring_uses_large_buffer(ring))
551 srrctl |= IGC_RXBUFFER_3072 >> IGC_SRRCTL_BSIZEPKT_SHIFT;
553 srrctl |= IGC_RXBUFFER_2048 >> IGC_SRRCTL_BSIZEPKT_SHIFT;
554 srrctl |= IGC_SRRCTL_DESCTYPE_ADV_ONEBUF;
556 wr32(IGC_SRRCTL(reg_idx), srrctl);
558 rxdctl |= IGC_RX_PTHRESH;
559 rxdctl |= IGC_RX_HTHRESH << 8;
560 rxdctl |= IGC_RX_WTHRESH << 16;
562 /* initialize rx_buffer_info */
563 memset(ring->rx_buffer_info, 0,
564 sizeof(struct igc_rx_buffer) * ring->count);
566 /* initialize Rx descriptor 0 */
567 rx_desc = IGC_RX_DESC(ring, 0);
568 rx_desc->wb.upper.length = 0;
570 /* enable receive descriptor fetching */
571 rxdctl |= IGC_RXDCTL_QUEUE_ENABLE;
573 wr32(IGC_RXDCTL(reg_idx), rxdctl);
577 * igc_configure_rx - Configure receive Unit after Reset
578 * @adapter: board private structure
580 * Configure the Rx unit of the MAC after a reset.
582 static void igc_configure_rx(struct igc_adapter *adapter)
586 /* Setup the HW Rx Head and Tail Descriptor Pointers and
587 * the Base and Length of the Rx Descriptor Ring
589 for (i = 0; i < adapter->num_rx_queues; i++)
590 igc_configure_rx_ring(adapter, adapter->rx_ring[i]);
594 * igc_configure_tx_ring - Configure transmit ring after Reset
595 * @adapter: board private structure
596 * @ring: tx ring to configure
598 * Configure a transmit ring after a reset.
600 static void igc_configure_tx_ring(struct igc_adapter *adapter,
601 struct igc_ring *ring)
603 struct igc_hw *hw = &adapter->hw;
604 int reg_idx = ring->reg_idx;
605 u64 tdba = ring->dma;
608 /* disable the queue */
609 wr32(IGC_TXDCTL(reg_idx), 0);
613 wr32(IGC_TDLEN(reg_idx),
614 ring->count * sizeof(union igc_adv_tx_desc));
615 wr32(IGC_TDBAL(reg_idx),
616 tdba & 0x00000000ffffffffULL);
617 wr32(IGC_TDBAH(reg_idx), tdba >> 32);
619 ring->tail = adapter->io_addr + IGC_TDT(reg_idx);
620 wr32(IGC_TDH(reg_idx), 0);
621 writel(0, ring->tail);
623 txdctl |= IGC_TX_PTHRESH;
624 txdctl |= IGC_TX_HTHRESH << 8;
625 txdctl |= IGC_TX_WTHRESH << 16;
627 txdctl |= IGC_TXDCTL_QUEUE_ENABLE;
628 wr32(IGC_TXDCTL(reg_idx), txdctl);
632 * igc_configure_tx - Configure transmit Unit after Reset
633 * @adapter: board private structure
635 * Configure the Tx unit of the MAC after a reset.
637 static void igc_configure_tx(struct igc_adapter *adapter)
641 for (i = 0; i < adapter->num_tx_queues; i++)
642 igc_configure_tx_ring(adapter, adapter->tx_ring[i]);
646 * igc_setup_mrqc - configure the multiple receive queue control registers
647 * @adapter: Board private structure
649 static void igc_setup_mrqc(struct igc_adapter *adapter)
651 struct igc_hw *hw = &adapter->hw;
652 u32 j, num_rx_queues;
656 netdev_rss_key_fill(rss_key, sizeof(rss_key));
657 for (j = 0; j < 10; j++)
658 wr32(IGC_RSSRK(j), rss_key[j]);
660 num_rx_queues = adapter->rss_queues;
662 if (adapter->rss_indir_tbl_init != num_rx_queues) {
663 for (j = 0; j < IGC_RETA_SIZE; j++)
664 adapter->rss_indir_tbl[j] =
665 (j * num_rx_queues) / IGC_RETA_SIZE;
666 adapter->rss_indir_tbl_init = num_rx_queues;
668 igc_write_rss_indir_tbl(adapter);
670 /* Disable raw packet checksumming so that RSS hash is placed in
671 * descriptor on writeback. No need to enable TCP/UDP/IP checksum
672 * offloads as they are enabled by default
674 rxcsum = rd32(IGC_RXCSUM);
675 rxcsum |= IGC_RXCSUM_PCSD;
677 /* Enable Receive Checksum Offload for SCTP */
678 rxcsum |= IGC_RXCSUM_CRCOFL;
680 /* Don't need to set TUOFL or IPOFL, they default to 1 */
681 wr32(IGC_RXCSUM, rxcsum);
683 /* Generate RSS hash based on packet types, TCP/UDP
684 * port numbers and/or IPv4/v6 src and dst addresses
686 mrqc = IGC_MRQC_RSS_FIELD_IPV4 |
687 IGC_MRQC_RSS_FIELD_IPV4_TCP |
688 IGC_MRQC_RSS_FIELD_IPV6 |
689 IGC_MRQC_RSS_FIELD_IPV6_TCP |
690 IGC_MRQC_RSS_FIELD_IPV6_TCP_EX;
692 if (adapter->flags & IGC_FLAG_RSS_FIELD_IPV4_UDP)
693 mrqc |= IGC_MRQC_RSS_FIELD_IPV4_UDP;
694 if (adapter->flags & IGC_FLAG_RSS_FIELD_IPV6_UDP)
695 mrqc |= IGC_MRQC_RSS_FIELD_IPV6_UDP;
697 mrqc |= IGC_MRQC_ENABLE_RSS_MQ;
699 wr32(IGC_MRQC, mrqc);
703 * igc_setup_rctl - configure the receive control registers
704 * @adapter: Board private structure
706 static void igc_setup_rctl(struct igc_adapter *adapter)
708 struct igc_hw *hw = &adapter->hw;
711 rctl = rd32(IGC_RCTL);
713 rctl &= ~(3 << IGC_RCTL_MO_SHIFT);
714 rctl &= ~(IGC_RCTL_LBM_TCVR | IGC_RCTL_LBM_MAC);
716 rctl |= IGC_RCTL_EN | IGC_RCTL_BAM | IGC_RCTL_RDMTS_HALF |
717 (hw->mac.mc_filter_type << IGC_RCTL_MO_SHIFT);
719 /* enable stripping of CRC. Newer features require
720 * that the HW strips the CRC.
722 rctl |= IGC_RCTL_SECRC;
724 /* disable store bad packets and clear size bits. */
725 rctl &= ~(IGC_RCTL_SBP | IGC_RCTL_SZ_256);
727 /* enable LPE to allow for reception of jumbo frames */
728 rctl |= IGC_RCTL_LPE;
730 /* disable queue 0 to prevent tail write w/o re-config */
731 wr32(IGC_RXDCTL(0), 0);
733 /* This is useful for sniffing bad packets. */
734 if (adapter->netdev->features & NETIF_F_RXALL) {
735 /* UPE and MPE will be handled by normal PROMISC logic
738 rctl |= (IGC_RCTL_SBP | /* Receive bad packets */
739 IGC_RCTL_BAM | /* RX All Bcast Pkts */
740 IGC_RCTL_PMCF); /* RX All MAC Ctrl Pkts */
742 rctl &= ~(IGC_RCTL_DPF | /* Allow filtered pause */
743 IGC_RCTL_CFIEN); /* Disable VLAN CFIEN Filter */
746 wr32(IGC_RCTL, rctl);
750 * igc_setup_tctl - configure the transmit control registers
751 * @adapter: Board private structure
753 static void igc_setup_tctl(struct igc_adapter *adapter)
755 struct igc_hw *hw = &adapter->hw;
758 /* disable queue 0 which icould be enabled by default */
759 wr32(IGC_TXDCTL(0), 0);
761 /* Program the Transmit Control Register */
762 tctl = rd32(IGC_TCTL);
763 tctl &= ~IGC_TCTL_CT;
764 tctl |= IGC_TCTL_PSP | IGC_TCTL_RTLC |
765 (IGC_COLLISION_THRESHOLD << IGC_CT_SHIFT);
767 /* Enable transmits */
770 wr32(IGC_TCTL, tctl);
774 * igc_set_mac - Change the Ethernet Address of the NIC
775 * @netdev: network interface device structure
776 * @p: pointer to an address structure
778 * Returns 0 on success, negative on failure
780 static int igc_set_mac(struct net_device *netdev, void *p)
782 struct igc_adapter *adapter = netdev_priv(netdev);
783 struct igc_hw *hw = &adapter->hw;
784 struct sockaddr *addr = p;
786 if (!is_valid_ether_addr(addr->sa_data))
787 return -EADDRNOTAVAIL;
789 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
790 memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
792 /* set the correct pool for the new PF MAC address in entry 0 */
793 igc_set_default_mac_filter(adapter);
799 * igc_write_mc_addr_list - write multicast addresses to MTA
800 * @netdev: network interface device structure
802 * Writes multicast address list to the MTA hash table.
803 * Returns: -ENOMEM on failure
804 * 0 on no addresses written
805 * X on writing X addresses to MTA
807 static int igc_write_mc_addr_list(struct net_device *netdev)
809 struct igc_adapter *adapter = netdev_priv(netdev);
810 struct igc_hw *hw = &adapter->hw;
811 struct netdev_hw_addr *ha;
815 if (netdev_mc_empty(netdev)) {
816 /* nothing to program, so clear mc list */
817 igc_update_mc_addr_list(hw, NULL, 0);
821 mta_list = kcalloc(netdev_mc_count(netdev), 6, GFP_ATOMIC);
825 /* The shared function expects a packed array of only addresses. */
827 netdev_for_each_mc_addr(ha, netdev)
828 memcpy(mta_list + (i++ * ETH_ALEN), ha->addr, ETH_ALEN);
830 igc_update_mc_addr_list(hw, mta_list, i);
833 return netdev_mc_count(netdev);
836 static void igc_tx_ctxtdesc(struct igc_ring *tx_ring,
837 struct igc_tx_buffer *first,
838 u32 vlan_macip_lens, u32 type_tucmd,
841 struct igc_adv_tx_context_desc *context_desc;
842 u16 i = tx_ring->next_to_use;
843 struct timespec64 ts;
845 context_desc = IGC_TX_CTXTDESC(tx_ring, i);
848 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
850 /* set bits to identify this as an advanced context descriptor */
851 type_tucmd |= IGC_TXD_CMD_DEXT | IGC_ADVTXD_DTYP_CTXT;
853 /* For 82575, context index must be unique per ring. */
854 if (test_bit(IGC_RING_FLAG_TX_CTX_IDX, &tx_ring->flags))
855 mss_l4len_idx |= tx_ring->reg_idx << 4;
857 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
858 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd);
859 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
861 /* We assume there is always a valid Tx time available. Invalid times
862 * should have been handled by the upper layers.
864 if (tx_ring->launchtime_enable) {
865 ts = ns_to_timespec64(first->skb->tstamp);
866 first->skb->tstamp = 0;
867 context_desc->launch_time = cpu_to_le32(ts.tv_nsec / 32);
869 context_desc->launch_time = 0;
873 static inline bool igc_ipv6_csum_is_sctp(struct sk_buff *skb)
875 unsigned int offset = 0;
877 ipv6_find_hdr(skb, &offset, IPPROTO_SCTP, NULL, NULL);
879 return offset == skb_checksum_start_offset(skb);
882 static void igc_tx_csum(struct igc_ring *tx_ring, struct igc_tx_buffer *first)
884 struct sk_buff *skb = first->skb;
885 u32 vlan_macip_lens = 0;
888 if (skb->ip_summed != CHECKSUM_PARTIAL) {
890 if (!(first->tx_flags & IGC_TX_FLAGS_VLAN) &&
891 !tx_ring->launchtime_enable)
896 switch (skb->csum_offset) {
897 case offsetof(struct tcphdr, check):
898 type_tucmd = IGC_ADVTXD_TUCMD_L4T_TCP;
900 case offsetof(struct udphdr, check):
902 case offsetof(struct sctphdr, checksum):
903 /* validate that this is actually an SCTP request */
904 if ((first->protocol == htons(ETH_P_IP) &&
905 (ip_hdr(skb)->protocol == IPPROTO_SCTP)) ||
906 (first->protocol == htons(ETH_P_IPV6) &&
907 igc_ipv6_csum_is_sctp(skb))) {
908 type_tucmd = IGC_ADVTXD_TUCMD_L4T_SCTP;
913 skb_checksum_help(skb);
917 /* update TX checksum flag */
918 first->tx_flags |= IGC_TX_FLAGS_CSUM;
919 vlan_macip_lens = skb_checksum_start_offset(skb) -
920 skb_network_offset(skb);
922 vlan_macip_lens |= skb_network_offset(skb) << IGC_ADVTXD_MACLEN_SHIFT;
923 vlan_macip_lens |= first->tx_flags & IGC_TX_FLAGS_VLAN_MASK;
925 igc_tx_ctxtdesc(tx_ring, first, vlan_macip_lens, type_tucmd, 0);
928 static int __igc_maybe_stop_tx(struct igc_ring *tx_ring, const u16 size)
930 struct net_device *netdev = tx_ring->netdev;
932 netif_stop_subqueue(netdev, tx_ring->queue_index);
934 /* memory barriier comment */
937 /* We need to check again in a case another CPU has just
938 * made room available.
940 if (igc_desc_unused(tx_ring) < size)
944 netif_wake_subqueue(netdev, tx_ring->queue_index);
946 u64_stats_update_begin(&tx_ring->tx_syncp2);
947 tx_ring->tx_stats.restart_queue2++;
948 u64_stats_update_end(&tx_ring->tx_syncp2);
953 static inline int igc_maybe_stop_tx(struct igc_ring *tx_ring, const u16 size)
955 if (igc_desc_unused(tx_ring) >= size)
957 return __igc_maybe_stop_tx(tx_ring, size);
960 static u32 igc_tx_cmd_type(struct sk_buff *skb, u32 tx_flags)
962 /* set type for advanced descriptor with frame checksum insertion */
963 u32 cmd_type = IGC_ADVTXD_DTYP_DATA |
964 IGC_ADVTXD_DCMD_DEXT |
965 IGC_ADVTXD_DCMD_IFCS;
970 static void igc_tx_olinfo_status(struct igc_ring *tx_ring,
971 union igc_adv_tx_desc *tx_desc,
972 u32 tx_flags, unsigned int paylen)
974 u32 olinfo_status = paylen << IGC_ADVTXD_PAYLEN_SHIFT;
976 /* insert L4 checksum */
977 olinfo_status |= (tx_flags & IGC_TX_FLAGS_CSUM) *
978 ((IGC_TXD_POPTS_TXSM << 8) /
981 /* insert IPv4 checksum */
982 olinfo_status |= (tx_flags & IGC_TX_FLAGS_IPV4) *
983 (((IGC_TXD_POPTS_IXSM << 8)) /
986 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
989 static int igc_tx_map(struct igc_ring *tx_ring,
990 struct igc_tx_buffer *first,
993 struct sk_buff *skb = first->skb;
994 struct igc_tx_buffer *tx_buffer;
995 union igc_adv_tx_desc *tx_desc;
996 u32 tx_flags = first->tx_flags;
998 u16 i = tx_ring->next_to_use;
999 unsigned int data_len, size;
1001 u32 cmd_type = igc_tx_cmd_type(skb, tx_flags);
1003 tx_desc = IGC_TX_DESC(tx_ring, i);
1005 igc_tx_olinfo_status(tx_ring, tx_desc, tx_flags, skb->len - hdr_len);
1007 size = skb_headlen(skb);
1008 data_len = skb->data_len;
1010 dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
1014 for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
1015 if (dma_mapping_error(tx_ring->dev, dma))
1018 /* record length, and DMA address */
1019 dma_unmap_len_set(tx_buffer, len, size);
1020 dma_unmap_addr_set(tx_buffer, dma, dma);
1022 tx_desc->read.buffer_addr = cpu_to_le64(dma);
1024 while (unlikely(size > IGC_MAX_DATA_PER_TXD)) {
1025 tx_desc->read.cmd_type_len =
1026 cpu_to_le32(cmd_type ^ IGC_MAX_DATA_PER_TXD);
1030 if (i == tx_ring->count) {
1031 tx_desc = IGC_TX_DESC(tx_ring, 0);
1034 tx_desc->read.olinfo_status = 0;
1036 dma += IGC_MAX_DATA_PER_TXD;
1037 size -= IGC_MAX_DATA_PER_TXD;
1039 tx_desc->read.buffer_addr = cpu_to_le64(dma);
1042 if (likely(!data_len))
1045 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type ^ size);
1049 if (i == tx_ring->count) {
1050 tx_desc = IGC_TX_DESC(tx_ring, 0);
1053 tx_desc->read.olinfo_status = 0;
1055 size = skb_frag_size(frag);
1058 dma = skb_frag_dma_map(tx_ring->dev, frag, 0,
1059 size, DMA_TO_DEVICE);
1061 tx_buffer = &tx_ring->tx_buffer_info[i];
1064 /* write last descriptor with RS and EOP bits */
1065 cmd_type |= size | IGC_TXD_DCMD;
1066 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type);
1068 netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount);
1070 /* set the timestamp */
1071 first->time_stamp = jiffies;
1073 skb_tx_timestamp(skb);
1075 /* Force memory writes to complete before letting h/w know there
1076 * are new descriptors to fetch. (Only applicable for weak-ordered
1077 * memory model archs, such as IA-64).
1079 * We also need this memory barrier to make certain all of the
1080 * status bits have been updated before next_to_watch is written.
1084 /* set next_to_watch value indicating a packet is present */
1085 first->next_to_watch = tx_desc;
1088 if (i == tx_ring->count)
1091 tx_ring->next_to_use = i;
1093 /* Make sure there is space in the ring for the next send. */
1094 igc_maybe_stop_tx(tx_ring, DESC_NEEDED);
1096 if (netif_xmit_stopped(txring_txq(tx_ring)) || !netdev_xmit_more()) {
1097 writel(i, tx_ring->tail);
1102 dev_err(tx_ring->dev, "TX DMA map failed\n");
1103 tx_buffer = &tx_ring->tx_buffer_info[i];
1105 /* clear dma mappings for failed tx_buffer_info map */
1106 while (tx_buffer != first) {
1107 if (dma_unmap_len(tx_buffer, len))
1108 dma_unmap_page(tx_ring->dev,
1109 dma_unmap_addr(tx_buffer, dma),
1110 dma_unmap_len(tx_buffer, len),
1112 dma_unmap_len_set(tx_buffer, len, 0);
1115 i += tx_ring->count;
1116 tx_buffer = &tx_ring->tx_buffer_info[i];
1119 if (dma_unmap_len(tx_buffer, len))
1120 dma_unmap_single(tx_ring->dev,
1121 dma_unmap_addr(tx_buffer, dma),
1122 dma_unmap_len(tx_buffer, len),
1124 dma_unmap_len_set(tx_buffer, len, 0);
1126 dev_kfree_skb_any(tx_buffer->skb);
1127 tx_buffer->skb = NULL;
1129 tx_ring->next_to_use = i;
1134 static netdev_tx_t igc_xmit_frame_ring(struct sk_buff *skb,
1135 struct igc_ring *tx_ring)
1137 u16 count = TXD_USE_COUNT(skb_headlen(skb));
1138 __be16 protocol = vlan_get_protocol(skb);
1139 struct igc_tx_buffer *first;
1144 /* need: 1 descriptor per page * PAGE_SIZE/IGC_MAX_DATA_PER_TXD,
1145 * + 1 desc for skb_headlen/IGC_MAX_DATA_PER_TXD,
1146 * + 2 desc gap to keep tail from touching head,
1147 * + 1 desc for context descriptor,
1148 * otherwise try next time
1150 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
1151 count += TXD_USE_COUNT(skb_frag_size(
1152 &skb_shinfo(skb)->frags[f]));
1154 if (igc_maybe_stop_tx(tx_ring, count + 3)) {
1155 /* this is a hard error */
1156 return NETDEV_TX_BUSY;
1159 /* record the location of the first descriptor for this packet */
1160 first = &tx_ring->tx_buffer_info[tx_ring->next_to_use];
1162 first->bytecount = skb->len;
1163 first->gso_segs = 1;
1165 /* record initial flags and protocol */
1166 first->tx_flags = tx_flags;
1167 first->protocol = protocol;
1169 igc_tx_csum(tx_ring, first);
1171 igc_tx_map(tx_ring, first, hdr_len);
1173 return NETDEV_TX_OK;
1176 static inline struct igc_ring *igc_tx_queue_mapping(struct igc_adapter *adapter,
1177 struct sk_buff *skb)
1179 unsigned int r_idx = skb->queue_mapping;
1181 if (r_idx >= adapter->num_tx_queues)
1182 r_idx = r_idx % adapter->num_tx_queues;
1184 return adapter->tx_ring[r_idx];
1187 static netdev_tx_t igc_xmit_frame(struct sk_buff *skb,
1188 struct net_device *netdev)
1190 struct igc_adapter *adapter = netdev_priv(netdev);
1192 /* The minimum packet size with TCTL.PSP set is 17 so pad the skb
1193 * in order to meet this minimum size requirement.
1195 if (skb->len < 17) {
1196 if (skb_padto(skb, 17))
1197 return NETDEV_TX_OK;
1201 return igc_xmit_frame_ring(skb, igc_tx_queue_mapping(adapter, skb));
1204 static void igc_rx_checksum(struct igc_ring *ring,
1205 union igc_adv_rx_desc *rx_desc,
1206 struct sk_buff *skb)
1208 skb_checksum_none_assert(skb);
1210 /* Ignore Checksum bit is set */
1211 if (igc_test_staterr(rx_desc, IGC_RXD_STAT_IXSM))
1214 /* Rx checksum disabled via ethtool */
1215 if (!(ring->netdev->features & NETIF_F_RXCSUM))
1218 /* TCP/UDP checksum error bit is set */
1219 if (igc_test_staterr(rx_desc,
1220 IGC_RXDEXT_STATERR_TCPE |
1221 IGC_RXDEXT_STATERR_IPE)) {
1222 /* work around errata with sctp packets where the TCPE aka
1223 * L4E bit is set incorrectly on 64 byte (60 byte w/o crc)
1224 * packets (aka let the stack check the crc32c)
1226 if (!(skb->len == 60 &&
1227 test_bit(IGC_RING_FLAG_RX_SCTP_CSUM, &ring->flags))) {
1228 u64_stats_update_begin(&ring->rx_syncp);
1229 ring->rx_stats.csum_err++;
1230 u64_stats_update_end(&ring->rx_syncp);
1232 /* let the stack verify checksum errors */
1235 /* It must be a TCP or UDP packet with a valid checksum */
1236 if (igc_test_staterr(rx_desc, IGC_RXD_STAT_TCPCS |
1237 IGC_RXD_STAT_UDPCS))
1238 skb->ip_summed = CHECKSUM_UNNECESSARY;
1240 dev_dbg(ring->dev, "cksum success: bits %08X\n",
1241 le32_to_cpu(rx_desc->wb.upper.status_error));
1244 static inline void igc_rx_hash(struct igc_ring *ring,
1245 union igc_adv_rx_desc *rx_desc,
1246 struct sk_buff *skb)
1248 if (ring->netdev->features & NETIF_F_RXHASH)
1250 le32_to_cpu(rx_desc->wb.lower.hi_dword.rss),
1255 * igc_process_skb_fields - Populate skb header fields from Rx descriptor
1256 * @rx_ring: rx descriptor ring packet is being transacted on
1257 * @rx_desc: pointer to the EOP Rx descriptor
1258 * @skb: pointer to current skb being populated
1260 * This function checks the ring, descriptor, and packet information in
1261 * order to populate the hash, checksum, VLAN, timestamp, protocol, and
1262 * other fields within the skb.
1264 static void igc_process_skb_fields(struct igc_ring *rx_ring,
1265 union igc_adv_rx_desc *rx_desc,
1266 struct sk_buff *skb)
1268 igc_rx_hash(rx_ring, rx_desc, skb);
1270 igc_rx_checksum(rx_ring, rx_desc, skb);
1272 skb_record_rx_queue(skb, rx_ring->queue_index);
1274 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
1277 static struct igc_rx_buffer *igc_get_rx_buffer(struct igc_ring *rx_ring,
1278 const unsigned int size)
1280 struct igc_rx_buffer *rx_buffer;
1282 rx_buffer = &rx_ring->rx_buffer_info[rx_ring->next_to_clean];
1283 prefetchw(rx_buffer->page);
1285 /* we are reusing so sync this buffer for CPU use */
1286 dma_sync_single_range_for_cpu(rx_ring->dev,
1288 rx_buffer->page_offset,
1292 rx_buffer->pagecnt_bias--;
1298 * igc_add_rx_frag - Add contents of Rx buffer to sk_buff
1299 * @rx_ring: rx descriptor ring to transact packets on
1300 * @rx_buffer: buffer containing page to add
1301 * @skb: sk_buff to place the data into
1302 * @size: size of buffer to be added
1304 * This function will add the data contained in rx_buffer->page to the skb.
1306 static void igc_add_rx_frag(struct igc_ring *rx_ring,
1307 struct igc_rx_buffer *rx_buffer,
1308 struct sk_buff *skb,
1311 #if (PAGE_SIZE < 8192)
1312 unsigned int truesize = igc_rx_pg_size(rx_ring) / 2;
1314 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_buffer->page,
1315 rx_buffer->page_offset, size, truesize);
1316 rx_buffer->page_offset ^= truesize;
1318 unsigned int truesize = ring_uses_build_skb(rx_ring) ?
1319 SKB_DATA_ALIGN(IGC_SKB_PAD + size) :
1320 SKB_DATA_ALIGN(size);
1321 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_buffer->page,
1322 rx_buffer->page_offset, size, truesize);
1323 rx_buffer->page_offset += truesize;
1327 static struct sk_buff *igc_build_skb(struct igc_ring *rx_ring,
1328 struct igc_rx_buffer *rx_buffer,
1329 union igc_adv_rx_desc *rx_desc,
1332 void *va = page_address(rx_buffer->page) + rx_buffer->page_offset;
1333 #if (PAGE_SIZE < 8192)
1334 unsigned int truesize = igc_rx_pg_size(rx_ring) / 2;
1336 unsigned int truesize = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
1337 SKB_DATA_ALIGN(IGC_SKB_PAD + size);
1339 struct sk_buff *skb;
1341 /* prefetch first cache line of first page */
1343 #if L1_CACHE_BYTES < 128
1344 prefetch(va + L1_CACHE_BYTES);
1347 /* build an skb around the page buffer */
1348 skb = build_skb(va - IGC_SKB_PAD, truesize);
1352 /* update pointers within the skb to store the data */
1353 skb_reserve(skb, IGC_SKB_PAD);
1354 __skb_put(skb, size);
1356 /* update buffer offset */
1357 #if (PAGE_SIZE < 8192)
1358 rx_buffer->page_offset ^= truesize;
1360 rx_buffer->page_offset += truesize;
1366 static struct sk_buff *igc_construct_skb(struct igc_ring *rx_ring,
1367 struct igc_rx_buffer *rx_buffer,
1368 union igc_adv_rx_desc *rx_desc,
1371 void *va = page_address(rx_buffer->page) + rx_buffer->page_offset;
1372 #if (PAGE_SIZE < 8192)
1373 unsigned int truesize = igc_rx_pg_size(rx_ring) / 2;
1375 unsigned int truesize = SKB_DATA_ALIGN(size);
1377 unsigned int headlen;
1378 struct sk_buff *skb;
1380 /* prefetch first cache line of first page */
1382 #if L1_CACHE_BYTES < 128
1383 prefetch(va + L1_CACHE_BYTES);
1386 /* allocate a skb to store the frags */
1387 skb = napi_alloc_skb(&rx_ring->q_vector->napi, IGC_RX_HDR_LEN);
1391 /* Determine available headroom for copy */
1393 if (headlen > IGC_RX_HDR_LEN)
1394 headlen = eth_get_headlen(skb->dev, va, IGC_RX_HDR_LEN);
1396 /* align pull length to size of long to optimize memcpy performance */
1397 memcpy(__skb_put(skb, headlen), va, ALIGN(headlen, sizeof(long)));
1399 /* update all of the pointers */
1402 skb_add_rx_frag(skb, 0, rx_buffer->page,
1403 (va + headlen) - page_address(rx_buffer->page),
1405 #if (PAGE_SIZE < 8192)
1406 rx_buffer->page_offset ^= truesize;
1408 rx_buffer->page_offset += truesize;
1411 rx_buffer->pagecnt_bias++;
1418 * igc_reuse_rx_page - page flip buffer and store it back on the ring
1419 * @rx_ring: rx descriptor ring to store buffers on
1420 * @old_buff: donor buffer to have page reused
1422 * Synchronizes page for reuse by the adapter
1424 static void igc_reuse_rx_page(struct igc_ring *rx_ring,
1425 struct igc_rx_buffer *old_buff)
1427 u16 nta = rx_ring->next_to_alloc;
1428 struct igc_rx_buffer *new_buff;
1430 new_buff = &rx_ring->rx_buffer_info[nta];
1432 /* update, and store next to alloc */
1434 rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
1436 /* Transfer page from old buffer to new buffer.
1437 * Move each member individually to avoid possible store
1438 * forwarding stalls.
1440 new_buff->dma = old_buff->dma;
1441 new_buff->page = old_buff->page;
1442 new_buff->page_offset = old_buff->page_offset;
1443 new_buff->pagecnt_bias = old_buff->pagecnt_bias;
1446 static inline bool igc_page_is_reserved(struct page *page)
1448 return (page_to_nid(page) != numa_mem_id()) || page_is_pfmemalloc(page);
1451 static bool igc_can_reuse_rx_page(struct igc_rx_buffer *rx_buffer)
1453 unsigned int pagecnt_bias = rx_buffer->pagecnt_bias;
1454 struct page *page = rx_buffer->page;
1456 /* avoid re-using remote pages */
1457 if (unlikely(igc_page_is_reserved(page)))
1460 #if (PAGE_SIZE < 8192)
1461 /* if we are only owner of page we can reuse it */
1462 if (unlikely((page_ref_count(page) - pagecnt_bias) > 1))
1465 #define IGC_LAST_OFFSET \
1466 (SKB_WITH_OVERHEAD(PAGE_SIZE) - IGC_RXBUFFER_2048)
1468 if (rx_buffer->page_offset > IGC_LAST_OFFSET)
1472 /* If we have drained the page fragment pool we need to update
1473 * the pagecnt_bias and page count so that we fully restock the
1474 * number of references the driver holds.
1476 if (unlikely(!pagecnt_bias)) {
1477 page_ref_add(page, USHRT_MAX);
1478 rx_buffer->pagecnt_bias = USHRT_MAX;
1485 * igc_is_non_eop - process handling of non-EOP buffers
1486 * @rx_ring: Rx ring being processed
1487 * @rx_desc: Rx descriptor for current buffer
1488 * @skb: current socket buffer containing buffer in progress
1490 * This function updates next to clean. If the buffer is an EOP buffer
1491 * this function exits returning false, otherwise it will place the
1492 * sk_buff in the next buffer to be chained and return true indicating
1493 * that this is in fact a non-EOP buffer.
1495 static bool igc_is_non_eop(struct igc_ring *rx_ring,
1496 union igc_adv_rx_desc *rx_desc)
1498 u32 ntc = rx_ring->next_to_clean + 1;
1500 /* fetch, update, and store next to clean */
1501 ntc = (ntc < rx_ring->count) ? ntc : 0;
1502 rx_ring->next_to_clean = ntc;
1504 prefetch(IGC_RX_DESC(rx_ring, ntc));
1506 if (likely(igc_test_staterr(rx_desc, IGC_RXD_STAT_EOP)))
1513 * igc_cleanup_headers - Correct corrupted or empty headers
1514 * @rx_ring: rx descriptor ring packet is being transacted on
1515 * @rx_desc: pointer to the EOP Rx descriptor
1516 * @skb: pointer to current skb being fixed
1518 * Address the case where we are pulling data in on pages only
1519 * and as such no data is present in the skb header.
1521 * In addition if skb is not at least 60 bytes we need to pad it so that
1522 * it is large enough to qualify as a valid Ethernet frame.
1524 * Returns true if an error was encountered and skb was freed.
1526 static bool igc_cleanup_headers(struct igc_ring *rx_ring,
1527 union igc_adv_rx_desc *rx_desc,
1528 struct sk_buff *skb)
1530 if (unlikely((igc_test_staterr(rx_desc,
1531 IGC_RXDEXT_ERR_FRAME_ERR_MASK)))) {
1532 struct net_device *netdev = rx_ring->netdev;
1534 if (!(netdev->features & NETIF_F_RXALL)) {
1535 dev_kfree_skb_any(skb);
1540 /* if eth_skb_pad returns an error the skb was freed */
1541 if (eth_skb_pad(skb))
1547 static void igc_put_rx_buffer(struct igc_ring *rx_ring,
1548 struct igc_rx_buffer *rx_buffer)
1550 if (igc_can_reuse_rx_page(rx_buffer)) {
1551 /* hand second half of page back to the ring */
1552 igc_reuse_rx_page(rx_ring, rx_buffer);
1554 /* We are not reusing the buffer so unmap it and free
1555 * any references we are holding to it
1557 dma_unmap_page_attrs(rx_ring->dev, rx_buffer->dma,
1558 igc_rx_pg_size(rx_ring), DMA_FROM_DEVICE,
1560 __page_frag_cache_drain(rx_buffer->page,
1561 rx_buffer->pagecnt_bias);
1564 /* clear contents of rx_buffer */
1565 rx_buffer->page = NULL;
1569 * igc_alloc_rx_buffers - Replace used receive buffers; packet split
1570 * @adapter: address of board private structure
1572 static void igc_alloc_rx_buffers(struct igc_ring *rx_ring, u16 cleaned_count)
1574 union igc_adv_rx_desc *rx_desc;
1575 u16 i = rx_ring->next_to_use;
1576 struct igc_rx_buffer *bi;
1583 rx_desc = IGC_RX_DESC(rx_ring, i);
1584 bi = &rx_ring->rx_buffer_info[i];
1585 i -= rx_ring->count;
1587 bufsz = igc_rx_bufsz(rx_ring);
1590 if (!igc_alloc_mapped_page(rx_ring, bi))
1593 /* sync the buffer for use by the device */
1594 dma_sync_single_range_for_device(rx_ring->dev, bi->dma,
1595 bi->page_offset, bufsz,
1598 /* Refresh the desc even if buffer_addrs didn't change
1599 * because each write-back erases this info.
1601 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
1607 rx_desc = IGC_RX_DESC(rx_ring, 0);
1608 bi = rx_ring->rx_buffer_info;
1609 i -= rx_ring->count;
1612 /* clear the length for the next_to_use descriptor */
1613 rx_desc->wb.upper.length = 0;
1616 } while (cleaned_count);
1618 i += rx_ring->count;
1620 if (rx_ring->next_to_use != i) {
1621 /* record the next descriptor to use */
1622 rx_ring->next_to_use = i;
1624 /* update next to alloc since we have filled the ring */
1625 rx_ring->next_to_alloc = i;
1627 /* Force memory writes to complete before letting h/w
1628 * know there are new descriptors to fetch. (Only
1629 * applicable for weak-ordered memory model archs,
1633 writel(i, rx_ring->tail);
1637 static int igc_clean_rx_irq(struct igc_q_vector *q_vector, const int budget)
1639 unsigned int total_bytes = 0, total_packets = 0;
1640 struct igc_ring *rx_ring = q_vector->rx.ring;
1641 struct sk_buff *skb = rx_ring->skb;
1642 u16 cleaned_count = igc_desc_unused(rx_ring);
1644 while (likely(total_packets < budget)) {
1645 union igc_adv_rx_desc *rx_desc;
1646 struct igc_rx_buffer *rx_buffer;
1649 /* return some buffers to hardware, one at a time is too slow */
1650 if (cleaned_count >= IGC_RX_BUFFER_WRITE) {
1651 igc_alloc_rx_buffers(rx_ring, cleaned_count);
1655 rx_desc = IGC_RX_DESC(rx_ring, rx_ring->next_to_clean);
1656 size = le16_to_cpu(rx_desc->wb.upper.length);
1660 /* This memory barrier is needed to keep us from reading
1661 * any other fields out of the rx_desc until we know the
1662 * descriptor has been written back
1666 rx_buffer = igc_get_rx_buffer(rx_ring, size);
1668 /* retrieve a buffer from the ring */
1670 igc_add_rx_frag(rx_ring, rx_buffer, skb, size);
1671 else if (ring_uses_build_skb(rx_ring))
1672 skb = igc_build_skb(rx_ring, rx_buffer, rx_desc, size);
1674 skb = igc_construct_skb(rx_ring, rx_buffer,
1677 /* exit if we failed to retrieve a buffer */
1679 rx_ring->rx_stats.alloc_failed++;
1680 rx_buffer->pagecnt_bias++;
1684 igc_put_rx_buffer(rx_ring, rx_buffer);
1687 /* fetch next buffer in frame if non-eop */
1688 if (igc_is_non_eop(rx_ring, rx_desc))
1691 /* verify the packet layout is correct */
1692 if (igc_cleanup_headers(rx_ring, rx_desc, skb)) {
1697 /* probably a little skewed due to removing CRC */
1698 total_bytes += skb->len;
1700 /* populate checksum, timestamp, VLAN, and protocol */
1701 igc_process_skb_fields(rx_ring, rx_desc, skb);
1703 napi_gro_receive(&q_vector->napi, skb);
1705 /* reset skb pointer */
1708 /* update budget accounting */
1712 /* place incomplete frames back on ring for completion */
1715 u64_stats_update_begin(&rx_ring->rx_syncp);
1716 rx_ring->rx_stats.packets += total_packets;
1717 rx_ring->rx_stats.bytes += total_bytes;
1718 u64_stats_update_end(&rx_ring->rx_syncp);
1719 q_vector->rx.total_packets += total_packets;
1720 q_vector->rx.total_bytes += total_bytes;
1723 igc_alloc_rx_buffers(rx_ring, cleaned_count);
1725 return total_packets;
1728 static inline unsigned int igc_rx_offset(struct igc_ring *rx_ring)
1730 return ring_uses_build_skb(rx_ring) ? IGC_SKB_PAD : 0;
1733 static bool igc_alloc_mapped_page(struct igc_ring *rx_ring,
1734 struct igc_rx_buffer *bi)
1736 struct page *page = bi->page;
1739 /* since we are recycling buffers we should seldom need to alloc */
1743 /* alloc new page for storage */
1744 page = dev_alloc_pages(igc_rx_pg_order(rx_ring));
1745 if (unlikely(!page)) {
1746 rx_ring->rx_stats.alloc_failed++;
1750 /* map page for use */
1751 dma = dma_map_page_attrs(rx_ring->dev, page, 0,
1752 igc_rx_pg_size(rx_ring),
1756 /* if mapping failed free memory back to system since
1757 * there isn't much point in holding memory we can't use
1759 if (dma_mapping_error(rx_ring->dev, dma)) {
1762 rx_ring->rx_stats.alloc_failed++;
1768 bi->page_offset = igc_rx_offset(rx_ring);
1769 bi->pagecnt_bias = 1;
1775 * igc_clean_tx_irq - Reclaim resources after transmit completes
1776 * @q_vector: pointer to q_vector containing needed info
1777 * @napi_budget: Used to determine if we are in netpoll
1779 * returns true if ring is completely cleaned
1781 static bool igc_clean_tx_irq(struct igc_q_vector *q_vector, int napi_budget)
1783 struct igc_adapter *adapter = q_vector->adapter;
1784 unsigned int total_bytes = 0, total_packets = 0;
1785 unsigned int budget = q_vector->tx.work_limit;
1786 struct igc_ring *tx_ring = q_vector->tx.ring;
1787 unsigned int i = tx_ring->next_to_clean;
1788 struct igc_tx_buffer *tx_buffer;
1789 union igc_adv_tx_desc *tx_desc;
1791 if (test_bit(__IGC_DOWN, &adapter->state))
1794 tx_buffer = &tx_ring->tx_buffer_info[i];
1795 tx_desc = IGC_TX_DESC(tx_ring, i);
1796 i -= tx_ring->count;
1799 union igc_adv_tx_desc *eop_desc = tx_buffer->next_to_watch;
1801 /* if next_to_watch is not set then there is no work pending */
1805 /* prevent any other reads prior to eop_desc */
1808 /* if DD is not set pending work has not been completed */
1809 if (!(eop_desc->wb.status & cpu_to_le32(IGC_TXD_STAT_DD)))
1812 /* clear next_to_watch to prevent false hangs */
1813 tx_buffer->next_to_watch = NULL;
1815 /* update the statistics for this packet */
1816 total_bytes += tx_buffer->bytecount;
1817 total_packets += tx_buffer->gso_segs;
1820 napi_consume_skb(tx_buffer->skb, napi_budget);
1822 /* unmap skb header data */
1823 dma_unmap_single(tx_ring->dev,
1824 dma_unmap_addr(tx_buffer, dma),
1825 dma_unmap_len(tx_buffer, len),
1828 /* clear tx_buffer data */
1829 dma_unmap_len_set(tx_buffer, len, 0);
1831 /* clear last DMA location and unmap remaining buffers */
1832 while (tx_desc != eop_desc) {
1837 i -= tx_ring->count;
1838 tx_buffer = tx_ring->tx_buffer_info;
1839 tx_desc = IGC_TX_DESC(tx_ring, 0);
1842 /* unmap any remaining paged data */
1843 if (dma_unmap_len(tx_buffer, len)) {
1844 dma_unmap_page(tx_ring->dev,
1845 dma_unmap_addr(tx_buffer, dma),
1846 dma_unmap_len(tx_buffer, len),
1848 dma_unmap_len_set(tx_buffer, len, 0);
1852 /* move us one more past the eop_desc for start of next pkt */
1857 i -= tx_ring->count;
1858 tx_buffer = tx_ring->tx_buffer_info;
1859 tx_desc = IGC_TX_DESC(tx_ring, 0);
1862 /* issue prefetch for next Tx descriptor */
1865 /* update budget accounting */
1867 } while (likely(budget));
1869 netdev_tx_completed_queue(txring_txq(tx_ring),
1870 total_packets, total_bytes);
1872 i += tx_ring->count;
1873 tx_ring->next_to_clean = i;
1874 u64_stats_update_begin(&tx_ring->tx_syncp);
1875 tx_ring->tx_stats.bytes += total_bytes;
1876 tx_ring->tx_stats.packets += total_packets;
1877 u64_stats_update_end(&tx_ring->tx_syncp);
1878 q_vector->tx.total_bytes += total_bytes;
1879 q_vector->tx.total_packets += total_packets;
1881 if (test_bit(IGC_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags)) {
1882 struct igc_hw *hw = &adapter->hw;
1884 /* Detect a transmit hang in hardware, this serializes the
1885 * check with the clearing of time_stamp and movement of i
1887 clear_bit(IGC_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags);
1888 if (tx_buffer->next_to_watch &&
1889 time_after(jiffies, tx_buffer->time_stamp +
1890 (adapter->tx_timeout_factor * HZ)) &&
1891 !(rd32(IGC_STATUS) & IGC_STATUS_TXOFF)) {
1892 /* detected Tx unit hang */
1893 dev_err(tx_ring->dev,
1894 "Detected Tx Unit Hang\n"
1898 " next_to_use <%x>\n"
1899 " next_to_clean <%x>\n"
1900 "buffer_info[next_to_clean]\n"
1901 " time_stamp <%lx>\n"
1902 " next_to_watch <%p>\n"
1904 " desc.status <%x>\n",
1905 tx_ring->queue_index,
1906 rd32(IGC_TDH(tx_ring->reg_idx)),
1907 readl(tx_ring->tail),
1908 tx_ring->next_to_use,
1909 tx_ring->next_to_clean,
1910 tx_buffer->time_stamp,
1911 tx_buffer->next_to_watch,
1913 tx_buffer->next_to_watch->wb.status);
1914 netif_stop_subqueue(tx_ring->netdev,
1915 tx_ring->queue_index);
1917 /* we are about to reset, no point in enabling stuff */
1922 #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
1923 if (unlikely(total_packets &&
1924 netif_carrier_ok(tx_ring->netdev) &&
1925 igc_desc_unused(tx_ring) >= TX_WAKE_THRESHOLD)) {
1926 /* Make sure that anybody stopping the queue after this
1927 * sees the new next_to_clean.
1930 if (__netif_subqueue_stopped(tx_ring->netdev,
1931 tx_ring->queue_index) &&
1932 !(test_bit(__IGC_DOWN, &adapter->state))) {
1933 netif_wake_subqueue(tx_ring->netdev,
1934 tx_ring->queue_index);
1936 u64_stats_update_begin(&tx_ring->tx_syncp);
1937 tx_ring->tx_stats.restart_queue++;
1938 u64_stats_update_end(&tx_ring->tx_syncp);
1946 * igc_up - Open the interface and prepare it to handle traffic
1947 * @adapter: board private structure
1949 void igc_up(struct igc_adapter *adapter)
1951 struct igc_hw *hw = &adapter->hw;
1954 /* hardware has been reset, we need to reload some things */
1955 igc_configure(adapter);
1957 clear_bit(__IGC_DOWN, &adapter->state);
1959 for (i = 0; i < adapter->num_q_vectors; i++)
1960 napi_enable(&adapter->q_vector[i]->napi);
1962 if (adapter->msix_entries)
1963 igc_configure_msix(adapter);
1965 igc_assign_vector(adapter->q_vector[0], 0);
1967 /* Clear any pending interrupts. */
1969 igc_irq_enable(adapter);
1971 netif_tx_start_all_queues(adapter->netdev);
1973 /* start the watchdog. */
1974 hw->mac.get_link_status = 1;
1975 schedule_work(&adapter->watchdog_task);
1979 * igc_update_stats - Update the board statistics counters
1980 * @adapter: board private structure
1982 void igc_update_stats(struct igc_adapter *adapter)
1984 struct rtnl_link_stats64 *net_stats = &adapter->stats64;
1985 struct pci_dev *pdev = adapter->pdev;
1986 struct igc_hw *hw = &adapter->hw;
1987 u64 _bytes, _packets;
1993 /* Prevent stats update while adapter is being reset, or if the pci
1994 * connection is down.
1996 if (adapter->link_speed == 0)
1998 if (pci_channel_offline(pdev))
2005 for (i = 0; i < adapter->num_rx_queues; i++) {
2006 struct igc_ring *ring = adapter->rx_ring[i];
2007 u32 rqdpc = rd32(IGC_RQDPC(i));
2009 if (hw->mac.type >= igc_i225)
2010 wr32(IGC_RQDPC(i), 0);
2013 ring->rx_stats.drops += rqdpc;
2014 net_stats->rx_fifo_errors += rqdpc;
2018 start = u64_stats_fetch_begin_irq(&ring->rx_syncp);
2019 _bytes = ring->rx_stats.bytes;
2020 _packets = ring->rx_stats.packets;
2021 } while (u64_stats_fetch_retry_irq(&ring->rx_syncp, start));
2023 packets += _packets;
2026 net_stats->rx_bytes = bytes;
2027 net_stats->rx_packets = packets;
2031 for (i = 0; i < adapter->num_tx_queues; i++) {
2032 struct igc_ring *ring = adapter->tx_ring[i];
2035 start = u64_stats_fetch_begin_irq(&ring->tx_syncp);
2036 _bytes = ring->tx_stats.bytes;
2037 _packets = ring->tx_stats.packets;
2038 } while (u64_stats_fetch_retry_irq(&ring->tx_syncp, start));
2040 packets += _packets;
2042 net_stats->tx_bytes = bytes;
2043 net_stats->tx_packets = packets;
2046 /* read stats registers */
2047 adapter->stats.crcerrs += rd32(IGC_CRCERRS);
2048 adapter->stats.gprc += rd32(IGC_GPRC);
2049 adapter->stats.gorc += rd32(IGC_GORCL);
2050 rd32(IGC_GORCH); /* clear GORCL */
2051 adapter->stats.bprc += rd32(IGC_BPRC);
2052 adapter->stats.mprc += rd32(IGC_MPRC);
2053 adapter->stats.roc += rd32(IGC_ROC);
2055 adapter->stats.prc64 += rd32(IGC_PRC64);
2056 adapter->stats.prc127 += rd32(IGC_PRC127);
2057 adapter->stats.prc255 += rd32(IGC_PRC255);
2058 adapter->stats.prc511 += rd32(IGC_PRC511);
2059 adapter->stats.prc1023 += rd32(IGC_PRC1023);
2060 adapter->stats.prc1522 += rd32(IGC_PRC1522);
2061 adapter->stats.symerrs += rd32(IGC_SYMERRS);
2062 adapter->stats.sec += rd32(IGC_SEC);
2064 mpc = rd32(IGC_MPC);
2065 adapter->stats.mpc += mpc;
2066 net_stats->rx_fifo_errors += mpc;
2067 adapter->stats.scc += rd32(IGC_SCC);
2068 adapter->stats.ecol += rd32(IGC_ECOL);
2069 adapter->stats.mcc += rd32(IGC_MCC);
2070 adapter->stats.latecol += rd32(IGC_LATECOL);
2071 adapter->stats.dc += rd32(IGC_DC);
2072 adapter->stats.rlec += rd32(IGC_RLEC);
2073 adapter->stats.xonrxc += rd32(IGC_XONRXC);
2074 adapter->stats.xontxc += rd32(IGC_XONTXC);
2075 adapter->stats.xoffrxc += rd32(IGC_XOFFRXC);
2076 adapter->stats.xofftxc += rd32(IGC_XOFFTXC);
2077 adapter->stats.fcruc += rd32(IGC_FCRUC);
2078 adapter->stats.gptc += rd32(IGC_GPTC);
2079 adapter->stats.gotc += rd32(IGC_GOTCL);
2080 rd32(IGC_GOTCH); /* clear GOTCL */
2081 adapter->stats.rnbc += rd32(IGC_RNBC);
2082 adapter->stats.ruc += rd32(IGC_RUC);
2083 adapter->stats.rfc += rd32(IGC_RFC);
2084 adapter->stats.rjc += rd32(IGC_RJC);
2085 adapter->stats.tor += rd32(IGC_TORH);
2086 adapter->stats.tot += rd32(IGC_TOTH);
2087 adapter->stats.tpr += rd32(IGC_TPR);
2089 adapter->stats.ptc64 += rd32(IGC_PTC64);
2090 adapter->stats.ptc127 += rd32(IGC_PTC127);
2091 adapter->stats.ptc255 += rd32(IGC_PTC255);
2092 adapter->stats.ptc511 += rd32(IGC_PTC511);
2093 adapter->stats.ptc1023 += rd32(IGC_PTC1023);
2094 adapter->stats.ptc1522 += rd32(IGC_PTC1522);
2096 adapter->stats.mptc += rd32(IGC_MPTC);
2097 adapter->stats.bptc += rd32(IGC_BPTC);
2099 adapter->stats.tpt += rd32(IGC_TPT);
2100 adapter->stats.colc += rd32(IGC_COLC);
2102 adapter->stats.algnerrc += rd32(IGC_ALGNERRC);
2104 adapter->stats.tsctc += rd32(IGC_TSCTC);
2105 adapter->stats.tsctfc += rd32(IGC_TSCTFC);
2107 adapter->stats.iac += rd32(IGC_IAC);
2108 adapter->stats.icrxoc += rd32(IGC_ICRXOC);
2109 adapter->stats.icrxptc += rd32(IGC_ICRXPTC);
2110 adapter->stats.icrxatc += rd32(IGC_ICRXATC);
2111 adapter->stats.ictxptc += rd32(IGC_ICTXPTC);
2112 adapter->stats.ictxatc += rd32(IGC_ICTXATC);
2113 adapter->stats.ictxqec += rd32(IGC_ICTXQEC);
2114 adapter->stats.ictxqmtc += rd32(IGC_ICTXQMTC);
2115 adapter->stats.icrxdmtc += rd32(IGC_ICRXDMTC);
2117 /* Fill out the OS statistics structure */
2118 net_stats->multicast = adapter->stats.mprc;
2119 net_stats->collisions = adapter->stats.colc;
2123 /* RLEC on some newer hardware can be incorrect so build
2124 * our own version based on RUC and ROC
2126 net_stats->rx_errors = adapter->stats.rxerrc +
2127 adapter->stats.crcerrs + adapter->stats.algnerrc +
2128 adapter->stats.ruc + adapter->stats.roc +
2129 adapter->stats.cexterr;
2130 net_stats->rx_length_errors = adapter->stats.ruc +
2132 net_stats->rx_crc_errors = adapter->stats.crcerrs;
2133 net_stats->rx_frame_errors = adapter->stats.algnerrc;
2134 net_stats->rx_missed_errors = adapter->stats.mpc;
2137 net_stats->tx_errors = adapter->stats.ecol +
2138 adapter->stats.latecol;
2139 net_stats->tx_aborted_errors = adapter->stats.ecol;
2140 net_stats->tx_window_errors = adapter->stats.latecol;
2141 net_stats->tx_carrier_errors = adapter->stats.tncrs;
2143 /* Tx Dropped needs to be maintained elsewhere */
2145 /* Management Stats */
2146 adapter->stats.mgptc += rd32(IGC_MGTPTC);
2147 adapter->stats.mgprc += rd32(IGC_MGTPRC);
2148 adapter->stats.mgpdc += rd32(IGC_MGTPDC);
2151 static void igc_nfc_filter_exit(struct igc_adapter *adapter)
2153 struct igc_nfc_filter *rule;
2155 spin_lock(&adapter->nfc_lock);
2157 hlist_for_each_entry(rule, &adapter->nfc_filter_list, nfc_node)
2158 igc_erase_filter(adapter, rule);
2160 hlist_for_each_entry(rule, &adapter->cls_flower_list, nfc_node)
2161 igc_erase_filter(adapter, rule);
2163 spin_unlock(&adapter->nfc_lock);
2166 static void igc_nfc_filter_restore(struct igc_adapter *adapter)
2168 struct igc_nfc_filter *rule;
2170 spin_lock(&adapter->nfc_lock);
2172 hlist_for_each_entry(rule, &adapter->nfc_filter_list, nfc_node)
2173 igc_add_filter(adapter, rule);
2175 spin_unlock(&adapter->nfc_lock);
2179 * igc_down - Close the interface
2180 * @adapter: board private structure
2182 void igc_down(struct igc_adapter *adapter)
2184 struct net_device *netdev = adapter->netdev;
2185 struct igc_hw *hw = &adapter->hw;
2189 set_bit(__IGC_DOWN, &adapter->state);
2191 /* disable receives in the hardware */
2192 rctl = rd32(IGC_RCTL);
2193 wr32(IGC_RCTL, rctl & ~IGC_RCTL_EN);
2194 /* flush and sleep below */
2196 igc_nfc_filter_exit(adapter);
2198 /* set trans_start so we don't get spurious watchdogs during reset */
2199 netif_trans_update(netdev);
2201 netif_carrier_off(netdev);
2202 netif_tx_stop_all_queues(netdev);
2204 /* disable transmits in the hardware */
2205 tctl = rd32(IGC_TCTL);
2206 tctl &= ~IGC_TCTL_EN;
2207 wr32(IGC_TCTL, tctl);
2208 /* flush both disables and wait for them to finish */
2210 usleep_range(10000, 20000);
2212 igc_irq_disable(adapter);
2214 adapter->flags &= ~IGC_FLAG_NEED_LINK_UPDATE;
2216 for (i = 0; i < adapter->num_q_vectors; i++) {
2217 if (adapter->q_vector[i]) {
2218 napi_synchronize(&adapter->q_vector[i]->napi);
2219 napi_disable(&adapter->q_vector[i]->napi);
2223 del_timer_sync(&adapter->watchdog_timer);
2224 del_timer_sync(&adapter->phy_info_timer);
2226 /* record the stats before reset*/
2227 spin_lock(&adapter->stats64_lock);
2228 igc_update_stats(adapter);
2229 spin_unlock(&adapter->stats64_lock);
2231 adapter->link_speed = 0;
2232 adapter->link_duplex = 0;
2234 if (!pci_channel_offline(adapter->pdev))
2237 /* clear VLAN promisc flag so VFTA will be updated if necessary */
2238 adapter->flags &= ~IGC_FLAG_VLAN_PROMISC;
2240 igc_clean_all_tx_rings(adapter);
2241 igc_clean_all_rx_rings(adapter);
2244 void igc_reinit_locked(struct igc_adapter *adapter)
2246 WARN_ON(in_interrupt());
2247 while (test_and_set_bit(__IGC_RESETTING, &adapter->state))
2248 usleep_range(1000, 2000);
2251 clear_bit(__IGC_RESETTING, &adapter->state);
2254 static void igc_reset_task(struct work_struct *work)
2256 struct igc_adapter *adapter;
2258 adapter = container_of(work, struct igc_adapter, reset_task);
2260 netdev_err(adapter->netdev, "Reset adapter\n");
2261 igc_reinit_locked(adapter);
2265 * igc_change_mtu - Change the Maximum Transfer Unit
2266 * @netdev: network interface device structure
2267 * @new_mtu: new value for maximum frame size
2269 * Returns 0 on success, negative on failure
2271 static int igc_change_mtu(struct net_device *netdev, int new_mtu)
2273 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2274 struct igc_adapter *adapter = netdev_priv(netdev);
2275 struct pci_dev *pdev = adapter->pdev;
2277 /* adjust max frame to be at least the size of a standard frame */
2278 if (max_frame < (ETH_FRAME_LEN + ETH_FCS_LEN))
2279 max_frame = ETH_FRAME_LEN + ETH_FCS_LEN;
2281 while (test_and_set_bit(__IGC_RESETTING, &adapter->state))
2282 usleep_range(1000, 2000);
2284 /* igc_down has a dependency on max_frame_size */
2285 adapter->max_frame_size = max_frame;
2287 if (netif_running(netdev))
2290 dev_info(&pdev->dev, "changing MTU from %d to %d\n",
2291 netdev->mtu, new_mtu);
2292 netdev->mtu = new_mtu;
2294 if (netif_running(netdev))
2299 clear_bit(__IGC_RESETTING, &adapter->state);
2305 * igc_get_stats - Get System Network Statistics
2306 * @netdev: network interface device structure
2308 * Returns the address of the device statistics structure.
2309 * The statistics are updated here and also from the timer callback.
2311 static struct net_device_stats *igc_get_stats(struct net_device *netdev)
2313 struct igc_adapter *adapter = netdev_priv(netdev);
2315 if (!test_bit(__IGC_RESETTING, &adapter->state))
2316 igc_update_stats(adapter);
2318 /* only return the current stats */
2319 return &netdev->stats;
2322 static netdev_features_t igc_fix_features(struct net_device *netdev,
2323 netdev_features_t features)
2325 /* Since there is no support for separate Rx/Tx vlan accel
2326 * enable/disable make sure Tx flag is always in same state as Rx.
2328 if (features & NETIF_F_HW_VLAN_CTAG_RX)
2329 features |= NETIF_F_HW_VLAN_CTAG_TX;
2331 features &= ~NETIF_F_HW_VLAN_CTAG_TX;
2336 static int igc_set_features(struct net_device *netdev,
2337 netdev_features_t features)
2339 netdev_features_t changed = netdev->features ^ features;
2340 struct igc_adapter *adapter = netdev_priv(netdev);
2342 /* Add VLAN support */
2343 if (!(changed & (NETIF_F_RXALL | NETIF_F_NTUPLE)))
2346 if (!(features & NETIF_F_NTUPLE)) {
2347 struct hlist_node *node2;
2348 struct igc_nfc_filter *rule;
2350 spin_lock(&adapter->nfc_lock);
2351 hlist_for_each_entry_safe(rule, node2,
2352 &adapter->nfc_filter_list, nfc_node) {
2353 igc_erase_filter(adapter, rule);
2354 hlist_del(&rule->nfc_node);
2357 spin_unlock(&adapter->nfc_lock);
2358 adapter->nfc_filter_count = 0;
2361 netdev->features = features;
2363 if (netif_running(netdev))
2364 igc_reinit_locked(adapter);
2371 static netdev_features_t
2372 igc_features_check(struct sk_buff *skb, struct net_device *dev,
2373 netdev_features_t features)
2375 unsigned int network_hdr_len, mac_hdr_len;
2377 /* Make certain the headers can be described by a context descriptor */
2378 mac_hdr_len = skb_network_header(skb) - skb->data;
2379 if (unlikely(mac_hdr_len > IGC_MAX_MAC_HDR_LEN))
2380 return features & ~(NETIF_F_HW_CSUM |
2382 NETIF_F_HW_VLAN_CTAG_TX |
2386 network_hdr_len = skb_checksum_start(skb) - skb_network_header(skb);
2387 if (unlikely(network_hdr_len > IGC_MAX_NETWORK_HDR_LEN))
2388 return features & ~(NETIF_F_HW_CSUM |
2393 /* We can only support IPv4 TSO in tunnels if we can mangle the
2394 * inner IP ID field, so strip TSO if MANGLEID is not supported.
2396 if (skb->encapsulation && !(features & NETIF_F_TSO_MANGLEID))
2397 features &= ~NETIF_F_TSO;
2403 * igc_configure - configure the hardware for RX and TX
2404 * @adapter: private board structure
2406 static void igc_configure(struct igc_adapter *adapter)
2408 struct net_device *netdev = adapter->netdev;
2411 igc_get_hw_control(adapter);
2412 igc_set_rx_mode(netdev);
2414 igc_setup_tctl(adapter);
2415 igc_setup_mrqc(adapter);
2416 igc_setup_rctl(adapter);
2418 igc_nfc_filter_restore(adapter);
2419 igc_configure_tx(adapter);
2420 igc_configure_rx(adapter);
2422 igc_rx_fifo_flush_base(&adapter->hw);
2424 /* call igc_desc_unused which always leaves
2425 * at least 1 descriptor unused to make sure
2426 * next_to_use != next_to_clean
2428 for (i = 0; i < adapter->num_rx_queues; i++) {
2429 struct igc_ring *ring = adapter->rx_ring[i];
2431 igc_alloc_rx_buffers(ring, igc_desc_unused(ring));
2436 * igc_rar_set_index - Sync RAL[index] and RAH[index] registers with MAC table
2437 * @adapter: address of board private structure
2438 * @index: Index of the RAR entry which need to be synced with MAC table
2440 static void igc_rar_set_index(struct igc_adapter *adapter, u32 index)
2442 u8 *addr = adapter->mac_table[index].addr;
2443 struct igc_hw *hw = &adapter->hw;
2444 u32 rar_low, rar_high;
2446 /* HW expects these to be in network order when they are plugged
2447 * into the registers which are little endian. In order to guarantee
2448 * that ordering we need to do an leXX_to_cpup here in order to be
2449 * ready for the byteswap that occurs with writel
2451 rar_low = le32_to_cpup((__le32 *)(addr));
2452 rar_high = le16_to_cpup((__le16 *)(addr + 4));
2454 /* Indicate to hardware the Address is Valid. */
2455 if (adapter->mac_table[index].state & IGC_MAC_STATE_IN_USE) {
2456 if (is_valid_ether_addr(addr))
2457 rar_high |= IGC_RAH_AV;
2459 rar_high |= IGC_RAH_POOL_1 <<
2460 adapter->mac_table[index].queue;
2463 wr32(IGC_RAL(index), rar_low);
2465 wr32(IGC_RAH(index), rar_high);
2469 /* Set default MAC address for the PF in the first RAR entry */
2470 static void igc_set_default_mac_filter(struct igc_adapter *adapter)
2472 struct igc_mac_addr *mac_table = &adapter->mac_table[0];
2474 ether_addr_copy(mac_table->addr, adapter->hw.mac.addr);
2475 mac_table->state = IGC_MAC_STATE_DEFAULT | IGC_MAC_STATE_IN_USE;
2477 igc_rar_set_index(adapter, 0);
2480 /* If the filter to be added and an already existing filter express
2481 * the same address and address type, it should be possible to only
2482 * override the other configurations, for example the queue to steer
2485 static bool igc_mac_entry_can_be_used(const struct igc_mac_addr *entry,
2486 const u8 *addr, const u8 flags)
2488 if (!(entry->state & IGC_MAC_STATE_IN_USE))
2491 if ((entry->state & IGC_MAC_STATE_SRC_ADDR) !=
2492 (flags & IGC_MAC_STATE_SRC_ADDR))
2495 if (!ether_addr_equal(addr, entry->addr))
2501 /* Add a MAC filter for 'addr' directing matching traffic to 'queue',
2502 * 'flags' is used to indicate what kind of match is made, match is by
2503 * default for the destination address, if matching by source address
2504 * is desired the flag IGC_MAC_STATE_SRC_ADDR can be used.
2506 static int igc_add_mac_filter_flags(struct igc_adapter *adapter,
2507 const u8 *addr, const u8 queue,
2510 struct igc_hw *hw = &adapter->hw;
2511 int rar_entries = hw->mac.rar_entry_count;
2514 if (is_zero_ether_addr(addr))
2517 /* Search for the first empty entry in the MAC table.
2518 * Do not touch entries at the end of the table reserved for the VF MAC
2521 for (i = 0; i < rar_entries; i++) {
2522 if (!igc_mac_entry_can_be_used(&adapter->mac_table[i],
2526 ether_addr_copy(adapter->mac_table[i].addr, addr);
2527 adapter->mac_table[i].queue = queue;
2528 adapter->mac_table[i].state |= IGC_MAC_STATE_IN_USE | flags;
2530 igc_rar_set_index(adapter, i);
2537 int igc_add_mac_steering_filter(struct igc_adapter *adapter,
2538 const u8 *addr, u8 queue, u8 flags)
2540 return igc_add_mac_filter_flags(adapter, addr, queue,
2541 IGC_MAC_STATE_QUEUE_STEERING | flags);
2544 /* Remove a MAC filter for 'addr' directing matching traffic to
2545 * 'queue', 'flags' is used to indicate what kind of match need to be
2546 * removed, match is by default for the destination address, if
2547 * matching by source address is to be removed the flag
2548 * IGC_MAC_STATE_SRC_ADDR can be used.
2550 static int igc_del_mac_filter_flags(struct igc_adapter *adapter,
2551 const u8 *addr, const u8 queue,
2554 struct igc_hw *hw = &adapter->hw;
2555 int rar_entries = hw->mac.rar_entry_count;
2558 if (is_zero_ether_addr(addr))
2561 /* Search for matching entry in the MAC table based on given address
2562 * and queue. Do not touch entries at the end of the table reserved
2563 * for the VF MAC addresses.
2565 for (i = 0; i < rar_entries; i++) {
2566 if (!(adapter->mac_table[i].state & IGC_MAC_STATE_IN_USE))
2568 if ((adapter->mac_table[i].state & flags) != flags)
2570 if (adapter->mac_table[i].queue != queue)
2572 if (!ether_addr_equal(adapter->mac_table[i].addr, addr))
2575 /* When a filter for the default address is "deleted",
2576 * we return it to its initial configuration
2578 if (adapter->mac_table[i].state & IGC_MAC_STATE_DEFAULT) {
2579 adapter->mac_table[i].state =
2580 IGC_MAC_STATE_DEFAULT | IGC_MAC_STATE_IN_USE;
2582 adapter->mac_table[i].state = 0;
2583 adapter->mac_table[i].queue = 0;
2584 memset(adapter->mac_table[i].addr, 0, ETH_ALEN);
2587 igc_rar_set_index(adapter, i);
2594 int igc_del_mac_steering_filter(struct igc_adapter *adapter,
2595 const u8 *addr, u8 queue, u8 flags)
2597 return igc_del_mac_filter_flags(adapter, addr, queue,
2598 IGC_MAC_STATE_QUEUE_STEERING | flags);
2601 /* Add a MAC filter for 'addr' directing matching traffic to 'queue',
2602 * 'flags' is used to indicate what kind of match is made, match is by
2603 * default for the destination address, if matching by source address
2604 * is desired the flag IGC_MAC_STATE_SRC_ADDR can be used.
2606 static int igc_add_mac_filter(struct igc_adapter *adapter,
2607 const u8 *addr, const u8 queue)
2609 struct igc_hw *hw = &adapter->hw;
2610 int rar_entries = hw->mac.rar_entry_count;
2613 if (is_zero_ether_addr(addr))
2616 /* Search for the first empty entry in the MAC table.
2617 * Do not touch entries at the end of the table reserved for the VF MAC
2620 for (i = 0; i < rar_entries; i++) {
2621 if (!igc_mac_entry_can_be_used(&adapter->mac_table[i],
2625 ether_addr_copy(adapter->mac_table[i].addr, addr);
2626 adapter->mac_table[i].queue = queue;
2627 adapter->mac_table[i].state |= IGC_MAC_STATE_IN_USE;
2629 igc_rar_set_index(adapter, i);
2636 /* Remove a MAC filter for 'addr' directing matching traffic to
2637 * 'queue', 'flags' is used to indicate what kind of match need to be
2638 * removed, match is by default for the destination address, if
2639 * matching by source address is to be removed the flag
2640 * IGC_MAC_STATE_SRC_ADDR can be used.
2642 static int igc_del_mac_filter(struct igc_adapter *adapter,
2643 const u8 *addr, const u8 queue)
2645 struct igc_hw *hw = &adapter->hw;
2646 int rar_entries = hw->mac.rar_entry_count;
2649 if (is_zero_ether_addr(addr))
2652 /* Search for matching entry in the MAC table based on given address
2653 * and queue. Do not touch entries at the end of the table reserved
2654 * for the VF MAC addresses.
2656 for (i = 0; i < rar_entries; i++) {
2657 if (!(adapter->mac_table[i].state & IGC_MAC_STATE_IN_USE))
2659 if (adapter->mac_table[i].state != 0)
2661 if (adapter->mac_table[i].queue != queue)
2663 if (!ether_addr_equal(adapter->mac_table[i].addr, addr))
2666 /* When a filter for the default address is "deleted",
2667 * we return it to its initial configuration
2669 if (adapter->mac_table[i].state & IGC_MAC_STATE_DEFAULT) {
2670 adapter->mac_table[i].state =
2671 IGC_MAC_STATE_DEFAULT | IGC_MAC_STATE_IN_USE;
2672 adapter->mac_table[i].queue = 0;
2674 adapter->mac_table[i].state = 0;
2675 adapter->mac_table[i].queue = 0;
2676 memset(adapter->mac_table[i].addr, 0, ETH_ALEN);
2679 igc_rar_set_index(adapter, i);
2686 static int igc_uc_sync(struct net_device *netdev, const unsigned char *addr)
2688 struct igc_adapter *adapter = netdev_priv(netdev);
2691 ret = igc_add_mac_filter(adapter, addr, adapter->num_rx_queues);
2693 return min_t(int, ret, 0);
2696 static int igc_uc_unsync(struct net_device *netdev, const unsigned char *addr)
2698 struct igc_adapter *adapter = netdev_priv(netdev);
2700 igc_del_mac_filter(adapter, addr, adapter->num_rx_queues);
2706 * igc_set_rx_mode - Secondary Unicast, Multicast and Promiscuous mode set
2707 * @netdev: network interface device structure
2709 * The set_rx_mode entry point is called whenever the unicast or multicast
2710 * address lists or the network interface flags are updated. This routine is
2711 * responsible for configuring the hardware for proper unicast, multicast,
2712 * promiscuous mode, and all-multi behavior.
2714 static void igc_set_rx_mode(struct net_device *netdev)
2716 struct igc_adapter *adapter = netdev_priv(netdev);
2717 struct igc_hw *hw = &adapter->hw;
2718 u32 rctl = 0, rlpml = MAX_JUMBO_FRAME_SIZE;
2721 /* Check for Promiscuous and All Multicast modes */
2722 if (netdev->flags & IFF_PROMISC) {
2723 rctl |= IGC_RCTL_UPE | IGC_RCTL_MPE;
2725 if (netdev->flags & IFF_ALLMULTI) {
2726 rctl |= IGC_RCTL_MPE;
2728 /* Write addresses to the MTA, if the attempt fails
2729 * then we should just turn on promiscuous mode so
2730 * that we can at least receive multicast traffic
2732 count = igc_write_mc_addr_list(netdev);
2734 rctl |= IGC_RCTL_MPE;
2738 /* Write addresses to available RAR registers, if there is not
2739 * sufficient space to store all the addresses then enable
2740 * unicast promiscuous mode
2742 if (__dev_uc_sync(netdev, igc_uc_sync, igc_uc_unsync))
2743 rctl |= IGC_RCTL_UPE;
2745 /* update state of unicast and multicast */
2746 rctl |= rd32(IGC_RCTL) & ~(IGC_RCTL_UPE | IGC_RCTL_MPE);
2747 wr32(IGC_RCTL, rctl);
2749 #if (PAGE_SIZE < 8192)
2750 if (adapter->max_frame_size <= IGC_MAX_FRAME_BUILD_SKB)
2751 rlpml = IGC_MAX_FRAME_BUILD_SKB;
2753 wr32(IGC_RLPML, rlpml);
2757 * igc_msix_other - msix other interrupt handler
2758 * @irq: interrupt number
2759 * @data: pointer to a q_vector
2761 static irqreturn_t igc_msix_other(int irq, void *data)
2763 struct igc_adapter *adapter = data;
2764 struct igc_hw *hw = &adapter->hw;
2765 u32 icr = rd32(IGC_ICR);
2767 /* reading ICR causes bit 31 of EICR to be cleared */
2768 if (icr & IGC_ICR_DRSTA)
2769 schedule_work(&adapter->reset_task);
2771 if (icr & IGC_ICR_DOUTSYNC) {
2772 /* HW is reporting DMA is out of sync */
2773 adapter->stats.doosync++;
2776 if (icr & IGC_ICR_LSC) {
2777 hw->mac.get_link_status = 1;
2778 /* guard against interrupt when we're going down */
2779 if (!test_bit(__IGC_DOWN, &adapter->state))
2780 mod_timer(&adapter->watchdog_timer, jiffies + 1);
2783 wr32(IGC_EIMS, adapter->eims_other);
2789 * igc_write_ivar - configure ivar for given MSI-X vector
2790 * @hw: pointer to the HW structure
2791 * @msix_vector: vector number we are allocating to a given ring
2792 * @index: row index of IVAR register to write within IVAR table
2793 * @offset: column offset of in IVAR, should be multiple of 8
2795 * The IVAR table consists of 2 columns,
2796 * each containing an cause allocation for an Rx and Tx ring, and a
2797 * variable number of rows depending on the number of queues supported.
2799 static void igc_write_ivar(struct igc_hw *hw, int msix_vector,
2800 int index, int offset)
2802 u32 ivar = array_rd32(IGC_IVAR0, index);
2804 /* clear any bits that are currently set */
2805 ivar &= ~((u32)0xFF << offset);
2807 /* write vector and valid bit */
2808 ivar |= (msix_vector | IGC_IVAR_VALID) << offset;
2810 array_wr32(IGC_IVAR0, index, ivar);
2813 static void igc_assign_vector(struct igc_q_vector *q_vector, int msix_vector)
2815 struct igc_adapter *adapter = q_vector->adapter;
2816 struct igc_hw *hw = &adapter->hw;
2817 int rx_queue = IGC_N0_QUEUE;
2818 int tx_queue = IGC_N0_QUEUE;
2820 if (q_vector->rx.ring)
2821 rx_queue = q_vector->rx.ring->reg_idx;
2822 if (q_vector->tx.ring)
2823 tx_queue = q_vector->tx.ring->reg_idx;
2825 switch (hw->mac.type) {
2827 if (rx_queue > IGC_N0_QUEUE)
2828 igc_write_ivar(hw, msix_vector,
2830 (rx_queue & 0x1) << 4);
2831 if (tx_queue > IGC_N0_QUEUE)
2832 igc_write_ivar(hw, msix_vector,
2834 ((tx_queue & 0x1) << 4) + 8);
2835 q_vector->eims_value = BIT(msix_vector);
2838 WARN_ONCE(hw->mac.type != igc_i225, "Wrong MAC type\n");
2842 /* add q_vector eims value to global eims_enable_mask */
2843 adapter->eims_enable_mask |= q_vector->eims_value;
2845 /* configure q_vector to set itr on first interrupt */
2846 q_vector->set_itr = 1;
2850 * igc_configure_msix - Configure MSI-X hardware
2851 * @adapter: Pointer to adapter structure
2853 * igc_configure_msix sets up the hardware to properly
2854 * generate MSI-X interrupts.
2856 static void igc_configure_msix(struct igc_adapter *adapter)
2858 struct igc_hw *hw = &adapter->hw;
2862 adapter->eims_enable_mask = 0;
2864 /* set vector for other causes, i.e. link changes */
2865 switch (hw->mac.type) {
2867 /* Turn on MSI-X capability first, or our settings
2868 * won't stick. And it will take days to debug.
2870 wr32(IGC_GPIE, IGC_GPIE_MSIX_MODE |
2871 IGC_GPIE_PBA | IGC_GPIE_EIAME |
2874 /* enable msix_other interrupt */
2875 adapter->eims_other = BIT(vector);
2876 tmp = (vector++ | IGC_IVAR_VALID) << 8;
2878 wr32(IGC_IVAR_MISC, tmp);
2881 /* do nothing, since nothing else supports MSI-X */
2883 } /* switch (hw->mac.type) */
2885 adapter->eims_enable_mask |= adapter->eims_other;
2887 for (i = 0; i < adapter->num_q_vectors; i++)
2888 igc_assign_vector(adapter->q_vector[i], vector++);
2893 static irqreturn_t igc_msix_ring(int irq, void *data)
2895 struct igc_q_vector *q_vector = data;
2897 /* Write the ITR value calculated from the previous interrupt. */
2898 igc_write_itr(q_vector);
2900 napi_schedule(&q_vector->napi);
2906 * igc_request_msix - Initialize MSI-X interrupts
2907 * @adapter: Pointer to adapter structure
2909 * igc_request_msix allocates MSI-X vectors and requests interrupts from the
2912 static int igc_request_msix(struct igc_adapter *adapter)
2914 int i = 0, err = 0, vector = 0, free_vector = 0;
2915 struct net_device *netdev = adapter->netdev;
2917 err = request_irq(adapter->msix_entries[vector].vector,
2918 &igc_msix_other, 0, netdev->name, adapter);
2922 for (i = 0; i < adapter->num_q_vectors; i++) {
2923 struct igc_q_vector *q_vector = adapter->q_vector[i];
2927 q_vector->itr_register = adapter->io_addr + IGC_EITR(vector);
2929 if (q_vector->rx.ring && q_vector->tx.ring)
2930 sprintf(q_vector->name, "%s-TxRx-%u", netdev->name,
2931 q_vector->rx.ring->queue_index);
2932 else if (q_vector->tx.ring)
2933 sprintf(q_vector->name, "%s-tx-%u", netdev->name,
2934 q_vector->tx.ring->queue_index);
2935 else if (q_vector->rx.ring)
2936 sprintf(q_vector->name, "%s-rx-%u", netdev->name,
2937 q_vector->rx.ring->queue_index);
2939 sprintf(q_vector->name, "%s-unused", netdev->name);
2941 err = request_irq(adapter->msix_entries[vector].vector,
2942 igc_msix_ring, 0, q_vector->name,
2948 igc_configure_msix(adapter);
2952 /* free already assigned IRQs */
2953 free_irq(adapter->msix_entries[free_vector++].vector, adapter);
2956 for (i = 0; i < vector; i++) {
2957 free_irq(adapter->msix_entries[free_vector++].vector,
2958 adapter->q_vector[i]);
2965 * igc_reset_q_vector - Reset config for interrupt vector
2966 * @adapter: board private structure to initialize
2967 * @v_idx: Index of vector to be reset
2969 * If NAPI is enabled it will delete any references to the
2970 * NAPI struct. This is preparation for igc_free_q_vector.
2972 static void igc_reset_q_vector(struct igc_adapter *adapter, int v_idx)
2974 struct igc_q_vector *q_vector = adapter->q_vector[v_idx];
2976 /* if we're coming from igc_set_interrupt_capability, the vectors are
2982 if (q_vector->tx.ring)
2983 adapter->tx_ring[q_vector->tx.ring->queue_index] = NULL;
2985 if (q_vector->rx.ring)
2986 adapter->rx_ring[q_vector->rx.ring->queue_index] = NULL;
2988 netif_napi_del(&q_vector->napi);
2991 static void igc_reset_interrupt_capability(struct igc_adapter *adapter)
2993 int v_idx = adapter->num_q_vectors;
2995 if (adapter->msix_entries) {
2996 pci_disable_msix(adapter->pdev);
2997 kfree(adapter->msix_entries);
2998 adapter->msix_entries = NULL;
2999 } else if (adapter->flags & IGC_FLAG_HAS_MSI) {
3000 pci_disable_msi(adapter->pdev);
3004 igc_reset_q_vector(adapter, v_idx);
3008 * igc_clear_interrupt_scheme - reset the device to a state of no interrupts
3009 * @adapter: Pointer to adapter structure
3011 * This function resets the device so that it has 0 rx queues, tx queues, and
3012 * MSI-X interrupts allocated.
3014 static void igc_clear_interrupt_scheme(struct igc_adapter *adapter)
3016 igc_free_q_vectors(adapter);
3017 igc_reset_interrupt_capability(adapter);
3021 * igc_free_q_vectors - Free memory allocated for interrupt vectors
3022 * @adapter: board private structure to initialize
3024 * This function frees the memory allocated to the q_vectors. In addition if
3025 * NAPI is enabled it will delete any references to the NAPI struct prior
3026 * to freeing the q_vector.
3028 static void igc_free_q_vectors(struct igc_adapter *adapter)
3030 int v_idx = adapter->num_q_vectors;
3032 adapter->num_tx_queues = 0;
3033 adapter->num_rx_queues = 0;
3034 adapter->num_q_vectors = 0;
3037 igc_reset_q_vector(adapter, v_idx);
3038 igc_free_q_vector(adapter, v_idx);
3043 * igc_free_q_vector - Free memory allocated for specific interrupt vector
3044 * @adapter: board private structure to initialize
3045 * @v_idx: Index of vector to be freed
3047 * This function frees the memory allocated to the q_vector.
3049 static void igc_free_q_vector(struct igc_adapter *adapter, int v_idx)
3051 struct igc_q_vector *q_vector = adapter->q_vector[v_idx];
3053 adapter->q_vector[v_idx] = NULL;
3055 /* igc_get_stats64() might access the rings on this vector,
3056 * we must wait a grace period before freeing it.
3059 kfree_rcu(q_vector, rcu);
3062 /* Need to wait a few seconds after link up to get diagnostic information from
3065 static void igc_update_phy_info(struct timer_list *t)
3067 struct igc_adapter *adapter = from_timer(adapter, t, phy_info_timer);
3069 igc_get_phy_info(&adapter->hw);
3073 * igc_has_link - check shared code for link and determine up/down
3074 * @adapter: pointer to driver private info
3076 bool igc_has_link(struct igc_adapter *adapter)
3078 struct igc_hw *hw = &adapter->hw;
3079 bool link_active = false;
3081 /* get_link_status is set on LSC (link status) interrupt or
3082 * rx sequence error interrupt. get_link_status will stay
3083 * false until the igc_check_for_link establishes link
3084 * for copper adapters ONLY
3086 switch (hw->phy.media_type) {
3087 case igc_media_type_copper:
3088 if (!hw->mac.get_link_status)
3090 hw->mac.ops.check_for_link(hw);
3091 link_active = !hw->mac.get_link_status;
3094 case igc_media_type_unknown:
3098 if (hw->mac.type == igc_i225 &&
3099 hw->phy.id == I225_I_PHY_ID) {
3100 if (!netif_carrier_ok(adapter->netdev)) {
3101 adapter->flags &= ~IGC_FLAG_NEED_LINK_UPDATE;
3102 } else if (!(adapter->flags & IGC_FLAG_NEED_LINK_UPDATE)) {
3103 adapter->flags |= IGC_FLAG_NEED_LINK_UPDATE;
3104 adapter->link_check_timeout = jiffies;
3112 * igc_watchdog - Timer Call-back
3113 * @data: pointer to adapter cast into an unsigned long
3115 static void igc_watchdog(struct timer_list *t)
3117 struct igc_adapter *adapter = from_timer(adapter, t, watchdog_timer);
3118 /* Do the rest outside of interrupt context */
3119 schedule_work(&adapter->watchdog_task);
3122 static void igc_watchdog_task(struct work_struct *work)
3124 struct igc_adapter *adapter = container_of(work,
3127 struct net_device *netdev = adapter->netdev;
3128 struct igc_hw *hw = &adapter->hw;
3129 struct igc_phy_info *phy = &hw->phy;
3130 u16 phy_data, retry_count = 20;
3135 link = igc_has_link(adapter);
3137 if (adapter->flags & IGC_FLAG_NEED_LINK_UPDATE) {
3138 if (time_after(jiffies, (adapter->link_check_timeout + HZ)))
3139 adapter->flags &= ~IGC_FLAG_NEED_LINK_UPDATE;
3144 /* Force link down if we have fiber to swap to */
3145 if (adapter->flags & IGC_FLAG_MAS_ENABLE) {
3146 if (hw->phy.media_type == igc_media_type_copper) {
3147 connsw = rd32(IGC_CONNSW);
3148 if (!(connsw & IGC_CONNSW_AUTOSENSE_EN))
3153 if (!netif_carrier_ok(netdev)) {
3156 hw->mac.ops.get_speed_and_duplex(hw,
3157 &adapter->link_speed,
3158 &adapter->link_duplex);
3160 ctrl = rd32(IGC_CTRL);
3161 /* Link status message must follow this format */
3163 "igc: %s NIC Link is Up %d Mbps %s Duplex, Flow Control: %s\n",
3165 adapter->link_speed,
3166 adapter->link_duplex == FULL_DUPLEX ?
3168 (ctrl & IGC_CTRL_TFCE) &&
3169 (ctrl & IGC_CTRL_RFCE) ? "RX/TX" :
3170 (ctrl & IGC_CTRL_RFCE) ? "RX" :
3171 (ctrl & IGC_CTRL_TFCE) ? "TX" : "None");
3173 /* check if SmartSpeed worked */
3174 igc_check_downshift(hw);
3175 if (phy->speed_downgraded)
3176 netdev_warn(netdev, "Link Speed was downgraded by SmartSpeed\n");
3178 /* adjust timeout factor according to speed/duplex */
3179 adapter->tx_timeout_factor = 1;
3180 switch (adapter->link_speed) {
3182 adapter->tx_timeout_factor = 14;
3185 /* maybe add some timeout factor ? */
3189 if (adapter->link_speed != SPEED_1000)
3192 /* wait for Remote receiver status OK */
3194 if (!igc_read_phy_reg(hw, PHY_1000T_STATUS,
3196 if (!(phy_data & SR_1000T_REMOTE_RX_STATUS) &&
3200 goto retry_read_status;
3201 } else if (!retry_count) {
3202 dev_err(&adapter->pdev->dev, "exceed max 2 second\n");
3205 dev_err(&adapter->pdev->dev, "read 1000Base-T Status Reg\n");
3208 netif_carrier_on(netdev);
3210 /* link state has changed, schedule phy info update */
3211 if (!test_bit(__IGC_DOWN, &adapter->state))
3212 mod_timer(&adapter->phy_info_timer,
3213 round_jiffies(jiffies + 2 * HZ));
3216 if (netif_carrier_ok(netdev)) {
3217 adapter->link_speed = 0;
3218 adapter->link_duplex = 0;
3220 /* Links status message must follow this format */
3221 netdev_info(netdev, "igc: %s NIC Link is Down\n",
3223 netif_carrier_off(netdev);
3225 /* link state has changed, schedule phy info update */
3226 if (!test_bit(__IGC_DOWN, &adapter->state))
3227 mod_timer(&adapter->phy_info_timer,
3228 round_jiffies(jiffies + 2 * HZ));
3230 /* link is down, time to check for alternate media */
3231 if (adapter->flags & IGC_FLAG_MAS_ENABLE) {
3232 if (adapter->flags & IGC_FLAG_MEDIA_RESET) {
3233 schedule_work(&adapter->reset_task);
3234 /* return immediately */
3239 /* also check for alternate media here */
3240 } else if (!netif_carrier_ok(netdev) &&
3241 (adapter->flags & IGC_FLAG_MAS_ENABLE)) {
3242 if (adapter->flags & IGC_FLAG_MEDIA_RESET) {
3243 schedule_work(&adapter->reset_task);
3244 /* return immediately */
3250 spin_lock(&adapter->stats64_lock);
3251 igc_update_stats(adapter);
3252 spin_unlock(&adapter->stats64_lock);
3254 for (i = 0; i < adapter->num_tx_queues; i++) {
3255 struct igc_ring *tx_ring = adapter->tx_ring[i];
3257 if (!netif_carrier_ok(netdev)) {
3258 /* We've lost link, so the controller stops DMA,
3259 * but we've got queued Tx work that's never going
3260 * to get done, so reset controller to flush Tx.
3261 * (Do the reset outside of interrupt context).
3263 if (igc_desc_unused(tx_ring) + 1 < tx_ring->count) {
3264 adapter->tx_timeout_count++;
3265 schedule_work(&adapter->reset_task);
3266 /* return immediately since reset is imminent */
3271 /* Force detection of hung controller every watchdog period */
3272 set_bit(IGC_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags);
3275 /* Cause software interrupt to ensure Rx ring is cleaned */
3276 if (adapter->flags & IGC_FLAG_HAS_MSIX) {
3279 for (i = 0; i < adapter->num_q_vectors; i++)
3280 eics |= adapter->q_vector[i]->eims_value;
3281 wr32(IGC_EICS, eics);
3283 wr32(IGC_ICS, IGC_ICS_RXDMT0);
3286 /* Reset the timer */
3287 if (!test_bit(__IGC_DOWN, &adapter->state)) {
3288 if (adapter->flags & IGC_FLAG_NEED_LINK_UPDATE)
3289 mod_timer(&adapter->watchdog_timer,
3290 round_jiffies(jiffies + HZ));
3292 mod_timer(&adapter->watchdog_timer,
3293 round_jiffies(jiffies + 2 * HZ));
3298 * igc_update_ring_itr - update the dynamic ITR value based on packet size
3299 * @q_vector: pointer to q_vector
3301 * Stores a new ITR value based on strictly on packet size. This
3302 * algorithm is less sophisticated than that used in igc_update_itr,
3303 * due to the difficulty of synchronizing statistics across multiple
3304 * receive rings. The divisors and thresholds used by this function
3305 * were determined based on theoretical maximum wire speed and testing
3306 * data, in order to minimize response time while increasing bulk
3308 * NOTE: This function is called only when operating in a multiqueue
3309 * receive environment.
3311 static void igc_update_ring_itr(struct igc_q_vector *q_vector)
3313 struct igc_adapter *adapter = q_vector->adapter;
3314 int new_val = q_vector->itr_val;
3315 int avg_wire_size = 0;
3316 unsigned int packets;
3318 /* For non-gigabit speeds, just fix the interrupt rate at 4000
3319 * ints/sec - ITR timer value of 120 ticks.
3321 switch (adapter->link_speed) {
3324 new_val = IGC_4K_ITR;
3330 packets = q_vector->rx.total_packets;
3332 avg_wire_size = q_vector->rx.total_bytes / packets;
3334 packets = q_vector->tx.total_packets;
3336 avg_wire_size = max_t(u32, avg_wire_size,
3337 q_vector->tx.total_bytes / packets);
3339 /* if avg_wire_size isn't set no work was done */
3343 /* Add 24 bytes to size to account for CRC, preamble, and gap */
3344 avg_wire_size += 24;
3346 /* Don't starve jumbo frames */
3347 avg_wire_size = min(avg_wire_size, 3000);
3349 /* Give a little boost to mid-size frames */
3350 if (avg_wire_size > 300 && avg_wire_size < 1200)
3351 new_val = avg_wire_size / 3;
3353 new_val = avg_wire_size / 2;
3355 /* conservative mode (itr 3) eliminates the lowest_latency setting */
3356 if (new_val < IGC_20K_ITR &&
3357 ((q_vector->rx.ring && adapter->rx_itr_setting == 3) ||
3358 (!q_vector->rx.ring && adapter->tx_itr_setting == 3)))
3359 new_val = IGC_20K_ITR;
3362 if (new_val != q_vector->itr_val) {
3363 q_vector->itr_val = new_val;
3364 q_vector->set_itr = 1;
3367 q_vector->rx.total_bytes = 0;
3368 q_vector->rx.total_packets = 0;
3369 q_vector->tx.total_bytes = 0;
3370 q_vector->tx.total_packets = 0;
3374 * igc_update_itr - update the dynamic ITR value based on statistics
3375 * @q_vector: pointer to q_vector
3376 * @ring_container: ring info to update the itr for
3378 * Stores a new ITR value based on packets and byte
3379 * counts during the last interrupt. The advantage of per interrupt
3380 * computation is faster updates and more accurate ITR for the current
3381 * traffic pattern. Constants in this function were computed
3382 * based on theoretical maximum wire speed and thresholds were set based
3383 * on testing data as well as attempting to minimize response time
3384 * while increasing bulk throughput.
3385 * NOTE: These calculations are only valid when operating in a single-
3386 * queue environment.
3388 static void igc_update_itr(struct igc_q_vector *q_vector,
3389 struct igc_ring_container *ring_container)
3391 unsigned int packets = ring_container->total_packets;
3392 unsigned int bytes = ring_container->total_bytes;
3393 u8 itrval = ring_container->itr;
3395 /* no packets, exit with status unchanged */
3400 case lowest_latency:
3401 /* handle TSO and jumbo frames */
3402 if (bytes / packets > 8000)
3403 itrval = bulk_latency;
3404 else if ((packets < 5) && (bytes > 512))
3405 itrval = low_latency;
3407 case low_latency: /* 50 usec aka 20000 ints/s */
3408 if (bytes > 10000) {
3409 /* this if handles the TSO accounting */
3410 if (bytes / packets > 8000)
3411 itrval = bulk_latency;
3412 else if ((packets < 10) || ((bytes / packets) > 1200))
3413 itrval = bulk_latency;
3414 else if ((packets > 35))
3415 itrval = lowest_latency;
3416 } else if (bytes / packets > 2000) {
3417 itrval = bulk_latency;
3418 } else if (packets <= 2 && bytes < 512) {
3419 itrval = lowest_latency;
3422 case bulk_latency: /* 250 usec aka 4000 ints/s */
3423 if (bytes > 25000) {
3425 itrval = low_latency;
3426 } else if (bytes < 1500) {
3427 itrval = low_latency;
3432 /* clear work counters since we have the values we need */
3433 ring_container->total_bytes = 0;
3434 ring_container->total_packets = 0;
3436 /* write updated itr to ring container */
3437 ring_container->itr = itrval;
3441 * igc_intr_msi - Interrupt Handler
3442 * @irq: interrupt number
3443 * @data: pointer to a network interface device structure
3445 static irqreturn_t igc_intr_msi(int irq, void *data)
3447 struct igc_adapter *adapter = data;
3448 struct igc_q_vector *q_vector = adapter->q_vector[0];
3449 struct igc_hw *hw = &adapter->hw;
3450 /* read ICR disables interrupts using IAM */
3451 u32 icr = rd32(IGC_ICR);
3453 igc_write_itr(q_vector);
3455 if (icr & IGC_ICR_DRSTA)
3456 schedule_work(&adapter->reset_task);
3458 if (icr & IGC_ICR_DOUTSYNC) {
3459 /* HW is reporting DMA is out of sync */
3460 adapter->stats.doosync++;
3463 if (icr & (IGC_ICR_RXSEQ | IGC_ICR_LSC)) {
3464 hw->mac.get_link_status = 1;
3465 if (!test_bit(__IGC_DOWN, &adapter->state))
3466 mod_timer(&adapter->watchdog_timer, jiffies + 1);
3469 napi_schedule(&q_vector->napi);
3475 * igc_intr - Legacy Interrupt Handler
3476 * @irq: interrupt number
3477 * @data: pointer to a network interface device structure
3479 static irqreturn_t igc_intr(int irq, void *data)
3481 struct igc_adapter *adapter = data;
3482 struct igc_q_vector *q_vector = adapter->q_vector[0];
3483 struct igc_hw *hw = &adapter->hw;
3484 /* Interrupt Auto-Mask...upon reading ICR, interrupts are masked. No
3485 * need for the IMC write
3487 u32 icr = rd32(IGC_ICR);
3489 /* IMS will not auto-mask if INT_ASSERTED is not set, and if it is
3490 * not set, then the adapter didn't send an interrupt
3492 if (!(icr & IGC_ICR_INT_ASSERTED))
3495 igc_write_itr(q_vector);
3497 if (icr & IGC_ICR_DRSTA)
3498 schedule_work(&adapter->reset_task);
3500 if (icr & IGC_ICR_DOUTSYNC) {
3501 /* HW is reporting DMA is out of sync */
3502 adapter->stats.doosync++;
3505 if (icr & (IGC_ICR_RXSEQ | IGC_ICR_LSC)) {
3506 hw->mac.get_link_status = 1;
3507 /* guard against interrupt when we're going down */
3508 if (!test_bit(__IGC_DOWN, &adapter->state))
3509 mod_timer(&adapter->watchdog_timer, jiffies + 1);
3512 napi_schedule(&q_vector->napi);
3517 static void igc_set_itr(struct igc_q_vector *q_vector)
3519 struct igc_adapter *adapter = q_vector->adapter;
3520 u32 new_itr = q_vector->itr_val;
3523 /* for non-gigabit speeds, just fix the interrupt rate at 4000 */
3524 switch (adapter->link_speed) {
3528 new_itr = IGC_4K_ITR;
3534 igc_update_itr(q_vector, &q_vector->tx);
3535 igc_update_itr(q_vector, &q_vector->rx);
3537 current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
3539 /* conservative mode (itr 3) eliminates the lowest_latency setting */
3540 if (current_itr == lowest_latency &&
3541 ((q_vector->rx.ring && adapter->rx_itr_setting == 3) ||
3542 (!q_vector->rx.ring && adapter->tx_itr_setting == 3)))
3543 current_itr = low_latency;
3545 switch (current_itr) {
3546 /* counts and packets in update_itr are dependent on these numbers */
3547 case lowest_latency:
3548 new_itr = IGC_70K_ITR; /* 70,000 ints/sec */
3551 new_itr = IGC_20K_ITR; /* 20,000 ints/sec */
3554 new_itr = IGC_4K_ITR; /* 4,000 ints/sec */
3561 if (new_itr != q_vector->itr_val) {
3562 /* this attempts to bias the interrupt rate towards Bulk
3563 * by adding intermediate steps when interrupt rate is
3566 new_itr = new_itr > q_vector->itr_val ?
3567 max((new_itr * q_vector->itr_val) /
3568 (new_itr + (q_vector->itr_val >> 2)),
3570 /* Don't write the value here; it resets the adapter's
3571 * internal timer, and causes us to delay far longer than
3572 * we should between interrupts. Instead, we write the ITR
3573 * value at the beginning of the next interrupt so the timing
3574 * ends up being correct.
3576 q_vector->itr_val = new_itr;
3577 q_vector->set_itr = 1;
3581 static void igc_ring_irq_enable(struct igc_q_vector *q_vector)
3583 struct igc_adapter *adapter = q_vector->adapter;
3584 struct igc_hw *hw = &adapter->hw;
3586 if ((q_vector->rx.ring && (adapter->rx_itr_setting & 3)) ||
3587 (!q_vector->rx.ring && (adapter->tx_itr_setting & 3))) {
3588 if (adapter->num_q_vectors == 1)
3589 igc_set_itr(q_vector);
3591 igc_update_ring_itr(q_vector);
3594 if (!test_bit(__IGC_DOWN, &adapter->state)) {
3595 if (adapter->msix_entries)
3596 wr32(IGC_EIMS, q_vector->eims_value);
3598 igc_irq_enable(adapter);
3603 * igc_poll - NAPI Rx polling callback
3604 * @napi: napi polling structure
3605 * @budget: count of how many packets we should handle
3607 static int igc_poll(struct napi_struct *napi, int budget)
3609 struct igc_q_vector *q_vector = container_of(napi,
3610 struct igc_q_vector,
3612 bool clean_complete = true;
3615 if (q_vector->tx.ring)
3616 clean_complete = igc_clean_tx_irq(q_vector, budget);
3618 if (q_vector->rx.ring) {
3619 int cleaned = igc_clean_rx_irq(q_vector, budget);
3621 work_done += cleaned;
3622 if (cleaned >= budget)
3623 clean_complete = false;
3626 /* If all work not completed, return budget and keep polling */
3627 if (!clean_complete)
3630 /* Exit the polling mode, but don't re-enable interrupts if stack might
3631 * poll us due to busy-polling
3633 if (likely(napi_complete_done(napi, work_done)))
3634 igc_ring_irq_enable(q_vector);
3636 return min(work_done, budget - 1);
3640 * igc_set_interrupt_capability - set MSI or MSI-X if supported
3641 * @adapter: Pointer to adapter structure
3643 * Attempt to configure interrupts using the best available
3644 * capabilities of the hardware and kernel.
3646 static void igc_set_interrupt_capability(struct igc_adapter *adapter,
3654 adapter->flags |= IGC_FLAG_HAS_MSIX;
3656 /* Number of supported queues. */
3657 adapter->num_rx_queues = adapter->rss_queues;
3659 adapter->num_tx_queues = adapter->rss_queues;
3661 /* start with one vector for every Rx queue */
3662 numvecs = adapter->num_rx_queues;
3664 /* if Tx handler is separate add 1 for every Tx queue */
3665 if (!(adapter->flags & IGC_FLAG_QUEUE_PAIRS))
3666 numvecs += adapter->num_tx_queues;
3668 /* store the number of vectors reserved for queues */
3669 adapter->num_q_vectors = numvecs;
3671 /* add 1 vector for link status interrupts */
3674 adapter->msix_entries = kcalloc(numvecs, sizeof(struct msix_entry),
3677 if (!adapter->msix_entries)
3680 /* populate entry values */
3681 for (i = 0; i < numvecs; i++)
3682 adapter->msix_entries[i].entry = i;
3684 err = pci_enable_msix_range(adapter->pdev,
3685 adapter->msix_entries,
3691 kfree(adapter->msix_entries);
3692 adapter->msix_entries = NULL;
3694 igc_reset_interrupt_capability(adapter);
3697 adapter->flags &= ~IGC_FLAG_HAS_MSIX;
3699 adapter->rss_queues = 1;
3700 adapter->flags |= IGC_FLAG_QUEUE_PAIRS;
3701 adapter->num_rx_queues = 1;
3702 adapter->num_tx_queues = 1;
3703 adapter->num_q_vectors = 1;
3704 if (!pci_enable_msi(adapter->pdev))
3705 adapter->flags |= IGC_FLAG_HAS_MSI;
3708 static void igc_add_ring(struct igc_ring *ring,
3709 struct igc_ring_container *head)
3716 * igc_alloc_q_vector - Allocate memory for a single interrupt vector
3717 * @adapter: board private structure to initialize
3718 * @v_count: q_vectors allocated on adapter, used for ring interleaving
3719 * @v_idx: index of vector in adapter struct
3720 * @txr_count: total number of Tx rings to allocate
3721 * @txr_idx: index of first Tx ring to allocate
3722 * @rxr_count: total number of Rx rings to allocate
3723 * @rxr_idx: index of first Rx ring to allocate
3725 * We allocate one q_vector. If allocation fails we return -ENOMEM.
3727 static int igc_alloc_q_vector(struct igc_adapter *adapter,
3728 unsigned int v_count, unsigned int v_idx,
3729 unsigned int txr_count, unsigned int txr_idx,
3730 unsigned int rxr_count, unsigned int rxr_idx)
3732 struct igc_q_vector *q_vector;
3733 struct igc_ring *ring;
3736 /* igc only supports 1 Tx and/or 1 Rx queue per vector */
3737 if (txr_count > 1 || rxr_count > 1)
3740 ring_count = txr_count + rxr_count;
3742 /* allocate q_vector and rings */
3743 q_vector = adapter->q_vector[v_idx];
3745 q_vector = kzalloc(struct_size(q_vector, ring, ring_count),
3748 memset(q_vector, 0, struct_size(q_vector, ring, ring_count));
3752 /* initialize NAPI */
3753 netif_napi_add(adapter->netdev, &q_vector->napi,
3756 /* tie q_vector and adapter together */
3757 adapter->q_vector[v_idx] = q_vector;
3758 q_vector->adapter = adapter;
3760 /* initialize work limits */
3761 q_vector->tx.work_limit = adapter->tx_work_limit;
3763 /* initialize ITR configuration */
3764 q_vector->itr_register = adapter->io_addr + IGC_EITR(0);
3765 q_vector->itr_val = IGC_START_ITR;
3767 /* initialize pointer to rings */
3768 ring = q_vector->ring;
3770 /* initialize ITR */
3772 /* rx or rx/tx vector */
3773 if (!adapter->rx_itr_setting || adapter->rx_itr_setting > 3)
3774 q_vector->itr_val = adapter->rx_itr_setting;
3776 /* tx only vector */
3777 if (!adapter->tx_itr_setting || adapter->tx_itr_setting > 3)
3778 q_vector->itr_val = adapter->tx_itr_setting;
3782 /* assign generic ring traits */
3783 ring->dev = &adapter->pdev->dev;
3784 ring->netdev = adapter->netdev;
3786 /* configure backlink on ring */
3787 ring->q_vector = q_vector;
3789 /* update q_vector Tx values */
3790 igc_add_ring(ring, &q_vector->tx);
3792 /* apply Tx specific ring traits */
3793 ring->count = adapter->tx_ring_count;
3794 ring->queue_index = txr_idx;
3796 /* assign ring to adapter */
3797 adapter->tx_ring[txr_idx] = ring;
3799 /* push pointer to next ring */
3804 /* assign generic ring traits */
3805 ring->dev = &adapter->pdev->dev;
3806 ring->netdev = adapter->netdev;
3808 /* configure backlink on ring */
3809 ring->q_vector = q_vector;
3811 /* update q_vector Rx values */
3812 igc_add_ring(ring, &q_vector->rx);
3814 /* apply Rx specific ring traits */
3815 ring->count = adapter->rx_ring_count;
3816 ring->queue_index = rxr_idx;
3818 /* assign ring to adapter */
3819 adapter->rx_ring[rxr_idx] = ring;
3826 * igc_alloc_q_vectors - Allocate memory for interrupt vectors
3827 * @adapter: board private structure to initialize
3829 * We allocate one q_vector per queue interrupt. If allocation fails we
3832 static int igc_alloc_q_vectors(struct igc_adapter *adapter)
3834 int rxr_remaining = adapter->num_rx_queues;
3835 int txr_remaining = adapter->num_tx_queues;
3836 int rxr_idx = 0, txr_idx = 0, v_idx = 0;
3837 int q_vectors = adapter->num_q_vectors;
3840 if (q_vectors >= (rxr_remaining + txr_remaining)) {
3841 for (; rxr_remaining; v_idx++) {
3842 err = igc_alloc_q_vector(adapter, q_vectors, v_idx,
3848 /* update counts and index */
3854 for (; v_idx < q_vectors; v_idx++) {
3855 int rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - v_idx);
3856 int tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - v_idx);
3858 err = igc_alloc_q_vector(adapter, q_vectors, v_idx,
3859 tqpv, txr_idx, rqpv, rxr_idx);
3864 /* update counts and index */
3865 rxr_remaining -= rqpv;
3866 txr_remaining -= tqpv;
3874 adapter->num_tx_queues = 0;
3875 adapter->num_rx_queues = 0;
3876 adapter->num_q_vectors = 0;
3879 igc_free_q_vector(adapter, v_idx);
3885 * igc_cache_ring_register - Descriptor ring to register mapping
3886 * @adapter: board private structure to initialize
3888 * Once we know the feature-set enabled for the device, we'll cache
3889 * the register offset the descriptor ring is assigned to.
3891 static void igc_cache_ring_register(struct igc_adapter *adapter)
3895 switch (adapter->hw.mac.type) {
3899 for (; i < adapter->num_rx_queues; i++)
3900 adapter->rx_ring[i]->reg_idx = i;
3901 for (; j < adapter->num_tx_queues; j++)
3902 adapter->tx_ring[j]->reg_idx = j;
3908 * igc_init_interrupt_scheme - initialize interrupts, allocate queues/vectors
3909 * @adapter: Pointer to adapter structure
3911 * This function initializes the interrupts and allocates all of the queues.
3913 static int igc_init_interrupt_scheme(struct igc_adapter *adapter, bool msix)
3915 struct pci_dev *pdev = adapter->pdev;
3918 igc_set_interrupt_capability(adapter, msix);
3920 err = igc_alloc_q_vectors(adapter);
3922 dev_err(&pdev->dev, "Unable to allocate memory for vectors\n");
3923 goto err_alloc_q_vectors;
3926 igc_cache_ring_register(adapter);
3930 err_alloc_q_vectors:
3931 igc_reset_interrupt_capability(adapter);
3935 static void igc_free_irq(struct igc_adapter *adapter)
3937 if (adapter->msix_entries) {
3940 free_irq(adapter->msix_entries[vector++].vector, adapter);
3942 for (i = 0; i < adapter->num_q_vectors; i++)
3943 free_irq(adapter->msix_entries[vector++].vector,
3944 adapter->q_vector[i]);
3946 free_irq(adapter->pdev->irq, adapter);
3951 * igc_irq_disable - Mask off interrupt generation on the NIC
3952 * @adapter: board private structure
3954 static void igc_irq_disable(struct igc_adapter *adapter)
3956 struct igc_hw *hw = &adapter->hw;
3958 if (adapter->msix_entries) {
3959 u32 regval = rd32(IGC_EIAM);
3961 wr32(IGC_EIAM, regval & ~adapter->eims_enable_mask);
3962 wr32(IGC_EIMC, adapter->eims_enable_mask);
3963 regval = rd32(IGC_EIAC);
3964 wr32(IGC_EIAC, regval & ~adapter->eims_enable_mask);
3971 if (adapter->msix_entries) {
3974 synchronize_irq(adapter->msix_entries[vector++].vector);
3976 for (i = 0; i < adapter->num_q_vectors; i++)
3977 synchronize_irq(adapter->msix_entries[vector++].vector);
3979 synchronize_irq(adapter->pdev->irq);
3984 * igc_irq_enable - Enable default interrupt generation settings
3985 * @adapter: board private structure
3987 static void igc_irq_enable(struct igc_adapter *adapter)
3989 struct igc_hw *hw = &adapter->hw;
3991 if (adapter->msix_entries) {
3992 u32 ims = IGC_IMS_LSC | IGC_IMS_DOUTSYNC | IGC_IMS_DRSTA;
3993 u32 regval = rd32(IGC_EIAC);
3995 wr32(IGC_EIAC, regval | adapter->eims_enable_mask);
3996 regval = rd32(IGC_EIAM);
3997 wr32(IGC_EIAM, regval | adapter->eims_enable_mask);
3998 wr32(IGC_EIMS, adapter->eims_enable_mask);
4001 wr32(IGC_IMS, IMS_ENABLE_MASK | IGC_IMS_DRSTA);
4002 wr32(IGC_IAM, IMS_ENABLE_MASK | IGC_IMS_DRSTA);
4007 * igc_request_irq - initialize interrupts
4008 * @adapter: Pointer to adapter structure
4010 * Attempts to configure interrupts using the best available
4011 * capabilities of the hardware and kernel.
4013 static int igc_request_irq(struct igc_adapter *adapter)
4015 struct net_device *netdev = adapter->netdev;
4016 struct pci_dev *pdev = adapter->pdev;
4019 if (adapter->flags & IGC_FLAG_HAS_MSIX) {
4020 err = igc_request_msix(adapter);
4023 /* fall back to MSI */
4024 igc_free_all_tx_resources(adapter);
4025 igc_free_all_rx_resources(adapter);
4027 igc_clear_interrupt_scheme(adapter);
4028 err = igc_init_interrupt_scheme(adapter, false);
4031 igc_setup_all_tx_resources(adapter);
4032 igc_setup_all_rx_resources(adapter);
4033 igc_configure(adapter);
4036 igc_assign_vector(adapter->q_vector[0], 0);
4038 if (adapter->flags & IGC_FLAG_HAS_MSI) {
4039 err = request_irq(pdev->irq, &igc_intr_msi, 0,
4040 netdev->name, adapter);
4044 /* fall back to legacy interrupts */
4045 igc_reset_interrupt_capability(adapter);
4046 adapter->flags &= ~IGC_FLAG_HAS_MSI;
4049 err = request_irq(pdev->irq, &igc_intr, IRQF_SHARED,
4050 netdev->name, adapter);
4053 dev_err(&pdev->dev, "Error %d getting interrupt\n",
4060 static void igc_write_itr(struct igc_q_vector *q_vector)
4062 u32 itr_val = q_vector->itr_val & IGC_QVECTOR_MASK;
4064 if (!q_vector->set_itr)
4068 itr_val = IGC_ITR_VAL_MASK;
4070 itr_val |= IGC_EITR_CNT_IGNR;
4072 writel(itr_val, q_vector->itr_register);
4073 q_vector->set_itr = 0;
4077 * igc_open - Called when a network interface is made active
4078 * @netdev: network interface device structure
4080 * Returns 0 on success, negative value on failure
4082 * The open entry point is called when a network interface is made
4083 * active by the system (IFF_UP). At this point all resources needed
4084 * for transmit and receive operations are allocated, the interrupt
4085 * handler is registered with the OS, the watchdog timer is started,
4086 * and the stack is notified that the interface is ready.
4088 static int __igc_open(struct net_device *netdev, bool resuming)
4090 struct igc_adapter *adapter = netdev_priv(netdev);
4091 struct igc_hw *hw = &adapter->hw;
4095 /* disallow open during test */
4097 if (test_bit(__IGC_TESTING, &adapter->state)) {
4102 netif_carrier_off(netdev);
4104 /* allocate transmit descriptors */
4105 err = igc_setup_all_tx_resources(adapter);
4109 /* allocate receive descriptors */
4110 err = igc_setup_all_rx_resources(adapter);
4114 igc_power_up_link(adapter);
4116 igc_configure(adapter);
4118 err = igc_request_irq(adapter);
4122 /* Notify the stack of the actual queue counts. */
4123 err = netif_set_real_num_tx_queues(netdev, adapter->num_tx_queues);
4125 goto err_set_queues;
4127 err = netif_set_real_num_rx_queues(netdev, adapter->num_rx_queues);
4129 goto err_set_queues;
4131 clear_bit(__IGC_DOWN, &adapter->state);
4133 for (i = 0; i < adapter->num_q_vectors; i++)
4134 napi_enable(&adapter->q_vector[i]->napi);
4136 /* Clear any pending interrupts. */
4138 igc_irq_enable(adapter);
4140 netif_tx_start_all_queues(netdev);
4142 /* start the watchdog. */
4143 hw->mac.get_link_status = 1;
4144 schedule_work(&adapter->watchdog_task);
4149 igc_free_irq(adapter);
4151 igc_release_hw_control(adapter);
4152 igc_power_down_link(adapter);
4153 igc_free_all_rx_resources(adapter);
4155 igc_free_all_tx_resources(adapter);
4162 static int igc_open(struct net_device *netdev)
4164 return __igc_open(netdev, false);
4168 * igc_close - Disables a network interface
4169 * @netdev: network interface device structure
4171 * Returns 0, this is not allowed to fail
4173 * The close entry point is called when an interface is de-activated
4174 * by the OS. The hardware is still under the driver's control, but
4175 * needs to be disabled. A global MAC reset is issued to stop the
4176 * hardware, and all transmit and receive resources are freed.
4178 static int __igc_close(struct net_device *netdev, bool suspending)
4180 struct igc_adapter *adapter = netdev_priv(netdev);
4182 WARN_ON(test_bit(__IGC_RESETTING, &adapter->state));
4186 igc_release_hw_control(adapter);
4188 igc_free_irq(adapter);
4190 igc_free_all_tx_resources(adapter);
4191 igc_free_all_rx_resources(adapter);
4196 static int igc_close(struct net_device *netdev)
4198 if (netif_device_present(netdev) || netdev->dismantle)
4199 return __igc_close(netdev, false);
4203 static const struct net_device_ops igc_netdev_ops = {
4204 .ndo_open = igc_open,
4205 .ndo_stop = igc_close,
4206 .ndo_start_xmit = igc_xmit_frame,
4207 .ndo_set_rx_mode = igc_set_rx_mode,
4208 .ndo_set_mac_address = igc_set_mac,
4209 .ndo_change_mtu = igc_change_mtu,
4210 .ndo_get_stats = igc_get_stats,
4211 .ndo_fix_features = igc_fix_features,
4212 .ndo_set_features = igc_set_features,
4213 .ndo_features_check = igc_features_check,
4216 /* PCIe configuration access */
4217 void igc_read_pci_cfg(struct igc_hw *hw, u32 reg, u16 *value)
4219 struct igc_adapter *adapter = hw->back;
4221 pci_read_config_word(adapter->pdev, reg, value);
4224 void igc_write_pci_cfg(struct igc_hw *hw, u32 reg, u16 *value)
4226 struct igc_adapter *adapter = hw->back;
4228 pci_write_config_word(adapter->pdev, reg, *value);
4231 s32 igc_read_pcie_cap_reg(struct igc_hw *hw, u32 reg, u16 *value)
4233 struct igc_adapter *adapter = hw->back;
4235 if (!pci_is_pcie(adapter->pdev))
4236 return -IGC_ERR_CONFIG;
4238 pcie_capability_read_word(adapter->pdev, reg, value);
4243 s32 igc_write_pcie_cap_reg(struct igc_hw *hw, u32 reg, u16 *value)
4245 struct igc_adapter *adapter = hw->back;
4247 if (!pci_is_pcie(adapter->pdev))
4248 return -IGC_ERR_CONFIG;
4250 pcie_capability_write_word(adapter->pdev, reg, *value);
4255 u32 igc_rd32(struct igc_hw *hw, u32 reg)
4257 struct igc_adapter *igc = container_of(hw, struct igc_adapter, hw);
4258 u8 __iomem *hw_addr = READ_ONCE(hw->hw_addr);
4261 if (IGC_REMOVED(hw_addr))
4264 value = readl(&hw_addr[reg]);
4266 /* reads should not return all F's */
4267 if (!(~value) && (!reg || !(~readl(hw_addr)))) {
4268 struct net_device *netdev = igc->netdev;
4271 netif_device_detach(netdev);
4272 netdev_err(netdev, "PCIe link lost, device now detached\n");
4273 WARN(pci_device_is_present(igc->pdev),
4274 "igc: Failed to read reg 0x%x!\n", reg);
4280 int igc_set_spd_dplx(struct igc_adapter *adapter, u32 spd, u8 dplx)
4282 struct pci_dev *pdev = adapter->pdev;
4283 struct igc_mac_info *mac = &adapter->hw.mac;
4287 /* Make sure dplx is at most 1 bit and lsb of speed is not set
4288 * for the switch() below to work
4290 if ((spd & 1) || (dplx & ~1))
4293 switch (spd + dplx) {
4294 case SPEED_10 + DUPLEX_HALF:
4295 mac->forced_speed_duplex = ADVERTISE_10_HALF;
4297 case SPEED_10 + DUPLEX_FULL:
4298 mac->forced_speed_duplex = ADVERTISE_10_FULL;
4300 case SPEED_100 + DUPLEX_HALF:
4301 mac->forced_speed_duplex = ADVERTISE_100_HALF;
4303 case SPEED_100 + DUPLEX_FULL:
4304 mac->forced_speed_duplex = ADVERTISE_100_FULL;
4306 case SPEED_1000 + DUPLEX_FULL:
4308 adapter->hw.phy.autoneg_advertised = ADVERTISE_1000_FULL;
4310 case SPEED_1000 + DUPLEX_HALF: /* not supported */
4312 case SPEED_2500 + DUPLEX_FULL:
4314 adapter->hw.phy.autoneg_advertised = ADVERTISE_2500_FULL;
4316 case SPEED_2500 + DUPLEX_HALF: /* not supported */
4321 /* clear MDI, MDI(-X) override is only allowed when autoneg enabled */
4322 adapter->hw.phy.mdix = AUTO_ALL_MODES;
4327 dev_err(&pdev->dev, "Unsupported Speed/Duplex configuration\n");
4332 * igc_probe - Device Initialization Routine
4333 * @pdev: PCI device information struct
4334 * @ent: entry in igc_pci_tbl
4336 * Returns 0 on success, negative on failure
4338 * igc_probe initializes an adapter identified by a pci_dev structure.
4339 * The OS initialization, configuring the adapter private structure,
4340 * and a hardware reset occur.
4342 static int igc_probe(struct pci_dev *pdev,
4343 const struct pci_device_id *ent)
4345 struct igc_adapter *adapter;
4346 struct net_device *netdev;
4348 const struct igc_info *ei = igc_info_tbl[ent->driver_data];
4351 err = pci_enable_device_mem(pdev);
4355 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
4357 err = dma_set_coherent_mask(&pdev->dev,
4360 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
4362 err = dma_set_coherent_mask(&pdev->dev,
4365 dev_err(&pdev->dev, "igc: Wrong DMA config\n");
4371 err = pci_request_selected_regions(pdev,
4372 pci_select_bars(pdev,
4378 pci_enable_pcie_error_reporting(pdev);
4380 pci_set_master(pdev);
4383 netdev = alloc_etherdev_mq(sizeof(struct igc_adapter),
4387 goto err_alloc_etherdev;
4389 SET_NETDEV_DEV(netdev, &pdev->dev);
4391 pci_set_drvdata(pdev, netdev);
4392 adapter = netdev_priv(netdev);
4393 adapter->netdev = netdev;
4394 adapter->pdev = pdev;
4397 adapter->port_num = hw->bus.func;
4398 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
4400 err = pci_save_state(pdev);
4405 adapter->io_addr = ioremap(pci_resource_start(pdev, 0),
4406 pci_resource_len(pdev, 0));
4407 if (!adapter->io_addr)
4410 /* hw->hw_addr can be zeroed, so use adapter->io_addr for unmap */
4411 hw->hw_addr = adapter->io_addr;
4413 netdev->netdev_ops = &igc_netdev_ops;
4414 igc_set_ethtool_ops(netdev);
4415 netdev->watchdog_timeo = 5 * HZ;
4417 netdev->mem_start = pci_resource_start(pdev, 0);
4418 netdev->mem_end = pci_resource_end(pdev, 0);
4420 /* PCI config space info */
4421 hw->vendor_id = pdev->vendor;
4422 hw->device_id = pdev->device;
4423 hw->revision_id = pdev->revision;
4424 hw->subsystem_vendor_id = pdev->subsystem_vendor;
4425 hw->subsystem_device_id = pdev->subsystem_device;
4427 /* Copy the default MAC and PHY function pointers */
4428 memcpy(&hw->mac.ops, ei->mac_ops, sizeof(hw->mac.ops));
4429 memcpy(&hw->phy.ops, ei->phy_ops, sizeof(hw->phy.ops));
4431 /* Initialize skew-specific constants */
4432 err = ei->get_invariants(hw);
4436 /* Add supported features to the features list*/
4437 netdev->features |= NETIF_F_RXCSUM;
4438 netdev->features |= NETIF_F_HW_CSUM;
4439 netdev->features |= NETIF_F_SCTP_CRC;
4441 /* setup the private structure */
4442 err = igc_sw_init(adapter);
4446 /* copy netdev features into list of user selectable features */
4447 netdev->hw_features |= NETIF_F_NTUPLE;
4448 netdev->hw_features |= netdev->features;
4450 /* MTU range: 68 - 9216 */
4451 netdev->min_mtu = ETH_MIN_MTU;
4452 netdev->max_mtu = MAX_STD_JUMBO_FRAME_SIZE;
4454 /* before reading the NVM, reset the controller to put the device in a
4455 * known good starting state
4457 hw->mac.ops.reset_hw(hw);
4459 if (igc_get_flash_presence_i225(hw)) {
4460 if (hw->nvm.ops.validate(hw) < 0) {
4462 "The NVM Checksum Is Not Valid\n");
4468 if (eth_platform_get_mac_address(&pdev->dev, hw->mac.addr)) {
4469 /* copy the MAC address out of the NVM */
4470 if (hw->mac.ops.read_mac_addr(hw))
4471 dev_err(&pdev->dev, "NVM Read Error\n");
4474 memcpy(netdev->dev_addr, hw->mac.addr, netdev->addr_len);
4476 if (!is_valid_ether_addr(netdev->dev_addr)) {
4477 dev_err(&pdev->dev, "Invalid MAC Address\n");
4482 /* configure RXPBSIZE and TXPBSIZE */
4483 wr32(IGC_RXPBS, I225_RXPBSIZE_DEFAULT);
4484 wr32(IGC_TXPBS, I225_TXPBSIZE_DEFAULT);
4486 timer_setup(&adapter->watchdog_timer, igc_watchdog, 0);
4487 timer_setup(&adapter->phy_info_timer, igc_update_phy_info, 0);
4489 INIT_WORK(&adapter->reset_task, igc_reset_task);
4490 INIT_WORK(&adapter->watchdog_task, igc_watchdog_task);
4492 /* Initialize link properties that are user-changeable */
4493 adapter->fc_autoneg = true;
4494 hw->mac.autoneg = true;
4495 hw->phy.autoneg_advertised = 0xaf;
4497 hw->fc.requested_mode = igc_fc_default;
4498 hw->fc.current_mode = igc_fc_default;
4500 /* reset the hardware with the new settings */
4503 /* let the f/w know that the h/w is now under the control of the
4506 igc_get_hw_control(adapter);
4508 strncpy(netdev->name, "eth%d", IFNAMSIZ);
4509 err = register_netdev(netdev);
4513 /* carrier off reporting is important to ethtool even BEFORE open */
4514 netif_carrier_off(netdev);
4516 /* Check if Media Autosense is enabled */
4519 /* print pcie link status and MAC address */
4520 pcie_print_link_status(pdev);
4521 netdev_info(netdev, "MAC: %pM\n", netdev->dev_addr);
4526 igc_release_hw_control(adapter);
4528 if (!igc_check_reset_block(hw))
4531 igc_clear_interrupt_scheme(adapter);
4532 iounmap(adapter->io_addr);
4534 free_netdev(netdev);
4536 pci_release_selected_regions(pdev,
4537 pci_select_bars(pdev, IORESOURCE_MEM));
4540 pci_disable_device(pdev);
4545 * igc_remove - Device Removal Routine
4546 * @pdev: PCI device information struct
4548 * igc_remove is called by the PCI subsystem to alert the driver
4549 * that it should release a PCI device. This could be caused by a
4550 * Hot-Plug event, or because the driver is going to be removed from
4553 static void igc_remove(struct pci_dev *pdev)
4555 struct net_device *netdev = pci_get_drvdata(pdev);
4556 struct igc_adapter *adapter = netdev_priv(netdev);
4558 set_bit(__IGC_DOWN, &adapter->state);
4560 del_timer_sync(&adapter->watchdog_timer);
4561 del_timer_sync(&adapter->phy_info_timer);
4563 cancel_work_sync(&adapter->reset_task);
4564 cancel_work_sync(&adapter->watchdog_task);
4566 /* Release control of h/w to f/w. If f/w is AMT enabled, this
4567 * would have already happened in close and is redundant.
4569 igc_release_hw_control(adapter);
4570 unregister_netdev(netdev);
4572 igc_clear_interrupt_scheme(adapter);
4573 pci_iounmap(pdev, adapter->io_addr);
4574 pci_release_mem_regions(pdev);
4576 kfree(adapter->mac_table);
4577 free_netdev(netdev);
4579 pci_disable_pcie_error_reporting(pdev);
4581 pci_disable_device(pdev);
4584 static struct pci_driver igc_driver = {
4585 .name = igc_driver_name,
4586 .id_table = igc_pci_tbl,
4588 .remove = igc_remove,
4591 void igc_set_flag_queue_pairs(struct igc_adapter *adapter,
4592 const u32 max_rss_queues)
4594 /* Determine if we need to pair queues. */
4595 /* If rss_queues > half of max_rss_queues, pair the queues in
4596 * order to conserve interrupts due to limited supply.
4598 if (adapter->rss_queues > (max_rss_queues / 2))
4599 adapter->flags |= IGC_FLAG_QUEUE_PAIRS;
4601 adapter->flags &= ~IGC_FLAG_QUEUE_PAIRS;
4604 unsigned int igc_get_max_rss_queues(struct igc_adapter *adapter)
4606 unsigned int max_rss_queues;
4608 /* Determine the maximum number of RSS queues supported. */
4609 max_rss_queues = IGC_MAX_RX_QUEUES;
4611 return max_rss_queues;
4614 static void igc_init_queue_configuration(struct igc_adapter *adapter)
4618 max_rss_queues = igc_get_max_rss_queues(adapter);
4619 adapter->rss_queues = min_t(u32, max_rss_queues, num_online_cpus());
4621 igc_set_flag_queue_pairs(adapter, max_rss_queues);
4625 * igc_sw_init - Initialize general software structures (struct igc_adapter)
4626 * @adapter: board private structure to initialize
4628 * igc_sw_init initializes the Adapter private data structure.
4629 * Fields are initialized based on PCI device information and
4630 * OS network device settings (MTU size).
4632 static int igc_sw_init(struct igc_adapter *adapter)
4634 struct net_device *netdev = adapter->netdev;
4635 struct pci_dev *pdev = adapter->pdev;
4636 struct igc_hw *hw = &adapter->hw;
4638 int size = sizeof(struct igc_mac_addr) * hw->mac.rar_entry_count;
4640 pci_read_config_word(pdev, PCI_COMMAND, &hw->bus.pci_cmd_word);
4642 /* set default ring sizes */
4643 adapter->tx_ring_count = IGC_DEFAULT_TXD;
4644 adapter->rx_ring_count = IGC_DEFAULT_RXD;
4646 /* set default ITR values */
4647 adapter->rx_itr_setting = IGC_DEFAULT_ITR;
4648 adapter->tx_itr_setting = IGC_DEFAULT_ITR;
4650 /* set default work limits */
4651 adapter->tx_work_limit = IGC_DEFAULT_TX_WORK;
4653 /* adjust max frame to be at least the size of a standard frame */
4654 adapter->max_frame_size = netdev->mtu + ETH_HLEN + ETH_FCS_LEN +
4656 adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
4658 spin_lock_init(&adapter->nfc_lock);
4659 spin_lock_init(&adapter->stats64_lock);
4660 /* Assume MSI-X interrupts, will be checked during IRQ allocation */
4661 adapter->flags |= IGC_FLAG_HAS_MSIX;
4663 adapter->mac_table = kzalloc(size, GFP_ATOMIC);
4664 if (!adapter->mac_table)
4667 igc_init_queue_configuration(adapter);
4669 /* This call may decrease the number of queues */
4670 if (igc_init_interrupt_scheme(adapter, true)) {
4671 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
4675 /* Explicitly disable IRQ since the NIC can be in any state. */
4676 igc_irq_disable(adapter);
4678 set_bit(__IGC_DOWN, &adapter->state);
4684 * igc_reinit_queues - return error
4685 * @adapter: pointer to adapter structure
4687 int igc_reinit_queues(struct igc_adapter *adapter)
4689 struct net_device *netdev = adapter->netdev;
4690 struct pci_dev *pdev = adapter->pdev;
4693 if (netif_running(netdev))
4696 igc_reset_interrupt_capability(adapter);
4698 if (igc_init_interrupt_scheme(adapter, true)) {
4699 dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
4703 if (netif_running(netdev))
4704 err = igc_open(netdev);
4710 * igc_get_hw_dev - return device
4711 * @hw: pointer to hardware structure
4713 * used by hardware layer to print debugging information
4715 struct net_device *igc_get_hw_dev(struct igc_hw *hw)
4717 struct igc_adapter *adapter = hw->back;
4719 return adapter->netdev;
4723 * igc_init_module - Driver Registration Routine
4725 * igc_init_module is the first routine called when the driver is
4726 * loaded. All it does is register with the PCI subsystem.
4728 static int __init igc_init_module(void)
4732 pr_info("%s - version %s\n",
4733 igc_driver_string, igc_driver_version);
4735 pr_info("%s\n", igc_copyright);
4737 ret = pci_register_driver(&igc_driver);
4741 module_init(igc_init_module);
4744 * igc_exit_module - Driver Exit Cleanup Routine
4746 * igc_exit_module is called just before the driver is removed
4749 static void __exit igc_exit_module(void)
4751 pci_unregister_driver(&igc_driver);
4754 module_exit(igc_exit_module);