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