1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
/* SPDX-License-Identifier: Apache-2.0
* Copyright(c) 2023 Cisco Systems, Inc.
*/
#include <vlib/vlib.h>
#include <vnet/dev/dev.h>
#include <dev_ena/ena.h>
#include <dev_ena/ena_inlines.h>
VLIB_REGISTER_LOG_CLASS (ena_log, static) = {
.class_name = "ena",
.subclass_name = "queue",
};
void
ena_rx_queue_free (vlib_main_t *vm, vnet_dev_rx_queue_t *rxq)
{
ena_rxq_t *eq = vnet_dev_get_rx_queue_data (rxq);
vnet_dev_port_t *port = rxq->port;
vnet_dev_t *dev = port->dev;
ASSERT (rxq->started == 0);
ASSERT (eq->cq_created == 0);
ASSERT (eq->sq_created == 0);
log_debug (dev, "queue %u", rxq->queue_id);
foreach_pointer (p, eq->buffer_indices, eq->compl_sqe_indices)
if (p)
clib_mem_free (p);
foreach_pointer (p, eq->cqes, eq->sqes)
vnet_dev_dma_mem_free (vm, dev, p);
}
vnet_dev_rv_t
ena_rx_queue_alloc (vlib_main_t *vm, vnet_dev_rx_queue_t *rxq)
{
vnet_dev_port_t *port = rxq->port;
vnet_dev_t *dev = port->dev;
ena_rxq_t *eq = vnet_dev_get_rx_queue_data (rxq);
u16 size = rxq->size;
vnet_dev_rv_t rv;
ASSERT (eq->buffer_indices == 0);
ASSERT (eq->compl_sqe_indices == 0);
ASSERT (eq->cqes == 0);
ASSERT (eq->sqes == 0);
log_debug (dev, "queue %u", rxq->queue_id);
eq->buffer_indices = clib_mem_alloc_aligned (
sizeof (eq->buffer_indices[0]) * size, CLIB_CACHE_LINE_BYTES);
eq->compl_sqe_indices = clib_mem_alloc_aligned (
sizeof (eq->compl_sqe_indices[0]) * size, CLIB_CACHE_LINE_BYTES);
if ((rv = vnet_dev_dma_mem_alloc (vm, dev, sizeof (eq->cqes[0]) * size, 0,
(void **) &eq->cqes)))
goto err;
if ((rv = vnet_dev_dma_mem_alloc (vm, dev, sizeof (eq->sqes[0]) * size, 0,
(void **) &eq->sqes)))
goto err;
return VNET_DEV_OK;
err:
ena_rx_queue_free (vm, rxq);
return rv;
}
void
ena_tx_queue_free (vlib_main_t *vm, vnet_dev_tx_queue_t *txq)
{
ena_txq_t *eq = vnet_dev_get_tx_queue_data (txq);
vnet_dev_port_t *port = txq->port;
vnet_dev_t *dev = port->dev;
ASSERT (txq->started == 0);
log_debug (dev, "queue %u", txq->queue_id);
foreach_pointer (p, eq->buffer_indices, eq->sqe_templates)
if (p)
clib_mem_free (p);
foreach_pointer (p, eq->cqes, eq->sqes)
vnet_dev_dma_mem_free (vm, dev, p);
}
vnet_dev_rv_t
ena_tx_queue_alloc (vlib_main_t *vm, vnet_dev_tx_queue_t *txq)
{
vnet_dev_port_t *port = txq->port;
vnet_dev_t *dev = port->dev;
ena_txq_t *eq = vnet_dev_get_tx_queue_data (txq);
u16 size = txq->size;
vnet_dev_rv_t rv;
ASSERT (eq->buffer_indices == 0);
ASSERT (eq->sqe_templates == 0);
ASSERT (eq->cqes == 0);
ASSERT (eq->sqes == 0);
log_debug (dev, "queue %u", txq->queue_id);
eq->buffer_indices = clib_mem_alloc_aligned (
sizeof (eq->buffer_indices[0]) * size, CLIB_CACHE_LINE_BYTES);
eq->sqe_templates = clib_mem_alloc_aligned (
sizeof (eq->sqe_templates[0]) * size, CLIB_CACHE_LINE_BYTES);
if ((rv = vnet_dev_dma_mem_alloc (vm, dev, sizeof (eq->cqes[0]) * size, 0,
(void **) &eq->cqes)))
goto err;
if ((rv = vnet_dev_dma_mem_alloc (vm, dev, sizeof (eq->sqes[0]) * size, 0,
(void **) &eq->sqes)))
goto err;
return VNET_DEV_OK;
err:
ena_tx_queue_free (vm, txq);
return rv;
}
vnet_dev_rv_t
ena_rx_queue_start (vlib_main_t *vm, vnet_dev_rx_queue_t *rxq)
{
ena_rxq_t *eq = vnet_dev_get_rx_queue_data (rxq);
vnet_dev_port_t *port = rxq->port;
vnet_dev_t *dev = port->dev;
ena_device_t *ed = vnet_dev_get_data (dev);
u16 buffer_size = vnet_dev_get_rx_queue_buffer_data_size (vm, rxq);
u16 size = rxq->size;
vnet_dev_rv_t rv;
/* Create Completion Queue */
ena_aq_create_cq_resp_t cqresp;
ena_aq_create_cq_cmd_t cqcmd = {
.interrupt_mode_enabled = 1,
.cq_entry_size_words = sizeof (ena_rx_cdesc_t) / 4,
.cq_depth = size,
.msix_vector = ~0,
};
ena_set_mem_addr (vm, dev, &cqcmd.cq_ba, eq->cqes);
if ((rv = ena_aq_create_cq (vm, dev, &cqcmd, &cqresp)))
{
log_err (dev, "queue %u cq creation failed", rxq->queue_id);
goto error;
}
eq->cq_idx = cqresp.cq_idx;
eq->cq_created = 1;
log_debug (dev, "queue %u cq %u created", rxq->queue_id, eq->cq_idx);
/* Create Submission Queue */
ena_aq_create_sq_resp_t sqresp;
ena_aq_create_sq_cmd_t sqcmd = {
.sq_direction = ENA_ADMIN_SQ_DIRECTION_RX,
.placement_policy = ENA_ADMIN_SQ_PLACEMENT_POLICY_HOST,
.completion_policy = ENA_ADMIN_SQ_COMPLETION_POLICY_DESC,
.is_physically_contiguous = 1,
.sq_depth = size,
.cq_idx = cqresp.cq_idx,
};
ena_set_mem_addr (vm, dev, &sqcmd.sq_ba, eq->sqes);
if ((rv = ena_aq_create_sq (vm, dev, &sqcmd, &sqresp)))
{
log_err (dev, "queue %u sq creation failed", rxq->queue_id);
goto error;
}
eq->sq_idx = sqresp.sq_idx;
eq->sq_db = (u32 *) ((u8 *) ed->reg_bar + sqresp.sq_doorbell_offset);
eq->sq_created = 1;
log_debug (dev, "queue %u sq %u created, sq_db %p", rxq->queue_id,
eq->sq_idx, eq->sq_db);
for (int i = 0; i < size; i++)
{
eq->sqes[i] = (ena_rx_desc_t){
.lo = {
.length = buffer_size,
.comp_req = 1,
.first = 1,
.last = 1,
.reserved5 = 1, /* ena_com says MBO */
.req_id = i,
},
};
eq->buffer_indices[i] = VLIB_BUFFER_INVALID_INDEX;
eq->compl_sqe_indices[i] = i;
}
eq->sq_next = 0;
eq->n_compl_sqes = size;
return VNET_DEV_OK;
error:
ena_rx_queue_stop (vm, rxq);
return rv;
}
vnet_dev_rv_t
ena_tx_queue_start (vlib_main_t *vm, vnet_dev_tx_queue_t *txq)
{
ena_txq_t *eq = vnet_dev_get_tx_queue_data (txq);
vnet_dev_port_t *port = txq->port;
vnet_dev_t *dev = port->dev;
ena_device_t *ed = vnet_dev_get_data (dev);
u16 size = txq->size;
vnet_dev_rv_t rv;
/* Create Completion Queue */
ena_aq_create_cq_resp_t cqresp;
ena_aq_create_cq_cmd_t cqcmd = {
.interrupt_mode_enabled = 1,
.cq_entry_size_words = sizeof (ena_tx_cdesc_t) / 4,
.cq_depth = size,
.msix_vector = ~0,
};
ena_set_mem_addr (vm, dev, &cqcmd.cq_ba, eq->cqes);
if ((rv = ena_aq_create_cq (vm, dev, &cqcmd, &cqresp)))
{
log_err (dev, "queue %u cq creation failed", txq->queue_id);
goto error;
}
eq->cq_idx = cqresp.cq_idx;
eq->cq_created = 1;
log_debug (dev, "queue %u cq %u created", txq->queue_id, eq->cq_idx);
/* Create Submission Queue */
ena_aq_create_sq_resp_t sqresp;
ena_aq_create_sq_cmd_t sqcmd = {
.sq_direction = ENA_ADMIN_SQ_DIRECTION_TX,
.placement_policy = eq->llq ? ENA_ADMIN_SQ_PLACEMENT_POLICY_DEVICE :
ENA_ADMIN_SQ_PLACEMENT_POLICY_HOST,
.completion_policy = ENA_ADMIN_SQ_COMPLETION_POLICY_DESC,
.is_physically_contiguous = 1,
.sq_depth = size,
.cq_idx = cqresp.cq_idx,
};
if (eq->llq == 0)
ena_set_mem_addr (vm, dev, &sqcmd.sq_ba, eq->sqes);
if ((rv = ena_aq_create_sq (vm, dev, &sqcmd, &sqresp)))
{
log_err (dev, "queue %u sq creation failed", txq->queue_id);
goto error;
}
eq->sq_idx = sqresp.sq_idx;
eq->sq_db = (u32 *) ((u8 *) ed->reg_bar + sqresp.sq_doorbell_offset);
eq->sq_created = 1;
log_debug (dev, "queue %u sq %u created, sq_db %p", txq->queue_id,
eq->sq_idx, eq->sq_db);
for (u32 i = 0; i < size; i++)
{
eq->sqe_templates[i] =
(ena_tx_desc_t){ .req_id_lo = i, .req_id_hi = i >> 10, .comp_req = 1 }
.as_u64x2[0];
eq->buffer_indices[i] = VLIB_BUFFER_INVALID_INDEX;
}
eq->sq_head = 0;
eq->sq_tail = 0;
eq->cq_next = 0;
#if 0
if (txq->llq)
txq->llq_descs =
(ena_tx_llq_desc128_t *) ((u8 *) ed->mem_bar +
sqresp.llq_descriptors_offset);
#endif
log_debug (dev, "queue %u sq %u created, sq_db %p llq_desc %p",
txq->queue_id, eq->sq_idx, eq->sq_db,
eq->llq ? eq->llq_descs : 0);
return VNET_DEV_OK;
error:
ena_tx_queue_stop (vm, txq);
return rv;
}
static void
ena_free_sq_buffer_indices (vlib_main_t *vm, u32 *sq_buffer_indices,
u32 n_desc)
{
u32 *to = sq_buffer_indices;
for (u32 *from = to; from < sq_buffer_indices + n_desc; from++)
if (from[0] != VLIB_BUFFER_INVALID_INDEX)
to++[0] = from[0];
if (to - sq_buffer_indices > 0)
vlib_buffer_free (vm, sq_buffer_indices, to - sq_buffer_indices);
}
void
ena_rx_queue_stop (vlib_main_t *vm, vnet_dev_rx_queue_t *rxq)
{
ena_rxq_t *eq = vnet_dev_get_rx_queue_data (rxq);
vnet_dev_t *dev = rxq->port->dev;
vnet_dev_rv_t rv;
if (eq->sq_created)
{
ena_aq_destroy_sq_cmd_t cmd = {
.sq_idx = eq->sq_idx,
.sq_direction = ENA_ADMIN_SQ_DIRECTION_TX,
};
if ((rv = ena_aq_destroy_sq (vm, dev, &cmd)))
log_err (dev, "queue %u failed to destroy sq %u", rxq->queue_id,
eq->sq_idx);
eq->sq_created = 0;
};
if (eq->cq_created)
{
ena_aq_destroy_cq_cmd_t cmd = {
.cq_idx = eq->cq_idx,
};
if ((rv = ena_aq_destroy_cq (vm, dev, &cmd)))
log_err (dev, "queue %u failed to destroy cq %u", rxq->queue_id,
eq->cq_idx);
eq->cq_created = 0;
};
if (eq->n_compl_sqes < rxq->size)
ena_free_sq_buffer_indices (vm, eq->buffer_indices, rxq->size);
}
void
ena_tx_queue_stop (vlib_main_t *vm, vnet_dev_tx_queue_t *txq)
{
ena_txq_t *eq = vnet_dev_get_tx_queue_data (txq);
vnet_dev_t *dev = txq->port->dev;
vnet_dev_rv_t rv;
if (eq->sq_created)
{
ena_aq_destroy_sq_cmd_t cmd = {
.sq_idx = eq->sq_idx,
.sq_direction = ENA_ADMIN_SQ_DIRECTION_TX,
};
if ((rv = ena_aq_destroy_sq (vm, dev, &cmd)))
log_err (dev, "queue %u failed to destroy sq %u", txq->queue_id,
eq->sq_idx);
eq->sq_created = 0;
};
if (eq->cq_created)
{
ena_aq_destroy_cq_cmd_t cmd = {
.cq_idx = eq->cq_idx,
};
if ((rv = ena_aq_destroy_cq (vm, dev, &cmd)))
log_err (dev, "queue %u failed to destroy cq %u", txq->queue_id,
eq->cq_idx);
eq->cq_created = 0;
};
if (eq->sq_head != eq->sq_tail)
ena_free_sq_buffer_indices (vm, eq->buffer_indices, txq->size);
}
|