Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright 2020 Mellanox Technologies, Ltd
3 : : */
4 : :
5 : : #include <stddef.h>
6 : : #include <errno.h>
7 : : #include <string.h>
8 : : #include <stdint.h>
9 : : #include <unistd.h>
10 : : #include <inttypes.h>
11 : : #include <sys/queue.h>
12 : :
13 : : #include "mlx5_autoconf.h"
14 : :
15 : : #include <rte_mbuf.h>
16 : : #include <rte_malloc.h>
17 : : #include <ethdev_driver.h>
18 : : #include <rte_common.h>
19 : : #include <rte_eal_paging.h>
20 : :
21 : : #include <mlx5_glue.h>
22 : : #include <mlx5_common.h>
23 : : #include <mlx5_common_mr.h>
24 : : #include <mlx5_verbs.h>
25 : : #include <mlx5_rx.h>
26 : : #include <mlx5_tx.h>
27 : : #include <mlx5_utils.h>
28 : : #include <mlx5_malloc.h>
29 : :
30 : : /**
31 : : * Modify Rx WQ vlan stripping offload
32 : : *
33 : : * @param rxq
34 : : * Rx queue.
35 : : *
36 : : * @return 0 on success, non-0 otherwise
37 : : */
38 : : static int
39 : 0 : mlx5_rxq_obj_modify_wq_vlan_strip(struct mlx5_rxq_priv *rxq, int on)
40 : : {
41 : : uint16_t vlan_offloads =
42 : 0 : (on ? IBV_WQ_FLAGS_CVLAN_STRIPPING : 0) |
43 : : 0;
44 : : struct ibv_wq_attr mod;
45 : 0 : mod = (struct ibv_wq_attr){
46 : : .attr_mask = IBV_WQ_ATTR_FLAGS,
47 : : .flags_mask = IBV_WQ_FLAGS_CVLAN_STRIPPING,
48 : : .flags = vlan_offloads,
49 : : };
50 : :
51 : 0 : return mlx5_glue->modify_wq(rxq->ctrl->obj->wq, &mod);
52 : : }
53 : :
54 : : /**
55 : : * Modifies the attributes for the specified WQ.
56 : : *
57 : : * @param rxq
58 : : * Verbs Rx queue.
59 : : * @param type
60 : : * Type of change queue state.
61 : : *
62 : : * @return
63 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
64 : : */
65 : : static int
66 : 0 : mlx5_ibv_modify_wq(struct mlx5_rxq_priv *rxq, uint8_t type)
67 : : {
68 : 0 : struct ibv_wq_attr mod = {
69 : : .attr_mask = IBV_WQ_ATTR_STATE,
70 : 0 : .wq_state = (enum ibv_wq_state)type,
71 : : };
72 : :
73 : 0 : return mlx5_glue->modify_wq(rxq->ctrl->obj->wq, &mod);
74 : : }
75 : :
76 : : /**
77 : : * Modify QP using Verbs API.
78 : : *
79 : : * @param txq_obj
80 : : * Verbs Tx queue object.
81 : : * @param type
82 : : * Type of change queue state.
83 : : * @param dev_port
84 : : * IB device port number.
85 : : *
86 : : * @return
87 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
88 : : */
89 : : static int
90 : 0 : mlx5_ibv_modify_qp(struct mlx5_txq_obj *obj, enum mlx5_txq_modify_type type,
91 : : uint8_t dev_port)
92 : : {
93 : 0 : struct ibv_qp_attr mod = {
94 : : .qp_state = IBV_QPS_RESET,
95 : : .port_num = dev_port,
96 : : };
97 : : int ret;
98 : :
99 [ # # ]: 0 : if (type != MLX5_TXQ_MOD_RST2RDY) {
100 : 0 : ret = mlx5_glue->modify_qp(obj->qp, &mod, IBV_QP_STATE);
101 [ # # ]: 0 : if (ret) {
102 : 0 : DRV_LOG(ERR, "Cannot change Tx QP state to RESET %s",
103 : : strerror(errno));
104 : 0 : rte_errno = errno;
105 : 0 : return ret;
106 : : }
107 [ # # ]: 0 : if (type == MLX5_TXQ_MOD_RDY2RST)
108 : : return 0;
109 : : }
110 : 0 : mod.qp_state = IBV_QPS_INIT;
111 : 0 : ret = mlx5_glue->modify_qp(obj->qp, &mod, IBV_QP_STATE | IBV_QP_PORT);
112 [ # # ]: 0 : if (ret) {
113 : 0 : DRV_LOG(ERR, "Cannot change Tx QP state to INIT %s",
114 : : strerror(errno));
115 : 0 : rte_errno = errno;
116 : 0 : return ret;
117 : : }
118 : 0 : mod.qp_state = IBV_QPS_RTR;
119 : 0 : ret = mlx5_glue->modify_qp(obj->qp, &mod, IBV_QP_STATE);
120 [ # # ]: 0 : if (ret) {
121 : 0 : DRV_LOG(ERR, "Cannot change Tx QP state to RTR %s",
122 : : strerror(errno));
123 : 0 : rte_errno = errno;
124 : 0 : return ret;
125 : : }
126 : 0 : mod.qp_state = IBV_QPS_RTS;
127 : 0 : ret = mlx5_glue->modify_qp(obj->qp, &mod, IBV_QP_STATE);
128 [ # # ]: 0 : if (ret) {
129 : 0 : DRV_LOG(ERR, "Cannot change Tx QP state to RTS %s",
130 : : strerror(errno));
131 : 0 : rte_errno = errno;
132 : 0 : return ret;
133 : : }
134 : : return 0;
135 : : }
136 : :
137 : : /**
138 : : * Create a CQ Verbs object.
139 : : *
140 : : * @param rxq
141 : : * Pointer to Rx queue.
142 : : *
143 : : * @return
144 : : * The Verbs CQ object initialized, NULL otherwise and rte_errno is set.
145 : : */
146 : : static struct ibv_cq *
147 : 0 : mlx5_rxq_ibv_cq_create(struct mlx5_rxq_priv *rxq)
148 : : {
149 : 0 : struct mlx5_priv *priv = rxq->priv;
150 : 0 : struct mlx5_rxq_ctrl *rxq_ctrl = rxq->ctrl;
151 : 0 : struct mlx5_rxq_data *rxq_data = &rxq_ctrl->rxq;
152 : 0 : struct mlx5_rxq_obj *rxq_obj = rxq_ctrl->obj;
153 : 0 : unsigned int cqe_n = mlx5_rxq_cqe_num(rxq_data);
154 : : struct {
155 : : struct ibv_cq_init_attr_ex ibv;
156 : : struct mlx5dv_cq_init_attr mlx5;
157 : : } cq_attr;
158 : :
159 : 0 : cq_attr.ibv = (struct ibv_cq_init_attr_ex){
160 : : .cqe = cqe_n,
161 : 0 : .channel = rxq_obj->ibv_channel,
162 : : .comp_mask = 0,
163 : : };
164 : 0 : cq_attr.mlx5 = (struct mlx5dv_cq_init_attr){
165 : : .comp_mask = 0,
166 : : };
167 [ # # # # ]: 0 : if (priv->config.cqe_comp && !rxq_data->hw_timestamp) {
168 : 0 : cq_attr.mlx5.comp_mask |=
169 : : MLX5DV_CQ_INIT_ATTR_MASK_COMPRESSED_CQE;
170 [ # # ]: 0 : rxq_data->byte_mask = UINT32_MAX;
171 : : #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
172 [ # # ]: 0 : if (mlx5_rxq_mprq_enabled(rxq_data)) {
173 : 0 : cq_attr.mlx5.cqe_comp_res_format =
174 : : MLX5DV_CQE_RES_FORMAT_CSUM_STRIDX;
175 : 0 : rxq_data->mcqe_format =
176 : : MLX5_CQE_RESP_FORMAT_CSUM_STRIDX;
177 : : } else {
178 : 0 : cq_attr.mlx5.cqe_comp_res_format =
179 : : MLX5DV_CQE_RES_FORMAT_HASH;
180 : 0 : rxq_data->mcqe_format =
181 : : MLX5_CQE_RESP_FORMAT_HASH;
182 : : }
183 : : #else
184 : : cq_attr.mlx5.cqe_comp_res_format = MLX5DV_CQE_RES_FORMAT_HASH;
185 : : rxq_data->mcqe_format = MLX5_CQE_RESP_FORMAT_HASH;
186 : : #endif
187 : : /*
188 : : * For vectorized Rx, it must not be doubled in order to
189 : : * make cq_ci and rq_ci aligned.
190 : : */
191 [ # # ]: 0 : if (mlx5_rxq_check_vec_support(rxq_data) < 0)
192 : 0 : cq_attr.ibv.cqe *= 2;
193 [ # # # # ]: 0 : } else if (priv->config.cqe_comp && rxq_data->hw_timestamp) {
194 : 0 : DRV_LOG(DEBUG,
195 : : "Port %u Rx CQE compression is disabled for HW"
196 : : " timestamp.",
197 : : priv->dev_data->port_id);
198 : : }
199 : : #ifdef HAVE_IBV_MLX5_MOD_CQE_128B_PAD
200 : : if (RTE_CACHE_LINE_SIZE == 128) {
201 : : cq_attr.mlx5.comp_mask |= MLX5DV_CQ_INIT_ATTR_MASK_FLAGS;
202 : : cq_attr.mlx5.flags |= MLX5DV_CQ_INIT_ATTR_FLAGS_CQE_PAD;
203 : : }
204 : : #endif
205 : 0 : return mlx5_glue->cq_ex_to_cq(mlx5_glue->dv_create_cq
206 : 0 : (priv->sh->cdev->ctx,
207 : : &cq_attr.ibv,
208 : : &cq_attr.mlx5));
209 : : }
210 : :
211 : : /**
212 : : * Create a WQ Verbs object.
213 : : *
214 : : * @param rxq
215 : : * Pointer to Rx queue.
216 : : *
217 : : * @return
218 : : * The Verbs WQ object initialized, NULL otherwise and rte_errno is set.
219 : : */
220 : : static struct ibv_wq *
221 : 0 : mlx5_rxq_ibv_wq_create(struct mlx5_rxq_priv *rxq)
222 : : {
223 : 0 : struct mlx5_priv *priv = rxq->priv;
224 : 0 : struct mlx5_rxq_ctrl *rxq_ctrl = rxq->ctrl;
225 : : struct mlx5_rxq_data *rxq_data = &rxq_ctrl->rxq;
226 : 0 : struct mlx5_rxq_obj *rxq_obj = rxq_ctrl->obj;
227 : 0 : unsigned int wqe_n = 1 << rxq_data->elts_n;
228 : : struct {
229 : : struct ibv_wq_init_attr ibv;
230 : : #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
231 : : struct mlx5dv_wq_init_attr mlx5;
232 : : #endif
233 : : } wq_attr;
234 : :
235 : 0 : wq_attr.ibv = (struct ibv_wq_init_attr){
236 : : .wq_context = NULL, /* Could be useful in the future. */
237 : : .wq_type = IBV_WQT_RQ,
238 : : /* Max number of outstanding WRs. */
239 : 0 : .max_wr = wqe_n >> rxq_data->sges_n,
240 : : /* Max number of scatter/gather elements in a WR. */
241 : 0 : .max_sge = 1 << rxq_data->sges_n,
242 : 0 : .pd = priv->sh->cdev->pd,
243 : 0 : .cq = rxq_obj->ibv_cq,
244 : : .comp_mask = IBV_WQ_FLAGS_CVLAN_STRIPPING | 0,
245 : 0 : .create_flags = (rxq_data->vlan_strip ?
246 : 0 : IBV_WQ_FLAGS_CVLAN_STRIPPING : 0),
247 : : };
248 : : /* By default, FCS (CRC) is stripped by hardware. */
249 [ # # ]: 0 : if (rxq_data->crc_present) {
250 : 0 : wq_attr.ibv.create_flags |= IBV_WQ_FLAGS_SCATTER_FCS;
251 : : wq_attr.ibv.comp_mask |= IBV_WQ_INIT_ATTR_FLAGS;
252 : : }
253 [ # # ]: 0 : if (priv->config.hw_padding) {
254 : : #if defined(HAVE_IBV_WQ_FLAG_RX_END_PADDING)
255 : : wq_attr.ibv.create_flags |= IBV_WQ_FLAG_RX_END_PADDING;
256 : : wq_attr.ibv.comp_mask |= IBV_WQ_INIT_ATTR_FLAGS;
257 : : #elif defined(HAVE_IBV_WQ_FLAGS_PCI_WRITE_END_PADDING)
258 : 0 : wq_attr.ibv.create_flags |= IBV_WQ_FLAGS_PCI_WRITE_END_PADDING;
259 : 0 : wq_attr.ibv.comp_mask |= IBV_WQ_INIT_ATTR_FLAGS;
260 : : #endif
261 : : }
262 : : #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
263 [ # # ]: 0 : wq_attr.mlx5 = (struct mlx5dv_wq_init_attr){
264 : : .comp_mask = 0,
265 : : };
266 [ # # ]: 0 : if (mlx5_rxq_mprq_enabled(rxq_data)) {
267 : : struct mlx5dv_striding_rq_init_attr *mprq_attr =
268 : : &wq_attr.mlx5.striding_rq_attrs;
269 : :
270 : 0 : wq_attr.mlx5.comp_mask |= MLX5DV_WQ_INIT_ATTR_MASK_STRIDING_RQ;
271 : 0 : *mprq_attr = (struct mlx5dv_striding_rq_init_attr){
272 : 0 : .single_stride_log_num_of_bytes = rxq_data->log_strd_sz,
273 : 0 : .single_wqe_log_num_of_strides = rxq_data->log_strd_num,
274 : : .two_byte_shift_en = MLX5_MPRQ_TWO_BYTE_SHIFT,
275 : : };
276 : : }
277 : 0 : rxq_obj->wq = mlx5_glue->dv_create_wq(priv->sh->cdev->ctx, &wq_attr.ibv,
278 : : &wq_attr.mlx5);
279 : : #else
280 : : rxq_obj->wq = mlx5_glue->create_wq(priv->sh->cdev->ctx, &wq_attr.ibv);
281 : : #endif
282 [ # # ]: 0 : if (rxq_obj->wq) {
283 : : /*
284 : : * Make sure number of WRs*SGEs match expectations since a queue
285 : : * cannot allocate more than "desc" buffers.
286 : : */
287 [ # # ]: 0 : if (wq_attr.ibv.max_wr != (wqe_n >> rxq_data->sges_n) ||
288 [ # # ]: 0 : wq_attr.ibv.max_sge != (1u << rxq_data->sges_n)) {
289 : 0 : DRV_LOG(ERR,
290 : : "Port %u Rx queue %u requested %u*%u but got"
291 : : " %u*%u WRs*SGEs.",
292 : : priv->dev_data->port_id, rxq->idx,
293 : : wqe_n >> rxq_data->sges_n,
294 : : (1 << rxq_data->sges_n),
295 : : wq_attr.ibv.max_wr, wq_attr.ibv.max_sge);
296 : 0 : claim_zero(mlx5_glue->destroy_wq(rxq_obj->wq));
297 : 0 : rxq_obj->wq = NULL;
298 : 0 : rte_errno = EINVAL;
299 : : }
300 : : }
301 : 0 : return rxq_obj->wq;
302 : : }
303 : :
304 : : /**
305 : : * Create the Rx queue Verbs object.
306 : : *
307 : : * @param rxq
308 : : * Pointer to Rx queue.
309 : : *
310 : : * @return
311 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
312 : : */
313 : : static int
314 : 0 : mlx5_rxq_ibv_obj_new(struct mlx5_rxq_priv *rxq)
315 : : {
316 : 0 : uint16_t idx = rxq->idx;
317 : 0 : struct mlx5_priv *priv = rxq->priv;
318 : 0 : uint16_t port_id = priv->dev_data->port_id;
319 : 0 : struct mlx5_rxq_ctrl *rxq_ctrl = rxq->ctrl;
320 : 0 : struct mlx5_rxq_data *rxq_data = &rxq_ctrl->rxq;
321 : 0 : struct mlx5_rxq_obj *tmpl = rxq_ctrl->obj;
322 : : struct mlx5dv_cq cq_info;
323 : : struct mlx5dv_rwq rwq;
324 : : int ret = 0;
325 : : struct mlx5dv_obj obj;
326 : :
327 : : MLX5_ASSERT(rxq_data);
328 : : MLX5_ASSERT(tmpl);
329 : 0 : tmpl->rxq_ctrl = rxq_ctrl;
330 [ # # ]: 0 : if (rxq_ctrl->irq) {
331 : 0 : tmpl->ibv_channel =
332 : 0 : mlx5_glue->create_comp_channel(priv->sh->cdev->ctx);
333 [ # # ]: 0 : if (!tmpl->ibv_channel) {
334 : 0 : DRV_LOG(ERR, "Port %u: comp channel creation failure.",
335 : : port_id);
336 : 0 : rte_errno = ENOMEM;
337 : 0 : goto error;
338 : : }
339 : 0 : tmpl->fd = ((struct ibv_comp_channel *)(tmpl->ibv_channel))->fd;
340 : : }
341 : : /* Create CQ using Verbs API. */
342 : 0 : tmpl->ibv_cq = mlx5_rxq_ibv_cq_create(rxq);
343 [ # # ]: 0 : if (!tmpl->ibv_cq) {
344 : 0 : DRV_LOG(ERR, "Port %u Rx queue %u CQ creation failure.",
345 : : port_id, idx);
346 : 0 : rte_errno = ENOMEM;
347 : 0 : goto error;
348 : : }
349 : 0 : obj.cq.in = tmpl->ibv_cq;
350 : 0 : obj.cq.out = &cq_info;
351 : 0 : ret = mlx5_glue->dv_init_obj(&obj, MLX5DV_OBJ_CQ);
352 [ # # ]: 0 : if (ret) {
353 : 0 : rte_errno = ret;
354 : 0 : goto error;
355 : : }
356 [ # # ]: 0 : if (cq_info.cqe_size != RTE_CACHE_LINE_SIZE) {
357 : 0 : DRV_LOG(ERR,
358 : : "Port %u wrong MLX5_CQE_SIZE environment "
359 : : "variable value: it should be set to %u.",
360 : : port_id, RTE_CACHE_LINE_SIZE);
361 : 0 : rte_errno = EINVAL;
362 : 0 : goto error;
363 : : }
364 : : /* Fill the rings. */
365 : 0 : rxq_data->cqe_n = log2above(cq_info.cqe_cnt);
366 : 0 : rxq_data->cq_db = cq_info.dbrec;
367 : 0 : rxq_data->cqes = (volatile struct mlx5_cqe (*)[])(uintptr_t)cq_info.buf;
368 : 0 : rxq_data->uar_data.db = RTE_PTR_ADD(cq_info.cq_uar, MLX5_CQ_DOORBELL);
369 : : #ifndef RTE_ARCH_64
370 : : rxq_data->uar_data.sl_p = &priv->sh->uar_lock_cq;
371 : : #endif
372 : 0 : rxq_data->cqn = cq_info.cqn;
373 : : /* Create WQ (RQ) using Verbs API. */
374 : 0 : tmpl->wq = mlx5_rxq_ibv_wq_create(rxq);
375 [ # # ]: 0 : if (!tmpl->wq) {
376 : 0 : DRV_LOG(ERR, "Port %u Rx queue %u WQ creation failure.",
377 : : port_id, idx);
378 : 0 : rte_errno = ENOMEM;
379 : 0 : goto error;
380 : : }
381 : : /* Change queue state to ready. */
382 : : ret = mlx5_ibv_modify_wq(rxq, IBV_WQS_RDY);
383 [ # # ]: 0 : if (ret) {
384 : 0 : DRV_LOG(ERR,
385 : : "Port %u Rx queue %u WQ state to IBV_WQS_RDY failed.",
386 : : port_id, idx);
387 : 0 : rte_errno = ret;
388 : 0 : goto error;
389 : : }
390 : 0 : obj.rwq.in = tmpl->wq;
391 : 0 : obj.rwq.out = &rwq;
392 : 0 : ret = mlx5_glue->dv_init_obj(&obj, MLX5DV_OBJ_RWQ);
393 [ # # ]: 0 : if (ret) {
394 : 0 : rte_errno = ret;
395 : 0 : goto error;
396 : : }
397 : 0 : rxq_data->wqes = rwq.buf;
398 : 0 : rxq_data->rq_db = rwq.dbrec;
399 : 0 : rxq_data->cq_arm_sn = 0;
400 : 0 : ret = mlx5_rxq_initialize(rxq_data);
401 [ # # ]: 0 : if (ret) {
402 : 0 : DRV_LOG(ERR, "Port %u Rx queue %u RQ initialization failure.",
403 : : priv->dev_data->port_id, rxq->idx);
404 : 0 : rte_errno = ENOMEM;
405 : 0 : goto error;
406 : : }
407 : 0 : rxq_data->cq_ci = 0;
408 : 0 : priv->dev_data->rx_queue_state[idx] = RTE_ETH_QUEUE_STATE_STARTED;
409 : 0 : rxq_ctrl->wqn = ((struct ibv_wq *)(tmpl->wq))->wq_num;
410 : 0 : return 0;
411 : 0 : error:
412 : 0 : ret = rte_errno; /* Save rte_errno before cleanup. */
413 [ # # ]: 0 : if (tmpl->wq)
414 : 0 : claim_zero(mlx5_glue->destroy_wq(tmpl->wq));
415 [ # # ]: 0 : if (tmpl->ibv_cq)
416 : 0 : claim_zero(mlx5_glue->destroy_cq(tmpl->ibv_cq));
417 [ # # ]: 0 : if (tmpl->ibv_channel)
418 : 0 : claim_zero(mlx5_glue->destroy_comp_channel(tmpl->ibv_channel));
419 : 0 : rte_errno = ret; /* Restore rte_errno. */
420 : 0 : return -rte_errno;
421 : : }
422 : :
423 : : /**
424 : : * Release an Rx verbs queue object.
425 : : *
426 : : * @param rxq
427 : : * Pointer to Rx queue.
428 : : */
429 : : static void
430 : 0 : mlx5_rxq_ibv_obj_release(struct mlx5_rxq_priv *rxq)
431 : : {
432 : 0 : struct mlx5_rxq_obj *rxq_obj = rxq->ctrl->obj;
433 : :
434 [ # # # # ]: 0 : if (rxq_obj == NULL || rxq_obj->wq == NULL)
435 : : return;
436 : 0 : claim_zero(mlx5_glue->destroy_wq(rxq_obj->wq));
437 : 0 : rxq_obj->wq = NULL;
438 : : MLX5_ASSERT(rxq_obj->ibv_cq);
439 : 0 : claim_zero(mlx5_glue->destroy_cq(rxq_obj->ibv_cq));
440 [ # # ]: 0 : if (rxq_obj->ibv_channel)
441 : 0 : claim_zero(mlx5_glue->destroy_comp_channel
442 : : (rxq_obj->ibv_channel));
443 : 0 : rxq->ctrl->started = false;
444 : : }
445 : :
446 : : /**
447 : : * Get event for an Rx verbs queue object.
448 : : *
449 : : * @param rxq_obj
450 : : * Verbs Rx queue object.
451 : : *
452 : : * @return
453 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
454 : : */
455 : : static int
456 : 0 : mlx5_rx_ibv_get_event(struct mlx5_rxq_obj *rxq_obj)
457 : : {
458 : : struct ibv_cq *ev_cq;
459 : : void *ev_ctx;
460 : 0 : int ret = mlx5_glue->get_cq_event(rxq_obj->ibv_channel,
461 : : &ev_cq, &ev_ctx);
462 : :
463 [ # # # # ]: 0 : if (ret < 0 || ev_cq != rxq_obj->ibv_cq)
464 : 0 : goto exit;
465 : 0 : mlx5_glue->ack_cq_events(rxq_obj->ibv_cq, 1);
466 : 0 : return 0;
467 : : exit:
468 [ # # ]: 0 : if (ret < 0)
469 : 0 : rte_errno = errno;
470 : : else
471 : 0 : rte_errno = EINVAL;
472 : 0 : return -rte_errno;
473 : : }
474 : :
475 : : /**
476 : : * Creates a receive work queue as a filed of indirection table.
477 : : *
478 : : * @param dev
479 : : * Pointer to Ethernet device.
480 : : * @param log_n
481 : : * Log of number of queues in the array.
482 : : * @param ind_tbl
483 : : * Verbs indirection table object.
484 : : *
485 : : * @return
486 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
487 : : */
488 : : static int
489 : 0 : mlx5_ibv_ind_table_new(struct rte_eth_dev *dev, const unsigned int log_n,
490 : : struct mlx5_ind_table_obj *ind_tbl)
491 : 0 : {
492 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
493 : 0 : struct ibv_wq *wq[1 << log_n];
494 : : unsigned int i, j;
495 : :
496 : : MLX5_ASSERT(ind_tbl);
497 [ # # ]: 0 : for (i = 0; i != ind_tbl->queues_n; ++i) {
498 : 0 : struct mlx5_rxq_priv *rxq = mlx5_rxq_get(dev,
499 : 0 : ind_tbl->queues[i]);
500 : :
501 : 0 : wq[i] = rxq->ctrl->obj->wq;
502 : : }
503 : : MLX5_ASSERT(i > 0);
504 : : /* Finalise indirection table. */
505 [ # # ]: 0 : for (j = 0; i != (unsigned int)(1 << log_n); ++j, ++i)
506 : 0 : wq[i] = wq[j];
507 : 0 : ind_tbl->ind_table = mlx5_glue->create_rwq_ind_table
508 : 0 : (priv->sh->cdev->ctx,
509 : 0 : &(struct ibv_rwq_ind_table_init_attr){
510 : : .log_ind_tbl_size = log_n,
511 : : .ind_tbl = wq,
512 : : .comp_mask = 0,
513 : : });
514 [ # # ]: 0 : if (!ind_tbl->ind_table) {
515 : 0 : rte_errno = errno;
516 : 0 : return -rte_errno;
517 : : }
518 : : return 0;
519 : : }
520 : :
521 : : /**
522 : : * Destroys the specified Indirection Table.
523 : : *
524 : : * @param ind_table
525 : : * Indirection table to release.
526 : : */
527 : : static void
528 : 0 : mlx5_ibv_ind_table_destroy(struct mlx5_ind_table_obj *ind_tbl)
529 : : {
530 : 0 : claim_zero(mlx5_glue->destroy_rwq_ind_table(ind_tbl->ind_table));
531 : 0 : }
532 : :
533 : : /**
534 : : * Create an Rx Hash queue.
535 : : *
536 : : * @param dev
537 : : * Pointer to Ethernet device.
538 : : * @param hrxq
539 : : * Pointer to Rx Hash queue.
540 : : * @param tunnel
541 : : * Tunnel type.
542 : : *
543 : : * @return
544 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
545 : : */
546 : : static int
547 : 0 : mlx5_ibv_hrxq_new(struct rte_eth_dev *dev, struct mlx5_hrxq *hrxq,
548 : : int tunnel __rte_unused)
549 : : {
550 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
551 : : struct ibv_qp *qp = NULL;
552 : 0 : struct mlx5_ind_table_obj *ind_tbl = hrxq->ind_table;
553 : 0 : const uint8_t *rss_key = hrxq->rss_key;
554 [ # # ]: 0 : uint64_t hash_fields = hrxq->hash_fields;
555 : : int err;
556 : : #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
557 : : struct mlx5dv_qp_init_attr qp_init_attr;
558 : :
559 : : memset(&qp_init_attr, 0, sizeof(qp_init_attr));
560 [ # # ]: 0 : if (tunnel) {
561 : 0 : qp_init_attr.comp_mask =
562 : : MLX5DV_QP_INIT_ATTR_MASK_QP_CREATE_FLAGS;
563 : 0 : qp_init_attr.create_flags = MLX5DV_QP_CREATE_TUNNEL_OFFLOADS;
564 : : }
565 : : #ifdef HAVE_IBV_FLOW_DV_SUPPORT
566 [ # # ]: 0 : if (dev->data->dev_conf.lpbk_mode) {
567 : : /* Allow packet sent from NIC loop back w/o source MAC check. */
568 : 0 : qp_init_attr.comp_mask |=
569 : : MLX5DV_QP_INIT_ATTR_MASK_QP_CREATE_FLAGS;
570 : 0 : qp_init_attr.create_flags |=
571 : : MLX5DV_QP_CREATE_TIR_ALLOW_SELF_LOOPBACK_UC;
572 : : }
573 : : #endif
574 : 0 : qp = mlx5_glue->dv_create_qp
575 : 0 : (priv->sh->cdev->ctx,
576 : 0 : &(struct ibv_qp_init_attr_ex){
577 : : .qp_type = IBV_QPT_RAW_PACKET,
578 : : .comp_mask =
579 : : IBV_QP_INIT_ATTR_PD |
580 : : IBV_QP_INIT_ATTR_IND_TABLE |
581 : : IBV_QP_INIT_ATTR_RX_HASH,
582 : : .rx_hash_conf = (struct ibv_rx_hash_conf){
583 : : .rx_hash_function =
584 : : IBV_RX_HASH_FUNC_TOEPLITZ,
585 : 0 : .rx_hash_key_len = hrxq->rss_key_len,
586 : : .rx_hash_key =
587 : : (void *)(uintptr_t)rss_key,
588 : : .rx_hash_fields_mask = hash_fields,
589 : : },
590 : 0 : .rwq_ind_tbl = ind_tbl->ind_table,
591 : 0 : .pd = priv->sh->cdev->pd,
592 : : },
593 : : &qp_init_attr);
594 : : #else
595 : : qp = mlx5_glue->create_qp_ex
596 : : (priv->sh->cdev->ctx,
597 : : &(struct ibv_qp_init_attr_ex){
598 : : .qp_type = IBV_QPT_RAW_PACKET,
599 : : .comp_mask =
600 : : IBV_QP_INIT_ATTR_PD |
601 : : IBV_QP_INIT_ATTR_IND_TABLE |
602 : : IBV_QP_INIT_ATTR_RX_HASH,
603 : : .rx_hash_conf = (struct ibv_rx_hash_conf){
604 : : .rx_hash_function =
605 : : IBV_RX_HASH_FUNC_TOEPLITZ,
606 : : .rx_hash_key_len = hrxq->rss_key_len,
607 : : .rx_hash_key =
608 : : (void *)(uintptr_t)rss_key,
609 : : .rx_hash_fields_mask = hash_fields,
610 : : },
611 : : .rwq_ind_tbl = ind_tbl->ind_table,
612 : : .pd = priv->sh->cdev->pd,
613 : : });
614 : : #endif
615 [ # # ]: 0 : if (!qp) {
616 : 0 : rte_errno = errno;
617 : 0 : goto error;
618 : : }
619 : 0 : hrxq->qp = qp;
620 : : #ifdef HAVE_IBV_FLOW_DV_SUPPORT
621 : 0 : hrxq->action = mlx5_glue->dv_create_flow_action_dest_ibv_qp(hrxq->qp);
622 [ # # ]: 0 : if (!hrxq->action) {
623 : 0 : rte_errno = errno;
624 : 0 : goto error;
625 : : }
626 : : #endif
627 : : return 0;
628 : 0 : error:
629 : 0 : err = rte_errno; /* Save rte_errno before cleanup. */
630 [ # # ]: 0 : if (qp)
631 : 0 : claim_zero(mlx5_glue->destroy_qp(qp));
632 : 0 : rte_errno = err; /* Restore rte_errno. */
633 : 0 : return -rte_errno;
634 : : }
635 : :
636 : : /**
637 : : * Destroy a Verbs queue pair.
638 : : *
639 : : * @param hrxq
640 : : * Hash Rx queue to release its qp.
641 : : */
642 : : static void
643 : 0 : mlx5_ibv_qp_destroy(struct mlx5_hrxq *hrxq)
644 : : {
645 : 0 : claim_zero(mlx5_glue->destroy_qp(hrxq->qp));
646 : 0 : }
647 : :
648 : : /**
649 : : * Release a drop Rx queue Verbs object.
650 : : *
651 : : * @param dev
652 : : * Pointer to Ethernet device.
653 : : */
654 : : static void
655 : 0 : mlx5_rxq_ibv_obj_drop_release(struct rte_eth_dev *dev)
656 : : {
657 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
658 : 0 : struct mlx5_rxq_priv *rxq = priv->drop_queue.rxq;
659 : : struct mlx5_rxq_obj *rxq_obj;
660 : :
661 [ # # ]: 0 : if (rxq == NULL)
662 : : return;
663 [ # # ]: 0 : if (rxq->ctrl == NULL)
664 : 0 : goto free_priv;
665 : 0 : rxq_obj = rxq->ctrl->obj;
666 [ # # ]: 0 : if (rxq_obj == NULL)
667 : 0 : goto free_ctrl;
668 [ # # ]: 0 : if (rxq_obj->wq)
669 : 0 : claim_zero(mlx5_glue->destroy_wq(rxq_obj->wq));
670 [ # # ]: 0 : if (rxq_obj->ibv_cq)
671 : 0 : claim_zero(mlx5_glue->destroy_cq(rxq_obj->ibv_cq));
672 : 0 : mlx5_free(rxq_obj);
673 : 0 : free_ctrl:
674 : 0 : mlx5_free(rxq->ctrl);
675 : 0 : free_priv:
676 : 0 : mlx5_free(rxq);
677 : 0 : priv->drop_queue.rxq = NULL;
678 : : }
679 : :
680 : : /**
681 : : * Create a drop Rx queue Verbs object.
682 : : *
683 : : * @param dev
684 : : * Pointer to Ethernet device.
685 : : *
686 : : * @return
687 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
688 : : */
689 : : static int
690 : 0 : mlx5_rxq_ibv_obj_drop_create(struct rte_eth_dev *dev)
691 : : {
692 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
693 : 0 : struct ibv_context *ctx = priv->sh->cdev->ctx;
694 : 0 : struct mlx5_rxq_priv *rxq = priv->drop_queue.rxq;
695 : : struct mlx5_rxq_ctrl *rxq_ctrl = NULL;
696 : : struct mlx5_rxq_obj *rxq_obj = NULL;
697 : :
698 [ # # ]: 0 : if (rxq != NULL)
699 : : return 0;
700 : 0 : rxq = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*rxq), 0, SOCKET_ID_ANY);
701 [ # # ]: 0 : if (rxq == NULL) {
702 : 0 : DRV_LOG(DEBUG, "Port %u cannot allocate drop Rx queue memory.",
703 : : dev->data->port_id);
704 : 0 : rte_errno = ENOMEM;
705 : 0 : return -rte_errno;
706 : : }
707 : 0 : priv->drop_queue.rxq = rxq;
708 : 0 : rxq_ctrl = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*rxq_ctrl), 0,
709 : : SOCKET_ID_ANY);
710 [ # # ]: 0 : if (rxq_ctrl == NULL) {
711 : 0 : DRV_LOG(DEBUG, "Port %u cannot allocate drop Rx queue control memory.",
712 : : dev->data->port_id);
713 : 0 : rte_errno = ENOMEM;
714 : 0 : goto error;
715 : : }
716 : 0 : rxq->ctrl = rxq_ctrl;
717 : 0 : rxq_obj = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*rxq_obj), 0,
718 : : SOCKET_ID_ANY);
719 [ # # ]: 0 : if (rxq_obj == NULL) {
720 : 0 : DRV_LOG(DEBUG, "Port %u cannot allocate drop Rx queue memory.",
721 : : dev->data->port_id);
722 : 0 : rte_errno = ENOMEM;
723 : 0 : goto error;
724 : : }
725 : 0 : rxq_ctrl->obj = rxq_obj;
726 : 0 : rxq_obj->ibv_cq = mlx5_glue->create_cq(ctx, 1, NULL, NULL, 0);
727 [ # # ]: 0 : if (!rxq_obj->ibv_cq) {
728 : 0 : DRV_LOG(DEBUG, "Port %u cannot allocate CQ for drop queue.",
729 : : dev->data->port_id);
730 : 0 : rte_errno = errno;
731 : 0 : goto error;
732 : : }
733 : 0 : rxq_obj->wq = mlx5_glue->create_wq(ctx, &(struct ibv_wq_init_attr){
734 : : .wq_type = IBV_WQT_RQ,
735 : : .max_wr = 1,
736 : : .max_sge = 1,
737 : 0 : .pd = priv->sh->cdev->pd,
738 : : .cq = rxq_obj->ibv_cq,
739 : : });
740 [ # # ]: 0 : if (!rxq_obj->wq) {
741 : 0 : DRV_LOG(DEBUG, "Port %u cannot allocate WQ for drop queue.",
742 : : dev->data->port_id);
743 : 0 : rte_errno = errno;
744 : 0 : goto error;
745 : : }
746 : : return 0;
747 : 0 : error:
748 : 0 : mlx5_rxq_ibv_obj_drop_release(dev);
749 : 0 : return -rte_errno;
750 : : }
751 : :
752 : : /**
753 : : * Create a Verbs drop action for Rx Hash queue.
754 : : *
755 : : * @param dev
756 : : * Pointer to Ethernet device.
757 : : *
758 : : * @return
759 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
760 : : */
761 : : static int
762 : 0 : mlx5_ibv_drop_action_create(struct rte_eth_dev *dev)
763 : : {
764 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
765 : 0 : struct mlx5_hrxq *hrxq = priv->drop_queue.hrxq;
766 : : struct ibv_rwq_ind_table *ind_tbl = NULL;
767 : : struct mlx5_rxq_obj *rxq;
768 : : int ret;
769 : :
770 : : MLX5_ASSERT(hrxq && hrxq->ind_table);
771 : 0 : ret = mlx5_rxq_ibv_obj_drop_create(dev);
772 [ # # ]: 0 : if (ret < 0)
773 : 0 : goto error;
774 : 0 : rxq = priv->drop_queue.rxq->ctrl->obj;
775 : 0 : ind_tbl = mlx5_glue->create_rwq_ind_table
776 : 0 : (priv->sh->cdev->ctx,
777 : 0 : &(struct ibv_rwq_ind_table_init_attr){
778 : : .log_ind_tbl_size = 0,
779 : 0 : .ind_tbl = (struct ibv_wq **)&rxq->wq,
780 : : .comp_mask = 0,
781 : : });
782 [ # # ]: 0 : if (!ind_tbl) {
783 : 0 : DRV_LOG(DEBUG, "Port %u"
784 : : " cannot allocate indirection table for drop queue.",
785 : : dev->data->port_id);
786 : 0 : rte_errno = errno;
787 : 0 : goto error;
788 : : }
789 : 0 : hrxq->qp = mlx5_glue->create_qp_ex(priv->sh->cdev->ctx,
790 : 0 : &(struct ibv_qp_init_attr_ex){
791 : : .qp_type = IBV_QPT_RAW_PACKET,
792 : : .comp_mask = IBV_QP_INIT_ATTR_PD |
793 : : IBV_QP_INIT_ATTR_IND_TABLE |
794 : : IBV_QP_INIT_ATTR_RX_HASH,
795 : : .rx_hash_conf = (struct ibv_rx_hash_conf){
796 : : .rx_hash_function = IBV_RX_HASH_FUNC_TOEPLITZ,
797 : : .rx_hash_key_len = MLX5_RSS_HASH_KEY_LEN,
798 : : .rx_hash_key = rss_hash_default_key,
799 : : .rx_hash_fields_mask = 0,
800 : : },
801 : : .rwq_ind_tbl = ind_tbl,
802 : 0 : .pd = priv->sh->cdev->pd
803 : : });
804 [ # # ]: 0 : if (!hrxq->qp) {
805 : 0 : DRV_LOG(DEBUG, "Port %u cannot allocate QP for drop queue.",
806 : : dev->data->port_id);
807 : 0 : rte_errno = errno;
808 : 0 : goto error;
809 : : }
810 : : #ifdef HAVE_IBV_FLOW_DV_SUPPORT
811 : 0 : hrxq->action = mlx5_glue->dv_create_flow_action_dest_ibv_qp(hrxq->qp);
812 [ # # ]: 0 : if (!hrxq->action) {
813 : 0 : rte_errno = errno;
814 : 0 : goto error;
815 : : }
816 : : #endif
817 : 0 : hrxq->ind_table->ind_table = ind_tbl;
818 : 0 : return 0;
819 : 0 : error:
820 [ # # ]: 0 : if (hrxq->qp)
821 : 0 : claim_zero(mlx5_glue->destroy_qp(hrxq->qp));
822 [ # # ]: 0 : if (ind_tbl)
823 : 0 : claim_zero(mlx5_glue->destroy_rwq_ind_table(ind_tbl));
824 [ # # ]: 0 : if (priv->drop_queue.rxq)
825 : 0 : mlx5_rxq_ibv_obj_drop_release(dev);
826 : 0 : return -rte_errno;
827 : : }
828 : :
829 : : /**
830 : : * Release a drop hash Rx queue.
831 : : *
832 : : * @param dev
833 : : * Pointer to Ethernet device.
834 : : */
835 : : static void
836 : 0 : mlx5_ibv_drop_action_destroy(struct rte_eth_dev *dev)
837 : : {
838 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
839 : 0 : struct mlx5_hrxq *hrxq = priv->drop_queue.hrxq;
840 : 0 : struct ibv_rwq_ind_table *ind_tbl = hrxq->ind_table->ind_table;
841 : :
842 : : #ifdef HAVE_IBV_FLOW_DV_SUPPORT
843 : 0 : claim_zero(mlx5_glue->destroy_flow_action(hrxq->action));
844 : : #endif
845 : 0 : claim_zero(mlx5_glue->destroy_qp(hrxq->qp));
846 : 0 : claim_zero(mlx5_glue->destroy_rwq_ind_table(ind_tbl));
847 : 0 : mlx5_rxq_ibv_obj_drop_release(dev);
848 : 0 : }
849 : :
850 : : /**
851 : : * Create a QP Verbs object.
852 : : *
853 : : * @param dev
854 : : * Pointer to Ethernet device.
855 : : * @param idx
856 : : * Queue index in DPDK Tx queue array.
857 : : *
858 : : * @return
859 : : * The QP Verbs object, NULL otherwise and rte_errno is set.
860 : : */
861 : : static struct ibv_qp *
862 : 0 : mlx5_txq_ibv_qp_create(struct rte_eth_dev *dev, uint16_t idx)
863 : : {
864 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
865 : 0 : struct mlx5_txq_data *txq_data = (*priv->txqs)[idx];
866 : : struct mlx5_txq_ctrl *txq_ctrl =
867 : 0 : container_of(txq_data, struct mlx5_txq_ctrl, txq);
868 : : struct ibv_qp *qp_obj = NULL;
869 : 0 : struct ibv_qp_init_attr_ex qp_attr = { 0 };
870 : 0 : const int desc = 1 << txq_data->elts_n;
871 : :
872 : : MLX5_ASSERT(txq_ctrl->obj->cq);
873 : : /* CQ to be associated with the send queue. */
874 : 0 : qp_attr.send_cq = txq_ctrl->obj->cq;
875 : : /* CQ to be associated with the receive queue. */
876 : 0 : qp_attr.recv_cq = txq_ctrl->obj->cq;
877 : : /* Max number of outstanding WRs. */
878 : 0 : qp_attr.cap.max_send_wr = RTE_MIN(priv->sh->dev_cap.max_qp_wr, desc);
879 : : /*
880 : : * Max number of scatter/gather elements in a WR, must be 1 to prevent
881 : : * libmlx5 from trying to affect must be 1 to prevent libmlx5 from
882 : : * trying to affect too much memory. TX gather is not impacted by the
883 : : * dev_cap.max_sge limit and will still work properly.
884 : : */
885 : 0 : qp_attr.cap.max_send_sge = 1;
886 : 0 : qp_attr.qp_type = IBV_QPT_RAW_PACKET,
887 : : /* Do *NOT* enable this, completions events are managed per Tx burst. */
888 : : qp_attr.sq_sig_all = 0;
889 : 0 : qp_attr.pd = priv->sh->cdev->pd;
890 : 0 : qp_attr.comp_mask = IBV_QP_INIT_ATTR_PD;
891 [ # # ]: 0 : if (txq_data->inlen_send)
892 : 0 : qp_attr.cap.max_inline_data = txq_ctrl->max_inline_data;
893 [ # # ]: 0 : if (txq_data->tso_en) {
894 : 0 : qp_attr.max_tso_header = txq_ctrl->max_tso_header;
895 : 0 : qp_attr.comp_mask |= IBV_QP_INIT_ATTR_MAX_TSO_HEADER;
896 : : }
897 : 0 : qp_obj = mlx5_glue->create_qp_ex(priv->sh->cdev->ctx, &qp_attr);
898 [ # # ]: 0 : if (qp_obj == NULL) {
899 : 0 : DRV_LOG(ERR, "Port %u Tx queue %u QP creation failure.",
900 : : dev->data->port_id, idx);
901 : 0 : rte_errno = errno;
902 : : }
903 : 0 : return qp_obj;
904 : : }
905 : :
906 : : /**
907 : : * Initialize Tx UAR registers for primary process.
908 : : *
909 : : * @param txq_ctrl
910 : : * Pointer to Tx queue control structure.
911 : : * @param bf_reg
912 : : * BlueFlame register from Verbs UAR.
913 : : */
914 : : static void
915 : 0 : mlx5_txq_ibv_uar_init(struct mlx5_txq_ctrl *txq_ctrl, void *bf_reg)
916 : : {
917 : 0 : struct mlx5_priv *priv = txq_ctrl->priv;
918 : 0 : struct mlx5_proc_priv *ppriv = MLX5_PROC_PRIV(PORT_ID(priv));
919 : 0 : const size_t page_size = rte_mem_page_size();
920 : : struct mlx5_txq_data *txq = &txq_ctrl->txq;
921 : 0 : off_t uar_mmap_offset = txq_ctrl->uar_mmap_offset;
922 : : #ifndef RTE_ARCH_64
923 : : unsigned int lock_idx;
924 : : #endif
925 : :
926 : : MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
927 : : MLX5_ASSERT(ppriv);
928 [ # # ]: 0 : if (page_size == (size_t)-1) {
929 : 0 : DRV_LOG(ERR, "Failed to get mem page size");
930 : 0 : rte_errno = ENOMEM;
931 : : }
932 [ # # ]: 0 : txq->db_heu = priv->sh->cdev->config.dbnc == MLX5_SQ_DB_HEURISTIC;
933 : 0 : txq->db_nc = mlx5_db_map_type_get(uar_mmap_offset, page_size);
934 : 0 : ppriv->uar_table[txq->idx].db = bf_reg;
935 : : #ifndef RTE_ARCH_64
936 : : /* Assign an UAR lock according to UAR page number. */
937 : : lock_idx = (uar_mmap_offset / page_size) & MLX5_UAR_PAGE_NUM_MASK;
938 : : ppriv->uar_table[txq->idx].sl_p = &priv->sh->uar_lock[lock_idx];
939 : : #endif
940 : 0 : }
941 : :
942 : : /**
943 : : * Create the Tx queue Verbs object.
944 : : *
945 : : * @param dev
946 : : * Pointer to Ethernet device.
947 : : * @param idx
948 : : * Queue index in DPDK Tx queue array.
949 : : *
950 : : * @return
951 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
952 : : */
953 : : int
954 : 0 : mlx5_txq_ibv_obj_new(struct rte_eth_dev *dev, uint16_t idx)
955 : : {
956 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
957 : 0 : struct mlx5_txq_data *txq_data = (*priv->txqs)[idx];
958 : : struct mlx5_txq_ctrl *txq_ctrl =
959 : 0 : container_of(txq_data, struct mlx5_txq_ctrl, txq);
960 : 0 : struct mlx5_txq_obj *txq_obj = txq_ctrl->obj;
961 : : unsigned int cqe_n;
962 : : struct mlx5dv_qp qp;
963 : : struct mlx5dv_cq cq_info;
964 : : struct mlx5dv_obj obj;
965 : 0 : const int desc = 1 << txq_data->elts_n;
966 : : int ret = 0;
967 : :
968 : : MLX5_ASSERT(txq_data);
969 : : MLX5_ASSERT(txq_obj);
970 : 0 : txq_obj->txq_ctrl = txq_ctrl;
971 [ # # ]: 0 : if (mlx5_getenv_int("MLX5_ENABLE_CQE_COMPRESSION")) {
972 : 0 : DRV_LOG(ERR, "Port %u MLX5_ENABLE_CQE_COMPRESSION "
973 : : "must never be set.", dev->data->port_id);
974 : 0 : rte_errno = EINVAL;
975 : 0 : return -rte_errno;
976 : : }
977 : : if (__rte_trace_point_fp_is_enabled() &&
978 : : txq_data->offloads & RTE_ETH_TX_OFFLOAD_SEND_ON_TIMESTAMP)
979 : : cqe_n = UINT16_MAX / 2 - 1;
980 : : else
981 : 0 : cqe_n = desc / MLX5_TX_COMP_THRESH +
982 : : 1 + MLX5_TX_COMP_THRESH_INLINE_DIV;
983 : 0 : txq_obj->cq = mlx5_glue->create_cq(priv->sh->cdev->ctx, cqe_n,
984 : : NULL, NULL, 0);
985 [ # # ]: 0 : if (txq_obj->cq == NULL) {
986 : 0 : DRV_LOG(ERR, "Port %u Tx queue %u CQ creation failure.",
987 : : dev->data->port_id, idx);
988 : 0 : rte_errno = errno;
989 : 0 : goto error;
990 : : }
991 : 0 : txq_obj->qp = mlx5_txq_ibv_qp_create(dev, idx);
992 [ # # ]: 0 : if (txq_obj->qp == NULL) {
993 : 0 : rte_errno = errno;
994 : 0 : goto error;
995 : : }
996 : 0 : ret = mlx5_ibv_modify_qp(txq_obj, MLX5_TXQ_MOD_RST2RDY,
997 : 0 : (uint8_t)priv->dev_port);
998 [ # # ]: 0 : if (ret) {
999 : 0 : DRV_LOG(ERR, "Port %u Tx queue %u QP state modifying failed.",
1000 : : dev->data->port_id, idx);
1001 : 0 : rte_errno = errno;
1002 : 0 : goto error;
1003 : : }
1004 : 0 : qp.comp_mask = MLX5DV_QP_MASK_UAR_MMAP_OFFSET;
1005 : : #ifdef HAVE_IBV_FLOW_DV_SUPPORT
1006 : : /* If using DevX, need additional mask to read tisn value. */
1007 [ # # # # ]: 0 : if (priv->sh->cdev->config.devx && !priv->sh->tdn)
1008 : 0 : qp.comp_mask |= MLX5DV_QP_MASK_RAW_QP_HANDLES;
1009 : : #endif
1010 : 0 : obj.cq.in = txq_obj->cq;
1011 : 0 : obj.cq.out = &cq_info;
1012 : 0 : obj.qp.in = txq_obj->qp;
1013 : 0 : obj.qp.out = &qp;
1014 : 0 : ret = mlx5_glue->dv_init_obj(&obj, MLX5DV_OBJ_CQ | MLX5DV_OBJ_QP);
1015 [ # # ]: 0 : if (ret != 0) {
1016 : 0 : rte_errno = errno;
1017 : 0 : goto error;
1018 : : }
1019 [ # # ]: 0 : if (cq_info.cqe_size != RTE_CACHE_LINE_SIZE) {
1020 : 0 : DRV_LOG(ERR,
1021 : : "Port %u wrong MLX5_CQE_SIZE environment variable"
1022 : : " value: it should be set to %u.",
1023 : : dev->data->port_id, RTE_CACHE_LINE_SIZE);
1024 : 0 : rte_errno = EINVAL;
1025 : 0 : goto error;
1026 : : }
1027 : 0 : txq_data->cqe_n = log2above(cq_info.cqe_cnt);
1028 : 0 : txq_data->cqe_s = 1 << txq_data->cqe_n;
1029 : 0 : txq_data->cqe_m = txq_data->cqe_s - 1;
1030 : 0 : txq_data->qp_num_8s = ((struct ibv_qp *)txq_obj->qp)->qp_num << 8;
1031 : 0 : txq_data->wqes = qp.sq.buf;
1032 : 0 : txq_data->wqe_n = log2above(qp.sq.wqe_cnt);
1033 : 0 : txq_data->wqe_s = 1 << txq_data->wqe_n;
1034 : 0 : txq_data->wqe_m = txq_data->wqe_s - 1;
1035 : 0 : txq_data->wqes_end = txq_data->wqes + txq_data->wqe_s;
1036 : 0 : txq_data->qp_db = &qp.dbrec[MLX5_SND_DBR];
1037 : 0 : txq_data->cq_db = cq_info.dbrec;
1038 : 0 : txq_data->cqes = (volatile struct mlx5_cqe *)cq_info.buf;
1039 : 0 : txq_data->cq_ci = 0;
1040 : 0 : txq_data->cq_pi = 0;
1041 : 0 : txq_data->wqe_ci = 0;
1042 : 0 : txq_data->wqe_pi = 0;
1043 : 0 : txq_data->wqe_comp = 0;
1044 : 0 : txq_data->wqe_thres = txq_data->wqe_s / MLX5_TX_COMP_THRESH_INLINE_DIV;
1045 [ # # ]: 0 : txq_data->wait_on_time = !!(!priv->sh->config.tx_pp &&
1046 [ # # ]: 0 : priv->sh->cdev->config.hca_attr.wait_on_time &&
1047 [ # # ]: 0 : txq_data->offloads &
1048 : : RTE_ETH_TX_OFFLOAD_SEND_ON_TIMESTAMP);
1049 : : #ifdef HAVE_IBV_FLOW_DV_SUPPORT
1050 : : /*
1051 : : * If using DevX need to query and store TIS transport domain value.
1052 : : * This is done once per port.
1053 : : * Will use this value on Rx, when creating matching TIR.
1054 : : */
1055 [ # # # # ]: 0 : if (priv->sh->cdev->config.devx && !priv->sh->tdn) {
1056 : 0 : ret = mlx5_devx_cmd_qp_query_tis_td(txq_obj->qp, qp.tisn,
1057 : : &priv->sh->tdn);
1058 [ # # ]: 0 : if (ret) {
1059 : 0 : DRV_LOG(ERR, "Fail to query port %u Tx queue %u QP TIS "
1060 : : "transport domain.", dev->data->port_id, idx);
1061 : 0 : rte_errno = EINVAL;
1062 : 0 : goto error;
1063 : : } else {
1064 : 0 : DRV_LOG(DEBUG, "Port %u Tx queue %u TIS number %d "
1065 : : "transport domain %d.", dev->data->port_id,
1066 : : idx, qp.tisn, priv->sh->tdn);
1067 : : }
1068 : : }
1069 : : #endif
1070 [ # # ]: 0 : if (qp.comp_mask & MLX5DV_QP_MASK_UAR_MMAP_OFFSET) {
1071 : 0 : txq_ctrl->uar_mmap_offset = qp.uar_mmap_offset;
1072 : 0 : DRV_LOG(DEBUG, "Port %u: uar_mmap_offset 0x%" PRIx64 ".",
1073 : : dev->data->port_id, txq_ctrl->uar_mmap_offset);
1074 : : } else {
1075 : 0 : DRV_LOG(ERR,
1076 : : "Port %u failed to retrieve UAR info, invalid libmlx5.so",
1077 : : dev->data->port_id);
1078 : 0 : rte_errno = EINVAL;
1079 : 0 : goto error;
1080 : : }
1081 : 0 : mlx5_txq_ibv_uar_init(txq_ctrl, qp.bf.reg);
1082 : 0 : dev->data->tx_queue_state[idx] = RTE_ETH_QUEUE_STATE_STARTED;
1083 : 0 : return 0;
1084 : 0 : error:
1085 : 0 : ret = rte_errno; /* Save rte_errno before cleanup. */
1086 [ # # ]: 0 : if (txq_obj->cq)
1087 : 0 : claim_zero(mlx5_glue->destroy_cq(txq_obj->cq));
1088 [ # # ]: 0 : if (txq_obj->qp)
1089 : 0 : claim_zero(mlx5_glue->destroy_qp(txq_obj->qp));
1090 : 0 : rte_errno = ret; /* Restore rte_errno. */
1091 : 0 : return -rte_errno;
1092 : : }
1093 : :
1094 : : /*
1095 : : * Create the dummy QP with minimal resources for loopback.
1096 : : *
1097 : : * @param dev
1098 : : * Pointer to Ethernet device.
1099 : : *
1100 : : * @return
1101 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
1102 : : */
1103 : : int
1104 : 0 : mlx5_rxq_ibv_obj_dummy_lb_create(struct rte_eth_dev *dev)
1105 : : {
1106 : : #if defined(HAVE_IBV_DEVICE_TUNNEL_SUPPORT) && defined(HAVE_IBV_FLOW_DV_SUPPORT)
1107 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
1108 : 0 : struct mlx5_dev_ctx_shared *sh = priv->sh;
1109 : 0 : struct ibv_context *ctx = sh->cdev->ctx;
1110 : 0 : struct mlx5dv_qp_init_attr qp_init_attr = {0};
1111 : : struct {
1112 : : struct ibv_cq_init_attr_ex ibv;
1113 : : struct mlx5dv_cq_init_attr mlx5;
1114 : 0 : } cq_attr = {{0}};
1115 : :
1116 [ # # ]: 0 : if (dev->data->dev_conf.lpbk_mode) {
1117 : : /* Allow packet sent from NIC loop back w/o source MAC check. */
1118 : 0 : qp_init_attr.comp_mask |=
1119 : : MLX5DV_QP_INIT_ATTR_MASK_QP_CREATE_FLAGS;
1120 : 0 : qp_init_attr.create_flags |=
1121 : : MLX5DV_QP_CREATE_TIR_ALLOW_SELF_LOOPBACK_UC;
1122 : : } else {
1123 : : return 0;
1124 : : }
1125 : : /* Only need to check refcnt, 0 after "sh" is allocated. */
1126 [ # # ]: 0 : if (!!(rte_atomic_fetch_add_explicit(&sh->self_lb.refcnt, 1, rte_memory_order_relaxed))) {
1127 : : MLX5_ASSERT(sh->self_lb.ibv_cq && sh->self_lb.qp);
1128 : 0 : priv->lb_used = 1;
1129 : 0 : return 0;
1130 : : }
1131 : 0 : cq_attr.ibv = (struct ibv_cq_init_attr_ex){
1132 : : .cqe = 1,
1133 : : .channel = NULL,
1134 : : .comp_mask = 0,
1135 : : };
1136 : 0 : cq_attr.mlx5 = (struct mlx5dv_cq_init_attr){
1137 : : .comp_mask = 0,
1138 : : };
1139 : : /* Only CQ is needed, no WQ(RQ) is required in this case. */
1140 : 0 : sh->self_lb.ibv_cq = mlx5_glue->cq_ex_to_cq(mlx5_glue->dv_create_cq(ctx,
1141 : : &cq_attr.ibv,
1142 : : &cq_attr.mlx5));
1143 [ # # ]: 0 : if (!sh->self_lb.ibv_cq) {
1144 : 0 : DRV_LOG(ERR, "Port %u cannot allocate CQ for loopback.",
1145 : : dev->data->port_id);
1146 : 0 : rte_errno = errno;
1147 : 0 : goto error;
1148 : : }
1149 : 0 : sh->self_lb.qp = mlx5_glue->dv_create_qp(ctx,
1150 : 0 : &(struct ibv_qp_init_attr_ex){
1151 : : .qp_type = IBV_QPT_RAW_PACKET,
1152 : : .comp_mask = IBV_QP_INIT_ATTR_PD,
1153 : 0 : .pd = sh->cdev->pd,
1154 : : .send_cq = sh->self_lb.ibv_cq,
1155 : : .recv_cq = sh->self_lb.ibv_cq,
1156 : : .cap.max_recv_wr = 1,
1157 : : },
1158 : : &qp_init_attr);
1159 [ # # ]: 0 : if (!sh->self_lb.qp) {
1160 : 0 : DRV_LOG(DEBUG, "Port %u cannot allocate QP for loopback.",
1161 : : dev->data->port_id);
1162 : 0 : rte_errno = errno;
1163 : 0 : goto error;
1164 : : }
1165 : 0 : priv->lb_used = 1;
1166 : 0 : return 0;
1167 : 0 : error:
1168 [ # # ]: 0 : if (sh->self_lb.ibv_cq) {
1169 : 0 : claim_zero(mlx5_glue->destroy_cq(sh->self_lb.ibv_cq));
1170 : 0 : sh->self_lb.ibv_cq = NULL;
1171 : : }
1172 : 0 : rte_atomic_fetch_sub_explicit(&sh->self_lb.refcnt, 1, rte_memory_order_relaxed);
1173 : 0 : return -rte_errno;
1174 : : #else
1175 : : RTE_SET_USED(dev);
1176 : : return 0;
1177 : : #endif
1178 : : }
1179 : :
1180 : : /*
1181 : : * Release the dummy queue resources for loopback.
1182 : : *
1183 : : * @param dev
1184 : : * Pointer to Ethernet device.
1185 : : */
1186 : : void
1187 : 0 : mlx5_rxq_ibv_obj_dummy_lb_release(struct rte_eth_dev *dev)
1188 : : {
1189 : : #if defined(HAVE_IBV_DEVICE_TUNNEL_SUPPORT) && defined(HAVE_IBV_FLOW_DV_SUPPORT)
1190 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
1191 : 0 : struct mlx5_dev_ctx_shared *sh = priv->sh;
1192 : :
1193 [ # # ]: 0 : if (!priv->lb_used)
1194 : : return;
1195 : : MLX5_ASSERT(rte_atomic_load_explicit(&sh->self_lb.refcnt, rte_memory_order_relaxed));
1196 [ # # ]: 0 : if (!(rte_atomic_fetch_sub_explicit(&sh->self_lb.refcnt, 1,
1197 : : rte_memory_order_relaxed) - 1)) {
1198 [ # # ]: 0 : if (sh->self_lb.qp) {
1199 : 0 : claim_zero(mlx5_glue->destroy_qp(sh->self_lb.qp));
1200 : 0 : sh->self_lb.qp = NULL;
1201 : : }
1202 [ # # ]: 0 : if (sh->self_lb.ibv_cq) {
1203 : 0 : claim_zero(mlx5_glue->destroy_cq(sh->self_lb.ibv_cq));
1204 : 0 : sh->self_lb.ibv_cq = NULL;
1205 : : }
1206 : : }
1207 : 0 : priv->lb_used = 0;
1208 : : #else
1209 : : RTE_SET_USED(dev);
1210 : : return;
1211 : : #endif
1212 : : }
1213 : :
1214 : : /**
1215 : : * Release an Tx verbs queue object.
1216 : : *
1217 : : * @param txq_obj
1218 : : * Verbs Tx queue object..
1219 : : */
1220 : : void
1221 : 0 : mlx5_txq_ibv_obj_release(struct mlx5_txq_obj *txq_obj)
1222 : : {
1223 : : MLX5_ASSERT(txq_obj);
1224 : 0 : claim_zero(mlx5_glue->destroy_qp(txq_obj->qp));
1225 : 0 : claim_zero(mlx5_glue->destroy_cq(txq_obj->cq));
1226 : 0 : }
1227 : :
1228 : : struct mlx5_obj_ops ibv_obj_ops = {
1229 : : .rxq_obj_modify_vlan_strip = mlx5_rxq_obj_modify_wq_vlan_strip,
1230 : : .rxq_obj_new = mlx5_rxq_ibv_obj_new,
1231 : : .rxq_event_get = mlx5_rx_ibv_get_event,
1232 : : .rxq_obj_modify = mlx5_ibv_modify_wq,
1233 : : .rxq_obj_release = mlx5_rxq_ibv_obj_release,
1234 : : .ind_table_new = mlx5_ibv_ind_table_new,
1235 : : .ind_table_destroy = mlx5_ibv_ind_table_destroy,
1236 : : .hrxq_new = mlx5_ibv_hrxq_new,
1237 : : .hrxq_destroy = mlx5_ibv_qp_destroy,
1238 : : .drop_action_create = mlx5_ibv_drop_action_create,
1239 : : .drop_action_destroy = mlx5_ibv_drop_action_destroy,
1240 : : .txq_obj_new = mlx5_txq_ibv_obj_new,
1241 : : .txq_obj_modify = mlx5_ibv_modify_qp,
1242 : : .txq_obj_release = mlx5_txq_ibv_obj_release,
1243 : : .lb_dummy_queue_create = NULL,
1244 : : .lb_dummy_queue_release = NULL,
1245 : : };
|