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 if (!dparms->flow_count_db_entries) {
84 BNXT_TF_DBG(DEBUG, "flow counter support is not enabled\n");
85 bnxt_ulp_cntxt_ptr2_fc_info_set(ctxt, NULL);
89 ulp_fc_info = rte_zmalloc("ulp_fc_info", sizeof(*ulp_fc_info), 0);
93 rc = pthread_mutex_init(&ulp_fc_info->fc_lock, NULL);
95 PMD_DRV_LOG(ERR, "Failed to initialize fc mutex\n");
99 /* Add the FC info tbl to the ulp context. */
100 bnxt_ulp_cntxt_ptr2_fc_info_set(ctxt, ulp_fc_info);
102 sw_acc_cntr_tbl_sz = sizeof(struct sw_acc_counter) *
103 dparms->flow_count_db_entries;
105 for (i = 0; i < TF_DIR_MAX; i++) {
106 ulp_fc_info->sw_acc_tbl[i] = rte_zmalloc("ulp_sw_acc_cntr_tbl",
107 sw_acc_cntr_tbl_sz, 0);
108 if (!ulp_fc_info->sw_acc_tbl[i])
112 hw_fc_mem_info_sz = sizeof(uint64_t) * dparms->flow_count_db_entries;
114 for (i = 0; i < TF_DIR_MAX; i++) {
115 rc = ulp_fc_mgr_shadow_mem_alloc(&ulp_fc_info->shadow_hw_tbl[i],
124 ulp_fc_mgr_deinit(ctxt);
126 "Failed to allocate memory for fc mgr\n");
132 * Release all resources in the Flow Counter Manager for this ulp context
134 * ctxt [in] The ulp context for the Flow Counter manager
138 ulp_fc_mgr_deinit(struct bnxt_ulp_context *ctxt)
140 struct bnxt_ulp_fc_info *ulp_fc_info;
143 ulp_fc_info = bnxt_ulp_cntxt_ptr2_fc_info_get(ctxt);
148 ulp_fc_mgr_thread_cancel(ctxt);
150 pthread_mutex_destroy(&ulp_fc_info->fc_lock);
152 for (i = 0; i < TF_DIR_MAX; i++)
153 rte_free(ulp_fc_info->sw_acc_tbl[i]);
155 for (i = 0; i < TF_DIR_MAX; i++)
156 ulp_fc_mgr_shadow_mem_free(&ulp_fc_info->shadow_hw_tbl[i]);
158 rte_free(ulp_fc_info);
160 /* Safe to ignore on deinit */
161 (void)bnxt_ulp_cntxt_ptr2_fc_info_set(ctxt, NULL);
167 * Check if the alarm thread that walks through the flows is started
169 * ctxt [in] The ulp context for the flow counter manager
172 bool ulp_fc_mgr_thread_isstarted(struct bnxt_ulp_context *ctxt)
174 struct bnxt_ulp_fc_info *ulp_fc_info;
176 ulp_fc_info = bnxt_ulp_cntxt_ptr2_fc_info_get(ctxt);
179 return !!(ulp_fc_info->flags & ULP_FLAG_FC_THREAD);
185 * Setup the Flow counter timer thread that will fetch/accumulate raw counter
186 * data from the chip's internal flow counters
188 * ctxt [in] The ulp context for the flow counter manager
192 ulp_fc_mgr_thread_start(struct bnxt_ulp_context *ctxt)
194 struct bnxt_ulp_fc_info *ulp_fc_info;
196 ulp_fc_info = bnxt_ulp_cntxt_ptr2_fc_info_get(ctxt);
198 if (ulp_fc_info && !(ulp_fc_info->flags & ULP_FLAG_FC_THREAD)) {
199 rte_eal_alarm_set(US_PER_S * ULP_FC_TIMER,
200 ulp_fc_mgr_alarm_cb, NULL);
201 ulp_fc_info->flags |= ULP_FLAG_FC_THREAD;
208 * Cancel the alarm handler
210 * ctxt [in] The ulp context for the flow counter manager
213 void ulp_fc_mgr_thread_cancel(struct bnxt_ulp_context *ctxt)
215 struct bnxt_ulp_fc_info *ulp_fc_info;
217 ulp_fc_info = bnxt_ulp_cntxt_ptr2_fc_info_get(ctxt);
221 ulp_fc_info->flags &= ~ULP_FLAG_FC_THREAD;
222 rte_eal_alarm_cancel(ulp_fc_mgr_alarm_cb, NULL);
226 * DMA-in the raw counter data from the HW and accumulate in the
227 * local accumulator table using the TF-Core API
229 * tfp [in] The TF-Core context
231 * fc_info [in] The ULP Flow counter info ptr
233 * dir [in] The direction of the flow
235 * num_counters [in] The number of counters
238 __rte_unused static int32_t
239 ulp_bulk_get_flow_stats(struct tf *tfp,
240 struct bnxt_ulp_fc_info *fc_info,
242 struct bnxt_ulp_device_params *dparms)
243 /* MARK AS UNUSED FOR NOW TO AVOID COMPILATION ERRORS TILL API is RESOLVED */
246 struct tf_tbl_get_bulk_parms parms = { 0 };
247 enum tf_tbl_type stype = TF_TBL_TYPE_ACT_STATS_64; /* TBD: Template? */
248 struct sw_acc_counter *sw_acc_tbl_entry = NULL;
249 uint64_t *stats = NULL;
254 parms.starting_idx = fc_info->shadow_hw_tbl[dir].start_idx;
255 parms.num_entries = dparms->flow_count_db_entries / 2; /* direction */
258 * Size of an entry needs to obtained from template
260 parms.entry_sz_in_bytes = sizeof(uint64_t);
261 stats = (uint64_t *)fc_info->shadow_hw_tbl[dir].mem_va;
262 parms.physical_mem_addr = (uint64_t)
263 ((uintptr_t)(fc_info->shadow_hw_tbl[dir].mem_pa));
267 "BULK: Memory not initialized id:0x%x dir:%d\n",
268 parms.starting_idx, dir);
272 rc = tf_tbl_bulk_get(tfp, &parms);
275 "BULK: Get failed for id:0x%x rc:%d\n",
276 parms.starting_idx, rc);
280 for (i = 0; i < parms.num_entries; i++) {
281 /* TBD - Get PKT/BYTE COUNT SHIFT/MASK from Template */
282 sw_acc_tbl_entry = &fc_info->sw_acc_tbl[dir][i];
283 if (!sw_acc_tbl_entry->valid)
285 sw_acc_tbl_entry->pkt_count += FLOW_CNTR_PKTS(stats[i],
287 sw_acc_tbl_entry->byte_count += FLOW_CNTR_BYTES(stats[i],
294 static int ulp_get_single_flow_stat(struct bnxt_ulp_context *ctxt,
296 struct bnxt_ulp_fc_info *fc_info,
299 struct bnxt_ulp_device_params *dparms)
302 struct tf_get_tbl_entry_parms parms = { 0 };
303 enum tf_tbl_type stype = TF_TBL_TYPE_ACT_STATS_64; /* TBD:Template? */
304 struct sw_acc_counter *sw_acc_tbl_entry = NULL, *t_sw;
306 uint32_t sw_cntr_indx = 0;
310 parms.idx = hw_cntr_id;
313 * Size of an entry needs to obtained from template
315 parms.data_sz_in_bytes = sizeof(uint64_t);
316 parms.data = (uint8_t *)&stats;
317 rc = tf_get_tbl_entry(tfp, &parms);
320 "Get failed for id:0x%x rc:%d\n",
325 /* TBD - Get PKT/BYTE COUNT SHIFT/MASK from Template */
326 sw_cntr_indx = hw_cntr_id - fc_info->shadow_hw_tbl[dir].start_idx;
327 sw_acc_tbl_entry = &fc_info->sw_acc_tbl[dir][sw_cntr_indx];
328 /* Some dpdk applications may accumulate the flow counters while some
329 * may not. In cases where the application is accumulating the counters
330 * the PMD need not do the accumulation itself and viceversa to report
331 * the correct flow counters.
333 if (ctxt->cfg_data->accum_stats) {
334 sw_acc_tbl_entry->pkt_count += FLOW_CNTR_PKTS(stats, dparms);
335 sw_acc_tbl_entry->byte_count += FLOW_CNTR_BYTES(stats, dparms);
337 sw_acc_tbl_entry->pkt_count = FLOW_CNTR_PKTS(stats, dparms);
338 sw_acc_tbl_entry->byte_count = FLOW_CNTR_BYTES(stats, dparms);
341 /* Update the parent counters if it is child flow */
342 if (sw_acc_tbl_entry->parent_flow_id) {
343 /* Update the parent counters */
344 t_sw = sw_acc_tbl_entry;
345 if (ulp_flow_db_parent_flow_count_update(ctxt,
346 t_sw->parent_flow_id,
349 PMD_DRV_LOG(ERR, "Error updating parent counters\n");
357 * Alarm handler that will issue the TF-Core API to fetch
358 * data from the chip's internal flow counters
360 * ctxt [in] The ulp context for the flow counter manager
365 ulp_fc_mgr_alarm_cb(void *arg __rte_unused)
370 struct bnxt_ulp_context *ctxt;
371 struct bnxt_ulp_fc_info *ulp_fc_info;
372 struct bnxt_ulp_device_params *dparms;
374 uint32_t dev_id, hw_cntr_id = 0, num_entries = 0;
376 ctxt = bnxt_ulp_cntxt_entry_acquire();
378 BNXT_TF_DBG(INFO, "could not get the ulp context lock\n");
379 rte_eal_alarm_set(US_PER_S * ULP_FC_TIMER,
380 ulp_fc_mgr_alarm_cb, NULL);
384 ulp_fc_info = bnxt_ulp_cntxt_ptr2_fc_info_get(ctxt);
386 bnxt_ulp_cntxt_entry_release();
390 if (bnxt_ulp_cntxt_dev_id_get(ctxt, &dev_id)) {
391 BNXT_TF_DBG(DEBUG, "Failed to get device id\n");
392 bnxt_ulp_cntxt_entry_release();
396 dparms = bnxt_ulp_device_params_get(dev_id);
398 BNXT_TF_DBG(DEBUG, "Failed to device parms\n");
399 bnxt_ulp_cntxt_entry_release();
403 tfp = bnxt_ulp_cntxt_tfp_get(ctxt, BNXT_ULP_SHARED_SESSION_NO);
405 BNXT_TF_DBG(ERR, "Failed to get the truflow pointer\n");
406 bnxt_ulp_cntxt_entry_release();
411 * Take the fc_lock to ensure no flow is destroyed
412 * during the bulk get
414 if (pthread_mutex_trylock(&ulp_fc_info->fc_lock))
417 if (!ulp_fc_info->num_entries) {
418 pthread_mutex_unlock(&ulp_fc_info->fc_lock);
419 ulp_fc_mgr_thread_cancel(ctxt);
420 bnxt_ulp_cntxt_entry_release();
424 * Commented for now till GET_BULK is resolved, just get the first flow
426 for (i = 0; i < TF_DIR_MAX; i++) {
427 rc = ulp_bulk_get_flow_stats(tfp, ulp_fc_info, i,
428 dparms->flow_count_db_entries);
434 /* reset the parent accumulation counters before accumulation if any */
435 ulp_flow_db_parent_flow_count_reset(ctxt);
437 num_entries = dparms->flow_count_db_entries / 2;
438 for (i = 0; i < TF_DIR_MAX; i++) {
439 for (j = 0; j < num_entries; j++) {
440 if (!ulp_fc_info->sw_acc_tbl[i][j].valid)
442 hw_cntr_id = ulp_fc_info->sw_acc_tbl[i][j].hw_cntr_id;
443 rc = ulp_get_single_flow_stat(ctxt, tfp, ulp_fc_info, i,
450 pthread_mutex_unlock(&ulp_fc_info->fc_lock);
453 * If cmd fails once, no need of
454 * invoking again every second
458 ulp_fc_mgr_thread_cancel(ctxt);
459 bnxt_ulp_cntxt_entry_release();
463 bnxt_ulp_cntxt_entry_release();
464 rte_eal_alarm_set(US_PER_S * ULP_FC_TIMER,
465 ulp_fc_mgr_alarm_cb, NULL);
469 * Set the starting index that indicates the first HW flow
472 * ctxt [in] The ulp context for the flow counter manager
474 * dir [in] The direction of the flow
476 * start_idx [in] The HW flow counter ID
479 bool ulp_fc_mgr_start_idx_isset(struct bnxt_ulp_context *ctxt, enum tf_dir dir)
481 struct bnxt_ulp_fc_info *ulp_fc_info;
483 ulp_fc_info = bnxt_ulp_cntxt_ptr2_fc_info_get(ctxt);
486 return ulp_fc_info->shadow_hw_tbl[dir].start_idx_is_set;
492 * Set the starting index that indicates the first HW flow
495 * ctxt [in] The ulp context for the flow counter manager
497 * dir [in] The direction of the flow
499 * start_idx [in] The HW flow counter ID
502 int32_t ulp_fc_mgr_start_idx_set(struct bnxt_ulp_context *ctxt, enum tf_dir dir,
505 struct bnxt_ulp_fc_info *ulp_fc_info;
507 ulp_fc_info = bnxt_ulp_cntxt_ptr2_fc_info_get(ctxt);
512 if (!ulp_fc_info->shadow_hw_tbl[dir].start_idx_is_set) {
513 ulp_fc_info->shadow_hw_tbl[dir].start_idx = start_idx;
514 ulp_fc_info->shadow_hw_tbl[dir].start_idx_is_set = true;
521 * Set the corresponding SW accumulator table entry based on
522 * the difference between this counter ID and the starting
523 * counter ID. Also, keep track of num of active counter enabled
526 * ctxt [in] The ulp context for the flow counter manager
528 * dir [in] The direction of the flow
530 * hw_cntr_id [in] The HW flow counter ID
533 int32_t ulp_fc_mgr_cntr_set(struct bnxt_ulp_context *ctxt, enum tf_dir dir,
536 struct bnxt_ulp_fc_info *ulp_fc_info;
537 uint32_t sw_cntr_idx;
539 ulp_fc_info = bnxt_ulp_cntxt_ptr2_fc_info_get(ctxt);
543 pthread_mutex_lock(&ulp_fc_info->fc_lock);
544 sw_cntr_idx = hw_cntr_id - ulp_fc_info->shadow_hw_tbl[dir].start_idx;
545 ulp_fc_info->sw_acc_tbl[dir][sw_cntr_idx].valid = true;
546 ulp_fc_info->sw_acc_tbl[dir][sw_cntr_idx].hw_cntr_id = hw_cntr_id;
547 ulp_fc_info->num_entries++;
548 pthread_mutex_unlock(&ulp_fc_info->fc_lock);
554 * Reset the corresponding SW accumulator table entry based on
555 * the difference between this counter ID and the starting
558 * ctxt [in] The ulp context for the flow counter manager
560 * dir [in] The direction of the flow
562 * hw_cntr_id [in] The HW flow counter ID
565 int32_t ulp_fc_mgr_cntr_reset(struct bnxt_ulp_context *ctxt, enum tf_dir dir,
568 struct bnxt_ulp_fc_info *ulp_fc_info;
569 uint32_t sw_cntr_idx;
571 ulp_fc_info = bnxt_ulp_cntxt_ptr2_fc_info_get(ctxt);
575 pthread_mutex_lock(&ulp_fc_info->fc_lock);
576 sw_cntr_idx = hw_cntr_id - ulp_fc_info->shadow_hw_tbl[dir].start_idx;
577 ulp_fc_info->sw_acc_tbl[dir][sw_cntr_idx].valid = false;
578 ulp_fc_info->sw_acc_tbl[dir][sw_cntr_idx].hw_cntr_id = 0;
579 ulp_fc_info->sw_acc_tbl[dir][sw_cntr_idx].pkt_count = 0;
580 ulp_fc_info->sw_acc_tbl[dir][sw_cntr_idx].byte_count = 0;
581 ulp_fc_info->num_entries--;
582 pthread_mutex_unlock(&ulp_fc_info->fc_lock);
588 * Fill the rte_flow_query_count 'data' argument passed
589 * in the rte_flow_query() with the values obtained and
590 * accumulated locally.
592 * ctxt [in] The ulp context for the flow counter manager
594 * flow_id [in] The HW flow ID
596 * count [out] The rte_flow_query_count 'data' that is set
599 int ulp_fc_mgr_query_count_get(struct bnxt_ulp_context *ctxt,
601 struct rte_flow_query_count *count)
604 uint32_t nxt_resource_index = 0;
605 struct bnxt_ulp_fc_info *ulp_fc_info;
606 struct ulp_flow_db_res_params params;
608 uint32_t hw_cntr_id = 0, sw_cntr_idx = 0;
609 struct sw_acc_counter *sw_acc_tbl_entry;
610 bool found_cntr_resource = false;
612 ulp_fc_info = bnxt_ulp_cntxt_ptr2_fc_info_get(ctxt);
616 if (bnxt_ulp_cntxt_acquire_fdb_lock(ctxt))
620 rc = ulp_flow_db_resource_get(ctxt,
621 BNXT_ULP_FDB_TYPE_REGULAR,
625 if (params.resource_func ==
626 BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE &&
627 (params.resource_sub_type ==
628 BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TABLE_INT_COUNT ||
629 params.resource_sub_type ==
630 BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TABLE_EXT_COUNT ||
631 params.resource_sub_type ==
632 BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TABLE_INT_COUNT_ACC)) {
633 found_cntr_resource = true;
636 } while (!rc && nxt_resource_index);
638 bnxt_ulp_cntxt_release_fdb_lock(ctxt);
640 if (rc || !found_cntr_resource)
643 dir = params.direction;
644 hw_cntr_id = params.resource_hndl;
645 if (params.resource_sub_type ==
646 BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TABLE_INT_COUNT) {
648 * Think about optimizing with try_lock later
650 pthread_mutex_lock(&ulp_fc_info->fc_lock);
651 sw_cntr_idx = hw_cntr_id -
652 ulp_fc_info->shadow_hw_tbl[dir].start_idx;
653 sw_acc_tbl_entry = &ulp_fc_info->sw_acc_tbl[dir][sw_cntr_idx];
654 if (sw_acc_tbl_entry->pkt_count) {
656 count->bytes_set = 1;
657 count->hits = sw_acc_tbl_entry->pkt_count;
658 count->bytes = sw_acc_tbl_entry->byte_count;
661 sw_acc_tbl_entry->pkt_count = 0;
662 sw_acc_tbl_entry->byte_count = 0;
664 pthread_mutex_unlock(&ulp_fc_info->fc_lock);
665 } else if (params.resource_sub_type ==
666 BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TABLE_INT_COUNT_ACC) {
667 /* Get stats from the parent child table */
668 ulp_flow_db_parent_flow_count_get(ctxt, flow_id,
669 &count->hits, &count->bytes,
672 count->bytes_set = 1;
674 /* TBD: Handle External counters */
682 * Set the parent flow if it is SW accumulation counter entry.
684 * ctxt [in] The ulp context for the flow counter manager
686 * dir [in] The direction of the flow
688 * hw_cntr_id [in] The HW flow counter ID
690 * fid [in] parent flow id
693 int32_t ulp_fc_mgr_cntr_parent_flow_set(struct bnxt_ulp_context *ctxt,
698 struct bnxt_ulp_fc_info *ulp_fc_info;
699 uint32_t sw_cntr_idx;
702 ulp_fc_info = bnxt_ulp_cntxt_ptr2_fc_info_get(ctxt);
706 pthread_mutex_lock(&ulp_fc_info->fc_lock);
707 sw_cntr_idx = hw_cntr_id - ulp_fc_info->shadow_hw_tbl[dir].start_idx;
708 if (ulp_fc_info->sw_acc_tbl[dir][sw_cntr_idx].valid) {
709 ulp_fc_info->sw_acc_tbl[dir][sw_cntr_idx].parent_flow_id = fid;
711 BNXT_TF_DBG(ERR, "Failed to set parent flow id %x:%x\n",
715 pthread_mutex_unlock(&ulp_fc_info->fc_lock);