summaryrefslogtreecommitdiffstats
path: root/src/plugins/dev_ena/aq.c
blob: 290d5bd52c6f12c302dfa2f783d2a32cd3080548 (plain)
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
/* 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>
#include <vnet/ethernet/ethernet.h>

VLIB_REGISTER_LOG_CLASS (ena_log, static) = {
  .class_name = "ena",
  .subclass_name = "admin",
};

VLIB_REGISTER_LOG_CLASS (ena_stats_log, static) = {
  .class_name = "ena",
  .subclass_name = "admin-stats",
};

ena_aq_feat_info_t feat_info[] = {
#define _(v, ver, gt, st, n, s)                                               \
  [v] = { .name = #n,                                                         \
	  .version = (ver),                                                   \
	  .data_sz = sizeof (s),                                              \
	  .get = (gt),                                                        \
	  .set = (st) },
  foreach_ena_aq_feature_id
#undef _
};

ena_aq_feat_info_t *
ena_aq_get_feat_info (ena_aq_feature_id_t id)
{
  if (id >= ARRAY_LEN (feat_info) || feat_info[id].data_sz == 0)
    return 0;

  return feat_info + id;
}

void
ena_aq_free (vlib_main_t *vm, vnet_dev_t *dev)
{
  ena_device_t *ed = vnet_dev_get_data (dev);
  vnet_dev_dma_mem_free (vm, dev, ed->aq.cq_entries);
  vnet_dev_dma_mem_free (vm, dev, ed->aq.sq_entries);
  ed->aq.depth = 0;
}

vnet_dev_rv_t
ena_aq_olloc (vlib_main_t *vm, vnet_dev_t *dev, u16 depth)
{
  ena_device_t *ed = vnet_dev_get_data (dev);
  vnet_dev_dma_mem_free (vm, dev, ed->aq.cq_entries);
  vnet_dev_dma_mem_free (vm, dev, ed->aq.sq_entries);
  u32 sq_alloc_sz = sizeof (ena_aq_sq_entry_t) * depth;
  u32 cq_alloc_sz = sizeof (ena_aq_cq_entry_t) * depth;
  vnet_dev_rv_t rv;

  ASSERT (ed->aq.sq_entries == 0);
  ASSERT (ed->aq.cq_entries == 0);

  rv = vnet_dev_dma_mem_alloc (vm, dev, sq_alloc_sz, 0,
			       (void **) &ed->aq.sq_entries);
  if (rv != VNET_DEV_OK)
    goto err;

  rv = vnet_dev_dma_mem_alloc (vm, dev, cq_alloc_sz, 0,
			       (void **) &ed->aq.cq_entries);
  if (rv != VNET_DEV_OK)
    goto err;

  ed->aq.depth = depth;

  return VNET_DEV_OK;
err:
  ena_aq_free (vm, dev);
  return rv;
}

vnet_dev_rv_t
ena_aq_start (vlib_main_t *vm, vnet_dev_t *dev)
{
  ena_device_t *ed = vnet_dev_get_data (dev);
  u16 depth = ed->aq.depth;
  u32 sq_alloc_sz = sizeof (ena_aq_sq_entry_t) * depth;
  u32 cq_alloc_sz = sizeof (ena_aq_cq_entry_t) * depth;

  ASSERT (ed->aq_started == 0);

  ena_reg_aq_caps_t aq_caps = {
    .depth = depth,
    .entry_size = sizeof (ena_aq_sq_entry_t),
  };

  ena_reg_acq_caps_t acq_caps = {
    .depth = depth,
    .entry_size = sizeof (ena_aq_cq_entry_t),
  };

  clib_memset (ed->aq.sq_entries, 0, sq_alloc_sz);
  clib_memset (ed->aq.cq_entries, 0, cq_alloc_sz);

  ed->aq.sq_next = 0;
  ed->aq.cq_head = 0;

  ena_reg_set_dma_addr (vm, dev, ENA_REG_AQ_BASE_LO, ENA_REG_AQ_BASE_HI,
			ed->aq.sq_entries);
  ena_reg_set_dma_addr (vm, dev, ENA_REG_ACQ_BASE_LO, ENA_REG_ACQ_BASE_HI,
			ed->aq.cq_entries);

  ena_reg_write (dev, ENA_REG_AQ_CAPS, &aq_caps);
  ena_reg_write (dev, ENA_REG_ACQ_CAPS, &acq_caps);

  ed->aq_started = 1;

  return VNET_DEV_OK;
}

void
ena_aq_stop (vlib_main_t *vm, vnet_dev_t *dev)
{
  ena_device_t *ed = vnet_dev_get_data (dev);
  ena_reg_aq_caps_t aq_caps = {};
  ena_reg_acq_caps_t acq_caps = {};

  if (ed->aq_started)
    {
      ena_reg_write (dev, ENA_REG_AQ_CAPS, &aq_caps);
      ena_reg_write (dev, ENA_REG_ACQ_CAPS, &acq_caps);
      ed->aq_started = 0;
    }
}
vnet_dev_rv_t
ena_aq_req (vlib_main_t *vm, vnet_dev_t *dev, ena_aq_opcode_t opcode,
	    void *sqe_data, u8 sqe_data_sz, void *cqe_data, u8 cqe_data_sz)
{
  ena_device_t *ed = vnet_dev_get_data (dev);
  u32 next = ed->aq.sq_next++;
  u32 index = next & pow2_mask (ENA_ADMIN_QUEUE_LOG2_DEPTH);
  u8 phase = 1 & (~(next >> ENA_ADMIN_QUEUE_LOG2_DEPTH));
  ena_aq_sq_entry_t *sqe = ed->aq.sq_entries + index;
  ena_aq_cq_entry_t *cqe = ed->aq.cq_entries + index;
  f64 suspend_time = 1e-6;

  clib_memcpy_fast (&sqe->data, sqe_data, sqe_data_sz);
  sqe->opcode = opcode;
  sqe->command_id = index;
  sqe->phase = phase;

  ena_reg_write (dev, ENA_REG_AQ_DB, &ed->aq.sq_next);

  while (cqe->phase != phase)
    {
      vlib_process_suspend (vm, suspend_time);
      suspend_time *= 2;
      if (suspend_time > 1e-3)
	{
	  log_err (dev, "admin queue timeout (opcode %U)",
		   format_ena_aq_opcode, opcode);
	  return VNET_DEV_ERR_TIMEOUT;
	}
    }

  if (cqe->status != ENA_ADMIN_COMPL_STATUS_SUCCESS)
    {
      log_err (dev,
	       "cqe[%u]: opcode %U status %U ext_status %u sq_head_idx %u",
	       cqe - ed->aq.cq_entries, format_ena_aq_opcode, opcode,
	       format_ena_aq_status, cqe->status, cqe->extended_status,
	       cqe->sq_head_indx);
      return VNET_DEV_ERR_DEVICE_NO_REPLY;
    }

  log_debug (dev, "cqe: status %u ext_status %u sq_head_idx %u", cqe->status,
	     cqe->extended_status, cqe->sq_head_indx);

  if (cqe_data && cqe_data_sz)
    clib_memcpy_fast (cqe_data, &cqe->data, cqe_data_sz);
  return VNET_DEV_OK;
}

vnet_dev_rv_t
ena_aq_set_feature (vlib_main_t *vm, vnet_dev_t *dev,
		    ena_aq_feature_id_t feat_id, void *data)
{
  vnet_dev_rv_t rv;

  struct
  {
    ena_aq_aq_ctrl_buff_info_t control_buffer;
    ena_aq_get_set_feature_common_desc_t feat_common;
    u32 data[11];
  } fd = {
    .feat_common.feature_id = feat_id,
    .feat_common.feature_version = feat_info[feat_id].version,
  };

  log_debug (dev, "set_feature(%s):\n  %U", feat_info[feat_id].name,
	     format_ena_aq_feat_desc, feat_id, data);

  ASSERT (feat_info[feat_id].data_sz > 1);
  clib_memcpy (&fd.data, data, feat_info[feat_id].data_sz);

  rv = ena_aq_req (vm, dev, ENA_AQ_OPCODE_SET_FEATURE, &fd, sizeof (fd), 0, 0);

  if (rv != VNET_DEV_OK)
    log_err (dev, "get_feature(%U) failed", format_ena_aq_feat_name, feat_id);

  return rv;
}

vnet_dev_rv_t
ena_aq_get_feature (vlib_main_t *vm, vnet_dev_t *dev,
		    ena_aq_feature_id_t feat_id, void *data)
{
  vnet_dev_rv_t rv;

  struct
  {
    ena_aq_aq_ctrl_buff_info_t control_buffer;
    ena_aq_get_set_feature_common_desc_t feat_common;
    u32 data[11];
  } fd = {
    .feat_common.feature_id = feat_id,
    .feat_common.feature_version = feat_info[feat_id].version,
  };

  rv = ena_aq_req (vm, dev, ENA_AQ_OPCODE_GET_FEATURE, &fd, sizeof (fd), data,
		   feat_info[feat_id].data_sz);

  if (rv != VNET_DEV_OK)
    {
      log_err (dev, "get_feature(%U) failed", format_ena_aq_feat_name,
	       feat_id);
      return rv;
    }

  ASSERT (feat_info[feat_id].data_sz > 1);

  log_debug (dev, "get_feature(%s):\n  %U", feat_info[feat_id].name,
	     format_ena_aq_feat_desc, feat_id, data);

  return 0;
}

vnet_dev_rv_t
ena_aq_create_sq (vlib_main_t *vm, vnet_dev_t *dev,
		  ena_aq_create_sq_cmd_t *cmd, ena_aq_create_sq_resp_t *resp)
{
  vnet_dev_rv_t rv;

  log_debug (dev, "create_sq_cmd_req:\n  %U", format_ena_aq_create_sq_cmd,
	     cmd);

  rv = ena_aq_req (vm, dev, ENA_AQ_OPCODE_CREATE_SQ, cmd, sizeof (*cmd), resp,
		   sizeof (*resp));

  if (rv != VNET_DEV_OK)
    log_debug (dev, "create_sq_cmd_resp:\n  %U", format_ena_aq_create_sq_resp,
	       resp);
  return rv;
}

vnet_dev_rv_t
ena_aq_create_cq (vlib_main_t *vm, vnet_dev_t *dev,
		  ena_aq_create_cq_cmd_t *cmd, ena_aq_create_cq_resp_t *resp)
{
  vnet_dev_rv_t rv;

  log_debug (dev, "create_cq_cmd_req:\n  %U", format_ena_aq_create_cq_cmd,
	     cmd);

  rv = ena_aq_req (vm, dev, ENA_AQ_OPCODE_CREATE_CQ, cmd, sizeof (*cmd), resp,
		   sizeof (*resp));

  if (rv != VNET_DEV_OK)
    log_debug (dev, "create_cq_cmd_resp:\n  %U", format_ena_aq_create_cq_resp,
	       resp);

  return rv;
}

vnet_dev_rv_t
ena_aq_destroy_sq (vlib_main_t *vm, vnet_dev_t *dev,
		   ena_aq_destroy_sq_cmd_t *cmd)
{
  log_debug (dev, "destroy_sq_cmd_req:\n  %U", format_ena_aq_destroy_sq_cmd,
	     cmd);

  return ena_aq_req (vm, dev, ENA_AQ_OPCODE_DESTROY_SQ, cmd, sizeof (*cmd), 0,
		     0);
}

vnet_dev_rv_t
ena_aq_destroy_cq (vlib_main_t *vm, vnet_dev_t *dev,
		   ena_aq_destroy_cq_cmd_t *cmd)
{
  log_debug (dev, "destroy_cq_cmd_req:\n  %U", format_ena_aq_destroy_cq_cmd,
	     cmd);

  return ena_aq_req (vm, dev, ENA_AQ_OPCODE_DESTROY_CQ, cmd, sizeof (*cmd), 0,
		     0);
}

vnet_dev_rv_t
ena_aq_get_stats (vlib_main_t *vm, vnet_dev_t *dev, ena_aq_stats_type_t type,
		  ena_aq_stats_scope_t scope, u16 queue_idx, void *data)
{
  vnet_dev_rv_t rv;
  format_function_t *ff = 0;
  u8 data_sz[] = {
    [ENA_ADMIN_STATS_TYPE_BASIC] = sizeof (ena_aq_basic_stats_t),
    [ENA_ADMIN_STATS_TYPE_EXTENDED] = 0,
    [ENA_ADMIN_STATS_TYPE_ENI] = sizeof (ena_aq_eni_stats_t),
  };

  char *type_str[] = {
#define _(n, s) [n] = #s,
    foreach_ena_aq_stats_type
#undef _
  };

  char *scope_str[] = {
#define _(n, s) [n] = #s,
    foreach_ena_aq_stats_scope
#undef _
  };

  ena_aq_get_stats_cmd_t cmd = {
    .type = type,
    .scope = scope,
    .queue_idx = scope == ENA_ADMIN_STATS_SCOPE_SPECIFIC_QUEUE ? queue_idx : 0,
    .device_id = 0xffff,
  };

  if ((rv = ena_aq_req (vm, dev, ENA_AQ_OPCODE_GET_STATS, &cmd, sizeof (cmd),
			data, data_sz[type])))
    {
      ena_stats_log_err (dev, "get_stats(%s, %s) failed", type_str[type],
			 scope_str[scope]);
      return rv;
    }

  if (type == ENA_ADMIN_STATS_TYPE_BASIC)
    ff = format_ena_aq_basic_stats;
  else if (type == ENA_ADMIN_STATS_TYPE_ENI)
    ff = format_ena_aq_eni_stats;

  if (ff)
    ena_stats_log_debug (dev, "get_stats(%s, %s, %u):\n  %U", type_str[type],
			 scope_str[scope], queue_idx, ff, data);
  else
    ena_stats_log_debug (dev, "get_stats(%s, %s, %u): unknown data",
			 type_str[type], scope_str[scope], queue_idx);

  return VNET_DEV_OK;
}