1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2014-2020 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 = (uintptr_t)fc_info->shadow_hw_tbl[dir].mem_pa;
258 "BULK: Memory not initialized id:0x%x dir:%d\n",
259 parms.starting_idx, dir);
263 rc = tf_tbl_bulk_get(tfp, &parms);
266 "BULK: Get failed for id:0x%x rc:%d\n",
267 parms.starting_idx, rc);
271 for (i = 0; i < parms.num_entries; i++) {
272 /* TBD - Get PKT/BYTE COUNT SHIFT/MASK from Template */
273 sw_acc_tbl_entry = &fc_info->sw_acc_tbl[dir][i];
274 if (!sw_acc_tbl_entry->valid)
276 sw_acc_tbl_entry->pkt_count += FLOW_CNTR_PKTS(stats[i],
278 sw_acc_tbl_entry->byte_count += FLOW_CNTR_BYTES(stats[i],
285 static int ulp_get_single_flow_stat(struct bnxt_ulp_context *ctxt,
287 struct bnxt_ulp_fc_info *fc_info,
290 struct bnxt_ulp_device_params *dparms)
293 struct tf_get_tbl_entry_parms parms = { 0 };
294 enum tf_tbl_type stype = TF_TBL_TYPE_ACT_STATS_64; /* TBD:Template? */
295 struct sw_acc_counter *sw_acc_tbl_entry = NULL, *t_sw;
297 uint32_t sw_cntr_indx = 0;
301 parms.idx = hw_cntr_id;
304 * Size of an entry needs to obtained from template
306 parms.data_sz_in_bytes = sizeof(uint64_t);
307 parms.data = (uint8_t *)&stats;
308 rc = tf_get_tbl_entry(tfp, &parms);
311 "Get failed for id:0x%x rc:%d\n",
316 /* TBD - Get PKT/BYTE COUNT SHIFT/MASK from Template */
317 sw_cntr_indx = hw_cntr_id - fc_info->shadow_hw_tbl[dir].start_idx;
318 sw_acc_tbl_entry = &fc_info->sw_acc_tbl[dir][sw_cntr_indx];
319 sw_acc_tbl_entry->pkt_count = FLOW_CNTR_PKTS(stats, dparms);
320 sw_acc_tbl_entry->byte_count = FLOW_CNTR_BYTES(stats, dparms);
322 /* Update the parent counters if it is child flow */
323 if (sw_acc_tbl_entry->parent_flow_id) {
324 /* Update the parent counters */
325 t_sw = sw_acc_tbl_entry;
326 if (ulp_flow_db_parent_flow_count_update(ctxt,
327 t_sw->parent_flow_id,
330 PMD_DRV_LOG(ERR, "Error updating parent counters\n");
338 * Alarm handler that will issue the TF-Core API to fetch
339 * data from the chip's internal flow counters
341 * ctxt [in] The ulp context for the flow counter manager
346 ulp_fc_mgr_alarm_cb(void *arg)
351 struct bnxt_ulp_context *ctxt = arg;
352 struct bnxt_ulp_fc_info *ulp_fc_info;
353 struct bnxt_ulp_device_params *dparms;
355 uint32_t dev_id, hw_cntr_id = 0, num_entries = 0;
357 ulp_fc_info = bnxt_ulp_cntxt_ptr2_fc_info_get(ctxt);
361 if (bnxt_ulp_cntxt_dev_id_get(ctxt, &dev_id)) {
362 BNXT_TF_DBG(DEBUG, "Failed to get device id\n");
366 dparms = bnxt_ulp_device_params_get(dev_id);
368 BNXT_TF_DBG(DEBUG, "Failed to device parms\n");
372 tfp = bnxt_ulp_cntxt_tfp_get(ctxt);
374 BNXT_TF_DBG(ERR, "Failed to get the truflow pointer\n");
379 * Take the fc_lock to ensure no flow is destroyed
380 * during the bulk get
382 if (pthread_mutex_trylock(&ulp_fc_info->fc_lock))
385 if (!ulp_fc_info->num_entries) {
386 pthread_mutex_unlock(&ulp_fc_info->fc_lock);
387 ulp_fc_mgr_thread_cancel(ctxt);
391 * Commented for now till GET_BULK is resolved, just get the first flow
393 for (i = 0; i < TF_DIR_MAX; i++) {
394 rc = ulp_bulk_get_flow_stats(tfp, ulp_fc_info, i,
395 dparms->flow_count_db_entries);
401 /* reset the parent accumulation counters before accumulation if any */
402 ulp_flow_db_parent_flow_count_reset(ctxt);
404 num_entries = dparms->flow_count_db_entries / 2;
405 for (i = 0; i < TF_DIR_MAX; i++) {
406 for (j = 0; j < num_entries; j++) {
407 if (!ulp_fc_info->sw_acc_tbl[i][j].valid)
409 hw_cntr_id = ulp_fc_info->sw_acc_tbl[i][j].hw_cntr_id;
410 rc = ulp_get_single_flow_stat(ctxt, tfp, ulp_fc_info, i,
417 pthread_mutex_unlock(&ulp_fc_info->fc_lock);
420 * If cmd fails once, no need of
421 * invoking again every second
425 ulp_fc_mgr_thread_cancel(ctxt);
429 rte_eal_alarm_set(US_PER_S * ULP_FC_TIMER,
435 * Set the starting index that indicates the first HW flow
438 * ctxt [in] The ulp context for the flow counter manager
440 * dir [in] The direction of the flow
442 * start_idx [in] The HW flow counter ID
445 bool ulp_fc_mgr_start_idx_isset(struct bnxt_ulp_context *ctxt, enum tf_dir dir)
447 struct bnxt_ulp_fc_info *ulp_fc_info;
449 ulp_fc_info = bnxt_ulp_cntxt_ptr2_fc_info_get(ctxt);
451 return ulp_fc_info->shadow_hw_tbl[dir].start_idx_is_set;
455 * Set the starting index that indicates the first HW flow
458 * ctxt [in] The ulp context for the flow counter manager
460 * dir [in] The direction of the flow
462 * start_idx [in] The HW flow counter ID
465 int32_t ulp_fc_mgr_start_idx_set(struct bnxt_ulp_context *ctxt, enum tf_dir dir,
468 struct bnxt_ulp_fc_info *ulp_fc_info;
470 ulp_fc_info = bnxt_ulp_cntxt_ptr2_fc_info_get(ctxt);
475 if (!ulp_fc_info->shadow_hw_tbl[dir].start_idx_is_set) {
476 ulp_fc_info->shadow_hw_tbl[dir].start_idx = start_idx;
477 ulp_fc_info->shadow_hw_tbl[dir].start_idx_is_set = true;
484 * Set the corresponding SW accumulator table entry based on
485 * the difference between this counter ID and the starting
486 * counter ID. Also, keep track of num of active counter enabled
489 * ctxt [in] The ulp context for the flow counter manager
491 * dir [in] The direction of the flow
493 * hw_cntr_id [in] The HW flow counter ID
496 int32_t ulp_fc_mgr_cntr_set(struct bnxt_ulp_context *ctxt, enum tf_dir dir,
499 struct bnxt_ulp_fc_info *ulp_fc_info;
500 uint32_t sw_cntr_idx;
502 ulp_fc_info = bnxt_ulp_cntxt_ptr2_fc_info_get(ctxt);
506 pthread_mutex_lock(&ulp_fc_info->fc_lock);
507 sw_cntr_idx = hw_cntr_id - ulp_fc_info->shadow_hw_tbl[dir].start_idx;
508 ulp_fc_info->sw_acc_tbl[dir][sw_cntr_idx].valid = true;
509 ulp_fc_info->sw_acc_tbl[dir][sw_cntr_idx].hw_cntr_id = hw_cntr_id;
510 ulp_fc_info->num_entries++;
511 pthread_mutex_unlock(&ulp_fc_info->fc_lock);
517 * Reset the corresponding SW accumulator table entry based on
518 * the difference between this counter ID and the starting
521 * ctxt [in] The ulp context for the flow counter manager
523 * dir [in] The direction of the flow
525 * hw_cntr_id [in] The HW flow counter ID
528 int32_t ulp_fc_mgr_cntr_reset(struct bnxt_ulp_context *ctxt, enum tf_dir dir,
531 struct bnxt_ulp_fc_info *ulp_fc_info;
532 uint32_t sw_cntr_idx;
534 ulp_fc_info = bnxt_ulp_cntxt_ptr2_fc_info_get(ctxt);
538 pthread_mutex_lock(&ulp_fc_info->fc_lock);
539 sw_cntr_idx = hw_cntr_id - ulp_fc_info->shadow_hw_tbl[dir].start_idx;
540 ulp_fc_info->sw_acc_tbl[dir][sw_cntr_idx].valid = false;
541 ulp_fc_info->sw_acc_tbl[dir][sw_cntr_idx].hw_cntr_id = 0;
542 ulp_fc_info->sw_acc_tbl[dir][sw_cntr_idx].pkt_count = 0;
543 ulp_fc_info->sw_acc_tbl[dir][sw_cntr_idx].byte_count = 0;
544 ulp_fc_info->num_entries--;
545 pthread_mutex_unlock(&ulp_fc_info->fc_lock);
551 * Fill the rte_flow_query_count 'data' argument passed
552 * in the rte_flow_query() with the values obtained and
553 * accumulated locally.
555 * ctxt [in] The ulp context for the flow counter manager
557 * flow_id [in] The HW flow ID
559 * count [out] The rte_flow_query_count 'data' that is set
562 int ulp_fc_mgr_query_count_get(struct bnxt_ulp_context *ctxt,
564 struct rte_flow_query_count *count)
567 uint32_t nxt_resource_index = 0;
568 struct bnxt_ulp_fc_info *ulp_fc_info;
569 struct ulp_flow_db_res_params params;
571 uint32_t hw_cntr_id = 0, sw_cntr_idx = 0;
572 struct sw_acc_counter *sw_acc_tbl_entry;
573 bool found_cntr_resource = false;
575 ulp_fc_info = bnxt_ulp_cntxt_ptr2_fc_info_get(ctxt);
579 if (bnxt_ulp_cntxt_acquire_fdb_lock(ctxt))
583 rc = ulp_flow_db_resource_get(ctxt,
584 BNXT_ULP_FDB_TYPE_REGULAR,
588 if (params.resource_func ==
589 BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE &&
590 (params.resource_sub_type ==
591 BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TYPE_INT_COUNT ||
592 params.resource_sub_type ==
593 BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TYPE_EXT_COUNT ||
594 params.resource_sub_type ==
595 BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TYPE_INT_COUNT_ACC)) {
596 found_cntr_resource = true;
599 } while (!rc && nxt_resource_index);
601 bnxt_ulp_cntxt_release_fdb_lock(ctxt);
603 if (rc || !found_cntr_resource)
606 dir = params.direction;
607 hw_cntr_id = params.resource_hndl;
608 if (params.resource_sub_type ==
609 BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TYPE_INT_COUNT) {
610 pthread_mutex_lock(&ulp_fc_info->fc_lock);
611 sw_cntr_idx = hw_cntr_id -
612 ulp_fc_info->shadow_hw_tbl[dir].start_idx;
613 sw_acc_tbl_entry = &ulp_fc_info->sw_acc_tbl[dir][sw_cntr_idx];
614 if (sw_acc_tbl_entry->pkt_count) {
616 count->bytes_set = 1;
617 count->hits = sw_acc_tbl_entry->pkt_count;
618 count->bytes = sw_acc_tbl_entry->byte_count;
621 sw_acc_tbl_entry->pkt_count = 0;
622 sw_acc_tbl_entry->byte_count = 0;
624 pthread_mutex_unlock(&ulp_fc_info->fc_lock);
625 } else if (params.resource_sub_type ==
626 BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TYPE_INT_COUNT_ACC) {
627 /* Get stats from the parent child table */
628 ulp_flow_db_parent_flow_count_get(ctxt, flow_id,
629 &count->hits, &count->bytes,
632 count->bytes_set = 1;
634 /* TBD: Handle External counters */
642 * Set the parent flow if it is SW accumulation counter entry.
644 * ctxt [in] The ulp context for the flow counter manager
646 * dir [in] The direction of the flow
648 * hw_cntr_id [in] The HW flow counter ID
650 * fid [in] parent flow id
653 int32_t ulp_fc_mgr_cntr_parent_flow_set(struct bnxt_ulp_context *ctxt,
658 struct bnxt_ulp_fc_info *ulp_fc_info;
659 uint32_t sw_cntr_idx;
662 ulp_fc_info = bnxt_ulp_cntxt_ptr2_fc_info_get(ctxt);
666 pthread_mutex_lock(&ulp_fc_info->fc_lock);
667 sw_cntr_idx = hw_cntr_id - ulp_fc_info->shadow_hw_tbl[dir].start_idx;
668 if (ulp_fc_info->sw_acc_tbl[dir][sw_cntr_idx].valid) {
669 ulp_fc_info->sw_acc_tbl[dir][sw_cntr_idx].parent_flow_id = fid;
671 BNXT_TF_DBG(ERR, "Failed to set parent flow id %x:%x\n",
675 pthread_mutex_unlock(&ulp_fc_info->fc_lock);