net/bnxt: configure PARIF for egress rules
[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         case BNXT_ULP_MAPPER_OPC_IF_COMP_FIELD_THEN_CF_ELSE_CF:
1002                 if (!ulp_operand_read(fld->result_operand,
1003                                       (uint8_t *)&idx,
1004                                       sizeof(uint16_t))) {
1005                         BNXT_TF_DBG(ERR, "%s key operand read failed.\n", name);
1006                         return -EINVAL;
1007                 }
1008                 idx = tfp_be_to_cpu_16(idx);
1009                 if (idx >= BNXT_ULP_CF_IDX_LAST) {
1010                         BNXT_TF_DBG(ERR, "%s invalid index %u\n", name, idx);
1011                         return -EINVAL;
1012                 }
1013                 /* check if the computed field is set */
1014                 if (ULP_COMP_FLD_IDX_RD(parms, idx))
1015                         val = fld->result_operand_true;
1016                 else
1017                         val = fld->result_operand_false;
1018
1019                 /* read the appropriate computed field */
1020                 if (!ulp_operand_read(val, (uint8_t *)&idx, sizeof(uint16_t))) {
1021                         BNXT_TF_DBG(ERR, "%s val operand read failed\n", name);
1022                         return -EINVAL;
1023                 }
1024                 idx = tfp_be_to_cpu_16(idx);
1025                 if (idx >= BNXT_ULP_CF_IDX_LAST) {
1026                         BNXT_TF_DBG(ERR, "%s invalid index %u\n", name, idx);
1027                         return -EINVAL;
1028                 }
1029                 val = ulp_blob_push_32(blob, &parms->comp_fld[idx],
1030                                        fld->field_bit_size);
1031                 if (!val) {
1032                         BNXT_TF_DBG(ERR, "%s push to key blob failed\n", name);
1033                         return -EINVAL;
1034                 }
1035                 break;
1036         default:
1037                 BNXT_TF_DBG(ERR, "invalid result mapper opcode 0x%x\n",
1038                             fld->result_opcode);
1039                 return -EINVAL;
1040         }
1041         return 0;
1042 }
1043
1044 /* Function to alloc action record and set the table. */
1045 static int32_t
1046 ulp_mapper_keymask_field_process(struct bnxt_ulp_mapper_parms *parms,
1047                                  enum tf_dir dir,
1048                                  struct bnxt_ulp_mapper_class_key_field_info *f,
1049                                  struct ulp_blob *blob,
1050                                  uint8_t is_key,
1051                                  const char *name)
1052 {
1053         uint64_t val64;
1054         uint16_t idx, bitlen;
1055         uint32_t opcode;
1056         uint8_t *operand;
1057         struct ulp_regfile *regfile = parms->regfile;
1058         uint8_t *val = NULL;
1059         struct bnxt_ulp_mapper_class_key_field_info *fld = f;
1060         uint32_t field_size;
1061
1062         if (is_key) {
1063                 operand = fld->spec_operand;
1064                 opcode  = fld->spec_opcode;
1065         } else {
1066                 operand = fld->mask_operand;
1067                 opcode  = fld->mask_opcode;
1068         }
1069
1070         bitlen = fld->field_bit_size;
1071
1072         switch (opcode) {
1073         case BNXT_ULP_MAPPER_OPC_SET_TO_CONSTANT:
1074                 val = operand;
1075                 if (!ulp_blob_push(blob, val, bitlen)) {
1076                         BNXT_TF_DBG(ERR, "%s push to key blob failed\n", name);
1077                         return -EINVAL;
1078                 }
1079                 break;
1080         case BNXT_ULP_MAPPER_OPC_SET_TO_ZERO:
1081                 if (ulp_blob_pad_push(blob, bitlen) < 0) {
1082                         BNXT_TF_DBG(ERR, "%s pad too large for blob\n", name);
1083                         return -EINVAL;
1084                 }
1085
1086                 break;
1087         case BNXT_ULP_MAPPER_OPC_SET_TO_HDR_FIELD:
1088                 if (!ulp_operand_read(operand, (uint8_t *)&idx,
1089                                       sizeof(uint16_t))) {
1090                         BNXT_TF_DBG(ERR, "%s key operand read failed.\n", name);
1091                         return -EINVAL;
1092                 }
1093                 idx = tfp_be_to_cpu_16(idx);
1094                 if (is_key)
1095                         val = parms->hdr_field[idx].spec;
1096                 else
1097                         val = parms->hdr_field[idx].mask;
1098
1099                 /*
1100                  * Need to account for how much data was pushed to the header
1101                  * field vs how much is to be inserted in the key/mask.
1102                  */
1103                 field_size = parms->hdr_field[idx].size;
1104                 if (bitlen < ULP_BYTE_2_BITS(field_size)) {
1105                         field_size  = field_size - ((bitlen + 7) / 8);
1106                         val += field_size;
1107                 }
1108
1109                 if (!ulp_blob_push(blob, val, bitlen)) {
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_COMP_FIELD:
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 (idx < BNXT_ULP_CF_IDX_LAST)
1122                         val = ulp_blob_push_32(blob, &parms->comp_fld[idx],
1123                                                bitlen);
1124                 if (!val) {
1125                         BNXT_TF_DBG(ERR, "%s push to key blob failed\n", name);
1126                         return -EINVAL;
1127                 }
1128                 break;
1129         case BNXT_ULP_MAPPER_OPC_SET_TO_REGFILE:
1130                 if (!ulp_operand_read(operand, (uint8_t *)&idx,
1131                                       sizeof(uint16_t))) {
1132                         BNXT_TF_DBG(ERR, "%s key operand read failed.\n", name);
1133                         return -EINVAL;
1134                 }
1135                 idx = tfp_be_to_cpu_16(idx);
1136
1137                 if (!ulp_regfile_read(regfile, idx, &val64)) {
1138                         BNXT_TF_DBG(ERR, "%s regfile[%d] read failed.\n",
1139                                     name, idx);
1140                         return -EINVAL;
1141                 }
1142
1143                 val = ulp_blob_push_64(blob, &val64, bitlen);
1144                 if (!val) {
1145                         BNXT_TF_DBG(ERR, "%s push to key blob failed\n", name);
1146                         return -EINVAL;
1147                 }
1148                 break;
1149         case BNXT_ULP_MAPPER_OPC_SET_TO_GLB_REGFILE:
1150                 if (!ulp_operand_read(operand, (uint8_t *)&idx,
1151                                       sizeof(uint16_t))) {
1152                         BNXT_TF_DBG(ERR, "%s key operand read failed.\n", name);
1153                         return -EINVAL;
1154                 }
1155                 idx = tfp_be_to_cpu_16(idx);
1156                 if (ulp_mapper_glb_resource_read(parms->mapper_data,
1157                                                  dir,
1158                                                  idx, &val64)) {
1159                         BNXT_TF_DBG(ERR, "%s regfile[%d] read failed.\n",
1160                                     name, idx);
1161                         return -EINVAL;
1162                 }
1163                 val = ulp_blob_push_64(blob, &val64, bitlen);
1164                 if (!val) {
1165                         BNXT_TF_DBG(ERR, "%s push to key blob failed\n", name);
1166                         return -EINVAL;
1167                 }
1168                 break;
1169         default:
1170                 BNXT_TF_DBG(ERR, "invalid keymask mapper opcode 0x%x\n",
1171                             opcode);
1172                 return -EINVAL;
1173                 break;
1174         }
1175
1176         return 0;
1177 }
1178
1179 static int32_t
1180 ulp_mapper_mark_gfid_process(struct bnxt_ulp_mapper_parms *parms,
1181                              struct bnxt_ulp_mapper_tbl_info *tbl,
1182                              uint64_t flow_id)
1183 {
1184         enum bnxt_ulp_mark_db_opcode mark_op = tbl->mark_db_opcode;
1185         struct ulp_flow_db_res_params fid_parms;
1186         uint32_t mark, gfid, mark_flag;
1187         int32_t rc = 0;
1188
1189         if (mark_op == BNXT_ULP_MARK_DB_OPCODE_NOP ||
1190             !(mark_op == BNXT_ULP_MARK_DB_OPCODE_SET_IF_MARK_ACTION &&
1191               ULP_BITMAP_ISSET(parms->act_bitmap->bits,
1192                                BNXT_ULP_ACTION_BIT_MARK)))
1193                 return rc; /* no need to perform gfid process */
1194
1195         /* Get the mark id details from action property */
1196         memcpy(&mark, &parms->act_prop->act_details[BNXT_ULP_ACT_PROP_IDX_MARK],
1197                sizeof(mark));
1198         mark = tfp_be_to_cpu_32(mark);
1199
1200         TF_GET_GFID_FROM_FLOW_ID(flow_id, gfid);
1201         mark_flag  = BNXT_ULP_MARK_GLOBAL_HW_FID;
1202         rc = ulp_mark_db_mark_add(parms->ulp_ctx, mark_flag,
1203                                   gfid, mark);
1204         if (rc) {
1205                 BNXT_TF_DBG(ERR, "Failed to add mark to flow\n");
1206                 return rc;
1207         }
1208         fid_parms.direction = tbl->direction;
1209         fid_parms.resource_func = BNXT_ULP_RESOURCE_FUNC_HW_FID;
1210         fid_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
1211         fid_parms.resource_type = mark_flag;
1212         fid_parms.resource_hndl = gfid;
1213         rc = ulp_flow_db_resource_add(parms->ulp_ctx,
1214                                       parms->tbl_idx,
1215                                       parms->fid,
1216                                       &fid_parms);
1217         if (rc)
1218                 BNXT_TF_DBG(ERR, "Fail to link res to flow rc = %d\n", rc);
1219         return rc;
1220 }
1221
1222 static int32_t
1223 ulp_mapper_mark_act_ptr_process(struct bnxt_ulp_mapper_parms *parms,
1224                                 struct bnxt_ulp_mapper_tbl_info *tbl)
1225 {
1226         enum bnxt_ulp_mark_db_opcode mark_op = tbl->mark_db_opcode;
1227         struct ulp_flow_db_res_params fid_parms;
1228         uint32_t act_idx, mark, mark_flag;
1229         uint64_t val64;
1230         int32_t rc = 0;
1231
1232         if (mark_op == BNXT_ULP_MARK_DB_OPCODE_NOP ||
1233             !(mark_op == BNXT_ULP_MARK_DB_OPCODE_SET_IF_MARK_ACTION &&
1234               ULP_BITMAP_ISSET(parms->act_bitmap->bits,
1235                                BNXT_ULP_ACTION_BIT_MARK)))
1236                 return rc; /* no need to perform mark action process */
1237
1238         /* Get the mark id details from action property */
1239         memcpy(&mark, &parms->act_prop->act_details[BNXT_ULP_ACT_PROP_IDX_MARK],
1240                sizeof(mark));
1241         mark = tfp_be_to_cpu_32(mark);
1242
1243         if (!ulp_regfile_read(parms->regfile,
1244                               BNXT_ULP_REGFILE_INDEX_MAIN_ACTION_PTR,
1245                               &val64)) {
1246                 BNXT_TF_DBG(ERR, "read action ptr main failed\n");
1247                 return -EINVAL;
1248         }
1249         act_idx = tfp_be_to_cpu_64(val64);
1250         mark_flag  = BNXT_ULP_MARK_LOCAL_HW_FID;
1251         rc = ulp_mark_db_mark_add(parms->ulp_ctx, mark_flag,
1252                                   act_idx, mark);
1253         if (rc) {
1254                 BNXT_TF_DBG(ERR, "Failed to add mark to flow\n");
1255                 return rc;
1256         }
1257         fid_parms.direction = tbl->direction;
1258         fid_parms.resource_func = BNXT_ULP_RESOURCE_FUNC_HW_FID;
1259         fid_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
1260         fid_parms.resource_type = mark_flag;
1261         fid_parms.resource_hndl = act_idx;
1262         rc = ulp_flow_db_resource_add(parms->ulp_ctx,
1263                                       parms->tbl_idx,
1264                                       parms->fid,
1265                                       &fid_parms);
1266         if (rc)
1267                 BNXT_TF_DBG(ERR, "Fail to link res to flow rc = %d\n", rc);
1268         return rc;
1269 }
1270
1271 static int32_t
1272 ulp_mapper_mark_vfr_idx_process(struct bnxt_ulp_mapper_parms *parms,
1273                                 struct bnxt_ulp_mapper_tbl_info *tbl)
1274 {
1275         struct ulp_flow_db_res_params fid_parms;
1276         uint32_t act_idx, mark, mark_flag;
1277         uint64_t val64;
1278         enum bnxt_ulp_mark_db_opcode mark_op = tbl->mark_db_opcode;
1279         int32_t rc = 0;
1280
1281         if (mark_op == BNXT_ULP_MARK_DB_OPCODE_NOP ||
1282             mark_op == BNXT_ULP_MARK_DB_OPCODE_SET_IF_MARK_ACTION)
1283                 return rc; /* no need to perform mark action process */
1284
1285         /* Get the mark id details from the computed field of dev port id */
1286         mark = ULP_COMP_FLD_IDX_RD(parms, BNXT_ULP_CF_IDX_DEV_PORT_ID);
1287
1288          /* Get the main action pointer */
1289         if (!ulp_regfile_read(parms->regfile,
1290                               BNXT_ULP_REGFILE_INDEX_MAIN_ACTION_PTR,
1291                               &val64)) {
1292                 BNXT_TF_DBG(ERR, "read action ptr main failed\n");
1293                 return -EINVAL;
1294         }
1295         act_idx = tfp_be_to_cpu_64(val64);
1296
1297         /* Set the mark flag to local fid and vfr flag */
1298         mark_flag  = BNXT_ULP_MARK_LOCAL_HW_FID | BNXT_ULP_MARK_VFR_ID;
1299
1300         rc = ulp_mark_db_mark_add(parms->ulp_ctx, mark_flag,
1301                                   act_idx, mark);
1302         if (rc) {
1303                 BNXT_TF_DBG(ERR, "Failed to add mark to flow\n");
1304                 return rc;
1305         }
1306         fid_parms.direction = tbl->direction;
1307         fid_parms.resource_func = BNXT_ULP_RESOURCE_FUNC_HW_FID;
1308         fid_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
1309         fid_parms.resource_type = mark_flag;
1310         fid_parms.resource_hndl = act_idx;
1311         rc = ulp_flow_db_resource_add(parms->ulp_ctx,
1312                                       parms->tbl_idx,
1313                                       parms->fid,
1314                                       &fid_parms);
1315         if (rc)
1316                 BNXT_TF_DBG(ERR, "Fail to link res to flow rc = %d\n", rc);
1317         return rc;
1318 }
1319
1320 static int32_t
1321 ulp_mapper_tcam_tbl_process(struct bnxt_ulp_mapper_parms *parms,
1322                             struct bnxt_ulp_mapper_tbl_info *tbl)
1323 {
1324         struct bnxt_ulp_mapper_class_key_field_info     *kflds;
1325         struct ulp_blob key, mask, data;
1326         uint32_t i, num_kflds;
1327         struct tf *tfp;
1328         int32_t rc, trc;
1329         struct tf_alloc_tcam_entry_parms aparms         = { 0 };
1330         struct tf_search_tcam_entry_parms searchparms   = { 0 };
1331         struct tf_set_tcam_entry_parms sparms           = { 0 };
1332         struct ulp_flow_db_res_params   fid_parms       = { 0 };
1333         struct tf_free_tcam_entry_parms free_parms      = { 0 };
1334         uint32_t hit = 0;
1335         uint16_t tmplen = 0;
1336         uint16_t idx;
1337
1338         /* Skip this if was handled by the cache. */
1339         if (parms->tcam_tbl_opc == BNXT_ULP_MAPPER_TCAM_TBL_OPC_CACHE_SKIP) {
1340                 parms->tcam_tbl_opc = BNXT_ULP_MAPPER_TCAM_TBL_OPC_NORMAL;
1341                 return 0;
1342         }
1343
1344         tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx);
1345         if (!tfp) {
1346                 BNXT_TF_DBG(ERR, "Failed to get truflow pointer\n");
1347                 return -EINVAL;
1348         }
1349
1350         kflds = ulp_mapper_key_fields_get(tbl, &num_kflds);
1351         if (!kflds || !num_kflds) {
1352                 BNXT_TF_DBG(ERR, "Failed to get key fields\n");
1353                 return -EINVAL;
1354         }
1355
1356         if (!ulp_blob_init(&key, tbl->key_bit_size,
1357                            parms->device_params->byte_order) ||
1358             !ulp_blob_init(&mask, tbl->key_bit_size,
1359                            parms->device_params->byte_order) ||
1360             !ulp_blob_init(&data, tbl->result_bit_size,
1361                            parms->device_params->byte_order)) {
1362                 BNXT_TF_DBG(ERR, "blob inits failed.\n");
1363                 return -EINVAL;
1364         }
1365
1366         /* create the key/mask */
1367         /*
1368          * NOTE: The WC table will require some kind of flag to handle the
1369          * mode bits within the key/mask
1370          */
1371         for (i = 0; i < num_kflds; i++) {
1372                 /* Setup the key */
1373                 rc = ulp_mapper_keymask_field_process(parms, tbl->direction,
1374                                                       &kflds[i],
1375                                                       &key, 1, "TCAM Key");
1376                 if (rc) {
1377                         BNXT_TF_DBG(ERR, "Key field set failed.\n");
1378                         return rc;
1379                 }
1380
1381                 /* Setup the mask */
1382                 rc = ulp_mapper_keymask_field_process(parms, tbl->direction,
1383                                                       &kflds[i],
1384                                                       &mask, 0, "TCAM Mask");
1385                 if (rc) {
1386                         BNXT_TF_DBG(ERR, "Mask field set failed.\n");
1387                         return rc;
1388                 }
1389         }
1390
1391         if (!tbl->srch_b4_alloc) {
1392                 /*
1393                  * No search for re-use is requested, so simply allocate the
1394                  * tcam index.
1395                  */
1396                 aparms.dir              = tbl->direction;
1397                 aparms.tcam_tbl_type    = tbl->resource_type;
1398                 aparms.search_enable    = tbl->srch_b4_alloc;
1399                 aparms.key_sz_in_bits   = tbl->key_bit_size;
1400                 aparms.key              = ulp_blob_data_get(&key, &tmplen);
1401                 if (tbl->key_bit_size != tmplen) {
1402                         BNXT_TF_DBG(ERR, "Key len (%d) != Expected (%d)\n",
1403                                     tmplen, tbl->key_bit_size);
1404                         return -EINVAL;
1405                 }
1406
1407                 aparms.mask             = ulp_blob_data_get(&mask, &tmplen);
1408                 if (tbl->key_bit_size != tmplen) {
1409                         BNXT_TF_DBG(ERR, "Mask len (%d) != Expected (%d)\n",
1410                                     tmplen, tbl->key_bit_size);
1411                         return -EINVAL;
1412                 }
1413
1414                 aparms.priority         = tbl->priority;
1415
1416                 /*
1417                  * All failures after this succeeds require the entry to be
1418                  * freed. cannot return directly on failure, but needs to goto
1419                  * error.
1420                  */
1421                 rc = tf_alloc_tcam_entry(tfp, &aparms);
1422                 if (rc) {
1423                         BNXT_TF_DBG(ERR, "tcam alloc failed rc=%d.\n", rc);
1424                         return rc;
1425                 }
1426                 idx = aparms.idx;
1427                 hit = aparms.hit;
1428         } else {
1429                 /*
1430                  * Searching before allocation to see if we already have an
1431                  * entry.  This allows re-use of a constrained resource.
1432                  */
1433                 searchparms.dir = tbl->direction;
1434                 searchparms.tcam_tbl_type = tbl->resource_type;
1435                 searchparms.key = ulp_blob_data_get(&key, &tmplen);
1436                 searchparms.key_sz_in_bits = tbl->key_bit_size;
1437                 searchparms.mask = ulp_blob_data_get(&mask, &tmplen);
1438                 searchparms.priority = tbl->priority;
1439                 searchparms.alloc = 1;
1440                 searchparms.result = ulp_blob_data_get(&data, &tmplen);
1441                 searchparms.result_sz_in_bits = tbl->result_bit_size;
1442
1443                 rc = tf_search_tcam_entry(tfp, &searchparms);
1444                 if (rc) {
1445                         BNXT_TF_DBG(ERR, "tcam search failed rc=%d\n", rc);
1446                         return rc;
1447                 }
1448
1449                 /* Successful search, check the result */
1450                 if (searchparms.search_status == REJECT) {
1451                         BNXT_TF_DBG(ERR, "tcam alloc rejected\n");
1452                         return -ENOMEM;
1453                 }
1454                 idx = searchparms.idx;
1455                 hit = searchparms.hit;
1456         }
1457
1458         /* Build the result */
1459         if (!tbl->srch_b4_alloc || !hit) {
1460                 struct bnxt_ulp_mapper_result_field_info *dflds;
1461                 struct bnxt_ulp_mapper_ident_info *idents;
1462                 uint32_t num_dflds, num_idents;
1463                 uint32_t encap_flds = 0;
1464
1465                 /*
1466                  * Since the cache entry is responsible for allocating
1467                  * identifiers when in use, allocate the identifiers only
1468                  * during normal processing.
1469                  */
1470                 if (parms->tcam_tbl_opc ==
1471                     BNXT_ULP_MAPPER_TCAM_TBL_OPC_NORMAL) {
1472                         idents = ulp_mapper_ident_fields_get(tbl, &num_idents);
1473
1474                         for (i = 0; i < num_idents; i++) {
1475                                 rc = ulp_mapper_ident_process(parms, tbl,
1476                                                               &idents[i], NULL);
1477                                 /* Already logged the error, just return */
1478                                 if (rc)
1479                                         goto error;
1480                         }
1481                 }
1482
1483                 /* Create the result data blob */
1484                 dflds = ulp_mapper_result_fields_get(tbl, &num_dflds,
1485                                                      &encap_flds);
1486                 if (!dflds || !num_dflds || encap_flds) {
1487                         BNXT_TF_DBG(ERR, "Failed to get data fields.\n");
1488                         rc = -EINVAL;
1489                         goto error;
1490                 }
1491
1492                 for (i = 0; i < num_dflds; i++) {
1493                         rc = ulp_mapper_result_field_process(parms,
1494                                                              tbl->direction,
1495                                                              &dflds[i],
1496                                                              &data,
1497                                                              "TCAM Result");
1498                         if (rc) {
1499                                 BNXT_TF_DBG(ERR, "Failed to set data fields\n");
1500                                 goto error;
1501                         }
1502                 }
1503
1504                 sparms.dir              = tbl->direction;
1505                 sparms.tcam_tbl_type    = tbl->resource_type;
1506                 sparms.idx              = idx;
1507                 /* Already verified the key/mask lengths */
1508                 sparms.key              = ulp_blob_data_get(&key, &tmplen);
1509                 sparms.mask             = ulp_blob_data_get(&mask, &tmplen);
1510                 sparms.key_sz_in_bits   = tbl->key_bit_size;
1511                 sparms.result           = ulp_blob_data_get(&data, &tmplen);
1512
1513                 if (tbl->result_bit_size != tmplen) {
1514                         BNXT_TF_DBG(ERR, "Result len (%d) != Expected (%d)\n",
1515                                     tmplen, tbl->result_bit_size);
1516                         rc = -EINVAL;
1517                         goto error;
1518                 }
1519                 sparms.result_sz_in_bits = tbl->result_bit_size;
1520
1521                 rc = tf_set_tcam_entry(tfp, &sparms);
1522                 if (rc) {
1523                         BNXT_TF_DBG(ERR, "tcam[%d][%s][%d] write failed.\n",
1524                                     sparms.tcam_tbl_type,
1525                                     (sparms.dir == TF_DIR_RX) ? "RX" : "TX",
1526                                     sparms.idx);
1527                         goto error;
1528                 }
1529
1530                 /* Update cache with TCAM index if the was cache allocated. */
1531                 if (parms->tcam_tbl_opc ==
1532                     BNXT_ULP_MAPPER_TCAM_TBL_OPC_CACHE_ALLOC) {
1533                         if (!parms->cache_ptr) {
1534                                 BNXT_TF_DBG(ERR, "Unable to update cache");
1535                                 rc = -EINVAL;
1536                                 goto error;
1537                         }
1538                         parms->cache_ptr->tcam_idx = idx;
1539                 }
1540
1541                 /* Mark action */
1542                 rc = ulp_mapper_mark_act_ptr_process(parms, tbl);
1543                 if (rc)
1544                         goto error;
1545
1546         } else {
1547                 struct bnxt_ulp_mapper_ident_info *idents;
1548                 uint32_t num_idents;
1549
1550                 /*
1551                  * Extract the listed identifiers from the result field,
1552                  * no need to allocate them.
1553                  */
1554                 idents = ulp_mapper_ident_fields_get(tbl, &num_idents);
1555                 for (i = 0; i < num_idents; i++) {
1556                         rc = ulp_mapper_ident_extract(parms, tbl,
1557                                                       &idents[i], &data);
1558                         if (rc) {
1559                                 BNXT_TF_DBG(ERR,
1560                                             "Error in ident extraction\n");
1561                                 goto error;
1562                         }
1563                 }
1564         }
1565
1566         /*
1567          * Only link the entry to the flow db in the event that cache was not
1568          * used.
1569          */
1570         if (parms->tcam_tbl_opc == BNXT_ULP_MAPPER_TCAM_TBL_OPC_NORMAL) {
1571                 fid_parms.direction = tbl->direction;
1572                 fid_parms.resource_func = tbl->resource_func;
1573                 fid_parms.resource_type = tbl->resource_type;
1574                 fid_parms.critical_resource = tbl->critical_resource;
1575                 fid_parms.resource_hndl = idx;
1576                 rc = ulp_flow_db_resource_add(parms->ulp_ctx,
1577                                               parms->tbl_idx,
1578                                               parms->fid,
1579                                               &fid_parms);
1580                 if (rc) {
1581                         BNXT_TF_DBG(ERR,
1582                                     "Failed to link resource to flow rc = %d\n",
1583                                     rc);
1584                         /* Need to free the identifier, so goto error */
1585                         goto error;
1586                 }
1587         } else {
1588                 /*
1589                  * Reset the tcam table opcode to normal in case the next tcam
1590                  * entry does not use cache.
1591                  */
1592                 parms->tcam_tbl_opc = BNXT_ULP_MAPPER_TCAM_TBL_OPC_NORMAL;
1593                 parms->cache_ptr = NULL;
1594         }
1595
1596         return 0;
1597 error:
1598         parms->tcam_tbl_opc = BNXT_ULP_MAPPER_TCAM_TBL_OPC_NORMAL;
1599         free_parms.dir                  = tbl->direction;
1600         free_parms.tcam_tbl_type        = tbl->resource_type;
1601         free_parms.idx                  = aparms.idx;
1602         trc = tf_free_tcam_entry(tfp, &free_parms);
1603         if (trc)
1604                 BNXT_TF_DBG(ERR, "Failed to free tcam[%d][%d][%d] on failure\n",
1605                             tbl->resource_type, tbl->direction, aparms.idx);
1606
1607         return rc;
1608 }
1609
1610 static int32_t
1611 ulp_mapper_em_tbl_process(struct bnxt_ulp_mapper_parms *parms,
1612                           struct bnxt_ulp_mapper_tbl_info *tbl)
1613 {
1614         struct bnxt_ulp_mapper_class_key_field_info     *kflds;
1615         struct bnxt_ulp_mapper_result_field_info *dflds;
1616         struct ulp_blob key, data;
1617         uint32_t i, num_kflds, num_dflds;
1618         uint16_t tmplen;
1619         struct tf *tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx);
1620         struct ulp_flow_db_res_params   fid_parms = { 0 };
1621         struct tf_insert_em_entry_parms iparms = { 0 };
1622         struct tf_delete_em_entry_parms free_parms = { 0 };
1623         int32_t trc;
1624         enum bnxt_ulp_flow_mem_type mtype = parms->device_params->flow_mem_type;
1625         int32_t rc = 0;
1626         uint32_t encap_flds = 0;
1627
1628         kflds = ulp_mapper_key_fields_get(tbl, &num_kflds);
1629         if (!kflds || !num_kflds) {
1630                 BNXT_TF_DBG(ERR, "Failed to get key fields\n");
1631                 return -EINVAL;
1632         }
1633
1634         /* Initialize the key/result blobs */
1635         if (!ulp_blob_init(&key, tbl->blob_key_bit_size,
1636                            parms->device_params->byte_order) ||
1637             !ulp_blob_init(&data, tbl->result_bit_size,
1638                            parms->device_params->byte_order)) {
1639                 BNXT_TF_DBG(ERR, "blob inits failed.\n");
1640                 return -EINVAL;
1641         }
1642
1643         /* create the key */
1644         for (i = 0; i < num_kflds; i++) {
1645                 /* Setup the key */
1646                 rc = ulp_mapper_keymask_field_process(parms, tbl->direction,
1647                                                       &kflds[i],
1648                                                       &key, 1, "EM Key");
1649                 if (rc) {
1650                         BNXT_TF_DBG(ERR, "Key field set failed.\n");
1651                         return rc;
1652                 }
1653         }
1654
1655         /*
1656          * TBD: Normally should process identifiers in case of using recycle or
1657          * loopback.  Not supporting recycle for now.
1658          */
1659
1660         /* Create the result data blob */
1661         dflds = ulp_mapper_result_fields_get(tbl, &num_dflds, &encap_flds);
1662         if (!dflds || !num_dflds || encap_flds) {
1663                 BNXT_TF_DBG(ERR, "Failed to get data fields.\n");
1664                 return -EINVAL;
1665         }
1666
1667         for (i = 0; i < num_dflds; i++) {
1668                 struct bnxt_ulp_mapper_result_field_info *fld;
1669
1670                 fld = &dflds[i];
1671
1672                 rc = ulp_mapper_result_field_process(parms,
1673                                                      tbl->direction,
1674                                                      fld,
1675                                                      &data,
1676                                                      "EM Result");
1677                 if (rc) {
1678                         BNXT_TF_DBG(ERR, "Failed to set data fields.\n");
1679                         return rc;
1680                 }
1681         }
1682
1683         /* do the transpose for the internal EM keys */
1684         if (tbl->resource_func == BNXT_ULP_RESOURCE_FUNC_INT_EM_TABLE)
1685                 ulp_blob_perform_byte_reverse(&key);
1686
1687         rc = bnxt_ulp_cntxt_tbl_scope_id_get(parms->ulp_ctx,
1688                                              &iparms.tbl_scope_id);
1689         if (rc) {
1690                 BNXT_TF_DBG(ERR, "Failed to get table scope rc=%d\n", rc);
1691                 return rc;
1692         }
1693
1694         /*
1695          * NOTE: the actual blob size will differ from the size in the tbl
1696          * entry due to the padding.
1697          */
1698         iparms.dup_check                = 0;
1699         iparms.dir                      = tbl->direction;
1700         iparms.mem                      = tbl->resource_type;
1701         iparms.key                      = ulp_blob_data_get(&key, &tmplen);
1702         iparms.key_sz_in_bits           = tbl->key_bit_size;
1703         iparms.em_record                = ulp_blob_data_get(&data, &tmplen);
1704         iparms.em_record_sz_in_bits     = tbl->result_bit_size;
1705
1706         rc = tf_insert_em_entry(tfp, &iparms);
1707         if (rc) {
1708                 BNXT_TF_DBG(ERR, "Failed to insert em entry rc=%d.\n", rc);
1709                 return rc;
1710         }
1711
1712         /* Mark action process */
1713         if (mtype == BNXT_ULP_FLOW_MEM_TYPE_EXT &&
1714             tbl->resource_type == TF_MEM_EXTERNAL)
1715                 rc = ulp_mapper_mark_gfid_process(parms, tbl, iparms.flow_id);
1716         else if (mtype == BNXT_ULP_FLOW_MEM_TYPE_INT &&
1717                  tbl->resource_type == TF_MEM_INTERNAL)
1718                 rc = ulp_mapper_mark_act_ptr_process(parms, tbl);
1719         if (rc) {
1720                 BNXT_TF_DBG(ERR, "Failed to add mark to flow\n");
1721                 goto error;
1722         }
1723
1724         /* Link the EM resource to the flow in the flow db */
1725         memset(&fid_parms, 0, sizeof(fid_parms));
1726         fid_parms.direction             = tbl->direction;
1727         fid_parms.resource_func         = tbl->resource_func;
1728         fid_parms.resource_type         = tbl->resource_type;
1729         fid_parms.critical_resource     = tbl->critical_resource;
1730         fid_parms.resource_hndl         = iparms.flow_handle;
1731
1732         rc = ulp_flow_db_resource_add(parms->ulp_ctx,
1733                                       parms->tbl_idx,
1734                                       parms->fid,
1735                                       &fid_parms);
1736         if (rc) {
1737                 BNXT_TF_DBG(ERR, "Fail to link res to flow rc = %d\n",
1738                             rc);
1739                 /* Need to free the identifier, so goto error */
1740                 goto error;
1741         }
1742
1743         return 0;
1744 error:
1745         free_parms.dir          = iparms.dir;
1746         free_parms.mem          = iparms.mem;
1747         free_parms.tbl_scope_id = iparms.tbl_scope_id;
1748         free_parms.flow_handle  = iparms.flow_handle;
1749
1750         trc = tf_delete_em_entry(tfp, &free_parms);
1751         if (trc)
1752                 BNXT_TF_DBG(ERR, "Failed to delete EM entry on failed add\n");
1753
1754         return rc;
1755 }
1756
1757 static int32_t
1758 ulp_mapper_index_tbl_process(struct bnxt_ulp_mapper_parms *parms,
1759                              struct bnxt_ulp_mapper_tbl_info *tbl,
1760                              bool is_class_tbl)
1761 {
1762         struct bnxt_ulp_mapper_result_field_info *flds;
1763         struct ulp_flow_db_res_params   fid_parms;
1764         struct ulp_blob data;
1765         uint64_t idx = 0;
1766         uint16_t tmplen;
1767         uint32_t i, num_flds;
1768         int32_t rc = 0, trc = 0;
1769         struct tf_alloc_tbl_entry_parms aparms = { 0 };
1770         struct tf_set_tbl_entry_parms   sparms = { 0 };
1771         struct tf_free_tbl_entry_parms  free_parms = { 0 };
1772         uint32_t tbl_scope_id;
1773         struct tf *tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx);
1774         uint16_t bit_size;
1775         uint32_t encap_flds = 0;
1776
1777         /* Get the scope id first */
1778         rc = bnxt_ulp_cntxt_tbl_scope_id_get(parms->ulp_ctx, &tbl_scope_id);
1779         if (rc) {
1780                 BNXT_TF_DBG(ERR, "Failed to get table scope rc=%d\n", rc);
1781                 return rc;
1782         }
1783
1784         /* use the max size if encap is enabled */
1785         if (tbl->encap_num_fields)
1786                 bit_size = BNXT_ULP_FLMP_BLOB_SIZE_IN_BITS;
1787         else
1788                 bit_size = tbl->result_bit_size;
1789
1790         /* Initialize the blob data */
1791         if (!ulp_blob_init(&data, bit_size,
1792                            parms->device_params->byte_order)) {
1793                 BNXT_TF_DBG(ERR, "Failed initial index table blob\n");
1794                 return -EINVAL;
1795         }
1796
1797         /* Get the result fields list */
1798         if (is_class_tbl)
1799                 flds = ulp_mapper_result_fields_get(tbl, &num_flds,
1800                                                     &encap_flds);
1801         else
1802                 flds = ulp_mapper_act_result_fields_get(tbl, &num_flds,
1803                                                         &encap_flds);
1804
1805         if (!flds || (!num_flds && !encap_flds)) {
1806                 BNXT_TF_DBG(ERR, "template undefined for the index table\n");
1807                 return -EINVAL;
1808         }
1809
1810         /* process the result fields, loop through them */
1811         for (i = 0; i < (num_flds + encap_flds); i++) {
1812                 /* set the swap index if encap swap bit is enabled */
1813                 if (parms->device_params->encap_byte_swap && encap_flds &&
1814                     (i == num_flds))
1815                         ulp_blob_encap_swap_idx_set(&data);
1816
1817                 /* Process the result fields */
1818                 rc = ulp_mapper_result_field_process(parms,
1819                                                      tbl->direction,
1820                                                      &flds[i],
1821                                                      &data,
1822                                                      "Indexed Result");
1823                 if (rc) {
1824                         BNXT_TF_DBG(ERR, "data field failed\n");
1825                         return rc;
1826                 }
1827         }
1828
1829         /* if encap bit swap is enabled perform the bit swap */
1830         if (parms->device_params->encap_byte_swap && encap_flds) {
1831                 ulp_blob_perform_encap_swap(&data);
1832         }
1833
1834         /*
1835          * Check for index opcode, if it is Global then
1836          * no need to allocate the table, just set the table
1837          * and exit since it is not maintained in the flow db.
1838          */
1839         if (tbl->index_opcode == BNXT_ULP_INDEX_OPCODE_GLOBAL) {
1840                 /* get the index from index operand */
1841                 if (tbl->index_operand < BNXT_ULP_GLB_REGFILE_INDEX_LAST &&
1842                     ulp_mapper_glb_resource_read(parms->mapper_data,
1843                                                  tbl->direction,
1844                                                  tbl->index_operand,
1845                                                  &idx)) {
1846                         BNXT_TF_DBG(ERR, "Glbl regfile[%d] read failed.\n",
1847                                     tbl->index_operand);
1848                         return -EINVAL;
1849                 }
1850                 /* set the Tf index table */
1851                 sparms.dir              = tbl->direction;
1852                 sparms.type             = tbl->resource_type;
1853                 sparms.data             = ulp_blob_data_get(&data, &tmplen);
1854                 sparms.data_sz_in_bytes = ULP_BITS_2_BYTE(tmplen);
1855                 sparms.idx              = tfp_be_to_cpu_64(idx);
1856                 sparms.tbl_scope_id     = tbl_scope_id;
1857
1858                 rc = tf_set_tbl_entry(tfp, &sparms);
1859                 if (rc) {
1860                         BNXT_TF_DBG(ERR,
1861                                     "Glbl Set table[%d][%s][%d] failed rc=%d\n",
1862                                     sparms.type,
1863                                     (sparms.dir == TF_DIR_RX) ? "RX" : "TX",
1864                                     sparms.idx,
1865                                     rc);
1866                         return rc;
1867                 }
1868                 return 0; /* success */
1869         }
1870
1871         /* Perform the tf table allocation by filling the alloc params */
1872         aparms.dir              = tbl->direction;
1873         aparms.type             = tbl->resource_type;
1874         aparms.search_enable    = tbl->srch_b4_alloc;
1875         aparms.result           = ulp_blob_data_get(&data, &tmplen);
1876         aparms.result_sz_in_bytes = ULP_BITS_2_BYTE(tmplen);
1877         aparms.tbl_scope_id     = tbl_scope_id;
1878
1879         /* All failures after the alloc succeeds require a free */
1880         rc = tf_alloc_tbl_entry(tfp, &aparms);
1881         if (rc) {
1882                 BNXT_TF_DBG(ERR, "Alloc table[%d][%s] failed rc=%d\n",
1883                             aparms.type,
1884                             (aparms.dir == TF_DIR_RX) ? "RX" : "TX",
1885                             rc);
1886                 return rc;
1887         }
1888
1889         /*
1890          * calculate the idx for the result record, for external EM the offset
1891          * needs to be shifted accordingly. If external non-inline table types
1892          * are used then need to revisit this logic.
1893          */
1894         if (aparms.type == TF_TBL_TYPE_EXT)
1895                 idx = TF_ACT_REC_OFFSET_2_PTR(aparms.idx);
1896         else
1897                 idx = aparms.idx;
1898
1899         /* Always storing values in Regfile in BE */
1900         idx = tfp_cpu_to_be_64(idx);
1901         if (tbl->index_opcode == BNXT_ULP_INDEX_OPCODE_ALLOCATE) {
1902                 rc = ulp_regfile_write(parms->regfile, tbl->index_operand, idx);
1903                 if (!rc) {
1904                         BNXT_TF_DBG(ERR, "Write regfile[%d] failed\n",
1905                                     tbl->index_operand);
1906                         goto error;
1907                 }
1908         }
1909
1910         /* Perform the tf table set by filling the set params */
1911         if (!tbl->srch_b4_alloc || !aparms.hit) {
1912                 sparms.dir              = tbl->direction;
1913                 sparms.type             = tbl->resource_type;
1914                 sparms.data             = ulp_blob_data_get(&data, &tmplen);
1915                 sparms.data_sz_in_bytes = ULP_BITS_2_BYTE(tmplen);
1916                 sparms.idx              = aparms.idx;
1917                 sparms.tbl_scope_id     = tbl_scope_id;
1918
1919                 rc = tf_set_tbl_entry(tfp, &sparms);
1920                 if (rc) {
1921                         BNXT_TF_DBG(ERR, "Set table[%d][%s][%d] failed rc=%d\n",
1922                                     sparms.type,
1923                                     (sparms.dir == TF_DIR_RX) ? "RX" : "TX",
1924                                     sparms.idx,
1925                                     rc);
1926                         goto error;
1927                 }
1928         }
1929
1930         /* Link the resource to the flow in the flow db */
1931         memset(&fid_parms, 0, sizeof(fid_parms));
1932         fid_parms.direction     = tbl->direction;
1933         fid_parms.resource_func = tbl->resource_func;
1934         fid_parms.resource_type = tbl->resource_type;
1935         fid_parms.resource_sub_type = tbl->resource_sub_type;
1936         fid_parms.resource_hndl = aparms.idx;
1937         fid_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
1938
1939         rc = ulp_flow_db_resource_add(parms->ulp_ctx,
1940                                       parms->tbl_idx,
1941                                       parms->fid,
1942                                       &fid_parms);
1943         if (rc) {
1944                 BNXT_TF_DBG(ERR, "Failed to link resource to flow rc = %d\n",
1945                             rc);
1946                 goto error;
1947         }
1948
1949         /* Perform the VF rep action */
1950         rc = ulp_mapper_mark_vfr_idx_process(parms, tbl);
1951         if (rc) {
1952                 BNXT_TF_DBG(ERR, "Failed to add vfr mark rc = %d\n", rc);
1953                 goto error;
1954         }
1955         return rc;
1956 error:
1957         /*
1958          * Free the allocated resource since we failed to either
1959          * write to the entry or link the flow
1960          */
1961         free_parms.dir  = tbl->direction;
1962         free_parms.type = tbl->resource_type;
1963         free_parms.idx  = aparms.idx;
1964         free_parms.tbl_scope_id = tbl_scope_id;
1965
1966         trc = tf_free_tbl_entry(tfp, &free_parms);
1967         if (trc)
1968                 BNXT_TF_DBG(ERR, "Failed to free tbl entry on failure\n");
1969
1970         return rc;
1971 }
1972
1973 static int32_t
1974 ulp_mapper_cache_tbl_process(struct bnxt_ulp_mapper_parms *parms,
1975                              struct bnxt_ulp_mapper_tbl_info *tbl)
1976 {
1977         struct bnxt_ulp_mapper_class_key_field_info *kflds;
1978         struct bnxt_ulp_mapper_cache_entry *cache_entry;
1979         struct bnxt_ulp_mapper_ident_info *idents;
1980         uint32_t i, num_kflds = 0, num_idents = 0;
1981         struct ulp_flow_db_res_params fid_parms;
1982         struct tf_free_identifier_parms fparms;
1983         uint16_t tmplen, tmp_ident;
1984         struct ulp_blob key;
1985         uint8_t *cache_key;
1986         uint64_t regval;
1987         uint16_t *ckey;
1988         int32_t rc;
1989
1990         /* Get the key fields list and build the key. */
1991         kflds = ulp_mapper_key_fields_get(tbl, &num_kflds);
1992         if (!kflds || !num_kflds) {
1993                 BNXT_TF_DBG(ERR, "Failed to get key fields\n");
1994                 return -EINVAL;
1995         }
1996         if (!ulp_blob_init(&key, tbl->key_bit_size,
1997                            parms->device_params->byte_order)) {
1998                 BNXT_TF_DBG(ERR, "Failed to alloc blob\n");
1999                 return -EINVAL;
2000         }
2001         for (i = 0; i < num_kflds; i++) {
2002                 /* Setup the key */
2003                 rc = ulp_mapper_keymask_field_process(parms, tbl->direction,
2004                                                       &kflds[i],
2005                                                       &key, 1, "Cache Key");
2006                 if (rc) {
2007                         BNXT_TF_DBG(ERR,
2008                                     "Failed to create key for Cache rc=%d\n",
2009                                     rc);
2010                         return -EINVAL;
2011                 }
2012         }
2013
2014         /*
2015          * Perform the lookup in the cache table with constructed key.  The
2016          * cache_key is a byte array of tmplen, it needs to be converted to a
2017          * index for the cache table.
2018          */
2019         cache_key = ulp_blob_data_get(&key, &tmplen);
2020         ckey = (uint16_t *)cache_key;
2021
2022         /*
2023          * The id computed based on resource sub type and direction where
2024          * dir is the bit0 and rest of the bits come from resource
2025          * sub type.
2026          */
2027         cache_entry = ulp_mapper_cache_entry_get(parms->ulp_ctx,
2028                                                  (tbl->resource_sub_type << 1 |
2029                                                  (tbl->direction & 0x1)),
2030                                                  *ckey);
2031
2032         /*
2033          * Get the identifier list for processing by both the hit and miss
2034          * processing.
2035          */
2036         idents = ulp_mapper_ident_fields_get(tbl, &num_idents);
2037
2038         if (!cache_entry->ref_count) {
2039                 /* Initialize the cache entry */
2040                 cache_entry->tcam_idx = 0;
2041                 cache_entry->ref_count = 0;
2042                 for (i = 0; i < BNXT_ULP_CACHE_TBL_IDENT_MAX_NUM; i++)
2043                         cache_entry->idents[i] = ULP_IDENTS_INVALID;
2044
2045                 /* Need to allocate identifiers for storing in the cache. */
2046                 for (i = 0; i < num_idents; i++) {
2047                         /*
2048                          * Since we are using the cache, the identifier does not
2049                          * get added to the flow db.  Pass in the pointer to the
2050                          * tmp_ident.
2051                          */
2052                         rc = ulp_mapper_ident_process(parms, tbl,
2053                                                       &idents[i], &tmp_ident);
2054                         if (rc)
2055                                 goto error;
2056
2057                         cache_entry->ident_types[i] = idents[i].ident_type;
2058                         cache_entry->idents[i] = tmp_ident;
2059                 }
2060
2061                 /* Tell the TCAM processor to alloc an entry */
2062                 parms->tcam_tbl_opc = BNXT_ULP_MAPPER_TCAM_TBL_OPC_CACHE_ALLOC;
2063                 /* Store the cache key for use by the tcam process code */
2064                 parms->cache_ptr = cache_entry;
2065         } else {
2066                 /* Cache hit, get values from result. */
2067                 for (i = 0; i < num_idents; i++) {
2068                         regval = (uint64_t)cache_entry->idents[i];
2069                         if (!ulp_regfile_write(parms->regfile,
2070                                                idents[i].regfile_idx,
2071                                                tfp_cpu_to_be_64(regval))) {
2072                                 BNXT_TF_DBG(ERR,
2073                                             "Failed to write to regfile\n");
2074                                 return -EINVAL;
2075                         }
2076                 }
2077                 /*
2078                  * The cached entry is being used, so let the tcam processing
2079                  * know not to process this table.
2080                  */
2081                 parms->tcam_tbl_opc = BNXT_ULP_MAPPER_TCAM_TBL_OPC_CACHE_SKIP;
2082         }
2083
2084         /* Made through the cache processing, increment the reference count. */
2085         cache_entry->ref_count++;
2086
2087         /* Link the cache to the flow db. */
2088         memset(&fid_parms, 0, sizeof(fid_parms));
2089         fid_parms.direction = tbl->direction;
2090         fid_parms.resource_func = tbl->resource_func;
2091
2092         /*
2093          * Cache resource type is composed of table_type, resource
2094          * sub type and direction, it needs to set appropriately via setter.
2095          */
2096         ulp_mapper_cache_res_type_set(&fid_parms,
2097                                       tbl->resource_type,
2098                                       (tbl->resource_sub_type << 1 |
2099                                        (tbl->direction & 0x1)));
2100         fid_parms.resource_hndl = (uint64_t)*ckey;
2101         fid_parms.critical_resource = tbl->critical_resource;
2102         rc = ulp_flow_db_resource_add(parms->ulp_ctx,
2103                                       parms->tbl_idx,
2104                                       parms->fid,
2105                                       &fid_parms);
2106         if (rc)
2107                 BNXT_TF_DBG(ERR, "Failed to add cache to flow db.\n");
2108
2109         return rc;
2110 error:
2111         /*
2112          * This error handling only gets called when the idents are being
2113          * allocated for the cache on misses.  Using the num_idents that was
2114          * previously set.
2115          */
2116         for (i = 0; i < num_idents; i++) {
2117                 if (cache_entry->idents[i] == ULP_IDENTS_INVALID)
2118                         continue;
2119
2120                 fparms.dir = tbl->direction;
2121                 fparms.ident_type = idents[i].ident_type;
2122                 fparms.id = cache_entry->idents[i];
2123                 tf_free_identifier(parms->tfp, &fparms);
2124         }
2125
2126         return rc;
2127 }
2128
2129 static int32_t
2130 ulp_mapper_if_tbl_process(struct bnxt_ulp_mapper_parms *parms,
2131                           struct bnxt_ulp_mapper_tbl_info *tbl)
2132 {
2133         struct bnxt_ulp_mapper_result_field_info *flds;
2134         struct ulp_blob data;
2135         uint64_t idx;
2136         uint16_t tmplen;
2137         uint32_t i, num_flds;
2138         int32_t rc = 0;
2139         struct tf_set_if_tbl_entry_parms iftbl_params = { 0 };
2140         struct tf *tfp = bnxt_ulp_cntxt_tfp_get(parms->ulp_ctx);
2141         uint32_t encap_flds;
2142
2143         /* Initialize the blob data */
2144         if (!ulp_blob_init(&data, tbl->result_bit_size,
2145                            parms->device_params->byte_order)) {
2146                 BNXT_TF_DBG(ERR, "Failed initial index table blob\n");
2147                 return -EINVAL;
2148         }
2149
2150         /* Get the result fields list */
2151         flds = ulp_mapper_result_fields_get(tbl, &num_flds, &encap_flds);
2152
2153         if (!flds || !num_flds || encap_flds) {
2154                 BNXT_TF_DBG(ERR, "template undefined for the IF table\n");
2155                 return -EINVAL;
2156         }
2157
2158         /* process the result fields, loop through them */
2159         for (i = 0; i < num_flds; i++) {
2160                 /* Process the result fields */
2161                 rc = ulp_mapper_result_field_process(parms,
2162                                                      tbl->direction,
2163                                                      &flds[i],
2164                                                      &data,
2165                                                      "IFtable Result");
2166                 if (rc) {
2167                         BNXT_TF_DBG(ERR, "data field failed\n");
2168                         return rc;
2169                 }
2170         }
2171
2172         /* Get the index details from computed field */
2173         if (tbl->index_opcode == BNXT_ULP_INDEX_OPCODE_COMP_FIELD) {
2174                 idx = ULP_COMP_FLD_IDX_RD(parms, tbl->index_operand);
2175         } else if (tbl->index_opcode == BNXT_ULP_INDEX_OPCODE_CONSTANT) {
2176                 idx = tbl->index_operand;
2177         } else {
2178                 BNXT_TF_DBG(ERR, "Invalid tbl index opcode\n");
2179                 return -EINVAL;
2180         }
2181
2182         /* Perform the tf table set by filling the set params */
2183         iftbl_params.dir = tbl->direction;
2184         iftbl_params.type = tbl->resource_type;
2185         iftbl_params.data = ulp_blob_data_get(&data, &tmplen);
2186         iftbl_params.data_sz_in_bytes = ULP_BITS_2_BYTE(tmplen);
2187         iftbl_params.idx = idx;
2188
2189         rc = tf_set_if_tbl_entry(tfp, &iftbl_params);
2190         if (rc) {
2191                 BNXT_TF_DBG(ERR, "Set table[%d][%s][%d] failed rc=%d\n",
2192                             iftbl_params.type,
2193                             (iftbl_params.dir == TF_DIR_RX) ? "RX" : "TX",
2194                             iftbl_params.idx,
2195                             rc);
2196                 return rc;
2197         }
2198
2199         /*
2200          * TBD: Need to look at the need to store idx in flow db for restore
2201          * the table to its original state on deletion of this entry.
2202          */
2203         return rc;
2204 }
2205
2206 static int32_t
2207 ulp_mapper_glb_resource_info_init(struct bnxt_ulp_context *ulp_ctx,
2208                                   struct bnxt_ulp_mapper_data *mapper_data)
2209 {
2210         struct bnxt_ulp_glb_resource_info *glb_res;
2211         uint32_t num_glb_res_ids, idx;
2212         int32_t rc = 0;
2213
2214         glb_res = ulp_mapper_glb_resource_info_list_get(&num_glb_res_ids);
2215         if (!glb_res || !num_glb_res_ids) {
2216                 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
2217                 return -EINVAL;
2218         }
2219
2220         /* Iterate the global resources and process each one */
2221         for (idx = 0; idx < num_glb_res_ids; idx++) {
2222                 switch (glb_res[idx].resource_func) {
2223                 case BNXT_ULP_RESOURCE_FUNC_IDENTIFIER:
2224                         rc = ulp_mapper_resource_ident_allocate(ulp_ctx,
2225                                                                 mapper_data,
2226                                                                 &glb_res[idx]);
2227                         break;
2228                 case BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE:
2229                         rc = ulp_mapper_resource_index_tbl_alloc(ulp_ctx,
2230                                                                  mapper_data,
2231                                                                  &glb_res[idx]);
2232                         break;
2233                 default:
2234                         BNXT_TF_DBG(ERR, "Global resource %x not supported\n",
2235                                     glb_res[idx].resource_func);
2236                         rc = -EINVAL;
2237                         break;
2238                 }
2239                 if (rc)
2240                         return rc;
2241         }
2242         return rc;
2243 }
2244
2245 /*
2246  * Function to process the conditional opcode of the mapper table.
2247  * returns 1 to skip the table.
2248  * return 0 to continue processing the table.
2249  */
2250 static int32_t
2251 ulp_mapper_tbl_cond_opcode_process(struct bnxt_ulp_mapper_parms *parms,
2252                                    struct bnxt_ulp_mapper_tbl_info *tbl)
2253 {
2254         int32_t rc = 1;
2255
2256         switch (tbl->cond_opcode) {
2257         case BNXT_ULP_COND_OPCODE_NOP:
2258                 rc = 0;
2259                 break;
2260         case BNXT_ULP_COND_OPCODE_COMP_FIELD_IS_SET:
2261                 if (tbl->cond_operand < BNXT_ULP_CF_IDX_LAST &&
2262                     ULP_COMP_FLD_IDX_RD(parms, tbl->cond_operand))
2263                         rc = 0;
2264                 break;
2265         case BNXT_ULP_COND_OPCODE_ACTION_BIT_IS_SET:
2266                 if (ULP_BITMAP_ISSET(parms->act_bitmap->bits,
2267                                      tbl->cond_operand))
2268                         rc = 0;
2269                 break;
2270         case BNXT_ULP_COND_OPCODE_HDR_BIT_IS_SET:
2271                 if (ULP_BITMAP_ISSET(parms->hdr_bitmap->bits,
2272                                      tbl->cond_operand))
2273                         rc = 0;
2274                 break;
2275         case BNXT_ULP_COND_OPCODE_COMP_FIELD_NOT_SET:
2276                 if (tbl->cond_operand < BNXT_ULP_CF_IDX_LAST &&
2277                     !ULP_COMP_FLD_IDX_RD(parms, tbl->cond_operand))
2278                         rc = 0;
2279                 break;
2280         case BNXT_ULP_COND_OPCODE_ACTION_BIT_NOT_SET:
2281                 if (!ULP_BITMAP_ISSET(parms->act_bitmap->bits,
2282                                       tbl->cond_operand))
2283                         rc = 0;
2284                 break;
2285         case BNXT_ULP_COND_OPCODE_HDR_BIT_NOT_SET:
2286                 if (!ULP_BITMAP_ISSET(parms->hdr_bitmap->bits,
2287                                       tbl->cond_operand))
2288                         rc = 0;
2289                 break;
2290         default:
2291                 BNXT_TF_DBG(ERR,
2292                             "Invalid arg in mapper tbl for cond opcode\n");
2293                 break;
2294         }
2295         return rc;
2296 }
2297
2298 /*
2299  * Function to process the action template. Iterate through the list
2300  * action info templates and process it.
2301  */
2302 static int32_t
2303 ulp_mapper_action_tbls_process(struct bnxt_ulp_mapper_parms *parms)
2304 {
2305         uint32_t        i;
2306         int32_t         rc = 0;
2307         struct bnxt_ulp_mapper_tbl_info *tbl;
2308
2309         if (!parms->atbls || !parms->num_atbls) {
2310                 BNXT_TF_DBG(ERR, "No action tables for template[%d][%d].\n",
2311                             parms->dev_id, parms->act_tid);
2312                 return -EINVAL;
2313         }
2314
2315         for (i = 0; i < parms->num_atbls; i++) {
2316                 tbl = &parms->atbls[i];
2317                 if (ulp_mapper_tbl_cond_opcode_process(parms, tbl))
2318                         continue;
2319
2320                 switch (tbl->resource_func) {
2321                 case BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE:
2322                         rc = ulp_mapper_index_tbl_process(parms, tbl, false);
2323                         if (rc) {
2324                                 BNXT_TF_DBG(ERR, "Resource type %d failed\n",
2325                                             tbl->resource_func);
2326                                 return rc;
2327                         }
2328                         break;
2329                 default:
2330                         BNXT_TF_DBG(ERR, "Unexpected action resource %d\n",
2331                                     tbl->resource_func);
2332                         return -EINVAL;
2333                 }
2334         }
2335
2336         return rc;
2337 }
2338
2339 /* Create the classifier table entries for a flow. */
2340 static int32_t
2341 ulp_mapper_class_tbls_process(struct bnxt_ulp_mapper_parms *parms)
2342 {
2343         uint32_t        i;
2344         int32_t         rc = 0;
2345
2346         if (!parms)
2347                 return -EINVAL;
2348
2349         if (!parms->ctbls || !parms->num_ctbls) {
2350                 BNXT_TF_DBG(ERR, "No class tables for template[%d][%d].\n",
2351                             parms->dev_id, parms->class_tid);
2352                 return -EINVAL;
2353         }
2354
2355         for (i = 0; i < parms->num_ctbls; i++) {
2356                 struct bnxt_ulp_mapper_tbl_info *tbl = &parms->ctbls[i];
2357
2358                 if (ulp_mapper_tbl_cond_opcode_process(parms, tbl))
2359                         continue;
2360
2361                 switch (tbl->resource_func) {
2362                 case BNXT_ULP_RESOURCE_FUNC_TCAM_TABLE:
2363                         rc = ulp_mapper_tcam_tbl_process(parms, tbl);
2364                         break;
2365                 case BNXT_ULP_RESOURCE_FUNC_EXT_EM_TABLE:
2366                 case BNXT_ULP_RESOURCE_FUNC_INT_EM_TABLE:
2367                         rc = ulp_mapper_em_tbl_process(parms, tbl);
2368                         break;
2369                 case BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE:
2370                         rc = ulp_mapper_index_tbl_process(parms, tbl, true);
2371                         break;
2372                 case BNXT_ULP_RESOURCE_FUNC_CACHE_TABLE:
2373                         rc = ulp_mapper_cache_tbl_process(parms, tbl);
2374                         break;
2375                 case BNXT_ULP_RESOURCE_FUNC_IF_TABLE:
2376                         rc = ulp_mapper_if_tbl_process(parms, tbl);
2377                         break;
2378                 default:
2379                         BNXT_TF_DBG(ERR, "Unexpected class resource %d\n",
2380                                     tbl->resource_func);
2381                         return -EINVAL;
2382                 }
2383
2384                 if (rc) {
2385                         BNXT_TF_DBG(ERR, "Resource type %d failed\n",
2386                                     tbl->resource_func);
2387                         return rc;
2388                 }
2389         }
2390
2391         return rc;
2392 }
2393
2394 static int32_t
2395 ulp_mapper_resource_free(struct bnxt_ulp_context *ulp,
2396                          struct ulp_flow_db_res_params *res)
2397 {
2398         struct tf *tfp;
2399         int32_t rc = 0;
2400
2401         if (!res || !ulp) {
2402                 BNXT_TF_DBG(ERR, "Unable to free resource\n ");
2403                 return -EINVAL;
2404         }
2405
2406         tfp = bnxt_ulp_cntxt_tfp_get(ulp);
2407         if (!tfp) {
2408                 BNXT_TF_DBG(ERR, "Unable to free resource failed to get tfp\n");
2409                 return -EINVAL;
2410         }
2411
2412         switch (res->resource_func) {
2413         case BNXT_ULP_RESOURCE_FUNC_CACHE_TABLE:
2414                 rc = ulp_mapper_cache_entry_free(ulp, tfp, res);
2415                 break;
2416         case BNXT_ULP_RESOURCE_FUNC_TCAM_TABLE:
2417                 rc = ulp_mapper_tcam_entry_free(ulp, tfp, res);
2418                 break;
2419         case BNXT_ULP_RESOURCE_FUNC_EXT_EM_TABLE:
2420         case BNXT_ULP_RESOURCE_FUNC_INT_EM_TABLE:
2421                 rc = ulp_mapper_em_entry_free(ulp, tfp, res);
2422                 break;
2423         case BNXT_ULP_RESOURCE_FUNC_INDEX_TABLE:
2424                 rc = ulp_mapper_index_entry_free(ulp, tfp, res);
2425                 break;
2426         case BNXT_ULP_RESOURCE_FUNC_IDENTIFIER:
2427                 rc = ulp_mapper_ident_free(ulp, tfp, res);
2428                 break;
2429         case BNXT_ULP_RESOURCE_FUNC_HW_FID:
2430                 rc = ulp_mapper_mark_free(ulp, res);
2431                 break;
2432         default:
2433                 break;
2434         }
2435
2436         return rc;
2437 }
2438
2439 int32_t
2440 ulp_mapper_resources_free(struct bnxt_ulp_context       *ulp_ctx,
2441                           uint32_t fid,
2442                           enum bnxt_ulp_flow_db_tables  tbl_type)
2443 {
2444         struct ulp_flow_db_res_params   res_parms = { 0 };
2445         int32_t                         rc, trc;
2446
2447         if (!ulp_ctx) {
2448                 BNXT_TF_DBG(ERR, "Invalid parms, unable to free flow\n");
2449                 return -EINVAL;
2450         }
2451
2452         /*
2453          * Set the critical resource on the first resource del, then iterate
2454          * while status is good
2455          */
2456         res_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_YES;
2457         rc = ulp_flow_db_resource_del(ulp_ctx, tbl_type, fid, &res_parms);
2458
2459         if (rc) {
2460                 /*
2461                  * This is unexpected on the first call to resource del.
2462                  * It likely means that the flow did not exist in the flow db.
2463                  */
2464                 BNXT_TF_DBG(ERR, "Flow[%d][0x%08x] failed to free (rc=%d)\n",
2465                             tbl_type, fid, rc);
2466                 return rc;
2467         }
2468
2469         while (!rc) {
2470                 trc = ulp_mapper_resource_free(ulp_ctx, &res_parms);
2471                 if (trc)
2472                         /*
2473                          * On fail, we still need to attempt to free the
2474                          * remaining resources.  Don't return
2475                          */
2476                         BNXT_TF_DBG(ERR,
2477                                     "Flow[%d][0x%x] Res[%d][0x%016" PRIx64
2478                                     "] failed rc=%d.\n",
2479                                     tbl_type, fid, res_parms.resource_func,
2480                                     res_parms.resource_hndl, trc);
2481
2482                 /* All subsequent call require the non-critical_resource */
2483                 res_parms.critical_resource = BNXT_ULP_CRITICAL_RESOURCE_NO;
2484
2485                 rc = ulp_flow_db_resource_del(ulp_ctx,
2486                                               tbl_type,
2487                                               fid,
2488                                               &res_parms);
2489         }
2490
2491         /* Free the Flow ID since we've removed all resources */
2492         rc = ulp_flow_db_fid_free(ulp_ctx, tbl_type, fid);
2493
2494         return rc;
2495 }
2496
2497 static void
2498 ulp_mapper_glb_resource_info_deinit(struct bnxt_ulp_context *ulp_ctx,
2499                                     struct bnxt_ulp_mapper_data *mapper_data)
2500 {
2501         struct bnxt_ulp_mapper_glb_resource_entry *ent;
2502         struct ulp_flow_db_res_params res;
2503         uint32_t dir, idx;
2504
2505         /* Iterate the global resources and process each one */
2506         for (dir = TF_DIR_RX; dir < TF_DIR_MAX; dir++) {
2507                 for (idx = 0; idx < BNXT_ULP_GLB_RESOURCE_TBL_MAX_SZ;
2508                       idx++) {
2509                         ent = &mapper_data->glb_res_tbl[dir][idx];
2510                         if (ent->resource_func ==
2511                             BNXT_ULP_RESOURCE_FUNC_INVALID)
2512                                 continue;
2513                         memset(&res, 0, sizeof(struct ulp_flow_db_res_params));
2514                         res.resource_func = ent->resource_func;
2515                         res.direction = dir;
2516                         res.resource_type = ent->resource_type;
2517                         /*convert it from BE to cpu */
2518                         res.resource_hndl =
2519                                 tfp_be_to_cpu_64(ent->resource_hndl);
2520                         ulp_mapper_resource_free(ulp_ctx, &res);
2521                 }
2522         }
2523 }
2524
2525 int32_t
2526 ulp_mapper_flow_destroy(struct bnxt_ulp_context *ulp_ctx, uint32_t fid,
2527                         enum bnxt_ulp_flow_db_tables flow_tbl_type)
2528 {
2529         if (!ulp_ctx) {
2530                 BNXT_TF_DBG(ERR, "Invalid parms, unable to free flow\n");
2531                 return -EINVAL;
2532         }
2533
2534         return ulp_mapper_resources_free(ulp_ctx, fid, flow_tbl_type);
2535 }
2536
2537 /* Function to handle the default global templates that are allocated during
2538  * the startup and reused later.
2539  */
2540 static int32_t
2541 ulp_mapper_glb_template_table_init(struct bnxt_ulp_context *ulp_ctx)
2542 {
2543         uint32_t *glbl_tmpl_list;
2544         uint32_t num_glb_tmpls, idx, dev_id;
2545         struct bnxt_ulp_mapper_parms parms;
2546         struct bnxt_ulp_mapper_data *mapper_data;
2547         int32_t rc = 0;
2548
2549         glbl_tmpl_list = ulp_mapper_glb_template_table_get(&num_glb_tmpls);
2550         if (!glbl_tmpl_list || !num_glb_tmpls)
2551                 return rc; /* No global templates to process */
2552
2553         /* Get the device id from the ulp context */
2554         if (bnxt_ulp_cntxt_dev_id_get(ulp_ctx, &dev_id)) {
2555                 BNXT_TF_DBG(ERR, "Invalid ulp context\n");
2556                 return -EINVAL;
2557         }
2558
2559         mapper_data = bnxt_ulp_cntxt_ptr2_mapper_data_get(ulp_ctx);
2560         if (!mapper_data) {
2561                 BNXT_TF_DBG(ERR, "Failed to get the ulp mapper data\n");
2562                 return -EINVAL;
2563         }
2564
2565         /* Iterate the global resources and process each one */
2566         for (idx = 0; idx < num_glb_tmpls; idx++) {
2567                 /* Initialize the parms structure */
2568                 memset(&parms, 0, sizeof(parms));
2569                 parms.tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx);
2570                 parms.ulp_ctx = ulp_ctx;
2571                 parms.dev_id = dev_id;
2572                 parms.mapper_data = mapper_data;
2573
2574                 /* Get the class table entry from dev id and class id */
2575                 parms.class_tid = glbl_tmpl_list[idx];
2576                 parms.ctbls = ulp_mapper_class_tbl_list_get(parms.dev_id,
2577                                                             parms.class_tid,
2578                                                             &parms.num_ctbls,
2579                                                             &parms.tbl_idx);
2580                 if (!parms.ctbls || !parms.num_ctbls) {
2581                         BNXT_TF_DBG(ERR, "No class tables for %d:%d\n",
2582                                     parms.dev_id, parms.class_tid);
2583                         return -EINVAL;
2584                 }
2585                 parms.device_params = bnxt_ulp_device_params_get(parms.dev_id);
2586                 if (!parms.device_params) {
2587                         BNXT_TF_DBG(ERR, "No class tables for %d:%d\n",
2588                                     parms.dev_id, parms.class_tid);
2589                         return -EINVAL;
2590                 }
2591                 rc = ulp_mapper_class_tbls_process(&parms);
2592                 if (rc) {
2593                         BNXT_TF_DBG(ERR,
2594                                     "class tables failed creation for %d:%d\n",
2595                                     parms.dev_id, parms.class_tid);
2596                         return rc;
2597                 }
2598         }
2599         return rc;
2600 }
2601
2602 /* Function to handle the mapping of the Flow to be compatible
2603  * with the underlying hardware.
2604  */
2605 int32_t
2606 ulp_mapper_flow_create(struct bnxt_ulp_context *ulp_ctx,
2607                        struct bnxt_ulp_mapper_create_parms *cparms,
2608                        uint32_t *flowid)
2609 {
2610         struct bnxt_ulp_mapper_parms parms;
2611         struct ulp_regfile regfile;
2612         int32_t  rc, trc;
2613
2614         if (!ulp_ctx || !cparms)
2615                 return -EINVAL;
2616
2617         /* Initialize the parms structure */
2618         memset(&parms, 0, sizeof(parms));
2619         parms.act_prop = cparms->act_prop;
2620         parms.act_bitmap = cparms->act;
2621         parms.hdr_bitmap = cparms->hdr_bitmap;
2622         parms.regfile = &regfile;
2623         parms.hdr_field = cparms->hdr_field;
2624         parms.comp_fld = cparms->comp_fld;
2625         parms.tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx);
2626         parms.ulp_ctx = ulp_ctx;
2627         parms.tcam_tbl_opc = BNXT_ULP_MAPPER_TCAM_TBL_OPC_NORMAL;
2628
2629         /* Get the device id from the ulp context */
2630         if (bnxt_ulp_cntxt_dev_id_get(ulp_ctx, &parms.dev_id)) {
2631                 BNXT_TF_DBG(ERR, "Invalid ulp context\n");
2632                 return -EINVAL;
2633         }
2634
2635         /*
2636          * Get the mapper data for dynamic mapper data such as default
2637          * ids.
2638          */
2639         parms.mapper_data = (struct bnxt_ulp_mapper_data *)
2640                 bnxt_ulp_cntxt_ptr2_mapper_data_get(ulp_ctx);
2641         if (!parms.mapper_data) {
2642                 BNXT_TF_DBG(ERR, "Failed to get the ulp mapper data\n");
2643                 return -EINVAL;
2644         }
2645
2646         /* Get the action table entry from device id and act context id */
2647         parms.act_tid = cparms->act_tid;
2648
2649         /*
2650          * Perform the action table get only if act template is not zero
2651          * for act template zero like for default rules ignore the action
2652          * table processing.
2653          */
2654         if (parms.act_tid) {
2655                 parms.atbls = ulp_mapper_action_tbl_list_get(parms.dev_id,
2656                                                              parms.act_tid,
2657                                                              &parms.num_atbls);
2658                 if (!parms.atbls || !parms.num_atbls) {
2659                         BNXT_TF_DBG(ERR, "No action tables for %d:%d\n",
2660                                     parms.dev_id, parms.act_tid);
2661                         return -EINVAL;
2662                 }
2663         }
2664
2665         /* Get the class table entry from device id and act context id */
2666         parms.class_tid = cparms->class_tid;
2667         parms.ctbls = ulp_mapper_class_tbl_list_get(parms.dev_id,
2668                                                     parms.class_tid,
2669                                                     &parms.num_ctbls,
2670                                                     &parms.tbl_idx);
2671         if (!parms.ctbls || !parms.num_ctbls) {
2672                 BNXT_TF_DBG(ERR, "No class tables for %d:%d\n",
2673                             parms.dev_id, parms.class_tid);
2674                 return -EINVAL;
2675         }
2676
2677         /* Get the device params, it will be used in later processing */
2678         parms.device_params = bnxt_ulp_device_params_get(parms.dev_id);
2679         if (!parms.device_params) {
2680                 BNXT_TF_DBG(ERR, "No class tables for %d:%d\n",
2681                             parms.dev_id, parms.class_tid);
2682                 return -EINVAL;
2683         }
2684
2685         /* initialize the registry file for further processing */
2686         if (!ulp_regfile_init(parms.regfile)) {
2687                 BNXT_TF_DBG(ERR, "regfile initialization failed.\n");
2688                 return -EINVAL;
2689         }
2690
2691         rc = ulp_regfile_write(parms.regfile,
2692                                BNXT_ULP_REGFILE_INDEX_CLASS_TID,
2693                                tfp_cpu_to_be_64((uint64_t)parms.class_tid));
2694         if (!rc) {
2695                 BNXT_TF_DBG(ERR, "Unable to write template ID to regfile\n");
2696                 return -EINVAL;
2697         }
2698
2699         /* Allocate a Flow ID for attaching all resources for the flow to.
2700          * Once allocated, all errors have to walk the list of resources and
2701          * free each of them.
2702          */
2703         rc = ulp_flow_db_fid_alloc(ulp_ctx,
2704                                    parms.tbl_idx,
2705                                    cparms->func_id,
2706                                    &parms.fid);
2707         if (rc) {
2708                 BNXT_TF_DBG(ERR, "Unable to allocate flow table entry\n");
2709                 return rc;
2710         }
2711
2712         /* Process the action template list from the selected action table*/
2713         if (parms.act_tid) {
2714                 rc = ulp_mapper_action_tbls_process(&parms);
2715                 if (rc) {
2716                         BNXT_TF_DBG(ERR,
2717                                     "action tables failed creation for %d:%d\n",
2718                                     parms.dev_id, parms.act_tid);
2719                         goto flow_error;
2720                 }
2721         }
2722
2723         /* All good. Now process the class template */
2724         rc = ulp_mapper_class_tbls_process(&parms);
2725         if (rc) {
2726                 BNXT_TF_DBG(ERR, "class tables failed creation for %d:%d\n",
2727                             parms.dev_id, parms.class_tid);
2728                 goto flow_error;
2729         }
2730
2731         *flowid = parms.fid;
2732
2733         return rc;
2734
2735 flow_error:
2736         /* Free all resources that were allocated during flow creation */
2737         trc = ulp_mapper_flow_destroy(ulp_ctx, parms.fid,
2738                                       BNXT_ULP_REGULAR_FLOW_TABLE);
2739         if (trc)
2740                 BNXT_TF_DBG(ERR, "Failed to free all resources rc=%d\n", trc);
2741
2742         return rc;
2743 }
2744
2745 int32_t
2746 ulp_mapper_init(struct bnxt_ulp_context *ulp_ctx)
2747 {
2748         struct bnxt_ulp_cache_tbl_params *tbl;
2749         struct bnxt_ulp_mapper_data *data;
2750         uint32_t i;
2751         struct tf *tfp;
2752         int32_t rc, csize;
2753
2754         if (!ulp_ctx)
2755                 return -EINVAL;
2756
2757         tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx);
2758         if (!tfp)
2759                 return -EINVAL;
2760
2761         data = rte_zmalloc("ulp_mapper_data",
2762                            sizeof(struct bnxt_ulp_mapper_data), 0);
2763         if (!data) {
2764                 BNXT_TF_DBG(ERR, "Failed to allocate the mapper data\n");
2765                 return -ENOMEM;
2766         }
2767
2768         if (bnxt_ulp_cntxt_ptr2_mapper_data_set(ulp_ctx, data)) {
2769                 BNXT_TF_DBG(ERR, "Failed to set mapper data in context\n");
2770                 /* Don't call deinit since the prof_func wasn't allocated. */
2771                 rte_free(data);
2772                 return -ENOMEM;
2773         }
2774
2775         /* Allocate the global resource ids */
2776         rc = ulp_mapper_glb_resource_info_init(ulp_ctx, data);
2777         if (rc) {
2778                 BNXT_TF_DBG(ERR, "Failed to initialize global resource ids\n");
2779                 goto error;
2780         }
2781
2782         /* Allocate the ulp cache tables. */
2783         for (i = 0; i < BNXT_ULP_CACHE_TBL_MAX_SZ; i++) {
2784                 tbl = ulp_mapper_cache_tbl_params_get(i);
2785                 if (!tbl) {
2786                         BNXT_TF_DBG(ERR, "Failed to get cache table parms (%d)",
2787                                     i);
2788                         goto error;
2789                 }
2790                 if (tbl->num_entries != 0) {
2791                         csize = sizeof(struct bnxt_ulp_mapper_cache_entry) *
2792                                 tbl->num_entries;
2793                         data->cache_tbl[i] = rte_zmalloc("ulp mapper cache tbl",
2794                                                          csize, 0);
2795                         if (!data->cache_tbl[i]) {
2796                                 BNXT_TF_DBG(ERR, "Failed to allocate Cache "
2797                                             "table %d.\n", i);
2798                                 rc = -ENOMEM;
2799                                 goto error;
2800                         }
2801                 }
2802         }
2803
2804         rc = ulp_mapper_glb_template_table_init(ulp_ctx);
2805         if (rc) {
2806                 BNXT_TF_DBG(ERR, "Failed to initialize global templates\n");
2807                 goto error;
2808         }
2809
2810         return 0;
2811 error:
2812         /* Ignore the return code in favor of returning the original error. */
2813         ulp_mapper_deinit(ulp_ctx);
2814         return rc;
2815 }
2816
2817 void
2818 ulp_mapper_deinit(struct bnxt_ulp_context *ulp_ctx)
2819 {
2820         struct bnxt_ulp_mapper_data *data;
2821         uint32_t i;
2822         struct tf *tfp;
2823
2824         if (!ulp_ctx) {
2825                 BNXT_TF_DBG(ERR,
2826                             "Failed to acquire ulp context, so data may "
2827                             "not be released.\n");
2828                 return;
2829         }
2830
2831         data = (struct bnxt_ulp_mapper_data *)
2832                 bnxt_ulp_cntxt_ptr2_mapper_data_get(ulp_ctx);
2833         if (!data) {
2834                 /* Go ahead and return since there is no allocated data. */
2835                 BNXT_TF_DBG(ERR, "No data appears to have been allocated.\n");
2836                 return;
2837         }
2838
2839         tfp = bnxt_ulp_cntxt_tfp_get(ulp_ctx);
2840         if (!tfp) {
2841                 BNXT_TF_DBG(ERR, "Failed to acquire tfp.\n");
2842                 /* Free the mapper data regardless of errors. */
2843                 goto free_mapper_data;
2844         }
2845
2846         /* Free the global resource info table entries */
2847         ulp_mapper_glb_resource_info_deinit(ulp_ctx, data);
2848
2849 free_mapper_data:
2850         /* Free the ulp cache tables */
2851         for (i = 0; i < BNXT_ULP_CACHE_TBL_MAX_SZ; i++) {
2852                 rte_free(data->cache_tbl[i]);
2853                 data->cache_tbl[i] = NULL;
2854         }
2855
2856         rte_free(data);
2857         /* Reset the data pointer within the ulp_ctx. */
2858         bnxt_ulp_cntxt_ptr2_mapper_data_set(ulp_ctx, NULL);
2859 }