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