1d89f8841SSasha Neftin /* SPDX-License-Identifier: GPL-2.0 */ 2d89f8841SSasha Neftin /* Copyright (c) 2018 Intel Corporation */ 3d89f8841SSasha Neftin 4d89f8841SSasha Neftin #ifndef _IGC_H_ 5d89f8841SSasha Neftin #define _IGC_H_ 6d89f8841SSasha Neftin 7d89f8841SSasha Neftin #include <linux/kobject.h> 8d89f8841SSasha Neftin 9d89f8841SSasha Neftin #include <linux/pci.h> 10d89f8841SSasha Neftin #include <linux/netdevice.h> 11d89f8841SSasha Neftin #include <linux/vmalloc.h> 12d89f8841SSasha Neftin 13d89f8841SSasha Neftin #include <linux/ethtool.h> 14d89f8841SSasha Neftin 15d89f8841SSasha Neftin #include <linux/sctp.h> 16d89f8841SSasha Neftin 17d89f8841SSasha Neftin #define IGC_ERR(args...) pr_err("igc: " args) 18d89f8841SSasha Neftin 19d89f8841SSasha Neftin #define PFX "igc: " 20d89f8841SSasha Neftin 21d89f8841SSasha Neftin #include <linux/timecounter.h> 22d89f8841SSasha Neftin #include <linux/net_tstamp.h> 23d89f8841SSasha Neftin #include <linux/ptp_clock_kernel.h> 24d89f8841SSasha Neftin 25146740f9SSasha Neftin #include "igc_hw.h" 26146740f9SSasha Neftin 27d89f8841SSasha Neftin /* main */ 28d89f8841SSasha Neftin extern char igc_driver_name[]; 29d89f8841SSasha Neftin extern char igc_driver_version[]; 30d89f8841SSasha Neftin 313df25e4cSSasha Neftin /* Interrupt defines */ 323df25e4cSSasha Neftin #define IGC_START_ITR 648 /* ~6000 ints/sec */ 333df25e4cSSasha Neftin #define IGC_FLAG_HAS_MSI BIT(0) 343df25e4cSSasha Neftin #define IGC_FLAG_QUEUE_PAIRS BIT(4) 353df25e4cSSasha Neftin #define IGC_FLAG_HAS_MSIX BIT(13) 363df25e4cSSasha Neftin 373df25e4cSSasha Neftin #define IGC_START_ITR 648 /* ~6000 ints/sec */ 383df25e4cSSasha Neftin #define IGC_4K_ITR 980 393df25e4cSSasha Neftin #define IGC_20K_ITR 196 403df25e4cSSasha Neftin #define IGC_70K_ITR 56 413df25e4cSSasha Neftin 42c9a11c23SSasha Neftin /* Transmit and receive queues */ 43c9a11c23SSasha Neftin #define IGC_MAX_RX_QUEUES 4 44c9a11c23SSasha Neftin #define IGC_MAX_TX_QUEUES 4 45c9a11c23SSasha Neftin 46c9a11c23SSasha Neftin #define MAX_Q_VECTORS 8 47c9a11c23SSasha Neftin #define MAX_STD_JUMBO_FRAME_SIZE 9216 48c9a11c23SSasha Neftin 4913b5b7fdSSasha Neftin /* Supported Rx Buffer Sizes */ 5013b5b7fdSSasha Neftin #define IGC_RXBUFFER_256 256 5113b5b7fdSSasha Neftin #define IGC_RXBUFFER_2048 2048 5213b5b7fdSSasha Neftin #define IGC_RXBUFFER_3072 3072 5313b5b7fdSSasha Neftin 5413b5b7fdSSasha Neftin #define IGC_RX_HDR_LEN IGC_RXBUFFER_256 5513b5b7fdSSasha Neftin 5613b5b7fdSSasha Neftin /* RX and TX descriptor control thresholds. 5713b5b7fdSSasha Neftin * PTHRESH - MAC will consider prefetch if it has fewer than this number of 5813b5b7fdSSasha Neftin * descriptors available in its onboard memory. 5913b5b7fdSSasha Neftin * Setting this to 0 disables RX descriptor prefetch. 6013b5b7fdSSasha Neftin * HTHRESH - MAC will only prefetch if there are at least this many descriptors 6113b5b7fdSSasha Neftin * available in host memory. 6213b5b7fdSSasha Neftin * If PTHRESH is 0, this should also be 0. 6313b5b7fdSSasha Neftin * WTHRESH - RX descriptor writeback threshold - MAC will delay writing back 6413b5b7fdSSasha Neftin * descriptors until either it has this many to write back, or the 6513b5b7fdSSasha Neftin * ITR timer expires. 6613b5b7fdSSasha Neftin */ 6713b5b7fdSSasha Neftin #define IGC_RX_PTHRESH 8 6813b5b7fdSSasha Neftin #define IGC_RX_HTHRESH 8 6913b5b7fdSSasha Neftin #define IGC_TX_PTHRESH 8 7013b5b7fdSSasha Neftin #define IGC_TX_HTHRESH 1 7113b5b7fdSSasha Neftin #define IGC_RX_WTHRESH 4 7213b5b7fdSSasha Neftin #define IGC_TX_WTHRESH 16 7313b5b7fdSSasha Neftin 7413b5b7fdSSasha Neftin #define IGC_RX_DMA_ATTR \ 7513b5b7fdSSasha Neftin (DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_WEAK_ORDERING) 7613b5b7fdSSasha Neftin 7713b5b7fdSSasha Neftin #define IGC_TS_HDR_LEN 16 7813b5b7fdSSasha Neftin 7913b5b7fdSSasha Neftin #define IGC_SKB_PAD (NET_SKB_PAD + NET_IP_ALIGN) 8013b5b7fdSSasha Neftin 8113b5b7fdSSasha Neftin #if (PAGE_SIZE < 8192) 8213b5b7fdSSasha Neftin #define IGC_MAX_FRAME_BUILD_SKB \ 8313b5b7fdSSasha Neftin (SKB_WITH_OVERHEAD(IGC_RXBUFFER_2048) - IGC_SKB_PAD - IGC_TS_HDR_LEN) 8413b5b7fdSSasha Neftin #else 8513b5b7fdSSasha Neftin #define IGC_MAX_FRAME_BUILD_SKB (IGC_RXBUFFER_2048 - IGC_TS_HDR_LEN) 8613b5b7fdSSasha Neftin #endif 8713b5b7fdSSasha Neftin 88c9a11c23SSasha Neftin enum igc_state_t { 89c9a11c23SSasha Neftin __IGC_TESTING, 90c9a11c23SSasha Neftin __IGC_RESETTING, 91c9a11c23SSasha Neftin __IGC_DOWN, 92c9a11c23SSasha Neftin __IGC_PTP_TX_IN_PROGRESS, 93c9a11c23SSasha Neftin }; 94c9a11c23SSasha Neftin 9513b5b7fdSSasha Neftin /* wrapper around a pointer to a socket buffer, 9613b5b7fdSSasha Neftin * so a DMA handle can be stored along with the buffer 9713b5b7fdSSasha Neftin */ 9813b5b7fdSSasha Neftin struct igc_tx_buffer { 9913b5b7fdSSasha Neftin union igc_adv_tx_desc *next_to_watch; 10013b5b7fdSSasha Neftin unsigned long time_stamp; 10113b5b7fdSSasha Neftin struct sk_buff *skb; 10213b5b7fdSSasha Neftin unsigned int bytecount; 10313b5b7fdSSasha Neftin u16 gso_segs; 10413b5b7fdSSasha Neftin __be16 protocol; 10513b5b7fdSSasha Neftin 10613b5b7fdSSasha Neftin DEFINE_DMA_UNMAP_ADDR(dma); 10713b5b7fdSSasha Neftin DEFINE_DMA_UNMAP_LEN(len); 10813b5b7fdSSasha Neftin u32 tx_flags; 10913b5b7fdSSasha Neftin }; 11013b5b7fdSSasha Neftin 11113b5b7fdSSasha Neftin struct igc_rx_buffer { 11213b5b7fdSSasha Neftin dma_addr_t dma; 11313b5b7fdSSasha Neftin struct page *page; 11413b5b7fdSSasha Neftin #if (BITS_PER_LONG > 32) || (PAGE_SIZE >= 65536) 11513b5b7fdSSasha Neftin __u32 page_offset; 11613b5b7fdSSasha Neftin #else 11713b5b7fdSSasha Neftin __u16 page_offset; 11813b5b7fdSSasha Neftin #endif 11913b5b7fdSSasha Neftin __u16 pagecnt_bias; 12013b5b7fdSSasha Neftin }; 12113b5b7fdSSasha Neftin 1223df25e4cSSasha Neftin struct igc_tx_queue_stats { 1233df25e4cSSasha Neftin u64 packets; 1243df25e4cSSasha Neftin u64 bytes; 1253df25e4cSSasha Neftin u64 restart_queue; 1263df25e4cSSasha Neftin }; 1273df25e4cSSasha Neftin 1283df25e4cSSasha Neftin struct igc_rx_queue_stats { 1293df25e4cSSasha Neftin u64 packets; 1303df25e4cSSasha Neftin u64 bytes; 1313df25e4cSSasha Neftin u64 drops; 1323df25e4cSSasha Neftin u64 csum_err; 1333df25e4cSSasha Neftin u64 alloc_failed; 1343df25e4cSSasha Neftin }; 1353df25e4cSSasha Neftin 1363df25e4cSSasha Neftin struct igc_rx_packet_stats { 1373df25e4cSSasha Neftin u64 ipv4_packets; /* IPv4 headers processed */ 1383df25e4cSSasha Neftin u64 ipv4e_packets; /* IPv4E headers with extensions processed */ 1393df25e4cSSasha Neftin u64 ipv6_packets; /* IPv6 headers processed */ 1403df25e4cSSasha Neftin u64 ipv6e_packets; /* IPv6E headers with extensions processed */ 1413df25e4cSSasha Neftin u64 tcp_packets; /* TCP headers processed */ 1423df25e4cSSasha Neftin u64 udp_packets; /* UDP headers processed */ 1433df25e4cSSasha Neftin u64 sctp_packets; /* SCTP headers processed */ 1443df25e4cSSasha Neftin u64 nfs_packets; /* NFS headers processe */ 1453df25e4cSSasha Neftin u64 other_packets; 1463df25e4cSSasha Neftin }; 1473df25e4cSSasha Neftin 1483df25e4cSSasha Neftin struct igc_ring_container { 1493df25e4cSSasha Neftin struct igc_ring *ring; /* pointer to linked list of rings */ 1503df25e4cSSasha Neftin unsigned int total_bytes; /* total bytes processed this int */ 1513df25e4cSSasha Neftin unsigned int total_packets; /* total packets processed this int */ 1523df25e4cSSasha Neftin u16 work_limit; /* total work allowed per interrupt */ 1533df25e4cSSasha Neftin u8 count; /* total number of rings in vector */ 1543df25e4cSSasha Neftin u8 itr; /* current ITR setting for ring */ 1553df25e4cSSasha Neftin }; 1563df25e4cSSasha Neftin 1573df25e4cSSasha Neftin struct igc_ring { 1583df25e4cSSasha Neftin struct igc_q_vector *q_vector; /* backlink to q_vector */ 1593df25e4cSSasha Neftin struct net_device *netdev; /* back pointer to net_device */ 1603df25e4cSSasha Neftin struct device *dev; /* device for dma mapping */ 1613df25e4cSSasha Neftin union { /* array of buffer info structs */ 1623df25e4cSSasha Neftin struct igc_tx_buffer *tx_buffer_info; 1633df25e4cSSasha Neftin struct igc_rx_buffer *rx_buffer_info; 1643df25e4cSSasha Neftin }; 1653df25e4cSSasha Neftin void *desc; /* descriptor ring memory */ 1663df25e4cSSasha Neftin unsigned long flags; /* ring specific flags */ 1673df25e4cSSasha Neftin void __iomem *tail; /* pointer to ring tail register */ 1683df25e4cSSasha Neftin dma_addr_t dma; /* phys address of the ring */ 1693df25e4cSSasha Neftin unsigned int size; /* length of desc. ring in bytes */ 1703df25e4cSSasha Neftin 1713df25e4cSSasha Neftin u16 count; /* number of desc. in the ring */ 1723df25e4cSSasha Neftin u8 queue_index; /* logical index of the ring*/ 1733df25e4cSSasha Neftin u8 reg_idx; /* physical index of the ring */ 1743df25e4cSSasha Neftin 1753df25e4cSSasha Neftin /* everything past this point are written often */ 1763df25e4cSSasha Neftin u16 next_to_clean; 1773df25e4cSSasha Neftin u16 next_to_use; 1783df25e4cSSasha Neftin u16 next_to_alloc; 1793df25e4cSSasha Neftin 1803df25e4cSSasha Neftin union { 1813df25e4cSSasha Neftin /* TX */ 1823df25e4cSSasha Neftin struct { 1833df25e4cSSasha Neftin struct igc_tx_queue_stats tx_stats; 1843df25e4cSSasha Neftin }; 1853df25e4cSSasha Neftin /* RX */ 1863df25e4cSSasha Neftin struct { 1873df25e4cSSasha Neftin struct igc_rx_queue_stats rx_stats; 1883df25e4cSSasha Neftin struct igc_rx_packet_stats pkt_stats; 1893df25e4cSSasha Neftin struct sk_buff *skb; 1903df25e4cSSasha Neftin }; 1913df25e4cSSasha Neftin }; 1923df25e4cSSasha Neftin } ____cacheline_internodealigned_in_smp; 1933df25e4cSSasha Neftin 194c9a11c23SSasha Neftin struct igc_q_vector { 195c9a11c23SSasha Neftin struct igc_adapter *adapter; /* backlink */ 1963df25e4cSSasha Neftin void __iomem *itr_register; 1973df25e4cSSasha Neftin u32 eims_value; /* EIMS mask value */ 1983df25e4cSSasha Neftin 1993df25e4cSSasha Neftin u16 itr_val; 2003df25e4cSSasha Neftin u8 set_itr; 2013df25e4cSSasha Neftin 2023df25e4cSSasha Neftin struct igc_ring_container rx, tx; 203c9a11c23SSasha Neftin 204c9a11c23SSasha Neftin struct napi_struct napi; 2053df25e4cSSasha Neftin 2063df25e4cSSasha Neftin struct rcu_head rcu; /* to avoid race with update stats on free */ 2073df25e4cSSasha Neftin char name[IFNAMSIZ + 9]; 2083df25e4cSSasha Neftin struct net_device poll_dev; 2093df25e4cSSasha Neftin 2103df25e4cSSasha Neftin /* for dynamic allocation of rings associated with this q_vector */ 2113df25e4cSSasha Neftin struct igc_ring ring[0] ____cacheline_internodealigned_in_smp; 212c9a11c23SSasha Neftin }; 213c9a11c23SSasha Neftin 214c9a11c23SSasha Neftin struct igc_mac_addr { 215c9a11c23SSasha Neftin u8 addr[ETH_ALEN]; 216c9a11c23SSasha Neftin u8 queue; 217c9a11c23SSasha Neftin u8 state; /* bitmask */ 218c9a11c23SSasha Neftin }; 219c9a11c23SSasha Neftin 220c9a11c23SSasha Neftin #define IGC_MAC_STATE_DEFAULT 0x1 221c9a11c23SSasha Neftin #define IGC_MAC_STATE_MODIFIED 0x2 222c9a11c23SSasha Neftin #define IGC_MAC_STATE_IN_USE 0x4 223c9a11c23SSasha Neftin 224146740f9SSasha Neftin /* Board specific private data structure */ 225146740f9SSasha Neftin struct igc_adapter { 226c9a11c23SSasha Neftin struct net_device *netdev; 227c9a11c23SSasha Neftin 228c9a11c23SSasha Neftin unsigned long state; 229c9a11c23SSasha Neftin unsigned int flags; 230c9a11c23SSasha Neftin unsigned int num_q_vectors; 2313df25e4cSSasha Neftin 2323df25e4cSSasha Neftin struct msix_entry *msix_entries; 2333df25e4cSSasha Neftin 2343df25e4cSSasha Neftin /* TX */ 2353df25e4cSSasha Neftin u16 tx_work_limit; 2363df25e4cSSasha Neftin int num_tx_queues; 2373df25e4cSSasha Neftin struct igc_ring *tx_ring[IGC_MAX_TX_QUEUES]; 2383df25e4cSSasha Neftin 2393df25e4cSSasha Neftin /* RX */ 2403df25e4cSSasha Neftin int num_rx_queues; 2413df25e4cSSasha Neftin struct igc_ring *rx_ring[IGC_MAX_RX_QUEUES]; 2423df25e4cSSasha Neftin 2433df25e4cSSasha Neftin struct timer_list watchdog_timer; 2443df25e4cSSasha Neftin struct timer_list dma_err_timer; 2453df25e4cSSasha Neftin struct timer_list phy_info_timer; 2463df25e4cSSasha Neftin 247c9a11c23SSasha Neftin u16 link_speed; 248c9a11c23SSasha Neftin u16 link_duplex; 249c9a11c23SSasha Neftin 250c9a11c23SSasha Neftin u8 port_num; 251c9a11c23SSasha Neftin 252146740f9SSasha Neftin u8 __iomem *io_addr; 2533df25e4cSSasha Neftin /* Interrupt Throttle Rate */ 2543df25e4cSSasha Neftin u32 rx_itr_setting; 2553df25e4cSSasha Neftin u32 tx_itr_setting; 2563df25e4cSSasha Neftin 2573df25e4cSSasha Neftin struct work_struct reset_task; 258c9a11c23SSasha Neftin struct work_struct watchdog_task; 2593df25e4cSSasha Neftin struct work_struct dma_err_task; 260c9a11c23SSasha Neftin 261c9a11c23SSasha Neftin int msg_enable; 262c9a11c23SSasha Neftin u32 max_frame_size; 263146740f9SSasha Neftin 264146740f9SSasha Neftin /* OS defined structs */ 265146740f9SSasha Neftin struct pci_dev *pdev; 266146740f9SSasha Neftin 267146740f9SSasha Neftin /* structs defined in igc_hw.h */ 268146740f9SSasha Neftin struct igc_hw hw; 2693df25e4cSSasha Neftin struct igc_hw_stats stats; 270c9a11c23SSasha Neftin 271c9a11c23SSasha Neftin struct igc_q_vector *q_vector[MAX_Q_VECTORS]; 2723df25e4cSSasha Neftin u32 eims_enable_mask; 2733df25e4cSSasha Neftin u32 eims_other; 2743df25e4cSSasha Neftin 2753df25e4cSSasha Neftin u16 tx_ring_count; 2763df25e4cSSasha Neftin u16 rx_ring_count; 2773df25e4cSSasha Neftin 2783df25e4cSSasha Neftin u32 rss_queues; 279c9a11c23SSasha Neftin 280c9a11c23SSasha Neftin struct igc_mac_addr *mac_table; 281146740f9SSasha Neftin }; 282146740f9SSasha Neftin 28313b5b7fdSSasha Neftin /* igc_desc_unused - calculate if we have unused descriptors */ 28413b5b7fdSSasha Neftin static inline u16 igc_desc_unused(const struct igc_ring *ring) 28513b5b7fdSSasha Neftin { 28613b5b7fdSSasha Neftin u16 ntc = ring->next_to_clean; 28713b5b7fdSSasha Neftin u16 ntu = ring->next_to_use; 28813b5b7fdSSasha Neftin 28913b5b7fdSSasha Neftin return ((ntc > ntu) ? 0 : ring->count) + ntc - ntu - 1; 29013b5b7fdSSasha Neftin } 29113b5b7fdSSasha Neftin 29213b5b7fdSSasha Neftin static inline struct netdev_queue *txring_txq(const struct igc_ring *tx_ring) 29313b5b7fdSSasha Neftin { 29413b5b7fdSSasha Neftin return netdev_get_tx_queue(tx_ring->netdev, tx_ring->queue_index); 29513b5b7fdSSasha Neftin } 29613b5b7fdSSasha Neftin 29713b5b7fdSSasha Neftin enum igc_ring_flags_t { 29813b5b7fdSSasha Neftin IGC_RING_FLAG_RX_3K_BUFFER, 29913b5b7fdSSasha Neftin IGC_RING_FLAG_RX_BUILD_SKB_ENABLED, 30013b5b7fdSSasha Neftin IGC_RING_FLAG_RX_SCTP_CSUM, 30113b5b7fdSSasha Neftin IGC_RING_FLAG_RX_LB_VLAN_BSWAP, 30213b5b7fdSSasha Neftin IGC_RING_FLAG_TX_CTX_IDX, 30313b5b7fdSSasha Neftin IGC_RING_FLAG_TX_DETECT_HANG 30413b5b7fdSSasha Neftin }; 30513b5b7fdSSasha Neftin 30613b5b7fdSSasha Neftin #define ring_uses_large_buffer(ring) \ 30713b5b7fdSSasha Neftin test_bit(IGC_RING_FLAG_RX_3K_BUFFER, &(ring)->flags) 30813b5b7fdSSasha Neftin 30913b5b7fdSSasha Neftin #define ring_uses_build_skb(ring) \ 31013b5b7fdSSasha Neftin test_bit(IGC_RING_FLAG_RX_BUILD_SKB_ENABLED, &(ring)->flags) 31113b5b7fdSSasha Neftin 31213b5b7fdSSasha Neftin static inline unsigned int igc_rx_bufsz(struct igc_ring *ring) 31313b5b7fdSSasha Neftin { 31413b5b7fdSSasha Neftin #if (PAGE_SIZE < 8192) 31513b5b7fdSSasha Neftin if (ring_uses_large_buffer(ring)) 31613b5b7fdSSasha Neftin return IGC_RXBUFFER_3072; 31713b5b7fdSSasha Neftin 31813b5b7fdSSasha Neftin if (ring_uses_build_skb(ring)) 31913b5b7fdSSasha Neftin return IGC_MAX_FRAME_BUILD_SKB + IGC_TS_HDR_LEN; 32013b5b7fdSSasha Neftin #endif 32113b5b7fdSSasha Neftin return IGC_RXBUFFER_2048; 32213b5b7fdSSasha Neftin } 32313b5b7fdSSasha Neftin 32413b5b7fdSSasha Neftin static inline unsigned int igc_rx_pg_order(struct igc_ring *ring) 32513b5b7fdSSasha Neftin { 32613b5b7fdSSasha Neftin #if (PAGE_SIZE < 8192) 32713b5b7fdSSasha Neftin if (ring_uses_large_buffer(ring)) 32813b5b7fdSSasha Neftin return 1; 32913b5b7fdSSasha Neftin #endif 33013b5b7fdSSasha Neftin return 0; 33113b5b7fdSSasha Neftin } 33213b5b7fdSSasha Neftin 33313b5b7fdSSasha Neftin #define igc_rx_pg_size(_ring) (PAGE_SIZE << igc_rx_pg_order(_ring)) 33413b5b7fdSSasha Neftin 33513b5b7fdSSasha Neftin #define IGC_RX_DESC(R, i) \ 33613b5b7fdSSasha Neftin (&(((union igc_adv_rx_desc *)((R)->desc))[i])) 33713b5b7fdSSasha Neftin #define IGC_TX_DESC(R, i) \ 33813b5b7fdSSasha Neftin (&(((union igc_adv_tx_desc *)((R)->desc))[i])) 33913b5b7fdSSasha Neftin #define IGC_TX_CTXTDESC(R, i) \ 34013b5b7fdSSasha Neftin (&(((struct igc_adv_tx_context_desc *)((R)->desc))[i])) 34113b5b7fdSSasha Neftin 342d89f8841SSasha Neftin #endif /* _IGC_H_ */ 343