igc.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* Copyright (c) 2018 Intel Corporation */
  3. #ifndef _IGC_H_
  4. #define _IGC_H_
  5. #include <linux/kobject.h>
  6. #include <linux/pci.h>
  7. #include <linux/netdevice.h>
  8. #include <linux/vmalloc.h>
  9. #include <linux/ethtool.h>
  10. #include <linux/sctp.h>
  11. #define IGC_ERR(args...) pr_err("igc: " args)
  12. #define PFX "igc: "
  13. #include <linux/timecounter.h>
  14. #include <linux/net_tstamp.h>
  15. #include <linux/ptp_clock_kernel.h>
  16. #include "igc_hw.h"
  17. /* main */
  18. extern char igc_driver_name[];
  19. extern char igc_driver_version[];
  20. /* Interrupt defines */
  21. #define IGC_START_ITR 648 /* ~6000 ints/sec */
  22. #define IGC_FLAG_HAS_MSI BIT(0)
  23. #define IGC_FLAG_QUEUE_PAIRS BIT(4)
  24. #define IGC_FLAG_NEED_LINK_UPDATE BIT(9)
  25. #define IGC_FLAG_HAS_MSIX BIT(13)
  26. #define IGC_FLAG_VLAN_PROMISC BIT(15)
  27. #define IGC_START_ITR 648 /* ~6000 ints/sec */
  28. #define IGC_4K_ITR 980
  29. #define IGC_20K_ITR 196
  30. #define IGC_70K_ITR 56
  31. #define IGC_DEFAULT_ITR 3 /* dynamic */
  32. #define IGC_MAX_ITR_USECS 10000
  33. #define IGC_MIN_ITR_USECS 10
  34. #define NON_Q_VECTORS 1
  35. #define MAX_MSIX_ENTRIES 10
  36. /* TX/RX descriptor defines */
  37. #define IGC_DEFAULT_TXD 256
  38. #define IGC_DEFAULT_TX_WORK 128
  39. #define IGC_MIN_TXD 80
  40. #define IGC_MAX_TXD 4096
  41. #define IGC_DEFAULT_RXD 256
  42. #define IGC_MIN_RXD 80
  43. #define IGC_MAX_RXD 4096
  44. /* Transmit and receive queues */
  45. #define IGC_MAX_RX_QUEUES 4
  46. #define IGC_MAX_TX_QUEUES 4
  47. #define MAX_Q_VECTORS 8
  48. #define MAX_STD_JUMBO_FRAME_SIZE 9216
  49. /* Supported Rx Buffer Sizes */
  50. #define IGC_RXBUFFER_256 256
  51. #define IGC_RXBUFFER_2048 2048
  52. #define IGC_RXBUFFER_3072 3072
  53. #define IGC_RX_HDR_LEN IGC_RXBUFFER_256
  54. /* RX and TX descriptor control thresholds.
  55. * PTHRESH - MAC will consider prefetch if it has fewer than this number of
  56. * descriptors available in its onboard memory.
  57. * Setting this to 0 disables RX descriptor prefetch.
  58. * HTHRESH - MAC will only prefetch if there are at least this many descriptors
  59. * available in host memory.
  60. * If PTHRESH is 0, this should also be 0.
  61. * WTHRESH - RX descriptor writeback threshold - MAC will delay writing back
  62. * descriptors until either it has this many to write back, or the
  63. * ITR timer expires.
  64. */
  65. #define IGC_RX_PTHRESH 8
  66. #define IGC_RX_HTHRESH 8
  67. #define IGC_TX_PTHRESH 8
  68. #define IGC_TX_HTHRESH 1
  69. #define IGC_RX_WTHRESH 4
  70. #define IGC_TX_WTHRESH 16
  71. #define IGC_RX_DMA_ATTR \
  72. (DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_WEAK_ORDERING)
  73. #define IGC_TS_HDR_LEN 16
  74. #define IGC_SKB_PAD (NET_SKB_PAD + NET_IP_ALIGN)
  75. #if (PAGE_SIZE < 8192)
  76. #define IGC_MAX_FRAME_BUILD_SKB \
  77. (SKB_WITH_OVERHEAD(IGC_RXBUFFER_2048) - IGC_SKB_PAD - IGC_TS_HDR_LEN)
  78. #else
  79. #define IGC_MAX_FRAME_BUILD_SKB (IGC_RXBUFFER_2048 - IGC_TS_HDR_LEN)
  80. #endif
  81. /* How many Rx Buffers do we bundle into one write to the hardware ? */
  82. #define IGC_RX_BUFFER_WRITE 16 /* Must be power of 2 */
  83. /* igc_test_staterr - tests bits within Rx descriptor status and error fields */
  84. static inline __le32 igc_test_staterr(union igc_adv_rx_desc *rx_desc,
  85. const u32 stat_err_bits)
  86. {
  87. return rx_desc->wb.upper.status_error & cpu_to_le32(stat_err_bits);
  88. }
  89. enum igc_state_t {
  90. __IGC_TESTING,
  91. __IGC_RESETTING,
  92. __IGC_DOWN,
  93. __IGC_PTP_TX_IN_PROGRESS,
  94. };
  95. enum igc_tx_flags {
  96. /* cmd_type flags */
  97. IGC_TX_FLAGS_VLAN = 0x01,
  98. IGC_TX_FLAGS_TSO = 0x02,
  99. IGC_TX_FLAGS_TSTAMP = 0x04,
  100. /* olinfo flags */
  101. IGC_TX_FLAGS_IPV4 = 0x10,
  102. IGC_TX_FLAGS_CSUM = 0x20,
  103. };
  104. enum igc_boards {
  105. board_base,
  106. };
  107. /* The largest size we can write to the descriptor is 65535. In order to
  108. * maintain a power of two alignment we have to limit ourselves to 32K.
  109. */
  110. #define IGC_MAX_TXD_PWR 15
  111. #define IGC_MAX_DATA_PER_TXD BIT(IGC_MAX_TXD_PWR)
  112. /* Tx Descriptors needed, worst case */
  113. #define TXD_USE_COUNT(S) DIV_ROUND_UP((S), IGC_MAX_DATA_PER_TXD)
  114. #define DESC_NEEDED (MAX_SKB_FRAGS + 4)
  115. /* wrapper around a pointer to a socket buffer,
  116. * so a DMA handle can be stored along with the buffer
  117. */
  118. struct igc_tx_buffer {
  119. union igc_adv_tx_desc *next_to_watch;
  120. unsigned long time_stamp;
  121. struct sk_buff *skb;
  122. unsigned int bytecount;
  123. u16 gso_segs;
  124. __be16 protocol;
  125. DEFINE_DMA_UNMAP_ADDR(dma);
  126. DEFINE_DMA_UNMAP_LEN(len);
  127. u32 tx_flags;
  128. };
  129. struct igc_rx_buffer {
  130. dma_addr_t dma;
  131. struct page *page;
  132. #if (BITS_PER_LONG > 32) || (PAGE_SIZE >= 65536)
  133. __u32 page_offset;
  134. #else
  135. __u16 page_offset;
  136. #endif
  137. __u16 pagecnt_bias;
  138. };
  139. struct igc_tx_queue_stats {
  140. u64 packets;
  141. u64 bytes;
  142. u64 restart_queue;
  143. u64 restart_queue2;
  144. };
  145. struct igc_rx_queue_stats {
  146. u64 packets;
  147. u64 bytes;
  148. u64 drops;
  149. u64 csum_err;
  150. u64 alloc_failed;
  151. };
  152. struct igc_rx_packet_stats {
  153. u64 ipv4_packets; /* IPv4 headers processed */
  154. u64 ipv4e_packets; /* IPv4E headers with extensions processed */
  155. u64 ipv6_packets; /* IPv6 headers processed */
  156. u64 ipv6e_packets; /* IPv6E headers with extensions processed */
  157. u64 tcp_packets; /* TCP headers processed */
  158. u64 udp_packets; /* UDP headers processed */
  159. u64 sctp_packets; /* SCTP headers processed */
  160. u64 nfs_packets; /* NFS headers processe */
  161. u64 other_packets;
  162. };
  163. struct igc_ring_container {
  164. struct igc_ring *ring; /* pointer to linked list of rings */
  165. unsigned int total_bytes; /* total bytes processed this int */
  166. unsigned int total_packets; /* total packets processed this int */
  167. u16 work_limit; /* total work allowed per interrupt */
  168. u8 count; /* total number of rings in vector */
  169. u8 itr; /* current ITR setting for ring */
  170. };
  171. struct igc_ring {
  172. struct igc_q_vector *q_vector; /* backlink to q_vector */
  173. struct net_device *netdev; /* back pointer to net_device */
  174. struct device *dev; /* device for dma mapping */
  175. union { /* array of buffer info structs */
  176. struct igc_tx_buffer *tx_buffer_info;
  177. struct igc_rx_buffer *rx_buffer_info;
  178. };
  179. void *desc; /* descriptor ring memory */
  180. unsigned long flags; /* ring specific flags */
  181. void __iomem *tail; /* pointer to ring tail register */
  182. dma_addr_t dma; /* phys address of the ring */
  183. unsigned int size; /* length of desc. ring in bytes */
  184. u16 count; /* number of desc. in the ring */
  185. u8 queue_index; /* logical index of the ring*/
  186. u8 reg_idx; /* physical index of the ring */
  187. /* everything past this point are written often */
  188. u16 next_to_clean;
  189. u16 next_to_use;
  190. u16 next_to_alloc;
  191. union {
  192. /* TX */
  193. struct {
  194. struct igc_tx_queue_stats tx_stats;
  195. struct u64_stats_sync tx_syncp;
  196. struct u64_stats_sync tx_syncp2;
  197. };
  198. /* RX */
  199. struct {
  200. struct igc_rx_queue_stats rx_stats;
  201. struct igc_rx_packet_stats pkt_stats;
  202. struct u64_stats_sync rx_syncp;
  203. struct sk_buff *skb;
  204. };
  205. };
  206. } ____cacheline_internodealigned_in_smp;
  207. struct igc_q_vector {
  208. struct igc_adapter *adapter; /* backlink */
  209. void __iomem *itr_register;
  210. u32 eims_value; /* EIMS mask value */
  211. u16 itr_val;
  212. u8 set_itr;
  213. struct igc_ring_container rx, tx;
  214. struct napi_struct napi;
  215. struct rcu_head rcu; /* to avoid race with update stats on free */
  216. char name[IFNAMSIZ + 9];
  217. struct net_device poll_dev;
  218. /* for dynamic allocation of rings associated with this q_vector */
  219. struct igc_ring ring[0] ____cacheline_internodealigned_in_smp;
  220. };
  221. struct igc_mac_addr {
  222. u8 addr[ETH_ALEN];
  223. u8 queue;
  224. u8 state; /* bitmask */
  225. };
  226. #define IGC_MAC_STATE_DEFAULT 0x1
  227. #define IGC_MAC_STATE_MODIFIED 0x2
  228. #define IGC_MAC_STATE_IN_USE 0x4
  229. /* Board specific private data structure */
  230. struct igc_adapter {
  231. struct net_device *netdev;
  232. unsigned long state;
  233. unsigned int flags;
  234. unsigned int num_q_vectors;
  235. struct msix_entry *msix_entries;
  236. /* TX */
  237. u16 tx_work_limit;
  238. int num_tx_queues;
  239. struct igc_ring *tx_ring[IGC_MAX_TX_QUEUES];
  240. /* RX */
  241. int num_rx_queues;
  242. struct igc_ring *rx_ring[IGC_MAX_RX_QUEUES];
  243. struct timer_list watchdog_timer;
  244. struct timer_list dma_err_timer;
  245. struct timer_list phy_info_timer;
  246. u16 link_speed;
  247. u16 link_duplex;
  248. u8 port_num;
  249. u8 __iomem *io_addr;
  250. /* Interrupt Throttle Rate */
  251. u32 rx_itr_setting;
  252. u32 tx_itr_setting;
  253. struct work_struct reset_task;
  254. struct work_struct watchdog_task;
  255. struct work_struct dma_err_task;
  256. u8 tx_timeout_factor;
  257. int msg_enable;
  258. u32 max_frame_size;
  259. u32 min_frame_size;
  260. /* OS defined structs */
  261. struct pci_dev *pdev;
  262. /* lock for statistics */
  263. spinlock_t stats64_lock;
  264. struct rtnl_link_stats64 stats64;
  265. /* structs defined in igc_hw.h */
  266. struct igc_hw hw;
  267. struct igc_hw_stats stats;
  268. struct igc_q_vector *q_vector[MAX_Q_VECTORS];
  269. u32 eims_enable_mask;
  270. u32 eims_other;
  271. u16 tx_ring_count;
  272. u16 rx_ring_count;
  273. u32 *shadow_vfta;
  274. u32 rss_queues;
  275. /* lock for RX network flow classification filter */
  276. spinlock_t nfc_lock;
  277. struct igc_mac_addr *mac_table;
  278. struct igc_info ei;
  279. };
  280. /* igc_desc_unused - calculate if we have unused descriptors */
  281. static inline u16 igc_desc_unused(const struct igc_ring *ring)
  282. {
  283. u16 ntc = ring->next_to_clean;
  284. u16 ntu = ring->next_to_use;
  285. return ((ntc > ntu) ? 0 : ring->count) + ntc - ntu - 1;
  286. }
  287. static inline struct netdev_queue *txring_txq(const struct igc_ring *tx_ring)
  288. {
  289. return netdev_get_tx_queue(tx_ring->netdev, tx_ring->queue_index);
  290. }
  291. enum igc_ring_flags_t {
  292. IGC_RING_FLAG_RX_3K_BUFFER,
  293. IGC_RING_FLAG_RX_BUILD_SKB_ENABLED,
  294. IGC_RING_FLAG_RX_SCTP_CSUM,
  295. IGC_RING_FLAG_RX_LB_VLAN_BSWAP,
  296. IGC_RING_FLAG_TX_CTX_IDX,
  297. IGC_RING_FLAG_TX_DETECT_HANG
  298. };
  299. #define ring_uses_large_buffer(ring) \
  300. test_bit(IGC_RING_FLAG_RX_3K_BUFFER, &(ring)->flags)
  301. #define ring_uses_build_skb(ring) \
  302. test_bit(IGC_RING_FLAG_RX_BUILD_SKB_ENABLED, &(ring)->flags)
  303. static inline unsigned int igc_rx_bufsz(struct igc_ring *ring)
  304. {
  305. #if (PAGE_SIZE < 8192)
  306. if (ring_uses_large_buffer(ring))
  307. return IGC_RXBUFFER_3072;
  308. if (ring_uses_build_skb(ring))
  309. return IGC_MAX_FRAME_BUILD_SKB + IGC_TS_HDR_LEN;
  310. #endif
  311. return IGC_RXBUFFER_2048;
  312. }
  313. static inline unsigned int igc_rx_pg_order(struct igc_ring *ring)
  314. {
  315. #if (PAGE_SIZE < 8192)
  316. if (ring_uses_large_buffer(ring))
  317. return 1;
  318. #endif
  319. return 0;
  320. }
  321. #define igc_rx_pg_size(_ring) (PAGE_SIZE << igc_rx_pg_order(_ring))
  322. #define IGC_TXD_DCMD (IGC_ADVTXD_DCMD_EOP | IGC_ADVTXD_DCMD_RS)
  323. #define IGC_RX_DESC(R, i) \
  324. (&(((union igc_adv_rx_desc *)((R)->desc))[i]))
  325. #define IGC_TX_DESC(R, i) \
  326. (&(((union igc_adv_tx_desc *)((R)->desc))[i]))
  327. #define IGC_TX_CTXTDESC(R, i) \
  328. (&(((struct igc_adv_tx_context_desc *)((R)->desc))[i]))
  329. #endif /* _IGC_H_ */