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