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