net/bnxt: refactor TRUFLOW processing
[dpdk.git] / drivers / net / bnxt / tf_ulp / ulp_flow_db.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2014-2021 Broadcom
3  * All rights reserved.
4  */
5
6 #include <rte_malloc.h>
7 #include "bnxt.h"
8 #include "bnxt_tf_common.h"
9 #include "ulp_utils.h"
10 #include "ulp_template_struct.h"
11 #include "ulp_mapper.h"
12 #include "ulp_flow_db.h"
13 #include "ulp_fc_mgr.h"
14 #include "ulp_tun.h"
15
16 #define ULP_FLOW_DB_RES_DIR_BIT         31
17 #define ULP_FLOW_DB_RES_DIR_MASK        0x80000000
18 #define ULP_FLOW_DB_RES_FUNC_BITS       28
19 #define ULP_FLOW_DB_RES_FUNC_MASK       0x70000000
20 #define ULP_FLOW_DB_RES_NXT_MASK        0x0FFFFFFF
21 #define ULP_FLOW_DB_RES_FUNC_UPPER      5
22 #define ULP_FLOW_DB_RES_FUNC_NEED_LOWER 0x80
23 #define ULP_FLOW_DB_RES_FUNC_LOWER_MASK 0x1F
24
25 /* Macro to copy the nxt_resource_idx */
26 #define ULP_FLOW_DB_RES_NXT_SET(dst, src)       {(dst) |= ((src) &\
27                                          ULP_FLOW_DB_RES_NXT_MASK); }
28 #define ULP_FLOW_DB_RES_NXT_RESET(dst)  ((dst) &= ~(ULP_FLOW_DB_RES_NXT_MASK))
29
30 /*
31  * Helper function to set the bit in the active flows
32  * No validation is done in this function.
33  *
34  * flow_db [in] Ptr to flow database
35  * flow_type [in] - specify default or regular
36  * idx [in] The index to bit to be set or reset.
37  * flag [in] 1 to set and 0 to reset.
38  *
39  * returns none
40  */
41 static void
42 ulp_flow_db_active_flows_bit_set(struct bnxt_ulp_flow_db *flow_db,
43                                  enum bnxt_ulp_fdb_type flow_type,
44                                  uint32_t idx,
45                                  uint32_t flag)
46 {
47         struct bnxt_ulp_flow_tbl *f_tbl = &flow_db->flow_tbl;
48         uint32_t a_idx = idx / ULP_INDEX_BITMAP_SIZE;
49
50         if (flag) {
51                 if (flow_type == BNXT_ULP_FDB_TYPE_REGULAR)
52                         ULP_INDEX_BITMAP_SET(f_tbl->active_reg_flows[a_idx],
53                                              idx);
54                 else
55                         ULP_INDEX_BITMAP_SET(f_tbl->active_dflt_flows[a_idx],
56                                              idx);
57         } else {
58                 if (flow_type == BNXT_ULP_FDB_TYPE_REGULAR)
59                         ULP_INDEX_BITMAP_RESET(f_tbl->active_reg_flows[a_idx],
60                                                idx);
61                 else
62                         ULP_INDEX_BITMAP_RESET(f_tbl->active_dflt_flows[a_idx],
63                                                idx);
64         }
65 }
66
67 /*
68  * Helper function to check if given fid is active flow.
69  * No validation being done in this function.
70  *
71  * flow_db [in] Ptr to flow database
72  * flow_type [in] - specify default or regular
73  * idx [in] The index to bit to be set or reset.
74  *
75  * returns 1 on set or 0 if not set.
76  */
77 static int32_t
78 ulp_flow_db_active_flows_bit_is_set(struct bnxt_ulp_flow_db *flow_db,
79                                     enum bnxt_ulp_fdb_type flow_type,
80                                     uint32_t idx)
81 {
82         struct bnxt_ulp_flow_tbl *f_tbl = &flow_db->flow_tbl;
83         uint32_t a_idx = idx / ULP_INDEX_BITMAP_SIZE;
84
85         if (flow_type == BNXT_ULP_FDB_TYPE_REGULAR)
86                 return ULP_INDEX_BITMAP_GET(f_tbl->active_reg_flows[a_idx],
87                                             idx);
88         else
89                 return ULP_INDEX_BITMAP_GET(f_tbl->active_dflt_flows[a_idx],
90                                             idx);
91 }
92
93 static inline enum tf_dir
94 ulp_flow_db_resource_dir_get(struct ulp_fdb_resource_info *res_info)
95 {
96         return ((res_info->nxt_resource_idx & ULP_FLOW_DB_RES_DIR_MASK) >>
97                 ULP_FLOW_DB_RES_DIR_BIT);
98 }
99
100 static uint8_t
101 ulp_flow_db_resource_func_get(struct ulp_fdb_resource_info *res_info)
102 {
103         uint8_t func;
104
105         func = (((res_info->nxt_resource_idx & ULP_FLOW_DB_RES_FUNC_MASK) >>
106                  ULP_FLOW_DB_RES_FUNC_BITS) << ULP_FLOW_DB_RES_FUNC_UPPER);
107         /* The reource func is split into upper and lower */
108         if (func & ULP_FLOW_DB_RES_FUNC_NEED_LOWER)
109                 return (func | res_info->resource_func_lower);
110         return func;
111 }
112
113 /*
114  * Helper function to copy the resource params to resource info
115  *  No validation being done in this function.
116  *
117  * resource_info [out] Ptr to resource information
118  * params [in] The input params from the caller
119  * returns none
120  */
121 static void
122 ulp_flow_db_res_params_to_info(struct ulp_fdb_resource_info *resource_info,
123                                struct ulp_flow_db_res_params *params)
124 {
125         uint32_t resource_func;
126
127         resource_info->nxt_resource_idx |= ((params->direction <<
128                                       ULP_FLOW_DB_RES_DIR_BIT) &
129                                      ULP_FLOW_DB_RES_DIR_MASK);
130         resource_func = (params->resource_func >> ULP_FLOW_DB_RES_FUNC_UPPER);
131         resource_info->nxt_resource_idx |= ((resource_func <<
132                                              ULP_FLOW_DB_RES_FUNC_BITS) &
133                                             ULP_FLOW_DB_RES_FUNC_MASK);
134
135         if (params->resource_func & ULP_FLOW_DB_RES_FUNC_NEED_LOWER) {
136                 /* Break the resource func into two parts */
137                 resource_func = (params->resource_func &
138                                  ULP_FLOW_DB_RES_FUNC_LOWER_MASK);
139                 resource_info->resource_func_lower = resource_func;
140         }
141
142         /* Store the handle as 64bit only for EM table entries */
143         if (params->resource_func != BNXT_ULP_RESOURCE_FUNC_EXT_EM_TABLE &&
144             params->resource_func != BNXT_ULP_RESOURCE_FUNC_INT_EM_TABLE) {
145                 resource_info->resource_hndl = (uint32_t)params->resource_hndl;
146                 resource_info->resource_type = params->resource_type;
147                 resource_info->resource_sub_type = params->resource_sub_type;
148                 resource_info->reserved = params->reserved;
149         } else {
150                 resource_info->resource_em_handle = params->resource_hndl;
151         }
152 }
153
154 /*
155  * Helper function to copy the resource params to resource info
156  *  No validation being done in this function.
157  *
158  * resource_info [in] Ptr to resource information
159  * params [out] The output params to the caller
160  *
161  * returns none
162  */
163 static void
164 ulp_flow_db_res_info_to_params(struct ulp_fdb_resource_info *resource_info,
165                                struct ulp_flow_db_res_params *params)
166 {
167         memset(params, 0, sizeof(struct ulp_flow_db_res_params));
168
169         /* use the helper function to get the resource func */
170         params->direction = ulp_flow_db_resource_dir_get(resource_info);
171         params->resource_func = ulp_flow_db_resource_func_get(resource_info);
172
173         if (params->resource_func == BNXT_ULP_RESOURCE_FUNC_EXT_EM_TABLE ||
174             params->resource_func == BNXT_ULP_RESOURCE_FUNC_INT_EM_TABLE) {
175                 params->resource_hndl = resource_info->resource_em_handle;
176         } else if (params->resource_func & ULP_FLOW_DB_RES_FUNC_NEED_LOWER) {
177                 params->resource_hndl = resource_info->resource_hndl;
178                 params->resource_type = resource_info->resource_type;
179                 params->resource_sub_type = resource_info->resource_sub_type;
180                 params->reserved = resource_info->reserved;
181         }
182 }
183
184 /*
185  * Helper function to allocate the flow table and initialize
186  * the stack for allocation operations.
187  *
188  * flow_db [in] Ptr to flow database structure
189  *
190  * Returns 0 on success or negative number on failure.
191  */
192 static int32_t
193 ulp_flow_db_alloc_resource(struct bnxt_ulp_flow_db *flow_db)
194 {
195         uint32_t                        idx = 0;
196         struct bnxt_ulp_flow_tbl        *flow_tbl;
197         uint32_t                        size;
198
199         flow_tbl = &flow_db->flow_tbl;
200
201         size = sizeof(struct ulp_fdb_resource_info) * flow_tbl->num_resources;
202         flow_tbl->flow_resources =
203                         rte_zmalloc("ulp_fdb_resource_info", size, 0);
204
205         if (!flow_tbl->flow_resources) {
206                 BNXT_TF_DBG(ERR, "Failed to alloc memory for flow table\n");
207                 return -ENOMEM;
208         }
209         size = sizeof(uint32_t) * flow_tbl->num_resources;
210         flow_tbl->flow_tbl_stack = rte_zmalloc("flow_tbl_stack", size, 0);
211         if (!flow_tbl->flow_tbl_stack) {
212                 BNXT_TF_DBG(ERR, "Failed to alloc memory flow tbl stack\n");
213                 return -ENOMEM;
214         }
215         size = (flow_tbl->num_flows / sizeof(uint64_t)) + 1;
216         size =  ULP_BYTE_ROUND_OFF_8(size);
217         flow_tbl->active_reg_flows = rte_zmalloc("active reg flows", size,
218                                                  ULP_BUFFER_ALIGN_64_BYTE);
219         if (!flow_tbl->active_reg_flows) {
220                 BNXT_TF_DBG(ERR, "Failed to alloc memory active reg flows\n");
221                 return -ENOMEM;
222         }
223
224         flow_tbl->active_dflt_flows = rte_zmalloc("active dflt flows", size,
225                                                   ULP_BUFFER_ALIGN_64_BYTE);
226         if (!flow_tbl->active_dflt_flows) {
227                 BNXT_TF_DBG(ERR, "Failed to alloc memory active dflt flows\n");
228                 return -ENOMEM;
229         }
230
231         /* Initialize the stack table. */
232         for (idx = 0; idx < flow_tbl->num_resources; idx++)
233                 flow_tbl->flow_tbl_stack[idx] = idx;
234
235         /* Ignore the first element in the list. */
236         flow_tbl->head_index = 1;
237         /* Tail points to the last entry in the list. */
238         flow_tbl->tail_index = flow_tbl->num_resources - 1;
239         return 0;
240 }
241
242 /*
243  * Helper function to deallocate the flow table.
244  *
245  * flow_db [in] Ptr to flow database structure
246  *
247  * Returns none.
248  */
249 static void
250 ulp_flow_db_dealloc_resource(struct bnxt_ulp_flow_db *flow_db)
251 {
252         struct bnxt_ulp_flow_tbl *flow_tbl = &flow_db->flow_tbl;
253
254         /* Free all the allocated tables in the flow table. */
255         if (flow_tbl->active_reg_flows) {
256                 rte_free(flow_tbl->active_reg_flows);
257                 flow_tbl->active_reg_flows = NULL;
258         }
259         if (flow_tbl->active_dflt_flows) {
260                 rte_free(flow_tbl->active_dflt_flows);
261                 flow_tbl->active_dflt_flows = NULL;
262         }
263
264         if (flow_tbl->flow_tbl_stack) {
265                 rte_free(flow_tbl->flow_tbl_stack);
266                 flow_tbl->flow_tbl_stack = NULL;
267         }
268
269         if (flow_tbl->flow_resources) {
270                 rte_free(flow_tbl->flow_resources);
271                 flow_tbl->flow_resources = NULL;
272         }
273 }
274
275 /*
276  * Helper function to add function id to the flow table
277  *
278  * flow_db [in] Ptr to flow table
279  * flow_id [in] The flow id of the flow
280  * func_id [in] The func_id to be set, for reset pass zero
281  *
282  * returns none
283  */
284 static void
285 ulp_flow_db_func_id_set(struct bnxt_ulp_flow_db *flow_db,
286                         uint32_t flow_id,
287                         uint32_t func_id)
288 {
289         /* set the function id in the function table */
290         if (flow_id < flow_db->func_id_tbl_size)
291                 flow_db->func_id_tbl[flow_id] = func_id;
292         else /* This should never happen */
293                 BNXT_TF_DBG(ERR, "Invalid flow id, flowdb corrupt\n");
294 }
295
296 /*
297  * Initialize the parent-child database. Memory is allocated in this
298  * call and assigned to the database
299  *
300  * flow_db [in] Ptr to flow table
301  * num_entries[in] - number of entries to allocate
302  *
303  * Returns 0 on success or negative number on failure.
304  */
305 static int32_t
306 ulp_flow_db_parent_tbl_init(struct bnxt_ulp_flow_db *flow_db,
307                             uint32_t num_entries)
308 {
309         struct ulp_fdb_parent_child_db *p_db;
310         uint32_t size, idx;
311
312         if (!num_entries)
313                 return 0;
314
315         /* update the sizes for the allocation */
316         p_db = &flow_db->parent_child_db;
317         p_db->child_bitset_size = (flow_db->flow_tbl.num_flows /
318                                    sizeof(uint64_t)) + 1; /* size in bytes */
319         p_db->child_bitset_size = ULP_BYTE_ROUND_OFF_8(p_db->child_bitset_size);
320         p_db->entries_count = num_entries;
321
322         /* allocate the memory */
323         p_db->parent_flow_tbl = rte_zmalloc("fdb parent flow tbl",
324                                             sizeof(struct ulp_fdb_parent_info) *
325                                             p_db->entries_count, 0);
326         if (!p_db->parent_flow_tbl) {
327                 BNXT_TF_DBG(ERR,
328                             "Failed to allocate memory fdb parent flow tbl\n");
329                 return -ENOMEM;
330         }
331         size = p_db->child_bitset_size * p_db->entries_count;
332
333         /*
334          * allocate the big chunk of memory to be statically carved into
335          * child_fid_bitset pointer.
336          */
337         p_db->parent_flow_tbl_mem = rte_zmalloc("fdb parent flow tbl mem",
338                                                 size,
339                                                 ULP_BUFFER_ALIGN_64_BYTE);
340         if (!p_db->parent_flow_tbl_mem) {
341                 BNXT_TF_DBG(ERR,
342                             "Failed to allocate memory fdb parent flow mem\n");
343                 return -ENOMEM;
344         }
345
346         /* set the pointers in parent table to their offsets */
347         for (idx = 0 ; idx < p_db->entries_count; idx++) {
348                 p_db->parent_flow_tbl[idx].child_fid_bitset =
349                         (uint64_t *)&p_db->parent_flow_tbl_mem[idx *
350                         p_db->child_bitset_size];
351         }
352         /* success */
353         return 0;
354 }
355
356 /*
357  * Deinitialize the parent-child database. Memory is deallocated in
358  * this call and all flows should have been purged before this
359  * call.
360  *
361  * flow_db [in] Ptr to flow table
362  *
363  * Returns none
364  */
365 static void
366 ulp_flow_db_parent_tbl_deinit(struct bnxt_ulp_flow_db *flow_db)
367 {
368         /* free the memory related to parent child database */
369         if (flow_db->parent_child_db.parent_flow_tbl_mem) {
370                 rte_free(flow_db->parent_child_db.parent_flow_tbl_mem);
371                 flow_db->parent_child_db.parent_flow_tbl_mem = NULL;
372         }
373         if (flow_db->parent_child_db.parent_flow_tbl) {
374                 rte_free(flow_db->parent_child_db.parent_flow_tbl);
375                 flow_db->parent_child_db.parent_flow_tbl = NULL;
376         }
377 }
378
379 /* internal validation function for parent flow tbl */
380 static struct bnxt_ulp_flow_db *
381 ulp_flow_db_parent_arg_validation(struct bnxt_ulp_context *ulp_ctxt,
382                                   uint32_t fid)
383 {
384         struct bnxt_ulp_flow_db *flow_db;
385
386         flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
387         if (!flow_db) {
388                 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
389                 return NULL;
390         }
391
392         /* check for max flows */
393         if (fid >= flow_db->flow_tbl.num_flows || !fid) {
394                 BNXT_TF_DBG(ERR, "Invalid flow index\n");
395                 return NULL;
396         }
397
398         /* No support for parent child db then just exit */
399         if (!flow_db->parent_child_db.entries_count) {
400                 BNXT_TF_DBG(ERR, "parent child db not supported\n");
401                 return NULL;
402         }
403
404         return flow_db;
405 }
406
407 /*
408  * Set the tunnel index in the parent flow
409  *
410  * ulp_ctxt [in] Ptr to ulp_context
411  * parent_idx [in] The parent index of the parent flow entry
412  *
413  * returns index on success and negative on failure.
414  */
415 static int32_t
416 ulp_flow_db_parent_tun_idx_set(struct bnxt_ulp_context *ulp_ctxt,
417                                uint32_t parent_idx, uint8_t tun_idx)
418 {
419         struct bnxt_ulp_flow_db *flow_db;
420         struct ulp_fdb_parent_child_db *p_pdb;
421
422         flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
423         if (!flow_db) {
424                 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
425                 return -EINVAL;
426         }
427
428         /* check for parent idx validity */
429         p_pdb = &flow_db->parent_child_db;
430         if (parent_idx >= p_pdb->entries_count ||
431             !p_pdb->parent_flow_tbl[parent_idx].parent_fid) {
432                 BNXT_TF_DBG(ERR, "Invalid parent flow index %x\n", parent_idx);
433                 return -EINVAL;
434         }
435
436         p_pdb->parent_flow_tbl[parent_idx].tun_idx = tun_idx;
437         return 0;
438 }
439
440 /*
441  * Get the tunnel index from the parent flow
442  *
443  * ulp_ctxt [in] Ptr to ulp_context
444  * parent_fid [in] The flow id of the parent flow entry
445  *
446  * returns 0 if counter accum is set else -1.
447  */
448 static int32_t
449 ulp_flow_db_parent_tun_idx_get(struct bnxt_ulp_context *ulp_ctxt,
450                                uint32_t parent_fid, uint8_t *tun_idx)
451 {
452         struct bnxt_ulp_flow_db *flow_db;
453         struct ulp_fdb_parent_child_db *p_pdb;
454         uint32_t idx;
455
456         /* validate the arguments */
457         flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, parent_fid);
458         if (!flow_db) {
459                 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
460                 return -EINVAL;
461         }
462
463         p_pdb = &flow_db->parent_child_db;
464         for (idx = 0; idx < p_pdb->entries_count; idx++) {
465                 if (p_pdb->parent_flow_tbl[idx].parent_fid == parent_fid) {
466                         *tun_idx = p_pdb->parent_flow_tbl[idx].tun_idx;
467                         return 0;
468                 }
469         }
470
471         return -EINVAL;
472 }
473
474 /*
475  * Initialize the flow database. Memory is allocated in this
476  * call and assigned to the flow database.
477  *
478  * ulp_ctxt [in] Ptr to ulp context
479  *
480  * Returns 0 on success or negative number on failure.
481  */
482 int32_t
483 ulp_flow_db_init(struct bnxt_ulp_context *ulp_ctxt)
484 {
485         struct bnxt_ulp_device_params *dparms;
486         struct bnxt_ulp_flow_tbl *flow_tbl;
487         struct bnxt_ulp_flow_db *flow_db;
488         uint32_t dev_id, num_flows;
489         enum bnxt_ulp_flow_mem_type mtype;
490
491         /* Get the dev specific number of flows that needed to be supported. */
492         if (bnxt_ulp_cntxt_dev_id_get(ulp_ctxt, &dev_id)) {
493                 BNXT_TF_DBG(ERR, "Invalid device id\n");
494                 return -EINVAL;
495         }
496
497         dparms = bnxt_ulp_device_params_get(dev_id);
498         if (!dparms) {
499                 BNXT_TF_DBG(ERR, "could not fetch the device params\n");
500                 return -ENODEV;
501         }
502
503         flow_db = rte_zmalloc("bnxt_ulp_flow_db",
504                               sizeof(struct bnxt_ulp_flow_db), 0);
505         if (!flow_db) {
506                 BNXT_TF_DBG(ERR,
507                             "Failed to allocate memory for flow table ptr\n");
508                 return -ENOMEM;
509         }
510
511         /* Attach the flow database to the ulp context. */
512         bnxt_ulp_cntxt_ptr2_flow_db_set(ulp_ctxt, flow_db);
513
514         /* Determine the number of flows based on EM type */
515         bnxt_ulp_cntxt_mem_type_get(ulp_ctxt, &mtype);
516         if (mtype == BNXT_ULP_FLOW_MEM_TYPE_INT)
517                 num_flows = dparms->int_flow_db_num_entries;
518         else
519                 num_flows = dparms->ext_flow_db_num_entries;
520
521         /* Populate the regular flow table limits. */
522         flow_tbl = &flow_db->flow_tbl;
523         flow_tbl->num_flows = num_flows + 1;
524         flow_tbl->num_resources = ((num_flows + 1) *
525                                    dparms->num_resources_per_flow);
526
527         /* Include the default flow table limits. */
528         flow_tbl->num_flows += (BNXT_FLOW_DB_DEFAULT_NUM_FLOWS + 1);
529         flow_tbl->num_resources += ((BNXT_FLOW_DB_DEFAULT_NUM_FLOWS + 1) *
530                                     BNXT_FLOW_DB_DEFAULT_NUM_RESOURCES);
531
532         /* Allocate the resource for the flow table. */
533         if (ulp_flow_db_alloc_resource(flow_db))
534                 goto error_free;
535
536         /* add 1 since we are not using index 0 for flow id */
537         flow_db->func_id_tbl_size = flow_tbl->num_flows + 1;
538         /* Allocate the function Id table */
539         flow_db->func_id_tbl = rte_zmalloc("bnxt_ulp_flow_db_func_id_table",
540                                            flow_db->func_id_tbl_size *
541                                            sizeof(uint16_t), 0);
542         if (!flow_db->func_id_tbl) {
543                 BNXT_TF_DBG(ERR,
544                             "Failed to allocate mem for flow table func id\n");
545                 goto error_free;
546         }
547         /* initialize the parent child database */
548         if (ulp_flow_db_parent_tbl_init(flow_db,
549                                         dparms->fdb_parent_flow_entries)) {
550                 BNXT_TF_DBG(ERR,
551                             "Failed to allocate mem for parent child db\n");
552                 goto error_free;
553         }
554
555         /* All good so return. */
556         BNXT_TF_DBG(INFO, "FlowDB initialized with %d flows.\n",
557                     flow_tbl->num_flows);
558         return 0;
559 error_free:
560         ulp_flow_db_deinit(ulp_ctxt);
561         return -ENOMEM;
562 }
563
564 /*
565  * Deinitialize the flow database. Memory is deallocated in
566  * this call and all flows should have been purged before this
567  * call.
568  *
569  * ulp_ctxt [in] Ptr to ulp context
570  *
571  * Returns 0 on success.
572  */
573 int32_t
574 ulp_flow_db_deinit(struct bnxt_ulp_context *ulp_ctxt)
575 {
576         struct bnxt_ulp_flow_db *flow_db;
577
578         flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
579         if (!flow_db)
580                 return -EINVAL;
581
582         /* Detach the flow database from the ulp context. */
583         bnxt_ulp_cntxt_ptr2_flow_db_set(ulp_ctxt, NULL);
584
585         /* Free up all the memory. */
586         ulp_flow_db_parent_tbl_deinit(flow_db);
587         ulp_flow_db_dealloc_resource(flow_db);
588         rte_free(flow_db->func_id_tbl);
589         rte_free(flow_db);
590
591         return 0;
592 }
593
594 /*
595  * Allocate the flow database entry
596  *
597  * ulp_ctxt [in] Ptr to ulp_context
598  * flow_type [in] - specify default or regular
599  * func_id [in].function id of the ingress port
600  * fid [out] The index to the flow entry
601  *
602  * returns 0 on success and negative on failure.
603  */
604 int32_t
605 ulp_flow_db_fid_alloc(struct bnxt_ulp_context *ulp_ctxt,
606                       enum bnxt_ulp_fdb_type flow_type,
607                       uint16_t func_id,
608                       uint32_t *fid)
609 {
610         struct bnxt_ulp_flow_db *flow_db;
611         struct bnxt_ulp_flow_tbl *flow_tbl;
612
613         *fid = 0; /* Initialize fid to invalid value */
614         flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
615         if (!flow_db) {
616                 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
617                 return -EINVAL;
618         }
619
620         if (flow_type > BNXT_ULP_FDB_TYPE_DEFAULT) {
621                 BNXT_TF_DBG(ERR, "Invalid flow type\n");
622                 return -EINVAL;
623         }
624
625         flow_tbl = &flow_db->flow_tbl;
626         /* check for max flows */
627         if (flow_tbl->num_flows <= flow_tbl->head_index) {
628                 BNXT_TF_DBG(ERR, "Flow database has reached max flows\n");
629                 return -ENOMEM;
630         }
631         if (flow_tbl->tail_index <= (flow_tbl->head_index + 1)) {
632                 BNXT_TF_DBG(ERR, "Flow database has reached max resources\n");
633                 return -ENOMEM;
634         }
635         *fid = flow_tbl->flow_tbl_stack[flow_tbl->head_index];
636         flow_tbl->head_index++;
637
638         /* Set the flow type */
639         ulp_flow_db_active_flows_bit_set(flow_db, flow_type, *fid, 1);
640
641         /* function id update is only valid for regular flow table */
642         if (flow_type == BNXT_ULP_FDB_TYPE_REGULAR)
643                 ulp_flow_db_func_id_set(flow_db, *fid, func_id);
644
645         /* return success */
646         return 0;
647 }
648
649 /*
650  * Allocate the flow database entry.
651  * The params->critical_resource has to be set to 0 to allocate a new resource.
652  *
653  * ulp_ctxt [in] Ptr to ulp_context
654  * flow_type [in] Specify it is regular or default flow
655  * fid [in] The index to the flow entry
656  * params [in] The contents to be copied into resource
657  *
658  * returns 0 on success and negative on failure.
659  */
660 int32_t
661 ulp_flow_db_resource_add(struct bnxt_ulp_context *ulp_ctxt,
662                          enum bnxt_ulp_fdb_type flow_type,
663                          uint32_t fid,
664                          struct ulp_flow_db_res_params *params)
665 {
666         struct bnxt_ulp_flow_db *flow_db;
667         struct bnxt_ulp_flow_tbl *flow_tbl;
668         struct ulp_fdb_resource_info *resource, *fid_resource;
669         uint32_t idx;
670
671         flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
672         if (!flow_db) {
673                 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
674                 return -EINVAL;
675         }
676
677         if (flow_type > BNXT_ULP_FDB_TYPE_DEFAULT) {
678                 BNXT_TF_DBG(ERR, "Invalid flow type\n");
679                 return -EINVAL;
680         }
681
682         flow_tbl = &flow_db->flow_tbl;
683         /* check for max flows */
684         if (fid >= flow_tbl->num_flows || !fid) {
685                 BNXT_TF_DBG(ERR, "Invalid flow index\n");
686                 return -EINVAL;
687         }
688
689         /* check if the flow is active or not */
690         if (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type, fid)) {
691                 BNXT_TF_DBG(ERR, "flow does not exist\n");
692                 return -EINVAL;
693         }
694
695         /* check for max resource */
696         if ((flow_tbl->head_index + 1) >= flow_tbl->tail_index) {
697                 BNXT_TF_DBG(ERR, "Flow db has reached max resources\n");
698                 return -ENOMEM;
699         }
700         fid_resource = &flow_tbl->flow_resources[fid];
701
702         if (!params->critical_resource) {
703                 /* Not the critical_resource so allocate a resource */
704                 idx = flow_tbl->flow_tbl_stack[flow_tbl->tail_index];
705                 resource = &flow_tbl->flow_resources[idx];
706                 flow_tbl->tail_index--;
707
708                 /* Update the chain list of resource*/
709                 ULP_FLOW_DB_RES_NXT_SET(resource->nxt_resource_idx,
710                                         fid_resource->nxt_resource_idx);
711                 /* update the contents */
712                 ulp_flow_db_res_params_to_info(resource, params);
713                 ULP_FLOW_DB_RES_NXT_RESET(fid_resource->nxt_resource_idx);
714                 ULP_FLOW_DB_RES_NXT_SET(fid_resource->nxt_resource_idx,
715                                         idx);
716         } else {
717                 /* critical resource. Just update the fid resource */
718                 ulp_flow_db_res_params_to_info(fid_resource, params);
719         }
720
721         if (params->resource_type == TF_TBL_TYPE_ACT_STATS_64 &&
722             params->resource_sub_type ==
723             BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TABLE_INT_COUNT) {
724                 /* Store the first HW counter ID for this table */
725                 if (!ulp_fc_mgr_start_idx_isset(ulp_ctxt, params->direction))
726                         ulp_fc_mgr_start_idx_set(ulp_ctxt, params->direction,
727                                                  params->resource_hndl);
728
729                 ulp_fc_mgr_cntr_set(ulp_ctxt, params->direction,
730                                     params->resource_hndl);
731
732                 if (!ulp_fc_mgr_thread_isstarted(ulp_ctxt))
733                         ulp_fc_mgr_thread_start(ulp_ctxt);
734         }
735
736         /* all good, return success */
737         return 0;
738 }
739
740 /*
741  * Free the flow database entry.
742  * The params->critical_resource has to be set to 1 to free the first resource.
743  *
744  * ulp_ctxt [in] Ptr to ulp_context
745  * flow_type [in] Specify it is regular or default flow
746  * fid [in] The index to the flow entry
747  * params [in/out] The contents to be copied into params.
748  * Onlythe critical_resource needs to be set by the caller.
749  *
750  * Returns 0 on success and negative on failure.
751  */
752 int32_t
753 ulp_flow_db_resource_del(struct bnxt_ulp_context *ulp_ctxt,
754                          enum bnxt_ulp_fdb_type flow_type,
755                          uint32_t fid,
756                          struct ulp_flow_db_res_params *params)
757 {
758         struct bnxt_ulp_flow_db *flow_db;
759         struct bnxt_ulp_flow_tbl *flow_tbl;
760         struct ulp_fdb_resource_info *nxt_resource, *fid_resource;
761         uint32_t nxt_idx = 0;
762         struct bnxt_tun_cache_entry *tun_tbl;
763         uint8_t tun_idx = 0;
764         int rc;
765
766         flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
767         if (!flow_db) {
768                 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
769                 return -EINVAL;
770         }
771
772         if (flow_type > BNXT_ULP_FDB_TYPE_DEFAULT) {
773                 BNXT_TF_DBG(ERR, "Invalid flow type\n");
774                 return -EINVAL;
775         }
776
777         flow_tbl = &flow_db->flow_tbl;
778         /* check for max flows */
779         if (fid >= flow_tbl->num_flows || !fid) {
780                 BNXT_TF_DBG(ERR, "Invalid flow index %x\n", fid);
781                 return -EINVAL;
782         }
783
784         /* check if the flow is active or not */
785         if (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type, fid)) {
786                 BNXT_TF_DBG(ERR, "flow does not exist\n");
787                 return -EINVAL;
788         }
789
790         fid_resource = &flow_tbl->flow_resources[fid];
791         if (!params->critical_resource) {
792                 /* Not the critical resource so free the resource */
793                 ULP_FLOW_DB_RES_NXT_SET(nxt_idx,
794                                         fid_resource->nxt_resource_idx);
795                 if (!nxt_idx) {
796                         /* reached end of resources */
797                         return -ENOENT;
798                 }
799                 nxt_resource = &flow_tbl->flow_resources[nxt_idx];
800
801                 /* connect the fid resource to the next resource */
802                 ULP_FLOW_DB_RES_NXT_RESET(fid_resource->nxt_resource_idx);
803                 ULP_FLOW_DB_RES_NXT_SET(fid_resource->nxt_resource_idx,
804                                         nxt_resource->nxt_resource_idx);
805
806                 /* update the contents to be given to caller */
807                 ulp_flow_db_res_info_to_params(nxt_resource, params);
808
809                 /* Delete the nxt_resource */
810                 memset(nxt_resource, 0, sizeof(struct ulp_fdb_resource_info));
811
812                 /* add it to the free list */
813                 flow_tbl->tail_index++;
814                 if (flow_tbl->tail_index >= flow_tbl->num_resources) {
815                         BNXT_TF_DBG(ERR, "FlowDB:Tail reached max\n");
816                         return -ENOENT;
817                 }
818                 flow_tbl->flow_tbl_stack[flow_tbl->tail_index] = nxt_idx;
819
820         } else {
821                 /* Critical resource. copy the contents and exit */
822                 ulp_flow_db_res_info_to_params(fid_resource, params);
823                 ULP_FLOW_DB_RES_NXT_SET(nxt_idx,
824                                         fid_resource->nxt_resource_idx);
825                 memset(fid_resource, 0, sizeof(struct ulp_fdb_resource_info));
826                 ULP_FLOW_DB_RES_NXT_SET(fid_resource->nxt_resource_idx,
827                                         nxt_idx);
828         }
829
830         /* Now that the HW Flow counter resource is deleted, reset it's
831          * corresponding slot in the SW accumulation table in the Flow Counter
832          * manager
833          */
834         if (params->resource_type == TF_TBL_TYPE_ACT_STATS_64 &&
835             params->resource_sub_type ==
836             BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TABLE_INT_COUNT) {
837                 ulp_fc_mgr_cntr_reset(ulp_ctxt, params->direction,
838                                       params->resource_hndl);
839         }
840
841         if (params->resource_func == BNXT_ULP_RESOURCE_FUNC_PARENT_FLOW) {
842                 tun_tbl = bnxt_ulp_cntxt_ptr2_tun_tbl_get(ulp_ctxt);
843                 if (!tun_tbl)
844                         return -EINVAL;
845
846                 rc = ulp_flow_db_parent_tun_idx_get(ulp_ctxt, fid, &tun_idx);
847                 if (rc)
848                         return rc;
849
850                 ulp_clear_tun_entry(tun_tbl, tun_idx);
851         }
852
853         /* all good, return success */
854         return 0;
855 }
856
857 /*
858  * Free the flow database entry
859  *
860  * ulp_ctxt [in] Ptr to ulp_context
861  * flow_type [in] - specify default or regular
862  * fid [in] The index to the flow entry
863  *
864  * returns 0 on success and negative on failure.
865  */
866 int32_t
867 ulp_flow_db_fid_free(struct bnxt_ulp_context *ulp_ctxt,
868                      enum bnxt_ulp_fdb_type flow_type,
869                      uint32_t fid)
870 {
871         struct bnxt_ulp_flow_db *flow_db;
872         struct bnxt_ulp_flow_tbl *flow_tbl;
873
874         flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
875         if (!flow_db) {
876                 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
877                 return -EINVAL;
878         }
879
880         if (flow_type > BNXT_ULP_FDB_TYPE_DEFAULT) {
881                 BNXT_TF_DBG(ERR, "Invalid flow type\n");
882                 return -EINVAL;
883         }
884
885         flow_tbl = &flow_db->flow_tbl;
886
887         /* check for limits of fid */
888         if (fid >= flow_tbl->num_flows || !fid) {
889                 BNXT_TF_DBG(ERR, "Invalid flow index\n");
890                 return -EINVAL;
891         }
892
893         /* check if the flow is active or not */
894         if (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type, fid)) {
895                 BNXT_TF_DBG(ERR, "flow does not exist\n");
896                 return -EINVAL;
897         }
898         flow_tbl->head_index--;
899         if (!flow_tbl->head_index) {
900                 BNXT_TF_DBG(ERR, "FlowDB: Head Ptr is zero\n");
901                 return -ENOENT;
902         }
903         flow_tbl->flow_tbl_stack[flow_tbl->head_index] = fid;
904
905         /* Clear the flows bitmap */
906         ulp_flow_db_active_flows_bit_set(flow_db, flow_type, fid, 0);
907
908         if (flow_type == BNXT_ULP_FDB_TYPE_REGULAR)
909                 ulp_flow_db_func_id_set(flow_db, fid, 0);
910
911         /* all good, return success */
912         return 0;
913 }
914
915 /*
916  * Get the flow database entry details
917  *
918  * ulp_ctxt [in] Ptr to ulp_context
919  * flow_type [in] - specify default or regular
920  * fid [in] The index to the flow entry
921  * nxt_idx [in/out] the index to the next entry
922  * params [out] The contents to be copied into params.
923  *
924  * returns 0 on success and negative on failure.
925  */
926 int32_t
927 ulp_flow_db_resource_get(struct bnxt_ulp_context *ulp_ctxt,
928                          enum bnxt_ulp_fdb_type flow_type,
929                          uint32_t fid,
930                          uint32_t *nxt_idx,
931                          struct ulp_flow_db_res_params *params)
932 {
933         struct bnxt_ulp_flow_db *flow_db;
934         struct bnxt_ulp_flow_tbl *flow_tbl;
935         struct ulp_fdb_resource_info *nxt_resource, *fid_resource;
936
937         flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
938         if (!flow_db) {
939                 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
940                 return -EINVAL;
941         }
942
943         if (flow_type > BNXT_ULP_FDB_TYPE_DEFAULT) {
944                 BNXT_TF_DBG(ERR, "Invalid flow type\n");
945                 return -EINVAL;
946         }
947
948         flow_tbl = &flow_db->flow_tbl;
949
950         /* check for limits of fid */
951         if (fid >= flow_tbl->num_flows || !fid) {
952                 BNXT_TF_DBG(ERR, "Invalid flow index\n");
953                 return -EINVAL;
954         }
955
956         /* check if the flow is active or not */
957         if (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type, fid)) {
958                 BNXT_TF_DBG(ERR, "flow does not exist\n");
959                 return -EINVAL;
960         }
961
962         if (!*nxt_idx) {
963                 fid_resource = &flow_tbl->flow_resources[fid];
964                 ulp_flow_db_res_info_to_params(fid_resource, params);
965                 ULP_FLOW_DB_RES_NXT_SET(*nxt_idx,
966                                         fid_resource->nxt_resource_idx);
967         } else {
968                 nxt_resource = &flow_tbl->flow_resources[*nxt_idx];
969                 ulp_flow_db_res_info_to_params(nxt_resource, params);
970                 *nxt_idx = 0;
971                 ULP_FLOW_DB_RES_NXT_SET(*nxt_idx,
972                                         nxt_resource->nxt_resource_idx);
973         }
974
975         /* all good, return success */
976         return 0;
977 }
978
979 /*
980  * Get the flow database entry iteratively
981  *
982  * flow_tbl [in] Ptr to flow table
983  * flow_type [in] - specify default or regular
984  * fid [in/out] The index to the flow entry
985  *
986  * returns 0 on success and negative on failure.
987  */
988 static int32_t
989 ulp_flow_db_next_entry_get(struct bnxt_ulp_flow_db *flow_db,
990                            enum bnxt_ulp_fdb_type flow_type,
991                            uint32_t *fid)
992 {
993         uint32_t lfid = *fid;
994         uint32_t idx, s_idx, mod_fid;
995         uint64_t bs;
996         uint64_t *active_flows;
997         struct bnxt_ulp_flow_tbl *flowtbl = &flow_db->flow_tbl;
998
999         if (flow_type == BNXT_ULP_FDB_TYPE_REGULAR)
1000                 active_flows = flowtbl->active_reg_flows;
1001         else
1002                 active_flows = flowtbl->active_dflt_flows;
1003
1004         do {
1005                 /* increment the flow id to find the next valid flow id */
1006                 lfid++;
1007                 if (lfid >= flowtbl->num_flows)
1008                         return -ENOENT;
1009                 idx = lfid / ULP_INDEX_BITMAP_SIZE;
1010                 mod_fid = lfid % ULP_INDEX_BITMAP_SIZE;
1011                 s_idx = idx;
1012                 while (!(bs = active_flows[idx])) {
1013                         idx++;
1014                         if ((idx * ULP_INDEX_BITMAP_SIZE) >= flowtbl->num_flows)
1015                                 return -ENOENT;
1016                 }
1017                 /*
1018                  * remove the previous bits in the bitset bs to find the
1019                  * next non zero bit in the bitset. This needs to be done
1020                  * only if the idx is same as he one you started.
1021                  */
1022                 if (s_idx == idx)
1023                         bs &= (-1UL >> mod_fid);
1024                 lfid = (idx * ULP_INDEX_BITMAP_SIZE) + __builtin_clzl(bs);
1025                 if (*fid >= lfid) {
1026                         BNXT_TF_DBG(ERR, "Flow Database is corrupt\n");
1027                         return -ENOENT;
1028                 }
1029         } while (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type,
1030                                                       lfid));
1031
1032         /* all good, return success */
1033         *fid = lfid;
1034         return 0;
1035 }
1036
1037 /*
1038  * Flush all flows in the flow database.
1039  *
1040  * ulp_ctxt [in] Ptr to ulp context
1041  * flow_type [in] - specify default or regular
1042  *
1043  * returns 0 on success or negative number on failure
1044  */
1045 int32_t
1046 ulp_flow_db_flush_flows(struct bnxt_ulp_context *ulp_ctx,
1047                         enum bnxt_ulp_fdb_type flow_type)
1048 {
1049         uint32_t fid = 0;
1050         struct bnxt_ulp_flow_db *flow_db;
1051
1052         if (!ulp_ctx) {
1053                 BNXT_TF_DBG(ERR, "Invalid Argument\n");
1054                 return -EINVAL;
1055         }
1056
1057         flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctx);
1058         if (!flow_db) {
1059                 BNXT_TF_DBG(ERR, "Flow database not found\n");
1060                 return -EINVAL;
1061         }
1062         if (bnxt_ulp_cntxt_acquire_fdb_lock(ulp_ctx)) {
1063                 BNXT_TF_DBG(ERR, "Flow db lock acquire failed\n");
1064                 return -EINVAL;
1065         }
1066
1067         while (!ulp_flow_db_next_entry_get(flow_db, flow_type, &fid))
1068                 ulp_mapper_resources_free(ulp_ctx, flow_type, fid);
1069
1070         bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
1071
1072         return 0;
1073 }
1074
1075 /*
1076  * Flush all flows in the flow database that belong to a device function.
1077  *
1078  * ulp_ctxt [in] Ptr to ulp context
1079  * func_id [in] - The port function id
1080  *
1081  * returns 0 on success or negative number on failure
1082  */
1083 int32_t
1084 ulp_flow_db_function_flow_flush(struct bnxt_ulp_context *ulp_ctx,
1085                                 uint16_t func_id)
1086 {
1087         uint32_t flow_id = 0;
1088         struct bnxt_ulp_flow_db *flow_db;
1089
1090         if (!ulp_ctx || !func_id) {
1091                 BNXT_TF_DBG(ERR, "Invalid Argument\n");
1092                 return -EINVAL;
1093         }
1094
1095         flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctx);
1096         if (!flow_db) {
1097                 BNXT_TF_DBG(ERR, "Flow database not found\n");
1098                 return -EINVAL;
1099         }
1100         if (bnxt_ulp_cntxt_acquire_fdb_lock(ulp_ctx)) {
1101                 BNXT_TF_DBG(ERR, "Flow db lock acquire failed\n");
1102                 return -EINVAL;
1103         }
1104
1105         while (!ulp_flow_db_next_entry_get(flow_db, BNXT_ULP_FDB_TYPE_REGULAR,
1106                                            &flow_id)) {
1107                 if (flow_db->func_id_tbl[flow_id] == func_id)
1108                         ulp_mapper_resources_free(ulp_ctx,
1109                                                   BNXT_ULP_FDB_TYPE_REGULAR,
1110                                                   flow_id);
1111         }
1112         bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
1113         return 0;
1114 }
1115
1116 /*
1117  * Flush all flows in the flow database that are associated with the session.
1118  *
1119  * ulp_ctxt [in] Ptr to ulp context
1120  *
1121  * returns 0 on success or negative number on failure
1122  */
1123 int32_t
1124 ulp_flow_db_session_flow_flush(struct bnxt_ulp_context *ulp_ctx)
1125 {
1126         /*
1127          * TBD: Tf core implementation of FW session flush shall change this
1128          * implementation.
1129          */
1130         return ulp_flow_db_flush_flows(ulp_ctx, BNXT_ULP_FDB_TYPE_REGULAR);
1131 }
1132
1133 /*
1134  * Check that flow id matches the function id or not
1135  *
1136  * ulp_ctxt [in] Ptr to ulp context
1137  * flow_db [in] Ptr to flow table
1138  * func_id [in] The func_id to be set, for reset pass zero.
1139  *
1140  * returns true on success or false on failure
1141  */
1142 bool
1143 ulp_flow_db_validate_flow_func(struct bnxt_ulp_context *ulp_ctx,
1144                                uint32_t flow_id,
1145                                uint32_t func_id)
1146 {
1147         struct bnxt_ulp_flow_db *flow_db;
1148
1149         flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctx);
1150         if (!flow_db) {
1151                 BNXT_TF_DBG(ERR, "Flow database not found\n");
1152                 return false;
1153         }
1154
1155         /* set the function id in the function table */
1156         if (flow_id < flow_db->func_id_tbl_size && func_id &&
1157             flow_db->func_id_tbl[flow_id] == func_id)
1158                 return true;
1159
1160         return false;
1161 }
1162
1163 /*
1164  * Internal api to traverse the resource list within a flow
1165  * and match a resource based on resource func and resource
1166  * sub type. This api should be used only for resources that
1167  * are unique and do not have multiple instances of resource
1168  * func and sub type combination since it will return only
1169  * the first match.
1170  */
1171 static int32_t
1172 ulp_flow_db_resource_params_get(struct bnxt_ulp_context *ulp_ctx,
1173                                 enum bnxt_ulp_fdb_type flow_type,
1174                                 uint32_t flow_id,
1175                                 uint32_t resource_func,
1176                                 uint32_t res_subtype,
1177                                 struct ulp_flow_db_res_params *params)
1178 {
1179         struct bnxt_ulp_flow_db *flow_db;
1180         struct bnxt_ulp_flow_tbl *flow_tbl;
1181         struct ulp_fdb_resource_info *fid_res;
1182         uint32_t res_id;
1183
1184         flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctx);
1185         if (!flow_db) {
1186                 BNXT_TF_DBG(ERR, "Flow database not found\n");
1187                 return -EINVAL;
1188         }
1189
1190         if (!params) {
1191                 BNXT_TF_DBG(ERR, "invalid argument\n");
1192                 return -EINVAL;
1193         }
1194
1195         if (flow_type > BNXT_ULP_FDB_TYPE_DEFAULT) {
1196                 BNXT_TF_DBG(ERR, "Invalid flow type\n");
1197                 return -EINVAL;
1198         }
1199
1200         flow_tbl = &flow_db->flow_tbl;
1201
1202         /* check for limits of fid */
1203         if (flow_id >= flow_tbl->num_flows || !flow_id) {
1204                 BNXT_TF_DBG(ERR, "Invalid flow index\n");
1205                 return -EINVAL;
1206         }
1207
1208         /* check if the flow is active or not */
1209         if (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type, flow_id)) {
1210                 BNXT_TF_DBG(ERR, "flow does not exist\n");
1211                 return -EINVAL;
1212         }
1213         /* Iterate the resource to get the resource handle */
1214         res_id =  flow_id;
1215         memset(params, 0, sizeof(struct ulp_flow_db_res_params));
1216         while (res_id) {
1217                 fid_res = &flow_tbl->flow_resources[res_id];
1218                 if (ulp_flow_db_resource_func_get(fid_res) == resource_func) {
1219                         if (resource_func & ULP_FLOW_DB_RES_FUNC_NEED_LOWER) {
1220                                 if (res_subtype == fid_res->resource_sub_type) {
1221                                         ulp_flow_db_res_info_to_params(fid_res,
1222                                                                        params);
1223                                         return 0;
1224                                 }
1225
1226                         } else if (resource_func ==
1227                                    BNXT_ULP_RESOURCE_FUNC_EXT_EM_TABLE ||
1228                                    resource_func ==
1229                                    BNXT_ULP_RESOURCE_FUNC_INT_EM_TABLE) {
1230                                 ulp_flow_db_res_info_to_params(fid_res,
1231                                                                params);
1232                                 return 0;
1233                         }
1234                 }
1235                 res_id = 0;
1236                 ULP_FLOW_DB_RES_NXT_SET(res_id, fid_res->nxt_resource_idx);
1237         }
1238         return -ENOENT;
1239 }
1240
1241 /*
1242  * Api to get the cfa action pointer from a flow.
1243  *
1244  * ulp_ctxt [in] Ptr to ulp context
1245  * flow_id [in] flow id
1246  * cfa_action [out] The resource handle stored in the flow database
1247  *
1248  * returns 0 on success
1249  */
1250 int32_t
1251 ulp_default_flow_db_cfa_action_get(struct bnxt_ulp_context *ulp_ctx,
1252                                    uint32_t flow_id,
1253                                    uint16_t *cfa_action)
1254 {
1255         uint8_t sub_typ = BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TABLE_VFR_CFA_ACTION;
1256         struct ulp_flow_db_res_params params;
1257         int32_t rc;
1258
1259         rc = ulp_flow_db_resource_params_get(ulp_ctx,
1260                                              BNXT_ULP_FDB_TYPE_DEFAULT,
1261                                              flow_id,
1262                                              BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE,
1263                                              sub_typ, &params);
1264         if (rc) {
1265                 BNXT_TF_DBG(ERR, "CFA Action ptr not found for flow id %u\n",
1266                             flow_id);
1267                 return -ENOENT;
1268         }
1269         *cfa_action = params.resource_hndl;
1270         return 0;
1271 }
1272
1273 /*
1274  * Allocate the entry in the parent-child database
1275  *
1276  * ulp_ctxt [in] Ptr to ulp_context
1277  * fid [in] The flow id to the flow entry
1278  *
1279  * returns index on success and negative on failure.
1280  */
1281 int32_t
1282 ulp_flow_db_parent_flow_alloc(struct bnxt_ulp_context *ulp_ctxt,
1283                               uint32_t fid)
1284 {
1285         struct bnxt_ulp_flow_db *flow_db;
1286         struct ulp_fdb_parent_child_db *p_pdb;
1287         uint32_t idx, free_idx = 0;
1288
1289         /* validate the arguments */
1290         flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, fid);
1291         if (!flow_db) {
1292                 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1293                 return -EINVAL;
1294         }
1295
1296         p_pdb = &flow_db->parent_child_db;
1297         for (idx = 0; idx < p_pdb->entries_count; idx++) {
1298                 if (p_pdb->parent_flow_tbl[idx].parent_fid == fid) {
1299                         BNXT_TF_DBG(ERR, "fid is already allocated\n");
1300                         return -EINVAL;
1301                 }
1302                 if (!p_pdb->parent_flow_tbl[idx].parent_fid && !free_idx)
1303                         free_idx = idx + 1;
1304         }
1305         /* no free slots */
1306         if (!free_idx) {
1307                 BNXT_TF_DBG(ERR, "parent child db is full\n");
1308                 return -ENOMEM;
1309         }
1310
1311         free_idx -= 1;
1312         /* set the Fid in the parent child */
1313         p_pdb->parent_flow_tbl[free_idx].parent_fid = fid;
1314         return free_idx;
1315 }
1316
1317 /*
1318  * Free the entry in the parent-child database
1319  *
1320  * ulp_ctxt [in] Ptr to ulp_context
1321  * fid [in] The flow id to the flow entry
1322  *
1323  * returns 0 on success and negative on failure.
1324  */
1325 int32_t
1326 ulp_flow_db_parent_flow_free(struct bnxt_ulp_context *ulp_ctxt,
1327                              uint32_t fid)
1328 {
1329         struct bnxt_ulp_flow_db *flow_db;
1330         struct ulp_fdb_parent_child_db *p_pdb;
1331         uint32_t idx;
1332
1333         /* validate the arguments */
1334         flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, fid);
1335         if (!flow_db) {
1336                 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1337                 return -EINVAL;
1338         }
1339
1340         p_pdb = &flow_db->parent_child_db;
1341         for (idx = 0; idx < p_pdb->entries_count; idx++) {
1342                 if (p_pdb->parent_flow_tbl[idx].parent_fid == fid) {
1343                         /* free the contents */
1344                         p_pdb->parent_flow_tbl[idx].parent_fid = 0;
1345                         memset(p_pdb->parent_flow_tbl[idx].child_fid_bitset,
1346                                0, p_pdb->child_bitset_size);
1347                         return 0;
1348                 }
1349         }
1350         BNXT_TF_DBG(ERR, "parent entry not found = %x\n", fid);
1351         return -EINVAL;
1352 }
1353
1354 /*
1355  * Set or reset the child flow in the parent-child database
1356  *
1357  * ulp_ctxt [in] Ptr to ulp_context
1358  * parent_fid [in] The flow id of the parent flow entry
1359  * child_fid [in] The flow id of the child flow entry
1360  * set_flag [in] Use 1 for setting child, 0 to reset
1361  *
1362  * returns zero on success and negative on failure.
1363  */
1364 int32_t
1365 ulp_flow_db_parent_child_flow_set(struct bnxt_ulp_context *ulp_ctxt,
1366                                   uint32_t parent_fid,
1367                                   uint32_t child_fid,
1368                                   uint32_t set_flag)
1369 {
1370         struct bnxt_ulp_flow_db *flow_db;
1371         struct ulp_fdb_parent_child_db *p_pdb;
1372         uint32_t idx, a_idx;
1373         uint64_t *t;
1374
1375         /* validate the arguments */
1376         flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, parent_fid);
1377         if (!flow_db) {
1378                 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1379                 return -EINVAL;
1380         }
1381
1382         /* check for fid validity */
1383         if (child_fid >= flow_db->flow_tbl.num_flows || !child_fid) {
1384                 BNXT_TF_DBG(ERR, "Invalid child flow index %x\n", child_fid);
1385                 return -EINVAL;
1386         }
1387
1388         p_pdb = &flow_db->parent_child_db;
1389         a_idx = child_fid / ULP_INDEX_BITMAP_SIZE;
1390         for (idx = 0; idx < p_pdb->entries_count; idx++) {
1391                 if (p_pdb->parent_flow_tbl[idx].parent_fid == parent_fid) {
1392                         t = p_pdb->parent_flow_tbl[idx].child_fid_bitset;
1393                         if (set_flag)
1394                                 ULP_INDEX_BITMAP_SET(t[a_idx], child_fid);
1395                         else
1396                                 ULP_INDEX_BITMAP_RESET(t[a_idx], child_fid);
1397                         return 0;
1398                 }
1399         }
1400         BNXT_TF_DBG(ERR, "Unable to set the parent-child flow %x:%x\n",
1401                     parent_fid, child_fid);
1402         return -1;
1403 }
1404
1405 /*
1406  * Get the parent index from the parent-child database
1407  *
1408  * ulp_ctxt [in] Ptr to ulp_context
1409  * parent_fid [in] The flow id of the parent flow entry
1410  * parent_idx [out] The parent index of parent flow entry
1411  *
1412  * returns zero on success and negative on failure.
1413  */
1414 int32_t
1415 ulp_flow_db_parent_flow_idx_get(struct bnxt_ulp_context *ulp_ctxt,
1416                                 uint32_t parent_fid,
1417                                 uint32_t *parent_idx)
1418 {
1419         struct bnxt_ulp_flow_db *flow_db;
1420         struct ulp_fdb_parent_child_db *p_pdb;
1421         uint32_t idx;
1422
1423         /* validate the arguments */
1424         flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, parent_fid);
1425         if (!flow_db) {
1426                 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1427                 return -EINVAL;
1428         }
1429
1430         p_pdb = &flow_db->parent_child_db;
1431         for (idx = 0; idx < p_pdb->entries_count; idx++) {
1432                 if (p_pdb->parent_flow_tbl[idx].parent_fid == parent_fid) {
1433                         *parent_idx = idx;
1434                         return 0;
1435                 }
1436         }
1437         BNXT_TF_DBG(ERR, "Unable to get the parent flow %x\n", parent_fid);
1438         return -1;
1439 }
1440
1441 /*
1442  * Get the next child flow in the parent-child database
1443  *
1444  * ulp_ctxt [in] Ptr to ulp_context
1445  * parent_fid [in] The flow id of the parent flow entry
1446  * child_fid [in/out] The flow id of the child flow entry
1447  *
1448  * returns zero on success and negative on failure.
1449  * Pass child_fid as zero for first entry.
1450  */
1451 int32_t
1452 ulp_flow_db_parent_child_flow_next_entry_get(struct bnxt_ulp_flow_db *flow_db,
1453                                              uint32_t parent_idx,
1454                                              uint32_t *child_fid)
1455 {
1456         struct ulp_fdb_parent_child_db *p_pdb;
1457         uint32_t idx, s_idx, mod_fid;
1458         uint32_t next_fid = *child_fid;
1459         uint64_t *child_bitset;
1460         uint64_t bs;
1461
1462         /* check for fid validity */
1463         p_pdb = &flow_db->parent_child_db;
1464         if (parent_idx >= p_pdb->entries_count ||
1465             !p_pdb->parent_flow_tbl[parent_idx].parent_fid) {
1466                 BNXT_TF_DBG(ERR, "Invalid parent flow index %x\n", parent_idx);
1467                 return -EINVAL;
1468         }
1469
1470         child_bitset = p_pdb->parent_flow_tbl[parent_idx].child_fid_bitset;
1471         do {
1472                 /* increment the flow id to find the next valid flow id */
1473                 next_fid++;
1474                 if (next_fid >= flow_db->flow_tbl.num_flows)
1475                         return -ENOENT;
1476                 idx = next_fid / ULP_INDEX_BITMAP_SIZE;
1477                 mod_fid = next_fid % ULP_INDEX_BITMAP_SIZE;
1478                 s_idx = idx;
1479                 while (!(bs = child_bitset[idx])) {
1480                         idx++;
1481                         if ((idx * ULP_INDEX_BITMAP_SIZE) >=
1482                             flow_db->flow_tbl.num_flows)
1483                                 return -ENOENT;
1484                 }
1485                 /*
1486                  * remove the previous bits in the bitset bs to find the
1487                  * next non zero bit in the bitset. This needs to be done
1488                  * only if the idx is same as he one you started.
1489                  */
1490                 if (s_idx == idx)
1491                         bs &= (-1UL >> mod_fid);
1492                 next_fid = (idx * ULP_INDEX_BITMAP_SIZE) + __builtin_clzl(bs);
1493                 if (*child_fid >= next_fid) {
1494                         BNXT_TF_DBG(ERR, "Parent Child Database is corrupt\n");
1495                         return -ENOENT;
1496                 }
1497                 idx = next_fid / ULP_INDEX_BITMAP_SIZE;
1498         } while (!ULP_INDEX_BITMAP_GET(child_bitset[idx], next_fid));
1499         *child_fid = next_fid;
1500         return 0;
1501 }
1502
1503 /*
1504  * Set the counter accumulation in the parent flow
1505  *
1506  * ulp_ctxt [in] Ptr to ulp_context
1507  * parent_idx [in] The parent index of the parent flow entry
1508  *
1509  * returns index on success and negative on failure.
1510  */
1511 static int32_t
1512 ulp_flow_db_parent_flow_count_accum_set(struct bnxt_ulp_context *ulp_ctxt,
1513                                         uint32_t parent_idx)
1514 {
1515         struct bnxt_ulp_flow_db *flow_db;
1516         struct ulp_fdb_parent_child_db *p_pdb;
1517
1518         flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
1519         if (!flow_db) {
1520                 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
1521                 return -EINVAL;
1522         }
1523
1524         /* check for parent idx validity */
1525         p_pdb = &flow_db->parent_child_db;
1526         if (parent_idx >= p_pdb->entries_count ||
1527             !p_pdb->parent_flow_tbl[parent_idx].parent_fid) {
1528                 BNXT_TF_DBG(ERR, "Invalid parent flow index %x\n", parent_idx);
1529                 return -EINVAL;
1530         }
1531
1532         p_pdb->parent_flow_tbl[parent_idx].counter_acc = 1;
1533         return 0;
1534 }
1535
1536 /*
1537  * Get the counter accumulation in the parent flow
1538  *
1539  * ulp_ctxt [in] Ptr to ulp_context
1540  * parent_fid [in] The flow id of the parent flow entry
1541  *
1542  * returns 0 if counter accum is set else -1.
1543  */
1544 static int32_t
1545 ulp_flow_db_parent_flow_count_accum_get(struct bnxt_ulp_context *ulp_ctxt,
1546                                         uint32_t parent_fid)
1547 {
1548         struct bnxt_ulp_flow_db *flow_db;
1549         struct ulp_fdb_parent_child_db *p_pdb;
1550         uint32_t idx;
1551
1552         /* validate the arguments */
1553         flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, parent_fid);
1554         if (!flow_db) {
1555                 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1556                 return -EINVAL;
1557         }
1558
1559         p_pdb = &flow_db->parent_child_db;
1560         for (idx = 0; idx < p_pdb->entries_count; idx++) {
1561                 if (p_pdb->parent_flow_tbl[idx].parent_fid == parent_fid) {
1562                         if (p_pdb->parent_flow_tbl[idx].counter_acc)
1563                                 return 0;
1564                         break;
1565                 }
1566         }
1567         return -1;
1568 }
1569
1570 /*
1571  * Orphan the child flow entry
1572  * This is called only for child flows that have
1573  * BNXT_ULP_RESOURCE_FUNC_CHILD_FLOW resource
1574  *
1575  * ulp_ctxt [in] Ptr to ulp_context
1576  * flow_type [in] Specify it is regular or default flow
1577  * fid [in] The index to the flow entry
1578  *
1579  * Returns 0 on success and negative on failure.
1580  */
1581 int32_t
1582 ulp_flow_db_child_flow_reset(struct bnxt_ulp_context *ulp_ctxt,
1583                              enum bnxt_ulp_fdb_type flow_type,
1584                              uint32_t fid)
1585 {
1586         struct bnxt_ulp_flow_db *flow_db;
1587         struct bnxt_ulp_flow_tbl *flow_tbl;
1588         struct ulp_fdb_resource_info *fid_res;
1589         uint32_t res_id = 0;
1590
1591         flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
1592         if (!flow_db) {
1593                 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
1594                 return -EINVAL;
1595         }
1596
1597         if (flow_type > BNXT_ULP_FDB_TYPE_DEFAULT) {
1598                 BNXT_TF_DBG(ERR, "Invalid flow type\n");
1599                 return -EINVAL;
1600         }
1601
1602         flow_tbl = &flow_db->flow_tbl;
1603         /* check for max flows */
1604         if (fid >= flow_tbl->num_flows || !fid) {
1605                 BNXT_TF_DBG(ERR, "Invalid flow index %x\n", fid);
1606                 return -EINVAL;
1607         }
1608
1609         /* check if the flow is active or not */
1610         if (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type, fid)) {
1611                 BNXT_TF_DBG(ERR, "flow does not exist\n");
1612                 return -EINVAL;
1613         }
1614
1615         /* Iterate the resource to get the resource handle */
1616         res_id =  fid;
1617         while (res_id) {
1618                 fid_res = &flow_tbl->flow_resources[res_id];
1619                 if (ulp_flow_db_resource_func_get(fid_res) ==
1620                     BNXT_ULP_RESOURCE_FUNC_CHILD_FLOW) {
1621                         /* invalidate the resource details */
1622                         fid_res->resource_hndl = 0;
1623                         return 0;
1624                 }
1625                 res_id = 0;
1626                 ULP_FLOW_DB_RES_NXT_SET(res_id, fid_res->nxt_resource_idx);
1627         }
1628         /* failed */
1629         return -1;
1630 }
1631
1632 /*
1633  * Create parent flow in the parent flow tbl
1634  *
1635  * parms [in] Ptr to mapper params
1636  *
1637  * Returns 0 on success and negative on failure.
1638  */
1639 int32_t
1640 ulp_flow_db_parent_flow_create(struct bnxt_ulp_mapper_parms *parms)
1641 {
1642         struct ulp_flow_db_res_params fid_parms;
1643         uint32_t sub_typ = BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TABLE_INT_COUNT_ACC;
1644         struct ulp_flow_db_res_params res_params;
1645         int32_t fid_idx, rc;
1646
1647         /* create the child flow entry in parent flow table */
1648         fid_idx = ulp_flow_db_parent_flow_alloc(parms->ulp_ctx, parms->fid);
1649         if (fid_idx < 0) {
1650                 BNXT_TF_DBG(ERR, "Error in creating parent flow fid %x\n",
1651                             parms->fid);
1652                 return -1;
1653         }
1654
1655         /* Add the parent details in the resource list of the flow */
1656         memset(&fid_parms, 0, sizeof(fid_parms));
1657         fid_parms.resource_func = BNXT_ULP_RESOURCE_FUNC_PARENT_FLOW;
1658         fid_parms.resource_hndl = fid_idx;
1659         fid_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
1660         if (ulp_flow_db_resource_add(parms->ulp_ctx, BNXT_ULP_FDB_TYPE_REGULAR,
1661                                      parms->fid, &fid_parms)) {
1662                 BNXT_TF_DBG(ERR, "Error in adding flow res for fid %x\n",
1663                             parms->fid);
1664                 return -1;
1665         }
1666
1667         /* check of the flow has internal counter accumulation enabled */
1668         if (!ulp_flow_db_resource_params_get(parms->ulp_ctx,
1669                                              BNXT_ULP_FDB_TYPE_REGULAR,
1670                                              parms->fid,
1671                                              BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE,
1672                                              sub_typ,
1673                                              &res_params)) {
1674                 /* Enable the counter accumulation in parent entry */
1675                 if (ulp_flow_db_parent_flow_count_accum_set(parms->ulp_ctx,
1676                                                             fid_idx)) {
1677                         BNXT_TF_DBG(ERR, "Error in setting counter acc %x\n",
1678                                     parms->fid);
1679                         return -1;
1680                 }
1681         }
1682
1683         rc  = ulp_flow_db_parent_tun_idx_set(parms->ulp_ctx, fid_idx,
1684                                              parms->tun_idx);
1685         if (rc) {
1686                 BNXT_TF_DBG(ERR, "Error setting tun_idx in the parent flow\n");
1687                 return rc;
1688         }
1689
1690         return 0;
1691 }
1692
1693 /*
1694  * Create child flow in the parent flow tbl
1695  *
1696  * parms [in] Ptr to mapper params
1697  *
1698  * Returns 0 on success and negative on failure.
1699  */
1700 int32_t
1701 ulp_flow_db_child_flow_create(struct bnxt_ulp_mapper_parms *parms)
1702 {
1703         struct ulp_flow_db_res_params fid_parms;
1704         uint32_t sub_type = BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TABLE_INT_COUNT;
1705         enum bnxt_ulp_resource_func res_fun;
1706         struct ulp_flow_db_res_params res_p;
1707         uint32_t parent_fid = parms->parent_fid;
1708         int32_t rc;
1709
1710         /* create the parent flow entry in parent flow table */
1711         rc = ulp_flow_db_parent_child_flow_set(parms->ulp_ctx,
1712                                                parms->parent_fid,
1713                                                parms->fid, 1);
1714         if (rc) {
1715                 BNXT_TF_DBG(ERR, "Error in setting child fid %x\n", parms->fid);
1716                 return rc;
1717         }
1718
1719         /* Add the parent details in the resource list of the flow */
1720         memset(&fid_parms, 0, sizeof(fid_parms));
1721         fid_parms.resource_func = BNXT_ULP_RESOURCE_FUNC_CHILD_FLOW;
1722         fid_parms.resource_hndl = parms->parent_fid;
1723         fid_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
1724         rc  = ulp_flow_db_resource_add(parms->ulp_ctx,
1725                                        BNXT_ULP_FDB_TYPE_REGULAR,
1726                                        parms->fid, &fid_parms);
1727         if (rc) {
1728                 BNXT_TF_DBG(ERR, "Error in adding flow res for fid %x\n",
1729                             parms->fid);
1730                 return rc;
1731         }
1732
1733         /* check if accumulation count is set for parent flow */
1734         rc = ulp_flow_db_parent_flow_count_accum_get(parms->ulp_ctx,
1735                                                      parms->parent_fid);
1736         if (!rc) {
1737                 /* check if internal count action included for this flow.*/
1738                 res_fun = BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE;
1739                 rc = ulp_flow_db_resource_params_get(parms->ulp_ctx,
1740                                                      BNXT_ULP_FDB_TYPE_REGULAR,
1741                                                      parms->fid,
1742                                                      res_fun,
1743                                                      sub_type,
1744                                                      &res_p);
1745                 if (!rc) {
1746                         /* update the counter manager to include parent fid */
1747                         if (ulp_fc_mgr_cntr_parent_flow_set(parms->ulp_ctx,
1748                                                             res_p.direction,
1749                                                             res_p.resource_hndl,
1750                                                             parent_fid)) {
1751                                 BNXT_TF_DBG(ERR, "Error in setting child %x\n",
1752                                             parms->fid);
1753                                 return -1;
1754                         }
1755                 }
1756         }
1757         /* return success */
1758         return 0;
1759 }
1760
1761 /*
1762  * Update the parent counters
1763  *
1764  * ulp_ctxt [in] Ptr to ulp_context
1765  * parent_fid [in] The flow id of the parent flow entry
1766  * packet_count [in] - packet count
1767  * byte_count [in] - byte count
1768  *
1769  * returns 0 on success
1770  */
1771 int32_t
1772 ulp_flow_db_parent_flow_count_update(struct bnxt_ulp_context *ulp_ctxt,
1773                                      uint32_t parent_fid,
1774                                      uint64_t packet_count,
1775                                      uint64_t byte_count)
1776 {
1777         struct bnxt_ulp_flow_db *flow_db;
1778         struct ulp_fdb_parent_child_db *p_pdb;
1779         uint32_t idx;
1780
1781         /* validate the arguments */
1782         flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, parent_fid);
1783         if (!flow_db) {
1784                 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1785                 return -EINVAL;
1786         }
1787
1788         p_pdb = &flow_db->parent_child_db;
1789         for (idx = 0; idx < p_pdb->entries_count; idx++) {
1790                 if (p_pdb->parent_flow_tbl[idx].parent_fid == parent_fid) {
1791                         if (p_pdb->parent_flow_tbl[idx].counter_acc) {
1792                                 p_pdb->parent_flow_tbl[idx].pkt_count +=
1793                                         packet_count;
1794                                 p_pdb->parent_flow_tbl[idx].byte_count +=
1795                                         byte_count;
1796                         }
1797                         return 0;
1798                 }
1799         }
1800         return -ENOENT;
1801 }
1802
1803 /*
1804  * Get the parent accumulation counters
1805  *
1806  * ulp_ctxt [in] Ptr to ulp_context
1807  * parent_fid [in] The flow id of the parent flow entry
1808  * packet_count [out] - packet count
1809  * byte_count [out] - byte count
1810  *
1811  * returns 0 on success
1812  */
1813 int32_t
1814 ulp_flow_db_parent_flow_count_get(struct bnxt_ulp_context *ulp_ctxt,
1815                                   uint32_t parent_fid,
1816                                   uint64_t *packet_count,
1817                                   uint64_t *byte_count)
1818 {
1819         struct bnxt_ulp_flow_db *flow_db;
1820         struct ulp_fdb_parent_child_db *p_pdb;
1821         uint32_t idx;
1822
1823         /* validate the arguments */
1824         flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, parent_fid);
1825         if (!flow_db) {
1826                 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1827                 return -EINVAL;
1828         }
1829
1830         p_pdb = &flow_db->parent_child_db;
1831         for (idx = 0; idx < p_pdb->entries_count; idx++) {
1832                 if (p_pdb->parent_flow_tbl[idx].parent_fid == parent_fid) {
1833                         if (p_pdb->parent_flow_tbl[idx].counter_acc) {
1834                                 *packet_count =
1835                                         p_pdb->parent_flow_tbl[idx].pkt_count;
1836                                 *byte_count =
1837                                         p_pdb->parent_flow_tbl[idx].byte_count;
1838                         }
1839                         return 0;
1840                 }
1841         }
1842         return -ENOENT;
1843 }
1844
1845 /*
1846  * reset the parent accumulation counters
1847  *
1848  * ulp_ctxt [in] Ptr to ulp_context
1849  *
1850  * returns none
1851  */
1852 void
1853 ulp_flow_db_parent_flow_count_reset(struct bnxt_ulp_context *ulp_ctxt)
1854 {
1855         struct bnxt_ulp_flow_db *flow_db;
1856         struct ulp_fdb_parent_child_db *p_pdb;
1857         uint32_t idx;
1858
1859         /* validate the arguments */
1860         flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, 1);
1861         if (!flow_db) {
1862                 BNXT_TF_DBG(ERR, "parent child db validation failed\n");
1863                 return;
1864         }
1865
1866         p_pdb = &flow_db->parent_child_db;
1867         for (idx = 0; idx < p_pdb->entries_count; idx++) {
1868                 if (p_pdb->parent_flow_tbl[idx].parent_fid &&
1869                     p_pdb->parent_flow_tbl[idx].counter_acc) {
1870                         p_pdb->parent_flow_tbl[idx].pkt_count = 0;
1871                         p_pdb->parent_flow_tbl[idx].byte_count = 0;
1872                 }
1873         }
1874 }