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