ena_netdev.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
  2. /*
  3. * Copyright 2015-2020 Amazon.com, Inc. or its affiliates. All rights reserved.
  4. */
  5. #ifndef ENA_H
  6. #define ENA_H
  7. #include <linux/bitops.h>
  8. #include <linux/dim.h>
  9. #include <linux/etherdevice.h>
  10. #include <linux/if_vlan.h>
  11. #include <linux/inetdevice.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/skbuff.h>
  15. #include <net/xdp.h>
  16. #include <uapi/linux/bpf.h>
  17. #include "ena_com.h"
  18. #include "ena_eth_com.h"
  19. #define DRV_MODULE_GEN_MAJOR 2
  20. #define DRV_MODULE_GEN_MINOR 1
  21. #define DRV_MODULE_GEN_SUBMINOR 0
  22. #define DRV_MODULE_NAME "ena"
  23. #define DEVICE_NAME "Elastic Network Adapter (ENA)"
  24. /* 1 for AENQ + ADMIN */
  25. #define ENA_ADMIN_MSIX_VEC 1
  26. #define ENA_MAX_MSIX_VEC(io_queues) (ENA_ADMIN_MSIX_VEC + (io_queues))
  27. /* The ENA buffer length fields is 16 bit long. So when PAGE_SIZE == 64kB the
  28. * driver passes 0.
  29. * Since the max packet size the ENA handles is ~9kB limit the buffer length to
  30. * 16kB.
  31. */
  32. #if PAGE_SIZE > SZ_16K
  33. #define ENA_PAGE_SIZE (_AC(SZ_16K, UL))
  34. #else
  35. #define ENA_PAGE_SIZE PAGE_SIZE
  36. #endif
  37. #define ENA_MIN_MSIX_VEC 2
  38. #define ENA_REG_BAR 0
  39. #define ENA_MEM_BAR 2
  40. #define ENA_BAR_MASK (BIT(ENA_REG_BAR) | BIT(ENA_MEM_BAR))
  41. #define ENA_DEFAULT_RING_SIZE (1024)
  42. #define ENA_MIN_RING_SIZE (256)
  43. #define ENA_MIN_RX_BUF_SIZE (2048)
  44. #define ENA_MIN_NUM_IO_QUEUES (1)
  45. #define ENA_TX_WAKEUP_THRESH (MAX_SKB_FRAGS + 2)
  46. #define ENA_DEFAULT_RX_COPYBREAK (256 - NET_IP_ALIGN)
  47. #define ENA_MIN_MTU 128
  48. #define ENA_NAME_MAX_LEN 20
  49. #define ENA_IRQNAME_SIZE 40
  50. #define ENA_PKT_MAX_BUFS 19
  51. #define ENA_RX_RSS_TABLE_LOG_SIZE 7
  52. #define ENA_RX_RSS_TABLE_SIZE (1 << ENA_RX_RSS_TABLE_LOG_SIZE)
  53. /* The number of tx packet completions that will be handled each NAPI poll
  54. * cycle is ring_size / ENA_TX_POLL_BUDGET_DIVIDER.
  55. */
  56. #define ENA_TX_POLL_BUDGET_DIVIDER 4
  57. /* Refill Rx queue when number of required descriptors is above
  58. * QUEUE_SIZE / ENA_RX_REFILL_THRESH_DIVIDER or ENA_RX_REFILL_THRESH_PACKET
  59. */
  60. #define ENA_RX_REFILL_THRESH_DIVIDER 8
  61. #define ENA_RX_REFILL_THRESH_PACKET 256
  62. /* Number of queues to check for missing queues per timer service */
  63. #define ENA_MONITORED_TX_QUEUES 4
  64. /* Max timeout packets before device reset */
  65. #define MAX_NUM_OF_TIMEOUTED_PACKETS 128
  66. #define ENA_TX_RING_IDX_NEXT(idx, ring_size) (((idx) + 1) & ((ring_size) - 1))
  67. #define ENA_RX_RING_IDX_NEXT(idx, ring_size) (((idx) + 1) & ((ring_size) - 1))
  68. #define ENA_RX_RING_IDX_ADD(idx, n, ring_size) \
  69. (((idx) + (n)) & ((ring_size) - 1))
  70. #define ENA_IO_TXQ_IDX(q) (2 * (q))
  71. #define ENA_IO_RXQ_IDX(q) (2 * (q) + 1)
  72. #define ENA_IO_TXQ_IDX_TO_COMBINED_IDX(q) ((q) / 2)
  73. #define ENA_IO_RXQ_IDX_TO_COMBINED_IDX(q) (((q) - 1) / 2)
  74. #define ENA_MGMNT_IRQ_IDX 0
  75. #define ENA_IO_IRQ_FIRST_IDX 1
  76. #define ENA_IO_IRQ_IDX(q) (ENA_IO_IRQ_FIRST_IDX + (q))
  77. #define ENA_ADMIN_POLL_DELAY_US 100
  78. /* ENA device should send keep alive msg every 1 sec.
  79. * We wait for 6 sec just to be on the safe side.
  80. */
  81. #define ENA_DEVICE_KALIVE_TIMEOUT (6 * HZ)
  82. #define ENA_MAX_NO_INTERRUPT_ITERATIONS 3
  83. #define ENA_MMIO_DISABLE_REG_READ BIT(0)
  84. struct ena_irq {
  85. irq_handler_t handler;
  86. void *data;
  87. int cpu;
  88. u32 vector;
  89. cpumask_t affinity_hint_mask;
  90. char name[ENA_IRQNAME_SIZE];
  91. };
  92. struct ena_napi {
  93. u8 first_interrupt ____cacheline_aligned;
  94. u8 interrupts_masked;
  95. struct napi_struct napi;
  96. struct ena_ring *tx_ring;
  97. struct ena_ring *rx_ring;
  98. u32 qid;
  99. struct dim dim;
  100. };
  101. struct ena_tx_buffer {
  102. union {
  103. struct sk_buff *skb;
  104. /* XDP buffer structure which is used for sending packets in
  105. * the xdp queues
  106. */
  107. struct xdp_frame *xdpf;
  108. };
  109. /* num of ena desc for this specific skb
  110. * (includes data desc and metadata desc)
  111. */
  112. u32 tx_descs;
  113. /* num of buffers used by this skb */
  114. u32 num_of_bufs;
  115. /* Total size of all buffers in bytes */
  116. u32 total_tx_size;
  117. /* Indicate if bufs[0] map the linear data of the skb. */
  118. u8 map_linear_data;
  119. /* Used for detect missing tx packets to limit the number of prints */
  120. u8 print_once;
  121. /* Save the last jiffies to detect missing tx packets
  122. *
  123. * sets to non zero value on ena_start_xmit and set to zero on
  124. * napi and timer_Service_routine.
  125. *
  126. * while this value is not protected by lock,
  127. * a given packet is not expected to be handled by ena_start_xmit
  128. * and by napi/timer_service at the same time.
  129. */
  130. unsigned long last_jiffies;
  131. struct ena_com_buf bufs[ENA_PKT_MAX_BUFS];
  132. } ____cacheline_aligned;
  133. struct ena_rx_buffer {
  134. struct sk_buff *skb;
  135. struct page *page;
  136. dma_addr_t dma_addr;
  137. u32 page_offset;
  138. u32 buf_offset;
  139. struct ena_com_buf ena_buf;
  140. } ____cacheline_aligned;
  141. struct ena_stats_tx {
  142. u64 cnt;
  143. u64 bytes;
  144. u64 queue_stop;
  145. u64 prepare_ctx_err;
  146. u64 queue_wakeup;
  147. u64 dma_mapping_err;
  148. u64 linearize;
  149. u64 linearize_failed;
  150. u64 napi_comp;
  151. u64 tx_poll;
  152. u64 doorbells;
  153. u64 bad_req_id;
  154. u64 llq_buffer_copy;
  155. u64 missed_tx;
  156. u64 unmask_interrupt;
  157. u64 last_napi_jiffies;
  158. };
  159. struct ena_stats_rx {
  160. u64 cnt;
  161. u64 bytes;
  162. u64 rx_copybreak_pkt;
  163. u64 csum_good;
  164. u64 refil_partial;
  165. u64 csum_bad;
  166. u64 page_alloc_fail;
  167. u64 skb_alloc_fail;
  168. u64 dma_mapping_err;
  169. u64 bad_desc_num;
  170. u64 bad_req_id;
  171. u64 empty_rx_ring;
  172. u64 csum_unchecked;
  173. u64 xdp_aborted;
  174. u64 xdp_drop;
  175. u64 xdp_pass;
  176. u64 xdp_tx;
  177. u64 xdp_invalid;
  178. u64 xdp_redirect;
  179. };
  180. struct ena_ring {
  181. /* Holds the empty requests for TX/RX
  182. * out of order completions
  183. */
  184. u16 *free_ids;
  185. union {
  186. struct ena_tx_buffer *tx_buffer_info;
  187. struct ena_rx_buffer *rx_buffer_info;
  188. };
  189. /* cache ptr to avoid using the adapter */
  190. struct device *dev;
  191. struct pci_dev *pdev;
  192. struct napi_struct *napi;
  193. struct net_device *netdev;
  194. struct ena_com_dev *ena_dev;
  195. struct ena_adapter *adapter;
  196. struct ena_com_io_cq *ena_com_io_cq;
  197. struct ena_com_io_sq *ena_com_io_sq;
  198. struct bpf_prog *xdp_bpf_prog;
  199. struct xdp_rxq_info xdp_rxq;
  200. spinlock_t xdp_tx_lock; /* synchronize XDP TX/Redirect traffic */
  201. /* Used for rx queues only to point to the xdp tx ring, to
  202. * which traffic should be redirected from this rx ring.
  203. */
  204. struct ena_ring *xdp_ring;
  205. u16 next_to_use;
  206. u16 next_to_clean;
  207. u16 rx_copybreak;
  208. u16 rx_headroom;
  209. u16 qid;
  210. u16 mtu;
  211. u16 sgl_size;
  212. /* The maximum header length the device can handle */
  213. u8 tx_max_header_size;
  214. bool disable_meta_caching;
  215. u16 no_interrupt_event_cnt;
  216. /* cpu and NUMA for TPH */
  217. int cpu;
  218. int numa_node;
  219. /* number of tx/rx_buffer_info's entries */
  220. int ring_size;
  221. enum ena_admin_placement_policy_type tx_mem_queue_type;
  222. struct ena_com_rx_buf_info ena_bufs[ENA_PKT_MAX_BUFS];
  223. u32 smoothed_interval;
  224. u32 per_napi_packets;
  225. u16 non_empty_napi_events;
  226. struct u64_stats_sync syncp;
  227. union {
  228. struct ena_stats_tx tx_stats;
  229. struct ena_stats_rx rx_stats;
  230. };
  231. u8 *push_buf_intermediate_buf;
  232. int empty_rx_queue;
  233. } ____cacheline_aligned;
  234. struct ena_stats_dev {
  235. u64 tx_timeout;
  236. u64 suspend;
  237. u64 resume;
  238. u64 wd_expired;
  239. u64 interface_up;
  240. u64 interface_down;
  241. u64 admin_q_pause;
  242. u64 rx_drops;
  243. u64 tx_drops;
  244. u64 reset_fail;
  245. };
  246. enum ena_flags_t {
  247. ENA_FLAG_DEVICE_RUNNING,
  248. ENA_FLAG_DEV_UP,
  249. ENA_FLAG_LINK_UP,
  250. ENA_FLAG_MSIX_ENABLED,
  251. ENA_FLAG_TRIGGER_RESET,
  252. ENA_FLAG_ONGOING_RESET
  253. };
  254. /* adapter specific private data structure */
  255. struct ena_adapter {
  256. struct ena_com_dev *ena_dev;
  257. /* OS defined structs */
  258. struct net_device *netdev;
  259. struct pci_dev *pdev;
  260. /* rx packets that shorter that this len will be copied to the skb
  261. * header
  262. */
  263. u32 rx_copybreak;
  264. u32 max_mtu;
  265. u32 num_io_queues;
  266. u32 max_num_io_queues;
  267. int msix_vecs;
  268. u32 missing_tx_completion_threshold;
  269. u32 requested_tx_ring_size;
  270. u32 requested_rx_ring_size;
  271. u32 max_tx_ring_size;
  272. u32 max_rx_ring_size;
  273. u32 msg_enable;
  274. /* large_llq_header_enabled is used for two purposes:
  275. * 1. Indicates that large LLQ has been requested.
  276. * 2. Indicates whether large LLQ is set or not after device
  277. * initialization / configuration.
  278. */
  279. bool large_llq_header_enabled;
  280. bool large_llq_header_supported;
  281. u16 max_tx_sgl_size;
  282. u16 max_rx_sgl_size;
  283. u8 mac_addr[ETH_ALEN];
  284. unsigned long keep_alive_timeout;
  285. unsigned long missing_tx_completion_to;
  286. char name[ENA_NAME_MAX_LEN];
  287. unsigned long flags;
  288. /* TX */
  289. struct ena_ring tx_ring[ENA_MAX_NUM_IO_QUEUES]
  290. ____cacheline_aligned_in_smp;
  291. /* RX */
  292. struct ena_ring rx_ring[ENA_MAX_NUM_IO_QUEUES]
  293. ____cacheline_aligned_in_smp;
  294. struct ena_napi ena_napi[ENA_MAX_NUM_IO_QUEUES];
  295. struct ena_irq irq_tbl[ENA_MAX_MSIX_VEC(ENA_MAX_NUM_IO_QUEUES)];
  296. /* timer service */
  297. struct work_struct reset_task;
  298. struct timer_list timer_service;
  299. bool wd_state;
  300. bool dev_up_before_reset;
  301. bool disable_meta_caching;
  302. unsigned long last_keep_alive_jiffies;
  303. struct u64_stats_sync syncp;
  304. struct ena_stats_dev dev_stats;
  305. struct ena_admin_eni_stats eni_stats;
  306. struct ena_admin_ena_srd_info ena_srd_info;
  307. /* last queue index that was checked for uncompleted tx packets */
  308. u32 last_monitored_tx_qid;
  309. enum ena_regs_reset_reason_types reset_reason;
  310. struct bpf_prog *xdp_bpf_prog;
  311. u32 xdp_first_ring;
  312. u32 xdp_num_queues;
  313. };
  314. void ena_set_ethtool_ops(struct net_device *netdev);
  315. void ena_dump_stats_to_dmesg(struct ena_adapter *adapter);
  316. void ena_dump_stats_to_buf(struct ena_adapter *adapter, u8 *buf);
  317. int ena_update_queue_params(struct ena_adapter *adapter,
  318. u32 new_tx_size,
  319. u32 new_rx_size,
  320. u32 new_llq_header_len);
  321. int ena_update_queue_count(struct ena_adapter *adapter, u32 new_channel_count);
  322. int ena_set_rx_copybreak(struct ena_adapter *adapter, u32 rx_copybreak);
  323. int ena_get_sset_count(struct net_device *netdev, int sset);
  324. static inline void ena_reset_device(struct ena_adapter *adapter,
  325. enum ena_regs_reset_reason_types reset_reason)
  326. {
  327. adapter->reset_reason = reset_reason;
  328. /* Make sure reset reason is set before triggering the reset */
  329. smp_mb__before_atomic();
  330. set_bit(ENA_FLAG_TRIGGER_RESET, &adapter->flags);
  331. }
  332. int handle_invalid_req_id(struct ena_ring *ring, u16 req_id,
  333. struct ena_tx_buffer *tx_info, bool is_xdp);
  334. /* Increase a stat by cnt while holding syncp seqlock on 32bit machines */
  335. static inline void ena_increase_stat(u64 *statp, u64 cnt,
  336. struct u64_stats_sync *syncp)
  337. {
  338. u64_stats_update_begin(syncp);
  339. (*statp) += cnt;
  340. u64_stats_update_end(syncp);
  341. }
  342. static inline void ena_ring_tx_doorbell(struct ena_ring *tx_ring)
  343. {
  344. ena_com_write_sq_doorbell(tx_ring->ena_com_io_sq);
  345. ena_increase_stat(&tx_ring->tx_stats.doorbells, 1, &tx_ring->syncp);
  346. }
  347. int ena_xmit_common(struct ena_adapter *adapter,
  348. struct ena_ring *ring,
  349. struct ena_tx_buffer *tx_info,
  350. struct ena_com_tx_ctx *ena_tx_ctx,
  351. u16 next_to_use,
  352. u32 bytes);
  353. void ena_unmap_tx_buff(struct ena_ring *tx_ring,
  354. struct ena_tx_buffer *tx_info);
  355. void ena_init_io_rings(struct ena_adapter *adapter,
  356. int first_index, int count);
  357. int ena_create_io_tx_queues_in_range(struct ena_adapter *adapter,
  358. int first_index, int count);
  359. int ena_setup_tx_resources_in_range(struct ena_adapter *adapter,
  360. int first_index, int count);
  361. void ena_free_all_io_tx_resources_in_range(struct ena_adapter *adapter,
  362. int first_index, int count);
  363. void ena_free_all_io_tx_resources(struct ena_adapter *adapter);
  364. void ena_down(struct ena_adapter *adapter);
  365. int ena_up(struct ena_adapter *adapter);
  366. void ena_unmask_interrupt(struct ena_ring *tx_ring, struct ena_ring *rx_ring);
  367. void ena_update_ring_numa_node(struct ena_ring *tx_ring,
  368. struct ena_ring *rx_ring);
  369. #endif /* !(ENA_H) */