LCOV - code coverage report
Current view: top level - drivers/net/mlx5 - mlx5_flow_aso.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 0 797 0.0 %
Date: 2026-11-01 19:11:22 Functions: 0 45 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 564 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright 2020 Mellanox Technologies, Ltd
       3                 :            :  */
       4                 :            : #include <mlx5_prm.h>
       5                 :            : #include <rte_malloc.h>
       6                 :            : #include <rte_cycles.h>
       7                 :            : #include <rte_eal_paging.h>
       8                 :            : 
       9                 :            : #include <mlx5_malloc.h>
      10                 :            : #include <mlx5_common_os.h>
      11                 :            : #include <mlx5_common_devx.h>
      12                 :            : 
      13                 :            : #include "mlx5.h"
      14                 :            : #include "mlx5_flow.h"
      15                 :            : #include "mlx5_hws_cnt.h"
      16                 :            : 
      17                 :            : #define MLX5_ASO_CNT_QUEUE_LOG_DESC 14
      18                 :            : 
      19                 :            : /**
      20                 :            :  * Free MR resources.
      21                 :            :  *
      22                 :            :  * @param[in] mr
      23                 :            :  *   MR to free.
      24                 :            :  */
      25                 :            : static void
      26                 :          0 : mlx5_aso_dereg_mr(struct mlx5_pmd_mr *mr)
      27                 :            : {
      28                 :          0 :         void *addr = mr->addr;
      29                 :            : 
      30                 :          0 :         mlx5_os_dereg_mr(mr);
      31                 :          0 :         mlx5_free(addr);
      32                 :            :         memset(mr, 0, sizeof(*mr));
      33                 :          0 : }
      34                 :            : 
      35                 :            : /**
      36                 :            :  * Register Memory Region.
      37                 :            :  *
      38                 :            :  * @param[in] cdev
      39                 :            :  *   Pointer to the mlx5 common device.
      40                 :            :  * @param[in] length
      41                 :            :  *   Size of MR buffer.
      42                 :            :  * @param[in/out] mr
      43                 :            :  *   Pointer to MR to create.
      44                 :            :  *
      45                 :            :  * @return
      46                 :            :  *   0 on success, a negative errno value otherwise and rte_errno is set.
      47                 :            :  */
      48                 :            : static int
      49                 :          0 : mlx5_aso_reg_mr(struct mlx5_common_device *cdev, size_t length,
      50                 :            :                 struct mlx5_pmd_mr *mr)
      51                 :            : {
      52                 :            :         int ret;
      53                 :            : 
      54                 :          0 :         mr->addr = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO, length, 4096,
      55                 :            :                                SOCKET_ID_ANY);
      56         [ #  # ]:          0 :         if (!mr->addr) {
      57                 :          0 :                 DRV_LOG(ERR, "Failed to create ASO bits mem for MR.");
      58                 :          0 :                 return -1;
      59                 :            :         }
      60                 :          0 :         ret = mlx5_os_reg_mr(cdev->pd, mr->addr, length, mr);
      61         [ #  # ]:          0 :         if (ret) {
      62                 :          0 :                 DRV_LOG(ERR, "Failed to create direct Mkey.");
      63                 :          0 :                 mlx5_free(mr->addr);
      64                 :          0 :                 return -1;
      65                 :            :         }
      66                 :            :         return 0;
      67                 :            : }
      68                 :            : 
      69                 :            : /**
      70                 :            :  * Destroy Send Queue used for ASO access.
      71                 :            :  *
      72                 :            :  * @param[in] sq
      73                 :            :  *   ASO SQ to destroy.
      74                 :            :  */
      75                 :            : void
      76                 :          0 : mlx5_aso_destroy_sq(struct mlx5_aso_sq *sq)
      77                 :            : {
      78                 :          0 :         mlx5_devx_sq_destroy(&sq->sq_obj);
      79                 :          0 :         mlx5_devx_cq_destroy(&sq->cq.cq_obj);
      80                 :            :         memset(sq, 0, sizeof(*sq));
      81                 :          0 : }
      82                 :            : 
      83                 :            : /**
      84                 :            :  * Initialize Send Queue used for ASO access counter.
      85                 :            :  *
      86                 :            :  * @param[in] sq
      87                 :            :  *   ASO SQ to initialize.
      88                 :            :  */
      89                 :            : static void
      90                 :          0 : mlx5_aso_cnt_init_sq(struct mlx5_aso_sq *sq)
      91                 :            : {
      92                 :            :         volatile struct mlx5_aso_wqe *restrict wqe;
      93                 :            :         int i;
      94                 :          0 :         int size = 1 << sq->log_desc_n;
      95                 :            : 
      96                 :            :         /* All the next fields state should stay constant. */
      97         [ #  # ]:          0 :         for (i = 0, wqe = &sq->sq_obj.aso_wqes[0]; i < size; ++i, ++wqe) {
      98         [ #  # ]:          0 :                 wqe->general_cseg.sq_ds = rte_cpu_to_be_32((sq->sqn << 8) |
      99                 :            :                                                           (sizeof(*wqe) >> 4));
     100                 :          0 :                 wqe->aso_cseg.operand_masks = rte_cpu_to_be_32
     101                 :            :                         (0u |
     102                 :            :                          (ASO_OPER_LOGICAL_OR << ASO_CSEG_COND_OPER_OFFSET) |
     103                 :            :                          (ASO_OP_ALWAYS_FALSE << ASO_CSEG_COND_1_OPER_OFFSET) |
     104                 :            :                          (ASO_OP_ALWAYS_FALSE << ASO_CSEG_COND_0_OPER_OFFSET) |
     105                 :            :                          (BYTEWISE_64BYTE << ASO_CSEG_DATA_MASK_MODE_OFFSET));
     106                 :          0 :                 wqe->aso_cseg.data_mask = RTE_BE64(UINT64_MAX);
     107                 :            :         }
     108                 :          0 : }
     109                 :            : 
     110                 :            : /**
     111                 :            :  * Initialize Send Queue used for ASO access.
     112                 :            :  *
     113                 :            :  * @param[in] sq
     114                 :            :  *   ASO SQ to initialize.
     115                 :            :  */
     116                 :            : static void
     117                 :          0 : mlx5_aso_age_init_sq(struct mlx5_aso_sq *sq)
     118                 :            : {
     119                 :            :         volatile struct mlx5_aso_wqe *restrict wqe;
     120                 :            :         int i;
     121                 :          0 :         int size = 1 << sq->log_desc_n;
     122                 :            :         uint64_t addr;
     123                 :            : 
     124                 :            :         /* All the next fields state should stay constant. */
     125         [ #  # ]:          0 :         for (i = 0, wqe = &sq->sq_obj.aso_wqes[0]; i < size; ++i, ++wqe) {
     126         [ #  # ]:          0 :                 wqe->general_cseg.sq_ds = rte_cpu_to_be_32((sq->sqn << 8) |
     127                 :            :                                                           (sizeof(*wqe) >> 4));
     128         [ #  # ]:          0 :                 wqe->aso_cseg.lkey = rte_cpu_to_be_32(sq->mr.lkey);
     129                 :          0 :                 addr = (uint64_t)((uint64_t *)sq->mr.addr + i *
     130                 :          0 :                                             MLX5_ASO_AGE_ACTIONS_PER_POOL / 64);
     131         [ #  # ]:          0 :                 wqe->aso_cseg.va_h = rte_cpu_to_be_32((uint32_t)(addr >> 32));
     132         [ #  # ]:          0 :                 wqe->aso_cseg.va_l_r = rte_cpu_to_be_32((uint32_t)addr | 1u);
     133                 :          0 :                 wqe->aso_cseg.operand_masks = rte_cpu_to_be_32
     134                 :            :                         (0u |
     135                 :            :                          (ASO_OPER_LOGICAL_OR << ASO_CSEG_COND_OPER_OFFSET) |
     136                 :            :                          (ASO_OP_ALWAYS_TRUE << ASO_CSEG_COND_1_OPER_OFFSET) |
     137                 :            :                          (ASO_OP_ALWAYS_TRUE << ASO_CSEG_COND_0_OPER_OFFSET) |
     138                 :            :                          (BYTEWISE_64BYTE << ASO_CSEG_DATA_MASK_MODE_OFFSET));
     139                 :          0 :                 wqe->aso_cseg.data_mask = RTE_BE64(UINT64_MAX);
     140                 :            :         }
     141                 :          0 : }
     142                 :            : 
     143                 :            : /**
     144                 :            :  * Initialize Send Queue used for ASO flow meter access.
     145                 :            :  *
     146                 :            :  * @param[in] sq
     147                 :            :  *   ASO SQ to initialize.
     148                 :            :  */
     149                 :            : void
     150                 :          0 : mlx5_aso_mtr_init_sq(struct mlx5_aso_sq *sq)
     151                 :            : {
     152                 :            :         volatile struct mlx5_aso_wqe *restrict wqe;
     153                 :            :         int i;
     154                 :          0 :         int size = 1 << sq->log_desc_n;
     155                 :            : 
     156                 :            :         /* All the next fields state should stay constant. */
     157         [ #  # ]:          0 :         for (i = 0, wqe = &sq->sq_obj.aso_wqes[0]; i < size; ++i, ++wqe) {
     158         [ #  # ]:          0 :                 wqe->general_cseg.sq_ds = rte_cpu_to_be_32((sq->sqn << 8) |
     159                 :            :                                                           (sizeof(*wqe) >> 4));
     160                 :          0 :                 wqe->aso_cseg.operand_masks = RTE_BE32(0u |
     161                 :            :                          (ASO_OPER_LOGICAL_OR << ASO_CSEG_COND_OPER_OFFSET) |
     162                 :            :                          (ASO_OP_ALWAYS_TRUE << ASO_CSEG_COND_1_OPER_OFFSET) |
     163                 :            :                          (ASO_OP_ALWAYS_TRUE << ASO_CSEG_COND_0_OPER_OFFSET) |
     164                 :            :                          (BYTEWISE_64BYTE << ASO_CSEG_DATA_MASK_MODE_OFFSET));
     165                 :          0 :                 wqe->general_cseg.flags = RTE_BE32(MLX5_COMP_ALWAYS <<
     166                 :            :                                                          MLX5_COMP_MODE_OFFSET);
     167                 :            :         }
     168                 :          0 : }
     169                 :            : 
     170                 :            : /*
     171                 :            :  * Initialize Send Queue used for ASO connection tracking.
     172                 :            :  *
     173                 :            :  * @param[in] sq
     174                 :            :  *   ASO SQ to initialize.
     175                 :            :  */
     176                 :            : static void
     177                 :          0 : mlx5_aso_ct_init_sq(struct mlx5_aso_sq *sq)
     178                 :            : {
     179                 :            :         volatile struct mlx5_aso_wqe *restrict wqe;
     180                 :            :         int i;
     181                 :          0 :         int size = 1 << sq->log_desc_n;
     182                 :            :         uint64_t addr;
     183                 :            : 
     184                 :            :         /* All the next fields state should stay constant. */
     185         [ #  # ]:          0 :         for (i = 0, wqe = &sq->sq_obj.aso_wqes[0]; i < size; ++i, ++wqe) {
     186         [ #  # ]:          0 :                 wqe->general_cseg.sq_ds = rte_cpu_to_be_32((sq->sqn << 8) |
     187                 :            :                                                           (sizeof(*wqe) >> 4));
     188                 :            :                 /* One unique MR for the query data. */
     189         [ #  # ]:          0 :                 wqe->aso_cseg.lkey = rte_cpu_to_be_32(sq->mr.lkey);
     190                 :            :                 /* Magic number 64 represents the length of a ASO CT obj. */
     191                 :          0 :                 addr = (uint64_t)((uintptr_t)sq->mr.addr + i * 64);
     192         [ #  # ]:          0 :                 wqe->aso_cseg.va_h = rte_cpu_to_be_32((uint32_t)(addr >> 32));
     193         [ #  # ]:          0 :                 wqe->aso_cseg.va_l_r = rte_cpu_to_be_32((uint32_t)addr | 1u);
     194                 :            :                 /*
     195                 :            :                  * The values of operand_masks are different for modify
     196                 :            :                  * and query.
     197                 :            :                  * And data_mask may be different for each modification. In
     198                 :            :                  * query, it could be zero and ignored.
     199                 :            :                  * CQE generation is always needed, in order to decide when
     200                 :            :                  * it is available to create the flow or read the data.
     201                 :            :                  */
     202                 :          0 :                 wqe->general_cseg.flags = RTE_BE32(MLX5_COMP_ALWAYS <<
     203                 :            :                                                    MLX5_COMP_MODE_OFFSET);
     204                 :            :         }
     205                 :          0 : }
     206                 :            : 
     207                 :            : /**
     208                 :            :  * Create Send Queue used for ASO access.
     209                 :            :  *
     210                 :            :  * @param[in] cdev
     211                 :            :  *   Pointer to the mlx5 common device.
     212                 :            :  * @param[in/out] sq
     213                 :            :  *   Pointer to SQ to create.
     214                 :            :  * @param[in] uar
     215                 :            :  *   User Access Region object.
     216                 :            :  *
     217                 :            :  * @return
     218                 :            :  *   0 on success, a negative errno value otherwise and rte_errno is set.
     219                 :            :  */
     220                 :            : int
     221                 :          0 : mlx5_aso_sq_create(struct mlx5_common_device *cdev, struct mlx5_aso_sq *sq,
     222                 :            :                    void *uar, uint16_t log_desc_n)
     223                 :            : {
     224         [ #  # ]:          0 :         struct mlx5_devx_cq_attr cq_attr = {
     225                 :            :                 .uar_page_id = mlx5_os_get_devx_uar_page_id(uar),
     226                 :            :         };
     227                 :          0 :         struct mlx5_devx_create_sq_attr sq_attr = {
     228                 :            :                 .user_index = 0xFFFF,
     229                 :            :                 .wq_attr = (struct mlx5_devx_wq_attr){
     230                 :          0 :                         .pd = cdev->pdn,
     231                 :          0 :                         .uar_page = mlx5_os_get_devx_uar_page_id(uar),
     232                 :            :                 },
     233                 :            :                 .ts_format =
     234                 :          0 :                         mlx5_ts_format_conv(cdev->config.hca_attr.sq_ts_format),
     235                 :            :         };
     236                 :          0 :         struct mlx5_devx_modify_sq_attr modify_attr = {
     237                 :            :                 .state = MLX5_SQC_STATE_RDY,
     238                 :            :         };
     239                 :            :         uint16_t log_wqbb_n;
     240                 :            :         int ret;
     241                 :            : 
     242         [ #  # ]:          0 :         if (mlx5_devx_cq_create(cdev->ctx, &sq->cq.cq_obj,
     243                 :            :                                 log_desc_n, &cq_attr,
     244                 :            :                                 SOCKET_ID_ANY))
     245                 :          0 :                 goto error;
     246                 :          0 :         sq->cq.cq_ci = 0;
     247                 :          0 :         sq->cq.log_desc_n = log_desc_n;
     248                 :          0 :         sq->log_desc_n = log_desc_n;
     249                 :          0 :         sq_attr.cqn = sq->cq.cq_obj.cq->id;
     250                 :            :         /* for mlx5_aso_wqe that is twice the size of mlx5_wqe */
     251                 :          0 :         log_wqbb_n = sq->log_desc_n + 1;
     252                 :          0 :         ret = mlx5_devx_sq_create(cdev->ctx, &sq->sq_obj, log_wqbb_n, &sq_attr,
     253                 :            :                                   SOCKET_ID_ANY);
     254         [ #  # ]:          0 :         if (ret) {
     255                 :          0 :                 DRV_LOG(ERR, "Can't create SQ object.");
     256                 :          0 :                 rte_errno = ENOMEM;
     257                 :          0 :                 goto error;
     258                 :            :         }
     259                 :          0 :         ret = mlx5_devx_cmd_modify_sq(sq->sq_obj.sq, &modify_attr);
     260         [ #  # ]:          0 :         if (ret) {
     261                 :          0 :                 DRV_LOG(ERR, "Can't change SQ state to ready.");
     262                 :          0 :                 rte_errno = ENOMEM;
     263                 :          0 :                 goto error;
     264                 :            :         }
     265                 :          0 :         sq->pi = 0;
     266                 :          0 :         sq->head = 0;
     267                 :          0 :         sq->tail = 0;
     268                 :          0 :         sq->sqn = sq->sq_obj.sq->id;
     269                 :            :         rte_spinlock_init(&sq->sqsl);
     270                 :          0 :         return 0;
     271                 :          0 : error:
     272                 :          0 :         mlx5_aso_destroy_sq(sq);
     273                 :          0 :         return -1;
     274                 :            : }
     275                 :            : 
     276                 :            : void
     277                 :          0 : mlx5_aso_mtr_queue_uninit(struct mlx5_dev_ctx_shared *sh __rte_unused,
     278                 :            :                           struct mlx5_aso_mtr_pool *hws_pool,
     279                 :            :                           struct mlx5_aso_mtr_pools_mng *pool_mng)
     280                 :            : {
     281                 :            :         uint32_t i;
     282                 :            : 
     283         [ #  # ]:          0 :         if (hws_pool) {
     284         [ #  # ]:          0 :                 for (i = 0; i < hws_pool->nb_sq; i++)
     285                 :          0 :                         mlx5_aso_destroy_sq(hws_pool->sq + i);
     286                 :          0 :                 mlx5_free(hws_pool->sq);
     287                 :          0 :                 return;
     288                 :            :         }
     289         [ #  # ]:          0 :         if (pool_mng)
     290                 :          0 :                 mlx5_aso_destroy_sq(&pool_mng->sq);
     291                 :            : }
     292                 :            : 
     293                 :            : int
     294                 :          0 : mlx5_aso_mtr_queue_init(struct mlx5_dev_ctx_shared *sh,
     295                 :            :                                 struct mlx5_aso_mtr_pool *hws_pool,
     296                 :            :                                 struct mlx5_aso_mtr_pools_mng *pool_mng,
     297                 :            :                                 uint32_t nb_queues)
     298                 :            : {
     299                 :          0 :         struct mlx5_common_device *cdev = sh->cdev;
     300                 :            :         struct mlx5_aso_sq *sq;
     301                 :            :         uint32_t i;
     302                 :            : 
     303         [ #  # ]:          0 :         if (hws_pool) {
     304                 :          0 :                 sq = mlx5_malloc(MLX5_MEM_ZERO,
     305                 :            :                         sizeof(struct mlx5_aso_sq) * nb_queues,
     306                 :            :                         RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
     307         [ #  # ]:          0 :                 if (!sq)
     308                 :            :                         return -1;
     309                 :          0 :                 hws_pool->sq = sq;
     310         [ #  # ]:          0 :                 for (i = 0; i < nb_queues; i++) {
     311         [ #  # ]:          0 :                         if (mlx5_aso_sq_create(cdev, hws_pool->sq + i,
     312                 :            :                                                sh->tx_uar.obj,
     313                 :            :                                                MLX5_ASO_QUEUE_LOG_DESC))
     314                 :          0 :                                 goto error;
     315                 :          0 :                         mlx5_aso_mtr_init_sq(hws_pool->sq + i);
     316                 :            :                 }
     317                 :          0 :                 hws_pool->nb_sq = nb_queues;
     318                 :            :         }
     319                 :            :         /*
     320                 :            :          * pools_mng->sq is shared by every port on the device.
     321                 :            :          * A send-queue id of zero is valid, so the DevX object pointer
     322                 :            :          * is the indicator that the queue was already created.
     323                 :            :          */
     324   [ #  #  #  # ]:          0 :         if (pool_mng != NULL && pool_mng->sq.sq_obj.sq == NULL) {
     325         [ #  # ]:          0 :                 if (mlx5_aso_sq_create(cdev, &pool_mng->sq,
     326                 :            :                                        sh->tx_uar.obj,
     327                 :            :                                        MLX5_ASO_QUEUE_LOG_DESC))
     328                 :            :                         return -1;
     329                 :          0 :                 mlx5_aso_mtr_init_sq(&pool_mng->sq);
     330                 :            :         }
     331                 :            :         return 0;
     332                 :            : error:
     333                 :            :         do {
     334                 :          0 :                 mlx5_aso_destroy_sq(hws_pool->sq + i);
     335         [ #  # ]:          0 :         } while (i--);
     336                 :            :         return -1;
     337                 :            : }
     338                 :            : 
     339                 :            : /**
     340                 :            :  * API to create and initialize Send Queue used for ASO access.
     341                 :            :  *
     342                 :            :  * @param[in] sh
     343                 :            :  *   Pointer to shared device context.
     344                 :            :  * @param[in] aso_opc_mod
     345                 :            :  *   Mode of ASO feature.
     346                 :            :  * @param[in] nb_queues
     347                 :            :  *   Number of Send Queues to create.
     348                 :            :  *
     349                 :            :  * @return
     350                 :            :  *   0 on success, a negative errno value otherwise and rte_errno is set.
     351                 :            :  */
     352                 :            : int
     353                 :          0 : mlx5_aso_queue_init(struct mlx5_dev_ctx_shared *sh,
     354                 :            :                     enum mlx5_access_aso_opc_mod aso_opc_mod,
     355                 :            :                         uint32_t nb_queues)
     356                 :            : {
     357                 :            :         uint32_t sq_desc_n = 1 << MLX5_ASO_QUEUE_LOG_DESC;
     358                 :          0 :         struct mlx5_common_device *cdev = sh->cdev;
     359                 :            : 
     360   [ #  #  #  # ]:          0 :         switch (aso_opc_mod) {
     361                 :          0 :         case ASO_OPC_MOD_FLOW_HIT:
     362         [ #  # ]:          0 :                 if (mlx5_aso_reg_mr(cdev, (MLX5_ASO_AGE_ACTIONS_PER_POOL / 8) *
     363                 :          0 :                                     sq_desc_n, &sh->aso_age_mng->aso_sq.mr))
     364                 :            :                         return -1;
     365         [ #  # ]:          0 :                 if (mlx5_aso_sq_create(cdev, &sh->aso_age_mng->aso_sq,
     366                 :            :                                        sh->tx_uar.obj,
     367                 :            :                                        MLX5_ASO_QUEUE_LOG_DESC)) {
     368                 :          0 :                         mlx5_aso_dereg_mr(&sh->aso_age_mng->aso_sq.mr);
     369                 :          0 :                         return -1;
     370                 :            :                 }
     371                 :          0 :                 mlx5_aso_age_init_sq(&sh->aso_age_mng->aso_sq);
     372                 :          0 :                 break;
     373                 :          0 :         case ASO_OPC_MOD_POLICER:
     374         [ #  # ]:          0 :                 if (mlx5_aso_mtr_queue_init(sh, NULL,
     375                 :          0 :                                             &sh->mtrmng->pools_mng, nb_queues))
     376                 :          0 :                         return -1;
     377                 :            :                 break;
     378                 :          0 :         case ASO_OPC_MOD_CONNECTION_TRACKING:
     379         [ #  # ]:          0 :                 if (mlx5_aso_ct_queue_init(sh, sh->ct_mng, MLX5_ASO_CT_SQ_NUM))
     380                 :          0 :                         return -1;
     381                 :            :                 break;
     382                 :          0 :         default:
     383                 :          0 :                 DRV_LOG(ERR, "Unknown ASO operation mode");
     384                 :          0 :                 return -1;
     385                 :            :         }
     386                 :            :         return 0;
     387                 :            : }
     388                 :            : 
     389                 :            : /**
     390                 :            :  * API to destroy Send Queue used for ASO access.
     391                 :            :  *
     392                 :            :  * @param[in] sh
     393                 :            :  *   Pointer to shared device context.
     394                 :            :  * @param[in] aso_opc_mod
     395                 :            :  *   Mode of ASO feature.
     396                 :            :  */
     397                 :            : void
     398                 :          0 : mlx5_aso_queue_uninit(struct mlx5_dev_ctx_shared *sh,
     399                 :            :                       enum mlx5_access_aso_opc_mod aso_opc_mod)
     400                 :            : {
     401                 :            :         struct mlx5_aso_sq *sq = NULL;
     402                 :            : 
     403   [ #  #  #  # ]:          0 :         switch (aso_opc_mod) {
     404                 :          0 :         case ASO_OPC_MOD_FLOW_HIT:
     405                 :          0 :                 mlx5_aso_dereg_mr(&sh->aso_age_mng->aso_sq.mr);
     406                 :          0 :                 sq = &sh->aso_age_mng->aso_sq;
     407                 :            :                 break;
     408                 :          0 :         case ASO_OPC_MOD_POLICER:
     409                 :          0 :                 mlx5_aso_mtr_queue_uninit(sh, NULL, &sh->mtrmng->pools_mng);
     410                 :            :                 break;
     411                 :          0 :         case ASO_OPC_MOD_CONNECTION_TRACKING:
     412                 :          0 :                 mlx5_aso_ct_queue_uninit(sh->ct_mng);
     413                 :            :                 break;
     414                 :          0 :         default:
     415                 :          0 :                 DRV_LOG(ERR, "Unknown ASO operation mode");
     416                 :          0 :                 return;
     417                 :            :         }
     418                 :            :         if (sq)
     419                 :          0 :                 mlx5_aso_destroy_sq(sq);
     420                 :            : }
     421                 :            : 
     422                 :            : /**
     423                 :            :  * Write a burst of WQEs to ASO SQ.
     424                 :            :  *
     425                 :            :  * @param[in] sh
     426                 :            :  *   Pointer to shared device context.
     427                 :            :  * @param[in] n
     428                 :            :  *   Index of the last valid pool.
     429                 :            :  *
     430                 :            :  * @return
     431                 :            :  *   Number of WQEs in burst.
     432                 :            :  */
     433                 :            : static uint16_t
     434                 :          0 : mlx5_aso_sq_enqueue_burst(struct mlx5_dev_ctx_shared *sh, uint16_t n)
     435                 :            : {
     436                 :          0 :         struct mlx5_aso_age_mng *mng = sh->aso_age_mng;
     437                 :            :         volatile struct mlx5_aso_wqe *wqe;
     438                 :            :         struct mlx5_aso_sq *sq = &mng->aso_sq;
     439                 :            :         struct mlx5_aso_age_pool *pool;
     440                 :          0 :         uint16_t size = 1 << sq->log_desc_n;
     441                 :          0 :         uint16_t mask = size - 1;
     442                 :            :         uint16_t max;
     443                 :          0 :         uint16_t start_head = sq->head;
     444                 :            : 
     445                 :          0 :         max = RTE_MIN(size - (uint16_t)(sq->head - sq->tail), n - sq->next);
     446         [ #  # ]:          0 :         if (unlikely(!max))
     447                 :            :                 return 0;
     448                 :          0 :         sq->elts[start_head & mask].burst_size = max;
     449                 :            :         do {
     450                 :          0 :                 wqe = &sq->sq_obj.aso_wqes[sq->head & mask];
     451                 :          0 :                 rte_prefetch0(&sq->sq_obj.aso_wqes[(sq->head + 1) & mask]);
     452                 :            :                 /* Fill next WQE. */
     453                 :          0 :                 rte_rwlock_read_lock(&mng->resize_rwl);
     454         [ #  # ]:          0 :                 pool = mng->pools[sq->next];
     455                 :            :                 rte_rwlock_read_unlock(&mng->resize_rwl);
     456                 :          0 :                 sq->elts[sq->head & mask].pool = pool;
     457                 :          0 :                 wqe->general_cseg.misc =
     458         [ #  # ]:          0 :                                 rte_cpu_to_be_32(((struct mlx5_devx_obj *)
     459                 :            :                                                  (pool->flow_hit_aso_obj))->id);
     460                 :          0 :                 wqe->general_cseg.flags = RTE_BE32(MLX5_COMP_ONLY_FIRST_ERR <<
     461                 :            :                                                          MLX5_COMP_MODE_OFFSET);
     462         [ #  # ]:          0 :                 wqe->general_cseg.opcode = rte_cpu_to_be_32
     463                 :            :                                                 (MLX5_OPCODE_ACCESS_ASO |
     464                 :            :                                                  (ASO_OPC_MOD_FLOW_HIT <<
     465                 :            :                                                   WQE_CSEG_OPC_MOD_OFFSET) |
     466                 :            :                                                  (sq->pi <<
     467                 :            :                                                   WQE_CSEG_WQE_INDEX_OFFSET));
     468                 :          0 :                 sq->pi += 2; /* Each WQE contains 2 WQEBB's. */
     469                 :          0 :                 sq->head++;
     470                 :          0 :                 sq->next++;
     471                 :          0 :                 max--;
     472         [ #  # ]:          0 :         } while (max);
     473                 :          0 :         wqe->general_cseg.flags = RTE_BE32(MLX5_COMP_ALWAYS <<
     474                 :            :                                                          MLX5_COMP_MODE_OFFSET);
     475                 :          0 :         mlx5_doorbell_ring(&sh->tx_uar.bf_db, *(volatile uint64_t *)wqe,
     476                 :          0 :                            sq->pi, &sq->sq_obj.db_rec[MLX5_SND_DBR],
     477                 :          0 :                            !sh->tx_uar.dbnc);
     478                 :          0 :         return sq->elts[start_head & mask].burst_size;
     479                 :            : }
     480                 :            : 
     481                 :            : /**
     482                 :            :  * Debug utility function. Dump contents of error CQE and WQE.
     483                 :            :  *
     484                 :            :  * @param[in] cqe
     485                 :            :  *   Error CQE to dump.
     486                 :            :  * @param[in] wqe
     487                 :            :  *   Error WQE to dump.
     488                 :            :  */
     489                 :            : static void
     490                 :          0 : mlx5_aso_dump_err_objs(volatile uint32_t *cqe, volatile uint32_t *wqe)
     491                 :            : {
     492                 :            :         int i;
     493                 :            : 
     494                 :          0 :         DRV_LOG(ERR, "Error cqe:");
     495         [ #  # ]:          0 :         for (i = 0; i < (int)sizeof(struct mlx5_error_cqe) / 4; i += 4)
     496                 :          0 :                 DRV_LOG(ERR, "%08X %08X %08X %08X", cqe[i], cqe[i + 1],
     497                 :            :                         cqe[i + 2], cqe[i + 3]);
     498                 :          0 :         DRV_LOG(ERR, "\nError wqe:");
     499         [ #  # ]:          0 :         for (i = 0; i < (int)sizeof(struct mlx5_aso_wqe) / 4; i += 4)
     500                 :          0 :                 DRV_LOG(ERR, "%08X %08X %08X %08X", wqe[i], wqe[i + 1],
     501                 :            :                         wqe[i + 2], wqe[i + 3]);
     502                 :          0 : }
     503                 :            : 
     504                 :            : /**
     505                 :            :  * Handle case of error CQE.
     506                 :            :  *
     507                 :            :  * @param[in] sq
     508                 :            :  *   ASO SQ to use.
     509                 :            :  */
     510                 :            : void
     511                 :          0 : mlx5_aso_cqe_err_handle(struct mlx5_aso_sq *sq)
     512                 :            : {
     513                 :            :         struct mlx5_aso_cq *cq = &sq->cq;
     514                 :          0 :         uint32_t idx = cq->cq_ci & ((1 << cq->log_desc_n) - 1);
     515                 :          0 :         volatile struct mlx5_error_cqe *cqe =
     516                 :          0 :                         (volatile struct mlx5_error_cqe *)&cq->cq_obj.cqes[idx];
     517                 :            : 
     518                 :          0 :         cq->errors++;
     519                 :          0 :         idx = rte_be_to_cpu_16(cqe->wqe_counter) & (1u << sq->log_desc_n);
     520                 :          0 :         mlx5_aso_dump_err_objs((volatile uint32_t *)cqe,
     521                 :          0 :                                (volatile uint32_t *)&sq->sq_obj.aso_wqes[idx]);
     522                 :          0 : }
     523                 :            : 
     524                 :            : int
     525                 :          0 : mlx5_aso_pull_completion(struct mlx5_aso_sq *sq,
     526                 :            :                          struct rte_flow_op_result res[],
     527                 :            :                          uint16_t n_res)
     528                 :            : {
     529                 :            :         struct mlx5_aso_cq *cq = &sq->cq;
     530                 :            :         volatile struct mlx5_cqe *restrict cqe;
     531                 :          0 :         const uint32_t cq_size = 1 << cq->log_desc_n;
     532                 :          0 :         const uint32_t mask = cq_size - 1;
     533                 :            :         uint32_t idx;
     534                 :            :         uint32_t next_idx;
     535                 :            :         uint16_t max;
     536                 :            :         uint16_t n = 0;
     537                 :            :         int ret;
     538                 :            : 
     539                 :          0 :         max = (uint16_t)(sq->head - sq->tail);
     540         [ #  # ]:          0 :         if (unlikely(!max || !n_res))
     541                 :            :                 return 0;
     542                 :          0 :         next_idx = cq->cq_ci & mask;
     543                 :            :         do {
     544                 :            :                 idx = next_idx;
     545                 :          0 :                 next_idx = (cq->cq_ci + 1) & mask;
     546                 :            :                 /* Need to confirm the position of the prefetch. */
     547                 :          0 :                 rte_prefetch0(&cq->cq_obj.cqes[next_idx]);
     548                 :          0 :                 cqe = &cq->cq_obj.cqes[idx];
     549         [ #  # ]:          0 :                 ret = check_cqe(cqe, cq_size, cq->cq_ci);
     550                 :            :                 /*
     551                 :            :                  * Be sure owner read is done before any other cookie field or
     552                 :            :                  * opaque field.
     553                 :            :                  */
     554                 :          0 :                 rte_io_rmb();
     555                 :            :                 if (ret == MLX5_CQE_STATUS_HW_OWN)
     556                 :            :                         break;
     557                 :          0 :                 res[n].user_data = sq->elts[(uint16_t)((sq->tail + n) & mask)].user_data;
     558         [ #  # ]:          0 :                 if (unlikely(ret != MLX5_CQE_STATUS_SW_OWN)) {
     559                 :          0 :                         mlx5_aso_cqe_err_handle(sq);
     560                 :          0 :                         res[n].status = RTE_FLOW_OP_ERROR;
     561                 :            :                 } else {
     562                 :          0 :                         res[n].status = RTE_FLOW_OP_SUCCESS;
     563                 :            :                 }
     564                 :          0 :                 cq->cq_ci++;
     565         [ #  # ]:          0 :                 if (++n == n_res)
     566                 :            :                         break;
     567                 :            :         } while (1);
     568         [ #  # ]:          0 :         if (likely(n)) {
     569                 :          0 :                 sq->tail += n;
     570                 :          0 :                 rte_io_wmb();
     571         [ #  # ]:          0 :                 cq->cq_obj.db_rec[0] = rte_cpu_to_be_32(cq->cq_ci);
     572                 :            :         }
     573                 :          0 :         return n;
     574                 :            : }
     575                 :            : 
     576                 :            : void
     577                 :          0 : mlx5_aso_push_wqe(struct mlx5_dev_ctx_shared *sh,
     578                 :            :                   struct mlx5_aso_sq *sq)
     579                 :            : {
     580         [ #  # ]:          0 :         if (sq->db_pi == sq->pi)
     581                 :            :                 return;
     582                 :          0 :         mlx5_doorbell_ring(&sh->tx_uar.bf_db, *(volatile uint64_t *)sq->db,
     583                 :          0 :                            sq->pi, &sq->sq_obj.db_rec[MLX5_SND_DBR],
     584                 :          0 :                            !sh->tx_uar.dbnc);
     585                 :          0 :         sq->db_pi = sq->pi;
     586                 :            : }
     587                 :            : 
     588                 :            : /**
     589                 :            :  * Update ASO objects upon completion.
     590                 :            :  *
     591                 :            :  * @param[in] sh
     592                 :            :  *   Shared device context.
     593                 :            :  * @param[in] n
     594                 :            :  *   Number of completed ASO objects.
     595                 :            :  */
     596                 :            : static void
     597                 :          0 : mlx5_aso_age_action_update(struct mlx5_dev_ctx_shared *sh, uint16_t n)
     598                 :            : {
     599                 :          0 :         struct mlx5_aso_age_mng *mng = sh->aso_age_mng;
     600                 :            :         struct mlx5_aso_sq *sq = &mng->aso_sq;
     601                 :            :         struct mlx5_age_info *age_info;
     602                 :          0 :         const uint16_t size = 1 << sq->log_desc_n;
     603                 :          0 :         const uint16_t mask = size - 1;
     604                 :          0 :         const uint64_t curr = MLX5_CURR_TIME_SEC;
     605                 :            :         uint16_t expected = AGE_CANDIDATE;
     606                 :            :         uint16_t i;
     607                 :            : 
     608         [ #  # ]:          0 :         for (i = 0; i < n; ++i) {
     609                 :          0 :                 uint16_t idx = (sq->tail + i) & mask;
     610                 :          0 :                 struct mlx5_aso_age_pool *pool = sq->elts[idx].pool;
     611                 :          0 :                 uint64_t diff = curr - pool->time_of_last_age_check;
     612                 :          0 :                 uint64_t *addr = sq->mr.addr;
     613                 :            :                 int j;
     614                 :            : 
     615                 :          0 :                 addr += idx * MLX5_ASO_AGE_ACTIONS_PER_POOL / 64;
     616                 :          0 :                 pool->time_of_last_age_check = curr;
     617         [ #  # ]:          0 :                 for (j = 0; j < MLX5_ASO_AGE_ACTIONS_PER_POOL; j++) {
     618                 :          0 :                         struct mlx5_aso_age_action *act = &pool->actions[j];
     619                 :            :                         struct mlx5_age_param *ap = &act->age_params;
     620                 :            :                         uint8_t byte;
     621                 :            :                         uint8_t offset;
     622                 :            :                         uint8_t *u8addr;
     623                 :            :                         uint8_t hit;
     624                 :            : 
     625         [ #  # ]:          0 :                         if (rte_atomic_load_explicit(&ap->state, rte_memory_order_relaxed) !=
     626                 :            :                                             AGE_CANDIDATE)
     627                 :          0 :                                 continue;
     628                 :          0 :                         byte = 63 - (j / 8);
     629                 :          0 :                         offset = j % 8;
     630                 :            :                         u8addr = (uint8_t *)addr;
     631                 :          0 :                         hit = (u8addr[byte] >> offset) & 0x1;
     632         [ #  # ]:          0 :                         if (hit) {
     633                 :          0 :                                 rte_atomic_store_explicit(&ap->sec_since_last_hit, 0,
     634                 :            :                                                  rte_memory_order_relaxed);
     635                 :            :                         } else {
     636                 :            :                                 struct mlx5_priv *priv;
     637                 :            : 
     638                 :          0 :                                 rte_atomic_fetch_add_explicit(&ap->sec_since_last_hit,
     639                 :            :                                                    diff, rte_memory_order_relaxed);
     640                 :            :                                 /* If timeout passed add to aged-out list. */
     641         [ #  # ]:          0 :                                 if (ap->sec_since_last_hit <= ap->timeout)
     642                 :          0 :                                         continue;
     643                 :          0 :                                 priv =
     644                 :          0 :                                 rte_eth_devices[ap->port_id].data->dev_private;
     645                 :          0 :                                 age_info = GET_PORT_AGE_INFO(priv);
     646                 :          0 :                                 rte_spinlock_lock(&age_info->aged_sl);
     647         [ #  # ]:          0 :                                 if (rte_atomic_compare_exchange_strong_explicit(&ap->state,
     648                 :            :                                                                 &expected,
     649                 :            :                                                                 AGE_TMOUT,
     650                 :            :                                                                rte_memory_order_relaxed,
     651                 :            :                                                             rte_memory_order_relaxed)) {
     652         [ #  # ]:          0 :                                         LIST_INSERT_HEAD(&age_info->aged_aso,
     653                 :            :                                                          act, next);
     654                 :          0 :                                         MLX5_AGE_SET(age_info,
     655                 :            :                                                      MLX5_AGE_EVENT_NEW);
     656                 :            :                                 }
     657                 :            :                                 rte_spinlock_unlock(&age_info->aged_sl);
     658                 :            :                         }
     659                 :            :                 }
     660                 :            :         }
     661                 :          0 :         mlx5_age_event_prepare(sh);
     662                 :          0 : }
     663                 :            : 
     664                 :            : /**
     665                 :            :  * Handle completions from WQEs sent to ASO SQ.
     666                 :            :  *
     667                 :            :  * @param[in] sh
     668                 :            :  *   Shared device context.
     669                 :            :  *
     670                 :            :  * @return
     671                 :            :  *   Number of CQEs handled.
     672                 :            :  */
     673                 :            : static uint16_t
     674                 :          0 : mlx5_aso_completion_handle(struct mlx5_dev_ctx_shared *sh)
     675                 :            : {
     676                 :          0 :         struct mlx5_aso_age_mng *mng = sh->aso_age_mng;
     677                 :          0 :         struct mlx5_aso_sq *sq = &mng->aso_sq;
     678                 :            :         struct mlx5_aso_cq *cq = &sq->cq;
     679                 :            :         volatile struct mlx5_cqe *restrict cqe;
     680                 :          0 :         const unsigned int cq_size = 1 << cq->log_desc_n;
     681                 :          0 :         const unsigned int mask = cq_size - 1;
     682                 :            :         uint32_t idx;
     683                 :          0 :         uint32_t next_idx = cq->cq_ci & mask;
     684                 :          0 :         const uint16_t max = (uint16_t)(sq->head - sq->tail);
     685                 :            :         uint16_t i = 0;
     686                 :            :         int ret;
     687         [ #  # ]:          0 :         if (unlikely(!max))
     688                 :            :                 return 0;
     689                 :            :         do {
     690                 :          0 :                 idx = next_idx;
     691                 :          0 :                 next_idx = (cq->cq_ci + 1) & mask;
     692                 :          0 :                 rte_prefetch0(&cq->cq_obj.cqes[next_idx]);
     693                 :          0 :                 cqe = &cq->cq_obj.cqes[idx];
     694         [ #  # ]:          0 :                 ret = check_cqe(cqe, cq_size, cq->cq_ci);
     695                 :            :                 /*
     696                 :            :                  * Be sure owner read is done before any other cookie field or
     697                 :            :                  * opaque field.
     698                 :            :                  */
     699                 :          0 :                 rte_io_rmb();
     700         [ #  # ]:          0 :                 if (unlikely(ret != MLX5_CQE_STATUS_SW_OWN)) {
     701         [ #  # ]:          0 :                         if (likely(ret == MLX5_CQE_STATUS_HW_OWN))
     702                 :            :                                 break;
     703                 :          0 :                         mlx5_aso_cqe_err_handle(sq);
     704                 :            :                 } else {
     705                 :          0 :                         i += sq->elts[(sq->tail + i) & mask].burst_size;
     706                 :            :                 }
     707                 :          0 :                 cq->cq_ci++;
     708                 :            :         } while (1);
     709         [ #  # ]:          0 :         if (likely(i)) {
     710                 :          0 :                 mlx5_aso_age_action_update(sh, i);
     711                 :          0 :                 sq->tail += i;
     712                 :          0 :                 rte_io_wmb();
     713         [ #  # ]:          0 :                 cq->cq_obj.db_rec[0] = rte_cpu_to_be_32(cq->cq_ci);
     714                 :            :         }
     715                 :            :         return i;
     716                 :            : }
     717                 :            : 
     718                 :            : /**
     719                 :            :  * Periodically read CQEs and send WQEs to ASO SQ.
     720                 :            :  *
     721                 :            :  * @param[in] arg
     722                 :            :  *   Shared device context containing the ASO SQ.
     723                 :            :  */
     724                 :            : static void
     725                 :          0 : mlx5_flow_aso_alarm(void *arg)
     726                 :            : {
     727                 :            :         struct mlx5_dev_ctx_shared *sh = arg;
     728                 :          0 :         struct mlx5_aso_sq *sq = &sh->aso_age_mng->aso_sq;
     729                 :            :         uint32_t us = 100u;
     730                 :            :         uint16_t n;
     731                 :            : 
     732                 :          0 :         rte_rwlock_read_lock(&sh->aso_age_mng->resize_rwl);
     733                 :          0 :         n = sh->aso_age_mng->next;
     734                 :            :         rte_rwlock_read_unlock(&sh->aso_age_mng->resize_rwl);
     735                 :          0 :         mlx5_aso_completion_handle(sh);
     736         [ #  # ]:          0 :         if (sq->next == n) {
     737                 :            :                 /* End of loop: wait 1 second. */
     738                 :            :                 us = US_PER_S;
     739                 :          0 :                 sq->next = 0;
     740                 :            :         }
     741                 :          0 :         mlx5_aso_sq_enqueue_burst(sh, n);
     742         [ #  # ]:          0 :         if (rte_eal_alarm_set(us, mlx5_flow_aso_alarm, sh))
     743                 :          0 :                 DRV_LOG(ERR, "Cannot reinitialize aso alarm.");
     744                 :          0 : }
     745                 :            : 
     746                 :            : /**
     747                 :            :  * API to start ASO access using ASO SQ.
     748                 :            :  *
     749                 :            :  * @param[in] sh
     750                 :            :  *   Pointer to shared device context.
     751                 :            :  *
     752                 :            :  * @return
     753                 :            :  *   0 on success, a negative errno value otherwise and rte_errno is set.
     754                 :            :  */
     755                 :            : int
     756                 :          0 : mlx5_aso_flow_hit_queue_poll_start(struct mlx5_dev_ctx_shared *sh)
     757                 :            : {
     758         [ #  # ]:          0 :         if (rte_eal_alarm_set(US_PER_S, mlx5_flow_aso_alarm, sh)) {
     759                 :          0 :                 DRV_LOG(ERR, "Cannot reinitialize ASO age alarm.");
     760                 :          0 :                 return -rte_errno;
     761                 :            :         }
     762                 :            :         return 0;
     763                 :            : }
     764                 :            : 
     765                 :            : /**
     766                 :            :  * API to stop ASO access using ASO SQ.
     767                 :            :  *
     768                 :            :  * @param[in] sh
     769                 :            :  *   Pointer to shared device context.
     770                 :            :  *
     771                 :            :  * @return
     772                 :            :  *   0 on success, a negative errno value otherwise and rte_errno is set.
     773                 :            :  */
     774                 :            : int
     775                 :          0 : mlx5_aso_flow_hit_queue_poll_stop(struct mlx5_dev_ctx_shared *sh)
     776                 :            : {
     777                 :            :         int retries = 1024;
     778                 :            : 
     779         [ #  # ]:          0 :         if (!sh->aso_age_mng->aso_sq.sq_obj.sq)
     780                 :            :                 return -EINVAL;
     781                 :          0 :         rte_errno = 0;
     782         [ #  # ]:          0 :         while (--retries) {
     783                 :          0 :                 rte_eal_alarm_cancel(mlx5_flow_aso_alarm, sh);
     784         [ #  # ]:          0 :                 if (rte_errno != EINPROGRESS)
     785                 :            :                         break;
     786                 :            :                 rte_pause();
     787                 :            :         }
     788                 :          0 :         return -rte_errno;
     789                 :            : }
     790                 :            : 
     791                 :            : static uint16_t
     792                 :          0 : mlx5_aso_mtr_sq_enqueue_single(struct mlx5_dev_ctx_shared *sh,
     793                 :            :                                struct mlx5_aso_sq *sq,
     794                 :            :                                struct mlx5_aso_mtr *aso_mtr,
     795                 :            :                                struct mlx5_mtr_bulk *bulk,
     796                 :            :                                bool need_lock,
     797                 :            :                                struct mlx5_hw_q_job *job,
     798                 :            :                                bool push)
     799                 :            : {
     800                 :            :         volatile struct mlx5_aso_wqe *wqe = NULL;
     801                 :            :         struct mlx5_flow_meter_info *fm = NULL;
     802                 :            :         struct mlx5_flow_meter_profile *fmp;
     803                 :          0 :         uint16_t size = 1 << sq->log_desc_n;
     804                 :          0 :         uint16_t mask = size - 1;
     805                 :            :         uint16_t res;
     806                 :            :         uint32_t dseg_idx = 0;
     807                 :            :         struct mlx5_aso_mtr_pool *pool = NULL;
     808                 :            :         uint32_t param_le;
     809                 :            :         int id;
     810                 :            : 
     811         [ #  # ]:          0 :         if (need_lock)
     812                 :          0 :                 rte_spinlock_lock(&sq->sqsl);
     813                 :          0 :         res = size - (uint16_t)(sq->head - sq->tail);
     814         [ #  # ]:          0 :         if (unlikely(!res)) {
     815                 :          0 :                 DRV_LOG(ERR, "Fail: SQ is full and no free WQE to send");
     816         [ #  # ]:          0 :                 if (need_lock)
     817                 :          0 :                         rte_spinlock_unlock(&sq->sqsl);
     818                 :          0 :                 return 0;
     819                 :            :         }
     820                 :          0 :         wqe = &sq->sq_obj.aso_wqes[sq->head & mask];
     821                 :          0 :         rte_prefetch0(&sq->sq_obj.aso_wqes[(sq->head + 1) & mask]);
     822                 :            :         /* Fill next WQE. */
     823                 :            :         fm = &aso_mtr->fm;
     824         [ #  # ]:          0 :         sq->elts[sq->head & mask].user_data = job ? job : (void *)aso_mtr;
     825         [ #  # ]:          0 :         if (aso_mtr->type == ASO_METER_INDIRECT) {
     826         [ #  # ]:          0 :                 if (likely(sh->config.dv_flow_en == 2))
     827                 :          0 :                         pool = aso_mtr->pool;
     828                 :            :                 else
     829                 :          0 :                         pool = container_of(aso_mtr, struct mlx5_aso_mtr_pool,
     830                 :            :                                             mtrs[aso_mtr->offset]);
     831                 :          0 :                 id = pool->devx_obj->id;
     832                 :            :         } else {
     833                 :          0 :                 id = bulk->devx_obj->id;
     834                 :            :         }
     835         [ #  # ]:          0 :         wqe->general_cseg.misc = rte_cpu_to_be_32(id +
     836                 :            :                                                   (aso_mtr->offset >> 1));
     837                 :          0 :         wqe->general_cseg.opcode =
     838         [ #  # ]:          0 :                 rte_cpu_to_be_32(MLX5_OPCODE_ACCESS_ASO |
     839                 :            :                         (ASO_OPC_MOD_POLICER << WQE_CSEG_OPC_MOD_OFFSET) |
     840                 :            :                          sq->pi << WQE_CSEG_WQE_INDEX_OFFSET);
     841                 :            :         /* There are 2 meters in one ASO cache line. */
     842                 :          0 :         dseg_idx = aso_mtr->offset & 0x1;
     843                 :          0 :         wqe->aso_cseg.data_mask =
     844   [ #  #  #  #  :          0 :                 RTE_BE64(MLX5_IFC_FLOW_METER_PARAM_MASK << (32 * !dseg_idx));
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
     845         [ #  # ]:          0 :         if (fm->is_enable) {
     846                 :          0 :                 wqe->aso_dseg.mtrs[dseg_idx].cbs_cir =
     847                 :          0 :                         fm->profile->srtcm_prm.cbs_cir;
     848                 :          0 :                 wqe->aso_dseg.mtrs[dseg_idx].ebs_eir =
     849                 :          0 :                         fm->profile->srtcm_prm.ebs_eir;
     850                 :            :         } else {
     851                 :          0 :                 wqe->aso_dseg.mtrs[dseg_idx].cbs_cir =
     852                 :            :                         RTE_BE32(MLX5_IFC_FLOW_METER_DISABLE_CBS_CIR_VAL);
     853                 :          0 :                 wqe->aso_dseg.mtrs[dseg_idx].ebs_eir = 0;
     854                 :            :         }
     855                 :          0 :         fmp = fm->profile;
     856                 :            :         param_le = (1 << ASO_DSEG_VALID_OFFSET);
     857         [ #  # ]:          0 :         if (fm->color_aware)
     858                 :            :                 param_le |= (MLX5_FLOW_COLOR_UNDEFINED << ASO_DSEG_SC_OFFSET);
     859                 :            :         else
     860                 :            :                 param_le |= (MLX5_FLOW_COLOR_GREEN << ASO_DSEG_SC_OFFSET);
     861         [ #  # ]:          0 :         if (fmp->profile.packet_mode)
     862                 :          0 :                 param_le |= (MLX5_METER_MODE_PKT << ASO_DSEG_MTR_MODE);
     863                 :          0 :         wqe->aso_dseg.mtrs[dseg_idx].v_bo_sc_bbog_mm = RTE_BE32(param_le);
     864   [ #  #  #  # ]:          0 :         switch (fmp->profile.alg) {
     865                 :          0 :         case RTE_MTR_SRTCM_RFC2697:
     866                 :            :                 /* Only needed for RFC2697. */
     867         [ #  # ]:          0 :                 if (fm->profile->srtcm_prm.ebs_eir)
     868                 :          0 :                         wqe->aso_dseg.mtrs[dseg_idx].v_bo_sc_bbog_mm |=
     869                 :            :                                         RTE_BE32(1 << ASO_DSEG_BO_OFFSET);
     870                 :            :                 break;
     871                 :          0 :         case RTE_MTR_TRTCM_RFC2698:
     872                 :          0 :                 wqe->aso_dseg.mtrs[dseg_idx].v_bo_sc_bbog_mm |=
     873                 :            :                                 RTE_BE32(1 << ASO_DSEG_BBOG_OFFSET);
     874                 :          0 :                 break;
     875                 :          0 :         case RTE_MTR_TRTCM_RFC4115:
     876                 :          0 :                 wqe->aso_dseg.mtrs[dseg_idx].v_bo_sc_bbog_mm |=
     877                 :            :                                 RTE_BE32(1 << ASO_DSEG_BO_OFFSET);
     878                 :          0 :                 break;
     879                 :            :         default:
     880                 :            :                 break;
     881                 :            :         }
     882                 :            :         /*
     883                 :            :          * Note:
     884                 :            :          * Due to software performance reason, the token fields will not be
     885                 :            :          * set when posting the WQE to ASO SQ. It will be filled by the HW
     886                 :            :          * automatically.
     887                 :            :          */
     888                 :          0 :         sq->head++;
     889                 :          0 :         sq->pi += 2;/* Each WQE contains 2 WQEBB's. */
     890         [ #  # ]:          0 :         if (push) {
     891                 :          0 :                 mlx5_doorbell_ring(&sh->tx_uar.bf_db, *(volatile uint64_t *)wqe,
     892                 :          0 :                            sq->pi, &sq->sq_obj.db_rec[MLX5_SND_DBR],
     893                 :          0 :                            !sh->tx_uar.dbnc);
     894                 :          0 :                 sq->db_pi = sq->pi;
     895                 :            :         }
     896                 :          0 :         sq->db = wqe;
     897         [ #  # ]:          0 :         if (need_lock)
     898                 :          0 :                 rte_spinlock_unlock(&sq->sqsl);
     899                 :            :         return 1;
     900                 :            : }
     901                 :            : 
     902                 :            : static void
     903                 :          0 : mlx5_aso_mtr_completion_handle(struct mlx5_aso_sq *sq, bool need_lock)
     904                 :            : {
     905                 :            :         struct mlx5_aso_cq *cq = &sq->cq;
     906                 :            :         volatile struct mlx5_cqe *restrict cqe;
     907                 :          0 :         const unsigned int cq_size = 1 << cq->log_desc_n;
     908                 :          0 :         const unsigned int mask = cq_size - 1;
     909                 :            :         uint32_t idx;
     910                 :          0 :         uint32_t next_idx = cq->cq_ci & mask;
     911                 :            :         uint16_t max;
     912                 :            :         uint16_t i, n = 0;
     913                 :            :         int ret;
     914                 :            : 
     915         [ #  # ]:          0 :         if (need_lock)
     916                 :          0 :                 rte_spinlock_lock(&sq->sqsl);
     917                 :          0 :         max = (uint16_t)(sq->head - sq->tail);
     918         [ #  # ]:          0 :         if (unlikely(!max)) {
     919         [ #  # ]:          0 :                 if (need_lock)
     920                 :          0 :                         rte_spinlock_unlock(&sq->sqsl);
     921                 :          0 :                 return;
     922                 :            :         }
     923                 :            :         do {
     924                 :          0 :                 idx = next_idx;
     925                 :          0 :                 next_idx = (cq->cq_ci + 1) & mask;
     926                 :          0 :                 rte_prefetch0(&cq->cq_obj.cqes[next_idx]);
     927                 :          0 :                 cqe = &cq->cq_obj.cqes[idx];
     928         [ #  # ]:          0 :                 ret = check_cqe(cqe, cq_size, cq->cq_ci);
     929                 :            :                 /*
     930                 :            :                  * Be sure owner read is done before any other cookie field or
     931                 :            :                  * opaque field.
     932                 :            :                  */
     933                 :          0 :                 rte_io_rmb();
     934                 :            :                 if (ret != MLX5_CQE_STATUS_SW_OWN) {
     935         [ #  # ]:          0 :                         if (likely(ret == MLX5_CQE_STATUS_HW_OWN))
     936                 :            :                                 break;
     937                 :          0 :                         mlx5_aso_cqe_err_handle(sq);
     938                 :            :                 } else {
     939                 :          0 :                         n++;
     940                 :            :                 }
     941                 :          0 :                 cq->cq_ci++;
     942                 :            :         } while (1);
     943         [ #  # ]:          0 :         if (likely(n)) {
     944                 :            :                 uint8_t exp_state = ASO_METER_WAIT;
     945                 :            :                 struct mlx5_aso_mtr *aso_mtr;
     946                 :            :                 __rte_unused bool verdict;
     947                 :            : 
     948         [ #  # ]:          0 :                 for (i = 0; i < n; ++i) {
     949                 :          0 :                         aso_mtr = sq->elts[(sq->tail + i) & mask].mtr;
     950                 :            :                         MLX5_ASSERT(aso_mtr);
     951                 :          0 :                         verdict = rte_atomic_compare_exchange_strong_explicit(&aso_mtr->state,
     952                 :            :                                                     &exp_state, ASO_METER_READY,
     953                 :            :                                                     rte_memory_order_relaxed,
     954                 :            :                                                     rte_memory_order_relaxed);
     955                 :            :                         MLX5_ASSERT(verdict);
     956                 :            :                 }
     957                 :          0 :                 sq->tail += n;
     958                 :          0 :                 rte_io_wmb();
     959         [ #  # ]:          0 :                 cq->cq_obj.db_rec[0] = rte_cpu_to_be_32(cq->cq_ci);
     960                 :            :         }
     961         [ #  # ]:          0 :         if (need_lock)
     962                 :          0 :                 rte_spinlock_unlock(&sq->sqsl);
     963                 :            : }
     964                 :            : 
     965                 :            : static __rte_always_inline struct mlx5_aso_sq *
     966                 :            : mlx5_aso_mtr_select_sq(struct mlx5_dev_ctx_shared *sh, uint32_t queue,
     967                 :            :                        struct mlx5_aso_mtr *mtr, bool *need_lock)
     968                 :            : {
     969                 :            :         struct mlx5_aso_sq *sq;
     970                 :            : 
     971         [ #  # ]:          0 :         if (likely(sh->config.dv_flow_en == 2) &&
     972   [ #  #  #  # ]:          0 :             mtr->type == ASO_METER_INDIRECT) {
     973         [ #  # ]:          0 :                 if (queue == MLX5_HW_INV_QUEUE) {
     974                 :          0 :                         sq = &mtr->pool->sq[mtr->pool->nb_sq - 1];
     975                 :            :                         *need_lock = true;
     976                 :            :                 } else {
     977                 :          0 :                         sq = &mtr->pool->sq[queue];
     978                 :            :                         *need_lock = false;
     979                 :            :                 }
     980                 :            :         } else {
     981                 :          0 :                 sq = &sh->mtrmng->pools_mng.sq;
     982                 :            :                 *need_lock = true;
     983                 :            :         }
     984                 :            :         return sq;
     985                 :            : }
     986                 :            : 
     987                 :            : #if defined(HAVE_MLX5_HWS_SUPPORT)
     988                 :            : static void
     989                 :          0 : mlx5_aso_poll_cq_mtr_hws(struct mlx5_priv *priv, struct mlx5_aso_sq *sq)
     990                 :            : {
     991                 :            : #define MLX5_HWS_MTR_CMPL_NUM 4
     992                 :            : 
     993                 :            :         int i, ret;
     994                 :            :         struct mlx5_aso_mtr *mtr;
     995                 :            :         uint8_t exp_state = ASO_METER_WAIT;
     996                 :            :         struct rte_flow_op_result res[MLX5_HWS_MTR_CMPL_NUM];
     997                 :            :         __rte_unused bool verdict;
     998                 :            : 
     999                 :          0 :         rte_spinlock_lock(&sq->sqsl);
    1000                 :          0 : repeat:
    1001                 :          0 :         ret = mlx5_aso_pull_completion(sq, res, MLX5_HWS_MTR_CMPL_NUM);
    1002         [ #  # ]:          0 :         if (ret) {
    1003         [ #  # ]:          0 :                 for (i = 0; i < ret; i++) {
    1004                 :          0 :                         struct mlx5_hw_q_job *job = res[i].user_data;
    1005                 :            : 
    1006                 :            :                         MLX5_ASSERT(job);
    1007                 :          0 :                         mtr = mlx5_ipool_get(priv->hws_mpool->idx_pool,
    1008                 :          0 :                                              MLX5_INDIRECT_ACTION_IDX_GET(job->action));
    1009                 :            :                         MLX5_ASSERT(mtr);
    1010                 :          0 :                         verdict = rte_atomic_compare_exchange_strong_explicit(&mtr->state,
    1011                 :            :                                                     &exp_state, ASO_METER_READY,
    1012                 :            :                                                     rte_memory_order_relaxed,
    1013                 :            :                                                     rte_memory_order_relaxed);
    1014                 :            :                         MLX5_ASSERT(verdict);
    1015                 :          0 :                         flow_hw_job_put(priv, job, CTRL_QUEUE_ID(priv));
    1016                 :            :                 }
    1017         [ #  # ]:          0 :                 if (ret == MLX5_HWS_MTR_CMPL_NUM)
    1018                 :          0 :                         goto repeat;
    1019                 :            :         }
    1020                 :            :         rte_spinlock_unlock(&sq->sqsl);
    1021                 :            : 
    1022                 :            : #undef MLX5_HWS_MTR_CMPL_NUM
    1023                 :          0 : }
    1024                 :            : #else
    1025                 :            : static void
    1026                 :            : mlx5_aso_poll_cq_mtr_hws(__rte_unused struct mlx5_priv *priv, __rte_unused struct mlx5_aso_sq *sq)
    1027                 :            : {
    1028                 :            :         MLX5_ASSERT(false);
    1029                 :            : }
    1030                 :            : #endif
    1031                 :            : 
    1032                 :            : static void
    1033                 :          0 : mlx5_aso_poll_cq_mtr_sws(__rte_unused struct mlx5_priv *priv,
    1034                 :            :                          struct mlx5_aso_sq *sq)
    1035                 :            : {
    1036                 :          0 :         mlx5_aso_mtr_completion_handle(sq, true);
    1037                 :          0 : }
    1038                 :            : 
    1039                 :            : typedef void (*poll_cq_t)(struct mlx5_priv *, struct mlx5_aso_sq *);
    1040                 :            : 
    1041                 :            : /**
    1042                 :            :  * Update meter parameter by send WQE.
    1043                 :            :  *
    1044                 :            :  * @param[in] dev
    1045                 :            :  *   Pointer to Ethernet device.
    1046                 :            :  * @param[in] priv
    1047                 :            :  *   Pointer to mlx5 private data structure.
    1048                 :            :  * @param[in] fm
    1049                 :            :  *   Pointer to flow meter to be modified.
    1050                 :            :  *
    1051                 :            :  * @return
    1052                 :            :  *   0 on success, a negative errno value otherwise and rte_errno is set.
    1053                 :            :  */
    1054                 :            : int
    1055                 :          0 : mlx5_aso_meter_update_by_wqe(struct mlx5_priv *priv, uint32_t queue,
    1056                 :            :                              struct mlx5_aso_mtr *mtr,
    1057                 :            :                              struct mlx5_mtr_bulk *bulk,
    1058                 :            :                              struct mlx5_hw_q_job *job, bool push)
    1059                 :            : {
    1060                 :            :         bool need_lock;
    1061         [ #  # ]:          0 :         struct mlx5_dev_ctx_shared *sh = priv->sh;
    1062                 :            :         struct mlx5_aso_sq *sq =
    1063                 :            :                 mlx5_aso_mtr_select_sq(sh, queue, mtr, &need_lock);
    1064                 :            :         uint32_t poll_wqe_times = MLX5_MTR_POLL_WQE_CQE_TIMES;
    1065                 :            :         poll_cq_t poll_mtr_cq =
    1066         [ #  # ]:          0 :                 job ? mlx5_aso_poll_cq_mtr_hws : mlx5_aso_poll_cq_mtr_sws;
    1067                 :            :         int ret;
    1068                 :            : 
    1069         [ #  # ]:          0 :         if (queue != MLX5_HW_INV_QUEUE) {
    1070                 :          0 :                 ret = mlx5_aso_mtr_sq_enqueue_single(sh, sq, mtr, bulk,
    1071                 :            :                                                      need_lock, job, push);
    1072         [ #  # ]:          0 :                 return ret > 0 ? 0 : -1;
    1073                 :            :         }
    1074                 :            :         do {
    1075                 :          0 :                 poll_mtr_cq(priv, sq);
    1076         [ #  # ]:          0 :                 if (mlx5_aso_mtr_sq_enqueue_single(sh, sq, mtr, bulk,
    1077                 :            :                                                    need_lock, job, true))
    1078                 :            :                         return 0;
    1079                 :            :                 /* Waiting for wqe resource. */
    1080                 :          0 :                 rte_delay_us_sleep(MLX5_ASO_WQE_CQE_RESPONSE_DELAY);
    1081         [ #  # ]:          0 :         } while (--poll_wqe_times);
    1082                 :          0 :         DRV_LOG(ERR, "Fail to send WQE for ASO meter offset %d",
    1083                 :            :                         mtr->offset);
    1084                 :          0 :         return -1;
    1085                 :            : }
    1086                 :            : 
    1087                 :            : /**
    1088                 :            :  * Wait for meter to be ready.
    1089                 :            :  *
    1090                 :            :  * @param[in] dev
    1091                 :            :  *   Pointer to Ethernet device.
    1092                 :            :  * @param[in] priv
    1093                 :            :  *   Pointer to mlx5 private data structure.
    1094                 :            :  * @param[in] fm
    1095                 :            :  *   Pointer to flow meter to be modified.
    1096                 :            :  *
    1097                 :            :  * @return
    1098                 :            :  *   0 on success, a negative errno value otherwise and rte_errno is set.
    1099                 :            :  */
    1100                 :            : int
    1101                 :          0 : mlx5_aso_mtr_wait(struct mlx5_priv *priv,
    1102                 :            :                   struct mlx5_aso_mtr *mtr, bool is_tmpl_api)
    1103                 :            : {
    1104                 :            :         bool need_lock;
    1105                 :            :         struct mlx5_aso_sq *sq;
    1106                 :          0 :         struct mlx5_dev_ctx_shared *sh = priv->sh;
    1107                 :            :         uint32_t poll_cqe_times = MLX5_MTR_POLL_WQE_CQE_TIMES;
    1108                 :          0 :         uint8_t state = rte_atomic_load_explicit(&mtr->state, rte_memory_order_relaxed);
    1109                 :            :         poll_cq_t poll_mtr_cq =
    1110         [ #  # ]:          0 :                 is_tmpl_api ? mlx5_aso_poll_cq_mtr_hws : mlx5_aso_poll_cq_mtr_sws;
    1111                 :            : 
    1112         [ #  # ]:          0 :         if (state == ASO_METER_READY || state == ASO_METER_WAIT_ASYNC)
    1113                 :            :                 return 0;
    1114                 :            :         sq = mlx5_aso_mtr_select_sq(sh, MLX5_HW_INV_QUEUE, mtr, &need_lock);
    1115                 :            :         do {
    1116                 :          0 :                 poll_mtr_cq(priv, sq);
    1117         [ #  # ]:          0 :                 if (rte_atomic_load_explicit(&mtr->state, rte_memory_order_relaxed) ==
    1118                 :            :                                             ASO_METER_READY)
    1119                 :            :                         return 0;
    1120                 :            :                 /* Waiting for CQE ready. */
    1121                 :          0 :                 rte_delay_us_sleep(MLX5_ASO_WQE_CQE_RESPONSE_DELAY);
    1122         [ #  # ]:          0 :         } while (--poll_cqe_times);
    1123                 :          0 :         DRV_LOG(ERR, "Fail to poll CQE ready for ASO meter offset %d",
    1124                 :            :                         mtr->offset);
    1125                 :          0 :         return -1;
    1126                 :            : }
    1127                 :            : 
    1128                 :            : static inline struct mlx5_aso_sq*
    1129                 :            : __mlx5_aso_ct_get_sq_in_hws(uint32_t queue,
    1130                 :            :                             struct mlx5_aso_ct_pool *pool)
    1131                 :            : {
    1132                 :            :         return (queue == MLX5_HW_INV_QUEUE) ?
    1133   [ #  #  #  #  :          0 :                 pool->shared_sq : &pool->sq[queue];
             #  #  #  # ]
    1134                 :            : }
    1135                 :            : 
    1136                 :            : static inline struct mlx5_aso_sq*
    1137                 :            : __mlx5_aso_ct_get_sq_in_sws(struct mlx5_dev_ctx_shared *sh,
    1138                 :            :                             struct mlx5_aso_ct_action *ct)
    1139                 :            : {
    1140                 :          0 :         return &sh->ct_mng->aso_sqs[ct->offset & (MLX5_ASO_CT_SQ_NUM - 1)];
    1141                 :            : }
    1142                 :            : 
    1143                 :            : static inline struct mlx5_aso_ct_pool*
    1144                 :            : __mlx5_aso_ct_get_pool(struct mlx5_dev_ctx_shared *sh,
    1145                 :            :                        struct mlx5_aso_ct_action *ct)
    1146                 :            : {
    1147   [ #  #  #  # ]:          0 :         if (likely(sh->config.dv_flow_en == 2))
    1148                 :          0 :                 return ct->pool;
    1149                 :          0 :         return container_of(ct, struct mlx5_aso_ct_pool, actions[ct->offset]);
    1150                 :            : }
    1151                 :            : 
    1152                 :            : int
    1153                 :          0 : mlx5_aso_ct_queue_uninit(struct mlx5_aso_ct_pools_mng *ct_mng)
    1154                 :            : {
    1155                 :            :         uint32_t i;
    1156                 :            : 
    1157                 :            :         /* 64B per object for query. */
    1158         [ #  # ]:          0 :         for (i = 0; i < ct_mng->nb_sq; i++) {
    1159         [ #  # ]:          0 :                 if (ct_mng->aso_sqs[i].mr.addr)
    1160                 :          0 :                         mlx5_aso_dereg_mr(&ct_mng->aso_sqs[i].mr);
    1161                 :          0 :                 mlx5_aso_destroy_sq(&ct_mng->aso_sqs[i]);
    1162                 :            :         }
    1163                 :          0 :         return 0;
    1164                 :            : }
    1165                 :            : 
    1166                 :            : /**
    1167                 :            :  * API to create and initialize CT Send Queue used for ASO access.
    1168                 :            :  *
    1169                 :            :  * @param[in] sh
    1170                 :            :  *   Pointer to shared device context.
    1171                 :            :  * @param[in] ct_mng
    1172                 :            :  *   Pointer to the CT management struct.
    1173                 :            :  * *param[in] nb_queues
    1174                 :            :  *   Number of queues to be allocated.
    1175                 :            :  *
    1176                 :            :  * @return
    1177                 :            :  *   0 on success, a negative errno value otherwise and rte_errno is set.
    1178                 :            :  */
    1179                 :            : int
    1180                 :          0 : mlx5_aso_ct_queue_init(struct mlx5_dev_ctx_shared *sh,
    1181                 :            :                        struct mlx5_aso_ct_pools_mng *ct_mng,
    1182                 :            :                        uint32_t nb_queues)
    1183                 :            : {
    1184                 :            :         uint32_t i;
    1185                 :            : 
    1186                 :            :         /* 64B per object for query. */
    1187         [ #  # ]:          0 :         for (i = 0; i < nb_queues; i++) {
    1188         [ #  # ]:          0 :                 if (mlx5_aso_reg_mr(sh->cdev, 64 * (1 << MLX5_ASO_QUEUE_LOG_DESC),
    1189                 :            :                                     &ct_mng->aso_sqs[i].mr))
    1190                 :          0 :                         goto error;
    1191         [ #  # ]:          0 :                 if (mlx5_aso_sq_create(sh->cdev, &ct_mng->aso_sqs[i],
    1192                 :            :                                        sh->tx_uar.obj,
    1193                 :            :                                        MLX5_ASO_QUEUE_LOG_DESC))
    1194                 :          0 :                         goto error;
    1195                 :          0 :                 mlx5_aso_ct_init_sq(&ct_mng->aso_sqs[i]);
    1196                 :            :         }
    1197                 :          0 :         ct_mng->nb_sq = nb_queues;
    1198                 :          0 :         return 0;
    1199                 :            : error:
    1200                 :            :         do {
    1201         [ #  # ]:          0 :                 if (ct_mng->aso_sqs[i].mr.addr)
    1202                 :          0 :                         mlx5_aso_dereg_mr(&ct_mng->aso_sqs[i].mr);
    1203                 :          0 :                 mlx5_aso_destroy_sq(&ct_mng->aso_sqs[i]);
    1204         [ #  # ]:          0 :         } while (i--);
    1205                 :          0 :         ct_mng->nb_sq = 0;
    1206                 :          0 :         return -1;
    1207                 :            : }
    1208                 :            : 
    1209                 :            : /*
    1210                 :            :  * Post a WQE to the ASO CT SQ to modify the context.
    1211                 :            :  *
    1212                 :            :  * @param[in] sh
    1213                 :            :  *   Pointer to shared device context.
    1214                 :            :  * @param[in] ct
    1215                 :            :  *   Pointer to the generic CT structure related to the context.
    1216                 :            :  * @param[in] profile
    1217                 :            :  *   Pointer to configuration profile.
    1218                 :            :  *
    1219                 :            :  * @return
    1220                 :            :  *   1 on success (WQE number), 0 on failure.
    1221                 :            :  */
    1222                 :            : static uint16_t
    1223                 :          0 : mlx5_aso_ct_sq_enqueue_single(struct mlx5_dev_ctx_shared *sh,
    1224                 :            :                               struct mlx5_aso_sq *sq,
    1225                 :            :                               struct mlx5_aso_ct_action *ct,
    1226                 :            :                               const struct rte_flow_action_conntrack *profile,
    1227                 :            :                               bool need_lock,
    1228                 :            :                               void *user_data,
    1229                 :            :                               bool push)
    1230                 :            : {
    1231                 :            :         volatile struct mlx5_aso_wqe *wqe = NULL;
    1232                 :          0 :         uint16_t size = 1 << sq->log_desc_n;
    1233                 :          0 :         uint16_t mask = size - 1;
    1234                 :            :         uint16_t res;
    1235                 :            :         struct mlx5_aso_ct_pool *pool;
    1236                 :            :         void *desg;
    1237                 :            :         void *orig_dir;
    1238                 :            :         void *reply_dir;
    1239                 :            : 
    1240         [ #  # ]:          0 :         if (need_lock)
    1241                 :          0 :                 rte_spinlock_lock(&sq->sqsl);
    1242                 :            :         /* Prevent other threads to update the index. */
    1243                 :          0 :         res = size - (uint16_t)(sq->head - sq->tail);
    1244         [ #  # ]:          0 :         if (unlikely(!res)) {
    1245         [ #  # ]:          0 :                 if (need_lock)
    1246                 :          0 :                         rte_spinlock_unlock(&sq->sqsl);
    1247                 :          0 :                 DRV_LOG(ERR, "Fail: SQ is full and no free WQE to send");
    1248                 :          0 :                 return 0;
    1249                 :            :         }
    1250                 :          0 :         wqe = &sq->sq_obj.aso_wqes[sq->head & mask];
    1251                 :          0 :         rte_prefetch0(&sq->sq_obj.aso_wqes[(sq->head + 1) & mask]);
    1252                 :            :         /* Fill next WQE. */
    1253         [ #  # ]:          0 :         MLX5_ASO_CT_UPDATE_STATE(ct,
    1254                 :            :                         user_data ? ASO_CONNTRACK_WAIT_ASYNC : ASO_CONNTRACK_WAIT);
    1255         [ #  # ]:          0 :         if (user_data) {
    1256                 :          0 :                 sq->elts[sq->head & mask].user_data = user_data;
    1257                 :            :         } else {
    1258                 :          0 :                 sq->elts[sq->head & mask].ct = ct;
    1259                 :          0 :                 sq->elts[sq->head & mask].query_data = NULL;
    1260                 :            :         }
    1261                 :            :         pool = __mlx5_aso_ct_get_pool(sh, ct);
    1262                 :            : 
    1263                 :            :         /* Each WQE will have a single CT object. */
    1264         [ #  # ]:          0 :         wqe->general_cseg.misc = rte_cpu_to_be_32(pool->devx_obj->id +
    1265                 :            :                                                   ct->offset);
    1266         [ #  # ]:          0 :         wqe->general_cseg.opcode = rte_cpu_to_be_32(MLX5_OPCODE_ACCESS_ASO |
    1267                 :            :                         (ASO_OPC_MOD_CONNECTION_TRACKING <<
    1268                 :            :                          WQE_CSEG_OPC_MOD_OFFSET) |
    1269                 :            :                         sq->pi << WQE_CSEG_WQE_INDEX_OFFSET);
    1270                 :          0 :         wqe->aso_cseg.operand_masks = rte_cpu_to_be_32
    1271                 :            :                         (0u |
    1272                 :            :                          (ASO_OPER_LOGICAL_OR << ASO_CSEG_COND_OPER_OFFSET) |
    1273                 :            :                          (ASO_OP_ALWAYS_TRUE << ASO_CSEG_COND_1_OPER_OFFSET) |
    1274                 :            :                          (ASO_OP_ALWAYS_TRUE << ASO_CSEG_COND_0_OPER_OFFSET) |
    1275                 :            :                          (BYTEWISE_64BYTE << ASO_CSEG_DATA_MASK_MODE_OFFSET));
    1276                 :          0 :         wqe->aso_cseg.data_mask = UINT64_MAX;
    1277                 :            :         /* To make compiler happy. */
    1278                 :            :         desg = (void *)(uintptr_t)wqe->aso_dseg.data;
    1279         [ #  # ]:          0 :         MLX5_SET(conn_track_aso, desg, valid, 1);
    1280         [ #  # ]:          0 :         MLX5_SET(conn_track_aso, desg, state, profile->state);
    1281         [ #  # ]:          0 :         MLX5_SET(conn_track_aso, desg, freeze_track, !profile->enable);
    1282         [ #  # ]:          0 :         MLX5_SET(conn_track_aso, desg, connection_assured,
    1283                 :            :                  profile->live_connection);
    1284         [ #  # ]:          0 :         MLX5_SET(conn_track_aso, desg, sack_permitted, profile->selective_ack);
    1285         [ #  # ]:          0 :         MLX5_SET(conn_track_aso, desg, challenged_acked,
    1286                 :            :                  profile->challenge_ack_passed);
    1287                 :            :         /* Heartbeat, retransmission_counter, retranmission_limit_exceeded: 0 */
    1288         [ #  # ]:          0 :         MLX5_SET(conn_track_aso, desg, heartbeat, 0);
    1289         [ #  # ]:          0 :         MLX5_SET(conn_track_aso, desg, max_ack_window,
    1290                 :            :                  profile->max_ack_window);
    1291         [ #  # ]:          0 :         MLX5_SET(conn_track_aso, desg, retransmission_counter, 0);
    1292         [ #  # ]:          0 :         MLX5_SET(conn_track_aso, desg, retranmission_limit_exceeded, 0);
    1293         [ #  # ]:          0 :         MLX5_SET(conn_track_aso, desg, retranmission_limit,
    1294                 :            :                  profile->retransmission_limit);
    1295         [ #  # ]:          0 :         MLX5_SET(conn_track_aso, desg, reply_direction_tcp_scale,
    1296                 :            :                  profile->reply_dir.scale);
    1297         [ #  # ]:          0 :         MLX5_SET(conn_track_aso, desg, reply_direction_tcp_close_initiated,
    1298                 :            :                  profile->reply_dir.close_initiated);
    1299                 :            :         /* Both directions will use the same liberal mode. */
    1300         [ #  # ]:          0 :         MLX5_SET(conn_track_aso, desg, reply_direction_tcp_liberal_enabled,
    1301                 :            :                  profile->liberal_mode);
    1302         [ #  # ]:          0 :         MLX5_SET(conn_track_aso, desg, reply_direction_tcp_data_unacked,
    1303                 :            :                  profile->reply_dir.data_unacked);
    1304         [ #  # ]:          0 :         MLX5_SET(conn_track_aso, desg, reply_direction_tcp_max_ack,
    1305                 :            :                  profile->reply_dir.last_ack_seen);
    1306         [ #  # ]:          0 :         MLX5_SET(conn_track_aso, desg, original_direction_tcp_scale,
    1307                 :            :                  profile->original_dir.scale);
    1308         [ #  # ]:          0 :         MLX5_SET(conn_track_aso, desg, original_direction_tcp_close_initiated,
    1309                 :            :                  profile->original_dir.close_initiated);
    1310         [ #  # ]:          0 :         MLX5_SET(conn_track_aso, desg, original_direction_tcp_liberal_enabled,
    1311                 :            :                  profile->liberal_mode);
    1312         [ #  # ]:          0 :         MLX5_SET(conn_track_aso, desg, original_direction_tcp_data_unacked,
    1313                 :            :                  profile->original_dir.data_unacked);
    1314         [ #  # ]:          0 :         MLX5_SET(conn_track_aso, desg, original_direction_tcp_max_ack,
    1315                 :            :                  profile->original_dir.last_ack_seen);
    1316         [ #  # ]:          0 :         MLX5_SET(conn_track_aso, desg, last_win, profile->last_window);
    1317         [ #  # ]:          0 :         MLX5_SET(conn_track_aso, desg, last_dir, profile->last_direction);
    1318         [ #  # ]:          0 :         MLX5_SET(conn_track_aso, desg, last_index, profile->last_index);
    1319         [ #  # ]:          0 :         MLX5_SET(conn_track_aso, desg, last_seq, profile->last_seq);
    1320         [ #  # ]:          0 :         MLX5_SET(conn_track_aso, desg, last_ack, profile->last_ack);
    1321         [ #  # ]:          0 :         MLX5_SET(conn_track_aso, desg, last_end, profile->last_end);
    1322                 :            :         orig_dir = MLX5_ADDR_OF(conn_track_aso, desg, original_dir);
    1323         [ #  # ]:          0 :         MLX5_SET(tcp_window_params, orig_dir, sent_end,
    1324                 :            :                  profile->original_dir.sent_end);
    1325         [ #  # ]:          0 :         MLX5_SET(tcp_window_params, orig_dir, reply_end,
    1326                 :            :                  profile->original_dir.reply_end);
    1327         [ #  # ]:          0 :         MLX5_SET(tcp_window_params, orig_dir, max_win,
    1328                 :            :                  profile->original_dir.max_win);
    1329         [ #  # ]:          0 :         MLX5_SET(tcp_window_params, orig_dir, max_ack,
    1330                 :            :                  profile->original_dir.max_ack);
    1331                 :            :         reply_dir = MLX5_ADDR_OF(conn_track_aso, desg, reply_dir);
    1332         [ #  # ]:          0 :         MLX5_SET(tcp_window_params, reply_dir, sent_end,
    1333                 :            :                  profile->reply_dir.sent_end);
    1334         [ #  # ]:          0 :         MLX5_SET(tcp_window_params, reply_dir, reply_end,
    1335                 :            :                  profile->reply_dir.reply_end);
    1336         [ #  # ]:          0 :         MLX5_SET(tcp_window_params, reply_dir, max_win,
    1337                 :            :                  profile->reply_dir.max_win);
    1338         [ #  # ]:          0 :         MLX5_SET(tcp_window_params, reply_dir, max_ack,
    1339                 :            :                  profile->reply_dir.max_ack);
    1340                 :          0 :         sq->head++;
    1341                 :          0 :         sq->pi += 2; /* Each WQE contains 2 WQEBB's. */
    1342         [ #  # ]:          0 :         if (push) {
    1343                 :          0 :                 mlx5_doorbell_ring(&sh->tx_uar.bf_db, *(volatile uint64_t *)wqe,
    1344                 :          0 :                                    sq->pi, &sq->sq_obj.db_rec[MLX5_SND_DBR],
    1345                 :          0 :                                    !sh->tx_uar.dbnc);
    1346                 :          0 :                 sq->db_pi = sq->pi;
    1347                 :            :         }
    1348                 :          0 :         sq->db = wqe;
    1349         [ #  # ]:          0 :         if (need_lock)
    1350                 :          0 :                 rte_spinlock_unlock(&sq->sqsl);
    1351                 :            :         return 1;
    1352                 :            : }
    1353                 :            : 
    1354                 :            : /*
    1355                 :            :  * Update the status field of CTs to indicate ready to be used by flows.
    1356                 :            :  * A continuous number of CTs since last update.
    1357                 :            :  *
    1358                 :            :  * @param[in] sq
    1359                 :            :  *   Pointer to ASO CT SQ.
    1360                 :            :  * @param[in] num
    1361                 :            :  *   Number of CT structures to be updated.
    1362                 :            :  *
    1363                 :            :  * @return
    1364                 :            :  *   0 on success, a negative value.
    1365                 :            :  */
    1366                 :            : static void
    1367                 :          0 : mlx5_aso_ct_status_update(struct mlx5_aso_sq *sq, uint16_t num)
    1368                 :            : {
    1369                 :          0 :         uint16_t size = 1 << sq->log_desc_n;
    1370                 :          0 :         uint16_t mask = size - 1;
    1371                 :            :         uint16_t i;
    1372                 :            :         struct mlx5_aso_ct_action *ct = NULL;
    1373                 :            :         uint16_t idx;
    1374                 :            : 
    1375         [ #  # ]:          0 :         for (i = 0; i < num; i++) {
    1376                 :          0 :                 idx = (uint16_t)((sq->tail + i) & mask);
    1377                 :          0 :                 ct = sq->elts[idx].ct;
    1378                 :            :                 MLX5_ASSERT(ct);
    1379                 :          0 :                 MLX5_ASO_CT_UPDATE_STATE(ct, ASO_CONNTRACK_READY);
    1380         [ #  # ]:          0 :                 if (sq->elts[idx].query_data)
    1381                 :          0 :                         rte_memcpy(sq->elts[idx].query_data,
    1382                 :          0 :                                    (char *)((uintptr_t)sq->mr.addr + idx * 64),
    1383                 :            :                                    64);
    1384                 :            :         }
    1385                 :          0 : }
    1386                 :            : 
    1387                 :            : /*
    1388                 :            :  * Post a WQE to the ASO CT SQ to query the current context.
    1389                 :            :  *
    1390                 :            :  * @param[in] sh
    1391                 :            :  *   Pointer to shared device context.
    1392                 :            :  * @param[in] ct
    1393                 :            :  *   Pointer to the generic CT structure related to the context.
    1394                 :            :  * @param[in] data
    1395                 :            :  *   Pointer to data area to be filled.
    1396                 :            :  *
    1397                 :            :  * @return
    1398                 :            :  *   1 on success (WQE number), 0 on failure.
    1399                 :            :  */
    1400                 :            : static int
    1401                 :          0 : mlx5_aso_ct_sq_query_single(struct mlx5_dev_ctx_shared *sh,
    1402                 :            :                             struct mlx5_aso_sq *sq,
    1403                 :            :                             struct mlx5_aso_ct_action *ct, char *data,
    1404                 :            :                             bool need_lock,
    1405                 :            :                             void *user_data,
    1406                 :            :                             bool push)
    1407                 :            : {
    1408                 :            :         volatile struct mlx5_aso_wqe *wqe = NULL;
    1409                 :          0 :         uint16_t size = 1 << sq->log_desc_n;
    1410                 :          0 :         uint16_t mask = size - 1;
    1411                 :            :         uint16_t res;
    1412                 :            :         uint16_t wqe_idx;
    1413                 :            :         struct mlx5_aso_ct_pool *pool;
    1414                 :            :         enum mlx5_aso_ct_state state =
    1415                 :          0 :                                 rte_atomic_load_explicit(&ct->state, rte_memory_order_relaxed);
    1416                 :            : 
    1417         [ #  # ]:          0 :         if (state == ASO_CONNTRACK_FREE) {
    1418                 :          0 :                 DRV_LOG(ERR, "Fail: No context to query");
    1419                 :          0 :                 return -1;
    1420         [ #  # ]:          0 :         } else if (state == ASO_CONNTRACK_WAIT) {
    1421                 :            :                 return 0;
    1422                 :            :         }
    1423         [ #  # ]:          0 :         if (need_lock)
    1424                 :          0 :                 rte_spinlock_lock(&sq->sqsl);
    1425                 :          0 :         res = size - (uint16_t)(sq->head - sq->tail);
    1426         [ #  # ]:          0 :         if (unlikely(!res)) {
    1427         [ #  # ]:          0 :                 if (need_lock)
    1428                 :          0 :                         rte_spinlock_unlock(&sq->sqsl);
    1429                 :          0 :                 DRV_LOG(ERR, "Fail: SQ is full and no free WQE to send");
    1430                 :          0 :                 return 0;
    1431                 :            :         }
    1432         [ #  # ]:          0 :         MLX5_ASO_CT_UPDATE_STATE(ct,
    1433                 :            :                         user_data ? ASO_CONNTRACK_WAIT_ASYNC : ASO_CONNTRACK_QUERY);
    1434                 :          0 :         wqe = &sq->sq_obj.aso_wqes[sq->head & mask];
    1435                 :            :         /* Confirm the location and address of the prefetch instruction. */
    1436                 :          0 :         rte_prefetch0(&sq->sq_obj.aso_wqes[(sq->head + 1) & mask]);
    1437                 :            :         /* Fill next WQE. */
    1438                 :          0 :         wqe_idx = sq->head & mask;
    1439                 :            :         /* Check if this is async mode. */
    1440         [ #  # ]:          0 :         if (user_data) {
    1441                 :            :                 struct mlx5_hw_q_job *job = (struct mlx5_hw_q_job *)user_data;
    1442                 :            : 
    1443                 :          0 :                 sq->elts[wqe_idx].ct = user_data;
    1444                 :          0 :                 job->query.hw = (char *)((uintptr_t)sq->mr.addr + wqe_idx * 64);
    1445                 :            :         } else {
    1446                 :          0 :                 sq->elts[wqe_idx].query_data = data;
    1447                 :          0 :                 sq->elts[wqe_idx].ct = ct;
    1448                 :            :         }
    1449                 :            :         pool = __mlx5_aso_ct_get_pool(sh, ct);
    1450                 :            :         /* Each WQE will have a single CT object. */
    1451         [ #  # ]:          0 :         wqe->general_cseg.misc = rte_cpu_to_be_32(pool->devx_obj->id +
    1452                 :            :                                                   ct->offset);
    1453         [ #  # ]:          0 :         wqe->general_cseg.opcode = rte_cpu_to_be_32(MLX5_OPCODE_ACCESS_ASO |
    1454                 :            :                         (ASO_OPC_MOD_CONNECTION_TRACKING <<
    1455                 :            :                          WQE_CSEG_OPC_MOD_OFFSET) |
    1456                 :            :                         sq->pi << WQE_CSEG_WQE_INDEX_OFFSET);
    1457                 :            :         /*
    1458                 :            :          * There is no write request is required.
    1459                 :            :          * ASO_OPER_LOGICAL_AND and ASO_OP_ALWAYS_FALSE are both 0.
    1460                 :            :          * "BYTEWISE_64BYTE" is needed for a whole context.
    1461                 :            :          * Set to 0 directly to reduce an endian swap. (Modify should rewrite.)
    1462                 :            :          * "data_mask" is ignored.
    1463                 :            :          * Buffer address was already filled during initialization.
    1464                 :            :          */
    1465                 :          0 :         wqe->aso_cseg.operand_masks = rte_cpu_to_be_32(BYTEWISE_64BYTE <<
    1466                 :            :                                         ASO_CSEG_DATA_MASK_MODE_OFFSET);
    1467                 :          0 :         wqe->aso_cseg.data_mask = 0;
    1468                 :          0 :         sq->head++;
    1469                 :            :         /*
    1470                 :            :          * Each WQE contains 2 WQEBB's, even though
    1471                 :            :          * data segment is not used in this case.
    1472                 :            :          */
    1473                 :          0 :         sq->pi += 2;
    1474         [ #  # ]:          0 :         if (push) {
    1475                 :          0 :                 mlx5_doorbell_ring(&sh->tx_uar.bf_db, *(volatile uint64_t *)wqe,
    1476                 :          0 :                                    sq->pi, &sq->sq_obj.db_rec[MLX5_SND_DBR],
    1477                 :          0 :                                    !sh->tx_uar.dbnc);
    1478                 :          0 :                 sq->db_pi = sq->pi;
    1479                 :            :         }
    1480                 :          0 :         sq->db = wqe;
    1481         [ #  # ]:          0 :         if (need_lock)
    1482                 :          0 :                 rte_spinlock_unlock(&sq->sqsl);
    1483                 :            :         return 1;
    1484                 :            : }
    1485                 :            : 
    1486                 :            : /*
    1487                 :            :  * Handle completions from WQEs sent to ASO CT.
    1488                 :            :  *
    1489                 :            :  * @param[in] mng
    1490                 :            :  *   Pointer to the CT pools management structure.
    1491                 :            :  */
    1492                 :            : static void
    1493                 :          0 : mlx5_aso_ct_completion_handle(struct mlx5_dev_ctx_shared *sh __rte_unused,
    1494                 :            :                               struct mlx5_aso_sq *sq,
    1495                 :            :                               bool need_lock)
    1496                 :            : {
    1497                 :            :         struct mlx5_aso_cq *cq = &sq->cq;
    1498                 :            :         volatile struct mlx5_cqe *restrict cqe;
    1499                 :          0 :         const uint32_t cq_size = 1 << cq->log_desc_n;
    1500                 :          0 :         const uint32_t mask = cq_size - 1;
    1501                 :            :         uint32_t idx;
    1502                 :            :         uint32_t next_idx;
    1503                 :            :         uint16_t max;
    1504                 :            :         uint16_t n = 0;
    1505                 :            :         int ret;
    1506                 :            : 
    1507         [ #  # ]:          0 :         if (need_lock)
    1508                 :          0 :                 rte_spinlock_lock(&sq->sqsl);
    1509                 :          0 :         max = (uint16_t)(sq->head - sq->tail);
    1510         [ #  # ]:          0 :         if (unlikely(!max)) {
    1511         [ #  # ]:          0 :                 if (need_lock)
    1512                 :          0 :                         rte_spinlock_unlock(&sq->sqsl);
    1513                 :          0 :                 return;
    1514                 :            :         }
    1515                 :          0 :         next_idx = cq->cq_ci & mask;
    1516                 :            :         do {
    1517                 :          0 :                 idx = next_idx;
    1518                 :          0 :                 next_idx = (cq->cq_ci + 1) & mask;
    1519                 :            :                 /* Need to confirm the position of the prefetch. */
    1520                 :          0 :                 rte_prefetch0(&cq->cq_obj.cqes[next_idx]);
    1521                 :          0 :                 cqe = &cq->cq_obj.cqes[idx];
    1522         [ #  # ]:          0 :                 ret = check_cqe(cqe, cq_size, cq->cq_ci);
    1523                 :            :                 /*
    1524                 :            :                  * Be sure owner read is done before any other cookie field or
    1525                 :            :                  * opaque field.
    1526                 :            :                  */
    1527                 :          0 :                 rte_io_rmb();
    1528         [ #  # ]:          0 :                 if (unlikely(ret != MLX5_CQE_STATUS_SW_OWN)) {
    1529         [ #  # ]:          0 :                         if (likely(ret == MLX5_CQE_STATUS_HW_OWN))
    1530                 :            :                                 break;
    1531                 :          0 :                         mlx5_aso_cqe_err_handle(sq);
    1532                 :            :                 } else {
    1533                 :          0 :                         n++;
    1534                 :            :                 }
    1535                 :          0 :                 cq->cq_ci++;
    1536                 :            :         } while (1);
    1537         [ #  # ]:          0 :         if (likely(n)) {
    1538                 :          0 :                 mlx5_aso_ct_status_update(sq, n);
    1539                 :          0 :                 sq->tail += n;
    1540                 :          0 :                 rte_io_wmb();
    1541         [ #  # ]:          0 :                 cq->cq_obj.db_rec[0] = rte_cpu_to_be_32(cq->cq_ci);
    1542                 :            :         }
    1543         [ #  # ]:          0 :         if (need_lock)
    1544                 :          0 :                 rte_spinlock_unlock(&sq->sqsl);
    1545                 :            : }
    1546                 :            : 
    1547                 :            : /*
    1548                 :            :  * Update connection tracking ASO context by sending WQE.
    1549                 :            :  *
    1550                 :            :  * @param[in] sh
    1551                 :            :  *   Pointer to mlx5_dev_ctx_shared object.
    1552                 :            :  * @param[in] queue
    1553                 :            :  *   The queue index.
    1554                 :            :  * @param[in] ct
    1555                 :            :  *   Pointer to connection tracking offload object.
    1556                 :            :  * @param[in] profile
    1557                 :            :  *   Pointer to connection tracking TCP parameter.
    1558                 :            :  *
    1559                 :            :  * @return
    1560                 :            :  *   0 on success, -1 on failure.
    1561                 :            :  */
    1562                 :            : int
    1563         [ #  # ]:          0 : mlx5_aso_ct_update_by_wqe(struct mlx5_dev_ctx_shared *sh,
    1564                 :            :                           uint32_t queue,
    1565                 :            :                           struct mlx5_aso_ct_action *ct,
    1566                 :            :                           const struct rte_flow_action_conntrack *profile,
    1567                 :            :                           void *user_data,
    1568                 :            :                           bool push)
    1569                 :            : {
    1570                 :            :         uint32_t poll_wqe_times = MLX5_CT_POLL_WQE_CQE_TIMES;
    1571                 :            :         struct mlx5_aso_ct_pool *pool = __mlx5_aso_ct_get_pool(sh, ct);
    1572                 :            :         struct mlx5_aso_sq *sq;
    1573                 :          0 :         bool need_lock = !!(queue == MLX5_HW_INV_QUEUE);
    1574                 :            :         int ret;
    1575                 :            : 
    1576         [ #  # ]:          0 :         if (sh->config.dv_flow_en == 2)
    1577                 :            :                 sq = __mlx5_aso_ct_get_sq_in_hws(queue, pool);
    1578                 :            :         else
    1579                 :            :                 sq = __mlx5_aso_ct_get_sq_in_sws(sh, ct);
    1580         [ #  # ]:          0 :         if (queue != MLX5_HW_INV_QUEUE) {
    1581                 :          0 :                 ret = mlx5_aso_ct_sq_enqueue_single(sh, sq, ct, profile,
    1582                 :            :                                                     need_lock, user_data, push);
    1583         [ #  # ]:          0 :                 return ret > 0 ? 0 : -1;
    1584                 :            :         }
    1585                 :            :         do {
    1586                 :          0 :                 mlx5_aso_ct_completion_handle(sh, sq,  need_lock);
    1587         [ #  # ]:          0 :                 if (mlx5_aso_ct_sq_enqueue_single(sh, sq, ct, profile,
    1588                 :            :                                                   need_lock, NULL, true))
    1589                 :            :                         return 0;
    1590                 :            :                 /* Waiting for wqe resource. */
    1591                 :          0 :                 rte_delay_us_sleep(10u);
    1592         [ #  # ]:          0 :         } while (--poll_wqe_times);
    1593                 :          0 :         DRV_LOG(ERR, "Fail to send WQE for ASO CT %d in pool %d",
    1594                 :            :                 ct->offset, pool->index);
    1595                 :          0 :         return -1;
    1596                 :            : }
    1597                 :            : 
    1598                 :            : /*
    1599                 :            :  * The routine is used to wait for WQE completion to continue with queried data.
    1600                 :            :  *
    1601                 :            :  * @param[in] sh
    1602                 :            :  *   Pointer to mlx5_dev_ctx_shared object.
    1603                 :            :  * @param[in] queue
    1604                 :            :  *   The queue which CT works on..
    1605                 :            :  * @param[in] ct
    1606                 :            :  *   Pointer to connection tracking offload object.
    1607                 :            :  *
    1608                 :            :  * @return
    1609                 :            :  *   0 on success, -1 on failure.
    1610                 :            :  */
    1611                 :            : int
    1612         [ #  # ]:          0 : mlx5_aso_ct_wait_ready(struct mlx5_dev_ctx_shared *sh, uint32_t queue,
    1613                 :            :                        struct mlx5_aso_ct_action *ct)
    1614                 :            : {
    1615                 :            :         uint32_t poll_cqe_times = MLX5_CT_POLL_WQE_CQE_TIMES;
    1616                 :            :         struct mlx5_aso_ct_pool *pool = __mlx5_aso_ct_get_pool(sh, ct);
    1617                 :            :         struct mlx5_aso_sq *sq;
    1618                 :          0 :         bool need_lock = !!(queue == MLX5_HW_INV_QUEUE);
    1619                 :            : 
    1620         [ #  # ]:          0 :         if (sh->config.dv_flow_en == 2)
    1621                 :            :                 sq = __mlx5_aso_ct_get_sq_in_hws(queue, pool);
    1622                 :            :         else
    1623                 :            :                 sq = __mlx5_aso_ct_get_sq_in_sws(sh, ct);
    1624         [ #  # ]:          0 :         if (rte_atomic_load_explicit(&ct->state, rte_memory_order_relaxed) ==
    1625                 :            :             ASO_CONNTRACK_READY)
    1626                 :            :                 return 0;
    1627                 :            :         do {
    1628                 :          0 :                 mlx5_aso_ct_completion_handle(sh, sq, need_lock);
    1629         [ #  # ]:          0 :                 if (rte_atomic_load_explicit(&ct->state, rte_memory_order_relaxed) ==
    1630                 :            :                     ASO_CONNTRACK_READY)
    1631                 :            :                         return 0;
    1632                 :            :                 /* Waiting for CQE ready, consider should block or sleep. */
    1633                 :          0 :                 rte_delay_us_sleep(MLX5_ASO_WQE_CQE_RESPONSE_DELAY);
    1634         [ #  # ]:          0 :         } while (--poll_cqe_times);
    1635                 :          0 :         DRV_LOG(ERR, "Fail to poll CQE for ASO CT %d in pool %d",
    1636                 :            :                 ct->offset, pool->index);
    1637                 :          0 :         return -1;
    1638                 :            : }
    1639                 :            : 
    1640                 :            : /*
    1641                 :            :  * Convert the hardware conntrack data format into the profile.
    1642                 :            :  *
    1643                 :            :  * @param[in] profile
    1644                 :            :  *   Pointer to conntrack profile to be filled after query.
    1645                 :            :  * @param[in] wdata
    1646                 :            :  *   Pointer to data fetched from hardware.
    1647                 :            :  */
    1648                 :            : void
    1649                 :          0 : mlx5_aso_ct_obj_analyze(struct rte_flow_action_conntrack *profile,
    1650                 :            :                         char *wdata)
    1651                 :            : {
    1652                 :            :         void *o_dir = MLX5_ADDR_OF(conn_track_aso, wdata, original_dir);
    1653                 :            :         void *r_dir = MLX5_ADDR_OF(conn_track_aso, wdata, reply_dir);
    1654                 :            : 
    1655                 :            :         /* MLX5_GET16 should be taken into consideration. */
    1656                 :          0 :         profile->state = (enum rte_flow_conntrack_state)
    1657         [ #  # ]:          0 :                          MLX5_GET(conn_track_aso, wdata, state);
    1658         [ #  # ]:          0 :         profile->enable = !MLX5_GET(conn_track_aso, wdata, freeze_track);
    1659         [ #  # ]:          0 :         profile->selective_ack = MLX5_GET(conn_track_aso, wdata,
    1660                 :            :                                           sack_permitted);
    1661         [ #  # ]:          0 :         profile->live_connection = MLX5_GET(conn_track_aso, wdata,
    1662                 :            :                                             connection_assured);
    1663         [ #  # ]:          0 :         profile->challenge_ack_passed = MLX5_GET(conn_track_aso, wdata,
    1664                 :            :                                                  challenged_acked);
    1665         [ #  # ]:          0 :         profile->max_ack_window = MLX5_GET(conn_track_aso, wdata,
    1666                 :            :                                            max_ack_window);
    1667         [ #  # ]:          0 :         profile->retransmission_limit = MLX5_GET(conn_track_aso, wdata,
    1668                 :            :                                                  retranmission_limit);
    1669         [ #  # ]:          0 :         profile->last_window = MLX5_GET(conn_track_aso, wdata, last_win);
    1670         [ #  # ]:          0 :         profile->last_direction = MLX5_GET(conn_track_aso, wdata, last_dir);
    1671                 :          0 :         profile->last_index = (enum rte_flow_conntrack_tcp_last_index)
    1672         [ #  # ]:          0 :                               MLX5_GET(conn_track_aso, wdata, last_index);
    1673         [ #  # ]:          0 :         profile->last_seq = MLX5_GET(conn_track_aso, wdata, last_seq);
    1674         [ #  # ]:          0 :         profile->last_ack = MLX5_GET(conn_track_aso, wdata, last_ack);
    1675         [ #  # ]:          0 :         profile->last_end = MLX5_GET(conn_track_aso, wdata, last_end);
    1676                 :          0 :         profile->liberal_mode = MLX5_GET(conn_track_aso, wdata,
    1677   [ #  #  #  # ]:          0 :                                 reply_direction_tcp_liberal_enabled) |
    1678                 :          0 :                                 MLX5_GET(conn_track_aso, wdata,
    1679                 :            :                                 original_direction_tcp_liberal_enabled);
    1680                 :            :         /* No liberal in the RTE structure profile. */
    1681         [ #  # ]:          0 :         profile->reply_dir.scale = MLX5_GET(conn_track_aso, wdata,
    1682                 :            :                                             reply_direction_tcp_scale);
    1683         [ #  # ]:          0 :         profile->reply_dir.close_initiated = MLX5_GET(conn_track_aso, wdata,
    1684                 :            :                                         reply_direction_tcp_close_initiated);
    1685         [ #  # ]:          0 :         profile->reply_dir.data_unacked = MLX5_GET(conn_track_aso, wdata,
    1686                 :            :                                         reply_direction_tcp_data_unacked);
    1687         [ #  # ]:          0 :         profile->reply_dir.last_ack_seen = MLX5_GET(conn_track_aso, wdata,
    1688                 :            :                                         reply_direction_tcp_max_ack);
    1689         [ #  # ]:          0 :         profile->reply_dir.sent_end = MLX5_GET(tcp_window_params,
    1690                 :            :                                                r_dir, sent_end);
    1691         [ #  # ]:          0 :         profile->reply_dir.reply_end = MLX5_GET(tcp_window_params,
    1692                 :            :                                                 r_dir, reply_end);
    1693         [ #  # ]:          0 :         profile->reply_dir.max_win = MLX5_GET(tcp_window_params,
    1694                 :            :                                               r_dir, max_win);
    1695         [ #  # ]:          0 :         profile->reply_dir.max_ack = MLX5_GET(tcp_window_params,
    1696                 :            :                                               r_dir, max_ack);
    1697         [ #  # ]:          0 :         profile->original_dir.scale = MLX5_GET(conn_track_aso, wdata,
    1698                 :            :                                                original_direction_tcp_scale);
    1699         [ #  # ]:          0 :         profile->original_dir.close_initiated = MLX5_GET(conn_track_aso, wdata,
    1700                 :            :                                         original_direction_tcp_close_initiated);
    1701         [ #  # ]:          0 :         profile->original_dir.data_unacked = MLX5_GET(conn_track_aso, wdata,
    1702                 :            :                                         original_direction_tcp_data_unacked);
    1703         [ #  # ]:          0 :         profile->original_dir.last_ack_seen = MLX5_GET(conn_track_aso, wdata,
    1704                 :            :                                         original_direction_tcp_max_ack);
    1705         [ #  # ]:          0 :         profile->original_dir.sent_end = MLX5_GET(tcp_window_params,
    1706                 :            :                                                   o_dir, sent_end);
    1707         [ #  # ]:          0 :         profile->original_dir.reply_end = MLX5_GET(tcp_window_params,
    1708                 :            :                                                    o_dir, reply_end);
    1709         [ #  # ]:          0 :         profile->original_dir.max_win = MLX5_GET(tcp_window_params,
    1710                 :            :                                                  o_dir, max_win);
    1711         [ #  # ]:          0 :         profile->original_dir.max_ack = MLX5_GET(tcp_window_params,
    1712                 :            :                                                  o_dir, max_ack);
    1713                 :          0 : }
    1714                 :            : 
    1715                 :            : /*
    1716                 :            :  * Query connection tracking information parameter by send WQE.
    1717                 :            :  *
    1718                 :            :  * @param[in] dev
    1719                 :            :  *   Pointer to Ethernet device.
    1720                 :            :  * @param[in] ct
    1721                 :            :  *   Pointer to connection tracking offload object.
    1722                 :            :  * @param[out] profile
    1723                 :            :  *   Pointer to connection tracking TCP information.
    1724                 :            :  *
    1725                 :            :  * @return
    1726                 :            :  *   0 on success, -1 on failure.
    1727                 :            :  */
    1728                 :            : int
    1729         [ #  # ]:          0 : mlx5_aso_ct_query_by_wqe(struct mlx5_dev_ctx_shared *sh,
    1730                 :            :                          uint32_t queue,
    1731                 :            :                          struct mlx5_aso_ct_action *ct,
    1732                 :            :                          struct rte_flow_action_conntrack *profile,
    1733                 :            :                          void *user_data, bool push)
    1734                 :            : {
    1735                 :            :         uint32_t poll_wqe_times = MLX5_CT_POLL_WQE_CQE_TIMES;
    1736                 :            :         struct mlx5_aso_ct_pool *pool = __mlx5_aso_ct_get_pool(sh, ct);
    1737                 :            :         struct mlx5_aso_sq *sq;
    1738                 :          0 :         bool need_lock = !!(queue == MLX5_HW_INV_QUEUE);
    1739                 :            :         char out_data[64 * 2];
    1740                 :            :         int ret;
    1741                 :            : 
    1742         [ #  # ]:          0 :         if (sh->config.dv_flow_en == 2)
    1743                 :            :                 sq = __mlx5_aso_ct_get_sq_in_hws(queue, pool);
    1744                 :            :         else
    1745                 :            :                 sq = __mlx5_aso_ct_get_sq_in_sws(sh, ct);
    1746         [ #  # ]:          0 :         if (queue != MLX5_HW_INV_QUEUE) {
    1747                 :          0 :                 ret = mlx5_aso_ct_sq_query_single(sh, sq, ct, out_data,
    1748                 :            :                                                   need_lock, user_data, push);
    1749         [ #  # ]:          0 :                 return ret > 0 ? 0 : -1;
    1750                 :            :         }
    1751                 :            :         do {
    1752                 :          0 :                 mlx5_aso_ct_completion_handle(sh, sq, need_lock);
    1753                 :          0 :                 ret = mlx5_aso_ct_sq_query_single(sh, sq, ct, out_data,
    1754                 :            :                                 need_lock, NULL, true);
    1755         [ #  # ]:          0 :                 if (ret < 0)
    1756                 :          0 :                         return ret;
    1757         [ #  # ]:          0 :                 else if (ret > 0)
    1758                 :          0 :                         goto data_handle;
    1759                 :            :                 /* Waiting for wqe resource or state. */
    1760                 :            :                 else
    1761                 :          0 :                         rte_delay_us_sleep(10u);
    1762         [ #  # ]:          0 :         } while (--poll_wqe_times);
    1763                 :          0 :         DRV_LOG(ERR, "Fail to send WQE for ASO CT %d in pool %d",
    1764                 :            :                 ct->offset, pool->index);
    1765                 :          0 :         return -1;
    1766                 :            : data_handle:
    1767                 :          0 :         ret = mlx5_aso_ct_wait_ready(sh, queue, ct);
    1768         [ #  # ]:          0 :         if (!ret)
    1769                 :          0 :                 mlx5_aso_ct_obj_analyze(profile, out_data);
    1770                 :            :         return ret;
    1771                 :            : }
    1772                 :            : 
    1773                 :            : /*
    1774                 :            :  * Make sure the conntrack context is synchronized with hardware before
    1775                 :            :  * creating a flow rule that uses it.
    1776                 :            :  *
    1777                 :            :  * @param[in] sh
    1778                 :            :  *   Pointer to shared device context.
    1779                 :            :  * @param[in] ct
    1780                 :            :  *   Pointer to connection tracking offload object.
    1781                 :            :  *
    1782                 :            :  * @return
    1783                 :            :  *   0 on success, a negative errno value otherwise and rte_errno is set.
    1784                 :            :  */
    1785                 :            : int
    1786         [ #  # ]:          0 : mlx5_aso_ct_available(struct mlx5_dev_ctx_shared *sh,
    1787                 :            :                       uint32_t queue,
    1788                 :            :                       struct mlx5_aso_ct_action *ct)
    1789                 :            : {
    1790                 :            :         struct mlx5_aso_ct_pool *pool = __mlx5_aso_ct_get_pool(sh, ct);
    1791                 :            :         struct mlx5_aso_sq *sq;
    1792                 :          0 :         bool need_lock = !!(queue == MLX5_HW_INV_QUEUE);
    1793                 :            :         uint32_t poll_cqe_times = MLX5_CT_POLL_WQE_CQE_TIMES;
    1794                 :            :         enum mlx5_aso_ct_state state =
    1795                 :          0 :                                 rte_atomic_load_explicit(&ct->state, rte_memory_order_relaxed);
    1796                 :            : 
    1797         [ #  # ]:          0 :         if (sh->config.dv_flow_en == 2)
    1798                 :            :                 sq = __mlx5_aso_ct_get_sq_in_hws(queue, pool);
    1799                 :            :         else
    1800                 :            :                 sq = __mlx5_aso_ct_get_sq_in_sws(sh, ct);
    1801         [ #  # ]:          0 :         if (state == ASO_CONNTRACK_FREE) {
    1802                 :          0 :                 rte_errno = ENXIO;
    1803                 :          0 :                 return -rte_errno;
    1804                 :          0 :         } else if (state == ASO_CONNTRACK_READY ||
    1805         [ #  # ]:          0 :                    state == ASO_CONNTRACK_QUERY ||
    1806                 :            :                    state == ASO_CONNTRACK_WAIT_ASYNC) {
    1807                 :            :                 return 0;
    1808                 :            :         }
    1809                 :            :         do {
    1810                 :          0 :                 mlx5_aso_ct_completion_handle(sh, sq, need_lock);
    1811                 :          0 :                 state = rte_atomic_load_explicit(&ct->state, rte_memory_order_relaxed);
    1812         [ #  # ]:          0 :                 if (state == ASO_CONNTRACK_READY ||
    1813                 :            :                     state == ASO_CONNTRACK_QUERY)
    1814                 :            :                         return 0;
    1815                 :            :                 /* Waiting for CQE ready, consider should block or sleep.  */
    1816                 :          0 :                 rte_delay_us_block(MLX5_ASO_WQE_CQE_RESPONSE_DELAY);
    1817         [ #  # ]:          0 :         } while (--poll_cqe_times);
    1818                 :          0 :         rte_errno = EBUSY;
    1819                 :          0 :         return -rte_errno;
    1820                 :            : }
    1821                 :            : 
    1822                 :            : int
    1823                 :          0 : mlx5_aso_cnt_queue_init(struct mlx5_dev_ctx_shared *sh)
    1824                 :            : {
    1825                 :            :         struct mlx5_hws_aso_mng *aso_mng = NULL;
    1826                 :            :         uint8_t idx;
    1827                 :            :         struct mlx5_aso_sq *sq;
    1828                 :            : 
    1829                 :            :         MLX5_ASSERT(sh);
    1830                 :            :         MLX5_ASSERT(sh->cnt_svc);
    1831                 :          0 :         aso_mng = &sh->cnt_svc->aso_mng;
    1832                 :          0 :         aso_mng->sq_num = HWS_CNT_ASO_SQ_NUM;
    1833         [ #  # ]:          0 :         for (idx = 0; idx < HWS_CNT_ASO_SQ_NUM; idx++) {
    1834                 :          0 :                 sq = &aso_mng->sqs[idx];
    1835         [ #  # ]:          0 :                 if (mlx5_aso_sq_create(sh->cdev, sq, sh->tx_uar.obj,
    1836                 :            :                                         MLX5_ASO_CNT_QUEUE_LOG_DESC))
    1837                 :          0 :                         goto error;
    1838                 :          0 :                 mlx5_aso_cnt_init_sq(sq);
    1839                 :            :         }
    1840                 :            :         return 0;
    1841                 :            : error:
    1842                 :          0 :         mlx5_aso_cnt_queue_uninit(sh);
    1843                 :          0 :         return -1;
    1844                 :            : }
    1845                 :            : 
    1846                 :            : void
    1847                 :          0 : mlx5_aso_cnt_queue_uninit(struct mlx5_dev_ctx_shared *sh)
    1848                 :            : {
    1849                 :            :         uint16_t idx;
    1850                 :            : 
    1851         [ #  # ]:          0 :         for (idx = 0; idx < sh->cnt_svc->aso_mng.sq_num; idx++)
    1852                 :          0 :                 mlx5_aso_destroy_sq(&sh->cnt_svc->aso_mng.sqs[idx]);
    1853                 :          0 :         sh->cnt_svc->aso_mng.sq_num = 0;
    1854                 :          0 : }
    1855                 :            : 
    1856                 :            : static uint32_t
    1857                 :            : aso_hw_id(uint32_t base, uint32_t offset)
    1858                 :            : {
    1859                 :          0 :         return (base + offset) / 4;
    1860                 :            : }
    1861                 :            : 
    1862                 :            : static uint32_t
    1863                 :          0 : mlx5_aso_cnt_sq_enqueue_burst(struct mlx5_hws_cnt_pool *cpool,
    1864                 :            :                 struct mlx5_dev_ctx_shared *sh,
    1865                 :            :                 struct mlx5_aso_sq *sq, uint32_t n,
    1866                 :            :                 uint32_t stats_mem_idx, uint32_t aso_id)
    1867                 :            : {
    1868                 :            :         volatile struct mlx5_aso_wqe *wqe;
    1869                 :          0 :         uint16_t size = 1 << sq->log_desc_n;
    1870                 :          0 :         uint16_t mask = size - 1;
    1871                 :            :         uint16_t max;
    1872                 :            :         uint64_t addr;
    1873                 :          0 :         uint8_t opcmod = sh->cdev->config.hca_attr.flow_access_aso_opc_mod;
    1874         [ #  # ]:          0 :         rte_be32_t lkey = rte_cpu_to_be_32(cpool->raw_mng->mr.lkey);
    1875                 :          0 :         uint16_t aso_n = (uint16_t)(RTE_ALIGN_CEIL(n, 4) / 4);
    1876                 :            :         uint32_t bursted_cnts = 0;
    1877                 :            : 
    1878                 :          0 :         max = RTE_MIN(size - (uint16_t)(sq->head - sq->tail), aso_n);
    1879         [ #  # ]:          0 :         if (unlikely(!max))
    1880                 :            :                 return 0;
    1881                 :            :         /* Because only one burst at one time, we can use the same elt. */
    1882                 :          0 :         sq->elts[0].burst_size = max;
    1883                 :            :         do {
    1884                 :          0 :                 wqe = &sq->sq_obj.aso_wqes[sq->head & mask];
    1885                 :          0 :                 rte_prefetch0(&sq->sq_obj.aso_wqes[(sq->head + 1) & mask]);
    1886         [ #  # ]:          0 :                 wqe->general_cseg.misc = rte_cpu_to_be_32(aso_id);
    1887                 :          0 :                 wqe->general_cseg.flags = RTE_BE32(MLX5_COMP_ONLY_FIRST_ERR <<
    1888                 :            :                                                          MLX5_COMP_MODE_OFFSET);
    1889         [ #  # ]:          0 :                 wqe->general_cseg.opcode = rte_cpu_to_be_32
    1890                 :            :                                                 (MLX5_OPCODE_ACCESS_ASO |
    1891                 :            :                                                  (opcmod <<
    1892                 :            :                                                   WQE_CSEG_OPC_MOD_OFFSET) |
    1893                 :            :                                                  (sq->pi <<
    1894                 :            :                                                   WQE_CSEG_WQE_INDEX_OFFSET));
    1895                 :          0 :                 addr = (uint64_t)RTE_PTR_ADD(cpool->raw_mng->raw,
    1896                 :            :                                 stats_mem_idx * sizeof(struct flow_counter_stats));
    1897         [ #  # ]:          0 :                 wqe->aso_cseg.va_h = rte_cpu_to_be_32((uint32_t)(addr >> 32));
    1898         [ #  # ]:          0 :                 wqe->aso_cseg.va_l_r = rte_cpu_to_be_32((uint32_t)addr | 1u);
    1899                 :          0 :                 wqe->aso_cseg.lkey = lkey;
    1900                 :          0 :                 sq->pi += 2; /* Each WQE contains 2 WQEBB's. */
    1901                 :          0 :                 sq->head++;
    1902                 :          0 :                 sq->next++;
    1903                 :          0 :                 aso_id++;
    1904                 :          0 :                 max--;
    1905                 :          0 :                 stats_mem_idx += 4;
    1906         [ #  # ]:          0 :         } while (max);
    1907                 :          0 :         wqe->general_cseg.flags = RTE_BE32(MLX5_COMP_ALWAYS <<
    1908                 :            :                                                          MLX5_COMP_MODE_OFFSET);
    1909                 :          0 :         mlx5_doorbell_ring(&sh->tx_uar.bf_db, *(volatile uint64_t *)wqe,
    1910                 :          0 :                            sq->pi, &sq->sq_obj.db_rec[MLX5_SND_DBR],
    1911                 :          0 :                            !sh->tx_uar.dbnc);
    1912                 :          0 :         bursted_cnts = RTE_MIN((uint32_t)(sq->elts[0].burst_size * 4), n);
    1913                 :          0 :         return bursted_cnts;
    1914                 :            : }
    1915                 :            : 
    1916                 :            : static uint16_t
    1917                 :          0 : mlx5_aso_cnt_completion_handle(struct mlx5_aso_sq *sq)
    1918                 :            : {
    1919                 :            :         struct mlx5_aso_cq *cq = &sq->cq;
    1920                 :            :         volatile struct mlx5_cqe *restrict cqe;
    1921                 :          0 :         const unsigned int cq_size = 1 << cq->log_desc_n;
    1922                 :          0 :         const unsigned int mask = cq_size - 1;
    1923                 :            :         uint32_t idx;
    1924                 :          0 :         uint32_t next_idx = cq->cq_ci & mask;
    1925                 :          0 :         const uint16_t max = (uint16_t)(sq->head - sq->tail);
    1926                 :            :         uint16_t i = 0;
    1927                 :            :         int ret;
    1928         [ #  # ]:          0 :         if (unlikely(!max))
    1929                 :            :                 return 0;
    1930                 :            :         idx = next_idx;
    1931                 :          0 :         next_idx = (cq->cq_ci + 1) & mask;
    1932                 :          0 :         rte_prefetch0(&cq->cq_obj.cqes[next_idx]);
    1933                 :          0 :         cqe = &cq->cq_obj.cqes[idx];
    1934         [ #  # ]:          0 :         ret = check_cqe(cqe, cq_size, cq->cq_ci);
    1935                 :            :         /*
    1936                 :            :          * Be sure owner read is done before any other cookie field or
    1937                 :            :          * opaque field.
    1938                 :            :          */
    1939                 :          0 :         rte_io_rmb();
    1940         [ #  # ]:          0 :         if (unlikely(ret != MLX5_CQE_STATUS_SW_OWN)) {
    1941         [ #  # ]:          0 :                 if (likely(ret == MLX5_CQE_STATUS_HW_OWN))
    1942                 :            :                         return 0; /* return immediately. */
    1943                 :          0 :                 mlx5_aso_cqe_err_handle(sq);
    1944                 :            :         }
    1945                 :          0 :         i += sq->elts[0].burst_size;
    1946                 :          0 :         sq->elts[0].burst_size = 0;
    1947                 :          0 :         cq->cq_ci++;
    1948         [ #  # ]:          0 :         if (likely(i)) {
    1949                 :          0 :                 sq->tail += i;
    1950                 :          0 :                 rte_io_wmb();
    1951         [ #  # ]:          0 :                 cq->cq_obj.db_rec[0] = rte_cpu_to_be_32(cq->cq_ci);
    1952                 :            :         }
    1953                 :            :         return i;
    1954                 :            : }
    1955                 :            : 
    1956                 :            : static uint64_t
    1957                 :          0 : mlx5_aso_cnt_query_one_dcs(struct mlx5_dev_ctx_shared *sh,
    1958                 :            :                            struct mlx5_hws_cnt_pool *cpool,
    1959                 :            :                            uint8_t dcs_idx, uint32_t num)
    1960                 :            : {
    1961                 :          0 :         uint32_t dcs_id = cpool->dcs_mng.dcs[dcs_idx].obj->id;
    1962                 :          0 :         uint64_t cnt_num = cpool->dcs_mng.dcs[dcs_idx].batch_sz;
    1963                 :            :         uint64_t left;
    1964                 :          0 :         uint32_t iidx = cpool->dcs_mng.dcs[dcs_idx].iidx;
    1965                 :            :         uint32_t bursted, dcs_offset = 0;
    1966                 :            :         uint16_t mask;
    1967                 :            :         uint16_t sq_idx;
    1968                 :            :         uint64_t burst_sz = (uint64_t)(1 << MLX5_ASO_CNT_QUEUE_LOG_DESC) * 4 *
    1969                 :            :                 sh->cnt_svc->aso_mng.sq_num;
    1970                 :            :         uint64_t qburst_sz = burst_sz / sh->cnt_svc->aso_mng.sq_num;
    1971                 :            :         uint64_t n;
    1972                 :            :         struct mlx5_aso_sq *sq;
    1973                 :            : 
    1974                 :          0 :         cnt_num = RTE_MIN(num, cnt_num);
    1975                 :            :         left = cnt_num;
    1976         [ #  # ]:          0 :         while (left) {
    1977                 :            :                 mask = 0;
    1978         [ #  # ]:          0 :                 for (sq_idx = 0; sq_idx < sh->cnt_svc->aso_mng.sq_num;
    1979                 :          0 :                                 sq_idx++) {
    1980         [ #  # ]:          0 :                         if (left == 0) {
    1981                 :          0 :                                 mask |= (1 << sq_idx);
    1982                 :          0 :                                 continue;
    1983                 :            :                         }
    1984                 :          0 :                         n = RTE_MIN(left, qburst_sz);
    1985                 :          0 :                         bursted = mlx5_aso_cnt_sq_enqueue_burst(cpool, sh,
    1986                 :          0 :                                                         &sh->cnt_svc->aso_mng.sqs[sq_idx], n,
    1987                 :            :                                                         iidx, aso_hw_id(dcs_id, dcs_offset));
    1988                 :          0 :                         left -= bursted;
    1989                 :          0 :                         dcs_offset += bursted;
    1990                 :          0 :                         iidx += bursted;
    1991                 :            :                 }
    1992                 :            :                 do {
    1993         [ #  # ]:          0 :                         for (sq_idx = 0; sq_idx < sh->cnt_svc->aso_mng.sq_num;
    1994                 :          0 :                                         sq_idx++) {
    1995                 :          0 :                                 sq = &sh->cnt_svc->aso_mng.sqs[sq_idx];
    1996         [ #  # ]:          0 :                                 if (mlx5_aso_cnt_completion_handle(sq))
    1997                 :          0 :                                         mask |= (1 << sq_idx);
    1998                 :            :                         }
    1999         [ #  # ]:          0 :                 } while (mask < ((1 << sh->cnt_svc->aso_mng.sq_num) - 1));
    2000                 :            :         }
    2001                 :          0 :         return cnt_num;
    2002                 :            : }
    2003                 :            : 
    2004                 :            : /*
    2005                 :            :  * Query FW counter via ASO WQE.
    2006                 :            :  *
    2007                 :            :  * ASO query counter use _sync_ mode, means:
    2008                 :            :  * 1. each SQ issue one burst with several WQEs
    2009                 :            :  * 2. ask for CQE at last WQE
    2010                 :            :  * 3. busy poll CQ of each SQ's
    2011                 :            :  * 4. If all SQ's CQE are received then goto step 1, issue next burst
    2012                 :            :  *
    2013                 :            :  * @param[in] sh
    2014                 :            :  *   Pointer to shared device.
    2015                 :            :  * @param[in] cpool
    2016                 :            :  *   Pointer to counter pool.
    2017                 :            :  *
    2018                 :            :  * @return
    2019                 :            :  *   0 on success, -1 on failure.
    2020                 :            :  */
    2021                 :            : int
    2022         [ #  # ]:          0 : mlx5_aso_cnt_query(struct mlx5_dev_ctx_shared *sh,
    2023                 :            :                    struct mlx5_hws_cnt_pool *cpool)
    2024                 :            : {
    2025                 :            :         uint32_t idx;
    2026                 :            :         uint32_t num;
    2027                 :          0 :         uint32_t cnt_num = mlx5_hws_cnt_pool_get_size(cpool) -
    2028                 :          0 :                 rte_ring_count(cpool->free_list);
    2029                 :            : 
    2030         [ #  # ]:          0 :         for (idx = 0; idx < cpool->dcs_mng.batch_total; idx++) {
    2031                 :          0 :                 num = RTE_MIN(cnt_num, cpool->dcs_mng.dcs[idx].batch_sz);
    2032                 :          0 :                 mlx5_aso_cnt_query_one_dcs(sh, cpool, idx, num);
    2033                 :          0 :                 cnt_num -= num;
    2034         [ #  # ]:          0 :                 if (cnt_num == 0)
    2035                 :            :                         break;
    2036                 :            :         }
    2037                 :          0 :         return 0;
    2038                 :            : }

Generated by: LCOV version 1.14