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

Generated by: LCOV version 1.14