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;
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_I226_LMVP), board_base },
60 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_IT), board_base },
61 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I226_LM), board_base },
62 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I226_V), board_base },
63 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I226_IT), board_base },
64 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I221_V), board_base },
65 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I226_BLANK_NVM), board_base },
66 { PCI_VDEVICE(INTEL, IGC_DEV_ID_I225_BLANK_NVM), board_base },
67 /* required last entry */
71 MODULE_DEVICE_TABLE(pci, igc_pci_tbl);
80 void igc_reset(struct igc_adapter *adapter)
82 struct net_device *dev = adapter->netdev;
83 struct igc_hw *hw = &adapter->hw;
84 struct igc_fc_info *fc = &hw->fc;
87 /* Repartition PBA for greater than 9k MTU if required */
90 /* flow control settings
91 * The high water mark must be low enough to fit one full frame
92 * after transmitting the pause frame. As such we must have enough
93 * space to allow for us to complete our current transmit and then
94 * receive the frame that is in progress from the link partner.
96 * - the full Rx FIFO size minus one full Tx plus one full Rx frame
98 hwm = (pba << 10) - (adapter->max_frame_size + MAX_JUMBO_FRAME_SIZE);
100 fc->high_water = hwm & 0xFFFFFFF0; /* 16-byte granularity */
101 fc->low_water = fc->high_water - 16;
102 fc->pause_time = 0xFFFF;
104 fc->current_mode = fc->requested_mode;
106 hw->mac.ops.reset_hw(hw);
108 if (hw->mac.ops.init_hw(hw))
109 netdev_err(dev, "Error on hardware initialization\n");
111 /* Re-establish EEE setting */
112 igc_set_eee_i225(hw, true, true, true);
114 if (!netif_running(adapter->netdev))
115 igc_power_down_phy_copper_base(&adapter->hw);
117 /* Enable HW to recognize an 802.1Q VLAN Ethernet packet */
118 wr32(IGC_VET, ETH_P_8021Q);
120 /* Re-enable PTP, where applicable. */
121 igc_ptp_reset(adapter);
123 /* Re-enable TSN offloading, where applicable. */
124 igc_tsn_reset(adapter);
126 igc_get_phy_info(hw);
130 * igc_power_up_link - Power up the phy link
131 * @adapter: address of board private structure
133 static void igc_power_up_link(struct igc_adapter *adapter)
135 igc_reset_phy(&adapter->hw);
137 igc_power_up_phy_copper(&adapter->hw);
139 igc_setup_link(&adapter->hw);
143 * igc_release_hw_control - release control of the h/w to f/w
144 * @adapter: address of board private structure
146 * igc_release_hw_control resets CTRL_EXT:DRV_LOAD bit.
147 * For ASF and Pass Through versions of f/w this means that the
148 * driver is no longer loaded.
150 static void igc_release_hw_control(struct igc_adapter *adapter)
152 struct igc_hw *hw = &adapter->hw;
155 if (!pci_device_is_present(adapter->pdev))
158 /* Let firmware take over control of h/w */
159 ctrl_ext = rd32(IGC_CTRL_EXT);
161 ctrl_ext & ~IGC_CTRL_EXT_DRV_LOAD);
165 * igc_get_hw_control - get control of the h/w from f/w
166 * @adapter: address of board private structure
168 * igc_get_hw_control sets CTRL_EXT:DRV_LOAD bit.
169 * For ASF and Pass Through versions of f/w this means that
170 * the driver is loaded.
172 static void igc_get_hw_control(struct igc_adapter *adapter)
174 struct igc_hw *hw = &adapter->hw;
177 /* Let firmware know the driver has taken over */
178 ctrl_ext = rd32(IGC_CTRL_EXT);
180 ctrl_ext | IGC_CTRL_EXT_DRV_LOAD);
183 static void igc_unmap_tx_buffer(struct device *dev, struct igc_tx_buffer *buf)
185 dma_unmap_single(dev, dma_unmap_addr(buf, dma),
186 dma_unmap_len(buf, len), DMA_TO_DEVICE);
188 dma_unmap_len_set(buf, len, 0);
192 * igc_clean_tx_ring - Free Tx Buffers
193 * @tx_ring: ring to be cleaned
195 static void igc_clean_tx_ring(struct igc_ring *tx_ring)
197 u16 i = tx_ring->next_to_clean;
198 struct igc_tx_buffer *tx_buffer = &tx_ring->tx_buffer_info[i];
201 while (i != tx_ring->next_to_use) {
202 union igc_adv_tx_desc *eop_desc, *tx_desc;
204 switch (tx_buffer->type) {
205 case IGC_TX_BUFFER_TYPE_XSK:
208 case IGC_TX_BUFFER_TYPE_XDP:
209 xdp_return_frame(tx_buffer->xdpf);
210 igc_unmap_tx_buffer(tx_ring->dev, tx_buffer);
212 case IGC_TX_BUFFER_TYPE_SKB:
213 dev_kfree_skb_any(tx_buffer->skb);
214 igc_unmap_tx_buffer(tx_ring->dev, tx_buffer);
217 netdev_warn_once(tx_ring->netdev, "Unknown Tx buffer type\n");
221 /* check for eop_desc to determine the end of the packet */
222 eop_desc = tx_buffer->next_to_watch;
223 tx_desc = IGC_TX_DESC(tx_ring, i);
225 /* unmap remaining buffers */
226 while (tx_desc != eop_desc) {
230 if (unlikely(i == tx_ring->count)) {
232 tx_buffer = tx_ring->tx_buffer_info;
233 tx_desc = IGC_TX_DESC(tx_ring, 0);
236 /* unmap any remaining paged data */
237 if (dma_unmap_len(tx_buffer, len))
238 igc_unmap_tx_buffer(tx_ring->dev, tx_buffer);
241 tx_buffer->next_to_watch = NULL;
243 /* move us one more past the eop_desc for start of next pkt */
246 if (unlikely(i == tx_ring->count)) {
248 tx_buffer = tx_ring->tx_buffer_info;
252 if (tx_ring->xsk_pool && xsk_frames)
253 xsk_tx_completed(tx_ring->xsk_pool, xsk_frames);
255 /* reset BQL for queue */
256 netdev_tx_reset_queue(txring_txq(tx_ring));
258 /* reset next_to_use and next_to_clean */
259 tx_ring->next_to_use = 0;
260 tx_ring->next_to_clean = 0;
264 * igc_free_tx_resources - Free Tx Resources per Queue
265 * @tx_ring: Tx descriptor ring for a specific queue
267 * Free all transmit software resources
269 void igc_free_tx_resources(struct igc_ring *tx_ring)
271 igc_clean_tx_ring(tx_ring);
273 vfree(tx_ring->tx_buffer_info);
274 tx_ring->tx_buffer_info = NULL;
276 /* if not set, then don't free */
280 dma_free_coherent(tx_ring->dev, tx_ring->size,
281 tx_ring->desc, tx_ring->dma);
283 tx_ring->desc = NULL;
287 * igc_free_all_tx_resources - Free Tx Resources for All Queues
288 * @adapter: board private structure
290 * Free all transmit software resources
292 static void igc_free_all_tx_resources(struct igc_adapter *adapter)
296 for (i = 0; i < adapter->num_tx_queues; i++)
297 igc_free_tx_resources(adapter->tx_ring[i]);
301 * igc_clean_all_tx_rings - Free Tx Buffers for all queues
302 * @adapter: board private structure
304 static void igc_clean_all_tx_rings(struct igc_adapter *adapter)
308 for (i = 0; i < adapter->num_tx_queues; i++)
309 if (adapter->tx_ring[i])
310 igc_clean_tx_ring(adapter->tx_ring[i]);
314 * igc_setup_tx_resources - allocate Tx resources (Descriptors)
315 * @tx_ring: tx descriptor ring (for a specific queue) to setup
317 * Return 0 on success, negative on failure
319 int igc_setup_tx_resources(struct igc_ring *tx_ring)
321 struct net_device *ndev = tx_ring->netdev;
322 struct device *dev = tx_ring->dev;
325 size = sizeof(struct igc_tx_buffer) * tx_ring->count;
326 tx_ring->tx_buffer_info = vzalloc(size);
327 if (!tx_ring->tx_buffer_info)
330 /* round up to nearest 4K */
331 tx_ring->size = tx_ring->count * sizeof(union igc_adv_tx_desc);
332 tx_ring->size = ALIGN(tx_ring->size, 4096);
334 tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
335 &tx_ring->dma, GFP_KERNEL);
340 tx_ring->next_to_use = 0;
341 tx_ring->next_to_clean = 0;
346 vfree(tx_ring->tx_buffer_info);
347 netdev_err(ndev, "Unable to allocate memory for Tx descriptor ring\n");
352 * igc_setup_all_tx_resources - wrapper to allocate Tx resources for all queues
353 * @adapter: board private structure
355 * Return 0 on success, negative on failure
357 static int igc_setup_all_tx_resources(struct igc_adapter *adapter)
359 struct net_device *dev = adapter->netdev;
362 for (i = 0; i < adapter->num_tx_queues; i++) {
363 err = igc_setup_tx_resources(adapter->tx_ring[i]);
365 netdev_err(dev, "Error on Tx queue %u setup\n", i);
366 for (i--; i >= 0; i--)
367 igc_free_tx_resources(adapter->tx_ring[i]);
375 static void igc_clean_rx_ring_page_shared(struct igc_ring *rx_ring)
377 u16 i = rx_ring->next_to_clean;
379 dev_kfree_skb(rx_ring->skb);
382 /* Free all the Rx ring sk_buffs */
383 while (i != rx_ring->next_to_alloc) {
384 struct igc_rx_buffer *buffer_info = &rx_ring->rx_buffer_info[i];
386 /* Invalidate cache lines that may have been written to by
387 * device so that we avoid corrupting memory.
389 dma_sync_single_range_for_cpu(rx_ring->dev,
391 buffer_info->page_offset,
392 igc_rx_bufsz(rx_ring),
395 /* free resources associated with mapping */
396 dma_unmap_page_attrs(rx_ring->dev,
398 igc_rx_pg_size(rx_ring),
401 __page_frag_cache_drain(buffer_info->page,
402 buffer_info->pagecnt_bias);
405 if (i == rx_ring->count)
410 static void igc_clean_rx_ring_xsk_pool(struct igc_ring *ring)
412 struct igc_rx_buffer *bi;
415 for (i = 0; i < ring->count; i++) {
416 bi = &ring->rx_buffer_info[i];
420 xsk_buff_free(bi->xdp);
426 * igc_clean_rx_ring - Free Rx Buffers per Queue
427 * @ring: ring to free buffers from
429 static void igc_clean_rx_ring(struct igc_ring *ring)
432 igc_clean_rx_ring_xsk_pool(ring);
434 igc_clean_rx_ring_page_shared(ring);
436 clear_ring_uses_large_buffer(ring);
438 ring->next_to_alloc = 0;
439 ring->next_to_clean = 0;
440 ring->next_to_use = 0;
444 * igc_clean_all_rx_rings - Free Rx Buffers for all queues
445 * @adapter: board private structure
447 static void igc_clean_all_rx_rings(struct igc_adapter *adapter)
451 for (i = 0; i < adapter->num_rx_queues; i++)
452 if (adapter->rx_ring[i])
453 igc_clean_rx_ring(adapter->rx_ring[i]);
457 * igc_free_rx_resources - Free Rx Resources
458 * @rx_ring: ring to clean the resources from
460 * Free all receive software resources
462 void igc_free_rx_resources(struct igc_ring *rx_ring)
464 igc_clean_rx_ring(rx_ring);
466 xdp_rxq_info_unreg(&rx_ring->xdp_rxq);
468 vfree(rx_ring->rx_buffer_info);
469 rx_ring->rx_buffer_info = NULL;
471 /* if not set, then don't free */
475 dma_free_coherent(rx_ring->dev, rx_ring->size,
476 rx_ring->desc, rx_ring->dma);
478 rx_ring->desc = NULL;
482 * igc_free_all_rx_resources - Free Rx Resources for All Queues
483 * @adapter: board private structure
485 * Free all receive software resources
487 static void igc_free_all_rx_resources(struct igc_adapter *adapter)
491 for (i = 0; i < adapter->num_rx_queues; i++)
492 igc_free_rx_resources(adapter->rx_ring[i]);
496 * igc_setup_rx_resources - allocate Rx resources (Descriptors)
497 * @rx_ring: rx descriptor ring (for a specific queue) to setup
499 * Returns 0 on success, negative on failure
501 int igc_setup_rx_resources(struct igc_ring *rx_ring)
503 struct net_device *ndev = rx_ring->netdev;
504 struct device *dev = rx_ring->dev;
505 u8 index = rx_ring->queue_index;
506 int size, desc_len, res;
508 /* XDP RX-queue info */
509 if (xdp_rxq_info_is_reg(&rx_ring->xdp_rxq))
510 xdp_rxq_info_unreg(&rx_ring->xdp_rxq);
511 res = xdp_rxq_info_reg(&rx_ring->xdp_rxq, ndev, index,
512 rx_ring->q_vector->napi.napi_id);
514 netdev_err(ndev, "Failed to register xdp_rxq index %u\n",
519 size = sizeof(struct igc_rx_buffer) * rx_ring->count;
520 rx_ring->rx_buffer_info = vzalloc(size);
521 if (!rx_ring->rx_buffer_info)
524 desc_len = sizeof(union igc_adv_rx_desc);
526 /* Round up to nearest 4K */
527 rx_ring->size = rx_ring->count * desc_len;
528 rx_ring->size = ALIGN(rx_ring->size, 4096);
530 rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
531 &rx_ring->dma, GFP_KERNEL);
536 rx_ring->next_to_alloc = 0;
537 rx_ring->next_to_clean = 0;
538 rx_ring->next_to_use = 0;
543 xdp_rxq_info_unreg(&rx_ring->xdp_rxq);
544 vfree(rx_ring->rx_buffer_info);
545 rx_ring->rx_buffer_info = NULL;
546 netdev_err(ndev, "Unable to allocate memory for Rx descriptor ring\n");
551 * igc_setup_all_rx_resources - wrapper to allocate Rx resources
552 * (Descriptors) for all queues
553 * @adapter: board private structure
555 * Return 0 on success, negative on failure
557 static int igc_setup_all_rx_resources(struct igc_adapter *adapter)
559 struct net_device *dev = adapter->netdev;
562 for (i = 0; i < adapter->num_rx_queues; i++) {
563 err = igc_setup_rx_resources(adapter->rx_ring[i]);
565 netdev_err(dev, "Error on Rx queue %u setup\n", i);
566 for (i--; i >= 0; i--)
567 igc_free_rx_resources(adapter->rx_ring[i]);
575 static struct xsk_buff_pool *igc_get_xsk_pool(struct igc_adapter *adapter,
576 struct igc_ring *ring)
578 if (!igc_xdp_is_enabled(adapter) ||
579 !test_bit(IGC_RING_FLAG_AF_XDP_ZC, &ring->flags))
582 return xsk_get_pool_from_qid(ring->netdev, ring->queue_index);
586 * igc_configure_rx_ring - Configure a receive ring after Reset
587 * @adapter: board private structure
588 * @ring: receive ring to be configured
590 * Configure the Rx unit of the MAC after a reset.
592 static void igc_configure_rx_ring(struct igc_adapter *adapter,
593 struct igc_ring *ring)
595 struct igc_hw *hw = &adapter->hw;
596 union igc_adv_rx_desc *rx_desc;
597 int reg_idx = ring->reg_idx;
598 u32 srrctl = 0, rxdctl = 0;
599 u64 rdba = ring->dma;
602 xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq);
603 ring->xsk_pool = igc_get_xsk_pool(adapter, ring);
604 if (ring->xsk_pool) {
605 WARN_ON(xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
606 MEM_TYPE_XSK_BUFF_POOL,
608 xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
610 WARN_ON(xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
611 MEM_TYPE_PAGE_SHARED,
615 if (igc_xdp_is_enabled(adapter))
616 set_ring_uses_large_buffer(ring);
618 /* disable the queue */
619 wr32(IGC_RXDCTL(reg_idx), 0);
621 /* Set DMA base address registers */
622 wr32(IGC_RDBAL(reg_idx),
623 rdba & 0x00000000ffffffffULL);
624 wr32(IGC_RDBAH(reg_idx), rdba >> 32);
625 wr32(IGC_RDLEN(reg_idx),
626 ring->count * sizeof(union igc_adv_rx_desc));
628 /* initialize head and tail */
629 ring->tail = adapter->io_addr + IGC_RDT(reg_idx);
630 wr32(IGC_RDH(reg_idx), 0);
631 writel(0, ring->tail);
633 /* reset next-to- use/clean to place SW in sync with hardware */
634 ring->next_to_clean = 0;
635 ring->next_to_use = 0;
638 buf_size = xsk_pool_get_rx_frame_size(ring->xsk_pool);
639 else if (ring_uses_large_buffer(ring))
640 buf_size = IGC_RXBUFFER_3072;
642 buf_size = IGC_RXBUFFER_2048;
644 srrctl = IGC_RX_HDR_LEN << IGC_SRRCTL_BSIZEHDRSIZE_SHIFT;
645 srrctl |= buf_size >> IGC_SRRCTL_BSIZEPKT_SHIFT;
646 srrctl |= IGC_SRRCTL_DESCTYPE_ADV_ONEBUF;
648 wr32(IGC_SRRCTL(reg_idx), srrctl);
650 rxdctl |= IGC_RX_PTHRESH;
651 rxdctl |= IGC_RX_HTHRESH << 8;
652 rxdctl |= IGC_RX_WTHRESH << 16;
654 /* initialize rx_buffer_info */
655 memset(ring->rx_buffer_info, 0,
656 sizeof(struct igc_rx_buffer) * ring->count);
658 /* initialize Rx descriptor 0 */
659 rx_desc = IGC_RX_DESC(ring, 0);
660 rx_desc->wb.upper.length = 0;
662 /* enable receive descriptor fetching */
663 rxdctl |= IGC_RXDCTL_QUEUE_ENABLE;
665 wr32(IGC_RXDCTL(reg_idx), rxdctl);
669 * igc_configure_rx - Configure receive Unit after Reset
670 * @adapter: board private structure
672 * Configure the Rx unit of the MAC after a reset.
674 static void igc_configure_rx(struct igc_adapter *adapter)
678 /* Setup the HW Rx Head and Tail Descriptor Pointers and
679 * the Base and Length of the Rx Descriptor Ring
681 for (i = 0; i < adapter->num_rx_queues; i++)
682 igc_configure_rx_ring(adapter, adapter->rx_ring[i]);
686 * igc_configure_tx_ring - Configure transmit ring after Reset
687 * @adapter: board private structure
688 * @ring: tx ring to configure
690 * Configure a transmit ring after a reset.
692 static void igc_configure_tx_ring(struct igc_adapter *adapter,
693 struct igc_ring *ring)
695 struct igc_hw *hw = &adapter->hw;
696 int reg_idx = ring->reg_idx;
697 u64 tdba = ring->dma;
700 ring->xsk_pool = igc_get_xsk_pool(adapter, ring);
702 /* disable the queue */
703 wr32(IGC_TXDCTL(reg_idx), 0);
707 wr32(IGC_TDLEN(reg_idx),
708 ring->count * sizeof(union igc_adv_tx_desc));
709 wr32(IGC_TDBAL(reg_idx),
710 tdba & 0x00000000ffffffffULL);
711 wr32(IGC_TDBAH(reg_idx), tdba >> 32);
713 ring->tail = adapter->io_addr + IGC_TDT(reg_idx);
714 wr32(IGC_TDH(reg_idx), 0);
715 writel(0, ring->tail);
717 txdctl |= IGC_TX_PTHRESH;
718 txdctl |= IGC_TX_HTHRESH << 8;
719 txdctl |= IGC_TX_WTHRESH << 16;
721 txdctl |= IGC_TXDCTL_QUEUE_ENABLE;
722 wr32(IGC_TXDCTL(reg_idx), txdctl);
726 * igc_configure_tx - Configure transmit Unit after Reset
727 * @adapter: board private structure
729 * Configure the Tx unit of the MAC after a reset.
731 static void igc_configure_tx(struct igc_adapter *adapter)
735 for (i = 0; i < adapter->num_tx_queues; i++)
736 igc_configure_tx_ring(adapter, adapter->tx_ring[i]);
740 * igc_setup_mrqc - configure the multiple receive queue control registers
741 * @adapter: Board private structure
743 static void igc_setup_mrqc(struct igc_adapter *adapter)
745 struct igc_hw *hw = &adapter->hw;
746 u32 j, num_rx_queues;
750 netdev_rss_key_fill(rss_key, sizeof(rss_key));
751 for (j = 0; j < 10; j++)
752 wr32(IGC_RSSRK(j), rss_key[j]);
754 num_rx_queues = adapter->rss_queues;
756 if (adapter->rss_indir_tbl_init != num_rx_queues) {
757 for (j = 0; j < IGC_RETA_SIZE; j++)
758 adapter->rss_indir_tbl[j] =
759 (j * num_rx_queues) / IGC_RETA_SIZE;
760 adapter->rss_indir_tbl_init = num_rx_queues;
762 igc_write_rss_indir_tbl(adapter);
764 /* Disable raw packet checksumming so that RSS hash is placed in
765 * descriptor on writeback. No need to enable TCP/UDP/IP checksum
766 * offloads as they are enabled by default
768 rxcsum = rd32(IGC_RXCSUM);
769 rxcsum |= IGC_RXCSUM_PCSD;
771 /* Enable Receive Checksum Offload for SCTP */
772 rxcsum |= IGC_RXCSUM_CRCOFL;
774 /* Don't need to set TUOFL or IPOFL, they default to 1 */
775 wr32(IGC_RXCSUM, rxcsum);
777 /* Generate RSS hash based on packet types, TCP/UDP
778 * port numbers and/or IPv4/v6 src and dst addresses
780 mrqc = IGC_MRQC_RSS_FIELD_IPV4 |
781 IGC_MRQC_RSS_FIELD_IPV4_TCP |
782 IGC_MRQC_RSS_FIELD_IPV6 |
783 IGC_MRQC_RSS_FIELD_IPV6_TCP |
784 IGC_MRQC_RSS_FIELD_IPV6_TCP_EX;
786 if (adapter->flags & IGC_FLAG_RSS_FIELD_IPV4_UDP)
787 mrqc |= IGC_MRQC_RSS_FIELD_IPV4_UDP;
788 if (adapter->flags & IGC_FLAG_RSS_FIELD_IPV6_UDP)
789 mrqc |= IGC_MRQC_RSS_FIELD_IPV6_UDP;
791 mrqc |= IGC_MRQC_ENABLE_RSS_MQ;
793 wr32(IGC_MRQC, mrqc);
797 * igc_setup_rctl - configure the receive control registers
798 * @adapter: Board private structure
800 static void igc_setup_rctl(struct igc_adapter *adapter)
802 struct igc_hw *hw = &adapter->hw;
805 rctl = rd32(IGC_RCTL);
807 rctl &= ~(3 << IGC_RCTL_MO_SHIFT);
808 rctl &= ~(IGC_RCTL_LBM_TCVR | IGC_RCTL_LBM_MAC);
810 rctl |= IGC_RCTL_EN | IGC_RCTL_BAM | IGC_RCTL_RDMTS_HALF |
811 (hw->mac.mc_filter_type << IGC_RCTL_MO_SHIFT);
813 /* enable stripping of CRC. Newer features require
814 * that the HW strips the CRC.
816 rctl |= IGC_RCTL_SECRC;
818 /* disable store bad packets and clear size bits. */
819 rctl &= ~(IGC_RCTL_SBP | IGC_RCTL_SZ_256);
821 /* enable LPE to allow for reception of jumbo frames */
822 rctl |= IGC_RCTL_LPE;
824 /* disable queue 0 to prevent tail write w/o re-config */
825 wr32(IGC_RXDCTL(0), 0);
827 /* This is useful for sniffing bad packets. */
828 if (adapter->netdev->features & NETIF_F_RXALL) {
829 /* UPE and MPE will be handled by normal PROMISC logic
832 rctl |= (IGC_RCTL_SBP | /* Receive bad packets */
833 IGC_RCTL_BAM | /* RX All Bcast Pkts */
834 IGC_RCTL_PMCF); /* RX All MAC Ctrl Pkts */
836 rctl &= ~(IGC_RCTL_DPF | /* Allow filtered pause */
837 IGC_RCTL_CFIEN); /* Disable VLAN CFIEN Filter */
840 wr32(IGC_RCTL, rctl);
844 * igc_setup_tctl - configure the transmit control registers
845 * @adapter: Board private structure
847 static void igc_setup_tctl(struct igc_adapter *adapter)
849 struct igc_hw *hw = &adapter->hw;
852 /* disable queue 0 which icould be enabled by default */
853 wr32(IGC_TXDCTL(0), 0);
855 /* Program the Transmit Control Register */
856 tctl = rd32(IGC_TCTL);
857 tctl &= ~IGC_TCTL_CT;
858 tctl |= IGC_TCTL_PSP | IGC_TCTL_RTLC |
859 (IGC_COLLISION_THRESHOLD << IGC_CT_SHIFT);
861 /* Enable transmits */
864 wr32(IGC_TCTL, tctl);
868 * igc_set_mac_filter_hw() - Set MAC address filter in hardware
869 * @adapter: Pointer to adapter where the filter should be set
870 * @index: Filter index
871 * @type: MAC address filter type (source or destination)
873 * @queue: If non-negative, queue assignment feature is enabled and frames
874 * matching the filter are enqueued onto 'queue'. Otherwise, queue
875 * assignment is disabled.
877 static void igc_set_mac_filter_hw(struct igc_adapter *adapter, int index,
878 enum igc_mac_filter_type type,
879 const u8 *addr, int queue)
881 struct net_device *dev = adapter->netdev;
882 struct igc_hw *hw = &adapter->hw;
885 if (WARN_ON(index >= hw->mac.rar_entry_count))
888 ral = le32_to_cpup((__le32 *)(addr));
889 rah = le16_to_cpup((__le16 *)(addr + 4));
891 if (type == IGC_MAC_FILTER_TYPE_SRC) {
892 rah &= ~IGC_RAH_ASEL_MASK;
893 rah |= IGC_RAH_ASEL_SRC_ADDR;
897 rah &= ~IGC_RAH_QSEL_MASK;
898 rah |= (queue << IGC_RAH_QSEL_SHIFT);
899 rah |= IGC_RAH_QSEL_ENABLE;
904 wr32(IGC_RAL(index), ral);
905 wr32(IGC_RAH(index), rah);
907 netdev_dbg(dev, "MAC address filter set in HW: index %d", index);
911 * igc_clear_mac_filter_hw() - Clear MAC address filter in hardware
912 * @adapter: Pointer to adapter where the filter should be cleared
913 * @index: Filter index
915 static void igc_clear_mac_filter_hw(struct igc_adapter *adapter, int index)
917 struct net_device *dev = adapter->netdev;
918 struct igc_hw *hw = &adapter->hw;
920 if (WARN_ON(index >= hw->mac.rar_entry_count))
923 wr32(IGC_RAL(index), 0);
924 wr32(IGC_RAH(index), 0);
926 netdev_dbg(dev, "MAC address filter cleared in HW: index %d", index);
929 /* Set default MAC address for the PF in the first RAR entry */
930 static void igc_set_default_mac_filter(struct igc_adapter *adapter)
932 struct net_device *dev = adapter->netdev;
933 u8 *addr = adapter->hw.mac.addr;
935 netdev_dbg(dev, "Set default MAC address filter: address %pM", addr);
937 igc_set_mac_filter_hw(adapter, 0, IGC_MAC_FILTER_TYPE_DST, addr, -1);
941 * igc_set_mac - Change the Ethernet Address of the NIC
942 * @netdev: network interface device structure
943 * @p: pointer to an address structure
945 * Returns 0 on success, negative on failure
947 static int igc_set_mac(struct net_device *netdev, void *p)
949 struct igc_adapter *adapter = netdev_priv(netdev);
950 struct igc_hw *hw = &adapter->hw;
951 struct sockaddr *addr = p;
953 if (!is_valid_ether_addr(addr->sa_data))
954 return -EADDRNOTAVAIL;
956 eth_hw_addr_set(netdev, addr->sa_data);
957 memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
959 /* set the correct pool for the new PF MAC address in entry 0 */
960 igc_set_default_mac_filter(adapter);
966 * igc_write_mc_addr_list - write multicast addresses to MTA
967 * @netdev: network interface device structure
969 * Writes multicast address list to the MTA hash table.
970 * Returns: -ENOMEM on failure
971 * 0 on no addresses written
972 * X on writing X addresses to MTA
974 static int igc_write_mc_addr_list(struct net_device *netdev)
976 struct igc_adapter *adapter = netdev_priv(netdev);
977 struct igc_hw *hw = &adapter->hw;
978 struct netdev_hw_addr *ha;
982 if (netdev_mc_empty(netdev)) {
983 /* nothing to program, so clear mc list */
984 igc_update_mc_addr_list(hw, NULL, 0);
988 mta_list = kcalloc(netdev_mc_count(netdev), 6, GFP_ATOMIC);
992 /* The shared function expects a packed array of only addresses. */
994 netdev_for_each_mc_addr(ha, netdev)
995 memcpy(mta_list + (i++ * ETH_ALEN), ha->addr, ETH_ALEN);
997 igc_update_mc_addr_list(hw, mta_list, i);
1000 return netdev_mc_count(netdev);
1003 static __le32 igc_tx_launchtime(struct igc_adapter *adapter, ktime_t txtime)
1005 ktime_t cycle_time = adapter->cycle_time;
1006 ktime_t base_time = adapter->base_time;
1009 /* FIXME: when using ETF together with taprio, we may have a
1010 * case where 'delta' is larger than the cycle_time, this may
1011 * cause problems if we don't read the current value of
1012 * IGC_BASET, as the value writen into the launchtime
1013 * descriptor field may be misinterpreted.
1015 div_s64_rem(ktime_sub_ns(txtime, base_time), cycle_time, &launchtime);
1017 return cpu_to_le32(launchtime);
1020 static void igc_tx_ctxtdesc(struct igc_ring *tx_ring,
1021 struct igc_tx_buffer *first,
1022 u32 vlan_macip_lens, u32 type_tucmd,
1025 struct igc_adv_tx_context_desc *context_desc;
1026 u16 i = tx_ring->next_to_use;
1028 context_desc = IGC_TX_CTXTDESC(tx_ring, i);
1031 tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
1033 /* set bits to identify this as an advanced context descriptor */
1034 type_tucmd |= IGC_TXD_CMD_DEXT | IGC_ADVTXD_DTYP_CTXT;
1036 /* For i225, context index must be unique per ring. */
1037 if (test_bit(IGC_RING_FLAG_TX_CTX_IDX, &tx_ring->flags))
1038 mss_l4len_idx |= tx_ring->reg_idx << 4;
1040 context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
1041 context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd);
1042 context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
1044 /* We assume there is always a valid Tx time available. Invalid times
1045 * should have been handled by the upper layers.
1047 if (tx_ring->launchtime_enable) {
1048 struct igc_adapter *adapter = netdev_priv(tx_ring->netdev);
1049 ktime_t txtime = first->skb->tstamp;
1051 skb_txtime_consumed(first->skb);
1052 context_desc->launch_time = igc_tx_launchtime(adapter,
1055 context_desc->launch_time = 0;
1059 static void igc_tx_csum(struct igc_ring *tx_ring, struct igc_tx_buffer *first)
1061 struct sk_buff *skb = first->skb;
1062 u32 vlan_macip_lens = 0;
1065 if (skb->ip_summed != CHECKSUM_PARTIAL) {
1067 if (!(first->tx_flags & IGC_TX_FLAGS_VLAN) &&
1068 !tx_ring->launchtime_enable)
1073 switch (skb->csum_offset) {
1074 case offsetof(struct tcphdr, check):
1075 type_tucmd = IGC_ADVTXD_TUCMD_L4T_TCP;
1077 case offsetof(struct udphdr, check):
1079 case offsetof(struct sctphdr, checksum):
1080 /* validate that this is actually an SCTP request */
1081 if (skb_csum_is_sctp(skb)) {
1082 type_tucmd = IGC_ADVTXD_TUCMD_L4T_SCTP;
1087 skb_checksum_help(skb);
1091 /* update TX checksum flag */
1092 first->tx_flags |= IGC_TX_FLAGS_CSUM;
1093 vlan_macip_lens = skb_checksum_start_offset(skb) -
1094 skb_network_offset(skb);
1096 vlan_macip_lens |= skb_network_offset(skb) << IGC_ADVTXD_MACLEN_SHIFT;
1097 vlan_macip_lens |= first->tx_flags & IGC_TX_FLAGS_VLAN_MASK;
1099 igc_tx_ctxtdesc(tx_ring, first, vlan_macip_lens, type_tucmd, 0);
1102 static int __igc_maybe_stop_tx(struct igc_ring *tx_ring, const u16 size)
1104 struct net_device *netdev = tx_ring->netdev;
1106 netif_stop_subqueue(netdev, tx_ring->queue_index);
1108 /* memory barriier comment */
1111 /* We need to check again in a case another CPU has just
1112 * made room available.
1114 if (igc_desc_unused(tx_ring) < size)
1118 netif_wake_subqueue(netdev, tx_ring->queue_index);
1120 u64_stats_update_begin(&tx_ring->tx_syncp2);
1121 tx_ring->tx_stats.restart_queue2++;
1122 u64_stats_update_end(&tx_ring->tx_syncp2);
1127 static inline int igc_maybe_stop_tx(struct igc_ring *tx_ring, const u16 size)
1129 if (igc_desc_unused(tx_ring) >= size)
1131 return __igc_maybe_stop_tx(tx_ring, size);
1134 #define IGC_SET_FLAG(_input, _flag, _result) \
1135 (((_flag) <= (_result)) ? \
1136 ((u32)((_input) & (_flag)) * ((_result) / (_flag))) : \
1137 ((u32)((_input) & (_flag)) / ((_flag) / (_result))))
1139 static u32 igc_tx_cmd_type(struct sk_buff *skb, u32 tx_flags)
1141 /* set type for advanced descriptor with frame checksum insertion */
1142 u32 cmd_type = IGC_ADVTXD_DTYP_DATA |
1143 IGC_ADVTXD_DCMD_DEXT |
1144 IGC_ADVTXD_DCMD_IFCS;
1146 /* set HW vlan bit if vlan is present */
1147 cmd_type |= IGC_SET_FLAG(tx_flags, IGC_TX_FLAGS_VLAN,
1148 IGC_ADVTXD_DCMD_VLE);
1150 /* set segmentation bits for TSO */
1151 cmd_type |= IGC_SET_FLAG(tx_flags, IGC_TX_FLAGS_TSO,
1152 (IGC_ADVTXD_DCMD_TSE));
1154 /* set timestamp bit if present */
1155 cmd_type |= IGC_SET_FLAG(tx_flags, IGC_TX_FLAGS_TSTAMP,
1156 (IGC_ADVTXD_MAC_TSTAMP));
1158 /* insert frame checksum */
1159 cmd_type ^= IGC_SET_FLAG(skb->no_fcs, 1, IGC_ADVTXD_DCMD_IFCS);
1164 static void igc_tx_olinfo_status(struct igc_ring *tx_ring,
1165 union igc_adv_tx_desc *tx_desc,
1166 u32 tx_flags, unsigned int paylen)
1168 u32 olinfo_status = paylen << IGC_ADVTXD_PAYLEN_SHIFT;
1170 /* insert L4 checksum */
1171 olinfo_status |= (tx_flags & IGC_TX_FLAGS_CSUM) *
1172 ((IGC_TXD_POPTS_TXSM << 8) /
1175 /* insert IPv4 checksum */
1176 olinfo_status |= (tx_flags & IGC_TX_FLAGS_IPV4) *
1177 (((IGC_TXD_POPTS_IXSM << 8)) /
1180 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
1183 static int igc_tx_map(struct igc_ring *tx_ring,
1184 struct igc_tx_buffer *first,
1187 struct sk_buff *skb = first->skb;
1188 struct igc_tx_buffer *tx_buffer;
1189 union igc_adv_tx_desc *tx_desc;
1190 u32 tx_flags = first->tx_flags;
1192 u16 i = tx_ring->next_to_use;
1193 unsigned int data_len, size;
1197 cmd_type = igc_tx_cmd_type(skb, tx_flags);
1198 tx_desc = IGC_TX_DESC(tx_ring, i);
1200 igc_tx_olinfo_status(tx_ring, tx_desc, tx_flags, skb->len - hdr_len);
1202 size = skb_headlen(skb);
1203 data_len = skb->data_len;
1205 dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
1209 for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
1210 if (dma_mapping_error(tx_ring->dev, dma))
1213 /* record length, and DMA address */
1214 dma_unmap_len_set(tx_buffer, len, size);
1215 dma_unmap_addr_set(tx_buffer, dma, dma);
1217 tx_desc->read.buffer_addr = cpu_to_le64(dma);
1219 while (unlikely(size > IGC_MAX_DATA_PER_TXD)) {
1220 tx_desc->read.cmd_type_len =
1221 cpu_to_le32(cmd_type ^ IGC_MAX_DATA_PER_TXD);
1225 if (i == tx_ring->count) {
1226 tx_desc = IGC_TX_DESC(tx_ring, 0);
1229 tx_desc->read.olinfo_status = 0;
1231 dma += IGC_MAX_DATA_PER_TXD;
1232 size -= IGC_MAX_DATA_PER_TXD;
1234 tx_desc->read.buffer_addr = cpu_to_le64(dma);
1237 if (likely(!data_len))
1240 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type ^ size);
1244 if (i == tx_ring->count) {
1245 tx_desc = IGC_TX_DESC(tx_ring, 0);
1248 tx_desc->read.olinfo_status = 0;
1250 size = skb_frag_size(frag);
1253 dma = skb_frag_dma_map(tx_ring->dev, frag, 0,
1254 size, DMA_TO_DEVICE);
1256 tx_buffer = &tx_ring->tx_buffer_info[i];
1259 /* write last descriptor with RS and EOP bits */
1260 cmd_type |= size | IGC_TXD_DCMD;
1261 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type);
1263 netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount);
1265 /* set the timestamp */
1266 first->time_stamp = jiffies;
1268 skb_tx_timestamp(skb);
1270 /* Force memory writes to complete before letting h/w know there
1271 * are new descriptors to fetch. (Only applicable for weak-ordered
1272 * memory model archs, such as IA-64).
1274 * We also need this memory barrier to make certain all of the
1275 * status bits have been updated before next_to_watch is written.
1279 /* set next_to_watch value indicating a packet is present */
1280 first->next_to_watch = tx_desc;
1283 if (i == tx_ring->count)
1286 tx_ring->next_to_use = i;
1288 /* Make sure there is space in the ring for the next send. */
1289 igc_maybe_stop_tx(tx_ring, DESC_NEEDED);
1291 if (netif_xmit_stopped(txring_txq(tx_ring)) || !netdev_xmit_more()) {
1292 writel(i, tx_ring->tail);
1297 netdev_err(tx_ring->netdev, "TX DMA map failed\n");
1298 tx_buffer = &tx_ring->tx_buffer_info[i];
1300 /* clear dma mappings for failed tx_buffer_info map */
1301 while (tx_buffer != first) {
1302 if (dma_unmap_len(tx_buffer, len))
1303 igc_unmap_tx_buffer(tx_ring->dev, tx_buffer);
1306 i += tx_ring->count;
1307 tx_buffer = &tx_ring->tx_buffer_info[i];
1310 if (dma_unmap_len(tx_buffer, len))
1311 igc_unmap_tx_buffer(tx_ring->dev, tx_buffer);
1313 dev_kfree_skb_any(tx_buffer->skb);
1314 tx_buffer->skb = NULL;
1316 tx_ring->next_to_use = i;
1321 static int igc_tso(struct igc_ring *tx_ring,
1322 struct igc_tx_buffer *first,
1325 u32 vlan_macip_lens, type_tucmd, mss_l4len_idx;
1326 struct sk_buff *skb = first->skb;
1337 u32 paylen, l4_offset;
1340 if (skb->ip_summed != CHECKSUM_PARTIAL)
1343 if (!skb_is_gso(skb))
1346 err = skb_cow_head(skb, 0);
1350 ip.hdr = skb_network_header(skb);
1351 l4.hdr = skb_checksum_start(skb);
1353 /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
1354 type_tucmd = IGC_ADVTXD_TUCMD_L4T_TCP;
1356 /* initialize outer IP header fields */
1357 if (ip.v4->version == 4) {
1358 unsigned char *csum_start = skb_checksum_start(skb);
1359 unsigned char *trans_start = ip.hdr + (ip.v4->ihl * 4);
1361 /* IP header will have to cancel out any data that
1362 * is not a part of the outer IP header
1364 ip.v4->check = csum_fold(csum_partial(trans_start,
1365 csum_start - trans_start,
1367 type_tucmd |= IGC_ADVTXD_TUCMD_IPV4;
1370 first->tx_flags |= IGC_TX_FLAGS_TSO |
1374 ip.v6->payload_len = 0;
1375 first->tx_flags |= IGC_TX_FLAGS_TSO |
1379 /* determine offset of inner transport header */
1380 l4_offset = l4.hdr - skb->data;
1382 /* remove payload length from inner checksum */
1383 paylen = skb->len - l4_offset;
1384 if (type_tucmd & IGC_ADVTXD_TUCMD_L4T_TCP) {
1385 /* compute length of segmentation header */
1386 *hdr_len = (l4.tcp->doff * 4) + l4_offset;
1387 csum_replace_by_diff(&l4.tcp->check,
1388 (__force __wsum)htonl(paylen));
1390 /* compute length of segmentation header */
1391 *hdr_len = sizeof(*l4.udp) + l4_offset;
1392 csum_replace_by_diff(&l4.udp->check,
1393 (__force __wsum)htonl(paylen));
1396 /* update gso size and bytecount with header size */
1397 first->gso_segs = skb_shinfo(skb)->gso_segs;
1398 first->bytecount += (first->gso_segs - 1) * *hdr_len;
1401 mss_l4len_idx = (*hdr_len - l4_offset) << IGC_ADVTXD_L4LEN_SHIFT;
1402 mss_l4len_idx |= skb_shinfo(skb)->gso_size << IGC_ADVTXD_MSS_SHIFT;
1404 /* VLAN MACLEN IPLEN */
1405 vlan_macip_lens = l4.hdr - ip.hdr;
1406 vlan_macip_lens |= (ip.hdr - skb->data) << IGC_ADVTXD_MACLEN_SHIFT;
1407 vlan_macip_lens |= first->tx_flags & IGC_TX_FLAGS_VLAN_MASK;
1409 igc_tx_ctxtdesc(tx_ring, first, vlan_macip_lens,
1410 type_tucmd, mss_l4len_idx);
1415 static netdev_tx_t igc_xmit_frame_ring(struct sk_buff *skb,
1416 struct igc_ring *tx_ring)
1418 u16 count = TXD_USE_COUNT(skb_headlen(skb));
1419 __be16 protocol = vlan_get_protocol(skb);
1420 struct igc_tx_buffer *first;
1426 /* need: 1 descriptor per page * PAGE_SIZE/IGC_MAX_DATA_PER_TXD,
1427 * + 1 desc for skb_headlen/IGC_MAX_DATA_PER_TXD,
1428 * + 2 desc gap to keep tail from touching head,
1429 * + 1 desc for context descriptor,
1430 * otherwise try next time
1432 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
1433 count += TXD_USE_COUNT(skb_frag_size(
1434 &skb_shinfo(skb)->frags[f]));
1436 if (igc_maybe_stop_tx(tx_ring, count + 3)) {
1437 /* this is a hard error */
1438 return NETDEV_TX_BUSY;
1441 /* record the location of the first descriptor for this packet */
1442 first = &tx_ring->tx_buffer_info[tx_ring->next_to_use];
1443 first->type = IGC_TX_BUFFER_TYPE_SKB;
1445 first->bytecount = skb->len;
1446 first->gso_segs = 1;
1448 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
1449 struct igc_adapter *adapter = netdev_priv(tx_ring->netdev);
1451 /* FIXME: add support for retrieving timestamps from
1452 * the other timer registers before skipping the
1453 * timestamping request.
1455 if (adapter->tstamp_config.tx_type == HWTSTAMP_TX_ON &&
1456 !test_and_set_bit_lock(__IGC_PTP_TX_IN_PROGRESS,
1458 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1459 tx_flags |= IGC_TX_FLAGS_TSTAMP;
1461 adapter->ptp_tx_skb = skb_get(skb);
1462 adapter->ptp_tx_start = jiffies;
1464 adapter->tx_hwtstamp_skipped++;
1468 if (skb_vlan_tag_present(skb)) {
1469 tx_flags |= IGC_TX_FLAGS_VLAN;
1470 tx_flags |= (skb_vlan_tag_get(skb) << IGC_TX_FLAGS_VLAN_SHIFT);
1473 /* record initial flags and protocol */
1474 first->tx_flags = tx_flags;
1475 first->protocol = protocol;
1477 tso = igc_tso(tx_ring, first, &hdr_len);
1481 igc_tx_csum(tx_ring, first);
1483 igc_tx_map(tx_ring, first, hdr_len);
1485 return NETDEV_TX_OK;
1488 dev_kfree_skb_any(first->skb);
1491 return NETDEV_TX_OK;
1494 static inline struct igc_ring *igc_tx_queue_mapping(struct igc_adapter *adapter,
1495 struct sk_buff *skb)
1497 unsigned int r_idx = skb->queue_mapping;
1499 if (r_idx >= adapter->num_tx_queues)
1500 r_idx = r_idx % adapter->num_tx_queues;
1502 return adapter->tx_ring[r_idx];
1505 static netdev_tx_t igc_xmit_frame(struct sk_buff *skb,
1506 struct net_device *netdev)
1508 struct igc_adapter *adapter = netdev_priv(netdev);
1510 /* The minimum packet size with TCTL.PSP set is 17 so pad the skb
1511 * in order to meet this minimum size requirement.
1513 if (skb->len < 17) {
1514 if (skb_padto(skb, 17))
1515 return NETDEV_TX_OK;
1519 return igc_xmit_frame_ring(skb, igc_tx_queue_mapping(adapter, skb));
1522 static void igc_rx_checksum(struct igc_ring *ring,
1523 union igc_adv_rx_desc *rx_desc,
1524 struct sk_buff *skb)
1526 skb_checksum_none_assert(skb);
1528 /* Ignore Checksum bit is set */
1529 if (igc_test_staterr(rx_desc, IGC_RXD_STAT_IXSM))
1532 /* Rx checksum disabled via ethtool */
1533 if (!(ring->netdev->features & NETIF_F_RXCSUM))
1536 /* TCP/UDP checksum error bit is set */
1537 if (igc_test_staterr(rx_desc,
1538 IGC_RXDEXT_STATERR_L4E |
1539 IGC_RXDEXT_STATERR_IPE)) {
1540 /* work around errata with sctp packets where the TCPE aka
1541 * L4E bit is set incorrectly on 64 byte (60 byte w/o crc)
1542 * packets (aka let the stack check the crc32c)
1544 if (!(skb->len == 60 &&
1545 test_bit(IGC_RING_FLAG_RX_SCTP_CSUM, &ring->flags))) {
1546 u64_stats_update_begin(&ring->rx_syncp);
1547 ring->rx_stats.csum_err++;
1548 u64_stats_update_end(&ring->rx_syncp);
1550 /* let the stack verify checksum errors */
1553 /* It must be a TCP or UDP packet with a valid checksum */
1554 if (igc_test_staterr(rx_desc, IGC_RXD_STAT_TCPCS |
1555 IGC_RXD_STAT_UDPCS))
1556 skb->ip_summed = CHECKSUM_UNNECESSARY;
1558 netdev_dbg(ring->netdev, "cksum success: bits %08X\n",
1559 le32_to_cpu(rx_desc->wb.upper.status_error));
1562 static inline void igc_rx_hash(struct igc_ring *ring,
1563 union igc_adv_rx_desc *rx_desc,
1564 struct sk_buff *skb)
1566 if (ring->netdev->features & NETIF_F_RXHASH)
1568 le32_to_cpu(rx_desc->wb.lower.hi_dword.rss),
1572 static void igc_rx_vlan(struct igc_ring *rx_ring,
1573 union igc_adv_rx_desc *rx_desc,
1574 struct sk_buff *skb)
1576 struct net_device *dev = rx_ring->netdev;
1579 if ((dev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
1580 igc_test_staterr(rx_desc, IGC_RXD_STAT_VP)) {
1581 if (igc_test_staterr(rx_desc, IGC_RXDEXT_STATERR_LB) &&
1582 test_bit(IGC_RING_FLAG_RX_LB_VLAN_BSWAP, &rx_ring->flags))
1583 vid = be16_to_cpu((__force __be16)rx_desc->wb.upper.vlan);
1585 vid = le16_to_cpu(rx_desc->wb.upper.vlan);
1587 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
1592 * igc_process_skb_fields - Populate skb header fields from Rx descriptor
1593 * @rx_ring: rx descriptor ring packet is being transacted on
1594 * @rx_desc: pointer to the EOP Rx descriptor
1595 * @skb: pointer to current skb being populated
1597 * This function checks the ring, descriptor, and packet information in order
1598 * to populate the hash, checksum, VLAN, protocol, and other fields within the
1601 static void igc_process_skb_fields(struct igc_ring *rx_ring,
1602 union igc_adv_rx_desc *rx_desc,
1603 struct sk_buff *skb)
1605 igc_rx_hash(rx_ring, rx_desc, skb);
1607 igc_rx_checksum(rx_ring, rx_desc, skb);
1609 igc_rx_vlan(rx_ring, rx_desc, skb);
1611 skb_record_rx_queue(skb, rx_ring->queue_index);
1613 skb->protocol = eth_type_trans(skb, rx_ring->netdev);
1616 static void igc_vlan_mode(struct net_device *netdev, netdev_features_t features)
1618 bool enable = !!(features & NETIF_F_HW_VLAN_CTAG_RX);
1619 struct igc_adapter *adapter = netdev_priv(netdev);
1620 struct igc_hw *hw = &adapter->hw;
1623 ctrl = rd32(IGC_CTRL);
1626 /* enable VLAN tag insert/strip */
1627 ctrl |= IGC_CTRL_VME;
1629 /* disable VLAN tag insert/strip */
1630 ctrl &= ~IGC_CTRL_VME;
1632 wr32(IGC_CTRL, ctrl);
1635 static void igc_restore_vlan(struct igc_adapter *adapter)
1637 igc_vlan_mode(adapter->netdev, adapter->netdev->features);
1640 static struct igc_rx_buffer *igc_get_rx_buffer(struct igc_ring *rx_ring,
1641 const unsigned int size,
1642 int *rx_buffer_pgcnt)
1644 struct igc_rx_buffer *rx_buffer;
1646 rx_buffer = &rx_ring->rx_buffer_info[rx_ring->next_to_clean];
1648 #if (PAGE_SIZE < 8192)
1649 page_count(rx_buffer->page);
1653 prefetchw(rx_buffer->page);
1655 /* we are reusing so sync this buffer for CPU use */
1656 dma_sync_single_range_for_cpu(rx_ring->dev,
1658 rx_buffer->page_offset,
1662 rx_buffer->pagecnt_bias--;
1667 static void igc_rx_buffer_flip(struct igc_rx_buffer *buffer,
1668 unsigned int truesize)
1670 #if (PAGE_SIZE < 8192)
1671 buffer->page_offset ^= truesize;
1673 buffer->page_offset += truesize;
1677 static unsigned int igc_get_rx_frame_truesize(struct igc_ring *ring,
1680 unsigned int truesize;
1682 #if (PAGE_SIZE < 8192)
1683 truesize = igc_rx_pg_size(ring) / 2;
1685 truesize = ring_uses_build_skb(ring) ?
1686 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
1687 SKB_DATA_ALIGN(IGC_SKB_PAD + size) :
1688 SKB_DATA_ALIGN(size);
1694 * igc_add_rx_frag - Add contents of Rx buffer to sk_buff
1695 * @rx_ring: rx descriptor ring to transact packets on
1696 * @rx_buffer: buffer containing page to add
1697 * @skb: sk_buff to place the data into
1698 * @size: size of buffer to be added
1700 * This function will add the data contained in rx_buffer->page to the skb.
1702 static void igc_add_rx_frag(struct igc_ring *rx_ring,
1703 struct igc_rx_buffer *rx_buffer,
1704 struct sk_buff *skb,
1707 unsigned int truesize;
1709 #if (PAGE_SIZE < 8192)
1710 truesize = igc_rx_pg_size(rx_ring) / 2;
1712 truesize = ring_uses_build_skb(rx_ring) ?
1713 SKB_DATA_ALIGN(IGC_SKB_PAD + size) :
1714 SKB_DATA_ALIGN(size);
1716 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_buffer->page,
1717 rx_buffer->page_offset, size, truesize);
1719 igc_rx_buffer_flip(rx_buffer, truesize);
1722 static struct sk_buff *igc_build_skb(struct igc_ring *rx_ring,
1723 struct igc_rx_buffer *rx_buffer,
1724 struct xdp_buff *xdp)
1726 unsigned int size = xdp->data_end - xdp->data;
1727 unsigned int truesize = igc_get_rx_frame_truesize(rx_ring, size);
1728 unsigned int metasize = xdp->data - xdp->data_meta;
1729 struct sk_buff *skb;
1731 /* prefetch first cache line of first page */
1732 net_prefetch(xdp->data_meta);
1734 /* build an skb around the page buffer */
1735 skb = napi_build_skb(xdp->data_hard_start, truesize);
1739 /* update pointers within the skb to store the data */
1740 skb_reserve(skb, xdp->data - xdp->data_hard_start);
1741 __skb_put(skb, size);
1743 skb_metadata_set(skb, metasize);
1745 igc_rx_buffer_flip(rx_buffer, truesize);
1749 static struct sk_buff *igc_construct_skb(struct igc_ring *rx_ring,
1750 struct igc_rx_buffer *rx_buffer,
1751 struct xdp_buff *xdp,
1754 unsigned int metasize = xdp->data - xdp->data_meta;
1755 unsigned int size = xdp->data_end - xdp->data;
1756 unsigned int truesize = igc_get_rx_frame_truesize(rx_ring, size);
1757 void *va = xdp->data;
1758 unsigned int headlen;
1759 struct sk_buff *skb;
1761 /* prefetch first cache line of first page */
1762 net_prefetch(xdp->data_meta);
1764 /* allocate a skb to store the frags */
1765 skb = napi_alloc_skb(&rx_ring->q_vector->napi,
1766 IGC_RX_HDR_LEN + metasize);
1771 skb_hwtstamps(skb)->hwtstamp = timestamp;
1773 /* Determine available headroom for copy */
1775 if (headlen > IGC_RX_HDR_LEN)
1776 headlen = eth_get_headlen(skb->dev, va, IGC_RX_HDR_LEN);
1778 /* align pull length to size of long to optimize memcpy performance */
1779 memcpy(__skb_put(skb, headlen + metasize), xdp->data_meta,
1780 ALIGN(headlen + metasize, sizeof(long)));
1783 skb_metadata_set(skb, metasize);
1784 __skb_pull(skb, metasize);
1787 /* update all of the pointers */
1790 skb_add_rx_frag(skb, 0, rx_buffer->page,
1791 (va + headlen) - page_address(rx_buffer->page),
1793 igc_rx_buffer_flip(rx_buffer, truesize);
1795 rx_buffer->pagecnt_bias++;
1802 * igc_reuse_rx_page - page flip buffer and store it back on the ring
1803 * @rx_ring: rx descriptor ring to store buffers on
1804 * @old_buff: donor buffer to have page reused
1806 * Synchronizes page for reuse by the adapter
1808 static void igc_reuse_rx_page(struct igc_ring *rx_ring,
1809 struct igc_rx_buffer *old_buff)
1811 u16 nta = rx_ring->next_to_alloc;
1812 struct igc_rx_buffer *new_buff;
1814 new_buff = &rx_ring->rx_buffer_info[nta];
1816 /* update, and store next to alloc */
1818 rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
1820 /* Transfer page from old buffer to new buffer.
1821 * Move each member individually to avoid possible store
1822 * forwarding stalls.
1824 new_buff->dma = old_buff->dma;
1825 new_buff->page = old_buff->page;
1826 new_buff->page_offset = old_buff->page_offset;
1827 new_buff->pagecnt_bias = old_buff->pagecnt_bias;
1830 static bool igc_can_reuse_rx_page(struct igc_rx_buffer *rx_buffer,
1831 int rx_buffer_pgcnt)
1833 unsigned int pagecnt_bias = rx_buffer->pagecnt_bias;
1834 struct page *page = rx_buffer->page;
1836 /* avoid re-using remote and pfmemalloc pages */
1837 if (!dev_page_is_reusable(page))
1840 #if (PAGE_SIZE < 8192)
1841 /* if we are only owner of page we can reuse it */
1842 if (unlikely((rx_buffer_pgcnt - pagecnt_bias) > 1))
1845 #define IGC_LAST_OFFSET \
1846 (SKB_WITH_OVERHEAD(PAGE_SIZE) - IGC_RXBUFFER_2048)
1848 if (rx_buffer->page_offset > IGC_LAST_OFFSET)
1852 /* If we have drained the page fragment pool we need to update
1853 * the pagecnt_bias and page count so that we fully restock the
1854 * number of references the driver holds.
1856 if (unlikely(pagecnt_bias == 1)) {
1857 page_ref_add(page, USHRT_MAX - 1);
1858 rx_buffer->pagecnt_bias = USHRT_MAX;
1865 * igc_is_non_eop - process handling of non-EOP buffers
1866 * @rx_ring: Rx ring being processed
1867 * @rx_desc: Rx descriptor for current buffer
1869 * This function updates next to clean. If the buffer is an EOP buffer
1870 * this function exits returning false, otherwise it will place the
1871 * sk_buff in the next buffer to be chained and return true indicating
1872 * that this is in fact a non-EOP buffer.
1874 static bool igc_is_non_eop(struct igc_ring *rx_ring,
1875 union igc_adv_rx_desc *rx_desc)
1877 u32 ntc = rx_ring->next_to_clean + 1;
1879 /* fetch, update, and store next to clean */
1880 ntc = (ntc < rx_ring->count) ? ntc : 0;
1881 rx_ring->next_to_clean = ntc;
1883 prefetch(IGC_RX_DESC(rx_ring, ntc));
1885 if (likely(igc_test_staterr(rx_desc, IGC_RXD_STAT_EOP)))
1892 * igc_cleanup_headers - Correct corrupted or empty headers
1893 * @rx_ring: rx descriptor ring packet is being transacted on
1894 * @rx_desc: pointer to the EOP Rx descriptor
1895 * @skb: pointer to current skb being fixed
1897 * Address the case where we are pulling data in on pages only
1898 * and as such no data is present in the skb header.
1900 * In addition if skb is not at least 60 bytes we need to pad it so that
1901 * it is large enough to qualify as a valid Ethernet frame.
1903 * Returns true if an error was encountered and skb was freed.
1905 static bool igc_cleanup_headers(struct igc_ring *rx_ring,
1906 union igc_adv_rx_desc *rx_desc,
1907 struct sk_buff *skb)
1909 /* XDP packets use error pointer so abort at this point */
1913 if (unlikely(igc_test_staterr(rx_desc, IGC_RXDEXT_STATERR_RXE))) {
1914 struct net_device *netdev = rx_ring->netdev;
1916 if (!(netdev->features & NETIF_F_RXALL)) {
1917 dev_kfree_skb_any(skb);
1922 /* if eth_skb_pad returns an error the skb was freed */
1923 if (eth_skb_pad(skb))
1929 static void igc_put_rx_buffer(struct igc_ring *rx_ring,
1930 struct igc_rx_buffer *rx_buffer,
1931 int rx_buffer_pgcnt)
1933 if (igc_can_reuse_rx_page(rx_buffer, rx_buffer_pgcnt)) {
1934 /* hand second half of page back to the ring */
1935 igc_reuse_rx_page(rx_ring, rx_buffer);
1937 /* We are not reusing the buffer so unmap it and free
1938 * any references we are holding to it
1940 dma_unmap_page_attrs(rx_ring->dev, rx_buffer->dma,
1941 igc_rx_pg_size(rx_ring), DMA_FROM_DEVICE,
1943 __page_frag_cache_drain(rx_buffer->page,
1944 rx_buffer->pagecnt_bias);
1947 /* clear contents of rx_buffer */
1948 rx_buffer->page = NULL;
1951 static inline unsigned int igc_rx_offset(struct igc_ring *rx_ring)
1953 struct igc_adapter *adapter = rx_ring->q_vector->adapter;
1955 if (ring_uses_build_skb(rx_ring))
1957 if (igc_xdp_is_enabled(adapter))
1958 return XDP_PACKET_HEADROOM;
1963 static bool igc_alloc_mapped_page(struct igc_ring *rx_ring,
1964 struct igc_rx_buffer *bi)
1966 struct page *page = bi->page;
1969 /* since we are recycling buffers we should seldom need to alloc */
1973 /* alloc new page for storage */
1974 page = dev_alloc_pages(igc_rx_pg_order(rx_ring));
1975 if (unlikely(!page)) {
1976 rx_ring->rx_stats.alloc_failed++;
1980 /* map page for use */
1981 dma = dma_map_page_attrs(rx_ring->dev, page, 0,
1982 igc_rx_pg_size(rx_ring),
1986 /* if mapping failed free memory back to system since
1987 * there isn't much point in holding memory we can't use
1989 if (dma_mapping_error(rx_ring->dev, dma)) {
1992 rx_ring->rx_stats.alloc_failed++;
1998 bi->page_offset = igc_rx_offset(rx_ring);
1999 page_ref_add(page, USHRT_MAX - 1);
2000 bi->pagecnt_bias = USHRT_MAX;
2006 * igc_alloc_rx_buffers - Replace used receive buffers; packet split
2007 * @rx_ring: rx descriptor ring
2008 * @cleaned_count: number of buffers to clean
2010 static void igc_alloc_rx_buffers(struct igc_ring *rx_ring, u16 cleaned_count)
2012 union igc_adv_rx_desc *rx_desc;
2013 u16 i = rx_ring->next_to_use;
2014 struct igc_rx_buffer *bi;
2021 rx_desc = IGC_RX_DESC(rx_ring, i);
2022 bi = &rx_ring->rx_buffer_info[i];
2023 i -= rx_ring->count;
2025 bufsz = igc_rx_bufsz(rx_ring);
2028 if (!igc_alloc_mapped_page(rx_ring, bi))
2031 /* sync the buffer for use by the device */
2032 dma_sync_single_range_for_device(rx_ring->dev, bi->dma,
2033 bi->page_offset, bufsz,
2036 /* Refresh the desc even if buffer_addrs didn't change
2037 * because each write-back erases this info.
2039 rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
2045 rx_desc = IGC_RX_DESC(rx_ring, 0);
2046 bi = rx_ring->rx_buffer_info;
2047 i -= rx_ring->count;
2050 /* clear the length for the next_to_use descriptor */
2051 rx_desc->wb.upper.length = 0;
2054 } while (cleaned_count);
2056 i += rx_ring->count;
2058 if (rx_ring->next_to_use != i) {
2059 /* record the next descriptor to use */
2060 rx_ring->next_to_use = i;
2062 /* update next to alloc since we have filled the ring */
2063 rx_ring->next_to_alloc = i;
2065 /* Force memory writes to complete before letting h/w
2066 * know there are new descriptors to fetch. (Only
2067 * applicable for weak-ordered memory model archs,
2071 writel(i, rx_ring->tail);
2075 static bool igc_alloc_rx_buffers_zc(struct igc_ring *ring, u16 count)
2077 union igc_adv_rx_desc *desc;
2078 u16 i = ring->next_to_use;
2079 struct igc_rx_buffer *bi;
2086 desc = IGC_RX_DESC(ring, i);
2087 bi = &ring->rx_buffer_info[i];
2091 bi->xdp = xsk_buff_alloc(ring->xsk_pool);
2097 dma = xsk_buff_xdp_get_dma(bi->xdp);
2098 desc->read.pkt_addr = cpu_to_le64(dma);
2104 desc = IGC_RX_DESC(ring, 0);
2105 bi = ring->rx_buffer_info;
2109 /* Clear the length for the next_to_use descriptor. */
2110 desc->wb.upper.length = 0;
2117 if (ring->next_to_use != i) {
2118 ring->next_to_use = i;
2120 /* Force memory writes to complete before letting h/w
2121 * know there are new descriptors to fetch. (Only
2122 * applicable for weak-ordered memory model archs,
2126 writel(i, ring->tail);
2132 /* This function requires __netif_tx_lock is held by the caller. */
2133 static int igc_xdp_init_tx_descriptor(struct igc_ring *ring,
2134 struct xdp_frame *xdpf)
2136 struct skb_shared_info *sinfo = xdp_get_shared_info_from_frame(xdpf);
2137 u8 nr_frags = unlikely(xdp_frame_has_frags(xdpf)) ? sinfo->nr_frags : 0;
2138 u16 count, index = ring->next_to_use;
2139 struct igc_tx_buffer *head = &ring->tx_buffer_info[index];
2140 struct igc_tx_buffer *buffer = head;
2141 union igc_adv_tx_desc *desc = IGC_TX_DESC(ring, index);
2142 u32 olinfo_status, len = xdpf->len, cmd_type;
2143 void *data = xdpf->data;
2146 count = TXD_USE_COUNT(len);
2147 for (i = 0; i < nr_frags; i++)
2148 count += TXD_USE_COUNT(skb_frag_size(&sinfo->frags[i]));
2150 if (igc_maybe_stop_tx(ring, count + 3)) {
2151 /* this is a hard error */
2156 head->bytecount = xdp_get_frame_len(xdpf);
2157 head->type = IGC_TX_BUFFER_TYPE_XDP;
2161 olinfo_status = head->bytecount << IGC_ADVTXD_PAYLEN_SHIFT;
2162 desc->read.olinfo_status = cpu_to_le32(olinfo_status);
2167 dma = dma_map_single(ring->dev, data, len, DMA_TO_DEVICE);
2168 if (dma_mapping_error(ring->dev, dma)) {
2169 netdev_err_once(ring->netdev,
2170 "Failed to map DMA for TX\n");
2174 dma_unmap_len_set(buffer, len, len);
2175 dma_unmap_addr_set(buffer, dma, dma);
2177 cmd_type = IGC_ADVTXD_DTYP_DATA | IGC_ADVTXD_DCMD_DEXT |
2178 IGC_ADVTXD_DCMD_IFCS | len;
2180 desc->read.cmd_type_len = cpu_to_le32(cmd_type);
2181 desc->read.buffer_addr = cpu_to_le64(dma);
2183 buffer->protocol = 0;
2185 if (++index == ring->count)
2191 buffer = &ring->tx_buffer_info[index];
2192 desc = IGC_TX_DESC(ring, index);
2193 desc->read.olinfo_status = 0;
2195 data = skb_frag_address(&sinfo->frags[i]);
2196 len = skb_frag_size(&sinfo->frags[i]);
2199 desc->read.cmd_type_len |= cpu_to_le32(IGC_TXD_DCMD);
2201 netdev_tx_sent_queue(txring_txq(ring), head->bytecount);
2202 /* set the timestamp */
2203 head->time_stamp = jiffies;
2204 /* set next_to_watch value indicating a packet is present */
2205 head->next_to_watch = desc;
2206 ring->next_to_use = index;
2212 buffer = &ring->tx_buffer_info[index];
2213 if (dma_unmap_len(buffer, len))
2214 dma_unmap_page(ring->dev,
2215 dma_unmap_addr(buffer, dma),
2216 dma_unmap_len(buffer, len),
2218 dma_unmap_len_set(buffer, len, 0);
2223 index += ring->count;
2230 static struct igc_ring *igc_xdp_get_tx_ring(struct igc_adapter *adapter,
2235 if (unlikely(index < 0))
2238 while (index >= adapter->num_tx_queues)
2239 index -= adapter->num_tx_queues;
2241 return adapter->tx_ring[index];
2244 static int igc_xdp_xmit_back(struct igc_adapter *adapter, struct xdp_buff *xdp)
2246 struct xdp_frame *xdpf = xdp_convert_buff_to_frame(xdp);
2247 int cpu = smp_processor_id();
2248 struct netdev_queue *nq;
2249 struct igc_ring *ring;
2252 if (unlikely(!xdpf))
2255 ring = igc_xdp_get_tx_ring(adapter, cpu);
2256 nq = txring_txq(ring);
2258 __netif_tx_lock(nq, cpu);
2259 res = igc_xdp_init_tx_descriptor(ring, xdpf);
2260 __netif_tx_unlock(nq);
2264 /* This function assumes rcu_read_lock() is held by the caller. */
2265 static int __igc_xdp_run_prog(struct igc_adapter *adapter,
2266 struct bpf_prog *prog,
2267 struct xdp_buff *xdp)
2269 u32 act = bpf_prog_run_xdp(prog, xdp);
2273 return IGC_XDP_PASS;
2275 if (igc_xdp_xmit_back(adapter, xdp) < 0)
2279 if (xdp_do_redirect(adapter->netdev, xdp, prog) < 0)
2281 return IGC_XDP_REDIRECT;
2284 bpf_warn_invalid_xdp_action(adapter->netdev, prog, act);
2288 trace_xdp_exception(adapter->netdev, prog, act);
2291 return IGC_XDP_CONSUMED;
2295 static struct sk_buff *igc_xdp_run_prog(struct igc_adapter *adapter,
2296 struct xdp_buff *xdp)
2298 struct bpf_prog *prog;
2301 prog = READ_ONCE(adapter->xdp_prog);
2307 res = __igc_xdp_run_prog(adapter, prog, xdp);
2310 return ERR_PTR(-res);
2313 /* This function assumes __netif_tx_lock is held by the caller. */
2314 static void igc_flush_tx_descriptors(struct igc_ring *ring)
2316 /* Once tail pointer is updated, hardware can fetch the descriptors
2317 * any time so we issue a write membar here to ensure all memory
2318 * writes are complete before the tail pointer is updated.
2321 writel(ring->next_to_use, ring->tail);
2324 static void igc_finalize_xdp(struct igc_adapter *adapter, int status)
2326 int cpu = smp_processor_id();
2327 struct netdev_queue *nq;
2328 struct igc_ring *ring;
2330 if (status & IGC_XDP_TX) {
2331 ring = igc_xdp_get_tx_ring(adapter, cpu);
2332 nq = txring_txq(ring);
2334 __netif_tx_lock(nq, cpu);
2335 igc_flush_tx_descriptors(ring);
2336 __netif_tx_unlock(nq);
2339 if (status & IGC_XDP_REDIRECT)
2343 static void igc_update_rx_stats(struct igc_q_vector *q_vector,
2344 unsigned int packets, unsigned int bytes)
2346 struct igc_ring *ring = q_vector->rx.ring;
2348 u64_stats_update_begin(&ring->rx_syncp);
2349 ring->rx_stats.packets += packets;
2350 ring->rx_stats.bytes += bytes;
2351 u64_stats_update_end(&ring->rx_syncp);
2353 q_vector->rx.total_packets += packets;
2354 q_vector->rx.total_bytes += bytes;
2357 static int igc_clean_rx_irq(struct igc_q_vector *q_vector, const int budget)
2359 unsigned int total_bytes = 0, total_packets = 0;
2360 struct igc_adapter *adapter = q_vector->adapter;
2361 struct igc_ring *rx_ring = q_vector->rx.ring;
2362 struct sk_buff *skb = rx_ring->skb;
2363 u16 cleaned_count = igc_desc_unused(rx_ring);
2364 int xdp_status = 0, rx_buffer_pgcnt;
2366 while (likely(total_packets < budget)) {
2367 union igc_adv_rx_desc *rx_desc;
2368 struct igc_rx_buffer *rx_buffer;
2369 unsigned int size, truesize;
2370 ktime_t timestamp = 0;
2371 struct xdp_buff xdp;
2375 /* return some buffers to hardware, one at a time is too slow */
2376 if (cleaned_count >= IGC_RX_BUFFER_WRITE) {
2377 igc_alloc_rx_buffers(rx_ring, cleaned_count);
2381 rx_desc = IGC_RX_DESC(rx_ring, rx_ring->next_to_clean);
2382 size = le16_to_cpu(rx_desc->wb.upper.length);
2386 /* This memory barrier is needed to keep us from reading
2387 * any other fields out of the rx_desc until we know the
2388 * descriptor has been written back
2392 rx_buffer = igc_get_rx_buffer(rx_ring, size, &rx_buffer_pgcnt);
2393 truesize = igc_get_rx_frame_truesize(rx_ring, size);
2395 pktbuf = page_address(rx_buffer->page) + rx_buffer->page_offset;
2397 if (igc_test_staterr(rx_desc, IGC_RXDADV_STAT_TSIP)) {
2398 timestamp = igc_ptp_rx_pktstamp(q_vector->adapter,
2400 pkt_offset = IGC_TS_HDR_LEN;
2401 size -= IGC_TS_HDR_LEN;
2405 xdp_init_buff(&xdp, truesize, &rx_ring->xdp_rxq);
2406 xdp_prepare_buff(&xdp, pktbuf - igc_rx_offset(rx_ring),
2407 igc_rx_offset(rx_ring) + pkt_offset,
2409 xdp_buff_clear_frags_flag(&xdp);
2411 skb = igc_xdp_run_prog(adapter, &xdp);
2415 unsigned int xdp_res = -PTR_ERR(skb);
2418 case IGC_XDP_CONSUMED:
2419 rx_buffer->pagecnt_bias++;
2422 case IGC_XDP_REDIRECT:
2423 igc_rx_buffer_flip(rx_buffer, truesize);
2424 xdp_status |= xdp_res;
2429 total_bytes += size;
2431 igc_add_rx_frag(rx_ring, rx_buffer, skb, size);
2432 else if (ring_uses_build_skb(rx_ring))
2433 skb = igc_build_skb(rx_ring, rx_buffer, &xdp);
2435 skb = igc_construct_skb(rx_ring, rx_buffer, &xdp,
2438 /* exit if we failed to retrieve a buffer */
2440 rx_ring->rx_stats.alloc_failed++;
2441 rx_buffer->pagecnt_bias++;
2445 igc_put_rx_buffer(rx_ring, rx_buffer, rx_buffer_pgcnt);
2448 /* fetch next buffer in frame if non-eop */
2449 if (igc_is_non_eop(rx_ring, rx_desc))
2452 /* verify the packet layout is correct */
2453 if (igc_cleanup_headers(rx_ring, rx_desc, skb)) {
2458 /* probably a little skewed due to removing CRC */
2459 total_bytes += skb->len;
2461 /* populate checksum, VLAN, and protocol */
2462 igc_process_skb_fields(rx_ring, rx_desc, skb);
2464 napi_gro_receive(&q_vector->napi, skb);
2466 /* reset skb pointer */
2469 /* update budget accounting */
2474 igc_finalize_xdp(adapter, xdp_status);
2476 /* place incomplete frames back on ring for completion */
2479 igc_update_rx_stats(q_vector, total_packets, total_bytes);
2482 igc_alloc_rx_buffers(rx_ring, cleaned_count);
2484 return total_packets;
2487 static struct sk_buff *igc_construct_skb_zc(struct igc_ring *ring,
2488 struct xdp_buff *xdp)
2490 unsigned int totalsize = xdp->data_end - xdp->data_meta;
2491 unsigned int metasize = xdp->data - xdp->data_meta;
2492 struct sk_buff *skb;
2494 net_prefetch(xdp->data_meta);
2496 skb = __napi_alloc_skb(&ring->q_vector->napi, totalsize,
2497 GFP_ATOMIC | __GFP_NOWARN);
2501 memcpy(__skb_put(skb, totalsize), xdp->data_meta,
2502 ALIGN(totalsize, sizeof(long)));
2505 skb_metadata_set(skb, metasize);
2506 __skb_pull(skb, metasize);
2512 static void igc_dispatch_skb_zc(struct igc_q_vector *q_vector,
2513 union igc_adv_rx_desc *desc,
2514 struct xdp_buff *xdp,
2517 struct igc_ring *ring = q_vector->rx.ring;
2518 struct sk_buff *skb;
2520 skb = igc_construct_skb_zc(ring, xdp);
2522 ring->rx_stats.alloc_failed++;
2527 skb_hwtstamps(skb)->hwtstamp = timestamp;
2529 if (igc_cleanup_headers(ring, desc, skb))
2532 igc_process_skb_fields(ring, desc, skb);
2533 napi_gro_receive(&q_vector->napi, skb);
2536 static int igc_clean_rx_irq_zc(struct igc_q_vector *q_vector, const int budget)
2538 struct igc_adapter *adapter = q_vector->adapter;
2539 struct igc_ring *ring = q_vector->rx.ring;
2540 u16 cleaned_count = igc_desc_unused(ring);
2541 int total_bytes = 0, total_packets = 0;
2542 u16 ntc = ring->next_to_clean;
2543 struct bpf_prog *prog;
2544 bool failure = false;
2549 prog = READ_ONCE(adapter->xdp_prog);
2551 while (likely(total_packets < budget)) {
2552 union igc_adv_rx_desc *desc;
2553 struct igc_rx_buffer *bi;
2554 ktime_t timestamp = 0;
2558 desc = IGC_RX_DESC(ring, ntc);
2559 size = le16_to_cpu(desc->wb.upper.length);
2563 /* This memory barrier is needed to keep us from reading
2564 * any other fields out of the rx_desc until we know the
2565 * descriptor has been written back
2569 bi = &ring->rx_buffer_info[ntc];
2571 if (igc_test_staterr(desc, IGC_RXDADV_STAT_TSIP)) {
2572 timestamp = igc_ptp_rx_pktstamp(q_vector->adapter,
2575 bi->xdp->data += IGC_TS_HDR_LEN;
2577 /* HW timestamp has been copied into local variable. Metadata
2578 * length when XDP program is called should be 0.
2580 bi->xdp->data_meta += IGC_TS_HDR_LEN;
2581 size -= IGC_TS_HDR_LEN;
2584 bi->xdp->data_end = bi->xdp->data + size;
2585 xsk_buff_dma_sync_for_cpu(bi->xdp, ring->xsk_pool);
2587 res = __igc_xdp_run_prog(adapter, prog, bi->xdp);
2590 igc_dispatch_skb_zc(q_vector, desc, bi->xdp, timestamp);
2592 case IGC_XDP_CONSUMED:
2593 xsk_buff_free(bi->xdp);
2596 case IGC_XDP_REDIRECT:
2602 total_bytes += size;
2606 if (ntc == ring->count)
2610 ring->next_to_clean = ntc;
2613 if (cleaned_count >= IGC_RX_BUFFER_WRITE)
2614 failure = !igc_alloc_rx_buffers_zc(ring, cleaned_count);
2617 igc_finalize_xdp(adapter, xdp_status);
2619 igc_update_rx_stats(q_vector, total_packets, total_bytes);
2621 if (xsk_uses_need_wakeup(ring->xsk_pool)) {
2622 if (failure || ring->next_to_clean == ring->next_to_use)
2623 xsk_set_rx_need_wakeup(ring->xsk_pool);
2625 xsk_clear_rx_need_wakeup(ring->xsk_pool);
2626 return total_packets;
2629 return failure ? budget : total_packets;
2632 static void igc_update_tx_stats(struct igc_q_vector *q_vector,
2633 unsigned int packets, unsigned int bytes)
2635 struct igc_ring *ring = q_vector->tx.ring;
2637 u64_stats_update_begin(&ring->tx_syncp);
2638 ring->tx_stats.bytes += bytes;
2639 ring->tx_stats.packets += packets;
2640 u64_stats_update_end(&ring->tx_syncp);
2642 q_vector->tx.total_bytes += bytes;
2643 q_vector->tx.total_packets += packets;
2646 static void igc_xdp_xmit_zc(struct igc_ring *ring)
2648 struct xsk_buff_pool *pool = ring->xsk_pool;
2649 struct netdev_queue *nq = txring_txq(ring);
2650 union igc_adv_tx_desc *tx_desc = NULL;
2651 int cpu = smp_processor_id();
2652 u16 ntu = ring->next_to_use;
2653 struct xdp_desc xdp_desc;
2656 if (!netif_carrier_ok(ring->netdev))
2659 __netif_tx_lock(nq, cpu);
2661 budget = igc_desc_unused(ring);
2663 while (xsk_tx_peek_desc(pool, &xdp_desc) && budget--) {
2664 u32 cmd_type, olinfo_status;
2665 struct igc_tx_buffer *bi;
2668 cmd_type = IGC_ADVTXD_DTYP_DATA | IGC_ADVTXD_DCMD_DEXT |
2669 IGC_ADVTXD_DCMD_IFCS | IGC_TXD_DCMD |
2671 olinfo_status = xdp_desc.len << IGC_ADVTXD_PAYLEN_SHIFT;
2673 dma = xsk_buff_raw_get_dma(pool, xdp_desc.addr);
2674 xsk_buff_raw_dma_sync_for_device(pool, dma, xdp_desc.len);
2676 tx_desc = IGC_TX_DESC(ring, ntu);
2677 tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type);
2678 tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
2679 tx_desc->read.buffer_addr = cpu_to_le64(dma);
2681 bi = &ring->tx_buffer_info[ntu];
2682 bi->type = IGC_TX_BUFFER_TYPE_XSK;
2684 bi->bytecount = xdp_desc.len;
2686 bi->time_stamp = jiffies;
2687 bi->next_to_watch = tx_desc;
2689 netdev_tx_sent_queue(txring_txq(ring), xdp_desc.len);
2692 if (ntu == ring->count)
2696 ring->next_to_use = ntu;
2698 igc_flush_tx_descriptors(ring);
2699 xsk_tx_release(pool);
2702 __netif_tx_unlock(nq);
2706 * igc_clean_tx_irq - Reclaim resources after transmit completes
2707 * @q_vector: pointer to q_vector containing needed info
2708 * @napi_budget: Used to determine if we are in netpoll
2710 * returns true if ring is completely cleaned
2712 static bool igc_clean_tx_irq(struct igc_q_vector *q_vector, int napi_budget)
2714 struct igc_adapter *adapter = q_vector->adapter;
2715 unsigned int total_bytes = 0, total_packets = 0;
2716 unsigned int budget = q_vector->tx.work_limit;
2717 struct igc_ring *tx_ring = q_vector->tx.ring;
2718 unsigned int i = tx_ring->next_to_clean;
2719 struct igc_tx_buffer *tx_buffer;
2720 union igc_adv_tx_desc *tx_desc;
2723 if (test_bit(__IGC_DOWN, &adapter->state))
2726 tx_buffer = &tx_ring->tx_buffer_info[i];
2727 tx_desc = IGC_TX_DESC(tx_ring, i);
2728 i -= tx_ring->count;
2731 union igc_adv_tx_desc *eop_desc = tx_buffer->next_to_watch;
2733 /* if next_to_watch is not set then there is no work pending */
2737 /* prevent any other reads prior to eop_desc */
2740 /* if DD is not set pending work has not been completed */
2741 if (!(eop_desc->wb.status & cpu_to_le32(IGC_TXD_STAT_DD)))
2744 /* clear next_to_watch to prevent false hangs */
2745 tx_buffer->next_to_watch = NULL;
2747 /* update the statistics for this packet */
2748 total_bytes += tx_buffer->bytecount;
2749 total_packets += tx_buffer->gso_segs;
2751 switch (tx_buffer->type) {
2752 case IGC_TX_BUFFER_TYPE_XSK:
2755 case IGC_TX_BUFFER_TYPE_XDP:
2756 xdp_return_frame(tx_buffer->xdpf);
2757 igc_unmap_tx_buffer(tx_ring->dev, tx_buffer);
2759 case IGC_TX_BUFFER_TYPE_SKB:
2760 napi_consume_skb(tx_buffer->skb, napi_budget);
2761 igc_unmap_tx_buffer(tx_ring->dev, tx_buffer);
2764 netdev_warn_once(tx_ring->netdev, "Unknown Tx buffer type\n");
2768 /* clear last DMA location and unmap remaining buffers */
2769 while (tx_desc != eop_desc) {
2774 i -= tx_ring->count;
2775 tx_buffer = tx_ring->tx_buffer_info;
2776 tx_desc = IGC_TX_DESC(tx_ring, 0);
2779 /* unmap any remaining paged data */
2780 if (dma_unmap_len(tx_buffer, len))
2781 igc_unmap_tx_buffer(tx_ring->dev, tx_buffer);
2784 /* move us one more past the eop_desc for start of next pkt */
2789 i -= tx_ring->count;
2790 tx_buffer = tx_ring->tx_buffer_info;
2791 tx_desc = IGC_TX_DESC(tx_ring, 0);
2794 /* issue prefetch for next Tx descriptor */
2797 /* update budget accounting */
2799 } while (likely(budget));
2801 netdev_tx_completed_queue(txring_txq(tx_ring),
2802 total_packets, total_bytes);
2804 i += tx_ring->count;
2805 tx_ring->next_to_clean = i;
2807 igc_update_tx_stats(q_vector, total_packets, total_bytes);
2809 if (tx_ring->xsk_pool) {
2811 xsk_tx_completed(tx_ring->xsk_pool, xsk_frames);
2812 if (xsk_uses_need_wakeup(tx_ring->xsk_pool))
2813 xsk_set_tx_need_wakeup(tx_ring->xsk_pool);
2814 igc_xdp_xmit_zc(tx_ring);
2817 if (test_bit(IGC_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags)) {
2818 struct igc_hw *hw = &adapter->hw;
2820 /* Detect a transmit hang in hardware, this serializes the
2821 * check with the clearing of time_stamp and movement of i
2823 clear_bit(IGC_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags);
2824 if (tx_buffer->next_to_watch &&
2825 time_after(jiffies, tx_buffer->time_stamp +
2826 (adapter->tx_timeout_factor * HZ)) &&
2827 !(rd32(IGC_STATUS) & IGC_STATUS_TXOFF)) {
2828 /* detected Tx unit hang */
2829 netdev_err(tx_ring->netdev,
2830 "Detected Tx Unit Hang\n"
2834 " next_to_use <%x>\n"
2835 " next_to_clean <%x>\n"
2836 "buffer_info[next_to_clean]\n"
2837 " time_stamp <%lx>\n"
2838 " next_to_watch <%p>\n"
2840 " desc.status <%x>\n",
2841 tx_ring->queue_index,
2842 rd32(IGC_TDH(tx_ring->reg_idx)),
2843 readl(tx_ring->tail),
2844 tx_ring->next_to_use,
2845 tx_ring->next_to_clean,
2846 tx_buffer->time_stamp,
2847 tx_buffer->next_to_watch,
2849 tx_buffer->next_to_watch->wb.status);
2850 netif_stop_subqueue(tx_ring->netdev,
2851 tx_ring->queue_index);
2853 /* we are about to reset, no point in enabling stuff */
2858 #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
2859 if (unlikely(total_packets &&
2860 netif_carrier_ok(tx_ring->netdev) &&
2861 igc_desc_unused(tx_ring) >= TX_WAKE_THRESHOLD)) {
2862 /* Make sure that anybody stopping the queue after this
2863 * sees the new next_to_clean.
2866 if (__netif_subqueue_stopped(tx_ring->netdev,
2867 tx_ring->queue_index) &&
2868 !(test_bit(__IGC_DOWN, &adapter->state))) {
2869 netif_wake_subqueue(tx_ring->netdev,
2870 tx_ring->queue_index);
2872 u64_stats_update_begin(&tx_ring->tx_syncp);
2873 tx_ring->tx_stats.restart_queue++;
2874 u64_stats_update_end(&tx_ring->tx_syncp);
2881 static int igc_find_mac_filter(struct igc_adapter *adapter,
2882 enum igc_mac_filter_type type, const u8 *addr)
2884 struct igc_hw *hw = &adapter->hw;
2885 int max_entries = hw->mac.rar_entry_count;
2889 for (i = 0; i < max_entries; i++) {
2890 ral = rd32(IGC_RAL(i));
2891 rah = rd32(IGC_RAH(i));
2893 if (!(rah & IGC_RAH_AV))
2895 if (!!(rah & IGC_RAH_ASEL_SRC_ADDR) != type)
2897 if ((rah & IGC_RAH_RAH_MASK) !=
2898 le16_to_cpup((__le16 *)(addr + 4)))
2900 if (ral != le32_to_cpup((__le32 *)(addr)))
2909 static int igc_get_avail_mac_filter_slot(struct igc_adapter *adapter)
2911 struct igc_hw *hw = &adapter->hw;
2912 int max_entries = hw->mac.rar_entry_count;
2916 for (i = 0; i < max_entries; i++) {
2917 rah = rd32(IGC_RAH(i));
2919 if (!(rah & IGC_RAH_AV))
2927 * igc_add_mac_filter() - Add MAC address filter
2928 * @adapter: Pointer to adapter where the filter should be added
2929 * @type: MAC address filter type (source or destination)
2930 * @addr: MAC address
2931 * @queue: If non-negative, queue assignment feature is enabled and frames
2932 * matching the filter are enqueued onto 'queue'. Otherwise, queue
2933 * assignment is disabled.
2935 * Return: 0 in case of success, negative errno code otherwise.
2937 static int igc_add_mac_filter(struct igc_adapter *adapter,
2938 enum igc_mac_filter_type type, const u8 *addr,
2941 struct net_device *dev = adapter->netdev;
2944 index = igc_find_mac_filter(adapter, type, addr);
2948 index = igc_get_avail_mac_filter_slot(adapter);
2952 netdev_dbg(dev, "Add MAC address filter: index %d type %s address %pM queue %d\n",
2953 index, type == IGC_MAC_FILTER_TYPE_DST ? "dst" : "src",
2957 igc_set_mac_filter_hw(adapter, index, type, addr, queue);
2962 * igc_del_mac_filter() - Delete MAC address filter
2963 * @adapter: Pointer to adapter where the filter should be deleted from
2964 * @type: MAC address filter type (source or destination)
2965 * @addr: MAC address
2967 static void igc_del_mac_filter(struct igc_adapter *adapter,
2968 enum igc_mac_filter_type type, const u8 *addr)
2970 struct net_device *dev = adapter->netdev;
2973 index = igc_find_mac_filter(adapter, type, addr);
2978 /* If this is the default filter, we don't actually delete it.
2979 * We just reset to its default value i.e. disable queue
2982 netdev_dbg(dev, "Disable default MAC filter queue assignment");
2984 igc_set_mac_filter_hw(adapter, 0, type, addr, -1);
2986 netdev_dbg(dev, "Delete MAC address filter: index %d type %s address %pM\n",
2988 type == IGC_MAC_FILTER_TYPE_DST ? "dst" : "src",
2991 igc_clear_mac_filter_hw(adapter, index);
2996 * igc_add_vlan_prio_filter() - Add VLAN priority filter
2997 * @adapter: Pointer to adapter where the filter should be added
2998 * @prio: VLAN priority value
2999 * @queue: Queue number which matching frames are assigned to
3001 * Return: 0 in case of success, negative errno code otherwise.
3003 static int igc_add_vlan_prio_filter(struct igc_adapter *adapter, int prio,
3006 struct net_device *dev = adapter->netdev;
3007 struct igc_hw *hw = &adapter->hw;
3010 vlanpqf = rd32(IGC_VLANPQF);
3012 if (vlanpqf & IGC_VLANPQF_VALID(prio)) {
3013 netdev_dbg(dev, "VLAN priority filter already in use\n");
3017 vlanpqf |= IGC_VLANPQF_QSEL(prio, queue);
3018 vlanpqf |= IGC_VLANPQF_VALID(prio);
3020 wr32(IGC_VLANPQF, vlanpqf);
3022 netdev_dbg(dev, "Add VLAN priority filter: prio %d queue %d\n",
3028 * igc_del_vlan_prio_filter() - Delete VLAN priority filter
3029 * @adapter: Pointer to adapter where the filter should be deleted from
3030 * @prio: VLAN priority value
3032 static void igc_del_vlan_prio_filter(struct igc_adapter *adapter, int prio)
3034 struct igc_hw *hw = &adapter->hw;
3037 vlanpqf = rd32(IGC_VLANPQF);
3039 vlanpqf &= ~IGC_VLANPQF_VALID(prio);
3040 vlanpqf &= ~IGC_VLANPQF_QSEL(prio, IGC_VLANPQF_QUEUE_MASK);
3042 wr32(IGC_VLANPQF, vlanpqf);
3044 netdev_dbg(adapter->netdev, "Delete VLAN priority filter: prio %d\n",
3048 static int igc_get_avail_etype_filter_slot(struct igc_adapter *adapter)
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_FILTER_ENABLE))
3064 * igc_add_etype_filter() - Add ethertype filter
3065 * @adapter: Pointer to adapter where the filter should be added
3066 * @etype: Ethertype value
3067 * @queue: If non-negative, queue assignment feature is enabled and frames
3068 * matching the filter are enqueued onto 'queue'. Otherwise, queue
3069 * assignment is disabled.
3071 * Return: 0 in case of success, negative errno code otherwise.
3073 static int igc_add_etype_filter(struct igc_adapter *adapter, u16 etype,
3076 struct igc_hw *hw = &adapter->hw;
3080 index = igc_get_avail_etype_filter_slot(adapter);
3084 etqf = rd32(IGC_ETQF(index));
3086 etqf &= ~IGC_ETQF_ETYPE_MASK;
3090 etqf &= ~IGC_ETQF_QUEUE_MASK;
3091 etqf |= (queue << IGC_ETQF_QUEUE_SHIFT);
3092 etqf |= IGC_ETQF_QUEUE_ENABLE;
3095 etqf |= IGC_ETQF_FILTER_ENABLE;
3097 wr32(IGC_ETQF(index), etqf);
3099 netdev_dbg(adapter->netdev, "Add ethertype filter: etype %04x queue %d\n",
3104 static int igc_find_etype_filter(struct igc_adapter *adapter, u16 etype)
3106 struct igc_hw *hw = &adapter->hw;
3109 for (i = 0; i < MAX_ETYPE_FILTER; i++) {
3110 u32 etqf = rd32(IGC_ETQF(i));
3112 if ((etqf & IGC_ETQF_ETYPE_MASK) == etype)
3120 * igc_del_etype_filter() - Delete ethertype filter
3121 * @adapter: Pointer to adapter where the filter should be deleted from
3122 * @etype: Ethertype value
3124 static void igc_del_etype_filter(struct igc_adapter *adapter, u16 etype)
3126 struct igc_hw *hw = &adapter->hw;
3129 index = igc_find_etype_filter(adapter, etype);
3133 wr32(IGC_ETQF(index), 0);
3135 netdev_dbg(adapter->netdev, "Delete ethertype filter: etype %04x\n",
3139 static int igc_flex_filter_select(struct igc_adapter *adapter,
3140 struct igc_flex_filter *input,
3143 struct igc_hw *hw = &adapter->hw;
3147 if (input->index >= MAX_FLEX_FILTER) {
3148 dev_err(&adapter->pdev->dev, "Wrong Flex Filter index selected!\n");
3152 /* Indirect table select register */
3153 fhftsl = rd32(IGC_FHFTSL);
3154 fhftsl &= ~IGC_FHFTSL_FTSL_MASK;
3155 switch (input->index) {
3169 wr32(IGC_FHFTSL, fhftsl);
3171 /* Normalize index down to host table register */
3172 fhft_index = input->index % 8;
3174 *fhft = (fhft_index < 4) ? IGC_FHFT(fhft_index) :
3175 IGC_FHFT_EXT(fhft_index - 4);
3180 static int igc_write_flex_filter_ll(struct igc_adapter *adapter,
3181 struct igc_flex_filter *input)
3183 struct device *dev = &adapter->pdev->dev;
3184 struct igc_hw *hw = &adapter->hw;
3185 u8 *data = input->data;
3186 u8 *mask = input->mask;
3193 /* Length has to be aligned to 8. Otherwise the filter will fail. Bail
3194 * out early to avoid surprises later.
3196 if (input->length % 8 != 0) {
3197 dev_err(dev, "The length of a flex filter has to be 8 byte aligned!\n");
3201 /* Select corresponding flex filter register and get base for host table. */
3202 ret = igc_flex_filter_select(adapter, input, &fhft);
3206 /* When adding a filter globally disable flex filter feature. That is
3207 * recommended within the datasheet.
3209 wufc = rd32(IGC_WUFC);
3210 wufc &= ~IGC_WUFC_FLEX_HQ;
3211 wr32(IGC_WUFC, wufc);
3213 /* Configure filter */
3214 queuing = input->length & IGC_FHFT_LENGTH_MASK;
3215 queuing |= (input->rx_queue << IGC_FHFT_QUEUE_SHIFT) & IGC_FHFT_QUEUE_MASK;
3216 queuing |= (input->prio << IGC_FHFT_PRIO_SHIFT) & IGC_FHFT_PRIO_MASK;
3218 if (input->immediate_irq)
3219 queuing |= IGC_FHFT_IMM_INT;
3222 queuing |= IGC_FHFT_DROP;
3224 wr32(fhft + 0xFC, queuing);
3226 /* Write data (128 byte) and mask (128 bit) */
3227 for (i = 0; i < 16; ++i) {
3228 const size_t data_idx = i * 8;
3229 const size_t row_idx = i * 16;
3231 (data[data_idx + 0] << 0) |
3232 (data[data_idx + 1] << 8) |
3233 (data[data_idx + 2] << 16) |
3234 (data[data_idx + 3] << 24);
3236 (data[data_idx + 4] << 0) |
3237 (data[data_idx + 5] << 8) |
3238 (data[data_idx + 6] << 16) |
3239 (data[data_idx + 7] << 24);
3242 /* Write row: dw0, dw1 and mask */
3243 wr32(fhft + row_idx, dw0);
3244 wr32(fhft + row_idx + 4, dw1);
3246 /* mask is only valid for MASK(7, 0) */
3247 tmp = rd32(fhft + row_idx + 8);
3248 tmp &= ~GENMASK(7, 0);
3250 wr32(fhft + row_idx + 8, tmp);
3253 /* Enable filter. */
3254 wufc |= IGC_WUFC_FLEX_HQ;
3255 if (input->index > 8) {
3256 /* Filter 0-7 are enabled via WUFC. The other 24 filters are not. */
3257 u32 wufc_ext = rd32(IGC_WUFC_EXT);
3259 wufc_ext |= (IGC_WUFC_EXT_FLX8 << (input->index - 8));
3261 wr32(IGC_WUFC_EXT, wufc_ext);
3263 wufc |= (IGC_WUFC_FLX0 << input->index);
3265 wr32(IGC_WUFC, wufc);
3267 dev_dbg(&adapter->pdev->dev, "Added flex filter %u to HW.\n",
3273 static void igc_flex_filter_add_field(struct igc_flex_filter *flex,
3274 const void *src, unsigned int offset,
3275 size_t len, const void *mask)
3280 memcpy(&flex->data[offset], src, len);
3283 for (i = 0; i < len; ++i) {
3284 const unsigned int idx = i + offset;
3285 const u8 *ptr = mask;
3289 flex->mask[idx / 8] |= BIT(idx % 8);
3294 flex->mask[idx / 8] |= BIT(idx % 8);
3298 static int igc_find_avail_flex_filter_slot(struct igc_adapter *adapter)
3300 struct igc_hw *hw = &adapter->hw;
3304 wufc = rd32(IGC_WUFC);
3305 wufc_ext = rd32(IGC_WUFC_EXT);
3307 for (i = 0; i < MAX_FLEX_FILTER; i++) {
3309 if (!(wufc & (IGC_WUFC_FLX0 << i)))
3312 if (!(wufc_ext & (IGC_WUFC_EXT_FLX8 << (i - 8))))
3320 static bool igc_flex_filter_in_use(struct igc_adapter *adapter)
3322 struct igc_hw *hw = &adapter->hw;
3325 wufc = rd32(IGC_WUFC);
3326 wufc_ext = rd32(IGC_WUFC_EXT);
3328 if (wufc & IGC_WUFC_FILTER_MASK)
3331 if (wufc_ext & IGC_WUFC_EXT_FILTER_MASK)
3337 static int igc_add_flex_filter(struct igc_adapter *adapter,
3338 struct igc_nfc_rule *rule)
3340 struct igc_flex_filter flex = { };
3341 struct igc_nfc_filter *filter = &rule->filter;
3342 unsigned int eth_offset, user_offset;
3346 index = igc_find_avail_flex_filter_slot(adapter);
3350 /* Construct the flex filter:
3357 * -> = 26 bytes => 32 length
3361 flex.rx_queue = rule->action;
3363 vlan = rule->filter.vlan_tci || rule->filter.vlan_etype;
3364 eth_offset = vlan ? 16 : 12;
3365 user_offset = vlan ? 18 : 14;
3367 /* Add destination MAC */
3368 if (rule->filter.match_flags & IGC_FILTER_FLAG_DST_MAC_ADDR)
3369 igc_flex_filter_add_field(&flex, &filter->dst_addr, 0,
3372 /* Add source MAC */
3373 if (rule->filter.match_flags & IGC_FILTER_FLAG_SRC_MAC_ADDR)
3374 igc_flex_filter_add_field(&flex, &filter->src_addr, 6,
3377 /* Add VLAN etype */
3378 if (rule->filter.match_flags & IGC_FILTER_FLAG_VLAN_ETYPE)
3379 igc_flex_filter_add_field(&flex, &filter->vlan_etype, 12,
3380 sizeof(filter->vlan_etype),
3384 if (rule->filter.match_flags & IGC_FILTER_FLAG_VLAN_TCI)
3385 igc_flex_filter_add_field(&flex, &filter->vlan_tci, 14,
3386 sizeof(filter->vlan_tci), NULL);
3388 /* Add Ether type */
3389 if (rule->filter.match_flags & IGC_FILTER_FLAG_ETHER_TYPE) {
3390 __be16 etype = cpu_to_be16(filter->etype);
3392 igc_flex_filter_add_field(&flex, &etype, eth_offset,
3393 sizeof(etype), NULL);
3397 if (rule->filter.match_flags & IGC_FILTER_FLAG_USER_DATA)
3398 igc_flex_filter_add_field(&flex, &filter->user_data,
3400 sizeof(filter->user_data),
3403 /* Add it down to the hardware and enable it. */
3404 ret = igc_write_flex_filter_ll(adapter, &flex);
3408 filter->flex_index = index;
3413 static void igc_del_flex_filter(struct igc_adapter *adapter,
3416 struct igc_hw *hw = &adapter->hw;
3419 /* Just disable the filter. The filter table itself is kept
3420 * intact. Another flex_filter_add() should override the "old" data
3423 if (reg_index > 8) {
3424 u32 wufc_ext = rd32(IGC_WUFC_EXT);
3426 wufc_ext &= ~(IGC_WUFC_EXT_FLX8 << (reg_index - 8));
3427 wr32(IGC_WUFC_EXT, wufc_ext);
3429 wufc = rd32(IGC_WUFC);
3431 wufc &= ~(IGC_WUFC_FLX0 << reg_index);
3432 wr32(IGC_WUFC, wufc);
3435 if (igc_flex_filter_in_use(adapter))
3438 /* No filters are in use, we may disable flex filters */
3439 wufc = rd32(IGC_WUFC);
3440 wufc &= ~IGC_WUFC_FLEX_HQ;
3441 wr32(IGC_WUFC, wufc);
3444 static int igc_enable_nfc_rule(struct igc_adapter *adapter,
3445 struct igc_nfc_rule *rule)
3450 return igc_add_flex_filter(adapter, rule);
3453 if (rule->filter.match_flags & IGC_FILTER_FLAG_ETHER_TYPE) {
3454 err = igc_add_etype_filter(adapter, rule->filter.etype,
3460 if (rule->filter.match_flags & IGC_FILTER_FLAG_SRC_MAC_ADDR) {
3461 err = igc_add_mac_filter(adapter, IGC_MAC_FILTER_TYPE_SRC,
3462 rule->filter.src_addr, rule->action);
3467 if (rule->filter.match_flags & IGC_FILTER_FLAG_DST_MAC_ADDR) {
3468 err = igc_add_mac_filter(adapter, IGC_MAC_FILTER_TYPE_DST,
3469 rule->filter.dst_addr, rule->action);
3474 if (rule->filter.match_flags & IGC_FILTER_FLAG_VLAN_TCI) {
3475 int prio = (rule->filter.vlan_tci & VLAN_PRIO_MASK) >>
3478 err = igc_add_vlan_prio_filter(adapter, prio, rule->action);
3486 static void igc_disable_nfc_rule(struct igc_adapter *adapter,
3487 const struct igc_nfc_rule *rule)
3490 igc_del_flex_filter(adapter, rule->filter.flex_index);
3494 if (rule->filter.match_flags & IGC_FILTER_FLAG_ETHER_TYPE)
3495 igc_del_etype_filter(adapter, rule->filter.etype);
3497 if (rule->filter.match_flags & IGC_FILTER_FLAG_VLAN_TCI) {
3498 int prio = (rule->filter.vlan_tci & VLAN_PRIO_MASK) >>
3501 igc_del_vlan_prio_filter(adapter, prio);
3504 if (rule->filter.match_flags & IGC_FILTER_FLAG_SRC_MAC_ADDR)
3505 igc_del_mac_filter(adapter, IGC_MAC_FILTER_TYPE_SRC,
3506 rule->filter.src_addr);
3508 if (rule->filter.match_flags & IGC_FILTER_FLAG_DST_MAC_ADDR)
3509 igc_del_mac_filter(adapter, IGC_MAC_FILTER_TYPE_DST,
3510 rule->filter.dst_addr);
3514 * igc_get_nfc_rule() - Get NFC rule
3515 * @adapter: Pointer to adapter
3516 * @location: Rule location
3518 * Context: Expects adapter->nfc_rule_lock to be held by caller.
3520 * Return: Pointer to NFC rule at @location. If not found, NULL.
3522 struct igc_nfc_rule *igc_get_nfc_rule(struct igc_adapter *adapter,
3525 struct igc_nfc_rule *rule;
3527 list_for_each_entry(rule, &adapter->nfc_rule_list, list) {
3528 if (rule->location == location)
3530 if (rule->location > location)
3538 * igc_del_nfc_rule() - Delete NFC rule
3539 * @adapter: Pointer to adapter
3540 * @rule: Pointer to rule to be deleted
3542 * Disable NFC rule in hardware and delete it from adapter.
3544 * Context: Expects adapter->nfc_rule_lock to be held by caller.
3546 void igc_del_nfc_rule(struct igc_adapter *adapter, struct igc_nfc_rule *rule)
3548 igc_disable_nfc_rule(adapter, rule);
3550 list_del(&rule->list);
3551 adapter->nfc_rule_count--;
3556 static void igc_flush_nfc_rules(struct igc_adapter *adapter)
3558 struct igc_nfc_rule *rule, *tmp;
3560 mutex_lock(&adapter->nfc_rule_lock);
3562 list_for_each_entry_safe(rule, tmp, &adapter->nfc_rule_list, list)
3563 igc_del_nfc_rule(adapter, rule);
3565 mutex_unlock(&adapter->nfc_rule_lock);
3569 * igc_add_nfc_rule() - Add NFC rule
3570 * @adapter: Pointer to adapter
3571 * @rule: Pointer to rule to be added
3573 * Enable NFC rule in hardware and add it to adapter.
3575 * Context: Expects adapter->nfc_rule_lock to be held by caller.
3577 * Return: 0 on success, negative errno on failure.
3579 int igc_add_nfc_rule(struct igc_adapter *adapter, struct igc_nfc_rule *rule)
3581 struct igc_nfc_rule *pred, *cur;
3584 err = igc_enable_nfc_rule(adapter, rule);
3589 list_for_each_entry(cur, &adapter->nfc_rule_list, list) {
3590 if (cur->location >= rule->location)
3595 list_add(&rule->list, pred ? &pred->list : &adapter->nfc_rule_list);
3596 adapter->nfc_rule_count++;
3600 static void igc_restore_nfc_rules(struct igc_adapter *adapter)
3602 struct igc_nfc_rule *rule;
3604 mutex_lock(&adapter->nfc_rule_lock);
3606 list_for_each_entry_reverse(rule, &adapter->nfc_rule_list, list)
3607 igc_enable_nfc_rule(adapter, rule);
3609 mutex_unlock(&adapter->nfc_rule_lock);
3612 static int igc_uc_sync(struct net_device *netdev, const unsigned char *addr)
3614 struct igc_adapter *adapter = netdev_priv(netdev);
3616 return igc_add_mac_filter(adapter, IGC_MAC_FILTER_TYPE_DST, addr, -1);
3619 static int igc_uc_unsync(struct net_device *netdev, const unsigned char *addr)
3621 struct igc_adapter *adapter = netdev_priv(netdev);
3623 igc_del_mac_filter(adapter, IGC_MAC_FILTER_TYPE_DST, addr);
3628 * igc_set_rx_mode - Secondary Unicast, Multicast and Promiscuous mode set
3629 * @netdev: network interface device structure
3631 * The set_rx_mode entry point is called whenever the unicast or multicast
3632 * address lists or the network interface flags are updated. This routine is
3633 * responsible for configuring the hardware for proper unicast, multicast,
3634 * promiscuous mode, and all-multi behavior.
3636 static void igc_set_rx_mode(struct net_device *netdev)
3638 struct igc_adapter *adapter = netdev_priv(netdev);
3639 struct igc_hw *hw = &adapter->hw;
3640 u32 rctl = 0, rlpml = MAX_JUMBO_FRAME_SIZE;
3643 /* Check for Promiscuous and All Multicast modes */
3644 if (netdev->flags & IFF_PROMISC) {
3645 rctl |= IGC_RCTL_UPE | IGC_RCTL_MPE;
3647 if (netdev->flags & IFF_ALLMULTI) {
3648 rctl |= IGC_RCTL_MPE;
3650 /* Write addresses to the MTA, if the attempt fails
3651 * then we should just turn on promiscuous mode so
3652 * that we can at least receive multicast traffic
3654 count = igc_write_mc_addr_list(netdev);
3656 rctl |= IGC_RCTL_MPE;
3660 /* Write addresses to available RAR registers, if there is not
3661 * sufficient space to store all the addresses then enable
3662 * unicast promiscuous mode
3664 if (__dev_uc_sync(netdev, igc_uc_sync, igc_uc_unsync))
3665 rctl |= IGC_RCTL_UPE;
3667 /* update state of unicast and multicast */
3668 rctl |= rd32(IGC_RCTL) & ~(IGC_RCTL_UPE | IGC_RCTL_MPE);
3669 wr32(IGC_RCTL, rctl);
3671 #if (PAGE_SIZE < 8192)
3672 if (adapter->max_frame_size <= IGC_MAX_FRAME_BUILD_SKB)
3673 rlpml = IGC_MAX_FRAME_BUILD_SKB;
3675 wr32(IGC_RLPML, rlpml);
3679 * igc_configure - configure the hardware for RX and TX
3680 * @adapter: private board structure
3682 static void igc_configure(struct igc_adapter *adapter)
3684 struct net_device *netdev = adapter->netdev;
3687 igc_get_hw_control(adapter);
3688 igc_set_rx_mode(netdev);
3690 igc_restore_vlan(adapter);
3692 igc_setup_tctl(adapter);
3693 igc_setup_mrqc(adapter);
3694 igc_setup_rctl(adapter);
3696 igc_set_default_mac_filter(adapter);
3697 igc_restore_nfc_rules(adapter);
3699 igc_configure_tx(adapter);
3700 igc_configure_rx(adapter);
3702 igc_rx_fifo_flush_base(&adapter->hw);
3704 /* call igc_desc_unused which always leaves
3705 * at least 1 descriptor unused to make sure
3706 * next_to_use != next_to_clean
3708 for (i = 0; i < adapter->num_rx_queues; i++) {
3709 struct igc_ring *ring = adapter->rx_ring[i];
3712 igc_alloc_rx_buffers_zc(ring, igc_desc_unused(ring));
3714 igc_alloc_rx_buffers(ring, igc_desc_unused(ring));
3719 * igc_write_ivar - configure ivar for given MSI-X vector
3720 * @hw: pointer to the HW structure
3721 * @msix_vector: vector number we are allocating to a given ring
3722 * @index: row index of IVAR register to write within IVAR table
3723 * @offset: column offset of in IVAR, should be multiple of 8
3725 * The IVAR table consists of 2 columns,
3726 * each containing an cause allocation for an Rx and Tx ring, and a
3727 * variable number of rows depending on the number of queues supported.
3729 static void igc_write_ivar(struct igc_hw *hw, int msix_vector,
3730 int index, int offset)
3732 u32 ivar = array_rd32(IGC_IVAR0, index);
3734 /* clear any bits that are currently set */
3735 ivar &= ~((u32)0xFF << offset);
3737 /* write vector and valid bit */
3738 ivar |= (msix_vector | IGC_IVAR_VALID) << offset;
3740 array_wr32(IGC_IVAR0, index, ivar);
3743 static void igc_assign_vector(struct igc_q_vector *q_vector, int msix_vector)
3745 struct igc_adapter *adapter = q_vector->adapter;
3746 struct igc_hw *hw = &adapter->hw;
3747 int rx_queue = IGC_N0_QUEUE;
3748 int tx_queue = IGC_N0_QUEUE;
3750 if (q_vector->rx.ring)
3751 rx_queue = q_vector->rx.ring->reg_idx;
3752 if (q_vector->tx.ring)
3753 tx_queue = q_vector->tx.ring->reg_idx;
3755 switch (hw->mac.type) {
3757 if (rx_queue > IGC_N0_QUEUE)
3758 igc_write_ivar(hw, msix_vector,
3760 (rx_queue & 0x1) << 4);
3761 if (tx_queue > IGC_N0_QUEUE)
3762 igc_write_ivar(hw, msix_vector,
3764 ((tx_queue & 0x1) << 4) + 8);
3765 q_vector->eims_value = BIT(msix_vector);
3768 WARN_ONCE(hw->mac.type != igc_i225, "Wrong MAC type\n");
3772 /* add q_vector eims value to global eims_enable_mask */
3773 adapter->eims_enable_mask |= q_vector->eims_value;
3775 /* configure q_vector to set itr on first interrupt */
3776 q_vector->set_itr = 1;
3780 * igc_configure_msix - Configure MSI-X hardware
3781 * @adapter: Pointer to adapter structure
3783 * igc_configure_msix sets up the hardware to properly
3784 * generate MSI-X interrupts.
3786 static void igc_configure_msix(struct igc_adapter *adapter)
3788 struct igc_hw *hw = &adapter->hw;
3792 adapter->eims_enable_mask = 0;
3794 /* set vector for other causes, i.e. link changes */
3795 switch (hw->mac.type) {
3797 /* Turn on MSI-X capability first, or our settings
3798 * won't stick. And it will take days to debug.
3800 wr32(IGC_GPIE, IGC_GPIE_MSIX_MODE |
3801 IGC_GPIE_PBA | IGC_GPIE_EIAME |
3804 /* enable msix_other interrupt */
3805 adapter->eims_other = BIT(vector);
3806 tmp = (vector++ | IGC_IVAR_VALID) << 8;
3808 wr32(IGC_IVAR_MISC, tmp);
3811 /* do nothing, since nothing else supports MSI-X */
3813 } /* switch (hw->mac.type) */
3815 adapter->eims_enable_mask |= adapter->eims_other;
3817 for (i = 0; i < adapter->num_q_vectors; i++)
3818 igc_assign_vector(adapter->q_vector[i], vector++);
3824 * igc_irq_enable - Enable default interrupt generation settings
3825 * @adapter: board private structure
3827 static void igc_irq_enable(struct igc_adapter *adapter)
3829 struct igc_hw *hw = &adapter->hw;
3831 if (adapter->msix_entries) {
3832 u32 ims = IGC_IMS_LSC | IGC_IMS_DOUTSYNC | IGC_IMS_DRSTA;
3833 u32 regval = rd32(IGC_EIAC);
3835 wr32(IGC_EIAC, regval | adapter->eims_enable_mask);
3836 regval = rd32(IGC_EIAM);
3837 wr32(IGC_EIAM, regval | adapter->eims_enable_mask);
3838 wr32(IGC_EIMS, adapter->eims_enable_mask);
3841 wr32(IGC_IMS, IMS_ENABLE_MASK | IGC_IMS_DRSTA);
3842 wr32(IGC_IAM, IMS_ENABLE_MASK | IGC_IMS_DRSTA);
3847 * igc_irq_disable - Mask off interrupt generation on the NIC
3848 * @adapter: board private structure
3850 static void igc_irq_disable(struct igc_adapter *adapter)
3852 struct igc_hw *hw = &adapter->hw;
3854 if (adapter->msix_entries) {
3855 u32 regval = rd32(IGC_EIAM);
3857 wr32(IGC_EIAM, regval & ~adapter->eims_enable_mask);
3858 wr32(IGC_EIMC, adapter->eims_enable_mask);
3859 regval = rd32(IGC_EIAC);
3860 wr32(IGC_EIAC, regval & ~adapter->eims_enable_mask);
3867 if (adapter->msix_entries) {
3870 synchronize_irq(adapter->msix_entries[vector++].vector);
3872 for (i = 0; i < adapter->num_q_vectors; i++)
3873 synchronize_irq(adapter->msix_entries[vector++].vector);
3875 synchronize_irq(adapter->pdev->irq);
3879 void igc_set_flag_queue_pairs(struct igc_adapter *adapter,
3880 const u32 max_rss_queues)
3882 /* Determine if we need to pair queues. */
3883 /* If rss_queues > half of max_rss_queues, pair the queues in
3884 * order to conserve interrupts due to limited supply.
3886 if (adapter->rss_queues > (max_rss_queues / 2))
3887 adapter->flags |= IGC_FLAG_QUEUE_PAIRS;
3889 adapter->flags &= ~IGC_FLAG_QUEUE_PAIRS;
3892 unsigned int igc_get_max_rss_queues(struct igc_adapter *adapter)
3894 return IGC_MAX_RX_QUEUES;
3897 static void igc_init_queue_configuration(struct igc_adapter *adapter)
3901 max_rss_queues = igc_get_max_rss_queues(adapter);
3902 adapter->rss_queues = min_t(u32, max_rss_queues, num_online_cpus());
3904 igc_set_flag_queue_pairs(adapter, max_rss_queues);
3908 * igc_reset_q_vector - Reset config for interrupt vector
3909 * @adapter: board private structure to initialize
3910 * @v_idx: Index of vector to be reset
3912 * If NAPI is enabled it will delete any references to the
3913 * NAPI struct. This is preparation for igc_free_q_vector.
3915 static void igc_reset_q_vector(struct igc_adapter *adapter, int v_idx)
3917 struct igc_q_vector *q_vector = adapter->q_vector[v_idx];
3919 /* if we're coming from igc_set_interrupt_capability, the vectors are
3925 if (q_vector->tx.ring)
3926 adapter->tx_ring[q_vector->tx.ring->queue_index] = NULL;
3928 if (q_vector->rx.ring)
3929 adapter->rx_ring[q_vector->rx.ring->queue_index] = NULL;
3931 netif_napi_del(&q_vector->napi);
3935 * igc_free_q_vector - Free memory allocated for specific interrupt vector
3936 * @adapter: board private structure to initialize
3937 * @v_idx: Index of vector to be freed
3939 * This function frees the memory allocated to the q_vector.
3941 static void igc_free_q_vector(struct igc_adapter *adapter, int v_idx)
3943 struct igc_q_vector *q_vector = adapter->q_vector[v_idx];
3945 adapter->q_vector[v_idx] = NULL;
3947 /* igc_get_stats64() might access the rings on this vector,
3948 * we must wait a grace period before freeing it.
3951 kfree_rcu(q_vector, rcu);
3955 * igc_free_q_vectors - Free memory allocated for interrupt vectors
3956 * @adapter: board private structure to initialize
3958 * This function frees the memory allocated to the q_vectors. In addition if
3959 * NAPI is enabled it will delete any references to the NAPI struct prior
3960 * to freeing the q_vector.
3962 static void igc_free_q_vectors(struct igc_adapter *adapter)
3964 int v_idx = adapter->num_q_vectors;
3966 adapter->num_tx_queues = 0;
3967 adapter->num_rx_queues = 0;
3968 adapter->num_q_vectors = 0;
3971 igc_reset_q_vector(adapter, v_idx);
3972 igc_free_q_vector(adapter, v_idx);
3977 * igc_update_itr - update the dynamic ITR value based on statistics
3978 * @q_vector: pointer to q_vector
3979 * @ring_container: ring info to update the itr for
3981 * Stores a new ITR value based on packets and byte
3982 * counts during the last interrupt. The advantage of per interrupt
3983 * computation is faster updates and more accurate ITR for the current
3984 * traffic pattern. Constants in this function were computed
3985 * based on theoretical maximum wire speed and thresholds were set based
3986 * on testing data as well as attempting to minimize response time
3987 * while increasing bulk throughput.
3988 * NOTE: These calculations are only valid when operating in a single-
3989 * queue environment.
3991 static void igc_update_itr(struct igc_q_vector *q_vector,
3992 struct igc_ring_container *ring_container)
3994 unsigned int packets = ring_container->total_packets;
3995 unsigned int bytes = ring_container->total_bytes;
3996 u8 itrval = ring_container->itr;
3998 /* no packets, exit with status unchanged */
4003 case lowest_latency:
4004 /* handle TSO and jumbo frames */
4005 if (bytes / packets > 8000)
4006 itrval = bulk_latency;
4007 else if ((packets < 5) && (bytes > 512))
4008 itrval = low_latency;
4010 case low_latency: /* 50 usec aka 20000 ints/s */
4011 if (bytes > 10000) {
4012 /* this if handles the TSO accounting */
4013 if (bytes / packets > 8000)
4014 itrval = bulk_latency;
4015 else if ((packets < 10) || ((bytes / packets) > 1200))
4016 itrval = bulk_latency;
4017 else if ((packets > 35))
4018 itrval = lowest_latency;
4019 } else if (bytes / packets > 2000) {
4020 itrval = bulk_latency;
4021 } else if (packets <= 2 && bytes < 512) {
4022 itrval = lowest_latency;
4025 case bulk_latency: /* 250 usec aka 4000 ints/s */
4026 if (bytes > 25000) {
4028 itrval = low_latency;
4029 } else if (bytes < 1500) {
4030 itrval = low_latency;
4035 /* clear work counters since we have the values we need */
4036 ring_container->total_bytes = 0;
4037 ring_container->total_packets = 0;
4039 /* write updated itr to ring container */
4040 ring_container->itr = itrval;
4043 static void igc_set_itr(struct igc_q_vector *q_vector)
4045 struct igc_adapter *adapter = q_vector->adapter;
4046 u32 new_itr = q_vector->itr_val;
4049 /* for non-gigabit speeds, just fix the interrupt rate at 4000 */
4050 switch (adapter->link_speed) {
4054 new_itr = IGC_4K_ITR;
4060 igc_update_itr(q_vector, &q_vector->tx);
4061 igc_update_itr(q_vector, &q_vector->rx);
4063 current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
4065 /* conservative mode (itr 3) eliminates the lowest_latency setting */
4066 if (current_itr == lowest_latency &&
4067 ((q_vector->rx.ring && adapter->rx_itr_setting == 3) ||
4068 (!q_vector->rx.ring && adapter->tx_itr_setting == 3)))
4069 current_itr = low_latency;
4071 switch (current_itr) {
4072 /* counts and packets in update_itr are dependent on these numbers */
4073 case lowest_latency:
4074 new_itr = IGC_70K_ITR; /* 70,000 ints/sec */
4077 new_itr = IGC_20K_ITR; /* 20,000 ints/sec */
4080 new_itr = IGC_4K_ITR; /* 4,000 ints/sec */
4087 if (new_itr != q_vector->itr_val) {
4088 /* this attempts to bias the interrupt rate towards Bulk
4089 * by adding intermediate steps when interrupt rate is
4092 new_itr = new_itr > q_vector->itr_val ?
4093 max((new_itr * q_vector->itr_val) /
4094 (new_itr + (q_vector->itr_val >> 2)),
4096 /* Don't write the value here; it resets the adapter's
4097 * internal timer, and causes us to delay far longer than
4098 * we should between interrupts. Instead, we write the ITR
4099 * value at the beginning of the next interrupt so the timing
4100 * ends up being correct.
4102 q_vector->itr_val = new_itr;
4103 q_vector->set_itr = 1;
4107 static void igc_reset_interrupt_capability(struct igc_adapter *adapter)
4109 int v_idx = adapter->num_q_vectors;
4111 if (adapter->msix_entries) {
4112 pci_disable_msix(adapter->pdev);
4113 kfree(adapter->msix_entries);
4114 adapter->msix_entries = NULL;
4115 } else if (adapter->flags & IGC_FLAG_HAS_MSI) {
4116 pci_disable_msi(adapter->pdev);
4120 igc_reset_q_vector(adapter, v_idx);
4124 * igc_set_interrupt_capability - set MSI or MSI-X if supported
4125 * @adapter: Pointer to adapter structure
4126 * @msix: boolean value for MSI-X capability
4128 * Attempt to configure interrupts using the best available
4129 * capabilities of the hardware and kernel.
4131 static void igc_set_interrupt_capability(struct igc_adapter *adapter,
4139 adapter->flags |= IGC_FLAG_HAS_MSIX;
4141 /* Number of supported queues. */
4142 adapter->num_rx_queues = adapter->rss_queues;
4144 adapter->num_tx_queues = adapter->rss_queues;
4146 /* start with one vector for every Rx queue */
4147 numvecs = adapter->num_rx_queues;
4149 /* if Tx handler is separate add 1 for every Tx queue */
4150 if (!(adapter->flags & IGC_FLAG_QUEUE_PAIRS))
4151 numvecs += adapter->num_tx_queues;
4153 /* store the number of vectors reserved for queues */
4154 adapter->num_q_vectors = numvecs;
4156 /* add 1 vector for link status interrupts */
4159 adapter->msix_entries = kcalloc(numvecs, sizeof(struct msix_entry),
4162 if (!adapter->msix_entries)
4165 /* populate entry values */
4166 for (i = 0; i < numvecs; i++)
4167 adapter->msix_entries[i].entry = i;
4169 err = pci_enable_msix_range(adapter->pdev,
4170 adapter->msix_entries,
4176 kfree(adapter->msix_entries);
4177 adapter->msix_entries = NULL;
4179 igc_reset_interrupt_capability(adapter);
4182 adapter->flags &= ~IGC_FLAG_HAS_MSIX;
4184 adapter->rss_queues = 1;
4185 adapter->flags |= IGC_FLAG_QUEUE_PAIRS;
4186 adapter->num_rx_queues = 1;
4187 adapter->num_tx_queues = 1;
4188 adapter->num_q_vectors = 1;
4189 if (!pci_enable_msi(adapter->pdev))
4190 adapter->flags |= IGC_FLAG_HAS_MSI;
4194 * igc_update_ring_itr - update the dynamic ITR value based on packet size
4195 * @q_vector: pointer to q_vector
4197 * Stores a new ITR value based on strictly on packet size. This
4198 * algorithm is less sophisticated than that used in igc_update_itr,
4199 * due to the difficulty of synchronizing statistics across multiple
4200 * receive rings. The divisors and thresholds used by this function
4201 * were determined based on theoretical maximum wire speed and testing
4202 * data, in order to minimize response time while increasing bulk
4204 * NOTE: This function is called only when operating in a multiqueue
4205 * receive environment.
4207 static void igc_update_ring_itr(struct igc_q_vector *q_vector)
4209 struct igc_adapter *adapter = q_vector->adapter;
4210 int new_val = q_vector->itr_val;
4211 int avg_wire_size = 0;
4212 unsigned int packets;
4214 /* For non-gigabit speeds, just fix the interrupt rate at 4000
4215 * ints/sec - ITR timer value of 120 ticks.
4217 switch (adapter->link_speed) {
4220 new_val = IGC_4K_ITR;
4226 packets = q_vector->rx.total_packets;
4228 avg_wire_size = q_vector->rx.total_bytes / packets;
4230 packets = q_vector->tx.total_packets;
4232 avg_wire_size = max_t(u32, avg_wire_size,
4233 q_vector->tx.total_bytes / packets);
4235 /* if avg_wire_size isn't set no work was done */
4239 /* Add 24 bytes to size to account for CRC, preamble, and gap */
4240 avg_wire_size += 24;
4242 /* Don't starve jumbo frames */
4243 avg_wire_size = min(avg_wire_size, 3000);
4245 /* Give a little boost to mid-size frames */
4246 if (avg_wire_size > 300 && avg_wire_size < 1200)
4247 new_val = avg_wire_size / 3;
4249 new_val = avg_wire_size / 2;
4251 /* conservative mode (itr 3) eliminates the lowest_latency setting */
4252 if (new_val < IGC_20K_ITR &&
4253 ((q_vector->rx.ring && adapter->rx_itr_setting == 3) ||
4254 (!q_vector->rx.ring && adapter->tx_itr_setting == 3)))
4255 new_val = IGC_20K_ITR;
4258 if (new_val != q_vector->itr_val) {
4259 q_vector->itr_val = new_val;
4260 q_vector->set_itr = 1;
4263 q_vector->rx.total_bytes = 0;
4264 q_vector->rx.total_packets = 0;
4265 q_vector->tx.total_bytes = 0;
4266 q_vector->tx.total_packets = 0;
4269 static void igc_ring_irq_enable(struct igc_q_vector *q_vector)
4271 struct igc_adapter *adapter = q_vector->adapter;
4272 struct igc_hw *hw = &adapter->hw;
4274 if ((q_vector->rx.ring && (adapter->rx_itr_setting & 3)) ||
4275 (!q_vector->rx.ring && (adapter->tx_itr_setting & 3))) {
4276 if (adapter->num_q_vectors == 1)
4277 igc_set_itr(q_vector);
4279 igc_update_ring_itr(q_vector);
4282 if (!test_bit(__IGC_DOWN, &adapter->state)) {
4283 if (adapter->msix_entries)
4284 wr32(IGC_EIMS, q_vector->eims_value);
4286 igc_irq_enable(adapter);
4290 static void igc_add_ring(struct igc_ring *ring,
4291 struct igc_ring_container *head)
4298 * igc_cache_ring_register - Descriptor ring to register mapping
4299 * @adapter: board private structure to initialize
4301 * Once we know the feature-set enabled for the device, we'll cache
4302 * the register offset the descriptor ring is assigned to.
4304 static void igc_cache_ring_register(struct igc_adapter *adapter)
4308 switch (adapter->hw.mac.type) {
4311 for (; i < adapter->num_rx_queues; i++)
4312 adapter->rx_ring[i]->reg_idx = i;
4313 for (; j < adapter->num_tx_queues; j++)
4314 adapter->tx_ring[j]->reg_idx = j;
4320 * igc_poll - NAPI Rx polling callback
4321 * @napi: napi polling structure
4322 * @budget: count of how many packets we should handle
4324 static int igc_poll(struct napi_struct *napi, int budget)
4326 struct igc_q_vector *q_vector = container_of(napi,
4327 struct igc_q_vector,
4329 struct igc_ring *rx_ring = q_vector->rx.ring;
4330 bool clean_complete = true;
4333 if (q_vector->tx.ring)
4334 clean_complete = igc_clean_tx_irq(q_vector, budget);
4337 int cleaned = rx_ring->xsk_pool ?
4338 igc_clean_rx_irq_zc(q_vector, budget) :
4339 igc_clean_rx_irq(q_vector, budget);
4341 work_done += cleaned;
4342 if (cleaned >= budget)
4343 clean_complete = false;
4346 /* If all work not completed, return budget and keep polling */
4347 if (!clean_complete)
4350 /* Exit the polling mode, but don't re-enable interrupts if stack might
4351 * poll us due to busy-polling
4353 if (likely(napi_complete_done(napi, work_done)))
4354 igc_ring_irq_enable(q_vector);
4356 return min(work_done, budget - 1);
4360 * igc_alloc_q_vector - Allocate memory for a single interrupt vector
4361 * @adapter: board private structure to initialize
4362 * @v_count: q_vectors allocated on adapter, used for ring interleaving
4363 * @v_idx: index of vector in adapter struct
4364 * @txr_count: total number of Tx rings to allocate
4365 * @txr_idx: index of first Tx ring to allocate
4366 * @rxr_count: total number of Rx rings to allocate
4367 * @rxr_idx: index of first Rx ring to allocate
4369 * We allocate one q_vector. If allocation fails we return -ENOMEM.
4371 static int igc_alloc_q_vector(struct igc_adapter *adapter,
4372 unsigned int v_count, unsigned int v_idx,
4373 unsigned int txr_count, unsigned int txr_idx,
4374 unsigned int rxr_count, unsigned int rxr_idx)
4376 struct igc_q_vector *q_vector;
4377 struct igc_ring *ring;
4380 /* igc only supports 1 Tx and/or 1 Rx queue per vector */
4381 if (txr_count > 1 || rxr_count > 1)
4384 ring_count = txr_count + rxr_count;
4386 /* allocate q_vector and rings */
4387 q_vector = adapter->q_vector[v_idx];
4389 q_vector = kzalloc(struct_size(q_vector, ring, ring_count),
4392 memset(q_vector, 0, struct_size(q_vector, ring, ring_count));
4396 /* initialize NAPI */
4397 netif_napi_add(adapter->netdev, &q_vector->napi,
4400 /* tie q_vector and adapter together */
4401 adapter->q_vector[v_idx] = q_vector;
4402 q_vector->adapter = adapter;
4404 /* initialize work limits */
4405 q_vector->tx.work_limit = adapter->tx_work_limit;
4407 /* initialize ITR configuration */
4408 q_vector->itr_register = adapter->io_addr + IGC_EITR(0);
4409 q_vector->itr_val = IGC_START_ITR;
4411 /* initialize pointer to rings */
4412 ring = q_vector->ring;
4414 /* initialize ITR */
4416 /* rx or rx/tx vector */
4417 if (!adapter->rx_itr_setting || adapter->rx_itr_setting > 3)
4418 q_vector->itr_val = adapter->rx_itr_setting;
4420 /* tx only vector */
4421 if (!adapter->tx_itr_setting || adapter->tx_itr_setting > 3)
4422 q_vector->itr_val = adapter->tx_itr_setting;
4426 /* assign generic ring traits */
4427 ring->dev = &adapter->pdev->dev;
4428 ring->netdev = adapter->netdev;
4430 /* configure backlink on ring */
4431 ring->q_vector = q_vector;
4433 /* update q_vector Tx values */
4434 igc_add_ring(ring, &q_vector->tx);
4436 /* apply Tx specific ring traits */
4437 ring->count = adapter->tx_ring_count;
4438 ring->queue_index = txr_idx;
4440 /* assign ring to adapter */
4441 adapter->tx_ring[txr_idx] = ring;
4443 /* push pointer to next ring */
4448 /* assign generic ring traits */
4449 ring->dev = &adapter->pdev->dev;
4450 ring->netdev = adapter->netdev;
4452 /* configure backlink on ring */
4453 ring->q_vector = q_vector;
4455 /* update q_vector Rx values */
4456 igc_add_ring(ring, &q_vector->rx);
4458 /* apply Rx specific ring traits */
4459 ring->count = adapter->rx_ring_count;
4460 ring->queue_index = rxr_idx;
4462 /* assign ring to adapter */
4463 adapter->rx_ring[rxr_idx] = ring;
4470 * igc_alloc_q_vectors - Allocate memory for interrupt vectors
4471 * @adapter: board private structure to initialize
4473 * We allocate one q_vector per queue interrupt. If allocation fails we
4476 static int igc_alloc_q_vectors(struct igc_adapter *adapter)
4478 int rxr_remaining = adapter->num_rx_queues;
4479 int txr_remaining = adapter->num_tx_queues;
4480 int rxr_idx = 0, txr_idx = 0, v_idx = 0;
4481 int q_vectors = adapter->num_q_vectors;
4484 if (q_vectors >= (rxr_remaining + txr_remaining)) {
4485 for (; rxr_remaining; v_idx++) {
4486 err = igc_alloc_q_vector(adapter, q_vectors, v_idx,
4492 /* update counts and index */
4498 for (; v_idx < q_vectors; v_idx++) {
4499 int rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - v_idx);
4500 int tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - v_idx);
4502 err = igc_alloc_q_vector(adapter, q_vectors, v_idx,
4503 tqpv, txr_idx, rqpv, rxr_idx);
4508 /* update counts and index */
4509 rxr_remaining -= rqpv;
4510 txr_remaining -= tqpv;
4518 adapter->num_tx_queues = 0;
4519 adapter->num_rx_queues = 0;
4520 adapter->num_q_vectors = 0;
4523 igc_free_q_vector(adapter, v_idx);
4529 * igc_init_interrupt_scheme - initialize interrupts, allocate queues/vectors
4530 * @adapter: Pointer to adapter structure
4531 * @msix: boolean for MSI-X capability
4533 * This function initializes the interrupts and allocates all of the queues.
4535 static int igc_init_interrupt_scheme(struct igc_adapter *adapter, bool msix)
4537 struct net_device *dev = adapter->netdev;
4540 igc_set_interrupt_capability(adapter, msix);
4542 err = igc_alloc_q_vectors(adapter);
4544 netdev_err(dev, "Unable to allocate memory for vectors\n");
4545 goto err_alloc_q_vectors;
4548 igc_cache_ring_register(adapter);
4552 err_alloc_q_vectors:
4553 igc_reset_interrupt_capability(adapter);
4558 * igc_sw_init - Initialize general software structures (struct igc_adapter)
4559 * @adapter: board private structure to initialize
4561 * igc_sw_init initializes the Adapter private data structure.
4562 * Fields are initialized based on PCI device information and
4563 * OS network device settings (MTU size).
4565 static int igc_sw_init(struct igc_adapter *adapter)
4567 struct net_device *netdev = adapter->netdev;
4568 struct pci_dev *pdev = adapter->pdev;
4569 struct igc_hw *hw = &adapter->hw;
4571 pci_read_config_word(pdev, PCI_COMMAND, &hw->bus.pci_cmd_word);
4573 /* set default ring sizes */
4574 adapter->tx_ring_count = IGC_DEFAULT_TXD;
4575 adapter->rx_ring_count = IGC_DEFAULT_RXD;
4577 /* set default ITR values */
4578 adapter->rx_itr_setting = IGC_DEFAULT_ITR;
4579 adapter->tx_itr_setting = IGC_DEFAULT_ITR;
4581 /* set default work limits */
4582 adapter->tx_work_limit = IGC_DEFAULT_TX_WORK;
4584 /* adjust max frame to be at least the size of a standard frame */
4585 adapter->max_frame_size = netdev->mtu + ETH_HLEN + ETH_FCS_LEN +
4587 adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
4589 mutex_init(&adapter->nfc_rule_lock);
4590 INIT_LIST_HEAD(&adapter->nfc_rule_list);
4591 adapter->nfc_rule_count = 0;
4593 spin_lock_init(&adapter->stats64_lock);
4594 /* Assume MSI-X interrupts, will be checked during IRQ allocation */
4595 adapter->flags |= IGC_FLAG_HAS_MSIX;
4597 igc_init_queue_configuration(adapter);
4599 /* This call may decrease the number of queues */
4600 if (igc_init_interrupt_scheme(adapter, true)) {
4601 netdev_err(netdev, "Unable to allocate memory for queues\n");
4605 /* Explicitly disable IRQ since the NIC can be in any state. */
4606 igc_irq_disable(adapter);
4608 set_bit(__IGC_DOWN, &adapter->state);
4614 * igc_up - Open the interface and prepare it to handle traffic
4615 * @adapter: board private structure
4617 void igc_up(struct igc_adapter *adapter)
4619 struct igc_hw *hw = &adapter->hw;
4622 /* hardware has been reset, we need to reload some things */
4623 igc_configure(adapter);
4625 clear_bit(__IGC_DOWN, &adapter->state);
4627 for (i = 0; i < adapter->num_q_vectors; i++)
4628 napi_enable(&adapter->q_vector[i]->napi);
4630 if (adapter->msix_entries)
4631 igc_configure_msix(adapter);
4633 igc_assign_vector(adapter->q_vector[0], 0);
4635 /* Clear any pending interrupts. */
4637 igc_irq_enable(adapter);
4639 netif_tx_start_all_queues(adapter->netdev);
4641 /* start the watchdog. */
4642 hw->mac.get_link_status = true;
4643 schedule_work(&adapter->watchdog_task);
4647 * igc_update_stats - Update the board statistics counters
4648 * @adapter: board private structure
4650 void igc_update_stats(struct igc_adapter *adapter)
4652 struct rtnl_link_stats64 *net_stats = &adapter->stats64;
4653 struct pci_dev *pdev = adapter->pdev;
4654 struct igc_hw *hw = &adapter->hw;
4655 u64 _bytes, _packets;
4661 /* Prevent stats update while adapter is being reset, or if the pci
4662 * connection is down.
4664 if (adapter->link_speed == 0)
4666 if (pci_channel_offline(pdev))
4673 for (i = 0; i < adapter->num_rx_queues; i++) {
4674 struct igc_ring *ring = adapter->rx_ring[i];
4675 u32 rqdpc = rd32(IGC_RQDPC(i));
4677 if (hw->mac.type >= igc_i225)
4678 wr32(IGC_RQDPC(i), 0);
4681 ring->rx_stats.drops += rqdpc;
4682 net_stats->rx_fifo_errors += rqdpc;
4686 start = u64_stats_fetch_begin_irq(&ring->rx_syncp);
4687 _bytes = ring->rx_stats.bytes;
4688 _packets = ring->rx_stats.packets;
4689 } while (u64_stats_fetch_retry_irq(&ring->rx_syncp, start));
4691 packets += _packets;
4694 net_stats->rx_bytes = bytes;
4695 net_stats->rx_packets = packets;
4699 for (i = 0; i < adapter->num_tx_queues; i++) {
4700 struct igc_ring *ring = adapter->tx_ring[i];
4703 start = u64_stats_fetch_begin_irq(&ring->tx_syncp);
4704 _bytes = ring->tx_stats.bytes;
4705 _packets = ring->tx_stats.packets;
4706 } while (u64_stats_fetch_retry_irq(&ring->tx_syncp, start));
4708 packets += _packets;
4710 net_stats->tx_bytes = bytes;
4711 net_stats->tx_packets = packets;
4714 /* read stats registers */
4715 adapter->stats.crcerrs += rd32(IGC_CRCERRS);
4716 adapter->stats.gprc += rd32(IGC_GPRC);
4717 adapter->stats.gorc += rd32(IGC_GORCL);
4718 rd32(IGC_GORCH); /* clear GORCL */
4719 adapter->stats.bprc += rd32(IGC_BPRC);
4720 adapter->stats.mprc += rd32(IGC_MPRC);
4721 adapter->stats.roc += rd32(IGC_ROC);
4723 adapter->stats.prc64 += rd32(IGC_PRC64);
4724 adapter->stats.prc127 += rd32(IGC_PRC127);
4725 adapter->stats.prc255 += rd32(IGC_PRC255);
4726 adapter->stats.prc511 += rd32(IGC_PRC511);
4727 adapter->stats.prc1023 += rd32(IGC_PRC1023);
4728 adapter->stats.prc1522 += rd32(IGC_PRC1522);
4729 adapter->stats.tlpic += rd32(IGC_TLPIC);
4730 adapter->stats.rlpic += rd32(IGC_RLPIC);
4731 adapter->stats.hgptc += rd32(IGC_HGPTC);
4733 mpc = rd32(IGC_MPC);
4734 adapter->stats.mpc += mpc;
4735 net_stats->rx_fifo_errors += mpc;
4736 adapter->stats.scc += rd32(IGC_SCC);
4737 adapter->stats.ecol += rd32(IGC_ECOL);
4738 adapter->stats.mcc += rd32(IGC_MCC);
4739 adapter->stats.latecol += rd32(IGC_LATECOL);
4740 adapter->stats.dc += rd32(IGC_DC);
4741 adapter->stats.rlec += rd32(IGC_RLEC);
4742 adapter->stats.xonrxc += rd32(IGC_XONRXC);
4743 adapter->stats.xontxc += rd32(IGC_XONTXC);
4744 adapter->stats.xoffrxc += rd32(IGC_XOFFRXC);
4745 adapter->stats.xofftxc += rd32(IGC_XOFFTXC);
4746 adapter->stats.fcruc += rd32(IGC_FCRUC);
4747 adapter->stats.gptc += rd32(IGC_GPTC);
4748 adapter->stats.gotc += rd32(IGC_GOTCL);
4749 rd32(IGC_GOTCH); /* clear GOTCL */
4750 adapter->stats.rnbc += rd32(IGC_RNBC);
4751 adapter->stats.ruc += rd32(IGC_RUC);
4752 adapter->stats.rfc += rd32(IGC_RFC);
4753 adapter->stats.rjc += rd32(IGC_RJC);
4754 adapter->stats.tor += rd32(IGC_TORH);
4755 adapter->stats.tot += rd32(IGC_TOTH);
4756 adapter->stats.tpr += rd32(IGC_TPR);
4758 adapter->stats.ptc64 += rd32(IGC_PTC64);
4759 adapter->stats.ptc127 += rd32(IGC_PTC127);
4760 adapter->stats.ptc255 += rd32(IGC_PTC255);
4761 adapter->stats.ptc511 += rd32(IGC_PTC511);
4762 adapter->stats.ptc1023 += rd32(IGC_PTC1023);
4763 adapter->stats.ptc1522 += rd32(IGC_PTC1522);
4765 adapter->stats.mptc += rd32(IGC_MPTC);
4766 adapter->stats.bptc += rd32(IGC_BPTC);
4768 adapter->stats.tpt += rd32(IGC_TPT);
4769 adapter->stats.colc += rd32(IGC_COLC);
4770 adapter->stats.colc += rd32(IGC_RERC);
4772 adapter->stats.algnerrc += rd32(IGC_ALGNERRC);
4774 adapter->stats.tsctc += rd32(IGC_TSCTC);
4776 adapter->stats.iac += rd32(IGC_IAC);
4778 /* Fill out the OS statistics structure */
4779 net_stats->multicast = adapter->stats.mprc;
4780 net_stats->collisions = adapter->stats.colc;
4784 /* RLEC on some newer hardware can be incorrect so build
4785 * our own version based on RUC and ROC
4787 net_stats->rx_errors = adapter->stats.rxerrc +
4788 adapter->stats.crcerrs + adapter->stats.algnerrc +
4789 adapter->stats.ruc + adapter->stats.roc +
4790 adapter->stats.cexterr;
4791 net_stats->rx_length_errors = adapter->stats.ruc +
4793 net_stats->rx_crc_errors = adapter->stats.crcerrs;
4794 net_stats->rx_frame_errors = adapter->stats.algnerrc;
4795 net_stats->rx_missed_errors = adapter->stats.mpc;
4798 net_stats->tx_errors = adapter->stats.ecol +
4799 adapter->stats.latecol;
4800 net_stats->tx_aborted_errors = adapter->stats.ecol;
4801 net_stats->tx_window_errors = adapter->stats.latecol;
4802 net_stats->tx_carrier_errors = adapter->stats.tncrs;
4804 /* Tx Dropped needs to be maintained elsewhere */
4806 /* Management Stats */
4807 adapter->stats.mgptc += rd32(IGC_MGTPTC);
4808 adapter->stats.mgprc += rd32(IGC_MGTPRC);
4809 adapter->stats.mgpdc += rd32(IGC_MGTPDC);
4813 * igc_down - Close the interface
4814 * @adapter: board private structure
4816 void igc_down(struct igc_adapter *adapter)
4818 struct net_device *netdev = adapter->netdev;
4819 struct igc_hw *hw = &adapter->hw;
4823 set_bit(__IGC_DOWN, &adapter->state);
4825 igc_ptp_suspend(adapter);
4827 if (pci_device_is_present(adapter->pdev)) {
4828 /* disable receives in the hardware */
4829 rctl = rd32(IGC_RCTL);
4830 wr32(IGC_RCTL, rctl & ~IGC_RCTL_EN);
4831 /* flush and sleep below */
4833 /* set trans_start so we don't get spurious watchdogs during reset */
4834 netif_trans_update(netdev);
4836 netif_carrier_off(netdev);
4837 netif_tx_stop_all_queues(netdev);
4839 if (pci_device_is_present(adapter->pdev)) {
4840 /* disable transmits in the hardware */
4841 tctl = rd32(IGC_TCTL);
4842 tctl &= ~IGC_TCTL_EN;
4843 wr32(IGC_TCTL, tctl);
4844 /* flush both disables and wait for them to finish */
4846 usleep_range(10000, 20000);
4848 igc_irq_disable(adapter);
4851 adapter->flags &= ~IGC_FLAG_NEED_LINK_UPDATE;
4853 for (i = 0; i < adapter->num_q_vectors; i++) {
4854 if (adapter->q_vector[i]) {
4855 napi_synchronize(&adapter->q_vector[i]->napi);
4856 napi_disable(&adapter->q_vector[i]->napi);
4860 del_timer_sync(&adapter->watchdog_timer);
4861 del_timer_sync(&adapter->phy_info_timer);
4863 /* record the stats before reset*/
4864 spin_lock(&adapter->stats64_lock);
4865 igc_update_stats(adapter);
4866 spin_unlock(&adapter->stats64_lock);
4868 adapter->link_speed = 0;
4869 adapter->link_duplex = 0;
4871 if (!pci_channel_offline(adapter->pdev))
4874 /* clear VLAN promisc flag so VFTA will be updated if necessary */
4875 adapter->flags &= ~IGC_FLAG_VLAN_PROMISC;
4877 igc_clean_all_tx_rings(adapter);
4878 igc_clean_all_rx_rings(adapter);
4881 void igc_reinit_locked(struct igc_adapter *adapter)
4883 while (test_and_set_bit(__IGC_RESETTING, &adapter->state))
4884 usleep_range(1000, 2000);
4887 clear_bit(__IGC_RESETTING, &adapter->state);
4890 static void igc_reset_task(struct work_struct *work)
4892 struct igc_adapter *adapter;
4894 adapter = container_of(work, struct igc_adapter, reset_task);
4897 /* If we're already down or resetting, just bail */
4898 if (test_bit(__IGC_DOWN, &adapter->state) ||
4899 test_bit(__IGC_RESETTING, &adapter->state)) {
4904 igc_rings_dump(adapter);
4905 igc_regs_dump(adapter);
4906 netdev_err(adapter->netdev, "Reset adapter\n");
4907 igc_reinit_locked(adapter);
4912 * igc_change_mtu - Change the Maximum Transfer Unit
4913 * @netdev: network interface device structure
4914 * @new_mtu: new value for maximum frame size
4916 * Returns 0 on success, negative on failure
4918 static int igc_change_mtu(struct net_device *netdev, int new_mtu)
4920 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
4921 struct igc_adapter *adapter = netdev_priv(netdev);
4923 if (igc_xdp_is_enabled(adapter) && new_mtu > ETH_DATA_LEN) {
4924 netdev_dbg(netdev, "Jumbo frames not supported with XDP");
4928 /* adjust max frame to be at least the size of a standard frame */
4929 if (max_frame < (ETH_FRAME_LEN + ETH_FCS_LEN))
4930 max_frame = ETH_FRAME_LEN + ETH_FCS_LEN;
4932 while (test_and_set_bit(__IGC_RESETTING, &adapter->state))
4933 usleep_range(1000, 2000);
4935 /* igc_down has a dependency on max_frame_size */
4936 adapter->max_frame_size = max_frame;
4938 if (netif_running(netdev))
4941 netdev_dbg(netdev, "changing MTU from %d to %d\n", netdev->mtu, new_mtu);
4942 netdev->mtu = new_mtu;
4944 if (netif_running(netdev))
4949 clear_bit(__IGC_RESETTING, &adapter->state);
4955 * igc_get_stats64 - Get System Network Statistics
4956 * @netdev: network interface device structure
4957 * @stats: rtnl_link_stats64 pointer
4959 * Returns the address of the device statistics structure.
4960 * The statistics are updated here and also from the timer callback.
4962 static void igc_get_stats64(struct net_device *netdev,
4963 struct rtnl_link_stats64 *stats)
4965 struct igc_adapter *adapter = netdev_priv(netdev);
4967 spin_lock(&adapter->stats64_lock);
4968 if (!test_bit(__IGC_RESETTING, &adapter->state))
4969 igc_update_stats(adapter);
4970 memcpy(stats, &adapter->stats64, sizeof(*stats));
4971 spin_unlock(&adapter->stats64_lock);
4974 static netdev_features_t igc_fix_features(struct net_device *netdev,
4975 netdev_features_t features)
4977 /* Since there is no support for separate Rx/Tx vlan accel
4978 * enable/disable make sure Tx flag is always in same state as Rx.
4980 if (features & NETIF_F_HW_VLAN_CTAG_RX)
4981 features |= NETIF_F_HW_VLAN_CTAG_TX;
4983 features &= ~NETIF_F_HW_VLAN_CTAG_TX;
4988 static int igc_set_features(struct net_device *netdev,
4989 netdev_features_t features)
4991 netdev_features_t changed = netdev->features ^ features;
4992 struct igc_adapter *adapter = netdev_priv(netdev);
4994 if (changed & NETIF_F_HW_VLAN_CTAG_RX)
4995 igc_vlan_mode(netdev, features);
4997 /* Add VLAN support */
4998 if (!(changed & (NETIF_F_RXALL | NETIF_F_NTUPLE)))
5001 if (!(features & NETIF_F_NTUPLE))
5002 igc_flush_nfc_rules(adapter);
5004 netdev->features = features;
5006 if (netif_running(netdev))
5007 igc_reinit_locked(adapter);
5014 static netdev_features_t
5015 igc_features_check(struct sk_buff *skb, struct net_device *dev,
5016 netdev_features_t features)
5018 unsigned int network_hdr_len, mac_hdr_len;
5020 /* Make certain the headers can be described by a context descriptor */
5021 mac_hdr_len = skb_network_header(skb) - skb->data;
5022 if (unlikely(mac_hdr_len > IGC_MAX_MAC_HDR_LEN))
5023 return features & ~(NETIF_F_HW_CSUM |
5025 NETIF_F_HW_VLAN_CTAG_TX |
5029 network_hdr_len = skb_checksum_start(skb) - skb_network_header(skb);
5030 if (unlikely(network_hdr_len > IGC_MAX_NETWORK_HDR_LEN))
5031 return features & ~(NETIF_F_HW_CSUM |
5036 /* We can only support IPv4 TSO in tunnels if we can mangle the
5037 * inner IP ID field, so strip TSO if MANGLEID is not supported.
5039 if (skb->encapsulation && !(features & NETIF_F_TSO_MANGLEID))
5040 features &= ~NETIF_F_TSO;
5045 static void igc_tsync_interrupt(struct igc_adapter *adapter)
5047 u32 ack, tsauxc, sec, nsec, tsicr;
5048 struct igc_hw *hw = &adapter->hw;
5049 struct ptp_clock_event event;
5050 struct timespec64 ts;
5052 tsicr = rd32(IGC_TSICR);
5055 if (tsicr & IGC_TSICR_SYS_WRAP) {
5056 event.type = PTP_CLOCK_PPS;
5057 if (adapter->ptp_caps.pps)
5058 ptp_clock_event(adapter->ptp_clock, &event);
5059 ack |= IGC_TSICR_SYS_WRAP;
5062 if (tsicr & IGC_TSICR_TXTS) {
5063 /* retrieve hardware timestamp */
5064 schedule_work(&adapter->ptp_tx_work);
5065 ack |= IGC_TSICR_TXTS;
5068 if (tsicr & IGC_TSICR_TT0) {
5069 spin_lock(&adapter->tmreg_lock);
5070 ts = timespec64_add(adapter->perout[0].start,
5071 adapter->perout[0].period);
5072 wr32(IGC_TRGTTIML0, ts.tv_nsec | IGC_TT_IO_TIMER_SEL_SYSTIM0);
5073 wr32(IGC_TRGTTIMH0, (u32)ts.tv_sec);
5074 tsauxc = rd32(IGC_TSAUXC);
5075 tsauxc |= IGC_TSAUXC_EN_TT0;
5076 wr32(IGC_TSAUXC, tsauxc);
5077 adapter->perout[0].start = ts;
5078 spin_unlock(&adapter->tmreg_lock);
5079 ack |= IGC_TSICR_TT0;
5082 if (tsicr & IGC_TSICR_TT1) {
5083 spin_lock(&adapter->tmreg_lock);
5084 ts = timespec64_add(adapter->perout[1].start,
5085 adapter->perout[1].period);
5086 wr32(IGC_TRGTTIML1, ts.tv_nsec | IGC_TT_IO_TIMER_SEL_SYSTIM0);
5087 wr32(IGC_TRGTTIMH1, (u32)ts.tv_sec);
5088 tsauxc = rd32(IGC_TSAUXC);
5089 tsauxc |= IGC_TSAUXC_EN_TT1;
5090 wr32(IGC_TSAUXC, tsauxc);
5091 adapter->perout[1].start = ts;
5092 spin_unlock(&adapter->tmreg_lock);
5093 ack |= IGC_TSICR_TT1;
5096 if (tsicr & IGC_TSICR_AUTT0) {
5097 nsec = rd32(IGC_AUXSTMPL0);
5098 sec = rd32(IGC_AUXSTMPH0);
5099 event.type = PTP_CLOCK_EXTTS;
5101 event.timestamp = sec * NSEC_PER_SEC + nsec;
5102 ptp_clock_event(adapter->ptp_clock, &event);
5103 ack |= IGC_TSICR_AUTT0;
5106 if (tsicr & IGC_TSICR_AUTT1) {
5107 nsec = rd32(IGC_AUXSTMPL1);
5108 sec = rd32(IGC_AUXSTMPH1);
5109 event.type = PTP_CLOCK_EXTTS;
5111 event.timestamp = sec * NSEC_PER_SEC + nsec;
5112 ptp_clock_event(adapter->ptp_clock, &event);
5113 ack |= IGC_TSICR_AUTT1;
5116 /* acknowledge the interrupts */
5117 wr32(IGC_TSICR, ack);
5121 * igc_msix_other - msix other interrupt handler
5122 * @irq: interrupt number
5123 * @data: pointer to a q_vector
5125 static irqreturn_t igc_msix_other(int irq, void *data)
5127 struct igc_adapter *adapter = data;
5128 struct igc_hw *hw = &adapter->hw;
5129 u32 icr = rd32(IGC_ICR);
5131 /* reading ICR causes bit 31 of EICR to be cleared */
5132 if (icr & IGC_ICR_DRSTA)
5133 schedule_work(&adapter->reset_task);
5135 if (icr & IGC_ICR_DOUTSYNC) {
5136 /* HW is reporting DMA is out of sync */
5137 adapter->stats.doosync++;
5140 if (icr & IGC_ICR_LSC) {
5141 hw->mac.get_link_status = true;
5142 /* guard against interrupt when we're going down */
5143 if (!test_bit(__IGC_DOWN, &adapter->state))
5144 mod_timer(&adapter->watchdog_timer, jiffies + 1);
5147 if (icr & IGC_ICR_TS)
5148 igc_tsync_interrupt(adapter);
5150 wr32(IGC_EIMS, adapter->eims_other);
5155 static void igc_write_itr(struct igc_q_vector *q_vector)
5157 u32 itr_val = q_vector->itr_val & IGC_QVECTOR_MASK;
5159 if (!q_vector->set_itr)
5163 itr_val = IGC_ITR_VAL_MASK;
5165 itr_val |= IGC_EITR_CNT_IGNR;
5167 writel(itr_val, q_vector->itr_register);
5168 q_vector->set_itr = 0;
5171 static irqreturn_t igc_msix_ring(int irq, void *data)
5173 struct igc_q_vector *q_vector = data;
5175 /* Write the ITR value calculated from the previous interrupt. */
5176 igc_write_itr(q_vector);
5178 napi_schedule(&q_vector->napi);
5184 * igc_request_msix - Initialize MSI-X interrupts
5185 * @adapter: Pointer to adapter structure
5187 * igc_request_msix allocates MSI-X vectors and requests interrupts from the
5190 static int igc_request_msix(struct igc_adapter *adapter)
5192 unsigned int num_q_vectors = adapter->num_q_vectors;
5193 int i = 0, err = 0, vector = 0, free_vector = 0;
5194 struct net_device *netdev = adapter->netdev;
5196 err = request_irq(adapter->msix_entries[vector].vector,
5197 &igc_msix_other, 0, netdev->name, adapter);
5201 if (num_q_vectors > MAX_Q_VECTORS) {
5202 num_q_vectors = MAX_Q_VECTORS;
5203 dev_warn(&adapter->pdev->dev,
5204 "The number of queue vectors (%d) is higher than max allowed (%d)\n",
5205 adapter->num_q_vectors, MAX_Q_VECTORS);
5207 for (i = 0; i < num_q_vectors; i++) {
5208 struct igc_q_vector *q_vector = adapter->q_vector[i];
5212 q_vector->itr_register = adapter->io_addr + IGC_EITR(vector);
5214 if (q_vector->rx.ring && q_vector->tx.ring)
5215 sprintf(q_vector->name, "%s-TxRx-%u", netdev->name,
5216 q_vector->rx.ring->queue_index);
5217 else if (q_vector->tx.ring)
5218 sprintf(q_vector->name, "%s-tx-%u", netdev->name,
5219 q_vector->tx.ring->queue_index);
5220 else if (q_vector->rx.ring)
5221 sprintf(q_vector->name, "%s-rx-%u", netdev->name,
5222 q_vector->rx.ring->queue_index);
5224 sprintf(q_vector->name, "%s-unused", netdev->name);
5226 err = request_irq(adapter->msix_entries[vector].vector,
5227 igc_msix_ring, 0, q_vector->name,
5233 igc_configure_msix(adapter);
5237 /* free already assigned IRQs */
5238 free_irq(adapter->msix_entries[free_vector++].vector, adapter);
5241 for (i = 0; i < vector; i++) {
5242 free_irq(adapter->msix_entries[free_vector++].vector,
5243 adapter->q_vector[i]);
5250 * igc_clear_interrupt_scheme - reset the device to a state of no interrupts
5251 * @adapter: Pointer to adapter structure
5253 * This function resets the device so that it has 0 rx queues, tx queues, and
5254 * MSI-X interrupts allocated.
5256 static void igc_clear_interrupt_scheme(struct igc_adapter *adapter)
5258 igc_free_q_vectors(adapter);
5259 igc_reset_interrupt_capability(adapter);
5262 /* Need to wait a few seconds after link up to get diagnostic information from
5265 static void igc_update_phy_info(struct timer_list *t)
5267 struct igc_adapter *adapter = from_timer(adapter, t, phy_info_timer);
5269 igc_get_phy_info(&adapter->hw);
5273 * igc_has_link - check shared code for link and determine up/down
5274 * @adapter: pointer to driver private info
5276 bool igc_has_link(struct igc_adapter *adapter)
5278 struct igc_hw *hw = &adapter->hw;
5279 bool link_active = false;
5281 /* get_link_status is set on LSC (link status) interrupt or
5282 * rx sequence error interrupt. get_link_status will stay
5283 * false until the igc_check_for_link establishes link
5284 * for copper adapters ONLY
5286 if (!hw->mac.get_link_status)
5288 hw->mac.ops.check_for_link(hw);
5289 link_active = !hw->mac.get_link_status;
5291 if (hw->mac.type == igc_i225) {
5292 if (!netif_carrier_ok(adapter->netdev)) {
5293 adapter->flags &= ~IGC_FLAG_NEED_LINK_UPDATE;
5294 } else if (!(adapter->flags & IGC_FLAG_NEED_LINK_UPDATE)) {
5295 adapter->flags |= IGC_FLAG_NEED_LINK_UPDATE;
5296 adapter->link_check_timeout = jiffies;
5304 * igc_watchdog - Timer Call-back
5305 * @t: timer for the watchdog
5307 static void igc_watchdog(struct timer_list *t)
5309 struct igc_adapter *adapter = from_timer(adapter, t, watchdog_timer);
5310 /* Do the rest outside of interrupt context */
5311 schedule_work(&adapter->watchdog_task);
5314 static void igc_watchdog_task(struct work_struct *work)
5316 struct igc_adapter *adapter = container_of(work,
5319 struct net_device *netdev = adapter->netdev;
5320 struct igc_hw *hw = &adapter->hw;
5321 struct igc_phy_info *phy = &hw->phy;
5322 u16 phy_data, retry_count = 20;
5326 link = igc_has_link(adapter);
5328 if (adapter->flags & IGC_FLAG_NEED_LINK_UPDATE) {
5329 if (time_after(jiffies, (adapter->link_check_timeout + HZ)))
5330 adapter->flags &= ~IGC_FLAG_NEED_LINK_UPDATE;
5336 /* Cancel scheduled suspend requests. */
5337 pm_runtime_resume(netdev->dev.parent);
5339 if (!netif_carrier_ok(netdev)) {
5342 hw->mac.ops.get_speed_and_duplex(hw,
5343 &adapter->link_speed,
5344 &adapter->link_duplex);
5346 ctrl = rd32(IGC_CTRL);
5347 /* Link status message must follow this format */
5349 "NIC Link is Up %d Mbps %s Duplex, Flow Control: %s\n",
5350 adapter->link_speed,
5351 adapter->link_duplex == FULL_DUPLEX ?
5353 (ctrl & IGC_CTRL_TFCE) &&
5354 (ctrl & IGC_CTRL_RFCE) ? "RX/TX" :
5355 (ctrl & IGC_CTRL_RFCE) ? "RX" :
5356 (ctrl & IGC_CTRL_TFCE) ? "TX" : "None");
5358 /* disable EEE if enabled */
5359 if ((adapter->flags & IGC_FLAG_EEE) &&
5360 adapter->link_duplex == HALF_DUPLEX) {
5362 "EEE Disabled: unsupported at half duplex. Re-enable using ethtool when at full duplex\n");
5363 adapter->hw.dev_spec._base.eee_enable = false;
5364 adapter->flags &= ~IGC_FLAG_EEE;
5367 /* check if SmartSpeed worked */
5368 igc_check_downshift(hw);
5369 if (phy->speed_downgraded)
5370 netdev_warn(netdev, "Link Speed was downgraded by SmartSpeed\n");
5372 /* adjust timeout factor according to speed/duplex */
5373 adapter->tx_timeout_factor = 1;
5374 switch (adapter->link_speed) {
5376 adapter->tx_timeout_factor = 14;
5381 adapter->tx_timeout_factor = 7;
5385 if (adapter->link_speed != SPEED_1000)
5388 /* wait for Remote receiver status OK */
5390 if (!igc_read_phy_reg(hw, PHY_1000T_STATUS,
5392 if (!(phy_data & SR_1000T_REMOTE_RX_STATUS) &&
5396 goto retry_read_status;
5397 } else if (!retry_count) {
5398 netdev_err(netdev, "exceed max 2 second\n");
5401 netdev_err(netdev, "read 1000Base-T Status Reg\n");
5404 netif_carrier_on(netdev);
5406 /* link state has changed, schedule phy info update */
5407 if (!test_bit(__IGC_DOWN, &adapter->state))
5408 mod_timer(&adapter->phy_info_timer,
5409 round_jiffies(jiffies + 2 * HZ));
5412 if (netif_carrier_ok(netdev)) {
5413 adapter->link_speed = 0;
5414 adapter->link_duplex = 0;
5416 /* Links status message must follow this format */
5417 netdev_info(netdev, "NIC Link is Down\n");
5418 netif_carrier_off(netdev);
5420 /* link state has changed, schedule phy info update */
5421 if (!test_bit(__IGC_DOWN, &adapter->state))
5422 mod_timer(&adapter->phy_info_timer,
5423 round_jiffies(jiffies + 2 * HZ));
5425 /* link is down, time to check for alternate media */
5426 if (adapter->flags & IGC_FLAG_MAS_ENABLE) {
5427 if (adapter->flags & IGC_FLAG_MEDIA_RESET) {
5428 schedule_work(&adapter->reset_task);
5429 /* return immediately */
5433 pm_schedule_suspend(netdev->dev.parent,
5436 /* also check for alternate media here */
5437 } else if (!netif_carrier_ok(netdev) &&
5438 (adapter->flags & IGC_FLAG_MAS_ENABLE)) {
5439 if (adapter->flags & IGC_FLAG_MEDIA_RESET) {
5440 schedule_work(&adapter->reset_task);
5441 /* return immediately */
5447 spin_lock(&adapter->stats64_lock);
5448 igc_update_stats(adapter);
5449 spin_unlock(&adapter->stats64_lock);
5451 for (i = 0; i < adapter->num_tx_queues; i++) {
5452 struct igc_ring *tx_ring = adapter->tx_ring[i];
5454 if (!netif_carrier_ok(netdev)) {
5455 /* We've lost link, so the controller stops DMA,
5456 * but we've got queued Tx work that's never going
5457 * to get done, so reset controller to flush Tx.
5458 * (Do the reset outside of interrupt context).
5460 if (igc_desc_unused(tx_ring) + 1 < tx_ring->count) {
5461 adapter->tx_timeout_count++;
5462 schedule_work(&adapter->reset_task);
5463 /* return immediately since reset is imminent */
5468 /* Force detection of hung controller every watchdog period */
5469 set_bit(IGC_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags);
5472 /* Cause software interrupt to ensure Rx ring is cleaned */
5473 if (adapter->flags & IGC_FLAG_HAS_MSIX) {
5476 for (i = 0; i < adapter->num_q_vectors; i++)
5477 eics |= adapter->q_vector[i]->eims_value;
5478 wr32(IGC_EICS, eics);
5480 wr32(IGC_ICS, IGC_ICS_RXDMT0);
5483 igc_ptp_tx_hang(adapter);
5485 /* Reset the timer */
5486 if (!test_bit(__IGC_DOWN, &adapter->state)) {
5487 if (adapter->flags & IGC_FLAG_NEED_LINK_UPDATE)
5488 mod_timer(&adapter->watchdog_timer,
5489 round_jiffies(jiffies + HZ));
5491 mod_timer(&adapter->watchdog_timer,
5492 round_jiffies(jiffies + 2 * HZ));
5497 * igc_intr_msi - Interrupt Handler
5498 * @irq: interrupt number
5499 * @data: pointer to a network interface device structure
5501 static irqreturn_t igc_intr_msi(int irq, void *data)
5503 struct igc_adapter *adapter = data;
5504 struct igc_q_vector *q_vector = adapter->q_vector[0];
5505 struct igc_hw *hw = &adapter->hw;
5506 /* read ICR disables interrupts using IAM */
5507 u32 icr = rd32(IGC_ICR);
5509 igc_write_itr(q_vector);
5511 if (icr & IGC_ICR_DRSTA)
5512 schedule_work(&adapter->reset_task);
5514 if (icr & IGC_ICR_DOUTSYNC) {
5515 /* HW is reporting DMA is out of sync */
5516 adapter->stats.doosync++;
5519 if (icr & (IGC_ICR_RXSEQ | IGC_ICR_LSC)) {
5520 hw->mac.get_link_status = true;
5521 if (!test_bit(__IGC_DOWN, &adapter->state))
5522 mod_timer(&adapter->watchdog_timer, jiffies + 1);
5525 if (icr & IGC_ICR_TS)
5526 igc_tsync_interrupt(adapter);
5528 napi_schedule(&q_vector->napi);
5534 * igc_intr - Legacy Interrupt Handler
5535 * @irq: interrupt number
5536 * @data: pointer to a network interface device structure
5538 static irqreturn_t igc_intr(int irq, void *data)
5540 struct igc_adapter *adapter = data;
5541 struct igc_q_vector *q_vector = adapter->q_vector[0];
5542 struct igc_hw *hw = &adapter->hw;
5543 /* Interrupt Auto-Mask...upon reading ICR, interrupts are masked. No
5544 * need for the IMC write
5546 u32 icr = rd32(IGC_ICR);
5548 /* IMS will not auto-mask if INT_ASSERTED is not set, and if it is
5549 * not set, then the adapter didn't send an interrupt
5551 if (!(icr & IGC_ICR_INT_ASSERTED))
5554 igc_write_itr(q_vector);
5556 if (icr & IGC_ICR_DRSTA)
5557 schedule_work(&adapter->reset_task);
5559 if (icr & IGC_ICR_DOUTSYNC) {
5560 /* HW is reporting DMA is out of sync */
5561 adapter->stats.doosync++;
5564 if (icr & (IGC_ICR_RXSEQ | IGC_ICR_LSC)) {
5565 hw->mac.get_link_status = true;
5566 /* guard against interrupt when we're going down */
5567 if (!test_bit(__IGC_DOWN, &adapter->state))
5568 mod_timer(&adapter->watchdog_timer, jiffies + 1);
5571 if (icr & IGC_ICR_TS)
5572 igc_tsync_interrupt(adapter);
5574 napi_schedule(&q_vector->napi);
5579 static void igc_free_irq(struct igc_adapter *adapter)
5581 if (adapter->msix_entries) {
5584 free_irq(adapter->msix_entries[vector++].vector, adapter);
5586 for (i = 0; i < adapter->num_q_vectors; i++)
5587 free_irq(adapter->msix_entries[vector++].vector,
5588 adapter->q_vector[i]);
5590 free_irq(adapter->pdev->irq, adapter);
5595 * igc_request_irq - initialize interrupts
5596 * @adapter: Pointer to adapter structure
5598 * Attempts to configure interrupts using the best available
5599 * capabilities of the hardware and kernel.
5601 static int igc_request_irq(struct igc_adapter *adapter)
5603 struct net_device *netdev = adapter->netdev;
5604 struct pci_dev *pdev = adapter->pdev;
5607 if (adapter->flags & IGC_FLAG_HAS_MSIX) {
5608 err = igc_request_msix(adapter);
5611 /* fall back to MSI */
5612 igc_free_all_tx_resources(adapter);
5613 igc_free_all_rx_resources(adapter);
5615 igc_clear_interrupt_scheme(adapter);
5616 err = igc_init_interrupt_scheme(adapter, false);
5619 igc_setup_all_tx_resources(adapter);
5620 igc_setup_all_rx_resources(adapter);
5621 igc_configure(adapter);
5624 igc_assign_vector(adapter->q_vector[0], 0);
5626 if (adapter->flags & IGC_FLAG_HAS_MSI) {
5627 err = request_irq(pdev->irq, &igc_intr_msi, 0,
5628 netdev->name, adapter);
5632 /* fall back to legacy interrupts */
5633 igc_reset_interrupt_capability(adapter);
5634 adapter->flags &= ~IGC_FLAG_HAS_MSI;
5637 err = request_irq(pdev->irq, &igc_intr, IRQF_SHARED,
5638 netdev->name, adapter);
5641 netdev_err(netdev, "Error %d getting interrupt\n", err);
5648 * __igc_open - Called when a network interface is made active
5649 * @netdev: network interface device structure
5650 * @resuming: boolean indicating if the device is resuming
5652 * Returns 0 on success, negative value on failure
5654 * The open entry point is called when a network interface is made
5655 * active by the system (IFF_UP). At this point all resources needed
5656 * for transmit and receive operations are allocated, the interrupt
5657 * handler is registered with the OS, the watchdog timer is started,
5658 * and the stack is notified that the interface is ready.
5660 static int __igc_open(struct net_device *netdev, bool resuming)
5662 struct igc_adapter *adapter = netdev_priv(netdev);
5663 struct pci_dev *pdev = adapter->pdev;
5664 struct igc_hw *hw = &adapter->hw;
5668 /* disallow open during test */
5670 if (test_bit(__IGC_TESTING, &adapter->state)) {
5676 pm_runtime_get_sync(&pdev->dev);
5678 netif_carrier_off(netdev);
5680 /* allocate transmit descriptors */
5681 err = igc_setup_all_tx_resources(adapter);
5685 /* allocate receive descriptors */
5686 err = igc_setup_all_rx_resources(adapter);
5690 igc_power_up_link(adapter);
5692 igc_configure(adapter);
5694 err = igc_request_irq(adapter);
5698 /* Notify the stack of the actual queue counts. */
5699 err = netif_set_real_num_tx_queues(netdev, adapter->num_tx_queues);
5701 goto err_set_queues;
5703 err = netif_set_real_num_rx_queues(netdev, adapter->num_rx_queues);
5705 goto err_set_queues;
5707 clear_bit(__IGC_DOWN, &adapter->state);
5709 for (i = 0; i < adapter->num_q_vectors; i++)
5710 napi_enable(&adapter->q_vector[i]->napi);
5712 /* Clear any pending interrupts. */
5714 igc_irq_enable(adapter);
5717 pm_runtime_put(&pdev->dev);
5719 netif_tx_start_all_queues(netdev);
5721 /* start the watchdog. */
5722 hw->mac.get_link_status = true;
5723 schedule_work(&adapter->watchdog_task);
5728 igc_free_irq(adapter);
5730 igc_release_hw_control(adapter);
5731 igc_power_down_phy_copper_base(&adapter->hw);
5732 igc_free_all_rx_resources(adapter);
5734 igc_free_all_tx_resources(adapter);
5738 pm_runtime_put(&pdev->dev);
5743 int igc_open(struct net_device *netdev)
5745 return __igc_open(netdev, false);
5749 * __igc_close - Disables a network interface
5750 * @netdev: network interface device structure
5751 * @suspending: boolean indicating the device is suspending
5753 * Returns 0, this is not allowed to fail
5755 * The close entry point is called when an interface is de-activated
5756 * by the OS. The hardware is still under the driver's control, but
5757 * needs to be disabled. A global MAC reset is issued to stop the
5758 * hardware, and all transmit and receive resources are freed.
5760 static int __igc_close(struct net_device *netdev, bool suspending)
5762 struct igc_adapter *adapter = netdev_priv(netdev);
5763 struct pci_dev *pdev = adapter->pdev;
5765 WARN_ON(test_bit(__IGC_RESETTING, &adapter->state));
5768 pm_runtime_get_sync(&pdev->dev);
5772 igc_release_hw_control(adapter);
5774 igc_free_irq(adapter);
5776 igc_free_all_tx_resources(adapter);
5777 igc_free_all_rx_resources(adapter);
5780 pm_runtime_put_sync(&pdev->dev);
5785 int igc_close(struct net_device *netdev)
5787 if (netif_device_present(netdev) || netdev->dismantle)
5788 return __igc_close(netdev, false);
5793 * igc_ioctl - Access the hwtstamp interface
5794 * @netdev: network interface device structure
5795 * @ifr: interface request data
5796 * @cmd: ioctl command
5798 static int igc_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
5802 return igc_ptp_get_ts_config(netdev, ifr);
5804 return igc_ptp_set_ts_config(netdev, ifr);
5810 static int igc_save_launchtime_params(struct igc_adapter *adapter, int queue,
5813 struct igc_ring *ring;
5815 if (queue < 0 || queue >= adapter->num_tx_queues)
5818 ring = adapter->tx_ring[queue];
5819 ring->launchtime_enable = enable;
5824 static bool is_base_time_past(ktime_t base_time, const struct timespec64 *now)
5826 struct timespec64 b;
5828 b = ktime_to_timespec64(base_time);
5830 return timespec64_compare(now, &b) > 0;
5833 static bool validate_schedule(struct igc_adapter *adapter,
5834 const struct tc_taprio_qopt_offload *qopt)
5836 int queue_uses[IGC_MAX_TX_QUEUES] = { };
5837 struct timespec64 now;
5840 if (qopt->cycle_time_extension)
5843 igc_ptp_read(adapter, &now);
5845 /* If we program the controller's BASET registers with a time
5846 * in the future, it will hold all the packets until that
5847 * time, causing a lot of TX Hangs, so to avoid that, we
5848 * reject schedules that would start in the future.
5850 if (!is_base_time_past(qopt->base_time, &now))
5853 for (n = 0; n < qopt->num_entries; n++) {
5854 const struct tc_taprio_sched_entry *e, *prev;
5857 prev = n ? &qopt->entries[n - 1] : NULL;
5858 e = &qopt->entries[n];
5860 /* i225 only supports "global" frame preemption
5863 if (e->command != TC_TAPRIO_CMD_SET_GATES)
5866 for (i = 0; i < adapter->num_tx_queues; i++) {
5867 if (e->gate_mask & BIT(i))
5870 /* There are limitations: A single queue cannot be
5871 * opened and closed multiple times per cycle unless the
5872 * gate stays open. Check for it.
5874 if (queue_uses[i] > 1 &&
5875 !(prev->gate_mask & BIT(i)))
5883 static int igc_tsn_enable_launchtime(struct igc_adapter *adapter,
5884 struct tc_etf_qopt_offload *qopt)
5886 struct igc_hw *hw = &adapter->hw;
5889 if (hw->mac.type != igc_i225)
5892 err = igc_save_launchtime_params(adapter, qopt->queue, qopt->enable);
5896 return igc_tsn_offload_apply(adapter);
5899 static int igc_tsn_clear_schedule(struct igc_adapter *adapter)
5903 adapter->base_time = 0;
5904 adapter->cycle_time = NSEC_PER_SEC;
5906 for (i = 0; i < adapter->num_tx_queues; i++) {
5907 struct igc_ring *ring = adapter->tx_ring[i];
5909 ring->start_time = 0;
5910 ring->end_time = NSEC_PER_SEC;
5916 static int igc_save_qbv_schedule(struct igc_adapter *adapter,
5917 struct tc_taprio_qopt_offload *qopt)
5919 bool queue_configured[IGC_MAX_TX_QUEUES] = { };
5920 u32 start_time = 0, end_time = 0;
5924 return igc_tsn_clear_schedule(adapter);
5926 if (adapter->base_time)
5929 if (!validate_schedule(adapter, qopt))
5932 adapter->cycle_time = qopt->cycle_time;
5933 adapter->base_time = qopt->base_time;
5935 for (n = 0; n < qopt->num_entries; n++) {
5936 struct tc_taprio_sched_entry *e = &qopt->entries[n];
5939 end_time += e->interval;
5941 for (i = 0; i < adapter->num_tx_queues; i++) {
5942 struct igc_ring *ring = adapter->tx_ring[i];
5944 if (!(e->gate_mask & BIT(i)))
5947 /* Check whether a queue stays open for more than one
5948 * entry. If so, keep the start and advance the end
5951 if (!queue_configured[i])
5952 ring->start_time = start_time;
5953 ring->end_time = end_time;
5955 queue_configured[i] = true;
5958 start_time += e->interval;
5964 static int igc_tsn_enable_qbv_scheduling(struct igc_adapter *adapter,
5965 struct tc_taprio_qopt_offload *qopt)
5967 struct igc_hw *hw = &adapter->hw;
5970 if (hw->mac.type != igc_i225)
5973 err = igc_save_qbv_schedule(adapter, qopt);
5977 return igc_tsn_offload_apply(adapter);
5980 static int igc_save_cbs_params(struct igc_adapter *adapter, int queue,
5981 bool enable, int idleslope, int sendslope,
5982 int hicredit, int locredit)
5984 bool cbs_status[IGC_MAX_SR_QUEUES] = { false };
5985 struct net_device *netdev = adapter->netdev;
5986 struct igc_ring *ring;
5989 /* i225 has two sets of credit-based shaper logic.
5990 * Supporting it only on the top two priority queues
5992 if (queue < 0 || queue > 1)
5995 ring = adapter->tx_ring[queue];
5997 for (i = 0; i < IGC_MAX_SR_QUEUES; i++)
5998 if (adapter->tx_ring[i])
5999 cbs_status[i] = adapter->tx_ring[i]->cbs_enable;
6001 /* CBS should be enabled on the highest priority queue first in order
6002 * for the CBS algorithm to operate as intended.
6005 if (queue == 1 && !cbs_status[0]) {
6007 "Enabling CBS on queue1 before queue0\n");
6011 if (queue == 0 && cbs_status[1]) {
6013 "Disabling CBS on queue0 before queue1\n");
6018 ring->cbs_enable = enable;
6019 ring->idleslope = idleslope;
6020 ring->sendslope = sendslope;
6021 ring->hicredit = hicredit;
6022 ring->locredit = locredit;
6027 static int igc_tsn_enable_cbs(struct igc_adapter *adapter,
6028 struct tc_cbs_qopt_offload *qopt)
6030 struct igc_hw *hw = &adapter->hw;
6033 if (hw->mac.type != igc_i225)
6036 if (qopt->queue < 0 || qopt->queue > 1)
6039 err = igc_save_cbs_params(adapter, qopt->queue, qopt->enable,
6040 qopt->idleslope, qopt->sendslope,
6041 qopt->hicredit, qopt->locredit);
6045 return igc_tsn_offload_apply(adapter);
6048 static int igc_setup_tc(struct net_device *dev, enum tc_setup_type type,
6051 struct igc_adapter *adapter = netdev_priv(dev);
6054 case TC_SETUP_QDISC_TAPRIO:
6055 return igc_tsn_enable_qbv_scheduling(adapter, type_data);
6057 case TC_SETUP_QDISC_ETF:
6058 return igc_tsn_enable_launchtime(adapter, type_data);
6060 case TC_SETUP_QDISC_CBS:
6061 return igc_tsn_enable_cbs(adapter, type_data);
6068 static int igc_bpf(struct net_device *dev, struct netdev_bpf *bpf)
6070 struct igc_adapter *adapter = netdev_priv(dev);
6072 switch (bpf->command) {
6073 case XDP_SETUP_PROG:
6074 return igc_xdp_set_prog(adapter, bpf->prog, bpf->extack);
6075 case XDP_SETUP_XSK_POOL:
6076 return igc_xdp_setup_pool(adapter, bpf->xsk.pool,
6083 static int igc_xdp_xmit(struct net_device *dev, int num_frames,
6084 struct xdp_frame **frames, u32 flags)
6086 struct igc_adapter *adapter = netdev_priv(dev);
6087 int cpu = smp_processor_id();
6088 struct netdev_queue *nq;
6089 struct igc_ring *ring;
6092 if (unlikely(test_bit(__IGC_DOWN, &adapter->state)))
6095 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
6098 ring = igc_xdp_get_tx_ring(adapter, cpu);
6099 nq = txring_txq(ring);
6101 __netif_tx_lock(nq, cpu);
6104 for (i = 0; i < num_frames; i++) {
6106 struct xdp_frame *xdpf = frames[i];
6108 err = igc_xdp_init_tx_descriptor(ring, xdpf);
6110 xdp_return_frame_rx_napi(xdpf);
6115 if (flags & XDP_XMIT_FLUSH)
6116 igc_flush_tx_descriptors(ring);
6118 __netif_tx_unlock(nq);
6120 return num_frames - drops;
6123 static void igc_trigger_rxtxq_interrupt(struct igc_adapter *adapter,
6124 struct igc_q_vector *q_vector)
6126 struct igc_hw *hw = &adapter->hw;
6129 eics |= q_vector->eims_value;
6130 wr32(IGC_EICS, eics);
6133 int igc_xsk_wakeup(struct net_device *dev, u32 queue_id, u32 flags)
6135 struct igc_adapter *adapter = netdev_priv(dev);
6136 struct igc_q_vector *q_vector;
6137 struct igc_ring *ring;
6139 if (test_bit(__IGC_DOWN, &adapter->state))
6142 if (!igc_xdp_is_enabled(adapter))
6145 if (queue_id >= adapter->num_rx_queues)
6148 ring = adapter->rx_ring[queue_id];
6150 if (!ring->xsk_pool)
6153 q_vector = adapter->q_vector[queue_id];
6154 if (!napi_if_scheduled_mark_missed(&q_vector->napi))
6155 igc_trigger_rxtxq_interrupt(adapter, q_vector);
6160 static const struct net_device_ops igc_netdev_ops = {
6161 .ndo_open = igc_open,
6162 .ndo_stop = igc_close,
6163 .ndo_start_xmit = igc_xmit_frame,
6164 .ndo_set_rx_mode = igc_set_rx_mode,
6165 .ndo_set_mac_address = igc_set_mac,
6166 .ndo_change_mtu = igc_change_mtu,
6167 .ndo_get_stats64 = igc_get_stats64,
6168 .ndo_fix_features = igc_fix_features,
6169 .ndo_set_features = igc_set_features,
6170 .ndo_features_check = igc_features_check,
6171 .ndo_eth_ioctl = igc_ioctl,
6172 .ndo_setup_tc = igc_setup_tc,
6174 .ndo_xdp_xmit = igc_xdp_xmit,
6175 .ndo_xsk_wakeup = igc_xsk_wakeup,
6178 /* PCIe configuration access */
6179 void igc_read_pci_cfg(struct igc_hw *hw, u32 reg, u16 *value)
6181 struct igc_adapter *adapter = hw->back;
6183 pci_read_config_word(adapter->pdev, reg, value);
6186 void igc_write_pci_cfg(struct igc_hw *hw, u32 reg, u16 *value)
6188 struct igc_adapter *adapter = hw->back;
6190 pci_write_config_word(adapter->pdev, reg, *value);
6193 s32 igc_read_pcie_cap_reg(struct igc_hw *hw, u32 reg, u16 *value)
6195 struct igc_adapter *adapter = hw->back;
6197 if (!pci_is_pcie(adapter->pdev))
6198 return -IGC_ERR_CONFIG;
6200 pcie_capability_read_word(adapter->pdev, reg, value);
6205 s32 igc_write_pcie_cap_reg(struct igc_hw *hw, u32 reg, u16 *value)
6207 struct igc_adapter *adapter = hw->back;
6209 if (!pci_is_pcie(adapter->pdev))
6210 return -IGC_ERR_CONFIG;
6212 pcie_capability_write_word(adapter->pdev, reg, *value);
6217 u32 igc_rd32(struct igc_hw *hw, u32 reg)
6219 struct igc_adapter *igc = container_of(hw, struct igc_adapter, hw);
6220 u8 __iomem *hw_addr = READ_ONCE(hw->hw_addr);
6223 if (IGC_REMOVED(hw_addr))
6226 value = readl(&hw_addr[reg]);
6228 /* reads should not return all F's */
6229 if (!(~value) && (!reg || !(~readl(hw_addr)))) {
6230 struct net_device *netdev = igc->netdev;
6233 netif_device_detach(netdev);
6234 netdev_err(netdev, "PCIe link lost, device now detached\n");
6235 WARN(pci_device_is_present(igc->pdev),
6236 "igc: Failed to read reg 0x%x!\n", reg);
6243 * igc_probe - Device Initialization Routine
6244 * @pdev: PCI device information struct
6245 * @ent: entry in igc_pci_tbl
6247 * Returns 0 on success, negative on failure
6249 * igc_probe initializes an adapter identified by a pci_dev structure.
6250 * The OS initialization, configuring the adapter private structure,
6251 * and a hardware reset occur.
6253 static int igc_probe(struct pci_dev *pdev,
6254 const struct pci_device_id *ent)
6256 struct igc_adapter *adapter;
6257 struct net_device *netdev;
6259 const struct igc_info *ei = igc_info_tbl[ent->driver_data];
6262 err = pci_enable_device_mem(pdev);
6266 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
6269 "No usable DMA configuration, aborting\n");
6273 err = pci_request_mem_regions(pdev, igc_driver_name);
6277 pci_enable_pcie_error_reporting(pdev);
6279 err = pci_enable_ptm(pdev, NULL);
6281 dev_info(&pdev->dev, "PCIe PTM not supported by PCIe bus/controller\n");
6283 pci_set_master(pdev);
6286 netdev = alloc_etherdev_mq(sizeof(struct igc_adapter),
6290 goto err_alloc_etherdev;
6292 SET_NETDEV_DEV(netdev, &pdev->dev);
6294 pci_set_drvdata(pdev, netdev);
6295 adapter = netdev_priv(netdev);
6296 adapter->netdev = netdev;
6297 adapter->pdev = pdev;
6300 adapter->port_num = hw->bus.func;
6301 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
6303 err = pci_save_state(pdev);
6308 adapter->io_addr = ioremap(pci_resource_start(pdev, 0),
6309 pci_resource_len(pdev, 0));
6310 if (!adapter->io_addr)
6313 /* hw->hw_addr can be zeroed, so use adapter->io_addr for unmap */
6314 hw->hw_addr = adapter->io_addr;
6316 netdev->netdev_ops = &igc_netdev_ops;
6317 igc_ethtool_set_ops(netdev);
6318 netdev->watchdog_timeo = 5 * HZ;
6320 netdev->mem_start = pci_resource_start(pdev, 0);
6321 netdev->mem_end = pci_resource_end(pdev, 0);
6323 /* PCI config space info */
6324 hw->vendor_id = pdev->vendor;
6325 hw->device_id = pdev->device;
6326 hw->revision_id = pdev->revision;
6327 hw->subsystem_vendor_id = pdev->subsystem_vendor;
6328 hw->subsystem_device_id = pdev->subsystem_device;
6330 /* Copy the default MAC and PHY function pointers */
6331 memcpy(&hw->mac.ops, ei->mac_ops, sizeof(hw->mac.ops));
6332 memcpy(&hw->phy.ops, ei->phy_ops, sizeof(hw->phy.ops));
6334 /* Initialize skew-specific constants */
6335 err = ei->get_invariants(hw);
6339 /* Add supported features to the features list*/
6340 netdev->features |= NETIF_F_SG;
6341 netdev->features |= NETIF_F_TSO;
6342 netdev->features |= NETIF_F_TSO6;
6343 netdev->features |= NETIF_F_TSO_ECN;
6344 netdev->features |= NETIF_F_RXCSUM;
6345 netdev->features |= NETIF_F_HW_CSUM;
6346 netdev->features |= NETIF_F_SCTP_CRC;
6347 netdev->features |= NETIF_F_HW_TC;
6349 #define IGC_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE | \
6350 NETIF_F_GSO_GRE_CSUM | \
6351 NETIF_F_GSO_IPXIP4 | \
6352 NETIF_F_GSO_IPXIP6 | \
6353 NETIF_F_GSO_UDP_TUNNEL | \
6354 NETIF_F_GSO_UDP_TUNNEL_CSUM)
6356 netdev->gso_partial_features = IGC_GSO_PARTIAL_FEATURES;
6357 netdev->features |= NETIF_F_GSO_PARTIAL | IGC_GSO_PARTIAL_FEATURES;
6359 /* setup the private structure */
6360 err = igc_sw_init(adapter);
6364 /* copy netdev features into list of user selectable features */
6365 netdev->hw_features |= NETIF_F_NTUPLE;
6366 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
6367 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
6368 netdev->hw_features |= netdev->features;
6370 netdev->features |= NETIF_F_HIGHDMA;
6372 netdev->vlan_features |= netdev->features | NETIF_F_TSO_MANGLEID;
6373 netdev->mpls_features |= NETIF_F_HW_CSUM;
6374 netdev->hw_enc_features |= netdev->vlan_features;
6376 /* MTU range: 68 - 9216 */
6377 netdev->min_mtu = ETH_MIN_MTU;
6378 netdev->max_mtu = MAX_STD_JUMBO_FRAME_SIZE;
6380 /* before reading the NVM, reset the controller to put the device in a
6381 * known good starting state
6383 hw->mac.ops.reset_hw(hw);
6385 if (igc_get_flash_presence_i225(hw)) {
6386 if (hw->nvm.ops.validate(hw) < 0) {
6387 dev_err(&pdev->dev, "The NVM Checksum Is Not Valid\n");
6393 if (eth_platform_get_mac_address(&pdev->dev, hw->mac.addr)) {
6394 /* copy the MAC address out of the NVM */
6395 if (hw->mac.ops.read_mac_addr(hw))
6396 dev_err(&pdev->dev, "NVM Read Error\n");
6399 eth_hw_addr_set(netdev, hw->mac.addr);
6401 if (!is_valid_ether_addr(netdev->dev_addr)) {
6402 dev_err(&pdev->dev, "Invalid MAC Address\n");
6407 /* configure RXPBSIZE and TXPBSIZE */
6408 wr32(IGC_RXPBS, I225_RXPBSIZE_DEFAULT);
6409 wr32(IGC_TXPBS, I225_TXPBSIZE_DEFAULT);
6411 timer_setup(&adapter->watchdog_timer, igc_watchdog, 0);
6412 timer_setup(&adapter->phy_info_timer, igc_update_phy_info, 0);
6414 INIT_WORK(&adapter->reset_task, igc_reset_task);
6415 INIT_WORK(&adapter->watchdog_task, igc_watchdog_task);
6417 /* Initialize link properties that are user-changeable */
6418 adapter->fc_autoneg = true;
6419 hw->mac.autoneg = true;
6420 hw->phy.autoneg_advertised = 0xaf;
6422 hw->fc.requested_mode = igc_fc_default;
6423 hw->fc.current_mode = igc_fc_default;
6425 /* By default, support wake on port A */
6426 adapter->flags |= IGC_FLAG_WOL_SUPPORTED;
6428 /* initialize the wol settings based on the eeprom settings */
6429 if (adapter->flags & IGC_FLAG_WOL_SUPPORTED)
6430 adapter->wol |= IGC_WUFC_MAG;
6432 device_set_wakeup_enable(&adapter->pdev->dev,
6433 adapter->flags & IGC_FLAG_WOL_SUPPORTED);
6435 igc_ptp_init(adapter);
6437 igc_tsn_clear_schedule(adapter);
6439 /* reset the hardware with the new settings */
6442 /* let the f/w know that the h/w is now under the control of the
6445 igc_get_hw_control(adapter);
6447 strncpy(netdev->name, "eth%d", IFNAMSIZ);
6448 err = register_netdev(netdev);
6452 /* carrier off reporting is important to ethtool even BEFORE open */
6453 netif_carrier_off(netdev);
6455 /* Check if Media Autosense is enabled */
6458 /* print pcie link status and MAC address */
6459 pcie_print_link_status(pdev);
6460 netdev_info(netdev, "MAC: %pM\n", netdev->dev_addr);
6462 dev_pm_set_driver_flags(&pdev->dev, DPM_FLAG_NO_DIRECT_COMPLETE);
6463 /* Disable EEE for internal PHY devices */
6464 hw->dev_spec._base.eee_enable = false;
6465 adapter->flags &= ~IGC_FLAG_EEE;
6466 igc_set_eee_i225(hw, false, false, false);
6468 pm_runtime_put_noidle(&pdev->dev);
6473 igc_release_hw_control(adapter);
6475 if (!igc_check_reset_block(hw))
6478 igc_clear_interrupt_scheme(adapter);
6479 iounmap(adapter->io_addr);
6481 free_netdev(netdev);
6483 pci_disable_pcie_error_reporting(pdev);
6484 pci_release_mem_regions(pdev);
6487 pci_disable_device(pdev);
6492 * igc_remove - Device Removal Routine
6493 * @pdev: PCI device information struct
6495 * igc_remove is called by the PCI subsystem to alert the driver
6496 * that it should release a PCI device. This could be caused by a
6497 * Hot-Plug event, or because the driver is going to be removed from
6500 static void igc_remove(struct pci_dev *pdev)
6502 struct net_device *netdev = pci_get_drvdata(pdev);
6503 struct igc_adapter *adapter = netdev_priv(netdev);
6505 pm_runtime_get_noresume(&pdev->dev);
6507 igc_flush_nfc_rules(adapter);
6509 igc_ptp_stop(adapter);
6511 set_bit(__IGC_DOWN, &adapter->state);
6513 del_timer_sync(&adapter->watchdog_timer);
6514 del_timer_sync(&adapter->phy_info_timer);
6516 cancel_work_sync(&adapter->reset_task);
6517 cancel_work_sync(&adapter->watchdog_task);
6519 /* Release control of h/w to f/w. If f/w is AMT enabled, this
6520 * would have already happened in close and is redundant.
6522 igc_release_hw_control(adapter);
6523 unregister_netdev(netdev);
6525 igc_clear_interrupt_scheme(adapter);
6526 pci_iounmap(pdev, adapter->io_addr);
6527 pci_release_mem_regions(pdev);
6529 free_netdev(netdev);
6531 pci_disable_pcie_error_reporting(pdev);
6533 pci_disable_device(pdev);
6536 static int __igc_shutdown(struct pci_dev *pdev, bool *enable_wake,
6539 struct net_device *netdev = pci_get_drvdata(pdev);
6540 struct igc_adapter *adapter = netdev_priv(netdev);
6541 u32 wufc = runtime ? IGC_WUFC_LNKC : adapter->wol;
6542 struct igc_hw *hw = &adapter->hw;
6543 u32 ctrl, rctl, status;
6547 netif_device_detach(netdev);
6549 if (netif_running(netdev))
6550 __igc_close(netdev, true);
6552 igc_ptp_suspend(adapter);
6554 igc_clear_interrupt_scheme(adapter);
6557 status = rd32(IGC_STATUS);
6558 if (status & IGC_STATUS_LU)
6559 wufc &= ~IGC_WUFC_LNKC;
6562 igc_setup_rctl(adapter);
6563 igc_set_rx_mode(netdev);
6565 /* turn on all-multi mode if wake on multicast is enabled */
6566 if (wufc & IGC_WUFC_MC) {
6567 rctl = rd32(IGC_RCTL);
6568 rctl |= IGC_RCTL_MPE;
6569 wr32(IGC_RCTL, rctl);
6572 ctrl = rd32(IGC_CTRL);
6573 ctrl |= IGC_CTRL_ADVD3WUC;
6574 wr32(IGC_CTRL, ctrl);
6576 /* Allow time for pending master requests to run */
6577 igc_disable_pcie_master(hw);
6579 wr32(IGC_WUC, IGC_WUC_PME_EN);
6580 wr32(IGC_WUFC, wufc);
6586 wake = wufc || adapter->en_mng_pt;
6588 igc_power_down_phy_copper_base(&adapter->hw);
6590 igc_power_up_link(adapter);
6593 *enable_wake = wake;
6595 /* Release control of h/w to f/w. If f/w is AMT enabled, this
6596 * would have already happened in close and is redundant.
6598 igc_release_hw_control(adapter);
6600 pci_disable_device(pdev);
6606 static int __maybe_unused igc_runtime_suspend(struct device *dev)
6608 return __igc_shutdown(to_pci_dev(dev), NULL, 1);
6611 static void igc_deliver_wake_packet(struct net_device *netdev)
6613 struct igc_adapter *adapter = netdev_priv(netdev);
6614 struct igc_hw *hw = &adapter->hw;
6615 struct sk_buff *skb;
6618 wupl = rd32(IGC_WUPL) & IGC_WUPL_MASK;
6620 /* WUPM stores only the first 128 bytes of the wake packet.
6621 * Read the packet only if we have the whole thing.
6623 if (wupl == 0 || wupl > IGC_WUPM_BYTES)
6626 skb = netdev_alloc_skb_ip_align(netdev, IGC_WUPM_BYTES);
6632 /* Ensure reads are 32-bit aligned */
6633 wupl = roundup(wupl, 4);
6635 memcpy_fromio(skb->data, hw->hw_addr + IGC_WUPM_REG(0), wupl);
6637 skb->protocol = eth_type_trans(skb, netdev);
6641 static int __maybe_unused igc_resume(struct device *dev)
6643 struct pci_dev *pdev = to_pci_dev(dev);
6644 struct net_device *netdev = pci_get_drvdata(pdev);
6645 struct igc_adapter *adapter = netdev_priv(netdev);
6646 struct igc_hw *hw = &adapter->hw;
6649 pci_set_power_state(pdev, PCI_D0);
6650 pci_restore_state(pdev);
6651 pci_save_state(pdev);
6653 if (!pci_device_is_present(pdev))
6655 err = pci_enable_device_mem(pdev);
6657 netdev_err(netdev, "Cannot enable PCI device from suspend\n");
6660 pci_set_master(pdev);
6662 pci_enable_wake(pdev, PCI_D3hot, 0);
6663 pci_enable_wake(pdev, PCI_D3cold, 0);
6665 if (igc_init_interrupt_scheme(adapter, true)) {
6666 netdev_err(netdev, "Unable to allocate memory for queues\n");
6672 /* let the f/w know that the h/w is now under the control of the
6675 igc_get_hw_control(adapter);
6677 val = rd32(IGC_WUS);
6678 if (val & WAKE_PKT_WUS)
6679 igc_deliver_wake_packet(netdev);
6684 if (!err && netif_running(netdev))
6685 err = __igc_open(netdev, true);
6688 netif_device_attach(netdev);
6694 static int __maybe_unused igc_runtime_resume(struct device *dev)
6696 return igc_resume(dev);
6699 static int __maybe_unused igc_suspend(struct device *dev)
6701 return __igc_shutdown(to_pci_dev(dev), NULL, 0);
6704 static int __maybe_unused igc_runtime_idle(struct device *dev)
6706 struct net_device *netdev = dev_get_drvdata(dev);
6707 struct igc_adapter *adapter = netdev_priv(netdev);
6709 if (!igc_has_link(adapter))
6710 pm_schedule_suspend(dev, MSEC_PER_SEC * 5);
6714 #endif /* CONFIG_PM */
6716 static void igc_shutdown(struct pci_dev *pdev)
6720 __igc_shutdown(pdev, &wake, 0);
6722 if (system_state == SYSTEM_POWER_OFF) {
6723 pci_wake_from_d3(pdev, wake);
6724 pci_set_power_state(pdev, PCI_D3hot);
6729 * igc_io_error_detected - called when PCI error is detected
6730 * @pdev: Pointer to PCI device
6731 * @state: The current PCI connection state
6733 * This function is called after a PCI bus error affecting
6734 * this device has been detected.
6736 static pci_ers_result_t igc_io_error_detected(struct pci_dev *pdev,
6737 pci_channel_state_t state)
6739 struct net_device *netdev = pci_get_drvdata(pdev);
6740 struct igc_adapter *adapter = netdev_priv(netdev);
6742 netif_device_detach(netdev);
6744 if (state == pci_channel_io_perm_failure)
6745 return PCI_ERS_RESULT_DISCONNECT;
6747 if (netif_running(netdev))
6749 pci_disable_device(pdev);
6751 /* Request a slot reset. */
6752 return PCI_ERS_RESULT_NEED_RESET;
6756 * igc_io_slot_reset - called after the PCI bus has been reset.
6757 * @pdev: Pointer to PCI device
6759 * Restart the card from scratch, as if from a cold-boot. Implementation
6760 * resembles the first-half of the igc_resume routine.
6762 static pci_ers_result_t igc_io_slot_reset(struct pci_dev *pdev)
6764 struct net_device *netdev = pci_get_drvdata(pdev);
6765 struct igc_adapter *adapter = netdev_priv(netdev);
6766 struct igc_hw *hw = &adapter->hw;
6767 pci_ers_result_t result;
6769 if (pci_enable_device_mem(pdev)) {
6770 netdev_err(netdev, "Could not re-enable PCI device after reset\n");
6771 result = PCI_ERS_RESULT_DISCONNECT;
6773 pci_set_master(pdev);
6774 pci_restore_state(pdev);
6775 pci_save_state(pdev);
6777 pci_enable_wake(pdev, PCI_D3hot, 0);
6778 pci_enable_wake(pdev, PCI_D3cold, 0);
6780 /* In case of PCI error, adapter loses its HW address
6781 * so we should re-assign it here.
6783 hw->hw_addr = adapter->io_addr;
6787 result = PCI_ERS_RESULT_RECOVERED;
6794 * igc_io_resume - called when traffic can start to flow again.
6795 * @pdev: Pointer to PCI device
6797 * This callback is called when the error recovery driver tells us that
6798 * its OK to resume normal operation. Implementation resembles the
6799 * second-half of the igc_resume routine.
6801 static void igc_io_resume(struct pci_dev *pdev)
6803 struct net_device *netdev = pci_get_drvdata(pdev);
6804 struct igc_adapter *adapter = netdev_priv(netdev);
6807 if (netif_running(netdev)) {
6808 if (igc_open(netdev)) {
6809 netdev_err(netdev, "igc_open failed after reset\n");
6814 netif_device_attach(netdev);
6816 /* let the f/w know that the h/w is now under the control of the
6819 igc_get_hw_control(adapter);
6823 static const struct pci_error_handlers igc_err_handler = {
6824 .error_detected = igc_io_error_detected,
6825 .slot_reset = igc_io_slot_reset,
6826 .resume = igc_io_resume,
6830 static const struct dev_pm_ops igc_pm_ops = {
6831 SET_SYSTEM_SLEEP_PM_OPS(igc_suspend, igc_resume)
6832 SET_RUNTIME_PM_OPS(igc_runtime_suspend, igc_runtime_resume,
6837 static struct pci_driver igc_driver = {
6838 .name = igc_driver_name,
6839 .id_table = igc_pci_tbl,
6841 .remove = igc_remove,
6843 .driver.pm = &igc_pm_ops,
6845 .shutdown = igc_shutdown,
6846 .err_handler = &igc_err_handler,
6850 * igc_reinit_queues - return error
6851 * @adapter: pointer to adapter structure
6853 int igc_reinit_queues(struct igc_adapter *adapter)
6855 struct net_device *netdev = adapter->netdev;
6858 if (netif_running(netdev))
6861 igc_reset_interrupt_capability(adapter);
6863 if (igc_init_interrupt_scheme(adapter, true)) {
6864 netdev_err(netdev, "Unable to allocate memory for queues\n");
6868 if (netif_running(netdev))
6869 err = igc_open(netdev);
6875 * igc_get_hw_dev - return device
6876 * @hw: pointer to hardware structure
6878 * used by hardware layer to print debugging information
6880 struct net_device *igc_get_hw_dev(struct igc_hw *hw)
6882 struct igc_adapter *adapter = hw->back;
6884 return adapter->netdev;
6887 static void igc_disable_rx_ring_hw(struct igc_ring *ring)
6889 struct igc_hw *hw = &ring->q_vector->adapter->hw;
6890 u8 idx = ring->reg_idx;
6893 rxdctl = rd32(IGC_RXDCTL(idx));
6894 rxdctl &= ~IGC_RXDCTL_QUEUE_ENABLE;
6895 rxdctl |= IGC_RXDCTL_SWFLUSH;
6896 wr32(IGC_RXDCTL(idx), rxdctl);
6899 void igc_disable_rx_ring(struct igc_ring *ring)
6901 igc_disable_rx_ring_hw(ring);
6902 igc_clean_rx_ring(ring);
6905 void igc_enable_rx_ring(struct igc_ring *ring)
6907 struct igc_adapter *adapter = ring->q_vector->adapter;
6909 igc_configure_rx_ring(adapter, ring);
6912 igc_alloc_rx_buffers_zc(ring, igc_desc_unused(ring));
6914 igc_alloc_rx_buffers(ring, igc_desc_unused(ring));
6917 static void igc_disable_tx_ring_hw(struct igc_ring *ring)
6919 struct igc_hw *hw = &ring->q_vector->adapter->hw;
6920 u8 idx = ring->reg_idx;
6923 txdctl = rd32(IGC_TXDCTL(idx));
6924 txdctl &= ~IGC_TXDCTL_QUEUE_ENABLE;
6925 txdctl |= IGC_TXDCTL_SWFLUSH;
6926 wr32(IGC_TXDCTL(idx), txdctl);
6929 void igc_disable_tx_ring(struct igc_ring *ring)
6931 igc_disable_tx_ring_hw(ring);
6932 igc_clean_tx_ring(ring);
6935 void igc_enable_tx_ring(struct igc_ring *ring)
6937 struct igc_adapter *adapter = ring->q_vector->adapter;
6939 igc_configure_tx_ring(adapter, ring);
6943 * igc_init_module - Driver Registration Routine
6945 * igc_init_module is the first routine called when the driver is
6946 * loaded. All it does is register with the PCI subsystem.
6948 static int __init igc_init_module(void)
6952 pr_info("%s\n", igc_driver_string);
6953 pr_info("%s\n", igc_copyright);
6955 ret = pci_register_driver(&igc_driver);
6959 module_init(igc_init_module);
6962 * igc_exit_module - Driver Exit Cleanup Routine
6964 * igc_exit_module is called just before the driver is removed
6967 static void __exit igc_exit_module(void)
6969 pci_unregister_driver(&igc_driver);
6972 module_exit(igc_exit_module);