92f2fc8307de573e19746c0c02803880fa57a393
[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 <rte_flow_driver.h>
5 #include <rte_malloc.h>
6 #include <unistd.h>
7
8 #include "mlx5.h"
9 #include "mlx5_glue.h"
10 #include "mlx5_prm.h"
11
12 /**
13  * Allocate flow counters via devx interface.
14  *
15  * @param[in] ctx
16  *   ibv contexts returned from mlx5dv_open_device.
17  * @param dcs
18  *   Pointer to counters properties structure to be filled by the routine.
19  * @param bulk_n_128
20  *   Bulk counter numbers in 128 counters units.
21  *
22  * @return
23  *   Pointer to counter object on success, a negative value otherwise and
24  *   rte_errno is set.
25  */
26 struct mlx5_devx_obj *
27 mlx5_devx_cmd_flow_counter_alloc(struct ibv_context *ctx, uint32_t bulk_n_128)
28 {
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};
32
33         if (!dcs) {
34                 rte_errno = ENOMEM;
35                 return NULL;
36         }
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));
42         if (!dcs->obj) {
43                 DRV_LOG(ERR, "Can't allocate counters - error %d\n", errno);
44                 rte_errno = errno;
45                 rte_free(dcs);
46                 return NULL;
47         }
48         dcs->id = MLX5_GET(alloc_flow_counter_out, out, flow_counter_id);
49         return dcs;
50 }
51
52 /**
53  * Query flow counters values.
54  *
55  * @param[in] dcs
56  *   devx object that was obtained from mlx5_devx_cmd_fc_alloc.
57  * @param[in] clear
58  *   Whether hardware should clear the counters after the query or not.
59  * @param[in] n_counters
60  *   The counter number to read.
61  *  @param pkts
62  *   The number of packets that matched the flow.
63  *  @param bytes
64  *    The number of bytes that matched the flow.
65  *  @param mkey
66  *   The mkey key for batch query.
67  *  @param addr
68  *    The address in the mkey range for batch query.
69  *
70  * @return
71  *   0 on success, a negative value otherwise.
72  */
73 int
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)
77 {
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};
82         void *stats;
83         int rc;
84
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);
90
91         if (n_counters) {
92                 MLX5_SET(query_flow_counter_in, in, num_of_counters,
93                          n_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);
98         }
99         rc = mlx5_glue->devx_obj_query(dcs->obj, in, sizeof(in), out, out_len);
100         if (rc) {
101                 DRV_LOG(ERR, "Failed to query devx counters with rc %d\n ", rc);
102                 rte_errno = rc;
103                 return -rc;
104         }
105         if (!n_counters) {
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);
110         }
111         return 0;
112 }
113
114 /**
115  * Create a new mkey.
116  *
117  * @param[in] ctx
118  *   ibv contexts returned from mlx5dv_open_device.
119  * @param[in] attr
120  *   Attributes of the requested mkey.
121  *
122  * @return
123  *   Pointer to Devx mkey on success, a negative value otherwise and rte_errno
124  *   is set.
125  */
126 struct mlx5_devx_obj *
127 mlx5_devx_cmd_mkey_create(struct ibv_context *ctx,
128                           struct mlx5_devx_mkey_attr *attr)
129 {
130         uint32_t in[MLX5_ST_SZ_DW(create_mkey_in)] = {0};
131         uint32_t out[MLX5_ST_SZ_DW(create_mkey_out)] = {0};
132         void *mkc;
133         struct mlx5_devx_obj *mkey = rte_zmalloc("mkey", sizeof(*mkey), 0);
134         size_t pgsize;
135         uint32_t translation_size;
136
137         if (!mkey) {
138                 rte_errno = ENOMEM;
139                 return NULL;
140         }
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,
145                  translation_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,
159                                                sizeof(out));
160         if (!mkey->obj) {
161                 DRV_LOG(ERR, "Can't create mkey - error %d\n", errno);
162                 rte_errno = errno;
163                 rte_free(mkey);
164                 return NULL;
165         }
166         mkey->id = MLX5_GET(create_mkey_out, out, mkey_index);
167         mkey->id = (mkey->id << 8) | (attr->umem_id & 0xFF);
168         return mkey;
169 }
170
171 /**
172  * Destroy any object allocated by a Devx API.
173  *
174  * @param[in] obj
175  *   Pointer to a general object.
176  *
177  * @return
178  *   0 on success, a negative value otherwise.
179  */
180 int
181 mlx5_devx_cmd_destroy(struct mlx5_devx_obj *obj)
182 {
183         int ret;
184
185         if (!obj)
186                 return 0;
187         ret =  mlx5_glue->devx_obj_destroy(obj->obj);
188         rte_free(obj);
189         return ret;
190 }
191
192 /**
193  * Query HCA attributes.
194  * Using those attributes we can check on run time if the device
195  * is having the required capabilities.
196  *
197  * @param[in] ctx
198  *   ibv contexts returned from mlx5dv_open_device.
199  * @param[out] attr
200  *   Attributes device values.
201  *
202  * @return
203  *   0 on success, a negative value otherwise.
204  */
205 int
206 mlx5_devx_cmd_query_hca_attr(struct ibv_context *ctx,
207                              struct mlx5_hca_attr *attr)
208 {
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};
211         void *hcattr;
212         int status, syndrome, rc;
213
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);
218
219         rc = mlx5_glue->devx_general_cmd(ctx,
220                                          in, sizeof(in), out, sizeof(out));
221         if (rc)
222                 return rc;
223         status = MLX5_GET(query_hca_cap_out, out, status);
224         syndrome = MLX5_GET(query_hca_cap_out, out, syndrome);
225         if (status) {
226                 DRV_LOG(DEBUG, "Failed to query devx HCA capabilities, "
227                         "status %x, syndrome = %x",
228                         status, syndrome);
229                 return -1;
230         }
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);
235         return 0;
236 }