net/bnxt: support ULP session manager cleanup
[dpdk.git] / drivers / net / bnxt / tf_ulp / ulp_mark_mgr.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2014-2020 Broadcom
3  * All rights reserved.
4  */
5
6 #include <rte_common.h>
7 #include <rte_malloc.h>
8 #include <rte_log.h>
9 #include "bnxt_ulp.h"
10 #include "tf_ext_flow_handle.h"
11 #include "ulp_mark_mgr.h"
12 #include "bnxt_tf_common.h"
13 #include "../bnxt.h"
14 #include "ulp_template_db.h"
15 #include "ulp_template_struct.h"
16
17 /*
18  * Allocate and Initialize all Mark Manager resources for this ulp context.
19  *
20  * ctxt [in] The ulp context for the mark manager.
21  *
22  */
23 int32_t
24 ulp_mark_db_init(struct bnxt_ulp_context *ctxt)
25 {
26         struct bnxt_ulp_device_params *dparms;
27         struct bnxt_ulp_mark_tbl *mark_tbl = NULL;
28         uint32_t dev_id;
29
30         if (!ctxt) {
31                 BNXT_TF_DBG(DEBUG, "Invalid ULP CTXT\n");
32                 return -EINVAL;
33         }
34
35         if (bnxt_ulp_cntxt_dev_id_get(ctxt, &dev_id)) {
36                 BNXT_TF_DBG(DEBUG, "Failed to get device id\n");
37                 return -EINVAL;
38         }
39
40         dparms = bnxt_ulp_device_params_get(dev_id);
41         if (!dparms) {
42                 BNXT_TF_DBG(DEBUG, "Failed to device parms\n");
43                 return -EINVAL;
44         }
45
46         mark_tbl = rte_zmalloc("ulp_rx_mark_tbl_ptr",
47                                sizeof(struct bnxt_ulp_mark_tbl), 0);
48         if (!mark_tbl)
49                 goto mem_error;
50
51         /* Need to allocate 2 * Num flows to account for hash type bit. */
52         mark_tbl->lfid_tbl = rte_zmalloc("ulp_rx_em_flow_mark_table",
53                                          dparms->lfid_entries *
54                                             sizeof(struct bnxt_lfid_mark_info),
55                                          0);
56
57         if (!mark_tbl->lfid_tbl)
58                 goto mem_error;
59
60         /* Need to allocate 2 * Num flows to account for hash type bit. */
61         mark_tbl->gfid_tbl = rte_zmalloc("ulp_rx_eem_flow_mark_table",
62                                          2 * dparms->num_flows *
63                                             sizeof(struct bnxt_gfid_mark_info),
64                                          0);
65         if (!mark_tbl->gfid_tbl)
66                 goto mem_error;
67
68         /*
69          * TBD: This needs to be generalized for better mark handling
70          * These values are used to compress the FID to the allowable index
71          * space.  The FID from hw may be the full hash.
72          */
73         mark_tbl->gfid_max      = dparms->gfid_entries - 1;
74         mark_tbl->gfid_mask     = (dparms->gfid_entries / 2) - 1;
75         mark_tbl->gfid_type_bit = (dparms->gfid_entries / 2);
76
77         BNXT_TF_DBG(DEBUG, "GFID Max = 0x%08x\nGFID MASK = 0x%08x\n",
78                     mark_tbl->gfid_max,
79                     mark_tbl->gfid_mask);
80
81         /* Add the mart tbl to the ulp context. */
82         bnxt_ulp_cntxt_ptr2_mark_db_set(ctxt, mark_tbl);
83
84         return 0;
85
86 mem_error:
87         rte_free(mark_tbl->gfid_tbl);
88         rte_free(mark_tbl->lfid_tbl);
89         rte_free(mark_tbl);
90         BNXT_TF_DBG(DEBUG,
91                     "Failed to allocate memory for mark mgr\n");
92
93         return -ENOMEM;
94 }
95
96 /*
97  * Release all resources in the Mark Manager for this ulp context
98  *
99  * ctxt [in] The ulp context for the mark manager
100  *
101  */
102 int32_t
103 ulp_mark_db_deinit(struct bnxt_ulp_context *ctxt)
104 {
105         struct bnxt_ulp_mark_tbl *mtbl;
106
107         mtbl = bnxt_ulp_cntxt_ptr2_mark_db_get(ctxt);
108
109         if (mtbl) {
110                 rte_free(mtbl->gfid_tbl);
111                 rte_free(mtbl->lfid_tbl);
112                 rte_free(mtbl);
113
114                 /* Safe to ignore on deinit */
115                 (void)bnxt_ulp_cntxt_ptr2_mark_db_set(ctxt, NULL);
116         }
117
118         return 0;
119 }