igc.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. /* The largest size we can write to the descriptor is 65535. In order to
  105. * maintain a power of two alignment we have to limit ourselves to 32K.
  106. */
  107. #define IGC_MAX_TXD_PWR 15
  108. #define IGC_MAX_DATA_PER_TXD BIT(IGC_MAX_TXD_PWR)
  109. /* Tx Descriptors needed, worst case */
  110. #define TXD_USE_COUNT(S) DIV_ROUND_UP((S), IGC_MAX_DATA_PER_TXD)
  111. #define DESC_NEEDED (MAX_SKB_FRAGS + 4)
  112. /* wrapper around a pointer to a socket buffer,
  113. * so a DMA handle can be stored along with the buffer
  114. */
  115. struct igc_tx_buffer {
  116. union igc_adv_tx_desc *next_to_watch;
  117. unsigned long time_stamp;
  118. struct sk_buff *skb;
  119. unsigned int bytecount;
  120. u16 gso_segs;
  121. __be16 protocol;
  122. DEFINE_DMA_UNMAP_ADDR(dma);
  123. DEFINE_DMA_UNMAP_LEN(len);
  124. u32 tx_flags;
  125. };
  126. struct igc_rx_buffer {
  127. dma_addr_t dma;
  128. struct page *page;
  129. #if (BITS_PER_LONG > 32) || (PAGE_SIZE >= 65536)
  130. __u32 page_offset;
  131. #else
  132. __u16 page_offset;
  133. #endif
  134. __u16 pagecnt_bias;
  135. };
  136. struct igc_tx_queue_stats {
  137. u64 packets;
  138. u64 bytes;
  139. u64 restart_queue;
  140. u64 restart_queue2;
  141. };
  142. struct igc_rx_queue_stats {
  143. u64 packets;
  144. u64 bytes;
  145. u64 drops;
  146. u64 csum_err;
  147. u64 alloc_failed;
  148. };
  149. struct igc_rx_packet_stats {
  150. u64 ipv4_packets; /* IPv4 headers processed */
  151. u64 ipv4e_packets; /* IPv4E headers with extensions processed */
  152. u64 ipv6_packets; /* IPv6 headers processed */
  153. u64 ipv6e_packets; /* IPv6E headers with extensions processed */
  154. u64 tcp_packets; /* TCP headers processed */
  155. u64 udp_packets; /* UDP headers processed */
  156. u64 sctp_packets; /* SCTP headers processed */
  157. u64 nfs_packets; /* NFS headers processe */
  158. u64 other_packets;
  159. };
  160. struct igc_ring_container {
  161. struct igc_ring *ring; /* pointer to linked list of rings */
  162. unsigned int total_bytes; /* total bytes processed this int */
  163. unsigned int total_packets; /* total packets processed this int */
  164. u16 work_limit; /* total work allowed per interrupt */
  165. u8 count; /* total number of rings in vector */
  166. u8 itr; /* current ITR setting for ring */
  167. };
  168. struct igc_ring {
  169. struct igc_q_vector *q_vector; /* backlink to q_vector */
  170. struct net_device *netdev; /* back pointer to net_device */
  171. struct device *dev; /* device for dma mapping */
  172. union { /* array of buffer info structs */
  173. struct igc_tx_buffer *tx_buffer_info;
  174. struct igc_rx_buffer *rx_buffer_info;
  175. };
  176. void *desc; /* descriptor ring memory */
  177. unsigned long flags; /* ring specific flags */
  178. void __iomem *tail; /* pointer to ring tail register */
  179. dma_addr_t dma; /* phys address of the ring */
  180. unsigned int size; /* length of desc. ring in bytes */
  181. u16 count; /* number of desc. in the ring */
  182. u8 queue_index; /* logical index of the ring*/
  183. u8 reg_idx; /* physical index of the ring */
  184. /* everything past this point are written often */
  185. u16 next_to_clean;
  186. u16 next_to_use;
  187. u16 next_to_alloc;
  188. union {
  189. /* TX */
  190. struct {
  191. struct igc_tx_queue_stats tx_stats;
  192. struct u64_stats_sync tx_syncp;
  193. struct u64_stats_sync tx_syncp2;
  194. };
  195. /* RX */
  196. struct {
  197. struct igc_rx_queue_stats rx_stats;
  198. struct igc_rx_packet_stats pkt_stats;
  199. struct u64_stats_sync rx_syncp;
  200. struct sk_buff *skb;
  201. };
  202. };
  203. } ____cacheline_internodealigned_in_smp;
  204. struct igc_q_vector {
  205. struct igc_adapter *adapter; /* backlink */
  206. void __iomem *itr_register;
  207. u32 eims_value; /* EIMS mask value */
  208. u16 itr_val;
  209. u8 set_itr;
  210. struct igc_ring_container rx, tx;
  211. struct napi_struct napi;
  212. struct rcu_head rcu; /* to avoid race with update stats on free */
  213. char name[IFNAMSIZ + 9];
  214. struct net_device poll_dev;
  215. /* for dynamic allocation of rings associated with this q_vector */
  216. struct igc_ring ring[0] ____cacheline_internodealigned_in_smp;
  217. };
  218. struct igc_mac_addr {
  219. u8 addr[ETH_ALEN];
  220. u8 queue;
  221. u8 state; /* bitmask */
  222. };
  223. #define IGC_MAC_STATE_DEFAULT 0x1
  224. #define IGC_MAC_STATE_MODIFIED 0x2
  225. #define IGC_MAC_STATE_IN_USE 0x4
  226. /* Board specific private data structure */
  227. struct igc_adapter {
  228. struct net_device *netdev;
  229. unsigned long state;
  230. unsigned int flags;
  231. unsigned int num_q_vectors;
  232. struct msix_entry *msix_entries;
  233. /* TX */
  234. u16 tx_work_limit;
  235. int num_tx_queues;
  236. struct igc_ring *tx_ring[IGC_MAX_TX_QUEUES];
  237. /* RX */
  238. int num_rx_queues;
  239. struct igc_ring *rx_ring[IGC_MAX_RX_QUEUES];
  240. struct timer_list watchdog_timer;
  241. struct timer_list dma_err_timer;
  242. struct timer_list phy_info_timer;
  243. u16 link_speed;
  244. u16 link_duplex;
  245. u8 port_num;
  246. u8 __iomem *io_addr;
  247. /* Interrupt Throttle Rate */
  248. u32 rx_itr_setting;
  249. u32 tx_itr_setting;
  250. struct work_struct reset_task;
  251. struct work_struct watchdog_task;
  252. struct work_struct dma_err_task;
  253. u8 tx_timeout_factor;
  254. int msg_enable;
  255. u32 max_frame_size;
  256. u32 min_frame_size;
  257. /* OS defined structs */
  258. struct pci_dev *pdev;
  259. /* lock for statistics */
  260. spinlock_t stats64_lock;
  261. struct rtnl_link_stats64 stats64;
  262. /* structs defined in igc_hw.h */
  263. struct igc_hw hw;
  264. struct igc_hw_stats stats;
  265. struct igc_q_vector *q_vector[MAX_Q_VECTORS];
  266. u32 eims_enable_mask;
  267. u32 eims_other;
  268. u16 tx_ring_count;
  269. u16 rx_ring_count;
  270. u32 *shadow_vfta;
  271. u32 rss_queues;
  272. /* lock for RX network flow classification filter */
  273. spinlock_t nfc_lock;
  274. struct igc_mac_addr *mac_table;
  275. };
  276. /* igc_desc_unused - calculate if we have unused descriptors */
  277. static inline u16 igc_desc_unused(const struct igc_ring *ring)
  278. {
  279. u16 ntc = ring->next_to_clean;
  280. u16 ntu = ring->next_to_use;
  281. return ((ntc > ntu) ? 0 : ring->count) + ntc - ntu - 1;
  282. }
  283. static inline struct netdev_queue *txring_txq(const struct igc_ring *tx_ring)
  284. {
  285. return netdev_get_tx_queue(tx_ring->netdev, tx_ring->queue_index);
  286. }
  287. enum igc_ring_flags_t {
  288. IGC_RING_FLAG_RX_3K_BUFFER,
  289. IGC_RING_FLAG_RX_BUILD_SKB_ENABLED,
  290. IGC_RING_FLAG_RX_SCTP_CSUM,
  291. IGC_RING_FLAG_RX_LB_VLAN_BSWAP,
  292. IGC_RING_FLAG_TX_CTX_IDX,
  293. IGC_RING_FLAG_TX_DETECT_HANG
  294. };
  295. #define ring_uses_large_buffer(ring) \
  296. test_bit(IGC_RING_FLAG_RX_3K_BUFFER, &(ring)->flags)
  297. #define ring_uses_build_skb(ring) \
  298. test_bit(IGC_RING_FLAG_RX_BUILD_SKB_ENABLED, &(ring)->flags)
  299. static inline unsigned int igc_rx_bufsz(struct igc_ring *ring)
  300. {
  301. #if (PAGE_SIZE < 8192)
  302. if (ring_uses_large_buffer(ring))
  303. return IGC_RXBUFFER_3072;
  304. if (ring_uses_build_skb(ring))
  305. return IGC_MAX_FRAME_BUILD_SKB + IGC_TS_HDR_LEN;
  306. #endif
  307. return IGC_RXBUFFER_2048;
  308. }
  309. static inline unsigned int igc_rx_pg_order(struct igc_ring *ring)
  310. {
  311. #if (PAGE_SIZE < 8192)
  312. if (ring_uses_large_buffer(ring))
  313. return 1;
  314. #endif
  315. return 0;
  316. }
  317. #define igc_rx_pg_size(_ring) (PAGE_SIZE << igc_rx_pg_order(_ring))
  318. #define IGC_TXD_DCMD (IGC_ADVTXD_DCMD_EOP | IGC_ADVTXD_DCMD_RS)
  319. #define IGC_RX_DESC(R, i) \
  320. (&(((union igc_adv_rx_desc *)((R)->desc))[i]))
  321. #define IGC_TX_DESC(R, i) \
  322. (&(((union igc_adv_tx_desc *)((R)->desc))[i]))
  323. #define IGC_TX_CTXTDESC(R, i) \
  324. (&(((struct igc_adv_tx_context_desc *)((R)->desc))[i]))
  325. #endif /* _IGC_H_ */