xref: /dpdk/drivers/net/mlx5/mlx5_devx.c (revision cd00dce6253f34504517a81b2134cf2f58ec6a0a)
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_common_devx.h>
19 #include <mlx5_malloc.h>
20 
21 #include "mlx5.h"
22 #include "mlx5_common_os.h"
23 #include "mlx5_tx.h"
24 #include "mlx5_rx.h"
25 #include "mlx5_utils.h"
26 #include "mlx5_devx.h"
27 #include "mlx5_flow.h"
28 #include "mlx5_flow_os.h"
29 
30 /**
31  * Validate given external queue's port is valid or not.
32  *
33  * @param[in] port_id
34  *   The port identifier of the Ethernet device.
35  *
36  * @return
37  *   0 on success, non-0 otherwise
38  */
39 int
40 mlx5_devx_extq_port_validate(uint16_t port_id)
41 {
42 	struct rte_eth_dev *dev;
43 	struct mlx5_priv *priv;
44 
45 	if (rte_eth_dev_is_valid_port(port_id) < 0) {
46 		DRV_LOG(ERR, "There is no Ethernet device for port %u.",
47 			port_id);
48 		rte_errno = ENODEV;
49 		return -rte_errno;
50 	}
51 	dev = &rte_eth_devices[port_id];
52 	priv = dev->data->dev_private;
53 	if (!mlx5_imported_pd_and_ctx(priv->sh->cdev)) {
54 		DRV_LOG(ERR, "Port %u "
55 			"external queue isn't supported on local PD and CTX.",
56 			port_id);
57 		rte_errno = ENOTSUP;
58 		return -rte_errno;
59 	}
60 	if (!mlx5_devx_obj_ops_en(priv->sh)) {
61 		DRV_LOG(ERR,
62 			"Port %u external queue isn't supported by Verbs API.",
63 			port_id);
64 		rte_errno = ENOTSUP;
65 		return -rte_errno;
66 	}
67 	return 0;
68 }
69 
70 /**
71  * Modify RQ vlan stripping offload
72  *
73  * @param rxq
74  *   Rx queue.
75  * @param on
76  *   Enable/disable VLAN stripping.
77  *
78  * @return
79  *   0 on success, non-0 otherwise
80  */
81 static int
82 mlx5_rxq_obj_modify_rq_vlan_strip(struct mlx5_rxq_priv *rxq, int on)
83 {
84 	struct mlx5_devx_modify_rq_attr rq_attr;
85 
86 	memset(&rq_attr, 0, sizeof(rq_attr));
87 	rq_attr.rq_state = MLX5_RQC_STATE_RDY;
88 	rq_attr.state = MLX5_RQC_STATE_RDY;
89 	rq_attr.vsd = (on ? 0 : 1);
90 	rq_attr.modify_bitmask = MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD;
91 	return mlx5_devx_cmd_modify_rq(rxq->devx_rq.rq, &rq_attr);
92 }
93 
94 /**
95  * Modify RQ using DevX API.
96  *
97  * @param rxq
98  *   DevX rx queue.
99  * @param type
100  *   Type of change queue state.
101  *
102  * @return
103  *   0 on success, a negative errno value otherwise and rte_errno is set.
104  */
105 int
106 mlx5_devx_modify_rq(struct mlx5_rxq_priv *rxq, uint8_t type)
107 {
108 	struct mlx5_devx_modify_rq_attr rq_attr;
109 
110 	memset(&rq_attr, 0, sizeof(rq_attr));
111 	switch (type) {
112 	case MLX5_RXQ_MOD_ERR2RST:
113 		rq_attr.rq_state = MLX5_RQC_STATE_ERR;
114 		rq_attr.state = MLX5_RQC_STATE_RST;
115 		break;
116 	case MLX5_RXQ_MOD_RST2RDY:
117 		rq_attr.rq_state = MLX5_RQC_STATE_RST;
118 		rq_attr.state = MLX5_RQC_STATE_RDY;
119 		if (rxq->lwm) {
120 			rq_attr.modify_bitmask |=
121 				MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_WQ_LWM;
122 			rq_attr.lwm = rxq->lwm;
123 		}
124 		break;
125 	case MLX5_RXQ_MOD_RDY2ERR:
126 		rq_attr.rq_state = MLX5_RQC_STATE_RDY;
127 		rq_attr.state = MLX5_RQC_STATE_ERR;
128 		break;
129 	case MLX5_RXQ_MOD_RDY2RST:
130 		rq_attr.rq_state = MLX5_RQC_STATE_RDY;
131 		rq_attr.state = MLX5_RQC_STATE_RST;
132 		break;
133 	case MLX5_RXQ_MOD_RDY2RDY:
134 		rq_attr.rq_state = MLX5_RQC_STATE_RDY;
135 		rq_attr.state = MLX5_RQC_STATE_RDY;
136 		rq_attr.modify_bitmask |= MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_WQ_LWM;
137 		rq_attr.lwm = rxq->lwm;
138 		break;
139 	default:
140 		break;
141 	}
142 	if (rxq->ctrl->is_hairpin)
143 		return mlx5_devx_cmd_modify_rq(rxq->ctrl->obj->rq, &rq_attr);
144 	return mlx5_devx_cmd_modify_rq(rxq->devx_rq.rq, &rq_attr);
145 }
146 
147 /**
148  * Modify SQ using DevX API.
149  *
150  * @param txq_obj
151  *   DevX Tx queue object.
152  * @param type
153  *   Type of change queue state.
154  * @param dev_port
155  *   Unnecessary.
156  *
157  * @return
158  *   0 on success, a negative errno value otherwise and rte_errno is set.
159  */
160 int
161 mlx5_txq_devx_modify(struct mlx5_txq_obj *obj, enum mlx5_txq_modify_type type,
162 		     uint8_t dev_port)
163 {
164 	struct mlx5_devx_modify_sq_attr msq_attr = { 0 };
165 	int ret;
166 
167 	if (type != MLX5_TXQ_MOD_RST2RDY) {
168 		/* Change queue state to reset. */
169 		if (type == MLX5_TXQ_MOD_ERR2RDY)
170 			msq_attr.sq_state = MLX5_SQC_STATE_ERR;
171 		else
172 			msq_attr.sq_state = MLX5_SQC_STATE_RDY;
173 		msq_attr.state = MLX5_SQC_STATE_RST;
174 		ret = mlx5_devx_cmd_modify_sq(obj->sq_obj.sq, &msq_attr);
175 		if (ret) {
176 			DRV_LOG(ERR, "Cannot change the Tx SQ state to RESET"
177 				" %s", strerror(errno));
178 			rte_errno = errno;
179 			return ret;
180 		}
181 	}
182 	if (type != MLX5_TXQ_MOD_RDY2RST) {
183 		/* Change queue state to ready. */
184 		msq_attr.sq_state = MLX5_SQC_STATE_RST;
185 		msq_attr.state = MLX5_SQC_STATE_RDY;
186 		ret = mlx5_devx_cmd_modify_sq(obj->sq_obj.sq, &msq_attr);
187 		if (ret) {
188 			DRV_LOG(ERR, "Cannot change the Tx SQ state to READY"
189 				" %s", strerror(errno));
190 			rte_errno = errno;
191 			return ret;
192 		}
193 	}
194 	/*
195 	 * The dev_port variable is relevant only in Verbs API, and there is a
196 	 * pointer that points to this function and a parallel function in verbs
197 	 * intermittently, so they should have the same parameters.
198 	 */
199 	(void)dev_port;
200 	return 0;
201 }
202 
203 /**
204  * Release an Rx DevX queue object.
205  *
206  * @param rxq
207  *   DevX Rx queue.
208  */
209 static void
210 mlx5_rxq_devx_obj_release(struct mlx5_rxq_priv *rxq)
211 {
212 	struct mlx5_rxq_obj *rxq_obj = rxq->ctrl->obj;
213 
214 	if (rxq_obj == NULL)
215 		return;
216 	if (rxq_obj->rxq_ctrl->is_hairpin) {
217 		if (rxq_obj->rq == NULL)
218 			return;
219 		mlx5_devx_modify_rq(rxq, MLX5_RXQ_MOD_RDY2RST);
220 		claim_zero(mlx5_devx_cmd_destroy(rxq_obj->rq));
221 	} else {
222 		if (rxq->devx_rq.rq == NULL)
223 			return;
224 		mlx5_devx_rq_destroy(&rxq->devx_rq);
225 		if (rxq->devx_rq.rmp != NULL && rxq->devx_rq.rmp->ref_cnt > 0)
226 			return;
227 		mlx5_devx_cq_destroy(&rxq_obj->cq_obj);
228 		memset(&rxq_obj->cq_obj, 0, sizeof(rxq_obj->cq_obj));
229 		if (rxq_obj->devx_channel) {
230 			mlx5_os_devx_destroy_event_channel
231 							(rxq_obj->devx_channel);
232 			rxq_obj->devx_channel = NULL;
233 		}
234 	}
235 	rxq->ctrl->started = false;
236 }
237 
238 /**
239  * Get event for an Rx DevX queue object.
240  *
241  * @param rxq_obj
242  *   DevX Rx queue object.
243  *
244  * @return
245  *   0 on success, a negative errno value otherwise and rte_errno is set.
246  */
247 static int
248 mlx5_rx_devx_get_event(struct mlx5_rxq_obj *rxq_obj)
249 {
250 #ifdef HAVE_IBV_DEVX_EVENT
251 	union {
252 		struct mlx5dv_devx_async_event_hdr event_resp;
253 		uint8_t buf[sizeof(struct mlx5dv_devx_async_event_hdr) + 128];
254 	} out;
255 	int ret = mlx5_glue->devx_get_event(rxq_obj->devx_channel,
256 					    &out.event_resp,
257 					    sizeof(out.buf));
258 
259 	if (ret < 0) {
260 		rte_errno = errno;
261 		return -rte_errno;
262 	}
263 	if (out.event_resp.cookie != (uint64_t)(uintptr_t)rxq_obj->cq_obj.cq) {
264 		rte_errno = EINVAL;
265 		return -rte_errno;
266 	}
267 	return 0;
268 #else
269 	(void)rxq_obj;
270 	rte_errno = ENOTSUP;
271 	return -rte_errno;
272 #endif /* HAVE_IBV_DEVX_EVENT */
273 }
274 
275 /**
276  * Get LWM event for shared context, return the correct port/rxq for this event.
277  *
278  * @param priv
279  *   Mlx5_priv object.
280  * @param rxq_idx [out]
281  *   Which rxq gets this event.
282  * @param port_id [out]
283  *   Which port gets this event.
284  *
285  * @return
286  *   0 on success, a negative errno value otherwise and rte_errno is set.
287  */
288 static int
289 mlx5_rx_devx_get_event_lwm(struct mlx5_priv *priv, int *rxq_idx, int *port_id)
290 {
291 #ifdef HAVE_IBV_DEVX_EVENT
292 	union {
293 		struct mlx5dv_devx_async_event_hdr event_resp;
294 		uint8_t buf[sizeof(struct mlx5dv_devx_async_event_hdr) + 128];
295 	} out;
296 	int ret;
297 
298 	memset(&out, 0, sizeof(out));
299 	ret = mlx5_glue->devx_get_event(priv->sh->devx_channel_lwm,
300 					&out.event_resp,
301 					sizeof(out.buf));
302 	if (ret < 0) {
303 		rte_errno = errno;
304 		DRV_LOG(WARNING, "%s err\n", __func__);
305 		return -rte_errno;
306 	}
307 	*port_id = (((uint32_t)out.event_resp.cookie) >>
308 		    LWM_COOKIE_PORTID_OFFSET) & LWM_COOKIE_PORTID_MASK;
309 	*rxq_idx = (((uint32_t)out.event_resp.cookie) >>
310 		    LWM_COOKIE_RXQID_OFFSET) & LWM_COOKIE_RXQID_MASK;
311 	return 0;
312 #else
313 	(void)priv;
314 	(void)rxq_idx;
315 	(void)port_id;
316 	rte_errno = ENOTSUP;
317 	return -rte_errno;
318 #endif /* HAVE_IBV_DEVX_EVENT */
319 }
320 
321 /**
322  * Create a RQ object using DevX.
323  *
324  * @param rxq
325  *   Pointer to Rx queue.
326  *
327  * @return
328  *   0 on success, a negative errno value otherwise and rte_errno is set.
329  */
330 static int
331 mlx5_rxq_create_devx_rq_resources(struct mlx5_rxq_priv *rxq)
332 {
333 	struct mlx5_priv *priv = rxq->priv;
334 	struct mlx5_common_device *cdev = priv->sh->cdev;
335 	struct mlx5_rxq_ctrl *rxq_ctrl = rxq->ctrl;
336 	struct mlx5_rxq_data *rxq_data = &rxq->ctrl->rxq;
337 	struct mlx5_devx_create_rq_attr rq_attr = { 0 };
338 	uint16_t log_desc_n = rxq_data->elts_n - rxq_data->sges_n;
339 	uint32_t wqe_size, log_wqe_size;
340 
341 	/* Fill RQ attributes. */
342 	rq_attr.mem_rq_type = MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE;
343 	rq_attr.flush_in_error_en = 1;
344 	rq_attr.vsd = (rxq_data->vlan_strip) ? 0 : 1;
345 	rq_attr.cqn = rxq_ctrl->obj->cq_obj.cq->id;
346 	rq_attr.scatter_fcs = (rxq_data->crc_present) ? 1 : 0;
347 	rq_attr.ts_format =
348 			mlx5_ts_format_conv(cdev->config.hca_attr.rq_ts_format);
349 	/* Fill WQ attributes for this RQ. */
350 	if (mlx5_rxq_mprq_enabled(rxq_data)) {
351 		rq_attr.wq_attr.wq_type = MLX5_WQ_TYPE_CYCLIC_STRIDING_RQ;
352 		/*
353 		 * Number of strides in each WQE:
354 		 * 512*2^single_wqe_log_num_of_strides.
355 		 */
356 		rq_attr.wq_attr.single_wqe_log_num_of_strides =
357 				rxq_data->log_strd_num -
358 				MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
359 		/* Stride size = (2^single_stride_log_num_of_bytes)*64B. */
360 		rq_attr.wq_attr.single_stride_log_num_of_bytes =
361 				rxq_data->log_strd_sz -
362 				MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
363 		wqe_size = sizeof(struct mlx5_wqe_mprq);
364 	} else {
365 		rq_attr.wq_attr.wq_type = MLX5_WQ_TYPE_CYCLIC;
366 		wqe_size = sizeof(struct mlx5_wqe_data_seg);
367 	}
368 	log_wqe_size = log2above(wqe_size) + rxq_data->sges_n;
369 	wqe_size = 1 << log_wqe_size; /* round up power of two.*/
370 	rq_attr.wq_attr.log_wq_stride = log_wqe_size;
371 	rq_attr.wq_attr.log_wq_sz = log_desc_n;
372 	rq_attr.wq_attr.end_padding_mode = priv->config.hw_padding ?
373 						MLX5_WQ_END_PAD_MODE_ALIGN :
374 						MLX5_WQ_END_PAD_MODE_NONE;
375 	rq_attr.wq_attr.pd = cdev->pdn;
376 	rq_attr.counter_set_id = priv->counter_set_id;
377 	rq_attr.delay_drop_en = rxq_data->delay_drop;
378 	rq_attr.user_index = rte_cpu_to_be_16(priv->dev_data->port_id);
379 	if (rxq_data->shared) /* Create RMP based RQ. */
380 		rxq->devx_rq.rmp = &rxq_ctrl->obj->devx_rmp;
381 	/* Create RQ using DevX API. */
382 	return mlx5_devx_rq_create(cdev->ctx, &rxq->devx_rq, wqe_size,
383 				   log_desc_n, &rq_attr, rxq_ctrl->socket);
384 }
385 
386 /**
387  * Create a DevX CQ object for an Rx queue.
388  *
389  * @param rxq
390  *   Pointer to Rx queue.
391  *
392  * @return
393  *   0 on success, a negative errno value otherwise and rte_errno is set.
394  */
395 static int
396 mlx5_rxq_create_devx_cq_resources(struct mlx5_rxq_priv *rxq)
397 {
398 	struct mlx5_devx_cq *cq_obj = 0;
399 	struct mlx5_devx_cq_attr cq_attr = { 0 };
400 	struct mlx5_priv *priv = rxq->priv;
401 	struct mlx5_dev_ctx_shared *sh = priv->sh;
402 	uint16_t port_id = priv->dev_data->port_id;
403 	struct mlx5_rxq_ctrl *rxq_ctrl = rxq->ctrl;
404 	struct mlx5_rxq_data *rxq_data = &rxq_ctrl->rxq;
405 	unsigned int cqe_n = mlx5_rxq_cqe_num(rxq_data);
406 	uint32_t log_cqe_n;
407 	uint16_t event_nums[1] = { 0 };
408 	int ret = 0;
409 
410 	if (rxq_ctrl->started)
411 		return 0;
412 	if (priv->config.cqe_comp && !rxq_data->hw_timestamp &&
413 	    !rxq_data->lro) {
414 		cq_attr.cqe_comp_en = 1u;
415 		cq_attr.cqe_comp_layout = priv->config.enh_cqe_comp;
416 		rxq_data->cqe_comp_layout = cq_attr.cqe_comp_layout;
417 		rxq_data->mcqe_format = priv->config.cqe_comp_fmt;
418 		rxq_data->byte_mask = UINT32_MAX;
419 		switch (priv->config.cqe_comp_fmt) {
420 		case MLX5_CQE_RESP_FORMAT_HASH:
421 			/* fallthrough */
422 		case MLX5_CQE_RESP_FORMAT_CSUM:
423 			/*
424 			 * Select CSUM miniCQE format only for non-vectorized
425 			 * MPRQ Rx burst, use HASH miniCQE format for others.
426 			 */
427 			if (mlx5_rxq_check_vec_support(rxq_data) < 0 &&
428 			    mlx5_rxq_mprq_enabled(rxq_data))
429 				cq_attr.mini_cqe_res_format =
430 					MLX5_CQE_RESP_FORMAT_CSUM_STRIDX;
431 			else
432 				cq_attr.mini_cqe_res_format =
433 					MLX5_CQE_RESP_FORMAT_HASH;
434 			rxq_data->mcqe_format = cq_attr.mini_cqe_res_format;
435 			break;
436 		case MLX5_CQE_RESP_FORMAT_FTAG_STRIDX:
437 			rxq_data->byte_mask = MLX5_LEN_WITH_MARK_MASK;
438 			/* fallthrough */
439 		case MLX5_CQE_RESP_FORMAT_CSUM_STRIDX:
440 			cq_attr.mini_cqe_res_format = priv->config.cqe_comp_fmt;
441 			break;
442 		case MLX5_CQE_RESP_FORMAT_L34H_STRIDX:
443 			cq_attr.mini_cqe_res_format = 0;
444 			cq_attr.mini_cqe_res_format_ext = 1;
445 			break;
446 		}
447 		DRV_LOG(DEBUG,
448 			"Port %u Rx CQE compression is enabled, format %d.",
449 			port_id, priv->config.cqe_comp_fmt);
450 		/*
451 		 * For vectorized Rx, it must not be doubled in order to
452 		 * make cq_ci and rq_ci aligned.
453 		 */
454 		if (mlx5_rxq_check_vec_support(rxq_data) < 0)
455 			cqe_n *= 2;
456 	} else if (priv->config.cqe_comp && rxq_data->hw_timestamp) {
457 		DRV_LOG(DEBUG,
458 			"Port %u Rx CQE compression is disabled for HW timestamp.",
459 			port_id);
460 	} else if (priv->config.cqe_comp && rxq_data->lro) {
461 		DRV_LOG(DEBUG,
462 			"Port %u Rx CQE compression is disabled for LRO.",
463 			port_id);
464 	}
465 	cq_attr.uar_page_id = mlx5_os_get_devx_uar_page_id(sh->rx_uar.obj);
466 	log_cqe_n = log2above(cqe_n);
467 	/* Create CQ using DevX API. */
468 	ret = mlx5_devx_cq_create(sh->cdev->ctx, &rxq_ctrl->obj->cq_obj,
469 				  log_cqe_n, &cq_attr, sh->numa_node);
470 	if (ret)
471 		return ret;
472 	cq_obj = &rxq_ctrl->obj->cq_obj;
473 	rxq_data->cqes = (volatile struct mlx5_cqe (*)[])
474 							(uintptr_t)cq_obj->cqes;
475 	rxq_data->cq_db = cq_obj->db_rec;
476 	rxq_data->uar_data = sh->rx_uar.cq_db;
477 	rxq_data->cqe_n = log_cqe_n;
478 	rxq_data->cqn = cq_obj->cq->id;
479 	rxq_data->cq_ci = 0;
480 	if (rxq_ctrl->obj->devx_channel) {
481 		ret = mlx5_os_devx_subscribe_devx_event
482 					      (rxq_ctrl->obj->devx_channel,
483 					       cq_obj->cq->obj,
484 					       sizeof(event_nums),
485 					       event_nums,
486 					       (uint64_t)(uintptr_t)cq_obj->cq);
487 		if (ret) {
488 			DRV_LOG(ERR, "Fail to subscribe CQ to event channel.");
489 			ret = errno;
490 			mlx5_devx_cq_destroy(cq_obj);
491 			memset(cq_obj, 0, sizeof(*cq_obj));
492 			rte_errno = ret;
493 			return -ret;
494 		}
495 	}
496 	return 0;
497 }
498 
499 /**
500  * Create a global queue counter for all the port hairpin queues.
501  *
502  * @param priv
503  *   Device private data.
504  *
505  * @return
506  *   The counter_set_id of the queue counter object, 0 otherwise.
507  */
508 static uint32_t
509 mlx5_set_hairpin_queue_counter_obj(struct mlx5_priv *priv)
510 {
511 	if (priv->q_counters_hairpin != NULL)
512 		return priv->q_counters_hairpin->id;
513 
514 	/* Queue counter allocation failed in the past - don't try again. */
515 	if (priv->q_counters_allocation_failure != 0)
516 		return 0;
517 
518 	if (priv->pci_dev == NULL) {
519 		DRV_LOG(DEBUG, "Hairpin out of buffer counter is "
520 				"only supported on PCI device.");
521 		priv->q_counters_allocation_failure = 1;
522 		return 0;
523 	}
524 
525 	switch (priv->pci_dev->id.device_id) {
526 	/* Counting out of buffer drops on hairpin queues is supported only on CX7 and up. */
527 	case PCI_DEVICE_ID_MELLANOX_CONNECTX7:
528 	case PCI_DEVICE_ID_MELLANOX_CONNECTXVF:
529 	case PCI_DEVICE_ID_MELLANOX_BLUEFIELD3:
530 	case PCI_DEVICE_ID_MELLANOX_BLUEFIELDVF:
531 
532 		priv->q_counters_hairpin = mlx5_devx_cmd_queue_counter_alloc(priv->sh->cdev->ctx);
533 		if (priv->q_counters_hairpin == NULL) {
534 			/* Failed to allocate */
535 			DRV_LOG(DEBUG, "Some of the statistics of port %d "
536 				"will not be available.", priv->dev_data->port_id);
537 			priv->q_counters_allocation_failure = 1;
538 			return 0;
539 		}
540 		return priv->q_counters_hairpin->id;
541 	default:
542 		DRV_LOG(DEBUG, "Hairpin out of buffer counter "
543 				"is not available on this NIC.");
544 		priv->q_counters_allocation_failure = 1;
545 		return 0;
546 	}
547 }
548 
549 /**
550  * Create the Rx hairpin queue object.
551  *
552  * @param rxq
553  *   Pointer to Rx queue.
554  *
555  * @return
556  *   0 on success, a negative errno value otherwise and rte_errno is set.
557  */
558 static int
559 mlx5_rxq_obj_hairpin_new(struct mlx5_rxq_priv *rxq)
560 {
561 	uint16_t idx = rxq->idx;
562 	struct mlx5_priv *priv = rxq->priv;
563 	struct mlx5_hca_attr *hca_attr __rte_unused = &priv->sh->cdev->config.hca_attr;
564 	struct mlx5_rxq_ctrl *rxq_ctrl = rxq->ctrl;
565 	struct mlx5_devx_create_rq_attr unlocked_attr = { 0 };
566 	struct mlx5_devx_create_rq_attr locked_attr = { 0 };
567 	struct mlx5_rxq_obj *tmpl = rxq_ctrl->obj;
568 	uint32_t max_wq_data;
569 
570 	MLX5_ASSERT(rxq != NULL && rxq->ctrl != NULL && tmpl != NULL);
571 	tmpl->rxq_ctrl = rxq_ctrl;
572 	unlocked_attr.hairpin = 1;
573 	max_wq_data =
574 		priv->sh->cdev->config.hca_attr.log_max_hairpin_wq_data_sz;
575 	/* Jumbo frames > 9KB should be supported, and more packets. */
576 	if (priv->config.log_hp_size != (uint32_t)MLX5_ARG_UNSET) {
577 		if (priv->config.log_hp_size > max_wq_data) {
578 			DRV_LOG(ERR, "Total data size %u power of 2 is "
579 				"too large for hairpin.",
580 				priv->config.log_hp_size);
581 			rte_errno = ERANGE;
582 			return -rte_errno;
583 		}
584 		unlocked_attr.wq_attr.log_hairpin_data_sz = priv->config.log_hp_size;
585 	} else {
586 		unlocked_attr.wq_attr.log_hairpin_data_sz =
587 				(max_wq_data < MLX5_HAIRPIN_JUMBO_LOG_SIZE) ?
588 				 max_wq_data : MLX5_HAIRPIN_JUMBO_LOG_SIZE;
589 	}
590 	/* Set the packets number to the maximum value for performance. */
591 	unlocked_attr.wq_attr.log_hairpin_num_packets =
592 			unlocked_attr.wq_attr.log_hairpin_data_sz -
593 			MLX5_HAIRPIN_QUEUE_STRIDE;
594 
595 	unlocked_attr.counter_set_id = mlx5_set_hairpin_queue_counter_obj(priv);
596 
597 	rxq_ctrl->rxq.delay_drop = priv->config.hp_delay_drop;
598 	unlocked_attr.delay_drop_en = priv->config.hp_delay_drop;
599 	unlocked_attr.hairpin_data_buffer_type =
600 			MLX5_RQC_HAIRPIN_DATA_BUFFER_TYPE_UNLOCKED_INTERNAL_BUFFER;
601 	if (rxq->hairpin_conf.use_locked_device_memory) {
602 		/*
603 		 * It is assumed that configuration is verified against capabilities
604 		 * during queue setup.
605 		 */
606 		MLX5_ASSERT(hca_attr->hairpin_data_buffer_locked);
607 		rte_memcpy(&locked_attr, &unlocked_attr, sizeof(locked_attr));
608 		locked_attr.hairpin_data_buffer_type =
609 				MLX5_RQC_HAIRPIN_DATA_BUFFER_TYPE_LOCKED_INTERNAL_BUFFER;
610 		tmpl->rq = mlx5_devx_cmd_create_rq(priv->sh->cdev->ctx, &locked_attr,
611 						   rxq_ctrl->socket);
612 		if (!tmpl->rq && rxq->hairpin_conf.force_memory) {
613 			DRV_LOG(ERR, "Port %u Rx hairpin queue %u can't create RQ object"
614 				     " with locked memory buffer",
615 				     priv->dev_data->port_id, idx);
616 			return -rte_errno;
617 		} else if (!tmpl->rq && !rxq->hairpin_conf.force_memory) {
618 			DRV_LOG(WARNING, "Port %u Rx hairpin queue %u can't create RQ object"
619 					 " with locked memory buffer. Falling back to unlocked"
620 					 " device memory.",
621 					 priv->dev_data->port_id, idx);
622 			rte_errno = 0;
623 			goto create_rq_unlocked;
624 		}
625 		goto create_rq_set_state;
626 	}
627 
628 create_rq_unlocked:
629 	tmpl->rq = mlx5_devx_cmd_create_rq(priv->sh->cdev->ctx, &unlocked_attr,
630 					   rxq_ctrl->socket);
631 	if (!tmpl->rq) {
632 		DRV_LOG(ERR,
633 			"Port %u Rx hairpin queue %u can't create rq object.",
634 			priv->dev_data->port_id, idx);
635 		rte_errno = errno;
636 		return -rte_errno;
637 	}
638 create_rq_set_state:
639 	priv->dev_data->rx_queue_state[idx] = RTE_ETH_QUEUE_STATE_HAIRPIN;
640 	return 0;
641 }
642 
643 /**
644  * Create the Rx queue DevX object.
645  *
646  * @param rxq
647  *   Pointer to Rx queue.
648  *
649  * @return
650  *   0 on success, a negative errno value otherwise and rte_errno is set.
651  */
652 static int
653 mlx5_rxq_devx_obj_new(struct mlx5_rxq_priv *rxq)
654 {
655 	struct mlx5_priv *priv = rxq->priv;
656 	struct mlx5_rxq_ctrl *rxq_ctrl = rxq->ctrl;
657 	struct mlx5_rxq_data *rxq_data = &rxq_ctrl->rxq;
658 	struct mlx5_rxq_obj *tmpl = rxq_ctrl->obj;
659 	int ret = 0;
660 
661 	MLX5_ASSERT(rxq_data);
662 	MLX5_ASSERT(tmpl);
663 	if (rxq_ctrl->is_hairpin)
664 		return mlx5_rxq_obj_hairpin_new(rxq);
665 	tmpl->rxq_ctrl = rxq_ctrl;
666 	if (rxq_ctrl->irq && !rxq_ctrl->started) {
667 		int devx_ev_flag =
668 			  MLX5DV_DEVX_CREATE_EVENT_CHANNEL_FLAGS_OMIT_EV_DATA;
669 
670 		tmpl->devx_channel = mlx5_os_devx_create_event_channel
671 							(priv->sh->cdev->ctx,
672 							 devx_ev_flag);
673 		if (!tmpl->devx_channel) {
674 			rte_errno = errno;
675 			DRV_LOG(ERR, "Failed to create event channel %d.",
676 				rte_errno);
677 			goto error;
678 		}
679 		tmpl->fd = mlx5_os_get_devx_channel_fd(tmpl->devx_channel);
680 	}
681 	/* Create CQ using DevX API. */
682 	ret = mlx5_rxq_create_devx_cq_resources(rxq);
683 	if (ret) {
684 		DRV_LOG(ERR, "Failed to create CQ.");
685 		goto error;
686 	}
687 	rxq_data->delay_drop = priv->config.std_delay_drop;
688 	/* Create RQ using DevX API. */
689 	ret = mlx5_rxq_create_devx_rq_resources(rxq);
690 	if (ret) {
691 		DRV_LOG(ERR, "Port %u Rx queue %u RQ creation failure.",
692 			priv->dev_data->port_id, rxq->idx);
693 		rte_errno = ENOMEM;
694 		goto error;
695 	}
696 	/* Change queue state to ready. */
697 	ret = mlx5_devx_modify_rq(rxq, MLX5_RXQ_MOD_RST2RDY);
698 	if (ret)
699 		goto error;
700 	if (!rxq_data->shared) {
701 		rxq_data->wqes = (void *)(uintptr_t)rxq->devx_rq.wq.umem_buf;
702 		rxq_data->rq_db = (uint32_t *)(uintptr_t)rxq->devx_rq.wq.db_rec;
703 	} else if (!rxq_ctrl->started) {
704 		rxq_data->wqes = (void *)(uintptr_t)tmpl->devx_rmp.wq.umem_buf;
705 		rxq_data->rq_db =
706 				(uint32_t *)(uintptr_t)tmpl->devx_rmp.wq.db_rec;
707 	}
708 	if (!rxq_ctrl->started) {
709 		mlx5_rxq_initialize(rxq_data);
710 		rxq_ctrl->wqn = rxq->devx_rq.rq->id;
711 	}
712 	priv->dev_data->rx_queue_state[rxq->idx] = RTE_ETH_QUEUE_STATE_STARTED;
713 	return 0;
714 error:
715 	ret = rte_errno; /* Save rte_errno before cleanup. */
716 	mlx5_rxq_devx_obj_release(rxq);
717 	rte_errno = ret; /* Restore rte_errno. */
718 	return -rte_errno;
719 }
720 
721 /**
722  * Prepare RQT attribute structure for DevX RQT API.
723  *
724  * @param dev
725  *   Pointer to Ethernet device.
726  * @param log_n
727  *   Log of number of queues in the array.
728  * @param queues
729  *   List of RX queue indices or NULL, in which case
730  *   the attribute will be filled by drop queue ID.
731  * @param queues_n
732  *   Size of @p queues array or 0 if it is NULL.
733  * @param ind_tbl
734  *   DevX indirection table object.
735  *
736  * @return
737  *   The RQT attr object initialized, NULL otherwise and rte_errno is set.
738  */
739 static struct mlx5_devx_rqt_attr *
740 mlx5_devx_ind_table_create_rqt_attr(struct rte_eth_dev *dev,
741 				     const unsigned int log_n,
742 				     const uint16_t *queues,
743 				     const uint32_t queues_n)
744 {
745 	struct mlx5_priv *priv = dev->data->dev_private;
746 	struct mlx5_devx_rqt_attr *rqt_attr = NULL;
747 	const unsigned int rqt_n = 1 << log_n;
748 	unsigned int i, j;
749 
750 	rqt_attr = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*rqt_attr) +
751 			      rqt_n * sizeof(uint32_t), 0, SOCKET_ID_ANY);
752 	if (!rqt_attr) {
753 		DRV_LOG(ERR, "Port %u cannot allocate RQT resources.",
754 			dev->data->port_id);
755 		rte_errno = ENOMEM;
756 		return NULL;
757 	}
758 	rqt_attr->rqt_max_size = priv->sh->dev_cap.ind_table_max_size;
759 	rqt_attr->rqt_actual_size = rqt_n;
760 	if (queues == NULL) {
761 		for (i = 0; i < rqt_n; i++)
762 			rqt_attr->rq_list[i] =
763 					priv->drop_queue.rxq->devx_rq.rq->id;
764 		return rqt_attr;
765 	}
766 	for (i = 0; i != queues_n; ++i) {
767 		if (mlx5_is_external_rxq(dev, queues[i])) {
768 			struct mlx5_external_q *ext_rxq =
769 					mlx5_ext_rxq_get(dev, queues[i]);
770 
771 			rqt_attr->rq_list[i] = ext_rxq->hw_id;
772 		} else {
773 			struct mlx5_rxq_priv *rxq =
774 					mlx5_rxq_get(dev, queues[i]);
775 
776 			MLX5_ASSERT(rxq != NULL);
777 			if (rxq->ctrl->is_hairpin)
778 				rqt_attr->rq_list[i] = rxq->ctrl->obj->rq->id;
779 			else
780 				rqt_attr->rq_list[i] = rxq->devx_rq.rq->id;
781 		}
782 	}
783 	MLX5_ASSERT(i > 0);
784 	for (j = 0; i != rqt_n; ++j, ++i)
785 		rqt_attr->rq_list[i] = rqt_attr->rq_list[j];
786 	return rqt_attr;
787 }
788 
789 /**
790  * Create RQT using DevX API as a filed of indirection table.
791  *
792  * @param dev
793  *   Pointer to Ethernet device.
794  * @param log_n
795  *   Log of number of queues in the array.
796  * @param ind_tbl
797  *   DevX indirection table object.
798  *
799  * @return
800  *   0 on success, a negative errno value otherwise and rte_errno is set.
801  */
802 static int
803 mlx5_devx_ind_table_new(struct rte_eth_dev *dev, const unsigned int log_n,
804 			struct mlx5_ind_table_obj *ind_tbl)
805 {
806 	struct mlx5_priv *priv = dev->data->dev_private;
807 	struct mlx5_devx_rqt_attr *rqt_attr = NULL;
808 	const uint16_t *queues = dev->data->dev_started ? ind_tbl->queues :
809 							  NULL;
810 
811 	MLX5_ASSERT(ind_tbl);
812 	rqt_attr = mlx5_devx_ind_table_create_rqt_attr(dev, log_n, queues,
813 						       ind_tbl->queues_n);
814 	if (!rqt_attr)
815 		return -rte_errno;
816 	ind_tbl->rqt = mlx5_devx_cmd_create_rqt(priv->sh->cdev->ctx, rqt_attr);
817 	mlx5_free(rqt_attr);
818 	if (!ind_tbl->rqt) {
819 		DRV_LOG(ERR, "Port %u cannot create DevX RQT.",
820 			dev->data->port_id);
821 		rte_errno = errno;
822 		return -rte_errno;
823 	}
824 	return 0;
825 }
826 
827 /**
828  * Modify RQT using DevX API as a filed of indirection table.
829  *
830  * @param dev
831  *   Pointer to Ethernet device.
832  * @param log_n
833  *   Log of number of queues in the array.
834  * @param ind_tbl
835  *   DevX indirection table object.
836  *
837  * @return
838  *   0 on success, a negative errno value otherwise and rte_errno is set.
839  */
840 static int
841 mlx5_devx_ind_table_modify(struct rte_eth_dev *dev, const unsigned int log_n,
842 			   const uint16_t *queues, const uint32_t queues_n,
843 			   struct mlx5_ind_table_obj *ind_tbl)
844 {
845 	int ret = 0;
846 	struct mlx5_devx_rqt_attr *rqt_attr = NULL;
847 
848 	MLX5_ASSERT(ind_tbl);
849 	rqt_attr = mlx5_devx_ind_table_create_rqt_attr(dev, log_n,
850 							queues,
851 							queues_n);
852 	if (!rqt_attr)
853 		return -rte_errno;
854 	ret = mlx5_devx_cmd_modify_rqt(ind_tbl->rqt, rqt_attr);
855 	mlx5_free(rqt_attr);
856 	if (ret)
857 		DRV_LOG(ERR, "Port %u cannot modify DevX RQT.",
858 			dev->data->port_id);
859 	return ret;
860 }
861 
862 /**
863  * Destroy the DevX RQT object.
864  *
865  * @param ind_table
866  *   Indirection table to release.
867  */
868 static void
869 mlx5_devx_ind_table_destroy(struct mlx5_ind_table_obj *ind_tbl)
870 {
871 	claim_zero(mlx5_devx_cmd_destroy(ind_tbl->rqt));
872 }
873 
874 /**
875  * Set TIR attribute struct with relevant input values.
876  *
877  * @param[in] dev
878  *   Pointer to Ethernet device.
879  * @param[in] rss_key
880  *   RSS key for the Rx hash queue.
881  * @param[in] hash_fields
882  *   Verbs protocol hash field to make the RSS on.
883  * @param[in] ind_tbl
884  *   Indirection table for TIR. If table queues array is NULL,
885  *   a TIR for drop queue is assumed.
886  * @param[in] tunnel
887  *   Tunnel type.
888  * @param[out] tir_attr
889  *   Parameters structure for TIR creation/modification.
890  *
891  * @return
892  *   The Verbs/DevX object initialised index, 0 otherwise and rte_errno is set.
893  */
894 static void
895 mlx5_devx_tir_attr_set(struct rte_eth_dev *dev, const uint8_t *rss_key,
896 		       uint64_t hash_fields,
897 		       const struct mlx5_ind_table_obj *ind_tbl,
898 		       int tunnel, bool symmetric_hash_function,
899 		       struct mlx5_devx_tir_attr *tir_attr)
900 {
901 	struct mlx5_priv *priv = dev->data->dev_private;
902 	bool is_hairpin;
903 	bool lro = false;
904 	uint32_t i;
905 
906 	/* NULL queues designate drop queue. */
907 	if (ind_tbl->queues == NULL) {
908 		is_hairpin = priv->drop_queue.rxq->ctrl->is_hairpin;
909 	} else if (mlx5_is_external_rxq(dev, ind_tbl->queues[0])) {
910 		/* External RxQ supports neither Hairpin nor LRO. */
911 		is_hairpin = false;
912 	} else {
913 		is_hairpin = mlx5_rxq_is_hairpin(dev, ind_tbl->queues[0]);
914 		lro = true;
915 		/* Enable TIR LRO only if all the queues were configured for. */
916 		for (i = 0; i < ind_tbl->queues_n; ++i) {
917 			struct mlx5_rxq_data *rxq_i =
918 				mlx5_rxq_data_get(dev, ind_tbl->queues[i]);
919 
920 			if (rxq_i != NULL && !rxq_i->lro) {
921 				lro = false;
922 				break;
923 			}
924 		}
925 	}
926 	memset(tir_attr, 0, sizeof(*tir_attr));
927 	tir_attr->disp_type = MLX5_TIRC_DISP_TYPE_INDIRECT;
928 	tir_attr->rx_hash_fn = MLX5_RX_HASH_FN_TOEPLITZ;
929 	tir_attr->tunneled_offload_en = !!tunnel;
930 	tir_attr->rx_hash_symmetric = symmetric_hash_function;
931 	/* If needed, translate hash_fields bitmap to PRM format. */
932 	if (hash_fields) {
933 		struct mlx5_rx_hash_field_select *rx_hash_field_select =
934 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
935 			hash_fields & IBV_RX_HASH_INNER ?
936 				&tir_attr->rx_hash_field_selector_inner :
937 #endif
938 				&tir_attr->rx_hash_field_selector_outer;
939 		/* 1 bit: 0: IPv4, 1: IPv6. */
940 		rx_hash_field_select->l3_prot_type =
941 					!!(hash_fields & MLX5_IPV6_IBV_RX_HASH);
942 		/* 1 bit: 0: TCP, 1: UDP. */
943 		rx_hash_field_select->l4_prot_type =
944 					!!(hash_fields & MLX5_UDP_IBV_RX_HASH);
945 		/* Bitmask which sets which fields to use in RX Hash. */
946 		rx_hash_field_select->selected_fields =
947 			((!!(hash_fields & MLX5_L3_SRC_IBV_RX_HASH)) <<
948 			 MLX5_RX_HASH_FIELD_SELECT_SELECTED_FIELDS_SRC_IP) |
949 			(!!(hash_fields & MLX5_L3_DST_IBV_RX_HASH)) <<
950 			 MLX5_RX_HASH_FIELD_SELECT_SELECTED_FIELDS_DST_IP |
951 			(!!(hash_fields & MLX5_L4_SRC_IBV_RX_HASH)) <<
952 			 MLX5_RX_HASH_FIELD_SELECT_SELECTED_FIELDS_L4_SPORT |
953 			(!!(hash_fields & MLX5_L4_DST_IBV_RX_HASH)) <<
954 			 MLX5_RX_HASH_FIELD_SELECT_SELECTED_FIELDS_L4_DPORT |
955 			(!!(hash_fields & IBV_RX_HASH_IPSEC_SPI)) <<
956 			 MLX5_RX_HASH_FIELD_SELECT_SELECTED_FIELDS_IPSEC_SPI;
957 	}
958 	if (is_hairpin)
959 		tir_attr->transport_domain = priv->sh->td->id;
960 	else
961 		tir_attr->transport_domain = priv->sh->tdn;
962 	memcpy(tir_attr->rx_hash_toeplitz_key, rss_key, MLX5_RSS_HASH_KEY_LEN);
963 	tir_attr->indirect_table = ind_tbl->rqt->id;
964 	if (dev->data->dev_conf.lpbk_mode)
965 		tir_attr->self_lb_block = MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST;
966 	if (lro) {
967 		MLX5_ASSERT(priv->sh->config.lro_allowed);
968 		tir_attr->lro_timeout_period_usecs = priv->config.lro_timeout;
969 		tir_attr->lro_max_msg_sz =
970 			priv->max_lro_msg_size / MLX5_LRO_SEG_CHUNK_SIZE;
971 		tir_attr->lro_enable_mask =
972 				MLX5_TIRC_LRO_ENABLE_MASK_IPV4_LRO |
973 				MLX5_TIRC_LRO_ENABLE_MASK_IPV6_LRO;
974 	}
975 }
976 
977 /**
978  * Create an Rx Hash queue.
979  *
980  * @param dev
981  *   Pointer to Ethernet device.
982  * @param hrxq
983  *   Pointer to Rx Hash queue.
984  * @param tunnel
985  *   Tunnel type.
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_new(struct rte_eth_dev *dev, struct mlx5_hrxq *hrxq,
992 		   int tunnel __rte_unused)
993 {
994 	struct mlx5_priv *priv = dev->data->dev_private;
995 	struct mlx5_devx_tir_attr tir_attr = {0};
996 	int err;
997 
998 	mlx5_devx_tir_attr_set(dev, hrxq->rss_key, hrxq->hash_fields,
999 			       hrxq->ind_table, tunnel, hrxq->symmetric_hash_function,
1000 			       &tir_attr);
1001 	hrxq->tir = mlx5_devx_cmd_create_tir(priv->sh->cdev->ctx, &tir_attr);
1002 	if (!hrxq->tir) {
1003 		DRV_LOG(ERR, "Port %u cannot create DevX TIR.",
1004 			dev->data->port_id);
1005 		rte_errno = errno;
1006 		goto error;
1007 	}
1008 #if defined(HAVE_IBV_FLOW_DV_SUPPORT) || !defined(HAVE_INFINIBAND_VERBS_H)
1009 #ifdef HAVE_MLX5_HWS_SUPPORT
1010 	if (hrxq->hws_flags) {
1011 		hrxq->action = mlx5dr_action_create_dest_tir
1012 			(priv->dr_ctx,
1013 			 (struct mlx5dr_devx_obj *)hrxq->tir, hrxq->hws_flags, true);
1014 		if (!hrxq->action)
1015 			goto error;
1016 		return 0;
1017 	}
1018 #endif
1019 	if (mlx5_flow_os_create_flow_action_dest_devx_tir(hrxq->tir,
1020 							  &hrxq->action)) {
1021 		rte_errno = errno;
1022 		goto error;
1023 	}
1024 #endif
1025 	return 0;
1026 error:
1027 	err = rte_errno; /* Save rte_errno before cleanup. */
1028 	if (hrxq->tir)
1029 		claim_zero(mlx5_devx_cmd_destroy(hrxq->tir));
1030 	rte_errno = err; /* Restore rte_errno. */
1031 	return -rte_errno;
1032 }
1033 
1034 /**
1035  * Destroy a DevX TIR object.
1036  *
1037  * @param hrxq
1038  *   Hash Rx queue to release its tir.
1039  */
1040 static void
1041 mlx5_devx_tir_destroy(struct mlx5_hrxq *hrxq)
1042 {
1043 	claim_zero(mlx5_devx_cmd_destroy(hrxq->tir));
1044 }
1045 
1046 /**
1047  * Modify an Rx Hash queue configuration.
1048  *
1049  * @param dev
1050  *   Pointer to Ethernet device.
1051  * @param hrxq
1052  *   Hash Rx queue to modify.
1053  * @param rss_key
1054  *   RSS key for the Rx hash queue.
1055  * @param hash_fields
1056  *   Verbs protocol hash field to make the RSS on.
1057  * @param[in] ind_tbl
1058  *   Indirection table for TIR.
1059  *
1060  * @return
1061  *   0 on success, a negative errno value otherwise and rte_errno is set.
1062  */
1063 static int
1064 mlx5_devx_hrxq_modify(struct rte_eth_dev *dev, struct mlx5_hrxq *hrxq,
1065 		       const uint8_t *rss_key,
1066 		       uint64_t hash_fields,
1067 		       bool symmetric_hash_function,
1068 		       const struct mlx5_ind_table_obj *ind_tbl)
1069 {
1070 	struct mlx5_devx_modify_tir_attr modify_tir = {0};
1071 
1072 	/*
1073 	 * untested for modification fields:
1074 	 * - rx_hash_fn set hard-coded in hrxq_new(),
1075 	 * - lro_xxx not set after rxq setup
1076 	 */
1077 	if (ind_tbl != hrxq->ind_table)
1078 		modify_tir.modify_bitmask |=
1079 			MLX5_MODIFY_TIR_IN_MODIFY_BITMASK_INDIRECT_TABLE;
1080 	if (hash_fields != hrxq->hash_fields ||
1081 			symmetric_hash_function != hrxq->symmetric_hash_function ||
1082 			memcmp(hrxq->rss_key, rss_key, MLX5_RSS_HASH_KEY_LEN))
1083 		modify_tir.modify_bitmask |=
1084 			MLX5_MODIFY_TIR_IN_MODIFY_BITMASK_HASH;
1085 	mlx5_devx_tir_attr_set(dev, rss_key, hash_fields, ind_tbl,
1086 			       0, /* N/A - tunnel modification unsupported */
1087 			       symmetric_hash_function,
1088 			       &modify_tir.tir);
1089 	modify_tir.tirn = hrxq->tir->id;
1090 	if (mlx5_devx_cmd_modify_tir(hrxq->tir, &modify_tir)) {
1091 		DRV_LOG(ERR, "port %u cannot modify DevX TIR",
1092 			dev->data->port_id);
1093 		rte_errno = errno;
1094 		return -rte_errno;
1095 	}
1096 	return 0;
1097 }
1098 
1099 /**
1100  * Create a DevX drop Rx queue.
1101  *
1102  * @param dev
1103  *   Pointer to Ethernet device.
1104  *
1105  * @return
1106  *   0 on success, a negative errno value otherwise and rte_errno is set.
1107  */
1108 static int
1109 mlx5_rxq_devx_obj_drop_create(struct rte_eth_dev *dev)
1110 {
1111 	struct mlx5_priv *priv = dev->data->dev_private;
1112 	int socket_id = dev->device->numa_node;
1113 	struct mlx5_rxq_priv *rxq;
1114 	struct mlx5_rxq_ctrl *rxq_ctrl = NULL;
1115 	struct mlx5_rxq_obj *rxq_obj = NULL;
1116 	int ret;
1117 
1118 	/*
1119 	 * Initialize dummy control structures.
1120 	 * They are required to hold pointers for cleanup
1121 	 * and are only accessible via drop queue DevX objects.
1122 	 */
1123 	rxq = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*rxq), 0, socket_id);
1124 	if (rxq == NULL) {
1125 		DRV_LOG(ERR, "Port %u could not allocate drop queue private",
1126 			dev->data->port_id);
1127 		rte_errno = ENOMEM;
1128 		goto error;
1129 	}
1130 	rxq_ctrl = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*rxq_ctrl),
1131 			       0, socket_id);
1132 	if (rxq_ctrl == NULL) {
1133 		DRV_LOG(ERR, "Port %u could not allocate drop queue control",
1134 			dev->data->port_id);
1135 		rte_errno = ENOMEM;
1136 		goto error;
1137 	}
1138 	rxq_obj = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*rxq_obj), 0, socket_id);
1139 	if (rxq_obj == NULL) {
1140 		DRV_LOG(ERR, "Port %u could not allocate drop queue object",
1141 			dev->data->port_id);
1142 		rte_errno = ENOMEM;
1143 		goto error;
1144 	}
1145 	/* set the CPU socket ID where the rxq_ctrl was allocated */
1146 	rxq_ctrl->socket = socket_id;
1147 	rxq_obj->rxq_ctrl = rxq_ctrl;
1148 	rxq_ctrl->is_hairpin = false;
1149 	rxq_ctrl->sh = priv->sh;
1150 	rxq_ctrl->obj = rxq_obj;
1151 	rxq->ctrl = rxq_ctrl;
1152 	rxq->priv = priv;
1153 	LIST_INSERT_HEAD(&rxq_ctrl->owners, rxq, owner_entry);
1154 	/* Create CQ using DevX API. */
1155 	ret = mlx5_rxq_create_devx_cq_resources(rxq);
1156 	if (ret != 0) {
1157 		DRV_LOG(ERR, "Port %u drop queue CQ creation failed.",
1158 			dev->data->port_id);
1159 		goto error;
1160 	}
1161 	rxq_ctrl->rxq.delay_drop = 0;
1162 	/* Create RQ using DevX API. */
1163 	ret = mlx5_rxq_create_devx_rq_resources(rxq);
1164 	if (ret != 0) {
1165 		DRV_LOG(ERR, "Port %u drop queue RQ creation failed.",
1166 			dev->data->port_id);
1167 		rte_errno = ENOMEM;
1168 		goto error;
1169 	}
1170 	/* Change queue state to ready. */
1171 	ret = mlx5_devx_modify_rq(rxq, MLX5_RXQ_MOD_RST2RDY);
1172 	if (ret != 0)
1173 		goto error;
1174 	/* Initialize drop queue. */
1175 	priv->drop_queue.rxq = rxq;
1176 	return 0;
1177 error:
1178 	ret = rte_errno; /* Save rte_errno before cleanup. */
1179 	if (rxq != NULL && rxq->devx_rq.rq != NULL)
1180 		mlx5_devx_rq_destroy(&rxq->devx_rq);
1181 	if (rxq_obj != NULL) {
1182 		if (rxq_obj->cq_obj.cq != NULL)
1183 			mlx5_devx_cq_destroy(&rxq_obj->cq_obj);
1184 		if (rxq_obj->devx_channel)
1185 			mlx5_os_devx_destroy_event_channel
1186 							(rxq_obj->devx_channel);
1187 		mlx5_free(rxq_obj);
1188 	}
1189 	if (rxq_ctrl != NULL)
1190 		mlx5_free(rxq_ctrl);
1191 	if (rxq != NULL)
1192 		mlx5_free(rxq);
1193 	rte_errno = ret; /* Restore rte_errno. */
1194 	return -rte_errno;
1195 }
1196 
1197 /**
1198  * Release drop Rx queue resources.
1199  *
1200  * @param dev
1201  *   Pointer to Ethernet device.
1202  */
1203 static void
1204 mlx5_rxq_devx_obj_drop_release(struct rte_eth_dev *dev)
1205 {
1206 	struct mlx5_priv *priv = dev->data->dev_private;
1207 	struct mlx5_rxq_priv *rxq = priv->drop_queue.rxq;
1208 	struct mlx5_rxq_ctrl *rxq_ctrl = rxq->ctrl;
1209 
1210 	mlx5_rxq_devx_obj_release(rxq);
1211 	mlx5_free(rxq_ctrl->obj);
1212 	mlx5_free(rxq_ctrl);
1213 	mlx5_free(rxq);
1214 	priv->drop_queue.rxq = NULL;
1215 }
1216 
1217 /**
1218  * Release a drop hash Rx queue.
1219  *
1220  * @param dev
1221  *   Pointer to Ethernet device.
1222  */
1223 static void
1224 mlx5_devx_drop_action_destroy(struct rte_eth_dev *dev)
1225 {
1226 	struct mlx5_priv *priv = dev->data->dev_private;
1227 	struct mlx5_hrxq *hrxq = priv->drop_queue.hrxq;
1228 
1229 #if defined(HAVE_IBV_FLOW_DV_SUPPORT) || !defined(HAVE_INFINIBAND_VERBS_H)
1230 	if (hrxq->action != NULL)
1231 		mlx5_flow_os_destroy_flow_action(hrxq->action);
1232 #endif
1233 	if (hrxq->tir != NULL)
1234 		mlx5_devx_tir_destroy(hrxq);
1235 	if (hrxq->ind_table->ind_table != NULL)
1236 		mlx5_devx_ind_table_destroy(hrxq->ind_table);
1237 	if (priv->drop_queue.rxq->devx_rq.rq != NULL)
1238 		mlx5_rxq_devx_obj_drop_release(dev);
1239 }
1240 
1241 /**
1242  * Create a DevX drop action for Rx Hash queue.
1243  *
1244  * @param dev
1245  *   Pointer to Ethernet device.
1246  *
1247  * @return
1248  *   0 on success, a negative errno value otherwise and rte_errno is set.
1249  */
1250 static int
1251 mlx5_devx_drop_action_create(struct rte_eth_dev *dev)
1252 {
1253 	struct mlx5_priv *priv = dev->data->dev_private;
1254 	struct mlx5_hrxq *hrxq = priv->drop_queue.hrxq;
1255 	int ret;
1256 
1257 	ret = mlx5_rxq_devx_obj_drop_create(dev);
1258 	if (ret != 0) {
1259 		DRV_LOG(ERR, "Cannot create drop RX queue");
1260 		return ret;
1261 	}
1262 	if (priv->sh->config.dv_flow_en == 2)
1263 		return 0;
1264 	/* hrxq->ind_table queues are NULL, drop RX queue ID will be used */
1265 	ret = mlx5_devx_ind_table_new(dev, 0, hrxq->ind_table);
1266 	if (ret != 0) {
1267 		DRV_LOG(ERR, "Cannot create drop hash RX queue indirection table");
1268 		goto error;
1269 	}
1270 	ret = mlx5_devx_hrxq_new(dev, hrxq, /* tunnel */ false);
1271 	if (ret != 0) {
1272 		DRV_LOG(ERR, "Cannot create drop hash RX queue");
1273 		goto error;
1274 	}
1275 	return 0;
1276 error:
1277 	mlx5_devx_drop_action_destroy(dev);
1278 	return ret;
1279 }
1280 
1281 /**
1282  * Select TXQ TIS number.
1283  *
1284  * @param dev
1285  *   Pointer to Ethernet device.
1286  * @param queue_idx
1287  *   Queue index in DPDK Tx queue array.
1288  *
1289  * @return
1290  *   > 0 on success, a negative errno value otherwise.
1291  */
1292 static uint32_t
1293 mlx5_get_txq_tis_num(struct rte_eth_dev *dev, uint16_t queue_idx)
1294 {
1295 	struct mlx5_priv *priv = dev->data->dev_private;
1296 	struct mlx5_txq_data *txq_data = (*priv->txqs)[queue_idx];
1297 	int tis_idx = 0;
1298 
1299 	if (priv->sh->bond.n_port) {
1300 		if (txq_data->tx_aggr_affinity) {
1301 			tis_idx = txq_data->tx_aggr_affinity;
1302 		} else if (priv->sh->lag.affinity_mode == MLX5_LAG_MODE_TIS) {
1303 			tis_idx = (priv->lag_affinity_idx + queue_idx) %
1304 				priv->sh->bond.n_port + 1;
1305 			DRV_LOG(INFO, "port %d txq %d gets affinity %d and maps to PF %d.",
1306 				dev->data->port_id, queue_idx, tis_idx,
1307 				priv->sh->lag.tx_remap_affinity[tis_idx - 1]);
1308 		}
1309 	}
1310 	MLX5_ASSERT(priv->sh->tis[tis_idx]);
1311 	return priv->sh->tis[tis_idx]->id;
1312 }
1313 
1314 /**
1315  * Create the Tx hairpin queue object.
1316  *
1317  * @param dev
1318  *   Pointer to Ethernet device.
1319  * @param idx
1320  *   Queue index in DPDK Tx queue array.
1321  *
1322  * @return
1323  *   0 on success, a negative errno value otherwise and rte_errno is set.
1324  */
1325 static int
1326 mlx5_txq_obj_hairpin_new(struct rte_eth_dev *dev, uint16_t idx)
1327 {
1328 	struct mlx5_priv *priv = dev->data->dev_private;
1329 	struct mlx5_hca_attr *hca_attr = &priv->sh->cdev->config.hca_attr;
1330 	struct mlx5_txq_data *txq_data = (*priv->txqs)[idx];
1331 	struct mlx5_txq_ctrl *txq_ctrl =
1332 		container_of(txq_data, struct mlx5_txq_ctrl, txq);
1333 	struct mlx5_devx_create_sq_attr dev_mem_attr = { 0 };
1334 	struct mlx5_devx_create_sq_attr host_mem_attr = { 0 };
1335 	struct mlx5_txq_obj *tmpl = txq_ctrl->obj;
1336 	void *umem_buf = NULL;
1337 	void *umem_obj = NULL;
1338 	uint32_t max_wq_data;
1339 
1340 	MLX5_ASSERT(txq_data);
1341 	MLX5_ASSERT(tmpl);
1342 	tmpl->txq_ctrl = txq_ctrl;
1343 	dev_mem_attr.hairpin = 1;
1344 	dev_mem_attr.tis_lst_sz = 1;
1345 	dev_mem_attr.tis_num = mlx5_get_txq_tis_num(dev, idx);
1346 	max_wq_data =
1347 		priv->sh->cdev->config.hca_attr.log_max_hairpin_wq_data_sz;
1348 	/* Jumbo frames > 9KB should be supported, and more packets. */
1349 	if (priv->config.log_hp_size != (uint32_t)MLX5_ARG_UNSET) {
1350 		if (priv->config.log_hp_size > max_wq_data) {
1351 			DRV_LOG(ERR, "Total data size %u power of 2 is "
1352 				"too large for hairpin.",
1353 				priv->config.log_hp_size);
1354 			rte_errno = ERANGE;
1355 			return -rte_errno;
1356 		}
1357 		dev_mem_attr.wq_attr.log_hairpin_data_sz = priv->config.log_hp_size;
1358 	} else {
1359 		dev_mem_attr.wq_attr.log_hairpin_data_sz =
1360 				(max_wq_data < MLX5_HAIRPIN_JUMBO_LOG_SIZE) ?
1361 				 max_wq_data : MLX5_HAIRPIN_JUMBO_LOG_SIZE;
1362 	}
1363 	/* Set the packets number to the maximum value for performance. */
1364 	dev_mem_attr.wq_attr.log_hairpin_num_packets =
1365 			dev_mem_attr.wq_attr.log_hairpin_data_sz -
1366 			MLX5_HAIRPIN_QUEUE_STRIDE;
1367 	dev_mem_attr.hairpin_wq_buffer_type = MLX5_SQC_HAIRPIN_WQ_BUFFER_TYPE_INTERNAL_BUFFER;
1368 	if (txq_ctrl->hairpin_conf.use_rte_memory) {
1369 		uint32_t umem_size;
1370 		uint32_t umem_dbrec;
1371 		size_t alignment = MLX5_WQE_BUF_ALIGNMENT;
1372 
1373 		if (alignment == (size_t)-1) {
1374 			DRV_LOG(ERR, "Failed to get WQE buf alignment.");
1375 			rte_errno = ENOMEM;
1376 			return -rte_errno;
1377 		}
1378 		/*
1379 		 * It is assumed that configuration is verified against capabilities
1380 		 * during queue setup.
1381 		 */
1382 		MLX5_ASSERT(hca_attr->hairpin_sq_wq_in_host_mem);
1383 		MLX5_ASSERT(hca_attr->hairpin_sq_wqe_bb_size > 0);
1384 		rte_memcpy(&host_mem_attr, &dev_mem_attr, sizeof(host_mem_attr));
1385 		umem_size = MLX5_WQE_SIZE *
1386 			RTE_BIT32(host_mem_attr.wq_attr.log_hairpin_num_packets);
1387 		umem_dbrec = RTE_ALIGN(umem_size, MLX5_DBR_SIZE);
1388 		umem_size += MLX5_DBR_SIZE;
1389 		umem_buf = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO, umem_size,
1390 				       alignment, priv->sh->numa_node);
1391 		if (umem_buf == NULL && txq_ctrl->hairpin_conf.force_memory) {
1392 			DRV_LOG(ERR, "Failed to allocate memory for hairpin TX queue");
1393 			rte_errno = ENOMEM;
1394 			return -rte_errno;
1395 		} else if (umem_buf == NULL && !txq_ctrl->hairpin_conf.force_memory) {
1396 			DRV_LOG(WARNING, "Failed to allocate memory for hairpin TX queue."
1397 					 " Falling back to TX queue located on the device.");
1398 			goto create_sq_on_device;
1399 		}
1400 		umem_obj = mlx5_os_umem_reg(priv->sh->cdev->ctx,
1401 					    (void *)(uintptr_t)umem_buf,
1402 					    umem_size,
1403 					    IBV_ACCESS_LOCAL_WRITE);
1404 		if (umem_obj == NULL && txq_ctrl->hairpin_conf.force_memory) {
1405 			DRV_LOG(ERR, "Failed to register UMEM for hairpin TX queue");
1406 			mlx5_free(umem_buf);
1407 			return -rte_errno;
1408 		} else if (umem_obj == NULL && !txq_ctrl->hairpin_conf.force_memory) {
1409 			DRV_LOG(WARNING, "Failed to register UMEM for hairpin TX queue."
1410 					 " Falling back to TX queue located on the device.");
1411 			rte_errno = 0;
1412 			mlx5_free(umem_buf);
1413 			goto create_sq_on_device;
1414 		}
1415 		host_mem_attr.wq_attr.wq_type = MLX5_WQ_TYPE_CYCLIC;
1416 		host_mem_attr.wq_attr.wq_umem_valid = 1;
1417 		host_mem_attr.wq_attr.wq_umem_id = mlx5_os_get_umem_id(umem_obj);
1418 		host_mem_attr.wq_attr.wq_umem_offset = 0;
1419 		host_mem_attr.wq_attr.dbr_umem_valid = 1;
1420 		host_mem_attr.wq_attr.dbr_umem_id = host_mem_attr.wq_attr.wq_umem_id;
1421 		host_mem_attr.wq_attr.dbr_addr = umem_dbrec;
1422 		host_mem_attr.wq_attr.log_wq_stride = rte_log2_u32(MLX5_WQE_SIZE);
1423 		host_mem_attr.wq_attr.log_wq_sz =
1424 				host_mem_attr.wq_attr.log_hairpin_num_packets *
1425 				hca_attr->hairpin_sq_wqe_bb_size;
1426 		host_mem_attr.wq_attr.log_wq_pg_sz = MLX5_LOG_PAGE_SIZE;
1427 		host_mem_attr.hairpin_wq_buffer_type = MLX5_SQC_HAIRPIN_WQ_BUFFER_TYPE_HOST_MEMORY;
1428 		tmpl->sq = mlx5_devx_cmd_create_sq(priv->sh->cdev->ctx, &host_mem_attr);
1429 		if (!tmpl->sq && txq_ctrl->hairpin_conf.force_memory) {
1430 			DRV_LOG(ERR,
1431 				"Port %u tx hairpin queue %u can't create SQ object.",
1432 				dev->data->port_id, idx);
1433 			claim_zero(mlx5_os_umem_dereg(umem_obj));
1434 			mlx5_free(umem_buf);
1435 			return -rte_errno;
1436 		} else if (!tmpl->sq && !txq_ctrl->hairpin_conf.force_memory) {
1437 			DRV_LOG(WARNING,
1438 				"Port %u tx hairpin queue %u failed to allocate SQ object"
1439 				" using host memory. Falling back to TX queue located"
1440 				" on the device",
1441 				dev->data->port_id, idx);
1442 			rte_errno = 0;
1443 			claim_zero(mlx5_os_umem_dereg(umem_obj));
1444 			mlx5_free(umem_buf);
1445 			goto create_sq_on_device;
1446 		}
1447 		tmpl->umem_buf_wq_buffer = umem_buf;
1448 		tmpl->umem_obj_wq_buffer = umem_obj;
1449 		return 0;
1450 	}
1451 
1452 create_sq_on_device:
1453 	tmpl->sq = mlx5_devx_cmd_create_sq(priv->sh->cdev->ctx, &dev_mem_attr);
1454 	if (!tmpl->sq) {
1455 		DRV_LOG(ERR,
1456 			"Port %u tx hairpin queue %u can't create SQ object.",
1457 			dev->data->port_id, idx);
1458 		rte_errno = errno;
1459 		return -rte_errno;
1460 	}
1461 	return 0;
1462 }
1463 
1464 #if defined(HAVE_MLX5DV_DEVX_UAR_OFFSET) || !defined(HAVE_INFINIBAND_VERBS_H)
1465 /**
1466  * Destroy the Tx queue DevX object.
1467  *
1468  * @param txq_obj
1469  *   Txq object to destroy.
1470  */
1471 static void
1472 mlx5_txq_release_devx_resources(struct mlx5_txq_obj *txq_obj)
1473 {
1474 	mlx5_devx_sq_destroy(&txq_obj->sq_obj);
1475 	memset(&txq_obj->sq_obj, 0, sizeof(txq_obj->sq_obj));
1476 	mlx5_devx_cq_destroy(&txq_obj->cq_obj);
1477 	memset(&txq_obj->cq_obj, 0, sizeof(txq_obj->cq_obj));
1478 }
1479 
1480 /**
1481  * Create a SQ object and its resources using DevX.
1482  *
1483  * @param dev
1484  *   Pointer to Ethernet device.
1485  * @param idx
1486  *   Queue index in DPDK Tx queue array.
1487  * @param[in] log_desc_n
1488  *   Log of number of descriptors in queue.
1489  *
1490  * @return
1491  *   0 on success, a negative errno value otherwise and rte_errno is set.
1492  */
1493 static int
1494 mlx5_txq_create_devx_sq_resources(struct rte_eth_dev *dev, uint16_t idx,
1495 				  uint16_t log_desc_n)
1496 {
1497 	struct mlx5_priv *priv = dev->data->dev_private;
1498 	struct mlx5_common_device *cdev = priv->sh->cdev;
1499 	struct mlx5_uar *uar = &priv->sh->tx_uar;
1500 	struct mlx5_txq_data *txq_data = (*priv->txqs)[idx];
1501 	struct mlx5_txq_ctrl *txq_ctrl =
1502 			container_of(txq_data, struct mlx5_txq_ctrl, txq);
1503 	struct mlx5_txq_obj *txq_obj = txq_ctrl->obj;
1504 	struct mlx5_devx_create_sq_attr sq_attr = {
1505 		.flush_in_error_en = 1,
1506 		.allow_multi_pkt_send_wqe = !!priv->config.mps,
1507 		.min_wqe_inline_mode = cdev->config.hca_attr.vport_inline_mode,
1508 		.allow_swp = !!priv->sh->dev_cap.swp,
1509 		.cqn = txq_obj->cq_obj.cq->id,
1510 		.tis_lst_sz = 1,
1511 		.wq_attr = (struct mlx5_devx_wq_attr){
1512 			.pd = cdev->pdn,
1513 			.uar_page = mlx5_os_get_devx_uar_page_id(uar->obj),
1514 		},
1515 		.ts_format =
1516 			mlx5_ts_format_conv(cdev->config.hca_attr.sq_ts_format),
1517 		.tis_num = mlx5_get_txq_tis_num(dev, idx),
1518 	};
1519 
1520 	/* Create Send Queue object with DevX. */
1521 	return mlx5_devx_sq_create(cdev->ctx, &txq_obj->sq_obj,
1522 				   log_desc_n, &sq_attr, priv->sh->numa_node);
1523 }
1524 #endif
1525 
1526 /**
1527  * Create the Tx queue DevX object.
1528  *
1529  * @param dev
1530  *   Pointer to Ethernet device.
1531  * @param idx
1532  *   Queue index in DPDK Tx queue array.
1533  *
1534  * @return
1535  *   0 on success, a negative errno value otherwise and rte_errno is set.
1536  */
1537 int
1538 mlx5_txq_devx_obj_new(struct rte_eth_dev *dev, uint16_t idx)
1539 {
1540 	struct mlx5_priv *priv = dev->data->dev_private;
1541 	struct mlx5_txq_data *txq_data = (*priv->txqs)[idx];
1542 	struct mlx5_txq_ctrl *txq_ctrl =
1543 			container_of(txq_data, struct mlx5_txq_ctrl, txq);
1544 
1545 	if (txq_ctrl->is_hairpin)
1546 		return mlx5_txq_obj_hairpin_new(dev, idx);
1547 #if !defined(HAVE_MLX5DV_DEVX_UAR_OFFSET) && defined(HAVE_INFINIBAND_VERBS_H)
1548 	DRV_LOG(ERR, "Port %u Tx queue %u cannot create with DevX, no UAR.",
1549 		     dev->data->port_id, idx);
1550 	rte_errno = ENOMEM;
1551 	return -rte_errno;
1552 #else
1553 	struct mlx5_proc_priv *ppriv = MLX5_PROC_PRIV(PORT_ID(priv));
1554 	struct mlx5_dev_ctx_shared *sh = priv->sh;
1555 	struct mlx5_txq_obj *txq_obj = txq_ctrl->obj;
1556 	struct mlx5_devx_cq_attr cq_attr = {
1557 		.uar_page_id = mlx5_os_get_devx_uar_page_id(sh->tx_uar.obj),
1558 	};
1559 	uint32_t cqe_n, log_desc_n;
1560 	uint32_t wqe_n, wqe_size;
1561 	int ret = 0;
1562 
1563 	MLX5_ASSERT(txq_data);
1564 	MLX5_ASSERT(txq_obj);
1565 	MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
1566 	MLX5_ASSERT(ppriv);
1567 	txq_obj->txq_ctrl = txq_ctrl;
1568 	txq_obj->dev = dev;
1569 	if (__rte_trace_point_fp_is_enabled() &&
1570 	    txq_data->offloads & RTE_ETH_TX_OFFLOAD_SEND_ON_TIMESTAMP)
1571 		cqe_n = UINT16_MAX / 2 - 1;
1572 	else
1573 		cqe_n = (1UL << txq_data->elts_n) / MLX5_TX_COMP_THRESH +
1574 			1 + MLX5_TX_COMP_THRESH_INLINE_DIV;
1575 	log_desc_n = log2above(cqe_n);
1576 	cqe_n = 1UL << log_desc_n;
1577 	if (cqe_n > UINT16_MAX) {
1578 		DRV_LOG(ERR, "Port %u Tx queue %u requests to many CQEs %u.",
1579 			dev->data->port_id, txq_data->idx, cqe_n);
1580 		rte_errno = EINVAL;
1581 		return 0;
1582 	}
1583 	/* Create completion queue object with DevX. */
1584 	ret = mlx5_devx_cq_create(sh->cdev->ctx, &txq_obj->cq_obj, log_desc_n,
1585 				  &cq_attr, priv->sh->numa_node);
1586 	if (ret) {
1587 		DRV_LOG(ERR, "Port %u Tx queue %u CQ creation failure.",
1588 			dev->data->port_id, idx);
1589 		goto error;
1590 	}
1591 	txq_data->cqe_n = log_desc_n;
1592 	txq_data->cqe_s = cqe_n;
1593 	txq_data->cqe_m = txq_data->cqe_s - 1;
1594 	txq_data->cqes = txq_obj->cq_obj.cqes;
1595 	txq_data->cq_ci = 0;
1596 	txq_data->cq_pi = 0;
1597 	txq_data->cq_db = txq_obj->cq_obj.db_rec;
1598 	*txq_data->cq_db = 0;
1599 	/*
1600 	 * Adjust the amount of WQEs depending on inline settings.
1601 	 * The number of descriptors should be enough to handle
1602 	 * the specified number of packets. If queue is being created
1603 	 * with Verbs the rdma-core does queue size adjustment
1604 	 * internally in the mlx5_calc_sq_size(), we do the same
1605 	 * for the queue being created with DevX at this point.
1606 	 */
1607 	wqe_size = txq_data->tso_en ?
1608 		   RTE_ALIGN(txq_ctrl->max_tso_header, MLX5_WSEG_SIZE) : 0;
1609 	wqe_size += sizeof(struct mlx5_wqe_cseg) +
1610 		    sizeof(struct mlx5_wqe_eseg) +
1611 		    sizeof(struct mlx5_wqe_dseg);
1612 	if (txq_data->inlen_send)
1613 		wqe_size = RTE_MAX(wqe_size, sizeof(struct mlx5_wqe_cseg) +
1614 					     sizeof(struct mlx5_wqe_eseg) +
1615 					     RTE_ALIGN(txq_data->inlen_send +
1616 						       sizeof(uint32_t),
1617 						       MLX5_WSEG_SIZE));
1618 	wqe_size = RTE_ALIGN(wqe_size, MLX5_WQE_SIZE) / MLX5_WQE_SIZE;
1619 	/* Create Send Queue object with DevX. */
1620 	wqe_n = RTE_MIN((1UL << txq_data->elts_n) * wqe_size,
1621 			(uint32_t)priv->sh->dev_cap.max_qp_wr);
1622 	log_desc_n = log2above(wqe_n);
1623 	ret = mlx5_txq_create_devx_sq_resources(dev, idx, log_desc_n);
1624 	if (ret) {
1625 		DRV_LOG(ERR, "Port %u Tx queue %u SQ creation failure.",
1626 			dev->data->port_id, idx);
1627 		rte_errno = errno;
1628 		goto error;
1629 	}
1630 	/* Create the Work Queue. */
1631 	txq_data->wqe_n = log_desc_n;
1632 	txq_data->wqe_s = 1 << txq_data->wqe_n;
1633 	txq_data->wqe_m = txq_data->wqe_s - 1;
1634 	txq_data->wqes = (struct mlx5_wqe *)(uintptr_t)txq_obj->sq_obj.wqes;
1635 	txq_data->wqes_end = txq_data->wqes + txq_data->wqe_s;
1636 	txq_data->wqe_ci = 0;
1637 	txq_data->wqe_pi = 0;
1638 	txq_data->wqe_comp = 0;
1639 	txq_data->wqe_thres = txq_data->wqe_s / MLX5_TX_COMP_THRESH_INLINE_DIV;
1640 	txq_data->qp_db = &txq_obj->sq_obj.db_rec[MLX5_SND_DBR];
1641 	*txq_data->qp_db = 0;
1642 	txq_data->qp_num_8s = txq_obj->sq_obj.sq->id << 8;
1643 	txq_data->db_heu = sh->cdev->config.dbnc == MLX5_SQ_DB_HEURISTIC;
1644 	txq_data->db_nc = sh->tx_uar.dbnc;
1645 	txq_data->wait_on_time = !!(!sh->config.tx_pp &&
1646 				    sh->cdev->config.hca_attr.wait_on_time);
1647 	/* Change Send Queue state to Ready-to-Send. */
1648 	ret = mlx5_txq_devx_modify(txq_obj, MLX5_TXQ_MOD_RST2RDY, 0);
1649 	if (ret) {
1650 		rte_errno = errno;
1651 		DRV_LOG(ERR,
1652 			"Port %u Tx queue %u SQ state to SQC_STATE_RDY failed.",
1653 			dev->data->port_id, idx);
1654 		goto error;
1655 	}
1656 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
1657 	/*
1658 	 * If using DevX need to query and store TIS transport domain value.
1659 	 * This is done once per port.
1660 	 * Will use this value on Rx, when creating matching TIR.
1661 	 */
1662 	if (!priv->sh->tdn)
1663 		priv->sh->tdn = priv->sh->td->id;
1664 #endif
1665 	txq_ctrl->uar_mmap_offset =
1666 			mlx5_os_get_devx_uar_mmap_offset(sh->tx_uar.obj);
1667 	ppriv->uar_table[txq_data->idx] = sh->tx_uar.bf_db;
1668 	dev->data->tx_queue_state[idx] = RTE_ETH_QUEUE_STATE_STARTED;
1669 	return 0;
1670 error:
1671 	ret = rte_errno; /* Save rte_errno before cleanup. */
1672 	mlx5_txq_release_devx_resources(txq_obj);
1673 	rte_errno = ret; /* Restore rte_errno. */
1674 	return -rte_errno;
1675 #endif
1676 }
1677 
1678 /**
1679  * Release an Tx DevX queue object.
1680  *
1681  * @param txq_obj
1682  *   DevX Tx queue object.
1683  */
1684 void
1685 mlx5_txq_devx_obj_release(struct mlx5_txq_obj *txq_obj)
1686 {
1687 	MLX5_ASSERT(txq_obj);
1688 	if (txq_obj->txq_ctrl->is_hairpin) {
1689 		if (txq_obj->sq) {
1690 			claim_zero(mlx5_devx_cmd_destroy(txq_obj->sq));
1691 			txq_obj->sq = NULL;
1692 		}
1693 		if (txq_obj->tis)
1694 			claim_zero(mlx5_devx_cmd_destroy(txq_obj->tis));
1695 		if (txq_obj->umem_obj_wq_buffer) {
1696 			claim_zero(mlx5_os_umem_dereg(txq_obj->umem_obj_wq_buffer));
1697 			txq_obj->umem_obj_wq_buffer = NULL;
1698 		}
1699 		if (txq_obj->umem_buf_wq_buffer) {
1700 			mlx5_free(txq_obj->umem_buf_wq_buffer);
1701 			txq_obj->umem_buf_wq_buffer = NULL;
1702 		}
1703 #if defined(HAVE_MLX5DV_DEVX_UAR_OFFSET) || !defined(HAVE_INFINIBAND_VERBS_H)
1704 	} else {
1705 		mlx5_txq_release_devx_resources(txq_obj);
1706 #endif
1707 	}
1708 }
1709 
1710 struct mlx5_obj_ops devx_obj_ops = {
1711 	.rxq_obj_modify_vlan_strip = mlx5_rxq_obj_modify_rq_vlan_strip,
1712 	.rxq_obj_new = mlx5_rxq_devx_obj_new,
1713 	.rxq_event_get = mlx5_rx_devx_get_event,
1714 	.rxq_obj_modify = mlx5_devx_modify_rq,
1715 	.rxq_obj_release = mlx5_rxq_devx_obj_release,
1716 	.rxq_event_get_lwm = mlx5_rx_devx_get_event_lwm,
1717 	.ind_table_new = mlx5_devx_ind_table_new,
1718 	.ind_table_modify = mlx5_devx_ind_table_modify,
1719 	.ind_table_destroy = mlx5_devx_ind_table_destroy,
1720 	.hrxq_new = mlx5_devx_hrxq_new,
1721 	.hrxq_destroy = mlx5_devx_tir_destroy,
1722 	.hrxq_modify = mlx5_devx_hrxq_modify,
1723 	.drop_action_create = mlx5_devx_drop_action_create,
1724 	.drop_action_destroy = mlx5_devx_drop_action_destroy,
1725 	.txq_obj_new = mlx5_txq_devx_obj_new,
1726 	.txq_obj_modify = mlx5_txq_devx_modify,
1727 	.txq_obj_release = mlx5_txq_devx_obj_release,
1728 	.lb_dummy_queue_create = NULL,
1729 	.lb_dummy_queue_release = NULL,
1730 };
1731