LCOV - code coverage report
Current view: top level - drivers/net/mlx5 - mlx5_hws_cnt.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 0 537 0.0 %
Date: 2025-10-01 17:51:42 Functions: 0 29 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 282 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright 2020 Mellanox Technologies, Ltd
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <stdint.h>
       6                 :            : #include <rte_malloc.h>
       7                 :            : #include <mlx5_malloc.h>
       8                 :            : #include <rte_ring.h>
       9                 :            : #include <mlx5_devx_cmds.h>
      10                 :            : #include <rte_cycles.h>
      11                 :            : #include <rte_eal_paging.h>
      12                 :            : #include <rte_thread.h>
      13                 :            : 
      14                 :            : #if defined(HAVE_IBV_FLOW_DV_SUPPORT) || !defined(HAVE_INFINIBAND_VERBS_H)
      15                 :            : 
      16                 :            : #include "mlx5_utils.h"
      17                 :            : #include "mlx5_hws_cnt.h"
      18                 :            : 
      19                 :            : #define HWS_CNT_CACHE_SZ_DEFAULT 511
      20                 :            : #define HWS_CNT_CACHE_PRELOAD_DEFAULT 254
      21                 :            : #define HWS_CNT_CACHE_FETCH_DEFAULT 254
      22                 :            : #define HWS_CNT_CACHE_THRESHOLD_DEFAULT 254
      23                 :            : #define HWS_CNT_ALLOC_FACTOR_DEFAULT 20
      24                 :            : 
      25                 :            : static int
      26         [ #  # ]:          0 : __hws_cnt_id_load(struct mlx5_hws_cnt_pool *cpool)
      27                 :            : {
      28                 :            :         uint32_t cnt_num = mlx5_hws_cnt_pool_get_size(cpool);
      29                 :            :         uint32_t iidx;
      30                 :            :         cnt_id_t *cnt_arr = NULL;
      31                 :            : 
      32                 :          0 :         cnt_arr = mlx5_malloc(MLX5_MEM_ANY | MLX5_MEM_ZERO,
      33                 :            :                               cnt_num * sizeof(cnt_id_t), 0, SOCKET_ID_ANY);
      34         [ #  # ]:          0 :         if (cnt_arr == NULL)
      35                 :            :                 return -ENOMEM;
      36                 :            :         /*
      37                 :            :          * Counter ID order is important for tracking the max number of in used
      38                 :            :          * counter for querying, which means counter internal index order must
      39                 :            :          * be from zero to the number user configured, i.e: 0 - 8000000.
      40                 :            :          * Need to load counter ID in this order into the cache firstly,
      41                 :            :          * and then the global free list.
      42                 :            :          * In the end, user fetch the counter from minimal to the maximum.
      43                 :            :          */
      44         [ #  # ]:          0 :         for (iidx = 0; iidx < cnt_num; iidx++) {
      45                 :            :                 cnt_id_t cnt_id  = mlx5_hws_cnt_id_gen(cpool, iidx);
      46                 :          0 :                 cnt_arr[iidx] = cnt_id;
      47                 :            :         }
      48   [ #  #  #  #  :          0 :         rte_ring_enqueue_bulk_elem(cpool->free_list, cnt_arr,
                      # ]
      49                 :            :                                    sizeof(cnt_id_t), cnt_num, NULL);
      50                 :          0 :         mlx5_free(cnt_arr);
      51                 :          0 :         return 0;
      52                 :            : }
      53                 :            : 
      54                 :            : static void
      55                 :          0 : __mlx5_hws_cnt_svc(struct mlx5_dev_ctx_shared *sh,
      56                 :            :                    struct mlx5_hws_cnt_pool *cpool)
      57                 :            : {
      58                 :          0 :         struct rte_ring *reset_list = cpool->wait_reset_list;
      59                 :          0 :         struct rte_ring *reuse_list = cpool->reuse_list;
      60                 :            :         uint32_t reset_cnt_num;
      61                 :            :         struct rte_ring_zc_data zcdr = {0};
      62                 :            :         struct rte_ring_zc_data zcdu = {0};
      63                 :            :         uint32_t ret __rte_unused;
      64                 :            : 
      65                 :            :         reset_cnt_num = rte_ring_count(reset_list);
      66                 :          0 :         cpool->query_gen++;
      67                 :          0 :         mlx5_aso_cnt_query(sh, cpool);
      68                 :            :         zcdr.n1 = 0;
      69                 :            :         zcdu.n1 = 0;
      70                 :            :         ret = rte_ring_enqueue_zc_burst_elem_start(reuse_list,
      71                 :            :                                                    sizeof(cnt_id_t),
      72                 :            :                                                    reset_cnt_num, &zcdu,
      73                 :            :                                                    NULL);
      74                 :            :         MLX5_ASSERT(ret == reset_cnt_num);
      75                 :            :         ret = rte_ring_dequeue_zc_burst_elem_start(reset_list,
      76                 :            :                                                    sizeof(cnt_id_t),
      77                 :            :                                                    reset_cnt_num, &zcdr,
      78                 :            :                                                    NULL);
      79                 :            :         MLX5_ASSERT(ret == reset_cnt_num);
      80                 :            :         __hws_cnt_r2rcpy(&zcdu, &zcdr, reset_cnt_num);
      81                 :            :         rte_ring_dequeue_zc_elem_finish(reset_list, reset_cnt_num);
      82                 :            :         rte_ring_enqueue_zc_elem_finish(reuse_list, reset_cnt_num);
      83                 :            : 
      84         [ #  # ]:          0 :         if (rte_log_can_log(mlx5_logtype, RTE_LOG_DEBUG)) {
      85                 :            :                 reset_cnt_num = rte_ring_count(reset_list);
      86                 :          0 :                 DRV_LOG(DEBUG, "ibdev %s cpool %p wait_reset_cnt=%" PRIu32,
      87                 :            :                                sh->ibdev_name, (void *)cpool, reset_cnt_num);
      88                 :            :         }
      89                 :          0 : }
      90                 :            : 
      91                 :            : /**
      92                 :            :  * Release AGE parameter.
      93                 :            :  *
      94                 :            :  * @param priv
      95                 :            :  *   Pointer to the port private data structure.
      96                 :            :  * @param own_cnt_index
      97                 :            :  *   Counter ID to created only for this AGE to release.
      98                 :            :  *   Zero means there is no such counter.
      99                 :            :  * @param age_ipool
     100                 :            :  *   Pointer to AGE parameter indexed pool.
     101                 :            :  * @param idx
     102                 :            :  *   Index of AGE parameter in the indexed pool.
     103                 :            :  */
     104                 :            : static void
     105                 :          0 : mlx5_hws_age_param_free(struct mlx5_priv *priv, cnt_id_t own_cnt_index,
     106                 :            :                         struct mlx5_indexed_pool *age_ipool, uint32_t idx)
     107                 :            : {
     108         [ #  # ]:          0 :         if (own_cnt_index) {
     109         [ #  # ]:          0 :                 struct mlx5_hws_cnt_pool *cpool = priv->hws_cpool;
     110                 :            : 
     111                 :            :                 MLX5_ASSERT(mlx5_hws_cnt_is_shared(cpool, own_cnt_index));
     112                 :            :                 mlx5_hws_cnt_shared_put(cpool, &own_cnt_index);
     113                 :            :         }
     114                 :          0 :         mlx5_ipool_free(age_ipool, idx);
     115                 :          0 : }
     116                 :            : 
     117                 :            : /**
     118                 :            :  * Check and callback event for new aged flow in the HWS counter pool.
     119                 :            :  *
     120                 :            :  * @param[in] priv
     121                 :            :  *   Pointer to port private object.
     122                 :            :  * @param[in] cpool
     123                 :            :  *   Pointer to current counter pool.
     124                 :            :  */
     125                 :            : static void
     126                 :          0 : mlx5_hws_aging_check(struct mlx5_priv *priv, struct mlx5_hws_cnt_pool *cpool)
     127                 :            : {
     128                 :          0 :         struct mlx5_age_info *age_info = GET_PORT_AGE_INFO(priv);
     129                 :          0 :         struct flow_counter_stats *stats = cpool->raw_mng->raw;
     130                 :            :         struct mlx5_hws_age_param *param;
     131                 :            :         struct rte_ring *r;
     132                 :          0 :         const uint64_t curr_time = MLX5_CURR_TIME_SEC;
     133         [ #  # ]:          0 :         const uint32_t time_delta = curr_time - cpool->time_of_last_age_check;
     134                 :            :         uint32_t nb_alloc_cnts = mlx5_hws_cnt_pool_get_size(cpool);
     135                 :            :         uint16_t expected1 = HWS_AGE_CANDIDATE;
     136                 :            :         uint16_t expected2 = HWS_AGE_CANDIDATE_INSIDE_RING;
     137                 :            :         uint32_t i;
     138                 :            : 
     139                 :          0 :         cpool->time_of_last_age_check = curr_time;
     140         [ #  # ]:          0 :         for (i = 0; i < nb_alloc_cnts; ++i) {
     141                 :          0 :                 uint32_t age_idx = cpool->pool[i].age_idx;
     142                 :            :                 uint64_t hits;
     143                 :            : 
     144   [ #  #  #  # ]:          0 :                 if (!cpool->pool[i].in_used || age_idx == 0)
     145                 :          0 :                         continue;
     146                 :          0 :                 param = mlx5_ipool_get(age_info->ages_ipool, age_idx);
     147         [ #  # ]:          0 :                 if (unlikely(param == NULL)) {
     148                 :            :                         /*
     149                 :            :                          * When AGE which used indirect counter it is user
     150                 :            :                          * responsibility not using this indirect counter
     151                 :            :                          * without this AGE.
     152                 :            :                          * If this counter is used after the AGE was freed, the
     153                 :            :                          * AGE index is invalid and using it here will cause a
     154                 :            :                          * segmentation fault.
     155                 :            :                          */
     156                 :          0 :                         DRV_LOG(WARNING,
     157                 :            :                                 "Counter %u is lost his AGE, it is unused.", i);
     158                 :          0 :                         continue;
     159                 :            :                 }
     160         [ #  # ]:          0 :                 if (param->timeout == 0)
     161                 :          0 :                         continue;
     162   [ #  #  #  # ]:          0 :                 switch (rte_atomic_load_explicit(&param->state, rte_memory_order_relaxed)) {
     163                 :          0 :                 case HWS_AGE_AGED_OUT_NOT_REPORTED:
     164                 :            :                 case HWS_AGE_AGED_OUT_REPORTED:
     165                 :            :                         /* Already aged-out, no action is needed. */
     166                 :          0 :                         continue;
     167                 :            :                 case HWS_AGE_CANDIDATE:
     168                 :            :                 case HWS_AGE_CANDIDATE_INSIDE_RING:
     169                 :            :                         /* This AGE candidate to be aged-out, go to checking. */
     170                 :            :                         break;
     171                 :          0 :                 case HWS_AGE_FREE:
     172                 :            :                         /*
     173                 :            :                          * Since this check is async, we may reach a race condition
     174                 :            :                          * where the age and counter are used in the same rule,
     175                 :            :                          * using the same counter index,
     176                 :            :                          * age was freed first, and counter was not freed yet.
     177                 :            :                          * Aging check can be safely ignored in that case.
     178                 :            :                          */
     179                 :          0 :                         continue;
     180                 :          0 :                 default:
     181                 :            :                         MLX5_ASSERT(0);
     182                 :          0 :                         continue;
     183                 :            :                 }
     184         [ #  # ]:          0 :                 hits = rte_be_to_cpu_64(stats[i].hits);
     185         [ #  # ]:          0 :                 if (param->nb_cnts == 1) {
     186         [ #  # ]:          0 :                         if (hits != param->accumulator_last_hits) {
     187                 :          0 :                                 rte_atomic_store_explicit(&param->sec_since_last_hit, 0,
     188                 :            :                                                  rte_memory_order_relaxed);
     189                 :          0 :                                 param->accumulator_last_hits = hits;
     190                 :          0 :                                 continue;
     191                 :            :                         }
     192                 :            :                 } else {
     193                 :          0 :                         param->accumulator_hits += hits;
     194                 :          0 :                         param->accumulator_cnt++;
     195         [ #  # ]:          0 :                         if (param->accumulator_cnt < param->nb_cnts)
     196                 :          0 :                                 continue;
     197                 :          0 :                         param->accumulator_cnt = 0;
     198         [ #  # ]:          0 :                         if (param->accumulator_last_hits !=
     199                 :            :                                                 param->accumulator_hits) {
     200                 :          0 :                                 rte_atomic_store_explicit(&param->sec_since_last_hit,
     201                 :            :                                                  0, rte_memory_order_relaxed);
     202                 :          0 :                                 param->accumulator_last_hits =
     203                 :          0 :                                                         param->accumulator_hits;
     204                 :          0 :                                 param->accumulator_hits = 0;
     205                 :          0 :                                 continue;
     206                 :            :                         }
     207                 :          0 :                         param->accumulator_hits = 0;
     208                 :            :                 }
     209                 :          0 :                 if (rte_atomic_fetch_add_explicit(&param->sec_since_last_hit, time_delta,
     210                 :          0 :                                        rte_memory_order_relaxed) + time_delta <=
     211         [ #  # ]:          0 :                    rte_atomic_load_explicit(&param->timeout, rte_memory_order_relaxed))
     212                 :          0 :                         continue;
     213                 :            :                 /* Prepare the relevant ring for this AGE parameter */
     214         [ #  # ]:          0 :                 if (priv->hws_strict_queue)
     215                 :          0 :                         r = age_info->hw_q_age->aged_lists[param->queue_id];
     216                 :            :                 else
     217                 :          0 :                         r = age_info->hw_age.aged_list;
     218                 :            :                 /* Changing the state atomically and insert it into the ring. */
     219         [ #  # ]:          0 :                 if (rte_atomic_compare_exchange_strong_explicit(&param->state, &expected1,
     220                 :            :                                                 HWS_AGE_AGED_OUT_NOT_REPORTED,
     221                 :            :                                                 rte_memory_order_relaxed,
     222                 :            :                                                 rte_memory_order_relaxed)) {
     223                 :            :                         int ret = rte_ring_enqueue_burst_elem(r, &age_idx,
     224                 :            :                                                               sizeof(uint32_t),
     225                 :            :                                                               1, NULL);
     226                 :            : 
     227                 :            :                         /*
     228                 :            :                          * The ring doesn't have enough room for this entry,
     229                 :            :                          * it replace back the state for the next second.
     230                 :            :                          *
     231                 :            :                          * FIXME: if until next sec it get traffic, we are going
     232                 :            :                          *        to lose this "aged out", will be fixed later
     233                 :            :                          *        when optimise it to fill ring in bulks.
     234                 :            :                          */
     235                 :            :                         expected2 = HWS_AGE_AGED_OUT_NOT_REPORTED;
     236   [ #  #  #  # ]:          0 :                         if (ret == 0 &&
     237                 :          0 :                             !rte_atomic_compare_exchange_strong_explicit(&param->state,
     238                 :            :                                                          &expected2, expected1,
     239                 :            :                                                          rte_memory_order_relaxed,
     240         [ #  # ]:          0 :                                                          rte_memory_order_relaxed) &&
     241                 :            :                             expected2 == HWS_AGE_FREE)
     242                 :          0 :                                 mlx5_hws_age_param_free(priv,
     243                 :            :                                                         param->own_cnt_index,
     244                 :            :                                                         age_info->ages_ipool,
     245                 :            :                                                         age_idx);
     246                 :            :                         /* The event is irrelevant in strict queue mode. */
     247         [ #  # ]:          0 :                         if (!priv->hws_strict_queue)
     248                 :          0 :                                 MLX5_AGE_SET(age_info, MLX5_AGE_EVENT_NEW);
     249                 :            :                 } else {
     250                 :          0 :                         rte_atomic_compare_exchange_strong_explicit(&param->state, &expected2,
     251                 :            :                                                   HWS_AGE_AGED_OUT_NOT_REPORTED,
     252                 :            :                                                   rte_memory_order_relaxed,
     253                 :            :                                                   rte_memory_order_relaxed);
     254                 :            :                 }
     255                 :            :         }
     256                 :            :         /* The event is irrelevant in strict queue mode. */
     257         [ #  # ]:          0 :         if (!priv->hws_strict_queue)
     258                 :          0 :                 mlx5_age_event_prepare(priv->sh);
     259                 :          0 : }
     260                 :            : 
     261                 :            : static void
     262                 :          0 : mlx5_hws_cnt_raw_data_free(struct mlx5_dev_ctx_shared *sh,
     263                 :            :                            struct mlx5_hws_cnt_raw_data_mng *mng)
     264                 :            : {
     265         [ #  # ]:          0 :         if (mng == NULL)
     266                 :            :                 return;
     267                 :          0 :         sh->cdev->mr_scache.dereg_mr_cb(&mng->mr);
     268                 :          0 :         mlx5_free(mng->raw);
     269                 :          0 :         mlx5_free(mng);
     270                 :            : }
     271                 :            : 
     272                 :            : __rte_unused
     273                 :            : static struct mlx5_hws_cnt_raw_data_mng *
     274                 :          0 : mlx5_hws_cnt_raw_data_alloc(struct mlx5_dev_ctx_shared *sh, uint32_t n,
     275                 :            :                             struct rte_flow_error *error)
     276                 :            : {
     277                 :            :         struct mlx5_hws_cnt_raw_data_mng *mng = NULL;
     278                 :            :         int ret;
     279                 :          0 :         size_t sz = n * sizeof(struct flow_counter_stats);
     280                 :          0 :         size_t pgsz = rte_mem_page_size();
     281                 :            : 
     282                 :            :         MLX5_ASSERT(pgsz > 0);
     283                 :          0 :         mng = mlx5_malloc(MLX5_MEM_ANY | MLX5_MEM_ZERO, sizeof(*mng), 0,
     284                 :            :                         SOCKET_ID_ANY);
     285         [ #  # ]:          0 :         if (mng == NULL) {
     286                 :          0 :                 rte_flow_error_set(error, ENOMEM,
     287                 :            :                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
     288                 :            :                                    NULL, "failed to allocate counters memory manager");
     289                 :          0 :                 goto error;
     290                 :            :         }
     291                 :          0 :         mng->raw = mlx5_malloc(MLX5_MEM_ANY | MLX5_MEM_ZERO, sz, pgsz,
     292                 :            :                         SOCKET_ID_ANY);
     293         [ #  # ]:          0 :         if (mng->raw == NULL) {
     294                 :          0 :                 rte_flow_error_set(error, ENOMEM,
     295                 :            :                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
     296                 :            :                                    NULL, "failed to allocate raw counters memory");
     297                 :          0 :                 goto error;
     298                 :            :         }
     299                 :          0 :         ret = sh->cdev->mr_scache.reg_mr_cb(sh->cdev->pd, mng->raw, sz,
     300                 :            :                                             &mng->mr);
     301         [ #  # ]:          0 :         if (ret) {
     302                 :          0 :                 rte_flow_error_set(error, errno,
     303                 :            :                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
     304                 :            :                                    NULL, "failed to register counters memory region");
     305                 :          0 :                 goto error;
     306                 :            :         }
     307                 :            :         return mng;
     308                 :          0 : error:
     309                 :          0 :         mlx5_hws_cnt_raw_data_free(sh, mng);
     310                 :          0 :         return NULL;
     311                 :            : }
     312                 :            : 
     313                 :            : static uint32_t
     314                 :          0 : mlx5_hws_cnt_svc(void *opaque)
     315                 :            : {
     316                 :            :         struct mlx5_dev_ctx_shared *sh =
     317                 :            :                 (struct mlx5_dev_ctx_shared *)opaque;
     318                 :          0 :         uint64_t interval =
     319                 :          0 :                 (uint64_t)sh->cnt_svc->query_interval * (US_PER_S / MS_PER_S);
     320                 :            :         struct mlx5_hws_cnt_pool *hws_cpool;
     321                 :            :         uint64_t start_cycle, query_cycle = 0;
     322                 :            :         uint64_t query_us;
     323                 :            :         uint64_t sleep_us;
     324                 :            : 
     325         [ #  # ]:          0 :         while (sh->cnt_svc->svc_running != 0) {
     326         [ #  # ]:          0 :                 if (rte_spinlock_trylock(&sh->cpool_lock) == 0)
     327                 :          0 :                         continue;
     328                 :            :                 start_cycle = rte_rdtsc();
     329                 :            :                 /* 200ms for 16M counters. */
     330         [ #  # ]:          0 :                 LIST_FOREACH(hws_cpool, &sh->hws_cpool_list, next) {
     331                 :          0 :                         struct mlx5_priv *opriv = hws_cpool->priv;
     332                 :            : 
     333                 :          0 :                         __mlx5_hws_cnt_svc(sh, hws_cpool);
     334         [ #  # ]:          0 :                         if (opriv->hws_age_req)
     335                 :          0 :                                 mlx5_hws_aging_check(opriv, hws_cpool);
     336                 :            :                 }
     337                 :          0 :                 query_cycle = rte_rdtsc() - start_cycle;
     338                 :            :                 rte_spinlock_unlock(&sh->cpool_lock);
     339                 :          0 :                 query_us = query_cycle / (rte_get_timer_hz() / US_PER_S);
     340                 :          0 :                 sleep_us = interval - query_us;
     341         [ #  # ]:          0 :                 DRV_LOG(DEBUG, "ibdev %s counter service thread: "
     342                 :            :                                "interval_us=%" PRIu64 " query_us=%" PRIu64 " "
     343                 :            :                                "sleep_us=%" PRIu64,
     344                 :            :                         sh->ibdev_name, interval, query_us,
     345                 :            :                         interval > query_us ? sleep_us : 0);
     346         [ #  # ]:          0 :                 if (interval > query_us)
     347                 :          0 :                         rte_delay_us_sleep(sleep_us);
     348                 :            :         }
     349                 :          0 :         return 0;
     350                 :            : }
     351                 :            : 
     352                 :            : static void
     353                 :          0 : mlx5_hws_cnt_pool_deinit(struct mlx5_hws_cnt_pool * const cntp)
     354                 :            : {
     355                 :            :         uint32_t qidx = 0;
     356         [ #  # ]:          0 :         if (cntp == NULL)
     357                 :            :                 return;
     358                 :          0 :         rte_ring_free(cntp->free_list);
     359                 :          0 :         rte_ring_free(cntp->wait_reset_list);
     360                 :          0 :         rte_ring_free(cntp->reuse_list);
     361         [ #  # ]:          0 :         if (cntp->cache) {
     362         [ #  # ]:          0 :                 for (qidx = 0; qidx < cntp->cache->q_num; qidx++)
     363                 :          0 :                         rte_ring_free(cntp->cache->qcache[qidx]);
     364                 :            :         }
     365                 :          0 :         mlx5_free(cntp->cache);
     366                 :          0 :         mlx5_free(cntp->raw_mng);
     367                 :          0 :         mlx5_free(cntp->pool);
     368                 :          0 :         mlx5_free(cntp);
     369                 :            : }
     370                 :            : 
     371                 :            : static bool
     372                 :            : mlx5_hws_cnt_should_enable_cache(const struct mlx5_hws_cnt_pool_cfg *pcfg,
     373                 :            :                                  const struct mlx5_hws_cache_param *ccfg)
     374                 :            : {
     375                 :            :         /*
     376                 :            :          * Enable cache if and only if there are enough counters requested
     377                 :            :          * to populate all of the caches.
     378                 :            :          */
     379                 :          0 :         return pcfg->request_num >= ccfg->q_num * ccfg->size;
     380                 :            : }
     381                 :            : 
     382                 :            : static struct mlx5_hws_cnt_pool_caches *
     383                 :          0 : mlx5_hws_cnt_cache_init(const struct mlx5_hws_cnt_pool_cfg *pcfg,
     384                 :            :                         const struct mlx5_hws_cache_param *ccfg)
     385                 :            : {
     386                 :            :         struct mlx5_hws_cnt_pool_caches *cache;
     387                 :            :         char mz_name[RTE_MEMZONE_NAMESIZE];
     388                 :            :         uint32_t qidx;
     389                 :            : 
     390                 :            :         /* If counter pool is big enough, setup the counter pool cache. */
     391                 :          0 :         cache = mlx5_malloc(MLX5_MEM_ANY | MLX5_MEM_ZERO,
     392                 :          0 :                         sizeof(*cache) +
     393                 :            :                         sizeof(((struct mlx5_hws_cnt_pool_caches *)0)->qcache[0])
     394                 :          0 :                                 * ccfg->q_num, 0, SOCKET_ID_ANY);
     395         [ #  # ]:          0 :         if (cache == NULL)
     396                 :            :                 return NULL;
     397                 :            :         /* Store the necessary cache parameters. */
     398                 :          0 :         cache->fetch_sz = ccfg->fetch_sz;
     399                 :          0 :         cache->preload_sz = ccfg->preload_sz;
     400                 :          0 :         cache->threshold = ccfg->threshold;
     401                 :          0 :         cache->q_num = ccfg->q_num;
     402         [ #  # ]:          0 :         for (qidx = 0; qidx < ccfg->q_num; qidx++) {
     403                 :          0 :                 snprintf(mz_name, sizeof(mz_name), "%s_qc/%x", pcfg->name, qidx);
     404                 :          0 :                 cache->qcache[qidx] = rte_ring_create(mz_name, ccfg->size,
     405                 :            :                                 SOCKET_ID_ANY,
     406                 :            :                                 RING_F_SP_ENQ | RING_F_SC_DEQ |
     407                 :            :                                 RING_F_EXACT_SZ);
     408         [ #  # ]:          0 :                 if (cache->qcache[qidx] == NULL)
     409                 :          0 :                         goto error;
     410                 :            :         }
     411                 :            :         return cache;
     412                 :            : 
     413                 :            : error:
     414         [ #  # ]:          0 :         while (qidx--)
     415                 :          0 :                 rte_ring_free(cache->qcache[qidx]);
     416                 :          0 :         mlx5_free(cache);
     417                 :          0 :         return NULL;
     418                 :            : }
     419                 :            : 
     420                 :            : static struct mlx5_hws_cnt_pool *
     421                 :          0 : mlx5_hws_cnt_pool_init(struct mlx5_dev_ctx_shared *sh,
     422                 :            :                        const struct mlx5_hws_cnt_pool_cfg *pcfg,
     423                 :            :                        const struct mlx5_hws_cache_param *ccfg,
     424                 :            :                        struct rte_flow_error *error)
     425                 :            : {
     426                 :            :         char mz_name[RTE_MEMZONE_NAMESIZE];
     427                 :            :         struct mlx5_hws_cnt_pool *cntp;
     428                 :            :         uint64_t cnt_num = 0;
     429                 :            : 
     430                 :            :         MLX5_ASSERT(pcfg);
     431                 :            :         MLX5_ASSERT(ccfg);
     432                 :          0 :         cntp = mlx5_malloc(MLX5_MEM_ANY | MLX5_MEM_ZERO, sizeof(*cntp), 0,
     433                 :            :                            SOCKET_ID_ANY);
     434         [ #  # ]:          0 :         if (cntp == NULL) {
     435                 :          0 :                 rte_flow_error_set(error, ENOMEM,
     436                 :            :                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
     437                 :            :                                    "failed to allocate counter pool context");
     438                 :          0 :                 return NULL;
     439                 :            :         }
     440                 :            : 
     441                 :          0 :         cntp->cfg = *pcfg;
     442         [ #  # ]:          0 :         if (cntp->cfg.host_cpool)
     443                 :            :                 return cntp;
     444         [ #  # ]:          0 :         if (pcfg->request_num > sh->hws_max_nb_counters) {
     445                 :          0 :                 DRV_LOG(ERR, "Counter number %u "
     446                 :            :                         "is greater than the maximum supported (%u).",
     447                 :            :                         pcfg->request_num, sh->hws_max_nb_counters);
     448                 :          0 :                 rte_flow_error_set(error, EINVAL,
     449                 :            :                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
     450                 :            :                                    "requested counters number exceeds supported capacity");
     451                 :          0 :                 goto error;
     452                 :            :         }
     453                 :          0 :         cnt_num = pcfg->request_num * (100 + pcfg->alloc_factor) / 100;
     454                 :            :         if (cnt_num > UINT32_MAX) {
     455                 :            :                 DRV_LOG(ERR, "counter number %"PRIu64" is out of 32bit range",
     456                 :            :                         cnt_num);
     457                 :            :                 rte_flow_error_set(error, EINVAL,
     458                 :            :                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
     459                 :            :                                    "counters number must fit in 32 bits");
     460                 :            :                 goto error;
     461                 :            :         }
     462                 :            :         /*
     463                 :            :          * When counter request number is supported, but the factor takes it
     464                 :            :          * out of size, the factor is reduced.
     465                 :            :          */
     466                 :          0 :         cnt_num = RTE_MIN((uint32_t)cnt_num, sh->hws_max_nb_counters);
     467                 :          0 :         cntp->pool = mlx5_malloc(MLX5_MEM_ANY | MLX5_MEM_ZERO,
     468                 :            :                                  sizeof(struct mlx5_hws_cnt) * cnt_num,
     469                 :            :                                  0, SOCKET_ID_ANY);
     470         [ #  # ]:          0 :         if (cntp->pool == NULL) {
     471                 :          0 :                 rte_flow_error_set(error, ENOMEM,
     472                 :            :                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
     473                 :            :                                    "failed to allocate counter pool context");
     474                 :          0 :                 goto error;
     475                 :            :         }
     476                 :          0 :         snprintf(mz_name, sizeof(mz_name), "%s_F_RING", pcfg->name);
     477                 :          0 :         cntp->free_list = rte_ring_create_elem(mz_name, sizeof(cnt_id_t),
     478                 :            :                                 (uint32_t)cnt_num, SOCKET_ID_ANY,
     479                 :            :                                 RING_F_MP_HTS_ENQ | RING_F_MC_HTS_DEQ |
     480                 :            :                                 RING_F_EXACT_SZ);
     481         [ #  # ]:          0 :         if (cntp->free_list == NULL) {
     482                 :          0 :                 rte_flow_error_set(error, ENOMEM,
     483                 :            :                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
     484                 :            :                                    "failed to allocate free counters ring");
     485                 :          0 :                 goto error;
     486                 :            :         }
     487                 :          0 :         snprintf(mz_name, sizeof(mz_name), "%s_R_RING", pcfg->name);
     488                 :          0 :         cntp->wait_reset_list = rte_ring_create_elem(mz_name, sizeof(cnt_id_t),
     489                 :            :                         (uint32_t)cnt_num, SOCKET_ID_ANY,
     490                 :            :                         RING_F_MP_HTS_ENQ | RING_F_SC_DEQ | RING_F_EXACT_SZ);
     491         [ #  # ]:          0 :         if (cntp->wait_reset_list == NULL) {
     492                 :          0 :                 rte_flow_error_set(error, ENOMEM,
     493                 :            :                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
     494                 :            :                                    "failed to allocate counters wait reset ring");
     495                 :          0 :                 goto error;
     496                 :            :         }
     497                 :          0 :         snprintf(mz_name, sizeof(mz_name), "%s_U_RING", pcfg->name);
     498                 :          0 :         cntp->reuse_list = rte_ring_create_elem(mz_name, sizeof(cnt_id_t),
     499                 :            :                         (uint32_t)cnt_num, SOCKET_ID_ANY,
     500                 :            :                         RING_F_MP_HTS_ENQ | RING_F_MC_HTS_DEQ | RING_F_EXACT_SZ);
     501         [ #  # ]:          0 :         if (cntp->reuse_list == NULL) {
     502                 :          0 :                 rte_flow_error_set(error, ENOMEM,
     503                 :            :                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
     504                 :            :                                    "failed to allocate counters reuse ring");
     505                 :          0 :                 goto error;
     506                 :            :         }
     507                 :            :         /* Allocate counter cache only if needed. */
     508         [ #  # ]:          0 :         if (mlx5_hws_cnt_should_enable_cache(pcfg, ccfg)) {
     509                 :          0 :                 cntp->cache = mlx5_hws_cnt_cache_init(pcfg, ccfg);
     510         [ #  # ]:          0 :                 if (cntp->cache == NULL) {
     511                 :          0 :                         rte_flow_error_set(error, ENOMEM,
     512                 :            :                                            RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
     513                 :            :                                            "failed to allocate counters cache");
     514                 :          0 :                         goto error;
     515                 :            :                 }
     516                 :            :         }
     517                 :            :         /* Initialize the time for aging-out calculation. */
     518                 :          0 :         cntp->time_of_last_age_check = MLX5_CURR_TIME_SEC;
     519                 :          0 :         return cntp;
     520                 :          0 : error:
     521                 :          0 :         mlx5_hws_cnt_pool_deinit(cntp);
     522                 :          0 :         return NULL;
     523                 :            : }
     524                 :            : 
     525                 :            : int
     526                 :          0 : mlx5_hws_cnt_service_thread_create(struct mlx5_dev_ctx_shared *sh)
     527                 :            : {
     528                 :            :         char name[RTE_THREAD_INTERNAL_NAME_SIZE];
     529                 :            :         rte_thread_attr_t attr;
     530                 :            :         int ret;
     531                 :          0 :         uint32_t service_core = sh->cnt_svc->service_core;
     532                 :            : 
     533                 :          0 :         ret = rte_thread_attr_init(&attr);
     534         [ #  # ]:          0 :         if (ret != 0)
     535                 :          0 :                 goto error;
     536         [ #  # ]:          0 :         CPU_SET(service_core, &attr.cpuset);
     537                 :          0 :         sh->cnt_svc->svc_running = 1;
     538                 :          0 :         ret = rte_thread_create(&sh->cnt_svc->service_thread,
     539                 :            :                         &attr, mlx5_hws_cnt_svc, sh);
     540         [ #  # ]:          0 :         if (ret != 0)
     541                 :          0 :                 goto error;
     542                 :            :         snprintf(name, sizeof(name), "mlx5-cn%d", service_core);
     543                 :          0 :         rte_thread_set_prefixed_name(sh->cnt_svc->service_thread, name);
     544                 :            : 
     545                 :          0 :         return 0;
     546                 :          0 : error:
     547                 :          0 :         DRV_LOG(ERR, "Failed to create HW steering's counter service thread.");
     548                 :          0 :         return ret;
     549                 :            : }
     550                 :            : 
     551                 :            : void
     552                 :          0 : mlx5_hws_cnt_service_thread_destroy(struct mlx5_dev_ctx_shared *sh)
     553                 :            : {
     554         [ #  # ]:          0 :         if (sh->cnt_svc->service_thread.opaque_id == 0)
     555                 :            :                 return;
     556                 :          0 :         sh->cnt_svc->svc_running = 0;
     557                 :          0 :         rte_thread_join(sh->cnt_svc->service_thread, NULL);
     558                 :          0 :         sh->cnt_svc->service_thread.opaque_id = 0;
     559                 :            : }
     560                 :            : 
     561                 :            : static int
     562                 :          0 : mlx5_hws_cnt_pool_dcs_alloc(struct mlx5_dev_ctx_shared *sh,
     563                 :            :                             struct mlx5_hws_cnt_pool *cpool,
     564                 :            :                             struct rte_flow_error *error)
     565                 :            : {
     566                 :          0 :         struct mlx5_hca_attr *hca_attr = &sh->cdev->config.hca_attr;
     567         [ #  # ]:          0 :         uint32_t max_log_bulk_sz = sh->hws_max_log_bulk_sz;
     568                 :            :         uint32_t log_bulk_sz;
     569                 :            :         uint32_t idx, alloc_candidate, alloced = 0;
     570                 :            :         unsigned int cnt_num = mlx5_hws_cnt_pool_get_size(cpool);
     571                 :          0 :         struct mlx5_devx_counter_attr attr = {0};
     572                 :            :         struct mlx5_devx_obj *dcs;
     573                 :            : 
     574                 :            :         MLX5_ASSERT(cpool->cfg.host_cpool == NULL);
     575         [ #  # ]:          0 :         if (hca_attr->flow_counter_bulk_log_max_alloc == 0)
     576                 :          0 :                 return rte_flow_error_set(error, ENOTSUP,
     577                 :            :                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
     578                 :            :                                           NULL, "FW doesn't support bulk log max alloc");
     579         [ #  # ]:          0 :         cnt_num = RTE_ALIGN_CEIL(cnt_num, 4); /* minimal 4 counter in bulk. */
     580                 :          0 :         log_bulk_sz = RTE_MIN(max_log_bulk_sz, rte_log2_u32(cnt_num));
     581                 :          0 :         attr.pd = sh->cdev->pdn;
     582                 :          0 :         attr.pd_valid = 1;
     583                 :          0 :         attr.bulk_log_max_alloc = 1;
     584                 :          0 :         attr.flow_counter_bulk_log_size = log_bulk_sz;
     585                 :            :         idx = 0;
     586                 :          0 :         dcs = mlx5_devx_cmd_flow_counter_alloc_general(sh->cdev->ctx, &attr);
     587         [ #  # ]:          0 :         if (dcs == NULL) {
     588                 :          0 :                 rte_flow_error_set(error, rte_errno,
     589                 :            :                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
     590                 :            :                                    NULL, "FW failed to allocate counters");
     591                 :          0 :                 goto error;
     592                 :            :         }
     593                 :          0 :         cpool->dcs_mng.dcs[idx].obj = dcs;
     594                 :          0 :         cpool->dcs_mng.dcs[idx].batch_sz = (1 << log_bulk_sz);
     595                 :          0 :         cpool->dcs_mng.batch_total++;
     596                 :            :         idx++;
     597                 :          0 :         cpool->dcs_mng.dcs[0].iidx = 0;
     598                 :            :         alloced = cpool->dcs_mng.dcs[0].batch_sz;
     599         [ #  # ]:          0 :         if (cnt_num > cpool->dcs_mng.dcs[0].batch_sz) {
     600         [ #  # ]:          0 :                 while (idx < MLX5_HWS_CNT_DCS_NUM) {
     601                 :          0 :                         attr.flow_counter_bulk_log_size = --max_log_bulk_sz;
     602                 :          0 :                         alloc_candidate = RTE_BIT32(max_log_bulk_sz);
     603         [ #  # ]:          0 :                         if (alloced + alloc_candidate > sh->hws_max_nb_counters)
     604                 :          0 :                                 continue;
     605                 :          0 :                         dcs = mlx5_devx_cmd_flow_counter_alloc_general
     606                 :          0 :                                 (sh->cdev->ctx, &attr);
     607         [ #  # ]:          0 :                         if (dcs == NULL) {
     608                 :          0 :                                 rte_flow_error_set(error, rte_errno,
     609                 :            :                                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
     610                 :            :                                                    NULL, "FW failed to allocate counters");
     611                 :          0 :                                 goto error;
     612                 :            :                         }
     613                 :          0 :                         cpool->dcs_mng.dcs[idx].obj = dcs;
     614                 :          0 :                         cpool->dcs_mng.dcs[idx].batch_sz = alloc_candidate;
     615                 :          0 :                         cpool->dcs_mng.dcs[idx].iidx = alloced;
     616                 :            :                         alloced += cpool->dcs_mng.dcs[idx].batch_sz;
     617                 :          0 :                         cpool->dcs_mng.batch_total++;
     618         [ #  # ]:          0 :                         if (alloced >= cnt_num)
     619                 :            :                                 break;
     620                 :          0 :                         idx++;
     621                 :            :                 }
     622                 :            :         }
     623                 :            :         return 0;
     624                 :          0 : error:
     625                 :          0 :         DRV_LOG(DEBUG,
     626                 :            :                 "Cannot alloc device counter, allocated[%" PRIu32 "] request[%" PRIu32 "]",
     627                 :            :                 alloced, cnt_num);
     628         [ #  # ]:          0 :         for (idx = 0; idx < cpool->dcs_mng.batch_total; idx++) {
     629                 :          0 :                 mlx5_devx_cmd_destroy(cpool->dcs_mng.dcs[idx].obj);
     630                 :          0 :                 cpool->dcs_mng.dcs[idx].obj = NULL;
     631                 :          0 :                 cpool->dcs_mng.dcs[idx].batch_sz = 0;
     632                 :          0 :                 cpool->dcs_mng.dcs[idx].iidx = 0;
     633                 :            :         }
     634                 :          0 :         cpool->dcs_mng.batch_total = 0;
     635                 :          0 :         return -1;
     636                 :            : }
     637                 :            : 
     638                 :            : static void
     639                 :          0 : mlx5_hws_cnt_pool_dcs_free(struct mlx5_dev_ctx_shared *sh,
     640                 :            :                            struct mlx5_hws_cnt_pool *cpool)
     641                 :            : {
     642                 :            :         uint32_t idx;
     643                 :            : 
     644         [ #  # ]:          0 :         if (cpool == NULL)
     645                 :            :                 return;
     646         [ #  # ]:          0 :         for (idx = 0; idx < MLX5_HWS_CNT_DCS_NUM; idx++)
     647                 :          0 :                 mlx5_devx_cmd_destroy(cpool->dcs_mng.dcs[idx].obj);
     648         [ #  # ]:          0 :         if (cpool->raw_mng) {
     649                 :          0 :                 mlx5_hws_cnt_raw_data_free(sh, cpool->raw_mng);
     650                 :          0 :                 cpool->raw_mng = NULL;
     651                 :            :         }
     652                 :            : }
     653                 :            : 
     654                 :            : static void
     655                 :          0 : mlx5_hws_cnt_pool_action_destroy(struct mlx5_hws_cnt_pool *cpool)
     656                 :            : {
     657                 :            :         uint32_t idx;
     658                 :            : 
     659         [ #  # ]:          0 :         for (idx = 0; idx < cpool->dcs_mng.batch_total; idx++) {
     660                 :            :                 struct mlx5_hws_cnt_dcs *dcs = &cpool->dcs_mng.dcs[idx];
     661                 :            : 
     662         [ #  # ]:          0 :                 if (dcs->dr_action != NULL) {
     663                 :          0 :                         mlx5dr_action_destroy(dcs->dr_action);
     664                 :          0 :                         dcs->dr_action = NULL;
     665                 :            :                 }
     666                 :            :         }
     667                 :          0 : }
     668                 :            : 
     669                 :            : static int
     670         [ #  # ]:          0 : mlx5_hws_cnt_pool_action_create(struct mlx5_priv *priv,
     671                 :            :                 struct mlx5_hws_cnt_pool *cpool)
     672                 :            : {
     673                 :            :         struct mlx5_hws_cnt_pool *hpool = mlx5_hws_cnt_host_pool(cpool);
     674                 :            :         uint32_t idx;
     675                 :            :         int ret = 0;
     676                 :            :         uint32_t flags;
     677                 :            : 
     678                 :            :         flags = MLX5DR_ACTION_FLAG_HWS_RX | MLX5DR_ACTION_FLAG_HWS_TX;
     679   [ #  #  #  # ]:          0 :         if (priv->sh->config.dv_esw_en && priv->master) {
     680                 :          0 :                 flags |= (is_unified_fdb(priv) ?
     681                 :            :                                 (MLX5DR_ACTION_FLAG_HWS_FDB_RX |
     682                 :            :                                  MLX5DR_ACTION_FLAG_HWS_FDB_TX |
     683         [ #  # ]:          0 :                                  MLX5DR_ACTION_FLAG_HWS_FDB_UNIFIED) :
     684                 :            :                                 MLX5DR_ACTION_FLAG_HWS_FDB);
     685                 :            :         }
     686         [ #  # ]:          0 :         for (idx = 0; idx < hpool->dcs_mng.batch_total; idx++) {
     687                 :            :                 struct mlx5_hws_cnt_dcs *hdcs = &hpool->dcs_mng.dcs[idx];
     688                 :            :                 struct mlx5_hws_cnt_dcs *dcs = &cpool->dcs_mng.dcs[idx];
     689                 :            : 
     690                 :          0 :                 dcs->dr_action = mlx5dr_action_create_counter(priv->dr_ctx,
     691                 :          0 :                                         (struct mlx5dr_devx_obj *)hdcs->obj,
     692                 :            :                                         flags);
     693         [ #  # ]:          0 :                 if (dcs->dr_action == NULL) {
     694                 :          0 :                         mlx5_hws_cnt_pool_action_destroy(cpool);
     695                 :            :                         ret = -ENOSYS;
     696                 :          0 :                         break;
     697                 :            :                 }
     698                 :            :         }
     699                 :          0 :         return ret;
     700                 :            : }
     701                 :            : 
     702                 :            : int
     703                 :          0 : mlx5_hws_cnt_pool_create(struct rte_eth_dev *dev,
     704                 :            :                          uint32_t nb_counters, uint16_t nb_queue,
     705                 :            :                          struct mlx5_hws_cnt_pool *chost,
     706                 :            :                          struct rte_flow_error *error)
     707                 :            : {
     708                 :            :         struct mlx5_hws_cnt_pool *cpool = NULL;
     709                 :          0 :         struct mlx5_priv *priv = dev->data->dev_private;
     710                 :          0 :         struct mlx5_hws_cache_param cparam = {0};
     711                 :          0 :         struct mlx5_hws_cnt_pool_cfg pcfg = {0};
     712                 :            :         char *mp_name;
     713                 :            :         int ret = 0;
     714                 :            :         size_t sz;
     715                 :            : 
     716                 :          0 :         mp_name = mlx5_malloc(MLX5_MEM_ZERO, RTE_MEMZONE_NAMESIZE, 0, SOCKET_ID_ANY);
     717         [ #  # ]:          0 :         if (mp_name == NULL) {
     718                 :          0 :                 ret = rte_flow_error_set(error, ENOMEM, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
     719                 :            :                                          "failed to allocate counter pool name prefix");
     720                 :          0 :                 goto error;
     721                 :            :         }
     722         [ #  # ]:          0 :         snprintf(mp_name, RTE_MEMZONE_NAMESIZE, "MLX5_HWS_CNT_P_%x", dev->data->port_id);
     723                 :          0 :         pcfg.name = mp_name;
     724                 :          0 :         pcfg.request_num = nb_counters;
     725                 :          0 :         pcfg.alloc_factor = HWS_CNT_ALLOC_FACTOR_DEFAULT;
     726         [ #  # ]:          0 :         if (chost) {
     727                 :          0 :                 pcfg.host_cpool = chost;
     728                 :          0 :                 cpool = mlx5_hws_cnt_pool_init(priv->sh, &pcfg, &cparam, error);
     729         [ #  # ]:          0 :                 if (cpool == NULL) {
     730                 :          0 :                         ret = -rte_errno;
     731                 :          0 :                         goto error;
     732                 :            :                 }
     733                 :          0 :                 ret = mlx5_hws_cnt_pool_action_create(priv, cpool);
     734         [ #  # ]:          0 :                 if (ret != 0) {
     735                 :          0 :                         rte_flow_error_set(error, -ret,
     736                 :            :                                            RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
     737                 :            :                                            NULL, "failed to allocate counter actions on guest port");
     738                 :          0 :                         goto error;
     739                 :            :                 }
     740                 :          0 :                 goto success;
     741                 :            :         }
     742                 :          0 :         cparam.fetch_sz = HWS_CNT_CACHE_FETCH_DEFAULT;
     743                 :          0 :         cparam.preload_sz = HWS_CNT_CACHE_PRELOAD_DEFAULT;
     744                 :          0 :         cparam.q_num = nb_queue;
     745                 :          0 :         cparam.threshold = HWS_CNT_CACHE_THRESHOLD_DEFAULT;
     746                 :          0 :         cparam.size = HWS_CNT_CACHE_SZ_DEFAULT;
     747                 :          0 :         cpool = mlx5_hws_cnt_pool_init(priv->sh, &pcfg, &cparam, error);
     748         [ #  # ]:          0 :         if (cpool == NULL) {
     749                 :          0 :                 ret = -rte_errno;
     750                 :          0 :                 goto error;
     751                 :            :         }
     752                 :          0 :         ret = mlx5_hws_cnt_pool_dcs_alloc(priv->sh, cpool, error);
     753         [ #  # ]:          0 :         if (ret != 0)
     754                 :          0 :                 goto error;
     755                 :          0 :         sz = RTE_ALIGN_CEIL(mlx5_hws_cnt_pool_get_size(cpool), 4);
     756                 :          0 :         cpool->raw_mng = mlx5_hws_cnt_raw_data_alloc(priv->sh, sz, error);
     757         [ #  # ]:          0 :         if (cpool->raw_mng == NULL) {
     758                 :          0 :                 ret = -rte_errno;
     759                 :          0 :                 goto error;
     760                 :            :         }
     761                 :          0 :         ret = __hws_cnt_id_load(cpool);
     762         [ #  # ]:          0 :         if (ret != 0)
     763                 :          0 :                 goto error;
     764                 :            :         /*
     765                 :            :          * Bump query gen right after pool create so the
     766                 :            :          * pre-loaded counters can be used directly
     767                 :            :          * because they already have init value no need
     768                 :            :          * to wait for query.
     769                 :            :          */
     770                 :          0 :         cpool->query_gen = 1;
     771                 :          0 :         ret = mlx5_hws_cnt_pool_action_create(priv, cpool);
     772         [ #  # ]:          0 :         if (ret != 0) {
     773                 :          0 :                 rte_flow_error_set(error, -ret,
     774                 :            :                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
     775                 :            :                                    NULL, "failed to allocate counter actions");
     776                 :          0 :                 goto error;
     777                 :            :         }
     778                 :            :         /* init cnt service if not. */
     779         [ #  # ]:          0 :         if (priv->sh->cnt_svc == NULL) {
     780                 :          0 :                 ret = mlx5_hws_cnt_svc_init(priv->sh, error);
     781         [ #  # ]:          0 :                 if (ret)
     782                 :          0 :                         goto error;
     783                 :            :         }
     784                 :          0 :         priv->sh->cnt_svc->refcnt++;
     785                 :          0 :         cpool->priv = priv;
     786                 :          0 :         rte_spinlock_lock(&priv->sh->cpool_lock);
     787         [ #  # ]:          0 :         LIST_INSERT_HEAD(&priv->sh->hws_cpool_list, cpool, next);
     788                 :          0 :         rte_spinlock_unlock(&priv->sh->cpool_lock);
     789                 :          0 : success:
     790                 :          0 :         priv->hws_cpool = cpool;
     791                 :          0 :         return 0;
     792                 :          0 : error:
     793                 :            :         MLX5_ASSERT(ret);
     794                 :          0 :         mlx5_hws_cnt_pool_destroy(priv->sh, cpool);
     795                 :          0 :         priv->hws_cpool = NULL;
     796                 :          0 :         mlx5_free(mp_name);
     797                 :          0 :         return ret;
     798                 :            : }
     799                 :            : 
     800                 :            : void
     801                 :          0 : mlx5_hws_cnt_pool_destroy(struct mlx5_dev_ctx_shared *sh,
     802                 :            :                 struct mlx5_hws_cnt_pool *cpool)
     803                 :            : {
     804         [ #  # ]:          0 :         if (cpool == NULL)
     805                 :            :                 return;
     806                 :            :         /*
     807                 :            :          * 16M counter consumes 200ms to finish the query.
     808                 :            :          * Maybe blocked for at most 200ms here.
     809                 :            :          */
     810                 :          0 :         rte_spinlock_lock(&sh->cpool_lock);
     811                 :            :         /* Try to remove cpool before it was added to list caused segfault. */
     812   [ #  #  #  # ]:          0 :         if (!LIST_EMPTY(&sh->hws_cpool_list) && cpool->next.le_prev)
     813         [ #  # ]:          0 :                 LIST_REMOVE(cpool, next);
     814                 :            :         rte_spinlock_unlock(&sh->cpool_lock);
     815         [ #  # ]:          0 :         if (cpool->cfg.host_cpool == NULL) {
     816   [ #  #  #  # ]:          0 :                 if (sh->cnt_svc && --sh->cnt_svc->refcnt == 0)
     817                 :          0 :                         mlx5_hws_cnt_svc_deinit(sh);
     818                 :            :         }
     819                 :          0 :         mlx5_hws_cnt_pool_action_destroy(cpool);
     820         [ #  # ]:          0 :         if (cpool->cfg.host_cpool == NULL) {
     821                 :          0 :                 mlx5_hws_cnt_pool_dcs_free(sh, cpool);
     822                 :          0 :                 mlx5_hws_cnt_raw_data_free(sh, cpool->raw_mng);
     823                 :            :         }
     824                 :          0 :         mlx5_free((void *)cpool->cfg.name);
     825                 :          0 :         mlx5_hws_cnt_pool_deinit(cpool);
     826                 :            : }
     827                 :            : 
     828                 :            : int
     829                 :          0 : mlx5_hws_cnt_svc_init(struct mlx5_dev_ctx_shared *sh,
     830                 :            :                       struct rte_flow_error *error)
     831                 :            : {
     832                 :            :         int ret;
     833                 :            : 
     834                 :          0 :         sh->cnt_svc = mlx5_malloc(MLX5_MEM_ANY | MLX5_MEM_ZERO,
     835                 :            :                         sizeof(*sh->cnt_svc), 0, SOCKET_ID_ANY);
     836         [ #  # ]:          0 :         if (sh->cnt_svc == NULL)
     837                 :          0 :                 goto err;
     838                 :          0 :         sh->cnt_svc->query_interval = sh->config.cnt_svc.cycle_time;
     839                 :          0 :         sh->cnt_svc->service_core = sh->config.cnt_svc.service_core;
     840                 :          0 :         ret = mlx5_aso_cnt_queue_init(sh);
     841         [ #  # ]:          0 :         if (ret != 0) {
     842                 :          0 :                 mlx5_free(sh->cnt_svc);
     843                 :          0 :                 sh->cnt_svc = NULL;
     844                 :          0 :                 goto err;
     845                 :            :         }
     846                 :          0 :         ret = mlx5_hws_cnt_service_thread_create(sh);
     847         [ #  # ]:          0 :         if (ret != 0) {
     848                 :          0 :                 mlx5_aso_cnt_queue_uninit(sh);
     849                 :          0 :                 mlx5_free(sh->cnt_svc);
     850                 :          0 :                 sh->cnt_svc = NULL;
     851                 :            :         }
     852                 :            :         return 0;
     853                 :          0 : err:
     854                 :          0 :         return rte_flow_error_set(error, ENOMEM,
     855                 :            :                                   RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
     856                 :            :                                   NULL, "failed to init counters service");
     857                 :            : 
     858                 :            : }
     859                 :            : 
     860                 :            : void
     861                 :          0 : mlx5_hws_cnt_svc_deinit(struct mlx5_dev_ctx_shared *sh)
     862                 :            : {
     863         [ #  # ]:          0 :         if (sh->cnt_svc == NULL)
     864                 :            :                 return;
     865                 :          0 :         mlx5_hws_cnt_service_thread_destroy(sh);
     866                 :          0 :         mlx5_aso_cnt_queue_uninit(sh);
     867                 :          0 :         mlx5_free(sh->cnt_svc);
     868                 :          0 :         sh->cnt_svc = NULL;
     869                 :            : }
     870                 :            : 
     871                 :            : /**
     872                 :            :  * Destroy AGE action.
     873                 :            :  *
     874                 :            :  * @param priv
     875                 :            :  *   Pointer to the port private data structure.
     876                 :            :  * @param idx
     877                 :            :  *   Index of AGE parameter.
     878                 :            :  * @param error
     879                 :            :  *   Pointer to error structure.
     880                 :            :  *
     881                 :            :  * @return
     882                 :            :  *   0 on success, a negative errno value otherwise and rte_errno is set.
     883                 :            :  */
     884                 :            : int
     885                 :          0 : mlx5_hws_age_action_destroy(struct mlx5_priv *priv, uint32_t idx,
     886                 :            :                             struct rte_flow_error *error)
     887                 :            : {
     888                 :          0 :         struct mlx5_age_info *age_info = GET_PORT_AGE_INFO(priv);
     889                 :          0 :         struct mlx5_indexed_pool *ipool = age_info->ages_ipool;
     890                 :          0 :         struct mlx5_hws_age_param *param = mlx5_ipool_get(ipool, idx);
     891                 :            : 
     892         [ #  # ]:          0 :         if (param == NULL)
     893                 :          0 :                 return rte_flow_error_set(error, EINVAL,
     894                 :            :                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
     895                 :            :                                           "invalid AGE parameter index");
     896      [ #  #  # ]:          0 :         switch (rte_atomic_exchange_explicit(&param->state, HWS_AGE_FREE,
     897                 :            :                                     rte_memory_order_relaxed)) {
     898                 :          0 :         case HWS_AGE_CANDIDATE:
     899                 :            :         case HWS_AGE_AGED_OUT_REPORTED:
     900                 :          0 :                 mlx5_hws_age_param_free(priv, param->own_cnt_index, ipool, idx);
     901                 :          0 :                 break;
     902                 :            :         case HWS_AGE_AGED_OUT_NOT_REPORTED:
     903                 :            :         case HWS_AGE_CANDIDATE_INSIDE_RING:
     904                 :            :                 /*
     905                 :            :                  * In both cases AGE is inside the ring. Change the state here
     906                 :            :                  * and destroy it later when it is taken out of ring.
     907                 :            :                  */
     908                 :            :                 break;
     909                 :          0 :         case HWS_AGE_FREE:
     910                 :            :                 /*
     911                 :            :                  * If index is valid and state is FREE, it says this AGE has
     912                 :            :                  * been freed for the user but not for the PMD since it is
     913                 :            :                  * inside the ring.
     914                 :            :                  */
     915                 :          0 :                 return rte_flow_error_set(error, EINVAL,
     916                 :            :                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
     917                 :            :                                           "this AGE has already been released");
     918                 :            :         default:
     919                 :            :                 MLX5_ASSERT(0);
     920                 :            :                 break;
     921                 :            :         }
     922                 :            :         return 0;
     923                 :            : }
     924                 :            : 
     925                 :            : /**
     926                 :            :  * Create AGE action parameter.
     927                 :            :  *
     928                 :            :  * @param[in] priv
     929                 :            :  *   Pointer to the port private data structure.
     930                 :            :  * @param[in] queue_id
     931                 :            :  *   Which HWS queue to be used.
     932                 :            :  * @param[in] shared
     933                 :            :  *   Whether it indirect AGE action.
     934                 :            :  * @param[in] flow_idx
     935                 :            :  *   Flow index from indexed pool.
     936                 :            :  *   For indirect AGE action it doesn't affect.
     937                 :            :  * @param[in] age
     938                 :            :  *   Pointer to the aging action configuration.
     939                 :            :  * @param[out] error
     940                 :            :  *   Pointer to error structure.
     941                 :            :  *
     942                 :            :  * @return
     943                 :            :  *   Index to AGE action parameter on success, 0 otherwise.
     944                 :            :  */
     945                 :            : uint32_t
     946                 :          0 : mlx5_hws_age_action_create(struct mlx5_priv *priv, uint32_t queue_id,
     947                 :            :                            bool shared, const struct rte_flow_action_age *age,
     948                 :            :                            uint32_t flow_idx, struct rte_flow_error *error)
     949                 :            : {
     950                 :          0 :         struct mlx5_age_info *age_info = GET_PORT_AGE_INFO(priv);
     951                 :          0 :         struct mlx5_indexed_pool *ipool = age_info->ages_ipool;
     952                 :            :         struct mlx5_hws_age_param *param;
     953                 :            :         uint32_t age_idx;
     954                 :            : 
     955                 :          0 :         param = mlx5_ipool_malloc(ipool, &age_idx);
     956         [ #  # ]:          0 :         if (param == NULL) {
     957                 :          0 :                 rte_flow_error_set(error, ENOMEM,
     958                 :            :                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
     959                 :            :                                    "cannot allocate AGE parameter");
     960                 :          0 :                 return 0;
     961                 :            :         }
     962                 :            :         MLX5_ASSERT(rte_atomic_load_explicit(&param->state,
     963                 :            :                                     rte_memory_order_relaxed) == HWS_AGE_FREE);
     964         [ #  # ]:          0 :         if (shared) {
     965                 :          0 :                 param->nb_cnts = 0;
     966                 :          0 :                 param->accumulator_hits = 0;
     967                 :          0 :                 param->accumulator_cnt = 0;
     968                 :          0 :                 flow_idx = age_idx;
     969                 :            :         } else {
     970                 :          0 :                 param->nb_cnts = 1;
     971                 :            :         }
     972         [ #  # ]:          0 :         param->context = age->context ? age->context :
     973                 :          0 :                                         (void *)(uintptr_t)flow_idx;
     974                 :          0 :         param->timeout = age->timeout;
     975                 :          0 :         param->queue_id = queue_id;
     976                 :          0 :         param->accumulator_last_hits = 0;
     977                 :          0 :         param->own_cnt_index = 0;
     978                 :          0 :         param->sec_since_last_hit = 0;
     979                 :          0 :         param->state = HWS_AGE_CANDIDATE;
     980                 :          0 :         return age_idx;
     981                 :            : }
     982                 :            : 
     983                 :            : /**
     984                 :            :  * Update indirect AGE action parameter.
     985                 :            :  *
     986                 :            :  * @param[in] priv
     987                 :            :  *   Pointer to the port private data structure.
     988                 :            :  * @param[in] idx
     989                 :            :  *   Index of AGE parameter.
     990                 :            :  * @param[in] update
     991                 :            :  *   Update value.
     992                 :            :  * @param[out] error
     993                 :            :  *   Pointer to error structure.
     994                 :            :  *
     995                 :            :  * @return
     996                 :            :  *   0 on success, a negative errno value otherwise and rte_errno is set.
     997                 :            :  */
     998                 :            : int
     999                 :          0 : mlx5_hws_age_action_update(struct mlx5_priv *priv, uint32_t idx,
    1000                 :            :                            const void *update, struct rte_flow_error *error)
    1001                 :            : {
    1002                 :            :         const struct rte_flow_update_age *update_ade = update;
    1003                 :          0 :         struct mlx5_age_info *age_info = GET_PORT_AGE_INFO(priv);
    1004                 :          0 :         struct mlx5_indexed_pool *ipool = age_info->ages_ipool;
    1005                 :          0 :         struct mlx5_hws_age_param *param = mlx5_ipool_get(ipool, idx);
    1006                 :            :         bool sec_since_last_hit_reset = false;
    1007                 :            :         bool state_update = false;
    1008                 :            : 
    1009         [ #  # ]:          0 :         if (param == NULL)
    1010                 :          0 :                 return rte_flow_error_set(error, EINVAL,
    1011                 :            :                                           RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
    1012                 :            :                                           "invalid AGE parameter index");
    1013         [ #  # ]:          0 :         if (update_ade->timeout_valid) {
    1014                 :          0 :                 uint32_t old_timeout = rte_atomic_exchange_explicit(&param->timeout,
    1015                 :            :                                                            update_ade->timeout,
    1016                 :            :                                                            rte_memory_order_relaxed);
    1017                 :            : 
    1018         [ #  # ]:          0 :                 if (old_timeout == 0)
    1019                 :            :                         sec_since_last_hit_reset = true;
    1020         [ #  # ]:          0 :                 else if (old_timeout < update_ade->timeout ||
    1021         [ #  # ]:          0 :                          update_ade->timeout == 0)
    1022                 :            :                         /*
    1023                 :            :                          * When timeout is increased, aged-out flows might be
    1024                 :            :                          * active again and state should be updated accordingly.
    1025                 :            :                          * When new timeout is 0, we update the state for not
    1026                 :            :                          * reporting aged-out stopped.
    1027                 :            :                          */
    1028                 :            :                         state_update = true;
    1029                 :            :         }
    1030         [ #  # ]:          0 :         if (update_ade->touch) {
    1031                 :            :                 sec_since_last_hit_reset = true;
    1032                 :            :                 state_update = true;
    1033                 :            :         }
    1034         [ #  # ]:          0 :         if (sec_since_last_hit_reset)
    1035                 :          0 :                 rte_atomic_store_explicit(&param->sec_since_last_hit, 0,
    1036                 :            :                                  rte_memory_order_relaxed);
    1037         [ #  # ]:          0 :         if (state_update) {
    1038                 :            :                 uint16_t expected = HWS_AGE_AGED_OUT_NOT_REPORTED;
    1039                 :            : 
    1040                 :            :                 /*
    1041                 :            :                  * Change states of aged-out flows to active:
    1042                 :            :                  *  - AGED_OUT_NOT_REPORTED -> CANDIDATE_INSIDE_RING
    1043                 :            :                  *  - AGED_OUT_REPORTED -> CANDIDATE
    1044                 :            :                  */
    1045         [ #  # ]:          0 :                 if (!rte_atomic_compare_exchange_strong_explicit(&param->state, &expected,
    1046                 :            :                                                  HWS_AGE_CANDIDATE_INSIDE_RING,
    1047                 :            :                                                  rte_memory_order_relaxed,
    1048         [ #  # ]:          0 :                                                  rte_memory_order_relaxed) &&
    1049                 :            :                     expected == HWS_AGE_AGED_OUT_REPORTED)
    1050                 :          0 :                         rte_atomic_store_explicit(&param->state, HWS_AGE_CANDIDATE,
    1051                 :            :                                          rte_memory_order_relaxed);
    1052                 :            :         }
    1053                 :            :         return 0;
    1054                 :            : }
    1055                 :            : 
    1056                 :            : /**
    1057                 :            :  * Get the AGE context if the aged-out index is still valid.
    1058                 :            :  *
    1059                 :            :  * @param priv
    1060                 :            :  *   Pointer to the port private data structure.
    1061                 :            :  * @param idx
    1062                 :            :  *   Index of AGE parameter.
    1063                 :            :  *
    1064                 :            :  * @return
    1065                 :            :  *   AGE context if the index is still aged-out, NULL otherwise.
    1066                 :            :  */
    1067                 :            : void *
    1068                 :          0 : mlx5_hws_age_context_get(struct mlx5_priv *priv, uint32_t idx)
    1069                 :            : {
    1070                 :          0 :         struct mlx5_age_info *age_info = GET_PORT_AGE_INFO(priv);
    1071                 :          0 :         struct mlx5_indexed_pool *ipool = age_info->ages_ipool;
    1072                 :          0 :         struct mlx5_hws_age_param *param = mlx5_ipool_get(ipool, idx);
    1073                 :            :         uint16_t expected = HWS_AGE_AGED_OUT_NOT_REPORTED;
    1074                 :            : 
    1075                 :            :         MLX5_ASSERT(param != NULL);
    1076         [ #  # ]:          0 :         if (rte_atomic_compare_exchange_strong_explicit(&param->state, &expected,
    1077                 :            :                                         HWS_AGE_AGED_OUT_REPORTED,
    1078                 :            :                                         rte_memory_order_relaxed, rte_memory_order_relaxed))
    1079                 :          0 :                 return param->context;
    1080      [ #  #  # ]:          0 :         switch (expected) {
    1081                 :          0 :         case HWS_AGE_FREE:
    1082                 :            :                 /*
    1083                 :            :                  * This AGE couldn't have been destroyed since it was inside
    1084                 :            :                  * the ring. Its state has updated, and now it is actually
    1085                 :            :                  * destroyed.
    1086                 :            :                  */
    1087                 :          0 :                 mlx5_hws_age_param_free(priv, param->own_cnt_index, ipool, idx);
    1088                 :          0 :                 break;
    1089                 :          0 :         case HWS_AGE_CANDIDATE_INSIDE_RING:
    1090                 :          0 :                 rte_atomic_store_explicit(&param->state, HWS_AGE_CANDIDATE,
    1091                 :            :                                  rte_memory_order_relaxed);
    1092                 :          0 :                 break;
    1093                 :            :         case HWS_AGE_CANDIDATE:
    1094                 :            :                 /*
    1095                 :            :                  * Only BG thread pushes to ring and it never pushes this state.
    1096                 :            :                  * When AGE inside the ring becomes candidate, it has a special
    1097                 :            :                  * state called HWS_AGE_CANDIDATE_INSIDE_RING.
    1098                 :            :                  * Fall-through.
    1099                 :            :                  */
    1100                 :            :         case HWS_AGE_AGED_OUT_REPORTED:
    1101                 :            :                 /*
    1102                 :            :                  * Only this thread (doing query) may write this state, and it
    1103                 :            :                  * happens only after the query thread takes it out of the ring.
    1104                 :            :                  * Fall-through.
    1105                 :            :                  */
    1106                 :            :         case HWS_AGE_AGED_OUT_NOT_REPORTED:
    1107                 :            :                 /*
    1108                 :            :                  * In this case the compare return true and function return
    1109                 :            :                  * the context immediately.
    1110                 :            :                  * Fall-through.
    1111                 :            :                  */
    1112                 :            :         default:
    1113                 :            :                 MLX5_ASSERT(0);
    1114                 :            :                 break;
    1115                 :            :         }
    1116                 :            :         return NULL;
    1117                 :            : }
    1118                 :            : 
    1119                 :            : #ifdef RTE_ARCH_64
    1120                 :            : #define MLX5_HWS_AGED_OUT_RING_SIZE_MAX UINT32_MAX
    1121                 :            : #else
    1122                 :            : #define MLX5_HWS_AGED_OUT_RING_SIZE_MAX RTE_BIT32(8)
    1123                 :            : #endif
    1124                 :            : 
    1125                 :            : /**
    1126                 :            :  * Get the size of aged out ring list for each queue.
    1127                 :            :  *
    1128                 :            :  * The size is one percent of nb_counters divided by nb_queues.
    1129                 :            :  * The ring size must be power of 2, so it align up to power of 2.
    1130                 :            :  * In 32 bit systems, the size is limited by 256.
    1131                 :            :  *
    1132                 :            :  * This function is called when RTE_FLOW_PORT_FLAG_STRICT_QUEUE is on.
    1133                 :            :  *
    1134                 :            :  * @param nb_counters
    1135                 :            :  *   Final number of allocated counter in the pool.
    1136                 :            :  * @param nb_queues
    1137                 :            :  *   Number of HWS queues in this port.
    1138                 :            :  *
    1139                 :            :  * @return
    1140                 :            :  *   Size of aged out ring per queue.
    1141                 :            :  */
    1142                 :            : static __rte_always_inline uint32_t
    1143                 :            : mlx5_hws_aged_out_q_ring_size_get(uint32_t nb_counters, uint32_t nb_queues)
    1144                 :            : {
    1145                 :          0 :         uint32_t size = rte_align32pow2((nb_counters / 100) / nb_queues);
    1146                 :            :         uint32_t max_size = MLX5_HWS_AGED_OUT_RING_SIZE_MAX;
    1147                 :            : 
    1148                 :            :         return RTE_MIN(size, max_size);
    1149                 :            : }
    1150                 :            : 
    1151                 :            : /**
    1152                 :            :  * Get the size of the aged out ring list.
    1153                 :            :  *
    1154                 :            :  * The size is one percent of nb_counters.
    1155                 :            :  * The ring size must be power of 2, so it align up to power of 2.
    1156                 :            :  * In 32 bit systems, the size is limited by 256.
    1157                 :            :  *
    1158                 :            :  * This function is called when RTE_FLOW_PORT_FLAG_STRICT_QUEUE is off.
    1159                 :            :  *
    1160                 :            :  * @param nb_counters
    1161                 :            :  *   Final number of allocated counter in the pool.
    1162                 :            :  *
    1163                 :            :  * @return
    1164                 :            :  *   Size of the aged out ring list.
    1165                 :            :  */
    1166                 :            : static __rte_always_inline uint32_t
    1167                 :            : mlx5_hws_aged_out_ring_size_get(uint32_t nb_counters)
    1168                 :            : {
    1169                 :          0 :         uint32_t size = rte_align32pow2(nb_counters / 100);
    1170                 :            :         uint32_t max_size = MLX5_HWS_AGED_OUT_RING_SIZE_MAX;
    1171                 :            : 
    1172                 :            :         return RTE_MIN(size, max_size);
    1173                 :            : }
    1174                 :            : 
    1175                 :            : /**
    1176                 :            :  * Initialize the shared aging list information per port.
    1177                 :            :  *
    1178                 :            :  * @param dev
    1179                 :            :  *   Pointer to the rte_eth_dev structure.
    1180                 :            :  * @param nb_queues
    1181                 :            :  *   Number of HWS queues.
    1182                 :            :  * @param strict_queue
    1183                 :            :  *   Indicator whether is strict_queue mode.
    1184                 :            :  * @param ring_size
    1185                 :            :  *   Size of aged-out ring for creation.
    1186                 :            :  *
    1187                 :            :  * @return
    1188                 :            :  *   0 on success, a negative errno value otherwise and rte_errno is set.
    1189                 :            :  */
    1190                 :            : static int
    1191                 :          0 : mlx5_hws_age_info_init(struct rte_eth_dev *dev, uint16_t nb_queues,
    1192                 :            :                        bool strict_queue, uint32_t ring_size)
    1193                 :            : {
    1194                 :          0 :         struct mlx5_priv *priv = dev->data->dev_private;
    1195                 :          0 :         struct mlx5_age_info *age_info = GET_PORT_AGE_INFO(priv);
    1196                 :            :         uint32_t flags = RING_F_SP_ENQ | RING_F_SC_DEQ | RING_F_EXACT_SZ;
    1197                 :            :         char mz_name[RTE_MEMZONE_NAMESIZE];
    1198                 :            :         struct rte_ring *r = NULL;
    1199                 :            :         uint32_t qidx;
    1200                 :            : 
    1201                 :          0 :         age_info->flags = 0;
    1202         [ #  # ]:          0 :         if (strict_queue) {
    1203                 :          0 :                 size_t size = sizeof(*age_info->hw_q_age) +
    1204                 :            :                               sizeof(struct rte_ring *) * nb_queues;
    1205                 :            : 
    1206                 :          0 :                 age_info->hw_q_age = mlx5_malloc(MLX5_MEM_ANY | MLX5_MEM_ZERO,
    1207                 :            :                                                  size, 0, SOCKET_ID_ANY);
    1208         [ #  # ]:          0 :                 if (age_info->hw_q_age == NULL)
    1209                 :            :                         return -ENOMEM;
    1210         [ #  # ]:          0 :                 for (qidx = 0; qidx < nb_queues; ++qidx) {
    1211                 :          0 :                         snprintf(mz_name, sizeof(mz_name),
    1212                 :            :                                  "port_%u_queue_%u_aged_out_ring",
    1213                 :          0 :                                  dev->data->port_id, qidx);
    1214                 :          0 :                         r = rte_ring_create(mz_name, ring_size, SOCKET_ID_ANY,
    1215                 :            :                                             flags);
    1216         [ #  # ]:          0 :                         if (r == NULL) {
    1217                 :          0 :                                 DRV_LOG(ERR, "\"%s\" creation failed: %s",
    1218                 :            :                                         mz_name, rte_strerror(rte_errno));
    1219                 :          0 :                                 goto error;
    1220                 :            :                         }
    1221                 :          0 :                         age_info->hw_q_age->aged_lists[qidx] = r;
    1222                 :          0 :                         DRV_LOG(DEBUG,
    1223                 :            :                                 "\"%s\" is successfully created (size=%u).",
    1224                 :            :                                 mz_name, ring_size);
    1225                 :            :                 }
    1226                 :          0 :                 age_info->hw_q_age->nb_rings = nb_queues;
    1227                 :            :         } else {
    1228                 :          0 :                 snprintf(mz_name, sizeof(mz_name), "port_%u_aged_out_ring",
    1229                 :          0 :                          dev->data->port_id);
    1230                 :          0 :                 r = rte_ring_create(mz_name, ring_size, SOCKET_ID_ANY, flags);
    1231         [ #  # ]:          0 :                 if (r == NULL) {
    1232                 :          0 :                         DRV_LOG(ERR, "\"%s\" creation failed: %s", mz_name,
    1233                 :            :                                 rte_strerror(rte_errno));
    1234                 :          0 :                         return -rte_errno;
    1235                 :            :                 }
    1236                 :          0 :                 age_info->hw_age.aged_list = r;
    1237                 :          0 :                 DRV_LOG(DEBUG, "\"%s\" is successfully created (size=%u).",
    1238                 :            :                         mz_name, ring_size);
    1239                 :            :                 /* In non "strict_queue" mode, initialize the event. */
    1240                 :          0 :                 MLX5_AGE_SET(age_info, MLX5_AGE_TRIGGER);
    1241                 :            :         }
    1242                 :            :         return 0;
    1243                 :            : error:
    1244                 :            :         MLX5_ASSERT(strict_queue);
    1245         [ #  # ]:          0 :         while (qidx--)
    1246                 :          0 :                 rte_ring_free(age_info->hw_q_age->aged_lists[qidx]);
    1247                 :          0 :         mlx5_free(age_info->hw_q_age);
    1248                 :          0 :         return -1;
    1249                 :            : }
    1250                 :            : 
    1251                 :            : /**
    1252                 :            :  * Cleanup aged-out ring before destroying.
    1253                 :            :  *
    1254                 :            :  * @param priv
    1255                 :            :  *   Pointer to port private object.
    1256                 :            :  * @param r
    1257                 :            :  *   Pointer to aged-out ring object.
    1258                 :            :  */
    1259                 :            : static void
    1260                 :          0 : mlx5_hws_aged_out_ring_cleanup(struct mlx5_priv *priv, struct rte_ring *r)
    1261                 :            : {
    1262                 :          0 :         int ring_size = rte_ring_count(r);
    1263                 :            : 
    1264         [ #  # ]:          0 :         while (ring_size > 0) {
    1265   [ #  #  #  #  :          0 :                 uint32_t age_idx = 0;
                      # ]
    1266                 :            : 
    1267                 :            :                 if (rte_ring_dequeue_elem(r, &age_idx, sizeof(uint32_t)) < 0)
    1268                 :            :                         break;
    1269                 :            :                 /* get the AGE context if the aged-out index is still valid. */
    1270                 :          0 :                 mlx5_hws_age_context_get(priv, age_idx);
    1271                 :          0 :                 ring_size--;
    1272                 :            :         }
    1273                 :          0 :         rte_ring_free(r);
    1274                 :          0 : }
    1275                 :            : 
    1276                 :            : /**
    1277                 :            :  * Destroy the shared aging list information per port.
    1278                 :            :  *
    1279                 :            :  * @param priv
    1280                 :            :  *   Pointer to port private object.
    1281                 :            :  */
    1282                 :            : static void
    1283                 :          0 : mlx5_hws_age_info_destroy(struct mlx5_priv *priv)
    1284                 :            : {
    1285                 :          0 :         struct mlx5_age_info *age_info = GET_PORT_AGE_INFO(priv);
    1286                 :          0 :         uint16_t nb_queues = age_info->hw_q_age->nb_rings;
    1287                 :            :         struct rte_ring *r;
    1288                 :            : 
    1289         [ #  # ]:          0 :         if (priv->hws_strict_queue) {
    1290                 :            :                 uint32_t qidx;
    1291                 :            : 
    1292         [ #  # ]:          0 :                 for (qidx = 0; qidx < nb_queues; ++qidx) {
    1293                 :          0 :                         r = age_info->hw_q_age->aged_lists[qidx];
    1294                 :          0 :                         mlx5_hws_aged_out_ring_cleanup(priv, r);
    1295                 :            :                 }
    1296                 :          0 :                 mlx5_free(age_info->hw_q_age);
    1297                 :            :         } else {
    1298                 :            :                 r = age_info->hw_age.aged_list;
    1299                 :          0 :                 mlx5_hws_aged_out_ring_cleanup(priv, r);
    1300                 :            :         }
    1301                 :          0 : }
    1302                 :            : 
    1303                 :            : /**
    1304                 :            :  * Initialize the aging mechanism per port.
    1305                 :            :  *
    1306                 :            :  * @param dev
    1307                 :            :  *   Pointer to the rte_eth_dev structure.
    1308                 :            :  * @param attr
    1309                 :            :  *   Port configuration attributes.
    1310                 :            :  * @param nb_queues
    1311                 :            :  *   Number of HWS queues.
    1312                 :            :  *
    1313                 :            :  * @return
    1314                 :            :  *   0 on success, a negative errno value otherwise and rte_errno is set.
    1315                 :            :  */
    1316                 :            : int
    1317                 :          0 : mlx5_hws_age_pool_init(struct rte_eth_dev *dev,
    1318                 :            :                        uint32_t nb_aging_objects,
    1319                 :            :                        uint16_t nb_queues,
    1320                 :            :                        bool strict_queue)
    1321                 :            : {
    1322                 :          0 :         struct mlx5_priv *priv = dev->data->dev_private;
    1323                 :          0 :         struct mlx5_age_info *age_info = GET_PORT_AGE_INFO(priv);
    1324                 :          0 :         struct mlx5_indexed_pool_config cfg = {
    1325                 :            :                 .size =
    1326                 :            :                       RTE_CACHE_LINE_ROUNDUP(sizeof(struct mlx5_hws_age_param)),
    1327                 :            :                 .trunk_size = 1 << 12,
    1328                 :            :                 .per_core_cache = 1 << 13,
    1329                 :            :                 .need_lock = 1,
    1330                 :          0 :                 .release_mem_en = !!priv->sh->config.reclaim_mode,
    1331                 :            :                 .malloc = mlx5_malloc,
    1332                 :            :                 .free = mlx5_free,
    1333                 :            :                 .type = "mlx5_hws_age_pool",
    1334                 :            :         };
    1335                 :            :         uint32_t nb_alloc_cnts;
    1336                 :            :         uint32_t rsize;
    1337                 :            :         uint32_t nb_ages_updated;
    1338                 :            :         int ret;
    1339                 :            : 
    1340                 :            :         MLX5_ASSERT(priv->hws_cpool);
    1341         [ #  # ]:          0 :         nb_alloc_cnts = mlx5_hws_cnt_pool_get_size(priv->hws_cpool);
    1342         [ #  # ]:          0 :         if (strict_queue) {
    1343                 :          0 :                 rsize = mlx5_hws_aged_out_q_ring_size_get(nb_alloc_cnts,
    1344                 :            :                                                           nb_queues);
    1345                 :          0 :                 nb_ages_updated = rsize * nb_queues + nb_aging_objects;
    1346                 :            :         } else {
    1347                 :            :                 rsize = mlx5_hws_aged_out_ring_size_get(nb_alloc_cnts);
    1348                 :          0 :                 nb_ages_updated = rsize + nb_aging_objects;
    1349                 :            :         }
    1350                 :          0 :         ret = mlx5_hws_age_info_init(dev, nb_queues, strict_queue, rsize);
    1351         [ #  # ]:          0 :         if (ret < 0)
    1352                 :            :                 return ret;
    1353                 :          0 :         cfg.max_idx = rte_align32pow2(nb_ages_updated);
    1354         [ #  # ]:          0 :         if (cfg.max_idx <= cfg.trunk_size) {
    1355                 :          0 :                 cfg.per_core_cache = 0;
    1356                 :          0 :                 cfg.trunk_size = cfg.max_idx;
    1357         [ #  # ]:          0 :         } else if (cfg.max_idx <= MLX5_HW_IPOOL_SIZE_THRESHOLD) {
    1358                 :          0 :                 cfg.per_core_cache = MLX5_HW_IPOOL_CACHE_MIN;
    1359                 :            :         }
    1360                 :          0 :         age_info->ages_ipool = mlx5_ipool_create(&cfg);
    1361         [ #  # ]:          0 :         if (age_info->ages_ipool == NULL) {
    1362                 :          0 :                 mlx5_hws_age_info_destroy(priv);
    1363                 :          0 :                 rte_errno = ENOMEM;
    1364                 :          0 :                 return -rte_errno;
    1365                 :            :         }
    1366                 :          0 :         priv->hws_age_req = 1;
    1367                 :          0 :         return 0;
    1368                 :            : }
    1369                 :            : 
    1370                 :            : /**
    1371                 :            :  * Cleanup all aging resources per port.
    1372                 :            :  *
    1373                 :            :  * @param priv
    1374                 :            :  *   Pointer to port private object.
    1375                 :            :  */
    1376                 :            : void
    1377                 :          0 : mlx5_hws_age_pool_destroy(struct mlx5_priv *priv)
    1378                 :            : {
    1379                 :          0 :         struct mlx5_age_info *age_info = GET_PORT_AGE_INFO(priv);
    1380                 :            : 
    1381                 :          0 :         rte_spinlock_lock(&priv->sh->cpool_lock);
    1382                 :            :         MLX5_ASSERT(priv->hws_age_req);
    1383                 :          0 :         mlx5_hws_age_info_destroy(priv);
    1384                 :          0 :         mlx5_ipool_destroy(age_info->ages_ipool);
    1385                 :          0 :         age_info->ages_ipool = NULL;
    1386                 :          0 :         priv->hws_age_req = 0;
    1387                 :          0 :         rte_spinlock_unlock(&priv->sh->cpool_lock);
    1388                 :          0 : }
    1389                 :            : 
    1390                 :            : #endif

Generated by: LCOV version 1.14