ena_netdev.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * Copyright 2015 Amazon.com, Inc. or its affiliates.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #ifndef ENA_H
  33. #define ENA_H
  34. #include <linux/bitops.h>
  35. #include <linux/net_dim.h>
  36. #include <linux/etherdevice.h>
  37. #include <linux/inetdevice.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/netdevice.h>
  40. #include <linux/skbuff.h>
  41. #include "ena_com.h"
  42. #include "ena_eth_com.h"
  43. #define DRV_MODULE_VER_MAJOR 2
  44. #define DRV_MODULE_VER_MINOR 1
  45. #define DRV_MODULE_VER_SUBMINOR 0
  46. #define DRV_MODULE_NAME "ena"
  47. #ifndef DRV_MODULE_VERSION
  48. #define DRV_MODULE_VERSION \
  49. __stringify(DRV_MODULE_VER_MAJOR) "." \
  50. __stringify(DRV_MODULE_VER_MINOR) "." \
  51. __stringify(DRV_MODULE_VER_SUBMINOR) "K"
  52. #endif
  53. #define DEVICE_NAME "Elastic Network Adapter (ENA)"
  54. /* 1 for AENQ + ADMIN */
  55. #define ENA_ADMIN_MSIX_VEC 1
  56. #define ENA_MAX_MSIX_VEC(io_queues) (ENA_ADMIN_MSIX_VEC + (io_queues))
  57. /* The ENA buffer length fields is 16 bit long. So when PAGE_SIZE == 64kB the
  58. * driver passes 0.
  59. * Since the max packet size the ENA handles is ~9kB limit the buffer length to
  60. * 16kB.
  61. */
  62. #if PAGE_SIZE > SZ_16K
  63. #define ENA_PAGE_SIZE SZ_16K
  64. #else
  65. #define ENA_PAGE_SIZE PAGE_SIZE
  66. #endif
  67. #define ENA_MIN_MSIX_VEC 2
  68. #define ENA_REG_BAR 0
  69. #define ENA_MEM_BAR 2
  70. #define ENA_BAR_MASK (BIT(ENA_REG_BAR) | BIT(ENA_MEM_BAR))
  71. #define ENA_DEFAULT_RING_SIZE (1024)
  72. #define ENA_MIN_RING_SIZE (256)
  73. #define ENA_TX_WAKEUP_THRESH (MAX_SKB_FRAGS + 2)
  74. #define ENA_DEFAULT_RX_COPYBREAK (256 - NET_IP_ALIGN)
  75. /* limit the buffer size to 600 bytes to handle MTU changes from very
  76. * small to very large, in which case the number of buffers per packet
  77. * could exceed ENA_PKT_MAX_BUFS
  78. */
  79. #define ENA_DEFAULT_MIN_RX_BUFF_ALLOC_SIZE 600
  80. #define ENA_MIN_MTU 128
  81. #define ENA_NAME_MAX_LEN 20
  82. #define ENA_IRQNAME_SIZE 40
  83. #define ENA_PKT_MAX_BUFS 19
  84. #define ENA_RX_RSS_TABLE_LOG_SIZE 7
  85. #define ENA_RX_RSS_TABLE_SIZE (1 << ENA_RX_RSS_TABLE_LOG_SIZE)
  86. #define ENA_HASH_KEY_SIZE 40
  87. /* The number of tx packet completions that will be handled each NAPI poll
  88. * cycle is ring_size / ENA_TX_POLL_BUDGET_DIVIDER.
  89. */
  90. #define ENA_TX_POLL_BUDGET_DIVIDER 4
  91. /* Refill Rx queue when number of required descriptors is above
  92. * QUEUE_SIZE / ENA_RX_REFILL_THRESH_DIVIDER or ENA_RX_REFILL_THRESH_PACKET
  93. */
  94. #define ENA_RX_REFILL_THRESH_DIVIDER 8
  95. #define ENA_RX_REFILL_THRESH_PACKET 256
  96. /* Number of queues to check for missing queues per timer service */
  97. #define ENA_MONITORED_TX_QUEUES 4
  98. /* Max timeout packets before device reset */
  99. #define MAX_NUM_OF_TIMEOUTED_PACKETS 128
  100. #define ENA_TX_RING_IDX_NEXT(idx, ring_size) (((idx) + 1) & ((ring_size) - 1))
  101. #define ENA_RX_RING_IDX_NEXT(idx, ring_size) (((idx) + 1) & ((ring_size) - 1))
  102. #define ENA_RX_RING_IDX_ADD(idx, n, ring_size) \
  103. (((idx) + (n)) & ((ring_size) - 1))
  104. #define ENA_IO_TXQ_IDX(q) (2 * (q))
  105. #define ENA_IO_RXQ_IDX(q) (2 * (q) + 1)
  106. #define ENA_IO_TXQ_IDX_TO_COMBINED_IDX(q) ((q) / 2)
  107. #define ENA_IO_RXQ_IDX_TO_COMBINED_IDX(q) (((q) - 1) / 2)
  108. #define ENA_MGMNT_IRQ_IDX 0
  109. #define ENA_IO_IRQ_FIRST_IDX 1
  110. #define ENA_IO_IRQ_IDX(q) (ENA_IO_IRQ_FIRST_IDX + (q))
  111. /* ENA device should send keep alive msg every 1 sec.
  112. * We wait for 6 sec just to be on the safe side.
  113. */
  114. #define ENA_DEVICE_KALIVE_TIMEOUT (6 * HZ)
  115. #define ENA_MAX_NO_INTERRUPT_ITERATIONS 3
  116. #define ENA_MMIO_DISABLE_REG_READ BIT(0)
  117. struct ena_irq {
  118. irq_handler_t handler;
  119. void *data;
  120. int cpu;
  121. u32 vector;
  122. cpumask_t affinity_hint_mask;
  123. char name[ENA_IRQNAME_SIZE];
  124. };
  125. struct ena_napi {
  126. struct napi_struct napi ____cacheline_aligned;
  127. struct ena_ring *tx_ring;
  128. struct ena_ring *rx_ring;
  129. u32 qid;
  130. struct net_dim dim;
  131. };
  132. struct ena_calc_queue_size_ctx {
  133. struct ena_com_dev_get_features_ctx *get_feat_ctx;
  134. struct ena_com_dev *ena_dev;
  135. struct pci_dev *pdev;
  136. u16 tx_queue_size;
  137. u16 rx_queue_size;
  138. u16 max_tx_queue_size;
  139. u16 max_rx_queue_size;
  140. u16 max_tx_sgl_size;
  141. u16 max_rx_sgl_size;
  142. };
  143. struct ena_tx_buffer {
  144. struct sk_buff *skb;
  145. /* num of ena desc for this specific skb
  146. * (includes data desc and metadata desc)
  147. */
  148. u32 tx_descs;
  149. /* num of buffers used by this skb */
  150. u32 num_of_bufs;
  151. /* Indicate if bufs[0] map the linear data of the skb. */
  152. u8 map_linear_data;
  153. /* Used for detect missing tx packets to limit the number of prints */
  154. u32 print_once;
  155. /* Save the last jiffies to detect missing tx packets
  156. *
  157. * sets to non zero value on ena_start_xmit and set to zero on
  158. * napi and timer_Service_routine.
  159. *
  160. * while this value is not protected by lock,
  161. * a given packet is not expected to be handled by ena_start_xmit
  162. * and by napi/timer_service at the same time.
  163. */
  164. unsigned long last_jiffies;
  165. struct ena_com_buf bufs[ENA_PKT_MAX_BUFS];
  166. } ____cacheline_aligned;
  167. struct ena_rx_buffer {
  168. struct sk_buff *skb;
  169. struct page *page;
  170. u32 page_offset;
  171. struct ena_com_buf ena_buf;
  172. } ____cacheline_aligned;
  173. struct ena_stats_tx {
  174. u64 cnt;
  175. u64 bytes;
  176. u64 queue_stop;
  177. u64 prepare_ctx_err;
  178. u64 queue_wakeup;
  179. u64 dma_mapping_err;
  180. u64 linearize;
  181. u64 linearize_failed;
  182. u64 napi_comp;
  183. u64 tx_poll;
  184. u64 doorbells;
  185. u64 bad_req_id;
  186. u64 llq_buffer_copy;
  187. u64 missed_tx;
  188. };
  189. struct ena_stats_rx {
  190. u64 cnt;
  191. u64 bytes;
  192. u64 rx_copybreak_pkt;
  193. u64 csum_good;
  194. u64 refil_partial;
  195. u64 bad_csum;
  196. u64 page_alloc_fail;
  197. u64 skb_alloc_fail;
  198. u64 dma_mapping_err;
  199. u64 bad_desc_num;
  200. u64 bad_req_id;
  201. u64 empty_rx_ring;
  202. u64 csum_unchecked;
  203. };
  204. struct ena_ring {
  205. /* Holds the empty requests for TX/RX
  206. * out of order completions
  207. */
  208. u16 *free_ids;
  209. union {
  210. struct ena_tx_buffer *tx_buffer_info;
  211. struct ena_rx_buffer *rx_buffer_info;
  212. };
  213. /* cache ptr to avoid using the adapter */
  214. struct device *dev;
  215. struct pci_dev *pdev;
  216. struct napi_struct *napi;
  217. struct net_device *netdev;
  218. struct ena_com_dev *ena_dev;
  219. struct ena_adapter *adapter;
  220. struct ena_com_io_cq *ena_com_io_cq;
  221. struct ena_com_io_sq *ena_com_io_sq;
  222. u16 next_to_use;
  223. u16 next_to_clean;
  224. u16 rx_copybreak;
  225. u16 qid;
  226. u16 mtu;
  227. u16 sgl_size;
  228. /* The maximum header length the device can handle */
  229. u8 tx_max_header_size;
  230. bool first_interrupt;
  231. u16 no_interrupt_event_cnt;
  232. /* cpu for TPH */
  233. int cpu;
  234. /* number of tx/rx_buffer_info's entries */
  235. int ring_size;
  236. enum ena_admin_placement_policy_type tx_mem_queue_type;
  237. struct ena_com_rx_buf_info ena_bufs[ENA_PKT_MAX_BUFS];
  238. u32 smoothed_interval;
  239. u32 per_napi_packets;
  240. u16 non_empty_napi_events;
  241. struct u64_stats_sync syncp;
  242. union {
  243. struct ena_stats_tx tx_stats;
  244. struct ena_stats_rx rx_stats;
  245. };
  246. u8 *push_buf_intermediate_buf;
  247. int empty_rx_queue;
  248. } ____cacheline_aligned;
  249. struct ena_stats_dev {
  250. u64 tx_timeout;
  251. u64 suspend;
  252. u64 resume;
  253. u64 wd_expired;
  254. u64 interface_up;
  255. u64 interface_down;
  256. u64 admin_q_pause;
  257. u64 rx_drops;
  258. };
  259. enum ena_flags_t {
  260. ENA_FLAG_DEVICE_RUNNING,
  261. ENA_FLAG_DEV_UP,
  262. ENA_FLAG_LINK_UP,
  263. ENA_FLAG_MSIX_ENABLED,
  264. ENA_FLAG_TRIGGER_RESET,
  265. ENA_FLAG_ONGOING_RESET
  266. };
  267. /* adapter specific private data structure */
  268. struct ena_adapter {
  269. struct ena_com_dev *ena_dev;
  270. /* OS defined structs */
  271. struct net_device *netdev;
  272. struct pci_dev *pdev;
  273. /* rx packets that shorter that this len will be copied to the skb
  274. * header
  275. */
  276. u32 rx_copybreak;
  277. u32 max_mtu;
  278. int num_queues;
  279. int msix_vecs;
  280. u32 missing_tx_completion_threshold;
  281. u32 requested_tx_ring_size;
  282. u32 requested_rx_ring_size;
  283. u32 max_tx_ring_size;
  284. u32 max_rx_ring_size;
  285. u32 msg_enable;
  286. u16 max_tx_sgl_size;
  287. u16 max_rx_sgl_size;
  288. u8 mac_addr[ETH_ALEN];
  289. unsigned long keep_alive_timeout;
  290. unsigned long missing_tx_completion_to;
  291. char name[ENA_NAME_MAX_LEN];
  292. unsigned long flags;
  293. /* TX */
  294. struct ena_ring tx_ring[ENA_MAX_NUM_IO_QUEUES]
  295. ____cacheline_aligned_in_smp;
  296. /* RX */
  297. struct ena_ring rx_ring[ENA_MAX_NUM_IO_QUEUES]
  298. ____cacheline_aligned_in_smp;
  299. struct ena_napi ena_napi[ENA_MAX_NUM_IO_QUEUES];
  300. struct ena_irq irq_tbl[ENA_MAX_MSIX_VEC(ENA_MAX_NUM_IO_QUEUES)];
  301. /* timer service */
  302. struct work_struct reset_task;
  303. struct timer_list timer_service;
  304. bool wd_state;
  305. bool dev_up_before_reset;
  306. unsigned long last_keep_alive_jiffies;
  307. struct u64_stats_sync syncp;
  308. struct ena_stats_dev dev_stats;
  309. /* last queue index that was checked for uncompleted tx packets */
  310. u32 last_monitored_tx_qid;
  311. enum ena_regs_reset_reason_types reset_reason;
  312. };
  313. void ena_set_ethtool_ops(struct net_device *netdev);
  314. void ena_dump_stats_to_dmesg(struct ena_adapter *adapter);
  315. void ena_dump_stats_to_buf(struct ena_adapter *adapter, u8 *buf);
  316. int ena_update_queue_sizes(struct ena_adapter *adapter,
  317. u32 new_tx_size,
  318. u32 new_rx_size);
  319. int ena_get_sset_count(struct net_device *netdev, int sset);
  320. #endif /* !(ENA_H) */