net/bnxt: support dynamic encap action
[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(uint64_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                 break;
1219         case BNXT_ULP_FIELD_SRC_ENC_HDR_BIT:
1220                 if (!ulp_operand_read(field_opr,
1221                                       (uint8_t *)&lregval, sizeof(uint64_t))) {
1222                         BNXT_TF_DBG(ERR, "Header bit read failed\n");
1223                         return -EINVAL;
1224                 }
1225                 lregval = tfp_be_to_cpu_64(lregval);
1226                 if (ULP_BITMAP_ISSET(parms->enc_hdr_bitmap->bits, lregval)) {
1227                         *val = mapper_fld_one;
1228                         *value = 1;
1229                 } else {
1230                         *val = mapper_fld_zeros;
1231                 }
1232                 break;
1233         case BNXT_ULP_FIELD_SRC_ENC_FIELD:
1234                 if (!ulp_operand_read(field_opr,
1235                                       (uint8_t *)&idx, sizeof(uint16_t))) {
1236                         BNXT_TF_DBG(ERR, "Header field read failed\n");
1237                         return -EINVAL;
1238                 }
1239                 idx = tfp_be_to_cpu_16(idx);
1240                 /* get the index from the global field list */
1241                 if (idx >= BNXT_ULP_ENC_FIELD_LAST) {
1242                         BNXT_TF_DBG(ERR, "invalid encap field tbl idx %d\n",
1243                                     idx);
1244                         return -EINVAL;
1245                 }
1246                 buffer = parms->enc_field[idx].spec;
1247                 field_size = parms->enc_field[idx].size;
1248                 if (bytelen > field_size) {
1249                         BNXT_TF_DBG(ERR, "Encap field[%d] size small %u\n",
1250                                     idx, field_size);
1251                         return -EINVAL;
1252                 }
1253                 *val = &buffer[field_size - bytelen];
1254                 break;
1255         case BNXT_ULP_FIELD_SRC_SKIP:
1256                 /* do nothing */
1257                 *val = mapper_fld_zeros;
1258                 *val_len = 0;
1259                 break;
1260         case BNXT_ULP_FIELD_SRC_REJECT:
1261                 return -EINVAL;
1262         default:
1263                 BNXT_TF_DBG(ERR, "invalid field opcode 0x%x\n", field_src);
1264                 return -EINVAL;
1265         }
1266         return 0;
1267 }
1268
1269 static int32_t ulp_mapper_field_buffer_eval(uint8_t *buffer, uint32_t bitlen,
1270                                             uint64_t *output)
1271 {
1272         uint16_t val_16;
1273         uint32_t val_32;
1274         uint64_t val_64;
1275         uint32_t bytelen;
1276
1277         bytelen = ULP_BITS_2_BYTE(bitlen);
1278         if (bytelen == sizeof(uint8_t)) {
1279                 *output = *((uint8_t *)buffer);
1280         } else if (bytelen == sizeof(uint16_t)) {
1281                 val_16 = *((uint16_t *)buffer);
1282                 *output =  tfp_be_to_cpu_16(val_16);
1283         } else if (bytelen == sizeof(uint32_t)) {
1284                 val_32 = *((uint32_t *)buffer);
1285                 *output =  tfp_be_to_cpu_32(val_32);
1286         } else if (bytelen == sizeof(val_64)) {
1287                 val_64 = *((uint64_t *)buffer);
1288                 *output =  tfp_be_to_cpu_64(val_64);
1289         } else {
1290                 *output = 0;
1291                 return -EINVAL;
1292         }
1293         return 0;
1294 }
1295
1296 static int32_t ulp_mapper_field_blob_write(enum bnxt_ulp_field_src fld_src,
1297                                            struct ulp_blob *blob,
1298                                            uint8_t *val,
1299                                            uint32_t val_len,
1300                                            uint8_t **out_val)
1301 {
1302         if (fld_src == BNXT_ULP_FIELD_SRC_ZERO) {
1303                 if (ulp_blob_pad_push(blob, val_len) < 0) {
1304                         BNXT_TF_DBG(ERR, "too large for blob\n");
1305                         return -EINVAL;
1306                 }
1307         } else if (fld_src == BNXT_ULP_FIELD_SRC_ACT_PROP_SZ) {
1308                 if (ulp_blob_push_encap(blob, val, val_len) < 0) {
1309                         BNXT_TF_DBG(ERR, "encap blob push failed\n");
1310                         return -EINVAL;
1311                 }
1312         } else if (fld_src == BNXT_ULP_FIELD_SRC_SKIP) {
1313                 /* do nothing */
1314         } else {
1315                 if (!ulp_blob_push(blob, val, val_len)) {
1316                         BNXT_TF_DBG(ERR, "push of val1 failed\n");
1317                         return -EINVAL;
1318                 }
1319         }
1320         *out_val = val;
1321         return 0;
1322 }
1323
1324 static int32_t
1325 ulp_mapper_field_opc_process(struct bnxt_ulp_mapper_parms *parms,
1326                              enum tf_dir dir,
1327                              struct bnxt_ulp_mapper_field_info *fld,
1328                              struct ulp_blob *blob,
1329                              uint8_t is_key,
1330                              const char *name)
1331 {
1332         uint16_t write_idx = blob->write_idx;
1333         uint8_t *val = NULL, *val1, *val2, *val3;
1334         uint32_t val_len = 0, val1_len = 0, val2_len = 0, val3_len = 0;
1335         uint8_t process_src1 = 0, process_src2 = 0, process_src3 = 0;
1336         uint8_t eval_src1 = 0, eval_src2 = 0, eval_src3 = 0;
1337         uint64_t val_int = 0, val1_int = 0, val2_int = 0, val3_int = 0;
1338         uint64_t value1 = 0, value2 = 0, value3 = 0;
1339         int32_t rc = 0;
1340
1341         /* prepare the field source and values */
1342         switch (fld->field_opc) {
1343         case BNXT_ULP_FIELD_OPC_SRC1:
1344                 process_src1 = 1;
1345                 break;
1346         case BNXT_ULP_FIELD_OPC_SRC1_THEN_SRC2_ELSE_SRC3:
1347                 process_src1 = 1;
1348                 break;
1349         case BNXT_ULP_FIELD_OPC_SRC1_OR_SRC2_OR_SRC3:
1350         case BNXT_ULP_FIELD_OPC_SRC1_AND_SRC2_OR_SRC3:
1351                 process_src3 = 1;
1352                 eval_src3 = 1;
1353                 process_src1 = 1;
1354                 process_src2 = 1;
1355                 eval_src1 = 1;
1356                 eval_src2 = 1;
1357                 break;
1358         case BNXT_ULP_FIELD_OPC_SRC1_PLUS_SRC2:
1359         case BNXT_ULP_FIELD_OPC_SRC1_MINUS_SRC2:
1360         case BNXT_ULP_FIELD_OPC_SRC1_PLUS_SRC2_POST:
1361         case BNXT_ULP_FIELD_OPC_SRC1_MINUS_SRC2_POST:
1362         case BNXT_ULP_FIELD_OPC_SRC1_OR_SRC2:
1363         case BNXT_ULP_FIELD_OPC_SRC1_AND_SRC2:
1364                 process_src1 = 1;
1365                 process_src2 = 1;
1366                 eval_src1 = 1;
1367                 eval_src2 = 1;
1368                 break;
1369         default:
1370                 break;
1371         }
1372
1373         /* process the src1 opcode  */
1374         if (process_src1) {
1375                 if (ulp_mapper_field_src_process(parms, fld->field_src1,
1376                                                  fld->field_opr1, dir, is_key,
1377                                                  fld->field_bit_size, &val1,
1378                                                  &val1_len, &value1)) {
1379                         BNXT_TF_DBG(ERR, "fld src1 process failed\n");
1380                         goto error;
1381                 }
1382                 if (eval_src1) {
1383                         if (ulp_mapper_field_buffer_eval(val1, val1_len,
1384                                                          &val1_int)) {
1385                                 BNXT_TF_DBG(ERR, "fld src1 eval failed\n");
1386                                 goto error;
1387                         }
1388                 }
1389         }
1390
1391         /* for "if then clause" set the correct process  */
1392         if (fld->field_opc == BNXT_ULP_FIELD_OPC_SRC1_THEN_SRC2_ELSE_SRC3) {
1393                 if (value1)
1394                         process_src2 = 1;
1395                 else
1396                         process_src3 = 1;
1397         }
1398
1399         /* process src2 opcode */
1400         if (process_src2) {
1401                 if (ulp_mapper_field_src_process(parms, fld->field_src2,
1402                                                  fld->field_opr2, dir, is_key,
1403                                                  fld->field_bit_size, &val2,
1404                                                  &val2_len, &value2)) {
1405                         BNXT_TF_DBG(ERR, "fld src2 process failed\n");
1406                         goto error;
1407                 }
1408                 if (eval_src2) {
1409                         if (ulp_mapper_field_buffer_eval(val2, val2_len,
1410                                                          &val2_int)) {
1411                                 BNXT_TF_DBG(ERR, "fld src2 eval failed\n");
1412                                 goto error;
1413                         }
1414                 }
1415         }
1416
1417         /* process src3 opcode */
1418         if (process_src3) {
1419                 if (ulp_mapper_field_src_process(parms, fld->field_src3,
1420                                                  fld->field_opr3, dir, is_key,
1421                                                  fld->field_bit_size, &val3,
1422                                                  &val3_len, &value3)) {
1423                         BNXT_TF_DBG(ERR, "fld src3 process failed\n");
1424                         goto error;
1425                 }
1426                 if (eval_src3) {
1427                         if (ulp_mapper_field_buffer_eval(val3, val3_len,
1428                                                          &val3_int)) {
1429                                 BNXT_TF_DBG(ERR, "fld src3 eval failed\n");
1430                                 goto error;
1431                         }
1432                 }
1433         }
1434
1435         val_len = fld->field_bit_size;
1436         /* process the field opcodes */
1437         switch (fld->field_opc) {
1438         case BNXT_ULP_FIELD_OPC_SRC1:
1439                 rc = ulp_mapper_field_blob_write(fld->field_src1,
1440                                                  blob, val1, val1_len, &val);
1441                 val_len = val1_len;
1442                 break;
1443         case BNXT_ULP_FIELD_OPC_SRC1_THEN_SRC2_ELSE_SRC3:
1444                 if (value1) {
1445                         rc = ulp_mapper_field_blob_write(fld->field_src2, blob,
1446                                                          val2, val2_len, &val);
1447                         val_len = val2_len;
1448                 } else {
1449                         rc = ulp_mapper_field_blob_write(fld->field_src3, blob,
1450                                                          val3, val3_len, &val);
1451                         val_len = val3_len;
1452                 }
1453                 break;
1454         case BNXT_ULP_FIELD_OPC_SRC1_PLUS_SRC2:
1455         case BNXT_ULP_FIELD_OPC_SRC1_PLUS_SRC2_POST:
1456                 val_int = val1_int + val2_int;
1457                 val_int = tfp_cpu_to_be_64(val_int);
1458                 val = ulp_blob_push_64(blob, &val_int, fld->field_bit_size);
1459                 if (!val)
1460                         rc = -EINVAL;
1461                 break;
1462         case BNXT_ULP_FIELD_OPC_SRC1_MINUS_SRC2:
1463         case BNXT_ULP_FIELD_OPC_SRC1_MINUS_SRC2_POST:
1464                 val_int = val1_int - val2_int;
1465                 val_int = tfp_cpu_to_be_64(val_int);
1466                 val = ulp_blob_push_64(blob, &val_int, fld->field_bit_size);
1467                 if (!val)
1468                         rc = -EINVAL;
1469                 break;
1470         case BNXT_ULP_FIELD_OPC_SRC1_OR_SRC2:
1471                 val_int = val1_int | val2_int;
1472                 val_int = tfp_cpu_to_be_64(val_int);
1473                 val = ulp_blob_push_64(blob, &val_int, fld->field_bit_size);
1474                 if (!val)
1475                         rc = -EINVAL;
1476                 break;
1477         case BNXT_ULP_FIELD_OPC_SRC1_OR_SRC2_OR_SRC3:
1478                 val_int = val1_int | val2_int | val3_int;
1479                 val_int = tfp_cpu_to_be_64(val_int);
1480                 val = ulp_blob_push_64(blob, &val_int, fld->field_bit_size);
1481                 if (!val)
1482                         rc = -EINVAL;
1483                 break;
1484         case BNXT_ULP_FIELD_OPC_SRC1_AND_SRC2:
1485                 val_int = val1_int & val2_int;
1486                 val_int = tfp_cpu_to_be_64(val_int);
1487                 val = ulp_blob_push_64(blob, &val_int, fld->field_bit_size);
1488                 if (!val)
1489                         rc = -EINVAL;
1490                 break;
1491         case BNXT_ULP_FIELD_OPC_SRC1_AND_SRC2_OR_SRC3:
1492                 val_int = val1_int & (val2_int | val3_int);
1493                 val_int = tfp_cpu_to_be_64(val_int);
1494                 val = ulp_blob_push_64(blob, &val_int, fld->field_bit_size);
1495                 if (!val)
1496                         rc = -EINVAL;
1497                 break;
1498         case BNXT_ULP_FIELD_OPC_SKIP:
1499                 break;
1500         default:
1501                 BNXT_TF_DBG(ERR, "Invalid fld opcode %u\n", fld->field_opc);
1502                 rc = -EINVAL;
1503                 break;
1504         }
1505
1506         if (!rc) {
1507 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG
1508 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG_MAPPER
1509                 if (fld->field_src1 != BNXT_ULP_FIELD_SRC_ZERO && val_len)
1510                         ulp_mapper_field_dump(name, fld, blob, write_idx, val,
1511                                               val_len);
1512 #endif
1513 #endif
1514                 return rc;
1515         }
1516 error:
1517         BNXT_TF_DBG(ERR, "Error in %s:%s process %u:%u\n", name,
1518                     fld->description, (val) ? write_idx : 0, val_len);
1519         return -EINVAL;
1520 }
1521
1522 /*
1523  * Result table process and fill the result blob.
1524  * data [out] - the result blob data
1525  */
1526 static int32_t
1527 ulp_mapper_tbl_result_build(struct bnxt_ulp_mapper_parms *parms,
1528                             struct bnxt_ulp_mapper_tbl_info *tbl,
1529                             struct ulp_blob *data,
1530                             const char *name)
1531 {
1532         struct bnxt_ulp_mapper_field_info *dflds;
1533         uint32_t i = 0, num_flds = 0, encap_flds = 0;
1534         struct ulp_blob encap_blob;
1535         int32_t rc = 0;
1536
1537         /* Get the result field list */
1538         dflds = ulp_mapper_result_fields_get(parms, tbl, &num_flds,
1539                                              &encap_flds);
1540
1541         /* validate the result field list counts */
1542         if ((tbl->resource_func == BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE &&
1543              (!num_flds && !encap_flds)) || !dflds ||
1544             (tbl->resource_func != BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE &&
1545                 (!num_flds || encap_flds))) {
1546                 BNXT_TF_DBG(ERR, "Failed to get data fields %x:%x\n",
1547                             num_flds, encap_flds);
1548                 return -EINVAL;
1549         }
1550
1551         /* process the result fields */
1552         for (i = 0; i < num_flds; i++) {
1553                 rc = ulp_mapper_field_opc_process(parms, tbl->direction,
1554                                                   &dflds[i], data, 0, name);
1555                 if (rc) {
1556                         BNXT_TF_DBG(ERR, "result field processing failed\n");
1557                         return rc;
1558                 }
1559         }
1560
1561         /* process encap fields if any */
1562         if (encap_flds) {
1563                 uint32_t pad = 0;
1564                 /* Initialize the encap blob */
1565                 if (!tbl->record_size) {
1566                         BNXT_TF_DBG(ERR, "Encap tbl record size incorrect\n");
1567                         return -EINVAL;
1568                 }
1569                 if (!ulp_blob_init(&encap_blob,
1570                                    ULP_BYTE_2_BITS(tbl->record_size),
1571                                    parms->device_params->encap_byte_order)) {
1572                         BNXT_TF_DBG(ERR, "blob inits failed.\n");
1573                         return -EINVAL;
1574                 }
1575                 for (; i < encap_flds; i++) {
1576                         rc = ulp_mapper_field_opc_process(parms, tbl->direction,
1577                                                           &dflds[i],
1578                                                           &encap_blob, 0, name);
1579                         if (rc) {
1580                                 BNXT_TF_DBG(ERR,
1581                                             "encap field processing failed\n");
1582                                 return rc;
1583                         }
1584                 }
1585                 /* add the dynamic pad push */
1586                 pad = ULP_BYTE_2_BITS(tbl->record_size) -
1587                         ulp_blob_data_len_get(&encap_blob);
1588                 ulp_blob_pad_push(&encap_blob, pad);
1589
1590                 /* perform the 64 bit byte swap */
1591                 ulp_blob_perform_64B_byte_swap(&encap_blob);
1592                 /* Append encap blob to the result blob */
1593                 rc = ulp_blob_buffer_copy(data, &encap_blob);
1594                 if (rc) {
1595                         BNXT_TF_DBG(ERR, "encap buffer copy failed\n");
1596                         return rc;
1597                 }
1598         }
1599 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG
1600 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG_MAPPER
1601         BNXT_TF_DBG(DEBUG, "Result dump\n");
1602         ulp_mapper_blob_dump(data);
1603 #endif
1604 #endif
1605         return rc;
1606 }
1607
1608 static int32_t
1609 ulp_mapper_mark_gfid_process(struct bnxt_ulp_mapper_parms *parms,
1610                              struct bnxt_ulp_mapper_tbl_info *tbl,
1611                              uint64_t flow_id)
1612 {
1613         struct ulp_flow_db_res_params fid_parms;
1614         uint32_t mark, gfid, mark_flag;
1615         enum bnxt_ulp_mark_db_opc mark_op = tbl->mark_db_opcode;
1616         int32_t rc = 0;
1617
1618         if (mark_op == BNXT_ULP_MARK_DB_OPC_NOP ||
1619             !(mark_op == BNXT_ULP_MARK_DB_OPC_PUSH_IF_MARK_ACTION &&
1620              ULP_BITMAP_ISSET(parms->act_bitmap->bits,
1621                               BNXT_ULP_ACT_BIT_MARK)))
1622                 return rc; /* no need to perform gfid process */
1623
1624         /* Get the mark id details from action property */
1625         memcpy(&mark, &parms->act_prop->act_details[BNXT_ULP_ACT_PROP_IDX_MARK],
1626                sizeof(mark));
1627         mark = tfp_be_to_cpu_32(mark);
1628
1629         TF_GET_GFID_FROM_FLOW_ID(flow_id, gfid);
1630         mark_flag  = BNXT_ULP_MARK_GLOBAL_HW_FID;
1631
1632         rc = ulp_mark_db_mark_add(parms->ulp_ctx, mark_flag,
1633                                   gfid, mark);
1634         if (rc) {
1635                 BNXT_TF_DBG(ERR, "Failed to add mark to flow\n");
1636                 return rc;
1637         }
1638         fid_parms.direction = tbl->direction;
1639         fid_parms.resource_func = BNXT_ULP_RESOURCE_FUNC_HW_FID;
1640         fid_parms.critical_resource = tbl->critical_resource;
1641         fid_parms.resource_type = mark_flag;
1642         fid_parms.resource_hndl = gfid;
1643         ulp_flow_db_shared_session_set(&fid_parms, tbl->shared_session);
1644
1645         rc = ulp_mapper_fdb_opc_process(parms, tbl, &fid_parms);
1646         if (rc)
1647                 BNXT_TF_DBG(ERR, "Fail to link res to flow rc = %d\n", rc);
1648         return rc;
1649 }
1650
1651 static int32_t
1652 ulp_mapper_mark_act_ptr_process(struct bnxt_ulp_mapper_parms *parms,
1653                                 struct bnxt_ulp_mapper_tbl_info *tbl)
1654 {
1655         struct ulp_flow_db_res_params fid_parms;
1656         uint32_t act_idx, mark, mark_flag;
1657         uint64_t val64;
1658         enum bnxt_ulp_mark_db_opc mark_op = tbl->mark_db_opcode;
1659         int32_t rc = 0;
1660
1661         if (mark_op == BNXT_ULP_MARK_DB_OPC_NOP ||
1662             !(mark_op == BNXT_ULP_MARK_DB_OPC_PUSH_IF_MARK_ACTION &&
1663              ULP_BITMAP_ISSET(parms->act_bitmap->bits,
1664                               BNXT_ULP_ACT_BIT_MARK)))
1665                 return rc; /* no need to perform mark action process */
1666
1667         /* Get the mark id details from action property */
1668         memcpy(&mark, &parms->act_prop->act_details[BNXT_ULP_ACT_PROP_IDX_MARK],
1669                sizeof(mark));
1670         mark = tfp_be_to_cpu_32(mark);
1671
1672         if (!ulp_regfile_read(parms->regfile,
1673                               BNXT_ULP_RF_IDX_MAIN_ACTION_PTR,
1674                               &val64)) {
1675                 BNXT_TF_DBG(ERR, "read action ptr main failed\n");
1676                 return -EINVAL;
1677         }
1678         act_idx = tfp_be_to_cpu_64(val64);
1679         mark_flag  = BNXT_ULP_MARK_LOCAL_HW_FID;
1680         rc = ulp_mark_db_mark_add(parms->ulp_ctx, mark_flag,
1681                                   act_idx, mark);
1682         if (rc) {
1683                 BNXT_TF_DBG(ERR, "Failed to add mark to flow\n");
1684                 return rc;
1685         }
1686         fid_parms.direction = tbl->direction;
1687         fid_parms.resource_func = BNXT_ULP_RESOURCE_FUNC_HW_FID;
1688         fid_parms.critical_resource = tbl->critical_resource;
1689         fid_parms.resource_type = mark_flag;
1690         fid_parms.resource_hndl = act_idx;
1691         ulp_flow_db_shared_session_set(&fid_parms, tbl->shared_session);
1692
1693         rc = ulp_mapper_fdb_opc_process(parms, tbl, &fid_parms);
1694         if (rc)
1695                 BNXT_TF_DBG(ERR, "Fail to link res to flow rc = %d\n", rc);
1696         return rc;
1697 }
1698
1699 static int32_t
1700 ulp_mapper_mark_vfr_idx_process(struct bnxt_ulp_mapper_parms *parms,
1701                                 struct bnxt_ulp_mapper_tbl_info *tbl)
1702 {
1703         struct ulp_flow_db_res_params fid_parms;
1704         uint32_t act_idx, mark, mark_flag;
1705         uint64_t val64;
1706         enum bnxt_ulp_mark_db_opc mark_op = tbl->mark_db_opcode;
1707         int32_t rc = 0;
1708
1709         if (mark_op == BNXT_ULP_MARK_DB_OPC_NOP ||
1710             mark_op == BNXT_ULP_MARK_DB_OPC_PUSH_IF_MARK_ACTION)
1711                 return rc; /* no need to perform mark action process */
1712
1713         /* Get the mark id details from the computed field of dev port id */
1714         mark = ULP_COMP_FLD_IDX_RD(parms, BNXT_ULP_CF_IDX_DEV_PORT_ID);
1715
1716          /* Get the main action pointer */
1717         if (!ulp_regfile_read(parms->regfile,
1718                               BNXT_ULP_RF_IDX_MAIN_ACTION_PTR,
1719                               &val64)) {
1720                 BNXT_TF_DBG(ERR, "read action ptr main failed\n");
1721                 return -EINVAL;
1722         }
1723         act_idx = tfp_be_to_cpu_64(val64);
1724
1725         /* Set the mark flag to local fid and vfr flag */
1726         mark_flag  = BNXT_ULP_MARK_LOCAL_HW_FID | BNXT_ULP_MARK_VFR_ID;
1727
1728         rc = ulp_mark_db_mark_add(parms->ulp_ctx, mark_flag,
1729                                   act_idx, mark);
1730         if (rc) {
1731                 BNXT_TF_DBG(ERR, "Failed to add mark to flow\n");
1732                 return rc;
1733         }
1734         fid_parms.direction = tbl->direction;
1735         fid_parms.resource_func = BNXT_ULP_RESOURCE_FUNC_HW_FID;
1736         fid_parms.critical_resource = tbl->critical_resource;
1737         fid_parms.resource_type = mark_flag;
1738         fid_parms.resource_hndl = act_idx;
1739         ulp_flow_db_shared_session_set(&fid_parms, tbl->shared_session);
1740
1741         rc = ulp_mapper_fdb_opc_process(parms, tbl, &fid_parms);
1742         if (rc)
1743                 BNXT_TF_DBG(ERR, "Fail to link res to flow rc = %d\n", rc);
1744         return rc;
1745 }
1746
1747 /* Tcam table scan the identifier list and allocate each identifier */
1748 static int32_t
1749 ulp_mapper_tcam_tbl_scan_ident_alloc(struct bnxt_ulp_mapper_parms *parms,
1750                                      struct bnxt_ulp_mapper_tbl_info *tbl)
1751 {
1752         struct bnxt_ulp_mapper_ident_info *idents;
1753         uint32_t num_idents;
1754         uint32_t i;
1755
1756         idents = ulp_mapper_ident_fields_get(parms, tbl, &num_idents);
1757         for (i = 0; i < num_idents; i++) {
1758                 if (ulp_mapper_ident_process(parms, tbl,
1759                                              &idents[i], NULL))
1760                         return -EINVAL;
1761         }
1762         return 0;
1763 }
1764
1765 /*
1766  * Tcam table scan the identifier list and extract the identifier from
1767  * the result blob.
1768  */
1769 static int32_t
1770 ulp_mapper_tcam_tbl_scan_ident_extract(struct bnxt_ulp_mapper_parms *parms,
1771                                        struct bnxt_ulp_mapper_tbl_info *tbl,
1772                                        struct ulp_blob *data)
1773 {
1774         struct bnxt_ulp_mapper_ident_info *idents;
1775         uint32_t num_idents = 0, i;
1776         int32_t rc = 0;
1777
1778         /*
1779          * Extract the listed identifiers from the result field,
1780          * no need to allocate them.
1781          */
1782         idents = ulp_mapper_ident_fields_get(parms, tbl, &num_idents);
1783         for (i = 0; i < num_idents; i++) {
1784                 rc = ulp_mapper_ident_extract(parms, tbl, &idents[i], data);
1785                 if (rc) {
1786                         BNXT_TF_DBG(ERR, "Error in identifier extraction\n");
1787                         return rc;
1788                 }
1789         }
1790         return rc;
1791 }
1792
1793 /* Internal function to write the tcam entry */
1794 static int32_t
1795 ulp_mapper_tcam_tbl_entry_write(struct bnxt_ulp_mapper_parms *parms,
1796                                 struct bnxt_ulp_mapper_tbl_info *tbl,
1797                                 struct ulp_blob *key,
1798                                 struct ulp_blob *mask,
1799                                 struct ulp_blob *data,
1800                                 uint16_t idx)
1801 {
1802         struct tf_set_tcam_entry_parms sparms = { 0 };
1803         struct tf *tfp;
1804         uint16_t tmplen;
1805         int32_t rc;
1806
1807         tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx, tbl->shared_session);
1808         if (!tfp) {
1809                 BNXT_TF_DBG(ERR, "Failed to get truflow pointer\n");
1810                 return -EINVAL;
1811         }
1812
1813         sparms.dir              = tbl->direction;
1814         sparms.tcam_tbl_type    = tbl->resource_type;
1815         sparms.idx              = idx;
1816         sparms.key              = ulp_blob_data_get(key, &tmplen);
1817         sparms.key_sz_in_bits   = tmplen;
1818         sparms.mask             = ulp_blob_data_get(mask, &tmplen);
1819         sparms.result           = ulp_blob_data_get(data, &tmplen);
1820         sparms.result_sz_in_bits = tmplen;
1821         if (tf_set_tcam_entry(tfp, &sparms)) {
1822                 BNXT_TF_DBG(ERR, "tcam[%s][%s][%x] write failed.\n",
1823                             tf_tcam_tbl_2_str(sparms.tcam_tbl_type),
1824                             tf_dir_2_str(sparms.dir), sparms.idx);
1825                 return -EIO;
1826         }
1827         BNXT_TF_INF("tcam[%s][%s][%x] write success.\n",
1828                     tf_tcam_tbl_2_str(sparms.tcam_tbl_type),
1829                     tf_dir_2_str(sparms.dir), sparms.idx);
1830
1831         /* Mark action */
1832         rc = ulp_mapper_mark_act_ptr_process(parms, tbl);
1833         if (rc) {
1834                 BNXT_TF_DBG(ERR, "failed mark action processing\n");
1835                 return rc;
1836         }
1837
1838 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG
1839 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG_MAPPER
1840         ulp_mapper_tcam_entry_dump("TCAM", idx, tbl, key, mask, data);
1841 #endif
1842 #endif
1843         return rc;
1844 }
1845
1846 /*
1847  * internal function to post process key/mask blobs for dynamic pad WC tcam tbl
1848  *
1849  * parms [in] The mappers parms with data related to the flow.
1850  *
1851  * key [in] The original key to be transformed
1852  *
1853  * mask [in] The original mask to be transformed
1854  *
1855  * tkey [in/out] The transformed key
1856  *
1857  * tmask [in/out] The transformed mask
1858  *
1859  * returns zero on success, non-zero on failure
1860  */
1861 static uint32_t
1862 ulp_mapper_wc_tcam_tbl_dyn_post_process(struct bnxt_ulp_device_params *dparms,
1863                                         struct ulp_blob *key,
1864                                         struct ulp_blob *mask,
1865                                         struct ulp_blob *tkey,
1866                                         struct ulp_blob *tmask)
1867 {
1868         uint16_t tlen, blen, clen, slice_width, num_slices, max_slices, offset;
1869         uint32_t cword, i, rc;
1870         int32_t pad;
1871         uint8_t *val;
1872
1873         slice_width = dparms->wc_slice_width;
1874         clen = dparms->wc_ctl_size_bits;
1875         max_slices = dparms->wc_max_slices;
1876         blen = ulp_blob_data_len_get(key);
1877
1878         /* Get the length of the key based on number of slices and width */
1879         num_slices = 1;
1880         tlen = slice_width;
1881         while (tlen < blen &&
1882                num_slices <= max_slices) {
1883                 num_slices = num_slices << 1;
1884                 tlen = tlen << 1;
1885         }
1886
1887         if (num_slices > max_slices) {
1888                 BNXT_TF_DBG(ERR, "Key size (%d) too large for WC\n", blen);
1889                 return -EINVAL;
1890         }
1891
1892         /* The key/mask may not be on a natural slice boundary, pad it */
1893         pad = tlen - blen;
1894         if (ulp_blob_pad_push(key, pad) < 0 ||
1895             ulp_blob_pad_push(mask, pad) < 0) {
1896                 BNXT_TF_DBG(ERR, "Unable to pad key/mask\n");
1897                 return -EINVAL;
1898         }
1899
1900         /* The new length accounts for the ctrl word length and num slices */
1901         tlen = tlen + clen * num_slices;
1902         if (!ulp_blob_init(tkey, tlen, key->byte_order) ||
1903             !ulp_blob_init(tmask, tlen, mask->byte_order)) {
1904                 BNXT_TF_DBG(ERR, "Unable to post process wc tcam entry\n");
1905                 return -EINVAL;
1906         }
1907
1908         /* Build the transformed key/mask */
1909         cword = dparms->wc_mode_list[num_slices - 1];
1910         cword = tfp_cpu_to_be_32(cword);
1911         offset = 0;
1912         for (i = 0; i < num_slices; i++) {
1913                 val = ulp_blob_push_32(tkey, &cword, clen);
1914                 if (!val) {
1915                         BNXT_TF_DBG(ERR, "Key ctrl word push failed\n");
1916                         return -EINVAL;
1917                 }
1918                 val = ulp_blob_push_32(tmask, &cword, clen);
1919                 if (!val) {
1920                         BNXT_TF_DBG(ERR, "Mask ctrl word push failed\n");
1921                         return -EINVAL;
1922                 }
1923                 rc = ulp_blob_append(tkey, key, offset, slice_width);
1924                 if (rc) {
1925                         BNXT_TF_DBG(ERR, "Key blob append failed\n");
1926                         return rc;
1927                 }
1928                 rc = ulp_blob_append(tmask, mask, offset, slice_width);
1929                 if (rc) {
1930                         BNXT_TF_DBG(ERR, "Mask blob append failed\n");
1931                         return rc;
1932                 }
1933                 offset += slice_width;
1934         }
1935
1936         /* The key/mask are byte reversed on every 4 byte chunk */
1937         ulp_blob_perform_byte_reverse(tkey, 4);
1938         ulp_blob_perform_byte_reverse(tmask, 4);
1939
1940         return 0;
1941 }
1942
1943 /* internal function to post process the key/mask blobs for wildcard tcam tbl */
1944 static void ulp_mapper_wc_tcam_tbl_post_process(struct ulp_blob *blob)
1945 {
1946         ulp_blob_perform_64B_word_swap(blob);
1947         ulp_blob_perform_64B_byte_swap(blob);
1948 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG
1949 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG_MAPPER
1950         BNXT_TF_DBG(INFO, "Dump after wc tcam post process\n");
1951         ulp_mapper_blob_dump(blob);
1952 #endif
1953 #endif
1954 }
1955
1956 static int32_t
1957 ulp_mapper_tcam_tbl_process(struct bnxt_ulp_mapper_parms *parms,
1958                             struct bnxt_ulp_mapper_tbl_info *tbl)
1959 {
1960         struct bnxt_ulp_mapper_key_info *kflds;
1961         struct ulp_blob okey, omask, data, update_data;
1962         struct ulp_blob tkey, tmask; /* transform key and mask */
1963         struct ulp_blob *key, *mask;
1964         uint32_t i, num_kflds;
1965         struct tf *tfp;
1966         int32_t rc, trc;
1967         struct bnxt_ulp_device_params *dparms = parms->device_params;
1968         struct tf_alloc_tcam_entry_parms aparms         = { 0 };
1969         struct tf_search_tcam_entry_parms searchparms   = { 0 };
1970         struct ulp_flow_db_res_params   fid_parms       = { 0 };
1971         struct tf_free_tcam_entry_parms free_parms      = { 0 };
1972         uint32_t hit = 0;
1973         uint16_t tmplen = 0;
1974         uint16_t idx;
1975
1976         /* Set the key and mask to the original key and mask. */
1977         key = &okey;
1978         mask = &omask;
1979
1980         /* Skip this if table opcode is NOP */
1981         if (tbl->tbl_opcode == BNXT_ULP_TCAM_TBL_OPC_NOT_USED ||
1982             tbl->tbl_opcode >= BNXT_ULP_TCAM_TBL_OPC_LAST) {
1983                 BNXT_TF_DBG(ERR, "Invalid tcam table opcode %d\n",
1984                             tbl->tbl_opcode);
1985                 return 0;
1986         }
1987
1988         tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx, tbl->shared_session);
1989         if (!tfp) {
1990                 BNXT_TF_DBG(ERR, "Failed to get truflow pointer\n");
1991                 return -EINVAL;
1992         }
1993
1994         /* If only allocation of identifier then perform and exit */
1995         if (tbl->tbl_opcode == BNXT_ULP_TCAM_TBL_OPC_ALLOC_IDENT) {
1996                 rc = ulp_mapper_tcam_tbl_scan_ident_alloc(parms, tbl);
1997                 return rc;
1998         }
1999
2000         kflds = ulp_mapper_key_fields_get(parms, tbl, &num_kflds);
2001         if (!kflds || !num_kflds) {
2002                 BNXT_TF_DBG(ERR, "Failed to get key fields\n");
2003                 return -EINVAL;
2004         }
2005
2006         if (!ulp_blob_init(key, tbl->blob_key_bit_size,
2007                            dparms->key_byte_order) ||
2008             !ulp_blob_init(mask, tbl->blob_key_bit_size,
2009                            dparms->key_byte_order) ||
2010             !ulp_blob_init(&data, tbl->result_bit_size,
2011                            dparms->result_byte_order) ||
2012             !ulp_blob_init(&update_data, tbl->result_bit_size,
2013                            dparms->result_byte_order)) {
2014                 BNXT_TF_DBG(ERR, "blob inits failed.\n");
2015                 return -EINVAL;
2016         }
2017
2018         /* create the key/mask */
2019         /*
2020          * NOTE: The WC table will require some kind of flag to handle the
2021          * mode bits within the key/mask
2022          */
2023         for (i = 0; i < num_kflds; i++) {
2024                 /* Setup the key */
2025                 rc = ulp_mapper_field_opc_process(parms, tbl->direction,
2026                                                   &kflds[i].field_info_spec,
2027                                                   key, 1, "TCAM Key");
2028                 if (rc) {
2029                         BNXT_TF_DBG(ERR, "Key field set failed %s\n",
2030                                     kflds[i].field_info_spec.description);
2031                         return rc;
2032                 }
2033
2034                 /* Setup the mask */
2035                 rc = ulp_mapper_field_opc_process(parms, tbl->direction,
2036                                                   &kflds[i].field_info_mask,
2037                                                   mask, 0, "TCAM Mask");
2038                 if (rc) {
2039                         BNXT_TF_DBG(ERR, "Mask field set failed %s\n",
2040                                     kflds[i].field_info_mask.description);
2041                         return rc;
2042                 }
2043         }
2044
2045         /* For wild card tcam perform the post process to swap the blob */
2046         if (tbl->resource_type == TF_TCAM_TBL_TYPE_WC_TCAM ||
2047             tbl->resource_type == TF_TCAM_TBL_TYPE_WC_TCAM_HIGH ||
2048             tbl->resource_type == TF_TCAM_TBL_TYPE_WC_TCAM_LOW) {
2049                 if (dparms->dynamic_pad_en) {
2050                         /* Sets up the slices for writing to the WC TCAM */
2051                         rc = ulp_mapper_wc_tcam_tbl_dyn_post_process(dparms,
2052                                                                      key, mask,
2053                                                                      &tkey,
2054                                                                      &tmask);
2055                         if (rc) {
2056                                 BNXT_TF_DBG(ERR,
2057                                             "Failed to post proc WC entry.\n");
2058                                 return rc;
2059                         }
2060                         /* Now need to use the transform Key/Mask */
2061                         key = &tkey;
2062                         mask = &tmask;
2063                 } else {
2064                         ulp_mapper_wc_tcam_tbl_post_process(key);
2065                         ulp_mapper_wc_tcam_tbl_post_process(mask);
2066                 }
2067
2068         }
2069
2070         if (tbl->tbl_opcode == BNXT_ULP_TCAM_TBL_OPC_ALLOC_WR_REGFILE) {
2071                 /* allocate the tcam index */
2072                 aparms.dir = tbl->direction;
2073                 aparms.tcam_tbl_type = tbl->resource_type;
2074                 aparms.key = ulp_blob_data_get(key, &tmplen);
2075                 aparms.key_sz_in_bits = tmplen;
2076                 aparms.mask = ulp_blob_data_get(mask, &tmplen);
2077
2078                 /* calculate the entry priority */
2079                 rc = ulp_mapper_priority_opc_process(parms, tbl,
2080                                                      &aparms.priority);
2081                 if (rc) {
2082                         BNXT_TF_DBG(ERR, "entry priority process failed\n");
2083                         return rc;
2084                 }
2085
2086                 rc = tf_alloc_tcam_entry(tfp, &aparms);
2087                 if (rc) {
2088                         BNXT_TF_DBG(ERR, "tcam alloc failed rc=%d.\n", rc);
2089                         return rc;
2090                 }
2091                 idx = aparms.idx;
2092                 hit = aparms.hit;
2093         } else {
2094                 /*
2095                  * Searching before allocation to see if we already have an
2096                  * entry.  This allows re-use of a constrained resource.
2097                  */
2098                 searchparms.dir = tbl->direction;
2099                 searchparms.tcam_tbl_type = tbl->resource_type;
2100                 searchparms.key = ulp_blob_data_get(key, &tmplen);
2101                 searchparms.key_sz_in_bits = tbl->key_bit_size;
2102                 searchparms.mask = ulp_blob_data_get(mask, &tmplen);
2103                 searchparms.alloc = 1;
2104                 searchparms.result = ulp_blob_data_get(&data, &tmplen);
2105                 searchparms.result_sz_in_bits = tbl->result_bit_size;
2106
2107                 /* calculate the entry priority */
2108                 rc = ulp_mapper_priority_opc_process(parms, tbl,
2109                                                      &searchparms.priority);
2110                 if (rc) {
2111                         BNXT_TF_DBG(ERR, "entry priority process failed\n");
2112                         return rc;
2113                 }
2114
2115                 rc = tf_search_tcam_entry(tfp, &searchparms);
2116                 if (rc) {
2117                         BNXT_TF_DBG(ERR, "tcam search failed rc=%d\n", rc);
2118                         return rc;
2119                 }
2120
2121                 /* Successful search, check the result */
2122                 if (searchparms.search_status == REJECT) {
2123                         BNXT_TF_DBG(ERR, "tcam alloc rejected\n");
2124                         return -ENOMEM;
2125                 }
2126                 idx = searchparms.idx;
2127                 hit = searchparms.hit;
2128         }
2129
2130         /* Write the tcam index into the regfile*/
2131         if (ulp_regfile_write(parms->regfile, tbl->tbl_operand,
2132                               (uint64_t)tfp_cpu_to_be_64(idx))) {
2133                 BNXT_TF_DBG(ERR, "Regfile[%d] write failed.\n",
2134                             tbl->tbl_operand);
2135                 rc = -EINVAL;
2136                 /* Need to free the tcam idx, so goto error */
2137                 goto error;
2138         }
2139
2140         /* if it is miss then it is same as no search before alloc */
2141         if (!hit || tbl->tbl_opcode == BNXT_ULP_TCAM_TBL_OPC_ALLOC_WR_REGFILE) {
2142                 /*Scan identifier list, allocate identifier and update regfile*/
2143                 rc = ulp_mapper_tcam_tbl_scan_ident_alloc(parms, tbl);
2144                 /* Create the result blob */
2145                 if (!rc)
2146                         rc = ulp_mapper_tbl_result_build(parms, tbl, &data,
2147                                                          "TCAM Result");
2148                 /* write the tcam entry */
2149                 if (!rc)
2150                         rc = ulp_mapper_tcam_tbl_entry_write(parms, tbl, key,
2151                                                              mask, &data, idx);
2152         } else {
2153                 /*Scan identifier list, extract identifier and update regfile*/
2154                 rc = ulp_mapper_tcam_tbl_scan_ident_extract(parms, tbl, &data);
2155         }
2156         if (rc)
2157                 goto error;
2158
2159         /* Add the tcam index to the flow database */
2160         fid_parms.direction = tbl->direction;
2161         fid_parms.resource_func = tbl->resource_func;
2162         fid_parms.resource_type = tbl->resource_type;
2163         fid_parms.critical_resource = tbl->critical_resource;
2164         fid_parms.resource_hndl = idx;
2165         ulp_flow_db_shared_session_set(&fid_parms, tbl->shared_session);
2166
2167         rc = ulp_mapper_fdb_opc_process(parms, tbl, &fid_parms);
2168         if (rc) {
2169                 BNXT_TF_DBG(ERR, "Failed to link resource to flow rc = %d\n",
2170                             rc);
2171                 /* Need to free the identifier, so goto error */
2172                 goto error;
2173         }
2174
2175         return 0;
2176 error:
2177         free_parms.dir                  = tbl->direction;
2178         free_parms.tcam_tbl_type        = tbl->resource_type;
2179         free_parms.idx                  = idx;
2180         trc = tf_free_tcam_entry(tfp, &free_parms);
2181         if (trc)
2182                 BNXT_TF_DBG(ERR, "Failed to free tcam[%d][%d][%d] on failure\n",
2183                             tbl->resource_type, tbl->direction, idx);
2184         return rc;
2185 }
2186
2187 static int32_t
2188 ulp_mapper_em_tbl_process(struct bnxt_ulp_mapper_parms *parms,
2189                           struct bnxt_ulp_mapper_tbl_info *tbl)
2190 {
2191         struct bnxt_ulp_mapper_key_info *kflds;
2192         struct ulp_blob key, data;
2193         uint32_t i, num_kflds;
2194         uint16_t tmplen;
2195         struct tf *tfp;
2196         struct ulp_flow_db_res_params   fid_parms = { 0 };
2197         struct tf_insert_em_entry_parms iparms = { 0 };
2198         struct tf_delete_em_entry_parms free_parms = { 0 };
2199         enum bnxt_ulp_flow_mem_type mtype;
2200         struct bnxt_ulp_device_params *dparms = parms->device_params;
2201         int32_t trc;
2202         int32_t rc = 0;
2203         int32_t pad = 0;
2204
2205         tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx, tbl->shared_session);
2206         rc = bnxt_ulp_cntxt_mem_type_get(parms->ulp_ctx, &mtype);
2207         if (rc) {
2208                 BNXT_TF_DBG(ERR, "Failed to get the mem type for EM\n");
2209                 return -EINVAL;
2210         }
2211
2212         kflds = ulp_mapper_key_fields_get(parms, tbl, &num_kflds);
2213         if (!kflds || !num_kflds) {
2214                 BNXT_TF_DBG(ERR, "Failed to get key fields\n");
2215                 return -EINVAL;
2216         }
2217
2218         /* Initialize the key/result blobs */
2219         if (!ulp_blob_init(&key, tbl->blob_key_bit_size,
2220                            dparms->key_byte_order) ||
2221             !ulp_blob_init(&data, tbl->result_bit_size,
2222                            dparms->result_byte_order)) {
2223                 BNXT_TF_DBG(ERR, "blob inits failed.\n");
2224                 return -EINVAL;
2225         }
2226
2227         /* create the key */
2228         for (i = 0; i < num_kflds; i++) {
2229                 /* Setup the key */
2230                 rc = ulp_mapper_field_opc_process(parms, tbl->direction,
2231                                                   &kflds[i].field_info_spec,
2232                                                   &key, 1, "EM Key");
2233                 if (rc) {
2234                         BNXT_TF_DBG(ERR, "Key field set failed.\n");
2235                         return rc;
2236                 }
2237         }
2238
2239         /* if dynamic padding is enabled then add padding to result data */
2240         if (dparms->dynamic_pad_en) {
2241                 /* add padding to make sure key is at byte boundary */
2242                 ulp_blob_pad_align(&key, ULP_BUFFER_ALIGN_8_BITS);
2243
2244                 /* add the pad */
2245                 pad = dparms->em_blk_align_bits - dparms->em_blk_size_bits;
2246                 if (pad < 0) {
2247                         BNXT_TF_DBG(ERR, "Invalid em blk size and align\n");
2248                         return -EINVAL;
2249                 }
2250                 ulp_blob_pad_push(&data, (uint32_t)pad);
2251         }
2252
2253         /* Create the result data blob */
2254         rc = ulp_mapper_tbl_result_build(parms, tbl, &data, "EM Result");
2255         if (rc) {
2256                 BNXT_TF_DBG(ERR, "Failed to build the result blob\n");
2257                 return rc;
2258         }
2259 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG
2260 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG_MAPPER
2261         ulp_mapper_result_dump("EM Result", tbl, &data);
2262 #endif
2263 #endif
2264         if (dparms->dynamic_pad_en) {
2265                 uint32_t abits = dparms->em_blk_align_bits;
2266
2267                 /* when dynamic padding is enabled merge result + key */
2268                 rc = ulp_blob_block_merge(&data, &key, abits, pad);
2269                 if (rc) {
2270                         BNXT_TF_DBG(ERR, "Failed to merge the result blob\n");
2271                         return rc;
2272                 }
2273
2274                 /* add padding to make sure merged result is at slice boundary*/
2275                 ulp_blob_pad_align(&data, abits);
2276
2277                 ulp_blob_perform_byte_reverse(&data, ULP_BITS_2_BYTE(abits));
2278 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG
2279 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG_MAPPER
2280         ulp_mapper_result_dump("EM Merged Result", tbl, &data);
2281 #endif
2282 #endif
2283         }
2284
2285         /* do the transpose for the internal EM keys */
2286         if (tbl->resource_type == TF_MEM_INTERNAL) {
2287                 if (dparms->em_key_align_bytes) {
2288                         int32_t b = ULP_BYTE_2_BITS(dparms->em_key_align_bytes);
2289
2290                         tmplen = ulp_blob_data_len_get(&key);
2291                         ulp_blob_pad_push(&key, b - tmplen);
2292                 }
2293                 tmplen = ulp_blob_data_len_get(&key);
2294                 ulp_blob_perform_byte_reverse(&key, ULP_BITS_2_BYTE(tmplen));
2295 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG
2296 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG_MAPPER
2297         ulp_mapper_result_dump("EM Key Transpose", tbl, &key);
2298 #endif
2299 #endif
2300         }
2301
2302         rc = bnxt_ulp_cntxt_tbl_scope_id_get(parms->ulp_ctx,
2303                                              &iparms.tbl_scope_id);
2304         if (rc) {
2305                 BNXT_TF_DBG(ERR, "Failed to get table scope rc=%d\n", rc);
2306                 return rc;
2307         }
2308
2309         /*
2310          * NOTE: the actual blob size will differ from the size in the tbl
2311          * entry due to the padding.
2312          */
2313         iparms.dup_check                = 0;
2314         iparms.dir                      = tbl->direction;
2315         iparms.mem                      = tbl->resource_type;
2316         iparms.key                      = ulp_blob_data_get(&key, &tmplen);
2317         iparms.key_sz_in_bits           = tbl->key_bit_size;
2318         iparms.em_record                = ulp_blob_data_get(&data, &tmplen);
2319         if (tbl->result_bit_size)
2320                 iparms.em_record_sz_in_bits     = tbl->result_bit_size;
2321         else
2322                 iparms.em_record_sz_in_bits     = tmplen;
2323
2324         rc = tf_insert_em_entry(tfp, &iparms);
2325         if (rc) {
2326                 BNXT_TF_DBG(ERR, "Failed to insert em entry rc=%d.\n", rc);
2327                 return rc;
2328         }
2329
2330 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG
2331 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG_MAPPER
2332         ulp_mapper_em_dump("EM", &key, &data, &iparms);
2333         /* tf_dump_tables(tfp, iparms.tbl_scope_id); */
2334 #endif
2335 #endif
2336         /* Mark action process */
2337         if (mtype == BNXT_ULP_FLOW_MEM_TYPE_EXT &&
2338             tbl->resource_type == TF_MEM_EXTERNAL)
2339                 rc = ulp_mapper_mark_gfid_process(parms, tbl, iparms.flow_id);
2340         else if (mtype == BNXT_ULP_FLOW_MEM_TYPE_INT &&
2341                  tbl->resource_type == TF_MEM_INTERNAL)
2342                 rc = ulp_mapper_mark_act_ptr_process(parms, tbl);
2343         if (rc) {
2344                 BNXT_TF_DBG(ERR, "Failed to add mark to flow\n");
2345                 goto error;
2346         }
2347
2348         /* Link the EM resource to the flow in the flow db */
2349         memset(&fid_parms, 0, sizeof(fid_parms));
2350         fid_parms.direction             = tbl->direction;
2351         fid_parms.resource_func         = tbl->resource_func;
2352         fid_parms.resource_type         = tbl->resource_type;
2353         fid_parms.critical_resource     = tbl->critical_resource;
2354         fid_parms.resource_hndl         = iparms.flow_handle;
2355
2356         rc = ulp_mapper_fdb_opc_process(parms, tbl, &fid_parms);
2357         if (rc) {
2358                 BNXT_TF_DBG(ERR, "Fail to link res to flow rc = %d\n",
2359                             rc);
2360                 /* Need to free the identifier, so goto error */
2361                 goto error;
2362         }
2363
2364         return 0;
2365 error:
2366         free_parms.dir          = iparms.dir;
2367         free_parms.mem          = iparms.mem;
2368         free_parms.tbl_scope_id = iparms.tbl_scope_id;
2369         free_parms.flow_handle  = iparms.flow_handle;
2370
2371         trc = tf_delete_em_entry(tfp, &free_parms);
2372         if (trc)
2373                 BNXT_TF_DBG(ERR, "Failed to delete EM entry on failed add\n");
2374
2375         return rc;
2376 }
2377
2378 static int32_t
2379 ulp_mapper_index_tbl_process(struct bnxt_ulp_mapper_parms *parms,
2380                              struct bnxt_ulp_mapper_tbl_info *tbl)
2381 {
2382         struct ulp_flow_db_res_params fid_parms;
2383         struct ulp_blob data;
2384         uint64_t regval = 0;
2385         uint16_t tmplen;
2386         uint32_t index;
2387         int32_t rc = 0, trc = 0;
2388         struct tf_alloc_tbl_entry_parms aparms = { 0 };
2389         struct tf_set_tbl_entry_parms sparms = { 0 };
2390         struct tf_get_tbl_entry_parms gparms = { 0 };
2391         struct tf_free_tbl_entry_parms free_parms = { 0 };
2392         uint32_t tbl_scope_id;
2393         struct tf *tfp;
2394         struct bnxt_ulp_glb_resource_info glb_res = { 0 };
2395         uint16_t bit_size;
2396         bool alloc = false;
2397         bool write = false;
2398         bool global = false;
2399         uint64_t act_rec_size;
2400         bool shared = false;
2401
2402         tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx, tbl->shared_session);
2403         /* use the max size if encap is enabled */
2404         if (tbl->encap_num_fields)
2405                 bit_size = BNXT_ULP_FLMP_BLOB_SIZE_IN_BITS;
2406         else
2407                 bit_size = tbl->result_bit_size;
2408
2409         /* Initialize the blob data */
2410         if (!ulp_blob_init(&data, bit_size,
2411                            parms->device_params->result_byte_order)) {
2412                 BNXT_TF_DBG(ERR, "Failed to initialize index table blob\n");
2413                 return -EINVAL;
2414         }
2415
2416         /* Get the scope id first */
2417         rc = bnxt_ulp_cntxt_tbl_scope_id_get(parms->ulp_ctx, &tbl_scope_id);
2418         if (rc) {
2419                 BNXT_TF_DBG(ERR, "Failed to get table scope rc=%d\n", rc);
2420                 return rc;
2421         }
2422
2423         switch (tbl->tbl_opcode) {
2424         case BNXT_ULP_INDEX_TBL_OPC_ALLOC_REGFILE:
2425                 alloc = true;
2426                 break;
2427         case BNXT_ULP_INDEX_TBL_OPC_ALLOC_WR_REGFILE:
2428                 /*
2429                  * Build the entry, alloc an index, write the table, and store
2430                  * the data in the regfile.
2431                  */
2432                 alloc = true;
2433                 write = true;
2434                 break;
2435         case BNXT_ULP_INDEX_TBL_OPC_WR_REGFILE:
2436                 /*
2437                  * get the index to write to from the regfile and then write
2438                  * the table entry.
2439                  */
2440                 if (!ulp_regfile_read(parms->regfile,
2441                                       tbl->tbl_operand,
2442                                       &regval)) {
2443                         BNXT_TF_DBG(ERR,
2444                                     "Failed to get tbl idx from regfile[%d].\n",
2445                                     tbl->tbl_operand);
2446                         return -EINVAL;
2447                 }
2448                 index = tfp_be_to_cpu_64(regval);
2449                 /* For external, we need to reverse shift */
2450                 if (tbl->resource_type == TF_TBL_TYPE_EXT)
2451                         index = TF_ACT_REC_PTR_2_OFFSET(index);
2452
2453                 write = true;
2454                 break;
2455         case BNXT_ULP_INDEX_TBL_OPC_ALLOC_WR_GLB_REGFILE:
2456                 /*
2457                  * Build the entry, alloc an index, write the table, and store
2458                  * the data in the global regfile.
2459                  */
2460                 alloc = true;
2461                 global = true;
2462                 write = true;
2463                 glb_res.direction = tbl->direction;
2464                 glb_res.resource_func = tbl->resource_func;
2465                 glb_res.resource_type = tbl->resource_type;
2466                 glb_res.glb_regfile_index = tbl->tbl_operand;
2467                 break;
2468         case BNXT_ULP_INDEX_TBL_OPC_WR_GLB_REGFILE:
2469                 if (tbl->fdb_opcode != BNXT_ULP_FDB_OPC_NOP) {
2470                         BNXT_TF_DBG(ERR, "Template error, wrong fdb opcode\n");
2471                         return -EINVAL;
2472                 }
2473                 /*
2474                  * get the index to write to from the global regfile and then
2475                  * write the table.
2476                  */
2477                 if (ulp_mapper_glb_resource_read(parms->mapper_data,
2478                                                  tbl->direction,
2479                                                  tbl->tbl_operand,
2480                                                  &regval, &shared)) {
2481                         BNXT_TF_DBG(ERR,
2482                                     "Failed to get tbl idx from Glb RF[%d].\n",
2483                                     tbl->tbl_operand);
2484                         return -EINVAL;
2485                 }
2486                 index = tfp_be_to_cpu_64(regval);
2487                 /* For external, we need to reverse shift */
2488                 if (tbl->resource_type == TF_TBL_TYPE_EXT)
2489                         index = TF_ACT_REC_PTR_2_OFFSET(index);
2490                 write = true;
2491                 break;
2492         case BNXT_ULP_INDEX_TBL_OPC_RD_REGFILE:
2493                 /*
2494                  * The read is different from the rest and can be handled here
2495                  * instead of trying to use common code.  Simply read the table
2496                  * with the index from the regfile, scan and store the
2497                  * identifiers, and return.
2498                  */
2499                 if (tbl->resource_type == TF_TBL_TYPE_EXT) {
2500                         /* Not currently supporting with EXT */
2501                         BNXT_TF_DBG(ERR,
2502                                     "Ext Table Read Opcode not supported.\n");
2503                         return -EINVAL;
2504                 }
2505                 if (!ulp_regfile_read(parms->regfile,
2506                                       tbl->tbl_operand, &regval)) {
2507                         BNXT_TF_DBG(ERR,
2508                                     "Failed to get tbl idx from regfile[%d]\n",
2509                                     tbl->tbl_operand);
2510                         return -EINVAL;
2511                 }
2512                 index = tfp_be_to_cpu_64(regval);
2513                 gparms.dir = tbl->direction;
2514                 gparms.type = tbl->resource_type;
2515                 gparms.data = ulp_blob_data_get(&data, &tmplen);
2516                 gparms.data_sz_in_bytes = ULP_BITS_2_BYTE(tbl->result_bit_size);
2517                 gparms.idx = index;
2518                 rc = tf_get_tbl_entry(tfp, &gparms);
2519                 if (rc) {
2520                         BNXT_TF_DBG(ERR, "Failed to read the tbl entry %d:%d\n",
2521                                     tbl->resource_type, index);
2522                         return rc;
2523                 }
2524                 /*
2525                  * Scan the fields in the entry and push them into the regfile.
2526                  */
2527                 rc = ulp_mapper_tbl_ident_scan_ext(parms, tbl,
2528                                                    gparms.data,
2529                                                    gparms.data_sz_in_bytes,
2530                                                    data.byte_order);
2531                 if (rc) {
2532                         BNXT_TF_DBG(ERR,
2533                                     "Failed to get flds on tbl read rc=%d\n",
2534                                     rc);
2535                         return rc;
2536                 }
2537                 return 0;
2538         default:
2539                 BNXT_TF_DBG(ERR, "Invalid index table opcode %d\n",
2540                             tbl->tbl_opcode);
2541                 return -EINVAL;
2542         }
2543
2544         if (write) {
2545                 /* Get the result fields list */
2546                 rc = ulp_mapper_tbl_result_build(parms,
2547                                                  tbl,
2548                                                  &data,
2549                                                  "Indexed Result");
2550                 if (rc) {
2551                         BNXT_TF_DBG(ERR, "Failed to build the result blob\n");
2552                         return rc;
2553                 }
2554         }
2555
2556         if (alloc) {
2557                 aparms.dir              = tbl->direction;
2558                 aparms.type             = tbl->resource_type;
2559                 aparms.tbl_scope_id     = tbl_scope_id;
2560
2561                 /* All failures after the alloc succeeds require a free */
2562                 rc = tf_alloc_tbl_entry(tfp, &aparms);
2563                 if (rc) {
2564                         BNXT_TF_DBG(ERR, "Alloc table[%s][%s] failed rc=%d\n",
2565                                     tf_tbl_type_2_str(tbl->resource_type),
2566                                     tf_dir_2_str(tbl->direction), rc);
2567                         return rc;
2568                 }
2569                 index = aparms.idx;
2570
2571                 /*
2572                  * Store the index in the regfile since we either allocated it
2573                  * or it was a hit.
2574                  *
2575                  * Calculate the idx for the result record, for external EM the
2576                  * offset needs to be shifted accordingly.
2577                  * If external non-inline table types are used then need to
2578                  * revisit this logic.
2579                  */
2580                 if (tbl->resource_type == TF_TBL_TYPE_EXT)
2581                         regval = TF_ACT_REC_OFFSET_2_PTR(index);
2582                 else
2583                         regval = index;
2584                 regval = tfp_cpu_to_be_64(regval);
2585
2586                 if (global) {
2587                         /*
2588                          * Shared resources are never allocated through this
2589                          * method, so the shared flag is always false.
2590                          */
2591                         rc = ulp_mapper_glb_resource_write(parms->mapper_data,
2592                                                            &glb_res, regval,
2593                                                            false);
2594                 } else {
2595                         rc = ulp_regfile_write(parms->regfile,
2596                                                tbl->tbl_operand, regval);
2597                 }
2598                 if (rc) {
2599                         BNXT_TF_DBG(ERR,
2600                                     "Failed to write %s regfile[%d] rc=%d\n",
2601                                     (global) ? "global" : "reg",
2602                                     tbl->tbl_operand, rc);
2603                         goto error;
2604                 }
2605         }
2606
2607         if (write) {
2608                 sparms.dir = tbl->direction;
2609                 sparms.type = tbl->resource_type;
2610                 sparms.data = ulp_blob_data_get(&data, &tmplen);
2611                 sparms.data_sz_in_bytes = ULP_BITS_2_BYTE(tmplen);
2612                 sparms.idx = index;
2613                 sparms.tbl_scope_id = tbl_scope_id;
2614                 if (shared)
2615                         tfp = bnxt_ulp_cntxt_shared_tfp_get(parms->ulp_ctx);
2616                 rc = tf_set_tbl_entry(tfp, &sparms);
2617                 if (rc) {
2618                         BNXT_TF_DBG(ERR,
2619                                     "Index table[%s][%s][%x] write fail rc=%d\n",
2620                                     tf_tbl_type_2_str(sparms.type),
2621                                     tf_dir_2_str(sparms.dir),
2622                                     sparms.idx, rc);
2623                         goto error;
2624                 }
2625                 BNXT_TF_INF("Index table[%s][%s][%x] write successful.\n",
2626                             tf_tbl_type_2_str(sparms.type),
2627                             tf_dir_2_str(sparms.dir), sparms.idx);
2628
2629                 /* Calculate action record size */
2630                 if (tbl->resource_type == TF_TBL_TYPE_EXT) {
2631                         act_rec_size = (ULP_BITS_2_BYTE_NR(tmplen) + 15) / 16;
2632                         act_rec_size--;
2633                         if (ulp_regfile_write(parms->regfile,
2634                                               BNXT_ULP_RF_IDX_ACTION_REC_SIZE,
2635                                               tfp_cpu_to_be_64(act_rec_size)))
2636                                 BNXT_TF_DBG(ERR,
2637                                             "Failed write the act rec size\n");
2638                 }
2639         }
2640
2641         /* Link the resource to the flow in the flow db */
2642         memset(&fid_parms, 0, sizeof(fid_parms));
2643         fid_parms.direction     = tbl->direction;
2644         fid_parms.resource_func = tbl->resource_func;
2645         fid_parms.resource_type = tbl->resource_type;
2646         fid_parms.resource_sub_type = tbl->resource_sub_type;
2647         fid_parms.resource_hndl = index;
2648         fid_parms.critical_resource = tbl->critical_resource;
2649         ulp_flow_db_shared_session_set(&fid_parms, tbl->shared_session);
2650
2651         rc = ulp_mapper_fdb_opc_process(parms, tbl, &fid_parms);
2652         if (rc) {
2653                 BNXT_TF_DBG(ERR, "Failed to link resource to flow rc = %d\n",
2654                             rc);
2655                 goto error;
2656         }
2657
2658         /* Perform the VF rep action */
2659         rc = ulp_mapper_mark_vfr_idx_process(parms, tbl);
2660         if (rc) {
2661                 BNXT_TF_DBG(ERR, "Failed to add vfr mark rc = %d\n", rc);
2662                 goto error;
2663         }
2664         return rc;
2665 error:
2666         /* Shared resources are not freed */
2667         if (shared)
2668                 return rc;
2669         /*
2670          * Free the allocated resource since we failed to either
2671          * write to the entry or link the flow
2672          */
2673         free_parms.dir  = tbl->direction;
2674         free_parms.type = tbl->resource_type;
2675         free_parms.idx  = index;
2676         free_parms.tbl_scope_id = tbl_scope_id;
2677
2678         trc = tf_free_tbl_entry(tfp, &free_parms);
2679         if (trc)
2680                 BNXT_TF_DBG(ERR, "Failed to free tbl entry on failure\n");
2681
2682         return rc;
2683 }
2684
2685 static int32_t
2686 ulp_mapper_if_tbl_process(struct bnxt_ulp_mapper_parms *parms,
2687                           struct bnxt_ulp_mapper_tbl_info *tbl)
2688 {
2689         struct ulp_blob data, res_blob;
2690         uint64_t idx;
2691         uint16_t tmplen;
2692         int32_t rc = 0;
2693         struct tf_set_if_tbl_entry_parms iftbl_params = { 0 };
2694         struct tf_get_if_tbl_entry_parms get_parms = { 0 };
2695         struct tf *tfp;
2696         enum bnxt_ulp_if_tbl_opc if_opc = tbl->tbl_opcode;
2697         uint32_t res_size;
2698
2699         tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx, tbl->shared_session);
2700         /* Initialize the blob data */
2701         if (!ulp_blob_init(&data, tbl->result_bit_size,
2702                            parms->device_params->result_byte_order)) {
2703                 BNXT_TF_DBG(ERR, "Failed initial index table blob\n");
2704                 return -EINVAL;
2705         }
2706
2707         /* create the result blob */
2708         rc = ulp_mapper_tbl_result_build(parms, tbl, &data, "IFtable Result");
2709         if (rc) {
2710                 BNXT_TF_DBG(ERR, "Failed to build the result blob\n");
2711                 return rc;
2712         }
2713
2714         /* Get the index details */
2715         switch (if_opc) {
2716         case BNXT_ULP_IF_TBL_OPC_WR_COMP_FIELD:
2717                 idx = ULP_COMP_FLD_IDX_RD(parms, tbl->tbl_operand);
2718                 break;
2719         case BNXT_ULP_IF_TBL_OPC_WR_REGFILE:
2720                 if (!ulp_regfile_read(parms->regfile, tbl->tbl_operand, &idx)) {
2721                         BNXT_TF_DBG(ERR, "regfile[%d] read oob\n",
2722                                     tbl->tbl_operand);
2723                         return -EINVAL;
2724                 }
2725                 idx = tfp_be_to_cpu_64(idx);
2726                 break;
2727         case BNXT_ULP_IF_TBL_OPC_WR_CONST:
2728                 idx = tbl->tbl_operand;
2729                 break;
2730         case BNXT_ULP_IF_TBL_OPC_RD_COMP_FIELD:
2731                 /* Initialize the result blob */
2732                 if (!ulp_blob_init(&res_blob, tbl->result_bit_size,
2733                                    parms->device_params->result_byte_order)) {
2734                         BNXT_TF_DBG(ERR, "Failed initial result blob\n");
2735                         return -EINVAL;
2736                 }
2737
2738                 /* read the interface table */
2739                 idx = ULP_COMP_FLD_IDX_RD(parms, tbl->tbl_operand);
2740                 res_size = ULP_BITS_2_BYTE(tbl->result_bit_size);
2741                 get_parms.dir = tbl->direction;
2742                 get_parms.type = tbl->resource_type;
2743                 get_parms.idx = idx;
2744                 get_parms.data = ulp_blob_data_get(&res_blob, &tmplen);
2745                 get_parms.data_sz_in_bytes = res_size;
2746
2747                 rc = tf_get_if_tbl_entry(tfp, &get_parms);
2748                 if (rc) {
2749                         BNXT_TF_DBG(ERR, "Get table[%d][%s][%x] failed rc=%d\n",
2750                                     get_parms.type,
2751                                     tf_dir_2_str(get_parms.dir),
2752                                     get_parms.idx, rc);
2753                         return rc;
2754                 }
2755                 rc = ulp_mapper_tbl_ident_scan_ext(parms, tbl,
2756                                                    res_blob.data,
2757                                                    res_size,
2758                                                    res_blob.byte_order);
2759                 if (rc)
2760                         BNXT_TF_DBG(ERR, "Scan and extract failed rc=%d\n", rc);
2761                 return rc;
2762         case BNXT_ULP_IF_TBL_OPC_NOT_USED:
2763                 return rc; /* skip it */
2764         default:
2765                 BNXT_TF_DBG(ERR, "Invalid tbl index opcode\n");
2766                 return -EINVAL;
2767         }
2768
2769         /* Perform the tf table set by filling the set params */
2770         iftbl_params.dir = tbl->direction;
2771         iftbl_params.type = tbl->resource_type;
2772         iftbl_params.data = ulp_blob_data_get(&data, &tmplen);
2773         iftbl_params.data_sz_in_bytes = ULP_BITS_2_BYTE(tmplen);
2774         iftbl_params.idx = idx;
2775
2776         rc = tf_set_if_tbl_entry(tfp, &iftbl_params);
2777         if (rc) {
2778                 BNXT_TF_DBG(ERR, "Set table[%d][%s][%x] failed rc=%d\n",
2779                             iftbl_params.type,/* TBD: add tf_if_tbl_2_str */
2780                             tf_dir_2_str(iftbl_params.dir),
2781                             iftbl_params.idx, rc);
2782                 return rc;
2783         }
2784         BNXT_TF_INF("Set table[%s][%s][%x] success.\n",
2785                     tf_if_tbl_2_str(iftbl_params.type),
2786                     tf_dir_2_str(iftbl_params.dir),
2787                     iftbl_params.idx);
2788
2789         /*
2790          * TBD: Need to look at the need to store idx in flow db for restore
2791          * the table to its original state on deletion of this entry.
2792          */
2793         return rc;
2794 }
2795
2796 static int32_t
2797 ulp_mapper_gen_tbl_process(struct bnxt_ulp_mapper_parms *parms,
2798                            struct bnxt_ulp_mapper_tbl_info *tbl)
2799 {
2800         struct ulp_mapper_gen_tbl_list *gen_tbl_list;
2801         struct bnxt_ulp_mapper_key_info *kflds;
2802         struct ulp_flow_db_res_params fid_parms;
2803         struct ulp_mapper_gen_tbl_entry gen_tbl_ent, *g;
2804         struct ulp_gen_hash_entry_params hash_entry;
2805         uint16_t tmplen = 0;
2806         struct ulp_blob key, data;
2807         uint8_t *cache_key;
2808         int32_t tbl_idx;
2809         uint32_t i, num_kflds = 0, key_index = 0;
2810         uint32_t gen_tbl_miss = 1, fdb_write = 0;
2811         uint8_t *byte_data;
2812         int32_t rc = 0;
2813
2814         /* Get the key fields list and build the key. */
2815         kflds = ulp_mapper_key_fields_get(parms, tbl, &num_kflds);
2816         if (!kflds || !num_kflds) {
2817                 BNXT_TF_DBG(ERR, "Failed to get key fields\n");
2818                 return -EINVAL;
2819         }
2820
2821         if (!ulp_blob_init(&key, tbl->key_bit_size,
2822                            parms->device_params->key_byte_order)) {
2823                 BNXT_TF_DBG(ERR, "Failed to alloc blob\n");
2824                 return -EINVAL;
2825         }
2826         for (i = 0; i < num_kflds; i++) {
2827                 /* Setup the key */
2828                 rc = ulp_mapper_field_opc_process(parms, tbl->direction,
2829                                                   &kflds[i].field_info_spec,
2830                                                   &key, 1, "Gen Tbl Key");
2831                 if (rc) {
2832                         BNXT_TF_DBG(ERR,
2833                                     "Failed to create key for Gen tbl rc=%d\n",
2834                                     rc);
2835                         return -EINVAL;
2836                 }
2837         }
2838
2839         /* Calculate the table index for the generic table*/
2840         tbl_idx = ulp_mapper_gen_tbl_idx_calculate(tbl->resource_sub_type,
2841                                                    tbl->direction);
2842         if (tbl_idx < 0) {
2843                 BNXT_TF_DBG(ERR, "Invalid table index %x:%x\n",
2844                             tbl->resource_sub_type, tbl->direction);
2845                 return -EINVAL;
2846         }
2847
2848         /* The_key is a byte array convert it to a search index */
2849         cache_key = ulp_blob_data_get(&key, &tmplen);
2850 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG
2851 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG_MAPPER
2852         BNXT_TF_DBG(DEBUG, "The gen_tbl[%u] key\n", tbl_idx);
2853         ulp_mapper_blob_dump(&key);
2854 #endif
2855 #endif
2856         /* get the generic table  */
2857         gen_tbl_list = &parms->mapper_data->gen_tbl_list[tbl_idx];
2858
2859         /* Check if generic hash table */
2860         if (gen_tbl_list->hash_tbl) {
2861                 if (tbl->gen_tbl_lkup_type !=
2862                     BNXT_ULP_GENERIC_TBL_LKUP_TYPE_HASH) {
2863                         BNXT_TF_DBG(ERR, "%s: Invalid template lkup type\n",
2864                                     gen_tbl_list->gen_tbl_name);
2865                         return -EINVAL;
2866                 }
2867                 hash_entry.key_data = cache_key;
2868                 hash_entry.key_length = ULP_BITS_2_BYTE(tmplen);
2869                 rc = ulp_gen_hash_tbl_list_key_search(gen_tbl_list->hash_tbl,
2870                                                       &hash_entry);
2871                 if (rc) {
2872                         BNXT_TF_DBG(ERR, "%s: hash tbl search failed\n",
2873                                     gen_tbl_list->gen_tbl_name);
2874                         return rc;
2875                 }
2876                 if (hash_entry.search_flag == ULP_GEN_HASH_SEARCH_FOUND) {
2877                         key_index = hash_entry.key_idx;
2878                         /* Get the generic table entry */
2879                         if (ulp_mapper_gen_tbl_entry_get(gen_tbl_list,
2880                                                          key_index,
2881                                                          &gen_tbl_ent))
2882                                 return -EINVAL;
2883                         /* store the hash index in the fdb */
2884                         key_index = hash_entry.hash_index;
2885                 }
2886         } else {
2887                 /* convert key to index directly */
2888                 if (ULP_BITS_2_BYTE(tmplen) > (int32_t)sizeof(key_index)) {
2889                         BNXT_TF_DBG(ERR, "%s: keysize is bigger then 4 bytes\n",
2890                                     gen_tbl_list->gen_tbl_name);
2891                         return -EINVAL;
2892                 }
2893                 memcpy(&key_index, cache_key, ULP_BITS_2_BYTE(tmplen));
2894                 /* Get the generic table entry */
2895                 if (ulp_mapper_gen_tbl_entry_get(gen_tbl_list, key_index,
2896                                                  &gen_tbl_ent))
2897                         return -EINVAL;
2898         }
2899         switch (tbl->tbl_opcode) {
2900         case BNXT_ULP_GENERIC_TBL_OPC_READ:
2901                 if (gen_tbl_list->hash_tbl) {
2902                         if (hash_entry.search_flag != ULP_GEN_HASH_SEARCH_FOUND)
2903                                 break; /* nothing to be done , no entry */
2904                 }
2905
2906                 /* check the reference count */
2907                 if (ULP_GEN_TBL_REF_CNT(&gen_tbl_ent)) {
2908                         g = &gen_tbl_ent;
2909                         /* Scan ident list and create the result blob*/
2910                         rc = ulp_mapper_tbl_ident_scan_ext(parms, tbl,
2911                                                            g->byte_data,
2912                                                            g->byte_data_size,
2913                                                            g->byte_order);
2914                         if (rc) {
2915                                 BNXT_TF_DBG(ERR,
2916                                             "Failed to scan ident list\n");
2917                                 return -EINVAL;
2918                         }
2919                         if (tbl->fdb_opcode != BNXT_ULP_FDB_OPC_NOP) {
2920                                 /* increment the reference count */
2921                                 ULP_GEN_TBL_REF_CNT_INC(&gen_tbl_ent);
2922                         }
2923
2924                         /* it is a hit */
2925                         gen_tbl_miss = 0;
2926                         fdb_write = 1;
2927                 }
2928                 break;
2929         case BNXT_ULP_GENERIC_TBL_OPC_WRITE:
2930                 if (gen_tbl_list->hash_tbl) {
2931                         rc = ulp_mapper_gen_tbl_hash_entry_add(gen_tbl_list,
2932                                                                &hash_entry,
2933                                                                &gen_tbl_ent);
2934                         if (rc)
2935                                 return rc;
2936                         /* store the hash index in the fdb */
2937                         key_index = hash_entry.hash_index;
2938                 }
2939                 /* check the reference count */
2940                 if (ULP_GEN_TBL_REF_CNT(&gen_tbl_ent)) {
2941                         /* a hit then error */
2942                         BNXT_TF_DBG(ERR, "generic entry already present\n");
2943                         return -EINVAL; /* success */
2944                 }
2945
2946                 /* Initialize the blob data */
2947                 if (!ulp_blob_init(&data, tbl->result_bit_size,
2948                                    gen_tbl_ent.byte_order)) {
2949                         BNXT_TF_DBG(ERR, "Failed initial index table blob\n");
2950                         return -EINVAL;
2951                 }
2952
2953                 /* Get the result fields list */
2954                 rc = ulp_mapper_tbl_result_build(parms, tbl, &data,
2955                                                  "Gen tbl Result");
2956                 if (rc) {
2957                         BNXT_TF_DBG(ERR, "Failed to build the result blob\n");
2958                         return rc;
2959                 }
2960                 byte_data = ulp_blob_data_get(&data, &tmplen);
2961                 rc = ulp_mapper_gen_tbl_entry_data_set(&gen_tbl_ent,
2962                                                        tmplen, byte_data,
2963                                                        ULP_BITS_2_BYTE(tmplen));
2964                 if (rc) {
2965                         BNXT_TF_DBG(ERR, "Failed to write generic table\n");
2966                         return -EINVAL;
2967                 }
2968
2969                 /* increment the reference count */
2970                 ULP_GEN_TBL_REF_CNT_INC(&gen_tbl_ent);
2971                 fdb_write = 1;
2972                 parms->shared_hndl = (uint64_t)tbl_idx << 32 | key_index;
2973                 break;
2974         default:
2975                 BNXT_TF_DBG(ERR, "Invalid table opcode %x\n", tbl->tbl_opcode);
2976                 return -EINVAL;
2977         }
2978
2979         /* Set the generic entry hit */
2980         rc = ulp_regfile_write(parms->regfile,
2981                                BNXT_ULP_RF_IDX_GENERIC_TBL_MISS,
2982                                tfp_cpu_to_be_64(gen_tbl_miss));
2983         if (rc) {
2984                 BNXT_TF_DBG(ERR, "Write regfile[%d] failed\n",
2985                             BNXT_ULP_RF_IDX_GENERIC_TBL_MISS);
2986                 return -EIO;
2987         }
2988
2989         /* add the entry to the flow database */
2990         if (fdb_write) {
2991                 memset(&fid_parms, 0, sizeof(fid_parms));
2992                 fid_parms.direction = tbl->direction;
2993                 fid_parms.resource_func = tbl->resource_func;
2994                 fid_parms.resource_sub_type = tbl->resource_sub_type;
2995                 fid_parms.resource_hndl = key_index;
2996                 fid_parms.critical_resource = tbl->critical_resource;
2997                 ulp_flow_db_shared_session_set(&fid_parms, tbl->shared_session);
2998
2999                 rc = ulp_mapper_fdb_opc_process(parms, tbl, &fid_parms);
3000                 if (rc)
3001                         BNXT_TF_DBG(ERR, "Fail to add gen ent flowdb %d\n", rc);
3002         }
3003         return rc;
3004 }
3005
3006 static int32_t
3007 ulp_mapper_ctrl_tbl_process(struct bnxt_ulp_mapper_parms *parms,
3008                             struct bnxt_ulp_mapper_tbl_info *tbl)
3009 {
3010         int32_t rc = 0;
3011
3012         /* process the fdb opcode for alloc push */
3013         if (tbl->fdb_opcode == BNXT_ULP_FDB_OPC_ALLOC_RID_REGFILE) {
3014                 rc = ulp_mapper_fdb_opc_alloc_rid(parms, tbl);
3015                 if (rc) {
3016                         BNXT_TF_DBG(ERR, "Failed to do fdb alloc\n");
3017                         return rc;
3018                 }
3019         }
3020         return rc;
3021 }
3022
3023 static int32_t
3024 ulp_mapper_glb_resource_info_init(struct bnxt_ulp_context *ulp_ctx,
3025                                   struct bnxt_ulp_mapper_data *mapper_data)
3026 {
3027         struct bnxt_ulp_glb_resource_info *glb_res;
3028         uint32_t num_glb_res_ids, idx, dev_id;
3029         uint8_t app_id;
3030         int32_t rc = 0;
3031
3032         glb_res = ulp_mapper_glb_resource_info_list_get(&num_glb_res_ids);
3033         if (!glb_res || !num_glb_res_ids) {
3034                 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
3035                 return -EINVAL;
3036         }
3037
3038         rc = bnxt_ulp_cntxt_dev_id_get(ulp_ctx, &dev_id);
3039         if (rc) {
3040                 BNXT_TF_DBG(ERR, "Failed to get device id for glb init (%d)\n",
3041                             rc);
3042                 return rc;
3043         }
3044
3045         rc = bnxt_ulp_cntxt_app_id_get(ulp_ctx, &app_id);
3046         if (rc) {
3047                 BNXT_TF_DBG(ERR, "Failed to get app id for glb init (%d)\n",
3048                             rc);
3049                 return rc;
3050         }
3051
3052         /* Iterate the global resources and process each one */
3053         for (idx = 0; idx < num_glb_res_ids; idx++) {
3054                 if (dev_id != glb_res[idx].device_id ||
3055                     glb_res[idx].app_id != app_id)
3056                         continue;
3057                 switch (glb_res[idx].resource_func) {
3058                 case BNXT_ULP_RESOURCE_FUNC_IDENTIFIER:
3059                         rc = ulp_mapper_resource_ident_allocate(ulp_ctx,
3060                                                                 mapper_data,
3061                                                                 &glb_res[idx]);
3062                         break;
3063                 case BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE:
3064                         rc = ulp_mapper_resource_index_tbl_alloc(ulp_ctx,
3065                                                                  mapper_data,
3066                                                                  &glb_res[idx]);
3067                         break;
3068                 default:
3069                         BNXT_TF_DBG(ERR, "Global resource %x not supported\n",
3070                                     glb_res[idx].resource_func);
3071                         rc = -EINVAL;
3072                         break;
3073                 }
3074                 if (rc)
3075                         return rc;
3076         }
3077         return rc;
3078 }
3079
3080 /*
3081  * Iterate over the shared resources assigned during tf_open_session and store
3082  * them in the global regfile with the shared flag.
3083  */
3084 static int32_t
3085 ulp_mapper_app_glb_resource_info_init(struct bnxt_ulp_context *ulp_ctx,
3086                                       struct bnxt_ulp_mapper_data *mapper_data)
3087 {
3088         struct tf_get_shared_tbl_increment_parms iparms;
3089         struct bnxt_ulp_glb_resource_info *glb_res;
3090         struct tf_get_session_info_parms sparms;
3091         uint32_t num_entries, i, dev_id, res;
3092         struct tf_resource_info *res_info;
3093         uint32_t addend;
3094         uint64_t regval;
3095         enum tf_dir dir;
3096         int32_t rc = 0;
3097         struct tf *tfp;
3098         uint8_t app_id;
3099
3100         memset(&sparms, 0, sizeof(sparms));
3101         glb_res = bnxt_ulp_app_glb_resource_info_list_get(&num_entries);
3102         if (!glb_res || !num_entries) {
3103                 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
3104                 return -EINVAL;
3105         }
3106         tfp = bnxt_ulp_cntxt_shared_tfp_get(ulp_ctx);
3107         if (!tfp) {
3108                 BNXT_TF_DBG(ERR, "Failed to get tfp for app global init");
3109                 return -EINVAL;
3110         }
3111         /*
3112          * Retrieve the resources that were assigned during the shared session
3113          * creation.
3114          */
3115         rc = tf_get_session_info(tfp, &sparms);
3116         if (rc) {
3117                 BNXT_TF_DBG(ERR, "Failed to get session info (%d)\n", rc);
3118                 return rc;
3119         }
3120
3121         rc = bnxt_ulp_cntxt_app_id_get(ulp_ctx, &app_id);
3122         if (rc) {
3123                 BNXT_TF_DBG(ERR, "Failed to get the app id in glb init (%d).\n",
3124                             rc);
3125                 return rc;
3126         }
3127
3128         rc = bnxt_ulp_cntxt_dev_id_get(ulp_ctx, &dev_id);
3129         if (rc) {
3130                 BNXT_TF_DBG(ERR, "Failed to get dev id for app glb init (%d)\n",
3131                             rc);
3132                 return rc;
3133         }
3134
3135         /* Store all the app global resources */
3136         for (i = 0; i < num_entries; i++) {
3137                 if (dev_id != glb_res[i].device_id ||
3138                     app_id != glb_res[i].app_id)
3139                         continue;
3140                 dir = glb_res[i].direction;
3141                 res = glb_res[i].resource_type;
3142                 addend = 1;
3143
3144                 switch (glb_res[i].resource_func) {
3145                 case BNXT_ULP_RESOURCE_FUNC_IDENTIFIER:
3146                         res_info = &sparms.session_info.ident[dir].info[res];
3147                         break;
3148                 case BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE:
3149                         /*
3150                          * Tables may have various strides for the allocations.
3151                          * Need to account.
3152                          */
3153                         memset(&iparms, 0, sizeof(iparms));
3154                         iparms.dir = dir;
3155                         iparms.type = res;
3156                         rc = tf_get_shared_tbl_increment(tfp, &iparms);
3157                         if (rc) {
3158                                 BNXT_TF_DBG(ERR,
3159                                             "Failed to get addend for %s[%s] rc=(%d)\n",
3160                                             tf_tbl_type_2_str(res),
3161                                             tf_dir_2_str(dir), rc);
3162                                 return rc;
3163                         }
3164                         addend = iparms.increment_cnt;
3165                         res_info = &sparms.session_info.tbl[dir].info[res];
3166                         break;
3167                 case BNXT_ULP_RESOURCE_FUNC_TCAM_TABLE:
3168                         res_info = &sparms.session_info.tcam[dir].info[res];
3169                         break;
3170                 case BNXT_ULP_RESOURCE_FUNC_EM_TABLE:
3171                         res_info = &sparms.session_info.em[dir].info[res];
3172                         break;
3173                 default:
3174                         BNXT_TF_DBG(ERR, "Unknown resource func (0x%x)\n",
3175                                     glb_res[i].resource_func);
3176                         continue;
3177                 }
3178                 regval = tfp_cpu_to_be_64((uint64_t)res_info->start);
3179                 res_info->start += addend;
3180                 /*
3181                  * All resources written to the global regfile are shared for
3182                  * this function.
3183                  */
3184                 rc = ulp_mapper_glb_resource_write(mapper_data, &glb_res[i],
3185                                                    regval, true);
3186                 if (rc)
3187                         return rc;
3188         }
3189
3190         return rc;
3191 }
3192
3193 /*
3194  * Common conditional opcode process routine that is used for both the template
3195  * rejection and table conditional execution.
3196  */
3197 static int32_t
3198 ulp_mapper_cond_opc_process(struct bnxt_ulp_mapper_parms *parms,
3199                             enum bnxt_ulp_cond_opc opc,
3200                             uint32_t operand,
3201                             int32_t *res)
3202 {
3203         enum bnxt_ulp_flow_mem_type mtype = BNXT_ULP_FLOW_MEM_TYPE_INT;
3204         int32_t rc = 0;
3205         uint8_t bit;
3206         uint64_t regval;
3207
3208         switch (opc) {
3209         case BNXT_ULP_COND_OPC_CF_IS_SET:
3210                 if (operand < BNXT_ULP_CF_IDX_LAST) {
3211                         *res = ULP_COMP_FLD_IDX_RD(parms, operand);
3212                 } else {
3213                         BNXT_TF_DBG(ERR, "comp field out of bounds %d\n",
3214                                     operand);
3215                         rc = -EINVAL;
3216                 }
3217                 break;
3218         case BNXT_ULP_COND_OPC_CF_NOT_SET:
3219                 if (operand < BNXT_ULP_CF_IDX_LAST) {
3220                         *res = !ULP_COMP_FLD_IDX_RD(parms, operand);
3221                 } else {
3222                         BNXT_TF_DBG(ERR, "comp field out of bounds %d\n",
3223                                     operand);
3224                         rc = -EINVAL;
3225                 }
3226                 break;
3227         case BNXT_ULP_COND_OPC_ACT_BIT_IS_SET:
3228                 if (operand < BNXT_ULP_ACT_BIT_LAST) {
3229                         *res = ULP_BITMAP_ISSET(parms->act_bitmap->bits,
3230                                                 operand);
3231                 } else {
3232                         BNXT_TF_DBG(ERR, "action bit out of bounds %d\n",
3233                                     operand);
3234                         rc = -EINVAL;
3235                 }
3236                 break;
3237         case BNXT_ULP_COND_OPC_ACT_BIT_NOT_SET:
3238                 if (operand < BNXT_ULP_ACT_BIT_LAST) {
3239                         *res = !ULP_BITMAP_ISSET(parms->act_bitmap->bits,
3240                                                operand);
3241                 } else {
3242                         BNXT_TF_DBG(ERR, "action bit out of bounds %d\n",
3243                                     operand);
3244                         rc = -EINVAL;
3245                 }
3246                 break;
3247         case BNXT_ULP_COND_OPC_HDR_BIT_IS_SET:
3248                 if (operand < BNXT_ULP_HDR_BIT_LAST) {
3249                         *res = ULP_BITMAP_ISSET(parms->hdr_bitmap->bits,
3250                                                 operand);
3251                 } else {
3252                         BNXT_TF_DBG(ERR, "header bit out of bounds %d\n",
3253                                     operand);
3254                         rc = -EINVAL;
3255                 }
3256                 break;
3257         case BNXT_ULP_COND_OPC_HDR_BIT_NOT_SET:
3258                 if (operand < BNXT_ULP_HDR_BIT_LAST) {
3259                         *res = !ULP_BITMAP_ISSET(parms->hdr_bitmap->bits,
3260                                                operand);
3261                 } else {
3262                         BNXT_TF_DBG(ERR, "header bit out of bounds %d\n",
3263                                     operand);
3264                         rc = -EINVAL;
3265                 }
3266                 break;
3267         case BNXT_ULP_COND_OPC_FIELD_BIT_IS_SET:
3268                 rc = ulp_mapper_glb_field_tbl_get(parms, operand, &bit);
3269                 if (rc) {
3270                         BNXT_TF_DBG(ERR, "invalid ulp_glb_field_tbl idx %d\n",
3271                                     operand);
3272                         return -EINVAL;
3273                 }
3274                 *res = ULP_INDEX_BITMAP_GET(parms->fld_bitmap->bits, bit);
3275                 break;
3276         case BNXT_ULP_COND_OPC_FIELD_BIT_NOT_SET:
3277                 rc = ulp_mapper_glb_field_tbl_get(parms, operand, &bit);
3278                 if (rc) {
3279                         BNXT_TF_DBG(ERR, "invalid ulp_glb_field_tbl idx %d\n",
3280                                     operand);
3281                         return -EINVAL;
3282                 }
3283                 *res = !ULP_INDEX_BITMAP_GET(parms->fld_bitmap->bits, bit);
3284                 break;
3285         case BNXT_ULP_COND_OPC_RF_IS_SET:
3286                 if (!ulp_regfile_read(parms->regfile, operand, &regval)) {
3287                         BNXT_TF_DBG(ERR, "regfile[%d] read oob\n", operand);
3288                         return -EINVAL;
3289                 }
3290                 *res = regval != 0;
3291                 break;
3292         case BNXT_ULP_COND_OPC_RF_NOT_SET:
3293                 if (!ulp_regfile_read(parms->regfile, operand, &regval)) {
3294                         BNXT_TF_DBG(ERR, "regfile[%d] read oob\n", operand);
3295                         return -EINVAL;
3296                 }
3297                 *res = regval == 0;
3298                 break;
3299         case BNXT_ULP_COND_OPC_FLOW_PAT_MATCH:
3300                 if (parms->flow_pattern_id == operand) {
3301                         BNXT_TF_DBG(ERR, "field pattern match failed %x\n",
3302                                     parms->flow_pattern_id);
3303                         return -EINVAL;
3304                 }
3305                 break;
3306         case BNXT_ULP_COND_OPC_ACT_PAT_MATCH:
3307                 if (parms->act_pattern_id == operand) {
3308                         BNXT_TF_DBG(ERR, "act pattern match failed %x\n",
3309                                     parms->act_pattern_id);
3310                         return -EINVAL;
3311                 }
3312                 break;
3313         case BNXT_ULP_COND_OPC_EXT_MEM_IS_SET:
3314                 if (bnxt_ulp_cntxt_mem_type_get(parms->ulp_ctx, &mtype)) {
3315                         BNXT_TF_DBG(ERR, "Failed to get the mem type\n");
3316                         return -EINVAL;
3317                 }
3318                 *res = (mtype == BNXT_ULP_FLOW_MEM_TYPE_INT) ? 0 : 1;
3319                 break;
3320         case BNXT_ULP_COND_OPC_EXT_MEM_NOT_SET:
3321                 if (bnxt_ulp_cntxt_mem_type_get(parms->ulp_ctx, &mtype)) {
3322                         BNXT_TF_DBG(ERR, "Failed to get the mem type\n");
3323                         return -EINVAL;
3324                 }
3325                 *res = (mtype == BNXT_ULP_FLOW_MEM_TYPE_INT) ? 1 : 0;
3326                 break;
3327         case BNXT_ULP_COND_OPC_ENC_HDR_BIT_IS_SET:
3328                 if (operand < BNXT_ULP_HDR_BIT_LAST) {
3329                         *res = ULP_BITMAP_ISSET(parms->enc_hdr_bitmap->bits,
3330                                                 operand);
3331                 } else {
3332                         BNXT_TF_DBG(ERR, "header bit out of bounds %d\n",
3333                                     operand);
3334                         rc = -EINVAL;
3335                 }
3336                 break;
3337         case BNXT_ULP_COND_OPC_ENC_HDR_BIT_NOT_SET:
3338                 if (operand < BNXT_ULP_HDR_BIT_LAST) {
3339                         *res = !ULP_BITMAP_ISSET(parms->enc_hdr_bitmap->bits,
3340                                                  operand);
3341                 } else {
3342                         BNXT_TF_DBG(ERR, "header bit out of bounds %d\n",
3343                                     operand);
3344                         rc = -EINVAL;
3345                 }
3346                 break;
3347         default:
3348                 BNXT_TF_DBG(ERR, "Invalid conditional opcode %d\n", opc);
3349                 rc = -EINVAL;
3350                 break;
3351         }
3352         return (rc);
3353 }
3354
3355 static int32_t
3356 ulp_mapper_func_opr_compute(struct bnxt_ulp_mapper_parms *parms,
3357                             enum tf_dir dir,
3358                             enum bnxt_ulp_func_src func_src,
3359                             uint16_t func_opr,
3360                             uint64_t *result)
3361 {
3362         uint64_t regval;
3363         bool shared;
3364
3365         *result =  false;
3366         switch (func_src) {
3367         case BNXT_ULP_FUNC_SRC_COMP_FIELD:
3368                 if (func_opr >= BNXT_ULP_CF_IDX_LAST) {
3369                         BNXT_TF_DBG(ERR, "invalid index %u\n", func_opr);
3370                         return -EINVAL;
3371                 }
3372                 *result = ULP_COMP_FLD_IDX_RD(parms, func_opr);
3373                 break;
3374         case BNXT_ULP_FUNC_SRC_REGFILE:
3375                 if (!ulp_regfile_read(parms->regfile, func_opr, &regval)) {
3376                         BNXT_TF_DBG(ERR, "regfile[%d] read oob\n", func_opr);
3377                         return -EINVAL;
3378                 }
3379                 *result = tfp_be_to_cpu_64(regval);
3380                 break;
3381         case BNXT_ULP_FUNC_SRC_GLB_REGFILE:
3382                 if (ulp_mapper_glb_resource_read(parms->mapper_data, dir,
3383                                                  func_opr, &regval, &shared)) {
3384                         BNXT_TF_DBG(ERR, "global regfile[%d] read failed.\n",
3385                                     func_opr);
3386                         return -EINVAL;
3387                 }
3388                 *result = tfp_be_to_cpu_64(regval);
3389                 break;
3390         case BNXT_ULP_FUNC_SRC_CONST:
3391                 *result = func_opr;
3392                 break;
3393         default:
3394                 BNXT_TF_DBG(ERR, "invalid src code %u\n", func_src);
3395                 return -EINVAL;
3396         }
3397         return 0;
3398 }
3399
3400 static int32_t
3401 ulp_mapper_func_info_process(struct bnxt_ulp_mapper_parms *parms,
3402                              struct bnxt_ulp_mapper_tbl_info *tbl)
3403 {
3404         struct bnxt_ulp_mapper_func_info *func_info = &tbl->func_info;
3405         uint64_t res = 0, res1 = 0, res2 = 0;
3406         int32_t rc = 0;
3407         uint32_t process_src1 = 0, process_src2 = 0;
3408
3409         /* determine which functional operands to compute */
3410         switch (func_info->func_opc) {
3411         case BNXT_ULP_FUNC_OPC_NOP:
3412                 return rc;
3413         case BNXT_ULP_FUNC_OPC_EQ:
3414         case BNXT_ULP_FUNC_OPC_NE:
3415         case BNXT_ULP_FUNC_OPC_GE:
3416         case BNXT_ULP_FUNC_OPC_GT:
3417         case BNXT_ULP_FUNC_OPC_LE:
3418         case BNXT_ULP_FUNC_OPC_LT:
3419                 process_src1 = 1;
3420                 process_src2 = 1;
3421                 break;
3422         case BNXT_ULP_FUNC_OPC_COPY_SRC1_TO_RF:
3423                 process_src1 = 1;
3424                 break;
3425         default:
3426                 break;
3427         }
3428
3429         if (process_src1) {
3430                 rc = ulp_mapper_func_opr_compute(parms, tbl->direction,
3431                                                  func_info->func_src1,
3432                                                  func_info->func_opr1, &res1);
3433                 if (rc)
3434                         return rc;
3435         }
3436
3437         if (process_src2) {
3438                 rc = ulp_mapper_func_opr_compute(parms, tbl->direction,
3439                                                  func_info->func_src2,
3440                                                  func_info->func_opr2, &res2);
3441                 if (rc)
3442                         return rc;
3443         }
3444
3445         /* perform the functional opcode operations */
3446         switch (func_info->func_opc) {
3447         case BNXT_ULP_FUNC_OPC_EQ:
3448                 if (res1 == res2)
3449                         res = 1;
3450                 break;
3451         case BNXT_ULP_FUNC_OPC_NE:
3452                 if (res1 != res2)
3453                         res = 1;
3454                 break;
3455         case BNXT_ULP_FUNC_OPC_GE:
3456                 if (res1 >= res2)
3457                         res = 1;
3458                 break;
3459         case BNXT_ULP_FUNC_OPC_GT:
3460                 if (res1 > res2)
3461                         res = 1;
3462                 break;
3463         case BNXT_ULP_FUNC_OPC_LE:
3464                 if (res1 <= res2)
3465                         res = 1;
3466                 break;
3467         case BNXT_ULP_FUNC_OPC_LT:
3468                 if (res1 < res2)
3469                         res = 1;
3470                 break;
3471         case BNXT_ULP_FUNC_OPC_COPY_SRC1_TO_RF:
3472                 res = res1;
3473                 break;
3474         case BNXT_ULP_FUNC_OPC_RSS_CONFIG:
3475                 /* apply the rss config using pmd method */
3476                 return bnxt_rss_config_action_apply(parms);
3477         case BNXT_ULP_FUNC_OPC_GET_PARENT_MAC_ADDR:
3478                 rc = bnxt_pmd_get_parent_mac_addr(parms, (uint8_t *)&res);
3479                 if (rc)
3480                         return -EINVAL;
3481                 res = tfp_be_to_cpu_64(res);
3482                 break;
3483         default:
3484                 BNXT_TF_DBG(ERR, "invalid func code %u\n", func_info->func_opc);
3485                 return -EINVAL;
3486         }
3487         if (ulp_regfile_write(parms->regfile, func_info->func_dst_opr,
3488                               tfp_cpu_to_be_64(res))) {
3489                 BNXT_TF_DBG(ERR, "Failed write the func_opc %u\n",
3490                             func_info->func_dst_opr);
3491                 return -EINVAL;
3492         }
3493
3494         return rc;
3495 }
3496
3497
3498 /*
3499  * Processes a list of conditions and returns both a status and result of the
3500  * list.  The status must be checked prior to verifying the result.
3501  *
3502  * returns 0 for success, negative on failure
3503  * returns res = 1 for true, res = 0 for false.
3504  */
3505 static int32_t
3506 ulp_mapper_cond_opc_list_process(struct bnxt_ulp_mapper_parms *parms,
3507                                  enum bnxt_ulp_cond_list_opc list_opc,
3508                                  struct bnxt_ulp_mapper_cond_info *list,
3509                                  uint32_t num,
3510                                  int32_t *res)
3511 {
3512         uint32_t i;
3513         int32_t rc = 0, trc = 0;
3514
3515         switch (list_opc) {
3516         case BNXT_ULP_COND_LIST_OPC_AND:
3517                 /* AND Defaults to true. */
3518                 *res = 1;
3519                 break;
3520         case BNXT_ULP_COND_LIST_OPC_OR:
3521                 /* OR Defaults to false. */
3522                 *res = 0;
3523                 break;
3524         case BNXT_ULP_COND_LIST_OPC_TRUE:
3525                 *res = 1;
3526                 return rc;
3527         case BNXT_ULP_COND_LIST_OPC_FALSE:
3528                 *res = 0;
3529                 return rc;
3530         default:
3531                 BNXT_TF_DBG(ERR, "Invalid conditional list opcode %d\n",
3532                             list_opc);
3533                 *res = 0;
3534                 return -EINVAL;
3535         }
3536
3537         for (i = 0; i < num; i++) {
3538                 rc = ulp_mapper_cond_opc_process(parms,
3539                                                  list[i].cond_opcode,
3540                                                  list[i].cond_operand,
3541                                                  &trc);
3542                 if (rc)
3543                         return rc;
3544
3545                 if (list_opc == BNXT_ULP_COND_LIST_OPC_AND) {
3546                         /* early return if result is ever zero */
3547                         if (!trc) {
3548                                 *res = trc;
3549                                 return rc;
3550                         }
3551                 } else {
3552                         /* early return if result is ever non-zero */
3553                         if (trc) {
3554                                 *res = trc;
3555                                 return rc;
3556                         }
3557                 }
3558         }
3559
3560         return rc;
3561 }
3562
3563 /*
3564  * Processes conflict resolution and returns both a status and result.
3565  * The status must be checked prior to verifying the result.
3566  *
3567  * returns 0 for success, negative on failure
3568  * returns res = 1 for true, res = 0 for false.
3569  */
3570 static int32_t
3571 ulp_mapper_conflict_resolution_process(struct bnxt_ulp_mapper_parms *parms,
3572                                        struct bnxt_ulp_mapper_tbl_info *tbl,
3573                                        int32_t *res)
3574 {
3575         int32_t rc = 0;
3576         uint64_t regval;
3577         uint64_t comp_sig;
3578
3579         *res = 0;
3580         switch (tbl->accept_opcode) {
3581         case BNXT_ULP_ACCEPT_OPC_ALWAYS:
3582                 *res = 1;
3583                 break;
3584         case BNXT_ULP_ACCEPT_OPC_FLOW_SIG_ID_MATCH:
3585                 /* perform the signature validation*/
3586                 if (tbl->resource_func ==
3587                     BNXT_ULP_RESOURCE_FUNC_GENERIC_TABLE) {
3588                         /* Perform the check that generic table is hit or not */
3589                         if (!ulp_regfile_read(parms->regfile,
3590                                               BNXT_ULP_RF_IDX_GENERIC_TBL_MISS,
3591                                               &regval)) {
3592                                 BNXT_TF_DBG(ERR, "regfile[%d] read oob\n",
3593                                             BNXT_ULP_RF_IDX_GENERIC_TBL_MISS);
3594                                 return -EINVAL;
3595                         }
3596                         if (regval) {
3597                                 /* not a hit so no need to check flow sign*/
3598                                 *res = 1;
3599                                 return rc;
3600                         }
3601                 }
3602                 /* compare the new flow signature against stored one */
3603                 if (!ulp_regfile_read(parms->regfile,
3604                                       BNXT_ULP_RF_IDX_FLOW_SIG_ID,
3605                                       &regval)) {
3606                         BNXT_TF_DBG(ERR, "regfile[%d] read oob\n",
3607                                     BNXT_ULP_RF_IDX_FLOW_SIG_ID);
3608                         return -EINVAL;
3609                 }
3610                 comp_sig = ULP_COMP_FLD_IDX_RD(parms,
3611                                                BNXT_ULP_CF_IDX_FLOW_SIG_ID);
3612                 regval = tfp_be_to_cpu_64(regval);
3613                 if (comp_sig == regval)
3614                         *res = 1;
3615                 else
3616                         BNXT_TF_DBG(ERR, "failed signature match 0x%016"
3617                                     PRIX64 ":%x\n", comp_sig, (uint32_t)regval);
3618                 break;
3619         default:
3620                 BNXT_TF_DBG(ERR, "Invalid accept opcode %d\n",
3621                             tbl->accept_opcode);
3622                 return -EINVAL;
3623         }
3624         return rc;
3625 }
3626
3627 static int32_t
3628 ulp_mapper_tbls_process(struct bnxt_ulp_mapper_parms *parms, uint32_t tid)
3629 {
3630         struct bnxt_ulp_mapper_cond_info *cond_tbls = NULL;
3631         enum bnxt_ulp_cond_list_opc cond_opc;
3632         struct bnxt_ulp_mapper_tbl_info *tbls;
3633         struct bnxt_ulp_mapper_tbl_info *tbl;
3634         uint32_t num_tbls, tbl_idx, num_cond_tbls;
3635         int32_t rc = -EINVAL, cond_rc = 0;
3636         int32_t cond_goto = 1;
3637
3638         cond_tbls = ulp_mapper_tmpl_reject_list_get(parms, tid,
3639                                                     &num_cond_tbls,
3640                                                     &cond_opc);
3641         /*
3642          * Process the reject list if exists, otherwise assume that the
3643          * template is allowed.
3644          */
3645         if (cond_tbls && num_cond_tbls) {
3646                 rc = ulp_mapper_cond_opc_list_process(parms,
3647                                                       cond_opc,
3648                                                       cond_tbls,
3649                                                       num_cond_tbls,
3650                                                       &cond_rc);
3651                 if (rc)
3652                         return rc;
3653
3654                 /* Reject the template if True */
3655                 if (cond_rc) {
3656                         BNXT_TF_DBG(ERR, "%s Template %d rejected.\n",
3657                                     ulp_mapper_tmpl_name_str(parms->tmpl_type),
3658                                     tid);
3659                         return -EINVAL;
3660                 }
3661         }
3662
3663         tbls = ulp_mapper_tbl_list_get(parms, tid, &num_tbls);
3664         if (!tbls || !num_tbls) {
3665                 BNXT_TF_DBG(ERR, "No %s tables for %d:%d\n",
3666                             ulp_mapper_tmpl_name_str(parms->tmpl_type),
3667                             parms->dev_id, tid);
3668                 return -EINVAL;
3669         }
3670
3671         for (tbl_idx = 0; tbl_idx < num_tbls && cond_goto;) {
3672                 tbl = &tbls[tbl_idx];
3673                 cond_goto = tbl->execute_info.cond_true_goto;
3674 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG
3675 #ifdef RTE_LIBRTE_BNXT_TRUFLOW_DEBUG_MAPPER
3676                 ulp_mapper_table_dump(tbl, tbl_idx);
3677 #endif
3678 #endif
3679                 /* Process the conditional func code opcodes */
3680                 if (ulp_mapper_func_info_process(parms, tbl)) {
3681                         BNXT_TF_DBG(ERR, "Failed to process cond update\n");
3682                         rc = -EINVAL;
3683                         goto error;
3684                 }
3685
3686                 cond_tbls = ulp_mapper_tbl_execute_list_get(parms, tbl,
3687                                                             &num_cond_tbls,
3688                                                             &cond_opc);
3689                 rc = ulp_mapper_cond_opc_list_process(parms, cond_opc,
3690                                                       cond_tbls, num_cond_tbls,
3691                                                       &cond_rc);
3692                 if (rc) {
3693                         BNXT_TF_DBG(ERR, "Failed to proc cond opc list (%d)\n",
3694                                     rc);
3695                         goto error;
3696                 }
3697                 /* Skip the table if False */
3698                 if (!cond_rc) {
3699                         cond_goto = tbl->execute_info.cond_false_goto;
3700                         goto next_iteration;
3701                 }
3702
3703                 switch (tbl->resource_func) {
3704                 case BNXT_ULP_RESOURCE_FUNC_TCAM_TABLE:
3705                         rc = ulp_mapper_tcam_tbl_process(parms, tbl);
3706                         break;
3707                 case BNXT_ULP_RESOURCE_FUNC_EM_TABLE:
3708                         rc = ulp_mapper_em_tbl_process(parms, tbl);
3709                         break;
3710                 case BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE:
3711                         rc = ulp_mapper_index_tbl_process(parms, tbl);
3712                         break;
3713                 case BNXT_ULP_RESOURCE_FUNC_IF_TABLE:
3714                         rc = ulp_mapper_if_tbl_process(parms, tbl);
3715                         break;
3716                 case BNXT_ULP_RESOURCE_FUNC_GENERIC_TABLE:
3717                         rc = ulp_mapper_gen_tbl_process(parms, tbl);
3718                         break;
3719                 case BNXT_ULP_RESOURCE_FUNC_CTRL_TABLE:
3720                         rc = ulp_mapper_ctrl_tbl_process(parms, tbl);
3721                         break;
3722                 case BNXT_ULP_RESOURCE_FUNC_INVALID:
3723                         rc = 0;
3724                         break;
3725                 default:
3726                         BNXT_TF_DBG(ERR, "Unexpected mapper resource %d\n",
3727                                     tbl->resource_func);
3728                         rc = -EINVAL;
3729                         goto error;
3730                 }
3731
3732                 if (rc) {
3733                         BNXT_TF_DBG(ERR, "Resource type %d failed\n",
3734                                     tbl->resource_func);
3735                         goto error;
3736                 }
3737
3738                 /* perform the post table process */
3739                 rc  = ulp_mapper_conflict_resolution_process(parms, tbl,
3740                                                              &cond_rc);
3741                 if (rc || !cond_rc) {
3742                         BNXT_TF_DBG(ERR, "Failed due to conflict resolution\n");
3743                         rc = -EINVAL;
3744                         goto error;
3745                 }
3746 next_iteration:
3747                 if (cond_goto == BNXT_ULP_COND_GOTO_REJECT) {
3748                         BNXT_TF_DBG(ERR, "reject the flow\n");
3749                         rc = -EINVAL;
3750                         goto error;
3751                 } else if (cond_goto & BNXT_ULP_COND_GOTO_RF) {
3752                         uint32_t rf_idx;
3753                         uint64_t regval;
3754
3755                         /* least significant 16 bits from reg_file index */
3756                         rf_idx = (uint32_t)(cond_goto & 0xFFFF);
3757                         if (!ulp_regfile_read(parms->regfile, rf_idx,
3758                                               &regval)) {
3759                                 BNXT_TF_DBG(ERR, "regfile[%d] read oob\n",
3760                                             rf_idx);
3761                                 rc = -EINVAL;
3762                                 goto error;
3763                         }
3764                         cond_goto = (int32_t)regval;
3765                 }
3766
3767                 if (cond_goto < 0 && ((int32_t)tbl_idx + cond_goto) < 0) {
3768                         BNXT_TF_DBG(ERR, "invalid conditional goto %d\n",
3769                                     cond_goto);
3770                         goto error;
3771                 }
3772                 tbl_idx += cond_goto;
3773         }
3774
3775         return rc;
3776 error:
3777         BNXT_TF_DBG(ERR, "%s tables failed creation for %d:%d\n",
3778                     ulp_mapper_tmpl_name_str(parms->tmpl_type),
3779                     parms->dev_id, tid);
3780         return rc;
3781 }
3782
3783 static int32_t
3784 ulp_mapper_resource_free(struct bnxt_ulp_context *ulp,
3785                          uint32_t fid,
3786                          struct ulp_flow_db_res_params *res)
3787 {
3788         struct tf *tfp;
3789         int32_t rc = 0;
3790
3791         if (!res || !ulp) {
3792                 BNXT_TF_DBG(ERR, "Unable to free resource\n ");
3793                 return -EINVAL;
3794         }
3795         if (res->fdb_flags & ULP_FDB_FLAG_SHARED_SESSION)
3796                 tfp = bnxt_ulp_cntxt_tfp_get(ulp, BNXT_ULP_SHARED_SESSION_YES);
3797         else
3798                 tfp = bnxt_ulp_cntxt_tfp_get(ulp, BNXT_ULP_SHARED_SESSION_NO);
3799         if (!tfp) {
3800                 BNXT_TF_DBG(ERR, "Unable to free resource failed to get tfp\n");
3801                 return -EINVAL;
3802         }
3803
3804         switch (res->resource_func) {
3805         case BNXT_ULP_RESOURCE_FUNC_TCAM_TABLE:
3806                 rc = ulp_mapper_tcam_entry_free(ulp, tfp, res);
3807                 break;
3808         case BNXT_ULP_RESOURCE_FUNC_EM_TABLE:
3809                 rc = ulp_mapper_em_entry_free(ulp, tfp, res);
3810                 break;
3811         case BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE:
3812                 rc = ulp_mapper_index_entry_free(ulp, tfp, res);
3813                 break;
3814         case BNXT_ULP_RESOURCE_FUNC_IDENTIFIER:
3815                 rc = ulp_mapper_ident_free(ulp, tfp, res);
3816                 break;
3817         case BNXT_ULP_RESOURCE_FUNC_HW_FID:
3818                 rc = ulp_mapper_mark_free(ulp, res);
3819                 break;
3820         case BNXT_ULP_RESOURCE_FUNC_PARENT_FLOW:
3821                 rc = ulp_mapper_parent_flow_free(ulp, fid, res);
3822                 break;
3823         case BNXT_ULP_RESOURCE_FUNC_CHILD_FLOW:
3824                 rc = ulp_mapper_child_flow_free(ulp, fid, res);
3825                 break;
3826         case BNXT_ULP_RESOURCE_FUNC_GENERIC_TABLE:
3827                 rc = ulp_mapper_gen_tbl_res_free(ulp, res);
3828                 break;
3829         default:
3830                 break;
3831         }
3832
3833         return rc;
3834 }
3835
3836 int32_t
3837 ulp_mapper_resources_free(struct bnxt_ulp_context *ulp_ctx,
3838                           enum bnxt_ulp_fdb_type flow_type,
3839                           uint32_t fid)
3840 {
3841         struct ulp_flow_db_res_params res_parms = { 0 };
3842         int32_t rc, trc;
3843
3844         if (!ulp_ctx) {
3845                 BNXT_TF_DBG(ERR, "Invalid parms, unable to free flow\n");
3846                 return -EINVAL;
3847         }
3848
3849         /*
3850          * Set the critical resource on the first resource del, then iterate
3851          * while status is good
3852          */
3853         if (flow_type != BNXT_ULP_FDB_TYPE_RID)
3854                 res_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_YES;
3855
3856         rc = ulp_flow_db_resource_del(ulp_ctx, flow_type, fid, &res_parms);
3857
3858         if (rc) {
3859                 /*
3860                  * This is unexpected on the first call to resource del.
3861                  * It likely means that the flow did not exist in the flow db.
3862                  */
3863                 BNXT_TF_DBG(ERR, "Flow[%d][0x%08x] failed to free (rc=%d)\n",
3864                             flow_type, fid, rc);
3865                 return rc;
3866         }
3867
3868         while (!rc) {
3869                 trc = ulp_mapper_resource_free(ulp_ctx, fid, &res_parms);
3870                 if (trc)
3871                         /*
3872                          * On fail, we still need to attempt to free the
3873                          * remaining resources.  Don't return
3874                          */
3875                         BNXT_TF_DBG(ERR,
3876                                     "Flow[%d][0x%x] Res[%d][0x%016" PRIX64
3877                                     "] failed rc=%d.\n",
3878                                     flow_type, fid, res_parms.resource_func,
3879                                     res_parms.resource_hndl, trc);
3880
3881                 /* All subsequent call require the non-critical_resource */
3882                 res_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
3883
3884                 rc = ulp_flow_db_resource_del(ulp_ctx,
3885                                               flow_type,
3886                                               fid,
3887                                               &res_parms);
3888         }
3889
3890         /* Free the Flow ID since we've removed all resources */
3891         rc = ulp_flow_db_fid_free(ulp_ctx, flow_type, fid);
3892
3893         return rc;
3894 }
3895
3896 static void
3897 ulp_mapper_glb_resource_info_deinit(struct bnxt_ulp_context *ulp_ctx,
3898                                     struct bnxt_ulp_mapper_data *mapper_data)
3899 {
3900         struct bnxt_ulp_mapper_glb_resource_entry *ent;
3901         struct ulp_flow_db_res_params res;
3902         uint32_t dir, idx;
3903
3904         /* Iterate the global resources and process each one */
3905         for (dir = TF_DIR_RX; dir < TF_DIR_MAX; dir++) {
3906                 for (idx = 0; idx < BNXT_ULP_GLB_RF_IDX_LAST; idx++) {
3907                         ent = &mapper_data->glb_res_tbl[dir][idx];
3908                         if (ent->resource_func ==
3909                             BNXT_ULP_RESOURCE_FUNC_INVALID ||
3910                             ent->shared)
3911                                 continue;
3912                         memset(&res, 0, sizeof(struct ulp_flow_db_res_params));
3913                         res.resource_func = ent->resource_func;
3914                         res.direction = dir;
3915                         res.resource_type = ent->resource_type;
3916                         /*convert it from BE to cpu */
3917                         res.resource_hndl =
3918                                 tfp_be_to_cpu_64(ent->resource_hndl);
3919                         ulp_mapper_resource_free(ulp_ctx, 0, &res);
3920                 }
3921         }
3922 }
3923
3924 int32_t
3925 ulp_mapper_flow_destroy(struct bnxt_ulp_context *ulp_ctx,
3926                         enum bnxt_ulp_fdb_type flow_type,
3927                         uint32_t fid)
3928 {
3929         int32_t rc;
3930
3931         if (!ulp_ctx) {
3932                 BNXT_TF_DBG(ERR, "Invalid parms, unable to free flow\n");
3933                 return -EINVAL;
3934         }
3935
3936         rc = ulp_mapper_resources_free(ulp_ctx, flow_type, fid);
3937         return rc;
3938 }
3939
3940 /* Function to handle the mapping of the Flow to be compatible
3941  * with the underlying hardware.
3942  */
3943 int32_t
3944 ulp_mapper_flow_create(struct bnxt_ulp_context *ulp_ctx,
3945                        struct bnxt_ulp_mapper_create_parms *cparms)
3946 {
3947         struct bnxt_ulp_mapper_parms parms;
3948         struct ulp_regfile regfile;
3949         int32_t  rc = 0, trc;
3950
3951         if (!ulp_ctx || !cparms)
3952                 return -EINVAL;
3953
3954         /* Initialize the parms structure */
3955         memset(&parms, 0, sizeof(parms));
3956         parms.act_prop = cparms->act_prop;
3957         parms.act_bitmap = cparms->act;
3958         parms.hdr_bitmap = cparms->hdr_bitmap;
3959         parms.enc_hdr_bitmap = cparms->enc_hdr_bitmap;
3960         parms.regfile = &regfile;
3961         parms.hdr_field = cparms->hdr_field;
3962         parms.enc_field = cparms->enc_field;
3963         parms.fld_bitmap = cparms->fld_bitmap;
3964         parms.comp_fld = cparms->comp_fld;
3965         parms.ulp_ctx = ulp_ctx;
3966         parms.act_tid = cparms->act_tid;
3967         parms.class_tid = cparms->class_tid;
3968         parms.flow_type = cparms->flow_type;
3969         parms.parent_flow = cparms->parent_flow;
3970         parms.child_flow = cparms->child_flow;
3971         parms.fid = cparms->flow_id;
3972         parms.tun_idx = cparms->tun_idx;
3973         parms.app_priority = cparms->app_priority;
3974         parms.flow_pattern_id = cparms->flow_pattern_id;
3975         parms.act_pattern_id = cparms->act_pattern_id;
3976         parms.app_id = cparms->app_id;
3977         parms.port_id = cparms->port_id;
3978
3979         /* Get the device id from the ulp context */
3980         if (bnxt_ulp_cntxt_dev_id_get(ulp_ctx, &parms.dev_id)) {
3981                 BNXT_TF_DBG(ERR, "Invalid ulp context\n");
3982                 return -EINVAL;
3983         }
3984
3985         /* Get the device params, it will be used in later processing */
3986         parms.device_params = bnxt_ulp_device_params_get(parms.dev_id);
3987         if (!parms.device_params) {
3988                 BNXT_TF_DBG(ERR, "No device parms for device id %d\n",
3989                             parms.dev_id);
3990                 return -EINVAL;
3991         }
3992
3993         /*
3994          * Get the mapper data for dynamic mapper data such as default
3995          * ids.
3996          */
3997         parms.mapper_data = (struct bnxt_ulp_mapper_data *)
3998                 bnxt_ulp_cntxt_ptr2_mapper_data_get(ulp_ctx);
3999         if (!parms.mapper_data) {
4000                 BNXT_TF_DBG(ERR, "Failed to get the ulp mapper data\n");
4001                 return -EINVAL;
4002         }
4003
4004         /* initialize the registry file for further processing */
4005         if (!ulp_regfile_init(parms.regfile)) {
4006                 BNXT_TF_DBG(ERR, "regfile initialization failed.\n");
4007                 return -EINVAL;
4008         }
4009
4010         /* Process the action template list from the selected action table*/
4011         if (parms.act_tid) {
4012                 parms.tmpl_type = BNXT_ULP_TEMPLATE_TYPE_ACTION;
4013                 /* Process the action template tables */
4014                 rc = ulp_mapper_tbls_process(&parms, parms.act_tid);
4015                 if (rc)
4016                         goto flow_error;
4017                 cparms->shared_hndl = parms.shared_hndl;
4018         }
4019
4020         if (parms.class_tid) {
4021                 parms.tmpl_type = BNXT_ULP_TEMPLATE_TYPE_CLASS;
4022
4023                 /* Process the class template tables.*/
4024                 rc = ulp_mapper_tbls_process(&parms, parms.class_tid);
4025                 if (rc)
4026                         goto flow_error;
4027         }
4028
4029         /* setup the parent-child details */
4030         if (parms.parent_flow) {
4031                 /* create a parent flow details */
4032                 rc = ulp_flow_db_parent_flow_create(&parms);
4033                 if (rc)
4034                         goto flow_error;
4035         } else if (parms.child_flow) {
4036                 /* create a child flow details */
4037                 rc = ulp_flow_db_child_flow_create(&parms);
4038                 if (rc)
4039                         goto flow_error;
4040         }
4041
4042         return rc;
4043
4044 flow_error:
4045         /* Free all resources that were allocated during flow creation */
4046         trc = ulp_mapper_flow_destroy(ulp_ctx, parms.flow_type,
4047                                       parms.fid);
4048         if (trc)
4049                 BNXT_TF_DBG(ERR, "Failed to free all resources rc=%d\n", trc);
4050
4051         return rc;
4052 }
4053
4054 int32_t
4055 ulp_mapper_init(struct bnxt_ulp_context *ulp_ctx)
4056 {
4057         struct bnxt_ulp_mapper_data *data;
4058         struct tf *tfp;
4059         int32_t rc;
4060
4061         if (!ulp_ctx)
4062                 return -EINVAL;
4063
4064         tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx, BNXT_ULP_SHARED_SESSION_NO);
4065         if (!tfp)
4066                 return -EINVAL;
4067
4068         data = rte_zmalloc("ulp_mapper_data",
4069                            sizeof(struct bnxt_ulp_mapper_data), 0);
4070         if (!data) {
4071                 BNXT_TF_DBG(ERR, "Failed to allocate the mapper data\n");
4072                 return -ENOMEM;
4073         }
4074
4075         if (bnxt_ulp_cntxt_ptr2_mapper_data_set(ulp_ctx, data)) {
4076                 BNXT_TF_DBG(ERR, "Failed to set mapper data in context\n");
4077                 /* Don't call deinit since the prof_func wasn't allocated. */
4078                 rte_free(data);
4079                 return -ENOMEM;
4080         }
4081
4082         /* Allocate the global resource ids */
4083         rc = ulp_mapper_glb_resource_info_init(ulp_ctx, data);
4084         if (rc) {
4085                 BNXT_TF_DBG(ERR, "Failed to initialize global resource ids\n");
4086                 goto error;
4087         }
4088
4089         /*
4090          * Only initialize the app global resources if a shared session was
4091          * created.
4092          */
4093         if (bnxt_ulp_cntxt_shared_session_enabled(ulp_ctx)) {
4094                 rc = ulp_mapper_app_glb_resource_info_init(ulp_ctx, data);
4095                 if (rc) {
4096                         BNXT_TF_DBG(ERR, "Failed to init app glb resources\n");
4097                         goto error;
4098                 }
4099         }
4100
4101         /* Allocate the generic table list */
4102         rc = ulp_mapper_generic_tbl_list_init(data);
4103         if (rc) {
4104                 BNXT_TF_DBG(ERR, "Failed to initialize generic tbl list\n");
4105                 goto error;
4106         }
4107
4108         return 0;
4109 error:
4110         /* Ignore the return code in favor of returning the original error. */
4111         ulp_mapper_deinit(ulp_ctx);
4112         return rc;
4113 }
4114
4115 void
4116 ulp_mapper_deinit(struct bnxt_ulp_context *ulp_ctx)
4117 {
4118         struct bnxt_ulp_mapper_data *data;
4119         struct tf *tfp;
4120
4121         if (!ulp_ctx) {
4122                 BNXT_TF_DBG(ERR,
4123                             "Failed to acquire ulp context, so data may not be released.\n");
4124                 return;
4125         }
4126
4127         data = (struct bnxt_ulp_mapper_data *)
4128                 bnxt_ulp_cntxt_ptr2_mapper_data_get(ulp_ctx);
4129         if (!data) {
4130                 /* Go ahead and return since there is no allocated data. */
4131                 BNXT_TF_DBG(ERR, "No data appears to have been allocated.\n");
4132                 return;
4133         }
4134
4135         tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx, BNXT_ULP_SHARED_SESSION_NO);
4136         if (!tfp) {
4137                 BNXT_TF_DBG(ERR, "Failed to acquire tfp.\n");
4138                 /* Free the mapper data regardless of errors. */
4139                 goto free_mapper_data;
4140         }
4141
4142         /* Free the global resource info table entries */
4143         ulp_mapper_glb_resource_info_deinit(ulp_ctx, data);
4144
4145 free_mapper_data:
4146         /* Free the generic table */
4147         (void)ulp_mapper_generic_tbl_list_deinit(data);
4148
4149         rte_free(data);
4150         /* Reset the data pointer within the ulp_ctx. */
4151         bnxt_ulp_cntxt_ptr2_mapper_data_set(ulp_ctx, NULL);
4152 }