net/mlx5: reorder Tx queue Verbs object creation
[dpdk.git] / drivers / net / mlx5 / mlx5_txq.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2015 6WIND S.A.
3  * Copyright 2015 Mellanox Technologies, Ltd
4  */
5
6 #include <stddef.h>
7 #include <errno.h>
8 #include <string.h>
9 #include <stdint.h>
10 #include <unistd.h>
11 #include <inttypes.h>
12
13 #include <rte_mbuf.h>
14 #include <rte_malloc.h>
15 #include <rte_ethdev_driver.h>
16 #include <rte_common.h>
17 #include <rte_eal_paging.h>
18
19 #include <mlx5_glue.h>
20 #include <mlx5_devx_cmds.h>
21 #include <mlx5_common.h>
22 #include <mlx5_common_mr.h>
23 #include <mlx5_common_os.h>
24 #include <mlx5_malloc.h>
25
26 #include "mlx5_defs.h"
27 #include "mlx5_utils.h"
28 #include "mlx5.h"
29 #include "mlx5_rxtx.h"
30 #include "mlx5_autoconf.h"
31
32 /**
33  * Allocate TX queue elements.
34  *
35  * @param txq_ctrl
36  *   Pointer to TX queue structure.
37  */
38 void
39 txq_alloc_elts(struct mlx5_txq_ctrl *txq_ctrl)
40 {
41         const unsigned int elts_n = 1 << txq_ctrl->txq.elts_n;
42         unsigned int i;
43
44         for (i = 0; (i != elts_n); ++i)
45                 txq_ctrl->txq.elts[i] = NULL;
46         DRV_LOG(DEBUG, "port %u Tx queue %u allocated and configured %u WRs",
47                 PORT_ID(txq_ctrl->priv), txq_ctrl->txq.idx, elts_n);
48         txq_ctrl->txq.elts_head = 0;
49         txq_ctrl->txq.elts_tail = 0;
50         txq_ctrl->txq.elts_comp = 0;
51 }
52
53 /**
54  * Free TX queue elements.
55  *
56  * @param txq_ctrl
57  *   Pointer to TX queue structure.
58  */
59 void
60 txq_free_elts(struct mlx5_txq_ctrl *txq_ctrl)
61 {
62         const uint16_t elts_n = 1 << txq_ctrl->txq.elts_n;
63         const uint16_t elts_m = elts_n - 1;
64         uint16_t elts_head = txq_ctrl->txq.elts_head;
65         uint16_t elts_tail = txq_ctrl->txq.elts_tail;
66         struct rte_mbuf *(*elts)[elts_n] = &txq_ctrl->txq.elts;
67
68         DRV_LOG(DEBUG, "port %u Tx queue %u freeing WRs",
69                 PORT_ID(txq_ctrl->priv), txq_ctrl->txq.idx);
70         txq_ctrl->txq.elts_head = 0;
71         txq_ctrl->txq.elts_tail = 0;
72         txq_ctrl->txq.elts_comp = 0;
73
74         while (elts_tail != elts_head) {
75                 struct rte_mbuf *elt = (*elts)[elts_tail & elts_m];
76
77                 MLX5_ASSERT(elt != NULL);
78                 rte_pktmbuf_free_seg(elt);
79 #ifdef RTE_LIBRTE_MLX5_DEBUG
80                 /* Poisoning. */
81                 memset(&(*elts)[elts_tail & elts_m],
82                        0x77,
83                        sizeof((*elts)[elts_tail & elts_m]));
84 #endif
85                 ++elts_tail;
86         }
87 }
88
89 /**
90  * Returns the per-port supported offloads.
91  *
92  * @param dev
93  *   Pointer to Ethernet device.
94  *
95  * @return
96  *   Supported Tx offloads.
97  */
98 uint64_t
99 mlx5_get_tx_port_offloads(struct rte_eth_dev *dev)
100 {
101         struct mlx5_priv *priv = dev->data->dev_private;
102         uint64_t offloads = (DEV_TX_OFFLOAD_MULTI_SEGS |
103                              DEV_TX_OFFLOAD_VLAN_INSERT);
104         struct mlx5_dev_config *config = &priv->config;
105
106         if (config->hw_csum)
107                 offloads |= (DEV_TX_OFFLOAD_IPV4_CKSUM |
108                              DEV_TX_OFFLOAD_UDP_CKSUM |
109                              DEV_TX_OFFLOAD_TCP_CKSUM);
110         if (config->tso)
111                 offloads |= DEV_TX_OFFLOAD_TCP_TSO;
112         if (config->tx_pp)
113                 offloads |= DEV_TX_OFFLOAD_SEND_ON_TIMESTAMP;
114         if (config->swp) {
115                 if (config->hw_csum)
116                         offloads |= DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM;
117                 if (config->tso)
118                         offloads |= (DEV_TX_OFFLOAD_IP_TNL_TSO |
119                                      DEV_TX_OFFLOAD_UDP_TNL_TSO);
120         }
121         if (config->tunnel_en) {
122                 if (config->hw_csum)
123                         offloads |= DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM;
124                 if (config->tso)
125                         offloads |= (DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
126                                      DEV_TX_OFFLOAD_GRE_TNL_TSO |
127                                      DEV_TX_OFFLOAD_GENEVE_TNL_TSO);
128         }
129         return offloads;
130 }
131
132 /* Fetches and drops all SW-owned and error CQEs to synchronize CQ. */
133 static void
134 txq_sync_cq(struct mlx5_txq_data *txq)
135 {
136         volatile struct mlx5_cqe *cqe;
137         int ret, i;
138
139         i = txq->cqe_s;
140         do {
141                 cqe = &txq->cqes[txq->cq_ci & txq->cqe_m];
142                 ret = check_cqe(cqe, txq->cqe_s, txq->cq_ci);
143                 if (unlikely(ret != MLX5_CQE_STATUS_SW_OWN)) {
144                         if (likely(ret != MLX5_CQE_STATUS_ERR)) {
145                                 /* No new CQEs in completion queue. */
146                                 MLX5_ASSERT(ret == MLX5_CQE_STATUS_HW_OWN);
147                                 break;
148                         }
149                 }
150                 ++txq->cq_ci;
151         } while (--i);
152         /* Move all CQEs to HW ownership. */
153         for (i = 0; i < txq->cqe_s; i++) {
154                 cqe = &txq->cqes[i];
155                 cqe->op_own = MLX5_CQE_INVALIDATE;
156         }
157         /* Resync CQE and WQE (WQ in reset state). */
158         rte_io_wmb();
159         *txq->cq_db = rte_cpu_to_be_32(txq->cq_ci);
160         rte_io_wmb();
161 }
162
163 /**
164  * Tx queue stop. Device queue goes to the idle state,
165  * all involved mbufs are freed from elts/WQ.
166  *
167  * @param dev
168  *   Pointer to Ethernet device structure.
169  * @param idx
170  *   Tx queue index.
171  *
172  * @return
173  *   0 on success, a negative errno value otherwise and rte_errno is set.
174  */
175 int
176 mlx5_tx_queue_stop_primary(struct rte_eth_dev *dev, uint16_t idx)
177 {
178         struct mlx5_priv *priv = dev->data->dev_private;
179         struct mlx5_txq_data *txq = (*priv->txqs)[idx];
180         struct mlx5_txq_ctrl *txq_ctrl =
181                         container_of(txq, struct mlx5_txq_ctrl, txq);
182         int ret;
183
184         MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
185         /* Move QP to RESET state. */
186         if (txq_ctrl->obj->type == MLX5_TXQ_OBJ_TYPE_DEVX_SQ) {
187                 struct mlx5_devx_modify_sq_attr msq_attr = { 0 };
188
189                 /* Change queue state to reset with DevX. */
190                 msq_attr.sq_state = MLX5_SQC_STATE_RDY;
191                 msq_attr.state = MLX5_SQC_STATE_RST;
192                 ret = mlx5_devx_cmd_modify_sq(txq_ctrl->obj->sq_devx,
193                                               &msq_attr);
194                 if (ret) {
195                         DRV_LOG(ERR, "Cannot change the "
196                                 "Tx QP state to RESET %s",
197                                 strerror(errno));
198                         rte_errno = errno;
199                         return ret;
200                 }
201         } else {
202                 struct ibv_qp_attr mod = {
203                         .qp_state = IBV_QPS_RESET,
204                         .port_num = (uint8_t)priv->dev_port,
205                 };
206                 struct ibv_qp *qp = txq_ctrl->obj->qp;
207
208                 /* Change queue state to reset with Verbs. */
209                 ret = mlx5_glue->modify_qp(qp, &mod, IBV_QP_STATE);
210                 if (ret) {
211                         DRV_LOG(ERR, "Cannot change the Tx QP state to RESET "
212                                 "%s", strerror(errno));
213                         rte_errno = errno;
214                         return ret;
215                 }
216         }
217         /* Handle all send completions. */
218         txq_sync_cq(txq);
219         /* Free elts stored in the SQ. */
220         txq_free_elts(txq_ctrl);
221         /* Prevent writing new pkts to SQ by setting no free WQE.*/
222         txq->wqe_ci = txq->wqe_s;
223         txq->wqe_pi = 0;
224         txq->elts_comp = 0;
225         /* Set the actual queue state. */
226         dev->data->tx_queue_state[idx] = RTE_ETH_QUEUE_STATE_STOPPED;
227         return 0;
228 }
229
230 /**
231  * Tx queue stop. Device queue goes to the idle state,
232  * all involved mbufs are freed from elts/WQ.
233  *
234  * @param dev
235  *   Pointer to Ethernet device structure.
236  * @param idx
237  *   Tx queue index.
238  *
239  * @return
240  *   0 on success, a negative errno value otherwise and rte_errno is set.
241  */
242 int
243 mlx5_tx_queue_stop(struct rte_eth_dev *dev, uint16_t idx)
244 {
245         int ret;
246
247         if (rte_eth_dev_is_tx_hairpin_queue(dev, idx)) {
248                 DRV_LOG(ERR, "Hairpin queue can't be stopped");
249                 rte_errno = EINVAL;
250                 return -EINVAL;
251         }
252         if (dev->data->tx_queue_state[idx] == RTE_ETH_QUEUE_STATE_STOPPED)
253                 return 0;
254         if (rte_eal_process_type() ==  RTE_PROC_SECONDARY) {
255                 ret = mlx5_mp_os_req_queue_control(dev, idx,
256                                                    MLX5_MP_REQ_QUEUE_TX_STOP);
257         } else {
258                 ret = mlx5_tx_queue_stop_primary(dev, idx);
259         }
260         return ret;
261 }
262
263 /**
264  * Rx queue start. Device queue goes to the ready state,
265  * all required mbufs are allocated and WQ is replenished.
266  *
267  * @param dev
268  *   Pointer to Ethernet device structure.
269  * @param idx
270  *   RX queue index.
271  *
272  * @return
273  *   0 on success, a negative errno value otherwise and rte_errno is set.
274  */
275 int
276 mlx5_tx_queue_start_primary(struct rte_eth_dev *dev, uint16_t idx)
277 {
278         struct mlx5_priv *priv = dev->data->dev_private;
279         struct mlx5_txq_data *txq = (*priv->txqs)[idx];
280         struct mlx5_txq_ctrl *txq_ctrl =
281                         container_of(txq, struct mlx5_txq_ctrl, txq);
282         int ret;
283
284         MLX5_ASSERT(rte_eal_process_type() ==  RTE_PROC_PRIMARY);
285         if (txq_ctrl->obj->type == MLX5_TXQ_OBJ_TYPE_DEVX_SQ) {
286                 struct mlx5_devx_modify_sq_attr msq_attr = { 0 };
287                 struct mlx5_txq_obj *obj = txq_ctrl->obj;
288
289                 msq_attr.sq_state = MLX5_SQC_STATE_RDY;
290                 msq_attr.state = MLX5_SQC_STATE_RST;
291                 ret = mlx5_devx_cmd_modify_sq(obj->sq_devx, &msq_attr);
292                 if (ret) {
293                         rte_errno = errno;
294                         DRV_LOG(ERR,
295                                 "Cannot change the Tx QP state to RESET "
296                                 "%s", strerror(errno));
297                         return ret;
298                 }
299                 msq_attr.sq_state = MLX5_SQC_STATE_RST;
300                 msq_attr.state = MLX5_SQC_STATE_RDY;
301                 ret = mlx5_devx_cmd_modify_sq(obj->sq_devx, &msq_attr);
302                 if (ret) {
303                         rte_errno = errno;
304                         DRV_LOG(ERR,
305                                 "Cannot change the Tx QP state to READY "
306                                 "%s", strerror(errno));
307                         return ret;
308                 }
309         } else {
310                 struct ibv_qp_attr mod = {
311                         .qp_state = IBV_QPS_RESET,
312                         .port_num = (uint8_t)priv->dev_port,
313                 };
314                 struct ibv_qp *qp = txq_ctrl->obj->qp;
315
316                 ret = mlx5_glue->modify_qp(qp, &mod, IBV_QP_STATE);
317                 if (ret) {
318                         DRV_LOG(ERR, "Cannot change the Tx QP state to RESET "
319                                 "%s", strerror(errno));
320                         rte_errno = errno;
321                         return ret;
322                 }
323                 mod.qp_state = IBV_QPS_INIT;
324                 ret = mlx5_glue->modify_qp(qp, &mod,
325                                            (IBV_QP_STATE | IBV_QP_PORT));
326                 if (ret) {
327                         DRV_LOG(ERR, "Cannot change Tx QP state to INIT %s",
328                                 strerror(errno));
329                         rte_errno = errno;
330                         return ret;
331                 }
332                 mod.qp_state = IBV_QPS_RTR;
333                 ret = mlx5_glue->modify_qp(qp, &mod, IBV_QP_STATE);
334                 if (ret) {
335                         DRV_LOG(ERR, "Cannot change Tx QP state to RTR %s",
336                                 strerror(errno));
337                         rte_errno = errno;
338                         return ret;
339                 }
340                 mod.qp_state = IBV_QPS_RTS;
341                 ret = mlx5_glue->modify_qp(qp, &mod, IBV_QP_STATE);
342                 if (ret) {
343                         DRV_LOG(ERR, "Cannot change Tx QP state to RTS %s",
344                                 strerror(errno));
345                         rte_errno = errno;
346                         return ret;
347                 }
348         }
349         txq_ctrl->txq.wqe_ci = 0;
350         txq_ctrl->txq.wqe_pi = 0;
351         txq_ctrl->txq.elts_comp = 0;
352         /* Set the actual queue state. */
353         dev->data->tx_queue_state[idx] = RTE_ETH_QUEUE_STATE_STARTED;
354         return 0;
355 }
356
357 /**
358  * Rx queue start. Device queue goes to the ready state,
359  * all required mbufs are allocated and WQ is replenished.
360  *
361  * @param dev
362  *   Pointer to Ethernet device structure.
363  * @param idx
364  *   RX queue index.
365  *
366  * @return
367  *   0 on success, a negative errno value otherwise and rte_errno is set.
368  */
369 int
370 mlx5_tx_queue_start(struct rte_eth_dev *dev, uint16_t idx)
371 {
372         int ret;
373
374         if (rte_eth_dev_is_tx_hairpin_queue(dev, idx)) {
375                 DRV_LOG(ERR, "Hairpin queue can't be started");
376                 rte_errno = EINVAL;
377                 return -EINVAL;
378         }
379         if (dev->data->tx_queue_state[idx] == RTE_ETH_QUEUE_STATE_STARTED)
380                 return 0;
381         if (rte_eal_process_type() ==  RTE_PROC_SECONDARY) {
382                 ret = mlx5_mp_os_req_queue_control(dev, idx,
383                                                    MLX5_MP_REQ_QUEUE_TX_START);
384         } else {
385                 ret = mlx5_tx_queue_start_primary(dev, idx);
386         }
387         return ret;
388 }
389
390 /**
391  * Tx queue presetup checks.
392  *
393  * @param dev
394  *   Pointer to Ethernet device structure.
395  * @param idx
396  *   Tx queue index.
397  * @param desc
398  *   Number of descriptors to configure in queue.
399  *
400  * @return
401  *   0 on success, a negative errno value otherwise and rte_errno is set.
402  */
403 static int
404 mlx5_tx_queue_pre_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t *desc)
405 {
406         struct mlx5_priv *priv = dev->data->dev_private;
407
408         if (*desc <= MLX5_TX_COMP_THRESH) {
409                 DRV_LOG(WARNING,
410                         "port %u number of descriptors requested for Tx queue"
411                         " %u must be higher than MLX5_TX_COMP_THRESH, using %u"
412                         " instead of %u", dev->data->port_id, idx,
413                         MLX5_TX_COMP_THRESH + 1, *desc);
414                 *desc = MLX5_TX_COMP_THRESH + 1;
415         }
416         if (!rte_is_power_of_2(*desc)) {
417                 *desc = 1 << log2above(*desc);
418                 DRV_LOG(WARNING,
419                         "port %u increased number of descriptors in Tx queue"
420                         " %u to the next power of two (%d)",
421                         dev->data->port_id, idx, *desc);
422         }
423         DRV_LOG(DEBUG, "port %u configuring queue %u for %u descriptors",
424                 dev->data->port_id, idx, *desc);
425         if (idx >= priv->txqs_n) {
426                 DRV_LOG(ERR, "port %u Tx queue index out of range (%u >= %u)",
427                         dev->data->port_id, idx, priv->txqs_n);
428                 rte_errno = EOVERFLOW;
429                 return -rte_errno;
430         }
431         if (!mlx5_txq_releasable(dev, idx)) {
432                 rte_errno = EBUSY;
433                 DRV_LOG(ERR, "port %u unable to release queue index %u",
434                         dev->data->port_id, idx);
435                 return -rte_errno;
436         }
437         mlx5_txq_release(dev, idx);
438         return 0;
439 }
440
441 /**
442  * DPDK callback to configure a TX queue.
443  *
444  * @param dev
445  *   Pointer to Ethernet device structure.
446  * @param idx
447  *   TX queue index.
448  * @param desc
449  *   Number of descriptors to configure in queue.
450  * @param socket
451  *   NUMA socket on which memory must be allocated.
452  * @param[in] conf
453  *   Thresholds parameters.
454  *
455  * @return
456  *   0 on success, a negative errno value otherwise and rte_errno is set.
457  */
458 int
459 mlx5_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
460                     unsigned int socket, const struct rte_eth_txconf *conf)
461 {
462         struct mlx5_priv *priv = dev->data->dev_private;
463         struct mlx5_txq_data *txq = (*priv->txqs)[idx];
464         struct mlx5_txq_ctrl *txq_ctrl =
465                 container_of(txq, struct mlx5_txq_ctrl, txq);
466         int res;
467
468         res = mlx5_tx_queue_pre_setup(dev, idx, &desc);
469         if (res)
470                 return res;
471         txq_ctrl = mlx5_txq_new(dev, idx, desc, socket, conf);
472         if (!txq_ctrl) {
473                 DRV_LOG(ERR, "port %u unable to allocate queue index %u",
474                         dev->data->port_id, idx);
475                 return -rte_errno;
476         }
477         DRV_LOG(DEBUG, "port %u adding Tx queue %u to list",
478                 dev->data->port_id, idx);
479         (*priv->txqs)[idx] = &txq_ctrl->txq;
480         dev->data->tx_queue_state[idx] = RTE_ETH_QUEUE_STATE_STARTED;
481         return 0;
482 }
483
484 /**
485  * DPDK callback to configure a TX hairpin queue.
486  *
487  * @param dev
488  *   Pointer to Ethernet device structure.
489  * @param idx
490  *   TX queue index.
491  * @param desc
492  *   Number of descriptors to configure in queue.
493  * @param[in] hairpin_conf
494  *   The hairpin binding configuration.
495  *
496  * @return
497  *   0 on success, a negative errno value otherwise and rte_errno is set.
498  */
499 int
500 mlx5_tx_hairpin_queue_setup(struct rte_eth_dev *dev, uint16_t idx,
501                             uint16_t desc,
502                             const struct rte_eth_hairpin_conf *hairpin_conf)
503 {
504         struct mlx5_priv *priv = dev->data->dev_private;
505         struct mlx5_txq_data *txq = (*priv->txqs)[idx];
506         struct mlx5_txq_ctrl *txq_ctrl =
507                 container_of(txq, struct mlx5_txq_ctrl, txq);
508         int res;
509
510         res = mlx5_tx_queue_pre_setup(dev, idx, &desc);
511         if (res)
512                 return res;
513         if (hairpin_conf->peer_count != 1 ||
514             hairpin_conf->peers[0].port != dev->data->port_id ||
515             hairpin_conf->peers[0].queue >= priv->rxqs_n) {
516                 DRV_LOG(ERR, "port %u unable to setup hairpin queue index %u "
517                         " invalid hairpind configuration", dev->data->port_id,
518                         idx);
519                 rte_errno = EINVAL;
520                 return -rte_errno;
521         }
522         txq_ctrl = mlx5_txq_hairpin_new(dev, idx, desc, hairpin_conf);
523         if (!txq_ctrl) {
524                 DRV_LOG(ERR, "port %u unable to allocate queue index %u",
525                         dev->data->port_id, idx);
526                 return -rte_errno;
527         }
528         DRV_LOG(DEBUG, "port %u adding Tx queue %u to list",
529                 dev->data->port_id, idx);
530         (*priv->txqs)[idx] = &txq_ctrl->txq;
531         dev->data->tx_queue_state[idx] = RTE_ETH_QUEUE_STATE_HAIRPIN;
532         return 0;
533 }
534
535 /**
536  * DPDK callback to release a TX queue.
537  *
538  * @param dpdk_txq
539  *   Generic TX queue pointer.
540  */
541 void
542 mlx5_tx_queue_release(void *dpdk_txq)
543 {
544         struct mlx5_txq_data *txq = (struct mlx5_txq_data *)dpdk_txq;
545         struct mlx5_txq_ctrl *txq_ctrl;
546         struct mlx5_priv *priv;
547         unsigned int i;
548
549         if (txq == NULL)
550                 return;
551         txq_ctrl = container_of(txq, struct mlx5_txq_ctrl, txq);
552         priv = txq_ctrl->priv;
553         for (i = 0; (i != priv->txqs_n); ++i)
554                 if ((*priv->txqs)[i] == txq) {
555                         DRV_LOG(DEBUG, "port %u removing Tx queue %u from list",
556                                 PORT_ID(priv), txq->idx);
557                         mlx5_txq_release(ETH_DEV(priv), i);
558                         break;
559                 }
560 }
561
562 /**
563  * Configure the doorbell register non-cached attribute.
564  *
565  * @param txq_ctrl
566  *   Pointer to Tx queue control structure.
567  * @param page_size
568  *   Systme page size
569  */
570 static void
571 txq_uar_ncattr_init(struct mlx5_txq_ctrl *txq_ctrl, size_t page_size)
572 {
573         struct mlx5_priv *priv = txq_ctrl->priv;
574         off_t cmd;
575
576         txq_ctrl->txq.db_heu = priv->config.dbnc == MLX5_TXDB_HEURISTIC;
577         txq_ctrl->txq.db_nc = 0;
578         /* Check the doorbell register mapping type. */
579         cmd = txq_ctrl->uar_mmap_offset / page_size;
580         cmd >>= MLX5_UAR_MMAP_CMD_SHIFT;
581         cmd &= MLX5_UAR_MMAP_CMD_MASK;
582         if (cmd == MLX5_MMAP_GET_NC_PAGES_CMD)
583                 txq_ctrl->txq.db_nc = 1;
584 }
585
586 /**
587  * Initialize Tx UAR registers for primary process.
588  *
589  * @param txq_ctrl
590  *   Pointer to Tx queue control structure.
591  */
592 static void
593 txq_uar_init(struct mlx5_txq_ctrl *txq_ctrl)
594 {
595         struct mlx5_priv *priv = txq_ctrl->priv;
596         struct mlx5_proc_priv *ppriv = MLX5_PROC_PRIV(PORT_ID(priv));
597 #ifndef RTE_ARCH_64
598         unsigned int lock_idx;
599 #endif
600         const size_t page_size = rte_mem_page_size();
601         if (page_size == (size_t)-1) {
602                 DRV_LOG(ERR, "Failed to get mem page size");
603                 rte_errno = ENOMEM;
604         }
605
606         if (txq_ctrl->type != MLX5_TXQ_TYPE_STANDARD)
607                 return;
608         MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
609         MLX5_ASSERT(ppriv);
610         ppriv->uar_table[txq_ctrl->txq.idx] = txq_ctrl->bf_reg;
611         txq_uar_ncattr_init(txq_ctrl, page_size);
612 #ifndef RTE_ARCH_64
613         /* Assign an UAR lock according to UAR page number */
614         lock_idx = (txq_ctrl->uar_mmap_offset / page_size) &
615                    MLX5_UAR_PAGE_NUM_MASK;
616         txq_ctrl->txq.uar_lock = &priv->sh->uar_lock[lock_idx];
617 #endif
618 }
619
620 /**
621  * Remap UAR register of a Tx queue for secondary process.
622  *
623  * Remapped address is stored at the table in the process private structure of
624  * the device, indexed by queue index.
625  *
626  * @param txq_ctrl
627  *   Pointer to Tx queue control structure.
628  * @param fd
629  *   Verbs file descriptor to map UAR pages.
630  *
631  * @return
632  *   0 on success, a negative errno value otherwise and rte_errno is set.
633  */
634 static int
635 txq_uar_init_secondary(struct mlx5_txq_ctrl *txq_ctrl, int fd)
636 {
637         struct mlx5_priv *priv = txq_ctrl->priv;
638         struct mlx5_proc_priv *ppriv = MLX5_PROC_PRIV(PORT_ID(priv));
639         struct mlx5_txq_data *txq = &txq_ctrl->txq;
640         void *addr;
641         uintptr_t uar_va;
642         uintptr_t offset;
643         const size_t page_size = rte_mem_page_size();
644         if (page_size == (size_t)-1) {
645                 DRV_LOG(ERR, "Failed to get mem page size");
646                 rte_errno = ENOMEM;
647                 return -rte_errno;
648         }
649
650         if (txq_ctrl->type != MLX5_TXQ_TYPE_STANDARD)
651                 return 0;
652         MLX5_ASSERT(ppriv);
653         /*
654          * As rdma-core, UARs are mapped in size of OS page
655          * size. Ref to libmlx5 function: mlx5_init_context()
656          */
657         uar_va = (uintptr_t)txq_ctrl->bf_reg;
658         offset = uar_va & (page_size - 1); /* Offset in page. */
659         addr = rte_mem_map(NULL, page_size, RTE_PROT_WRITE, RTE_MAP_SHARED,
660                             fd, txq_ctrl->uar_mmap_offset);
661         if (!addr) {
662                 DRV_LOG(ERR,
663                         "port %u mmap failed for BF reg of txq %u",
664                         txq->port_id, txq->idx);
665                 rte_errno = ENXIO;
666                 return -rte_errno;
667         }
668         addr = RTE_PTR_ADD(addr, offset);
669         ppriv->uar_table[txq->idx] = addr;
670         txq_uar_ncattr_init(txq_ctrl, page_size);
671         return 0;
672 }
673
674 /**
675  * Unmap UAR register of a Tx queue for secondary process.
676  *
677  * @param txq_ctrl
678  *   Pointer to Tx queue control structure.
679  */
680 static void
681 txq_uar_uninit_secondary(struct mlx5_txq_ctrl *txq_ctrl)
682 {
683         struct mlx5_proc_priv *ppriv = MLX5_PROC_PRIV(PORT_ID(txq_ctrl->priv));
684         void *addr;
685         const size_t page_size = rte_mem_page_size();
686         if (page_size == (size_t)-1) {
687                 DRV_LOG(ERR, "Failed to get mem page size");
688                 rte_errno = ENOMEM;
689         }
690
691         if (txq_ctrl->type != MLX5_TXQ_TYPE_STANDARD)
692                 return;
693         addr = ppriv->uar_table[txq_ctrl->txq.idx];
694         rte_mem_unmap(RTE_PTR_ALIGN_FLOOR(addr, page_size), page_size);
695 }
696
697 /**
698  * Deinitialize Tx UAR registers for secondary process.
699  *
700  * @param dev
701  *   Pointer to Ethernet device.
702  */
703 void
704 mlx5_tx_uar_uninit_secondary(struct rte_eth_dev *dev)
705 {
706         struct mlx5_priv *priv = dev->data->dev_private;
707         struct mlx5_txq_data *txq;
708         struct mlx5_txq_ctrl *txq_ctrl;
709         unsigned int i;
710
711         MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_SECONDARY);
712         for (i = 0; i != priv->txqs_n; ++i) {
713                 if (!(*priv->txqs)[i])
714                         continue;
715                 txq = (*priv->txqs)[i];
716                 txq_ctrl = container_of(txq, struct mlx5_txq_ctrl, txq);
717                 txq_uar_uninit_secondary(txq_ctrl);
718         }
719 }
720
721 /**
722  * Initialize Tx UAR registers for secondary process.
723  *
724  * @param dev
725  *   Pointer to Ethernet device.
726  * @param fd
727  *   Verbs file descriptor to map UAR pages.
728  *
729  * @return
730  *   0 on success, a negative errno value otherwise and rte_errno is set.
731  */
732 int
733 mlx5_tx_uar_init_secondary(struct rte_eth_dev *dev, int fd)
734 {
735         struct mlx5_priv *priv = dev->data->dev_private;
736         struct mlx5_txq_data *txq;
737         struct mlx5_txq_ctrl *txq_ctrl;
738         unsigned int i;
739         int ret;
740
741         MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_SECONDARY);
742         for (i = 0; i != priv->txqs_n; ++i) {
743                 if (!(*priv->txqs)[i])
744                         continue;
745                 txq = (*priv->txqs)[i];
746                 txq_ctrl = container_of(txq, struct mlx5_txq_ctrl, txq);
747                 if (txq_ctrl->type != MLX5_TXQ_TYPE_STANDARD)
748                         continue;
749                 MLX5_ASSERT(txq->idx == (uint16_t)i);
750                 ret = txq_uar_init_secondary(txq_ctrl, fd);
751                 if (ret)
752                         goto error;
753         }
754         return 0;
755 error:
756         /* Rollback. */
757         do {
758                 if (!(*priv->txqs)[i])
759                         continue;
760                 txq = (*priv->txqs)[i];
761                 txq_ctrl = container_of(txq, struct mlx5_txq_ctrl, txq);
762                 txq_uar_uninit_secondary(txq_ctrl);
763         } while (i--);
764         return -rte_errno;
765 }
766
767 /**
768  * Create the Tx hairpin queue object.
769  *
770  * @param dev
771  *   Pointer to Ethernet device.
772  * @param idx
773  *   Queue index in DPDK Tx queue array
774  *
775  * @return
776  *   The hairpin DevX object initialised, NULL otherwise and rte_errno is set.
777  */
778 static struct mlx5_txq_obj *
779 mlx5_txq_obj_hairpin_new(struct rte_eth_dev *dev, uint16_t idx)
780 {
781         struct mlx5_priv *priv = dev->data->dev_private;
782         struct mlx5_txq_data *txq_data = (*priv->txqs)[idx];
783         struct mlx5_txq_ctrl *txq_ctrl =
784                 container_of(txq_data, struct mlx5_txq_ctrl, txq);
785         struct mlx5_devx_create_sq_attr attr = { 0 };
786         struct mlx5_txq_obj *tmpl = NULL;
787         uint32_t max_wq_data;
788
789         MLX5_ASSERT(txq_data);
790         MLX5_ASSERT(!txq_ctrl->obj);
791         tmpl = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO, sizeof(*tmpl), 0,
792                            txq_ctrl->socket);
793         if (!tmpl) {
794                 DRV_LOG(ERR,
795                         "port %u Tx queue %u cannot allocate memory resources",
796                         dev->data->port_id, txq_data->idx);
797                 rte_errno = ENOMEM;
798                 return NULL;
799         }
800         tmpl->type = MLX5_TXQ_OBJ_TYPE_DEVX_HAIRPIN;
801         tmpl->txq_ctrl = txq_ctrl;
802         attr.hairpin = 1;
803         attr.tis_lst_sz = 1;
804         max_wq_data = priv->config.hca_attr.log_max_hairpin_wq_data_sz;
805         /* Jumbo frames > 9KB should be supported, and more packets. */
806         if (priv->config.log_hp_size != (uint32_t)MLX5_ARG_UNSET) {
807                 if (priv->config.log_hp_size > max_wq_data) {
808                         DRV_LOG(ERR, "total data size %u power of 2 is "
809                                 "too large for hairpin",
810                                 priv->config.log_hp_size);
811                         mlx5_free(tmpl);
812                         rte_errno = ERANGE;
813                         return NULL;
814                 }
815                 attr.wq_attr.log_hairpin_data_sz = priv->config.log_hp_size;
816         } else {
817                 attr.wq_attr.log_hairpin_data_sz =
818                                 (max_wq_data < MLX5_HAIRPIN_JUMBO_LOG_SIZE) ?
819                                  max_wq_data : MLX5_HAIRPIN_JUMBO_LOG_SIZE;
820         }
821         /* Set the packets number to the maximum value for performance. */
822         attr.wq_attr.log_hairpin_num_packets =
823                         attr.wq_attr.log_hairpin_data_sz -
824                         MLX5_HAIRPIN_QUEUE_STRIDE;
825         attr.tis_num = priv->sh->tis->id;
826         tmpl->sq = mlx5_devx_cmd_create_sq(priv->sh->ctx, &attr);
827         if (!tmpl->sq) {
828                 DRV_LOG(ERR,
829                         "port %u tx hairpin queue %u can't create sq object",
830                         dev->data->port_id, idx);
831                 mlx5_free(tmpl);
832                 rte_errno = errno;
833                 return NULL;
834         }
835         DRV_LOG(DEBUG, "port %u sxq %u updated with %p", dev->data->port_id,
836                 idx, (void *)&tmpl);
837         LIST_INSERT_HEAD(&priv->txqsobj, tmpl, next);
838         return tmpl;
839 }
840
841 /**
842  * Release DevX SQ resources.
843  *
844  * @param txq_ctrl
845  *   DevX Tx queue object.
846  */
847 static void
848 txq_release_devx_sq_resources(struct mlx5_txq_obj *txq_obj)
849 {
850         if (txq_obj->sq_devx)
851                 claim_zero(mlx5_devx_cmd_destroy(txq_obj->sq_devx));
852         if (txq_obj->sq_umem)
853                 claim_zero(mlx5_glue->devx_umem_dereg(txq_obj->sq_umem));
854         if (txq_obj->sq_buf)
855                 mlx5_free(txq_obj->sq_buf);
856         if (txq_obj->sq_dbrec_page)
857                 claim_zero(mlx5_release_dbr(&txq_obj->txq_ctrl->priv->dbrpgs,
858                                             mlx5_os_get_umem_id
859                                                  (txq_obj->sq_dbrec_page->umem),
860                                             txq_obj->sq_dbrec_offset));
861 }
862
863 /**
864  * Release DevX Tx CQ resources.
865  *
866  * @param txq_ctrl
867  *   DevX Tx queue object.
868  */
869 static void
870 txq_release_devx_cq_resources(struct mlx5_txq_obj *txq_obj)
871 {
872         if (txq_obj->cq_devx)
873                 claim_zero(mlx5_devx_cmd_destroy(txq_obj->cq_devx));
874         if (txq_obj->cq_umem)
875                 claim_zero(mlx5_glue->devx_umem_dereg(txq_obj->cq_umem));
876         if (txq_obj->cq_buf)
877                 mlx5_free(txq_obj->cq_buf);
878         if (txq_obj->cq_dbrec_page)
879                 claim_zero(mlx5_release_dbr(&txq_obj->txq_ctrl->priv->dbrpgs,
880                                             mlx5_os_get_umem_id
881                                                  (txq_obj->cq_dbrec_page->umem),
882                                             txq_obj->cq_dbrec_offset));
883 }
884
885 /**
886  * Destroy the Tx queue DevX object.
887  *
888  * @param txq_obj
889  *   Txq object to destroy
890  */
891 static void
892 txq_release_devx_resources(struct mlx5_txq_obj *txq_obj)
893 {
894         MLX5_ASSERT(txq_obj->type == MLX5_TXQ_OBJ_TYPE_DEVX_SQ);
895
896         txq_release_devx_sq_resources(txq_obj);
897         txq_release_devx_cq_resources(txq_obj);
898 }
899
900 #ifdef HAVE_MLX5DV_DEVX_UAR_OFFSET
901 /**
902  * Create a DevX CQ object for a Tx queue.
903  *
904  * @param dev
905  *   Pointer to Ethernet device.
906  * @param cqe_n
907  *   Number of entries in the CQ.
908  * @param idx
909  *   Queue index in DPDK Tx queue array.
910  * @param type
911  *   Type of the Tx queue object to create.
912  *
913  * @return
914  *   The DevX CQ object initialized, NULL otherwise and rte_errno is set.
915  */
916 static struct mlx5_devx_obj *
917 mlx5_devx_cq_new(struct rte_eth_dev *dev, uint32_t cqe_n, uint16_t idx,
918                  struct mlx5_txq_obj *txq_obj)
919 {
920         struct mlx5_priv *priv = dev->data->dev_private;
921         struct mlx5_txq_data *txq_data = (*priv->txqs)[idx];
922         struct mlx5_devx_obj *cq_obj = NULL;
923         struct mlx5_devx_cq_attr cq_attr = { 0 };
924         struct mlx5_cqe *cqe;
925         size_t page_size;
926         size_t alignment;
927         uint32_t i;
928         int ret;
929
930         MLX5_ASSERT(txq_data);
931         MLX5_ASSERT(txq_obj);
932         page_size = rte_mem_page_size();
933         if (page_size == (size_t)-1) {
934                 DRV_LOG(ERR, "Failed to get mem page size");
935                 rte_errno = ENOMEM;
936                 return NULL;
937         }
938         /* Allocate memory buffer for CQEs. */
939         alignment = MLX5_CQE_BUF_ALIGNMENT;
940         if (alignment == (size_t)-1) {
941                 DRV_LOG(ERR, "Failed to get CQE buf alignment");
942                 rte_errno = ENOMEM;
943                 return NULL;
944         }
945         cqe_n = 1UL << log2above(cqe_n);
946         if (cqe_n > UINT16_MAX) {
947                 DRV_LOG(ERR,
948                         "port %u Tx queue %u requests to many CQEs %u",
949                         dev->data->port_id, txq_data->idx, cqe_n);
950                 rte_errno = EINVAL;
951                 return NULL;
952         }
953         txq_obj->cq_buf = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO,
954                                       cqe_n * sizeof(struct mlx5_cqe),
955                                       alignment,
956                                       priv->sh->numa_node);
957         if (!txq_obj->cq_buf) {
958                 DRV_LOG(ERR,
959                         "port %u Tx queue %u cannot allocate memory (CQ)",
960                         dev->data->port_id, txq_data->idx);
961                 rte_errno = ENOMEM;
962                 return NULL;
963         }
964         /* Register allocated buffer in user space with DevX. */
965         txq_obj->cq_umem = mlx5_glue->devx_umem_reg(priv->sh->ctx,
966                                                 (void *)txq_obj->cq_buf,
967                                                 cqe_n * sizeof(struct mlx5_cqe),
968                                                 IBV_ACCESS_LOCAL_WRITE);
969         if (!txq_obj->cq_umem) {
970                 rte_errno = errno;
971                 DRV_LOG(ERR,
972                         "port %u Tx queue %u cannot register memory (CQ)",
973                         dev->data->port_id, txq_data->idx);
974                 goto error;
975         }
976         /* Allocate doorbell record for completion queue. */
977         txq_obj->cq_dbrec_offset = mlx5_get_dbr(priv->sh->ctx,
978                                                 &priv->dbrpgs,
979                                                 &txq_obj->cq_dbrec_page);
980         if (txq_obj->cq_dbrec_offset < 0) {
981                 rte_errno = errno;
982                 DRV_LOG(ERR, "Failed to allocate CQ door-bell.");
983                 goto error;
984         }
985         cq_attr.cqe_size = (sizeof(struct mlx5_cqe) == 128) ?
986                             MLX5_CQE_SIZE_128B : MLX5_CQE_SIZE_64B;
987         cq_attr.uar_page_id = mlx5_os_get_devx_uar_page_id(priv->sh->tx_uar);
988         cq_attr.eqn = priv->sh->txpp.eqn;
989         cq_attr.q_umem_valid = 1;
990         cq_attr.q_umem_offset = (uintptr_t)txq_obj->cq_buf % page_size;
991         cq_attr.q_umem_id = mlx5_os_get_umem_id(txq_obj->cq_umem);
992         cq_attr.db_umem_valid = 1;
993         cq_attr.db_umem_offset = txq_obj->cq_dbrec_offset;
994         cq_attr.db_umem_id = mlx5_os_get_umem_id(txq_obj->cq_dbrec_page->umem);
995         cq_attr.log_cq_size = rte_log2_u32(cqe_n);
996         cq_attr.log_page_size = rte_log2_u32(page_size);
997         /* Create completion queue object with DevX. */
998         cq_obj = mlx5_devx_cmd_create_cq(priv->sh->ctx, &cq_attr);
999         if (!cq_obj) {
1000                 rte_errno = errno;
1001                 DRV_LOG(ERR, "port %u Tx queue %u CQ creation failure",
1002                         dev->data->port_id, idx);
1003                 goto error;
1004         }
1005         txq_data->cqe_n = log2above(cqe_n);
1006         txq_data->cqe_s = 1 << txq_data->cqe_n;
1007         /* Initial fill CQ buffer with invalid CQE opcode. */
1008         cqe = (struct mlx5_cqe *)txq_obj->cq_buf;
1009         for (i = 0; i < txq_data->cqe_s; i++) {
1010                 cqe->op_own = (MLX5_CQE_INVALID << 4) | MLX5_CQE_OWNER_MASK;
1011                 ++cqe;
1012         }
1013         return cq_obj;
1014 error:
1015         ret = rte_errno;
1016         txq_release_devx_cq_resources(txq_obj);
1017         rte_errno = ret;
1018         return NULL;
1019 }
1020
1021 /**
1022  * Create a SQ object using DevX.
1023  *
1024  * @param dev
1025  *   Pointer to Ethernet device.
1026  * @param idx
1027  *   Queue index in DPDK Tx queue array.
1028  * @param type
1029  *   Type of the Tx queue object to create.
1030  *
1031  * @return
1032  *   The DevX object initialized, NULL otherwise and rte_errno is set.
1033  */
1034 static struct mlx5_devx_obj *
1035 mlx5_devx_sq_new(struct rte_eth_dev *dev, uint16_t idx,
1036                  struct mlx5_txq_obj *txq_obj)
1037 {
1038         struct mlx5_priv *priv = dev->data->dev_private;
1039         struct mlx5_txq_data *txq_data = (*priv->txqs)[idx];
1040         struct mlx5_devx_create_sq_attr sq_attr = { 0 };
1041         struct mlx5_devx_obj *sq_obj = NULL;
1042         size_t page_size;
1043         uint32_t wqe_n;
1044         int ret;
1045
1046         MLX5_ASSERT(txq_data);
1047         MLX5_ASSERT(txq_obj);
1048         page_size = rte_mem_page_size();
1049         if (page_size == (size_t)-1) {
1050                 DRV_LOG(ERR, "Failed to get mem page size");
1051                 rte_errno = ENOMEM;
1052                 return NULL;
1053         }
1054         wqe_n = RTE_MIN(1UL << txq_data->elts_n,
1055                         (uint32_t)priv->sh->device_attr.max_qp_wr);
1056         txq_obj->sq_buf = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO,
1057                                       wqe_n * sizeof(struct mlx5_wqe),
1058                                       page_size, priv->sh->numa_node);
1059         if (!txq_obj->sq_buf) {
1060                 DRV_LOG(ERR,
1061                         "port %u Tx queue %u cannot allocate memory (SQ)",
1062                         dev->data->port_id, txq_data->idx);
1063                 rte_errno = ENOMEM;
1064                 goto error;
1065         }
1066         /* Register allocated buffer in user space with DevX. */
1067         txq_obj->sq_umem = mlx5_glue->devx_umem_reg
1068                                         (priv->sh->ctx,
1069                                          (void *)txq_obj->sq_buf,
1070                                          wqe_n * sizeof(struct mlx5_wqe),
1071                                          IBV_ACCESS_LOCAL_WRITE);
1072         if (!txq_obj->sq_umem) {
1073                 rte_errno = errno;
1074                 DRV_LOG(ERR,
1075                         "port %u Tx queue %u cannot register memory (SQ)",
1076                         dev->data->port_id, txq_data->idx);
1077                 goto error;
1078         }
1079         /* Allocate doorbell record for send queue. */
1080         txq_obj->sq_dbrec_offset = mlx5_get_dbr(priv->sh->ctx,
1081                                                 &priv->dbrpgs,
1082                                                 &txq_obj->sq_dbrec_page);
1083         if (txq_obj->sq_dbrec_offset < 0) {
1084                 rte_errno = errno;
1085                 DRV_LOG(ERR, "Failed to allocate SQ door-bell.");
1086                 goto error;
1087         }
1088         sq_attr.tis_lst_sz = 1;
1089         sq_attr.tis_num = priv->sh->tis->id;
1090         sq_attr.state = MLX5_SQC_STATE_RST;
1091         sq_attr.cqn = txq_obj->cq_devx->id;
1092         sq_attr.flush_in_error_en = 1;
1093         sq_attr.allow_multi_pkt_send_wqe = !!priv->config.mps;
1094         sq_attr.allow_swp = !!priv->config.swp;
1095         sq_attr.min_wqe_inline_mode = priv->config.hca_attr.vport_inline_mode;
1096         sq_attr.wq_attr.uar_page =
1097                                 mlx5_os_get_devx_uar_page_id(priv->sh->tx_uar);
1098         sq_attr.wq_attr.wq_type = MLX5_WQ_TYPE_CYCLIC;
1099         sq_attr.wq_attr.pd = priv->sh->pdn;
1100         sq_attr.wq_attr.log_wq_stride = rte_log2_u32(MLX5_WQE_SIZE);
1101         sq_attr.wq_attr.log_wq_sz = log2above(wqe_n);
1102         sq_attr.wq_attr.dbr_umem_valid = 1;
1103         sq_attr.wq_attr.dbr_addr = txq_obj->sq_dbrec_offset;
1104         sq_attr.wq_attr.dbr_umem_id =
1105                         mlx5_os_get_umem_id(txq_obj->sq_dbrec_page->umem);
1106         sq_attr.wq_attr.wq_umem_valid = 1;
1107         sq_attr.wq_attr.wq_umem_id = mlx5_os_get_umem_id(txq_obj->sq_umem);
1108         sq_attr.wq_attr.wq_umem_offset = (uintptr_t)txq_obj->sq_buf % page_size;
1109         /* Create Send Queue object with DevX. */
1110         sq_obj = mlx5_devx_cmd_create_sq(priv->sh->ctx, &sq_attr);
1111         if (!sq_obj) {
1112                 rte_errno = errno;
1113                 DRV_LOG(ERR, "port %u Tx queue %u SQ creation failure",
1114                         dev->data->port_id, idx);
1115                 goto error;
1116         }
1117         txq_data->wqe_n = log2above(wqe_n);
1118         return sq_obj;
1119 error:
1120         ret = rte_errno;
1121         txq_release_devx_sq_resources(txq_obj);
1122         rte_errno = ret;
1123         return NULL;
1124 }
1125 #endif
1126
1127 /**
1128  * Create the Tx queue DevX object.
1129  *
1130  * @param dev
1131  *   Pointer to Ethernet device.
1132  * @param idx
1133  *   Queue index in DPDK Tx queue array.
1134  *
1135  * @return
1136  *   The DevX object initialised, NULL otherwise and rte_errno is set.
1137  */
1138 static struct mlx5_txq_obj *
1139 mlx5_txq_obj_devx_new(struct rte_eth_dev *dev, uint16_t idx)
1140 {
1141 #ifndef HAVE_MLX5DV_DEVX_UAR_OFFSET
1142         DRV_LOG(ERR, "port %u Tx queue %u cannot create with DevX, no UAR",
1143                      dev->data->port_id, idx);
1144         rte_errno = ENOMEM;
1145         return NULL;
1146 #else
1147         struct mlx5_priv *priv = dev->data->dev_private;
1148         struct mlx5_dev_ctx_shared *sh = priv->sh;
1149         struct mlx5_txq_data *txq_data = (*priv->txqs)[idx];
1150         struct mlx5_txq_ctrl *txq_ctrl =
1151                 container_of(txq_data, struct mlx5_txq_ctrl, txq);
1152         struct mlx5_devx_modify_sq_attr msq_attr = { 0 };
1153         struct mlx5_txq_obj *txq_obj = NULL;
1154         void *reg_addr;
1155         uint32_t cqe_n;
1156         int ret = 0;
1157
1158         MLX5_ASSERT(txq_data);
1159         MLX5_ASSERT(!txq_ctrl->obj);
1160         txq_obj = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO,
1161                               sizeof(struct mlx5_txq_obj), 0,
1162                               txq_ctrl->socket);
1163         if (!txq_obj) {
1164                 DRV_LOG(ERR,
1165                         "port %u Tx queue %u cannot allocate memory resources",
1166                         dev->data->port_id, txq_data->idx);
1167                 rte_errno = ENOMEM;
1168                 return NULL;
1169         }
1170         txq_obj->type = MLX5_TXQ_OBJ_TYPE_DEVX_SQ;
1171         txq_obj->txq_ctrl = txq_ctrl;
1172         txq_obj->dev = dev;
1173         /* Create the Completion Queue. */
1174         cqe_n = (1UL << txq_data->elts_n) / MLX5_TX_COMP_THRESH +
1175                 1 + MLX5_TX_COMP_THRESH_INLINE_DIV;
1176         /* Create completion queue object with DevX. */
1177         txq_obj->cq_devx = mlx5_devx_cq_new(dev, cqe_n, idx, txq_obj);
1178         if (!txq_obj->cq_devx) {
1179                 rte_errno = errno;
1180                 goto error;
1181         }
1182         txq_data->cqe_m = txq_data->cqe_s - 1;
1183         txq_data->cqes = (volatile struct mlx5_cqe *)txq_obj->cq_buf;
1184         txq_data->cq_ci = 0;
1185         txq_data->cq_pi = 0;
1186         txq_data->cq_db = (volatile uint32_t *)(txq_obj->cq_dbrec_page->dbrs +
1187                                                 txq_obj->cq_dbrec_offset);
1188         *txq_data->cq_db = 0;
1189         /* Create Send Queue object with DevX. */
1190         txq_obj->sq_devx = mlx5_devx_sq_new(dev, idx, txq_obj);
1191         if (!txq_obj->sq_devx) {
1192                 rte_errno = errno;
1193                 goto error;
1194         }
1195         /* Create the Work Queue. */
1196         txq_data->wqe_s = 1 << txq_data->wqe_n;
1197         txq_data->wqe_m = txq_data->wqe_s - 1;
1198         txq_data->wqes = (struct mlx5_wqe *)txq_obj->sq_buf;
1199         txq_data->wqes_end = txq_data->wqes + txq_data->wqe_s;
1200         txq_data->wqe_ci = 0;
1201         txq_data->wqe_pi = 0;
1202         txq_data->wqe_comp = 0;
1203         txq_data->wqe_thres = txq_data->wqe_s / MLX5_TX_COMP_THRESH_INLINE_DIV;
1204         txq_data->qp_db = (volatile uint32_t *)
1205                                         (txq_obj->sq_dbrec_page->dbrs +
1206                                          txq_obj->sq_dbrec_offset +
1207                                          MLX5_SND_DBR * sizeof(uint32_t));
1208         *txq_data->qp_db = 0;
1209         txq_data->qp_num_8s = txq_obj->sq_devx->id << 8;
1210         /* Change Send Queue state to Ready-to-Send. */
1211         msq_attr.sq_state = MLX5_SQC_STATE_RST;
1212         msq_attr.state = MLX5_SQC_STATE_RDY;
1213         ret = mlx5_devx_cmd_modify_sq(txq_obj->sq_devx, &msq_attr);
1214         if (ret) {
1215                 rte_errno = errno;
1216                 DRV_LOG(ERR,
1217                         "port %u Tx queue %u SP state to SQC_STATE_RDY failed",
1218                         dev->data->port_id, idx);
1219                 goto error;
1220         }
1221         txq_data->fcqs = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO,
1222                                      txq_data->cqe_s * sizeof(*txq_data->fcqs),
1223                                      RTE_CACHE_LINE_SIZE,
1224                                      txq_ctrl->socket);
1225         if (!txq_data->fcqs) {
1226                 DRV_LOG(ERR, "port %u Tx queue %u cannot allocate memory (FCQ)",
1227                         dev->data->port_id, idx);
1228                 rte_errno = ENOMEM;
1229                 goto error;
1230         }
1231 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
1232         /*
1233          * If using DevX need to query and store TIS transport domain value.
1234          * This is done once per port.
1235          * Will use this value on Rx, when creating matching TIR.
1236          */
1237         if (priv->config.devx && !priv->sh->tdn)
1238                 priv->sh->tdn = priv->sh->td->id;
1239 #endif
1240         MLX5_ASSERT(sh->tx_uar);
1241         reg_addr = mlx5_os_get_devx_uar_reg_addr(sh->tx_uar);
1242         MLX5_ASSERT(reg_addr);
1243         txq_ctrl->bf_reg = reg_addr;
1244         txq_ctrl->uar_mmap_offset =
1245                 mlx5_os_get_devx_uar_mmap_offset(sh->tx_uar);
1246         txq_uar_init(txq_ctrl);
1247         LIST_INSERT_HEAD(&priv->txqsobj, txq_obj, next);
1248         return txq_obj;
1249 error:
1250         ret = rte_errno; /* Save rte_errno before cleanup. */
1251         txq_release_devx_resources(txq_obj);
1252         if (txq_data->fcqs) {
1253                 mlx5_free(txq_data->fcqs);
1254                 txq_data->fcqs = NULL;
1255         }
1256         mlx5_free(txq_obj);
1257         rte_errno = ret; /* Restore rte_errno. */
1258         return NULL;
1259 #endif
1260 }
1261
1262 /**
1263  * Create a QP Verbs object.
1264  *
1265  * @param dev
1266  *   Pointer to Ethernet device.
1267  * @param idx
1268  *   Queue index in DPDK Tx queue array.
1269  * @param rxq_obj
1270  *   Pointer to Tx queue object data.
1271  *
1272  * @return
1273  *   The QP Verbs object initialized, NULL otherwise and rte_errno is set.
1274  */
1275 static struct ibv_qp *
1276 mlx5_ibv_qp_new(struct rte_eth_dev *dev, uint16_t idx,
1277                 struct mlx5_txq_obj *txq_obj)
1278 {
1279         struct mlx5_priv *priv = dev->data->dev_private;
1280         struct mlx5_txq_data *txq_data = (*priv->txqs)[idx];
1281         struct mlx5_txq_ctrl *txq_ctrl =
1282                         container_of(txq_data, struct mlx5_txq_ctrl, txq);
1283         struct ibv_qp *qp_obj = NULL;
1284         struct ibv_qp_init_attr_ex qp_attr = { 0 };
1285         const int desc = 1 << txq_data->elts_n;
1286
1287         MLX5_ASSERT(!txq_ctrl->obj);
1288         /* CQ to be associated with the send queue. */
1289         qp_attr.send_cq = txq_obj->cq;
1290         /* CQ to be associated with the receive queue. */
1291         qp_attr.recv_cq = txq_obj->cq;
1292         /* Max number of outstanding WRs. */
1293         qp_attr.cap.max_send_wr = ((priv->sh->device_attr.max_qp_wr < desc) ?
1294                                    priv->sh->device_attr.max_qp_wr : desc);
1295         /*
1296          * Max number of scatter/gather elements in a WR, must be 1 to prevent
1297          * libmlx5 from trying to affect must be 1 to prevent libmlx5 from
1298          * trying to affect too much memory. TX gather is not impacted by the
1299          * device_attr.max_sge limit and will still work properly.
1300          */
1301         qp_attr.cap.max_send_sge = 1;
1302         qp_attr.qp_type = IBV_QPT_RAW_PACKET,
1303         /* Do *NOT* enable this, completions events are managed per Tx burst. */
1304         qp_attr.sq_sig_all = 0;
1305         qp_attr.pd = priv->sh->pd;
1306         qp_attr.comp_mask = IBV_QP_INIT_ATTR_PD;
1307         if (txq_data->inlen_send)
1308                 qp_attr.cap.max_inline_data = txq_ctrl->max_inline_data;
1309         if (txq_data->tso_en) {
1310                 qp_attr.max_tso_header = txq_ctrl->max_tso_header;
1311                 qp_attr.comp_mask |= IBV_QP_INIT_ATTR_MAX_TSO_HEADER;
1312         }
1313         qp_obj = mlx5_glue->create_qp_ex(priv->sh->ctx, &qp_attr);
1314         if (qp_obj == NULL) {
1315                 DRV_LOG(ERR, "port %u Tx queue %u QP creation failure",
1316                         dev->data->port_id, idx);
1317                 rte_errno = errno;
1318         }
1319         return qp_obj;
1320 }
1321
1322 /**
1323  * Create the Tx queue Verbs object.
1324  *
1325  * @param dev
1326  *   Pointer to Ethernet device.
1327  * @param idx
1328  *   Queue index in DPDK Tx queue array.
1329  * @param type
1330  *   Type of the Tx queue object to create.
1331  *
1332  * @return
1333  *   The Verbs object initialised, NULL otherwise and rte_errno is set.
1334  */
1335 struct mlx5_txq_obj *
1336 mlx5_txq_obj_new(struct rte_eth_dev *dev, uint16_t idx,
1337                  enum mlx5_txq_obj_type type)
1338 {
1339         struct mlx5_priv *priv = dev->data->dev_private;
1340         struct mlx5_txq_data *txq_data = (*priv->txqs)[idx];
1341         struct mlx5_txq_ctrl *txq_ctrl =
1342                 container_of(txq_data, struct mlx5_txq_ctrl, txq);
1343         struct mlx5_txq_obj tmpl;
1344         struct mlx5_txq_obj *txq_obj = NULL;
1345         struct ibv_qp_attr mod;
1346         unsigned int cqe_n;
1347         struct mlx5dv_qp qp = { .comp_mask = MLX5DV_QP_MASK_UAR_MMAP_OFFSET };
1348         struct mlx5dv_cq cq_info;
1349         struct mlx5dv_obj obj;
1350         const int desc = 1 << txq_data->elts_n;
1351         int ret = 0;
1352
1353         if (type == MLX5_TXQ_OBJ_TYPE_DEVX_HAIRPIN)
1354                 return mlx5_txq_obj_hairpin_new(dev, idx);
1355         if (type == MLX5_TXQ_OBJ_TYPE_DEVX_SQ)
1356                 return mlx5_txq_obj_devx_new(dev, idx);
1357 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
1358         /* If using DevX, need additional mask to read tisn value. */
1359         if (priv->config.devx && !priv->sh->tdn)
1360                 qp.comp_mask |= MLX5DV_QP_MASK_RAW_QP_HANDLES;
1361 #endif
1362         MLX5_ASSERT(txq_data);
1363         priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_TX_QUEUE;
1364         priv->verbs_alloc_ctx.obj = txq_ctrl;
1365         if (mlx5_getenv_int("MLX5_ENABLE_CQE_COMPRESSION")) {
1366                 DRV_LOG(ERR,
1367                         "port %u MLX5_ENABLE_CQE_COMPRESSION must never be set",
1368                         dev->data->port_id);
1369                 rte_errno = EINVAL;
1370                 return NULL;
1371         }
1372         memset(&tmpl, 0, sizeof(struct mlx5_txq_obj));
1373         cqe_n = desc / MLX5_TX_COMP_THRESH +
1374                 1 + MLX5_TX_COMP_THRESH_INLINE_DIV;
1375         tmpl.cq = mlx5_glue->create_cq(priv->sh->ctx, cqe_n, NULL, NULL, 0);
1376         if (tmpl.cq == NULL) {
1377                 DRV_LOG(ERR, "port %u Tx queue %u CQ creation failure",
1378                         dev->data->port_id, idx);
1379                 rte_errno = errno;
1380                 goto error;
1381         }
1382         tmpl.qp = mlx5_ibv_qp_new(dev, idx, &tmpl);
1383         if (tmpl.qp == NULL) {
1384                 rte_errno = errno;
1385                 goto error;
1386         }
1387         mod = (struct ibv_qp_attr){
1388                 /* Move the QP to this state. */
1389                 .qp_state = IBV_QPS_INIT,
1390                 /* IB device port number. */
1391                 .port_num = (uint8_t)priv->dev_port,
1392         };
1393         ret = mlx5_glue->modify_qp(tmpl.qp, &mod,
1394                                    (IBV_QP_STATE | IBV_QP_PORT));
1395         if (ret) {
1396                 DRV_LOG(ERR,
1397                         "port %u Tx queue %u QP state to IBV_QPS_INIT failed",
1398                         dev->data->port_id, idx);
1399                 rte_errno = errno;
1400                 goto error;
1401         }
1402         mod = (struct ibv_qp_attr){
1403                 .qp_state = IBV_QPS_RTR
1404         };
1405         ret = mlx5_glue->modify_qp(tmpl.qp, &mod, IBV_QP_STATE);
1406         if (ret) {
1407                 DRV_LOG(ERR,
1408                         "port %u Tx queue %u QP state to IBV_QPS_RTR failed",
1409                         dev->data->port_id, idx);
1410                 rte_errno = errno;
1411                 goto error;
1412         }
1413         mod.qp_state = IBV_QPS_RTS;
1414         ret = mlx5_glue->modify_qp(tmpl.qp, &mod, IBV_QP_STATE);
1415         if (ret) {
1416                 DRV_LOG(ERR,
1417                         "port %u Tx queue %u QP state to IBV_QPS_RTS failed",
1418                         dev->data->port_id, idx);
1419                 rte_errno = errno;
1420                 goto error;
1421         }
1422         txq_obj = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO,
1423                               sizeof(struct mlx5_txq_obj), 0,
1424                               txq_ctrl->socket);
1425         if (!txq_obj) {
1426                 DRV_LOG(ERR, "port %u Tx queue %u cannot allocate memory",
1427                         dev->data->port_id, idx);
1428                 rte_errno = ENOMEM;
1429                 goto error;
1430         }
1431         obj.cq.in = tmpl.cq;
1432         obj.cq.out = &cq_info;
1433         obj.qp.in = tmpl.qp;
1434         obj.qp.out = &qp;
1435         ret = mlx5_glue->dv_init_obj(&obj, MLX5DV_OBJ_CQ | MLX5DV_OBJ_QP);
1436         if (ret != 0) {
1437                 rte_errno = errno;
1438                 goto error;
1439         }
1440         if (cq_info.cqe_size != RTE_CACHE_LINE_SIZE) {
1441                 DRV_LOG(ERR,
1442                         "port %u wrong MLX5_CQE_SIZE environment variable"
1443                         " value: it should be set to %u",
1444                         dev->data->port_id, RTE_CACHE_LINE_SIZE);
1445                 rte_errno = EINVAL;
1446                 goto error;
1447         }
1448         txq_data->cqe_n = log2above(cq_info.cqe_cnt);
1449         txq_data->cqe_s = 1 << txq_data->cqe_n;
1450         txq_data->cqe_m = txq_data->cqe_s - 1;
1451         txq_data->qp_num_8s = ((struct ibv_qp *)tmpl.qp)->qp_num << 8;
1452         txq_data->wqes = qp.sq.buf;
1453         txq_data->wqe_n = log2above(qp.sq.wqe_cnt);
1454         txq_data->wqe_s = 1 << txq_data->wqe_n;
1455         txq_data->wqe_m = txq_data->wqe_s - 1;
1456         txq_data->wqes_end = txq_data->wqes + txq_data->wqe_s;
1457         txq_data->qp_db = &qp.dbrec[MLX5_SND_DBR];
1458         txq_data->cq_db = cq_info.dbrec;
1459         txq_data->cqes = (volatile struct mlx5_cqe *)cq_info.buf;
1460         txq_data->cq_ci = 0;
1461         txq_data->cq_pi = 0;
1462         txq_data->wqe_ci = 0;
1463         txq_data->wqe_pi = 0;
1464         txq_data->wqe_comp = 0;
1465         txq_data->wqe_thres = txq_data->wqe_s / MLX5_TX_COMP_THRESH_INLINE_DIV;
1466         txq_data->fcqs = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO,
1467                                      txq_data->cqe_s * sizeof(*txq_data->fcqs),
1468                                      RTE_CACHE_LINE_SIZE, txq_ctrl->socket);
1469         if (!txq_data->fcqs) {
1470                 DRV_LOG(ERR, "port %u Tx queue %u cannot allocate memory (FCQ)",
1471                         dev->data->port_id, idx);
1472                 rte_errno = ENOMEM;
1473                 goto error;
1474         }
1475 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
1476         /*
1477          * If using DevX need to query and store TIS transport domain value.
1478          * This is done once per port.
1479          * Will use this value on Rx, when creating matching TIR.
1480          */
1481         if (priv->config.devx && !priv->sh->tdn) {
1482                 ret = mlx5_devx_cmd_qp_query_tis_td(tmpl.qp, qp.tisn,
1483                                                     &priv->sh->tdn);
1484                 if (ret) {
1485                         DRV_LOG(ERR, "Fail to query port %u Tx queue %u QP TIS "
1486                                 "transport domain", dev->data->port_id, idx);
1487                         rte_errno = EINVAL;
1488                         goto error;
1489                 } else {
1490                         DRV_LOG(DEBUG, "port %u Tx queue %u TIS number %d "
1491                                 "transport domain %d", dev->data->port_id,
1492                                 idx, qp.tisn, priv->sh->tdn);
1493                 }
1494         }
1495 #endif
1496         txq_obj->qp = tmpl.qp;
1497         txq_obj->cq = tmpl.cq;
1498         txq_ctrl->bf_reg = qp.bf.reg;
1499         if (qp.comp_mask & MLX5DV_QP_MASK_UAR_MMAP_OFFSET) {
1500                 txq_ctrl->uar_mmap_offset = qp.uar_mmap_offset;
1501                 DRV_LOG(DEBUG, "port %u: uar_mmap_offset 0x%"PRIx64,
1502                         dev->data->port_id, txq_ctrl->uar_mmap_offset);
1503         } else {
1504                 DRV_LOG(ERR,
1505                         "port %u failed to retrieve UAR info, invalid"
1506                         " libmlx5.so",
1507                         dev->data->port_id);
1508                 rte_errno = EINVAL;
1509                 goto error;
1510         }
1511         txq_uar_init(txq_ctrl);
1512         LIST_INSERT_HEAD(&priv->txqsobj, txq_obj, next);
1513         txq_obj->txq_ctrl = txq_ctrl;
1514         priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_NONE;
1515         return txq_obj;
1516 error:
1517         ret = rte_errno; /* Save rte_errno before cleanup. */
1518         if (tmpl.cq)
1519                 claim_zero(mlx5_glue->destroy_cq(tmpl.cq));
1520         if (tmpl.qp)
1521                 claim_zero(mlx5_glue->destroy_qp(tmpl.qp));
1522         if (txq_data && txq_data->fcqs) {
1523                 mlx5_free(txq_data->fcqs);
1524                 txq_data->fcqs = NULL;
1525         }
1526         if (txq_obj)
1527                 mlx5_free(txq_obj);
1528         priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_NONE;
1529         rte_errno = ret; /* Restore rte_errno. */
1530         return NULL;
1531 }
1532
1533 /**
1534  * Release an Tx verbs queue object.
1535  *
1536  * @param txq_obj
1537  *   Verbs Tx queue object..
1538  */
1539 void
1540 mlx5_txq_obj_release(struct mlx5_txq_obj *txq_obj)
1541 {
1542         MLX5_ASSERT(txq_obj);
1543         if (txq_obj->type == MLX5_TXQ_OBJ_TYPE_DEVX_HAIRPIN) {
1544                 if (txq_obj->tis)
1545                         claim_zero(mlx5_devx_cmd_destroy(txq_obj->tis));
1546         } else if (txq_obj->type == MLX5_TXQ_OBJ_TYPE_DEVX_SQ) {
1547                 txq_release_devx_resources(txq_obj);
1548         } else {
1549                 claim_zero(mlx5_glue->destroy_qp(txq_obj->qp));
1550                 claim_zero(mlx5_glue->destroy_cq(txq_obj->cq));
1551         }
1552         if (txq_obj->txq_ctrl->txq.fcqs) {
1553                 mlx5_free(txq_obj->txq_ctrl->txq.fcqs);
1554                 txq_obj->txq_ctrl->txq.fcqs = NULL;
1555         }
1556         LIST_REMOVE(txq_obj, next);
1557         mlx5_free(txq_obj);
1558 }
1559
1560 /**
1561  * Verify the Verbs Tx queue list is empty
1562  *
1563  * @param dev
1564  *   Pointer to Ethernet device.
1565  *
1566  * @return
1567  *   The number of object not released.
1568  */
1569 int
1570 mlx5_txq_obj_verify(struct rte_eth_dev *dev)
1571 {
1572         struct mlx5_priv *priv = dev->data->dev_private;
1573         int ret = 0;
1574         struct mlx5_txq_obj *txq_obj;
1575
1576         LIST_FOREACH(txq_obj, &priv->txqsobj, next) {
1577                 DRV_LOG(DEBUG, "port %u Verbs Tx queue %u still referenced",
1578                         dev->data->port_id, txq_obj->txq_ctrl->txq.idx);
1579                 ++ret;
1580         }
1581         return ret;
1582 }
1583
1584 /**
1585  * Calculate the total number of WQEBB for Tx queue.
1586  *
1587  * Simplified version of calc_sq_size() in rdma-core.
1588  *
1589  * @param txq_ctrl
1590  *   Pointer to Tx queue control structure.
1591  *
1592  * @return
1593  *   The number of WQEBB.
1594  */
1595 static int
1596 txq_calc_wqebb_cnt(struct mlx5_txq_ctrl *txq_ctrl)
1597 {
1598         unsigned int wqe_size;
1599         const unsigned int desc = 1 << txq_ctrl->txq.elts_n;
1600
1601         wqe_size = MLX5_WQE_CSEG_SIZE +
1602                    MLX5_WQE_ESEG_SIZE +
1603                    MLX5_WSEG_SIZE -
1604                    MLX5_ESEG_MIN_INLINE_SIZE +
1605                    txq_ctrl->max_inline_data;
1606         return rte_align32pow2(wqe_size * desc) / MLX5_WQE_SIZE;
1607 }
1608
1609 /**
1610  * Calculate the maximal inline data size for Tx queue.
1611  *
1612  * @param txq_ctrl
1613  *   Pointer to Tx queue control structure.
1614  *
1615  * @return
1616  *   The maximal inline data size.
1617  */
1618 static unsigned int
1619 txq_calc_inline_max(struct mlx5_txq_ctrl *txq_ctrl)
1620 {
1621         const unsigned int desc = 1 << txq_ctrl->txq.elts_n;
1622         struct mlx5_priv *priv = txq_ctrl->priv;
1623         unsigned int wqe_size;
1624
1625         wqe_size = priv->sh->device_attr.max_qp_wr / desc;
1626         if (!wqe_size)
1627                 return 0;
1628         /*
1629          * This calculation is derived from tthe source of
1630          * mlx5_calc_send_wqe() in rdma_core library.
1631          */
1632         wqe_size = wqe_size * MLX5_WQE_SIZE -
1633                    MLX5_WQE_CSEG_SIZE -
1634                    MLX5_WQE_ESEG_SIZE -
1635                    MLX5_WSEG_SIZE -
1636                    MLX5_WSEG_SIZE +
1637                    MLX5_DSEG_MIN_INLINE_SIZE;
1638         return wqe_size;
1639 }
1640
1641 /**
1642  * Set Tx queue parameters from device configuration.
1643  *
1644  * @param txq_ctrl
1645  *   Pointer to Tx queue control structure.
1646  */
1647 static void
1648 txq_set_params(struct mlx5_txq_ctrl *txq_ctrl)
1649 {
1650         struct mlx5_priv *priv = txq_ctrl->priv;
1651         struct mlx5_dev_config *config = &priv->config;
1652         unsigned int inlen_send; /* Inline data for ordinary SEND.*/
1653         unsigned int inlen_empw; /* Inline data for enhanced MPW. */
1654         unsigned int inlen_mode; /* Minimal required Inline data. */
1655         unsigned int txqs_inline; /* Min Tx queues to enable inline. */
1656         uint64_t dev_txoff = priv->dev_data->dev_conf.txmode.offloads;
1657         bool tso = txq_ctrl->txq.offloads & (DEV_TX_OFFLOAD_TCP_TSO |
1658                                             DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
1659                                             DEV_TX_OFFLOAD_GRE_TNL_TSO |
1660                                             DEV_TX_OFFLOAD_IP_TNL_TSO |
1661                                             DEV_TX_OFFLOAD_UDP_TNL_TSO);
1662         bool vlan_inline;
1663         unsigned int temp;
1664
1665         if (config->txqs_inline == MLX5_ARG_UNSET)
1666                 txqs_inline =
1667 #if defined(RTE_ARCH_ARM64)
1668                 (priv->pci_dev->id.device_id ==
1669                         PCI_DEVICE_ID_MELLANOX_CONNECTX5BF) ?
1670                         MLX5_INLINE_MAX_TXQS_BLUEFIELD :
1671 #endif
1672                         MLX5_INLINE_MAX_TXQS;
1673         else
1674                 txqs_inline = (unsigned int)config->txqs_inline;
1675         inlen_send = (config->txq_inline_max == MLX5_ARG_UNSET) ?
1676                      MLX5_SEND_DEF_INLINE_LEN :
1677                      (unsigned int)config->txq_inline_max;
1678         inlen_empw = (config->txq_inline_mpw == MLX5_ARG_UNSET) ?
1679                      MLX5_EMPW_DEF_INLINE_LEN :
1680                      (unsigned int)config->txq_inline_mpw;
1681         inlen_mode = (config->txq_inline_min == MLX5_ARG_UNSET) ?
1682                      0 : (unsigned int)config->txq_inline_min;
1683         if (config->mps != MLX5_MPW_ENHANCED && config->mps != MLX5_MPW)
1684                 inlen_empw = 0;
1685         /*
1686          * If there is requested minimal amount of data to inline
1687          * we MUST enable inlining. This is a case for ConnectX-4
1688          * which usually requires L2 inlined for correct operating
1689          * and ConnectX-4 Lx which requires L2-L4 inlined to
1690          * support E-Switch Flows.
1691          */
1692         if (inlen_mode) {
1693                 if (inlen_mode <= MLX5_ESEG_MIN_INLINE_SIZE) {
1694                         /*
1695                          * Optimize minimal inlining for single
1696                          * segment packets to fill one WQEBB
1697                          * without gaps.
1698                          */
1699                         temp = MLX5_ESEG_MIN_INLINE_SIZE;
1700                 } else {
1701                         temp = inlen_mode - MLX5_ESEG_MIN_INLINE_SIZE;
1702                         temp = RTE_ALIGN(temp, MLX5_WSEG_SIZE) +
1703                                MLX5_ESEG_MIN_INLINE_SIZE;
1704                         temp = RTE_MIN(temp, MLX5_SEND_MAX_INLINE_LEN);
1705                 }
1706                 if (temp != inlen_mode) {
1707                         DRV_LOG(INFO,
1708                                 "port %u minimal required inline setting"
1709                                 " aligned from %u to %u",
1710                                 PORT_ID(priv), inlen_mode, temp);
1711                         inlen_mode = temp;
1712                 }
1713         }
1714         /*
1715          * If port is configured to support VLAN insertion and device
1716          * does not support this feature by HW (for NICs before ConnectX-5
1717          * or in case of wqe_vlan_insert flag is not set) we must enable
1718          * data inline on all queues because it is supported by single
1719          * tx_burst routine.
1720          */
1721         txq_ctrl->txq.vlan_en = config->hw_vlan_insert;
1722         vlan_inline = (dev_txoff & DEV_TX_OFFLOAD_VLAN_INSERT) &&
1723                       !config->hw_vlan_insert;
1724         /*
1725          * If there are few Tx queues it is prioritized
1726          * to save CPU cycles and disable data inlining at all.
1727          */
1728         if (inlen_send && priv->txqs_n >= txqs_inline) {
1729                 /*
1730                  * The data sent with ordinal MLX5_OPCODE_SEND
1731                  * may be inlined in Ethernet Segment, align the
1732                  * length accordingly to fit entire WQEBBs.
1733                  */
1734                 temp = RTE_MAX(inlen_send,
1735                                MLX5_ESEG_MIN_INLINE_SIZE + MLX5_WQE_DSEG_SIZE);
1736                 temp -= MLX5_ESEG_MIN_INLINE_SIZE + MLX5_WQE_DSEG_SIZE;
1737                 temp = RTE_ALIGN(temp, MLX5_WQE_SIZE);
1738                 temp += MLX5_ESEG_MIN_INLINE_SIZE + MLX5_WQE_DSEG_SIZE;
1739                 temp = RTE_MIN(temp, MLX5_WQE_SIZE_MAX +
1740                                      MLX5_ESEG_MIN_INLINE_SIZE -
1741                                      MLX5_WQE_CSEG_SIZE -
1742                                      MLX5_WQE_ESEG_SIZE -
1743                                      MLX5_WQE_DSEG_SIZE * 2);
1744                 temp = RTE_MIN(temp, MLX5_SEND_MAX_INLINE_LEN);
1745                 temp = RTE_MAX(temp, inlen_mode);
1746                 if (temp != inlen_send) {
1747                         DRV_LOG(INFO,
1748                                 "port %u ordinary send inline setting"
1749                                 " aligned from %u to %u",
1750                                 PORT_ID(priv), inlen_send, temp);
1751                         inlen_send = temp;
1752                 }
1753                 /*
1754                  * Not aligned to cache lines, but to WQEs.
1755                  * First bytes of data (initial alignment)
1756                  * is going to be copied explicitly at the
1757                  * beginning of inlining buffer in Ethernet
1758                  * Segment.
1759                  */
1760                 MLX5_ASSERT(inlen_send >= MLX5_ESEG_MIN_INLINE_SIZE);
1761                 MLX5_ASSERT(inlen_send <= MLX5_WQE_SIZE_MAX +
1762                                           MLX5_ESEG_MIN_INLINE_SIZE -
1763                                           MLX5_WQE_CSEG_SIZE -
1764                                           MLX5_WQE_ESEG_SIZE -
1765                                           MLX5_WQE_DSEG_SIZE * 2);
1766         } else if (inlen_mode) {
1767                 /*
1768                  * If minimal inlining is requested we must
1769                  * enable inlining in general, despite the
1770                  * number of configured queues. Ignore the
1771                  * txq_inline_max devarg, this is not
1772                  * full-featured inline.
1773                  */
1774                 inlen_send = inlen_mode;
1775                 inlen_empw = 0;
1776         } else if (vlan_inline) {
1777                 /*
1778                  * Hardware does not report offload for
1779                  * VLAN insertion, we must enable data inline
1780                  * to implement feature by software.
1781                  */
1782                 inlen_send = MLX5_ESEG_MIN_INLINE_SIZE;
1783                 inlen_empw = 0;
1784         } else {
1785                 inlen_send = 0;
1786                 inlen_empw = 0;
1787         }
1788         txq_ctrl->txq.inlen_send = inlen_send;
1789         txq_ctrl->txq.inlen_mode = inlen_mode;
1790         txq_ctrl->txq.inlen_empw = 0;
1791         if (inlen_send && inlen_empw && priv->txqs_n >= txqs_inline) {
1792                 /*
1793                  * The data sent with MLX5_OPCODE_ENHANCED_MPSW
1794                  * may be inlined in Data Segment, align the
1795                  * length accordingly to fit entire WQEBBs.
1796                  */
1797                 temp = RTE_MAX(inlen_empw,
1798                                MLX5_WQE_SIZE + MLX5_DSEG_MIN_INLINE_SIZE);
1799                 temp -= MLX5_DSEG_MIN_INLINE_SIZE;
1800                 temp = RTE_ALIGN(temp, MLX5_WQE_SIZE);
1801                 temp += MLX5_DSEG_MIN_INLINE_SIZE;
1802                 temp = RTE_MIN(temp, MLX5_WQE_SIZE_MAX +
1803                                      MLX5_DSEG_MIN_INLINE_SIZE -
1804                                      MLX5_WQE_CSEG_SIZE -
1805                                      MLX5_WQE_ESEG_SIZE -
1806                                      MLX5_WQE_DSEG_SIZE);
1807                 temp = RTE_MIN(temp, MLX5_EMPW_MAX_INLINE_LEN);
1808                 if (temp != inlen_empw) {
1809                         DRV_LOG(INFO,
1810                                 "port %u enhanced empw inline setting"
1811                                 " aligned from %u to %u",
1812                                 PORT_ID(priv), inlen_empw, temp);
1813                         inlen_empw = temp;
1814                 }
1815                 MLX5_ASSERT(inlen_empw >= MLX5_ESEG_MIN_INLINE_SIZE);
1816                 MLX5_ASSERT(inlen_empw <= MLX5_WQE_SIZE_MAX +
1817                                           MLX5_DSEG_MIN_INLINE_SIZE -
1818                                           MLX5_WQE_CSEG_SIZE -
1819                                           MLX5_WQE_ESEG_SIZE -
1820                                           MLX5_WQE_DSEG_SIZE);
1821                 txq_ctrl->txq.inlen_empw = inlen_empw;
1822         }
1823         txq_ctrl->max_inline_data = RTE_MAX(inlen_send, inlen_empw);
1824         if (tso) {
1825                 txq_ctrl->max_tso_header = MLX5_MAX_TSO_HEADER;
1826                 txq_ctrl->max_inline_data = RTE_MAX(txq_ctrl->max_inline_data,
1827                                                     MLX5_MAX_TSO_HEADER);
1828                 txq_ctrl->txq.tso_en = 1;
1829         }
1830         txq_ctrl->txq.tunnel_en = config->tunnel_en | config->swp;
1831         txq_ctrl->txq.swp_en = ((DEV_TX_OFFLOAD_IP_TNL_TSO |
1832                                  DEV_TX_OFFLOAD_UDP_TNL_TSO |
1833                                  DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM) &
1834                                 txq_ctrl->txq.offloads) && config->swp;
1835 }
1836
1837 /**
1838  * Adjust Tx queue data inline parameters for large queue sizes.
1839  * The data inline feature requires multiple WQEs to fit the packets,
1840  * and if the large amount of Tx descriptors is requested by application
1841  * the total WQE amount may exceed the hardware capabilities. If the
1842  * default inline setting are used we can try to adjust these ones and
1843  * meet the hardware requirements and not exceed the queue size.
1844  *
1845  * @param txq_ctrl
1846  *   Pointer to Tx queue control structure.
1847  *
1848  * @return
1849  *   Zero on success, otherwise the parameters can not be adjusted.
1850  */
1851 static int
1852 txq_adjust_params(struct mlx5_txq_ctrl *txq_ctrl)
1853 {
1854         struct mlx5_priv *priv = txq_ctrl->priv;
1855         struct mlx5_dev_config *config = &priv->config;
1856         unsigned int max_inline;
1857
1858         max_inline = txq_calc_inline_max(txq_ctrl);
1859         if (!txq_ctrl->txq.inlen_send) {
1860                 /*
1861                  * Inline data feature is not engaged at all.
1862                  * There is nothing to adjust.
1863                  */
1864                 return 0;
1865         }
1866         if (txq_ctrl->max_inline_data <= max_inline) {
1867                 /*
1868                  * The requested inline data length does not
1869                  * exceed queue capabilities.
1870                  */
1871                 return 0;
1872         }
1873         if (txq_ctrl->txq.inlen_mode > max_inline) {
1874                 DRV_LOG(ERR,
1875                         "minimal data inline requirements (%u) are not"
1876                         " satisfied (%u) on port %u, try the smaller"
1877                         " Tx queue size (%d)",
1878                         txq_ctrl->txq.inlen_mode, max_inline,
1879                         priv->dev_data->port_id,
1880                         priv->sh->device_attr.max_qp_wr);
1881                 goto error;
1882         }
1883         if (txq_ctrl->txq.inlen_send > max_inline &&
1884             config->txq_inline_max != MLX5_ARG_UNSET &&
1885             config->txq_inline_max > (int)max_inline) {
1886                 DRV_LOG(ERR,
1887                         "txq_inline_max requirements (%u) are not"
1888                         " satisfied (%u) on port %u, try the smaller"
1889                         " Tx queue size (%d)",
1890                         txq_ctrl->txq.inlen_send, max_inline,
1891                         priv->dev_data->port_id,
1892                         priv->sh->device_attr.max_qp_wr);
1893                 goto error;
1894         }
1895         if (txq_ctrl->txq.inlen_empw > max_inline &&
1896             config->txq_inline_mpw != MLX5_ARG_UNSET &&
1897             config->txq_inline_mpw > (int)max_inline) {
1898                 DRV_LOG(ERR,
1899                         "txq_inline_mpw requirements (%u) are not"
1900                         " satisfied (%u) on port %u, try the smaller"
1901                         " Tx queue size (%d)",
1902                         txq_ctrl->txq.inlen_empw, max_inline,
1903                         priv->dev_data->port_id,
1904                         priv->sh->device_attr.max_qp_wr);
1905                 goto error;
1906         }
1907         if (txq_ctrl->txq.tso_en && max_inline < MLX5_MAX_TSO_HEADER) {
1908                 DRV_LOG(ERR,
1909                         "tso header inline requirements (%u) are not"
1910                         " satisfied (%u) on port %u, try the smaller"
1911                         " Tx queue size (%d)",
1912                         MLX5_MAX_TSO_HEADER, max_inline,
1913                         priv->dev_data->port_id,
1914                         priv->sh->device_attr.max_qp_wr);
1915                 goto error;
1916         }
1917         if (txq_ctrl->txq.inlen_send > max_inline) {
1918                 DRV_LOG(WARNING,
1919                         "adjust txq_inline_max (%u->%u)"
1920                         " due to large Tx queue on port %u",
1921                         txq_ctrl->txq.inlen_send, max_inline,
1922                         priv->dev_data->port_id);
1923                 txq_ctrl->txq.inlen_send = max_inline;
1924         }
1925         if (txq_ctrl->txq.inlen_empw > max_inline) {
1926                 DRV_LOG(WARNING,
1927                         "adjust txq_inline_mpw (%u->%u)"
1928                         "due to large Tx queue on port %u",
1929                         txq_ctrl->txq.inlen_empw, max_inline,
1930                         priv->dev_data->port_id);
1931                 txq_ctrl->txq.inlen_empw = max_inline;
1932         }
1933         txq_ctrl->max_inline_data = RTE_MAX(txq_ctrl->txq.inlen_send,
1934                                             txq_ctrl->txq.inlen_empw);
1935         MLX5_ASSERT(txq_ctrl->max_inline_data <= max_inline);
1936         MLX5_ASSERT(txq_ctrl->txq.inlen_mode <= max_inline);
1937         MLX5_ASSERT(txq_ctrl->txq.inlen_mode <= txq_ctrl->txq.inlen_send);
1938         MLX5_ASSERT(txq_ctrl->txq.inlen_mode <= txq_ctrl->txq.inlen_empw ||
1939                     !txq_ctrl->txq.inlen_empw);
1940         return 0;
1941 error:
1942         rte_errno = ENOMEM;
1943         return -ENOMEM;
1944 }
1945
1946 /**
1947  * Create a DPDK Tx queue.
1948  *
1949  * @param dev
1950  *   Pointer to Ethernet device.
1951  * @param idx
1952  *   TX queue index.
1953  * @param desc
1954  *   Number of descriptors to configure in queue.
1955  * @param socket
1956  *   NUMA socket on which memory must be allocated.
1957  * @param[in] conf
1958  *  Thresholds parameters.
1959  *
1960  * @return
1961  *   A DPDK queue object on success, NULL otherwise and rte_errno is set.
1962  */
1963 struct mlx5_txq_ctrl *
1964 mlx5_txq_new(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1965              unsigned int socket, const struct rte_eth_txconf *conf)
1966 {
1967         struct mlx5_priv *priv = dev->data->dev_private;
1968         struct mlx5_txq_ctrl *tmpl;
1969
1970         tmpl = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO, sizeof(*tmpl) +
1971                            desc * sizeof(struct rte_mbuf *), 0, socket);
1972         if (!tmpl) {
1973                 rte_errno = ENOMEM;
1974                 return NULL;
1975         }
1976         if (mlx5_mr_btree_init(&tmpl->txq.mr_ctrl.cache_bh,
1977                                MLX5_MR_BTREE_CACHE_N, socket)) {
1978                 /* rte_errno is already set. */
1979                 goto error;
1980         }
1981         /* Save pointer of global generation number to check memory event. */
1982         tmpl->txq.mr_ctrl.dev_gen_ptr = &priv->sh->share_cache.dev_gen;
1983         MLX5_ASSERT(desc > MLX5_TX_COMP_THRESH);
1984         tmpl->txq.offloads = conf->offloads |
1985                              dev->data->dev_conf.txmode.offloads;
1986         tmpl->priv = priv;
1987         tmpl->socket = socket;
1988         tmpl->txq.elts_n = log2above(desc);
1989         tmpl->txq.elts_s = desc;
1990         tmpl->txq.elts_m = desc - 1;
1991         tmpl->txq.port_id = dev->data->port_id;
1992         tmpl->txq.idx = idx;
1993         txq_set_params(tmpl);
1994         if (txq_adjust_params(tmpl))
1995                 goto error;
1996         if (txq_calc_wqebb_cnt(tmpl) >
1997             priv->sh->device_attr.max_qp_wr) {
1998                 DRV_LOG(ERR,
1999                         "port %u Tx WQEBB count (%d) exceeds the limit (%d),"
2000                         " try smaller queue size",
2001                         dev->data->port_id, txq_calc_wqebb_cnt(tmpl),
2002                         priv->sh->device_attr.max_qp_wr);
2003                 rte_errno = ENOMEM;
2004                 goto error;
2005         }
2006         rte_atomic32_inc(&tmpl->refcnt);
2007         tmpl->type = MLX5_TXQ_TYPE_STANDARD;
2008         LIST_INSERT_HEAD(&priv->txqsctrl, tmpl, next);
2009         return tmpl;
2010 error:
2011         mlx5_free(tmpl);
2012         return NULL;
2013 }
2014
2015 /**
2016  * Create a DPDK Tx hairpin queue.
2017  *
2018  * @param dev
2019  *   Pointer to Ethernet device.
2020  * @param idx
2021  *   TX queue index.
2022  * @param desc
2023  *   Number of descriptors to configure in queue.
2024  * @param hairpin_conf
2025  *  The hairpin configuration.
2026  *
2027  * @return
2028  *   A DPDK queue object on success, NULL otherwise and rte_errno is set.
2029  */
2030 struct mlx5_txq_ctrl *
2031 mlx5_txq_hairpin_new(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
2032                      const struct rte_eth_hairpin_conf *hairpin_conf)
2033 {
2034         struct mlx5_priv *priv = dev->data->dev_private;
2035         struct mlx5_txq_ctrl *tmpl;
2036
2037         tmpl = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO, sizeof(*tmpl), 0,
2038                            SOCKET_ID_ANY);
2039         if (!tmpl) {
2040                 rte_errno = ENOMEM;
2041                 return NULL;
2042         }
2043         tmpl->priv = priv;
2044         tmpl->socket = SOCKET_ID_ANY;
2045         tmpl->txq.elts_n = log2above(desc);
2046         tmpl->txq.port_id = dev->data->port_id;
2047         tmpl->txq.idx = idx;
2048         tmpl->hairpin_conf = *hairpin_conf;
2049         tmpl->type = MLX5_TXQ_TYPE_HAIRPIN;
2050         rte_atomic32_inc(&tmpl->refcnt);
2051         LIST_INSERT_HEAD(&priv->txqsctrl, tmpl, next);
2052         return tmpl;
2053 }
2054
2055 /**
2056  * Get a Tx queue.
2057  *
2058  * @param dev
2059  *   Pointer to Ethernet device.
2060  * @param idx
2061  *   TX queue index.
2062  *
2063  * @return
2064  *   A pointer to the queue if it exists.
2065  */
2066 struct mlx5_txq_ctrl *
2067 mlx5_txq_get(struct rte_eth_dev *dev, uint16_t idx)
2068 {
2069         struct mlx5_priv *priv = dev->data->dev_private;
2070         struct mlx5_txq_data *txq_data = (*priv->txqs)[idx];
2071         struct mlx5_txq_ctrl *ctrl = NULL;
2072
2073         if (txq_data) {
2074                 ctrl = container_of(txq_data, struct mlx5_txq_ctrl, txq);
2075                 rte_atomic32_inc(&ctrl->refcnt);
2076         }
2077         return ctrl;
2078 }
2079
2080 /**
2081  * Release a Tx queue.
2082  *
2083  * @param dev
2084  *   Pointer to Ethernet device.
2085  * @param idx
2086  *   TX queue index.
2087  *
2088  * @return
2089  *   1 while a reference on it exists, 0 when freed.
2090  */
2091 int
2092 mlx5_txq_release(struct rte_eth_dev *dev, uint16_t idx)
2093 {
2094         struct mlx5_priv *priv = dev->data->dev_private;
2095         struct mlx5_txq_ctrl *txq;
2096
2097         if (!(*priv->txqs)[idx])
2098                 return 0;
2099         txq = container_of((*priv->txqs)[idx], struct mlx5_txq_ctrl, txq);
2100         if (!rte_atomic32_dec_and_test(&txq->refcnt))
2101                 return 1;
2102         if (txq->obj) {
2103                 mlx5_txq_obj_release(txq->obj);
2104                 txq->obj = NULL;
2105         }
2106         txq_free_elts(txq);
2107         mlx5_mr_btree_free(&txq->txq.mr_ctrl.cache_bh);
2108         LIST_REMOVE(txq, next);
2109         mlx5_free(txq);
2110         (*priv->txqs)[idx] = NULL;
2111         dev->data->tx_queue_state[idx] = RTE_ETH_QUEUE_STATE_STOPPED;
2112         return 0;
2113 }
2114
2115 /**
2116  * Verify if the queue can be released.
2117  *
2118  * @param dev
2119  *   Pointer to Ethernet device.
2120  * @param idx
2121  *   TX queue index.
2122  *
2123  * @return
2124  *   1 if the queue can be released.
2125  */
2126 int
2127 mlx5_txq_releasable(struct rte_eth_dev *dev, uint16_t idx)
2128 {
2129         struct mlx5_priv *priv = dev->data->dev_private;
2130         struct mlx5_txq_ctrl *txq;
2131
2132         if (!(*priv->txqs)[idx])
2133                 return -1;
2134         txq = container_of((*priv->txqs)[idx], struct mlx5_txq_ctrl, txq);
2135         return (rte_atomic32_read(&txq->refcnt) == 1);
2136 }
2137
2138 /**
2139  * Verify the Tx Queue list is empty
2140  *
2141  * @param dev
2142  *   Pointer to Ethernet device.
2143  *
2144  * @return
2145  *   The number of object not released.
2146  */
2147 int
2148 mlx5_txq_verify(struct rte_eth_dev *dev)
2149 {
2150         struct mlx5_priv *priv = dev->data->dev_private;
2151         struct mlx5_txq_ctrl *txq_ctrl;
2152         int ret = 0;
2153
2154         LIST_FOREACH(txq_ctrl, &priv->txqsctrl, next) {
2155                 DRV_LOG(DEBUG, "port %u Tx queue %u still referenced",
2156                         dev->data->port_id, txq_ctrl->txq.idx);
2157                 ++ret;
2158         }
2159         return ret;
2160 }
2161
2162 /**
2163  * Set the Tx queue dynamic timestamp (mask and offset)
2164  *
2165  * @param[in] dev
2166  *   Pointer to the Ethernet device structure.
2167  */
2168 void
2169 mlx5_txq_dynf_timestamp_set(struct rte_eth_dev *dev)
2170 {
2171         struct mlx5_priv *priv = dev->data->dev_private;
2172         struct mlx5_dev_ctx_shared *sh = priv->sh;
2173         struct mlx5_txq_data *data;
2174         int off, nbit;
2175         unsigned int i;
2176         uint64_t mask = 0;
2177
2178         nbit = rte_mbuf_dynflag_lookup
2179                                 (RTE_MBUF_DYNFLAG_TX_TIMESTAMP_NAME, NULL);
2180         off = rte_mbuf_dynfield_lookup
2181                                 (RTE_MBUF_DYNFIELD_TIMESTAMP_NAME, NULL);
2182         if (nbit > 0 && off >= 0 && sh->txpp.refcnt)
2183                 mask = 1ULL << nbit;
2184         for (i = 0; i != priv->txqs_n; ++i) {
2185                 data = (*priv->txqs)[i];
2186                 if (!data)
2187                         continue;
2188                 data->sh = sh;
2189                 data->ts_mask = mask;
2190                 data->ts_offset = off;
2191         }
2192 }