+static struct ice_fdir_counter *
+ice_fdir_counter_shared_search(struct ice_fdir_counter_pool_container
+ *container,
+ uint32_t id)
+{
+ struct ice_fdir_counter_pool *pool;
+ struct ice_fdir_counter *counter;
+ int i;
+
+ TAILQ_FOREACH(pool, &container->pool_list, next) {
+ for (i = 0; i < ICE_FDIR_COUNTERS_PER_BLOCK; i++) {
+ counter = &pool->counters[i];
+
+ if (counter->shared &&
+ counter->ref_cnt &&
+ counter->id == id)
+ return counter;
+ }
+ }
+
+ return NULL;
+}
+
+static struct ice_fdir_counter *
+ice_fdir_counter_alloc(struct ice_pf *pf, uint32_t shared, uint32_t id)
+{
+ struct ice_hw *hw = ICE_PF_TO_HW(pf);
+ struct ice_fdir_info *fdir_info = &pf->fdir;
+ struct ice_fdir_counter_pool_container *container =
+ &fdir_info->counter;
+ struct ice_fdir_counter_pool *pool = NULL;
+ struct ice_fdir_counter *counter_free = NULL;
+
+ if (shared) {
+ counter_free = ice_fdir_counter_shared_search(container, id);
+ if (counter_free) {
+ if (counter_free->ref_cnt + 1 == 0) {
+ rte_errno = E2BIG;
+ return NULL;
+ }
+ counter_free->ref_cnt++;
+ return counter_free;
+ }
+ }
+
+ TAILQ_FOREACH(pool, &container->pool_list, next) {
+ counter_free = TAILQ_FIRST(&pool->counter_list);
+ if (counter_free)
+ break;
+ counter_free = NULL;
+ }
+
+ if (!counter_free) {
+ PMD_DRV_LOG(ERR, "No free counter found\n");
+ return NULL;
+ }
+
+ counter_free->shared = shared;
+ counter_free->id = id;
+ counter_free->ref_cnt = 1;
+ counter_free->pool = pool;
+
+ /* reset statistic counter value */
+ ICE_WRITE_REG(hw, GLSTAT_FD_CNT0H(counter_free->hw_index), 0);
+ ICE_WRITE_REG(hw, GLSTAT_FD_CNT0L(counter_free->hw_index), 0);
+
+ TAILQ_REMOVE(&pool->counter_list, counter_free, next);
+ if (TAILQ_EMPTY(&pool->counter_list)) {
+ TAILQ_REMOVE(&container->pool_list, pool, next);
+ TAILQ_INSERT_TAIL(&container->pool_list, pool, next);
+ }
+
+ return counter_free;
+}
+
+static void
+ice_fdir_counter_free(__rte_unused struct ice_pf *pf,
+ struct ice_fdir_counter *counter)
+{
+ if (!counter)
+ return;
+
+ if (--counter->ref_cnt == 0) {
+ struct ice_fdir_counter_pool *pool = counter->pool;
+
+ TAILQ_INSERT_TAIL(&pool->counter_list, counter, next);
+ }
+}
+