1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2014-2020 Broadcom
6 #include <rte_common.h>
7 #include <rte_malloc.h>
11 #include "tf_ext_flow_handle.h"
12 #include "ulp_mark_mgr.h"
13 #include "bnxt_tf_common.h"
14 #include "ulp_template_db_enum.h"
15 #include "ulp_template_struct.h"
17 #define ULP_MARK_DB_ENTRY_SET_VALID(mark_info) ((mark_info)->flags |=\
19 #define ULP_MARK_DB_ENTRY_IS_INVALID(mark_info) (!((mark_info)->flags &\
21 #define ULP_MARK_DB_ENTRY_IS_VFR_ID(mark_info) ((mark_info)->flags &\
23 #define ULP_MARK_DB_ENTRY_IS_GLOBAL_HW_FID(mark_info) ((mark_info)->flags &\
24 BNXT_ULP_MARK_GLOBAL_HW_FID)
26 static inline uint32_t
27 ulp_mark_db_idx_get(bool is_gfid, uint32_t fid, struct bnxt_ulp_mark_tbl *mtbl)
29 uint32_t idx = 0, hashtype = 0;
32 TF_GET_HASH_TYPE_FROM_GFID(fid, hashtype);
33 TF_GET_HASH_INDEX_FROM_GFID(fid, idx);
35 /* Need to truncate anything beyond supported flows */
36 idx &= mtbl->gfid_mask;
38 idx |= mtbl->gfid_type_bit;
46 * Allocate and Initialize all Mark Manager resources for this ulp context.
48 * ctxt [in] The ulp context for the mark manager.
52 ulp_mark_db_init(struct bnxt_ulp_context *ctxt)
54 struct bnxt_ulp_device_params *dparms;
55 struct bnxt_ulp_mark_tbl *mark_tbl = NULL;
59 BNXT_TF_DBG(DEBUG, "Invalid ULP CTXT\n");
63 if (bnxt_ulp_cntxt_dev_id_get(ctxt, &dev_id)) {
64 BNXT_TF_DBG(DEBUG, "Failed to get device id\n");
68 dparms = bnxt_ulp_device_params_get(dev_id);
70 BNXT_TF_DBG(DEBUG, "Failed to device parms\n");
74 mark_tbl = rte_zmalloc("ulp_rx_mark_tbl_ptr",
75 sizeof(struct bnxt_ulp_mark_tbl), 0);
79 /* Need to allocate 2 * Num flows to account for hash type bit.*/
80 mark_tbl->lfid_num_entries = dparms->mark_db_lfid_entries;
81 mark_tbl->lfid_tbl = rte_zmalloc("ulp_rx_em_flow_mark_table",
82 mark_tbl->lfid_num_entries *
83 sizeof(struct bnxt_lfid_mark_info),
85 if (!mark_tbl->lfid_tbl)
88 /* Need to allocate 2 * Num flows to account for hash type bit */
89 mark_tbl->gfid_num_entries = dparms->mark_db_gfid_entries;
90 if (!mark_tbl->gfid_num_entries)
91 goto gfid_not_required;
93 mark_tbl->gfid_tbl = rte_zmalloc("ulp_rx_eem_flow_mark_table",
94 mark_tbl->gfid_num_entries *
95 sizeof(struct bnxt_gfid_mark_info),
97 if (!mark_tbl->gfid_tbl)
101 * These values are used to compress the FID to the allowable index
102 * space. The FID from hw may be the full hash which may be a big
103 * value to allocate and so allocate only needed hash values.
104 * gfid mask is the number of flow entries for the each left/right
105 * hash The gfid type bit is used to get to the higher or lower hash
108 mark_tbl->gfid_mask = (mark_tbl->gfid_num_entries / 2) - 1;
109 mark_tbl->gfid_type_bit = (mark_tbl->gfid_num_entries / 2);
111 BNXT_TF_DBG(DEBUG, "GFID Max = 0x%08x\nGFID MASK = 0x%08x\n",
112 mark_tbl->gfid_num_entries - 1,
113 mark_tbl->gfid_mask);
116 /* Add the mark tbl to the ulp context. */
117 bnxt_ulp_cntxt_ptr2_mark_db_set(ctxt, mark_tbl);
121 rte_free(mark_tbl->gfid_tbl);
122 rte_free(mark_tbl->lfid_tbl);
124 BNXT_TF_DBG(DEBUG, "Failed to allocate memory for mark mgr\n");
129 * Release all resources in the Mark Manager for this ulp context
131 * ctxt [in] The ulp context for the mark manager
135 ulp_mark_db_deinit(struct bnxt_ulp_context *ctxt)
137 struct bnxt_ulp_mark_tbl *mtbl;
139 mtbl = bnxt_ulp_cntxt_ptr2_mark_db_get(ctxt);
142 rte_free(mtbl->gfid_tbl);
143 rte_free(mtbl->lfid_tbl);
146 /* Safe to ignore on deinit */
147 (void)bnxt_ulp_cntxt_ptr2_mark_db_set(ctxt, NULL);
154 * Get a Mark from the Mark Manager
156 * ctxt [in] The ulp context for the mark manager
158 * is_gfid [in] The type of fid (GFID or LFID)
160 * fid [in] The flow id that is returned by HW in BD
162 * vfr_flag [out].it indicatesif mark is vfr_id or mark id
164 * mark [out] The mark that is associated with the FID
168 ulp_mark_db_mark_get(struct bnxt_ulp_context *ctxt,
174 struct bnxt_ulp_mark_tbl *mtbl;
180 mtbl = bnxt_ulp_cntxt_ptr2_mark_db_get(ctxt);
182 BNXT_TF_DBG(ERR, "Unable to get Mark Table\n");
186 idx = ulp_mark_db_idx_get(is_gfid, fid, mtbl);
189 if (idx >= mtbl->gfid_num_entries ||
190 ULP_MARK_DB_ENTRY_IS_INVALID(&mtbl->gfid_tbl[idx]))
193 BNXT_TF_DBG(DEBUG, "Get GFID[0x%0x] = 0x%0x\n",
194 idx, mtbl->gfid_tbl[idx].mark_id);
196 *vfr_flag = ULP_MARK_DB_ENTRY_IS_VFR_ID(&mtbl->gfid_tbl[idx]);
197 *mark = mtbl->gfid_tbl[idx].mark_id;
199 if (idx >= mtbl->lfid_num_entries ||
200 ULP_MARK_DB_ENTRY_IS_INVALID(&mtbl->lfid_tbl[idx]))
203 BNXT_TF_DBG(DEBUG, "Get LFID[0x%0x] = 0x%0x\n",
204 idx, mtbl->lfid_tbl[idx].mark_id);
206 *vfr_flag = ULP_MARK_DB_ENTRY_IS_VFR_ID(&mtbl->lfid_tbl[idx]);
207 *mark = mtbl->lfid_tbl[idx].mark_id;
214 * Adds a Mark to the Mark Manager
216 * ctxt [in] The ulp context for the mark manager
218 * mark_flag [in] mark flags.
220 * fid [in] The flow id that is returned by HW in BD
222 * mark [in] The mark to be associated with the FID
226 ulp_mark_db_mark_add(struct bnxt_ulp_context *ctxt,
231 struct bnxt_ulp_mark_tbl *mtbl;
236 BNXT_TF_DBG(ERR, "Invalid ulp context\n");
240 mtbl = bnxt_ulp_cntxt_ptr2_mark_db_get(ctxt);
242 BNXT_TF_DBG(ERR, "Unable to get Mark DB\n");
246 is_gfid = (mark_flag & BNXT_ULP_MARK_GLOBAL_HW_FID);
248 idx = ulp_mark_db_idx_get(is_gfid, fid, mtbl);
249 if (idx >= mtbl->gfid_num_entries) {
250 BNXT_TF_DBG(ERR, "Mark index greater than allocated\n");
253 BNXT_TF_DBG(DEBUG, "Set GFID[0x%0x] = 0x%0x\n", idx, mark);
254 mtbl->gfid_tbl[idx].mark_id = mark;
255 ULP_MARK_DB_ENTRY_SET_VALID(&mtbl->gfid_tbl[idx]);
258 /* For the LFID, the FID is used as the index */
259 if (fid >= mtbl->lfid_num_entries) {
260 BNXT_TF_DBG(ERR, "Mark index greater than allocated\n");
263 BNXT_TF_DBG(DEBUG, "Set LFID[0x%0x] = 0x%0x\n", fid, mark);
264 mtbl->lfid_tbl[fid].mark_id = mark;
265 ULP_MARK_DB_ENTRY_SET_VALID(&mtbl->lfid_tbl[fid]);
272 * Removes a Mark from the Mark Manager
274 * ctxt [in] The ulp context for the mark manager
276 * mark_flag [in] mark flags.
278 * fid [in] The flow id that is returned by HW in BD
282 ulp_mark_db_mark_del(struct bnxt_ulp_context *ctxt,
286 struct bnxt_ulp_mark_tbl *mtbl;
291 BNXT_TF_DBG(ERR, "Invalid ulp context\n");
295 mtbl = bnxt_ulp_cntxt_ptr2_mark_db_get(ctxt);
297 BNXT_TF_DBG(ERR, "Unable to get Mark DB\n");
301 is_gfid = (mark_flag & BNXT_ULP_MARK_GLOBAL_HW_FID);
303 idx = ulp_mark_db_idx_get(is_gfid, fid, mtbl);
304 if (idx >= mtbl->gfid_num_entries) {
305 BNXT_TF_DBG(ERR, "Mark index greater than allocated\n");
308 BNXT_TF_DBG(DEBUG, "Reset GFID[0x%0x]\n", idx);
309 memset(&mtbl->gfid_tbl[idx], 0,
310 sizeof(struct bnxt_gfid_mark_info));
313 /* For the LFID, the FID is used as the index */
314 if (fid >= mtbl->lfid_num_entries) {
315 BNXT_TF_DBG(ERR, "Mark index greater than allocated\n");
318 memset(&mtbl->lfid_tbl[fid], 0,
319 sizeof(struct bnxt_lfid_mark_info));