ena: Amazon Elastic Network Adapter (ENA) native driver
[vpp.git] / src / plugins / dev_ena / ena.h
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright (c) 2023 Cisco Systems, Inc.
3  */
4
5 #ifndef _ENA_H_
6 #define _ENA_H_
7
8 #include <vppinfra/clib.h>
9 #include <vppinfra/error_bootstrap.h>
10 #include <vppinfra/format.h>
11 #include <vnet/vnet.h>
12 #include <vnet/dev/types.h>
13 #include <dev_ena/ena_defs.h>
14
15 #define ENA_ADMIN_QUEUE_LOG2_DEPTH 2
16 #define ENA_ASYNC_QUEUE_LOG2_DEPTH 5
17 #define ENA_ADMIN_QUEUE_DEPTH      (1 << ENA_ADMIN_QUEUE_LOG2_DEPTH)
18 #define ENA_ASYNC_QUEUE_DEPTH      (1 << ENA_ASYNC_QUEUE_LOG2_DEPTH)
19
20 typedef struct
21 {
22   u8 readless : 1;
23   u8 aq_started : 1;
24   u8 aenq_started : 1;
25   u8 llq : 1;
26
27   void *reg_bar;
28
29   /* mmio */
30   ena_mmio_resp_t *mmio_resp;
31
32   /* admin queue */
33   struct
34   {
35     ena_aq_sq_entry_t *sq_entries;
36     ena_aq_cq_entry_t *cq_entries;
37     u16 sq_next;
38     u16 cq_head;
39     u16 depth;
40   } aq;
41
42   /* host info */
43   ena_aq_host_info_t *host_info;
44
45   /* device info */
46   ena_aq_feat_device_attr_t dev_attr;
47
48   /* async event notification */
49   struct
50   {
51     ena_aenq_entry_t *entries;
52     u16 head;
53     u16 depth;
54     f64 last_keepalive;
55     u64 tx_drops, tx_drops0;
56     u64 rx_drops, rx_drops0;
57   } aenq;
58
59 } ena_device_t;
60
61 typedef struct
62 {
63 } ena_port_t;
64
65 typedef struct
66 {
67   u32 *buffer_indices;
68   u16 *compl_sqe_indices;
69   ena_rx_desc_t *sqes;
70   ena_rx_cdesc_t *cqes;
71   u32 *sq_db;
72   u32 sq_next;
73   u32 cq_next;
74   u16 cq_idx;
75   u16 sq_idx;
76   u16 n_compl_sqes;
77   u8 cq_created : 1;
78   u8 sq_created : 1;
79 } ena_rxq_t;
80
81 typedef struct
82 {
83   u32 *buffer_indices;
84   ena_tx_desc_t *sqes;
85   ena_tx_llq_desc128_t *llq_descs;
86   ena_tx_cdesc_t *cqes;
87   u64 *sqe_templates;
88   u32 *sq_db;
89   u32 sq_tail;
90   u32 sq_head;
91   u32 cq_next;
92   u16 cq_idx;
93   u16 sq_idx;
94   u8 cq_created : 1;
95   u8 sq_created : 1;
96   u8 llq : 1;
97 } ena_txq_t;
98
99 typedef struct
100 {
101   u16 qid;
102   u16 next_index;
103   u32 hw_if_index;
104   ena_rx_cdesc_status_t status;
105   u16 length;
106   u16 n_desc;
107   u16 req_id;
108 } ena_rx_trace_t;
109
110 /* admin.c */
111 typedef struct
112 {
113   char *name;
114   u8 version;
115   u8 data_sz;
116   u8 get;
117   u8 set;
118 } ena_aq_feat_info_t;
119
120 ena_aq_feat_info_t *ena_aq_get_feat_info (ena_aq_feature_id_t);
121 vnet_dev_rv_t ena_aq_olloc (vlib_main_t *, vnet_dev_t *, u16);
122 vnet_dev_rv_t ena_aq_start (vlib_main_t *, vnet_dev_t *);
123 void ena_aq_stop (vlib_main_t *, vnet_dev_t *);
124 void ena_aq_free (vlib_main_t *, vnet_dev_t *);
125 vnet_dev_rv_t ena_aq_create_sq (vlib_main_t *, vnet_dev_t *,
126                                 ena_aq_create_sq_cmd_t *,
127                                 ena_aq_create_sq_resp_t *);
128 vnet_dev_rv_t ena_aq_create_cq (vlib_main_t *, vnet_dev_t *,
129                                 ena_aq_create_cq_cmd_t *,
130                                 ena_aq_create_cq_resp_t *);
131 vnet_dev_rv_t ena_aq_destroy_sq (vlib_main_t *, vnet_dev_t *,
132                                  ena_aq_destroy_sq_cmd_t *);
133 vnet_dev_rv_t ena_aq_destroy_cq (vlib_main_t *, vnet_dev_t *,
134                                  ena_aq_destroy_cq_cmd_t *);
135 vnet_dev_rv_t ena_aq_set_feature (vlib_main_t *, vnet_dev_t *,
136                                   ena_aq_feature_id_t, void *);
137 vnet_dev_rv_t ena_aq_get_feature (vlib_main_t *, vnet_dev_t *,
138                                   ena_aq_feature_id_t, void *);
139 vnet_dev_rv_t ena_aq_get_stats (vlib_main_t *, vnet_dev_t *,
140                                 ena_aq_stats_type_t, ena_aq_stats_scope_t, u16,
141                                 void *);
142
143 /* aenq.c */
144 vnet_dev_rv_t ena_aenq_olloc (vlib_main_t *, vnet_dev_t *, u16);
145 vnet_dev_rv_t ena_aenq_start (vlib_main_t *, vnet_dev_t *);
146 void ena_aenq_stop (vlib_main_t *, vnet_dev_t *);
147 void ena_aenq_free (vlib_main_t *, vnet_dev_t *);
148
149 /* reg.c */
150 void ena_reg_write (vnet_dev_t *, ena_reg_t, void *);
151 void ena_reg_read (vnet_dev_t *, ena_reg_t, const void *);
152 void ena_reg_set_dma_addr (vlib_main_t *, vnet_dev_t *, u32, u32, void *);
153 vnet_dev_rv_t ena_reg_reset (vlib_main_t *, vnet_dev_t *, ena_reset_reason_t);
154
155 /* port.c */
156 vnet_dev_rv_t ena_port_init (vlib_main_t *, vnet_dev_port_t *);
157 vnet_dev_rv_t ena_port_start (vlib_main_t *, vnet_dev_port_t *);
158 void ena_port_stop (vlib_main_t *, vnet_dev_port_t *);
159 vnet_dev_rv_t ena_port_cfg_change (vlib_main_t *, vnet_dev_port_t *,
160                                    vnet_dev_port_cfg_change_req_t *);
161 vnet_dev_rv_t ena_port_cfg_change_validate (vlib_main_t *, vnet_dev_port_t *,
162                                             vnet_dev_port_cfg_change_req_t *);
163
164 /* queue.c */
165 vnet_dev_rv_t ena_rx_queue_alloc (vlib_main_t *, vnet_dev_rx_queue_t *);
166 vnet_dev_rv_t ena_tx_queue_alloc (vlib_main_t *, vnet_dev_tx_queue_t *);
167 void ena_rx_queue_free (vlib_main_t *, vnet_dev_rx_queue_t *);
168 void ena_tx_queue_free (vlib_main_t *, vnet_dev_tx_queue_t *);
169 vnet_dev_rv_t ena_rx_queue_start (vlib_main_t *, vnet_dev_rx_queue_t *);
170 vnet_dev_rv_t ena_tx_queue_start (vlib_main_t *, vnet_dev_tx_queue_t *);
171 void ena_rx_queue_stop (vlib_main_t *, vnet_dev_rx_queue_t *);
172 void ena_tx_queue_stop (vlib_main_t *, vnet_dev_tx_queue_t *);
173
174 /* format.c */
175 format_function_t format_ena_dev_info;
176 format_function_t format_ena_mem_addr;
177 format_function_t format_ena_tx_desc;
178 format_function_t format_ena_rx_trace;
179
180 /* format_admin.c */
181 format_function_t format_ena_aq_feat_desc;
182 format_function_t format_ena_aq_feat_name;
183 format_function_t format_ena_aq_opcode;
184 format_function_t format_ena_aq_status;
185 format_function_t format_ena_aq_feat_id_bitmap;
186 format_function_t format_ena_aq_create_sq_cmd;
187 format_function_t format_ena_aq_create_cq_cmd;
188 format_function_t format_ena_aq_create_sq_resp;
189 format_function_t format_ena_aq_create_cq_resp;
190 format_function_t format_ena_aq_destroy_sq_cmd;
191 format_function_t format_ena_aq_destroy_cq_cmd;
192 format_function_t format_ena_aq_basic_stats;
193 format_function_t format_ena_aq_eni_stats;
194
195 #define foreach_ena_rx_node_counter                                           \
196   _ (BUFFER_ALLOC, buffer_alloc, ERROR, "buffer alloc error")
197
198 typedef enum
199 {
200 #define _(f, lf, t, s) ENA_RX_NODE_CTR_##f,
201   foreach_ena_rx_node_counter
202 #undef _
203     ENA_RX_NODE_N_CTRS,
204 } ena_rx_node_ctr_t;
205
206 #define foreach_ena_tx_node_counter                                           \
207   _ (CHAIN_TOO_LONG, chain_too_long, ERROR, "buffer chain too long")          \
208   _ (NO_FREE_SLOTS, no_free_slots, ERROR, "no free tx slots")
209
210 typedef enum
211 {
212 #define _(f, lf, t, s) ENA_TX_NODE_CTR_##f,
213   foreach_ena_tx_node_counter
214 #undef _
215     ENA_TX_NODE_N_CTRS,
216 } ena_tx_node_ctr_t;
217
218 #define log_debug(dev, f, ...)                                                \
219   vlib_log (VLIB_LOG_LEVEL_DEBUG, ena_log.class, "%U" f, format_vnet_dev_log, \
220             (dev), clib_string_skip_prefix (__func__, "ena_"), ##__VA_ARGS__)
221 #define log_info(dev, f, ...)                                                 \
222   vlib_log (VLIB_LOG_LEVEL_INFO, ena_log.class, "%U: " f,                     \
223             format_vnet_dev_addr, (dev), ##__VA_ARGS__)
224 #define log_notice(dev, f, ...)                                               \
225   vlib_log (VLIB_LOG_LEVEL_NOTICE, ena_log.class, "%U: " f,                   \
226             format_vnet_dev_addr, (dev), ##__VA_ARGS__)
227 #define log_warn(dev, f, ...)                                                 \
228   vlib_log (VLIB_LOG_LEVEL_WARNING, ena_log.class, "%U: " f,                  \
229             format_vnet_dev_addr, (dev), ##__VA_ARGS__)
230 #define log_err(dev, f, ...)                                                  \
231   vlib_log (VLIB_LOG_LEVEL_ERR, ena_log.class, "%U: " f,                      \
232             format_vnet_dev_addr, (dev), ##__VA_ARGS__)
233
234 #endif /* _ENA_H_ */