xref: /dpdk/drivers/net/mlx5/mlx5_devx.c (revision c053d9e9629b2337093dfd344eb904f0aff703bd)
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 <stdbool.h>
8 #include <string.h>
9 #include <stdint.h>
10 #include <sys/queue.h>
11 
12 #include <rte_malloc.h>
13 #include <rte_common.h>
14 #include <rte_eal_paging.h>
15 
16 #include <mlx5_glue.h>
17 #include <mlx5_devx_cmds.h>
18 #include <mlx5_malloc.h>
19 
20 #include "mlx5.h"
21 #include "mlx5_common_os.h"
22 #include "mlx5_rxtx.h"
23 #include "mlx5_utils.h"
24 #include "mlx5_devx.h"
25 #include "mlx5_flow.h"
26 
27 
28 /**
29  * Modify RQ vlan stripping offload
30  *
31  * @param rxq_obj
32  *   Rx queue object.
33  *
34  * @return
35  *   0 on success, non-0 otherwise
36  */
37 static int
38 mlx5_rxq_obj_modify_rq_vlan_strip(struct mlx5_rxq_obj *rxq_obj, int on)
39 {
40 	struct mlx5_devx_modify_rq_attr rq_attr;
41 
42 	memset(&rq_attr, 0, sizeof(rq_attr));
43 	rq_attr.rq_state = MLX5_RQC_STATE_RDY;
44 	rq_attr.state = MLX5_RQC_STATE_RDY;
45 	rq_attr.vsd = (on ? 0 : 1);
46 	rq_attr.modify_bitmask = MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD;
47 	return mlx5_devx_cmd_modify_rq(rxq_obj->rq, &rq_attr);
48 }
49 
50 /**
51  * Modify RQ using DevX API.
52  *
53  * @param rxq_obj
54  *   DevX Rx queue object.
55  * @param type
56  *   Type of change queue state.
57  *
58  * @return
59  *   0 on success, a negative errno value otherwise and rte_errno is set.
60  */
61 static int
62 mlx5_devx_modify_rq(struct mlx5_rxq_obj *rxq_obj, uint8_t type)
63 {
64 	struct mlx5_devx_modify_rq_attr rq_attr;
65 
66 	memset(&rq_attr, 0, sizeof(rq_attr));
67 	switch (type) {
68 	case MLX5_RXQ_MOD_ERR2RST:
69 		rq_attr.rq_state = MLX5_RQC_STATE_ERR;
70 		rq_attr.state = MLX5_RQC_STATE_RST;
71 		break;
72 	case MLX5_RXQ_MOD_RST2RDY:
73 		rq_attr.rq_state = MLX5_RQC_STATE_RST;
74 		rq_attr.state = MLX5_RQC_STATE_RDY;
75 		break;
76 	case MLX5_RXQ_MOD_RDY2ERR:
77 		rq_attr.rq_state = MLX5_RQC_STATE_RDY;
78 		rq_attr.state = MLX5_RQC_STATE_ERR;
79 		break;
80 	case MLX5_RXQ_MOD_RDY2RST:
81 		rq_attr.rq_state = MLX5_RQC_STATE_RDY;
82 		rq_attr.state = MLX5_RQC_STATE_RST;
83 		break;
84 	default:
85 		break;
86 	}
87 	return mlx5_devx_cmd_modify_rq(rxq_obj->rq, &rq_attr);
88 }
89 
90 /**
91  * Modify SQ using DevX API.
92  *
93  * @param txq_obj
94  *   DevX Tx queue object.
95  * @param type
96  *   Type of change queue state.
97  * @param dev_port
98  *   Unnecessary.
99  *
100  * @return
101  *   0 on success, a negative errno value otherwise and rte_errno is set.
102  */
103 static int
104 mlx5_devx_modify_sq(struct mlx5_txq_obj *obj, enum mlx5_txq_modify_type type,
105 		    uint8_t dev_port)
106 {
107 	struct mlx5_devx_modify_sq_attr msq_attr = { 0 };
108 	int ret;
109 
110 	if (type != MLX5_TXQ_MOD_RST2RDY) {
111 		/* Change queue state to reset. */
112 		if (type == MLX5_TXQ_MOD_ERR2RDY)
113 			msq_attr.sq_state = MLX5_SQC_STATE_ERR;
114 		else
115 			msq_attr.sq_state = MLX5_SQC_STATE_RDY;
116 		msq_attr.state = MLX5_SQC_STATE_RST;
117 		ret = mlx5_devx_cmd_modify_sq(obj->sq_devx, &msq_attr);
118 		if (ret) {
119 			DRV_LOG(ERR, "Cannot change the Tx SQ state to RESET"
120 				" %s", strerror(errno));
121 			rte_errno = errno;
122 			return ret;
123 		}
124 	}
125 	if (type != MLX5_TXQ_MOD_RDY2RST) {
126 		/* Change queue state to ready. */
127 		msq_attr.sq_state = MLX5_SQC_STATE_RST;
128 		msq_attr.state = MLX5_SQC_STATE_RDY;
129 		ret = mlx5_devx_cmd_modify_sq(obj->sq_devx, &msq_attr);
130 		if (ret) {
131 			DRV_LOG(ERR, "Cannot change the Tx SQ state to READY"
132 				" %s", strerror(errno));
133 			rte_errno = errno;
134 			return ret;
135 		}
136 	}
137 	/*
138 	 * The dev_port variable is relevant only in Verbs API, and there is a
139 	 * pointer that points to this function and a parallel function in verbs
140 	 * intermittently, so they should have the same parameters.
141 	 */
142 	(void)dev_port;
143 	return 0;
144 }
145 
146 /**
147  * Release the resources allocated for an RQ DevX object.
148  *
149  * @param rxq_ctrl
150  *   DevX Rx queue object.
151  */
152 static void
153 mlx5_rxq_release_devx_rq_resources(struct mlx5_rxq_ctrl *rxq_ctrl)
154 {
155 	struct mlx5_devx_dbr_page *dbr_page = rxq_ctrl->rq_dbrec_page;
156 
157 	if (rxq_ctrl->wq_umem) {
158 		mlx5_glue->devx_umem_dereg(rxq_ctrl->wq_umem);
159 		rxq_ctrl->wq_umem = NULL;
160 	}
161 	if (rxq_ctrl->rxq.wqes) {
162 		mlx5_free((void *)(uintptr_t)rxq_ctrl->rxq.wqes);
163 		rxq_ctrl->rxq.wqes = NULL;
164 	}
165 	if (dbr_page) {
166 		claim_zero(mlx5_release_dbr(&rxq_ctrl->priv->dbrpgs,
167 					    mlx5_os_get_umem_id(dbr_page->umem),
168 					    rxq_ctrl->rq_dbr_offset));
169 		rxq_ctrl->rq_dbrec_page = NULL;
170 	}
171 }
172 
173 /**
174  * Release the resources allocated for the Rx CQ DevX object.
175  *
176  * @param rxq_ctrl
177  *   DevX Rx queue object.
178  */
179 static void
180 mlx5_rxq_release_devx_cq_resources(struct mlx5_rxq_ctrl *rxq_ctrl)
181 {
182 	struct mlx5_devx_dbr_page *dbr_page = rxq_ctrl->cq_dbrec_page;
183 
184 	if (rxq_ctrl->cq_umem) {
185 		mlx5_glue->devx_umem_dereg(rxq_ctrl->cq_umem);
186 		rxq_ctrl->cq_umem = NULL;
187 	}
188 	if (rxq_ctrl->rxq.cqes) {
189 		rte_free((void *)(uintptr_t)rxq_ctrl->rxq.cqes);
190 		rxq_ctrl->rxq.cqes = NULL;
191 	}
192 	if (dbr_page) {
193 		claim_zero(mlx5_release_dbr(&rxq_ctrl->priv->dbrpgs,
194 					    mlx5_os_get_umem_id(dbr_page->umem),
195 					    rxq_ctrl->cq_dbr_offset));
196 		rxq_ctrl->cq_dbrec_page = NULL;
197 	}
198 }
199 
200 /**
201  * Release an Rx DevX queue object.
202  *
203  * @param rxq_obj
204  *   DevX Rx queue object.
205  */
206 static void
207 mlx5_rxq_devx_obj_release(struct mlx5_rxq_obj *rxq_obj)
208 {
209 	MLX5_ASSERT(rxq_obj);
210 	MLX5_ASSERT(rxq_obj->rq);
211 	if (rxq_obj->rxq_ctrl->type == MLX5_RXQ_TYPE_HAIRPIN) {
212 		mlx5_devx_modify_rq(rxq_obj, MLX5_RXQ_MOD_RDY2RST);
213 		claim_zero(mlx5_devx_cmd_destroy(rxq_obj->rq));
214 	} else {
215 		MLX5_ASSERT(rxq_obj->devx_cq);
216 		claim_zero(mlx5_devx_cmd_destroy(rxq_obj->rq));
217 		claim_zero(mlx5_devx_cmd_destroy(rxq_obj->devx_cq));
218 		if (rxq_obj->devx_channel)
219 			mlx5_glue->devx_destroy_event_channel
220 							(rxq_obj->devx_channel);
221 		mlx5_rxq_release_devx_rq_resources(rxq_obj->rxq_ctrl);
222 		mlx5_rxq_release_devx_cq_resources(rxq_obj->rxq_ctrl);
223 	}
224 }
225 
226 /**
227  * Get event for an Rx DevX queue object.
228  *
229  * @param rxq_obj
230  *   DevX Rx queue object.
231  *
232  * @return
233  *   0 on success, a negative errno value otherwise and rte_errno is set.
234  */
235 static int
236 mlx5_rx_devx_get_event(struct mlx5_rxq_obj *rxq_obj)
237 {
238 #ifdef HAVE_IBV_DEVX_EVENT
239 	union {
240 		struct mlx5dv_devx_async_event_hdr event_resp;
241 		uint8_t buf[sizeof(struct mlx5dv_devx_async_event_hdr) + 128];
242 	} out;
243 	int ret = mlx5_glue->devx_get_event(rxq_obj->devx_channel,
244 					    &out.event_resp,
245 					    sizeof(out.buf));
246 
247 	if (ret < 0) {
248 		rte_errno = errno;
249 		return -rte_errno;
250 	}
251 	if (out.event_resp.cookie != (uint64_t)(uintptr_t)rxq_obj->devx_cq) {
252 		rte_errno = EINVAL;
253 		return -rte_errno;
254 	}
255 	return 0;
256 #else
257 	(void)rxq_obj;
258 	rte_errno = ENOTSUP;
259 	return -rte_errno;
260 #endif /* HAVE_IBV_DEVX_EVENT */
261 }
262 
263 /**
264  * Fill common fields of create RQ attributes structure.
265  *
266  * @param rxq_data
267  *   Pointer to Rx queue data.
268  * @param cqn
269  *   CQ number to use with this RQ.
270  * @param rq_attr
271  *   RQ attributes structure to fill..
272  */
273 static void
274 mlx5_devx_create_rq_attr_fill(struct mlx5_rxq_data *rxq_data, uint32_t cqn,
275 			      struct mlx5_devx_create_rq_attr *rq_attr)
276 {
277 	rq_attr->state = MLX5_RQC_STATE_RST;
278 	rq_attr->vsd = (rxq_data->vlan_strip) ? 0 : 1;
279 	rq_attr->cqn = cqn;
280 	rq_attr->scatter_fcs = (rxq_data->crc_present) ? 1 : 0;
281 }
282 
283 /**
284  * Fill common fields of DevX WQ attributes structure.
285  *
286  * @param priv
287  *   Pointer to device private data.
288  * @param rxq_ctrl
289  *   Pointer to Rx queue control structure.
290  * @param wq_attr
291  *   WQ attributes structure to fill..
292  */
293 static void
294 mlx5_devx_wq_attr_fill(struct mlx5_priv *priv, struct mlx5_rxq_ctrl *rxq_ctrl,
295 		       struct mlx5_devx_wq_attr *wq_attr)
296 {
297 	wq_attr->end_padding_mode = priv->config.hw_padding ?
298 					MLX5_WQ_END_PAD_MODE_ALIGN :
299 					MLX5_WQ_END_PAD_MODE_NONE;
300 	wq_attr->pd = priv->sh->pdn;
301 	wq_attr->dbr_addr = rxq_ctrl->rq_dbr_offset;
302 	wq_attr->dbr_umem_id =
303 			mlx5_os_get_umem_id(rxq_ctrl->rq_dbrec_page->umem);
304 	wq_attr->dbr_umem_valid = 1;
305 	wq_attr->wq_umem_id = mlx5_os_get_umem_id(rxq_ctrl->wq_umem);
306 	wq_attr->wq_umem_valid = 1;
307 }
308 
309 /**
310  * Create a RQ object using DevX.
311  *
312  * @param dev
313  *   Pointer to Ethernet device.
314  * @param idx
315  *   Queue index in DPDK Rx queue array.
316  *
317  * @return
318  *   The DevX RQ object initialized, NULL otherwise and rte_errno is set.
319  */
320 static struct mlx5_devx_obj *
321 mlx5_rxq_create_devx_rq_resources(struct rte_eth_dev *dev, uint16_t idx)
322 {
323 	struct mlx5_priv *priv = dev->data->dev_private;
324 	struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[idx];
325 	struct mlx5_rxq_ctrl *rxq_ctrl =
326 		container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
327 	struct mlx5_devx_create_rq_attr rq_attr = { 0 };
328 	uint32_t wqe_n = 1 << (rxq_data->elts_n - rxq_data->sges_n);
329 	uint32_t cqn = rxq_ctrl->obj->devx_cq->id;
330 	struct mlx5_devx_dbr_page *dbr_page;
331 	int64_t dbr_offset;
332 	uint32_t wq_size = 0;
333 	uint32_t wqe_size = 0;
334 	uint32_t log_wqe_size = 0;
335 	void *buf = NULL;
336 	struct mlx5_devx_obj *rq;
337 
338 	/* Fill RQ attributes. */
339 	rq_attr.mem_rq_type = MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE;
340 	rq_attr.flush_in_error_en = 1;
341 	mlx5_devx_create_rq_attr_fill(rxq_data, cqn, &rq_attr);
342 	/* Fill WQ attributes for this RQ. */
343 	if (mlx5_rxq_mprq_enabled(rxq_data)) {
344 		rq_attr.wq_attr.wq_type = MLX5_WQ_TYPE_CYCLIC_STRIDING_RQ;
345 		/*
346 		 * Number of strides in each WQE:
347 		 * 512*2^single_wqe_log_num_of_strides.
348 		 */
349 		rq_attr.wq_attr.single_wqe_log_num_of_strides =
350 				rxq_data->strd_num_n -
351 				MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
352 		/* Stride size = (2^single_stride_log_num_of_bytes)*64B. */
353 		rq_attr.wq_attr.single_stride_log_num_of_bytes =
354 				rxq_data->strd_sz_n -
355 				MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
356 		wqe_size = sizeof(struct mlx5_wqe_mprq);
357 	} else {
358 		rq_attr.wq_attr.wq_type = MLX5_WQ_TYPE_CYCLIC;
359 		wqe_size = sizeof(struct mlx5_wqe_data_seg);
360 	}
361 	log_wqe_size = log2above(wqe_size) + rxq_data->sges_n;
362 	rq_attr.wq_attr.log_wq_stride = log_wqe_size;
363 	rq_attr.wq_attr.log_wq_sz = rxq_data->elts_n - rxq_data->sges_n;
364 	/* Calculate and allocate WQ memory space. */
365 	wqe_size = 1 << log_wqe_size; /* round up power of two.*/
366 	wq_size = wqe_n * wqe_size;
367 	size_t alignment = MLX5_WQE_BUF_ALIGNMENT;
368 	if (alignment == (size_t)-1) {
369 		DRV_LOG(ERR, "Failed to get mem page size");
370 		rte_errno = ENOMEM;
371 		return NULL;
372 	}
373 	buf = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO, wq_size,
374 			  alignment, rxq_ctrl->socket);
375 	if (!buf)
376 		return NULL;
377 	rxq_data->wqes = buf;
378 	rxq_ctrl->wq_umem = mlx5_glue->devx_umem_reg(priv->sh->ctx,
379 						     buf, wq_size, 0);
380 	if (!rxq_ctrl->wq_umem)
381 		goto error;
382 	/* Allocate RQ door-bell. */
383 	dbr_offset = mlx5_get_dbr(priv->sh->ctx, &priv->dbrpgs, &dbr_page);
384 	if (dbr_offset < 0) {
385 		DRV_LOG(ERR, "Failed to allocate RQ door-bell.");
386 		goto error;
387 	}
388 	rxq_ctrl->rq_dbr_offset = dbr_offset;
389 	rxq_ctrl->rq_dbrec_page = dbr_page;
390 	rxq_data->rq_db = (uint32_t *)((uintptr_t)dbr_page->dbrs +
391 			  (uintptr_t)rxq_ctrl->rq_dbr_offset);
392 	/* Create RQ using DevX API. */
393 	mlx5_devx_wq_attr_fill(priv, rxq_ctrl, &rq_attr.wq_attr);
394 	rq = mlx5_devx_cmd_create_rq(priv->sh->ctx, &rq_attr, rxq_ctrl->socket);
395 	if (!rq)
396 		goto error;
397 	return rq;
398 error:
399 	mlx5_rxq_release_devx_rq_resources(rxq_ctrl);
400 	return NULL;
401 }
402 
403 /**
404  * Create a DevX CQ object for an Rx queue.
405  *
406  * @param dev
407  *   Pointer to Ethernet device.
408  * @param idx
409  *   Queue index in DPDK Rx queue array.
410  *
411  * @return
412  *   The DevX CQ object initialized, NULL otherwise and rte_errno is set.
413  */
414 static struct mlx5_devx_obj *
415 mlx5_rxq_create_devx_cq_resources(struct rte_eth_dev *dev, uint16_t idx)
416 {
417 	struct mlx5_devx_obj *cq_obj = 0;
418 	struct mlx5_devx_cq_attr cq_attr = { 0 };
419 	struct mlx5_priv *priv = dev->data->dev_private;
420 	struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[idx];
421 	struct mlx5_rxq_ctrl *rxq_ctrl =
422 		container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
423 	size_t page_size = rte_mem_page_size();
424 	unsigned int cqe_n = mlx5_rxq_cqe_num(rxq_data);
425 	struct mlx5_devx_dbr_page *dbr_page;
426 	int64_t dbr_offset;
427 	void *buf = NULL;
428 	uint16_t event_nums[1] = {0};
429 	uint32_t log_cqe_n;
430 	uint32_t cq_size;
431 	int ret = 0;
432 
433 	if (page_size == (size_t)-1) {
434 		DRV_LOG(ERR, "Failed to get page_size.");
435 		goto error;
436 	}
437 	if (priv->config.cqe_comp && !rxq_data->hw_timestamp &&
438 	    !rxq_data->lro) {
439 		cq_attr.cqe_comp_en = 1u;
440 		rxq_data->mcqe_format = priv->config.cqe_comp_fmt;
441 		rxq_data->byte_mask = UINT32_MAX;
442 		switch (priv->config.cqe_comp_fmt) {
443 		case MLX5_CQE_RESP_FORMAT_HASH:
444 			/* fallthrough */
445 		case MLX5_CQE_RESP_FORMAT_CSUM:
446 			/*
447 			 * Select CSUM miniCQE format only for non-vectorized
448 			 * MPRQ Rx burst, use HASH miniCQE format for others.
449 			 */
450 			if (mlx5_rxq_check_vec_support(rxq_data) < 0 &&
451 			    mlx5_rxq_mprq_enabled(rxq_data))
452 				cq_attr.mini_cqe_res_format =
453 					MLX5_CQE_RESP_FORMAT_CSUM_STRIDX;
454 			else
455 				cq_attr.mini_cqe_res_format =
456 					MLX5_CQE_RESP_FORMAT_HASH;
457 			rxq_data->mcqe_format = cq_attr.mini_cqe_res_format;
458 			break;
459 		case MLX5_CQE_RESP_FORMAT_FTAG_STRIDX:
460 			rxq_data->byte_mask = MLX5_LEN_WITH_MARK_MASK;
461 			/* fallthrough */
462 		case MLX5_CQE_RESP_FORMAT_CSUM_STRIDX:
463 			cq_attr.mini_cqe_res_format = priv->config.cqe_comp_fmt;
464 			break;
465 		case MLX5_CQE_RESP_FORMAT_L34H_STRIDX:
466 			cq_attr.mini_cqe_res_format = 0;
467 			cq_attr.mini_cqe_res_format_ext = 1;
468 			break;
469 		}
470 		DRV_LOG(DEBUG,
471 			"Port %u Rx CQE compression is enabled, format %d.",
472 			dev->data->port_id, priv->config.cqe_comp_fmt);
473 		/*
474 		 * For vectorized Rx, it must not be doubled in order to
475 		 * make cq_ci and rq_ci aligned.
476 		 */
477 		if (mlx5_rxq_check_vec_support(rxq_data) < 0)
478 			cqe_n *= 2;
479 	} else if (priv->config.cqe_comp && rxq_data->hw_timestamp) {
480 		DRV_LOG(DEBUG,
481 			"Port %u Rx CQE compression is disabled for HW"
482 			" timestamp.",
483 			dev->data->port_id);
484 	} else if (priv->config.cqe_comp && rxq_data->lro) {
485 		DRV_LOG(DEBUG,
486 			"Port %u Rx CQE compression is disabled for LRO.",
487 			dev->data->port_id);
488 	}
489 	if (priv->config.cqe_pad)
490 		cq_attr.cqe_size = MLX5_CQE_SIZE_128B;
491 	log_cqe_n = log2above(cqe_n);
492 	cq_size = sizeof(struct mlx5_cqe) * (1 << log_cqe_n);
493 	buf = rte_calloc_socket(__func__, 1, cq_size, page_size,
494 				rxq_ctrl->socket);
495 	if (!buf) {
496 		DRV_LOG(ERR, "Failed to allocate memory for CQ.");
497 		goto error;
498 	}
499 	rxq_data->cqes = (volatile struct mlx5_cqe (*)[])(uintptr_t)buf;
500 	rxq_ctrl->cq_umem = mlx5_glue->devx_umem_reg(priv->sh->ctx, buf,
501 						     cq_size,
502 						     IBV_ACCESS_LOCAL_WRITE);
503 	if (!rxq_ctrl->cq_umem) {
504 		DRV_LOG(ERR, "Failed to register umem for CQ.");
505 		goto error;
506 	}
507 	/* Allocate CQ door-bell. */
508 	dbr_offset = mlx5_get_dbr(priv->sh->ctx, &priv->dbrpgs, &dbr_page);
509 	if (dbr_offset < 0) {
510 		DRV_LOG(ERR, "Failed to allocate CQ door-bell.");
511 		goto error;
512 	}
513 	rxq_ctrl->cq_dbr_offset = dbr_offset;
514 	rxq_ctrl->cq_dbrec_page = dbr_page;
515 	rxq_data->cq_db = (uint32_t *)((uintptr_t)dbr_page->dbrs +
516 			  (uintptr_t)rxq_ctrl->cq_dbr_offset);
517 	rxq_data->cq_uar =
518 			mlx5_os_get_devx_uar_base_addr(priv->sh->devx_rx_uar);
519 	/* Create CQ using DevX API. */
520 	cq_attr.eqn = priv->sh->eqn;
521 	cq_attr.uar_page_id =
522 			mlx5_os_get_devx_uar_page_id(priv->sh->devx_rx_uar);
523 	cq_attr.q_umem_id = mlx5_os_get_umem_id(rxq_ctrl->cq_umem);
524 	cq_attr.q_umem_valid = 1;
525 	cq_attr.log_cq_size = log_cqe_n;
526 	cq_attr.log_page_size = rte_log2_u32(page_size);
527 	cq_attr.db_umem_offset = rxq_ctrl->cq_dbr_offset;
528 	cq_attr.db_umem_id = mlx5_os_get_umem_id(dbr_page->umem);
529 	cq_attr.db_umem_valid = 1;
530 	cq_obj = mlx5_devx_cmd_create_cq(priv->sh->ctx, &cq_attr);
531 	if (!cq_obj)
532 		goto error;
533 	rxq_data->cqe_n = log_cqe_n;
534 	rxq_data->cqn = cq_obj->id;
535 	if (rxq_ctrl->obj->devx_channel) {
536 		ret = mlx5_glue->devx_subscribe_devx_event
537 						(rxq_ctrl->obj->devx_channel,
538 						 cq_obj->obj,
539 						 sizeof(event_nums),
540 						 event_nums,
541 						 (uint64_t)(uintptr_t)cq_obj);
542 		if (ret) {
543 			DRV_LOG(ERR, "Fail to subscribe CQ to event channel.");
544 			rte_errno = errno;
545 			goto error;
546 		}
547 	}
548 	/* Initialise CQ to 1's to mark HW ownership for all CQEs. */
549 	memset((void *)(uintptr_t)rxq_data->cqes, 0xFF, cq_size);
550 	return cq_obj;
551 error:
552 	if (cq_obj)
553 		mlx5_devx_cmd_destroy(cq_obj);
554 	mlx5_rxq_release_devx_cq_resources(rxq_ctrl);
555 	return NULL;
556 }
557 
558 /**
559  * Create the Rx hairpin queue object.
560  *
561  * @param dev
562  *   Pointer to Ethernet device.
563  * @param idx
564  *   Queue index in DPDK Rx queue array.
565  *
566  * @return
567  *   0 on success, a negative errno value otherwise and rte_errno is set.
568  */
569 static int
570 mlx5_rxq_obj_hairpin_new(struct rte_eth_dev *dev, uint16_t idx)
571 {
572 	struct mlx5_priv *priv = dev->data->dev_private;
573 	struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[idx];
574 	struct mlx5_rxq_ctrl *rxq_ctrl =
575 		container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
576 	struct mlx5_devx_create_rq_attr attr = { 0 };
577 	struct mlx5_rxq_obj *tmpl = rxq_ctrl->obj;
578 	uint32_t max_wq_data;
579 
580 	MLX5_ASSERT(rxq_data);
581 	MLX5_ASSERT(tmpl);
582 	tmpl->rxq_ctrl = rxq_ctrl;
583 	attr.hairpin = 1;
584 	max_wq_data = priv->config.hca_attr.log_max_hairpin_wq_data_sz;
585 	/* Jumbo frames > 9KB should be supported, and more packets. */
586 	if (priv->config.log_hp_size != (uint32_t)MLX5_ARG_UNSET) {
587 		if (priv->config.log_hp_size > max_wq_data) {
588 			DRV_LOG(ERR, "Total data size %u power of 2 is "
589 				"too large for hairpin.",
590 				priv->config.log_hp_size);
591 			rte_errno = ERANGE;
592 			return -rte_errno;
593 		}
594 		attr.wq_attr.log_hairpin_data_sz = priv->config.log_hp_size;
595 	} else {
596 		attr.wq_attr.log_hairpin_data_sz =
597 				(max_wq_data < MLX5_HAIRPIN_JUMBO_LOG_SIZE) ?
598 				 max_wq_data : MLX5_HAIRPIN_JUMBO_LOG_SIZE;
599 	}
600 	/* Set the packets number to the maximum value for performance. */
601 	attr.wq_attr.log_hairpin_num_packets =
602 			attr.wq_attr.log_hairpin_data_sz -
603 			MLX5_HAIRPIN_QUEUE_STRIDE;
604 	tmpl->rq = mlx5_devx_cmd_create_rq(priv->sh->ctx, &attr,
605 					   rxq_ctrl->socket);
606 	if (!tmpl->rq) {
607 		DRV_LOG(ERR,
608 			"Port %u Rx hairpin queue %u can't create rq object.",
609 			dev->data->port_id, idx);
610 		rte_errno = errno;
611 		return -rte_errno;
612 	}
613 	dev->data->rx_queue_state[idx] = RTE_ETH_QUEUE_STATE_HAIRPIN;
614 	return 0;
615 }
616 
617 /**
618  * Create the Rx queue DevX object.
619  *
620  * @param dev
621  *   Pointer to Ethernet device.
622  * @param idx
623  *   Queue index in DPDK Rx queue array.
624  *
625  * @return
626  *   0 on success, a negative errno value otherwise and rte_errno is set.
627  */
628 static int
629 mlx5_rxq_devx_obj_new(struct rte_eth_dev *dev, uint16_t idx)
630 {
631 	struct mlx5_priv *priv = dev->data->dev_private;
632 	struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[idx];
633 	struct mlx5_rxq_ctrl *rxq_ctrl =
634 		container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
635 	struct mlx5_rxq_obj *tmpl = rxq_ctrl->obj;
636 	int ret = 0;
637 
638 	MLX5_ASSERT(rxq_data);
639 	MLX5_ASSERT(tmpl);
640 	if (rxq_ctrl->type == MLX5_RXQ_TYPE_HAIRPIN)
641 		return mlx5_rxq_obj_hairpin_new(dev, idx);
642 	tmpl->rxq_ctrl = rxq_ctrl;
643 	if (rxq_ctrl->irq) {
644 		int devx_ev_flag =
645 			  MLX5DV_DEVX_CREATE_EVENT_CHANNEL_FLAGS_OMIT_EV_DATA;
646 
647 		tmpl->devx_channel = mlx5_glue->devx_create_event_channel
648 								(priv->sh->ctx,
649 								 devx_ev_flag);
650 		if (!tmpl->devx_channel) {
651 			rte_errno = errno;
652 			DRV_LOG(ERR, "Failed to create event channel %d.",
653 				rte_errno);
654 			goto error;
655 		}
656 		tmpl->fd = mlx5_os_get_devx_channel_fd(tmpl->devx_channel);
657 	}
658 	/* Create CQ using DevX API. */
659 	tmpl->devx_cq = mlx5_rxq_create_devx_cq_resources(dev, idx);
660 	if (!tmpl->devx_cq) {
661 		DRV_LOG(ERR, "Failed to create CQ.");
662 		goto error;
663 	}
664 	/* Create RQ using DevX API. */
665 	tmpl->rq = mlx5_rxq_create_devx_rq_resources(dev, idx);
666 	if (!tmpl->rq) {
667 		DRV_LOG(ERR, "Port %u Rx queue %u RQ creation failure.",
668 			dev->data->port_id, idx);
669 		rte_errno = ENOMEM;
670 		goto error;
671 	}
672 	/* Change queue state to ready. */
673 	ret = mlx5_devx_modify_rq(tmpl, MLX5_RXQ_MOD_RST2RDY);
674 	if (ret)
675 		goto error;
676 	rxq_data->cq_arm_sn = 0;
677 	mlx5_rxq_initialize(rxq_data);
678 	rxq_data->cq_ci = 0;
679 	dev->data->rx_queue_state[idx] = RTE_ETH_QUEUE_STATE_STARTED;
680 	rxq_ctrl->wqn = tmpl->rq->id;
681 	return 0;
682 error:
683 	ret = rte_errno; /* Save rte_errno before cleanup. */
684 	if (tmpl->rq)
685 		claim_zero(mlx5_devx_cmd_destroy(tmpl->rq));
686 	if (tmpl->devx_cq)
687 		claim_zero(mlx5_devx_cmd_destroy(tmpl->devx_cq));
688 	if (tmpl->devx_channel)
689 		mlx5_glue->devx_destroy_event_channel(tmpl->devx_channel);
690 	mlx5_rxq_release_devx_rq_resources(rxq_ctrl);
691 	mlx5_rxq_release_devx_cq_resources(rxq_ctrl);
692 	rte_errno = ret; /* Restore rte_errno. */
693 	return -rte_errno;
694 }
695 
696 /**
697  * Prepare RQT attribute structure for DevX RQT API.
698  *
699  * @param dev
700  *   Pointer to Ethernet device.
701  * @param log_n
702  *   Log of number of queues in the array.
703  * @param ind_tbl
704  *   DevX indirection table object.
705  *
706  * @return
707  *   The RQT attr object initialized, NULL otherwise and rte_errno is set.
708  */
709 static struct mlx5_devx_rqt_attr *
710 mlx5_devx_ind_table_create_rqt_attr(struct rte_eth_dev *dev,
711 				     const unsigned int log_n,
712 				     const uint16_t *queues,
713 				     const uint32_t queues_n)
714 {
715 	struct mlx5_priv *priv = dev->data->dev_private;
716 	struct mlx5_devx_rqt_attr *rqt_attr = NULL;
717 	const unsigned int rqt_n = 1 << log_n;
718 	unsigned int i, j;
719 
720 	rqt_attr = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*rqt_attr) +
721 			      rqt_n * sizeof(uint32_t), 0, SOCKET_ID_ANY);
722 	if (!rqt_attr) {
723 		DRV_LOG(ERR, "Port %u cannot allocate RQT resources.",
724 			dev->data->port_id);
725 		rte_errno = ENOMEM;
726 		return NULL;
727 	}
728 	rqt_attr->rqt_max_size = priv->config.ind_table_max_size;
729 	rqt_attr->rqt_actual_size = rqt_n;
730 	for (i = 0; i != queues_n; ++i) {
731 		struct mlx5_rxq_data *rxq = (*priv->rxqs)[queues[i]];
732 		struct mlx5_rxq_ctrl *rxq_ctrl =
733 				container_of(rxq, struct mlx5_rxq_ctrl, rxq);
734 
735 		rqt_attr->rq_list[i] = rxq_ctrl->obj->rq->id;
736 	}
737 	MLX5_ASSERT(i > 0);
738 	for (j = 0; i != rqt_n; ++j, ++i)
739 		rqt_attr->rq_list[i] = rqt_attr->rq_list[j];
740 	return rqt_attr;
741 }
742 
743 /**
744  * Create RQT using DevX API as a filed of indirection table.
745  *
746  * @param dev
747  *   Pointer to Ethernet device.
748  * @param log_n
749  *   Log of number of queues in the array.
750  * @param ind_tbl
751  *   DevX indirection table object.
752  *
753  * @return
754  *   0 on success, a negative errno value otherwise and rte_errno is set.
755  */
756 static int
757 mlx5_devx_ind_table_new(struct rte_eth_dev *dev, const unsigned int log_n,
758 			struct mlx5_ind_table_obj *ind_tbl)
759 {
760 	struct mlx5_priv *priv = dev->data->dev_private;
761 	struct mlx5_devx_rqt_attr *rqt_attr = NULL;
762 
763 	MLX5_ASSERT(ind_tbl);
764 	rqt_attr = mlx5_devx_ind_table_create_rqt_attr(dev, log_n,
765 							ind_tbl->queues,
766 							ind_tbl->queues_n);
767 	if (!rqt_attr)
768 		return -rte_errno;
769 	ind_tbl->rqt = mlx5_devx_cmd_create_rqt(priv->sh->ctx, rqt_attr);
770 	mlx5_free(rqt_attr);
771 	if (!ind_tbl->rqt) {
772 		DRV_LOG(ERR, "Port %u cannot create DevX RQT.",
773 			dev->data->port_id);
774 		rte_errno = errno;
775 		return -rte_errno;
776 	}
777 	return 0;
778 }
779 
780 /**
781  * Modify RQT using DevX API as a filed of indirection table.
782  *
783  * @param dev
784  *   Pointer to Ethernet device.
785  * @param log_n
786  *   Log of number of queues in the array.
787  * @param ind_tbl
788  *   DevX indirection table object.
789  *
790  * @return
791  *   0 on success, a negative errno value otherwise and rte_errno is set.
792  */
793 static int
794 mlx5_devx_ind_table_modify(struct rte_eth_dev *dev, const unsigned int log_n,
795 			   const uint16_t *queues, const uint32_t queues_n,
796 			   struct mlx5_ind_table_obj *ind_tbl)
797 {
798 	int ret = 0;
799 	struct mlx5_devx_rqt_attr *rqt_attr = NULL;
800 
801 	MLX5_ASSERT(ind_tbl);
802 	rqt_attr = mlx5_devx_ind_table_create_rqt_attr(dev, log_n,
803 							queues,
804 							queues_n);
805 	if (!rqt_attr)
806 		return -rte_errno;
807 	ret = mlx5_devx_cmd_modify_rqt(ind_tbl->rqt, rqt_attr);
808 	mlx5_free(rqt_attr);
809 	if (ret)
810 		DRV_LOG(ERR, "Port %u cannot modify DevX RQT.",
811 			dev->data->port_id);
812 	return ret;
813 }
814 
815 /**
816  * Destroy the DevX RQT object.
817  *
818  * @param ind_table
819  *   Indirection table to release.
820  */
821 static void
822 mlx5_devx_ind_table_destroy(struct mlx5_ind_table_obj *ind_tbl)
823 {
824 	claim_zero(mlx5_devx_cmd_destroy(ind_tbl->rqt));
825 }
826 
827 /**
828  * Set TIR attribute struct with relevant input values.
829  *
830  * @param[in] dev
831  *   Pointer to Ethernet device.
832  * @param[in] rss_key
833  *   RSS key for the Rx hash queue.
834  * @param[in] hash_fields
835  *   Verbs protocol hash field to make the RSS on.
836  * @param[in] ind_tbl
837  *   Indirection table for TIR.
838  * @param[in] tunnel
839  *   Tunnel type.
840  * @param[out] tir_attr
841  *   Parameters structure for TIR creation/modification.
842  *
843  * @return
844  *   The Verbs/DevX object initialised index, 0 otherwise and rte_errno is set.
845  */
846 static void
847 mlx5_devx_tir_attr_set(struct rte_eth_dev *dev, const uint8_t *rss_key,
848 		       uint64_t hash_fields,
849 		       const struct mlx5_ind_table_obj *ind_tbl,
850 		       int tunnel, struct mlx5_devx_tir_attr *tir_attr)
851 {
852 	struct mlx5_priv *priv = dev->data->dev_private;
853 	struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[ind_tbl->queues[0]];
854 	struct mlx5_rxq_ctrl *rxq_ctrl =
855 		container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
856 	enum mlx5_rxq_type rxq_obj_type = rxq_ctrl->type;
857 	bool lro = true;
858 	uint32_t i;
859 
860 	/* Enable TIR LRO only if all the queues were configured for. */
861 	for (i = 0; i < ind_tbl->queues_n; ++i) {
862 		if (!(*priv->rxqs)[ind_tbl->queues[i]]->lro) {
863 			lro = false;
864 			break;
865 		}
866 	}
867 	memset(tir_attr, 0, sizeof(*tir_attr));
868 	tir_attr->disp_type = MLX5_TIRC_DISP_TYPE_INDIRECT;
869 	tir_attr->rx_hash_fn = MLX5_RX_HASH_FN_TOEPLITZ;
870 	tir_attr->tunneled_offload_en = !!tunnel;
871 	/* If needed, translate hash_fields bitmap to PRM format. */
872 	if (hash_fields) {
873 		struct mlx5_rx_hash_field_select *rx_hash_field_select =
874 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
875 			hash_fields & IBV_RX_HASH_INNER ?
876 				&tir_attr->rx_hash_field_selector_inner :
877 #endif
878 				&tir_attr->rx_hash_field_selector_outer;
879 		/* 1 bit: 0: IPv4, 1: IPv6. */
880 		rx_hash_field_select->l3_prot_type =
881 					!!(hash_fields & MLX5_IPV6_IBV_RX_HASH);
882 		/* 1 bit: 0: TCP, 1: UDP. */
883 		rx_hash_field_select->l4_prot_type =
884 					!!(hash_fields & MLX5_UDP_IBV_RX_HASH);
885 		/* Bitmask which sets which fields to use in RX Hash. */
886 		rx_hash_field_select->selected_fields =
887 			((!!(hash_fields & MLX5_L3_SRC_IBV_RX_HASH)) <<
888 			 MLX5_RX_HASH_FIELD_SELECT_SELECTED_FIELDS_SRC_IP) |
889 			(!!(hash_fields & MLX5_L3_DST_IBV_RX_HASH)) <<
890 			 MLX5_RX_HASH_FIELD_SELECT_SELECTED_FIELDS_DST_IP |
891 			(!!(hash_fields & MLX5_L4_SRC_IBV_RX_HASH)) <<
892 			 MLX5_RX_HASH_FIELD_SELECT_SELECTED_FIELDS_L4_SPORT |
893 			(!!(hash_fields & MLX5_L4_DST_IBV_RX_HASH)) <<
894 			 MLX5_RX_HASH_FIELD_SELECT_SELECTED_FIELDS_L4_DPORT;
895 	}
896 	if (rxq_obj_type == MLX5_RXQ_TYPE_HAIRPIN)
897 		tir_attr->transport_domain = priv->sh->td->id;
898 	else
899 		tir_attr->transport_domain = priv->sh->tdn;
900 	memcpy(tir_attr->rx_hash_toeplitz_key, rss_key, MLX5_RSS_HASH_KEY_LEN);
901 	tir_attr->indirect_table = ind_tbl->rqt->id;
902 	if (dev->data->dev_conf.lpbk_mode)
903 		tir_attr->self_lb_block =
904 					MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST;
905 	if (lro) {
906 		tir_attr->lro_timeout_period_usecs = priv->config.lro.timeout;
907 		tir_attr->lro_max_msg_sz = priv->max_lro_msg_size;
908 		tir_attr->lro_enable_mask =
909 				MLX5_TIRC_LRO_ENABLE_MASK_IPV4_LRO |
910 				MLX5_TIRC_LRO_ENABLE_MASK_IPV6_LRO;
911 	}
912 }
913 
914 /**
915  * Create an Rx Hash queue.
916  *
917  * @param dev
918  *   Pointer to Ethernet device.
919  * @param hrxq
920  *   Pointer to Rx Hash queue.
921  * @param tunnel
922  *   Tunnel type.
923  *
924  * @return
925  *   0 on success, a negative errno value otherwise and rte_errno is set.
926  */
927 static int
928 mlx5_devx_hrxq_new(struct rte_eth_dev *dev, struct mlx5_hrxq *hrxq,
929 		   int tunnel __rte_unused)
930 {
931 	struct mlx5_priv *priv = dev->data->dev_private;
932 	struct mlx5_devx_tir_attr tir_attr = {0};
933 	int err;
934 
935 	mlx5_devx_tir_attr_set(dev, hrxq->rss_key, hrxq->hash_fields,
936 			       hrxq->ind_table, tunnel, &tir_attr);
937 	hrxq->tir = mlx5_devx_cmd_create_tir(priv->sh->ctx, &tir_attr);
938 	if (!hrxq->tir) {
939 		DRV_LOG(ERR, "Port %u cannot create DevX TIR.",
940 			dev->data->port_id);
941 		rte_errno = errno;
942 		goto error;
943 	}
944 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
945 	hrxq->action = mlx5_glue->dv_create_flow_action_dest_devx_tir
946 							       (hrxq->tir->obj);
947 	if (!hrxq->action) {
948 		rte_errno = errno;
949 		goto error;
950 	}
951 #endif
952 	return 0;
953 error:
954 	err = rte_errno; /* Save rte_errno before cleanup. */
955 	if (hrxq->tir)
956 		claim_zero(mlx5_devx_cmd_destroy(hrxq->tir));
957 	rte_errno = err; /* Restore rte_errno. */
958 	return -rte_errno;
959 }
960 
961 /**
962  * Destroy a DevX TIR object.
963  *
964  * @param hrxq
965  *   Hash Rx queue to release its tir.
966  */
967 static void
968 mlx5_devx_tir_destroy(struct mlx5_hrxq *hrxq)
969 {
970 	claim_zero(mlx5_devx_cmd_destroy(hrxq->tir));
971 }
972 
973 /**
974  * Modify an Rx Hash queue configuration.
975  *
976  * @param dev
977  *   Pointer to Ethernet device.
978  * @param hrxq
979  *   Hash Rx queue to modify.
980  * @param rss_key
981  *   RSS key for the Rx hash queue.
982  * @param hash_fields
983  *   Verbs protocol hash field to make the RSS on.
984  * @param[in] ind_tbl
985  *   Indirection table for TIR.
986  *
987  * @return
988  *   0 on success, a negative errno value otherwise and rte_errno is set.
989  */
990 static int
991 mlx5_devx_hrxq_modify(struct rte_eth_dev *dev, struct mlx5_hrxq *hrxq,
992 		       const uint8_t *rss_key,
993 		       uint64_t hash_fields,
994 		       const struct mlx5_ind_table_obj *ind_tbl)
995 {
996 	struct mlx5_devx_modify_tir_attr modify_tir = {0};
997 
998 	/*
999 	 * untested for modification fields:
1000 	 * - rx_hash_symmetric not set in hrxq_new(),
1001 	 * - rx_hash_fn set hard-coded in hrxq_new(),
1002 	 * - lro_xxx not set after rxq setup
1003 	 */
1004 	if (ind_tbl != hrxq->ind_table)
1005 		modify_tir.modify_bitmask |=
1006 			MLX5_MODIFY_TIR_IN_MODIFY_BITMASK_INDIRECT_TABLE;
1007 	if (hash_fields != hrxq->hash_fields ||
1008 			memcmp(hrxq->rss_key, rss_key, MLX5_RSS_HASH_KEY_LEN))
1009 		modify_tir.modify_bitmask |=
1010 			MLX5_MODIFY_TIR_IN_MODIFY_BITMASK_HASH;
1011 	mlx5_devx_tir_attr_set(dev, rss_key, hash_fields, ind_tbl,
1012 			       0, /* N/A - tunnel modification unsupported */
1013 			       &modify_tir.tir);
1014 	modify_tir.tirn = hrxq->tir->id;
1015 	if (mlx5_devx_cmd_modify_tir(hrxq->tir, &modify_tir)) {
1016 		DRV_LOG(ERR, "port %u cannot modify DevX TIR",
1017 			dev->data->port_id);
1018 		rte_errno = errno;
1019 		return -rte_errno;
1020 	}
1021 	return 0;
1022 }
1023 
1024 /**
1025  * Create a DevX drop action for Rx Hash queue.
1026  *
1027  * @param dev
1028  *   Pointer to Ethernet device.
1029  *
1030  * @return
1031  *   0 on success, a negative errno value otherwise and rte_errno is set.
1032  */
1033 static int
1034 mlx5_devx_drop_action_create(struct rte_eth_dev *dev)
1035 {
1036 	(void)dev;
1037 	DRV_LOG(ERR, "DevX drop action is not supported yet.");
1038 	rte_errno = ENOTSUP;
1039 	return -rte_errno;
1040 }
1041 
1042 /**
1043  * Release a drop hash Rx queue.
1044  *
1045  * @param dev
1046  *   Pointer to Ethernet device.
1047  */
1048 static void
1049 mlx5_devx_drop_action_destroy(struct rte_eth_dev *dev)
1050 {
1051 	(void)dev;
1052 	DRV_LOG(ERR, "DevX drop action is not supported yet.");
1053 	rte_errno = ENOTSUP;
1054 }
1055 
1056 /**
1057  * Create the Tx hairpin queue object.
1058  *
1059  * @param dev
1060  *   Pointer to Ethernet device.
1061  * @param idx
1062  *   Queue index in DPDK Tx queue array.
1063  *
1064  * @return
1065  *   0 on success, a negative errno value otherwise and rte_errno is set.
1066  */
1067 static int
1068 mlx5_txq_obj_hairpin_new(struct rte_eth_dev *dev, uint16_t idx)
1069 {
1070 	struct mlx5_priv *priv = dev->data->dev_private;
1071 	struct mlx5_txq_data *txq_data = (*priv->txqs)[idx];
1072 	struct mlx5_txq_ctrl *txq_ctrl =
1073 		container_of(txq_data, struct mlx5_txq_ctrl, txq);
1074 	struct mlx5_devx_create_sq_attr attr = { 0 };
1075 	struct mlx5_txq_obj *tmpl = txq_ctrl->obj;
1076 	uint32_t max_wq_data;
1077 
1078 	MLX5_ASSERT(txq_data);
1079 	MLX5_ASSERT(tmpl);
1080 	tmpl->txq_ctrl = txq_ctrl;
1081 	attr.hairpin = 1;
1082 	attr.tis_lst_sz = 1;
1083 	max_wq_data = priv->config.hca_attr.log_max_hairpin_wq_data_sz;
1084 	/* Jumbo frames > 9KB should be supported, and more packets. */
1085 	if (priv->config.log_hp_size != (uint32_t)MLX5_ARG_UNSET) {
1086 		if (priv->config.log_hp_size > max_wq_data) {
1087 			DRV_LOG(ERR, "Total data size %u power of 2 is "
1088 				"too large for hairpin.",
1089 				priv->config.log_hp_size);
1090 			rte_errno = ERANGE;
1091 			return -rte_errno;
1092 		}
1093 		attr.wq_attr.log_hairpin_data_sz = priv->config.log_hp_size;
1094 	} else {
1095 		attr.wq_attr.log_hairpin_data_sz =
1096 				(max_wq_data < MLX5_HAIRPIN_JUMBO_LOG_SIZE) ?
1097 				 max_wq_data : MLX5_HAIRPIN_JUMBO_LOG_SIZE;
1098 	}
1099 	/* Set the packets number to the maximum value for performance. */
1100 	attr.wq_attr.log_hairpin_num_packets =
1101 			attr.wq_attr.log_hairpin_data_sz -
1102 			MLX5_HAIRPIN_QUEUE_STRIDE;
1103 	attr.tis_num = priv->sh->tis->id;
1104 	tmpl->sq = mlx5_devx_cmd_create_sq(priv->sh->ctx, &attr);
1105 	if (!tmpl->sq) {
1106 		DRV_LOG(ERR,
1107 			"Port %u tx hairpin queue %u can't create SQ object.",
1108 			dev->data->port_id, idx);
1109 		rte_errno = errno;
1110 		return -rte_errno;
1111 	}
1112 	return 0;
1113 }
1114 
1115 #ifdef HAVE_MLX5DV_DEVX_UAR_OFFSET
1116 /**
1117  * Release DevX SQ resources.
1118  *
1119  * @param txq_obj
1120  *   DevX Tx queue object.
1121  */
1122 static void
1123 mlx5_txq_release_devx_sq_resources(struct mlx5_txq_obj *txq_obj)
1124 {
1125 	if (txq_obj->sq_devx) {
1126 		claim_zero(mlx5_devx_cmd_destroy(txq_obj->sq_devx));
1127 		txq_obj->sq_devx = NULL;
1128 	}
1129 	if (txq_obj->sq_umem) {
1130 		claim_zero(mlx5_glue->devx_umem_dereg(txq_obj->sq_umem));
1131 		txq_obj->sq_umem = NULL;
1132 	}
1133 	if (txq_obj->sq_buf) {
1134 		mlx5_free(txq_obj->sq_buf);
1135 		txq_obj->sq_buf = NULL;
1136 	}
1137 	if (txq_obj->sq_dbrec_page) {
1138 		claim_zero(mlx5_release_dbr(&txq_obj->txq_ctrl->priv->dbrpgs,
1139 					    mlx5_os_get_umem_id
1140 						 (txq_obj->sq_dbrec_page->umem),
1141 					    txq_obj->sq_dbrec_offset));
1142 		txq_obj->sq_dbrec_page = NULL;
1143 	}
1144 }
1145 
1146 /**
1147  * Release DevX Tx CQ resources.
1148  *
1149  * @param txq_obj
1150  *   DevX Tx queue object.
1151  */
1152 static void
1153 mlx5_txq_release_devx_cq_resources(struct mlx5_txq_obj *txq_obj)
1154 {
1155 	if (txq_obj->cq_devx)
1156 		claim_zero(mlx5_devx_cmd_destroy(txq_obj->cq_devx));
1157 	if (txq_obj->cq_umem)
1158 		claim_zero(mlx5_glue->devx_umem_dereg(txq_obj->cq_umem));
1159 	if (txq_obj->cq_buf)
1160 		mlx5_free(txq_obj->cq_buf);
1161 	if (txq_obj->cq_dbrec_page)
1162 		claim_zero(mlx5_release_dbr(&txq_obj->txq_ctrl->priv->dbrpgs,
1163 					    mlx5_os_get_umem_id
1164 						 (txq_obj->cq_dbrec_page->umem),
1165 					    txq_obj->cq_dbrec_offset));
1166 }
1167 
1168 /**
1169  * Destroy the Tx queue DevX object.
1170  *
1171  * @param txq_obj
1172  *   Txq object to destroy.
1173  */
1174 static void
1175 mlx5_txq_release_devx_resources(struct mlx5_txq_obj *txq_obj)
1176 {
1177 	mlx5_txq_release_devx_sq_resources(txq_obj);
1178 	mlx5_txq_release_devx_cq_resources(txq_obj);
1179 }
1180 
1181 /**
1182  * Create a DevX CQ object and its resources for an Tx queue.
1183  *
1184  * @param dev
1185  *   Pointer to Ethernet device.
1186  * @param idx
1187  *   Queue index in DPDK Tx queue array.
1188  *
1189  * @return
1190  *   Number of CQEs in CQ, 0 otherwise and rte_errno is set.
1191  */
1192 static uint32_t
1193 mlx5_txq_create_devx_cq_resources(struct rte_eth_dev *dev, uint16_t idx)
1194 {
1195 	struct mlx5_priv *priv = dev->data->dev_private;
1196 	struct mlx5_txq_data *txq_data = (*priv->txqs)[idx];
1197 	struct mlx5_txq_ctrl *txq_ctrl =
1198 			container_of(txq_data, struct mlx5_txq_ctrl, txq);
1199 	struct mlx5_txq_obj *txq_obj = txq_ctrl->obj;
1200 	struct mlx5_devx_cq_attr cq_attr = { 0 };
1201 	struct mlx5_cqe *cqe;
1202 	size_t page_size;
1203 	size_t alignment;
1204 	uint32_t cqe_n;
1205 	uint32_t i;
1206 	int ret;
1207 
1208 	MLX5_ASSERT(txq_data);
1209 	MLX5_ASSERT(txq_obj);
1210 	page_size = rte_mem_page_size();
1211 	if (page_size == (size_t)-1) {
1212 		DRV_LOG(ERR, "Failed to get mem page size.");
1213 		rte_errno = ENOMEM;
1214 		return 0;
1215 	}
1216 	/* Allocate memory buffer for CQEs. */
1217 	alignment = MLX5_CQE_BUF_ALIGNMENT;
1218 	if (alignment == (size_t)-1) {
1219 		DRV_LOG(ERR, "Failed to get CQE buf alignment.");
1220 		rte_errno = ENOMEM;
1221 		return 0;
1222 	}
1223 	/* Create the Completion Queue. */
1224 	cqe_n = (1UL << txq_data->elts_n) / MLX5_TX_COMP_THRESH +
1225 		1 + MLX5_TX_COMP_THRESH_INLINE_DIV;
1226 	cqe_n = 1UL << log2above(cqe_n);
1227 	if (cqe_n > UINT16_MAX) {
1228 		DRV_LOG(ERR,
1229 			"Port %u Tx queue %u requests to many CQEs %u.",
1230 			dev->data->port_id, txq_data->idx, cqe_n);
1231 		rte_errno = EINVAL;
1232 		return 0;
1233 	}
1234 	txq_obj->cq_buf = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO,
1235 				      cqe_n * sizeof(struct mlx5_cqe),
1236 				      alignment,
1237 				      priv->sh->numa_node);
1238 	if (!txq_obj->cq_buf) {
1239 		DRV_LOG(ERR,
1240 			"Port %u Tx queue %u cannot allocate memory (CQ).",
1241 			dev->data->port_id, txq_data->idx);
1242 		rte_errno = ENOMEM;
1243 		return 0;
1244 	}
1245 	/* Register allocated buffer in user space with DevX. */
1246 	txq_obj->cq_umem = mlx5_glue->devx_umem_reg(priv->sh->ctx,
1247 						(void *)txq_obj->cq_buf,
1248 						cqe_n * sizeof(struct mlx5_cqe),
1249 						IBV_ACCESS_LOCAL_WRITE);
1250 	if (!txq_obj->cq_umem) {
1251 		rte_errno = errno;
1252 		DRV_LOG(ERR,
1253 			"Port %u Tx queue %u cannot register memory (CQ).",
1254 			dev->data->port_id, txq_data->idx);
1255 		goto error;
1256 	}
1257 	/* Allocate doorbell record for completion queue. */
1258 	txq_obj->cq_dbrec_offset = mlx5_get_dbr(priv->sh->ctx,
1259 						&priv->dbrpgs,
1260 						&txq_obj->cq_dbrec_page);
1261 	if (txq_obj->cq_dbrec_offset < 0) {
1262 		rte_errno = errno;
1263 		DRV_LOG(ERR, "Failed to allocate CQ door-bell.");
1264 		goto error;
1265 	}
1266 	cq_attr.cqe_size = (sizeof(struct mlx5_cqe) == 128) ?
1267 			    MLX5_CQE_SIZE_128B : MLX5_CQE_SIZE_64B;
1268 	cq_attr.uar_page_id = mlx5_os_get_devx_uar_page_id(priv->sh->tx_uar);
1269 	cq_attr.eqn = priv->sh->eqn;
1270 	cq_attr.q_umem_valid = 1;
1271 	cq_attr.q_umem_offset = (uintptr_t)txq_obj->cq_buf % page_size;
1272 	cq_attr.q_umem_id = mlx5_os_get_umem_id(txq_obj->cq_umem);
1273 	cq_attr.db_umem_valid = 1;
1274 	cq_attr.db_umem_offset = txq_obj->cq_dbrec_offset;
1275 	cq_attr.db_umem_id = mlx5_os_get_umem_id(txq_obj->cq_dbrec_page->umem);
1276 	cq_attr.log_cq_size = rte_log2_u32(cqe_n);
1277 	cq_attr.log_page_size = rte_log2_u32(page_size);
1278 	/* Create completion queue object with DevX. */
1279 	txq_obj->cq_devx = mlx5_devx_cmd_create_cq(priv->sh->ctx, &cq_attr);
1280 	if (!txq_obj->cq_devx) {
1281 		rte_errno = errno;
1282 		DRV_LOG(ERR, "Port %u Tx queue %u CQ creation failure.",
1283 			dev->data->port_id, idx);
1284 		goto error;
1285 	}
1286 	/* Initial fill CQ buffer with invalid CQE opcode. */
1287 	cqe = (struct mlx5_cqe *)txq_obj->cq_buf;
1288 	for (i = 0; i < cqe_n; i++) {
1289 		cqe->op_own = (MLX5_CQE_INVALID << 4) | MLX5_CQE_OWNER_MASK;
1290 		++cqe;
1291 	}
1292 	return cqe_n;
1293 error:
1294 	ret = rte_errno;
1295 	mlx5_txq_release_devx_cq_resources(txq_obj);
1296 	rte_errno = ret;
1297 	return 0;
1298 }
1299 
1300 /**
1301  * Create a SQ object and its resources using DevX.
1302  *
1303  * @param dev
1304  *   Pointer to Ethernet device.
1305  * @param idx
1306  *   Queue index in DPDK Tx queue array.
1307  *
1308  * @return
1309  *   Number of WQEs in SQ, 0 otherwise and rte_errno is set.
1310  */
1311 static uint32_t
1312 mlx5_txq_create_devx_sq_resources(struct rte_eth_dev *dev, uint16_t idx)
1313 {
1314 	struct mlx5_priv *priv = dev->data->dev_private;
1315 	struct mlx5_txq_data *txq_data = (*priv->txqs)[idx];
1316 	struct mlx5_txq_ctrl *txq_ctrl =
1317 			container_of(txq_data, struct mlx5_txq_ctrl, txq);
1318 	struct mlx5_txq_obj *txq_obj = txq_ctrl->obj;
1319 	struct mlx5_devx_create_sq_attr sq_attr = { 0 };
1320 	size_t page_size;
1321 	uint32_t wqe_n;
1322 	int ret;
1323 
1324 	MLX5_ASSERT(txq_data);
1325 	MLX5_ASSERT(txq_obj);
1326 	page_size = rte_mem_page_size();
1327 	if (page_size == (size_t)-1) {
1328 		DRV_LOG(ERR, "Failed to get mem page size.");
1329 		rte_errno = ENOMEM;
1330 		return 0;
1331 	}
1332 	wqe_n = RTE_MIN(1UL << txq_data->elts_n,
1333 			(uint32_t)priv->sh->device_attr.max_qp_wr);
1334 	txq_obj->sq_buf = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO,
1335 				      wqe_n * sizeof(struct mlx5_wqe),
1336 				      page_size, priv->sh->numa_node);
1337 	if (!txq_obj->sq_buf) {
1338 		DRV_LOG(ERR,
1339 			"Port %u Tx queue %u cannot allocate memory (SQ).",
1340 			dev->data->port_id, txq_data->idx);
1341 		rte_errno = ENOMEM;
1342 		goto error;
1343 	}
1344 	/* Register allocated buffer in user space with DevX. */
1345 	txq_obj->sq_umem = mlx5_glue->devx_umem_reg
1346 					(priv->sh->ctx,
1347 					 (void *)txq_obj->sq_buf,
1348 					 wqe_n * sizeof(struct mlx5_wqe),
1349 					 IBV_ACCESS_LOCAL_WRITE);
1350 	if (!txq_obj->sq_umem) {
1351 		rte_errno = errno;
1352 		DRV_LOG(ERR,
1353 			"Port %u Tx queue %u cannot register memory (SQ).",
1354 			dev->data->port_id, txq_data->idx);
1355 		goto error;
1356 	}
1357 	/* Allocate doorbell record for send queue. */
1358 	txq_obj->sq_dbrec_offset = mlx5_get_dbr(priv->sh->ctx,
1359 						&priv->dbrpgs,
1360 						&txq_obj->sq_dbrec_page);
1361 	if (txq_obj->sq_dbrec_offset < 0) {
1362 		rte_errno = errno;
1363 		DRV_LOG(ERR, "Failed to allocate SQ door-bell.");
1364 		goto error;
1365 	}
1366 	sq_attr.tis_lst_sz = 1;
1367 	sq_attr.tis_num = priv->sh->tis->id;
1368 	sq_attr.state = MLX5_SQC_STATE_RST;
1369 	sq_attr.cqn = txq_obj->cq_devx->id;
1370 	sq_attr.flush_in_error_en = 1;
1371 	sq_attr.allow_multi_pkt_send_wqe = !!priv->config.mps;
1372 	sq_attr.allow_swp = !!priv->config.swp;
1373 	sq_attr.min_wqe_inline_mode = priv->config.hca_attr.vport_inline_mode;
1374 	sq_attr.wq_attr.uar_page =
1375 				mlx5_os_get_devx_uar_page_id(priv->sh->tx_uar);
1376 	sq_attr.wq_attr.wq_type = MLX5_WQ_TYPE_CYCLIC;
1377 	sq_attr.wq_attr.pd = priv->sh->pdn;
1378 	sq_attr.wq_attr.log_wq_stride = rte_log2_u32(MLX5_WQE_SIZE);
1379 	sq_attr.wq_attr.log_wq_sz = log2above(wqe_n);
1380 	sq_attr.wq_attr.dbr_umem_valid = 1;
1381 	sq_attr.wq_attr.dbr_addr = txq_obj->sq_dbrec_offset;
1382 	sq_attr.wq_attr.dbr_umem_id =
1383 			mlx5_os_get_umem_id(txq_obj->sq_dbrec_page->umem);
1384 	sq_attr.wq_attr.wq_umem_valid = 1;
1385 	sq_attr.wq_attr.wq_umem_id = mlx5_os_get_umem_id(txq_obj->sq_umem);
1386 	sq_attr.wq_attr.wq_umem_offset = (uintptr_t)txq_obj->sq_buf % page_size;
1387 	/* Create Send Queue object with DevX. */
1388 	txq_obj->sq_devx = mlx5_devx_cmd_create_sq(priv->sh->ctx, &sq_attr);
1389 	if (!txq_obj->sq_devx) {
1390 		rte_errno = errno;
1391 		DRV_LOG(ERR, "Port %u Tx queue %u SQ creation failure.",
1392 			dev->data->port_id, idx);
1393 		goto error;
1394 	}
1395 	return wqe_n;
1396 error:
1397 	ret = rte_errno;
1398 	mlx5_txq_release_devx_sq_resources(txq_obj);
1399 	rte_errno = ret;
1400 	return 0;
1401 }
1402 #endif
1403 
1404 /**
1405  * Create the Tx queue DevX object.
1406  *
1407  * @param dev
1408  *   Pointer to Ethernet device.
1409  * @param idx
1410  *   Queue index in DPDK Tx queue array.
1411  *
1412  * @return
1413  *   0 on success, a negative errno value otherwise and rte_errno is set.
1414  */
1415 int
1416 mlx5_txq_devx_obj_new(struct rte_eth_dev *dev, uint16_t idx)
1417 {
1418 	struct mlx5_priv *priv = dev->data->dev_private;
1419 	struct mlx5_txq_data *txq_data = (*priv->txqs)[idx];
1420 	struct mlx5_txq_ctrl *txq_ctrl =
1421 			container_of(txq_data, struct mlx5_txq_ctrl, txq);
1422 
1423 	if (txq_ctrl->type == MLX5_TXQ_TYPE_HAIRPIN)
1424 		return mlx5_txq_obj_hairpin_new(dev, idx);
1425 #ifndef HAVE_MLX5DV_DEVX_UAR_OFFSET
1426 	DRV_LOG(ERR, "Port %u Tx queue %u cannot create with DevX, no UAR.",
1427 		     dev->data->port_id, idx);
1428 	rte_errno = ENOMEM;
1429 	return -rte_errno;
1430 #else
1431 	struct mlx5_dev_ctx_shared *sh = priv->sh;
1432 	struct mlx5_txq_obj *txq_obj = txq_ctrl->obj;
1433 	void *reg_addr;
1434 	uint32_t cqe_n;
1435 	uint32_t wqe_n;
1436 	int ret = 0;
1437 
1438 	MLX5_ASSERT(txq_data);
1439 	MLX5_ASSERT(txq_obj);
1440 	txq_obj->txq_ctrl = txq_ctrl;
1441 	txq_obj->dev = dev;
1442 	cqe_n = mlx5_txq_create_devx_cq_resources(dev, idx);
1443 	if (!cqe_n) {
1444 		rte_errno = errno;
1445 		goto error;
1446 	}
1447 	txq_data->cqe_n = log2above(cqe_n);
1448 	txq_data->cqe_s = 1 << txq_data->cqe_n;
1449 	txq_data->cqe_m = txq_data->cqe_s - 1;
1450 	txq_data->cqes = (volatile struct mlx5_cqe *)txq_obj->cq_buf;
1451 	txq_data->cq_ci = 0;
1452 	txq_data->cq_pi = 0;
1453 	txq_data->cq_db = (volatile uint32_t *)(txq_obj->cq_dbrec_page->dbrs +
1454 						txq_obj->cq_dbrec_offset);
1455 	*txq_data->cq_db = 0;
1456 	/* Create Send Queue object with DevX. */
1457 	wqe_n = mlx5_txq_create_devx_sq_resources(dev, idx);
1458 	if (!wqe_n) {
1459 		rte_errno = errno;
1460 		goto error;
1461 	}
1462 	/* Create the Work Queue. */
1463 	txq_data->wqe_n = log2above(wqe_n);
1464 	txq_data->wqe_s = 1 << txq_data->wqe_n;
1465 	txq_data->wqe_m = txq_data->wqe_s - 1;
1466 	txq_data->wqes = (struct mlx5_wqe *)txq_obj->sq_buf;
1467 	txq_data->wqes_end = txq_data->wqes + txq_data->wqe_s;
1468 	txq_data->wqe_ci = 0;
1469 	txq_data->wqe_pi = 0;
1470 	txq_data->wqe_comp = 0;
1471 	txq_data->wqe_thres = txq_data->wqe_s / MLX5_TX_COMP_THRESH_INLINE_DIV;
1472 	txq_data->qp_db = (volatile uint32_t *)
1473 					(txq_obj->sq_dbrec_page->dbrs +
1474 					 txq_obj->sq_dbrec_offset +
1475 					 MLX5_SND_DBR * sizeof(uint32_t));
1476 	*txq_data->qp_db = 0;
1477 	txq_data->qp_num_8s = txq_obj->sq_devx->id << 8;
1478 	/* Change Send Queue state to Ready-to-Send. */
1479 	ret = mlx5_devx_modify_sq(txq_obj, MLX5_TXQ_MOD_RST2RDY, 0);
1480 	if (ret) {
1481 		rte_errno = errno;
1482 		DRV_LOG(ERR,
1483 			"Port %u Tx queue %u SQ state to SQC_STATE_RDY failed.",
1484 			dev->data->port_id, idx);
1485 		goto error;
1486 	}
1487 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
1488 	/*
1489 	 * If using DevX need to query and store TIS transport domain value.
1490 	 * This is done once per port.
1491 	 * Will use this value on Rx, when creating matching TIR.
1492 	 */
1493 	if (!priv->sh->tdn)
1494 		priv->sh->tdn = priv->sh->td->id;
1495 #endif
1496 	MLX5_ASSERT(sh->tx_uar);
1497 	reg_addr = mlx5_os_get_devx_uar_reg_addr(sh->tx_uar);
1498 	MLX5_ASSERT(reg_addr);
1499 	txq_ctrl->bf_reg = reg_addr;
1500 	txq_ctrl->uar_mmap_offset =
1501 				mlx5_os_get_devx_uar_mmap_offset(sh->tx_uar);
1502 	txq_uar_init(txq_ctrl);
1503 	dev->data->tx_queue_state[idx] = RTE_ETH_QUEUE_STATE_STARTED;
1504 	return 0;
1505 error:
1506 	ret = rte_errno; /* Save rte_errno before cleanup. */
1507 	mlx5_txq_release_devx_resources(txq_obj);
1508 	rte_errno = ret; /* Restore rte_errno. */
1509 	return -rte_errno;
1510 #endif
1511 }
1512 
1513 /**
1514  * Release an Tx DevX queue object.
1515  *
1516  * @param txq_obj
1517  *   DevX Tx queue object.
1518  */
1519 void
1520 mlx5_txq_devx_obj_release(struct mlx5_txq_obj *txq_obj)
1521 {
1522 	MLX5_ASSERT(txq_obj);
1523 	if (txq_obj->txq_ctrl->type == MLX5_TXQ_TYPE_HAIRPIN) {
1524 		if (txq_obj->tis)
1525 			claim_zero(mlx5_devx_cmd_destroy(txq_obj->tis));
1526 #ifdef HAVE_MLX5DV_DEVX_UAR_OFFSET
1527 	} else {
1528 		mlx5_txq_release_devx_resources(txq_obj);
1529 #endif
1530 	}
1531 }
1532 
1533 struct mlx5_obj_ops devx_obj_ops = {
1534 	.rxq_obj_modify_vlan_strip = mlx5_rxq_obj_modify_rq_vlan_strip,
1535 	.rxq_obj_new = mlx5_rxq_devx_obj_new,
1536 	.rxq_event_get = mlx5_rx_devx_get_event,
1537 	.rxq_obj_modify = mlx5_devx_modify_rq,
1538 	.rxq_obj_release = mlx5_rxq_devx_obj_release,
1539 	.ind_table_new = mlx5_devx_ind_table_new,
1540 	.ind_table_modify = mlx5_devx_ind_table_modify,
1541 	.ind_table_destroy = mlx5_devx_ind_table_destroy,
1542 	.hrxq_new = mlx5_devx_hrxq_new,
1543 	.hrxq_destroy = mlx5_devx_tir_destroy,
1544 	.hrxq_modify = mlx5_devx_hrxq_modify,
1545 	.drop_action_create = mlx5_devx_drop_action_create,
1546 	.drop_action_destroy = mlx5_devx_drop_action_destroy,
1547 	.txq_obj_new = mlx5_txq_devx_obj_new,
1548 	.txq_obj_modify = mlx5_devx_modify_sq,
1549 	.txq_obj_release = mlx5_txq_devx_obj_release,
1550 };
1551