10e7186c5082af4ee3c8a0ac8efea928c198b157
[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 conditional opcode of the mapper table.
2013  * returns 1 to skip the table.
2014  * return 0 to continue processing the table.
2015  */
2016 static int32_t
2017 ulp_mapper_tbl_cond_opcode_process(struct bnxt_ulp_mapper_parms *parms,
2018                                    struct bnxt_ulp_mapper_tbl_info *tbl)
2019 {
2020         int32_t rc = 1;
2021
2022         switch (tbl->cond_opcode) {
2023         case BNXT_ULP_COND_OPCODE_NOP:
2024                 rc = 0;
2025                 break;
2026         case BNXT_ULP_COND_OPCODE_COMP_FIELD:
2027                 if (tbl->cond_operand < BNXT_ULP_CF_IDX_LAST &&
2028                     ULP_COMP_FLD_IDX_RD(parms, tbl->cond_operand))
2029                         rc = 0;
2030                 break;
2031         case BNXT_ULP_COND_OPCODE_ACTION_BIT:
2032                 if (ULP_BITMAP_ISSET(parms->act_bitmap->bits,
2033                                      tbl->cond_operand))
2034                         rc = 0;
2035                 break;
2036         case BNXT_ULP_COND_OPCODE_HDR_BIT:
2037                 if (ULP_BITMAP_ISSET(parms->hdr_bitmap->bits,
2038                                      tbl->cond_operand))
2039                         rc = 0;
2040                 break;
2041         default:
2042                 BNXT_TF_DBG(ERR,
2043                             "Invalid arg in mapper tbl for cond opcode\n");
2044                 break;
2045         }
2046         return rc;
2047 }
2048
2049 /*
2050  * Function to process the action template. Iterate through the list
2051  * action info templates and process it.
2052  */
2053 static int32_t
2054 ulp_mapper_action_tbls_process(struct bnxt_ulp_mapper_parms *parms)
2055 {
2056         uint32_t        i;
2057         int32_t         rc = 0;
2058         struct bnxt_ulp_mapper_tbl_info *tbl;
2059
2060         if (!parms->atbls || !parms->num_atbls) {
2061                 BNXT_TF_DBG(ERR, "No action tables for template[%d][%d].\n",
2062                             parms->dev_id, parms->act_tid);
2063                 return -EINVAL;
2064         }
2065
2066         for (i = 0; i < parms->num_atbls; i++) {
2067                 tbl = &parms->atbls[i];
2068                 if (ulp_mapper_tbl_cond_opcode_process(parms, tbl))
2069                         continue;
2070
2071                 switch (tbl->resource_func) {
2072                 case BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE:
2073                         rc = ulp_mapper_index_tbl_process(parms, tbl, false);
2074                         if (rc) {
2075                                 BNXT_TF_DBG(ERR, "Resource type %d failed\n",
2076                                             tbl->resource_func);
2077                                 return rc;
2078                         }
2079                         break;
2080                 default:
2081                         BNXT_TF_DBG(ERR, "Unexpected action resource %d\n",
2082                                     tbl->resource_func);
2083                         return -EINVAL;
2084                 }
2085         }
2086
2087         return rc;
2088 }
2089
2090 /* Create the classifier table entries for a flow. */
2091 static int32_t
2092 ulp_mapper_class_tbls_process(struct bnxt_ulp_mapper_parms *parms)
2093 {
2094         uint32_t        i;
2095         int32_t         rc = 0;
2096
2097         if (!parms)
2098                 return -EINVAL;
2099
2100         if (!parms->ctbls || !parms->num_ctbls) {
2101                 BNXT_TF_DBG(ERR, "No class tables for template[%d][%d].\n",
2102                             parms->dev_id, parms->class_tid);
2103                 return -EINVAL;
2104         }
2105
2106         for (i = 0; i < parms->num_ctbls; i++) {
2107                 struct bnxt_ulp_mapper_tbl_info *tbl = &parms->ctbls[i];
2108
2109                 if (ulp_mapper_tbl_cond_opcode_process(parms, tbl))
2110                         continue;
2111
2112                 switch (tbl->resource_func) {
2113                 case BNXT_ULP_RESOURCE_FUNC_TCAM_TABLE:
2114                         rc = ulp_mapper_tcam_tbl_process(parms, tbl);
2115                         break;
2116                 case BNXT_ULP_RESOURCE_FUNC_EXT_EM_TABLE:
2117                 case BNXT_ULP_RESOURCE_FUNC_INT_EM_TABLE:
2118                         rc = ulp_mapper_em_tbl_process(parms, tbl);
2119                         break;
2120                 case BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE:
2121                         rc = ulp_mapper_index_tbl_process(parms, tbl, true);
2122                         break;
2123                 case BNXT_ULP_RESOURCE_FUNC_CACHE_TABLE:
2124                         rc = ulp_mapper_cache_tbl_process(parms, tbl);
2125                         break;
2126                 case BNXT_ULP_RESOURCE_FUNC_IF_TABLE:
2127                         rc = ulp_mapper_if_tbl_process(parms, tbl);
2128                         break;
2129                 default:
2130                         BNXT_TF_DBG(ERR, "Unexpected class resource %d\n",
2131                                     tbl->resource_func);
2132                         return -EINVAL;
2133                 }
2134
2135                 if (rc) {
2136                         BNXT_TF_DBG(ERR, "Resource type %d failed\n",
2137                                     tbl->resource_func);
2138                         return rc;
2139                 }
2140         }
2141
2142         return rc;
2143 }
2144
2145 static int32_t
2146 ulp_mapper_resource_free(struct bnxt_ulp_context *ulp,
2147                          struct ulp_flow_db_res_params *res)
2148 {
2149         struct tf *tfp;
2150         int32_t rc = 0;
2151
2152         if (!res || !ulp) {
2153                 BNXT_TF_DBG(ERR, "Unable to free resource\n ");
2154                 return -EINVAL;
2155         }
2156
2157         tfp = bnxt_ulp_cntxt_tfp_get(ulp);
2158         if (!tfp) {
2159                 BNXT_TF_DBG(ERR, "Unable to free resource failed to get tfp\n");
2160                 return -EINVAL;
2161         }
2162
2163         switch (res->resource_func) {
2164         case BNXT_ULP_RESOURCE_FUNC_CACHE_TABLE:
2165                 rc = ulp_mapper_cache_entry_free(ulp, tfp, res);
2166                 break;
2167         case BNXT_ULP_RESOURCE_FUNC_TCAM_TABLE:
2168                 rc = ulp_mapper_tcam_entry_free(ulp, tfp, res);
2169                 break;
2170         case BNXT_ULP_RESOURCE_FUNC_EXT_EM_TABLE:
2171         case BNXT_ULP_RESOURCE_FUNC_INT_EM_TABLE:
2172                 rc = ulp_mapper_em_entry_free(ulp, tfp, res);
2173                 break;
2174         case BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE:
2175                 rc = ulp_mapper_index_entry_free(ulp, tfp, res);
2176                 break;
2177         case BNXT_ULP_RESOURCE_FUNC_IDENTIFIER:
2178                 rc = ulp_mapper_ident_free(ulp, tfp, res);
2179                 break;
2180         case BNXT_ULP_RESOURCE_FUNC_HW_FID:
2181                 rc = ulp_mapper_mark_free(ulp, res);
2182                 break;
2183         default:
2184                 break;
2185         }
2186
2187         return rc;
2188 }
2189
2190 int32_t
2191 ulp_mapper_resources_free(struct bnxt_ulp_context       *ulp_ctx,
2192                           uint32_t fid,
2193                           enum bnxt_ulp_flow_db_tables  tbl_type)
2194 {
2195         struct ulp_flow_db_res_params   res_parms = { 0 };
2196         int32_t                         rc, trc;
2197
2198         if (!ulp_ctx) {
2199                 BNXT_TF_DBG(ERR, "Invalid parms, unable to free flow\n");
2200                 return -EINVAL;
2201         }
2202
2203         /*
2204          * Set the critical resource on the first resource del, then iterate
2205          * while status is good
2206          */
2207         res_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_YES;
2208         rc = ulp_flow_db_resource_del(ulp_ctx, tbl_type, fid, &res_parms);
2209
2210         if (rc) {
2211                 /*
2212                  * This is unexpected on the first call to resource del.
2213                  * It likely means that the flow did not exist in the flow db.
2214                  */
2215                 BNXT_TF_DBG(ERR, "Flow[%d][0x%08x] failed to free (rc=%d)\n",
2216                             tbl_type, fid, rc);
2217                 return rc;
2218         }
2219
2220         while (!rc) {
2221                 trc = ulp_mapper_resource_free(ulp_ctx, &res_parms);
2222                 if (trc)
2223                         /*
2224                          * On fail, we still need to attempt to free the
2225                          * remaining resources.  Don't return
2226                          */
2227                         BNXT_TF_DBG(ERR,
2228                                     "Flow[%d][0x%x] Res[%d][0x%016" PRIx64
2229                                     "] failed rc=%d.\n",
2230                                     tbl_type, fid, res_parms.resource_func,
2231                                     res_parms.resource_hndl, trc);
2232
2233                 /* All subsequent call require the non-critical_resource */
2234                 res_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
2235
2236                 rc = ulp_flow_db_resource_del(ulp_ctx,
2237                                               tbl_type,
2238                                               fid,
2239                                               &res_parms);
2240         }
2241
2242         /* Free the Flow ID since we've removed all resources */
2243         rc = ulp_flow_db_fid_free(ulp_ctx, tbl_type, fid);
2244
2245         return rc;
2246 }
2247
2248 static void
2249 ulp_mapper_glb_resource_info_deinit(struct bnxt_ulp_context *ulp_ctx,
2250                                     struct bnxt_ulp_mapper_data *mapper_data)
2251 {
2252         struct bnxt_ulp_mapper_glb_resource_entry *ent;
2253         struct ulp_flow_db_res_params res;
2254         uint32_t dir, idx;
2255
2256         /* Iterate the global resources and process each one */
2257         for (dir = TF_DIR_RX; dir < TF_DIR_MAX; dir++) {
2258                 for (idx = 0; idx < BNXT_ULP_GLB_RESOURCE_INFO_TBL_MAX_SZ;
2259                       idx++) {
2260                         ent = &mapper_data->glb_res_tbl[dir][idx];
2261                         if (ent->resource_func ==
2262                             BNXT_ULP_RESOURCE_FUNC_INVALID)
2263                                 continue;
2264                         memset(&res, 0, sizeof(struct ulp_flow_db_res_params));
2265                         res.resource_func = ent->resource_func;
2266                         res.direction = dir;
2267                         res.resource_type = ent->resource_type;
2268                         /*convert it from BE to cpu */
2269                         res.resource_hndl =
2270                                 tfp_be_to_cpu_64(ent->resource_hndl);
2271                         ulp_mapper_resource_free(ulp_ctx, &res);
2272                 }
2273         }
2274 }
2275
2276 int32_t
2277 ulp_mapper_flow_destroy(struct bnxt_ulp_context *ulp_ctx, uint32_t fid)
2278 {
2279         if (!ulp_ctx) {
2280                 BNXT_TF_DBG(ERR, "Invalid parms, unable to free flow\n");
2281                 return -EINVAL;
2282         }
2283
2284         return ulp_mapper_resources_free(ulp_ctx,
2285                                          fid,
2286                                          BNXT_ULP_REGULAR_FLOW_TABLE);
2287 }
2288
2289 /* Function to handle the default global templates that are allocated during
2290  * the startup and reused later.
2291  */
2292 static int32_t
2293 ulp_mapper_glb_template_table_init(struct bnxt_ulp_context *ulp_ctx)
2294 {
2295         uint32_t *glbl_tmpl_list;
2296         uint32_t num_glb_tmpls, idx, dev_id;
2297         struct bnxt_ulp_mapper_parms parms;
2298         struct bnxt_ulp_mapper_data *mapper_data;
2299         int32_t rc = 0;
2300
2301         glbl_tmpl_list = ulp_mapper_glb_template_table_get(&num_glb_tmpls);
2302         if (!glbl_tmpl_list || !num_glb_tmpls)
2303                 return rc; /* No global templates to process */
2304
2305         /* Get the device id from the ulp context */
2306         if (bnxt_ulp_cntxt_dev_id_get(ulp_ctx, &dev_id)) {
2307                 BNXT_TF_DBG(ERR, "Invalid ulp context\n");
2308                 return -EINVAL;
2309         }
2310
2311         mapper_data = bnxt_ulp_cntxt_ptr2_mapper_data_get(ulp_ctx);
2312         if (!mapper_data) {
2313                 BNXT_TF_DBG(ERR, "Failed to get the ulp mapper data\n");
2314                 return -EINVAL;
2315         }
2316
2317         /* Iterate the global resources and process each one */
2318         for (idx = 0; idx < num_glb_tmpls; idx++) {
2319                 /* Initialize the parms structure */
2320                 memset(&parms, 0, sizeof(parms));
2321                 parms.tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx);
2322                 parms.ulp_ctx = ulp_ctx;
2323                 parms.dev_id = dev_id;
2324                 parms.mapper_data = mapper_data;
2325
2326                 /* Get the class table entry from dev id and class id */
2327                 parms.class_tid = glbl_tmpl_list[idx];
2328                 parms.ctbls = ulp_mapper_class_tbl_list_get(parms.dev_id,
2329                                                             parms.class_tid,
2330                                                             &parms.num_ctbls,
2331                                                             &parms.tbl_idx);
2332                 if (!parms.ctbls || !parms.num_ctbls) {
2333                         BNXT_TF_DBG(ERR, "No class tables for %d:%d\n",
2334                                     parms.dev_id, parms.class_tid);
2335                         return -EINVAL;
2336                 }
2337                 parms.device_params = bnxt_ulp_device_params_get(parms.dev_id);
2338                 if (!parms.device_params) {
2339                         BNXT_TF_DBG(ERR, "No class tables for %d:%d\n",
2340                                     parms.dev_id, parms.class_tid);
2341                         return -EINVAL;
2342                 }
2343                 rc = ulp_mapper_class_tbls_process(&parms);
2344                 if (rc) {
2345                         BNXT_TF_DBG(ERR,
2346                                     "class tables failed creation for %d:%d\n",
2347                                     parms.dev_id, parms.class_tid);
2348                         return rc;
2349                 }
2350         }
2351         return rc;
2352 }
2353
2354 /* Function to handle the mapping of the Flow to be compatible
2355  * with the underlying hardware.
2356  */
2357 int32_t
2358 ulp_mapper_flow_create(struct bnxt_ulp_context *ulp_ctx,
2359                        struct bnxt_ulp_mapper_create_parms *cparms,
2360                        uint32_t *flowid)
2361 {
2362         struct bnxt_ulp_mapper_parms parms;
2363         struct ulp_regfile regfile;
2364         int32_t  rc, trc;
2365
2366         if (!ulp_ctx || !cparms)
2367                 return -EINVAL;
2368
2369         /* Initialize the parms structure */
2370         memset(&parms, 0, sizeof(parms));
2371         parms.act_prop = cparms->act_prop;
2372         parms.act_bitmap = cparms->act;
2373         parms.hdr_bitmap = cparms->hdr_bitmap;
2374         parms.regfile = &regfile;
2375         parms.hdr_field = cparms->hdr_field;
2376         parms.comp_fld = cparms->comp_fld;
2377         parms.tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx);
2378         parms.ulp_ctx = ulp_ctx;
2379         parms.tcam_tbl_opc = BNXT_ULP_MAPPER_TCAM_TBL_OPC_NORMAL;
2380
2381         /* Get the device id from the ulp context */
2382         if (bnxt_ulp_cntxt_dev_id_get(ulp_ctx, &parms.dev_id)) {
2383                 BNXT_TF_DBG(ERR, "Invalid ulp context\n");
2384                 return -EINVAL;
2385         }
2386
2387         /*
2388          * Get the mapper data for dynamic mapper data such as default
2389          * ids.
2390          */
2391         parms.mapper_data = (struct bnxt_ulp_mapper_data *)
2392                 bnxt_ulp_cntxt_ptr2_mapper_data_get(ulp_ctx);
2393         if (!parms.mapper_data) {
2394                 BNXT_TF_DBG(ERR, "Failed to get the ulp mapper data\n");
2395                 return -EINVAL;
2396         }
2397
2398         /* Get the action table entry from device id and act context id */
2399         parms.act_tid = cparms->act_tid;
2400
2401         /*
2402          * Perform the action table get only if act template is not zero
2403          * for act template zero like for default rules ignore the action
2404          * table processing.
2405          */
2406         if (parms.act_tid) {
2407                 parms.atbls = ulp_mapper_action_tbl_list_get(parms.dev_id,
2408                                                              parms.act_tid,
2409                                                              &parms.num_atbls);
2410                 if (!parms.atbls || !parms.num_atbls) {
2411                         BNXT_TF_DBG(ERR, "No action tables for %d:%d\n",
2412                                     parms.dev_id, parms.act_tid);
2413                         return -EINVAL;
2414                 }
2415         }
2416
2417         /* Get the class table entry from device id and act context id */
2418         parms.class_tid = cparms->class_tid;
2419         parms.ctbls = ulp_mapper_class_tbl_list_get(parms.dev_id,
2420                                                     parms.class_tid,
2421                                                     &parms.num_ctbls,
2422                                                     &parms.tbl_idx);
2423         if (!parms.ctbls || !parms.num_ctbls) {
2424                 BNXT_TF_DBG(ERR, "No class tables for %d:%d\n",
2425                             parms.dev_id, parms.class_tid);
2426                 return -EINVAL;
2427         }
2428
2429         /* Get the device params, it will be used in later processing */
2430         parms.device_params = bnxt_ulp_device_params_get(parms.dev_id);
2431         if (!parms.device_params) {
2432                 BNXT_TF_DBG(ERR, "No class tables for %d:%d\n",
2433                             parms.dev_id, parms.class_tid);
2434                 return -EINVAL;
2435         }
2436
2437         /* initialize the registry file for further processing */
2438         if (!ulp_regfile_init(parms.regfile)) {
2439                 BNXT_TF_DBG(ERR, "regfile initialization failed.\n");
2440                 return -EINVAL;
2441         }
2442
2443         rc = ulp_regfile_write(parms.regfile,
2444                                BNXT_ULP_REGFILE_INDEX_CLASS_TID,
2445                                tfp_cpu_to_be_64((uint64_t)parms.class_tid));
2446         if (!rc) {
2447                 BNXT_TF_DBG(ERR, "Unable to write template ID to regfile\n");
2448                 return -EINVAL;
2449         }
2450
2451         /* Allocate a Flow ID for attaching all resources for the flow to.
2452          * Once allocated, all errors have to walk the list of resources and
2453          * free each of them.
2454          */
2455         rc = ulp_flow_db_fid_alloc(ulp_ctx,
2456                                    parms.tbl_idx,
2457                                    cparms->func_id,
2458                                    &parms.fid);
2459         if (rc) {
2460                 BNXT_TF_DBG(ERR, "Unable to allocate flow table entry\n");
2461                 return rc;
2462         }
2463
2464         /* Process the action template list from the selected action table*/
2465         if (parms.act_tid) {
2466                 rc = ulp_mapper_action_tbls_process(&parms);
2467                 if (rc) {
2468                         BNXT_TF_DBG(ERR,
2469                                     "action tables failed creation for %d:%d\n",
2470                                     parms.dev_id, parms.act_tid);
2471                         goto flow_error;
2472                 }
2473         }
2474
2475         /* All good. Now process the class template */
2476         rc = ulp_mapper_class_tbls_process(&parms);
2477         if (rc) {
2478                 BNXT_TF_DBG(ERR, "class tables failed creation for %d:%d\n",
2479                             parms.dev_id, parms.class_tid);
2480                 goto flow_error;
2481         }
2482
2483         *flowid = parms.fid;
2484
2485         return rc;
2486
2487 flow_error:
2488         /* Free all resources that were allocated during flow creation */
2489         trc = ulp_mapper_flow_destroy(ulp_ctx, parms.fid);
2490         if (trc)
2491                 BNXT_TF_DBG(ERR, "Failed to free all resources rc=%d\n", trc);
2492
2493         return rc;
2494 }
2495
2496 int32_t
2497 ulp_mapper_init(struct bnxt_ulp_context *ulp_ctx)
2498 {
2499         struct bnxt_ulp_cache_tbl_params *tbl;
2500         struct bnxt_ulp_mapper_data *data;
2501         uint32_t i;
2502         struct tf *tfp;
2503         int32_t rc, csize;
2504
2505         if (!ulp_ctx)
2506                 return -EINVAL;
2507
2508         tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx);
2509         if (!tfp)
2510                 return -EINVAL;
2511
2512         data = rte_zmalloc("ulp_mapper_data",
2513                            sizeof(struct bnxt_ulp_mapper_data), 0);
2514         if (!data) {
2515                 BNXT_TF_DBG(ERR, "Failed to allocate the mapper data\n");
2516                 return -ENOMEM;
2517         }
2518
2519         if (bnxt_ulp_cntxt_ptr2_mapper_data_set(ulp_ctx, data)) {
2520                 BNXT_TF_DBG(ERR, "Failed to set mapper data in context\n");
2521                 /* Don't call deinit since the prof_func wasn't allocated. */
2522                 rte_free(data);
2523                 return -ENOMEM;
2524         }
2525
2526         /* Allocate the global resource ids */
2527         rc = ulp_mapper_glb_resource_info_init(ulp_ctx, data);
2528         if (rc) {
2529                 BNXT_TF_DBG(ERR, "Failed to initialize global resource ids\n");
2530                 goto error;
2531         }
2532
2533         /* Allocate the ulp cache tables. */
2534         for (i = 0; i < BNXT_ULP_CACHE_TBL_MAX_SZ; i++) {
2535                 tbl = ulp_mapper_cache_tbl_params_get(i);
2536                 if (!tbl) {
2537                         BNXT_TF_DBG(ERR, "Failed to get cache table parms (%d)",
2538                                     i);
2539                         goto error;
2540                 }
2541                 if (tbl->num_entries != 0) {
2542                         csize = sizeof(struct bnxt_ulp_mapper_cache_entry) *
2543                                 tbl->num_entries;
2544                         data->cache_tbl[i] = rte_zmalloc("ulp mapper cache tbl",
2545                                                          csize, 0);
2546                         if (!data->cache_tbl[i]) {
2547                                 BNXT_TF_DBG(ERR, "Failed to allocate Cache "
2548                                             "table %d.\n", i);
2549                                 rc = -ENOMEM;
2550                                 goto error;
2551                         }
2552                 }
2553         }
2554
2555         rc = ulp_mapper_glb_template_table_init(ulp_ctx);
2556         if (rc) {
2557                 BNXT_TF_DBG(ERR, "Failed to initialize global templates\n");
2558                 goto error;
2559         }
2560
2561         return 0;
2562 error:
2563         /* Ignore the return code in favor of returning the original error. */
2564         ulp_mapper_deinit(ulp_ctx);
2565         return rc;
2566 }
2567
2568 void
2569 ulp_mapper_deinit(struct bnxt_ulp_context *ulp_ctx)
2570 {
2571         struct bnxt_ulp_mapper_data *data;
2572         uint32_t i;
2573         struct tf *tfp;
2574
2575         if (!ulp_ctx) {
2576                 BNXT_TF_DBG(ERR,
2577                             "Failed to acquire ulp context, so data may "
2578                             "not be released.\n");
2579                 return;
2580         }
2581
2582         data = (struct bnxt_ulp_mapper_data *)
2583                 bnxt_ulp_cntxt_ptr2_mapper_data_get(ulp_ctx);
2584         if (!data) {
2585                 /* Go ahead and return since there is no allocated data. */
2586                 BNXT_TF_DBG(ERR, "No data appears to have been allocated.\n");
2587                 return;
2588         }
2589
2590         tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx);
2591         if (!tfp) {
2592                 BNXT_TF_DBG(ERR, "Failed to acquire tfp.\n");
2593                 /* Free the mapper data regardless of errors. */
2594                 goto free_mapper_data;
2595         }
2596
2597         /* Free the global resource info table entries */
2598         ulp_mapper_glb_resource_info_deinit(ulp_ctx, data);
2599
2600 free_mapper_data:
2601         /* Free the ulp cache tables */
2602         for (i = 0; i < BNXT_ULP_CACHE_TBL_MAX_SZ; i++) {
2603                 rte_free(data->cache_tbl[i]);
2604                 data->cache_tbl[i] = NULL;
2605         }
2606
2607         rte_free(data);
2608         /* Reset the data pointer within the ulp_ctx. */
2609         bnxt_ulp_cntxt_ptr2_mapper_data_set(ulp_ctx, NULL);
2610 }