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