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