net/mlx5: add reference counter on memory region
[dpdk.git] / drivers / net / mlx5 / mlx5_txq.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2015 6WIND S.A.
5  *   Copyright 2015 Mellanox.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of 6WIND S.A. nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stddef.h>
35 #include <assert.h>
36 #include <errno.h>
37 #include <string.h>
38 #include <stdint.h>
39 #include <unistd.h>
40 #include <sys/mman.h>
41
42 /* Verbs header. */
43 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
44 #ifdef PEDANTIC
45 #pragma GCC diagnostic ignored "-Wpedantic"
46 #endif
47 #include <infiniband/verbs.h>
48 #ifdef PEDANTIC
49 #pragma GCC diagnostic error "-Wpedantic"
50 #endif
51
52 #include <rte_mbuf.h>
53 #include <rte_malloc.h>
54 #include <rte_ethdev.h>
55 #include <rte_common.h>
56
57 #include "mlx5_utils.h"
58 #include "mlx5_defs.h"
59 #include "mlx5.h"
60 #include "mlx5_rxtx.h"
61 #include "mlx5_autoconf.h"
62
63 /**
64  * Allocate TX queue elements.
65  *
66  * @param txq_ctrl
67  *   Pointer to TX queue structure.
68  * @param elts_n
69  *   Number of elements to allocate.
70  */
71 static void
72 txq_alloc_elts(struct mlx5_txq_ctrl *txq_ctrl, unsigned int elts_n)
73 {
74         unsigned int i;
75
76         for (i = 0; (i != elts_n); ++i)
77                 (*txq_ctrl->txq.elts)[i] = NULL;
78         for (i = 0; (i != (1u << txq_ctrl->txq.wqe_n)); ++i) {
79                 volatile struct mlx5_wqe64 *wqe =
80                         (volatile struct mlx5_wqe64 *)
81                         txq_ctrl->txq.wqes + i;
82
83                 memset((void *)(uintptr_t)wqe, 0x0, sizeof(*wqe));
84         }
85         DEBUG("%p: allocated and configured %u WRs", (void *)txq_ctrl, elts_n);
86         txq_ctrl->txq.elts_head = 0;
87         txq_ctrl->txq.elts_tail = 0;
88         txq_ctrl->txq.elts_comp = 0;
89 }
90
91 /**
92  * Free TX queue elements.
93  *
94  * @param txq_ctrl
95  *   Pointer to TX queue structure.
96  */
97 static void
98 txq_free_elts(struct mlx5_txq_ctrl *txq_ctrl)
99 {
100         const uint16_t elts_n = 1 << txq_ctrl->txq.elts_n;
101         const uint16_t elts_m = elts_n - 1;
102         uint16_t elts_head = txq_ctrl->txq.elts_head;
103         uint16_t elts_tail = txq_ctrl->txq.elts_tail;
104         struct rte_mbuf *(*elts)[elts_n] = txq_ctrl->txq.elts;
105
106         DEBUG("%p: freeing WRs", (void *)txq_ctrl);
107         txq_ctrl->txq.elts_head = 0;
108         txq_ctrl->txq.elts_tail = 0;
109         txq_ctrl->txq.elts_comp = 0;
110
111         while (elts_tail != elts_head) {
112                 struct rte_mbuf *elt = (*elts)[elts_tail & elts_m];
113
114                 assert(elt != NULL);
115                 rte_pktmbuf_free_seg(elt);
116 #ifndef NDEBUG
117                 /* Poisoning. */
118                 memset(&(*elts)[elts_tail & elts_m],
119                        0x77,
120                        sizeof((*elts)[elts_tail & elts_m]));
121 #endif
122                 ++elts_tail;
123         }
124 }
125
126 /**
127  * Clean up a TX queue.
128  *
129  * Destroy objects, free allocated memory and reset the structure for reuse.
130  *
131  * @param txq_ctrl
132  *   Pointer to TX queue structure.
133  */
134 void
135 mlx5_txq_cleanup(struct mlx5_txq_ctrl *txq_ctrl)
136 {
137         size_t i;
138
139         DEBUG("cleaning up %p", (void *)txq_ctrl);
140         txq_free_elts(txq_ctrl);
141         if (txq_ctrl->qp != NULL)
142                 claim_zero(ibv_destroy_qp(txq_ctrl->qp));
143         if (txq_ctrl->cq != NULL)
144                 claim_zero(ibv_destroy_cq(txq_ctrl->cq));
145         for (i = 0; (i != RTE_DIM(txq_ctrl->txq.mp2mr)); ++i)
146                 if (txq_ctrl->txq.mp2mr[i])
147                         priv_mr_release(txq_ctrl->priv, txq_ctrl->txq.mp2mr[i]);
148         memset(txq_ctrl, 0, sizeof(*txq_ctrl));
149 }
150
151 /**
152  * Initialize TX queue.
153  *
154  * @param tmpl
155  *   Pointer to TX queue control template.
156  * @param txq_ctrl
157  *   Pointer to TX queue control.
158  *
159  * @return
160  *   0 on success, errno value on failure.
161  */
162 static inline int
163 txq_setup(struct mlx5_txq_ctrl *tmpl, struct mlx5_txq_ctrl *txq_ctrl)
164 {
165         struct mlx5dv_qp qp;
166         struct ibv_cq *ibcq = tmpl->cq;
167         struct mlx5dv_cq cq_info;
168         struct mlx5dv_obj obj;
169         int ret = 0;
170
171         qp.comp_mask = MLX5DV_QP_MASK_UAR_MMAP_OFFSET;
172         obj.cq.in = ibcq;
173         obj.cq.out = &cq_info;
174         obj.qp.in = tmpl->qp;
175         obj.qp.out = &qp;
176         ret = mlx5dv_init_obj(&obj, MLX5DV_OBJ_CQ | MLX5DV_OBJ_QP);
177         if (ret != 0) {
178                 return -EINVAL;
179         }
180         if (cq_info.cqe_size != RTE_CACHE_LINE_SIZE) {
181                 ERROR("Wrong MLX5_CQE_SIZE environment variable value: "
182                       "it should be set to %u", RTE_CACHE_LINE_SIZE);
183                 return EINVAL;
184         }
185         tmpl->txq.cqe_n = log2above(cq_info.cqe_cnt);
186         tmpl->txq.qp_num_8s = tmpl->qp->qp_num << 8;
187         tmpl->txq.wqes = qp.sq.buf;
188         tmpl->txq.wqe_n = log2above(qp.sq.wqe_cnt);
189         tmpl->txq.qp_db = &qp.dbrec[MLX5_SND_DBR];
190         tmpl->txq.bf_reg = qp.bf.reg;
191         tmpl->txq.cq_db = cq_info.dbrec;
192         tmpl->txq.cqes =
193                 (volatile struct mlx5_cqe (*)[])
194                 (uintptr_t)cq_info.buf;
195         tmpl->txq.elts =
196                 (struct rte_mbuf *(*)[1 << tmpl->txq.elts_n])
197                 ((uintptr_t)txq_ctrl + sizeof(*txq_ctrl));
198         if (qp.comp_mask | MLX5DV_QP_MASK_UAR_MMAP_OFFSET) {
199                 tmpl->uar_mmap_offset = qp.uar_mmap_offset;
200         } else {
201                 ERROR("Failed to retrieve UAR info, invalid libmlx5.so version");
202                 return EINVAL;
203         }
204
205         return 0;
206 }
207
208 /**
209  * Configure a TX queue.
210  *
211  * @param dev
212  *   Pointer to Ethernet device structure.
213  * @param txq_ctrl
214  *   Pointer to TX queue structure.
215  * @param desc
216  *   Number of descriptors to configure in queue.
217  * @param socket
218  *   NUMA socket on which memory must be allocated.
219  * @param[in] conf
220  *   Thresholds parameters.
221  *
222  * @return
223  *   0 on success, errno value on failure.
224  */
225 int
226 mlx5_txq_ctrl_setup(struct rte_eth_dev *dev, struct mlx5_txq_ctrl *txq_ctrl,
227                     uint16_t desc, unsigned int socket,
228                     const struct rte_eth_txconf *conf)
229 {
230         struct priv *priv = mlx5_get_priv(dev);
231         struct mlx5_txq_ctrl tmpl = {
232                 .priv = priv,
233                 .socket = socket,
234         };
235         union {
236                 struct ibv_qp_init_attr_ex init;
237                 struct ibv_cq_init_attr_ex cq;
238                 struct ibv_qp_attr mod;
239                 struct ibv_cq_ex cq_attr;
240         } attr;
241         unsigned int cqe_n;
242         const unsigned int max_tso_inline = ((MLX5_MAX_TSO_HEADER +
243                                              (RTE_CACHE_LINE_SIZE - 1)) /
244                                               RTE_CACHE_LINE_SIZE);
245         int ret = 0;
246
247         if (mlx5_getenv_int("MLX5_ENABLE_CQE_COMPRESSION")) {
248                 ret = ENOTSUP;
249                 ERROR("MLX5_ENABLE_CQE_COMPRESSION must never be set");
250                 goto error;
251         }
252         tmpl.txq.flags = conf->txq_flags;
253         assert(desc > MLX5_TX_COMP_THRESH);
254         tmpl.txq.elts_n = log2above(desc);
255         if (priv->mps == MLX5_MPW_ENHANCED)
256                 tmpl.txq.mpw_hdr_dseg = priv->mpw_hdr_dseg;
257         /* MRs will be registered in mp2mr[] later. */
258         attr.cq = (struct ibv_cq_init_attr_ex){
259                 .comp_mask = 0,
260         };
261         cqe_n = ((desc / MLX5_TX_COMP_THRESH) - 1) ?
262                 ((desc / MLX5_TX_COMP_THRESH) - 1) : 1;
263         if (priv->mps == MLX5_MPW_ENHANCED)
264                 cqe_n += MLX5_TX_COMP_THRESH_INLINE_DIV;
265         tmpl.cq = ibv_create_cq(priv->ctx,
266                                 cqe_n,
267                                 NULL, NULL, 0);
268         if (tmpl.cq == NULL) {
269                 ret = ENOMEM;
270                 ERROR("%p: CQ creation failure: %s",
271                       (void *)dev, strerror(ret));
272                 goto error;
273         }
274         DEBUG("priv->device_attr.max_qp_wr is %d",
275               priv->device_attr.orig_attr.max_qp_wr);
276         DEBUG("priv->device_attr.max_sge is %d",
277               priv->device_attr.orig_attr.max_sge);
278         attr.init = (struct ibv_qp_init_attr_ex){
279                 /* CQ to be associated with the send queue. */
280                 .send_cq = tmpl.cq,
281                 /* CQ to be associated with the receive queue. */
282                 .recv_cq = tmpl.cq,
283                 .cap = {
284                         /* Max number of outstanding WRs. */
285                         .max_send_wr =
286                          ((priv->device_attr.orig_attr.max_qp_wr < desc) ?
287                            priv->device_attr.orig_attr.max_qp_wr :
288                            desc),
289                         /*
290                          * Max number of scatter/gather elements in a WR,
291                          * must be 1 to prevent libmlx5 from trying to affect
292                          * too much memory. TX gather is not impacted by the
293                          * priv->device_attr.max_sge limit and will still work
294                          * properly.
295                          */
296                         .max_send_sge = 1,
297                 },
298                 .qp_type = IBV_QPT_RAW_PACKET,
299                 /* Do *NOT* enable this, completions events are managed per
300                  * TX burst. */
301                 .sq_sig_all = 0,
302                 .pd = priv->pd,
303                 .comp_mask = IBV_QP_INIT_ATTR_PD,
304         };
305         if (priv->txq_inline && (priv->txqs_n >= priv->txqs_inline)) {
306                 unsigned int ds_cnt;
307
308                 tmpl.txq.max_inline =
309                         ((priv->txq_inline + (RTE_CACHE_LINE_SIZE - 1)) /
310                          RTE_CACHE_LINE_SIZE);
311                 tmpl.txq.inline_en = 1;
312                 /* TSO and MPS can't be enabled concurrently. */
313                 assert(!priv->tso || !priv->mps);
314                 if (priv->mps == MLX5_MPW_ENHANCED) {
315                         tmpl.txq.inline_max_packet_sz =
316                                 priv->inline_max_packet_sz;
317                         /* To minimize the size of data set, avoid requesting
318                          * too large WQ.
319                          */
320                         attr.init.cap.max_inline_data =
321                                 ((RTE_MIN(priv->txq_inline,
322                                           priv->inline_max_packet_sz) +
323                                   (RTE_CACHE_LINE_SIZE - 1)) /
324                                  RTE_CACHE_LINE_SIZE) * RTE_CACHE_LINE_SIZE;
325                 } else if (priv->tso) {
326                         int inline_diff = tmpl.txq.max_inline - max_tso_inline;
327
328                         /*
329                          * Adjust inline value as Verbs aggregates
330                          * tso_inline and txq_inline fields.
331                          */
332                         attr.init.cap.max_inline_data = inline_diff > 0 ?
333                                                         inline_diff *
334                                                         RTE_CACHE_LINE_SIZE :
335                                                         0;
336                 } else {
337                         attr.init.cap.max_inline_data =
338                                 tmpl.txq.max_inline * RTE_CACHE_LINE_SIZE;
339                 }
340                 /*
341                  * Check if the inline size is too large in a way which
342                  * can make the WQE DS to overflow.
343                  * Considering in calculation:
344                  *      WQE CTRL (1 DS)
345                  *      WQE ETH  (1 DS)
346                  *      Inline part (N DS)
347                  */
348                 ds_cnt = 2 +
349                         (attr.init.cap.max_inline_data / MLX5_WQE_DWORD_SIZE);
350                 if (ds_cnt > MLX5_DSEG_MAX) {
351                         unsigned int max_inline = (MLX5_DSEG_MAX - 2) *
352                                                    MLX5_WQE_DWORD_SIZE;
353
354                         max_inline = max_inline - (max_inline %
355                                                    RTE_CACHE_LINE_SIZE);
356                         WARN("txq inline is too large (%d) setting it to "
357                              "the maximum possible: %d\n",
358                              priv->txq_inline, max_inline);
359                         tmpl.txq.max_inline = max_inline / RTE_CACHE_LINE_SIZE;
360                         attr.init.cap.max_inline_data = max_inline;
361                 }
362         }
363         if (priv->tso) {
364                 attr.init.max_tso_header =
365                         max_tso_inline * RTE_CACHE_LINE_SIZE;
366                 attr.init.comp_mask |= IBV_QP_INIT_ATTR_MAX_TSO_HEADER;
367                 tmpl.txq.max_inline = RTE_MAX(tmpl.txq.max_inline,
368                                               max_tso_inline);
369                 tmpl.txq.tso_en = 1;
370         }
371         if (priv->tunnel_en)
372                 tmpl.txq.tunnel_en = 1;
373         tmpl.qp = ibv_create_qp_ex(priv->ctx, &attr.init);
374         if (tmpl.qp == NULL) {
375                 ret = (errno ? errno : EINVAL);
376                 ERROR("%p: QP creation failure: %s",
377                       (void *)dev, strerror(ret));
378                 goto error;
379         }
380         DEBUG("TX queue capabilities: max_send_wr=%u, max_send_sge=%u,"
381               " max_inline_data=%u",
382               attr.init.cap.max_send_wr,
383               attr.init.cap.max_send_sge,
384               attr.init.cap.max_inline_data);
385         attr.mod = (struct ibv_qp_attr){
386                 /* Move the QP to this state. */
387                 .qp_state = IBV_QPS_INIT,
388                 /* Primary port number. */
389                 .port_num = priv->port
390         };
391         ret = ibv_modify_qp(tmpl.qp, &attr.mod,
392                             (IBV_QP_STATE | IBV_QP_PORT));
393         if (ret) {
394                 ERROR("%p: QP state to IBV_QPS_INIT failed: %s",
395                       (void *)dev, strerror(ret));
396                 goto error;
397         }
398         ret = txq_setup(&tmpl, txq_ctrl);
399         if (ret) {
400                 ERROR("%p: cannot initialize TX queue structure: %s",
401                       (void *)dev, strerror(ret));
402                 goto error;
403         }
404         txq_alloc_elts(&tmpl, desc);
405         attr.mod = (struct ibv_qp_attr){
406                 .qp_state = IBV_QPS_RTR
407         };
408         ret = ibv_modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE);
409         if (ret) {
410                 ERROR("%p: QP state to IBV_QPS_RTR failed: %s",
411                       (void *)dev, strerror(ret));
412                 goto error;
413         }
414         attr.mod.qp_state = IBV_QPS_RTS;
415         ret = ibv_modify_qp(tmpl.qp, &attr.mod, IBV_QP_STATE);
416         if (ret) {
417                 ERROR("%p: QP state to IBV_QPS_RTS failed: %s",
418                       (void *)dev, strerror(ret));
419                 goto error;
420         }
421         /* Clean up txq in case we're reinitializing it. */
422         DEBUG("%p: cleaning-up old txq just in case", (void *)txq_ctrl);
423         mlx5_txq_cleanup(txq_ctrl);
424         *txq_ctrl = tmpl;
425         DEBUG("%p: txq updated with %p", (void *)txq_ctrl, (void *)&tmpl);
426         /* Pre-register known mempools. */
427         rte_mempool_walk(mlx5_txq_mp2mr_iter, txq_ctrl);
428         assert(ret == 0);
429         return 0;
430 error:
431         mlx5_txq_cleanup(&tmpl);
432         assert(ret > 0);
433         return ret;
434 }
435
436 /**
437  * DPDK callback to configure a TX queue.
438  *
439  * @param dev
440  *   Pointer to Ethernet device structure.
441  * @param idx
442  *   TX queue index.
443  * @param desc
444  *   Number of descriptors to configure in queue.
445  * @param socket
446  *   NUMA socket on which memory must be allocated.
447  * @param[in] conf
448  *   Thresholds parameters.
449  *
450  * @return
451  *   0 on success, negative errno value on failure.
452  */
453 int
454 mlx5_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
455                     unsigned int socket, const struct rte_eth_txconf *conf)
456 {
457         struct priv *priv = dev->data->dev_private;
458         struct mlx5_txq_data *txq = (*priv->txqs)[idx];
459         struct mlx5_txq_ctrl *txq_ctrl =
460                 container_of(txq, struct mlx5_txq_ctrl, txq);
461         int ret;
462
463         if (mlx5_is_secondary())
464                 return -E_RTE_SECONDARY;
465
466         priv_lock(priv);
467         if (desc <= MLX5_TX_COMP_THRESH) {
468                 WARN("%p: number of descriptors requested for TX queue %u"
469                      " must be higher than MLX5_TX_COMP_THRESH, using"
470                      " %u instead of %u",
471                      (void *)dev, idx, MLX5_TX_COMP_THRESH + 1, desc);
472                 desc = MLX5_TX_COMP_THRESH + 1;
473         }
474         if (!rte_is_power_of_2(desc)) {
475                 desc = 1 << log2above(desc);
476                 WARN("%p: increased number of descriptors in TX queue %u"
477                      " to the next power of two (%d)",
478                      (void *)dev, idx, desc);
479         }
480         DEBUG("%p: configuring queue %u for %u descriptors",
481               (void *)dev, idx, desc);
482         if (idx >= priv->txqs_n) {
483                 ERROR("%p: queue index out of range (%u >= %u)",
484                       (void *)dev, idx, priv->txqs_n);
485                 priv_unlock(priv);
486                 return -EOVERFLOW;
487         }
488         if (txq != NULL) {
489                 DEBUG("%p: reusing already allocated queue index %u (%p)",
490                       (void *)dev, idx, (void *)txq);
491                 if (dev->data->dev_started) {
492                         priv_unlock(priv);
493                         return -EEXIST;
494                 }
495                 (*priv->txqs)[idx] = NULL;
496                 mlx5_txq_cleanup(txq_ctrl);
497                 /* Resize if txq size is changed. */
498                 if (txq_ctrl->txq.elts_n != log2above(desc)) {
499                         txq_ctrl = rte_realloc(txq_ctrl,
500                                                sizeof(*txq_ctrl) +
501                                                desc * sizeof(struct rte_mbuf *),
502                                                RTE_CACHE_LINE_SIZE);
503                         if (!txq_ctrl) {
504                                 ERROR("%p: unable to reallocate queue index %u",
505                                         (void *)dev, idx);
506                                 priv_unlock(priv);
507                                 return -ENOMEM;
508                         }
509                 }
510         } else {
511                 txq_ctrl =
512                         rte_calloc_socket("TXQ", 1,
513                                           sizeof(*txq_ctrl) +
514                                           desc * sizeof(struct rte_mbuf *),
515                                           0, socket);
516                 if (txq_ctrl == NULL) {
517                         ERROR("%p: unable to allocate queue index %u",
518                               (void *)dev, idx);
519                         priv_unlock(priv);
520                         return -ENOMEM;
521                 }
522         }
523         ret = mlx5_txq_ctrl_setup(dev, txq_ctrl, desc, socket, conf);
524         if (ret)
525                 rte_free(txq_ctrl);
526         else {
527                 txq_ctrl->txq.stats.idx = idx;
528                 DEBUG("%p: adding TX queue %p to list",
529                       (void *)dev, (void *)txq_ctrl);
530                 (*priv->txqs)[idx] = &txq_ctrl->txq;
531         }
532         priv_unlock(priv);
533         return -ret;
534 }
535
536 /**
537  * DPDK callback to release a TX queue.
538  *
539  * @param dpdk_txq
540  *   Generic TX queue pointer.
541  */
542 void
543 mlx5_tx_queue_release(void *dpdk_txq)
544 {
545         struct mlx5_txq_data *txq = (struct mlx5_txq_data *)dpdk_txq;
546         struct mlx5_txq_ctrl *txq_ctrl;
547         struct priv *priv;
548         unsigned int i;
549
550         if (mlx5_is_secondary())
551                 return;
552
553         if (txq == NULL)
554                 return;
555         txq_ctrl = container_of(txq, struct mlx5_txq_ctrl, txq);
556         priv = txq_ctrl->priv;
557         priv_lock(priv);
558         for (i = 0; (i != priv->txqs_n); ++i)
559                 if ((*priv->txqs)[i] == txq) {
560                         DEBUG("%p: removing TX queue %p from list",
561                               (void *)priv->dev, (void *)txq_ctrl);
562                         (*priv->txqs)[i] = NULL;
563                         break;
564                 }
565         mlx5_txq_cleanup(txq_ctrl);
566         rte_free(txq_ctrl);
567         priv_unlock(priv);
568 }
569
570
571 /**
572  * Map locally UAR used in Tx queues for BlueFlame doorbell.
573  *
574  * @param[in] priv
575  *   Pointer to private structure.
576  * @param fd
577  *   Verbs file descriptor to map UAR pages.
578  *
579  * @return
580  *   0 on success, errno value on failure.
581  */
582 int
583 priv_tx_uar_remap(struct priv *priv, int fd)
584 {
585         unsigned int i, j;
586         uintptr_t pages[priv->txqs_n];
587         unsigned int pages_n = 0;
588         uintptr_t uar_va;
589         void *addr;
590         struct mlx5_txq_data *txq;
591         struct mlx5_txq_ctrl *txq_ctrl;
592         int already_mapped;
593         size_t page_size = sysconf(_SC_PAGESIZE);
594
595         /*
596          * As rdma-core, UARs are mapped in size of OS page size.
597          * Use aligned address to avoid duplicate mmap.
598          * Ref to libmlx5 function: mlx5_init_context()
599          */
600         for (i = 0; i != priv->txqs_n; ++i) {
601                 txq = (*priv->txqs)[i];
602                 txq_ctrl = container_of(txq, struct mlx5_txq_ctrl, txq);
603                 uar_va = (uintptr_t)txq_ctrl->txq.bf_reg;
604                 uar_va = RTE_ALIGN_FLOOR(uar_va, page_size);
605                 already_mapped = 0;
606                 for (j = 0; j != pages_n; ++j) {
607                         if (pages[j] == uar_va) {
608                                 already_mapped = 1;
609                                 break;
610                         }
611                 }
612                 if (already_mapped)
613                         continue;
614                 pages[pages_n++] = uar_va;
615                 addr = mmap((void *)uar_va, page_size,
616                             PROT_WRITE, MAP_FIXED | MAP_SHARED, fd,
617                             txq_ctrl->uar_mmap_offset);
618                 if (addr != (void *)uar_va) {
619                         ERROR("call to mmap failed on UAR for txq %d\n", i);
620                         return -1;
621                 }
622         }
623         return 0;
624 }