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