1 // SPDX-License-Identifier: BSD-3-Clause
2 /* Copyright 2018 Mellanox Technologies, Ltd */
4 #include <rte_flow_driver.h>
5 #include <rte_malloc.h>
13 * Allocate flow counters via devx interface.
16 * ibv contexts returned from mlx5dv_open_device.
18 * Pointer to counters properties structure to be filled by the routine.
20 * Bulk counter numbers in 128 counters units.
23 * Pointer to counter object on success, a negative value otherwise and
26 struct mlx5_devx_obj *
27 mlx5_devx_cmd_flow_counter_alloc(struct ibv_context *ctx, uint32_t bulk_n_128)
29 struct mlx5_devx_obj *dcs = rte_zmalloc("dcs", sizeof(*dcs), 0);
30 uint32_t in[MLX5_ST_SZ_DW(alloc_flow_counter_in)] = {0};
31 uint32_t out[MLX5_ST_SZ_DW(alloc_flow_counter_out)] = {0};
37 MLX5_SET(alloc_flow_counter_in, in, opcode,
38 MLX5_CMD_OP_ALLOC_FLOW_COUNTER);
39 MLX5_SET(alloc_flow_counter_in, in, flow_counter_bulk, bulk_n_128);
40 dcs->obj = mlx5_glue->devx_obj_create(ctx, in,
41 sizeof(in), out, sizeof(out));
43 DRV_LOG(ERR, "Can't allocate counters - error %d\n", errno);
48 dcs->id = MLX5_GET(alloc_flow_counter_out, out, flow_counter_id);
53 * Query flow counters values.
56 * devx object that was obtained from mlx5_devx_cmd_fc_alloc.
58 * Whether hardware should clear the counters after the query or not.
59 * @param[in] n_counters
60 * The counter number to read.
62 * The number of packets that matched the flow.
64 * The number of bytes that matched the flow.
66 * The mkey key for batch query.
68 * The address in the mkey range for batch query.
71 * 0 on success, a negative value otherwise.
74 mlx5_devx_cmd_flow_counter_query(struct mlx5_devx_obj *dcs, int clear,
75 uint32_t n_counters, uint64_t *pkts,
76 uint64_t *bytes, uint32_t mkey, void *addr)
78 int out_len = MLX5_ST_SZ_BYTES(query_flow_counter_out) +
79 MLX5_ST_SZ_BYTES(traffic_counter);
80 uint32_t out[out_len];
81 uint32_t in[MLX5_ST_SZ_DW(query_flow_counter_in)] = {0};
85 MLX5_SET(query_flow_counter_in, in, opcode,
86 MLX5_CMD_OP_QUERY_FLOW_COUNTER);
87 MLX5_SET(query_flow_counter_in, in, op_mod, 0);
88 MLX5_SET(query_flow_counter_in, in, flow_counter_id, dcs->id);
89 MLX5_SET(query_flow_counter_in, in, clear, !!clear);
92 MLX5_SET(query_flow_counter_in, in, num_of_counters,
94 MLX5_SET(query_flow_counter_in, in, dump_to_memory, 1);
95 MLX5_SET(query_flow_counter_in, in, mkey, mkey);
96 MLX5_SET64(query_flow_counter_in, in, address,
97 (uint64_t)(uintptr_t)addr);
99 rc = mlx5_glue->devx_obj_query(dcs->obj, in, sizeof(in), out, out_len);
101 DRV_LOG(ERR, "Failed to query devx counters with rc %d\n ", rc);
106 stats = MLX5_ADDR_OF(query_flow_counter_out,
107 out, flow_statistics);
108 *pkts = MLX5_GET64(traffic_counter, stats, packets);
109 *bytes = MLX5_GET64(traffic_counter, stats, octets);
118 * ibv contexts returned from mlx5dv_open_device.
120 * Attributes of the requested mkey.
123 * Pointer to Devx mkey on success, a negative value otherwise and rte_errno
126 struct mlx5_devx_obj *
127 mlx5_devx_cmd_mkey_create(struct ibv_context *ctx,
128 struct mlx5_devx_mkey_attr *attr)
130 uint32_t in[MLX5_ST_SZ_DW(create_mkey_in)] = {0};
131 uint32_t out[MLX5_ST_SZ_DW(create_mkey_out)] = {0};
133 struct mlx5_devx_obj *mkey = rte_zmalloc("mkey", sizeof(*mkey), 0);
135 uint32_t translation_size;
141 pgsize = sysconf(_SC_PAGESIZE);
142 translation_size = (RTE_ALIGN(attr->size, pgsize) * 8) / 16;
143 MLX5_SET(create_mkey_in, in, opcode, MLX5_CMD_OP_CREATE_MKEY);
144 MLX5_SET(create_mkey_in, in, translations_octword_actual_size,
146 MLX5_SET(create_mkey_in, in, mkey_umem_id, attr->umem_id);
147 mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
148 MLX5_SET(mkc, mkc, lw, 0x1);
149 MLX5_SET(mkc, mkc, lr, 0x1);
150 MLX5_SET(mkc, mkc, access_mode_1_0, MLX5_MKC_ACCESS_MODE_MTT);
151 MLX5_SET(mkc, mkc, qpn, 0xffffff);
152 MLX5_SET(mkc, mkc, pd, attr->pd);
153 MLX5_SET(mkc, mkc, mkey_7_0, attr->umem_id & 0xFF);
154 MLX5_SET(mkc, mkc, translations_octword_size, translation_size);
155 MLX5_SET64(mkc, mkc, start_addr, attr->addr);
156 MLX5_SET64(mkc, mkc, len, attr->size);
157 MLX5_SET(mkc, mkc, log_page_size, rte_log2_u32(pgsize));
158 mkey->obj = mlx5_glue->devx_obj_create(ctx, in, sizeof(in), out,
161 DRV_LOG(ERR, "Can't create mkey - error %d\n", errno);
166 mkey->id = MLX5_GET(create_mkey_out, out, mkey_index);
167 mkey->id = (mkey->id << 8) | (attr->umem_id & 0xFF);
172 * Destroy any object allocated by a Devx API.
175 * Pointer to a general object.
178 * 0 on success, a negative value otherwise.
181 mlx5_devx_cmd_destroy(struct mlx5_devx_obj *obj)
187 ret = mlx5_glue->devx_obj_destroy(obj->obj);
193 * Query HCA attributes.
194 * Using those attributes we can check on run time if the device
195 * is having the required capabilities.
198 * ibv contexts returned from mlx5dv_open_device.
200 * Attributes device values.
203 * 0 on success, a negative value otherwise.
206 mlx5_devx_cmd_query_hca_attr(struct ibv_context *ctx,
207 struct mlx5_hca_attr *attr)
209 uint32_t in[MLX5_ST_SZ_DW(query_hca_cap_in)] = {0};
210 uint32_t out[MLX5_ST_SZ_DW(query_hca_cap_out)] = {0};
212 int status, syndrome, rc;
214 MLX5_SET(query_hca_cap_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_CAP);
215 MLX5_SET(query_hca_cap_in, in, op_mod,
216 MLX5_GET_HCA_CAP_OP_MOD_GENERAL_DEVICE |
217 MLX5_HCA_CAP_OPMOD_GET_CUR);
219 rc = mlx5_glue->devx_general_cmd(ctx,
220 in, sizeof(in), out, sizeof(out));
223 status = MLX5_GET(query_hca_cap_out, out, status);
224 syndrome = MLX5_GET(query_hca_cap_out, out, syndrome);
226 DRV_LOG(DEBUG, "Failed to query devx HCA capabilities, "
227 "status %x, syndrome = %x",
231 hcattr = MLX5_ADDR_OF(query_hca_cap_out, out, capability);
232 attr->flow_counter_bulk_alloc_bitmap =
233 MLX5_GET(cmd_hca_cap, hcattr, flow_counter_bulk_alloc);
234 attr->eswitch_manager = MLX5_GET(cmd_hca_cap, hcattr, eswitch_manager);