net/mlx5: separate DevX commands interface
[dpdk.git] / drivers / net / mlx5 / mlx5_devx_cmds.c
1 // SPDX-License-Identifier: BSD-3-Clause
2 /* Copyright 2018 Mellanox Technologies, Ltd */
3
4 #include <unistd.h>
5
6 #include <rte_flow_driver.h>
7 #include <rte_malloc.h>
8
9 #include "mlx5_prm.h"
10 #include "mlx5_devx_cmds.h"
11 #include "mlx5_utils.h"
12
13
14 /**
15  * Allocate flow counters via devx interface.
16  *
17  * @param[in] ctx
18  *   ibv contexts returned from mlx5dv_open_device.
19  * @param dcs
20  *   Pointer to counters properties structure to be filled by the routine.
21  * @param bulk_n_128
22  *   Bulk counter numbers in 128 counters units.
23  *
24  * @return
25  *   Pointer to counter object on success, a negative value otherwise and
26  *   rte_errno is set.
27  */
28 struct mlx5_devx_obj *
29 mlx5_devx_cmd_flow_counter_alloc(struct ibv_context *ctx, uint32_t bulk_n_128)
30 {
31         struct mlx5_devx_obj *dcs = rte_zmalloc("dcs", sizeof(*dcs), 0);
32         uint32_t in[MLX5_ST_SZ_DW(alloc_flow_counter_in)]   = {0};
33         uint32_t out[MLX5_ST_SZ_DW(alloc_flow_counter_out)] = {0};
34
35         if (!dcs) {
36                 rte_errno = ENOMEM;
37                 return NULL;
38         }
39         MLX5_SET(alloc_flow_counter_in, in, opcode,
40                  MLX5_CMD_OP_ALLOC_FLOW_COUNTER);
41         MLX5_SET(alloc_flow_counter_in, in, flow_counter_bulk, bulk_n_128);
42         dcs->obj = mlx5_glue->devx_obj_create(ctx, in,
43                                               sizeof(in), out, sizeof(out));
44         if (!dcs->obj) {
45                 DRV_LOG(ERR, "Can't allocate counters - error %d", errno);
46                 rte_errno = errno;
47                 rte_free(dcs);
48                 return NULL;
49         }
50         dcs->id = MLX5_GET(alloc_flow_counter_out, out, flow_counter_id);
51         return dcs;
52 }
53
54 /**
55  * Query flow counters values.
56  *
57  * @param[in] dcs
58  *   devx object that was obtained from mlx5_devx_cmd_fc_alloc.
59  * @param[in] clear
60  *   Whether hardware should clear the counters after the query or not.
61  * @param[in] n_counters
62  *   0 in case of 1 counter to read, otherwise the counter number to read.
63  *  @param pkts
64  *   The number of packets that matched the flow.
65  *  @param bytes
66  *    The number of bytes that matched the flow.
67  *  @param mkey
68  *   The mkey key for batch query.
69  *  @param addr
70  *    The address in the mkey range for batch query.
71  *  @param cmd_comp
72  *   The completion object for asynchronous batch query.
73  *  @param async_id
74  *    The ID to be returned in the asynchronous batch query response.
75  *
76  * @return
77  *   0 on success, a negative value otherwise.
78  */
79 int
80 mlx5_devx_cmd_flow_counter_query(struct mlx5_devx_obj *dcs,
81                                  int clear, uint32_t n_counters,
82                                  uint64_t *pkts, uint64_t *bytes,
83                                  uint32_t mkey, void *addr,
84                                  struct mlx5dv_devx_cmd_comp *cmd_comp,
85                                  uint64_t async_id)
86 {
87         int out_len = MLX5_ST_SZ_BYTES(query_flow_counter_out) +
88                         MLX5_ST_SZ_BYTES(traffic_counter);
89         uint32_t out[out_len];
90         uint32_t in[MLX5_ST_SZ_DW(query_flow_counter_in)] = {0};
91         void *stats;
92         int rc;
93
94         MLX5_SET(query_flow_counter_in, in, opcode,
95                  MLX5_CMD_OP_QUERY_FLOW_COUNTER);
96         MLX5_SET(query_flow_counter_in, in, op_mod, 0);
97         MLX5_SET(query_flow_counter_in, in, flow_counter_id, dcs->id);
98         MLX5_SET(query_flow_counter_in, in, clear, !!clear);
99
100         if (n_counters) {
101                 MLX5_SET(query_flow_counter_in, in, num_of_counters,
102                          n_counters);
103                 MLX5_SET(query_flow_counter_in, in, dump_to_memory, 1);
104                 MLX5_SET(query_flow_counter_in, in, mkey, mkey);
105                 MLX5_SET64(query_flow_counter_in, in, address,
106                            (uint64_t)(uintptr_t)addr);
107         }
108         if (!cmd_comp)
109                 rc = mlx5_glue->devx_obj_query(dcs->obj, in, sizeof(in), out,
110                                                out_len);
111         else
112                 rc = mlx5_glue->devx_obj_query_async(dcs->obj, in, sizeof(in),
113                                                      out_len, async_id,
114                                                      cmd_comp);
115         if (rc) {
116                 DRV_LOG(ERR, "Failed to query devx counters with rc %d", rc);
117                 rte_errno = rc;
118                 return -rc;
119         }
120         if (!n_counters) {
121                 stats = MLX5_ADDR_OF(query_flow_counter_out,
122                                      out, flow_statistics);
123                 *pkts = MLX5_GET64(traffic_counter, stats, packets);
124                 *bytes = MLX5_GET64(traffic_counter, stats, octets);
125         }
126         return 0;
127 }
128
129 /**
130  * Create a new mkey.
131  *
132  * @param[in] ctx
133  *   ibv contexts returned from mlx5dv_open_device.
134  * @param[in] attr
135  *   Attributes of the requested mkey.
136  *
137  * @return
138  *   Pointer to Devx mkey on success, a negative value otherwise and rte_errno
139  *   is set.
140  */
141 struct mlx5_devx_obj *
142 mlx5_devx_cmd_mkey_create(struct ibv_context *ctx,
143                           struct mlx5_devx_mkey_attr *attr)
144 {
145         uint32_t in[MLX5_ST_SZ_DW(create_mkey_in)] = {0};
146         uint32_t out[MLX5_ST_SZ_DW(create_mkey_out)] = {0};
147         void *mkc;
148         struct mlx5_devx_obj *mkey = rte_zmalloc("mkey", sizeof(*mkey), 0);
149         size_t pgsize;
150         uint32_t translation_size;
151
152         if (!mkey) {
153                 rte_errno = ENOMEM;
154                 return NULL;
155         }
156         pgsize = sysconf(_SC_PAGESIZE);
157         translation_size = (RTE_ALIGN(attr->size, pgsize) * 8) / 16;
158         MLX5_SET(create_mkey_in, in, opcode, MLX5_CMD_OP_CREATE_MKEY);
159         MLX5_SET(create_mkey_in, in, translations_octword_actual_size,
160                  translation_size);
161         MLX5_SET(create_mkey_in, in, mkey_umem_id, attr->umem_id);
162         mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
163         MLX5_SET(mkc, mkc, lw, 0x1);
164         MLX5_SET(mkc, mkc, lr, 0x1);
165         MLX5_SET(mkc, mkc, access_mode_1_0, MLX5_MKC_ACCESS_MODE_MTT);
166         MLX5_SET(mkc, mkc, qpn, 0xffffff);
167         MLX5_SET(mkc, mkc, pd, attr->pd);
168         MLX5_SET(mkc, mkc, mkey_7_0, attr->umem_id & 0xFF);
169         MLX5_SET(mkc, mkc, translations_octword_size, translation_size);
170         MLX5_SET64(mkc, mkc, start_addr, attr->addr);
171         MLX5_SET64(mkc, mkc, len, attr->size);
172         MLX5_SET(mkc, mkc, log_page_size, rte_log2_u32(pgsize));
173         mkey->obj = mlx5_glue->devx_obj_create(ctx, in, sizeof(in), out,
174                                                sizeof(out));
175         if (!mkey->obj) {
176                 DRV_LOG(ERR, "Can't create mkey - error %d", errno);
177                 rte_errno = errno;
178                 rte_free(mkey);
179                 return NULL;
180         }
181         mkey->id = MLX5_GET(create_mkey_out, out, mkey_index);
182         mkey->id = (mkey->id << 8) | (attr->umem_id & 0xFF);
183         return mkey;
184 }
185
186 /**
187  * Get status of devx command response.
188  * Mainly used for asynchronous commands.
189  *
190  * @param[in] out
191  *   The out response buffer.
192  *
193  * @return
194  *   0 on success, non-zero value otherwise.
195  */
196 int
197 mlx5_devx_get_out_command_status(void *out)
198 {
199         int status;
200
201         if (!out)
202                 return -EINVAL;
203         status = MLX5_GET(query_flow_counter_out, out, status);
204         if (status) {
205                 int syndrome = MLX5_GET(query_flow_counter_out, out, syndrome);
206
207                 DRV_LOG(ERR, "Bad devX status %x, syndrome = %x", status,
208                         syndrome);
209         }
210         return status;
211 }
212
213 /**
214  * Destroy any object allocated by a Devx API.
215  *
216  * @param[in] obj
217  *   Pointer to a general object.
218  *
219  * @return
220  *   0 on success, a negative value otherwise.
221  */
222 int
223 mlx5_devx_cmd_destroy(struct mlx5_devx_obj *obj)
224 {
225         int ret;
226
227         if (!obj)
228                 return 0;
229         ret =  mlx5_glue->devx_obj_destroy(obj->obj);
230         rte_free(obj);
231         return ret;
232 }
233
234 /**
235  * Query NIC vport context.
236  * Fills minimal inline attribute.
237  *
238  * @param[in] ctx
239  *   ibv contexts returned from mlx5dv_open_device.
240  * @param[in] vport
241  *   vport index
242  * @param[out] attr
243  *   Attributes device values.
244  *
245  * @return
246  *   0 on success, a negative value otherwise.
247  */
248 static int
249 mlx5_devx_cmd_query_nic_vport_context(struct ibv_context *ctx,
250                                       unsigned int vport,
251                                       struct mlx5_hca_attr *attr)
252 {
253         uint32_t in[MLX5_ST_SZ_DW(query_nic_vport_context_in)] = {0};
254         uint32_t out[MLX5_ST_SZ_DW(query_nic_vport_context_out)] = {0};
255         void *vctx;
256         int status, syndrome, rc;
257
258         /* Query NIC vport context to determine inline mode. */
259         MLX5_SET(query_nic_vport_context_in, in, opcode,
260                  MLX5_CMD_OP_QUERY_NIC_VPORT_CONTEXT);
261         MLX5_SET(query_nic_vport_context_in, in, vport_number, vport);
262         if (vport)
263                 MLX5_SET(query_nic_vport_context_in, in, other_vport, 1);
264         rc = mlx5_glue->devx_general_cmd(ctx,
265                                          in, sizeof(in),
266                                          out, sizeof(out));
267         if (rc)
268                 goto error;
269         status = MLX5_GET(query_nic_vport_context_out, out, status);
270         syndrome = MLX5_GET(query_nic_vport_context_out, out, syndrome);
271         if (status) {
272                 DRV_LOG(DEBUG, "Failed to query NIC vport context, "
273                         "status %x, syndrome = %x",
274                         status, syndrome);
275                 return -1;
276         }
277         vctx = MLX5_ADDR_OF(query_nic_vport_context_out, out,
278                             nic_vport_context);
279         attr->vport_inline_mode = MLX5_GET(nic_vport_context, vctx,
280                                            min_wqe_inline_mode);
281         return 0;
282 error:
283         rc = (rc > 0) ? -rc : rc;
284         return rc;
285 }
286
287 /**
288  * Query HCA attributes.
289  * Using those attributes we can check on run time if the device
290  * is having the required capabilities.
291  *
292  * @param[in] ctx
293  *   ibv contexts returned from mlx5dv_open_device.
294  * @param[out] attr
295  *   Attributes device values.
296  *
297  * @return
298  *   0 on success, a negative value otherwise.
299  */
300 int
301 mlx5_devx_cmd_query_hca_attr(struct ibv_context *ctx,
302                              struct mlx5_hca_attr *attr)
303 {
304         uint32_t in[MLX5_ST_SZ_DW(query_hca_cap_in)] = {0};
305         uint32_t out[MLX5_ST_SZ_DW(query_hca_cap_out)] = {0};
306         void *hcattr;
307         int status, syndrome, rc;
308
309         MLX5_SET(query_hca_cap_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_CAP);
310         MLX5_SET(query_hca_cap_in, in, op_mod,
311                  MLX5_GET_HCA_CAP_OP_MOD_GENERAL_DEVICE |
312                  MLX5_HCA_CAP_OPMOD_GET_CUR);
313
314         rc = mlx5_glue->devx_general_cmd(ctx,
315                                          in, sizeof(in), out, sizeof(out));
316         if (rc)
317                 goto error;
318         status = MLX5_GET(query_hca_cap_out, out, status);
319         syndrome = MLX5_GET(query_hca_cap_out, out, syndrome);
320         if (status) {
321                 DRV_LOG(DEBUG, "Failed to query devx HCA capabilities, "
322                         "status %x, syndrome = %x",
323                         status, syndrome);
324                 return -1;
325         }
326         hcattr = MLX5_ADDR_OF(query_hca_cap_out, out, capability);
327         attr->flow_counter_bulk_alloc_bitmap =
328                         MLX5_GET(cmd_hca_cap, hcattr, flow_counter_bulk_alloc);
329         attr->flow_counters_dump = MLX5_GET(cmd_hca_cap, hcattr,
330                                             flow_counters_dump);
331         attr->eswitch_manager = MLX5_GET(cmd_hca_cap, hcattr, eswitch_manager);
332         attr->hairpin = MLX5_GET(cmd_hca_cap, hcattr, hairpin);
333         attr->log_max_hairpin_queues = MLX5_GET(cmd_hca_cap, hcattr,
334                                                 log_max_hairpin_queues);
335         attr->log_max_hairpin_wq_data_sz = MLX5_GET(cmd_hca_cap, hcattr,
336                                                     log_max_hairpin_wq_data_sz);
337         attr->log_max_hairpin_num_packets = MLX5_GET
338                 (cmd_hca_cap, hcattr, log_min_hairpin_wq_data_sz);
339         attr->vhca_id = MLX5_GET(cmd_hca_cap, hcattr, vhca_id);
340         attr->eth_net_offloads = MLX5_GET(cmd_hca_cap, hcattr,
341                                           eth_net_offloads);
342         attr->eth_virt = MLX5_GET(cmd_hca_cap, hcattr, eth_virt);
343         attr->flex_parser_protocols = MLX5_GET(cmd_hca_cap, hcattr,
344                                                flex_parser_protocols);
345         attr->qos.sup = MLX5_GET(cmd_hca_cap, hcattr, qos);
346         if (attr->qos.sup) {
347                 MLX5_SET(query_hca_cap_in, in, op_mod,
348                          MLX5_GET_HCA_CAP_OP_MOD_QOS_CAP |
349                          MLX5_HCA_CAP_OPMOD_GET_CUR);
350                 rc = mlx5_glue->devx_general_cmd(ctx, in, sizeof(in),
351                                                  out, sizeof(out));
352                 if (rc)
353                         goto error;
354                 if (status) {
355                         DRV_LOG(DEBUG, "Failed to query devx QOS capabilities,"
356                                 " status %x, syndrome = %x",
357                                 status, syndrome);
358                         return -1;
359                 }
360                 hcattr = MLX5_ADDR_OF(query_hca_cap_out, out, capability);
361                 attr->qos.srtcm_sup =
362                                 MLX5_GET(qos_cap, hcattr, flow_meter_srtcm);
363                 attr->qos.log_max_flow_meter =
364                                 MLX5_GET(qos_cap, hcattr, log_max_flow_meter);
365                 attr->qos.flow_meter_reg_c_ids =
366                         MLX5_GET(qos_cap, hcattr, flow_meter_reg_id);
367                 attr->qos.flow_meter_reg_share =
368                         MLX5_GET(qos_cap, hcattr, flow_meter_reg_share);
369         }
370         if (!attr->eth_net_offloads)
371                 return 0;
372
373         /* Query HCA offloads for Ethernet protocol. */
374         memset(in, 0, sizeof(in));
375         memset(out, 0, sizeof(out));
376         MLX5_SET(query_hca_cap_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_CAP);
377         MLX5_SET(query_hca_cap_in, in, op_mod,
378                  MLX5_GET_HCA_CAP_OP_MOD_ETHERNET_OFFLOAD_CAPS |
379                  MLX5_HCA_CAP_OPMOD_GET_CUR);
380
381         rc = mlx5_glue->devx_general_cmd(ctx,
382                                          in, sizeof(in),
383                                          out, sizeof(out));
384         if (rc) {
385                 attr->eth_net_offloads = 0;
386                 goto error;
387         }
388         status = MLX5_GET(query_hca_cap_out, out, status);
389         syndrome = MLX5_GET(query_hca_cap_out, out, syndrome);
390         if (status) {
391                 DRV_LOG(DEBUG, "Failed to query devx HCA capabilities, "
392                         "status %x, syndrome = %x",
393                         status, syndrome);
394                 attr->eth_net_offloads = 0;
395                 return -1;
396         }
397         hcattr = MLX5_ADDR_OF(query_hca_cap_out, out, capability);
398         attr->wqe_vlan_insert = MLX5_GET(per_protocol_networking_offload_caps,
399                                          hcattr, wqe_vlan_insert);
400         attr->lro_cap = MLX5_GET(per_protocol_networking_offload_caps, hcattr,
401                                  lro_cap);
402         attr->tunnel_lro_gre = MLX5_GET(per_protocol_networking_offload_caps,
403                                         hcattr, tunnel_lro_gre);
404         attr->tunnel_lro_vxlan = MLX5_GET(per_protocol_networking_offload_caps,
405                                           hcattr, tunnel_lro_vxlan);
406         attr->lro_max_msg_sz_mode = MLX5_GET
407                                         (per_protocol_networking_offload_caps,
408                                          hcattr, lro_max_msg_sz_mode);
409         for (int i = 0 ; i < MLX5_LRO_NUM_SUPP_PERIODS ; i++) {
410                 attr->lro_timer_supported_periods[i] =
411                         MLX5_GET(per_protocol_networking_offload_caps, hcattr,
412                                  lro_timer_supported_periods[i]);
413         }
414         attr->tunnel_stateless_geneve_rx =
415                             MLX5_GET(per_protocol_networking_offload_caps,
416                                      hcattr, tunnel_stateless_geneve_rx);
417         attr->geneve_max_opt_len =
418                     MLX5_GET(per_protocol_networking_offload_caps,
419                              hcattr, max_geneve_opt_len);
420         attr->wqe_inline_mode = MLX5_GET(per_protocol_networking_offload_caps,
421                                          hcattr, wqe_inline_mode);
422         attr->tunnel_stateless_gtp = MLX5_GET
423                                         (per_protocol_networking_offload_caps,
424                                          hcattr, tunnel_stateless_gtp);
425         if (attr->wqe_inline_mode != MLX5_CAP_INLINE_MODE_VPORT_CONTEXT)
426                 return 0;
427         if (attr->eth_virt) {
428                 rc = mlx5_devx_cmd_query_nic_vport_context(ctx, 0, attr);
429                 if (rc) {
430                         attr->eth_virt = 0;
431                         goto error;
432                 }
433         }
434         return 0;
435 error:
436         rc = (rc > 0) ? -rc : rc;
437         return rc;
438 }
439
440 /**
441  * Query TIS transport domain from QP verbs object using DevX API.
442  *
443  * @param[in] qp
444  *   Pointer to verbs QP returned by ibv_create_qp .
445  * @param[in] tis_num
446  *   TIS number of TIS to query.
447  * @param[out] tis_td
448  *   Pointer to TIS transport domain variable, to be set by the routine.
449  *
450  * @return
451  *   0 on success, a negative value otherwise.
452  */
453 int
454 mlx5_devx_cmd_qp_query_tis_td(struct ibv_qp *qp, uint32_t tis_num,
455                               uint32_t *tis_td)
456 {
457         uint32_t in[MLX5_ST_SZ_DW(query_tis_in)] = {0};
458         uint32_t out[MLX5_ST_SZ_DW(query_tis_out)] = {0};
459         int rc;
460         void *tis_ctx;
461
462         MLX5_SET(query_tis_in, in, opcode, MLX5_CMD_OP_QUERY_TIS);
463         MLX5_SET(query_tis_in, in, tisn, tis_num);
464         rc = mlx5_glue->devx_qp_query(qp, in, sizeof(in), out, sizeof(out));
465         if (rc) {
466                 DRV_LOG(ERR, "Failed to query QP using DevX");
467                 return -rc;
468         };
469         tis_ctx = MLX5_ADDR_OF(query_tis_out, out, tis_context);
470         *tis_td = MLX5_GET(tisc, tis_ctx, transport_domain);
471         return 0;
472 }
473
474 /**
475  * Fill WQ data for DevX API command.
476  * Utility function for use when creating DevX objects containing a WQ.
477  *
478  * @param[in] wq_ctx
479  *   Pointer to WQ context to fill with data.
480  * @param [in] wq_attr
481  *   Pointer to WQ attributes structure to fill in WQ context.
482  */
483 static void
484 devx_cmd_fill_wq_data(void *wq_ctx, struct mlx5_devx_wq_attr *wq_attr)
485 {
486         MLX5_SET(wq, wq_ctx, wq_type, wq_attr->wq_type);
487         MLX5_SET(wq, wq_ctx, wq_signature, wq_attr->wq_signature);
488         MLX5_SET(wq, wq_ctx, end_padding_mode, wq_attr->end_padding_mode);
489         MLX5_SET(wq, wq_ctx, cd_slave, wq_attr->cd_slave);
490         MLX5_SET(wq, wq_ctx, hds_skip_first_sge, wq_attr->hds_skip_first_sge);
491         MLX5_SET(wq, wq_ctx, log2_hds_buf_size, wq_attr->log2_hds_buf_size);
492         MLX5_SET(wq, wq_ctx, page_offset, wq_attr->page_offset);
493         MLX5_SET(wq, wq_ctx, lwm, wq_attr->lwm);
494         MLX5_SET(wq, wq_ctx, pd, wq_attr->pd);
495         MLX5_SET(wq, wq_ctx, uar_page, wq_attr->uar_page);
496         MLX5_SET64(wq, wq_ctx, dbr_addr, wq_attr->dbr_addr);
497         MLX5_SET(wq, wq_ctx, hw_counter, wq_attr->hw_counter);
498         MLX5_SET(wq, wq_ctx, sw_counter, wq_attr->sw_counter);
499         MLX5_SET(wq, wq_ctx, log_wq_stride, wq_attr->log_wq_stride);
500         MLX5_SET(wq, wq_ctx, log_wq_pg_sz, wq_attr->log_wq_pg_sz);
501         MLX5_SET(wq, wq_ctx, log_wq_sz, wq_attr->log_wq_sz);
502         MLX5_SET(wq, wq_ctx, dbr_umem_valid, wq_attr->dbr_umem_valid);
503         MLX5_SET(wq, wq_ctx, wq_umem_valid, wq_attr->wq_umem_valid);
504         MLX5_SET(wq, wq_ctx, log_hairpin_num_packets,
505                  wq_attr->log_hairpin_num_packets);
506         MLX5_SET(wq, wq_ctx, log_hairpin_data_sz, wq_attr->log_hairpin_data_sz);
507         MLX5_SET(wq, wq_ctx, single_wqe_log_num_of_strides,
508                  wq_attr->single_wqe_log_num_of_strides);
509         MLX5_SET(wq, wq_ctx, two_byte_shift_en, wq_attr->two_byte_shift_en);
510         MLX5_SET(wq, wq_ctx, single_stride_log_num_of_bytes,
511                  wq_attr->single_stride_log_num_of_bytes);
512         MLX5_SET(wq, wq_ctx, dbr_umem_id, wq_attr->dbr_umem_id);
513         MLX5_SET(wq, wq_ctx, wq_umem_id, wq_attr->wq_umem_id);
514         MLX5_SET64(wq, wq_ctx, wq_umem_offset, wq_attr->wq_umem_offset);
515 }
516
517 /**
518  * Create RQ using DevX API.
519  *
520  * @param[in] ctx
521  *   ibv_context returned from mlx5dv_open_device.
522  * @param [in] rq_attr
523  *   Pointer to create RQ attributes structure.
524  * @param [in] socket
525  *   CPU socket ID for allocations.
526  *
527  * @return
528  *   The DevX object created, NULL otherwise and rte_errno is set.
529  */
530 struct mlx5_devx_obj *
531 mlx5_devx_cmd_create_rq(struct ibv_context *ctx,
532                         struct mlx5_devx_create_rq_attr *rq_attr,
533                         int socket)
534 {
535         uint32_t in[MLX5_ST_SZ_DW(create_rq_in)] = {0};
536         uint32_t out[MLX5_ST_SZ_DW(create_rq_out)] = {0};
537         void *rq_ctx, *wq_ctx;
538         struct mlx5_devx_wq_attr *wq_attr;
539         struct mlx5_devx_obj *rq = NULL;
540
541         rq = rte_calloc_socket(__func__, 1, sizeof(*rq), 0, socket);
542         if (!rq) {
543                 DRV_LOG(ERR, "Failed to allocate RQ data");
544                 rte_errno = ENOMEM;
545                 return NULL;
546         }
547         MLX5_SET(create_rq_in, in, opcode, MLX5_CMD_OP_CREATE_RQ);
548         rq_ctx = MLX5_ADDR_OF(create_rq_in, in, ctx);
549         MLX5_SET(rqc, rq_ctx, rlky, rq_attr->rlky);
550         MLX5_SET(rqc, rq_ctx, delay_drop_en, rq_attr->delay_drop_en);
551         MLX5_SET(rqc, rq_ctx, scatter_fcs, rq_attr->scatter_fcs);
552         MLX5_SET(rqc, rq_ctx, vsd, rq_attr->vsd);
553         MLX5_SET(rqc, rq_ctx, mem_rq_type, rq_attr->mem_rq_type);
554         MLX5_SET(rqc, rq_ctx, state, rq_attr->state);
555         MLX5_SET(rqc, rq_ctx, flush_in_error_en, rq_attr->flush_in_error_en);
556         MLX5_SET(rqc, rq_ctx, hairpin, rq_attr->hairpin);
557         MLX5_SET(rqc, rq_ctx, user_index, rq_attr->user_index);
558         MLX5_SET(rqc, rq_ctx, cqn, rq_attr->cqn);
559         MLX5_SET(rqc, rq_ctx, counter_set_id, rq_attr->counter_set_id);
560         MLX5_SET(rqc, rq_ctx, rmpn, rq_attr->rmpn);
561         wq_ctx = MLX5_ADDR_OF(rqc, rq_ctx, wq);
562         wq_attr = &rq_attr->wq_attr;
563         devx_cmd_fill_wq_data(wq_ctx, wq_attr);
564         rq->obj = mlx5_glue->devx_obj_create(ctx, in, sizeof(in),
565                                                   out, sizeof(out));
566         if (!rq->obj) {
567                 DRV_LOG(ERR, "Failed to create RQ using DevX");
568                 rte_errno = errno;
569                 rte_free(rq);
570                 return NULL;
571         }
572         rq->id = MLX5_GET(create_rq_out, out, rqn);
573         return rq;
574 }
575
576 /**
577  * Modify RQ using DevX API.
578  *
579  * @param[in] rq
580  *   Pointer to RQ object structure.
581  * @param [in] rq_attr
582  *   Pointer to modify RQ attributes structure.
583  *
584  * @return
585  *   0 on success, a negative errno value otherwise and rte_errno is set.
586  */
587 int
588 mlx5_devx_cmd_modify_rq(struct mlx5_devx_obj *rq,
589                         struct mlx5_devx_modify_rq_attr *rq_attr)
590 {
591         uint32_t in[MLX5_ST_SZ_DW(modify_rq_in)] = {0};
592         uint32_t out[MLX5_ST_SZ_DW(modify_rq_out)] = {0};
593         void *rq_ctx, *wq_ctx;
594         int ret;
595
596         MLX5_SET(modify_rq_in, in, opcode, MLX5_CMD_OP_MODIFY_RQ);
597         MLX5_SET(modify_rq_in, in, rq_state, rq_attr->rq_state);
598         MLX5_SET(modify_rq_in, in, rqn, rq->id);
599         MLX5_SET64(modify_rq_in, in, modify_bitmask, rq_attr->modify_bitmask);
600         rq_ctx = MLX5_ADDR_OF(modify_rq_in, in, ctx);
601         MLX5_SET(rqc, rq_ctx, state, rq_attr->state);
602         if (rq_attr->modify_bitmask &
603                         MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_SCATTER_FCS)
604                 MLX5_SET(rqc, rq_ctx, scatter_fcs, rq_attr->scatter_fcs);
605         if (rq_attr->modify_bitmask & MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD)
606                 MLX5_SET(rqc, rq_ctx, vsd, rq_attr->vsd);
607         if (rq_attr->modify_bitmask &
608                         MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_RQ_COUNTER_SET_ID)
609                 MLX5_SET(rqc, rq_ctx, counter_set_id, rq_attr->counter_set_id);
610         MLX5_SET(rqc, rq_ctx, hairpin_peer_sq, rq_attr->hairpin_peer_sq);
611         MLX5_SET(rqc, rq_ctx, hairpin_peer_vhca, rq_attr->hairpin_peer_vhca);
612         if (rq_attr->modify_bitmask & MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_WQ_LWM) {
613                 wq_ctx = MLX5_ADDR_OF(rqc, rq_ctx, wq);
614                 MLX5_SET(wq, wq_ctx, lwm, rq_attr->lwm);
615         }
616         ret = mlx5_glue->devx_obj_modify(rq->obj, in, sizeof(in),
617                                          out, sizeof(out));
618         if (ret) {
619                 DRV_LOG(ERR, "Failed to modify RQ using DevX");
620                 rte_errno = errno;
621                 return -errno;
622         }
623         return ret;
624 }
625
626 /**
627  * Create TIR using DevX API.
628  *
629  * @param[in] ctx
630  *   ibv_context returned from mlx5dv_open_device.
631  * @param [in] tir_attr
632  *   Pointer to TIR attributes structure.
633  *
634  * @return
635  *   The DevX object created, NULL otherwise and rte_errno is set.
636  */
637 struct mlx5_devx_obj *
638 mlx5_devx_cmd_create_tir(struct ibv_context *ctx,
639                          struct mlx5_devx_tir_attr *tir_attr)
640 {
641         uint32_t in[MLX5_ST_SZ_DW(create_tir_in)] = {0};
642         uint32_t out[MLX5_ST_SZ_DW(create_tir_out)] = {0};
643         void *tir_ctx, *outer, *inner;
644         struct mlx5_devx_obj *tir = NULL;
645         int i;
646
647         tir = rte_calloc(__func__, 1, sizeof(*tir), 0);
648         if (!tir) {
649                 DRV_LOG(ERR, "Failed to allocate TIR data");
650                 rte_errno = ENOMEM;
651                 return NULL;
652         }
653         MLX5_SET(create_tir_in, in, opcode, MLX5_CMD_OP_CREATE_TIR);
654         tir_ctx = MLX5_ADDR_OF(create_tir_in, in, ctx);
655         MLX5_SET(tirc, tir_ctx, disp_type, tir_attr->disp_type);
656         MLX5_SET(tirc, tir_ctx, lro_timeout_period_usecs,
657                  tir_attr->lro_timeout_period_usecs);
658         MLX5_SET(tirc, tir_ctx, lro_enable_mask, tir_attr->lro_enable_mask);
659         MLX5_SET(tirc, tir_ctx, lro_max_msg_sz, tir_attr->lro_max_msg_sz);
660         MLX5_SET(tirc, tir_ctx, inline_rqn, tir_attr->inline_rqn);
661         MLX5_SET(tirc, tir_ctx, rx_hash_symmetric, tir_attr->rx_hash_symmetric);
662         MLX5_SET(tirc, tir_ctx, tunneled_offload_en,
663                  tir_attr->tunneled_offload_en);
664         MLX5_SET(tirc, tir_ctx, indirect_table, tir_attr->indirect_table);
665         MLX5_SET(tirc, tir_ctx, rx_hash_fn, tir_attr->rx_hash_fn);
666         MLX5_SET(tirc, tir_ctx, self_lb_block, tir_attr->self_lb_block);
667         MLX5_SET(tirc, tir_ctx, transport_domain, tir_attr->transport_domain);
668         for (i = 0; i < 10; i++) {
669                 MLX5_SET(tirc, tir_ctx, rx_hash_toeplitz_key[i],
670                          tir_attr->rx_hash_toeplitz_key[i]);
671         }
672         outer = MLX5_ADDR_OF(tirc, tir_ctx, rx_hash_field_selector_outer);
673         MLX5_SET(rx_hash_field_select, outer, l3_prot_type,
674                  tir_attr->rx_hash_field_selector_outer.l3_prot_type);
675         MLX5_SET(rx_hash_field_select, outer, l4_prot_type,
676                  tir_attr->rx_hash_field_selector_outer.l4_prot_type);
677         MLX5_SET(rx_hash_field_select, outer, selected_fields,
678                  tir_attr->rx_hash_field_selector_outer.selected_fields);
679         inner = MLX5_ADDR_OF(tirc, tir_ctx, rx_hash_field_selector_inner);
680         MLX5_SET(rx_hash_field_select, inner, l3_prot_type,
681                  tir_attr->rx_hash_field_selector_inner.l3_prot_type);
682         MLX5_SET(rx_hash_field_select, inner, l4_prot_type,
683                  tir_attr->rx_hash_field_selector_inner.l4_prot_type);
684         MLX5_SET(rx_hash_field_select, inner, selected_fields,
685                  tir_attr->rx_hash_field_selector_inner.selected_fields);
686         tir->obj = mlx5_glue->devx_obj_create(ctx, in, sizeof(in),
687                                                    out, sizeof(out));
688         if (!tir->obj) {
689                 DRV_LOG(ERR, "Failed to create TIR using DevX");
690                 rte_errno = errno;
691                 rte_free(tir);
692                 return NULL;
693         }
694         tir->id = MLX5_GET(create_tir_out, out, tirn);
695         return tir;
696 }
697
698 /**
699  * Create RQT using DevX API.
700  *
701  * @param[in] ctx
702  *   ibv_context returned from mlx5dv_open_device.
703  * @param [in] rqt_attr
704  *   Pointer to RQT attributes structure.
705  *
706  * @return
707  *   The DevX object created, NULL otherwise and rte_errno is set.
708  */
709 struct mlx5_devx_obj *
710 mlx5_devx_cmd_create_rqt(struct ibv_context *ctx,
711                          struct mlx5_devx_rqt_attr *rqt_attr)
712 {
713         uint32_t *in = NULL;
714         uint32_t inlen = MLX5_ST_SZ_BYTES(create_rqt_in) +
715                          rqt_attr->rqt_actual_size * sizeof(uint32_t);
716         uint32_t out[MLX5_ST_SZ_DW(create_rqt_out)] = {0};
717         void *rqt_ctx;
718         struct mlx5_devx_obj *rqt = NULL;
719         int i;
720
721         in = rte_calloc(__func__, 1, inlen, 0);
722         if (!in) {
723                 DRV_LOG(ERR, "Failed to allocate RQT IN data");
724                 rte_errno = ENOMEM;
725                 return NULL;
726         }
727         rqt = rte_calloc(__func__, 1, sizeof(*rqt), 0);
728         if (!rqt) {
729                 DRV_LOG(ERR, "Failed to allocate RQT data");
730                 rte_errno = ENOMEM;
731                 rte_free(in);
732                 return NULL;
733         }
734         MLX5_SET(create_rqt_in, in, opcode, MLX5_CMD_OP_CREATE_RQT);
735         rqt_ctx = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
736         MLX5_SET(rqtc, rqt_ctx, rqt_max_size, rqt_attr->rqt_max_size);
737         MLX5_SET(rqtc, rqt_ctx, rqt_actual_size, rqt_attr->rqt_actual_size);
738         for (i = 0; i < rqt_attr->rqt_actual_size; i++)
739                 MLX5_SET(rqtc, rqt_ctx, rq_num[i], rqt_attr->rq_list[i]);
740         rqt->obj = mlx5_glue->devx_obj_create(ctx, in, inlen, out, sizeof(out));
741         rte_free(in);
742         if (!rqt->obj) {
743                 DRV_LOG(ERR, "Failed to create RQT using DevX");
744                 rte_errno = errno;
745                 rte_free(rqt);
746                 return NULL;
747         }
748         rqt->id = MLX5_GET(create_rqt_out, out, rqtn);
749         return rqt;
750 }
751
752 /**
753  * Create SQ using DevX API.
754  *
755  * @param[in] ctx
756  *   ibv_context returned from mlx5dv_open_device.
757  * @param [in] sq_attr
758  *   Pointer to SQ attributes structure.
759  * @param [in] socket
760  *   CPU socket ID for allocations.
761  *
762  * @return
763  *   The DevX object created, NULL otherwise and rte_errno is set.
764  **/
765 struct mlx5_devx_obj *
766 mlx5_devx_cmd_create_sq(struct ibv_context *ctx,
767                         struct mlx5_devx_create_sq_attr *sq_attr)
768 {
769         uint32_t in[MLX5_ST_SZ_DW(create_sq_in)] = {0};
770         uint32_t out[MLX5_ST_SZ_DW(create_sq_out)] = {0};
771         void *sq_ctx;
772         void *wq_ctx;
773         struct mlx5_devx_wq_attr *wq_attr;
774         struct mlx5_devx_obj *sq = NULL;
775
776         sq = rte_calloc(__func__, 1, sizeof(*sq), 0);
777         if (!sq) {
778                 DRV_LOG(ERR, "Failed to allocate SQ data");
779                 rte_errno = ENOMEM;
780                 return NULL;
781         }
782         MLX5_SET(create_sq_in, in, opcode, MLX5_CMD_OP_CREATE_SQ);
783         sq_ctx = MLX5_ADDR_OF(create_sq_in, in, ctx);
784         MLX5_SET(sqc, sq_ctx, rlky, sq_attr->rlky);
785         MLX5_SET(sqc, sq_ctx, cd_master, sq_attr->cd_master);
786         MLX5_SET(sqc, sq_ctx, fre, sq_attr->fre);
787         MLX5_SET(sqc, sq_ctx, flush_in_error_en, sq_attr->flush_in_error_en);
788         MLX5_SET(sqc, sq_ctx, allow_multi_pkt_send_wqe,
789                  sq_attr->flush_in_error_en);
790         MLX5_SET(sqc, sq_ctx, min_wqe_inline_mode,
791                  sq_attr->min_wqe_inline_mode);
792         MLX5_SET(sqc, sq_ctx, state, sq_attr->state);
793         MLX5_SET(sqc, sq_ctx, reg_umr, sq_attr->reg_umr);
794         MLX5_SET(sqc, sq_ctx, allow_swp, sq_attr->allow_swp);
795         MLX5_SET(sqc, sq_ctx, hairpin, sq_attr->hairpin);
796         MLX5_SET(sqc, sq_ctx, user_index, sq_attr->user_index);
797         MLX5_SET(sqc, sq_ctx, cqn, sq_attr->cqn);
798         MLX5_SET(sqc, sq_ctx, packet_pacing_rate_limit_index,
799                  sq_attr->packet_pacing_rate_limit_index);
800         MLX5_SET(sqc, sq_ctx, tis_lst_sz, sq_attr->tis_lst_sz);
801         MLX5_SET(sqc, sq_ctx, tis_num_0, sq_attr->tis_num);
802         wq_ctx = MLX5_ADDR_OF(sqc, sq_ctx, wq);
803         wq_attr = &sq_attr->wq_attr;
804         devx_cmd_fill_wq_data(wq_ctx, wq_attr);
805         sq->obj = mlx5_glue->devx_obj_create(ctx, in, sizeof(in),
806                                              out, sizeof(out));
807         if (!sq->obj) {
808                 DRV_LOG(ERR, "Failed to create SQ using DevX");
809                 rte_errno = errno;
810                 rte_free(sq);
811                 return NULL;
812         }
813         sq->id = MLX5_GET(create_sq_out, out, sqn);
814         return sq;
815 }
816
817 /**
818  * Modify SQ using DevX API.
819  *
820  * @param[in] sq
821  *   Pointer to SQ object structure.
822  * @param [in] sq_attr
823  *   Pointer to SQ attributes structure.
824  *
825  * @return
826  *   0 on success, a negative errno value otherwise and rte_errno is set.
827  */
828 int
829 mlx5_devx_cmd_modify_sq(struct mlx5_devx_obj *sq,
830                         struct mlx5_devx_modify_sq_attr *sq_attr)
831 {
832         uint32_t in[MLX5_ST_SZ_DW(modify_sq_in)] = {0};
833         uint32_t out[MLX5_ST_SZ_DW(modify_sq_out)] = {0};
834         void *sq_ctx;
835         int ret;
836
837         MLX5_SET(modify_sq_in, in, opcode, MLX5_CMD_OP_MODIFY_SQ);
838         MLX5_SET(modify_sq_in, in, sq_state, sq_attr->sq_state);
839         MLX5_SET(modify_sq_in, in, sqn, sq->id);
840         sq_ctx = MLX5_ADDR_OF(modify_sq_in, in, ctx);
841         MLX5_SET(sqc, sq_ctx, state, sq_attr->state);
842         MLX5_SET(sqc, sq_ctx, hairpin_peer_rq, sq_attr->hairpin_peer_rq);
843         MLX5_SET(sqc, sq_ctx, hairpin_peer_vhca, sq_attr->hairpin_peer_vhca);
844         ret = mlx5_glue->devx_obj_modify(sq->obj, in, sizeof(in),
845                                          out, sizeof(out));
846         if (ret) {
847                 DRV_LOG(ERR, "Failed to modify SQ using DevX");
848                 rte_errno = errno;
849                 return -errno;
850         }
851         return ret;
852 }
853
854 /**
855  * Create TIS using DevX API.
856  *
857  * @param[in] ctx
858  *   ibv_context returned from mlx5dv_open_device.
859  * @param [in] tis_attr
860  *   Pointer to TIS attributes structure.
861  *
862  * @return
863  *   The DevX object created, NULL otherwise and rte_errno is set.
864  */
865 struct mlx5_devx_obj *
866 mlx5_devx_cmd_create_tis(struct ibv_context *ctx,
867                          struct mlx5_devx_tis_attr *tis_attr)
868 {
869         uint32_t in[MLX5_ST_SZ_DW(create_tis_in)] = {0};
870         uint32_t out[MLX5_ST_SZ_DW(create_tis_out)] = {0};
871         struct mlx5_devx_obj *tis = NULL;
872         void *tis_ctx;
873
874         tis = rte_calloc(__func__, 1, sizeof(*tis), 0);
875         if (!tis) {
876                 DRV_LOG(ERR, "Failed to allocate TIS object");
877                 rte_errno = ENOMEM;
878                 return NULL;
879         }
880         MLX5_SET(create_tis_in, in, opcode, MLX5_CMD_OP_CREATE_TIS);
881         tis_ctx = MLX5_ADDR_OF(create_tis_in, in, ctx);
882         MLX5_SET(tisc, tis_ctx, strict_lag_tx_port_affinity,
883                  tis_attr->strict_lag_tx_port_affinity);
884         MLX5_SET(tisc, tis_ctx, strict_lag_tx_port_affinity,
885                  tis_attr->strict_lag_tx_port_affinity);
886         MLX5_SET(tisc, tis_ctx, prio, tis_attr->prio);
887         MLX5_SET(tisc, tis_ctx, transport_domain,
888                  tis_attr->transport_domain);
889         tis->obj = mlx5_glue->devx_obj_create(ctx, in, sizeof(in),
890                                               out, sizeof(out));
891         if (!tis->obj) {
892                 DRV_LOG(ERR, "Failed to create TIS using DevX");
893                 rte_errno = errno;
894                 rte_free(tis);
895                 return NULL;
896         }
897         tis->id = MLX5_GET(create_tis_out, out, tisn);
898         return tis;
899 }
900
901 /**
902  * Create transport domain using DevX API.
903  *
904  * @param[in] ctx
905  *   ibv_context returned from mlx5dv_open_device.
906  *
907  * @return
908  *   The DevX object created, NULL otherwise and rte_errno is set.
909  */
910 struct mlx5_devx_obj *
911 mlx5_devx_cmd_create_td(struct ibv_context *ctx)
912 {
913         uint32_t in[MLX5_ST_SZ_DW(alloc_transport_domain_in)] = {0};
914         uint32_t out[MLX5_ST_SZ_DW(alloc_transport_domain_out)] = {0};
915         struct mlx5_devx_obj *td = NULL;
916
917         td = rte_calloc(__func__, 1, sizeof(*td), 0);
918         if (!td) {
919                 DRV_LOG(ERR, "Failed to allocate TD object");
920                 rte_errno = ENOMEM;
921                 return NULL;
922         }
923         MLX5_SET(alloc_transport_domain_in, in, opcode,
924                  MLX5_CMD_OP_ALLOC_TRANSPORT_DOMAIN);
925         td->obj = mlx5_glue->devx_obj_create(ctx, in, sizeof(in),
926                                              out, sizeof(out));
927         if (!td->obj) {
928                 DRV_LOG(ERR, "Failed to create TIS using DevX");
929                 rte_errno = errno;
930                 rte_free(td);
931                 return NULL;
932         }
933         td->id = MLX5_GET(alloc_transport_domain_out, out,
934                            transport_domain);
935         return td;
936 }
937
938 /**
939  * Dump all flows to file.
940  *
941  * @param[in] fdb_domain
942  *   FDB domain.
943  * @param[in] rx_domain
944  *   RX domain.
945  * @param[in] tx_domain
946  *   TX domain.
947  * @param[out] file
948  *   Pointer to file stream.
949  *
950  * @return
951  *   0 on success, a nagative value otherwise.
952  */
953 int
954 mlx5_devx_cmd_flow_dump(void *fdb_domain __rte_unused,
955                         void *rx_domain __rte_unused,
956                         void *tx_domain __rte_unused, FILE *file __rte_unused)
957 {
958         int ret = 0;
959
960 #ifdef HAVE_MLX5_DR_FLOW_DUMP
961         if (fdb_domain) {
962                 ret = mlx5_glue->dr_dump_domain(file, fdb_domain);
963                 if (ret)
964                         return ret;
965         }
966         assert(rx_domain);
967         ret = mlx5_glue->dr_dump_domain(file, rx_domain);
968         if (ret)
969                 return ret;
970         assert(tx_domain);
971         ret = mlx5_glue->dr_dump_domain(file, tx_domain);
972 #else
973         ret = ENOTSUP;
974 #endif
975         return -ret;
976 }