1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018 Intel Corporation */
4 #include <linux/module.h>
5 #include <linux/types.h>
6 #include <linux/if_vlan.h>
11 #include <linux/pm_runtime.h>
12 #include <net/pkt_sched.h>
13 #include <linux/bpf_trace.h>
14 #include <net/xdp_sock_drv.h>
15 #include <linux/pci.h>
24 #define DRV_SUMMARY "Intel(R) 2.5G Ethernet Linux Driver"
26 #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
28 #define IGC_XDP_PASS 0
29 #define IGC_XDP_CONSUMED BIT(0)
30 #define IGC_XDP_TX BIT(1)
31 #define IGC_XDP_REDIRECT BIT(2)
33 static int debug = -1;
35 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
36 MODULE_DESCRIPTION(DRV_SUMMARY);
37 MODULE_LICENSE("GPL v2");
38 module_param(debug, int, 0);
39 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
41 char igc_driver_name[] = "igc";
42 static const char igc_driver_string[] = DRV_SUMMARY;
43 static const char igc_copyright[] =
44 "Copyright(c) 2018 Intel Corporation.";
46 static const struct igc_info *igc_info_tbl[] = {
47 [board_base] = &igc_base_info,
50 static const struct pci_device_id igc_pci_tbl[] = {
51 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_LM), board_base },
52 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_V), board_base },
53 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_I), board_base },
54 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I220_V), board_base },
55 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_K), board_base },
56 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_K2), board_base },
57 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I226_K), board_base },
58 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_LMVP), board_base },
59 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_IT), board_base },
60 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I226_LM), board_base },
61 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I226_V), board_base },
62 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I226_IT), board_base },
63 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I221_V), board_base },
64 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I226_BLANK_NVM), board_base },
65 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_BLANK_NVM), board_base },
66 /* required last entry */
70 MODULE_DEVICE_TABLE(pci, igc_pci_tbl);
79 void igc_reset(struct igc_adapter *adapter)
81 struct net_device *dev = adapter->netdev;
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 netdev_err(dev, "Error on hardware initialization\n");
110 /* Re-establish EEE setting */
111 igc_set_eee_i225(hw, true, true, true);
113 if (!netif_running(adapter->netdev))
114 igc_power_down_phy_copper_base(&adapter->hw);
116 /* Enable HW to recognize an 802.1Q VLAN Ethernet packet */
117 wr32(IGC_VET, ETH_P_8021Q);
119 /* Re-enable PTP, where applicable. */
120 igc_ptp_reset(adapter);
122 /* Re-enable TSN offloading, where applicable. */
123 igc_tsn_offload_apply(adapter);
125 igc_get_phy_info(hw);
129 * igc_power_up_link - Power up the phy link
130 * @adapter: address of board private structure
132 static void igc_power_up_link(struct igc_adapter *adapter)
134 igc_reset_phy(&adapter->hw);
136 igc_power_up_phy_copper(&adapter->hw);
138 igc_setup_link(&adapter->hw);
142 * igc_release_hw_control - release control of the h/w to f/w
143 * @adapter: address of board private structure
145 * igc_release_hw_control resets CTRL_EXT:DRV_LOAD bit.
146 * For ASF and Pass Through versions of f/w this means that the
147 * driver is no longer loaded.
149 static void igc_release_hw_control(struct igc_adapter *adapter)
151 struct igc_hw *hw = &adapter->hw;
154 if (!pci_device_is_present(adapter->pdev))
157 /* Let firmware take over control of h/w */
158 ctrl_ext = rd32(IGC_CTRL_EXT);
160 ctrl_ext & ~IGC_CTRL_EXT_DRV_LOAD);
164 * igc_get_hw_control - get control of the h/w from f/w
165 * @adapter: address of board private structure
167 * igc_get_hw_control sets CTRL_EXT:DRV_LOAD bit.
168 * For ASF and Pass Through versions of f/w this means that
169 * the driver is loaded.
171 static void igc_get_hw_control(struct igc_adapter *adapter)
173 struct igc_hw *hw = &adapter->hw;
176 /* Let firmware know the driver has taken over */
177 ctrl_ext = rd32(IGC_CTRL_EXT);
179 ctrl_ext | IGC_CTRL_EXT_DRV_LOAD);
182 static void igc_unmap_tx_buffer(struct device *dev, struct igc_tx_buffer *buf)
184 dma_unmap_single(dev, dma_unmap_addr(buf, dma),
185 dma_unmap_len(buf, len), DMA_TO_DEVICE);
187 dma_unmap_len_set(buf, len, 0);
191 * igc_clean_tx_ring - Free Tx Buffers
192 * @tx_ring: ring to be cleaned
194 static void igc_clean_tx_ring(struct igc_ring *tx_ring)
196 u16 i = tx_ring->next_to_clean;
197 struct igc_tx_buffer *tx_buffer = &tx_ring->tx_buffer_info[i];
200 while (i != tx_ring->next_to_use) {
201 union igc_adv_tx_desc *eop_desc, *tx_desc;
203 switch (tx_buffer->type) {
204 case IGC_TX_BUFFER_TYPE_XSK:
207 case IGC_TX_BUFFER_TYPE_XDP:
208 xdp_return_frame(tx_buffer->xdpf);
209 igc_unmap_tx_buffer(tx_ring->dev, tx_buffer);
211 case IGC_TX_BUFFER_TYPE_SKB:
212 dev_kfree_skb_any(tx_buffer->skb);
213 igc_unmap_tx_buffer(tx_ring->dev, tx_buffer);
216 netdev_warn_once(tx_ring->netdev, "Unknown Tx buffer type\n");
220 /* check for eop_desc to determine the end of the packet */
221 eop_desc = tx_buffer->next_to_watch;
222 tx_desc = IGC_TX_DESC(tx_ring, i);
224 /* unmap remaining buffers */
225 while (tx_desc != eop_desc) {
229 if (unlikely(i == tx_ring->count)) {
231 tx_buffer = tx_ring->tx_buffer_info;
232 tx_desc = IGC_TX_DESC(tx_ring, 0);
235 /* unmap any remaining paged data */
236 if (dma_unmap_len(tx_buffer, len))
237 igc_unmap_tx_buffer(tx_ring->dev, tx_buffer);
240 tx_buffer->next_to_watch = NULL;
242 /* move us one more past the eop_desc for start of next pkt */
245 if (unlikely(i == tx_ring->count)) {
247 tx_buffer = tx_ring->tx_buffer_info;
251 if (tx_ring->xsk_pool && xsk_frames)
252 xsk_tx_completed(tx_ring->xsk_pool, xsk_frames);
254 /* reset BQL for queue */
255 netdev_tx_reset_queue(txring_txq(tx_ring));
257 /* reset next_to_use and next_to_clean */
258 tx_ring->next_to_use = 0;
259 tx_ring->next_to_clean = 0;
263 * igc_free_tx_resources - Free Tx Resources per Queue
264 * @tx_ring: Tx descriptor ring for a specific queue
266 * Free all transmit software resources
268 void igc_free_tx_resources(struct igc_ring *tx_ring)
270 igc_clean_tx_ring(tx_ring);
272 vfree(tx_ring->tx_buffer_info);
273 tx_ring->tx_buffer_info = NULL;
275 /* if not set, then don't free */
279 dma_free_coherent(tx_ring->dev, tx_ring->size,
280 tx_ring->desc, tx_ring->dma);
282 tx_ring->desc = NULL;
286 * igc_free_all_tx_resources - Free Tx Resources for All Queues
287 * @adapter: board private structure
289 * Free all transmit software resources
291 static void igc_free_all_tx_resources(struct igc_adapter *adapter)
295 for (i = 0; i < adapter->num_tx_queues; i++)
296 igc_free_tx_resources(adapter->tx_ring[i]);
300 * igc_clean_all_tx_rings - Free Tx Buffers for all queues
301 * @adapter: board private structure
303 static void igc_clean_all_tx_rings(struct igc_adapter *adapter)
307 for (i = 0; i < adapter->num_tx_queues; i++)
308 if (adapter->tx_ring[i])
309 igc_clean_tx_ring(adapter->tx_ring[i]);
313 * igc_setup_tx_resources - allocate Tx resources (Descriptors)
314 * @tx_ring: tx descriptor ring (for a specific queue) to setup
316 * Return 0 on success, negative on failure
318 int igc_setup_tx_resources(struct igc_ring *tx_ring)
320 struct net_device *ndev = tx_ring->netdev;
321 struct device *dev = tx_ring->dev;
324 size = sizeof(struct igc_tx_buffer) * tx_ring->count;
325 tx_ring->tx_buffer_info = vzalloc(size);
326 if (!tx_ring->tx_buffer_info)
329 /* round up to nearest 4K */
330 tx_ring->size = tx_ring->count * sizeof(union igc_adv_tx_desc);
331 tx_ring->size = ALIGN(tx_ring->size, 4096);
333 tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
334 &tx_ring->dma, GFP_KERNEL);
339 tx_ring->next_to_use = 0;
340 tx_ring->next_to_clean = 0;
345 vfree(tx_ring->tx_buffer_info);
346 netdev_err(ndev, "Unable to allocate memory for Tx descriptor ring\n");
351 * igc_setup_all_tx_resources - wrapper to allocate Tx resources for all queues
352 * @adapter: board private structure
354 * Return 0 on success, negative on failure
356 static int igc_setup_all_tx_resources(struct igc_adapter *adapter)
358 struct net_device *dev = adapter->netdev;
361 for (i = 0; i < adapter->num_tx_queues; i++) {
362 err = igc_setup_tx_resources(adapter->tx_ring[i]);
364 netdev_err(dev, "Error on Tx queue %u setup\n", i);
365 for (i--; i >= 0; i--)
366 igc_free_tx_resources(adapter->tx_ring[i]);
374 static void igc_clean_rx_ring_page_shared(struct igc_ring *rx_ring)
376 u16 i = rx_ring->next_to_clean;
378 dev_kfree_skb(rx_ring->skb);
381 /* Free all the Rx ring sk_buffs */
382 while (i != rx_ring->next_to_alloc) {
383 struct igc_rx_buffer *buffer_info = &rx_ring->rx_buffer_info[i];
385 /* Invalidate cache lines that may have been written to by
386 * device so that we avoid corrupting memory.
388 dma_sync_single_range_for_cpu(rx_ring->dev,
390 buffer_info->page_offset,
391 igc_rx_bufsz(rx_ring),
394 /* free resources associated with mapping */
395 dma_unmap_page_attrs(rx_ring->dev,
397 igc_rx_pg_size(rx_ring),
400 __page_frag_cache_drain(buffer_info->page,
401 buffer_info->pagecnt_bias);
404 if (i == rx_ring->count)
409 static void igc_clean_rx_ring_xsk_pool(struct igc_ring *ring)
411 struct igc_rx_buffer *bi;
414 for (i = 0; i < ring->count; i++) {
415 bi = &ring->rx_buffer_info[i];
419 xsk_buff_free(bi->xdp);
425 * igc_clean_rx_ring - Free Rx Buffers per Queue
426 * @ring: ring to free buffers from
428 static void igc_clean_rx_ring(struct igc_ring *ring)
431 igc_clean_rx_ring_xsk_pool(ring);
433 igc_clean_rx_ring_page_shared(ring);
435 clear_ring_uses_large_buffer(ring);
437 ring->next_to_alloc = 0;
438 ring->next_to_clean = 0;
439 ring->next_to_use = 0;
443 * igc_clean_all_rx_rings - Free Rx Buffers for all queues
444 * @adapter: board private structure
446 static void igc_clean_all_rx_rings(struct igc_adapter *adapter)
450 for (i = 0; i < adapter->num_rx_queues; i++)
451 if (adapter->rx_ring[i])
452 igc_clean_rx_ring(adapter->rx_ring[i]);
456 * igc_free_rx_resources - Free Rx Resources
457 * @rx_ring: ring to clean the resources from
459 * Free all receive software resources
461 void igc_free_rx_resources(struct igc_ring *rx_ring)
463 igc_clean_rx_ring(rx_ring);
465 xdp_rxq_info_unreg(&rx_ring->xdp_rxq);
467 vfree(rx_ring->rx_buffer_info);
468 rx_ring->rx_buffer_info = NULL;
470 /* if not set, then don't free */
474 dma_free_coherent(rx_ring->dev, rx_ring->size,
475 rx_ring->desc, rx_ring->dma);
477 rx_ring->desc = NULL;
481 * igc_free_all_rx_resources - Free Rx Resources for All Queues
482 * @adapter: board private structure
484 * Free all receive software resources
486 static void igc_free_all_rx_resources(struct igc_adapter *adapter)
490 for (i = 0; i < adapter->num_rx_queues; i++)
491 igc_free_rx_resources(adapter->rx_ring[i]);
495 * igc_setup_rx_resources - allocate Rx resources (Descriptors)
496 * @rx_ring: rx descriptor ring (for a specific queue) to setup
498 * Returns 0 on success, negative on failure
500 int igc_setup_rx_resources(struct igc_ring *rx_ring)
502 struct net_device *ndev = rx_ring->netdev;
503 struct device *dev = rx_ring->dev;
504 u8 index = rx_ring->queue_index;
505 int size, desc_len, res;
507 res = xdp_rxq_info_reg(&rx_ring->xdp_rxq, ndev, index,
508 rx_ring->q_vector->napi.napi_id);
510 netdev_err(ndev, "Failed to register xdp_rxq index %u\n",
515 size = sizeof(struct igc_rx_buffer) * rx_ring->count;
516 rx_ring->rx_buffer_info = vzalloc(size);
517 if (!rx_ring->rx_buffer_info)
520 desc_len = sizeof(union igc_adv_rx_desc);
522 /* Round up to nearest 4K */
523 rx_ring->size = rx_ring->count * desc_len;
524 rx_ring->size = ALIGN(rx_ring->size, 4096);
526 rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
527 &rx_ring->dma, GFP_KERNEL);
532 rx_ring->next_to_alloc = 0;
533 rx_ring->next_to_clean = 0;
534 rx_ring->next_to_use = 0;
539 xdp_rxq_info_unreg(&rx_ring->xdp_rxq);
540 vfree(rx_ring->rx_buffer_info);
541 rx_ring->rx_buffer_info = NULL;
542 netdev_err(ndev, "Unable to allocate memory for Rx descriptor ring\n");
547 * igc_setup_all_rx_resources - wrapper to allocate Rx resources
548 * (Descriptors) for all queues
549 * @adapter: board private structure
551 * Return 0 on success, negative on failure
553 static int igc_setup_all_rx_resources(struct igc_adapter *adapter)
555 struct net_device *dev = adapter->netdev;
558 for (i = 0; i < adapter->num_rx_queues; i++) {
559 err = igc_setup_rx_resources(adapter->rx_ring[i]);
561 netdev_err(dev, "Error on Rx queue %u setup\n", i);
562 for (i--; i >= 0; i--)
563 igc_free_rx_resources(adapter->rx_ring[i]);
571 static struct xsk_buff_pool *igc_get_xsk_pool(struct igc_adapter *adapter,
572 struct igc_ring *ring)
574 if (!igc_xdp_is_enabled(adapter) ||
575 !test_bit(IGC_RING_FLAG_AF_XDP_ZC, &ring->flags))
578 return xsk_get_pool_from_qid(ring->netdev, ring->queue_index);
582 * igc_configure_rx_ring - Configure a receive ring after Reset
583 * @adapter: board private structure
584 * @ring: receive ring to be configured
586 * Configure the Rx unit of the MAC after a reset.
588 static void igc_configure_rx_ring(struct igc_adapter *adapter,
589 struct igc_ring *ring)
591 struct igc_hw *hw = &adapter->hw;
592 union igc_adv_rx_desc *rx_desc;
593 int reg_idx = ring->reg_idx;
594 u32 srrctl = 0, rxdctl = 0;
595 u64 rdba = ring->dma;
598 xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq);
599 ring->xsk_pool = igc_get_xsk_pool(adapter, ring);
600 if (ring->xsk_pool) {
601 WARN_ON(xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
602 MEM_TYPE_XSK_BUFF_POOL,
604 xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
606 WARN_ON(xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
607 MEM_TYPE_PAGE_SHARED,
611 if (igc_xdp_is_enabled(adapter))
612 set_ring_uses_large_buffer(ring);
614 /* disable the queue */
615 wr32(IGC_RXDCTL(reg_idx), 0);
617 /* Set DMA base address registers */
618 wr32(IGC_RDBAL(reg_idx),
619 rdba & 0x00000000ffffffffULL);
620 wr32(IGC_RDBAH(reg_idx), rdba >> 32);
621 wr32(IGC_RDLEN(reg_idx),
622 ring->count * sizeof(union igc_adv_rx_desc));
624 /* initialize head and tail */
625 ring->tail = adapter->io_addr + IGC_RDT(reg_idx);
626 wr32(IGC_RDH(reg_idx), 0);
627 writel(0, ring->tail);
629 /* reset next-to- use/clean to place SW in sync with hardware */
630 ring->next_to_clean = 0;
631 ring->next_to_use = 0;
634 buf_size = xsk_pool_get_rx_frame_size(ring->xsk_pool);
635 else if (ring_uses_large_buffer(ring))
636 buf_size = IGC_RXBUFFER_3072;
638 buf_size = IGC_RXBUFFER_2048;
640 srrctl = IGC_RX_HDR_LEN << IGC_SRRCTL_BSIZEHDRSIZE_SHIFT;
641 srrctl |= buf_size >> IGC_SRRCTL_BSIZEPKT_SHIFT;
642 srrctl |= IGC_SRRCTL_DESCTYPE_ADV_ONEBUF;
644 wr32(IGC_SRRCTL(reg_idx), srrctl);
646 rxdctl |= IGC_RX_PTHRESH;
647 rxdctl |= IGC_RX_HTHRESH << 8;
648 rxdctl |= IGC_RX_WTHRESH << 16;
650 /* initialize rx_buffer_info */
651 memset(ring->rx_buffer_info, 0,
652 sizeof(struct igc_rx_buffer) * ring->count);
654 /* initialize Rx descriptor 0 */
655 rx_desc = IGC_RX_DESC(ring, 0);
656 rx_desc->wb.upper.length = 0;
658 /* enable receive descriptor fetching */
659 rxdctl |= IGC_RXDCTL_QUEUE_ENABLE;
661 wr32(IGC_RXDCTL(reg_idx), rxdctl);
665 * igc_configure_rx - Configure receive Unit after Reset
666 * @adapter: board private structure
668 * Configure the Rx unit of the MAC after a reset.
670 static void igc_configure_rx(struct igc_adapter *adapter)
674 /* Setup the HW Rx Head and Tail Descriptor Pointers and
675 * the Base and Length of the Rx Descriptor Ring
677 for (i = 0; i < adapter->num_rx_queues; i++)
678 igc_configure_rx_ring(adapter, adapter->rx_ring[i]);
682 * igc_configure_tx_ring - Configure transmit ring after Reset
683 * @adapter: board private structure
684 * @ring: tx ring to configure
686 * Configure a transmit ring after a reset.
688 static void igc_configure_tx_ring(struct igc_adapter *adapter,
689 struct igc_ring *ring)
691 struct igc_hw *hw = &adapter->hw;
692 int reg_idx = ring->reg_idx;
693 u64 tdba = ring->dma;
696 ring->xsk_pool = igc_get_xsk_pool(adapter, ring);
698 /* disable the queue */
699 wr32(IGC_TXDCTL(reg_idx), 0);
703 wr32(IGC_TDLEN(reg_idx),
704 ring->count * sizeof(union igc_adv_tx_desc));
705 wr32(IGC_TDBAL(reg_idx),
706 tdba & 0x00000000ffffffffULL);
707 wr32(IGC_TDBAH(reg_idx), tdba >> 32);
709 ring->tail = adapter->io_addr + IGC_TDT(reg_idx);
710 wr32(IGC_TDH(reg_idx), 0);
711 writel(0, ring->tail);
713 txdctl |= IGC_TX_PTHRESH;
714 txdctl |= IGC_TX_HTHRESH << 8;
715 txdctl |= IGC_TX_WTHRESH << 16;
717 txdctl |= IGC_TXDCTL_QUEUE_ENABLE;
718 wr32(IGC_TXDCTL(reg_idx), txdctl);
722 * igc_configure_tx - Configure transmit Unit after Reset
723 * @adapter: board private structure
725 * Configure the Tx unit of the MAC after a reset.
727 static void igc_configure_tx(struct igc_adapter *adapter)
731 for (i = 0; i < adapter->num_tx_queues; i++)
732 igc_configure_tx_ring(adapter, adapter->tx_ring[i]);
736 * igc_setup_mrqc - configure the multiple receive queue control registers
737 * @adapter: Board private structure
739 static void igc_setup_mrqc(struct igc_adapter *adapter)
741 struct igc_hw *hw = &adapter->hw;
742 u32 j, num_rx_queues;
746 netdev_rss_key_fill(rss_key, sizeof(rss_key));
747 for (j = 0; j < 10; j++)
748 wr32(IGC_RSSRK(j), rss_key[j]);
750 num_rx_queues = adapter->rss_queues;
752 if (adapter->rss_indir_tbl_init != num_rx_queues) {
753 for (j = 0; j < IGC_RETA_SIZE; j++)
754 adapter->rss_indir_tbl[j] =
755 (j * num_rx_queues) / IGC_RETA_SIZE;
756 adapter->rss_indir_tbl_init = num_rx_queues;
758 igc_write_rss_indir_tbl(adapter);
760 /* Disable raw packet checksumming so that RSS hash is placed in
761 * descriptor on writeback. No need to enable TCP/UDP/IP checksum
762 * offloads as they are enabled by default
764 rxcsum = rd32(IGC_RXCSUM);
765 rxcsum |= IGC_RXCSUM_PCSD;
767 /* Enable Receive Checksum Offload for SCTP */
768 rxcsum |= IGC_RXCSUM_CRCOFL;
770 /* Don't need to set TUOFL or IPOFL, they default to 1 */
771 wr32(IGC_RXCSUM, rxcsum);
773 /* Generate RSS hash based on packet types, TCP/UDP
774 * port numbers and/or IPv4/v6 src and dst addresses
776 mrqc = IGC_MRQC_RSS_FIELD_IPV4 |
777 IGC_MRQC_RSS_FIELD_IPV4_TCP |
778 IGC_MRQC_RSS_FIELD_IPV6 |
779 IGC_MRQC_RSS_FIELD_IPV6_TCP |
780 IGC_MRQC_RSS_FIELD_IPV6_TCP_EX;
782 if (adapter->flags & IGC_FLAG_RSS_FIELD_IPV4_UDP)
783 mrqc |= IGC_MRQC_RSS_FIELD_IPV4_UDP;
784 if (adapter->flags & IGC_FLAG_RSS_FIELD_IPV6_UDP)
785 mrqc |= IGC_MRQC_RSS_FIELD_IPV6_UDP;
787 mrqc |= IGC_MRQC_ENABLE_RSS_MQ;
789 wr32(IGC_MRQC, mrqc);
793 * igc_setup_rctl - configure the receive control registers
794 * @adapter: Board private structure
796 static void igc_setup_rctl(struct igc_adapter *adapter)
798 struct igc_hw *hw = &adapter->hw;
801 rctl = rd32(IGC_RCTL);
803 rctl &= ~(3 << IGC_RCTL_MO_SHIFT);
804 rctl &= ~(IGC_RCTL_LBM_TCVR | IGC_RCTL_LBM_MAC);
806 rctl |= IGC_RCTL_EN | IGC_RCTL_BAM | IGC_RCTL_RDMTS_HALF |
807 (hw->mac.mc_filter_type << IGC_RCTL_MO_SHIFT);
809 /* enable stripping of CRC. Newer features require
810 * that the HW strips the CRC.
812 rctl |= IGC_RCTL_SECRC;
814 /* disable store bad packets and clear size bits. */
815 rctl &= ~(IGC_RCTL_SBP | IGC_RCTL_SZ_256);
817 /* enable LPE to allow for reception of jumbo frames */
818 rctl |= IGC_RCTL_LPE;
820 /* disable queue 0 to prevent tail write w/o re-config */
821 wr32(IGC_RXDCTL(0), 0);
823 /* This is useful for sniffing bad packets. */
824 if (adapter->netdev->features & NETIF_F_RXALL) {
825 /* UPE and MPE will be handled by normal PROMISC logic
828 rctl |= (IGC_RCTL_SBP | /* Receive bad packets */
829 IGC_RCTL_BAM | /* RX All Bcast Pkts */
830 IGC_RCTL_PMCF); /* RX All MAC Ctrl Pkts */
832 rctl &= ~(IGC_RCTL_DPF | /* Allow filtered pause */
833 IGC_RCTL_CFIEN); /* Disable VLAN CFIEN Filter */
836 wr32(IGC_RCTL, rctl);
840 * igc_setup_tctl - configure the transmit control registers
841 * @adapter: Board private structure
843 static void igc_setup_tctl(struct igc_adapter *adapter)
845 struct igc_hw *hw = &adapter->hw;
848 /* disable queue 0 which icould be enabled by default */
849 wr32(IGC_TXDCTL(0), 0);
851 /* Program the Transmit Control Register */
852 tctl = rd32(IGC_TCTL);
853 tctl &= ~IGC_TCTL_CT;
854 tctl |= IGC_TCTL_PSP | IGC_TCTL_RTLC |
855 (IGC_COLLISION_THRESHOLD << IGC_CT_SHIFT);
857 /* Enable transmits */
860 wr32(IGC_TCTL, tctl);
864 * igc_set_mac_filter_hw() - Set MAC address filter in hardware
865 * @adapter: Pointer to adapter where the filter should be set
866 * @index: Filter index
867 * @type: MAC address filter type (source or destination)
869 * @queue: If non-negative, queue assignment feature is enabled and frames
870 * matching the filter are enqueued onto 'queue'. Otherwise, queue
871 * assignment is disabled.
873 static void igc_set_mac_filter_hw(struct igc_adapter *adapter, int index,
874 enum igc_mac_filter_type type,
875 const u8 *addr, int queue)
877 struct net_device *dev = adapter->netdev;
878 struct igc_hw *hw = &adapter->hw;
881 if (WARN_ON(index >= hw->mac.rar_entry_count))
884 ral = le32_to_cpup((__le32 *)(addr));
885 rah = le16_to_cpup((__le16 *)(addr + 4));
887 if (type == IGC_MAC_FILTER_TYPE_SRC) {
888 rah &= ~IGC_RAH_ASEL_MASK;
889 rah |= IGC_RAH_ASEL_SRC_ADDR;
893 rah &= ~IGC_RAH_QSEL_MASK;
894 rah |= (queue << IGC_RAH_QSEL_SHIFT);
895 rah |= IGC_RAH_QSEL_ENABLE;
900 wr32(IGC_RAL(index), ral);
901 wr32(IGC_RAH(index), rah);
903 netdev_dbg(dev, "MAC address filter set in HW: index %d", index);
907 * igc_clear_mac_filter_hw() - Clear MAC address filter in hardware
908 * @adapter: Pointer to adapter where the filter should be cleared
909 * @index: Filter index
911 static void igc_clear_mac_filter_hw(struct igc_adapter *adapter, int index)
913 struct net_device *dev = adapter->netdev;
914 struct igc_hw *hw = &adapter->hw;
916 if (WARN_ON(index >= hw->mac.rar_entry_count))
919 wr32(IGC_RAL(index), 0);
920 wr32(IGC_RAH(index), 0);
922 netdev_dbg(dev, "MAC address filter cleared in HW: index %d", index);
925 /* Set default MAC address for the PF in the first RAR entry */
926 static void igc_set_default_mac_filter(struct igc_adapter *adapter)
928 struct net_device *dev = adapter->netdev;
929 u8 *addr = adapter->hw.mac.addr;
931 netdev_dbg(dev, "Set default MAC address filter: address %pM", addr);
933 igc_set_mac_filter_hw(adapter, 0, IGC_MAC_FILTER_TYPE_DST, addr, -1);
937 * igc_set_mac - Change the Ethernet Address of the NIC
938 * @netdev: network interface device structure
939 * @p: pointer to an address structure
941 * Returns 0 on success, negative on failure
943 static int igc_set_mac(struct net_device *netdev, void *p)
945 struct igc_adapter *adapter = netdev_priv(netdev);
946 struct igc_hw *hw = &adapter->hw;
947 struct sockaddr *addr = p;
949 if (!is_valid_ether_addr(addr->sa_data))
950 return -EADDRNOTAVAIL;
952 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
953 memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
955 /* set the correct pool for the new PF MAC address in entry 0 */
956 igc_set_default_mac_filter(adapter);
962 * igc_write_mc_addr_list - write multicast addresses to MTA
963 * @netdev: network interface device structure
965 * Writes multicast address list to the MTA hash table.
966 * Returns: -ENOMEM on failure
967 * 0 on no addresses written
968 * X on writing X addresses to MTA
970 static int igc_write_mc_addr_list(struct net_device *netdev)
972 struct igc_adapter *adapter = netdev_priv(netdev);
973 struct igc_hw *hw = &adapter->hw;
974 struct netdev_hw_addr *ha;
978 if (netdev_mc_empty(netdev)) {
979 /* nothing to program, so clear mc list */
980 igc_update_mc_addr_list(hw, NULL, 0);
984 mta_list = kcalloc(netdev_mc_count(netdev), 6, GFP_ATOMIC);
988 /* The shared function expects a packed array of only addresses. */
990 netdev_for_each_mc_addr(ha, netdev)
991 memcpy(mta_list + (i++ * ETH_ALEN), ha->addr, ETH_ALEN);
993 igc_update_mc_addr_list(hw, mta_list, i);
996 return netdev_mc_count(netdev);
999 static __le32 igc_tx_launchtime(struct igc_adapter *adapter, ktime_t txtime)
1001 ktime_t cycle_time = adapter->cycle_time;
1002 ktime_t base_time = adapter->base_time;
1005 /* FIXME: when using ETF together with taprio, we may have a
1006 * case where 'delta' is larger than the cycle_time, this may
1007 * cause problems if we don't read the current value of
1008 * IGC_BASET, as the value writen into the launchtime
1009 * descriptor field may be misinterpreted.
1011 div_s64_rem(ktime_sub_ns(txtime, base_time), cycle_time, &launchtime);
1013 return cpu_to_le32(launchtime);
1016 static void igc_tx_ctxtdesc(struct igc_ring *tx_ring,
1017 struct igc_tx_buffer *first,
1018 u32 vlan_macip_lens, u32 type_tucmd,
1021 struct igc_adv_tx_context_desc *context_desc;
1022 u16 i = tx_ring->next_to_use;
1024 context_desc = IGC_TX_CTXTDESC(tx_ring, i);
1027 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
1029 /* set bits to identify this as an advanced context descriptor */
1030 type_tucmd |= IGC_TXD_CMD_DEXT | IGC_ADVTXD_DTYP_CTXT;
1032 /* For i225, context index must be unique per ring. */
1033 if (test_bit(IGC_RING_FLAG_TX_CTX_IDX, &tx_ring->flags))
1034 mss_l4len_idx |= tx_ring->reg_idx << 4;
1036 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
1037 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd);
1038 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
1040 /* We assume there is always a valid Tx time available. Invalid times
1041 * should have been handled by the upper layers.
1043 if (tx_ring->launchtime_enable) {
1044 struct igc_adapter *adapter = netdev_priv(tx_ring->netdev);
1045 ktime_t txtime = first->skb->tstamp;
1047 skb_txtime_consumed(first->skb);
1048 context_desc->launch_time = igc_tx_launchtime(adapter,
1051 context_desc->launch_time = 0;
1055 static void igc_tx_csum(struct igc_ring *tx_ring, struct igc_tx_buffer *first)
1057 struct sk_buff *skb = first->skb;
1058 u32 vlan_macip_lens = 0;
1061 if (skb->ip_summed != CHECKSUM_PARTIAL) {
1063 if (!(first->tx_flags & IGC_TX_FLAGS_VLAN) &&
1064 !tx_ring->launchtime_enable)
1069 switch (skb->csum_offset) {
1070 case offsetof(struct tcphdr, check):
1071 type_tucmd = IGC_ADVTXD_TUCMD_L4T_TCP;
1073 case offsetof(struct udphdr, check):
1075 case offsetof(struct sctphdr, checksum):
1076 /* validate that this is actually an SCTP request */
1077 if (skb_csum_is_sctp(skb)) {
1078 type_tucmd = IGC_ADVTXD_TUCMD_L4T_SCTP;
1083 skb_checksum_help(skb);
1087 /* update TX checksum flag */
1088 first->tx_flags |= IGC_TX_FLAGS_CSUM;
1089 vlan_macip_lens = skb_checksum_start_offset(skb) -
1090 skb_network_offset(skb);
1092 vlan_macip_lens |= skb_network_offset(skb) << IGC_ADVTXD_MACLEN_SHIFT;
1093 vlan_macip_lens |= first->tx_flags & IGC_TX_FLAGS_VLAN_MASK;
1095 igc_tx_ctxtdesc(tx_ring, first, vlan_macip_lens, type_tucmd, 0);
1098 static int __igc_maybe_stop_tx(struct igc_ring *tx_ring, const u16 size)
1100 struct net_device *netdev = tx_ring->netdev;
1102 netif_stop_subqueue(netdev, tx_ring->queue_index);
1104 /* memory barriier comment */
1107 /* We need to check again in a case another CPU has just
1108 * made room available.
1110 if (igc_desc_unused(tx_ring) < size)
1114 netif_wake_subqueue(netdev, tx_ring->queue_index);
1116 u64_stats_update_begin(&tx_ring->tx_syncp2);
1117 tx_ring->tx_stats.restart_queue2++;
1118 u64_stats_update_end(&tx_ring->tx_syncp2);
1123 static inline int igc_maybe_stop_tx(struct igc_ring *tx_ring, const u16 size)
1125 if (igc_desc_unused(tx_ring) >= size)
1127 return __igc_maybe_stop_tx(tx_ring, size);
1130 #define IGC_SET_FLAG(_input, _flag, _result) \
1131 (((_flag) <= (_result)) ? \
1132 ((u32)((_input) & (_flag)) * ((_result) / (_flag))) : \
1133 ((u32)((_input) & (_flag)) / ((_flag) / (_result))))
1135 static u32 igc_tx_cmd_type(struct sk_buff *skb, u32 tx_flags)
1137 /* set type for advanced descriptor with frame checksum insertion */
1138 u32 cmd_type = IGC_ADVTXD_DTYP_DATA |
1139 IGC_ADVTXD_DCMD_DEXT |
1140 IGC_ADVTXD_DCMD_IFCS;
1142 /* set HW vlan bit if vlan is present */
1143 cmd_type |= IGC_SET_FLAG(tx_flags, IGC_TX_FLAGS_VLAN,
1144 IGC_ADVTXD_DCMD_VLE);
1146 /* set segmentation bits for TSO */
1147 cmd_type |= IGC_SET_FLAG(tx_flags, IGC_TX_FLAGS_TSO,
1148 (IGC_ADVTXD_DCMD_TSE));
1150 /* set timestamp bit if present */
1151 cmd_type |= IGC_SET_FLAG(tx_flags, IGC_TX_FLAGS_TSTAMP,
1152 (IGC_ADVTXD_MAC_TSTAMP));
1154 /* insert frame checksum */
1155 cmd_type ^= IGC_SET_FLAG(skb->no_fcs, 1, IGC_ADVTXD_DCMD_IFCS);
1160 static void igc_tx_olinfo_status(struct igc_ring *tx_ring,
1161 union igc_adv_tx_desc *tx_desc,
1162 u32 tx_flags, unsigned int paylen)
1164 u32 olinfo_status = paylen << IGC_ADVTXD_PAYLEN_SHIFT;
1166 /* insert L4 checksum */
1167 olinfo_status |= (tx_flags & IGC_TX_FLAGS_CSUM) *
1168 ((IGC_TXD_POPTS_TXSM << 8) /
1171 /* insert IPv4 checksum */
1172 olinfo_status |= (tx_flags & IGC_TX_FLAGS_IPV4) *
1173 (((IGC_TXD_POPTS_IXSM << 8)) /
1176 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
1179 static int igc_tx_map(struct igc_ring *tx_ring,
1180 struct igc_tx_buffer *first,
1183 struct sk_buff *skb = first->skb;
1184 struct igc_tx_buffer *tx_buffer;
1185 union igc_adv_tx_desc *tx_desc;
1186 u32 tx_flags = first->tx_flags;
1188 u16 i = tx_ring->next_to_use;
1189 unsigned int data_len, size;
1193 cmd_type = igc_tx_cmd_type(skb, tx_flags);
1194 tx_desc = IGC_TX_DESC(tx_ring, i);
1196 igc_tx_olinfo_status(tx_ring, tx_desc, tx_flags, skb->len - hdr_len);
1198 size = skb_headlen(skb);
1199 data_len = skb->data_len;
1201 dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
1205 for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
1206 if (dma_mapping_error(tx_ring->dev, dma))
1209 /* record length, and DMA address */
1210 dma_unmap_len_set(tx_buffer, len, size);
1211 dma_unmap_addr_set(tx_buffer, dma, dma);
1213 tx_desc->read.buffer_addr = cpu_to_le64(dma);
1215 while (unlikely(size > IGC_MAX_DATA_PER_TXD)) {
1216 tx_desc->read.cmd_type_len =
1217 cpu_to_le32(cmd_type ^ IGC_MAX_DATA_PER_TXD);
1221 if (i == tx_ring->count) {
1222 tx_desc = IGC_TX_DESC(tx_ring, 0);
1225 tx_desc->read.olinfo_status = 0;
1227 dma += IGC_MAX_DATA_PER_TXD;
1228 size -= IGC_MAX_DATA_PER_TXD;
1230 tx_desc->read.buffer_addr = cpu_to_le64(dma);
1233 if (likely(!data_len))
1236 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type ^ size);
1240 if (i == tx_ring->count) {
1241 tx_desc = IGC_TX_DESC(tx_ring, 0);
1244 tx_desc->read.olinfo_status = 0;
1246 size = skb_frag_size(frag);
1249 dma = skb_frag_dma_map(tx_ring->dev, frag, 0,
1250 size, DMA_TO_DEVICE);
1252 tx_buffer = &tx_ring->tx_buffer_info[i];
1255 /* write last descriptor with RS and EOP bits */
1256 cmd_type |= size | IGC_TXD_DCMD;
1257 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type);
1259 netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount);
1261 /* set the timestamp */
1262 first->time_stamp = jiffies;
1264 skb_tx_timestamp(skb);
1266 /* Force memory writes to complete before letting h/w know there
1267 * are new descriptors to fetch. (Only applicable for weak-ordered
1268 * memory model archs, such as IA-64).
1270 * We also need this memory barrier to make certain all of the
1271 * status bits have been updated before next_to_watch is written.
1275 /* set next_to_watch value indicating a packet is present */
1276 first->next_to_watch = tx_desc;
1279 if (i == tx_ring->count)
1282 tx_ring->next_to_use = i;
1284 /* Make sure there is space in the ring for the next send. */
1285 igc_maybe_stop_tx(tx_ring, DESC_NEEDED);
1287 if (netif_xmit_stopped(txring_txq(tx_ring)) || !netdev_xmit_more()) {
1288 writel(i, tx_ring->tail);
1293 netdev_err(tx_ring->netdev, "TX DMA map failed\n");
1294 tx_buffer = &tx_ring->tx_buffer_info[i];
1296 /* clear dma mappings for failed tx_buffer_info map */
1297 while (tx_buffer != first) {
1298 if (dma_unmap_len(tx_buffer, len))
1299 igc_unmap_tx_buffer(tx_ring->dev, tx_buffer);
1302 i += tx_ring->count;
1303 tx_buffer = &tx_ring->tx_buffer_info[i];
1306 if (dma_unmap_len(tx_buffer, len))
1307 igc_unmap_tx_buffer(tx_ring->dev, tx_buffer);
1309 dev_kfree_skb_any(tx_buffer->skb);
1310 tx_buffer->skb = NULL;
1312 tx_ring->next_to_use = i;
1317 static int igc_tso(struct igc_ring *tx_ring,
1318 struct igc_tx_buffer *first,
1321 u32 vlan_macip_lens, type_tucmd, mss_l4len_idx;
1322 struct sk_buff *skb = first->skb;
1333 u32 paylen, l4_offset;
1336 if (skb->ip_summed != CHECKSUM_PARTIAL)
1339 if (!skb_is_gso(skb))
1342 err = skb_cow_head(skb, 0);
1346 ip.hdr = skb_network_header(skb);
1347 l4.hdr = skb_checksum_start(skb);
1349 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
1350 type_tucmd = IGC_ADVTXD_TUCMD_L4T_TCP;
1352 /* initialize outer IP header fields */
1353 if (ip.v4->version == 4) {
1354 unsigned char *csum_start = skb_checksum_start(skb);
1355 unsigned char *trans_start = ip.hdr + (ip.v4->ihl * 4);
1357 /* IP header will have to cancel out any data that
1358 * is not a part of the outer IP header
1360 ip.v4->check = csum_fold(csum_partial(trans_start,
1361 csum_start - trans_start,
1363 type_tucmd |= IGC_ADVTXD_TUCMD_IPV4;
1366 first->tx_flags |= IGC_TX_FLAGS_TSO |
1370 ip.v6->payload_len = 0;
1371 first->tx_flags |= IGC_TX_FLAGS_TSO |
1375 /* determine offset of inner transport header */
1376 l4_offset = l4.hdr - skb->data;
1378 /* remove payload length from inner checksum */
1379 paylen = skb->len - l4_offset;
1380 if (type_tucmd & IGC_ADVTXD_TUCMD_L4T_TCP) {
1381 /* compute length of segmentation header */
1382 *hdr_len = (l4.tcp->doff * 4) + l4_offset;
1383 csum_replace_by_diff(&l4.tcp->check,
1384 (__force __wsum)htonl(paylen));
1386 /* compute length of segmentation header */
1387 *hdr_len = sizeof(*l4.udp) + l4_offset;
1388 csum_replace_by_diff(&l4.udp->check,
1389 (__force __wsum)htonl(paylen));
1392 /* update gso size and bytecount with header size */
1393 first->gso_segs = skb_shinfo(skb)->gso_segs;
1394 first->bytecount += (first->gso_segs - 1) * *hdr_len;
1397 mss_l4len_idx = (*hdr_len - l4_offset) << IGC_ADVTXD_L4LEN_SHIFT;
1398 mss_l4len_idx |= skb_shinfo(skb)->gso_size << IGC_ADVTXD_MSS_SHIFT;
1400 /* VLAN MACLEN IPLEN */
1401 vlan_macip_lens = l4.hdr - ip.hdr;
1402 vlan_macip_lens |= (ip.hdr - skb->data) << IGC_ADVTXD_MACLEN_SHIFT;
1403 vlan_macip_lens |= first->tx_flags & IGC_TX_FLAGS_VLAN_MASK;
1405 igc_tx_ctxtdesc(tx_ring, first, vlan_macip_lens,
1406 type_tucmd, mss_l4len_idx);
1411 static netdev_tx_t igc_xmit_frame_ring(struct sk_buff *skb,
1412 struct igc_ring *tx_ring)
1414 u16 count = TXD_USE_COUNT(skb_headlen(skb));
1415 __be16 protocol = vlan_get_protocol(skb);
1416 struct igc_tx_buffer *first;
1422 /* need: 1 descriptor per page * PAGE_SIZE/IGC_MAX_DATA_PER_TXD,
1423 * + 1 desc for skb_headlen/IGC_MAX_DATA_PER_TXD,
1424 * + 2 desc gap to keep tail from touching head,
1425 * + 1 desc for context descriptor,
1426 * otherwise try next time
1428 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
1429 count += TXD_USE_COUNT(skb_frag_size(
1430 &skb_shinfo(skb)->frags[f]));
1432 if (igc_maybe_stop_tx(tx_ring, count + 3)) {
1433 /* this is a hard error */
1434 return NETDEV_TX_BUSY;
1437 /* record the location of the first descriptor for this packet */
1438 first = &tx_ring->tx_buffer_info[tx_ring->next_to_use];
1439 first->type = IGC_TX_BUFFER_TYPE_SKB;
1441 first->bytecount = skb->len;
1442 first->gso_segs = 1;
1444 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
1445 struct igc_adapter *adapter = netdev_priv(tx_ring->netdev);
1447 /* FIXME: add support for retrieving timestamps from
1448 * the other timer registers before skipping the
1449 * timestamping request.
1451 if (adapter->tstamp_config.tx_type == HWTSTAMP_TX_ON &&
1452 !test_and_set_bit_lock(__IGC_PTP_TX_IN_PROGRESS,
1454 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1455 tx_flags |= IGC_TX_FLAGS_TSTAMP;
1457 adapter->ptp_tx_skb = skb_get(skb);
1458 adapter->ptp_tx_start = jiffies;
1460 adapter->tx_hwtstamp_skipped++;
1464 if (skb_vlan_tag_present(skb)) {
1465 tx_flags |= IGC_TX_FLAGS_VLAN;
1466 tx_flags |= (skb_vlan_tag_get(skb) << IGC_TX_FLAGS_VLAN_SHIFT);
1469 /* record initial flags and protocol */
1470 first->tx_flags = tx_flags;
1471 first->protocol = protocol;
1473 tso = igc_tso(tx_ring, first, &hdr_len);
1477 igc_tx_csum(tx_ring, first);
1479 igc_tx_map(tx_ring, first, hdr_len);
1481 return NETDEV_TX_OK;
1484 dev_kfree_skb_any(first->skb);
1487 return NETDEV_TX_OK;
1490 static inline struct igc_ring *igc_tx_queue_mapping(struct igc_adapter *adapter,
1491 struct sk_buff *skb)
1493 unsigned int r_idx = skb->queue_mapping;
1495 if (r_idx >= adapter->num_tx_queues)
1496 r_idx = r_idx % adapter->num_tx_queues;
1498 return adapter->tx_ring[r_idx];
1501 static netdev_tx_t igc_xmit_frame(struct sk_buff *skb,
1502 struct net_device *netdev)
1504 struct igc_adapter *adapter = netdev_priv(netdev);
1506 /* The minimum packet size with TCTL.PSP set is 17 so pad the skb
1507 * in order to meet this minimum size requirement.
1509 if (skb->len < 17) {
1510 if (skb_padto(skb, 17))
1511 return NETDEV_TX_OK;
1515 return igc_xmit_frame_ring(skb, igc_tx_queue_mapping(adapter, skb));
1518 static void igc_rx_checksum(struct igc_ring *ring,
1519 union igc_adv_rx_desc *rx_desc,
1520 struct sk_buff *skb)
1522 skb_checksum_none_assert(skb);
1524 /* Ignore Checksum bit is set */
1525 if (igc_test_staterr(rx_desc, IGC_RXD_STAT_IXSM))
1528 /* Rx checksum disabled via ethtool */
1529 if (!(ring->netdev->features & NETIF_F_RXCSUM))
1532 /* TCP/UDP checksum error bit is set */
1533 if (igc_test_staterr(rx_desc,
1534 IGC_RXDEXT_STATERR_L4E |
1535 IGC_RXDEXT_STATERR_IPE)) {
1536 /* work around errata with sctp packets where the TCPE aka
1537 * L4E bit is set incorrectly on 64 byte (60 byte w/o crc)
1538 * packets (aka let the stack check the crc32c)
1540 if (!(skb->len == 60 &&
1541 test_bit(IGC_RING_FLAG_RX_SCTP_CSUM, &ring->flags))) {
1542 u64_stats_update_begin(&ring->rx_syncp);
1543 ring->rx_stats.csum_err++;
1544 u64_stats_update_end(&ring->rx_syncp);
1546 /* let the stack verify checksum errors */
1549 /* It must be a TCP or UDP packet with a valid checksum */
1550 if (igc_test_staterr(rx_desc, IGC_RXD_STAT_TCPCS |
1551 IGC_RXD_STAT_UDPCS))
1552 skb->ip_summed = CHECKSUM_UNNECESSARY;
1554 netdev_dbg(ring->netdev, "cksum success: bits %08X\n",
1555 le32_to_cpu(rx_desc->wb.upper.status_error));
1558 static inline void igc_rx_hash(struct igc_ring *ring,
1559 union igc_adv_rx_desc *rx_desc,
1560 struct sk_buff *skb)
1562 if (ring->netdev->features & NETIF_F_RXHASH)
1564 le32_to_cpu(rx_desc->wb.lower.hi_dword.rss),
1568 static void igc_rx_vlan(struct igc_ring *rx_ring,
1569 union igc_adv_rx_desc *rx_desc,
1570 struct sk_buff *skb)
1572 struct net_device *dev = rx_ring->netdev;
1575 if ((dev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
1576 igc_test_staterr(rx_desc, IGC_RXD_STAT_VP)) {
1577 if (igc_test_staterr(rx_desc, IGC_RXDEXT_STATERR_LB) &&
1578 test_bit(IGC_RING_FLAG_RX_LB_VLAN_BSWAP, &rx_ring->flags))
1579 vid = be16_to_cpu((__force __be16)rx_desc->wb.upper.vlan);
1581 vid = le16_to_cpu(rx_desc->wb.upper.vlan);
1583 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
1588 * igc_process_skb_fields - Populate skb header fields from Rx descriptor
1589 * @rx_ring: rx descriptor ring packet is being transacted on
1590 * @rx_desc: pointer to the EOP Rx descriptor
1591 * @skb: pointer to current skb being populated
1593 * This function checks the ring, descriptor, and packet information in order
1594 * to populate the hash, checksum, VLAN, protocol, and other fields within the
1597 static void igc_process_skb_fields(struct igc_ring *rx_ring,
1598 union igc_adv_rx_desc *rx_desc,
1599 struct sk_buff *skb)
1601 igc_rx_hash(rx_ring, rx_desc, skb);
1603 igc_rx_checksum(rx_ring, rx_desc, skb);
1605 igc_rx_vlan(rx_ring, rx_desc, skb);
1607 skb_record_rx_queue(skb, rx_ring->queue_index);
1609 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
1612 static void igc_vlan_mode(struct net_device *netdev, netdev_features_t features)
1614 bool enable = !!(features & NETIF_F_HW_VLAN_CTAG_RX);
1615 struct igc_adapter *adapter = netdev_priv(netdev);
1616 struct igc_hw *hw = &adapter->hw;
1619 ctrl = rd32(IGC_CTRL);
1622 /* enable VLAN tag insert/strip */
1623 ctrl |= IGC_CTRL_VME;
1625 /* disable VLAN tag insert/strip */
1626 ctrl &= ~IGC_CTRL_VME;
1628 wr32(IGC_CTRL, ctrl);
1631 static void igc_restore_vlan(struct igc_adapter *adapter)
1633 igc_vlan_mode(adapter->netdev, adapter->netdev->features);
1636 static struct igc_rx_buffer *igc_get_rx_buffer(struct igc_ring *rx_ring,
1637 const unsigned int size,
1638 int *rx_buffer_pgcnt)
1640 struct igc_rx_buffer *rx_buffer;
1642 rx_buffer = &rx_ring->rx_buffer_info[rx_ring->next_to_clean];
1644 #if (PAGE_SIZE < 8192)
1645 page_count(rx_buffer->page);
1649 prefetchw(rx_buffer->page);
1651 /* we are reusing so sync this buffer for CPU use */
1652 dma_sync_single_range_for_cpu(rx_ring->dev,
1654 rx_buffer->page_offset,
1658 rx_buffer->pagecnt_bias--;
1663 static void igc_rx_buffer_flip(struct igc_rx_buffer *buffer,
1664 unsigned int truesize)
1666 #if (PAGE_SIZE < 8192)
1667 buffer->page_offset ^= truesize;
1669 buffer->page_offset += truesize;
1673 static unsigned int igc_get_rx_frame_truesize(struct igc_ring *ring,
1676 unsigned int truesize;
1678 #if (PAGE_SIZE < 8192)
1679 truesize = igc_rx_pg_size(ring) / 2;
1681 truesize = ring_uses_build_skb(ring) ?
1682 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
1683 SKB_DATA_ALIGN(IGC_SKB_PAD + size) :
1684 SKB_DATA_ALIGN(size);
1690 * igc_add_rx_frag - Add contents of Rx buffer to sk_buff
1691 * @rx_ring: rx descriptor ring to transact packets on
1692 * @rx_buffer: buffer containing page to add
1693 * @skb: sk_buff to place the data into
1694 * @size: size of buffer to be added
1696 * This function will add the data contained in rx_buffer->page to the skb.
1698 static void igc_add_rx_frag(struct igc_ring *rx_ring,
1699 struct igc_rx_buffer *rx_buffer,
1700 struct sk_buff *skb,
1703 unsigned int truesize;
1705 #if (PAGE_SIZE < 8192)
1706 truesize = igc_rx_pg_size(rx_ring) / 2;
1708 truesize = ring_uses_build_skb(rx_ring) ?
1709 SKB_DATA_ALIGN(IGC_SKB_PAD + size) :
1710 SKB_DATA_ALIGN(size);
1712 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_buffer->page,
1713 rx_buffer->page_offset, size, truesize);
1715 igc_rx_buffer_flip(rx_buffer, truesize);
1718 static struct sk_buff *igc_build_skb(struct igc_ring *rx_ring,
1719 struct igc_rx_buffer *rx_buffer,
1720 union igc_adv_rx_desc *rx_desc,
1723 void *va = page_address(rx_buffer->page) + rx_buffer->page_offset;
1724 unsigned int truesize = igc_get_rx_frame_truesize(rx_ring, size);
1725 struct sk_buff *skb;
1727 /* prefetch first cache line of first page */
1730 /* build an skb around the page buffer */
1731 skb = build_skb(va - IGC_SKB_PAD, truesize);
1735 /* update pointers within the skb to store the data */
1736 skb_reserve(skb, IGC_SKB_PAD);
1737 __skb_put(skb, size);
1739 igc_rx_buffer_flip(rx_buffer, truesize);
1743 static struct sk_buff *igc_construct_skb(struct igc_ring *rx_ring,
1744 struct igc_rx_buffer *rx_buffer,
1745 struct xdp_buff *xdp,
1748 unsigned int size = xdp->data_end - xdp->data;
1749 unsigned int truesize = igc_get_rx_frame_truesize(rx_ring, size);
1750 void *va = xdp->data;
1751 unsigned int headlen;
1752 struct sk_buff *skb;
1754 /* prefetch first cache line of first page */
1757 /* allocate a skb to store the frags */
1758 skb = napi_alloc_skb(&rx_ring->q_vector->napi, IGC_RX_HDR_LEN);
1763 skb_hwtstamps(skb)->hwtstamp = timestamp;
1765 /* Determine available headroom for copy */
1767 if (headlen > IGC_RX_HDR_LEN)
1768 headlen = eth_get_headlen(skb->dev, va, IGC_RX_HDR_LEN);
1770 /* align pull length to size of long to optimize memcpy performance */
1771 memcpy(__skb_put(skb, headlen), va, ALIGN(headlen, sizeof(long)));
1773 /* update all of the pointers */
1776 skb_add_rx_frag(skb, 0, rx_buffer->page,
1777 (va + headlen) - page_address(rx_buffer->page),
1779 igc_rx_buffer_flip(rx_buffer, truesize);
1781 rx_buffer->pagecnt_bias++;
1788 * igc_reuse_rx_page - page flip buffer and store it back on the ring
1789 * @rx_ring: rx descriptor ring to store buffers on
1790 * @old_buff: donor buffer to have page reused
1792 * Synchronizes page for reuse by the adapter
1794 static void igc_reuse_rx_page(struct igc_ring *rx_ring,
1795 struct igc_rx_buffer *old_buff)
1797 u16 nta = rx_ring->next_to_alloc;
1798 struct igc_rx_buffer *new_buff;
1800 new_buff = &rx_ring->rx_buffer_info[nta];
1802 /* update, and store next to alloc */
1804 rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
1806 /* Transfer page from old buffer to new buffer.
1807 * Move each member individually to avoid possible store
1808 * forwarding stalls.
1810 new_buff->dma = old_buff->dma;
1811 new_buff->page = old_buff->page;
1812 new_buff->page_offset = old_buff->page_offset;
1813 new_buff->pagecnt_bias = old_buff->pagecnt_bias;
1816 static bool igc_can_reuse_rx_page(struct igc_rx_buffer *rx_buffer,
1817 int rx_buffer_pgcnt)
1819 unsigned int pagecnt_bias = rx_buffer->pagecnt_bias;
1820 struct page *page = rx_buffer->page;
1822 /* avoid re-using remote and pfmemalloc pages */
1823 if (!dev_page_is_reusable(page))
1826 #if (PAGE_SIZE < 8192)
1827 /* if we are only owner of page we can reuse it */
1828 if (unlikely((rx_buffer_pgcnt - pagecnt_bias) > 1))
1831 #define IGC_LAST_OFFSET \
1832 (SKB_WITH_OVERHEAD(PAGE_SIZE) - IGC_RXBUFFER_2048)
1834 if (rx_buffer->page_offset > IGC_LAST_OFFSET)
1838 /* If we have drained the page fragment pool we need to update
1839 * the pagecnt_bias and page count so that we fully restock the
1840 * number of references the driver holds.
1842 if (unlikely(pagecnt_bias == 1)) {
1843 page_ref_add(page, USHRT_MAX - 1);
1844 rx_buffer->pagecnt_bias = USHRT_MAX;
1851 * igc_is_non_eop - process handling of non-EOP buffers
1852 * @rx_ring: Rx ring being processed
1853 * @rx_desc: Rx descriptor for current buffer
1855 * This function updates next to clean. If the buffer is an EOP buffer
1856 * this function exits returning false, otherwise it will place the
1857 * sk_buff in the next buffer to be chained and return true indicating
1858 * that this is in fact a non-EOP buffer.
1860 static bool igc_is_non_eop(struct igc_ring *rx_ring,
1861 union igc_adv_rx_desc *rx_desc)
1863 u32 ntc = rx_ring->next_to_clean + 1;
1865 /* fetch, update, and store next to clean */
1866 ntc = (ntc < rx_ring->count) ? ntc : 0;
1867 rx_ring->next_to_clean = ntc;
1869 prefetch(IGC_RX_DESC(rx_ring, ntc));
1871 if (likely(igc_test_staterr(rx_desc, IGC_RXD_STAT_EOP)))
1878 * igc_cleanup_headers - Correct corrupted or empty headers
1879 * @rx_ring: rx descriptor ring packet is being transacted on
1880 * @rx_desc: pointer to the EOP Rx descriptor
1881 * @skb: pointer to current skb being fixed
1883 * Address the case where we are pulling data in on pages only
1884 * and as such no data is present in the skb header.
1886 * In addition if skb is not at least 60 bytes we need to pad it so that
1887 * it is large enough to qualify as a valid Ethernet frame.
1889 * Returns true if an error was encountered and skb was freed.
1891 static bool igc_cleanup_headers(struct igc_ring *rx_ring,
1892 union igc_adv_rx_desc *rx_desc,
1893 struct sk_buff *skb)
1895 /* XDP packets use error pointer so abort at this point */
1899 if (unlikely(igc_test_staterr(rx_desc, IGC_RXDEXT_STATERR_RXE))) {
1900 struct net_device *netdev = rx_ring->netdev;
1902 if (!(netdev->features & NETIF_F_RXALL)) {
1903 dev_kfree_skb_any(skb);
1908 /* if eth_skb_pad returns an error the skb was freed */
1909 if (eth_skb_pad(skb))
1915 static void igc_put_rx_buffer(struct igc_ring *rx_ring,
1916 struct igc_rx_buffer *rx_buffer,
1917 int rx_buffer_pgcnt)
1919 if (igc_can_reuse_rx_page(rx_buffer, rx_buffer_pgcnt)) {
1920 /* hand second half of page back to the ring */
1921 igc_reuse_rx_page(rx_ring, rx_buffer);
1923 /* We are not reusing the buffer so unmap it and free
1924 * any references we are holding to it
1926 dma_unmap_page_attrs(rx_ring->dev, rx_buffer->dma,
1927 igc_rx_pg_size(rx_ring), DMA_FROM_DEVICE,
1929 __page_frag_cache_drain(rx_buffer->page,
1930 rx_buffer->pagecnt_bias);
1933 /* clear contents of rx_buffer */
1934 rx_buffer->page = NULL;
1937 static inline unsigned int igc_rx_offset(struct igc_ring *rx_ring)
1939 struct igc_adapter *adapter = rx_ring->q_vector->adapter;
1941 if (ring_uses_build_skb(rx_ring))
1943 if (igc_xdp_is_enabled(adapter))
1944 return XDP_PACKET_HEADROOM;
1949 static bool igc_alloc_mapped_page(struct igc_ring *rx_ring,
1950 struct igc_rx_buffer *bi)
1952 struct page *page = bi->page;
1955 /* since we are recycling buffers we should seldom need to alloc */
1959 /* alloc new page for storage */
1960 page = dev_alloc_pages(igc_rx_pg_order(rx_ring));
1961 if (unlikely(!page)) {
1962 rx_ring->rx_stats.alloc_failed++;
1966 /* map page for use */
1967 dma = dma_map_page_attrs(rx_ring->dev, page, 0,
1968 igc_rx_pg_size(rx_ring),
1972 /* if mapping failed free memory back to system since
1973 * there isn't much point in holding memory we can't use
1975 if (dma_mapping_error(rx_ring->dev, dma)) {
1978 rx_ring->rx_stats.alloc_failed++;
1984 bi->page_offset = igc_rx_offset(rx_ring);
1985 page_ref_add(page, USHRT_MAX - 1);
1986 bi->pagecnt_bias = USHRT_MAX;
1992 * igc_alloc_rx_buffers - Replace used receive buffers; packet split
1993 * @rx_ring: rx descriptor ring
1994 * @cleaned_count: number of buffers to clean
1996 static void igc_alloc_rx_buffers(struct igc_ring *rx_ring, u16 cleaned_count)
1998 union igc_adv_rx_desc *rx_desc;
1999 u16 i = rx_ring->next_to_use;
2000 struct igc_rx_buffer *bi;
2007 rx_desc = IGC_RX_DESC(rx_ring, i);
2008 bi = &rx_ring->rx_buffer_info[i];
2009 i -= rx_ring->count;
2011 bufsz = igc_rx_bufsz(rx_ring);
2014 if (!igc_alloc_mapped_page(rx_ring, bi))
2017 /* sync the buffer for use by the device */
2018 dma_sync_single_range_for_device(rx_ring->dev, bi->dma,
2019 bi->page_offset, bufsz,
2022 /* Refresh the desc even if buffer_addrs didn't change
2023 * because each write-back erases this info.
2025 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
2031 rx_desc = IGC_RX_DESC(rx_ring, 0);
2032 bi = rx_ring->rx_buffer_info;
2033 i -= rx_ring->count;
2036 /* clear the length for the next_to_use descriptor */
2037 rx_desc->wb.upper.length = 0;
2040 } while (cleaned_count);
2042 i += rx_ring->count;
2044 if (rx_ring->next_to_use != i) {
2045 /* record the next descriptor to use */
2046 rx_ring->next_to_use = i;
2048 /* update next to alloc since we have filled the ring */
2049 rx_ring->next_to_alloc = i;
2051 /* Force memory writes to complete before letting h/w
2052 * know there are new descriptors to fetch. (Only
2053 * applicable for weak-ordered memory model archs,
2057 writel(i, rx_ring->tail);
2061 static bool igc_alloc_rx_buffers_zc(struct igc_ring *ring, u16 count)
2063 union igc_adv_rx_desc *desc;
2064 u16 i = ring->next_to_use;
2065 struct igc_rx_buffer *bi;
2072 desc = IGC_RX_DESC(ring, i);
2073 bi = &ring->rx_buffer_info[i];
2077 bi->xdp = xsk_buff_alloc(ring->xsk_pool);
2083 dma = xsk_buff_xdp_get_dma(bi->xdp);
2084 desc->read.pkt_addr = cpu_to_le64(dma);
2090 desc = IGC_RX_DESC(ring, 0);
2091 bi = ring->rx_buffer_info;
2095 /* Clear the length for the next_to_use descriptor. */
2096 desc->wb.upper.length = 0;
2103 if (ring->next_to_use != i) {
2104 ring->next_to_use = i;
2106 /* Force memory writes to complete before letting h/w
2107 * know there are new descriptors to fetch. (Only
2108 * applicable for weak-ordered memory model archs,
2112 writel(i, ring->tail);
2118 static int igc_xdp_init_tx_buffer(struct igc_tx_buffer *buffer,
2119 struct xdp_frame *xdpf,
2120 struct igc_ring *ring)
2124 dma = dma_map_single(ring->dev, xdpf->data, xdpf->len, DMA_TO_DEVICE);
2125 if (dma_mapping_error(ring->dev, dma)) {
2126 netdev_err_once(ring->netdev, "Failed to map DMA for TX\n");
2130 buffer->type = IGC_TX_BUFFER_TYPE_XDP;
2131 buffer->xdpf = xdpf;
2132 buffer->protocol = 0;
2133 buffer->bytecount = xdpf->len;
2134 buffer->gso_segs = 1;
2135 buffer->time_stamp = jiffies;
2136 dma_unmap_len_set(buffer, len, xdpf->len);
2137 dma_unmap_addr_set(buffer, dma, dma);
2141 /* This function requires __netif_tx_lock is held by the caller. */
2142 static int igc_xdp_init_tx_descriptor(struct igc_ring *ring,
2143 struct xdp_frame *xdpf)
2145 struct igc_tx_buffer *buffer;
2146 union igc_adv_tx_desc *desc;
2147 u32 cmd_type, olinfo_status;
2150 if (!igc_desc_unused(ring))
2153 buffer = &ring->tx_buffer_info[ring->next_to_use];
2154 err = igc_xdp_init_tx_buffer(buffer, xdpf, ring);
2158 cmd_type = IGC_ADVTXD_DTYP_DATA | IGC_ADVTXD_DCMD_DEXT |
2159 IGC_ADVTXD_DCMD_IFCS | IGC_TXD_DCMD |
2161 olinfo_status = buffer->bytecount << IGC_ADVTXD_PAYLEN_SHIFT;
2163 desc = IGC_TX_DESC(ring, ring->next_to_use);
2164 desc->read.cmd_type_len = cpu_to_le32(cmd_type);
2165 desc->read.olinfo_status = cpu_to_le32(olinfo_status);
2166 desc->read.buffer_addr = cpu_to_le64(dma_unmap_addr(buffer, dma));
2168 netdev_tx_sent_queue(txring_txq(ring), buffer->bytecount);
2170 buffer->next_to_watch = desc;
2172 ring->next_to_use++;
2173 if (ring->next_to_use == ring->count)
2174 ring->next_to_use = 0;
2179 static struct igc_ring *igc_xdp_get_tx_ring(struct igc_adapter *adapter,
2184 if (unlikely(index < 0))
2187 while (index >= adapter->num_tx_queues)
2188 index -= adapter->num_tx_queues;
2190 return adapter->tx_ring[index];
2193 static int igc_xdp_xmit_back(struct igc_adapter *adapter, struct xdp_buff *xdp)
2195 struct xdp_frame *xdpf = xdp_convert_buff_to_frame(xdp);
2196 int cpu = smp_processor_id();
2197 struct netdev_queue *nq;
2198 struct igc_ring *ring;
2201 if (unlikely(!xdpf))
2204 ring = igc_xdp_get_tx_ring(adapter, cpu);
2205 nq = txring_txq(ring);
2207 __netif_tx_lock(nq, cpu);
2208 res = igc_xdp_init_tx_descriptor(ring, xdpf);
2209 __netif_tx_unlock(nq);
2213 /* This function assumes rcu_read_lock() is held by the caller. */
2214 static int __igc_xdp_run_prog(struct igc_adapter *adapter,
2215 struct bpf_prog *prog,
2216 struct xdp_buff *xdp)
2218 u32 act = bpf_prog_run_xdp(prog, xdp);
2222 return IGC_XDP_PASS;
2224 if (igc_xdp_xmit_back(adapter, xdp) < 0)
2228 if (xdp_do_redirect(adapter->netdev, xdp, prog) < 0)
2230 return IGC_XDP_REDIRECT;
2233 bpf_warn_invalid_xdp_action(act);
2237 trace_xdp_exception(adapter->netdev, prog, act);
2240 return IGC_XDP_CONSUMED;
2244 static struct sk_buff *igc_xdp_run_prog(struct igc_adapter *adapter,
2245 struct xdp_buff *xdp)
2247 struct bpf_prog *prog;
2250 prog = READ_ONCE(adapter->xdp_prog);
2256 res = __igc_xdp_run_prog(adapter, prog, xdp);
2259 return ERR_PTR(-res);
2262 /* This function assumes __netif_tx_lock is held by the caller. */
2263 static void igc_flush_tx_descriptors(struct igc_ring *ring)
2265 /* Once tail pointer is updated, hardware can fetch the descriptors
2266 * any time so we issue a write membar here to ensure all memory
2267 * writes are complete before the tail pointer is updated.
2270 writel(ring->next_to_use, ring->tail);
2273 static void igc_finalize_xdp(struct igc_adapter *adapter, int status)
2275 int cpu = smp_processor_id();
2276 struct netdev_queue *nq;
2277 struct igc_ring *ring;
2279 if (status & IGC_XDP_TX) {
2280 ring = igc_xdp_get_tx_ring(adapter, cpu);
2281 nq = txring_txq(ring);
2283 __netif_tx_lock(nq, cpu);
2284 igc_flush_tx_descriptors(ring);
2285 __netif_tx_unlock(nq);
2288 if (status & IGC_XDP_REDIRECT)
2292 static void igc_update_rx_stats(struct igc_q_vector *q_vector,
2293 unsigned int packets, unsigned int bytes)
2295 struct igc_ring *ring = q_vector->rx.ring;
2297 u64_stats_update_begin(&ring->rx_syncp);
2298 ring->rx_stats.packets += packets;
2299 ring->rx_stats.bytes += bytes;
2300 u64_stats_update_end(&ring->rx_syncp);
2302 q_vector->rx.total_packets += packets;
2303 q_vector->rx.total_bytes += bytes;
2306 static int igc_clean_rx_irq(struct igc_q_vector *q_vector, const int budget)
2308 unsigned int total_bytes = 0, total_packets = 0;
2309 struct igc_adapter *adapter = q_vector->adapter;
2310 struct igc_ring *rx_ring = q_vector->rx.ring;
2311 struct sk_buff *skb = rx_ring->skb;
2312 u16 cleaned_count = igc_desc_unused(rx_ring);
2313 int xdp_status = 0, rx_buffer_pgcnt;
2315 while (likely(total_packets < budget)) {
2316 union igc_adv_rx_desc *rx_desc;
2317 struct igc_rx_buffer *rx_buffer;
2318 unsigned int size, truesize;
2319 ktime_t timestamp = 0;
2320 struct xdp_buff xdp;
2324 /* return some buffers to hardware, one at a time is too slow */
2325 if (cleaned_count >= IGC_RX_BUFFER_WRITE) {
2326 igc_alloc_rx_buffers(rx_ring, cleaned_count);
2330 rx_desc = IGC_RX_DESC(rx_ring, rx_ring->next_to_clean);
2331 size = le16_to_cpu(rx_desc->wb.upper.length);
2335 /* This memory barrier is needed to keep us from reading
2336 * any other fields out of the rx_desc until we know the
2337 * descriptor has been written back
2341 rx_buffer = igc_get_rx_buffer(rx_ring, size, &rx_buffer_pgcnt);
2342 truesize = igc_get_rx_frame_truesize(rx_ring, size);
2344 pktbuf = page_address(rx_buffer->page) + rx_buffer->page_offset;
2346 if (igc_test_staterr(rx_desc, IGC_RXDADV_STAT_TSIP)) {
2347 timestamp = igc_ptp_rx_pktstamp(q_vector->adapter,
2349 pkt_offset = IGC_TS_HDR_LEN;
2350 size -= IGC_TS_HDR_LEN;
2354 xdp_init_buff(&xdp, truesize, &rx_ring->xdp_rxq);
2355 xdp_prepare_buff(&xdp, pktbuf - igc_rx_offset(rx_ring),
2356 igc_rx_offset(rx_ring) + pkt_offset, size, false);
2358 skb = igc_xdp_run_prog(adapter, &xdp);
2362 unsigned int xdp_res = -PTR_ERR(skb);
2365 case IGC_XDP_CONSUMED:
2366 rx_buffer->pagecnt_bias++;
2369 case IGC_XDP_REDIRECT:
2370 igc_rx_buffer_flip(rx_buffer, truesize);
2371 xdp_status |= xdp_res;
2376 total_bytes += size;
2378 igc_add_rx_frag(rx_ring, rx_buffer, skb, size);
2379 else if (ring_uses_build_skb(rx_ring))
2380 skb = igc_build_skb(rx_ring, rx_buffer, rx_desc, size);
2382 skb = igc_construct_skb(rx_ring, rx_buffer, &xdp,
2385 /* exit if we failed to retrieve a buffer */
2387 rx_ring->rx_stats.alloc_failed++;
2388 rx_buffer->pagecnt_bias++;
2392 igc_put_rx_buffer(rx_ring, rx_buffer, rx_buffer_pgcnt);
2395 /* fetch next buffer in frame if non-eop */
2396 if (igc_is_non_eop(rx_ring, rx_desc))
2399 /* verify the packet layout is correct */
2400 if (igc_cleanup_headers(rx_ring, rx_desc, skb)) {
2405 /* probably a little skewed due to removing CRC */
2406 total_bytes += skb->len;
2408 /* populate checksum, VLAN, and protocol */
2409 igc_process_skb_fields(rx_ring, rx_desc, skb);
2411 napi_gro_receive(&q_vector->napi, skb);
2413 /* reset skb pointer */
2416 /* update budget accounting */
2421 igc_finalize_xdp(adapter, xdp_status);
2423 /* place incomplete frames back on ring for completion */
2426 igc_update_rx_stats(q_vector, total_packets, total_bytes);
2429 igc_alloc_rx_buffers(rx_ring, cleaned_count);
2431 return total_packets;
2434 static struct sk_buff *igc_construct_skb_zc(struct igc_ring *ring,
2435 struct xdp_buff *xdp)
2437 unsigned int metasize = xdp->data - xdp->data_meta;
2438 unsigned int datasize = xdp->data_end - xdp->data;
2439 unsigned int totalsize = metasize + datasize;
2440 struct sk_buff *skb;
2442 skb = __napi_alloc_skb(&ring->q_vector->napi,
2443 xdp->data_end - xdp->data_hard_start,
2444 GFP_ATOMIC | __GFP_NOWARN);
2448 skb_reserve(skb, xdp->data_meta - xdp->data_hard_start);
2449 memcpy(__skb_put(skb, totalsize), xdp->data_meta, totalsize);
2451 skb_metadata_set(skb, metasize);
2456 static void igc_dispatch_skb_zc(struct igc_q_vector *q_vector,
2457 union igc_adv_rx_desc *desc,
2458 struct xdp_buff *xdp,
2461 struct igc_ring *ring = q_vector->rx.ring;
2462 struct sk_buff *skb;
2464 skb = igc_construct_skb_zc(ring, xdp);
2466 ring->rx_stats.alloc_failed++;
2471 skb_hwtstamps(skb)->hwtstamp = timestamp;
2473 if (igc_cleanup_headers(ring, desc, skb))
2476 igc_process_skb_fields(ring, desc, skb);
2477 napi_gro_receive(&q_vector->napi, skb);
2480 static int igc_clean_rx_irq_zc(struct igc_q_vector *q_vector, const int budget)
2482 struct igc_adapter *adapter = q_vector->adapter;
2483 struct igc_ring *ring = q_vector->rx.ring;
2484 u16 cleaned_count = igc_desc_unused(ring);
2485 int total_bytes = 0, total_packets = 0;
2486 u16 ntc = ring->next_to_clean;
2487 struct bpf_prog *prog;
2488 bool failure = false;
2493 prog = READ_ONCE(adapter->xdp_prog);
2495 while (likely(total_packets < budget)) {
2496 union igc_adv_rx_desc *desc;
2497 struct igc_rx_buffer *bi;
2498 ktime_t timestamp = 0;
2502 desc = IGC_RX_DESC(ring, ntc);
2503 size = le16_to_cpu(desc->wb.upper.length);
2507 /* This memory barrier is needed to keep us from reading
2508 * any other fields out of the rx_desc until we know the
2509 * descriptor has been written back
2513 bi = &ring->rx_buffer_info[ntc];
2515 if (igc_test_staterr(desc, IGC_RXDADV_STAT_TSIP)) {
2516 timestamp = igc_ptp_rx_pktstamp(q_vector->adapter,
2519 bi->xdp->data += IGC_TS_HDR_LEN;
2521 /* HW timestamp has been copied into local variable. Metadata
2522 * length when XDP program is called should be 0.
2524 bi->xdp->data_meta += IGC_TS_HDR_LEN;
2525 size -= IGC_TS_HDR_LEN;
2528 bi->xdp->data_end = bi->xdp->data + size;
2529 xsk_buff_dma_sync_for_cpu(bi->xdp, ring->xsk_pool);
2531 res = __igc_xdp_run_prog(adapter, prog, bi->xdp);
2534 igc_dispatch_skb_zc(q_vector, desc, bi->xdp, timestamp);
2536 case IGC_XDP_CONSUMED:
2537 xsk_buff_free(bi->xdp);
2540 case IGC_XDP_REDIRECT:
2546 total_bytes += size;
2550 if (ntc == ring->count)
2554 ring->next_to_clean = ntc;
2557 if (cleaned_count >= IGC_RX_BUFFER_WRITE)
2558 failure = !igc_alloc_rx_buffers_zc(ring, cleaned_count);
2561 igc_finalize_xdp(adapter, xdp_status);
2563 igc_update_rx_stats(q_vector, total_packets, total_bytes);
2565 if (xsk_uses_need_wakeup(ring->xsk_pool)) {
2566 if (failure || ring->next_to_clean == ring->next_to_use)
2567 xsk_set_rx_need_wakeup(ring->xsk_pool);
2569 xsk_clear_rx_need_wakeup(ring->xsk_pool);
2570 return total_packets;
2573 return failure ? budget : total_packets;
2576 static void igc_update_tx_stats(struct igc_q_vector *q_vector,
2577 unsigned int packets, unsigned int bytes)
2579 struct igc_ring *ring = q_vector->tx.ring;
2581 u64_stats_update_begin(&ring->tx_syncp);
2582 ring->tx_stats.bytes += bytes;
2583 ring->tx_stats.packets += packets;
2584 u64_stats_update_end(&ring->tx_syncp);
2586 q_vector->tx.total_bytes += bytes;
2587 q_vector->tx.total_packets += packets;
2590 static void igc_xdp_xmit_zc(struct igc_ring *ring)
2592 struct xsk_buff_pool *pool = ring->xsk_pool;
2593 struct netdev_queue *nq = txring_txq(ring);
2594 union igc_adv_tx_desc *tx_desc = NULL;
2595 int cpu = smp_processor_id();
2596 u16 ntu = ring->next_to_use;
2597 struct xdp_desc xdp_desc;
2600 if (!netif_carrier_ok(ring->netdev))
2603 __netif_tx_lock(nq, cpu);
2605 budget = igc_desc_unused(ring);
2607 while (xsk_tx_peek_desc(pool, &xdp_desc) && budget--) {
2608 u32 cmd_type, olinfo_status;
2609 struct igc_tx_buffer *bi;
2612 cmd_type = IGC_ADVTXD_DTYP_DATA | IGC_ADVTXD_DCMD_DEXT |
2613 IGC_ADVTXD_DCMD_IFCS | IGC_TXD_DCMD |
2615 olinfo_status = xdp_desc.len << IGC_ADVTXD_PAYLEN_SHIFT;
2617 dma = xsk_buff_raw_get_dma(pool, xdp_desc.addr);
2618 xsk_buff_raw_dma_sync_for_device(pool, dma, xdp_desc.len);
2620 tx_desc = IGC_TX_DESC(ring, ntu);
2621 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type);
2622 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
2623 tx_desc->read.buffer_addr = cpu_to_le64(dma);
2625 bi = &ring->tx_buffer_info[ntu];
2626 bi->type = IGC_TX_BUFFER_TYPE_XSK;
2628 bi->bytecount = xdp_desc.len;
2630 bi->time_stamp = jiffies;
2631 bi->next_to_watch = tx_desc;
2633 netdev_tx_sent_queue(txring_txq(ring), xdp_desc.len);
2636 if (ntu == ring->count)
2640 ring->next_to_use = ntu;
2642 igc_flush_tx_descriptors(ring);
2643 xsk_tx_release(pool);
2646 __netif_tx_unlock(nq);
2650 * igc_clean_tx_irq - Reclaim resources after transmit completes
2651 * @q_vector: pointer to q_vector containing needed info
2652 * @napi_budget: Used to determine if we are in netpoll
2654 * returns true if ring is completely cleaned
2656 static bool igc_clean_tx_irq(struct igc_q_vector *q_vector, int napi_budget)
2658 struct igc_adapter *adapter = q_vector->adapter;
2659 unsigned int total_bytes = 0, total_packets = 0;
2660 unsigned int budget = q_vector->tx.work_limit;
2661 struct igc_ring *tx_ring = q_vector->tx.ring;
2662 unsigned int i = tx_ring->next_to_clean;
2663 struct igc_tx_buffer *tx_buffer;
2664 union igc_adv_tx_desc *tx_desc;
2667 if (test_bit(__IGC_DOWN, &adapter->state))
2670 tx_buffer = &tx_ring->tx_buffer_info[i];
2671 tx_desc = IGC_TX_DESC(tx_ring, i);
2672 i -= tx_ring->count;
2675 union igc_adv_tx_desc *eop_desc = tx_buffer->next_to_watch;
2677 /* if next_to_watch is not set then there is no work pending */
2681 /* prevent any other reads prior to eop_desc */
2684 /* if DD is not set pending work has not been completed */
2685 if (!(eop_desc->wb.status & cpu_to_le32(IGC_TXD_STAT_DD)))
2688 /* clear next_to_watch to prevent false hangs */
2689 tx_buffer->next_to_watch = NULL;
2691 /* update the statistics for this packet */
2692 total_bytes += tx_buffer->bytecount;
2693 total_packets += tx_buffer->gso_segs;
2695 switch (tx_buffer->type) {
2696 case IGC_TX_BUFFER_TYPE_XSK:
2699 case IGC_TX_BUFFER_TYPE_XDP:
2700 xdp_return_frame(tx_buffer->xdpf);
2701 igc_unmap_tx_buffer(tx_ring->dev, tx_buffer);
2703 case IGC_TX_BUFFER_TYPE_SKB:
2704 napi_consume_skb(tx_buffer->skb, napi_budget);
2705 igc_unmap_tx_buffer(tx_ring->dev, tx_buffer);
2708 netdev_warn_once(tx_ring->netdev, "Unknown Tx buffer type\n");
2712 /* clear last DMA location and unmap remaining buffers */
2713 while (tx_desc != eop_desc) {
2718 i -= tx_ring->count;
2719 tx_buffer = tx_ring->tx_buffer_info;
2720 tx_desc = IGC_TX_DESC(tx_ring, 0);
2723 /* unmap any remaining paged data */
2724 if (dma_unmap_len(tx_buffer, len))
2725 igc_unmap_tx_buffer(tx_ring->dev, tx_buffer);
2728 /* move us one more past the eop_desc for start of next pkt */
2733 i -= tx_ring->count;
2734 tx_buffer = tx_ring->tx_buffer_info;
2735 tx_desc = IGC_TX_DESC(tx_ring, 0);
2738 /* issue prefetch for next Tx descriptor */
2741 /* update budget accounting */
2743 } while (likely(budget));
2745 netdev_tx_completed_queue(txring_txq(tx_ring),
2746 total_packets, total_bytes);
2748 i += tx_ring->count;
2749 tx_ring->next_to_clean = i;
2751 igc_update_tx_stats(q_vector, total_packets, total_bytes);
2753 if (tx_ring->xsk_pool) {
2755 xsk_tx_completed(tx_ring->xsk_pool, xsk_frames);
2756 if (xsk_uses_need_wakeup(tx_ring->xsk_pool))
2757 xsk_set_tx_need_wakeup(tx_ring->xsk_pool);
2758 igc_xdp_xmit_zc(tx_ring);
2761 if (test_bit(IGC_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags)) {
2762 struct igc_hw *hw = &adapter->hw;
2764 /* Detect a transmit hang in hardware, this serializes the
2765 * check with the clearing of time_stamp and movement of i
2767 clear_bit(IGC_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags);
2768 if (tx_buffer->next_to_watch &&
2769 time_after(jiffies, tx_buffer->time_stamp +
2770 (adapter->tx_timeout_factor * HZ)) &&
2771 !(rd32(IGC_STATUS) & IGC_STATUS_TXOFF)) {
2772 /* detected Tx unit hang */
2773 netdev_err(tx_ring->netdev,
2774 "Detected Tx Unit Hang\n"
2778 " next_to_use <%x>\n"
2779 " next_to_clean <%x>\n"
2780 "buffer_info[next_to_clean]\n"
2781 " time_stamp <%lx>\n"
2782 " next_to_watch <%p>\n"
2784 " desc.status <%x>\n",
2785 tx_ring->queue_index,
2786 rd32(IGC_TDH(tx_ring->reg_idx)),
2787 readl(tx_ring->tail),
2788 tx_ring->next_to_use,
2789 tx_ring->next_to_clean,
2790 tx_buffer->time_stamp,
2791 tx_buffer->next_to_watch,
2793 tx_buffer->next_to_watch->wb.status);
2794 netif_stop_subqueue(tx_ring->netdev,
2795 tx_ring->queue_index);
2797 /* we are about to reset, no point in enabling stuff */
2802 #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
2803 if (unlikely(total_packets &&
2804 netif_carrier_ok(tx_ring->netdev) &&
2805 igc_desc_unused(tx_ring) >= TX_WAKE_THRESHOLD)) {
2806 /* Make sure that anybody stopping the queue after this
2807 * sees the new next_to_clean.
2810 if (__netif_subqueue_stopped(tx_ring->netdev,
2811 tx_ring->queue_index) &&
2812 !(test_bit(__IGC_DOWN, &adapter->state))) {
2813 netif_wake_subqueue(tx_ring->netdev,
2814 tx_ring->queue_index);
2816 u64_stats_update_begin(&tx_ring->tx_syncp);
2817 tx_ring->tx_stats.restart_queue++;
2818 u64_stats_update_end(&tx_ring->tx_syncp);
2825 static int igc_find_mac_filter(struct igc_adapter *adapter,
2826 enum igc_mac_filter_type type, const u8 *addr)
2828 struct igc_hw *hw = &adapter->hw;
2829 int max_entries = hw->mac.rar_entry_count;
2833 for (i = 0; i < max_entries; i++) {
2834 ral = rd32(IGC_RAL(i));
2835 rah = rd32(IGC_RAH(i));
2837 if (!(rah & IGC_RAH_AV))
2839 if (!!(rah & IGC_RAH_ASEL_SRC_ADDR) != type)
2841 if ((rah & IGC_RAH_RAH_MASK) !=
2842 le16_to_cpup((__le16 *)(addr + 4)))
2844 if (ral != le32_to_cpup((__le32 *)(addr)))
2853 static int igc_get_avail_mac_filter_slot(struct igc_adapter *adapter)
2855 struct igc_hw *hw = &adapter->hw;
2856 int max_entries = hw->mac.rar_entry_count;
2860 for (i = 0; i < max_entries; i++) {
2861 rah = rd32(IGC_RAH(i));
2863 if (!(rah & IGC_RAH_AV))
2871 * igc_add_mac_filter() - Add MAC address filter
2872 * @adapter: Pointer to adapter where the filter should be added
2873 * @type: MAC address filter type (source or destination)
2874 * @addr: MAC address
2875 * @queue: If non-negative, queue assignment feature is enabled and frames
2876 * matching the filter are enqueued onto 'queue'. Otherwise, queue
2877 * assignment is disabled.
2879 * Return: 0 in case of success, negative errno code otherwise.
2881 static int igc_add_mac_filter(struct igc_adapter *adapter,
2882 enum igc_mac_filter_type type, const u8 *addr,
2885 struct net_device *dev = adapter->netdev;
2888 index = igc_find_mac_filter(adapter, type, addr);
2892 index = igc_get_avail_mac_filter_slot(adapter);
2896 netdev_dbg(dev, "Add MAC address filter: index %d type %s address %pM queue %d\n",
2897 index, type == IGC_MAC_FILTER_TYPE_DST ? "dst" : "src",
2901 igc_set_mac_filter_hw(adapter, index, type, addr, queue);
2906 * igc_del_mac_filter() - Delete MAC address filter
2907 * @adapter: Pointer to adapter where the filter should be deleted from
2908 * @type: MAC address filter type (source or destination)
2909 * @addr: MAC address
2911 static void igc_del_mac_filter(struct igc_adapter *adapter,
2912 enum igc_mac_filter_type type, const u8 *addr)
2914 struct net_device *dev = adapter->netdev;
2917 index = igc_find_mac_filter(adapter, type, addr);
2922 /* If this is the default filter, we don't actually delete it.
2923 * We just reset to its default value i.e. disable queue
2926 netdev_dbg(dev, "Disable default MAC filter queue assignment");
2928 igc_set_mac_filter_hw(adapter, 0, type, addr, -1);
2930 netdev_dbg(dev, "Delete MAC address filter: index %d type %s address %pM\n",
2932 type == IGC_MAC_FILTER_TYPE_DST ? "dst" : "src",
2935 igc_clear_mac_filter_hw(adapter, index);
2940 * igc_add_vlan_prio_filter() - Add VLAN priority filter
2941 * @adapter: Pointer to adapter where the filter should be added
2942 * @prio: VLAN priority value
2943 * @queue: Queue number which matching frames are assigned to
2945 * Return: 0 in case of success, negative errno code otherwise.
2947 static int igc_add_vlan_prio_filter(struct igc_adapter *adapter, int prio,
2950 struct net_device *dev = adapter->netdev;
2951 struct igc_hw *hw = &adapter->hw;
2954 vlanpqf = rd32(IGC_VLANPQF);
2956 if (vlanpqf & IGC_VLANPQF_VALID(prio)) {
2957 netdev_dbg(dev, "VLAN priority filter already in use\n");
2961 vlanpqf |= IGC_VLANPQF_QSEL(prio, queue);
2962 vlanpqf |= IGC_VLANPQF_VALID(prio);
2964 wr32(IGC_VLANPQF, vlanpqf);
2966 netdev_dbg(dev, "Add VLAN priority filter: prio %d queue %d\n",
2972 * igc_del_vlan_prio_filter() - Delete VLAN priority filter
2973 * @adapter: Pointer to adapter where the filter should be deleted from
2974 * @prio: VLAN priority value
2976 static void igc_del_vlan_prio_filter(struct igc_adapter *adapter, int prio)
2978 struct igc_hw *hw = &adapter->hw;
2981 vlanpqf = rd32(IGC_VLANPQF);
2983 vlanpqf &= ~IGC_VLANPQF_VALID(prio);
2984 vlanpqf &= ~IGC_VLANPQF_QSEL(prio, IGC_VLANPQF_QUEUE_MASK);
2986 wr32(IGC_VLANPQF, vlanpqf);
2988 netdev_dbg(adapter->netdev, "Delete VLAN priority filter: prio %d\n",
2992 static int igc_get_avail_etype_filter_slot(struct igc_adapter *adapter)
2994 struct igc_hw *hw = &adapter->hw;
2997 for (i = 0; i < MAX_ETYPE_FILTER; i++) {
2998 u32 etqf = rd32(IGC_ETQF(i));
3000 if (!(etqf & IGC_ETQF_FILTER_ENABLE))
3008 * igc_add_etype_filter() - Add ethertype filter
3009 * @adapter: Pointer to adapter where the filter should be added
3010 * @etype: Ethertype value
3011 * @queue: If non-negative, queue assignment feature is enabled and frames
3012 * matching the filter are enqueued onto 'queue'. Otherwise, queue
3013 * assignment is disabled.
3015 * Return: 0 in case of success, negative errno code otherwise.
3017 static int igc_add_etype_filter(struct igc_adapter *adapter, u16 etype,
3020 struct igc_hw *hw = &adapter->hw;
3024 index = igc_get_avail_etype_filter_slot(adapter);
3028 etqf = rd32(IGC_ETQF(index));
3030 etqf &= ~IGC_ETQF_ETYPE_MASK;
3034 etqf &= ~IGC_ETQF_QUEUE_MASK;
3035 etqf |= (queue << IGC_ETQF_QUEUE_SHIFT);
3036 etqf |= IGC_ETQF_QUEUE_ENABLE;
3039 etqf |= IGC_ETQF_FILTER_ENABLE;
3041 wr32(IGC_ETQF(index), etqf);
3043 netdev_dbg(adapter->netdev, "Add ethertype filter: etype %04x queue %d\n",
3048 static int igc_find_etype_filter(struct igc_adapter *adapter, u16 etype)
3050 struct igc_hw *hw = &adapter->hw;
3053 for (i = 0; i < MAX_ETYPE_FILTER; i++) {
3054 u32 etqf = rd32(IGC_ETQF(i));
3056 if ((etqf & IGC_ETQF_ETYPE_MASK) == etype)
3064 * igc_del_etype_filter() - Delete ethertype filter
3065 * @adapter: Pointer to adapter where the filter should be deleted from
3066 * @etype: Ethertype value
3068 static void igc_del_etype_filter(struct igc_adapter *adapter, u16 etype)
3070 struct igc_hw *hw = &adapter->hw;
3073 index = igc_find_etype_filter(adapter, etype);
3077 wr32(IGC_ETQF(index), 0);
3079 netdev_dbg(adapter->netdev, "Delete ethertype filter: etype %04x\n",
3083 static int igc_flex_filter_select(struct igc_adapter *adapter,
3084 struct igc_flex_filter *input,
3087 struct igc_hw *hw = &adapter->hw;
3091 if (input->index >= MAX_FLEX_FILTER) {
3092 dev_err(&adapter->pdev->dev, "Wrong Flex Filter index selected!\n");
3096 /* Indirect table select register */
3097 fhftsl = rd32(IGC_FHFTSL);
3098 fhftsl &= ~IGC_FHFTSL_FTSL_MASK;
3099 switch (input->index) {
3113 wr32(IGC_FHFTSL, fhftsl);
3115 /* Normalize index down to host table register */
3116 fhft_index = input->index % 8;
3118 *fhft = (fhft_index < 4) ? IGC_FHFT(fhft_index) :
3119 IGC_FHFT_EXT(fhft_index - 4);
3124 static int igc_write_flex_filter_ll(struct igc_adapter *adapter,
3125 struct igc_flex_filter *input)
3127 struct device *dev = &adapter->pdev->dev;
3128 struct igc_hw *hw = &adapter->hw;
3129 u8 *data = input->data;
3130 u8 *mask = input->mask;
3137 /* Length has to be aligned to 8. Otherwise the filter will fail. Bail
3138 * out early to avoid surprises later.
3140 if (input->length % 8 != 0) {
3141 dev_err(dev, "The length of a flex filter has to be 8 byte aligned!\n");
3145 /* Select corresponding flex filter register and get base for host table. */
3146 ret = igc_flex_filter_select(adapter, input, &fhft);
3150 /* When adding a filter globally disable flex filter feature. That is
3151 * recommended within the datasheet.
3153 wufc = rd32(IGC_WUFC);
3154 wufc &= ~IGC_WUFC_FLEX_HQ;
3155 wr32(IGC_WUFC, wufc);
3157 /* Configure filter */
3158 queuing = input->length & IGC_FHFT_LENGTH_MASK;
3159 queuing |= (input->rx_queue << IGC_FHFT_QUEUE_SHIFT) & IGC_FHFT_QUEUE_MASK;
3160 queuing |= (input->prio << IGC_FHFT_PRIO_SHIFT) & IGC_FHFT_PRIO_MASK;
3162 if (input->immediate_irq)
3163 queuing |= IGC_FHFT_IMM_INT;
3166 queuing |= IGC_FHFT_DROP;
3168 wr32(fhft + 0xFC, queuing);
3170 /* Write data (128 byte) and mask (128 bit) */
3171 for (i = 0; i < 16; ++i) {
3172 const size_t data_idx = i * 8;
3173 const size_t row_idx = i * 16;
3175 (data[data_idx + 0] << 0) |
3176 (data[data_idx + 1] << 8) |
3177 (data[data_idx + 2] << 16) |
3178 (data[data_idx + 3] << 24);
3180 (data[data_idx + 4] << 0) |
3181 (data[data_idx + 5] << 8) |
3182 (data[data_idx + 6] << 16) |
3183 (data[data_idx + 7] << 24);
3186 /* Write row: dw0, dw1 and mask */
3187 wr32(fhft + row_idx, dw0);
3188 wr32(fhft + row_idx + 4, dw1);
3190 /* mask is only valid for MASK(7, 0) */
3191 tmp = rd32(fhft + row_idx + 8);
3192 tmp &= ~GENMASK(7, 0);
3194 wr32(fhft + row_idx + 8, tmp);
3197 /* Enable filter. */
3198 wufc |= IGC_WUFC_FLEX_HQ;
3199 if (input->index > 8) {
3200 /* Filter 0-7 are enabled via WUFC. The other 24 filters are not. */
3201 u32 wufc_ext = rd32(IGC_WUFC_EXT);
3203 wufc_ext |= (IGC_WUFC_EXT_FLX8 << (input->index - 8));
3205 wr32(IGC_WUFC_EXT, wufc_ext);
3207 wufc |= (IGC_WUFC_FLX0 << input->index);
3209 wr32(IGC_WUFC, wufc);
3211 dev_dbg(&adapter->pdev->dev, "Added flex filter %u to HW.\n",
3217 static void igc_flex_filter_add_field(struct igc_flex_filter *flex,
3218 const void *src, unsigned int offset,
3219 size_t len, const void *mask)
3224 memcpy(&flex->data[offset], src, len);
3227 for (i = 0; i < len; ++i) {
3228 const unsigned int idx = i + offset;
3229 const u8 *ptr = mask;
3233 flex->mask[idx / 8] |= BIT(idx % 8);
3238 flex->mask[idx / 8] |= BIT(idx % 8);
3242 static int igc_find_avail_flex_filter_slot(struct igc_adapter *adapter)
3244 struct igc_hw *hw = &adapter->hw;
3248 wufc = rd32(IGC_WUFC);
3249 wufc_ext = rd32(IGC_WUFC_EXT);
3251 for (i = 0; i < MAX_FLEX_FILTER; i++) {
3253 if (!(wufc & (IGC_WUFC_FLX0 << i)))
3256 if (!(wufc_ext & (IGC_WUFC_EXT_FLX8 << (i - 8))))
3264 static bool igc_flex_filter_in_use(struct igc_adapter *adapter)
3266 struct igc_hw *hw = &adapter->hw;
3269 wufc = rd32(IGC_WUFC);
3270 wufc_ext = rd32(IGC_WUFC_EXT);
3272 if (wufc & IGC_WUFC_FILTER_MASK)
3275 if (wufc_ext & IGC_WUFC_EXT_FILTER_MASK)
3281 static int igc_add_flex_filter(struct igc_adapter *adapter,
3282 struct igc_nfc_rule *rule)
3284 struct igc_flex_filter flex = { };
3285 struct igc_nfc_filter *filter = &rule->filter;
3286 unsigned int eth_offset, user_offset;
3290 index = igc_find_avail_flex_filter_slot(adapter);
3294 /* Construct the flex filter:
3301 * -> = 26 bytes => 32 length
3305 flex.rx_queue = rule->action;
3307 vlan = rule->filter.vlan_tci || rule->filter.vlan_etype;
3308 eth_offset = vlan ? 16 : 12;
3309 user_offset = vlan ? 18 : 14;
3311 /* Add destination MAC */
3312 if (rule->filter.match_flags & IGC_FILTER_FLAG_DST_MAC_ADDR)
3313 igc_flex_filter_add_field(&flex, &filter->dst_addr, 0,
3316 /* Add source MAC */
3317 if (rule->filter.match_flags & IGC_FILTER_FLAG_SRC_MAC_ADDR)
3318 igc_flex_filter_add_field(&flex, &filter->src_addr, 6,
3321 /* Add VLAN etype */
3322 if (rule->filter.match_flags & IGC_FILTER_FLAG_VLAN_ETYPE)
3323 igc_flex_filter_add_field(&flex, &filter->vlan_etype, 12,
3324 sizeof(filter->vlan_etype),
3328 if (rule->filter.match_flags & IGC_FILTER_FLAG_VLAN_TCI)
3329 igc_flex_filter_add_field(&flex, &filter->vlan_tci, 14,
3330 sizeof(filter->vlan_tci), NULL);
3332 /* Add Ether type */
3333 if (rule->filter.match_flags & IGC_FILTER_FLAG_ETHER_TYPE) {
3334 __be16 etype = cpu_to_be16(filter->etype);
3336 igc_flex_filter_add_field(&flex, &etype, eth_offset,
3337 sizeof(etype), NULL);
3341 if (rule->filter.match_flags & IGC_FILTER_FLAG_USER_DATA)
3342 igc_flex_filter_add_field(&flex, &filter->user_data,
3344 sizeof(filter->user_data),
3347 /* Add it down to the hardware and enable it. */
3348 ret = igc_write_flex_filter_ll(adapter, &flex);
3352 filter->flex_index = index;
3357 static void igc_del_flex_filter(struct igc_adapter *adapter,
3360 struct igc_hw *hw = &adapter->hw;
3363 /* Just disable the filter. The filter table itself is kept
3364 * intact. Another flex_filter_add() should override the "old" data
3367 if (reg_index > 8) {
3368 u32 wufc_ext = rd32(IGC_WUFC_EXT);
3370 wufc_ext &= ~(IGC_WUFC_EXT_FLX8 << (reg_index - 8));
3371 wr32(IGC_WUFC_EXT, wufc_ext);
3373 wufc = rd32(IGC_WUFC);
3375 wufc &= ~(IGC_WUFC_FLX0 << reg_index);
3376 wr32(IGC_WUFC, wufc);
3379 if (igc_flex_filter_in_use(adapter))
3382 /* No filters are in use, we may disable flex filters */
3383 wufc = rd32(IGC_WUFC);
3384 wufc &= ~IGC_WUFC_FLEX_HQ;
3385 wr32(IGC_WUFC, wufc);
3388 static int igc_enable_nfc_rule(struct igc_adapter *adapter,
3389 struct igc_nfc_rule *rule)
3394 return igc_add_flex_filter(adapter, rule);
3397 if (rule->filter.match_flags & IGC_FILTER_FLAG_ETHER_TYPE) {
3398 err = igc_add_etype_filter(adapter, rule->filter.etype,
3404 if (rule->filter.match_flags & IGC_FILTER_FLAG_SRC_MAC_ADDR) {
3405 err = igc_add_mac_filter(adapter, IGC_MAC_FILTER_TYPE_SRC,
3406 rule->filter.src_addr, rule->action);
3411 if (rule->filter.match_flags & IGC_FILTER_FLAG_DST_MAC_ADDR) {
3412 err = igc_add_mac_filter(adapter, IGC_MAC_FILTER_TYPE_DST,
3413 rule->filter.dst_addr, rule->action);
3418 if (rule->filter.match_flags & IGC_FILTER_FLAG_VLAN_TCI) {
3419 int prio = (rule->filter.vlan_tci & VLAN_PRIO_MASK) >>
3422 err = igc_add_vlan_prio_filter(adapter, prio, rule->action);
3430 static void igc_disable_nfc_rule(struct igc_adapter *adapter,
3431 const struct igc_nfc_rule *rule)
3434 igc_del_flex_filter(adapter, rule->filter.flex_index);
3438 if (rule->filter.match_flags & IGC_FILTER_FLAG_ETHER_TYPE)
3439 igc_del_etype_filter(adapter, rule->filter.etype);
3441 if (rule->filter.match_flags & IGC_FILTER_FLAG_VLAN_TCI) {
3442 int prio = (rule->filter.vlan_tci & VLAN_PRIO_MASK) >>
3445 igc_del_vlan_prio_filter(adapter, prio);
3448 if (rule->filter.match_flags & IGC_FILTER_FLAG_SRC_MAC_ADDR)
3449 igc_del_mac_filter(adapter, IGC_MAC_FILTER_TYPE_SRC,
3450 rule->filter.src_addr);
3452 if (rule->filter.match_flags & IGC_FILTER_FLAG_DST_MAC_ADDR)
3453 igc_del_mac_filter(adapter, IGC_MAC_FILTER_TYPE_DST,
3454 rule->filter.dst_addr);
3458 * igc_get_nfc_rule() - Get NFC rule
3459 * @adapter: Pointer to adapter
3460 * @location: Rule location
3462 * Context: Expects adapter->nfc_rule_lock to be held by caller.
3464 * Return: Pointer to NFC rule at @location. If not found, NULL.
3466 struct igc_nfc_rule *igc_get_nfc_rule(struct igc_adapter *adapter,
3469 struct igc_nfc_rule *rule;
3471 list_for_each_entry(rule, &adapter->nfc_rule_list, list) {
3472 if (rule->location == location)
3474 if (rule->location > location)
3482 * igc_del_nfc_rule() - Delete NFC rule
3483 * @adapter: Pointer to adapter
3484 * @rule: Pointer to rule to be deleted
3486 * Disable NFC rule in hardware and delete it from adapter.
3488 * Context: Expects adapter->nfc_rule_lock to be held by caller.
3490 void igc_del_nfc_rule(struct igc_adapter *adapter, struct igc_nfc_rule *rule)
3492 igc_disable_nfc_rule(adapter, rule);
3494 list_del(&rule->list);
3495 adapter->nfc_rule_count--;
3500 static void igc_flush_nfc_rules(struct igc_adapter *adapter)
3502 struct igc_nfc_rule *rule, *tmp;
3504 mutex_lock(&adapter->nfc_rule_lock);
3506 list_for_each_entry_safe(rule, tmp, &adapter->nfc_rule_list, list)
3507 igc_del_nfc_rule(adapter, rule);
3509 mutex_unlock(&adapter->nfc_rule_lock);
3513 * igc_add_nfc_rule() - Add NFC rule
3514 * @adapter: Pointer to adapter
3515 * @rule: Pointer to rule to be added
3517 * Enable NFC rule in hardware and add it to adapter.
3519 * Context: Expects adapter->nfc_rule_lock to be held by caller.
3521 * Return: 0 on success, negative errno on failure.
3523 int igc_add_nfc_rule(struct igc_adapter *adapter, struct igc_nfc_rule *rule)
3525 struct igc_nfc_rule *pred, *cur;
3528 err = igc_enable_nfc_rule(adapter, rule);
3533 list_for_each_entry(cur, &adapter->nfc_rule_list, list) {
3534 if (cur->location >= rule->location)
3539 list_add(&rule->list, pred ? &pred->list : &adapter->nfc_rule_list);
3540 adapter->nfc_rule_count++;
3544 static void igc_restore_nfc_rules(struct igc_adapter *adapter)
3546 struct igc_nfc_rule *rule;
3548 mutex_lock(&adapter->nfc_rule_lock);
3550 list_for_each_entry_reverse(rule, &adapter->nfc_rule_list, list)
3551 igc_enable_nfc_rule(adapter, rule);
3553 mutex_unlock(&adapter->nfc_rule_lock);
3556 static int igc_uc_sync(struct net_device *netdev, const unsigned char *addr)
3558 struct igc_adapter *adapter = netdev_priv(netdev);
3560 return igc_add_mac_filter(adapter, IGC_MAC_FILTER_TYPE_DST, addr, -1);
3563 static int igc_uc_unsync(struct net_device *netdev, const unsigned char *addr)
3565 struct igc_adapter *adapter = netdev_priv(netdev);
3567 igc_del_mac_filter(adapter, IGC_MAC_FILTER_TYPE_DST, addr);
3572 * igc_set_rx_mode - Secondary Unicast, Multicast and Promiscuous mode set
3573 * @netdev: network interface device structure
3575 * The set_rx_mode entry point is called whenever the unicast or multicast
3576 * address lists or the network interface flags are updated. This routine is
3577 * responsible for configuring the hardware for proper unicast, multicast,
3578 * promiscuous mode, and all-multi behavior.
3580 static void igc_set_rx_mode(struct net_device *netdev)
3582 struct igc_adapter *adapter = netdev_priv(netdev);
3583 struct igc_hw *hw = &adapter->hw;
3584 u32 rctl = 0, rlpml = MAX_JUMBO_FRAME_SIZE;
3587 /* Check for Promiscuous and All Multicast modes */
3588 if (netdev->flags & IFF_PROMISC) {
3589 rctl |= IGC_RCTL_UPE | IGC_RCTL_MPE;
3591 if (netdev->flags & IFF_ALLMULTI) {
3592 rctl |= IGC_RCTL_MPE;
3594 /* Write addresses to the MTA, if the attempt fails
3595 * then we should just turn on promiscuous mode so
3596 * that we can at least receive multicast traffic
3598 count = igc_write_mc_addr_list(netdev);
3600 rctl |= IGC_RCTL_MPE;
3604 /* Write addresses to available RAR registers, if there is not
3605 * sufficient space to store all the addresses then enable
3606 * unicast promiscuous mode
3608 if (__dev_uc_sync(netdev, igc_uc_sync, igc_uc_unsync))
3609 rctl |= IGC_RCTL_UPE;
3611 /* update state of unicast and multicast */
3612 rctl |= rd32(IGC_RCTL) & ~(IGC_RCTL_UPE | IGC_RCTL_MPE);
3613 wr32(IGC_RCTL, rctl);
3615 #if (PAGE_SIZE < 8192)
3616 if (adapter->max_frame_size <= IGC_MAX_FRAME_BUILD_SKB)
3617 rlpml = IGC_MAX_FRAME_BUILD_SKB;
3619 wr32(IGC_RLPML, rlpml);
3623 * igc_configure - configure the hardware for RX and TX
3624 * @adapter: private board structure
3626 static void igc_configure(struct igc_adapter *adapter)
3628 struct net_device *netdev = adapter->netdev;
3631 igc_get_hw_control(adapter);
3632 igc_set_rx_mode(netdev);
3634 igc_restore_vlan(adapter);
3636 igc_setup_tctl(adapter);
3637 igc_setup_mrqc(adapter);
3638 igc_setup_rctl(adapter);
3640 igc_set_default_mac_filter(adapter);
3641 igc_restore_nfc_rules(adapter);
3643 igc_configure_tx(adapter);
3644 igc_configure_rx(adapter);
3646 igc_rx_fifo_flush_base(&adapter->hw);
3648 /* call igc_desc_unused which always leaves
3649 * at least 1 descriptor unused to make sure
3650 * next_to_use != next_to_clean
3652 for (i = 0; i < adapter->num_rx_queues; i++) {
3653 struct igc_ring *ring = adapter->rx_ring[i];
3656 igc_alloc_rx_buffers_zc(ring, igc_desc_unused(ring));
3658 igc_alloc_rx_buffers(ring, igc_desc_unused(ring));
3663 * igc_write_ivar - configure ivar for given MSI-X vector
3664 * @hw: pointer to the HW structure
3665 * @msix_vector: vector number we are allocating to a given ring
3666 * @index: row index of IVAR register to write within IVAR table
3667 * @offset: column offset of in IVAR, should be multiple of 8
3669 * The IVAR table consists of 2 columns,
3670 * each containing an cause allocation for an Rx and Tx ring, and a
3671 * variable number of rows depending on the number of queues supported.
3673 static void igc_write_ivar(struct igc_hw *hw, int msix_vector,
3674 int index, int offset)
3676 u32 ivar = array_rd32(IGC_IVAR0, index);
3678 /* clear any bits that are currently set */
3679 ivar &= ~((u32)0xFF << offset);
3681 /* write vector and valid bit */
3682 ivar |= (msix_vector | IGC_IVAR_VALID) << offset;
3684 array_wr32(IGC_IVAR0, index, ivar);
3687 static void igc_assign_vector(struct igc_q_vector *q_vector, int msix_vector)
3689 struct igc_adapter *adapter = q_vector->adapter;
3690 struct igc_hw *hw = &adapter->hw;
3691 int rx_queue = IGC_N0_QUEUE;
3692 int tx_queue = IGC_N0_QUEUE;
3694 if (q_vector->rx.ring)
3695 rx_queue = q_vector->rx.ring->reg_idx;
3696 if (q_vector->tx.ring)
3697 tx_queue = q_vector->tx.ring->reg_idx;
3699 switch (hw->mac.type) {
3701 if (rx_queue > IGC_N0_QUEUE)
3702 igc_write_ivar(hw, msix_vector,
3704 (rx_queue & 0x1) << 4);
3705 if (tx_queue > IGC_N0_QUEUE)
3706 igc_write_ivar(hw, msix_vector,
3708 ((tx_queue & 0x1) << 4) + 8);
3709 q_vector->eims_value = BIT(msix_vector);
3712 WARN_ONCE(hw->mac.type != igc_i225, "Wrong MAC type\n");
3716 /* add q_vector eims value to global eims_enable_mask */
3717 adapter->eims_enable_mask |= q_vector->eims_value;
3719 /* configure q_vector to set itr on first interrupt */
3720 q_vector->set_itr = 1;
3724 * igc_configure_msix - Configure MSI-X hardware
3725 * @adapter: Pointer to adapter structure
3727 * igc_configure_msix sets up the hardware to properly
3728 * generate MSI-X interrupts.
3730 static void igc_configure_msix(struct igc_adapter *adapter)
3732 struct igc_hw *hw = &adapter->hw;
3736 adapter->eims_enable_mask = 0;
3738 /* set vector for other causes, i.e. link changes */
3739 switch (hw->mac.type) {
3741 /* Turn on MSI-X capability first, or our settings
3742 * won't stick. And it will take days to debug.
3744 wr32(IGC_GPIE, IGC_GPIE_MSIX_MODE |
3745 IGC_GPIE_PBA | IGC_GPIE_EIAME |
3748 /* enable msix_other interrupt */
3749 adapter->eims_other = BIT(vector);
3750 tmp = (vector++ | IGC_IVAR_VALID) << 8;
3752 wr32(IGC_IVAR_MISC, tmp);
3755 /* do nothing, since nothing else supports MSI-X */
3757 } /* switch (hw->mac.type) */
3759 adapter->eims_enable_mask |= adapter->eims_other;
3761 for (i = 0; i < adapter->num_q_vectors; i++)
3762 igc_assign_vector(adapter->q_vector[i], vector++);
3768 * igc_irq_enable - Enable default interrupt generation settings
3769 * @adapter: board private structure
3771 static void igc_irq_enable(struct igc_adapter *adapter)
3773 struct igc_hw *hw = &adapter->hw;
3775 if (adapter->msix_entries) {
3776 u32 ims = IGC_IMS_LSC | IGC_IMS_DOUTSYNC | IGC_IMS_DRSTA;
3777 u32 regval = rd32(IGC_EIAC);
3779 wr32(IGC_EIAC, regval | adapter->eims_enable_mask);
3780 regval = rd32(IGC_EIAM);
3781 wr32(IGC_EIAM, regval | adapter->eims_enable_mask);
3782 wr32(IGC_EIMS, adapter->eims_enable_mask);
3785 wr32(IGC_IMS, IMS_ENABLE_MASK | IGC_IMS_DRSTA);
3786 wr32(IGC_IAM, IMS_ENABLE_MASK | IGC_IMS_DRSTA);
3791 * igc_irq_disable - Mask off interrupt generation on the NIC
3792 * @adapter: board private structure
3794 static void igc_irq_disable(struct igc_adapter *adapter)
3796 struct igc_hw *hw = &adapter->hw;
3798 if (adapter->msix_entries) {
3799 u32 regval = rd32(IGC_EIAM);
3801 wr32(IGC_EIAM, regval & ~adapter->eims_enable_mask);
3802 wr32(IGC_EIMC, adapter->eims_enable_mask);
3803 regval = rd32(IGC_EIAC);
3804 wr32(IGC_EIAC, regval & ~adapter->eims_enable_mask);
3811 if (adapter->msix_entries) {
3814 synchronize_irq(adapter->msix_entries[vector++].vector);
3816 for (i = 0; i < adapter->num_q_vectors; i++)
3817 synchronize_irq(adapter->msix_entries[vector++].vector);
3819 synchronize_irq(adapter->pdev->irq);
3823 void igc_set_flag_queue_pairs(struct igc_adapter *adapter,
3824 const u32 max_rss_queues)
3826 /* Determine if we need to pair queues. */
3827 /* If rss_queues > half of max_rss_queues, pair the queues in
3828 * order to conserve interrupts due to limited supply.
3830 if (adapter->rss_queues > (max_rss_queues / 2))
3831 adapter->flags |= IGC_FLAG_QUEUE_PAIRS;
3833 adapter->flags &= ~IGC_FLAG_QUEUE_PAIRS;
3836 unsigned int igc_get_max_rss_queues(struct igc_adapter *adapter)
3838 return IGC_MAX_RX_QUEUES;
3841 static void igc_init_queue_configuration(struct igc_adapter *adapter)
3845 max_rss_queues = igc_get_max_rss_queues(adapter);
3846 adapter->rss_queues = min_t(u32, max_rss_queues, num_online_cpus());
3848 igc_set_flag_queue_pairs(adapter, max_rss_queues);
3852 * igc_reset_q_vector - Reset config for interrupt vector
3853 * @adapter: board private structure to initialize
3854 * @v_idx: Index of vector to be reset
3856 * If NAPI is enabled it will delete any references to the
3857 * NAPI struct. This is preparation for igc_free_q_vector.
3859 static void igc_reset_q_vector(struct igc_adapter *adapter, int v_idx)
3861 struct igc_q_vector *q_vector = adapter->q_vector[v_idx];
3863 /* if we're coming from igc_set_interrupt_capability, the vectors are
3869 if (q_vector->tx.ring)
3870 adapter->tx_ring[q_vector->tx.ring->queue_index] = NULL;
3872 if (q_vector->rx.ring)
3873 adapter->rx_ring[q_vector->rx.ring->queue_index] = NULL;
3875 netif_napi_del(&q_vector->napi);
3879 * igc_free_q_vector - Free memory allocated for specific interrupt vector
3880 * @adapter: board private structure to initialize
3881 * @v_idx: Index of vector to be freed
3883 * This function frees the memory allocated to the q_vector.
3885 static void igc_free_q_vector(struct igc_adapter *adapter, int v_idx)
3887 struct igc_q_vector *q_vector = adapter->q_vector[v_idx];
3889 adapter->q_vector[v_idx] = NULL;
3891 /* igc_get_stats64() might access the rings on this vector,
3892 * we must wait a grace period before freeing it.
3895 kfree_rcu(q_vector, rcu);
3899 * igc_free_q_vectors - Free memory allocated for interrupt vectors
3900 * @adapter: board private structure to initialize
3902 * This function frees the memory allocated to the q_vectors. In addition if
3903 * NAPI is enabled it will delete any references to the NAPI struct prior
3904 * to freeing the q_vector.
3906 static void igc_free_q_vectors(struct igc_adapter *adapter)
3908 int v_idx = adapter->num_q_vectors;
3910 adapter->num_tx_queues = 0;
3911 adapter->num_rx_queues = 0;
3912 adapter->num_q_vectors = 0;
3915 igc_reset_q_vector(adapter, v_idx);
3916 igc_free_q_vector(adapter, v_idx);
3921 * igc_update_itr - update the dynamic ITR value based on statistics
3922 * @q_vector: pointer to q_vector
3923 * @ring_container: ring info to update the itr for
3925 * Stores a new ITR value based on packets and byte
3926 * counts during the last interrupt. The advantage of per interrupt
3927 * computation is faster updates and more accurate ITR for the current
3928 * traffic pattern. Constants in this function were computed
3929 * based on theoretical maximum wire speed and thresholds were set based
3930 * on testing data as well as attempting to minimize response time
3931 * while increasing bulk throughput.
3932 * NOTE: These calculations are only valid when operating in a single-
3933 * queue environment.
3935 static void igc_update_itr(struct igc_q_vector *q_vector,
3936 struct igc_ring_container *ring_container)
3938 unsigned int packets = ring_container->total_packets;
3939 unsigned int bytes = ring_container->total_bytes;
3940 u8 itrval = ring_container->itr;
3942 /* no packets, exit with status unchanged */
3947 case lowest_latency:
3948 /* handle TSO and jumbo frames */
3949 if (bytes / packets > 8000)
3950 itrval = bulk_latency;
3951 else if ((packets < 5) && (bytes > 512))
3952 itrval = low_latency;
3954 case low_latency: /* 50 usec aka 20000 ints/s */
3955 if (bytes > 10000) {
3956 /* this if handles the TSO accounting */
3957 if (bytes / packets > 8000)
3958 itrval = bulk_latency;
3959 else if ((packets < 10) || ((bytes / packets) > 1200))
3960 itrval = bulk_latency;
3961 else if ((packets > 35))
3962 itrval = lowest_latency;
3963 } else if (bytes / packets > 2000) {
3964 itrval = bulk_latency;
3965 } else if (packets <= 2 && bytes < 512) {
3966 itrval = lowest_latency;
3969 case bulk_latency: /* 250 usec aka 4000 ints/s */
3970 if (bytes > 25000) {
3972 itrval = low_latency;
3973 } else if (bytes < 1500) {
3974 itrval = low_latency;
3979 /* clear work counters since we have the values we need */
3980 ring_container->total_bytes = 0;
3981 ring_container->total_packets = 0;
3983 /* write updated itr to ring container */
3984 ring_container->itr = itrval;
3987 static void igc_set_itr(struct igc_q_vector *q_vector)
3989 struct igc_adapter *adapter = q_vector->adapter;
3990 u32 new_itr = q_vector->itr_val;
3993 /* for non-gigabit speeds, just fix the interrupt rate at 4000 */
3994 switch (adapter->link_speed) {
3998 new_itr = IGC_4K_ITR;
4004 igc_update_itr(q_vector, &q_vector->tx);
4005 igc_update_itr(q_vector, &q_vector->rx);
4007 current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
4009 /* conservative mode (itr 3) eliminates the lowest_latency setting */
4010 if (current_itr == lowest_latency &&
4011 ((q_vector->rx.ring && adapter->rx_itr_setting == 3) ||
4012 (!q_vector->rx.ring && adapter->tx_itr_setting == 3)))
4013 current_itr = low_latency;
4015 switch (current_itr) {
4016 /* counts and packets in update_itr are dependent on these numbers */
4017 case lowest_latency:
4018 new_itr = IGC_70K_ITR; /* 70,000 ints/sec */
4021 new_itr = IGC_20K_ITR; /* 20,000 ints/sec */
4024 new_itr = IGC_4K_ITR; /* 4,000 ints/sec */
4031 if (new_itr != q_vector->itr_val) {
4032 /* this attempts to bias the interrupt rate towards Bulk
4033 * by adding intermediate steps when interrupt rate is
4036 new_itr = new_itr > q_vector->itr_val ?
4037 max((new_itr * q_vector->itr_val) /
4038 (new_itr + (q_vector->itr_val >> 2)),
4040 /* Don't write the value here; it resets the adapter's
4041 * internal timer, and causes us to delay far longer than
4042 * we should between interrupts. Instead, we write the ITR
4043 * value at the beginning of the next interrupt so the timing
4044 * ends up being correct.
4046 q_vector->itr_val = new_itr;
4047 q_vector->set_itr = 1;
4051 static void igc_reset_interrupt_capability(struct igc_adapter *adapter)
4053 int v_idx = adapter->num_q_vectors;
4055 if (adapter->msix_entries) {
4056 pci_disable_msix(adapter->pdev);
4057 kfree(adapter->msix_entries);
4058 adapter->msix_entries = NULL;
4059 } else if (adapter->flags & IGC_FLAG_HAS_MSI) {
4060 pci_disable_msi(adapter->pdev);
4064 igc_reset_q_vector(adapter, v_idx);
4068 * igc_set_interrupt_capability - set MSI or MSI-X if supported
4069 * @adapter: Pointer to adapter structure
4070 * @msix: boolean value for MSI-X capability
4072 * Attempt to configure interrupts using the best available
4073 * capabilities of the hardware and kernel.
4075 static void igc_set_interrupt_capability(struct igc_adapter *adapter,
4083 adapter->flags |= IGC_FLAG_HAS_MSIX;
4085 /* Number of supported queues. */
4086 adapter->num_rx_queues = adapter->rss_queues;
4088 adapter->num_tx_queues = adapter->rss_queues;
4090 /* start with one vector for every Rx queue */
4091 numvecs = adapter->num_rx_queues;
4093 /* if Tx handler is separate add 1 for every Tx queue */
4094 if (!(adapter->flags & IGC_FLAG_QUEUE_PAIRS))
4095 numvecs += adapter->num_tx_queues;
4097 /* store the number of vectors reserved for queues */
4098 adapter->num_q_vectors = numvecs;
4100 /* add 1 vector for link status interrupts */
4103 adapter->msix_entries = kcalloc(numvecs, sizeof(struct msix_entry),
4106 if (!adapter->msix_entries)
4109 /* populate entry values */
4110 for (i = 0; i < numvecs; i++)
4111 adapter->msix_entries[i].entry = i;
4113 err = pci_enable_msix_range(adapter->pdev,
4114 adapter->msix_entries,
4120 kfree(adapter->msix_entries);
4121 adapter->msix_entries = NULL;
4123 igc_reset_interrupt_capability(adapter);
4126 adapter->flags &= ~IGC_FLAG_HAS_MSIX;
4128 adapter->rss_queues = 1;
4129 adapter->flags |= IGC_FLAG_QUEUE_PAIRS;
4130 adapter->num_rx_queues = 1;
4131 adapter->num_tx_queues = 1;
4132 adapter->num_q_vectors = 1;
4133 if (!pci_enable_msi(adapter->pdev))
4134 adapter->flags |= IGC_FLAG_HAS_MSI;
4138 * igc_update_ring_itr - update the dynamic ITR value based on packet size
4139 * @q_vector: pointer to q_vector
4141 * Stores a new ITR value based on strictly on packet size. This
4142 * algorithm is less sophisticated than that used in igc_update_itr,
4143 * due to the difficulty of synchronizing statistics across multiple
4144 * receive rings. The divisors and thresholds used by this function
4145 * were determined based on theoretical maximum wire speed and testing
4146 * data, in order to minimize response time while increasing bulk
4148 * NOTE: This function is called only when operating in a multiqueue
4149 * receive environment.
4151 static void igc_update_ring_itr(struct igc_q_vector *q_vector)
4153 struct igc_adapter *adapter = q_vector->adapter;
4154 int new_val = q_vector->itr_val;
4155 int avg_wire_size = 0;
4156 unsigned int packets;
4158 /* For non-gigabit speeds, just fix the interrupt rate at 4000
4159 * ints/sec - ITR timer value of 120 ticks.
4161 switch (adapter->link_speed) {
4164 new_val = IGC_4K_ITR;
4170 packets = q_vector->rx.total_packets;
4172 avg_wire_size = q_vector->rx.total_bytes / packets;
4174 packets = q_vector->tx.total_packets;
4176 avg_wire_size = max_t(u32, avg_wire_size,
4177 q_vector->tx.total_bytes / packets);
4179 /* if avg_wire_size isn't set no work was done */
4183 /* Add 24 bytes to size to account for CRC, preamble, and gap */
4184 avg_wire_size += 24;
4186 /* Don't starve jumbo frames */
4187 avg_wire_size = min(avg_wire_size, 3000);
4189 /* Give a little boost to mid-size frames */
4190 if (avg_wire_size > 300 && avg_wire_size < 1200)
4191 new_val = avg_wire_size / 3;
4193 new_val = avg_wire_size / 2;
4195 /* conservative mode (itr 3) eliminates the lowest_latency setting */
4196 if (new_val < IGC_20K_ITR &&
4197 ((q_vector->rx.ring && adapter->rx_itr_setting == 3) ||
4198 (!q_vector->rx.ring && adapter->tx_itr_setting == 3)))
4199 new_val = IGC_20K_ITR;
4202 if (new_val != q_vector->itr_val) {
4203 q_vector->itr_val = new_val;
4204 q_vector->set_itr = 1;
4207 q_vector->rx.total_bytes = 0;
4208 q_vector->rx.total_packets = 0;
4209 q_vector->tx.total_bytes = 0;
4210 q_vector->tx.total_packets = 0;
4213 static void igc_ring_irq_enable(struct igc_q_vector *q_vector)
4215 struct igc_adapter *adapter = q_vector->adapter;
4216 struct igc_hw *hw = &adapter->hw;
4218 if ((q_vector->rx.ring && (adapter->rx_itr_setting & 3)) ||
4219 (!q_vector->rx.ring && (adapter->tx_itr_setting & 3))) {
4220 if (adapter->num_q_vectors == 1)
4221 igc_set_itr(q_vector);
4223 igc_update_ring_itr(q_vector);
4226 if (!test_bit(__IGC_DOWN, &adapter->state)) {
4227 if (adapter->msix_entries)
4228 wr32(IGC_EIMS, q_vector->eims_value);
4230 igc_irq_enable(adapter);
4234 static void igc_add_ring(struct igc_ring *ring,
4235 struct igc_ring_container *head)
4242 * igc_cache_ring_register - Descriptor ring to register mapping
4243 * @adapter: board private structure to initialize
4245 * Once we know the feature-set enabled for the device, we'll cache
4246 * the register offset the descriptor ring is assigned to.
4248 static void igc_cache_ring_register(struct igc_adapter *adapter)
4252 switch (adapter->hw.mac.type) {
4255 for (; i < adapter->num_rx_queues; i++)
4256 adapter->rx_ring[i]->reg_idx = i;
4257 for (; j < adapter->num_tx_queues; j++)
4258 adapter->tx_ring[j]->reg_idx = j;
4264 * igc_poll - NAPI Rx polling callback
4265 * @napi: napi polling structure
4266 * @budget: count of how many packets we should handle
4268 static int igc_poll(struct napi_struct *napi, int budget)
4270 struct igc_q_vector *q_vector = container_of(napi,
4271 struct igc_q_vector,
4273 struct igc_ring *rx_ring = q_vector->rx.ring;
4274 bool clean_complete = true;
4277 if (q_vector->tx.ring)
4278 clean_complete = igc_clean_tx_irq(q_vector, budget);
4281 int cleaned = rx_ring->xsk_pool ?
4282 igc_clean_rx_irq_zc(q_vector, budget) :
4283 igc_clean_rx_irq(q_vector, budget);
4285 work_done += cleaned;
4286 if (cleaned >= budget)
4287 clean_complete = false;
4290 /* If all work not completed, return budget and keep polling */
4291 if (!clean_complete)
4294 /* Exit the polling mode, but don't re-enable interrupts if stack might
4295 * poll us due to busy-polling
4297 if (likely(napi_complete_done(napi, work_done)))
4298 igc_ring_irq_enable(q_vector);
4300 return min(work_done, budget - 1);
4304 * igc_alloc_q_vector - Allocate memory for a single interrupt vector
4305 * @adapter: board private structure to initialize
4306 * @v_count: q_vectors allocated on adapter, used for ring interleaving
4307 * @v_idx: index of vector in adapter struct
4308 * @txr_count: total number of Tx rings to allocate
4309 * @txr_idx: index of first Tx ring to allocate
4310 * @rxr_count: total number of Rx rings to allocate
4311 * @rxr_idx: index of first Rx ring to allocate
4313 * We allocate one q_vector. If allocation fails we return -ENOMEM.
4315 static int igc_alloc_q_vector(struct igc_adapter *adapter,
4316 unsigned int v_count, unsigned int v_idx,
4317 unsigned int txr_count, unsigned int txr_idx,
4318 unsigned int rxr_count, unsigned int rxr_idx)
4320 struct igc_q_vector *q_vector;
4321 struct igc_ring *ring;
4324 /* igc only supports 1 Tx and/or 1 Rx queue per vector */
4325 if (txr_count > 1 || rxr_count > 1)
4328 ring_count = txr_count + rxr_count;
4330 /* allocate q_vector and rings */
4331 q_vector = adapter->q_vector[v_idx];
4333 q_vector = kzalloc(struct_size(q_vector, ring, ring_count),
4336 memset(q_vector, 0, struct_size(q_vector, ring, ring_count));
4340 /* initialize NAPI */
4341 netif_napi_add(adapter->netdev, &q_vector->napi,
4344 /* tie q_vector and adapter together */
4345 adapter->q_vector[v_idx] = q_vector;
4346 q_vector->adapter = adapter;
4348 /* initialize work limits */
4349 q_vector->tx.work_limit = adapter->tx_work_limit;
4351 /* initialize ITR configuration */
4352 q_vector->itr_register = adapter->io_addr + IGC_EITR(0);
4353 q_vector->itr_val = IGC_START_ITR;
4355 /* initialize pointer to rings */
4356 ring = q_vector->ring;
4358 /* initialize ITR */
4360 /* rx or rx/tx vector */
4361 if (!adapter->rx_itr_setting || adapter->rx_itr_setting > 3)
4362 q_vector->itr_val = adapter->rx_itr_setting;
4364 /* tx only vector */
4365 if (!adapter->tx_itr_setting || adapter->tx_itr_setting > 3)
4366 q_vector->itr_val = adapter->tx_itr_setting;
4370 /* assign generic ring traits */
4371 ring->dev = &adapter->pdev->dev;
4372 ring->netdev = adapter->netdev;
4374 /* configure backlink on ring */
4375 ring->q_vector = q_vector;
4377 /* update q_vector Tx values */
4378 igc_add_ring(ring, &q_vector->tx);
4380 /* apply Tx specific ring traits */
4381 ring->count = adapter->tx_ring_count;
4382 ring->queue_index = txr_idx;
4384 /* assign ring to adapter */
4385 adapter->tx_ring[txr_idx] = ring;
4387 /* push pointer to next ring */
4392 /* assign generic ring traits */
4393 ring->dev = &adapter->pdev->dev;
4394 ring->netdev = adapter->netdev;
4396 /* configure backlink on ring */
4397 ring->q_vector = q_vector;
4399 /* update q_vector Rx values */
4400 igc_add_ring(ring, &q_vector->rx);
4402 /* apply Rx specific ring traits */
4403 ring->count = adapter->rx_ring_count;
4404 ring->queue_index = rxr_idx;
4406 /* assign ring to adapter */
4407 adapter->rx_ring[rxr_idx] = ring;
4414 * igc_alloc_q_vectors - Allocate memory for interrupt vectors
4415 * @adapter: board private structure to initialize
4417 * We allocate one q_vector per queue interrupt. If allocation fails we
4420 static int igc_alloc_q_vectors(struct igc_adapter *adapter)
4422 int rxr_remaining = adapter->num_rx_queues;
4423 int txr_remaining = adapter->num_tx_queues;
4424 int rxr_idx = 0, txr_idx = 0, v_idx = 0;
4425 int q_vectors = adapter->num_q_vectors;
4428 if (q_vectors >= (rxr_remaining + txr_remaining)) {
4429 for (; rxr_remaining; v_idx++) {
4430 err = igc_alloc_q_vector(adapter, q_vectors, v_idx,
4436 /* update counts and index */
4442 for (; v_idx < q_vectors; v_idx++) {
4443 int rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - v_idx);
4444 int tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - v_idx);
4446 err = igc_alloc_q_vector(adapter, q_vectors, v_idx,
4447 tqpv, txr_idx, rqpv, rxr_idx);
4452 /* update counts and index */
4453 rxr_remaining -= rqpv;
4454 txr_remaining -= tqpv;
4462 adapter->num_tx_queues = 0;
4463 adapter->num_rx_queues = 0;
4464 adapter->num_q_vectors = 0;
4467 igc_free_q_vector(adapter, v_idx);
4473 * igc_init_interrupt_scheme - initialize interrupts, allocate queues/vectors
4474 * @adapter: Pointer to adapter structure
4475 * @msix: boolean for MSI-X capability
4477 * This function initializes the interrupts and allocates all of the queues.
4479 static int igc_init_interrupt_scheme(struct igc_adapter *adapter, bool msix)
4481 struct net_device *dev = adapter->netdev;
4484 igc_set_interrupt_capability(adapter, msix);
4486 err = igc_alloc_q_vectors(adapter);
4488 netdev_err(dev, "Unable to allocate memory for vectors\n");
4489 goto err_alloc_q_vectors;
4492 igc_cache_ring_register(adapter);
4496 err_alloc_q_vectors:
4497 igc_reset_interrupt_capability(adapter);
4502 * igc_sw_init - Initialize general software structures (struct igc_adapter)
4503 * @adapter: board private structure to initialize
4505 * igc_sw_init initializes the Adapter private data structure.
4506 * Fields are initialized based on PCI device information and
4507 * OS network device settings (MTU size).
4509 static int igc_sw_init(struct igc_adapter *adapter)
4511 struct net_device *netdev = adapter->netdev;
4512 struct pci_dev *pdev = adapter->pdev;
4513 struct igc_hw *hw = &adapter->hw;
4515 pci_read_config_word(pdev, PCI_COMMAND, &hw->bus.pci_cmd_word);
4517 /* set default ring sizes */
4518 adapter->tx_ring_count = IGC_DEFAULT_TXD;
4519 adapter->rx_ring_count = IGC_DEFAULT_RXD;
4521 /* set default ITR values */
4522 adapter->rx_itr_setting = IGC_DEFAULT_ITR;
4523 adapter->tx_itr_setting = IGC_DEFAULT_ITR;
4525 /* set default work limits */
4526 adapter->tx_work_limit = IGC_DEFAULT_TX_WORK;
4528 /* adjust max frame to be at least the size of a standard frame */
4529 adapter->max_frame_size = netdev->mtu + ETH_HLEN + ETH_FCS_LEN +
4531 adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
4533 mutex_init(&adapter->nfc_rule_lock);
4534 INIT_LIST_HEAD(&adapter->nfc_rule_list);
4535 adapter->nfc_rule_count = 0;
4537 spin_lock_init(&adapter->stats64_lock);
4538 /* Assume MSI-X interrupts, will be checked during IRQ allocation */
4539 adapter->flags |= IGC_FLAG_HAS_MSIX;
4541 igc_init_queue_configuration(adapter);
4543 /* This call may decrease the number of queues */
4544 if (igc_init_interrupt_scheme(adapter, true)) {
4545 netdev_err(netdev, "Unable to allocate memory for queues\n");
4549 /* Explicitly disable IRQ since the NIC can be in any state. */
4550 igc_irq_disable(adapter);
4552 set_bit(__IGC_DOWN, &adapter->state);
4558 * igc_up - Open the interface and prepare it to handle traffic
4559 * @adapter: board private structure
4561 void igc_up(struct igc_adapter *adapter)
4563 struct igc_hw *hw = &adapter->hw;
4566 /* hardware has been reset, we need to reload some things */
4567 igc_configure(adapter);
4569 clear_bit(__IGC_DOWN, &adapter->state);
4571 for (i = 0; i < adapter->num_q_vectors; i++)
4572 napi_enable(&adapter->q_vector[i]->napi);
4574 if (adapter->msix_entries)
4575 igc_configure_msix(adapter);
4577 igc_assign_vector(adapter->q_vector[0], 0);
4579 /* Clear any pending interrupts. */
4581 igc_irq_enable(adapter);
4583 netif_tx_start_all_queues(adapter->netdev);
4585 /* start the watchdog. */
4586 hw->mac.get_link_status = true;
4587 schedule_work(&adapter->watchdog_task);
4591 * igc_update_stats - Update the board statistics counters
4592 * @adapter: board private structure
4594 void igc_update_stats(struct igc_adapter *adapter)
4596 struct rtnl_link_stats64 *net_stats = &adapter->stats64;
4597 struct pci_dev *pdev = adapter->pdev;
4598 struct igc_hw *hw = &adapter->hw;
4599 u64 _bytes, _packets;
4605 /* Prevent stats update while adapter is being reset, or if the pci
4606 * connection is down.
4608 if (adapter->link_speed == 0)
4610 if (pci_channel_offline(pdev))
4617 for (i = 0; i < adapter->num_rx_queues; i++) {
4618 struct igc_ring *ring = adapter->rx_ring[i];
4619 u32 rqdpc = rd32(IGC_RQDPC(i));
4621 if (hw->mac.type >= igc_i225)
4622 wr32(IGC_RQDPC(i), 0);
4625 ring->rx_stats.drops += rqdpc;
4626 net_stats->rx_fifo_errors += rqdpc;
4630 start = u64_stats_fetch_begin_irq(&ring->rx_syncp);
4631 _bytes = ring->rx_stats.bytes;
4632 _packets = ring->rx_stats.packets;
4633 } while (u64_stats_fetch_retry_irq(&ring->rx_syncp, start));
4635 packets += _packets;
4638 net_stats->rx_bytes = bytes;
4639 net_stats->rx_packets = packets;
4643 for (i = 0; i < adapter->num_tx_queues; i++) {
4644 struct igc_ring *ring = adapter->tx_ring[i];
4647 start = u64_stats_fetch_begin_irq(&ring->tx_syncp);
4648 _bytes = ring->tx_stats.bytes;
4649 _packets = ring->tx_stats.packets;
4650 } while (u64_stats_fetch_retry_irq(&ring->tx_syncp, start));
4652 packets += _packets;
4654 net_stats->tx_bytes = bytes;
4655 net_stats->tx_packets = packets;
4658 /* read stats registers */
4659 adapter->stats.crcerrs += rd32(IGC_CRCERRS);
4660 adapter->stats.gprc += rd32(IGC_GPRC);
4661 adapter->stats.gorc += rd32(IGC_GORCL);
4662 rd32(IGC_GORCH); /* clear GORCL */
4663 adapter->stats.bprc += rd32(IGC_BPRC);
4664 adapter->stats.mprc += rd32(IGC_MPRC);
4665 adapter->stats.roc += rd32(IGC_ROC);
4667 adapter->stats.prc64 += rd32(IGC_PRC64);
4668 adapter->stats.prc127 += rd32(IGC_PRC127);
4669 adapter->stats.prc255 += rd32(IGC_PRC255);
4670 adapter->stats.prc511 += rd32(IGC_PRC511);
4671 adapter->stats.prc1023 += rd32(IGC_PRC1023);
4672 adapter->stats.prc1522 += rd32(IGC_PRC1522);
4673 adapter->stats.tlpic += rd32(IGC_TLPIC);
4674 adapter->stats.rlpic += rd32(IGC_RLPIC);
4675 adapter->stats.hgptc += rd32(IGC_HGPTC);
4677 mpc = rd32(IGC_MPC);
4678 adapter->stats.mpc += mpc;
4679 net_stats->rx_fifo_errors += mpc;
4680 adapter->stats.scc += rd32(IGC_SCC);
4681 adapter->stats.ecol += rd32(IGC_ECOL);
4682 adapter->stats.mcc += rd32(IGC_MCC);
4683 adapter->stats.latecol += rd32(IGC_LATECOL);
4684 adapter->stats.dc += rd32(IGC_DC);
4685 adapter->stats.rlec += rd32(IGC_RLEC);
4686 adapter->stats.xonrxc += rd32(IGC_XONRXC);
4687 adapter->stats.xontxc += rd32(IGC_XONTXC);
4688 adapter->stats.xoffrxc += rd32(IGC_XOFFRXC);
4689 adapter->stats.xofftxc += rd32(IGC_XOFFTXC);
4690 adapter->stats.fcruc += rd32(IGC_FCRUC);
4691 adapter->stats.gptc += rd32(IGC_GPTC);
4692 adapter->stats.gotc += rd32(IGC_GOTCL);
4693 rd32(IGC_GOTCH); /* clear GOTCL */
4694 adapter->stats.rnbc += rd32(IGC_RNBC);
4695 adapter->stats.ruc += rd32(IGC_RUC);
4696 adapter->stats.rfc += rd32(IGC_RFC);
4697 adapter->stats.rjc += rd32(IGC_RJC);
4698 adapter->stats.tor += rd32(IGC_TORH);
4699 adapter->stats.tot += rd32(IGC_TOTH);
4700 adapter->stats.tpr += rd32(IGC_TPR);
4702 adapter->stats.ptc64 += rd32(IGC_PTC64);
4703 adapter->stats.ptc127 += rd32(IGC_PTC127);
4704 adapter->stats.ptc255 += rd32(IGC_PTC255);
4705 adapter->stats.ptc511 += rd32(IGC_PTC511);
4706 adapter->stats.ptc1023 += rd32(IGC_PTC1023);
4707 adapter->stats.ptc1522 += rd32(IGC_PTC1522);
4709 adapter->stats.mptc += rd32(IGC_MPTC);
4710 adapter->stats.bptc += rd32(IGC_BPTC);
4712 adapter->stats.tpt += rd32(IGC_TPT);
4713 adapter->stats.colc += rd32(IGC_COLC);
4714 adapter->stats.colc += rd32(IGC_RERC);
4716 adapter->stats.algnerrc += rd32(IGC_ALGNERRC);
4718 adapter->stats.tsctc += rd32(IGC_TSCTC);
4720 adapter->stats.iac += rd32(IGC_IAC);
4722 /* Fill out the OS statistics structure */
4723 net_stats->multicast = adapter->stats.mprc;
4724 net_stats->collisions = adapter->stats.colc;
4728 /* RLEC on some newer hardware can be incorrect so build
4729 * our own version based on RUC and ROC
4731 net_stats->rx_errors = adapter->stats.rxerrc +
4732 adapter->stats.crcerrs + adapter->stats.algnerrc +
4733 adapter->stats.ruc + adapter->stats.roc +
4734 adapter->stats.cexterr;
4735 net_stats->rx_length_errors = adapter->stats.ruc +
4737 net_stats->rx_crc_errors = adapter->stats.crcerrs;
4738 net_stats->rx_frame_errors = adapter->stats.algnerrc;
4739 net_stats->rx_missed_errors = adapter->stats.mpc;
4742 net_stats->tx_errors = adapter->stats.ecol +
4743 adapter->stats.latecol;
4744 net_stats->tx_aborted_errors = adapter->stats.ecol;
4745 net_stats->tx_window_errors = adapter->stats.latecol;
4746 net_stats->tx_carrier_errors = adapter->stats.tncrs;
4748 /* Tx Dropped needs to be maintained elsewhere */
4750 /* Management Stats */
4751 adapter->stats.mgptc += rd32(IGC_MGTPTC);
4752 adapter->stats.mgprc += rd32(IGC_MGTPRC);
4753 adapter->stats.mgpdc += rd32(IGC_MGTPDC);
4757 * igc_down - Close the interface
4758 * @adapter: board private structure
4760 void igc_down(struct igc_adapter *adapter)
4762 struct net_device *netdev = adapter->netdev;
4763 struct igc_hw *hw = &adapter->hw;
4767 set_bit(__IGC_DOWN, &adapter->state);
4769 igc_ptp_suspend(adapter);
4771 if (pci_device_is_present(adapter->pdev)) {
4772 /* disable receives in the hardware */
4773 rctl = rd32(IGC_RCTL);
4774 wr32(IGC_RCTL, rctl & ~IGC_RCTL_EN);
4775 /* flush and sleep below */
4777 /* set trans_start so we don't get spurious watchdogs during reset */
4778 netif_trans_update(netdev);
4780 netif_carrier_off(netdev);
4781 netif_tx_stop_all_queues(netdev);
4783 if (pci_device_is_present(adapter->pdev)) {
4784 /* disable transmits in the hardware */
4785 tctl = rd32(IGC_TCTL);
4786 tctl &= ~IGC_TCTL_EN;
4787 wr32(IGC_TCTL, tctl);
4788 /* flush both disables and wait for them to finish */
4790 usleep_range(10000, 20000);
4792 igc_irq_disable(adapter);
4795 adapter->flags &= ~IGC_FLAG_NEED_LINK_UPDATE;
4797 for (i = 0; i < adapter->num_q_vectors; i++) {
4798 if (adapter->q_vector[i]) {
4799 napi_synchronize(&adapter->q_vector[i]->napi);
4800 napi_disable(&adapter->q_vector[i]->napi);
4804 del_timer_sync(&adapter->watchdog_timer);
4805 del_timer_sync(&adapter->phy_info_timer);
4807 /* record the stats before reset*/
4808 spin_lock(&adapter->stats64_lock);
4809 igc_update_stats(adapter);
4810 spin_unlock(&adapter->stats64_lock);
4812 adapter->link_speed = 0;
4813 adapter->link_duplex = 0;
4815 if (!pci_channel_offline(adapter->pdev))
4818 /* clear VLAN promisc flag so VFTA will be updated if necessary */
4819 adapter->flags &= ~IGC_FLAG_VLAN_PROMISC;
4821 igc_clean_all_tx_rings(adapter);
4822 igc_clean_all_rx_rings(adapter);
4825 void igc_reinit_locked(struct igc_adapter *adapter)
4827 while (test_and_set_bit(__IGC_RESETTING, &adapter->state))
4828 usleep_range(1000, 2000);
4831 clear_bit(__IGC_RESETTING, &adapter->state);
4834 static void igc_reset_task(struct work_struct *work)
4836 struct igc_adapter *adapter;
4838 adapter = container_of(work, struct igc_adapter, reset_task);
4841 /* If we're already down or resetting, just bail */
4842 if (test_bit(__IGC_DOWN, &adapter->state) ||
4843 test_bit(__IGC_RESETTING, &adapter->state)) {
4848 igc_rings_dump(adapter);
4849 igc_regs_dump(adapter);
4850 netdev_err(adapter->netdev, "Reset adapter\n");
4851 igc_reinit_locked(adapter);
4856 * igc_change_mtu - Change the Maximum Transfer Unit
4857 * @netdev: network interface device structure
4858 * @new_mtu: new value for maximum frame size
4860 * Returns 0 on success, negative on failure
4862 static int igc_change_mtu(struct net_device *netdev, int new_mtu)
4864 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
4865 struct igc_adapter *adapter = netdev_priv(netdev);
4867 if (igc_xdp_is_enabled(adapter) && new_mtu > ETH_DATA_LEN) {
4868 netdev_dbg(netdev, "Jumbo frames not supported with XDP");
4872 /* adjust max frame to be at least the size of a standard frame */
4873 if (max_frame < (ETH_FRAME_LEN + ETH_FCS_LEN))
4874 max_frame = ETH_FRAME_LEN + ETH_FCS_LEN;
4876 while (test_and_set_bit(__IGC_RESETTING, &adapter->state))
4877 usleep_range(1000, 2000);
4879 /* igc_down has a dependency on max_frame_size */
4880 adapter->max_frame_size = max_frame;
4882 if (netif_running(netdev))
4885 netdev_dbg(netdev, "changing MTU from %d to %d\n", netdev->mtu, new_mtu);
4886 netdev->mtu = new_mtu;
4888 if (netif_running(netdev))
4893 clear_bit(__IGC_RESETTING, &adapter->state);
4899 * igc_get_stats64 - Get System Network Statistics
4900 * @netdev: network interface device structure
4901 * @stats: rtnl_link_stats64 pointer
4903 * Returns the address of the device statistics structure.
4904 * The statistics are updated here and also from the timer callback.
4906 static void igc_get_stats64(struct net_device *netdev,
4907 struct rtnl_link_stats64 *stats)
4909 struct igc_adapter *adapter = netdev_priv(netdev);
4911 spin_lock(&adapter->stats64_lock);
4912 if (!test_bit(__IGC_RESETTING, &adapter->state))
4913 igc_update_stats(adapter);
4914 memcpy(stats, &adapter->stats64, sizeof(*stats));
4915 spin_unlock(&adapter->stats64_lock);
4918 static netdev_features_t igc_fix_features(struct net_device *netdev,
4919 netdev_features_t features)
4921 /* Since there is no support for separate Rx/Tx vlan accel
4922 * enable/disable make sure Tx flag is always in same state as Rx.
4924 if (features & NETIF_F_HW_VLAN_CTAG_RX)
4925 features |= NETIF_F_HW_VLAN_CTAG_TX;
4927 features &= ~NETIF_F_HW_VLAN_CTAG_TX;
4932 static int igc_set_features(struct net_device *netdev,
4933 netdev_features_t features)
4935 netdev_features_t changed = netdev->features ^ features;
4936 struct igc_adapter *adapter = netdev_priv(netdev);
4938 if (changed & NETIF_F_HW_VLAN_CTAG_RX)
4939 igc_vlan_mode(netdev, features);
4941 /* Add VLAN support */
4942 if (!(changed & (NETIF_F_RXALL | NETIF_F_NTUPLE)))
4945 if (!(features & NETIF_F_NTUPLE))
4946 igc_flush_nfc_rules(adapter);
4948 netdev->features = features;
4950 if (netif_running(netdev))
4951 igc_reinit_locked(adapter);
4958 static netdev_features_t
4959 igc_features_check(struct sk_buff *skb, struct net_device *dev,
4960 netdev_features_t features)
4962 unsigned int network_hdr_len, mac_hdr_len;
4964 /* Make certain the headers can be described by a context descriptor */
4965 mac_hdr_len = skb_network_header(skb) - skb->data;
4966 if (unlikely(mac_hdr_len > IGC_MAX_MAC_HDR_LEN))
4967 return features & ~(NETIF_F_HW_CSUM |
4969 NETIF_F_HW_VLAN_CTAG_TX |
4973 network_hdr_len = skb_checksum_start(skb) - skb_network_header(skb);
4974 if (unlikely(network_hdr_len > IGC_MAX_NETWORK_HDR_LEN))
4975 return features & ~(NETIF_F_HW_CSUM |
4980 /* We can only support IPv4 TSO in tunnels if we can mangle the
4981 * inner IP ID field, so strip TSO if MANGLEID is not supported.
4983 if (skb->encapsulation && !(features & NETIF_F_TSO_MANGLEID))
4984 features &= ~NETIF_F_TSO;
4989 static void igc_tsync_interrupt(struct igc_adapter *adapter)
4991 u32 ack, tsauxc, sec, nsec, tsicr;
4992 struct igc_hw *hw = &adapter->hw;
4993 struct ptp_clock_event event;
4994 struct timespec64 ts;
4996 tsicr = rd32(IGC_TSICR);
4999 if (tsicr & IGC_TSICR_SYS_WRAP) {
5000 event.type = PTP_CLOCK_PPS;
5001 if (adapter->ptp_caps.pps)
5002 ptp_clock_event(adapter->ptp_clock, &event);
5003 ack |= IGC_TSICR_SYS_WRAP;
5006 if (tsicr & IGC_TSICR_TXTS) {
5007 /* retrieve hardware timestamp */
5008 schedule_work(&adapter->ptp_tx_work);
5009 ack |= IGC_TSICR_TXTS;
5012 if (tsicr & IGC_TSICR_TT0) {
5013 spin_lock(&adapter->tmreg_lock);
5014 ts = timespec64_add(adapter->perout[0].start,
5015 adapter->perout[0].period);
5016 wr32(IGC_TRGTTIML0, ts.tv_nsec | IGC_TT_IO_TIMER_SEL_SYSTIM0);
5017 wr32(IGC_TRGTTIMH0, (u32)ts.tv_sec);
5018 tsauxc = rd32(IGC_TSAUXC);
5019 tsauxc |= IGC_TSAUXC_EN_TT0;
5020 wr32(IGC_TSAUXC, tsauxc);
5021 adapter->perout[0].start = ts;
5022 spin_unlock(&adapter->tmreg_lock);
5023 ack |= IGC_TSICR_TT0;
5026 if (tsicr & IGC_TSICR_TT1) {
5027 spin_lock(&adapter->tmreg_lock);
5028 ts = timespec64_add(adapter->perout[1].start,
5029 adapter->perout[1].period);
5030 wr32(IGC_TRGTTIML1, ts.tv_nsec | IGC_TT_IO_TIMER_SEL_SYSTIM0);
5031 wr32(IGC_TRGTTIMH1, (u32)ts.tv_sec);
5032 tsauxc = rd32(IGC_TSAUXC);
5033 tsauxc |= IGC_TSAUXC_EN_TT1;
5034 wr32(IGC_TSAUXC, tsauxc);
5035 adapter->perout[1].start = ts;
5036 spin_unlock(&adapter->tmreg_lock);
5037 ack |= IGC_TSICR_TT1;
5040 if (tsicr & IGC_TSICR_AUTT0) {
5041 nsec = rd32(IGC_AUXSTMPL0);
5042 sec = rd32(IGC_AUXSTMPH0);
5043 event.type = PTP_CLOCK_EXTTS;
5045 event.timestamp = sec * NSEC_PER_SEC + nsec;
5046 ptp_clock_event(adapter->ptp_clock, &event);
5047 ack |= IGC_TSICR_AUTT0;
5050 if (tsicr & IGC_TSICR_AUTT1) {
5051 nsec = rd32(IGC_AUXSTMPL1);
5052 sec = rd32(IGC_AUXSTMPH1);
5053 event.type = PTP_CLOCK_EXTTS;
5055 event.timestamp = sec * NSEC_PER_SEC + nsec;
5056 ptp_clock_event(adapter->ptp_clock, &event);
5057 ack |= IGC_TSICR_AUTT1;
5060 /* acknowledge the interrupts */
5061 wr32(IGC_TSICR, ack);
5065 * igc_msix_other - msix other interrupt handler
5066 * @irq: interrupt number
5067 * @data: pointer to a q_vector
5069 static irqreturn_t igc_msix_other(int irq, void *data)
5071 struct igc_adapter *adapter = data;
5072 struct igc_hw *hw = &adapter->hw;
5073 u32 icr = rd32(IGC_ICR);
5075 /* reading ICR causes bit 31 of EICR to be cleared */
5076 if (icr & IGC_ICR_DRSTA)
5077 schedule_work(&adapter->reset_task);
5079 if (icr & IGC_ICR_DOUTSYNC) {
5080 /* HW is reporting DMA is out of sync */
5081 adapter->stats.doosync++;
5084 if (icr & IGC_ICR_LSC) {
5085 hw->mac.get_link_status = true;
5086 /* guard against interrupt when we're going down */
5087 if (!test_bit(__IGC_DOWN, &adapter->state))
5088 mod_timer(&adapter->watchdog_timer, jiffies + 1);
5091 if (icr & IGC_ICR_TS)
5092 igc_tsync_interrupt(adapter);
5094 wr32(IGC_EIMS, adapter->eims_other);
5099 static void igc_write_itr(struct igc_q_vector *q_vector)
5101 u32 itr_val = q_vector->itr_val & IGC_QVECTOR_MASK;
5103 if (!q_vector->set_itr)
5107 itr_val = IGC_ITR_VAL_MASK;
5109 itr_val |= IGC_EITR_CNT_IGNR;
5111 writel(itr_val, q_vector->itr_register);
5112 q_vector->set_itr = 0;
5115 static irqreturn_t igc_msix_ring(int irq, void *data)
5117 struct igc_q_vector *q_vector = data;
5119 /* Write the ITR value calculated from the previous interrupt. */
5120 igc_write_itr(q_vector);
5122 napi_schedule(&q_vector->napi);
5128 * igc_request_msix - Initialize MSI-X interrupts
5129 * @adapter: Pointer to adapter structure
5131 * igc_request_msix allocates MSI-X vectors and requests interrupts from the
5134 static int igc_request_msix(struct igc_adapter *adapter)
5136 unsigned int num_q_vectors = adapter->num_q_vectors;
5137 int i = 0, err = 0, vector = 0, free_vector = 0;
5138 struct net_device *netdev = adapter->netdev;
5140 err = request_irq(adapter->msix_entries[vector].vector,
5141 &igc_msix_other, 0, netdev->name, adapter);
5145 if (num_q_vectors > MAX_Q_VECTORS) {
5146 num_q_vectors = MAX_Q_VECTORS;
5147 dev_warn(&adapter->pdev->dev,
5148 "The number of queue vectors (%d) is higher than max allowed (%d)\n",
5149 adapter->num_q_vectors, MAX_Q_VECTORS);
5151 for (i = 0; i < num_q_vectors; i++) {
5152 struct igc_q_vector *q_vector = adapter->q_vector[i];
5156 q_vector->itr_register = adapter->io_addr + IGC_EITR(vector);
5158 if (q_vector->rx.ring && q_vector->tx.ring)
5159 sprintf(q_vector->name, "%s-TxRx-%u", netdev->name,
5160 q_vector->rx.ring->queue_index);
5161 else if (q_vector->tx.ring)
5162 sprintf(q_vector->name, "%s-tx-%u", netdev->name,
5163 q_vector->tx.ring->queue_index);
5164 else if (q_vector->rx.ring)
5165 sprintf(q_vector->name, "%s-rx-%u", netdev->name,
5166 q_vector->rx.ring->queue_index);
5168 sprintf(q_vector->name, "%s-unused", netdev->name);
5170 err = request_irq(adapter->msix_entries[vector].vector,
5171 igc_msix_ring, 0, q_vector->name,
5177 igc_configure_msix(adapter);
5181 /* free already assigned IRQs */
5182 free_irq(adapter->msix_entries[free_vector++].vector, adapter);
5185 for (i = 0; i < vector; i++) {
5186 free_irq(adapter->msix_entries[free_vector++].vector,
5187 adapter->q_vector[i]);
5194 * igc_clear_interrupt_scheme - reset the device to a state of no interrupts
5195 * @adapter: Pointer to adapter structure
5197 * This function resets the device so that it has 0 rx queues, tx queues, and
5198 * MSI-X interrupts allocated.
5200 static void igc_clear_interrupt_scheme(struct igc_adapter *adapter)
5202 igc_free_q_vectors(adapter);
5203 igc_reset_interrupt_capability(adapter);
5206 /* Need to wait a few seconds after link up to get diagnostic information from
5209 static void igc_update_phy_info(struct timer_list *t)
5211 struct igc_adapter *adapter = from_timer(adapter, t, phy_info_timer);
5213 igc_get_phy_info(&adapter->hw);
5217 * igc_has_link - check shared code for link and determine up/down
5218 * @adapter: pointer to driver private info
5220 bool igc_has_link(struct igc_adapter *adapter)
5222 struct igc_hw *hw = &adapter->hw;
5223 bool link_active = false;
5225 /* get_link_status is set on LSC (link status) interrupt or
5226 * rx sequence error interrupt. get_link_status will stay
5227 * false until the igc_check_for_link establishes link
5228 * for copper adapters ONLY
5230 if (!hw->mac.get_link_status)
5232 hw->mac.ops.check_for_link(hw);
5233 link_active = !hw->mac.get_link_status;
5235 if (hw->mac.type == igc_i225) {
5236 if (!netif_carrier_ok(adapter->netdev)) {
5237 adapter->flags &= ~IGC_FLAG_NEED_LINK_UPDATE;
5238 } else if (!(adapter->flags & IGC_FLAG_NEED_LINK_UPDATE)) {
5239 adapter->flags |= IGC_FLAG_NEED_LINK_UPDATE;
5240 adapter->link_check_timeout = jiffies;
5248 * igc_watchdog - Timer Call-back
5249 * @t: timer for the watchdog
5251 static void igc_watchdog(struct timer_list *t)
5253 struct igc_adapter *adapter = from_timer(adapter, t, watchdog_timer);
5254 /* Do the rest outside of interrupt context */
5255 schedule_work(&adapter->watchdog_task);
5258 static void igc_watchdog_task(struct work_struct *work)
5260 struct igc_adapter *adapter = container_of(work,
5263 struct net_device *netdev = adapter->netdev;
5264 struct igc_hw *hw = &adapter->hw;
5265 struct igc_phy_info *phy = &hw->phy;
5266 u16 phy_data, retry_count = 20;
5270 link = igc_has_link(adapter);
5272 if (adapter->flags & IGC_FLAG_NEED_LINK_UPDATE) {
5273 if (time_after(jiffies, (adapter->link_check_timeout + HZ)))
5274 adapter->flags &= ~IGC_FLAG_NEED_LINK_UPDATE;
5280 /* Cancel scheduled suspend requests. */
5281 pm_runtime_resume(netdev->dev.parent);
5283 if (!netif_carrier_ok(netdev)) {
5286 hw->mac.ops.get_speed_and_duplex(hw,
5287 &adapter->link_speed,
5288 &adapter->link_duplex);
5290 ctrl = rd32(IGC_CTRL);
5291 /* Link status message must follow this format */
5293 "NIC Link is Up %d Mbps %s Duplex, Flow Control: %s\n",
5294 adapter->link_speed,
5295 adapter->link_duplex == FULL_DUPLEX ?
5297 (ctrl & IGC_CTRL_TFCE) &&
5298 (ctrl & IGC_CTRL_RFCE) ? "RX/TX" :
5299 (ctrl & IGC_CTRL_RFCE) ? "RX" :
5300 (ctrl & IGC_CTRL_TFCE) ? "TX" : "None");
5302 /* disable EEE if enabled */
5303 if ((adapter->flags & IGC_FLAG_EEE) &&
5304 adapter->link_duplex == HALF_DUPLEX) {
5306 "EEE Disabled: unsupported at half duplex. Re-enable using ethtool when at full duplex\n");
5307 adapter->hw.dev_spec._base.eee_enable = false;
5308 adapter->flags &= ~IGC_FLAG_EEE;
5311 /* check if SmartSpeed worked */
5312 igc_check_downshift(hw);
5313 if (phy->speed_downgraded)
5314 netdev_warn(netdev, "Link Speed was downgraded by SmartSpeed\n");
5316 /* adjust timeout factor according to speed/duplex */
5317 adapter->tx_timeout_factor = 1;
5318 switch (adapter->link_speed) {
5320 adapter->tx_timeout_factor = 14;
5325 adapter->tx_timeout_factor = 7;
5329 if (adapter->link_speed != SPEED_1000)
5332 /* wait for Remote receiver status OK */
5334 if (!igc_read_phy_reg(hw, PHY_1000T_STATUS,
5336 if (!(phy_data & SR_1000T_REMOTE_RX_STATUS) &&
5340 goto retry_read_status;
5341 } else if (!retry_count) {
5342 netdev_err(netdev, "exceed max 2 second\n");
5345 netdev_err(netdev, "read 1000Base-T Status Reg\n");
5348 netif_carrier_on(netdev);
5350 /* link state has changed, schedule phy info update */
5351 if (!test_bit(__IGC_DOWN, &adapter->state))
5352 mod_timer(&adapter->phy_info_timer,
5353 round_jiffies(jiffies + 2 * HZ));
5356 if (netif_carrier_ok(netdev)) {
5357 adapter->link_speed = 0;
5358 adapter->link_duplex = 0;
5360 /* Links status message must follow this format */
5361 netdev_info(netdev, "NIC Link is Down\n");
5362 netif_carrier_off(netdev);
5364 /* link state has changed, schedule phy info update */
5365 if (!test_bit(__IGC_DOWN, &adapter->state))
5366 mod_timer(&adapter->phy_info_timer,
5367 round_jiffies(jiffies + 2 * HZ));
5369 /* link is down, time to check for alternate media */
5370 if (adapter->flags & IGC_FLAG_MAS_ENABLE) {
5371 if (adapter->flags & IGC_FLAG_MEDIA_RESET) {
5372 schedule_work(&adapter->reset_task);
5373 /* return immediately */
5377 pm_schedule_suspend(netdev->dev.parent,
5380 /* also check for alternate media here */
5381 } else if (!netif_carrier_ok(netdev) &&
5382 (adapter->flags & IGC_FLAG_MAS_ENABLE)) {
5383 if (adapter->flags & IGC_FLAG_MEDIA_RESET) {
5384 schedule_work(&adapter->reset_task);
5385 /* return immediately */
5391 spin_lock(&adapter->stats64_lock);
5392 igc_update_stats(adapter);
5393 spin_unlock(&adapter->stats64_lock);
5395 for (i = 0; i < adapter->num_tx_queues; i++) {
5396 struct igc_ring *tx_ring = adapter->tx_ring[i];
5398 if (!netif_carrier_ok(netdev)) {
5399 /* We've lost link, so the controller stops DMA,
5400 * but we've got queued Tx work that's never going
5401 * to get done, so reset controller to flush Tx.
5402 * (Do the reset outside of interrupt context).
5404 if (igc_desc_unused(tx_ring) + 1 < tx_ring->count) {
5405 adapter->tx_timeout_count++;
5406 schedule_work(&adapter->reset_task);
5407 /* return immediately since reset is imminent */
5412 /* Force detection of hung controller every watchdog period */
5413 set_bit(IGC_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags);
5416 /* Cause software interrupt to ensure Rx ring is cleaned */
5417 if (adapter->flags & IGC_FLAG_HAS_MSIX) {
5420 for (i = 0; i < adapter->num_q_vectors; i++)
5421 eics |= adapter->q_vector[i]->eims_value;
5422 wr32(IGC_EICS, eics);
5424 wr32(IGC_ICS, IGC_ICS_RXDMT0);
5427 igc_ptp_tx_hang(adapter);
5429 /* Reset the timer */
5430 if (!test_bit(__IGC_DOWN, &adapter->state)) {
5431 if (adapter->flags & IGC_FLAG_NEED_LINK_UPDATE)
5432 mod_timer(&adapter->watchdog_timer,
5433 round_jiffies(jiffies + HZ));
5435 mod_timer(&adapter->watchdog_timer,
5436 round_jiffies(jiffies + 2 * HZ));
5441 * igc_intr_msi - Interrupt Handler
5442 * @irq: interrupt number
5443 * @data: pointer to a network interface device structure
5445 static irqreturn_t igc_intr_msi(int irq, void *data)
5447 struct igc_adapter *adapter = data;
5448 struct igc_q_vector *q_vector = adapter->q_vector[0];
5449 struct igc_hw *hw = &adapter->hw;
5450 /* read ICR disables interrupts using IAM */
5451 u32 icr = rd32(IGC_ICR);
5453 igc_write_itr(q_vector);
5455 if (icr & IGC_ICR_DRSTA)
5456 schedule_work(&adapter->reset_task);
5458 if (icr & IGC_ICR_DOUTSYNC) {
5459 /* HW is reporting DMA is out of sync */
5460 adapter->stats.doosync++;
5463 if (icr & (IGC_ICR_RXSEQ | IGC_ICR_LSC)) {
5464 hw->mac.get_link_status = true;
5465 if (!test_bit(__IGC_DOWN, &adapter->state))
5466 mod_timer(&adapter->watchdog_timer, jiffies + 1);
5469 napi_schedule(&q_vector->napi);
5475 * igc_intr - Legacy Interrupt Handler
5476 * @irq: interrupt number
5477 * @data: pointer to a network interface device structure
5479 static irqreturn_t igc_intr(int irq, void *data)
5481 struct igc_adapter *adapter = data;
5482 struct igc_q_vector *q_vector = adapter->q_vector[0];
5483 struct igc_hw *hw = &adapter->hw;
5484 /* Interrupt Auto-Mask...upon reading ICR, interrupts are masked. No
5485 * need for the IMC write
5487 u32 icr = rd32(IGC_ICR);
5489 /* IMS will not auto-mask if INT_ASSERTED is not set, and if it is
5490 * not set, then the adapter didn't send an interrupt
5492 if (!(icr & IGC_ICR_INT_ASSERTED))
5495 igc_write_itr(q_vector);
5497 if (icr & IGC_ICR_DRSTA)
5498 schedule_work(&adapter->reset_task);
5500 if (icr & IGC_ICR_DOUTSYNC) {
5501 /* HW is reporting DMA is out of sync */
5502 adapter->stats.doosync++;
5505 if (icr & (IGC_ICR_RXSEQ | IGC_ICR_LSC)) {
5506 hw->mac.get_link_status = true;
5507 /* guard against interrupt when we're going down */
5508 if (!test_bit(__IGC_DOWN, &adapter->state))
5509 mod_timer(&adapter->watchdog_timer, jiffies + 1);
5512 napi_schedule(&q_vector->napi);
5517 static void igc_free_irq(struct igc_adapter *adapter)
5519 if (adapter->msix_entries) {
5522 free_irq(adapter->msix_entries[vector++].vector, adapter);
5524 for (i = 0; i < adapter->num_q_vectors; i++)
5525 free_irq(adapter->msix_entries[vector++].vector,
5526 adapter->q_vector[i]);
5528 free_irq(adapter->pdev->irq, adapter);
5533 * igc_request_irq - initialize interrupts
5534 * @adapter: Pointer to adapter structure
5536 * Attempts to configure interrupts using the best available
5537 * capabilities of the hardware and kernel.
5539 static int igc_request_irq(struct igc_adapter *adapter)
5541 struct net_device *netdev = adapter->netdev;
5542 struct pci_dev *pdev = adapter->pdev;
5545 if (adapter->flags & IGC_FLAG_HAS_MSIX) {
5546 err = igc_request_msix(adapter);
5549 /* fall back to MSI */
5550 igc_free_all_tx_resources(adapter);
5551 igc_free_all_rx_resources(adapter);
5553 igc_clear_interrupt_scheme(adapter);
5554 err = igc_init_interrupt_scheme(adapter, false);
5557 igc_setup_all_tx_resources(adapter);
5558 igc_setup_all_rx_resources(adapter);
5559 igc_configure(adapter);
5562 igc_assign_vector(adapter->q_vector[0], 0);
5564 if (adapter->flags & IGC_FLAG_HAS_MSI) {
5565 err = request_irq(pdev->irq, &igc_intr_msi, 0,
5566 netdev->name, adapter);
5570 /* fall back to legacy interrupts */
5571 igc_reset_interrupt_capability(adapter);
5572 adapter->flags &= ~IGC_FLAG_HAS_MSI;
5575 err = request_irq(pdev->irq, &igc_intr, IRQF_SHARED,
5576 netdev->name, adapter);
5579 netdev_err(netdev, "Error %d getting interrupt\n", err);
5586 * __igc_open - Called when a network interface is made active
5587 * @netdev: network interface device structure
5588 * @resuming: boolean indicating if the device is resuming
5590 * Returns 0 on success, negative value on failure
5592 * The open entry point is called when a network interface is made
5593 * active by the system (IFF_UP). At this point all resources needed
5594 * for transmit and receive operations are allocated, the interrupt
5595 * handler is registered with the OS, the watchdog timer is started,
5596 * and the stack is notified that the interface is ready.
5598 static int __igc_open(struct net_device *netdev, bool resuming)
5600 struct igc_adapter *adapter = netdev_priv(netdev);
5601 struct pci_dev *pdev = adapter->pdev;
5602 struct igc_hw *hw = &adapter->hw;
5606 /* disallow open during test */
5608 if (test_bit(__IGC_TESTING, &adapter->state)) {
5614 pm_runtime_get_sync(&pdev->dev);
5616 netif_carrier_off(netdev);
5618 /* allocate transmit descriptors */
5619 err = igc_setup_all_tx_resources(adapter);
5623 /* allocate receive descriptors */
5624 err = igc_setup_all_rx_resources(adapter);
5628 igc_power_up_link(adapter);
5630 igc_configure(adapter);
5632 err = igc_request_irq(adapter);
5636 /* Notify the stack of the actual queue counts. */
5637 err = netif_set_real_num_tx_queues(netdev, adapter->num_tx_queues);
5639 goto err_set_queues;
5641 err = netif_set_real_num_rx_queues(netdev, adapter->num_rx_queues);
5643 goto err_set_queues;
5645 clear_bit(__IGC_DOWN, &adapter->state);
5647 for (i = 0; i < adapter->num_q_vectors; i++)
5648 napi_enable(&adapter->q_vector[i]->napi);
5650 /* Clear any pending interrupts. */
5652 igc_irq_enable(adapter);
5655 pm_runtime_put(&pdev->dev);
5657 netif_tx_start_all_queues(netdev);
5659 /* start the watchdog. */
5660 hw->mac.get_link_status = true;
5661 schedule_work(&adapter->watchdog_task);
5666 igc_free_irq(adapter);
5668 igc_release_hw_control(adapter);
5669 igc_power_down_phy_copper_base(&adapter->hw);
5670 igc_free_all_rx_resources(adapter);
5672 igc_free_all_tx_resources(adapter);
5676 pm_runtime_put(&pdev->dev);
5681 int igc_open(struct net_device *netdev)
5683 return __igc_open(netdev, false);
5687 * __igc_close - Disables a network interface
5688 * @netdev: network interface device structure
5689 * @suspending: boolean indicating the device is suspending
5691 * Returns 0, this is not allowed to fail
5693 * The close entry point is called when an interface is de-activated
5694 * by the OS. The hardware is still under the driver's control, but
5695 * needs to be disabled. A global MAC reset is issued to stop the
5696 * hardware, and all transmit and receive resources are freed.
5698 static int __igc_close(struct net_device *netdev, bool suspending)
5700 struct igc_adapter *adapter = netdev_priv(netdev);
5701 struct pci_dev *pdev = adapter->pdev;
5703 WARN_ON(test_bit(__IGC_RESETTING, &adapter->state));
5706 pm_runtime_get_sync(&pdev->dev);
5710 igc_release_hw_control(adapter);
5712 igc_free_irq(adapter);
5714 igc_free_all_tx_resources(adapter);
5715 igc_free_all_rx_resources(adapter);
5718 pm_runtime_put_sync(&pdev->dev);
5723 int igc_close(struct net_device *netdev)
5725 if (netif_device_present(netdev) || netdev->dismantle)
5726 return __igc_close(netdev, false);
5731 * igc_ioctl - Access the hwtstamp interface
5732 * @netdev: network interface device structure
5733 * @ifr: interface request data
5734 * @cmd: ioctl command
5736 static int igc_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
5740 return igc_ptp_get_ts_config(netdev, ifr);
5742 return igc_ptp_set_ts_config(netdev, ifr);
5748 static int igc_save_launchtime_params(struct igc_adapter *adapter, int queue,
5751 struct igc_ring *ring;
5754 if (queue < 0 || queue >= adapter->num_tx_queues)
5757 ring = adapter->tx_ring[queue];
5758 ring->launchtime_enable = enable;
5760 if (adapter->base_time)
5763 adapter->cycle_time = NSEC_PER_SEC;
5765 for (i = 0; i < adapter->num_tx_queues; i++) {
5766 ring = adapter->tx_ring[i];
5767 ring->start_time = 0;
5768 ring->end_time = NSEC_PER_SEC;
5774 static bool is_base_time_past(ktime_t base_time, const struct timespec64 *now)
5776 struct timespec64 b;
5778 b = ktime_to_timespec64(base_time);
5780 return timespec64_compare(now, &b) > 0;
5783 static bool validate_schedule(struct igc_adapter *adapter,
5784 const struct tc_taprio_qopt_offload *qopt)
5786 int queue_uses[IGC_MAX_TX_QUEUES] = { };
5787 struct timespec64 now;
5790 if (qopt->cycle_time_extension)
5793 igc_ptp_read(adapter, &now);
5795 /* If we program the controller's BASET registers with a time
5796 * in the future, it will hold all the packets until that
5797 * time, causing a lot of TX Hangs, so to avoid that, we
5798 * reject schedules that would start in the future.
5800 if (!is_base_time_past(qopt->base_time, &now))
5803 for (n = 0; n < qopt->num_entries; n++) {
5804 const struct tc_taprio_sched_entry *e;
5807 e = &qopt->entries[n];
5809 /* i225 only supports "global" frame preemption
5812 if (e->command != TC_TAPRIO_CMD_SET_GATES)
5815 for (i = 0; i < adapter->num_tx_queues; i++) {
5816 if (e->gate_mask & BIT(i))
5819 if (queue_uses[i] > 1)
5827 static int igc_tsn_enable_launchtime(struct igc_adapter *adapter,
5828 struct tc_etf_qopt_offload *qopt)
5830 struct igc_hw *hw = &adapter->hw;
5833 if (hw->mac.type != igc_i225)
5836 err = igc_save_launchtime_params(adapter, qopt->queue, qopt->enable);
5840 return igc_tsn_offload_apply(adapter);
5843 static int igc_save_qbv_schedule(struct igc_adapter *adapter,
5844 struct tc_taprio_qopt_offload *qopt)
5846 u32 start_time = 0, end_time = 0;
5849 if (!qopt->enable) {
5850 adapter->base_time = 0;
5854 if (adapter->base_time)
5857 if (!validate_schedule(adapter, qopt))
5860 adapter->cycle_time = qopt->cycle_time;
5861 adapter->base_time = qopt->base_time;
5863 /* FIXME: be a little smarter about cases when the gate for a
5864 * queue stays open for more than one entry.
5866 for (n = 0; n < qopt->num_entries; n++) {
5867 struct tc_taprio_sched_entry *e = &qopt->entries[n];
5870 end_time += e->interval;
5872 for (i = 0; i < adapter->num_tx_queues; i++) {
5873 struct igc_ring *ring = adapter->tx_ring[i];
5875 if (!(e->gate_mask & BIT(i)))
5878 ring->start_time = start_time;
5879 ring->end_time = end_time;
5882 start_time += e->interval;
5888 static int igc_tsn_enable_qbv_scheduling(struct igc_adapter *adapter,
5889 struct tc_taprio_qopt_offload *qopt)
5891 struct igc_hw *hw = &adapter->hw;
5894 if (hw->mac.type != igc_i225)
5897 err = igc_save_qbv_schedule(adapter, qopt);
5901 return igc_tsn_offload_apply(adapter);
5904 static int igc_setup_tc(struct net_device *dev, enum tc_setup_type type,
5907 struct igc_adapter *adapter = netdev_priv(dev);
5910 case TC_SETUP_QDISC_TAPRIO:
5911 return igc_tsn_enable_qbv_scheduling(adapter, type_data);
5913 case TC_SETUP_QDISC_ETF:
5914 return igc_tsn_enable_launchtime(adapter, type_data);
5921 static int igc_bpf(struct net_device *dev, struct netdev_bpf *bpf)
5923 struct igc_adapter *adapter = netdev_priv(dev);
5925 switch (bpf->command) {
5926 case XDP_SETUP_PROG:
5927 return igc_xdp_set_prog(adapter, bpf->prog, bpf->extack);
5928 case XDP_SETUP_XSK_POOL:
5929 return igc_xdp_setup_pool(adapter, bpf->xsk.pool,
5936 static int igc_xdp_xmit(struct net_device *dev, int num_frames,
5937 struct xdp_frame **frames, u32 flags)
5939 struct igc_adapter *adapter = netdev_priv(dev);
5940 int cpu = smp_processor_id();
5941 struct netdev_queue *nq;
5942 struct igc_ring *ring;
5945 if (unlikely(test_bit(__IGC_DOWN, &adapter->state)))
5948 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
5951 ring = igc_xdp_get_tx_ring(adapter, cpu);
5952 nq = txring_txq(ring);
5954 __netif_tx_lock(nq, cpu);
5957 for (i = 0; i < num_frames; i++) {
5959 struct xdp_frame *xdpf = frames[i];
5961 err = igc_xdp_init_tx_descriptor(ring, xdpf);
5963 xdp_return_frame_rx_napi(xdpf);
5968 if (flags & XDP_XMIT_FLUSH)
5969 igc_flush_tx_descriptors(ring);
5971 __netif_tx_unlock(nq);
5973 return num_frames - drops;
5976 static void igc_trigger_rxtxq_interrupt(struct igc_adapter *adapter,
5977 struct igc_q_vector *q_vector)
5979 struct igc_hw *hw = &adapter->hw;
5982 eics |= q_vector->eims_value;
5983 wr32(IGC_EICS, eics);
5986 int igc_xsk_wakeup(struct net_device *dev, u32 queue_id, u32 flags)
5988 struct igc_adapter *adapter = netdev_priv(dev);
5989 struct igc_q_vector *q_vector;
5990 struct igc_ring *ring;
5992 if (test_bit(__IGC_DOWN, &adapter->state))
5995 if (!igc_xdp_is_enabled(adapter))
5998 if (queue_id >= adapter->num_rx_queues)
6001 ring = adapter->rx_ring[queue_id];
6003 if (!ring->xsk_pool)
6006 q_vector = adapter->q_vector[queue_id];
6007 if (!napi_if_scheduled_mark_missed(&q_vector->napi))
6008 igc_trigger_rxtxq_interrupt(adapter, q_vector);
6013 static const struct net_device_ops igc_netdev_ops = {
6014 .ndo_open = igc_open,
6015 .ndo_stop = igc_close,
6016 .ndo_start_xmit = igc_xmit_frame,
6017 .ndo_set_rx_mode = igc_set_rx_mode,
6018 .ndo_set_mac_address = igc_set_mac,
6019 .ndo_change_mtu = igc_change_mtu,
6020 .ndo_get_stats64 = igc_get_stats64,
6021 .ndo_fix_features = igc_fix_features,
6022 .ndo_set_features = igc_set_features,
6023 .ndo_features_check = igc_features_check,
6024 .ndo_eth_ioctl = igc_ioctl,
6025 .ndo_setup_tc = igc_setup_tc,
6027 .ndo_xdp_xmit = igc_xdp_xmit,
6028 .ndo_xsk_wakeup = igc_xsk_wakeup,
6031 /* PCIe configuration access */
6032 void igc_read_pci_cfg(struct igc_hw *hw, u32 reg, u16 *value)
6034 struct igc_adapter *adapter = hw->back;
6036 pci_read_config_word(adapter->pdev, reg, value);
6039 void igc_write_pci_cfg(struct igc_hw *hw, u32 reg, u16 *value)
6041 struct igc_adapter *adapter = hw->back;
6043 pci_write_config_word(adapter->pdev, reg, *value);
6046 s32 igc_read_pcie_cap_reg(struct igc_hw *hw, u32 reg, u16 *value)
6048 struct igc_adapter *adapter = hw->back;
6050 if (!pci_is_pcie(adapter->pdev))
6051 return -IGC_ERR_CONFIG;
6053 pcie_capability_read_word(adapter->pdev, reg, value);
6058 s32 igc_write_pcie_cap_reg(struct igc_hw *hw, u32 reg, u16 *value)
6060 struct igc_adapter *adapter = hw->back;
6062 if (!pci_is_pcie(adapter->pdev))
6063 return -IGC_ERR_CONFIG;
6065 pcie_capability_write_word(adapter->pdev, reg, *value);
6070 u32 igc_rd32(struct igc_hw *hw, u32 reg)
6072 struct igc_adapter *igc = container_of(hw, struct igc_adapter, hw);
6073 u8 __iomem *hw_addr = READ_ONCE(hw->hw_addr);
6076 value = readl(&hw_addr[reg]);
6078 /* reads should not return all F's */
6079 if (!(~value) && (!reg || !(~readl(hw_addr)))) {
6080 struct net_device *netdev = igc->netdev;
6083 netif_device_detach(netdev);
6084 netdev_err(netdev, "PCIe link lost, device now detached\n");
6085 WARN(pci_device_is_present(igc->pdev),
6086 "igc: Failed to read reg 0x%x!\n", reg);
6092 int igc_set_spd_dplx(struct igc_adapter *adapter, u32 spd, u8 dplx)
6094 struct igc_mac_info *mac = &adapter->hw.mac;
6096 mac->autoneg = false;
6098 /* Make sure dplx is at most 1 bit and lsb of speed is not set
6099 * for the switch() below to work
6101 if ((spd & 1) || (dplx & ~1))
6104 switch (spd + dplx) {
6105 case SPEED_10 + DUPLEX_HALF:
6106 mac->forced_speed_duplex = ADVERTISE_10_HALF;
6108 case SPEED_10 + DUPLEX_FULL:
6109 mac->forced_speed_duplex = ADVERTISE_10_FULL;
6111 case SPEED_100 + DUPLEX_HALF:
6112 mac->forced_speed_duplex = ADVERTISE_100_HALF;
6114 case SPEED_100 + DUPLEX_FULL:
6115 mac->forced_speed_duplex = ADVERTISE_100_FULL;
6117 case SPEED_1000 + DUPLEX_FULL:
6118 mac->autoneg = true;
6119 adapter->hw.phy.autoneg_advertised = ADVERTISE_1000_FULL;
6121 case SPEED_1000 + DUPLEX_HALF: /* not supported */
6123 case SPEED_2500 + DUPLEX_FULL:
6124 mac->autoneg = true;
6125 adapter->hw.phy.autoneg_advertised = ADVERTISE_2500_FULL;
6127 case SPEED_2500 + DUPLEX_HALF: /* not supported */
6132 /* clear MDI, MDI(-X) override is only allowed when autoneg enabled */
6133 adapter->hw.phy.mdix = AUTO_ALL_MODES;
6138 netdev_err(adapter->netdev, "Unsupported Speed/Duplex configuration\n");
6143 * igc_probe - Device Initialization Routine
6144 * @pdev: PCI device information struct
6145 * @ent: entry in igc_pci_tbl
6147 * Returns 0 on success, negative on failure
6149 * igc_probe initializes an adapter identified by a pci_dev structure.
6150 * The OS initialization, configuring the adapter private structure,
6151 * and a hardware reset occur.
6153 static int igc_probe(struct pci_dev *pdev,
6154 const struct pci_device_id *ent)
6156 struct igc_adapter *adapter;
6157 struct net_device *netdev;
6159 const struct igc_info *ei = igc_info_tbl[ent->driver_data];
6160 int err, pci_using_dac;
6162 err = pci_enable_device_mem(pdev);
6167 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
6171 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
6174 "No usable DMA configuration, aborting\n");
6179 err = pci_request_mem_regions(pdev, igc_driver_name);
6183 pci_enable_pcie_error_reporting(pdev);
6185 err = pci_enable_ptm(pdev, NULL);
6187 dev_info(&pdev->dev, "PCIe PTM not supported by PCIe bus/controller\n");
6189 pci_set_master(pdev);
6192 netdev = alloc_etherdev_mq(sizeof(struct igc_adapter),
6196 goto err_alloc_etherdev;
6198 SET_NETDEV_DEV(netdev, &pdev->dev);
6200 pci_set_drvdata(pdev, netdev);
6201 adapter = netdev_priv(netdev);
6202 adapter->netdev = netdev;
6203 adapter->pdev = pdev;
6206 adapter->port_num = hw->bus.func;
6207 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
6209 err = pci_save_state(pdev);
6214 adapter->io_addr = ioremap(pci_resource_start(pdev, 0),
6215 pci_resource_len(pdev, 0));
6216 if (!adapter->io_addr)
6219 /* hw->hw_addr can be zeroed, so use adapter->io_addr for unmap */
6220 hw->hw_addr = adapter->io_addr;
6222 netdev->netdev_ops = &igc_netdev_ops;
6223 igc_ethtool_set_ops(netdev);
6224 netdev->watchdog_timeo = 5 * HZ;
6226 netdev->mem_start = pci_resource_start(pdev, 0);
6227 netdev->mem_end = pci_resource_end(pdev, 0);
6229 /* PCI config space info */
6230 hw->vendor_id = pdev->vendor;
6231 hw->device_id = pdev->device;
6232 hw->revision_id = pdev->revision;
6233 hw->subsystem_vendor_id = pdev->subsystem_vendor;
6234 hw->subsystem_device_id = pdev->subsystem_device;
6236 /* Copy the default MAC and PHY function pointers */
6237 memcpy(&hw->mac.ops, ei->mac_ops, sizeof(hw->mac.ops));
6238 memcpy(&hw->phy.ops, ei->phy_ops, sizeof(hw->phy.ops));
6240 /* Initialize skew-specific constants */
6241 err = ei->get_invariants(hw);
6245 /* Add supported features to the features list*/
6246 netdev->features |= NETIF_F_SG;
6247 netdev->features |= NETIF_F_TSO;
6248 netdev->features |= NETIF_F_TSO6;
6249 netdev->features |= NETIF_F_TSO_ECN;
6250 netdev->features |= NETIF_F_RXCSUM;
6251 netdev->features |= NETIF_F_HW_CSUM;
6252 netdev->features |= NETIF_F_SCTP_CRC;
6253 netdev->features |= NETIF_F_HW_TC;
6255 #define IGC_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE | \
6256 NETIF_F_GSO_GRE_CSUM | \
6257 NETIF_F_GSO_IPXIP4 | \
6258 NETIF_F_GSO_IPXIP6 | \
6259 NETIF_F_GSO_UDP_TUNNEL | \
6260 NETIF_F_GSO_UDP_TUNNEL_CSUM)
6262 netdev->gso_partial_features = IGC_GSO_PARTIAL_FEATURES;
6263 netdev->features |= NETIF_F_GSO_PARTIAL | IGC_GSO_PARTIAL_FEATURES;
6265 /* setup the private structure */
6266 err = igc_sw_init(adapter);
6270 /* copy netdev features into list of user selectable features */
6271 netdev->hw_features |= NETIF_F_NTUPLE;
6272 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
6273 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
6274 netdev->hw_features |= netdev->features;
6277 netdev->features |= NETIF_F_HIGHDMA;
6279 netdev->vlan_features |= netdev->features;
6281 /* MTU range: 68 - 9216 */
6282 netdev->min_mtu = ETH_MIN_MTU;
6283 netdev->max_mtu = MAX_STD_JUMBO_FRAME_SIZE;
6285 /* before reading the NVM, reset the controller to put the device in a
6286 * known good starting state
6288 hw->mac.ops.reset_hw(hw);
6290 if (igc_get_flash_presence_i225(hw)) {
6291 if (hw->nvm.ops.validate(hw) < 0) {
6292 dev_err(&pdev->dev, "The NVM Checksum Is Not Valid\n");
6298 if (eth_platform_get_mac_address(&pdev->dev, hw->mac.addr)) {
6299 /* copy the MAC address out of the NVM */
6300 if (hw->mac.ops.read_mac_addr(hw))
6301 dev_err(&pdev->dev, "NVM Read Error\n");
6304 memcpy(netdev->dev_addr, hw->mac.addr, netdev->addr_len);
6306 if (!is_valid_ether_addr(netdev->dev_addr)) {
6307 dev_err(&pdev->dev, "Invalid MAC Address\n");
6312 /* configure RXPBSIZE and TXPBSIZE */
6313 wr32(IGC_RXPBS, I225_RXPBSIZE_DEFAULT);
6314 wr32(IGC_TXPBS, I225_TXPBSIZE_DEFAULT);
6316 timer_setup(&adapter->watchdog_timer, igc_watchdog, 0);
6317 timer_setup(&adapter->phy_info_timer, igc_update_phy_info, 0);
6319 INIT_WORK(&adapter->reset_task, igc_reset_task);
6320 INIT_WORK(&adapter->watchdog_task, igc_watchdog_task);
6322 /* Initialize link properties that are user-changeable */
6323 adapter->fc_autoneg = true;
6324 hw->mac.autoneg = true;
6325 hw->phy.autoneg_advertised = 0xaf;
6327 hw->fc.requested_mode = igc_fc_default;
6328 hw->fc.current_mode = igc_fc_default;
6330 /* By default, support wake on port A */
6331 adapter->flags |= IGC_FLAG_WOL_SUPPORTED;
6333 /* initialize the wol settings based on the eeprom settings */
6334 if (adapter->flags & IGC_FLAG_WOL_SUPPORTED)
6335 adapter->wol |= IGC_WUFC_MAG;
6337 device_set_wakeup_enable(&adapter->pdev->dev,
6338 adapter->flags & IGC_FLAG_WOL_SUPPORTED);
6340 igc_ptp_init(adapter);
6342 /* reset the hardware with the new settings */
6345 /* let the f/w know that the h/w is now under the control of the
6348 igc_get_hw_control(adapter);
6350 strncpy(netdev->name, "eth%d", IFNAMSIZ);
6351 err = register_netdev(netdev);
6355 /* carrier off reporting is important to ethtool even BEFORE open */
6356 netif_carrier_off(netdev);
6358 /* Check if Media Autosense is enabled */
6361 /* print pcie link status and MAC address */
6362 pcie_print_link_status(pdev);
6363 netdev_info(netdev, "MAC: %pM\n", netdev->dev_addr);
6365 dev_pm_set_driver_flags(&pdev->dev, DPM_FLAG_NO_DIRECT_COMPLETE);
6366 /* Disable EEE for internal PHY devices */
6367 hw->dev_spec._base.eee_enable = false;
6368 adapter->flags &= ~IGC_FLAG_EEE;
6369 igc_set_eee_i225(hw, false, false, false);
6371 pm_runtime_put_noidle(&pdev->dev);
6376 igc_release_hw_control(adapter);
6378 if (!igc_check_reset_block(hw))
6381 igc_clear_interrupt_scheme(adapter);
6382 iounmap(adapter->io_addr);
6384 free_netdev(netdev);
6386 pci_disable_pcie_error_reporting(pdev);
6387 pci_release_mem_regions(pdev);
6390 pci_disable_device(pdev);
6395 * igc_remove - Device Removal Routine
6396 * @pdev: PCI device information struct
6398 * igc_remove is called by the PCI subsystem to alert the driver
6399 * that it should release a PCI device. This could be caused by a
6400 * Hot-Plug event, or because the driver is going to be removed from
6403 static void igc_remove(struct pci_dev *pdev)
6405 struct net_device *netdev = pci_get_drvdata(pdev);
6406 struct igc_adapter *adapter = netdev_priv(netdev);
6408 pm_runtime_get_noresume(&pdev->dev);
6410 igc_flush_nfc_rules(adapter);
6412 igc_ptp_stop(adapter);
6414 set_bit(__IGC_DOWN, &adapter->state);
6416 del_timer_sync(&adapter->watchdog_timer);
6417 del_timer_sync(&adapter->phy_info_timer);
6419 cancel_work_sync(&adapter->reset_task);
6420 cancel_work_sync(&adapter->watchdog_task);
6422 /* Release control of h/w to f/w. If f/w is AMT enabled, this
6423 * would have already happened in close and is redundant.
6425 igc_release_hw_control(adapter);
6426 unregister_netdev(netdev);
6428 igc_clear_interrupt_scheme(adapter);
6429 pci_iounmap(pdev, adapter->io_addr);
6430 pci_release_mem_regions(pdev);
6432 free_netdev(netdev);
6434 pci_disable_pcie_error_reporting(pdev);
6436 pci_disable_device(pdev);
6439 static int __igc_shutdown(struct pci_dev *pdev, bool *enable_wake,
6442 struct net_device *netdev = pci_get_drvdata(pdev);
6443 struct igc_adapter *adapter = netdev_priv(netdev);
6444 u32 wufc = runtime ? IGC_WUFC_LNKC : adapter->wol;
6445 struct igc_hw *hw = &adapter->hw;
6446 u32 ctrl, rctl, status;
6450 netif_device_detach(netdev);
6452 if (netif_running(netdev))
6453 __igc_close(netdev, true);
6455 igc_ptp_suspend(adapter);
6457 igc_clear_interrupt_scheme(adapter);
6460 status = rd32(IGC_STATUS);
6461 if (status & IGC_STATUS_LU)
6462 wufc &= ~IGC_WUFC_LNKC;
6465 igc_setup_rctl(adapter);
6466 igc_set_rx_mode(netdev);
6468 /* turn on all-multi mode if wake on multicast is enabled */
6469 if (wufc & IGC_WUFC_MC) {
6470 rctl = rd32(IGC_RCTL);
6471 rctl |= IGC_RCTL_MPE;
6472 wr32(IGC_RCTL, rctl);
6475 ctrl = rd32(IGC_CTRL);
6476 ctrl |= IGC_CTRL_ADVD3WUC;
6477 wr32(IGC_CTRL, ctrl);
6479 /* Allow time for pending master requests to run */
6480 igc_disable_pcie_master(hw);
6482 wr32(IGC_WUC, IGC_WUC_PME_EN);
6483 wr32(IGC_WUFC, wufc);
6489 wake = wufc || adapter->en_mng_pt;
6491 igc_power_down_phy_copper_base(&adapter->hw);
6493 igc_power_up_link(adapter);
6496 *enable_wake = wake;
6498 /* Release control of h/w to f/w. If f/w is AMT enabled, this
6499 * would have already happened in close and is redundant.
6501 igc_release_hw_control(adapter);
6503 pci_disable_device(pdev);
6509 static int __maybe_unused igc_runtime_suspend(struct device *dev)
6511 return __igc_shutdown(to_pci_dev(dev), NULL, 1);
6514 static void igc_deliver_wake_packet(struct net_device *netdev)
6516 struct igc_adapter *adapter = netdev_priv(netdev);
6517 struct igc_hw *hw = &adapter->hw;
6518 struct sk_buff *skb;
6521 wupl = rd32(IGC_WUPL) & IGC_WUPL_MASK;
6523 /* WUPM stores only the first 128 bytes of the wake packet.
6524 * Read the packet only if we have the whole thing.
6526 if (wupl == 0 || wupl > IGC_WUPM_BYTES)
6529 skb = netdev_alloc_skb_ip_align(netdev, IGC_WUPM_BYTES);
6535 /* Ensure reads are 32-bit aligned */
6536 wupl = roundup(wupl, 4);
6538 memcpy_fromio(skb->data, hw->hw_addr + IGC_WUPM_REG(0), wupl);
6540 skb->protocol = eth_type_trans(skb, netdev);
6544 static int __maybe_unused igc_resume(struct device *dev)
6546 struct pci_dev *pdev = to_pci_dev(dev);
6547 struct net_device *netdev = pci_get_drvdata(pdev);
6548 struct igc_adapter *adapter = netdev_priv(netdev);
6549 struct igc_hw *hw = &adapter->hw;
6552 pci_set_power_state(pdev, PCI_D0);
6553 pci_restore_state(pdev);
6554 pci_save_state(pdev);
6556 if (!pci_device_is_present(pdev))
6558 err = pci_enable_device_mem(pdev);
6560 netdev_err(netdev, "Cannot enable PCI device from suspend\n");
6563 pci_set_master(pdev);
6565 pci_enable_wake(pdev, PCI_D3hot, 0);
6566 pci_enable_wake(pdev, PCI_D3cold, 0);
6568 if (igc_init_interrupt_scheme(adapter, true)) {
6569 netdev_err(netdev, "Unable to allocate memory for queues\n");
6575 /* let the f/w know that the h/w is now under the control of the
6578 igc_get_hw_control(adapter);
6580 val = rd32(IGC_WUS);
6581 if (val & WAKE_PKT_WUS)
6582 igc_deliver_wake_packet(netdev);
6587 if (!err && netif_running(netdev))
6588 err = __igc_open(netdev, true);
6591 netif_device_attach(netdev);
6597 static int __maybe_unused igc_runtime_resume(struct device *dev)
6599 return igc_resume(dev);
6602 static int __maybe_unused igc_suspend(struct device *dev)
6604 return __igc_shutdown(to_pci_dev(dev), NULL, 0);
6607 static int __maybe_unused igc_runtime_idle(struct device *dev)
6609 struct net_device *netdev = dev_get_drvdata(dev);
6610 struct igc_adapter *adapter = netdev_priv(netdev);
6612 if (!igc_has_link(adapter))
6613 pm_schedule_suspend(dev, MSEC_PER_SEC * 5);
6617 #endif /* CONFIG_PM */
6619 static void igc_shutdown(struct pci_dev *pdev)
6623 __igc_shutdown(pdev, &wake, 0);
6625 if (system_state == SYSTEM_POWER_OFF) {
6626 pci_wake_from_d3(pdev, wake);
6627 pci_set_power_state(pdev, PCI_D3hot);
6632 * igc_io_error_detected - called when PCI error is detected
6633 * @pdev: Pointer to PCI device
6634 * @state: The current PCI connection state
6636 * This function is called after a PCI bus error affecting
6637 * this device has been detected.
6639 static pci_ers_result_t igc_io_error_detected(struct pci_dev *pdev,
6640 pci_channel_state_t state)
6642 struct net_device *netdev = pci_get_drvdata(pdev);
6643 struct igc_adapter *adapter = netdev_priv(netdev);
6645 netif_device_detach(netdev);
6647 if (state == pci_channel_io_perm_failure)
6648 return PCI_ERS_RESULT_DISCONNECT;
6650 if (netif_running(netdev))
6652 pci_disable_device(pdev);
6654 /* Request a slot reset. */
6655 return PCI_ERS_RESULT_NEED_RESET;
6659 * igc_io_slot_reset - called after the PCI bus has been reset.
6660 * @pdev: Pointer to PCI device
6662 * Restart the card from scratch, as if from a cold-boot. Implementation
6663 * resembles the first-half of the igc_resume routine.
6665 static pci_ers_result_t igc_io_slot_reset(struct pci_dev *pdev)
6667 struct net_device *netdev = pci_get_drvdata(pdev);
6668 struct igc_adapter *adapter = netdev_priv(netdev);
6669 struct igc_hw *hw = &adapter->hw;
6670 pci_ers_result_t result;
6672 if (pci_enable_device_mem(pdev)) {
6673 netdev_err(netdev, "Could not re-enable PCI device after reset\n");
6674 result = PCI_ERS_RESULT_DISCONNECT;
6676 pci_set_master(pdev);
6677 pci_restore_state(pdev);
6678 pci_save_state(pdev);
6680 pci_enable_wake(pdev, PCI_D3hot, 0);
6681 pci_enable_wake(pdev, PCI_D3cold, 0);
6683 /* In case of PCI error, adapter loses its HW address
6684 * so we should re-assign it here.
6686 hw->hw_addr = adapter->io_addr;
6690 result = PCI_ERS_RESULT_RECOVERED;
6697 * igc_io_resume - called when traffic can start to flow again.
6698 * @pdev: Pointer to PCI device
6700 * This callback is called when the error recovery driver tells us that
6701 * its OK to resume normal operation. Implementation resembles the
6702 * second-half of the igc_resume routine.
6704 static void igc_io_resume(struct pci_dev *pdev)
6706 struct net_device *netdev = pci_get_drvdata(pdev);
6707 struct igc_adapter *adapter = netdev_priv(netdev);
6710 if (netif_running(netdev)) {
6711 if (igc_open(netdev)) {
6712 netdev_err(netdev, "igc_open failed after reset\n");
6717 netif_device_attach(netdev);
6719 /* let the f/w know that the h/w is now under the control of the
6722 igc_get_hw_control(adapter);
6726 static const struct pci_error_handlers igc_err_handler = {
6727 .error_detected = igc_io_error_detected,
6728 .slot_reset = igc_io_slot_reset,
6729 .resume = igc_io_resume,
6733 static const struct dev_pm_ops igc_pm_ops = {
6734 SET_SYSTEM_SLEEP_PM_OPS(igc_suspend, igc_resume)
6735 SET_RUNTIME_PM_OPS(igc_runtime_suspend, igc_runtime_resume,
6740 static struct pci_driver igc_driver = {
6741 .name = igc_driver_name,
6742 .id_table = igc_pci_tbl,
6744 .remove = igc_remove,
6746 .driver.pm = &igc_pm_ops,
6748 .shutdown = igc_shutdown,
6749 .err_handler = &igc_err_handler,
6753 * igc_reinit_queues - return error
6754 * @adapter: pointer to adapter structure
6756 int igc_reinit_queues(struct igc_adapter *adapter)
6758 struct net_device *netdev = adapter->netdev;
6761 if (netif_running(netdev))
6764 igc_reset_interrupt_capability(adapter);
6766 if (igc_init_interrupt_scheme(adapter, true)) {
6767 netdev_err(netdev, "Unable to allocate memory for queues\n");
6771 if (netif_running(netdev))
6772 err = igc_open(netdev);
6778 * igc_get_hw_dev - return device
6779 * @hw: pointer to hardware structure
6781 * used by hardware layer to print debugging information
6783 struct net_device *igc_get_hw_dev(struct igc_hw *hw)
6785 struct igc_adapter *adapter = hw->back;
6787 return adapter->netdev;
6790 static void igc_disable_rx_ring_hw(struct igc_ring *ring)
6792 struct igc_hw *hw = &ring->q_vector->adapter->hw;
6793 u8 idx = ring->reg_idx;
6796 rxdctl = rd32(IGC_RXDCTL(idx));
6797 rxdctl &= ~IGC_RXDCTL_QUEUE_ENABLE;
6798 rxdctl |= IGC_RXDCTL_SWFLUSH;
6799 wr32(IGC_RXDCTL(idx), rxdctl);
6802 void igc_disable_rx_ring(struct igc_ring *ring)
6804 igc_disable_rx_ring_hw(ring);
6805 igc_clean_rx_ring(ring);
6808 void igc_enable_rx_ring(struct igc_ring *ring)
6810 struct igc_adapter *adapter = ring->q_vector->adapter;
6812 igc_configure_rx_ring(adapter, ring);
6815 igc_alloc_rx_buffers_zc(ring, igc_desc_unused(ring));
6817 igc_alloc_rx_buffers(ring, igc_desc_unused(ring));
6820 static void igc_disable_tx_ring_hw(struct igc_ring *ring)
6822 struct igc_hw *hw = &ring->q_vector->adapter->hw;
6823 u8 idx = ring->reg_idx;
6826 txdctl = rd32(IGC_TXDCTL(idx));
6827 txdctl &= ~IGC_TXDCTL_QUEUE_ENABLE;
6828 txdctl |= IGC_TXDCTL_SWFLUSH;
6829 wr32(IGC_TXDCTL(idx), txdctl);
6832 void igc_disable_tx_ring(struct igc_ring *ring)
6834 igc_disable_tx_ring_hw(ring);
6835 igc_clean_tx_ring(ring);
6838 void igc_enable_tx_ring(struct igc_ring *ring)
6840 struct igc_adapter *adapter = ring->q_vector->adapter;
6842 igc_configure_tx_ring(adapter, ring);
6846 * igc_init_module - Driver Registration Routine
6848 * igc_init_module is the first routine called when the driver is
6849 * loaded. All it does is register with the PCI subsystem.
6851 static int __init igc_init_module(void)
6855 pr_info("%s\n", igc_driver_string);
6856 pr_info("%s\n", igc_copyright);
6858 ret = pci_register_driver(&igc_driver);
6862 module_init(igc_init_module);
6865 * igc_exit_module - Driver Exit Cleanup Routine
6867 * igc_exit_module is called just before the driver is removed
6870 static void __exit igc_exit_module(void)
6872 pci_unregister_driver(&igc_driver);
6875 module_exit(igc_exit_module);