File: ulp_flow_db.c

package info (click to toggle)
dpdk 25.11-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 127,892 kB
  • sloc: ansic: 2,358,479; python: 16,426; sh: 4,474; makefile: 1,713; awk: 70
file content (1850 lines) | stat: -rw-r--r-- 51,330 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2014-2023 Broadcom
 * All rights reserved.
 */

#include <rte_bitops.h>
#include <rte_malloc.h>

#include "bnxt.h"
#include "bnxt_tf_common.h"
#include "bnxt_ulp_utils.h"
#include "bnxt_ulp_tfc.h"
#include "ulp_utils.h"
#include "ulp_template_struct.h"
#include "ulp_mapper.h"
#include "ulp_flow_db.h"
#include "ulp_fc_mgr.h"
#include "ulp_sc_mgr.h"
#include "ulp_tun.h"
#ifdef TF_FLOW_SCALE_QUERY
#include "tf_resources.h"
#include "tfc_resources.h"
#endif /* TF_FLOW_SCALE_QUERY */

#define ULP_FLOW_DB_RES_DIR_BIT		31
#define ULP_FLOW_DB_RES_DIR_MASK	0x80000000
#define ULP_FLOW_DB_RES_NXT_MASK	0x7FFFFFFF

/* Macro to copy the nxt_resource_idx */
#define ULP_FLOW_DB_RES_NXT_SET(dst, src)	{(dst) |= ((src) &\
					 ULP_FLOW_DB_RES_NXT_MASK); }
#define ULP_FLOW_DB_RES_NXT_RESET(dst)	((dst) &= ~(ULP_FLOW_DB_RES_NXT_MASK))

/*
 * Helper function to set the bit in the active flows
 * No validation is done in this function.
 *
 * flow_db [in] Ptr to flow database
 * flow_type [in] - specify default or regular
 * idx [in] The index to bit to be set or reset.
 * flag [in] 1 to set and 0 to reset.
 *
 * returns none
 */
static void
ulp_flow_db_active_flows_bit_set(struct bnxt_ulp_flow_db *flow_db,
				 enum bnxt_ulp_fdb_type flow_type,
				 uint32_t idx,
				 uint32_t flag)
{
	struct bnxt_ulp_flow_tbl *f_tbl = &flow_db->flow_tbl;
	uint32_t a_idx = idx / ULP_INDEX_BITMAP_SIZE;

	if (flag) {
		if (flow_type == BNXT_ULP_FDB_TYPE_REGULAR || flow_type ==
		    BNXT_ULP_FDB_TYPE_RID)
			ULP_INDEX_BITMAP_SET(f_tbl->active_reg_flows[a_idx],
					     idx);
		if (flow_type == BNXT_ULP_FDB_TYPE_DEFAULT || flow_type ==
		    BNXT_ULP_FDB_TYPE_RID)
			ULP_INDEX_BITMAP_SET(f_tbl->active_dflt_flows[a_idx],
					     idx);
	} else {
		if (flow_type == BNXT_ULP_FDB_TYPE_REGULAR || flow_type ==
		    BNXT_ULP_FDB_TYPE_RID)
			ULP_INDEX_BITMAP_RESET(f_tbl->active_reg_flows[a_idx],
					       idx);
		if (flow_type == BNXT_ULP_FDB_TYPE_DEFAULT || flow_type ==
		    BNXT_ULP_FDB_TYPE_RID)
			ULP_INDEX_BITMAP_RESET(f_tbl->active_dflt_flows[a_idx],
					       idx);
	}
}

/*
 * Helper function to check if given fid is active flow.
 * No validation being done in this function.
 *
 * flow_db [in] Ptr to flow database
 * flow_type [in] - specify default or regular
 * idx [in] The index to bit to be set or reset.
 *
 * returns 1 on set or 0 if not set.
 */
static int32_t
ulp_flow_db_active_flows_bit_is_set(struct bnxt_ulp_flow_db *flow_db,
				    enum bnxt_ulp_fdb_type flow_type,
				    uint32_t idx)
{
	struct bnxt_ulp_flow_tbl *f_tbl = &flow_db->flow_tbl;
	uint32_t a_idx = idx / ULP_INDEX_BITMAP_SIZE;
	uint32_t reg, dflt;

	reg = ULP_INDEX_BITMAP_GET(f_tbl->active_reg_flows[a_idx], idx);
	dflt = ULP_INDEX_BITMAP_GET(f_tbl->active_dflt_flows[a_idx], idx);

	switch (flow_type) {
	case BNXT_ULP_FDB_TYPE_REGULAR:
		return (reg && !dflt);
	case BNXT_ULP_FDB_TYPE_DEFAULT:
		return (!reg && dflt);
	case BNXT_ULP_FDB_TYPE_RID:
		return (reg && dflt);
	default:
		return 0;
	}
}

static inline enum tf_dir
ulp_flow_db_resource_dir_get(struct ulp_fdb_resource_info *res_info)
{
	return ((res_info->nxt_resource_idx & ULP_FLOW_DB_RES_DIR_MASK) >>
		ULP_FLOW_DB_RES_DIR_BIT);
}

static inline uint8_t
ulp_flow_db_resource_func_get(struct ulp_fdb_resource_info *res_info)
{
	return res_info->resource_func;
}

/*
 * Helper function to copy the resource params to resource info
 *  No validation being done in this function.
 *
 * resource_info [out] Ptr to resource information
 * params [in] The input params from the caller
 * returns none
 */
static void
ulp_flow_db_res_params_to_info(struct ulp_fdb_resource_info *resource_info,
			       struct ulp_flow_db_res_params *params)
{
	resource_info->nxt_resource_idx |= ((params->direction <<
				      ULP_FLOW_DB_RES_DIR_BIT) &
				     ULP_FLOW_DB_RES_DIR_MASK);
	resource_info->resource_func = params->resource_func;
	resource_info->resource_type = params->resource_type;
	resource_info->resource_sub_type = params->resource_sub_type;
	resource_info->fdb_flags = params->fdb_flags;
	resource_info->resource_hndl = params->resource_hndl;
}

/*
 * Helper function to copy the resource params to resource info
 *  No validation being done in this function.
 *
 * resource_info [in] Ptr to resource information
 * params [out] The output params to the caller
 *
 * returns none
 */
static void
ulp_flow_db_res_info_to_params(struct ulp_fdb_resource_info *resource_info,
			       struct ulp_flow_db_res_params *params)
{
	memset(params, 0, sizeof(struct ulp_flow_db_res_params));
	/* use the helper function to get the resource func */
	params->direction = ulp_flow_db_resource_dir_get(resource_info);
	params->resource_func = ulp_flow_db_resource_func_get(resource_info);
	params->resource_type = resource_info->resource_type;
	params->resource_sub_type = resource_info->resource_sub_type;
	params->fdb_flags = resource_info->fdb_flags;
	params->resource_hndl = resource_info->resource_hndl;
}

/*
 * Helper function to allocate the flow table and initialize
 * the stack for allocation operations.
 *
 * flow_db [in] Ptr to flow database structure
 *
 * Returns 0 on success or negative number on failure.
 */
static int32_t
ulp_flow_db_alloc_resource(struct bnxt_ulp_flow_db *flow_db)
{
	uint32_t			idx = 0;
	struct bnxt_ulp_flow_tbl	*flow_tbl;
	uint32_t			size;

	flow_tbl = &flow_db->flow_tbl;

	size = sizeof(struct ulp_fdb_resource_info) * flow_tbl->num_resources;
	flow_tbl->flow_resources =
			rte_zmalloc("ulp_fdb_resource_info", size, 0);

	if (!flow_tbl->flow_resources) {
		BNXT_DRV_DBG(ERR, "Failed to alloc memory for flow table\n");
		return -ENOMEM;
	}
	size = sizeof(uint32_t) * flow_tbl->num_resources;
	flow_tbl->flow_tbl_stack = rte_zmalloc("flow_tbl_stack", size, 0);
	if (!flow_tbl->flow_tbl_stack) {
		BNXT_DRV_DBG(ERR, "Failed to alloc memory flow tbl stack\n");
		return -ENOMEM;
	}
	size = (flow_tbl->num_flows / sizeof(uint64_t)) + 1;
	size = ULP_BYTE_ROUND_OFF_8(size);
	flow_tbl->active_reg_flows = rte_zmalloc("active reg flows", size,
						 ULP_BUFFER_ALIGN_64_BYTE);
	if (!flow_tbl->active_reg_flows) {
		BNXT_DRV_DBG(ERR, "Failed to alloc memory active reg flows\n");
		return -ENOMEM;
	}

	flow_tbl->active_dflt_flows = rte_zmalloc("active dflt flows", size,
						  ULP_BUFFER_ALIGN_64_BYTE);
	if (!flow_tbl->active_dflt_flows) {
		BNXT_DRV_DBG(ERR, "Failed to alloc memory active dflt flows\n");
		return -ENOMEM;
	}

	/* Initialize the stack table. */
	for (idx = 0; idx < flow_tbl->num_resources; idx++)
		flow_tbl->flow_tbl_stack[idx] = idx;

	/* Ignore the first element in the list. */
	flow_tbl->head_index = 1;
	/* Tail points to the last entry in the list. */
	flow_tbl->tail_index = flow_tbl->num_resources - 1;
	return 0;
}

/*
 * Helper function to deallocate the flow table.
 *
 * flow_db [in] Ptr to flow database structure
 *
 * Returns none.
 */
static void
ulp_flow_db_dealloc_resource(struct bnxt_ulp_flow_db *flow_db)
{
	struct bnxt_ulp_flow_tbl *flow_tbl = &flow_db->flow_tbl;

	/* Free all the allocated tables in the flow table. */
	if (flow_tbl->active_reg_flows) {
		rte_free(flow_tbl->active_reg_flows);
		flow_tbl->active_reg_flows = NULL;
	}
	if (flow_tbl->active_dflt_flows) {
		rte_free(flow_tbl->active_dflt_flows);
		flow_tbl->active_dflt_flows = NULL;
	}

	if (flow_tbl->flow_tbl_stack) {
		rte_free(flow_tbl->flow_tbl_stack);
		flow_tbl->flow_tbl_stack = NULL;
	}

	if (flow_tbl->flow_resources) {
		rte_free(flow_tbl->flow_resources);
		flow_tbl->flow_resources = NULL;
	}
}

/*
 * Helper function to add function id to the flow table
 *
 * flow_db [in] Ptr to flow table
 * flow_id [in] The flow id of the flow
 * func_id [in] The func_id to be set, for reset pass zero
 *
 * returns none
 */
static void
ulp_flow_db_func_id_set(struct bnxt_ulp_flow_db *flow_db,
			uint32_t flow_id,
			uint32_t func_id)
{
	/* set the function id in the function table */
	if (flow_id < flow_db->func_id_tbl_size)
		flow_db->func_id_tbl[flow_id] = func_id;
	else /* This should never happen */
		BNXT_DRV_DBG(ERR, "Invalid flow id, flowdb corrupt\n");
}

/*
 * Initialize the parent-child database. Memory is allocated in this
 * call and assigned to the database
 *
 * flow_db [in] Ptr to flow table
 * num_entries[in] - number of entries to allocate
 *
 * Returns 0 on success or negative number on failure.
 */
static int32_t
ulp_flow_db_parent_tbl_init(struct bnxt_ulp_flow_db *flow_db,
			    uint32_t num_entries)
{
	struct ulp_fdb_parent_child_db *p_db;
	uint32_t size, idx;

	if (!num_entries)
		return 0;

	/* update the sizes for the allocation */
	p_db = &flow_db->parent_child_db;
	p_db->child_bitset_size = (flow_db->flow_tbl.num_flows /
				   sizeof(uint64_t)) + 1; /* size in bytes */
	p_db->child_bitset_size = ULP_BYTE_ROUND_OFF_8(p_db->child_bitset_size);
	p_db->entries_count = num_entries;

	/* allocate the memory */
	p_db->parent_flow_tbl = rte_zmalloc("fdb parent flow tbl",
					    sizeof(struct ulp_fdb_parent_info) *
					    p_db->entries_count, 0);
	if (!p_db->parent_flow_tbl) {
		BNXT_DRV_DBG(ERR,
			     "Failed to allocate memory fdb parent flow tbl\n");
		return -ENOMEM;
	}
	size = p_db->child_bitset_size * p_db->entries_count;

	/*
	 * allocate the big chunk of memory to be statically carved into
	 * child_fid_bitset pointer.
	 */
	p_db->parent_flow_tbl_mem = rte_zmalloc("fdb parent flow tbl mem",
						size,
						ULP_BUFFER_ALIGN_64_BYTE);
	if (!p_db->parent_flow_tbl_mem) {
		BNXT_DRV_DBG(ERR,
			     "Failed to allocate memory fdb parent flow mem\n");
		return -ENOMEM;
	}

	/* set the pointers in parent table to their offsets */
	for (idx = 0 ; idx < p_db->entries_count; idx++) {
		p_db->parent_flow_tbl[idx].child_fid_bitset =
			(uint64_t *)&p_db->parent_flow_tbl_mem[idx *
			p_db->child_bitset_size];
	}
	/* success */
	return 0;
}

/*
 * Deinitialize the parent-child database. Memory is deallocated in
 * this call and all flows should have been purged before this
 * call.
 *
 * flow_db [in] Ptr to flow table
 *
 * Returns none
 */
static void
ulp_flow_db_parent_tbl_deinit(struct bnxt_ulp_flow_db *flow_db)
{
	/* free the memory related to parent child database */
	if (flow_db->parent_child_db.parent_flow_tbl_mem) {
		rte_free(flow_db->parent_child_db.parent_flow_tbl_mem);
		flow_db->parent_child_db.parent_flow_tbl_mem = NULL;
	}
	if (flow_db->parent_child_db.parent_flow_tbl) {
		rte_free(flow_db->parent_child_db.parent_flow_tbl);
		flow_db->parent_child_db.parent_flow_tbl = NULL;
	}
}

/*
 * Initialize the flow database. Memory is allocated in this
 * call and assigned to the flow database.
 *
 * ulp_ctxt [in] Ptr to ulp context
 *
 * Returns 0 on success or negative number on failure.
 */
int32_t
ulp_flow_db_init(struct bnxt_ulp_context *ulp_ctxt)
{
	struct bnxt_ulp_device_params *dparms;
	struct bnxt_ulp_flow_tbl *flow_tbl;
	struct bnxt_ulp_flow_db *flow_db;
	uint32_t dev_id, num_flows;
	enum bnxt_ulp_flow_mem_type mtype;

	/* Get the dev specific number of flows that needed to be supported. */
	if (bnxt_ulp_cntxt_dev_id_get(ulp_ctxt, &dev_id)) {
		BNXT_DRV_DBG(ERR, "Invalid device id\n");
		return -EINVAL;
	}

	dparms = bnxt_ulp_device_params_get(dev_id);
	if (!dparms) {
		BNXT_DRV_DBG(ERR, "could not fetch the device params\n");
		return -ENODEV;
	}

	flow_db = rte_zmalloc("bnxt_ulp_flow_db",
			      sizeof(struct bnxt_ulp_flow_db), 0);
	if (!flow_db) {
		BNXT_DRV_DBG(ERR,
			     "Failed to allocate memory for flow table ptr\n");
		return -ENOMEM;
	}

	/* Attach the flow database to the ulp context. */
	bnxt_ulp_cntxt_ptr2_flow_db_set(ulp_ctxt, flow_db);

	/* Determine the number of flows based on EM type */
	if (bnxt_ulp_cntxt_mem_type_get(ulp_ctxt, &mtype))
		goto error_free;

	if (mtype == BNXT_ULP_FLOW_MEM_TYPE_INT)
		num_flows = dparms->int_flow_db_num_entries;
	else
		num_flows = dparms->ext_flow_db_num_entries;

	/* Populate the regular flow table limits. */
	flow_tbl = &flow_db->flow_tbl;
	flow_tbl->num_flows = num_flows + 1;
	flow_tbl->num_resources = ((num_flows + 1) *
				   dparms->num_resources_per_flow);

	/* Include the default flow table limits. */
	flow_tbl->num_flows += (BNXT_FLOW_DB_DEFAULT_NUM_FLOWS + 1);
	flow_tbl->num_resources += ((BNXT_FLOW_DB_DEFAULT_NUM_FLOWS + 1) *
				    BNXT_FLOW_DB_DEFAULT_NUM_RESOURCES);

	/* Allocate the resource for the flow table. */
	if (ulp_flow_db_alloc_resource(flow_db))
		goto error_free;

	/* add 1 since we are not using index 0 for flow id */
	flow_db->func_id_tbl_size = flow_tbl->num_flows + 1;
	/* Allocate the function Id table */
	flow_db->func_id_tbl = rte_zmalloc("bnxt_ulp_flow_db_func_id_table",
					   flow_db->func_id_tbl_size *
					   sizeof(uint16_t), 0);
	if (!flow_db->func_id_tbl) {
		BNXT_DRV_DBG(ERR,
			     "Failed to allocate mem for flow table func id\n");
		goto error_free;
	}
	/* initialize the parent child database */
	if (ulp_flow_db_parent_tbl_init(flow_db,
					dparms->fdb_parent_flow_entries)) {
		BNXT_DRV_DBG(ERR,
			     "Failed to allocate mem for parent child db\n");
		goto error_free;
	}

	/* All good so return. */
	BNXT_DRV_DBG(DEBUG, "FlowDB initialized with %d flows.\n",
		     flow_tbl->num_flows);
	return 0;
error_free:
	ulp_flow_db_deinit(ulp_ctxt);
	return -ENOMEM;
}

/*
 * Deinitialize the flow database. Memory is deallocated in
 * this call and all flows should have been purged before this
 * call.
 *
 * ulp_ctxt [in] Ptr to ulp context
 *
 * Returns 0 on success.
 */
int32_t
ulp_flow_db_deinit(struct bnxt_ulp_context *ulp_ctxt)
{
	struct bnxt_ulp_flow_db *flow_db;

	flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
	if (!flow_db)
		return -EINVAL;

	/* Detach the flow database from the ulp context. */
	bnxt_ulp_cntxt_ptr2_flow_db_set(ulp_ctxt, NULL);

	/* Free up all the memory. */
	ulp_flow_db_parent_tbl_deinit(flow_db);
	ulp_flow_db_dealloc_resource(flow_db);
	rte_free(flow_db->func_id_tbl);
	rte_free(flow_db);

	return 0;
}

/*
 * Allocate the flow database entry
 *
 * ulp_ctxt [in] Ptr to ulp_context
 * flow_type [in] - specify default or regular
 * func_id [in].function id of the ingress port
 * fid [out] The index to the flow entry
 *
 * returns 0 on success and negative on failure.
 */
int32_t
ulp_flow_db_fid_alloc(struct bnxt_ulp_context *ulp_ctxt,
		      enum bnxt_ulp_fdb_type flow_type,
		      uint16_t func_id,
		      uint32_t *fid)
{
	struct bnxt_ulp_flow_db *flow_db;
	struct bnxt_ulp_flow_tbl *flow_tbl;

	*fid = 0; /* Initialize fid to invalid value */
	flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
	if (!flow_db) {
		BNXT_DRV_DBG(ERR, "Invalid Arguments\n");
		return -EINVAL;
	}

	if (flow_type >= BNXT_ULP_FDB_TYPE_LAST) {
		BNXT_DRV_DBG(ERR, "Invalid flow type\n");
		return -EINVAL;
	}

	flow_tbl = &flow_db->flow_tbl;
	/* check for max flows */
	if (flow_tbl->num_flows <= flow_tbl->head_index) {
		BNXT_DRV_DBG(ERR, "Flow database has reached max flows\n");
		return -ENOMEM;
	}
	if (flow_tbl->tail_index <= (flow_tbl->head_index + 1)) {
		BNXT_DRV_DBG(ERR, "Flow database has reached max resources\n");
		return -ENOMEM;
	}
	*fid = flow_tbl->flow_tbl_stack[flow_tbl->head_index];
	flow_tbl->head_index++;

	/* Set the flow type */
	ulp_flow_db_active_flows_bit_set(flow_db, flow_type, *fid, 1);

	/* function id update is only valid for regular flow table */
	if (flow_type == BNXT_ULP_FDB_TYPE_REGULAR)
		ulp_flow_db_func_id_set(flow_db, *fid, func_id);

	/* return success */
	return 0;
}

/*
 * Allocate the flow database entry.
 * The params->critical_resource has to be set to 0 to allocate a new resource.
 *
 * ulp_ctxt [in] Ptr to ulp_context
 * flow_type [in] Specify it is regular or default flow
 * fid [in] The index to the flow entry
 * params [in] The contents to be copied into resource
 *
 * returns 0 on success and negative on failure.
 */
int32_t
ulp_flow_db_resource_add(struct bnxt_ulp_context *ulp_ctxt,
			 enum bnxt_ulp_fdb_type flow_type,
			 uint32_t fid,
			 struct ulp_flow_db_res_params *params)
{
	struct bnxt_ulp_flow_db *flow_db;
	struct bnxt_ulp_flow_tbl *flow_tbl;
	struct ulp_fdb_resource_info *resource, *fid_resource;
	struct bnxt_ulp_fc_info *ulp_fc_info;
	uint32_t idx;

	flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
	if (!flow_db) {
		BNXT_DRV_DBG(ERR, "Invalid Arguments\n");
		return -EINVAL;
	}

	if (flow_type >= BNXT_ULP_FDB_TYPE_LAST) {
		BNXT_DRV_DBG(ERR, "Invalid flow type\n");
		return -EINVAL;
	}

	flow_tbl = &flow_db->flow_tbl;
	/* check for max flows */
	if (fid >= flow_tbl->num_flows || !fid) {
		BNXT_DRV_DBG(ERR, "Invalid flow index\n");
		return -EINVAL;
	}

	/* check if the flow is active or not */
	if (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type, fid)) {
		BNXT_DRV_DBG(ERR, "flow does not exist %x:%x\n", flow_type,
			     fid);
		return -EINVAL;
	}

	/* check for max resource */
	if ((flow_tbl->head_index + 1) >= flow_tbl->tail_index) {
		BNXT_DRV_DBG(ERR, "Flow db has reached max resources\n");
		return -ENOMEM;
	}
	fid_resource = &flow_tbl->flow_resources[fid];

	if (params->critical_resource && (fid_resource->fdb_flags &
					  ULP_FDB_FLAG_CRITICAL_RES)) {
		BNXT_DRV_DBG(DEBUG, "Ignore multiple critical resources\n");
		/* Ignore the multiple critical resources */
		params->critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
	}

	if (!params->critical_resource) {
		/* Not the critical_resource so allocate a resource */
		idx = flow_tbl->flow_tbl_stack[flow_tbl->tail_index];
		resource = &flow_tbl->flow_resources[idx];
		flow_tbl->tail_index--;

		/* Update the chain list of resource*/
		ULP_FLOW_DB_RES_NXT_SET(resource->nxt_resource_idx,
					fid_resource->nxt_resource_idx);
		/* update the contents */
		ulp_flow_db_res_params_to_info(resource, params);
		ULP_FLOW_DB_RES_NXT_RESET(fid_resource->nxt_resource_idx);
		ULP_FLOW_DB_RES_NXT_SET(fid_resource->nxt_resource_idx,
					idx);
	} else {
		/* critical resource. Just update the fid resource */
		ulp_flow_db_res_params_to_info(fid_resource, params);
		fid_resource->fdb_flags |= ULP_FDB_FLAG_CRITICAL_RES;
	}

	ulp_fc_info = bnxt_ulp_cntxt_ptr2_fc_info_get(ulp_ctxt);
	if (params->resource_type == TF_TBL_TYPE_ACT_STATS_64 &&
	    params->resource_sub_type ==
	    BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TABLE_INT_COUNT &&
	    ulp_fc_info && ulp_fc_info->num_counters) {
		/* Store the first HW counter ID for this table */
		if (!ulp_fc_mgr_start_idx_isset(ulp_ctxt, params->direction))
			ulp_fc_mgr_start_idx_set(ulp_ctxt, params->direction,
						 params->resource_hndl);

		ulp_fc_mgr_cntr_set(ulp_ctxt, params->direction,
				    params->resource_hndl,
				    ulp_flow_db_shared_session_get(params));

		if (!ulp_fc_mgr_thread_isstarted(ulp_ctxt))
			ulp_fc_mgr_thread_start(ulp_ctxt);

		if (!ulp_sc_mgr_thread_isstarted(ulp_ctxt))
			ulp_sc_mgr_thread_start(ulp_ctxt);
	}

	/* all good, return success */
	return 0;
}

/*
 * Free the flow database entry.
 * The params->critical_resource has to be set to 1 to free the first resource.
 *
 * ulp_ctxt [in] Ptr to ulp_context
 * flow_type [in] Specify it is regular or default flow
 * fid [in] The index to the flow entry
 * params [in/out] The contents to be copied into params.
 * Only the critical_resource needs to be set by the caller.
 *
 * Returns 0 on success and negative on failure.
 */
int32_t
ulp_flow_db_resource_del(struct bnxt_ulp_context *ulp_ctxt,
			 enum bnxt_ulp_fdb_type flow_type,
			 uint32_t fid,
			 struct ulp_flow_db_res_params *params)
{
	struct bnxt_ulp_flow_db *flow_db;
	struct bnxt_ulp_flow_tbl *flow_tbl;
	struct ulp_fdb_resource_info *nxt_resource, *fid_resource;
	uint32_t nxt_idx = 0;

	flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
	if (!flow_db) {
		BNXT_DRV_DBG(ERR, "Invalid Arguments\n");
		return -EINVAL;
	}

	if (flow_type >= BNXT_ULP_FDB_TYPE_LAST) {
		BNXT_DRV_DBG(ERR, "Invalid flow type\n");
		return -EINVAL;
	}

	flow_tbl = &flow_db->flow_tbl;
	/* check for max flows */
	if (fid >= flow_tbl->num_flows || !fid) {
		BNXT_DRV_DBG(ERR, "Invalid flow index %x\n", fid);
		return -EINVAL;
	}

	/* check if the flow is active or not */
	if (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type, fid)) {
		BNXT_DRV_DBG(ERR, "flow does not exist %x:%x\n", flow_type,
			     fid);
		return -EINVAL;
	}

	fid_resource = &flow_tbl->flow_resources[fid];
	if (!params->critical_resource) {
		/* Not the critical resource so free the resource */
		ULP_FLOW_DB_RES_NXT_SET(nxt_idx,
					fid_resource->nxt_resource_idx);
		if (!nxt_idx) {
			/* reached end of resources */
			return -ENOENT;
		}
		nxt_resource = &flow_tbl->flow_resources[nxt_idx];

		/* connect the fid resource to the next resource */
		ULP_FLOW_DB_RES_NXT_RESET(fid_resource->nxt_resource_idx);
		ULP_FLOW_DB_RES_NXT_SET(fid_resource->nxt_resource_idx,
					nxt_resource->nxt_resource_idx);

		/* update the contents to be given to caller */
		ulp_flow_db_res_info_to_params(nxt_resource, params);

		/* Delete the nxt_resource */
		memset(nxt_resource, 0, sizeof(struct ulp_fdb_resource_info));

		/* add it to the free list */
		flow_tbl->tail_index++;
		if (flow_tbl->tail_index >= flow_tbl->num_resources) {
			BNXT_DRV_DBG(ERR, "FlowDB:Tail reached max\n");
			return -ENOENT;
		}
		flow_tbl->flow_tbl_stack[flow_tbl->tail_index] = nxt_idx;

	} else {
		/* Critical resource. copy the contents and exit */
		ulp_flow_db_res_info_to_params(fid_resource, params);
		ULP_FLOW_DB_RES_NXT_SET(nxt_idx,
					fid_resource->nxt_resource_idx);
		memset(fid_resource, 0, sizeof(struct ulp_fdb_resource_info));
		ULP_FLOW_DB_RES_NXT_SET(fid_resource->nxt_resource_idx,
					nxt_idx);
	}

	/* Now that the HW Flow counter resource is deleted, reset it's
	 * corresponding slot in the SW accumulation table in the Flow Counter
	 * manager
	 */
	if (params->resource_type == TF_TBL_TYPE_ACT_STATS_64 &&
	    params->resource_sub_type ==
	    BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TABLE_INT_COUNT) {
		ulp_fc_mgr_cntr_reset(ulp_ctxt, params->direction,
				      params->resource_hndl);
	}

	/* all good, return success */
	return 0;
}

/*
 * Free the flow database entry
 *
 * ulp_ctxt [in] Ptr to ulp_context
 * flow_type [in] - specify default or regular
 * fid [in] The index to the flow entry
 *
 * returns 0 on success and negative on failure.
 */
int32_t
ulp_flow_db_fid_free(struct bnxt_ulp_context *ulp_ctxt,
		     enum bnxt_ulp_fdb_type flow_type,
		     uint32_t fid)
{
	struct bnxt_ulp_flow_tbl *flow_tbl;
	struct bnxt_ulp_flow_db *flow_db;

	flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
	if (!flow_db) {
		BNXT_DRV_DBG(ERR, "Invalid Arguments\n");
		return -EINVAL;
	}

	if (flow_type >= BNXT_ULP_FDB_TYPE_LAST) {
		BNXT_DRV_DBG(ERR, "Invalid flow type\n");
		return -EINVAL;
	}

	flow_tbl = &flow_db->flow_tbl;

	/* check for limits of fid */
	if (fid >= flow_tbl->num_flows || !fid) {
		BNXT_DRV_DBG(ERR, "Invalid flow index\n");
		return -EINVAL;
	}

	/* check if the flow is active or not */
	if (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type, fid)) {
		BNXT_DRV_DBG(ERR, "flow does not exist %x:%x\n", flow_type,
			     fid);
		return -EINVAL;
	}
	flow_tbl->head_index--;
	if (!flow_tbl->head_index) {
		BNXT_DRV_DBG(ERR, "FlowDB: Head Ptr is zero\n");
		return -ENOENT;
	}

	flow_tbl->flow_tbl_stack[flow_tbl->head_index] = fid;

	/* Clear the flows bitmap */
	ulp_flow_db_active_flows_bit_set(flow_db, flow_type, fid, 0);

	if (flow_type == BNXT_ULP_FDB_TYPE_REGULAR)
		ulp_flow_db_func_id_set(flow_db, fid, 0);

	/* all good, return success */
	return 0;
}

/*
 *Get the flow database entry details
 *
 * ulp_ctxt [in] Ptr to ulp_context
 * flow_type [in] - specify default or regular
 * fid [in] The index to the flow entry
 * nxt_idx [in/out] the index to the next entry
 * params [out] The contents to be copied into params.
 *
 * returns 0 on success and negative on failure.
 */
int32_t
ulp_flow_db_resource_get(struct bnxt_ulp_context *ulp_ctxt,
			 enum bnxt_ulp_fdb_type flow_type,
			 uint32_t fid,
			 uint32_t *nxt_idx,
			 struct ulp_flow_db_res_params *params)
{
	struct bnxt_ulp_flow_db *flow_db;
	struct bnxt_ulp_flow_tbl *flow_tbl;
	struct ulp_fdb_resource_info *nxt_resource, *fid_resource;

	flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
	if (!flow_db) {
		BNXT_DRV_DBG(ERR, "Invalid Arguments\n");
		return -EINVAL;
	}

	if (flow_type >= BNXT_ULP_FDB_TYPE_LAST) {
		BNXT_DRV_DBG(ERR, "Invalid flow type\n");
		return -EINVAL;
	}

	flow_tbl = &flow_db->flow_tbl;

	/* check for limits of fid */
	if (fid >= flow_tbl->num_flows || !fid) {
		BNXT_DRV_DBG(ERR, "Invalid flow index\n");
		return -EINVAL;
	}

	/* check if the flow is active or not */
	if (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type, fid)) {
		BNXT_DRV_DBG(ERR, "flow does not exist\n");
		return -EINVAL;
	}

	if (!*nxt_idx) {
		fid_resource = &flow_tbl->flow_resources[fid];
		ulp_flow_db_res_info_to_params(fid_resource, params);
		ULP_FLOW_DB_RES_NXT_SET(*nxt_idx,
					fid_resource->nxt_resource_idx);
	} else {
		nxt_resource = &flow_tbl->flow_resources[*nxt_idx];
		ulp_flow_db_res_info_to_params(nxt_resource, params);
		*nxt_idx = 0;
		ULP_FLOW_DB_RES_NXT_SET(*nxt_idx,
					nxt_resource->nxt_resource_idx);
	}

	/* all good, return success */
	return 0;
}

/*
 * Get the flow database entry iteratively
 *
 * flow_tbl [in] Ptr to flow table
 * flow_type [in] - specify default or regular
 * fid [in/out] The index to the flow entry
 *
 * returns 0 on success and negative on failure.
 */
int32_t
ulp_flow_db_next_entry_get(struct bnxt_ulp_flow_db *flow_db,
			   enum bnxt_ulp_fdb_type flow_type,
			   uint32_t *fid)
{
	uint32_t lfid = *fid;
	uint32_t idx, s_idx, mod_fid;
	uint64_t bs;
	uint64_t *active_flows;
	struct bnxt_ulp_flow_tbl *flowtbl = &flow_db->flow_tbl;

	if (flow_type == BNXT_ULP_FDB_TYPE_REGULAR) {
		active_flows = flowtbl->active_reg_flows;
	} else if (flow_type == BNXT_ULP_FDB_TYPE_DEFAULT) {
		active_flows = flowtbl->active_dflt_flows;
	} else {
		BNXT_DRV_DBG(ERR, "Invalid flow type %x\n", flow_type);
			return -EINVAL;
	}

	do {
		/* increment the flow id to find the next valid flow id */
		lfid++;
		if (lfid >= flowtbl->num_flows)
			return -ENOENT;
		idx = lfid / ULP_INDEX_BITMAP_SIZE;
		mod_fid = lfid % ULP_INDEX_BITMAP_SIZE;
		s_idx = idx;
		while (!(bs = active_flows[idx])) {
			idx++;
			if ((idx * ULP_INDEX_BITMAP_SIZE) >= flowtbl->num_flows)
				return -ENOENT;
		}
		/*
		 * remove the previous bits in the bitset bs to find the
		 * next non zero bit in the bitset. This needs to be done
		 * only if the idx is same as he one you started.
		 */
		if (s_idx == idx)
			bs &= (-1UL >> mod_fid);
		lfid = (idx * ULP_INDEX_BITMAP_SIZE) + rte_clz64(bs);
		if (*fid >= lfid) {
			BNXT_DRV_DBG(ERR, "Flow Database is corrupt\n");
			return -ENOENT;
		}
	} while (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type,
						      lfid));

	/* all good, return success */
	*fid = lfid;
	return 0;
}

/*
 * Flush all flows in the flow database.
 *
 * ulp_ctxt [in] Ptr to ulp context
 * flow_type [in] - specify default or regular
 *
 * returns 0 on success or negative number on failure
 */
int32_t
ulp_flow_db_flush_flows(struct bnxt_ulp_context *ulp_ctx,
			enum bnxt_ulp_fdb_type flow_type)
{
	uint32_t fid = 0;
	struct bnxt_ulp_flow_db *flow_db;

	if (!ulp_ctx) {
		BNXT_DRV_DBG(ERR, "Invalid Argument\n");
		return -EINVAL;
	}

	flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctx);
	if (!flow_db) {
		BNXT_DRV_DBG(ERR, "Flow database not found\n");
		return -EINVAL;
	}
	if (bnxt_ulp_cntxt_acquire_fdb_lock(ulp_ctx)) {
		BNXT_DRV_DBG(ERR, "Flow db lock acquire failed\n");
		return -EINVAL;
	}

#ifdef TF_FLOW_SCALE_QUERY
	tf_resc_pause_usage_update();
#endif

	while (!ulp_flow_db_next_entry_get(flow_db, flow_type, &fid))
		ulp_mapper_resources_free(ulp_ctx, flow_type, fid, NULL);

#ifdef TF_FLOW_SCALE_QUERY
	ulp_resc_usage_sync(ulp_ctx);
#endif

	bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);

	return 0;
}

/*
 * Flush all flows in the flow database that belong to a device function.
 *
 * ulp_ctxt [in] Ptr to ulp context
 * func_id [in] - The port function id
 *
 * returns 0 on success or negative number on failure
 */
int32_t
ulp_flow_db_function_flow_flush(struct bnxt_ulp_context *ulp_ctx,
				uint16_t func_id)
{
	uint32_t flow_id = 0;
	struct bnxt_ulp_flow_db *flow_db;

	if (!ulp_ctx || !func_id) {
		BNXT_DRV_DBG(ERR, "Invalid Argument\n");
		return -EINVAL;
	}

	flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctx);
	if (!flow_db) {
		BNXT_DRV_DBG(ERR, "Flow database not found\n");
		return -EINVAL;
	}
	if (bnxt_ulp_cntxt_acquire_fdb_lock(ulp_ctx)) {
		BNXT_DRV_DBG(ERR, "Flow db lock acquire failed\n");
		return -EINVAL;
	}

#ifdef TF_FLOW_SCALE_QUERY
	tf_resc_pause_usage_update();
#endif
	while (!ulp_flow_db_next_entry_get(flow_db, BNXT_ULP_FDB_TYPE_REGULAR,
					   &flow_id)) {
		if (flow_db->func_id_tbl[flow_id] == func_id)
			ulp_mapper_resources_free(ulp_ctx,
						  BNXT_ULP_FDB_TYPE_REGULAR,
						  flow_id,
						  NULL);
	}
#ifdef TF_FLOW_SCALE_QUERY
	ulp_resc_usage_sync(ulp_ctx);
#endif
	bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
	return 0;
}

/*
 * Flush all flows in the flow database that are associated with the session.
 *
 * ulp_ctxt [in] Ptr to ulp context
 *
 * returns 0 on success or negative number on failure
 */
int32_t
ulp_flow_db_session_flow_flush(struct bnxt_ulp_context *ulp_ctx)
{
	/*
	 * TBD: Tf core implementation of FW session flush shall change this
	 * implementation.
	 */
	return ulp_flow_db_flush_flows(ulp_ctx, BNXT_ULP_FDB_TYPE_REGULAR);
}

/*
 * Check that flow id matches the function id or not
 *
 * ulp_ctxt [in] Ptr to ulp context
 * flow_db [in] Ptr to flow table
 * func_id [in] The func_id to be set, for reset pass zero.
 *
 * returns true on success or false on failure
 */
bool
ulp_flow_db_validate_flow_func(struct bnxt_ulp_context *ulp_ctx,
			       uint32_t flow_id,
			       uint32_t func_id)
{
	struct bnxt_ulp_flow_db *flow_db;

	flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctx);
	if (!flow_db) {
		BNXT_DRV_DBG(ERR, "Flow database not found\n");
		return false;
	}

	/* set the function id in the function table */
	if (flow_id < flow_db->func_id_tbl_size && func_id &&
	    flow_db->func_id_tbl[flow_id] == func_id)
		return true;

	return false;
}

/*
 * Internal api to traverse the resource list within a flow
 * and match a resource based on resource func and resource
 * sub type. This api should be used only for resources that
 * are unique and do not have multiple instances of resource
 * func and sub type combination since it will return only
 * the first match.
 */
static int32_t
ulp_flow_db_resource_params_get(struct bnxt_ulp_context *ulp_ctx,
				enum bnxt_ulp_fdb_type flow_type,
				uint32_t flow_id,
				uint32_t resource_func,
				uint32_t res_subtype,
				struct ulp_flow_db_res_params *params)
{
	struct bnxt_ulp_flow_db *flow_db;
	struct bnxt_ulp_flow_tbl *flow_tbl;
	struct ulp_fdb_resource_info *fid_res;
	uint32_t res_id;

	flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctx);
	if (!flow_db) {
		BNXT_DRV_DBG(ERR, "Flow database not found\n");
		return -EINVAL;
	}

	if (!params) {
		BNXT_DRV_DBG(ERR, "invalid argument\n");
		return -EINVAL;
	}

	if (flow_type >= BNXT_ULP_FDB_TYPE_LAST) {
		BNXT_DRV_DBG(ERR, "Invalid flow type\n");
		return -EINVAL;
	}

	flow_tbl = &flow_db->flow_tbl;

	/* check for limits of fid */
	if (flow_id >= flow_tbl->num_flows || !flow_id) {
		BNXT_DRV_DBG(ERR, "Invalid flow index\n");
		return -EINVAL;
	}

	/* check if the flow is active or not */
	if (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type, flow_id)) {
		BNXT_DRV_DBG(ERR, "flow does not exist\n");
		return -EINVAL;
	}
	/* Iterate the resource to get the resource handle */
	res_id =  flow_id;
	memset(params, 0, sizeof(struct ulp_flow_db_res_params));
	while (res_id) {
		fid_res = &flow_tbl->flow_resources[res_id];
		if (ulp_flow_db_resource_func_get(fid_res) == resource_func &&
		    fid_res->resource_sub_type == res_subtype) {
			ulp_flow_db_res_info_to_params(fid_res, params);
			return 0;
		}
		res_id = 0;
		ULP_FLOW_DB_RES_NXT_SET(res_id, fid_res->nxt_resource_idx);
	}
	return -ENOENT;
}

/*
 * Api to get the cfa action pointer from a flow.
 *
 * ulp_ctxt [in] Ptr to ulp context
 * flow_id [in] flow id
 * cfa_action [out] The resource handle stored in the flow database
 *
 * returns 0 on success
 */
int32_t
ulp_default_flow_db_cfa_action_get(struct bnxt_ulp_context *ulp_ctx,
				   uint32_t flow_id,
				   uint32_t *cfa_action)
{
	uint8_t sub_typ = BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TABLE_VFR_CFA_ACTION;
	struct ulp_flow_db_res_params params;
	int32_t rc;

	rc = ulp_flow_db_resource_params_get(ulp_ctx,
					     BNXT_ULP_FDB_TYPE_DEFAULT,
					     flow_id,
					     BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE,
					     sub_typ, &params);
	if (rc) {
		BNXT_DRV_DBG(DEBUG, "CFA Action ptr not found for flow id %u\n",
			     flow_id);
		return -ENOENT;
	}
	*cfa_action = params.resource_hndl;
	return 0;
}

/* internal validation function for parent flow tbl */
struct ulp_fdb_parent_info *
ulp_flow_db_pc_db_entry_get(struct bnxt_ulp_context *ulp_ctxt,
			    uint32_t pc_idx)
{
	struct bnxt_ulp_flow_db *flow_db;

	flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
	if (!flow_db) {
		BNXT_DRV_DBG(ERR, "Invalid Arguments\n");
		return NULL;
	}

	/* check for max flows */
	if (pc_idx >= BNXT_ULP_MAX_TUN_CACHE_ENTRIES) {
		BNXT_DRV_DBG(ERR, "Invalid tunnel index\n");
		return NULL;
	}

	/* No support for parent child db then just exit */
	if (!flow_db->parent_child_db.entries_count) {
		BNXT_DRV_DBG(ERR, "parent child db not supported\n");
		return NULL;
	}
	if (!flow_db->parent_child_db.parent_flow_tbl[pc_idx].valid) {
		BNXT_DRV_DBG(ERR, "Not a valid tunnel index\n");
		return NULL;
	}

	return &flow_db->parent_child_db.parent_flow_tbl[pc_idx];
}

/* internal validation function for parent flow tbl */
static struct bnxt_ulp_flow_db *
ulp_flow_db_parent_arg_validation(struct bnxt_ulp_context *ulp_ctxt,
				  uint32_t tun_idx)
{
	struct bnxt_ulp_flow_db *flow_db;

	flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
	if (!flow_db) {
		BNXT_DRV_DBG(ERR, "Invalid Arguments\n");
		return NULL;
	}

	/* check for max flows */
	if (tun_idx >= BNXT_ULP_MAX_TUN_CACHE_ENTRIES) {
		BNXT_DRV_DBG(ERR, "Invalid tunnel index\n");
		return NULL;
	}

	/* No support for parent child db then just exit */
	if (!flow_db->parent_child_db.entries_count) {
		BNXT_DRV_DBG(ERR, "parent child db not supported\n");
		return NULL;
	}

	return flow_db;
}

/*
 * Allocate the entry in the parent-child database
 *
 * ulp_ctxt [in] Ptr to ulp_context
 * tun_idx [in] The tunnel index of the flow entry
 *
 * returns index on success and negative on failure.
 */
static int32_t
ulp_flow_db_pc_db_idx_alloc(struct bnxt_ulp_context *ulp_ctxt,
			    uint32_t tun_idx)
{
	struct bnxt_ulp_flow_db *flow_db;
	struct ulp_fdb_parent_child_db *p_pdb;
	uint32_t idx, free_idx = 0;

	/* validate the arguments */
	flow_db = ulp_flow_db_parent_arg_validation(ulp_ctxt, tun_idx);
	if (!flow_db) {
		BNXT_DRV_DBG(ERR, "parent child db validation failed\n");
		return -EINVAL;
	}

	p_pdb = &flow_db->parent_child_db;
	for (idx = 0; idx < p_pdb->entries_count; idx++) {
		if (p_pdb->parent_flow_tbl[idx].valid &&
		    p_pdb->parent_flow_tbl[idx].tun_idx == tun_idx) {
			return idx;
		}
		if (!p_pdb->parent_flow_tbl[idx].valid && !free_idx)
			free_idx = idx + 1;
	}
	/* no free slots */
	if (!free_idx) {
		BNXT_DRV_DBG(ERR, "parent child db is full\n");
		return -ENOMEM;
	}

	free_idx -= 1;
	/* set the Fid in the parent child */
	p_pdb->parent_flow_tbl[free_idx].tun_idx = tun_idx;
	p_pdb->parent_flow_tbl[free_idx].valid = 1;
	return free_idx;
}

/*
 * Free the entry in the parent-child database
 *
 * pc_entry [in] Ptr to parent child db entry
 *
 * returns none.
 */
static void
ulp_flow_db_pc_db_entry_free(struct bnxt_ulp_context *ulp_ctxt,
			     struct ulp_fdb_parent_info *pc_entry)
{
	struct bnxt_tun_cache_entry *tun_tbl;
	struct bnxt_ulp_flow_db *flow_db;
	uint64_t *tmp_bitset;

	/* free the tunnel entry */
	tun_tbl = bnxt_ulp_cntxt_ptr2_tun_tbl_get(ulp_ctxt);
	if (tun_tbl)
		ulp_tunnel_offload_entry_clear(tun_tbl, pc_entry->tun_idx);

	/* free the child bitset*/
	flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
	if (flow_db)
		memset(pc_entry->child_fid_bitset, 0,
		       flow_db->parent_child_db.child_bitset_size);

	/* free the contents */
	tmp_bitset = pc_entry->child_fid_bitset;
	memset(pc_entry, 0, sizeof(struct ulp_fdb_parent_info));
	pc_entry->child_fid_bitset = tmp_bitset;
}

/*
 * Set or reset the parent flow in the parent-child database
 *
 * ulp_ctxt [in] Ptr to ulp_context
 * pc_idx [in] The index to parent child db
 * parent_fid [in] The flow id of the parent flow entry
 * set_flag [in] Use 1 for setting child, 0 to reset
 *
 * returns zero on success and negative on failure.
 */
int32_t
ulp_flow_db_pc_db_parent_flow_set(struct bnxt_ulp_context *ulp_ctxt,
				  uint32_t pc_idx,
				  uint32_t parent_fid,
				  uint32_t set_flag)
{
	struct ulp_fdb_parent_info *pc_entry;
	struct bnxt_ulp_flow_db *flow_db;

	flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
	if (!flow_db) {
		BNXT_DRV_DBG(ERR, "parent child db validation failed\n");
		return -EINVAL;
	}

	/* check for fid validity */
	if (parent_fid >= flow_db->flow_tbl.num_flows || !parent_fid) {
		BNXT_DRV_DBG(ERR, "Invalid parent flow index %x\n", parent_fid);
		return -EINVAL;
	}

	/* validate the arguments and parent child entry */
	pc_entry = ulp_flow_db_pc_db_entry_get(ulp_ctxt, pc_idx);
	if (!pc_entry) {
		BNXT_DRV_DBG(ERR, "failed to get the parent child entry\n");
		return -EINVAL;
	}

	if (set_flag) {
		pc_entry->parent_fid = parent_fid;
		pc_entry->parent_ref_cnt++;
	} else {
		if (pc_entry->parent_ref_cnt > 0)
			pc_entry->parent_ref_cnt--;
		/* Free the parent child db entry if no user present */
		if (!pc_entry->parent_ref_cnt && !pc_entry->f2_cnt)
			ulp_flow_db_pc_db_entry_free(ulp_ctxt, pc_entry);
	}
	return 0;
}

/*
 * Set or reset the child flow in the parent-child database
 *
 * ulp_ctxt [in] Ptr to ulp_context
 * pc_idx [in] The index to parent child db
 * child_fid [in] The flow id of the child flow entry
 * set_flag [in] Use 1 for setting child, 0 to reset
 *
 * returns zero on success and negative on failure.
 */
int32_t
ulp_flow_db_pc_db_child_flow_set(struct bnxt_ulp_context *ulp_ctxt,
				 uint32_t pc_idx,
				 uint32_t child_fid,
				 uint32_t set_flag)
{
	struct ulp_fdb_parent_info *pc_entry;
	struct bnxt_ulp_flow_db *flow_db;
	uint32_t a_idx;
	uint64_t *t;

	flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
	if (!flow_db) {
		BNXT_DRV_DBG(ERR, "parent child db validation failed\n");
		return -EINVAL;
	}

	/* check for fid validity */
	if (child_fid >= flow_db->flow_tbl.num_flows || !child_fid) {
		BNXT_DRV_DBG(ERR, "Invalid child flow index %x\n", child_fid);
		return -EINVAL;
	}

	/* validate the arguments and parent child entry */
	pc_entry = ulp_flow_db_pc_db_entry_get(ulp_ctxt, pc_idx);
	if (!pc_entry) {
		BNXT_DRV_DBG(ERR, "failed to get the parent child entry\n");
		return -EINVAL;
	}

	a_idx = child_fid / ULP_INDEX_BITMAP_SIZE;
	t = pc_entry->child_fid_bitset;
	if (set_flag) {
		ULP_INDEX_BITMAP_SET(t[a_idx], child_fid);
		pc_entry->f2_cnt++;
	} else {
		ULP_INDEX_BITMAP_RESET(t[a_idx], child_fid);
		if (pc_entry->f2_cnt)
			pc_entry->f2_cnt--;
		if (!pc_entry->f2_cnt && !pc_entry->parent_ref_cnt)
			ulp_flow_db_pc_db_entry_free(ulp_ctxt, pc_entry);
	}
	return 0;
}

/*
 * Get the next child flow in the parent-child database
 *
 * ulp_ctxt [in] Ptr to ulp_context
 * parent_fid [in] The flow id of the parent flow entry
 * child_fid [in/out] The flow id of the child flow entry
 *
 * returns zero on success and negative on failure.
 * Pass child_fid as zero for first entry.
 */
int32_t
ulp_flow_db_parent_child_flow_next_entry_get(struct bnxt_ulp_flow_db *flow_db,
					     uint32_t parent_idx,
					     uint32_t *child_fid)
{
	struct ulp_fdb_parent_child_db *p_pdb;
	uint32_t idx, s_idx, mod_fid;
	uint32_t next_fid = *child_fid;
	uint64_t *child_bitset;
	uint64_t bs;

	/* check for fid validity */
	p_pdb = &flow_db->parent_child_db;
	if (parent_idx >= p_pdb->entries_count ||
	    !p_pdb->parent_flow_tbl[parent_idx].parent_fid) {
		BNXT_DRV_DBG(ERR, "Invalid parent flow index %x\n", parent_idx);
		return -EINVAL;
	}

	child_bitset = p_pdb->parent_flow_tbl[parent_idx].child_fid_bitset;
	do {
		/* increment the flow id to find the next valid flow id */
		next_fid++;
		if (next_fid >= flow_db->flow_tbl.num_flows)
			return -ENOENT;
		idx = next_fid / ULP_INDEX_BITMAP_SIZE;
		mod_fid = next_fid % ULP_INDEX_BITMAP_SIZE;
		s_idx = idx;
		while (!(bs = child_bitset[idx])) {
			idx++;
			if ((idx * ULP_INDEX_BITMAP_SIZE) >=
			    flow_db->flow_tbl.num_flows)
				return -ENOENT;
		}
		/*
		 * remove the previous bits in the bitset bs to find the
		 * next non zero bit in the bitset. This needs to be done
		 * only if the idx is same as he one you started.
		 */
		if (s_idx == idx)
			bs &= (-1UL >> mod_fid);
		next_fid = (idx * ULP_INDEX_BITMAP_SIZE) + rte_clz64(bs);
		if (*child_fid >= next_fid) {
			BNXT_DRV_DBG(ERR, "Parent Child Database is corrupt\n");
			return -ENOENT;
		}
		idx = next_fid / ULP_INDEX_BITMAP_SIZE;
	} while (!ULP_INDEX_BITMAP_GET(child_bitset[idx], next_fid));
	*child_fid = next_fid;
	return 0;
}

/*
 * Set the counter accumulation in the parent flow
 *
 * ulp_ctxt [in] Ptr to ulp_context
 * pc_idx [in] The parent child index of the parent flow entry
 *
 * returns index on success and negative on failure.
 */
static int32_t
ulp_flow_db_parent_flow_count_accum_set(struct bnxt_ulp_context *ulp_ctxt,
					uint32_t pc_idx)
{
	struct bnxt_ulp_flow_db *flow_db;
	struct ulp_fdb_parent_child_db *p_pdb;

	flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
	if (!flow_db) {
		BNXT_DRV_DBG(ERR, "Invalid Arguments\n");
		return -EINVAL;
	}

	/* check for parent idx validity */
	p_pdb = &flow_db->parent_child_db;
	if (pc_idx >= p_pdb->entries_count ||
	    !p_pdb->parent_flow_tbl[pc_idx].parent_ref_cnt) {
		BNXT_DRV_DBG(ERR, "Invalid parent child index %x\n", pc_idx);
		return -EINVAL;
	}

	p_pdb->parent_flow_tbl[pc_idx].counter_acc = 1;
	return 0;
}

/*
 * Orphan the child flow entry
 * This is called only for child flows that have
 * BNXT_ULP_RESOURCE_FUNC_CHILD_FLOW resource
 *
 * ulp_ctxt [in] Ptr to ulp_context
 * flow_type [in] Specify it is regular or default flow
 * fid [in] The index to the flow entry
 *
 * Returns 0 on success and negative on failure.
 */
int32_t
ulp_flow_db_child_flow_reset(struct bnxt_ulp_context *ulp_ctxt,
			     enum bnxt_ulp_fdb_type flow_type,
			     uint32_t fid)
{
	struct bnxt_ulp_flow_db *flow_db;
	struct bnxt_ulp_flow_tbl *flow_tbl;
	struct ulp_fdb_resource_info *fid_res;
	uint32_t res_id = 0;

	flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
	if (!flow_db) {
		BNXT_DRV_DBG(ERR, "Invalid Arguments\n");
		return -EINVAL;
	}

	if (flow_type >= BNXT_ULP_FDB_TYPE_LAST) {
		BNXT_DRV_DBG(ERR, "Invalid flow type\n");
		return -EINVAL;
	}

	flow_tbl = &flow_db->flow_tbl;
	/* check for max flows */
	if (fid >= flow_tbl->num_flows || !fid) {
		BNXT_DRV_DBG(ERR, "Invalid flow index %x\n", fid);
		return -EINVAL;
	}

	/* check if the flow is active or not */
	if (!ulp_flow_db_active_flows_bit_is_set(flow_db, flow_type, fid)) {
		BNXT_DRV_DBG(ERR, "flow does not exist\n");
		return -EINVAL;
	}

	/* Iterate the resource to get the resource handle */
	res_id =  fid;
	while (res_id) {
		fid_res = &flow_tbl->flow_resources[res_id];
		if (ulp_flow_db_resource_func_get(fid_res) ==
		    BNXT_ULP_RESOURCE_FUNC_CHILD_FLOW) {
			/* invalidate the resource details */
			fid_res->resource_hndl = 0;
			return 0;
		}
		res_id = 0;
		ULP_FLOW_DB_RES_NXT_SET(res_id, fid_res->nxt_resource_idx);
	}
	/* failed */
	return -1;
}

/*
 * Create parent flow in the parent flow tbl
 *
 * parms [in] Ptr to mapper params
 *
 * Returns 0 on success and negative on failure.
 */
int32_t
ulp_flow_db_parent_flow_create(struct bnxt_ulp_mapper_parms *parms)
{
	struct ulp_flow_db_res_params fid_parms;
	uint32_t sub_typ = BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TABLE_INT_COUNT;
	struct ulp_flow_db_res_params res_params;
	int32_t pc_idx;

	/* create or get the parent child database */
	pc_idx = ulp_flow_db_pc_db_idx_alloc(parms->ulp_ctx, parms->tun_idx);
	if (pc_idx < 0) {
		BNXT_DRV_DBG(ERR, "Error in getting parent child db %x\n",
			     parms->tun_idx);
		return -EINVAL;
	}

	/* Update the parent fid */
	if (ulp_flow_db_pc_db_parent_flow_set(parms->ulp_ctx, pc_idx,
					      parms->flow_id, 1)) {
		BNXT_DRV_DBG(ERR, "Error in setting parent fid %x\n",
			     parms->tun_idx);
		return -EINVAL;
	}

	/* Add the parent details in the resource list of the flow */
	memset(&fid_parms, 0, sizeof(fid_parms));
	fid_parms.resource_func	= BNXT_ULP_RESOURCE_FUNC_PARENT_FLOW;
	fid_parms.resource_hndl	= pc_idx;
	fid_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
	if (ulp_flow_db_resource_add(parms->ulp_ctx, BNXT_ULP_FDB_TYPE_REGULAR,
				     parms->flow_id, &fid_parms)) {
		BNXT_DRV_DBG(ERR, "Error in adding flow res for flow id %x\n",
			     parms->flow_id);
		return -1;
	}

	/* check of the flow has internal counter accumulation enabled */
	if (!ulp_flow_db_resource_params_get(parms->ulp_ctx,
					     BNXT_ULP_FDB_TYPE_REGULAR,
					     parms->flow_id,
					     BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE,
					     sub_typ,
					     &res_params)) {
		/* Enable the counter accumulation in parent entry */
		if (ulp_flow_db_parent_flow_count_accum_set(parms->ulp_ctx,
							    pc_idx)) {
			BNXT_DRV_DBG(ERR, "Error in setting counter acc %x\n",
				     parms->flow_id);
			return -1;
		}
	}

	/* Set parent flow entry idx in stats cache entry */
	ulp_sc_mgr_set_pc_idx(parms->ulp_ctx, parms->flow_id, pc_idx);
	return 0;
}

/*
 * Create child flow in the parent flow tbl
 *
 * parms [in] Ptr to mapper params
 *
 * Returns 0 on success and negative on failure.
 */
int32_t
ulp_flow_db_child_flow_create(struct bnxt_ulp_mapper_parms *parms)
{
	struct ulp_flow_db_res_params fid_parms;
	uint32_t sub_type = BNXT_ULP_RESOURCE_SUB_TYPE_INDEX_TABLE_INT_COUNT;
	enum bnxt_ulp_resource_func res_fun;
	struct ulp_flow_db_res_params res_p;
	int32_t rc, pc_idx;

	/* create or get the parent child database */
	pc_idx = ulp_flow_db_pc_db_idx_alloc(parms->ulp_ctx, parms->tun_idx);
	if (pc_idx < 0) {
		BNXT_DRV_DBG(ERR, "Error in getting parent child db %x\n",
			     parms->tun_idx);
		return -1;
	}

	/* create the parent flow entry in parent flow table */
	rc = ulp_flow_db_pc_db_child_flow_set(parms->ulp_ctx, pc_idx,
					      parms->flow_id, 1);
	if (rc) {
		BNXT_DRV_DBG(ERR, "Error in setting child fid %x\n",
			     parms->flow_id);
		return rc;
	}

	/* Add the parent details in the resource list of the flow */
	memset(&fid_parms, 0, sizeof(fid_parms));
	fid_parms.resource_func	= BNXT_ULP_RESOURCE_FUNC_CHILD_FLOW;
	fid_parms.resource_hndl	= pc_idx;
	fid_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
	rc  = ulp_flow_db_resource_add(parms->ulp_ctx,
				       BNXT_ULP_FDB_TYPE_REGULAR,
				       parms->flow_id, &fid_parms);
	if (rc) {
		BNXT_DRV_DBG(ERR, "Error in adding flow res for flow id %x\n",
			     parms->flow_id);
		return rc;
	}

	/* check if internal count action included for this flow.*/
	res_fun = BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE;
	rc = ulp_flow_db_resource_params_get(parms->ulp_ctx,
					     BNXT_ULP_FDB_TYPE_REGULAR,
					     parms->flow_id,
					     res_fun,
					     sub_type,
					     &res_p);
	if (!rc) {
		/* update the counter manager to include parent fid */
		if (ulp_fc_mgr_cntr_parent_flow_set(parms->ulp_ctx,
						    res_p.direction,
						    res_p.resource_hndl,
						    pc_idx)) {
			BNXT_DRV_DBG(ERR, "Error in setting child %x\n",
				     parms->flow_id);
			return -1;
		}
	}

	/* return success */
	return 0;
}

/*
 * Update the parent counters
 *
 * ulp_ctxt [in] Ptr to ulp_context
 * pc_idx [in] The parent flow entry idx
 * packet_count [in] - packet count
 * byte_count [in] - byte count
 *
 * returns 0 on success
 */
int32_t
ulp_flow_db_parent_flow_count_update(struct bnxt_ulp_context *ulp_ctxt,
				     uint32_t pc_idx,
				     uint64_t packet_count,
				     uint64_t byte_count)
{
	struct ulp_fdb_parent_info *pc_entry;

	/* validate the arguments and get parent child entry */
	pc_entry = ulp_flow_db_pc_db_entry_get(ulp_ctxt, pc_idx);
	if (!pc_entry) {
		BNXT_DRV_DBG(ERR, "failed to get the parent child entry\n");
		return -EINVAL;
	}

	if (pc_entry->counter_acc) {
		pc_entry->pkt_count += packet_count;
		pc_entry->byte_count += byte_count;
	}
	return 0;
}

/*
 * Get the parent accumulation counters
 *
 * ulp_ctxt [in] Ptr to ulp_context
 * pc_idx [in] The parent flow entry idx
 * packet_count [out] - packet count
 * byte_count [out] - byte count
 *
 * returns 0 on success
 */
int32_t
ulp_flow_db_parent_flow_count_get(struct bnxt_ulp_context *ulp_ctxt,
				  uint32_t flow_id,
				  uint32_t pc_idx, uint64_t *packet_count,
				  uint64_t *byte_count, uint8_t count_reset)
{
	struct ulp_fdb_parent_info *pc_entry;

	/* validate the arguments and get parent child entry */
	pc_entry = ulp_flow_db_pc_db_entry_get(ulp_ctxt, pc_idx);
	if (!pc_entry) {
		BNXT_DRV_DBG(ERR, "failed to get the parent child entry\n");
		return -EINVAL;
	}

	/* stale parent fid */
	if (flow_id != pc_entry->parent_fid) {
		*packet_count = 0;
		*byte_count = 0;
		return 0;
	}

	if (pc_entry->counter_acc) {
		*packet_count = pc_entry->pkt_count;
		*byte_count = pc_entry->byte_count;
		if (count_reset) {
			pc_entry->pkt_count = 0;
			pc_entry->byte_count = 0;
		}
	}
	return 0;
}

/*
 * reset the parent accumulation counters
 *
 * ulp_ctxt [in] Ptr to ulp_context
 *
 * returns none
 */
void
ulp_flow_db_parent_flow_count_reset(struct bnxt_ulp_context *ulp_ctxt)
{
	struct bnxt_ulp_flow_db *flow_db;
	struct ulp_fdb_parent_child_db *p_pdb;
	uint32_t idx;

	/* validate the arguments */
	flow_db = bnxt_ulp_cntxt_ptr2_flow_db_get(ulp_ctxt);
	if (!flow_db) {
		BNXT_DRV_DBG(ERR, "parent child db validation failed\n");
		return;
	}

	p_pdb = &flow_db->parent_child_db;
	for (idx = 0; idx < p_pdb->entries_count; idx++) {
		if (p_pdb->parent_flow_tbl[idx].valid &&
		    p_pdb->parent_flow_tbl[idx].counter_acc) {
			p_pdb->parent_flow_tbl[idx].pkt_count = 0;
			p_pdb->parent_flow_tbl[idx].byte_count = 0;
		}
	}
}

/*
 * Set the shared bit for the flow db entry
 *
 * res [in] Ptr to fdb entry
 * shared [in] shared flag
 *
 * returns none
 */
void ulp_flow_db_shared_session_set(struct ulp_flow_db_res_params *res,
				    enum bnxt_ulp_session_type s_type)
{
	if (res && (s_type & BNXT_ULP_SESSION_TYPE_SHARED))
		res->fdb_flags |= ULP_FDB_FLAG_SHARED_SESSION;
	else if (res && (s_type & BNXT_ULP_SESSION_TYPE_SHARED_WC))
		res->fdb_flags |= ULP_FDB_FLAG_SHARED_WC_SESSION;
}

/*
 * get the shared bit for the flow db entry
 *
 * res [in] Ptr to fdb entry
 *
 * returns session type
 */
enum bnxt_ulp_session_type
ulp_flow_db_shared_session_get(struct ulp_flow_db_res_params *res)
{
	enum bnxt_ulp_session_type stype = BNXT_ULP_SESSION_TYPE_DEFAULT;

	if (res && (res->fdb_flags & ULP_FDB_FLAG_SHARED_SESSION))
		stype = BNXT_ULP_SESSION_TYPE_SHARED;
	else if (res && (res->fdb_flags & ULP_FDB_FLAG_SHARED_WC_SESSION))
		stype = BNXT_ULP_SESSION_TYPE_SHARED_WC;

	return stype;
}