1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2014-2021 Broadcom
6 #include <rte_common.h>
7 #include <rte_cycles.h>
8 #include <rte_malloc.h>
10 #include <rte_alarm.h>
13 #include "bnxt_tf_common.h"
14 #include "ulp_fc_mgr.h"
15 #include "ulp_flow_db.h"
16 #include "ulp_template_db_enum.h"
17 #include "ulp_template_struct.h"
21 ulp_fc_mgr_shadow_mem_alloc(struct hw_fc_mem_info *parms, int size)
27 parms->mem_va = rte_zmalloc("ulp_fc_info",
28 RTE_CACHE_LINE_ROUNDUP(size),
31 BNXT_TF_DBG(ERR, "Allocate failed mem_va\n");
35 rte_mem_lock_page(parms->mem_va);
37 parms->mem_pa = (void *)(uintptr_t)rte_mem_virt2phy(parms->mem_va);
38 if (parms->mem_pa == (void *)(uintptr_t)RTE_BAD_IOVA) {
39 BNXT_TF_DBG(ERR, "Allocate failed mem_pa\n");
47 ulp_fc_mgr_shadow_mem_free(struct hw_fc_mem_info *parms)
49 rte_free(parms->mem_va);
53 * Allocate and Initialize all Flow Counter Manager resources for this ulp
56 * ctxt [in] The ulp context for the Flow Counter manager.
60 ulp_fc_mgr_init(struct bnxt_ulp_context *ctxt)
62 struct bnxt_ulp_device_params *dparms;
63 uint32_t dev_id, sw_acc_cntr_tbl_sz, hw_fc_mem_info_sz;
64 struct bnxt_ulp_fc_info *ulp_fc_info;
68 BNXT_TF_DBG(DEBUG, "Invalid ULP CTXT\n");
72 if (bnxt_ulp_cntxt_dev_id_get(ctxt, &dev_id)) {
73 BNXT_TF_DBG(DEBUG, "Failed to get device id\n");
77 dparms = bnxt_ulp_device_params_get(dev_id);
79 BNXT_TF_DBG(DEBUG, "Failed to device parms\n");
83 ulp_fc_info = rte_zmalloc("ulp_fc_info", sizeof(*ulp_fc_info), 0);
87 rc = pthread_mutex_init(&ulp_fc_info->fc_lock, NULL);
89 PMD_DRV_LOG(ERR, "Failed to initialize fc mutex\n");
93 /* Add the FC info tbl to the ulp context. */
94 bnxt_ulp_cntxt_ptr2_fc_info_set(ctxt, ulp_fc_info);
96 sw_acc_cntr_tbl_sz = sizeof(struct sw_acc_counter) *
97 dparms->flow_count_db_entries;
99 for (i = 0; i < TF_DIR_MAX; i++) {
100 ulp_fc_info->sw_acc_tbl[i] = rte_zmalloc("ulp_sw_acc_cntr_tbl",
101 sw_acc_cntr_tbl_sz, 0);
102 if (!ulp_fc_info->sw_acc_tbl[i])
106 hw_fc_mem_info_sz = sizeof(uint64_t) * dparms->flow_count_db_entries;
108 for (i = 0; i < TF_DIR_MAX; i++) {
109 rc = ulp_fc_mgr_shadow_mem_alloc(&ulp_fc_info->shadow_hw_tbl[i],
118 ulp_fc_mgr_deinit(ctxt);
120 "Failed to allocate memory for fc mgr\n");
126 * Release all resources in the Flow Counter Manager for this ulp context
128 * ctxt [in] The ulp context for the Flow Counter manager
132 ulp_fc_mgr_deinit(struct bnxt_ulp_context *ctxt)
134 struct bnxt_ulp_fc_info *ulp_fc_info;
137 ulp_fc_info = bnxt_ulp_cntxt_ptr2_fc_info_get(ctxt);
142 ulp_fc_mgr_thread_cancel(ctxt);
144 pthread_mutex_destroy(&ulp_fc_info->fc_lock);
146 for (i = 0; i < TF_DIR_MAX; i++)
147 rte_free(ulp_fc_info->sw_acc_tbl[i]);
149 for (i = 0; i < TF_DIR_MAX; i++)
150 ulp_fc_mgr_shadow_mem_free(&ulp_fc_info->shadow_hw_tbl[i]);
152 rte_free(ulp_fc_info);
154 /* Safe to ignore on deinit */
155 (void)bnxt_ulp_cntxt_ptr2_fc_info_set(ctxt, NULL);
161 * Check if the alarm thread that walks through the flows is started
163 * ctxt [in] The ulp context for the flow counter manager
166 bool ulp_fc_mgr_thread_isstarted(struct bnxt_ulp_context *ctxt)
168 struct bnxt_ulp_fc_info *ulp_fc_info;
170 ulp_fc_info = bnxt_ulp_cntxt_ptr2_fc_info_get(ctxt);
172 return !!(ulp_fc_info->flags & ULP_FLAG_FC_THREAD);
176 * Setup the Flow counter timer thread that will fetch/accumulate raw counter
177 * data from the chip's internal flow counters
179 * ctxt [in] The ulp context for the flow counter manager
183 ulp_fc_mgr_thread_start(struct bnxt_ulp_context *ctxt)
185 struct bnxt_ulp_fc_info *ulp_fc_info;
187 ulp_fc_info = bnxt_ulp_cntxt_ptr2_fc_info_get(ctxt);
189 if (!(ulp_fc_info->flags & ULP_FLAG_FC_THREAD)) {
190 rte_eal_alarm_set(US_PER_S * ULP_FC_TIMER,
193 ulp_fc_info->flags |= ULP_FLAG_FC_THREAD;
200 * Cancel the alarm handler
202 * ctxt [in] The ulp context for the flow counter manager
205 void ulp_fc_mgr_thread_cancel(struct bnxt_ulp_context *ctxt)
207 struct bnxt_ulp_fc_info *ulp_fc_info;
209 ulp_fc_info = bnxt_ulp_cntxt_ptr2_fc_info_get(ctxt);
213 ulp_fc_info->flags &= ~ULP_FLAG_FC_THREAD;
214 rte_eal_alarm_cancel(ulp_fc_mgr_alarm_cb, (void *)ctxt);
218 * DMA-in the raw counter data from the HW and accumulate in the
219 * local accumulator table using the TF-Core API
221 * tfp [in] The TF-Core context
223 * fc_info [in] The ULP Flow counter info ptr
225 * dir [in] The direction of the flow
227 * num_counters [in] The number of counters
230 __rte_unused static int32_t
231 ulp_bulk_get_flow_stats(struct tf *tfp,
232 struct bnxt_ulp_fc_info *fc_info,
234 struct bnxt_ulp_device_params *dparms)
235 /* MARK AS UNUSED FOR NOW TO AVOID COMPILATION ERRORS TILL API is RESOLVED */
238 struct tf_tbl_get_bulk_parms parms = { 0 };
239 enum tf_tbl_type stype = TF_TBL_TYPE_ACT_STATS_64; /* TBD: Template? */
240 struct sw_acc_counter *sw_acc_tbl_entry = NULL;
241 uint64_t *stats = NULL;
246 parms.starting_idx = fc_info->shadow_hw_tbl[dir].start_idx;
247 parms.num_entries = dparms->flow_count_db_entries / 2; /* direction */
250 * Size of an entry needs to obtained from template
252 parms.entry_sz_in_bytes = sizeof(uint64_t);
253 stats = (uint64_t *)fc_info->shadow_hw_tbl[dir].mem_va;
254 parms.physical_mem_addr = (uint64_t)
255 ((uintptr_t)(fc_info->shadow_hw_tbl[dir].mem_pa));
259 "BULK: Memory not initialized id:0x%x dir:%d\n",
260 parms.starting_idx, dir);
264 rc = tf_tbl_bulk_get(tfp, &parms);
267 "BULK: Get failed for id:0x%x rc:%d\n",
268 parms.starting_idx, rc);
272 for (i = 0; i < parms.num_entries; i++) {
273 /* TBD - Get PKT/BYTE COUNT SHIFT/MASK from Template */
274 sw_acc_tbl_entry = &fc_info->sw_acc_tbl[dir][i];
275 if (!sw_acc_tbl_entry->valid)
277 sw_acc_tbl_entry->pkt_count += FLOW_CNTR_PKTS(stats[i],
279 sw_acc_tbl_entry->byte_count += FLOW_CNTR_BYTES(stats[i],
286 static int ulp_get_single_flow_stat(struct bnxt_ulp_context *ctxt,
288 struct bnxt_ulp_fc_info *fc_info,
291 struct bnxt_ulp_device_params *dparms)
294 struct tf_get_tbl_entry_parms parms = { 0 };
295 enum tf_tbl_type stype = TF_TBL_TYPE_ACT_STATS_64; /* TBD:Template? */
296 struct sw_acc_counter *sw_acc_tbl_entry = NULL, *t_sw;
298 uint32_t sw_cntr_indx = 0;
302 parms.idx = hw_cntr_id;
305 * Size of an entry needs to obtained from template
307 parms.data_sz_in_bytes = sizeof(uint64_t);
308 parms.data = (uint8_t *)&stats;
309 rc = tf_get_tbl_entry(tfp, &parms);
312 "Get failed for id:0x%x rc:%d\n",
317 /* TBD - Get PKT/BYTE COUNT SHIFT/MASK from Template */
318 sw_cntr_indx = hw_cntr_id - fc_info->shadow_hw_tbl[dir].start_idx;
319 sw_acc_tbl_entry = &fc_info->sw_acc_tbl[dir][sw_cntr_indx];
320 /* Some dpdk applications may accumulate the flow counters while some
321 * may not. In cases where the application is accumulating the counters
322 * the PMD need not do the accumulation itself and viceversa to report
323 * the correct flow counters.
325 if (ctxt->cfg_data->accum_stats) {
326 sw_acc_tbl_entry->pkt_count += FLOW_CNTR_PKTS(stats, dparms);
327 sw_acc_tbl_entry->byte_count += FLOW_CNTR_BYTES(stats, dparms);
329 sw_acc_tbl_entry->pkt_count = FLOW_CNTR_PKTS(stats, dparms);
330 sw_acc_tbl_entry->byte_count = FLOW_CNTR_BYTES(stats, dparms);
333 /* Update the parent counters if it is child flow */
334 if (sw_acc_tbl_entry->parent_flow_id) {
335 /* Update the parent counters */
336 t_sw = sw_acc_tbl_entry;
337 if (ulp_flow_db_parent_flow_count_update(ctxt,
338 t_sw->parent_flow_id,
341 PMD_DRV_LOG(ERR, "Error updating parent counters\n");
349 * Alarm handler that will issue the TF-Core API to fetch
350 * data from the chip's internal flow counters
352 * ctxt [in] The ulp context for the flow counter manager
357 ulp_fc_mgr_alarm_cb(void *arg)
362 struct bnxt_ulp_context *ctxt = arg;
363 struct bnxt_ulp_fc_info *ulp_fc_info;
364 struct bnxt_ulp_device_params *dparms;
366 uint32_t dev_id, hw_cntr_id = 0, num_entries = 0;
368 ulp_fc_info = bnxt_ulp_cntxt_ptr2_fc_info_get(ctxt);
372 if (bnxt_ulp_cntxt_dev_id_get(ctxt, &dev_id)) {
373 BNXT_TF_DBG(DEBUG, "Failed to get device id\n");
377 dparms = bnxt_ulp_device_params_get(dev_id);
379 BNXT_TF_DBG(DEBUG, "Failed to device parms\n");
383 tfp = bnxt_ulp_cntxt_tfp_get(ctxt);
385 BNXT_TF_DBG(ERR, "Failed to get the truflow pointer\n");
390 * Take the fc_lock to ensure no flow is destroyed
391 * during the bulk get
393 if (pthread_mutex_trylock(&ulp_fc_info->fc_lock))
396 if (!ulp_fc_info->num_entries) {
397 pthread_mutex_unlock(&ulp_fc_info->fc_lock);
398 ulp_fc_mgr_thread_cancel(ctxt);
402 * Commented for now till GET_BULK is resolved, just get the first flow
404 for (i = 0; i < TF_DIR_MAX; i++) {
405 rc = ulp_bulk_get_flow_stats(tfp, ulp_fc_info, i,
406 dparms->flow_count_db_entries);
412 /* reset the parent accumulation counters before accumulation if any */
413 ulp_flow_db_parent_flow_count_reset(ctxt);
415 num_entries = dparms->flow_count_db_entries / 2;
416 for (i = 0; i < TF_DIR_MAX; i++) {
417 for (j = 0; j < num_entries; j++) {
418 if (!ulp_fc_info->sw_acc_tbl[i][j].valid)
420 hw_cntr_id = ulp_fc_info->sw_acc_tbl[i][j].hw_cntr_id;
421 rc = ulp_get_single_flow_stat(ctxt, tfp, ulp_fc_info, i,
428 pthread_mutex_unlock(&ulp_fc_info->fc_lock);
431 * If cmd fails once, no need of
432 * invoking again every second
436 ulp_fc_mgr_thread_cancel(ctxt);
440 rte_eal_alarm_set(US_PER_S * ULP_FC_TIMER,
446 * Set the starting index that indicates the first HW flow
449 * ctxt [in] The ulp context for the flow counter manager
451 * dir [in] The direction of the flow
453 * start_idx [in] The HW flow counter ID
456 bool ulp_fc_mgr_start_idx_isset(struct bnxt_ulp_context *ctxt, enum tf_dir dir)
458 struct bnxt_ulp_fc_info *ulp_fc_info;
460 ulp_fc_info = bnxt_ulp_cntxt_ptr2_fc_info_get(ctxt);
462 return ulp_fc_info->shadow_hw_tbl[dir].start_idx_is_set;
466 * Set the starting index that indicates the first HW flow
469 * ctxt [in] The ulp context for the flow counter manager
471 * dir [in] The direction of the flow
473 * start_idx [in] The HW flow counter ID
476 int32_t ulp_fc_mgr_start_idx_set(struct bnxt_ulp_context *ctxt, enum tf_dir dir,
479 struct bnxt_ulp_fc_info *ulp_fc_info;
481 ulp_fc_info = bnxt_ulp_cntxt_ptr2_fc_info_get(ctxt);
486 if (!ulp_fc_info->shadow_hw_tbl[dir].start_idx_is_set) {
487 ulp_fc_info->shadow_hw_tbl[dir].start_idx = start_idx;
488 ulp_fc_info->shadow_hw_tbl[dir].start_idx_is_set = true;
495 * Set the corresponding SW accumulator table entry based on
496 * the difference between this counter ID and the starting
497 * counter ID. Also, keep track of num of active counter enabled
500 * ctxt [in] The ulp context for the flow counter manager
502 * dir [in] The direction of the flow
504 * hw_cntr_id [in] The HW flow counter ID
507 int32_t ulp_fc_mgr_cntr_set(struct bnxt_ulp_context *ctxt, enum tf_dir dir,
510 struct bnxt_ulp_fc_info *ulp_fc_info;
511 uint32_t sw_cntr_idx;
513 ulp_fc_info = bnxt_ulp_cntxt_ptr2_fc_info_get(ctxt);
517 pthread_mutex_lock(&ulp_fc_info->fc_lock);
518 sw_cntr_idx = hw_cntr_id - ulp_fc_info->shadow_hw_tbl[dir].start_idx;
519 ulp_fc_info->sw_acc_tbl[dir][sw_cntr_idx].valid = true;
520 ulp_fc_info->sw_acc_tbl[dir][sw_cntr_idx].hw_cntr_id = hw_cntr_id;
521 ulp_fc_info->num_entries++;
522 pthread_mutex_unlock(&ulp_fc_info->fc_lock);
528 * Reset the corresponding SW accumulator table entry based on
529 * the difference between this counter ID and the starting
532 * ctxt [in] The ulp context for the flow counter manager
534 * dir [in] The direction of the flow
536 * hw_cntr_id [in] The HW flow counter ID
539 int32_t ulp_fc_mgr_cntr_reset(struct bnxt_ulp_context *ctxt, enum tf_dir dir,
542 struct bnxt_ulp_fc_info *ulp_fc_info;
543 uint32_t sw_cntr_idx;
545 ulp_fc_info = bnxt_ulp_cntxt_ptr2_fc_info_get(ctxt);
549 pthread_mutex_lock(&ulp_fc_info->fc_lock);
550 sw_cntr_idx = hw_cntr_id - ulp_fc_info->shadow_hw_tbl[dir].start_idx;
551 ulp_fc_info->sw_acc_tbl[dir][sw_cntr_idx].valid = false;
552 ulp_fc_info->sw_acc_tbl[dir][sw_cntr_idx].hw_cntr_id = 0;
553 ulp_fc_info->sw_acc_tbl[dir][sw_cntr_idx].pkt_count = 0;
554 ulp_fc_info->sw_acc_tbl[dir][sw_cntr_idx].byte_count = 0;
555 ulp_fc_info->num_entries--;
556 pthread_mutex_unlock(&ulp_fc_info->fc_lock);
562 * Fill the rte_flow_query_count 'data' argument passed
563 * in the rte_flow_query() with the values obtained and
564 * accumulated locally.
566 * ctxt [in] The ulp context for the flow counter manager
568 * flow_id [in] The HW flow ID
570 * count [out] The rte_flow_query_count 'data' that is set
573 int ulp_fc_mgr_query_count_get(struct bnxt_ulp_context *ctxt,
575 struct rte_flow_query_count *count)
578 uint32_t nxt_resource_index = 0;
579 struct bnxt_ulp_fc_info *ulp_fc_info;
580 struct ulp_flow_db_res_params params;
582 uint32_t hw_cntr_id = 0, sw_cntr_idx = 0;
583 struct sw_acc_counter *sw_acc_tbl_entry;
584 bool found_cntr_resource = false;
586 ulp_fc_info = bnxt_ulp_cntxt_ptr2_fc_info_get(ctxt);
590 if (bnxt_ulp_cntxt_acquire_fdb_lock(ctxt))
594 rc = ulp_flow_db_resource_get(ctxt,
595 BNXT_ULP_FDB_TYPE_REGULAR,
599 if (params.resource_func ==
600 BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE &&
601 (params.resource_sub_type ==
602 BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TABLE_INT_COUNT ||
603 params.resource_sub_type ==
604 BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TABLE_EXT_COUNT ||
605 params.resource_sub_type ==
606 BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TABLE_INT_COUNT_ACC)) {
607 found_cntr_resource = true;
610 } while (!rc && nxt_resource_index);
612 bnxt_ulp_cntxt_release_fdb_lock(ctxt);
614 if (rc || !found_cntr_resource)
617 dir = params.direction;
618 hw_cntr_id = params.resource_hndl;
619 if (params.resource_sub_type ==
620 BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TABLE_INT_COUNT) {
622 * Think about optimizing with try_lock later
624 pthread_mutex_lock(&ulp_fc_info->fc_lock);
625 sw_cntr_idx = hw_cntr_id -
626 ulp_fc_info->shadow_hw_tbl[dir].start_idx;
627 sw_acc_tbl_entry = &ulp_fc_info->sw_acc_tbl[dir][sw_cntr_idx];
628 if (sw_acc_tbl_entry->pkt_count) {
630 count->bytes_set = 1;
631 count->hits = sw_acc_tbl_entry->pkt_count;
632 count->bytes = sw_acc_tbl_entry->byte_count;
635 sw_acc_tbl_entry->pkt_count = 0;
636 sw_acc_tbl_entry->byte_count = 0;
638 pthread_mutex_unlock(&ulp_fc_info->fc_lock);
639 } else if (params.resource_sub_type ==
640 BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TABLE_INT_COUNT_ACC) {
641 /* Get stats from the parent child table */
642 ulp_flow_db_parent_flow_count_get(ctxt, flow_id,
643 &count->hits, &count->bytes,
646 count->bytes_set = 1;
648 /* TBD: Handle External counters */
656 * Set the parent flow if it is SW accumulation counter entry.
658 * ctxt [in] The ulp context for the flow counter manager
660 * dir [in] The direction of the flow
662 * hw_cntr_id [in] The HW flow counter ID
664 * fid [in] parent flow id
667 int32_t ulp_fc_mgr_cntr_parent_flow_set(struct bnxt_ulp_context *ctxt,
672 struct bnxt_ulp_fc_info *ulp_fc_info;
673 uint32_t sw_cntr_idx;
676 ulp_fc_info = bnxt_ulp_cntxt_ptr2_fc_info_get(ctxt);
680 pthread_mutex_lock(&ulp_fc_info->fc_lock);
681 sw_cntr_idx = hw_cntr_id - ulp_fc_info->shadow_hw_tbl[dir].start_idx;
682 if (ulp_fc_info->sw_acc_tbl[dir][sw_cntr_idx].valid) {
683 ulp_fc_info->sw_acc_tbl[dir][sw_cntr_idx].parent_flow_id = fid;
685 BNXT_TF_DBG(ERR, "Failed to set parent flow id %x:%x\n",
689 pthread_mutex_unlock(&ulp_fc_info->fc_lock);