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