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