net/bnxt: support tunnel offload
[dpdk.git] / drivers / net / bnxt / tf_ulp / ulp_mapper.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2014-2021 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_mapper.h"
18 #include "ulp_flow_db.h"
19 #include "tf_util.h"
20 #include "ulp_template_db_tbl.h"
21 #include "ulp_port_db.h"
22 #include "ulp_ha_mgr.h"
23 #include "bnxt_tf_pmd_shim.h"
24
25 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG
26 #include "ulp_template_debug_proto.h"
27 #include "ulp_tf_debug.h"
28 #endif
29
30 static uint8_t mapper_fld_zeros[16] = { 0 };
31
32 static uint8_t mapper_fld_ones[16] = {
33         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
34         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
35 };
36
37 static uint8_t mapper_fld_one[16] = {
38         0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
39         0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01
40 };
41
42 static const char *
43 ulp_mapper_tmpl_name_str(enum bnxt_ulp_template_type tmpl_type)
44 {
45         switch (tmpl_type) {
46         case BNXT_ULP_TEMPLATE_TYPE_CLASS:
47                 return "class";
48         case BNXT_ULP_TEMPLATE_TYPE_ACTION:
49                 return "action";
50         default:
51                 return "invalid template type";
52         }
53 }
54
55 static struct bnxt_ulp_glb_resource_info *
56 ulp_mapper_glb_resource_info_list_get(uint32_t *num_entries)
57 {
58         if (!num_entries)
59                 return NULL;
60         *num_entries = BNXT_ULP_GLB_RESOURCE_TBL_MAX_SZ;
61         return ulp_glb_resource_tbl;
62 }
63
64 /*
65  * Read the global resource from the mapper global resource list
66  *
67  * The regval is always returned in big-endian.
68  *
69  * returns 0 on success
70  */
71 static int32_t
72 ulp_mapper_glb_resource_read(struct bnxt_ulp_mapper_data *mapper_data,
73                              enum tf_dir dir,
74                              uint16_t idx,
75                              uint64_t *regval,
76                              bool *shared)
77 {
78         if (!mapper_data || !regval || !shared ||
79             dir >= TF_DIR_MAX || idx >= BNXT_ULP_GLB_RF_IDX_LAST)
80                 return -EINVAL;
81
82         *regval = mapper_data->glb_res_tbl[dir][idx].resource_hndl;
83         *shared = mapper_data->glb_res_tbl[dir][idx].shared;
84         return 0;
85 }
86
87 /*
88  * Write a global resource to the mapper global resource list
89  *
90  * The regval value must be in big-endian.
91  *
92  * return 0 on success.
93  */
94 static int32_t
95 ulp_mapper_glb_resource_write(struct bnxt_ulp_mapper_data *data,
96                               struct bnxt_ulp_glb_resource_info *res,
97                               uint64_t regval, bool shared)
98 {
99         struct bnxt_ulp_mapper_glb_resource_entry *ent;
100
101         /* validate the arguments */
102         if (!data || res->direction >= TF_DIR_MAX ||
103             res->glb_regfile_index >= BNXT_ULP_GLB_RF_IDX_LAST)
104                 return -EINVAL;
105
106         /* write to the mapper data */
107         ent = &data->glb_res_tbl[res->direction][res->glb_regfile_index];
108         ent->resource_func = res->resource_func;
109         ent->resource_type = res->resource_type;
110         ent->resource_hndl = regval;
111         ent->shared = shared;
112         return 0;
113 }
114
115 /*
116  * Internal function to allocate identity resource and store it in mapper data.
117  *
118  * returns 0 on success
119  */
120 static int32_t
121 ulp_mapper_resource_ident_allocate(struct bnxt_ulp_context *ulp_ctx,
122                                    struct bnxt_ulp_mapper_data *mapper_data,
123                                    struct bnxt_ulp_glb_resource_info *glb_res)
124 {
125         struct tf_alloc_identifier_parms iparms = { 0 };
126         struct tf_free_identifier_parms fparms;
127         uint64_t regval;
128         struct tf *tfp;
129         int32_t rc = 0;
130
131         tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx, BNXT_ULP_SHARED_SESSION_NO);
132         if (!tfp)
133                 return -EINVAL;
134
135         iparms.ident_type = glb_res->resource_type;
136         iparms.dir = glb_res->direction;
137
138         /* Allocate the Identifier using tf api */
139         rc = tf_alloc_identifier(tfp, &iparms);
140         if (rc) {
141                 BNXT_TF_DBG(ERR, "Failed to alloc identifier [%s][%d]\n",
142                             tf_dir_2_str(iparms.dir),
143                             iparms.ident_type);
144                 return rc;
145         }
146
147         /* entries are stored as big-endian format */
148         regval = tfp_cpu_to_be_64((uint64_t)iparms.id);
149         /*
150          * write to the mapper global resource
151          * Shared resources are never allocated through this method, so the
152          * shared flag is always false.
153          */
154         rc = ulp_mapper_glb_resource_write(mapper_data, glb_res, regval, false);
155         if (rc) {
156                 BNXT_TF_DBG(ERR, "Failed to write to global resource id\n");
157                 /* Free the identifier when update failed */
158                 fparms.dir = iparms.dir;
159                 fparms.ident_type = iparms.ident_type;
160                 fparms.id = iparms.id;
161                 tf_free_identifier(tfp, &fparms);
162                 return rc;
163         }
164 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG
165 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG_MAPPER
166         BNXT_TF_DBG(DEBUG, "Allocated Glb Res Ident [%s][%d][%d] = 0x%04x\n",
167                     tf_dir_2_str(iparms.dir),
168                     glb_res->glb_regfile_index, iparms.ident_type, iparms.id);
169 #endif
170 #endif
171         return rc;
172 }
173
174 /*
175  * Internal function to allocate index tbl resource and store it in mapper data.
176  *
177  * returns 0 on success
178  */
179 static int32_t
180 ulp_mapper_resource_index_tbl_alloc(struct bnxt_ulp_context *ulp_ctx,
181                                     struct bnxt_ulp_mapper_data *mapper_data,
182                                     struct bnxt_ulp_glb_resource_info *glb_res)
183 {
184         struct tf_alloc_tbl_entry_parms aparms = { 0 };
185         struct tf_free_tbl_entry_parms  free_parms = { 0 };
186         uint64_t regval;
187         struct tf *tfp;
188         uint32_t tbl_scope_id;
189         int32_t rc = 0;
190
191         tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx, BNXT_ULP_SHARED_SESSION_NO);
192         if (!tfp)
193                 return -EINVAL;
194
195         /* Get the scope id */
196         rc = bnxt_ulp_cntxt_tbl_scope_id_get(ulp_ctx, &tbl_scope_id);
197         if (rc) {
198                 BNXT_TF_DBG(ERR, "Failed to get table scope rc=%d\n", rc);
199                 return rc;
200         }
201
202         aparms.type = glb_res->resource_type;
203         aparms.dir = glb_res->direction;
204         aparms.tbl_scope_id = tbl_scope_id;
205
206         /* Allocate the index tbl using tf api */
207         rc = tf_alloc_tbl_entry(tfp, &aparms);
208         if (rc) {
209                 BNXT_TF_DBG(ERR, "Failed to alloc index table [%s][%d]\n",
210                             tf_dir_2_str(aparms.dir), aparms.type);
211                 return rc;
212         }
213
214         /* entries are stored as big-endian format */
215         regval = tfp_cpu_to_be_64((uint64_t)aparms.idx);
216         /*
217          * write to the mapper global resource
218          * Shared resources are never allocated through this method, so the
219          * shared flag is always false.
220          */
221         rc = ulp_mapper_glb_resource_write(mapper_data, glb_res, regval, false);
222         if (rc) {
223                 BNXT_TF_DBG(ERR, "Failed to write to global resource id\n");
224                 /* Free the identifier when update failed */
225                 free_parms.dir = aparms.dir;
226                 free_parms.type = aparms.type;
227                 free_parms.idx = aparms.idx;
228                 tf_free_tbl_entry(tfp, &free_parms);
229                 return rc;
230         }
231 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG
232 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG_MAPPER
233         BNXT_TF_DBG(DEBUG, "Allocated Glb Res Index [%s][%d][%d] = 0x%04x\n",
234                     tf_dir_2_str(aparms.dir),
235                     glb_res->glb_regfile_index, aparms.type, aparms.idx);
236 #endif
237 #endif
238         return rc;
239 }
240
241 static int32_t
242 ulp_mapper_glb_field_tbl_get(struct bnxt_ulp_mapper_parms *parms,
243                              uint32_t operand,
244                              uint8_t *val)
245 {
246         uint32_t t_idx;
247
248         t_idx = parms->app_id << (BNXT_ULP_APP_ID_SHIFT +
249                                   BNXT_ULP_HDR_SIG_ID_SHIFT +
250                                   BNXT_ULP_GLB_FIELD_TBL_SHIFT);
251         t_idx += parms->class_tid << (BNXT_ULP_HDR_SIG_ID_SHIFT +
252                                       BNXT_ULP_GLB_FIELD_TBL_SHIFT);
253         t_idx += ULP_COMP_FLD_IDX_RD(parms, BNXT_ULP_CF_IDX_HDR_SIG_ID) <<
254                 BNXT_ULP_GLB_FIELD_TBL_SHIFT;
255         t_idx += operand;
256
257         if (t_idx >= BNXT_ULP_GLB_FIELD_TBL_SIZE) {
258                 BNXT_TF_DBG(ERR, "Invalid hdr field index %x:%x:%x\n",
259                             parms->class_tid, t_idx, operand);
260                 *val = 0;
261                 return -EINVAL; /* error */
262         }
263         *val = ulp_glb_field_tbl[t_idx];
264         return 0;
265 }
266
267 /*
268  * Get the size of the action property for a given index.
269  *
270  * idx [in] The index for the action property
271  *
272  * returns the size of the action property.
273  */
274 static uint32_t
275 ulp_mapper_act_prop_size_get(uint32_t idx)
276 {
277         if (idx >= BNXT_ULP_ACT_PROP_IDX_LAST)
278                 return 0;
279         return ulp_act_prop_map_table[idx];
280 }
281
282 static struct bnxt_ulp_mapper_cond_info *
283 ulp_mapper_tmpl_reject_list_get(struct bnxt_ulp_mapper_parms *mparms,
284                                 uint32_t tid,
285                                 uint32_t *num_tbls,
286                                 enum bnxt_ulp_cond_list_opc *opc)
287 {
288         uint32_t idx;
289         const struct bnxt_ulp_template_device_tbls *dev_tbls;
290
291         dev_tbls = &mparms->device_params->dev_tbls[mparms->tmpl_type];
292         *num_tbls = dev_tbls->tmpl_list[tid].reject_info.cond_nums;
293         *opc = dev_tbls->tmpl_list[tid].reject_info.cond_list_opcode;
294         idx = dev_tbls->tmpl_list[tid].reject_info.cond_start_idx;
295
296         return &dev_tbls->cond_list[idx];
297 }
298
299 static struct bnxt_ulp_mapper_cond_info *
300 ulp_mapper_tbl_execute_list_get(struct bnxt_ulp_mapper_parms *mparms,
301                                 struct bnxt_ulp_mapper_tbl_info *tbl,
302                                 uint32_t *num_tbls,
303                                 enum bnxt_ulp_cond_list_opc *opc)
304 {
305         uint32_t idx;
306         const struct bnxt_ulp_template_device_tbls *dev_tbls;
307
308         dev_tbls = &mparms->device_params->dev_tbls[mparms->tmpl_type];
309         *num_tbls = tbl->execute_info.cond_nums;
310         *opc = tbl->execute_info.cond_list_opcode;
311         idx = tbl->execute_info.cond_start_idx;
312
313         return &dev_tbls->cond_list[idx];
314 }
315
316 /*
317  * Get a list of classifier tables that implement the flow
318  * Gets a device dependent list of tables that implement the class template id
319  *
320  * mparms [in] The mappers parms with data related to the flow.
321  *
322  * tid [in] The template id that matches the flow
323  *
324  * num_tbls [out] The number of classifier tables in the returned array
325  *
326  * returns An array of classifier tables to implement the flow, or NULL on
327  * error
328  */
329 static struct bnxt_ulp_mapper_tbl_info *
330 ulp_mapper_tbl_list_get(struct bnxt_ulp_mapper_parms *mparms,
331                         uint32_t tid,
332                         uint32_t *num_tbls)
333 {
334         uint32_t idx;
335         const struct bnxt_ulp_template_device_tbls *dev_tbls;
336
337         dev_tbls = &mparms->device_params->dev_tbls[mparms->tmpl_type];
338
339         idx = dev_tbls->tmpl_list[tid].start_tbl_idx;
340         *num_tbls = dev_tbls->tmpl_list[tid].num_tbls;
341
342         return &dev_tbls->tbl_list[idx];
343 }
344
345 /*
346  * Get the list of key fields that implement the flow.
347  *
348  * mparms [in] The mapper parms with information about the flow
349  *
350  * tbl [in] A single table instance to get the key fields from
351  *
352  * num_flds [out] The number of key fields in the returned array
353  *
354  * Returns array of Key fields, or NULL on error.
355  */
356 static struct bnxt_ulp_mapper_key_info *
357 ulp_mapper_key_fields_get(struct bnxt_ulp_mapper_parms *mparms,
358                           struct bnxt_ulp_mapper_tbl_info *tbl,
359                           uint32_t *num_flds)
360 {
361         uint32_t idx;
362         const struct bnxt_ulp_template_device_tbls *dev_tbls;
363
364         dev_tbls = &mparms->device_params->dev_tbls[mparms->tmpl_type];
365         if (!dev_tbls->key_info_list) {
366                 *num_flds = 0;
367                 return NULL;
368         }
369
370         idx             = tbl->key_start_idx;
371         *num_flds       = tbl->key_num_fields;
372
373         return &dev_tbls->key_info_list[idx];
374 }
375
376 /*
377  * Get the list of data fields that implement the flow.
378  *
379  * mparms [in] The mapper parms with information about the flow
380  *
381  * tbl [in] A single table instance to get the data fields from
382  *
383  * num_flds [out] The number of data fields in the returned array.
384  *
385  * num_encap_flds [out] The number of encap fields in the returned array.
386  *
387  * Returns array of data fields, or NULL on error.
388  */
389 static struct bnxt_ulp_mapper_field_info *
390 ulp_mapper_result_fields_get(struct bnxt_ulp_mapper_parms *mparms,
391                              struct bnxt_ulp_mapper_tbl_info *tbl,
392                              uint32_t *num_flds,
393                              uint32_t *num_encap_flds)
394 {
395         uint32_t idx;
396         const struct bnxt_ulp_template_device_tbls *dev_tbls;
397
398         dev_tbls = &mparms->device_params->dev_tbls[mparms->tmpl_type];
399         if (!dev_tbls->result_field_list) {
400                 *num_flds = 0;
401                 *num_encap_flds = 0;
402                 return NULL;
403         }
404
405         idx             = tbl->result_start_idx;
406         *num_flds       = tbl->result_num_fields;
407         *num_encap_flds = tbl->encap_num_fields;
408
409         return &dev_tbls->result_field_list[idx];
410 }
411
412 /*
413  * Get the list of ident fields that implement the flow
414  *
415  * tbl [in] A single table instance to get the ident fields from
416  *
417  * num_flds [out] The number of ident fields in the returned array
418  *
419  * returns array of ident fields, or NULL on error
420  */
421 static struct bnxt_ulp_mapper_ident_info *
422 ulp_mapper_ident_fields_get(struct bnxt_ulp_mapper_parms *mparms,
423                             struct bnxt_ulp_mapper_tbl_info *tbl,
424                             uint32_t *num_flds)
425 {
426         uint32_t idx;
427         const struct bnxt_ulp_template_device_tbls *dev_tbls;
428
429         dev_tbls = &mparms->device_params->dev_tbls[mparms->tmpl_type];
430         if (!dev_tbls->ident_list) {
431                 *num_flds = 0;
432                 return NULL;
433         }
434
435         idx = tbl->ident_start_idx;
436         *num_flds = tbl->ident_nums;
437
438         return &dev_tbls->ident_list[idx];
439 }
440
441 static inline int32_t
442 ulp_mapper_tcam_entry_free(struct bnxt_ulp_context *ulp,
443                            struct tf *tfp,
444                            struct ulp_flow_db_res_params *res)
445 {
446         struct tf_free_tcam_entry_parms fparms = {
447                 .dir            = res->direction,
448                 .tcam_tbl_type  = res->resource_type,
449                 .idx            = (uint16_t)res->resource_hndl
450         };
451
452         /* If HA is enabled, we may have to remap the TF Type */
453         if (bnxt_ulp_cntxt_ha_enabled(ulp)) {
454                 enum ulp_ha_mgr_region region;
455                 int32_t rc;
456
457                 switch (res->resource_type) {
458                 case TF_TCAM_TBL_TYPE_WC_TCAM_HIGH:
459                 case TF_TCAM_TBL_TYPE_WC_TCAM_LOW:
460                         rc = ulp_ha_mgr_region_get(ulp, &region);
461                         if (rc)
462                                 /* Log this, but assume region is correct */
463                                 BNXT_TF_DBG(ERR,
464                                             "Unable to get HA region (%d)\n",
465                                             rc);
466                         else
467                                 fparms.tcam_tbl_type =
468                                         (region == ULP_HA_REGION_LOW) ?
469                                         TF_TCAM_TBL_TYPE_WC_TCAM_LOW :
470                                         TF_TCAM_TBL_TYPE_WC_TCAM_HIGH;
471                         break;
472                 default:
473                         break;
474                 }
475         }
476         return tf_free_tcam_entry(tfp, &fparms);
477 }
478
479 static inline int32_t
480 ulp_mapper_index_entry_free(struct bnxt_ulp_context *ulp,
481                             struct tf *tfp,
482                             struct ulp_flow_db_res_params *res)
483 {
484         struct tf_free_tbl_entry_parms fparms = {
485                 .dir    = res->direction,
486                 .type   = res->resource_type,
487                 .idx    = (uint32_t)res->resource_hndl
488         };
489
490         /*
491          * Just get the table scope, it will be ignored if not necessary
492          * by the tf_free_tbl_entry
493          */
494         (void)bnxt_ulp_cntxt_tbl_scope_id_get(ulp, &fparms.tbl_scope_id);
495
496         return tf_free_tbl_entry(tfp, &fparms);
497 }
498
499 static inline int32_t
500 ulp_mapper_em_entry_free(struct bnxt_ulp_context *ulp,
501                          struct tf *tfp,
502                          struct ulp_flow_db_res_params *res)
503 {
504         struct tf_delete_em_entry_parms fparms = { 0 };
505         int32_t rc;
506
507         fparms.dir              = res->direction;
508         fparms.flow_handle      = res->resource_hndl;
509
510         rc = bnxt_ulp_cntxt_tbl_scope_id_get(ulp, &fparms.tbl_scope_id);
511         if (rc) {
512                 BNXT_TF_DBG(ERR, "Failed to get table scope\n");
513                 return -EINVAL;
514         }
515
516         return tf_delete_em_entry(tfp, &fparms);
517 }
518
519 static inline int32_t
520 ulp_mapper_ident_free(struct bnxt_ulp_context *ulp __rte_unused,
521                       struct tf *tfp,
522                       struct ulp_flow_db_res_params *res)
523 {
524         struct tf_free_identifier_parms fparms = {
525                 .dir            = res->direction,
526                 .ident_type     = res->resource_type,
527                 .id             = (uint16_t)res->resource_hndl
528         };
529
530         return tf_free_identifier(tfp, &fparms);
531 }
532
533 static inline int32_t
534 ulp_mapper_mark_free(struct bnxt_ulp_context *ulp,
535                      struct ulp_flow_db_res_params *res)
536 {
537         return ulp_mark_db_mark_del(ulp,
538                                     res->resource_type,
539                                     res->resource_hndl);
540 }
541
542 static inline int32_t
543 ulp_mapper_parent_flow_free(struct bnxt_ulp_context *ulp,
544                             uint32_t parent_fid,
545                             struct ulp_flow_db_res_params *res)
546 {
547         uint32_t pc_idx;
548
549         pc_idx = (uint32_t)res->resource_hndl;
550
551         /* reset the child flow bitset*/
552         if (ulp_flow_db_pc_db_parent_flow_set(ulp, pc_idx, parent_fid, 0)) {
553                 BNXT_TF_DBG(ERR, "error in reset parent flow bitset %x:%x\n",
554                             pc_idx, parent_fid);
555                 return -EINVAL;
556         }
557         return 0;
558 }
559
560 static inline int32_t
561 ulp_mapper_child_flow_free(struct bnxt_ulp_context *ulp,
562                            uint32_t child_fid,
563                            struct ulp_flow_db_res_params *res)
564 {
565         uint32_t pc_idx;
566
567         pc_idx = (uint32_t)res->resource_hndl;
568
569         /* reset the child flow bitset*/
570         if (ulp_flow_db_pc_db_child_flow_set(ulp, pc_idx, child_fid, 0)) {
571                 BNXT_TF_DBG(ERR, "error in resetting child flow bitset %x:%x\n",
572                             pc_idx, child_fid);
573                 return -EINVAL;
574         }
575         return 0;
576 }
577
578 /*
579  * Process the flow database opcode alloc action.
580  * returns 0 on success
581  */
582 static int32_t
583 ulp_mapper_fdb_opc_alloc_rid(struct bnxt_ulp_mapper_parms *parms,
584                              struct bnxt_ulp_mapper_tbl_info *tbl)
585 {
586         uint32_t rid = 0;
587         uint64_t val64;
588         int32_t rc = 0;
589
590         /* allocate a new fid */
591         rc = ulp_flow_db_fid_alloc(parms->ulp_ctx,
592                                    BNXT_ULP_FDB_TYPE_RID,
593                                    0, &rid);
594         if (rc) {
595                 BNXT_TF_DBG(ERR,
596                             "Unable to allocate flow table entry\n");
597                 return -EINVAL;
598         }
599         /* Store the allocated fid in regfile*/
600         val64 = rid;
601         rc = ulp_regfile_write(parms->regfile, tbl->fdb_operand,
602                                tfp_cpu_to_be_64(val64));
603         if (rc) {
604                 BNXT_TF_DBG(ERR, "Write regfile[%d] failed\n",
605                             tbl->fdb_operand);
606                 ulp_flow_db_fid_free(parms->ulp_ctx,
607                                      BNXT_ULP_FDB_TYPE_RID, rid);
608                 return -EINVAL;
609         }
610         return 0;
611 }
612
613 /*
614  * Process the flow database opcode action.
615  * returns 0 on success.
616  */
617 static int32_t
618 ulp_mapper_fdb_opc_process(struct bnxt_ulp_mapper_parms *parms,
619                            struct bnxt_ulp_mapper_tbl_info *tbl,
620                            struct ulp_flow_db_res_params *fid_parms)
621 {
622         uint32_t push_fid;
623         uint64_t val64;
624         enum bnxt_ulp_fdb_type flow_type;
625         int32_t rc = 0;
626
627         switch (tbl->fdb_opcode) {
628         case BNXT_ULP_FDB_OPC_PUSH_FID:
629                 push_fid = parms->fid;
630                 flow_type = parms->flow_type;
631                 break;
632         case BNXT_ULP_FDB_OPC_PUSH_RID_REGFILE:
633                 /* get the fid from the regfile */
634                 rc = ulp_regfile_read(parms->regfile, tbl->fdb_operand,
635                                       &val64);
636                 if (!rc) {
637                         BNXT_TF_DBG(ERR, "regfile[%d] read oob\n",
638                                     tbl->fdb_operand);
639                         return -EINVAL;
640                 }
641                 /* Use the extracted fid to update the flow resource */
642                 push_fid = (uint32_t)tfp_be_to_cpu_64(val64);
643                 flow_type = BNXT_ULP_FDB_TYPE_RID;
644                 break;
645         default:
646                 return rc; /* Nothing to be done */
647         }
648
649         /* Add the resource to the flow database */
650         rc = ulp_flow_db_resource_add(parms->ulp_ctx, flow_type,
651                                       push_fid, fid_parms);
652         if (rc)
653                 BNXT_TF_DBG(ERR, "Failed to add res to flow %x rc = %d\n",
654                             push_fid, rc);
655         return rc;
656 }
657
658 /*
659  * Process the flow database opcode action.
660  * returns 0 on success.
661  */
662 static int32_t
663 ulp_mapper_priority_opc_process(struct bnxt_ulp_mapper_parms *parms,
664                                 struct bnxt_ulp_mapper_tbl_info *tbl,
665                                 uint32_t *priority)
666 {
667         int32_t rc = 0;
668
669         switch (tbl->pri_opcode) {
670         case BNXT_ULP_PRI_OPC_NOT_USED:
671                 *priority = 0;
672                 break;
673         case BNXT_ULP_PRI_OPC_CONST:
674                 *priority = tbl->pri_operand;
675                 break;
676         case BNXT_ULP_PRI_OPC_APP_PRI:
677                 *priority = parms->app_priority;
678                 break;
679         default:
680                 BNXT_TF_DBG(ERR, "Priority opcode not supported %d\n",
681                             tbl->pri_opcode);
682                 rc = -EINVAL;
683                 break;
684         }
685         return rc;
686 }
687
688 /*
689  * Process the identifier list in the given table.
690  * Extract the ident from the table entry and
691  * write it to the reg file.
692  * returns 0 on success.
693  */
694 static int32_t
695 ulp_mapper_tbl_ident_scan_ext(struct bnxt_ulp_mapper_parms *parms,
696                               struct bnxt_ulp_mapper_tbl_info *tbl,
697                               uint8_t *byte_data,
698                               uint32_t byte_data_size,
699                               enum bnxt_ulp_byte_order byte_order)
700 {
701         struct bnxt_ulp_mapper_ident_info *idents;
702         uint32_t i, num_idents = 0;
703         uint64_t val64;
704
705         /* validate the null arguments */
706         if (!byte_data) {
707                 BNXT_TF_DBG(ERR, "invalid argument\n");
708                 return -EINVAL;
709         }
710
711         /* Get the ident list and process each one */
712         idents = ulp_mapper_ident_fields_get(parms, tbl, &num_idents);
713
714         for (i = 0; i < num_idents; i++) {
715                 /* check the size of the buffer for validation */
716                 if ((idents[i].ident_bit_pos + idents[i].ident_bit_size) >
717                     ULP_BYTE_2_BITS(byte_data_size) ||
718                     idents[i].ident_bit_size > ULP_BYTE_2_BITS(sizeof(val64))) {
719                         BNXT_TF_DBG(ERR, "invalid offset or length %x:%x:%x\n",
720                                     idents[i].ident_bit_pos,
721                                     idents[i].ident_bit_size,
722                                     byte_data_size);
723                         return -EINVAL;
724                 }
725                 val64 = 0;
726                 if (byte_order == BNXT_ULP_BYTE_ORDER_LE)
727                         ulp_bs_pull_lsb(byte_data, (uint8_t *)&val64,
728                                         sizeof(val64),
729                                         idents[i].ident_bit_pos,
730                                         idents[i].ident_bit_size);
731                 else
732                         ulp_bs_pull_msb(byte_data, (uint8_t *)&val64,
733                                         idents[i].ident_bit_pos,
734                                         idents[i].ident_bit_size);
735
736                 /* Write it to the regfile, val64 is already in big-endian*/
737                 if (ulp_regfile_write(parms->regfile,
738                                       idents[i].regfile_idx, val64)) {
739                         BNXT_TF_DBG(ERR, "Regfile[%d] write failed.\n",
740                                     idents[i].regfile_idx);
741                         return -EINVAL;
742                 }
743         }
744         return 0;
745 }
746
747 /*
748  * Process the identifier instruction and either store it in the flow database
749  * or return it in the val (if not NULL) on success.  If val is NULL, the
750  * identifier is to be stored in the flow database.
751  */
752 static int32_t
753 ulp_mapper_ident_process(struct bnxt_ulp_mapper_parms *parms,
754                          struct bnxt_ulp_mapper_tbl_info *tbl,
755                          struct bnxt_ulp_mapper_ident_info *ident,
756                          uint16_t *val)
757 {
758         struct ulp_flow_db_res_params   fid_parms;
759         uint64_t id = 0;
760         int32_t idx;
761         struct tf_alloc_identifier_parms iparms = { 0 };
762         struct tf_free_identifier_parms free_parms = { 0 };
763         struct tf *tfp;
764         int rc;
765
766         tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx, tbl->shared_session);
767         if (!tfp) {
768                 BNXT_TF_DBG(ERR, "Failed to get tf pointer\n");
769                 return -EINVAL;
770         }
771
772         idx = ident->regfile_idx;
773
774         iparms.ident_type = ident->ident_type;
775         iparms.dir = tbl->direction;
776
777         rc = tf_alloc_identifier(tfp, &iparms);
778         if (rc) {
779                 BNXT_TF_DBG(ERR, "Alloc ident %s:%s failed.\n",
780                             tf_dir_2_str(iparms.dir),
781                             tf_ident_2_str(iparms.ident_type));
782                 return rc;
783         }
784         BNXT_TF_INF("Alloc ident %s:%s.success.\n",
785                     tf_dir_2_str(iparms.dir),
786                     tf_ident_2_str(iparms.ident_type));
787
788         id = (uint64_t)tfp_cpu_to_be_64(iparms.id);
789         if (ulp_regfile_write(parms->regfile, idx, id)) {
790                 BNXT_TF_DBG(ERR, "Regfile[%d] write failed.\n", idx);
791                 rc = -EINVAL;
792                 /* Need to free the identifier, so goto error */
793                 goto error;
794         }
795
796         /* Link the resource to the flow in the flow db */
797         if (!val) {
798                 memset(&fid_parms, 0, sizeof(fid_parms));
799                 fid_parms.direction             = tbl->direction;
800                 fid_parms.resource_func = ident->resource_func;
801                 fid_parms.resource_type = ident->ident_type;
802                 fid_parms.resource_hndl = iparms.id;
803                 fid_parms.critical_resource = tbl->critical_resource;
804                 ulp_flow_db_shared_session_set(&fid_parms, tbl->shared_session);
805
806                 rc = ulp_mapper_fdb_opc_process(parms, tbl, &fid_parms);
807                 if (rc) {
808                         BNXT_TF_DBG(ERR, "Failed to link res to flow rc = %d\n",
809                                     rc);
810                         /* Need to free the identifier, so goto error */
811                         goto error;
812                 }
813         } else {
814                 *val = iparms.id;
815         }
816 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG
817 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG_MAPPER
818         ulp_mapper_ident_field_dump("Ident", ident, tbl, iparms.id);
819 #endif
820 #endif
821         return 0;
822
823 error:
824         /* Need to free the identifier */
825         free_parms.dir          = tbl->direction;
826         free_parms.ident_type   = ident->ident_type;
827         free_parms.id           = iparms.id;
828
829         (void)tf_free_identifier(tfp, &free_parms);
830
831         BNXT_TF_DBG(ERR, "Ident process failed for %s:%s\n",
832                     ident->description,
833                     tf_dir_2_str(tbl->direction));
834         return rc;
835 }
836
837 /*
838  * Process the identifier instruction and extract it from result blob.
839  * Increment the identifier reference count and store it in the flow database.
840  */
841 static int32_t
842 ulp_mapper_ident_extract(struct bnxt_ulp_mapper_parms *parms,
843                          struct bnxt_ulp_mapper_tbl_info *tbl,
844                          struct bnxt_ulp_mapper_ident_info *ident,
845                          struct ulp_blob *res_blob)
846 {
847         struct ulp_flow_db_res_params   fid_parms;
848         uint64_t id = 0;
849         uint32_t idx = 0;
850         struct tf_search_identifier_parms sparms = { 0 };
851         struct tf_free_identifier_parms free_parms = { 0 };
852         struct tf *tfp;
853         int rc;
854
855         /* Get the tfp from ulp context */
856         tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx, tbl->shared_session);
857         if (!tfp) {
858                 BNXT_TF_DBG(ERR, "Failed to get tf pointer\n");
859                 return -EINVAL;
860         }
861
862         /* Extract the index from the result blob */
863         rc = ulp_blob_pull(res_blob, (uint8_t *)&idx, sizeof(idx),
864                            ident->ident_bit_pos, ident->ident_bit_size);
865         if (rc) {
866                 BNXT_TF_DBG(ERR, "Failed to extract identifier from blob\n");
867                 return -EIO;
868         }
869
870         /* populate the search params and search identifier shadow table */
871         sparms.ident_type = ident->ident_type;
872         sparms.dir = tbl->direction;
873         /* convert the idx into cpu format */
874         sparms.search_id = tfp_be_to_cpu_32(idx);
875
876         /* Search identifier also increase the reference count */
877         rc = tf_search_identifier(tfp, &sparms);
878         if (rc) {
879                 BNXT_TF_DBG(ERR, "Search ident %s:%s:%x failed.\n",
880                             tf_dir_2_str(sparms.dir),
881                             tf_ident_2_str(sparms.ident_type),
882                             sparms.search_id);
883                 return rc;
884         }
885         BNXT_TF_INF("Search ident %s:%s:%x.success.\n",
886                     tf_dir_2_str(sparms.dir),
887                     tf_tbl_type_2_str(sparms.ident_type),
888                     sparms.search_id);
889
890         /* Write it to the regfile */
891         id = (uint64_t)tfp_cpu_to_be_64(sparms.search_id);
892         if (ulp_regfile_write(parms->regfile, ident->regfile_idx, id)) {
893                 BNXT_TF_DBG(ERR, "Regfile[%d] write failed.\n", idx);
894                 rc = -EINVAL;
895                 /* Need to free the identifier, so goto error */
896                 goto error;
897         }
898
899         /* Link the resource to the flow in the flow db */
900         memset(&fid_parms, 0, sizeof(fid_parms));
901         fid_parms.direction = tbl->direction;
902         fid_parms.resource_func = ident->resource_func;
903         fid_parms.resource_type = ident->ident_type;
904         fid_parms.resource_hndl = sparms.search_id;
905         fid_parms.critical_resource = tbl->critical_resource;
906         ulp_flow_db_shared_session_set(&fid_parms, tbl->shared_session);
907
908         rc = ulp_mapper_fdb_opc_process(parms, tbl, &fid_parms);
909         if (rc) {
910                 BNXT_TF_DBG(ERR, "Failed to link res to flow rc = %d\n",
911                             rc);
912                 /* Need to free the identifier, so goto error */
913                 goto error;
914         }
915
916 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG
917 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG_MAPPER
918         ulp_mapper_ident_field_dump("Ident", ident, tbl, sparms.search_id);
919 #endif
920 #endif
921         return 0;
922
923 error:
924         /* Need to free the identifier */
925         free_parms.dir = tbl->direction;
926         free_parms.ident_type = ident->ident_type;
927         free_parms.id = sparms.search_id;
928         (void)tf_free_identifier(tfp, &free_parms);
929         BNXT_TF_DBG(ERR, "Ident extract failed for %s:%s:%x\n",
930                     ident->description,
931                     tf_dir_2_str(tbl->direction), sparms.search_id);
932         return rc;
933 }
934
935 static int32_t
936 ulp_mapper_field_port_db_process(struct bnxt_ulp_mapper_parms *parms,
937                                  uint32_t port_id,
938                                  uint16_t val16,
939                                  uint8_t **val)
940 {
941         enum bnxt_ulp_port_table port_data = val16;
942
943         switch (port_data) {
944         case BNXT_ULP_PORT_TABLE_DRV_FUNC_PARENT_MAC:
945                 if (ulp_port_db_parent_mac_addr_get(parms->ulp_ctx, port_id,
946                                                     val)) {
947                         BNXT_TF_DBG(ERR, "Invalid port id %u\n", port_id);
948                         return -EINVAL;
949                 }
950                 break;
951         case BNXT_ULP_PORT_TABLE_DRV_FUNC_MAC:
952                 if (ulp_port_db_drv_mac_addr_get(parms->ulp_ctx, port_id,
953                                                  val)) {
954                         BNXT_TF_DBG(ERR, "Invalid port id %u\n", port_id);
955                         return -EINVAL;
956                 }
957                 break;
958         case BNXT_ULP_PORT_TABLE_DRV_FUNC_PARENT_VNIC:
959                 if (ulp_port_db_parent_vnic_get(parms->ulp_ctx, port_id,
960                                                 val)) {
961                         BNXT_TF_DBG(ERR, "Invalid port id %u\n", port_id);
962                         return -EINVAL;
963                 }
964                 break;
965         default:
966                 BNXT_TF_DBG(ERR, "Invalid port_data %d\n", port_data);
967                 return -EINVAL;
968         }
969         return 0;
970 }
971
972 static int32_t
973 ulp_mapper_field_src_process(struct bnxt_ulp_mapper_parms *parms,
974                              enum bnxt_ulp_field_src field_src,
975                              uint8_t *field_opr,
976                              enum tf_dir dir,
977                              uint8_t is_key,
978                              uint32_t bitlen,
979                              uint8_t **val,
980                              uint32_t *val_len,
981                              uint64_t *value)
982 {
983         struct bnxt_ulp_mapper_data *m;
984         uint8_t bit;
985         uint32_t port_id, val_size, field_size;
986         uint16_t idx, size_idx, offset;
987         uint32_t bytelen = ULP_BITS_2_BYTE(bitlen);
988         uint8_t *buffer;
989         uint64_t lregval;
990         bool shared;
991
992         *val_len = bitlen;
993         *value = 0;
994         /* Perform the action */
995         switch (field_src) {
996         case BNXT_ULP_FIELD_SRC_ZERO:
997                 *val = mapper_fld_zeros;
998                 break;
999         case BNXT_ULP_FIELD_SRC_CONST:
1000                 *val = field_opr;
1001                 break;
1002         case BNXT_ULP_FIELD_SRC_ONES:
1003                 *val = mapper_fld_ones;
1004                 *value = 1;
1005                 break;
1006         case BNXT_ULP_FIELD_SRC_CF:
1007                 if (!ulp_operand_read(field_opr,
1008                                       (uint8_t *)&idx, sizeof(uint16_t))) {
1009                         BNXT_TF_DBG(ERR, "CF operand read failed\n");
1010                         return -EINVAL;
1011                 }
1012                 idx = tfp_be_to_cpu_16(idx);
1013                 if (idx >= BNXT_ULP_CF_IDX_LAST || bytelen > sizeof(uint32_t)) {
1014                         BNXT_TF_DBG(ERR, "comp field [%d] read oob %d\n", idx,
1015                                     bytelen);
1016                         return -EINVAL;
1017                 }
1018                 buffer = (uint8_t *)&parms->comp_fld[idx];
1019                 *val = &buffer[sizeof(uint64_t) - bytelen];
1020                 *value = ULP_COMP_FLD_IDX_RD(parms, idx);
1021                 break;
1022         case BNXT_ULP_FIELD_SRC_RF:
1023                 if (!ulp_operand_read(field_opr,
1024                                       (uint8_t *)&idx, sizeof(uint16_t))) {
1025                         BNXT_TF_DBG(ERR, "RF operand read failed\n");
1026                         return -EINVAL;
1027                 }
1028
1029                 idx = tfp_be_to_cpu_16(idx);
1030                 /* Uninitialized regfile entries return 0 */
1031                 if (!ulp_regfile_read(parms->regfile, idx, &lregval) ||
1032                     sizeof(uint64_t) < bytelen) {
1033                         BNXT_TF_DBG(ERR, "regfile[%d] read oob %u\n", idx,
1034                                     bytelen);
1035                         return -EINVAL;
1036                 }
1037                 buffer = (uint8_t *)&parms->regfile->entry[idx].data;
1038                 *val = &buffer[sizeof(uint64_t) - bytelen];
1039                 *value = tfp_be_to_cpu_64(lregval);
1040                 break;
1041         case BNXT_ULP_FIELD_SRC_ACT_PROP:
1042                 if (!ulp_operand_read(field_opr,
1043                                       (uint8_t *)&idx, sizeof(uint16_t))) {
1044                         BNXT_TF_DBG(ERR, "Action operand read failed\n");
1045                         return -EINVAL;
1046                 }
1047                 idx = tfp_be_to_cpu_16(idx);
1048                 if (idx >= BNXT_ULP_ACT_PROP_IDX_LAST) {
1049                         BNXT_TF_DBG(ERR, "act_prop[%d] oob\n", idx);
1050                         return -EINVAL;
1051                 }
1052                 buffer = &parms->act_prop->act_details[idx];
1053                 field_size = ulp_mapper_act_prop_size_get(idx);
1054                 if (bytelen > field_size) {
1055                         BNXT_TF_DBG(ERR, "act_prop[%d] field size small %u\n",
1056                                     idx, field_size);
1057                         return -EINVAL;
1058                 }
1059                 *val = &buffer[field_size - bytelen];
1060                 break;
1061         case BNXT_ULP_FIELD_SRC_ACT_PROP_SZ:
1062                 if (!ulp_operand_read(field_opr,
1063                                       (uint8_t *)&idx, sizeof(uint16_t))) {
1064                         BNXT_TF_DBG(ERR, "Action sz operand read failed\n");
1065                         return -EINVAL;
1066                 }
1067                 idx = tfp_be_to_cpu_16(idx);
1068
1069                 if (idx >= BNXT_ULP_ACT_PROP_IDX_LAST) {
1070                         BNXT_TF_DBG(ERR, "act_prop_sz[%d] oob\n", idx);
1071                         return -EINVAL;
1072                 }
1073                 *val = &parms->act_prop->act_details[idx];
1074
1075                 /* get the size index next */
1076                 if (!ulp_operand_read(&field_opr[sizeof(uint16_t)],
1077                                       (uint8_t *)&size_idx, sizeof(uint16_t))) {
1078                         BNXT_TF_DBG(ERR, "Action sz operand read failed\n");
1079                         return -EINVAL;
1080                 }
1081                 size_idx = tfp_be_to_cpu_16(size_idx);
1082                 if (size_idx >= BNXT_ULP_ACT_PROP_IDX_LAST) {
1083                         BNXT_TF_DBG(ERR, "act_prop[%d] oob\n", size_idx);
1084                         return -EINVAL;
1085                 }
1086                 memcpy(&val_size, &parms->act_prop->act_details[size_idx],
1087                        sizeof(uint32_t));
1088                 val_size = tfp_be_to_cpu_32(val_size);
1089                 *val_len = ULP_BYTE_2_BITS(val_size);
1090                 break;
1091         case BNXT_ULP_FIELD_SRC_GLB_RF:
1092                 if (!ulp_operand_read(field_opr,
1093                                       (uint8_t *)&idx, sizeof(uint16_t))) {
1094                         BNXT_TF_DBG(ERR, "Global regfile read failed\n");
1095                         return -EINVAL;
1096                 }
1097                 idx = tfp_be_to_cpu_16(idx);
1098                 if (ulp_mapper_glb_resource_read(parms->mapper_data,
1099                                                  dir, idx, &lregval, &shared) ||
1100                     sizeof(uint64_t) < bytelen) {
1101                         BNXT_TF_DBG(ERR, "Global regfile[%d] read failed %u\n",
1102                                     idx, bytelen);
1103                         return -EINVAL;
1104                 }
1105                 m = parms->mapper_data;
1106                 buffer = (uint8_t *)&m->glb_res_tbl[dir][idx].resource_hndl;
1107                 *val = &buffer[sizeof(uint64_t) - bytelen];
1108                 *value = tfp_be_to_cpu_64(lregval);
1109                 break;
1110         case BNXT_ULP_FIELD_SRC_HF:
1111         case BNXT_ULP_FIELD_SRC_SUB_HF:
1112                 if (!ulp_operand_read(field_opr,
1113                                       (uint8_t *)&idx, sizeof(uint16_t))) {
1114                         BNXT_TF_DBG(ERR, "Header field read failed\n");
1115                         return -EINVAL;
1116                 }
1117                 idx = tfp_be_to_cpu_16(idx);
1118                 /* get the index from the global field list */
1119                 if (ulp_mapper_glb_field_tbl_get(parms, idx, &bit)) {
1120                         BNXT_TF_DBG(ERR, "invalid ulp_glb_field_tbl idx %d\n",
1121                                     idx);
1122                         return -EINVAL;
1123                 }
1124                 if (is_key)
1125                         buffer = parms->hdr_field[bit].spec;
1126                 else
1127                         buffer = parms->hdr_field[bit].mask;
1128
1129                 field_size = parms->hdr_field[bit].size;
1130                 if (bytelen > field_size) {
1131                         BNXT_TF_DBG(ERR, "Hdr field[%d] size small %u\n",
1132                                     bit, field_size);
1133                         return -EINVAL;
1134                 }
1135                 if (field_src == BNXT_ULP_FIELD_SRC_HF) {
1136                         *val = &buffer[field_size - bytelen];
1137                 } else {
1138                         /* get the offset next */
1139                         if (!ulp_operand_read(&field_opr[sizeof(uint16_t)],
1140                                               (uint8_t *)&offset,
1141                                               sizeof(uint16_t))) {
1142                                 BNXT_TF_DBG(ERR, "Hdr fld size read failed\n");
1143                                 return -EINVAL;
1144                         }
1145                         offset = tfp_be_to_cpu_16(offset);
1146                         offset = ULP_BITS_2_BYTE_NR(offset);
1147                         if ((offset + bytelen) > field_size) {
1148                                 BNXT_TF_DBG(ERR, "Hdr field[%d] oob\n", bit);
1149                                 return -EINVAL;
1150                         }
1151                         *val = &buffer[offset];
1152                 }
1153                 break;
1154         case BNXT_ULP_FIELD_SRC_HDR_BIT:
1155                 if (!ulp_operand_read(field_opr,
1156                                       (uint8_t *)&lregval, sizeof(uint64_t))) {
1157                         BNXT_TF_DBG(ERR, "Header bit read failed\n");
1158                         return -EINVAL;
1159                 }
1160                 lregval = tfp_be_to_cpu_64(lregval);
1161                 if (ULP_BITMAP_ISSET(parms->hdr_bitmap->bits, lregval)) {
1162                         *val = mapper_fld_one;
1163                         *value = 1;
1164                 } else {
1165                         *val = mapper_fld_zeros;
1166                 }
1167                 break;
1168         case BNXT_ULP_FIELD_SRC_ACT_BIT:
1169                 if (!ulp_operand_read(field_opr,
1170                                       (uint8_t *)&lregval, sizeof(uint64_t))) {
1171                         BNXT_TF_DBG(ERR, "Action bit read failed\n");
1172                         return -EINVAL;
1173                 }
1174                 lregval = tfp_be_to_cpu_64(lregval);
1175                 if (ULP_BITMAP_ISSET(parms->act_bitmap->bits, lregval)) {
1176                         *val = mapper_fld_one;
1177                         *value = 1;
1178                 } else {
1179                         *val = mapper_fld_zeros;
1180                 }
1181                 break;
1182         case BNXT_ULP_FIELD_SRC_FIELD_BIT:
1183                 if (!ulp_operand_read(field_opr,
1184                                       (uint8_t *)&idx, sizeof(uint16_t))) {
1185                         BNXT_TF_DBG(ERR, "Field bit read failed\n");
1186                         return -EINVAL;
1187                 }
1188                 idx = tfp_be_to_cpu_16(idx);
1189                 /* get the index from the global field list */
1190                 if (ulp_mapper_glb_field_tbl_get(parms, idx, &bit)) {
1191                         BNXT_TF_DBG(ERR, "invalid ulp_glb_field_tbl idx %d\n",
1192                                     idx);
1193                         return -EINVAL;
1194                 }
1195                 if (ULP_INDEX_BITMAP_GET(parms->fld_bitmap->bits, bit)) {
1196                         *val = mapper_fld_one;
1197                         *value = 1;
1198                 } else {
1199                         *val = mapper_fld_zeros;
1200                 }
1201                 break;
1202         case BNXT_ULP_FIELD_SRC_PORT_TABLE:
1203                 /* The port id is present in the comp field list */
1204                 port_id = ULP_COMP_FLD_IDX_RD(parms,
1205                                               BNXT_ULP_CF_IDX_DEV_PORT_ID);
1206                 /* get the port table enum  */
1207                 if (!ulp_operand_read(field_opr,
1208                                       (uint8_t *)&idx, sizeof(uint16_t))) {
1209                         BNXT_TF_DBG(ERR, "Port table enum read failed\n");
1210                         return -EINVAL;
1211                 }
1212                 idx = tfp_be_to_cpu_16(idx);
1213                 if (ulp_mapper_field_port_db_process(parms, port_id, idx,
1214                                                      val)) {
1215                         BNXT_TF_DBG(ERR, "field port table failed\n");
1216                         return -EINVAL;
1217                 }
1218         case BNXT_ULP_FIELD_SRC_SKIP:
1219                 /* do nothing */
1220                 break;
1221         case BNXT_ULP_FIELD_SRC_REJECT:
1222                 return -EINVAL;
1223         default:
1224                 BNXT_TF_DBG(ERR, "invalid field opcode 0x%x\n", field_src);
1225                 return -EINVAL;
1226         }
1227         return 0;
1228 }
1229
1230 static int32_t ulp_mapper_field_buffer_eval(uint8_t *buffer, uint32_t bitlen,
1231                                             uint64_t *output)
1232 {
1233         uint16_t val_16;
1234         uint32_t val_32;
1235         uint64_t val_64;
1236         uint32_t bytelen;
1237
1238         bytelen = ULP_BITS_2_BYTE(bitlen);
1239         if (bytelen == sizeof(uint8_t)) {
1240                 *output = *((uint8_t *)buffer);
1241         } else if (bytelen == sizeof(uint16_t)) {
1242                 val_16 = *((uint16_t *)buffer);
1243                 *output =  tfp_be_to_cpu_16(val_16);
1244         } else if (bytelen == sizeof(uint32_t)) {
1245                 val_32 = *((uint32_t *)buffer);
1246                 *output =  tfp_be_to_cpu_32(val_32);
1247         } else if (bytelen == sizeof(val_64)) {
1248                 val_64 = *((uint64_t *)buffer);
1249                 *output =  tfp_be_to_cpu_64(val_64);
1250         } else {
1251                 *output = 0;
1252                 return -EINVAL;
1253         }
1254         return 0;
1255 }
1256
1257 static int32_t ulp_mapper_field_blob_write(enum bnxt_ulp_field_src fld_src,
1258                                            struct ulp_blob *blob,
1259                                            uint8_t *val,
1260                                            uint32_t val_len,
1261                                            uint8_t **out_val)
1262 {
1263         if (fld_src == BNXT_ULP_FIELD_SRC_ZERO) {
1264                 if (ulp_blob_pad_push(blob, val_len) < 0) {
1265                         BNXT_TF_DBG(ERR, "too large for blob\n");
1266                         return -EINVAL;
1267                 }
1268         } else if (fld_src == BNXT_ULP_FIELD_SRC_ACT_PROP_SZ) {
1269                 if (ulp_blob_push_encap(blob, val, val_len) < 0) {
1270                         BNXT_TF_DBG(ERR, "encap blob push failed\n");
1271                         return -EINVAL;
1272                 }
1273         } else {
1274                 if (!ulp_blob_push(blob, val, val_len)) {
1275                         BNXT_TF_DBG(ERR, "push of val1 failed\n");
1276                         return -EINVAL;
1277                 }
1278         }
1279         *out_val = val;
1280         return 0;
1281 }
1282
1283 static int32_t
1284 ulp_mapper_field_opc_process(struct bnxt_ulp_mapper_parms *parms,
1285                              enum tf_dir dir,
1286                              struct bnxt_ulp_mapper_field_info *fld,
1287                              struct ulp_blob *blob,
1288                              uint8_t is_key,
1289                              const char *name)
1290 {
1291         uint16_t write_idx = blob->write_idx;
1292         uint8_t *val = NULL, *val1, *val2, *val3;
1293         uint32_t val_len = 0, val1_len = 0, val2_len = 0, val3_len = 0;
1294         uint8_t process_src1 = 0, process_src2 = 0, process_src3 = 0;
1295         uint8_t eval_src1 = 0, eval_src2 = 0, eval_src3 = 0;
1296         uint64_t val_int = 0, val1_int = 0, val2_int = 0, val3_int = 0;
1297         uint64_t value1 = 0, value2 = 0, value3 = 0;
1298         int32_t rc = 0;
1299
1300         /* prepare the field source and values */
1301         switch (fld->field_opc) {
1302         case BNXT_ULP_FIELD_OPC_SRC1:
1303                 process_src1 = 1;
1304                 break;
1305         case BNXT_ULP_FIELD_OPC_SRC1_THEN_SRC2_ELSE_SRC3:
1306                 process_src1 = 1;
1307                 break;
1308         case BNXT_ULP_FIELD_OPC_SRC1_OR_SRC2_OR_SRC3:
1309         case BNXT_ULP_FIELD_OPC_SRC1_AND_SRC2_OR_SRC3:
1310                 process_src3 = 1;
1311                 eval_src3 = 1;
1312                 process_src1 = 1;
1313                 process_src2 = 1;
1314                 eval_src1 = 1;
1315                 eval_src2 = 1;
1316                 break;
1317         case BNXT_ULP_FIELD_OPC_SRC1_PLUS_SRC2:
1318         case BNXT_ULP_FIELD_OPC_SRC1_MINUS_SRC2:
1319         case BNXT_ULP_FIELD_OPC_SRC1_PLUS_SRC2_POST:
1320         case BNXT_ULP_FIELD_OPC_SRC1_MINUS_SRC2_POST:
1321         case BNXT_ULP_FIELD_OPC_SRC1_OR_SRC2:
1322         case BNXT_ULP_FIELD_OPC_SRC1_AND_SRC2:
1323                 process_src1 = 1;
1324                 process_src2 = 1;
1325                 eval_src1 = 1;
1326                 eval_src2 = 1;
1327                 break;
1328         default:
1329                 break;
1330         }
1331
1332         /* process the src1 opcode  */
1333         if (process_src1) {
1334                 if (ulp_mapper_field_src_process(parms, fld->field_src1,
1335                                                  fld->field_opr1, dir, is_key,
1336                                                  fld->field_bit_size, &val1,
1337                                                  &val1_len, &value1)) {
1338                         BNXT_TF_DBG(ERR, "fld src1 process failed\n");
1339                         goto error;
1340                 }
1341                 if (eval_src1) {
1342                         if (ulp_mapper_field_buffer_eval(val1, val1_len,
1343                                                          &val1_int)) {
1344                                 BNXT_TF_DBG(ERR, "fld src1 eval failed\n");
1345                                 goto error;
1346                         }
1347                 }
1348         }
1349
1350         /* for "if then clause" set the correct process  */
1351         if (fld->field_opc == BNXT_ULP_FIELD_OPC_SRC1_THEN_SRC2_ELSE_SRC3) {
1352                 if (value1)
1353                         process_src2 = 1;
1354                 else
1355                         process_src3 = 1;
1356         }
1357
1358         /* process src2 opcode */
1359         if (process_src2) {
1360                 if (ulp_mapper_field_src_process(parms, fld->field_src2,
1361                                                  fld->field_opr2, dir, is_key,
1362                                                  fld->field_bit_size, &val2,
1363                                                  &val2_len, &value2)) {
1364                         BNXT_TF_DBG(ERR, "fld src2 process failed\n");
1365                         goto error;
1366                 }
1367                 if (eval_src2) {
1368                         if (ulp_mapper_field_buffer_eval(val2, val2_len,
1369                                                          &val2_int)) {
1370                                 BNXT_TF_DBG(ERR, "fld src2 eval failed\n");
1371                                 goto error;
1372                         }
1373                 }
1374         }
1375
1376         /* process src3 opcode */
1377         if (process_src3) {
1378                 if (ulp_mapper_field_src_process(parms, fld->field_src3,
1379                                                  fld->field_opr3, dir, is_key,
1380                                                  fld->field_bit_size, &val3,
1381                                                  &val3_len, &value3)) {
1382                         BNXT_TF_DBG(ERR, "fld src3 process failed\n");
1383                         goto error;
1384                 }
1385                 if (eval_src3) {
1386                         if (ulp_mapper_field_buffer_eval(val3, val3_len,
1387                                                          &val3_int)) {
1388                                 BNXT_TF_DBG(ERR, "fld src3 eval failed\n");
1389                                 goto error;
1390                         }
1391                 }
1392         }
1393
1394         val_len = fld->field_bit_size;
1395         /* process the field opcodes */
1396         switch (fld->field_opc) {
1397         case BNXT_ULP_FIELD_OPC_SRC1:
1398                 rc = ulp_mapper_field_blob_write(fld->field_src1,
1399                                                  blob, val1, val1_len, &val);
1400                 val_len = val1_len;
1401                 break;
1402         case BNXT_ULP_FIELD_OPC_SRC1_THEN_SRC2_ELSE_SRC3:
1403                 if (value1) {
1404                         rc = ulp_mapper_field_blob_write(fld->field_src2, blob,
1405                                                          val2, val2_len, &val);
1406                         val_len = val2_len;
1407                 } else {
1408                         rc = ulp_mapper_field_blob_write(fld->field_src3, blob,
1409                                                          val3, val3_len, &val);
1410                         val_len = val3_len;
1411                 }
1412                 break;
1413         case BNXT_ULP_FIELD_OPC_SRC1_PLUS_SRC2:
1414         case BNXT_ULP_FIELD_OPC_SRC1_PLUS_SRC2_POST:
1415                 val_int = val1_int + val2_int;
1416                 val_int = tfp_cpu_to_be_64(val_int);
1417                 val = ulp_blob_push_64(blob, &val_int, fld->field_bit_size);
1418                 if (!val)
1419                         rc = -EINVAL;
1420                 break;
1421         case BNXT_ULP_FIELD_OPC_SRC1_MINUS_SRC2:
1422         case BNXT_ULP_FIELD_OPC_SRC1_MINUS_SRC2_POST:
1423                 val_int = val1_int - val2_int;
1424                 val_int = tfp_cpu_to_be_64(val_int);
1425                 val = ulp_blob_push_64(blob, &val_int, fld->field_bit_size);
1426                 if (!val)
1427                         rc = -EINVAL;
1428                 break;
1429         case BNXT_ULP_FIELD_OPC_SRC1_OR_SRC2:
1430                 val_int = val1_int | val2_int;
1431                 val_int = tfp_cpu_to_be_64(val_int);
1432                 val = ulp_blob_push_64(blob, &val_int, fld->field_bit_size);
1433                 if (!val)
1434                         rc = -EINVAL;
1435                 break;
1436         case BNXT_ULP_FIELD_OPC_SRC1_OR_SRC2_OR_SRC3:
1437                 val_int = val1_int | val2_int | val3_int;
1438                 val_int = tfp_cpu_to_be_64(val_int);
1439                 val = ulp_blob_push_64(blob, &val_int, fld->field_bit_size);
1440                 if (!val)
1441                         rc = -EINVAL;
1442                 break;
1443         case BNXT_ULP_FIELD_OPC_SRC1_AND_SRC2:
1444                 val_int = val1_int & val2_int;
1445                 val_int = tfp_cpu_to_be_64(val_int);
1446                 val = ulp_blob_push_64(blob, &val_int, fld->field_bit_size);
1447                 if (!val)
1448                         rc = -EINVAL;
1449                 break;
1450         case BNXT_ULP_FIELD_OPC_SRC1_AND_SRC2_OR_SRC3:
1451                 val_int = val1_int & (val2_int | val3_int);
1452                 val_int = tfp_cpu_to_be_64(val_int);
1453                 val = ulp_blob_push_64(blob, &val_int, fld->field_bit_size);
1454                 if (!val)
1455                         rc = -EINVAL;
1456                 break;
1457         case BNXT_ULP_FIELD_OPC_SKIP:
1458                 break;
1459         default:
1460                 BNXT_TF_DBG(ERR, "Invalid fld opcode %u\n", fld->field_opc);
1461                 rc = -EINVAL;
1462                 break;
1463         }
1464
1465         if (!rc) {
1466 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG
1467 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG_MAPPER
1468                 if (fld->field_src1 != BNXT_ULP_FIELD_SRC_ZERO)
1469                         ulp_mapper_field_dump(name, fld, blob, write_idx, val,
1470                                               val_len);
1471 #endif
1472 #endif
1473                 return rc;
1474         }
1475 error:
1476         BNXT_TF_DBG(ERR, "Error in %s:%s process %u:%u\n", name,
1477                     fld->description, (val) ? write_idx : 0, val_len);
1478         return -EINVAL;
1479 }
1480
1481 /*
1482  * Result table process and fill the result blob.
1483  * data [out] - the result blob data
1484  */
1485 static int32_t
1486 ulp_mapper_tbl_result_build(struct bnxt_ulp_mapper_parms *parms,
1487                             struct bnxt_ulp_mapper_tbl_info *tbl,
1488                             struct ulp_blob *data,
1489                             const char *name)
1490 {
1491         struct bnxt_ulp_mapper_field_info *dflds;
1492         uint32_t i, num_flds = 0, encap_flds = 0;
1493         int32_t rc = 0;
1494
1495         /* Get the result field list */
1496         dflds = ulp_mapper_result_fields_get(parms, tbl, &num_flds,
1497                                              &encap_flds);
1498
1499         /* validate the result field list counts */
1500         if ((tbl->resource_func == BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE &&
1501              (!num_flds && !encap_flds)) || !dflds ||
1502             (tbl->resource_func != BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE &&
1503                 (!num_flds || encap_flds))) {
1504                 BNXT_TF_DBG(ERR, "Failed to get data fields %x:%x\n",
1505                             num_flds, encap_flds);
1506                 return -EINVAL;
1507         }
1508
1509         /* process the result fields, loop through them */
1510         for (i = 0; i < (num_flds + encap_flds); i++) {
1511                 /* set the swap index if encap swap bit is enabled */
1512                 if (parms->device_params->encap_byte_swap && encap_flds &&
1513                     i == num_flds)
1514                         ulp_blob_encap_swap_idx_set(data);
1515
1516                 /* Process the result fields */
1517                 rc = ulp_mapper_field_opc_process(parms, tbl->direction,
1518                                                   &dflds[i], data, 0, name);
1519                 if (rc) {
1520                         BNXT_TF_DBG(ERR, "data field failed\n");
1521                         return rc;
1522                 }
1523         }
1524
1525         /* if encap bit swap is enabled perform the bit swap */
1526         if (parms->device_params->encap_byte_swap && encap_flds) {
1527                 ulp_blob_perform_encap_swap(data);
1528 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG
1529 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG_MAPPER
1530                 BNXT_TF_DBG(INFO, "Dump after encap swap\n");
1531                 ulp_mapper_blob_dump(data);
1532 #endif
1533 #endif
1534         }
1535
1536         return rc;
1537 }
1538
1539 static int32_t
1540 ulp_mapper_mark_gfid_process(struct bnxt_ulp_mapper_parms *parms,
1541                              struct bnxt_ulp_mapper_tbl_info *tbl,
1542                              uint64_t flow_id)
1543 {
1544         struct ulp_flow_db_res_params fid_parms;
1545         uint32_t mark, gfid, mark_flag;
1546         enum bnxt_ulp_mark_db_opc mark_op = tbl->mark_db_opcode;
1547         int32_t rc = 0;
1548
1549         if (mark_op == BNXT_ULP_MARK_DB_OPC_NOP ||
1550             !(mark_op == BNXT_ULP_MARK_DB_OPC_PUSH_IF_MARK_ACTION &&
1551              ULP_BITMAP_ISSET(parms->act_bitmap->bits,
1552                               BNXT_ULP_ACT_BIT_MARK)))
1553                 return rc; /* no need to perform gfid process */
1554
1555         /* Get the mark id details from action property */
1556         memcpy(&mark, &parms->act_prop->act_details[BNXT_ULP_ACT_PROP_IDX_MARK],
1557                sizeof(mark));
1558         mark = tfp_be_to_cpu_32(mark);
1559
1560         TF_GET_GFID_FROM_FLOW_ID(flow_id, gfid);
1561         mark_flag  = BNXT_ULP_MARK_GLOBAL_HW_FID;
1562
1563         rc = ulp_mark_db_mark_add(parms->ulp_ctx, mark_flag,
1564                                   gfid, mark);
1565         if (rc) {
1566                 BNXT_TF_DBG(ERR, "Failed to add mark to flow\n");
1567                 return rc;
1568         }
1569         fid_parms.direction = tbl->direction;
1570         fid_parms.resource_func = BNXT_ULP_RESOURCE_FUNC_HW_FID;
1571         fid_parms.critical_resource = tbl->critical_resource;
1572         fid_parms.resource_type = mark_flag;
1573         fid_parms.resource_hndl = gfid;
1574         ulp_flow_db_shared_session_set(&fid_parms, tbl->shared_session);
1575
1576         rc = ulp_mapper_fdb_opc_process(parms, tbl, &fid_parms);
1577         if (rc)
1578                 BNXT_TF_DBG(ERR, "Fail to link res to flow rc = %d\n", rc);
1579         return rc;
1580 }
1581
1582 static int32_t
1583 ulp_mapper_mark_act_ptr_process(struct bnxt_ulp_mapper_parms *parms,
1584                                 struct bnxt_ulp_mapper_tbl_info *tbl)
1585 {
1586         struct ulp_flow_db_res_params fid_parms;
1587         uint32_t act_idx, mark, mark_flag;
1588         uint64_t val64;
1589         enum bnxt_ulp_mark_db_opc mark_op = tbl->mark_db_opcode;
1590         int32_t rc = 0;
1591
1592         if (mark_op == BNXT_ULP_MARK_DB_OPC_NOP ||
1593             !(mark_op == BNXT_ULP_MARK_DB_OPC_PUSH_IF_MARK_ACTION &&
1594              ULP_BITMAP_ISSET(parms->act_bitmap->bits,
1595                               BNXT_ULP_ACT_BIT_MARK)))
1596                 return rc; /* no need to perform mark action process */
1597
1598         /* Get the mark id details from action property */
1599         memcpy(&mark, &parms->act_prop->act_details[BNXT_ULP_ACT_PROP_IDX_MARK],
1600                sizeof(mark));
1601         mark = tfp_be_to_cpu_32(mark);
1602
1603         if (!ulp_regfile_read(parms->regfile,
1604                               BNXT_ULP_RF_IDX_MAIN_ACTION_PTR,
1605                               &val64)) {
1606                 BNXT_TF_DBG(ERR, "read action ptr main failed\n");
1607                 return -EINVAL;
1608         }
1609         act_idx = tfp_be_to_cpu_64(val64);
1610         mark_flag  = BNXT_ULP_MARK_LOCAL_HW_FID;
1611         rc = ulp_mark_db_mark_add(parms->ulp_ctx, mark_flag,
1612                                   act_idx, mark);
1613         if (rc) {
1614                 BNXT_TF_DBG(ERR, "Failed to add mark to flow\n");
1615                 return rc;
1616         }
1617         fid_parms.direction = tbl->direction;
1618         fid_parms.resource_func = BNXT_ULP_RESOURCE_FUNC_HW_FID;
1619         fid_parms.critical_resource = tbl->critical_resource;
1620         fid_parms.resource_type = mark_flag;
1621         fid_parms.resource_hndl = act_idx;
1622         ulp_flow_db_shared_session_set(&fid_parms, tbl->shared_session);
1623
1624         rc = ulp_mapper_fdb_opc_process(parms, tbl, &fid_parms);
1625         if (rc)
1626                 BNXT_TF_DBG(ERR, "Fail to link res to flow rc = %d\n", rc);
1627         return rc;
1628 }
1629
1630 static int32_t
1631 ulp_mapper_mark_vfr_idx_process(struct bnxt_ulp_mapper_parms *parms,
1632                                 struct bnxt_ulp_mapper_tbl_info *tbl)
1633 {
1634         struct ulp_flow_db_res_params fid_parms;
1635         uint32_t act_idx, mark, mark_flag;
1636         uint64_t val64;
1637         enum bnxt_ulp_mark_db_opc mark_op = tbl->mark_db_opcode;
1638         int32_t rc = 0;
1639
1640         if (mark_op == BNXT_ULP_MARK_DB_OPC_NOP ||
1641             mark_op == BNXT_ULP_MARK_DB_OPC_PUSH_IF_MARK_ACTION)
1642                 return rc; /* no need to perform mark action process */
1643
1644         /* Get the mark id details from the computed field of dev port id */
1645         mark = ULP_COMP_FLD_IDX_RD(parms, BNXT_ULP_CF_IDX_DEV_PORT_ID);
1646
1647          /* Get the main action pointer */
1648         if (!ulp_regfile_read(parms->regfile,
1649                               BNXT_ULP_RF_IDX_MAIN_ACTION_PTR,
1650                               &val64)) {
1651                 BNXT_TF_DBG(ERR, "read action ptr main failed\n");
1652                 return -EINVAL;
1653         }
1654         act_idx = tfp_be_to_cpu_64(val64);
1655
1656         /* Set the mark flag to local fid and vfr flag */
1657         mark_flag  = BNXT_ULP_MARK_LOCAL_HW_FID | BNXT_ULP_MARK_VFR_ID;
1658
1659         rc = ulp_mark_db_mark_add(parms->ulp_ctx, mark_flag,
1660                                   act_idx, mark);
1661         if (rc) {
1662                 BNXT_TF_DBG(ERR, "Failed to add mark to flow\n");
1663                 return rc;
1664         }
1665         fid_parms.direction = tbl->direction;
1666         fid_parms.resource_func = BNXT_ULP_RESOURCE_FUNC_HW_FID;
1667         fid_parms.critical_resource = tbl->critical_resource;
1668         fid_parms.resource_type = mark_flag;
1669         fid_parms.resource_hndl = act_idx;
1670         ulp_flow_db_shared_session_set(&fid_parms, tbl->shared_session);
1671
1672         rc = ulp_mapper_fdb_opc_process(parms, tbl, &fid_parms);
1673         if (rc)
1674                 BNXT_TF_DBG(ERR, "Fail to link res to flow rc = %d\n", rc);
1675         return rc;
1676 }
1677
1678 /* Tcam table scan the identifier list and allocate each identifier */
1679 static int32_t
1680 ulp_mapper_tcam_tbl_scan_ident_alloc(struct bnxt_ulp_mapper_parms *parms,
1681                                      struct bnxt_ulp_mapper_tbl_info *tbl)
1682 {
1683         struct bnxt_ulp_mapper_ident_info *idents;
1684         uint32_t num_idents;
1685         uint32_t i;
1686
1687         idents = ulp_mapper_ident_fields_get(parms, tbl, &num_idents);
1688         for (i = 0; i < num_idents; i++) {
1689                 if (ulp_mapper_ident_process(parms, tbl,
1690                                              &idents[i], NULL))
1691                         return -EINVAL;
1692         }
1693         return 0;
1694 }
1695
1696 /*
1697  * Tcam table scan the identifier list and extract the identifier from
1698  * the result blob.
1699  */
1700 static int32_t
1701 ulp_mapper_tcam_tbl_scan_ident_extract(struct bnxt_ulp_mapper_parms *parms,
1702                                        struct bnxt_ulp_mapper_tbl_info *tbl,
1703                                        struct ulp_blob *data)
1704 {
1705         struct bnxt_ulp_mapper_ident_info *idents;
1706         uint32_t num_idents = 0, i;
1707         int32_t rc = 0;
1708
1709         /*
1710          * Extract the listed identifiers from the result field,
1711          * no need to allocate them.
1712          */
1713         idents = ulp_mapper_ident_fields_get(parms, tbl, &num_idents);
1714         for (i = 0; i < num_idents; i++) {
1715                 rc = ulp_mapper_ident_extract(parms, tbl, &idents[i], data);
1716                 if (rc) {
1717                         BNXT_TF_DBG(ERR, "Error in identifier extraction\n");
1718                         return rc;
1719                 }
1720         }
1721         return rc;
1722 }
1723
1724 /* Internal function to write the tcam entry */
1725 static int32_t
1726 ulp_mapper_tcam_tbl_entry_write(struct bnxt_ulp_mapper_parms *parms,
1727                                 struct bnxt_ulp_mapper_tbl_info *tbl,
1728                                 struct ulp_blob *key,
1729                                 struct ulp_blob *mask,
1730                                 struct ulp_blob *data,
1731                                 uint16_t idx)
1732 {
1733         struct tf_set_tcam_entry_parms sparms = { 0 };
1734         struct tf *tfp;
1735         uint16_t tmplen;
1736         int32_t rc;
1737
1738         tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx, tbl->shared_session);
1739         if (!tfp) {
1740                 BNXT_TF_DBG(ERR, "Failed to get truflow pointer\n");
1741                 return -EINVAL;
1742         }
1743
1744         sparms.dir              = tbl->direction;
1745         sparms.tcam_tbl_type    = tbl->resource_type;
1746         sparms.idx              = idx;
1747         sparms.key              = ulp_blob_data_get(key, &tmplen);
1748         sparms.key_sz_in_bits   = tmplen;
1749         sparms.mask             = ulp_blob_data_get(mask, &tmplen);
1750         sparms.result           = ulp_blob_data_get(data, &tmplen);
1751         sparms.result_sz_in_bits = tmplen;
1752         if (tf_set_tcam_entry(tfp, &sparms)) {
1753                 BNXT_TF_DBG(ERR, "tcam[%s][%s][%x] write failed.\n",
1754                             tf_tcam_tbl_2_str(sparms.tcam_tbl_type),
1755                             tf_dir_2_str(sparms.dir), sparms.idx);
1756                 return -EIO;
1757         }
1758         BNXT_TF_INF("tcam[%s][%s][%x] write success.\n",
1759                     tf_tcam_tbl_2_str(sparms.tcam_tbl_type),
1760                     tf_dir_2_str(sparms.dir), sparms.idx);
1761
1762         /* Mark action */
1763         rc = ulp_mapper_mark_act_ptr_process(parms, tbl);
1764         if (rc) {
1765                 BNXT_TF_DBG(ERR, "failed mark action processing\n");
1766                 return rc;
1767         }
1768
1769 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG
1770 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG_MAPPER
1771         ulp_mapper_tcam_entry_dump("TCAM", idx, tbl, key, mask, data);
1772 #endif
1773 #endif
1774         return rc;
1775 }
1776
1777 /*
1778  * internal function to post process key/mask blobs for dynamic pad WC tcam tbl
1779  *
1780  * parms [in] The mappers parms with data related to the flow.
1781  *
1782  * key [in] The original key to be transformed
1783  *
1784  * mask [in] The original mask to be transformed
1785  *
1786  * tkey [in/out] The transformed key
1787  *
1788  * tmask [in/out] The transformed mask
1789  *
1790  * returns zero on success, non-zero on failure
1791  */
1792 static uint32_t
1793 ulp_mapper_wc_tcam_tbl_dyn_post_process(struct bnxt_ulp_device_params *dparms,
1794                                         struct ulp_blob *key,
1795                                         struct ulp_blob *mask,
1796                                         struct ulp_blob *tkey,
1797                                         struct ulp_blob *tmask)
1798 {
1799         uint16_t tlen, blen, clen, slice_width, num_slices, max_slices, offset;
1800         uint32_t cword, i, rc;
1801         int32_t pad;
1802         uint8_t *val;
1803
1804         slice_width = dparms->wc_slice_width;
1805         clen = dparms->wc_ctl_size_bits;
1806         max_slices = dparms->wc_max_slices;
1807         blen = ulp_blob_data_len_get(key);
1808
1809         /* Get the length of the key based on number of slices and width */
1810         num_slices = 1;
1811         tlen = slice_width;
1812         while (tlen < blen &&
1813                num_slices <= max_slices) {
1814                 num_slices = num_slices << 1;
1815                 tlen = tlen << 1;
1816         }
1817
1818         if (num_slices > max_slices) {
1819                 BNXT_TF_DBG(ERR, "Key size (%d) too large for WC\n", blen);
1820                 return -EINVAL;
1821         }
1822
1823         /* The key/mask may not be on a natural slice boundary, pad it */
1824         pad = tlen - blen;
1825         if (ulp_blob_pad_push(key, pad) < 0 ||
1826             ulp_blob_pad_push(mask, pad) < 0) {
1827                 BNXT_TF_DBG(ERR, "Unable to pad key/mask\n");
1828                 return -EINVAL;
1829         }
1830
1831         /* The new length accounts for the ctrl word length and num slices */
1832         tlen = tlen + clen * num_slices;
1833         if (!ulp_blob_init(tkey, tlen, key->byte_order) ||
1834             !ulp_blob_init(tmask, tlen, mask->byte_order)) {
1835                 BNXT_TF_DBG(ERR, "Unable to post process wc tcam entry\n");
1836                 return -EINVAL;
1837         }
1838
1839         /* Build the transformed key/mask */
1840         cword = dparms->wc_mode_list[num_slices - 1];
1841         cword = tfp_cpu_to_be_32(cword);
1842         offset = 0;
1843         for (i = 0; i < num_slices; i++) {
1844                 val = ulp_blob_push_32(tkey, &cword, clen);
1845                 if (!val) {
1846                         BNXT_TF_DBG(ERR, "Key ctrl word push failed\n");
1847                         return -EINVAL;
1848                 }
1849                 val = ulp_blob_push_32(tmask, &cword, clen);
1850                 if (!val) {
1851                         BNXT_TF_DBG(ERR, "Mask ctrl word push failed\n");
1852                         return -EINVAL;
1853                 }
1854                 rc = ulp_blob_append(tkey, key, offset, slice_width);
1855                 if (rc) {
1856                         BNXT_TF_DBG(ERR, "Key blob append failed\n");
1857                         return rc;
1858                 }
1859                 rc = ulp_blob_append(tmask, mask, offset, slice_width);
1860                 if (rc) {
1861                         BNXT_TF_DBG(ERR, "Mask blob append failed\n");
1862                         return rc;
1863                 }
1864                 offset += slice_width;
1865         }
1866
1867         /* The key/mask are byte reversed on every 4 byte chunk */
1868         ulp_blob_perform_byte_reverse(tkey, 4);
1869         ulp_blob_perform_byte_reverse(tmask, 4);
1870
1871         return 0;
1872 }
1873
1874 /* internal function to post process the key/mask blobs for wildcard tcam tbl */
1875 static void ulp_mapper_wc_tcam_tbl_post_process(struct ulp_blob *blob)
1876 {
1877         ulp_blob_perform_64B_word_swap(blob);
1878         ulp_blob_perform_64B_byte_swap(blob);
1879 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG
1880 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG_MAPPER
1881         BNXT_TF_DBG(INFO, "Dump after wc tcam post process\n");
1882         ulp_mapper_blob_dump(blob);
1883 #endif
1884 #endif
1885 }
1886
1887 static int32_t
1888 ulp_mapper_tcam_tbl_process(struct bnxt_ulp_mapper_parms *parms,
1889                             struct bnxt_ulp_mapper_tbl_info *tbl)
1890 {
1891         struct bnxt_ulp_mapper_key_info *kflds;
1892         struct ulp_blob okey, omask, data, update_data;
1893         struct ulp_blob tkey, tmask; /* transform key and mask */
1894         struct ulp_blob *key, *mask;
1895         uint32_t i, num_kflds;
1896         struct tf *tfp;
1897         int32_t rc, trc;
1898         struct bnxt_ulp_device_params *dparms = parms->device_params;
1899         struct tf_alloc_tcam_entry_parms aparms         = { 0 };
1900         struct tf_search_tcam_entry_parms searchparms   = { 0 };
1901         struct ulp_flow_db_res_params   fid_parms       = { 0 };
1902         struct tf_free_tcam_entry_parms free_parms      = { 0 };
1903         uint32_t hit = 0;
1904         uint16_t tmplen = 0;
1905         uint16_t idx;
1906
1907         /* Set the key and mask to the original key and mask. */
1908         key = &okey;
1909         mask = &omask;
1910
1911         /* Skip this if table opcode is NOP */
1912         if (tbl->tbl_opcode == BNXT_ULP_TCAM_TBL_OPC_NOT_USED ||
1913             tbl->tbl_opcode >= BNXT_ULP_TCAM_TBL_OPC_LAST) {
1914                 BNXT_TF_DBG(ERR, "Invalid tcam table opcode %d\n",
1915                             tbl->tbl_opcode);
1916                 return 0;
1917         }
1918
1919         tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx, tbl->shared_session);
1920         if (!tfp) {
1921                 BNXT_TF_DBG(ERR, "Failed to get truflow pointer\n");
1922                 return -EINVAL;
1923         }
1924
1925         /* If only allocation of identifier then perform and exit */
1926         if (tbl->tbl_opcode == BNXT_ULP_TCAM_TBL_OPC_ALLOC_IDENT) {
1927                 rc = ulp_mapper_tcam_tbl_scan_ident_alloc(parms, tbl);
1928                 return rc;
1929         }
1930
1931         kflds = ulp_mapper_key_fields_get(parms, tbl, &num_kflds);
1932         if (!kflds || !num_kflds) {
1933                 BNXT_TF_DBG(ERR, "Failed to get key fields\n");
1934                 return -EINVAL;
1935         }
1936
1937         if (!ulp_blob_init(key, tbl->blob_key_bit_size, tbl->byte_order) ||
1938             !ulp_blob_init(mask, tbl->blob_key_bit_size, tbl->byte_order) ||
1939             !ulp_blob_init(&data, tbl->result_bit_size, dparms->byte_order) ||
1940             !ulp_blob_init(&update_data, tbl->result_bit_size,
1941                            dparms->byte_order)) {
1942                 BNXT_TF_DBG(ERR, "blob inits failed.\n");
1943                 return -EINVAL;
1944         }
1945
1946         /* create the key/mask */
1947         /*
1948          * NOTE: The WC table will require some kind of flag to handle the
1949          * mode bits within the key/mask
1950          */
1951         for (i = 0; i < num_kflds; i++) {
1952                 /* Setup the key */
1953                 rc = ulp_mapper_field_opc_process(parms, tbl->direction,
1954                                                   &kflds[i].field_info_spec,
1955                                                   key, 1, "TCAM Key");
1956                 if (rc) {
1957                         BNXT_TF_DBG(ERR, "Key field set failed %s\n",
1958                                     kflds[i].field_info_spec.description);
1959                         return rc;
1960                 }
1961
1962                 /* Setup the mask */
1963                 rc = ulp_mapper_field_opc_process(parms, tbl->direction,
1964                                                   &kflds[i].field_info_mask,
1965                                                   mask, 0, "TCAM Mask");
1966                 if (rc) {
1967                         BNXT_TF_DBG(ERR, "Mask field set failed %s\n",
1968                                     kflds[i].field_info_mask.description);
1969                         return rc;
1970                 }
1971         }
1972
1973         /* For wild card tcam perform the post process to swap the blob */
1974         if (tbl->resource_type == TF_TCAM_TBL_TYPE_WC_TCAM ||
1975             tbl->resource_type == TF_TCAM_TBL_TYPE_WC_TCAM_HIGH ||
1976             tbl->resource_type == TF_TCAM_TBL_TYPE_WC_TCAM_LOW) {
1977                 if (dparms->dynamic_pad_en) {
1978                         /* Sets up the slices for writing to the WC TCAM */
1979                         rc = ulp_mapper_wc_tcam_tbl_dyn_post_process(dparms,
1980                                                                      key, mask,
1981                                                                      &tkey,
1982                                                                      &tmask);
1983                         if (rc) {
1984                                 BNXT_TF_DBG(ERR,
1985                                             "Failed to post proc WC entry.\n");
1986                                 return rc;
1987                         }
1988                         /* Now need to use the transform Key/Mask */
1989                         key = &tkey;
1990                         mask = &tmask;
1991                 } else {
1992                         ulp_mapper_wc_tcam_tbl_post_process(key);
1993                         ulp_mapper_wc_tcam_tbl_post_process(mask);
1994                 }
1995
1996         }
1997
1998         if (tbl->tbl_opcode == BNXT_ULP_TCAM_TBL_OPC_ALLOC_WR_REGFILE) {
1999                 /* allocate the tcam index */
2000                 aparms.dir = tbl->direction;
2001                 aparms.tcam_tbl_type = tbl->resource_type;
2002                 aparms.key = ulp_blob_data_get(key, &tmplen);
2003                 aparms.key_sz_in_bits = tmplen;
2004                 aparms.mask = ulp_blob_data_get(mask, &tmplen);
2005
2006                 /* calculate the entry priority */
2007                 rc = ulp_mapper_priority_opc_process(parms, tbl,
2008                                                      &aparms.priority);
2009                 if (rc) {
2010                         BNXT_TF_DBG(ERR, "entry priority process failed\n");
2011                         return rc;
2012                 }
2013
2014                 rc = tf_alloc_tcam_entry(tfp, &aparms);
2015                 if (rc) {
2016                         BNXT_TF_DBG(ERR, "tcam alloc failed rc=%d.\n", rc);
2017                         return rc;
2018                 }
2019                 idx = aparms.idx;
2020                 hit = aparms.hit;
2021         } else {
2022                 /*
2023                  * Searching before allocation to see if we already have an
2024                  * entry.  This allows re-use of a constrained resource.
2025                  */
2026                 searchparms.dir = tbl->direction;
2027                 searchparms.tcam_tbl_type = tbl->resource_type;
2028                 searchparms.key = ulp_blob_data_get(key, &tmplen);
2029                 searchparms.key_sz_in_bits = tbl->key_bit_size;
2030                 searchparms.mask = ulp_blob_data_get(mask, &tmplen);
2031                 searchparms.alloc = 1;
2032                 searchparms.result = ulp_blob_data_get(&data, &tmplen);
2033                 searchparms.result_sz_in_bits = tbl->result_bit_size;
2034
2035                 /* calculate the entry priority */
2036                 rc = ulp_mapper_priority_opc_process(parms, tbl,
2037                                                      &searchparms.priority);
2038                 if (rc) {
2039                         BNXT_TF_DBG(ERR, "entry priority process failed\n");
2040                         return rc;
2041                 }
2042
2043                 rc = tf_search_tcam_entry(tfp, &searchparms);
2044                 if (rc) {
2045                         BNXT_TF_DBG(ERR, "tcam search failed rc=%d\n", rc);
2046                         return rc;
2047                 }
2048
2049                 /* Successful search, check the result */
2050                 if (searchparms.search_status == REJECT) {
2051                         BNXT_TF_DBG(ERR, "tcam alloc rejected\n");
2052                         return -ENOMEM;
2053                 }
2054                 idx = searchparms.idx;
2055                 hit = searchparms.hit;
2056         }
2057
2058         /* Write the tcam index into the regfile*/
2059         if (ulp_regfile_write(parms->regfile, tbl->tbl_operand,
2060                               (uint64_t)tfp_cpu_to_be_64(idx))) {
2061                 BNXT_TF_DBG(ERR, "Regfile[%d] write failed.\n",
2062                             tbl->tbl_operand);
2063                 rc = -EINVAL;
2064                 /* Need to free the tcam idx, so goto error */
2065                 goto error;
2066         }
2067
2068         /* if it is miss then it is same as no search before alloc */
2069         if (!hit || tbl->tbl_opcode == BNXT_ULP_TCAM_TBL_OPC_ALLOC_WR_REGFILE) {
2070                 /*Scan identifier list, allocate identifier and update regfile*/
2071                 rc = ulp_mapper_tcam_tbl_scan_ident_alloc(parms, tbl);
2072                 /* Create the result blob */
2073                 if (!rc)
2074                         rc = ulp_mapper_tbl_result_build(parms, tbl, &data,
2075                                                          "TCAM Result");
2076                 /* write the tcam entry */
2077                 if (!rc)
2078                         rc = ulp_mapper_tcam_tbl_entry_write(parms, tbl, key,
2079                                                              mask, &data, idx);
2080         } else {
2081                 /*Scan identifier list, extract identifier and update regfile*/
2082                 rc = ulp_mapper_tcam_tbl_scan_ident_extract(parms, tbl, &data);
2083         }
2084         if (rc)
2085                 goto error;
2086
2087         /* Add the tcam index to the flow database */
2088         fid_parms.direction = tbl->direction;
2089         fid_parms.resource_func = tbl->resource_func;
2090         fid_parms.resource_type = tbl->resource_type;
2091         fid_parms.critical_resource = tbl->critical_resource;
2092         fid_parms.resource_hndl = idx;
2093         ulp_flow_db_shared_session_set(&fid_parms, tbl->shared_session);
2094
2095         rc = ulp_mapper_fdb_opc_process(parms, tbl, &fid_parms);
2096         if (rc) {
2097                 BNXT_TF_DBG(ERR, "Failed to link resource to flow rc = %d\n",
2098                             rc);
2099                 /* Need to free the identifier, so goto error */
2100                 goto error;
2101         }
2102
2103         return 0;
2104 error:
2105         free_parms.dir                  = tbl->direction;
2106         free_parms.tcam_tbl_type        = tbl->resource_type;
2107         free_parms.idx                  = idx;
2108         trc = tf_free_tcam_entry(tfp, &free_parms);
2109         if (trc)
2110                 BNXT_TF_DBG(ERR, "Failed to free tcam[%d][%d][%d] on failure\n",
2111                             tbl->resource_type, tbl->direction, idx);
2112         return rc;
2113 }
2114
2115 static int32_t
2116 ulp_mapper_em_tbl_process(struct bnxt_ulp_mapper_parms *parms,
2117                           struct bnxt_ulp_mapper_tbl_info *tbl)
2118 {
2119         struct bnxt_ulp_mapper_key_info *kflds;
2120         struct ulp_blob key, data;
2121         uint32_t i, num_kflds;
2122         uint16_t tmplen;
2123         struct tf *tfp;
2124         struct ulp_flow_db_res_params   fid_parms = { 0 };
2125         struct tf_insert_em_entry_parms iparms = { 0 };
2126         struct tf_delete_em_entry_parms free_parms = { 0 };
2127         enum bnxt_ulp_flow_mem_type mtype;
2128         struct bnxt_ulp_device_params *dparms = parms->device_params;
2129         int32_t trc;
2130         int32_t rc = 0;
2131         int32_t pad = 0;
2132
2133         tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx, tbl->shared_session);
2134         rc = bnxt_ulp_cntxt_mem_type_get(parms->ulp_ctx, &mtype);
2135         if (rc) {
2136                 BNXT_TF_DBG(ERR, "Failed to get the mem type for EM\n");
2137                 return -EINVAL;
2138         }
2139
2140         kflds = ulp_mapper_key_fields_get(parms, tbl, &num_kflds);
2141         if (!kflds || !num_kflds) {
2142                 BNXT_TF_DBG(ERR, "Failed to get key fields\n");
2143                 return -EINVAL;
2144         }
2145
2146         /* Initialize the key/result blobs */
2147         if (!ulp_blob_init(&key, tbl->blob_key_bit_size,
2148                            tbl->byte_order) ||
2149             !ulp_blob_init(&data, tbl->result_bit_size,
2150                            tbl->byte_order)) {
2151                 BNXT_TF_DBG(ERR, "blob inits failed.\n");
2152                 return -EINVAL;
2153         }
2154
2155         /* create the key */
2156         for (i = 0; i < num_kflds; i++) {
2157                 /* Setup the key */
2158                 rc = ulp_mapper_field_opc_process(parms, tbl->direction,
2159                                                   &kflds[i].field_info_spec,
2160                                                   &key, 1, "EM Key");
2161                 if (rc) {
2162                         BNXT_TF_DBG(ERR, "Key field set failed.\n");
2163                         return rc;
2164                 }
2165         }
2166
2167         /* if dynamic padding is enabled then add padding to result data */
2168         if (dparms->dynamic_pad_en) {
2169                 /* add padding to make sure key is at byte boundary */
2170                 ulp_blob_pad_align(&key, ULP_BUFFER_ALIGN_8_BITS);
2171
2172                 /* add the pad */
2173                 pad = dparms->em_blk_align_bits - dparms->em_blk_size_bits;
2174                 if (pad < 0) {
2175                         BNXT_TF_DBG(ERR, "Invalid em blk size and align\n");
2176                         return -EINVAL;
2177                 }
2178                 ulp_blob_pad_push(&data, (uint32_t)pad);
2179         }
2180
2181         /* Create the result data blob */
2182         rc = ulp_mapper_tbl_result_build(parms, tbl, &data, "EM Result");
2183         if (rc) {
2184                 BNXT_TF_DBG(ERR, "Failed to build the result blob\n");
2185                 return rc;
2186         }
2187 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG
2188 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG_MAPPER
2189         ulp_mapper_result_dump("EM Result", tbl, &data);
2190 #endif
2191 #endif
2192         if (dparms->dynamic_pad_en) {
2193                 uint32_t abits = dparms->em_blk_align_bits;
2194
2195                 /* when dynamic padding is enabled merge result + key */
2196                 rc = ulp_blob_block_merge(&data, &key, abits, pad);
2197                 if (rc) {
2198                         BNXT_TF_DBG(ERR, "Failed to merge the result blob\n");
2199                         return rc;
2200                 }
2201
2202                 /* add padding to make sure merged result is at slice boundary*/
2203                 ulp_blob_pad_align(&data, abits);
2204
2205                 ulp_blob_perform_byte_reverse(&data, ULP_BITS_2_BYTE(abits));
2206 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG
2207 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG_MAPPER
2208         ulp_mapper_result_dump("EM Merged Result", tbl, &data);
2209 #endif
2210 #endif
2211         }
2212
2213         /* do the transpose for the internal EM keys */
2214         if (tbl->resource_type == TF_MEM_INTERNAL) {
2215                 if (dparms->em_key_align_bytes) {
2216                         int32_t b = ULP_BYTE_2_BITS(dparms->em_key_align_bytes);
2217
2218                         tmplen = ulp_blob_data_len_get(&key);
2219                         ulp_blob_pad_push(&key, b - tmplen);
2220                 }
2221                 tmplen = ulp_blob_data_len_get(&key);
2222                 ulp_blob_perform_byte_reverse(&key, ULP_BITS_2_BYTE(tmplen));
2223 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG
2224 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG_MAPPER
2225         ulp_mapper_result_dump("EM Key Transpose", tbl, &key);
2226 #endif
2227 #endif
2228         }
2229
2230         rc = bnxt_ulp_cntxt_tbl_scope_id_get(parms->ulp_ctx,
2231                                              &iparms.tbl_scope_id);
2232         if (rc) {
2233                 BNXT_TF_DBG(ERR, "Failed to get table scope rc=%d\n", rc);
2234                 return rc;
2235         }
2236
2237         /*
2238          * NOTE: the actual blob size will differ from the size in the tbl
2239          * entry due to the padding.
2240          */
2241         iparms.dup_check                = 0;
2242         iparms.dir                      = tbl->direction;
2243         iparms.mem                      = tbl->resource_type;
2244         iparms.key                      = ulp_blob_data_get(&key, &tmplen);
2245         iparms.key_sz_in_bits           = tbl->key_bit_size;
2246         iparms.em_record                = ulp_blob_data_get(&data, &tmplen);
2247         if (tbl->result_bit_size)
2248                 iparms.em_record_sz_in_bits     = tbl->result_bit_size;
2249         else
2250                 iparms.em_record_sz_in_bits     = tmplen;
2251
2252         rc = tf_insert_em_entry(tfp, &iparms);
2253         if (rc) {
2254                 BNXT_TF_DBG(ERR, "Failed to insert em entry rc=%d.\n", rc);
2255                 return rc;
2256         }
2257
2258 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG
2259 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG_MAPPER
2260         ulp_mapper_em_dump("EM", &key, &data, &iparms);
2261         /* tf_dump_tables(tfp, iparms.tbl_scope_id); */
2262 #endif
2263 #endif
2264         /* Mark action process */
2265         if (mtype == BNXT_ULP_FLOW_MEM_TYPE_EXT &&
2266             tbl->resource_type == TF_MEM_EXTERNAL)
2267                 rc = ulp_mapper_mark_gfid_process(parms, tbl, iparms.flow_id);
2268         else if (mtype == BNXT_ULP_FLOW_MEM_TYPE_INT &&
2269                  tbl->resource_type == TF_MEM_INTERNAL)
2270                 rc = ulp_mapper_mark_act_ptr_process(parms, tbl);
2271         if (rc) {
2272                 BNXT_TF_DBG(ERR, "Failed to add mark to flow\n");
2273                 goto error;
2274         }
2275
2276         /* Link the EM resource to the flow in the flow db */
2277         memset(&fid_parms, 0, sizeof(fid_parms));
2278         fid_parms.direction             = tbl->direction;
2279         fid_parms.resource_func         = tbl->resource_func;
2280         fid_parms.resource_type         = tbl->resource_type;
2281         fid_parms.critical_resource     = tbl->critical_resource;
2282         fid_parms.resource_hndl         = iparms.flow_handle;
2283
2284         rc = ulp_mapper_fdb_opc_process(parms, tbl, &fid_parms);
2285         if (rc) {
2286                 BNXT_TF_DBG(ERR, "Fail to link res to flow rc = %d\n",
2287                             rc);
2288                 /* Need to free the identifier, so goto error */
2289                 goto error;
2290         }
2291
2292         return 0;
2293 error:
2294         free_parms.dir          = iparms.dir;
2295         free_parms.mem          = iparms.mem;
2296         free_parms.tbl_scope_id = iparms.tbl_scope_id;
2297         free_parms.flow_handle  = iparms.flow_handle;
2298
2299         trc = tf_delete_em_entry(tfp, &free_parms);
2300         if (trc)
2301                 BNXT_TF_DBG(ERR, "Failed to delete EM entry on failed add\n");
2302
2303         return rc;
2304 }
2305
2306 static int32_t
2307 ulp_mapper_index_tbl_process(struct bnxt_ulp_mapper_parms *parms,
2308                              struct bnxt_ulp_mapper_tbl_info *tbl)
2309 {
2310         struct ulp_flow_db_res_params fid_parms;
2311         struct ulp_blob data;
2312         uint64_t regval = 0;
2313         uint16_t tmplen;
2314         uint32_t index;
2315         int32_t rc = 0, trc = 0;
2316         struct tf_alloc_tbl_entry_parms aparms = { 0 };
2317         struct tf_set_tbl_entry_parms sparms = { 0 };
2318         struct tf_get_tbl_entry_parms gparms = { 0 };
2319         struct tf_free_tbl_entry_parms free_parms = { 0 };
2320         uint32_t tbl_scope_id;
2321         struct tf *tfp;
2322         struct bnxt_ulp_glb_resource_info glb_res = { 0 };
2323         uint16_t bit_size;
2324         bool alloc = false;
2325         bool write = false;
2326         bool global = false;
2327         uint64_t act_rec_size;
2328         bool shared = false;
2329
2330         tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx, tbl->shared_session);
2331         /* use the max size if encap is enabled */
2332         if (tbl->encap_num_fields)
2333                 bit_size = BNXT_ULP_FLMP_BLOB_SIZE_IN_BITS;
2334         else
2335                 bit_size = tbl->result_bit_size;
2336
2337         /* Initialize the blob data */
2338         if (!ulp_blob_init(&data, bit_size,
2339                            parms->device_params->byte_order)) {
2340                 BNXT_TF_DBG(ERR, "Failed to initialize index table blob\n");
2341                 return -EINVAL;
2342         }
2343
2344         /* Get the scope id first */
2345         rc = bnxt_ulp_cntxt_tbl_scope_id_get(parms->ulp_ctx, &tbl_scope_id);
2346         if (rc) {
2347                 BNXT_TF_DBG(ERR, "Failed to get table scope rc=%d\n", rc);
2348                 return rc;
2349         }
2350
2351         switch (tbl->tbl_opcode) {
2352         case BNXT_ULP_INDEX_TBL_OPC_ALLOC_REGFILE:
2353                 alloc = true;
2354                 break;
2355         case BNXT_ULP_INDEX_TBL_OPC_ALLOC_WR_REGFILE:
2356                 /*
2357                  * Build the entry, alloc an index, write the table, and store
2358                  * the data in the regfile.
2359                  */
2360                 alloc = true;
2361                 write = true;
2362                 break;
2363         case BNXT_ULP_INDEX_TBL_OPC_WR_REGFILE:
2364                 /*
2365                  * get the index to write to from the regfile and then write
2366                  * the table entry.
2367                  */
2368                 if (!ulp_regfile_read(parms->regfile,
2369                                       tbl->tbl_operand,
2370                                       &regval)) {
2371                         BNXT_TF_DBG(ERR,
2372                                     "Failed to get tbl idx from regfile[%d].\n",
2373                                     tbl->tbl_operand);
2374                         return -EINVAL;
2375                 }
2376                 index = tfp_be_to_cpu_64(regval);
2377                 /* For external, we need to reverse shift */
2378                 if (tbl->resource_type == TF_TBL_TYPE_EXT)
2379                         index = TF_ACT_REC_PTR_2_OFFSET(index);
2380
2381                 write = true;
2382                 break;
2383         case BNXT_ULP_INDEX_TBL_OPC_ALLOC_WR_GLB_REGFILE:
2384                 /*
2385                  * Build the entry, alloc an index, write the table, and store
2386                  * the data in the global regfile.
2387                  */
2388                 alloc = true;
2389                 global = true;
2390                 write = true;
2391                 glb_res.direction = tbl->direction;
2392                 glb_res.resource_func = tbl->resource_func;
2393                 glb_res.resource_type = tbl->resource_type;
2394                 glb_res.glb_regfile_index = tbl->tbl_operand;
2395                 break;
2396         case BNXT_ULP_INDEX_TBL_OPC_WR_GLB_REGFILE:
2397                 if (tbl->fdb_opcode != BNXT_ULP_FDB_OPC_NOP) {
2398                         BNXT_TF_DBG(ERR, "Template error, wrong fdb opcode\n");
2399                         return -EINVAL;
2400                 }
2401                 /*
2402                  * get the index to write to from the global regfile and then
2403                  * write the table.
2404                  */
2405                 if (ulp_mapper_glb_resource_read(parms->mapper_data,
2406                                                  tbl->direction,
2407                                                  tbl->tbl_operand,
2408                                                  &regval, &shared)) {
2409                         BNXT_TF_DBG(ERR,
2410                                     "Failed to get tbl idx from Glb RF[%d].\n",
2411                                     tbl->tbl_operand);
2412                         return -EINVAL;
2413                 }
2414                 index = tfp_be_to_cpu_64(regval);
2415                 /* For external, we need to reverse shift */
2416                 if (tbl->resource_type == TF_TBL_TYPE_EXT)
2417                         index = TF_ACT_REC_PTR_2_OFFSET(index);
2418                 write = true;
2419                 break;
2420         case BNXT_ULP_INDEX_TBL_OPC_RD_REGFILE:
2421                 /*
2422                  * The read is different from the rest and can be handled here
2423                  * instead of trying to use common code.  Simply read the table
2424                  * with the index from the regfile, scan and store the
2425                  * identifiers, and return.
2426                  */
2427                 if (tbl->resource_type == TF_TBL_TYPE_EXT) {
2428                         /* Not currently supporting with EXT */
2429                         BNXT_TF_DBG(ERR,
2430                                     "Ext Table Read Opcode not supported.\n");
2431                         return -EINVAL;
2432                 }
2433                 if (!ulp_regfile_read(parms->regfile,
2434                                       tbl->tbl_operand, &regval)) {
2435                         BNXT_TF_DBG(ERR,
2436                                     "Failed to get tbl idx from regfile[%d]\n",
2437                                     tbl->tbl_operand);
2438                         return -EINVAL;
2439                 }
2440                 index = tfp_be_to_cpu_64(regval);
2441                 gparms.dir = tbl->direction;
2442                 gparms.type = tbl->resource_type;
2443                 gparms.data = ulp_blob_data_get(&data, &tmplen);
2444                 gparms.data_sz_in_bytes = ULP_BITS_2_BYTE(tbl->result_bit_size);
2445                 gparms.idx = index;
2446                 rc = tf_get_tbl_entry(tfp, &gparms);
2447                 if (rc) {
2448                         BNXT_TF_DBG(ERR, "Failed to read the tbl entry %d:%d\n",
2449                                     tbl->resource_type, index);
2450                         return rc;
2451                 }
2452                 /*
2453                  * Scan the fields in the entry and push them into the regfile.
2454                  */
2455                 rc = ulp_mapper_tbl_ident_scan_ext(parms, tbl,
2456                                                    gparms.data,
2457                                                    gparms.data_sz_in_bytes,
2458                                                    data.byte_order);
2459                 if (rc) {
2460                         BNXT_TF_DBG(ERR,
2461                                     "Failed to get flds on tbl read rc=%d\n",
2462                                     rc);
2463                         return rc;
2464                 }
2465                 return 0;
2466         default:
2467                 BNXT_TF_DBG(ERR, "Invalid index table opcode %d\n",
2468                             tbl->tbl_opcode);
2469                 return -EINVAL;
2470         }
2471
2472         if (write) {
2473                 /* Get the result fields list */
2474                 rc = ulp_mapper_tbl_result_build(parms,
2475                                                  tbl,
2476                                                  &data,
2477                                                  "Indexed Result");
2478                 if (rc) {
2479                         BNXT_TF_DBG(ERR, "Failed to build the result blob\n");
2480                         return rc;
2481                 }
2482         }
2483
2484         if (alloc) {
2485                 aparms.dir              = tbl->direction;
2486                 aparms.type             = tbl->resource_type;
2487                 aparms.tbl_scope_id     = tbl_scope_id;
2488
2489                 /* All failures after the alloc succeeds require a free */
2490                 rc = tf_alloc_tbl_entry(tfp, &aparms);
2491                 if (rc) {
2492                         BNXT_TF_DBG(ERR, "Alloc table[%s][%s] failed rc=%d\n",
2493                                     tf_tbl_type_2_str(tbl->resource_type),
2494                                     tf_dir_2_str(tbl->direction), rc);
2495                         return rc;
2496                 }
2497                 index = aparms.idx;
2498
2499                 /*
2500                  * Store the index in the regfile since we either allocated it
2501                  * or it was a hit.
2502                  *
2503                  * Calculate the idx for the result record, for external EM the
2504                  * offset needs to be shifted accordingly.
2505                  * If external non-inline table types are used then need to
2506                  * revisit this logic.
2507                  */
2508                 if (tbl->resource_type == TF_TBL_TYPE_EXT)
2509                         regval = TF_ACT_REC_OFFSET_2_PTR(index);
2510                 else
2511                         regval = index;
2512                 regval = tfp_cpu_to_be_64(regval);
2513
2514                 if (global) {
2515                         /*
2516                          * Shared resources are never allocated through this
2517                          * method, so the shared flag is always false.
2518                          */
2519                         rc = ulp_mapper_glb_resource_write(parms->mapper_data,
2520                                                            &glb_res, regval,
2521                                                            false);
2522                 } else {
2523                         rc = ulp_regfile_write(parms->regfile,
2524                                                tbl->tbl_operand, regval);
2525                 }
2526                 if (rc) {
2527                         BNXT_TF_DBG(ERR,
2528                                     "Failed to write %s regfile[%d] rc=%d\n",
2529                                     (global) ? "global" : "reg",
2530                                     tbl->tbl_operand, rc);
2531                         goto error;
2532                 }
2533         }
2534
2535         if (write) {
2536                 sparms.dir = tbl->direction;
2537                 sparms.type = tbl->resource_type;
2538                 sparms.data = ulp_blob_data_get(&data, &tmplen);
2539                 sparms.data_sz_in_bytes = ULP_BITS_2_BYTE(tmplen);
2540                 sparms.idx = index;
2541                 sparms.tbl_scope_id = tbl_scope_id;
2542                 if (shared)
2543                         tfp = bnxt_ulp_cntxt_shared_tfp_get(parms->ulp_ctx);
2544                 rc = tf_set_tbl_entry(tfp, &sparms);
2545                 if (rc) {
2546                         BNXT_TF_DBG(ERR,
2547                                     "Index table[%s][%s][%x] write fail rc=%d\n",
2548                                     tf_tbl_type_2_str(sparms.type),
2549                                     tf_dir_2_str(sparms.dir),
2550                                     sparms.idx, rc);
2551                         goto error;
2552                 }
2553                 BNXT_TF_INF("Index table[%s][%s][%x] write successful.\n",
2554                             tf_tbl_type_2_str(sparms.type),
2555                             tf_dir_2_str(sparms.dir), sparms.idx);
2556
2557                 /* Calculate action record size */
2558                 if (tbl->resource_type == TF_TBL_TYPE_EXT) {
2559                         act_rec_size = (ULP_BITS_2_BYTE_NR(tmplen) + 15) / 16;
2560                         act_rec_size--;
2561                         if (ulp_regfile_write(parms->regfile,
2562                                               BNXT_ULP_RF_IDX_ACTION_REC_SIZE,
2563                                               tfp_cpu_to_be_64(act_rec_size)))
2564                                 BNXT_TF_DBG(ERR,
2565                                             "Failed write the act rec size\n");
2566                 }
2567         }
2568
2569         /* Link the resource to the flow in the flow db */
2570         memset(&fid_parms, 0, sizeof(fid_parms));
2571         fid_parms.direction     = tbl->direction;
2572         fid_parms.resource_func = tbl->resource_func;
2573         fid_parms.resource_type = tbl->resource_type;
2574         fid_parms.resource_sub_type = tbl->resource_sub_type;
2575         fid_parms.resource_hndl = index;
2576         fid_parms.critical_resource = tbl->critical_resource;
2577         ulp_flow_db_shared_session_set(&fid_parms, tbl->shared_session);
2578
2579         rc = ulp_mapper_fdb_opc_process(parms, tbl, &fid_parms);
2580         if (rc) {
2581                 BNXT_TF_DBG(ERR, "Failed to link resource to flow rc = %d\n",
2582                             rc);
2583                 goto error;
2584         }
2585
2586         /* Perform the VF rep action */
2587         rc = ulp_mapper_mark_vfr_idx_process(parms, tbl);
2588         if (rc) {
2589                 BNXT_TF_DBG(ERR, "Failed to add vfr mark rc = %d\n", rc);
2590                 goto error;
2591         }
2592         return rc;
2593 error:
2594         /* Shared resources are not freed */
2595         if (shared)
2596                 return rc;
2597         /*
2598          * Free the allocated resource since we failed to either
2599          * write to the entry or link the flow
2600          */
2601         free_parms.dir  = tbl->direction;
2602         free_parms.type = tbl->resource_type;
2603         free_parms.idx  = index;
2604         free_parms.tbl_scope_id = tbl_scope_id;
2605
2606         trc = tf_free_tbl_entry(tfp, &free_parms);
2607         if (trc)
2608                 BNXT_TF_DBG(ERR, "Failed to free tbl entry on failure\n");
2609
2610         return rc;
2611 }
2612
2613 static int32_t
2614 ulp_mapper_if_tbl_process(struct bnxt_ulp_mapper_parms *parms,
2615                           struct bnxt_ulp_mapper_tbl_info *tbl)
2616 {
2617         struct ulp_blob data, res_blob;
2618         uint64_t idx;
2619         uint16_t tmplen;
2620         int32_t rc = 0;
2621         struct tf_set_if_tbl_entry_parms iftbl_params = { 0 };
2622         struct tf_get_if_tbl_entry_parms get_parms = { 0 };
2623         struct tf *tfp;
2624         enum bnxt_ulp_if_tbl_opc if_opc = tbl->tbl_opcode;
2625         uint32_t res_size;
2626
2627         tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx, tbl->shared_session);
2628         /* Initialize the blob data */
2629         if (!ulp_blob_init(&data, tbl->result_bit_size,
2630                            parms->device_params->byte_order)) {
2631                 BNXT_TF_DBG(ERR, "Failed initial index table blob\n");
2632                 return -EINVAL;
2633         }
2634
2635         /* create the result blob */
2636         rc = ulp_mapper_tbl_result_build(parms, tbl, &data, "IFtable Result");
2637         if (rc) {
2638                 BNXT_TF_DBG(ERR, "Failed to build the result blob\n");
2639                 return rc;
2640         }
2641
2642         /* Get the index details */
2643         switch (if_opc) {
2644         case BNXT_ULP_IF_TBL_OPC_WR_COMP_FIELD:
2645                 idx = ULP_COMP_FLD_IDX_RD(parms, tbl->tbl_operand);
2646                 break;
2647         case BNXT_ULP_IF_TBL_OPC_WR_REGFILE:
2648                 if (!ulp_regfile_read(parms->regfile, tbl->tbl_operand, &idx)) {
2649                         BNXT_TF_DBG(ERR, "regfile[%d] read oob\n",
2650                                     tbl->tbl_operand);
2651                         return -EINVAL;
2652                 }
2653                 idx = tfp_be_to_cpu_64(idx);
2654                 break;
2655         case BNXT_ULP_IF_TBL_OPC_WR_CONST:
2656                 idx = tbl->tbl_operand;
2657                 break;
2658         case BNXT_ULP_IF_TBL_OPC_RD_COMP_FIELD:
2659                 /* Initialize the result blob */
2660                 if (!ulp_blob_init(&res_blob, tbl->result_bit_size,
2661                                    parms->device_params->byte_order)) {
2662                         BNXT_TF_DBG(ERR, "Failed initial result blob\n");
2663                         return -EINVAL;
2664                 }
2665
2666                 /* read the interface table */
2667                 idx = ULP_COMP_FLD_IDX_RD(parms, tbl->tbl_operand);
2668                 res_size = ULP_BITS_2_BYTE(tbl->result_bit_size);
2669                 get_parms.dir = tbl->direction;
2670                 get_parms.type = tbl->resource_type;
2671                 get_parms.idx = idx;
2672                 get_parms.data = ulp_blob_data_get(&res_blob, &tmplen);
2673                 get_parms.data_sz_in_bytes = res_size;
2674
2675                 rc = tf_get_if_tbl_entry(tfp, &get_parms);
2676                 if (rc) {
2677                         BNXT_TF_DBG(ERR, "Get table[%d][%s][%x] failed rc=%d\n",
2678                                     get_parms.type,
2679                                     tf_dir_2_str(get_parms.dir),
2680                                     get_parms.idx, rc);
2681                         return rc;
2682                 }
2683                 rc = ulp_mapper_tbl_ident_scan_ext(parms, tbl,
2684                                                    res_blob.data,
2685                                                    res_size,
2686                                                    res_blob.byte_order);
2687                 if (rc)
2688                         BNXT_TF_DBG(ERR, "Scan and extract failed rc=%d\n", rc);
2689                 return rc;
2690         case BNXT_ULP_IF_TBL_OPC_NOT_USED:
2691                 return rc; /* skip it */
2692         default:
2693                 BNXT_TF_DBG(ERR, "Invalid tbl index opcode\n");
2694                 return -EINVAL;
2695         }
2696
2697         /* Perform the tf table set by filling the set params */
2698         iftbl_params.dir = tbl->direction;
2699         iftbl_params.type = tbl->resource_type;
2700         iftbl_params.data = ulp_blob_data_get(&data, &tmplen);
2701         iftbl_params.data_sz_in_bytes = ULP_BITS_2_BYTE(tmplen);
2702         iftbl_params.idx = idx;
2703
2704         rc = tf_set_if_tbl_entry(tfp, &iftbl_params);
2705         if (rc) {
2706                 BNXT_TF_DBG(ERR, "Set table[%d][%s][%x] failed rc=%d\n",
2707                             iftbl_params.type,/* TBD: add tf_if_tbl_2_str */
2708                             tf_dir_2_str(iftbl_params.dir),
2709                             iftbl_params.idx, rc);
2710                 return rc;
2711         }
2712         BNXT_TF_INF("Set table[%s][%s][%x] success.\n",
2713                     tf_if_tbl_2_str(iftbl_params.type),
2714                     tf_dir_2_str(iftbl_params.dir),
2715                     iftbl_params.idx);
2716
2717         /*
2718          * TBD: Need to look at the need to store idx in flow db for restore
2719          * the table to its original state on deletion of this entry.
2720          */
2721         return rc;
2722 }
2723
2724 static int32_t
2725 ulp_mapper_gen_tbl_process(struct bnxt_ulp_mapper_parms *parms,
2726                            struct bnxt_ulp_mapper_tbl_info *tbl)
2727 {
2728         struct ulp_mapper_gen_tbl_list *gen_tbl_list;
2729         struct bnxt_ulp_mapper_key_info *kflds;
2730         struct ulp_flow_db_res_params fid_parms;
2731         struct ulp_mapper_gen_tbl_entry gen_tbl_ent, *g;
2732         struct ulp_gen_hash_entry_params hash_entry;
2733         uint16_t tmplen = 0;
2734         struct ulp_blob key, data;
2735         uint8_t *cache_key;
2736         int32_t tbl_idx;
2737         uint32_t i, num_kflds = 0, key_index = 0;
2738         uint32_t gen_tbl_miss = 1, fdb_write = 0;
2739         uint8_t *byte_data;
2740         int32_t rc = 0;
2741
2742         /* Get the key fields list and build the key. */
2743         kflds = ulp_mapper_key_fields_get(parms, tbl, &num_kflds);
2744         if (!kflds || !num_kflds) {
2745                 BNXT_TF_DBG(ERR, "Failed to get key fields\n");
2746                 return -EINVAL;
2747         }
2748
2749         if (!ulp_blob_init(&key, tbl->key_bit_size,
2750                            parms->device_params->byte_order)) {
2751                 BNXT_TF_DBG(ERR, "Failed to alloc blob\n");
2752                 return -EINVAL;
2753         }
2754         for (i = 0; i < num_kflds; i++) {
2755                 /* Setup the key */
2756                 rc = ulp_mapper_field_opc_process(parms, tbl->direction,
2757                                                   &kflds[i].field_info_spec,
2758                                                   &key, 1, "Gen Tbl Key");
2759                 if (rc) {
2760                         BNXT_TF_DBG(ERR,
2761                                     "Failed to create key for Gen tbl rc=%d\n",
2762                                     rc);
2763                         return -EINVAL;
2764                 }
2765         }
2766
2767         /* Calculate the table index for the generic table*/
2768         tbl_idx = ulp_mapper_gen_tbl_idx_calculate(tbl->resource_sub_type,
2769                                                    tbl->direction);
2770         if (tbl_idx < 0) {
2771                 BNXT_TF_DBG(ERR, "Invalid table index %x:%x\n",
2772                             tbl->resource_sub_type, tbl->direction);
2773                 return -EINVAL;
2774         }
2775
2776         /* The_key is a byte array convert it to a search index */
2777         cache_key = ulp_blob_data_get(&key, &tmplen);
2778 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG
2779 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG_MAPPER
2780         BNXT_TF_DBG(DEBUG, "The gen_tbl[%u] key\n", tbl_idx);
2781         ulp_mapper_blob_dump(&key);
2782 #endif
2783 #endif
2784         /* get the generic table  */
2785         gen_tbl_list = &parms->mapper_data->gen_tbl_list[tbl_idx];
2786
2787         /* Check if generic hash table */
2788         if (gen_tbl_list->hash_tbl) {
2789                 if (tbl->gen_tbl_lkup_type !=
2790                     BNXT_ULP_GENERIC_TBL_LKUP_TYPE_HASH) {
2791                         BNXT_TF_DBG(ERR, "%s: Invalid template lkup type\n",
2792                                     gen_tbl_list->gen_tbl_name);
2793                         return -EINVAL;
2794                 }
2795                 hash_entry.key_data = cache_key;
2796                 hash_entry.key_length = ULP_BITS_2_BYTE(tmplen);
2797                 rc = ulp_gen_hash_tbl_list_key_search(gen_tbl_list->hash_tbl,
2798                                                       &hash_entry);
2799                 if (rc) {
2800                         BNXT_TF_DBG(ERR, "%s: hash tbl search failed\n",
2801                                     gen_tbl_list->gen_tbl_name);
2802                         return rc;
2803                 }
2804                 if (hash_entry.search_flag == ULP_GEN_HASH_SEARCH_FOUND) {
2805                         key_index = hash_entry.key_idx;
2806                         /* Get the generic table entry */
2807                         if (ulp_mapper_gen_tbl_entry_get(gen_tbl_list,
2808                                                          key_index,
2809                                                          &gen_tbl_ent))
2810                                 return -EINVAL;
2811                         /* store the hash index in the fdb */
2812                         key_index = hash_entry.hash_index;
2813                 }
2814         } else {
2815                 /* convert key to index directly */
2816                 if (ULP_BITS_2_BYTE(tmplen) > (int32_t)sizeof(key_index)) {
2817                         BNXT_TF_DBG(ERR, "%s: keysize is bigger then 4 bytes\n",
2818                                     gen_tbl_list->gen_tbl_name);
2819                         return -EINVAL;
2820                 }
2821                 memcpy(&key_index, cache_key, ULP_BITS_2_BYTE(tmplen));
2822                 /* Get the generic table entry */
2823                 if (ulp_mapper_gen_tbl_entry_get(gen_tbl_list, key_index,
2824                                                  &gen_tbl_ent))
2825                         return -EINVAL;
2826         }
2827         switch (tbl->tbl_opcode) {
2828         case BNXT_ULP_GENERIC_TBL_OPC_READ:
2829                 if (gen_tbl_list->hash_tbl) {
2830                         if (hash_entry.search_flag != ULP_GEN_HASH_SEARCH_FOUND)
2831                                 break; /* nothing to be done , no entry */
2832                 }
2833
2834                 /* check the reference count */
2835                 if (ULP_GEN_TBL_REF_CNT(&gen_tbl_ent)) {
2836                         g = &gen_tbl_ent;
2837                         /* Scan ident list and create the result blob*/
2838                         rc = ulp_mapper_tbl_ident_scan_ext(parms, tbl,
2839                                                            g->byte_data,
2840                                                            g->byte_data_size,
2841                                                            g->byte_order);
2842                         if (rc) {
2843                                 BNXT_TF_DBG(ERR,
2844                                             "Failed to scan ident list\n");
2845                                 return -EINVAL;
2846                         }
2847                         if (tbl->fdb_opcode != BNXT_ULP_FDB_OPC_NOP) {
2848                                 /* increment the reference count */
2849                                 ULP_GEN_TBL_REF_CNT_INC(&gen_tbl_ent);
2850                         }
2851
2852                         /* it is a hit */
2853                         gen_tbl_miss = 0;
2854                         fdb_write = 1;
2855                 }
2856                 break;
2857         case BNXT_ULP_GENERIC_TBL_OPC_WRITE:
2858                 if (gen_tbl_list->hash_tbl) {
2859                         rc = ulp_mapper_gen_tbl_hash_entry_add(gen_tbl_list,
2860                                                                &hash_entry,
2861                                                                &gen_tbl_ent);
2862                         if (rc)
2863                                 return rc;
2864                         /* store the hash index in the fdb */
2865                         key_index = hash_entry.hash_index;
2866                 }
2867                 /* check the reference count */
2868                 if (ULP_GEN_TBL_REF_CNT(&gen_tbl_ent)) {
2869                         /* a hit then error */
2870                         BNXT_TF_DBG(ERR, "generic entry already present\n");
2871                         return -EINVAL; /* success */
2872                 }
2873
2874                 /* Initialize the blob data */
2875                 if (!ulp_blob_init(&data, tbl->result_bit_size,
2876                                    gen_tbl_ent.byte_order)) {
2877                         BNXT_TF_DBG(ERR, "Failed initial index table blob\n");
2878                         return -EINVAL;
2879                 }
2880
2881                 /* Get the result fields list */
2882                 rc = ulp_mapper_tbl_result_build(parms, tbl, &data,
2883                                                  "Gen tbl Result");
2884                 if (rc) {
2885                         BNXT_TF_DBG(ERR, "Failed to build the result blob\n");
2886                         return rc;
2887                 }
2888                 byte_data = ulp_blob_data_get(&data, &tmplen);
2889                 rc = ulp_mapper_gen_tbl_entry_data_set(&gen_tbl_ent,
2890                                                        tmplen, byte_data,
2891                                                        ULP_BITS_2_BYTE(tmplen));
2892                 if (rc) {
2893                         BNXT_TF_DBG(ERR, "Failed to write generic table\n");
2894                         return -EINVAL;
2895                 }
2896
2897                 /* increment the reference count */
2898                 ULP_GEN_TBL_REF_CNT_INC(&gen_tbl_ent);
2899                 fdb_write = 1;
2900                 parms->shared_hndl = (uint64_t)tbl_idx << 32 | key_index;
2901                 break;
2902         default:
2903                 BNXT_TF_DBG(ERR, "Invalid table opcode %x\n", tbl->tbl_opcode);
2904                 return -EINVAL;
2905         }
2906
2907         /* Set the generic entry hit */
2908         rc = ulp_regfile_write(parms->regfile,
2909                                BNXT_ULP_RF_IDX_GENERIC_TBL_MISS,
2910                                tfp_cpu_to_be_64(gen_tbl_miss));
2911         if (rc) {
2912                 BNXT_TF_DBG(ERR, "Write regfile[%d] failed\n",
2913                             BNXT_ULP_RF_IDX_GENERIC_TBL_MISS);
2914                 return -EIO;
2915         }
2916
2917         /* add the entry to the flow database */
2918         if (fdb_write) {
2919                 memset(&fid_parms, 0, sizeof(fid_parms));
2920                 fid_parms.direction = tbl->direction;
2921                 fid_parms.resource_func = tbl->resource_func;
2922                 fid_parms.resource_sub_type = tbl->resource_sub_type;
2923                 fid_parms.resource_hndl = key_index;
2924                 fid_parms.critical_resource = tbl->critical_resource;
2925                 ulp_flow_db_shared_session_set(&fid_parms, tbl->shared_session);
2926
2927                 rc = ulp_mapper_fdb_opc_process(parms, tbl, &fid_parms);
2928                 if (rc)
2929                         BNXT_TF_DBG(ERR, "Fail to add gen ent flowdb %d\n", rc);
2930         }
2931         return rc;
2932 }
2933
2934 static int32_t
2935 ulp_mapper_ctrl_tbl_process(struct bnxt_ulp_mapper_parms *parms,
2936                             struct bnxt_ulp_mapper_tbl_info *tbl)
2937 {
2938         int32_t rc = 0;
2939
2940         /* process the fdb opcode for alloc push */
2941         if (tbl->fdb_opcode == BNXT_ULP_FDB_OPC_ALLOC_RID_REGFILE) {
2942                 rc = ulp_mapper_fdb_opc_alloc_rid(parms, tbl);
2943                 if (rc) {
2944                         BNXT_TF_DBG(ERR, "Failed to do fdb alloc\n");
2945                         return rc;
2946                 }
2947         }
2948         return rc;
2949 }
2950
2951 static int32_t
2952 ulp_mapper_glb_resource_info_init(struct bnxt_ulp_context *ulp_ctx,
2953                                   struct bnxt_ulp_mapper_data *mapper_data)
2954 {
2955         struct bnxt_ulp_glb_resource_info *glb_res;
2956         uint32_t num_glb_res_ids, idx, dev_id;
2957         uint8_t app_id;
2958         int32_t rc = 0;
2959
2960         glb_res = ulp_mapper_glb_resource_info_list_get(&num_glb_res_ids);
2961         if (!glb_res || !num_glb_res_ids) {
2962                 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
2963                 return -EINVAL;
2964         }
2965
2966         rc = bnxt_ulp_cntxt_dev_id_get(ulp_ctx, &dev_id);
2967         if (rc) {
2968                 BNXT_TF_DBG(ERR, "Failed to get device id for glb init (%d)\n",
2969                             rc);
2970                 return rc;
2971         }
2972
2973         rc = bnxt_ulp_cntxt_app_id_get(ulp_ctx, &app_id);
2974         if (rc) {
2975                 BNXT_TF_DBG(ERR, "Failed to get app id for glb init (%d)\n",
2976                             rc);
2977                 return rc;
2978         }
2979
2980         /* Iterate the global resources and process each one */
2981         for (idx = 0; idx < num_glb_res_ids; idx++) {
2982                 if (dev_id != glb_res[idx].device_id ||
2983                     glb_res[idx].app_id != app_id)
2984                         continue;
2985                 switch (glb_res[idx].resource_func) {
2986                 case BNXT_ULP_RESOURCE_FUNC_IDENTIFIER:
2987                         rc = ulp_mapper_resource_ident_allocate(ulp_ctx,
2988                                                                 mapper_data,
2989                                                                 &glb_res[idx]);
2990                         break;
2991                 case BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE:
2992                         rc = ulp_mapper_resource_index_tbl_alloc(ulp_ctx,
2993                                                                  mapper_data,
2994                                                                  &glb_res[idx]);
2995                         break;
2996                 default:
2997                         BNXT_TF_DBG(ERR, "Global resource %x not supported\n",
2998                                     glb_res[idx].resource_func);
2999                         rc = -EINVAL;
3000                         break;
3001                 }
3002                 if (rc)
3003                         return rc;
3004         }
3005         return rc;
3006 }
3007
3008 /*
3009  * Iterate over the shared resources assigned during tf_open_session and store
3010  * them in the global regfile with the shared flag.
3011  */
3012 static int32_t
3013 ulp_mapper_app_glb_resource_info_init(struct bnxt_ulp_context *ulp_ctx,
3014                                       struct bnxt_ulp_mapper_data *mapper_data)
3015 {
3016         struct tf_get_shared_tbl_increment_parms iparms;
3017         struct bnxt_ulp_glb_resource_info *glb_res;
3018         struct tf_get_session_info_parms sparms;
3019         uint32_t num_entries, i, dev_id, res;
3020         struct tf_resource_info *res_info;
3021         uint32_t addend;
3022         uint64_t regval;
3023         enum tf_dir dir;
3024         int32_t rc = 0;
3025         struct tf *tfp;
3026         uint8_t app_id;
3027
3028         memset(&sparms, 0, sizeof(sparms));
3029         glb_res = bnxt_ulp_app_glb_resource_info_list_get(&num_entries);
3030         if (!glb_res || !num_entries) {
3031                 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
3032                 return -EINVAL;
3033         }
3034         tfp = bnxt_ulp_cntxt_shared_tfp_get(ulp_ctx);
3035         if (!tfp) {
3036                 BNXT_TF_DBG(ERR, "Failed to get tfp for app global init");
3037                 return -EINVAL;
3038         }
3039         /*
3040          * Retrieve the resources that were assigned during the shared session
3041          * creation.
3042          */
3043         rc = tf_get_session_info(tfp, &sparms);
3044         if (rc) {
3045                 BNXT_TF_DBG(ERR, "Failed to get session info (%d)\n", rc);
3046                 return rc;
3047         }
3048
3049         rc = bnxt_ulp_cntxt_app_id_get(ulp_ctx, &app_id);
3050         if (rc) {
3051                 BNXT_TF_DBG(ERR, "Failed to get the app id in glb init (%d).\n",
3052                             rc);
3053                 return rc;
3054         }
3055
3056         rc = bnxt_ulp_cntxt_dev_id_get(ulp_ctx, &dev_id);
3057         if (rc) {
3058                 BNXT_TF_DBG(ERR, "Failed to get dev id for app glb init (%d)\n",
3059                             rc);
3060                 return rc;
3061         }
3062
3063         /* Store all the app global resources */
3064         for (i = 0; i < num_entries; i++) {
3065                 if (dev_id != glb_res[i].device_id ||
3066                     app_id != glb_res[i].app_id)
3067                         continue;
3068                 dir = glb_res[i].direction;
3069                 res = glb_res[i].resource_type;
3070                 addend = 1;
3071
3072                 switch (glb_res[i].resource_func) {
3073                 case BNXT_ULP_RESOURCE_FUNC_IDENTIFIER:
3074                         res_info = &sparms.session_info.ident[dir].info[res];
3075                         break;
3076                 case BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE:
3077                         /*
3078                          * Tables may have various strides for the allocations.
3079                          * Need to account.
3080                          */
3081                         memset(&iparms, 0, sizeof(iparms));
3082                         iparms.dir = dir;
3083                         iparms.type = res;
3084                         rc = tf_get_shared_tbl_increment(tfp, &iparms);
3085                         if (rc) {
3086                                 BNXT_TF_DBG(ERR,
3087                                             "Failed to get addend for %s[%s] rc=(%d)\n",
3088                                             tf_tbl_type_2_str(res),
3089                                             tf_dir_2_str(dir), rc);
3090                                 return rc;
3091                         }
3092                         addend = iparms.increment_cnt;
3093                         res_info = &sparms.session_info.tbl[dir].info[res];
3094                         break;
3095                 case BNXT_ULP_RESOURCE_FUNC_TCAM_TABLE:
3096                         res_info = &sparms.session_info.tcam[dir].info[res];
3097                         break;
3098                 case BNXT_ULP_RESOURCE_FUNC_EM_TABLE:
3099                         res_info = &sparms.session_info.em[dir].info[res];
3100                         break;
3101                 default:
3102                         BNXT_TF_DBG(ERR, "Unknown resource func (0x%x)\n",
3103                                     glb_res[i].resource_func);
3104                         continue;
3105                 }
3106                 regval = tfp_cpu_to_be_64((uint64_t)res_info->start);
3107                 res_info->start += addend;
3108                 /*
3109                  * All resources written to the global regfile are shared for
3110                  * this function.
3111                  */
3112                 rc = ulp_mapper_glb_resource_write(mapper_data, &glb_res[i],
3113                                                    regval, true);
3114                 if (rc)
3115                         return rc;
3116         }
3117
3118         return rc;
3119 }
3120
3121 /*
3122  * Common conditional opcode process routine that is used for both the template
3123  * rejection and table conditional execution.
3124  */
3125 static int32_t
3126 ulp_mapper_cond_opc_process(struct bnxt_ulp_mapper_parms *parms,
3127                             enum bnxt_ulp_cond_opc opc,
3128                             uint32_t operand,
3129                             int32_t *res)
3130 {
3131         enum bnxt_ulp_flow_mem_type mtype = BNXT_ULP_FLOW_MEM_TYPE_INT;
3132         int32_t rc = 0;
3133         uint8_t bit;
3134         uint64_t regval;
3135
3136         switch (opc) {
3137         case BNXT_ULP_COND_OPC_CF_IS_SET:
3138                 if (operand < BNXT_ULP_CF_IDX_LAST) {
3139                         *res = ULP_COMP_FLD_IDX_RD(parms, operand);
3140                 } else {
3141                         BNXT_TF_DBG(ERR, "comp field out of bounds %d\n",
3142                                     operand);
3143                         rc = -EINVAL;
3144                 }
3145                 break;
3146         case BNXT_ULP_COND_OPC_CF_NOT_SET:
3147                 if (operand < BNXT_ULP_CF_IDX_LAST) {
3148                         *res = !ULP_COMP_FLD_IDX_RD(parms, operand);
3149                 } else {
3150                         BNXT_TF_DBG(ERR, "comp field out of bounds %d\n",
3151                                     operand);
3152                         rc = -EINVAL;
3153                 }
3154                 break;
3155         case BNXT_ULP_COND_OPC_ACT_BIT_IS_SET:
3156                 if (operand < BNXT_ULP_ACT_BIT_LAST) {
3157                         *res = ULP_BITMAP_ISSET(parms->act_bitmap->bits,
3158                                                 operand);
3159                 } else {
3160                         BNXT_TF_DBG(ERR, "action bit out of bounds %d\n",
3161                                     operand);
3162                         rc = -EINVAL;
3163                 }
3164                 break;
3165         case BNXT_ULP_COND_OPC_ACT_BIT_NOT_SET:
3166                 if (operand < BNXT_ULP_ACT_BIT_LAST) {
3167                         *res = !ULP_BITMAP_ISSET(parms->act_bitmap->bits,
3168                                                operand);
3169                 } else {
3170                         BNXT_TF_DBG(ERR, "action bit out of bounds %d\n",
3171                                     operand);
3172                         rc = -EINVAL;
3173                 }
3174                 break;
3175         case BNXT_ULP_COND_OPC_HDR_BIT_IS_SET:
3176                 if (operand < BNXT_ULP_HDR_BIT_LAST) {
3177                         *res = ULP_BITMAP_ISSET(parms->hdr_bitmap->bits,
3178                                                 operand);
3179                 } else {
3180                         BNXT_TF_DBG(ERR, "header bit out of bounds %d\n",
3181                                     operand);
3182                         rc = -EINVAL;
3183                 }
3184                 break;
3185         case BNXT_ULP_COND_OPC_HDR_BIT_NOT_SET:
3186                 if (operand < BNXT_ULP_HDR_BIT_LAST) {
3187                         *res = !ULP_BITMAP_ISSET(parms->hdr_bitmap->bits,
3188                                                operand);
3189                 } else {
3190                         BNXT_TF_DBG(ERR, "header bit out of bounds %d\n",
3191                                     operand);
3192                         rc = -EINVAL;
3193                 }
3194                 break;
3195         case BNXT_ULP_COND_OPC_FIELD_BIT_IS_SET:
3196                 rc = ulp_mapper_glb_field_tbl_get(parms, operand, &bit);
3197                 if (rc) {
3198                         BNXT_TF_DBG(ERR, "invalid ulp_glb_field_tbl idx %d\n",
3199                                     operand);
3200                         return -EINVAL;
3201                 }
3202                 *res = ULP_INDEX_BITMAP_GET(parms->fld_bitmap->bits, bit);
3203                 break;
3204         case BNXT_ULP_COND_OPC_FIELD_BIT_NOT_SET:
3205                 rc = ulp_mapper_glb_field_tbl_get(parms, operand, &bit);
3206                 if (rc) {
3207                         BNXT_TF_DBG(ERR, "invalid ulp_glb_field_tbl idx %d\n",
3208                                     operand);
3209                         return -EINVAL;
3210                 }
3211                 *res = !ULP_INDEX_BITMAP_GET(parms->fld_bitmap->bits, bit);
3212                 break;
3213         case BNXT_ULP_COND_OPC_RF_IS_SET:
3214                 if (!ulp_regfile_read(parms->regfile, operand, &regval)) {
3215                         BNXT_TF_DBG(ERR, "regfile[%d] read oob\n", operand);
3216                         return -EINVAL;
3217                 }
3218                 *res = regval != 0;
3219                 break;
3220         case BNXT_ULP_COND_OPC_RF_NOT_SET:
3221                 if (!ulp_regfile_read(parms->regfile, operand, &regval)) {
3222                         BNXT_TF_DBG(ERR, "regfile[%d] read oob\n", operand);
3223                         return -EINVAL;
3224                 }
3225                 *res = regval == 0;
3226                 break;
3227         case BNXT_ULP_COND_OPC_FLOW_PAT_MATCH:
3228                 if (parms->flow_pattern_id == operand) {
3229                         BNXT_TF_DBG(ERR, "field pattern match failed %x\n",
3230                                     parms->flow_pattern_id);
3231                         return -EINVAL;
3232                 }
3233                 break;
3234         case BNXT_ULP_COND_OPC_ACT_PAT_MATCH:
3235                 if (parms->act_pattern_id == operand) {
3236                         BNXT_TF_DBG(ERR, "act pattern match failed %x\n",
3237                                     parms->act_pattern_id);
3238                         return -EINVAL;
3239                 }
3240                 break;
3241         case BNXT_ULP_COND_OPC_EXT_MEM_IS_SET:
3242                 if (bnxt_ulp_cntxt_mem_type_get(parms->ulp_ctx, &mtype)) {
3243                         BNXT_TF_DBG(ERR, "Failed to get the mem type\n");
3244                         return -EINVAL;
3245                 }
3246                 *res = (mtype == BNXT_ULP_FLOW_MEM_TYPE_INT) ? 0 : 1;
3247                 break;
3248         case BNXT_ULP_COND_OPC_EXT_MEM_NOT_SET:
3249                 if (bnxt_ulp_cntxt_mem_type_get(parms->ulp_ctx, &mtype)) {
3250                         BNXT_TF_DBG(ERR, "Failed to get the mem type\n");
3251                         return -EINVAL;
3252                 }
3253                 *res = (mtype == BNXT_ULP_FLOW_MEM_TYPE_INT) ? 1 : 0;
3254                 break;
3255         default:
3256                 BNXT_TF_DBG(ERR, "Invalid conditional opcode %d\n", opc);
3257                 rc = -EINVAL;
3258                 break;
3259         }
3260         return (rc);
3261 }
3262
3263 static int32_t
3264 ulp_mapper_func_opr_compute(struct bnxt_ulp_mapper_parms *parms,
3265                             enum tf_dir dir,
3266                             enum bnxt_ulp_func_src func_src,
3267                             uint16_t func_opr,
3268                             uint64_t *result)
3269 {
3270         uint64_t regval;
3271         bool shared;
3272
3273         *result =  false;
3274         switch (func_src) {
3275         case BNXT_ULP_FUNC_SRC_COMP_FIELD:
3276                 if (func_opr >= BNXT_ULP_CF_IDX_LAST) {
3277                         BNXT_TF_DBG(ERR, "invalid index %u\n", func_opr);
3278                         return -EINVAL;
3279                 }
3280                 *result = ULP_COMP_FLD_IDX_RD(parms, func_opr);
3281                 break;
3282         case BNXT_ULP_FUNC_SRC_REGFILE:
3283                 if (!ulp_regfile_read(parms->regfile, func_opr, &regval)) {
3284                         BNXT_TF_DBG(ERR, "regfile[%d] read oob\n", func_opr);
3285                         return -EINVAL;
3286                 }
3287                 *result = tfp_be_to_cpu_64(regval);
3288                 break;
3289         case BNXT_ULP_FUNC_SRC_GLB_REGFILE:
3290                 if (ulp_mapper_glb_resource_read(parms->mapper_data, dir,
3291                                                  func_opr, &regval, &shared)) {
3292                         BNXT_TF_DBG(ERR, "global regfile[%d] read failed.\n",
3293                                     func_opr);
3294                         return -EINVAL;
3295                 }
3296                 *result = tfp_be_to_cpu_64(regval);
3297                 break;
3298         case BNXT_ULP_FUNC_SRC_CONST:
3299                 *result = func_opr;
3300                 break;
3301         default:
3302                 BNXT_TF_DBG(ERR, "invalid src code %u\n", func_src);
3303                 return -EINVAL;
3304         }
3305         return 0;
3306 }
3307
3308 static int32_t
3309 ulp_mapper_func_info_process(struct bnxt_ulp_mapper_parms *parms,
3310                              struct bnxt_ulp_mapper_tbl_info *tbl)
3311 {
3312         struct bnxt_ulp_mapper_func_info *func_info = &tbl->func_info;
3313         uint64_t res = 0, res1 = 0, res2 = 0;
3314         int32_t rc = 0;
3315         uint32_t process_src1 = 0, process_src2 = 0;
3316
3317         /* determine which functional operands to compute */
3318         switch (func_info->func_opc) {
3319         case BNXT_ULP_FUNC_OPC_NOP:
3320                 return rc;
3321         case BNXT_ULP_FUNC_OPC_EQ:
3322         case BNXT_ULP_FUNC_OPC_NE:
3323         case BNXT_ULP_FUNC_OPC_GE:
3324         case BNXT_ULP_FUNC_OPC_GT:
3325         case BNXT_ULP_FUNC_OPC_LE:
3326         case BNXT_ULP_FUNC_OPC_LT:
3327                 process_src1 = 1;
3328                 process_src2 = 1;
3329                 break;
3330         case BNXT_ULP_FUNC_OPC_COPY_SRC1_TO_RF:
3331                 process_src1 = 1;
3332                 break;
3333         default:
3334                 break;
3335         }
3336
3337         if (process_src1) {
3338                 rc = ulp_mapper_func_opr_compute(parms, tbl->direction,
3339                                                  func_info->func_src1,
3340                                                  func_info->func_opr1, &res1);
3341                 if (rc)
3342                         return rc;
3343         }
3344
3345         if (process_src2) {
3346                 rc = ulp_mapper_func_opr_compute(parms, tbl->direction,
3347                                                  func_info->func_src2,
3348                                                  func_info->func_opr2, &res2);
3349                 if (rc)
3350                         return rc;
3351         }
3352
3353         /* perform the functional opcode operations */
3354         switch (func_info->func_opc) {
3355         case BNXT_ULP_FUNC_OPC_EQ:
3356                 if (res1 == res2)
3357                         res = 1;
3358                 break;
3359         case BNXT_ULP_FUNC_OPC_NE:
3360                 if (res1 != res2)
3361                         res = 1;
3362                 break;
3363         case BNXT_ULP_FUNC_OPC_GE:
3364                 if (res1 >= res2)
3365                         res = 1;
3366                 break;
3367         case BNXT_ULP_FUNC_OPC_GT:
3368                 if (res1 > res2)
3369                         res = 1;
3370                 break;
3371         case BNXT_ULP_FUNC_OPC_LE:
3372                 if (res1 <= res2)
3373                         res = 1;
3374                 break;
3375         case BNXT_ULP_FUNC_OPC_LT:
3376                 if (res1 < res2)
3377                         res = 1;
3378                 break;
3379         case BNXT_ULP_FUNC_OPC_COPY_SRC1_TO_RF:
3380                 res = res1;
3381                 break;
3382         case BNXT_ULP_FUNC_OPC_RSS_CONFIG:
3383                 /* apply the rss config using pmd method */
3384                 return bnxt_rss_config_action_apply(parms);
3385         case BNXT_ULP_FUNC_OPC_GET_PARENT_MAC_ADDR:
3386                 rc = bnxt_pmd_get_parent_mac_addr(parms, (uint8_t *)&res);
3387                 if (rc)
3388                         return -EINVAL;
3389                 res = tfp_be_to_cpu_64(res);
3390                 break;
3391         default:
3392                 BNXT_TF_DBG(ERR, "invalid func code %u\n", func_info->func_opc);
3393                 return -EINVAL;
3394         }
3395         if (ulp_regfile_write(parms->regfile, func_info->func_dst_opr,
3396                               tfp_cpu_to_be_64(res))) {
3397                 BNXT_TF_DBG(ERR, "Failed write the func_opc %u\n",
3398                             func_info->func_dst_opr);
3399                 return -EINVAL;
3400         }
3401
3402         return rc;
3403 }
3404
3405
3406 /*
3407  * Processes a list of conditions and returns both a status and result of the
3408  * list.  The status must be checked prior to verifying the result.
3409  *
3410  * returns 0 for success, negative on failure
3411  * returns res = 1 for true, res = 0 for false.
3412  */
3413 static int32_t
3414 ulp_mapper_cond_opc_list_process(struct bnxt_ulp_mapper_parms *parms,
3415                                  enum bnxt_ulp_cond_list_opc list_opc,
3416                                  struct bnxt_ulp_mapper_cond_info *list,
3417                                  uint32_t num,
3418                                  int32_t *res)
3419 {
3420         uint32_t i;
3421         int32_t rc = 0, trc = 0;
3422
3423         switch (list_opc) {
3424         case BNXT_ULP_COND_LIST_OPC_AND:
3425                 /* AND Defaults to true. */
3426                 *res = 1;
3427                 break;
3428         case BNXT_ULP_COND_LIST_OPC_OR:
3429                 /* OR Defaults to false. */
3430                 *res = 0;
3431                 break;
3432         case BNXT_ULP_COND_LIST_OPC_TRUE:
3433                 *res = 1;
3434                 return rc;
3435         case BNXT_ULP_COND_LIST_OPC_FALSE:
3436                 *res = 0;
3437                 return rc;
3438         default:
3439                 BNXT_TF_DBG(ERR, "Invalid conditional list opcode %d\n",
3440                             list_opc);
3441                 *res = 0;
3442                 return -EINVAL;
3443         }
3444
3445         for (i = 0; i < num; i++) {
3446                 rc = ulp_mapper_cond_opc_process(parms,
3447                                                  list[i].cond_opcode,
3448                                                  list[i].cond_operand,
3449                                                  &trc);
3450                 if (rc)
3451                         return rc;
3452
3453                 if (list_opc == BNXT_ULP_COND_LIST_OPC_AND) {
3454                         /* early return if result is ever zero */
3455                         if (!trc) {
3456                                 *res = trc;
3457                                 return rc;
3458                         }
3459                 } else {
3460                         /* early return if result is ever non-zero */
3461                         if (trc) {
3462                                 *res = trc;
3463                                 return rc;
3464                         }
3465                 }
3466         }
3467
3468         return rc;
3469 }
3470
3471 /*
3472  * Processes conflict resolution and returns both a status and result.
3473  * The status must be checked prior to verifying the result.
3474  *
3475  * returns 0 for success, negative on failure
3476  * returns res = 1 for true, res = 0 for false.
3477  */
3478 static int32_t
3479 ulp_mapper_conflict_resolution_process(struct bnxt_ulp_mapper_parms *parms,
3480                                        struct bnxt_ulp_mapper_tbl_info *tbl,
3481                                        int32_t *res)
3482 {
3483         int32_t rc = 0;
3484         uint64_t regval;
3485         uint64_t comp_sig;
3486
3487         *res = 0;
3488         switch (tbl->accept_opcode) {
3489         case BNXT_ULP_ACCEPT_OPC_ALWAYS:
3490                 *res = 1;
3491                 break;
3492         case BNXT_ULP_ACCEPT_OPC_FLOW_SIG_ID_MATCH:
3493                 /* perform the signature validation*/
3494                 if (tbl->resource_func ==
3495                     BNXT_ULP_RESOURCE_FUNC_GENERIC_TABLE) {
3496                         /* Perform the check that generic table is hit or not */
3497                         if (!ulp_regfile_read(parms->regfile,
3498                                               BNXT_ULP_RF_IDX_GENERIC_TBL_MISS,
3499                                               &regval)) {
3500                                 BNXT_TF_DBG(ERR, "regfile[%d] read oob\n",
3501                                             BNXT_ULP_RF_IDX_GENERIC_TBL_MISS);
3502                                 return -EINVAL;
3503                         }
3504                         if (regval) {
3505                                 /* not a hit so no need to check flow sign*/
3506                                 *res = 1;
3507                                 return rc;
3508                         }
3509                 }
3510                 /* compare the new flow signature against stored one */
3511                 if (!ulp_regfile_read(parms->regfile,
3512                                       BNXT_ULP_RF_IDX_FLOW_SIG_ID,
3513                                       &regval)) {
3514                         BNXT_TF_DBG(ERR, "regfile[%d] read oob\n",
3515                                     BNXT_ULP_RF_IDX_FLOW_SIG_ID);
3516                         return -EINVAL;
3517                 }
3518                 comp_sig = ULP_COMP_FLD_IDX_RD(parms,
3519                                                BNXT_ULP_CF_IDX_FLOW_SIG_ID);
3520                 regval = tfp_be_to_cpu_64(regval);
3521                 if (comp_sig == regval)
3522                         *res = 1;
3523                 else
3524                         BNXT_TF_DBG(ERR, "failed signature match 0x%016"
3525                                     PRIX64 ":%x\n", comp_sig, (uint32_t)regval);
3526                 break;
3527         default:
3528                 BNXT_TF_DBG(ERR, "Invalid accept opcode %d\n",
3529                             tbl->accept_opcode);
3530                 return -EINVAL;
3531         }
3532         return rc;
3533 }
3534
3535 static int32_t
3536 ulp_mapper_tbls_process(struct bnxt_ulp_mapper_parms *parms, uint32_t tid)
3537 {
3538         struct bnxt_ulp_mapper_cond_info *cond_tbls = NULL;
3539         enum bnxt_ulp_cond_list_opc cond_opc;
3540         struct bnxt_ulp_mapper_tbl_info *tbls;
3541         struct bnxt_ulp_mapper_tbl_info *tbl;
3542         uint32_t num_tbls, tbl_idx, num_cond_tbls;
3543         int32_t rc = -EINVAL, cond_rc = 0;
3544         int32_t cond_goto = 1;
3545
3546         cond_tbls = ulp_mapper_tmpl_reject_list_get(parms, tid,
3547                                                     &num_cond_tbls,
3548                                                     &cond_opc);
3549         /*
3550          * Process the reject list if exists, otherwise assume that the
3551          * template is allowed.
3552          */
3553         if (cond_tbls && num_cond_tbls) {
3554                 rc = ulp_mapper_cond_opc_list_process(parms,
3555                                                       cond_opc,
3556                                                       cond_tbls,
3557                                                       num_cond_tbls,
3558                                                       &cond_rc);
3559                 if (rc)
3560                         return rc;
3561
3562                 /* Reject the template if True */
3563                 if (cond_rc) {
3564                         BNXT_TF_DBG(ERR, "%s Template %d rejected.\n",
3565                                     ulp_mapper_tmpl_name_str(parms->tmpl_type),
3566                                     tid);
3567                         return -EINVAL;
3568                 }
3569         }
3570
3571         tbls = ulp_mapper_tbl_list_get(parms, tid, &num_tbls);
3572         if (!tbls || !num_tbls) {
3573                 BNXT_TF_DBG(ERR, "No %s tables for %d:%d\n",
3574                             ulp_mapper_tmpl_name_str(parms->tmpl_type),
3575                             parms->dev_id, tid);
3576                 return -EINVAL;
3577         }
3578
3579         for (tbl_idx = 0; tbl_idx < num_tbls && cond_goto;) {
3580                 tbl = &tbls[tbl_idx];
3581                 cond_goto = tbl->execute_info.cond_true_goto;
3582 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG
3583 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG_MAPPER
3584                 ulp_mapper_table_dump(tbl, tbl_idx);
3585 #endif
3586 #endif
3587                 /* Process the conditional func code opcodes */
3588                 if (ulp_mapper_func_info_process(parms, tbl)) {
3589                         BNXT_TF_DBG(ERR, "Failed to process cond update\n");
3590                         rc = -EINVAL;
3591                         goto error;
3592                 }
3593
3594                 cond_tbls = ulp_mapper_tbl_execute_list_get(parms, tbl,
3595                                                             &num_cond_tbls,
3596                                                             &cond_opc);
3597                 rc = ulp_mapper_cond_opc_list_process(parms, cond_opc,
3598                                                       cond_tbls, num_cond_tbls,
3599                                                       &cond_rc);
3600                 if (rc) {
3601                         BNXT_TF_DBG(ERR, "Failed to proc cond opc list (%d)\n",
3602                                     rc);
3603                         goto error;
3604                 }
3605                 /* Skip the table if False */
3606                 if (!cond_rc) {
3607                         cond_goto = tbl->execute_info.cond_false_goto;
3608                         goto next_iteration;
3609                 }
3610
3611                 switch (tbl->resource_func) {
3612                 case BNXT_ULP_RESOURCE_FUNC_TCAM_TABLE:
3613                         rc = ulp_mapper_tcam_tbl_process(parms, tbl);
3614                         break;
3615                 case BNXT_ULP_RESOURCE_FUNC_EM_TABLE:
3616                         rc = ulp_mapper_em_tbl_process(parms, tbl);
3617                         break;
3618                 case BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE:
3619                         rc = ulp_mapper_index_tbl_process(parms, tbl);
3620                         break;
3621                 case BNXT_ULP_RESOURCE_FUNC_IF_TABLE:
3622                         rc = ulp_mapper_if_tbl_process(parms, tbl);
3623                         break;
3624                 case BNXT_ULP_RESOURCE_FUNC_GENERIC_TABLE:
3625                         rc = ulp_mapper_gen_tbl_process(parms, tbl);
3626                         break;
3627                 case BNXT_ULP_RESOURCE_FUNC_CTRL_TABLE:
3628                         rc = ulp_mapper_ctrl_tbl_process(parms, tbl);
3629                         break;
3630                 case BNXT_ULP_RESOURCE_FUNC_INVALID:
3631                         rc = 0;
3632                         break;
3633                 default:
3634                         BNXT_TF_DBG(ERR, "Unexpected mapper resource %d\n",
3635                                     tbl->resource_func);
3636                         rc = -EINVAL;
3637                         goto error;
3638                 }
3639
3640                 if (rc) {
3641                         BNXT_TF_DBG(ERR, "Resource type %d failed\n",
3642                                     tbl->resource_func);
3643                         goto error;
3644                 }
3645
3646                 /* perform the post table process */
3647                 rc  = ulp_mapper_conflict_resolution_process(parms, tbl,
3648                                                              &cond_rc);
3649                 if (rc || !cond_rc) {
3650                         BNXT_TF_DBG(ERR, "Failed due to conflict resolution\n");
3651                         rc = -EINVAL;
3652                         goto error;
3653                 }
3654 next_iteration:
3655                 if (cond_goto == BNXT_ULP_COND_GOTO_REJECT) {
3656                         BNXT_TF_DBG(ERR, "reject the flow\n");
3657                         rc = -EINVAL;
3658                         goto error;
3659                 } else if (cond_goto & BNXT_ULP_COND_GOTO_RF) {
3660                         uint32_t rf_idx;
3661                         uint64_t regval;
3662
3663                         /* least significant 16 bits from reg_file index */
3664                         rf_idx = (uint32_t)(cond_goto & 0xFFFF);
3665                         if (!ulp_regfile_read(parms->regfile, rf_idx,
3666                                               &regval)) {
3667                                 BNXT_TF_DBG(ERR, "regfile[%d] read oob\n",
3668                                             rf_idx);
3669                                 rc = -EINVAL;
3670                                 goto error;
3671                         }
3672                         cond_goto = (int32_t)regval;
3673                 }
3674
3675                 if (cond_goto < 0 && ((int32_t)tbl_idx + cond_goto) < 0) {
3676                         BNXT_TF_DBG(ERR, "invalid conditional goto %d\n",
3677                                     cond_goto);
3678                         goto error;
3679                 }
3680                 tbl_idx += cond_goto;
3681         }
3682
3683         return rc;
3684 error:
3685         BNXT_TF_DBG(ERR, "%s tables failed creation for %d:%d\n",
3686                     ulp_mapper_tmpl_name_str(parms->tmpl_type),
3687                     parms->dev_id, tid);
3688         return rc;
3689 }
3690
3691 static int32_t
3692 ulp_mapper_resource_free(struct bnxt_ulp_context *ulp,
3693                          uint32_t fid,
3694                          struct ulp_flow_db_res_params *res)
3695 {
3696         struct tf *tfp;
3697         int32_t rc = 0;
3698
3699         if (!res || !ulp) {
3700                 BNXT_TF_DBG(ERR, "Unable to free resource\n ");
3701                 return -EINVAL;
3702         }
3703         if (res->fdb_flags & ULP_FDB_FLAG_SHARED_SESSION)
3704                 tfp = bnxt_ulp_cntxt_tfp_get(ulp, BNXT_ULP_SHARED_SESSION_YES);
3705         else
3706                 tfp = bnxt_ulp_cntxt_tfp_get(ulp, BNXT_ULP_SHARED_SESSION_NO);
3707         if (!tfp) {
3708                 BNXT_TF_DBG(ERR, "Unable to free resource failed to get tfp\n");
3709                 return -EINVAL;
3710         }
3711
3712         switch (res->resource_func) {
3713         case BNXT_ULP_RESOURCE_FUNC_TCAM_TABLE:
3714                 rc = ulp_mapper_tcam_entry_free(ulp, tfp, res);
3715                 break;
3716         case BNXT_ULP_RESOURCE_FUNC_EM_TABLE:
3717                 rc = ulp_mapper_em_entry_free(ulp, tfp, res);
3718                 break;
3719         case BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE:
3720                 rc = ulp_mapper_index_entry_free(ulp, tfp, res);
3721                 break;
3722         case BNXT_ULP_RESOURCE_FUNC_IDENTIFIER:
3723                 rc = ulp_mapper_ident_free(ulp, tfp, res);
3724                 break;
3725         case BNXT_ULP_RESOURCE_FUNC_HW_FID:
3726                 rc = ulp_mapper_mark_free(ulp, res);
3727                 break;
3728         case BNXT_ULP_RESOURCE_FUNC_PARENT_FLOW:
3729                 rc = ulp_mapper_parent_flow_free(ulp, fid, res);
3730                 break;
3731         case BNXT_ULP_RESOURCE_FUNC_CHILD_FLOW:
3732                 rc = ulp_mapper_child_flow_free(ulp, fid, res);
3733                 break;
3734         case BNXT_ULP_RESOURCE_FUNC_GENERIC_TABLE:
3735                 rc = ulp_mapper_gen_tbl_res_free(ulp, res);
3736                 break;
3737         default:
3738                 break;
3739         }
3740
3741         return rc;
3742 }
3743
3744 int32_t
3745 ulp_mapper_resources_free(struct bnxt_ulp_context *ulp_ctx,
3746                           enum bnxt_ulp_fdb_type flow_type,
3747                           uint32_t fid)
3748 {
3749         struct ulp_flow_db_res_params res_parms = { 0 };
3750         int32_t rc, trc;
3751
3752         if (!ulp_ctx) {
3753                 BNXT_TF_DBG(ERR, "Invalid parms, unable to free flow\n");
3754                 return -EINVAL;
3755         }
3756
3757         /*
3758          * Set the critical resource on the first resource del, then iterate
3759          * while status is good
3760          */
3761         if (flow_type != BNXT_ULP_FDB_TYPE_RID)
3762                 res_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_YES;
3763
3764         rc = ulp_flow_db_resource_del(ulp_ctx, flow_type, fid, &res_parms);
3765
3766         if (rc) {
3767                 /*
3768                  * This is unexpected on the first call to resource del.
3769                  * It likely means that the flow did not exist in the flow db.
3770                  */
3771                 BNXT_TF_DBG(ERR, "Flow[%d][0x%08x] failed to free (rc=%d)\n",
3772                             flow_type, fid, rc);
3773                 return rc;
3774         }
3775
3776         while (!rc) {
3777                 trc = ulp_mapper_resource_free(ulp_ctx, fid, &res_parms);
3778                 if (trc)
3779                         /*
3780                          * On fail, we still need to attempt to free the
3781                          * remaining resources.  Don't return
3782                          */
3783                         BNXT_TF_DBG(ERR,
3784                                     "Flow[%d][0x%x] Res[%d][0x%016" PRIX64
3785                                     "] failed rc=%d.\n",
3786                                     flow_type, fid, res_parms.resource_func,
3787                                     res_parms.resource_hndl, trc);
3788
3789                 /* All subsequent call require the non-critical_resource */
3790                 res_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
3791
3792                 rc = ulp_flow_db_resource_del(ulp_ctx,
3793                                               flow_type,
3794                                               fid,
3795                                               &res_parms);
3796         }
3797
3798         /* Free the Flow ID since we've removed all resources */
3799         rc = ulp_flow_db_fid_free(ulp_ctx, flow_type, fid);
3800
3801         return rc;
3802 }
3803
3804 static void
3805 ulp_mapper_glb_resource_info_deinit(struct bnxt_ulp_context *ulp_ctx,
3806                                     struct bnxt_ulp_mapper_data *mapper_data)
3807 {
3808         struct bnxt_ulp_mapper_glb_resource_entry *ent;
3809         struct ulp_flow_db_res_params res;
3810         uint32_t dir, idx;
3811
3812         /* Iterate the global resources and process each one */
3813         for (dir = TF_DIR_RX; dir < TF_DIR_MAX; dir++) {
3814                 for (idx = 0; idx < BNXT_ULP_GLB_RF_IDX_LAST; idx++) {
3815                         ent = &mapper_data->glb_res_tbl[dir][idx];
3816                         if (ent->resource_func ==
3817                             BNXT_ULP_RESOURCE_FUNC_INVALID ||
3818                             ent->shared)
3819                                 continue;
3820                         memset(&res, 0, sizeof(struct ulp_flow_db_res_params));
3821                         res.resource_func = ent->resource_func;
3822                         res.direction = dir;
3823                         res.resource_type = ent->resource_type;
3824                         /*convert it from BE to cpu */
3825                         res.resource_hndl =
3826                                 tfp_be_to_cpu_64(ent->resource_hndl);
3827                         ulp_mapper_resource_free(ulp_ctx, 0, &res);
3828                 }
3829         }
3830 }
3831
3832 int32_t
3833 ulp_mapper_flow_destroy(struct bnxt_ulp_context *ulp_ctx,
3834                         enum bnxt_ulp_fdb_type flow_type,
3835                         uint32_t fid)
3836 {
3837         int32_t rc;
3838
3839         if (!ulp_ctx) {
3840                 BNXT_TF_DBG(ERR, "Invalid parms, unable to free flow\n");
3841                 return -EINVAL;
3842         }
3843
3844         rc = ulp_mapper_resources_free(ulp_ctx, flow_type, fid);
3845         return rc;
3846 }
3847
3848 /* Function to handle the mapping of the Flow to be compatible
3849  * with the underlying hardware.
3850  */
3851 int32_t
3852 ulp_mapper_flow_create(struct bnxt_ulp_context *ulp_ctx,
3853                        struct bnxt_ulp_mapper_create_parms *cparms)
3854 {
3855         struct bnxt_ulp_mapper_parms parms;
3856         struct ulp_regfile regfile;
3857         int32_t  rc = 0, trc;
3858
3859         if (!ulp_ctx || !cparms)
3860                 return -EINVAL;
3861
3862         /* Initialize the parms structure */
3863         memset(&parms, 0, sizeof(parms));
3864         parms.act_prop = cparms->act_prop;
3865         parms.act_bitmap = cparms->act;
3866         parms.hdr_bitmap = cparms->hdr_bitmap;
3867         parms.regfile = &regfile;
3868         parms.hdr_field = cparms->hdr_field;
3869         parms.fld_bitmap = cparms->fld_bitmap;
3870         parms.comp_fld = cparms->comp_fld;
3871         parms.ulp_ctx = ulp_ctx;
3872         parms.act_tid = cparms->act_tid;
3873         parms.class_tid = cparms->class_tid;
3874         parms.flow_type = cparms->flow_type;
3875         parms.parent_flow = cparms->parent_flow;
3876         parms.child_flow = cparms->child_flow;
3877         parms.fid = cparms->flow_id;
3878         parms.tun_idx = cparms->tun_idx;
3879         parms.app_priority = cparms->app_priority;
3880         parms.flow_pattern_id = cparms->flow_pattern_id;
3881         parms.act_pattern_id = cparms->act_pattern_id;
3882         parms.app_id = cparms->app_id;
3883         parms.port_id = cparms->port_id;
3884
3885         /* Get the device id from the ulp context */
3886         if (bnxt_ulp_cntxt_dev_id_get(ulp_ctx, &parms.dev_id)) {
3887                 BNXT_TF_DBG(ERR, "Invalid ulp context\n");
3888                 return -EINVAL;
3889         }
3890
3891         /* Get the device params, it will be used in later processing */
3892         parms.device_params = bnxt_ulp_device_params_get(parms.dev_id);
3893         if (!parms.device_params) {
3894                 BNXT_TF_DBG(ERR, "No device parms for device id %d\n",
3895                             parms.dev_id);
3896                 return -EINVAL;
3897         }
3898
3899         /*
3900          * Get the mapper data for dynamic mapper data such as default
3901          * ids.
3902          */
3903         parms.mapper_data = (struct bnxt_ulp_mapper_data *)
3904                 bnxt_ulp_cntxt_ptr2_mapper_data_get(ulp_ctx);
3905         if (!parms.mapper_data) {
3906                 BNXT_TF_DBG(ERR, "Failed to get the ulp mapper data\n");
3907                 return -EINVAL;
3908         }
3909
3910         /* initialize the registry file for further processing */
3911         if (!ulp_regfile_init(parms.regfile)) {
3912                 BNXT_TF_DBG(ERR, "regfile initialization failed.\n");
3913                 return -EINVAL;
3914         }
3915
3916         /* Process the action template list from the selected action table*/
3917         if (parms.act_tid) {
3918                 parms.tmpl_type = BNXT_ULP_TEMPLATE_TYPE_ACTION;
3919                 /* Process the action template tables */
3920                 rc = ulp_mapper_tbls_process(&parms, parms.act_tid);
3921                 if (rc)
3922                         goto flow_error;
3923                 cparms->shared_hndl = parms.shared_hndl;
3924         }
3925
3926         if (parms.class_tid) {
3927                 parms.tmpl_type = BNXT_ULP_TEMPLATE_TYPE_CLASS;
3928
3929                 /* Process the class template tables.*/
3930                 rc = ulp_mapper_tbls_process(&parms, parms.class_tid);
3931                 if (rc)
3932                         goto flow_error;
3933         }
3934
3935         /* setup the parent-child details */
3936         if (parms.parent_flow) {
3937                 /* create a parent flow details */
3938                 rc = ulp_flow_db_parent_flow_create(&parms);
3939                 if (rc)
3940                         goto flow_error;
3941         } else if (parms.child_flow) {
3942                 /* create a child flow details */
3943                 rc = ulp_flow_db_child_flow_create(&parms);
3944                 if (rc)
3945                         goto flow_error;
3946         }
3947
3948         return rc;
3949
3950 flow_error:
3951         /* Free all resources that were allocated during flow creation */
3952         trc = ulp_mapper_flow_destroy(ulp_ctx, parms.flow_type,
3953                                       parms.fid);
3954         if (trc)
3955                 BNXT_TF_DBG(ERR, "Failed to free all resources rc=%d\n", trc);
3956
3957         return rc;
3958 }
3959
3960 int32_t
3961 ulp_mapper_init(struct bnxt_ulp_context *ulp_ctx)
3962 {
3963         struct bnxt_ulp_mapper_data *data;
3964         struct tf *tfp;
3965         int32_t rc;
3966
3967         if (!ulp_ctx)
3968                 return -EINVAL;
3969
3970         tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx, BNXT_ULP_SHARED_SESSION_NO);
3971         if (!tfp)
3972                 return -EINVAL;
3973
3974         data = rte_zmalloc("ulp_mapper_data",
3975                            sizeof(struct bnxt_ulp_mapper_data), 0);
3976         if (!data) {
3977                 BNXT_TF_DBG(ERR, "Failed to allocate the mapper data\n");
3978                 return -ENOMEM;
3979         }
3980
3981         if (bnxt_ulp_cntxt_ptr2_mapper_data_set(ulp_ctx, data)) {
3982                 BNXT_TF_DBG(ERR, "Failed to set mapper data in context\n");
3983                 /* Don't call deinit since the prof_func wasn't allocated. */
3984                 rte_free(data);
3985                 return -ENOMEM;
3986         }
3987
3988         /* Allocate the global resource ids */
3989         rc = ulp_mapper_glb_resource_info_init(ulp_ctx, data);
3990         if (rc) {
3991                 BNXT_TF_DBG(ERR, "Failed to initialize global resource ids\n");
3992                 goto error;
3993         }
3994
3995         /*
3996          * Only initialize the app global resources if a shared session was
3997          * created.
3998          */
3999         if (bnxt_ulp_cntxt_shared_session_enabled(ulp_ctx)) {
4000                 rc = ulp_mapper_app_glb_resource_info_init(ulp_ctx, data);
4001                 if (rc) {
4002                         BNXT_TF_DBG(ERR, "Failed to init app glb resources\n");
4003                         goto error;
4004                 }
4005         }
4006
4007         /* Allocate the generic table list */
4008         rc = ulp_mapper_generic_tbl_list_init(data);
4009         if (rc) {
4010                 BNXT_TF_DBG(ERR, "Failed to initialize generic tbl list\n");
4011                 goto error;
4012         }
4013
4014         return 0;
4015 error:
4016         /* Ignore the return code in favor of returning the original error. */
4017         ulp_mapper_deinit(ulp_ctx);
4018         return rc;
4019 }
4020
4021 void
4022 ulp_mapper_deinit(struct bnxt_ulp_context *ulp_ctx)
4023 {
4024         struct bnxt_ulp_mapper_data *data;
4025         struct tf *tfp;
4026
4027         if (!ulp_ctx) {
4028                 BNXT_TF_DBG(ERR,
4029                             "Failed to acquire ulp context, so data may not be released.\n");
4030                 return;
4031         }
4032
4033         data = (struct bnxt_ulp_mapper_data *)
4034                 bnxt_ulp_cntxt_ptr2_mapper_data_get(ulp_ctx);
4035         if (!data) {
4036                 /* Go ahead and return since there is no allocated data. */
4037                 BNXT_TF_DBG(ERR, "No data appears to have been allocated.\n");
4038                 return;
4039         }
4040
4041         tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx, BNXT_ULP_SHARED_SESSION_NO);
4042         if (!tfp) {
4043                 BNXT_TF_DBG(ERR, "Failed to acquire tfp.\n");
4044                 /* Free the mapper data regardless of errors. */
4045                 goto free_mapper_data;
4046         }
4047
4048         /* Free the global resource info table entries */
4049         ulp_mapper_glb_resource_info_deinit(ulp_ctx, data);
4050
4051 free_mapper_data:
4052         /* Free the generic table */
4053         (void)ulp_mapper_generic_tbl_list_deinit(data);
4054
4055         rte_free(data);
4056         /* Reset the data pointer within the ulp_ctx. */
4057         bnxt_ulp_cntxt_ptr2_mapper_data_set(ulp_ctx, NULL);
4058 }