net/bnxt: combine default and regular flows
[dpdk.git] / drivers / net / bnxt / tf_ulp / ulp_mapper.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2014-2020 Broadcom
3  * All rights reserved.
4  */
5
6 #include <rte_log.h>
7 #include <rte_malloc.h>
8 #include "bnxt.h"
9 #include "ulp_template_db_enum.h"
10 #include "ulp_template_struct.h"
11 #include "bnxt_tf_common.h"
12 #include "ulp_utils.h"
13 #include "bnxt_ulp.h"
14 #include "tfp.h"
15 #include "tf_ext_flow_handle.h"
16 #include "ulp_mark_mgr.h"
17 #include "ulp_flow_db.h"
18 #include "ulp_mapper.h"
19 #include "tf_util.h"
20
21 static struct bnxt_ulp_glb_resource_info *
22 ulp_mapper_glb_resource_info_list_get(uint32_t *num_entries)
23 {
24         if (!num_entries)
25                 return NULL;
26         *num_entries = BNXT_ULP_GLB_RESOURCE_TBL_MAX_SZ;
27         return ulp_glb_resource_tbl;
28 }
29
30 /*
31  * Read the global resource from the mapper global resource list
32  *
33  * The regval is always returned in big-endian.
34  *
35  * returns 0 on success
36  */
37 static int32_t
38 ulp_mapper_glb_resource_read(struct bnxt_ulp_mapper_data *mapper_data,
39                              enum tf_dir dir,
40                              uint16_t idx,
41                              uint64_t *regval)
42 {
43         if (!mapper_data || !regval ||
44             dir >= TF_DIR_MAX || idx >= BNXT_ULP_GLB_REGFILE_INDEX_LAST)
45                 return -EINVAL;
46
47         *regval = mapper_data->glb_res_tbl[dir][idx].resource_hndl;
48         return 0;
49 }
50
51 /*
52  * Write a global resource to the mapper global resource list
53  *
54  * The regval value must be in big-endian.
55  *
56  * return 0 on success.
57  */
58 static int32_t
59 ulp_mapper_glb_resource_write(struct bnxt_ulp_mapper_data *data,
60                               struct bnxt_ulp_glb_resource_info *res,
61                               uint64_t regval)
62 {
63         struct bnxt_ulp_mapper_glb_resource_entry *ent;
64
65         /* validate the arguments */
66         if (!data || res->direction >= TF_DIR_MAX ||
67             res->glb_regfile_index >= BNXT_ULP_GLB_REGFILE_INDEX_LAST)
68                 return -EINVAL;
69
70         /* write to the mapper data */
71         ent = &data->glb_res_tbl[res->direction][res->glb_regfile_index];
72         ent->resource_func = res->resource_func;
73         ent->resource_type = res->resource_type;
74         ent->resource_hndl = regval;
75         return 0;
76 }
77
78 /*
79  * Internal function to allocate identity resource and store it in mapper data.
80  *
81  * returns 0 on success
82  */
83 static int32_t
84 ulp_mapper_resource_ident_allocate(struct bnxt_ulp_context *ulp_ctx,
85                                    struct bnxt_ulp_mapper_data *mapper_data,
86                                    struct bnxt_ulp_glb_resource_info *glb_res)
87 {
88         struct tf_alloc_identifier_parms iparms = { 0 };
89         struct tf_free_identifier_parms fparms;
90         uint64_t regval;
91         struct tf *tfp;
92         int32_t rc = 0;
93
94         tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx);
95         if (!tfp)
96                 return -EINVAL;
97
98         iparms.ident_type = glb_res->resource_type;
99         iparms.dir = glb_res->direction;
100
101         /* Allocate the Identifier using tf api */
102         rc = tf_alloc_identifier(tfp, &iparms);
103         if (rc) {
104                 BNXT_TF_DBG(ERR, "Failed to alloc identifier [%s][%d]\n",
105                             (iparms.dir == TF_DIR_RX) ? "RX" : "TX",
106                             iparms.ident_type);
107                 return rc;
108         }
109
110         /* entries are stored as big-endian format */
111         regval = tfp_cpu_to_be_64((uint64_t)iparms.id);
112         /* write to the mapper global resource */
113         rc = ulp_mapper_glb_resource_write(mapper_data, glb_res, regval);
114         if (rc) {
115                 BNXT_TF_DBG(ERR, "Failed to write to global resource id\n");
116                 /* Free the identifier when update failed */
117                 fparms.dir = iparms.dir;
118                 fparms.ident_type = iparms.ident_type;
119                 fparms.id = iparms.id;
120                 tf_free_identifier(tfp, &fparms);
121                 return rc;
122         }
123         return rc;
124 }
125
126 /*
127  * Internal function to allocate index tbl resource and store it in mapper data.
128  *
129  * returns 0 on success
130  */
131 static int32_t
132 ulp_mapper_resource_index_tbl_alloc(struct bnxt_ulp_context *ulp_ctx,
133                                     struct bnxt_ulp_mapper_data *mapper_data,
134                                     struct bnxt_ulp_glb_resource_info *glb_res)
135 {
136         struct tf_alloc_tbl_entry_parms aparms = { 0 };
137         struct tf_free_tbl_entry_parms  free_parms = { 0 };
138         uint64_t regval;
139         struct tf *tfp;
140         uint32_t tbl_scope_id;
141         int32_t rc = 0;
142
143         tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx);
144         if (!tfp)
145                 return -EINVAL;
146
147         /* Get the scope id */
148         rc = bnxt_ulp_cntxt_tbl_scope_id_get(ulp_ctx, &tbl_scope_id);
149         if (rc) {
150                 BNXT_TF_DBG(ERR, "Failed to get table scope rc=%d\n", rc);
151                 return rc;
152         }
153
154         aparms.type = glb_res->resource_type;
155         aparms.dir = glb_res->direction;
156         aparms.search_enable = BNXT_ULP_SEARCH_BEFORE_ALLOC_NO;
157         aparms.tbl_scope_id = tbl_scope_id;
158
159         /* Allocate the index tbl using tf api */
160         rc = tf_alloc_tbl_entry(tfp, &aparms);
161         if (rc) {
162                 BNXT_TF_DBG(ERR, "Failed to alloc identifier [%s][%d]\n",
163                             (aparms.dir == TF_DIR_RX) ? "RX" : "TX",
164                             aparms.type);
165                 return rc;
166         }
167
168         /* entries are stored as big-endian format */
169         regval = tfp_cpu_to_be_64((uint64_t)aparms.idx);
170         /* write to the mapper global resource */
171         rc = ulp_mapper_glb_resource_write(mapper_data, glb_res, regval);
172         if (rc) {
173                 BNXT_TF_DBG(ERR, "Failed to write to global resource id\n");
174                 /* Free the identifier when update failed */
175                 free_parms.dir = aparms.dir;
176                 free_parms.type = aparms.type;
177                 free_parms.idx = aparms.idx;
178                 tf_free_tbl_entry(tfp, &free_parms);
179                 return rc;
180         }
181         return rc;
182 }
183
184 /* Retrieve the cache initialization parameters for the tbl_idx */
185 static struct bnxt_ulp_cache_tbl_params *
186 ulp_mapper_cache_tbl_params_get(uint32_t tbl_idx)
187 {
188         if (tbl_idx >= BNXT_ULP_CACHE_TBL_MAX_SZ)
189                 return NULL;
190
191         return &ulp_cache_tbl_params[tbl_idx];
192 }
193
194 /* Retrieve the global template table */
195 static uint32_t *
196 ulp_mapper_glb_template_table_get(uint32_t *num_entries)
197 {
198         if (!num_entries)
199                 return NULL;
200         *num_entries = BNXT_ULP_GLB_TEMPLATE_TBL_MAX_SZ;
201         return ulp_glb_template_tbl;
202 }
203
204 /*
205  * Get the size of the action property for a given index.
206  *
207  * idx [in] The index for the action property
208  *
209  * returns the size of the action property.
210  */
211 static uint32_t
212 ulp_mapper_act_prop_size_get(uint32_t idx)
213 {
214         if (idx >= BNXT_ULP_ACT_PROP_IDX_LAST)
215                 return 0;
216         return ulp_act_prop_map_table[idx];
217 }
218
219 /*
220  * Get a list of classifier tables that implement the flow
221  * Gets a device dependent list of tables that implement the class template id
222  *
223  * mparms [in] The mappers parms with data related to the flow.
224  *
225  * tid [in] The template id that matches the flow
226  *
227  * num_tbls [out] The number of classifier tables in the returned array
228  *
229  * returns An array of classifier tables to implement the flow, or NULL on
230  * error
231  */
232 static struct bnxt_ulp_mapper_tbl_info *
233 ulp_mapper_tbl_list_get(struct bnxt_ulp_mapper_parms *mparms,
234                         uint32_t tid,
235                         uint32_t *num_tbls)
236 {
237         uint32_t idx;
238         const struct ulp_template_device_tbls *dev_tbls;
239
240         dev_tbls = &mparms->device_params->dev_tbls[mparms->tmpl_type];
241
242         idx = dev_tbls->tmpl_list[tid].start_tbl_idx;
243         *num_tbls = dev_tbls->tmpl_list[tid].num_tbls;
244
245         return &dev_tbls->tbl_list[idx];
246 }
247
248 /*
249  * Get the list of key fields that implement the flow.
250  *
251  * mparms [in] The mapper parms with information about the flow
252  *
253  * tbl [in] A single table instance to get the key fields from
254  *
255  * num_flds [out] The number of key fields in the returned array
256  *
257  * Returns array of Key fields, or NULL on error.
258  */
259 static struct bnxt_ulp_mapper_class_key_field_info *
260 ulp_mapper_key_fields_get(struct bnxt_ulp_mapper_parms *mparms,
261                           struct bnxt_ulp_mapper_tbl_info *tbl,
262                           uint32_t *num_flds)
263 {
264         uint32_t idx;
265         const struct ulp_template_device_tbls *dev_tbls;
266
267         dev_tbls = &mparms->device_params->dev_tbls[mparms->tmpl_type];
268         if (!dev_tbls->key_field_list) {
269                 *num_flds = 0;
270                 return NULL;
271         }
272
273         idx             = tbl->key_start_idx;
274         *num_flds       = tbl->key_num_fields;
275
276         return &dev_tbls->key_field_list[idx];
277 }
278
279 /*
280  * Get the list of data fields that implement the flow.
281  *
282  * mparms [in] The mapper parms with information about the flow
283  *
284  * tbl [in] A single table instance to get the data fields from
285  *
286  * num_flds [out] The number of data fields in the returned array.
287  *
288  * num_encap_flds [out] The number of encap fields in the returned array.
289  *
290  * Returns array of data fields, or NULL on error.
291  */
292 static struct bnxt_ulp_mapper_result_field_info *
293 ulp_mapper_result_fields_get(struct bnxt_ulp_mapper_parms *mparms,
294                              struct bnxt_ulp_mapper_tbl_info *tbl,
295                              uint32_t *num_flds,
296                              uint32_t *num_encap_flds)
297 {
298         uint32_t idx;
299         const struct ulp_template_device_tbls *dev_tbls;
300
301         dev_tbls = &mparms->device_params->dev_tbls[mparms->tmpl_type];
302         if (!dev_tbls->result_field_list) {
303                 *num_flds = 0;
304                 *num_encap_flds = 0;
305                 return NULL;
306         }
307
308         idx             = tbl->result_start_idx;
309         *num_flds       = tbl->result_num_fields;
310         *num_encap_flds = tbl->encap_num_fields;
311
312         return &dev_tbls->result_field_list[idx];
313 }
314
315 /*
316  * Get the list of ident fields that implement the flow
317  *
318  * tbl [in] A single table instance to get the ident fields from
319  *
320  * num_flds [out] The number of ident fields in the returned array
321  *
322  * returns array of ident fields, or NULL on error
323  */
324 static struct bnxt_ulp_mapper_ident_info *
325 ulp_mapper_ident_fields_get(struct bnxt_ulp_mapper_parms *mparms,
326                             struct bnxt_ulp_mapper_tbl_info *tbl,
327                             uint32_t *num_flds)
328 {
329         uint32_t idx;
330         const struct ulp_template_device_tbls *dev_tbls;
331
332         dev_tbls = &mparms->device_params->dev_tbls[mparms->tmpl_type];
333         if (!dev_tbls->ident_list) {
334                 *num_flds = 0;
335                 return NULL;
336         }
337
338         idx = tbl->ident_start_idx;
339         *num_flds = tbl->ident_nums;
340
341         return &dev_tbls->ident_list[idx];
342 }
343
344 static struct bnxt_ulp_mapper_cache_entry *
345 ulp_mapper_cache_entry_get(struct bnxt_ulp_context *ulp,
346                            uint32_t id,
347                            uint16_t key)
348 {
349         struct bnxt_ulp_mapper_data *mapper_data;
350
351         mapper_data = bnxt_ulp_cntxt_ptr2_mapper_data_get(ulp);
352         if (!mapper_data || id >= BNXT_ULP_CACHE_TBL_MAX_SZ ||
353             !mapper_data->cache_tbl[id]) {
354                 BNXT_TF_DBG(ERR, "Unable to acquire the cache tbl (%d)\n", id);
355                 return NULL;
356         }
357
358         return &mapper_data->cache_tbl[id][key];
359 }
360
361 /*
362  * Concatenates the tbl_type and tbl_id into a 32bit value for storing in the
363  * resource_type.  This is done to conserve memory since both the tbl_type and
364  * tbl_id are 16bit.
365  */
366 static inline void
367 ulp_mapper_cache_res_type_set(struct ulp_flow_db_res_params *res,
368                               uint16_t tbl_type,
369                               uint16_t tbl_id)
370 {
371         res->resource_type = tbl_type;
372         res->resource_sub_type = tbl_id;
373 }
374
375 /* Extracts the tbl_type and tbl_id from the 32bit resource type. */
376 static inline void
377 ulp_mapper_cache_res_type_get(struct ulp_flow_db_res_params *res,
378                               uint16_t *tbl_type,
379                               uint16_t *tbl_id)
380 {
381         *tbl_type = res->resource_type;
382         *tbl_id = res->resource_sub_type;
383 }
384
385 static int32_t
386 ulp_mapper_cache_entry_free(struct bnxt_ulp_context *ulp,
387                             struct tf *tfp,
388                             struct ulp_flow_db_res_params *res)
389 {
390         struct bnxt_ulp_mapper_cache_entry *cache_entry;
391         struct tf_free_identifier_parms ident_parms;
392         struct tf_free_tcam_entry_parms tcam_parms;
393         uint16_t table_id, table_type;
394         int32_t rc, trc, i;
395
396         /*
397          * The table id, used for cache, and table_type, used for tcam, are
398          * both encoded within the resource.  We must first extract them to
399          * formulate the args for tf calls.
400          */
401         ulp_mapper_cache_res_type_get(res, &table_type, &table_id);
402         cache_entry = ulp_mapper_cache_entry_get(ulp, table_id,
403                                                  (uint16_t)res->resource_hndl);
404         if (!cache_entry || !cache_entry->ref_count) {
405                 BNXT_TF_DBG(ERR, "Cache entry (%d:%d) not valid on free.\n",
406                             table_id, (uint16_t)res->resource_hndl);
407                 return -EINVAL;
408         }
409
410         /*
411          * See if we need to delete the entry.  The tcam and identifiers are all
412          * tracked by the cached entries reference count.  All are deleted when
413          * the reference count hit zero.
414          */
415         cache_entry->ref_count--;
416         if (cache_entry->ref_count)
417                 return 0;
418
419         /*
420          * Need to delete the tcam entry and the allocated identifiers.
421          * In the event of a failure, need to try to delete the remaining
422          * resources before returning error.
423          */
424         tcam_parms.dir = res->direction;
425         tcam_parms.tcam_tbl_type = table_type;
426         tcam_parms.idx = cache_entry->tcam_idx;
427         rc = tf_free_tcam_entry(tfp, &tcam_parms);
428         if (rc)
429                 BNXT_TF_DBG(ERR, "Failed to free tcam [%d][%s][0x%04x] rc=%d\n",
430                             table_type,
431                             (res->direction == TF_DIR_RX) ? "RX" : "TX",
432                             tcam_parms.idx, rc);
433
434         /*
435          * Free the identifiers associated with the tcam entry.  Entries with
436          * negative one are considered uninitialized.
437          */
438         for (i = 0; i < BNXT_ULP_CACHE_TBL_IDENT_MAX_NUM; i++) {
439                 if (cache_entry->idents[i] == ULP_IDENTS_INVALID)
440                         continue;
441
442                 ident_parms.dir = res->direction;
443                 ident_parms.ident_type = cache_entry->ident_types[i];
444                 ident_parms.id = cache_entry->idents[i];
445                 trc = tf_free_identifier(tfp, &ident_parms);
446                 if (trc) {
447                         BNXT_TF_DBG(ERR, "Failed to free identifier "
448                                     "[%d][%s][0x%04x] rc=%d\n",
449                                     ident_parms.ident_type,
450                                     (res->direction == TF_DIR_RX) ? "RX" : "TX",
451                                     ident_parms.id, trc);
452                         rc = trc;
453                 }
454         }
455
456         return rc;
457 }
458
459 static inline int32_t
460 ulp_mapper_tcam_entry_free(struct bnxt_ulp_context *ulp  __rte_unused,
461                            struct tf *tfp,
462                            struct ulp_flow_db_res_params *res)
463 {
464         struct tf_free_tcam_entry_parms fparms = {
465                 .dir            = res->direction,
466                 .tcam_tbl_type  = res->resource_type,
467                 .idx            = (uint16_t)res->resource_hndl
468         };
469
470         return tf_free_tcam_entry(tfp, &fparms);
471 }
472
473 static inline int32_t
474 ulp_mapper_index_entry_free(struct bnxt_ulp_context *ulp,
475                             struct tf *tfp,
476                             struct ulp_flow_db_res_params *res)
477 {
478         struct tf_free_tbl_entry_parms fparms = {
479                 .dir    = res->direction,
480                 .type   = res->resource_type,
481                 .idx    = (uint32_t)res->resource_hndl
482         };
483
484         /*
485          * Just get the table scope, it will be ignored if not necessary
486          * by the tf_free_tbl_entry
487          */
488         (void)bnxt_ulp_cntxt_tbl_scope_id_get(ulp, &fparms.tbl_scope_id);
489
490         return tf_free_tbl_entry(tfp, &fparms);
491 }
492
493 static inline int32_t
494 ulp_mapper_em_entry_free(struct bnxt_ulp_context *ulp,
495                          struct tf *tfp,
496                          struct ulp_flow_db_res_params *res)
497 {
498         struct tf_delete_em_entry_parms fparms = { 0 };
499         int32_t rc;
500
501         fparms.dir              = res->direction;
502         if (res->resource_func == BNXT_ULP_RESOURCE_FUNC_EXT_EM_TABLE)
503                 fparms.mem = TF_MEM_EXTERNAL;
504         else
505                 fparms.mem = TF_MEM_INTERNAL;
506         fparms.flow_handle      = res->resource_hndl;
507
508         rc = bnxt_ulp_cntxt_tbl_scope_id_get(ulp, &fparms.tbl_scope_id);
509         if (rc) {
510                 BNXT_TF_DBG(ERR, "Failed to get table scope\n");
511                 return -EINVAL;
512         }
513
514         return tf_delete_em_entry(tfp, &fparms);
515 }
516
517 static inline int32_t
518 ulp_mapper_ident_free(struct bnxt_ulp_context *ulp __rte_unused,
519                       struct tf *tfp,
520                       struct ulp_flow_db_res_params *res)
521 {
522         struct tf_free_identifier_parms fparms = {
523                 .dir            = res->direction,
524                 .ident_type     = res->resource_type,
525                 .id             = (uint16_t)res->resource_hndl
526         };
527
528         return tf_free_identifier(tfp, &fparms);
529 }
530
531 static inline int32_t
532 ulp_mapper_mark_free(struct bnxt_ulp_context *ulp,
533                      struct ulp_flow_db_res_params *res)
534 {
535         return ulp_mark_db_mark_del(ulp,
536                                     res->resource_type,
537                                     res->resource_hndl);
538 }
539
540 /*
541  * Process the identifier instruction and either store it in the flow database
542  * or return it in the val (if not NULL) on success.  If val is NULL, the
543  * identifier is to be stored in the flow database.
544  */
545 static int32_t
546 ulp_mapper_ident_process(struct bnxt_ulp_mapper_parms *parms,
547                          struct bnxt_ulp_mapper_tbl_info *tbl,
548                          struct bnxt_ulp_mapper_ident_info *ident,
549                          uint16_t *val)
550 {
551         struct ulp_flow_db_res_params   fid_parms;
552         uint64_t id = 0;
553         int32_t idx;
554         struct tf_alloc_identifier_parms iparms = { 0 };
555         struct tf_free_identifier_parms free_parms = { 0 };
556         struct tf *tfp;
557         int rc;
558
559         tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx);
560         if (!tfp) {
561                 BNXT_TF_DBG(ERR, "Failed to get tf pointer\n");
562                 return -EINVAL;
563         }
564
565         idx = ident->regfile_idx;
566
567         iparms.ident_type = ident->ident_type;
568         iparms.dir = tbl->direction;
569
570         rc = tf_alloc_identifier(tfp, &iparms);
571         if (rc) {
572                 BNXT_TF_DBG(ERR, "Alloc ident %s:%d failed.\n",
573                             (iparms.dir == TF_DIR_RX) ? "RX" : "TX",
574                             iparms.ident_type);
575                 return rc;
576         }
577
578         id = (uint64_t)tfp_cpu_to_be_64(iparms.id);
579         if (!ulp_regfile_write(parms->regfile, idx, id)) {
580                 BNXT_TF_DBG(ERR, "Regfile[%d] write failed.\n", idx);
581                 rc = -EINVAL;
582                 /* Need to free the identifier, so goto error */
583                 goto error;
584         }
585
586         /* Link the resource to the flow in the flow db */
587         if (!val) {
588                 memset(&fid_parms, 0, sizeof(fid_parms));
589                 fid_parms.direction             = tbl->direction;
590                 fid_parms.resource_func = ident->resource_func;
591                 fid_parms.resource_type = ident->ident_type;
592                 fid_parms.resource_hndl = iparms.id;
593                 fid_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
594
595                 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
596                                               parms->flow_type,
597                                               parms->fid,
598                                               &fid_parms);
599                 if (rc) {
600                         BNXT_TF_DBG(ERR, "Failed to link res to flow rc = %d\n",
601                                     rc);
602                         /* Need to free the identifier, so goto error */
603                         goto error;
604                 }
605         } else {
606                 *val = iparms.id;
607         }
608
609         return 0;
610
611 error:
612         /* Need to free the identifier */
613         free_parms.dir          = tbl->direction;
614         free_parms.ident_type   = ident->ident_type;
615         free_parms.id           = iparms.id;
616
617         (void)tf_free_identifier(tfp, &free_parms);
618
619         BNXT_TF_DBG(ERR, "Ident process failed for %s:%s\n",
620                     ident->description,
621                     (tbl->direction == TF_DIR_RX) ? "RX" : "TX");
622         return rc;
623 }
624
625 /*
626  * Process the identifier instruction and extract it from result blob.
627  * Increment the identifier reference count and store it in the flow database.
628  */
629 static int32_t
630 ulp_mapper_ident_extract(struct bnxt_ulp_mapper_parms *parms,
631                          struct bnxt_ulp_mapper_tbl_info *tbl,
632                          struct bnxt_ulp_mapper_ident_info *ident,
633                          struct ulp_blob *res_blob)
634 {
635         struct ulp_flow_db_res_params   fid_parms;
636         uint64_t id = 0;
637         uint32_t idx = 0;
638         struct tf_search_identifier_parms sparms = { 0 };
639         struct tf_free_identifier_parms free_parms = { 0 };
640         struct tf *tfp;
641         int rc;
642
643         /* Get the tfp from ulp context */
644         tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx);
645         if (!tfp) {
646                 BNXT_TF_DBG(ERR, "Failed to get tf pointer\n");
647                 return -EINVAL;
648         }
649
650         /* Extract the index from the result blob */
651         rc = ulp_blob_pull(res_blob, (uint8_t *)&idx, sizeof(idx),
652                            ident->ident_bit_pos, ident->ident_bit_size);
653         if (rc) {
654                 BNXT_TF_DBG(ERR, "Failed to extract identifier from blob\n");
655                 return -EIO;
656         }
657
658         /* populate the search params and search identifier shadow table */
659         sparms.ident_type = ident->ident_type;
660         sparms.dir = tbl->direction;
661         /* convert the idx into cpu format */
662         sparms.search_id = tfp_be_to_cpu_32(idx);
663
664         /* Search identifier also increase the reference count */
665         rc = tf_search_identifier(tfp, &sparms);
666         if (rc) {
667                 BNXT_TF_DBG(ERR, "Search ident %s:%x failed.\n",
668                             tf_dir_2_str(sparms.dir),
669                             sparms.search_id);
670                 return rc;
671         }
672         BNXT_TF_DBG(INFO, "Search ident %s:%x.success.\n",
673                     tf_dir_2_str(sparms.dir),
674                     sparms.search_id);
675
676         /* Write it to the regfile */
677         id = (uint64_t)tfp_cpu_to_be_64(sparms.search_id);
678         if (!ulp_regfile_write(parms->regfile, ident->regfile_idx, id)) {
679                 BNXT_TF_DBG(ERR, "Regfile[%d] write failed.\n", idx);
680                 rc = -EINVAL;
681                 /* Need to free the identifier, so goto error */
682                 goto error;
683         }
684
685         /* Link the resource to the flow in the flow db */
686         memset(&fid_parms, 0, sizeof(fid_parms));
687         fid_parms.direction = tbl->direction;
688         fid_parms.resource_func = ident->resource_func;
689         fid_parms.resource_type = ident->ident_type;
690         fid_parms.resource_hndl = sparms.search_id;
691         fid_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
692         rc = ulp_flow_db_resource_add(parms->ulp_ctx,
693                                       parms->flow_type,
694                                       parms->fid,
695                                       &fid_parms);
696         if (rc) {
697                 BNXT_TF_DBG(ERR, "Failed to link res to flow rc = %d\n",
698                             rc);
699                 /* Need to free the identifier, so goto error */
700                 goto error;
701         }
702
703         return 0;
704
705 error:
706         /* Need to free the identifier */
707         free_parms.dir = tbl->direction;
708         free_parms.ident_type = ident->ident_type;
709         free_parms.id = sparms.search_id;
710         (void)tf_free_identifier(tfp, &free_parms);
711         BNXT_TF_DBG(ERR, "Ident extract failed for %s:%s:%x\n",
712                     ident->description,
713                     tf_dir_2_str(tbl->direction), sparms.search_id);
714         return rc;
715 }
716
717 static int32_t
718 ulp_mapper_result_field_process(struct bnxt_ulp_mapper_parms *parms,
719                                 enum tf_dir dir,
720                                 struct bnxt_ulp_mapper_result_field_info *fld,
721                                 struct ulp_blob *blob,
722                                 const char *name)
723 {
724         uint16_t idx, size_idx;
725         uint8_t  *val = NULL;
726         uint64_t regval;
727         uint32_t val_size = 0, field_size = 0;
728         uint64_t act_bit;
729         uint8_t act_val[16];
730         uint64_t hdr_bit;
731
732         switch (fld->result_opcode) {
733         case BNXT_ULP_MAPPER_OPC_SET_TO_CONSTANT:
734                 val = fld->result_operand;
735                 if (!ulp_blob_push(blob, val, fld->field_bit_size)) {
736                         BNXT_TF_DBG(ERR, "%s failed to add field\n", name);
737                         return -EINVAL;
738                 }
739                 break;
740         case BNXT_ULP_MAPPER_OPC_SET_TO_ACT_PROP:
741                 if (!ulp_operand_read(fld->result_operand,
742                                       (uint8_t *)&idx, sizeof(uint16_t))) {
743                         BNXT_TF_DBG(ERR, "%s operand read failed\n", name);
744                         return -EINVAL;
745                 }
746                 idx = tfp_be_to_cpu_16(idx);
747
748                 if (idx >= BNXT_ULP_ACT_PROP_IDX_LAST) {
749                         BNXT_TF_DBG(ERR, "%s act_prop[%d] oob\n", name, idx);
750                         return -EINVAL;
751                 }
752                 val = &parms->act_prop->act_details[idx];
753                 field_size = ulp_mapper_act_prop_size_get(idx);
754                 if (fld->field_bit_size < ULP_BYTE_2_BITS(field_size)) {
755                         field_size  = field_size -
756                             ((fld->field_bit_size + 7) / 8);
757                         val += field_size;
758                 }
759                 if (!ulp_blob_push(blob, val, fld->field_bit_size)) {
760                         BNXT_TF_DBG(ERR, "%s push field failed\n", name);
761                         return -EINVAL;
762                 }
763                 break;
764         case BNXT_ULP_MAPPER_OPC_SET_TO_ACT_BIT:
765                 if (!ulp_operand_read(fld->result_operand,
766                                       (uint8_t *)&act_bit, sizeof(uint64_t))) {
767                         BNXT_TF_DBG(ERR, "%s operand read failed\n", name);
768                         return -EINVAL;
769                 }
770                 act_bit = tfp_be_to_cpu_64(act_bit);
771                 memset(act_val, 0, sizeof(act_val));
772                 if (ULP_BITMAP_ISSET(parms->act_bitmap->bits, act_bit))
773                         act_val[0] = 1;
774                 if (fld->field_bit_size > ULP_BYTE_2_BITS(sizeof(act_val))) {
775                         BNXT_TF_DBG(ERR, "%s field size is incorrect\n", name);
776                         return -EINVAL;
777                 }
778                 if (!ulp_blob_push(blob, act_val, fld->field_bit_size)) {
779                         BNXT_TF_DBG(ERR, "%s push field failed\n", name);
780                         return -EINVAL;
781                 }
782                 val = act_val;
783                 break;
784         case BNXT_ULP_MAPPER_OPC_SET_TO_ENCAP_ACT_PROP_SZ:
785                 if (!ulp_operand_read(fld->result_operand,
786                                       (uint8_t *)&idx, sizeof(uint16_t))) {
787                         BNXT_TF_DBG(ERR, "%s operand read failed\n", name);
788                         return -EINVAL;
789                 }
790                 idx = tfp_be_to_cpu_16(idx);
791
792                 if (idx >= BNXT_ULP_ACT_PROP_IDX_LAST) {
793                         BNXT_TF_DBG(ERR, "%s act_prop[%d] oob\n", name, idx);
794                         return -EINVAL;
795                 }
796                 val = &parms->act_prop->act_details[idx];
797
798                 /* get the size index next */
799                 if (!ulp_operand_read(&fld->result_operand[sizeof(uint16_t)],
800                                       (uint8_t *)&size_idx, sizeof(uint16_t))) {
801                         BNXT_TF_DBG(ERR, "%s operand read failed\n", name);
802                         return -EINVAL;
803                 }
804                 size_idx = tfp_be_to_cpu_16(size_idx);
805
806                 if (size_idx >= BNXT_ULP_ACT_PROP_IDX_LAST) {
807                         BNXT_TF_DBG(ERR, "act_prop[%d] oob\n", size_idx);
808                         return -EINVAL;
809                 }
810                 memcpy(&val_size, &parms->act_prop->act_details[size_idx],
811                        sizeof(uint32_t));
812                 val_size = tfp_be_to_cpu_32(val_size);
813                 val_size = ULP_BYTE_2_BITS(val_size);
814                 ulp_blob_push_encap(blob, val, val_size);
815                 break;
816         case BNXT_ULP_MAPPER_OPC_SET_TO_REGFILE:
817                 if (!ulp_operand_read(fld->result_operand,
818                                       (uint8_t *)&idx, sizeof(uint16_t))) {
819                         BNXT_TF_DBG(ERR, "%s operand read failed\n", name);
820                         return -EINVAL;
821                 }
822
823                 idx = tfp_be_to_cpu_16(idx);
824                 /* Uninitialized regfile entries return 0 */
825                 if (!ulp_regfile_read(parms->regfile, idx, &regval)) {
826                         BNXT_TF_DBG(ERR, "%s regfile[%d] read oob\n",
827                                     name, idx);
828                         return -EINVAL;
829                 }
830
831                 val = ulp_blob_push_64(blob, &regval, fld->field_bit_size);
832                 if (!val) {
833                         BNXT_TF_DBG(ERR, "%s push field failed\n", name);
834                         return -EINVAL;
835                 }
836                 break;
837         case BNXT_ULP_MAPPER_OPC_SET_TO_GLB_REGFILE:
838                 if (!ulp_operand_read(fld->result_operand,
839                                       (uint8_t *)&idx,
840                                       sizeof(uint16_t))) {
841                         BNXT_TF_DBG(ERR, "%s key operand read failed.\n", name);
842                         return -EINVAL;
843                 }
844                 idx = tfp_be_to_cpu_16(idx);
845                 if (ulp_mapper_glb_resource_read(parms->mapper_data,
846                                                  dir,
847                                                  idx, &regval)) {
848                         BNXT_TF_DBG(ERR, "%s regfile[%d] read failed.\n",
849                                     name, idx);
850                         return -EINVAL;
851                 }
852                 val = ulp_blob_push_64(blob, &regval, fld->field_bit_size);
853                 if (!val) {
854                         BNXT_TF_DBG(ERR, "%s push to key blob failed\n", name);
855                         return -EINVAL;
856                 }
857                 break;
858         case BNXT_ULP_MAPPER_OPC_SET_TO_COMP_FIELD:
859                 if (!ulp_operand_read(fld->result_operand,
860                                       (uint8_t *)&idx,
861                                       sizeof(uint16_t))) {
862                         BNXT_TF_DBG(ERR, "%s key operand read failed.\n", name);
863                         return -EINVAL;
864                 }
865                 idx = tfp_be_to_cpu_16(idx);
866                 if (idx < BNXT_ULP_CF_IDX_LAST)
867                         val = ulp_blob_push_32(blob, &parms->comp_fld[idx],
868                                                fld->field_bit_size);
869                 if (!val) {
870                         BNXT_TF_DBG(ERR, "%s push to key blob failed\n", name);
871                         return -EINVAL;
872                 }
873                 break;
874         case BNXT_ULP_MAPPER_OPC_SET_TO_ZERO:
875                 if (ulp_blob_pad_push(blob, fld->field_bit_size) < 0) {
876                         BNXT_TF_DBG(ERR, "%s too large for blob\n", name);
877                         return -EINVAL;
878                 }
879
880                 break;
881         case BNXT_ULP_MAPPER_OPC_IF_ACT_BIT_THEN_ACT_PROP_ELSE_CONST:
882                 if (!ulp_operand_read(fld->result_operand,
883                                       (uint8_t *)&act_bit, sizeof(uint64_t))) {
884                         BNXT_TF_DBG(ERR, "%s operand read failed\n", name);
885                         return -EINVAL;
886                 }
887                 act_bit = tfp_be_to_cpu_64(act_bit);
888                 if (ULP_BITMAP_ISSET(parms->act_bitmap->bits, act_bit)) {
889                         /* Action bit is set so consider operand_true */
890                         if (!ulp_operand_read(fld->result_operand_true,
891                                               (uint8_t *)&idx,
892                                               sizeof(uint16_t))) {
893                                 BNXT_TF_DBG(ERR, "%s operand read failed\n",
894                                             name);
895                                 return -EINVAL;
896                         }
897                         idx = tfp_be_to_cpu_16(idx);
898                         if (idx >= BNXT_ULP_ACT_PROP_IDX_LAST) {
899                                 BNXT_TF_DBG(ERR, "%s act_prop[%d] oob\n",
900                                             name, idx);
901                                 return -EINVAL;
902                         }
903                         val = &parms->act_prop->act_details[idx];
904                         field_size = ulp_mapper_act_prop_size_get(idx);
905                         if (fld->field_bit_size < ULP_BYTE_2_BITS(field_size)) {
906                                 field_size  = field_size -
907                                     ((fld->field_bit_size + 7) / 8);
908                                 val += field_size;
909                         }
910                         if (!ulp_blob_push(blob, val, fld->field_bit_size)) {
911                                 BNXT_TF_DBG(ERR, "%s push field failed\n",
912                                             name);
913                                 return -EINVAL;
914                         }
915                 } else {
916                         /* action bit is not set, use the operand false */
917                         val = fld->result_operand_false;
918                         if (!ulp_blob_push(blob, val, fld->field_bit_size)) {
919                                 BNXT_TF_DBG(ERR, "%s failed to add field\n",
920                                             name);
921                                 return -EINVAL;
922                         }
923                 }
924                 break;
925         case BNXT_ULP_MAPPER_OPC_IF_ACT_BIT_THEN_CONST_ELSE_CONST:
926                 if (!ulp_operand_read(fld->result_operand,
927                                       (uint8_t *)&act_bit, sizeof(uint64_t))) {
928                         BNXT_TF_DBG(ERR, "%s operand read failed\n", name);
929                         return -EINVAL;
930                 }
931                 act_bit = tfp_be_to_cpu_64(act_bit);
932                 if (ULP_BITMAP_ISSET(parms->act_bitmap->bits, act_bit)) {
933                         /* Action bit is set so consider operand_true */
934                         val = fld->result_operand_true;
935                 } else {
936                         /* action bit is not set, use the operand false */
937                         val = fld->result_operand_false;
938                 }
939                 if (!ulp_blob_push(blob, val, fld->field_bit_size)) {
940                         BNXT_TF_DBG(ERR, "%s failed to add field\n",
941                                     name);
942                         return -EINVAL;
943                 }
944                 break;
945         case BNXT_ULP_MAPPER_OPC_IF_COMP_FIELD_THEN_CF_ELSE_CF:
946                 if (!ulp_operand_read(fld->result_operand,
947                                       (uint8_t *)&idx,
948                                       sizeof(uint16_t))) {
949                         BNXT_TF_DBG(ERR, "%s key operand read failed.\n", name);
950                         return -EINVAL;
951                 }
952                 idx = tfp_be_to_cpu_16(idx);
953                 if (idx >= BNXT_ULP_CF_IDX_LAST) {
954                         BNXT_TF_DBG(ERR, "%s invalid index %u\n", name, idx);
955                         return -EINVAL;
956                 }
957                 /* check if the computed field is set */
958                 if (ULP_COMP_FLD_IDX_RD(parms, idx))
959                         val = fld->result_operand_true;
960                 else
961                         val = fld->result_operand_false;
962
963                 /* read the appropriate computed field */
964                 if (!ulp_operand_read(val, (uint8_t *)&idx, sizeof(uint16_t))) {
965                         BNXT_TF_DBG(ERR, "%s val operand read failed\n", name);
966                         return -EINVAL;
967                 }
968                 idx = tfp_be_to_cpu_16(idx);
969                 if (idx >= BNXT_ULP_CF_IDX_LAST) {
970                         BNXT_TF_DBG(ERR, "%s invalid index %u\n", name, idx);
971                         return -EINVAL;
972                 }
973                 val = ulp_blob_push_32(blob, &parms->comp_fld[idx],
974                                        fld->field_bit_size);
975                 if (!val) {
976                         BNXT_TF_DBG(ERR, "%s push to key blob failed\n", name);
977                         return -EINVAL;
978                 }
979                 break;
980         case BNXT_ULP_MAPPER_OPC_IF_HDR_BIT_THEN_CONST_ELSE_CONST:
981                 if (!ulp_operand_read(fld->result_operand,
982                                       (uint8_t *)&hdr_bit, sizeof(uint64_t))) {
983                         BNXT_TF_DBG(ERR, "%s operand read failed\n", name);
984                         return -EINVAL;
985                 }
986                 hdr_bit = tfp_be_to_cpu_64(hdr_bit);
987                 if (ULP_BITMAP_ISSET(parms->hdr_bitmap->bits, hdr_bit)) {
988                         /* Header bit is set so consider operand_true */
989                         val = fld->result_operand_true;
990                 } else {
991                         /* Header bit is not set, use the operand false */
992                         val = fld->result_operand_false;
993                 }
994                 if (!ulp_blob_push(blob, val, fld->field_bit_size)) {
995                         BNXT_TF_DBG(ERR, "%s failed to add field\n",
996                                     name);
997                         return -EINVAL;
998                 }
999                 break;
1000         default:
1001                 BNXT_TF_DBG(ERR, "invalid result mapper opcode 0x%x\n",
1002                             fld->result_opcode);
1003                 return -EINVAL;
1004         }
1005         return 0;
1006 }
1007
1008 /* Function to alloc action record and set the table. */
1009 static int32_t
1010 ulp_mapper_keymask_field_process(struct bnxt_ulp_mapper_parms *parms,
1011                                  enum tf_dir dir,
1012                                  struct bnxt_ulp_mapper_class_key_field_info *f,
1013                                  struct ulp_blob *blob,
1014                                  uint8_t is_key,
1015                                  const char *name)
1016 {
1017         uint64_t val64;
1018         uint16_t idx, bitlen;
1019         uint32_t opcode;
1020         uint8_t *operand;
1021         struct ulp_regfile *regfile = parms->regfile;
1022         uint8_t *val = NULL;
1023         struct bnxt_ulp_mapper_class_key_field_info *fld = f;
1024         uint32_t field_size;
1025
1026         if (is_key) {
1027                 operand = fld->spec_operand;
1028                 opcode  = fld->spec_opcode;
1029         } else {
1030                 operand = fld->mask_operand;
1031                 opcode  = fld->mask_opcode;
1032         }
1033
1034         bitlen = fld->field_bit_size;
1035
1036         switch (opcode) {
1037         case BNXT_ULP_MAPPER_OPC_SET_TO_CONSTANT:
1038                 val = operand;
1039                 if (!ulp_blob_push(blob, val, bitlen)) {
1040                         BNXT_TF_DBG(ERR, "%s push to key blob failed\n", name);
1041                         return -EINVAL;
1042                 }
1043                 break;
1044         case BNXT_ULP_MAPPER_OPC_SET_TO_ZERO:
1045                 if (ulp_blob_pad_push(blob, bitlen) < 0) {
1046                         BNXT_TF_DBG(ERR, "%s pad too large for blob\n", name);
1047                         return -EINVAL;
1048                 }
1049
1050                 break;
1051         case BNXT_ULP_MAPPER_OPC_SET_TO_HDR_FIELD:
1052                 if (!ulp_operand_read(operand, (uint8_t *)&idx,
1053                                       sizeof(uint16_t))) {
1054                         BNXT_TF_DBG(ERR, "%s key operand read failed.\n", name);
1055                         return -EINVAL;
1056                 }
1057                 idx = tfp_be_to_cpu_16(idx);
1058                 if (is_key)
1059                         val = parms->hdr_field[idx].spec;
1060                 else
1061                         val = parms->hdr_field[idx].mask;
1062
1063                 /*
1064                  * Need to account for how much data was pushed to the header
1065                  * field vs how much is to be inserted in the key/mask.
1066                  */
1067                 field_size = parms->hdr_field[idx].size;
1068                 if (bitlen < ULP_BYTE_2_BITS(field_size)) {
1069                         field_size  = field_size - ((bitlen + 7) / 8);
1070                         val += field_size;
1071                 }
1072
1073                 if (!ulp_blob_push(blob, val, bitlen)) {
1074                         BNXT_TF_DBG(ERR, "%s push to key blob failed\n", name);
1075                         return -EINVAL;
1076                 }
1077                 break;
1078         case BNXT_ULP_MAPPER_OPC_SET_TO_COMP_FIELD:
1079                 if (!ulp_operand_read(operand, (uint8_t *)&idx,
1080                                       sizeof(uint16_t))) {
1081                         BNXT_TF_DBG(ERR, "%s key operand read failed.\n", name);
1082                         return -EINVAL;
1083                 }
1084                 idx = tfp_be_to_cpu_16(idx);
1085                 if (idx < BNXT_ULP_CF_IDX_LAST)
1086                         val = ulp_blob_push_32(blob, &parms->comp_fld[idx],
1087                                                bitlen);
1088                 if (!val) {
1089                         BNXT_TF_DBG(ERR, "%s push to key blob failed\n", name);
1090                         return -EINVAL;
1091                 }
1092                 break;
1093         case BNXT_ULP_MAPPER_OPC_SET_TO_REGFILE:
1094                 if (!ulp_operand_read(operand, (uint8_t *)&idx,
1095                                       sizeof(uint16_t))) {
1096                         BNXT_TF_DBG(ERR, "%s key operand read failed.\n", name);
1097                         return -EINVAL;
1098                 }
1099                 idx = tfp_be_to_cpu_16(idx);
1100
1101                 if (!ulp_regfile_read(regfile, idx, &val64)) {
1102                         BNXT_TF_DBG(ERR, "%s regfile[%d] read failed.\n",
1103                                     name, idx);
1104                         return -EINVAL;
1105                 }
1106
1107                 val = ulp_blob_push_64(blob, &val64, bitlen);
1108                 if (!val) {
1109                         BNXT_TF_DBG(ERR, "%s push to key blob failed\n", name);
1110                         return -EINVAL;
1111                 }
1112                 break;
1113         case BNXT_ULP_MAPPER_OPC_SET_TO_GLB_REGFILE:
1114                 if (!ulp_operand_read(operand, (uint8_t *)&idx,
1115                                       sizeof(uint16_t))) {
1116                         BNXT_TF_DBG(ERR, "%s key operand read failed.\n", name);
1117                         return -EINVAL;
1118                 }
1119                 idx = tfp_be_to_cpu_16(idx);
1120                 if (ulp_mapper_glb_resource_read(parms->mapper_data,
1121                                                  dir,
1122                                                  idx, &val64)) {
1123                         BNXT_TF_DBG(ERR, "%s regfile[%d] read failed.\n",
1124                                     name, idx);
1125                         return -EINVAL;
1126                 }
1127                 val = ulp_blob_push_64(blob, &val64, bitlen);
1128                 if (!val) {
1129                         BNXT_TF_DBG(ERR, "%s push to key blob failed\n", name);
1130                         return -EINVAL;
1131                 }
1132                 break;
1133         default:
1134                 BNXT_TF_DBG(ERR, "invalid keymask mapper opcode 0x%x\n",
1135                             opcode);
1136                 return -EINVAL;
1137                 break;
1138         }
1139
1140         return 0;
1141 }
1142
1143 static int32_t
1144 ulp_mapper_mark_gfid_process(struct bnxt_ulp_mapper_parms *parms,
1145                              struct bnxt_ulp_mapper_tbl_info *tbl,
1146                              uint64_t flow_id)
1147 {
1148         enum bnxt_ulp_mark_db_opcode mark_op = tbl->mark_db_opcode;
1149         struct ulp_flow_db_res_params fid_parms;
1150         uint32_t mark, gfid, mark_flag;
1151         int32_t rc = 0;
1152
1153         if (mark_op == BNXT_ULP_MARK_DB_OPCODE_NOP ||
1154             !(mark_op == BNXT_ULP_MARK_DB_OPCODE_SET_IF_MARK_ACTION &&
1155               ULP_BITMAP_ISSET(parms->act_bitmap->bits,
1156                                BNXT_ULP_ACTION_BIT_MARK)))
1157                 return rc; /* no need to perform gfid process */
1158
1159         /* Get the mark id details from action property */
1160         memcpy(&mark, &parms->act_prop->act_details[BNXT_ULP_ACT_PROP_IDX_MARK],
1161                sizeof(mark));
1162         mark = tfp_be_to_cpu_32(mark);
1163
1164         TF_GET_GFID_FROM_FLOW_ID(flow_id, gfid);
1165         mark_flag  = BNXT_ULP_MARK_GLOBAL_HW_FID;
1166         rc = ulp_mark_db_mark_add(parms->ulp_ctx, mark_flag,
1167                                   gfid, mark);
1168         if (rc) {
1169                 BNXT_TF_DBG(ERR, "Failed to add mark to flow\n");
1170                 return rc;
1171         }
1172         fid_parms.direction = tbl->direction;
1173         fid_parms.resource_func = BNXT_ULP_RESOURCE_FUNC_HW_FID;
1174         fid_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
1175         fid_parms.resource_type = mark_flag;
1176         fid_parms.resource_hndl = gfid;
1177         rc = ulp_flow_db_resource_add(parms->ulp_ctx,
1178                                       parms->flow_type,
1179                                       parms->fid,
1180                                       &fid_parms);
1181         if (rc)
1182                 BNXT_TF_DBG(ERR, "Fail to link res to flow rc = %d\n", rc);
1183         return rc;
1184 }
1185
1186 static int32_t
1187 ulp_mapper_mark_act_ptr_process(struct bnxt_ulp_mapper_parms *parms,
1188                                 struct bnxt_ulp_mapper_tbl_info *tbl)
1189 {
1190         enum bnxt_ulp_mark_db_opcode mark_op = tbl->mark_db_opcode;
1191         struct ulp_flow_db_res_params fid_parms;
1192         uint32_t act_idx, mark, mark_flag;
1193         uint64_t val64;
1194         int32_t rc = 0;
1195
1196         if (mark_op == BNXT_ULP_MARK_DB_OPCODE_NOP ||
1197             !(mark_op == BNXT_ULP_MARK_DB_OPCODE_SET_IF_MARK_ACTION &&
1198               ULP_BITMAP_ISSET(parms->act_bitmap->bits,
1199                                BNXT_ULP_ACTION_BIT_MARK)))
1200                 return rc; /* no need to perform mark action process */
1201
1202         /* Get the mark id details from action property */
1203         memcpy(&mark, &parms->act_prop->act_details[BNXT_ULP_ACT_PROP_IDX_MARK],
1204                sizeof(mark));
1205         mark = tfp_be_to_cpu_32(mark);
1206
1207         if (!ulp_regfile_read(parms->regfile,
1208                               BNXT_ULP_REGFILE_INDEX_MAIN_ACTION_PTR,
1209                               &val64)) {
1210                 BNXT_TF_DBG(ERR, "read action ptr main failed\n");
1211                 return -EINVAL;
1212         }
1213         act_idx = tfp_be_to_cpu_64(val64);
1214         mark_flag  = BNXT_ULP_MARK_LOCAL_HW_FID;
1215         rc = ulp_mark_db_mark_add(parms->ulp_ctx, mark_flag,
1216                                   act_idx, mark);
1217         if (rc) {
1218                 BNXT_TF_DBG(ERR, "Failed to add mark to flow\n");
1219                 return rc;
1220         }
1221         fid_parms.direction = tbl->direction;
1222         fid_parms.resource_func = BNXT_ULP_RESOURCE_FUNC_HW_FID;
1223         fid_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
1224         fid_parms.resource_type = mark_flag;
1225         fid_parms.resource_hndl = act_idx;
1226         rc = ulp_flow_db_resource_add(parms->ulp_ctx,
1227                                       parms->flow_type,
1228                                       parms->fid,
1229                                       &fid_parms);
1230         if (rc)
1231                 BNXT_TF_DBG(ERR, "Fail to link res to flow rc = %d\n", rc);
1232         return rc;
1233 }
1234
1235 static int32_t
1236 ulp_mapper_mark_vfr_idx_process(struct bnxt_ulp_mapper_parms *parms,
1237                                 struct bnxt_ulp_mapper_tbl_info *tbl)
1238 {
1239         struct ulp_flow_db_res_params fid_parms;
1240         uint32_t act_idx, mark, mark_flag;
1241         uint64_t val64;
1242         enum bnxt_ulp_mark_db_opcode mark_op = tbl->mark_db_opcode;
1243         int32_t rc = 0;
1244
1245         if (mark_op == BNXT_ULP_MARK_DB_OPCODE_NOP ||
1246             mark_op == BNXT_ULP_MARK_DB_OPCODE_SET_IF_MARK_ACTION)
1247                 return rc; /* no need to perform mark action process */
1248
1249         /* Get the mark id details from the computed field of dev port id */
1250         mark = ULP_COMP_FLD_IDX_RD(parms, BNXT_ULP_CF_IDX_DEV_PORT_ID);
1251
1252          /* Get the main action pointer */
1253         if (!ulp_regfile_read(parms->regfile,
1254                               BNXT_ULP_REGFILE_INDEX_MAIN_ACTION_PTR,
1255                               &val64)) {
1256                 BNXT_TF_DBG(ERR, "read action ptr main failed\n");
1257                 return -EINVAL;
1258         }
1259         act_idx = tfp_be_to_cpu_64(val64);
1260
1261         /* Set the mark flag to local fid and vfr flag */
1262         mark_flag  = BNXT_ULP_MARK_LOCAL_HW_FID | BNXT_ULP_MARK_VFR_ID;
1263
1264         rc = ulp_mark_db_mark_add(parms->ulp_ctx, mark_flag,
1265                                   act_idx, mark);
1266         if (rc) {
1267                 BNXT_TF_DBG(ERR, "Failed to add mark to flow\n");
1268                 return rc;
1269         }
1270         fid_parms.direction = tbl->direction;
1271         fid_parms.resource_func = BNXT_ULP_RESOURCE_FUNC_HW_FID;
1272         fid_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
1273         fid_parms.resource_type = mark_flag;
1274         fid_parms.resource_hndl = act_idx;
1275         rc = ulp_flow_db_resource_add(parms->ulp_ctx,
1276                                       parms->flow_type,
1277                                       parms->fid,
1278                                       &fid_parms);
1279         if (rc)
1280                 BNXT_TF_DBG(ERR, "Fail to link res to flow rc = %d\n", rc);
1281         return rc;
1282 }
1283
1284 /*
1285  * Tcam table - create the result blob.
1286  * data [out] - the result blob data
1287  */
1288 static int32_t
1289 ulp_mapper_tcam_tbl_result_create(struct bnxt_ulp_mapper_parms *parms,
1290                                   struct bnxt_ulp_mapper_tbl_info *tbl,
1291                                   struct ulp_blob *data)
1292 {
1293         struct bnxt_ulp_mapper_result_field_info *dflds;
1294         uint32_t num_dflds;
1295         uint32_t encap_flds = 0;
1296         uint32_t i;
1297         int32_t rc = 0;
1298
1299         /* Create the result data blob */
1300         dflds = ulp_mapper_result_fields_get(parms, tbl, &num_dflds,
1301                                              &encap_flds);
1302         if (!dflds || !num_dflds || encap_flds) {
1303                 BNXT_TF_DBG(ERR, "Failed to get data fields.\n");
1304                 return -EINVAL;
1305         }
1306
1307         for (i = 0; i < num_dflds; i++) {
1308                 rc = ulp_mapper_result_field_process(parms,
1309                                                      tbl->direction,
1310                                                      &dflds[i],
1311                                                      data,
1312                                                      "TCAM Result");
1313                 if (rc) {
1314                         BNXT_TF_DBG(ERR, "Failed to set data fields\n");
1315                         return -EINVAL;
1316                 }
1317         }
1318         return rc;
1319 }
1320
1321 /* Tcam table scan the identifier list and allocate each identifier */
1322 static int32_t
1323 ulp_mapper_tcam_tbl_scan_ident_alloc(struct bnxt_ulp_mapper_parms *parms,
1324                                      struct bnxt_ulp_mapper_tbl_info *tbl)
1325 {
1326         struct bnxt_ulp_mapper_ident_info *idents;
1327         uint32_t num_idents;
1328         uint32_t i;
1329
1330         /*
1331          * Since the cache entry is responsible for allocating
1332          * identifiers when in use, allocate the identifiers only
1333          * during normal processing.
1334          */
1335         if (parms->tcam_tbl_opc ==
1336             BNXT_ULP_MAPPER_TCAM_TBL_OPC_NORMAL) {
1337                 idents = ulp_mapper_ident_fields_get(parms, tbl, &num_idents);
1338
1339                 for (i = 0; i < num_idents; i++) {
1340                         if (ulp_mapper_ident_process(parms, tbl,
1341                                                      &idents[i], NULL))
1342                                 return -EINVAL;
1343                 }
1344         }
1345         return 0;
1346 }
1347
1348 /*
1349  * Tcam table scan the identifier list and extract the identifier from
1350  * the result blob.
1351  */
1352 static int32_t
1353 ulp_mapper_tcam_tbl_scan_ident_extract(struct bnxt_ulp_mapper_parms *parms,
1354                                        struct bnxt_ulp_mapper_tbl_info *tbl,
1355                                        struct ulp_blob *data)
1356 {
1357         struct bnxt_ulp_mapper_ident_info *idents;
1358         uint32_t num_idents = 0, i;
1359         int32_t rc = 0;
1360
1361         /*
1362          * Extract the listed identifiers from the result field,
1363          * no need to allocate them.
1364          */
1365         idents = ulp_mapper_ident_fields_get(parms, tbl, &num_idents);
1366         for (i = 0; i < num_idents; i++) {
1367                 rc = ulp_mapper_ident_extract(parms, tbl, &idents[i], data);
1368                 if (rc) {
1369                         BNXT_TF_DBG(ERR, "Error in identifier extraction\n");
1370                         return rc;
1371                 }
1372         }
1373         return rc;
1374 }
1375
1376 /* Internal function to write the tcam entry */
1377 static int32_t
1378 ulp_mapper_tcam_tbl_entry_write(struct bnxt_ulp_mapper_parms *parms,
1379                                 struct bnxt_ulp_mapper_tbl_info *tbl,
1380                                 struct ulp_blob *key,
1381                                 struct ulp_blob *mask,
1382                                 struct ulp_blob *data,
1383                                 uint16_t idx)
1384 {
1385         struct tf_set_tcam_entry_parms sparms = { 0 };
1386         struct tf *tfp;
1387         uint16_t tmplen;
1388         int32_t rc;
1389
1390         tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx);
1391         if (!tfp) {
1392                 BNXT_TF_DBG(ERR, "Failed to get truflow pointer\n");
1393                 return -EINVAL;
1394         }
1395
1396         sparms.dir              = tbl->direction;
1397         sparms.tcam_tbl_type    = tbl->resource_type;
1398         sparms.idx              = idx;
1399         /* Already verified the key/mask lengths */
1400         sparms.key              = ulp_blob_data_get(key, &tmplen);
1401         sparms.mask             = ulp_blob_data_get(mask, &tmplen);
1402         sparms.key_sz_in_bits   = tbl->key_bit_size;
1403         sparms.result           = ulp_blob_data_get(data, &tmplen);
1404
1405         if (tbl->result_bit_size != tmplen) {
1406                 BNXT_TF_DBG(ERR, "Result len (%d) != Expected (%d)\n",
1407                             tmplen, tbl->result_bit_size);
1408                 return -EINVAL;
1409         }
1410         sparms.result_sz_in_bits = tbl->result_bit_size;
1411         if (tf_set_tcam_entry(tfp, &sparms)) {
1412                 BNXT_TF_DBG(ERR, "tcam[%s][%s][%x] write failed.\n",
1413                             tf_tcam_tbl_2_str(sparms.tcam_tbl_type),
1414                             tf_dir_2_str(sparms.dir), sparms.idx);
1415                 return -EIO;
1416         }
1417         BNXT_TF_DBG(INFO, "tcam[%s][%s][%x] write success.\n",
1418                     tf_tcam_tbl_2_str(sparms.tcam_tbl_type),
1419                     tf_dir_2_str(sparms.dir), sparms.idx);
1420
1421         /* Update cache with TCAM index if the was cache allocated. */
1422         if (parms->tcam_tbl_opc ==
1423             BNXT_ULP_MAPPER_TCAM_TBL_OPC_CACHE_ALLOC) {
1424                 if (!parms->cache_ptr) {
1425                         BNXT_TF_DBG(ERR, "Unable to update cache");
1426                         return -EINVAL;
1427                 }
1428                 parms->cache_ptr->tcam_idx = idx;
1429         }
1430
1431         /* Mark action */
1432         rc = ulp_mapper_mark_act_ptr_process(parms, tbl);
1433         if (rc) {
1434                 BNXT_TF_DBG(ERR, "failed mark action processing\n");
1435                 return rc;
1436         }
1437
1438         return rc;
1439 }
1440
1441 static int32_t
1442 ulp_mapper_tcam_tbl_process(struct bnxt_ulp_mapper_parms *parms,
1443                             struct bnxt_ulp_mapper_tbl_info *tbl)
1444 {
1445         struct bnxt_ulp_mapper_class_key_field_info     *kflds;
1446         struct ulp_blob key, mask, data, update_data;
1447         uint32_t i, num_kflds;
1448         struct tf *tfp;
1449         int32_t rc, trc;
1450         struct tf_alloc_tcam_entry_parms aparms         = { 0 };
1451         struct tf_search_tcam_entry_parms searchparms   = { 0 };
1452         struct ulp_flow_db_res_params   fid_parms       = { 0 };
1453         struct tf_free_tcam_entry_parms free_parms      = { 0 };
1454         enum bnxt_ulp_search_before_alloc search_flag;
1455         uint32_t hit = 0;
1456         uint16_t tmplen = 0;
1457         uint16_t idx;
1458
1459         /* Skip this if was handled by the cache. */
1460         if (parms->tcam_tbl_opc == BNXT_ULP_MAPPER_TCAM_TBL_OPC_CACHE_SKIP) {
1461                 parms->tcam_tbl_opc = BNXT_ULP_MAPPER_TCAM_TBL_OPC_NORMAL;
1462                 return 0;
1463         }
1464
1465         tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx);
1466         if (!tfp) {
1467                 BNXT_TF_DBG(ERR, "Failed to get truflow pointer\n");
1468                 return -EINVAL;
1469         }
1470
1471         kflds = ulp_mapper_key_fields_get(parms, tbl, &num_kflds);
1472         if (!kflds || !num_kflds) {
1473                 BNXT_TF_DBG(ERR, "Failed to get key fields\n");
1474                 return -EINVAL;
1475         }
1476
1477         if (!ulp_blob_init(&key, tbl->key_bit_size,
1478                            parms->device_params->byte_order) ||
1479             !ulp_blob_init(&mask, tbl->key_bit_size,
1480                            parms->device_params->byte_order) ||
1481             !ulp_blob_init(&data, tbl->result_bit_size,
1482                            parms->device_params->byte_order) ||
1483             !ulp_blob_init(&update_data, tbl->result_bit_size,
1484                            parms->device_params->byte_order)) {
1485                 BNXT_TF_DBG(ERR, "blob inits failed.\n");
1486                 return -EINVAL;
1487         }
1488
1489         /* create the key/mask */
1490         /*
1491          * NOTE: The WC table will require some kind of flag to handle the
1492          * mode bits within the key/mask
1493          */
1494         for (i = 0; i < num_kflds; i++) {
1495                 /* Setup the key */
1496                 rc = ulp_mapper_keymask_field_process(parms, tbl->direction,
1497                                                       &kflds[i],
1498                                                       &key, 1, "TCAM Key");
1499                 if (rc) {
1500                         BNXT_TF_DBG(ERR, "Key field set failed.\n");
1501                         return rc;
1502                 }
1503
1504                 /* Setup the mask */
1505                 rc = ulp_mapper_keymask_field_process(parms, tbl->direction,
1506                                                       &kflds[i],
1507                                                       &mask, 0, "TCAM Mask");
1508                 if (rc) {
1509                         BNXT_TF_DBG(ERR, "Mask field set failed.\n");
1510                         return rc;
1511                 }
1512         }
1513
1514         if (tbl->srch_b4_alloc == BNXT_ULP_SEARCH_BEFORE_ALLOC_NO) {
1515                 /*
1516                  * No search for re-use is requested, so simply allocate the
1517                  * tcam index.
1518                  */
1519                 aparms.dir              = tbl->direction;
1520                 aparms.tcam_tbl_type    = tbl->resource_type;
1521                 aparms.search_enable    = tbl->srch_b4_alloc;
1522                 aparms.key_sz_in_bits   = tbl->key_bit_size;
1523                 aparms.key              = ulp_blob_data_get(&key, &tmplen);
1524                 if (tbl->key_bit_size != tmplen) {
1525                         BNXT_TF_DBG(ERR, "Key len (%d) != Expected (%d)\n",
1526                                     tmplen, tbl->key_bit_size);
1527                         return -EINVAL;
1528                 }
1529
1530                 aparms.mask             = ulp_blob_data_get(&mask, &tmplen);
1531                 if (tbl->key_bit_size != tmplen) {
1532                         BNXT_TF_DBG(ERR, "Mask len (%d) != Expected (%d)\n",
1533                                     tmplen, tbl->key_bit_size);
1534                         return -EINVAL;
1535                 }
1536
1537                 aparms.priority         = tbl->priority;
1538
1539                 /*
1540                  * All failures after this succeeds require the entry to be
1541                  * freed. cannot return directly on failure, but needs to goto
1542                  * error.
1543                  */
1544                 rc = tf_alloc_tcam_entry(tfp, &aparms);
1545                 if (rc) {
1546                         BNXT_TF_DBG(ERR, "tcam alloc failed rc=%d.\n", rc);
1547                         return rc;
1548                 }
1549                 idx = aparms.idx;
1550                 hit = aparms.hit;
1551         } else {
1552                 /*
1553                  * Searching before allocation to see if we already have an
1554                  * entry.  This allows re-use of a constrained resource.
1555                  */
1556                 searchparms.dir = tbl->direction;
1557                 searchparms.tcam_tbl_type = tbl->resource_type;
1558                 searchparms.key = ulp_blob_data_get(&key, &tmplen);
1559                 searchparms.key_sz_in_bits = tbl->key_bit_size;
1560                 searchparms.mask = ulp_blob_data_get(&mask, &tmplen);
1561                 searchparms.priority = tbl->priority;
1562                 searchparms.alloc = 1;
1563                 searchparms.result = ulp_blob_data_get(&data, &tmplen);
1564                 searchparms.result_sz_in_bits = tbl->result_bit_size;
1565
1566                 rc = tf_search_tcam_entry(tfp, &searchparms);
1567                 if (rc) {
1568                         BNXT_TF_DBG(ERR, "tcam search failed rc=%d\n", rc);
1569                         return rc;
1570                 }
1571
1572                 /* Successful search, check the result */
1573                 if (searchparms.search_status == REJECT) {
1574                         BNXT_TF_DBG(ERR, "tcam alloc rejected\n");
1575                         return -ENOMEM;
1576                 }
1577                 idx = searchparms.idx;
1578                 hit = searchparms.hit;
1579         }
1580
1581         /* if it is miss then it is same as no search before alloc */
1582         if (!hit)
1583                 search_flag = BNXT_ULP_SEARCH_BEFORE_ALLOC_NO;
1584         else
1585                 search_flag = tbl->srch_b4_alloc;
1586
1587         switch (search_flag) {
1588         case BNXT_ULP_SEARCH_BEFORE_ALLOC_NO:
1589                 /*Scan identifier list, allocate identifier and update regfile*/
1590                 rc = ulp_mapper_tcam_tbl_scan_ident_alloc(parms, tbl);
1591                 /* Create the result blob */
1592                 if (!rc)
1593                         rc = ulp_mapper_tcam_tbl_result_create(parms, tbl,
1594                                                                &data);
1595                 /* write the tcam entry */
1596                 if (!rc)
1597                         rc = ulp_mapper_tcam_tbl_entry_write(parms, tbl, &key,
1598                                                              &mask, &data, idx);
1599                 break;
1600         case BNXT_ULP_SEARCH_BEFORE_ALLOC_SEARCH_IF_HIT_SKIP:
1601                 /*Scan identifier list, extract identifier and update regfile*/
1602                 rc = ulp_mapper_tcam_tbl_scan_ident_extract(parms, tbl, &data);
1603                 break;
1604         case BNXT_ULP_SEARCH_BEFORE_ALLOC_SEARCH_IF_HIT_UPDATE:
1605                 /*Scan identifier list, extract identifier and update regfile*/
1606                 rc = ulp_mapper_tcam_tbl_scan_ident_extract(parms, tbl, &data);
1607                 /* Create the result blob */
1608                 if (!rc)
1609                         rc = ulp_mapper_tcam_tbl_result_create(parms, tbl,
1610                                                                &update_data);
1611                 /* Update/overwrite the tcam entry */
1612                 if (!rc)
1613                         rc = ulp_mapper_tcam_tbl_entry_write(parms, tbl, &key,
1614                                                              &mask,
1615                                                              &update_data, idx);
1616                 break;
1617         default:
1618                 BNXT_TF_DBG(ERR, "invalid search opcode\n");
1619                 rc =  -EINVAL;
1620                 break;
1621         }
1622         if (rc)
1623                 goto error;
1624
1625         /*
1626          * Only link the entry to the flow db in the event that cache was not
1627          * used.
1628          */
1629         if (parms->tcam_tbl_opc == BNXT_ULP_MAPPER_TCAM_TBL_OPC_NORMAL) {
1630                 fid_parms.direction = tbl->direction;
1631                 fid_parms.resource_func = tbl->resource_func;
1632                 fid_parms.resource_type = tbl->resource_type;
1633                 fid_parms.critical_resource = tbl->critical_resource;
1634                 fid_parms.resource_hndl = idx;
1635                 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
1636                                               parms->flow_type,
1637                                               parms->fid,
1638                                               &fid_parms);
1639                 if (rc) {
1640                         BNXT_TF_DBG(ERR,
1641                                     "Failed to link resource to flow rc = %d\n",
1642                                     rc);
1643                         /* Need to free the identifier, so goto error */
1644                         goto error;
1645                 }
1646         } else {
1647                 /*
1648                  * Reset the tcam table opcode to normal in case the next tcam
1649                  * entry does not use cache.
1650                  */
1651                 parms->tcam_tbl_opc = BNXT_ULP_MAPPER_TCAM_TBL_OPC_NORMAL;
1652                 parms->cache_ptr = NULL;
1653         }
1654
1655         return 0;
1656 error:
1657         parms->tcam_tbl_opc = BNXT_ULP_MAPPER_TCAM_TBL_OPC_NORMAL;
1658         free_parms.dir                  = tbl->direction;
1659         free_parms.tcam_tbl_type        = tbl->resource_type;
1660         free_parms.idx                  = idx;
1661         trc = tf_free_tcam_entry(tfp, &free_parms);
1662         if (trc)
1663                 BNXT_TF_DBG(ERR, "Failed to free tcam[%d][%d][%d] on failure\n",
1664                             tbl->resource_type, tbl->direction, idx);
1665
1666         return rc;
1667 }
1668
1669 static int32_t
1670 ulp_mapper_em_tbl_process(struct bnxt_ulp_mapper_parms *parms,
1671                           struct bnxt_ulp_mapper_tbl_info *tbl)
1672 {
1673         struct bnxt_ulp_mapper_class_key_field_info     *kflds;
1674         struct bnxt_ulp_mapper_result_field_info *dflds;
1675         struct ulp_blob key, data;
1676         uint32_t i, num_kflds, num_dflds;
1677         uint16_t tmplen;
1678         struct tf *tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx);
1679         struct ulp_flow_db_res_params   fid_parms = { 0 };
1680         struct tf_insert_em_entry_parms iparms = { 0 };
1681         struct tf_delete_em_entry_parms free_parms = { 0 };
1682         int32_t trc;
1683         enum bnxt_ulp_flow_mem_type mtype = parms->device_params->flow_mem_type;
1684         int32_t rc = 0;
1685         uint32_t encap_flds = 0;
1686
1687         kflds = ulp_mapper_key_fields_get(parms, tbl, &num_kflds);
1688         if (!kflds || !num_kflds) {
1689                 BNXT_TF_DBG(ERR, "Failed to get key fields\n");
1690                 return -EINVAL;
1691         }
1692
1693         /* Initialize the key/result blobs */
1694         if (!ulp_blob_init(&key, tbl->blob_key_bit_size,
1695                            parms->device_params->byte_order) ||
1696             !ulp_blob_init(&data, tbl->result_bit_size,
1697                            parms->device_params->byte_order)) {
1698                 BNXT_TF_DBG(ERR, "blob inits failed.\n");
1699                 return -EINVAL;
1700         }
1701
1702         /* create the key */
1703         for (i = 0; i < num_kflds; i++) {
1704                 /* Setup the key */
1705                 rc = ulp_mapper_keymask_field_process(parms, tbl->direction,
1706                                                       &kflds[i],
1707                                                       &key, 1, "EM Key");
1708                 if (rc) {
1709                         BNXT_TF_DBG(ERR, "Key field set failed.\n");
1710                         return rc;
1711                 }
1712         }
1713
1714         /*
1715          * TBD: Normally should process identifiers in case of using recycle or
1716          * loopback.  Not supporting recycle for now.
1717          */
1718
1719         /* Create the result data blob */
1720         dflds = ulp_mapper_result_fields_get(parms, tbl,
1721                                              &num_dflds, &encap_flds);
1722         if (!dflds || !num_dflds || encap_flds) {
1723                 BNXT_TF_DBG(ERR, "Failed to get data fields.\n");
1724                 return -EINVAL;
1725         }
1726
1727         for (i = 0; i < num_dflds; i++) {
1728                 struct bnxt_ulp_mapper_result_field_info *fld;
1729
1730                 fld = &dflds[i];
1731
1732                 rc = ulp_mapper_result_field_process(parms,
1733                                                      tbl->direction,
1734                                                      fld,
1735                                                      &data,
1736                                                      "EM Result");
1737                 if (rc) {
1738                         BNXT_TF_DBG(ERR, "Failed to set data fields.\n");
1739                         return rc;
1740                 }
1741         }
1742
1743         /* do the transpose for the internal EM keys */
1744         if (tbl->resource_func == BNXT_ULP_RESOURCE_FUNC_INT_EM_TABLE)
1745                 ulp_blob_perform_byte_reverse(&key);
1746
1747         rc = bnxt_ulp_cntxt_tbl_scope_id_get(parms->ulp_ctx,
1748                                              &iparms.tbl_scope_id);
1749         if (rc) {
1750                 BNXT_TF_DBG(ERR, "Failed to get table scope rc=%d\n", rc);
1751                 return rc;
1752         }
1753
1754         /*
1755          * NOTE: the actual blob size will differ from the size in the tbl
1756          * entry due to the padding.
1757          */
1758         iparms.dup_check                = 0;
1759         iparms.dir                      = tbl->direction;
1760         iparms.mem                      = tbl->resource_type;
1761         iparms.key                      = ulp_blob_data_get(&key, &tmplen);
1762         iparms.key_sz_in_bits           = tbl->key_bit_size;
1763         iparms.em_record                = ulp_blob_data_get(&data, &tmplen);
1764         iparms.em_record_sz_in_bits     = tbl->result_bit_size;
1765
1766         rc = tf_insert_em_entry(tfp, &iparms);
1767         if (rc) {
1768                 BNXT_TF_DBG(ERR, "Failed to insert em entry rc=%d.\n", rc);
1769                 return rc;
1770         }
1771
1772         /* Mark action process */
1773         if (mtype == BNXT_ULP_FLOW_MEM_TYPE_EXT &&
1774             tbl->resource_type == TF_MEM_EXTERNAL)
1775                 rc = ulp_mapper_mark_gfid_process(parms, tbl, iparms.flow_id);
1776         else if (mtype == BNXT_ULP_FLOW_MEM_TYPE_INT &&
1777                  tbl->resource_type == TF_MEM_INTERNAL)
1778                 rc = ulp_mapper_mark_act_ptr_process(parms, tbl);
1779         if (rc) {
1780                 BNXT_TF_DBG(ERR, "Failed to add mark to flow\n");
1781                 goto error;
1782         }
1783
1784         /* Link the EM resource to the flow in the flow db */
1785         memset(&fid_parms, 0, sizeof(fid_parms));
1786         fid_parms.direction             = tbl->direction;
1787         fid_parms.resource_func         = tbl->resource_func;
1788         fid_parms.resource_type         = tbl->resource_type;
1789         fid_parms.critical_resource     = tbl->critical_resource;
1790         fid_parms.resource_hndl         = iparms.flow_handle;
1791
1792         rc = ulp_flow_db_resource_add(parms->ulp_ctx,
1793                                       parms->flow_type,
1794                                       parms->fid,
1795                                       &fid_parms);
1796         if (rc) {
1797                 BNXT_TF_DBG(ERR, "Fail to link res to flow rc = %d\n",
1798                             rc);
1799                 /* Need to free the identifier, so goto error */
1800                 goto error;
1801         }
1802
1803         return 0;
1804 error:
1805         free_parms.dir          = iparms.dir;
1806         free_parms.mem          = iparms.mem;
1807         free_parms.tbl_scope_id = iparms.tbl_scope_id;
1808         free_parms.flow_handle  = iparms.flow_handle;
1809
1810         trc = tf_delete_em_entry(tfp, &free_parms);
1811         if (trc)
1812                 BNXT_TF_DBG(ERR, "Failed to delete EM entry on failed add\n");
1813
1814         return rc;
1815 }
1816
1817 static int32_t
1818 ulp_mapper_index_tbl_process(struct bnxt_ulp_mapper_parms *parms,
1819                              struct bnxt_ulp_mapper_tbl_info *tbl)
1820 {
1821         struct bnxt_ulp_mapper_result_field_info *flds;
1822         struct ulp_flow_db_res_params   fid_parms;
1823         struct ulp_blob data;
1824         uint64_t idx = 0;
1825         uint16_t tmplen;
1826         uint32_t i, num_flds, index, hit;
1827         int32_t rc = 0, trc = 0;
1828         struct tf_alloc_tbl_entry_parms aparms = { 0 };
1829         struct tf_search_tbl_entry_parms srchparms = { 0 };
1830         struct tf_set_tbl_entry_parms   sparms = { 0 };
1831         struct tf_free_tbl_entry_parms  free_parms = { 0 };
1832         uint32_t tbl_scope_id;
1833         struct tf *tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx);
1834         uint16_t bit_size;
1835         uint32_t encap_flds = 0;
1836
1837         /* Get the scope id first */
1838         rc = bnxt_ulp_cntxt_tbl_scope_id_get(parms->ulp_ctx, &tbl_scope_id);
1839         if (rc) {
1840                 BNXT_TF_DBG(ERR, "Failed to get table scope rc=%d\n", rc);
1841                 return rc;
1842         }
1843
1844         /* use the max size if encap is enabled */
1845         if (tbl->encap_num_fields)
1846                 bit_size = BNXT_ULP_FLMP_BLOB_SIZE_IN_BITS;
1847         else
1848                 bit_size = tbl->result_bit_size;
1849
1850         /* Initialize the blob data */
1851         if (!ulp_blob_init(&data, bit_size,
1852                            parms->device_params->byte_order)) {
1853                 BNXT_TF_DBG(ERR, "Failed initial index table blob\n");
1854                 return -EINVAL;
1855         }
1856
1857         /* Get the result fields list */
1858         flds = ulp_mapper_result_fields_get(parms, tbl, &num_flds, &encap_flds);
1859
1860         if (!flds || (!num_flds && !encap_flds)) {
1861                 BNXT_TF_DBG(ERR, "template undefined for the index table\n");
1862                 return -EINVAL;
1863         }
1864
1865         /* process the result fields, loop through them */
1866         for (i = 0; i < (num_flds + encap_flds); i++) {
1867                 /* set the swap index if encap swap bit is enabled */
1868                 if (parms->device_params->encap_byte_swap && encap_flds &&
1869                     (i == num_flds))
1870                         ulp_blob_encap_swap_idx_set(&data);
1871
1872                 /* Process the result fields */
1873                 rc = ulp_mapper_result_field_process(parms,
1874                                                      tbl->direction,
1875                                                      &flds[i],
1876                                                      &data,
1877                                                      "Indexed Result");
1878                 if (rc) {
1879                         BNXT_TF_DBG(ERR, "data field failed\n");
1880                         return rc;
1881                 }
1882         }
1883
1884         /* if encap bit swap is enabled perform the bit swap */
1885         if (parms->device_params->encap_byte_swap && encap_flds) {
1886                 ulp_blob_perform_encap_swap(&data);
1887         }
1888
1889         /*
1890          * Check for index opcode, if it is Global then
1891          * no need to allocate the table, just set the table
1892          * and exit since it is not maintained in the flow db.
1893          */
1894         if (tbl->index_opcode == BNXT_ULP_INDEX_OPCODE_GLOBAL) {
1895                 /* get the index from index operand */
1896                 if (tbl->index_operand < BNXT_ULP_GLB_REGFILE_INDEX_LAST &&
1897                     ulp_mapper_glb_resource_read(parms->mapper_data,
1898                                                  tbl->direction,
1899                                                  tbl->index_operand,
1900                                                  &idx)) {
1901                         BNXT_TF_DBG(ERR, "Glbl regfile[%d] read failed.\n",
1902                                     tbl->index_operand);
1903                         return -EINVAL;
1904                 }
1905                 /* set the Tf index table */
1906                 sparms.dir              = tbl->direction;
1907                 sparms.type             = tbl->resource_type;
1908                 sparms.data             = ulp_blob_data_get(&data, &tmplen);
1909                 sparms.data_sz_in_bytes = ULP_BITS_2_BYTE(tmplen);
1910                 sparms.idx              = tfp_be_to_cpu_64(idx);
1911                 sparms.tbl_scope_id     = tbl_scope_id;
1912
1913                 rc = tf_set_tbl_entry(tfp, &sparms);
1914                 if (rc) {
1915                         BNXT_TF_DBG(ERR,
1916                                     "Glbl Set table[%d][%s][%d] failed rc=%d\n",
1917                                     sparms.type,
1918                                     (sparms.dir == TF_DIR_RX) ? "RX" : "TX",
1919                                     sparms.idx,
1920                                     rc);
1921                         return rc;
1922                 }
1923                 return 0; /* success */
1924         }
1925
1926         index = 0;
1927         hit = 0;
1928         /* Perform the tf table allocation by filling the alloc params */
1929         if (tbl->srch_b4_alloc) {
1930                 memset(&srchparms, 0, sizeof(srchparms));
1931                 srchparms.dir = tbl->direction;
1932                 srchparms.type = tbl->resource_type;
1933                 srchparms.alloc = 1;
1934                 srchparms.result = ulp_blob_data_get(&data, &tmplen);
1935                 srchparms.result_sz_in_bytes = ULP_BITS_2_BYTE(tmplen);
1936                 srchparms.tbl_scope_id = tbl_scope_id;
1937                 rc = tf_search_tbl_entry(tfp, &srchparms);
1938                 if (rc) {
1939                         BNXT_TF_DBG(ERR, "Alloc table[%s][%s] failed rc=%d\n",
1940                                     tf_tbl_type_2_str(tbl->resource_type),
1941                                     tf_dir_2_str(tbl->direction), rc);
1942                         return rc;
1943                 }
1944                 if (srchparms.search_status == REJECT) {
1945                         BNXT_TF_DBG(ERR, "Alloc table[%s][%s] rejected.\n",
1946                                     tf_tbl_type_2_str(tbl->resource_type),
1947                                     tf_dir_2_str(tbl->direction));
1948                         return -ENOMEM;
1949                 }
1950                 index = srchparms.idx;
1951                 hit = srchparms.hit;
1952         } else {
1953                 aparms.dir              = tbl->direction;
1954                 aparms.type             = tbl->resource_type;
1955                 aparms.search_enable    = tbl->srch_b4_alloc;
1956                 aparms.result           = ulp_blob_data_get(&data, &tmplen);
1957                 aparms.result_sz_in_bytes = ULP_BITS_2_BYTE(tmplen);
1958                 aparms.tbl_scope_id     = tbl_scope_id;
1959
1960                 /* All failures after the alloc succeeds require a free */
1961                 rc = tf_alloc_tbl_entry(tfp, &aparms);
1962                 if (rc) {
1963                         BNXT_TF_DBG(ERR, "Alloc table[%s][%s] failed rc=%d\n",
1964                                     tf_tbl_type_2_str(tbl->resource_type),
1965                                     tf_dir_2_str(tbl->direction), rc);
1966                         return rc;
1967                 }
1968                 index = aparms.idx;
1969         }
1970         /*
1971          * calculate the idx for the result record, for external EM the offset
1972          * needs to be shifted accordingly. If external non-inline table types
1973          * are used then need to revisit this logic.
1974          */
1975         if (tbl->resource_type == TF_TBL_TYPE_EXT)
1976                 idx = TF_ACT_REC_OFFSET_2_PTR(index);
1977         else
1978                 idx = index;
1979
1980         /* Always storing values in Regfile in BE */
1981         idx = tfp_cpu_to_be_64(idx);
1982         if (tbl->index_opcode == BNXT_ULP_INDEX_OPCODE_ALLOCATE) {
1983                 rc = ulp_regfile_write(parms->regfile, tbl->index_operand, idx);
1984                 if (!rc) {
1985                         BNXT_TF_DBG(ERR, "Write regfile[%d] failed\n",
1986                                     tbl->index_operand);
1987                         goto error;
1988                 }
1989         }
1990
1991         /* Perform the tf table set by filling the set params */
1992         if (!tbl->srch_b4_alloc || !hit) {
1993                 sparms.dir              = tbl->direction;
1994                 sparms.type             = tbl->resource_type;
1995                 sparms.data             = ulp_blob_data_get(&data, &tmplen);
1996                 sparms.data_sz_in_bytes = ULP_BITS_2_BYTE(tmplen);
1997                 sparms.idx              = index;
1998                 sparms.tbl_scope_id     = tbl_scope_id;
1999
2000                 rc = tf_set_tbl_entry(tfp, &sparms);
2001                 if (rc) {
2002                         BNXT_TF_DBG(ERR, "Set table[%d][%s][%d] failed rc=%d\n",
2003                                     sparms.type,
2004                                     (sparms.dir == TF_DIR_RX) ? "RX" : "TX",
2005                                     sparms.idx,
2006                                     rc);
2007                         goto error;
2008                 }
2009         }
2010
2011         /* Link the resource to the flow in the flow db */
2012         memset(&fid_parms, 0, sizeof(fid_parms));
2013         fid_parms.direction     = tbl->direction;
2014         fid_parms.resource_func = tbl->resource_func;
2015         fid_parms.resource_type = tbl->resource_type;
2016         fid_parms.resource_sub_type = tbl->resource_sub_type;
2017         fid_parms.resource_hndl = index;
2018         fid_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
2019
2020         rc = ulp_flow_db_resource_add(parms->ulp_ctx,
2021                                       parms->flow_type,
2022                                       parms->fid,
2023                                       &fid_parms);
2024         if (rc) {
2025                 BNXT_TF_DBG(ERR, "Failed to link resource to flow rc = %d\n",
2026                             rc);
2027                 goto error;
2028         }
2029
2030         /* Perform the VF rep action */
2031         rc = ulp_mapper_mark_vfr_idx_process(parms, tbl);
2032         if (rc) {
2033                 BNXT_TF_DBG(ERR, "Failed to add vfr mark rc = %d\n", rc);
2034                 goto error;
2035         }
2036         return rc;
2037 error:
2038         /*
2039          * Free the allocated resource since we failed to either
2040          * write to the entry or link the flow
2041          */
2042         free_parms.dir  = tbl->direction;
2043         free_parms.type = tbl->resource_type;
2044         free_parms.idx  = index;
2045         free_parms.tbl_scope_id = tbl_scope_id;
2046
2047         trc = tf_free_tbl_entry(tfp, &free_parms);
2048         if (trc)
2049                 BNXT_TF_DBG(ERR, "Failed to free tbl entry on failure\n");
2050
2051         return rc;
2052 }
2053
2054 static int32_t
2055 ulp_mapper_cache_tbl_process(struct bnxt_ulp_mapper_parms *parms,
2056                              struct bnxt_ulp_mapper_tbl_info *tbl)
2057 {
2058         struct bnxt_ulp_mapper_class_key_field_info *kflds;
2059         struct bnxt_ulp_mapper_cache_entry *cache_entry;
2060         struct bnxt_ulp_mapper_ident_info *idents;
2061         uint32_t i, num_kflds = 0, num_idents = 0;
2062         struct ulp_flow_db_res_params fid_parms;
2063         struct tf_free_identifier_parms fparms;
2064         uint16_t tmplen, tmp_ident;
2065         struct ulp_blob key;
2066         uint8_t *cache_key;
2067         uint64_t regval;
2068         uint16_t *ckey;
2069         int32_t rc;
2070
2071         /* Get the key fields list and build the key. */
2072         kflds = ulp_mapper_key_fields_get(parms, tbl, &num_kflds);
2073         if (!kflds || !num_kflds) {
2074                 BNXT_TF_DBG(ERR, "Failed to get key fields\n");
2075                 return -EINVAL;
2076         }
2077         if (!ulp_blob_init(&key, tbl->key_bit_size,
2078                            parms->device_params->byte_order)) {
2079                 BNXT_TF_DBG(ERR, "Failed to alloc blob\n");
2080                 return -EINVAL;
2081         }
2082         for (i = 0; i < num_kflds; i++) {
2083                 /* Setup the key */
2084                 rc = ulp_mapper_keymask_field_process(parms, tbl->direction,
2085                                                       &kflds[i],
2086                                                       &key, 1, "Cache Key");
2087                 if (rc) {
2088                         BNXT_TF_DBG(ERR,
2089                                     "Failed to create key for Cache rc=%d\n",
2090                                     rc);
2091                         return -EINVAL;
2092                 }
2093         }
2094
2095         /*
2096          * Perform the lookup in the cache table with constructed key.  The
2097          * cache_key is a byte array of tmplen, it needs to be converted to a
2098          * index for the cache table.
2099          */
2100         cache_key = ulp_blob_data_get(&key, &tmplen);
2101         ckey = (uint16_t *)cache_key;
2102
2103         /*
2104          * The id computed based on resource sub type and direction where
2105          * dir is the bit0 and rest of the bits come from resource
2106          * sub type.
2107          */
2108         cache_entry = ulp_mapper_cache_entry_get(parms->ulp_ctx,
2109                                                  (tbl->resource_sub_type << 1 |
2110                                                  (tbl->direction & 0x1)),
2111                                                  *ckey);
2112
2113         /*
2114          * Get the identifier list for processing by both the hit and miss
2115          * processing.
2116          */
2117         idents = ulp_mapper_ident_fields_get(parms, tbl, &num_idents);
2118
2119         if (!cache_entry->ref_count) {
2120                 /* Initialize the cache entry */
2121                 cache_entry->tcam_idx = 0;
2122                 cache_entry->ref_count = 0;
2123                 for (i = 0; i < BNXT_ULP_CACHE_TBL_IDENT_MAX_NUM; i++)
2124                         cache_entry->idents[i] = ULP_IDENTS_INVALID;
2125
2126                 /* Need to allocate identifiers for storing in the cache. */
2127                 for (i = 0; i < num_idents; i++) {
2128                         /*
2129                          * Since we are using the cache, the identifier does not
2130                          * get added to the flow db.  Pass in the pointer to the
2131                          * tmp_ident.
2132                          */
2133                         rc = ulp_mapper_ident_process(parms, tbl,
2134                                                       &idents[i], &tmp_ident);
2135                         if (rc)
2136                                 goto error;
2137
2138                         cache_entry->ident_types[i] = idents[i].ident_type;
2139                         cache_entry->idents[i] = tmp_ident;
2140                 }
2141
2142                 /* Tell the TCAM processor to alloc an entry */
2143                 parms->tcam_tbl_opc = BNXT_ULP_MAPPER_TCAM_TBL_OPC_CACHE_ALLOC;
2144                 /* Store the cache key for use by the tcam process code */
2145                 parms->cache_ptr = cache_entry;
2146         } else {
2147                 /* Cache hit, get values from result. */
2148                 for (i = 0; i < num_idents; i++) {
2149                         regval = (uint64_t)cache_entry->idents[i];
2150                         if (!ulp_regfile_write(parms->regfile,
2151                                                idents[i].regfile_idx,
2152                                                tfp_cpu_to_be_64(regval))) {
2153                                 BNXT_TF_DBG(ERR,
2154                                             "Failed to write to regfile\n");
2155                                 return -EINVAL;
2156                         }
2157                 }
2158                 /*
2159                  * The cached entry is being used, so let the tcam processing
2160                  * know not to process this table.
2161                  */
2162                 parms->tcam_tbl_opc = BNXT_ULP_MAPPER_TCAM_TBL_OPC_CACHE_SKIP;
2163         }
2164
2165         /* Made through the cache processing, increment the reference count. */
2166         cache_entry->ref_count++;
2167
2168         /* Link the cache to the flow db. */
2169         memset(&fid_parms, 0, sizeof(fid_parms));
2170         fid_parms.direction = tbl->direction;
2171         fid_parms.resource_func = tbl->resource_func;
2172
2173         /*
2174          * Cache resource type is composed of table_type, resource
2175          * sub type and direction, it needs to set appropriately via setter.
2176          */
2177         ulp_mapper_cache_res_type_set(&fid_parms,
2178                                       tbl->resource_type,
2179                                       (tbl->resource_sub_type << 1 |
2180                                        (tbl->direction & 0x1)));
2181         fid_parms.resource_hndl = (uint64_t)*ckey;
2182         fid_parms.critical_resource = tbl->critical_resource;
2183         rc = ulp_flow_db_resource_add(parms->ulp_ctx,
2184                                       parms->flow_type,
2185                                       parms->fid,
2186                                       &fid_parms);
2187         if (rc)
2188                 BNXT_TF_DBG(ERR, "Failed to add cache to flow db.\n");
2189
2190         return rc;
2191 error:
2192         /*
2193          * This error handling only gets called when the idents are being
2194          * allocated for the cache on misses.  Using the num_idents that was
2195          * previously set.
2196          */
2197         for (i = 0; i < num_idents; i++) {
2198                 if (cache_entry->idents[i] == ULP_IDENTS_INVALID)
2199                         continue;
2200
2201                 fparms.dir = tbl->direction;
2202                 fparms.ident_type = idents[i].ident_type;
2203                 fparms.id = cache_entry->idents[i];
2204                 tf_free_identifier(parms->tfp, &fparms);
2205         }
2206
2207         return rc;
2208 }
2209
2210 static int32_t
2211 ulp_mapper_if_tbl_process(struct bnxt_ulp_mapper_parms *parms,
2212                           struct bnxt_ulp_mapper_tbl_info *tbl)
2213 {
2214         struct bnxt_ulp_mapper_result_field_info *flds;
2215         struct ulp_blob data;
2216         uint64_t idx;
2217         uint16_t tmplen;
2218         uint32_t i, num_flds;
2219         int32_t rc = 0;
2220         struct tf_set_if_tbl_entry_parms iftbl_params = { 0 };
2221         struct tf *tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx);
2222         uint32_t encap_flds;
2223
2224         /* Initialize the blob data */
2225         if (!ulp_blob_init(&data, tbl->result_bit_size,
2226                            parms->device_params->byte_order)) {
2227                 BNXT_TF_DBG(ERR, "Failed initial index table blob\n");
2228                 return -EINVAL;
2229         }
2230
2231         /* Get the result fields list */
2232         flds = ulp_mapper_result_fields_get(parms, tbl, &num_flds, &encap_flds);
2233
2234         if (!flds || !num_flds || encap_flds) {
2235                 BNXT_TF_DBG(ERR, "template undefined for the IF table\n");
2236                 return -EINVAL;
2237         }
2238
2239         /* process the result fields, loop through them */
2240         for (i = 0; i < num_flds; i++) {
2241                 /* Process the result fields */
2242                 rc = ulp_mapper_result_field_process(parms,
2243                                                      tbl->direction,
2244                                                      &flds[i],
2245                                                      &data,
2246                                                      "IFtable Result");
2247                 if (rc) {
2248                         BNXT_TF_DBG(ERR, "data field failed\n");
2249                         return rc;
2250                 }
2251         }
2252
2253         /* Get the index details from computed field */
2254         if (tbl->index_opcode == BNXT_ULP_INDEX_OPCODE_COMP_FIELD) {
2255                 idx = ULP_COMP_FLD_IDX_RD(parms, tbl->index_operand);
2256         } else if (tbl->index_opcode == BNXT_ULP_INDEX_OPCODE_CONSTANT) {
2257                 idx = tbl->index_operand;
2258         } else {
2259                 BNXT_TF_DBG(ERR, "Invalid tbl index opcode\n");
2260                 return -EINVAL;
2261         }
2262
2263         /* Perform the tf table set by filling the set params */
2264         iftbl_params.dir = tbl->direction;
2265         iftbl_params.type = tbl->resource_type;
2266         iftbl_params.data = ulp_blob_data_get(&data, &tmplen);
2267         iftbl_params.data_sz_in_bytes = ULP_BITS_2_BYTE(tmplen);
2268         iftbl_params.idx = idx;
2269
2270         rc = tf_set_if_tbl_entry(tfp, &iftbl_params);
2271         if (rc) {
2272                 BNXT_TF_DBG(ERR, "Set table[%d][%s][%d] failed rc=%d\n",
2273                             iftbl_params.type,
2274                             (iftbl_params.dir == TF_DIR_RX) ? "RX" : "TX",
2275                             iftbl_params.idx,
2276                             rc);
2277                 return rc;
2278         }
2279
2280         /*
2281          * TBD: Need to look at the need to store idx in flow db for restore
2282          * the table to its original state on deletion of this entry.
2283          */
2284         return rc;
2285 }
2286
2287 static int32_t
2288 ulp_mapper_glb_resource_info_init(struct bnxt_ulp_context *ulp_ctx,
2289                                   struct bnxt_ulp_mapper_data *mapper_data)
2290 {
2291         struct bnxt_ulp_glb_resource_info *glb_res;
2292         uint32_t num_glb_res_ids, idx;
2293         int32_t rc = 0;
2294
2295         glb_res = ulp_mapper_glb_resource_info_list_get(&num_glb_res_ids);
2296         if (!glb_res || !num_glb_res_ids) {
2297                 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
2298                 return -EINVAL;
2299         }
2300
2301         /* Iterate the global resources and process each one */
2302         for (idx = 0; idx < num_glb_res_ids; idx++) {
2303                 switch (glb_res[idx].resource_func) {
2304                 case BNXT_ULP_RESOURCE_FUNC_IDENTIFIER:
2305                         rc = ulp_mapper_resource_ident_allocate(ulp_ctx,
2306                                                                 mapper_data,
2307                                                                 &glb_res[idx]);
2308                         break;
2309                 case BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE:
2310                         rc = ulp_mapper_resource_index_tbl_alloc(ulp_ctx,
2311                                                                  mapper_data,
2312                                                                  &glb_res[idx]);
2313                         break;
2314                 default:
2315                         BNXT_TF_DBG(ERR, "Global resource %x not supported\n",
2316                                     glb_res[idx].resource_func);
2317                         rc = -EINVAL;
2318                         break;
2319                 }
2320                 if (rc)
2321                         return rc;
2322         }
2323         return rc;
2324 }
2325
2326 /*
2327  * Function to process the conditional opcode of the mapper table.
2328  * returns 1 to skip the table.
2329  * return 0 to continue processing the table.
2330  */
2331 static int32_t
2332 ulp_mapper_tbl_cond_opcode_process(struct bnxt_ulp_mapper_parms *parms,
2333                                    struct bnxt_ulp_mapper_tbl_info *tbl)
2334 {
2335         int32_t rc = 1;
2336
2337         switch (tbl->cond_opcode) {
2338         case BNXT_ULP_COND_OPCODE_NOP:
2339                 rc = 0;
2340                 break;
2341         case BNXT_ULP_COND_OPCODE_COMP_FIELD_IS_SET:
2342                 if (tbl->cond_operand < BNXT_ULP_CF_IDX_LAST &&
2343                     ULP_COMP_FLD_IDX_RD(parms, tbl->cond_operand))
2344                         rc = 0;
2345                 break;
2346         case BNXT_ULP_COND_OPCODE_ACTION_BIT_IS_SET:
2347                 if (ULP_BITMAP_ISSET(parms->act_bitmap->bits,
2348                                      tbl->cond_operand))
2349                         rc = 0;
2350                 break;
2351         case BNXT_ULP_COND_OPCODE_HDR_BIT_IS_SET:
2352                 if (ULP_BITMAP_ISSET(parms->hdr_bitmap->bits,
2353                                      tbl->cond_operand))
2354                         rc = 0;
2355                 break;
2356         case BNXT_ULP_COND_OPCODE_COMP_FIELD_NOT_SET:
2357                 if (tbl->cond_operand < BNXT_ULP_CF_IDX_LAST &&
2358                     !ULP_COMP_FLD_IDX_RD(parms, tbl->cond_operand))
2359                         rc = 0;
2360                 break;
2361         case BNXT_ULP_COND_OPCODE_ACTION_BIT_NOT_SET:
2362                 if (!ULP_BITMAP_ISSET(parms->act_bitmap->bits,
2363                                       tbl->cond_operand))
2364                         rc = 0;
2365                 break;
2366         case BNXT_ULP_COND_OPCODE_HDR_BIT_NOT_SET:
2367                 if (!ULP_BITMAP_ISSET(parms->hdr_bitmap->bits,
2368                                       tbl->cond_operand))
2369                         rc = 0;
2370                 break;
2371         default:
2372                 BNXT_TF_DBG(ERR,
2373                             "Invalid arg in mapper tbl for cond opcode\n");
2374                 break;
2375         }
2376         return rc;
2377 }
2378
2379 static int32_t
2380 ulp_mapper_tbls_process(struct bnxt_ulp_mapper_parms *parms, uint32_t tid)
2381 {
2382         struct bnxt_ulp_mapper_tbl_info *tbls;
2383         uint32_t num_tbls, i;
2384         int32_t rc = -EINVAL;
2385
2386         tbls = ulp_mapper_tbl_list_get(parms, tid, &num_tbls);
2387         if (!tbls || !num_tbls) {
2388                 BNXT_TF_DBG(ERR, "No %s tables for %d:%d\n",
2389                             (parms->tmpl_type = BNXT_ULP_TEMPLATE_TYPE_CLASS) ?
2390                             "class" : "action", parms->dev_id, tid);
2391                 return -EINVAL;
2392         }
2393
2394         for (i = 0; i < num_tbls; i++) {
2395                 struct bnxt_ulp_mapper_tbl_info *tbl = &tbls[i];
2396
2397                 if (ulp_mapper_tbl_cond_opcode_process(parms, tbl))
2398                         continue;
2399
2400                 switch (tbl->resource_func) {
2401                 case BNXT_ULP_RESOURCE_FUNC_TCAM_TABLE:
2402                         rc = ulp_mapper_tcam_tbl_process(parms, tbl);
2403                         break;
2404                 case BNXT_ULP_RESOURCE_FUNC_EXT_EM_TABLE:
2405                 case BNXT_ULP_RESOURCE_FUNC_INT_EM_TABLE:
2406                         rc = ulp_mapper_em_tbl_process(parms, tbl);
2407                         break;
2408                 case BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE:
2409                         rc = ulp_mapper_index_tbl_process(parms, tbl);
2410                         break;
2411                 case BNXT_ULP_RESOURCE_FUNC_CACHE_TABLE:
2412                         rc = ulp_mapper_cache_tbl_process(parms, tbl);
2413                         break;
2414                 case BNXT_ULP_RESOURCE_FUNC_IF_TABLE:
2415                         rc = ulp_mapper_if_tbl_process(parms, tbl);
2416                         break;
2417                 default:
2418                         BNXT_TF_DBG(ERR, "Unexpected mapper resource %d\n",
2419                                     tbl->resource_func);
2420                         rc = -EINVAL;
2421                         goto error;
2422                 }
2423
2424                 if (rc) {
2425                         BNXT_TF_DBG(ERR, "Resource type %d failed\n",
2426                                     tbl->resource_func);
2427                         goto error;
2428                 }
2429         }
2430
2431         return rc;
2432 error:
2433         BNXT_TF_DBG(ERR, "%s tables failed creation for %d:%d\n",
2434                     (parms->tmpl_type = BNXT_ULP_TEMPLATE_TYPE_CLASS) ?
2435                     "class" : "action", parms->dev_id, tid);
2436         return rc;
2437 }
2438
2439 static int32_t
2440 ulp_mapper_resource_free(struct bnxt_ulp_context *ulp,
2441                          struct ulp_flow_db_res_params *res)
2442 {
2443         struct tf *tfp;
2444         int32_t rc = 0;
2445
2446         if (!res || !ulp) {
2447                 BNXT_TF_DBG(ERR, "Unable to free resource\n ");
2448                 return -EINVAL;
2449         }
2450
2451         tfp = bnxt_ulp_cntxt_tfp_get(ulp);
2452         if (!tfp) {
2453                 BNXT_TF_DBG(ERR, "Unable to free resource failed to get tfp\n");
2454                 return -EINVAL;
2455         }
2456
2457         switch (res->resource_func) {
2458         case BNXT_ULP_RESOURCE_FUNC_CACHE_TABLE:
2459                 rc = ulp_mapper_cache_entry_free(ulp, tfp, res);
2460                 break;
2461         case BNXT_ULP_RESOURCE_FUNC_TCAM_TABLE:
2462                 rc = ulp_mapper_tcam_entry_free(ulp, tfp, res);
2463                 break;
2464         case BNXT_ULP_RESOURCE_FUNC_EXT_EM_TABLE:
2465         case BNXT_ULP_RESOURCE_FUNC_INT_EM_TABLE:
2466                 rc = ulp_mapper_em_entry_free(ulp, tfp, res);
2467                 break;
2468         case BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE:
2469                 rc = ulp_mapper_index_entry_free(ulp, tfp, res);
2470                 break;
2471         case BNXT_ULP_RESOURCE_FUNC_IDENTIFIER:
2472                 rc = ulp_mapper_ident_free(ulp, tfp, res);
2473                 break;
2474         case BNXT_ULP_RESOURCE_FUNC_HW_FID:
2475                 rc = ulp_mapper_mark_free(ulp, res);
2476                 break;
2477         default:
2478                 break;
2479         }
2480
2481         return rc;
2482 }
2483
2484 int32_t
2485 ulp_mapper_resources_free(struct bnxt_ulp_context *ulp_ctx,
2486                           enum bnxt_ulp_fdb_type flow_type,
2487                           uint32_t fid)
2488 {
2489         struct ulp_flow_db_res_params res_parms = { 0 };
2490         int32_t rc, trc;
2491
2492         if (!ulp_ctx) {
2493                 BNXT_TF_DBG(ERR, "Invalid parms, unable to free flow\n");
2494                 return -EINVAL;
2495         }
2496
2497         /*
2498          * Set the critical resource on the first resource del, then iterate
2499          * while status is good
2500          */
2501         res_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_YES;
2502         rc = ulp_flow_db_resource_del(ulp_ctx, flow_type, fid, &res_parms);
2503
2504         if (rc) {
2505                 /*
2506                  * This is unexpected on the first call to resource del.
2507                  * It likely means that the flow did not exist in the flow db.
2508                  */
2509                 BNXT_TF_DBG(ERR, "Flow[%d][0x%08x] failed to free (rc=%d)\n",
2510                             flow_type, fid, rc);
2511                 return rc;
2512         }
2513
2514         while (!rc) {
2515                 trc = ulp_mapper_resource_free(ulp_ctx, &res_parms);
2516                 if (trc)
2517                         /*
2518                          * On fail, we still need to attempt to free the
2519                          * remaining resources.  Don't return
2520                          */
2521                         BNXT_TF_DBG(ERR,
2522                                     "Flow[%d][0x%x] Res[%d][0x%016" PRIx64
2523                                     "] failed rc=%d.\n",
2524                                     flow_type, fid, res_parms.resource_func,
2525                                     res_parms.resource_hndl, trc);
2526
2527                 /* All subsequent call require the non-critical_resource */
2528                 res_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
2529
2530                 rc = ulp_flow_db_resource_del(ulp_ctx,
2531                                               flow_type,
2532                                               fid,
2533                                               &res_parms);
2534         }
2535
2536         /* Free the Flow ID since we've removed all resources */
2537         rc = ulp_flow_db_fid_free(ulp_ctx, flow_type, fid);
2538
2539         return rc;
2540 }
2541
2542 static void
2543 ulp_mapper_glb_resource_info_deinit(struct bnxt_ulp_context *ulp_ctx,
2544                                     struct bnxt_ulp_mapper_data *mapper_data)
2545 {
2546         struct bnxt_ulp_mapper_glb_resource_entry *ent;
2547         struct ulp_flow_db_res_params res;
2548         uint32_t dir, idx;
2549
2550         /* Iterate the global resources and process each one */
2551         for (dir = TF_DIR_RX; dir < TF_DIR_MAX; dir++) {
2552                 for (idx = 0; idx < BNXT_ULP_GLB_RESOURCE_TBL_MAX_SZ;
2553                       idx++) {
2554                         ent = &mapper_data->glb_res_tbl[dir][idx];
2555                         if (ent->resource_func ==
2556                             BNXT_ULP_RESOURCE_FUNC_INVALID)
2557                                 continue;
2558                         memset(&res, 0, sizeof(struct ulp_flow_db_res_params));
2559                         res.resource_func = ent->resource_func;
2560                         res.direction = dir;
2561                         res.resource_type = ent->resource_type;
2562                         /*convert it from BE to cpu */
2563                         res.resource_hndl =
2564                                 tfp_be_to_cpu_64(ent->resource_hndl);
2565                         ulp_mapper_resource_free(ulp_ctx, &res);
2566                 }
2567         }
2568 }
2569
2570 int32_t
2571 ulp_mapper_flow_destroy(struct bnxt_ulp_context *ulp_ctx,
2572                         enum bnxt_ulp_fdb_type flow_type,
2573                         uint32_t fid)
2574 {
2575         int32_t rc;
2576
2577         if (!ulp_ctx) {
2578                 BNXT_TF_DBG(ERR, "Invalid parms, unable to free flow\n");
2579                 return -EINVAL;
2580         }
2581         if (bnxt_ulp_cntxt_acquire_fdb_lock(ulp_ctx)) {
2582                 BNXT_TF_DBG(ERR, "Flow db lock acquire failed\n");
2583                 return -EINVAL;
2584         }
2585
2586         rc = ulp_mapper_resources_free(ulp_ctx, flow_type, fid);
2587         bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
2588         return rc;
2589
2590 }
2591
2592 /* Function to handle the default global templates that are allocated during
2593  * the startup and reused later.
2594  */
2595 static int32_t
2596 ulp_mapper_glb_template_table_init(struct bnxt_ulp_context *ulp_ctx)
2597 {
2598         uint32_t *glbl_tmpl_list;
2599         uint32_t num_glb_tmpls, idx, dev_id;
2600         struct bnxt_ulp_mapper_parms parms;
2601         struct bnxt_ulp_mapper_data *mapper_data;
2602         int32_t rc = 0;
2603
2604         glbl_tmpl_list = ulp_mapper_glb_template_table_get(&num_glb_tmpls);
2605         if (!glbl_tmpl_list || !num_glb_tmpls)
2606                 return rc; /* No global templates to process */
2607
2608         /* Get the device id from the ulp context */
2609         if (bnxt_ulp_cntxt_dev_id_get(ulp_ctx, &dev_id)) {
2610                 BNXT_TF_DBG(ERR, "Invalid ulp context\n");
2611                 return -EINVAL;
2612         }
2613
2614         mapper_data = bnxt_ulp_cntxt_ptr2_mapper_data_get(ulp_ctx);
2615         if (!mapper_data) {
2616                 BNXT_TF_DBG(ERR, "Failed to get the ulp mapper data\n");
2617                 return -EINVAL;
2618         }
2619
2620         /* Iterate the global resources and process each one */
2621         for (idx = 0; idx < num_glb_tmpls; idx++) {
2622                 /* Initialize the parms structure */
2623                 memset(&parms, 0, sizeof(parms));
2624                 parms.tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx);
2625                 parms.ulp_ctx = ulp_ctx;
2626                 parms.dev_id = dev_id;
2627                 parms.mapper_data = mapper_data;
2628                 parms.flow_type = BNXT_ULP_FDB_TYPE_DEFAULT;
2629                 parms.tmpl_type = BNXT_ULP_TEMPLATE_TYPE_CLASS;
2630
2631                 /* Get the class table entry from dev id and class id */
2632                 parms.class_tid = glbl_tmpl_list[idx];
2633
2634                 parms.device_params = bnxt_ulp_device_params_get(parms.dev_id);
2635                 if (!parms.device_params) {
2636                         BNXT_TF_DBG(ERR, "No device for device id %d\n",
2637                                     parms.dev_id);
2638                         return -EINVAL;
2639                 }
2640
2641                 rc = ulp_mapper_tbls_process(&parms, parms.class_tid);
2642                 if (rc)
2643                         return rc;
2644         }
2645         return rc;
2646 }
2647
2648 /* Function to handle the mapping of the Flow to be compatible
2649  * with the underlying hardware.
2650  */
2651 int32_t
2652 ulp_mapper_flow_create(struct bnxt_ulp_context *ulp_ctx,
2653                        struct bnxt_ulp_mapper_create_parms *cparms,
2654                        uint32_t *flowid)
2655 {
2656         struct bnxt_ulp_mapper_parms parms;
2657         struct ulp_regfile regfile;
2658         int32_t  rc, trc;
2659
2660         if (!ulp_ctx || !cparms)
2661                 return -EINVAL;
2662
2663         /* Initialize the parms structure */
2664         memset(&parms, 0, sizeof(parms));
2665         parms.act_prop = cparms->act_prop;
2666         parms.act_bitmap = cparms->act;
2667         parms.hdr_bitmap = cparms->hdr_bitmap;
2668         parms.regfile = &regfile;
2669         parms.hdr_field = cparms->hdr_field;
2670         parms.comp_fld = cparms->comp_fld;
2671         parms.tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx);
2672         parms.ulp_ctx = ulp_ctx;
2673         parms.tcam_tbl_opc = BNXT_ULP_MAPPER_TCAM_TBL_OPC_NORMAL;
2674         parms.act_tid = cparms->act_tid;
2675         parms.class_tid = cparms->class_tid;
2676         parms.flow_type = cparms->flow_type;
2677
2678         /* Get the device id from the ulp context */
2679         if (bnxt_ulp_cntxt_dev_id_get(ulp_ctx, &parms.dev_id)) {
2680                 BNXT_TF_DBG(ERR, "Invalid ulp context\n");
2681                 return -EINVAL;
2682         }
2683
2684         /* Get the device params, it will be used in later processing */
2685         parms.device_params = bnxt_ulp_device_params_get(parms.dev_id);
2686         if (!parms.device_params) {
2687                 BNXT_TF_DBG(ERR, "No device parms for device id %d\n",
2688                             parms.dev_id);
2689                 return -EINVAL;
2690         }
2691
2692         /*
2693          * Get the mapper data for dynamic mapper data such as default
2694          * ids.
2695          */
2696         parms.mapper_data = (struct bnxt_ulp_mapper_data *)
2697                 bnxt_ulp_cntxt_ptr2_mapper_data_get(ulp_ctx);
2698         if (!parms.mapper_data) {
2699                 BNXT_TF_DBG(ERR, "Failed to get the ulp mapper data\n");
2700                 return -EINVAL;
2701         }
2702
2703         /* initialize the registry file for further processing */
2704         if (!ulp_regfile_init(parms.regfile)) {
2705                 BNXT_TF_DBG(ERR, "regfile initialization failed.\n");
2706                 return -EINVAL;
2707         }
2708
2709         rc = ulp_regfile_write(parms.regfile,
2710                                BNXT_ULP_REGFILE_INDEX_CLASS_TID,
2711                                tfp_cpu_to_be_64((uint64_t)parms.class_tid));
2712         if (!rc) {
2713                 BNXT_TF_DBG(ERR, "Unable to write template ID to regfile\n");
2714                 return -EINVAL;
2715         }
2716
2717         /* Protect flow creation */
2718         if (bnxt_ulp_cntxt_acquire_fdb_lock(ulp_ctx)) {
2719                 BNXT_TF_DBG(ERR, "Flow db lock acquire failed\n");
2720                 return -EINVAL;
2721         }
2722
2723         /* Allocate a Flow ID for attaching all resources for the flow to.
2724          * Once allocated, all errors have to walk the list of resources and
2725          * free each of them.
2726          */
2727         rc = ulp_flow_db_fid_alloc(ulp_ctx,
2728                                    parms.flow_type,
2729                                    cparms->func_id,
2730                                    &parms.fid);
2731         if (rc) {
2732                 BNXT_TF_DBG(ERR, "Unable to allocate flow table entry\n");
2733                 bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
2734                 return rc;
2735         }
2736
2737         if (parms.act_tid) {
2738                 parms.tmpl_type = BNXT_ULP_TEMPLATE_TYPE_ACTION;
2739                 /* Process the action template tables */
2740                 rc = ulp_mapper_tbls_process(&parms, parms.act_tid);
2741                 if (rc)
2742                         goto flow_error;
2743         }
2744
2745         if (parms.class_tid) {
2746                 parms.tmpl_type = BNXT_ULP_TEMPLATE_TYPE_CLASS;
2747
2748                 /* Process the class template tables.*/
2749                 rc = ulp_mapper_tbls_process(&parms, parms.class_tid);
2750                 if (rc)
2751                         goto flow_error;
2752         }
2753
2754         *flowid = parms.fid;
2755         bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
2756
2757         return rc;
2758
2759 flow_error:
2760         bnxt_ulp_cntxt_release_fdb_lock(ulp_ctx);
2761         /* Free all resources that were allocated during flow creation */
2762         trc = ulp_mapper_flow_destroy(ulp_ctx, BNXT_ULP_FDB_TYPE_REGULAR,
2763                                       parms.fid);
2764         if (trc)
2765                 BNXT_TF_DBG(ERR, "Failed to free all resources rc=%d\n", trc);
2766
2767         return rc;
2768 }
2769
2770 int32_t
2771 ulp_mapper_init(struct bnxt_ulp_context *ulp_ctx)
2772 {
2773         struct bnxt_ulp_cache_tbl_params *tbl;
2774         struct bnxt_ulp_mapper_data *data;
2775         uint32_t i;
2776         struct tf *tfp;
2777         int32_t rc, csize;
2778
2779         if (!ulp_ctx)
2780                 return -EINVAL;
2781
2782         tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx);
2783         if (!tfp)
2784                 return -EINVAL;
2785
2786         data = rte_zmalloc("ulp_mapper_data",
2787                            sizeof(struct bnxt_ulp_mapper_data), 0);
2788         if (!data) {
2789                 BNXT_TF_DBG(ERR, "Failed to allocate the mapper data\n");
2790                 return -ENOMEM;
2791         }
2792
2793         if (bnxt_ulp_cntxt_ptr2_mapper_data_set(ulp_ctx, data)) {
2794                 BNXT_TF_DBG(ERR, "Failed to set mapper data in context\n");
2795                 /* Don't call deinit since the prof_func wasn't allocated. */
2796                 rte_free(data);
2797                 return -ENOMEM;
2798         }
2799
2800         /* Allocate the global resource ids */
2801         rc = ulp_mapper_glb_resource_info_init(ulp_ctx, data);
2802         if (rc) {
2803                 BNXT_TF_DBG(ERR, "Failed to initialize global resource ids\n");
2804                 goto error;
2805         }
2806
2807         /* Allocate the ulp cache tables. */
2808         for (i = 0; i < BNXT_ULP_CACHE_TBL_MAX_SZ; i++) {
2809                 tbl = ulp_mapper_cache_tbl_params_get(i);
2810                 if (!tbl) {
2811                         BNXT_TF_DBG(ERR, "Failed to get cache table parms (%d)",
2812                                     i);
2813                         goto error;
2814                 }
2815                 if (tbl->num_entries != 0) {
2816                         csize = sizeof(struct bnxt_ulp_mapper_cache_entry) *
2817                                 tbl->num_entries;
2818                         data->cache_tbl[i] = rte_zmalloc("ulp mapper cache tbl",
2819                                                          csize, 0);
2820                         if (!data->cache_tbl[i]) {
2821                                 BNXT_TF_DBG(ERR, "Failed to allocate Cache "
2822                                             "table %d.\n", i);
2823                                 rc = -ENOMEM;
2824                                 goto error;
2825                         }
2826                 }
2827         }
2828
2829         rc = ulp_mapper_glb_template_table_init(ulp_ctx);
2830         if (rc) {
2831                 BNXT_TF_DBG(ERR, "Failed to initialize global templates\n");
2832                 goto error;
2833         }
2834
2835         return 0;
2836 error:
2837         /* Ignore the return code in favor of returning the original error. */
2838         ulp_mapper_deinit(ulp_ctx);
2839         return rc;
2840 }
2841
2842 void
2843 ulp_mapper_deinit(struct bnxt_ulp_context *ulp_ctx)
2844 {
2845         struct bnxt_ulp_mapper_data *data;
2846         uint32_t i;
2847         struct tf *tfp;
2848
2849         if (!ulp_ctx) {
2850                 BNXT_TF_DBG(ERR,
2851                             "Failed to acquire ulp context, so data may "
2852                             "not be released.\n");
2853                 return;
2854         }
2855
2856         data = (struct bnxt_ulp_mapper_data *)
2857                 bnxt_ulp_cntxt_ptr2_mapper_data_get(ulp_ctx);
2858         if (!data) {
2859                 /* Go ahead and return since there is no allocated data. */
2860                 BNXT_TF_DBG(ERR, "No data appears to have been allocated.\n");
2861                 return;
2862         }
2863
2864         tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx);
2865         if (!tfp) {
2866                 BNXT_TF_DBG(ERR, "Failed to acquire tfp.\n");
2867                 /* Free the mapper data regardless of errors. */
2868                 goto free_mapper_data;
2869         }
2870
2871         /* Free the global resource info table entries */
2872         ulp_mapper_glb_resource_info_deinit(ulp_ctx, data);
2873
2874 free_mapper_data:
2875         /* Free the ulp cache tables */
2876         for (i = 0; i < BNXT_ULP_CACHE_TBL_MAX_SZ; i++) {
2877                 rte_free(data->cache_tbl[i]);
2878                 data->cache_tbl[i] = NULL;
2879         }
2880
2881         rte_free(data);
2882         /* Reset the data pointer within the ulp_ctx. */
2883         bnxt_ulp_cntxt_ptr2_mapper_data_set(ulp_ctx, NULL);
2884 }